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