blob: fa9490a8874c8212987727e17186c9faf6950ac1 [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Joel Becker7063fbf2005-12-15 14:29:43 -08002/* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
4 *
5 * configfs.h - definitions for the device driver filesystem
6 *
Joel Becker7063fbf2005-12-15 14:29:43 -08007 * Based on sysfs:
8 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
9 *
10 * Based on kobject.h:
11 * Copyright (c) 2002-2003 Patrick Mochel
12 * Copyright (c) 2002-2003 Open Source Development Labs
13 *
14 * configfs Copyright (C) 2005 Oracle. All rights reserved.
15 *
Randy Dunlap55dff492009-09-23 15:56:17 -070016 * Please read Documentation/filesystems/configfs/configfs.txt before using
17 * the configfs interface, ESPECIALLY the parts about reference counts and
Joel Becker7063fbf2005-12-15 14:29:43 -080018 * item destructors.
19 */
20
21#ifndef _CONFIGFS_H_
22#define _CONFIGFS_H_
23
Bart Van Asschea6ab5372016-11-14 15:53:05 -080024#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 */
Joel Becker7063fbf2005-12-15 14:29:43 -080029
30#define CONFIGFS_ITEM_NAME_LEN 20
31
32struct module;
33
34struct configfs_item_operations;
35struct configfs_group_operations;
36struct configfs_attribute;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +030037struct configfs_bin_attribute;
Joel Becker7063fbf2005-12-15 14:29:43 -080038struct configfs_subsystem;
39
40struct config_item {
41 char *ci_name;
42 char ci_namebuf[CONFIGFS_ITEM_NAME_LEN];
43 struct kref ci_kref;
44 struct list_head ci_entry;
45 struct config_item *ci_parent;
46 struct config_group *ci_group;
Bhumika Goyalaa293582017-10-16 17:18:40 +020047 const struct config_item_type *ci_type;
Joel Becker7063fbf2005-12-15 14:29:43 -080048 struct dentry *ci_dentry;
49};
50
Nicolas Iooss8db14862015-07-17 16:23:42 -070051extern __printf(2, 3)
52int config_item_set_name(struct config_item *, const char *, ...);
Joel Becker7063fbf2005-12-15 14:29:43 -080053
54static inline char *config_item_name(struct config_item * item)
55{
56 return item->ci_name;
57}
58
Joel Becker7063fbf2005-12-15 14:29:43 -080059extern void config_item_init_type_name(struct config_item *item,
60 const char *name,
Bhumika Goyalaa293582017-10-16 17:18:40 +020061 const struct config_item_type *type);
Joel Becker7063fbf2005-12-15 14:29:43 -080062
Bart Van Assche19e72d32017-02-09 17:28:50 -080063extern struct config_item *config_item_get(struct config_item *);
64extern struct config_item *config_item_get_unless_zero(struct config_item *);
Joel Becker7063fbf2005-12-15 14:29:43 -080065extern void config_item_put(struct config_item *);
66
67struct 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;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +030072 struct configfs_bin_attribute **ct_bin_attrs;
Joel Becker7063fbf2005-12-15 14:29:43 -080073};
74
Joel Becker7063fbf2005-12-15 14:29:43 -080075/**
76 * group - a group of config_items of a specific type, belonging
77 * to a specific subsystem.
78 */
Joel Becker7063fbf2005-12-15 14:29:43 -080079struct config_group {
80 struct config_item cg_item;
81 struct list_head cg_children;
82 struct configfs_subsystem *cg_subsys;
Christoph Hellwig1ae16022016-02-26 11:02:14 +010083 struct list_head default_groups;
84 struct list_head group_entry;
Joel Becker7063fbf2005-12-15 14:29:43 -080085};
86
Joel Becker7063fbf2005-12-15 14:29:43 -080087extern void config_group_init(struct config_group *group);
88extern void config_group_init_type_name(struct config_group *group,
89 const char *name,
Bhumika Goyalaa293582017-10-16 17:18:40 +020090 const struct config_item_type *type);
Joel Becker7063fbf2005-12-15 14:29:43 -080091
Joel Becker7063fbf2005-12-15 14:29:43 -080092static inline struct config_group *to_config_group(struct config_item *item)
93{
94 return item ? container_of(item,struct config_group,cg_item) : NULL;
95}
96
97static inline struct config_group *config_group_get(struct config_group *group)
98{
99 return group ? to_config_group(config_item_get(&group->cg_item)) : NULL;
100}
101
102static inline void config_group_put(struct config_group *group)
103{
104 config_item_put(&group->cg_item);
105}
106
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530107extern struct config_item *config_group_find_item(struct config_group *,
108 const char *);
Joel Becker7063fbf2005-12-15 14:29:43 -0800109
110
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100111static inline void configfs_add_default_group(struct config_group *new_group,
112 struct config_group *group)
113{
114 list_add_tail(&new_group->group_entry, &group->default_groups);
115}
116
Joel Becker7063fbf2005-12-15 14:29:43 -0800117struct configfs_attribute {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800118 const char *ca_name;
Joel Becker7063fbf2005-12-15 14:29:43 -0800119 struct module *ca_owner;
Al Viro43947512011-07-25 00:05:26 -0400120 umode_t ca_mode;
Christoph Hellwig870823e2015-10-03 15:32:37 +0200121 ssize_t (*show)(struct config_item *, char *);
122 ssize_t (*store)(struct config_item *, const char *, size_t);
Joel Becker7063fbf2005-12-15 14:29:43 -0800123};
124
Christoph Hellwig870823e2015-10-03 15:32:37 +0200125#define CONFIGFS_ATTR(_pfx, _name) \
126static 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, \
132}
133
134#define CONFIGFS_ATTR_RO(_pfx, _name) \
135static 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, \
140}
141
142#define CONFIGFS_ATTR_WO(_pfx, _name) \
143static 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, \
148}
149
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300150struct file;
151struct vm_area_struct;
152
153struct 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);
159};
160
161#define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz) \
162static struct configfs_bin_attribute _pfx##attr_##_name = { \
163 .cb_attr = { \
164 .ca_name = __stringify(_name), \
165 .ca_mode = S_IRUGO | S_IWUSR, \
166 .ca_owner = THIS_MODULE, \
167 }, \
168 .cb_private = _priv, \
169 .cb_max_size = _maxsz, \
170 .read = _pfx##_name##_read, \
171 .write = _pfx##_name##_write, \
172}
173
174#define CONFIGFS_BIN_ATTR_RO(_pfx, _name, _priv, _maxsz) \
Octavian Purdila96c22a322016-03-23 14:14:48 +0200175static struct configfs_bin_attribute _pfx##attr_##_name = { \
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300176 .cb_attr = { \
177 .ca_name = __stringify(_name), \
178 .ca_mode = S_IRUGO, \
179 .ca_owner = THIS_MODULE, \
180 }, \
181 .cb_private = _priv, \
182 .cb_max_size = _maxsz, \
183 .read = _pfx##_name##_read, \
184}
185
186#define CONFIGFS_BIN_ATTR_WO(_pfx, _name, _priv, _maxsz) \
Octavian Purdila96c22a322016-03-23 14:14:48 +0200187static struct configfs_bin_attribute _pfx##attr_##_name = { \
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300188 .cb_attr = { \
189 .ca_name = __stringify(_name), \
190 .ca_mode = S_IWUSR, \
191 .ca_owner = THIS_MODULE, \
192 }, \
193 .cb_private = _priv, \
194 .cb_max_size = _maxsz, \
195 .write = _pfx##_name##_write, \
196}
197
Satyam Sharma9b1d9aa2007-07-04 16:37:06 +0530198/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800199 * 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
Joel Beckera6795e92008-07-17 15:21:29 -0700203 * supports make_item(), one can create config_item children. make_group()
204 * and make_item() return ERR_PTR() on errors. If it has
Joel Becker7063fbf2005-12-15 14:29:43 -0800205 * 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.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300209 * If the group has commit(), it supports pending and committed (active)
Joel Becker7063fbf2005-12-15 14:29:43 -0800210 * items.
211 */
212struct configfs_item_operations {
213 void (*release)(struct config_item *);
Joel Becker7063fbf2005-12-15 14:29:43 -0800214 int (*allow_link)(struct config_item *src, struct config_item *target);
Andrzej Pietrasiewicze16769d2016-11-28 13:22:42 +0100215 void (*drop_link)(struct config_item *src, struct config_item *target);
Joel Becker7063fbf2005-12-15 14:29:43 -0800216};
217
218struct configfs_group_operations {
Joel Beckerf89ab862008-07-17 14:53:48 -0700219 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);
Joel Becker7063fbf2005-12-15 14:29:43 -0800221 int (*commit_item)(struct config_item *item);
Joel Becker299894c2006-10-06 17:33:23 -0700222 void (*disconnect_notify)(struct config_group *group, struct config_item *item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800223 void (*drop_item)(struct config_group *group, struct config_item *item);
224};
225
Joel Becker7063fbf2005-12-15 14:29:43 -0800226struct configfs_subsystem {
227 struct config_group su_group;
Joel Beckere6bd07a2007-07-06 23:33:17 -0700228 struct mutex su_mutex;
Joel Becker7063fbf2005-12-15 14:29:43 -0800229};
230
231static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group)
232{
233 return group ?
234 container_of(group, struct configfs_subsystem, su_group) :
235 NULL;
236}
237
238int configfs_register_subsystem(struct configfs_subsystem *subsys);
239void configfs_unregister_subsystem(struct configfs_subsystem *subsys);
240
Daniel Baluta5cf6a512015-11-20 15:56:53 -0800241int configfs_register_group(struct config_group *parent_group,
242 struct config_group *group);
243void configfs_unregister_group(struct config_group *group);
244
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100245void configfs_remove_default_groups(struct config_group *group);
246
Daniel Baluta5cf6a512015-11-20 15:56:53 -0800247struct config_group *
248configfs_register_default_group(struct config_group *parent_group,
249 const char *name,
Bhumika Goyalaa293582017-10-16 17:18:40 +0200250 const struct config_item_type *item_type);
Daniel Baluta5cf6a512015-11-20 15:56:53 -0800251void configfs_unregister_default_group(struct config_group *group);
252
Joel Becker631d1fe2007-06-18 18:06:09 -0700253/* These functions can sleep and can alloc with GFP_KERNEL */
254/* WARNING: These cannot be called underneath configfs callbacks!! */
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +0100255int configfs_depend_item(struct configfs_subsystem *subsys,
256 struct config_item *target);
257void configfs_undepend_item(struct config_item *target);
Joel Becker631d1fe2007-06-18 18:06:09 -0700258
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +0100259/*
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)
265 */
266int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
267 struct config_item *target);
268
269
270static inline void configfs_undepend_item_unlocked(struct config_item *target)
271{
272 configfs_undepend_item(target);
273}
Joel Becker7063fbf2005-12-15 14:29:43 -0800274
Joel Becker7063fbf2005-12-15 14:29:43 -0800275#endif /* _CONFIGFS_H_ */