Merge branch 'for-linus' of git://git.kernel.dk/data/git/linux-2.6-block
[safe/jmp/linux-2.6] / lib / kobject.c
index a181abe..03d4036 100644 (file)
@@ -2,6 +2,8 @@
  * kobject.c - library routines for handling generic kernel objects
  *
  * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
+ * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
+ * Copyright (c) 2006-2007 Novell Inc.
  *
  * This file is released under the GPLv2.
  *
@@ -72,6 +74,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);
@@ -95,11 +99,12 @@ 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, gfp_t gfp_mask)
 {
@@ -107,14 +112,16 @@ char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
        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.
@@ -122,6 +129,8 @@ char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
  */
 void kobject_init(struct kobject * kobj)
 {
+       if (!kobj)
+               return;
        kref_init(&kobj->kref);
        INIT_LIST_HEAD(&kobj->entry);
        kobj->kset = kset_get(kobj->kset);
@@ -161,12 +170,18 @@ int kobject_add(struct kobject * kobj)
        if (!(kobj = kobject_get(kobj)))
                return -ENOENT;
        if (!kobj->k_name)
-               kobj->k_name = kobj->name;
+               kobject_set_name(kobj, "NO_NAME");
+       if (!*kobj->k_name) {
+               pr_debug("kobject attempted to be registered with no name!\n");
+               WARN_ON(1);
+               kobject_put(kobj);
+               return -EINVAL;
+       }
        parent = kobject_get(kobj->parent);
 
        pr_debug("kobject %s: registering. parent: %s, set: %s\n",
                 kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>", 
-                kobj->kset ? kobj->kset->kobj.name : "<NULL>" );
+                kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" );
 
        if (kobj->kset) {
                spin_lock(&kobj->kset->list_lock);
@@ -176,21 +191,30 @@ int kobject_add(struct kobject * kobj)
 
                list_add_tail(&kobj->entry,&kobj->kset->list);
                spin_unlock(&kobj->kset->list_lock);
+               kobj->parent = parent;
        }
-       kobj->parent = parent;
 
        error = create_dir(kobj);
        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(KERN_ERR "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(KERN_ERR "kobject_add failed for %s (%d)\n",
+                              kobject_name(kobj), error);
+               dump_stack();
        }
 
        return error;
 }
 
-
 /**
  *     kobject_register - initialize and add an object.
  *     @kobj:  object in question.
@@ -198,18 +222,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;
 }
 
@@ -226,54 +245,50 @@ int kobject_register(struct kobject * kobj)
 int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
 {
        int error = 0;
-       int limit = KOBJ_NAME_LEN;
+       int limit;
        int need;
        va_list args;
-       char * name;
+       char *name;
 
-       /* 
-        * First, try the static array 
-        */
-       va_start(args,fmt);
-       need = vsnprintf(kobj->name,limit,fmt,args);
+       /* find out how big a buffer we need */
+       name = kmalloc(1024, GFP_KERNEL);
+       if (!name) {
+               error = -ENOMEM;
+               goto done;
+       }
+       va_start(args, fmt);
+       need = vsnprintf(name, 1024, fmt, args);
        va_end(args);
-       if (need < limit) 
-               name = kobj->name;
-       else {
-               /* 
-                * Need more space? Allocate it and try again 
-                */
-               limit = need + 1;
-               name = kmalloc(limit,GFP_KERNEL);
-               if (!name) {
-                       error = -ENOMEM;
-                       goto Done;
-               }
-               va_start(args,fmt);
-               need = vsnprintf(name,limit,fmt,args);
-               va_end(args);
-
-               /* Still? Give up. */
-               if (need >= limit) {
-                       kfree(name);
-                       error = -EFAULT;
-                       goto Done;
-               }
+       kfree(name);
+
+       /* Allocate the new space and copy the string in */
+       limit = need + 1;
+       name = kmalloc(limit, GFP_KERNEL);
+       if (!name) {
+               error = -ENOMEM;
+               goto done;
+       }
+       va_start(args, fmt);
+       need = vsnprintf(name, limit, fmt, args);
+       va_end(args);
+
+       /* something wrong with the string we copied? */
+       if (need >= limit) {
+               kfree(name);
+               error = -EFAULT;
+               goto done;
        }
 
        /* Free the old name, if necessary. */
-       if (kobj->k_name && kobj->k_name != kobj->name)
-               kfree(kobj->k_name);
+       kfree(kobj->k_name);
 
        /* Now, set the new name */
        kobj->k_name = name;
- Done:
+done:
        return error;
 }
-
 EXPORT_SYMBOL(kobject_set_name);
 
-
 /**
  *     kobject_rename - change the name of an object
  *     @kobj:  object in question.
@@ -283,23 +298,109 @@ EXPORT_SYMBOL(kobject_set_name);
 int kobject_rename(struct kobject * kobj, const char *new_name)
 {
        int error = 0;
+       const char *devpath = NULL;
+       char *devpath_string = NULL;
+       char *envp[2];
 
        kobj = kobject_get(kobj);
        if (!kobj)
                return -EINVAL;
+       if (!kobj->parent)
+               return -EINVAL;
+
+       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;
+       /* Note : if we want to send the new name alone, not the full path,
+        * we could probably use kobject_name(kobj); */
+
        error = sysfs_rename_dir(kobj, new_name);
+
+       /* This function is mostly/only used for network interface.
+        * Some hotplug package track interfaces by their name and
+        * therefore want to know when the name is changed by the user. */
+       if (!error)
+               kobject_uevent_env(kobj, KOBJ_MOVE, envp);
+
+out:
+       kfree(devpath_string);
+       kfree(devpath);
        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;
+       new_parent = NULL;
+       kobject_put(old_parent);
+       kobject_uevent_env(kobj, KOBJ_MOVE, envp);
+out:
+       kobject_put(new_parent);
+       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);
 }
@@ -311,8 +412,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);
 }
@@ -339,17 +442,19 @@ void kobject_cleanup(struct kobject * kobj)
        struct kobj_type * t = get_ktype(kobj);
        struct kset * s = kobj->kset;
        struct kobject * parent = kobj->parent;
+       const char *name = kobj->k_name;
 
        pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
-       if (kobj->k_name != kobj->name)
-               kfree(kobj->k_name);
-       kobj->k_name = NULL;
-       if (t && t->release)
+       if (t && t->release) {
                t->release(kobj);
+               /* If we have a release function, we can guess that this was
+                * not a statically allocated kobject, so we should be safe to
+                * free the name */
+               kfree(name);
+       }
        if (s)
                kset_put(s);
-       if (parent) 
-               kobject_put(parent);
+       kobject_put(parent);
 }
 
 static void kobject_release(struct kref *kref)
@@ -370,6 +475,65 @@ 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_kset_add_dir - add sub directory of object.
+ *     @kset:          kset the directory is belongs to.
+ *     @parent:        object in which a directory is created.
+ *     @name:  directory name.
+ *
+ *     Add a plain directory object as child of given object.
+ */
+struct kobject *kobject_kset_add_dir(struct kset *kset,
+                                    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->kset = kset;
+       k->parent = parent;
+       k->ktype = &dir_ktype;
+       kobject_set_name(k, name);
+       ret = kobject_register(k);
+       if (ret < 0) {
+               printk(KERN_WARNING "%s: kobject_register error: %d\n",
+                       __func__, ret);
+               kobject_del(k);
+               return NULL;
+       }
+
+       return k;
+}
+
+/**
+ *     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)
+{
+       return kobject_kset_add_dir(NULL, parent, name);
+}
+
 /**
  *     kset_init - initialize a kset for use
  *     @k:     kset 
@@ -386,22 +550,10 @@ void kset_init(struct kset * k)
 /**
  *     kset_add - add a kset object to the hierarchy.
  *     @k:     kset.
- *
- *     Simply, this adds the kset's embedded kobject to the 
- *     hierarchy. 
- *     We also try to make sure that the kset's embedded kobject
- *     has a parent before it is added. We only care if the embedded
- *     kobject is not part of a kset itself, since kobject_add()
- *     assigns a parent in that case. 
- *     If that is the case, and the kset has a controlling subsystem,
- *     then we set the kset's parent to be said subsystem. 
  */
 
 int kset_add(struct kset * k)
 {
-       if (!k->kobj.parent && !k->kobj.kset && k->subsys)
-               k->kobj.parent = &k->subsys->kset.kobj;
-
        return kobject_add(&k->kobj);
 }
 
@@ -413,8 +565,17 @@ int kset_add(struct kset * k)
 
 int kset_register(struct kset * k)
 {
+       int err;
+
+       if (!k)
+               return -EINVAL;
+
        kset_init(k);
-       return kset_add(k);
+       err = kset_add(k);
+       if (err)
+               return err;
+       kobject_uevent(&k->kobj, KOBJ_ADD);
+       return 0;
 }
 
 
@@ -425,6 +586,8 @@ int kset_register(struct kset * k)
 
 void kset_unregister(struct kset * k)
 {
+       if (!k)
+               return;
        kobject_unregister(&k->kobj);
 }
 
@@ -456,72 +619,34 @@ struct kobject * kset_find_obj(struct kset * kset, const char * name)
        return ret;
 }
 
-
-void subsystem_init(struct subsystem * s)
+int subsystem_register(struct kset *s)
 {
-       init_rwsem(&s->rwsem);
-       kset_init(&s->kset);
-}
-
-/**
- *     subsystem_register - register a subsystem.
- *     @s:     the subsystem we're registering.
- *
- *     Once we register the subsystem, we want to make sure that 
- *     the kset points back to this subsystem for correct usage of 
- *     the rwsem. 
- */
-
-int subsystem_register(struct subsystem * s)
-{
-       int error;
-
-       subsystem_init(s);
-       pr_debug("subsystem %s: registering\n",s->kset.kobj.name);
-
-       if (!(error = kset_add(&s->kset))) {
-               if (!s->kset.subsys)
-                       s->kset.subsys = s;
-       }
-       return error;
+       return kset_register(s);
 }
 
-void subsystem_unregister(struct subsystem * s)
+void subsystem_unregister(struct kset *s)
 {
-       pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name);
-       kset_unregister(&s->kset);
+       kset_unregister(s);
 }
 
-
 /**
  *     subsystem_create_file - export sysfs attribute file.
  *     @s:     subsystem.
  *     @a:     subsystem attribute descriptor.
  */
 
-int subsys_create_file(struct subsystem * s, struct subsys_attribute * a)
+int subsys_create_file(struct kset *s, struct subsys_attribute *a)
 {
        int error = 0;
-       if (subsys_get(s)) {
-               error = sysfs_create_file(&s->kset.kobj,&a->attr);
-               subsys_put(s);
-       }
-       return error;
-}
-
 
-/**
- *     subsystem_remove_file - remove sysfs attribute file.
- *     @s:     subsystem.
- *     @a:     attribute desciptor.
- */
+       if (!s || !a)
+               return -EINVAL;
 
-void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a)
-{
-       if (subsys_get(s)) {
-               sysfs_remove_file(&s->kset.kobj,&a->attr);
-               subsys_put(s);
+       if (kset_get(s)) {
+               error = sysfs_create_file(&s->kobj, &a->attr);
+               kset_put(s);
        }
+       return error;
 }
 
 EXPORT_SYMBOL(kobject_init);
@@ -534,10 +659,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);