sysfs.txt 11 KB

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