configfs.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. configfs - Userspace-driven kernel object configuration.
  2. Joel Becker <joel.becker@oracle.com>
  3. Updated: 31 March 2005
  4. Copyright (c) 2005 Oracle Corporation,
  5. Joel Becker <joel.becker@oracle.com>
  6. [What is configfs?]
  7. configfs is a ram-based filesystem that provides the converse of
  8. sysfs's functionality. Where sysfs is a filesystem-based view of
  9. kernel objects, configfs is a filesystem-based manager of kernel
  10. objects, or config_items.
  11. With sysfs, an object is created in kernel (for example, when a device
  12. is discovered) and it is registered with sysfs. Its attributes then
  13. appear in sysfs, allowing userspace to read the attributes via
  14. readdir(3)/read(2). It may allow some attributes to be modified via
  15. write(2). The important point is that the object is created and
  16. destroyed in kernel, the kernel controls the lifecycle of the sysfs
  17. representation, and sysfs is merely a window on all this.
  18. A configfs config_item is created via an explicit userspace operation:
  19. mkdir(2). It is destroyed via rmdir(2). The attributes appear at
  20. mkdir(2) time, and can be read or modified via read(2) and write(2).
  21. As with sysfs, readdir(3) queries the list of items and/or attributes.
  22. symlink(2) can be used to group items together. Unlike sysfs, the
  23. lifetime of the representation is completely driven by userspace. The
  24. kernel modules backing the items must respond to this.
  25. Both sysfs and configfs can and should exist together on the same
  26. system. One is not a replacement for the other.
  27. [Using configfs]
  28. configfs can be compiled as a module or into the kernel. You can access
  29. it by doing
  30. mount -t configfs none /config
  31. The configfs tree will be empty unless client modules are also loaded.
  32. These are modules that register their item types with configfs as
  33. subsystems. Once a client subsystem is loaded, it will appear as a
  34. subdirectory (or more than one) under /config. Like sysfs, the
  35. configfs tree is always there, whether mounted on /config or not.
  36. An item is created via mkdir(2). The item's attributes will also
  37. appear at this time. readdir(3) can determine what the attributes are,
  38. read(2) can query their default values, and write(2) can store new
  39. values. Don't mix more than one attribute in one attribute file.
  40. There are two types of configfs attributes:
  41. * Normal attributes, which similar to sysfs attributes, are small ASCII text
  42. files, with a maximum size of one page (PAGE_SIZE, 4096 on i386). Preferably
  43. only one value per file should be used, and the same caveats from sysfs apply.
  44. Configfs expects write(2) to store the entire buffer at once. When writing to
  45. normal configfs attributes, userspace processes should first read the entire
  46. file, modify the portions they wish to change, and then write the entire
  47. buffer back.
  48. * Binary attributes, which are somewhat similar to sysfs binary attributes,
  49. but with a few slight changes to semantics. The PAGE_SIZE limitation does not
  50. apply, but the whole binary item must fit in single kernel vmalloc'ed buffer.
  51. The write(2) calls from user space are buffered, and the attributes'
  52. write_bin_attribute method will be invoked on the final close, therefore it is
  53. imperative for user-space to check the return code of close(2) in order to
  54. verify that the operation finished successfully.
  55. To avoid a malicious user OOMing the kernel, there's a per-binary attribute
  56. maximum buffer value.
  57. When an item needs to be destroyed, remove it with rmdir(2). An
  58. item cannot be destroyed if any other item has a link to it (via
  59. symlink(2)). Links can be removed via unlink(2).
  60. [Configuring FakeNBD: an Example]
  61. Imagine there's a Network Block Device (NBD) driver that allows you to
  62. access remote block devices. Call it FakeNBD. FakeNBD uses configfs
  63. for its configuration. Obviously, there will be a nice program that
  64. sysadmins use to configure FakeNBD, but somehow that program has to tell
  65. the driver about it. Here's where configfs comes in.
  66. When the FakeNBD driver is loaded, it registers itself with configfs.
  67. readdir(3) sees this just fine:
  68. # ls /config
  69. fakenbd
  70. A fakenbd connection can be created with mkdir(2). The name is
  71. arbitrary, but likely the tool will make some use of the name. Perhaps
  72. it is a uuid or a disk name:
  73. # mkdir /config/fakenbd/disk1
  74. # ls /config/fakenbd/disk1
  75. target device rw
  76. The target attribute contains the IP address of the server FakeNBD will
  77. connect to. The device attribute is the device on the server.
  78. Predictably, the rw attribute determines whether the connection is
  79. read-only or read-write.
  80. # echo 10.0.0.1 > /config/fakenbd/disk1/target
  81. # echo /dev/sda1 > /config/fakenbd/disk1/device
  82. # echo 1 > /config/fakenbd/disk1/rw
  83. That's it. That's all there is. Now the device is configured, via the
  84. shell no less.
  85. [Coding With configfs]
  86. Every object in configfs is a config_item. A config_item reflects an
  87. object in the subsystem. It has attributes that match values on that
  88. object. configfs handles the filesystem representation of that object
  89. and its attributes, allowing the subsystem to ignore all but the
  90. basic show/store interaction.
  91. Items are created and destroyed inside a config_group. A group is a
  92. collection of items that share the same attributes and operations.
  93. Items are created by mkdir(2) and removed by rmdir(2), but configfs
  94. handles that. The group has a set of operations to perform these tasks
  95. A subsystem is the top level of a client module. During initialization,
  96. the client module registers the subsystem with configfs, the subsystem
  97. appears as a directory at the top of the configfs filesystem. A
  98. subsystem is also a config_group, and can do everything a config_group
  99. can.
  100. [struct config_item]
  101. struct config_item {
  102. char *ci_name;
  103. char ci_namebuf[UOBJ_NAME_LEN];
  104. struct kref ci_kref;
  105. struct list_head ci_entry;
  106. struct config_item *ci_parent;
  107. struct config_group *ci_group;
  108. struct config_item_type *ci_type;
  109. struct dentry *ci_dentry;
  110. };
  111. void config_item_init(struct config_item *);
  112. void config_item_init_type_name(struct config_item *,
  113. const char *name,
  114. struct config_item_type *type);
  115. struct config_item *config_item_get(struct config_item *);
  116. void config_item_put(struct config_item *);
  117. Generally, struct config_item is embedded in a container structure, a
  118. structure that actually represents what the subsystem is doing. The
  119. config_item portion of that structure is how the object interacts with
  120. configfs.
  121. Whether statically defined in a source file or created by a parent
  122. config_group, a config_item must have one of the _init() functions
  123. called on it. This initializes the reference count and sets up the
  124. appropriate fields.
  125. All users of a config_item should have a reference on it via
  126. config_item_get(), and drop the reference when they are done via
  127. config_item_put().
  128. By itself, a config_item cannot do much more than appear in configfs.
  129. Usually a subsystem wants the item to display and/or store attributes,
  130. among other things. For that, it needs a type.
  131. [struct config_item_type]
  132. struct configfs_item_operations {
  133. void (*release)(struct config_item *);
  134. int (*allow_link)(struct config_item *src,
  135. struct config_item *target);
  136. int (*drop_link)(struct config_item *src,
  137. struct config_item *target);
  138. };
  139. struct config_item_type {
  140. struct module *ct_owner;
  141. struct configfs_item_operations *ct_item_ops;
  142. struct configfs_group_operations *ct_group_ops;
  143. struct configfs_attribute **ct_attrs;
  144. struct configfs_bin_attribute **ct_bin_attrs;
  145. };
  146. The most basic function of a config_item_type is to define what
  147. operations can be performed on a config_item. All items that have been
  148. allocated dynamically will need to provide the ct_item_ops->release()
  149. method. This method is called when the config_item's reference count
  150. reaches zero.
  151. [struct configfs_attribute]
  152. struct configfs_attribute {
  153. char *ca_name;
  154. struct module *ca_owner;
  155. umode_t ca_mode;
  156. ssize_t (*show)(struct config_item *, char *);
  157. ssize_t (*store)(struct config_item *, const char *, size_t);
  158. };
  159. When a config_item wants an attribute to appear as a file in the item's
  160. configfs directory, it must define a configfs_attribute describing it.
  161. It then adds the attribute to the NULL-terminated array
  162. config_item_type->ct_attrs. When the item appears in configfs, the
  163. attribute file will appear with the configfs_attribute->ca_name
  164. filename. configfs_attribute->ca_mode specifies the file permissions.
  165. If an attribute is readable and provides a ->show method, that method will
  166. be called whenever userspace asks for a read(2) on the attribute. If an
  167. attribute is writable and provides a ->store method, that method will be
  168. be called whenever userspace asks for a write(2) on the attribute.
  169. [struct configfs_bin_attribute]
  170. struct configfs_attribute {
  171. struct configfs_attribute cb_attr;
  172. void *cb_private;
  173. size_t cb_max_size;
  174. };
  175. The binary attribute is used when the one needs to use binary blob to
  176. appear as the contents of a file in the item's configfs directory.
  177. To do so add the binary attribute to the NULL-terminated array
  178. config_item_type->ct_bin_attrs, and the item appears in configfs, the
  179. attribute file will appear with the configfs_bin_attribute->cb_attr.ca_name
  180. filename. configfs_bin_attribute->cb_attr.ca_mode specifies the file
  181. permissions.
  182. The cb_private member is provided for use by the driver, while the
  183. cb_max_size member specifies the maximum amount of vmalloc buffer
  184. to be used.
  185. If binary attribute is readable and the config_item provides a
  186. ct_item_ops->read_bin_attribute() method, that method will be called
  187. whenever userspace asks for a read(2) on the attribute. The converse
  188. will happen for write(2). The reads/writes are bufferred so only a
  189. single read/write will occur; the attributes' need not concern itself
  190. with it.
  191. [struct config_group]
  192. A config_item cannot live in a vacuum. The only way one can be created
  193. is via mkdir(2) on a config_group. This will trigger creation of a
  194. child item.
  195. struct config_group {
  196. struct config_item cg_item;
  197. struct list_head cg_children;
  198. struct configfs_subsystem *cg_subsys;
  199. struct list_head default_groups;
  200. struct list_head group_entry;
  201. };
  202. void config_group_init(struct config_group *group);
  203. void config_group_init_type_name(struct config_group *group,
  204. const char *name,
  205. struct config_item_type *type);
  206. The config_group structure contains a config_item. Properly configuring
  207. that item means that a group can behave as an item in its own right.
  208. However, it can do more: it can create child items or groups. This is
  209. accomplished via the group operations specified on the group's
  210. config_item_type.
  211. struct configfs_group_operations {
  212. struct config_item *(*make_item)(struct config_group *group,
  213. const char *name);
  214. struct config_group *(*make_group)(struct config_group *group,
  215. const char *name);
  216. int (*commit_item)(struct config_item *item);
  217. void (*disconnect_notify)(struct config_group *group,
  218. struct config_item *item);
  219. void (*drop_item)(struct config_group *group,
  220. struct config_item *item);
  221. };
  222. A group creates child items by providing the
  223. ct_group_ops->make_item() method. If provided, this method is called from mkdir(2) in the group's directory. The subsystem allocates a new
  224. config_item (or more likely, its container structure), initializes it,
  225. and returns it to configfs. Configfs will then populate the filesystem
  226. tree to reflect the new item.
  227. If the subsystem wants the child to be a group itself, the subsystem
  228. provides ct_group_ops->make_group(). Everything else behaves the same,
  229. using the group _init() functions on the group.
  230. Finally, when userspace calls rmdir(2) on the item or group,
  231. ct_group_ops->drop_item() is called. As a config_group is also a
  232. config_item, it is not necessary for a separate drop_group() method.
  233. The subsystem must config_item_put() the reference that was initialized
  234. upon item allocation. If a subsystem has no work to do, it may omit
  235. the ct_group_ops->drop_item() method, and configfs will call
  236. config_item_put() on the item on behalf of the subsystem.
  237. IMPORTANT: drop_item() is void, and as such cannot fail. When rmdir(2)
  238. is called, configfs WILL remove the item from the filesystem tree
  239. (assuming that it has no children to keep it busy). The subsystem is
  240. responsible for responding to this. If the subsystem has references to
  241. the item in other threads, the memory is safe. It may take some time
  242. for the item to actually disappear from the subsystem's usage. But it
  243. is gone from configfs.
  244. When drop_item() is called, the item's linkage has already been torn
  245. down. It no longer has a reference on its parent and has no place in
  246. the item hierarchy. If a client needs to do some cleanup before this
  247. teardown happens, the subsystem can implement the
  248. ct_group_ops->disconnect_notify() method. The method is called after
  249. configfs has removed the item from the filesystem view but before the
  250. item is removed from its parent group. Like drop_item(),
  251. disconnect_notify() is void and cannot fail. Client subsystems should
  252. not drop any references here, as they still must do it in drop_item().
  253. A config_group cannot be removed while it still has child items. This
  254. is implemented in the configfs rmdir(2) code. ->drop_item() will not be
  255. called, as the item has not been dropped. rmdir(2) will fail, as the
  256. directory is not empty.
  257. [struct configfs_subsystem]
  258. A subsystem must register itself, usually at module_init time. This
  259. tells configfs to make the subsystem appear in the file tree.
  260. struct configfs_subsystem {
  261. struct config_group su_group;
  262. struct mutex su_mutex;
  263. };
  264. int configfs_register_subsystem(struct configfs_subsystem *subsys);
  265. void configfs_unregister_subsystem(struct configfs_subsystem *subsys);
  266. A subsystem consists of a toplevel config_group and a mutex.
  267. The group is where child config_items are created. For a subsystem,
  268. this group is usually defined statically. Before calling
  269. configfs_register_subsystem(), the subsystem must have initialized the
  270. group via the usual group _init() functions, and it must also have
  271. initialized the mutex.
  272. When the register call returns, the subsystem is live, and it
  273. will be visible via configfs. At that point, mkdir(2) can be called and
  274. the subsystem must be ready for it.
  275. [An Example]
  276. The best example of these basic concepts is the simple_children
  277. subsystem/group and the simple_child item in
  278. samples/configfs/configfs_sample.c. It shows a trivial object displaying
  279. and storing an attribute, and a simple group creating and destroying
  280. these children.
  281. [Hierarchy Navigation and the Subsystem Mutex]
  282. There is an extra bonus that configfs provides. The config_groups and
  283. config_items are arranged in a hierarchy due to the fact that they
  284. appear in a filesystem. A subsystem is NEVER to touch the filesystem
  285. parts, but the subsystem might be interested in this hierarchy. For
  286. this reason, the hierarchy is mirrored via the config_group->cg_children
  287. and config_item->ci_parent structure members.
  288. A subsystem can navigate the cg_children list and the ci_parent pointer
  289. to see the tree created by the subsystem. This can race with configfs'
  290. management of the hierarchy, so configfs uses the subsystem mutex to
  291. protect modifications. Whenever a subsystem wants to navigate the
  292. hierarchy, it must do so under the protection of the subsystem
  293. mutex.
  294. A subsystem will be prevented from acquiring the mutex while a newly
  295. allocated item has not been linked into this hierarchy. Similarly, it
  296. will not be able to acquire the mutex while a dropping item has not
  297. yet been unlinked. This means that an item's ci_parent pointer will
  298. never be NULL while the item is in configfs, and that an item will only
  299. be in its parent's cg_children list for the same duration. This allows
  300. a subsystem to trust ci_parent and cg_children while they hold the
  301. mutex.
  302. [Item Aggregation Via symlink(2)]
  303. configfs provides a simple group via the group->item parent/child
  304. relationship. Often, however, a larger environment requires aggregation
  305. outside of the parent/child connection. This is implemented via
  306. symlink(2).
  307. A config_item may provide the ct_item_ops->allow_link() and
  308. ct_item_ops->drop_link() methods. If the ->allow_link() method exists,
  309. symlink(2) may be called with the config_item as the source of the link.
  310. These links are only allowed between configfs config_items. Any
  311. symlink(2) attempt outside the configfs filesystem will be denied.
  312. When symlink(2) is called, the source config_item's ->allow_link()
  313. method is called with itself and a target item. If the source item
  314. allows linking to target item, it returns 0. A source item may wish to
  315. reject a link if it only wants links to a certain type of object (say,
  316. in its own subsystem).
  317. When unlink(2) is called on the symbolic link, the source item is
  318. notified via the ->drop_link() method. Like the ->drop_item() method,
  319. this is a void function and cannot return failure. The subsystem is
  320. responsible for responding to the change.
  321. A config_item cannot be removed while it links to any other item, nor
  322. can it be removed while an item links to it. Dangling symlinks are not
  323. allowed in configfs.
  324. [Automatically Created Subgroups]
  325. A new config_group may want to have two types of child config_items.
  326. While this could be codified by magic names in ->make_item(), it is much
  327. more explicit to have a method whereby userspace sees this divergence.
  328. Rather than have a group where some items behave differently than
  329. others, configfs provides a method whereby one or many subgroups are
  330. automatically created inside the parent at its creation. Thus,
  331. mkdir("parent") results in "parent", "parent/subgroup1", up through
  332. "parent/subgroupN". Items of type 1 can now be created in
  333. "parent/subgroup1", and items of type N can be created in
  334. "parent/subgroupN".
  335. These automatic subgroups, or default groups, do not preclude other
  336. children of the parent group. If ct_group_ops->make_group() exists,
  337. other child groups can be created on the parent group directly.
  338. A configfs subsystem specifies default groups by adding them using the
  339. configfs_add_default_group() function to the parent config_group
  340. structure. Each added group is populated in the configfs tree at the same
  341. time as the parent group. Similarly, they are removed at the same time
  342. as the parent. No extra notification is provided. When a ->drop_item()
  343. method call notifies the subsystem the parent group is going away, it
  344. also means every default group child associated with that parent group.
  345. As a consequence of this, default groups cannot be removed directly via
  346. rmdir(2). They also are not considered when rmdir(2) on the parent
  347. group is checking for children.
  348. [Dependent Subsystems]
  349. Sometimes other drivers depend on particular configfs items. For
  350. example, ocfs2 mounts depend on a heartbeat region item. If that
  351. region item is removed with rmdir(2), the ocfs2 mount must BUG or go
  352. readonly. Not happy.
  353. configfs provides two additional API calls: configfs_depend_item() and
  354. configfs_undepend_item(). A client driver can call
  355. configfs_depend_item() on an existing item to tell configfs that it is
  356. depended on. configfs will then return -EBUSY from rmdir(2) for that
  357. item. When the item is no longer depended on, the client driver calls
  358. configfs_undepend_item() on it.
  359. These API cannot be called underneath any configfs callbacks, as
  360. they will conflict. They can block and allocate. A client driver
  361. probably shouldn't calling them of its own gumption. Rather it should
  362. be providing an API that external subsystems call.
  363. How does this work? Imagine the ocfs2 mount process. When it mounts,
  364. it asks for a heartbeat region item. This is done via a call into the
  365. heartbeat code. Inside the heartbeat code, the region item is looked
  366. up. Here, the heartbeat code calls configfs_depend_item(). If it
  367. succeeds, then heartbeat knows the region is safe to give to ocfs2.
  368. If it fails, it was being torn down anyway, and heartbeat can gracefully
  369. pass up an error.
  370. [Committable Items]
  371. NOTE: Committable items are currently unimplemented.
  372. Some config_items cannot have a valid initial state. That is, no
  373. default values can be specified for the item's attributes such that the
  374. item can do its work. Userspace must configure one or more attributes,
  375. after which the subsystem can start whatever entity this item
  376. represents.
  377. Consider the FakeNBD device from above. Without a target address *and*
  378. a target device, the subsystem has no idea what block device to import.
  379. The simple example assumes that the subsystem merely waits until all the
  380. appropriate attributes are configured, and then connects. This will,
  381. indeed, work, but now every attribute store must check if the attributes
  382. are initialized. Every attribute store must fire off the connection if
  383. that condition is met.
  384. Far better would be an explicit action notifying the subsystem that the
  385. config_item is ready to go. More importantly, an explicit action allows
  386. the subsystem to provide feedback as to whether the attributes are
  387. initialized in a way that makes sense. configfs provides this as
  388. committable items.
  389. configfs still uses only normal filesystem operations. An item is
  390. committed via rename(2). The item is moved from a directory where it
  391. can be modified to a directory where it cannot.
  392. Any group that provides the ct_group_ops->commit_item() method has
  393. committable items. When this group appears in configfs, mkdir(2) will
  394. not work directly in the group. Instead, the group will have two
  395. subdirectories: "live" and "pending". The "live" directory does not
  396. support mkdir(2) or rmdir(2) either. It only allows rename(2). The
  397. "pending" directory does allow mkdir(2) and rmdir(2). An item is
  398. created in the "pending" directory. Its attributes can be modified at
  399. will. Userspace commits the item by renaming it into the "live"
  400. directory. At this point, the subsystem receives the ->commit_item()
  401. callback. If all required attributes are filled to satisfaction, the
  402. method returns zero and the item is moved to the "live" directory.
  403. As rmdir(2) does not work in the "live" directory, an item must be
  404. shutdown, or "uncommitted". Again, this is done via rename(2), this
  405. time from the "live" directory back to the "pending" one. The subsystem
  406. is notified by the ct_group_ops->uncommit_object() method.