fs: add ->f_real() file operation
Subsystems (6)
Amir Goldstein
maintainerPATCH v2 1/1 fs: add ->f_real() file operation
sync_file_range(2) and cachestat(2) operate on file->f_mapping. For a file on a stacking filesystem, such as overlayfs, the data and its page cache are in the real (backing) file, so on a stacked file sync_file_range() writes back nothing and cachestat() reports nothing.
Add a ->f_real() file operation, modelled after ->d_real(), and a file_real() helper in backing-file.h that returns the backing file holding the data of a stacked file, or the file itself. file_real() is a no-op without CONFIG_FS_STACK; otherwise it calls backing_file_real() in fs/backing-file.c, which calls ->f_real() and verifies that it returned a backing file. The real file is owned by the stacked file, so it is valid for as long as the caller holds the stacked file.
file_real() is only meant for inspecting and writing back the page cache of the data. Operations that need the credentials or permission checks of the stacking filesystem need a dedicated file operation, like ->fadvise().
Use file_real() in sync_file_range() (also used by io_uring) and in cachestat(), and implement ->f_real() for overlayfs.
---
Christian,
v2 fixes the nested overlayfs case that Pavel pointed out.
Thanks,
Amir.
Changes since v1:
- ovl_f_real() recurses into a stacked lower fs (Pavel)
- Rename __file_real() to backing_file_real() and export it for ovl
- Added Pavel's tested-by and link to his new test
v1: https://lore.kernel.org/r/[email protected]/
Documentation/filesystems/locking.rst | 1 + Documentation/filesystems/vfs.rst | 17 +++++++++++++++++ fs/backing-file.c | 13 +++++++++++++ fs/overlayfs/file.c | 17 +++++++++++++++++ fs/sync.c | 8 ++++++++ include/linux/backing-file.h | 26 ++++++++++++++++++++++++++ include/linux/fs.h | 1 + mm/filemap.c | 9 ++++++++- 8 files changed, 91 insertions(+), 1 deletion(-)
Documentation/filesystems/locking.rst +1 -0
@@ -485,6 +485,7 @@ prototypes:: struct file *file_out, loff_t pos_out, loff_t len, unsigned int remap_flags); int (*fadvise)(struct file *, loff_t, loff_t, int); + struct file *(*f_real)(const struct file *); locking rules: All may block.
Documentation/filesystems/vfs.rst +17 -0
@@ -1038,6 +1038,7 @@ This describes how the VFS can manipulate an open file. As of kernel struct file *file_out, loff_t pos_out, loff_t len, unsigned int remap_flags); int (*fadvise)(struct file *, loff_t, loff_t, int); + struct file *(*f_real)(const struct file *); int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags); int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *, unsigned int poll_flags);
@@ -1162,6 +1163,22 @@ otherwise noted. ``fadvise`` possibly called by the fadvise64() system call. +``f_real`` + stacking filesystems (e.g. overlayfs) whose file data is accessed + through a backing file implement this method to return that backing + file. It is called through file_real() by system calls that operate + on the page cache of the data, such as sync_file_range(2) and + cachestat(2). + + The returned file must be a backing file (FMODE_BACKING) or the file + itself. It is owned by the file and must remain valid for as long + as the caller holds a reference to the file. + + The returned file is only used to inspect and write back the page + cache of the data. Operations that need the credentials or the + permission checks of the stacking filesystem need a dedicated file + operation (e.g. ``fadvise``). + ``mmap_prepare`` Called by the mmap(2) system call. Allows a VFS to set up a file-backed memory mapping, most notably establishing relevant
fs/backing-file.c +13 -0
@@ -102,6 +102,19 @@ struct file *backing_tmpfile_open(const struct file *user_file, int flags, }EXPORT_SYMBOL (backing_tmpfile_open); +/* Get the innermost backing file of @user_file */ +struct file *backing_file_real (struct file *user_file) +{ +struct file *real_file = user_file->f_op->f_real (user_file); + +if (!IS_ERR (real_file) && real_file != user_file && +WARN_ON_ONCE (!(real_file->f_mode &FMODE_BACKING ))) +return ERR_PTR (-EIO ); + +return real_file; +} +EXPORT_SYMBOL_GPL (backing_file_real); +struct backing_aio {struct kiocb iocb;refcount_t ref;
fs/overlayfs/file.c +17 -0
@@ -195,6 +195,22 @@ static struct file *ovl_real_file(const struct file *file)return ovl_real_file_path (file, &realpath); } +/* +* The real file that holds the page cache of the data: the upper file after +* copy up, otherwise the lower data file. After copy up of a file that was +* opened from lower, this opens the upper file, just like a write would. +*/ +static struct file *ovl_f_real (const struct file *file) +{ +struct file *real_file =ovl_real_file (file); + +if (IS_ERR (real_file)) +return real_file; + +/* Handle recursion into stacked lower fs */ +return file_real (real_file); +} +static int ovl_open (struct inode *inode,struct file *file) {struct dentry *dentry =file_dentry (file);
@@ -656,6 +672,7 @@ const struct file_operations ovl_file_operations = { .mmap = ovl_mmap, .fallocate = ovl_fallocate, .fadvise = ovl_fadvise, + .f_real = ovl_f_real, .flush = ovl_flush, .splice_read = ovl_splice_read, .splice_write = ovl_splice_write,
fs/sync.c +8 -0
@@ -7,6 +7,7 @@#include <linux/kernel.h> #include <linux/file.h> #include <linux/fs.h> +#include <linux/backing-file.h> #include <linux/slab.h> #include <linux/export.h> #include <linux/namei.h>
@@ -269,6 +270,13 @@ int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,if (!S_ISREG (i_mode) && !S_ISBLK (i_mode) && !S_ISDIR (i_mode)) goto out; +/* The page cache of a stacked file is in its real file */ + file =file_real (file); +if (IS_ERR (file)) { + ret =PTR_ERR (file); + goto out; + } + mapping = file->f_mapping; ret =0 ;if (flags &SYNC_FILE_RANGE_WAIT_BEFORE ) {
include/linux/backing-file.h +26 -0
@@ -40,5 +40,31 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe, struct backing_file_ctx *ctx);int backing_file_mmap (struct file *file,struct vm_area_struct *vma,struct backing_file_ctx *ctx); +struct file *backing_file_real (struct file *user_file); + +/** +* file_real - Return the real file hosting the data +* @file: the file to query +* +* If @file is on a stacking filesystem (e.g. overlayfs) whose data is +* accessed through a backing file, return that backing file. +* Otherwise return @file itself. +* +* The returned file is owned by @file and is valid for as long as the +* caller holds a reference to @file. +* +* The real file is only meant to be used for inspecting and writing +* back the page cache of the data (e.g. sync_file_range(2), cachestat(2)). +* It must not be used for operations that require the credentials or the +* permission checks of the stacking filesystem; those need a dedicated +* file operation (e.g. ->fadvise()). +*/ +static inline struct file *file_real (struct file *file) +{ +if (!IS_ENABLED (CONFIG_FS_STACK ) ||likely (!file->f_op->f_real)) +return file; + +return backing_file_real (file); +}#endif /* _LINUX_BACKING_FILE_H */
include/linux/fs.h +1 -0
@@ -1960,6 +1960,7 @@ struct file_operations { struct file *file_out, loff_t pos_out, loff_t len, unsigned int remap_flags);int (*fadvise)(struct file *,loff_t ,loff_t ,int ); +struct file *(*f_real)(const struct file *);int (*uring_cmd)(struct io_uring_cmd *ioucmd,unsigned int issue_flags);int (*uring_cmd_iopoll)(struct io_uring_cmd *,struct io_comp_batch *,unsigned int poll_flags);
mm/filemap.c +8 -1
@@ -31,6 +31,7 @@#include <linux/hash.h> #include <linux/writeback.h> #include <linux/backing-dev.h> +#include <linux/backing-file.h> #include <linux/folio_batch.h> #include <linux/security.h> #include <linux/cpuset.h>
@@ -4782,6 +4783,7 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd, struct cachestat __user *, cstat, unsigned int, flags) {CLASS (fd ,f )(fd ); +struct file *real_file;struct address_space *mapping;struct cachestat_range csr;struct cachestat cs;
@@ -4804,11 +4806,16 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd,if (flags !=0 )return -EINVAL ; +/* The page cache of a stacked file is in its real file */ + real_file =file_real (fd_file (f)); +if (IS_ERR (real_file)) +return PTR_ERR (real_file); + first_index = csr.off >>PAGE_SHIFT ; last_index = csr.len ==0 ?ULONG_MAX : (csr.off + csr.len -1 ) >>PAGE_SHIFT ;memset (&cs,0 ,sizeof (struct cachestat )); - mapping =fd_file (f)->f_mapping; + mapping = real_file->f_mapping;filemap_cachestat (mapping, first_index, last_index, &cs);if (copy_to_user (cstat, &cs,sizeof (struct cachestat )))
Signature · 1 line
2.56.1
Quoted 29 lines · Amir Goldstein
sync_file_range(2)and cachestat(2) operate on file->f_mapping. For a
file on a stacking filesystem, such as overlayfs, the data and its page
cache are in the real (backing) file, so on a stacked filesync_file_range()writes back nothing andcachestat()reports nothing.Add a ->f_real() file operation, modelled after ->d_real(), and a
file_real()helper inbacking-file.hthat returns the backing file holding the data of a stacked file, or the file itself.file_real()is
a no-op withoutCONFIG_FS_STACK; otherwise it callsbacking_file_real()
infs/backing-file.c, which calls ->f_real() and verifies that it returned a backing file. The real file is owned by the stacked file, so
it is valid for as long as the caller holds the stacked file.
file_real()is only meant for inspecting and writing back the page cache
of the data. Operations that need the credentials or permission checks
of the stacking filesystem need a dedicated file operation, like ->fadvise().Use
file_real()insync_file_range()(also used byio_uring) and incachestat(), and implement ->f_real() for overlayfs.
Quoted 7 lines
Tested-by Ding Feng <[email protected]>Link:
https://lore.kernel.org/r/[email protected]/Tested-by Pavel Tikhomirov <[email protected]>Link:
https://lore.kernel.org/r/[email protected]/Signed-off-by Amir Goldstein <[email protected]>
Hi Amir,
please feel free to add:
Kind regards,
Alex
Quoted 285 lines
---
Christian,
v2 fixes the nested overlayfs case that Pavel pointed out.
Thanks,
Amir.Changes since v1:
-ovl_f_real()recurses into a stacked lower fs (Pavel)
- Rename__file_real()tobacking_file_real()and export it for ovl
- Added Pavel's tested-by and link to his new testv1:
https://lore.kernel.org/r/[email protected]/Documentation/filesystems/locking.rst | 1 + Documentation/filesystems/vfs.rst | 17 +++++++++++++++++ fs/backing-file.c | 13 +++++++++++++ fs/overlayfs/file.c | 17 +++++++++++++++++ fs/sync.c | 8 ++++++++ include/linux/backing-file.h | 26
++++++++++++++++++++++++++
include/linux/fs.h | 1 + mm/filemap.c | 9 ++++++++-8 files changed, 91 insertions(+), 1 deletion(-)Documentation/filesystems/locking.rst +1
-0@@ -485,6 +485,7 @@ prototypes:: struct file *file_out, loff_t pos_out, loff_t len, unsigned int remap_flags); int (*fadvise)(struct file *, loff_t, loff_t, int); + struct file *(*f_real)(const struct file *);
locking rules:
All may block.Documentation/filesystems/vfs.rst +0
-0@@ -1038,6 +1038,7 @@ This describes how the VFS can manipulate anopen file. As of kernel
struct file*file_out,loff_tpos_out,
loff_tlen, unsigned intremap_flags);
int (*fadvise)(struct file *,loff_t,loff_t, int);Documentation/filesystems/vfs.rst +1
-0+ struct file *(*f_real)(const struct file *);int (*uring_cmd)(struct
io_uring_cmd*ioucmd, unsigned intissue_flags);
int (*uring_cmd_iopoll)(structio_uring_cmd*, structio_comp_batch*,
unsigned intpoll_flags);Documentation/filesystems/vfs.rst +0
-0@@ -1162,6 +1163,22 @@ otherwise noted.``fadvise``
possibly called by thefadvise64()system call.
+``f_real``Documentation/filesystems/vfs.rst +1
-0+ stacking filesystems (e.g. overlayfs) whose file data isaccessed
Documentation/filesystems/vfs.rst +1
-0+ through a backing file implement this method to return thatbacking
Documentation/filesystems/vfs.rst +1
-0+ file. It is called through file_real() by system calls thatoperate
Documentation/filesystems/vfs.rst +1
-0+ on the page cache of the data, such as sync_file_range(2)and
Documentation/filesystems/vfs.rst +3
-0+ cachestat(2). + + The returned file must be a backing file (FMODE_BACKING) orthe file
Documentation/filesystems/vfs.rst +1
-0+ itself. It is owned by the file and must remain valid foras long
Documentation/filesystems/vfs.rst +3
-0+ as the caller holds a reference to the file. + + The returned file is only used to inspect and write back thepage
Documentation/filesystems/vfs.rst +1
-0+ cache of the data. Operations that need the credentials orthe
Documentation/filesystems/vfs.rst +1
-0+ permission checks of the stacking filesystem need adedicated file
Documentation/filesystems/vfs.rst +2
-0+ operation (e.g. ``fadvise``). +``mmap_prepare``
Called by the mmap(2) system call. Allows a VFS to set up a file-backed memory mapping, most notably establishing relevantfs/backing-file.c +0
-0@@ -102,6 +102,19 @@ struct file *backing_tmpfile_open(const struct file *user_file, int flags, } EXPORT_SYMBOL(backing_tmpfile_open);fs/backing-file.c +13
-0+/* Get the innermost backing file of @user_file */ +struct file *backing_file_real (struct file *user_file) +{ +struct file *real_file = user_file->f_op->f_real (user_file); + +if (!IS_ERR (real_file) && real_file != user_file && + WARN_ON_ONCE(!(real_file->f_mode & FMODE_BACKING))) +return ERR_PTR (-EIO ); + +return real_file; +} +EXPORT_SYMBOL_GPL (backing_file_real); +struct
backing_aio{
struct kiocb iocb;
refcount_tref;fs/overlayfs/file.c +0
-0@@ -195,6 +195,22 @@ static struct file *ovl_real_file(const struct file *file) return ovl_real_file_path(file, &realpath); }
+/*
+ * The real file that holds the page cache of the data: the upper file after
+ * copy up, otherwise the lower data file. After copy up of a file that was
+ * opened from lower, this opens the upper file, just like a write would.
+ */fs/overlayfs/file.c +11
-0+static struct file *ovl_f_real (const struct file *file) +{ +struct file *real_file =ovl_real_file (file); + +if (IS_ERR (real_file)) +return real_file; + +/* Handle recursion into stacked lower fs */ +return file_real (real_file); +} +static int
ovl_open(struct inode *inode, struct file *file){
struct dentry *dentry =file_dentry(file);fs/overlayfs/file.c +1
-0@@ -656,6 +672,7 @@ const struct file_operations ovl_file_operations = { .mmap = ovl_mmap, .fallocate = ovl_fallocate, .fadvise = ovl_fadvise, + .f_real = ovl_f_real, .flush = ovl_flush, .splice_read = ovl_splice_read,.splice_write =
ovl_splice_write,fs/sync.c +1
-0@@ -7,6 +7,7 @@ #include <linux/kernel.h> #include <linux/file.h> #include <linux/fs.h> +#include <linux/backing-file.h> #include <linux/slab.h> #include <linux/export.h> #include <linux/namei.h>@@ -269,6 +270,13 @@ int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode))
goto out;fs/sync.c +7
-0+/* The page cache of a stacked file is in its real file */ + file =file_real (file); +if (IS_ERR (file)) { + ret =PTR_ERR (file); + goto out; + } +mapping =
file->f_mapping;
ret = 0;
if (flags &SYNC_FILE_RANGE_WAIT_BEFORE) {include/linux/backing- renamed from include/linux/backing-file.h +0
-0
file.h
indexc939cd222730c..dec7f8f017418100644include/linux/backing-file.h +7
-0@@ -40,5 +40,31 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe, struct backing_file_ctx *ctx); int backing_file_mmap(struct file *file, struct vm_area_struct *vma, struct backing_file_ctx *ctx); +struct file *backing_file_real (struct file *user_file); + +/** +* file_real - Return the real file hosting the data +* @file: the file to query +* +* If @file is on a stacking filesystem (e.g. overlayfs) whose data is
+ * accessed through a backing file, return that backing file.
+ * Otherwise return @file itself.
+ *
+ * The returned file is owned by @file and is valid for as long as the
+ * caller holds a reference to @file.
+ *
+ * The real file is only meant to be used for inspecting and writing
+ * back the page cache of the data (e.g.sync_file_range(2), cachestat(2)).
+ * It must not be used for operations that require the credentials or the
+ * permission checks of the stacking filesystem; those need a dedicated
+ * file operation (e.g. ->fadvise()).
+ */include/linux/backing-file.h +3
-0+static inline struct file *file_real(struct file *file) +{ + if (!IS_ENABLED(CONFIG_FS_STACK) || likely(!file->f_op-Quoted 1 line
f_real))include/linux/backing-file.h +4
-0+return file; + +return backing_file_real (file); +}
#endif /*_LINUX_BACKING_FILE_H*/include/linux/fs.h +0
-0@@ -1960,6 +1960,7 @@ struct file_operations {struct file
*file_out,loff_tpos_out,
loff_tlen, unsigned intremap_flags);
int (*fadvise)(struct file *,loff_t,loff_t, int);include/linux/fs.h +1
-0+struct file *(*f_real)(const struct file *);int (*uring_cmd)(struct
io_uring_cmd*ioucmd, unsigned intissue_flags);
int (*uring_cmd_iopoll)(structio_uring_cmd*, structio_comp_batch*,
unsigned intpoll_flags);mm/filemap.c +2
-0@@ -31,6 +31,7 @@ #include <linux/hash.h> #include <linux/writeback.h> #include <linux/backing-dev.h> +#include <linux/backing-file.h> #include <linux/folio_batch.h> #include <linux/security.h> #include <linux/cpuset.h>@@ -4782,6 +4783,7 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd, struct cachestat __user *, cstat, unsigned int, flags) { CLASS(fd, f)(fd); +struct file *real_file; struct address_space *mapping; struct cachestat_range csr;struct cachestat cs;
mm/filemap.c +0
-0@@ -4804,11 +4806,16 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd, if (flags != 0) return -EINVAL;mm/filemap.c +5
-0+/* The page cache of a stacked file is in its real file */ + real_file =file_real (fd_file (f)); +if (IS_ERR (real_file)) +return PTR_ERR (real_file); +
first_index= csr.off >>PAGE_SHIFT;
last_index=
csr.len == 0 ?ULONG_MAX: (csr.off + csr.len - 1)Quoted 1 line
Quoted 1 line
PAGE_SHIFT;
memset(&cs, 0, sizeof(struct cachestat));mm/filemap.c +1
-1- mapping =fd_file (f)->f_mapping; + mapping = real_file->f_mapping;
filemap_cachestat(mapping, first_index, last_index, &cs);
if (copy_to_user(cstat, &cs, sizeof(struct cachestat)))
Quoted 13 lines · Amir Goldstein
sync_file_range(2)and cachestat(2) operate onfile->f_mapping. For a file on a stacking filesystem, such as overlayfs, the data and its page cache are in the real (backing) file, so on a stacked filesync_file_range()writes back nothing andcachestat()reports nothing.Add a ->f_real() file operation, modelled after ->d_real(), and a
file_real()helper inbacking-file.hthat returns the backing file holding the data of a stacked file, or the file itself.file_real()is a no-op withoutCONFIG_FS_STACK; otherwise it callsbacking_file_real()infs/backing-file.c, which calls ->f_real() and verifies that it returned a backing file. The real file is owned by the stacked file, so it is valid for as long as the caller holds the stacked file.
Quoted 1 line
[...]
Signature · 1 line
Pavel Tikhomirov <[email protected]>
Quoted 13 lines · Amir Goldstein
sync_file_range(2)and cachestat(2) operate onfile->f_mapping. For a file on a stacking filesystem, such as overlayfs, the data and its page cache are in the real (backing) file, so on a stacked filesync_file_range()writes back nothing andcachestat()reports nothing.Add a ->f_real() file operation, modelled after ->d_real(), and a
file_real()helper inbacking-file.hthat returns the backing file holding the data of a stacked file, or the file itself.file_real()is a no-op withoutCONFIG_FS_STACK; otherwise it callsbacking_file_real()infs/backing-file.c, which calls ->f_real() and verifies that it returned a backing file. The real file is owned by the stacked file, so it is valid for as long as the caller holds the stacked file.
Quoted 1 line
[...]
Applied to the vfs-7.5.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.5.misc branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.5.misc
[1/1] fs: add ->f_real() file operation
https://git.kernel.org/vfs/vfs/c/f9b088d910e8