2 * kobject.c - library routines for handling generic kernel objects
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.
8 * This file is released under the GPLv2.
11 * Please see the file Documentation/kobject.txt for critical information
12 * about using the kobject interface.
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>
23 * kobject_namespace - return @kobj's namespace tag
24 * @kobj: kobject in question
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
30 const void *kobject_namespace(struct kobject
*kobj
)
32 const struct kobj_ns_type_operations
*ns_ops
= kobj_ns_ops(kobj
);
34 if (!ns_ops
|| ns_ops
->type
== KOBJ_NS_TYPE_NONE
)
37 return kobj
->ktype
->namespace(kobj
);
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
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.
50 void kobject_get_ownership(struct kobject
*kobj
, kuid_t
*uid
, kgid_t
*gid
)
52 *uid
= GLOBAL_ROOT_UID
;
53 *gid
= GLOBAL_ROOT_GID
;
55 if (kobj
->ktype
->get_ownership
)
56 kobj
->ktype
->get_ownership(kobj
, uid
, gid
);
60 * populate_dir - populate directory with attributes.
61 * @kobj: object we're working on.
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.
68 static int populate_dir(struct kobject
*kobj
)
70 struct kobj_type
*t
= get_ktype(kobj
);
71 struct attribute
*attr
;
75 if (t
&& t
->default_attrs
) {
76 for (i
= 0; (attr
= t
->default_attrs
[i
]) != NULL
; i
++) {
77 error
= sysfs_create_file(kobj
, attr
);
85 static int create_dir(struct kobject
*kobj
)
87 const struct kobj_ns_type_operations
*ops
;
90 error
= sysfs_create_dir_ns(kobj
, kobject_namespace(kobj
));
94 error
= populate_dir(kobj
);
96 sysfs_remove_dir(kobj
);
101 * @kobj->sd may be deleted by an ancestor going away. Hold an
102 * extra reference so that it stays until @kobj is gone.
107 * If @kobj has ns_ops, its children need to be filtered based on
108 * their namespace tags. Enable namespace support on @kobj->sd.
110 ops
= kobj_child_ns_ops(kobj
);
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
));
116 sysfs_enable_ns(kobj
->sd
);
122 static int get_kobj_path_length(struct kobject
*kobj
)
125 struct kobject
*parent
= kobj
;
127 /* walk up the ancestors until we hit the one pointing to the
129 * Add 1 to strlen for leading '/' of each level.
132 if (kobject_name(parent
) == NULL
)
134 length
+= strlen(kobject_name(parent
)) + 1;
135 parent
= parent
->parent
;
140 static void fill_kobj_path(struct kobject
*kobj
, char *path
, int length
)
142 struct kobject
*parent
;
145 for (parent
= kobj
; parent
; parent
= parent
->parent
) {
146 int cur
= strlen(kobject_name(parent
));
147 /* back up enough to print this name with '/' */
149 strncpy(path
+ length
, kobject_name(parent
), cur
);
150 *(path
+ --length
) = '/';
153 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj
),
154 kobj
, __func__
, path
);
158 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
160 * @kobj: kobject in question, with which to build the path
161 * @gfp_mask: the allocation type used to allocate the path
163 * The result must be freed by the caller with kfree().
165 char *kobject_get_path(struct kobject
*kobj
, gfp_t gfp_mask
)
170 len
= get_kobj_path_length(kobj
);
173 path
= kzalloc(len
, gfp_mask
);
176 fill_kobj_path(kobj
, path
, len
);
180 EXPORT_SYMBOL_GPL(kobject_get_path
);
182 /* add the kobject to its kset's list */
183 static void kobj_kset_join(struct kobject
*kobj
)
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
);
194 /* remove the kobject from its kset's list */
195 static void kobj_kset_leave(struct kobject
*kobj
)
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
);
206 static void kobject_init_internal(struct kobject
*kobj
)
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;
219 static int kobject_add_internal(struct kobject
*kobj
)
222 struct kobject
*parent
;
227 if (!kobj
->name
|| !kobj
->name
[0]) {
228 WARN(1, "kobject: (%p): attempted to be registered with empty "
233 parent
= kobject_get(kobj
->parent
);
235 /* join kset if set, use it as parent if we do not already have one */
238 parent
= kobject_get(&kobj
->kset
->kobj
);
239 kobj_kset_join(kobj
);
240 kobj
->parent
= parent
;
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>");
248 error
= create_dir(kobj
);
250 kobj_kset_leave(kobj
);
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
));
259 pr_err("%s failed for %s (error: %d parent: %s)\n",
260 __func__
, kobject_name(kobj
), error
,
261 parent
? kobject_name(parent
) : "'none'");
263 kobj
->state_in_sysfs
= 1;
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.
274 int kobject_set_name_vargs(struct kobject
*kobj
, const char *fmt
,
279 if (kobj
->name
&& !fmt
)
282 s
= kvasprintf_const(GFP_KERNEL
, fmt
, vargs
);
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.
292 if (strchr(s
, '/')) {
295 t
= kstrdup(s
, GFP_KERNEL
);
299 strreplace(t
, '/', '!');
302 kfree_const(kobj
->name
);
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
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.
317 int kobject_set_name(struct kobject
*kobj
, const char *fmt
, ...)
322 va_start(vargs
, fmt
);
323 retval
= kobject_set_name_vargs(kobj
, fmt
, vargs
);
328 EXPORT_SYMBOL(kobject_set_name
);
331 * kobject_init - initialize a kobject structure
332 * @kobj: pointer to the kobject to initialize
333 * @ktype: pointer to the ktype for this kobject.
335 * This function will properly initialize a kobject such that it can then
336 * be passed to the kobject_add() call.
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.
342 void kobject_init(struct kobject
*kobj
, struct kobj_type
*ktype
)
347 err_str
= "invalid kobject pointer!";
351 err_str
= "must have a ktype to be initialized properly!\n";
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
);
361 kobject_init_internal(kobj
);
366 printk(KERN_ERR
"kobject (%p): %s\n", kobj
, err_str
);
369 EXPORT_SYMBOL(kobject_init
);
371 static __printf(3, 0) int kobject_add_varg(struct kobject
*kobj
,
372 struct kobject
*parent
,
373 const char *fmt
, va_list vargs
)
377 retval
= kobject_set_name_vargs(kobj
, fmt
, vargs
);
379 printk(KERN_ERR
"kobject: can not set name properly!\n");
382 kobj
->parent
= parent
;
383 return kobject_add_internal(kobj
);
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.
392 * The kobject name is set and added to the kobject hierarchy in this
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.
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.
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.
411 int kobject_add(struct kobject
*kobj
, struct kobject
*parent
,
412 const char *fmt
, ...)
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
);
428 retval
= kobject_add_varg(kobj
, parent
, fmt
, args
);
433 EXPORT_SYMBOL(kobject_add
);
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.
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.
446 int kobject_init_and_add(struct kobject
*kobj
, struct kobj_type
*ktype
,
447 struct kobject
*parent
, const char *fmt
, ...)
452 kobject_init(kobj
, ktype
);
455 retval
= kobject_add_varg(kobj
, parent
, fmt
, args
);
460 EXPORT_SYMBOL_GPL(kobject_init_and_add
);
463 * kobject_rename - change the name of an object
464 * @kobj: object in question.
465 * @new_name: object's new name
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.
472 int kobject_rename(struct kobject
*kobj
, const char *new_name
)
475 const char *devpath
= NULL
;
476 const char *dup_name
= NULL
, *name
;
477 char *devpath_string
= NULL
;
480 kobj
= kobject_get(kobj
);
486 devpath
= kobject_get_path(kobj
, GFP_KERNEL
);
491 devpath_string
= kmalloc(strlen(devpath
) + 15, GFP_KERNEL
);
492 if (!devpath_string
) {
496 sprintf(devpath_string
, "DEVPATH_OLD=%s", devpath
);
497 envp
[0] = devpath_string
;
500 name
= dup_name
= kstrdup_const(new_name
, GFP_KERNEL
);
506 error
= sysfs_rename_dir_ns(kobj
, new_name
, kobject_namespace(kobj
));
510 /* Install the new kobject name */
511 dup_name
= kobj
->name
;
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
);
520 kfree_const(dup_name
);
521 kfree(devpath_string
);
527 EXPORT_SYMBOL_GPL(kobject_rename
);
530 * kobject_move - move object to another parent
531 * @kobj: object in question.
532 * @new_parent: object's new parent (can be NULL)
534 int kobject_move(struct kobject
*kobj
, struct kobject
*new_parent
)
537 struct kobject
*old_parent
;
538 const char *devpath
= NULL
;
539 char *devpath_string
= NULL
;
542 kobj
= kobject_get(kobj
);
545 new_parent
= kobject_get(new_parent
);
548 new_parent
= kobject_get(&kobj
->kset
->kobj
);
551 /* old object path */
552 devpath
= kobject_get_path(kobj
, GFP_KERNEL
);
557 devpath_string
= kmalloc(strlen(devpath
) + 15, GFP_KERNEL
);
558 if (!devpath_string
) {
562 sprintf(devpath_string
, "DEVPATH_OLD=%s", devpath
);
563 envp
[0] = devpath_string
;
565 error
= sysfs_move_dir_ns(kobj
, new_parent
, kobject_namespace(kobj
));
568 old_parent
= kobj
->parent
;
569 kobj
->parent
= new_parent
;
571 kobject_put(old_parent
);
572 kobject_uevent_env(kobj
, KOBJ_MOVE
, envp
);
574 kobject_put(new_parent
);
576 kfree(devpath_string
);
580 EXPORT_SYMBOL_GPL(kobject_move
);
583 * kobject_del - unlink kobject from hierarchy.
586 void kobject_del(struct kobject
*kobj
)
588 struct kernfs_node
*sd
;
594 sysfs_remove_dir(kobj
);
597 kobj
->state_in_sysfs
= 0;
598 kobj_kset_leave(kobj
);
599 kobject_put(kobj
->parent
);
602 EXPORT_SYMBOL(kobject_del
);
605 * kobject_get - increment refcount for object.
608 struct kobject
*kobject_get(struct kobject
*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
);
619 EXPORT_SYMBOL(kobject_get
);
621 struct kobject
* __must_check
kobject_get_unless_zero(struct kobject
*kobj
)
625 if (!kref_get_unless_zero(&kobj
->kref
))
629 EXPORT_SYMBOL(kobject_get_unless_zero
);
632 * kobject_cleanup - free kobject resources.
633 * @kobj: object to cleanup
635 static void kobject_cleanup(struct kobject
*kobj
)
637 struct kobj_type
*t
= get_ktype(kobj
);
638 const char *name
= kobj
->name
;
640 pr_debug("kobject: '%s' (%p): %s, parent %p\n",
641 kobject_name(kobj
), kobj
, __func__
, kobj
->parent
);
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
);
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
);
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
);
662 if (t
&& t
->release
) {
663 pr_debug("kobject: '%s' (%p): calling ktype release\n",
664 kobject_name(kobj
), kobj
);
668 /* free name if we allocated it */
670 pr_debug("kobject: '%s': free name\n", name
);
675 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
676 static void kobject_delayed_cleanup(struct work_struct
*work
)
678 kobject_cleanup(container_of(to_delayed_work(work
),
679 struct kobject
, release
));
683 static void kobject_release(struct kref
*kref
)
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
);
692 schedule_delayed_work(&kobj
->release
, delay
);
694 kobject_cleanup(kobj
);
699 * kobject_put - decrement refcount for object.
702 * Decrement the refcount, and if 0, call kobject_cleanup().
704 void kobject_put(struct kobject
*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
);
714 EXPORT_SYMBOL(kobject_put
);
716 static void dynamic_kobj_release(struct kobject
*kobj
)
718 pr_debug("kobject: (%p): %s\n", kobj
, __func__
);
722 static struct kobj_type dynamic_kobj_ktype
= {
723 .release
= dynamic_kobj_release
,
724 .sysfs_ops
= &kobj_sysfs_ops
,
728 * kobject_create - create a struct kobject dynamically
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.
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.
738 struct kobject
*kobject_create(void)
740 struct kobject
*kobj
;
742 kobj
= kzalloc(sizeof(*kobj
), GFP_KERNEL
);
746 kobject_init(kobj
, &dynamic_kobj_ktype
);
751 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
753 * @name: the name for the kobject
754 * @parent: the parent kobject of this kobject, if any.
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.
761 * If the kobject was not able to be created, NULL will be returned.
763 struct kobject
*kobject_create_and_add(const char *name
, struct kobject
*parent
)
765 struct kobject
*kobj
;
768 kobj
= kobject_create();
772 retval
= kobject_add(kobj
, parent
, "%s", name
);
774 printk(KERN_WARNING
"%s: kobject_add error: %d\n",
781 EXPORT_SYMBOL_GPL(kobject_create_and_add
);
784 * kset_init - initialize a kset for use
787 void kset_init(struct kset
*k
)
789 kobject_init_internal(&k
->kobj
);
790 INIT_LIST_HEAD(&k
->list
);
791 spin_lock_init(&k
->list_lock
);
794 /* default kobject attribute operations */
795 static ssize_t
kobj_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
798 struct kobj_attribute
*kattr
;
801 kattr
= container_of(attr
, struct kobj_attribute
, attr
);
803 ret
= kattr
->show(kobj
, kattr
, buf
);
807 static ssize_t
kobj_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
808 const char *buf
, size_t count
)
810 struct kobj_attribute
*kattr
;
813 kattr
= container_of(attr
, struct kobj_attribute
, attr
);
815 ret
= kattr
->store(kobj
, kattr
, buf
, count
);
819 const struct sysfs_ops kobj_sysfs_ops
= {
820 .show
= kobj_attr_show
,
821 .store
= kobj_attr_store
,
823 EXPORT_SYMBOL_GPL(kobj_sysfs_ops
);
826 * kset_register - initialize and add a kset.
829 int kset_register(struct kset
*k
)
837 err
= kobject_add_internal(&k
->kobj
);
840 kobject_uevent(&k
->kobj
, KOBJ_ADD
);
843 EXPORT_SYMBOL(kset_register
);
846 * kset_unregister - remove a kset.
849 void kset_unregister(struct kset
*k
)
853 kobject_del(&k
->kobj
);
854 kobject_put(&k
->kobj
);
856 EXPORT_SYMBOL(kset_unregister
);
859 * kset_find_obj - search for object in kset.
860 * @kset: kset we're looking in.
861 * @name: object's name.
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.
867 struct kobject
*kset_find_obj(struct kset
*kset
, const char *name
)
870 struct kobject
*ret
= NULL
;
872 spin_lock(&kset
->list_lock
);
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
);
881 spin_unlock(&kset
->list_lock
);
884 EXPORT_SYMBOL_GPL(kset_find_obj
);
886 static void kset_release(struct kobject
*kobj
)
888 struct kset
*kset
= container_of(kobj
, struct kset
, kobj
);
889 pr_debug("kobject: '%s' (%p): %s\n",
890 kobject_name(kobj
), kobj
, __func__
);
894 void kset_get_ownership(struct kobject
*kobj
, kuid_t
*uid
, kgid_t
*gid
)
897 kobject_get_ownership(kobj
->parent
, uid
, gid
);
900 static struct kobj_type kset_ktype
= {
901 .sysfs_ops
= &kobj_sysfs_ops
,
902 .release
= kset_release
,
903 .get_ownership
= kset_get_ownership
,
907 * kset_create - create a struct kset dynamically
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.
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.
919 * If the kset was not able to be created, NULL will be returned.
921 static struct kset
*kset_create(const char *name
,
922 const struct kset_uevent_ops
*uevent_ops
,
923 struct kobject
*parent_kobj
)
928 kset
= kzalloc(sizeof(*kset
), GFP_KERNEL
);
931 retval
= kobject_set_name(&kset
->kobj
, "%s", name
);
936 kset
->uevent_ops
= uevent_ops
;
937 kset
->kobj
.parent
= parent_kobj
;
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.
944 kset
->kobj
.ktype
= &kset_ktype
;
945 kset
->kobj
.kset
= NULL
;
951 * kset_create_and_add - create a struct kset dynamically and add it to sysfs
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.
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.
962 * If the kset was not able to be created, NULL will be returned.
964 struct kset
*kset_create_and_add(const char *name
,
965 const struct kset_uevent_ops
*uevent_ops
,
966 struct kobject
*parent_kobj
)
971 kset
= kset_create(name
, uevent_ops
, parent_kobj
);
974 error
= kset_register(kset
);
981 EXPORT_SYMBOL_GPL(kset_create_and_add
);
984 static DEFINE_SPINLOCK(kobj_ns_type_lock
);
985 static const struct kobj_ns_type_operations
*kobj_ns_ops_tbl
[KOBJ_NS_TYPES
];
987 int kobj_ns_type_register(const struct kobj_ns_type_operations
*ops
)
989 enum kobj_ns_type type
= ops
->type
;
992 spin_lock(&kobj_ns_type_lock
);
995 if (type
>= KOBJ_NS_TYPES
)
999 if (type
<= KOBJ_NS_TYPE_NONE
)
1003 if (kobj_ns_ops_tbl
[type
])
1007 kobj_ns_ops_tbl
[type
] = ops
;
1010 spin_unlock(&kobj_ns_type_lock
);
1014 int kobj_ns_type_registered(enum kobj_ns_type type
)
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
);
1026 const struct kobj_ns_type_operations
*kobj_child_ns_ops(struct kobject
*parent
)
1028 const struct kobj_ns_type_operations
*ops
= NULL
;
1030 if (parent
&& parent
->ktype
&& parent
->ktype
->child_ns_type
)
1031 ops
= parent
->ktype
->child_ns_type(parent
);
1036 const struct kobj_ns_type_operations
*kobj_ns_ops(struct kobject
*kobj
)
1038 return kobj_child_ns_ops(kobj
->parent
);
1041 bool kobj_ns_current_may_mount(enum kobj_ns_type type
)
1043 bool may_mount
= true;
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
);
1054 void *kobj_ns_grab_current(enum kobj_ns_type type
)
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
);
1067 const void *kobj_ns_netlink(enum kobj_ns_type type
, struct sock
*sk
)
1069 const void *ns
= NULL
;
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
);
1080 const void *kobj_ns_initial(enum kobj_ns_type type
)
1082 const void *ns
= NULL
;
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
);
1093 void kobj_ns_drop(enum kobj_ns_type type
, void *ns
)
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
);