1 .. SPDX-License-Identifier: GPL-2.0
3 =====================================================
4 sysfs - _The_ filesystem for exporting kernel objects
5 =====================================================
7 Patrick Mochel <mochel@osdl.org>
9 Mike Murphy <mamurph@cs.clemson.edu>
11 :Revised: 16 August 2011
12 :Original: 10 January 2003
18 sysfs is a ram-based filesystem initially based on ramfs. It provides
19 a means to export kernel data structures, their attributes, and the
20 linkages between them to userspace.
22 sysfs is tied inherently to the kobject infrastructure. Please read
23 Documentation/kobject.txt for more information concerning the kobject
30 sysfs is always compiled in if CONFIG_SYSFS is defined. You can access
33 mount -t sysfs sysfs /sys
39 For every kobject that is registered with the system, a directory is
40 created for it in sysfs. That directory is created as a subdirectory
41 of the kobject's parent, expressing internal object hierarchies to
42 userspace. Top-level directories in sysfs represent the common
43 ancestors of object hierarchies; i.e. the subsystems the objects
46 Sysfs internally stores a pointer to the kobject that implements a
47 directory in the kernfs_node object associated with the directory. In
48 the past this kobject pointer has been used by sysfs to do reference
49 counting directly on the kobject whenever the file is opened or closed.
50 With the current sysfs implementation the kobject reference count is
51 only modified directly by the function sysfs_schedule_callback().
57 Attributes can be exported for kobjects in the form of regular files in
58 the filesystem. Sysfs forwards file I/O operations to methods defined
59 for the attributes, providing a means to read and write kernel
62 Attributes should be ASCII text files, preferably with only one value
63 per file. It is noted that it may not be efficient to contain only one
64 value per file, so it is socially acceptable to express an array of
65 values of the same type.
67 Mixing types, expressing multiple lines of data, and doing fancy
68 formatting of data is heavily frowned upon. Doing these things may get
69 you publicly humiliated and your code rewritten without notice.
72 An attribute definition is simply::
81 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr);
82 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr);
85 A bare attribute contains no means to read or write the value of the
86 attribute. Subsystems are encouraged to define their own attribute
87 structure and wrapper functions for adding and removing attributes for
88 a specific object type.
90 For example, the driver model defines struct device_attribute like::
92 struct device_attribute {
93 struct attribute attr;
94 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
96 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
97 const char *buf, size_t count);
100 int device_create_file(struct device *, const struct device_attribute *);
101 void device_remove_file(struct device *, const struct device_attribute *);
103 It also defines this helper for defining device attributes::
105 #define DEVICE_ATTR(_name, _mode, _show, _store) \
106 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
108 For example, declaring::
110 static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo);
112 is equivalent to doing::
114 static struct device_attribute dev_attr_foo = {
117 .mode = S_IWUSR | S_IRUGO,
123 Note as stated in include/linux/kernel.h "OTHER_WRITABLE? Generally
124 considered a bad idea." so trying to set a sysfs file writable for
125 everyone will fail reverting to RO mode for "Others".
127 For the common cases sysfs.h provides convenience macros to make
128 defining attributes easier as well as making code more concise and
129 readable. The above case could be shortened to:
131 static struct device_attribute dev_attr_foo = __ATTR_RW(foo);
133 the list of helpers available to define your wrapper function is:
136 assumes default name_show and mode 0444
138 assumes a name_store only and is restricted to mode
139 0200 that is root write access only.
140 __ATTR_RO_MODE(name, mode):
141 fore more restrictive RO access currently
142 only use case is the EFI System Resource Table
143 (see drivers/firmware/efi/esrt.c)
145 assumes default name_show, name_store and setting
148 which sets the name to NULL and is used as end of list
149 indicator (see: kernel/workqueue.c)
151 Subsystem-Specific Callbacks
152 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154 When a subsystem defines a new attribute type, it must implement a
155 set of sysfs operations for forwarding read and write calls to the
156 show and store methods of the attribute owners::
159 ssize_t (*show)(struct kobject *, struct attribute *, char *);
160 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
163 [ Subsystems should have already defined a struct kobj_type as a
164 descriptor for this type, which is where the sysfs_ops pointer is
165 stored. See the kobject documentation for more information. ]
167 When a file is read or written, sysfs calls the appropriate method
168 for the type. The method then translates the generic struct kobject
169 and struct attribute pointers to the appropriate pointer types, and
170 calls the associated methods.
175 #define to_dev(obj) container_of(obj, struct device, kobj)
176 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
178 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
181 struct device_attribute *dev_attr = to_dev_attr(attr);
182 struct device *dev = to_dev(kobj);
186 ret = dev_attr->show(dev, dev_attr, buf);
187 if (ret >= (ssize_t)PAGE_SIZE) {
188 printk("dev_attr_show: %pS returned bad count\n",
196 Reading/Writing Attribute Data
197 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
199 To read or write attributes, show() or store() methods must be
200 specified when declaring the attribute. The method types should be as
201 simple as those defined for device attributes::
203 ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
204 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
205 const char *buf, size_t count);
207 IOW, they should take only an object, an attribute, and a buffer as parameters.
210 sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the
211 method. Sysfs will call the method exactly once for each read or
212 write. This forces the following behavior on the method
215 - On read(2), the show() method should fill the entire buffer.
216 Recall that an attribute should only be exporting one value, or an
217 array of similar values, so this shouldn't be that expensive.
219 This allows userspace to do partial reads and forward seeks
220 arbitrarily over the entire file at will. If userspace seeks back to
221 zero or does a pread(2) with an offset of '0' the show() method will
222 be called again, rearmed, to fill the buffer.
224 - On write(2), sysfs expects the entire buffer to be passed during the
225 first write. Sysfs then passes the entire buffer to the store() method.
226 A terminating null is added after the data on stores. This makes
227 functions like sysfs_streq() safe to use.
229 When writing sysfs files, userspace processes should first read the
230 entire file, modify the values it wishes to change, then write the
233 Attribute method implementations should operate on an identical
234 buffer when reading and writing values.
238 - Writing causes the show() method to be rearmed regardless of current
241 - The buffer will always be PAGE_SIZE bytes in length. On i386, this
244 - show() methods should return the number of bytes printed into the
245 buffer. This is the return value of scnprintf().
247 - show() must not use snprintf() when formatting the value to be
248 returned to user space. If you can guarantee that an overflow
249 will never happen you can use sprintf() otherwise you must use
252 - store() should return the number of bytes used from the buffer. If the
253 entire buffer has been used, just return the count argument.
255 - show() or store() can always return errors. If a bad value comes
256 through, be sure to return an error.
258 - The object passed to the methods will be pinned in memory via sysfs
259 referencing counting its embedded object. However, the physical
260 entity (e.g. device) the object represents may not be present. Be
261 sure to have a way to check this, if necessary.
264 A very simple (and naive) implementation of a device attribute is::
266 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
269 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->name);
272 static ssize_t store_name(struct device *dev, struct device_attribute *attr,
273 const char *buf, size_t count)
275 snprintf(dev->name, sizeof(dev->name), "%.*s",
276 (int)min(count, sizeof(dev->name) - 1), buf);
280 static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
283 (Note that the real implementation doesn't allow userspace to set the
287 Top Level Directory Layout
288 ~~~~~~~~~~~~~~~~~~~~~~~~~~
290 The sysfs directory arrangement exposes the relationship of kernel
293 The top level sysfs directory looks like::
304 devices/ contains a filesystem representation of the device tree. It maps
305 directly to the internal kernel device tree, which is a hierarchy of
308 bus/ contains flat directory layout of the various bus types in the
309 kernel. Each bus's directory contains two subdirectories::
314 devices/ contains symlinks for each device discovered in the system
315 that point to the device's directory under root/.
317 drivers/ contains a directory for each device driver that is loaded
318 for devices on that particular bus (this assumes that drivers do not
319 span multiple bus types).
321 fs/ contains a directory for some filesystems. Currently each
322 filesystem wanting to export attributes must create its own hierarchy
323 below fs/ (see ./fuse.txt for an example).
325 dev/ contains two directories char/ and block/. Inside these two
326 directories there are symlinks named <major>:<minor>. These symlinks
327 point to the sysfs directory for the given device. /sys/dev provides a
328 quick way to lookup the sysfs interface for a device from the result of
331 More information can driver-model specific features can be found in
332 Documentation/driver-api/driver-model/.
335 TODO: Finish this section.
341 The following interface layers currently exist in sysfs:
344 devices (include/linux/device.h)
345 --------------------------------
348 struct device_attribute {
349 struct attribute attr;
350 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
352 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
353 const char *buf, size_t count);
358 DEVICE_ATTR(_name, _mode, _show, _store);
362 int device_create_file(struct device *dev, const struct device_attribute * attr);
363 void device_remove_file(struct device *dev, const struct device_attribute * attr);
366 bus drivers (include/linux/device.h)
367 ------------------------------------
370 struct bus_attribute {
371 struct attribute attr;
372 ssize_t (*show)(struct bus_type *, char * buf);
373 ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
378 static BUS_ATTR_RW(name);
379 static BUS_ATTR_RO(name);
380 static BUS_ATTR_WO(name);
384 int bus_create_file(struct bus_type *, struct bus_attribute *);
385 void bus_remove_file(struct bus_type *, struct bus_attribute *);
388 device drivers (include/linux/device.h)
389 ---------------------------------------
393 struct driver_attribute {
394 struct attribute attr;
395 ssize_t (*show)(struct device_driver *, char * buf);
396 ssize_t (*store)(struct device_driver *, const char * buf,
402 DRIVER_ATTR_RO(_name)
403 DRIVER_ATTR_RW(_name)
407 int driver_create_file(struct device_driver *, const struct driver_attribute *);
408 void driver_remove_file(struct device_driver *, const struct driver_attribute *);
414 The sysfs directory structure and the attributes in each directory define an
415 ABI between the kernel and user space. As for any ABI, it is important that
416 this ABI is stable and properly documented. All new sysfs attributes must be
417 documented in Documentation/ABI. See also Documentation/ABI/README for more