2 * vim: noexpandtab ts=8 sts=0 sw=8:
4 * configfs_example_explicit.c - This file is a demonstration module
5 * containing a number of configfs subsystems. It explicitly defines
6 * each structure without using the helper macros defined in
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 021110-1307, USA.
25 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
27 * configfs Copyright (C) 2005 Oracle. All rights reserved.
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
34 #include <linux/configfs.h>
41 * This first example is a childless subsystem. It cannot create
42 * any config_items. It just has attributes.
44 * Note that we are enclosing the configfs_subsystem inside a container.
45 * This is not necessary if a subsystem has no attributes directly
46 * on the subsystem. See the next example, 02-simple-children, for
51 struct configfs_subsystem subsys
;
56 struct childless_attribute
{
57 struct configfs_attribute attr
;
58 ssize_t (*show
)(struct childless
*, char *);
59 ssize_t (*store
)(struct childless
*, const char *, size_t);
62 static inline struct childless
*to_childless(struct config_item
*item
)
64 return item
? container_of(to_configfs_subsystem(to_config_group(item
)), struct childless
, subsys
) : NULL
;
67 static ssize_t
childless_showme_read(struct childless
*childless
,
72 pos
= sprintf(page
, "%d\n", childless
->showme
);
78 static ssize_t
childless_storeme_read(struct childless
*childless
,
81 return sprintf(page
, "%d\n", childless
->storeme
);
84 static ssize_t
childless_storeme_write(struct childless
*childless
,
89 char *p
= (char *) page
;
91 tmp
= simple_strtoul(p
, &p
, 10);
92 if ((*p
!= '\0') && (*p
!= '\n'))
98 childless
->storeme
= tmp
;
103 static ssize_t
childless_description_read(struct childless
*childless
,
109 "The childless subsystem is the simplest possible subsystem in\n"
110 "configfs. It does not support the creation of child config_items.\n"
111 "It only has a few attributes. In fact, it isn't much different\n"
112 "than a directory in /proc.\n");
115 static struct childless_attribute childless_attr_showme
= {
116 .attr
= { .ca_owner
= THIS_MODULE
, .ca_name
= "showme", .ca_mode
= S_IRUGO
},
117 .show
= childless_showme_read
,
119 static struct childless_attribute childless_attr_storeme
= {
120 .attr
= { .ca_owner
= THIS_MODULE
, .ca_name
= "storeme", .ca_mode
= S_IRUGO
| S_IWUSR
},
121 .show
= childless_storeme_read
,
122 .store
= childless_storeme_write
,
124 static struct childless_attribute childless_attr_description
= {
125 .attr
= { .ca_owner
= THIS_MODULE
, .ca_name
= "description", .ca_mode
= S_IRUGO
},
126 .show
= childless_description_read
,
129 static struct configfs_attribute
*childless_attrs
[] = {
130 &childless_attr_showme
.attr
,
131 &childless_attr_storeme
.attr
,
132 &childless_attr_description
.attr
,
136 static ssize_t
childless_attr_show(struct config_item
*item
,
137 struct configfs_attribute
*attr
,
140 struct childless
*childless
= to_childless(item
);
141 struct childless_attribute
*childless_attr
=
142 container_of(attr
, struct childless_attribute
, attr
);
145 if (childless_attr
->show
)
146 ret
= childless_attr
->show(childless
, page
);
150 static ssize_t
childless_attr_store(struct config_item
*item
,
151 struct configfs_attribute
*attr
,
152 const char *page
, size_t count
)
154 struct childless
*childless
= to_childless(item
);
155 struct childless_attribute
*childless_attr
=
156 container_of(attr
, struct childless_attribute
, attr
);
157 ssize_t ret
= -EINVAL
;
159 if (childless_attr
->store
)
160 ret
= childless_attr
->store(childless
, page
, count
);
164 static struct configfs_item_operations childless_item_ops
= {
165 .show_attribute
= childless_attr_show
,
166 .store_attribute
= childless_attr_store
,
169 static struct config_item_type childless_type
= {
170 .ct_item_ops
= &childless_item_ops
,
171 .ct_attrs
= childless_attrs
,
172 .ct_owner
= THIS_MODULE
,
175 static struct childless childless_subsys
= {
179 .ci_namebuf
= "01-childless",
180 .ci_type
= &childless_type
,
187 /* ----------------------------------------------------------------- */
192 * This example merely has a simple one-attribute child. Note that
193 * there is no extra attribute structure, as the child's attribute is
194 * known from the get-go. Also, there is no container for the
195 * subsystem, as it has no attributes of its own.
198 struct simple_child
{
199 struct config_item item
;
203 static inline struct simple_child
*to_simple_child(struct config_item
*item
)
205 return item
? container_of(item
, struct simple_child
, item
) : NULL
;
208 static struct configfs_attribute simple_child_attr_storeme
= {
209 .ca_owner
= THIS_MODULE
,
210 .ca_name
= "storeme",
211 .ca_mode
= S_IRUGO
| S_IWUSR
,
214 static struct configfs_attribute
*simple_child_attrs
[] = {
215 &simple_child_attr_storeme
,
219 static ssize_t
simple_child_attr_show(struct config_item
*item
,
220 struct configfs_attribute
*attr
,
224 struct simple_child
*simple_child
= to_simple_child(item
);
226 count
= sprintf(page
, "%d\n", simple_child
->storeme
);
231 static ssize_t
simple_child_attr_store(struct config_item
*item
,
232 struct configfs_attribute
*attr
,
233 const char *page
, size_t count
)
235 struct simple_child
*simple_child
= to_simple_child(item
);
237 char *p
= (char *) page
;
239 tmp
= simple_strtoul(p
, &p
, 10);
240 if (!p
|| (*p
&& (*p
!= '\n')))
246 simple_child
->storeme
= tmp
;
251 static void simple_child_release(struct config_item
*item
)
253 kfree(to_simple_child(item
));
256 static struct configfs_item_operations simple_child_item_ops
= {
257 .release
= simple_child_release
,
258 .show_attribute
= simple_child_attr_show
,
259 .store_attribute
= simple_child_attr_store
,
262 static struct config_item_type simple_child_type
= {
263 .ct_item_ops
= &simple_child_item_ops
,
264 .ct_attrs
= simple_child_attrs
,
265 .ct_owner
= THIS_MODULE
,
269 struct simple_children
{
270 struct config_group group
;
273 static inline struct simple_children
*to_simple_children(struct config_item
*item
)
275 return item
? container_of(to_config_group(item
), struct simple_children
, group
) : NULL
;
278 static struct config_item
*simple_children_make_item(struct config_group
*group
, const char *name
)
280 struct simple_child
*simple_child
;
282 simple_child
= kzalloc(sizeof(struct simple_child
), GFP_KERNEL
);
284 return ERR_PTR(-ENOMEM
);
286 config_item_init_type_name(&simple_child
->item
, name
,
289 simple_child
->storeme
= 0;
291 return &simple_child
->item
;
294 static struct configfs_attribute simple_children_attr_description
= {
295 .ca_owner
= THIS_MODULE
,
296 .ca_name
= "description",
300 static struct configfs_attribute
*simple_children_attrs
[] = {
301 &simple_children_attr_description
,
305 static ssize_t
simple_children_attr_show(struct config_item
*item
,
306 struct configfs_attribute
*attr
,
310 "[02-simple-children]\n"
312 "This subsystem allows the creation of child config_items. These\n"
313 "items have only one attribute that is readable and writeable.\n");
316 static void simple_children_release(struct config_item
*item
)
318 kfree(to_simple_children(item
));
321 static struct configfs_item_operations simple_children_item_ops
= {
322 .release
= simple_children_release
,
323 .show_attribute
= simple_children_attr_show
,
327 * Note that, since no extra work is required on ->drop_item(),
328 * no ->drop_item() is provided.
330 static struct configfs_group_operations simple_children_group_ops
= {
331 .make_item
= simple_children_make_item
,
334 static struct config_item_type simple_children_type
= {
335 .ct_item_ops
= &simple_children_item_ops
,
336 .ct_group_ops
= &simple_children_group_ops
,
337 .ct_attrs
= simple_children_attrs
,
338 .ct_owner
= THIS_MODULE
,
341 static struct configfs_subsystem simple_children_subsys
= {
344 .ci_namebuf
= "02-simple-children",
345 .ci_type
= &simple_children_type
,
351 /* ----------------------------------------------------------------- */
356 * This example reuses the simple_children group from above. However,
357 * the simple_children group is not the subsystem itself, it is a
358 * child of the subsystem. Creation of a group in the subsystem creates
359 * a new simple_children group. That group can then have simple_child
360 * children of its own.
363 static struct config_group
*group_children_make_group(struct config_group
*group
, const char *name
)
365 struct simple_children
*simple_children
;
367 simple_children
= kzalloc(sizeof(struct simple_children
),
369 if (!simple_children
)
370 return ERR_PTR(-ENOMEM
);
372 config_group_init_type_name(&simple_children
->group
, name
,
373 &simple_children_type
);
375 return &simple_children
->group
;
378 static struct configfs_attribute group_children_attr_description
= {
379 .ca_owner
= THIS_MODULE
,
380 .ca_name
= "description",
384 static struct configfs_attribute
*group_children_attrs
[] = {
385 &group_children_attr_description
,
389 static ssize_t
group_children_attr_show(struct config_item
*item
,
390 struct configfs_attribute
*attr
,
394 "[03-group-children]\n"
396 "This subsystem allows the creation of child config_groups. These\n"
397 "groups are like the subsystem simple-children.\n");
400 static struct configfs_item_operations group_children_item_ops
= {
401 .show_attribute
= group_children_attr_show
,
405 * Note that, since no extra work is required on ->drop_item(),
406 * no ->drop_item() is provided.
408 static struct configfs_group_operations group_children_group_ops
= {
409 .make_group
= group_children_make_group
,
412 static struct config_item_type group_children_type
= {
413 .ct_item_ops
= &group_children_item_ops
,
414 .ct_group_ops
= &group_children_group_ops
,
415 .ct_attrs
= group_children_attrs
,
416 .ct_owner
= THIS_MODULE
,
419 static struct configfs_subsystem group_children_subsys
= {
422 .ci_namebuf
= "03-group-children",
423 .ci_type
= &group_children_type
,
428 /* ----------------------------------------------------------------- */
431 * We're now done with our subsystem definitions.
432 * For convenience in this module, here's a list of them all. It
433 * allows the init function to easily register them. Most modules
434 * will only have one subsystem, and will only call register_subsystem
437 static struct configfs_subsystem
*example_subsys
[] = {
438 &childless_subsys
.subsys
,
439 &simple_children_subsys
,
440 &group_children_subsys
,
444 static int __init
configfs_example_init(void)
448 struct configfs_subsystem
*subsys
;
450 for (i
= 0; example_subsys
[i
]; i
++) {
451 subsys
= example_subsys
[i
];
453 config_group_init(&subsys
->su_group
);
454 mutex_init(&subsys
->su_mutex
);
455 ret
= configfs_register_subsystem(subsys
);
457 printk(KERN_ERR
"Error %d while registering subsystem %s\n",
459 subsys
->su_group
.cg_item
.ci_namebuf
);
467 for (i
--; i
>= 0; i
--)
468 configfs_unregister_subsystem(example_subsys
[i
]);
473 static void __exit
configfs_example_exit(void)
477 for (i
= 0; example_subsys
[i
]; i
++)
478 configfs_unregister_subsystem(example_subsys
[i
]);
481 module_init(configfs_example_init
);
482 module_exit(configfs_example_exit
);
483 MODULE_LICENSE("GPL");