VFS: Put a small type field into struct dentry::d_flags
[firefly-linux-kernel-4.4.55.git] / fs / namei.c
index 645268f23eb64cb8c2931ce33391beb2d0080c36..e1fa43346c612e5e65e75c3e83735fddebf7f236 100644 (file)
@@ -482,18 +482,6 @@ EXPORT_SYMBOL(path_put);
  * to restart the path walk from the beginning in ref-walk mode.
  */
 
-static inline void lock_rcu_walk(void)
-{
-       br_read_lock(&vfsmount_lock);
-       rcu_read_lock();
-}
-
-static inline void unlock_rcu_walk(void)
-{
-       rcu_read_unlock();
-       br_read_unlock(&vfsmount_lock);
-}
-
 /**
  * unlazy_walk - try to switch to ref-walk mode.
  * @nd: nameidata pathwalk data
@@ -512,26 +500,23 @@ static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
        BUG_ON(!(nd->flags & LOOKUP_RCU));
 
        /*
-        * Get a reference to the parent first: we're
-        * going to make "path_put(nd->path)" valid in
-        * non-RCU context for "terminate_walk()".
-        *
-        * If this doesn't work, return immediately with
-        * RCU walking still active (and then we will do
-        * the RCU walk cleanup in terminate_walk()).
+        * After legitimizing the bastards, terminate_walk()
+        * will do the right thing for non-RCU mode, and all our
+        * subsequent exit cases should rcu_read_unlock()
+        * before returning.  Do vfsmount first; if dentry
+        * can't be legitimized, just set nd->path.dentry to NULL
+        * and rely on dput(NULL) being a no-op.
         */
-       if (!lockref_get_not_dead(&parent->d_lockref))
+       if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
                return -ECHILD;
-
-       /*
-        * After the mntget(), we terminate_walk() will do
-        * the right thing for non-RCU mode, and all our
-        * subsequent exit cases should unlock_rcu_walk()
-        * before returning.
-        */
-       mntget(nd->path.mnt);
        nd->flags &= ~LOOKUP_RCU;
 
+       if (!lockref_get_not_dead(&parent->d_lockref)) {
+               nd->path.dentry = NULL; 
+               rcu_read_unlock();
+               return -ECHILD;
+       }
+
        /*
         * For a negative lookup, the lookup sequence point is the parents
         * sequence point, and it only needs to revalidate the parent dentry.
@@ -566,17 +551,17 @@ static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
                spin_unlock(&fs->lock);
        }
 
-       unlock_rcu_walk();
+       rcu_read_unlock();
        return 0;
 
 unlock_and_drop_dentry:
        spin_unlock(&fs->lock);
 drop_dentry:
-       unlock_rcu_walk();
+       rcu_read_unlock();
        dput(dentry);
        goto drop_root_mnt;
 out:
-       unlock_rcu_walk();
+       rcu_read_unlock();
 drop_root_mnt:
        if (!(nd->flags & LOOKUP_ROOT))
                nd->root.mnt = NULL;
@@ -608,17 +593,22 @@ static int complete_walk(struct nameidata *nd)
                if (!(nd->flags & LOOKUP_ROOT))
                        nd->root.mnt = NULL;
 
+               if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
+                       rcu_read_unlock();
+                       return -ECHILD;
+               }
                if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
-                       unlock_rcu_walk();
+                       rcu_read_unlock();
+                       mntput(nd->path.mnt);
                        return -ECHILD;
                }
                if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
-                       unlock_rcu_walk();
+                       rcu_read_unlock();
                        dput(dentry);
+                       mntput(nd->path.mnt);
                        return -ECHILD;
                }
-               mntget(nd->path.mnt);
-               unlock_rcu_walk();
+               rcu_read_unlock();
        }
 
        if (likely(!(nd->flags & LOOKUP_JUMPED)))
@@ -909,15 +899,15 @@ int follow_up(struct path *path)
        struct mount *parent;
        struct dentry *mountpoint;
 
-       br_read_lock(&vfsmount_lock);
+       read_seqlock_excl(&mount_lock);
        parent = mnt->mnt_parent;
        if (parent == mnt) {
-               br_read_unlock(&vfsmount_lock);
+               read_sequnlock_excl(&mount_lock);
                return 0;
        }
        mntget(&parent->mnt);
        mountpoint = dget(mnt->mnt_mountpoint);
-       br_read_unlock(&vfsmount_lock);
+       read_sequnlock_excl(&mount_lock);
        dput(path->dentry);
        path->dentry = mountpoint;
        mntput(path->mnt);
@@ -1048,8 +1038,8 @@ static int follow_managed(struct path *path, unsigned flags)
 
                        /* Something is mounted on this dentry in another
                         * namespace and/or whatever was mounted there in this
-                        * namespace got unmounted before we managed to get the
-                        * vfsmount_lock */
+                        * namespace got unmounted before lookup_mnt() could
+                        * get it */
                }
 
                /* Handle an automount point */
@@ -1111,7 +1101,7 @@ static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
                if (!d_mountpoint(path->dentry))
                        break;
 
-               mounted = __lookup_mnt(path->mnt, path->dentry, 1);
+               mounted = __lookup_mnt(path->mnt, path->dentry);
                if (!mounted)
                        break;
                path->mnt = &mounted->mnt;
@@ -1132,7 +1122,7 @@ static void follow_mount_rcu(struct nameidata *nd)
 {
        while (d_mountpoint(nd->path.dentry)) {
                struct mount *mounted;
-               mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
+               mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
                if (!mounted)
                        break;
                nd->path.mnt = &mounted->mnt;
@@ -1174,7 +1164,7 @@ failed:
        nd->flags &= ~LOOKUP_RCU;
        if (!(nd->flags & LOOKUP_ROOT))
                nd->root.mnt = NULL;
-       unlock_rcu_walk();
+       rcu_read_unlock();
        return -ECHILD;
 }
 
@@ -1501,7 +1491,7 @@ static void terminate_walk(struct nameidata *nd)
                nd->flags &= ~LOOKUP_RCU;
                if (!(nd->flags & LOOKUP_ROOT))
                        nd->root.mnt = NULL;
-               unlock_rcu_walk();
+               rcu_read_unlock();
        }
 }
 
@@ -1511,18 +1501,9 @@ static void terminate_walk(struct nameidata *nd)
  * so we keep a cache of "no, this doesn't need follow_link"
  * for the common case.
  */
-static inline int should_follow_link(struct inode *inode, int follow)
+static inline int should_follow_link(struct dentry *dentry, int follow)
 {
-       if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
-               if (likely(inode->i_op->follow_link))
-                       return follow;
-
-               /* This gets set once for the inode lifetime */
-               spin_lock(&inode->i_lock);
-               inode->i_opflags |= IOP_NOFOLLOW;
-               spin_unlock(&inode->i_lock);
-       }
-       return 0;
+       return unlikely(d_is_symlink(dentry)) ? follow : 0;
 }
 
 static inline int walk_component(struct nameidata *nd, struct path *path,
@@ -1552,7 +1533,7 @@ static inline int walk_component(struct nameidata *nd, struct path *path,
        if (!inode)
                goto out_path_put;
 
-       if (should_follow_link(inode, follow)) {
+       if (should_follow_link(path->dentry, follow)) {
                if (nd->flags & LOOKUP_RCU) {
                        if (unlikely(unlazy_walk(nd, path->dentry))) {
                                err = -ECHILD;
@@ -1610,26 +1591,6 @@ static inline int nested_symlink(struct path *path, struct nameidata *nd)
        return res;
 }
 
-/*
- * We really don't want to look at inode->i_op->lookup
- * when we don't have to. So we keep a cache bit in
- * the inode ->i_opflags field that says "yes, we can
- * do lookup on this inode".
- */
-static inline int can_lookup(struct inode *inode)
-{
-       if (likely(inode->i_opflags & IOP_LOOKUP))
-               return 1;
-       if (likely(!inode->i_op->lookup))
-               return 0;
-
-       /* We do this once for the lifetime of the inode */
-       spin_lock(&inode->i_lock);
-       inode->i_opflags |= IOP_LOOKUP;
-       spin_unlock(&inode->i_lock);
-       return 1;
-}
-
 /*
  * We can do the critical dentry name comparison and hashing
  * operations one word at a time, but we are limited to:
@@ -1833,7 +1794,7 @@ static int link_path_walk(const char *name, struct nameidata *nd)
                        if (err)
                                return err;
                }
-               if (!can_lookup(nd->inode)) {
+               if (!d_is_directory(nd->path.dentry)) {
                        err = -ENOTDIR; 
                        break;
                }
@@ -1851,9 +1812,10 @@ static int path_init(int dfd, const char *name, unsigned int flags,
        nd->flags = flags | LOOKUP_JUMPED;
        nd->depth = 0;
        if (flags & LOOKUP_ROOT) {
-               struct inode *inode = nd->root.dentry->d_inode;
+               struct dentry *root = nd->root.dentry;
+               struct inode *inode = root->d_inode;
                if (*name) {
-                       if (!can_lookup(inode))
+                       if (!d_is_directory(root))
                                return -ENOTDIR;
                        retval = inode_permission(inode, MAY_EXEC);
                        if (retval)
@@ -1862,8 +1824,9 @@ static int path_init(int dfd, const char *name, unsigned int flags,
                nd->path = nd->root;
                nd->inode = inode;
                if (flags & LOOKUP_RCU) {
-                       lock_rcu_walk();
+                       rcu_read_lock();
                        nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
+                       nd->m_seq = read_seqbegin(&mount_lock);
                } else {
                        path_get(&nd->path);
                }
@@ -1872,9 +1835,10 @@ static int path_init(int dfd, const char *name, unsigned int flags,
 
        nd->root.mnt = NULL;
 
+       nd->m_seq = read_seqbegin(&mount_lock);
        if (*name=='/') {
                if (flags & LOOKUP_RCU) {
-                       lock_rcu_walk();
+                       rcu_read_lock();
                        set_root_rcu(nd);
                } else {
                        set_root(nd);
@@ -1886,7 +1850,7 @@ static int path_init(int dfd, const char *name, unsigned int flags,
                        struct fs_struct *fs = current->fs;
                        unsigned seq;
 
-                       lock_rcu_walk();
+                       rcu_read_lock();
 
                        do {
                                seq = read_seqcount_begin(&fs->seq);
@@ -1907,7 +1871,7 @@ static int path_init(int dfd, const char *name, unsigned int flags,
                dentry = f.file->f_path.dentry;
 
                if (*name) {
-                       if (!can_lookup(dentry->d_inode)) {
+                       if (!d_is_directory(dentry)) {
                                fdput(f);
                                return -ENOTDIR;
                        }
@@ -1918,7 +1882,7 @@ static int path_init(int dfd, const char *name, unsigned int flags,
                        if (f.need_put)
                                *fp = f.file;
                        nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
-                       lock_rcu_walk();
+                       rcu_read_lock();
                } else {
                        path_get(&nd->path);
                        fdput(f);
@@ -1989,7 +1953,7 @@ static int path_lookupat(int dfd, const char *name,
                err = complete_walk(nd);
 
        if (!err && nd->flags & LOOKUP_DIRECTORY) {
-               if (!can_lookup(nd->inode)) {
+               if (!d_is_directory(nd->path.dentry)) {
                        path_put(&nd->path);
                        err = -ENOTDIR;
                }
@@ -2281,7 +2245,7 @@ done:
        }
        path->dentry = dentry;
        path->mnt = mntget(nd->path.mnt);
-       if (should_follow_link(dentry->d_inode, nd->flags & LOOKUP_FOLLOW))
+       if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
                return 1;
        follow_mount(path);
        error = 0;
@@ -2425,12 +2389,14 @@ static inline int check_sticky(struct inode *dir, struct inode *inode)
  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  *     nfs_async_unlink().
  */
-static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
+static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
 {
+       struct inode *inode = victim->d_inode;
        int error;
 
-       if (!victim->d_inode)
+       if (d_is_negative(victim))
                return -ENOENT;
+       BUG_ON(!inode);
 
        BUG_ON(victim->d_parent->d_inode != dir);
        audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
@@ -2440,15 +2406,16 @@ static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
                return error;
        if (IS_APPEND(dir))
                return -EPERM;
-       if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
-           IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
+
+       if (check_sticky(dir, inode) || IS_APPEND(inode) ||
+           IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
                return -EPERM;
        if (isdir) {
-               if (!S_ISDIR(victim->d_inode->i_mode))
+               if (!d_is_directory(victim) && !d_is_autodir(victim))
                        return -ENOTDIR;
                if (IS_ROOT(victim))
                        return -EBUSY;
-       } else if (S_ISDIR(victim->d_inode->i_mode))
+       } else if (d_is_directory(victim) || d_is_autodir(victim))
                return -EISDIR;
        if (IS_DEADDIR(dir))
                return -ENOENT;
@@ -2982,7 +2949,7 @@ retry_lookup:
        /*
         * create/update audit record if it already exists.
         */
-       if (path->dentry->d_inode)
+       if (d_is_positive(path->dentry))
                audit_inode(name, path->dentry, 0);
 
        /*
@@ -3011,12 +2978,12 @@ retry_lookup:
 finish_lookup:
        /* we _can_ be in RCU mode here */
        error = -ENOENT;
-       if (!inode) {
+       if (d_is_negative(path->dentry)) {
                path_to_nameidata(path, nd);
                goto out;
        }
 
-       if (should_follow_link(inode, !symlink_ok)) {
+       if (should_follow_link(path->dentry, !symlink_ok)) {
                if (nd->flags & LOOKUP_RCU) {
                        if (unlikely(unlazy_walk(nd, path->dentry))) {
                                error = -ECHILD;
@@ -3045,10 +3012,11 @@ finish_open:
        }
        audit_inode(name, nd->path.dentry, 0);
        error = -EISDIR;
-       if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
+       if ((open_flag & O_CREAT) &&
+           (d_is_directory(nd->path.dentry) || d_is_autodir(nd->path.dentry)))
                goto out;
        error = -ENOTDIR;
-       if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
+       if ((nd->flags & LOOKUP_DIRECTORY) && !d_is_directory(nd->path.dentry))
                goto out;
        if (!S_ISREG(nd->inode->i_mode))
                will_truncate = false;
@@ -3274,7 +3242,7 @@ struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
        nd.root.mnt = mnt;
        nd.root.dentry = dentry;
 
-       if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
+       if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
                return ERR_PTR(-ELOOP);
 
        file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
@@ -3324,8 +3292,9 @@ struct dentry *kern_path_create(int dfd, const char *pathname,
                goto unlock;
 
        error = -EEXIST;
-       if (dentry->d_inode)
+       if (d_is_positive(dentry))
                goto fail;
+
        /*
         * Special case - lookup gave negative, but... we had foo/bar/
         * From the vfs_mknod() POV we just have a negative dentry -
@@ -3714,7 +3683,7 @@ retry:
                if (nd.last.name[nd.last.len])
                        goto slashes;
                inode = dentry->d_inode;
-               if (!inode)
+               if (d_is_negative(dentry))
                        goto slashes;
                ihold(inode);
                error = security_path_unlink(&nd.path, dentry);
@@ -3739,8 +3708,12 @@ exit1:
        return error;
 
 slashes:
-       error = !dentry->d_inode ? -ENOENT :
-               S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
+       if (d_is_negative(dentry))
+               error = -ENOENT;
+       else if (d_is_directory(dentry) || d_is_autodir(dentry))
+               error = -EISDIR;
+       else
+               error = -ENOTDIR;
        goto exit2;
 }
 
@@ -4054,7 +4027,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
               struct inode *new_dir, struct dentry *new_dentry)
 {
        int error;
-       int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
+       int is_dir = d_is_directory(old_dentry) || d_is_autodir(old_dentry);
        const unsigned char *old_name;
 
        if (old_dentry->d_inode == new_dentry->d_inode)
@@ -4142,10 +4115,10 @@ retry:
                goto exit3;
        /* source must exist */
        error = -ENOENT;
-       if (!old_dentry->d_inode)
+       if (d_is_negative(old_dentry))
                goto exit4;
        /* unless the source is a directory trailing slashes give -ENOTDIR */
-       if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
+       if (!d_is_directory(old_dentry) && !d_is_autodir(old_dentry)) {
                error = -ENOTDIR;
                if (oldnd.last.name[oldnd.last.len])
                        goto exit4;