configfs.h 9.1 KB

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