2 sysfs - _The_ filesystem for exporting kernel objects.
4 Patrick Mochel <mochel@osdl.org>
12 sysfs is a ram-based filesystem initially based on ramfs. It provides
13 a means to export kernel data structures, their attributes, and the
14 linkages between them to userspace.
16 sysfs is tied inherently to the kobject infrastructure. Please read
17 Documentation/kobject.txt for more information concerning the kobject
24 sysfs is always compiled in. You can access it by doing:
26 mount -t sysfs sysfs /sys
32 For every kobject that is registered with the system, a directory is
33 created for it in sysfs. That directory is created as a subdirectory
34 of the kobject's parent, expressing internal object hierarchies to
35 userspace. Top-level directories in sysfs represent the common
36 ancestors of object hierarchies; i.e. the subsystems the objects
39 Sysfs internally stores the kobject that owns the directory in the
40 ->d_fsdata pointer of the directory's dentry. This allows sysfs to do
41 reference counting directly on the kobject when the file is opened and
48 Attributes can be exported for kobjects in the form of regular files in
49 the filesystem. Sysfs forwards file I/O operations to methods defined
50 for the attributes, providing a means to read and write kernel
53 Attributes should be ASCII text files, preferably with only one value
54 per file. It is noted that it may not be efficient to contain only
55 value per file, so it is socially acceptable to express an array of
56 values of the same type.
58 Mixing types, expressing multiple lines of data, and doing fancy
59 formatting of data is heavily frowned upon. Doing these things may get
60 you publically humiliated and your code rewritten without notice.
63 An attribute definition is simply:
71 int sysfs_create_file(struct kobject * kobj, struct attribute * attr);
72 void sysfs_remove_file(struct kobject * kobj, struct attribute * attr);
75 A bare attribute contains no means to read or write the value of the
76 attribute. Subsystems are encouraged to define their own attribute
77 structure and wrapper functions for adding and removing attributes for
78 a specific object type.
80 For example, the driver model defines struct device_attribute like:
82 struct device_attribute {
83 struct attribute attr;
84 ssize_t (*show)(struct device * dev, char * buf);
85 ssize_t (*store)(struct device * dev, const char * buf);
88 int device_create_file(struct device *, struct device_attribute *);
89 void device_remove_file(struct device *, struct device_attribute *);
91 It also defines this helper for defining device attributes:
93 #define DEVICE_ATTR(_name,_mode,_show,_store) \
94 struct device_attribute dev_attr_##_name = { \
95 .attr = {.name = __stringify(_name) , .mode = _mode }, \
100 For example, declaring
102 static DEVICE_ATTR(foo,0644,show_foo,store_foo);
104 is equivalent to doing:
106 static struct device_attribute dev_attr_foo = {
116 Subsystem-Specific Callbacks
117 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119 When a subsystem defines a new attribute type, it must implement a
120 set of sysfs operations for forwarding read and write calls to the
121 show and store methods of the attribute owners.
124 ssize_t (*show)(struct kobject *, struct attribute *,char *);
125 ssize_t (*store)(struct kobject *,struct attribute *,const char *);
128 [ Subsystems should have already defined a struct kobj_type as a
129 descriptor for this type, which is where the sysfs_ops pointer is
130 stored. See the kobject documentation for more information. ]
132 When a file is read or written, sysfs calls the appropriate method
133 for the type. The method then translates the generic struct kobject
134 and struct attribute pointers to the appropriate pointer types, and
135 calls the associated methods.
140 #define to_dev_attr(_attr) container_of(_attr,struct device_attribute,attr)
141 #define to_dev(d) container_of(d, struct device, kobj)
144 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
146 struct device_attribute * dev_attr = to_dev_attr(attr);
147 struct device * dev = to_dev(kobj);
151 ret = dev_attr->show(dev,buf);
157 Reading/Writing Attribute Data
158 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160 To read or write attributes, show() or store() methods must be
161 specified when declaring the attribute. The method types should be as
162 simple as those defined for device attributes:
164 ssize_t (*show)(struct device * dev, char * buf);
165 ssize_t (*store)(struct device * dev, const char * buf);
167 IOW, they should take only an object and a buffer as parameters.
170 sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the
171 method. Sysfs will call the method exactly once for each read or
172 write. This forces the following behavior on the method
175 - On read(2), the show() method should fill the entire buffer.
176 Recall that an attribute should only be exporting one value, or an
177 array of similar values, so this shouldn't be that expensive.
179 This allows userspace to do partial reads and seeks arbitrarily over
180 the entire file at will.
182 - On write(2), sysfs expects the entire buffer to be passed during the
183 first write. Sysfs then passes the entire buffer to the store()
186 When writing sysfs files, userspace processes should first read the
187 entire file, modify the values it wishes to change, then write the
190 Attribute method implementations should operate on an identical
191 buffer when reading and writing values.
195 - The buffer will always be PAGE_SIZE bytes in length. On i386, this
198 - show() methods should return the number of bytes printed into the
199 buffer. This is the return value of snprintf().
201 - show() should always use snprintf().
203 - store() should return the number of bytes used from the buffer. This
204 can be done using strlen().
206 - show() or store() can always return errors. If a bad value comes
207 through, be sure to return an error.
209 - The object passed to the methods will be pinned in memory via sysfs
210 referencing counting its embedded object. However, the physical
211 entity (e.g. device) the object represents may not be present. Be
212 sure to have a way to check this, if necessary.
215 A very simple (and naive) implementation of a device attribute is:
217 static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
219 return sprintf(buf,"%s\n",dev->name);
222 static ssize_t store_name(struct device * dev, const char * buf)
224 sscanf(buf,"%20s",dev->name);
228 static DEVICE_ATTR(name,S_IRUGO,show_name,store_name);
231 (Note that the real implementation doesn't allow userspace to set the
235 Top Level Directory Layout
236 ~~~~~~~~~~~~~~~~~~~~~~~~~~
238 The sysfs directory arrangement exposes the relationship of kernel
241 The top level sysfs diretory looks like:
250 devices/ contains a filesystem representation of the device tree. It maps
251 directly to the internal kernel device tree, which is a hierarchy of
254 bus/ contains flat directory layout of the various bus types in the
255 kernel. Each bus's directory contains two subdirectories:
260 devices/ contains symlinks for each device discovered in the system
261 that point to the device's directory under root/.
263 drivers/ contains a directory for each device driver that is loaded
264 for devices on that particular bus (this assumes that drivers do not
265 span multiple bus types).
268 More information can driver-model specific features can be found in
269 Documentation/driver-model/.
272 TODO: Finish this section.
278 The following interface layers currently exist in sysfs:
281 - devices (include/linux/device.h)
282 ----------------------------------
285 struct device_attribute {
286 struct attribute attr;
287 ssize_t (*show)(struct device * dev, char * buf);
288 ssize_t (*store)(struct device * dev, const char * buf);
293 DEVICE_ATTR(_name,_str,_mode,_show,_store);
297 int device_create_file(struct device *device, struct device_attribute * attr);
298 void device_remove_file(struct device * dev, struct device_attribute * attr);
301 - bus drivers (include/linux/device.h)
302 --------------------------------------
305 struct bus_attribute {
306 struct attribute attr;
307 ssize_t (*show)(struct bus_type *, char * buf);
308 ssize_t (*store)(struct bus_type *, const char * buf);
313 BUS_ATTR(_name,_mode,_show,_store)
317 int bus_create_file(struct bus_type *, struct bus_attribute *);
318 void bus_remove_file(struct bus_type *, struct bus_attribute *);
321 - device drivers (include/linux/device.h)
322 -----------------------------------------
326 struct driver_attribute {
327 struct attribute attr;
328 ssize_t (*show)(struct device_driver *, char * buf);
329 ssize_t (*store)(struct device_driver *, const char * buf);
334 DRIVER_ATTR(_name,_mode,_show,_store)
338 int driver_create_file(struct device_driver *, struct driver_attribute *);
339 void driver_remove_file(struct device_driver *, struct driver_attribute *);