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; |
| 54 | struct configfs_subsystem; |
| 55 | |
| 56 | struct config_item { |
| 57 | char *ci_name; |
| 58 | char ci_namebuf[CONFIGFS_ITEM_NAME_LEN]; |
| 59 | struct kref ci_kref; |
| 60 | struct list_head ci_entry; |
| 61 | struct config_item *ci_parent; |
| 62 | struct config_group *ci_group; |
| 63 | struct config_item_type *ci_type; |
| 64 | struct dentry *ci_dentry; |
| 65 | }; |
| 66 | |
Nicolas Iooss | 8db1486 | 2015-07-17 16:23:42 -0700 | [diff] [blame] | 67 | extern __printf(2, 3) |
| 68 | int config_item_set_name(struct config_item *, const char *, ...); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 69 | |
| 70 | static inline char *config_item_name(struct config_item * item) |
| 71 | { |
| 72 | return item->ci_name; |
| 73 | } |
| 74 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 75 | extern void config_item_init_type_name(struct config_item *item, |
| 76 | const char *name, |
| 77 | struct config_item_type *type); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 78 | |
| 79 | extern struct config_item * config_item_get(struct config_item *); |
| 80 | extern void config_item_put(struct config_item *); |
| 81 | |
| 82 | struct config_item_type { |
| 83 | struct module *ct_owner; |
| 84 | struct configfs_item_operations *ct_item_ops; |
| 85 | struct configfs_group_operations *ct_group_ops; |
| 86 | struct configfs_attribute **ct_attrs; |
| 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; |
| 97 | struct config_group **default_groups; |
| 98 | }; |
| 99 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 100 | extern void config_group_init(struct config_group *group); |
| 101 | extern void config_group_init_type_name(struct config_group *group, |
| 102 | const char *name, |
| 103 | struct config_item_type *type); |
| 104 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 105 | static inline struct config_group *to_config_group(struct config_item *item) |
| 106 | { |
| 107 | return item ? container_of(item,struct config_group,cg_item) : NULL; |
| 108 | } |
| 109 | |
| 110 | static inline struct config_group *config_group_get(struct config_group *group) |
| 111 | { |
| 112 | return group ? to_config_group(config_item_get(&group->cg_item)) : NULL; |
| 113 | } |
| 114 | |
| 115 | static inline void config_group_put(struct config_group *group) |
| 116 | { |
| 117 | config_item_put(&group->cg_item); |
| 118 | } |
| 119 | |
Satyam Sharma | 3fe6c5c | 2007-07-04 16:37:16 +0530 | [diff] [blame] | 120 | extern struct config_item *config_group_find_item(struct config_group *, |
| 121 | const char *); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 122 | |
| 123 | |
| 124 | struct configfs_attribute { |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 125 | const char *ca_name; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 126 | struct module *ca_owner; |
Al Viro | 4394751 | 2011-07-25 00:05:26 -0400 | [diff] [blame] | 127 | umode_t ca_mode; |
Christoph Hellwig | 870823e | 2015-10-03 15:32:37 +0200 | [diff] [blame] | 128 | ssize_t (*show)(struct config_item *, char *); |
| 129 | ssize_t (*store)(struct config_item *, const char *, size_t); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
Christoph Hellwig | 870823e | 2015-10-03 15:32:37 +0200 | [diff] [blame] | 132 | #define CONFIGFS_ATTR(_pfx, _name) \ |
| 133 | static struct configfs_attribute _pfx##attr_##_name = { \ |
| 134 | .ca_name = __stringify(_name), \ |
| 135 | .ca_mode = S_IRUGO | S_IWUSR, \ |
| 136 | .ca_owner = THIS_MODULE, \ |
| 137 | .show = _pfx##_name##_show, \ |
| 138 | .store = _pfx##_name##_store, \ |
| 139 | } |
| 140 | |
| 141 | #define CONFIGFS_ATTR_RO(_pfx, _name) \ |
| 142 | static struct configfs_attribute _pfx##attr_##_name = { \ |
| 143 | .ca_name = __stringify(_name), \ |
| 144 | .ca_mode = S_IRUGO, \ |
| 145 | .ca_owner = THIS_MODULE, \ |
| 146 | .show = _pfx##_name##_show, \ |
| 147 | } |
| 148 | |
| 149 | #define CONFIGFS_ATTR_WO(_pfx, _name) \ |
| 150 | static struct configfs_attribute _pfx##attr_##_name = { \ |
| 151 | .ca_name = __stringify(_name), \ |
| 152 | .ca_mode = S_IWUSR, \ |
| 153 | .ca_owner = THIS_MODULE, \ |
| 154 | .store = _pfx##_name##_store, \ |
| 155 | } |
| 156 | |
Satyam Sharma | 9b1d9aa | 2007-07-04 16:37:06 +0530 | [diff] [blame] | 157 | /* |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 158 | * If allow_link() exists, the item can symlink(2) out to other |
| 159 | * items. If the item is a group, it may support mkdir(2). |
| 160 | * Groups supply one of make_group() and make_item(). If the |
| 161 | * group supports make_group(), one can create group children. If it |
Joel Becker | a6795e9 | 2008-07-17 15:21:29 -0700 | [diff] [blame] | 162 | * supports make_item(), one can create config_item children. make_group() |
| 163 | * and make_item() return ERR_PTR() on errors. If it has |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 164 | * default_groups on group->default_groups, it has automatically created |
| 165 | * group children. default_groups may coexist alongsize make_group() or |
| 166 | * make_item(), but if the group wishes to have only default_groups |
| 167 | * children (disallowing mkdir(2)), it need not provide either function. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 168 | * If the group has commit(), it supports pending and committed (active) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 169 | * items. |
| 170 | */ |
| 171 | struct configfs_item_operations { |
| 172 | void (*release)(struct config_item *); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 173 | int (*allow_link)(struct config_item *src, struct config_item *target); |
| 174 | int (*drop_link)(struct config_item *src, struct config_item *target); |
| 175 | }; |
| 176 | |
| 177 | struct configfs_group_operations { |
Joel Becker | f89ab86 | 2008-07-17 14:53:48 -0700 | [diff] [blame] | 178 | struct config_item *(*make_item)(struct config_group *group, const char *name); |
| 179 | struct config_group *(*make_group)(struct config_group *group, const char *name); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 180 | int (*commit_item)(struct config_item *item); |
Joel Becker | 299894c | 2006-10-06 17:33:23 -0700 | [diff] [blame] | 181 | void (*disconnect_notify)(struct config_group *group, struct config_item *item); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 182 | void (*drop_item)(struct config_group *group, struct config_item *item); |
| 183 | }; |
| 184 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 185 | struct configfs_subsystem { |
| 186 | struct config_group su_group; |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 187 | struct mutex su_mutex; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group) |
| 191 | { |
| 192 | return group ? |
| 193 | container_of(group, struct configfs_subsystem, su_group) : |
| 194 | NULL; |
| 195 | } |
| 196 | |
| 197 | int configfs_register_subsystem(struct configfs_subsystem *subsys); |
| 198 | void configfs_unregister_subsystem(struct configfs_subsystem *subsys); |
| 199 | |
Joel Becker | 631d1fe | 2007-06-18 18:06:09 -0700 | [diff] [blame] | 200 | /* These functions can sleep and can alloc with GFP_KERNEL */ |
| 201 | /* WARNING: These cannot be called underneath configfs callbacks!! */ |
| 202 | int configfs_depend_item(struct configfs_subsystem *subsys, struct config_item *target); |
| 203 | void configfs_undepend_item(struct configfs_subsystem *subsys, struct config_item *target); |
| 204 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 205 | #endif /* _CONFIGFS_H_ */ |