Thomas Gleixner | 328970d | 2019-05-24 12:04:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 2 | /* -*- 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 Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 7 | * 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 Dunlap | 55dff49 | 2009-09-23 15:56:17 -0700 | [diff] [blame] | 16 | * Please read Documentation/filesystems/configfs/configfs.txt before using |
| 17 | * the configfs interface, ESPECIALLY the parts about reference counts and |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 18 | * item destructors. |
| 19 | */ |
| 20 | |
| 21 | #ifndef _CONFIGFS_H_ |
| 22 | #define _CONFIGFS_H_ |
| 23 | |
Bart Van Assche | a6ab537 | 2016-11-14 15:53:05 -0800 | [diff] [blame] | 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 */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 29 | |
| 30 | #define CONFIGFS_ITEM_NAME_LEN 20 |
| 31 | |
| 32 | struct module; |
| 33 | |
| 34 | struct configfs_item_operations; |
| 35 | struct configfs_group_operations; |
| 36 | struct configfs_attribute; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 37 | struct configfs_bin_attribute; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 38 | struct configfs_subsystem; |
| 39 | |
| 40 | struct 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 Goyal | aa29358 | 2017-10-16 17:18:40 +0200 | [diff] [blame] | 47 | const struct config_item_type *ci_type; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 48 | struct dentry *ci_dentry; |
| 49 | }; |
| 50 | |
Nicolas Iooss | 8db1486 | 2015-07-17 16:23:42 -0700 | [diff] [blame] | 51 | extern __printf(2, 3) |
| 52 | int config_item_set_name(struct config_item *, const char *, ...); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 53 | |
| 54 | static inline char *config_item_name(struct config_item * item) |
| 55 | { |
| 56 | return item->ci_name; |
| 57 | } |
| 58 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 59 | extern void config_item_init_type_name(struct config_item *item, |
| 60 | const char *name, |
Bhumika Goyal | aa29358 | 2017-10-16 17:18:40 +0200 | [diff] [blame] | 61 | const struct config_item_type *type); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 62 | |
Bart Van Assche | 19e72d3 | 2017-02-09 17:28:50 -0800 | [diff] [blame] | 63 | extern struct config_item *config_item_get(struct config_item *); |
| 64 | extern struct config_item *config_item_get_unless_zero(struct config_item *); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 65 | extern void config_item_put(struct config_item *); |
| 66 | |
| 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; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 72 | struct configfs_bin_attribute **ct_bin_attrs; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 75 | /** |
| 76 | * group - a group of config_items of a specific type, belonging |
| 77 | * to a specific subsystem. |
| 78 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 79 | struct config_group { |
| 80 | struct config_item cg_item; |
| 81 | struct list_head cg_children; |
| 82 | struct configfs_subsystem *cg_subsys; |
Christoph Hellwig | 1ae1602 | 2016-02-26 11:02:14 +0100 | [diff] [blame] | 83 | struct list_head default_groups; |
| 84 | struct list_head group_entry; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 87 | extern void config_group_init(struct config_group *group); |
| 88 | extern void config_group_init_type_name(struct config_group *group, |
| 89 | const char *name, |
Bhumika Goyal | aa29358 | 2017-10-16 17:18:40 +0200 | [diff] [blame] | 90 | const struct config_item_type *type); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 91 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 92 | static 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 | |
| 97 | static 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 | |
| 102 | static inline void config_group_put(struct config_group *group) |
| 103 | { |
| 104 | config_item_put(&group->cg_item); |
| 105 | } |
| 106 | |
Satyam Sharma | 3fe6c5c | 2007-07-04 16:37:16 +0530 | [diff] [blame] | 107 | extern struct config_item *config_group_find_item(struct config_group *, |
| 108 | const char *); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 109 | |
| 110 | |
Christoph Hellwig | 1ae1602 | 2016-02-26 11:02:14 +0100 | [diff] [blame] | 111 | static 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 Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 117 | struct configfs_attribute { |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 118 | const char *ca_name; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 119 | struct module *ca_owner; |
Al Viro | 4394751 | 2011-07-25 00:05:26 -0400 | [diff] [blame] | 120 | umode_t ca_mode; |
Christoph Hellwig | 870823e | 2015-10-03 15:32:37 +0200 | [diff] [blame] | 121 | ssize_t (*show)(struct config_item *, char *); |
| 122 | ssize_t (*store)(struct config_item *, const char *, size_t); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
Christoph Hellwig | 870823e | 2015-10-03 15:32:37 +0200 | [diff] [blame] | 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, \ |
| 132 | } |
| 133 | |
| 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, \ |
| 140 | } |
| 141 | |
| 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, \ |
| 148 | } |
| 149 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 150 | struct file; |
| 151 | struct vm_area_struct; |
| 152 | |
| 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); |
| 159 | }; |
| 160 | |
| 161 | #define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz) \ |
| 162 | static 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 Purdila | 96c22a32 | 2016-03-23 14:14:48 +0200 | [diff] [blame] | 175 | static struct configfs_bin_attribute _pfx##attr_##_name = { \ |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 176 | .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 Purdila | 96c22a32 | 2016-03-23 14:14:48 +0200 | [diff] [blame] | 187 | static struct configfs_bin_attribute _pfx##attr_##_name = { \ |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 188 | .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 Sharma | 9b1d9aa | 2007-07-04 16:37:06 +0530 | [diff] [blame] | 198 | /* |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 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 |
Joel Becker | a6795e9 | 2008-07-17 15:21:29 -0700 | [diff] [blame] | 203 | * supports make_item(), one can create config_item children. make_group() |
| 204 | * and make_item() return ERR_PTR() on errors. If it has |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 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. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 209 | * If the group has commit(), it supports pending and committed (active) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 210 | * items. |
| 211 | */ |
| 212 | struct configfs_item_operations { |
| 213 | void (*release)(struct config_item *); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 214 | int (*allow_link)(struct config_item *src, struct config_item *target); |
Andrzej Pietrasiewicz | e16769d | 2016-11-28 13:22:42 +0100 | [diff] [blame] | 215 | void (*drop_link)(struct config_item *src, struct config_item *target); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | struct configfs_group_operations { |
Joel Becker | f89ab86 | 2008-07-17 14:53:48 -0700 | [diff] [blame] | 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); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 221 | int (*commit_item)(struct config_item *item); |
Joel Becker | 299894c | 2006-10-06 17:33:23 -0700 | [diff] [blame] | 222 | void (*disconnect_notify)(struct config_group *group, struct config_item *item); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 223 | void (*drop_item)(struct config_group *group, struct config_item *item); |
| 224 | }; |
| 225 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 226 | struct configfs_subsystem { |
| 227 | struct config_group su_group; |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 228 | struct mutex su_mutex; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | static 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 | |
| 238 | int configfs_register_subsystem(struct configfs_subsystem *subsys); |
| 239 | void configfs_unregister_subsystem(struct configfs_subsystem *subsys); |
| 240 | |
Daniel Baluta | 5cf6a51 | 2015-11-20 15:56:53 -0800 | [diff] [blame] | 241 | int configfs_register_group(struct config_group *parent_group, |
| 242 | struct config_group *group); |
| 243 | void configfs_unregister_group(struct config_group *group); |
| 244 | |
Christoph Hellwig | 1ae1602 | 2016-02-26 11:02:14 +0100 | [diff] [blame] | 245 | void configfs_remove_default_groups(struct config_group *group); |
| 246 | |
Daniel Baluta | 5cf6a51 | 2015-11-20 15:56:53 -0800 | [diff] [blame] | 247 | struct config_group * |
| 248 | configfs_register_default_group(struct config_group *parent_group, |
| 249 | const char *name, |
Bhumika Goyal | aa29358 | 2017-10-16 17:18:40 +0200 | [diff] [blame] | 250 | const struct config_item_type *item_type); |
Daniel Baluta | 5cf6a51 | 2015-11-20 15:56:53 -0800 | [diff] [blame] | 251 | void configfs_unregister_default_group(struct config_group *group); |
| 252 | |
Joel Becker | 631d1fe | 2007-06-18 18:06:09 -0700 | [diff] [blame] | 253 | /* These functions can sleep and can alloc with GFP_KERNEL */ |
| 254 | /* WARNING: These cannot be called underneath configfs callbacks!! */ |
Krzysztof Opasiak | 9a9e341 | 2015-12-11 16:06:09 +0100 | [diff] [blame] | 255 | int configfs_depend_item(struct configfs_subsystem *subsys, |
| 256 | struct config_item *target); |
| 257 | void configfs_undepend_item(struct config_item *target); |
Joel Becker | 631d1fe | 2007-06-18 18:06:09 -0700 | [diff] [blame] | 258 | |
Krzysztof Opasiak | d79d75b | 2015-12-11 16:06:12 +0100 | [diff] [blame] | 259 | /* |
| 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 | */ |
| 266 | int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys, |
| 267 | struct config_item *target); |
| 268 | |
| 269 | |
| 270 | static inline void configfs_undepend_item_unlocked(struct config_item *target) |
| 271 | { |
| 272 | configfs_undepend_item(target); |
| 273 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 274 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 275 | #endif /* _CONFIGFS_H_ */ |