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>
This commit is contained in:
Greg Kroah-Hartman 2007-11-05 13:16:15 -08:00
parent b727c70289
commit 3f9e3ee9dc
2 changed files with 66 additions and 18 deletions

View File

@ -91,6 +91,9 @@ extern int __must_check kobject_init_and_add(struct kobject *kobj,
extern void kobject_del(struct kobject *); extern void kobject_del(struct kobject *);
extern struct kobject * __must_check kobject_create_and_add(const char *name,
struct kobject *parent);
extern int __must_check kobject_rename(struct kobject *, const char *new_name); extern int __must_check kobject_rename(struct kobject *, const char *new_name);
extern int __must_check kobject_move(struct kobject *, struct kobject *); extern int __must_check kobject_move(struct kobject *, struct kobject *);

View File

@ -619,18 +619,69 @@ void kobject_put(struct kobject * kobj)
kref_put(&kobj->kref, kobject_release); kref_put(&kobj->kref, kobject_release);
} }
static void dynamic_kobj_release(struct kobject *kobj)
static void dir_release(struct kobject *kobj)
{ {
pr_debug("%s: freeing %s\n", __FUNCTION__, kobject_name(kobj));
kfree(kobj); kfree(kobj);
} }
static struct kobj_type dir_ktype = { static struct kobj_type dynamic_kobj_ktype = {
.release = dir_release, .release = dynamic_kobj_release,
.sysfs_ops = NULL,
.default_attrs = NULL,
}; };
/*
* kobject_create - create a struct kobject dynamically
*
* This function creates a kobject structure dynamically and sets it up
* to be a "dynamic" kobject with a default release function set up.
*
* If the kobject was not able to be created, NULL will be returned.
*/
static struct kobject *kobject_create(void)
{
struct kobject *kobj;
kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
if (!kobj)
return NULL;
kobject_init_ng(kobj, &dynamic_kobj_ktype);
return kobj;
}
/**
* kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
*
* @name: the name for the kset
* @parent: the parent kobject of this kobject, if any.
*
* This function creates a kset structure dynamically and registers it
* with sysfs. When you are finished with this structure, call
* kobject_unregister() and the structure will be dynamically freed when
* it is no longer being used.
*
* If the kobject was not able to be created, NULL will be returned.
*/
struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
{
struct kobject *kobj;
int retval;
kobj = kobject_create();
if (!kobj)
return NULL;
retval = kobject_add_ng(kobj, parent, "%s", name);
if (retval) {
printk(KERN_WARNING "%s: kobject_add error: %d\n",
__FUNCTION__, retval);
kobject_put(kobj);
kobj = NULL;
}
return kobj;
}
EXPORT_SYMBOL_GPL(kobject_create_and_add);
/** /**
* kobject_kset_add_dir - add sub directory of object. * kobject_kset_add_dir - add sub directory of object.
* @kset: kset the directory is belongs to. * @kset: kset the directory is belongs to.
@ -645,23 +696,17 @@ struct kobject *kobject_kset_add_dir(struct kset *kset,
struct kobject *k; struct kobject *k;
int ret; int ret;
if (!parent) k = kobject_create();
return NULL;
k = kzalloc(sizeof(*k), GFP_KERNEL);
if (!k) if (!k)
return NULL; return NULL;
k->kset = kset; k->kset = kset;
k->parent = parent; ret = kobject_add_ng(k, parent, "%s", name);
k->ktype = &dir_ktype;
kobject_set_name(k, name);
ret = kobject_register(k);
if (ret < 0) { if (ret < 0) {
printk(KERN_WARNING "%s: kobject_register error: %d\n", printk(KERN_WARNING "%s: kobject_add error: %d\n",
__func__, ret); __func__, ret);
kobject_del(k); kobject_put(k);
return NULL; k = NULL;
} }
return k; return k;
@ -676,7 +721,7 @@ struct kobject *kobject_kset_add_dir(struct kset *kset,
*/ */
struct kobject *kobject_add_dir(struct kobject *parent, const char *name) struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
{ {
return kobject_kset_add_dir(NULL, parent, name); return kobject_create_and_add(name, parent);
} }
/** /**