]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | |
2 | sysfs - _The_ filesystem for exporting kernel objects. | |
3 | ||
4 | Patrick Mochel <[email protected]> | |
f8a1af6b | 5 | Mike Murphy <[email protected]> |
1da177e4 | 6 | |
86028619 | 7 | Revised: 16 August 2011 |
f8a1af6b | 8 | Original: 10 January 2003 |
1da177e4 LT |
9 | |
10 | ||
11 | What it is: | |
12 | ~~~~~~~~~~~ | |
13 | ||
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. | |
17 | ||
18 | sysfs is tied inherently to the kobject infrastructure. Please read | |
19 | Documentation/kobject.txt for more information concerning the kobject | |
20 | interface. | |
21 | ||
22 | ||
23 | Using sysfs | |
24 | ~~~~~~~~~~~ | |
25 | ||
a39ea210 LAG |
26 | sysfs is always compiled in if CONFIG_SYSFS is defined. You can access |
27 | it by doing: | |
1da177e4 LT |
28 | |
29 | mount -t sysfs sysfs /sys | |
30 | ||
31 | ||
32 | Directory Creation | |
33 | ~~~~~~~~~~~~~~~~~~ | |
34 | ||
35 | For every kobject that is registered with the system, a directory is | |
36 | created for it in sysfs. That directory is created as a subdirectory | |
37 | of the kobject's parent, expressing internal object hierarchies to | |
38 | userspace. Top-level directories in sysfs represent the common | |
39 | ancestors of object hierarchies; i.e. the subsystems the objects | |
40 | belong to. | |
41 | ||
5480bcdd | 42 | Sysfs internally stores a pointer to the kobject that implements a |
390b421c | 43 | directory in the kernfs_node object associated with the directory. In |
5480bcdd BVA |
44 | the past this kobject pointer has been used by sysfs to do reference |
45 | counting directly on the kobject whenever the file is opened or closed. | |
46 | With the current sysfs implementation the kobject reference count is | |
47 | only modified directly by the function sysfs_schedule_callback(). | |
1da177e4 LT |
48 | |
49 | ||
50 | Attributes | |
51 | ~~~~~~~~~~ | |
52 | ||
53 | Attributes can be exported for kobjects in the form of regular files in | |
54 | the filesystem. Sysfs forwards file I/O operations to methods defined | |
55 | for the attributes, providing a means to read and write kernel | |
56 | attributes. | |
57 | ||
58 | Attributes should be ASCII text files, preferably with only one value | |
f8c34f98 | 59 | per file. It is noted that it may not be efficient to contain only one |
1da177e4 LT |
60 | value per file, so it is socially acceptable to express an array of |
61 | values of the same type. | |
62 | ||
63 | Mixing types, expressing multiple lines of data, and doing fancy | |
64 | formatting of data is heavily frowned upon. Doing these things may get | |
25985edc | 65 | you publicly humiliated and your code rewritten without notice. |
1da177e4 LT |
66 | |
67 | ||
68 | An attribute definition is simply: | |
69 | ||
70 | struct attribute { | |
71 | char * name; | |
f8a1af6b | 72 | struct module *owner; |
faef2b6c | 73 | umode_t mode; |
1da177e4 LT |
74 | }; |
75 | ||
76 | ||
f8a1af6b MM |
77 | int sysfs_create_file(struct kobject * kobj, const struct attribute * attr); |
78 | void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr); | |
1da177e4 LT |
79 | |
80 | ||
81 | A bare attribute contains no means to read or write the value of the | |
82 | attribute. Subsystems are encouraged to define their own attribute | |
83 | structure and wrapper functions for adding and removing attributes for | |
84 | a specific object type. | |
85 | ||
86 | For example, the driver model defines struct device_attribute like: | |
87 | ||
88 | struct device_attribute { | |
f8a1af6b MM |
89 | struct attribute attr; |
90 | ssize_t (*show)(struct device *dev, struct device_attribute *attr, | |
91 | char *buf); | |
92 | ssize_t (*store)(struct device *dev, struct device_attribute *attr, | |
93 | const char *buf, size_t count); | |
1da177e4 LT |
94 | }; |
95 | ||
26579ab7 PC |
96 | int device_create_file(struct device *, const struct device_attribute *); |
97 | void device_remove_file(struct device *, const struct device_attribute *); | |
1da177e4 LT |
98 | |
99 | It also defines this helper for defining device attributes: | |
100 | ||
f8a1af6b MM |
101 | #define DEVICE_ATTR(_name, _mode, _show, _store) \ |
102 | struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) | |
1da177e4 LT |
103 | |
104 | For example, declaring | |
105 | ||
91e49001 | 106 | static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo); |
1da177e4 LT |
107 | |
108 | is equivalent to doing: | |
109 | ||
110 | static struct device_attribute dev_attr_foo = { | |
c1083732 | 111 | .attr = { |
1da177e4 | 112 | .name = "foo", |
91e49001 | 113 | .mode = S_IWUSR | S_IRUGO, |
1da177e4 | 114 | }, |
c1083732 AR |
115 | .show = show_foo, |
116 | .store = store_foo, | |
1da177e4 LT |
117 | }; |
118 | ||
119 | ||
120 | Subsystem-Specific Callbacks | |
121 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
122 | ||
123 | When a subsystem defines a new attribute type, it must implement a | |
124 | set of sysfs operations for forwarding read and write calls to the | |
125 | show and store methods of the attribute owners. | |
126 | ||
127 | struct sysfs_ops { | |
f8d825bf | 128 | ssize_t (*show)(struct kobject *, struct attribute *, char *); |
30a69000 | 129 | ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); |
1da177e4 LT |
130 | }; |
131 | ||
132 | [ Subsystems should have already defined a struct kobj_type as a | |
133 | descriptor for this type, which is where the sysfs_ops pointer is | |
134 | stored. See the kobject documentation for more information. ] | |
135 | ||
136 | When a file is read or written, sysfs calls the appropriate method | |
137 | for the type. The method then translates the generic struct kobject | |
138 | and struct attribute pointers to the appropriate pointer types, and | |
139 | calls the associated methods. | |
140 | ||
141 | ||
142 | To illustrate: | |
143 | ||
30a69000 | 144 | #define to_dev(obj) container_of(obj, struct device, kobj) |
f8d825bf | 145 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
1da177e4 | 146 | |
30a69000 BVA |
147 | static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, |
148 | char *buf) | |
1da177e4 | 149 | { |
30a69000 BVA |
150 | struct device_attribute *dev_attr = to_dev_attr(attr); |
151 | struct device *dev = to_dev(kobj); | |
152 | ssize_t ret = -EIO; | |
1da177e4 LT |
153 | |
154 | if (dev_attr->show) | |
30a69000 BVA |
155 | ret = dev_attr->show(dev, dev_attr, buf); |
156 | if (ret >= (ssize_t)PAGE_SIZE) { | |
157 | print_symbol("dev_attr_show: %s returned bad count\n", | |
158 | (unsigned long)dev_attr->show); | |
159 | } | |
1da177e4 LT |
160 | return ret; |
161 | } | |
162 | ||
163 | ||
164 | ||
165 | Reading/Writing Attribute Data | |
166 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
167 | ||
168 | To read or write attributes, show() or store() methods must be | |
169 | specified when declaring the attribute. The method types should be as | |
170 | simple as those defined for device attributes: | |
171 | ||
30a69000 BVA |
172 | ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf); |
173 | ssize_t (*store)(struct device *dev, struct device_attribute *attr, | |
174 | const char *buf, size_t count); | |
1da177e4 | 175 | |
f8a1af6b | 176 | IOW, they should take only an object, an attribute, and a buffer as parameters. |
1da177e4 LT |
177 | |
178 | ||
179 | sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the | |
180 | method. Sysfs will call the method exactly once for each read or | |
181 | write. This forces the following behavior on the method | |
182 | implementations: | |
183 | ||
184 | - On read(2), the show() method should fill the entire buffer. | |
185 | Recall that an attribute should only be exporting one value, or an | |
186 | array of similar values, so this shouldn't be that expensive. | |
187 | ||
2424b5dd DW |
188 | This allows userspace to do partial reads and forward seeks |
189 | arbitrarily over the entire file at will. If userspace seeks back to | |
190 | zero or does a pread(2) with an offset of '0' the show() method will | |
191 | be called again, rearmed, to fill the buffer. | |
1da177e4 LT |
192 | |
193 | - On write(2), sysfs expects the entire buffer to be passed during the | |
17666497 UM |
194 | first write. Sysfs then passes the entire buffer to the store() method. |
195 | A terminating null is added after the data on stores. This makes | |
196 | functions like sysfs_streq() safe to use. | |
197 | ||
1da177e4 LT |
198 | When writing sysfs files, userspace processes should first read the |
199 | entire file, modify the values it wishes to change, then write the | |
200 | entire buffer back. | |
201 | ||
202 | Attribute method implementations should operate on an identical | |
203 | buffer when reading and writing values. | |
204 | ||
205 | Other notes: | |
206 | ||
2424b5dd DW |
207 | - Writing causes the show() method to be rearmed regardless of current |
208 | file position. | |
209 | ||
1da177e4 LT |
210 | - The buffer will always be PAGE_SIZE bytes in length. On i386, this |
211 | is 4096. | |
212 | ||
213 | - show() methods should return the number of bytes printed into the | |
d3f70bef | 214 | buffer. This is the return value of scnprintf(). |
1da177e4 | 215 | |
223e8f01 SS |
216 | - show() must not use snprintf() when formatting the value to be |
217 | returned to user space. If you can guarantee that an overflow | |
218 | will never happen you can use sprintf() otherwise you must use | |
219 | scnprintf(). | |
1da177e4 | 220 | |
30a69000 BVA |
221 | - store() should return the number of bytes used from the buffer. If the |
222 | entire buffer has been used, just return the count argument. | |
1da177e4 LT |
223 | |
224 | - show() or store() can always return errors. If a bad value comes | |
225 | through, be sure to return an error. | |
226 | ||
227 | - The object passed to the methods will be pinned in memory via sysfs | |
228 | referencing counting its embedded object. However, the physical | |
229 | entity (e.g. device) the object represents may not be present. Be | |
230 | sure to have a way to check this, if necessary. | |
231 | ||
232 | ||
233 | A very simple (and naive) implementation of a device attribute is: | |
234 | ||
30a69000 BVA |
235 | static ssize_t show_name(struct device *dev, struct device_attribute *attr, |
236 | char *buf) | |
1da177e4 | 237 | { |
d3f70bef | 238 | return scnprintf(buf, PAGE_SIZE, "%s\n", dev->name); |
1da177e4 LT |
239 | } |
240 | ||
30a69000 BVA |
241 | static ssize_t store_name(struct device *dev, struct device_attribute *attr, |
242 | const char *buf, size_t count) | |
1da177e4 | 243 | { |
30a69000 BVA |
244 | snprintf(dev->name, sizeof(dev->name), "%.*s", |
245 | (int)min(count, sizeof(dev->name) - 1), buf); | |
246 | return count; | |
1da177e4 LT |
247 | } |
248 | ||
f8d825bf | 249 | static DEVICE_ATTR(name, S_IRUGO, show_name, store_name); |
1da177e4 LT |
250 | |
251 | ||
252 | (Note that the real implementation doesn't allow userspace to set the | |
253 | name for a device.) | |
254 | ||
255 | ||
256 | Top Level Directory Layout | |
257 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
258 | ||
259 | The sysfs directory arrangement exposes the relationship of kernel | |
260 | data structures. | |
261 | ||
fff9289b | 262 | The top level sysfs directory looks like: |
1da177e4 LT |
263 | |
264 | block/ | |
265 | bus/ | |
266 | class/ | |
e105b8bf | 267 | dev/ |
1da177e4 LT |
268 | devices/ |
269 | firmware/ | |
270 | net/ | |
c86d90df | 271 | fs/ |
1da177e4 LT |
272 | |
273 | devices/ contains a filesystem representation of the device tree. It maps | |
274 | directly to the internal kernel device tree, which is a hierarchy of | |
275 | struct device. | |
276 | ||
277 | bus/ contains flat directory layout of the various bus types in the | |
278 | kernel. Each bus's directory contains two subdirectories: | |
279 | ||
280 | devices/ | |
281 | drivers/ | |
282 | ||
283 | devices/ contains symlinks for each device discovered in the system | |
284 | that point to the device's directory under root/. | |
285 | ||
286 | drivers/ contains a directory for each device driver that is loaded | |
287 | for devices on that particular bus (this assumes that drivers do not | |
288 | span multiple bus types). | |
289 | ||
c86d90df MS |
290 | fs/ contains a directory for some filesystems. Currently each |
291 | filesystem wanting to export attributes must create its own hierarchy | |
292 | below fs/ (see ./fuse.txt for an example). | |
293 | ||
e105b8bf DW |
294 | dev/ contains two directories char/ and block/. Inside these two |
295 | directories there are symlinks named <major>:<minor>. These symlinks | |
296 | point to the sysfs directory for the given device. /sys/dev provides a | |
297 | quick way to lookup the sysfs interface for a device from the result of | |
298 | a stat(2) operation. | |
1da177e4 LT |
299 | |
300 | More information can driver-model specific features can be found in | |
301 | Documentation/driver-model/. | |
302 | ||
303 | ||
304 | TODO: Finish this section. | |
305 | ||
306 | ||
307 | Current Interfaces | |
308 | ~~~~~~~~~~~~~~~~~~ | |
309 | ||
310 | The following interface layers currently exist in sysfs: | |
311 | ||
312 | ||
313 | - devices (include/linux/device.h) | |
314 | ---------------------------------- | |
315 | Structure: | |
316 | ||
317 | struct device_attribute { | |
f8a1af6b MM |
318 | struct attribute attr; |
319 | ssize_t (*show)(struct device *dev, struct device_attribute *attr, | |
320 | char *buf); | |
321 | ssize_t (*store)(struct device *dev, struct device_attribute *attr, | |
322 | const char *buf, size_t count); | |
1da177e4 LT |
323 | }; |
324 | ||
325 | Declaring: | |
326 | ||
f8a1af6b | 327 | DEVICE_ATTR(_name, _mode, _show, _store); |
1da177e4 LT |
328 | |
329 | Creation/Removal: | |
330 | ||
26579ab7 PC |
331 | int device_create_file(struct device *dev, const struct device_attribute * attr); |
332 | void device_remove_file(struct device *dev, const struct device_attribute * attr); | |
1da177e4 LT |
333 | |
334 | ||
335 | - bus drivers (include/linux/device.h) | |
336 | -------------------------------------- | |
337 | Structure: | |
338 | ||
339 | struct bus_attribute { | |
340 | struct attribute attr; | |
341 | ssize_t (*show)(struct bus_type *, char * buf); | |
a5307032 | 342 | ssize_t (*store)(struct bus_type *, const char * buf, size_t count); |
1da177e4 LT |
343 | }; |
344 | ||
345 | Declaring: | |
346 | ||
f8d825bf | 347 | BUS_ATTR(_name, _mode, _show, _store) |
1da177e4 LT |
348 | |
349 | Creation/Removal: | |
350 | ||
351 | int bus_create_file(struct bus_type *, struct bus_attribute *); | |
352 | void bus_remove_file(struct bus_type *, struct bus_attribute *); | |
353 | ||
354 | ||
355 | - device drivers (include/linux/device.h) | |
356 | ----------------------------------------- | |
357 | ||
358 | Structure: | |
359 | ||
360 | struct driver_attribute { | |
361 | struct attribute attr; | |
362 | ssize_t (*show)(struct device_driver *, char * buf); | |
f8a1af6b MM |
363 | ssize_t (*store)(struct device_driver *, const char * buf, |
364 | size_t count); | |
1da177e4 LT |
365 | }; |
366 | ||
367 | Declaring: | |
368 | ||
850fdec8 GKH |
369 | DRIVER_ATTR_RO(_name) |
370 | DRIVER_ATTR_RW(_name) | |
1da177e4 LT |
371 | |
372 | Creation/Removal: | |
373 | ||
099c2f21 PC |
374 | int driver_create_file(struct device_driver *, const struct driver_attribute *); |
375 | void driver_remove_file(struct device_driver *, const struct driver_attribute *); | |
1da177e4 LT |
376 | |
377 | ||
86028619 BVA |
378 | Documentation |
379 | ~~~~~~~~~~~~~ | |
380 | ||
381 | The sysfs directory structure and the attributes in each directory define an | |
382 | ABI between the kernel and user space. As for any ABI, it is important that | |
383 | this ABI is stable and properly documented. All new sysfs attributes must be | |
384 | documented in Documentation/ABI. See also Documentation/ABI/README for more | |
385 | information. |