autofs4: mkdir and symlink always get a dentry that had passed lookup
[firefly-linux-kernel-4.4.55.git] / fs / autofs4 / root.c
index 9194e274f849b65b570be6efd621e5974750fdac..a5b93e8f49b558a533172aa3088d915a4299d693 100644 (file)
@@ -36,7 +36,7 @@ static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
 static int autofs4_dir_open(struct inode *inode, struct file *file);
 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
 static struct vfsmount *autofs4_d_automount(struct path *);
-static int autofs4_d_manage(struct dentry *, bool);
+static int autofs4_d_manage(struct dentry *, bool, bool);
 
 const struct file_operations autofs4_root_operations = {
        .open           = dcache_dir_open,
@@ -66,13 +66,7 @@ const struct inode_operations autofs4_dir_inode_operations = {
        .rmdir          = autofs4_dir_rmdir,
 };
 
-/* For dentries that don't initiate mounting */
 const struct dentry_operations autofs4_dentry_operations = {
-       .d_release      = autofs4_dentry_release,
-};
-
-/* For dentries that do initiate mounting */
-const struct dentry_operations autofs4_mount_dentry_operations = {
        .d_automount    = autofs4_d_automount,
        .d_manage       = autofs4_d_manage,
        .d_release      = autofs4_dentry_release,
@@ -450,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);
 
@@ -464,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);
 
@@ -490,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_dentry_operations);
-
                /*
                 * A dentry that is not within the root can never trigger a
                 * mount operation, unless the directory already exists, so we
@@ -508,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_mount_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);
 
@@ -535,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,
@@ -543,41 +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);
 
-       d_set_d_op(dentry, &autofs4_dentry_operations);
-
-       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->u.symlink = cp;
        dir->i_mtime = CURRENT_TIME;
 
        return 0;
@@ -740,25 +730,21 @@ 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);
 
        if (sbi->version < 5)
                autofs_set_leaf_automount_flags(dentry);
 
-       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)
@@ -844,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_mount_dentry_operations ||
-                dentry->d_op == &autofs4_dentry_operations) &&
+               dentry->d_op == &autofs4_dentry_operations &&
                dentry->d_fsdata != NULL;
 }