blob: 4d6a30e76168c3f548ce48dbbb1428e0baf81d49 [file] [log] [blame]
Joel Becker7063fbf2005-12-15 14:29:43 -08001/* -*- 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 Frederickf6b1fe72014-06-04 16:05:57 -070022 * kobject is Copyright (c) 2002-2003 Patrick Mochel
Joel Becker7063fbf2005-12-15 14:29:43 -080023 *
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
25 *
Paul Bolle395cf962011-08-15 02:02:26 +020026 * Please see the file Documentation/filesystems/configfs/configfs.txt for
Joel Becker7063fbf2005-12-15 14:29:43 -080027 * 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 Frederickf6b1fe72014-06-04 16:05:57 -070038static inline struct config_item *to_item(struct list_head *entry)
Joel Becker7063fbf2005-12-15 14:29:43 -080039{
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070040 return container_of(entry, struct config_item, ci_entry);
Joel Becker7063fbf2005-12-15 14:29:43 -080041}
42
43/* Evil kernel */
44static void config_item_release(struct kref *kref);
45
46/**
47 * config_item_init - initialize item.
48 * @item: item in question.
49 */
Fabian Frederick5286d202015-06-24 16:54:51 -070050static void config_item_init(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -080051{
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 Frederickf6b1fe72014-06-04 16:05:57 -070059 * @fmt: The vsnprintf()'s format string.
Joel Becker7063fbf2005-12-15 14:29:43 -080060 *
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 Frederickf6b1fe72014-06-04 16:05:57 -070065int config_item_set_name(struct config_item *item, const char *fmt, ...)
Joel Becker7063fbf2005-12-15 14:29:43 -080066{
67 int error = 0;
68 int limit = CONFIGFS_ITEM_NAME_LEN;
69 int need;
70 va_list args;
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070071 char *name;
Joel Becker7063fbf2005-12-15 14:29:43 -080072
73 /*
74 * First, try the static array
75 */
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070076 va_start(args, fmt);
77 need = vsnprintf(item->ci_namebuf, limit, fmt, args);
Joel Becker7063fbf2005-12-15 14:29:43 -080078 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 Frederickf6b1fe72014-06-04 16:05:57 -070086 name = kmalloc(limit, GFP_KERNEL);
Joel Becker7063fbf2005-12-15 14:29:43 -080087 if (!name) {
88 error = -ENOMEM;
89 goto Done;
90 }
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070091 va_start(args, fmt);
92 need = vsnprintf(name, limit, fmt, args);
Joel Becker7063fbf2005-12-15 14:29:43 -080093 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 Becker7063fbf2005-12-15 14:29:43 -0800112EXPORT_SYMBOL(config_item_set_name);
113
114void config_item_init_type_name(struct config_item *item,
115 const char *name,
116 struct config_item_type *type)
117{
118 config_item_set_name(item, name);
119 item->ci_type = type;
120 config_item_init(item);
121}
122EXPORT_SYMBOL(config_item_init_type_name);
123
124void config_group_init_type_name(struct config_group *group, const char *name,
125 struct config_item_type *type)
126{
127 config_item_set_name(&group->cg_item, name);
128 group->cg_item.ci_type = type;
129 config_group_init(group);
130}
131EXPORT_SYMBOL(config_group_init_type_name);
132
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700133struct config_item *config_item_get(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -0800134{
135 if (item)
136 kref_get(&item->ci_kref);
137 return item;
138}
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700139EXPORT_SYMBOL(config_item_get);
Joel Becker7063fbf2005-12-15 14:29:43 -0800140
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700141static void config_item_cleanup(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -0800142{
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700143 struct config_item_type *t = item->ci_type;
144 struct config_group *s = item->ci_group;
145 struct config_item *parent = item->ci_parent;
Joel Becker7063fbf2005-12-15 14:29:43 -0800146
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700147 pr_debug("config_item %s: cleaning up\n", config_item_name(item));
Joel Becker7063fbf2005-12-15 14:29:43 -0800148 if (item->ci_name != item->ci_namebuf)
149 kfree(item->ci_name);
150 item->ci_name = NULL;
151 if (t && t->ct_item_ops && t->ct_item_ops->release)
152 t->ct_item_ops->release(item);
153 if (s)
154 config_group_put(s);
155 if (parent)
156 config_item_put(parent);
157}
158
159static void config_item_release(struct kref *kref)
160{
161 config_item_cleanup(container_of(kref, struct config_item, ci_kref));
162}
163
164/**
165 * config_item_put - decrement refcount for item.
166 * @item: item.
167 *
168 * Decrement the refcount, and if 0, call config_item_cleanup().
169 */
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700170void config_item_put(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -0800171{
172 if (item)
173 kref_put(&item->ci_kref, config_item_release);
174}
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700175EXPORT_SYMBOL(config_item_put);
Joel Becker7063fbf2005-12-15 14:29:43 -0800176
Joel Becker7063fbf2005-12-15 14:29:43 -0800177/**
178 * config_group_init - initialize a group for use
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700179 * @group: config_group
Joel Becker7063fbf2005-12-15 14:29:43 -0800180 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800181void config_group_init(struct config_group *group)
182{
183 config_item_init(&group->cg_item);
184 INIT_LIST_HEAD(&group->cg_children);
185}
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700186EXPORT_SYMBOL(config_group_init);
Joel Becker7063fbf2005-12-15 14:29:43 -0800187
Joel Becker7063fbf2005-12-15 14:29:43 -0800188/**
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530189 * config_group_find_item - search for item in group.
Joel Becker7063fbf2005-12-15 14:29:43 -0800190 * @group: group we're looking in.
191 * @name: item's name.
192 *
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530193 * Iterate over @group->cg_list, looking for a matching config_item.
194 * If matching item is found take a reference and return the item.
195 * Caller must have locked group via @group->cg_subsys->su_mtx.
Joel Becker7063fbf2005-12-15 14:29:43 -0800196 */
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530197struct config_item *config_group_find_item(struct config_group *group,
198 const char *name)
Joel Becker7063fbf2005-12-15 14:29:43 -0800199{
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700200 struct list_head *entry;
201 struct config_item *ret = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800202
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700203 list_for_each(entry, &group->cg_children) {
204 struct config_item *item = to_item(entry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800205 if (config_item_name(item) &&
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530206 !strcmp(config_item_name(item), name)) {
Joel Becker7063fbf2005-12-15 14:29:43 -0800207 ret = config_item_get(item);
208 break;
209 }
210 }
211 return ret;
212}
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530213EXPORT_SYMBOL(config_group_find_item);