configfs_sample.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * vim: noexpandtab ts=8 sts=0 sw=8:
  3. *
  4. * configfs_example_macros.c - This file is a demonstration module
  5. * containing a number of configfs subsystems. It uses the helper
  6. * macros defined by configfs.h
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 021110-1307, USA.
  22. *
  23. * Based on sysfs:
  24. * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
  25. *
  26. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  27. */
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/configfs.h>
  32. /*
  33. * 01-childless
  34. *
  35. * This first example is a childless subsystem. It cannot create
  36. * any config_items. It just has attributes.
  37. *
  38. * Note that we are enclosing the configfs_subsystem inside a container.
  39. * This is not necessary if a subsystem has no attributes directly
  40. * on the subsystem. See the next example, 02-simple-children, for
  41. * such a subsystem.
  42. */
  43. struct childless {
  44. struct configfs_subsystem subsys;
  45. int showme;
  46. int storeme;
  47. };
  48. static inline struct childless *to_childless(struct config_item *item)
  49. {
  50. return item ? container_of(to_configfs_subsystem(to_config_group(item)),
  51. struct childless, subsys) : NULL;
  52. }
  53. static ssize_t childless_showme_show(struct config_item *item, char *page)
  54. {
  55. struct childless *childless = to_childless(item);
  56. ssize_t pos;
  57. pos = sprintf(page, "%d\n", childless->showme);
  58. childless->showme++;
  59. return pos;
  60. }
  61. static ssize_t childless_storeme_show(struct config_item *item, char *page)
  62. {
  63. return sprintf(page, "%d\n", to_childless(item)->storeme);
  64. }
  65. static ssize_t childless_storeme_store(struct config_item *item,
  66. const char *page, size_t count)
  67. {
  68. struct childless *childless = to_childless(item);
  69. unsigned long tmp;
  70. char *p = (char *) page;
  71. tmp = simple_strtoul(p, &p, 10);
  72. if (!p || (*p && (*p != '\n')))
  73. return -EINVAL;
  74. if (tmp > INT_MAX)
  75. return -ERANGE;
  76. childless->storeme = tmp;
  77. return count;
  78. }
  79. static ssize_t childless_description_show(struct config_item *item, char *page)
  80. {
  81. return sprintf(page,
  82. "[01-childless]\n"
  83. "\n"
  84. "The childless subsystem is the simplest possible subsystem in\n"
  85. "configfs. It does not support the creation of child config_items.\n"
  86. "It only has a few attributes. In fact, it isn't much different\n"
  87. "than a directory in /proc.\n");
  88. }
  89. CONFIGFS_ATTR_RO(childless_, showme);
  90. CONFIGFS_ATTR(childless_, storeme);
  91. CONFIGFS_ATTR_RO(childless_, description);
  92. static struct configfs_attribute *childless_attrs[] = {
  93. &childless_attr_showme,
  94. &childless_attr_storeme,
  95. &childless_attr_description,
  96. NULL,
  97. };
  98. static struct config_item_type childless_type = {
  99. .ct_attrs = childless_attrs,
  100. .ct_owner = THIS_MODULE,
  101. };
  102. static struct childless childless_subsys = {
  103. .subsys = {
  104. .su_group = {
  105. .cg_item = {
  106. .ci_namebuf = "01-childless",
  107. .ci_type = &childless_type,
  108. },
  109. },
  110. },
  111. };
  112. /* ----------------------------------------------------------------- */
  113. /*
  114. * 02-simple-children
  115. *
  116. * This example merely has a simple one-attribute child. Note that
  117. * there is no extra attribute structure, as the child's attribute is
  118. * known from the get-go. Also, there is no container for the
  119. * subsystem, as it has no attributes of its own.
  120. */
  121. struct simple_child {
  122. struct config_item item;
  123. int storeme;
  124. };
  125. static inline struct simple_child *to_simple_child(struct config_item *item)
  126. {
  127. return item ? container_of(item, struct simple_child, item) : NULL;
  128. }
  129. static ssize_t simple_child_storeme_show(struct config_item *item, char *page)
  130. {
  131. return sprintf(page, "%d\n", to_simple_child(item)->storeme);
  132. }
  133. static ssize_t simple_child_storeme_store(struct config_item *item,
  134. const char *page, size_t count)
  135. {
  136. struct simple_child *simple_child = to_simple_child(item);
  137. unsigned long tmp;
  138. char *p = (char *) page;
  139. tmp = simple_strtoul(p, &p, 10);
  140. if (!p || (*p && (*p != '\n')))
  141. return -EINVAL;
  142. if (tmp > INT_MAX)
  143. return -ERANGE;
  144. simple_child->storeme = tmp;
  145. return count;
  146. }
  147. CONFIGFS_ATTR(simple_child_, storeme);
  148. static struct configfs_attribute *simple_child_attrs[] = {
  149. &simple_child_attr_storeme,
  150. NULL,
  151. };
  152. static void simple_child_release(struct config_item *item)
  153. {
  154. kfree(to_simple_child(item));
  155. }
  156. static struct configfs_item_operations simple_child_item_ops = {
  157. .release = simple_child_release,
  158. };
  159. static struct config_item_type simple_child_type = {
  160. .ct_item_ops = &simple_child_item_ops,
  161. .ct_attrs = simple_child_attrs,
  162. .ct_owner = THIS_MODULE,
  163. };
  164. struct simple_children {
  165. struct config_group group;
  166. };
  167. static inline struct simple_children *to_simple_children(struct config_item *item)
  168. {
  169. return item ? container_of(to_config_group(item),
  170. struct simple_children, group) : NULL;
  171. }
  172. static struct config_item *simple_children_make_item(struct config_group *group,
  173. const char *name)
  174. {
  175. struct simple_child *simple_child;
  176. simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL);
  177. if (!simple_child)
  178. return ERR_PTR(-ENOMEM);
  179. config_item_init_type_name(&simple_child->item, name,
  180. &simple_child_type);
  181. simple_child->storeme = 0;
  182. return &simple_child->item;
  183. }
  184. static ssize_t simple_children_description_show(struct config_item *item,
  185. char *page)
  186. {
  187. return sprintf(page,
  188. "[02-simple-children]\n"
  189. "\n"
  190. "This subsystem allows the creation of child config_items. These\n"
  191. "items have only one attribute that is readable and writeable.\n");
  192. }
  193. CONFIGFS_ATTR_RO(simple_children_, description);
  194. static struct configfs_attribute *simple_children_attrs[] = {
  195. &simple_children_attr_description,
  196. NULL,
  197. };
  198. static void simple_children_release(struct config_item *item)
  199. {
  200. kfree(to_simple_children(item));
  201. }
  202. static struct configfs_item_operations simple_children_item_ops = {
  203. .release = simple_children_release,
  204. };
  205. /*
  206. * Note that, since no extra work is required on ->drop_item(),
  207. * no ->drop_item() is provided.
  208. */
  209. static struct configfs_group_operations simple_children_group_ops = {
  210. .make_item = simple_children_make_item,
  211. };
  212. static struct config_item_type simple_children_type = {
  213. .ct_item_ops = &simple_children_item_ops,
  214. .ct_group_ops = &simple_children_group_ops,
  215. .ct_attrs = simple_children_attrs,
  216. .ct_owner = THIS_MODULE,
  217. };
  218. static struct configfs_subsystem simple_children_subsys = {
  219. .su_group = {
  220. .cg_item = {
  221. .ci_namebuf = "02-simple-children",
  222. .ci_type = &simple_children_type,
  223. },
  224. },
  225. };
  226. /* ----------------------------------------------------------------- */
  227. /*
  228. * 03-group-children
  229. *
  230. * This example reuses the simple_children group from above. However,
  231. * the simple_children group is not the subsystem itself, it is a
  232. * child of the subsystem. Creation of a group in the subsystem creates
  233. * a new simple_children group. That group can then have simple_child
  234. * children of its own.
  235. */
  236. static struct config_group *group_children_make_group(
  237. struct config_group *group, const char *name)
  238. {
  239. struct simple_children *simple_children;
  240. simple_children = kzalloc(sizeof(struct simple_children),
  241. GFP_KERNEL);
  242. if (!simple_children)
  243. return ERR_PTR(-ENOMEM);
  244. config_group_init_type_name(&simple_children->group, name,
  245. &simple_children_type);
  246. return &simple_children->group;
  247. }
  248. static ssize_t group_children_description_show(struct config_item *item,
  249. char *page)
  250. {
  251. return sprintf(page,
  252. "[03-group-children]\n"
  253. "\n"
  254. "This subsystem allows the creation of child config_groups. These\n"
  255. "groups are like the subsystem simple-children.\n");
  256. }
  257. CONFIGFS_ATTR_RO(group_children_, description);
  258. static struct configfs_attribute *group_children_attrs[] = {
  259. &group_children_attr_description,
  260. NULL,
  261. };
  262. /*
  263. * Note that, since no extra work is required on ->drop_item(),
  264. * no ->drop_item() is provided.
  265. */
  266. static struct configfs_group_operations group_children_group_ops = {
  267. .make_group = group_children_make_group,
  268. };
  269. static struct config_item_type group_children_type = {
  270. .ct_group_ops = &group_children_group_ops,
  271. .ct_attrs = group_children_attrs,
  272. .ct_owner = THIS_MODULE,
  273. };
  274. static struct configfs_subsystem group_children_subsys = {
  275. .su_group = {
  276. .cg_item = {
  277. .ci_namebuf = "03-group-children",
  278. .ci_type = &group_children_type,
  279. },
  280. },
  281. };
  282. /* ----------------------------------------------------------------- */
  283. /*
  284. * We're now done with our subsystem definitions.
  285. * For convenience in this module, here's a list of them all. It
  286. * allows the init function to easily register them. Most modules
  287. * will only have one subsystem, and will only call register_subsystem
  288. * on it directly.
  289. */
  290. static struct configfs_subsystem *example_subsys[] = {
  291. &childless_subsys.subsys,
  292. &simple_children_subsys,
  293. &group_children_subsys,
  294. NULL,
  295. };
  296. static int __init configfs_example_init(void)
  297. {
  298. int ret;
  299. int i;
  300. struct configfs_subsystem *subsys;
  301. for (i = 0; example_subsys[i]; i++) {
  302. subsys = example_subsys[i];
  303. config_group_init(&subsys->su_group);
  304. mutex_init(&subsys->su_mutex);
  305. ret = configfs_register_subsystem(subsys);
  306. if (ret) {
  307. printk(KERN_ERR "Error %d while registering subsystem %s\n",
  308. ret,
  309. subsys->su_group.cg_item.ci_namebuf);
  310. goto out_unregister;
  311. }
  312. }
  313. return 0;
  314. out_unregister:
  315. for (i--; i >= 0; i--)
  316. configfs_unregister_subsystem(example_subsys[i]);
  317. return ret;
  318. }
  319. static void __exit configfs_example_exit(void)
  320. {
  321. int i;
  322. for (i = 0; example_subsys[i]; i++)
  323. configfs_unregister_subsystem(example_subsys[i]);
  324. }
  325. module_init(configfs_example_init);
  326. module_exit(configfs_example_exit);
  327. MODULE_LICENSE("GPL");