diff options
author | Greg Kroah-Hartman <gregkh@suse.de> | 2007-11-05 16:16:15 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2008-01-24 23:40:10 -0500 |
commit | 3f9e3ee9dc3605e5c593b5d708494571fb0d3970 (patch) | |
tree | b5490463c14b919f88ec18833846a38cfb621aa3 | |
parent | b727c702896f88d2ff6c3e03bd011d7c3dffe3e1 (diff) |
kobject: add kobject_create_and_add function
This lets users create dynamic kobjects much easier.
Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
-rw-r--r-- | include/linux/kobject.h | 3 | ||||
-rw-r--r-- | lib/kobject.c | 81 |
2 files changed, 66 insertions, 18 deletions
diff --git a/include/linux/kobject.h b/include/linux/kobject.h index f91aeb745664..33e7a6142a75 100644 --- a/include/linux/kobject.h +++ b/include/linux/kobject.h | |||
@@ -91,6 +91,9 @@ extern int __must_check kobject_init_and_add(struct kobject *kobj, | |||
91 | 91 | ||
92 | extern void kobject_del(struct kobject *); | 92 | extern void kobject_del(struct kobject *); |
93 | 93 | ||
94 | extern struct kobject * __must_check kobject_create_and_add(const char *name, | ||
95 | struct kobject *parent); | ||
96 | |||
94 | extern int __must_check kobject_rename(struct kobject *, const char *new_name); | 97 | extern int __must_check kobject_rename(struct kobject *, const char *new_name); |
95 | extern int __must_check kobject_move(struct kobject *, struct kobject *); | 98 | extern int __must_check kobject_move(struct kobject *, struct kobject *); |
96 | 99 | ||
diff --git a/lib/kobject.c b/lib/kobject.c index 4fb27ba28807..98422a3eeffc 100644 --- a/lib/kobject.c +++ b/lib/kobject.c | |||
@@ -619,18 +619,69 @@ void kobject_put(struct kobject * kobj) | |||
619 | kref_put(&kobj->kref, kobject_release); | 619 | kref_put(&kobj->kref, kobject_release); |
620 | } | 620 | } |
621 | 621 | ||
622 | 622 | static void dynamic_kobj_release(struct kobject *kobj) | |
623 | static void dir_release(struct kobject *kobj) | ||
624 | { | 623 | { |
624 | pr_debug("%s: freeing %s\n", __FUNCTION__, kobject_name(kobj)); | ||
625 | kfree(kobj); | 625 | kfree(kobj); |
626 | } | 626 | } |
627 | 627 | ||
628 | static struct kobj_type dir_ktype = { | 628 | static struct kobj_type dynamic_kobj_ktype = { |
629 | .release = dir_release, | 629 | .release = dynamic_kobj_release, |
630 | .sysfs_ops = NULL, | ||
631 | .default_attrs = NULL, | ||
632 | }; | 630 | }; |
633 | 631 | ||
632 | /* | ||
633 | * kobject_create - create a struct kobject dynamically | ||
634 | * | ||
635 | * This function creates a kobject structure dynamically and sets it up | ||
636 | * to be a "dynamic" kobject with a default release function set up. | ||
637 | * | ||
638 | * If the kobject was not able to be created, NULL will be returned. | ||
639 | */ | ||
640 | static struct kobject *kobject_create(void) | ||
641 | { | ||
642 | struct kobject *kobj; | ||
643 | |||
644 | kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); | ||
645 | if (!kobj) | ||
646 | return NULL; | ||
647 | |||
648 | kobject_init_ng(kobj, &dynamic_kobj_ktype); | ||
649 | return kobj; | ||
650 | } | ||
651 | |||
652 | /** | ||
653 | * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs | ||
654 | * | ||
655 | * @name: the name for the kset | ||
656 | * @parent: the parent kobject of this kobject, if any. | ||
657 | * | ||
658 | * This function creates a kset structure dynamically and registers it | ||
659 | * with sysfs. When you are finished with this structure, call | ||
660 | * kobject_unregister() and the structure will be dynamically freed when | ||
661 | * it is no longer being used. | ||
662 | * | ||
663 | * If the kobject was not able to be created, NULL will be returned. | ||
664 | */ | ||
665 | struct kobject *kobject_create_and_add(const char *name, struct kobject *parent) | ||
666 | { | ||
667 | struct kobject *kobj; | ||
668 | int retval; | ||
669 | |||
670 | kobj = kobject_create(); | ||
671 | if (!kobj) | ||
672 | return NULL; | ||
673 | |||
674 | retval = kobject_add_ng(kobj, parent, "%s", name); | ||
675 | if (retval) { | ||
676 | printk(KERN_WARNING "%s: kobject_add error: %d\n", | ||
677 | __FUNCTION__, retval); | ||
678 | kobject_put(kobj); | ||
679 | kobj = NULL; | ||
680 | } | ||
681 | return kobj; | ||
682 | } | ||
683 | EXPORT_SYMBOL_GPL(kobject_create_and_add); | ||
684 | |||
634 | /** | 685 | /** |
635 | * kobject_kset_add_dir - add sub directory of object. | 686 | * kobject_kset_add_dir - add sub directory of object. |
636 | * @kset: kset the directory is belongs to. | 687 | * @kset: kset the directory is belongs to. |
@@ -645,23 +696,17 @@ struct kobject *kobject_kset_add_dir(struct kset *kset, | |||
645 | struct kobject *k; | 696 | struct kobject *k; |
646 | int ret; | 697 | int ret; |
647 | 698 | ||
648 | if (!parent) | 699 | k = kobject_create(); |
649 | return NULL; | ||
650 | |||
651 | k = kzalloc(sizeof(*k), GFP_KERNEL); | ||
652 | if (!k) | 700 | if (!k) |
653 | return NULL; | 701 | return NULL; |
654 | 702 | ||
655 | k->kset = kset; | 703 | k->kset = kset; |
656 | k->parent = parent; | 704 | ret = kobject_add_ng(k, parent, "%s", name); |
657 | k->ktype = &dir_ktype; | ||
658 | kobject_set_name(k, name); | ||
659 | ret = kobject_register(k); | ||
660 | if (ret < 0) { | 705 | if (ret < 0) { |
661 | printk(KERN_WARNING "%s: kobject_register error: %d\n", | 706 | printk(KERN_WARNING "%s: kobject_add error: %d\n", |
662 | __func__, ret); | 707 | __func__, ret); |
663 | kobject_del(k); | 708 | kobject_put(k); |
664 | return NULL; | 709 | k = NULL; |
665 | } | 710 | } |
666 | 711 | ||
667 | return k; | 712 | return k; |
@@ -676,7 +721,7 @@ struct kobject *kobject_kset_add_dir(struct kset *kset, | |||
676 | */ | 721 | */ |
677 | struct kobject *kobject_add_dir(struct kobject *parent, const char *name) | 722 | struct kobject *kobject_add_dir(struct kobject *parent, const char *name) |
678 | { | 723 | { |
679 | return kobject_kset_add_dir(NULL, parent, name); | 724 | return kobject_create_and_add(name, parent); |
680 | } | 725 | } |
681 | 726 | ||
682 | /** | 727 | /** |