VFS: improve interface for lookup_one functions
authorNeilBrown <neil@brown.name>
Wed, 19 Mar 2025 03:01:32 +0000 (14:01 +1100)
committerChristian Brauner <brauner@kernel.org>
Mon, 7 Apr 2025 07:25:32 +0000 (09:25 +0200)
The family of functions:
  lookup_one()
  lookup_one_unlocked()
  lookup_one_positive_unlocked()

appear designed to be used by external clients of the filesystem rather
than by filesystems acting on themselves as the lookup_one_len family
are used.

They are used by:
   btrfs/ioctl - which is a user-space interface rather than an internal
     activity
   exportfs - i.e. from nfsd or the open_by_handle_at interface
   overlayfs - at access the underlying filesystems
   smb/server - for file service

They should be used by nfsd (more than just the exportfs path) and
cachefs but aren't.

It would help if the documentation didn't claim they should "not be
called by generic code".

Also the path component name is passed as "name" and "len" which are
(confusingly?) separate by the "base".  In some cases the len in simply
"strlen" and so passing a qstr using QSTR() would make the calling
clearer.
Other callers do pass separate name and len which are stored in a
struct.  Sometimes these are already stored in a qstr, other times it
easily could be.

So this patch changes these three functions to receive a 'struct qstr *',
and improves the documentation.

QSTR_LEN() is added to make it easy to pass a QSTR containing a known
len.

[brauner@kernel.org: take a struct qstr pointer]
Signed-off-by: NeilBrown <neil@brown.name>
Link: https://lore.kernel.org/r/20250319031545.2999807-2-neil@brown.name
Signed-off-by: Christian Brauner <brauner@kernel.org>
Documentation/filesystems/porting.rst
fs/btrfs/ioctl.c
fs/exportfs/expfs.c
fs/namei.c
fs/overlayfs/namei.c
fs/overlayfs/overlayfs.h
fs/overlayfs/readdir.c
fs/smb/server/smb2pdu.c
include/linux/dcache.h
include/linux/namei.h

index 767b2927c762d7bec6cc374ced59920abfb8ca16..57dcba6de7434fc5de563fc046a7900685cf45f3 100644 (file)
@@ -1203,3 +1203,12 @@ should use d_drop();d_splice_alias() and return the result of the latter.
 If a positive dentry cannot be returned for some reason, in-kernel
 clients such as cachefiles, nfsd, smb/server may not perform ideally but
 will fail-safe.
+
+---
+
+** mandatory**
+
+lookup_one(), lookup_one_unlocked(), lookup_one_positive_unlocked() now
+take a qstr instead of a name and len.  These, not the "one_len"
+versions, should be used whenever accessing a filesystem from outside
+that filesysmtem, through a mount point - which will have a mnt_idmap.
index a13d81bb56a089a5eb458d3339f486a58c15678c..502303438dd739e572576393688bf32e1fc503d8 100644 (file)
@@ -909,7 +909,7 @@ static noinline int btrfs_mksubvol(const struct path *parent,
        if (error == -EINTR)
                return error;
 
-       dentry = lookup_one(idmap, name, parent->dentry, namelen);
+       dentry = lookup_one(idmap, &QSTR_LEN(name, namelen), parent->dentry);
        error = PTR_ERR(dentry);
        if (IS_ERR(dentry))
                goto out_unlock;
@@ -2288,7 +2288,6 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
        struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
        struct mnt_idmap *idmap = file_mnt_idmap(file);
        char *subvol_name, *subvol_name_ptr = NULL;
-       int subvol_namelen;
        int ret = 0;
        bool destroy_parent = false;
 
@@ -2411,10 +2410,8 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
                        goto out;
        }
 
-       subvol_namelen = strlen(subvol_name);
-
        if (strchr(subvol_name, '/') ||
-           strncmp(subvol_name, "..", subvol_namelen) == 0) {
+           strcmp(subvol_name, "..") == 0) {
                ret = -EINVAL;
                goto free_subvol_name;
        }
@@ -2427,7 +2424,7 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
        ret = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
        if (ret == -EINTR)
                goto free_subvol_name;
-       dentry = lookup_one(idmap, subvol_name, parent, subvol_namelen);
+       dentry = lookup_one(idmap, &QSTR(subvol_name), parent);
        if (IS_ERR(dentry)) {
                ret = PTR_ERR(dentry);
                goto out_unlock_dir;
index 128dd092916bf74f7849456777a8a80dfa006468..f0ede3e81cf754578590aff874d6c1f760148dcb 100644 (file)
@@ -143,7 +143,7 @@ static struct dentry *reconnect_one(struct vfsmount *mnt,
        if (err)
                goto out_err;
        dprintk("%s: found name: %s\n", __func__, nbuf);
-       tmp = lookup_one_unlocked(mnt_idmap(mnt), nbuf, parent, strlen(nbuf));
+       tmp = lookup_one_unlocked(mnt_idmap(mnt), &QSTR(nbuf), parent);
        if (IS_ERR(tmp)) {
                dprintk("lookup failed: %ld\n", PTR_ERR(tmp));
                err = PTR_ERR(tmp);
@@ -549,8 +549,7 @@ exportfs_decode_fh_raw(struct vfsmount *mnt, struct fid *fid, int fh_len,
                }
 
                inode_lock(target_dir->d_inode);
-               nresult = lookup_one(mnt_idmap(mnt), nbuf,
-                                    target_dir, strlen(nbuf));
+               nresult = lookup_one(mnt_idmap(mnt), &QSTR(nbuf), target_dir);
                if (!IS_ERR(nresult)) {
                        if (unlikely(nresult->d_inode != result->d_inode)) {
                                dput(nresult);
index 360a86ca1f0270ca34d6bd49cc46dfb8361e0b3b..e499fb609271bbb2a0f165bfbde41221dd9661a0 100644 (file)
@@ -2922,19 +2922,17 @@ struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
 EXPORT_SYMBOL(lookup_one_len);
 
 /**
- * lookup_one - filesystem helper to lookup single pathname component
+ * lookup_one - lookup single pathname component
  * @idmap:     idmap of the mount the lookup is performed from
- * @name:      pathname component to lookup
+ * @name:      qstr holding 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.
+ * This can be used for in-kernel filesystem clients such as file servers.
  *
  * The caller must hold base->i_mutex.
  */
-struct dentry *lookup_one(struct mnt_idmap *idmap, const char *name,
-                         struct dentry *base, int len)
+struct dentry *lookup_one(struct mnt_idmap *idmap, struct qstr *name,
+                         struct dentry *base)
 {
        struct dentry *dentry;
        struct qstr this;
@@ -2942,7 +2940,7 @@ struct dentry *lookup_one(struct mnt_idmap *idmap, const char *name,
 
        WARN_ON_ONCE(!inode_is_locked(base->d_inode));
 
-       err = lookup_one_common(idmap, name, base, len, &this);
+       err = lookup_one_common(idmap, name->name, base, name->len, &this);
        if (err)
                return ERR_PTR(err);
 
@@ -2952,27 +2950,24 @@ struct dentry *lookup_one(struct mnt_idmap *idmap, const char *name,
 EXPORT_SYMBOL(lookup_one);
 
 /**
- * lookup_one_unlocked - filesystem helper to lookup single pathname component
+ * lookup_one_unlocked - lookup single pathname component
  * @idmap:     idmap of the mount the lookup is performed from
- * @name:      pathname component to lookup
+ * @name:      qstr olding 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.
+ * This can be used for in-kernel filesystem clients such as file servers.
  *
  * Unlike lookup_one_len, it should be called without the parent
- * i_mutex held, and will take the i_mutex itself if necessary.
+ * i_rwsem held, and will take the i_rwsem itself if necessary.
  */
 struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap,
-                                  const char *name, struct dentry *base,
-                                  int len)
+                                  struct qstr *name, struct dentry *base)
 {
        struct qstr this;
        int err;
        struct dentry *ret;
 
-       err = lookup_one_common(idmap, name, base, len, &this);
+       err = lookup_one_common(idmap, name->name, base, name->len, &this);
        if (err)
                return ERR_PTR(err);
 
@@ -2984,12 +2979,10 @@ struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap,
 EXPORT_SYMBOL(lookup_one_unlocked);
 
 /**
- * lookup_one_positive_unlocked - filesystem helper to lookup single
- *                               pathname component
+ * lookup_one_positive_unlocked - lookup single pathname component
  * @idmap:     idmap of the mount the lookup is performed from
- * @name:      pathname component to lookup
+ * @name:      qstr holding 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.
@@ -2998,16 +2991,15 @@ EXPORT_SYMBOL(lookup_one_unlocked);
  * 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.
+ * This can be used for in-kernel filesystem clients such as file servers.
  *
- * The helper should be called without i_mutex held.
+ * The helper should be called without i_rwsem held.
  */
 struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap,
-                                           const char *name,
-                                           struct dentry *base, int len)
+                                           struct qstr *name,
+                                           struct dentry *base)
 {
-       struct dentry *ret = lookup_one_unlocked(idmap, name, base, len);
+       struct dentry *ret = lookup_one_unlocked(idmap, name, base);
 
        if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
                dput(ret);
@@ -3032,7 +3024,7 @@ EXPORT_SYMBOL(lookup_one_positive_unlocked);
 struct dentry *lookup_one_len_unlocked(const char *name,
                                       struct dentry *base, int len)
 {
-       return lookup_one_unlocked(&nop_mnt_idmap, name, base, len);
+       return lookup_one_unlocked(&nop_mnt_idmap, &QSTR_LEN(name, len), base);
 }
 EXPORT_SYMBOL(lookup_one_len_unlocked);
 
@@ -3047,7 +3039,8 @@ EXPORT_SYMBOL(lookup_one_len_unlocked);
 struct dentry *lookup_positive_unlocked(const char *name,
                                       struct dentry *base, int len)
 {
-       return lookup_one_positive_unlocked(&nop_mnt_idmap, name, base, len);
+       return lookup_one_positive_unlocked(&nop_mnt_idmap,
+                                           &QSTR_LEN(name, len), base);
 }
 EXPORT_SYMBOL(lookup_positive_unlocked);
 
index be5c65d6f8484b1fba6b3fee379ba1d034c0df8a..ac6e893e846a0ed977e570ddae931e0b66d8820a 100644 (file)
@@ -205,8 +205,8 @@ static struct dentry *ovl_lookup_positive_unlocked(struct ovl_lookup_data *d,
                                                   struct dentry *base, int len,
                                                   bool drop_negative)
 {
-       struct dentry *ret = lookup_one_unlocked(mnt_idmap(d->layer->mnt), name,
-                                                base, len);
+       struct dentry *ret = lookup_one_unlocked(mnt_idmap(d->layer->mnt),
+                                                &QSTR_LEN(name, len), base);
 
        if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
                if (drop_negative && ret->d_lockref.count == 1) {
@@ -789,8 +789,8 @@ struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
        if (err)
                return ERR_PTR(err);
 
-       index = lookup_one_positive_unlocked(ovl_upper_mnt_idmap(ofs), name.name,
-                                            ofs->workdir, name.len);
+       index = lookup_one_positive_unlocked(ovl_upper_mnt_idmap(ofs), &name,
+                                            ofs->workdir);
        if (IS_ERR(index)) {
                err = PTR_ERR(index);
                if (err == -ENOENT) {
@@ -1371,7 +1371,7 @@ out:
 bool ovl_lower_positive(struct dentry *dentry)
 {
        struct ovl_entry *poe = OVL_E(dentry->d_parent);
-       const struct qstr *name = &dentry->d_name;
+       struct qstr *name = &dentry->d_name;
        const struct cred *old_cred;
        unsigned int i;
        bool positive = false;
@@ -1396,7 +1396,7 @@ bool ovl_lower_positive(struct dentry *dentry)
 
                this = lookup_one_positive_unlocked(
                                mnt_idmap(parentpath->layer->mnt),
-                               name->name, parentpath->dentry, name->len);
+                               name, parentpath->dentry);
                if (IS_ERR(this)) {
                        switch (PTR_ERR(this)) {
                        case -ENOENT:
index 6f2f8f4cfbbc177fbd5441483395d7ff72efe332..0db8d415c6d8053f6d3109bd75de5e13a2e07bf5 100644 (file)
@@ -402,7 +402,7 @@ static inline struct dentry *ovl_lookup_upper(struct ovl_fs *ofs,
                                              const char *name,
                                              struct dentry *base, int len)
 {
-       return lookup_one(ovl_upper_mnt_idmap(ofs), name, base, len);
+       return lookup_one(ovl_upper_mnt_idmap(ofs), &QSTR_LEN(name, len), base);
 }
 
 static inline bool ovl_open_flags_need_copy_up(int flags)
index 881ec5592da52dfb27a588496582e7084b2fbd3b..2fa450a7854d4ecb1c786c5b28562ad8f1329695 100644 (file)
@@ -271,7 +271,6 @@ static bool ovl_fill_merge(struct dir_context *ctx, const char *name,
 static int ovl_check_whiteouts(const struct path *path, struct ovl_readdir_data *rdd)
 {
        int err;
-       struct ovl_cache_entry *p;
        struct dentry *dentry, *dir = path->dentry;
        const struct cred *old_cred;
 
@@ -280,9 +279,11 @@ static int ovl_check_whiteouts(const struct path *path, struct ovl_readdir_data
        err = down_write_killable(&dir->d_inode->i_rwsem);
        if (!err) {
                while (rdd->first_maybe_whiteout) {
-                       p = rdd->first_maybe_whiteout;
+                       struct ovl_cache_entry *p =
+                               rdd->first_maybe_whiteout;
                        rdd->first_maybe_whiteout = p->next_maybe_whiteout;
-                       dentry = lookup_one(mnt_idmap(path->mnt), p->name, dir, p->len);
+                       dentry = lookup_one(mnt_idmap(path->mnt),
+                                           &QSTR_LEN(p->name, p->len), dir);
                        if (!IS_ERR(dentry)) {
                                p->is_whiteout = ovl_is_whiteout(dentry);
                                dput(dentry);
@@ -492,7 +493,7 @@ static int ovl_cache_update(const struct path *path, struct ovl_cache_entry *p,
                }
        }
        /* This checks also for xwhiteouts */
-       this = lookup_one(mnt_idmap(path->mnt), p->name, dir, p->len);
+       this = lookup_one(mnt_idmap(path->mnt), &QSTR_LEN(p->name, p->len), dir);
        if (IS_ERR_OR_NULL(this) || !this->d_inode) {
                /* Mark a stale entry */
                p->is_whiteout = true;
index d24d95d15d876b325d25d7ca250503e889c3867c..0894128b92664885202edc89006763db8c10005c 100644 (file)
@@ -4117,9 +4117,10 @@ static int process_query_dir_entries(struct smb2_query_dir_private *priv)
                        return -EINVAL;
 
                lock_dir(priv->dir_fp);
-               dent = lookup_one(idmap, priv->d_info->name,
-                                 priv->dir_fp->filp->f_path.dentry,
-                                 priv->d_info->name_len);
+               dent = lookup_one(idmap,
+                                 &QSTR_LEN(priv->d_info->name,
+                                           priv->d_info->name_len),
+                                 priv->dir_fp->filp->f_path.dentry);
                unlock_dir(priv->dir_fp);
 
                if (IS_ERR(dent)) {
index 8d1395f945bfef10014179fd5770c30306ddc83a..e974e63bcdbcba928866323fbd8303fa0a84f127 100644 (file)
@@ -57,7 +57,8 @@ struct qstr {
 };
 
 #define QSTR_INIT(n,l) { { { .len = l } }, .name = n }
-#define QSTR(n) (struct qstr)QSTR_INIT(n, strlen(n))
+#define QSTR_LEN(n,l) (struct qstr)QSTR_INIT(n,l)
+#define QSTR(n) QSTR_LEN(n, strlen(n))
 
 extern const struct qstr empty_name;
 extern const struct qstr slash_name;
index e3042176cdf489080933425cd4974fd8b260b02c..ba02304a2a8a0227bfb2560898ba8d22bc9063a5 100644 (file)
@@ -73,13 +73,12 @@ extern struct dentry *try_lookup_one_len(const char *, struct dentry *, int);
 extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
 extern struct dentry *lookup_one_len_unlocked(const char *, struct dentry *, int);
 extern struct dentry *lookup_positive_unlocked(const char *, struct dentry *, int);
-struct dentry *lookup_one(struct mnt_idmap *, const char *, struct dentry *, int);
+struct dentry *lookup_one(struct mnt_idmap *, struct qstr *, struct dentry *);
 struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap,
-                                  const char *name, struct dentry *base,
-                                  int len);
+                                  struct qstr *name, struct dentry *base);
 struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap,
-                                           const char *name,
-                                           struct dentry *base, int len);
+                                           struct qstr *name,
+                                           struct dentry *base);
 
 extern int follow_down_one(struct path *);
 extern int follow_down(struct path *path, unsigned int flags);