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