configfs.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/stat.h> /* S_IRUGO */
  37. #include <linux/types.h> /* ssize_t */
  38. #include <linux/list.h> /* struct list_head */
  39. #include <linux/kref.h> /* struct kref */
  40. #include <linux/mutex.h> /* struct mutex */
  41. #define CONFIGFS_ITEM_NAME_LEN 20
  42. struct module;
  43. struct configfs_item_operations;
  44. struct configfs_group_operations;
  45. struct configfs_attribute;
  46. struct configfs_bin_attribute;
  47. struct configfs_subsystem;
  48. struct config_item {
  49. char *ci_name;
  50. char ci_namebuf[CONFIGFS_ITEM_NAME_LEN];
  51. struct kref ci_kref;
  52. struct list_head ci_entry;
  53. struct config_item *ci_parent;
  54. struct config_group *ci_group;
  55. const struct config_item_type *ci_type;
  56. struct dentry *ci_dentry;
  57. };
  58. extern __printf(2, 3)
  59. 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_type_name(struct config_item *item,
  65. const char *name,
  66. const struct config_item_type *type);
  67. extern struct config_item *config_item_get(struct config_item *);
  68. extern struct config_item *config_item_get_unless_zero(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. struct configfs_bin_attribute **ct_bin_attrs;
  76. };
  77. /**
  78. * group - a group of config_items of a specific type, belonging
  79. * to a specific subsystem.
  80. */
  81. struct config_group {
  82. struct config_item cg_item;
  83. struct list_head cg_children;
  84. struct configfs_subsystem *cg_subsys;
  85. struct list_head default_groups;
  86. struct list_head group_entry;
  87. };
  88. extern void config_group_init(struct config_group *group);
  89. extern void config_group_init_type_name(struct config_group *group,
  90. const char *name,
  91. const struct config_item_type *type);
  92. static inline struct config_group *to_config_group(struct config_item *item)
  93. {
  94. return item ? container_of(item,struct config_group,cg_item) : NULL;
  95. }
  96. static inline struct config_group *config_group_get(struct config_group *group)
  97. {
  98. return group ? to_config_group(config_item_get(&group->cg_item)) : NULL;
  99. }
  100. static inline void config_group_put(struct config_group *group)
  101. {
  102. config_item_put(&group->cg_item);
  103. }
  104. extern struct config_item *config_group_find_item(struct config_group *,
  105. const char *);
  106. static inline void configfs_add_default_group(struct config_group *new_group,
  107. struct config_group *group)
  108. {
  109. list_add_tail(&new_group->group_entry, &group->default_groups);
  110. }
  111. struct configfs_attribute {
  112. const char *ca_name;
  113. struct module *ca_owner;
  114. umode_t ca_mode;
  115. ssize_t (*show)(struct config_item *, char *);
  116. ssize_t (*store)(struct config_item *, const char *, size_t);
  117. };
  118. #define CONFIGFS_ATTR(_pfx, _name) \
  119. static struct configfs_attribute _pfx##attr_##_name = { \
  120. .ca_name = __stringify(_name), \
  121. .ca_mode = S_IRUGO | S_IWUSR, \
  122. .ca_owner = THIS_MODULE, \
  123. .show = _pfx##_name##_show, \
  124. .store = _pfx##_name##_store, \
  125. }
  126. #define CONFIGFS_ATTR_RO(_pfx, _name) \
  127. static struct configfs_attribute _pfx##attr_##_name = { \
  128. .ca_name = __stringify(_name), \
  129. .ca_mode = S_IRUGO, \
  130. .ca_owner = THIS_MODULE, \
  131. .show = _pfx##_name##_show, \
  132. }
  133. #define CONFIGFS_ATTR_WO(_pfx, _name) \
  134. static struct configfs_attribute _pfx##attr_##_name = { \
  135. .ca_name = __stringify(_name), \
  136. .ca_mode = S_IWUSR, \
  137. .ca_owner = THIS_MODULE, \
  138. .store = _pfx##_name##_store, \
  139. }
  140. struct file;
  141. struct vm_area_struct;
  142. struct configfs_bin_attribute {
  143. struct configfs_attribute cb_attr; /* std. attribute */
  144. void *cb_private; /* for user */
  145. size_t cb_max_size; /* max core size */
  146. ssize_t (*read)(struct config_item *, void *, size_t);
  147. ssize_t (*write)(struct config_item *, const void *, size_t);
  148. };
  149. #define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz) \
  150. static struct configfs_bin_attribute _pfx##attr_##_name = { \
  151. .cb_attr = { \
  152. .ca_name = __stringify(_name), \
  153. .ca_mode = S_IRUGO | S_IWUSR, \
  154. .ca_owner = THIS_MODULE, \
  155. }, \
  156. .cb_private = _priv, \
  157. .cb_max_size = _maxsz, \
  158. .read = _pfx##_name##_read, \
  159. .write = _pfx##_name##_write, \
  160. }
  161. #define CONFIGFS_BIN_ATTR_RO(_pfx, _name, _priv, _maxsz) \
  162. static struct configfs_bin_attribute _pfx##attr_##_name = { \
  163. .cb_attr = { \
  164. .ca_name = __stringify(_name), \
  165. .ca_mode = S_IRUGO, \
  166. .ca_owner = THIS_MODULE, \
  167. }, \
  168. .cb_private = _priv, \
  169. .cb_max_size = _maxsz, \
  170. .read = _pfx##_name##_read, \
  171. }
  172. #define CONFIGFS_BIN_ATTR_WO(_pfx, _name, _priv, _maxsz) \
  173. static struct configfs_bin_attribute _pfx##attr_##_name = { \
  174. .cb_attr = { \
  175. .ca_name = __stringify(_name), \
  176. .ca_mode = S_IWUSR, \
  177. .ca_owner = THIS_MODULE, \
  178. }, \
  179. .cb_private = _priv, \
  180. .cb_max_size = _maxsz, \
  181. .write = _pfx##_name##_write, \
  182. }
  183. /*
  184. * If allow_link() exists, the item can symlink(2) out to other
  185. * items. If the item is a group, it may support mkdir(2).
  186. * Groups supply one of make_group() and make_item(). If the
  187. * group supports make_group(), one can create group children. If it
  188. * supports make_item(), one can create config_item children. make_group()
  189. * and make_item() return ERR_PTR() on errors. If it has
  190. * default_groups on group->default_groups, it has automatically created
  191. * group children. default_groups may coexist alongsize make_group() or
  192. * make_item(), but if the group wishes to have only default_groups
  193. * children (disallowing mkdir(2)), it need not provide either function.
  194. * If the group has commit(), it supports pending and committed (active)
  195. * items.
  196. */
  197. struct configfs_item_operations {
  198. void (*release)(struct config_item *);
  199. int (*allow_link)(struct config_item *src, struct config_item *target);
  200. void (*drop_link)(struct config_item *src, struct config_item *target);
  201. };
  202. struct configfs_group_operations {
  203. struct config_item *(*make_item)(struct config_group *group, const char *name);
  204. struct config_group *(*make_group)(struct config_group *group, const char *name);
  205. int (*commit_item)(struct config_item *item);
  206. void (*disconnect_notify)(struct config_group *group, struct config_item *item);
  207. void (*drop_item)(struct config_group *group, struct config_item *item);
  208. };
  209. struct configfs_subsystem {
  210. struct config_group su_group;
  211. struct mutex su_mutex;
  212. };
  213. static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group)
  214. {
  215. return group ?
  216. container_of(group, struct configfs_subsystem, su_group) :
  217. NULL;
  218. }
  219. int configfs_register_subsystem(struct configfs_subsystem *subsys);
  220. void configfs_unregister_subsystem(struct configfs_subsystem *subsys);
  221. int configfs_register_group(struct config_group *parent_group,
  222. struct config_group *group);
  223. void configfs_unregister_group(struct config_group *group);
  224. void configfs_remove_default_groups(struct config_group *group);
  225. struct config_group *
  226. configfs_register_default_group(struct config_group *parent_group,
  227. const char *name,
  228. const struct config_item_type *item_type);
  229. void configfs_unregister_default_group(struct config_group *group);
  230. /* These functions can sleep and can alloc with GFP_KERNEL */
  231. /* WARNING: These cannot be called underneath configfs callbacks!! */
  232. int configfs_depend_item(struct configfs_subsystem *subsys,
  233. struct config_item *target);
  234. void configfs_undepend_item(struct config_item *target);
  235. /*
  236. * These functions can sleep and can alloc with GFP_KERNEL
  237. * NOTE: These should be called only underneath configfs callbacks.
  238. * NOTE: First parameter is a caller's subsystem, not target's.
  239. * WARNING: These cannot be called on newly created item
  240. * (in make_group()/make_item() callback)
  241. */
  242. int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
  243. struct config_item *target);
  244. static inline void configfs_undepend_item_unlocked(struct config_item *target)
  245. {
  246. configfs_undepend_item(target);
  247. }
  248. #endif /* _CONFIGFS_H_ */