]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/autofs4/root.c
autofs4: cleanup redundant readir code
[linux-2.6-omap-h63xx.git] / fs / autofs4 / root.c
index 9ead2279df4fad5b70306074ab50a2e7d66b8874..51c873ca8e8d8e51a7bfa445e2251c3ceb5c6c4a 100644 (file)
@@ -25,12 +25,13 @@ static int autofs4_dir_rmdir(struct inode *,struct dentry *);
 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
 static int autofs4_dir_open(struct inode *inode, struct file *file);
-static int autofs4_dir_close(struct inode *inode, struct file *file);
-static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
 static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
 static void *autofs4_follow_link(struct dentry *, struct nameidata *);
 
+#define TRIGGER_FLAGS   (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
+#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
+
 const struct file_operations autofs4_root_operations = {
        .open           = dcache_dir_open,
        .release        = dcache_dir_close,
@@ -41,9 +42,9 @@ const struct file_operations autofs4_root_operations = {
 
 const struct file_operations autofs4_dir_operations = {
        .open           = autofs4_dir_open,
-       .release        = autofs4_dir_close,
+       .release        = dcache_dir_close,
        .read           = generic_read_dir,
-       .readdir        = autofs4_dir_readdir,
+       .readdir        = dcache_readdir,
 };
 
 const struct inode_operations autofs4_indirect_root_inode_operations = {
@@ -95,17 +96,7 @@ static int autofs4_root_readdir(struct file *file, void *dirent,
 static int autofs4_dir_open(struct inode *inode, struct file *file)
 {
        struct dentry *dentry = file->f_path.dentry;
-       struct vfsmount *mnt = file->f_path.mnt;
        struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
-       struct dentry *cursor;
-       int status;
-
-       status = dcache_dir_open(inode, file);
-       if (status)
-               goto out;
-
-       cursor = file->private_data;
-       cursor->d_fsdata = NULL;
 
        DPRINTK("file=%p dentry=%p %.*s",
                file, dentry, dentry->d_name.len, dentry->d_name.name);
@@ -113,136 +104,30 @@ static int autofs4_dir_open(struct inode *inode, struct file *file)
        if (autofs4_oz_mode(sbi))
                goto out;
 
-       if (autofs4_ispending(dentry)) {
-               DPRINTK("dentry busy");
-               dcache_dir_close(inode, file);
-               status = -EBUSY;
-               goto out;
-       }
-
-       status = -ENOENT;
-       if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
-               struct nameidata nd;
-               int empty, ret;
-
-               /* In case there are stale directory dentrys from a failed mount */
-               spin_lock(&dcache_lock);
-               empty = list_empty(&dentry->d_subdirs);
+       /*
+        * An empty directory in an autofs file system is always a
+        * mount point. The daemon must have failed to mount this
+        * during lookup so it doesn't exist. This can happen, for
+        * example, if user space returns an incorrect status for a
+        * mount request. Otherwise we're doing a readdir on the
+        * autofs file system so just let the libfs routines handle
+        * it.
+        */
+       spin_lock(&dcache_lock);
+       if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
                spin_unlock(&dcache_lock);
-
-               if (!empty)
-                       d_invalidate(dentry);
-
-               nd.flags = LOOKUP_DIRECTORY;
-               ret = (dentry->d_op->d_revalidate)(dentry, &nd);
-
-               if (ret <= 0) {
-                       if (ret < 0)
-                               status = ret;
-                       dcache_dir_close(inode, file);
-                       goto out;
-               }
-       }
-
-       if (d_mountpoint(dentry)) {
-               struct file *fp = NULL;
-               struct path fp_path = { .dentry = dentry, .mnt = mnt };
-
-               path_get(&fp_path);
-
-               if (!autofs4_follow_mount(&fp_path.mnt, &fp_path.dentry)) {
-                       path_put(&fp_path);
-                       dcache_dir_close(inode, file);
-                       goto out;
-               }
-
-               fp = dentry_open(fp_path.dentry, fp_path.mnt, file->f_flags);
-               status = PTR_ERR(fp);
-               if (IS_ERR(fp)) {
-                       dcache_dir_close(inode, file);
-                       goto out;
-               }
-               cursor->d_fsdata = fp;
-       }
-       return 0;
-out:
-       return status;
-}
-
-static int autofs4_dir_close(struct inode *inode, struct file *file)
-{
-       struct dentry *dentry = file->f_path.dentry;
-       struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
-       struct dentry *cursor = file->private_data;
-       int status = 0;
-
-       DPRINTK("file=%p dentry=%p %.*s",
-               file, dentry, dentry->d_name.len, dentry->d_name.name);
-
-       if (autofs4_oz_mode(sbi))
-               goto out;
-
-       if (autofs4_ispending(dentry)) {
-               DPRINTK("dentry busy");
-               status = -EBUSY;
-               goto out;
-       }
-
-       if (d_mountpoint(dentry)) {
-               struct file *fp = cursor->d_fsdata;
-               if (!fp) {
-                       status = -ENOENT;
-                       goto out;
-               }
-               filp_close(fp, current->files);
-       }
-out:
-       dcache_dir_close(inode, file);
-       return status;
-}
-
-static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
-{
-       struct dentry *dentry = file->f_path.dentry;
-       struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
-       struct dentry *cursor = file->private_data;
-       int status;
-
-       DPRINTK("file=%p dentry=%p %.*s",
-               file, dentry, dentry->d_name.len, dentry->d_name.name);
-
-       if (autofs4_oz_mode(sbi))
-               goto out;
-
-       if (autofs4_ispending(dentry)) {
-               DPRINTK("dentry busy");
-               return -EBUSY;
+               return -ENOENT;
        }
+       spin_unlock(&dcache_lock);
 
-       if (d_mountpoint(dentry)) {
-               struct file *fp = cursor->d_fsdata;
-
-               if (!fp)
-                       return -ENOENT;
-
-               if (!fp->f_op || !fp->f_op->readdir)
-                       goto out;
-
-               status = vfs_readdir(fp, filldir, dirent);
-               file->f_pos = fp->f_pos;
-               if (status)
-                       autofs4_copy_atime(file, fp);
-               return status;
-       }
 out:
-       return dcache_readdir(file, dirent, filldir);
+       return dcache_dir_open(inode, file);
 }
 
 static int try_to_fill_dentry(struct dentry *dentry, int flags)
 {
        struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
        struct autofs_info *ino = autofs4_dentry_ino(dentry);
-       struct dentry *new;
        int status;
 
        /* Block on any pending expiry here; invalidate the dentry
@@ -292,7 +177,7 @@ static int try_to_fill_dentry(struct dentry *dentry, int flags)
                        return status;
                }
        /* Trigger mount for path component or follow link */
-       } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
+       } else if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) ||
                        current->link_count) {
                DPRINTK("waiting for mount name=%.*s",
                        dentry->d_name.len, dentry->d_name.name);
@@ -320,26 +205,6 @@ static int try_to_fill_dentry(struct dentry *dentry, int flags)
        dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
        spin_unlock(&dentry->d_lock);
 
-       /*
-        * The dentry that is passed in from lookup may not be the one
-        * we end up using, as mkdir can create a new one.  If this
-        * happens, and another process tries the lookup at the same time,
-        * it will set the PENDING flag on this new dentry, but add itself
-        * to our waitq.  Then, if after the lookup succeeds, the first
-        * process that requested the mount performs another lookup of the
-        * same directory, it will show up as still pending!  So, we need
-        * to redo the lookup here and clear pending on that dentry.
-        */
-       if (d_unhashed(dentry)) {
-               new = d_lookup(dentry->d_parent, &dentry->d_name);
-               if (new) {
-                       spin_lock(&new->d_lock);
-                       new->d_flags &= ~DCACHE_AUTOFS_PENDING;
-                       spin_unlock(&new->d_lock);
-                       dput(new);
-               }
-       }
-
        return 0;
 }
 
@@ -357,7 +222,7 @@ static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
                nd->flags);
 
        /* If it's our master or we shouldn't trigger a mount we're done */
-       lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
+       lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS);
        if (oz_mode || !lookup_type)
                goto done;
 
@@ -494,6 +359,8 @@ void autofs4_dentry_release(struct dentry *de)
 
                if (sbi) {
                        spin_lock(&sbi->lookup_lock);
+                       if (!list_empty(&inf->active))
+                               list_del(&inf->active);
                        if (!list_empty(&inf->expiring))
                                list_del(&inf->expiring);
                        spin_unlock(&sbi->lookup_lock);
@@ -518,6 +385,58 @@ static struct dentry_operations autofs4_dentry_operations = {
        .d_release      = autofs4_dentry_release,
 };
 
+static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
+{
+       unsigned int len = name->len;
+       unsigned int hash = name->hash;
+       const unsigned char *str = name->name;
+       struct list_head *p, *head;
+
+       spin_lock(&dcache_lock);
+       spin_lock(&sbi->lookup_lock);
+       head = &sbi->active_list;
+       list_for_each(p, head) {
+               struct autofs_info *ino;
+               struct dentry *dentry;
+               struct qstr *qstr;
+
+               ino = list_entry(p, struct autofs_info, active);
+               dentry = ino->dentry;
+
+               spin_lock(&dentry->d_lock);
+
+               /* Already gone? */
+               if (atomic_read(&dentry->d_count) == 0)
+                       goto next;
+
+               qstr = &dentry->d_name;
+
+               if (dentry->d_name.hash != hash)
+                       goto next;
+               if (dentry->d_parent != parent)
+                       goto next;
+
+               if (qstr->len != len)
+                       goto next;
+               if (memcmp(qstr->name, str, len))
+                       goto next;
+
+               if (d_unhashed(dentry)) {
+                       dget(dentry);
+                       spin_unlock(&dentry->d_lock);
+                       spin_unlock(&sbi->lookup_lock);
+                       spin_unlock(&dcache_lock);
+                       return dentry;
+               }
+next:
+               spin_unlock(&dentry->d_lock);
+       }
+       spin_unlock(&sbi->lookup_lock);
+       spin_unlock(&dcache_lock);
+
+       return NULL;
+}
+
 static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
 {
        unsigned int len = name->len;
@@ -574,7 +493,8 @@ next:
 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
 {
        struct autofs_sb_info *sbi;
-       struct dentry *expiring;
+       struct autofs_info *ino;
+       struct dentry *expiring, *unhashed;
        int oz_mode;
 
        DPRINTK("name = %.*s",
@@ -592,12 +512,12 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s
 
        expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
        if (expiring) {
-               struct autofs_info *ino = autofs4_dentry_ino(expiring);
                /*
                 * If we are racing with expire the request might not
                 * be quite complete but the directory has been removed
                 * so it must have been successful, so just wait for it.
                 */
+               ino = autofs4_dentry_ino(expiring);
                while (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
                        DPRINTK("wait for incomplete expire %p name=%.*s",
                                expiring, expiring->d_name.len,
@@ -612,32 +532,51 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s
                dput(expiring);
        }
 
-       /*
-        * Mark the dentry incomplete but don't hash it. We do this
-        * to serialize our inode creation operations (symlink and
-        * mkdir) which prevents deadlock during the callback to
-        * the daemon. Subsequent user space lookups for the same
-        * dentry are placed on the wait queue while the daemon
-        * itself is allowed passage unresticted so the create
-        * operation itself can then hash the dentry. Finally,
-        * we check for the hashed dentry and return the newly
-        * hashed dentry.
-        */
-       dentry->d_op = &autofs4_root_dentry_operations;
+       unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
+       if (unhashed)
+               dentry = unhashed;
+       else {
+               /*
+                * Mark the dentry incomplete but don't hash it. We do this
+                * to serialize our inode creation operations (symlink and
+                * mkdir) which prevents deadlock during the callback to
+                * the daemon. Subsequent user space lookups for the same
+                * dentry are placed on the wait queue while the daemon
+                * itself is allowed passage unresticted so the create
+                * operation itself can then hash the dentry. Finally,
+                * we check for the hashed dentry and return the newly
+                * hashed dentry.
+                */
+               dentry->d_op = &autofs4_root_dentry_operations;
+
+               /*
+                * And we need to ensure that the same dentry is used for
+                * all following lookup calls until it is hashed so that
+                * the dentry flags are persistent throughout the request.
+                */
+               ino = autofs4_init_ino(NULL, sbi, 0555);
+               if (!ino)
+                       return ERR_PTR(-ENOMEM);
+
+               dentry->d_fsdata = ino;
+               ino->dentry = dentry;
 
-       dentry->d_fsdata = NULL;
-       d_instantiate(dentry, NULL);
+               spin_lock(&sbi->lookup_lock);
+               list_add(&ino->active, &sbi->active_list);
+               spin_unlock(&sbi->lookup_lock);
+
+               d_instantiate(dentry, NULL);
+       }
 
        if (!oz_mode) {
                spin_lock(&dentry->d_lock);
                dentry->d_flags |= DCACHE_AUTOFS_PENDING;
                spin_unlock(&dentry->d_lock);
-       }
-
-       if (dentry->d_op && dentry->d_op->d_revalidate) {
-               mutex_unlock(&dir->i_mutex);
-               (dentry->d_op->d_revalidate)(dentry, nd);
-               mutex_lock(&dir->i_mutex);
+               if (dentry->d_op && dentry->d_op->d_revalidate) {
+                       mutex_unlock(&dir->i_mutex);
+                       (dentry->d_op->d_revalidate)(dentry, nd);
+                       mutex_lock(&dir->i_mutex);
+               }
        }
 
        /*
@@ -651,12 +590,16 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s
                        if (sigismember (sigset, SIGKILL) ||
                            sigismember (sigset, SIGQUIT) ||
                            sigismember (sigset, SIGINT)) {
+                           if (unhashed)
+                               dput(unhashed);
                            return ERR_PTR(-ERESTARTNOINTR);
                        }
                }
-               spin_lock(&dentry->d_lock);
-               dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
-               spin_unlock(&dentry->d_lock);
+               if (!oz_mode) {
+                       spin_lock(&dentry->d_lock);
+                       dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
+                       spin_unlock(&dentry->d_lock);
+               }
        }
 
        /*
@@ -680,9 +623,15 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s
                else
                        dentry = ERR_PTR(-ENOENT);
 
+               if (unhashed)
+                       dput(unhashed);
+
                return dentry;
        }
 
+       if (unhashed)
+               return unhashed;
+
        return NULL;
 }
 
@@ -703,20 +652,31 @@ static int autofs4_dir_symlink(struct inode *dir,
                return -EACCES;
 
        ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
-       if (ino == NULL)
-               return -ENOSPC;
+       if (!ino)
+               return -ENOMEM;
 
-       ino->size = strlen(symname);
-       ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
+       spin_lock(&sbi->lookup_lock);
+       if (!list_empty(&ino->active))
+               list_del_init(&ino->active);
+       spin_unlock(&sbi->lookup_lock);
 
-       if (cp == NULL) {
-               kfree(ino);
-               return -ENOSPC;
+       ino->size = strlen(symname);
+       cp = kmalloc(ino->size + 1, GFP_KERNEL);
+       if (!cp) {
+               if (!dentry->d_fsdata)
+                       kfree(ino);
+               return -ENOMEM;
        }
 
        strcpy(cp, symname);
 
        inode = autofs4_get_inode(dir->i_sb, ino);
+       if (!inode) {
+               kfree(cp);
+               if (!dentry->d_fsdata)
+                       kfree(ino);
+               return -ENOMEM;
+       }
        d_add(dentry, inode);
 
        if (dir == dir->i_sb->s_root->d_inode)
@@ -732,6 +692,7 @@ static int autofs4_dir_symlink(struct inode *dir,
                atomic_inc(&p_ino->count);
        ino->inode = inode;
 
+       ino->u.symlink = cp;
        dir->i_mtime = CURRENT_TIME;
 
        return 0;
@@ -776,7 +737,8 @@ static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
 
        spin_lock(&dcache_lock);
        spin_lock(&sbi->lookup_lock);
-       list_add(&ino->expiring, &sbi->expiring_list);
+       if (list_empty(&ino->expiring))
+               list_add(&ino->expiring, &sbi->expiring_list);
        spin_unlock(&sbi->lookup_lock);
        spin_lock(&dentry->d_lock);
        __d_drop(dentry);
@@ -804,7 +766,8 @@ static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
                return -ENOTEMPTY;
        }
        spin_lock(&sbi->lookup_lock);
-       list_add(&ino->expiring, &sbi->expiring_list);
+       if (list_empty(&ino->expiring))
+               list_add(&ino->expiring, &sbi->expiring_list);
        spin_unlock(&sbi->lookup_lock);
        spin_lock(&dentry->d_lock);
        __d_drop(dentry);
@@ -840,10 +803,20 @@ static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
                dentry, dentry->d_name.len, dentry->d_name.name);
 
        ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
-       if (ino == NULL)
-               return -ENOSPC;
+       if (!ino)
+               return -ENOMEM;
+
+       spin_lock(&sbi->lookup_lock);
+       if (!list_empty(&ino->active))
+               list_del_init(&ino->active);
+       spin_unlock(&sbi->lookup_lock);
 
        inode = autofs4_get_inode(dir->i_sb, ino);
+       if (!inode) {
+               if (!dentry->d_fsdata)
+                       kfree(ino);
+               return -ENOMEM;
+       }
        d_add(dentry, inode);
 
        if (dir == dir->i_sb->s_root->d_inode)