diff options
| author | Joel Becker <joel.becker@oracle.com> | 2008-06-18 22:29:05 -0400 |
|---|---|---|
| committer | Mark Fasheh <mfasheh@suse.com> | 2008-07-31 19:21:13 -0400 |
| commit | ecb3d28c7edd58b54f16838c434b342ba9195bec (patch) | |
| tree | 2babdc068dde924a905b0b25e673be0ca7a7dccd /Documentation/filesystems/configfs/configfs_example.c | |
| parent | 70526b67443a980d5029d9cf06903bef731a4e96 (diff) | |
[PATCH] configfs: Convenience macros for attribute definition.
Sysfs has the _ATTR() and _ATTR_RO() macros to make defining extended
form attributes easier. configfs should have something similiar.
- _CONFIGFS_ATTR() and _CONFIGFS_ATTR_RO() are the counterparts to the
sysfs macros.
- CONFIGFS_ATTR_STRUCT() creates the extended form attribute structure.
- CONFIGFS_ATTR_OPS() defines the show_attribute()/store_attribute()
operations that call the show()/store() operations of the extended
form configfs_attributes.
Signed-off-by: Joel Becker <joel.becker@oracle.com>
Signed-off-by: Mark Fasheh <mfasheh@suse.com>
Diffstat (limited to 'Documentation/filesystems/configfs/configfs_example.c')
| -rw-r--r-- | Documentation/filesystems/configfs/configfs_example.c | 485 |
1 files changed, 0 insertions, 485 deletions
diff --git a/Documentation/filesystems/configfs/configfs_example.c b/Documentation/filesystems/configfs/configfs_example.c deleted file mode 100644 index 039648791701..000000000000 --- a/Documentation/filesystems/configfs/configfs_example.c +++ /dev/null | |||
| @@ -1,485 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * vim: noexpandtab ts=8 sts=0 sw=8: | ||
| 3 | * | ||
| 4 | * configfs_example.c - This file is a demonstration module containing | ||
| 5 | * a number of configfs subsystems. | ||
| 6 | * | ||
| 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. | ||
| 11 | * | ||
| 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. | ||
| 16 | * | ||
| 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. | ||
| 21 | * | ||
| 22 | * Based on sysfs: | ||
| 23 | * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel | ||
| 24 | * | ||
| 25 | * configfs Copyright (C) 2005 Oracle. All rights reserved. | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include <linux/init.h> | ||
| 29 | #include <linux/module.h> | ||
| 30 | #include <linux/slab.h> | ||
| 31 | |||
| 32 | #include <linux/configfs.h> | ||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | /* | ||
| 37 | * 01-childless | ||
| 38 | * | ||
| 39 | * This first example is a childless subsystem. It cannot create | ||
| 40 | * any config_items. It just has attributes. | ||
| 41 | * | ||
| 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 | ||
| 45 | * such a subsystem. | ||
| 46 | */ | ||
| 47 | |||
| 48 | struct childless { | ||
| 49 | struct configfs_subsystem subsys; | ||
| 50 | int showme; | ||
| 51 | int storeme; | ||
| 52 | }; | ||
| 53 | |||
| 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); | ||
| 58 | }; | ||
| 59 | |||
| 60 | static inline struct childless *to_childless(struct config_item *item) | ||
| 61 | { | ||
| 62 | return item ? container_of(to_configfs_subsystem(to_config_group(item)), struct childless, subsys) : NULL; | ||
| 63 | } | ||
| 64 | |||
| 65 | static ssize_t childless_showme_read(struct childless *childless, | ||
| 66 | char *page) | ||
| 67 | { | ||
| 68 | ssize_t pos; | ||
| 69 | |||
| 70 | pos = sprintf(page, "%d\n", childless->showme); | ||
| 71 | childless->showme++; | ||
| 72 | |||
| 73 | return pos; | ||
| 74 | } | ||
| 75 | |||
| 76 | static ssize_t childless_storeme_read(struct childless *childless, | ||
| 77 | char *page) | ||
| 78 | { | ||
| 79 | return sprintf(page, "%d\n", childless->storeme); | ||
| 80 | } | ||
| 81 | |||
| 82 | static ssize_t childless_storeme_write(struct childless *childless, | ||
| 83 | const char *page, | ||
| 84 | size_t count) | ||
| 85 | { | ||
| 86 | unsigned long tmp; | ||
| 87 | char *p = (char *) page; | ||
| 88 | |||
| 89 | tmp = simple_strtoul(p, &p, 10); | ||
| 90 | if (!p || (*p && (*p != '\n'))) | ||
| 91 | return -EINVAL; | ||
| 92 | |||
| 93 | if (tmp > INT_MAX) | ||
| 94 | return -ERANGE; | ||
| 95 | |||
| 96 | childless->storeme = tmp; | ||
| 97 | |||
| 98 | return count; | ||
| 99 | } | ||
| 100 | |||
| 101 | static ssize_t childless_description_read(struct childless *childless, | ||
| 102 | char *page) | ||
| 103 | { | ||
| 104 | return sprintf(page, | ||
| 105 | "[01-childless]\n" | ||
| 106 | "\n" | ||
| 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"); | ||
| 111 | } | ||
| 112 | |||
| 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, | ||
| 116 | }; | ||
| 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, | ||
| 121 | }; | ||
| 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, | ||
| 125 | }; | ||
| 126 | |||
| 127 | static struct configfs_attribute *childless_attrs[] = { | ||
| 128 | &childless_attr_showme.attr, | ||
| 129 | &childless_attr_storeme.attr, | ||
| 130 | &childless_attr_description.attr, | ||
| 131 | NULL, | ||
| 132 | }; | ||
| 133 | |||
| 134 | static ssize_t childless_attr_show(struct config_item *item, | ||
| 135 | struct configfs_attribute *attr, | ||
| 136 | char *page) | ||
| 137 | { | ||
| 138 | struct childless *childless = to_childless(item); | ||
| 139 | struct childless_attribute *childless_attr = | ||
| 140 | container_of(attr, struct childless_attribute, attr); | ||
| 141 | ssize_t ret = 0; | ||
| 142 | |||
| 143 | if (childless_attr->show) | ||
| 144 | ret = childless_attr->show(childless, page); | ||
| 145 | return ret; | ||
| 146 | } | ||
| 147 | |||
| 148 | static ssize_t childless_attr_store(struct config_item *item, | ||
| 149 | struct configfs_attribute *attr, | ||
| 150 | const char *page, size_t count) | ||
| 151 | { | ||
| 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; | ||
| 156 | |||
| 157 | if (childless_attr->store) | ||
| 158 | ret = childless_attr->store(childless, page, count); | ||
| 159 | return ret; | ||
| 160 | } | ||
| 161 | |||
| 162 | static struct configfs_item_operations childless_item_ops = { | ||
| 163 | .show_attribute = childless_attr_show, | ||
| 164 | .store_attribute = childless_attr_store, | ||
| 165 | }; | ||
| 166 | |||
| 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, | ||
| 171 | }; | ||
| 172 | |||
| 173 | static struct childless childless_subsys = { | ||
| 174 | .subsys = { | ||
| 175 | .su_group = { | ||
| 176 | .cg_item = { | ||
| 177 | .ci_namebuf = "01-childless", | ||
| 178 | .ci_type = &childless_type, | ||
| 179 | }, | ||
| 180 | }, | ||
| 181 | }, | ||
| 182 | }; | ||
| 183 | |||
| 184 | |||
| 185 | /* ----------------------------------------------------------------- */ | ||
| 186 | |||
| 187 | /* | ||
| 188 | * 02-simple-children | ||
| 189 | * | ||
| 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. | ||
| 194 | */ | ||
| 195 | |||
| 196 | struct simple_child { | ||
| 197 | struct config_item item; | ||
| 198 | int storeme; | ||
| 199 | }; | ||
| 200 | |||
| 201 | static inline struct simple_child *to_simple_child(struct config_item *item) | ||
| 202 | { | ||
| 203 | return item ? container_of(item, struct simple_child, item) : NULL; | ||
| 204 | } | ||
| 205 | |||
| 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, | ||
| 210 | }; | ||
| 211 | |||
| 212 | static struct configfs_attribute *simple_child_attrs[] = { | ||
| 213 | &simple_child_attr_storeme, | ||
| 214 | NULL, | ||
| 215 | }; | ||
| 216 | |||
| 217 | static ssize_t simple_child_attr_show(struct config_item *item, | ||
| 218 | struct configfs_attribute *attr, | ||
| 219 | char *page) | ||
| 220 | { | ||
| 221 | ssize_t count; | ||
| 222 | struct simple_child *simple_child = to_simple_child(item); | ||
| 223 | |||
| 224 | count = sprintf(page, "%d\n", simple_child->storeme); | ||
| 225 | |||
| 226 | return count; | ||
| 227 | } | ||
| 228 | |||
| 229 | static ssize_t simple_child_attr_store(struct config_item *item, | ||
| 230 | struct configfs_attribute *attr, | ||
| 231 | const char *page, size_t count) | ||
| 232 | { | ||
| 233 | struct simple_child *simple_child = to_simple_child(item); | ||
| 234 | unsigned long tmp; | ||
| 235 | char *p = (char *) page; | ||
| 236 | |||
| 237 | tmp = simple_strtoul(p, &p, 10); | ||
| 238 | if (!p || (*p && (*p != '\n'))) | ||
| 239 | return -EINVAL; | ||
| 240 | |||
| 241 | if (tmp > INT_MAX) | ||
| 242 | return -ERANGE; | ||
| 243 | |||
| 244 | simple_child->storeme = tmp; | ||
| 245 | |||
| 246 | return count; | ||
| 247 | } | ||
| 248 | |||
| 249 | static void simple_child_release(struct config_item *item) | ||
| 250 | { | ||
| 251 | kfree(to_simple_child(item)); | ||
| 252 | } | ||
| 253 | |||
| 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, | ||
| 258 | }; | ||
| 259 | |||
| 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, | ||
| 264 | }; | ||
| 265 | |||
| 266 | |||
| 267 | struct simple_children { | ||
| 268 | struct config_group group; | ||
| 269 | }; | ||
| 270 | |||
| 271 | static inline struct simple_children *to_simple_children(struct config_item *item) | ||
| 272 | { | ||
| 273 | return item ? container_of(to_config_group(item), struct simple_children, group) : NULL; | ||
| 274 | } | ||
| 275 | |||
| 276 | static struct config_item *simple_children_make_item(struct config_group *group, const char *name) | ||
| 277 | { | ||
| 278 | struct simple_child *simple_child; | ||
| 279 | |||
| 280 | simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL); | ||
| 281 | if (!simple_child) | ||
| 282 | return ERR_PTR(-ENOMEM); | ||
| 283 | |||
| 284 | |||
| 285 | config_item_init_type_name(&simple_child->item, name, | ||
| 286 | &simple_child_type); | ||
| 287 | |||
| 288 | simple_child->storeme = 0; | ||
| 289 | |||
| 290 | return &simple_child->item; | ||
| 291 | } | ||
| 292 | |||
| 293 | static struct configfs_attribute simple_children_attr_description = { | ||
| 294 | .ca_owner = THIS_MODULE, | ||
| 295 | .ca_name = "description", | ||
| 296 | .ca_mode = S_IRUGO, | ||
| 297 | }; | ||
| 298 | |||
| 299 | static struct configfs_attribute *simple_children_attrs[] = { | ||
| 300 | &simple_children_attr_description, | ||
| 301 | NULL, | ||
| 302 | }; | ||
| 303 | |||
| 304 | static ssize_t simple_children_attr_show(struct config_item *item, | ||
| 305 | struct configfs_attribute *attr, | ||
| 306 | char *page) | ||
| 307 | { | ||
| 308 | return sprintf(page, | ||
| 309 | "[02-simple-children]\n" | ||
| 310 | "\n" | ||
| 311 | "This subsystem allows the creation of child config_items. These\n" | ||
| 312 | "items have only one attribute that is readable and writeable.\n"); | ||
| 313 | } | ||
| 314 | |||
| 315 | static void simple_children_release(struct config_item *item) | ||
| 316 | { | ||
| 317 | kfree(to_simple_children(item)); | ||
| 318 | } | ||
| 319 | |||
| 320 | static struct configfs_item_operations simple_children_item_ops = { | ||
| 321 | .release = simple_children_release, | ||
| 322 | .show_attribute = simple_children_attr_show, | ||
| 323 | }; | ||
| 324 | |||
| 325 | /* | ||
| 326 | * Note that, since no extra work is required on ->drop_item(), | ||
| 327 | * no ->drop_item() is provided. | ||
| 328 | */ | ||
| 329 | static struct configfs_group_operations simple_children_group_ops = { | ||
| 330 | .make_item = simple_children_make_item, | ||
| 331 | }; | ||
| 332 | |||
| 333 | static struct config_item_type simple_children_type = { | ||
| 334 | .ct_item_ops = &simple_children_item_ops, | ||
| 335 | .ct_group_ops = &simple_children_group_ops, | ||
| 336 | .ct_attrs = simple_children_attrs, | ||
| 337 | .ct_owner = THIS_MODULE, | ||
| 338 | }; | ||
| 339 | |||
| 340 | static struct configfs_subsystem simple_children_subsys = { | ||
| 341 | .su_group = { | ||
| 342 | .cg_item = { | ||
| 343 | .ci_namebuf = "02-simple-children", | ||
| 344 | .ci_type = &simple_children_type, | ||
| 345 | }, | ||
| 346 | }, | ||
| 347 | }; | ||
| 348 | |||
| 349 | |||
| 350 | /* ----------------------------------------------------------------- */ | ||
| 351 | |||
| 352 | /* | ||
| 353 | * 03-group-children | ||
| 354 | * | ||
| 355 | * This example reuses the simple_children group from above. However, | ||
| 356 | * the simple_children group is not the subsystem itself, it is a | ||
| 357 | * child of the subsystem. Creation of a group in the subsystem creates | ||
| 358 | * a new simple_children group. That group can then have simple_child | ||
| 359 | * children of its own. | ||
| 360 | */ | ||
| 361 | |||
| 362 | static struct config_group *group_children_make_group(struct config_group *group, const char *name) | ||
| 363 | { | ||
| 364 | struct simple_children *simple_children; | ||
| 365 | |||
| 366 | simple_children = kzalloc(sizeof(struct simple_children), | ||
| 367 | GFP_KERNEL); | ||
| 368 | if (!simple_children) | ||
| 369 | return ERR_PTR(-ENOMEM); | ||
| 370 | |||
| 371 | |||
| 372 | config_group_init_type_name(&simple_children->group, name, | ||
| 373 | &simple_children_type); | ||
| 374 | |||
| 375 | return &simple_children->group; | ||
| 376 | } | ||
| 377 | |||
| 378 | static struct configfs_attribute group_children_attr_description = { | ||
| 379 | .ca_owner = THIS_MODULE, | ||
| 380 | .ca_name = "description", | ||
| 381 | .ca_mode = S_IRUGO, | ||
| 382 | }; | ||
| 383 | |||
| 384 | static struct configfs_attribute *group_children_attrs[] = { | ||
| 385 | &group_children_attr_description, | ||
| 386 | NULL, | ||
| 387 | }; | ||
| 388 | |||
| 389 | static ssize_t group_children_attr_show(struct config_item *item, | ||
| 390 | struct configfs_attribute *attr, | ||
| 391 | char *page) | ||
| 392 | { | ||
| 393 | return sprintf(page, | ||
| 394 | "[03-group-children]\n" | ||
| 395 | "\n" | ||
| 396 | "This subsystem allows the creation of child config_groups. These\n" | ||
| 397 | "groups are like the subsystem simple-children.\n"); | ||
| 398 | } | ||
| 399 | |||
| 400 | static struct configfs_item_operations group_children_item_ops = { | ||
| 401 | .show_attribute = group_children_attr_show, | ||
| 402 | }; | ||
| 403 | |||
| 404 | /* | ||
| 405 | * Note that, since no extra work is required on ->drop_item(), | ||
| 406 | * no ->drop_item() is provided. | ||
| 407 | */ | ||
| 408 | static struct configfs_group_operations group_children_group_ops = { | ||
| 409 | .make_group = group_children_make_group, | ||
| 410 | }; | ||
| 411 | |||
| 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, | ||
| 417 | }; | ||
| 418 | |||
| 419 | static struct configfs_subsystem group_children_subsys = { | ||
| 420 | .su_group = { | ||
| 421 | .cg_item = { | ||
| 422 | .ci_namebuf = "03-group-children", | ||
| 423 | .ci_type = &group_children_type, | ||
| 424 | }, | ||
| 425 | }, | ||
| 426 | }; | ||
| 427 | |||
| 428 | /* ----------------------------------------------------------------- */ | ||
| 429 | |||
| 430 | /* | ||
| 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 | ||
| 435 | * on it directly. | ||
| 436 | */ | ||
| 437 | static struct configfs_subsystem *example_subsys[] = { | ||
| 438 | &childless_subsys.subsys, | ||
| 439 | &simple_children_subsys, | ||
| 440 | &group_children_subsys, | ||
| 441 | NULL, | ||
| 442 | }; | ||
| 443 | |||
| 444 | static int __init configfs_example_init(void) | ||
| 445 | { | ||
| 446 | int ret; | ||
| 447 | int i; | ||
| 448 | struct configfs_subsystem *subsys; | ||
| 449 | |||
| 450 | for (i = 0; example_subsys[i]; i++) { | ||
| 451 | subsys = example_subsys[i]; | ||
| 452 | |||
| 453 | config_group_init(&subsys->su_group); | ||
| 454 | mutex_init(&subsys->su_mutex); | ||
| 455 | ret = configfs_register_subsystem(subsys); | ||
| 456 | if (ret) { | ||
| 457 | printk(KERN_ERR "Error %d while registering subsystem %s\n", | ||
| 458 | ret, | ||
| 459 | subsys->su_group.cg_item.ci_namebuf); | ||
| 460 | goto out_unregister; | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | return 0; | ||
| 465 | |||
| 466 | out_unregister: | ||
| 467 | for (; i >= 0; i--) { | ||
| 468 | configfs_unregister_subsystem(example_subsys[i]); | ||
| 469 | } | ||
| 470 | |||
| 471 | return ret; | ||
| 472 | } | ||
| 473 | |||
| 474 | static void __exit configfs_example_exit(void) | ||
| 475 | { | ||
| 476 | int i; | ||
| 477 | |||
| 478 | for (i = 0; example_subsys[i]; i++) { | ||
| 479 | configfs_unregister_subsystem(example_subsys[i]); | ||
| 480 | } | ||
| 481 | } | ||
| 482 | |||
| 483 | module_init(configfs_example_init); | ||
| 484 | module_exit(configfs_example_exit); | ||
| 485 | MODULE_LICENSE("GPL"); | ||
