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