configfs_sample.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  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. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/configfs.h>
  17. /*
  18. * 01-childless
  19. *
  20. * This first example is a childless subsystem. It cannot create
  21. * any config_items. It just has attributes.
  22. *
  23. * Note that we are enclosing the configfs_subsystem inside a container.
  24. * This is not necessary if a subsystem has no attributes directly
  25. * on the subsystem. See the next example, 02-simple-children, for
  26. * such a subsystem.
  27. */
  28. struct childless {
  29. struct configfs_subsystem subsys;
  30. int showme;
  31. int storeme;
  32. };
  33. static inline struct childless *to_childless(struct config_item *item)
  34. {
  35. return container_of(to_configfs_subsystem(to_config_group(item)),
  36. struct childless, subsys);
  37. }
  38. static ssize_t childless_showme_show(struct config_item *item, char *page)
  39. {
  40. struct childless *childless = to_childless(item);
  41. ssize_t pos;
  42. pos = sprintf(page, "%d\n", childless->showme);
  43. childless->showme++;
  44. return pos;
  45. }
  46. static ssize_t childless_storeme_show(struct config_item *item, char *page)
  47. {
  48. return sprintf(page, "%d\n", to_childless(item)->storeme);
  49. }
  50. static ssize_t childless_storeme_store(struct config_item *item,
  51. const char *page, size_t count)
  52. {
  53. struct childless *childless = to_childless(item);
  54. int ret;
  55. ret = kstrtoint(page, 10, &childless->storeme);
  56. if (ret)
  57. return ret;
  58. return count;
  59. }
  60. static ssize_t childless_description_show(struct config_item *item, char *page)
  61. {
  62. return sprintf(page,
  63. "[01-childless]\n"
  64. "\n"
  65. "The childless subsystem is the simplest possible subsystem in\n"
  66. "configfs. It does not support the creation of child config_items.\n"
  67. "It only has a few attributes. In fact, it isn't much different\n"
  68. "than a directory in /proc.\n");
  69. }
  70. CONFIGFS_ATTR_RO(childless_, showme);
  71. CONFIGFS_ATTR(childless_, storeme);
  72. CONFIGFS_ATTR_RO(childless_, description);
  73. static struct configfs_attribute *childless_attrs[] = {
  74. &childless_attr_showme,
  75. &childless_attr_storeme,
  76. &childless_attr_description,
  77. NULL,
  78. };
  79. static const struct config_item_type childless_type = {
  80. .ct_attrs = childless_attrs,
  81. .ct_owner = THIS_MODULE,
  82. };
  83. static struct childless childless_subsys = {
  84. .subsys = {
  85. .su_group = {
  86. .cg_item = {
  87. .ci_namebuf = "01-childless",
  88. .ci_type = &childless_type,
  89. },
  90. },
  91. },
  92. };
  93. /* ----------------------------------------------------------------- */
  94. /*
  95. * 02-simple-children
  96. *
  97. * This example merely has a simple one-attribute child. Note that
  98. * there is no extra attribute structure, as the child's attribute is
  99. * known from the get-go. Also, there is no container for the
  100. * subsystem, as it has no attributes of its own.
  101. */
  102. struct simple_child {
  103. struct config_item item;
  104. int storeme;
  105. };
  106. static inline struct simple_child *to_simple_child(struct config_item *item)
  107. {
  108. return container_of(item, struct simple_child, item);
  109. }
  110. static ssize_t simple_child_storeme_show(struct config_item *item, char *page)
  111. {
  112. return sprintf(page, "%d\n", to_simple_child(item)->storeme);
  113. }
  114. static ssize_t simple_child_storeme_store(struct config_item *item,
  115. const char *page, size_t count)
  116. {
  117. struct simple_child *simple_child = to_simple_child(item);
  118. int ret;
  119. ret = kstrtoint(page, 10, &simple_child->storeme);
  120. if (ret)
  121. return ret;
  122. return count;
  123. }
  124. CONFIGFS_ATTR(simple_child_, storeme);
  125. static struct configfs_attribute *simple_child_attrs[] = {
  126. &simple_child_attr_storeme,
  127. NULL,
  128. };
  129. static void simple_child_release(struct config_item *item)
  130. {
  131. kfree(to_simple_child(item));
  132. }
  133. static struct configfs_item_operations simple_child_item_ops = {
  134. .release = simple_child_release,
  135. };
  136. static const struct config_item_type simple_child_type = {
  137. .ct_item_ops = &simple_child_item_ops,
  138. .ct_attrs = simple_child_attrs,
  139. .ct_owner = THIS_MODULE,
  140. };
  141. struct simple_children {
  142. struct config_group group;
  143. };
  144. static inline struct simple_children *to_simple_children(struct config_item *item)
  145. {
  146. return container_of(to_config_group(item),
  147. struct simple_children, group);
  148. }
  149. static struct config_item *simple_children_make_item(struct config_group *group,
  150. const char *name)
  151. {
  152. struct simple_child *simple_child;
  153. simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL);
  154. if (!simple_child)
  155. return ERR_PTR(-ENOMEM);
  156. config_item_init_type_name(&simple_child->item, name,
  157. &simple_child_type);
  158. return &simple_child->item;
  159. }
  160. static ssize_t simple_children_description_show(struct config_item *item,
  161. char *page)
  162. {
  163. return sprintf(page,
  164. "[02-simple-children]\n"
  165. "\n"
  166. "This subsystem allows the creation of child config_items. These\n"
  167. "items have only one attribute that is readable and writeable.\n");
  168. }
  169. CONFIGFS_ATTR_RO(simple_children_, description);
  170. static struct configfs_attribute *simple_children_attrs[] = {
  171. &simple_children_attr_description,
  172. NULL,
  173. };
  174. static void simple_children_release(struct config_item *item)
  175. {
  176. kfree(to_simple_children(item));
  177. }
  178. static struct configfs_item_operations simple_children_item_ops = {
  179. .release = simple_children_release,
  180. };
  181. /*
  182. * Note that, since no extra work is required on ->drop_item(),
  183. * no ->drop_item() is provided.
  184. */
  185. static struct configfs_group_operations simple_children_group_ops = {
  186. .make_item = simple_children_make_item,
  187. };
  188. static const struct config_item_type simple_children_type = {
  189. .ct_item_ops = &simple_children_item_ops,
  190. .ct_group_ops = &simple_children_group_ops,
  191. .ct_attrs = simple_children_attrs,
  192. .ct_owner = THIS_MODULE,
  193. };
  194. static struct configfs_subsystem simple_children_subsys = {
  195. .su_group = {
  196. .cg_item = {
  197. .ci_namebuf = "02-simple-children",
  198. .ci_type = &simple_children_type,
  199. },
  200. },
  201. };
  202. /* ----------------------------------------------------------------- */
  203. /*
  204. * 03-group-children
  205. *
  206. * This example reuses the simple_children group from above. However,
  207. * the simple_children group is not the subsystem itself, it is a
  208. * child of the subsystem. Creation of a group in the subsystem creates
  209. * a new simple_children group. That group can then have simple_child
  210. * children of its own.
  211. */
  212. static struct config_group *group_children_make_group(
  213. struct config_group *group, const char *name)
  214. {
  215. struct simple_children *simple_children;
  216. simple_children = kzalloc(sizeof(struct simple_children),
  217. GFP_KERNEL);
  218. if (!simple_children)
  219. return ERR_PTR(-ENOMEM);
  220. config_group_init_type_name(&simple_children->group, name,
  221. &simple_children_type);
  222. return &simple_children->group;
  223. }
  224. static ssize_t group_children_description_show(struct config_item *item,
  225. char *page)
  226. {
  227. return sprintf(page,
  228. "[03-group-children]\n"
  229. "\n"
  230. "This subsystem allows the creation of child config_groups. These\n"
  231. "groups are like the subsystem simple-children.\n");
  232. }
  233. CONFIGFS_ATTR_RO(group_children_, description);
  234. static struct configfs_attribute *group_children_attrs[] = {
  235. &group_children_attr_description,
  236. NULL,
  237. };
  238. /*
  239. * Note that, since no extra work is required on ->drop_item(),
  240. * no ->drop_item() is provided.
  241. */
  242. static struct configfs_group_operations group_children_group_ops = {
  243. .make_group = group_children_make_group,
  244. };
  245. static const struct config_item_type group_children_type = {
  246. .ct_group_ops = &group_children_group_ops,
  247. .ct_attrs = group_children_attrs,
  248. .ct_owner = THIS_MODULE,
  249. };
  250. static struct configfs_subsystem group_children_subsys = {
  251. .su_group = {
  252. .cg_item = {
  253. .ci_namebuf = "03-group-children",
  254. .ci_type = &group_children_type,
  255. },
  256. },
  257. };
  258. /* ----------------------------------------------------------------- */
  259. /*
  260. * We're now done with our subsystem definitions.
  261. * For convenience in this module, here's a list of them all. It
  262. * allows the init function to easily register them. Most modules
  263. * will only have one subsystem, and will only call register_subsystem
  264. * on it directly.
  265. */
  266. static struct configfs_subsystem *example_subsys[] = {
  267. &childless_subsys.subsys,
  268. &simple_children_subsys,
  269. &group_children_subsys,
  270. NULL,
  271. };
  272. static int __init configfs_example_init(void)
  273. {
  274. struct configfs_subsystem *subsys;
  275. int ret, i;
  276. for (i = 0; example_subsys[i]; i++) {
  277. subsys = example_subsys[i];
  278. config_group_init(&subsys->su_group);
  279. mutex_init(&subsys->su_mutex);
  280. ret = configfs_register_subsystem(subsys);
  281. if (ret) {
  282. pr_err("Error %d while registering subsystem %s\n",
  283. ret, subsys->su_group.cg_item.ci_namebuf);
  284. goto out_unregister;
  285. }
  286. }
  287. return 0;
  288. out_unregister:
  289. for (i--; i >= 0; i--)
  290. configfs_unregister_subsystem(example_subsys[i]);
  291. return ret;
  292. }
  293. static void __exit configfs_example_exit(void)
  294. {
  295. int i;
  296. for (i = 0; example_subsys[i]; i++)
  297. configfs_unregister_subsystem(example_subsys[i]);
  298. }
  299. module_init(configfs_example_init);
  300. module_exit(configfs_example_exit);
  301. MODULE_LICENSE("GPL");