2 * vim: noexpandtab ts=8 sts=0 sw=8:
4 * configfs_example.c - This file is a demonstration module containing
5 * a number of configfs subsystems.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 021110-1307, USA.
23 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
25 * configfs Copyright (C) 2005 Oracle. All rights reserved.
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
32 #include <linux/configfs.h>
39 * This first example is a childless subsystem. It cannot create
40 * any config_items. It just has attributes.
42 * Note that we are enclosing the configfs_subsystem inside a container.
43 * This is not necessary if a subsystem has no attributes directly
44 * on the subsystem. See the next example, 02-simple-children, for
49 struct configfs_subsystem subsys;
54 struct childless_attribute {
55 struct configfs_attribute attr;
56 ssize_t (*show)(struct childless *, char *);
57 ssize_t (*store)(struct childless *, const char *, size_t);
60 static inline struct childless *to_childless(struct config_item *item)
62 return item ? container_of(to_configfs_subsystem(to_config_group(item)), struct childless, subsys) : NULL;
65 static ssize_t childless_showme_read(struct childless *childless,
70 pos = sprintf(page, "%d\n", childless->showme);
76 static ssize_t childless_storeme_read(struct childless *childless,
79 return sprintf(page, "%d\n", childless->storeme);
82 static ssize_t childless_storeme_write(struct childless *childless,
87 char *p = (char *) page;
89 tmp = simple_strtoul(p, &p, 10);
90 if (!p || (*p && (*p != '\n')))
96 childless->storeme = tmp;
101 static ssize_t childless_description_read(struct childless *childless,
107 "The childless subsystem is the simplest possible subsystem in\n"
108 "configfs. It does not support the creation of child config_items.\n"
109 "It only has a few attributes. In fact, it isn't much different\n"
110 "than a directory in /proc.\n");
113 static struct childless_attribute childless_attr_showme = {
114 .attr = { .ca_owner = THIS_MODULE, .ca_name = "showme", .ca_mode = S_IRUGO },
115 .show = childless_showme_read,
117 static struct childless_attribute childless_attr_storeme = {
118 .attr = { .ca_owner = THIS_MODULE, .ca_name = "storeme", .ca_mode = S_IRUGO | S_IWUSR },
119 .show = childless_storeme_read,
120 .store = childless_storeme_write,
122 static struct childless_attribute childless_attr_description = {
123 .attr = { .ca_owner = THIS_MODULE, .ca_name = "description", .ca_mode = S_IRUGO },
124 .show = childless_description_read,
127 static struct configfs_attribute *childless_attrs[] = {
128 &childless_attr_showme.attr,
129 &childless_attr_storeme.attr,
130 &childless_attr_description.attr,
134 static ssize_t childless_attr_show(struct config_item *item,
135 struct configfs_attribute *attr,
138 struct childless *childless = to_childless(item);
139 struct childless_attribute *childless_attr =
140 container_of(attr, struct childless_attribute, attr);
143 if (childless_attr->show)
144 ret = childless_attr->show(childless, page);
148 static ssize_t childless_attr_store(struct config_item *item,
149 struct configfs_attribute *attr,
150 const char *page, size_t count)
152 struct childless *childless = to_childless(item);
153 struct childless_attribute *childless_attr =
154 container_of(attr, struct childless_attribute, attr);
155 ssize_t ret = -EINVAL;
157 if (childless_attr->store)
158 ret = childless_attr->store(childless, page, count);
162 static struct configfs_item_operations childless_item_ops = {
163 .show_attribute = childless_attr_show,
164 .store_attribute = childless_attr_store,
167 static struct config_item_type childless_type = {
168 .ct_item_ops = &childless_item_ops,
169 .ct_attrs = childless_attrs,
170 .ct_owner = THIS_MODULE,
173 static struct childless childless_subsys = {
177 .ci_namebuf = "01-childless",
178 .ci_type = &childless_type,
185 /* ----------------------------------------------------------------- */
190 * This example merely has a simple one-attribute child. Note that
191 * there is no extra attribute structure, as the child's attribute is
192 * known from the get-go. Also, there is no container for the
193 * subsystem, as it has no attributes of its own.
196 struct simple_child {
197 struct config_item item;
201 static inline struct simple_child *to_simple_child(struct config_item *item)
203 return item ? container_of(item, struct simple_child, item) : NULL;
206 static struct configfs_attribute simple_child_attr_storeme = {
207 .ca_owner = THIS_MODULE,
208 .ca_name = "storeme",
209 .ca_mode = S_IRUGO | S_IWUSR,
212 static struct configfs_attribute *simple_child_attrs[] = {
213 &simple_child_attr_storeme,
217 static ssize_t simple_child_attr_show(struct config_item *item,
218 struct configfs_attribute *attr,
222 struct simple_child *simple_child = to_simple_child(item);
224 count = sprintf(page, "%d\n", simple_child->storeme);
229 static ssize_t simple_child_attr_store(struct config_item *item,
230 struct configfs_attribute *attr,
231 const char *page, size_t count)
233 struct simple_child *simple_child = to_simple_child(item);
235 char *p = (char *) page;
237 tmp = simple_strtoul(p, &p, 10);
238 if (!p || (*p && (*p != '\n')))
244 simple_child->storeme = tmp;
249 static void simple_child_release(struct config_item *item)
251 kfree(to_simple_child(item));
254 static struct configfs_item_operations simple_child_item_ops = {
255 .release = simple_child_release,
256 .show_attribute = simple_child_attr_show,
257 .store_attribute = simple_child_attr_store,
260 static struct config_item_type simple_child_type = {
261 .ct_item_ops = &simple_child_item_ops,
262 .ct_attrs = simple_child_attrs,
263 .ct_owner = THIS_MODULE,
267 static struct config_item *simple_children_make_item(struct config_group *group, const char *name)
269 struct simple_child *simple_child;
271 simple_child = kmalloc(sizeof(struct simple_child), GFP_KERNEL);
275 memset(simple_child, 0, sizeof(struct simple_child));
277 config_item_init_type_name(&simple_child->item, name,
280 simple_child->storeme = 0;
282 return &simple_child->item;
285 static struct configfs_attribute simple_children_attr_description = {
286 .ca_owner = THIS_MODULE,
287 .ca_name = "description",
291 static struct configfs_attribute *simple_children_attrs[] = {
292 &simple_children_attr_description,
296 static ssize_t simple_children_attr_show(struct config_item *item,
297 struct configfs_attribute *attr,
301 "[02-simple-children]\n"
303 "This subsystem allows the creation of child config_items. These\n"
304 "items have only one attribute that is readable and writeable.\n");
307 static struct configfs_item_operations simple_children_item_ops = {
308 .show_attribute = simple_children_attr_show,
312 * Note that, since no extra work is required on ->drop_item(),
313 * no ->drop_item() is provided.
315 static struct configfs_group_operations simple_children_group_ops = {
316 .make_item = simple_children_make_item,
319 static struct config_item_type simple_children_type = {
320 .ct_item_ops = &simple_children_item_ops,
321 .ct_group_ops = &simple_children_group_ops,
322 .ct_attrs = simple_children_attrs,
325 static struct configfs_subsystem simple_children_subsys = {
328 .ci_namebuf = "02-simple-children",
329 .ci_type = &simple_children_type,
335 /* ----------------------------------------------------------------- */
340 * This example reuses the simple_children group from above. However,
341 * the simple_children group is not the subsystem itself, it is a
342 * child of the subsystem. Creation of a group in the subsystem creates
343 * a new simple_children group. That group can then have simple_child
344 * children of its own.
347 struct simple_children {
348 struct config_group group;
351 static struct config_group *group_children_make_group(struct config_group *group, const char *name)
353 struct simple_children *simple_children;
355 simple_children = kmalloc(sizeof(struct simple_children),
357 if (!simple_children)
360 memset(simple_children, 0, sizeof(struct simple_children));
362 config_group_init_type_name(&simple_children->group, name,
363 &simple_children_type);
365 return &simple_children->group;
368 static struct configfs_attribute group_children_attr_description = {
369 .ca_owner = THIS_MODULE,
370 .ca_name = "description",
374 static struct configfs_attribute *group_children_attrs[] = {
375 &group_children_attr_description,
379 static ssize_t group_children_attr_show(struct config_item *item,
380 struct configfs_attribute *attr,
384 "[03-group-children]\n"
386 "This subsystem allows the creation of child config_groups. These\n"
387 "groups are like the subsystem simple-children.\n");
390 static struct configfs_item_operations group_children_item_ops = {
391 .show_attribute = group_children_attr_show,
395 * Note that, since no extra work is required on ->drop_item(),
396 * no ->drop_item() is provided.
398 static struct configfs_group_operations group_children_group_ops = {
399 .make_group = group_children_make_group,
402 static struct config_item_type group_children_type = {
403 .ct_item_ops = &group_children_item_ops,
404 .ct_group_ops = &group_children_group_ops,
405 .ct_attrs = group_children_attrs,
408 static struct configfs_subsystem group_children_subsys = {
411 .ci_namebuf = "03-group-children",
412 .ci_type = &group_children_type,
417 /* ----------------------------------------------------------------- */
420 * We're now done with our subsystem definitions.
421 * For convenience in this module, here's a list of them all. It
422 * allows the init function to easily register them. Most modules
423 * will only have one subsystem, and will only call register_subsystem
426 static struct configfs_subsystem *example_subsys[] = {
427 &childless_subsys.subsys,
428 &simple_children_subsys,
429 &group_children_subsys,
433 static int __init configfs_example_init(void)
437 struct configfs_subsystem *subsys;
439 for (i = 0; example_subsys[i]; i++) {
440 subsys = example_subsys[i];
442 config_group_init(&subsys->su_group);
443 init_MUTEX(&subsys->su_sem);
444 ret = configfs_register_subsystem(subsys);
446 printk(KERN_ERR "Error %d while registering subsystem %s\n",
448 subsys->su_group.cg_item.ci_namebuf);
456 for (; i >= 0; i--) {
457 configfs_unregister_subsystem(example_subsys[i]);
463 static void __exit configfs_example_exit(void)
467 for (i = 0; example_subsys[i]; i++) {
468 configfs_unregister_subsystem(example_subsys[i]);
472 module_init(configfs_example_init);
473 module_exit(configfs_example_exit);
474 MODULE_LICENSE("GPL");