sysfs.txt 10 KB

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