Commit 30a69000 authored by Bart Van Assche's avatar Bart Van Assche Committed by Greg Kroah-Hartman

sysfs: fix discrepancies between implementation and documentation

Fix all discrepancies I know of between the sysfs implementation and its
documentation.
Signed-off-by: default avatarBart Van Assche <bart.vanassche@gmail.com>
Cc: Randy Dunlap <randy.dunlap@oracle.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent e3ed249a
...@@ -4,7 +4,7 @@ sysfs - _The_ filesystem for exporting kernel objects. ...@@ -4,7 +4,7 @@ sysfs - _The_ filesystem for exporting kernel objects.
Patrick Mochel <mochel@osdl.org> Patrick Mochel <mochel@osdl.org>
Mike Murphy <mamurph@cs.clemson.edu> Mike Murphy <mamurph@cs.clemson.edu>
Revised: 22 February 2009 Revised: 10 July 2010
Original: 10 January 2003 Original: 10 January 2003
...@@ -124,7 +124,7 @@ show and store methods of the attribute owners. ...@@ -124,7 +124,7 @@ show and store methods of the attribute owners.
struct sysfs_ops { struct sysfs_ops {
ssize_t (*show)(struct kobject *, struct attribute *, char *); ssize_t (*show)(struct kobject *, struct attribute *, char *);
ssize_t (*store)(struct kobject *, struct attribute *, const char *); ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
}; };
[ Subsystems should have already defined a struct kobj_type as a [ Subsystems should have already defined a struct kobj_type as a
...@@ -139,18 +139,22 @@ calls the associated methods. ...@@ -139,18 +139,22 @@ calls the associated methods.
To illustrate: To illustrate:
#define to_dev(obj) container_of(obj, struct device, kobj)
#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
#define to_dev(d) container_of(d, struct device, kobj)
static ssize_t static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) char *buf)
{ {
struct device_attribute * dev_attr = to_dev_attr(attr); struct device_attribute *dev_attr = to_dev_attr(attr);
struct device * dev = to_dev(kobj); struct device *dev = to_dev(kobj);
ssize_t ret = 0; ssize_t ret = -EIO;
if (dev_attr->show) if (dev_attr->show)
ret = dev_attr->show(dev, buf); ret = dev_attr->show(dev, dev_attr, buf);
if (ret >= (ssize_t)PAGE_SIZE) {
print_symbol("dev_attr_show: %s returned bad count\n",
(unsigned long)dev_attr->show);
}
return ret; return ret;
} }
...@@ -163,10 +167,9 @@ To read or write attributes, show() or store() methods must be ...@@ -163,10 +167,9 @@ To read or write attributes, show() or store() methods must be
specified when declaring the attribute. The method types should be as specified when declaring the attribute. The method types should be as
simple as those defined for device attributes: simple as those defined for device attributes:
ssize_t (*show)(struct device * dev, struct device_attribute * attr, ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
char * buf); ssize_t (*store)(struct device *dev, struct device_attribute *attr,
ssize_t (*store)(struct device * dev, struct device_attribute * attr, const char *buf, size_t count);
const char * buf);
IOW, they should take only an object, an attribute, and a buffer as parameters. IOW, they should take only an object, an attribute, and a buffer as parameters.
...@@ -209,8 +212,8 @@ Other notes: ...@@ -209,8 +212,8 @@ Other notes:
- show() should always use snprintf(). - show() should always use snprintf().
- store() should return the number of bytes used from the buffer. This - store() should return the number of bytes used from the buffer. If the
can be done using strlen(). entire buffer has been used, just return the count argument.
- show() or store() can always return errors. If a bad value comes - show() or store() can always return errors. If a bad value comes
through, be sure to return an error. through, be sure to return an error.
...@@ -223,15 +226,18 @@ Other notes: ...@@ -223,15 +226,18 @@ Other notes:
A very simple (and naive) implementation of a device attribute is: A very simple (and naive) implementation of a device attribute is:
static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf) static ssize_t show_name(struct device *dev, struct device_attribute *attr,
char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%s\n", dev->name); return snprintf(buf, PAGE_SIZE, "%s\n", dev->name);
} }
static ssize_t store_name(struct device * dev, const char * buf) static ssize_t store_name(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{ {
sscanf(buf, "%20s", dev->name); snprintf(dev->name, sizeof(dev->name), "%.*s",
return strnlen(buf, PAGE_SIZE); (int)min(count, sizeof(dev->name) - 1), buf);
return count;
} }
static DEVICE_ATTR(name, S_IRUGO, show_name, store_name); static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment