A configfs config_item is created via an explicit userspace operation: mkdir(2). It is destroyed via rmdir(2). The attributes appear at mkdir(2) time, and can be read or modified via read(2) and write(2). As with sysfs, readdir(3) queries the list of items and/or attributes. symlink(2) can be used to group items together. Unlike sysfs, the lifetime of the representation is completely driven by userspace. The kernel modules backing the items must respond to this.
Both sysfs and configfs can and should exist together on the same system. One is not a replacement for the other.
configfs can be compiled as a module or into the kernel. You can access it by doing
mount -t configfs none /config
The configfs tree will be empty unless client modules are also loaded. These are modules that register their item types with configfs as subsystems. Once a client subsystem is loaded, it will appear as a subdirectory (or more than one) under /config. Like sysfs, the configfs tree is always there, whether mounted on /config or not.
An item is created via mkdir(2). The item's attributes will also appear at this time. readdir(3) can determine what the attributes are, read(2) can query their default values, and write(2) can store new values. Don't mix more than one attribute in one attribute file.
There are two types of configfs attributes:
* Normal attributes, which similar to sysfs attributes, are small ASCII text files, with a maximum size of one page (PAGE_SIZE, 4096 on i386). Preferably only one value per file should be used, and the same caveats from sysfs apply. Configfs expects write(2) to store the entire buffer at once. When writing to normal configfs attributes, userspace processes should first read the entire file, modify the portions they wish to change, and then write the entire buffer back.
* Binary attributes, which are somewhat similar to sysfs binary attributes, but with a few slight changes to semantics. The PAGE_SIZE limitation does not apply, but the whole binary item must fit in single kernel vmalloc'ed buffer. The write(2) calls from user space are buffered, and the attributes' write_bin_attribute method will be invoked on the final close, therefore it is imperative for user-space to check the return code of close(2) in order to verify that the operation finished successfully. To avoid a malicious user OOMing the kernel, there's a per-binary attribute maximum buffer value.
When an item needs to be destroyed, remove it with rmdir(2). An item cannot be destroyed if any other item has a link to it (via symlink(2)). Links can be removed via unlink(2).
九、Coding With configfs
Every object in configfs is a config_item. A config_item reflects an object in the subsystem. It has attributes that match values on that object. configfs handles the filesystem representation of that object and its attributes, allowing the subsystem to ignore all but the basic show/store interaction.
Items are created and destroyed inside a config_group. A group is a collection of items that share the same attributes and operations. Items are created by mkdir(2) and removed by rmdir(2), but configfs handles that. The group has a set of operations to perform these tasks
A subsystem is the top level of a client module. During initialization, the client module registers the subsystem with configfs, the subsystem appears as a directory at the top of the configfs filesystem. A subsystem is also a config_group, and can do everything a config_group can.
struct configfs_subsystem {
struct config_group su_group;
struct mutex su_mutex;
};
-r–r–r–. 1 root root 4096 Sep 27 05:16 description
-r–r–r–. 1 root root 4096 Sep 27 05:16 showme
-rw-r–r–. 1 root root 4096 Sep 27 05:16 storeme
37/*
38 * 01-childless
39 *
40 * This first example is a childless subsystem. It cannot create
41 * any config_items. It just has attributes.
42 *
43 * Note that we are enclosing the configfs_subsystem inside a container.
44 * This is not necessary if a subsystem has no attributes directly
45 * on the subsystem. See the next example, 02-simple-children, for
46 * such a subsystem.
47 */
48
49struct childless {
50 struct configfs_subsystem subsys;
51 int showme;
52 int storeme;
53};
创建一个结构体包含了configfs子系统,两个属性showme, storeme放在了这里。
138static struct childless childless_subsys = {
139 .subsys = {
140 .su_group = {
141 .cg_item = {
142 .ci_namebuf = "01-childless",
143 .ci_type = &childless_type,
144 },
145 },
146 },
147};
142行,定义目录名称。
133行,属性操作定义在128行和129行,这两个函数又是由126行宏定义的。126行宏依赖一个函数to_##_item,其中_item是宏的传入参数,这个函数的作用是从struct config_item转换到第49行的结构,然后作为函数show_attribute和store_attribute的传入参数。
407static int __init configfs_example_init(void)
408{
409 int ret;
410 int i;
411 struct configfs_subsystem *subsys;
412
413 for (i = 0; example_subsys[i]; i++) {
414 subsys = example_subsys[i];
415
416 config_group_init(&subsys->su_group);
417 mutex_init(&subsys->su_mutex);
418 ret = configfs_register_subsystem(subsys);
419 if (ret) {
420 printk(KERN_ERR "Error %d while registering subsystem %s\n",
421 ret,
422 subsys->su_group.cg_item.ci_namebuf);
423 goto out_unregister;
424 }
425 }
426
427 return 0;
416-417行,初始化子系统。
304static struct configfs_subsystem simple_children_subsys = {
305 .su_group = {
306 .cg_item = {
307 .ci_namebuf = "02-simple-children",
308 .ci_type = &simple_children_type,
309 },
310 },
311};
接着定义config_item_type,
289/*
290 * Note that, since no extra work is required on ->drop_item(),
291 * no ->drop_item() is provided.
292 */
293static struct configfs_group_operations simple_children_group_ops = {
294 .make_item = simple_children_make_item,
295};
296
297static struct config_item_type simple_children_type = {
298 .ct_item_ops = &simple_children_item_ops,
299 .ct_group_ops = &simple_children_group_ops,
300 .ct_attrs = simple_children_attrs,
301 .ct_owner = THIS_MODULE,
302};
298行,定义了子系统根目录属性操作,跟示例1类似
232struct simple_children {
233 struct config_group group;
234};
235
236static inline struct simple_children *to_simple_children(struct config_item *item)
237{
238 return item ? container_of(to_config_group(item), struct simple_children, group) : NULL;
239}
240
241static struct config_item *simple_children_make_item(struct config_group *group, const char *name)
242{
243 struct simple_child *simple_child;
244
245 simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL);
246 if (!simple_child)
247 return ERR_PTR(-ENOMEM);
248
249 config_item_init_type_name(&simple_child->item, name,
250 &simple_child_type);
251
252 simple_child->storeme = 0;
253
254 return &simple_child->item;
255}
171static struct configfs_attribute simple_child_attr_storeme = {
172 .ca_owner = THIS_MODULE,
173 .ca_name = "storeme",
174 .ca_mode = S_IRUGO | S_IWUSR,
175};
176
177static struct configfs_attribute *simple_child_attrs[] = {
178 &simple_child_attr_storeme,
179 NULL,
180};
219static struct configfs_item_operations simple_child_item_ops = {
220 .release = simple_child_release,
221 .show_attribute = simple_child_attr_show,
222 .store_attribute = simple_child_attr_store,
223};
224
225static struct config_item_type simple_child_type = {
226 .ct_item_ops = &simple_child_item_ops,
227 .ct_attrs = simple_child_attrs,
228 .ct_owner = THIS_MODULE,
229};
225行,定义了config_item_type。
description
-r–r–r–. 1 root root 4096 Sep 28 21:24 description
drwxr-xr-x. 2 root root 0 Sep 28 21:24 test
.
|– description
`– test
`– storeme
1 directory, 2 files
0
1
description
.
|– description
`– test
|– description
|– test1
| `– storeme
|– test2
| `– storeme
`– test3
`– storeme
382static struct configfs_subsystem group_children_subsys = {
383 .su_group = {
384 .cg_item = {
385 .ci_namebuf = "03-group-children",
386 .ci_type = &group_children_type,
387 },
388 },
389};
371static struct configfs_group_operations group_children_group_ops = {
372 .make_group = group_children_make_group,
373};
374
375static struct config_item_type group_children_type = {
376 .ct_item_ops = &group_children_item_ops,
377 .ct_group_ops = &group_children_group_ops,
378 .ct_attrs = group_children_attrs,
379 .ct_owner = THIS_MODULE,
380};
326static struct config_group *group_children_make_group(struct config_group *group, const char *name)
327{
328 struct simple_children *simple_children;
329
330 simple_children = kzalloc(sizeof(struct simple_children),
331 GFP_KERNEL);
332 if (!simple_children)
333 return ERR_PTR(-ENOMEM);
334
335 config_group_init_type_name(&simple_children->group, name,
336 &simple_children_type);
337
338 return &simple_children->group;
339}