sysfs.txt 12 KB

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