configfs_example_macros.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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)), struct childless, subsys) : NULL;
  51. }
  52. CONFIGFS_ATTR_STRUCT(childless);
  53. #define CHILDLESS_ATTR(_name, _mode, _show, _store) \
  54. struct childless_attribute childless_attr_##_name = __CONFIGFS_ATTR(_name, _mode, _show, _store)
  55. #define CHILDLESS_ATTR_RO(_name, _show) \
  56. struct childless_attribute childless_attr_##_name = __CONFIGFS_ATTR_RO(_name, _show);
  57. static ssize_t childless_showme_read(struct childless *childless,
  58. char *page)
  59. {
  60. ssize_t pos;
  61. pos = sprintf(page, "%d\n", childless->showme);
  62. childless->showme++;
  63. return pos;
  64. }
  65. static ssize_t childless_storeme_read(struct childless *childless,
  66. char *page)
  67. {
  68. return sprintf(page, "%d\n", childless->storeme);
  69. }
  70. static ssize_t childless_storeme_write(struct childless *childless,
  71. const char *page,
  72. size_t count)
  73. {
  74. unsigned long tmp;
  75. char *p = (char *) page;
  76. tmp = simple_strtoul(p, &p, 10);
  77. if (!p || (*p && (*p != '\n')))
  78. return -EINVAL;
  79. if (tmp > INT_MAX)
  80. return -ERANGE;
  81. childless->storeme = tmp;
  82. return count;
  83. }
  84. static ssize_t childless_description_read(struct childless *childless,
  85. char *page)
  86. {
  87. return sprintf(page,
  88. "[01-childless]\n"
  89. "\n"
  90. "The childless subsystem is the simplest possible subsystem in\n"
  91. "configfs. It does not support the creation of child config_items.\n"
  92. "It only has a few attributes. In fact, it isn't much different\n"
  93. "than a directory in /proc.\n");
  94. }
  95. CHILDLESS_ATTR_RO(showme, childless_showme_read);
  96. CHILDLESS_ATTR(storeme, S_IRUGO | S_IWUSR, childless_storeme_read,
  97. childless_storeme_write);
  98. CHILDLESS_ATTR_RO(description, childless_description_read);
  99. static struct configfs_attribute *childless_attrs[] = {
  100. &childless_attr_showme.attr,
  101. &childless_attr_storeme.attr,
  102. &childless_attr_description.attr,
  103. NULL,
  104. };
  105. CONFIGFS_ATTR_OPS(childless);
  106. static struct configfs_item_operations childless_item_ops = {
  107. .show_attribute = childless_attr_show,
  108. .store_attribute = childless_attr_store,
  109. };
  110. static struct config_item_type childless_type = {
  111. .ct_item_ops = &childless_item_ops,
  112. .ct_attrs = childless_attrs,
  113. .ct_owner = THIS_MODULE,
  114. };
  115. static struct childless childless_subsys = {
  116. .subsys = {
  117. .su_group = {
  118. .cg_item = {
  119. .ci_namebuf = "01-childless",
  120. .ci_type = &childless_type,
  121. },
  122. },
  123. },
  124. };
  125. /* ----------------------------------------------------------------- */
  126. /*
  127. * 02-simple-children
  128. *
  129. * This example merely has a simple one-attribute child. Note that
  130. * there is no extra attribute structure, as the child's attribute is
  131. * known from the get-go. Also, there is no container for the
  132. * subsystem, as it has no attributes of its own.
  133. */
  134. struct simple_child {
  135. struct config_item item;
  136. int storeme;
  137. };
  138. static inline struct simple_child *to_simple_child(struct config_item *item)
  139. {
  140. return item ? container_of(item, struct simple_child, item) : NULL;
  141. }
  142. static struct configfs_attribute simple_child_attr_storeme = {
  143. .ca_owner = THIS_MODULE,
  144. .ca_name = "storeme",
  145. .ca_mode = S_IRUGO | S_IWUSR,
  146. };
  147. static struct configfs_attribute *simple_child_attrs[] = {
  148. &simple_child_attr_storeme,
  149. NULL,
  150. };
  151. static ssize_t simple_child_attr_show(struct config_item *item,
  152. struct configfs_attribute *attr,
  153. char *page)
  154. {
  155. ssize_t count;
  156. struct simple_child *simple_child = to_simple_child(item);
  157. count = sprintf(page, "%d\n", simple_child->storeme);
  158. return count;
  159. }
  160. static ssize_t simple_child_attr_store(struct config_item *item,
  161. struct configfs_attribute *attr,
  162. const char *page, size_t count)
  163. {
  164. struct simple_child *simple_child = to_simple_child(item);
  165. unsigned long tmp;
  166. char *p = (char *) page;
  167. tmp = simple_strtoul(p, &p, 10);
  168. if (!p || (*p && (*p != '\n')))
  169. return -EINVAL;
  170. if (tmp > INT_MAX)
  171. return -ERANGE;
  172. simple_child->storeme = tmp;
  173. return count;
  174. }
  175. static void simple_child_release(struct config_item *item)
  176. {
  177. kfree(to_simple_child(item));
  178. }
  179. static struct configfs_item_operations simple_child_item_ops = {
  180. .release = simple_child_release,
  181. .show_attribute = simple_child_attr_show,
  182. .store_attribute = simple_child_attr_store,
  183. };
  184. static struct config_item_type simple_child_type = {
  185. .ct_item_ops = &simple_child_item_ops,
  186. .ct_attrs = simple_child_attrs,
  187. .ct_owner = THIS_MODULE,
  188. };
  189. struct simple_children {
  190. struct config_group group;
  191. };
  192. static inline struct simple_children *to_simple_children(struct config_item *item)
  193. {
  194. return item ? container_of(to_config_group(item), struct simple_children, group) : NULL;
  195. }
  196. static struct config_item *simple_children_make_item(struct config_group *group, const char *name)
  197. {
  198. struct simple_child *simple_child;
  199. simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL);
  200. if (!simple_child)
  201. return ERR_PTR(-ENOMEM);
  202. config_item_init_type_name(&simple_child->item, name,
  203. &simple_child_type);
  204. simple_child->storeme = 0;
  205. return &simple_child->item;
  206. }
  207. static struct configfs_attribute simple_children_attr_description = {
  208. .ca_owner = THIS_MODULE,
  209. .ca_name = "description",
  210. .ca_mode = S_IRUGO,
  211. };
  212. static struct configfs_attribute *simple_children_attrs[] = {
  213. &simple_children_attr_description,
  214. NULL,
  215. };
  216. static ssize_t simple_children_attr_show(struct config_item *item,
  217. struct configfs_attribute *attr,
  218. char *page)
  219. {
  220. return sprintf(page,
  221. "[02-simple-children]\n"
  222. "\n"
  223. "This subsystem allows the creation of child config_items. These\n"
  224. "items have only one attribute that is readable and writeable.\n");
  225. }
  226. static void simple_children_release(struct config_item *item)
  227. {
  228. kfree(to_simple_children(item));
  229. }
  230. static struct configfs_item_operations simple_children_item_ops = {
  231. .release = simple_children_release,
  232. .show_attribute = simple_children_attr_show,
  233. };
  234. /*
  235. * Note that, since no extra work is required on ->drop_item(),
  236. * no ->drop_item() is provided.
  237. */
  238. static struct configfs_group_operations simple_children_group_ops = {
  239. .make_item = simple_children_make_item,
  240. };
  241. static struct config_item_type simple_children_type = {
  242. .ct_item_ops = &simple_children_item_ops,
  243. .ct_group_ops = &simple_children_group_ops,
  244. .ct_attrs = simple_children_attrs,
  245. .ct_owner = THIS_MODULE,
  246. };
  247. static struct configfs_subsystem simple_children_subsys = {
  248. .su_group = {
  249. .cg_item = {
  250. .ci_namebuf = "02-simple-children",
  251. .ci_type = &simple_children_type,
  252. },
  253. },
  254. };
  255. /* ----------------------------------------------------------------- */
  256. /*
  257. * 03-group-children
  258. *
  259. * This example reuses the simple_children group from above. However,
  260. * the simple_children group is not the subsystem itself, it is a
  261. * child of the subsystem. Creation of a group in the subsystem creates
  262. * a new simple_children group. That group can then have simple_child
  263. * children of its own.
  264. */
  265. static struct config_group *group_children_make_group(struct config_group *group, const char *name)
  266. {
  267. struct simple_children *simple_children;
  268. simple_children = kzalloc(sizeof(struct simple_children),
  269. GFP_KERNEL);
  270. if (!simple_children)
  271. return ERR_PTR(-ENOMEM);
  272. config_group_init_type_name(&simple_children->group, name,
  273. &simple_children_type);
  274. return &simple_children->group;
  275. }
  276. static struct configfs_attribute group_children_attr_description = {
  277. .ca_owner = THIS_MODULE,
  278. .ca_name = "description",
  279. .ca_mode = S_IRUGO,
  280. };
  281. static struct configfs_attribute *group_children_attrs[] = {
  282. &group_children_attr_description,
  283. NULL,
  284. };
  285. static ssize_t group_children_attr_show(struct config_item *item,
  286. struct configfs_attribute *attr,
  287. char *page)
  288. {
  289. return sprintf(page,
  290. "[03-group-children]\n"
  291. "\n"
  292. "This subsystem allows the creation of child config_groups. These\n"
  293. "groups are like the subsystem simple-children.\n");
  294. }
  295. static struct configfs_item_operations group_children_item_ops = {
  296. .show_attribute = group_children_attr_show,
  297. };
  298. /*
  299. * Note that, since no extra work is required on ->drop_item(),
  300. * no ->drop_item() is provided.
  301. */
  302. static struct configfs_group_operations group_children_group_ops = {
  303. .make_group = group_children_make_group,
  304. };
  305. static struct config_item_type group_children_type = {
  306. .ct_item_ops = &group_children_item_ops,
  307. .ct_group_ops = &group_children_group_ops,
  308. .ct_attrs = group_children_attrs,
  309. .ct_owner = THIS_MODULE,
  310. };
  311. static struct configfs_subsystem group_children_subsys = {
  312. .su_group = {
  313. .cg_item = {
  314. .ci_namebuf = "03-group-children",
  315. .ci_type = &group_children_type,
  316. },
  317. },
  318. };
  319. /* ----------------------------------------------------------------- */
  320. /*
  321. * We're now done with our subsystem definitions.
  322. * For convenience in this module, here's a list of them all. It
  323. * allows the init function to easily register them. Most modules
  324. * will only have one subsystem, and will only call register_subsystem
  325. * on it directly.
  326. */
  327. static struct configfs_subsystem *example_subsys[] = {
  328. &childless_subsys.subsys,
  329. &simple_children_subsys,
  330. &group_children_subsys,
  331. NULL,
  332. };
  333. static int __init configfs_example_init(void)
  334. {
  335. int ret;
  336. int i;
  337. struct configfs_subsystem *subsys;
  338. for (i = 0; example_subsys[i]; i++) {
  339. subsys = example_subsys[i];
  340. config_group_init(&subsys->su_group);
  341. mutex_init(&subsys->su_mutex);
  342. ret = configfs_register_subsystem(subsys);
  343. if (ret) {
  344. printk(KERN_ERR "Error %d while registering subsystem %s\n",
  345. ret,
  346. subsys->su_group.cg_item.ci_namebuf);
  347. goto out_unregister;
  348. }
  349. }
  350. return 0;
  351. out_unregister:
  352. for (i--; i >= 0; i--)
  353. configfs_unregister_subsystem(example_subsys[i]);
  354. return ret;
  355. }
  356. static void __exit configfs_example_exit(void)
  357. {
  358. int i;
  359. for (i = 0; example_subsys[i]; i++)
  360. configfs_unregister_subsystem(example_subsys[i]);
  361. }
  362. module_init(configfs_example_init);
  363. module_exit(configfs_example_exit);
  364. MODULE_LICENSE("GPL");