cifs: refactor new_inode() calls and inode initialization
[safe/jmp/linux-2.6] / fs / sysfs / dir.c
index d3118d4..82d3b79 100644 (file)
 #include <linux/idr.h>
 #include <linux/completion.h>
 #include <linux/mutex.h>
+#include <linux/slab.h>
 #include "sysfs.h"
 
 DEFINE_MUTEX(sysfs_mutex);
 DEFINE_MUTEX(sysfs_rename_mutex);
-spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
+DEFINE_SPINLOCK(sysfs_assoc_lock);
 
-static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
+static DEFINE_SPINLOCK(sysfs_ino_lock);
 static DEFINE_IDA(sysfs_ino_ida);
 
 /**
@@ -132,7 +133,7 @@ struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
  *     RETURNS:
  *     Pointer to @sd on success, NULL on failure.
  */
-struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
+static struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
 {
        if (unlikely(!sd))
                return NULL;
@@ -161,7 +162,7 @@ struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  *     Put an active reference to @sd.  This function is noop if @sd
  *     is NULL.
  */
-void sysfs_put_active(struct sysfs_dirent *sd)
+static void sysfs_put_active(struct sysfs_dirent *sd)
 {
        struct completion *cmpl;
        int v;
@@ -369,17 +370,17 @@ void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
        memset(acxt, 0, sizeof(*acxt));
        acxt->parent_sd = parent_sd;
 
-       /* Lookup parent inode.  inode initialization and I_NEW
-        * clearing are protected by sysfs_mutex.  By grabbing it and
-        * looking up with _nowait variant, inode state can be
-        * determined reliably.
+       /* Lookup parent inode.  inode initialization is protected by
+        * sysfs_mutex, so inode existence can be determined by
+        * looking up inode while holding sysfs_mutex.
         */
        mutex_lock(&sysfs_mutex);
 
-       inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
-                               parent_sd);
+       inode = ilookup5(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
+                        parent_sd);
+       if (inode) {
+               WARN_ON(inode->i_state & I_NEW);
 
-       if (inode && !(inode->i_state & I_NEW)) {
                /* parent inode available */
                acxt->parent_inode = inode;
 
@@ -392,12 +393,11 @@ void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
                        mutex_lock(&inode->i_mutex);
                        mutex_lock(&sysfs_mutex);
                }
-       } else
-               iput(inode);
+       }
 }
 
 /**
- *     sysfs_add_one - add sysfs_dirent to parent
+ *     __sysfs_add_one - add sysfs_dirent to parent without warning
  *     @acxt: addrm context to use
  *     @sd: sysfs_dirent to be added
  *
@@ -416,14 +416,10 @@ void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  *     0 on success, -EEXIST if entry with the given name already
  *     exists.
  */
-int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
+int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
 {
-       if (sysfs_find_dirent(acxt->parent_sd, sd->s_name)) {
-               printk(KERN_WARNING "sysfs: duplicate filename '%s' "
-                      "can not be created\n", sd->s_name);
-               WARN_ON(1);
+       if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
                return -EEXIST;
-       }
 
        sd->s_parent = sysfs_get(acxt->parent_sd);
 
@@ -438,10 +434,40 @@ int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
 }
 
 /**
- *     sysfs_remove_one - remove sysfs_dirent from parent
+ *     sysfs_add_one - add sysfs_dirent to parent
  *     @acxt: addrm context to use
  *     @sd: sysfs_dirent to be added
  *
+ *     Get @acxt->parent_sd and set sd->s_parent to it and increment
+ *     nlink of parent inode if @sd is a directory and link into the
+ *     children list of the parent.
+ *
+ *     This function should be called between calls to
+ *     sysfs_addrm_start() and sysfs_addrm_finish() and should be
+ *     passed the same @acxt as passed to sysfs_addrm_start().
+ *
+ *     LOCKING:
+ *     Determined by sysfs_addrm_start().
+ *
+ *     RETURNS:
+ *     0 on success, -EEXIST if entry with the given name already
+ *     exists.
+ */
+int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
+{
+       int ret;
+
+       ret = __sysfs_add_one(acxt, sd);
+       WARN(ret == -EEXIST, KERN_WARNING "sysfs: duplicate filename '%s' "
+                      "can not be created\n", sd->s_name);
+       return ret;
+}
+
+/**
+ *     sysfs_remove_one - remove sysfs_dirent from parent
+ *     @acxt: addrm context to use
+ *     @sd: sysfs_dirent to be removed
+ *
  *     Mark @sd removed and drop nlink of parent inode if @sd is a
  *     directory.  @sd is unlinked from the children list.
  *
@@ -609,6 +635,7 @@ struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
 
        return sd;
 }
+EXPORT_SYMBOL_GPL(sysfs_get_dirent);
 
 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
                      const char *name, struct sysfs_dirent **p_sd)
@@ -678,8 +705,10 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
        sd = sysfs_find_dirent(parent_sd, dentry->d_name.name);
 
        /* no such entry */
-       if (!sd)
+       if (!sd) {
+               ret = ERR_PTR(-ENOENT);
                goto out_unlock;
+       }
 
        /* attach dentry and inode */
        inode = sysfs_get_inode(sd);
@@ -781,6 +810,7 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
        old_dentry = sysfs_get_dentry(sd);
        if (IS_ERR(old_dentry)) {
                error = PTR_ERR(old_dentry);
+               old_dentry = NULL;
                goto out;
        }
 
@@ -799,16 +829,12 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
        if (!new_dentry)
                goto out_unlock;
 
-       /* rename kobject and sysfs_dirent */
+       /* rename sysfs_dirent */
        error = -ENOMEM;
        new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
        if (!new_name)
                goto out_unlock;
 
-       error = kobject_set_name(kobj, "%s", new_name);
-       if (error)
-               goto out_unlock;
-
        dup_name = sd->s_name;
        sd->s_name = new_name;
 
@@ -848,6 +874,7 @@ int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
        old_dentry = sysfs_get_dentry(sd);
        if (IS_ERR(old_dentry)) {
                error = PTR_ERR(old_dentry);
+               old_dentry = NULL;
                goto out;
        }
        old_parent = old_dentry->d_parent;
@@ -855,6 +882,7 @@ int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
        new_parent = sysfs_get_dentry(new_parent_sd);
        if (IS_ERR(new_parent)) {
                error = PTR_ERR(new_parent);
+               new_parent = NULL;
                goto out;
        }
 
@@ -878,7 +906,6 @@ again:
        error = 0;
        d_add(new_dentry, NULL);
        d_move(old_dentry, new_dentry);
-       dput(new_dentry);
 
        /* Remove from old parent's list and insert into new parent's list. */
        sysfs_unlink_sibling(sd);
@@ -956,4 +983,5 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
 const struct file_operations sysfs_dir_operations = {
        .read           = generic_read_dir,
        .readdir        = sysfs_readdir,
+       .llseek         = generic_file_llseek,
 };