diff options
author | Bart Van Assche <bvanassche@acm.org> | 2010-07-20 18:22:05 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2010-08-05 16:53:34 -0400 |
commit | 30a69000a4ba9cf49e8b826431847cc80881b59b (patch) | |
tree | a0fe01bb1f5256046c4ab86d75bc6777fa821bc5 /Documentation/filesystems | |
parent | e3ed249af8cb2f73fc6ef5494d2ddef43fb0ff19 (diff) |
sysfs: fix discrepancies between implementation and documentation
Fix all discrepancies I know of between the sysfs implementation and its
documentation.
Signed-off-by: Bart Van Assche <bart.vanassche@gmail.com>
Cc: Randy Dunlap <randy.dunlap@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'Documentation/filesystems')
-rw-r--r-- | Documentation/filesystems/sysfs.txt | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/Documentation/filesystems/sysfs.txt b/Documentation/filesystems/sysfs.txt index 931c806642c5..d78ed0bb2756 100644 --- a/Documentation/filesystems/sysfs.txt +++ b/Documentation/filesystems/sysfs.txt | |||
@@ -4,7 +4,7 @@ sysfs - _The_ filesystem for exporting kernel objects. | |||
4 | Patrick Mochel <mochel@osdl.org> | 4 | Patrick Mochel <mochel@osdl.org> |
5 | Mike Murphy <mamurph@cs.clemson.edu> | 5 | Mike Murphy <mamurph@cs.clemson.edu> |
6 | 6 | ||
7 | Revised: 22 February 2009 | 7 | Revised: 10 July 2010 |
8 | Original: 10 January 2003 | 8 | Original: 10 January 2003 |
9 | 9 | ||
10 | 10 | ||
@@ -124,7 +124,7 @@ show and store methods of the attribute owners. | |||
124 | 124 | ||
125 | struct sysfs_ops { | 125 | struct sysfs_ops { |
126 | ssize_t (*show)(struct kobject *, struct attribute *, char *); | 126 | ssize_t (*show)(struct kobject *, struct attribute *, char *); |
127 | ssize_t (*store)(struct kobject *, struct attribute *, const char *); | 127 | ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); |
128 | }; | 128 | }; |
129 | 129 | ||
130 | [ Subsystems should have already defined a struct kobj_type as a | 130 | [ Subsystems should have already defined a struct kobj_type as a |
@@ -139,18 +139,22 @@ calls the associated methods. | |||
139 | 139 | ||
140 | To illustrate: | 140 | To illustrate: |
141 | 141 | ||
142 | #define to_dev(obj) container_of(obj, struct device, kobj) | ||
142 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) | 143 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
143 | #define to_dev(d) container_of(d, struct device, kobj) | ||
144 | 144 | ||
145 | static ssize_t | 145 | static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, |
146 | dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | 146 | char *buf) |
147 | { | 147 | { |
148 | struct device_attribute * dev_attr = to_dev_attr(attr); | 148 | struct device_attribute *dev_attr = to_dev_attr(attr); |
149 | struct device * dev = to_dev(kobj); | 149 | struct device *dev = to_dev(kobj); |
150 | ssize_t ret = 0; | 150 | ssize_t ret = -EIO; |
151 | 151 | ||
152 | if (dev_attr->show) | 152 | if (dev_attr->show) |
153 | ret = dev_attr->show(dev, buf); | 153 | ret = dev_attr->show(dev, dev_attr, buf); |
154 | if (ret >= (ssize_t)PAGE_SIZE) { | ||
155 | print_symbol("dev_attr_show: %s returned bad count\n", | ||
156 | (unsigned long)dev_attr->show); | ||
157 | } | ||
154 | return ret; | 158 | return ret; |
155 | } | 159 | } |
156 | 160 | ||
@@ -163,10 +167,9 @@ To read or write attributes, show() or store() methods must be | |||
163 | specified when declaring the attribute. The method types should be as | 167 | specified when declaring the attribute. The method types should be as |
164 | simple as those defined for device attributes: | 168 | simple as those defined for device attributes: |
165 | 169 | ||
166 | ssize_t (*show)(struct device * dev, struct device_attribute * attr, | 170 | ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf); |
167 | char * buf); | 171 | ssize_t (*store)(struct device *dev, struct device_attribute *attr, |
168 | ssize_t (*store)(struct device * dev, struct device_attribute * attr, | 172 | const char *buf, size_t count); |
169 | const char * buf); | ||
170 | 173 | ||
171 | IOW, they should take only an object, an attribute, and a buffer as parameters. | 174 | IOW, they should take only an object, an attribute, and a buffer as parameters. |
172 | 175 | ||
@@ -209,8 +212,8 @@ Other notes: | |||
209 | 212 | ||
210 | - show() should always use snprintf(). | 213 | - show() should always use snprintf(). |
211 | 214 | ||
212 | - store() should return the number of bytes used from the buffer. This | 215 | - store() should return the number of bytes used from the buffer. If the |
213 | can be done using strlen(). | 216 | entire buffer has been used, just return the count argument. |
214 | 217 | ||
215 | - show() or store() can always return errors. If a bad value comes | 218 | - show() or store() can always return errors. If a bad value comes |
216 | through, be sure to return an error. | 219 | through, be sure to return an error. |
@@ -223,15 +226,18 @@ Other notes: | |||
223 | 226 | ||
224 | A very simple (and naive) implementation of a device attribute is: | 227 | A very simple (and naive) implementation of a device attribute is: |
225 | 228 | ||
226 | static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf) | 229 | static ssize_t show_name(struct device *dev, struct device_attribute *attr, |
230 | char *buf) | ||
227 | { | 231 | { |
228 | return snprintf(buf, PAGE_SIZE, "%s\n", dev->name); | 232 | return snprintf(buf, PAGE_SIZE, "%s\n", dev->name); |
229 | } | 233 | } |
230 | 234 | ||
231 | static ssize_t store_name(struct device * dev, const char * buf) | 235 | static ssize_t store_name(struct device *dev, struct device_attribute *attr, |
236 | const char *buf, size_t count) | ||
232 | { | 237 | { |
233 | sscanf(buf, "%20s", dev->name); | 238 | snprintf(dev->name, sizeof(dev->name), "%.*s", |
234 | return strnlen(buf, PAGE_SIZE); | 239 | (int)min(count, sizeof(dev->name) - 1), buf); |
240 | return count; | ||
235 | } | 241 | } |
236 | 242 | ||
237 | static DEVICE_ATTR(name, S_IRUGO, show_name, store_name); | 243 | static DEVICE_ATTR(name, S_IRUGO, show_name, store_name); |