configfs.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* -*- mode: c; c-basic-offset: 8; -*-
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * configfs.h - definitions for the device driver filesystem
  6. *
  7. * Based on sysfs:
  8. * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
  9. *
  10. * Based on kobject.h:
  11. * Copyright (c) 2002-2003 Patrick Mochel
  12. * Copyright (c) 2002-2003 Open Source Development Labs
  13. *
  14. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  15. *
  16. * Please read Documentation/filesystems/configfs.rst before using
  17. * the configfs interface, ESPECIALLY the parts about reference counts and
  18. * item destructors.
  19. */
  20. #ifndef _CONFIGFS_H_
  21. #define _CONFIGFS_H_
  22. #include <linux/stat.h> /* S_IRUGO */
  23. #include <linux/types.h> /* ssize_t */
  24. #include <linux/list.h> /* struct list_head */
  25. #include <linux/kref.h> /* struct kref */
  26. #include <linux/mutex.h> /* struct mutex */
  27. #define CONFIGFS_ITEM_NAME_LEN 20
  28. struct module;
  29. struct configfs_item_operations;
  30. struct configfs_group_operations;
  31. struct configfs_attribute;
  32. struct configfs_bin_attribute;
  33. struct configfs_subsystem;
  34. struct config_item {
  35. char *ci_name;
  36. char ci_namebuf[CONFIGFS_ITEM_NAME_LEN];
  37. struct kref ci_kref;
  38. struct list_head ci_entry;
  39. struct config_item *ci_parent;
  40. struct config_group *ci_group;
  41. const struct config_item_type *ci_type;
  42. struct dentry *ci_dentry;
  43. };
  44. extern __printf(2, 3)
  45. int config_item_set_name(struct config_item *, const char *, ...);
  46. static inline char *config_item_name(struct config_item * item)
  47. {
  48. return item->ci_name;
  49. }
  50. extern void config_item_init_type_name(struct config_item *item,
  51. const char *name,
  52. const struct config_item_type *type);
  53. extern struct config_item *config_item_get(struct config_item *);
  54. extern struct config_item *config_item_get_unless_zero(struct config_item *);
  55. extern void config_item_put(struct config_item *);
  56. struct config_item_type {
  57. struct module *ct_owner;
  58. struct configfs_item_operations *ct_item_ops;
  59. struct configfs_group_operations *ct_group_ops;
  60. struct configfs_attribute **ct_attrs;
  61. struct configfs_bin_attribute **ct_bin_attrs;
  62. };
  63. /**
  64. * group - a group of config_items of a specific type, belonging
  65. * to a specific subsystem.
  66. */
  67. struct config_group {
  68. struct config_item cg_item;
  69. struct list_head cg_children;
  70. struct configfs_subsystem *cg_subsys;
  71. struct list_head default_groups;
  72. struct list_head group_entry;
  73. };
  74. extern void config_group_init(struct config_group *group);
  75. extern void config_group_init_type_name(struct config_group *group,
  76. const char *name,
  77. const struct config_item_type *type);
  78. static inline struct config_group *to_config_group(struct config_item *item)
  79. {
  80. return item ? container_of(item,struct config_group,cg_item) : NULL;
  81. }
  82. static inline struct config_group *config_group_get(struct config_group *group)
  83. {
  84. return group ? to_config_group(config_item_get(&group->cg_item)) : NULL;
  85. }
  86. static inline void config_group_put(struct config_group *group)
  87. {
  88. config_item_put(&group->cg_item);
  89. }
  90. extern struct config_item *config_group_find_item(struct config_group *,
  91. const char *);
  92. static inline void configfs_add_default_group(struct config_group *new_group,
  93. struct config_group *group)
  94. {
  95. list_add_tail(&new_group->group_entry, &group->default_groups);
  96. }
  97. struct configfs_attribute {
  98. const char *ca_name;
  99. struct module *ca_owner;
  100. umode_t ca_mode;
  101. ssize_t (*show)(struct config_item *, char *);
  102. ssize_t (*store)(struct config_item *, const char *, size_t);
  103. };
  104. #define CONFIGFS_ATTR(_pfx, _name) \
  105. static struct configfs_attribute _pfx##attr_##_name = { \
  106. .ca_name = __stringify(_name), \
  107. .ca_mode = S_IRUGO | S_IWUSR, \
  108. .ca_owner = THIS_MODULE, \
  109. .show = _pfx##_name##_show, \
  110. .store = _pfx##_name##_store, \
  111. }
  112. #define CONFIGFS_ATTR_RO(_pfx, _name) \
  113. static struct configfs_attribute _pfx##attr_##_name = { \
  114. .ca_name = __stringify(_name), \
  115. .ca_mode = S_IRUGO, \
  116. .ca_owner = THIS_MODULE, \
  117. .show = _pfx##_name##_show, \
  118. }
  119. #define CONFIGFS_ATTR_WO(_pfx, _name) \
  120. static struct configfs_attribute _pfx##attr_##_name = { \
  121. .ca_name = __stringify(_name), \
  122. .ca_mode = S_IWUSR, \
  123. .ca_owner = THIS_MODULE, \
  124. .store = _pfx##_name##_store, \
  125. }
  126. struct file;
  127. struct vm_area_struct;
  128. struct configfs_bin_attribute {
  129. struct configfs_attribute cb_attr; /* std. attribute */
  130. void *cb_private; /* for user */
  131. size_t cb_max_size; /* max core size */
  132. ssize_t (*read)(struct config_item *, void *, size_t);
  133. ssize_t (*write)(struct config_item *, const void *, size_t);
  134. };
  135. #define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz) \
  136. static struct configfs_bin_attribute _pfx##attr_##_name = { \
  137. .cb_attr = { \
  138. .ca_name = __stringify(_name), \
  139. .ca_mode = S_IRUGO | S_IWUSR, \
  140. .ca_owner = THIS_MODULE, \
  141. }, \
  142. .cb_private = _priv, \
  143. .cb_max_size = _maxsz, \
  144. .read = _pfx##_name##_read, \
  145. .write = _pfx##_name##_write, \
  146. }
  147. #define CONFIGFS_BIN_ATTR_RO(_pfx, _name, _priv, _maxsz) \
  148. static struct configfs_bin_attribute _pfx##attr_##_name = { \
  149. .cb_attr = { \
  150. .ca_name = __stringify(_name), \
  151. .ca_mode = S_IRUGO, \
  152. .ca_owner = THIS_MODULE, \
  153. }, \
  154. .cb_private = _priv, \
  155. .cb_max_size = _maxsz, \
  156. .read = _pfx##_name##_read, \
  157. }
  158. #define CONFIGFS_BIN_ATTR_WO(_pfx, _name, _priv, _maxsz) \
  159. static struct configfs_bin_attribute _pfx##attr_##_name = { \
  160. .cb_attr = { \
  161. .ca_name = __stringify(_name), \
  162. .ca_mode = S_IWUSR, \
  163. .ca_owner = THIS_MODULE, \
  164. }, \
  165. .cb_private = _priv, \
  166. .cb_max_size = _maxsz, \
  167. .write = _pfx##_name##_write, \
  168. }
  169. /*
  170. * If allow_link() exists, the item can symlink(2) out to other
  171. * items. If the item is a group, it may support mkdir(2).
  172. * Groups supply one of make_group() and make_item(). If the
  173. * group supports make_group(), one can create group children. If it
  174. * supports make_item(), one can create config_item children. make_group()
  175. * and make_item() return ERR_PTR() on errors. If it has
  176. * default_groups on group->default_groups, it has automatically created
  177. * group children. default_groups may coexist alongsize make_group() or
  178. * make_item(), but if the group wishes to have only default_groups
  179. * children (disallowing mkdir(2)), it need not provide either function.
  180. * If the group has commit(), it supports pending and committed (active)
  181. * items.
  182. */
  183. struct configfs_item_operations {
  184. void (*release)(struct config_item *);
  185. int (*allow_link)(struct config_item *src, struct config_item *target);
  186. void (*drop_link)(struct config_item *src, struct config_item *target);
  187. };
  188. struct configfs_group_operations {
  189. struct config_item *(*make_item)(struct config_group *group, const char *name);
  190. struct config_group *(*make_group)(struct config_group *group, const char *name);
  191. int (*commit_item)(struct config_item *item);
  192. void (*disconnect_notify)(struct config_group *group, struct config_item *item);
  193. void (*drop_item)(struct config_group *group, struct config_item *item);
  194. };
  195. struct configfs_subsystem {
  196. struct config_group su_group;
  197. struct mutex su_mutex;
  198. };
  199. static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group)
  200. {
  201. return group ?
  202. container_of(group, struct configfs_subsystem, su_group) :
  203. NULL;
  204. }
  205. int configfs_register_subsystem(struct configfs_subsystem *subsys);
  206. void configfs_unregister_subsystem(struct configfs_subsystem *subsys);
  207. int configfs_register_group(struct config_group *parent_group,
  208. struct config_group *group);
  209. void configfs_unregister_group(struct config_group *group);
  210. void configfs_remove_default_groups(struct config_group *group);
  211. struct config_group *
  212. configfs_register_default_group(struct config_group *parent_group,
  213. const char *name,
  214. const struct config_item_type *item_type);
  215. void configfs_unregister_default_group(struct config_group *group);
  216. /* These functions can sleep and can alloc with GFP_KERNEL */
  217. /* WARNING: These cannot be called underneath configfs callbacks!! */
  218. int configfs_depend_item(struct configfs_subsystem *subsys,
  219. struct config_item *target);
  220. void configfs_undepend_item(struct config_item *target);
  221. /*
  222. * These functions can sleep and can alloc with GFP_KERNEL
  223. * NOTE: These should be called only underneath configfs callbacks.
  224. * NOTE: First parameter is a caller's subsystem, not target's.
  225. * WARNING: These cannot be called on newly created item
  226. * (in make_group()/make_item() callback)
  227. */
  228. int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
  229. struct config_item *target);
  230. static inline void configfs_undepend_item_unlocked(struct config_item *target)
  231. {
  232. configfs_undepend_item(target);
  233. }
  234. #endif /* _CONFIGFS_H_ */