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