S2IO: Optimized the delay to wait for command completion
[safe/jmp/linux-2.6] / lib / kobject.c
index 9404882..f4f6176 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/string.h>
 #include <linux/module.h>
 #include <linux/stat.h>
+#include <linux/slab.h>
 
 /**
  *     populate_dir - populate directory with attributes.
@@ -43,11 +44,11 @@ static int populate_dir(struct kobject * kobj)
        return error;
 }
 
-static int create_dir(struct kobject * kobj)
+static int create_dir(struct kobject * kobj, struct dentry *shadow_parent)
 {
        int error = 0;
        if (kobject_name(kobj)) {
-               error = sysfs_create_dir(kobj);
+               error = sysfs_create_dir(kobj, shadow_parent);
                if (!error) {
                        if ((error = populate_dir(kobj)))
                                sysfs_remove_dir(kobj);
@@ -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);
@@ -94,26 +97,29 @@ static void fill_kobj_path(struct kobject *kobj, char *path, int length)
 }
 
 /**
- * kobject_get_path - generate and return the path associated with a given kobj
- * and kset pair.  The result must be freed by the caller with kfree().
+ * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
  *
  * @kobj:      kobject in question, with which to build the path
  * @gfp_mask:  the allocation type used to allocate the path
+ *
+ * The result must be freed by the caller with kfree().
  */
-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.
@@ -121,8 +127,11 @@ char *kobject_get_path(struct kobject *kobj, int gfp_mask)
  */
 void kobject_init(struct kobject * kobj)
 {
+       if (!kobj)
+               return;
        kref_init(&kobj->kref);
        INIT_LIST_HEAD(&kobj->entry);
+       init_waitqueue_head(&kobj->poll);
        kobj->kset = kset_get(kobj->kset);
 }
 
@@ -150,9 +159,10 @@ static void unlink(struct kobject * kobj)
 /**
  *     kobject_add - add an object to the hierarchy.
  *     @kobj:  object.
+ *     @shadow_parent: sysfs directory to add to.
  */
 
-int kobject_add(struct kobject * kobj)
+int kobject_shadow_add(struct kobject * kobj, struct dentry *shadow_parent)
 {
        int error = 0;
        struct kobject * parent;
@@ -161,6 +171,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",
@@ -178,17 +193,36 @@ int kobject_add(struct kobject * kobj)
        }
        kobj->parent = parent;
 
-       error = create_dir(kobj);
+       error = create_dir(kobj, shadow_parent);
        if (error) {
                /* unlink does the kobject_put() for us */
                unlink(kobj);
-               if (parent)
-                       kobject_put(parent);
+               kobject_put(parent);
+
+               /* 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;
 }
 
+/**
+ *     kobject_add - add an object to the hierarchy.
+ *     @kobj:  object.
+ */
+int kobject_add(struct kobject * kobj)
+{
+       return kobject_shadow_add(kobj, NULL);
+}
+
 
 /**
  *     kobject_register - initialize and add an object.
@@ -197,18 +231,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
-                       kobject_hotplug(kobj, KOBJ_ADD);
-       } else
-               error = -EINVAL;
+               if (!error)
+                       kobject_uevent(kobj, KOBJ_ADD);
+       }
        return error;
 }
 
@@ -279,26 +308,101 @@ 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;
 
        kobj = kobject_get(kobj);
        if (!kobj)
                return -EINVAL;
-       error = sysfs_rename_dir(kobj, new_name);
+       if (!kobj->parent)
+               return -EINVAL;
+       error = sysfs_rename_dir(kobj, kobj->parent->dentry, new_name);
        kobject_put(kobj);
 
        return error;
 }
 
 /**
+ *     kobject_rename - change the name of an object
+ *     @kobj:  object in question.
+ *     @new_parent: object's new parent
+ *     @new_name: object's new name
+ */
+
+int kobject_shadow_rename(struct kobject * kobj, struct dentry *new_parent,
+                         const char *new_name)
+{
+       int error = 0;
+
+       kobj = kobject_get(kobj);
+       if (!kobj)
+               return -EINVAL;
+       error = sysfs_rename_dir(kobj, new_parent, new_name);
+       kobject_put(kobj);
+
+       return error;
+}
+
+/**
+ *     kobject_move - move object to another parent
+ *     @kobj:  object in question.
+ *     @new_parent: object's new parent (can be NULL)
+ */
+
+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) {
+               if (kobj->kset)
+                       new_parent = kobject_get(&kobj->kset->kobj);
+       }
+       /* 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.
  */
 
 void kobject_del(struct kobject * kobj)
 {
+       if (!kobj)
+               return;
        sysfs_remove_dir(kobj);
        unlink(kobj);
 }
@@ -310,8 +414,10 @@ void kobject_del(struct kobject * kobj)
 
 void kobject_unregister(struct kobject * kobj)
 {
+       if (!kobj)
+               return;
        pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
-       kobject_hotplug(kobj, KOBJ_REMOVE);
+       kobject_uevent(kobj, KOBJ_REMOVE);
        kobject_del(kobj);
        kobject_put(kobj);
 }
@@ -347,8 +453,7 @@ void kobject_cleanup(struct kobject * kobj)
                t->release(kobj);
        if (s)
                kset_put(s);
-       if (parent) 
-               kobject_put(parent);
+       kobject_put(parent);
 }
 
 static void kobject_release(struct kref *kref)
@@ -369,6 +474,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 
@@ -412,6 +561,8 @@ int kset_add(struct kset * k)
 
 int kset_register(struct kset * k)
 {
+       if (!k)
+               return -EINVAL;
        kset_init(k);
        return kset_add(k);
 }
@@ -424,6 +575,8 @@ int kset_register(struct kset * k)
 
 void kset_unregister(struct kset * k)
 {
+       if (!k)
+               return;
        kobject_unregister(&k->kobj);
 }
 
@@ -475,6 +628,9 @@ int subsystem_register(struct subsystem * s)
 {
        int error;
 
+       if (!s)
+               return -EINVAL;
+
        subsystem_init(s);
        pr_debug("subsystem %s: registering\n",s->kset.kobj.name);
 
@@ -487,6 +643,8 @@ int subsystem_register(struct subsystem * s)
 
 void subsystem_unregister(struct subsystem * s)
 {
+       if (!s)
+               return;
        pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name);
        kset_unregister(&s->kset);
 }
@@ -501,6 +659,10 @@ void subsystem_unregister(struct subsystem * s)
 int subsys_create_file(struct subsystem * s, struct subsys_attribute * a)
 {
        int error = 0;
+
+       if (!s || !a)
+               return -EINVAL;
+
        if (subsys_get(s)) {
                error = sysfs_create_file(&s->kset.kobj,&a->attr);
                subsys_put(s);
@@ -514,7 +676,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)) {
@@ -522,6 +684,7 @@ void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a)
                subsys_put(s);
        }
 }
+#endif  /*  0  */
 
 EXPORT_SYMBOL(kobject_init);
 EXPORT_SYMBOL(kobject_register);
@@ -533,10 +696,7 @@ EXPORT_SYMBOL(kobject_del);
 
 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);