blob: 15838d706ea24afaac3d1951d3452006b15d9742 [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
54values. Like sysfs, attributes should be ASCII text files, preferably
55with only one value per file. The same efficiency caveats from sysfs
56apply. Don't mix more than one attribute in one attribute file.
57
58Like sysfs, configfs expects write(2) to store the entire buffer at
59once. When writing to configfs attributes, userspace processes should
60first read the entire file, modify the portions they wish to change, and
61then write the entire buffer back. Attribute files have a maximum size
62of one page (PAGE_SIZE, 4096 on i386).
63
64When an item needs to be destroyed, remove it with rmdir(2). An
65item cannot be destroyed if any other item has a link to it (via
66symlink(2)). Links can be removed via unlink(2).
67
68[Configuring FakeNBD: an Example]
69
70Imagine there's a Network Block Device (NBD) driver that allows you to
71access remote block devices. Call it FakeNBD. FakeNBD uses configfs
72for its configuration. Obviously, there will be a nice program that
73sysadmins use to configure FakeNBD, but somehow that program has to tell
74the driver about it. Here's where configfs comes in.
75
76When the FakeNBD driver is loaded, it registers itself with configfs.
77readdir(3) sees this just fine:
78
79 # ls /config
80 fakenbd
81
82A fakenbd connection can be created with mkdir(2). The name is
83arbitrary, but likely the tool will make some use of the name. Perhaps
84it is a uuid or a disk name:
85
86 # mkdir /config/fakenbd/disk1
87 # ls /config/fakenbd/disk1
88 target device rw
89
90The target attribute contains the IP address of the server FakeNBD will
91connect to. The device attribute is the device on the server.
92Predictably, the rw attribute determines whether the connection is
93read-only or read-write.
94
95 # echo 10.0.0.1 > /config/fakenbd/disk1/target
96 # echo /dev/sda1 > /config/fakenbd/disk1/device
97 # echo 1 > /config/fakenbd/disk1/rw
98
99That's it. That's all there is. Now the device is configured, via the
100shell no less.
101
102[Coding With configfs]
103
104Every object in configfs is a config_item. A config_item reflects an
105object in the subsystem. It has attributes that match values on that
106object. configfs handles the filesystem representation of that object
107and its attributes, allowing the subsystem to ignore all but the
108basic show/store interaction.
109
110Items are created and destroyed inside a config_group. A group is a
111collection of items that share the same attributes and operations.
112Items are created by mkdir(2) and removed by rmdir(2), but configfs
113handles that. The group has a set of operations to perform these tasks
114
115A subsystem is the top level of a client module. During initialization,
116the client module registers the subsystem with configfs, the subsystem
117appears as a directory at the top of the configfs filesystem. A
118subsystem is also a config_group, and can do everything a config_group
119can.
120
121[struct config_item]
122
123 struct config_item {
124 char *ci_name;
125 char ci_namebuf[UOBJ_NAME_LEN];
126 struct kref ci_kref;
127 struct list_head ci_entry;
128 struct config_item *ci_parent;
129 struct config_group *ci_group;
130 struct config_item_type *ci_type;
131 struct dentry *ci_dentry;
132 };
133
134 void config_item_init(struct config_item *);
135 void config_item_init_type_name(struct config_item *,
136 const char *name,
137 struct config_item_type *type);
138 struct config_item *config_item_get(struct config_item *);
139 void config_item_put(struct config_item *);
140
141Generally, struct config_item is embedded in a container structure, a
142structure that actually represents what the subsystem is doing. The
143config_item portion of that structure is how the object interacts with
144configfs.
145
146Whether statically defined in a source file or created by a parent
147config_group, a config_item must have one of the _init() functions
148called on it. This initializes the reference count and sets up the
149appropriate fields.
150
151All users of a config_item should have a reference on it via
152config_item_get(), and drop the reference when they are done via
153config_item_put().
154
155By itself, a config_item cannot do much more than appear in configfs.
156Usually a subsystem wants the item to display and/or store attributes,
157among other things. For that, it needs a type.
158
159[struct config_item_type]
160
161 struct configfs_item_operations {
162 void (*release)(struct config_item *);
163 ssize_t (*show_attribute)(struct config_item *,
164 struct configfs_attribute *,
165 char *);
166 ssize_t (*store_attribute)(struct config_item *,
167 struct configfs_attribute *,
168 const char *, size_t);
169 int (*allow_link)(struct config_item *src,
170 struct config_item *target);
171 int (*drop_link)(struct config_item *src,
172 struct config_item *target);
173 };
174
175 struct config_item_type {
176 struct module *ct_owner;
177 struct configfs_item_operations *ct_item_ops;
178 struct configfs_group_operations *ct_group_ops;
179 struct configfs_attribute **ct_attrs;
180 };
181
182The most basic function of a config_item_type is to define what
183operations can be performed on a config_item. All items that have been
184allocated dynamically will need to provide the ct_item_ops->release()
185method. This method is called when the config_item's reference count
186reaches zero. Items that wish to display an attribute need to provide
187the ct_item_ops->show_attribute() method. Similarly, storing a new
188attribute value uses the store_attribute() method.
189
190[struct configfs_attribute]
191
192 struct configfs_attribute {
193 char *ca_name;
194 struct module *ca_owner;
195 mode_t ca_mode;
196 };
197
198When a config_item wants an attribute to appear as a file in the item's
199configfs directory, it must define a configfs_attribute describing it.
200It then adds the attribute to the NULL-terminated array
201config_item_type->ct_attrs. When the item appears in configfs, the
202attribute file will appear with the configfs_attribute->ca_name
203filename. configfs_attribute->ca_mode specifies the file permissions.
204
205If an attribute is readable and the config_item provides a
206ct_item_ops->show_attribute() method, that method will be called
207whenever userspace asks for a read(2) on the attribute. The converse
208will happen for write(2).
209
210[struct config_group]
211
Matt LaPlante4ae0edc2006-11-30 04:58:40 +0100212A config_item cannot live in a vacuum. The only way one can be created
Joel Becker7063fbf2005-12-15 14:29:43 -0800213is via mkdir(2) on a config_group. This will trigger creation of a
214child item.
215
216 struct config_group {
217 struct config_item cg_item;
218 struct list_head cg_children;
219 struct configfs_subsystem *cg_subsys;
220 struct config_group **default_groups;
221 };
222
223 void config_group_init(struct config_group *group);
224 void config_group_init_type_name(struct config_group *group,
225 const char *name,
226 struct config_item_type *type);
227
228
229The config_group structure contains a config_item. Properly configuring
230that item means that a group can behave as an item in its own right.
231However, it can do more: it can create child items or groups. This is
232accomplished via the group operations specified on the group's
233config_item_type.
234
235 struct configfs_group_operations {
Joel Becker11c3b792008-06-12 14:00:18 -0700236 int (*make_item)(struct config_group *group,
237 const char *name,
238 struct config_item **new_item);
239 int (*make_group)(struct config_group *group,
240 const char *name,
241 struct config_group **new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800242 int (*commit_item)(struct config_item *item);
Joel Becker299894c2006-10-06 17:33:23 -0700243 void (*disconnect_notify)(struct config_group *group,
244 struct config_item *item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800245 void (*drop_item)(struct config_group *group,
246 struct config_item *item);
247 };
248
249A group creates child items by providing the
250ct_group_ops->make_item() method. If provided, this method is called from mkdir(2) in the group's directory. The subsystem allocates a new
251config_item (or more likely, its container structure), initializes it,
252and returns it to configfs. Configfs will then populate the filesystem
253tree to reflect the new item.
254
255If the subsystem wants the child to be a group itself, the subsystem
256provides ct_group_ops->make_group(). Everything else behaves the same,
257using the group _init() functions on the group.
258
259Finally, when userspace calls rmdir(2) on the item or group,
260ct_group_ops->drop_item() is called. As a config_group is also a
Matt LaPlante53cb4722006-10-03 22:55:17 +0200261config_item, it is not necessary for a separate drop_group() method.
Joel Becker7063fbf2005-12-15 14:29:43 -0800262The subsystem must config_item_put() the reference that was initialized
263upon item allocation. If a subsystem has no work to do, it may omit
264the ct_group_ops->drop_item() method, and configfs will call
265config_item_put() on the item on behalf of the subsystem.
266
267IMPORTANT: drop_item() is void, and as such cannot fail. When rmdir(2)
268is called, configfs WILL remove the item from the filesystem tree
269(assuming that it has no children to keep it busy). The subsystem is
270responsible for responding to this. If the subsystem has references to
271the item in other threads, the memory is safe. It may take some time
272for the item to actually disappear from the subsystem's usage. But it
273is gone from configfs.
274
Joel Becker299894c2006-10-06 17:33:23 -0700275When drop_item() is called, the item's linkage has already been torn
276down. It no longer has a reference on its parent and has no place in
277the item hierarchy. If a client needs to do some cleanup before this
278teardown happens, the subsystem can implement the
279ct_group_ops->disconnect_notify() method. The method is called after
280configfs has removed the item from the filesystem view but before the
281item is removed from its parent group. Like drop_item(),
282disconnect_notify() is void and cannot fail. Client subsystems should
283not drop any references here, as they still must do it in drop_item().
284
Joel Becker7063fbf2005-12-15 14:29:43 -0800285A config_group cannot be removed while it still has child items. This
286is implemented in the configfs rmdir(2) code. ->drop_item() will not be
287called, as the item has not been dropped. rmdir(2) will fail, as the
288directory is not empty.
289
290[struct configfs_subsystem]
291
Matt LaPlante4ae0edc2006-11-30 04:58:40 +0100292A subsystem must register itself, usually at module_init time. This
Joel Becker7063fbf2005-12-15 14:29:43 -0800293tells configfs to make the subsystem appear in the file tree.
294
295 struct configfs_subsystem {
296 struct config_group su_group;
Joel Beckere6bd07a2007-07-06 23:33:17 -0700297 struct mutex su_mutex;
Joel Becker7063fbf2005-12-15 14:29:43 -0800298 };
299
300 int configfs_register_subsystem(struct configfs_subsystem *subsys);
301 void configfs_unregister_subsystem(struct configfs_subsystem *subsys);
302
Joel Beckere6bd07a2007-07-06 23:33:17 -0700303 A subsystem consists of a toplevel config_group and a mutex.
Joel Becker7063fbf2005-12-15 14:29:43 -0800304The group is where child config_items are created. For a subsystem,
305this group is usually defined statically. Before calling
306configfs_register_subsystem(), the subsystem must have initialized the
307group via the usual group _init() functions, and it must also have
Joel Beckere6bd07a2007-07-06 23:33:17 -0700308initialized the mutex.
Joel Becker7063fbf2005-12-15 14:29:43 -0800309 When the register call returns, the subsystem is live, and it
310will be visible via configfs. At that point, mkdir(2) can be called and
311the subsystem must be ready for it.
312
313[An Example]
314
315The best example of these basic concepts is the simple_children
316subsystem/group and the simple_child item in configfs_example.c It
317shows a trivial object displaying and storing an attribute, and a simple
318group creating and destroying these children.
319
Joel Beckere6bd07a2007-07-06 23:33:17 -0700320[Hierarchy Navigation and the Subsystem Mutex]
Joel Becker7063fbf2005-12-15 14:29:43 -0800321
322There is an extra bonus that configfs provides. The config_groups and
323config_items are arranged in a hierarchy due to the fact that they
324appear in a filesystem. A subsystem is NEVER to touch the filesystem
325parts, but the subsystem might be interested in this hierarchy. For
326this reason, the hierarchy is mirrored via the config_group->cg_children
327and config_item->ci_parent structure members.
328
329A subsystem can navigate the cg_children list and the ci_parent pointer
330to see the tree created by the subsystem. This can race with configfs'
Joel Beckere6bd07a2007-07-06 23:33:17 -0700331management of the hierarchy, so configfs uses the subsystem mutex to
Joel Becker7063fbf2005-12-15 14:29:43 -0800332protect modifications. Whenever a subsystem wants to navigate the
333hierarchy, it must do so under the protection of the subsystem
Joel Beckere6bd07a2007-07-06 23:33:17 -0700334mutex.
Joel Becker7063fbf2005-12-15 14:29:43 -0800335
Joel Beckere6bd07a2007-07-06 23:33:17 -0700336A subsystem will be prevented from acquiring the mutex while a newly
Joel Becker7063fbf2005-12-15 14:29:43 -0800337allocated item has not been linked into this hierarchy. Similarly, it
Joel Beckere6bd07a2007-07-06 23:33:17 -0700338will not be able to acquire the mutex while a dropping item has not
Joel Becker7063fbf2005-12-15 14:29:43 -0800339yet been unlinked. This means that an item's ci_parent pointer will
340never be NULL while the item is in configfs, and that an item will only
341be in its parent's cg_children list for the same duration. This allows
342a subsystem to trust ci_parent and cg_children while they hold the
Joel Beckere6bd07a2007-07-06 23:33:17 -0700343mutex.
Joel Becker7063fbf2005-12-15 14:29:43 -0800344
345[Item Aggregation Via symlink(2)]
346
347configfs provides a simple group via the group->item parent/child
348relationship. Often, however, a larger environment requires aggregation
349outside of the parent/child connection. This is implemented via
350symlink(2).
351
352A config_item may provide the ct_item_ops->allow_link() and
353ct_item_ops->drop_link() methods. If the ->allow_link() method exists,
354symlink(2) may be called with the config_item as the source of the link.
355These links are only allowed between configfs config_items. Any
356symlink(2) attempt outside the configfs filesystem will be denied.
357
358When symlink(2) is called, the source config_item's ->allow_link()
359method is called with itself and a target item. If the source item
360allows linking to target item, it returns 0. A source item may wish to
361reject a link if it only wants links to a certain type of object (say,
362in its own subsystem).
363
364When unlink(2) is called on the symbolic link, the source item is
365notified via the ->drop_link() method. Like the ->drop_item() method,
366this is a void function and cannot return failure. The subsystem is
367responsible for responding to the change.
368
369A config_item cannot be removed while it links to any other item, nor
370can it be removed while an item links to it. Dangling symlinks are not
371allowed in configfs.
372
373[Automatically Created Subgroups]
374
375A new config_group may want to have two types of child config_items.
376While this could be codified by magic names in ->make_item(), it is much
377more explicit to have a method whereby userspace sees this divergence.
378
379Rather than have a group where some items behave differently than
380others, configfs provides a method whereby one or many subgroups are
381automatically created inside the parent at its creation. Thus,
Masatake YAMATO48cc7ec2008-02-03 16:10:08 +0200382mkdir("parent") results in "parent", "parent/subgroup1", up through
Joel Becker7063fbf2005-12-15 14:29:43 -0800383"parent/subgroupN". Items of type 1 can now be created in
384"parent/subgroup1", and items of type N can be created in
385"parent/subgroupN".
386
387These automatic subgroups, or default groups, do not preclude other
388children of the parent group. If ct_group_ops->make_group() exists,
389other child groups can be created on the parent group directly.
390
391A configfs subsystem specifies default groups by filling in the
392NULL-terminated array default_groups on the config_group structure.
393Each group in that array is populated in the configfs tree at the same
394time as the parent group. Similarly, they are removed at the same time
395as the parent. No extra notification is provided. When a ->drop_item()
396method call notifies the subsystem the parent group is going away, it
397also means every default group child associated with that parent group.
398
399As a consequence of this, default_groups cannot be removed directly via
400rmdir(2). They also are not considered when rmdir(2) on the parent
401group is checking for children.
402
Joel Becker631d1fe2007-06-18 18:06:09 -0700403[Dependant Subsystems]
404
405Sometimes other drivers depend on particular configfs items. For
406example, ocfs2 mounts depend on a heartbeat region item. If that
407region item is removed with rmdir(2), the ocfs2 mount must BUG or go
408readonly. Not happy.
409
410configfs provides two additional API calls: configfs_depend_item() and
411configfs_undepend_item(). A client driver can call
412configfs_depend_item() on an existing item to tell configfs that it is
413depended on. configfs will then return -EBUSY from rmdir(2) for that
414item. When the item is no longer depended on, the client driver calls
415configfs_undepend_item() on it.
416
417These API cannot be called underneath any configfs callbacks, as
418they will conflict. They can block and allocate. A client driver
419probably shouldn't calling them of its own gumption. Rather it should
420be providing an API that external subsystems call.
421
422How does this work? Imagine the ocfs2 mount process. When it mounts,
423it asks for a heartbeat region item. This is done via a call into the
424heartbeat code. Inside the heartbeat code, the region item is looked
425up. Here, the heartbeat code calls configfs_depend_item(). If it
426succeeds, then heartbeat knows the region is safe to give to ocfs2.
427If it fails, it was being torn down anyway, and heartbeat can gracefully
428pass up an error.
429
Joel Becker7063fbf2005-12-15 14:29:43 -0800430[Committable Items]
431
432NOTE: Committable items are currently unimplemented.
433
434Some config_items cannot have a valid initial state. That is, no
435default values can be specified for the item's attributes such that the
436item can do its work. Userspace must configure one or more attributes,
437after which the subsystem can start whatever entity this item
438represents.
439
440Consider the FakeNBD device from above. Without a target address *and*
441a target device, the subsystem has no idea what block device to import.
442The simple example assumes that the subsystem merely waits until all the
443appropriate attributes are configured, and then connects. This will,
444indeed, work, but now every attribute store must check if the attributes
445are initialized. Every attribute store must fire off the connection if
446that condition is met.
447
448Far better would be an explicit action notifying the subsystem that the
449config_item is ready to go. More importantly, an explicit action allows
Matt LaPlante3f6dee92006-10-03 22:45:33 +0200450the subsystem to provide feedback as to whether the attributes are
Joel Becker7063fbf2005-12-15 14:29:43 -0800451initialized in a way that makes sense. configfs provides this as
452committable items.
453
454configfs still uses only normal filesystem operations. An item is
455committed via rename(2). The item is moved from a directory where it
456can be modified to a directory where it cannot.
457
458Any group that provides the ct_group_ops->commit_item() method has
459committable items. When this group appears in configfs, mkdir(2) will
460not work directly in the group. Instead, the group will have two
461subdirectories: "live" and "pending". The "live" directory does not
462support mkdir(2) or rmdir(2) either. It only allows rename(2). The
463"pending" directory does allow mkdir(2) and rmdir(2). An item is
464created in the "pending" directory. Its attributes can be modified at
465will. Userspace commits the item by renaming it into the "live"
Matt LaPlanted6bc8ac2006-10-03 22:54:15 +0200466directory. At this point, the subsystem receives the ->commit_item()
Joel Becker7063fbf2005-12-15 14:29:43 -0800467callback. If all required attributes are filled to satisfaction, the
468method returns zero and the item is moved to the "live" directory.
469
470As rmdir(2) does not work in the "live" directory, an item must be
471shutdown, or "uncommitted". Again, this is done via rename(2), this
472time from the "live" directory back to the "pending" one. The subsystem
473is notified by the ct_group_ops->uncommit_object() method.
474
475