AVR32: Spinlock initializer cleanup
[linux-2.6.22.y-op.git] / fs / configfs / item.c
blob24421209f8544607072981c933ed5c6eb7425647
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * item.c - library routines for handling generic config items
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.
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.
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.
21 * Based on kobject:
22 * kobject is Copyright (c) 2002-2003 Patrick Mochel
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
26 * Please see the file Documentation/filesystems/configfs.txt for
27 * critical information about using the config_item interface.
30 #include <linux/string.h>
31 #include <linux/module.h>
32 #include <linux/stat.h>
33 #include <linux/slab.h>
35 #include <linux/configfs.h>
38 static inline struct config_item * to_item(struct list_head * entry)
40 return container_of(entry,struct config_item,ci_entry);
43 /* Evil kernel */
44 static void config_item_release(struct kref *kref);
46 /**
47 * config_item_init - initialize item.
48 * @item: item in question.
50 void config_item_init(struct config_item * item)
52 kref_init(&item->ci_kref);
53 INIT_LIST_HEAD(&item->ci_entry);
56 /**
57 * config_item_set_name - Set the name of an item
58 * @item: item.
59 * @name: name.
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.
66 int config_item_set_name(struct config_item * item, const char * fmt, ...)
68 int error = 0;
69 int limit = CONFIGFS_ITEM_NAME_LEN;
70 int need;
71 va_list args;
72 char * name;
75 * First, try the static array
77 va_start(args,fmt);
78 need = vsnprintf(item->ci_namebuf,limit,fmt,args);
79 va_end(args);
80 if (need < limit)
81 name = item->ci_namebuf;
82 else {
84 * Need more space? Allocate it and try again
86 limit = need + 1;
87 name = kmalloc(limit,GFP_KERNEL);
88 if (!name) {
89 error = -ENOMEM;
90 goto Done;
92 va_start(args,fmt);
93 need = vsnprintf(name,limit,fmt,args);
94 va_end(args);
96 /* Still? Give up. */
97 if (need >= limit) {
98 kfree(name);
99 error = -EFAULT;
100 goto Done;
104 /* Free the old name, if necessary. */
105 if (item->ci_name && item->ci_name != item->ci_namebuf)
106 kfree(item->ci_name);
108 /* Now, set the new name */
109 item->ci_name = name;
110 Done:
111 return error;
114 EXPORT_SYMBOL(config_item_set_name);
116 void config_item_init_type_name(struct config_item *item,
117 const char *name,
118 struct config_item_type *type)
120 config_item_set_name(item, name);
121 item->ci_type = type;
122 config_item_init(item);
124 EXPORT_SYMBOL(config_item_init_type_name);
126 void config_group_init_type_name(struct config_group *group, const char *name,
127 struct config_item_type *type)
129 config_item_set_name(&group->cg_item, name);
130 group->cg_item.ci_type = type;
131 config_group_init(group);
133 EXPORT_SYMBOL(config_group_init_type_name);
135 struct config_item * config_item_get(struct config_item * item)
137 if (item)
138 kref_get(&item->ci_kref);
139 return item;
143 * config_item_cleanup - free config_item resources.
144 * @item: item.
147 void config_item_cleanup(struct config_item * item)
149 struct config_item_type * t = item->ci_type;
150 struct config_group * s = item->ci_group;
151 struct config_item * parent = item->ci_parent;
153 pr_debug("config_item %s: cleaning up\n",config_item_name(item));
154 if (item->ci_name != item->ci_namebuf)
155 kfree(item->ci_name);
156 item->ci_name = NULL;
157 if (t && t->ct_item_ops && t->ct_item_ops->release)
158 t->ct_item_ops->release(item);
159 if (s)
160 config_group_put(s);
161 if (parent)
162 config_item_put(parent);
165 static void config_item_release(struct kref *kref)
167 config_item_cleanup(container_of(kref, struct config_item, ci_kref));
171 * config_item_put - decrement refcount for item.
172 * @item: item.
174 * Decrement the refcount, and if 0, call config_item_cleanup().
176 void config_item_put(struct config_item * item)
178 if (item)
179 kref_put(&item->ci_kref, config_item_release);
184 * config_group_init - initialize a group for use
185 * @k: group
188 void config_group_init(struct config_group *group)
190 config_item_init(&group->cg_item);
191 INIT_LIST_HEAD(&group->cg_children);
196 * config_group_find_obj - search for item in group.
197 * @group: group we're looking in.
198 * @name: item's name.
200 * Lock group via @group->cg_subsys, and iterate over @group->cg_list,
201 * looking for a matching config_item. If matching item is found
202 * take a reference and return the item.
205 struct config_item * config_group_find_obj(struct config_group * group, const char * name)
207 struct list_head * entry;
208 struct config_item * ret = NULL;
210 /* XXX LOCKING! */
211 list_for_each(entry,&group->cg_children) {
212 struct config_item * item = to_item(entry);
213 if (config_item_name(item) &&
214 !strcmp(config_item_name(item), name)) {
215 ret = config_item_get(item);
216 break;
219 return ret;
223 EXPORT_SYMBOL(config_item_init);
224 EXPORT_SYMBOL(config_group_init);
225 EXPORT_SYMBOL(config_item_get);
226 EXPORT_SYMBOL(config_item_put);
227 EXPORT_SYMBOL(config_group_find_obj);