aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/filesystems/sysfs.txt
diff options
context:
space:
mode:
authorBart Van Assche <bvanassche@acm.org>2010-07-20 18:22:05 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2010-08-05 16:53:34 -0400
commit30a69000a4ba9cf49e8b826431847cc80881b59b (patch)
treea0fe01bb1f5256046c4ab86d75bc6777fa821bc5 /Documentation/filesystems/sysfs.txt
parente3ed249af8cb2f73fc6ef5494d2ddef43fb0ff19 (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/sysfs.txt')
-rw-r--r--Documentation/filesystems/sysfs.txt44
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.
4Patrick Mochel <mochel@osdl.org> 4Patrick Mochel <mochel@osdl.org>
5Mike Murphy <mamurph@cs.clemson.edu> 5Mike Murphy <mamurph@cs.clemson.edu>
6 6
7Revised: 22 February 2009 7Revised: 10 July 2010
8Original: 10 January 2003 8Original: 10 January 2003
9 9
10 10
@@ -124,7 +124,7 @@ show and store methods of the attribute owners.
124 124
125struct sysfs_ops { 125struct 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
140To illustrate: 140To 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
145static ssize_t 145static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
146dev_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
163specified when declaring the attribute. The method types should be as 167specified when declaring the attribute. The method types should be as
164simple as those defined for device attributes: 168simple as those defined for device attributes:
165 169
166ssize_t (*show)(struct device * dev, struct device_attribute * attr, 170ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
167 char * buf); 171ssize_t (*store)(struct device *dev, struct device_attribute *attr,
168ssize_t (*store)(struct device * dev, struct device_attribute * attr, 172 const char *buf, size_t count);
169 const char * buf);
170 173
171IOW, they should take only an object, an attribute, and a buffer as parameters. 174IOW, 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
224A very simple (and naive) implementation of a device attribute is: 227A very simple (and naive) implementation of a device attribute is:
225 228
226static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf) 229static 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
231static ssize_t store_name(struct device * dev, const char * buf) 235static 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
237static DEVICE_ATTR(name, S_IRUGO, show_name, store_name); 243static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);