sysfs.txt 11 KB

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