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
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
26 sysfs is always compiled in. You can access it by doing:
28 mount -t sysfs sysfs /sys
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
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
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
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:
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,
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 = {
110 .mode = S_IWUSR | S_IRUGO,
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.
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.
141 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
142 #define to_dev(d) container_of(d, struct device, kobj)
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);
152 ret = dev_attr->show(dev, buf);
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,
167 ssize_t (*store)(struct device * dev, struct device_attribute * attr,
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
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()
191 When writing sysfs files, userspace processes should first read the
192 entire file, modify the values it wishes to change, then write the
195 Attribute method implementations should operate on an identical
196 buffer when reading and writing values.
200 - Writing causes the show() method to be rearmed regardless of current
203 - The buffer will always be PAGE_SIZE bytes in length. On i386, this
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
243 Top Level Directory Layout
244 ~~~~~~~~~~~~~~~~~~~~~~~~~~
246 The sysfs directory arrangement exposes the relationship of kernel
249 The top level sysfs directory looks like:
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
264 bus/ contains flat directory layout of the various bus types in the
265 kernel. Each bus's directory contains two subdirectories:
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
287 More information can driver-model specific features can be found in
288 Documentation/driver-model/.
291 TODO: Finish this section.
297 The following interface layers currently exist in sysfs:
300 - devices (include/linux/device.h)
301 ----------------------------------
304 struct device_attribute {
305 struct attribute attr;
306 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
308 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
309 const char *buf, size_t count);
314 DEVICE_ATTR(_name, _mode, _show, _store);
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 --------------------------------------
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);
334 BUS_ATTR(_name, _mode, _show, _store)
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 -----------------------------------------
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,
356 DRIVER_ATTR(_name, _mode, _show, _store)
360 int driver_create_file(struct device_driver *, struct driver_attribute *);
361 void driver_remove_file(struct device_driver *, struct driver_attribute *);