Commit eee031649707db3c9920d9498f8d03819b74fc23

Authored by Jeff Mahoney
Committed by Greg Kroah-Hartman
1 parent 1296fc02c2

kobject: introduce kobj_completion

A common way to handle kobject lifetimes in embedded in objects with
different lifetime rules is to pair the kobject with a struct completion.

This introduces a kobj_completion structure that can be used in place
of the pairing, along with several convenience functions for
initialization, release, and put-and-wait.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Showing 2 changed files with 68 additions and 0 deletions Inline Diff

include/linux/kobj_completion.h
File was created 1 #ifndef _KOBJ_COMPLETION_H_
2 #define _KOBJ_COMPLETION_H_
3
4 #include <linux/kobject.h>
5 #include <linux/completion.h>
6
7 struct kobj_completion {
8 struct kobject kc_kobj;
9 struct completion kc_unregister;
10 };
11
12 #define kobj_to_kobj_completion(kobj) \
13 container_of(kobj, struct kobj_completion, kc_kobj)
14
15 void kobj_completion_init(struct kobj_completion *kc, struct kobj_type *ktype);
16 void kobj_completion_release(struct kobject *kobj);
17 void kobj_completion_del_and_wait(struct kobj_completion *kc);
18 #endif /* _KOBJ_COMPLETION_H_ */
19
1 /* 1 /*
2 * kobject.c - library routines for handling generic kernel objects 2 * kobject.c - library routines for handling generic kernel objects
3 * 3 *
4 * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org> 4 * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
5 * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com> 5 * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (c) 2006-2007 Novell Inc. 6 * Copyright (c) 2006-2007 Novell Inc.
7 * 7 *
8 * This file is released under the GPLv2. 8 * This file is released under the GPLv2.
9 * 9 *
10 * 10 *
11 * Please see the file Documentation/kobject.txt for critical information 11 * Please see the file Documentation/kobject.txt for critical information
12 * about using the kobject interface. 12 * about using the kobject interface.
13 */ 13 */
14 14
15 #include <linux/kobject.h> 15 #include <linux/kobject.h>
16 #include <linux/kobj_completion.h>
16 #include <linux/string.h> 17 #include <linux/string.h>
17 #include <linux/export.h> 18 #include <linux/export.h>
18 #include <linux/stat.h> 19 #include <linux/stat.h>
19 #include <linux/slab.h> 20 #include <linux/slab.h>
20 21
21 /** 22 /**
22 * kobject_namespace - return @kobj's namespace tag 23 * kobject_namespace - return @kobj's namespace tag
23 * @kobj: kobject in question 24 * @kobj: kobject in question
24 * 25 *
25 * Returns namespace tag of @kobj if its parent has namespace ops enabled 26 * Returns namespace tag of @kobj if its parent has namespace ops enabled
26 * and thus @kobj should have a namespace tag associated with it. Returns 27 * and thus @kobj should have a namespace tag associated with it. Returns
27 * %NULL otherwise. 28 * %NULL otherwise.
28 */ 29 */
29 const void *kobject_namespace(struct kobject *kobj) 30 const void *kobject_namespace(struct kobject *kobj)
30 { 31 {
31 const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj); 32 const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
32 const void *ns; 33 const void *ns;
33 34
34 if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE) 35 if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
35 return NULL; 36 return NULL;
36 37
37 ns = kobj->ktype->namespace(kobj); 38 ns = kobj->ktype->namespace(kobj);
38 WARN_ON(!ns); /* @kobj in a namespace is required to have !NULL tag */ 39 WARN_ON(!ns); /* @kobj in a namespace is required to have !NULL tag */
39 return ns; 40 return ns;
40 } 41 }
41 42
42 /* 43 /*
43 * populate_dir - populate directory with attributes. 44 * populate_dir - populate directory with attributes.
44 * @kobj: object we're working on. 45 * @kobj: object we're working on.
45 * 46 *
46 * Most subsystems have a set of default attributes that are associated 47 * Most subsystems have a set of default attributes that are associated
47 * with an object that registers with them. This is a helper called during 48 * with an object that registers with them. This is a helper called during
48 * object registration that loops through the default attributes of the 49 * object registration that loops through the default attributes of the
49 * subsystem and creates attributes files for them in sysfs. 50 * subsystem and creates attributes files for them in sysfs.
50 */ 51 */
51 static int populate_dir(struct kobject *kobj) 52 static int populate_dir(struct kobject *kobj)
52 { 53 {
53 struct kobj_type *t = get_ktype(kobj); 54 struct kobj_type *t = get_ktype(kobj);
54 struct attribute *attr; 55 struct attribute *attr;
55 int error = 0; 56 int error = 0;
56 int i; 57 int i;
57 58
58 if (t && t->default_attrs) { 59 if (t && t->default_attrs) {
59 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) { 60 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
60 error = sysfs_create_file(kobj, attr); 61 error = sysfs_create_file(kobj, attr);
61 if (error) 62 if (error)
62 break; 63 break;
63 } 64 }
64 } 65 }
65 return error; 66 return error;
66 } 67 }
67 68
68 static int create_dir(struct kobject *kobj) 69 static int create_dir(struct kobject *kobj)
69 { 70 {
70 int error; 71 int error;
71 72
72 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj)); 73 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
73 if (!error) { 74 if (!error) {
74 error = populate_dir(kobj); 75 error = populate_dir(kobj);
75 if (error) 76 if (error)
76 sysfs_remove_dir(kobj); 77 sysfs_remove_dir(kobj);
77 } 78 }
78 return error; 79 return error;
79 } 80 }
80 81
81 static int get_kobj_path_length(struct kobject *kobj) 82 static int get_kobj_path_length(struct kobject *kobj)
82 { 83 {
83 int length = 1; 84 int length = 1;
84 struct kobject *parent = kobj; 85 struct kobject *parent = kobj;
85 86
86 /* walk up the ancestors until we hit the one pointing to the 87 /* walk up the ancestors until we hit the one pointing to the
87 * root. 88 * root.
88 * Add 1 to strlen for leading '/' of each level. 89 * Add 1 to strlen for leading '/' of each level.
89 */ 90 */
90 do { 91 do {
91 if (kobject_name(parent) == NULL) 92 if (kobject_name(parent) == NULL)
92 return 0; 93 return 0;
93 length += strlen(kobject_name(parent)) + 1; 94 length += strlen(kobject_name(parent)) + 1;
94 parent = parent->parent; 95 parent = parent->parent;
95 } while (parent); 96 } while (parent);
96 return length; 97 return length;
97 } 98 }
98 99
99 static void fill_kobj_path(struct kobject *kobj, char *path, int length) 100 static void fill_kobj_path(struct kobject *kobj, char *path, int length)
100 { 101 {
101 struct kobject *parent; 102 struct kobject *parent;
102 103
103 --length; 104 --length;
104 for (parent = kobj; parent; parent = parent->parent) { 105 for (parent = kobj; parent; parent = parent->parent) {
105 int cur = strlen(kobject_name(parent)); 106 int cur = strlen(kobject_name(parent));
106 /* back up enough to print this name with '/' */ 107 /* back up enough to print this name with '/' */
107 length -= cur; 108 length -= cur;
108 strncpy(path + length, kobject_name(parent), cur); 109 strncpy(path + length, kobject_name(parent), cur);
109 *(path + --length) = '/'; 110 *(path + --length) = '/';
110 } 111 }
111 112
112 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj), 113 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
113 kobj, __func__, path); 114 kobj, __func__, path);
114 } 115 }
115 116
116 /** 117 /**
117 * kobject_get_path - generate and return the path associated with a given kobj and kset pair. 118 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
118 * 119 *
119 * @kobj: kobject in question, with which to build the path 120 * @kobj: kobject in question, with which to build the path
120 * @gfp_mask: the allocation type used to allocate the path 121 * @gfp_mask: the allocation type used to allocate the path
121 * 122 *
122 * The result must be freed by the caller with kfree(). 123 * The result must be freed by the caller with kfree().
123 */ 124 */
124 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask) 125 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
125 { 126 {
126 char *path; 127 char *path;
127 int len; 128 int len;
128 129
129 len = get_kobj_path_length(kobj); 130 len = get_kobj_path_length(kobj);
130 if (len == 0) 131 if (len == 0)
131 return NULL; 132 return NULL;
132 path = kzalloc(len, gfp_mask); 133 path = kzalloc(len, gfp_mask);
133 if (!path) 134 if (!path)
134 return NULL; 135 return NULL;
135 fill_kobj_path(kobj, path, len); 136 fill_kobj_path(kobj, path, len);
136 137
137 return path; 138 return path;
138 } 139 }
139 EXPORT_SYMBOL_GPL(kobject_get_path); 140 EXPORT_SYMBOL_GPL(kobject_get_path);
140 141
141 /* add the kobject to its kset's list */ 142 /* add the kobject to its kset's list */
142 static void kobj_kset_join(struct kobject *kobj) 143 static void kobj_kset_join(struct kobject *kobj)
143 { 144 {
144 if (!kobj->kset) 145 if (!kobj->kset)
145 return; 146 return;
146 147
147 kset_get(kobj->kset); 148 kset_get(kobj->kset);
148 spin_lock(&kobj->kset->list_lock); 149 spin_lock(&kobj->kset->list_lock);
149 list_add_tail(&kobj->entry, &kobj->kset->list); 150 list_add_tail(&kobj->entry, &kobj->kset->list);
150 spin_unlock(&kobj->kset->list_lock); 151 spin_unlock(&kobj->kset->list_lock);
151 } 152 }
152 153
153 /* remove the kobject from its kset's list */ 154 /* remove the kobject from its kset's list */
154 static void kobj_kset_leave(struct kobject *kobj) 155 static void kobj_kset_leave(struct kobject *kobj)
155 { 156 {
156 if (!kobj->kset) 157 if (!kobj->kset)
157 return; 158 return;
158 159
159 spin_lock(&kobj->kset->list_lock); 160 spin_lock(&kobj->kset->list_lock);
160 list_del_init(&kobj->entry); 161 list_del_init(&kobj->entry);
161 spin_unlock(&kobj->kset->list_lock); 162 spin_unlock(&kobj->kset->list_lock);
162 kset_put(kobj->kset); 163 kset_put(kobj->kset);
163 } 164 }
164 165
165 static void kobject_init_internal(struct kobject *kobj) 166 static void kobject_init_internal(struct kobject *kobj)
166 { 167 {
167 if (!kobj) 168 if (!kobj)
168 return; 169 return;
169 kref_init(&kobj->kref); 170 kref_init(&kobj->kref);
170 INIT_LIST_HEAD(&kobj->entry); 171 INIT_LIST_HEAD(&kobj->entry);
171 kobj->state_in_sysfs = 0; 172 kobj->state_in_sysfs = 0;
172 kobj->state_add_uevent_sent = 0; 173 kobj->state_add_uevent_sent = 0;
173 kobj->state_remove_uevent_sent = 0; 174 kobj->state_remove_uevent_sent = 0;
174 kobj->state_initialized = 1; 175 kobj->state_initialized = 1;
175 } 176 }
176 177
177 178
178 static int kobject_add_internal(struct kobject *kobj) 179 static int kobject_add_internal(struct kobject *kobj)
179 { 180 {
180 int error = 0; 181 int error = 0;
181 struct kobject *parent; 182 struct kobject *parent;
182 183
183 if (!kobj) 184 if (!kobj)
184 return -ENOENT; 185 return -ENOENT;
185 186
186 if (!kobj->name || !kobj->name[0]) { 187 if (!kobj->name || !kobj->name[0]) {
187 WARN(1, "kobject: (%p): attempted to be registered with empty " 188 WARN(1, "kobject: (%p): attempted to be registered with empty "
188 "name!\n", kobj); 189 "name!\n", kobj);
189 return -EINVAL; 190 return -EINVAL;
190 } 191 }
191 192
192 parent = kobject_get(kobj->parent); 193 parent = kobject_get(kobj->parent);
193 194
194 /* join kset if set, use it as parent if we do not already have one */ 195 /* join kset if set, use it as parent if we do not already have one */
195 if (kobj->kset) { 196 if (kobj->kset) {
196 if (!parent) 197 if (!parent)
197 parent = kobject_get(&kobj->kset->kobj); 198 parent = kobject_get(&kobj->kset->kobj);
198 kobj_kset_join(kobj); 199 kobj_kset_join(kobj);
199 kobj->parent = parent; 200 kobj->parent = parent;
200 } 201 }
201 202
202 pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n", 203 pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
203 kobject_name(kobj), kobj, __func__, 204 kobject_name(kobj), kobj, __func__,
204 parent ? kobject_name(parent) : "<NULL>", 205 parent ? kobject_name(parent) : "<NULL>",
205 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>"); 206 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
206 207
207 error = create_dir(kobj); 208 error = create_dir(kobj);
208 if (error) { 209 if (error) {
209 kobj_kset_leave(kobj); 210 kobj_kset_leave(kobj);
210 kobject_put(parent); 211 kobject_put(parent);
211 kobj->parent = NULL; 212 kobj->parent = NULL;
212 213
213 /* be noisy on error issues */ 214 /* be noisy on error issues */
214 if (error == -EEXIST) 215 if (error == -EEXIST)
215 WARN(1, "%s failed for %s with " 216 WARN(1, "%s failed for %s with "
216 "-EEXIST, don't try to register things with " 217 "-EEXIST, don't try to register things with "
217 "the same name in the same directory.\n", 218 "the same name in the same directory.\n",
218 __func__, kobject_name(kobj)); 219 __func__, kobject_name(kobj));
219 else 220 else
220 WARN(1, "%s failed for %s (error: %d parent: %s)\n", 221 WARN(1, "%s failed for %s (error: %d parent: %s)\n",
221 __func__, kobject_name(kobj), error, 222 __func__, kobject_name(kobj), error,
222 parent ? kobject_name(parent) : "'none'"); 223 parent ? kobject_name(parent) : "'none'");
223 } else 224 } else
224 kobj->state_in_sysfs = 1; 225 kobj->state_in_sysfs = 1;
225 226
226 return error; 227 return error;
227 } 228 }
228 229
229 /** 230 /**
230 * kobject_set_name_vargs - Set the name of an kobject 231 * kobject_set_name_vargs - Set the name of an kobject
231 * @kobj: struct kobject to set the name of 232 * @kobj: struct kobject to set the name of
232 * @fmt: format string used to build the name 233 * @fmt: format string used to build the name
233 * @vargs: vargs to format the string. 234 * @vargs: vargs to format the string.
234 */ 235 */
235 int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, 236 int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
236 va_list vargs) 237 va_list vargs)
237 { 238 {
238 const char *old_name = kobj->name; 239 const char *old_name = kobj->name;
239 char *s; 240 char *s;
240 241
241 if (kobj->name && !fmt) 242 if (kobj->name && !fmt)
242 return 0; 243 return 0;
243 244
244 kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs); 245 kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
245 if (!kobj->name) 246 if (!kobj->name)
246 return -ENOMEM; 247 return -ENOMEM;
247 248
248 /* ewww... some of these buggers have '/' in the name ... */ 249 /* ewww... some of these buggers have '/' in the name ... */
249 while ((s = strchr(kobj->name, '/'))) 250 while ((s = strchr(kobj->name, '/')))
250 s[0] = '!'; 251 s[0] = '!';
251 252
252 kfree(old_name); 253 kfree(old_name);
253 return 0; 254 return 0;
254 } 255 }
255 256
256 /** 257 /**
257 * kobject_set_name - Set the name of a kobject 258 * kobject_set_name - Set the name of a kobject
258 * @kobj: struct kobject to set the name of 259 * @kobj: struct kobject to set the name of
259 * @fmt: format string used to build the name 260 * @fmt: format string used to build the name
260 * 261 *
261 * This sets the name of the kobject. If you have already added the 262 * This sets the name of the kobject. If you have already added the
262 * kobject to the system, you must call kobject_rename() in order to 263 * kobject to the system, you must call kobject_rename() in order to
263 * change the name of the kobject. 264 * change the name of the kobject.
264 */ 265 */
265 int kobject_set_name(struct kobject *kobj, const char *fmt, ...) 266 int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
266 { 267 {
267 va_list vargs; 268 va_list vargs;
268 int retval; 269 int retval;
269 270
270 va_start(vargs, fmt); 271 va_start(vargs, fmt);
271 retval = kobject_set_name_vargs(kobj, fmt, vargs); 272 retval = kobject_set_name_vargs(kobj, fmt, vargs);
272 va_end(vargs); 273 va_end(vargs);
273 274
274 return retval; 275 return retval;
275 } 276 }
276 EXPORT_SYMBOL(kobject_set_name); 277 EXPORT_SYMBOL(kobject_set_name);
277 278
278 /** 279 /**
279 * kobject_init - initialize a kobject structure 280 * kobject_init - initialize a kobject structure
280 * @kobj: pointer to the kobject to initialize 281 * @kobj: pointer to the kobject to initialize
281 * @ktype: pointer to the ktype for this kobject. 282 * @ktype: pointer to the ktype for this kobject.
282 * 283 *
283 * This function will properly initialize a kobject such that it can then 284 * This function will properly initialize a kobject such that it can then
284 * be passed to the kobject_add() call. 285 * be passed to the kobject_add() call.
285 * 286 *
286 * After this function is called, the kobject MUST be cleaned up by a call 287 * After this function is called, the kobject MUST be cleaned up by a call
287 * to kobject_put(), not by a call to kfree directly to ensure that all of 288 * to kobject_put(), not by a call to kfree directly to ensure that all of
288 * the memory is cleaned up properly. 289 * the memory is cleaned up properly.
289 */ 290 */
290 void kobject_init(struct kobject *kobj, struct kobj_type *ktype) 291 void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
291 { 292 {
292 char *err_str; 293 char *err_str;
293 294
294 if (!kobj) { 295 if (!kobj) {
295 err_str = "invalid kobject pointer!"; 296 err_str = "invalid kobject pointer!";
296 goto error; 297 goto error;
297 } 298 }
298 if (!ktype) { 299 if (!ktype) {
299 err_str = "must have a ktype to be initialized properly!\n"; 300 err_str = "must have a ktype to be initialized properly!\n";
300 goto error; 301 goto error;
301 } 302 }
302 if (kobj->state_initialized) { 303 if (kobj->state_initialized) {
303 /* do not error out as sometimes we can recover */ 304 /* do not error out as sometimes we can recover */
304 printk(KERN_ERR "kobject (%p): tried to init an initialized " 305 printk(KERN_ERR "kobject (%p): tried to init an initialized "
305 "object, something is seriously wrong.\n", kobj); 306 "object, something is seriously wrong.\n", kobj);
306 dump_stack(); 307 dump_stack();
307 } 308 }
308 309
309 kobject_init_internal(kobj); 310 kobject_init_internal(kobj);
310 kobj->ktype = ktype; 311 kobj->ktype = ktype;
311 return; 312 return;
312 313
313 error: 314 error:
314 printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str); 315 printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
315 dump_stack(); 316 dump_stack();
316 } 317 }
317 EXPORT_SYMBOL(kobject_init); 318 EXPORT_SYMBOL(kobject_init);
318 319
319 static int kobject_add_varg(struct kobject *kobj, struct kobject *parent, 320 static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
320 const char *fmt, va_list vargs) 321 const char *fmt, va_list vargs)
321 { 322 {
322 int retval; 323 int retval;
323 324
324 retval = kobject_set_name_vargs(kobj, fmt, vargs); 325 retval = kobject_set_name_vargs(kobj, fmt, vargs);
325 if (retval) { 326 if (retval) {
326 printk(KERN_ERR "kobject: can not set name properly!\n"); 327 printk(KERN_ERR "kobject: can not set name properly!\n");
327 return retval; 328 return retval;
328 } 329 }
329 kobj->parent = parent; 330 kobj->parent = parent;
330 return kobject_add_internal(kobj); 331 return kobject_add_internal(kobj);
331 } 332 }
332 333
333 /** 334 /**
334 * kobject_add - the main kobject add function 335 * kobject_add - the main kobject add function
335 * @kobj: the kobject to add 336 * @kobj: the kobject to add
336 * @parent: pointer to the parent of the kobject. 337 * @parent: pointer to the parent of the kobject.
337 * @fmt: format to name the kobject with. 338 * @fmt: format to name the kobject with.
338 * 339 *
339 * The kobject name is set and added to the kobject hierarchy in this 340 * The kobject name is set and added to the kobject hierarchy in this
340 * function. 341 * function.
341 * 342 *
342 * If @parent is set, then the parent of the @kobj will be set to it. 343 * If @parent is set, then the parent of the @kobj will be set to it.
343 * If @parent is NULL, then the parent of the @kobj will be set to the 344 * If @parent is NULL, then the parent of the @kobj will be set to the
344 * kobject associted with the kset assigned to this kobject. If no kset 345 * kobject associted with the kset assigned to this kobject. If no kset
345 * is assigned to the kobject, then the kobject will be located in the 346 * is assigned to the kobject, then the kobject will be located in the
346 * root of the sysfs tree. 347 * root of the sysfs tree.
347 * 348 *
348 * If this function returns an error, kobject_put() must be called to 349 * If this function returns an error, kobject_put() must be called to
349 * properly clean up the memory associated with the object. 350 * properly clean up the memory associated with the object.
350 * Under no instance should the kobject that is passed to this function 351 * Under no instance should the kobject that is passed to this function
351 * be directly freed with a call to kfree(), that can leak memory. 352 * be directly freed with a call to kfree(), that can leak memory.
352 * 353 *
353 * Note, no "add" uevent will be created with this call, the caller should set 354 * Note, no "add" uevent will be created with this call, the caller should set
354 * up all of the necessary sysfs files for the object and then call 355 * up all of the necessary sysfs files for the object and then call
355 * kobject_uevent() with the UEVENT_ADD parameter to ensure that 356 * kobject_uevent() with the UEVENT_ADD parameter to ensure that
356 * userspace is properly notified of this kobject's creation. 357 * userspace is properly notified of this kobject's creation.
357 */ 358 */
358 int kobject_add(struct kobject *kobj, struct kobject *parent, 359 int kobject_add(struct kobject *kobj, struct kobject *parent,
359 const char *fmt, ...) 360 const char *fmt, ...)
360 { 361 {
361 va_list args; 362 va_list args;
362 int retval; 363 int retval;
363 364
364 if (!kobj) 365 if (!kobj)
365 return -EINVAL; 366 return -EINVAL;
366 367
367 if (!kobj->state_initialized) { 368 if (!kobj->state_initialized) {
368 printk(KERN_ERR "kobject '%s' (%p): tried to add an " 369 printk(KERN_ERR "kobject '%s' (%p): tried to add an "
369 "uninitialized object, something is seriously wrong.\n", 370 "uninitialized object, something is seriously wrong.\n",
370 kobject_name(kobj), kobj); 371 kobject_name(kobj), kobj);
371 dump_stack(); 372 dump_stack();
372 return -EINVAL; 373 return -EINVAL;
373 } 374 }
374 va_start(args, fmt); 375 va_start(args, fmt);
375 retval = kobject_add_varg(kobj, parent, fmt, args); 376 retval = kobject_add_varg(kobj, parent, fmt, args);
376 va_end(args); 377 va_end(args);
377 378
378 return retval; 379 return retval;
379 } 380 }
380 EXPORT_SYMBOL(kobject_add); 381 EXPORT_SYMBOL(kobject_add);
381 382
382 /** 383 /**
383 * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy 384 * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
384 * @kobj: pointer to the kobject to initialize 385 * @kobj: pointer to the kobject to initialize
385 * @ktype: pointer to the ktype for this kobject. 386 * @ktype: pointer to the ktype for this kobject.
386 * @parent: pointer to the parent of this kobject. 387 * @parent: pointer to the parent of this kobject.
387 * @fmt: the name of the kobject. 388 * @fmt: the name of the kobject.
388 * 389 *
389 * This function combines the call to kobject_init() and 390 * This function combines the call to kobject_init() and
390 * kobject_add(). The same type of error handling after a call to 391 * kobject_add(). The same type of error handling after a call to
391 * kobject_add() and kobject lifetime rules are the same here. 392 * kobject_add() and kobject lifetime rules are the same here.
392 */ 393 */
393 int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, 394 int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
394 struct kobject *parent, const char *fmt, ...) 395 struct kobject *parent, const char *fmt, ...)
395 { 396 {
396 va_list args; 397 va_list args;
397 int retval; 398 int retval;
398 399
399 kobject_init(kobj, ktype); 400 kobject_init(kobj, ktype);
400 401
401 va_start(args, fmt); 402 va_start(args, fmt);
402 retval = kobject_add_varg(kobj, parent, fmt, args); 403 retval = kobject_add_varg(kobj, parent, fmt, args);
403 va_end(args); 404 va_end(args);
404 405
405 return retval; 406 return retval;
406 } 407 }
407 EXPORT_SYMBOL_GPL(kobject_init_and_add); 408 EXPORT_SYMBOL_GPL(kobject_init_and_add);
408 409
409 /** 410 /**
410 * kobject_rename - change the name of an object 411 * kobject_rename - change the name of an object
411 * @kobj: object in question. 412 * @kobj: object in question.
412 * @new_name: object's new name 413 * @new_name: object's new name
413 * 414 *
414 * It is the responsibility of the caller to provide mutual 415 * It is the responsibility of the caller to provide mutual
415 * exclusion between two different calls of kobject_rename 416 * exclusion between two different calls of kobject_rename
416 * on the same kobject and to ensure that new_name is valid and 417 * on the same kobject and to ensure that new_name is valid and
417 * won't conflict with other kobjects. 418 * won't conflict with other kobjects.
418 */ 419 */
419 int kobject_rename(struct kobject *kobj, const char *new_name) 420 int kobject_rename(struct kobject *kobj, const char *new_name)
420 { 421 {
421 int error = 0; 422 int error = 0;
422 const char *devpath = NULL; 423 const char *devpath = NULL;
423 const char *dup_name = NULL, *name; 424 const char *dup_name = NULL, *name;
424 char *devpath_string = NULL; 425 char *devpath_string = NULL;
425 char *envp[2]; 426 char *envp[2];
426 427
427 kobj = kobject_get(kobj); 428 kobj = kobject_get(kobj);
428 if (!kobj) 429 if (!kobj)
429 return -EINVAL; 430 return -EINVAL;
430 if (!kobj->parent) 431 if (!kobj->parent)
431 return -EINVAL; 432 return -EINVAL;
432 433
433 devpath = kobject_get_path(kobj, GFP_KERNEL); 434 devpath = kobject_get_path(kobj, GFP_KERNEL);
434 if (!devpath) { 435 if (!devpath) {
435 error = -ENOMEM; 436 error = -ENOMEM;
436 goto out; 437 goto out;
437 } 438 }
438 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL); 439 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
439 if (!devpath_string) { 440 if (!devpath_string) {
440 error = -ENOMEM; 441 error = -ENOMEM;
441 goto out; 442 goto out;
442 } 443 }
443 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); 444 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
444 envp[0] = devpath_string; 445 envp[0] = devpath_string;
445 envp[1] = NULL; 446 envp[1] = NULL;
446 447
447 name = dup_name = kstrdup(new_name, GFP_KERNEL); 448 name = dup_name = kstrdup(new_name, GFP_KERNEL);
448 if (!name) { 449 if (!name) {
449 error = -ENOMEM; 450 error = -ENOMEM;
450 goto out; 451 goto out;
451 } 452 }
452 453
453 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj)); 454 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
454 if (error) 455 if (error)
455 goto out; 456 goto out;
456 457
457 /* Install the new kobject name */ 458 /* Install the new kobject name */
458 dup_name = kobj->name; 459 dup_name = kobj->name;
459 kobj->name = name; 460 kobj->name = name;
460 461
461 /* This function is mostly/only used for network interface. 462 /* This function is mostly/only used for network interface.
462 * Some hotplug package track interfaces by their name and 463 * Some hotplug package track interfaces by their name and
463 * therefore want to know when the name is changed by the user. */ 464 * therefore want to know when the name is changed by the user. */
464 kobject_uevent_env(kobj, KOBJ_MOVE, envp); 465 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
465 466
466 out: 467 out:
467 kfree(dup_name); 468 kfree(dup_name);
468 kfree(devpath_string); 469 kfree(devpath_string);
469 kfree(devpath); 470 kfree(devpath);
470 kobject_put(kobj); 471 kobject_put(kobj);
471 472
472 return error; 473 return error;
473 } 474 }
474 EXPORT_SYMBOL_GPL(kobject_rename); 475 EXPORT_SYMBOL_GPL(kobject_rename);
475 476
476 /** 477 /**
477 * kobject_move - move object to another parent 478 * kobject_move - move object to another parent
478 * @kobj: object in question. 479 * @kobj: object in question.
479 * @new_parent: object's new parent (can be NULL) 480 * @new_parent: object's new parent (can be NULL)
480 */ 481 */
481 int kobject_move(struct kobject *kobj, struct kobject *new_parent) 482 int kobject_move(struct kobject *kobj, struct kobject *new_parent)
482 { 483 {
483 int error; 484 int error;
484 struct kobject *old_parent; 485 struct kobject *old_parent;
485 const char *devpath = NULL; 486 const char *devpath = NULL;
486 char *devpath_string = NULL; 487 char *devpath_string = NULL;
487 char *envp[2]; 488 char *envp[2];
488 489
489 kobj = kobject_get(kobj); 490 kobj = kobject_get(kobj);
490 if (!kobj) 491 if (!kobj)
491 return -EINVAL; 492 return -EINVAL;
492 new_parent = kobject_get(new_parent); 493 new_parent = kobject_get(new_parent);
493 if (!new_parent) { 494 if (!new_parent) {
494 if (kobj->kset) 495 if (kobj->kset)
495 new_parent = kobject_get(&kobj->kset->kobj); 496 new_parent = kobject_get(&kobj->kset->kobj);
496 } 497 }
497 498
498 /* old object path */ 499 /* old object path */
499 devpath = kobject_get_path(kobj, GFP_KERNEL); 500 devpath = kobject_get_path(kobj, GFP_KERNEL);
500 if (!devpath) { 501 if (!devpath) {
501 error = -ENOMEM; 502 error = -ENOMEM;
502 goto out; 503 goto out;
503 } 504 }
504 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL); 505 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
505 if (!devpath_string) { 506 if (!devpath_string) {
506 error = -ENOMEM; 507 error = -ENOMEM;
507 goto out; 508 goto out;
508 } 509 }
509 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); 510 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
510 envp[0] = devpath_string; 511 envp[0] = devpath_string;
511 envp[1] = NULL; 512 envp[1] = NULL;
512 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj)); 513 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
513 if (error) 514 if (error)
514 goto out; 515 goto out;
515 old_parent = kobj->parent; 516 old_parent = kobj->parent;
516 kobj->parent = new_parent; 517 kobj->parent = new_parent;
517 new_parent = NULL; 518 new_parent = NULL;
518 kobject_put(old_parent); 519 kobject_put(old_parent);
519 kobject_uevent_env(kobj, KOBJ_MOVE, envp); 520 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
520 out: 521 out:
521 kobject_put(new_parent); 522 kobject_put(new_parent);
522 kobject_put(kobj); 523 kobject_put(kobj);
523 kfree(devpath_string); 524 kfree(devpath_string);
524 kfree(devpath); 525 kfree(devpath);
525 return error; 526 return error;
526 } 527 }
527 528
528 /** 529 /**
529 * kobject_del - unlink kobject from hierarchy. 530 * kobject_del - unlink kobject from hierarchy.
530 * @kobj: object. 531 * @kobj: object.
531 */ 532 */
532 void kobject_del(struct kobject *kobj) 533 void kobject_del(struct kobject *kobj)
533 { 534 {
534 if (!kobj) 535 if (!kobj)
535 return; 536 return;
536 537
537 sysfs_remove_dir(kobj); 538 sysfs_remove_dir(kobj);
538 kobj->state_in_sysfs = 0; 539 kobj->state_in_sysfs = 0;
539 kobj_kset_leave(kobj); 540 kobj_kset_leave(kobj);
540 kobject_put(kobj->parent); 541 kobject_put(kobj->parent);
541 kobj->parent = NULL; 542 kobj->parent = NULL;
542 } 543 }
543 544
544 /** 545 /**
545 * kobject_get - increment refcount for object. 546 * kobject_get - increment refcount for object.
546 * @kobj: object. 547 * @kobj: object.
547 */ 548 */
548 struct kobject *kobject_get(struct kobject *kobj) 549 struct kobject *kobject_get(struct kobject *kobj)
549 { 550 {
550 if (kobj) 551 if (kobj)
551 kref_get(&kobj->kref); 552 kref_get(&kobj->kref);
552 return kobj; 553 return kobj;
553 } 554 }
554 555
555 static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj) 556 static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
556 { 557 {
557 if (!kref_get_unless_zero(&kobj->kref)) 558 if (!kref_get_unless_zero(&kobj->kref))
558 kobj = NULL; 559 kobj = NULL;
559 return kobj; 560 return kobj;
560 } 561 }
561 562
562 /* 563 /*
563 * kobject_cleanup - free kobject resources. 564 * kobject_cleanup - free kobject resources.
564 * @kobj: object to cleanup 565 * @kobj: object to cleanup
565 */ 566 */
566 static void kobject_cleanup(struct kobject *kobj) 567 static void kobject_cleanup(struct kobject *kobj)
567 { 568 {
568 struct kobj_type *t = get_ktype(kobj); 569 struct kobj_type *t = get_ktype(kobj);
569 const char *name = kobj->name; 570 const char *name = kobj->name;
570 571
571 pr_debug("kobject: '%s' (%p): %s, parent %p\n", 572 pr_debug("kobject: '%s' (%p): %s, parent %p\n",
572 kobject_name(kobj), kobj, __func__, kobj->parent); 573 kobject_name(kobj), kobj, __func__, kobj->parent);
573 574
574 if (t && !t->release) 575 if (t && !t->release)
575 pr_debug("kobject: '%s' (%p): does not have a release() " 576 pr_debug("kobject: '%s' (%p): does not have a release() "
576 "function, it is broken and must be fixed.\n", 577 "function, it is broken and must be fixed.\n",
577 kobject_name(kobj), kobj); 578 kobject_name(kobj), kobj);
578 579
579 /* send "remove" if the caller did not do it but sent "add" */ 580 /* send "remove" if the caller did not do it but sent "add" */
580 if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) { 581 if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
581 pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n", 582 pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
582 kobject_name(kobj), kobj); 583 kobject_name(kobj), kobj);
583 kobject_uevent(kobj, KOBJ_REMOVE); 584 kobject_uevent(kobj, KOBJ_REMOVE);
584 } 585 }
585 586
586 /* remove from sysfs if the caller did not do it */ 587 /* remove from sysfs if the caller did not do it */
587 if (kobj->state_in_sysfs) { 588 if (kobj->state_in_sysfs) {
588 pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n", 589 pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
589 kobject_name(kobj), kobj); 590 kobject_name(kobj), kobj);
590 kobject_del(kobj); 591 kobject_del(kobj);
591 } 592 }
592 593
593 if (t && t->release) { 594 if (t && t->release) {
594 pr_debug("kobject: '%s' (%p): calling ktype release\n", 595 pr_debug("kobject: '%s' (%p): calling ktype release\n",
595 kobject_name(kobj), kobj); 596 kobject_name(kobj), kobj);
596 t->release(kobj); 597 t->release(kobj);
597 } 598 }
598 599
599 /* free name if we allocated it */ 600 /* free name if we allocated it */
600 if (name) { 601 if (name) {
601 pr_debug("kobject: '%s': free name\n", name); 602 pr_debug("kobject: '%s': free name\n", name);
602 kfree(name); 603 kfree(name);
603 } 604 }
604 } 605 }
605 606
606 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE 607 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
607 static void kobject_delayed_cleanup(struct work_struct *work) 608 static void kobject_delayed_cleanup(struct work_struct *work)
608 { 609 {
609 kobject_cleanup(container_of(to_delayed_work(work), 610 kobject_cleanup(container_of(to_delayed_work(work),
610 struct kobject, release)); 611 struct kobject, release));
611 } 612 }
612 #endif 613 #endif
613 614
614 static void kobject_release(struct kref *kref) 615 static void kobject_release(struct kref *kref)
615 { 616 {
616 struct kobject *kobj = container_of(kref, struct kobject, kref); 617 struct kobject *kobj = container_of(kref, struct kobject, kref);
617 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE 618 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
618 pr_debug("kobject: '%s' (%p): %s, parent %p (delayed)\n", 619 pr_debug("kobject: '%s' (%p): %s, parent %p (delayed)\n",
619 kobject_name(kobj), kobj, __func__, kobj->parent); 620 kobject_name(kobj), kobj, __func__, kobj->parent);
620 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup); 621 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
621 schedule_delayed_work(&kobj->release, HZ); 622 schedule_delayed_work(&kobj->release, HZ);
622 #else 623 #else
623 kobject_cleanup(kobj); 624 kobject_cleanup(kobj);
624 #endif 625 #endif
625 } 626 }
626 627
627 /** 628 /**
628 * kobject_put - decrement refcount for object. 629 * kobject_put - decrement refcount for object.
629 * @kobj: object. 630 * @kobj: object.
630 * 631 *
631 * Decrement the refcount, and if 0, call kobject_cleanup(). 632 * Decrement the refcount, and if 0, call kobject_cleanup().
632 */ 633 */
633 void kobject_put(struct kobject *kobj) 634 void kobject_put(struct kobject *kobj)
634 { 635 {
635 if (kobj) { 636 if (kobj) {
636 if (!kobj->state_initialized) 637 if (!kobj->state_initialized)
637 WARN(1, KERN_WARNING "kobject: '%s' (%p): is not " 638 WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
638 "initialized, yet kobject_put() is being " 639 "initialized, yet kobject_put() is being "
639 "called.\n", kobject_name(kobj), kobj); 640 "called.\n", kobject_name(kobj), kobj);
640 kref_put(&kobj->kref, kobject_release); 641 kref_put(&kobj->kref, kobject_release);
641 } 642 }
642 } 643 }
643 644
644 static void dynamic_kobj_release(struct kobject *kobj) 645 static void dynamic_kobj_release(struct kobject *kobj)
645 { 646 {
646 pr_debug("kobject: (%p): %s\n", kobj, __func__); 647 pr_debug("kobject: (%p): %s\n", kobj, __func__);
647 kfree(kobj); 648 kfree(kobj);
648 } 649 }
649 650
650 static struct kobj_type dynamic_kobj_ktype = { 651 static struct kobj_type dynamic_kobj_ktype = {
651 .release = dynamic_kobj_release, 652 .release = dynamic_kobj_release,
652 .sysfs_ops = &kobj_sysfs_ops, 653 .sysfs_ops = &kobj_sysfs_ops,
653 }; 654 };
654 655
655 /** 656 /**
656 * kobject_create - create a struct kobject dynamically 657 * kobject_create - create a struct kobject dynamically
657 * 658 *
658 * This function creates a kobject structure dynamically and sets it up 659 * This function creates a kobject structure dynamically and sets it up
659 * to be a "dynamic" kobject with a default release function set up. 660 * to be a "dynamic" kobject with a default release function set up.
660 * 661 *
661 * If the kobject was not able to be created, NULL will be returned. 662 * If the kobject was not able to be created, NULL will be returned.
662 * The kobject structure returned from here must be cleaned up with a 663 * The kobject structure returned from here must be cleaned up with a
663 * call to kobject_put() and not kfree(), as kobject_init() has 664 * call to kobject_put() and not kfree(), as kobject_init() has
664 * already been called on this structure. 665 * already been called on this structure.
665 */ 666 */
666 struct kobject *kobject_create(void) 667 struct kobject *kobject_create(void)
667 { 668 {
668 struct kobject *kobj; 669 struct kobject *kobj;
669 670
670 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); 671 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
671 if (!kobj) 672 if (!kobj)
672 return NULL; 673 return NULL;
673 674
674 kobject_init(kobj, &dynamic_kobj_ktype); 675 kobject_init(kobj, &dynamic_kobj_ktype);
675 return kobj; 676 return kobj;
676 } 677 }
677 678
678 /** 679 /**
679 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs 680 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
680 * 681 *
681 * @name: the name for the kobject 682 * @name: the name for the kobject
682 * @parent: the parent kobject of this kobject, if any. 683 * @parent: the parent kobject of this kobject, if any.
683 * 684 *
684 * This function creates a kobject structure dynamically and registers it 685 * This function creates a kobject structure dynamically and registers it
685 * with sysfs. When you are finished with this structure, call 686 * with sysfs. When you are finished with this structure, call
686 * kobject_put() and the structure will be dynamically freed when 687 * kobject_put() and the structure will be dynamically freed when
687 * it is no longer being used. 688 * it is no longer being used.
688 * 689 *
689 * If the kobject was not able to be created, NULL will be returned. 690 * If the kobject was not able to be created, NULL will be returned.
690 */ 691 */
691 struct kobject *kobject_create_and_add(const char *name, struct kobject *parent) 692 struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
692 { 693 {
693 struct kobject *kobj; 694 struct kobject *kobj;
694 int retval; 695 int retval;
695 696
696 kobj = kobject_create(); 697 kobj = kobject_create();
697 if (!kobj) 698 if (!kobj)
698 return NULL; 699 return NULL;
699 700
700 retval = kobject_add(kobj, parent, "%s", name); 701 retval = kobject_add(kobj, parent, "%s", name);
701 if (retval) { 702 if (retval) {
702 printk(KERN_WARNING "%s: kobject_add error: %d\n", 703 printk(KERN_WARNING "%s: kobject_add error: %d\n",
703 __func__, retval); 704 __func__, retval);
704 kobject_put(kobj); 705 kobject_put(kobj);
705 kobj = NULL; 706 kobj = NULL;
706 } 707 }
707 return kobj; 708 return kobj;
708 } 709 }
709 EXPORT_SYMBOL_GPL(kobject_create_and_add); 710 EXPORT_SYMBOL_GPL(kobject_create_and_add);
710 711
711 /** 712 /**
712 * kset_init - initialize a kset for use 713 * kset_init - initialize a kset for use
713 * @k: kset 714 * @k: kset
714 */ 715 */
715 void kset_init(struct kset *k) 716 void kset_init(struct kset *k)
716 { 717 {
717 kobject_init_internal(&k->kobj); 718 kobject_init_internal(&k->kobj);
718 INIT_LIST_HEAD(&k->list); 719 INIT_LIST_HEAD(&k->list);
719 spin_lock_init(&k->list_lock); 720 spin_lock_init(&k->list_lock);
720 } 721 }
721 722
722 /* default kobject attribute operations */ 723 /* default kobject attribute operations */
723 static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr, 724 static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
724 char *buf) 725 char *buf)
725 { 726 {
726 struct kobj_attribute *kattr; 727 struct kobj_attribute *kattr;
727 ssize_t ret = -EIO; 728 ssize_t ret = -EIO;
728 729
729 kattr = container_of(attr, struct kobj_attribute, attr); 730 kattr = container_of(attr, struct kobj_attribute, attr);
730 if (kattr->show) 731 if (kattr->show)
731 ret = kattr->show(kobj, kattr, buf); 732 ret = kattr->show(kobj, kattr, buf);
732 return ret; 733 return ret;
733 } 734 }
734 735
735 static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr, 736 static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
736 const char *buf, size_t count) 737 const char *buf, size_t count)
737 { 738 {
738 struct kobj_attribute *kattr; 739 struct kobj_attribute *kattr;
739 ssize_t ret = -EIO; 740 ssize_t ret = -EIO;
740 741
741 kattr = container_of(attr, struct kobj_attribute, attr); 742 kattr = container_of(attr, struct kobj_attribute, attr);
742 if (kattr->store) 743 if (kattr->store)
743 ret = kattr->store(kobj, kattr, buf, count); 744 ret = kattr->store(kobj, kattr, buf, count);
744 return ret; 745 return ret;
745 } 746 }
746 747
747 const struct sysfs_ops kobj_sysfs_ops = { 748 const struct sysfs_ops kobj_sysfs_ops = {
748 .show = kobj_attr_show, 749 .show = kobj_attr_show,
749 .store = kobj_attr_store, 750 .store = kobj_attr_store,
750 }; 751 };
752
753 /**
754 * kobj_completion_init - initialize a kobj_completion object.
755 * @kc: kobj_completion
756 * @ktype: type of kobject to initialize
757 *
758 * kobj_completion structures can be embedded within structures with different
759 * lifetime rules. During the release of the enclosing object, we can
760 * wait on the release of the kobject so that we don't free it while it's
761 * still busy.
762 */
763 void kobj_completion_init(struct kobj_completion *kc, struct kobj_type *ktype)
764 {
765 init_completion(&kc->kc_unregister);
766 kobject_init(&kc->kc_kobj, ktype);
767 }
768 EXPORT_SYMBOL_GPL(kobj_completion_init);
769
770 /**
771 * kobj_completion_release - release a kobj_completion object
772 * @kobj: kobject embedded in kobj_completion
773 *
774 * Used with kobject_release to notify waiters that the kobject has been
775 * released.
776 */
777 void kobj_completion_release(struct kobject *kobj)
778 {
779 struct kobj_completion *kc = kobj_to_kobj_completion(kobj);
780 complete(&kc->kc_unregister);
781 }
782 EXPORT_SYMBOL_GPL(kobj_completion_release);
783
784 /**
785 * kobj_completion_del_and_wait - release the kobject and wait for it
786 * @kc: kobj_completion object to release
787 *
788 * Delete the kobject from sysfs and drop the reference count. Then wait
789 * until any other outstanding references are also dropped. This routine
790 * is only necessary once other references may have been taken on the
791 * kobject. Typically this happens when the kobject has been published
792 * to sysfs via kobject_add.
793 */
794 void kobj_completion_del_and_wait(struct kobj_completion *kc)
795 {
796 kobject_del(&kc->kc_kobj);
797 kobject_put(&kc->kc_kobj);
798 wait_for_completion(&kc->kc_unregister);
799 }
800 EXPORT_SYMBOL_GPL(kobj_completion_del_and_wait);
751 801
752 /** 802 /**
753 * kset_register - initialize and add a kset. 803 * kset_register - initialize and add a kset.
754 * @k: kset. 804 * @k: kset.
755 */ 805 */
756 int kset_register(struct kset *k) 806 int kset_register(struct kset *k)
757 { 807 {
758 int err; 808 int err;
759 809
760 if (!k) 810 if (!k)
761 return -EINVAL; 811 return -EINVAL;
762 812
763 kset_init(k); 813 kset_init(k);
764 err = kobject_add_internal(&k->kobj); 814 err = kobject_add_internal(&k->kobj);
765 if (err) 815 if (err)
766 return err; 816 return err;
767 kobject_uevent(&k->kobj, KOBJ_ADD); 817 kobject_uevent(&k->kobj, KOBJ_ADD);
768 return 0; 818 return 0;
769 } 819 }
770 820
771 /** 821 /**
772 * kset_unregister - remove a kset. 822 * kset_unregister - remove a kset.
773 * @k: kset. 823 * @k: kset.
774 */ 824 */
775 void kset_unregister(struct kset *k) 825 void kset_unregister(struct kset *k)
776 { 826 {
777 if (!k) 827 if (!k)
778 return; 828 return;
779 kobject_put(&k->kobj); 829 kobject_put(&k->kobj);
780 } 830 }
781 831
782 /** 832 /**
783 * kset_find_obj - search for object in kset. 833 * kset_find_obj - search for object in kset.
784 * @kset: kset we're looking in. 834 * @kset: kset we're looking in.
785 * @name: object's name. 835 * @name: object's name.
786 * 836 *
787 * Lock kset via @kset->subsys, and iterate over @kset->list, 837 * Lock kset via @kset->subsys, and iterate over @kset->list,
788 * looking for a matching kobject. If matching object is found 838 * looking for a matching kobject. If matching object is found
789 * take a reference and return the object. 839 * take a reference and return the object.
790 */ 840 */
791 struct kobject *kset_find_obj(struct kset *kset, const char *name) 841 struct kobject *kset_find_obj(struct kset *kset, const char *name)
792 { 842 {
793 struct kobject *k; 843 struct kobject *k;
794 struct kobject *ret = NULL; 844 struct kobject *ret = NULL;
795 845
796 spin_lock(&kset->list_lock); 846 spin_lock(&kset->list_lock);
797 847
798 list_for_each_entry(k, &kset->list, entry) { 848 list_for_each_entry(k, &kset->list, entry) {
799 if (kobject_name(k) && !strcmp(kobject_name(k), name)) { 849 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
800 ret = kobject_get_unless_zero(k); 850 ret = kobject_get_unless_zero(k);
801 break; 851 break;
802 } 852 }
803 } 853 }
804 854
805 spin_unlock(&kset->list_lock); 855 spin_unlock(&kset->list_lock);
806 return ret; 856 return ret;
807 } 857 }
808 858
809 static void kset_release(struct kobject *kobj) 859 static void kset_release(struct kobject *kobj)
810 { 860 {
811 struct kset *kset = container_of(kobj, struct kset, kobj); 861 struct kset *kset = container_of(kobj, struct kset, kobj);
812 pr_debug("kobject: '%s' (%p): %s\n", 862 pr_debug("kobject: '%s' (%p): %s\n",
813 kobject_name(kobj), kobj, __func__); 863 kobject_name(kobj), kobj, __func__);
814 kfree(kset); 864 kfree(kset);
815 } 865 }
816 866
817 static struct kobj_type kset_ktype = { 867 static struct kobj_type kset_ktype = {
818 .sysfs_ops = &kobj_sysfs_ops, 868 .sysfs_ops = &kobj_sysfs_ops,
819 .release = kset_release, 869 .release = kset_release,
820 }; 870 };
821 871
822 /** 872 /**
823 * kset_create - create a struct kset dynamically 873 * kset_create - create a struct kset dynamically
824 * 874 *
825 * @name: the name for the kset 875 * @name: the name for the kset
826 * @uevent_ops: a struct kset_uevent_ops for the kset 876 * @uevent_ops: a struct kset_uevent_ops for the kset
827 * @parent_kobj: the parent kobject of this kset, if any. 877 * @parent_kobj: the parent kobject of this kset, if any.
828 * 878 *
829 * This function creates a kset structure dynamically. This structure can 879 * This function creates a kset structure dynamically. This structure can
830 * then be registered with the system and show up in sysfs with a call to 880 * then be registered with the system and show up in sysfs with a call to
831 * kset_register(). When you are finished with this structure, if 881 * kset_register(). When you are finished with this structure, if
832 * kset_register() has been called, call kset_unregister() and the 882 * kset_register() has been called, call kset_unregister() and the
833 * structure will be dynamically freed when it is no longer being used. 883 * structure will be dynamically freed when it is no longer being used.
834 * 884 *
835 * If the kset was not able to be created, NULL will be returned. 885 * If the kset was not able to be created, NULL will be returned.
836 */ 886 */
837 static struct kset *kset_create(const char *name, 887 static struct kset *kset_create(const char *name,
838 const struct kset_uevent_ops *uevent_ops, 888 const struct kset_uevent_ops *uevent_ops,
839 struct kobject *parent_kobj) 889 struct kobject *parent_kobj)
840 { 890 {
841 struct kset *kset; 891 struct kset *kset;
842 int retval; 892 int retval;
843 893
844 kset = kzalloc(sizeof(*kset), GFP_KERNEL); 894 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
845 if (!kset) 895 if (!kset)
846 return NULL; 896 return NULL;
847 retval = kobject_set_name(&kset->kobj, "%s", name); 897 retval = kobject_set_name(&kset->kobj, "%s", name);
848 if (retval) { 898 if (retval) {
849 kfree(kset); 899 kfree(kset);
850 return NULL; 900 return NULL;
851 } 901 }
852 kset->uevent_ops = uevent_ops; 902 kset->uevent_ops = uevent_ops;
853 kset->kobj.parent = parent_kobj; 903 kset->kobj.parent = parent_kobj;
854 904
855 /* 905 /*
856 * The kobject of this kset will have a type of kset_ktype and belong to 906 * The kobject of this kset will have a type of kset_ktype and belong to
857 * no kset itself. That way we can properly free it when it is 907 * no kset itself. That way we can properly free it when it is
858 * finished being used. 908 * finished being used.
859 */ 909 */
860 kset->kobj.ktype = &kset_ktype; 910 kset->kobj.ktype = &kset_ktype;
861 kset->kobj.kset = NULL; 911 kset->kobj.kset = NULL;
862 912
863 return kset; 913 return kset;
864 } 914 }
865 915
866 /** 916 /**
867 * kset_create_and_add - create a struct kset dynamically and add it to sysfs 917 * kset_create_and_add - create a struct kset dynamically and add it to sysfs
868 * 918 *
869 * @name: the name for the kset 919 * @name: the name for the kset
870 * @uevent_ops: a struct kset_uevent_ops for the kset 920 * @uevent_ops: a struct kset_uevent_ops for the kset
871 * @parent_kobj: the parent kobject of this kset, if any. 921 * @parent_kobj: the parent kobject of this kset, if any.
872 * 922 *
873 * This function creates a kset structure dynamically and registers it 923 * This function creates a kset structure dynamically and registers it
874 * with sysfs. When you are finished with this structure, call 924 * with sysfs. When you are finished with this structure, call
875 * kset_unregister() and the structure will be dynamically freed when it 925 * kset_unregister() and the structure will be dynamically freed when it
876 * is no longer being used. 926 * is no longer being used.
877 * 927 *
878 * If the kset was not able to be created, NULL will be returned. 928 * If the kset was not able to be created, NULL will be returned.
879 */ 929 */
880 struct kset *kset_create_and_add(const char *name, 930 struct kset *kset_create_and_add(const char *name,
881 const struct kset_uevent_ops *uevent_ops, 931 const struct kset_uevent_ops *uevent_ops,
882 struct kobject *parent_kobj) 932 struct kobject *parent_kobj)
883 { 933 {
884 struct kset *kset; 934 struct kset *kset;
885 int error; 935 int error;
886 936
887 kset = kset_create(name, uevent_ops, parent_kobj); 937 kset = kset_create(name, uevent_ops, parent_kobj);
888 if (!kset) 938 if (!kset)
889 return NULL; 939 return NULL;
890 error = kset_register(kset); 940 error = kset_register(kset);
891 if (error) { 941 if (error) {
892 kfree(kset); 942 kfree(kset);
893 return NULL; 943 return NULL;
894 } 944 }
895 return kset; 945 return kset;
896 } 946 }
897 EXPORT_SYMBOL_GPL(kset_create_and_add); 947 EXPORT_SYMBOL_GPL(kset_create_and_add);
898 948
899 949
900 static DEFINE_SPINLOCK(kobj_ns_type_lock); 950 static DEFINE_SPINLOCK(kobj_ns_type_lock);
901 static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES]; 951 static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
902 952
903 int kobj_ns_type_register(const struct kobj_ns_type_operations *ops) 953 int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
904 { 954 {
905 enum kobj_ns_type type = ops->type; 955 enum kobj_ns_type type = ops->type;
906 int error; 956 int error;
907 957
908 spin_lock(&kobj_ns_type_lock); 958 spin_lock(&kobj_ns_type_lock);
909 959
910 error = -EINVAL; 960 error = -EINVAL;
911 if (type >= KOBJ_NS_TYPES) 961 if (type >= KOBJ_NS_TYPES)
912 goto out; 962 goto out;
913 963
914 error = -EINVAL; 964 error = -EINVAL;
915 if (type <= KOBJ_NS_TYPE_NONE) 965 if (type <= KOBJ_NS_TYPE_NONE)
916 goto out; 966 goto out;
917 967
918 error = -EBUSY; 968 error = -EBUSY;
919 if (kobj_ns_ops_tbl[type]) 969 if (kobj_ns_ops_tbl[type])
920 goto out; 970 goto out;
921 971
922 error = 0; 972 error = 0;
923 kobj_ns_ops_tbl[type] = ops; 973 kobj_ns_ops_tbl[type] = ops;
924 974
925 out: 975 out:
926 spin_unlock(&kobj_ns_type_lock); 976 spin_unlock(&kobj_ns_type_lock);
927 return error; 977 return error;
928 } 978 }
929 979
930 int kobj_ns_type_registered(enum kobj_ns_type type) 980 int kobj_ns_type_registered(enum kobj_ns_type type)
931 { 981 {
932 int registered = 0; 982 int registered = 0;
933 983
934 spin_lock(&kobj_ns_type_lock); 984 spin_lock(&kobj_ns_type_lock);
935 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES)) 985 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
936 registered = kobj_ns_ops_tbl[type] != NULL; 986 registered = kobj_ns_ops_tbl[type] != NULL;
937 spin_unlock(&kobj_ns_type_lock); 987 spin_unlock(&kobj_ns_type_lock);
938 988
939 return registered; 989 return registered;
940 } 990 }
941 991
942 const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent) 992 const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
943 { 993 {
944 const struct kobj_ns_type_operations *ops = NULL; 994 const struct kobj_ns_type_operations *ops = NULL;
945 995
946 if (parent && parent->ktype->child_ns_type) 996 if (parent && parent->ktype->child_ns_type)
947 ops = parent->ktype->child_ns_type(parent); 997 ops = parent->ktype->child_ns_type(parent);
948 998
949 return ops; 999 return ops;
950 } 1000 }
951 1001
952 const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj) 1002 const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
953 { 1003 {
954 return kobj_child_ns_ops(kobj->parent); 1004 return kobj_child_ns_ops(kobj->parent);
955 } 1005 }
956 1006
957 bool kobj_ns_current_may_mount(enum kobj_ns_type type) 1007 bool kobj_ns_current_may_mount(enum kobj_ns_type type)
958 { 1008 {
959 bool may_mount = false; 1009 bool may_mount = false;
960 1010
961 if (type == KOBJ_NS_TYPE_NONE) 1011 if (type == KOBJ_NS_TYPE_NONE)
962 return true; 1012 return true;
963 1013
964 spin_lock(&kobj_ns_type_lock); 1014 spin_lock(&kobj_ns_type_lock);
965 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && 1015 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
966 kobj_ns_ops_tbl[type]) 1016 kobj_ns_ops_tbl[type])
967 may_mount = kobj_ns_ops_tbl[type]->current_may_mount(); 1017 may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
968 spin_unlock(&kobj_ns_type_lock); 1018 spin_unlock(&kobj_ns_type_lock);
969 1019
970 return may_mount; 1020 return may_mount;
971 } 1021 }
972 1022
973 void *kobj_ns_grab_current(enum kobj_ns_type type) 1023 void *kobj_ns_grab_current(enum kobj_ns_type type)
974 { 1024 {
975 void *ns = NULL; 1025 void *ns = NULL;
976 1026
977 spin_lock(&kobj_ns_type_lock); 1027 spin_lock(&kobj_ns_type_lock);
978 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && 1028 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
979 kobj_ns_ops_tbl[type]) 1029 kobj_ns_ops_tbl[type])
980 ns = kobj_ns_ops_tbl[type]->grab_current_ns(); 1030 ns = kobj_ns_ops_tbl[type]->grab_current_ns();
981 spin_unlock(&kobj_ns_type_lock); 1031 spin_unlock(&kobj_ns_type_lock);
982 1032
983 return ns; 1033 return ns;
984 } 1034 }
985 1035
986 const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk) 1036 const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
987 { 1037 {
988 const void *ns = NULL; 1038 const void *ns = NULL;
989 1039
990 spin_lock(&kobj_ns_type_lock); 1040 spin_lock(&kobj_ns_type_lock);
991 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && 1041 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
992 kobj_ns_ops_tbl[type]) 1042 kobj_ns_ops_tbl[type])
993 ns = kobj_ns_ops_tbl[type]->netlink_ns(sk); 1043 ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
994 spin_unlock(&kobj_ns_type_lock); 1044 spin_unlock(&kobj_ns_type_lock);
995 1045
996 return ns; 1046 return ns;
997 } 1047 }
998 1048
999 const void *kobj_ns_initial(enum kobj_ns_type type) 1049 const void *kobj_ns_initial(enum kobj_ns_type type)
1000 { 1050 {
1001 const void *ns = NULL; 1051 const void *ns = NULL;
1002 1052
1003 spin_lock(&kobj_ns_type_lock); 1053 spin_lock(&kobj_ns_type_lock);
1004 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && 1054 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1005 kobj_ns_ops_tbl[type]) 1055 kobj_ns_ops_tbl[type])
1006 ns = kobj_ns_ops_tbl[type]->initial_ns(); 1056 ns = kobj_ns_ops_tbl[type]->initial_ns();
1007 spin_unlock(&kobj_ns_type_lock); 1057 spin_unlock(&kobj_ns_type_lock);
1008 1058
1009 return ns; 1059 return ns;
1010 } 1060 }
1011 1061
1012 void kobj_ns_drop(enum kobj_ns_type type, void *ns) 1062 void kobj_ns_drop(enum kobj_ns_type type, void *ns)
1013 { 1063 {
1014 spin_lock(&kobj_ns_type_lock); 1064 spin_lock(&kobj_ns_type_lock);
1015 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && 1065 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1016 kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns) 1066 kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
1017 kobj_ns_ops_tbl[type]->drop_ns(ns); 1067 kobj_ns_ops_tbl[type]->drop_ns(ns);
1018 spin_unlock(&kobj_ns_type_lock); 1068 spin_unlock(&kobj_ns_type_lock);
1019 } 1069 }
1020 1070
1021 EXPORT_SYMBOL(kobject_get); 1071 EXPORT_SYMBOL(kobject_get);
1022 EXPORT_SYMBOL(kobject_put); 1072 EXPORT_SYMBOL(kobject_put);
1023 EXPORT_SYMBOL(kobject_del); 1073 EXPORT_SYMBOL(kobject_del);
1024 1074
1025 EXPORT_SYMBOL(kset_register); 1075 EXPORT_SYMBOL(kset_register);
1026 EXPORT_SYMBOL(kset_unregister); 1076 EXPORT_SYMBOL(kset_unregister);
1027 1077