]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - lib/kobject.c
iommu/amd: Reserve exclusion range in iova-domain
[mirror_ubuntu-bionic-kernel.git] / lib / kobject.c
1 /*
2 * kobject.c - library routines for handling generic kernel objects
3 *
4 * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
5 * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (c) 2006-2007 Novell Inc.
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>
17 #include <linux/export.h>
18 #include <linux/stat.h>
19 #include <linux/slab.h>
20 #include <linux/random.h>
21
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 */
30 const 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
37 return kobj->ktype->namespace(kobj);
38 }
39
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 */
50 void 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
59 /*
60 * populate_dir - populate directory with attributes.
61 * @kobj: object we're working on.
62 *
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.
67 */
68 static int populate_dir(struct kobject *kobj)
69 {
70 struct kobj_type *t = get_ktype(kobj);
71 struct attribute *attr;
72 int error = 0;
73 int i;
74
75 if (t && t->default_attrs) {
76 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
77 error = sysfs_create_file(kobj, attr);
78 if (error)
79 break;
80 }
81 }
82 return error;
83 }
84
85 static int create_dir(struct kobject *kobj)
86 {
87 const struct kobj_ns_type_operations *ops;
88 int error;
89
90 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
91 if (error)
92 return error;
93
94 error = populate_dir(kobj);
95 if (error) {
96 sysfs_remove_dir(kobj);
97 return error;
98 }
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
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
116 sysfs_enable_ns(kobj->sd);
117 }
118
119 return 0;
120 }
121
122 static int get_kobj_path_length(struct kobject *kobj)
123 {
124 int length = 1;
125 struct kobject *parent = kobj;
126
127 /* walk up the ancestors until we hit the one pointing to the
128 * root.
129 * Add 1 to strlen for leading '/' of each level.
130 */
131 do {
132 if (kobject_name(parent) == NULL)
133 return 0;
134 length += strlen(kobject_name(parent)) + 1;
135 parent = parent->parent;
136 } while (parent);
137 return length;
138 }
139
140 static void fill_kobj_path(struct kobject *kobj, char *path, int length)
141 {
142 struct kobject *parent;
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;
149 strncpy(path + length, kobject_name(parent), cur);
150 *(path + --length) = '/';
151 }
152
153 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
154 kobj, __func__, path);
155 }
156
157 /**
158 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
159 *
160 * @kobj: kobject in question, with which to build the path
161 * @gfp_mask: the allocation type used to allocate the path
162 *
163 * The result must be freed by the caller with kfree().
164 */
165 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
166 {
167 char *path;
168 int len;
169
170 len = get_kobj_path_length(kobj);
171 if (len == 0)
172 return NULL;
173 path = kzalloc(len, gfp_mask);
174 if (!path)
175 return NULL;
176 fill_kobj_path(kobj, path, len);
177
178 return path;
179 }
180 EXPORT_SYMBOL_GPL(kobject_get_path);
181
182 /* add the kobject to its kset's list */
183 static void kobj_kset_join(struct kobject *kobj)
184 {
185 if (!kobj->kset)
186 return;
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);
192 }
193
194 /* remove the kobject from its kset's list */
195 static void kobj_kset_leave(struct kobject *kobj)
196 {
197 if (!kobj->kset)
198 return;
199
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 }
205
206 static void kobject_init_internal(struct kobject *kobj)
207 {
208 if (!kobj)
209 return;
210 kref_init(&kobj->kref);
211 INIT_LIST_HEAD(&kobj->entry);
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;
216 }
217
218
219 static int kobject_add_internal(struct kobject *kobj)
220 {
221 int error = 0;
222 struct kobject *parent;
223
224 if (!kobj)
225 return -ENOENT;
226
227 if (!kobj->name || !kobj->name[0]) {
228 WARN(1, "kobject: (%p): attempted to be registered with empty "
229 "name!\n", kobj);
230 return -EINVAL;
231 }
232
233 parent = kobject_get(kobj->parent);
234
235 /* join kset if set, use it as parent if we do not already have one */
236 if (kobj->kset) {
237 if (!parent)
238 parent = kobject_get(&kobj->kset->kobj);
239 kobj_kset_join(kobj);
240 kobj->parent = parent;
241 }
242
243 pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
244 kobject_name(kobj), kobj, __func__,
245 parent ? kobject_name(parent) : "<NULL>",
246 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
247
248 error = create_dir(kobj);
249 if (error) {
250 kobj_kset_leave(kobj);
251 kobject_put(parent);
252 kobj->parent = NULL;
253
254 /* be noisy on error issues */
255 if (error == -EEXIST)
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));
258 else
259 pr_err("%s failed for %s (error: %d parent: %s)\n",
260 __func__, kobject_name(kobj), error,
261 parent ? kobject_name(parent) : "'none'");
262 } else
263 kobj->state_in_sysfs = 1;
264
265 return error;
266 }
267
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 */
274 int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
275 va_list vargs)
276 {
277 const char *s;
278
279 if (kobj->name && !fmt)
280 return 0;
281
282 s = kvasprintf_const(GFP_KERNEL, fmt, vargs);
283 if (!s)
284 return -ENOMEM;
285
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);
303 kobj->name = s;
304
305 return 0;
306 }
307
308 /**
309 * kobject_set_name - Set the name of a kobject
310 * @kobj: struct kobject to set the name of
311 * @fmt: format string used to build the name
312 *
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.
316 */
317 int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
318 {
319 va_list vargs;
320 int retval;
321
322 va_start(vargs, fmt);
323 retval = kobject_set_name_vargs(kobj, fmt, vargs);
324 va_end(vargs);
325
326 return retval;
327 }
328 EXPORT_SYMBOL(kobject_set_name);
329
330 /**
331 * kobject_init - initialize a kobject structure
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 */
342 void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
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 }
354 if (kobj->state_initialized) {
355 /* do not error out as sometimes we can recover */
356 printk(KERN_ERR "kobject (%p): tried to init an initialized "
357 "object, something is seriously wrong.\n", kobj);
358 dump_stack();
359 }
360
361 kobject_init_internal(kobj);
362 kobj->ktype = ktype;
363 return;
364
365 error:
366 printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
367 dump_stack();
368 }
369 EXPORT_SYMBOL(kobject_init);
370
371 static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
372 struct kobject *parent,
373 const char *fmt, va_list vargs)
374 {
375 int retval;
376
377 retval = kobject_set_name_vargs(kobj, fmt, vargs);
378 if (retval) {
379 printk(KERN_ERR "kobject: can not set name properly!\n");
380 return retval;
381 }
382 kobj->parent = parent;
383 return kobject_add_internal(kobj);
384 }
385
386 /**
387 * kobject_add - the main kobject add function
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
397 * kobject associated with the kset assigned to this kobject. If no kset
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.
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 *
406 * Note, no "add" uevent will be created with this call, the caller should set
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 */
411 int kobject_add(struct kobject *kobj, struct kobject *parent,
412 const char *fmt, ...)
413 {
414 va_list args;
415 int retval;
416
417 if (!kobj)
418 return -EINVAL;
419
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 }
427 va_start(args, fmt);
428 retval = kobject_add_varg(kobj, parent, fmt, args);
429 va_end(args);
430
431 return retval;
432 }
433 EXPORT_SYMBOL(kobject_add);
434
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 *
442 * This function combines the call to kobject_init() and
443 * kobject_add(). The same type of error handling after a call to
444 * kobject_add() and kobject lifetime rules are the same here.
445 */
446 int 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
452 kobject_init(kobj, ktype);
453
454 va_start(args, fmt);
455 retval = kobject_add_varg(kobj, parent, fmt, args);
456 va_end(args);
457
458 return retval;
459 }
460 EXPORT_SYMBOL_GPL(kobject_init_and_add);
461
462 /**
463 * kobject_rename - change the name of an object
464 * @kobj: object in question.
465 * @new_name: object's new name
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.
471 */
472 int kobject_rename(struct kobject *kobj, const char *new_name)
473 {
474 int error = 0;
475 const char *devpath = NULL;
476 const char *dup_name = NULL, *name;
477 char *devpath_string = NULL;
478 char *envp[2];
479
480 kobj = kobject_get(kobj);
481 if (!kobj)
482 return -EINVAL;
483 if (!kobj->parent)
484 return -EINVAL;
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;
499
500 name = dup_name = kstrdup_const(new_name, GFP_KERNEL);
501 if (!name) {
502 error = -ENOMEM;
503 goto out;
504 }
505
506 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
507 if (error)
508 goto out;
509
510 /* Install the new kobject name */
511 dup_name = kobj->name;
512 kobj->name = name;
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. */
517 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
518
519 out:
520 kfree_const(dup_name);
521 kfree(devpath_string);
522 kfree(devpath);
523 kobject_put(kobj);
524
525 return error;
526 }
527 EXPORT_SYMBOL_GPL(kobject_rename);
528
529 /**
530 * kobject_move - move object to another parent
531 * @kobj: object in question.
532 * @new_parent: object's new parent (can be NULL)
533 */
534 int 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) {
547 if (kobj->kset)
548 new_parent = kobject_get(&kobj->kset->kobj);
549 }
550
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;
565 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
566 if (error)
567 goto out;
568 old_parent = kobj->parent;
569 kobj->parent = new_parent;
570 new_parent = NULL;
571 kobject_put(old_parent);
572 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
573 out:
574 kobject_put(new_parent);
575 kobject_put(kobj);
576 kfree(devpath_string);
577 kfree(devpath);
578 return error;
579 }
580 EXPORT_SYMBOL_GPL(kobject_move);
581
582 /**
583 * kobject_del - unlink kobject from hierarchy.
584 * @kobj: object.
585 */
586 void kobject_del(struct kobject *kobj)
587 {
588 struct kernfs_node *sd;
589
590 if (!kobj)
591 return;
592
593 sd = kobj->sd;
594 sysfs_remove_dir(kobj);
595 sysfs_put(sd);
596
597 kobj->state_in_sysfs = 0;
598 kobj_kset_leave(kobj);
599 kobject_put(kobj->parent);
600 kobj->parent = NULL;
601 }
602 EXPORT_SYMBOL(kobject_del);
603
604 /**
605 * kobject_get - increment refcount for object.
606 * @kobj: object.
607 */
608 struct kobject *kobject_get(struct kobject *kobj)
609 {
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);
615 kref_get(&kobj->kref);
616 }
617 return kobj;
618 }
619 EXPORT_SYMBOL(kobject_get);
620
621 struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
622 {
623 if (!kobj)
624 return NULL;
625 if (!kref_get_unless_zero(&kobj->kref))
626 kobj = NULL;
627 return kobj;
628 }
629 EXPORT_SYMBOL(kobject_get_unless_zero);
630
631 /*
632 * kobject_cleanup - free kobject resources.
633 * @kobj: object to cleanup
634 */
635 static void kobject_cleanup(struct kobject *kobj)
636 {
637 struct kobj_type *t = get_ktype(kobj);
638 const char *name = kobj->name;
639
640 pr_debug("kobject: '%s' (%p): %s, parent %p\n",
641 kobject_name(kobj), kobj, __func__, kobj->parent);
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
662 if (t && t->release) {
663 pr_debug("kobject: '%s' (%p): calling ktype release\n",
664 kobject_name(kobj), kobj);
665 t->release(kobj);
666 }
667
668 /* free name if we allocated it */
669 if (name) {
670 pr_debug("kobject: '%s': free name\n", name);
671 kfree_const(name);
672 }
673 }
674
675 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
676 static 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
683 static void kobject_release(struct kref *kref)
684 {
685 struct kobject *kobj = container_of(kref, struct kobject, kref);
686 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
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);
690 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
691
692 schedule_delayed_work(&kobj->release, delay);
693 #else
694 kobject_cleanup(kobj);
695 #endif
696 }
697
698 /**
699 * kobject_put - decrement refcount for object.
700 * @kobj: object.
701 *
702 * Decrement the refcount, and if 0, call kobject_cleanup().
703 */
704 void kobject_put(struct kobject *kobj)
705 {
706 if (kobj) {
707 if (!kobj->state_initialized)
708 WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
709 "initialized, yet kobject_put() is being "
710 "called.\n", kobject_name(kobj), kobj);
711 kref_put(&kobj->kref, kobject_release);
712 }
713 }
714 EXPORT_SYMBOL(kobject_put);
715
716 static void dynamic_kobj_release(struct kobject *kobj)
717 {
718 pr_debug("kobject: (%p): %s\n", kobj, __func__);
719 kfree(kobj);
720 }
721
722 static struct kobj_type dynamic_kobj_ktype = {
723 .release = dynamic_kobj_release,
724 .sysfs_ops = &kobj_sysfs_ops,
725 };
726
727 /**
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.
734 * The kobject structure returned from here must be cleaned up with a
735 * call to kobject_put() and not kfree(), as kobject_init() has
736 * already been called on this structure.
737 */
738 struct kobject *kobject_create(void)
739 {
740 struct kobject *kobj;
741
742 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
743 if (!kobj)
744 return NULL;
745
746 kobject_init(kobj, &dynamic_kobj_ktype);
747 return kobj;
748 }
749
750 /**
751 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
752 *
753 * @name: the name for the kobject
754 * @parent: the parent kobject of this kobject, if any.
755 *
756 * This function creates a kobject structure dynamically and registers it
757 * with sysfs. When you are finished with this structure, call
758 * kobject_put() and the structure will be dynamically freed when
759 * it is no longer being used.
760 *
761 * If the kobject was not able to be created, NULL will be returned.
762 */
763 struct 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
772 retval = kobject_add(kobj, parent, "%s", name);
773 if (retval) {
774 printk(KERN_WARNING "%s: kobject_add error: %d\n",
775 __func__, retval);
776 kobject_put(kobj);
777 kobj = NULL;
778 }
779 return kobj;
780 }
781 EXPORT_SYMBOL_GPL(kobject_create_and_add);
782
783 /**
784 * kset_init - initialize a kset for use
785 * @k: kset
786 */
787 void kset_init(struct kset *k)
788 {
789 kobject_init_internal(&k->kobj);
790 INIT_LIST_HEAD(&k->list);
791 spin_lock_init(&k->list_lock);
792 }
793
794 /* default kobject attribute operations */
795 static 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
807 static 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
819 const struct sysfs_ops kobj_sysfs_ops = {
820 .show = kobj_attr_show,
821 .store = kobj_attr_store,
822 };
823 EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
824
825 /**
826 * kset_register - initialize and add a kset.
827 * @k: kset.
828 */
829 int kset_register(struct kset *k)
830 {
831 int err;
832
833 if (!k)
834 return -EINVAL;
835
836 kset_init(k);
837 err = kobject_add_internal(&k->kobj);
838 if (err)
839 return err;
840 kobject_uevent(&k->kobj, KOBJ_ADD);
841 return 0;
842 }
843 EXPORT_SYMBOL(kset_register);
844
845 /**
846 * kset_unregister - remove a kset.
847 * @k: kset.
848 */
849 void kset_unregister(struct kset *k)
850 {
851 if (!k)
852 return;
853 kobject_del(&k->kobj);
854 kobject_put(&k->kobj);
855 }
856 EXPORT_SYMBOL(kset_unregister);
857
858 /**
859 * kset_find_obj - search for object in kset.
860 * @kset: kset we're looking in.
861 * @name: object's name.
862 *
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.
866 */
867 struct kobject *kset_find_obj(struct kset *kset, const char *name)
868 {
869 struct kobject *k;
870 struct kobject *ret = NULL;
871
872 spin_lock(&kset->list_lock);
873
874 list_for_each_entry(k, &kset->list, entry) {
875 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
876 ret = kobject_get_unless_zero(k);
877 break;
878 }
879 }
880
881 spin_unlock(&kset->list_lock);
882 return ret;
883 }
884 EXPORT_SYMBOL_GPL(kset_find_obj);
885
886 static void kset_release(struct kobject *kobj)
887 {
888 struct kset *kset = container_of(kobj, struct kset, kobj);
889 pr_debug("kobject: '%s' (%p): %s\n",
890 kobject_name(kobj), kobj, __func__);
891 kfree(kset);
892 }
893
894 void 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
900 static struct kobj_type kset_ktype = {
901 .sysfs_ops = &kobj_sysfs_ops,
902 .release = kset_release,
903 .get_ownership = kset_get_ownership,
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 */
921 static struct kset *kset_create(const char *name,
922 const struct kset_uevent_ops *uevent_ops,
923 struct kobject *parent_kobj)
924 {
925 struct kset *kset;
926 int retval;
927
928 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
929 if (!kset)
930 return NULL;
931 retval = kobject_set_name(&kset->kobj, "%s", name);
932 if (retval) {
933 kfree(kset);
934 return NULL;
935 }
936 kset->uevent_ops = uevent_ops;
937 kset->kobj.parent = parent_kobj;
938
939 /*
940 * The kobject of this kset will have a type of kset_ktype and belong to
941 * no kset itself. That way we can properly free it when it is
942 * finished being used.
943 */
944 kset->kobj.ktype = &kset_ktype;
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 */
964 struct kset *kset_create_and_add(const char *name,
965 const struct kset_uevent_ops *uevent_ops,
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 }
981 EXPORT_SYMBOL_GPL(kset_create_and_add);
982
983
984 static DEFINE_SPINLOCK(kobj_ns_type_lock);
985 static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
986
987 int 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
1009 out:
1010 spin_unlock(&kobj_ns_type_lock);
1011 return error;
1012 }
1013
1014 int 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
1026 const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
1027 {
1028 const struct kobj_ns_type_operations *ops = NULL;
1029
1030 if (parent && parent->ktype && parent->ktype->child_ns_type)
1031 ops = parent->ktype->child_ns_type(parent);
1032
1033 return ops;
1034 }
1035
1036 const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
1037 {
1038 return kobj_child_ns_ops(kobj->parent);
1039 }
1040
1041 bool kobj_ns_current_may_mount(enum kobj_ns_type type)
1042 {
1043 bool may_mount = true;
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 }
1053
1054 void *kobj_ns_grab_current(enum kobj_ns_type type)
1055 {
1056 void *ns = NULL;
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])
1061 ns = kobj_ns_ops_tbl[type]->grab_current_ns();
1062 spin_unlock(&kobj_ns_type_lock);
1063
1064 return ns;
1065 }
1066
1067 const 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
1080 const 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
1093 void kobj_ns_drop(enum kobj_ns_type type, void *ns)
1094 {
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);
1100 }