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