1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * configfs.h - definitions for the device driver filesystem 4 * 5 * Based on sysfs: 6 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel 7 * 8 * Based on kobject.h: 9 * Copyright (c) 2002-2003 Patrick Mochel 10 * Copyright (c) 2002-2003 Open Source Development Labs 11 * 12 * configfs Copyright (C) 2005 Oracle. All rights reserved. 13 * 14 * Please read Documentation/filesystems/configfs.rst before using 15 * the configfs interface, ESPECIALLY the parts about reference counts and 16 * item destructors. 17 */ 18 19 #ifndef _CONFIGFS_H_ 20 #define _CONFIGFS_H_ 21 22 #include <linux/stat.h> /* S_IRUGO */ 23 #include <linux/types.h> /* ssize_t */ 24 #include <linux/list.h> /* struct list_head */ 25 #include <linux/kref.h> /* struct kref */ 26 #include <linux/mutex.h> /* struct mutex */ 27 28 #define CONFIGFS_ITEM_NAME_LEN 20 29 30 struct module; 31 32 struct configfs_item_operations; 33 struct configfs_group_operations; 34 struct configfs_attribute; 35 struct configfs_bin_attribute; 36 struct configfs_subsystem; 37 38 struct config_item { 39 char *ci_name; 40 char ci_namebuf[CONFIGFS_ITEM_NAME_LEN]; 41 struct kref ci_kref; 42 struct list_head ci_entry; 43 struct config_item *ci_parent; 44 struct config_group *ci_group; 45 const struct config_item_type *ci_type; 46 struct dentry *ci_dentry; 47 }; 48 49 extern __printf(2, 3) 50 int config_item_set_name(struct config_item *, const char *, ...); 51 52 static inline char *config_item_name(struct config_item * item) 53 { 54 return item->ci_name; 55 } 56 57 extern void config_item_init_type_name(struct config_item *item, 58 const char *name, 59 const struct config_item_type *type); 60 61 extern struct config_item *config_item_get(struct config_item *); 62 extern struct config_item *config_item_get_unless_zero(struct config_item *); 63 extern void config_item_put(struct config_item *); 64 65 struct config_item_type { 66 struct module *ct_owner; 67 const struct configfs_item_operations *ct_item_ops; 68 const struct configfs_group_operations *ct_group_ops; 69 union { 70 struct configfs_attribute **ct_attrs; 71 const struct configfs_attribute *const *ct_attrs_const; 72 }; 73 const struct configfs_bin_attribute *const *ct_bin_attrs; 74 }; 75 76 /** 77 * group - a group of config_items of a specific type, belonging 78 * to a specific subsystem. 79 */ 80 struct config_group { 81 struct config_item cg_item; 82 struct list_head cg_children; 83 struct configfs_subsystem *cg_subsys; 84 struct list_head default_groups; 85 struct list_head group_entry; 86 }; 87 88 extern void config_group_init(struct config_group *group); 89 extern void config_group_init_type_name(struct config_group *group, 90 const char *name, 91 const struct config_item_type *type); 92 93 static inline struct config_group *to_config_group(struct config_item *item) 94 { 95 return item ? container_of(item,struct config_group,cg_item) : NULL; 96 } 97 98 static inline struct config_group *config_group_get(struct config_group *group) 99 { 100 return group ? to_config_group(config_item_get(&group->cg_item)) : NULL; 101 } 102 103 static inline void config_group_put(struct config_group *group) 104 { 105 config_item_put(&group->cg_item); 106 } 107 108 extern struct config_item *config_group_find_item(struct config_group *, 109 const char *); 110 111 112 static inline void configfs_add_default_group(struct config_group *new_group, 113 struct config_group *group) 114 { 115 list_add_tail(&new_group->group_entry, &group->default_groups); 116 } 117 118 struct configfs_attribute { 119 const char *ca_name; 120 struct module *ca_owner; 121 umode_t ca_mode; 122 ssize_t (*show)(struct config_item *, char *); 123 ssize_t (*store)(struct config_item *, const char *, size_t); 124 }; 125 126 #define CONFIGFS_ATTR_PERM(_pfx, _name, _perm) \ 127 static struct configfs_attribute _pfx##attr_##_name = { \ 128 .ca_name = __stringify(_name), \ 129 .ca_mode = _perm, \ 130 .ca_owner = THIS_MODULE, \ 131 .show = _pfx##_name##_show, \ 132 .store = _pfx##_name##_store, \ 133 } 134 135 #define CONFIGFS_ATTR(_pfx, _name) CONFIGFS_ATTR_PERM( \ 136 _pfx, _name, S_IRUGO | S_IWUSR \ 137 ) 138 139 #define CONFIGFS_ATTR_RO(_pfx, _name) \ 140 static struct configfs_attribute _pfx##attr_##_name = { \ 141 .ca_name = __stringify(_name), \ 142 .ca_mode = S_IRUGO, \ 143 .ca_owner = THIS_MODULE, \ 144 .show = _pfx##_name##_show, \ 145 } 146 147 #define CONFIGFS_ATTR_WO(_pfx, _name) \ 148 static struct configfs_attribute _pfx##attr_##_name = { \ 149 .ca_name = __stringify(_name), \ 150 .ca_mode = S_IWUSR, \ 151 .ca_owner = THIS_MODULE, \ 152 .store = _pfx##_name##_store, \ 153 } 154 155 struct file; 156 struct vm_area_struct; 157 158 struct configfs_bin_attribute { 159 struct configfs_attribute cb_attr; /* std. attribute */ 160 void *cb_private; /* for user */ 161 size_t cb_max_size; /* max core size */ 162 ssize_t (*read)(struct config_item *, void *, size_t); 163 ssize_t (*write)(struct config_item *, const void *, size_t); 164 }; 165 166 #define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz) \ 167 static const struct configfs_bin_attribute _pfx##attr_##_name = { \ 168 .cb_attr = { \ 169 .ca_name = __stringify(_name), \ 170 .ca_mode = S_IRUGO | S_IWUSR, \ 171 .ca_owner = THIS_MODULE, \ 172 }, \ 173 .cb_private = _priv, \ 174 .cb_max_size = _maxsz, \ 175 .read = _pfx##_name##_read, \ 176 .write = _pfx##_name##_write, \ 177 } 178 179 #define CONFIGFS_BIN_ATTR_RO(_pfx, _name, _priv, _maxsz) \ 180 static const struct configfs_bin_attribute _pfx##attr_##_name = { \ 181 .cb_attr = { \ 182 .ca_name = __stringify(_name), \ 183 .ca_mode = S_IRUGO, \ 184 .ca_owner = THIS_MODULE, \ 185 }, \ 186 .cb_private = _priv, \ 187 .cb_max_size = _maxsz, \ 188 .read = _pfx##_name##_read, \ 189 } 190 191 #define CONFIGFS_BIN_ATTR_WO(_pfx, _name, _priv, _maxsz) \ 192 static const struct configfs_bin_attribute _pfx##attr_##_name = { \ 193 .cb_attr = { \ 194 .ca_name = __stringify(_name), \ 195 .ca_mode = S_IWUSR, \ 196 .ca_owner = THIS_MODULE, \ 197 }, \ 198 .cb_private = _priv, \ 199 .cb_max_size = _maxsz, \ 200 .write = _pfx##_name##_write, \ 201 } 202 203 /* 204 * If allow_link() exists, the item can symlink(2) out to other 205 * items. If the item is a group, it may support mkdir(2). 206 * Groups supply one of make_group() and make_item(). If the 207 * group supports make_group(), one can create group children. If it 208 * supports make_item(), one can create config_item children. make_group() 209 * and make_item() return ERR_PTR() on errors. If it has 210 * default_groups on group->default_groups, it has automatically created 211 * group children. default_groups may coexist alongsize make_group() or 212 * make_item(), but if the group wishes to have only default_groups 213 * children (disallowing mkdir(2)), it need not provide either function. 214 */ 215 struct configfs_item_operations { 216 void (*release)(struct config_item *); 217 int (*allow_link)(struct config_item *src, struct config_item *target); 218 void (*drop_link)(struct config_item *src, struct config_item *target); 219 }; 220 221 struct configfs_group_operations { 222 struct config_item *(*make_item)(struct config_group *group, const char *name); 223 struct config_group *(*make_group)(struct config_group *group, const char *name); 224 void (*disconnect_notify)(struct config_group *group, struct config_item *item); 225 void (*drop_item)(struct config_group *group, struct config_item *item); 226 bool (*is_visible)(struct config_item *item, const struct configfs_attribute *attr, int n); 227 bool (*is_bin_visible)(struct config_item *item, const struct configfs_bin_attribute *attr, 228 int n); 229 }; 230 231 struct configfs_subsystem { 232 struct config_group su_group; 233 struct mutex su_mutex; 234 }; 235 236 static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group) 237 { 238 return group ? 239 container_of(group, struct configfs_subsystem, su_group) : 240 NULL; 241 } 242 243 int configfs_register_subsystem(struct configfs_subsystem *subsys); 244 void configfs_unregister_subsystem(struct configfs_subsystem *subsys); 245 246 int configfs_register_group(struct config_group *parent_group, 247 struct config_group *group); 248 void configfs_unregister_group(struct config_group *group); 249 250 void configfs_remove_default_groups(struct config_group *group); 251 252 struct config_group * 253 configfs_register_default_group(struct config_group *parent_group, 254 const char *name, 255 const struct config_item_type *item_type); 256 void configfs_unregister_default_group(struct config_group *group); 257 258 /* These functions can sleep and can alloc with GFP_KERNEL */ 259 /* WARNING: These cannot be called underneath configfs callbacks!! */ 260 int configfs_depend_item(struct configfs_subsystem *subsys, 261 struct config_item *target); 262 void configfs_undepend_item(struct config_item *target); 263 264 /* 265 * These functions can sleep and can alloc with GFP_KERNEL 266 * NOTE: These should be called only underneath configfs callbacks. 267 * NOTE: First parameter is a caller's subsystem, not target's. 268 * WARNING: These cannot be called on newly created item 269 * (in make_group()/make_item() callback) 270 */ 271 int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys, 272 struct config_item *target); 273 274 275 static inline void configfs_undepend_item_unlocked(struct config_item *target) 276 { 277 configfs_undepend_item(target); 278 } 279 280 #endif /* _CONFIGFS_H_ */ 281