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 | * item.c - library routines for handling generic config items |
| 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 kobject: |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 22 | * kobject is Copyright (c) 2002-2003 Patrick Mochel |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 23 | * |
| 24 | * configfs Copyright (C) 2005 Oracle. All rights reserved. |
| 25 | * |
Paul Bolle | 395cf96 | 2011-08-15 02:02:26 +0200 | [diff] [blame] | 26 | * Please see the file Documentation/filesystems/configfs/configfs.txt for |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 27 | * critical information about using the config_item interface. |
| 28 | */ |
| 29 | |
| 30 | #include <linux/string.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/stat.h> |
| 33 | #include <linux/slab.h> |
| 34 | |
| 35 | #include <linux/configfs.h> |
| 36 | |
| 37 | |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 38 | static inline struct config_item *to_item(struct list_head *entry) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 39 | { |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 40 | return container_of(entry, struct config_item, ci_entry); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* Evil kernel */ |
| 44 | static void config_item_release(struct kref *kref); |
| 45 | |
| 46 | /** |
| 47 | * config_item_init - initialize item. |
| 48 | * @item: item in question. |
| 49 | */ |
Fabian Frederick | 5286d20 | 2015-06-24 16:54:51 -0700 | [diff] [blame] | 50 | static void config_item_init(struct config_item *item) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 51 | { |
| 52 | kref_init(&item->ci_kref); |
| 53 | INIT_LIST_HEAD(&item->ci_entry); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * config_item_set_name - Set the name of an item |
| 58 | * @item: item. |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 59 | * @fmt: The vsnprintf()'s format string. |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 60 | * |
| 61 | * If strlen(name) >= CONFIGFS_ITEM_NAME_LEN, then use a |
| 62 | * dynamically allocated string that @item->ci_name points to. |
| 63 | * Otherwise, use the static @item->ci_namebuf array. |
| 64 | */ |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 65 | int config_item_set_name(struct config_item *item, const char *fmt, ...) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 66 | { |
| 67 | int error = 0; |
| 68 | int limit = CONFIGFS_ITEM_NAME_LEN; |
| 69 | int need; |
| 70 | va_list args; |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 71 | char *name; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * First, try the static array |
| 75 | */ |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 76 | va_start(args, fmt); |
| 77 | need = vsnprintf(item->ci_namebuf, limit, fmt, args); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 78 | va_end(args); |
| 79 | if (need < limit) |
| 80 | name = item->ci_namebuf; |
| 81 | else { |
| 82 | /* |
| 83 | * Need more space? Allocate it and try again |
| 84 | */ |
| 85 | limit = need + 1; |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 86 | name = kmalloc(limit, GFP_KERNEL); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 87 | if (!name) { |
| 88 | error = -ENOMEM; |
| 89 | goto Done; |
| 90 | } |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 91 | va_start(args, fmt); |
| 92 | need = vsnprintf(name, limit, fmt, args); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 93 | va_end(args); |
| 94 | |
| 95 | /* Still? Give up. */ |
| 96 | if (need >= limit) { |
| 97 | kfree(name); |
| 98 | error = -EFAULT; |
| 99 | goto Done; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* Free the old name, if necessary. */ |
| 104 | if (item->ci_name && item->ci_name != item->ci_namebuf) |
| 105 | kfree(item->ci_name); |
| 106 | |
| 107 | /* Now, set the new name */ |
| 108 | item->ci_name = name; |
| 109 | Done: |
| 110 | return error; |
| 111 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 112 | EXPORT_SYMBOL(config_item_set_name); |
| 113 | |
| 114 | void config_item_init_type_name(struct config_item *item, |
| 115 | const char *name, |
Bhumika Goyal | aa29358 | 2017-10-16 17:18:40 +0200 | [diff] [blame] | 116 | const struct config_item_type *type) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 117 | { |
Nicolas Iooss | 3958b79 | 2015-07-17 16:23:45 -0700 | [diff] [blame] | 118 | config_item_set_name(item, "%s", name); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 119 | item->ci_type = type; |
| 120 | config_item_init(item); |
| 121 | } |
| 122 | EXPORT_SYMBOL(config_item_init_type_name); |
| 123 | |
| 124 | void config_group_init_type_name(struct config_group *group, const char *name, |
Bhumika Goyal | aa29358 | 2017-10-16 17:18:40 +0200 | [diff] [blame] | 125 | const struct config_item_type *type) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 126 | { |
Nicolas Iooss | 3958b79 | 2015-07-17 16:23:45 -0700 | [diff] [blame] | 127 | config_item_set_name(&group->cg_item, "%s", name); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 128 | group->cg_item.ci_type = type; |
| 129 | config_group_init(group); |
| 130 | } |
| 131 | EXPORT_SYMBOL(config_group_init_type_name); |
| 132 | |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 133 | struct config_item *config_item_get(struct config_item *item) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 134 | { |
| 135 | if (item) |
| 136 | kref_get(&item->ci_kref); |
| 137 | return item; |
| 138 | } |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 139 | EXPORT_SYMBOL(config_item_get); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 140 | |
Bart Van Assche | 19e72d3 | 2017-02-09 17:28:50 -0800 | [diff] [blame] | 141 | struct config_item *config_item_get_unless_zero(struct config_item *item) |
| 142 | { |
| 143 | if (item && kref_get_unless_zero(&item->ci_kref)) |
| 144 | return item; |
| 145 | return NULL; |
| 146 | } |
| 147 | EXPORT_SYMBOL(config_item_get_unless_zero); |
| 148 | |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 149 | static void config_item_cleanup(struct config_item *item) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 150 | { |
Bhumika Goyal | aa29358 | 2017-10-16 17:18:40 +0200 | [diff] [blame] | 151 | const struct config_item_type *t = item->ci_type; |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 152 | struct config_group *s = item->ci_group; |
| 153 | struct config_item *parent = item->ci_parent; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 154 | |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 155 | pr_debug("config_item %s: cleaning up\n", config_item_name(item)); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 156 | if (item->ci_name != item->ci_namebuf) |
| 157 | kfree(item->ci_name); |
| 158 | item->ci_name = NULL; |
| 159 | if (t && t->ct_item_ops && t->ct_item_ops->release) |
| 160 | t->ct_item_ops->release(item); |
| 161 | if (s) |
| 162 | config_group_put(s); |
| 163 | if (parent) |
| 164 | config_item_put(parent); |
| 165 | } |
| 166 | |
| 167 | static void config_item_release(struct kref *kref) |
| 168 | { |
| 169 | config_item_cleanup(container_of(kref, struct config_item, ci_kref)); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * config_item_put - decrement refcount for item. |
| 174 | * @item: item. |
| 175 | * |
| 176 | * Decrement the refcount, and if 0, call config_item_cleanup(). |
| 177 | */ |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 178 | void config_item_put(struct config_item *item) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 179 | { |
| 180 | if (item) |
| 181 | kref_put(&item->ci_kref, config_item_release); |
| 182 | } |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 183 | EXPORT_SYMBOL(config_item_put); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 184 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 185 | /** |
| 186 | * config_group_init - initialize a group for use |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 187 | * @group: config_group |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 188 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 189 | void config_group_init(struct config_group *group) |
| 190 | { |
| 191 | config_item_init(&group->cg_item); |
| 192 | INIT_LIST_HEAD(&group->cg_children); |
Christoph Hellwig | 1ae1602 | 2016-02-26 11:02:14 +0100 | [diff] [blame] | 193 | INIT_LIST_HEAD(&group->default_groups); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 194 | } |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 195 | EXPORT_SYMBOL(config_group_init); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 196 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 197 | /** |
Satyam Sharma | 3fe6c5c | 2007-07-04 16:37:16 +0530 | [diff] [blame] | 198 | * config_group_find_item - search for item in group. |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 199 | * @group: group we're looking in. |
| 200 | * @name: item's name. |
| 201 | * |
Satyam Sharma | 3fe6c5c | 2007-07-04 16:37:16 +0530 | [diff] [blame] | 202 | * Iterate over @group->cg_list, looking for a matching config_item. |
| 203 | * If matching item is found take a reference and return the item. |
| 204 | * Caller must have locked group via @group->cg_subsys->su_mtx. |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 205 | */ |
Satyam Sharma | 3fe6c5c | 2007-07-04 16:37:16 +0530 | [diff] [blame] | 206 | struct config_item *config_group_find_item(struct config_group *group, |
| 207 | const char *name) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 208 | { |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 209 | struct list_head *entry; |
| 210 | struct config_item *ret = NULL; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 211 | |
Fabian Frederick | f6b1fe7 | 2014-06-04 16:05:57 -0700 | [diff] [blame] | 212 | list_for_each(entry, &group->cg_children) { |
| 213 | struct config_item *item = to_item(entry); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 214 | if (config_item_name(item) && |
Satyam Sharma | 3fe6c5c | 2007-07-04 16:37:16 +0530 | [diff] [blame] | 215 | !strcmp(config_item_name(item), name)) { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 216 | ret = config_item_get(item); |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | return ret; |
| 221 | } |
Satyam Sharma | 3fe6c5c | 2007-07-04 16:37:16 +0530 | [diff] [blame] | 222 | EXPORT_SYMBOL(config_group_find_item); |