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