configfs.h 8.5 KB

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