configfs.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * configfs.h - definitions for the device driver filesystem
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 021110-1307, USA.
  20. *
  21. * Based on sysfs:
  22. * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
  23. *
  24. * Based on kobject.h:
  25. * Copyright (c) 2002-2003 Patrick Mochel
  26. * Copyright (c) 2002-2003 Open Source Development Labs
  27. *
  28. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  29. *
  30. * Please read Documentation/filesystems/configfs/configfs.txt before using
  31. * the configfs interface, ESPECIALLY the parts about reference counts and
  32. * item destructors.
  33. */
  34. #ifndef _CONFIGFS_H_
  35. #define _CONFIGFS_H_
  36. #include <linux/kernel.h>
  37. #include <linux/types.h>
  38. #include <linux/list.h>
  39. #include <linux/kref.h>
  40. #include <linux/mutex.h>
  41. #include <linux/err.h>
  42. #include <linux/atomic.h>
  43. #define CONFIGFS_ITEM_NAME_LEN 20
  44. struct module;
  45. struct configfs_item_operations;
  46. struct configfs_group_operations;
  47. struct configfs_attribute;
  48. struct configfs_subsystem;
  49. struct config_item {
  50. char *ci_name;
  51. char ci_namebuf[CONFIGFS_ITEM_NAME_LEN];
  52. struct kref ci_kref;
  53. struct list_head ci_entry;
  54. struct config_item *ci_parent;
  55. struct config_group *ci_group;
  56. struct config_item_type *ci_type;
  57. struct dentry *ci_dentry;
  58. };
  59. extern int config_item_set_name(struct config_item *, const char *, ...);
  60. static inline char *config_item_name(struct config_item * item)
  61. {
  62. return item->ci_name;
  63. }
  64. extern void config_item_init(struct config_item *);
  65. extern void config_item_init_type_name(struct config_item *item,
  66. const char *name,
  67. struct config_item_type *type);
  68. extern struct config_item * config_item_get(struct config_item *);
  69. extern void config_item_put(struct config_item *);
  70. struct config_item_type {
  71. struct module *ct_owner;
  72. struct configfs_item_operations *ct_item_ops;
  73. struct configfs_group_operations *ct_group_ops;
  74. struct configfs_attribute **ct_attrs;
  75. };
  76. /**
  77. * group - a group of config_items of a specific type, belonging
  78. * to a specific subsystem.
  79. */
  80. struct config_group {
  81. struct config_item cg_item;
  82. struct list_head cg_children;
  83. struct configfs_subsystem *cg_subsys;
  84. struct config_group **default_groups;
  85. };
  86. extern void config_group_init(struct config_group *group);
  87. extern void config_group_init_type_name(struct config_group *group,
  88. const char *name,
  89. struct config_item_type *type);
  90. static inline struct config_group *to_config_group(struct config_item *item)
  91. {
  92. return item ? container_of(item,struct config_group,cg_item) : NULL;
  93. }
  94. static inline struct config_group *config_group_get(struct config_group *group)
  95. {
  96. return group ? to_config_group(config_item_get(&group->cg_item)) : NULL;
  97. }
  98. static inline void config_group_put(struct config_group *group)
  99. {
  100. config_item_put(&group->cg_item);
  101. }
  102. extern struct config_item *config_group_find_item(struct config_group *,
  103. const char *);
  104. struct configfs_attribute {
  105. const char *ca_name;
  106. struct module *ca_owner;
  107. umode_t ca_mode;
  108. };
  109. /*
  110. * Users often need to create attribute structures for their configurable
  111. * attributes, containing a configfs_attribute member and function pointers
  112. * for the show() and store() operations on that attribute. If they don't
  113. * need anything else on the extended attribute structure, they can use
  114. * this macro to define it The argument _item is the name of the
  115. * config_item structure.
  116. */
  117. #define CONFIGFS_ATTR_STRUCT(_item) \
  118. struct _item##_attribute { \
  119. struct configfs_attribute attr; \
  120. ssize_t (*show)(struct _item *, char *); \
  121. ssize_t (*store)(struct _item *, const char *, size_t); \
  122. }
  123. /*
  124. * With the extended attribute structure, users can use this macro
  125. * (similar to sysfs' __ATTR) to make defining attributes easier.
  126. * An example:
  127. * #define MYITEM_ATTR(_name, _mode, _show, _store) \
  128. * struct myitem_attribute childless_attr_##_name = \
  129. * __CONFIGFS_ATTR(_name, _mode, _show, _store)
  130. */
  131. #define __CONFIGFS_ATTR(_name, _mode, _show, _store) \
  132. { \
  133. .attr = { \
  134. .ca_name = __stringify(_name), \
  135. .ca_mode = _mode, \
  136. .ca_owner = THIS_MODULE, \
  137. }, \
  138. .show = _show, \
  139. .store = _store, \
  140. }
  141. /* Here is a readonly version, only requiring a show() operation */
  142. #define __CONFIGFS_ATTR_RO(_name, _show) \
  143. { \
  144. .attr = { \
  145. .ca_name = __stringify(_name), \
  146. .ca_mode = 0444, \
  147. .ca_owner = THIS_MODULE, \
  148. }, \
  149. .show = _show, \
  150. }
  151. /*
  152. * With these extended attributes, the simple show_attribute() and
  153. * store_attribute() operations need to call the show() and store() of the
  154. * attributes. This is a common pattern, so we provide a macro to define
  155. * them. The argument _item is the name of the config_item structure.
  156. * This macro expects the attributes to be named "struct <name>_attribute"
  157. * and the function to_<name>() to exist;
  158. */
  159. #define CONFIGFS_ATTR_OPS(_item) \
  160. static ssize_t _item##_attr_show(struct config_item *item, \
  161. struct configfs_attribute *attr, \
  162. char *page) \
  163. { \
  164. struct _item *_item = to_##_item(item); \
  165. struct _item##_attribute *_item##_attr = \
  166. container_of(attr, struct _item##_attribute, attr); \
  167. ssize_t ret = 0; \
  168. \
  169. if (_item##_attr->show) \
  170. ret = _item##_attr->show(_item, page); \
  171. return ret; \
  172. } \
  173. static ssize_t _item##_attr_store(struct config_item *item, \
  174. struct configfs_attribute *attr, \
  175. const char *page, size_t count) \
  176. { \
  177. struct _item *_item = to_##_item(item); \
  178. struct _item##_attribute *_item##_attr = \
  179. container_of(attr, struct _item##_attribute, attr); \
  180. ssize_t ret = -EINVAL; \
  181. \
  182. if (_item##_attr->store) \
  183. ret = _item##_attr->store(_item, page, count); \
  184. return ret; \
  185. }
  186. /*
  187. * If allow_link() exists, the item can symlink(2) out to other
  188. * items. If the item is a group, it may support mkdir(2).
  189. * Groups supply one of make_group() and make_item(). If the
  190. * group supports make_group(), one can create group children. If it
  191. * supports make_item(), one can create config_item children. make_group()
  192. * and make_item() return ERR_PTR() on errors. If it has
  193. * default_groups on group->default_groups, it has automatically created
  194. * group children. default_groups may coexist alongsize make_group() or
  195. * make_item(), but if the group wishes to have only default_groups
  196. * children (disallowing mkdir(2)), it need not provide either function.
  197. * If the group has commit(), it supports pending and committed (active)
  198. * items.
  199. */
  200. struct configfs_item_operations {
  201. void (*release)(struct config_item *);
  202. ssize_t (*show_attribute)(struct config_item *, struct configfs_attribute *,char *);
  203. ssize_t (*store_attribute)(struct config_item *,struct configfs_attribute *,const char *, size_t);
  204. int (*allow_link)(struct config_item *src, struct config_item *target);
  205. int (*drop_link)(struct config_item *src, struct config_item *target);
  206. };
  207. struct configfs_group_operations {
  208. struct config_item *(*make_item)(struct config_group *group, const char *name);
  209. struct config_group *(*make_group)(struct config_group *group, const char *name);
  210. int (*commit_item)(struct config_item *item);
  211. void (*disconnect_notify)(struct config_group *group, struct config_item *item);
  212. void (*drop_item)(struct config_group *group, struct config_item *item);
  213. };
  214. struct configfs_subsystem {
  215. struct config_group su_group;
  216. struct mutex su_mutex;
  217. };
  218. static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group)
  219. {
  220. return group ?
  221. container_of(group, struct configfs_subsystem, su_group) :
  222. NULL;
  223. }
  224. int configfs_register_subsystem(struct configfs_subsystem *subsys);
  225. void configfs_unregister_subsystem(struct configfs_subsystem *subsys);
  226. /* These functions can sleep and can alloc with GFP_KERNEL */
  227. /* WARNING: These cannot be called underneath configfs callbacks!! */
  228. int configfs_depend_item(struct configfs_subsystem *subsys, struct config_item *target);
  229. void configfs_undepend_item(struct configfs_subsystem *subsys, struct config_item *target);
  230. #endif /* _CONFIGFS_H_ */