blob: 2319b8c108e87b9e87c11cc4c9aa314d24eb0364 [file] [log] [blame]
Joel Becker7063fbf2005-12-15 14:29:43 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * configfs.h - definitions for the device driver filesystem
5 *
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.
10 *
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.
15 *
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.
20 *
21 * Based on sysfs:
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
23 *
24 * Based on kobject.h:
25 * Copyright (c) 2002-2003 Patrick Mochel
26 * Copyright (c) 2002-2003 Open Source Development Labs
27 *
28 * configfs Copyright (C) 2005 Oracle. All rights reserved.
29 *
Randy Dunlap55dff492009-09-23 15:56:17 -070030 * Please read Documentation/filesystems/configfs/configfs.txt before using
31 * the configfs interface, ESPECIALLY the parts about reference counts and
Joel Becker7063fbf2005-12-15 14:29:43 -080032 * item destructors.
33 */
34
35#ifndef _CONFIGFS_H_
36#define _CONFIGFS_H_
37
Bart Van Asschea6ab5372016-11-14 15:53:05 -080038#include <linux/stat.h> /* S_IRUGO */
39#include <linux/types.h> /* ssize_t */
40#include <linux/list.h> /* struct list_head */
41#include <linux/kref.h> /* struct kref */
42#include <linux/mutex.h> /* struct mutex */
Joel Becker7063fbf2005-12-15 14:29:43 -080043
44#define CONFIGFS_ITEM_NAME_LEN 20
45
46struct module;
47
48struct configfs_item_operations;
49struct configfs_group_operations;
50struct configfs_attribute;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +030051struct configfs_bin_attribute;
Joel Becker7063fbf2005-12-15 14:29:43 -080052struct configfs_subsystem;
53
54struct config_item {
55 char *ci_name;
56 char ci_namebuf[CONFIGFS_ITEM_NAME_LEN];
57 struct kref ci_kref;
58 struct list_head ci_entry;
59 struct config_item *ci_parent;
60 struct config_group *ci_group;
61 struct config_item_type *ci_type;
62 struct dentry *ci_dentry;
63};
64
Nicolas Iooss8db14862015-07-17 16:23:42 -070065extern __printf(2, 3)
66int config_item_set_name(struct config_item *, const char *, ...);
Joel Becker7063fbf2005-12-15 14:29:43 -080067
68static inline char *config_item_name(struct config_item * item)
69{
70 return item->ci_name;
71}
72
Joel Becker7063fbf2005-12-15 14:29:43 -080073extern void config_item_init_type_name(struct config_item *item,
74 const char *name,
75 struct config_item_type *type);
Joel Becker7063fbf2005-12-15 14:29:43 -080076
77extern struct config_item * config_item_get(struct config_item *);
78extern void config_item_put(struct config_item *);
79
80struct config_item_type {
81 struct module *ct_owner;
82 struct configfs_item_operations *ct_item_ops;
83 struct configfs_group_operations *ct_group_ops;
84 struct configfs_attribute **ct_attrs;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +030085 struct configfs_bin_attribute **ct_bin_attrs;
Joel Becker7063fbf2005-12-15 14:29:43 -080086};
87
Joel Becker7063fbf2005-12-15 14:29:43 -080088/**
89 * group - a group of config_items of a specific type, belonging
90 * to a specific subsystem.
91 */
Joel Becker7063fbf2005-12-15 14:29:43 -080092struct config_group {
93 struct config_item cg_item;
94 struct list_head cg_children;
95 struct configfs_subsystem *cg_subsys;
Christoph Hellwig1ae16022016-02-26 11:02:14 +010096 struct list_head default_groups;
97 struct list_head group_entry;
Joel Becker7063fbf2005-12-15 14:29:43 -080098};
99
Joel Becker7063fbf2005-12-15 14:29:43 -0800100extern void config_group_init(struct config_group *group);
101extern void config_group_init_type_name(struct config_group *group,
102 const char *name,
103 struct config_item_type *type);
104
Joel Becker7063fbf2005-12-15 14:29:43 -0800105static inline struct config_group *to_config_group(struct config_item *item)
106{
107 return item ? container_of(item,struct config_group,cg_item) : NULL;
108}
109
110static inline struct config_group *config_group_get(struct config_group *group)
111{
112 return group ? to_config_group(config_item_get(&group->cg_item)) : NULL;
113}
114
115static inline void config_group_put(struct config_group *group)
116{
117 config_item_put(&group->cg_item);
118}
119
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530120extern struct config_item *config_group_find_item(struct config_group *,
121 const char *);
Joel Becker7063fbf2005-12-15 14:29:43 -0800122
123
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100124static inline void configfs_add_default_group(struct config_group *new_group,
125 struct config_group *group)
126{
127 list_add_tail(&new_group->group_entry, &group->default_groups);
128}
129
Joel Becker7063fbf2005-12-15 14:29:43 -0800130struct configfs_attribute {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800131 const char *ca_name;
Joel Becker7063fbf2005-12-15 14:29:43 -0800132 struct module *ca_owner;
Al Viro43947512011-07-25 00:05:26 -0400133 umode_t ca_mode;
Christoph Hellwig870823e2015-10-03 15:32:37 +0200134 ssize_t (*show)(struct config_item *, char *);
135 ssize_t (*store)(struct config_item *, const char *, size_t);
Joel Becker7063fbf2005-12-15 14:29:43 -0800136};
137
Christoph Hellwig870823e2015-10-03 15:32:37 +0200138#define CONFIGFS_ATTR(_pfx, _name) \
139static struct configfs_attribute _pfx##attr_##_name = { \
140 .ca_name = __stringify(_name), \
141 .ca_mode = S_IRUGO | S_IWUSR, \
142 .ca_owner = THIS_MODULE, \
143 .show = _pfx##_name##_show, \
144 .store = _pfx##_name##_store, \
145}
146
147#define CONFIGFS_ATTR_RO(_pfx, _name) \
148static struct configfs_attribute _pfx##attr_##_name = { \
149 .ca_name = __stringify(_name), \
150 .ca_mode = S_IRUGO, \
151 .ca_owner = THIS_MODULE, \
152 .show = _pfx##_name##_show, \
153}
154
155#define CONFIGFS_ATTR_WO(_pfx, _name) \
156static struct configfs_attribute _pfx##attr_##_name = { \
157 .ca_name = __stringify(_name), \
158 .ca_mode = S_IWUSR, \
159 .ca_owner = THIS_MODULE, \
160 .store = _pfx##_name##_store, \
161}
162
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300163struct file;
164struct vm_area_struct;
165
166struct configfs_bin_attribute {
167 struct configfs_attribute cb_attr; /* std. attribute */
168 void *cb_private; /* for user */
169 size_t cb_max_size; /* max core size */
170 ssize_t (*read)(struct config_item *, void *, size_t);
171 ssize_t (*write)(struct config_item *, const void *, size_t);
172};
173
174#define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz) \
175static struct configfs_bin_attribute _pfx##attr_##_name = { \
176 .cb_attr = { \
177 .ca_name = __stringify(_name), \
178 .ca_mode = S_IRUGO | S_IWUSR, \
179 .ca_owner = THIS_MODULE, \
180 }, \
181 .cb_private = _priv, \
182 .cb_max_size = _maxsz, \
183 .read = _pfx##_name##_read, \
184 .write = _pfx##_name##_write, \
185}
186
187#define CONFIGFS_BIN_ATTR_RO(_pfx, _name, _priv, _maxsz) \
Octavian Purdila96c22a322016-03-23 14:14:48 +0200188static struct configfs_bin_attribute _pfx##attr_##_name = { \
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300189 .cb_attr = { \
190 .ca_name = __stringify(_name), \
191 .ca_mode = S_IRUGO, \
192 .ca_owner = THIS_MODULE, \
193 }, \
194 .cb_private = _priv, \
195 .cb_max_size = _maxsz, \
196 .read = _pfx##_name##_read, \
197}
198
199#define CONFIGFS_BIN_ATTR_WO(_pfx, _name, _priv, _maxsz) \
Octavian Purdila96c22a322016-03-23 14:14:48 +0200200static struct configfs_bin_attribute _pfx##attr_##_name = { \
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300201 .cb_attr = { \
202 .ca_name = __stringify(_name), \
203 .ca_mode = S_IWUSR, \
204 .ca_owner = THIS_MODULE, \
205 }, \
206 .cb_private = _priv, \
207 .cb_max_size = _maxsz, \
208 .write = _pfx##_name##_write, \
209}
210
Satyam Sharma9b1d9aa2007-07-04 16:37:06 +0530211/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800212 * If allow_link() exists, the item can symlink(2) out to other
213 * items. If the item is a group, it may support mkdir(2).
214 * Groups supply one of make_group() and make_item(). If the
215 * group supports make_group(), one can create group children. If it
Joel Beckera6795e92008-07-17 15:21:29 -0700216 * supports make_item(), one can create config_item children. make_group()
217 * and make_item() return ERR_PTR() on errors. If it has
Joel Becker7063fbf2005-12-15 14:29:43 -0800218 * default_groups on group->default_groups, it has automatically created
219 * group children. default_groups may coexist alongsize make_group() or
220 * make_item(), but if the group wishes to have only default_groups
221 * children (disallowing mkdir(2)), it need not provide either function.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300222 * If the group has commit(), it supports pending and committed (active)
Joel Becker7063fbf2005-12-15 14:29:43 -0800223 * items.
224 */
225struct configfs_item_operations {
226 void (*release)(struct config_item *);
Joel Becker7063fbf2005-12-15 14:29:43 -0800227 int (*allow_link)(struct config_item *src, struct config_item *target);
Andrzej Pietrasiewicze16769d2016-11-28 13:22:42 +0100228 void (*drop_link)(struct config_item *src, struct config_item *target);
Joel Becker7063fbf2005-12-15 14:29:43 -0800229};
230
231struct configfs_group_operations {
Joel Beckerf89ab862008-07-17 14:53:48 -0700232 struct config_item *(*make_item)(struct config_group *group, const char *name);
233 struct config_group *(*make_group)(struct config_group *group, const char *name);
Joel Becker7063fbf2005-12-15 14:29:43 -0800234 int (*commit_item)(struct config_item *item);
Joel Becker299894c2006-10-06 17:33:23 -0700235 void (*disconnect_notify)(struct config_group *group, struct config_item *item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800236 void (*drop_item)(struct config_group *group, struct config_item *item);
237};
238
Joel Becker7063fbf2005-12-15 14:29:43 -0800239struct configfs_subsystem {
240 struct config_group su_group;
Joel Beckere6bd07a2007-07-06 23:33:17 -0700241 struct mutex su_mutex;
Joel Becker7063fbf2005-12-15 14:29:43 -0800242};
243
244static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group)
245{
246 return group ?
247 container_of(group, struct configfs_subsystem, su_group) :
248 NULL;
249}
250
251int configfs_register_subsystem(struct configfs_subsystem *subsys);
252void configfs_unregister_subsystem(struct configfs_subsystem *subsys);
253
Daniel Baluta5cf6a512015-11-20 15:56:53 -0800254int configfs_register_group(struct config_group *parent_group,
255 struct config_group *group);
256void configfs_unregister_group(struct config_group *group);
257
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100258void configfs_remove_default_groups(struct config_group *group);
259
Daniel Baluta5cf6a512015-11-20 15:56:53 -0800260struct config_group *
261configfs_register_default_group(struct config_group *parent_group,
262 const char *name,
263 struct config_item_type *item_type);
264void configfs_unregister_default_group(struct config_group *group);
265
Joel Becker631d1fe2007-06-18 18:06:09 -0700266/* These functions can sleep and can alloc with GFP_KERNEL */
267/* WARNING: These cannot be called underneath configfs callbacks!! */
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +0100268int configfs_depend_item(struct configfs_subsystem *subsys,
269 struct config_item *target);
270void configfs_undepend_item(struct config_item *target);
Joel Becker631d1fe2007-06-18 18:06:09 -0700271
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +0100272/*
273 * These functions can sleep and can alloc with GFP_KERNEL
274 * NOTE: These should be called only underneath configfs callbacks.
275 * NOTE: First parameter is a caller's subsystem, not target's.
276 * WARNING: These cannot be called on newly created item
277 * (in make_group()/make_item() callback)
278 */
279int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
280 struct config_item *target);
281
282
283static inline void configfs_undepend_item_unlocked(struct config_item *target)
284{
285 configfs_undepend_item(target);
286}
Joel Becker7063fbf2005-12-15 14:29:43 -0800287
Joel Becker7063fbf2005-12-15 14:29:43 -0800288#endif /* _CONFIGFS_H_ */