patch-5.15.79-rt54.patch
[platform/kernel/linux-rpi.git] / fs / namei.c
index 95a881e..71c0b93 100644 (file)
@@ -255,7 +255,7 @@ getname_kernel(const char * filename)
 
 void putname(struct filename *name)
 {
-       if (IS_ERR_OR_NULL(name))
+       if (IS_ERR(name))
                return;
 
        BUG_ON(name->refcnt <= 0);
@@ -1461,6 +1461,8 @@ static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
                                 * becoming unpinned.
                                 */
                                flags = dentry->d_flags;
+                               if (read_seqretry(&mount_lock, nd->m_seq))
+                                       return false;
                                continue;
                        }
                        if (read_seqretry(&mount_lock, nd->m_seq))
@@ -1633,7 +1635,7 @@ static struct dentry *__lookup_slow(const struct qstr *name,
 {
        struct dentry *dentry, *old;
        struct inode *inode = dir->d_inode;
-       DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
+       DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
 
        /* Don't go there if it's already dead */
        if (unlikely(IS_DEADDIR(inode)))
@@ -2467,7 +2469,7 @@ static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path
        return err;
 }
 
-static int __filename_lookup(int dfd, struct filename *name, unsigned flags,
+int filename_lookup(int dfd, struct filename *name, unsigned flags,
                    struct path *path, struct path *root)
 {
        int retval;
@@ -2488,15 +2490,6 @@ static int __filename_lookup(int dfd, struct filename *name, unsigned flags,
        return retval;
 }
 
-int filename_lookup(int dfd, struct filename *name, unsigned flags,
-                   struct path *path, struct path *root)
-{
-       int retval = __filename_lookup(dfd, name, flags, path, root);
-
-       putname(name);
-       return retval;
-}
-
 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
 static int path_parentat(struct nameidata *nd, unsigned flags,
                                struct path *parent)
@@ -2514,9 +2507,10 @@ static int path_parentat(struct nameidata *nd, unsigned flags,
        return err;
 }
 
-static int __filename_parentat(int dfd, struct filename *name,
-                               unsigned int flags, struct path *parent,
-                               struct qstr *last, int *type)
+/* Note: this does not consume "name" */
+static int filename_parentat(int dfd, struct filename *name,
+                            unsigned int flags, struct path *parent,
+                            struct qstr *last, int *type)
 {
        int retval;
        struct nameidata nd;
@@ -2538,25 +2532,14 @@ static int __filename_parentat(int dfd, struct filename *name,
        return retval;
 }
 
-static int filename_parentat(int dfd, struct filename *name,
-                               unsigned int flags, struct path *parent,
-                               struct qstr *last, int *type)
-{
-       int retval = __filename_parentat(dfd, name, flags, parent, last, type);
-
-       putname(name);
-       return retval;
-}
-
 /* does lookup, returns the object with parent locked */
-struct dentry *kern_path_locked(const char *name, struct path *path)
+static struct dentry *__kern_path_locked(struct filename *name, struct path *path)
 {
        struct dentry *d;
        struct qstr last;
        int type, error;
 
-       error = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
-                                   &last, &type);
+       error = filename_parentat(AT_FDCWD, name, 0, path, &last, &type);
        if (error)
                return ERR_PTR(error);
        if (unlikely(type != LAST_NORM)) {
@@ -2572,10 +2555,23 @@ struct dentry *kern_path_locked(const char *name, struct path *path)
        return d;
 }
 
+struct dentry *kern_path_locked(const char *name, struct path *path)
+{
+       struct filename *filename = getname_kernel(name);
+       struct dentry *res = __kern_path_locked(filename, path);
+
+       putname(filename);
+       return res;
+}
+
 int kern_path(const char *name, unsigned int flags, struct path *path)
 {
-       return filename_lookup(AT_FDCWD, getname_kernel(name),
-                              flags, path, NULL);
+       struct filename *filename = getname_kernel(name);
+       int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL);
+
+       putname(filename);
+       return ret;
+
 }
 EXPORT_SYMBOL(kern_path);
 
@@ -2591,10 +2587,15 @@ int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
                    const char *name, unsigned int flags,
                    struct path *path)
 {
+       struct filename *filename;
        struct path root = {.mnt = mnt, .dentry = dentry};
+       int ret;
+
+       filename = getname_kernel(name);
        /* the first argument of filename_lookup() is ignored with root */
-       return filename_lookup(AT_FDCWD, getname_kernel(name),
-                              flags , path, &root);
+       ret = filename_lookup(AT_FDCWD, filename, flags, path, &root);
+       putname(filename);
+       return ret;
 }
 EXPORT_SYMBOL(vfs_path_lookup);
 
@@ -2719,7 +2720,8 @@ struct dentry *lookup_one(struct user_namespace *mnt_userns, const char *name,
 EXPORT_SYMBOL(lookup_one);
 
 /**
- * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
+ * lookup_one_unlocked - filesystem helper to lookup single pathname component
+ * @mnt_userns:        idmapping of the mount the lookup is performed from
  * @name:      pathname component to lookup
  * @base:      base directory to lookup from
  * @len:       maximum length @len should be interpreted to
@@ -2730,14 +2732,15 @@ EXPORT_SYMBOL(lookup_one);
  * Unlike lookup_one_len, it should be called without the parent
  * i_mutex held, and will take the i_mutex itself if necessary.
  */
-struct dentry *lookup_one_len_unlocked(const char *name,
-                                      struct dentry *base, int len)
+struct dentry *lookup_one_unlocked(struct user_namespace *mnt_userns,
+                                  const char *name, struct dentry *base,
+                                  int len)
 {
        struct qstr this;
        int err;
        struct dentry *ret;
 
-       err = lookup_one_common(&init_user_ns, name, base, len, &this);
+       err = lookup_one_common(mnt_userns, name, base, len, &this);
        if (err)
                return ERR_PTR(err);
 
@@ -2746,6 +2749,59 @@ struct dentry *lookup_one_len_unlocked(const char *name,
                ret = lookup_slow(&this, base, 0);
        return ret;
 }
+EXPORT_SYMBOL(lookup_one_unlocked);
+
+/**
+ * lookup_one_positive_unlocked - filesystem helper to lookup single
+ *                               pathname component
+ * @mnt_userns:        idmapping of the mount the lookup is performed from
+ * @name:      pathname component to lookup
+ * @base:      base directory to lookup from
+ * @len:       maximum length @len should be interpreted to
+ *
+ * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns
+ * known positive or ERR_PTR(). This is what most of the users want.
+ *
+ * Note that pinned negative with unlocked parent _can_ become positive at any
+ * time, so callers of lookup_one_unlocked() need to be very careful; pinned
+ * positives have >d_inode stable, so this one avoids such problems.
+ *
+ * Note that this routine is purely a helper for filesystem usage and should
+ * not be called by generic code.
+ *
+ * The helper should be called without i_mutex held.
+ */
+struct dentry *lookup_one_positive_unlocked(struct user_namespace *mnt_userns,
+                                           const char *name,
+                                           struct dentry *base, int len)
+{
+       struct dentry *ret = lookup_one_unlocked(mnt_userns, name, base, len);
+
+       if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
+               dput(ret);
+               ret = ERR_PTR(-ENOENT);
+       }
+       return ret;
+}
+EXPORT_SYMBOL(lookup_one_positive_unlocked);
+
+/**
+ * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
+ * @name:      pathname component to lookup
+ * @base:      base directory to lookup from
+ * @len:       maximum length @len should be interpreted to
+ *
+ * Note that this routine is purely a helper for filesystem usage and should
+ * not be called by generic code.
+ *
+ * Unlike lookup_one_len, it should be called without the parent
+ * i_mutex held, and will take the i_mutex itself if necessary.
+ */
+struct dentry *lookup_one_len_unlocked(const char *name,
+                                      struct dentry *base, int len)
+{
+       return lookup_one_unlocked(&init_user_ns, name, base, len);
+}
 EXPORT_SYMBOL(lookup_one_len_unlocked);
 
 /*
@@ -2759,12 +2815,7 @@ EXPORT_SYMBOL(lookup_one_len_unlocked);
 struct dentry *lookup_positive_unlocked(const char *name,
                                       struct dentry *base, int len)
 {
-       struct dentry *ret = lookup_one_len_unlocked(name, base, len);
-       if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
-               dput(ret);
-               ret = ERR_PTR(-ENOENT);
-       }
-       return ret;
+       return lookup_one_positive_unlocked(&init_user_ns, name, base, len);
 }
 EXPORT_SYMBOL(lookup_positive_unlocked);
 
@@ -2798,8 +2849,11 @@ int path_pts(struct path *path)
 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
                 struct path *path, int *empty)
 {
-       return filename_lookup(dfd, getname_flags(name, flags, empty),
-                              flags, path, NULL);
+       struct filename *filename = getname_flags(name, flags, empty);
+       int ret = filename_lookup(dfd, filename, flags, path, NULL);
+
+       putname(filename);
+       return ret;
 }
 EXPORT_SYMBOL(user_path_at_empty);
 
@@ -3192,7 +3246,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
        struct dentry *dentry;
        int error, create_error = 0;
        umode_t mode = op->mode;
-       DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
+       DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
 
        if (unlikely(IS_DEADDIR(dir_inode)))
                return ERR_PTR(-ENOENT);
@@ -3471,6 +3525,8 @@ struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns,
        child = d_alloc(dentry, &slash_name);
        if (unlikely(!child))
                goto out_err;
+       if (!IS_POSIXACL(dir))
+               mode &= ~current_umask();
        error = dir->i_op->tmpfile(mnt_userns, dir, child, mode);
        if (error)
                goto out_err;
@@ -3618,23 +3674,19 @@ struct file *do_file_open_root(const struct path *root,
        return file;
 }
 
-static struct dentry *__filename_create(int dfd, struct filename *name,
-                               struct path *path, unsigned int lookup_flags)
+static struct dentry *filename_create(int dfd, struct filename *name,
+                                     struct path *path, unsigned int lookup_flags)
 {
        struct dentry *dentry = ERR_PTR(-EEXIST);
        struct qstr last;
+       bool want_dir = lookup_flags & LOOKUP_DIRECTORY;
+       unsigned int reval_flag = lookup_flags & LOOKUP_REVAL;
+       unsigned int create_flags = LOOKUP_CREATE | LOOKUP_EXCL;
        int type;
        int err2;
        int error;
-       bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
 
-       /*
-        * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
-        * other flags passed in are ignored!
-        */
-       lookup_flags &= LOOKUP_REVAL;
-
-       error = __filename_parentat(dfd, name, lookup_flags, path, &last, &type);
+       error = filename_parentat(dfd, name, reval_flag, path, &last, &type);
        if (error)
                return ERR_PTR(error);
 
@@ -3648,11 +3700,13 @@ static struct dentry *__filename_create(int dfd, struct filename *name,
        /* don't fail immediately if it's r/o, at least try to report other errors */
        err2 = mnt_want_write(path->mnt);
        /*
-        * Do the final lookup.
+        * Do the final lookup.  Suppress 'create' if there is a trailing
+        * '/', and a directory wasn't requested.
         */
-       lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
+       if (last.name[last.len] && !want_dir)
+               create_flags = 0;
        inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
-       dentry = __lookup_hash(&last, path->dentry, lookup_flags);
+       dentry = __lookup_hash(&last, path->dentry, reval_flag | create_flags);
        if (IS_ERR(dentry))
                goto unlock;
 
@@ -3666,7 +3720,7 @@ static struct dentry *__filename_create(int dfd, struct filename *name,
         * all is fine. Let's be bastards - you had / on the end, you've
         * been asking for (non-existent) directory. -ENOENT for you.
         */
-       if (unlikely(!is_dir && last.name[last.len])) {
+       if (unlikely(!create_flags)) {
                error = -ENOENT;
                goto fail;
        }
@@ -3687,21 +3741,15 @@ out:
        return dentry;
 }
 
-static inline struct dentry *filename_create(int dfd, struct filename *name,
+struct dentry *kern_path_create(int dfd, const char *pathname,
                                struct path *path, unsigned int lookup_flags)
 {
-       struct dentry *res = __filename_create(dfd, name, path, lookup_flags);
+       struct filename *filename = getname_kernel(pathname);
+       struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
 
-       putname(name);
+       putname(filename);
        return res;
 }
-
-struct dentry *kern_path_create(int dfd, const char *pathname,
-                               struct path *path, unsigned int lookup_flags)
-{
-       return filename_create(dfd, getname_kernel(pathname),
-                               path, lookup_flags);
-}
 EXPORT_SYMBOL(kern_path_create);
 
 void done_path_create(struct path *path, struct dentry *dentry)
@@ -3716,7 +3764,11 @@ EXPORT_SYMBOL(done_path_create);
 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
                                struct path *path, unsigned int lookup_flags)
 {
-       return filename_create(dfd, getname(pathname), path, lookup_flags);
+       struct filename *filename = getname(pathname);
+       struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
+
+       putname(filename);
+       return res;
 }
 EXPORT_SYMBOL(user_path_create);
 
@@ -3797,7 +3849,7 @@ static int do_mknodat(int dfd, struct filename *name, umode_t mode,
        if (error)
                goto out1;
 retry:
-       dentry = __filename_create(dfd, name, &path, lookup_flags);
+       dentry = filename_create(dfd, name, &path, lookup_flags);
        error = PTR_ERR(dentry);
        if (IS_ERR(dentry))
                goto out1;
@@ -3897,7 +3949,7 @@ int do_mkdirat(int dfd, struct filename *name, umode_t mode)
        unsigned int lookup_flags = LOOKUP_DIRECTORY;
 
 retry:
-       dentry = __filename_create(dfd, name, &path, lookup_flags);
+       dentry = filename_create(dfd, name, &path, lookup_flags);
        error = PTR_ERR(dentry);
        if (IS_ERR(dentry))
                goto out_putname;
@@ -3975,13 +4027,12 @@ int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir,
        dentry->d_inode->i_flags |= S_DEAD;
        dont_mount(dentry);
        detach_mounts(dentry);
-       fsnotify_rmdir(dir, dentry);
 
 out:
        inode_unlock(dentry->d_inode);
        dput(dentry);
        if (!error)
-               d_delete(dentry);
+               d_delete_notify(dir, dentry);
        return error;
 }
 EXPORT_SYMBOL(vfs_rmdir);
@@ -3996,7 +4047,7 @@ int do_rmdir(int dfd, struct filename *name)
        int type;
        unsigned int lookup_flags = 0;
 retry:
-       error = __filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
+       error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
        if (error)
                goto exit1;
 
@@ -4103,7 +4154,6 @@ int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir,
                        if (!error) {
                                dont_mount(dentry);
                                detach_mounts(dentry);
-                               fsnotify_unlink(dir, dentry);
                        }
                }
        }
@@ -4111,9 +4161,11 @@ out:
        inode_unlock(target);
 
        /* We don't d_delete() NFS sillyrenamed files--they still exist. */
-       if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
+       if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) {
+               fsnotify_unlink(dir, dentry);
+       } else if (!error) {
                fsnotify_link_count(target);
-               d_delete(dentry);
+               d_delete_notify(dir, dentry);
        }
 
        return error;
@@ -4137,7 +4189,7 @@ int do_unlinkat(int dfd, struct filename *name)
        struct inode *delegated_inode = NULL;
        unsigned int lookup_flags = 0;
 retry:
-       error = __filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
+       error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
        if (error)
                goto exit1;
 
@@ -4266,7 +4318,7 @@ int do_symlinkat(struct filename *from, int newdfd, struct filename *to)
                goto out_putnames;
        }
 retry:
-       dentry = __filename_create(newdfd, to, &path, lookup_flags);
+       dentry = filename_create(newdfd, to, &path, lookup_flags);
        error = PTR_ERR(dentry);
        if (IS_ERR(dentry))
                goto out_putnames;
@@ -4426,11 +4478,11 @@ int do_linkat(int olddfd, struct filename *old, int newdfd,
        if (flags & AT_SYMLINK_FOLLOW)
                how |= LOOKUP_FOLLOW;
 retry:
-       error = __filename_lookup(olddfd, old, how, &old_path, NULL);
+       error = filename_lookup(olddfd, old, how, &old_path, NULL);
        if (error)
                goto out_putnames;
 
-       new_dentry = __filename_create(newdfd, new, &new_path,
+       new_dentry = filename_create(newdfd, new, &new_path,
                                        (how & LOOKUP_REVAL));
        error = PTR_ERR(new_dentry);
        if (IS_ERR(new_dentry))
@@ -4689,13 +4741,13 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd,
                target_flags = 0;
 
 retry:
-       error = __filename_parentat(olddfd, from, lookup_flags, &old_path,
-                                       &old_last, &old_type);
+       error = filename_parentat(olddfd, from, lookup_flags, &old_path,
+                                 &old_last, &old_type);
        if (error)
                goto put_names;
 
-       error = __filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
-                               &new_type);
+       error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
+                                 &new_type);
        if (error)
                goto exit1;