blob: e65f9ffbb99951ba83843a2836fb311f5e6af034 [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 Frederickf6b1fe72014-06-04 16:05:57 -070050void 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}
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070055EXPORT_SYMBOL(config_item_init);
Joel Becker7063fbf2005-12-15 14:29:43 -080056
57/**
58 * config_item_set_name - Set the name of an item
59 * @item: item.
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070060 * @fmt: The vsnprintf()'s format string.
Joel Becker7063fbf2005-12-15 14:29:43 -080061 *
62 * If strlen(name) >= CONFIGFS_ITEM_NAME_LEN, then use a
63 * dynamically allocated string that @item->ci_name points to.
64 * Otherwise, use the static @item->ci_namebuf array.
65 */
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070066int config_item_set_name(struct config_item *item, const char *fmt, ...)
Joel Becker7063fbf2005-12-15 14:29:43 -080067{
68 int error = 0;
69 int limit = CONFIGFS_ITEM_NAME_LEN;
70 int need;
71 va_list args;
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070072 char *name;
Joel Becker7063fbf2005-12-15 14:29:43 -080073
74 /*
75 * First, try the static array
76 */
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070077 va_start(args, fmt);
78 need = vsnprintf(item->ci_namebuf, limit, fmt, args);
Joel Becker7063fbf2005-12-15 14:29:43 -080079 va_end(args);
80 if (need < limit)
81 name = item->ci_namebuf;
82 else {
83 /*
84 * Need more space? Allocate it and try again
85 */
86 limit = need + 1;
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070087 name = kmalloc(limit, GFP_KERNEL);
Joel Becker7063fbf2005-12-15 14:29:43 -080088 if (!name) {
89 error = -ENOMEM;
90 goto Done;
91 }
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070092 va_start(args, fmt);
93 need = vsnprintf(name, limit, fmt, args);
Joel Becker7063fbf2005-12-15 14:29:43 -080094 va_end(args);
95
96 /* Still? Give up. */
97 if (need >= limit) {
98 kfree(name);
99 error = -EFAULT;
100 goto Done;
101 }
102 }
103
104 /* Free the old name, if necessary. */
105 if (item->ci_name && item->ci_name != item->ci_namebuf)
106 kfree(item->ci_name);
107
108 /* Now, set the new name */
109 item->ci_name = name;
110 Done:
111 return error;
112}
Joel Becker7063fbf2005-12-15 14:29:43 -0800113EXPORT_SYMBOL(config_item_set_name);
114
115void config_item_init_type_name(struct config_item *item,
116 const char *name,
117 struct config_item_type *type)
118{
119 config_item_set_name(item, name);
120 item->ci_type = type;
121 config_item_init(item);
122}
123EXPORT_SYMBOL(config_item_init_type_name);
124
125void config_group_init_type_name(struct config_group *group, const char *name,
126 struct config_item_type *type)
127{
128 config_item_set_name(&group->cg_item, name);
129 group->cg_item.ci_type = type;
130 config_group_init(group);
131}
132EXPORT_SYMBOL(config_group_init_type_name);
133
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700134struct config_item *config_item_get(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -0800135{
136 if (item)
137 kref_get(&item->ci_kref);
138 return item;
139}
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700140EXPORT_SYMBOL(config_item_get);
Joel Becker7063fbf2005-12-15 14:29:43 -0800141
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700142static void config_item_cleanup(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -0800143{
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700144 struct config_item_type *t = item->ci_type;
145 struct config_group *s = item->ci_group;
146 struct config_item *parent = item->ci_parent;
Joel Becker7063fbf2005-12-15 14:29:43 -0800147
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700148 pr_debug("config_item %s: cleaning up\n", config_item_name(item));
Joel Becker7063fbf2005-12-15 14:29:43 -0800149 if (item->ci_name != item->ci_namebuf)
150 kfree(item->ci_name);
151 item->ci_name = NULL;
152 if (t && t->ct_item_ops && t->ct_item_ops->release)
153 t->ct_item_ops->release(item);
154 if (s)
155 config_group_put(s);
156 if (parent)
157 config_item_put(parent);
158}
159
160static void config_item_release(struct kref *kref)
161{
162 config_item_cleanup(container_of(kref, struct config_item, ci_kref));
163}
164
165/**
166 * config_item_put - decrement refcount for item.
167 * @item: item.
168 *
169 * Decrement the refcount, and if 0, call config_item_cleanup().
170 */
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700171void config_item_put(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -0800172{
173 if (item)
174 kref_put(&item->ci_kref, config_item_release);
175}
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700176EXPORT_SYMBOL(config_item_put);
Joel Becker7063fbf2005-12-15 14:29:43 -0800177
Joel Becker7063fbf2005-12-15 14:29:43 -0800178/**
179 * config_group_init - initialize a group for use
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700180 * @group: config_group
Joel Becker7063fbf2005-12-15 14:29:43 -0800181 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800182void config_group_init(struct config_group *group)
183{
184 config_item_init(&group->cg_item);
185 INIT_LIST_HEAD(&group->cg_children);
186}
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700187EXPORT_SYMBOL(config_group_init);
Joel Becker7063fbf2005-12-15 14:29:43 -0800188
Joel Becker7063fbf2005-12-15 14:29:43 -0800189/**
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530190 * config_group_find_item - search for item in group.
Joel Becker7063fbf2005-12-15 14:29:43 -0800191 * @group: group we're looking in.
192 * @name: item's name.
193 *
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530194 * Iterate over @group->cg_list, looking for a matching config_item.
195 * If matching item is found take a reference and return the item.
196 * Caller must have locked group via @group->cg_subsys->su_mtx.
Joel Becker7063fbf2005-12-15 14:29:43 -0800197 */
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530198struct config_item *config_group_find_item(struct config_group *group,
199 const char *name)
Joel Becker7063fbf2005-12-15 14:29:43 -0800200{
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700201 struct list_head *entry;
202 struct config_item *ret = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800203
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700204 list_for_each(entry, &group->cg_children) {
205 struct config_item *item = to_item(entry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800206 if (config_item_name(item) &&
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530207 !strcmp(config_item_name(item), name)) {
Joel Becker7063fbf2005-12-15 14:29:43 -0800208 ret = config_item_get(item);
209 break;
210 }
211 }
212 return ret;
213}
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530214EXPORT_SYMBOL(config_group_find_item);