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/module.h>
18 #include <linux/stat.h>
19 #include <linux/slab.h>
20 #include <linux/kallsyms.h>
21 #include <asm-generic/sections.h>
24 * populate_dir - populate directory with attributes.
25 * @kobj: object we're working on.
27 * Most subsystems have a set of default attributes that
28 * are associated with an object that registers with them.
29 * This is a helper called during object registration that
30 * loops through the default attributes of the subsystem
31 * and creates attributes files for them in sysfs.
35 static int populate_dir(struct kobject
* kobj
)
37 struct kobj_type
* t
= get_ktype(kobj
);
38 struct attribute
* attr
;
42 if (t
&& t
->default_attrs
) {
43 for (i
= 0; (attr
= t
->default_attrs
[i
]) != NULL
; i
++) {
44 if ((error
= sysfs_create_file(kobj
,attr
)))
51 static int create_dir(struct kobject
* kobj
)
54 if (kobject_name(kobj
)) {
55 error
= sysfs_create_dir(kobj
);
57 if ((error
= populate_dir(kobj
)))
58 sysfs_remove_dir(kobj
);
64 static inline struct kobject
* to_kobj(struct list_head
* entry
)
66 return container_of(entry
,struct kobject
,entry
);
69 static int get_kobj_path_length(struct kobject
*kobj
)
72 struct kobject
* parent
= kobj
;
74 /* walk up the ancestors until we hit the one pointing to the
76 * Add 1 to strlen for leading '/' of each level.
79 if (kobject_name(parent
) == NULL
)
81 length
+= strlen(kobject_name(parent
)) + 1;
82 parent
= parent
->parent
;
87 static void fill_kobj_path(struct kobject
*kobj
, char *path
, int length
)
89 struct kobject
* parent
;
92 for (parent
= kobj
; parent
; parent
= parent
->parent
) {
93 int cur
= strlen(kobject_name(parent
));
94 /* back up enough to print this name with '/' */
96 strncpy (path
+ length
, kobject_name(parent
), cur
);
97 *(path
+ --length
) = '/';
100 pr_debug("%s: path = '%s'\n",__FUNCTION__
,path
);
104 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
106 * @kobj: kobject in question, with which to build the path
107 * @gfp_mask: the allocation type used to allocate the path
109 * The result must be freed by the caller with kfree().
111 char *kobject_get_path(struct kobject
*kobj
, gfp_t gfp_mask
)
116 len
= get_kobj_path_length(kobj
);
119 path
= kzalloc(len
, gfp_mask
);
122 fill_kobj_path(kobj
, path
, len
);
126 EXPORT_SYMBOL_GPL(kobject_get_path
);
129 static int ptr_in_range(void *ptr
, void *start
, void *end
)
132 * This should hopefully get rid of causing warnings
133 * if the architecture did not set one of the section
139 if ((ptr
>= start
) && (ptr
< end
))
144 void verify_dynamic_kobject_allocation(struct kobject
*kobj
)
149 namebuf
= kzalloc(KSYM_NAME_LEN
, GFP_KERNEL
);
150 ret
= kallsyms_lookup((unsigned long)kobj
, NULL
, NULL
, NULL
,
153 * This is the X86_32-only part of this function.
154 * This is here because it is valid to have a kobject
155 * in an __init section, but only after those
156 * sections have been freed back to the dynamic pool.
158 if (!initmem_now_dynamic
&&
159 ptr_in_range(kobj
, __init_begin
, __init_end
))
161 if (!ret
|| !strlen(ret
))
163 pr_debug("---- begin silly warning ----\n");
164 pr_debug("This is a janitorial warning, not a kernel bug.\n");
165 #ifdef CONFIG_DEBUG_KOBJECT
166 pr_debug("The kobject at, or inside '%s'@(0x%p) is not dynamically allocated.\n",
169 pr_debug("kobjects must be dynamically allocated, not static\n");
171 pr_debug("---- end silly warning ----\n");
176 static void verify_dynamic_kobject_allocation(struct kobject
*kobj
)
182 * kobject_init - initialize object.
183 * @kobj: object in question.
185 void kobject_init(struct kobject
* kobj
)
189 WARN_ON(atomic_read(&kobj
->kref
.refcount
));
190 verify_dynamic_kobject_allocation(kobj
);
191 kref_init(&kobj
->kref
);
192 INIT_LIST_HEAD(&kobj
->entry
);
193 kobj
->kset
= kset_get(kobj
->kset
);
198 * unlink - remove kobject from kset list.
201 * Remove the kobject from the kset list and decrement
202 * its parent's refcount.
203 * This is separated out, so we can use it in both
204 * kobject_del() and kobject_add() on error.
207 static void unlink(struct kobject
* kobj
)
210 spin_lock(&kobj
->kset
->list_lock
);
211 list_del_init(&kobj
->entry
);
212 spin_unlock(&kobj
->kset
->list_lock
);
218 * kobject_add - add an object to the hierarchy.
222 int kobject_add(struct kobject
* kobj
)
225 struct kobject
* parent
;
227 if (!(kobj
= kobject_get(kobj
)))
230 kobject_set_name(kobj
, "NO_NAME");
231 if (!*kobj
->k_name
) {
232 pr_debug("kobject attempted to be registered with no name!\n");
237 parent
= kobject_get(kobj
->parent
);
239 pr_debug("kobject %s: registering. parent: %s, set: %s\n",
240 kobject_name(kobj
), parent
? kobject_name(parent
) : "<NULL>",
241 kobj
->kset
? kobject_name(&kobj
->kset
->kobj
) : "<NULL>" );
244 spin_lock(&kobj
->kset
->list_lock
);
247 parent
= kobject_get(&kobj
->kset
->kobj
);
249 * If the kset is our parent, get a second
250 * reference, we drop both the kset and the
251 * parent ref on cleanup
256 list_add_tail(&kobj
->entry
,&kobj
->kset
->list
);
257 spin_unlock(&kobj
->kset
->list_lock
);
258 kobj
->parent
= parent
;
261 error
= create_dir(kobj
);
263 /* unlink does the kobject_put() for us */
267 /* be noisy on error issues */
268 if (error
== -EEXIST
)
269 printk(KERN_ERR
"kobject_add failed for %s with "
270 "-EEXIST, don't try to register things with "
271 "the same name in the same directory.\n",
274 printk(KERN_ERR
"kobject_add failed for %s (%d)\n",
275 kobject_name(kobj
), error
);
283 * kobject_register - initialize and add an object.
284 * @kobj: object in question.
287 int kobject_register(struct kobject
* kobj
)
292 error
= kobject_add(kobj
);
294 kobject_uevent(kobj
, KOBJ_ADD
);
301 * kobject_set_name - Set the name of an object
303 * @fmt: format string used to build the name
305 * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
306 * string that @kobj->k_name points to. Otherwise, use the static
309 int kobject_set_name(struct kobject
* kobj
, const char * fmt
, ...)
317 /* find out how big a buffer we need */
318 name
= kmalloc(1024, GFP_KERNEL
);
324 need
= vsnprintf(name
, 1024, fmt
, args
);
328 /* Allocate the new space and copy the string in */
330 name
= kmalloc(limit
, GFP_KERNEL
);
336 need
= vsnprintf(name
, limit
, fmt
, args
);
339 /* something wrong with the string we copied? */
346 /* Free the old name, if necessary. */
349 /* Now, set the new name */
354 EXPORT_SYMBOL(kobject_set_name
);
357 * kobject_rename - change the name of an object
358 * @kobj: object in question.
359 * @new_name: object's new name
362 int kobject_rename(struct kobject
* kobj
, const char *new_name
)
365 const char *devpath
= NULL
;
366 char *devpath_string
= NULL
;
369 kobj
= kobject_get(kobj
);
375 /* see if this name is already in use */
377 struct kobject
*temp_kobj
;
378 temp_kobj
= kset_find_obj(kobj
->kset
, new_name
);
380 printk(KERN_WARNING
"kobject '%s' can not be renamed "
381 "to '%s' as '%s' is already in existance.\n",
382 kobject_name(kobj
), new_name
, new_name
);
383 kobject_put(temp_kobj
);
388 devpath
= kobject_get_path(kobj
, GFP_KERNEL
);
393 devpath_string
= kmalloc(strlen(devpath
) + 15, GFP_KERNEL
);
394 if (!devpath_string
) {
398 sprintf(devpath_string
, "DEVPATH_OLD=%s", devpath
);
399 envp
[0] = devpath_string
;
402 error
= sysfs_rename_dir(kobj
, new_name
);
404 /* This function is mostly/only used for network interface.
405 * Some hotplug package track interfaces by their name and
406 * therefore want to know when the name is changed by the user. */
408 kobject_uevent_env(kobj
, KOBJ_MOVE
, envp
);
411 kfree(devpath_string
);
419 * kobject_move - move object to another parent
420 * @kobj: object in question.
421 * @new_parent: object's new parent (can be NULL)
424 int kobject_move(struct kobject
*kobj
, struct kobject
*new_parent
)
427 struct kobject
*old_parent
;
428 const char *devpath
= NULL
;
429 char *devpath_string
= NULL
;
432 kobj
= kobject_get(kobj
);
435 new_parent
= kobject_get(new_parent
);
438 new_parent
= kobject_get(&kobj
->kset
->kobj
);
440 /* old object path */
441 devpath
= kobject_get_path(kobj
, GFP_KERNEL
);
446 devpath_string
= kmalloc(strlen(devpath
) + 15, GFP_KERNEL
);
447 if (!devpath_string
) {
451 sprintf(devpath_string
, "DEVPATH_OLD=%s", devpath
);
452 envp
[0] = devpath_string
;
454 error
= sysfs_move_dir(kobj
, new_parent
);
457 old_parent
= kobj
->parent
;
458 kobj
->parent
= new_parent
;
460 kobject_put(old_parent
);
461 kobject_uevent_env(kobj
, KOBJ_MOVE
, envp
);
463 kobject_put(new_parent
);
465 kfree(devpath_string
);
471 * kobject_del - unlink kobject from hierarchy.
475 void kobject_del(struct kobject
* kobj
)
479 sysfs_remove_dir(kobj
);
484 * kobject_unregister - remove object from hierarchy and decrement refcount.
485 * @kobj: object going away.
488 void kobject_unregister(struct kobject
* kobj
)
492 pr_debug("kobject %s: unregistering\n",kobject_name(kobj
));
493 kobject_uevent(kobj
, KOBJ_REMOVE
);
499 * kobject_get - increment refcount for object.
503 struct kobject
* kobject_get(struct kobject
* kobj
)
506 kref_get(&kobj
->kref
);
511 * kobject_cleanup - free kobject resources.
515 void kobject_cleanup(struct kobject
* kobj
)
517 struct kobj_type
* t
= get_ktype(kobj
);
518 struct kset
* s
= kobj
->kset
;
519 struct kobject
* parent
= kobj
->parent
;
520 const char *name
= kobj
->k_name
;
522 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj
));
523 if (t
&& t
->release
) {
525 /* If we have a release function, we can guess that this was
526 * not a statically allocated kobject, so we should be safe to
530 pr_debug("kobject '%s' does not have a release() function, "
531 "if this is not a directory kobject, it is broken "
532 "and must be fixed.\n", name
);
539 static void kobject_release(struct kref
*kref
)
541 kobject_cleanup(container_of(kref
, struct kobject
, kref
));
545 * kobject_put - decrement refcount for object.
548 * Decrement the refcount, and if 0, call kobject_cleanup().
550 void kobject_put(struct kobject
* kobj
)
553 kref_put(&kobj
->kref
, kobject_release
);
556 static void dynamic_kobj_release(struct kobject
*kobj
)
558 pr_debug("%s: freeing %s\n", __FUNCTION__
, kobject_name(kobj
));
562 static struct kobj_type dynamic_kobj_ktype
= {
563 .release
= dynamic_kobj_release
,
564 .sysfs_ops
= &kobj_sysfs_ops
,
568 * kobject_create - create a struct kobject dynamically
570 * @name: the name for the kset
571 * @parent: the parent kobject of this kobject, if any.
573 * This function creates a kobject structure dynamically and sets it up
574 * to be a "dynamic" kobject with a default release function set up.
576 * If the kobject was not able to be created, NULL will be returned.
578 struct kobject
*kobject_create(const char *name
, struct kobject
*parent
)
580 struct kobject
*kobj
;
582 kobj
= kzalloc(sizeof(*kobj
), GFP_KERNEL
);
586 kobject_set_name(kobj
, name
);
587 kobj
->parent
= parent
;
588 kobj
->ktype
= &dynamic_kobj_ktype
;
593 * kobject_create_and_register - create a struct kobject dynamically and register it with sysfs
595 * @name: the name for the kset
596 * @parent: the parent kobject of this kobject, if any.
598 * This function creates a kset structure dynamically and registers it
599 * with sysfs. When you are finished with this structure, call
600 * kobject_unregister() and the structure will be dynamically freed when
601 * it is no longer being used.
603 * If the kobject was not able to be created, NULL will be returned.
605 struct kobject
*kobject_create_and_register(const char *name
,
606 struct kobject
*parent
)
608 struct kobject
*kobj
;
611 kobj
= kobject_create(name
, parent
);
615 retval
= kobject_register(kobj
);
617 printk(KERN_WARNING
"%s: kobject_register error: %d\n",
618 __FUNCTION__
, retval
);
624 EXPORT_SYMBOL_GPL(kobject_create_and_register
);
627 * kset_init - initialize a kset for use
631 void kset_init(struct kset
* k
)
633 kobject_init(&k
->kobj
);
634 INIT_LIST_HEAD(&k
->list
);
635 spin_lock_init(&k
->list_lock
);
638 /* default kobject attribute operations */
639 static ssize_t
kobj_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
642 struct kobj_attribute
*kattr
;
645 kattr
= container_of(attr
, struct kobj_attribute
, attr
);
647 ret
= kattr
->show(kobj
, kattr
, buf
);
651 static ssize_t
kobj_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
652 const char *buf
, size_t count
)
654 struct kobj_attribute
*kattr
;
657 kattr
= container_of(attr
, struct kobj_attribute
, attr
);
659 ret
= kattr
->store(kobj
, kattr
, buf
, count
);
663 struct sysfs_ops kobj_sysfs_ops
= {
664 .show
= kobj_attr_show
,
665 .store
= kobj_attr_store
,
669 * kset_add - add a kset object to the hierarchy.
673 int kset_add(struct kset
* k
)
675 return kobject_add(&k
->kobj
);
680 * kset_register - initialize and add a kset.
684 int kset_register(struct kset
* k
)
695 kobject_uevent(&k
->kobj
, KOBJ_ADD
);
701 * kset_unregister - remove a kset.
705 void kset_unregister(struct kset
* k
)
709 kobject_unregister(&k
->kobj
);
714 * kset_find_obj - search for object in kset.
715 * @kset: kset we're looking in.
716 * @name: object's name.
718 * Lock kset via @kset->subsys, and iterate over @kset->list,
719 * looking for a matching kobject. If matching object is found
720 * take a reference and return the object.
723 struct kobject
* kset_find_obj(struct kset
* kset
, const char * name
)
725 struct list_head
* entry
;
726 struct kobject
* ret
= NULL
;
728 spin_lock(&kset
->list_lock
);
729 list_for_each(entry
,&kset
->list
) {
730 struct kobject
* k
= to_kobj(entry
);
731 if (kobject_name(k
) && !strcmp(kobject_name(k
),name
)) {
732 ret
= kobject_get(k
);
736 spin_unlock(&kset
->list_lock
);
740 static void kset_release(struct kobject
*kobj
)
742 struct kset
*kset
= container_of(kobj
, struct kset
, kobj
);
743 pr_debug("kset %s: now freed\n", kobject_name(kobj
));
747 static struct kobj_type kset_ktype
= {
748 .sysfs_ops
= &kobj_sysfs_ops
,
749 .release
= kset_release
,
753 * kset_create - create a struct kset dynamically
755 * @name: the name for the kset
756 * @uevent_ops: a struct kset_uevent_ops for the kset
757 * @parent_kobj: the parent kobject of this kset, if any.
758 * @parent_kset: the parent kset of this kset, if any.
760 * This function creates a kset structure dynamically. This structure can
761 * then be registered with the system and show up in sysfs with a call to
762 * kset_register(). When you are finished with this structure, if
763 * kset_register() has been called, call kset_unregister() and the
764 * structure will be dynamically freed when it is no longer being used.
766 * If the kset was not able to be created, NULL will be returned.
768 * NOTE, you can not have both a @parent_kobj and a @parent_kset, pick one
771 static struct kset
*kset_create(const char *name
,
772 struct kset_uevent_ops
*uevent_ops
,
773 struct kobject
*parent_kobj
,
774 struct kset
*parent_kset
)
778 if ((parent_kobj
) && (parent_kset
)) {
779 printk(KERN_WARNING
"Can not specify both a parent kset and a "
780 "parent kobject for %s\n", __FUNCTION__
);
785 kset
= kzalloc(sizeof(*kset
), GFP_KERNEL
);
788 kobject_set_name(&kset
->kobj
, name
);
789 kset
->uevent_ops
= uevent_ops
;
790 kset
->kobj
.parent
= parent_kobj
;
791 kset
->kobj
.kset
= parent_kset
;
792 kset
->kobj
.ktype
= &kset_ktype
;
798 * kset_create_and_register - create a struct kset dynamically and register it with sysfs
800 * @name: the name for the kset
801 * @uevent_ops: a struct kset_uevent_ops for the kset
802 * @parent_kobj: the parent kobject of this kset, if any.
803 * @parent_kset: the parent kset of this kset, if any.
805 * This function creates a kset structure dynamically and registers it
806 * with sysfs. When you are finished with this structure, call
807 * kset_unregister() and the structure will be dynamically freed when it
808 * is no longer being used.
810 * If the kset was not able to be created, NULL will be returned.
812 * NOTE, you can not have both a @parent_kobj and a @parent_kset, pick one
815 struct kset
*kset_create_and_register(const char *name
,
816 struct kset_uevent_ops
*uevent_ops
,
817 struct kobject
*parent_kobj
,
818 struct kset
*parent_kset
)
823 kset
= kset_create(name
, uevent_ops
, parent_kobj
, parent_kset
);
826 error
= kset_register(kset
);
833 EXPORT_SYMBOL_GPL(kset_create_and_register
);
835 EXPORT_SYMBOL(kobject_init
);
836 EXPORT_SYMBOL(kobject_register
);
837 EXPORT_SYMBOL(kobject_unregister
);
838 EXPORT_SYMBOL(kobject_get
);
839 EXPORT_SYMBOL(kobject_put
);
840 EXPORT_SYMBOL(kobject_add
);
841 EXPORT_SYMBOL(kobject_del
);
843 EXPORT_SYMBOL(kset_register
);
844 EXPORT_SYMBOL(kset_unregister
);