X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fkobject.c;h=7ce6dc138e90ed391618b222847a664024daf402;hb=refs%2Fheads%2Fvserver;hp=8737dfd72d36072ffb62da436bc2947b78c02a2b;hpb=6a77f38946aaee1cd85eeec6cf4229b204c15071;p=linux-2.6.git diff --git a/lib/kobject.c b/lib/kobject.c index 8737dfd72..7ce6dc138 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -14,6 +14,7 @@ #include #include #include +#include /** * populate_dir - populate directory with attributes. @@ -71,6 +72,8 @@ static int get_kobj_path_length(struct kobject *kobj) * Add 1 to strlen for leading '/' of each level. */ do { + if (kobject_name(parent) == NULL) + return 0; length += strlen(kobject_name(parent)) + 1; parent = parent->parent; } while (parent); @@ -100,20 +103,22 @@ static void fill_kobj_path(struct kobject *kobj, char *path, int length) * @kobj: kobject in question, with which to build the path * @gfp_mask: the allocation type used to allocate the path */ -char *kobject_get_path(struct kobject *kobj, int gfp_mask) +char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask) { char *path; int len; len = get_kobj_path_length(kobj); - path = kmalloc(len, gfp_mask); + if (len == 0) + return NULL; + path = kzalloc(len, gfp_mask); if (!path) return NULL; - memset(path, 0x00, len); fill_kobj_path(kobj, path, len); return path; } +EXPORT_SYMBOL_GPL(kobject_get_path); /** * kobject_init - initialize object. @@ -123,6 +128,7 @@ void kobject_init(struct kobject * kobj) { kref_init(&kobj->kref); INIT_LIST_HEAD(&kobj->entry); + init_waitqueue_head(&kobj->poll); kobj->kset = kset_get(kobj->kset); } @@ -140,9 +146,9 @@ void kobject_init(struct kobject * kobj) static void unlink(struct kobject * kobj) { if (kobj->kset) { - down_write(&kobj->kset->subsys->rwsem); + spin_lock(&kobj->kset->list_lock); list_del_init(&kobj->entry); - up_write(&kobj->kset->subsys->rwsem); + spin_unlock(&kobj->kset->list_lock); } kobject_put(kobj); } @@ -161,6 +167,11 @@ int kobject_add(struct kobject * kobj) return -ENOENT; if (!kobj->k_name) kobj->k_name = kobj->name; + if (!kobj->k_name) { + pr_debug("kobject attempted to be registered with no name!\n"); + WARN_ON(1); + return -EINVAL; + } parent = kobject_get(kobj->parent); pr_debug("kobject %s: registering. parent: %s, set: %s\n", @@ -168,13 +179,13 @@ int kobject_add(struct kobject * kobj) kobj->kset ? kobj->kset->kobj.name : "" ); if (kobj->kset) { - down_write(&kobj->kset->subsys->rwsem); + spin_lock(&kobj->kset->list_lock); if (!parent) parent = kobject_get(&kobj->kset->kobj); list_add_tail(&kobj->entry,&kobj->kset->list); - up_write(&kobj->kset->subsys->rwsem); + spin_unlock(&kobj->kset->list_lock); } kobj->parent = parent; @@ -184,8 +195,17 @@ int kobject_add(struct kobject * kobj) unlink(kobj); if (parent) kobject_put(parent); - } else { - kobject_hotplug(kobj, KOBJ_ADD); + + /* be noisy on error issues */ + if (error == -EEXIST) + printk("kobject_add failed for %s with -EEXIST, " + "don't try to register things with the " + "same name in the same directory.\n", + kobject_name(kobj)); + else + printk("kobject_add failed for %s (%d)\n", + kobject_name(kobj), error); + dump_stack(); } return error; @@ -199,17 +219,13 @@ int kobject_add(struct kobject * kobj) int kobject_register(struct kobject * kobj) { - int error = 0; + int error = -EINVAL; if (kobj) { kobject_init(kobj); error = kobject_add(kobj); - if (error) { - printk("kobject_register failed for %s (%d)\n", - kobject_name(kobj),error); - dump_stack(); - } - } else - error = -EINVAL; + if (!error) + kobject_uevent(kobj, KOBJ_ADD); + } return error; } @@ -217,13 +233,12 @@ int kobject_register(struct kobject * kobj) /** * kobject_set_name - Set the name of an object * @kobj: object. - * @name: name. + * @fmt: format string used to build the name * * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated * string that @kobj->k_name points to. Otherwise, use the static * @kobj->name array. */ - int kobject_set_name(struct kobject * kobj, const char * fmt, ...) { int error = 0; @@ -281,7 +296,7 @@ EXPORT_SYMBOL(kobject_set_name); * @new_name: object's new name */ -int kobject_rename(struct kobject * kobj, char *new_name) +int kobject_rename(struct kobject * kobj, const char *new_name) { int error = 0; @@ -294,6 +309,56 @@ int kobject_rename(struct kobject * kobj, char *new_name) return error; } +/** + * kobject_move - move object to another parent + * @kobj: object in question. + * @new_parent: object's new parent + */ + +int kobject_move(struct kobject *kobj, struct kobject *new_parent) +{ + int error; + struct kobject *old_parent; + const char *devpath = NULL; + char *devpath_string = NULL; + char *envp[2]; + + kobj = kobject_get(kobj); + if (!kobj) + return -EINVAL; + new_parent = kobject_get(new_parent); + if (!new_parent) { + error = -EINVAL; + goto out; + } + /* old object path */ + devpath = kobject_get_path(kobj, GFP_KERNEL); + if (!devpath) { + error = -ENOMEM; + goto out; + } + devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL); + if (!devpath_string) { + error = -ENOMEM; + goto out; + } + sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); + envp[0] = devpath_string; + envp[1] = NULL; + error = sysfs_move_dir(kobj, new_parent); + if (error) + goto out; + old_parent = kobj->parent; + kobj->parent = new_parent; + kobject_put(old_parent); + kobject_uevent_env(kobj, KOBJ_MOVE, envp); +out: + kobject_put(kobj); + kfree(devpath_string); + kfree(devpath); + return error; +} + /** * kobject_del - unlink kobject from hierarchy. * @kobj: object. @@ -301,7 +366,6 @@ int kobject_rename(struct kobject * kobj, char *new_name) void kobject_del(struct kobject * kobj) { - kobject_hotplug(kobj, KOBJ_REMOVE); sysfs_remove_dir(kobj); unlink(kobj); } @@ -314,6 +378,7 @@ void kobject_del(struct kobject * kobj) void kobject_unregister(struct kobject * kobj) { pr_debug("kobject %s: unregistering\n",kobject_name(kobj)); + kobject_uevent(kobj, KOBJ_REMOVE); kobject_del(kobj); kobject_put(kobj); } @@ -371,6 +436,50 @@ void kobject_put(struct kobject * kobj) } +static void dir_release(struct kobject *kobj) +{ + kfree(kobj); +} + +static struct kobj_type dir_ktype = { + .release = dir_release, + .sysfs_ops = NULL, + .default_attrs = NULL, +}; + +/** + * kobject_add_dir - add sub directory of object. + * @parent: object in which a directory is created. + * @name: directory name. + * + * Add a plain directory object as child of given object. + */ +struct kobject *kobject_add_dir(struct kobject *parent, const char *name) +{ + struct kobject *k; + int ret; + + if (!parent) + return NULL; + + k = kzalloc(sizeof(*k), GFP_KERNEL); + if (!k) + return NULL; + + k->parent = parent; + k->ktype = &dir_ktype; + kobject_set_name(k, name); + ret = kobject_register(k); + if (ret < 0) { + printk(KERN_WARNING "kobject_add_dir: " + "kobject_register error: %d\n", ret); + kobject_del(k); + return NULL; + } + + return k; +} + /** * kset_init - initialize a kset for use * @k: kset @@ -380,6 +489,7 @@ void kset_init(struct kset * k) { kobject_init(&k->kobj); INIT_LIST_HEAD(&k->list); + spin_lock_init(&k->list_lock); } @@ -444,7 +554,7 @@ struct kobject * kset_find_obj(struct kset * kset, const char * name) struct list_head * entry; struct kobject * ret = NULL; - down_read(&kset->subsys->rwsem); + spin_lock(&kset->list_lock); list_for_each(entry,&kset->list) { struct kobject * k = to_kobj(entry); if (kobject_name(k) && !strcmp(kobject_name(k),name)) { @@ -452,7 +562,7 @@ struct kobject * kset_find_obj(struct kset * kset, const char * name) break; } } - up_read(&kset->subsys->rwsem); + spin_unlock(&kset->list_lock); return ret; } @@ -515,7 +625,7 @@ int subsys_create_file(struct subsystem * s, struct subsys_attribute * a) * @s: subsystem. * @a: attribute desciptor. */ - +#if 0 void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a) { if (subsys_get(s)) { @@ -523,8 +633,8 @@ void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a) subsys_put(s); } } +#endif /* 0 */ -EXPORT_SYMBOL(kobject_get_path); EXPORT_SYMBOL(kobject_init); EXPORT_SYMBOL(kobject_register); EXPORT_SYMBOL(kobject_unregister); @@ -532,14 +642,10 @@ EXPORT_SYMBOL(kobject_get); EXPORT_SYMBOL(kobject_put); EXPORT_SYMBOL(kobject_add); EXPORT_SYMBOL(kobject_del); -EXPORT_SYMBOL(kobject_rename); EXPORT_SYMBOL(kset_register); EXPORT_SYMBOL(kset_unregister); -EXPORT_SYMBOL(kset_find_obj); -EXPORT_SYMBOL(subsystem_init); EXPORT_SYMBOL(subsystem_register); EXPORT_SYMBOL(subsystem_unregister); EXPORT_SYMBOL(subsys_create_file); -EXPORT_SYMBOL(subsys_remove_file);