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