Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1 | /* -*- 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 Dunlap | 55dff49 | 2009-09-23 15:56:17 -0700 | [diff] [blame] | 30 | * Please read Documentation/filesystems/configfs/configfs.txt before using |
| 31 | * the configfs interface, ESPECIALLY the parts about reference counts and |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 32 | * item destructors. |
| 33 | */ |
| 34 | |
| 35 | #ifndef _CONFIGFS_H_ |
| 36 | #define _CONFIGFS_H_ |
| 37 | |
Ben Nizette | 0778361 | 2008-02-14 19:31:31 -0800 | [diff] [blame] | 38 | #include <linux/kernel.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 39 | #include <linux/types.h> |
| 40 | #include <linux/list.h> |
| 41 | #include <linux/kref.h> |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 42 | #include <linux/mutex.h> |
Joel Becker | dacdd0e | 2008-07-17 16:54:19 -0700 | [diff] [blame] | 43 | #include <linux/err.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 44 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 45 | #include <linux/atomic.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 46 | |
| 47 | #define CONFIGFS_ITEM_NAME_LEN 20 |
| 48 | |
| 49 | struct module; |
| 50 | |
| 51 | struct configfs_item_operations; |
| 52 | struct configfs_group_operations; |
| 53 | struct configfs_attribute; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 54 | struct configfs_bin_attribute; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 55 | struct configfs_subsystem; |
| 56 | |
| 57 | struct config_item { |
| 58 | char *ci_name; |
| 59 | char ci_namebuf[CONFIGFS_ITEM_NAME_LEN]; |
| 60 | struct kref ci_kref; |
| 61 | struct list_head ci_entry; |
| 62 | struct config_item *ci_parent; |
| 63 | struct config_group *ci_group; |
| 64 | struct config_item_type *ci_type; |
| 65 | struct dentry *ci_dentry; |
| 66 | }; |
| 67 | |
Nicolas Iooss | 8db1486 | 2015-07-17 16:23:42 -0700 | [diff] [blame] | 68 | extern __printf(2, 3) |
| 69 | int config_item_set_name(struct config_item *, const char *, ...); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 70 | |
| 71 | static inline char *config_item_name(struct config_item * item) |
| 72 | { |
| 73 | return item->ci_name; |
| 74 | } |
| 75 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 76 | extern void config_item_init_type_name(struct config_item *item, |
| 77 | const char *name, |
| 78 | struct config_item_type *type); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 79 | |
| 80 | extern struct config_item * config_item_get(struct config_item *); |
| 81 | extern void config_item_put(struct config_item *); |
| 82 | |
| 83 | struct config_item_type { |
| 84 | struct module *ct_owner; |
| 85 | struct configfs_item_operations *ct_item_ops; |
| 86 | struct configfs_group_operations *ct_group_ops; |
| 87 | struct configfs_attribute **ct_attrs; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 88 | struct configfs_bin_attribute **ct_bin_attrs; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 91 | /** |
| 92 | * group - a group of config_items of a specific type, belonging |
| 93 | * to a specific subsystem. |
| 94 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 95 | struct config_group { |
| 96 | struct config_item cg_item; |
| 97 | struct list_head cg_children; |
| 98 | struct configfs_subsystem *cg_subsys; |
Christoph Hellwig | 1ae1602 | 2016-02-26 11:02:14 +0100 | [diff] [blame] | 99 | struct list_head default_groups; |
| 100 | struct list_head group_entry; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 103 | extern void config_group_init(struct config_group *group); |
| 104 | extern void config_group_init_type_name(struct config_group *group, |
| 105 | const char *name, |
| 106 | struct config_item_type *type); |
| 107 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 108 | static inline struct config_group *to_config_group(struct config_item *item) |
| 109 | { |
| 110 | return item ? container_of(item,struct config_group,cg_item) : NULL; |
| 111 | } |
| 112 | |
| 113 | static inline struct config_group *config_group_get(struct config_group *group) |
| 114 | { |
| 115 | return group ? to_config_group(config_item_get(&group->cg_item)) : NULL; |
| 116 | } |
| 117 | |
| 118 | static inline void config_group_put(struct config_group *group) |
| 119 | { |
| 120 | config_item_put(&group->cg_item); |
| 121 | } |
| 122 | |
Satyam Sharma | 3fe6c5c | 2007-07-04 16:37:16 +0530 | [diff] [blame] | 123 | extern struct config_item *config_group_find_item(struct config_group *, |
| 124 | const char *); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 125 | |
| 126 | |
Christoph Hellwig | 1ae1602 | 2016-02-26 11:02:14 +0100 | [diff] [blame] | 127 | static inline void configfs_add_default_group(struct config_group *new_group, |
| 128 | struct config_group *group) |
| 129 | { |
| 130 | list_add_tail(&new_group->group_entry, &group->default_groups); |
| 131 | } |
| 132 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 133 | struct configfs_attribute { |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 134 | const char *ca_name; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 135 | struct module *ca_owner; |
Al Viro | 4394751 | 2011-07-25 00:05:26 -0400 | [diff] [blame] | 136 | umode_t ca_mode; |
Christoph Hellwig | 870823e | 2015-10-03 15:32:37 +0200 | [diff] [blame] | 137 | ssize_t (*show)(struct config_item *, char *); |
| 138 | ssize_t (*store)(struct config_item *, const char *, size_t); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
Christoph Hellwig | 870823e | 2015-10-03 15:32:37 +0200 | [diff] [blame] | 141 | #define CONFIGFS_ATTR(_pfx, _name) \ |
| 142 | static struct configfs_attribute _pfx##attr_##_name = { \ |
| 143 | .ca_name = __stringify(_name), \ |
| 144 | .ca_mode = S_IRUGO | S_IWUSR, \ |
| 145 | .ca_owner = THIS_MODULE, \ |
| 146 | .show = _pfx##_name##_show, \ |
| 147 | .store = _pfx##_name##_store, \ |
| 148 | } |
| 149 | |
| 150 | #define CONFIGFS_ATTR_RO(_pfx, _name) \ |
| 151 | static struct configfs_attribute _pfx##attr_##_name = { \ |
| 152 | .ca_name = __stringify(_name), \ |
| 153 | .ca_mode = S_IRUGO, \ |
| 154 | .ca_owner = THIS_MODULE, \ |
| 155 | .show = _pfx##_name##_show, \ |
| 156 | } |
| 157 | |
| 158 | #define CONFIGFS_ATTR_WO(_pfx, _name) \ |
| 159 | static struct configfs_attribute _pfx##attr_##_name = { \ |
| 160 | .ca_name = __stringify(_name), \ |
| 161 | .ca_mode = S_IWUSR, \ |
| 162 | .ca_owner = THIS_MODULE, \ |
| 163 | .store = _pfx##_name##_store, \ |
| 164 | } |
| 165 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 166 | struct file; |
| 167 | struct vm_area_struct; |
| 168 | |
| 169 | struct configfs_bin_attribute { |
| 170 | struct configfs_attribute cb_attr; /* std. attribute */ |
| 171 | void *cb_private; /* for user */ |
| 172 | size_t cb_max_size; /* max core size */ |
| 173 | ssize_t (*read)(struct config_item *, void *, size_t); |
| 174 | ssize_t (*write)(struct config_item *, const void *, size_t); |
| 175 | }; |
| 176 | |
| 177 | #define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz) \ |
| 178 | static struct configfs_bin_attribute _pfx##attr_##_name = { \ |
| 179 | .cb_attr = { \ |
| 180 | .ca_name = __stringify(_name), \ |
| 181 | .ca_mode = S_IRUGO | S_IWUSR, \ |
| 182 | .ca_owner = THIS_MODULE, \ |
| 183 | }, \ |
| 184 | .cb_private = _priv, \ |
| 185 | .cb_max_size = _maxsz, \ |
| 186 | .read = _pfx##_name##_read, \ |
| 187 | .write = _pfx##_name##_write, \ |
| 188 | } |
| 189 | |
| 190 | #define CONFIGFS_BIN_ATTR_RO(_pfx, _name, _priv, _maxsz) \ |
| 191 | static struct configfs_attribute _pfx##attr_##_name = { \ |
| 192 | .cb_attr = { \ |
| 193 | .ca_name = __stringify(_name), \ |
| 194 | .ca_mode = S_IRUGO, \ |
| 195 | .ca_owner = THIS_MODULE, \ |
| 196 | }, \ |
| 197 | .cb_private = _priv, \ |
| 198 | .cb_max_size = _maxsz, \ |
| 199 | .read = _pfx##_name##_read, \ |
| 200 | } |
| 201 | |
| 202 | #define CONFIGFS_BIN_ATTR_WO(_pfx, _name, _priv, _maxsz) \ |
| 203 | static struct configfs_attribute _pfx##attr_##_name = { \ |
| 204 | .cb_attr = { \ |
| 205 | .ca_name = __stringify(_name), \ |
| 206 | .ca_mode = S_IWUSR, \ |
| 207 | .ca_owner = THIS_MODULE, \ |
| 208 | }, \ |
| 209 | .cb_private = _priv, \ |
| 210 | .cb_max_size = _maxsz, \ |
| 211 | .write = _pfx##_name##_write, \ |
| 212 | } |
| 213 | |
Satyam Sharma | 9b1d9aa | 2007-07-04 16:37:06 +0530 | [diff] [blame] | 214 | /* |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 215 | * If allow_link() exists, the item can symlink(2) out to other |
| 216 | * items. If the item is a group, it may support mkdir(2). |
| 217 | * Groups supply one of make_group() and make_item(). If the |
| 218 | * group supports make_group(), one can create group children. If it |
Joel Becker | a6795e9 | 2008-07-17 15:21:29 -0700 | [diff] [blame] | 219 | * supports make_item(), one can create config_item children. make_group() |
| 220 | * and make_item() return ERR_PTR() on errors. If it has |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 221 | * default_groups on group->default_groups, it has automatically created |
| 222 | * group children. default_groups may coexist alongsize make_group() or |
| 223 | * make_item(), but if the group wishes to have only default_groups |
| 224 | * children (disallowing mkdir(2)), it need not provide either function. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 225 | * If the group has commit(), it supports pending and committed (active) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 226 | * items. |
| 227 | */ |
| 228 | struct configfs_item_operations { |
| 229 | void (*release)(struct config_item *); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 230 | int (*allow_link)(struct config_item *src, struct config_item *target); |
| 231 | int (*drop_link)(struct config_item *src, struct config_item *target); |
| 232 | }; |
| 233 | |
| 234 | struct configfs_group_operations { |
Joel Becker | f89ab86 | 2008-07-17 14:53:48 -0700 | [diff] [blame] | 235 | struct config_item *(*make_item)(struct config_group *group, const char *name); |
| 236 | struct config_group *(*make_group)(struct config_group *group, const char *name); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 237 | int (*commit_item)(struct config_item *item); |
Joel Becker | 299894c | 2006-10-06 17:33:23 -0700 | [diff] [blame] | 238 | void (*disconnect_notify)(struct config_group *group, struct config_item *item); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 239 | void (*drop_item)(struct config_group *group, struct config_item *item); |
| 240 | }; |
| 241 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 242 | struct configfs_subsystem { |
| 243 | struct config_group su_group; |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 244 | struct mutex su_mutex; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group) |
| 248 | { |
| 249 | return group ? |
| 250 | container_of(group, struct configfs_subsystem, su_group) : |
| 251 | NULL; |
| 252 | } |
| 253 | |
| 254 | int configfs_register_subsystem(struct configfs_subsystem *subsys); |
| 255 | void configfs_unregister_subsystem(struct configfs_subsystem *subsys); |
| 256 | |
Daniel Baluta | 5cf6a51 | 2015-11-20 15:56:53 -0800 | [diff] [blame] | 257 | int configfs_register_group(struct config_group *parent_group, |
| 258 | struct config_group *group); |
| 259 | void configfs_unregister_group(struct config_group *group); |
| 260 | |
Christoph Hellwig | 1ae1602 | 2016-02-26 11:02:14 +0100 | [diff] [blame] | 261 | void configfs_remove_default_groups(struct config_group *group); |
| 262 | |
Daniel Baluta | 5cf6a51 | 2015-11-20 15:56:53 -0800 | [diff] [blame] | 263 | struct config_group * |
| 264 | configfs_register_default_group(struct config_group *parent_group, |
| 265 | const char *name, |
| 266 | struct config_item_type *item_type); |
| 267 | void configfs_unregister_default_group(struct config_group *group); |
| 268 | |
Joel Becker | 631d1fe | 2007-06-18 18:06:09 -0700 | [diff] [blame] | 269 | /* These functions can sleep and can alloc with GFP_KERNEL */ |
| 270 | /* WARNING: These cannot be called underneath configfs callbacks!! */ |
Krzysztof Opasiak | 9a9e341 | 2015-12-11 16:06:09 +0100 | [diff] [blame] | 271 | int configfs_depend_item(struct configfs_subsystem *subsys, |
| 272 | struct config_item *target); |
| 273 | void configfs_undepend_item(struct config_item *target); |
Joel Becker | 631d1fe | 2007-06-18 18:06:09 -0700 | [diff] [blame] | 274 | |
Krzysztof Opasiak | d79d75b | 2015-12-11 16:06:12 +0100 | [diff] [blame] | 275 | /* |
| 276 | * These functions can sleep and can alloc with GFP_KERNEL |
| 277 | * NOTE: These should be called only underneath configfs callbacks. |
| 278 | * NOTE: First parameter is a caller's subsystem, not target's. |
| 279 | * WARNING: These cannot be called on newly created item |
| 280 | * (in make_group()/make_item() callback) |
| 281 | */ |
| 282 | int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys, |
| 283 | struct config_item *target); |
| 284 | |
| 285 | |
| 286 | static inline void configfs_undepend_item_unlocked(struct config_item *target) |
| 287 | { |
| 288 | configfs_undepend_item(target); |
| 289 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 290 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 291 | #endif /* _CONFIGFS_H_ */ |