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 | * |
| 30 | * Please read Documentation/filesystems/configfs.txt before using the |
| 31 | * configfs interface, ESPECIALLY the parts about reference counts and |
| 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 | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 43 | |
| 44 | #include <asm/atomic.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 45 | |
| 46 | #define CONFIGFS_ITEM_NAME_LEN 20 |
| 47 | |
| 48 | struct module; |
| 49 | |
| 50 | struct configfs_item_operations; |
| 51 | struct configfs_group_operations; |
| 52 | struct configfs_attribute; |
| 53 | struct configfs_subsystem; |
| 54 | |
| 55 | struct config_item { |
| 56 | char *ci_name; |
| 57 | char ci_namebuf[CONFIGFS_ITEM_NAME_LEN]; |
| 58 | struct kref ci_kref; |
| 59 | struct list_head ci_entry; |
| 60 | struct config_item *ci_parent; |
| 61 | struct config_group *ci_group; |
| 62 | struct config_item_type *ci_type; |
| 63 | struct dentry *ci_dentry; |
| 64 | }; |
| 65 | |
| 66 | extern int config_item_set_name(struct config_item *, const char *, ...); |
| 67 | |
| 68 | static inline char *config_item_name(struct config_item * item) |
| 69 | { |
| 70 | return item->ci_name; |
| 71 | } |
| 72 | |
| 73 | extern void config_item_init(struct config_item *); |
| 74 | extern void config_item_init_type_name(struct config_item *item, |
| 75 | const char *name, |
| 76 | struct config_item_type *type); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 77 | |
| 78 | extern struct config_item * config_item_get(struct config_item *); |
| 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; |
| 86 | }; |
| 87 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 88 | /** |
| 89 | * group - a group of config_items of a specific type, belonging |
| 90 | * to a specific subsystem. |
| 91 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 92 | struct config_group { |
| 93 | struct config_item cg_item; |
| 94 | struct list_head cg_children; |
| 95 | struct configfs_subsystem *cg_subsys; |
| 96 | struct config_group **default_groups; |
| 97 | }; |
| 98 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 99 | extern void config_group_init(struct config_group *group); |
| 100 | extern void config_group_init_type_name(struct config_group *group, |
| 101 | const char *name, |
| 102 | struct config_item_type *type); |
| 103 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 104 | static inline struct config_group *to_config_group(struct config_item *item) |
| 105 | { |
| 106 | return item ? container_of(item,struct config_group,cg_item) : NULL; |
| 107 | } |
| 108 | |
| 109 | static inline struct config_group *config_group_get(struct config_group *group) |
| 110 | { |
| 111 | return group ? to_config_group(config_item_get(&group->cg_item)) : NULL; |
| 112 | } |
| 113 | |
| 114 | static inline void config_group_put(struct config_group *group) |
| 115 | { |
| 116 | config_item_put(&group->cg_item); |
| 117 | } |
| 118 | |
Satyam Sharma | 3fe6c5c | 2007-07-04 16:37:16 +0530 | [diff] [blame] | 119 | extern struct config_item *config_group_find_item(struct config_group *, |
| 120 | const char *); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 121 | |
| 122 | |
| 123 | struct configfs_attribute { |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 124 | const char *ca_name; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 125 | struct module *ca_owner; |
| 126 | mode_t ca_mode; |
| 127 | }; |
| 128 | |
Satyam Sharma | 9b1d9aa | 2007-07-04 16:37:06 +0530 | [diff] [blame] | 129 | /* |
| 130 | * Users often need to create attribute structures for their configurable |
| 131 | * attributes, containing a configfs_attribute member and function pointers |
| 132 | * for the show() and store() operations on that attribute. They can use |
| 133 | * this macro (similar to sysfs' __ATTR) to make defining attributes easier. |
| 134 | */ |
| 135 | #define __CONFIGFS_ATTR(_name, _mode, _show, _store) \ |
| 136 | { \ |
| 137 | .attr = { \ |
| 138 | .ca_name = __stringify(_name), \ |
| 139 | .ca_mode = _mode, \ |
| 140 | .ca_owner = THIS_MODULE, \ |
| 141 | }, \ |
| 142 | .show = _show, \ |
| 143 | .store = _store, \ |
| 144 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | * If allow_link() exists, the item can symlink(2) out to other |
| 148 | * items. If the item is a group, it may support mkdir(2). |
| 149 | * Groups supply one of make_group() and make_item(). If the |
| 150 | * group supports make_group(), one can create group children. If it |
| 151 | * supports make_item(), one can create config_item children. If it has |
| 152 | * default_groups on group->default_groups, it has automatically created |
| 153 | * group children. default_groups may coexist alongsize make_group() or |
| 154 | * make_item(), but if the group wishes to have only default_groups |
| 155 | * children (disallowing mkdir(2)), it need not provide either function. |
| 156 | * If the group has commit(), it supports pending and commited (active) |
| 157 | * items. |
| 158 | */ |
| 159 | struct configfs_item_operations { |
| 160 | void (*release)(struct config_item *); |
| 161 | ssize_t (*show_attribute)(struct config_item *, struct configfs_attribute *,char *); |
| 162 | ssize_t (*store_attribute)(struct config_item *,struct configfs_attribute *,const char *, size_t); |
| 163 | int (*allow_link)(struct config_item *src, struct config_item *target); |
| 164 | int (*drop_link)(struct config_item *src, struct config_item *target); |
| 165 | }; |
| 166 | |
| 167 | struct configfs_group_operations { |
| 168 | struct config_item *(*make_item)(struct config_group *group, const char *name); |
| 169 | struct config_group *(*make_group)(struct config_group *group, const char *name); |
| 170 | int (*commit_item)(struct config_item *item); |
Joel Becker | 299894c | 2006-10-06 17:33:23 -0700 | [diff] [blame] | 171 | void (*disconnect_notify)(struct config_group *group, struct config_item *item); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 172 | void (*drop_item)(struct config_group *group, struct config_item *item); |
| 173 | }; |
| 174 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 175 | struct configfs_subsystem { |
| 176 | struct config_group su_group; |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 177 | struct mutex su_mutex; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | static inline struct configfs_subsystem *to_configfs_subsystem(struct config_group *group) |
| 181 | { |
| 182 | return group ? |
| 183 | container_of(group, struct configfs_subsystem, su_group) : |
| 184 | NULL; |
| 185 | } |
| 186 | |
| 187 | int configfs_register_subsystem(struct configfs_subsystem *subsys); |
| 188 | void configfs_unregister_subsystem(struct configfs_subsystem *subsys); |
| 189 | |
Joel Becker | 631d1fe | 2007-06-18 18:06:09 -0700 | [diff] [blame] | 190 | /* These functions can sleep and can alloc with GFP_KERNEL */ |
| 191 | /* WARNING: These cannot be called underneath configfs callbacks!! */ |
| 192 | int configfs_depend_item(struct configfs_subsystem *subsys, struct config_item *target); |
| 193 | void configfs_undepend_item(struct configfs_subsystem *subsys, struct config_item *target); |
| 194 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 195 | #endif /* _CONFIGFS_H_ */ |