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