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