1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
38static inline struct config_item *to_item(struct list_head *entry)
39{
40 return container_of(entry, struct config_item, ci_entry);
41}
42
43
44static void config_item_release(struct kref *kref);
45
46
47
48
49
50static void config_item_init(struct config_item *item)
51{
52 kref_init(&item->ci_kref);
53 INIT_LIST_HEAD(&item->ci_entry);
54}
55
56
57
58
59
60
61
62
63
64
65int config_item_set_name(struct config_item *item, const char *fmt, ...)
66{
67 int error = 0;
68 int limit = CONFIGFS_ITEM_NAME_LEN;
69 int need;
70 va_list args;
71 char *name;
72
73
74
75
76 va_start(args, fmt);
77 need = vsnprintf(item->ci_namebuf, limit, fmt, args);
78 va_end(args);
79 if (need < limit)
80 name = item->ci_namebuf;
81 else {
82
83
84
85 limit = need + 1;
86 name = kmalloc(limit, GFP_KERNEL);
87 if (!name) {
88 error = -ENOMEM;
89 goto Done;
90 }
91 va_start(args, fmt);
92 need = vsnprintf(name, limit, fmt, args);
93 va_end(args);
94
95
96 if (need >= limit) {
97 kfree(name);
98 error = -EFAULT;
99 goto Done;
100 }
101 }
102
103
104 if (item->ci_name && item->ci_name != item->ci_namebuf)
105 kfree(item->ci_name);
106
107
108 item->ci_name = name;
109 Done:
110 return error;
111}
112EXPORT_SYMBOL(config_item_set_name);
113
114void config_item_init_type_name(struct config_item *item,
115 const char *name,
116 const struct config_item_type *type)
117{
118 config_item_set_name(item, "%s", 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 const struct config_item_type *type)
126{
127 config_item_set_name(&group->cg_item, "%s", name);
128 group->cg_item.ci_type = type;
129 config_group_init(group);
130}
131EXPORT_SYMBOL(config_group_init_type_name);
132
133struct config_item *config_item_get(struct config_item *item)
134{
135 if (item)
136 kref_get(&item->ci_kref);
137 return item;
138}
139EXPORT_SYMBOL(config_item_get);
140
141struct 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}
147EXPORT_SYMBOL(config_item_get_unless_zero);
148
149static void config_item_cleanup(struct config_item *item)
150{
151 const struct config_item_type *t = item->ci_type;
152 struct config_group *s = item->ci_group;
153 struct config_item *parent = item->ci_parent;
154
155 pr_debug("config_item %s: cleaning up\n", config_item_name(item));
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
167static void config_item_release(struct kref *kref)
168{
169 config_item_cleanup(container_of(kref, struct config_item, ci_kref));
170}
171
172
173
174
175
176
177
178void config_item_put(struct config_item *item)
179{
180 if (item)
181 kref_put(&item->ci_kref, config_item_release);
182}
183EXPORT_SYMBOL(config_item_put);
184
185
186
187
188
189void config_group_init(struct config_group *group)
190{
191 config_item_init(&group->cg_item);
192 INIT_LIST_HEAD(&group->cg_children);
193 INIT_LIST_HEAD(&group->default_groups);
194}
195EXPORT_SYMBOL(config_group_init);
196
197
198
199
200
201
202
203
204
205
206struct config_item *config_group_find_item(struct config_group *group,
207 const char *name)
208{
209 struct list_head *entry;
210 struct config_item *ret = NULL;
211
212 list_for_each(entry, &group->cg_children) {
213 struct config_item *item = to_item(entry);
214 if (config_item_name(item) &&
215 !strcmp(config_item_name(item), name)) {
216 ret = config_item_get(item);
217 break;
218 }
219 }
220 return ret;
221}
222EXPORT_SYMBOL(config_group_find_item);
223