sysfs is a ram-based filesystem initially based on ramfs. It provides a means to export kernel data structures, their attributes, and the linkages between them to userspace.
sysfs is tied inherently to the kobject infrastructure. Please read Documentation/core-api/kobject.rst for more information concerning the kobject interface.
使用 sysfs
如果定义了 CONFIG_SYSFS,sysfs 总是被编译进去。你可以通过以下命令访问它:
1 2 3 4
sysfs is always compiled in if CONFIG_SYSFS is defined. You can access it by doing::
For every kobject that is registered with the system, a directory is created for it in sysfs. That directory is created as a subdirectory of the kobject's parent, expressing internal object hierarchies to userspace. Top-level directories in sysfs represent the common ancestors of object hierarchies; i.e. the subsystems the objects belong to.
Attributes can be exported for kobjects in the form of regular files in the filesystem. Sysfs forwards file I/O operations to methods defined for the attributes, providing a means to read and write kernel attributes.
Attributes should be ASCII text files, preferably with only one value per file. It is noted that it may not be efficient to contain only one value per file, so it is socially acceptable to express an array of values of the same type.
Mixing types, expressing multiple lines of data, and doing fancy formatting of data is heavily frowned upon. Doing these things may get you publicly humiliated and your code rewritten without notice.
A bare attribute contains no means to read or write the value of the attribute. Subsystems are encouraged to define their own attribute structure and wrapper functions for adding and removing attributes for a specific object type.
例如,驱动模型定义了 struct device_attribute 如下:
1 2 3 4 5 6 7 8 9 10 11 12
For example, the driver model defines structdevice_attributelike::
Note as stated in include/linux/kernel.h "OTHER_WRITABLE? Generally considered a bad idea." so trying to set a sysfs file writable for everyone will fail reverting to RO mode for"Others".
For the common cases sysfs.h provides convenience macros to make defining attributes easier as well as making code more concise and readable. The above case could be shortened to:
the list of helpers available to define your wrapper function is:
__ATTR_RO(name): assumes default name_show and mode 0444 __ATTR_WO(name): assumes a name_store only and is restricted to mode 0200 that is root write access only. __ATTR_RO_MODE(name, mode): fore more restrictive RO access currently only use case is the EFI System Resource Table (see drivers/firmware/efi/esrt.c) __ATTR_RW(name): assumes default name_show, name_store and setting mode to 0644. __ATTR_NULL: which sets the name to NULL and is used as end of list indicator (see: kernel/workqueue.c)
子系统特定的回调
当子系统定义一个新的属性类型时,它必须实现一组 sysfs 操作,用于将读写调用转发给属性所有者的 show 和 store 方法:
When a subsystem defines a new attribute type, it must implement a set of sysfs operations for forwarding read and write calls to the show and store methods of the attribute owners::
[ Subsystems should have already defined a struct kobj_type as a descriptor for this type, which is where the sysfs_ops pointer is stored. See the kobject documentation for more information. ]
When a file is read or written, sysfs calls the appropriate method for the type. The method then translates the generic struct kobject and struct attribute pointers to the appropriate pointer types, and calls the associated methods.
if (dev_attr->show) ret = dev_attr->show(dev, dev_attr, buf); if (ret >= (ssize_t)PAGE_SIZE) { printk("dev_attr_show: %pS returned bad count\n", dev_attr->show); } return ret; }
Reading/Writing Attribute Data ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To read or write attributes, show() or store() methods must be specified when declaring the attribute. The method types should be as simple as those defined for device attributes::
sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the method. Sysfs will call the method exactly once for each read or write. This forces the following behavior on the method implementations:
- On read(2), the show() method should fill the entire buffer. Recall that an attribute should only be exporting one value, or an array of similar values, so this shouldn't be that expensive.
This allows userspace to do partial reads and forward seeks arbitrarily over the entire file at will. If userspace seeks back to zero or does a pread(2) with an offset of '0' the show() method will be called again, rearmed, to fill the buffer.
- On write(2), sysfs expects the entire buffer to be passed during the first write. Sysfs then passes the entire buffer to the store() method. A terminating null is added after the data on stores. This makes functions like sysfs_streq() safe to use.
When writing sysfs files, userspace processes should first read the entire file, modify the values it wishes to change, then write the entire buffer back.
属性方法实现应该在读取和写入值时操作相同的缓冲区。
1 2
Attribute method implementations should operate on an identical buffer when reading and writing values.
其他注意事项:
1
Other notes:
写入会导致 show() 方法重新激活,无论当前文件位置如何。
1 2
- Writing causes the show() method to be rearmed regardless of current file position.
- The object passed to the methods will be pinned in memory via sysfs referencing counting its embedded object. However, the physical entity (e.g. device) the object represents may not be present. Be sure to have a way to check this, if necessary.
一个非常简单(且天真的)设备属性实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
A very simple(and naive) implementation of a device attribute is::
devices/ contains a filesystem representation of the device tree. It maps directly to the internal kernel device tree, which is a hierarchy of struct device.
bus/ 包含各种总线类型的扁平目录布局。每个总线的目录包含两个子目录:
1 2 3 4 5
bus/ contains flat directory layout of the various bus types in the kernel. Each bus's directory contains two subdirectories::
devices/ drivers/
devices/ 包含系统中发现的每个设备的符号链接,指向设备在根目录下的目录。
1 2
devices/ contains symlinks for each device discovered in the system that point to the device's directory under root/.
drivers/ contains a directory for each device driver that is loaded for devices on that particular bus (this assumes that drivers do not span multiple bus types).
fs/ contains a directory for some filesystems. Currently each filesystem wanting to export attributes must create its own hierarchy below fs/ (see ./fuse.txt for an example).
dev/ contains two directories char/ and block/. Inside these two directories there are symlinks named <major>:<minor>. These symlinks point to the sysfs directory for the given device. /sys/dev provides a quick way to lookup the sysfs interface for a device from the result of a stat(2) operation.
sysfs 目录结构和每个目录中的属性定义了内核与用户空间之间的 ABI。对于任何 ABI 来说,确保其稳定性和正确文档化是非常重要的。所有新的 sysfs 属性必须在 Documentation/ABI 中进行文档化。有关更多信息,请参阅 Documentation/ABI/README。
1 2 3 4 5 6 7 8
Documentation ~~~~~~~~~~~~~
The sysfs directory structure and the attributes in each directory define an ABI between the kernel and user space. As for any ABI, it is important that this ABI is stable and properly documented. All new sysfs attributes must be documented in Documentation/ABI. See also Documentation/ABI/README for more information.