autofs4: mkdir and symlink always get a dentry that had passed lookup
[firefly-linux-kernel-4.4.55.git] / fs / autofs4 / root.c
index f1076b91a0faea7428c8e07b2ee9ee242fa291db..a5b93e8f49b558a533172aa3088d915a4299d693 100644 (file)
@@ -35,9 +35,8 @@ static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
 #endif
 static int autofs4_dir_open(struct inode *inode, struct file *file);
 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
-
-#define TRIGGER_FLAGS   (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
-#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
+static struct vfsmount *autofs4_d_automount(struct path *);
+static int autofs4_d_manage(struct dentry *, bool, bool);
 
 const struct file_operations autofs4_root_operations = {
        .open           = dcache_dir_open,
@@ -59,7 +58,7 @@ const struct file_operations autofs4_dir_operations = {
        .llseek         = dcache_dir_lseek,
 };
 
-const struct inode_operations autofs4_indirect_root_inode_operations = {
+const struct inode_operations autofs4_dir_inode_operations = {
        .lookup         = autofs4_lookup,
        .unlink         = autofs4_dir_unlink,
        .symlink        = autofs4_dir_symlink,
@@ -67,19 +66,10 @@ const struct inode_operations autofs4_indirect_root_inode_operations = {
        .rmdir          = autofs4_dir_rmdir,
 };
 
-const struct inode_operations autofs4_direct_root_inode_operations = {
-       .lookup         = autofs4_lookup,
-       .unlink         = autofs4_dir_unlink,
-       .mkdir          = autofs4_dir_mkdir,
-       .rmdir          = autofs4_dir_rmdir,
-};
-
-const struct inode_operations autofs4_dir_inode_operations = {
-       .lookup         = autofs4_lookup,
-       .unlink         = autofs4_dir_unlink,
-       .symlink        = autofs4_dir_symlink,
-       .mkdir          = autofs4_dir_mkdir,
-       .rmdir          = autofs4_dir_rmdir,
+const struct dentry_operations autofs4_dentry_operations = {
+       .d_automount    = autofs4_d_automount,
+       .d_manage       = autofs4_d_manage,
+       .d_release      = autofs4_dentry_release,
 };
 
 static void autofs4_add_active(struct dentry *dentry)
@@ -114,14 +104,6 @@ static void autofs4_del_active(struct dentry *dentry)
        return;
 }
 
-static unsigned int autofs4_need_mount(unsigned int flags)
-{
-       unsigned int res = 0;
-       if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
-               res = 1;
-       return res;
-}
-
 static int autofs4_dir_open(struct inode *inode, struct file *file)
 {
        struct dentry *dentry = file->f_path.dentry;
@@ -156,238 +138,6 @@ out:
        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);
-       int status;
-
-       DPRINTK("dentry=%p %.*s ino=%p",
-                dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
-
-       /*
-        * Wait for a pending mount, triggering one if there
-        * isn't one already
-        */
-       if (dentry->d_inode == NULL) {
-               DPRINTK("waiting for mount name=%.*s",
-                        dentry->d_name.len, dentry->d_name.name);
-
-               status = autofs4_wait(sbi, dentry, NFY_MOUNT);
-
-               DPRINTK("mount done status=%d", status);
-
-               /* Turn this into a real negative dentry? */
-               if (status == -ENOENT) {
-                       spin_lock(&sbi->fs_lock);
-                       ino->flags &= ~AUTOFS_INF_PENDING;
-                       spin_unlock(&sbi->fs_lock);
-                       return status;
-               } else if (status) {
-                       /* Return a negative dentry, but leave it "pending" */
-                       return status;
-               }
-       /* Trigger mount for path component or follow link */
-       } else if (ino->flags & AUTOFS_INF_PENDING ||
-                       autofs4_need_mount(flags)) {
-               DPRINTK("waiting for mount name=%.*s",
-                       dentry->d_name.len, dentry->d_name.name);
-
-               spin_lock(&sbi->fs_lock);
-               ino->flags |= AUTOFS_INF_PENDING;
-               spin_unlock(&sbi->fs_lock);
-               status = autofs4_wait(sbi, dentry, NFY_MOUNT);
-
-               DPRINTK("mount done status=%d", status);
-
-               if (status) {
-                       spin_lock(&sbi->fs_lock);
-                       ino->flags &= ~AUTOFS_INF_PENDING;
-                       spin_unlock(&sbi->fs_lock);
-                       return status;
-               }
-       }
-
-       /* Initialize expiry counter after successful mount */
-       ino->last_used = jiffies;
-
-       spin_lock(&sbi->fs_lock);
-       ino->flags &= ~AUTOFS_INF_PENDING;
-       spin_unlock(&sbi->fs_lock);
-
-       return 0;
-}
-
-/* For autofs direct mounts the follow link triggers the mount */
-static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
-{
-       struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
-       struct autofs_info *ino = autofs4_dentry_ino(dentry);
-       int oz_mode = autofs4_oz_mode(sbi);
-       unsigned int lookup_type;
-       int status;
-
-       DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
-               dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
-               nd->flags);
-       /*
-        * For an expire of a covered direct or offset mount we need
-        * to break out of follow_down_one() at the autofs mount trigger
-        * (d_mounted--), so we can see the expiring flag, and manage
-        * the blocking and following here until the expire is completed.
-        */
-       if (oz_mode) {
-               spin_lock(&sbi->fs_lock);
-               if (ino->flags & AUTOFS_INF_EXPIRING) {
-                       spin_unlock(&sbi->fs_lock);
-                       /* Follow down to our covering mount. */
-                       if (!follow_down_one(&nd->path))
-                               goto done;
-                       goto follow;
-               }
-               spin_unlock(&sbi->fs_lock);
-               goto done;
-       }
-
-       /* If an expire request is pending everyone must wait. */
-       autofs4_expire_wait(dentry);
-
-       /* We trigger a mount for almost all flags */
-       lookup_type = autofs4_need_mount(nd->flags);
-       spin_lock(&sbi->fs_lock);
-       spin_lock(&autofs4_lock);
-       spin_lock(&dentry->d_lock);
-       if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
-               spin_unlock(&dentry->d_lock);
-               spin_unlock(&autofs4_lock);
-               spin_unlock(&sbi->fs_lock);
-               goto follow;
-       }
-
-       /*
-        * If the dentry contains directories then it is an autofs
-        * multi-mount with no root mount offset. So don't try to
-        * mount it again.
-        */
-       if (ino->flags & AUTOFS_INF_PENDING ||
-           (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
-               spin_unlock(&dentry->d_lock);
-               spin_unlock(&autofs4_lock);
-               spin_unlock(&sbi->fs_lock);
-
-               status = try_to_fill_dentry(dentry, nd->flags);
-               if (status)
-                       goto out_error;
-
-               goto follow;
-       }
-       spin_unlock(&dentry->d_lock);
-       spin_unlock(&autofs4_lock);
-       spin_unlock(&sbi->fs_lock);
-follow:
-       /*
-        * If there is no root mount it must be an autofs
-        * multi-mount with no root offset so we don't need
-        * to follow it.
-        */
-       if (d_managed(dentry)) {
-               status = follow_down(&nd->path, false);
-               if (status < 0)
-                       goto out_error;
-       }
-
-done:
-       return NULL;
-
-out_error:
-       path_put(&nd->path);
-       return ERR_PTR(status);
-}
-
-/*
- * Revalidate is called on every cache lookup.  Some of those
- * cache lookups may actually happen while the dentry is not
- * yet completely filled in, and revalidate has to delay such
- * lookups..
- */
-static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
-{
-       struct inode *dir;
-       struct autofs_sb_info *sbi;
-       int oz_mode;
-       int flags = nd ? nd->flags : 0;
-       int status = 1;
-
-       if (flags & LOOKUP_RCU)
-               return -ECHILD;
-
-       dir = dentry->d_parent->d_inode;
-       sbi = autofs4_sbi(dir->i_sb);
-       oz_mode = autofs4_oz_mode(sbi);
-
-       /* Pending dentry */
-       spin_lock(&sbi->fs_lock);
-       if (autofs4_ispending(dentry)) {
-               /* The daemon never causes a mount to trigger */
-               spin_unlock(&sbi->fs_lock);
-
-               if (oz_mode)
-                       return 1;
-
-               /*
-                * If the directory has gone away due to an expire
-                * we have been called as ->d_revalidate() and so
-                * we need to return false and proceed to ->lookup().
-                */
-               if (autofs4_expire_wait(dentry) == -EAGAIN)
-                       return 0;
-
-               /*
-                * A zero status is success otherwise we have a
-                * negative error code.
-                */
-               status = try_to_fill_dentry(dentry, flags);
-               if (status == 0)
-                       return 1;
-
-               return status;
-       }
-       spin_unlock(&sbi->fs_lock);
-
-       /* Negative dentry.. invalidate if "old" */
-       if (dentry->d_inode == NULL)
-               return 0;
-
-       /* Check for a non-mountpoint directory with no contents */
-       spin_lock(&autofs4_lock);
-       spin_lock(&dentry->d_lock);
-       if (S_ISDIR(dentry->d_inode->i_mode) &&
-           !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
-               DPRINTK("dentry=%p %.*s, emptydir",
-                        dentry, dentry->d_name.len, dentry->d_name.name);
-               spin_unlock(&dentry->d_lock);
-               spin_unlock(&autofs4_lock);
-
-               /* The daemon never causes a mount to trigger */
-               if (oz_mode)
-                       return 1;
-
-               /*
-                * A zero status is success otherwise we have a
-                * negative error code.
-                */
-               status = try_to_fill_dentry(dentry, flags);
-               if (status == 0)
-                       return 1;
-
-               return status;
-       }
-       spin_unlock(&dentry->d_lock);
-       spin_unlock(&autofs4_lock);
-
-       return 1;
-}
-
 void autofs4_dentry_release(struct dentry *de)
 {
        struct autofs_info *inf;
@@ -395,11 +145,8 @@ void autofs4_dentry_release(struct dentry *de)
        DPRINTK("releasing %p", de);
 
        inf = autofs4_dentry_ino(de);
-       de->d_fsdata = NULL;
-
        if (inf) {
                struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
-
                if (sbi) {
                        spin_lock(&sbi->lookup_lock);
                        if (!list_empty(&inf->active))
@@ -408,26 +155,10 @@ void autofs4_dentry_release(struct dentry *de)
                                list_del(&inf->expiring);
                        spin_unlock(&sbi->lookup_lock);
                }
-
-               inf->dentry = NULL;
-               inf->inode = NULL;
-
                autofs4_free_ino(inf);
        }
 }
 
-/* For dentries of directories in the root dir */
-static const struct dentry_operations autofs4_root_dentry_operations = {
-       .d_release      = autofs4_dentry_release,
-};
-
-/* For other dentries */
-static const struct dentry_operations autofs4_dentry_operations = {
-       .d_automount    = autofs4_d_automount,
-       .d_manage       = autofs4_d_manage,
-       .d_release      = autofs4_dentry_release,
-};
-
 static struct dentry *autofs4_lookup_active(struct dentry *dentry)
 {
        struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
@@ -595,7 +326,7 @@ static struct dentry *autofs4_mountpoint_changed(struct path *path)
        return path->dentry;
 }
 
-struct vfsmount *autofs4_d_automount(struct path *path)
+static struct vfsmount *autofs4_d_automount(struct path *path)
 {
        struct dentry *dentry = path->dentry;
        struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
@@ -713,7 +444,7 @@ done:
        return NULL;
 }
 
-int autofs4_d_manage(struct dentry *dentry, bool mounting_here)
+int autofs4_d_manage(struct dentry *dentry, bool mounting_here, bool rcu_walk)
 {
        struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
 
@@ -727,6 +458,10 @@ int autofs4_d_manage(struct dentry *dentry, bool mounting_here)
                return 0;
        }
 
+       /* We need to sleep, so we need pathwalk to be in ref-mode */
+       if (rcu_walk)
+               return -ECHILD;
+
        /* Wait for pending expires */
        do_expire_wait(dentry);
 
@@ -753,14 +488,13 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s
        sbi = autofs4_sbi(dir->i_sb);
 
        DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
-               current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
+               current->pid, task_pgrp_nr(current), sbi->catatonic,
+               autofs4_oz_mode(sbi));
 
        active = autofs4_lookup_active(dentry);
        if (active) {
                return active;
        } else {
-               d_set_d_op(dentry, &autofs4_root_dentry_operations);
-
                /*
                 * A dentry that is not within the root can never trigger a
                 * mount operation, unless the directory already exists, so we
@@ -771,12 +505,10 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s
                        return ERR_PTR(-ENOENT);
 
                /* Mark entries in the root as mount triggers */
-               if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent)) {
-                       d_set_d_op(dentry, &autofs4_dentry_operations);
+               if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent))
                        __managed_dentry_set_managed(dentry);
-               }
 
-               ino = autofs4_init_ino(NULL, sbi, 0555);
+               ino = autofs4_init_ino(NULL, sbi);
                if (!ino)
                        return ERR_PTR(-ENOMEM);
 
@@ -798,6 +530,7 @@ static int autofs4_dir_symlink(struct inode *dir,
        struct autofs_info *ino = autofs4_dentry_ino(dentry);
        struct autofs_info *p_ino;
        struct inode *inode;
+       size_t size = strlen(symname);
        char *cp;
 
        DPRINTK("%s <- %.*s", symname,
@@ -806,40 +539,35 @@ static int autofs4_dir_symlink(struct inode *dir,
        if (!autofs4_oz_mode(sbi))
                return -EACCES;
 
-       ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
-       if (!ino)
-               return -ENOMEM;
+       BUG_ON(!ino);
+
+       autofs4_init_ino(ino, sbi);
 
        autofs4_del_active(dentry);
 
-       ino->size = strlen(symname);
-       cp = kmalloc(ino->size + 1, GFP_KERNEL);
-       if (!cp) {
-               if (!dentry->d_fsdata)
-                       kfree(ino);
+       cp = kmalloc(size + 1, GFP_KERNEL);
+       if (!cp)
                return -ENOMEM;
-       }
 
        strcpy(cp, symname);
 
-       inode = autofs4_get_inode(dir->i_sb, ino);
+       inode = autofs4_get_inode(dir->i_sb, S_IFLNK | 0555);
        if (!inode) {
                kfree(cp);
                if (!dentry->d_fsdata)
                        kfree(ino);
                return -ENOMEM;
        }
+       inode->i_private = cp;
+       inode->i_size = size;
        d_add(dentry, inode);
 
-       dentry->d_fsdata = ino;
-       ino->dentry = dget(dentry);
+       dget(dentry);
        atomic_inc(&ino->count);
        p_ino = autofs4_dentry_ino(dentry->d_parent);
        if (p_ino && dentry->d_parent != dentry)
                atomic_inc(&p_ino->count);
-       ino->inode = inode;
 
-       ino->u.symlink = cp;
        dir->i_mtime = CURRENT_TIME;
 
        return 0;
@@ -892,6 +620,58 @@ static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
        return 0;
 }
 
+/*
+ * Version 4 of autofs provides a pseudo direct mount implementation
+ * that relies on directories at the leaves of a directory tree under
+ * an indirect mount to trigger mounts. To allow for this we need to
+ * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
+ * of the directory tree. There is no need to clear the automount flag
+ * following a mount or restore it after an expire because these mounts
+ * are always covered. However, it is neccessary to ensure that these
+ * flags are clear on non-empty directories to avoid unnecessary calls
+ * during path walks.
+ */
+static void autofs_set_leaf_automount_flags(struct dentry *dentry)
+{
+       struct dentry *parent;
+
+       /* root and dentrys in the root are already handled */
+       if (IS_ROOT(dentry->d_parent))
+               return;
+
+       managed_dentry_set_managed(dentry);
+
+       parent = dentry->d_parent;
+       /* only consider parents below dentrys in the root */
+       if (IS_ROOT(parent->d_parent))
+               return;
+       managed_dentry_clear_managed(parent);
+       return;
+}
+
+static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
+{
+       struct list_head *d_child;
+       struct dentry *parent;
+
+       /* flags for dentrys in the root are handled elsewhere */
+       if (IS_ROOT(dentry->d_parent))
+               return;
+
+       managed_dentry_clear_managed(dentry);
+
+       parent = dentry->d_parent;
+       /* only consider parents below dentrys in the root */
+       if (IS_ROOT(parent->d_parent))
+               return;
+       d_child = &dentry->d_u.d_child;
+       /* Set parent managed if it's becoming empty */
+       if (d_child->next == &parent->d_subdirs &&
+           d_child->prev == &parent->d_subdirs)
+               managed_dentry_set_managed(parent);
+       return;
+}
+
 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
 {
        struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
@@ -919,6 +699,9 @@ static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
        spin_unlock(&dentry->d_lock);
        spin_unlock(&autofs4_lock);
 
+       if (sbi->version < 5)
+               autofs_clear_leaf_automount_flags(dentry);
+
        if (atomic_dec_and_test(&ino->count)) {
                p_ino = autofs4_dentry_ino(dentry->d_parent);
                if (p_ino && dentry->d_parent != dentry)
@@ -947,27 +730,25 @@ static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
        DPRINTK("dentry %p, creating %.*s",
                dentry, dentry->d_name.len, dentry->d_name.name);
 
-       ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
-       if (!ino)
-               return -ENOMEM;
+       BUG_ON(!ino);
+
+       autofs4_init_ino(ino, sbi);
 
        autofs4_del_active(dentry);
 
-       inode = autofs4_get_inode(dir->i_sb, ino);
-       if (!inode) {
-               if (!dentry->d_fsdata)
-                       kfree(ino);
+       inode = autofs4_get_inode(dir->i_sb, S_IFDIR | 0555);
+       if (!inode)
                return -ENOMEM;
-       }
        d_add(dentry, inode);
 
-       dentry->d_fsdata = ino;
-       ino->dentry = dget(dentry);
+       if (sbi->version < 5)
+               autofs_set_leaf_automount_flags(dentry);
+
+       dget(dentry);
        atomic_inc(&ino->count);
        p_ino = autofs4_dentry_ino(dentry->d_parent);
        if (p_ino && dentry->d_parent != dentry)
                atomic_inc(&p_ino->count);
-       ino->inode = inode;
        inc_nlink(dir);
        dir->i_mtime = CURRENT_TIME;
 
@@ -1049,8 +830,7 @@ static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
 int is_autofs4_dentry(struct dentry *dentry)
 {
        return dentry && dentry->d_inode &&
-               (dentry->d_op == &autofs4_root_dentry_operations ||
-                dentry->d_op == &autofs4_dentry_operations) &&
+               dentry->d_op == &autofs4_dentry_operations &&
                dentry->d_fsdata != NULL;
 }