configfs_example.c 12 KB

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