| 1 | |
| 2 | |
| 3 | * configfs_example_macros.c - This file is a demonstration module
|
| 4 | * containing a number of configfs subsystems. It uses the helper
|
| 5 | * macros defined by configfs.h
|
| 6 | *
|
| 7 | * Based on sysfs:
|
| 8 | * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
|
| 9 | *
|
| 10 | * configfs Copyright (C) 2005 Oracle. All rights reserved.
|
| 11 |
|
| 12 |
|
| 13 | #include <linux/init.h>
|
| 14 | #include <linux/kernel.h>
|
| 15 | #include <linux/module.h>
|
| 16 | #include <linux/slab.h>
|
| 17 | #include <linux/configfs.h>
|
| 18 |
|
| 19 | |
| 20 | * 01-childless
|
| 21 | *
|
| 22 | * This first example is a childless subsystem. It cannot create
|
| 23 | * any config_items. It just has attributes.
|
| 24 | *
|
| 25 | * Note that we are enclosing the configfs_subsystem inside a container.
|
| 26 | * This is not necessary if a subsystem has no attributes directly
|
| 27 | * on the subsystem. See the next example, 02-simple-children, for
|
| 28 | * such a subsystem.
|
| 29 |
|
| 30 |
|
| 31 | struct childless {
|
| 32 | struct configfs_subsystem subsys;
|
| 33 | int showme;
|
| 34 | int storeme;
|
| 35 | };
|
| 36 |
|
| 37 | static inline struct childless *to_childless(struct config_item *item)
|
| 38 | {
|
| 39 | return container_of(to_configfs_subsystem(to_config_group(item)),
|
| 40 | struct childless, subsys);
|
| 41 | }
|
| 42 |
|
| 43 | static ssize_t childless_showme_show(struct config_item *item, char *page)
|
| 44 | {
|
| 45 | struct childless *childless = to_childless(item);
|
| 46 | ssize_t pos;
|
| 47 |
|
| 48 | pos = sprintf(page, "%d\n", childless->showme);
|
| 49 | childless->showme++;
|
| 50 |
|
| 51 | return pos;
|
| 52 | }
|
| 53 |
|
| 54 | static ssize_t childless_storeme_show(struct config_item *item, char *page)
|
| 55 | {
|
| 56 | return sprintf(page, "%d\n", to_childless(item)->storeme);
|
| 57 | }
|
| 58 |
|
| 59 | static ssize_t childless_storeme_store(struct config_item *item,
|
| 60 | const char *page, size_t count)
|
| 61 | {
|
| 62 | struct childless *childless = to_childless(item);
|
| 63 | int ret;
|
| 64 |
|
| 65 | ret = kstrtoint(page, 10, &childless->storeme);
|
| 66 | if (ret)
|
| 67 | return ret;
|
| 68 |
|
| 69 | return count;
|
| 70 | }
|
| 71 |
|
| 72 | static ssize_t childless_description_show(struct config_item *item, char *page)
|
| 73 | {
|
| 74 | return sprintf(page,
|
| 75 | "[01-childless]\n"
|
| 76 | "\n"
|
| 77 | "The childless subsystem is the simplest possible subsystem in\n"
|
| 78 | "configfs. It does not support the creation of child config_items.\n"
|
| 79 | "It only has a few attributes. In fact, it isn't much different\n"
|
| 80 | "than a directory in /proc.\n");
|
| 81 | }
|
| 82 |
|
| 83 | CONFIGFS_ATTR_RO(childless_, showme);
|
| 84 | CONFIGFS_ATTR(childless_, storeme);
|
| 85 | CONFIGFS_ATTR_RO(childless_, description);
|
| 86 |
|
| 87 | static const struct configfs_attribute *const childless_attrs[] = {
|
| 88 | &childless_attr_showme,
|
| 89 | &childless_attr_storeme,
|
| 90 | &childless_attr_description,
|
| 91 | NULL,
|
| 92 | };
|
| 93 |
|
| 94 | static const struct config_item_type childless_type = {
|
| 95 | .ct_attrs_const = childless_attrs,
|
| 96 | .ct_owner = THIS_MODULE,
|
| 97 | };
|
| 98 |
|
| 99 | static struct childless childless_subsys = {
|
| 100 | .subsys = {
|
| 101 | .su_group = {
|
| 102 | .cg_item = {
|
| 103 | .ci_namebuf = "01-childless",
|
| 104 | .ci_type = &childless_type,
|
| 105 | },
|
| 106 | },
|
| 107 | },
|
| 108 | };
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 | |
| 113 | * 02-simple-children
|
| 114 | *
|
| 115 | * This example merely has a simple one-attribute child. Note that
|
| 116 | * there is no extra attribute structure, as the child's attribute is
|
| 117 | * known from the get-go. Also, there is no container for the
|
| 118 | * subsystem, as it has no attributes of its own.
|
| 119 |
|
| 120 |
|
| 121 | struct simple_child {
|
| 122 | struct config_item item;
|
| 123 | int storeme;
|
| 124 | };
|
| 125 |
|
| 126 | static inline struct simple_child *to_simple_child(struct config_item *item)
|
| 127 | {
|
| 128 | return container_of(item, struct simple_child, item);
|
| 129 | }
|
| 130 |
|
| 131 | static ssize_t simple_child_storeme_show(struct config_item *item, char *page)
|
| 132 | {
|
| 133 | return sprintf(page, "%d\n", to_simple_child(item)->storeme);
|
| 134 | }
|
| 135 |
|
| 136 | static ssize_t simple_child_storeme_store(struct config_item *item,
|
| 137 | const char *page, size_t count)
|
| 138 | {
|
| 139 | struct simple_child *simple_child = to_simple_child(item);
|
| 140 | int ret;
|
| 141 |
|
| 142 | ret = kstrtoint(page, 10, &simple_child->storeme);
|
| 143 | if (ret)
|
| 144 | return ret;
|
| 145 |
|
| 146 | return count;
|
| 147 | }
|
| 148 |
|
| 149 | CONFIGFS_ATTR(simple_child_, storeme);
|
| 150 |
|
| 151 | static const struct configfs_attribute *const simple_child_attrs[] = {
|
| 152 | &simple_child_attr_storeme,
|
| 153 | NULL,
|
| 154 | };
|
| 155 |
|
| 156 | static void simple_child_release(struct config_item *item)
|
| 157 | {
|
| 158 | kfree(to_simple_child(item));
|
| 159 | }
|
| 160 |
|
| 161 | static const struct configfs_item_operations simple_child_item_ops = {
|
| 162 | .release = simple_child_release,
|
| 163 | };
|
| 164 |
|
| 165 | static const struct config_item_type simple_child_type = {
|
| 166 | .ct_item_ops = &simple_child_item_ops,
|
| 167 | .ct_attrs_const = simple_child_attrs,
|
| 168 | .ct_owner = THIS_MODULE,
|
| 169 | };
|
| 170 |
|
| 171 | struct simple_children {
|
| 172 | struct config_group group;
|
| 173 | };
|
| 174 |
|
| 175 | static inline struct simple_children *to_simple_children(struct config_item *item)
|
| 176 | {
|
| 177 | return container_of(to_config_group(item),
|
| 178 | struct simple_children, group);
|
| 179 | }
|
| 180 |
|
| 181 | static struct config_item *simple_children_make_item(struct config_group *group,
|
| 182 | const char *name)
|
| 183 | {
|
| 184 | struct simple_child *simple_child;
|
| 185 |
|
| 186 | simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL);
|
| 187 | if (!simple_child)
|
| 188 | return ERR_PTR(-ENOMEM);
|
| 189 |
|
| 190 | config_item_init_type_name(&simple_child->item, name,
|
| 191 | &simple_child_type);
|
| 192 |
|
| 193 | return &simple_child->item;
|
| 194 | }
|
| 195 |
|
| 196 | static ssize_t simple_children_description_show(struct config_item *item,
|
| 197 | char *page)
|
| 198 | {
|
| 199 | return sprintf(page,
|
| 200 | "[02-simple-children]\n"
|
| 201 | "\n"
|
| 202 | "This subsystem allows the creation of child config_items. These\n"
|
| 203 | "items have only one attribute that is readable and writeable.\n");
|
| 204 | }
|
| 205 |
|
| 206 | CONFIGFS_ATTR_RO(simple_children_, description);
|
| 207 |
|
| 208 | static const struct configfs_attribute *const simple_children_attrs[] = {
|
| 209 | &simple_children_attr_description,
|
| 210 | NULL,
|
| 211 | };
|
| 212 |
|
| 213 | static void simple_children_release(struct config_item *item)
|
| 214 | {
|
| 215 | kfree(to_simple_children(item));
|
| 216 | }
|
| 217 |
|
| 218 | static const struct configfs_item_operations simple_children_item_ops = {
|
| 219 | .release = simple_children_release,
|
| 220 | };
|
| 221 |
|
| 222 | |
| 223 | * Note that, since no extra work is required on ->drop_item(),
|
| 224 | * no ->drop_item() is provided.
|
| 225 |
|
| 226 | static const struct configfs_group_operations simple_children_group_ops = {
|
| 227 | .make_item = simple_children_make_item,
|
| 228 | };
|
| 229 |
|
| 230 | static const struct config_item_type simple_children_type = {
|
| 231 | .ct_item_ops = &simple_children_item_ops,
|
| 232 | .ct_group_ops = &simple_children_group_ops,
|
| 233 | .ct_attrs_const = simple_children_attrs,
|
| 234 | .ct_owner = THIS_MODULE,
|
| 235 | };
|
| 236 |
|
| 237 | static struct configfs_subsystem simple_children_subsys = {
|
| 238 | .su_group = {
|
| 239 | .cg_item = {
|
| 240 | .ci_namebuf = "02-simple-children",
|
| 241 | .ci_type = &simple_children_type,
|
| 242 | },
|
| 243 | },
|
| 244 | };
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 | |
| 249 | * 03-group-children
|
| 250 | *
|
| 251 | * This example reuses the simple_children group from above. However,
|
| 252 | * the simple_children group is not the subsystem itself, it is a
|
| 253 | * child of the subsystem. Creation of a group in the subsystem creates
|
| 254 | * a new simple_children group. That group can then have simple_child
|
| 255 | * children of its own.
|
| 256 |
|
| 257 |
|
| 258 | static struct config_group *group_children_make_group(
|
| 259 | struct config_group *group, const char *name)
|
| 260 | {
|
| 261 | struct simple_children *simple_children;
|
| 262 |
|
| 263 | simple_children = kzalloc(sizeof(struct simple_children),
|
| 264 | GFP_KERNEL);
|
| 265 | if (!simple_children)
|
| 266 | return ERR_PTR(-ENOMEM);
|
| 267 |
|
| 268 | config_group_init_type_name(&simple_children->group, name,
|
| 269 | &simple_children_type);
|
| 270 |
|
| 271 | return &simple_children->group;
|
| 272 | }
|
| 273 |
|
| 274 | static ssize_t group_children_description_show(struct config_item *item,
|
| 275 | char *page)
|
| 276 | {
|
| 277 | return sprintf(page,
|
| 278 | "[03-group-children]\n"
|
| 279 | "\n"
|
| 280 | "This subsystem allows the creation of child config_groups. These\n"
|
| 281 | "groups are like the subsystem simple-children.\n");
|
| 282 | }
|
| 283 |
|
| 284 | CONFIGFS_ATTR_RO(group_children_, description);
|
| 285 |
|
| 286 | static const struct configfs_attribute *const group_children_attrs[] = {
|
| 287 | &group_children_attr_description,
|
| 288 | NULL,
|
| 289 | };
|
| 290 |
|
| 291 | |
| 292 | * Note that, since no extra work is required on ->drop_item(),
|
| 293 | * no ->drop_item() is provided.
|
| 294 |
|
| 295 | static const struct configfs_group_operations group_children_group_ops = {
|
| 296 | .make_group = group_children_make_group,
|
| 297 | };
|
| 298 |
|
| 299 | static const struct config_item_type group_children_type = {
|
| 300 | .ct_group_ops = &group_children_group_ops,
|
| 301 | .ct_attrs_const = group_children_attrs,
|
| 302 | .ct_owner = THIS_MODULE,
|
| 303 | };
|
| 304 |
|
| 305 | static struct configfs_subsystem group_children_subsys = {
|
| 306 | .su_group = {
|
| 307 | .cg_item = {
|
| 308 | .ci_namebuf = "03-group-children",
|
| 309 | .ci_type = &group_children_type,
|
| 310 | },
|
| 311 | },
|
| 312 | };
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 | |
| 317 | * 04-symlink-children
|
| 318 | *
|
| 319 | * This example has children that are valid sources for symlink(2). A
|
| 320 | * child accepts a link to any other config_item and reports how many
|
| 321 | * links it currently holds, so ->allow_link() and ->drop_link() are
|
| 322 | * observable from userspace.
|
| 323 |
|
| 324 |
|
| 325 | struct symlink_child {
|
| 326 | struct config_item item;
|
| 327 | int nlinks;
|
| 328 | };
|
| 329 |
|
| 330 | static inline struct symlink_child *to_symlink_child(struct config_item *item)
|
| 331 | {
|
| 332 | return container_of(item, struct symlink_child, item);
|
| 333 | }
|
| 334 |
|
| 335 | static ssize_t symlink_child_nlinks_show(struct config_item *item, char *page)
|
| 336 | {
|
| 337 | return sprintf(page, "%d\n", to_symlink_child(item)->nlinks);
|
| 338 | }
|
| 339 |
|
| 340 | CONFIGFS_ATTR_RO(symlink_child_, nlinks);
|
| 341 |
|
| 342 | static struct configfs_attribute *symlink_child_attrs[] = {
|
| 343 | &symlink_child_attr_nlinks,
|
| 344 | NULL,
|
| 345 | };
|
| 346 |
|
| 347 | |
| 348 | * The VFS holds the source item's directory locked across symlink(2) and
|
| 349 | * unlink(2), so ->nlinks needs no lock of its own.
|
| 350 |
|
| 351 | static int symlink_child_allow_link(struct config_item *src,
|
| 352 | struct config_item *target)
|
| 353 | {
|
| 354 | to_symlink_child(src)->nlinks++;
|
| 355 |
|
| 356 | return 0;
|
| 357 | }
|
| 358 |
|
| 359 | static void symlink_child_drop_link(struct config_item *src,
|
| 360 | struct config_item *target)
|
| 361 | {
|
| 362 | to_symlink_child(src)->nlinks--;
|
| 363 | }
|
| 364 |
|
| 365 | static void symlink_child_release(struct config_item *item)
|
| 366 | {
|
| 367 | kfree(to_symlink_child(item));
|
| 368 | }
|
| 369 |
|
| 370 | static const struct configfs_item_operations symlink_child_item_ops = {
|
| 371 | .release = symlink_child_release,
|
| 372 | .allow_link = symlink_child_allow_link,
|
| 373 | .drop_link = symlink_child_drop_link,
|
| 374 | };
|
| 375 |
|
| 376 | static const struct config_item_type symlink_child_type = {
|
| 377 | .ct_item_ops = &symlink_child_item_ops,
|
| 378 | .ct_attrs = symlink_child_attrs,
|
| 379 | .ct_owner = THIS_MODULE,
|
| 380 | };
|
| 381 |
|
| 382 | static struct config_item *symlink_children_make_item(
|
| 383 | struct config_group *group, const char *name)
|
| 384 | {
|
| 385 | struct symlink_child *symlink_child;
|
| 386 |
|
| 387 | symlink_child = kzalloc_obj(*symlink_child, GFP_KERNEL);
|
| 388 | if (!symlink_child)
|
| 389 | return ERR_PTR(-ENOMEM);
|
| 390 |
|
| 391 | config_item_init_type_name(&symlink_child->item, name,
|
| 392 | &symlink_child_type);
|
| 393 |
|
| 394 | return &symlink_child->item;
|
| 395 | }
|
| 396 |
|
| 397 | static ssize_t symlink_children_description_show(struct config_item *item,
|
| 398 | char *page)
|
| 399 | {
|
| 400 | return sprintf(page,
|
| 401 | "[04-symlink-children]\n"
|
| 402 | "\n"
|
| 403 | "This subsystem allows the creation of child config_items that\n"
|
| 404 | "symlink(2) can point at other config_items from. Each child\n"
|
| 405 | "reports the number of links it holds.\n");
|
| 406 | }
|
| 407 |
|
| 408 | CONFIGFS_ATTR_RO(symlink_children_, description);
|
| 409 |
|
| 410 | static struct configfs_attribute *symlink_children_attrs[] = {
|
| 411 | &symlink_children_attr_description,
|
| 412 | NULL,
|
| 413 | };
|
| 414 |
|
| 415 | static const struct configfs_group_operations symlink_children_group_ops = {
|
| 416 | .make_item = symlink_children_make_item,
|
| 417 | };
|
| 418 |
|
| 419 | static const struct config_item_type symlink_children_type = {
|
| 420 | .ct_group_ops = &symlink_children_group_ops,
|
| 421 | .ct_attrs = symlink_children_attrs,
|
| 422 | .ct_owner = THIS_MODULE,
|
| 423 | };
|
| 424 |
|
| 425 | static struct configfs_subsystem symlink_children_subsys = {
|
| 426 | .su_group = {
|
| 427 | .cg_item = {
|
| 428 | .ci_namebuf = "04-symlink-children",
|
| 429 | .ci_type = &symlink_children_type,
|
| 430 | },
|
| 431 | },
|
| 432 | };
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 | |
| 437 | * We're now done with our subsystem definitions.
|
| 438 | * For convenience in this module, here's a list of them all. It
|
| 439 | * allows the init function to easily register them. Most modules
|
| 440 | * will only have one subsystem, and will only call register_subsystem
|
| 441 | * on it directly.
|
| 442 |
|
| 443 | static struct configfs_subsystem *example_subsys[] = {
|
| 444 | &childless_subsys.subsys,
|
| 445 | &simple_children_subsys,
|
| 446 | &group_children_subsys,
|
| 447 | &symlink_children_subsys,
|
| 448 | NULL,
|
| 449 | };
|
| 450 |
|
| 451 | static int __init configfs_example_init(void)
|
| 452 | {
|
| 453 | struct configfs_subsystem *subsys;
|
| 454 | int ret, i;
|
| 455 |
|
| 456 | for (i = 0; example_subsys[i]; i++) {
|
| 457 | subsys = example_subsys[i];
|
| 458 |
|
| 459 | config_group_init(&subsys->su_group);
|
| 460 | mutex_init(&subsys->su_mutex);
|
| 461 | ret = configfs_register_subsystem(subsys);
|
| 462 | if (ret) {
|
| 463 | pr_err("Error %d while registering subsystem %s\n",
|
| 464 | ret, subsys->su_group.cg_item.ci_namebuf);
|
| 465 | goto out_unregister;
|
| 466 | }
|
| 467 | }
|
| 468 |
|
| 469 | return 0;
|
| 470 |
|
| 471 | out_unregister:
|
| 472 | for (i--; i >= 0; i--)
|
| 473 | configfs_unregister_subsystem(example_subsys[i]);
|
| 474 |
|
| 475 | return ret;
|
| 476 | }
|
| 477 |
|
| 478 | static void __exit configfs_example_exit(void)
|
| 479 | {
|
| 480 | int i;
|
| 481 |
|
| 482 | for (i = 0; example_subsys[i]; i++)
|
| 483 | configfs_unregister_subsystem(example_subsys[i]);
|
| 484 | }
|
| 485 |
|
| 486 | module_init(configfs_example_init);
|
| 487 | module_exit(configfs_example_exit);
|
| 488 | MODULE_DESCRIPTION("Sample configfs module");
|
| 489 | MODULE_LICENSE("GPL");
|