1 // SPDX-License-Identifier: GPL-2.0
3 * kobject.c - library routines for handling generic kernel objects
5 * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
6 * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (c) 2006-2007 Novell Inc.
9 * Please see the file Documentation/core-api/kobject.rst for critical information
10 * about using the kobject interface.
13 #include <linux/kobject.h>
14 #include <linux/string.h>
15 #include <linux/export.h>
16 #include <linux/stat.h>
17 #include <linux/slab.h>
18 #include <linux/random.h>
21 * kobject_namespace() - Return @kobj's namespace tag.
22 * @kobj: kobject in question
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
28 const void *kobject_namespace(struct kobject *kobj)
30 const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
32 if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
35 return kobj->ktype->namespace(kobj);
39 * kobject_get_ownership() - Get sysfs ownership data for @kobj.
40 * @kobj: kobject in question
41 * @uid: kernel user ID for sysfs objects
42 * @gid: kernel group ID for sysfs objects
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.
48 void kobject_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
50 *uid = GLOBAL_ROOT_UID;
51 *gid = GLOBAL_ROOT_GID;
53 if (kobj->ktype->get_ownership)
54 kobj->ktype->get_ownership(kobj, uid, gid);
57 static int create_dir(struct kobject *kobj)
59 const struct kobj_type *ktype = get_ktype(kobj);
60 const struct kobj_ns_type_operations *ops;
63 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
68 error = sysfs_create_groups(kobj, ktype->default_groups);
70 sysfs_remove_dir(kobj);
76 * @kobj->sd may be deleted by an ancestor going away. Hold an
77 * extra reference so that it stays until @kobj is gone.
82 * If @kobj has ns_ops, its children need to be filtered based on
83 * their namespace tags. Enable namespace support on @kobj->sd.
85 ops = kobj_child_ns_ops(kobj);
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));
91 sysfs_enable_ns(kobj->sd);
97 static int get_kobj_path_length(const struct kobject *kobj)
100 const struct kobject *parent = kobj;
102 /* walk up the ancestors until we hit the one pointing to the
104 * Add 1 to strlen for leading '/' of each level.
107 if (kobject_name(parent) == NULL)
109 length += strlen(kobject_name(parent)) + 1;
110 parent = parent->parent;
115 static int fill_kobj_path(const struct kobject *kobj, char *path, int length)
117 const struct kobject *parent;
120 for (parent = kobj; parent; parent = parent->parent) {
121 int cur = strlen(kobject_name(parent));
122 /* back up enough to print this name with '/' */
126 memcpy(path + length, kobject_name(parent), cur);
127 *(path + --length) = '/';
130 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
131 kobj, __func__, path);
137 * kobject_get_path() - Allocate memory and fill in the path for @kobj.
138 * @kobj: kobject in question, with which to build the path
139 * @gfp_mask: the allocation type used to allocate the path
141 * Return: The newly allocated memory, caller must free with kfree().
143 char *kobject_get_path(const struct kobject *kobj, gfp_t gfp_mask)
149 len = get_kobj_path_length(kobj);
152 path = kzalloc(len, gfp_mask);
155 if (fill_kobj_path(kobj, path, len)) {
162 EXPORT_SYMBOL_GPL(kobject_get_path);
164 /* add the kobject to its kset's list */
165 static void kobj_kset_join(struct kobject *kobj)
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);
176 /* remove the kobject from its kset's list */
177 static void kobj_kset_leave(struct kobject *kobj)
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);
188 static void kobject_init_internal(struct kobject *kobj)
192 kref_init(&kobj->kref);
193 INIT_LIST_HEAD(&kobj->entry);
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;
201 static int kobject_add_internal(struct kobject *kobj)
204 struct kobject *parent;
209 if (!kobj->name || !kobj->name[0]) {
211 "kobject: (%p): attempted to be registered with empty name!\n",
216 parent = kobject_get(kobj->parent);
218 /* join kset if set, use it as parent if we do not already have one */
221 parent = kobject_get(&kobj->kset->kobj);
222 kobj_kset_join(kobj);
223 kobj->parent = parent;
226 pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
227 kobject_name(kobj), kobj, __func__,
228 parent ? kobject_name(parent) : "<NULL>",
229 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
231 error = create_dir(kobj);
233 kobj_kset_leave(kobj);
237 /* be noisy on error issues */
238 if (error == -EEXIST)
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));
242 pr_err("%s failed for %s (error: %d parent: %s)\n",
243 __func__, kobject_name(kobj), error,
244 parent ? kobject_name(parent) : "'none'");
246 kobj->state_in_sysfs = 1;
252 * kobject_set_name_vargs() - Set the name of a kobject.
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.
257 int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
262 if (kobj->name && !fmt)
265 s = kvasprintf_const(GFP_KERNEL, fmt, vargs);
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.
275 if (strchr(s, '/')) {
278 t = kstrdup(s, GFP_KERNEL);
282 strreplace(t, '/', '!');
285 kfree_const(kobj->name);
292 * kobject_set_name() - Set the name of a kobject.
293 * @kobj: struct kobject to set the name of
294 * @fmt: format string used to build the name
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.
300 int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
305 va_start(vargs, fmt);
306 retval = kobject_set_name_vargs(kobj, fmt, vargs);
311 EXPORT_SYMBOL(kobject_set_name);
314 * kobject_init() - Initialize a kobject structure.
315 * @kobj: pointer to the kobject to initialize
316 * @ktype: pointer to the ktype for this kobject.
318 * This function will properly initialize a kobject such that it can then
319 * be passed to the kobject_add() call.
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.
325 void kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
330 err_str = "invalid kobject pointer!";
334 err_str = "must have a ktype to be initialized properly!\n";
337 if (kobj->state_initialized) {
338 /* do not error out as sometimes we can recover */
339 pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n",
344 kobject_init_internal(kobj);
349 pr_err("kobject (%p): %s\n", kobj, err_str);
352 EXPORT_SYMBOL(kobject_init);
354 static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
355 struct kobject *parent,
356 const char *fmt, va_list vargs)
360 retval = kobject_set_name_vargs(kobj, fmt, vargs);
362 pr_err("kobject: can not set name properly!\n");
365 kobj->parent = parent;
366 return kobject_add_internal(kobj);
370 * kobject_add() - The main kobject add function.
371 * @kobj: the kobject to add
372 * @parent: pointer to the parent of the kobject.
373 * @fmt: format to name the kobject with.
375 * The kobject name is set and added to the kobject hierarchy in this
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
380 * kobject associated with the kset assigned to this kobject. If no kset
381 * is assigned to the kobject, then the kobject will be located in the
382 * root of the sysfs tree.
384 * Note, no "add" uevent will be created with this call, the caller should set
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.
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.
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.
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
402 int kobject_add(struct kobject *kobj, struct kobject *parent,
403 const char *fmt, ...)
411 if (!kobj->state_initialized) {
412 pr_err("kobject '%s' (%p): tried to add an uninitialized object, something is seriously wrong.\n",
413 kobject_name(kobj), kobj);
418 retval = kobject_add_varg(kobj, parent, fmt, args);
423 EXPORT_SYMBOL(kobject_add);
426 * kobject_init_and_add() - Initialize a kobject structure and add it to
427 * the kobject hierarchy.
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.
433 * This function combines the call to kobject_init() and kobject_add().
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.
440 int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
441 struct kobject *parent, const char *fmt, ...)
446 kobject_init(kobj, ktype);
449 retval = kobject_add_varg(kobj, parent, fmt, args);
454 EXPORT_SYMBOL_GPL(kobject_init_and_add);
457 * kobject_rename() - Change the name of an object.
458 * @kobj: object in question.
459 * @new_name: object's new name
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.
466 int kobject_rename(struct kobject *kobj, const char *new_name)
469 const char *devpath = NULL;
470 const char *dup_name = NULL, *name;
471 char *devpath_string = NULL;
474 kobj = kobject_get(kobj);
482 devpath = kobject_get_path(kobj, GFP_KERNEL);
487 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
488 if (!devpath_string) {
492 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
493 envp[0] = devpath_string;
496 name = dup_name = kstrdup_const(new_name, GFP_KERNEL);
502 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
506 /* Install the new kobject name */
507 dup_name = kobj->name;
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. */
513 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
516 kfree_const(dup_name);
517 kfree(devpath_string);
523 EXPORT_SYMBOL_GPL(kobject_rename);
526 * kobject_move() - Move object to another parent.
527 * @kobj: object in question.
528 * @new_parent: object's new parent (can be NULL)
530 int kobject_move(struct kobject *kobj, struct kobject *new_parent)
533 struct kobject *old_parent;
534 const char *devpath = NULL;
535 char *devpath_string = NULL;
538 kobj = kobject_get(kobj);
541 new_parent = kobject_get(new_parent);
544 new_parent = kobject_get(&kobj->kset->kobj);
547 /* old object path */
548 devpath = kobject_get_path(kobj, GFP_KERNEL);
553 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
554 if (!devpath_string) {
558 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
559 envp[0] = devpath_string;
561 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
564 old_parent = kobj->parent;
565 kobj->parent = new_parent;
567 kobject_put(old_parent);
568 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
570 kobject_put(new_parent);
572 kfree(devpath_string);
576 EXPORT_SYMBOL_GPL(kobject_move);
578 static void __kobject_del(struct kobject *kobj)
580 struct kernfs_node *sd;
581 const struct kobj_type *ktype;
584 ktype = get_ktype(kobj);
587 sysfs_remove_groups(kobj, ktype->default_groups);
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);
596 sysfs_remove_dir(kobj);
599 kobj->state_in_sysfs = 0;
600 kobj_kset_leave(kobj);
605 * kobject_del() - Unlink kobject from hierarchy.
608 * This is the function that should be called to delete an object
609 * successfully added via kobject_add().
611 void kobject_del(struct kobject *kobj)
613 struct kobject *parent;
618 parent = kobj->parent;
622 EXPORT_SYMBOL(kobject_del);
625 * kobject_get() - Increment refcount for object.
628 struct kobject *kobject_get(struct kobject *kobj)
631 if (!kobj->state_initialized)
633 "kobject: '%s' (%p): is not initialized, yet kobject_get() is being called.\n",
634 kobject_name(kobj), kobj);
635 kref_get(&kobj->kref);
639 EXPORT_SYMBOL(kobject_get);
641 struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
645 if (!kref_get_unless_zero(&kobj->kref))
649 EXPORT_SYMBOL(kobject_get_unless_zero);
652 * kobject_cleanup - free kobject resources.
653 * @kobj: object to cleanup
655 static void kobject_cleanup(struct kobject *kobj)
657 struct kobject *parent = kobj->parent;
658 const struct kobj_type *t = get_ktype(kobj);
659 const char *name = kobj->name;
661 pr_debug("kobject: '%s' (%p): %s, parent %p\n",
662 kobject_name(kobj), kobj, __func__, kobj->parent);
664 if (t && !t->release)
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",
666 kobject_name(kobj), kobj);
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);
674 /* avoid dropping the parent reference unnecessarily */
678 if (t && t->release) {
679 pr_debug("kobject: '%s' (%p): calling ktype release\n",
680 kobject_name(kobj), kobj);
684 /* free name if we allocated it */
686 pr_debug("kobject: '%s': free name\n", name);
693 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
694 static void kobject_delayed_cleanup(struct work_struct *work)
696 kobject_cleanup(container_of(to_delayed_work(work),
697 struct kobject, release));
701 static void kobject_release(struct kref *kref)
703 struct kobject *kobj = container_of(kref, struct kobject, kref);
704 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
705 unsigned long delay = HZ + HZ * prandom_u32_max(4);
706 pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
707 kobject_name(kobj), kobj, __func__, kobj->parent, delay);
708 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
710 schedule_delayed_work(&kobj->release, delay);
712 kobject_cleanup(kobj);
717 * kobject_put() - Decrement refcount for object.
720 * Decrement the refcount, and if 0, call kobject_cleanup().
722 void kobject_put(struct kobject *kobj)
725 if (!kobj->state_initialized)
727 "kobject: '%s' (%p): is not initialized, yet kobject_put() is being called.\n",
728 kobject_name(kobj), kobj);
729 kref_put(&kobj->kref, kobject_release);
732 EXPORT_SYMBOL(kobject_put);
734 static void dynamic_kobj_release(struct kobject *kobj)
736 pr_debug("kobject: (%p): %s\n", kobj, __func__);
740 static struct kobj_type dynamic_kobj_ktype = {
741 .release = dynamic_kobj_release,
742 .sysfs_ops = &kobj_sysfs_ops,
746 * kobject_create() - Create a struct kobject dynamically.
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.
751 * If the kobject was not able to be created, NULL will be returned.
752 * The kobject structure returned from here must be cleaned up with a
753 * call to kobject_put() and not kfree(), as kobject_init() has
754 * already been called on this structure.
756 static struct kobject *kobject_create(void)
758 struct kobject *kobj;
760 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
764 kobject_init(kobj, &dynamic_kobj_ktype);
769 * kobject_create_and_add() - Create a struct kobject dynamically and
770 * register it with sysfs.
771 * @name: the name for the kobject
772 * @parent: the parent kobject of this kobject, if any.
774 * This function creates a kobject structure dynamically and registers it
775 * with sysfs. When you are finished with this structure, call
776 * kobject_put() and the structure will be dynamically freed when
777 * it is no longer being used.
779 * If the kobject was not able to be created, NULL will be returned.
781 struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
783 struct kobject *kobj;
786 kobj = kobject_create();
790 retval = kobject_add(kobj, parent, "%s", name);
792 pr_warn("%s: kobject_add error: %d\n", __func__, retval);
798 EXPORT_SYMBOL_GPL(kobject_create_and_add);
801 * kset_init() - Initialize a kset for use.
804 void kset_init(struct kset *k)
806 kobject_init_internal(&k->kobj);
807 INIT_LIST_HEAD(&k->list);
808 spin_lock_init(&k->list_lock);
811 /* default kobject attribute operations */
812 static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
815 struct kobj_attribute *kattr;
818 kattr = container_of(attr, struct kobj_attribute, attr);
820 ret = kattr->show(kobj, kattr, buf);
824 static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
825 const char *buf, size_t count)
827 struct kobj_attribute *kattr;
830 kattr = container_of(attr, struct kobj_attribute, attr);
832 ret = kattr->store(kobj, kattr, buf, count);
836 const struct sysfs_ops kobj_sysfs_ops = {
837 .show = kobj_attr_show,
838 .store = kobj_attr_store,
840 EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
843 * kset_register() - Initialize and add a kset.
846 int kset_register(struct kset *k)
854 err = kobject_add_internal(&k->kobj);
857 kobject_uevent(&k->kobj, KOBJ_ADD);
860 EXPORT_SYMBOL(kset_register);
863 * kset_unregister() - Remove a kset.
866 void kset_unregister(struct kset *k)
870 kobject_del(&k->kobj);
871 kobject_put(&k->kobj);
873 EXPORT_SYMBOL(kset_unregister);
876 * kset_find_obj() - Search for object in kset.
877 * @kset: kset we're looking in.
878 * @name: object's name.
880 * Lock kset via @kset->subsys, and iterate over @kset->list,
881 * looking for a matching kobject. If matching object is found
882 * take a reference and return the object.
884 struct kobject *kset_find_obj(struct kset *kset, const char *name)
887 struct kobject *ret = NULL;
889 spin_lock(&kset->list_lock);
891 list_for_each_entry(k, &kset->list, entry) {
892 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
893 ret = kobject_get_unless_zero(k);
898 spin_unlock(&kset->list_lock);
901 EXPORT_SYMBOL_GPL(kset_find_obj);
903 static void kset_release(struct kobject *kobj)
905 struct kset *kset = container_of(kobj, struct kset, kobj);
906 pr_debug("kobject: '%s' (%p): %s\n",
907 kobject_name(kobj), kobj, __func__);
911 static void kset_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
914 kobject_get_ownership(kobj->parent, uid, gid);
917 static struct kobj_type kset_ktype = {
918 .sysfs_ops = &kobj_sysfs_ops,
919 .release = kset_release,
920 .get_ownership = kset_get_ownership,
924 * kset_create() - Create a struct kset dynamically.
926 * @name: the name for the kset
927 * @uevent_ops: a struct kset_uevent_ops for the kset
928 * @parent_kobj: the parent kobject of this kset, if any.
930 * This function creates a kset structure dynamically. This structure can
931 * then be registered with the system and show up in sysfs with a call to
932 * kset_register(). When you are finished with this structure, if
933 * kset_register() has been called, call kset_unregister() and the
934 * structure will be dynamically freed when it is no longer being used.
936 * If the kset was not able to be created, NULL will be returned.
938 static struct kset *kset_create(const char *name,
939 const struct kset_uevent_ops *uevent_ops,
940 struct kobject *parent_kobj)
945 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
948 retval = kobject_set_name(&kset->kobj, "%s", name);
953 kset->uevent_ops = uevent_ops;
954 kset->kobj.parent = parent_kobj;
957 * The kobject of this kset will have a type of kset_ktype and belong to
958 * no kset itself. That way we can properly free it when it is
959 * finished being used.
961 kset->kobj.ktype = &kset_ktype;
962 kset->kobj.kset = NULL;
968 * kset_create_and_add() - Create a struct kset dynamically and add it to sysfs.
970 * @name: the name for the kset
971 * @uevent_ops: a struct kset_uevent_ops for the kset
972 * @parent_kobj: the parent kobject of this kset, if any.
974 * This function creates a kset structure dynamically and registers it
975 * with sysfs. When you are finished with this structure, call
976 * kset_unregister() and the structure will be dynamically freed when it
977 * is no longer being used.
979 * If the kset was not able to be created, NULL will be returned.
981 struct kset *kset_create_and_add(const char *name,
982 const struct kset_uevent_ops *uevent_ops,
983 struct kobject *parent_kobj)
988 kset = kset_create(name, uevent_ops, parent_kobj);
991 error = kset_register(kset);
998 EXPORT_SYMBOL_GPL(kset_create_and_add);
1001 static DEFINE_SPINLOCK(kobj_ns_type_lock);
1002 static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
1004 int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
1006 enum kobj_ns_type type = ops->type;
1009 spin_lock(&kobj_ns_type_lock);
1012 if (type >= KOBJ_NS_TYPES)
1016 if (type <= KOBJ_NS_TYPE_NONE)
1020 if (kobj_ns_ops_tbl[type])
1024 kobj_ns_ops_tbl[type] = ops;
1027 spin_unlock(&kobj_ns_type_lock);
1031 int kobj_ns_type_registered(enum kobj_ns_type type)
1035 spin_lock(&kobj_ns_type_lock);
1036 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
1037 registered = kobj_ns_ops_tbl[type] != NULL;
1038 spin_unlock(&kobj_ns_type_lock);
1043 const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
1045 const struct kobj_ns_type_operations *ops = NULL;
1047 if (parent && parent->ktype && parent->ktype->child_ns_type)
1048 ops = parent->ktype->child_ns_type(parent);
1053 const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
1055 return kobj_child_ns_ops(kobj->parent);
1058 bool kobj_ns_current_may_mount(enum kobj_ns_type type)
1060 bool may_mount = true;
1062 spin_lock(&kobj_ns_type_lock);
1063 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1064 kobj_ns_ops_tbl[type])
1065 may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
1066 spin_unlock(&kobj_ns_type_lock);
1071 void *kobj_ns_grab_current(enum kobj_ns_type type)
1075 spin_lock(&kobj_ns_type_lock);
1076 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1077 kobj_ns_ops_tbl[type])
1078 ns = kobj_ns_ops_tbl[type]->grab_current_ns();
1079 spin_unlock(&kobj_ns_type_lock);
1083 EXPORT_SYMBOL_GPL(kobj_ns_grab_current);
1085 const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
1087 const void *ns = NULL;
1089 spin_lock(&kobj_ns_type_lock);
1090 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1091 kobj_ns_ops_tbl[type])
1092 ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
1093 spin_unlock(&kobj_ns_type_lock);
1098 const void *kobj_ns_initial(enum kobj_ns_type type)
1100 const void *ns = NULL;
1102 spin_lock(&kobj_ns_type_lock);
1103 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1104 kobj_ns_ops_tbl[type])
1105 ns = kobj_ns_ops_tbl[type]->initial_ns();
1106 spin_unlock(&kobj_ns_type_lock);
1111 void kobj_ns_drop(enum kobj_ns_type type, void *ns)
1113 spin_lock(&kobj_ns_type_lock);
1114 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1115 kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
1116 kobj_ns_ops_tbl[type]->drop_ns(ns);
1117 spin_unlock(&kobj_ns_type_lock);
1119 EXPORT_SYMBOL_GPL(kobj_ns_drop);