1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * configfs.h - definitions for the device driver filesystem
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.
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.
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.
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
25 * Copyright (c) 2002-2003 Patrick Mochel
26 * Copyright (c) 2002-2003 Open Source Development Labs
28 * configfs Copyright (C) 2005 Oracle. All rights reserved.
30 * Please read Documentation/filesystems/configfs/configfs.txt before using
31 * the configfs interface, ESPECIALLY the parts about reference counts and
38 #include <linux/kernel.h>
39 #include <linux/types.h>
40 #include <linux/list.h>
41 #include <linux/kref.h>
42 #include <linux/mutex.h>
43 #include <linux/err.h>
45 #include <linux/atomic.h>
47 #define CONFIGFS_ITEM_NAME_LEN 20
51 struct configfs_item_operations;
52 struct configfs_group_operations;
53 struct configfs_attribute;
54 struct configfs_subsystem;
58 char ci_namebuf[CONFIGFS_ITEM_NAME_LEN];
60 struct list_head ci_entry;
61 struct config_item *ci_parent;
62 struct config_group *ci_group;
63 struct config_item_type *ci_type;
64 struct dentry *ci_dentry;
68 int config_item_set_name(struct config_item *, const char *, ...);
70 static inline char *config_item_name(struct config_item * item)
75 extern void config_item_init_type_name(struct config_item *item,
77 struct config_item_type *type);
79 extern struct config_item * config_item_get(struct config_item *);
80 extern void config_item_put(struct config_item *);
82 struct config_item_type {
83 struct module *ct_owner;
84 struct configfs_item_operations *ct_item_ops;
85 struct configfs_group_operations *ct_group_ops;
86 struct configfs_attribute **ct_attrs;
90 * group - a group of config_items of a specific type, belonging
91 * to a specific subsystem.
94 struct config_item cg_item;
95 struct list_head cg_children;
96 struct configfs_subsystem *cg_subsys;
97 struct config_group **default_groups;
100 extern void config_group_init(struct config_group *group);
101 extern void config_group_init_type_name(struct config_group *group,
103 struct config_item_type *type);
105 static inline struct config_group *to_config_group(struct config_item *item)
107 return item ? container_of(item,struct config_group,cg_item) : NULL;
110 static inline struct config_group *config_group_get(struct config_group *group)
112 return group ? to_config_group(config_item_get(&group->cg_item)) : NULL;
115 static inline void config_group_put(struct config_group *group)
117 config_item_put(&group->cg_item);
120 extern struct config_item *config_group_find_item(struct config_group *,
124 struct configfs_attribute {
126 struct module *ca_owner;
128 ssize_t (*show)(struct config_item *, char *);
129 ssize_t (*store)(struct config_item *, const char *, size_t);
132 #define CONFIGFS_ATTR(_pfx, _name) \
133 static struct configfs_attribute _pfx##attr_##_name = { \
134 .ca_name = __stringify(_name), \
135 .ca_mode = S_IRUGO | S_IWUSR, \
136 .ca_owner = THIS_MODULE, \
137 .show = _pfx##_name##_show, \
138 .store = _pfx##_name##_store, \
141 #define CONFIGFS_ATTR_RO(_pfx, _name) \
142 static struct configfs_attribute _pfx##attr_##_name = { \
143 .ca_name = __stringify(_name), \
144 .ca_mode = S_IRUGO, \
145 .ca_owner = THIS_MODULE, \
146 .show = _pfx##_name##_show, \
149 #define CONFIGFS_ATTR_WO(_pfx, _name) \
150 static struct configfs_attribute _pfx##attr_##_name = { \
151 .ca_name = __stringify(_name), \
152 .ca_mode = S_IWUSR, \
153 .ca_owner = THIS_MODULE, \
154 .store = _pfx##_name##_store, \
158 * Users often need to create attribute structures for their configurable
159 * attributes, containing a configfs_attribute member and function pointers
160 * for the show() and store() operations on that attribute. If they don't
161 * need anything else on the extended attribute structure, they can use
162 * this macro to define it The argument _item is the name of the
163 * config_item structure.
165 #define CONFIGFS_ATTR_STRUCT(_item) \
166 struct _item##_attribute { \
167 struct configfs_attribute attr; \
168 ssize_t (*show)(struct _item *, char *); \
169 ssize_t (*store)(struct _item *, const char *, size_t); \
173 * With the extended attribute structure, users can use this macro
174 * (similar to sysfs' __ATTR) to make defining attributes easier.
176 * #define MYITEM_ATTR(_name, _mode, _show, _store) \
177 * struct myitem_attribute childless_attr_##_name = \
178 * __CONFIGFS_ATTR(_name, _mode, _show, _store)
180 #define __CONFIGFS_ATTR(_name, _mode, _show, _store) \
183 .ca_name = __stringify(_name), \
185 .ca_owner = THIS_MODULE, \
190 /* Here is a readonly version, only requiring a show() operation */
191 #define __CONFIGFS_ATTR_RO(_name, _show) \
194 .ca_name = __stringify(_name), \
196 .ca_owner = THIS_MODULE, \
202 * With these extended attributes, the simple show_attribute() and
203 * store_attribute() operations need to call the show() and store() of the
204 * attributes. This is a common pattern, so we provide a macro to define
205 * them. The argument _item is the name of the config_item structure.
206 * This macro expects the attributes to be named "struct <name>_attribute"
207 * and the function to_<name>() to exist;
209 #define CONFIGFS_ATTR_OPS(_item) \
210 static ssize_t _item##_attr_show(struct config_item *item, \
211 struct configfs_attribute *attr, \
214 struct _item *_item = to_##_item(item); \
215 struct _item##_attribute *_item##_attr = \
216 container_of(attr, struct _item##_attribute, attr); \
219 if (_item##_attr->show) \
220 ret = _item##_attr->show(_item, page); \
223 static ssize_t _item##_attr_store(struct config_item *item, \
224 struct configfs_attribute *attr, \
225 const char *page, size_t count) \
227 struct _item *_item = to_##_item(item); \
228 struct _item##_attribute *_item##_attr = \
229 container_of(attr, struct _item##_attribute, attr); \
230 ssize_t ret = -EINVAL; \
232 if (_item##_attr->store) \
233 ret = _item##_attr->store(_item, page, count); \
238 * If allow_link() exists, the item can symlink(2) out to other
239 * items. If the item is a group, it may support mkdir(2).
240 * Groups supply one of make_group() and make_item(). If the
241 * group supports make_group(), one can create group children. If it
242 * supports make_item(), one can create config_item children. make_group()
243 * and make_item() return ERR_PTR() on errors. If it has
244 * default_groups on group->default_groups, it has automatically created
245 * group children. default_groups may coexist alongsize make_group() or
246 * make_item(), but if the group wishes to have only default_groups
247 * children (disallowing mkdir(2)), it need not provide either function.
248 * If the group has commit(), it supports pending and committed (active)
251 struct configfs_item_operations {
252 void (*release)(struct config_item *);
253 ssize_t (*show_attribute)(struct config_item *, struct configfs_attribute *,char *);
254 ssize_t (*store_attribute)(struct config_item *,struct configfs_attribute *,const char *, size_t);
255 int (*allow_link)(struct config_item *src, struct config_item *target);
256 int (*drop_link)(struct config_item *src, struct config_item *target);
259 struct configfs_group_operations {
260 struct config_item *(*make_item)(struct config_group *group, const char *name);
261 struct config_group *(*make_group)(struct config_group *group, const char *name);
262 int (*commit_item)(struct config_item *item);
263 void (*disconnect_notify)(struct config_group *group, struct config_item *item);
264 void (*drop_item)(struct config_group *group, struct config_item *item);
267 struct configfs_subsystem {
268 struct config_group su_group;
269 struct mutex su_mutex;
272 static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group)
275 container_of(group, struct configfs_subsystem, su_group) :
279 int configfs_register_subsystem(struct configfs_subsystem *subsys);
280 void configfs_unregister_subsystem(struct configfs_subsystem *subsys);
282 /* These functions can sleep and can alloc with GFP_KERNEL */
283 /* WARNING: These cannot be called underneath configfs callbacks!! */
284 int configfs_depend_item(struct configfs_subsystem *subsys, struct config_item *target);
285 void configfs_undepend_item(struct configfs_subsystem *subsys, struct config_item *target);
287 #endif /* _CONFIGFS_H_ */