ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / Documentation / filesystems / sysfs.txt
blob7e81e37c0b1ec0ccbea30a92ffbf70f495ffdbd0
2 sysfs - _The_ filesystem for exporting kernel objects. 
4 Patrick Mochel  <mochel@osdl.org>
5 Mike Murphy <mamurph@cs.clemson.edu>
7 Revised:    22 February 2009
8 Original:   10 January 2003
11 What it is:
12 ~~~~~~~~~~~
14 sysfs is a ram-based filesystem initially based on ramfs. It provides
15 a means to export kernel data structures, their attributes, and the 
16 linkages between them to userspace. 
18 sysfs is tied inherently to the kobject infrastructure. Please read
19 Documentation/kobject.txt for more information concerning the kobject
20 interface. 
23 Using sysfs
24 ~~~~~~~~~~~
26 sysfs is always compiled in. You can access it by doing:
28     mount -t sysfs sysfs /sys 
31 Directory Creation
32 ~~~~~~~~~~~~~~~~~~
34 For every kobject that is registered with the system, a directory is
35 created for it in sysfs. That directory is created as a subdirectory
36 of the kobject's parent, expressing internal object hierarchies to
37 userspace. Top-level directories in sysfs represent the common
38 ancestors of object hierarchies; i.e. the subsystems the objects
39 belong to. 
41 Sysfs internally stores the kobject that owns the directory in the
42 ->d_fsdata pointer of the directory's dentry. This allows sysfs to do
43 reference counting directly on the kobject when the file is opened and
44 closed. 
47 Attributes
48 ~~~~~~~~~~
50 Attributes can be exported for kobjects in the form of regular files in
51 the filesystem. Sysfs forwards file I/O operations to methods defined
52 for the attributes, providing a means to read and write kernel
53 attributes.
55 Attributes should be ASCII text files, preferably with only one value
56 per file. It is noted that it may not be efficient to contain only one
57 value per file, so it is socially acceptable to express an array of
58 values of the same type. 
60 Mixing types, expressing multiple lines of data, and doing fancy
61 formatting of data is heavily frowned upon. Doing these things may get
62 you publically humiliated and your code rewritten without notice. 
65 An attribute definition is simply:
67 struct attribute {
68         char                    * name;
69         struct module           *owner;
70         mode_t                  mode;
74 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr);
75 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr);
78 A bare attribute contains no means to read or write the value of the
79 attribute. Subsystems are encouraged to define their own attribute
80 structure and wrapper functions for adding and removing attributes for
81 a specific object type. 
83 For example, the driver model defines struct device_attribute like:
85 struct device_attribute {
86         struct attribute        attr;
87         ssize_t (*show)(struct device *dev, struct device_attribute *attr,
88                         char *buf);
89         ssize_t (*store)(struct device *dev, struct device_attribute *attr,
90                          const char *buf, size_t count);
93 int device_create_file(struct device *, struct device_attribute *);
94 void device_remove_file(struct device *, struct device_attribute *);
96 It also defines this helper for defining device attributes: 
98 #define DEVICE_ATTR(_name, _mode, _show, _store) \
99 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
101 For example, declaring
103 static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo);
105 is equivalent to doing:
107 static struct device_attribute dev_attr_foo = {
108        .attr    = {
109                 .name = "foo",
110                 .mode = S_IWUSR | S_IRUGO,
111                 .show = show_foo,
112                 .store = store_foo,
113         },
117 Subsystem-Specific Callbacks
118 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120 When a subsystem defines a new attribute type, it must implement a
121 set of sysfs operations for forwarding read and write calls to the
122 show and store methods of the attribute owners. 
124 struct sysfs_ops {
125         ssize_t (*show)(struct kobject *, struct attribute *, char *);
126         ssize_t (*store)(struct kobject *, struct attribute *, const char *);
129 [ Subsystems should have already defined a struct kobj_type as a
130 descriptor for this type, which is where the sysfs_ops pointer is
131 stored. See the kobject documentation for more information. ]
133 When a file is read or written, sysfs calls the appropriate method
134 for the type. The method then translates the generic struct kobject
135 and struct attribute pointers to the appropriate pointer types, and
136 calls the associated methods. 
139 To illustrate:
141 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
142 #define to_dev(d) container_of(d, struct device, kobj)
144 static ssize_t
145 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
147         struct device_attribute * dev_attr = to_dev_attr(attr);
148         struct device * dev = to_dev(kobj);
149         ssize_t ret = 0;
151         if (dev_attr->show)
152                 ret = dev_attr->show(dev, buf);
153         return ret;
158 Reading/Writing Attribute Data
159 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161 To read or write attributes, show() or store() methods must be
162 specified when declaring the attribute. The method types should be as
163 simple as those defined for device attributes:
165 ssize_t (*show)(struct device * dev, struct device_attribute * attr,
166                 char * buf);
167 ssize_t (*store)(struct device * dev, struct device_attribute * attr,
168                  const char * buf);
170 IOW, they should take only an object, an attribute, and a buffer as parameters.
173 sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the
174 method. Sysfs will call the method exactly once for each read or
175 write. This forces the following behavior on the method
176 implementations: 
178 - On read(2), the show() method should fill the entire buffer. 
179   Recall that an attribute should only be exporting one value, or an
180   array of similar values, so this shouldn't be that expensive. 
182   This allows userspace to do partial reads and forward seeks
183   arbitrarily over the entire file at will. If userspace seeks back to
184   zero or does a pread(2) with an offset of '0' the show() method will
185   be called again, rearmed, to fill the buffer.
187 - On write(2), sysfs expects the entire buffer to be passed during the
188   first write. Sysfs then passes the entire buffer to the store()
189   method. 
190   
191   When writing sysfs files, userspace processes should first read the
192   entire file, modify the values it wishes to change, then write the
193   entire buffer back. 
195   Attribute method implementations should operate on an identical
196   buffer when reading and writing values. 
198 Other notes:
200 - Writing causes the show() method to be rearmed regardless of current
201   file position.
203 - The buffer will always be PAGE_SIZE bytes in length. On i386, this
204   is 4096. 
206 - show() methods should return the number of bytes printed into the
207   buffer. This is the return value of snprintf().
209 - show() should always use snprintf(). 
211 - store() should return the number of bytes used from the buffer. This
212   can be done using strlen().
214 - show() or store() can always return errors. If a bad value comes
215   through, be sure to return an error.
217 - The object passed to the methods will be pinned in memory via sysfs
218   referencing counting its embedded object. However, the physical 
219   entity (e.g. device) the object represents may not be present. Be 
220   sure to have a way to check this, if necessary. 
223 A very simple (and naive) implementation of a device attribute is:
225 static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
227         return snprintf(buf, PAGE_SIZE, "%s\n", dev->name);
230 static ssize_t store_name(struct device * dev, const char * buf)
232         sscanf(buf, "%20s", dev->name);
233         return strnlen(buf, PAGE_SIZE);
236 static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
239 (Note that the real implementation doesn't allow userspace to set the 
240 name for a device.)
243 Top Level Directory Layout
244 ~~~~~~~~~~~~~~~~~~~~~~~~~~
246 The sysfs directory arrangement exposes the relationship of kernel
247 data structures. 
249 The top level sysfs directory looks like:
251 block/
252 bus/
253 class/
254 dev/
255 devices/
256 firmware/
257 net/
260 devices/ contains a filesystem representation of the device tree. It maps
261 directly to the internal kernel device tree, which is a hierarchy of
262 struct device. 
264 bus/ contains flat directory layout of the various bus types in the
265 kernel. Each bus's directory contains two subdirectories:
267         devices/
268         drivers/
270 devices/ contains symlinks for each device discovered in the system
271 that point to the device's directory under root/.
273 drivers/ contains a directory for each device driver that is loaded
274 for devices on that particular bus (this assumes that drivers do not
275 span multiple bus types).
277 fs/ contains a directory for some filesystems.  Currently each
278 filesystem wanting to export attributes must create its own hierarchy
279 below fs/ (see ./fuse.txt for an example).
281 dev/ contains two directories char/ and block/. Inside these two
282 directories there are symlinks named <major>:<minor>.  These symlinks
283 point to the sysfs directory for the given device.  /sys/dev provides a
284 quick way to lookup the sysfs interface for a device from the result of
285 a stat(2) operation.
287 More information can driver-model specific features can be found in
288 Documentation/driver-model/. 
291 TODO: Finish this section.
294 Current Interfaces
295 ~~~~~~~~~~~~~~~~~~
297 The following interface layers currently exist in sysfs:
300 - devices (include/linux/device.h)
301 ----------------------------------
302 Structure:
304 struct device_attribute {
305         struct attribute        attr;
306         ssize_t (*show)(struct device *dev, struct device_attribute *attr,
307                         char *buf);
308         ssize_t (*store)(struct device *dev, struct device_attribute *attr,
309                          const char *buf, size_t count);
312 Declaring:
314 DEVICE_ATTR(_name, _mode, _show, _store);
316 Creation/Removal:
318 int device_create_file(struct device *device, struct device_attribute * attr);
319 void device_remove_file(struct device * dev, struct device_attribute * attr);
322 - bus drivers (include/linux/device.h)
323 --------------------------------------
324 Structure:
326 struct bus_attribute {
327         struct attribute        attr;
328         ssize_t (*show)(struct bus_type *, char * buf);
329         ssize_t (*store)(struct bus_type *, const char * buf);
332 Declaring:
334 BUS_ATTR(_name, _mode, _show, _store)
336 Creation/Removal:
338 int bus_create_file(struct bus_type *, struct bus_attribute *);
339 void bus_remove_file(struct bus_type *, struct bus_attribute *);
342 - device drivers (include/linux/device.h)
343 -----------------------------------------
345 Structure:
347 struct driver_attribute {
348         struct attribute        attr;
349         ssize_t (*show)(struct device_driver *, char * buf);
350         ssize_t (*store)(struct device_driver *, const char * buf,
351                          size_t count);
354 Declaring:
356 DRIVER_ATTR(_name, _mode, _show, _store)
358 Creation/Removal:
360 int driver_create_file(struct device_driver *, struct driver_attribute *);
361 void driver_remove_file(struct device_driver *, struct driver_attribute *);