From 48b50624aec454ce0fa8f78ef96e2f43bc0be495 Mon Sep 17 00:00:00 2001 From: Miklos Szeredi Date: Mon, 21 Oct 2024 12:33:38 +0200 Subject: [PATCH] backing-file: clean up the API - Pass iocb to ctx->end_write() instead of file + pos - Get rid of ctx->user_file, which is redundant most of the time - Instead pass iocb to backing_file_splice_read and backing_file_splice_write Signed-off-by: Miklos Szeredi Signed-off-by: Amir Goldstein --- fs/backing-file.c | 35 +++++++++++++++++------------------ fs/fuse/passthrough.c | 32 ++++++++++++++++++-------------- fs/overlayfs/file.c | 22 +++++++++++++--------- include/linux/backing-file.h | 11 +++++------ 4 files changed, 53 insertions(+), 47 deletions(-) diff --git a/fs/backing-file.c b/fs/backing-file.c index 09a9be945d45..a38737592ec7 100644 --- a/fs/backing-file.c +++ b/fs/backing-file.c @@ -80,7 +80,7 @@ struct backing_aio { refcount_t ref; struct kiocb *orig_iocb; /* used for aio completion */ - void (*end_write)(struct file *, loff_t, ssize_t); + void (*end_write)(struct kiocb *iocb, ssize_t); struct work_struct work; long res; }; @@ -108,10 +108,10 @@ static void backing_aio_cleanup(struct backing_aio *aio, long res) struct kiocb *iocb = &aio->iocb; struct kiocb *orig_iocb = aio->orig_iocb; - if (aio->end_write) - aio->end_write(orig_iocb->ki_filp, iocb->ki_pos, res); - orig_iocb->ki_pos = iocb->ki_pos; + if (aio->end_write) + aio->end_write(orig_iocb, res); + backing_aio_put(aio); } @@ -200,7 +200,7 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter, revert_creds(old_cred); if (ctx->accessed) - ctx->accessed(ctx->user_file); + ctx->accessed(iocb->ki_filp); return ret; } @@ -219,7 +219,7 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter, if (!iov_iter_count(iter)) return 0; - ret = file_remove_privs(ctx->user_file); + ret = file_remove_privs(iocb->ki_filp); if (ret) return ret; @@ -239,7 +239,7 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter, ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf); if (ctx->end_write) - ctx->end_write(ctx->user_file, iocb->ki_pos, ret); + ctx->end_write(iocb, ret); } else { struct backing_aio *aio; @@ -270,7 +270,7 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter, } EXPORT_SYMBOL_GPL(backing_file_write_iter); -ssize_t backing_file_splice_read(struct file *in, loff_t *ppos, +ssize_t backing_file_splice_read(struct file *in, struct kiocb *iocb, struct pipe_inode_info *pipe, size_t len, unsigned int flags, struct backing_file_ctx *ctx) @@ -282,19 +282,19 @@ ssize_t backing_file_splice_read(struct file *in, loff_t *ppos, return -EIO; old_cred = override_creds(ctx->cred); - ret = vfs_splice_read(in, ppos, pipe, len, flags); + ret = vfs_splice_read(in, &iocb->ki_pos, pipe, len, flags); revert_creds(old_cred); if (ctx->accessed) - ctx->accessed(ctx->user_file); + ctx->accessed(iocb->ki_filp); return ret; } EXPORT_SYMBOL_GPL(backing_file_splice_read); ssize_t backing_file_splice_write(struct pipe_inode_info *pipe, - struct file *out, loff_t *ppos, size_t len, - unsigned int flags, + struct file *out, struct kiocb *iocb, + size_t len, unsigned int flags, struct backing_file_ctx *ctx) { const struct cred *old_cred; @@ -306,18 +306,18 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe, if (!out->f_op->splice_write) return -EINVAL; - ret = file_remove_privs(ctx->user_file); + ret = file_remove_privs(iocb->ki_filp); if (ret) return ret; old_cred = override_creds(ctx->cred); file_start_write(out); - ret = out->f_op->splice_write(pipe, out, ppos, len, flags); + ret = out->f_op->splice_write(pipe, out, &iocb->ki_pos, len, flags); file_end_write(out); revert_creds(old_cred); if (ctx->end_write) - ctx->end_write(ctx->user_file, ppos ? *ppos : 0, ret); + ctx->end_write(iocb, ret); return ret; } @@ -329,8 +329,7 @@ int backing_file_mmap(struct file *file, struct vm_area_struct *vma, const struct cred *old_cred; int ret; - if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)) || - WARN_ON_ONCE(ctx->user_file != vma->vm_file)) + if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING))) return -EIO; if (!file->f_op->mmap) @@ -343,7 +342,7 @@ int backing_file_mmap(struct file *file, struct vm_area_struct *vma, revert_creds(old_cred); if (ctx->accessed) - ctx->accessed(ctx->user_file); + ctx->accessed(vma->vm_file); return ret; } diff --git a/fs/fuse/passthrough.c b/fs/fuse/passthrough.c index bbac547dfcb3..607ef735ad4a 100644 --- a/fs/fuse/passthrough.c +++ b/fs/fuse/passthrough.c @@ -18,11 +18,11 @@ static void fuse_file_accessed(struct file *file) fuse_invalidate_atime(inode); } -static void fuse_passthrough_end_write(struct file *file, loff_t pos, ssize_t ret) +static void fuse_passthrough_end_write(struct kiocb *iocb, ssize_t ret) { - struct inode *inode = file_inode(file); + struct inode *inode = file_inode(iocb->ki_filp); - fuse_write_update_attr(inode, pos, ret); + fuse_write_update_attr(inode, iocb->ki_pos, ret); } ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *iter) @@ -34,7 +34,6 @@ ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *iter) ssize_t ret; struct backing_file_ctx ctx = { .cred = ff->cred, - .user_file = file, .accessed = fuse_file_accessed, }; @@ -62,7 +61,6 @@ ssize_t fuse_passthrough_write_iter(struct kiocb *iocb, ssize_t ret; struct backing_file_ctx ctx = { .cred = ff->cred, - .user_file = file, .end_write = fuse_passthrough_end_write, }; @@ -88,15 +86,20 @@ ssize_t fuse_passthrough_splice_read(struct file *in, loff_t *ppos, struct file *backing_file = fuse_file_passthrough(ff); struct backing_file_ctx ctx = { .cred = ff->cred, - .user_file = in, .accessed = fuse_file_accessed, }; + struct kiocb iocb; + ssize_t ret; pr_debug("%s: backing_file=0x%p, pos=%lld, len=%zu, flags=0x%x\n", __func__, - backing_file, ppos ? *ppos : 0, len, flags); + backing_file, *ppos, len, flags); - return backing_file_splice_read(backing_file, ppos, pipe, len, flags, - &ctx); + init_sync_kiocb(&iocb, in); + iocb.ki_pos = *ppos; + ret = backing_file_splice_read(backing_file, &iocb, pipe, len, flags, &ctx); + *ppos = iocb.ki_pos; + + return ret; } ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe, @@ -109,16 +112,18 @@ ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe, ssize_t ret; struct backing_file_ctx ctx = { .cred = ff->cred, - .user_file = out, .end_write = fuse_passthrough_end_write, }; + struct kiocb iocb; pr_debug("%s: backing_file=0x%p, pos=%lld, len=%zu, flags=0x%x\n", __func__, - backing_file, ppos ? *ppos : 0, len, flags); + backing_file, *ppos, len, flags); inode_lock(inode); - ret = backing_file_splice_write(pipe, backing_file, ppos, len, flags, - &ctx); + init_sync_kiocb(&iocb, out); + iocb.ki_pos = *ppos; + ret = backing_file_splice_write(pipe, backing_file, &iocb, len, flags, &ctx); + *ppos = iocb.ki_pos; inode_unlock(inode); return ret; @@ -130,7 +135,6 @@ ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma) struct file *backing_file = fuse_file_passthrough(ff); struct backing_file_ctx ctx = { .cred = ff->cred, - .user_file = file, .accessed = fuse_file_accessed, }; diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index 4444c78e2e0c..12c4d502ff91 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -231,9 +231,9 @@ static void ovl_file_modified(struct file *file) ovl_copyattr(file_inode(file)); } -static void ovl_file_end_write(struct file *file, loff_t pos, ssize_t ret) +static void ovl_file_end_write(struct kiocb *iocb, ssize_t ret) { - ovl_file_modified(file); + ovl_file_modified(iocb->ki_filp); } static void ovl_file_accessed(struct file *file) @@ -271,7 +271,6 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) ssize_t ret; struct backing_file_ctx ctx = { .cred = ovl_creds(file_inode(file)->i_sb), - .user_file = file, .accessed = ovl_file_accessed, }; @@ -298,7 +297,6 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) int ifl = iocb->ki_flags; struct backing_file_ctx ctx = { .cred = ovl_creds(inode->i_sb), - .user_file = file, .end_write = ovl_file_end_write, }; @@ -338,15 +336,18 @@ static ssize_t ovl_splice_read(struct file *in, loff_t *ppos, ssize_t ret; struct backing_file_ctx ctx = { .cred = ovl_creds(file_inode(in)->i_sb), - .user_file = in, .accessed = ovl_file_accessed, }; + struct kiocb iocb; ret = ovl_real_fdget(in, &real); if (ret) return ret; - ret = backing_file_splice_read(fd_file(real), ppos, pipe, len, flags, &ctx); + init_sync_kiocb(&iocb, in); + iocb.ki_pos = *ppos; + ret = backing_file_splice_read(fd_file(real), &iocb, pipe, len, flags, &ctx); + *ppos = iocb.ki_pos; fdput(real); return ret; @@ -368,9 +369,9 @@ static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, ssize_t ret; struct backing_file_ctx ctx = { .cred = ovl_creds(inode->i_sb), - .user_file = out, .end_write = ovl_file_end_write, }; + struct kiocb iocb; inode_lock(inode); /* Update mode */ @@ -380,9 +381,13 @@ static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, if (ret) goto out_unlock; - ret = backing_file_splice_write(pipe, fd_file(real), ppos, len, flags, &ctx); + init_sync_kiocb(&iocb, out); + iocb.ki_pos = *ppos; + ret = backing_file_splice_write(pipe, fd_file(real), &iocb, len, flags, &ctx); + *ppos = iocb.ki_pos; fdput(real); + out_unlock: inode_unlock(inode); @@ -420,7 +425,6 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma) struct file *realfile = file->private_data; struct backing_file_ctx ctx = { .cred = ovl_creds(file_inode(file)->i_sb), - .user_file = file, .accessed = ovl_file_accessed, }; diff --git a/include/linux/backing-file.h b/include/linux/backing-file.h index 2eed0ffb5e8f..1476a6ed1bfd 100644 --- a/include/linux/backing-file.h +++ b/include/linux/backing-file.h @@ -14,9 +14,8 @@ struct backing_file_ctx { const struct cred *cred; - struct file *user_file; - void (*accessed)(struct file *); - void (*end_write)(struct file *, loff_t, ssize_t); + void (*accessed)(struct file *file); + void (*end_write)(struct kiocb *iocb, ssize_t); }; struct file *backing_file_open(const struct path *user_path, int flags, @@ -31,13 +30,13 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter, ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter, struct kiocb *iocb, int flags, struct backing_file_ctx *ctx); -ssize_t backing_file_splice_read(struct file *in, loff_t *ppos, +ssize_t backing_file_splice_read(struct file *in, struct kiocb *iocb, struct pipe_inode_info *pipe, size_t len, unsigned int flags, struct backing_file_ctx *ctx); ssize_t backing_file_splice_write(struct pipe_inode_info *pipe, - struct file *out, loff_t *ppos, size_t len, - unsigned int flags, + struct file *out, struct kiocb *iocb, + size_t len, unsigned int flags, struct backing_file_ctx *ctx); int backing_file_mmap(struct file *file, struct vm_area_struct *vma, struct backing_file_ctx *ctx);