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