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:
5 * configfs.h - definitions for the device driver filesystem
8 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
11 * Copyright (c) 2002-2003 Patrick Mochel
12 * Copyright (c) 2002-2003 Open Source Development Labs
14 * configfs Copyright (C) 2005 Oracle. All rights reserved.
16 * Please read Documentation/filesystems/configfs/configfs.txt before using
17 * the configfs interface, ESPECIALLY the parts about reference counts and
24 #include <linux/stat.h> /* S_IRUGO */
25 #include <linux/types.h> /* ssize_t */
26 #include <linux/list.h> /* struct list_head */
27 #include <linux/kref.h> /* struct kref */
28 #include <linux/mutex.h> /* struct mutex */
30 #define CONFIGFS_ITEM_NAME_LEN 20
34 struct configfs_item_operations
;
35 struct configfs_group_operations
;
36 struct configfs_attribute
;
37 struct configfs_bin_attribute
;
38 struct configfs_subsystem
;
42 char ci_namebuf
[CONFIGFS_ITEM_NAME_LEN
];
44 struct list_head ci_entry
;
45 struct config_item
*ci_parent
;
46 struct config_group
*ci_group
;
47 const struct config_item_type
*ci_type
;
48 struct dentry
*ci_dentry
;
52 int config_item_set_name(struct config_item
*, const char *, ...);
54 static inline char *config_item_name(struct config_item
* item
)
59 extern void config_item_init_type_name(struct config_item
*item
,
61 const struct config_item_type
*type
);
63 extern struct config_item
*config_item_get(struct config_item
*);
64 extern struct config_item
*config_item_get_unless_zero(struct config_item
*);
65 extern void config_item_put(struct config_item
*);
67 struct config_item_type
{
68 struct module
*ct_owner
;
69 struct configfs_item_operations
*ct_item_ops
;
70 struct configfs_group_operations
*ct_group_ops
;
71 struct configfs_attribute
**ct_attrs
;
72 struct configfs_bin_attribute
**ct_bin_attrs
;
76 * group - a group of config_items of a specific type, belonging
77 * to a specific subsystem.
80 struct config_item cg_item
;
81 struct list_head cg_children
;
82 struct configfs_subsystem
*cg_subsys
;
83 struct list_head default_groups
;
84 struct list_head group_entry
;
87 extern void config_group_init(struct config_group
*group
);
88 extern void config_group_init_type_name(struct config_group
*group
,
90 const struct config_item_type
*type
);
92 static inline struct config_group
*to_config_group(struct config_item
*item
)
94 return item
? container_of(item
,struct config_group
,cg_item
) : NULL
;
97 static inline struct config_group
*config_group_get(struct config_group
*group
)
99 return group
? to_config_group(config_item_get(&group
->cg_item
)) : NULL
;
102 static inline void config_group_put(struct config_group
*group
)
104 config_item_put(&group
->cg_item
);
107 extern struct config_item
*config_group_find_item(struct config_group
*,
111 static inline void configfs_add_default_group(struct config_group
*new_group
,
112 struct config_group
*group
)
114 list_add_tail(&new_group
->group_entry
, &group
->default_groups
);
117 struct configfs_attribute
{
119 struct module
*ca_owner
;
121 ssize_t (*show
)(struct config_item
*, char *);
122 ssize_t (*store
)(struct config_item
*, const char *, size_t);
125 #define CONFIGFS_ATTR(_pfx, _name) \
126 static struct configfs_attribute _pfx##attr_##_name = { \
127 .ca_name = __stringify(_name), \
128 .ca_mode = S_IRUGO | S_IWUSR, \
129 .ca_owner = THIS_MODULE, \
130 .show = _pfx##_name##_show, \
131 .store = _pfx##_name##_store, \
134 #define CONFIGFS_ATTR_RO(_pfx, _name) \
135 static struct configfs_attribute _pfx##attr_##_name = { \
136 .ca_name = __stringify(_name), \
137 .ca_mode = S_IRUGO, \
138 .ca_owner = THIS_MODULE, \
139 .show = _pfx##_name##_show, \
142 #define CONFIGFS_ATTR_WO(_pfx, _name) \
143 static struct configfs_attribute _pfx##attr_##_name = { \
144 .ca_name = __stringify(_name), \
145 .ca_mode = S_IWUSR, \
146 .ca_owner = THIS_MODULE, \
147 .store = _pfx##_name##_store, \
151 struct vm_area_struct
;
153 struct configfs_bin_attribute
{
154 struct configfs_attribute cb_attr
; /* std. attribute */
155 void *cb_private
; /* for user */
156 size_t cb_max_size
; /* max core size */
157 ssize_t (*read
)(struct config_item
*, void *, size_t);
158 ssize_t (*write
)(struct config_item
*, const void *, size_t);
161 #define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz) \
162 static struct configfs_bin_attribute _pfx##attr_##_name = { \
164 .ca_name = __stringify(_name), \
165 .ca_mode = S_IRUGO | S_IWUSR, \
166 .ca_owner = THIS_MODULE, \
168 .cb_private = _priv, \
169 .cb_max_size = _maxsz, \
170 .read = _pfx##_name##_read, \
171 .write = _pfx##_name##_write, \
174 #define CONFIGFS_BIN_ATTR_RO(_pfx, _name, _priv, _maxsz) \
175 static struct configfs_bin_attribute _pfx##attr_##_name = { \
177 .ca_name = __stringify(_name), \
178 .ca_mode = S_IRUGO, \
179 .ca_owner = THIS_MODULE, \
181 .cb_private = _priv, \
182 .cb_max_size = _maxsz, \
183 .read = _pfx##_name##_read, \
186 #define CONFIGFS_BIN_ATTR_WO(_pfx, _name, _priv, _maxsz) \
187 static struct configfs_bin_attribute _pfx##attr_##_name = { \
189 .ca_name = __stringify(_name), \
190 .ca_mode = S_IWUSR, \
191 .ca_owner = THIS_MODULE, \
193 .cb_private = _priv, \
194 .cb_max_size = _maxsz, \
195 .write = _pfx##_name##_write, \
199 * If allow_link() exists, the item can symlink(2) out to other
200 * items. If the item is a group, it may support mkdir(2).
201 * Groups supply one of make_group() and make_item(). If the
202 * group supports make_group(), one can create group children. If it
203 * supports make_item(), one can create config_item children. make_group()
204 * and make_item() return ERR_PTR() on errors. If it has
205 * default_groups on group->default_groups, it has automatically created
206 * group children. default_groups may coexist alongsize make_group() or
207 * make_item(), but if the group wishes to have only default_groups
208 * children (disallowing mkdir(2)), it need not provide either function.
209 * If the group has commit(), it supports pending and committed (active)
212 struct configfs_item_operations
{
213 void (*release
)(struct config_item
*);
214 int (*allow_link
)(struct config_item
*src
, struct config_item
*target
);
215 void (*drop_link
)(struct config_item
*src
, struct config_item
*target
);
218 struct configfs_group_operations
{
219 struct config_item
*(*make_item
)(struct config_group
*group
, const char *name
);
220 struct config_group
*(*make_group
)(struct config_group
*group
, const char *name
);
221 int (*commit_item
)(struct config_item
*item
);
222 void (*disconnect_notify
)(struct config_group
*group
, struct config_item
*item
);
223 void (*drop_item
)(struct config_group
*group
, struct config_item
*item
);
226 struct configfs_subsystem
{
227 struct config_group su_group
;
228 struct mutex su_mutex
;
231 static inline struct configfs_subsystem
*to_configfs_subsystem(struct config_group
*group
)
234 container_of(group
, struct configfs_subsystem
, su_group
) :
238 int configfs_register_subsystem(struct configfs_subsystem
*subsys
);
239 void configfs_unregister_subsystem(struct configfs_subsystem
*subsys
);
241 int configfs_register_group(struct config_group
*parent_group
,
242 struct config_group
*group
);
243 void configfs_unregister_group(struct config_group
*group
);
245 void configfs_remove_default_groups(struct config_group
*group
);
247 struct config_group
*
248 configfs_register_default_group(struct config_group
*parent_group
,
250 const struct config_item_type
*item_type
);
251 void configfs_unregister_default_group(struct config_group
*group
);
253 /* These functions can sleep and can alloc with GFP_KERNEL */
254 /* WARNING: These cannot be called underneath configfs callbacks!! */
255 int configfs_depend_item(struct configfs_subsystem
*subsys
,
256 struct config_item
*target
);
257 void configfs_undepend_item(struct config_item
*target
);
260 * These functions can sleep and can alloc with GFP_KERNEL
261 * NOTE: These should be called only underneath configfs callbacks.
262 * NOTE: First parameter is a caller's subsystem, not target's.
263 * WARNING: These cannot be called on newly created item
264 * (in make_group()/make_item() callback)
266 int configfs_depend_item_unlocked(struct configfs_subsystem
*caller_subsys
,
267 struct config_item
*target
);
270 static inline void configfs_undepend_item_unlocked(struct config_item
*target
)
272 configfs_undepend_item(target
);
275 #endif /* _CONFIGFS_H_ */