Commit | Line | Data |
---|---|---|
328970de | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
fa60ce2c | 2 | /* |
7063fbf2 JB |
3 | * configfs.h - definitions for the device driver filesystem |
4 | * | |
7063fbf2 JB |
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 | * | |
98264991 | 14 | * Please read Documentation/filesystems/configfs.rst before using |
55dff495 | 15 | * the configfs interface, ESPECIALLY the parts about reference counts and |
7063fbf2 JB |
16 | * item destructors. |
17 | */ | |
18 | ||
19 | #ifndef _CONFIGFS_H_ | |
20 | #define _CONFIGFS_H_ | |
21 | ||
a6ab5374 BVA |
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 */ | |
7063fbf2 JB |
27 | |
28 | #define CONFIGFS_ITEM_NAME_LEN 20 | |
29 | ||
30 | struct module; | |
31 | ||
32 | struct configfs_item_operations; | |
33 | struct configfs_group_operations; | |
34 | struct configfs_attribute; | |
03607ace | 35 | struct configfs_bin_attribute; |
7063fbf2 JB |
36 | struct configfs_subsystem; |
37 | ||
38 | struct config_item { | |
39 | char *ci_name; | |
40 | char ci_namebuf[CONFIGFS_ITEM_NAME_LEN]; | |
41 | struct kref ci_kref; | |
42 | struct list_head ci_entry; | |
43 | struct config_item *ci_parent; | |
44 | struct config_group *ci_group; | |
aa293583 | 45 | const struct config_item_type *ci_type; |
7063fbf2 JB |
46 | struct dentry *ci_dentry; |
47 | }; | |
48 | ||
8db14860 NI |
49 | extern __printf(2, 3) |
50 | int config_item_set_name(struct config_item *, const char *, ...); | |
7063fbf2 JB |
51 | |
52 | static inline char *config_item_name(struct config_item * item) | |
53 | { | |
54 | return item->ci_name; | |
55 | } | |
56 | ||
7063fbf2 JB |
57 | extern void config_item_init_type_name(struct config_item *item, |
58 | const char *name, | |
aa293583 | 59 | const struct config_item_type *type); |
7063fbf2 | 60 | |
19e72d3a BVA |
61 | extern struct config_item *config_item_get(struct config_item *); |
62 | extern struct config_item *config_item_get_unless_zero(struct config_item *); | |
7063fbf2 JB |
63 | extern void config_item_put(struct config_item *); |
64 | ||
65 | struct config_item_type { | |
66 | struct module *ct_owner; | |
67 | struct configfs_item_operations *ct_item_ops; | |
68 | struct configfs_group_operations *ct_group_ops; | |
69 | struct configfs_attribute **ct_attrs; | |
03607ace | 70 | struct configfs_bin_attribute **ct_bin_attrs; |
7063fbf2 JB |
71 | }; |
72 | ||
7063fbf2 JB |
73 | /** |
74 | * group - a group of config_items of a specific type, belonging | |
75 | * to a specific subsystem. | |
76 | */ | |
7063fbf2 JB |
77 | struct config_group { |
78 | struct config_item cg_item; | |
79 | struct list_head cg_children; | |
80 | struct configfs_subsystem *cg_subsys; | |
1ae1602d CH |
81 | struct list_head default_groups; |
82 | struct list_head group_entry; | |
7063fbf2 JB |
83 | }; |
84 | ||
7063fbf2 JB |
85 | extern void config_group_init(struct config_group *group); |
86 | extern void config_group_init_type_name(struct config_group *group, | |
87 | const char *name, | |
aa293583 | 88 | const struct config_item_type *type); |
7063fbf2 | 89 | |
7063fbf2 JB |
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 | ||
95 | static inline struct config_group *config_group_get(struct config_group *group) | |
96 | { | |
97 | return group ? to_config_group(config_item_get(&group->cg_item)) : NULL; | |
98 | } | |
99 | ||
100 | static inline void config_group_put(struct config_group *group) | |
101 | { | |
102 | config_item_put(&group->cg_item); | |
103 | } | |
104 | ||
3fe6c5ce SS |
105 | extern struct config_item *config_group_find_item(struct config_group *, |
106 | const char *); | |
7063fbf2 JB |
107 | |
108 | ||
1ae1602d CH |
109 | static inline void configfs_add_default_group(struct config_group *new_group, |
110 | struct config_group *group) | |
111 | { | |
112 | list_add_tail(&new_group->group_entry, &group->default_groups); | |
113 | } | |
114 | ||
7063fbf2 | 115 | struct configfs_attribute { |
3d0f89bb | 116 | const char *ca_name; |
7063fbf2 | 117 | struct module *ca_owner; |
43947514 | 118 | umode_t ca_mode; |
870823e6 CH |
119 | ssize_t (*show)(struct config_item *, char *); |
120 | ssize_t (*store)(struct config_item *, const char *, size_t); | |
7063fbf2 JB |
121 | }; |
122 | ||
870823e6 CH |
123 | #define CONFIGFS_ATTR(_pfx, _name) \ |
124 | static struct configfs_attribute _pfx##attr_##_name = { \ | |
125 | .ca_name = __stringify(_name), \ | |
126 | .ca_mode = S_IRUGO | S_IWUSR, \ | |
127 | .ca_owner = THIS_MODULE, \ | |
128 | .show = _pfx##_name##_show, \ | |
129 | .store = _pfx##_name##_store, \ | |
130 | } | |
131 | ||
132 | #define CONFIGFS_ATTR_RO(_pfx, _name) \ | |
133 | static struct configfs_attribute _pfx##attr_##_name = { \ | |
134 | .ca_name = __stringify(_name), \ | |
135 | .ca_mode = S_IRUGO, \ | |
136 | .ca_owner = THIS_MODULE, \ | |
137 | .show = _pfx##_name##_show, \ | |
138 | } | |
139 | ||
140 | #define CONFIGFS_ATTR_WO(_pfx, _name) \ | |
141 | static struct configfs_attribute _pfx##attr_##_name = { \ | |
142 | .ca_name = __stringify(_name), \ | |
143 | .ca_mode = S_IWUSR, \ | |
144 | .ca_owner = THIS_MODULE, \ | |
145 | .store = _pfx##_name##_store, \ | |
146 | } | |
147 | ||
03607ace PA |
148 | struct file; |
149 | struct vm_area_struct; | |
150 | ||
151 | struct configfs_bin_attribute { | |
152 | struct configfs_attribute cb_attr; /* std. attribute */ | |
153 | void *cb_private; /* for user */ | |
154 | size_t cb_max_size; /* max core size */ | |
155 | ssize_t (*read)(struct config_item *, void *, size_t); | |
156 | ssize_t (*write)(struct config_item *, const void *, size_t); | |
157 | }; | |
158 | ||
159 | #define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz) \ | |
160 | static struct configfs_bin_attribute _pfx##attr_##_name = { \ | |
161 | .cb_attr = { \ | |
162 | .ca_name = __stringify(_name), \ | |
163 | .ca_mode = S_IRUGO | S_IWUSR, \ | |
164 | .ca_owner = THIS_MODULE, \ | |
165 | }, \ | |
166 | .cb_private = _priv, \ | |
167 | .cb_max_size = _maxsz, \ | |
168 | .read = _pfx##_name##_read, \ | |
169 | .write = _pfx##_name##_write, \ | |
170 | } | |
171 | ||
172 | #define CONFIGFS_BIN_ATTR_RO(_pfx, _name, _priv, _maxsz) \ | |
96c22a32 | 173 | static struct configfs_bin_attribute _pfx##attr_##_name = { \ |
03607ace PA |
174 | .cb_attr = { \ |
175 | .ca_name = __stringify(_name), \ | |
176 | .ca_mode = S_IRUGO, \ | |
177 | .ca_owner = THIS_MODULE, \ | |
178 | }, \ | |
179 | .cb_private = _priv, \ | |
180 | .cb_max_size = _maxsz, \ | |
181 | .read = _pfx##_name##_read, \ | |
182 | } | |
183 | ||
184 | #define CONFIGFS_BIN_ATTR_WO(_pfx, _name, _priv, _maxsz) \ | |
96c22a32 | 185 | static struct configfs_bin_attribute _pfx##attr_##_name = { \ |
03607ace PA |
186 | .cb_attr = { \ |
187 | .ca_name = __stringify(_name), \ | |
188 | .ca_mode = S_IWUSR, \ | |
189 | .ca_owner = THIS_MODULE, \ | |
190 | }, \ | |
191 | .cb_private = _priv, \ | |
192 | .cb_max_size = _maxsz, \ | |
193 | .write = _pfx##_name##_write, \ | |
194 | } | |
195 | ||
7063fbf2 JB |
196 | /* |
197 | * If allow_link() exists, the item can symlink(2) out to other | |
198 | * items. If the item is a group, it may support mkdir(2). | |
199 | * Groups supply one of make_group() and make_item(). If the | |
200 | * group supports make_group(), one can create group children. If it | |
a6795e9e JB |
201 | * supports make_item(), one can create config_item children. make_group() |
202 | * and make_item() return ERR_PTR() on errors. If it has | |
7063fbf2 JB |
203 | * default_groups on group->default_groups, it has automatically created |
204 | * group children. default_groups may coexist alongsize make_group() or | |
205 | * make_item(), but if the group wishes to have only default_groups | |
206 | * children (disallowing mkdir(2)), it need not provide either function. | |
7063fbf2 JB |
207 | */ |
208 | struct configfs_item_operations { | |
209 | void (*release)(struct config_item *); | |
7063fbf2 | 210 | int (*allow_link)(struct config_item *src, struct config_item *target); |
e16769d4 | 211 | void (*drop_link)(struct config_item *src, struct config_item *target); |
7063fbf2 JB |
212 | }; |
213 | ||
214 | struct configfs_group_operations { | |
f89ab861 JB |
215 | struct config_item *(*make_item)(struct config_group *group, const char *name); |
216 | struct config_group *(*make_group)(struct config_group *group, const char *name); | |
299894cc | 217 | void (*disconnect_notify)(struct config_group *group, struct config_item *item); |
7063fbf2 JB |
218 | void (*drop_item)(struct config_group *group, struct config_item *item); |
219 | }; | |
220 | ||
7063fbf2 JB |
221 | struct configfs_subsystem { |
222 | struct config_group su_group; | |
e6bd07ae | 223 | struct mutex su_mutex; |
7063fbf2 JB |
224 | }; |
225 | ||
226 | static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group) | |
227 | { | |
228 | return group ? | |
229 | container_of(group, struct configfs_subsystem, su_group) : | |
230 | NULL; | |
231 | } | |
232 | ||
233 | int configfs_register_subsystem(struct configfs_subsystem *subsys); | |
234 | void configfs_unregister_subsystem(struct configfs_subsystem *subsys); | |
235 | ||
5cf6a51e DB |
236 | int configfs_register_group(struct config_group *parent_group, |
237 | struct config_group *group); | |
238 | void configfs_unregister_group(struct config_group *group); | |
239 | ||
1ae1602d CH |
240 | void configfs_remove_default_groups(struct config_group *group); |
241 | ||
5cf6a51e DB |
242 | struct config_group * |
243 | configfs_register_default_group(struct config_group *parent_group, | |
244 | const char *name, | |
aa293583 | 245 | const struct config_item_type *item_type); |
5cf6a51e DB |
246 | void configfs_unregister_default_group(struct config_group *group); |
247 | ||
631d1feb JB |
248 | /* These functions can sleep and can alloc with GFP_KERNEL */ |
249 | /* WARNING: These cannot be called underneath configfs callbacks!! */ | |
9a9e3415 KO |
250 | int configfs_depend_item(struct configfs_subsystem *subsys, |
251 | struct config_item *target); | |
252 | void configfs_undepend_item(struct config_item *target); | |
631d1feb | 253 | |
d79d75b5 KO |
254 | /* |
255 | * These functions can sleep and can alloc with GFP_KERNEL | |
256 | * NOTE: These should be called only underneath configfs callbacks. | |
257 | * NOTE: First parameter is a caller's subsystem, not target's. | |
258 | * WARNING: These cannot be called on newly created item | |
259 | * (in make_group()/make_item() callback) | |
260 | */ | |
261 | int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys, | |
262 | struct config_item *target); | |
263 | ||
264 | ||
265 | static inline void configfs_undepend_item_unlocked(struct config_item *target) | |
266 | { | |
267 | configfs_undepend_item(target); | |
268 | } | |
631d1feb | 269 | |
7063fbf2 | 270 | #endif /* _CONFIGFS_H_ */ |