configfs_example_explicit.c 12 KB

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