vserver 1.9.5.x5
[linux-2.6.git] / lib / kobject.c
index 781f3e8..8737dfd 100644 (file)
@@ -10,8 +10,6 @@
  * about using the kobject interface.
  */
 
-#undef DEBUG
-
 #include <linux/kobject.h>
 #include <linux/string.h>
 #include <linux/module.h>
@@ -58,15 +56,12 @@ static int create_dir(struct kobject * kobj)
        return error;
 }
 
-
 static inline struct kobject * to_kobj(struct list_head * entry)
 {
        return container_of(entry,struct kobject,entry);
 }
 
-
-#ifdef CONFIG_HOTPLUG
-static int get_kobj_path_length(struct kset *kset, struct kobject *kobj)
+static int get_kobj_path_length(struct kobject *kobj)
 {
        int length = 1;
        struct kobject * parent = kobj;
@@ -82,7 +77,7 @@ static int get_kobj_path_length(struct kset *kset, struct kobject *kobj)
        return length;
 }
 
-static void fill_kobj_path(struct kset *kset, struct kobject *kobj, char *path, int length)
+static void fill_kobj_path(struct kobject *kobj, char *path, int length)
 {
        struct kobject * parent;
 
@@ -98,137 +93,35 @@ static void fill_kobj_path(struct kset *kset, struct kobject *kobj, char *path,
        pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
 }
 
-#define BUFFER_SIZE    1024    /* should be enough memory for the env */
-#define NUM_ENVP       32      /* number of env pointers */
-static unsigned long sequence_num;
-static spinlock_t sequence_lock = SPIN_LOCK_UNLOCKED;
-
-static void kset_hotplug(const char *action, struct kset *kset,
-                        struct kobject *kobj)
+/**
+ * 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().
+ *
+ * @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 *argv [3];
-       char **envp = NULL;
-       char *buffer = NULL;
-       char *scratch;
-       int i = 0;
-       int retval;
-       int kobj_path_length;
-       char *kobj_path = NULL;
-       char *name = NULL;
-       unsigned long seq;
-
-       /* If the kset has a filter operation, call it. If it returns
-          failure, no hotplug event is required. */
-       if (kset->hotplug_ops->filter) {
-               if (!kset->hotplug_ops->filter(kset, kobj))
-                       return;
-       }
-
-       pr_debug ("%s\n", __FUNCTION__);
-
-       if (!hotplug_path[0])
-               return;
-
-       envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
-       if (!envp)
-               return;
-       memset (envp, 0x00, NUM_ENVP * sizeof (char *));
-
-       buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
-       if (!buffer)
-               goto exit;
-
-       if (kset->hotplug_ops->name)
-               name = kset->hotplug_ops->name(kset, kobj);
-       if (name == NULL)
-               name = kset->kobj.name;
-
-       argv [0] = hotplug_path;
-       argv [1] = name;
-       argv [2] = NULL;
-
-       /* minimal command environment */
-       envp [i++] = "HOME=/";
-       envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
-
-       scratch = buffer;
-
-       envp [i++] = scratch;
-       scratch += sprintf(scratch, "ACTION=%s", action) + 1;
-
-       spin_lock(&sequence_lock);
-       seq = sequence_num++;
-       spin_unlock(&sequence_lock);
-
-       envp [i++] = scratch;
-       scratch += sprintf(scratch, "SEQNUM=%ld", seq) + 1;
-
-       kobj_path_length = get_kobj_path_length (kset, kobj);
-       kobj_path = kmalloc (kobj_path_length, GFP_KERNEL);
-       if (!kobj_path)
-               goto exit;
-       memset (kobj_path, 0x00, kobj_path_length);
-       fill_kobj_path (kset, kobj, kobj_path, kobj_path_length);
-
-       envp [i++] = scratch;
-       scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
+       char *path;
+       int len;
 
-       if (kset->hotplug_ops->hotplug) {
-               /* have the kset specific function add its stuff */
-               retval = kset->hotplug_ops->hotplug (kset, kobj,
-                                 &envp[i], NUM_ENVP - i, scratch,
-                                 BUFFER_SIZE - (scratch - buffer));
-               if (retval) {
-                       pr_debug ("%s - hotplug() returned %d\n",
-                                 __FUNCTION__, retval);
-                       goto exit;
-               }
-       }
+       len = get_kobj_path_length(kobj);
+       path = kmalloc(len, gfp_mask);
+       if (!path)
+               return NULL;
+       memset(path, 0x00, len);
+       fill_kobj_path(kobj, path, len);
 
-       pr_debug ("%s: %s %s %s %s %s %s %s\n", __FUNCTION__, argv[0], argv[1],
-                 envp[0], envp[1], envp[2], envp[3], envp[4]);
-       retval = call_usermodehelper (argv[0], argv, envp, 0);
-       if (retval)
-               pr_debug ("%s - call_usermodehelper returned %d\n",
-                         __FUNCTION__, retval);
-
-exit:
-       kfree(kobj_path);
-       kfree(buffer);
-       kfree(envp);
-       return;
+       return path;
 }
 
-void kobject_hotplug(const char *action, struct kobject *kobj)
-{
-       struct kobject * top_kobj = kobj;
-
-       /* If this kobj does not belong to a kset,
-          try to find a parent that does. */
-       if (!top_kobj->kset && top_kobj->parent) {
-               do {
-                       top_kobj = top_kobj->parent;
-               } while (!top_kobj->kset && top_kobj->parent);
-       }
-
-       if (top_kobj->kset && top_kobj->kset->hotplug_ops)
-               kset_hotplug(action, top_kobj->kset, kobj);
-}
-#else
-void kobject_hotplug(const char *action, struct kobject *kobj)
-{
-       return;
-}
-#endif /* CONFIG_HOTPLUG */
-
 /**
  *     kobject_init - initialize object.
  *     @kobj:  object in question.
  */
-
 void kobject_init(struct kobject * kobj)
 {
-       atomic_set(&kobj->refcount,1);
+       kref_init(&kobj->kref);
        INIT_LIST_HEAD(&kobj->entry);
        kobj->kset = kset_get(kobj->kset);
 }
@@ -287,11 +180,12 @@ int kobject_add(struct kobject * kobj)
 
        error = create_dir(kobj);
        if (error) {
+               /* unlink does the kobject_put() for us */
                unlink(kobj);
                if (parent)
                        kobject_put(parent);
        } else {
-               kobject_hotplug("add", kobj);
+               kobject_hotplug(kobj, KOBJ_ADD);
        }
 
        return error;
@@ -325,7 +219,7 @@ int kobject_register(struct kobject * kobj)
  *     @kobj:  object.
  *     @name:  name. 
  *
- *     If strlen(name) < KOBJ_NAME_LEN, then use a dynamically allocated
+ *     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.
  */
@@ -338,11 +232,12 @@ int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
        va_list args;
        char * name;
 
-       va_start(args,fmt);
        /* 
         * First, try the static array 
         */
+       va_start(args,fmt);
        need = vsnprintf(kobj->name,limit,fmt,args);
+       va_end(args);
        if (need < limit) 
                name = kobj->name;
        else {
@@ -355,7 +250,9 @@ int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
                        error = -ENOMEM;
                        goto Done;
                }
+               va_start(args,fmt);
                need = vsnprintf(name,limit,fmt,args);
+               va_end(args);
 
                /* Still? Give up. */
                if (need >= limit) {
@@ -372,7 +269,6 @@ int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
        /* Now, set the new name */
        kobj->k_name = name;
  Done:
-       va_end(args);
        return error;
 }
 
@@ -405,7 +301,7 @@ int kobject_rename(struct kobject * kobj, char *new_name)
 
 void kobject_del(struct kobject * kobj)
 {
-       kobject_hotplug("remove", kobj);
+       kobject_hotplug(kobj, KOBJ_REMOVE);
        sysfs_remove_dir(kobj);
        unlink(kobj);
 }
@@ -429,10 +325,8 @@ void kobject_unregister(struct kobject * kobj)
 
 struct kobject * kobject_get(struct kobject * kobj)
 {
-       if (kobj) {
-               WARN_ON(!atomic_read(&kobj->refcount));
-               atomic_inc(&kobj->refcount);
-       }
+       if (kobj)
+               kref_get(&kobj->kref);
        return kobj;
 }
 
@@ -459,17 +353,21 @@ void kobject_cleanup(struct kobject * kobj)
                kobject_put(parent);
 }
 
+static void kobject_release(struct kref *kref)
+{
+       kobject_cleanup(container_of(kref, struct kobject, kref));
+}
+
 /**
  *     kobject_put - decrement refcount for object.
  *     @kobj:  object.
  *
  *     Decrement the refcount, and if 0, call kobject_cleanup().
  */
-
 void kobject_put(struct kobject * kobj)
 {
-       if (atomic_dec_and_test(&kobj->refcount))
-               kobject_cleanup(kobj);
+       if (kobj)
+               kref_put(&kobj->kref, kobject_release);
 }
 
 
@@ -626,7 +524,7 @@ void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a)
        }
 }
 
-
+EXPORT_SYMBOL(kobject_get_path);
 EXPORT_SYMBOL(kobject_init);
 EXPORT_SYMBOL(kobject_register);
 EXPORT_SYMBOL(kobject_unregister);
@@ -635,7 +533,6 @@ EXPORT_SYMBOL(kobject_put);
 EXPORT_SYMBOL(kobject_add);
 EXPORT_SYMBOL(kobject_del);
 EXPORT_SYMBOL(kobject_rename);
-EXPORT_SYMBOL(kobject_hotplug);
 
 EXPORT_SYMBOL(kset_register);
 EXPORT_SYMBOL(kset_unregister);