[PATCH v2 04/21] fuse: implement passthrough for readdir
Joanne Koong <[email protected]> Fri, 15 May 2026 17:39:47 -0700
| Newsgroups | org.kernel.vger.linux-unionfs,dev.linux.lists.fuse-devel |
|---|---|
| Message-ID | <[email protected]> |
From: Amir Goldstein <[email protected]> Requires both requesting the passthrough inode op READDIR when setting up the backing file and opening the dir with FOPEN_PASSTHROUGH. The dir opened with FOPEN_PASSTHROUGH must not request cached readdir with FOPEN_CACHE_DIR flag. Like regular files, a directory inode cannot be opened at the same time for cached readdir and readdir passthrough. A directory opened without both FOPEN_CACHE_DIR and FOPEN_PASSTHROUGH is marked as FOPEN_DIRECT_IO and does not affect io mode. Note that opt-in for passthrough of READDIR operation means that there is no READDIRPLUS call to server. For the ls -l use case, the cost of FUSE_LOOKUP to server will be paid for every entry, so passthrough of READDIR is not always a performance win. Reviewed-by: Joanne Koong <[email protected]> Signed-off-by: Amir Goldstein <[email protected]> --- fs/fuse/backing.c | 5 +++++ fs/fuse/dir.c | 7 +++++++ fs/fuse/file.c | 15 +++++++++++++-- fs/fuse/fuse_i.h | 7 ++++++- fs/fuse/iomode.c | 9 ++++++++- fs/fuse/passthrough.c | 24 ++++++++++++++++++++++++ fs/fuse/readdir.c | 3 +++ include/uapi/linux/fuse.h | 1 + 8 files changed, 67 insertions(+), 4 deletions(-) diff --git a/fs/fuse/backing.c b/fs/fuse/backing.c index 1fbb4d876eec..52fccb9ff283 100644 --- a/fs/fuse/backing.c +++ b/fs/fuse/backing.c @@ -114,6 +114,11 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map) !d_is_reg(file->f_path.dentry)) goto out_fput; + res = -ENOTDIR; + if (map->ops_mask & FUSE_PASSTHROUGH_DIR_OPS && + !d_is_dir(file->f_path.dentry)) + goto out_fput; + backing_sb = file_inode(file)->i_sb; res = -ELOOP; if (backing_sb->s_stack_depth >= fc->max_stack_depth) diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c index 937165788e79..05f69b8fa4c4 100644 --- a/fs/fuse/dir.c +++ b/fs/fuse/dir.c @@ -1891,6 +1891,7 @@ static const char *fuse_get_link(struct dentry *dentry, struct inode *inode, static int fuse_dir_open(struct inode *inode, struct file *file) { struct fuse_mount *fm = get_fuse_mount(inode); + struct fuse_inode *fi = get_fuse_inode(inode); int err; if (fuse_is_bad(inode)) @@ -1904,6 +1905,12 @@ static int fuse_dir_open(struct inode *inode, struct file *file) if (!err) { struct fuse_file *ff = file->private_data; + err = fuse_file_io_open(file, inode); + if (err) { + fuse_sync_release(fi, ff, file->f_flags, true); + return err; + } + /* * Keep handling FOPEN_STREAM and FOPEN_NONSEEKABLE for * directories for backward compatibility, though it's unlikely diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 332032cd5622..828e3fc0ec31 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -180,8 +180,19 @@ struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid, } } - if (isdir) - ff->open_flags &= ~FOPEN_DIRECT_IO; + /* + * Mark an uncached dir open as FOPEN_DIRECT_IO for fuse_file_io_open(). + * The explicit combination of FOPEN_PASSTHROUGH | FOPEN_DIRECT_IO is + * allowed and it means (similar to regular files) a request from the + * server for uncached readdir open for an inode that already has files + * open in passthrough readdir mode. + */ + if (isdir) { + if (ff->open_flags & FOPEN_CACHE_DIR) + ff->open_flags &= ~FOPEN_DIRECT_IO; + else if (!(ff->open_flags & FOPEN_PASSTHROUGH)) + ff->open_flags |= FOPEN_DIRECT_IO; + } ff->nodeid = nodeid; diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 2436219338b3..23d2510f8e93 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -1264,9 +1264,13 @@ void fuse_file_release(struct inode *inode, struct fuse_file *ff, #define FUSE_PASSTHROUGH_RW_OPS \ (FUSE_PASSTHROUGH_OP_READ | FUSE_PASSTHROUGH_OP_WRITE) +/* Passthrough operations for directories */ +#define FUSE_PASSTHROUGH_DIR_OPS \ + (FUSE_PASSTHROUGH_OP_READDIR) + /* File passthrough operations require a file opened with FOPEN_PASSTHROUGH */ #define FUSE_PASSTHROUGH_FILE_OPS \ - (FUSE_PASSTHROUGH_RW_OPS) + (FUSE_PASSTHROUGH_RW_OPS | FUSE_PASSTHROUGH_OP_READDIR) /* Inode passthrough operations for backing file attached to inode */ #define FUSE_PASSTHROUGH_INODE_OPS (0) @@ -1343,6 +1347,7 @@ ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags); ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma); +int fuse_passthrough_readdir(struct file *file, struct dir_context *ctx); static inline bool fuse_passthrough_op(struct inode *inode, enum fuse_opcode op) { diff --git a/fs/fuse/iomode.c b/fs/fuse/iomode.c index e2e7fbaac9af..c83a070cd834 100644 --- a/fs/fuse/iomode.c +++ b/fs/fuse/iomode.c @@ -201,15 +201,20 @@ static int fuse_file_passthrough_open(struct inode *inode, struct file *file) if (IS_ERR(fb)) return PTR_ERR(fb); + err = -EOPNOTSUPP; /* * Inode ops passthrough requires backing file setup at * creation/lookup time. */ - err = -EOPNOTSUPP; if (!fuse_inode_backing(fi) && (fb->ops_mask & FUSE_PASSTHROUGH_INODE_OPS)) goto fail; + /* Readdir passthrough requires opt-in on backing file setup */ + if (S_ISDIR(inode->i_mode) && + !(fb->ops_mask & FUSE_PASSTHROUGH_OP_READDIR)) + goto fail; + /* First passthrough file open denies caching inode io mode */ err = fuse_file_uncached_io_open(inode, ff, fb); if (!err) @@ -253,6 +258,8 @@ int fuse_file_io_open(struct file *file, struct inode *inode) /* * First passthrough file open denies caching inode io mode. * First caching file open enters caching inode io mode. + * A directory opened without FOPEN_CACHE_DIR is marked with + * FOPEN_DIRECT_IO and like regular file dio, does not affect io mode. * * Note that if user opens a file open with O_DIRECT, but server did * not specify FOPEN_DIRECT_IO, a later fcntl() could remove O_DIRECT, diff --git a/fs/fuse/passthrough.c b/fs/fuse/passthrough.c index 72de97c03d0e..a1d87ed51a94 100644 --- a/fs/fuse/passthrough.c +++ b/fs/fuse/passthrough.c @@ -144,6 +144,30 @@ ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma) return backing_file_mmap(backing_file, vma, &ctx); } +int fuse_passthrough_readdir(struct file *file, struct dir_context *ctx) +{ + int ret; + const struct cred *old_cred; + struct inode *inode = file_inode(file); + struct fuse_file *ff = file->private_data; + struct file *backing_file = fuse_file_passthrough(ff); + bool locked; + + pr_debug("%s: backing_file=0x%p, pos=%lld\n", __func__, + backing_file, ctx->pos); + + old_cred = override_creds(ff->cred); + locked = fuse_lock_inode(inode); + /* Respect seekdir() on fuse dir */ + vfs_llseek(backing_file, ctx->pos, SEEK_SET); + ret = iterate_dir(backing_file, ctx); + fuse_invalidate_atime(inode); + fuse_unlock_inode(inode, locked); + revert_creds(old_cred); + + return ret; +} + /* * Setup passthrough to a backing file. * diff --git a/fs/fuse/readdir.c b/fs/fuse/readdir.c index c88194e52d18..49226f022339 100644 --- a/fs/fuse/readdir.c +++ b/fs/fuse/readdir.c @@ -593,6 +593,9 @@ int fuse_readdir(struct file *file, struct dir_context *ctx) if (fuse_is_bad(inode)) return -EIO; + if (fuse_file_passthrough(ff)) + return fuse_passthrough_readdir(file, ctx); + err = UNCACHED; if (ff->open_flags & FOPEN_CACHE_DIR) err = fuse_readdir_cached(file, ctx); diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h index 0f1e1c1ec367..df366e390f0c 100644 --- a/include/uapi/linux/fuse.h +++ b/include/uapi/linux/fuse.h @@ -1142,6 +1142,7 @@ struct fuse_backing_map { /* op bits for fuse_backing_map ops_mask */ #define FUSE_PASSTHROUGH_OP_READ FUSE_PASSTHROUGH_OP(FUSE_READ) #define FUSE_PASSTHROUGH_OP_WRITE FUSE_PASSTHROUGH_OP(FUSE_WRITE) +#define FUSE_PASSTHROUGH_OP_READDIR FUSE_PASSTHROUGH_OP(FUSE_READDIR) /* Device ioctls: */ #define FUSE_DEV_IOC_MAGIC 229 -- 2.52.0