fuse: containerize ext4 for safer operation
Subsystems (8)
Re: [PATCH 10/31] fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
In reply to Darrick J. WongQuoted 71 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Implement the basic file mapping reporting functions like FIEMAP, BMAP, and SEEK_DATA/HOLE.
Signed-off-by "Darrick J. Wong" <[email protected]>--- fs/fuse/fuse_i.h | 8 ++++++ fs/fuse/dir.c | 1 + fs/fuse/file.c | 13 ++++++++++ fs/fuse/file_iomap.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 89 insertions(+), 1 deletion(-)fs/fuse/fuse_i.h +8
-0@@ -1730,6 +1730,11 @@ static inline bool fuse_inode_has_iomap(const struct inode *inode) return test_bit(FUSE_I_IOMAP, &fi->state); } + +int fuse_iomap_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo, +u64 start,u64 length); +loff_t fuse_iomap_lseek (struct file *file,loff_t offset,int whence); +sector_t fuse_iomap_bmap (struct address_space *mapping,sector_t block);#else # define fuse_iomap_enabled (...) (false)# define fuse_has_iomap (...) (false)@@ -1739,6 +1744,9 @@ static inline bool fuse_inode_has_iomap(const struct inode *inode)# define fuse_iomap_init_nonreg_inode (...) ((void)0)# define fuse_iomap_evict_inode (...) ((void)0)# define fuse_inode_has_iomap (...) (false) +# define fuse_iomap_fiemap NULL +# define fuse_iomap_lseek (...) (-ENOSYS) +# define fuse_iomap_bmap (...) (-ENOSYS)#endif #endif /* _FS_FUSE_I_H */ fs/fuse/dir.c +1
-0@@ -2296,6 +2296,7 @@ static const struct inode_operations fuse_common_inode_operations = { .set_acl = fuse_set_acl, .fileattr_get = fuse_fileattr_get, .fileattr_set = fuse_fileattr_set, + .fiemap = fuse_iomap_fiemap, };static const struct inode_operations fuse_symlink_inode_operations = {fs/fuse/file.c +6
-0@@ -2512,6 +2512,12 @@ static sector_t fuse_bmap(struct address_space *mapping, sector_t block)struct fuse_bmap_out outarg;int err; +if (fuse_inode_has_iomap (inode)) { +sector_t alt_sec =fuse_iomap_bmap (mapping, block); +if (alt_sec >0 ) +return alt_sec; + } +if (!inode->i_sb->s_bdev || fm->fc->no_bmap)return 0 ;
fs/fuse/file.c static loff_t fuse_lseek(struct file *file, loff_t offset, int whence) · line 2559
Quoted 7 lines
fs/fuse/file.c +4
-0@@ -2547,6 +2553,13 @@ static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)struct fuse_lseek_out outarg;int err; +if (fuse_inode_has_iomap (inode)) { +loff_t alt_pos =fuse_iomap_lseek (file, offset, whence); + +if (alt_pos >=0 || (alt_pos <0 && alt_pos != -ENOSYS ))
I don't think you technically need the "alt_pos < 0" part here since the "alt_pos >= 0 ||" part already accounts for that
Quoted 36 lines
fs/fuse/file.c +3
-0+return alt_pos; + } +if (fm->fc->no_lseek) goto fallback;fs/fuse/file_iomap.c +7
-1@@ -4,6 +4,7 @@* Author: Darrick J. Wong <[email protected]> */ #include <linux/iomap.h> +#include <linux/fiemap.h> #include "fuse_i.h" #include "fuse_trace.h" #include "iomap_i.h" @@ -561,7 +562,7 @@ static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count,return err; } -const struct iomap_ops fuse_iomap_ops = { +static const struct iomap_ops fuse_iomap_ops = { .iomap_begin = fuse_iomap_begin, .iomap_end = fuse_iomap_end, };@@ -690,3 +691,68 @@ void fuse_iomap_evict_inode(struct inode *inode)if (conn->iomap &&fuse_inode_is_exclusive (inode))clear_bit (FUSE_I_EXCLUSIVE , &fi->state); } + +int fuse_iomap_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo, +u64 start,u64 count) +{ +struct fuse_conn *fc =get_fuse_conn (inode);
fs/fuse/file_iomap.c void fuse_iomap_evict_inode(struct inode *inode) · line 708
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+int error; + +/* +* We are called directly from the vfs so we need to check per-inode +* support here explicitly. +*/ +if (!fuse_inode_has_iomap (inode)) +return -EOPNOTSUPP ; + +if (fieinfo->fi_flags &FIEMAP_FLAG_XATTR )
I don't see where FIEMAP_FLAG_SYNC and FIEMAP_FLAG_CACHE are supported either, should these return -EOPNOTSUPP if they're set as well?
Quoted 33 lines
fs/fuse/file_iomap.c +33
-0+return -EOPNOTSUPP ; + +if (fuse_is_bad (inode)) +return -EIO ; + +if (!fuse_allow_current_process (fc)) +return -EACCES ; + +inode_lock_shared (inode); + error =iomap_fiemap (inode, fieinfo, start, count, &fuse_iomap_ops); +inode_unlock_shared (inode); + +return error; +} + +sector_t fuse_iomap_bmap (struct address_space *mapping,sector_t block) +{ +ASSERT (fuse_inode_has_iomap (mapping->host)); + +return iomap_bmap (mapping, block, &fuse_iomap_ops); +} + +loff_t fuse_iomap_lseek (struct file *file,loff_t offset,int whence) +{ +struct inode *inode = file->f_mapping->host; +struct fuse_conn *fc =get_fuse_conn (inode); + +ASSERT (fuse_inode_has_iomap (inode)); + +if (fuse_is_bad (inode)) +return -EIO ; + +if (!fuse_allow_current_process (fc))
fs/fuse/file_iomap.c void fuse_iomap_evict_inode(struct inode *inode) · line 751
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+return -EACCES ; + +switch (whence) { +case SEEK_HOLE : + offset =iomap_seek_hole (inode, offset, &fuse_iomap_ops); +break ; +case SEEK_DATA : + offset =iomap_seek_data (inode, offset, &fuse_iomap_ops); +break ; +default :
Does it make sense to have the default case just call generic_file_llseek()?
Thanks,
Joanne
Quoted 8 lines
fs/fuse/file_iomap.c +7
-0+return -ENOSYS ; + } + +if (offset <0 ) +return offset; +return vfs_setpos (file, offset, inode->i_sb->s_maxbytes); +}
Re: [PATCH 10/31] fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
Quoted 75 lines · Joanne Koong
Quoted 74 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Implement the basic file mapping reporting functions like FIEMAP, BMAP, and SEEK_DATA/HOLE.
Signed-off-by "Darrick J. Wong" <[email protected]>--- fs/fuse/fuse_i.h | 8 ++++++ fs/fuse/dir.c | 1 + fs/fuse/file.c | 13 ++++++++++ fs/fuse/file_iomap.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 89 insertions(+), 1 deletion(-)fs/fuse/fuse_i.h +8
-0@@ -1730,6 +1730,11 @@ static inline bool fuse_inode_has_iomap(const struct inode *inode) return test_bit(FUSE_I_IOMAP, &fi->state); } + +int fuse_iomap_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo, +u64 start,u64 length); +loff_t fuse_iomap_lseek (struct file *file,loff_t offset,int whence); +sector_t fuse_iomap_bmap (struct address_space *mapping,sector_t block);#else # define fuse_iomap_enabled (...) (false)# define fuse_has_iomap (...) (false)@@ -1739,6 +1744,9 @@ static inline bool fuse_inode_has_iomap(const struct inode *inode)# define fuse_iomap_init_nonreg_inode (...) ((void)0)# define fuse_iomap_evict_inode (...) ((void)0)# define fuse_inode_has_iomap (...) (false) +# define fuse_iomap_fiemap NULL +# define fuse_iomap_lseek (...) (-ENOSYS) +# define fuse_iomap_bmap (...) (-ENOSYS)#endif #endif /* _FS_FUSE_I_H */ fs/fuse/dir.c +1
-0@@ -2296,6 +2296,7 @@ static const struct inode_operations fuse_common_inode_operations = { .set_acl = fuse_set_acl, .fileattr_get = fuse_fileattr_get, .fileattr_set = fuse_fileattr_set, + .fiemap = fuse_iomap_fiemap, };static const struct inode_operations fuse_symlink_inode_operations = {fs/fuse/file.c +6
-0@@ -2512,6 +2512,12 @@ static sector_t fuse_bmap(struct address_space *mapping, sector_t block)struct fuse_bmap_out outarg;int err; +if (fuse_inode_has_iomap (inode)) { +sector_t alt_sec =fuse_iomap_bmap (mapping, block); +if (alt_sec >0 ) +return alt_sec; + } +if (!inode->i_sb->s_bdev || fm->fc->no_bmap)return 0 ;@@ -2547,6 +2553,13 @@ static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)struct fuse_lseek_out outarg;int err;
Quoted 7 lines
fs/fuse/file.c static loff_t fuse_lseek(struct file *file, loff_t offset, int whence) · line 2559
Quoted 4 lines
fs/fuse/file.c +4
-0+if (fuse_inode_has_iomap (inode)) { +loff_t alt_pos =fuse_iomap_lseek (file, offset, whence); + +if (alt_pos >=0 || (alt_pos <0 && alt_pos != -ENOSYS ))I don't think you technically need the "
alt_pos< 0" part here since the "alt_pos>= 0 ||" part already accounts for that
alt_pos is loff_t, which is a signed type.
But I think this could be more concise:
alt_pos = fuse_iomap_lseek(...); if (alt_pos != -ENOSYS) return alt_pos;
Quoted 41 lines
Quoted 41 lines
fs/fuse/file.c +3
-0+return alt_pos; + } +if (fm->fc->no_lseek) goto fallback;fs/fuse/file_iomap.c +12
-1@@ -4,6 +4,7 @@* Author: Darrick J. Wong <[email protected]> */ #include <linux/iomap.h> +#include <linux/fiemap.h> #include "fuse_i.h" #include "fuse_trace.h" #include "iomap_i.h" @@ -561,7 +562,7 @@ static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count,return err; } -const struct iomap_ops fuse_iomap_ops = { +static const struct iomap_ops fuse_iomap_ops = { .iomap_begin = fuse_iomap_begin, .iomap_end = fuse_iomap_end, };@@ -690,3 +691,68 @@ void fuse_iomap_evict_inode(struct inode *inode)if (conn->iomap &&fuse_inode_is_exclusive (inode))clear_bit (FUSE_I_EXCLUSIVE , &fi->state); } + +int fuse_iomap_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo, +u64 start,u64 count) +{ +struct fuse_conn *fc =get_fuse_conn (inode); +int error; + +/* +* We are called directly from the vfs so we need to check per-inode +* support here explicitly.
Quoted 8 lines
fs/fuse/file_iomap.c void fuse_iomap_evict_inode(struct inode *inode) · line 708
Quoted 5 lines
fs/fuse/file_iomap.c +5
-0+*/ +if (!fuse_inode_has_iomap (inode)) +return -EOPNOTSUPP ; + +if (fieinfo->fi_flags &FIEMAP_FLAG_XATTR )I don't see where
FIEMAP_FLAG_SYNCandFIEMAP_FLAG_CACHEare supported either, should these return-EOPNOTSUPPif they're set as well?
The vfs implements FIEMAP_FLAG_SYNC for us in fiemap_prep, which is called by iomap_fiemap.
I'm not sure what FIEMAP_FLAG_CACHE means in this context. Its comment says "request caching of the extents" which doesn't sound like doing anything is mandatory.
Quoted 37 lines
Quoted 37 lines
fs/fuse/file_iomap.c +37
-0+return -EOPNOTSUPP ; + +if (fuse_is_bad (inode)) +return -EIO ; + +if (!fuse_allow_current_process (fc)) +return -EACCES ; + +inode_lock_shared (inode); + error =iomap_fiemap (inode, fieinfo, start, count, &fuse_iomap_ops); +inode_unlock_shared (inode); + +return error; +} + +sector_t fuse_iomap_bmap (struct address_space *mapping,sector_t block) +{ +ASSERT (fuse_inode_has_iomap (mapping->host)); + +return iomap_bmap (mapping, block, &fuse_iomap_ops); +} + +loff_t fuse_iomap_lseek (struct file *file,loff_t offset,int whence) +{ +struct inode *inode = file->f_mapping->host; +struct fuse_conn *fc =get_fuse_conn (inode); + +ASSERT (fuse_inode_has_iomap (inode)); + +if (fuse_is_bad (inode)) +return -EIO ; + +if (!fuse_allow_current_process (fc)) +return -EACCES ; + +switch (whence) { +case SEEK_HOLE :
Quoted 8 lines
fs/fuse/file_iomap.c void fuse_iomap_evict_inode(struct inode *inode) · line 751
Quoted 6 lines
fs/fuse/file_iomap.c +6
-0+ offset =iomap_seek_hole (inode, offset, &fuse_iomap_ops); +break ; +case SEEK_DATA : + offset =iomap_seek_data (inode, offset, &fuse_iomap_ops); +break ; +default :Does it make sense to have the default case just call
generic_file_llseek()?
Yes. Thanks for spotting that bug!
--D
Quoted 12 lines
Thanks,
JoanneQuoted 8 lines
fs/fuse/file_iomap.c +7
-0+return -ENOSYS ; + } + +if (offset <0 ) +return offset; +return vfs_setpos (file, offset, inode->i_sb->s_maxbytes); +}
PATCH 11/31 fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/file_iomap.c | 4 ++++ 2 files changed, 50 insertions(+)
fs/fuse/fuse_trace.h +46 -0
@@ -532,6 +532,52 @@ DEFINE_EVENT(fuse_inode_state_class, name, \ TP_ARGS(inode))DEFINE_FUSE_INODE_STATE_EVENT (fuse_iomap_init_inode);DEFINE_FUSE_INODE_STATE_EVENT (fuse_iomap_evict_inode); + +TRACE_EVENT (fuse_iomap_fiemap, + TP_PROTO(const struct inode *inode, u64 start, u64 count, + unsigned int flags), + +TP_ARGS (inode, start, count, flags), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + __field(unsigned int, flags) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = start; + __entry->length = count; + __entry->flags = flags; + ), + + TP_printk(FUSE_IO_RANGE_FMT("fiemap") " flags 0x%x", +FUSE_IO_RANGE_PRINTK_ARGS (), + __entry->flags) +); + +TRACE_EVENT (fuse_iomap_lseek, + TP_PROTO(const struct inode *inode, loff_t offset, int whence), + +TP_ARGS (inode, offset, whence), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS + __field(loff_t, offset) +__field (int, whence) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = offset; + __entry->whence = whence; + ), + +TP_printk (FUSE_INODE_FMT " offset 0x%llx whence %d" , +FUSE_INODE_PRINTK_ARGS , + __entry->offset, + __entry->whence) +);#endif /* CONFIG_FUSE_IOMAP */ #endif /* _TRACE_FUSE_H */
fs/fuse/file_iomap.c +4 -0
@@ -714,6 +714,8 @@ int fuse_iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,if (!fuse_allow_current_process (fc))return -EACCES ; +trace_fuse_iomap_fiemap (inode, start, count, fieinfo->fi_flags); +inode_lock_shared (inode); error =iomap_fiemap (inode, fieinfo, start, count, &fuse_iomap_ops);inode_unlock_shared (inode);
@@ -741,6 +743,8 @@ loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence)if (!fuse_allow_current_process (fc))return -EACCES ; +trace_fuse_iomap_lseek (inode, offset, whence); +switch (whence) {case SEEK_HOLE : offset =iomap_seek_hole (inode, offset, &fuse_iomap_ops);
PATCH 12/31 fuse: implement direct IO with iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.
--- fs/fuse/fuse_i.h | 30 +++++ include/uapi/linux/fuse.h | 22 ++++ fs/fuse/dir.c | 7 + fs/fuse/file.c | 16 +++ fs/fuse/file_iomap.c | 249 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/trace.c | 1 6 files changed, 323 insertions(+), 2 deletions(-)
fs/fuse/fuse_i.h +30 -0
@@ -649,6 +649,16 @@ struct fuse_sync_bucket {struct rcu_head rcu; }; +#ifdef CONFIG_FUSE_IOMAP +struct fuse_iomap_conn { +/* fuse server doesn't implement iomap_end */ +unsigned int no_end:1 ; + +/* fuse server doesn't implement iomap_ioend */ +unsigned int no_ioend:1 ; +}; +#endif +/** * A Fuse connection. *
@@ -998,6 +1008,11 @@ struct fuse_conn {struct idr backing_files_map;#endif +#ifdef CONFIG_FUSE_IOMAP +/** iomap information */ +struct fuse_iomap_conn iomap_conn; +#endif +#ifdef CONFIG_FUSE_IO_URING /** uring connection information*/ struct fuse_ring *ring;
@@ -1735,6 +1750,17 @@ int fuse_iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, u64 start, u64 length);loff_t fuse_iomap_lseek (struct file *file,loff_t offset,int whence);sector_t fuse_iomap_bmap (struct address_space *mapping,sector_t block); + +void fuse_iomap_open (struct inode *inode,struct file *file); + +static inline bool fuse_want_iomap_directio (const struct kiocb *iocb) +{ +return (iocb->ki_flags &IOCB_DIRECT ) && +fuse_inode_has_iomap (file_inode (iocb->ki_filp)); +} + +ssize_t fuse_iomap_direct_read (struct kiocb *iocb,struct iov_iter *to); +ssize_t fuse_iomap_direct_write (struct kiocb *iocb,struct iov_iter *from);#else # define fuse_iomap_enabled (...) (false)# define fuse_has_iomap (...) (false)
@@ -1747,6 +1773,10 @@ sector_t fuse_iomap_bmap(struct address_space *mapping, sector_t block);# define fuse_iomap_fiemap NULL# define fuse_iomap_lseek (...) (-ENOSYS)# define fuse_iomap_bmap (...) (-ENOSYS) +# define fuse_iomap_open (...) ((void)0) +# define fuse_want_iomap_directio (...) (false) +# define fuse_iomap_direct_read (...) (-ENOSYS) +# define fuse_iomap_direct_write (...) (-ENOSYS)#endif #endif /* _FS_FUSE_I_H */
include/uapi/linux/fuse.h +22 -0
@@ -672,6 +672,7 @@ enum fuse_opcode {FUSE_STATX =52 ,FUSE_COPY_FILE_RANGE_64 =53 , +FUSE_IOMAP_IOEND =4093 ,FUSE_IOMAP_BEGIN =4094 , FUSE_IOMAP_END = 4095,
@@ -1406,4 +1407,25 @@ struct fuse_iomap_end_in { struct fuse_iomap_io map; }; +/* out of place write extent */ +#define FUSE_IOMAP_IOEND_SHARED (1U << 0) +/* unwritten extent */ +#define FUSE_IOMAP_IOEND_UNWRITTEN (1U << 1) +/* don't merge into previous ioend */ +#define FUSE_IOMAP_IOEND_BOUNDARY (1U << 2) +/* is direct I/O */ +#define FUSE_IOMAP_IOEND_DIRECT (1U << 3) +/* is append ioend */ +#define FUSE_IOMAP_IOEND_APPEND (1U << 4) + +struct fuse_iomap_ioend_in { +uint32_t ioendflags;/* FUSE_IOMAP_IOEND_* */ +int32_t error;/* negative errno or 0 */ +uint64_t attr_ino;/* matches fuse_attr:ino */ +uint64_t pos;/* file position, in bytes */ +uint64_t new_addr;/* disk offset of new mapping, in bytes */ +uint32_t written;/* bytes processed */ +uint32_t reserved1;/* zero */ +}; +#endif /* _LINUX_FUSE_H */
fs/fuse/dir.c +7 -0
@@ -712,6 +712,10 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,if (err) goto out_acl_release;fuse_dir_changed (dir); + +if (fuse_inode_has_iomap (inode)) +fuse_iomap_open (inode, file); + err =generic_file_open (inode, file);if (!err) { file->private_data = ff;
@@ -1743,6 +1747,9 @@ static int fuse_dir_open(struct inode *inode, struct file *file)if (fuse_is_bad (inode))return -EIO ; +if (fuse_inode_has_iomap (inode)) +fuse_iomap_open (inode, file); + err =generic_file_open (inode, file);if (err)return err;
fs/fuse/file.c +16 -0
@@ -246,6 +246,9 @@ static int fuse_open(struct inode *inode, struct file *file)if (fuse_is_bad (inode))return -EIO ; +if (is_iomap) +fuse_iomap_open (inode, file); + err =generic_file_open (inode, file);if (err)return err;
@@ -1751,10 +1754,17 @@ static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)struct file *file = iocb->ki_filp;struct fuse_file *ff = file->private_data;struct inode *inode =file_inode (file); +ssize_t ret;if (fuse_is_bad (inode))return -EIO ; +if (fuse_want_iomap_directio (iocb)) { + ret =fuse_iomap_direct_read (iocb, to); +if (ret != -ENOSYS ) +return ret; + } +if (FUSE_IS_DAX (inode))return fuse_dax_read_iter (iocb, to);
@@ -1776,6 +1786,12 @@ static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)if (fuse_is_bad (inode))return -EIO ; +if (fuse_want_iomap_directio (iocb)) { +ssize_t ret =fuse_iomap_direct_write (iocb, from); +if (ret != -ENOSYS ) +return ret; + } +if (FUSE_IS_DAX (inode))return fuse_dax_write_iter (iocb, from);
fs/fuse/file_iomap.c +247 -2
285 lines not on this page · raw message
fs/fuse/trace.c +1 -0
@@ -9,6 +9,7 @@#include "iomap_i.h" #include <linux/pagemap.h> +#include <linux/iomap.h> #define CREATE_TRACE_POINTS #include "fuse_trace.h"
Re: [PATCH 12/31] fuse: implement direct IO with iomap
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.
Overall, this makes sense to me. Left a few comments below.
Quoted 31 lines
Signed-off-by "Darrick J. Wong" <[email protected]>--- fs/fuse/fuse_i.h | 30 +++++ include/uapi/linux/fuse.h | 22 ++++ fs/fuse/dir.c | 7 + fs/fuse/file.c | 16 +++ fs/fuse/file_iomap.c | 249 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/trace.c | 1 6 files changed, 323 insertions(+), 2 deletions(-)include/uapi/linux/fuse.h +4
-0@@ -672,6 +672,7 @@ enum fuse_opcode {FUSE_STATX =52 ,FUSE_COPY_FILE_RANGE_64 =53 , +FUSE_IOMAP_IOEND =4093 ,FUSE_IOMAP_BEGIN =4094 , FUSE_IOMAP_END = 4095,@@ -1406,4 +1407,25 @@ struct fuse_iomap_end_in {struct fuse_iomap_io map; }; +/* out of place write extent */ +#define FUSE_IOMAP_IOEND_SHARED (1U << 0) +/* unwritten extent */
include/uapi/linux/fuse.h struct fuse_iomap_end_in { · line 1422
Quoted 10 lines
include/uapi/linux/fuse.h +10
-0+#define FUSE_IOMAP_IOEND_UNWRITTEN (1U << 1) +/* don't merge into previous ioend */ +#define FUSE_IOMAP_IOEND_BOUNDARY (1U << 2) +/* is direct I/O */ +#define FUSE_IOMAP_IOEND_DIRECT (1U << 3) +/* is append ioend */ +#define FUSE_IOMAP_IOEND_APPEND (1U << 4) + +struct fuse_iomap_ioend_in { +uint32_t ioendflags;/* FUSE_IOMAP_IOEND_* */
Hmm, maybe just "flags" is descriptive enough? Or if not, then "ioend_flags"?
include/uapi/linux/fuse.h struct fuse_iomap_end_in { · line 1427
Quoted 5 lines
include/uapi/linux/fuse.h +5
-0+int32_t error;/* negative errno or 0 */ +uint64_t attr_ino;/* matches fuse_attr:ino */ +uint64_t pos;/* file position, in bytes */ +uint64_t new_addr;/* disk offset of new mapping, in bytes */ +uint32_t written;/* bytes processed */
Is uint32_t enough here or does it need to be bigger? Asking mostly because I see in fuse_iomap_ioend() that the written passed in is size_t.
Quoted 34 lines
include/uapi/linux/fuse.h +3
-0+ uint32_t reserved1; /* zero */ +}; +#endif /* _LINUX_FUSE_H */ fs/fuse/dir.c +7
-0@@ -712,6 +712,10 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,if (err) goto out_acl_release;fuse_dir_changed (dir); + +if (fuse_inode_has_iomap (inode)) +fuse_iomap_open (inode, file); + err =generic_file_open (inode, file);if (!err) { file->private_data = ff;@@ -1743,6 +1747,9 @@ static int fuse_dir_open(struct inode *inode, struct file *file)if (fuse_is_bad (inode))return -EIO ; +if (fuse_inode_has_iomap (inode)) +fuse_iomap_open (inode, file); + err =generic_file_open (inode, file);if (err)return err;fs/fuse/file.c +0
-0
fs/fuse/file.c static int fuse_open(struct inode *inode, struct file *file) · line 251
Quoted 6 lines
fs/fuse/file.c +3
-0@@ -246,6 +246,9 @@ static int fuse_open(struct inode *inode, struct file *file)if (fuse_is_bad (inode))return -EIO ; +if (is_iomap) +fuse_iomap_open (inode, file); +
AFAICT, there aren't any calls to generic_file_open() where we don't also do this "if (is_iomap) ..." check, so maybe we should just put this logic inside generic_file_open()?
fs/fuse/file.c static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 1762
Quoted 13 lines
fs/fuse/file.c +2
-0err =generic_file_open (inode, file);if (err)return err;@@ -1751,10 +1754,17 @@ static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)struct file *file = iocb->ki_filp;struct fuse_file *ff = file->private_data;struct inode *inode =file_inode (file); +ssize_t ret;if (fuse_is_bad (inode))return -EIO ; +if (fuse_want_iomap_directio (iocb)) {
In fuse, directio is also done if the server sets FOPEN_DIRECT_IO as part of the struct fuse_open_out open_flags arg, even if iocb->ki_flags doesn't have IOCB_DIRECT set.
fs/fuse/file.c static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 1764
Quoted 2 lines
fs/fuse/file.c +2
-0+ ret =fuse_iomap_direct_read (iocb, to); +if (ret != -ENOSYS )
Hmm, where does fuse_iomap_direct_read() return -ENOSYS? afaict, neither fuse_iomap_ilock_iocb() nor iomap_dio_rw() do?
fs/fuse/file.c static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 1766
Quoted 2 lines
fs/fuse/file.c +2
-0+return ret; + }
I see that later on, in the patch that adds the implementation for buffered IO with iomap, this logic later becomes something like
if (fuse_want_iomap_directio(iocb)) {
...
}
if (fuse_want_iomap_buffered_io(iocb))
return fuse_iomap_buffered_read(iocb, to);imo (if -ENOSYS is indeed not possible) something like this is maybe cleaner:
if (fuse_inode_has_iomap(inode))
fuse_iomap_read_iter(iocb, to);to move as much iomap-specific logic away from generic fuse files?
And then I think this would also let us get rid of the fuse_want_iomap_directio()/fuse_want_iomap_buffered_io() helpers, eg:
ssize_t fuse_iomap_read_iter(struct kiocb *iocb, struct iov_iter *to) {
if (iocb->ki_flags & IOCB_DIRECT)
return fuse_iomap_direct_read(iocb, to);
return fuse_iomap_buffered_read(iocb, to);
}fs/fuse/file.c static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from) · line 1793
Quoted 13 lines
fs/fuse/file.c +6
-0+if (FUSE_IS_DAX (inode))return fuse_dax_read_iter (iocb, to);@@ -1776,6 +1786,12 @@ static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)if (fuse_is_bad (inode))return -EIO ; +if (fuse_want_iomap_directio (iocb)) { +ssize_t ret =fuse_iomap_direct_write (iocb, from); +if (ret != -ENOSYS ) +return ret; + }
Same questions as above about -ENOSYS
Quoted 99 lines
fs/fuse/file.c +1
-0+if (FUSE_IS_DAX (inode))return fuse_dax_write_iter (iocb, from);fs/fuse/file_iomap.c +61
-2@@ -495,10 +495,15 @@ static int fuse_iomap_begin(struct inode *inode, loff_t pos, loff_t count, }/* Decide if we send FUSE_IOMAP_END to the fuse server */ -static bool fuse_should_send_iomap_end (const struct iomap *iomap, +static bool fuse_should_send_iomap_end (const struct fuse_mount *fm, +const struct iomap *iomap,unsigned int opflags,loff_t count,ssize_t written) { +/* Not implemented on fuse server */ +if (fm->fc->iomap_conn.no_end) +return false; +/* fuse server demanded an iomap_end call. */ if (iomap->flags &FUSE_IOMAP_F_WANT_IOMAP_END )return true;@@ -523,7 +528,7 @@ static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count,struct fuse_mount *fm =get_fuse_mount (inode);int err =0 ; -if (fuse_should_send_iomap_end (iomap, opflags, count, written)) { +if (fuse_should_send_iomap_end (fm, iomap, opflags, count, written)) {struct fuse_iomap_end_in inarg = { .opflags =fuse_iomap_op_to_server (opflags), .attr_ino = fi->orig_ino,@@ -549,6 +554,7 @@ static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count,* libfuse returns ENOSYS for servers that don't * implement iomap_end */ + fm->fc->iomap_conn.no_end =1 ; err =0 ;break ;case 0 :@@ -567,6 +573,95 @@ static const struct iomap_ops fuse_iomap_ops = { .iomap_end = fuse_iomap_end, }; +static inline bool +fuse_should_send_iomap_ioend (const struct fuse_mount *fm, +const struct fuse_iomap_ioend_in *inarg) +{ +/* Not implemented on fuse server */ +if (fm->fc->iomap_conn.no_ioend) +return false; + +/* Always send an ioend for errors. */ +if (inarg->error) +return true; + +/* Send an ioend if we performed an IO involving metadata changes. */ +return inarg->written >0 && + (inarg->ioendflags & (FUSE_IOMAP_IOEND_SHARED | +FUSE_IOMAP_IOEND_UNWRITTEN | +FUSE_IOMAP_IOEND_APPEND )); +} + +/* +* Fast and loose check if this write could update the on-disk inode size. +*/ +static inline bool fuse_ioend_is_append (const struct fuse_inode *fi, +loff_t pos,size_t written) +{ +return pos + written >i_size_read (&fi->inode); +} + +static int fuse_iomap_ioend (struct inode *inode,loff_t pos,size_t written, +int error,unsigned ioendflags,sector_t new_addr) +{ +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_mount *fm =get_fuse_mount (inode); +struct fuse_iomap_ioend_in inarg = { + .ioendflags = ioendflags, + .error = error, + .attr_ino = fi->orig_ino, + .pos = pos, + .written = written, + .new_addr = new_addr, + }; + +if (fuse_ioend_is_append (fi, pos, written)) + inarg.ioendflags |=FUSE_IOMAP_IOEND_APPEND ; + +if (fuse_should_send_iomap_ioend (fm, &inarg)) { +FUSE_ARGS (args); +int err; + + args.opcode =FUSE_IOMAP_IOEND ; + args.nodeid =get_node_id (inode); + args.in_numargs =1 ; + args.in_args[0 ].size =sizeof (inarg);
fs/fuse/file_iomap.c static const struct iomap_ops fuse_iomap_ops = { · line 638
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+ args.in_args[0 ].value = &inarg; + err =fuse_simple_request (fm, &args); +switch (err) { +case -ENOSYS : +/* +* fuse servers can return ENOSYS if ioend processing +* is never needed for this filesystem. +*/ + fm->fc->iomap_conn.no_ioend =1 ; + err =0 ;
It doesn't look like we need to set err here or maybe I'm missing something
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+break ; +case 0 : +break ; +default : +/* +* If the write IO failed, return the failure code to +* the caller no matter what happens with the ioend. +* If the write IO succeeded but the ioend did not, +* pass the new error up to the caller. +*/
fs/fuse/file_iomap.c static const struct iomap_ops fuse_iomap_ops = { · line 658
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+if (!error) + error = err; +break ; + } + } +if (error) +return error; + +/* +* If there weren't any ioend errors, update the incore isize, which
Not sure if incore is a standard term, but it had me confused for a bit. I think incore just means kernel-internal?
Quoted 10 lines
fs/fuse/file_iomap.c +6
-0+* confusingly takes the new i_size as "pos". +*/ +fuse_write_update_attr (inode, pos + written, written); +return 0 ; +} +static int fuse_iomap_may_admin (struct fuse_conn *fc,unsigned int flags) {if (!fc->iomap)
fs/fuse/file_iomap.c void fuse_iomap_mount(struct fuse_mount *fm) · line 717
Quoted 5 lines
fs/fuse/file_iomap.c +2
-0@@ -618,6 +713,8 @@ void fuse_iomap_mount(struct fuse_mount *fm)* freeze/thaw properly. */ fc->sync_fs = true; + fc->iomap_conn.no_end =0 ; + fc->iomap_conn.no_ioend =0 ;
fc after it's first allocated has all its fields memset to 0
Quoted 26 lines
fs/fuse/file_iomap.c +19
-0}void fuse_iomap_unmount (struct fuse_mount *fm)@@ -760,3 +857,151 @@ loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence)return offset;return vfs_setpos (file, offset, inode->i_sb->s_maxbytes); } + +void fuse_iomap_open (struct inode *inode,struct file *file) +{ +ASSERT (fuse_inode_has_iomap (inode)); + + file->f_mode |=FMODE_NOWAIT |FMODE_CAN_ODIRECT ; +} + +enum fuse_ilock_type { +SHARED , +EXCL , +}; + +static int fuse_iomap_ilock_iocb (const struct kiocb *iocb, +enum fuse_ilock_type type) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (iocb->ki_flags &IOCB_NOWAIT ) {
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 888
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+switch (type) { +case SHARED : +return inode_trylock_shared (inode) ?0 : -EAGAIN ; +case EXCL : +return inode_trylock (inode) ?0 : -EAGAIN ; +default : +ASSERT (0 ); +return -EIO ; + } + }else {
nit: the else {} scoping doesn't seem needed here
Quoted 17 lines
fs/fuse/file_iomap.c +17
-0+switch (type) { +case SHARED : +inode_lock_shared (inode); +break ; +case EXCL : +inode_lock (inode); +break ; +default : +ASSERT (0 ); +return -EIO ; + } + } + +return 0 ; +} + +ssize_t fuse_iomap_direct_read (struct kiocb *iocb,struct iov_iter *to)
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 915
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+{ +struct inode *inode =file_inode (iocb->ki_filp); +ssize_t ret; + +ASSERT (fuse_inode_has_iomap (inode)); + +if (!iov_iter_count (to)) +return 0 ;/* skip atime */ + +file_accessed (iocb->ki_filp);
Does it make sense for this to be moved below so it's called only if fuse_iomap_ilock_iocb() succeeded?
Quoted 24 lines
fs/fuse/file_iomap.c +24
-0+ + ret =fuse_iomap_ilock_iocb (iocb,SHARED ); +if (ret) +return ret; + ret =iomap_dio_rw (iocb, to, &fuse_iomap_ops,NULL ,0 ,NULL ,0 ); +inode_unlock_shared (inode); + +return ret; +} + +static int fuse_iomap_dio_write_end_io (struct kiocb *iocb,ssize_t written, +int error,unsigned dioflags) +{ +struct inode *inode =file_inode (iocb->ki_filp); +unsigned int nofs_flag; +unsigned int ioendflags =FUSE_IOMAP_IOEND_DIRECT ; +int ret; + +if (fuse_is_bad (inode)) +return -EIO ; + +ASSERT (fuse_inode_has_iomap (inode)); + +if (dioflags &IOMAP_DIO_COW )
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 949
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+ ioendflags |=FUSE_IOMAP_IOEND_SHARED ; +if (dioflags &IOMAP_DIO_UNWRITTEN ) + ioendflags |=FUSE_IOMAP_IOEND_UNWRITTEN ; + +/* +* We can allocate memory here while doing writeback on behalf of +* memory reclaim. To avoid memory allocation deadlocks set the +* task-wide nofs context for the following operations. +*/ + nofs_flag =memalloc_nofs_save ();
I'm a bit confused by this part. Could you explain how it's invoked while doing writeback for memory reclaim? As I understand it, writeback goes through buffered io and not direct io?
Quoted 25 lines
fs/fuse/file_iomap.c +25
-0+ ret =fuse_iomap_ioend (inode, iocb->ki_pos, written, error, ioendflags, +FUSE_IOMAP_NULL_ADDR ); +memalloc_nofs_restore (nofs_flag); +return ret; +} + +static const struct iomap_dio_ops fuse_iomap_dio_write_ops = { + .end_io = fuse_iomap_dio_write_end_io, +}; + +ssize_t fuse_iomap_direct_write (struct kiocb *iocb,struct iov_iter *from) +{ +struct inode *inode =file_inode (iocb->ki_filp); +loff_t blockmask =i_blocksize (inode) -1 ; +size_t count =iov_iter_count (from); +unsigned int flags =0 ; +ssize_t ret; + +ASSERT (fuse_inode_has_iomap (inode)); + +if (!count) +return 0 ; + +/* +* Unaligned direct writes require zeroing of unwritten head and tail
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 984
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+* blocks. Extending writes require zeroing of post-EOF tail blocks. +* The zeroing writes must complete before we return the direct write +* to userspace. Don't even bother trying the fast path. +*/ +if ((iocb->ki_pos | count) & blockmask) + flags =IOMAP_DIO_FORCE_WAIT ; + + ret =fuse_iomap_ilock_iocb (iocb,EXCL ); +if (ret) + goto out_dsync;
I wonder if we need the out_dsync goto at all. Maybe just return ret here directly?
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 997
Quoted 13 lines
fs/fuse/file_iomap.c +13
-0+ ret =generic_write_checks (iocb, from); +if (ret <=0 ) + goto out_unlock; + +/* +* If we are doing exclusive unaligned I/O, this must be the only I/O +* in-flight. Otherwise we risk data corruption due to unwritten +* extent conversions from the AIO end_io handler. Wait for all other +* I/O to drain first. +*/ +if (flags &IOMAP_DIO_FORCE_WAIT ) +inode_dio_wait (inode); +
Should we add a file_modified() call here?
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 1001
Quoted 4 lines
fs/fuse/file_iomap.c +4
-0+ ret =iomap_dio_rw (iocb, from, &fuse_iomap_ops, + &fuse_iomap_dio_write_ops, flags,NULL ,0 ); +if (ret) + goto out_unlock;
I think we could get rid of this if (ret) check
Quoted 11 lines
fs/fuse/file_iomap.c +6
-0+ +out_unlock: +inode_unlock (inode); +out_dsync: +return ret; +}fs/fuse/trace.c +0
-0
fs/fuse/trace.c line 12
Quoted 4 lines
fs/fuse/trace.c +1
-0@@ -9,6 +9,7 @@#include "iomap_i.h" #include <linux/pagemap.h> +#include <linux/iomap.h>
Was this meant to be part of the subsequent trace.h patch? I haven't tried compiling this though so maybe I' mmissing something but I'm not seeing which part of the logic above needs this.
Thanks,
Joanne
Quoted 4 lines
fs/fuse/trace.c +0
-0#define CREATE_TRACE_POINTS #include "fuse_trace.h"
Re: [PATCH 12/31] fuse: implement direct IO with iomap
Quoted 9 lines · Joanne Koong
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.
Overall, this makes sense to me. Left a few comments below.
<nod>
Quoted 28 lines
Quoted 28 lines
Signed-off-by "Darrick J. Wong" <[email protected]>--- fs/fuse/fuse_i.h | 30 +++++ include/uapi/linux/fuse.h | 22 ++++ fs/fuse/dir.c | 7 + fs/fuse/file.c | 16 +++ fs/fuse/file_iomap.c | 249 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/trace.c | 1 6 files changed, 323 insertions(+), 2 deletions(-)include/uapi/linux/fuse.h +1
-0@@ -672,6 +672,7 @@ enum fuse_opcode {FUSE_STATX =52 ,FUSE_COPY_FILE_RANGE_64 =53 , +FUSE_IOMAP_IOEND =4093 ,FUSE_IOMAP_BEGIN =4094 , FUSE_IOMAP_END = 4095,@@ -1406,4 +1407,25 @@ struct fuse_iomap_end_in {struct fuse_iomap_io map; };
Quoted 15 lines
include/uapi/linux/fuse.h struct fuse_iomap_end_in { · line 1422
Quoted 13 lines
include/uapi/linux/fuse.h +13
-0+/* out of place write extent */ +#define FUSE_IOMAP_IOEND_SHARED (1U << 0) +/* unwritten extent */ +#define FUSE_IOMAP_IOEND_UNWRITTEN (1U << 1) +/* don't merge into previous ioend */ +#define FUSE_IOMAP_IOEND_BOUNDARY (1U << 2) +/* is direct I/O */ +#define FUSE_IOMAP_IOEND_DIRECT (1U << 3) +/* is append ioend */ +#define FUSE_IOMAP_IOEND_APPEND (1U << 4) + +struct fuse_iomap_ioend_in { +uint32_t ioendflags;/* FUSE_IOMAP_IOEND_* */ Hmm, maybe just "flags" is descriptive enough? Or if not, then "
ioend_flags"?
flags is fine, will change.
Quoted 9 lines
include/uapi/linux/fuse.h struct fuse_iomap_end_in { · line 1427
Quoted 5 lines
include/uapi/linux/fuse.h +5
-0+int32_t error;/* negative errno or 0 */ +uint64_t attr_ino;/* matches fuse_attr:ino */ +uint64_t pos;/* file position, in bytes */ +uint64_t new_addr;/* disk offset of new mapping, in bytes */ +uint32_t written;/* bytes processed */ Is
uint32_tenough here or does it need to be bigger? Asking mostly because I see infuse_iomap_ioend()that the written passed in issize_t.
Hrmm. A directio write cannot exceed MAX_RW_COUNT, which is slightly less than 2GB. On the other hand, the iomap ioend allows us to chain together up to (IOEND_BATCH_SIZE * folio_size) bytes of writeback completions. Even on x86 that could be 8GB, so you're right, this ought to be a u64.
Quoted 37 lines
Quoted 37 lines
include/uapi/linux/fuse.h +3
-0+ uint32_t reserved1; /* zero */ +}; +#endif /* _LINUX_FUSE_H */ fs/fuse/dir.c +7
-0@@ -712,6 +712,10 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,if (err) goto out_acl_release;fuse_dir_changed (dir); + +if (fuse_inode_has_iomap (inode)) +fuse_iomap_open (inode, file); + err =generic_file_open (inode, file);if (!err) { file->private_data = ff;@@ -1743,6 +1747,9 @@ static int fuse_dir_open(struct inode *inode, struct file *file)if (fuse_is_bad (inode))return -EIO ; +if (fuse_inode_has_iomap (inode)) +fuse_iomap_open (inode, file); + err =generic_file_open (inode, file);if (err)return err;fs/fuse/file.c +0
-0@@ -246,6 +246,9 @@ static int fuse_open(struct inode *inode, struct file *file)if (fuse_is_bad (inode))return -EIO ;
Quoted 7 lines
fs/fuse/file.c static int fuse_open(struct inode *inode, struct file *file) · line 251
Quoted 3 lines
fs/fuse/file.c +3
-0+if (is_iomap) +fuse_iomap_open (inode, file); +AFAICT, there aren't any calls to
generic_file_open()where we don't also do this "if (is_iomap) ..." check, so maybe we should just put this logic insidegeneric_file_open()?
I'm confused by your question; there are many users of generic_file_open...?
$ git grep generic_file_open fs/9p/vfs_inode.c:800: err = finish_open(file, dentry, generic_file_open); fs/9p/vfs_inode_dotl.c:317: err = finish_open(file, dentry, generic_file_open); fs/btrfs/file.c:3821: return generic_file_open(inode, filp); fs/fuse/dir.c:916: err = generic_file_open(inode, file); fs/fuse/dir.c:2007: err = generic_file_open(inode, file); fs/fuse/file.c:270: err = generic_file_open(inode, file); fs/gfs2/file.c:632: ret = generic_file_open(inode, file); fs/jffs2/file.c:55: .open = generic_file_open, fs/nilfs2/file.c:148: .open = generic_file_open, fs/ntfs3/file.c:1364: return generic_file_open(inode, file); fs/open.c:1607:int generic_file_open(struct inode * inode, struct file * filp) fs/open.c:1614:EXPORT_SYMBOL(generic_file_open); fs/orangefs/file.c:580: .open = generic_file_open, fs/quota/dquot.c:2219: error = generic_file_open(inode, file); fs/smb/client/dir.c:532: rc = finish_open(file, direntry, generic_file_open); fs/udf/file.c:203: .open = generic_file_open, fs/ufs/file.c:42: .open = generic_file_open, fs/vboxsf/dir.c:341: err = finish_open(file, dentry, generic_file_open); fs/xfs/xfs_file.c:1612: return generic_file_open(inode, file); fs/xfs/xfs_file.c:1626: error = generic_file_open(inode, file); ...
Quoted 9 lines
Quoted 9 lines
fs/fuse/file.c +1
-0err =generic_file_open (inode, file);if (err)return err;@@ -1751,10 +1754,17 @@ static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)struct file *file = iocb->ki_filp;struct fuse_file *ff = file->private_data;struct inode *inode =file_inode (file); +ssize_t ret;
Quoted 8 lines
fs/fuse/file.c static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 1762
Quoted 4 lines
fs/fuse/file.c +1
-0if (fuse_is_bad (inode))return -EIO ; +if (fuse_want_iomap_directio (iocb)) {In fuse, directio is also done if the server sets
FOPEN_DIRECT_IOas part of the structfuse_open_outopen_flagsarg, even ifiocb->ki_flagsdoesn't haveIOCB_DIRECTset.
Yikes. Let me look at the other fuse FOPEN_ flags:
* FOPEN_DIRECT_IO: bypass page cache for this open file
Hrm. This wouldn't be hard to implement, but what happens if the read or write range aren't aligned to the block size? Is it ok to return EINVAL here?
* FOPEN_KEEP_CACHE: don't invalidate the data cache on open
fuse_open takes care of this too. Evidently I need to fix fuse4fs to set this flag.
* FOPEN_NONSEEKABLE: the file is not seekable
* FOPEN_STREAM: the file is stream-like (no file position at all)
Not sure why you'd have a non-seekable regular iomap file but I think fuse_finish_open takes care of this.
* FOPEN_CACHE_DIR: allow caching this directory
Oh wow building up a dirent cache when someone does a readdir. fuse4fs should set this too.
* FOPEN_NOFLUSH: don't flush data cache on close (unless FUSE_WRITEBACK_CACHE)
Hrm. In theory fuse4fs could set this one too, but I think given the higher chance of failure (or the usb stick being yanked out) I might not set this one.
* FOPEN_PARALLEL_DIRECT_WRITES: Allow concurrent direct writes on the same inode
iomap supports this, but fuse-iomap does not. Are we supposed to return EOPNOTSUPP/EINVAL or can we quietly ignore this flag until we support it?
* FOPEN_PASSTHROUGH: passthrough read/write io for this open file
I need to screen this out too.
Quoted 5 lines
fs/fuse/file.c static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 1764
Quoted 2 lines
fs/fuse/file.c +2
-0+ ret =fuse_iomap_direct_read (iocb, to); +if (ret != -ENOSYS )Hmm, where does
fuse_iomap_direct_read()return-ENOSYS? afaict, neitherfuse_iomap_ilock_iocb()noriomap_dio_rw()do?
I /think/ the fuse server can reply with ENOSYS to fuse_iomap_begin(), in which case it'll percolate upwards to iomap_iter into dio->error where it can then be returned through iomap_dio_complete into iomap_dio_rw into fuse_iomap_direct_read.
That will demote a directio...
Quoted 6 lines
fs/fuse/file.c static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 1766
Quoted 2 lines
fs/fuse/file.c +2
-0+return ret; + }I see that later on, in the patch that adds the implementation for buffered IO with iomap, this logic later becomes something like
Quoted 6 lines
if (fuse_want_iomap_directio(iocb)) {
...
}
if (fuse_want_iomap_buffered_io(iocb))
return fuse_iomap_buffered_read(iocb, to);...into a buffered io. Maybe I should special-case ENOTBLK since that's what the other iomap users do.
Originally directio on XFS would never fall back to buffered io. Then we introduced out of place writes for reflink, which forced us to implement the fallback for sub-allocation-unit directio writes that were aligned to LBA size (since XFS has always allowed that).
Later on ext4 (which always allowed fallbacks) entered the chat and now we're stuck with it. :)
Quoted 6 lines
imo (if
-ENOSYSis indeed not possible) something like this is maybe cleaner:if (fuse_inode_has_iomap(inode)) fuse_iomap_read_iter(iocb, to);to move as much iomap-specific logic away from generic fuse files?
That would be cleaner. It might even be cleaner to give iomap files their own file_operations structure completely.
Quoted 16 lines
fuse_want_iomap_directio()/fuse_want_iomap_buffered_io() helpers, eg:
ssize_t fuse_iomap_read_iter(struct kiocb *iocb, struct iov_iter *to) { if (iocb->ki_flags & IOCB_DIRECT) return fuse_iomap_direct_read(iocb, to); return fuse_iomap_buffered_read(iocb, to); }Quoted 8 lines
fs/fuse/file.c +1
-0+if (FUSE_IS_DAX (inode))return fuse_dax_read_iter (iocb, to);@@ -1776,6 +1786,12 @@ static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)if (fuse_is_bad (inode))return -EIO ;
Quoted 7 lines
fs/fuse/file.c static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from) · line 1793
Quoted 5 lines
fs/fuse/file.c +5
-0+if (fuse_want_iomap_directio (iocb)) { +ssize_t ret =fuse_iomap_direct_write (iocb, from); +if (ret != -ENOSYS ) +return ret; + }Same questions as above about
-ENOSYS
Same weird answer, too. :P
Quoted 103 lines
Quoted 103 lines
fs/fuse/file.c +1
-0+if (FUSE_IS_DAX (inode))return fuse_dax_write_iter (iocb, from);fs/fuse/file_iomap.c +65
-2@@ -495,10 +495,15 @@ static int fuse_iomap_begin(struct inode *inode, loff_t pos, loff_t count, }/* Decide if we send FUSE_IOMAP_END to the fuse server */ -static bool fuse_should_send_iomap_end (const struct iomap *iomap, +static bool fuse_should_send_iomap_end (const struct fuse_mount *fm, +const struct iomap *iomap,unsigned int opflags,loff_t count,ssize_t written) { +/* Not implemented on fuse server */ +if (fm->fc->iomap_conn.no_end) +return false; +/* fuse server demanded an iomap_end call. */ if (iomap->flags &FUSE_IOMAP_F_WANT_IOMAP_END )return true;@@ -523,7 +528,7 @@ static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count,struct fuse_mount *fm =get_fuse_mount (inode);int err =0 ; -if (fuse_should_send_iomap_end (iomap, opflags, count, written)) { +if (fuse_should_send_iomap_end (fm, iomap, opflags, count, written)) {struct fuse_iomap_end_in inarg = { .opflags =fuse_iomap_op_to_server (opflags), .attr_ino = fi->orig_ino,@@ -549,6 +554,7 @@ static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count,* libfuse returns ENOSYS for servers that don't * implement iomap_end */ + fm->fc->iomap_conn.no_end =1 ; err =0 ;break ;case 0 :@@ -567,6 +573,95 @@ static const struct iomap_ops fuse_iomap_ops = { .iomap_end = fuse_iomap_end, }; +static inline bool +fuse_should_send_iomap_ioend (const struct fuse_mount *fm, +const struct fuse_iomap_ioend_in *inarg) +{ +/* Not implemented on fuse server */ +if (fm->fc->iomap_conn.no_ioend) +return false; + +/* Always send an ioend for errors. */ +if (inarg->error) +return true; + +/* Send an ioend if we performed an IO involving metadata changes. */ +return inarg->written >0 && + (inarg->ioendflags & (FUSE_IOMAP_IOEND_SHARED | +FUSE_IOMAP_IOEND_UNWRITTEN | +FUSE_IOMAP_IOEND_APPEND )); +} + +/* +* Fast and loose check if this write could update the on-disk inode size. +*/ +static inline bool fuse_ioend_is_append (const struct fuse_inode *fi, +loff_t pos,size_t written) +{ +return pos + written >i_size_read (&fi->inode); +} + +static int fuse_iomap_ioend (struct inode *inode,loff_t pos,size_t written, +int error,unsigned ioendflags,sector_t new_addr) +{ +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_mount *fm =get_fuse_mount (inode); +struct fuse_iomap_ioend_in inarg = { + .ioendflags = ioendflags, + .error = error, + .attr_ino = fi->orig_ino, + .pos = pos, + .written = written, + .new_addr = new_addr, + }; + +if (fuse_ioend_is_append (fi, pos, written)) + inarg.ioendflags |=FUSE_IOMAP_IOEND_APPEND ; + +if (fuse_should_send_iomap_ioend (fm, &inarg)) { +FUSE_ARGS (args); +int err; + + args.opcode =FUSE_IOMAP_IOEND ; + args.nodeid =get_node_id (inode); + args.in_numargs =1 ; + args.in_args[0 ].size =sizeof (inarg); + args.in_args[0 ].value = &inarg; + err =fuse_simple_request (fm, &args); +switch (err) { +case -ENOSYS :
Quoted 8 lines
fs/fuse/file_iomap.c static const struct iomap_ops fuse_iomap_ops = { · line 638
Quoted 6 lines
fs/fuse/file_iomap.c +6
-0+/* +* fuse servers can return ENOSYS if ioend processing +* is never needed for this filesystem. +*/ + fm->fc->iomap_conn.no_ioend =1 ; + err =0 ;It doesn't look like we need to set err here or maybe I'm missing something
Maybe we both are? :D
There shouldn't be variables named @error _and_ @err in the same function; and the err assignment here is indeed pointless as you state. But that's a very confusing thing fo rme to have done.
fuse_iomap_ioend should return the error passed into it; or if no error was passed into it, then it can convey an error that occurred during processing of the ioend itself (e.g. remapping after a write failed).
Quoted 21 lines
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+break ; +case 0 : +break ; +default : +/* +* If the write IO failed, return the failure code to +* the caller no matter what happens with the ioend. +* If the write IO succeeded but the ioend did not, +* pass the new error up to the caller. +*/ fs/fuse/file_iomap.c static const struct iomap_ops fuse_iomap_ops = { · line 658
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+if (!error) + error = err; +break ; + } + } +if (error) +return error; + +/* +* If there weren't any ioend errors, update the incore isize, which
Quoted 2 lines
Not sure if incore is a standard term, but it had me confused for a bit. I think incore just means kernel-internal?
Yes. Later on in the pagecache path we'll introduce the notion of the "ondisk" isize. Both of these names are iomap/xfs anachronisms.
The incore isize is of course the same kernel-internal file size. The "ondisk" isize is the file size according to the fuse server.
Because iomap supports things like delalloc, this means that the in-memory file can become quite a bit larger than what the kernel has pushed to disk (and the fuse server) via writeback. If userspace asks the kernel to do something that requires immediate metadata changes such as fallocate, it's critical to preflush anything dirty between the "ondisk" EOF and the affected range.
Example: Let's say you write a 100K file and do not fsync it. The incore isize is 100K, but nobody's told the fuse server anything so it thinks the file size is still empty. Next, you pwrite another 22K but this time at offset 20,000K. The incore isize is now 22,022K, but pagecache writeback hasn't triggered yet, so the fuse server thinks the file is still empty.
Now you ask to fallocate 4K at offset 10,000K. This requires a trip to the fuse server to fill that 4k hole, but before you can do that, you have to flush any dirty data between the ondisk size (0) and the start of the fallocate range (10000K) because the fuse command will change the file size to 10,004K, and as the write came before the fallocate, the effects of that write must be persisted before the effects of the fallocate.
But at this point we're only doing direct writes so this is a confusing long way to update isize on an extending write.
Quoted 9 lines
Quoted 9 lines
fs/fuse/file_iomap.c +6
-0+* confusingly takes the new i_size as "pos". +*/ +fuse_write_update_attr (inode, pos + written, written); +return 0 ; +} +static int fuse_iomap_may_admin (struct fuse_conn *fc,unsigned int flags) {if (!fc->iomap)
Quoted 8 lines
fs/fuse/file_iomap.c void fuse_iomap_mount(struct fuse_mount *fm) · line 717
Quoted 6 lines
fs/fuse/file_iomap.c +2
-0@@ -618,6 +713,8 @@ void fuse_iomap_mount(struct fuse_mount *fm)* freeze/thaw properly. */ fc->sync_fs = true; + fc->iomap_conn.no_end =0 ; + fc->iomap_conn.no_ioend =0 ;fc after it's first allocated has all its fields memset to 0
Ok, will fix.
Quoted 30 lines
Quoted 30 lines
fs/fuse/file_iomap.c +23
-0}void fuse_iomap_unmount (struct fuse_mount *fm)@@ -760,3 +857,151 @@ loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence)return offset;return vfs_setpos (file, offset, inode->i_sb->s_maxbytes); } + +void fuse_iomap_open (struct inode *inode,struct file *file) +{ +ASSERT (fuse_inode_has_iomap (inode)); + + file->f_mode |=FMODE_NOWAIT |FMODE_CAN_ODIRECT ; +} + +enum fuse_ilock_type { +SHARED , +EXCL , +}; + +static int fuse_iomap_ilock_iocb (const struct kiocb *iocb, +enum fuse_ilock_type type) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (iocb->ki_flags &IOCB_NOWAIT ) { +switch (type) { +case SHARED : +return inode_trylock_shared (inode) ?0 : -EAGAIN ; +case EXCL :
Quoted 8 lines
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 888
Quoted 6 lines
fs/fuse/file_iomap.c +6
-0+return inode_trylock (inode) ?0 : -EAGAIN ; +default : +ASSERT (0 ); +return -EIO ; + } + }else {nit: the else {} scoping doesn't seem needed here
<nod>
Quoted 22 lines
Quoted 22 lines
fs/fuse/file_iomap.c +22
-0+switch (type) { +case SHARED : +inode_lock_shared (inode); +break ; +case EXCL : +inode_lock (inode); +break ; +default : +ASSERT (0 ); +return -EIO ; + } + } + +return 0 ; +} + +ssize_t fuse_iomap_direct_read (struct kiocb *iocb,struct iov_iter *to) +{ +struct inode *inode =file_inode (iocb->ki_filp); +ssize_t ret; + +ASSERT (fuse_inode_has_iomap (inode));
Quoted 8 lines
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 915
Quoted 5 lines
fs/fuse/file_iomap.c +5
-0+ +if (!iov_iter_count (to)) +return 0 ;/* skip atime */ + +file_accessed (iocb->ki_filp);Does it make sense for this to be moved below so it's called only if
fuse_iomap_ilock_iocb()succeeded?
Ideally we'd do it on successful return from iomap_dio_read.
Curiously, XFS does it this way (bump atime, take i_rwsem, do read), whereas ext4 relies on filemap_read, which does it at the end.
Weird.
Quoted 30 lines
Quoted 30 lines
fs/fuse/file_iomap.c +30
-0+ + ret =fuse_iomap_ilock_iocb (iocb,SHARED ); +if (ret) +return ret; + ret =iomap_dio_rw (iocb, to, &fuse_iomap_ops,NULL ,0 ,NULL ,0 ); +inode_unlock_shared (inode); + +return ret; +} + +static int fuse_iomap_dio_write_end_io (struct kiocb *iocb,ssize_t written, +int error,unsigned dioflags) +{ +struct inode *inode =file_inode (iocb->ki_filp); +unsigned int nofs_flag; +unsigned int ioendflags =FUSE_IOMAP_IOEND_DIRECT ; +int ret; + +if (fuse_is_bad (inode)) +return -EIO ; + +ASSERT (fuse_inode_has_iomap (inode)); + +if (dioflags &IOMAP_DIO_COW ) + ioendflags |=FUSE_IOMAP_IOEND_SHARED ; +if (dioflags &IOMAP_DIO_UNWRITTEN ) + ioendflags |=FUSE_IOMAP_IOEND_UNWRITTEN ; + +/* +* We can allocate memory here while doing writeback on behalf of
Quoted 8 lines
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 949
Quoted 4 lines
fs/fuse/file_iomap.c +4
-0+* memory reclaim. To avoid memory allocation deadlocks set the +* task-wide nofs context for the following operations. +*/ + nofs_flag =memalloc_nofs_save ();I'm a bit confused by this part. Could you explain how it's invoked while doing writeback for memory reclaim? As I understand it, writeback goes through buffered io and not direct io?
I think this is a throwback to earlier versions of the iomap patchset where I tried using the actual directio write machinery to perform writebacks. They're separate now, so I think this can go away.
Quoted 30 lines
Quoted 30 lines
fs/fuse/file_iomap.c +30
-0+ ret =fuse_iomap_ioend (inode, iocb->ki_pos, written, error, ioendflags, +FUSE_IOMAP_NULL_ADDR ); +memalloc_nofs_restore (nofs_flag); +return ret; +} + +static const struct iomap_dio_ops fuse_iomap_dio_write_ops = { + .end_io = fuse_iomap_dio_write_end_io, +}; + +ssize_t fuse_iomap_direct_write (struct kiocb *iocb,struct iov_iter *from) +{ +struct inode *inode =file_inode (iocb->ki_filp); +loff_t blockmask =i_blocksize (inode) -1 ; +size_t count =iov_iter_count (from); +unsigned int flags =0 ; +ssize_t ret; + +ASSERT (fuse_inode_has_iomap (inode)); + +if (!count) +return 0 ; + +/* +* Unaligned direct writes require zeroing of unwritten head and tail +* blocks. Extending writes require zeroing of post-EOF tail blocks. +* The zeroing writes must complete before we return the direct write +* to userspace. Don't even bother trying the fast path. +*/ +if ((iocb->ki_pos | count) & blockmask)
Quoted 8 lines
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 984
Quoted 5 lines
fs/fuse/file_iomap.c +5
-0+ flags =IOMAP_DIO_FORCE_WAIT ; + + ret =fuse_iomap_ilock_iocb (iocb,EXCL ); +if (ret) + goto out_dsync;I wonder if we need the
out_dsyncgoto at all. Maybe just return ret here directly?
Ok.
Quoted 7 lines
Quoted 7 lines
fs/fuse/file_iomap.c +7
-0+ ret =generic_write_checks (iocb, from); +if (ret <=0 ) + goto out_unlock; + +/* +* If we are doing exclusive unaligned I/O, this must be the only I/O +* in-flight. Otherwise we risk data corruption due to unwritten
Quoted 8 lines
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 997
Quoted 6 lines
fs/fuse/file_iomap.c +6
-0+* extent conversions from the AIO end_io handler. Wait for all other +* I/O to drain first. +*/ +if (flags &IOMAP_DIO_FORCE_WAIT ) +inode_dio_wait (inode); +Should we add a
file_modified()call here?
Urk. That will get fixed when I implement fuse_iomap_write_checks in the next patch that does buffered IO, but yes, it's needed here too.
Quoted 7 lines
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 1001
Quoted 5 lines
fs/fuse/file_iomap.c +4
-0+ ret =iomap_dio_rw (iocb, from, &fuse_iomap_ops, + &fuse_iomap_dio_write_ops, flags,NULL ,0 ); +if (ret) + goto out_unlock;I think we could get rid of this if (ret) check
Will do.
Quoted 13 lines
Quoted 13 lines
fs/fuse/file_iomap.c +6
-0+ +out_unlock: +inode_unlock (inode); +out_dsync: +return ret; +}fs/fuse/trace.c +0
-0@@ -9,6 +9,7 @@#include "iomap_i.h"
Quoted 6 lines
fs/fuse/trace.c line 12
Quoted 2 lines
fs/fuse/trace.c +1
-0#include <linux/pagemap.h> +#include <linux/iomap.h> Was this meant to be part of the subsequent
trace.hpatch? I haven't tried compiling this though so maybe I' mmissing something but I'm not seeing which part of the logic above needs this.
Yes. Originally the tracepoints were not broken out, but Miklos asked for things to be this way.
--D
Quoted 7 lines
Thanks,
JoanneQuoted 4 lines
fs/fuse/trace.c +0
-0#define CREATE_TRACE_POINTS #include "fuse_trace.h"
Re: [PATCH 12/31] fuse: implement direct IO with iomap
In reply to Darrick J. WongQuoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit fab88aeb077db04c5f4fdbaff31a58f4dd9450c8
Author: Darrick J. Wong <[email protected]>
fuse: implement direct IO with iomap
Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.
Quoted 4 lines
fs/fuse/file.c +0
-0
[ ... ]
fs/fuse/file.c void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, · line 421
Quoted 8 lines
fs/fuse/file.c +1
-1@@ -408,7 +416,7 @@ void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, {WARN_ON (refcount_read (&ff->count) >1 );fuse_prepare_release (fi, ff, flags,FUSE_RELEASE , true); -fuse_file_put (ff, true); +fuse_file_put (ff, &fi->inode, true); }EXPORT_SYMBOL_GPL (fuse_sync_release);
Can this code dereference a NULL pointer when fi is NULL?
Looking at fuse_create_open in dir.c, when fuse_iget() fails, it calls:
fuse_sync_release(NULL, ff, flags);
The fuse_prepare_release function explicitly handles fi == NULL with the comment "Inode is NULL on error path of fuse_create_open()".
However, the new call to fuse_file_put passes &fi->inode, which evaluates to &NULL->inode. While the inode parameter is unused in the current commit's implementation of fuse_file_put, the API contract is broken.
In commit 5f4e9030b26b (fuse: enable iomap), fuse_file_put uses the inode parameter to call fuse_inode_has_iomap(inode). Since struct inode is the first member of struct fuse_inode (offsetof == 0), &fi->inode evaluates to NULL, and fuse_inode_has_iomap(NULL) will dereference NULL.
Quoted 4 lines
fs/fuse/fuse_iomap.c +0
-0
[ ... ]
Quoted 22 lines
fs/fuse/fuse_iomap.c +18
-0@@ -545,6 +551,120 @@ static const struct iomap_ops fuse_iomap_ops = { .iomap_end = fuse_iomap_end, }; +static inline bool +fuse_should_send_iomap_ioend (const struct fuse_mount *fm, +const struct fuse_iomap_ioend_in *inarg) +{ +/* Not implemented on fuse server */ +if (fm->fc->iomap_conn.no_ioend) +return false; + +/* Always send an ioend for errors. */ +if (inarg->error) +return true; + +/* Send an ioend if we performed an IO involving metadata changes. */ +return inarg->written >0 && + (inarg->flags & (FUSE_IOMAP_IOEND_SHARED | +FUSE_IOMAP_IOEND_UNWRITTEN | +FUSE_IOMAP_IOEND_APPEND )); +}
[ ... ]
Quoted 62 lines
fs/fuse/fuse_iomap.c +62
-0+static int fuse_iomap_ioend (struct inode *inode,loff_t pos,size_t written, +int error,unsigned ioendflags, +struct block_device *bdev,sector_t new_addr) +{ +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_mount *fm =get_fuse_mount (inode); +struct fuse_iomap_ioend_in inarg = { + .flags = ioendflags, + .error = error, + .attr_ino = fi->orig_ino, + .pos = pos, + .written = written, + .dev =fuse_iomap_find_backing_id (fm->fc, bdev), + .new_addr = new_addr, + }; +struct fuse_iomap_ioend_out outarg = { }; + +if (fuse_ioend_is_append (fi, pos, written)) + inarg.flags |=FUSE_IOMAP_IOEND_APPEND ; + +if (fuse_should_send_iomap_ioend (fm, &inarg)) { +FUSE_ARGS (args); +int iomap_error; + + args.opcode =FUSE_IOMAP_IOEND ; + args.nodeid =get_node_id (inode); + args.in_numargs =1 ; + args.in_args[0 ].size =sizeof (inarg); + args.in_args[0 ].value = &inarg; + args.out_numargs =1 ; + args.out_args[0 ].size =sizeof (outarg); + args.out_args[0 ].value = &outarg; + iomap_error =fuse_simple_request (fm, &args); +switch (iomap_error) { +case -ENOSYS : +/* +* fuse servers can return ENOSYS if ioend processing +* is never needed for this filesystem. Don't pass +* that up to iomap. +*/ + fm->fc->iomap_conn.no_ioend =1 ; +break ; +case 0 : +break ; +default : +/* +* If the write IO failed, return the failure code to +* the caller no matter what happens with the ioend. +* If the write IO succeeded but the ioend did not, +* pass the new error up to the caller. +*/ +if (!error) + error = iomap_error; +break ; + } + } + +/* +* Pass whatever error iomap gave us (or any new errors since then) +* back to iomap. +*/ +if (error)
fs/fuse/fuse_iomap.c
Quoted 10 lines
fs/fuse/fuse_iomap.c +10
-0+return error; + +/* +* If there weren't any ioend errors, update the incore isize, which +* confusingly takes the new i_size as "pos". +*/ + fi->i_disk_size = outarg.newsize; +fuse_write_update_attr (inode, pos + written, written); +return 0 ; +}
When the fuse server returns -ENOSYS (indicating it doesn't implement FUSE_IOMAP_IOEND), the code sets fm->fc->iomap_conn.no_ioend = 1 but does NOT return early. It falls through to the update code where 'outarg' was zero-initialized and never filled by the server.
This corrupts i_disk_size by setting it to 0:
fi->i_disk_size = outarg.newsize; // outarg.newsize == 0
On subsequent writes, fuse_should_send_iomap_ioend() returns false due to no_ioend being set, so the code skips the server call and again uses the zero-initialized outarg.newsize.
The i_disk_size field is used by fuse_ioend_is_append() to detect append writes, so corrupting it to 0 will cause incorrect append detection for all subsequent writes.
Should the code return early when the request is not sent or returns -ENOSYS, or skip the i_disk_size update in this case?
Quoted 8 lines
fs/fuse/fuse_iomap.c +8
-0+static int fuse_iomap_ilock_iocb (const struct kiocb *iocb, +enum fuse_ilock_type type) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (iocb->ki_flags &IOCB_NOWAIT ) { +switch (type) { +case SHARED :
fs/fuse/fuse_iomap.c
Quoted 10 lines
fs/fuse/fuse_iomap.c +10
-0+return inode_trylock_shared (inode) ?0 : -EAGAIN ; +case EXCL : +return inode_trylock (inode) ?0 : -EAGAIN ; +default : +ASSERT (0 ); +return -EIO ; + } + +return 0 ; + }
This isn't a bug, but the 'return 0;' after the switch statement inside the IOCB_NOWAIT block is unreachable since every switch case returns.
Re: [PATCH 12/31] fuse: implement direct IO with iomap
Quoted 8 lines · Chris Mason
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.
Quoted 6 lines
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
I should mention: I appreciate you rolling out the AI reviews by slowly expanding the number of trees you scan for bugs.
Quoted 22 lines
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit
fab88aeb077db04c5f4fdbaff31a58f4dd9450c8
Author: Darrick J. Wong <[email protected]>fuse: implement direct IO with iomap
Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.
Quoted 4 lines
fs/fuse/file.c +0
-0[ ... ]
Quoted 10 lines
fs/fuse/file.c void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, · line 421
Quoted 8 lines
fs/fuse/file.c +1
-1@@ -408,7 +416,7 @@ void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, {WARN_ON (refcount_read (&ff->count) >1 );fuse_prepare_release (fi, ff, flags,FUSE_RELEASE , true); -fuse_file_put (ff, true); +fuse_file_put (ff, &fi->inode, true); }EXPORT_SYMBOL_GPL (fuse_sync_release);Can this code dereference a NULL pointer when fi is NULL?
Yikes, yes it can...
Quoted 11 lines
Looking at
fuse_create_openindir.c, whenfuse_iget()fails, it calls:fuse_sync_release(NULL, ff, flags);The
fuse_prepare_releasefunction explicitly handles fi == NULL with the comment "Inode is NULL on error path offuse_create_open()".However, the new call to
fuse_file_putpasses&fi->inode, which evaluates to&NULL->inode. While the inode parameter is unused in the current commit's implementation offuse_file_put, the API contract is broken.
...so this is a very good catch! Though as it turns out we don't need this change to fuse_file_put until we actually add fuse_iomap_release() several patchsets from now, so I think I'll revert this part of the patch.
Quoted 115 lines
In commit
5f4e9030b26b(fuse: enable iomap),fuse_file_putuses the inode parameter to callfuse_inode_has_iomap(inode). Since struct inode is the first member of structfuse_inode(offsetof == 0),&fi->inodeevaluates to NULL, andfuse_inode_has_iomap(NULL)will dereference NULL.Quoted 4 lines
fs/fuse/fuse_iomap.c +0
-0[ ... ]
Quoted 22 lines
fs/fuse/fuse_iomap.c +18
-0@@ -545,6 +551,120 @@ static const struct iomap_ops fuse_iomap_ops = { .iomap_end = fuse_iomap_end, }; +static inline bool +fuse_should_send_iomap_ioend (const struct fuse_mount *fm, +const struct fuse_iomap_ioend_in *inarg) +{ +/* Not implemented on fuse server */ +if (fm->fc->iomap_conn.no_ioend) +return false; + +/* Always send an ioend for errors. */ +if (inarg->error) +return true; + +/* Send an ioend if we performed an IO involving metadata changes. */ +return inarg->written >0 && + (inarg->flags & (FUSE_IOMAP_IOEND_SHARED | +FUSE_IOMAP_IOEND_UNWRITTEN | +FUSE_IOMAP_IOEND_APPEND )); +}[ ... ]
Quoted 62 lines
fs/fuse/fuse_iomap.c +62
-0+static int fuse_iomap_ioend (struct inode *inode,loff_t pos,size_t written, +int error,unsigned ioendflags, +struct block_device *bdev,sector_t new_addr) +{ +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_mount *fm =get_fuse_mount (inode); +struct fuse_iomap_ioend_in inarg = { + .flags = ioendflags, + .error = error, + .attr_ino = fi->orig_ino, + .pos = pos, + .written = written, + .dev =fuse_iomap_find_backing_id (fm->fc, bdev), + .new_addr = new_addr, + }; +struct fuse_iomap_ioend_out outarg = { }; + +if (fuse_ioend_is_append (fi, pos, written)) + inarg.flags |=FUSE_IOMAP_IOEND_APPEND ; + +if (fuse_should_send_iomap_ioend (fm, &inarg)) { +FUSE_ARGS (args); +int iomap_error; + + args.opcode =FUSE_IOMAP_IOEND ; + args.nodeid =get_node_id (inode); + args.in_numargs =1 ; + args.in_args[0 ].size =sizeof (inarg); + args.in_args[0 ].value = &inarg; + args.out_numargs =1 ; + args.out_args[0 ].size =sizeof (outarg); + args.out_args[0 ].value = &outarg; + iomap_error =fuse_simple_request (fm, &args); +switch (iomap_error) { +case -ENOSYS : +/* +* fuse servers can return ENOSYS if ioend processing +* is never needed for this filesystem. Don't pass +* that up to iomap. +*/ + fm->fc->iomap_conn.no_ioend =1 ; +break ; +case 0 : +break ; +default : +/* +* If the write IO failed, return the failure code to +* the caller no matter what happens with the ioend. +* If the write IO succeeded but the ioend did not, +* pass the new error up to the caller. +*/ +if (!error) + error = iomap_error; +break ; + } + } + +/* +* Pass whatever error iomap gave us (or any new errors since then) +* back to iomap. +*/ +if (error)fs/fuse/fuse_iomap.c
Quoted 10 lines
fs/fuse/fuse_iomap.c +10
-0+return error; + +/* +* If there weren't any ioend errors, update the incore isize, which +* confusingly takes the new i_size as "pos". +*/ + fi->i_disk_size = outarg.newsize; +fuse_write_update_attr (inode, pos + written, written); +return 0 ; +}When the fuse server returns
-ENOSYS(indicating it doesn't implementFUSE_IOMAP_IOEND), the code setsfm->fc->iomap_conn.no_ioend= 1 but does NOT return early. It falls through to the update code where 'outarg' was zero-initialized and never filled by the server.
Quoted 7 lines
This corrupts
i_disk_sizeby setting it to 0:fi->i_disk_size = outarg.newsize; // outarg.newsize == 0On subsequent writes,
fuse_should_send_iomap_ioend()returns false due tono_ioendbeing set, so the code skips the server call and again uses the zero-initialized outarg.newsize.
Yeah. outarg should be initialized with fi->i_disk_size, though I think this bug is benign because no_ioend being set to 1 in that ENOSYS case means that this function doesn't do much of anything after that.
Quoted 20 lines
The
i_disk_sizefield is used byfuse_ioend_is_append()to detect append writes, so corrupting it to 0 will cause incorrect append detection for all subsequent writes.Should the code return early when the request is not sent or returns
-ENOSYS, or skip thei_disk_sizeupdate in this case?Quoted 13 lines
fs/fuse/fuse_iomap.c +13
-0+static int fuse_iomap_ilock_iocb (const struct kiocb *iocb, +enum fuse_ilock_type type) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (iocb->ki_flags &IOCB_NOWAIT ) { +switch (type) { +case SHARED : +return inode_trylock_shared (inode) ?0 : -EAGAIN ; +case EXCL : +return inode_trylock (inode) ?0 : -EAGAIN ; +default : +ASSERT (0 );
Quoted 8 lines
fs/fuse/fuse_iomap.c
Quoted 5 lines
fs/fuse/fuse_iomap.c +5
-0+return -EIO ; + } + +return 0 ; + }This isn't a bug, but the 'return 0;' after the switch statement inside the
IOCB_NOWAITblock is unreachable since every switch case returns.
gcc is too stupid to detect that it's impossible to reach this case and whines about the lack of a return.
--D
Re: [PATCH 12/31] fuse: implement direct IO with iomap
Quoted 11 lines · Darrick J. Wong
Quoted 10 lines · Chris Mason
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.
Hi everyone,
Quoted 7 lines
Quoted 4 lines
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
I should mention: I appreciate you rolling out the AI reviews by slowly expanding the number of trees you scan for bugs.
Thanks, I'm trying not to send unsolicited AI unless it feels like it's mostly right. I did discard one false positive, which looked accurate but also looked like intended behavior.
Now that the false positive rate is pretty reasonable, I'll try to collect some fs/* Fixes: tagged patches and see if I can teach claude how to spot the bugs. In past kernel-wide scans, it gets ~35%, which is better than 0, but not as good as I was hoping for.
[ ... ]
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
fs/fuse/file_iomap.c +13
-0+static int fuse_iomap_ilock_iocb (const struct kiocb *iocb, +enum fuse_ilock_type type) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (iocb->ki_flags &IOCB_NOWAIT ) { +switch (type) { +case SHARED : +return inode_trylock_shared (inode) ?0 : -EAGAIN ; +case EXCL : +return inode_trylock (inode) ?0 : -EAGAIN ; +default : +ASSERT (0 );Quoted 8 lines
fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 903
Quoted 5 lines
fs/fuse/file_iomap.c +5
-0+return -EIO ; + } + +return 0 ; + }This isn't a bug, but the 'return 0;' after the switch statement inside the
IOCB_NOWAITblock is unreachable since every switch case returns.
Quoted 2 lines
gcc is too stupid to detect that it's impossible to reach this case and whines about the lack of a return.
Oh that's great.
-chris
Re: [PATCH 12/31] fuse: implement direct IO with iomap
Quoted 20 lines · Chris Mason
Quoted 11 lines · Darrick J. Wong
Quoted 10 lines · Chris Mason
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.
Hi everyone,
Quoted 7 lines
Quoted 4 lines
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
I should mention: I appreciate you rolling out the AI reviews by slowly expanding the number of trees you scan for bugs.
Quoted 8 lines
Thanks, I'm trying not to send unsolicited AI unless it feels like it's mostly right. I did discard one false positive, which looked accurate but also looked like intended behavior.
Now that the false positive rate is pretty reasonable, I'll try to collect some
fs/*Fixes: tagged patches and see if I can teach claude how to spot the bugs. In past kernel-wide scans, it gets ~35%, which is better than 0, but not as good as I was hoping for.
<nod> You've found some very good bugs, especially in the fuse-iomap branch! At some point I'm going to have to figure out how to run these tools myself, but until then you're quite welcome to keep scanning my dev trees. :)
I wonder, have you tried it on non-kernel repos like e2fsprogs (ha!) or fstests?
Quoted 24 lines
[ ... ]
Quoted 22 lines
Quoted 21 lines
Quoted 8 lines
fs/fuse/file_iomap.c +8
-0+static int fuse_iomap_ilock_iocb (const struct kiocb *iocb, +enum fuse_ilock_type type) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (iocb->ki_flags &IOCB_NOWAIT ) { +switch (type) { +case SHARED :fs/fuse/file_iomap.c loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence) · line 903
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+return inode_trylock_shared (inode) ?0 : -EAGAIN ; +case EXCL : +return inode_trylock (inode) ?0 : -EAGAIN ; +default : +ASSERT (0 ); +return -EIO ; + } + +return 0 ; + }This isn't a bug, but the 'return 0;' after the switch statement inside the
IOCB_NOWAITblock is unreachable since every switch case returns.
Quoted 4 lines
Quoted 2 lines
gcc is too stupid to detect that it's impossible to reach this case and whines about the lack of a return.
Oh that's great.
I know, right? :(
--D
Quoted 2 lines
-chris
Re: [PATCH 12/31] fuse: implement direct IO with iomap
Quoted 30 lines · Darrick J. Wong
Quoted 20 lines · Chris Mason
Quoted 11 lines · Darrick J. Wong
Quoted 10 lines · Chris Mason
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.
Hi everyone,
Quoted 7 lines
Quoted 4 lines
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
I should mention: I appreciate you rolling out the AI reviews by slowly expanding the number of trees you scan for bugs.
Quoted 8 lines
Thanks, I'm trying not to send unsolicited AI unless it feels like it's mostly right. I did discard one false positive, which looked accurate but also looked like intended behavior.
Now that the false positive rate is pretty reasonable, I'll try to collect some
fs/*Fixes: tagged patches and see if I can teach claude how to spot the bugs. In past kernel-wide scans, it gets ~35%, which is better than 0, but not as good as I was hoping for.
Quoted 6 lines
<nod> You've found some very good bugs, especially in the fuse-iomap branch! At some point I'm going to have to figure out how to run these tools myself, but until then you're quite welcome to keep scanning my dev trees. :)
Quoted 1 line
I wonder, have you tried it on non-kernel repos like e2fsprogs (ha!) or
fstests?
The prompts are here:
https://github.com/masoncl/review-prompts
And thanks to Christian they now have both kernel and systemd specific directories. The original versions of the prompts had a lot of details about exactly how to review code, but recent models don't seem to need (or follow) that level of detail.
Instead it's really just forcing larger chunks of the call graph into the AI context window, and adding some kernel specific knowledge about locking, rcu, gfp masks, sleepable vs irq context etc. Basically the weird stuff that we've forgotten is weird.
So non-kernel projects would mostly work but would need a few fixups depending on how far they stray from kernel semantics. It's easy enough to add branches into the prompts, with the asterisk that from time to time AI ignores all the instructions and does what it wants.
-chris
PATCH 13/31 fuse_trace: implement direct IO with iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/file_iomap.c | 13 +++++ 2 files changed, 157 insertions(+)
fs/fuse/fuse_trace.h +144 -0
@@ -60,6 +60,7 @@ EM( FUSE_STATX, "FUSE_STATX") \ EM( FUSE_IOMAP_BEGIN, "FUSE_IOMAP_BEGIN") \ EM( FUSE_IOMAP_END, "FUSE_IOMAP_END") \ + EM( FUSE_IOMAP_IOEND, "FUSE_IOMAP_IOEND") \EMe (CUSE_INIT ,"CUSE_INIT" )/*
@@ -300,6 +301,17 @@ DEFINE_FUSE_BACKING_EVENT(fuse_backing_close); { FUSE_IOMAP_TYPE_UNWRITTEN, "unwritten" }, \ { FUSE_IOMAP_TYPE_INLINE, "inline" } +#define FUSE_IOMAP_IOEND_STRINGS \ + { FUSE_IOMAP_IOEND_SHARED, "shared" }, \ + { FUSE_IOMAP_IOEND_UNWRITTEN, "unwritten" }, \ + { FUSE_IOMAP_IOEND_BOUNDARY, "boundary" }, \ + { FUSE_IOMAP_IOEND_DIRECT, "direct" }, \ + { FUSE_IOMAP_IOEND_APPEND, "append" } + +#define IOMAP_DIOEND_STRINGS \ + { IOMAP_DIO_UNWRITTEN, "unwritten" }, \ + { IOMAP_DIO_COW, "cow" } +TRACE_DEFINE_ENUM (FUSE_I_ADVISE_RDPLUS );TRACE_DEFINE_ENUM (FUSE_I_INIT_RDPLUS );TRACE_DEFINE_ENUM (FUSE_I_SIZE_UNSTABLE );
@@ -484,6 +496,65 @@ TRACE_EVENT(fuse_iomap_end_error, __entry->error) ); +TRACE_EVENT (fuse_iomap_ioend, + TP_PROTO(const struct inode *inode, + const struct fuse_iomap_ioend_in *inarg), + +TP_ARGS (inode, inarg), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + __field(unsigned, ioendflags) + __field(int, error) +__field (uint64_t, new_addr) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = inarg->pos; + __entry->length = inarg->written; + __entry->ioendflags = inarg->ioendflags; + __entry->error = inarg->error; + __entry->new_addr = inarg->new_addr; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " ioendflags (%s) error %d new_addr 0x%llx", +FUSE_IO_RANGE_PRINTK_ARGS (), +__print_flags (__entry->ioendflags,"|" ,FUSE_IOMAP_IOEND_STRINGS ), + __entry->error, + __entry->new_addr) +); + +TRACE_EVENT (fuse_iomap_ioend_error, + TP_PROTO(const struct inode *inode, + const struct fuse_iomap_ioend_in *inarg, + int error), + +TP_ARGS (inode, inarg, error), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + __field(unsigned, ioendflags) + __field(int, error) +__field (uint64_t, new_addr) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = inarg->pos; + __entry->length = inarg->written; + __entry->ioendflags = inarg->ioendflags; + __entry->error = error; + __entry->new_addr = inarg->new_addr; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " ioendflags (%s) error %d new_addr 0x%llx", +FUSE_IO_RANGE_PRINTK_ARGS (), +__print_flags (__entry->ioendflags,"|" ,FUSE_IOMAP_IOEND_STRINGS ), + __entry->error, + __entry->new_addr) +); + TRACE_EVENT(fuse_iomap_dev_add, TP_PROTO(const struct fuse_conn *fc, const struct fuse_backing_map *map),
@@ -578,6 +649,79 @@ TRACE_EVENT(fuse_iomap_lseek, __entry->offset, __entry->whence) ); + +DECLARE_EVENT_CLASS (fuse_iomap_file_io_class, + TP_PROTO(const struct kiocb *iocb, const struct iov_iter *iter), +TP_ARGS (iocb, iter), +TP_STRUCT__entry ( +FUSE_IO_RANGE_FIELDS () + ), +TP_fast_assign ( + FUSE_INODE_ASSIGN(file_inode(iocb->ki_filp), fi, fm); + __entry->offset = iocb->ki_pos; + __entry->length = iov_iter_count(iter); + ), +TP_printk (FUSE_IO_RANGE_FMT (), +FUSE_IO_RANGE_PRINTK_ARGS ()) +) +#define DEFINE_FUSE_IOMAP_FILE_IO_EVENT (name) \ +DEFINE_EVENT(fuse_iomap_file_io_class, name, \ + TP_PROTO(const struct kiocb *iocb, const struct iov_iter *iter), \ + TP_ARGS(iocb, iter)) +DEFINE_FUSE_IOMAP_FILE_IO_EVENT (fuse_iomap_direct_read); +DEFINE_FUSE_IOMAP_FILE_IO_EVENT (fuse_iomap_direct_write); + +DECLARE_EVENT_CLASS (fuse_iomap_file_ioend_class, + TP_PROTO(const struct kiocb *iocb, const struct iov_iter *iter, + ssize_t ret), +TP_ARGS (iocb, iter, ret), +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() +__field (ssize_t, ret) + ), +TP_fast_assign ( + FUSE_INODE_ASSIGN(file_inode(iocb->ki_filp), fi, fm); + __entry->offset = iocb->ki_pos; + __entry->length = iov_iter_count(iter); + __entry->ret = ret; + ), + TP_printk(FUSE_IO_RANGE_FMT() " ret 0x%zx", +FUSE_IO_RANGE_PRINTK_ARGS (), + __entry->ret) +) +#define DEFINE_FUSE_IOMAP_FILE_IOEND_EVENT (name) \ +DEFINE_EVENT(fuse_iomap_file_ioend_class, name, \ + TP_PROTO(const struct kiocb *iocb, const struct iov_iter *iter, \ + ssize_t ret), \ + TP_ARGS(iocb, iter, ret)) +DEFINE_FUSE_IOMAP_FILE_IOEND_EVENT (fuse_iomap_direct_read_end); +DEFINE_FUSE_IOMAP_FILE_IOEND_EVENT (fuse_iomap_direct_write_end); + +TRACE_EVENT (fuse_iomap_dio_write_end_io, + TP_PROTO(const struct inode *inode, loff_t pos, ssize_t written, + int error, unsigned flags), + +TP_ARGS (inode, pos, written, error, flags), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + __field(unsigned, dioendflags) +__field (int, error) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = pos; + __entry->length = written; + __entry->dioendflags = flags; + __entry->error = error; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " dioendflags (%s) error %d", +FUSE_IO_RANGE_PRINTK_ARGS (), +__print_flags (__entry->dioendflags,"|" ,IOMAP_DIOEND_STRINGS ), + __entry->error) +);#endif /* CONFIG_FUSE_IOMAP */ #endif /* _TRACE_FUSE_H */
fs/fuse/file_iomap.c +13 -0
@@ -618,6 +618,8 @@ static int fuse_iomap_ioend(struct inode *inode, loff_t pos, size_t written,if (fuse_ioend_is_append (fi, pos, written)) inarg.ioendflags |=FUSE_IOMAP_IOEND_APPEND ; +trace_fuse_iomap_ioend (inode, &inarg); +if (fuse_should_send_iomap_ioend (fm, &inarg)) {FUSE_ARGS (args);int err;
@@ -640,6 +642,8 @@ static int fuse_iomap_ioend(struct inode *inode, loff_t pos, size_t written,case 0 :break ;default : +trace_fuse_iomap_ioend_error (inode, &inarg, err); +/* * If the write IO failed, return the failure code to * the caller no matter what happens with the ioend.
@@ -909,6 +913,8 @@ ssize_t fuse_iomap_direct_read(struct kiocb *iocb, struct iov_iter *to)ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_direct_read (iocb, to); +if (!iov_iter_count (to))return 0 ;/* skip atime */
@@ -920,6 +926,7 @@ ssize_t fuse_iomap_direct_read(struct kiocb *iocb, struct iov_iter *to) ret =iomap_dio_rw (iocb, to, &fuse_iomap_ops,NULL ,0 ,NULL ,0 );inode_unlock_shared (inode); +trace_fuse_iomap_direct_read_end (iocb, to, ret);return ret; }
@@ -936,6 +943,9 @@ static int fuse_iomap_dio_write_end_io(struct kiocb *iocb, ssize_t written,ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_dio_write_end_io (inode, iocb->ki_pos, written, error, + dioflags); +if (dioflags &IOMAP_DIO_COW ) ioendflags |=FUSE_IOMAP_IOEND_SHARED ;if (dioflags &IOMAP_DIO_UNWRITTEN )
@@ -967,6 +977,8 @@ ssize_t fuse_iomap_direct_write(struct kiocb *iocb, struct iov_iter *from)ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_direct_write (iocb, from); +if (!count)return 0 ;
@@ -1003,5 +1015,6 @@ ssize_t fuse_iomap_direct_write(struct kiocb *iocb, struct iov_iter *from) out_unlock:inode_unlock (inode); out_dsync: +trace_fuse_iomap_direct_write_end (iocb, from, ret);return ret; }
Re: [PATCH 13/31] fuse_trace: implement direct IO with iomap
Quoted 4 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
commit 3fad15ebe8d51187a2b6820ed6db4774a8b065b0
Author: Darrick J. Wong <[email protected]>
fuse_trace: implement direct IO with iomap
Add tracepoints for the previous patch.
Quoted 4 lines
fs/fuse/fuse_trace.h +0
-0
[ ... ]
Quoted 21 lines
fs/fuse/fuse_trace.h +20
-0@@ -484,6 +499,75 @@ TRACE_EVENT(fuse_iomap_end_error, +TRACE_EVENT(fuse_iomap_ioend, + TP_PROTO(const struct inode *inode, + const struct fuse_iomap_ioend_in *inarg), + + TP_ARGS(inode, inarg), + + TP_STRUCT__entry( + FUSE_IO_RANGE_FIELDS() + __field(unsigned, ioendflags) + __field(int, error) + __field(uint32_t, dev) + __field(uint64_t, new_addr) + ), + + TP_fast_assign( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = inarg->pos; + __entry->length = inarg->written; + __entry->ioendflags = inarg->flags; + __entry->error = inarg->error;
fs/fuse/fuse_trace.h TRACE_EVENT(fuse_iomap_end_error, · line 528
Quoted 10 lines
fs/fuse/fuse_trace.h +10
-0+ __entry->dev = inarg->dev; + __entry->new_addr = inarg->new_addr; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " ioendflags (%s) error %d dev %u new_addr 0x%llx", +FUSE_IO_RANGE_PRINTK_ARGS (), +__print_flags (__entry->ioendflags,"|" ,FUSE_IOMAP_IOEND_STRINGS ), + __entry->dev, + __entry->error, + __entry->new_addr)
^^^^
Are the error and dev fields swapped in the argument list?
The format string expects 'error %d dev %u' but the arguments are provided as __entry->dev, __entry->error. This would cause the trace output to display the dev value as error and the error value as dev.
Quoted 27 lines
fs/fuse/fuse_trace.h +27
-0+); + +TRACE_EVENT(fuse_iomap_ioend_error, + TP_PROTO(const struct inode *inode, + const struct fuse_iomap_ioend_in *inarg, + const struct fuse_iomap_ioend_out *outarg, + int error), + + TP_ARGS(inode, inarg, outarg, error), + + TP_STRUCT__entry( + FUSE_IO_RANGE_FIELDS() + __field(unsigned, ioendflags) + __field(int, error) + __field(uint32_t, dev) + __field(uint64_t, new_addr) + __field(uint64_t, new_size) + ), + + TP_fast_assign( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = inarg->pos; + __entry->length = inarg->written; + __entry->ioendflags = inarg->flags; + __entry->error = error; + __entry->dev = inarg->dev; + __entry->new_addr = inarg->new_addr;
fs/fuse/fuse_trace.h TRACE_EVENT(fuse_iomap_end_error,
Quoted 10 lines
fs/fuse/fuse_trace.h +10
-0+ __entry->new_size = outarg->newsize; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " ioendflags (%s) error %d dev %u new_addr 0x%llx new_size 0x%llx", +FUSE_IO_RANGE_PRINTK_ARGS (), +__print_flags (__entry->ioendflags,"|" ,FUSE_IOMAP_IOEND_STRINGS ), + __entry->dev, + __entry->error, + __entry->new_addr, + __entry->new_size)
^^^^
Same issue here. The format string expects 'error %d dev %u' but the arguments are provided as __entry->dev, __entry->error, swapping the values in the trace output.
Quoted 2 lines
fs/fuse/fuse_trace.h +2
-0+); +
[ ... ]
Re: [PATCH 13/31] fuse_trace: implement direct IO with iomap
Quoted 98 lines · Chris Mason
Quoted 4 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
commit
3fad15ebe8d51187a2b6820ed6db4774a8b065b0
Author: Darrick J. Wong <[email protected]>
fuse_trace: implement direct IO with iomapAdd tracepoints for the previous patch.
Quoted 4 lines
fs/fuse/fuse_trace.h +0
-0[ ... ]
Quoted 21 lines
fs/fuse/fuse_trace.h +20
-0@@ -484,6 +499,75 @@ TRACE_EVENT(fuse_iomap_end_error, +TRACE_EVENT(fuse_iomap_ioend, + TP_PROTO(const struct inode *inode, + const struct fuse_iomap_ioend_in *inarg), + + TP_ARGS(inode, inarg), + + TP_STRUCT__entry( + FUSE_IO_RANGE_FIELDS() + __field(unsigned, ioendflags) + __field(int, error) + __field(uint32_t, dev) + __field(uint64_t, new_addr) + ), + + TP_fast_assign( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = inarg->pos; + __entry->length = inarg->written; + __entry->ioendflags = inarg->flags; + __entry->error = inarg->error;fs/fuse/fuse_trace.h TRACE_EVENT(fuse_iomap_end_error, · line 528
Quoted 10 lines
fs/fuse/fuse_trace.h +10
-0+ __entry->dev = inarg->dev; + __entry->new_addr = inarg->new_addr; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " ioendflags (%s) error %d dev %u new_addr 0x%llx", +FUSE_IO_RANGE_PRINTK_ARGS (), +__print_flags (__entry->ioendflags,"|" ,FUSE_IOMAP_IOEND_STRINGS ), + __entry->dev, + __entry->error, + __entry->new_addr)^^^^Are the error and dev fields swapped in the argument list?
The format string expects 'error %d dev %u' but the arguments are provided as
__entry->dev,__entry->error. This would cause the trace output to display the dev value as error and the error value as dev.Quoted 27 lines
fs/fuse/fuse_trace.h +27
-0+); + +TRACE_EVENT(fuse_iomap_ioend_error, + TP_PROTO(const struct inode *inode, + const struct fuse_iomap_ioend_in *inarg, + const struct fuse_iomap_ioend_out *outarg, + int error), + + TP_ARGS(inode, inarg, outarg, error), + + TP_STRUCT__entry( + FUSE_IO_RANGE_FIELDS() + __field(unsigned, ioendflags) + __field(int, error) + __field(uint32_t, dev) + __field(uint64_t, new_addr) + __field(uint64_t, new_size) + ), + + TP_fast_assign( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = inarg->pos; + __entry->length = inarg->written; + __entry->ioendflags = inarg->flags; + __entry->error = error; + __entry->dev = inarg->dev; + __entry->new_addr = inarg->new_addr;fs/fuse/fuse_trace.h TRACE_EVENT(fuse_iomap_end_error,
Quoted 10 lines
fs/fuse/fuse_trace.h +10
-0+ __entry->new_size = outarg->newsize; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " ioendflags (%s) error %d dev %u new_addr 0x%llx new_size 0x%llx", +FUSE_IO_RANGE_PRINTK_ARGS (), +__print_flags (__entry->ioendflags,"|" ,FUSE_IOMAP_IOEND_STRINGS ), + __entry->dev, + __entry->error, + __entry->new_addr, + __entry->new_size)^^^^
Quoted 3 lines
Same issue here. The format string expects 'error %d dev %u' but the arguments are provided as
__entry->dev,__entry->error, swapping the values in the trace output.
Yep, both wrong, both fixed. Thanks!
--D
Quoted 7 lines
Quoted 3 lines
fs/fuse/fuse_trace.h +2
-0+); +[ ... ]
PATCH 14/31 fuse: implement buffered IO with iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Implement pagecache IO with iomap, complete with hooks into truncate and fallocate so that the fuse server needn't implement disk block zeroing of post-EOF and unaligned punch/zero regions.
--- fs/fuse/fuse_i.h | 30 ++ include/uapi/linux/fuse.h | 5 fs/fuse/dir.c | 23 ++ fs/fuse/file.c | 86 +++++- fs/fuse/file_iomap.c | 655 ++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 775 insertions(+), 24 deletions(-)
fs/fuse/fuse_i.h +30 -0
@@ -181,6 +181,13 @@ struct fuse_inode {/* waitq for direct-io completion */ wait_queue_head_t direct_io_waitq; + +#ifdef CONFIG_FUSE_IOMAP +/* pending io completions */ +spinlock_t ioend_lock; +struct work_struct ioend_work; +struct list_head ioend_list; +#endif };/* readdir cache (directory only) */
@@ -1722,6 +1729,8 @@ void fuse_iomap_sysfs_cleanup(struct kobject *kobj);# define fuse_iomap_sysfs_cleanup (...) ((void)0)#endif +sector_t fuse_bmap (struct address_space *mapping,sector_t block); +#if IS_ENABLED (CONFIG_FUSE_IOMAP )bool fuse_iomap_enabled (void );
@@ -1761,6 +1770,20 @@ static inline bool fuse_want_iomap_directio(const struct kiocb *iocb)ssize_t fuse_iomap_direct_read (struct kiocb *iocb,struct iov_iter *to);ssize_t fuse_iomap_direct_write (struct kiocb *iocb,struct iov_iter *from); + +static inline bool fuse_want_iomap_buffered_io (const struct kiocb *iocb) +{ +return fuse_inode_has_iomap (file_inode (iocb->ki_filp)); +} + +int fuse_iomap_mmap (struct file *file,struct vm_area_struct *vma); +ssize_t fuse_iomap_buffered_read (struct kiocb *iocb,struct iov_iter *to); +ssize_t fuse_iomap_buffered_write (struct kiocb *iocb,struct iov_iter *from); +int fuse_iomap_setsize_start (struct inode *inode,loff_t newsize); +int fuse_iomap_fallocate (struct file *file,int mode,loff_t offset, +loff_t length,loff_t new_size); +int fuse_iomap_flush_unmap_range (struct inode *inode,loff_t pos, +loff_t endpos);#else # define fuse_iomap_enabled (...) (false)# define fuse_has_iomap (...) (false)
@@ -1777,6 +1800,13 @@ ssize_t fuse_iomap_direct_write(struct kiocb *iocb, struct iov_iter *from);# define fuse_want_iomap_directio (...) (false)# define fuse_iomap_direct_read (...) (-ENOSYS)# define fuse_iomap_direct_write (...) (-ENOSYS) +# define fuse_want_iomap_buffered_io (...) (false) +# define fuse_iomap_mmap (...) (-ENOSYS) +# define fuse_iomap_buffered_read (...) (-ENOSYS) +# define fuse_iomap_buffered_write (...) (-ENOSYS) +# define fuse_iomap_setsize_start (...) (-ENOSYS) +# define fuse_iomap_fallocate (...) (-ENOSYS) +# define fuse_iomap_flush_unmap_range (...) (-ENOSYS)#endif #endif /* _FS_FUSE_I_H */
include/uapi/linux/fuse.h +5 -0
@@ -1368,6 +1368,9 @@ struct fuse_uring_cmd_req {#define FUSE_IOMAP_OP_ATOMIC (1U << 9)#define FUSE_IOMAP_OP_DONTCACHE (1U << 10) +/* pagecache writeback operation */ +#define FUSE_IOMAP_OP_WRITEBACK (1U << 31) +#define FUSE_IOMAP_NULL_ADDR (-1ULL)/* addr is not valid */ struct fuse_iomap_io {
@@ -1417,6 +1420,8 @@ struct fuse_iomap_end_in {#define FUSE_IOMAP_IOEND_DIRECT (1U << 3)/* is append ioend */ #define FUSE_IOMAP_IOEND_APPEND (1U << 4) +/* is pagecache writeback */ +#define FUSE_IOMAP_IOEND_WRITEBACK (1U << 5)struct fuse_iomap_ioend_in {uint32_t ioendflags;/* FUSE_IOMAP_IOEND_* */
fs/fuse/dir.c +22 -1
@@ -2020,7 +2020,10 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry, is_truncate = true; } -if (FUSE_IS_DAX (inode) && is_truncate) { +if (is_iomap && is_truncate) { +filemap_invalidate_lock (mapping); + fault_blocked = true; + }else if (FUSE_IS_DAX (inode) && is_truncate) {filemap_invalidate_lock (mapping); fault_blocked = true; err =fuse_dax_break_layouts (inode,0 ,-1 );
@@ -2035,6 +2038,18 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,WARN_ON (!(attr->ia_valid &ATTR_SIZE ));WARN_ON (attr->ia_size !=0 );if (fc->atomic_o_trunc) { +if (is_iomap) { +/* +* fuse_open already set the size to zero and +* truncated the pagecache, and we've since +* cycled the inode locks. Another thread +* could have performed an appending write, so +* we don't want to touch the file further. +*/ +filemap_invalidate_unlock (mapping); +return 0 ; + } +/* * No need to send request to userspace, since actual * truncation has already been done by OPEN. But still
@@ -2068,6 +2083,12 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,set_bit (FUSE_I_SIZE_UNSTABLE , &fi->state);if (trust_local_cmtime && attr->ia_size != inode->i_size) attr->ia_valid |=ATTR_MTIME |ATTR_CTIME ; + +if (is_iomap) { + err =fuse_iomap_setsize_start (inode, attr->ia_size); +if (err) + goto error; + } }memset (&inarg,0 ,sizeof (inarg));
fs/fuse/file.c +73 -13
175 lines not on this page · raw message
fs/fuse/file_iomap.c +645 -10
694 lines not on this page · raw message
Re: [PATCH 14/31] fuse: implement buffered IO with iomap
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Implement pagecache IO with iomap, complete with hooks into truncate and fallocate so that the fuse server needn't implement disk block zeroing of post-EOF and unaligned punch/zero regions.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit 05d561a4dd38273cb93745e8ddf913f490f81ad5
Author: Darrick J. Wong <[email protected]>
fuse: implement buffered IO with iomap
Implement pagecache IO with iomap, complete with hooks into truncate and fallocate so that the fuse server needn't implement disk block zeroing of post-EOF and unaligned punch/zero regions.
Quoted 1 line
fs/fuse/fuse_iomap.c +0
-0
[ ... ]
Quoted 10 lines
fs/fuse/fuse_iomap.c +6
-0@@ -1056,6 +1144,365 @@ void fuse_iomap_open_truncate(struct inode *inode) fi->i_disk_size =0 ; } +struct fuse_writepage_ctx { +struct iomap_writepage_ctx ctx; +}; + +static void fuse_iomap_end_ioend (struct iomap_ioend *ioend) +{
fs/fuse/fuse_iomap.c void fuse_iomap_open_truncate(struct inode *inode) · line 1162
Quoted 10 lines
fs/fuse/fuse_iomap.c +9
-0+struct inode *inode = ioend->io_inode; +unsigned int ioendflags =FUSE_IOMAP_IOEND_WRITEBACK ; +unsigned int nofs_flag; +int error =blk_status_to_errno (ioend->io_bio.bi_status); + +ASSERT (fuse_inode_has_iomap (inode)); + +if (fuse_is_bad (inode)) +return ; ^^^^
Does this early return leak the ioend? When fuse_is_bad(inode) returns true, this function returns without calling iomap_finish_ioends().
Looking at the iomap code, iomap_finish_ioends() is responsible for:
1. Calling bio_put() to free the bio structure embedded in the ioend
2. Calling iomap_finish_folio_write() to end writeback on the folios
Without this cleanup, we get two problems:
- Memory leak: the bio structure is never freed
- Folios stuck in writeback state forever, which can cause system hangs as pages cannot be reclaimed or written again
The cleanup at line 1176 appears mandatory regardless of inode state.
[ ... ]
Re: [PATCH 14/31] fuse: implement buffered IO with iomap
Quoted 63 lines · Chris Mason
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Implement pagecache IO with iomap, complete with hooks into truncate and fallocate so that the fuse server needn't implement disk block zeroing of post-EOF and unaligned punch/zero regions.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit
05d561a4dd38273cb93745e8ddf913f490f81ad5
Author: Darrick J. Wong <[email protected]>fuse: implement buffered IO with iomap
Implement pagecache IO with iomap, complete with hooks into truncate and fallocate so that the fuse server needn't implement disk block zeroing of post-EOF and unaligned punch/zero regions.
Quoted 1 line
fs/fuse/fuse_iomap.c +0
-0[ ... ]
Quoted 10 lines
fs/fuse/fuse_iomap.c +6
-0@@ -1056,6 +1144,365 @@ void fuse_iomap_open_truncate(struct inode *inode) fi->i_disk_size =0 ; } +struct fuse_writepage_ctx { +struct iomap_writepage_ctx ctx; +}; + +static void fuse_iomap_end_ioend (struct iomap_ioend *ioend) +{fs/fuse/fuse_iomap.c void fuse_iomap_open_truncate(struct inode *inode) · line 1162
Quoted 10 lines
fs/fuse/fuse_iomap.c +9
-0+struct inode *inode = ioend->io_inode; +unsigned int ioendflags =FUSE_IOMAP_IOEND_WRITEBACK ; +unsigned int nofs_flag; +int error =blk_status_to_errno (ioend->io_bio.bi_status); + +ASSERT (fuse_inode_has_iomap (inode)); + +if (fuse_is_bad (inode)) +return ; ^^^^Does this early return leak the ioend? When
fuse_is_bad(inode)returns true, this function returns without callingiomap_finish_ioends().Looking at the iomap code,
iomap_finish_ioends()is responsible for:1. Calling
bio_put()to free the bio structure embedded in the ioend
2. Callingiomap_finish_folio_write()to end writeback on the folios
Quoted 7 lines
Without this cleanup, we get two problems:
- Memory leak: the bio structure is never freed
- Folios stuck in writeback state forever, which can cause system hangs as pages cannot be reclaimed or written againThe cleanup at line 1176 appears mandatory regardless of inode state.
Correct, will fix.
--D
Quoted 4 lines
[ ... ]
PATCH 15/31 fuse_trace: implement buffered IO with iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/file_iomap.c | 40 ++++++++ 2 files changed, 288 insertions(+), 4 deletions(-)
fs/fuse/fuse_trace.h +250 -2
@@ -224,6 +224,9 @@ DEFINE_FUSE_BACKING_EVENT(fuse_backing_close);#endif /* CONFIG_FUSE_BACKING */ #if IS_ENABLED (CONFIG_FUSE_IOMAP ) +struct iomap_writepage_ctx ; +struct iomap_ioend ; +/* tracepoint boilerplate so we don't have to keep doing this */ #define FUSE_IOMAP_OPFLAGS_FIELD \ __field(unsigned, opflags)
@@ -291,7 +294,8 @@ DEFINE_FUSE_BACKING_EVENT(fuse_backing_close); { FUSE_IOMAP_OP_UNSHARE, "unshare" }, \ { FUSE_IOMAP_OP_DAX, "fsdax" }, \ { FUSE_IOMAP_OP_ATOMIC, "atomic" }, \ - { FUSE_IOMAP_OP_DONTCACHE, "dontcache" } + { FUSE_IOMAP_OP_DONTCACHE, "dontcache" }, \ + { FUSE_IOMAP_OP_WRITEBACK, "writeback" } #define FUSE_IOMAP_TYPE_STRINGS \ { FUSE_IOMAP_TYPE_PURE_OVERWRITE, "overwrite" }, \
@@ -306,7 +310,8 @@ DEFINE_FUSE_BACKING_EVENT(fuse_backing_close); { FUSE_IOMAP_IOEND_UNWRITTEN, "unwritten" }, \ { FUSE_IOMAP_IOEND_BOUNDARY, "boundary" }, \ { FUSE_IOMAP_IOEND_DIRECT, "direct" }, \ - { FUSE_IOMAP_IOEND_APPEND, "append" } + { FUSE_IOMAP_IOEND_APPEND, "append" }, \ + { FUSE_IOMAP_IOEND_WRITEBACK, "writeback" } #define IOMAP_DIOEND_STRINGS \ { IOMAP_DIO_UNWRITTEN, "unwritten" }, \
@@ -331,6 +336,12 @@ TRACE_DEFINE_ENUM(FUSE_I_IOMAP); { 1 << FUSE_I_EXCLUSIVE, "excl" }, \ { 1 << FUSE_I_IOMAP, "iomap" } +#define IOMAP_IOEND_STRINGS \ + { IOMAP_IOEND_SHARED, "shared" }, \ + { IOMAP_IOEND_UNWRITTEN, "unwritten" }, \ + { IOMAP_IOEND_BOUNDARY, "boundary" }, \ + { IOMAP_IOEND_DIRECT, "direct" } + DECLARE_EVENT_CLASS(fuse_iomap_check_class, TP_PROTO(const char *func, int line, const char *condition),
@@ -670,6 +681,9 @@ DEFINE_EVENT(fuse_iomap_file_io_class, name, \ TP_ARGS(iocb, iter))DEFINE_FUSE_IOMAP_FILE_IO_EVENT (fuse_iomap_direct_read);DEFINE_FUSE_IOMAP_FILE_IO_EVENT (fuse_iomap_direct_write); +DEFINE_FUSE_IOMAP_FILE_IO_EVENT (fuse_iomap_buffered_read); +DEFINE_FUSE_IOMAP_FILE_IO_EVENT (fuse_iomap_buffered_write); +DEFINE_FUSE_IOMAP_FILE_IO_EVENT (fuse_iomap_write_zero_eof); DECLARE_EVENT_CLASS(fuse_iomap_file_ioend_class, TP_PROTO(const struct kiocb *iocb, const struct iov_iter *iter,
@@ -696,6 +710,8 @@ DEFINE_EVENT(fuse_iomap_file_ioend_class, name, \ TP_ARGS(iocb, iter, ret))DEFINE_FUSE_IOMAP_FILE_IOEND_EVENT (fuse_iomap_direct_read_end);DEFINE_FUSE_IOMAP_FILE_IOEND_EVENT (fuse_iomap_direct_write_end); +DEFINE_FUSE_IOMAP_FILE_IOEND_EVENT (fuse_iomap_buffered_read_end); +DEFINE_FUSE_IOMAP_FILE_IOEND_EVENT (fuse_iomap_buffered_write_end); TRACE_EVENT(fuse_iomap_dio_write_end_io, TP_PROTO(const struct inode *inode, loff_t pos, ssize_t written,
@@ -722,6 +738,238 @@ TRACE_EVENT(fuse_iomap_dio_write_end_io,__print_flags (__entry->dioendflags,"|" ,IOMAP_DIOEND_STRINGS ), __entry->error) ); + +TRACE_EVENT (fuse_iomap_end_ioend, + TP_PROTO(const struct iomap_ioend *ioend), + +TP_ARGS (ioend), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + __field(unsigned int, ioendflags) +__field (int, error) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(ioend->io_inode, fi, fm); + __entry->offset = ioend->io_offset; + __entry->length = ioend->io_size; + __entry->ioendflags = ioend->io_flags; + __entry->error = blk_status_to_errno(ioend->io_bio.bi_status); + ), + + TP_printk(FUSE_IO_RANGE_FMT() " ioendflags (%s) error %d", +FUSE_IO_RANGE_PRINTK_ARGS (), +__print_flags (__entry->ioendflags,"|" ,IOMAP_IOEND_STRINGS ), + __entry->error) +); + +TRACE_EVENT (fuse_iomap_writeback_range, + TP_PROTO(const struct inode *inode, u64 offset, unsigned int count, + u64 end_pos), + +TP_ARGS (inode, offset, count, end_pos), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() +__field (uint64_t, end_pos) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = offset; + __entry->length = count; + __entry->end_pos = end_pos; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " end_pos 0x%llx", +FUSE_IO_RANGE_PRINTK_ARGS (), + __entry->end_pos) +); + +TRACE_EVENT (fuse_iomap_writeback_submit, + TP_PROTO(const struct iomap_writepage_ctx *wpc, int error), + +TP_ARGS (wpc, error), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + __field(unsigned int, nr_folios) + __field(uint64_t, addr) +__field (int, error) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(wpc->inode, fi, fm); + __entry->nr_folios = wpc->nr_folios; + __entry->offset = wpc->iomap.offset; + __entry->length = wpc->iomap.length; + __entry->addr = wpc->iomap.addr << 9; + __entry->error = error; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " addr 0x%llx nr_folios %u error %d", +FUSE_IO_RANGE_PRINTK_ARGS (), + __entry->addr, + __entry->nr_folios, + __entry->error) +); + +TRACE_EVENT (fuse_iomap_discard_folio, + TP_PROTO(const struct inode *inode, loff_t offset, size_t count), + +TP_ARGS (inode, offset, count), + +TP_STRUCT__entry ( +FUSE_IO_RANGE_FIELDS () + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = offset; + __entry->length = count; + ), + +TP_printk (FUSE_IO_RANGE_FMT (), +FUSE_IO_RANGE_PRINTK_ARGS ()) +); + +TRACE_EVENT (fuse_iomap_writepages, + TP_PROTO(const struct inode *inode, const struct writeback_control *wbc), + +TP_ARGS (inode, wbc), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + __field(long, nr_to_write) +__field (bool, sync_all) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = wbc->range_start; + __entry->length = wbc->range_end - wbc->range_start + 1; + __entry->nr_to_write = wbc->nr_to_write; + __entry->sync_all = wbc->sync_mode == WB_SYNC_ALL; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " nr_folios %ld sync_all? %d", +FUSE_IO_RANGE_PRINTK_ARGS (), + __entry->nr_to_write, + __entry->sync_all) +); + +TRACE_EVENT (fuse_iomap_read_folio, + TP_PROTO(const struct folio *folio), + +TP_ARGS (folio), + +TP_STRUCT__entry ( +FUSE_IO_RANGE_FIELDS () + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(folio->mapping->host, fi, fm); + __entry->offset = folio_pos(folio); + __entry->length = folio_size(folio); + ), + +TP_printk (FUSE_IO_RANGE_FMT (), +FUSE_IO_RANGE_PRINTK_ARGS ()) +); + +TRACE_EVENT (fuse_iomap_readahead, + TP_PROTO(const struct readahead_control *rac), + +TP_ARGS (rac), + +TP_STRUCT__entry ( +FUSE_IO_RANGE_FIELDS () + ), + +TP_fast_assign ( + struct readahead_control *mutrac = (struct readahead_control *)rac; + FUSE_INODE_ASSIGN(file_inode(rac->file), fi, fm); + __entry->offset = readahead_pos(mutrac); + __entry->length = readahead_length(mutrac); + ), + +TP_printk (FUSE_IO_RANGE_FMT (), +FUSE_IO_RANGE_PRINTK_ARGS ()) +); + +TRACE_EVENT (fuse_iomap_page_mkwrite, + TP_PROTO(const struct vm_fault *vmf), + +TP_ARGS (vmf), + +TP_STRUCT__entry ( +FUSE_IO_RANGE_FIELDS () + ), + +TP_fast_assign ( + struct folio *folio = page_folio(vmf->page); + FUSE_INODE_ASSIGN(file_inode(vmf->vma->vm_file), fi, fm); + __entry->offset = folio_pos(folio); + __entry->length = folio_size(folio); + ), + +TP_printk (FUSE_IO_RANGE_FMT (), +FUSE_IO_RANGE_PRINTK_ARGS ()) +); + +DECLARE_EVENT_CLASS (fuse_iomap_file_range_class, + TP_PROTO(const struct inode *inode, loff_t offset, loff_t length), + +TP_ARGS (inode, offset, length), + +TP_STRUCT__entry ( +FUSE_IO_RANGE_FIELDS () + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = offset; + __entry->length = length; + ), + +TP_printk (FUSE_IO_RANGE_FMT (), +FUSE_IO_RANGE_PRINTK_ARGS ()) +) +#define DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT (name) \ +DEFINE_EVENT(fuse_iomap_file_range_class, name, \ + TP_PROTO(const struct inode *inode, loff_t offset, loff_t length), \ + TP_ARGS(inode, offset, length)) +DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT (fuse_iomap_truncate_up); +DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT (fuse_iomap_truncate_down); +DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT (fuse_iomap_punch_range); +DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT (fuse_iomap_setsize); +DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT (fuse_iomap_flush_unmap_range); + +TRACE_EVENT (fuse_iomap_fallocate, + TP_PROTO(const struct inode *inode, int mode, loff_t offset, + loff_t length, loff_t newsize), +TP_ARGS (inode, mode, offset, length, newsize), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + __field(loff_t, newsize) +__field (int, mode) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = offset; + __entry->length = length; + __entry->mode = mode; + __entry->newsize = newsize; + ), + + TP_printk(FUSE_IO_RANGE_FMT() " mode 0x%x newsize 0x%llx", +FUSE_IO_RANGE_PRINTK_ARGS (), + __entry->mode, + __entry->newsize) +);#endif /* CONFIG_FUSE_IOMAP */ #endif /* _TRACE_FUSE_H */
fs/fuse/file_iomap.c +38 -2
@@ -1026,6 +1026,8 @@ fuse_iomap_write_zero_eof(return 1 ; } +trace_fuse_iomap_write_zero_eof (iocb, from); +filemap_invalidate_lock (mapping); error =fuse_iomap_zero_range (inode, isize, iocb->ki_pos - isize,NULL );filemap_invalidate_unlock (mapping);
@@ -1138,6 +1140,8 @@ static void fuse_iomap_end_ioend(struct iomap_ioend *ioend)if (fuse_is_bad (inode))return ; +trace_fuse_iomap_end_ioend (ioend); +if (ioend->io_flags &IOMAP_IOEND_SHARED ) ioendflags |=FUSE_IOMAP_IOEND_SHARED ;if (ioend->io_flags &IOMAP_IOEND_UNWRITTEN )
@@ -1246,6 +1250,8 @@ static void fuse_iomap_discard_folio(struct folio *folio, loff_t pos, int error)ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_discard_folio (inode, pos,folio_size (folio)); +printk_ratelimited (KERN_ERR "page discard on page %px, inode 0x%llx, pos %llu." , folio, fi->orig_ino, pos);
@@ -1269,6 +1275,8 @@ static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc,ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_writeback_range (inode, offset, len, end_pos); +if (!fuse_iomap_revalidate_writeback (wpc, offset)) { ret = fuse_iomap_begin(inode, offset, len, FUSE_IOMAP_OP_WRITEBACK,
@@ -1306,6 +1314,8 @@ static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc,ASSERT (fuse_inode_has_iomap (ioend->io_inode)); +trace_fuse_iomap_writeback_submit (wpc, error); +/* always call our ioend function, even if we cancel the bio */ ioend->io_bio.bi_end_io = fuse_iomap_end_bio;return iomap_ioend_writeback_submit (wpc, error);
@@ -1329,6 +1339,8 @@ static int fuse_iomap_writepages(struct address_space *mapping,ASSERT (fuse_inode_has_iomap (mapping->host)); +trace_fuse_iomap_writepages (mapping->host, wbc); +return iomap_writepages (&wpc.ctx); }
@@ -1336,6 +1348,8 @@ static int fuse_iomap_read_folio(struct file *file, struct folio *folio) {ASSERT (fuse_inode_has_iomap (file_inode (file))); +trace_fuse_iomap_read_folio (folio); +return iomap_read_folio (folio, &fuse_iomap_ops); }
@@ -1343,6 +1357,8 @@ static void fuse_iomap_readahead(struct readahead_control *rac) {ASSERT (fuse_inode_has_iomap (file_inode (rac->file))); +trace_fuse_iomap_readahead (rac); +iomap_readahead (rac, &fuse_iomap_ops); }
@@ -1391,6 +1407,8 @@ static vm_fault_t fuse_iomap_page_mkwrite(struct vm_fault *vmf)ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_page_mkwrite (vmf); +sb_start_pagefault (inode->i_sb);file_update_time (vmf->vma->vm_file);
@@ -1424,6 +1442,8 @@ ssize_t fuse_iomap_buffered_read(struct kiocb *iocb, struct iov_iter *to)ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_buffered_read (iocb, to); +if (!iov_iter_count (to))return 0 ;/* skip atime */
@@ -1435,6 +1455,7 @@ ssize_t fuse_iomap_buffered_read(struct kiocb *iocb, struct iov_iter *to) ret =generic_file_read_iter (iocb, to);inode_unlock_shared (inode); +trace_fuse_iomap_buffered_read_end (iocb, to, ret);return ret; }
@@ -1447,6 +1468,8 @@ ssize_t fuse_iomap_buffered_write(struct kiocb *iocb, struct iov_iter *from)ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_buffered_write (iocb, from); +if (!iov_iter_count (from))return 0 ;
@@ -1475,6 +1498,7 @@ ssize_t fuse_iomap_buffered_write(struct kiocb *iocb, struct iov_iter *from)/* Handle various SYNC-type writes */ ret =generic_write_sync (iocb, ret); } +trace_fuse_iomap_buffered_write_end (iocb, from, ret);return ret; }
@@ -1520,11 +1544,17 @@ fuse_iomap_setsize_start(* extension, or zeroing out the rest of the block on a downward * truncate. */ -if (newsize > oldsize) +if (newsize > oldsize) { +trace_fuse_iomap_truncate_up (inode, oldsize, newsize - oldsize); + error =fuse_iomap_zero_range (inode, oldsize, newsize - oldsize, &did_zeroing); -else + }else { +trace_fuse_iomap_truncate_down (inode, newsize, + oldsize - newsize); + error =fuse_iomap_truncate_page (inode, newsize, &did_zeroing); + }if (error)return error;
@@ -1571,6 +1601,8 @@ int fuse_iomap_flush_unmap_range(struct inode *inode, loff_t pos, start =round_down (pos, rounding); end =round_up (endpos +1 , rounding) -1 ; +trace_fuse_iomap_flush_unmap_range (inode, start, end +1 - start); + error =filemap_write_and_wait_range (inode->i_mapping, start, end);if (error)return error;
@@ -1584,6 +1616,8 @@ static int fuse_iomap_punch_range(struct inode *inode, loff_t offset,loff_t isize =i_size_read (inode);int error; +trace_fuse_iomap_punch_range (inode, offset, length); +/* * Now that we've unmap all full blocks we'll have to zero out any * partial block at the beginning and/or end. iomap_zero_range is
@@ -1627,6 +1661,8 @@ fuse_iomap_fallocate(ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_fallocate (inode, mode, offset, length, new_size); +/* * If we unmapped blocks from the file range, then we zero the * pagecache for those regions and push them to disk rather than make
PATCH 16/31 fuse: implement large folios for iomap pagecache files
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Use large folios when we're using iomap.
--- fs/fuse/file_iomap.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/fuse/file_iomap.c +6 -0
@@ -1380,12 +1380,18 @@ static const struct address_space_operations fuse_iomap_aops = {static inline void fuse_inode_set_iomap (struct inode *inode) {struct fuse_inode *fi =get_fuse_inode (inode); +unsigned int min_order =0 ; inode->i_data.a_ops = &fuse_iomap_aops;INIT_WORK (&fi->ioend_work, fuse_iomap_end_io);INIT_LIST_HEAD (&fi->ioend_list);spin_lock_init (&fi->ioend_lock); + +if (inode->i_blkbits >PAGE_SHIFT ) + min_order = inode->i_blkbits -PAGE_SHIFT ; + +mapping_set_folio_min_order (inode->i_mapping, min_order);set_bit (FUSE_I_IOMAP , &fi->state); }
Re: [PATCH 16/31] fuse: implement large folios for iomap pagecache files
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Use large folios when we're using iomap.
Signed-off-by "Darrick J. Wong" <[email protected]>
Quoted 29 lines
--- fs/fuse/file_iomap.c | 6 ++++++ 1 file changed, 6 insertions(+)fs/fuse/file_iomap.c +6
-0@@ -1380,12 +1380,18 @@ static const struct address_space_operations fuse_iomap_aops = {static inline void fuse_inode_set_iomap (struct inode *inode) {struct fuse_inode *fi =get_fuse_inode (inode); +unsigned int min_order =0 ; inode->i_data.a_ops = &fuse_iomap_aops;INIT_WORK (&fi->ioend_work, fuse_iomap_end_io);INIT_LIST_HEAD (&fi->ioend_list);spin_lock_init (&fi->ioend_lock); + +if (inode->i_blkbits >PAGE_SHIFT ) + min_order = inode->i_blkbits -PAGE_SHIFT ; + +mapping_set_folio_min_order (inode->i_mapping, min_order);set_bit (FUSE_I_IOMAP , &fi->state); }
PATCH 17/31 fuse: use an unrestricted backing device with iomap pagecache io
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
With iomap support turned on for the pagecache, the kernel issues writeback to directly to block devices and we no longer have to push all those pages through the fuse device to userspace. Therefore, we don't need the tight dirty limits (~1M) that are used for regular fuse. This dramatically increases the performance of fuse's pagecache IO.
--- fs/fuse/file_iomap.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
fs/fuse/file_iomap.c +21 -0
@@ -713,6 +713,27 @@ const struct fuse_backing_ops fuse_iomap_backing_ops = {void fuse_iomap_mount (struct fuse_mount *fm) {struct fuse_conn *fc = fm->fc; +struct super_block *sb = fm->sb; +struct backing_dev_info *old_bdi = sb->s_bdi; +char *suffix = sb->s_bdev ?"-fuseblk" :"-fuse" ; +int res; + +/* +* sb->s_bdi points to the initial private bdi. However, we want to +* redirect it to a new private bdi with default dirty and readahead +* settings because iomap writeback won't be pushing a ton of dirty +* data through the fuse device. If this fails we fall back to the +* initial fuse bdi. +*/ + sb->s_bdi = &noop_backing_dev_info; + res =super_setup_bdi_name (sb,"%u:%u%s.iomap" ,MAJOR (fc->dev), +MINOR (fc->dev), suffix); +if (res) { + sb->s_bdi = old_bdi; + }else { +bdi_unregister (old_bdi); +bdi_put (old_bdi); + }/* * Enable syncfs for iomap fuse servers so that we can send a final
Re: [PATCH 17/31] fuse: use an unrestricted backing device with iomap pagecache io
Quoted 34 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
With iomap support turned on for the pagecache, the kernel issues writeback to directly to block devices and we no longer have to push all those pages through the fuse device to userspace. Therefore, we don't need the tight dirty limits (~1M) that are used for regular fuse. This dramatically increases the performance of fuse's pagecache IO.
Signed-off-by "Darrick J. Wong" <[email protected]>--- fs/fuse/file_iomap.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)fs/fuse/file_iomap.c +11
-0@@ -713,6 +713,27 @@ const struct fuse_backing_ops fuse_iomap_backing_ops = {void fuse_iomap_mount (struct fuse_mount *fm) {struct fuse_conn *fc = fm->fc; +struct super_block *sb = fm->sb; +struct backing_dev_info *old_bdi = sb->s_bdi; +char *suffix = sb->s_bdev ?"-fuseblk" :"-fuse" ; +int res; + +/* +* sb->s_bdi points to the initial private bdi. However, we want to +* redirect it to a new private bdi with default dirty and readahead +* settings because iomap writeback won't be pushing a ton of dirty +* data through the fuse device. If this fails we fall back to the +* initial fuse bdi.
fs/fuse/file_iomap.c const struct fuse_backing_ops fuse_iomap_backing_ops = { · line 736
Quoted 10 lines
fs/fuse/file_iomap.c +10
-0+*/ + sb->s_bdi = &noop_backing_dev_info; + res =super_setup_bdi_name (sb,"%u:%u%s.iomap" ,MAJOR (fc->dev), +MINOR (fc->dev), suffix); +if (res) { + sb->s_bdi = old_bdi; + }else { +bdi_unregister (old_bdi); +bdi_put (old_bdi); + }
Maybe I'm missing something here, but isn't sb->s_bdi already set to noop_backing_dev_info when fuse_iomap_mount() is called? fuse_fill_super() -> fuse_fill_super_common() -> fuse_bdi_init() does this already before the fuse_iomap_mount() call, afaict. I think what we need to do is just unset BDI_CAP_STRICTLIMIT and adjust the bdi max ratio? This is more of a nit, but I think it'd also be nice if we swapped the ordering of this patch with the previous one enabling large folios, so that large folios gets enabled only when all the bdi stuff for it is ready.
Thanks,
Joanne
Quoted 4 lines
fs/fuse/file_iomap.c +0
-0/* * Enable syncfs for iomap fuse servers so that we can send a final
Re: [PATCH 17/31] fuse: use an unrestricted backing device with iomap pagecache io
Quoted 42 lines · Joanne Koong
Quoted 41 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
With iomap support turned on for the pagecache, the kernel issues writeback to directly to block devices and we no longer have to push all those pages through the fuse device to userspace. Therefore, we don't need the tight dirty limits (~1M) that are used for regular fuse. This dramatically increases the performance of fuse's pagecache IO.
Signed-off-by "Darrick J. Wong" <[email protected]>--- fs/fuse/file_iomap.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)fs/fuse/file_iomap.c +18
-0@@ -713,6 +713,27 @@ const struct fuse_backing_ops fuse_iomap_backing_ops = {void fuse_iomap_mount (struct fuse_mount *fm) {struct fuse_conn *fc = fm->fc; +struct super_block *sb = fm->sb; +struct backing_dev_info *old_bdi = sb->s_bdi; +char *suffix = sb->s_bdev ?"-fuseblk" :"-fuse" ; +int res; + +/* +* sb->s_bdi points to the initial private bdi. However, we want to +* redirect it to a new private bdi with default dirty and readahead +* settings because iomap writeback won't be pushing a ton of dirty +* data through the fuse device. If this fails we fall back to the +* initial fuse bdi. +*/ + sb->s_bdi = &noop_backing_dev_info; + res =super_setup_bdi_name (sb,"%u:%u%s.iomap" ,MAJOR (fc->dev), +MINOR (fc->dev), suffix); +if (res) { + sb->s_bdi = old_bdi; + }else {
Quoted 8 lines
fs/fuse/file_iomap.c const struct fuse_backing_ops fuse_iomap_backing_ops = { · line 736
Quoted 3 lines
fs/fuse/file_iomap.c +3
-0+bdi_unregister (old_bdi); +bdi_put (old_bdi); + }Maybe I'm missing something here, but isn't
sb->s_bdialready set tonoop_backing_dev_infowhenfuse_iomap_mount()is called?fuse_fill_super()->fuse_fill_super_common()->fuse_bdi_init()does this already before thefuse_iomap_mount()call, afaict.
Right.
Quoted 2 lines
I think what we need to do is just unset
BDI_CAP_STRICTLIMITand adjust the bdi max ratio?
That's sufficient to undo the effects of fuse_bdi_init, yes. However the BDI gets created with the name "$major:$minor{-fuseblk}" and there are "management" scripts that try to tweak fuse BDIs for better performance.
I don't want some dumb script to mismanage a fuse-iomap filesystem because it can't tell the difference, so I create a new bdi with the name "$major:$minor.iomap" to make it obvious. But super_setup_bdi_name gets cranky if s_bdi isn't set to noop and we don't want to fail a mount here due to ENOMEM so ... I implemented this weird switcheroo code.
Quoted 4 lines
This is more of a nit, but I think it'd also be nice if we swapped the ordering of this patch with the previous one enabling large folios, so that large folios gets enabled only when all the bdi stuff for it is ready.
Will do, thanks for reading these patches!
Also note that I've changed this part of the patchset quite a lot since this posting; iomap configuration is now a completely separate fuse command that gets triggered after the FUSE_INIT reply is received.
--D
Quoted 8 lines
Thanks,
JoanneQuoted 4 lines
fs/fuse/file_iomap.c +0
-0/* * Enable syncfs for iomap fuse servers so that we can send a final
Re: [PATCH 17/31] fuse: use an unrestricted backing device with iomap pagecache io
Quoted 63 lines · Darrick J. Wong
Quoted 42 lines · Joanne Koong
Quoted 41 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
With iomap support turned on for the pagecache, the kernel issues writeback to directly to block devices and we no longer have to push all those pages through the fuse device to userspace. Therefore, we don't need the tight dirty limits (~1M) that are used for regular fuse. This dramatically increases the performance of fuse's pagecache IO.
Signed-off-by "Darrick J. Wong" <[email protected]>--- fs/fuse/file_iomap.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)fs/fuse/file_iomap.c +18
-0@@ -713,6 +713,27 @@ const struct fuse_backing_ops fuse_iomap_backing_ops = {void fuse_iomap_mount (struct fuse_mount *fm) {struct fuse_conn *fc = fm->fc; +struct super_block *sb = fm->sb; +struct backing_dev_info *old_bdi = sb->s_bdi; +char *suffix = sb->s_bdev ?"-fuseblk" :"-fuse" ; +int res; + +/* +* sb->s_bdi points to the initial private bdi. However, we want to +* redirect it to a new private bdi with default dirty and readahead +* settings because iomap writeback won't be pushing a ton of dirty +* data through the fuse device. If this fails we fall back to the +* initial fuse bdi. +*/ + sb->s_bdi = &noop_backing_dev_info; + res =super_setup_bdi_name (sb,"%u:%u%s.iomap" ,MAJOR (fc->dev), +MINOR (fc->dev), suffix); +if (res) { + sb->s_bdi = old_bdi; + }else {Quoted 8 lines
fs/fuse/file_iomap.c const struct fuse_backing_ops fuse_iomap_backing_ops = { · line 736
Quoted 3 lines
fs/fuse/file_iomap.c +3
-0+bdi_unregister (old_bdi); +bdi_put (old_bdi); + }Maybe I'm missing something here, but isn't
sb->s_bdialready set tonoop_backing_dev_infowhenfuse_iomap_mount()is called?fuse_fill_super()->fuse_fill_super_common()->fuse_bdi_init()does this already before thefuse_iomap_mount()call, afaict.Right.
Quoted 2 lines
I think what we need to do is just unset
BDI_CAP_STRICTLIMITand adjust the bdi max ratio?That's sufficient to undo the effects of
fuse_bdi_init, yes. However the BDI gets created with the name "$major:$minor{-fuseblk}" and there are "management" scripts that try to tweak fuse BDIs for better performance.
Quoted 5 lines
I don't want some dumb script to mismanage a fuse-iomap filesystem because it can't tell the difference, so I create a new bdi with the name "$major:$minor.iomap" to make it obvious. But
super_setup_bdi_namegets cranky ifs_bdiisn't set to noop and we don't want to fail a mount here due to ENOMEM so ... I implemented this weird switcheroo code.
I see. It might be useful to copy/paste this into the commit message just for added context. I don't see a better way of doing it than what you have in this patch then since we rely on the init reply to know whether iomap should be used or not...
If the new bdi setup fails, I wonder if the mount should just fail entirely then. That seems better to me than letting it succeed with strictlimiting enforced, especially since large folios will be enabled for fuse iomap. [1] has some numbers for the performance degradations I saw for writes with strictlimiting on and large folios enabled.
Speaking of strictlimiting though, from a policy standpoint if we think strictlimiting is needed in general in fuse (there's a thread from last year [1] about removing strict limiting), then I think that would need to apply to iomap as well, at least for unprivileged servers.
[1] https://lore.kernel.org/linux-fsdevel/CAJnrk1bwat_r4+pmhaWH-ThAi+zoAJFwmJG65ANj1Zv0O0s4_A@mail.gmail.com/
[2] https://lore.kernel.org/linux-fsdevel/20251010150113.GC6174@frogsfrogsfrogs/T/#ma34ff5ae338a83f8b2e946d7e5332ea835fa0ff6
Quoted 11 lines
Quoted 5 lines
This is more of a nit, but I think it'd also be nice if we swapped the ordering of this patch with the previous one enabling large folios, so that large folios gets enabled only when all the bdi stuff for it is ready.
Will do, thanks for reading these patches!
Also note that I've changed this part of the patchset quite a lot since this posting; iomap configuration is now a completely separate fuse command that gets triggered after the
FUSE_INITreply is received.
Great, I'll look at your upstream tree then for this part.
Thanks,
Joanne
Quoted 11 lines
--D
Quoted 8 lines
Thanks,
JoanneQuoted 4 lines
fs/fuse/file_iomap.c +0
-0/* * Enable syncfs for iomap fuse servers so that we can send a final
Re: [PATCH 17/31] fuse: use an unrestricted backing device with iomap pagecache io
Quoted 70 lines · Joanne Koong
Quoted 63 lines · Darrick J. Wong
Quoted 42 lines · Joanne Koong
Quoted 41 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
With iomap support turned on for the pagecache, the kernel issues writeback to directly to block devices and we no longer have to push all those pages through the fuse device to userspace. Therefore, we don't need the tight dirty limits (~1M) that are used for regular fuse. This dramatically increases the performance of fuse's pagecache IO.
Signed-off-by "Darrick J. Wong" <[email protected]>--- fs/fuse/file_iomap.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)fs/fuse/file_iomap.c +18
-0@@ -713,6 +713,27 @@ const struct fuse_backing_ops fuse_iomap_backing_ops = {void fuse_iomap_mount (struct fuse_mount *fm) {struct fuse_conn *fc = fm->fc; +struct super_block *sb = fm->sb; +struct backing_dev_info *old_bdi = sb->s_bdi; +char *suffix = sb->s_bdev ?"-fuseblk" :"-fuse" ; +int res; + +/* +* sb->s_bdi points to the initial private bdi. However, we want to +* redirect it to a new private bdi with default dirty and readahead +* settings because iomap writeback won't be pushing a ton of dirty +* data through the fuse device. If this fails we fall back to the +* initial fuse bdi. +*/ + sb->s_bdi = &noop_backing_dev_info; + res =super_setup_bdi_name (sb,"%u:%u%s.iomap" ,MAJOR (fc->dev), +MINOR (fc->dev), suffix); +if (res) { + sb->s_bdi = old_bdi; + }else {Quoted 8 lines
fs/fuse/file_iomap.c const struct fuse_backing_ops fuse_iomap_backing_ops = { · line 736
Quoted 3 lines
fs/fuse/file_iomap.c +3
-0+bdi_unregister (old_bdi); +bdi_put (old_bdi); + }Maybe I'm missing something here, but isn't
sb->s_bdialready set tonoop_backing_dev_infowhenfuse_iomap_mount()is called?fuse_fill_super()->fuse_fill_super_common()->fuse_bdi_init()does this already before thefuse_iomap_mount()call, afaict.Right.
Quoted 2 lines
I think what we need to do is just unset
BDI_CAP_STRICTLIMITand adjust the bdi max ratio?That's sufficient to undo the effects of
fuse_bdi_init, yes. However the BDI gets created with the name "$major:$minor{-fuseblk}" and there are "management" scripts that try to tweak fuse BDIs for better performance.Quoted 5 lines
I don't want some dumb script to mismanage a fuse-iomap filesystem because it can't tell the difference, so I create a new bdi with the name "$major:$minor.iomap" to make it obvious. But
super_setup_bdi_namegets cranky ifs_bdiisn't set to noop and we don't want to fail a mount here due to ENOMEM so ... I implemented this weird switcheroo code.
Quoted 4 lines
I see. It might be useful to copy/paste this into the commit message just for added context. I don't see a better way of doing it than what you have in this patch then since we rely on the init reply to know whether iomap should be used or not...
I'll do that. I will also add that as soon as any BDI is created, it will be exposed to userspace in sysfs. That means that running the code from fuse_bdi_init in reverse will not necessarily produce the same results as a freshly created BDI.
Quoted 2 lines
If the new bdi setup fails, I wonder if the mount should just fail entirely then. That seems better to me than letting it succeed with
Err, which new bdi setup? If fuse-iomap can't create a new BDI, it will set s_bdi back to the old one and move on. You'll get degraded performance, but that's not the end of the world.
Quoted 3 lines
strictlimiting enforced, especially since large folios will be enabled for fuse iomap. [1] has some numbers for the performance degradations I saw for writes with strictlimiting on and large folios enabled.
If fuse_bdi_init can't set up a bdi it will fail the mount.
That said... from reading [1], if strictlimiting is enabled with large folios, then can we figure out what is the effective max folio size and lower it to that?
Quoted 3 lines
Speaking of strictlimiting though, from a policy standpoint if we think strictlimiting is needed in general in fuse (there's a thread from last year [1] about removing strict limiting), then I think that
(did you mean [2] here?)
Quoted 2 lines
would need to apply to iomap as well, at least for unprivileged servers.
iomap requires a privileged server, FWIW.
Quoted 9 lines
[1] https://lore.kernel.org/linux-fsdevel/CAJnrk1bwat_r4+pmhaWH-ThAi+zoAJFwmJG65ANj1Zv0O0s4_A@mail.gmail.com/
[2] https://lore.kernel.org/linux-fsdevel/20251010150113.GC6174@frogsfrogsfrogs/T/#ma34ff5ae338a83f8b2e946d7e5332ea835fa0ff6Quoted 6 lines
Quoted 5 lines
This is more of a nit, but I think it'd also be nice if we swapped the ordering of this patch with the previous one enabling large folios, so that large folios gets enabled only when all the bdi stuff for it is ready.
Quoted 7 lines
Quoted 5 lines
Will do, thanks for reading these patches!
Also note that I've changed this part of the patchset quite a lot since this posting; iomap configuration is now a completely separate fuse command that gets triggered after the
FUSE_INITreply is received.Great, I'll look at your upstream tree then for this part.
Ok.
--D
Quoted 15 lines
Thanks,
JoanneQuoted 11 lines
--D
Quoted 8 lines
Thanks,
JoanneQuoted 4 lines
fs/fuse/file_iomap.c +0
-0/* * Enable syncfs for iomap fuse servers so that we can send a final
Re: [PATCH 17/31] fuse: use an unrestricted backing device with iomap pagecache io
Quoted 82 lines · Darrick J. Wong
Quoted 70 lines · Joanne Koong
Quoted 63 lines · Darrick J. Wong
Quoted 42 lines · Joanne Koong
Quoted 41 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
With iomap support turned on for the pagecache, the kernel issues writeback to directly to block devices and we no longer have to push all those pages through the fuse device to userspace. Therefore, we don't need the tight dirty limits (~1M) that are used for regular fuse. This dramatically increases the performance of fuse's pagecache IO.
Signed-off-by "Darrick J. Wong" <[email protected]>--- fs/fuse/file_iomap.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)fs/fuse/file_iomap.c +18
-0@@ -713,6 +713,27 @@ const struct fuse_backing_ops fuse_iomap_backing_ops = {void fuse_iomap_mount (struct fuse_mount *fm) {struct fuse_conn *fc = fm->fc; +struct super_block *sb = fm->sb; +struct backing_dev_info *old_bdi = sb->s_bdi; +char *suffix = sb->s_bdev ?"-fuseblk" :"-fuse" ; +int res; + +/* +* sb->s_bdi points to the initial private bdi. However, we want to +* redirect it to a new private bdi with default dirty and readahead +* settings because iomap writeback won't be pushing a ton of dirty +* data through the fuse device. If this fails we fall back to the +* initial fuse bdi. +*/ + sb->s_bdi = &noop_backing_dev_info; + res =super_setup_bdi_name (sb,"%u:%u%s.iomap" ,MAJOR (fc->dev), +MINOR (fc->dev), suffix); +if (res) { + sb->s_bdi = old_bdi; + }else {Quoted 8 lines
fs/fuse/file_iomap.c const struct fuse_backing_ops fuse_iomap_backing_ops = { · line 736
Quoted 3 lines
fs/fuse/file_iomap.c +3
-0+bdi_unregister (old_bdi); +bdi_put (old_bdi); + }Maybe I'm missing something here, but isn't
sb->s_bdialready set tonoop_backing_dev_infowhenfuse_iomap_mount()is called?fuse_fill_super()->fuse_fill_super_common()->fuse_bdi_init()does this already before thefuse_iomap_mount()call, afaict.Right.
Quoted 2 lines
I think what we need to do is just unset
BDI_CAP_STRICTLIMITand adjust the bdi max ratio?That's sufficient to undo the effects of
fuse_bdi_init, yes. However the BDI gets created with the name "$major:$minor{-fuseblk}" and there are "management" scripts that try to tweak fuse BDIs for better performance.Quoted 5 lines
I don't want some dumb script to mismanage a fuse-iomap filesystem because it can't tell the difference, so I create a new bdi with the name "$major:$minor.iomap" to make it obvious. But
super_setup_bdi_namegets cranky ifs_bdiisn't set to noop and we don't want to fail a mount here due to ENOMEM so ... I implemented this weird switcheroo code.Quoted 4 lines
I see. It might be useful to copy/paste this into the commit message just for added context. I don't see a better way of doing it than what you have in this patch then since we rely on the init reply to know whether iomap should be used or not...
I'll do that. I will also add that as soon as any BDI is created, it will be exposed to userspace in sysfs. That means that running the code from
fuse_bdi_initin reverse will not necessarily produce the same results as a freshly created BDI.
Quoted 6 lines
Quoted 2 lines
If the new bdi setup fails, I wonder if the mount should just fail entirely then. That seems better to me than letting it succeed with
Err, which new bdi setup? If fuse-iomap can't create a new BDI, it will set
s_bdiback to the old one and move on. You'll get degraded performance, but that's not the end of the world.
I was thinking from the user POV, I'd rather the whole mount fail (which it seems like would only be a transient failure, eg running out of memory) and I retry, than it work but have writes potentially run 10x slower (10x comes from the benchmarks Jingbo saw in [1])
Quoted 10 lines
Quoted 4 lines
strictlimiting enforced, especially since large folios will be enabled for fuse iomap. [1] has some numbers for the performance degradations I saw for writes with strictlimiting on and large folios enabled.
If
fuse_bdi_initcan't set up a bdi it will fail the mount.That said... from reading [1], if strictlimiting is enabled with large folios, then can we figure out what is the effective max folio size and lower it to that?
I'm not really sure how we figure that out, unless I guess we try to do it experimentally? The throttling logic for this is in balance_dirty_pages().
Quoted 6 lines
Quoted 4 lines
Speaking of strictlimiting though, from a policy standpoint if we think strictlimiting is needed in general in fuse (there's a thread from last year [1] about removing strict limiting), then I think that
(did you mean [2] here?)
Ah yes sorry, I had meant [2].
Quoted 5 lines
Quoted 3 lines
would need to apply to iomap as well, at least for unprivileged servers.
iomap requires a privileged server, FWIW.
Oh right, I forgot iomap only runs with privileges enabled. In that case, I think that makes the whole strictlimiting thing a lot simpler then. imo for privileged servers, we should get rid of strictlimiting entirely. Though I'm not sure how MIklos feels about that.
Thanks,
Joanne
Quoted 37 lines
Quoted 10 lines
[1] https://lore.kernel.org/linux-fsdevel/CAJnrk1bwat_r4+pmhaWH-ThAi+zoAJFwmJG65ANj1Zv0O0s4_A@mail.gmail.com/
[2] https://lore.kernel.org/linux-fsdevel/20251010150113.GC6174@frogsfrogsfrogs/T/#ma34ff5ae338a83f8b2e946d7e5332ea835fa0ff6Quoted 6 lines
Quoted 5 lines
This is more of a nit, but I think it'd also be nice if we swapped the ordering of this patch with the previous one enabling large folios, so that large folios gets enabled only when all the bdi stuff for it is ready.
Quoted 7 lines
Quoted 5 lines
Will do, thanks for reading these patches!
Also note that I've changed this part of the patchset quite a lot since this posting; iomap configuration is now a completely separate fuse command that gets triggered after the
FUSE_INITreply is received.Great, I'll look at your upstream tree then for this part.
Ok.
--D
Quoted 15 lines
Thanks,
JoanneQuoted 11 lines
--D
Quoted 8 lines
Thanks,
JoanneQuoted 4 lines
fs/fuse/file_iomap.c +0
-0/* * Enable syncfs for iomap fuse servers so that we can send a final
Re: [PATCH 17/31] fuse: use an unrestricted backing device with iomap pagecache io
Quoted 86 lines · Joanne Koong
Quoted 85 lines · Darrick J. Wong
Quoted 70 lines · Joanne Koong
Quoted 63 lines · Darrick J. Wong
Quoted 42 lines · Joanne Koong
Quoted 41 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
With iomap support turned on for the pagecache, the kernel issues writeback to directly to block devices and we no longer have to push all those pages through the fuse device to userspace. Therefore, we don't need the tight dirty limits (~1M) that are used for regular fuse. This dramatically increases the performance of fuse's pagecache IO.
Signed-off-by "Darrick J. Wong" <[email protected]>--- fs/fuse/file_iomap.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)fs/fuse/file_iomap.c +18
-0@@ -713,6 +713,27 @@ const struct fuse_backing_ops fuse_iomap_backing_ops = {void fuse_iomap_mount (struct fuse_mount *fm) {struct fuse_conn *fc = fm->fc; +struct super_block *sb = fm->sb; +struct backing_dev_info *old_bdi = sb->s_bdi; +char *suffix = sb->s_bdev ?"-fuseblk" :"-fuse" ; +int res; + +/* +* sb->s_bdi points to the initial private bdi. However, we want to +* redirect it to a new private bdi with default dirty and readahead +* settings because iomap writeback won't be pushing a ton of dirty +* data through the fuse device. If this fails we fall back to the +* initial fuse bdi. +*/ + sb->s_bdi = &noop_backing_dev_info; + res =super_setup_bdi_name (sb,"%u:%u%s.iomap" ,MAJOR (fc->dev), +MINOR (fc->dev), suffix); +if (res) { + sb->s_bdi = old_bdi; + }else {Quoted 8 lines
fs/fuse/file_iomap.c const struct fuse_backing_ops fuse_iomap_backing_ops = { · line 736
Quoted 3 lines
fs/fuse/file_iomap.c +3
-0+bdi_unregister (old_bdi); +bdi_put (old_bdi); + }Maybe I'm missing something here, but isn't
sb->s_bdialready set tonoop_backing_dev_infowhenfuse_iomap_mount()is called?fuse_fill_super()->fuse_fill_super_common()->fuse_bdi_init()does this already before thefuse_iomap_mount()call, afaict.Right.
Quoted 2 lines
I think what we need to do is just unset
BDI_CAP_STRICTLIMITand adjust the bdi max ratio?That's sufficient to undo the effects of
fuse_bdi_init, yes. However the BDI gets created with the name "$major:$minor{-fuseblk}" and there are "management" scripts that try to tweak fuse BDIs for better performance.Quoted 5 lines
I don't want some dumb script to mismanage a fuse-iomap filesystem because it can't tell the difference, so I create a new bdi with the name "$major:$minor.iomap" to make it obvious. But
super_setup_bdi_namegets cranky ifs_bdiisn't set to noop and we don't want to fail a mount here due to ENOMEM so ... I implemented this weird switcheroo code.Quoted 4 lines
I see. It might be useful to copy/paste this into the commit message just for added context. I don't see a better way of doing it than what you have in this patch then since we rely on the init reply to know whether iomap should be used or not...
I'll do that. I will also add that as soon as any BDI is created, it will be exposed to userspace in sysfs. That means that running the code from
fuse_bdi_initin reverse will not necessarily produce the same results as a freshly created BDI.Quoted 2 lines
If the new bdi setup fails, I wonder if the mount should just fail entirely then. That seems better to me than letting it succeed with
Quoted 8 lines
Quoted 3 lines
Err, which new bdi setup? If fuse-iomap can't create a new BDI, it will set
s_bdiback to the old one and move on. You'll get degraded performance, but that's not the end of the world.I was thinking from the user POV, I'd rather the whole mount fail (which it seems like would only be a transient failure, eg running out of memory) and I retry, than it work but have writes potentially run 10x slower (10x comes from the benchmarks Jingbo saw in [1])
Hrmm. The difficulty of preallocating the iomap bdi is that I think you'd have to do it in fuse_bdi_init, which occurs before the kernel has even seen the reply to FUSE_INIT and therefore knows if the fuse server even cares about iomap.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
strictlimiting enforced, especially since large folios will be enabled for fuse iomap. [1] has some numbers for the performance degradations I saw for writes with strictlimiting on and large folios enabled.
If
fuse_bdi_initcan't set up a bdi it will fail the mount.
Quoted 7 lines
Quoted 3 lines
That said... from reading [1], if strictlimiting is enabled with large folios, then can we figure out what is the effective max folio size and lower it to that?
I'm not really sure how we figure that out, unless I guess we try to do it experimentally? The throttling logic for this is in
balance_dirty_pages().
Oh, I see, it's a dynamic limit. I don't know how to deal with that either.
Quoted 11 lines
Quoted 5 lines
Quoted 3 lines
Speaking of strictlimiting though, from a policy standpoint if we think strictlimiting is needed in general in fuse (there's a thread from last year [1] about removing strict limiting), then I think that
(did you mean [2] here?)
Ah yes sorry, I had meant [2].
Quoted 4 lines
Quoted 3 lines
would need to apply to iomap as well, at least for unprivileged servers.
Quoted 6 lines
Quoted 1 line
iomap requires a privileged server, FWIW.
Oh right, I forgot iomap only runs with privileges enabled. In that case, I think that makes the whole strictlimiting thing a lot simpler then. imo for privileged servers, we should get rid of strictlimiting entirely. Though I'm not sure how MIklos feels about that.
<nod>
--D
Quoted 41 lines
Thanks,
JoanneQuoted 37 lines
Quoted 10 lines
[1] https://lore.kernel.org/linux-fsdevel/CAJnrk1bwat_r4+pmhaWH-ThAi+zoAJFwmJG65ANj1Zv0O0s4_A@mail.gmail.com/
[2] https://lore.kernel.org/linux-fsdevel/20251010150113.GC6174@frogsfrogsfrogs/T/#ma34ff5ae338a83f8b2e946d7e5332ea835fa0ff6Quoted 6 lines
Quoted 5 lines
This is more of a nit, but I think it'd also be nice if we swapped the ordering of this patch with the previous one enabling large folios, so that large folios gets enabled only when all the bdi stuff for it is ready.
Quoted 7 lines
Quoted 5 lines
Will do, thanks for reading these patches!
Also note that I've changed this part of the patchset quite a lot since this posting; iomap configuration is now a completely separate fuse command that gets triggered after the
FUSE_INITreply is received.Great, I'll look at your upstream tree then for this part.
Ok.
--D
Quoted 15 lines
Thanks,
JoanneQuoted 11 lines
--D
Quoted 8 lines
Thanks,
JoanneQuoted 4 lines
fs/fuse/file_iomap.c +0
-0/* * Enable syncfs for iomap fuse servers so that we can send a final
PATCH 18/31 fuse: advertise support for iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Advertise our new IO paths programmatically by creating an ioctl that can return the capabilities of the kernel.
--- fs/fuse/fuse_i.h | 4 ++++ include/uapi/linux/fuse.h | 9 +++++++++ fs/fuse/dev.c | 3 +++ fs/fuse/file_iomap.c | 13 +++++++++++++ 4 files changed, 29 insertions(+)
fs/fuse/fuse_i.h +4 -0
@@ -1784,6 +1784,9 @@ int fuse_iomap_fallocate(struct file *file, int mode, loff_t offset, loff_t length, loff_t new_size);int fuse_iomap_flush_unmap_range (struct inode *inode,loff_t pos,loff_t endpos); + +int fuse_dev_ioctl_iomap_support (struct file *file, +struct fuse_iomap_support __user *argp);#else # define fuse_iomap_enabled (...) (false)# define fuse_has_iomap (...) (false)
@@ -1807,6 +1810,7 @@ int fuse_iomap_flush_unmap_range(struct inode *inode, loff_t pos,# define fuse_iomap_setsize_start (...) (-ENOSYS)# define fuse_iomap_fallocate (...) (-ENOSYS)# define fuse_iomap_flush_unmap_range (...) (-ENOSYS) +# define fuse_dev_ioctl_iomap_support (...) (-EOPNOTSUPP)#endif #endif /* _FS_FUSE_I_H */
include/uapi/linux/fuse.h +9 -0
@@ -1151,6 +1151,13 @@ struct fuse_backing_map {uint64_t padding; }; +/* basic file I/O functionality through iomap */ +#define FUSE_IOMAP_SUPPORT_FILEIO (1ULL << 0) +struct fuse_iomap_support { +uint64_t flags; +uint64_t padding; +}; +/* Device ioctls: */ #define FUSE_DEV_IOC_MAGIC 229#define FUSE_DEV_IOC_CLONE _IOR(FUSE_DEV_IOC_MAGIC, 0, uint32_t)
@@ -1158,6 +1165,8 @@ struct fuse_backing_map { struct fuse_backing_map)#define FUSE_DEV_IOC_BACKING_CLOSE _IOW(FUSE_DEV_IOC_MAGIC, 2, uint32_t)#define FUSE_DEV_IOC_SYNC_INIT _IO(FUSE_DEV_IOC_MAGIC, 3) +#define FUSE_DEV_IOC_IOMAP_SUPPORT _IOR(FUSE_DEV_IOC_MAGIC, 99, \ + struct fuse_iomap_support)struct fuse_lseek_in {uint64_t fh;
fs/fuse/dev.c +3 -0
@@ -2719,6 +2719,9 @@ static long fuse_dev_ioctl(struct file *file, unsigned int cmd,case FUSE_DEV_IOC_SYNC_INIT :return fuse_dev_ioctl_sync_init (file); +case FUSE_DEV_IOC_IOMAP_SUPPORT : +return fuse_dev_ioctl_iomap_support (file, argp); +default :return -ENOTTY ; }
fs/fuse/file_iomap.c +13 -0
@@ -1716,3 +1716,16 @@ fuse_iomap_fallocate(file_update_time (file);return 0 ; } + +int fuse_dev_ioctl_iomap_support (struct file *file, +struct fuse_iomap_support __user *argp) +{ +struct fuse_iomap_support ios = { }; + +if (fuse_iomap_enabled ()) + ios.flags =FUSE_IOMAP_SUPPORT_FILEIO ; + +if (copy_to_user (argp, &ios,sizeof (ios))) +return -EFAULT ; +return 0 ; +}
PATCH 19/31 fuse: query filesystem geometry when using iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add a new upcall to the fuse server so that the kernel can request filesystem geometry bits when iomap mode is in use.
--- fs/fuse/fuse_i.h | 10 ++- include/uapi/linux/fuse.h | 39 ++++++++++++ fs/fuse/file_iomap.c | 147 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 42 ++++++++++--- 4 files changed, 227 insertions(+), 11 deletions(-)
fs/fuse/fuse_i.h +8 -2
@@ -1025,6 +1025,9 @@ struct fuse_conn {struct fuse_ring *ring;#endif +/** How many subsystems still need initialization? */ +atomic_t need_init; +/** Only used if the connection opts into request timeouts */ struct {/* Worker for checking if any requests have timed out */
@@ -1429,6 +1432,7 @@ struct fuse_dev *fuse_dev_alloc(void);void fuse_dev_install (struct fuse_dev *fud,struct fuse_conn *fc);void fuse_dev_free (struct fuse_dev *fud);int fuse_send_init (struct fuse_mount *fm); +void fuse_finish_init (struct fuse_conn *fc,bool ok);/** * Fill in superblock and initialize fuse connection
@@ -1741,7 +1745,8 @@ static inline bool fuse_has_iomap(const struct inode *inode)extern const struct fuse_backing_ops fuse_iomap_backing_ops; -void fuse_iomap_mount (struct fuse_mount *fm); +int fuse_iomap_mount (struct fuse_mount *fm); +void fuse_iomap_mount_async (struct fuse_mount *fm);void fuse_iomap_unmount (struct fuse_mount *fm);void fuse_iomap_init_reg_inode (struct inode *inode,unsigned attr_flags);
@@ -1790,7 +1795,8 @@ int fuse_dev_ioctl_iomap_support(struct file *file,#else # define fuse_iomap_enabled (...) (false)# define fuse_has_iomap (...) (false) -# define fuse_iomap_mount (...) ((void)0) +# define fuse_iomap_mount (...) (0) +# define fuse_iomap_mount_async (...) ((void)0)# define fuse_iomap_unmount (...) ((void)0)# define fuse_iomap_init_reg_inode (...) ((void)0)# define fuse_iomap_init_nonreg_inode (...) ((void)0)
include/uapi/linux/fuse.h +39 -0
@@ -244,6 +244,7 @@* 7.99 * - add FUSE_IOMAP and iomap_{begin,end,ioend} for regular file operations * - add FUSE_ATTR_IOMAP to enable iomap for specific inodes +* - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry */ #ifndef _LINUX_FUSE_H
@@ -672,6 +673,7 @@ enum fuse_opcode {FUSE_STATX =52 ,FUSE_COPY_FILE_RANGE_64 =53 , +FUSE_IOMAP_CONFIG =4092 ,FUSE_IOMAP_IOEND =4093 ,FUSE_IOMAP_BEGIN =4094 , FUSE_IOMAP_END = 4095,
@@ -1442,4 +1444,41 @@ struct fuse_iomap_ioend_in { uint32_t reserved1; /* zero */ }; +struct fuse_iomap_config_in { +uint64_t flags;/* supported FUSE_IOMAP_CONFIG_* flags */ +int64_t maxbytes;/* maximum supported file size */ +uint64_t padding[6 ];/* zero */ +}; + +/* Which fields are set in fuse_iomap_config_out? */ +#define FUSE_IOMAP_CONFIG_SID (1 << 0ULL) +#define FUSE_IOMAP_CONFIG_UUID (1 << 1ULL) +#define FUSE_IOMAP_CONFIG_BLOCKSIZE (1 << 2ULL) +#define FUSE_IOMAP_CONFIG_MAX_LINKS (1 << 3ULL) +#define FUSE_IOMAP_CONFIG_TIME (1 << 4ULL) +#define FUSE_IOMAP_CONFIG_MAXBYTES (1 << 5ULL) + +struct fuse_iomap_config_out { +uint64_t flags;/* FUSE_IOMAP_CONFIG_* */ + +char s_id[32 ];/* Informational name */ +char s_uuid[16 ];/* UUID */ + +uint8_t s_uuid_len;/* length of s_uuid */ + +uint8_t s_pad[3 ];/* must be zeroes */ + +uint32_t s_blocksize;/* fs block size */ +uint32_t s_max_links;/* max hard links */ + +/* Granularity of c/m/atime in ns (cannot be worse than a second) */ +uint32_t s_time_gran; + +/* Time limits for c/m/atime in seconds */ +int64_t s_time_min; +int64_t s_time_max; + +int64_t s_maxbytes;/* max file size */ +}; +#endif /* _LINUX_FUSE_H */
fs/fuse/file_iomap.c +146 -1
166 lines not on this page · raw message
fs/fuse/inode.c +34 -8
69 lines not on this page · raw message
Re: [PATCH 19/31] fuse: query filesystem geometry when using iomap
Quoted 5 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Add a new upcall to the fuse server so that the kernel can request filesystem geometry bits when iomap mode is in use.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit d529d13100b2e00c33a507c371b6c1b19f2206ae
Author: Darrick J. Wong <[email protected]>
fuse_trace: query filesystem geometry when using iomap
This commit adds tracepoints for the FUSE_IOMAP_CONFIG operation, enabling tracing of filesystem geometry queries when using iomap with FUSE. The tracepoint captures configuration flags, block size, link limits, time granularity and ranges, maximum file size, and UUID length.
Quoted 12 lines
fs/fuse/fuse_iomap.c +2
-0@@ -743,6 +743,8 @@ static int fuse_iomap_process_config(struct fuse_mount *fm, int error,return error; } +trace_fuse_iomap_config (fm, outarg); +if (outarg->flags & ~FUSE_IOMAP_CONFIG_ALL )return -EINVAL ;
[ ... ]
Quoted 3 lines
fs/fuse/fuse_trace.h +0
-0
[ ... ]
Quoted 17 lines
fs/fuse/fuse_trace.h +13
-0@@ -1005,6 +1014,45 @@ __entry->mode, __entry->newsize) ); + +TRACE_EVENT(fuse_iomap_config, + TP_PROTO(const struct fuse_mount *fm, + const struct fuse_iomap_config_out *outarg), + TP_ARGS(fm, outarg), + + TP_STRUCT__entry( + __field(dev_t, connection) + + __field(uint32_t, flags) + __field(uint32_t, blocksize) + __field(uint32_t, max_links) + __field(uint32_t, time_gran)
fs/fuse/fuse_trace.h line 1039
Quoted 10 lines
fs/fuse/fuse_trace.h +10
-0+ + __field(int64_t, time_min) + __field(int64_t, time_max) + __field(int64_t, maxbytes) +__field (uint8_t, uuid_len) + ), + +TP_fast_assign ( + __entry->connection = fm->fc->dev; + __entry->flags = outarg->flags;
^^^^
Does this truncate the upper 32 bits of the flags field? The flags field in struct fuse_iomap_config_out (include/uapi/linux/fuse.h:1471) is defined as uint64_t:
struct fuse_iomap_config_out {
uint64_t flags; /* FUSE_IOMAP_CONFIG_* */
...
}But the trace event declares it as uint32_t in TP_STRUCT__entry. While current flag definitions (FUSE_IOMAP_CONFIG_SID through FUSE_IOMAP_CONFIG_MAXBYTES) only use bits 0-5, using uint32_t will lose the upper 32 bits if future flags use them.
Should the trace event field be declared as uint64_t to match the structure definition?
Re: [PATCH 19/31] fuse: query filesystem geometry when using iomap
Quoted 89 lines · Chris Mason
Quoted 5 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Add a new upcall to the fuse server so that the kernel can request filesystem geometry bits when iomap mode is in use.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit
d529d13100b2e00c33a507c371b6c1b19f2206ae
Author: Darrick J. Wong <[email protected]>
fuse_trace: query filesystem geometry when using iomapThis commit adds tracepoints for the
FUSE_IOMAP_CONFIGoperation, enabling tracing of filesystem geometry queries when using iomap with FUSE. The tracepoint captures configuration flags, block size, link limits, time granularity and ranges, maximum file size, and UUID length.Quoted 12 lines
fs/fuse/fuse_iomap.c +2
-0@@ -743,6 +743,8 @@ static int fuse_iomap_process_config(struct fuse_mount *fm, int error,return error; } +trace_fuse_iomap_config (fm, outarg); +if (outarg->flags & ~FUSE_IOMAP_CONFIG_ALL )return -EINVAL ;[ ... ]
Quoted 3 lines
fs/fuse/fuse_trace.h +0
-0[ ... ]
Quoted 17 lines
fs/fuse/fuse_trace.h +13
-0@@ -1005,6 +1014,45 @@ __entry->mode, __entry->newsize) ); + +TRACE_EVENT(fuse_iomap_config, + TP_PROTO(const struct fuse_mount *fm, + const struct fuse_iomap_config_out *outarg), + TP_ARGS(fm, outarg), + + TP_STRUCT__entry( + __field(dev_t, connection) + + __field(uint32_t, flags) + __field(uint32_t, blocksize) + __field(uint32_t, max_links) + __field(uint32_t, time_gran)fs/fuse/fuse_trace.h line 1039
Quoted 10 lines
fs/fuse/fuse_trace.h +10
-0+ + __field(int64_t, time_min) + __field(int64_t, time_max) + __field(int64_t, maxbytes) +__field (uint8_t, uuid_len) + ), + +TP_fast_assign ( + __entry->connection = fm->fc->dev; + __entry->flags = outarg->flags;^^^^Does this truncate the upper 32 bits of the flags field? The flags field in struct
fuse_iomap_config_out(include/uapi/linux/fuse.h:1471) is defined asuint64_t:struct fuse_iomap_config_out { uint64_t flags; /* FUSE_IOMAP_CONFIG_* */ ... }
Quoted 7 lines
But the trace event declares it as
uint32_tinTP_STRUCT__entry. While current flag definitions (FUSE_IOMAP_CONFIG_SIDthroughFUSE_IOMAP_CONFIG_MAXBYTES) only use bits 0-5, usinguint32_twill lose the upper 32 bits if future flags use them.Should the trace event field be declared as
uint64_tto match the structure definition?
Yes. Fixed, thanks!
--D
PATCH 20/31 fuse_trace: query filesystem geometry when using iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/file_iomap.c | 3 +++ 2 files changed, 51 insertions(+)
fs/fuse/fuse_trace.h +48 -0
@@ -58,6 +58,7 @@ EM( FUSE_SYNCFS, "FUSE_SYNCFS") \ EM( FUSE_TMPFILE, "FUSE_TMPFILE") \ EM( FUSE_STATX, "FUSE_STATX") \ + EM( FUSE_IOMAP_CONFIG, "FUSE_IOMAP_CONFIG") \ EM( FUSE_IOMAP_BEGIN, "FUSE_IOMAP_BEGIN") \ EM( FUSE_IOMAP_END, "FUSE_IOMAP_END") \EM (FUSE_IOMAP_IOEND ,"FUSE_IOMAP_IOEND" ) \
@@ -342,6 +343,14 @@ TRACE_DEFINE_ENUM(FUSE_I_IOMAP); { IOMAP_IOEND_BOUNDARY, "boundary" }, \ { IOMAP_IOEND_DIRECT, "direct" } +#define FUSE_IOMAP_CONFIG_STRINGS \ + { FUSE_IOMAP_CONFIG_SID, "sid" }, \ + { FUSE_IOMAP_CONFIG_UUID, "uuid" }, \ + { FUSE_IOMAP_CONFIG_BLOCKSIZE, "blocksize" }, \ + { FUSE_IOMAP_CONFIG_MAX_LINKS, "max_links" }, \ + { FUSE_IOMAP_CONFIG_TIME, "time" }, \ + { FUSE_IOMAP_CONFIG_MAXBYTES, "maxbytes" } + DECLARE_EVENT_CLASS(fuse_iomap_check_class, TP_PROTO(const char *func, int line, const char *condition),
@@ -970,6 +979,45 @@ TRACE_EVENT(fuse_iomap_fallocate, __entry->mode, __entry->newsize) ); + +TRACE_EVENT (fuse_iomap_config, + TP_PROTO(const struct fuse_mount *fm, + const struct fuse_iomap_config_out *outarg), +TP_ARGS (fm, outarg), + +TP_STRUCT__entry ( + __field(dev_t, connection) + + __field(uint32_t, flags) + __field(uint32_t, blocksize) + __field(uint32_t, max_links) + __field(uint32_t, time_gran) + + __field(int64_t, time_min) + __field(int64_t, time_max) + __field(int64_t, maxbytes) +__field (uint8_t, uuid_len) + ), + +TP_fast_assign ( + __entry->connection = fm->fc->dev; + __entry->flags = outarg->flags; + __entry->blocksize = outarg->s_blocksize; + __entry->max_links = outarg->s_max_links; + __entry->time_gran = outarg->s_time_gran; + __entry->time_min = outarg->s_time_min; + __entry->time_max = outarg->s_time_max; + __entry->maxbytes = outarg->s_maxbytes; + __entry->uuid_len = outarg->s_uuid_len; + ), + +TP_printk ("connection %u flags (%s) blocksize 0x%x max_links %u time_gran %u time_min %lld time_max %lld maxbytes 0x%llx uuid_len %u" , + __entry->connection, +__print_flags (__entry->flags,"|" ,FUSE_IOMAP_CONFIG_STRINGS ), + __entry->blocksize, __entry->max_links, __entry->time_gran, + __entry->time_min, __entry->time_max, __entry->maxbytes, + __entry->uuid_len) +);#endif /* CONFIG_FUSE_IOMAP */ #endif /* _TRACE_FUSE_H */
fs/fuse/file_iomap.c +3 -0
@@ -737,6 +737,8 @@ static int fuse_iomap_process_config(struct fuse_mount *fm, int error,return error; } +trace_fuse_iomap_config (fm, outarg); +if (outarg->flags & ~FUSE_IOMAP_CONFIG_ALL )return -EINVAL ;
@@ -762,6 +764,7 @@ static int fuse_iomap_process_config(struct fuse_mount *fm, int error, sb->s_blocksize = outarg->s_blocksize; sb->s_blocksize_bits =blksize_bits (outarg->s_blocksize); } + fm->fc->blkbits = sb->s_blocksize_bits; }if (outarg->flags &FUSE_IOMAP_CONFIG_SID )
PATCH 21/31 fuse: implement fadvise for iomap files
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
If userspace asks us to perform readahead on a file, take i_rwsem so that it can't race with hole punching or writes.
--- fs/fuse/fuse_i.h | 3 +++ fs/fuse/file.c | 1 + fs/fuse/file_iomap.c | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+)
fs/fuse/fuse_i.h +3 -0
@@ -1792,6 +1792,8 @@ int fuse_iomap_flush_unmap_range(struct inode *inode, loff_t pos,int fuse_dev_ioctl_iomap_support (struct file *file,struct fuse_iomap_support __user *argp); + +int fuse_iomap_fadvise (struct file *file,loff_t start,loff_t end,int advice);#else # define fuse_iomap_enabled (...) (false)# define fuse_has_iomap (...) (false)
@@ -1817,6 +1819,7 @@ int fuse_dev_ioctl_iomap_support(struct file *file,# define fuse_iomap_fallocate (...) (-ENOSYS)# define fuse_iomap_flush_unmap_range (...) (-ENOSYS)# define fuse_dev_ioctl_iomap_support (...) (-EOPNOTSUPP) +# define fuse_iomap_fadvise NULL#endif #endif /* _FS_FUSE_I_H */
fs/fuse/file.c +1 -0
@@ -3199,6 +3199,7 @@ static const struct file_operations fuse_file_operations = { .poll = fuse_file_poll, .fallocate = fuse_file_fallocate, .copy_file_range = fuse_copy_file_range, + .fadvise = fuse_iomap_fadvise, };static const struct address_space_operations fuse_file_aops = {
fs/fuse/file_iomap.c +20 -0
@@ -7,6 +7,7 @@#include <linux/fiemap.h> #include <linux/pagemap.h> #include <linux/falloc.h> +#include <linux/fadvise.h> #include "fuse_i.h" #include "fuse_trace.h" #include "iomap_i.h"
@@ -1877,3 +1878,22 @@ int fuse_dev_ioctl_iomap_support(struct file *file,return -EFAULT ;return 0 ; } + +int fuse_iomap_fadvise (struct file *file,loff_t start,loff_t end,int advice) +{ +struct inode *inode =file_inode (file); +bool needlock = advice ==POSIX_FADV_WILLNEED && +fuse_inode_has_iomap (inode); +int ret; + +/* +* Operations creating pages in page cache need protection from hole +* punching and similar ops +*/ +if (needlock) +inode_lock_shared (inode); + ret =generic_fadvise (file, start, end, advice); +if (needlock) +inode_unlock_shared (inode); +return ret; +}
PATCH 22/31 fuse: invalidate ranges of block devices being used for iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Make it easier to invalidate the page cache for a block device that is being used in conjunction with iomap. This allows a fuse server to kill all cached data for a block that is being freed, so that block reuse doesn't result in file corruption. Right now, the only way to do this is with fadvise, which ignores and doesn't wait for pages undergoing writeback.
--- fs/fuse/fuse_i.h | 3 +++ include/uapi/linux/fuse.h | 11 +++++++++++ fs/fuse/dev.c | 27 +++++++++++++++++++++++++++ fs/fuse/file_iomap.c | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+)
fs/fuse/fuse_i.h +3 -0
@@ -1792,6 +1792,8 @@ int fuse_iomap_flush_unmap_range(struct inode *inode, loff_t pos,int fuse_dev_ioctl_iomap_support (struct file *file,struct fuse_iomap_support __user *argp); +int fuse_iomap_dev_inval (struct fuse_conn *fc, +const struct fuse_iomap_dev_inval_out *arg);int fuse_iomap_fadvise (struct file *file,loff_t start,loff_t end,int advice);#else
@@ -1819,6 +1821,7 @@ int fuse_iomap_fadvise(struct file *file, loff_t start, loff_t end, int advice);# define fuse_iomap_fallocate (...) (-ENOSYS)# define fuse_iomap_flush_unmap_range (...) (-ENOSYS)# define fuse_dev_ioctl_iomap_support (...) (-EOPNOTSUPP) +# define fuse_iomap_dev_inval (...) (-ENOSYS)# define fuse_iomap_fadvise NULL#endif
include/uapi/linux/fuse.h +11 -0
@@ -245,6 +245,7 @@* - add FUSE_IOMAP and iomap_{begin,end,ioend} for regular file operations * - add FUSE_ATTR_IOMAP to enable iomap for specific inodes * - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry +* - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges */ #ifndef _LINUX_FUSE_H
@@ -696,6 +697,8 @@ enum fuse_notify_code {FUSE_NOTIFY_RESEND =7 ,FUSE_NOTIFY_INC_EPOCH =8 ,FUSE_NOTIFY_PRUNE =9 , +FUSE_NOTIFY_IOMAP_DEV_INVAL =99 , + FUSE_NOTIFY_CODE_MAX, };/* The read buffer is required to be at least 8k, but may be much larger */
@@ -1481,4 +1484,12 @@ struct fuse_iomap_config_out { int64_t s_maxbytes; /* max file size */ }; +struct fuse_iomap_dev_inval_out { +uint32_t dev;/* device cookie */ +uint32_t reserved;/* zero */ + +uint64_t offset;/* range to invalidate pagecache, bytes */ +uint64_t length; +}; +#endif /* _LINUX_FUSE_H */
fs/fuse/dev.c +27 -0
@@ -1843,6 +1843,30 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,return err; } +static int fuse_notify_iomap_dev_inval (struct fuse_conn *fc,unsigned int size, +struct fuse_copy_state *cs) +{ +struct fuse_iomap_dev_inval_out outarg; +int err = -EINVAL ; + +if (size !=sizeof (outarg)) + goto err; + + err =fuse_copy_one (cs, &outarg,sizeof (outarg)); +if (err) + goto err; +if (outarg.reserved) { + err = -EINVAL ; + goto err; + } +fuse_copy_finish (cs); + +return fuse_iomap_dev_inval (fc, &outarg); +err: +fuse_copy_finish (cs); +return err; +} +struct fuse_retrieve_args {struct fuse_args_pages ap;struct fuse_notify_retrieve_in inarg;
@@ -2123,6 +2147,9 @@ static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,case FUSE_NOTIFY_PRUNE :return fuse_notify_prune (fc, size, cs); +case FUSE_NOTIFY_IOMAP_DEV_INVAL : +return fuse_notify_iomap_dev_inval (fc, size, cs); +default :return -EINVAL ; }
fs/fuse/file_iomap.c +40 -0
@@ -1897,3 +1897,43 @@ int fuse_iomap_fadvise(struct file *file, loff_t start, loff_t end, int advice)inode_unlock_shared (inode);return ret; } + +int fuse_iomap_dev_inval (struct fuse_conn *fc, +const struct fuse_iomap_dev_inval_out *arg) +{ +struct fuse_backing *fb; +struct block_device *bdev; +loff_t end; +int ret =0 ; + +if (!fc->iomap || arg->dev ==FUSE_IOMAP_DEV_NULL ) +return -EINVAL ; + +down_read (&fc->killsb); + fb =fuse_backing_lookup (fc, &fuse_iomap_backing_ops, arg->dev); +if (!fb) { + ret = -ENODEV ; + goto out_killsb; + } + bdev = fb->bdev; + +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping); + +if (check_add_overflow (arg->offset, arg->length, &end) || + arg->offset >=bdev_nr_bytes (bdev)) { + ret = -EINVAL ; + goto out_unlock; + } + + end =min (end,bdev_nr_bytes (bdev)); +truncate_inode_pages_range (bdev->bd_mapping, arg->offset, end -1 ); + +out_unlock: +filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host); +fuse_backing_put (fb); +out_killsb: +up_read (&fc->killsb); +return ret; +}
PATCH 23/31 fuse_trace: invalidate ranges of block devices being used for iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 26 ++++++++++++++++++++++++++ fs/fuse/file_iomap.c | 2 ++ 2 files changed, 28 insertions(+)
fs/fuse/fuse_trace.h +26 -0
@@ -1018,6 +1018,32 @@ TRACE_EVENT(fuse_iomap_config, __entry->time_min, __entry->time_max, __entry->maxbytes, __entry->uuid_len) ); + +TRACE_EVENT (fuse_iomap_dev_inval, + TP_PROTO(const struct fuse_conn *fc, + const struct fuse_iomap_dev_inval_out *arg), +TP_ARGS (fc, arg), + +TP_STRUCT__entry ( + __field(dev_t, connection) + __field(int, dev) + __field(unsigned long long, offset) + __field(unsigned long long, length) + ), + +TP_fast_assign ( + __entry->connection = fc->dev; + __entry->dev = arg->dev; + __entry->offset = arg->offset; + __entry->length = arg->length; + ), + +TP_printk ("connection %u dev %d offset 0x%llx length 0x%llx" , + __entry->connection, + __entry->dev, + __entry->offset, + __entry->length) +);#endif /* CONFIG_FUSE_IOMAP */ #endif /* _TRACE_FUSE_H */
fs/fuse/file_iomap.c +2 -0
@@ -1906,6 +1906,8 @@ int fuse_iomap_dev_inval(struct fuse_conn *fc,loff_t end;int ret =0 ; +trace_fuse_iomap_dev_inval (fc, arg); +if (!fc->iomap || arg->dev ==FUSE_IOMAP_DEV_NULL )return -EINVAL ;
PATCH 24/31 fuse: implement inline data file IO via iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Implement inline data file IO by issuing FUSE_READ/FUSE_WRITE commands in response to an inline data mapping.
--- fs/fuse/file_iomap.c | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+)
fs/fuse/file_iomap.c +184 -0
@@ -417,6 +417,150 @@ fuse_iomap_find_dev(struct fuse_conn *fc, const struct fuse_iomap_io *map)return ret; } +static inline int fuse_iomap_inline_alloc (struct iomap *iomap) +{ +ASSERT (iomap->inline_data ==NULL ); +ASSERT (iomap->length >0 ); + + iomap->inline_data =kvzalloc (iomap->length,GFP_KERNEL ); +return iomap->inline_data ?0 : -ENOMEM ; +} + +static inline void fuse_iomap_inline_free (struct iomap *iomap) +{ +kvfree (iomap->inline_data); + iomap->inline_data =NULL ; +} + +/* +* Use the FUSE_READ command to read inline file data from the fuse server. +* Note that there's no file handle attached, so the fuse server must be able +* to reconnect to the inode via the nodeid. +*/ +static int fuse_iomap_inline_read (struct inode *inode,loff_t pos, +loff_t count,struct iomap *iomap) +{ +struct fuse_read_in in = { + .offset = pos, + .size = count, + }; +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_mount *fm =get_fuse_mount (inode); +FUSE_ARGS (args); +ssize_t ret; + +if (BAD_DATA (!iomap_inline_data_valid (iomap))) +return -EFSCORRUPTED ; + + args.opcode =FUSE_READ ; + args.nodeid = fi->nodeid; + args.in_numargs =1 ; + args.in_args[0 ].size =sizeof (in); + args.in_args[0 ].value = ∈ + args.out_argvar = true; + args.out_numargs =1 ; + args.out_args[0 ].size = count; + args.out_args[0 ].value =iomap_inline_data (iomap, pos); + + ret =fuse_simple_request (fm, &args); +if (ret <0 ) { +fuse_iomap_inline_free (iomap); +return ret; + } +/* no readahead means something bad happened */ +if (ret ==0 ) { +fuse_iomap_inline_free (iomap); +return -EIO ; + } + +return 0 ; +} + +/* +* Use the FUSE_WRITE command to write inline file data from the fuse server. +* Note that there's no file handle attached, so the fuse server must be able +* to reconnect to the inode via the nodeid. +*/ +static int fuse_iomap_inline_write (struct inode *inode,loff_t pos, +loff_t count,struct iomap *iomap) +{ +struct fuse_write_in in = { + .offset = pos, + .size = count, + }; +struct fuse_write_out out = { }; +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_mount *fm =get_fuse_mount (inode); +FUSE_ARGS (args); +ssize_t ret; + +if (BAD_DATA (!iomap_inline_data_valid (iomap))) +return -EFSCORRUPTED ; + + args.opcode =FUSE_WRITE ; + args.nodeid = fi->nodeid; + args.in_numargs =2 ; + args.in_args[0 ].size =sizeof (in); + args.in_args[0 ].value = ∈ + args.in_args[1 ].size = count; + args.in_args[1 ].value =iomap_inline_data (iomap, pos); + args.out_numargs =1 ; + args.out_args[0 ].size =sizeof (out); + args.out_args[0 ].value = &out; + + ret =fuse_simple_request (fm, &args); +if (ret <0 ) { +fuse_iomap_inline_free (iomap); +return ret; + } +/* short write means something bad happened */ +if (out.size < count) { +fuse_iomap_inline_free (iomap); +return -EIO ; + } + +return 0 ; +} + +/* Set up inline data buffers for iomap_begin */ +static int fuse_iomap_set_inline (struct inode *inode,unsigned opflags, +loff_t pos,loff_t count, +struct iomap *iomap,struct iomap *srcmap) +{ +int err; + +if (opflags &IOMAP_REPORT ) +return 0 ; + +if (fuse_is_iomap_file_write (opflags)) { +if (iomap->type ==IOMAP_INLINE ) { + err =fuse_iomap_inline_alloc (iomap); +if (err) +return err; + } + +if (srcmap->type ==IOMAP_INLINE ) { + err =fuse_iomap_inline_alloc (srcmap); +if (!err) + err =fuse_iomap_inline_read (inode, pos, count, + srcmap); +if (err) { +fuse_iomap_inline_free (iomap); +return err; + } + } + }else if (iomap->type ==IOMAP_INLINE ) { +/* inline data read */ + err =fuse_iomap_inline_alloc (iomap); +if (!err) + err =fuse_iomap_inline_read (inode, pos, count, iomap); +if (err) +return err; + } + +return 0 ; +} +static int fuse_iomap_begin (struct inode *inode,loff_t pos,loff_t count,unsigned opflags,struct iomap *iomap,struct iomap *srcmap)
@@ -486,12 +630,20 @@ static int fuse_iomap_begin(struct inode *inode, loff_t pos, loff_t count,fuse_iomap_from_server (inode, iomap, read_dev, &outarg.read); } +if (iomap->type ==IOMAP_INLINE || srcmap->type ==IOMAP_INLINE ) { + err =fuse_iomap_set_inline (inode, opflags, pos, count, iomap, + srcmap); +if (err) + goto out_write_dev; + } +/* * XXX: if we ever want to support closing devices, we need a way to * track the fuse_backing refcount all the way through bio endios. * For now we put the refcount here because you can't remove an iomap * device until unmount time. */ +out_write_dev:fuse_backing_put (write_dev); out_read_dev:fuse_backing_put (read_dev);
@@ -530,8 +682,28 @@ static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count, {struct fuse_inode *fi =get_fuse_inode (inode);struct fuse_mount *fm =get_fuse_mount (inode); + struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap); +struct iomap *srcmap = &iter->srcmap;int err =0 ; +if (srcmap->inline_data) +fuse_iomap_inline_free (srcmap); + +if (iomap->inline_data) { +if (fuse_is_iomap_file_write (opflags) && written >0 ) { + err =fuse_iomap_inline_write (inode, pos, written, + iomap); +fuse_iomap_inline_free (iomap); +if (err) +return err; + }else { +fuse_iomap_inline_free (iomap); + } + +/* fuse server should already be aware of what happened */ +return 0 ; + } +if (fuse_should_send_iomap_end (fm, iomap, opflags, count, written)) {struct fuse_iomap_end_in inarg = { .opflags = fuse_iomap_op_to_server(opflags),
@@ -1454,6 +1626,18 @@ static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc,if (ret) goto discard_folio; +if (BAD_DATA (write_iomap.type ==IOMAP_INLINE )) { +/* +* iomap assumes that inline data writes are completed +* by the time ->iomap_end completes, so it should +* never mark a pagecache folio dirty. +*/ +fuse_iomap_end (inode, offset, len,0 , +FUSE_IOMAP_OP_WRITEBACK , &write_iomap); + ret = -EIO ; + goto discard_folio; + } +/* * Landed in a hole or beyond EOF? Send that to iomap, it'll * skip writing back the file range.
Re: [PATCH 24/31] fuse: implement inline data file IO via iomap
Quoted 4 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Implement inline data file IO by issuing FUSE_READ/FUSE_WRITE commands in response to an inline data mapping.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit 1263eae0852acb1359182377ad94d570701e2274
Author: Darrick J. Wong <[email protected]>
fuse: implement inline data file IO via iomap
Implement inline data file IO by issuing FUSE_READ/FUSE_WRITE commands in response to an inline data mapping.
Quoted 16 lines
fs/fuse/fuse_iomap.c +9
-0@@ -398,6 +398,150 @@ fuse_iomap_find_dev(struct fuse_conn *fc, const struct fuse_iomap_io *map)return ret; } +/* +* Use the FUSE_READ command to read inline file data from the fuse server. +* Note that there's no file handle attached, so the fuse server must be able +* to reconnect to the inode via the nodeid. +*/ +static int fuse_iomap_inline_read (struct inode *inode,loff_t pos, +loff_t count,struct iomap *iomap) +{ +struct fuse_read_in in = {
fs/fuse/fuse_iomap.c fuse_iomap_find_dev(struct fuse_conn *fc, const struct fuse_iomap_io *map) · line 419
Quoted 10 lines
fs/fuse/fuse_iomap.c +10
-0+ .offset = pos, + .size = count, + }; +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_mount *fm =get_fuse_mount (inode); +FUSE_ARGS (args); +ssize_t ret; + +if (BAD_DATA (!iomap_inline_data_valid (iomap))) +return -EFSCORRUPTED ;
Does this code leak memory when the BAD_DATA check fails? Looking at the call chain:
fuse_iomap_set_inline()->fuse_iomap_inline_alloc() allocates iomap->inline_data via kvzalloc. If the iomap_inline_data_valid check fails (i.e., iomap->length > PAGE_SIZE - offset_in_page(iomap->inline_data)), the function returns -EFSCORRUPTED without freeing iomap->inline_data.
Other error paths in this function (around the fuse_simple_request call) properly call fuse_iomap_inline_free(iomap) before returning errors.
The caller fuse_iomap_set_inline doesn't clean up iomap->inline_data on error in the read path (line 538: 'if (err) return err;'), so the allocation is leaked when a FUSE server returns an IOMAP_INLINE mapping with length > PAGE_SIZE.
Quoted 24 lines
fs/fuse/fuse_iomap.c +24
-0+ + args.opcode =FUSE_READ ; + args.nodeid = fi->nodeid; + args.in_numargs =1 ; + args.in_args[0 ].size =sizeof (in); + args.in_args[0 ].value = ∈ + args.out_argvar = true; + args.out_numargs =1 ; + args.out_args[0 ].size = count; + args.out_args[0 ].value =iomap_inline_data (iomap, pos); + + ret =fuse_simple_request (fm, &args); +if (ret <0 ) { +fuse_iomap_inline_free (iomap); +return ret; + } +/* no readahead means something bad happened */ +if (ret ==0 ) { +fuse_iomap_inline_free (iomap); +return -EIO ; + } + +return 0 ; +}
[ ... ]
fs/fuse/fuse_iomap.c static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count, · line 667
Quoted 6 lines
fs/fuse/fuse_iomap.c +2
-0@@ -511,8 +663,28 @@ static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count, {struct fuse_inode *fi =get_fuse_inode (inode);struct fuse_mount *fm =get_fuse_mount (inode); + struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap); +struct iomap *srcmap = &iter->srcmap;
Can this container_of usage corrupt memory? Looking at fuse_iomap_writeback_range, it calls fuse_iomap_end with &write_iomap, which is a stack-local struct iomap variable (not embedded in an iomap_iter):
fuse_iomap_writeback_range()
struct iomap write_iomap = { };
...
fuse_iomap_end(inode, offset, len, 0,
FUSE_IOMAP_OP_WRITEBACK, &write_iomap);The container_of macro computes an invalid pointer by subtracting the offset of the iomap member from the address, resulting in iter pointing to garbage memory on the stack. Subsequently, accessing iter->srcmap reads from invalid memory, potentially causing undefined behavior or crashes.
The iomap core calls fuse_iomap_end via fuse_iomap_ops where iomap IS properly embedded in iomap_iter, but the direct call from fuse_iomap_writeback_range violates this assumption.
Re: [PATCH 24/31] fuse: implement inline data file IO via iomap
Quoted 62 lines · Chris Mason
Quoted 4 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Implement inline data file IO by issuing FUSE_READ/FUSE_WRITE commands in response to an inline data mapping.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit
1263eae0852acb1359182377ad94d570701e2274
Author: Darrick J. Wong <[email protected]>fuse: implement inline data file IO via iomap
Implement inline data file IO by issuing FUSE_READ/FUSE_WRITE commands in response to an inline data mapping.
Quoted 16 lines
fs/fuse/fuse_iomap.c +9
-0@@ -398,6 +398,150 @@ fuse_iomap_find_dev(struct fuse_conn *fc, const struct fuse_iomap_io *map)return ret; } +/* +* Use the FUSE_READ command to read inline file data from the fuse server. +* Note that there's no file handle attached, so the fuse server must be able +* to reconnect to the inode via the nodeid. +*/ +static int fuse_iomap_inline_read (struct inode *inode,loff_t pos, +loff_t count,struct iomap *iomap) +{ +struct fuse_read_in in = {fs/fuse/fuse_iomap.c fuse_iomap_find_dev(struct fuse_conn *fc, const struct fuse_iomap_io *map) · line 419
Quoted 10 lines
fs/fuse/fuse_iomap.c +10
-0+ .offset = pos, + .size = count, + }; +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_mount *fm =get_fuse_mount (inode); +FUSE_ARGS (args); +ssize_t ret; + +if (BAD_DATA (!iomap_inline_data_valid (iomap))) +return -EFSCORRUPTED ;Does this code leak memory when the
BAD_DATAcheck fails? Looking at the call chain:fuse_iomap_set_inline()->fuse_iomap_inline_alloc() allocates
iomap->inline_datavia kvzalloc. If theiomap_inline_data_validcheck fails (i.e.,iomap->length>PAGE_SIZE-offset_in_page(iomap->inline_data)), the function returns-EFSCORRUPTEDwithout freeingiomap->inline_data.
Quoted 7 lines
Other error paths in this function (around the
fuse_simple_requestcall) properly callfuse_iomap_inline_free(iomap)before returning errors.The caller
fuse_iomap_set_inlinedoesn't clean upiomap->inline_dataon error in the read path (line 538: 'if (err) return err;'), so the allocation is leaked when a FUSE server returns anIOMAP_INLINEmapping with length >PAGE_SIZE.
Yep, it does leak the ioend and the bio. I'll amend this to call fuse_iomap_inline_free on its way out.
Quoted 51 lines
Quoted 24 lines
fs/fuse/fuse_iomap.c +24
-0+ + args.opcode =FUSE_READ ; + args.nodeid = fi->nodeid; + args.in_numargs =1 ; + args.in_args[0 ].size =sizeof (in); + args.in_args[0 ].value = ∈ + args.out_argvar = true; + args.out_numargs =1 ; + args.out_args[0 ].size = count; + args.out_args[0 ].value =iomap_inline_data (iomap, pos); + + ret =fuse_simple_request (fm, &args); +if (ret <0 ) { +fuse_iomap_inline_free (iomap); +return ret; + } +/* no readahead means something bad happened */ +if (ret ==0 ) { +fuse_iomap_inline_free (iomap); +return -EIO ; + } + +return 0 ; +}[ ... ]
fs/fuse/fuse_iomap.c static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count, · line 667
Quoted 6 lines
fs/fuse/fuse_iomap.c +2
-0@@ -511,8 +663,28 @@ static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count, {struct fuse_inode *fi =get_fuse_inode (inode);struct fuse_mount *fm =get_fuse_mount (inode); + struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap); +struct iomap *srcmap = &iter->srcmap;Can this
container_ofusage corrupt memory? Looking atfuse_iomap_writeback_range, it callsfuse_iomap_endwith&write_iomap, which is a stack-local struct iomap variable (not embedded in aniomap_iter):
fuse_iomap_writeback_range()struct iomap write_iomap = { }; ... fuse_iomap_end(inode, offset, len, 0, FUSE_IOMAP_OP_WRITEBACK, &write_iomap);The
container_ofmacro computes an invalid pointer by subtracting the offset of the iomap member from the address, resulting in iter pointing to garbage memory on the stack. Subsequently, accessingiter->srcmapreads from invalid memory, potentially causing undefined behavior or crashes.
Quoted 3 lines
The iomap core calls
fuse_iomap_endviafuse_iomap_opswhere iomap IS properly embedded iniomap_iter, but the direct call fromfuse_iomap_writeback_rangeviolates this assumption.
Oops, that's a severe bug. fuse_iomap_writeback_range should indeed define a whole iomap_iter instead of just the iomap. I'll fix that, thanks for pointing out these bugs.
--D
PATCH 25/31 fuse_trace: implement inline data file IO via iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/file_iomap.c | 7 +++++++ 2 files changed, 52 insertions(+)
fs/fuse/fuse_trace.h +45 -0
@@ -227,6 +227,7 @@ DEFINE_FUSE_BACKING_EVENT(fuse_backing_close);#if IS_ENABLED (CONFIG_FUSE_IOMAP )struct iomap_writepage_ctx ;struct iomap_ioend ; +struct iomap ;/* tracepoint boilerplate so we don't have to keep doing this */ #define FUSE_IOMAP_OPFLAGS_FIELD \
@@ -1044,6 +1045,50 @@ TRACE_EVENT(fuse_iomap_dev_inval, __entry->offset, __entry->length) ); + +DECLARE_EVENT_CLASS (fuse_iomap_inline_class, + TP_PROTO(const struct inode *inode, loff_t pos, uint64_t count, + const struct iomap *map), +TP_ARGS (inode, pos, count, map), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + FUSE_IOMAP_MAP_FIELDS(map) + __field(bool, has_buf) +__field (uint64_t, validity_cookie) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = pos; + __entry->length = count; + + __entry->mapdev = FUSE_IOMAP_DEV_NULL; + __entry->mapaddr = map->addr; + __entry->mapoffset = map->offset; + __entry->maplength = map->length; + __entry->maptype = map->type; + __entry->mapflags = map->flags; + + __entry->has_buf = map->inline_data != NULL; + __entry->validity_cookie= map->validity_cookie; + ), + + TP_printk(FUSE_IO_RANGE_FMT() FUSE_IOMAP_MAP_FMT() " has_buf? %d cookie 0x%llx", +FUSE_IO_RANGE_PRINTK_ARGS (), +FUSE_IOMAP_MAP_PRINTK_ARGS (map), + __entry->has_buf, + __entry->validity_cookie) +); +#define DEFINE_FUSE_IOMAP_INLINE_EVENT (name) \ +DEFINE_EVENT(fuse_iomap_inline_class, name, \ + TP_PROTO(const struct inode *inode, loff_t pos, uint64_t count, \ + const struct iomap *map), \ + TP_ARGS(inode, pos, count, map)) +DEFINE_FUSE_IOMAP_INLINE_EVENT (fuse_iomap_inline_read); +DEFINE_FUSE_IOMAP_INLINE_EVENT (fuse_iomap_inline_write); +DEFINE_FUSE_IOMAP_INLINE_EVENT (fuse_iomap_set_inline_iomap); +DEFINE_FUSE_IOMAP_INLINE_EVENT (fuse_iomap_set_inline_srcmap);#endif /* CONFIG_FUSE_IOMAP */ #endif /* _TRACE_FUSE_H */
fs/fuse/file_iomap.c +7 -0
@@ -452,6 +452,8 @@ static int fuse_iomap_inline_read(struct inode *inode, loff_t pos,if (BAD_DATA (!iomap_inline_data_valid (iomap)))return -EFSCORRUPTED ; +trace_fuse_iomap_inline_read (inode, pos, count, iomap); + args.opcode =FUSE_READ ; args.nodeid = fi->nodeid; args.in_numargs =1 ;
@@ -497,6 +499,8 @@ static int fuse_iomap_inline_write(struct inode *inode, loff_t pos,if (BAD_DATA (!iomap_inline_data_valid (iomap)))return -EFSCORRUPTED ; +trace_fuse_iomap_inline_write (inode, pos, count, iomap); + args.opcode =FUSE_WRITE ; args.nodeid = fi->nodeid; args.in_numargs =2 ;
@@ -558,6 +562,9 @@ static int fuse_iomap_set_inline(struct inode *inode, unsigned opflags,return err; } +trace_fuse_iomap_set_inline_iomap (inode, pos, count, iomap); +trace_fuse_iomap_set_inline_srcmap (inode, pos, count, srcmap); +return 0 ; }
PATCH 26/31 fuse: allow more statx fields
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Allow the fuse server to supply us with the more recently added fields of struct statx.
--- fs/fuse/fuse_i.h | 8 +++++ include/uapi/linux/fuse.h | 15 ++++++++- fs/fuse/dir.c | 75 ++++++++++++++++++++++++++++++++++++++------- 3 files changed, 86 insertions(+), 12 deletions(-)
fs/fuse/fuse_i.h +8 -0
@@ -1735,6 +1735,14 @@ void fuse_iomap_sysfs_cleanup(struct kobject *kobj);sector_t fuse_bmap (struct address_space *mapping,sector_t block); +#if IS_ENABLED (CONFIG_FUSE_IOMAP_DEBUG ) +int fuse_iomap_sysfs_init (struct kobject *kobj); +void fuse_iomap_sysfs_cleanup (struct kobject *kobj); +#else +# define fuse_iomap_sysfs_init (...) (0) +# define fuse_iomap_sysfs_cleanup (...) ((void)0) +#endif +#if IS_ENABLED (CONFIG_FUSE_IOMAP )bool fuse_iomap_enabled (void );
include/uapi/linux/fuse.h +14 -1
@@ -339,7 +339,20 @@ struct fuse_statx {uint32_t rdev_minor;uint32_t dev_major;uint32_t dev_minor; -uint64_t __spare2[14 ]; + +uint64_t mnt_id; +uint32_t dio_mem_align; +uint32_t dio_offset_align; +uint64_t subvol; + +uint32_t atomic_write_unit_min; +uint32_t atomic_write_unit_max; +uint32_t atomic_write_segments_max; +uint32_t dio_read_offset_align; +uint32_t atomic_write_unit_max_opt; +uint32_t __spare2[1 ]; + +uint64_t __spare3[8 ]; };struct fuse_kstatfs {
fs/fuse/dir.c +64 -11
111 lines not on this page · raw message
PATCH 27/31 fuse: support atomic writes with iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Enable untorn writes of up to a single fsblock, if iomap is enabled.
--- fs/fuse/fuse_i.h | 9 +++++++++ include/uapi/linux/fuse.h | 5 +++++ fs/fuse/file_iomap.c | 46 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 1 deletion(-)
fs/fuse/fuse_i.h +9 -0
@@ -266,6 +266,8 @@ enum {FUSE_I_EXCLUSIVE ,/* Use iomap for this inode */ FUSE_I_IOMAP , +/* Enable untorn writes */ + FUSE_I_ATOMIC, };struct fuse_conn ;
@@ -1768,6 +1770,13 @@ static inline bool fuse_inode_has_iomap(const struct inode *inode)return test_bit (FUSE_I_IOMAP , &fi->state); } +static inline bool fuse_inode_has_atomic (const struct inode *inode) +{ +const struct fuse_inode *fi =get_fuse_inode (inode); + +return test_bit (FUSE_I_ATOMIC , &fi->state); +} +int fuse_iomap_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo,u64 start,u64 length);loff_t fuse_iomap_lseek (struct file *file,loff_t offset,int whence);
include/uapi/linux/fuse.h +5 -0
@@ -246,6 +246,7 @@* - add FUSE_ATTR_IOMAP to enable iomap for specific inodes * - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry * - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges +* - add FUSE_ATTR_ATOMIC for single-fsblock atomic write support */ #ifndef _LINUX_FUSE_H
@@ -600,10 +601,12 @@ struct fuse_file_lock {* FUSE_ATTR_SUBMOUNT: Object is a submount root * FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode * FUSE_ATTR_IOMAP: Use iomap for this inode +* FUSE_ATTR_ATOMIC: Enable untorn writes */ #define FUSE_ATTR_SUBMOUNT (1 << 0)#define FUSE_ATTR_DAX (1 << 1)#define FUSE_ATTR_IOMAP (1 << 2) +#define FUSE_ATTR_ATOMIC (1 << 3)/** * Open flags
@@ -1171,6 +1174,8 @@ struct fuse_backing_map {/* basic file I/O functionality through iomap */ #define FUSE_IOMAP_SUPPORT_FILEIO (1ULL << 0) +/* untorn writes through iomap */ +#define FUSE_IOMAP_SUPPORT_ATOMIC (1ULL << 1)struct fuse_iomap_support {uint64_t flags;uint64_t padding;
fs/fuse/file_iomap.c +45 -1
94 lines not on this page · raw message
PATCH 28/31 fuse_trace: support atomic writes with iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/fuse/fuse_trace.h +3 -1
@@ -327,6 +327,7 @@ TRACE_DEFINE_ENUM(FUSE_I_BTIME);TRACE_DEFINE_ENUM (FUSE_I_CACHE_IO_MODE );TRACE_DEFINE_ENUM (FUSE_I_EXCLUSIVE );TRACE_DEFINE_ENUM (FUSE_I_IOMAP ); +TRACE_DEFINE_ENUM (FUSE_I_ATOMIC );#define FUSE_IFLAG_STRINGS \ { 1 << FUSE_I_ADVISE_RDPLUS, "advise_rdplus" }, \
@@ -336,7 +337,8 @@ TRACE_DEFINE_ENUM(FUSE_I_IOMAP); { 1 << FUSE_I_BTIME, "btime" }, \ { 1 << FUSE_I_CACHE_IO_MODE, "cacheio" }, \ { 1 << FUSE_I_EXCLUSIVE, "excl" }, \ - { 1 << FUSE_I_IOMAP, "iomap" } + { 1 << FUSE_I_IOMAP, "iomap" }, \ + { 1 << FUSE_I_ATOMIC, "atomic" } #define IOMAP_IOEND_STRINGS \ { IOMAP_IOEND_SHARED, "shared" }, \
PATCH 29/31 fuse: disable direct reclaim for any fuse server that uses iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Any fuse server that uses iomap can create a substantial amount of dirty pages in the pagecache because we don't write dirty stuff until reclaim or fsync. Therefore, memory reclaim on any fuse iomap server musn't ever recurse back into the same filesystem. We must also never throttle the fuse server writes to a bdi because that will just slow down metadata operations.
--- fs/fuse/file_iomap.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/fuse/file_iomap.c +6 -0
@@ -1016,6 +1016,12 @@ static void fuse_iomap_config_reply(struct fuse_mount *fm, fc->iomap_conn.no_end =0 ; fc->iomap_conn.no_ioend =0 ; +/* +* We could be on the hook for a substantial amount of writeback, so +* prohibit reclaim from recursing into fuse or the kernel from +* throttling any bdis that the fuse server might write to. +*/ + current->flags |=PF_MEMALLOC_NOFS |PF_LOCAL_THROTTLE ; done:kfree (ia);fuse_finish_init (fc, ok);
Re: [PATCH 29/31] fuse: disable direct reclaim for any fuse server that uses iomap
Quoted 8 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Any fuse server that uses iomap can create a substantial amount of dirty pages in the pagecache because we don't write dirty stuff until reclaim or fsync. Therefore, memory reclaim on any fuse iomap server musn't ever recurse back into the same filesystem. We must also never throttle the fuse server writes to a bdi because that will just slow down metadata operations.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit 3bdd586d6fa00e067dbc3676642ac75e54ec3031
Author: Darrick J. Wong <[email protected]>
fuse: disable direct reclaim for any fuse server that uses iomap
Any fuse server that uses iomap can create a substantial amount of dirty pages in the pagecache because we don't write dirty stuff until reclaim or fsync. Therefore, memory reclaim on any fuse iomap server must avoid recursing back into the same filesystem and throttling the fuse server.
fs/fuse/fuse_iomap.c static void fuse_iomap_config_reply(struct fuse_mount *fm, · line 1027
Quoted 13 lines
fs/fuse/fuse_iomap.c +6
-0@@ -1019,6 +1019,12 @@ static void fuse_iomap_config_reply(struct fuse_mount *fm,*/ fc->sync_fs = true; +/* +* We could be on the hook for a substantial amount of writeback, so +* prohibit reclaim from recursing into fuse or the kernel from +* throttling any bdis that the fuse server might write to. +*/ + current->flags |=PF_MEMALLOC_NOFS |PF_LOCAL_THROTTLE ;
Are these process flags being set on the correct process? Looking at the call chain:
Sync path:
fuse_fill_super() -> fuse_iomap_mount() -> fuse_iomap_config_reply()
Async path:
fuse_request_end() -> args->end() -> fuse_iomap_config_reply()
In the sync path, current points to the mount(2) process which exits after mounting completes. In the async path, current points to whichever process writes the reply to /dev/fuse (the fuse daemon thread) or a kernel worker thread for virtio-fs.
The commit message states the goal is to prohibit reclaim from recursing into the fuse server, but the fuse server is a separate userspace process. Setting flags on the mount process (which exits) or on a random fuse daemon thread (only once during init) doesn't protect the fuse server threads that actually perform I/O operations.
Should the flags be set on the correct process context? Or if they need to be set here, should they follow the pattern used in fs/nfsd/vfs.c and fs/nfs/localio.c which save and restore the flags?
Looking at fs/nfsd/vfs.c:1438-1509:
unsigned long old_flags = current->flags; current->flags |= PF_LOCAL_THROTTLE; ... current_restore_flags(pflags, PF_LOCAL_THROTTLE);
and fs/nfs/localio.c:824-828:
unsigned long old_flags = current->flags; current->flags |= PF_LOCAL_THROTTLE; ... current_restore_flags(old_flags, PF_LOCAL_THROTTLE);
Without saving and restoring, the flags permanently alter the process state. For the mount process this is wasted when it exits, and for fuse daemon threads this causes permanent behavior changes.
Re: [PATCH 29/31] fuse: disable direct reclaim for any fuse server that uses iomap
Quoted 80 lines · Chris Mason
Quoted 8 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Any fuse server that uses iomap can create a substantial amount of dirty pages in the pagecache because we don't write dirty stuff until reclaim or fsync. Therefore, memory reclaim on any fuse iomap server musn't ever recurse back into the same filesystem. We must also never throttle the fuse server writes to a bdi because that will just slow down metadata operations.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit
3bdd586d6fa00e067dbc3676642ac75e54ec3031
Author: Darrick J. Wong <[email protected]>fuse: disable direct reclaim for any fuse server that uses iomap
Any fuse server that uses iomap can create a substantial amount of dirty pages in the pagecache because we don't write dirty stuff until reclaim or fsync. Therefore, memory reclaim on any fuse iomap server must avoid recursing back into the same filesystem and throttling the fuse server.
fs/fuse/fuse_iomap.c static void fuse_iomap_config_reply(struct fuse_mount *fm, · line 1027
Quoted 13 lines
fs/fuse/fuse_iomap.c +6
-0@@ -1019,6 +1019,12 @@ static void fuse_iomap_config_reply(struct fuse_mount *fm,*/ fc->sync_fs = true; +/* +* We could be on the hook for a substantial amount of writeback, so +* prohibit reclaim from recursing into fuse or the kernel from +* throttling any bdis that the fuse server might write to. +*/ + current->flags |=PF_MEMALLOC_NOFS |PF_LOCAL_THROTTLE ;Are these process flags being set on the correct process? Looking at the call chain:
Sync path:
fuse_fill_super()->fuse_iomap_mount()->fuse_iomap_config_reply()Async path:
fuse_request_end()->args->end()->fuse_iomap_config_reply()In the sync path, current points to the mount(2) process which exits after mounting completes. In the async path, current points to whichever process writes the reply to
/dev/fuse(the fuse daemon thread) or a kernel worker thread for virtio-fs.The commit message states the goal is to prohibit reclaim from recursing into the fuse server, but the fuse server is a separate userspace process. Setting flags on the mount process (which exits) or on a random fuse daemon thread (only once during init) doesn't protect the fuse server threads that actually perform I/O operations.
Should the flags be set on the correct process context? Or if they need to be set here, should they follow the pattern used in
fs/nfsd/vfs.candfs/nfs/localio.cwhich save and restore the flags?Looking at fs/nfsd/vfs.c:1438-1509:
unsigned long old_flags = current->flags; current->flags |= PF_LOCAL_THROTTLE; ... current_restore_flags(pflags, PF_LOCAL_THROTTLE);and fs/nfs/localio.c:824-828:
Quoted 8 lines
unsigned long old_flags = current->flags; current->flags |= PF_LOCAL_THROTTLE; ... current_restore_flags(old_flags, PF_LOCAL_THROTTLE);Without saving and restoring, the flags permanently alter the process state. For the mount process this is wasted when it exits, and for fuse daemon threads this causes permanent behavior changes.
Hrmm. Normally the fuse server itself calls mount(2) via libfuse to set up the mount point, so this does (AFAICT) set PF_ flags on the fuse server itself.
However, any fuse setup that doesn't handle things this way would indeed set the PF_ flags on the wrong process. There are (a) other fuse libraries out there, and (b) the fuse service architecture mentioned downthread has a mount helper that starts up a fuse server on the other end of a unix socket, passes it resources including /dev/fuse to start up, and then calls mount(2) itself instead of the fuse server doing that.
That part's broken, and I'll have to think about how to solve that. Or maybe someone else will tell me this is all undesirable and I'll just drop this patch. :)
--D
PATCH 30/31 fuse: enable swapfile activation on iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
It turns out that fuse supports swapfile activation, so let's enable that.
--- fs/fuse/fuse_trace.h | 1 + include/uapi/linux/fuse.h | 5 +++++ fs/fuse/file_iomap.c | 50 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-)
fs/fuse/fuse_trace.h +1 -0
@@ -297,6 +297,7 @@ struct iomap; { FUSE_IOMAP_OP_DAX, "fsdax" }, \ { FUSE_IOMAP_OP_ATOMIC, "atomic" }, \ { FUSE_IOMAP_OP_DONTCACHE, "dontcache" }, \ + { FUSE_IOMAP_OP_SWAPFILE, "swapfile" }, \ { FUSE_IOMAP_OP_WRITEBACK, "writeback" } #define FUSE_IOMAP_TYPE_STRINGS \
include/uapi/linux/fuse.h +5 -0
@@ -1400,6 +1400,9 @@ struct fuse_uring_cmd_req {#define FUSE_IOMAP_OP_ATOMIC (1U << 9)#define FUSE_IOMAP_OP_DONTCACHE (1U << 10) +/* swapfile config operation */ +#define FUSE_IOMAP_OP_SWAPFILE (1U << 30) +/* pagecache writeback operation */ #define FUSE_IOMAP_OP_WRITEBACK (1U << 31)
@@ -1454,6 +1457,8 @@ struct fuse_iomap_end_in {#define FUSE_IOMAP_IOEND_APPEND (1U << 4)/* is pagecache writeback */ #define FUSE_IOMAP_IOEND_WRITEBACK (1U << 5) +/* swapfile deactivation */ +#define FUSE_IOMAP_IOEND_SWAPOFF (1U << 6)struct fuse_iomap_ioend_in {uint32_t ioendflags;/* FUSE_IOMAP_IOEND_* */
fs/fuse/file_iomap.c +49 -1
86 lines not on this page · raw message
PATCH 31/31 fuse: implement freeze and shutdowns for iomap filesystems
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Implement filesystem freezing and block device shutdown notifications for iomap-based servers
--- include/uapi/linux/fuse.h | 12 +++++++ fs/fuse/inode.c | 73 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+)
include/uapi/linux/fuse.h +12 -0
@@ -690,6 +690,10 @@ enum fuse_opcode {FUSE_STATX =52 ,FUSE_COPY_FILE_RANGE_64 =53 , +FUSE_FREEZE_FS =4089 , +FUSE_UNFREEZE_FS =4090 , +FUSE_SHUTDOWN_FS =4091 , +FUSE_IOMAP_CONFIG =4092 ,FUSE_IOMAP_IOEND =4093 , FUSE_IOMAP_BEGIN = 4094,
@@ -1251,6 +1255,14 @@ struct fuse_syncfs_in {uint64_t padding; }; +struct fuse_freezefs_in { +uint64_t unlinked; +}; + +struct fuse_shutdownfs_in { +uint64_t flags; +}; +/* * For each security context, send fuse_secctx with size of security context * fuse_secctx will be followed by security context name and this in turn
fs/fuse/inode.c +73 -0
85 lines not on this page · raw message
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
In reply to Darrick J. WongQuoted 2 lines
By keeping the I/O path mostly within the kernel, we can dramatically increase the speed of disk-based filesystems.
ZFS, BTRFS, and bcachefs all support compression, checksumming, and RAID. ZFS and bcachefs also support encryption, and f2fs and ext4 support fscrypt.
Will this patchset be able to improve FUSE implementations of these filesystems? I'd rather not be in the situation where one can have a FUSE filesystem that is fast, but only if it doesn't support modern data integrity or security features.
I'm not a filesystem developer, but here are some ideas (that you can take or leave):
1. Keep the compression, checksumming, and/or encryption in-kernel, and have userspace tell the kernel what algorithm and/or encryption key to use. These algorithms are generally well-known and secure against malicious input. It might be necessary to make an extra data copy, but ideally that copy could just stay within the CPU caches.
2. Somehow integrate with the blk-crypto framework. This has the advantage that it supports inline encryption hardware, which I suspect is needed for this to be usable on mobile devices. After all, the keys on these systems are often not even visible to the kernel, let alone to userspace.
3. Figure out a way to make a userspace data path fast enough. To prevent data corruption by unprivileged users of the FS, it's necessary to make a copy before checksumming, compression, or authenticated encryption. If this copy is done in the kernel, the server doesn't have to perform its own copy. By using large ring buffers, it might be possible to amortize the context switch cost away.
Authenticated encryption also needs a copy in the *other* direction:
if the (untrusted) client can see unauthenticated plaintext, it's a security vulnerability. That needs another copy from server buffers to client buffers, and the kernel can do that as well.
4. Make context switches much faster. L4-style IPC is incredibly fast, at least if one doesn't have to worry about Spectre. Unfortunately, nowadays one *does* need to worry about Spectre.
Obviously, none of these will be as fast as doing DMA directly to user buffers. However, all of these features (except for encryption using inline encryption hardware) come at a performance penalty already. I just don't want a FUSE server to have to pay a much larger penalty than a kernel filesystem would.
I'm CCing the bcachefs, BTRFS, and ZFS-on-Linux mailing lists.
Signature · 2 lines
Sincerely, Demi Marie Obenour (she/her/hers)
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 12 lines · Demi Marie Obenour
Quoted 2 lines
By keeping the I/O path mostly within the kernel, we can dramatically increase the speed of disk-based filesystems.
ZFS, BTRFS, and bcachefs all support compression, checksumming, and RAID. ZFS and bcachefs also support encryption, and f2fs and ext4 support fscrypt.
Will this patchset be able to improve FUSE implementations of these filesystems? I'd rather not be in the situation where one can have a FUSE filesystem that is fast, but only if it doesn't support modern data integrity or security features.
Quoted 5 lines
I'm not a filesystem developer, but here are some ideas (that you can take or leave):
1. Keep the compression, checksumming, and/or encryption in-kernel, and have userspace tell the kernel what algorithm and/or encryption
I don't think it's generally feasible unless it's limited to specific implementations because each transformation-like ondisk encoded data has its own design, which is unlike raw data.
Although the algorithms are well-known but the ondisk data could be wrapped up with headers, footers, or specific markers.
I think for the specific fscrypt or fsverity it could be possible (for example, I'm not sure zfs is 100%-compatible with fscrypt or fsverity, if they implements similiar stuffs), but considering generic compression, checksumming, and encryption, filesystem implementations can do various ways (even in various orders) with possible additional representations.
Quoted 4 lines
key to use. These algorithms are generally well-known and secure against malicious input. It might be necessary to make an extra data copy, but ideally that copy could just stay within the CPU caches.
Thanks,
Gao Xiang
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
In reply to Demi Marie ObenourQuoted 11 lines · Demi Marie Obenour
Quoted 2 lines
By keeping the I/O path mostly within the kernel, we can dramatically increase the speed of disk-based filesystems.
ZFS, BTRFS, and bcachefs all support compression, checksumming, and RAID. ZFS and bcachefs also support encryption, and f2fs and ext4 support fscrypt.
Will this patchset be able to improve FUSE implementations of these filesystems? I'd rather not be in the situation where one can have a FUSE filesystem that is fast, but only if it doesn't support modern data integrity or security features.
Not on its own, no.
Quoted 9 lines
I'm not a filesystem developer, but here are some ideas (that you can take or leave):
1. Keep the compression, checksumming, and/or encryption in-kernel, and have userspace tell the kernel what algorithm and/or encryption key to use. These algorithms are generally well-known and secure against malicious input. It might be necessary to make an extra data copy, but ideally that copy could just stay within the CPU caches.
I think this is easily doable for fscrypt and compression since (IIRC) the kernel filesystems already know how to transform data for I/O, and nowadays iomap allows hooking of bios before submission and/or after endio. Obviously you'd have to store encryption keys in the kernel somewhere.
Checksumming is harder though, since the checksum information has to be persisted in the metadata somewhere and AFAICT each checksumming fs does things differently. For that, I think the fuse server would have to convey to the kernel (a) a description of the checksum geometry and (b) a buffer for storing the checksums. On write the kernel would compute the checksum and write it to the buffer for the fs to persist as part of the ioend; and for read the fuse server would have to read the checksums into the buffer and pass that to the kernel.
(Note that fsverity won't have this problem because all current implementations stuff the merkle tree in post-eof datablocks; the fsverity code only wants fses to read it in the pagecache; and pass it the page)
Quoted 5 lines
2. Somehow integrate with the blk-crypto framework. This has the advantage that it supports inline encryption hardware, which I suspect is needed for this to be usable on mobile devices. After all, the keys on these systems are often not even visible to the kernel, let alone to userspace.
Yes, that would be even easier than messing around with bounce buffers.
Quoted 8 lines
3. Figure out a way to make a userspace data path fast enough. To prevent data corruption by unprivileged users of the FS, it's necessary to make a copy before checksumming, compression, or authenticated encryption. If this copy is done in the kernel, the server doesn't have to perform its own copy. By using large ring buffers, it might be possible to amortize the context switch cost away.
Quoted 8 lines
Authenticated encryption also needs a copy in the *other* direction:
if the (untrusted) client can see unauthenticated plaintext, it's a security vulnerability. That needs another copy from server buffers to client buffers, and the kernel can do that as well.4. Make context switches much faster. L4-style IPC is incredibly fast, at least if one doesn't have to worry about Spectre. Unfortunately, nowadays one *does* need to worry about Spectre.
I don't think context switching overhead is going down.
Quoted 10 lines
Obviously, none of these will be as fast as doing DMA directly to user buffers. However, all of these features (except for encryption using inline encryption hardware) come at a performance penalty already. I just don't want a FUSE server to have to pay a much larger penalty than a kernel filesystem would.
I'm CCing the bcachefs, BTRFS, and ZFS-on-Linux mailing lists.
Signature · 2 lines
Sincerely, Demi Marie Obenour (she/her/hers)
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 25 lines · Darrick J. Wong
Quoted 11 lines · Demi Marie Obenour
Quoted 2 lines
By keeping the I/O path mostly within the kernel, we can dramatically increase the speed of disk-based filesystems.
ZFS, BTRFS, and bcachefs all support compression, checksumming, and RAID. ZFS and bcachefs also support encryption, and f2fs and ext4 support fscrypt.
Will this patchset be able to improve FUSE implementations of these filesystems? I'd rather not be in the situation where one can have a FUSE filesystem that is fast, but only if it doesn't support modern data integrity or security features.
Not on its own, no.
Quoted 9 lines
I'm not a filesystem developer, but here are some ideas (that you can take or leave):
1. Keep the compression, checksumming, and/or encryption in-kernel, and have userspace tell the kernel what algorithm and/or encryption key to use. These algorithms are generally well-known and secure against malicious input. It might be necessary to make an extra data copy, but ideally that copy could just stay within the CPU caches.
Quoted 5 lines
I think this is easily doable for fscrypt and compression since (IIRC) the kernel filesystems already know how to transform data for I/O, and nowadays iomap allows hooking of bios before submission and/or after endio. Obviously you'd have to store encryption keys in the kernel somewhere.
I think it depends, since (this way) it tries to reuse some of the existing kernel filesystem implementations (and assuming the code is safe), so at least it still needs to load a dedicated kernel module for such usage at least.
I think it's not an issue for userspace ext4 of course (because ext4 and fscrypt is almost builtin for all kernels), but for out-of-tree fses even pure userspace fses, I'm not sure it's doable to load the module in a container context.
Maybe eBPF is useful for this area, but it's still not quite flexible compared to native kernel filesystems.
Thanks,
Gao Xiang
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 39 lines · Gao Xiang
Quoted 25 lines · Darrick J. Wong
Quoted 11 lines · Demi Marie Obenour
Quoted 2 lines
By keeping the I/O path mostly within the kernel, we can dramatically increase the speed of disk-based filesystems.
ZFS, BTRFS, and bcachefs all support compression, checksumming, and RAID. ZFS and bcachefs also support encryption, and f2fs and ext4 support fscrypt.
Will this patchset be able to improve FUSE implementations of these filesystems? I'd rather not be in the situation where one can have a FUSE filesystem that is fast, but only if it doesn't support modern data integrity or security features.
Not on its own, no.
Quoted 9 lines
I'm not a filesystem developer, but here are some ideas (that you can take or leave):
1. Keep the compression, checksumming, and/or encryption in-kernel, and have userspace tell the kernel what algorithm and/or encryption key to use. These algorithms are generally well-known and secure against malicious input. It might be necessary to make an extra data copy, but ideally that copy could just stay within the CPU caches.
Quoted 5 lines
I think this is easily doable for fscrypt and compression since (IIRC) the kernel filesystems already know how to transform data for I/O, and nowadays iomap allows hooking of bios before submission and/or after endio. Obviously you'd have to store encryption keys in the kernel somewhere.
I think it depends, since (this way) it tries to reuse some of the existing kernel filesystem implementations (and assuming the code is safe), so at least it still needs to load a dedicated kernel module for such usage at least.
Quoted 4 lines
I think it's not an issue for userspace ext4 of course (because ext4 and fscrypt is almost builtin for all kernels), but for out-of-tree fses even pure userspace fses, I'm not sure it's doable to load the module in a container context.
Two examples for reference:
- For compression, in-tree f2fs already has a compression header in data of each compressed extent:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/f2fs/f2fs.h?h=v6.17#n1497
while other fs may store additional metadata in extent metadata or other place.
- gocryptfs (a pure userspace FUSE fs) uses a different format from fscrypt (encrypted data seems even unaligned on disk):
https://github.com/rfjakob/gocryptfs/blob/master/Documentation/file-format.md
Quoted 6 lines
Maybe eBPF is useful for this area, but it's still not quite flexible compared to native kernel filesystems.
Thanks,
Gao Xiang
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 43 lines · Gao Xiang
Quoted 37 lines · Gao Xiang
Quoted 25 lines · Darrick J. Wong
Quoted 11 lines · Demi Marie Obenour
Quoted 2 lines
By keeping the I/O path mostly within the kernel, we can dramatically increase the speed of disk-based filesystems.
ZFS, BTRFS, and bcachefs all support compression, checksumming, and RAID. ZFS and bcachefs also support encryption, and f2fs and ext4 support fscrypt.
Will this patchset be able to improve FUSE implementations of these filesystems? I'd rather not be in the situation where one can have a FUSE filesystem that is fast, but only if it doesn't support modern data integrity or security features.
Not on its own, no.
Quoted 9 lines
I'm not a filesystem developer, but here are some ideas (that you can take or leave):
1. Keep the compression, checksumming, and/or encryption in-kernel, and have userspace tell the kernel what algorithm and/or encryption key to use. These algorithms are generally well-known and secure against malicious input. It might be necessary to make an extra data copy, but ideally that copy could just stay within the CPU caches.
Quoted 5 lines
I think this is easily doable for fscrypt and compression since (IIRC) the kernel filesystems already know how to transform data for I/O, and nowadays iomap allows hooking of bios before submission and/or after endio. Obviously you'd have to store encryption keys in the kernel somewhere.
I think it depends, since (this way) it tries to reuse some of the existing kernel filesystem implementations (and assuming the code is safe), so at least it still needs to load a dedicated kernel module for such usage at least.
Quoted 4 lines
I think it's not an issue for userspace ext4 of course (because ext4 and fscrypt is almost builtin for all kernels), but for out-of-tree fses even pure userspace fses, I'm not sure it's doable to load the module in a container context.
Quoted 8 lines
Two examples for reference:
- For compression, in-tree f2fs already has a compression header in data of each compressed extent:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/f2fs/f2fs.h?h=v6.17#n1497while other fs may store additional metadata in extent metadata or other place.
Extent metadata shouldn't be a problem, as that is already available during reads and written asynchronously for writes. The headers are awkward, though, and might need some special-casing.
Quoted 3 lines
- gocryptfs (a pure userspace FUSE fs) uses a different format from fscrypt (encrypted data seems even unaligned on disk):
https://github.com/rfjakob/gocryptfs/blob/master/Documentation/file-format.md
This is probably an anti-pattern in general, as I expect it precludes the use of inline encryption hardware via blk-crypto.
Signature · 2 lines
Sincerely, Demi Marie Obenour (she/her/hers)
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
In reply to Gao XiangQuoted 34 lines · Gao Xiang
Quoted 25 lines · Darrick J. Wong
Quoted 11 lines · Demi Marie Obenour
Quoted 2 lines
By keeping the I/O path mostly within the kernel, we can dramatically increase the speed of disk-based filesystems.
ZFS, BTRFS, and bcachefs all support compression, checksumming, and RAID. ZFS and bcachefs also support encryption, and f2fs and ext4 support fscrypt.
Will this patchset be able to improve FUSE implementations of these filesystems? I'd rather not be in the situation where one can have a FUSE filesystem that is fast, but only if it doesn't support modern data integrity or security features.
Not on its own, no.
Quoted 9 lines
I'm not a filesystem developer, but here are some ideas (that you can take or leave):
1. Keep the compression, checksumming, and/or encryption in-kernel, and have userspace tell the kernel what algorithm and/or encryption key to use. These algorithms are generally well-known and secure against malicious input. It might be necessary to make an extra data copy, but ideally that copy could just stay within the CPU caches.
Quoted 5 lines
I think this is easily doable for fscrypt and compression since (IIRC) the kernel filesystems already know how to transform data for I/O, and nowadays iomap allows hooking of bios before submission and/or after endio. Obviously you'd have to store encryption keys in the kernel somewhere.
Quoted 4 lines
I think it depends, since (this way) it tries to reuse some of the existing kernel filesystem implementations (and assuming the code is safe), so at least it still needs to load a dedicated kernel module for such usage at least.
My hope is that these modules could be generic library code. Compression, checksumming, and encryption algorithms aren't specific to any particular filesystem, and the kernel might well be using them already for other purposes.
Of course, it's still the host admin's job to make sure the relevant modules are loaded, unless they are autoloaded.
Quoted 10 lines
I think it's not an issue for userspace ext4 of course (because ext4 and fscrypt is almost builtin for all kernels), but for out-of-tree fses even pure userspace fses, I'm not sure it's doable to load the module in a container context.
Maybe eBPF is useful for this area, but it's still not quite flexible compared to native kernel filesystems.
Thanks,
Gao Xiang
Thank you for the feedback!
Signature · 2 lines
Sincerely, Demi Marie Obenour (she/her/hers)
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 35 lines · Demi Marie Obenour
Quoted 34 lines · Gao Xiang
Quoted 30 lines · Darrick J. Wong
Quoted 11 lines · Demi Marie Obenour
Quoted 2 lines
By keeping the I/O path mostly within the kernel, we can dramatically increase the speed of disk-based filesystems.
ZFS, BTRFS, and bcachefs all support compression, checksumming, and RAID. ZFS and bcachefs also support encryption, and f2fs and ext4 support fscrypt.
Will this patchset be able to improve FUSE implementations of these filesystems? I'd rather not be in the situation where one can have a FUSE filesystem that is fast, but only if it doesn't support modern data integrity or security features.
Not on its own, no.
Quoted 9 lines
I'm not a filesystem developer, but here are some ideas (that you can take or leave):
1. Keep the compression, checksumming, and/or encryption in-kernel, and have userspace tell the kernel what algorithm and/or encryption key to use. These algorithms are generally well-known and secure against malicious input. It might be necessary to make an extra data copy, but ideally that copy could just stay within the CPU caches.
I think this is easily doable for fscrypt and compression since (IIRC) the kernel filesystems already know how to transform data for I/O, and nowadays iomap allows hooking of bios before submission and/or after endio. Obviously you'd have to store encryption keys in the kernel somewhere.
Quoted 6 lines
Quoted 4 lines
I think it depends, since (this way) it tries to reuse some of the existing kernel filesystem implementations (and assuming the code is safe), so at least it still needs to load a dedicated kernel module for such usage at least.
My hope is that these modules could be generic library code.
Actually, the proposed generic library code for compression, checksumming, and encryption is already in "crypto/", but except for checksumming usage, filesystems rarely use the others, mostly because of inflexibility (for example, algorithms may have case-by-case advanced functionality.)
Quoted 6 lines
Compression, checksumming, and encryption algorithms aren't specific to any particular filesystem, and the kernel might well be using them already for other purposes.
Of course, it's still the host admin's job to make sure the relevant modules are loaded, unless they are autoloaded.
My thought is still roughly that, although algorithms could be generic, the specific implementations are still varied due to different filesystem on-disk intrinsicness (each filesystem has its own special trait) and/or whether designs are made with thoughtful thinking. fscrypt and fsverity are Linux kernel reference implementations, but, for example, fsverity metadata representation still takes a while for XFS folks to discuss (of course it doesn't impact the main mechanism).
Quoted 6 lines
Quoted 6 lines
I think it's not an issue for userspace ext4 of course (because ext4 and fscrypt is almost builtin for all kernels), but for out-of-tree fses even pure userspace fses, I'm not sure it's doable to load the module in a container context.
Quoted 6 lines
Quoted 5 lines
Maybe eBPF is useful for this area, but it's still not quite flexible compared to native kernel filesystems.
Thanks,
Gao XiangThank you for the feedback!
Thanks,
Gao Xiang
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
In reply to Darrick J. WongThank you so much for the helpful responses!
Quoted 8 lines · Darrick J. Wong
Quoted 7 lines · Demi Marie Obenour
Quoted 2 lines
By keeping the I/O path mostly within the kernel, we can dramatically increase the speed of disk-based filesystems.
ZFS, BTRFS, and bcachefs all support compression, checksumming, and RAID. ZFS and bcachefs also support encryption, and f2fs and ext4 support fscrypt.
Quoted 6 lines
Quoted 4 lines
Will this patchset be able to improve FUSE implementations of these filesystems? I'd rather not be in the situation where one can have a FUSE filesystem that is fast, but only if it doesn't support modern data integrity or security features.
Not on its own, no.
Not surprised. I'm mostly curious if there is a path forward to add such support in the future.
Quoted 16 lines
Quoted 9 lines
I'm not a filesystem developer, but here are some ideas (that you can take or leave):
1. Keep the compression, checksumming, and/or encryption in-kernel, and have userspace tell the kernel what algorithm and/or encryption key to use. These algorithms are generally well-known and secure against malicious input. It might be necessary to make an extra data copy, but ideally that copy could just stay within the CPU caches.
I think this is easily doable for fscrypt and compression since (IIRC) the kernel filesystems already know how to transform data for I/O, and nowadays iomap allows hooking of bios before submission and/or after endio. Obviously you'd have to store encryption keys in the kernel somewhere.
Quoted 8 lines
Checksumming is harder though, since the checksum information has to be persisted in the metadata somewhere and AFAICT each checksumming fs does things differently. For that, I think the fuse server would have to convey to the kernel (a) a description of the checksum geometry and (b) a buffer for storing the checksums. On write the kernel would compute the checksum and write it to the buffer for the fs to persist as part of the ioend; and for read the fuse server would have to read the checksums into the buffer and pass that to the kernel.
That definitely sounds doable. Bcachefs, and I believe ZFS and BTRFS, store the checksum in the pointer. This means that when the kernel is asked to read data from the buffer, the checksum or authentication tag is already available.
For CoW filesystems, there is still the problem that every write requires a metadata operation. Does that mean these filesystems will not be able to benefit for writes? Or can the latency be hidden somehow?
Quoted 5 lines
(Note that fsverity won't have this problem because all current implementations stuff the merkle tree in post-eof datablocks; the fsverity code only wants fses to read it in the pagecache; and pass it the page)
Quoted 7 lines
Quoted 5 lines
2. Somehow integrate with the blk-crypto framework. This has the advantage that it supports inline encryption hardware, which I suspect is needed for this to be usable on mobile devices. After all, the keys on these systems are often not even visible to the kernel, let alone to userspace.
Yes, that would be even easier than messing around with bounce buffers.
Makes sense.
Quoted 13 lines
Quoted 13 lines
3. Figure out a way to make a userspace data path fast enough. To prevent data corruption by unprivileged users of the FS, it's necessary to make a copy before checksumming, compression, or authenticated encryption. If this copy is done in the kernel, the server doesn't have to perform its own copy. By using large ring buffers, it might be possible to amortize the context switch cost away.
Authenticated encryption also needs a copy in the *other* direction:
if the (untrusted) client can see unauthenticated plaintext, it's a security vulnerability. That needs another copy from server buffers to client buffers, and the kernel can do that as well.
Quoted 5 lines
Quoted 3 lines
4. Make context switches much faster. L4-style IPC is incredibly fast, at least if one doesn't have to worry about Spectre. Unfortunately, nowadays one *does* need to worry about Spectre.
I don't think context switching overhead is going down.
I agree, at least for big CPUs.
Quoted 7 lines
Quoted 7 lines
Obviously, none of these will be as fast as doing DMA directly to user buffers. However, all of these features (except for encryption using inline encryption hardware) come at a performance penalty already. I just don't want a FUSE server to have to pay a much larger penalty than a kernel filesystem would.
I'm CCing the bcachefs, BTRFS, and ZFS-on-Linux mailing lists.
Signature · 2 lines
Sincerely, Demi Marie Obenour (she/her/hers)
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
In reply to Darrick J. WongQuoted 10 lines · Darrick J. Wong
Hi all,
This series connects fuse (the userspace filesystem layer) to fs-iomap to get fuse servers out of the business of handling file I/O themselves. By keeping the IO path mostly within the kernel, we can dramatically improve the speed of disk-based filesystems. This enables us to move all the filesystem metadata parsing code out of the kernel and into userspace, which means that we can containerize them for security without losing a lot of performance.
I haven't looked through how the fuse2fs or fuse4fs servers are implemented yet (also, could you explain the difference between the two? Which one should we look at to see how it all ties together?), but I wonder if having bpf infrastructure hooked up to fuse would be especially helpful for what you're doing here with fuse iomap. afaict, every read/write whether it's buffered or direct will incur at least 1 call to ->iomap_begin() to get the mapping metadata, which will be 2 context-switches (and if the server has ->iomap_end() implemented, then 2 more context-switches). But it seems like the logic for retrieving mapping offsets/lengths/metadata should be pretty straightforward? If the extent lookups are table lookups or tree traversals without complex side effects, then having ->iomap_begin()/->iomap_end() be executed as a bpf program would avoid the context switches and allow all the caching logic to be moved from the kernel to the server-side (eg using bpf maps). Is this your assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.
Thanks,
Joanne
Quoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 12 lines · Joanne Koong
Quoted 10 lines · Darrick J. Wong
Hi all,
This series connects fuse (the userspace filesystem layer) to fs-iomap to get fuse servers out of the business of handling file I/O themselves. By keeping the IO path mostly within the kernel, we can dramatically improve the speed of disk-based filesystems. This enables us to move all the filesystem metadata parsing code out of the kernel and into userspace, which means that we can containerize them for security without losing a lot of performance.
Quoted 3 lines
I haven't looked through how the fuse2fs or fuse4fs servers are implemented yet (also, could you explain the difference between the two? Which one should we look at to see how it all ties together?),
fuse4fs is a lowlevel fuse server; fuse2fs is a high(?) level fuse server. fuse4fs is the successor to fuse2fs, at least on Linux and BSD.
Quoted 6 lines
but I wonder if having bpf infrastructure hooked up to fuse would be especially helpful for what you're doing here with fuse iomap. afaict, every read/write whether it's buffered or direct will incur at least 1 call to ->iomap_begin() to get the mapping metadata, which will be 2 context-switches (and if the server has ->iomap_end() implemented, then 2 more context-switches).
Yes, I agree that's a lot of context switching for file IO...
Quoted 2 lines
But it seems like the logic for retrieving mapping offsets/lengths/metadata should be pretty straightforward?
...but it gets very cheap if the fuse server can cache mappings in the kernel to avoid all that. That is, incidentally, what patchset #7 implements.
Quoted 5 lines
If the extent lookups are table lookups or tree
traversals without complex side effects, then having ->iomap_begin()/->iomap_end() be executed as a bpf program would avoid the context switches and allow all the caching logic to be moved from the kernel to the server-side (eg using bpf maps).
Hrmm. Now that /is/ an interesting proposal. Does BPF have a data structure that supports interval mappings? I think the existing bpf map only does key -> value. Also, is there an upper limit on the size of a map? You could have hundreds of millions of maps for a very fragmented regular file.
At one point I suggested to the famfs maintainer that it might be easier/better to implement the interleaved mapping lookups as bpf programs instead of being stuck with a fixed format in the fuse userspace abi, but I don't know if he ever implemented that.
Quoted 5 lines
Is this your
assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.
For disk-based filesystems I think it would be difficult to model a bpf program to do mappings, since they can basically point anywhere and be of any size.
OTOH it would be enormously hilarious to me if one could load a file mapping predictive model into the kernel as a bpf program and use that as a first tier before checking the in-memory btree mapping cache from patchset 7. Quite a few years ago now there was a FAST paper establishing that even a stupid linear regression model could in theory beat a disk btree lookup.
--D
Quoted 67 lines
Thanks,
JoanneQuoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 14 lines · Darrick J. Wong
Quoted 12 lines · Joanne Koong
Quoted 10 lines · Darrick J. Wong
Hi all,
This series connects fuse (the userspace filesystem layer) to fs-iomap to get fuse servers out of the business of handling file I/O themselves. By keeping the IO path mostly within the kernel, we can dramatically improve the speed of disk-based filesystems. This enables us to move all the filesystem metadata parsing code out of the kernel and into userspace, which means that we can containerize them for security without losing a lot of performance.
Quoted 6 lines
Quoted 3 lines
I haven't looked through how the fuse2fs or fuse4fs servers are implemented yet (also, could you explain the difference between the two? Which one should we look at to see how it all ties together?),
fuse4fs is a lowlevel fuse server; fuse2fs is a high(?) level fuse server. fuse4fs is the successor to fuse2fs, at least on Linux and BSD.
Ah I see, thanks for the explanation. In that case, I'll just look at fuse4fs then.
Quoted 19 lines
Quoted 7 lines
but I wonder if having bpf infrastructure hooked up to fuse would be especially helpful for what you're doing here with fuse iomap. afaict, every read/write whether it's buffered or direct will incur at least 1 call to ->iomap_begin() to get the mapping metadata, which will be 2 context-switches (and if the server has ->iomap_end() implemented, then 2 more context-switches).
Yes, I agree that's a lot of context switching for file IO...
Quoted 2 lines
But it seems like the logic for retrieving mapping offsets/lengths/metadata should be pretty straightforward?
...but it gets very cheap if the fuse server can cache mappings in the kernel to avoid all that. That is, incidentally, what patchset #7 implements.
Quoted 8 lines
Quoted 5 lines
If the extent lookups are table lookups or tree
traversals without complex side effects, then having ->iomap_begin()/->iomap_end() be executed as a bpf program would avoid the context switches and allow all the caching logic to be moved from the kernel to the server-side (eg using bpf maps).Hrmm. Now that /is/ an interesting proposal. Does BPF have a data structure that supports interval mappings? I think the existing bpf map
Not yet but I don't see why a b+ tree like data strucutre couldn't be added. Maybe one workaround in the meantime that could work is using a sorted array map and doing binary search on that, until interval mappings can be natively supported?
Quoted 3 lines
only does key -> value. Also, is there an upper limit on the size of a map? You could have hundreds of millions of maps for a very fragmented regular file.
If I'm remembering correctly, there's an upper limit on the number of map entries, which is bounded by u32
Quoted 5 lines
At one point I suggested to the famfs maintainer that it might be easier/better to implement the interleaved mapping lookups as bpf programs instead of being stuck with a fixed format in the fuse userspace abi, but I don't know if he ever implemented that.
This seems like a good use case for it too
Quoted 10 lines
Quoted 6 lines
Is this your
assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.For disk-based filesystems I think it would be difficult to model a bpf program to do mappings, since they can basically point anywhere and be of any size.
Hmm I'm not familiar enough with disk-based filesystems to know what the "point anywhere and be of any size" means. For the mapping stuff, doesn't it just point to a block number? Or are you saying the problem would be there's too many mappings since a mapping could be any size?
I was thinking the issue would be more that there might be other logic inside ->iomap_begin()/->iomap_end() besides the mapping stuff that would need to be done that would be too out-of-scope for bpf. But I think I need to read through the fuse4fs stuff to understand more what it's doing in those functions.
Thanks,
Joanne
Quoted 77 lines
OTOH it would be enormously hilarious to me if one could load a file mapping predictive model into the kernel as a bpf program and use that as a first tier before checking the in-memory btree mapping cache from patchset 7. Quite a few years ago now there was a FAST paper establishing that even a stupid linear regression model could in theory beat a disk btree lookup.
--D
Quoted 67 lines
Thanks,
JoanneQuoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 50 lines · Joanne Koong
Quoted 14 lines · Darrick J. Wong
Quoted 12 lines · Joanne Koong
Quoted 10 lines · Darrick J. Wong
Hi all,
This series connects fuse (the userspace filesystem layer) to fs-iomap to get fuse servers out of the business of handling file I/O themselves. By keeping the IO path mostly within the kernel, we can dramatically improve the speed of disk-based filesystems. This enables us to move all the filesystem metadata parsing code out of the kernel and into userspace, which means that we can containerize them for security without losing a lot of performance.
Quoted 6 lines
Quoted 3 lines
I haven't looked through how the fuse2fs or fuse4fs servers are implemented yet (also, could you explain the difference between the two? Which one should we look at to see how it all ties together?),
fuse4fs is a lowlevel fuse server; fuse2fs is a high(?) level fuse server. fuse4fs is the successor to fuse2fs, at least on Linux and BSD.
Ah I see, thanks for the explanation. In that case, I'll just look at fuse4fs then.
Quoted 25 lines
Quoted 7 lines
but I wonder if having bpf infrastructure hooked up to fuse would be especially helpful for what you're doing here with fuse iomap. afaict, every read/write whether it's buffered or direct will incur at least 1 call to ->iomap_begin() to get the mapping metadata, which will be 2 context-switches (and if the server has ->iomap_end() implemented, then 2 more context-switches).
Yes, I agree that's a lot of context switching for file IO...
Quoted 2 lines
But it seems like the logic for retrieving mapping offsets/lengths/metadata should be pretty straightforward?
...but it gets very cheap if the fuse server can cache mappings in the kernel to avoid all that. That is, incidentally, what patchset #7 implements.
Quoted 5 lines
If the extent lookups are table lookups or tree
traversals without complex side effects, then having ->iomap_begin()/->iomap_end() be executed as a bpf program would avoid the context switches and allow all the caching logic to be moved from the kernel to the server-side (eg using bpf maps).
Quoted 7 lines
Quoted 2 lines
Hrmm. Now that /is/ an interesting proposal. Does BPF have a data structure that supports interval mappings? I think the existing bpf map
Not yet but I don't see why a b+ tree like data strucutre couldn't be added. Maybe one workaround in the meantime that could work is using a sorted array map and doing binary search on that, until interval mappings can be natively supported?
I guess, though I already had a C structure to borrow from xfs ;)
Quoted 6 lines
Quoted 3 lines
only does key -> value. Also, is there an upper limit on the size of a map? You could have hundreds of millions of maps for a very fragmented regular file.
If I'm remembering correctly, there's an upper limit on the number of map entries, which is bounded by u32
That's problematic, since files can have 64-bit logical block numbers.
Quoted 13 lines
Quoted 4 lines
At one point I suggested to the famfs maintainer that it might be easier/better to implement the interleaved mapping lookups as bpf programs instead of being stuck with a fixed format in the fuse userspace abi, but I don't know if he ever implemented that.
This seems like a good use case for it too
Quoted 7 lines
Quoted 6 lines
Is this your
assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.
Quoted 8 lines
Quoted 3 lines
For disk-based filesystems I think it would be difficult to model a bpf program to do mappings, since they can basically point anywhere and be of any size.
Hmm I'm not familiar enough with disk-based filesystems to know what the "point anywhere and be of any size" means. For the mapping stuff, doesn't it just point to a block number? Or are you saying the problem would be there's too many mappings since a mapping could be any size?
The second -- mappings can be any size, and unprivileged userspace can control the mappings.
Quoted 5 lines
I was thinking the issue would be more that there might be other logic inside ->iomap_begin()/->iomap_end() besides the mapping stuff that would need to be done that would be too out-of-scope for bpf. But I think I need to read through the fuse4fs stuff to understand more what it's doing in those functions.
<nod>
--D
Quoted 81 lines
Thanks,
JoanneQuoted 77 lines
OTOH it would be enormously hilarious to me if one could load a file mapping predictive model into the kernel as a bpf program and use that as a first tier before checking the in-memory btree mapping cache from patchset 7. Quite a few years ago now there was a FAST paper establishing that even a stupid linear regression model could in theory beat a disk btree lookup.
--D
Quoted 67 lines
Thanks,
JoanneQuoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 62 lines · Darrick J. Wong
Quoted 50 lines · Joanne Koong
Quoted 14 lines · Darrick J. Wong
Quoted 12 lines · Joanne Koong
Quoted 10 lines · Darrick J. Wong
Hi all,
This series connects fuse (the userspace filesystem layer) to fs-iomap to get fuse servers out of the business of handling file I/O themselves. By keeping the IO path mostly within the kernel, we can dramatically improve the speed of disk-based filesystems. This enables us to move all the filesystem metadata parsing code out of the kernel and into userspace, which means that we can containerize them for security without losing a lot of performance.
Quoted 6 lines
Quoted 3 lines
I haven't looked through how the fuse2fs or fuse4fs servers are implemented yet (also, could you explain the difference between the two? Which one should we look at to see how it all ties together?),
fuse4fs is a lowlevel fuse server; fuse2fs is a high(?) level fuse server. fuse4fs is the successor to fuse2fs, at least on Linux and BSD.
Ah I see, thanks for the explanation. In that case, I'll just look at fuse4fs then.
Quoted 25 lines
Quoted 7 lines
but I wonder if having bpf infrastructure hooked up to fuse would be especially helpful for what you're doing here with fuse iomap. afaict, every read/write whether it's buffered or direct will incur at least 1 call to ->iomap_begin() to get the mapping metadata, which will be 2 context-switches (and if the server has ->iomap_end() implemented, then 2 more context-switches).
Yes, I agree that's a lot of context switching for file IO...
Quoted 2 lines
But it seems like the logic for retrieving mapping offsets/lengths/metadata should be pretty straightforward?
...but it gets very cheap if the fuse server can cache mappings in the kernel to avoid all that. That is, incidentally, what patchset #7 implements.
Quoted 5 lines
If the extent lookups are table lookups or tree
traversals without complex side effects, then having ->iomap_begin()/->iomap_end() be executed as a bpf program would avoid the context switches and allow all the caching logic to be moved from the kernel to the server-side (eg using bpf maps).Quoted 7 lines
Quoted 2 lines
Hrmm. Now that /is/ an interesting proposal. Does BPF have a data structure that supports interval mappings? I think the existing bpf map
Not yet but I don't see why a b+ tree like data strucutre couldn't be added. Maybe one workaround in the meantime that could work is using a sorted array map and doing binary search on that, until interval mappings can be natively supported?
I guess, though I already had a C structure to borrow from xfs ;)
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
only does key -> value. Also, is there an upper limit on the size of a map? You could have hundreds of millions of maps for a very fragmented regular file.
If I'm remembering correctly, there's an upper limit on the number of map entries, which is bounded by u32
That's problematic, since files can have 64-bit logical block numbers.
The key size supports 64-bits. The u32 bound would be the limit on the number of extents for the file.
Quoted 18 lines
Quoted 18 lines
Quoted 5 lines
At one point I suggested to the famfs maintainer that it might be easier/better to implement the interleaved mapping lookups as bpf programs instead of being stuck with a fixed format in the fuse userspace abi, but I don't know if he ever implemented that.
This seems like a good use case for it too
Quoted 10 lines
Quoted 6 lines
Is this your
assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.For disk-based filesystems I think it would be difficult to model a bpf program to do mappings, since they can basically point anywhere and be of any size.
Quoted 7 lines
Quoted 4 lines
Hmm I'm not familiar enough with disk-based filesystems to know what the "point anywhere and be of any size" means. For the mapping stuff, doesn't it just point to a block number? Or are you saying the problem would be there's too many mappings since a mapping could be any size?
The second -- mappings can be any size, and unprivileged userspace can control the mappings.
If I'm understanding what you're saying here, this is the same discussion as the one above about the u32 bound, correct?
Quoted 6 lines
Quoted 6 lines
I was thinking the issue would be more that there might be other logic inside ->iomap_begin()/->iomap_end() besides the mapping stuff that would need to be done that would be too out-of-scope for bpf. But I think I need to read through the fuse4fs stuff to understand more what it's doing in those functions.
Looking at fuse4fs logic cursorily, it seems doable? What I like about offloading this to bpf too is it would also then allow John's famfs to just go through your iomap plumbing as a use case of it instead of being an entirely separate thing. Though maybe there's some other reason for that that you guys have discussed prior. In any case, I'll ask this on John's main famfs patchset. It kind of seems to me that you guys are pretty much doing the exact same thing conceptually.
Thanks,
Joanne
Quoted 86 lines
<nod>
--D
Quoted 81 lines
Thanks,
JoanneQuoted 77 lines
OTOH it would be enormously hilarious to me if one could load a file mapping predictive model into the kernel as a bpf program and use that as a first tier before checking the in-memory btree mapping cache from patchset 7. Quite a few years ago now there was a FAST paper establishing that even a stupid linear regression model could in theory beat a disk btree lookup.
--D
Quoted 67 lines
Thanks,
JoanneQuoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 67 lines · Joanne Koong
Quoted 66 lines · Darrick J. Wong
Quoted 50 lines · Joanne Koong
Quoted 14 lines · Darrick J. Wong
Quoted 12 lines · Joanne Koong
Quoted 10 lines · Darrick J. Wong
Hi all,
This series connects fuse (the userspace filesystem layer) to fs-iomap to get fuse servers out of the business of handling file I/O themselves. By keeping the IO path mostly within the kernel, we can dramatically improve the speed of disk-based filesystems. This enables us to move all the filesystem metadata parsing code out of the kernel and into userspace, which means that we can containerize them for security without losing a lot of performance.
Quoted 6 lines
Quoted 3 lines
I haven't looked through how the fuse2fs or fuse4fs servers are implemented yet (also, could you explain the difference between the two? Which one should we look at to see how it all ties together?),
fuse4fs is a lowlevel fuse server; fuse2fs is a high(?) level fuse server. fuse4fs is the successor to fuse2fs, at least on Linux and BSD.
Ah I see, thanks for the explanation. In that case, I'll just look at fuse4fs then.
Quoted 25 lines
Quoted 7 lines
but I wonder if having bpf infrastructure hooked up to fuse would be especially helpful for what you're doing here with fuse iomap. afaict, every read/write whether it's buffered or direct will incur at least 1 call to ->iomap_begin() to get the mapping metadata, which will be 2 context-switches (and if the server has ->iomap_end() implemented, then 2 more context-switches).
Yes, I agree that's a lot of context switching for file IO...
Quoted 2 lines
But it seems like the logic for retrieving mapping offsets/lengths/metadata should be pretty straightforward?
...but it gets very cheap if the fuse server can cache mappings in the kernel to avoid all that. That is, incidentally, what patchset #7 implements.
Quoted 5 lines
If the extent lookups are table lookups or tree
traversals without complex side effects, then having ->iomap_begin()/->iomap_end() be executed as a bpf program would avoid the context switches and allow all the caching logic to be moved from the kernel to the server-side (eg using bpf maps).Quoted 7 lines
Quoted 2 lines
Hrmm. Now that /is/ an interesting proposal. Does BPF have a data structure that supports interval mappings? I think the existing bpf map
Not yet but I don't see why a b+ tree like data strucutre couldn't be added. Maybe one workaround in the meantime that could work is using a sorted array map and doing binary search on that, until interval mappings can be natively supported?
I guess, though I already had a C structure to borrow from xfs ;)
Quoted 4 lines
Quoted 3 lines
only does key -> value. Also, is there an upper limit on the size of a map? You could have hundreds of millions of maps for a very fragmented regular file.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
If I'm remembering correctly, there's an upper limit on the number of map entries, which is bounded by u32
That's problematic, since files can have 64-bit logical block numbers.
The key size supports 64-bits. The u32 bound would be the limit on the number of extents for the file.
Oh, ok. If one treats the incore map as a cache and evicts things when they get too old, then that would be fine. I misread that as an upper limit on the *range* of the map entry keys. :/
As it stands, I need to figure out a way to trim the iomap btree when memory gets tight. Right now it'll drop the cache whenever someone closes the file, but that won't help for long-life processes that open a heavily fragmented file and never close it.
A coding-intensive way to do that would be to register a shrinker and deal with that, but ugh. A really stupid way would be to drop the whole cache once you get beyond (say) 64k of memory usage (~2000 mappings).
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 4 lines
At one point I suggested to the famfs maintainer that it might be easier/better to implement the interleaved mapping lookups as bpf programs instead of being stuck with a fixed format in the fuse userspace abi, but I don't know if he ever implemented that.
This seems like a good use case for it too
Quoted 10 lines
Quoted 6 lines
Is this your
assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.For disk-based filesystems I think it would be difficult to model a bpf program to do mappings, since they can basically point anywhere and be of any size.
Hmm I'm not familiar enough with disk-based filesystems to know what the "point anywhere and be of any size" means. For the mapping stuff, doesn't it just point to a block number? Or are you saying the problem would be there's too many mappings since a mapping could be any size?
Quoted 5 lines
Quoted 2 lines
The second -- mappings can be any size, and unprivileged userspace can control the mappings.
If I'm understanding what you're saying here, this is the same discussion as the one above about the u32 bound, correct?
A different thing -- file data mappings are irregularly sized, can contain sparse holes, etc. Userspace controls the size and offset of each mapping record (thanks to magic things like fallocate) so it'd be very difficult to create a bpf program to generate mappings on the fly.
Also you could have 2^33 mappings records for a file, so I think you can't even write a bpf program that large.
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
I was thinking the issue would be more that there might be other logic inside ->iomap_begin()/->iomap_end() besides the mapping stuff that would need to be done that would be too out-of-scope for bpf. But I think I need to read through the fuse4fs stuff to understand more what it's doing in those functions.
Quoted 7 lines
Looking at fuse4fs logic cursorily, it seems doable? What I like about offloading this to bpf too is it would also then allow John's famfs to just go through your iomap plumbing as a use case of it instead of being an entirely separate thing. Though maybe there's some other reason for that that you guys have discussed prior. In any case, I'll ask this on John's main famfs patchset. It kind of seems to me that you guys are pretty much doing the exact same thing conceptually.
Yes, though John's famfs has the nice property that memory controller interleaving is mathematically regular and likely makes for a compact bpf program.
--D
Quoted 90 lines
Thanks,
JoanneQuoted 86 lines
<nod>
--D
Quoted 81 lines
Thanks,
JoanneQuoted 77 lines
OTOH it would be enormously hilarious to me if one could load a file mapping predictive model into the kernel as a bpf program and use that as a first tier before checking the in-memory btree mapping cache from patchset 7. Quite a few years ago now there was a FAST paper establishing that even a stupid linear regression model could in theory beat a disk btree lookup.
--D
Quoted 67 lines
Thanks,
JoanneQuoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 72 lines · Darrick J. Wong
Quoted 70 lines · Joanne Koong
Quoted 69 lines · Darrick J. Wong
Quoted 50 lines · Joanne Koong
Quoted 14 lines · Darrick J. Wong
Quoted 12 lines · Joanne Koong
Quoted 10 lines · Darrick J. Wong
Hi all,
This series connects fuse (the userspace filesystem layer) to fs-iomap to get fuse servers out of the business of handling file I/O themselves. By keeping the IO path mostly within the kernel, we can dramatically improve the speed of disk-based filesystems. This enables us to move all the filesystem metadata parsing code out of the kernel and into userspace, which means that we can containerize them for security without losing a lot of performance.
Quoted 6 lines
Quoted 3 lines
I haven't looked through how the fuse2fs or fuse4fs servers are implemented yet (also, could you explain the difference between the two? Which one should we look at to see how it all ties together?),
fuse4fs is a lowlevel fuse server; fuse2fs is a high(?) level fuse server. fuse4fs is the successor to fuse2fs, at least on Linux and BSD.
Ah I see, thanks for the explanation. In that case, I'll just look at fuse4fs then.
Quoted 25 lines
Quoted 7 lines
but I wonder if having bpf infrastructure hooked up to fuse would be especially helpful for what you're doing here with fuse iomap. afaict, every read/write whether it's buffered or direct will incur at least 1 call to ->iomap_begin() to get the mapping metadata, which will be 2 context-switches (and if the server has ->iomap_end() implemented, then 2 more context-switches).
Yes, I agree that's a lot of context switching for file IO...
Quoted 2 lines
But it seems like the logic for retrieving mapping offsets/lengths/metadata should be pretty straightforward?
...but it gets very cheap if the fuse server can cache mappings in the kernel to avoid all that. That is, incidentally, what patchset #7 implements.
Quoted 5 lines
If the extent lookups are table lookups or tree
traversals without complex side effects, then having ->iomap_begin()/->iomap_end() be executed as a bpf program would avoid the context switches and allow all the caching logic to be moved from the kernel to the server-side (eg using bpf maps).Quoted 7 lines
Quoted 2 lines
Hrmm. Now that /is/ an interesting proposal. Does BPF have a data structure that supports interval mappings? I think the existing bpf map
Not yet but I don't see why a b+ tree like data strucutre couldn't be added. Maybe one workaround in the meantime that could work is using a sorted array map and doing binary search on that, until interval mappings can be natively supported?
I guess, though I already had a C structure to borrow from xfs ;)
Quoted 6 lines
Quoted 3 lines
only does key -> value. Also, is there an upper limit on the size of a map? You could have hundreds of millions of maps for a very fragmented regular file.
If I'm remembering correctly, there's an upper limit on the number of map entries, which is bounded by u32
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
That's problematic, since files can have 64-bit logical block numbers.
The key size supports 64-bits. The u32 bound would be the limit on the number of extents for the file.
Oh, ok. If one treats the incore map as a cache and evicts things when they get too old, then that would be fine. I misread that as an upper limit on the *range* of the map entry keys. :/
I think for more complicated servers, the bpf prog handling for iomap_begin() would essentially just serve as a cache where if it's not found in the cache, then it sends off the FUSE_IOMAP_BEGIN request to the server. For servers that don't need as much complicated logic (eg famfs), the iomap_begin() logic would just be executed within the bpf prog itself.
Quoted 9 lines
As it stands, I need to figure out a way to trim the iomap btree when memory gets tight. Right now it'll drop the cache whenever someone closes the file, but that won't help for long-life processes that open a heavily fragmented file and never close it.
A coding-intensive way to do that would be to register a shrinker and deal with that, but ugh. A really stupid way would be to drop the whole cache once you get beyond (say) 64k of memory usage (~2000 mappings).
This kind of seems like another point in favor of giving userspace control of the caching layer. They could then implement whatever eviction policies they want.
It also allows them to prepopulate the cache upfront (eg when servicing a file open request, if the file is below a certain size or if the server knows what'll be hot, it could put those extents into the map from the get-go).
in my opinion, the fuse-iomap layer should try to be as simple/minimal and as generic as possible. I haven't read through iomap_cache.c yet but the header comment suggests it's adapted from the xfs extent tree cache. As I understand it, different filesystem implementations have different caching architectures that are better suited for their use cases (I'm guessing that's the case, otherwise there would just be one general cache inside iomap all the filesystems would use?). It seems a lot better to me to just let the userspace server define that themselves. And selfishly from the fuse perspective, would be less code we would have to maintain. And I guess too if some servers don't need caching (like famfs?), they could avoid that overhead.
Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 14 lines
Quoted 5 lines
At one point I suggested to the famfs maintainer that it might be easier/better to implement the interleaved mapping lookups as bpf programs instead of being stuck with a fixed format in the fuse userspace abi, but I don't know if he ever implemented that.
This seems like a good use case for it too
Quoted 7 lines
Quoted 6 lines
Is this your
assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.Quoted 8 lines
Quoted 3 lines
For disk-based filesystems I think it would be difficult to model a bpf program to do mappings, since they can basically point anywhere and be of any size.
Hmm I'm not familiar enough with disk-based filesystems to know what the "point anywhere and be of any size" means. For the mapping stuff, doesn't it just point to a block number? Or are you saying the problem would be there's too many mappings since a mapping could be any size?
The second -- mappings can be any size, and unprivileged userspace can control the mappings.
Quoted 7 lines
Quoted 2 lines
If I'm understanding what you're saying here, this is the same discussion as the one above about the u32 bound, correct?
A different thing -- file data mappings are irregularly sized, can contain sparse holes, etc. Userspace controls the size and offset of each mapping record (thanks to magic things like fallocate) so it'd be very difficult to create a bpf program to generate mappings on the fly.
Would the bpf prog have to generate mappings on the fly though? If the userspace does things like fallocate, those operations would still go through to the server as a regular request (eg FUSE_FALLOCATE) and on the server side, it'd add that to the map dynamically from userspace.
Quoted 3 lines
Also you could have 2^33 mappings records for a file, so I think you can't even write a bpf program that large.
I think this depends on what map structure gets used. If there is native support added for b+ tree like data structures, I don't see why it wouldn't be able to.
Quoted 15 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
I was thinking the issue would be more that there might be other logic inside ->iomap_begin()/->iomap_end() besides the mapping stuff that would need to be done that would be too out-of-scope for bpf. But I think I need to read through the fuse4fs stuff to understand more what it's doing in those functions.
Quoted 7 lines
Looking at fuse4fs logic cursorily, it seems doable? What I like about offloading this to bpf too is it would also then allow John's famfs to just go through your iomap plumbing as a use case of it instead of being an entirely separate thing. Though maybe there's some other reason for that that you guys have discussed prior. In any case, I'll ask this on John's main famfs patchset. It kind of seems to me that you guys are pretty much doing the exact same thing conceptually.
Quoted 3 lines
Yes, though John's famfs has the nice property that memory controller interleaving is mathematically regular and likely makes for a compact bpf program.
I tried out integrating the bpf hooks into fuse for iomap_begin() just to see if it was realistic and it seems relatively straightforward so far (though maybe the devil is in the details...). I used the drivers/hid/bpf/hid_bpf_struct_ops.c program as a model for how to set up the fuse bpf struct ops on the kernel side. calling it from file_iomap.c looks something like
static int fuse_iomap_begin(...)
{
...
struct fuse_bpf_ops *bpf_ops = fuse_get_bpf_ops();
...
err = -EOPNOTSUPP;
if (bpf_ops && bpf_ops->iomap_begin)
err = bpf_ops->iomap_begin(inode, pos, len, flags, &outarg);
if (err)
err = fuse_simple_request(fm, &args);
...
}and I was able to verify that iomap_begin() is able to return back populated outarg fields from the bpf prog. If we were to actually implement it i'm sure it'd be more complicated (eg we'd need to make the fuse_bpf_ops registered per-connection, etc) but on the whole it seems doable. My worry is that if we land the iomap cache patchset now then we can't remove it in the future without breaking backwards compatibility for being a performance regression (though maybe we can since the fuse-iomap stuff is experimental?), so imo it'd be great if we figured out what direction we want to go before landing the cache stuff. And I think we need to have this conversation too on the main famfs patchset (eg whether it should go through your general iomap plumbing with bpf helpers vs. being a separate implementation) since once that lands, it'd be irrevocable.
Thanks,
Joanne
Quoted 93 lines
--D
Quoted 90 lines
Thanks,
JoanneQuoted 86 lines
<nod>
--D
Quoted 81 lines
Thanks,
JoanneQuoted 77 lines
OTOH it would be enormously hilarious to me if one could load a file mapping predictive model into the kernel as a bpf program and use that as a first tier before checking the in-memory btree mapping cache from patchset 7. Quite a few years ago now there was a FAST paper establishing that even a stupid linear regression model could in theory beat a disk btree lookup.
--D
Quoted 67 lines
Thanks,
JoanneQuoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
On Wed, Jan 28, 2026 at 05:12:54PM -0800, Joanne Koong wrote:
<snip>
Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 7 lines
Quoted 2 lines
Hrmm. Now that /is/ an interesting proposal. Does BPF have a data structure that supports interval mappings? I think the existing bpf map
Not yet but I don't see why a b+ tree like data strucutre couldn't be added. Maybe one workaround in the meantime that could work is using a sorted array map and doing binary search on that, until interval mappings can be natively supported?
I guess, though I already had a C structure to borrow from xfs ;)
Quoted 6 lines
Quoted 3 lines
only does key -> value. Also, is there an upper limit on the size of a map? You could have hundreds of millions of maps for a very fragmented regular file.
If I'm remembering correctly, there's an upper limit on the number of map entries, which is bounded by u32
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
That's problematic, since files can have 64-bit logical block numbers.
The key size supports 64-bits. The u32 bound would be the limit on the number of extents for the file.
Oh, ok. If one treats the incore map as a cache and evicts things when they get too old, then that would be fine. I misread that as an upper limit on the *range* of the map entry keys. :/
Quoted 6 lines
I think for more complicated servers, the bpf prog handling for
iomap_begin()would essentially just serve as a cache where if it's not found in the cache, then it sends off theFUSE_IOMAP_BEGINrequest to the server. For servers that don't need as much complicated logic (eg famfs), theiomap_begin()logic would just be executed within the bpf prog itself.
Yes, I like the fuse_iomap_begin logic flow of:
1. Try to use a mapping in the iext tree
2. Call a BPF program to try to generate a mapping
3. Issue a fuse command to userspace
wherein #2 and #3 can signal that #1 should be retried. (This is already provided by FUSE_IOMAP_TYPE_RETRY_CACHE, FWIW)
That said, BPF doesn't expose an interval btree data structure. I think it would be better to add the iext mapping cache and make it so that bpf programs could call fuse_iomap_cache_{upsert,remove,lookup}. You could use the interval tree too, but the iext tree has the advantage of higher fanout factor.
Quoted 5 lines
Quoted 5 lines
As it stands, I need to figure out a way to trim the iomap btree when memory gets tight. Right now it'll drop the cache whenever someone closes the file, but that won't help for long-life processes that open a heavily fragmented file and never close it.
Quoted 7 lines
Quoted 3 lines
A coding-intensive way to do that would be to register a shrinker and deal with that, but ugh. A really stupid way would be to drop the whole cache once you get beyond (say) 64k of memory usage (~2000 mappings).
This kind of seems like another point in favor of giving userspace control of the caching layer. They could then implement whatever eviction policies they want.
Note that userspace already can control the cached iomappings -- FUSE_NOTIFY_IOMAP_UPSERT pushes a mapping into the iext tree, and FUSE_NOTIFY_IOMAP_INVAL removes them. The fuse server can decide to evict whenever it pleases, though admittedly the iext tree doesn't track usage information of any kind, so how would the fuse server know?
The static limit is merely the kernel's means to establish a hard limit on the memory consumption of the iext tree, since it can't trust userspace completely.
Quoted 4 lines
It also allows them to prepopulate the cache upfront (eg when servicing a file open request, if the file is below a certain size or if the server knows what'll be hot, it could put those extents into the map from the get-go).
Hrm. I haven't tried issuing FUSE_NOTIFY_IOMAP_UPSERT during an open call, but I suppose it's possible.
Quoted 3 lines
in my opinion, the fuse-iomap layer should try to be as simple/minimal and as generic as possible. I haven't read through
iomap_cache.cyet but the header comment suggests it's adapted from the xfs extent tree
Rudely copied, not adapted ;)
I actually wonder if I should make a horrible macro to generate the fuse_iext_* structures and functions, and then xfs_iext_tree.c and fuse_iomap_cache.c can "share" that hairba^Wcode.
Quoted 3 lines
cache. As I understand it, different filesystem implementations have different caching architectures that are better suited for their use cases
Err. The way this evolved is ... way too long to go into in this email. Here's a truncated version; I can tell you the full story next week.
Most filesystems store their file mapping data on disk in whatever format the designers specified. When the pagecache asks them to read or write the cache, they attach buffer heads to the folio, fill out the buffer heads with the minimum mapping information needed to map the folios to disk addresses. bios are constructed for each folio based on what's in the bufferhead.
This was fine for filesystems that map each block individually, such as FFS/ext2/ext3/fat...
Quoted 2 lines
(I'm guessing that's the case, otherwise there would just be one general cache inside iomap all the filesystems would use?). It seems a
...but newer filesystems such as xfs/ext4/btrfs map a bunch of blocks at a time. Each of them invented their own private incore mapping structures to mirror the ondisk structure. xfs kept using the old bufferheads into the early 2010s, ext4 is still using them, and btrfs went its own way from the start.
Eventually XFS grew its own internal extent-to-bio mapping code that flipped the model -- rather than get a pagecache folio, map the folio to blocks, and issue IOs based on the blocks, it would get the file mapping, grab folios for the whole mapping, and issue bios for the batch of folios. That's more efficient, but at this point we have a legacy codebase problem for everything else in fs/.
In 2019, hch and I decided to export the extent-to-bio mapping code from xfs so that new filesystems could start with something cleaner than bufferheads. In the past 7 years, nobody's added a new filesystem with complex mapping requirements; they've only ported existing filesystems to it, without further refactoring of their incore data structures. That's why there's no generic iomap cache.
Quoted 2 lines
lot better to me to just let the userspace server define that themselves. And selfishly from the fuse perspective, would be less
Well if I turned the iext code into a template then fuse would only need enough glue code to declare a template class and use it. The glue part is only ... 230LOC.
Quoted 2 lines
code we would have to maintain. And I guess too if some servers don't need caching (like famfs?), they could avoid that overhead.
Hrm. Right now the struct fuse_iomap_cache is embedded in struct fuse_inode, but that could be turned into a dynamic allocation.
Quoted 33 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 13 lines
Quoted 4 lines
At one point I suggested to the famfs maintainer that it might be easier/better to implement the interleaved mapping lookups as bpf programs instead of being stuck with a fixed format in the fuse userspace abi, but I don't know if he ever implemented that.
This seems like a good use case for it too
Quoted 7 lines
Quoted 6 lines
Is this your
assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.Quoted 8 lines
Quoted 3 lines
For disk-based filesystems I think it would be difficult to model a bpf program to do mappings, since they can basically point anywhere and be of any size.
Hmm I'm not familiar enough with disk-based filesystems to know what the "point anywhere and be of any size" means. For the mapping stuff, doesn't it just point to a block number? Or are you saying the problem would be there's too many mappings since a mapping could be any size?
The second -- mappings can be any size, and unprivileged userspace can control the mappings.
Quoted 7 lines
Quoted 2 lines
If I'm understanding what you're saying here, this is the same discussion as the one above about the u32 bound, correct?
A different thing -- file data mappings are irregularly sized, can contain sparse holes, etc. Userspace controls the size and offset of each mapping record (thanks to magic things like fallocate) so it'd be very difficult to create a bpf program to generate mappings on the fly.
Quoted 4 lines
Would the bpf prog have to generate mappings on the fly though? If the userspace does things like fallocate, those operations would still go through to the server as a regular request (eg
FUSE_FALLOCATE) and on the server side, it'd add that to the map dynamically from userspace.
That depends on the fuse server design. For simple things like famfs where the layout is bog simple and there's no fancy features like delayed allocation or unwritten extents, then you could probably get away a BPF program to generate the entire mapping set. I suspect an object-store type filesystem (aka write a file once, close it, snapshot it, and never change it again) might be good at landing all the file data in relatively few extent mappings, and it could actually compile a custom bpf program for that file and push it to the kernel.
Quoted 6 lines
Quoted 2 lines
Also you could have 2^33 mappings records for a file, so I think you can't even write a bpf program that large.
I think this depends on what map structure gets used. If there is native support added for b+ tree like data structures, I don't see why it wouldn't be able to.
<nod>
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
I was thinking the issue would be more that there might be other logic inside ->iomap_begin()/->iomap_end() besides the mapping stuff that would need to be done that would be too out-of-scope for bpf. But I think I need to read through the fuse4fs stuff to understand more what it's doing in those functions.
Looking at fuse4fs logic cursorily, it seems doable? What I like about offloading this to bpf too is it would also then allow John's famfs to just go through your iomap plumbing as a use case of it instead of being an entirely separate thing. Though maybe there's some other reason for that that you guys have discussed prior. In any case, I'll ask this on John's main famfs patchset. It kind of seems to me that you guys are pretty much doing the exact same thing conceptually.
Quoted 7 lines
Quoted 3 lines
Yes, though John's famfs has the nice property that memory controller interleaving is mathematically regular and likely makes for a compact bpf program.
I tried out integrating the bpf hooks into fuse for
iomap_begin()just to see if it was realistic and it seems relatively straightforward so far (though maybe the devil is in the details...). I used the
Ok, now *that's* interesting! I guess I had better push the latest fuse-iomap code ... but I cannot share a link, because I cannot get through the @!#%%!!! kernel.org anubis bullcrap.
So I generated a pull request and I *think* this munged URL will work
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-service-container_2026-01-29
Or I guess you could just git-pull this:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git tags/fuse-service-container_2026-01-29
Quoted 5 lines
drivers/hid/bpf/hid_bpf_struct_ops.cprogram as a model for how to set up the fuse bpf struct ops on the kernel side. calling it fromfile_iomap.clooks something likestatic int
fuse_iomap_begin(...)
Quoted 11 lines
{
...
struct fuse_bpf_ops *bpf_ops = fuse_get_bpf_ops();
...
err = -EOPNOTSUPP;
if (bpf_ops && bpf_ops->iomap_begin)
err = bpf_ops->iomap_begin(inode, pos, len, flags, &outarg);
if (err)
err = fuse_simple_request(fm, &args);
...
}I'm curious what the rest of the bpf integration code looks like.
Quoted 4 lines
and I was able to verify that
iomap_begin()is able to return back populated outarg fields from the bpf prog. If we were to actually implement it i'm sure it'd be more complicated (eg we'd need to make thefuse_bpf_opsregistered per-connection, etc) but on the whole it
What is a fuse_bpf_ops? I'm assuming that's the attachment point for a bpf program that the fuse server would compile? In which case, yes, I think that ought to be per-connection.
So the bpf program can examine the struct inode, and the pos/len/opflags field; and from that information it has to write the appropriate fields in &outarg? That's new, I didn't think bpf was allowed to write to kernel memory. But it's been a few years since I last touched the bpf internals.
Some bpf programs might just know how to fill out outarg on their own (e.g. famfs memory interleaving) but other bpf programs might perform a range query on some imaginary bpf-interval-tree wherein you can do a fast lookup based on (inumber, pos, len)?
I guess that's an interesting question -- would each fuse connection have one big bpf-interval-tree? Or would you shard things by inode to reduce contention? And if you sharded like that, then would you need a fuse_bpf_ops per inode?
(I'm imagining that the fuse_bpf_ops might be where you'd stash the root of the bpf data structure, but I know nothing of bpf internals ;))
Rolling on: how easy is it for a userspace program to compile and upload bpf programs into the kernel? I've played around with bcc enough to write some fairly stupid latency tracing tools for XFS, but at the end of the day it still python scripts feeding a string full of maybe-C into whatever the BPF does under the hood.
I /think/ it calls clang on the provided text, links that against the current kernel's header files, and pushes the compiled bpf binary into the kernel, right? So fuse4fs would have to learn how to do that; and now fuse4fs has a runtime dependency on libllvm.
And while I'm on the topic of fuse-bpf uapi: It's ok for us to expose primitive-typed variables (pos/len/opflags) and existing fuse uapi directly to a bpf program, but I don't think we should expose struct inode/fuse_inode. Maybe just fuse_inode::nodeid? If we're careful not to allow #include'ing structured types in the fuse bpf code, then perhaps the bpf programs could be compiled at the same time as the fuse server.
Quoted 4 lines
seems doable. My worry is that if we land the iomap cache patchset now then we can't remove it in the future without breaking backwards compatibility for being a performance regression (though maybe we can since the fuse-iomap stuff is experimental?), so imo it'd be great if
I don't think it's a huge problem to remove functionality while the EXPERIMENTAL warnings are in place. We'd forever lose the command codes for FUSE_NOTIFY_IOMAP_UPSERT and FUSE_NOTIFY_IOMAP_INVAL, but we've only used 12 out of INT_MAX so that's not likely to be a concern.
Quoted 5 lines
we figured out what direction we want to go before landing the cache stuff. And I think we need to have this conversation too on the main famfs patchset (eg whether it should go through your general iomap plumbing with bpf helpers vs. being a separate implementation) since once that lands, it'd be irrevocable.
I've of two minds on that -- John got here first, so I don't want to delay his patchset whilst I slowly work on this thing. OTOH from an architecture standpoint we probably ought to push for three ways for a fuse server to upload mappings:
1. Upserting mappings with arbitrary offset and size into a cache
2. Self contained bpf program that can generate any mapping
3. Sprawling bpf program that can read any other artifacts that another bpf program might have set up for it
But yeah, let's involve John.
--D
Quoted 97 lines
Thanks,
JoanneQuoted 93 lines
--D
Quoted 90 lines
Thanks,
JoanneQuoted 86 lines
<nod>
--D
Quoted 81 lines
Thanks,
JoanneQuoted 77 lines
OTOH it would be enormously hilarious to me if one could load a file mapping predictive model into the kernel as a bpf program and use that as a first tier before checking the in-memory btree mapping cache from patchset 7. Quite a few years ago now there was a FAST paper establishing that even a stupid linear regression model could in theory beat a disk btree lookup.
--D
Quoted 67 lines
Thanks,
JoanneQuoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 87 lines · Darrick J. Wong
On Wed, Jan 28, 2026 at 05:12:54PM -0800, Joanne Koong wrote:
<snip>
Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 7 lines
Quoted 2 lines
Hrmm. Now that /is/ an interesting proposal. Does BPF have a data structure that supports interval mappings? I think the existing bpf map
Not yet but I don't see why a b+ tree like data strucutre couldn't be added. Maybe one workaround in the meantime that could work is using a sorted array map and doing binary search on that, until interval mappings can be natively supported?
I guess, though I already had a C structure to borrow from xfs ;)
Quoted 6 lines
Quoted 3 lines
only does key -> value. Also, is there an upper limit on the size of a map? You could have hundreds of millions of maps for a very fragmented regular file.
If I'm remembering correctly, there's an upper limit on the number of map entries, which is bounded by u32
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
That's problematic, since files can have 64-bit logical block numbers.
The key size supports 64-bits. The u32 bound would be the limit on the number of extents for the file.
Oh, ok. If one treats the incore map as a cache and evicts things when they get too old, then that would be fine. I misread that as an upper limit on the *range* of the map entry keys. :/
Quoted 6 lines
I think for more complicated servers, the bpf prog handling for
iomap_begin()would essentially just serve as a cache where if it's not found in the cache, then it sends off theFUSE_IOMAP_BEGINrequest to the server. For servers that don't need as much complicated logic (eg famfs), theiomap_begin()logic would just be executed within the bpf prog itself.Yes, I like the
fuse_iomap_beginlogic flow of:1. Try to use a mapping in the iext tree
2. Call a BPF program to try to generate a mapping
3. Issue a fuse command to userspacewherein #2 and #3 can signal that #1 should be retried. (This is already provided by
FUSE_IOMAP_TYPE_RETRY_CACHE, FWIW)That said, BPF doesn't expose an interval btree data structure. I think it would be better to add the iext mapping cache and make it so that bpf programs could call fuse_iomap_cache_{upsert,remove,lookup}. You could use the interval tree too, but the iext tree has the advantage of higher fanout factor.
Quoted 5 lines
Quoted 5 lines
As it stands, I need to figure out a way to trim the iomap btree when memory gets tight. Right now it'll drop the cache whenever someone closes the file, but that won't help for long-life processes that open a heavily fragmented file and never close it.
Quoted 7 lines
Quoted 3 lines
A coding-intensive way to do that would be to register a shrinker and deal with that, but ugh. A really stupid way would be to drop the whole cache once you get beyond (say) 64k of memory usage (~2000 mappings).
This kind of seems like another point in favor of giving userspace control of the caching layer. They could then implement whatever eviction policies they want.
Note that userspace already can control the cached iomappings --
FUSE_NOTIFY_IOMAP_UPSERTpushes a mapping into the iext tree, andFUSE_NOTIFY_IOMAP_INVALremoves them. The fuse server can decide to evict whenever it pleases, though admittedly the iext tree doesn't track usage information of any kind, so how would the fuse server know?The static limit is merely the kernel's means to establish a hard limit on the memory consumption of the iext tree, since it can't trust userspace completely.
Quoted 4 lines
It also allows them to prepopulate the cache upfront (eg when servicing a file open request, if the file is below a certain size or if the server knows what'll be hot, it could put those extents into the map from the get-go).
Hrm. I haven't tried issuing
FUSE_NOTIFY_IOMAP_UPSERTduring an open call, but I suppose it's possible.Quoted 3 lines
in my opinion, the fuse-iomap layer should try to be as simple/minimal and as generic as possible. I haven't read through
iomap_cache.cyet but the header comment suggests it's adapted from the xfs extent tree
Quoted 5 lines
Rudely copied, not adapted ;)
I actually wonder if I should make a horrible macro to generate the fuse_iext_* structures and functions, and then
xfs_iext_tree.candfuse_iomap_cache.ccan "share" that hairba^Wcode.
I tried templatizing this with cpp macros and very rapidly lost all sanity. :(
--D
Quoted 335 lines
Quoted 3 lines
cache. As I understand it, different filesystem implementations have different caching architectures that are better suited for their use cases
Err. The way this evolved is ... way too long to go into in this email. Here's a truncated version; I can tell you the full story next week.
Most filesystems store their file mapping data on disk in whatever format the designers specified. When the pagecache asks them to read or write the cache, they attach buffer heads to the folio, fill out the buffer heads with the minimum mapping information needed to map the folios to disk addresses. bios are constructed for each folio based on what's in the bufferhead.
This was fine for filesystems that map each block individually, such as FFS/ext2/ext3/fat...
Quoted 2 lines
(I'm guessing that's the case, otherwise there would just be one general cache inside iomap all the filesystems would use?). It seems a
...but newer filesystems such as xfs/ext4/btrfs map a bunch of blocks at a time. Each of them invented their own private incore mapping structures to mirror the ondisk structure. xfs kept using the old bufferheads into the early 2010s, ext4 is still using them, and btrfs went its own way from the start.
Eventually XFS grew its own internal extent-to-bio mapping code that flipped the model -- rather than get a pagecache folio, map the folio to blocks, and issue IOs based on the blocks, it would get the file mapping, grab folios for the whole mapping, and issue bios for the batch of folios. That's more efficient, but at this point we have a legacy codebase problem for everything else in
fs/.In 2019, hch and I decided to export the extent-to-bio mapping code from xfs so that new filesystems could start with something cleaner than bufferheads. In the past 7 years, nobody's added a new filesystem with complex mapping requirements; they've only ported existing filesystems to it, without further refactoring of their incore data structures. That's why there's no generic iomap cache.
Quoted 2 lines
lot better to me to just let the userspace server define that themselves. And selfishly from the fuse perspective, would be less
Well if I turned the iext code into a template then fuse would only need enough glue code to declare a template class and use it. The glue part is only ... 230LOC.
Quoted 2 lines
code we would have to maintain. And I guess too if some servers don't need caching (like famfs?), they could avoid that overhead.
Hrm. Right now the struct
fuse_iomap_cacheis embedded in structfuse_inode, but that could be turned into a dynamic allocation.Quoted 33 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 13 lines
Quoted 4 lines
At one point I suggested to the famfs maintainer that it might be easier/better to implement the interleaved mapping lookups as bpf programs instead of being stuck with a fixed format in the fuse userspace abi, but I don't know if he ever implemented that.
This seems like a good use case for it too
Quoted 7 lines
Quoted 6 lines
Is this your
assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.Quoted 8 lines
Quoted 3 lines
For disk-based filesystems I think it would be difficult to model a bpf program to do mappings, since they can basically point anywhere and be of any size.
Hmm I'm not familiar enough with disk-based filesystems to know what the "point anywhere and be of any size" means. For the mapping stuff, doesn't it just point to a block number? Or are you saying the problem would be there's too many mappings since a mapping could be any size?
The second -- mappings can be any size, and unprivileged userspace can control the mappings.
Quoted 7 lines
Quoted 2 lines
If I'm understanding what you're saying here, this is the same discussion as the one above about the u32 bound, correct?
A different thing -- file data mappings are irregularly sized, can contain sparse holes, etc. Userspace controls the size and offset of each mapping record (thanks to magic things like fallocate) so it'd be very difficult to create a bpf program to generate mappings on the fly.
Quoted 4 lines
Would the bpf prog have to generate mappings on the fly though? If the userspace does things like fallocate, those operations would still go through to the server as a regular request (eg
FUSE_FALLOCATE) and on the server side, it'd add that to the map dynamically from userspace.That depends on the fuse server design. For simple things like famfs where the layout is bog simple and there's no fancy features like delayed allocation or unwritten extents, then you could probably get away a BPF program to generate the entire mapping set. I suspect an object-store type filesystem (aka write a file once, close it, snapshot it, and never change it again) might be good at landing all the file data in relatively few extent mappings, and it could actually compile a custom bpf program for that file and push it to the kernel.
Quoted 6 lines
Quoted 2 lines
Also you could have 2^33 mappings records for a file, so I think you can't even write a bpf program that large.
I think this depends on what map structure gets used. If there is native support added for b+ tree like data structures, I don't see why it wouldn't be able to.
<nod>
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
I was thinking the issue would be more that there might be other logic inside ->iomap_begin()/->iomap_end() besides the mapping stuff that would need to be done that would be too out-of-scope for bpf. But I think I need to read through the fuse4fs stuff to understand more what it's doing in those functions.
Looking at fuse4fs logic cursorily, it seems doable? What I like about offloading this to bpf too is it would also then allow John's famfs to just go through your iomap plumbing as a use case of it instead of being an entirely separate thing. Though maybe there's some other reason for that that you guys have discussed prior. In any case, I'll ask this on John's main famfs patchset. It kind of seems to me that you guys are pretty much doing the exact same thing conceptually.
Quoted 7 lines
Quoted 3 lines
Yes, though John's famfs has the nice property that memory controller interleaving is mathematically regular and likely makes for a compact bpf program.
I tried out integrating the bpf hooks into fuse for
iomap_begin()just to see if it was realistic and it seems relatively straightforward so far (though maybe the devil is in the details...). I used theOk, now *that's* interesting! I guess I had better push the latest fuse-iomap code ... but I cannot share a link, because I cannot get through the @!#%%!!! kernel.org anubis bullcrap.
So I generated a pull request and I *think* this munged URL will work
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-service-container_2026-01-29Or I guess you could just git-pull this:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git tags/fuse-service-container_2026-01-29Quoted 5 lines
drivers/hid/bpf/hid_bpf_struct_ops.cprogram as a model for how to set up the fuse bpf struct ops on the kernel side. calling it fromfile_iomap.clooks something likestatic int
fuse_iomap_begin(...)Quoted 11 lines
{ ... struct fuse_bpf_ops *bpf_ops = fuse_get_bpf_ops(); ... err = -EOPNOTSUPP; if (bpf_ops && bpf_ops->iomap_begin) err = bpf_ops->iomap_begin(inode, pos, len, flags, &outarg); if (err) err = fuse_simple_request(fm, &args); ... }I'm curious what the rest of the bpf integration code looks like.
Quoted 4 lines
and I was able to verify that
iomap_begin()is able to return back populated outarg fields from the bpf prog. If we were to actually implement it i'm sure it'd be more complicated (eg we'd need to make thefuse_bpf_opsregistered per-connection, etc) but on the whole itWhat is a
fuse_bpf_ops? I'm assuming that's the attachment point for a bpf program that the fuse server would compile? In which case, yes, I think that ought to be per-connection.So the bpf program can examine the struct inode, and the pos/len/opflags field; and from that information it has to write the appropriate fields in &outarg? That's new, I didn't think bpf was allowed to write to kernel memory. But it's been a few years since I last touched the bpf internals.
Some bpf programs might just know how to fill out outarg on their own (e.g. famfs memory interleaving) but other bpf programs might perform a range query on some imaginary bpf-interval-tree wherein you can do a fast lookup based on (inumber, pos, len)?
I guess that's an interesting question -- would each fuse connection have one big bpf-interval-tree? Or would you shard things by inode to reduce contention? And if you sharded like that, then would you need a
fuse_bpf_opsper inode?(I'm imagining that the
fuse_bpf_opsmight be where you'd stash the root of the bpf data structure, but I know nothing of bpf internals ;))Rolling on: how easy is it for a userspace program to compile and upload bpf programs into the kernel? I've played around with bcc enough to write some fairly stupid latency tracing tools for XFS, but at the end of the day it still python scripts feeding a string full of maybe-C into whatever the BPF does under the hood.
I /think/ it calls clang on the provided text, links that against the current kernel's header files, and pushes the compiled bpf binary into the kernel, right? So fuse4fs would have to learn how to do that; and now fuse4fs has a runtime dependency on libllvm.
And while I'm on the topic of fuse-bpf uapi: It's ok for us to expose primitive-typed variables (pos/len/opflags) and existing fuse uapi directly to a bpf program, but I don't think we should expose struct inode/fuse_inode. Maybe just
fuse_inode::nodeid? If we're careful not to allow #include'ing structured types in the fuse bpf code, then perhaps the bpf programs could be compiled at the same time as the fuse server.Quoted 4 lines
seems doable. My worry is that if we land the iomap cache patchset now then we can't remove it in the future without breaking backwards compatibility for being a performance regression (though maybe we can since the fuse-iomap stuff is experimental?), so imo it'd be great if
I don't think it's a huge problem to remove functionality while the EXPERIMENTAL warnings are in place. We'd forever lose the command codes for
FUSE_NOTIFY_IOMAP_UPSERTandFUSE_NOTIFY_IOMAP_INVAL, but we've only used 12 out ofINT_MAXso that's not likely to be a concern.Quoted 5 lines
we figured out what direction we want to go before landing the cache stuff. And I think we need to have this conversation too on the main famfs patchset (eg whether it should go through your general iomap plumbing with bpf helpers vs. being a separate implementation) since once that lands, it'd be irrevocable.
I've of two minds on that -- John got here first, so I don't want to delay his patchset whilst I slowly work on this thing. OTOH from an architecture standpoint we probably ought to push for three ways for a fuse server to upload mappings:
1. Upserting mappings with arbitrary offset and size into a cache
2. Self contained bpf program that can generate any mapping
3. Sprawling bpf program that can read any other artifacts that another bpf program might have set up for itBut yeah, let's involve John.
--D
Quoted 97 lines
Thanks,
JoanneQuoted 93 lines
--D
Quoted 90 lines
Thanks,
JoanneQuoted 86 lines
<nod>
--D
Quoted 81 lines
Thanks,
JoanneQuoted 77 lines
OTOH it would be enormously hilarious to me if one could load a file mapping predictive model into the kernel as a bpf program and use that as a first tier before checking the in-memory btree mapping cache from patchset 7. Quite a few years ago now there was a FAST paper establishing that even a stupid linear regression model could in theory beat a disk btree lookup.
--D
Quoted 67 lines
Thanks,
JoanneQuoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
In reply to Darrick J. WongQuoted 62 lines · Darrick J. Wong
On Wed, Jan 28, 2026 at 05:12:54PM -0800, Joanne Koong wrote:
<snip>
Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 7 lines
Quoted 2 lines
Hrmm. Now that /is/ an interesting proposal. Does BPF have a data structure that supports interval mappings? I think the existing bpf map
Not yet but I don't see why a b+ tree like data strucutre couldn't be added. Maybe one workaround in the meantime that could work is using a sorted array map and doing binary search on that, until interval mappings can be natively supported?
I guess, though I already had a C structure to borrow from xfs ;)
Quoted 6 lines
Quoted 3 lines
only does key -> value. Also, is there an upper limit on the size of a map? You could have hundreds of millions of maps for a very fragmented regular file.
If I'm remembering correctly, there's an upper limit on the number of map entries, which is bounded by u32
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
That's problematic, since files can have 64-bit logical block numbers.
The key size supports 64-bits. The u32 bound would be the limit on the number of extents for the file.
Oh, ok. If one treats the incore map as a cache and evicts things when they get too old, then that would be fine. I misread that as an upper limit on the *range* of the map entry keys. :/
Quoted 6 lines
I think for more complicated servers, the bpf prog handling for
iomap_begin()would essentially just serve as a cache where if it's not found in the cache, then it sends off theFUSE_IOMAP_BEGINrequest to the server. For servers that don't need as much complicated logic (eg famfs), theiomap_begin()logic would just be executed within the bpf prog itself.Yes, I like the
fuse_iomap_beginlogic flow of:1. Try to use a mapping in the iext tree
2. Call a BPF program to try to generate a mapping
3. Issue a fuse command to userspacewherein #2 and #3 can signal that #1 should be retried. (This is already provided by
FUSE_IOMAP_TYPE_RETRY_CACHE, FWIW)That said, BPF doesn't expose an interval btree data structure. I think it would be better to add the iext mapping cache and make it so that bpf programs could call fuse_iomap_cache_{upsert,remove,lookup}. You could use the interval tree too, but the iext tree has the advantage of higher fanout factor.
Quoted 9 lines
Quoted 8 lines
As it stands, I need to figure out a way to trim the iomap btree when memory gets tight. Right now it'll drop the cache whenever someone closes the file, but that won't help for long-life processes that open a heavily fragmented file and never close it.
A coding-intensive way to do that would be to register a shrinker and deal with that, but ugh. A really stupid way would be to drop the whole cache once you get beyond (say) 64k of memory usage (~2000 mappings).
Quoted 7 lines
Quoted 3 lines
This kind of seems like another point in favor of giving userspace control of the caching layer. They could then implement whatever eviction policies they want.
Note that userspace already can control the cached iomappings --
FUSE_NOTIFY_IOMAP_UPSERTpushes a mapping into the iext tree, andFUSE_NOTIFY_IOMAP_INVALremoves them. The fuse server can decide to
This incurs round-trip context-switch costs though, which the bpf prog approach doesn't incur.
Quoted 58 lines
evict whenever it pleases, though admittedly the iext tree doesn't track usage information of any kind, so how would the fuse server know?
The static limit is merely the kernel's means to establish a hard limit on the memory consumption of the iext tree, since it can't trust userspace completely.
Quoted 4 lines
It also allows them to prepopulate the cache upfront (eg when servicing a file open request, if the file is below a certain size or if the server knows what'll be hot, it could put those extents into the map from the get-go).
Hrm. I haven't tried issuing
FUSE_NOTIFY_IOMAP_UPSERTduring an open call, but I suppose it's possible.Quoted 3 lines
in my opinion, the fuse-iomap layer should try to be as simple/minimal and as generic as possible. I haven't read through
iomap_cache.cyet but the header comment suggests it's adapted from the xfs extent treeRudely copied, not adapted ;)
I actually wonder if I should make a horrible macro to generate the fuse_iext_* structures and functions, and then
xfs_iext_tree.candfuse_iomap_cache.ccan "share" that hairba^Wcode.Quoted 3 lines
cache. As I understand it, different filesystem implementations have different caching architectures that are better suited for their use cases
Err. The way this evolved is ... way too long to go into in this email. Here's a truncated version; I can tell you the full story next week.
Most filesystems store their file mapping data on disk in whatever format the designers specified. When the pagecache asks them to read or write the cache, they attach buffer heads to the folio, fill out the buffer heads with the minimum mapping information needed to map the folios to disk addresses. bios are constructed for each folio based on what's in the bufferhead.
This was fine for filesystems that map each block individually, such as FFS/ext2/ext3/fat...
Quoted 2 lines
(I'm guessing that's the case, otherwise there would just be one general cache inside iomap all the filesystems would use?). It seems a
...but newer filesystems such as xfs/ext4/btrfs map a bunch of blocks at a time. Each of them invented their own private incore mapping structures to mirror the ondisk structure. xfs kept using the old bufferheads into the early 2010s, ext4 is still using them, and btrfs went its own way from the start.
Eventually XFS grew its own internal extent-to-bio mapping code that flipped the model -- rather than get a pagecache folio, map the folio to blocks, and issue IOs based on the blocks, it would get the file mapping, grab folios for the whole mapping, and issue bios for the batch of folios. That's more efficient, but at this point we have a legacy codebase problem for everything else in
fs/.
Quoted 6 lines
In 2019, hch and I decided to export the extent-to-bio mapping code from xfs so that new filesystems could start with something cleaner than bufferheads. In the past 7 years, nobody's added a new filesystem with complex mapping requirements; they've only ported existing filesystems to it, without further refactoring of their incore data structures. That's why there's no generic iomap cache.
Oh I see, so it actually *is* a generic cache? Just that the other filesystems haven't ported over to it yet? That changes my opinion a lot on this then. If it's a generic cache that pretty much any modern filesystem should use, then it seems reasonable to me to have it on the fuse iomap kernel side. Though in that case, it seems a lot cleaner imo if the cache could be ported to the iomap layer as a generic cache (which seems like it'd be useful anyways for other filesystems to use if/when they port over to it, if I'm understanding what you're saying correctly), and then fuse just call into that api.
Quoted 7 lines
Quoted 3 lines
lot better to me to just let the userspace server define that themselves. And selfishly from the fuse perspective, would be less
Well if I turned the iext code into a template then fuse would only need enough glue code to declare a template class and use it. The glue part is only ... 230LOC.
Nice, I think this would be a lot nicer / less of a headache in the future for fuse to maintain.
Quoted 112 lines
Quoted 3 lines
code we would have to maintain. And I guess too if some servers don't need caching (like famfs?), they could avoid that overhead.
Hrm. Right now the struct
fuse_iomap_cacheis embedded in structfuse_inode, but that could be turned into a dynamic allocation.Quoted 33 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 13 lines
Quoted 4 lines
At one point I suggested to the famfs maintainer that it might be easier/better to implement the interleaved mapping lookups as bpf programs instead of being stuck with a fixed format in the fuse userspace abi, but I don't know if he ever implemented that.
This seems like a good use case for it too
Quoted 7 lines
Quoted 6 lines
Is this your
assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.Quoted 8 lines
Quoted 3 lines
For disk-based filesystems I think it would be difficult to model a bpf program to do mappings, since they can basically point anywhere and be of any size.
Hmm I'm not familiar enough with disk-based filesystems to know what the "point anywhere and be of any size" means. For the mapping stuff, doesn't it just point to a block number? Or are you saying the problem would be there's too many mappings since a mapping could be any size?
The second -- mappings can be any size, and unprivileged userspace can control the mappings.
Quoted 7 lines
Quoted 2 lines
If I'm understanding what you're saying here, this is the same discussion as the one above about the u32 bound, correct?
A different thing -- file data mappings are irregularly sized, can contain sparse holes, etc. Userspace controls the size and offset of each mapping record (thanks to magic things like fallocate) so it'd be very difficult to create a bpf program to generate mappings on the fly.
Quoted 4 lines
Would the bpf prog have to generate mappings on the fly though? If the userspace does things like fallocate, those operations would still go through to the server as a regular request (eg
FUSE_FALLOCATE) and on the server side, it'd add that to the map dynamically from userspace.That depends on the fuse server design. For simple things like famfs where the layout is bog simple and there's no fancy features like delayed allocation or unwritten extents, then you could probably get away a BPF program to generate the entire mapping set. I suspect an object-store type filesystem (aka write a file once, close it, snapshot it, and never change it again) might be good at landing all the file data in relatively few extent mappings, and it could actually compile a custom bpf program for that file and push it to the kernel.
Quoted 6 lines
Quoted 2 lines
Also you could have 2^33 mappings records for a file, so I think you can't even write a bpf program that large.
I think this depends on what map structure gets used. If there is native support added for b+ tree like data structures, I don't see why it wouldn't be able to.
<nod>
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
I was thinking the issue would be more that there might be other logic inside ->iomap_begin()/->iomap_end() besides the mapping stuff that would need to be done that would be too out-of-scope for bpf. But I think I need to read through the fuse4fs stuff to understand more what it's doing in those functions.
Looking at fuse4fs logic cursorily, it seems doable? What I like about offloading this to bpf too is it would also then allow John's famfs to just go through your iomap plumbing as a use case of it instead of being an entirely separate thing. Though maybe there's some other reason for that that you guys have discussed prior. In any case, I'll ask this on John's main famfs patchset. It kind of seems to me that you guys are pretty much doing the exact same thing conceptually.
Quoted 7 lines
Quoted 3 lines
Yes, though John's famfs has the nice property that memory controller interleaving is mathematically regular and likely makes for a compact bpf program.
I tried out integrating the bpf hooks into fuse for
iomap_begin()just to see if it was realistic and it seems relatively straightforward so far (though maybe the devil is in the details...). I used theOk, now *that's* interesting! I guess I had better push the latest fuse-iomap code ... but I cannot share a link, because I cannot get through the @!#%%!!! kernel.org anubis bullcrap.
So I generated a pull request and I *think* this munged URL will work
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-service-container_2026-01-29Or I guess you could just git-pull this:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git tags/fuse-service-container_2026-01-29Quoted 5 lines
drivers/hid/bpf/hid_bpf_struct_ops.cprogram as a model for how to set up the fuse bpf struct ops on the kernel side. calling it fromfile_iomap.clooks something likestatic int
fuse_iomap_begin(...)Quoted 11 lines
{ ... struct fuse_bpf_ops *bpf_ops = fuse_get_bpf_ops(); ... err = -EOPNOTSUPP; if (bpf_ops && bpf_ops->iomap_begin) err = bpf_ops->iomap_begin(inode, pos, len, flags, &outarg); if (err) err = fuse_simple_request(fm, &args); ... }
Quoted 1 line
I'm curious what the rest of the bpf integration code looks like.
This is the code I had yesterday (I didn't know how to run the fuse4fs stuff, so I used passthrough_hp as the server and had the trigger go through statfs):
fs/fuse/Makefile +1 -0
@@ -13,6 +13,7 @@ obj-$(CONFIG_VIRTIO_FS) += virtiofs.o fuse-y := trace.o # put trace.o first so we see ftrace errors sooner fuse-y += dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o ioctl.o fuse-y += iomode.o +fuse-y += fuse_bpf.o fuse-$(CONFIG_FUSE_DAX) += dax.o fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o backing.o fuse-$(CONFIG_SYSCTL) += sysctl.o
fs/fuse/fuse_bpf.c new file +123 -0
123 lines not on this page · raw message
fs/fuse/fuse_bpf.h new file +29 -0
@@ -0,0 +1,29 @@ +#ifndef _FS_FUSE_BPF_H +#define _FS_FUSE_BPF_H + +#include "fuse_i.h" +#include <linux/iomap.h> + +/* copied from darrick's iomap patchset */ +struct fuse_iomap_io { +uint64_t offset;/* file offset of mapping, bytes */ +uint64_t length;/* length of mapping, bytes */ +uint64_t addr;/* disk offset of mapping, bytes */ +uint16_t type;/* FUSE_IOMAP_TYPE_* */ +uint16_t flags;/* FUSE_IOMAP_F_* */ +uint32_t dev;/* device cookie */ +}; + +struct fuse_bpf_ops { +int (*iomap_begin)(struct inode *inode,loff_t pos, +loff_t length,unsigned int flags, +struct fuse_iomap_io *out_io__nullable); + +/* Required for bpf struct_ops */ +char name[16 ]; +}; + +struct fuse_bpf_ops *fuse_get_bpf_ops (void ); +int fuse_bpf_init (void ); + +#endif /* _FS_FUSE_BPF_H */
fs/fuse/inode.c +1 -0
@@ -7,6 +7,7 @@*/ #include "fuse_i.h" +#include "fuse_bpf.h" #include "fuse_dev_i.h" #include "dev_uring_i.h"
@@ -662,6 +663,21 @@ static int fuse_statfs(struct dentry *dentry,
struct kstatfs *buf)
struct fuse_statfs_out outarg;
int err;+ printk("fuse_statfs() called!\n");
+
+ struct fuse_bpf_ops *bpf_ops = fuse_get_bpf_ops();
+ struct fuse_iomap_io out_io = {};
+
+ /* call BPF prog if attached */
+ if (bpf_ops && bpf_ops->iomap_begin) {
+ err = bpf_ops->iomap_begin(d_inode(dentry), 111, 222,
+ 333, &out_io);
+ printk("bpf prog returned: err=%d, out_io->offset=%llu\n",
+ err, out_io.offset);
+ } else {
+ printk("did not find a bpf prog\n");
+ }
+
if (!fuse_allow_current_process(fm->fc)) {
buf->f_type = FUSE_SUPER_MAGIC;
return 0;
@@ -2194,6 +2210,12 @@ static int __init fuse_fs_init(void)
if (!fuse_inode_cachep)
goto out;+ err = fuse_bpf_init();
+ if (err) {
+ printk("fuse_bpf_init() failed %d\n", err);
+ goto out2;
+ }
+
err = register_fuseblk();
if (err)
goto out2;These are the changes for the libfuse side:
https://github.com/joannekoong/libfuse/commit/1a6198f17dd215c93fd82ec020641c079aae1241
To run it, run "make clean; make" from libfuse/example, and then sudo ninja from libfuse/build, and then
sudo ~/libfuse/build/example/passthrough_hp ~/liburing ~/mounts/tmp2
--nopassthrough --foreground
Then from ~/mounts/tmp2, run "stat -f filename" and that will show a few things:
on the kernel side it'll print a statement like "bpf prog returned:
err=0, out_io->offset=999" which shows that the prog can return back a "struct fuse_iomap_io" with all the requisite metadata filled out on the server-side, if you run " sudo cat /sys/kernel/debug/tracing/trace_pipe", that should print out "bpf_trace_printk: fuse iomap_begin: inode=ffff8a75cbe63800 pos=111 len=222 flags=333" which shows the prog can take in whatever pos/len/flags values you pass it from the fuse kernel
Quoted 10 lines
Quoted 5 lines
and I was able to verify that
iomap_begin()is able to return back populated outarg fields from the bpf prog. If we were to actually implement it i'm sure it'd be more complicated (eg we'd need to make thefuse_bpf_opsregistered per-connection, etc) but on the whole itWhat is a
fuse_bpf_ops? I'm assuming that's the attachment point for a bpf program that the fuse server would compile? In which case, yes, I think that ought to be per-connection.
Quoted 5 lines
So the bpf program can examine the struct inode, and the pos/len/opflags field; and from that information it has to write the appropriate fields in &outarg? That's new, I didn't think bpf was allowed to write to kernel memory. But it's been a few years since I last touched the bpf internals.
It's been a few years since I looked at bpf as well but yes fuse_bpf_ops is basically the kernel-side struct_ops interface for getting fuse to trigger the attached bpf program's callback implementations. When the bpf program is loaded in, its callback functions get swapped in and fuse_bpf_ops's function pointers now point to the bpf's callback functions, so when you invoke fuse_bpf_ops's callbacks, it calls into the bpf prog's callback.
Quoted 10 lines
Some bpf programs might just know how to fill out outarg on their own (e.g. famfs memory interleaving) but other bpf programs might perform a range query on some imaginary bpf-interval-tree wherein you can do a fast lookup based on (inumber, pos, len)?
I guess that's an interesting question -- would each fuse connection have one big bpf-interval-tree? Or would you shard things by inode to reduce contention? And if you sharded like that, then would you need a
fuse_bpf_opsper inode?
Hmm the cache's tree is per-inode as I understand it, so probably each inode would have its own tree / map?
Quoted 9 lines
(I'm imagining that the
fuse_bpf_opsmight be where you'd stash the root of the bpf data structure, but I know nothing of bpf internals ;))Rolling on: how easy is it for a userspace program to compile and upload bpf programs into the kernel? I've played around with bcc enough to write some fairly stupid latency tracing tools for XFS, but at the end of the day it still python scripts feeding a string full of maybe-C into whatever the BPF does under the hood.
I found it pretty easy with the libbpf library which will generate the skeletons and provide the api helpers to load it in and other stuff (the libfuse link I pasted above for the userspace side has the code for compiling it and loading it).
Quoted 5 lines
I /think/ it calls clang on the provided text, links that against the current kernel's header files, and pushes the compiled bpf binary into the kernel, right? So fuse4fs would have to learn how to do that; and now fuse4fs has a runtime dependency on libllvm.
I think the libbpf library takes care of a lot of that for you. I think fuse4fs would just need to do the same thing as in that libfuse link above
Quoted 8 lines
And while I'm on the topic of fuse-bpf uapi: It's ok for us to expose primitive-typed variables (pos/len/opflags) and existing fuse uapi directly to a bpf program, but I don't think we should expose struct inode/fuse_inode. Maybe just
fuse_inode::nodeid? If we're careful not to allow #include'ing structured types in the fuse bpf code, then perhaps the bpf programs could be compiled at the same time as the fuse server.
I agree, I think if we do decide to go further with this approach, we'll need to define exactly what the interfaces should be that would be safe to expose.
Quoted 17 lines
Quoted 5 lines
seems doable. My worry is that if we land the iomap cache patchset now then we can't remove it in the future without breaking backwards compatibility for being a performance regression (though maybe we can since the fuse-iomap stuff is experimental?), so imo it'd be great if
I don't think it's a huge problem to remove functionality while the EXPERIMENTAL warnings are in place. We'd forever lose the command codes for
FUSE_NOTIFY_IOMAP_UPSERTandFUSE_NOTIFY_IOMAP_INVAL, but we've only used 12 out ofINT_MAXso that's not likely to be a concern.Quoted 5 lines
we figured out what direction we want to go before landing the cache stuff. And I think we need to have this conversation too on the main famfs patchset (eg whether it should go through your general iomap plumbing with bpf helpers vs. being a separate implementation) since once that lands, it'd be irrevocable.
Quoted 4 lines
I've of two minds on that -- John got here first, so I don't want to delay his patchset whilst I slowly work on this thing. OTOH from an architecture standpoint we probably ought to push for three ways for a fuse server to upload mappings:
I think if John/Miklos wanted to go in this direction, all that would be needed from your series is the first one or two patches that define the basic fuse_iomap_io / fuse_iomap_begin / fuse_iomap_end structs and init config plumbing.
Thanks,
Joanne
Quoted 107 lines
1. Upserting mappings with arbitrary offset and size into a cache
2. Self contained bpf program that can generate any mapping
3. Sprawling bpf program that can read any other artifacts that another bpf program might have set up for itBut yeah, let's involve John.
--D
Quoted 97 lines
Thanks,
JoanneQuoted 93 lines
--D
Quoted 90 lines
Thanks,
JoanneQuoted 86 lines
<nod>
--D
Quoted 81 lines
Thanks,
JoanneQuoted 77 lines
OTOH it would be enormously hilarious to me if one could load a file mapping predictive model into the kernel as a bpf program and use that as a first tier before checking the in-memory btree mapping cache from patchset 7. Quite a few years ago now there was a FAST paper establishing that even a stupid linear regression model could in theory beat a disk btree lookup.
--D
Quoted 67 lines
Thanks,
JoanneQuoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
Re: [PATCHSET v6 4/8] fuse: allow servers to use iomap for better file IO performance
Quoted 67 lines · Joanne Koong
Quoted 66 lines · Darrick J. Wong
On Wed, Jan 28, 2026 at 05:12:54PM -0800, Joanne Koong wrote:
<snip>
Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 7 lines
Quoted 2 lines
Hrmm. Now that /is/ an interesting proposal. Does BPF have a data structure that supports interval mappings? I think the existing bpf map
Not yet but I don't see why a b+ tree like data strucutre couldn't be added. Maybe one workaround in the meantime that could work is using a sorted array map and doing binary search on that, until interval mappings can be natively supported?
I guess, though I already had a C structure to borrow from xfs ;)
Quoted 6 lines
Quoted 3 lines
only does key -> value. Also, is there an upper limit on the size of a map? You could have hundreds of millions of maps for a very fragmented regular file.
If I'm remembering correctly, there's an upper limit on the number of map entries, which is bounded by u32
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
That's problematic, since files can have 64-bit logical block numbers.
The key size supports 64-bits. The u32 bound would be the limit on the number of extents for the file.
Oh, ok. If one treats the incore map as a cache and evicts things when they get too old, then that would be fine. I misread that as an upper limit on the *range* of the map entry keys. :/
Quoted 6 lines
I think for more complicated servers, the bpf prog handling for
iomap_begin()would essentially just serve as a cache where if it's not found in the cache, then it sends off theFUSE_IOMAP_BEGINrequest to the server. For servers that don't need as much complicated logic (eg famfs), theiomap_begin()logic would just be executed within the bpf prog itself.Yes, I like the
fuse_iomap_beginlogic flow of:1. Try to use a mapping in the iext tree
2. Call a BPF program to try to generate a mapping
3. Issue a fuse command to userspacewherein #2 and #3 can signal that #1 should be retried. (This is already provided by
FUSE_IOMAP_TYPE_RETRY_CACHE, FWIW)That said, BPF doesn't expose an interval btree data structure. I think it would be better to add the iext mapping cache and make it so that bpf programs could call fuse_iomap_cache_{upsert,remove,lookup}. You could use the interval tree too, but the iext tree has the advantage of higher fanout factor.
Quoted 12 lines
Quoted 8 lines
As it stands, I need to figure out a way to trim the iomap btree when memory gets tight. Right now it'll drop the cache whenever someone closes the file, but that won't help for long-life processes that open a heavily fragmented file and never close it.
A coding-intensive way to do that would be to register a shrinker and deal with that, but ugh. A really stupid way would be to drop the whole cache once you get beyond (say) 64k of memory usage (~2000 mappings).
This kind of seems like another point in favor of giving userspace control of the caching layer. They could then implement whatever eviction policies they want.
Quoted 6 lines
Quoted 3 lines
Note that userspace already can control the cached iomappings --
FUSE_NOTIFY_IOMAP_UPSERTpushes a mapping into the iext tree, andFUSE_NOTIFY_IOMAP_INVALremoves them. The fuse server can decide toThis incurs round-trip context-switch costs though, which the bpf prog approach doesn't incur.
I realize that, but I think we're simply headed towards a fast path wherein you can upload a bpf program; and a slow path for big complex filesystems like ext4. ext4 always gonna be slow. :)
Quoted 7 lines
Quoted 7 lines
evict whenever it pleases, though admittedly the iext tree doesn't track usage information of any kind, so how would the fuse server know?
The static limit is merely the kernel's means to establish a hard limit on the memory consumption of the iext tree, since it can't trust userspace completely.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
It also allows them to prepopulate the cache upfront (eg when servicing a file open request, if the file is below a certain size or if the server knows what'll be hot, it could put those extents into the map from the get-go).
Hrm. I haven't tried issuing
FUSE_NOTIFY_IOMAP_UPSERTduring an open call, but I suppose it's possible.
(It's quite possible; running through QA now)
Quoted 43 lines
Quoted 43 lines
Quoted 3 lines
in my opinion, the fuse-iomap layer should try to be as simple/minimal and as generic as possible. I haven't read through
iomap_cache.cyet but the header comment suggests it's adapted from the xfs extent treeRudely copied, not adapted ;)
I actually wonder if I should make a horrible macro to generate the fuse_iext_* structures and functions, and then
xfs_iext_tree.candfuse_iomap_cache.ccan "share" that hairba^Wcode.Quoted 3 lines
cache. As I understand it, different filesystem implementations have different caching architectures that are better suited for their use cases
Err. The way this evolved is ... way too long to go into in this email. Here's a truncated version; I can tell you the full story next week.
Most filesystems store their file mapping data on disk in whatever format the designers specified. When the pagecache asks them to read or write the cache, they attach buffer heads to the folio, fill out the buffer heads with the minimum mapping information needed to map the folios to disk addresses. bios are constructed for each folio based on what's in the bufferhead.
This was fine for filesystems that map each block individually, such as FFS/ext2/ext3/fat...
Quoted 2 lines
(I'm guessing that's the case, otherwise there would just be one general cache inside iomap all the filesystems would use?). It seems a
...but newer filesystems such as xfs/ext4/btrfs map a bunch of blocks at a time. Each of them invented their own private incore mapping structures to mirror the ondisk structure. xfs kept using the old bufferheads into the early 2010s, ext4 is still using them, and btrfs went its own way from the start.
Eventually XFS grew its own internal extent-to-bio mapping code that flipped the model -- rather than get a pagecache folio, map the folio to blocks, and issue IOs based on the blocks, it would get the file mapping, grab folios for the whole mapping, and issue bios for the batch of folios. That's more efficient, but at this point we have a legacy codebase problem for everything else in
fs/.
Quoted 8 lines
Quoted 6 lines
In 2019, hch and I decided to export the extent-to-bio mapping code from xfs so that new filesystems could start with something cleaner than bufferheads. In the past 7 years, nobody's added a new filesystem with complex mapping requirements; they've only ported existing filesystems to it, without further refactoring of their incore data structures. That's why there's no generic iomap cache.
Oh I see, so it actually *is* a generic cache? Just that the other
No, quite the opposite. fs/iomap/ handles individual mapping records, but does not itself implement a cache. A filesystem /could/ decide that it wants to translate its ondisk mappings directly into a struct iomap and then push that struct iomap object into a cache, but it would have to implement the incore cache itself.
"struct iomap" is merely a generic interface; prior to the hoist, the IO mapping code in xfs directly handled struct xfs_bmbt_irec objects. Now xfs translates xfs_bmbt_irec objects from its own cache into a struct iomap and returns that from ->iomap_begin.
That said it wouldn't be terrible to provide a generic cache implementation. But the hard part is that caching struct iomap objects would result in more memory use for XFS because xfs_bmbt_irec is bitpacked.
Quoted 8 lines
filesystems haven't ported over to it yet? That changes my opinion a lot on this then. If it's a generic cache that pretty much any modern filesystem should use, then it seems reasonable to me to have it on the fuse iomap kernel side. Though in that case, it seems a lot cleaner imo if the cache could be ported to the iomap layer as a generic cache (which seems like it'd be useful anyways for other filesystems to use if/when they port over to it, if I'm understanding what you're saying correctly), and then fuse just call into that api.
<nod> That sounds like a good long term goal. For now let's get it working inside fuse and then we can see if any other filesystems are interested.
Also unclear: do filesystems want to hang more data off the incore mapping data than the dataset that iomap itself needs? That would hamper attempts to make a generic iomap cache.
Quoted 123 lines
Quoted 7 lines
Quoted 3 lines
lot better to me to just let the userspace server define that themselves. And selfishly from the fuse perspective, would be less
Well if I turned the iext code into a template then fuse would only need enough glue code to declare a template class and use it. The glue part is only ... 230LOC.
Nice, I think this would be a lot nicer / less of a headache in the future for fuse to maintain.
Quoted 112 lines
Quoted 3 lines
code we would have to maintain. And I guess too if some servers don't need caching (like famfs?), they could avoid that overhead.
Hrm. Right now the struct
fuse_iomap_cacheis embedded in structfuse_inode, but that could be turned into a dynamic allocation.Quoted 33 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 13 lines
Quoted 4 lines
At one point I suggested to the famfs maintainer that it might be easier/better to implement the interleaved mapping lookups as bpf programs instead of being stuck with a fixed format in the fuse userspace abi, but I don't know if he ever implemented that.
This seems like a good use case for it too
Quoted 7 lines
Quoted 6 lines
Is this your
assessment of it as well or do you think the server-side logic for iomap_begin()/iomap_end() is too complicated to make this realistic? Asking because I'm curious whether this direction makes sense, not because I think it would be a blocker for your series.Quoted 8 lines
Quoted 3 lines
For disk-based filesystems I think it would be difficult to model a bpf program to do mappings, since they can basically point anywhere and be of any size.
Hmm I'm not familiar enough with disk-based filesystems to know what the "point anywhere and be of any size" means. For the mapping stuff, doesn't it just point to a block number? Or are you saying the problem would be there's too many mappings since a mapping could be any size?
The second -- mappings can be any size, and unprivileged userspace can control the mappings.
Quoted 7 lines
Quoted 2 lines
If I'm understanding what you're saying here, this is the same discussion as the one above about the u32 bound, correct?
A different thing -- file data mappings are irregularly sized, can contain sparse holes, etc. Userspace controls the size and offset of each mapping record (thanks to magic things like fallocate) so it'd be very difficult to create a bpf program to generate mappings on the fly.
Quoted 4 lines
Would the bpf prog have to generate mappings on the fly though? If the userspace does things like fallocate, those operations would still go through to the server as a regular request (eg
FUSE_FALLOCATE) and on the server side, it'd add that to the map dynamically from userspace.That depends on the fuse server design. For simple things like famfs where the layout is bog simple and there's no fancy features like delayed allocation or unwritten extents, then you could probably get away a BPF program to generate the entire mapping set. I suspect an object-store type filesystem (aka write a file once, close it, snapshot it, and never change it again) might be good at landing all the file data in relatively few extent mappings, and it could actually compile a custom bpf program for that file and push it to the kernel.
Quoted 6 lines
Quoted 2 lines
Also you could have 2^33 mappings records for a file, so I think you can't even write a bpf program that large.
I think this depends on what map structure gets used. If there is native support added for b+ tree like data structures, I don't see why it wouldn't be able to.
<nod>
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
I was thinking the issue would be more that there might be other logic inside ->iomap_begin()/->iomap_end() besides the mapping stuff that would need to be done that would be too out-of-scope for bpf. But I think I need to read through the fuse4fs stuff to understand more what it's doing in those functions.
Looking at fuse4fs logic cursorily, it seems doable? What I like about offloading this to bpf too is it would also then allow John's famfs to just go through your iomap plumbing as a use case of it instead of being an entirely separate thing. Though maybe there's some other reason for that that you guys have discussed prior. In any case, I'll ask this on John's main famfs patchset. It kind of seems to me that you guys are pretty much doing the exact same thing conceptually.
Quoted 7 lines
Quoted 3 lines
Yes, though John's famfs has the nice property that memory controller interleaving is mathematically regular and likely makes for a compact bpf program.
I tried out integrating the bpf hooks into fuse for
iomap_begin()just to see if it was realistic and it seems relatively straightforward so far (though maybe the devil is in the details...). I used theOk, now *that's* interesting! I guess I had better push the latest fuse-iomap code ... but I cannot share a link, because I cannot get through the @!#%%!!! kernel.org anubis bullcrap.
So I generated a pull request and I *think* this munged URL will work
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-service-container_2026-01-29Or I guess you could just git-pull this:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git tags/fuse-service-container_2026-01-29Quoted 16 lines
drivers/hid/bpf/hid_bpf_struct_ops.cprogram as a model for how to set up the fuse bpf struct ops on the kernel side. calling it fromfile_iomap.clooks something likestatic int
fuse_iomap_begin(...){ ... struct fuse_bpf_ops *bpf_ops = fuse_get_bpf_ops(); ... err = -EOPNOTSUPP; if (bpf_ops && bpf_ops->iomap_begin) err = bpf_ops->iomap_begin(inode, pos, len, flags, &outarg); if (err) err = fuse_simple_request(fm, &args); ... }
Quoted 5 lines
Quoted 1 line
I'm curious what the rest of the bpf integration code looks like.
This is the code I had yesterday (I didn't know how to run the fuse4fs stuff, so I used
passthrough_hpas the server and had the trigger go through statfs):
Yeah, it's nasty ... you have to go build a patched libfuse3, then grab the modified e2fsprogs, modify your ./configure commandline to point itself at the built libfuse3, and then it'll build fuse4fs.
Quoted 227 lines
fs/fuse/Makefile +1
-0@@ -13,6 +13,7 @@ obj-$(CONFIG_VIRTIO_FS) += virtiofs.o fuse-y := trace.o # put trace.o first so we see ftrace errors sooner fuse-y += dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o ioctl.o fuse-y += iomode.o +fuse-y += fuse_bpf.o fuse-$(CONFIG_FUSE_DAX) += dax.o fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o backing.o fuse-$(CONFIG_SYSCTL) += sysctl.ofs/fuse/fuse_bpf.c new file +123
-0@@ -0,0 +1,123 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> + +#include "fuse_i.h" +#include "fuse_dev_i.h" +#include "fuse_bpf.h" + +static struct btf *fuse_bpf_ops_btf; +static struct fuse_bpf_ops *active_bpf_ops; + +static int fuse_bpf_ops_init (struct btf *btf) +{ + fuse_bpf_ops_btf = btf; +return 0 ; +} + +static bool fuse_bpf_ops_is_valid_access (int off,int size, +enum bpf_access_type type, +const struct bpf_prog *prog, +struct bpf_insn_access_aux *info) +{ +return bpf_tracing_btf_ctx_access (off, size, type, prog, info); +} + +static int fuse_bpf_ops_check_member (const struct btf_type *t, +const struct btf_member *member, +const struct bpf_prog *prog) +{ +return 0 ; +} + +static int fuse_bpf_ops_btf_struct_access (struct bpf_verifier_log *log, +const struct bpf_reg_state *reg, +int off,int size) +{ +return 0 ; +} + +static const struct bpf_verifier_ops fuse_bpf_verifier_ops = { + .get_func_proto = bpf_base_func_proto, + .is_valid_access = fuse_bpf_ops_is_valid_access, + .btf_struct_access = fuse_bpf_ops_btf_struct_access, +}; + +static int fuse_bpf_ops_init_member (const struct btf_type *t, +const struct btf_member *member, +void *kdata,const void *udata) +{ +const struct fuse_bpf_ops *u_ops = udata; +struct fuse_bpf_ops *ops = kdata; +u32 moff; + + moff =__btf_member_bit_offset (t, member) /8 ; +switch (moff) { +case offsetof(struct fuse_bpf_ops , name): +if (bpf_obj_name_cpy (ops->name, u_ops->name, +sizeof (ops->name)) <=0 ) +return -EINVAL ; +return 1 ;/* Handled */ + } + +/* Not handled, use default */ +return 0 ; +} + +static int fuse_bpf_reg (void *kdata,struct bpf_link *link) +{ +struct fuse_bpf_ops *ops = kdata; + + active_bpf_ops = ops; + +printk ("fuse_bpf: registered ops '%s'\n" , ops->name); + +return 0 ; +} + +static void fuse_bpf_unreg (void *kdata,struct bpf_link *link) +{ +struct fuse_bpf_ops *ops = kdata; + +if (active_bpf_ops == ops) + active_bpf_ops =NULL ; + +printk ("fuse_bpf: unregistered ops '%s'\n" , ops->name); +} + +static int __iomap_begin (struct inode *inode,loff_t pos, +loff_t length,unsigned int flags, +struct fuse_iomap_io *out_io) +{ +printk ("stub __iomap_begin(). should never get called\n" ); +return 0 ; +} + +static struct fuse_bpf_ops __fuse_bpf_ops = { + .iomap_begin = __iomap_begin, +}; + +static struct bpf_struct_ops fuse_bpf_struct_ops = { + .verifier_ops = &fuse_bpf_verifier_ops, + .init = fuse_bpf_ops_init, + .check_member = fuse_bpf_ops_check_member, + .init_member = fuse_bpf_ops_init_member, + .reg = fuse_bpf_reg, + .unreg = fuse_bpf_unreg, + .name ="fuse_bpf_ops" , + .cfi_stubs = &__fuse_bpf_ops, + .owner =THIS_MODULE , +}; + +struct fuse_bpf_ops *fuse_get_bpf_ops (void ) +{ +return active_bpf_ops; +} + +int fuse_bpf_init (void ) +{ +return register_bpf_struct_ops (&fuse_bpf_struct_ops, fuse_bpf_ops); +} + +BTF_ID_LIST_GLOBAL_SINGLE(btf_fuse_bpf_ops_id, struct, fuse_bpf_ops) +BTF_ID_LIST_GLOBAL_SINGLE (btf_fuse_iomap_io_id, struct, fuse_iomap_io)fs/fuse/fuse_bpf.h new file +29
-0@@ -0,0 +1,29 @@ +#ifndef _FS_FUSE_BPF_H +#define _FS_FUSE_BPF_H + +#include "fuse_i.h" +#include <linux/iomap.h> + +/* copied from darrick's iomap patchset */ +struct fuse_iomap_io { +uint64_t offset;/* file offset of mapping, bytes */ +uint64_t length;/* length of mapping, bytes */ +uint64_t addr;/* disk offset of mapping, bytes */ +uint16_t type;/* FUSE_IOMAP_TYPE_* */ +uint16_t flags;/* FUSE_IOMAP_F_* */ +uint32_t dev;/* device cookie */ +}; + +struct fuse_bpf_ops { +int (*iomap_begin)(struct inode *inode,loff_t pos, +loff_t length,unsigned int flags, +struct fuse_iomap_io *out_io__nullable); + +/* Required for bpf struct_ops */ +char name[16 ]; +}; + +struct fuse_bpf_ops *fuse_get_bpf_ops (void ); +int fuse_bpf_init (void ); + +#endif /* _FS_FUSE_BPF_H */ fs/fuse/inode.c +16
-0@@ -7,6 +7,7 @@*/ #include "fuse_i.h" +#include "fuse_bpf.h" #include "fuse_dev_i.h" #include "dev_uring_i.h" @@ -662,6 +663,21 @@ static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)struct fuse_statfs_out outarg;int err; +printk ("fuse_statfs() called!\n" ); + +struct fuse_bpf_ops *bpf_ops =fuse_get_bpf_ops (); +struct fuse_iomap_io out_io = {}; + +/* call BPF prog if attached */ +if (bpf_ops && bpf_ops->iomap_begin) { + err = bpf_ops->iomap_begin (d_inode (dentry),111 ,222 , +333 , &out_io); +printk ("bpf prog returned: err=%d, out_io->offset=%llu\n" , + err, out_io.offset); + }else { +printk ("did not find a bpf prog\n" ); + } +if (!fuse_allow_current_process (fm->fc)) { buf->f_type =FUSE_SUPER_MAGIC ;return 0;fs/fuse/inode.c +6
-0@@ -2194,6 +2210,12 @@ static int __init fuse_fs_init(void)if (!fuse_inode_cachep) goto out; + err =fuse_bpf_init (); +if (err) { +printk ("fuse_bpf_init() failed %d\n" , err); + goto out2; + } + err =register_fuseblk ();if (err) goto out2;
Quoted 2 lines
These are the changes for the libfuse side:
https://github.com/joannekoong/libfuse/commit/1a6198f17dd215c93fd82ec020641c079aae1241
/me sees "SEC("struct_ops/iomap_begin")"
ohhhh my.
Well at least now I know that C programs can do bpf stuff via libfuse.
Quoted 5 lines
To run it, run "make clean; make" from libfuse/example, and then sudo ninja from libfuse/build, and then
sudo ~/libfuse/build/example/passthrough_hp ~/liburing ~/mounts/tmp2
--nopassthrough --foreground
Quoted 9 lines
Then from ~/mounts/tmp2, run "stat -f filename" and that will show a few things:
on the kernel side it'll print a statement like "bpf prog returned:
err=0, out_io->offset=999" which shows that the prog can return back a "structfuse_iomap_io" with all the requisite metadata filled out on the server-side, if you run " sudo cat/sys/kernel/debug/tracing/trace_pipe", that should print out "bpf_trace_printk: fuseiomap_begin: inode=ffff8a75cbe63800 pos=111 len=222 flags=333" which shows the prog can take in whatever pos/len/flags values you pass it from the fuse kernel
Hehhehe. Will give that a shot.
--D
Quoted 215 lines
Quoted 11 lines
Quoted 6 lines
and I was able to verify that
iomap_begin()is able to return back populated outarg fields from the bpf prog. If we were to actually implement it i'm sure it'd be more complicated (eg we'd need to make thefuse_bpf_opsregistered per-connection, etc) but on the whole itWhat is a
fuse_bpf_ops? I'm assuming that's the attachment point for a bpf program that the fuse server would compile? In which case, yes, I think that ought to be per-connection.Quoted 5 lines
So the bpf program can examine the struct inode, and the pos/len/opflags field; and from that information it has to write the appropriate fields in &outarg? That's new, I didn't think bpf was allowed to write to kernel memory. But it's been a few years since I last touched the bpf internals.
It's been a few years since I looked at bpf as well but yes
fuse_bpf_opsis basically the kernel-sidestruct_opsinterface for getting fuse to trigger the attached bpf program's callback implementations. When the bpf program is loaded in, its callback functions get swapped in and fuse_bpf_ops's function pointers now point to the bpf's callback functions, so when you invoke fuse_bpf_ops's callbacks, it calls into the bpf prog's callback.Quoted 10 lines
Some bpf programs might just know how to fill out outarg on their own (e.g. famfs memory interleaving) but other bpf programs might perform a range query on some imaginary bpf-interval-tree wherein you can do a fast lookup based on (inumber, pos, len)?
I guess that's an interesting question -- would each fuse connection have one big bpf-interval-tree? Or would you shard things by inode to reduce contention? And if you sharded like that, then would you need a
fuse_bpf_opsper inode?Hmm the cache's tree is per-inode as I understand it, so probably each inode would have its own tree / map?
Quoted 9 lines
(I'm imagining that the
fuse_bpf_opsmight be where you'd stash the root of the bpf data structure, but I know nothing of bpf internals ;))Rolling on: how easy is it for a userspace program to compile and upload bpf programs into the kernel? I've played around with bcc enough to write some fairly stupid latency tracing tools for XFS, but at the end of the day it still python scripts feeding a string full of maybe-C into whatever the BPF does under the hood.
I found it pretty easy with the libbpf library which will generate the skeletons and provide the api helpers to load it in and other stuff (the libfuse link I pasted above for the userspace side has the code for compiling it and loading it).
Quoted 5 lines
I /think/ it calls clang on the provided text, links that against the current kernel's header files, and pushes the compiled bpf binary into the kernel, right? So fuse4fs would have to learn how to do that; and now fuse4fs has a runtime dependency on libllvm.
I think the libbpf library takes care of a lot of that for you. I think fuse4fs would just need to do the same thing as in that libfuse link above
Quoted 8 lines
And while I'm on the topic of fuse-bpf uapi: It's ok for us to expose primitive-typed variables (pos/len/opflags) and existing fuse uapi directly to a bpf program, but I don't think we should expose struct inode/fuse_inode. Maybe just
fuse_inode::nodeid? If we're careful not to allow #include'ing structured types in the fuse bpf code, then perhaps the bpf programs could be compiled at the same time as the fuse server.I agree, I think if we do decide to go further with this approach, we'll need to define exactly what the interfaces should be that would be safe to expose.
Quoted 17 lines
Quoted 5 lines
seems doable. My worry is that if we land the iomap cache patchset now then we can't remove it in the future without breaking backwards compatibility for being a performance regression (though maybe we can since the fuse-iomap stuff is experimental?), so imo it'd be great if
I don't think it's a huge problem to remove functionality while the EXPERIMENTAL warnings are in place. We'd forever lose the command codes for
FUSE_NOTIFY_IOMAP_UPSERTandFUSE_NOTIFY_IOMAP_INVAL, but we've only used 12 out ofINT_MAXso that's not likely to be a concern.Quoted 5 lines
we figured out what direction we want to go before landing the cache stuff. And I think we need to have this conversation too on the main famfs patchset (eg whether it should go through your general iomap plumbing with bpf helpers vs. being a separate implementation) since once that lands, it'd be irrevocable.
Quoted 4 lines
I've of two minds on that -- John got here first, so I don't want to delay his patchset whilst I slowly work on this thing. OTOH from an architecture standpoint we probably ought to push for three ways for a fuse server to upload mappings:
I think if John/Miklos wanted to go in this direction, all that would be needed from your series is the first one or two patches that define the basic
fuse_iomap_io/fuse_iomap_begin/fuse_iomap_endstructs and init config plumbing.Thanks,
JoanneQuoted 107 lines
1. Upserting mappings with arbitrary offset and size into a cache
2. Self contained bpf program that can generate any mapping
3. Sprawling bpf program that can read any other artifacts that another bpf program might have set up for itBut yeah, let's involve John.
--D
Quoted 97 lines
Thanks,
JoanneQuoted 93 lines
--D
Quoted 90 lines
Thanks,
JoanneQuoted 86 lines
<nod>
--D
Quoted 81 lines
Thanks,
JoanneQuoted 77 lines
OTOH it would be enormously hilarious to me if one could load a file mapping predictive model into the kernel as a bpf program and use that as a first tier before checking the in-memory btree mapping cache from patchset 7. Quite a few years ago now there was a FAST paper establishing that even a stupid linear regression model could in theory beat a disk btree lookup.
--D
Quoted 67 lines
Thanks,
JoanneQuoted 63 lines
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* fuse: implement the basic iomap mechanisms
*fuse_trace: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
*fuse_trace: adapt FUSE_DEV_IOC_BACKING_{OPEN,CLOSE} to add new iomap devices
* fuse: flush events and sendFUSE_SYNCFSandFUSE_DESTROYon unmount
* fuse: create a per-inode flag for toggling iomap
*fuse_trace: create a per-inode flag for toggling iomap
* fuse: isolate the other regular file IO paths from iomap
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
*fuse_trace: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
*fuse_trace: implement direct IO with iomap
* fuse: implement buffered IO with iomap
*fuse_trace: implement buffered IO with iomap
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
*fuse_trace: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: invalidate ranges of block devices being used for iomap
*fuse_trace: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
*fuse_trace: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
*fuse_trace: support atomic writes with iomap
* fuse: disable direct reclaim for any fuse server that uses iomap
* fuse: enable swapfile activation on iomap
* fuse: implement freeze and shutdowns for iomap filesystems--- fs/fuse/fuse_i.h | 161 +++ fs/fuse/fuse_trace.h | 939 +++++++++++++++++++ fs/fuse/iomap_i.h | 52 + include/uapi/linux/fuse.h | 219 ++++ fs/fuse/Kconfig | 48 + fs/fuse/Makefile | 1 fs/fuse/backing.c | 12 fs/fuse/dev.c | 30 + fs/fuse/dir.c | 120 ++ fs/fuse/file.c | 133 ++- fs/fuse/file_iomap.c | 2230 +++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/inode.c | 162 +++ fs/fuse/iomode.c | 2 fs/fuse/trace.c | 2 14 files changed, 4056 insertions(+), 55 deletions(-) create mode 100644 fs/fuse/iomap_i.h create mode 100644 fs/fuse/file_iomap.c
PATCH v6 5/8 fuse: allow servers to specify root node id
In reply to Darrick J. WongHi all,
This series grants fuse servers full control over the entire node id address space by allowing them to specify the nodeid of the root directory. With this new feature, fuse4fs will not have to translate node ids.
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-root-nodeid
---
Commits in this patchset:
* fuse: make the root nodeid dynamic
* fuse_trace: make the root nodeid dynamic
* fuse: allow setting of root nodeid
--- fs/fuse/fuse_i.h | 9 +++++++-- fs/fuse/fuse_trace.h | 6 ++++-- fs/fuse/dir.c | 10 ++++++---- fs/fuse/inode.c | 22 ++++++++++++++++++---- fs/fuse/readdir.c | 10 +++++----- 5 files changed, 40 insertions(+), 17 deletions(-)
PATCH 1/3 fuse: make the root nodeid dynamic
From: Darrick J. Wong <[email protected]>
Change this from a hardcoded constant to a dynamic field so that fuse servers don't need to translate.
--- fs/fuse/fuse_i.h | 7 +++++-- fs/fuse/dir.c | 10 ++++++---- fs/fuse/inode.c | 11 +++++++---- fs/fuse/readdir.c | 10 +++++----- 4 files changed, 23 insertions(+), 15 deletions(-)
fs/fuse/fuse_i.h +5 -2
@@ -690,6 +690,9 @@ struct fuse_conn {struct rcu_head rcu; +/* node id of the root directory */ +u64 root_nodeid; +/** The user id for this mount */ kuid_t user_id;
@@ -1118,9 +1121,9 @@ static inline u64 get_node_id(struct inode *inode)return get_fuse_inode (inode)->nodeid; } -static inline int invalid_nodeid (u64 nodeid) +static inline int invalid_nodeid (const struct fuse_conn *fc,u64 nodeid) { -return !nodeid || nodeid ==FUSE_ROOT_ID ; +return !nodeid || nodeid == fc->root_nodeid; }static inline u64 fuse_get_attr_version (struct fuse_conn *fc)
fs/fuse/dir.c +6 -4
@@ -386,7 +386,7 @@ int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name err = -EIO ;if (fuse_invalid_attr (&outarg->attr)) goto out_put_forget; -if (outarg->nodeid ==FUSE_ROOT_ID && outarg->generation !=0 ) { +if (outarg->nodeid == fm->fc->root_nodeid && outarg->generation !=0 ) {pr_warn_once ("root generation should be zero\n" ); outarg->generation =0 ; }
@@ -436,7 +436,7 @@ static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, goto out_err; err = -EIO ; -if (inode &&get_node_id (inode) ==FUSE_ROOT_ID ) +if (inode &&get_node_id (inode) == fc->root_nodeid) goto out_iput; newent =d_splice_alias (inode, entry);
@@ -687,7 +687,8 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, goto out_free_ff; err = -EIO ; -if (!S_ISREG (outentry.attr.mode) ||invalid_nodeid (outentry.nodeid) || +if (!S_ISREG (outentry.attr.mode) || +invalid_nodeid (fm->fc, outentry.nodeid) ||fuse_invalid_attr (&outentry.attr)) goto out_free_ff;
@@ -831,7 +832,8 @@ static struct dentry *create_new_entry(struct mnt_idmap *idmap, struct fuse_moun goto out_put_forget_req; err = -EIO ; -if (invalid_nodeid (outarg.nodeid) ||fuse_invalid_attr (&outarg.attr)) +if (invalid_nodeid (fm->fc, outarg.nodeid) || +fuse_invalid_attr (&outarg.attr)) goto out_put_forget_req;if ((outarg.attr.mode ^ mode) &S_IFMT )
fs/fuse/inode.c +7 -4
@@ -1018,6 +1018,7 @@ void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm, fc->max_pages_limit = fuse_max_pages_limit; fc->name_max =FUSE_NAME_LOW_MAX ; fc->timeout.req_timeout =0 ; + fc->root_nodeid =FUSE_ROOT_ID ;if (IS_ENABLED (CONFIG_FUSE_BACKING ))fuse_backing_files_init (fc);
@@ -1073,12 +1074,14 @@ EXPORT_SYMBOL_GPL(fuse_conn_get);static struct inode *fuse_get_root_inode (struct super_block *sb,unsigned int mode) {struct fuse_attr attr; +struct fuse_conn *fc =get_fuse_conn_super (sb); +memset (&attr,0 ,sizeof (attr)); attr.mode = mode; - attr.ino =FUSE_ROOT_ID ; + attr.ino = fc->root_nodeid; attr.nlink =1 ; -return fuse_iget (sb,FUSE_ROOT_ID ,0 , &attr,0 ,0 ,0 ); +return fuse_iget (sb, fc->root_nodeid,0 , &attr,0 ,0 ,0 ); }struct fuse_inode_handle {
@@ -1122,7 +1125,7 @@ static struct dentry *fuse_get_dentry(struct super_block *sb, goto out_iput; entry =d_obtain_alias (inode); -if (!IS_ERR (entry) &&get_node_id (inode) !=FUSE_ROOT_ID ) +if (!IS_ERR (entry) &&get_node_id (inode) != fc->root_nodeid)fuse_invalidate_entry_cache (entry);return entry;
@@ -1215,7 +1218,7 @@ static struct dentry *fuse_get_parent(struct dentry *child) } parent =d_obtain_alias (inode); -if (!IS_ERR (parent) &&get_node_id (inode) !=FUSE_ROOT_ID ) +if (!IS_ERR (parent) &&get_node_id (inode) != fc->root_nodeid)fuse_invalidate_entry_cache (parent);return parent;
fs/fuse/readdir.c +5 -5
@@ -185,12 +185,12 @@ static int fuse_direntplus_link(struct file *file,return 0 ; } -if (invalid_nodeid (o->nodeid)) -return -EIO ; -if (fuse_invalid_attr (&o->attr)) -return -EIO ; - fc =get_fuse_conn (dir); +if (invalid_nodeid (fc, o->nodeid)) +return -EIO ; +if (fuse_invalid_attr (&o->attr)) +return -EIO ; + epoch =atomic_read (&fc->epoch); name.hash =full_name_hash (parent, name.name, name.len);
PATCH 2/3 fuse_trace: make the root nodeid dynamic
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Enhance the iomap config tracepoint to report the node id of the root directory.
--- fs/fuse/fuse_trace.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/fuse/fuse_trace.h +4 -2
@@ -991,6 +991,7 @@ TRACE_EVENT(fuse_iomap_config,TP_STRUCT__entry (__field (dev_t ,connection ) +__field (uint64_t, root_nodeid)__field (uint32_t, flags) __field(uint32_t, blocksize)
@@ -1005,6 +1006,7 @@ TRACE_EVENT(fuse_iomap_config,TP_fast_assign ( __entry->connection = fm->fc->dev; + __entry->root_nodeid = fm->fc->root_nodeid; __entry->flags = outarg->flags; __entry->blocksize = outarg->s_blocksize; __entry->max_links = outarg->s_max_links;
@@ -1015,8 +1017,8 @@ TRACE_EVENT(fuse_iomap_config, __entry->uuid_len = outarg->s_uuid_len; ), - TP_printk("connection %u flags (%s) blocksize 0x%x max_links %u time_gran %u time_min %lld time_max %lld maxbytes 0x%llx uuid_len %u", - __entry->connection, + TP_printk("connection %u root_ino 0x%llx flags (%s) blocksize 0x%x max_links %u time_gran %u time_min %lld time_max %lld maxbytes 0x%llx uuid_len %u", + __entry->connection, __entry->root_nodeid, __print_flags(__entry->flags, "|", FUSE_IOMAP_CONFIG_STRINGS), __entry->blocksize, __entry->max_links, __entry->time_gran, __entry->time_min, __entry->time_max, __entry->maxbytes,
PATCH 3/3 fuse: allow setting of root nodeid
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Provide a new mount option so that fuse servers can actually set the root nodeid.
--- fs/fuse/fuse_i.h | 2 ++ fs/fuse/inode.c | 11 +++++++++++ 2 files changed, 13 insertions(+)
fs/fuse/fuse_i.h +2 -0
@@ -626,6 +626,7 @@ struct fuse_fs_context {int fd;struct file *file;unsigned int rootmode; +u64 root_nodeid;kuid_t user_id;kgid_t group_id; bool is_bdev:1;
@@ -639,6 +640,7 @@ struct fuse_fs_context { bool no_control:1; bool no_force_umount:1; bool legacy_opts_show:1; + bool root_nodeid_present:1;enum fuse_dax_mode dax_mode;unsigned int max_read;unsigned int blksize;
fs/fuse/inode.c +11 -0
@@ -802,6 +802,7 @@ enum {OPT_ALLOW_OTHER ,OPT_MAX_READ ,OPT_BLKSIZE , +OPT_ROOT_NODEID , OPT_ERR };
@@ -816,6 +817,7 @@ static const struct fs_parameter_spec fuse_fs_parameters[] = {fsparam_u32 ("max_read" ,OPT_MAX_READ ),fsparam_u32 ("blksize" ,OPT_BLKSIZE ),fsparam_string ("subtype" ,OPT_SUBTYPE ), + fsparam_u64 ("root_nodeid", OPT_ROOT_NODEID), {} };
@@ -911,6 +913,11 @@ static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param) ctx->blksize = result.uint_32;break ; +case OPT_ROOT_NODEID : + ctx->root_nodeid = result.uint_64; + ctx->root_nodeid_present = true; +break ; +default :return -EINVAL ; }
@@ -946,6 +953,8 @@ static int fuse_show_options(struct seq_file *m, struct dentry *root)seq_printf (m,",max_read=%u" , fc->max_read);if (sb->s_bdev && sb->s_blocksize !=FUSE_DEFAULT_BLKSIZE )seq_printf (m,",blksize=%lu" , sb->s_blocksize); +if (fc->root_nodeid && fc->root_nodeid !=FUSE_ROOT_ID ) +seq_printf (m,",root_nodeid=%llu" , fc->root_nodeid); }#ifdef CONFIG_FUSE_DAX if (fc->dax_mode ==FUSE_DAX_ALWAYS )
@@ -2002,6 +2011,8 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) sb->s_flags |=SB_POSIXACL ; fc->default_permissions = ctx->default_permissions; +if (ctx->root_nodeid_present) + fc->root_nodeid = ctx->root_nodeid; fc->allow_other = ctx->allow_other; fc->user_id = ctx->user_id; fc->group_id = ctx->group_id;
PATCH v6 6/8 fuse: handle timestamps and ACLs correctly when iomap is enabled
In reply to Darrick J. WongHi all,
When iomap is enabled for a fuse file, we try to keep as much of the file IO path in the kernel as we possibly can. That means no calling out to the fuse server in the IO path when we can avoid it. However, the existing FUSE architecture defers all file attributes to the fuse server -- [cm]time updates, ACL metadata management, set[ug]id removal, and permissions checking thereof, etc.
We'd really rather do all these attribute updates in the kernel, and only push them to the fuse server when it's actually necessary (e.g. fsync). Furthermore, the POSIX ACL code has the weird behavior that if the access ACL can be represented entirely by i_mode bits, it will change the mode and delete the ACL, which fuse servers generally don't seem to implement.
IOWs, we want consistent and correct (as defined by fstests) behavior of file attributes in iomap mode. Let's make the kernel manage all that and push the results to userspace as needed. This improves performance even further, since it's sort of like writeback_cache mode but more aggressive.
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-attrs
---
Commits in this patchset:
* fuse: enable caching of timestamps
* fuse: force a ctime update after a fileattr_set call when in iomap mode
* fuse: allow local filesystems to set some VFS iflags
* fuse_trace: allow local filesystems to set some VFS iflags
* fuse: cache atime when in iomap mode
* fuse: let the kernel handle KILL_SUID/KILL_SGID for iomap filesystems
* fuse_trace: let the kernel handle KILL_SUID/KILL_SGID for iomap filesystems
* fuse: update ctime when updating acls on an iomap inode
* fuse: always cache ACLs when using iomap
--- fs/fuse/fuse_i.h | 1 + fs/fuse/fuse_trace.h | 87 +++++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/fuse.h | 8 ++++ fs/fuse/acl.c | 29 +++++++++++++-- fs/fuse/dir.c | 38 ++++++++++++++++---- fs/fuse/file.c | 18 ++++++--- fs/fuse/file_iomap.c | 6 +++ fs/fuse/inode.c | 27 +++++++++++--- fs/fuse/ioctl.c | 68 +++++++++++++++++++++++++++++++++++ fs/fuse/readdir.c | 3 +- 10 files changed, 261 insertions(+), 24 deletions(-)
PATCH 1/9 fuse: enable caching of timestamps
From: Darrick J. Wong <[email protected]>
Cache the timestamps in the kernel so that the kernel sends FUSE_SETATTR calls to the fuse server after writes, because the iomap infrastructure won't do that for us.
--- fs/fuse/dir.c | 5 ++++- fs/fuse/file.c | 18 ++++++++++++------ fs/fuse/file_iomap.c | 6 ++++++ fs/fuse/inode.c | 13 +++++++------ 4 files changed, 29 insertions(+), 13 deletions(-)
fs/fuse/dir.c +4 -1
@@ -2055,7 +2055,8 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,struct fuse_attr_out outarg;const bool is_iomap =fuse_inode_has_iomap (inode);bool is_truncate = false; -bool is_wb = fc->writeback_cache &&S_ISREG (inode->i_mode); +bool is_wb = (is_iomap || fc->writeback_cache) && +S_ISREG (inode->i_mode);loff_t oldsize;int err;bool trust_local_cmtime = is_wb;
@@ -2189,6 +2190,8 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,spin_lock (&fi->lock);/* the kernel maintains i_mtime locally */ if (trust_local_cmtime) { +if ((attr->ia_valid &ATTR_ATIME ) && is_iomap) +inode_set_atime_to_ts (inode, attr->ia_atime);if (attr->ia_valid &ATTR_MTIME )inode_set_mtime_to_ts (inode, attr->ia_mtime);if (attr->ia_valid &ATTR_CTIME )
fs/fuse/file.c +12 -6
@@ -240,7 +240,7 @@ static int fuse_open(struct inode *inode, struct file *file)int err;const bool is_iomap =fuse_inode_has_iomap (inode);bool is_truncate = (file->f_flags &O_TRUNC ) && fc->atomic_o_trunc; -bool is_wb_truncate = is_truncate && fc->writeback_cache; +bool is_wb_truncate = is_truncate && (is_iomap || fc->writeback_cache);bool dax_truncate = is_truncate &&FUSE_IS_DAX (inode);if (fuse_is_bad (inode))
@@ -453,12 +453,14 @@ static int fuse_flush(struct file *file, fl_owner_t id)struct fuse_file *ff = file->private_data;struct fuse_flush_in inarg;FUSE_ARGS (args); +const bool is_iomap =fuse_inode_has_iomap (inode);int err;if (fuse_is_bad (inode))return -EIO ; -if (ff->open_flags &FOPEN_NOFLUSH && !fm->fc->writeback_cache) +if ((ff->open_flags &FOPEN_NOFLUSH ) && + !fm->fc->writeback_cache && !is_iomap)return 0 ; err =write_inode_now (inode,1 );
@@ -494,7 +496,7 @@ static int fuse_flush(struct file *file, fl_owner_t id)* In memory i_blocks is not maintained by fuse, if writeback cache is * enabled, i_blocks from cached attr may not be accurate. */ -if (!err && fm->fc->writeback_cache) +if (!err && (is_iomap || fm->fc->writeback_cache))fuse_invalidate_attr_mask (inode,STATX_BLOCKS );return err; }
@@ -796,8 +798,10 @@ static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,* If writeback_cache is enabled, a short read means there's a hole in * the file. Some data after the hole is in page cache, but has not * reached the client fs yet. So the hole is not present there. +* If iomap is enabled, a short read means we hit EOF so there's +* nothing to adjust. */ -if (!fc->writeback_cache) { +if (!fc->writeback_cache && !fuse_inode_has_iomap (inode)) {loff_t pos =folio_pos (ap->folios[0 ]) + num_read;fuse_read_update_size (inode, pos, attr_ver); }
@@ -1409,6 +1413,8 @@ static int fuse_iomap_begin(struct inode *inode, loff_t offset, loff_t length, unsigned int flags, struct iomap *iomap, struct iomap *srcmap) { +WARN_ON (fuse_inode_has_iomap (inode )); + iomap->type =IOMAP_MAPPED ; iomap->length = length; iomap->offset = offset;
@@ -1972,7 +1978,7 @@ static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,* Do this only if writeback_cache is not enabled. If writeback_cache * is enabled, we trust local ctime/mtime. */ -if (!fc->writeback_cache) +if (!fc->writeback_cache && !fuse_inode_has_iomap (inode))fuse_invalidate_attr_mask (inode,FUSE_STATX_MODIFY );spin_lock (&fi->lock); fi->writectr--;
@@ -3057,7 +3063,7 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,/* mark unstable when write-back is not used, and file_out gets * extended */ const bool is_iomap =fuse_inode_has_iomap (inode_out); -bool is_unstable = (!fc->writeback_cache) && +bool is_unstable = (!fc->writeback_cache && !is_iomap) && ((pos_out + len) > inode_out->i_size);if (fc->no_copy_file_range)
fs/fuse/file_iomap.c +6 -0
@@ -1834,6 +1834,12 @@ static inline void fuse_inode_set_iomap(struct inode *inode)struct fuse_inode *fi =get_fuse_inode (inode);unsigned int min_order =0 ; +/* +* Manage timestamps ourselves, don't make the fuse server do it. This +* is critical for mtime updates to work correctly with page_mkwrite. +*/ + inode->i_flags &= ~S_NOCMTIME ; + inode->i_flags &= ~S_NOATIME ; inode->i_data.a_ops = &fuse_iomap_aops;INIT_WORK (&fi->ioend_work, fuse_iomap_end_io);
fs/fuse/inode.c +7 -6
@@ -326,10 +326,11 @@ u32 fuse_get_cache_mask(struct inode *inode) {struct fuse_conn *fc =get_fuse_conn (inode); -if (!fc->writeback_cache || !S_ISREG (inode->i_mode)) -return 0 ; +if (S_ISREG (inode->i_mode) && + (fuse_inode_has_iomap (inode) || fc->writeback_cache)) +return STATX_MTIME |STATX_CTIME |STATX_SIZE ; -return STATX_MTIME |STATX_CTIME |STATX_SIZE ; +return 0 ; } static void fuse_change_attributes_i(struct inode *inode, struct fuse_attr *attr,
@@ -344,9 +345,9 @@ static void fuse_change_attributes_i(struct inode *inode, struct fuse_attr *attrspin_lock (&fi->lock);/* -* In case of writeback_cache enabled, writes update mtime, ctime and -* may update i_size. In these cases trust the cached value in the -* inode. +* In case of writeback_cache or iomap enabled, writes update mtime, +* ctime and may update i_size. In these cases trust the cached value +* in the inode. */ cache_mask =fuse_get_cache_mask (inode);if (cache_mask &STATX_SIZE )
PATCH 2/9 fuse: force a ctime update after a fileattr_set call when in iomap mode
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
In iomap mode, the kernel is in charge of driving ctime updates to the fuse server and ignores updates coming from the fuse server. Therefore, when someone calls fileattr_set to change file attributes, we must force a ctime update.
Found by generic/277.
--- fs/fuse/ioctl.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
fs/fuse/ioctl.c +11 -0
@@ -546,8 +546,13 @@ int fuse_fileattr_set(struct mnt_idmap *idmap,struct fuse_file *ff;unsigned int flags = fa->flags;struct fsxattr xfa; +struct file_kattr old_ma = { }; +bool is_wb = (fuse_get_cache_mask (inode) &STATX_CTIME );int err; +if (is_wb) +vfs_fileattr_get (dentry, &old_ma); + ff =fuse_priv_ioctl_prepare (inode);if (IS_ERR (ff))return PTR_ERR (ff);
@@ -571,6 +576,12 @@ int fuse_fileattr_set(struct mnt_idmap *idmap, cleanup:fuse_priv_ioctl_cleanup (inode, ff); +/* +* If we cache ctime updates and the fileattr changed, then force a +* ctime update. +*/ +if (is_wb &&memcmp (&old_ma, fa,sizeof (old_ma))) +fuse_update_ctime (inode);return err; }
PATCH 3/9 fuse: allow local filesystems to set some VFS iflags
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
There are three inode flags (immutable, append, sync) that are enforced by the VFS. Whenever we go around setting iflags, let's update the VFS state so that they actually work. Make it so that the fuse server can set these three inode flags at load time and have the kernel advertise and enforce them.
--- fs/fuse/fuse_i.h | 1 + include/uapi/linux/fuse.h | 8 +++++++ fs/fuse/dir.c | 1 + fs/fuse/inode.c | 1 + fs/fuse/ioctl.c | 50 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+)
fs/fuse/fuse_i.h +1 -0
@@ -1633,6 +1633,7 @@ long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,int fuse_fileattr_get (struct dentry *dentry,struct file_kattr *fa);int fuse_fileattr_set (struct mnt_idmap *idmap,struct dentry *dentry,struct file_kattr *fa); +void fuse_fileattr_init (struct inode *inode,const struct fuse_attr *attr);/* iomode.c */ int fuse_file_cached_io_open (struct inode *inode,struct fuse_file *ff);
include/uapi/linux/fuse.h +8 -0
@@ -247,6 +247,8 @@* - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry * - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges * - add FUSE_ATTR_ATOMIC for single-fsblock atomic write support +* - add FUSE_ATTR_{SYNC,IMMUTABLE,APPEND} for VFS enforcement of file +* attributes */ #ifndef _LINUX_FUSE_H
@@ -602,11 +604,17 @@ struct fuse_file_lock {* FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode * FUSE_ATTR_IOMAP: Use iomap for this inode * FUSE_ATTR_ATOMIC: Enable untorn writes +* FUSE_ATTR_SYNC: File writes are synchronous +* FUSE_ATTR_IMMUTABLE: File is immutable +* FUSE_ATTR_APPEND: File is append-only */ #define FUSE_ATTR_SUBMOUNT (1 << 0)#define FUSE_ATTR_DAX (1 << 1)#define FUSE_ATTR_IOMAP (1 << 2)#define FUSE_ATTR_ATOMIC (1 << 3) +#define FUSE_ATTR_SYNC (1 << 4) +#define FUSE_ATTR_IMMUTABLE (1 << 5) +#define FUSE_ATTR_APPEND (1 << 6)/** * Open flags
fs/fuse/dir.c +1 -0
@@ -1247,6 +1247,7 @@ static void fuse_fillattr(struct mnt_idmap *idmap, struct inode *inode, blkbits = fc->blkbits; stat->blksize =1 << blkbits; +generic_fill_statx_attr (inode, stat); }static void fuse_statx_to_attr (struct fuse_statx *sx,struct fuse_attr *attr)
fs/fuse/inode.c +1 -0
@@ -531,6 +531,7 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid, inode->i_flags |=S_NOCMTIME ; inode->i_generation = generation;fuse_init_inode (inode, attr, fc); +fuse_fileattr_init (inode, attr);unlock_new_inode (inode); }else if (fuse_stale_inode (inode, generation, attr)) {/* nodeid was reused, any I/O on the old inode should fail */
fs/fuse/ioctl.c +50 -0
63 lines not on this page · raw message
PATCH 4/9 fuse_trace: allow local filesystems to set some VFS iflags
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 29 +++++++++++++++++++++++++++++ fs/fuse/ioctl.c | 7 +++++++ 2 files changed, 36 insertions(+)
fs/fuse/fuse_trace.h +29 -0
@@ -176,6 +176,35 @@ TRACE_EVENT(fuse_request_end, __entry->unique, __entry->len, __entry->error) ); +DECLARE_EVENT_CLASS (fuse_fileattr_class, + TP_PROTO(const struct inode *inode, unsigned int old_iflags), + +TP_ARGS (inode, old_iflags), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS + __field(unsigned int, old_iflags) + __field(unsigned int, new_iflags) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->old_iflags = old_iflags; + __entry->new_iflags = inode->i_flags; + ), + +TP_printk (FUSE_INODE_FMT " old_iflags 0x%x iflags 0x%x" , +FUSE_INODE_PRINTK_ARGS , + __entry->old_iflags, + __entry->new_iflags) +); +#define DEFINE_FUSE_FILEATTR_EVENT (name) \ +DEFINE_EVENT(fuse_fileattr_class, name, \ + TP_PROTO(const struct inode *inode, unsigned int old_iflags), \ + TP_ARGS(inode, old_iflags)) +DEFINE_FUSE_FILEATTR_EVENT (fuse_fileattr_update_inode); +DEFINE_FUSE_FILEATTR_EVENT (fuse_fileattr_init); +#ifdef CONFIG_FUSE_BACKING #define FUSE_BACKING_FLAG_STRINGS \ { FUSE_BACKING_TYPE_PASSTHROUGH, "pass" }, \
fs/fuse/ioctl.c +7 -0
@@ -4,6 +4,7 @@*/ #include "fuse_i.h" +#include "fuse_trace.h" #include <linux/uio.h> #include <linux/compat.h>
@@ -530,12 +531,16 @@ static void fuse_fileattr_update_inode(struct inode *inode,update_iflag (inode,S_APPEND , fa->fsx_xflags &FS_XFLAG_APPEND ); } +trace_fuse_fileattr_update_inode (inode, old_iflags); +if (old_iflags != inode->i_flags)fuse_invalidate_attr (inode); }void fuse_fileattr_init (struct inode *inode,const struct fuse_attr *attr) { +unsigned int old_iflags = inode->i_flags; +if (!fuse_inode_is_exclusive (inode))return ;
@@ -547,6 +552,8 @@ void fuse_fileattr_init(struct inode *inode, const struct fuse_attr *attr)if (attr->flags &FUSE_ATTR_APPEND ) inode->i_flags |=S_APPEND ; + +trace_fuse_fileattr_init (inode, old_iflags); }int fuse_fileattr_get (struct dentry *dentry,struct file_kattr *fa)
PATCH 5/9 fuse: cache atime when in iomap mode
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
When we're running in iomap mode, allow the kernel to cache the access timestamp to further reduce the number of roundtrips to the fuse server.
--- fs/fuse/dir.c | 5 +++++ fs/fuse/inode.c | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-)
fs/fuse/dir.c +5 -0
@@ -2026,6 +2026,11 @@ int fuse_flush_times(struct inode *inode, struct fuse_file *ff) inarg.ctime =inode_get_ctime_sec (inode); inarg.ctimensec =inode_get_ctime_nsec (inode); } +if (fuse_inode_has_iomap (inode)) { + inarg.valid |=FATTR_ATIME ; + inarg.atime =inode_get_atime_sec (inode); + inarg.atimensec =inode_get_atime_nsec (inode); + }if (ff) { inarg.valid |=FATTR_FH ; inarg.fh = ff->fh;
fs/fuse/inode.c +16 -3
@@ -263,7 +263,8 @@ void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr, attr->mtimensec =min_t (u32, attr->mtimensec,NSEC_PER_SEC -1 ); attr->ctimensec =min_t (u32, attr->ctimensec,NSEC_PER_SEC -1 ); -inode_set_atime (inode, attr->atime, attr->atimensec); +if (!(cache_mask &STATX_ATIME )) +inode_set_atime (inode, attr->atime, attr->atimensec);/* mtime from server may be stale due to local buffered write */ if (!(cache_mask &STATX_MTIME )) {inode_set_mtime (inode, attr->mtime, attr->mtimensec);
@@ -326,8 +327,12 @@ u32 fuse_get_cache_mask(struct inode *inode) {struct fuse_conn *fc =get_fuse_conn (inode); -if (S_ISREG (inode->i_mode) && - (fuse_inode_has_iomap (inode) || fc->writeback_cache)) +if (!S_ISREG (inode->i_mode)) +return 0 ; + +if (fuse_inode_has_iomap (inode)) +return STATX_MTIME |STATX_CTIME |STATX_ATIME |STATX_SIZE ; +if (fc->writeback_cache)return STATX_MTIME |STATX_CTIME |STATX_SIZE ;return 0 ;
@@ -458,6 +463,14 @@ static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr,BUG ();break ; } + +/* +* iomap caches atime too, so we must load it from the fuse server +* at instantiation time. +*/ +if (fuse_inode_has_iomap (inode)) +inode_set_atime (inode, attr->atime, attr->atimensec); +/* * Ensure that we don't cache acls for daemons without FUSE_POSIX_ACL * so they see the exact same behavior as before.
PATCH 6/9 fuse: let the kernel handle KILL_SUID/KILL_SGID for iomap filesystems
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Let the kernel handle killing the suid/sgid bits because the write/falloc/truncate/chown code already does this, and we don't have to worry about external modifications that are only visible to the fuse server (i.e. we're not a cluster fs).
--- fs/fuse/dir.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
fs/fuse/dir.c +8 -3
@@ -2261,6 +2261,7 @@ static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry,struct inode *inode =d_inode (entry);struct fuse_conn *fc =get_fuse_conn (inode);struct file *file = (attr->ia_valid &ATTR_FILE ) ? attr->ia_file :NULL ; +const bool is_iomap =fuse_inode_has_iomap (inode);int ret;if (fuse_is_bad (inode))
@@ -2269,15 +2270,19 @@ static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry,if (!fuse_allow_current_process (get_fuse_conn (inode)))return -EACCES ; -if (attr->ia_valid & (ATTR_KILL_SUID |ATTR_KILL_SGID )) { +if (!is_iomap && + (attr->ia_valid & (ATTR_KILL_SUID |ATTR_KILL_SGID ))) { attr->ia_valid &= ~(ATTR_KILL_SUID |ATTR_KILL_SGID |ATTR_MODE );/* * The only sane way to reliably kill suid/sgid is to do it in -* the userspace filesystem +* the userspace filesystem if this isn't an iomap file. For +* iomap filesystems we let the kernel kill the setuid/setgid +* bits. * -* This should be done on write(), truncate() and chown(). +* This should be done on write(), truncate(), chown(), and +* fallocate(). */ if (!fc->handle_killpriv && !fc->handle_killpriv_v2) {/*
PATCH 7/9 fuse_trace: let the kernel handle KILL_SUID/KILL_SGID for iomap filesystems
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/dir.c | 5 ++++ 2 files changed, 63 insertions(+)
fs/fuse/fuse_trace.h +58 -0
@@ -205,6 +205,64 @@ DEFINE_EVENT(fuse_fileattr_class, name, \DEFINE_FUSE_FILEATTR_EVENT (fuse_fileattr_update_inode);DEFINE_FUSE_FILEATTR_EVENT (fuse_fileattr_init); +TRACE_EVENT (fuse_setattr_fill, + TP_PROTO(const struct inode *inode, + const struct fuse_setattr_in *inarg), +TP_ARGS (inode, inarg), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS + __field(umode_t, mode) + __field(uint32_t, valid) + __field(umode_t, new_mode) +__field (uint64_t, new_size) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->mode = inode->i_mode; + __entry->valid = inarg->valid; + __entry->new_mode = inarg->mode; + __entry->new_size = inarg->size; + ), + +TP_printk (FUSE_INODE_FMT " mode 0%o valid 0x%x new_mode 0%o new_size 0x%llx" , +FUSE_INODE_PRINTK_ARGS , + __entry->mode, + __entry->valid, + __entry->new_mode, + __entry->new_size) +); + +TRACE_EVENT (fuse_setattr, + TP_PROTO(const struct inode *inode, + const struct iattr *inarg), +TP_ARGS (inode, inarg), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS + __field(umode_t, mode) + __field(uint32_t, valid) + __field(umode_t, new_mode) +__field (uint64_t, new_size) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->mode = inode->i_mode; + __entry->valid = inarg->ia_valid; + __entry->new_mode = inarg->ia_mode; + __entry->new_size = inarg->ia_size; + ), + +TP_printk (FUSE_INODE_FMT " mode 0%o valid 0x%x new_mode 0%o new_size 0x%llx" , +FUSE_INODE_PRINTK_ARGS , + __entry->mode, + __entry->valid, + __entry->new_mode, + __entry->new_size) +); +#ifdef CONFIG_FUSE_BACKING #define FUSE_BACKING_FLAG_STRINGS \ { FUSE_BACKING_TYPE_PASSTHROUGH, "pass" }, \
fs/fuse/dir.c +5 -0
@@ -7,6 +7,7 @@*/ #include "fuse_i.h" +#include "fuse_trace.h" #include <linux/pagemap.h> #include <linux/file.h>
@@ -1995,6 +1996,8 @@ static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,struct fuse_setattr_in *inarg_p, struct fuse_attr_out *outarg_p) { +trace_fuse_setattr_fill (inode ,inarg_p ); + args->opcode =FUSE_SETATTR ; args->nodeid =get_node_id (inode); args->in_numargs =1 ;
@@ -2270,6 +2273,8 @@ static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry,if (!fuse_allow_current_process (get_fuse_conn (inode)))return -EACCES ; +trace_fuse_setattr (inode, attr); +if (!is_iomap && (attr->ia_valid & (ATTR_KILL_SUID |ATTR_KILL_SGID ))) { attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
PATCH 8/9 fuse: update ctime when updating acls on an iomap inode
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
In iomap mode, the fuse kernel driver is in charge of updating file attributes, so we need to update ctime after an ACL change.
--- fs/fuse/acl.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
fs/fuse/acl.c +16 -1
@@ -111,6 +111,7 @@ int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,struct fuse_conn *fc =get_fuse_conn (inode);const char *name;umode_t mode = inode->i_mode; +const bool is_iomap =fuse_inode_has_iomap (inode);int ret;if (fuse_is_bad (inode))
@@ -182,10 +183,24 @@ int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, ret =0 ; } -/* If we scheduled a mode update above, push that to userspace now. */ if (!ret) {struct iattr attr = { }; +/* +* When we're running in iomap mode, we need to update mode and +* ctime ourselves instead of letting the fuse server figure +* that out. +*/ +if (is_iomap) { + attr.ia_valid |=ATTR_CTIME ; +inode_set_ctime_current (inode); + attr.ia_ctime =inode_get_ctime (inode); + } + +/* +* If we scheduled a mode update above, push that to userspace +* now. +*/ if (mode != inode->i_mode) { attr.ia_valid |=ATTR_MODE ; attr.ia_mode = mode;
PATCH 9/9 fuse: always cache ACLs when using iomap
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Keep ACLs cached in memory when we're using iomap, so that we don't have to make a round trip to the fuse server. This might want to become a FUSE_ATTR_ flag.
--- fs/fuse/acl.c | 12 +++++++++--- fs/fuse/dir.c | 11 ++++++++--- fs/fuse/readdir.c | 3 ++- 3 files changed, 19 insertions(+), 7 deletions(-)
fs/fuse/acl.c +9 -3
@@ -213,10 +213,16 @@ int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,if (fc->posix_acl) {/* * Fuse daemons without FUSE_POSIX_ACL never cached POSIX ACLs -* and didn't invalidate attributes. Retain that behavior. +* and didn't invalidate attributes. Retain that behavior +* except for iomap, where we assume that only the source of +* ACL changes is userspace. */ -forget_all_cached_acls (inode); -fuse_invalidate_attr (inode); +if (!ret && is_iomap) { +set_cached_acl (inode, type, acl); + }else { +forget_all_cached_acls (inode); +fuse_invalidate_attr (inode); + } }return ret;
fs/fuse/dir.c +8 -3
@@ -261,7 +261,8 @@ static int fuse_dentry_revalidate(struct inode *dir, const struct qstr *name, fuse_stale_inode(inode, outarg.generation, &outarg.attr)) goto invalid; -forget_all_cached_acls (inode); +if (!fuse_inode_has_iomap (inode)) +forget_all_cached_acls (inode);fuse_change_attributes (inode, &outarg.attr,NULL ,ATTR_TIMEOUT (&outarg), attr_version);
@@ -1463,7 +1464,8 @@ static int fuse_update_get_attr(struct mnt_idmap *idmap, struct inode *inode, sync =time_before64 (fi->i_time,get_jiffies_64 ());if (sync) { -forget_all_cached_acls (inode); +if (!fuse_inode_has_iomap (inode)) +forget_all_cached_acls (inode);/* Try statx if a field not covered by regular stat is wanted */ if (!fc->no_statx && (request_mask & ~STATX_BASIC_STATS )) { err =fuse_do_statx (idmap, inode, file, stat);
@@ -1641,6 +1643,9 @@ static int fuse_access(struct inode *inode, int mask)static int fuse_perm_getattr (struct inode *inode,int mask) { +if (fuse_inode_has_iomap (inode)) +return 0 ; +if (mask &MAY_NOT_BLOCK )return -ECHILD ;
@@ -2318,7 +2323,7 @@ static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry,* If filesystem supports acls it may have updated acl xattrs in * the filesystem, so forget cached acls for the inode. */ -if (fc->posix_acl) +if (fc->posix_acl && !is_iomap)forget_all_cached_acls (inode);/* Directory mode changed, may need to revalidate access */
fs/fuse/readdir.c +2 -1
@@ -224,7 +224,8 @@ static int fuse_direntplus_link(struct file *file, fi->nlookup++;spin_unlock (&fi->lock); -forget_all_cached_acls (inode); +if (!fuse_inode_has_iomap (inode)) +forget_all_cached_acls (inode);fuse_change_attributes (inode, &o->attr,NULL ,ATTR_TIMEOUT (o), attr_version);
PATCH v6 7/8 fuse: cache iomap mappings for even better file IO performance
In reply to Darrick J. WongHi all,
This series improves the performance (and correctness for some filesystems) by adding the ability to cache iomap mappings in the kernel. For filesystems that can change mapping states during pagecache writeback (e.g. unwritten extent conversion) this is absolutely necessary to deal with races with writes to the pagecache because writeback does not take i_rwsem. For everyone else, it simply eliminates roundtrips to userspace.
If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.
This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
---
Commits in this patchset:
* fuse: cache iomaps
* fuse_trace: cache iomaps
* fuse: use the iomap cache for iomap_begin
* fuse_trace: use the iomap cache for iomap_begin
* fuse: invalidate iomap cache after file updates
* fuse_trace: invalidate iomap cache after file updates
* fuse: enable iomap cache management
* fuse_trace: enable iomap cache management
* fuse: overlay iomap inode info in struct fuse_inode
* fuse: enable iomap
--- fs/fuse/fuse_i.h | 60 ++ fs/fuse/fuse_trace.h | 440 ++++++++++++ fs/fuse/iomap_i.h | 149 ++++ include/uapi/linux/fuse.h | 33 + fs/fuse/Makefile | 2 fs/fuse/dev.c | 44 + fs/fuse/dir.c | 6 fs/fuse/file.c | 26 - fs/fuse/file_iomap.c | 541 ++++++++++++++ fs/fuse/iomap_cache.c | 1693 +++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 2968 insertions(+), 26 deletions(-) create mode 100644 fs/fuse/iomap_cache.c
PATCH 1/10 fuse: cache iomaps
From: Darrick J. Wong <[email protected]>
Cache iomaps to a file so that we don't have to upcall the server.
--- fs/fuse/fuse_i.h | 39 + fs/fuse/iomap_i.h | 135 ++++ include/uapi/linux/fuse.h | 5 fs/fuse/Makefile | 2 fs/fuse/file_iomap.c | 21 + fs/fuse/iomap_cache.c | 1629 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 1825 insertions(+), 6 deletions(-) create mode 100644 fs/fuse/iomap_cache.c
fs/fuse/fuse_i.h +39 -0
@@ -122,6 +122,24 @@ struct fuse_backing {struct rcu_head rcu; }; +#if IS_ENABLED (CONFIG_FUSE_IOMAP ) +/* +* File incore extent information, present for each of data & attr forks. +*/ +struct fuse_ifork { +int64_t if_bytes;/* bytes in if_data */ +void *if_data;/* extent tree root */ +int if_height;/* height of the extent tree */ +}; + +struct fuse_iomap_cache { +struct fuse_ifork im_read; +struct fuse_ifork *im_write; +uint64_t im_seq;/* validity counter */ +struct rw_semaphore im_lock;/* mapping lock */ +}; +#endif +/** FUSE inode */ struct fuse_inode {/** Inode data */
@@ -187,6 +205,9 @@ struct fuse_inode {spinlock_t ioend_lock;struct work_struct ioend_work;struct list_head ioend_list; + +/* cached iomap mappings */ +struct fuse_iomap_cache cache;#endif };
@@ -268,6 +289,11 @@ enum {FUSE_I_IOMAP ,/* Enable untorn writes */ FUSE_I_ATOMIC , +/* +* Cache iomaps in the kernel. This is required for any filesystem +* that needs to synchronize pagecache write and writeback. +*/ + FUSE_I_IOMAP_CACHE, };struct fuse_conn ;
@@ -1819,6 +1845,18 @@ int fuse_iomap_dev_inval(struct fuse_conn *fc, const struct fuse_iomap_dev_inval_out *arg);int fuse_iomap_fadvise (struct file *file,loff_t start,loff_t end,int advice); + +static inline bool fuse_inode_caches_iomaps (const struct inode *inode) +{ +const struct fuse_inode *fi =get_fuse_inode (inode); + +return test_bit (FUSE_I_IOMAP_CACHE , &fi->state); +} + +enum fuse_iomap_iodir { +READ_MAPPING , +WRITE_MAPPING , +};#else # define fuse_iomap_enabled (...) (false)# define fuse_has_iomap (...) (false)
@@ -1846,6 +1884,7 @@ int fuse_iomap_fadvise(struct file *file, loff_t start, loff_t end, int advice);# define fuse_dev_ioctl_iomap_support (...) (-EOPNOTSUPP)# define fuse_iomap_dev_inval (...) (-ENOSYS)# define fuse_iomap_fadvise NULL +# define fuse_inode_caches_iomaps (...) (false)#endif #endif /* _FS_FUSE_I_H */
fs/fuse/iomap_i.h +130 -5
148 lines not on this page · raw message
include/uapi/linux/fuse.h +5 -0
@@ -1388,6 +1388,8 @@ struct fuse_uring_cmd_req {/* fuse-specific mapping type indicating that writes use the read mapping */ #define FUSE_IOMAP_TYPE_PURE_OVERWRITE (255) +/* fuse-specific mapping type saying the server has populated the cache */ +#define FUSE_IOMAP_TYPE_RETRY_CACHE (254)#define FUSE_IOMAP_DEV_NULL (0U)/* null device cookie */
@@ -1535,4 +1537,7 @@ struct fuse_iomap_dev_inval_out { uint64_t length; }; +/* invalidate all cached iomap mappings up to EOF */ +#define FUSE_IOMAP_INVAL_TO_EOF (~0ULL) +#endif /* _LINUX_FUSE_H */
fs/fuse/Makefile +1 -1
@@ -18,6 +18,6 @@ fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o fuse-$(CONFIG_FUSE_BACKING) += backing.o fuse-$(CONFIG_SYSCTL) += sysctl.o fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o -fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o +fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o iomap_cache.o virtiofs-y := virtio_fs.o
fs/fuse/file_iomap.c +21 -0
@@ -1130,6 +1130,19 @@ static inline void fuse_inode_clear_atomic(struct inode *inode)clear_bit (FUSE_I_ATOMIC , &fi->state); } +static inline void fuse_iomap_clear_cache (struct inode *inode) +{ +struct fuse_inode *fi =get_fuse_inode (inode); + +clear_bit (FUSE_I_IOMAP_CACHE , &fi->state); + +fuse_iext_destroy (&fi->cache.im_read); +if (fi->cache.im_write) { +fuse_iext_destroy (fi->cache.im_write); +kfree (fi->cache.im_write); + } +} +void fuse_iomap_init_nonreg_inode (struct inode *inode,unsigned attr_flags) {struct fuse_conn *conn =get_fuse_conn (inode);
@@ -1167,6 +1180,8 @@ void fuse_iomap_evict_inode(struct inode *inode)trace_fuse_iomap_evict_inode (inode); +if (fuse_inode_caches_iomaps (inode)) +fuse_iomap_clear_cache (inode);if (fuse_inode_has_atomic (inode))fuse_inode_clear_atomic (inode);if (fuse_inode_has_iomap (inode))
@@ -1850,6 +1865,12 @@ static inline void fuse_inode_set_iomap(struct inode *inode) min_order = inode->i_blkbits -PAGE_SHIFT ;mapping_set_folio_min_order (inode->i_mapping, min_order); + +memset (&fi->cache.im_read,0 ,sizeof (fi->cache.im_read)); + fi->cache.im_seq =0 ; + fi->cache.im_write =NULL ; + +init_rwsem (&fi->cache.im_lock);set_bit (FUSE_I_IOMAP , &fi->state); }
fs/fuse/iomap_cache.c new file +1629 -0
1,629 lines not on this page · raw message
PATCH 2/10 fuse_trace: cache iomaps
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 295 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/iomap_cache.c | 31 +++++ 2 files changed, 325 insertions(+), 1 deletion(-)
fs/fuse/fuse_trace.h +294 -1
@@ -315,6 +315,8 @@ DEFINE_FUSE_BACKING_EVENT(fuse_backing_close);struct iomap_writepage_ctx ;struct iomap_ioend ;struct iomap ; +struct fuse_iext_cursor ; +struct fuse_iomap_lookup ;/* tracepoint boilerplate so we don't have to keep doing this */ #define FUSE_IOMAP_OPFLAGS_FIELD \
@@ -345,6 +347,16 @@ struct iomap; __entry->prefix##addr, \ __print_flags(__entry->prefix##flags, "|", FUSE_IOMAP_F_STRINGS) +#define FUSE_IOMAP_IODIR_FIELD \ + __field(enum fuse_iomap_iodir, iodir) + +#define FUSE_IOMAP_IODIR_FMT \ + " iodir %s" + +#define FUSE_IOMAP_IODIR_PRINTK_ARGS \ + __print_symbolic(__entry->iodir, FUSE_IOMAP_FORK_STRINGS) + +/* combinations of boilerplate to reduce typing further */ #define FUSE_IOMAP_OP_FIELDS (prefix) \ FUSE_INODE_FIELDS \
@@ -416,6 +428,7 @@ TRACE_DEFINE_ENUM(FUSE_I_CACHE_IO_MODE);TRACE_DEFINE_ENUM (FUSE_I_EXCLUSIVE );TRACE_DEFINE_ENUM (FUSE_I_IOMAP );TRACE_DEFINE_ENUM (FUSE_I_ATOMIC ); +TRACE_DEFINE_ENUM (FUSE_I_IOMAP_CACHE );#define FUSE_IFLAG_STRINGS \ { 1 << FUSE_I_ADVISE_RDPLUS, "advise_rdplus" }, \
@@ -426,7 +439,8 @@ TRACE_DEFINE_ENUM(FUSE_I_ATOMIC); { 1 << FUSE_I_CACHE_IO_MODE, "cacheio" }, \ { 1 << FUSE_I_EXCLUSIVE, "excl" }, \ { 1 << FUSE_I_IOMAP, "iomap" }, \ - { 1 << FUSE_I_ATOMIC, "atomic" } + { 1 << FUSE_I_ATOMIC, "atomic" }, \ + { 1 << FUSE_I_IOMAP_CACHE, "iomap_cache" } #define IOMAP_IOEND_STRINGS \ { IOMAP_IOEND_SHARED, "shared" }, \
@@ -442,6 +456,22 @@ TRACE_DEFINE_ENUM(FUSE_I_ATOMIC); { FUSE_IOMAP_CONFIG_TIME, "time" }, \ { FUSE_IOMAP_CONFIG_MAXBYTES, "maxbytes" } +TRACE_DEFINE_ENUM (READ_MAPPING ); +TRACE_DEFINE_ENUM (WRITE_MAPPING ); + +#define FUSE_IOMAP_FORK_STRINGS \ + { READ_MAPPING, "read" }, \ + { WRITE_MAPPING, "write" } + +#define FUSE_IEXT_STATE_STRINGS \ + { FUSE_IEXT_LEFT_CONTIG, "l_cont" }, \ + { FUSE_IEXT_RIGHT_CONTIG, "r_cont" }, \ + { FUSE_IEXT_LEFT_FILLING, "l_fill" }, \ + { FUSE_IEXT_RIGHT_FILLING, "r_fill" }, \ + { FUSE_IEXT_LEFT_VALID, "l_valid" }, \ + { FUSE_IEXT_RIGHT_VALID, "r_valid" }, \ + { FUSE_IEXT_WRITE_MAPPING, "write" } + DECLARE_EVENT_CLASS(fuse_iomap_check_class, TP_PROTO(const char *func, int line, const char *condition),
@@ -1181,6 +1211,269 @@ DEFINE_FUSE_IOMAP_INLINE_EVENT(fuse_iomap_inline_read);DEFINE_FUSE_IOMAP_INLINE_EVENT (fuse_iomap_inline_write);DEFINE_FUSE_IOMAP_INLINE_EVENT (fuse_iomap_set_inline_iomap);DEFINE_FUSE_IOMAP_INLINE_EVENT (fuse_iomap_set_inline_srcmap); + +DECLARE_EVENT_CLASS (fuse_iext_class, + TP_PROTO(const struct inode *inode, const struct fuse_iext_cursor *cur, + int state, unsigned long caller_ip), + +TP_ARGS (inode, cur, state, caller_ip), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS + FUSE_IOMAP_MAP_FIELDS(map) + __field(void *, leaf) + __field(int, pos) + __field(int, iext_state) + __field(unsigned long, caller_ip) + ), +TP_fast_assign ( + const struct fuse_ifork *ifp; + struct fuse_iomap_io r = { }; + FUSE_INODE_ASSIGN(inode, fi, fm); + + if (state & FUSE_IEXT_WRITE_MAPPING) + ifp = fi->cache.im_write; + else + ifp = &fi->cache.im_read; + if (ifp) + fuse_iext_get_extent(ifp, cur, &r); + + __entry->mapoffset = r.offset; + __entry->mapaddr = r.addr; + __entry->maplength = r.length; + __entry->mapdev = r.dev; + __entry->maptype = r.type; + __entry->mapflags = r.flags; + + __entry->leaf = cur->leaf; + __entry->pos = cur->pos; + + __entry->iext_state = state; + __entry->caller_ip = caller_ip; + ), + TP_printk(FUSE_INODE_FMT " state (%s) cur %p/%d " FUSE_IOMAP_MAP_FMT() " caller %pS", +FUSE_INODE_PRINTK_ARGS , +__print_flags (__entry->iext_state,"|" ,FUSE_IEXT_STATE_STRINGS ), + __entry->leaf, + __entry->pos, +FUSE_IOMAP_MAP_PRINTK_ARGS (map), + (void *)__entry->caller_ip) +) + +#define DEFINE_IEXT_EVENT (name) \ +DEFINE_EVENT(fuse_iext_class, name, \ + TP_PROTO(const struct inode *inode, const struct fuse_iext_cursor *cur, \ + int state, unsigned long caller_ip), \ + TP_ARGS(inode, cur, state, caller_ip)) +DEFINE_IEXT_EVENT (fuse_iext_insert); +DEFINE_IEXT_EVENT (fuse_iext_remove); +DEFINE_IEXT_EVENT (fuse_iext_pre_update); +DEFINE_IEXT_EVENT (fuse_iext_post_update); + +TRACE_EVENT (fuse_iext_update_class, + TP_PROTO(const struct inode *inode, uint32_t iext_state, + const struct fuse_iomap_io *map), +TP_ARGS (inode, iext_state, map), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS + FUSE_IOMAP_MAP_FIELDS(map) +__field (uint32_t, iext_state) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->mapoffset = map->offset; + __entry->maplength = map->length; + __entry->maptype = map->type; + __entry->mapflags = map->flags; + __entry->mapdev = map->dev; + __entry->mapaddr = map->addr; + + __entry->iext_state = iext_state; + ), + +TP_printk (FUSE_INODE_FMT " state (%s)" FUSE_IOMAP_MAP_FMT (), +FUSE_INODE_PRINTK_ARGS , +__print_flags (__entry->iext_state,"|" ,FUSE_IEXT_STATE_STRINGS ), +FUSE_IOMAP_MAP_PRINTK_ARGS (map)) +); +#define DEFINE_IEXT_UPDATE_EVENT (name) \ +DEFINE_EVENT(fuse_iext_update_class, name, \ + TP_PROTO(const struct inode *inode, uint32_t iext_state, \ + const struct fuse_iomap_io *map), \ + TP_ARGS(inode, iext_state, map)) +DEFINE_IEXT_UPDATE_EVENT (fuse_iext_del_mapping); +DEFINE_IEXT_UPDATE_EVENT (fuse_iext_add_mapping); + +TRACE_EVENT (fuse_iext_alt_update_class, + TP_PROTO(const struct inode *inode, const struct fuse_iomap_io *map), +TP_ARGS (inode, map), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS +FUSE_IOMAP_MAP_FIELDS (map) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + + __entry->mapoffset = map->offset; + __entry->maplength = map->length; + __entry->maptype = map->type; + __entry->mapflags = map->flags; + __entry->mapdev = map->dev; + __entry->mapaddr = map->addr; + ), + + TP_printk(FUSE_INODE_FMT FUSE_IOMAP_MAP_FMT(), +FUSE_INODE_PRINTK_ARGS , +FUSE_IOMAP_MAP_PRINTK_ARGS (map)) +); +#define DEFINE_IEXT_ALT_UPDATE_EVENT (name) \ +DEFINE_EVENT(fuse_iext_alt_update_class, name, \ + TP_PROTO(const struct inode *inode, const struct fuse_iomap_io *map), \ + TP_ARGS(inode, map)) +DEFINE_IEXT_ALT_UPDATE_EVENT (fuse_iext_del_mapping_got); +DEFINE_IEXT_ALT_UPDATE_EVENT (fuse_iext_add_mapping_left); +DEFINE_IEXT_ALT_UPDATE_EVENT (fuse_iext_add_mapping_right); + +TRACE_EVENT (fuse_iomap_cache_remove, + TP_PROTO(const struct inode *inode, enum fuse_iomap_iodir iodir, + loff_t offset, uint64_t length, unsigned long caller_ip), +TP_ARGS (inode, iodir, offset, length, caller_ip), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + FUSE_IOMAP_IODIR_FIELD + __field(unsigned long, caller_ip) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->iodir = iodir; + __entry->offset = offset; + __entry->length = length; + __entry->caller_ip = caller_ip; + ), + + TP_printk(FUSE_IO_RANGE_FMT() FUSE_IOMAP_IODIR_FMT " caller %pS", +FUSE_IO_RANGE_PRINTK_ARGS (), +FUSE_IOMAP_IODIR_PRINTK_ARGS , + (void *)__entry->caller_ip) +); + +TRACE_EVENT (fuse_iomap_cached_mapping_class, + TP_PROTO(const struct inode *inode, enum fuse_iomap_iodir iodir, + const struct fuse_iomap_io *map, unsigned long caller_ip), +TP_ARGS (inode, iodir, map, caller_ip), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS + FUSE_IOMAP_IODIR_FIELD + FUSE_IOMAP_MAP_FIELDS(map) + __field(unsigned long, caller_ip) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->iodir = iodir; + + __entry->mapoffset = map->offset; + __entry->maplength = map->length; + __entry->maptype = map->type; + __entry->mapflags = map->flags; + __entry->mapdev = map->dev; + __entry->mapaddr = map->addr; + + __entry->caller_ip = caller_ip; + ), + + TP_printk(FUSE_INODE_FMT FUSE_IOMAP_IODIR_FMT FUSE_IOMAP_MAP_FMT() " caller %pS", +FUSE_INODE_PRINTK_ARGS , +FUSE_IOMAP_IODIR_PRINTK_ARGS , +FUSE_IOMAP_MAP_PRINTK_ARGS (map), + (void *)__entry->caller_ip) +); +#define DEFINE_FUSE_IOMAP_CACHED_MAPPING_EVENT (name) \ +DEFINE_EVENT(fuse_iomap_cached_mapping_class, name, \ + TP_PROTO(const struct inode *inode, enum fuse_iomap_iodir iodir, \ + const struct fuse_iomap_io *map, unsigned long caller_ip), \ + TP_ARGS(inode, iodir, map, caller_ip)) +DEFINE_FUSE_IOMAP_CACHED_MAPPING_EVENT (fuse_iomap_cache_add); +DEFINE_FUSE_IOMAP_CACHED_MAPPING_EVENT (fuse_iext_check_mapping); + +TRACE_EVENT (fuse_iomap_cache_lookup, + TP_PROTO(const struct inode *inode, enum fuse_iomap_iodir iodir, + loff_t pos, uint64_t count, unsigned long caller_ip), +TP_ARGS (inode, iodir, pos, count, caller_ip), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + FUSE_IOMAP_IODIR_FIELD + __field(unsigned long, caller_ip) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->iodir = iodir; + __entry->offset = pos; + __entry->length = count; + __entry->caller_ip = caller_ip; + ), + + TP_printk(FUSE_IO_RANGE_FMT() FUSE_IOMAP_IODIR_FMT " caller %pS", +FUSE_IO_RANGE_PRINTK_ARGS (), +FUSE_IOMAP_IODIR_PRINTK_ARGS , + (void *)__entry->caller_ip) +); + +TRACE_EVENT (fuse_iomap_cache_lookup_result, + TP_PROTO(const struct inode *inode, enum fuse_iomap_iodir iodir, + loff_t pos, uint64_t count, const struct fuse_iomap_io *got, + const struct fuse_iomap_lookup *map), +TP_ARGS (inode, iodir, pos, count, got, map), + +TP_STRUCT__entry ( + FUSE_IO_RANGE_FIELDS() + + FUSE_IOMAP_MAP_FIELDS(got) + FUSE_IOMAP_MAP_FIELDS(map) + + FUSE_IOMAP_IODIR_FIELD +__field (uint64_t, validity_cookie) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->iodir = iodir; + __entry->offset = pos; + __entry->length = count; + + __entry->gotoffset = got->offset; + __entry->gotlength = got->length; + __entry->gottype = got->type; + __entry->gotflags = got->flags; + __entry->gotdev = got->dev; + __entry->gotaddr = got->addr; + + __entry->mapoffset = map->map.offset; + __entry->maplength = map->map.length; + __entry->maptype = map->map.type; + __entry->mapflags = map->map.flags; + __entry->mapdev = map->map.dev; + __entry->mapaddr = map->map.addr; + + __entry->validity_cookie= map->validity_cookie; + ), + + TP_printk(FUSE_IO_RANGE_FMT() FUSE_IOMAP_IODIR_FMT FUSE_IOMAP_MAP_FMT("map") FUSE_IOMAP_MAP_FMT("got") " cookie 0x%llx", +FUSE_IO_RANGE_PRINTK_ARGS (), +FUSE_IOMAP_IODIR_PRINTK_ARGS , +FUSE_IOMAP_MAP_PRINTK_ARGS (map), +FUSE_IOMAP_MAP_PRINTK_ARGS (got), + __entry->validity_cookie) +);#endif /* CONFIG_FUSE_IOMAP */ #endif /* _TRACE_FUSE_H */
fs/fuse/iomap_cache.c +31 -0
@@ -717,6 +717,7 @@ fuse_iext_insert(struct fuse_ifork *ifp =fuse_iext_state_to_fork (ip, state);fuse_iext_insert_raw (ip, ifp, cur, irec); +trace_fuse_iext_insert (VFS_I (ip), cur, state, _RET_IP_); } static struct fuse_iext_node *
@@ -920,6 +921,8 @@ fuse_iext_remove(loff_t offset =fuse_iext_leaf_key (leaf,0 );int i, nr_entries; +trace_fuse_iext_remove (VFS_I (ip), cur, state, _RET_IP_); +ASSERT (ifp->if_height >0 );ASSERT (ifp->if_data !=NULL );ASSERT (fuse_iext_valid (ifp, cur));
@@ -1042,7 +1045,9 @@ fuse_iext_update_extent( } } +trace_fuse_iext_pre_update (VFS_I (ip), cur, state, _RET_IP_);fuse_iext_set (cur_rec (cur), new); +trace_fuse_iext_post_update (VFS_I (ip), cur, state, _RET_IP_); }/*
@@ -1150,17 +1155,25 @@ static void fuse_iext_check_mappings(struct inode *inode,struct fuse_iext_cursor icur;struct fuse_iomap_io prev, got;unsigned long long nr =0 ; +enum fuse_iomap_iodir iodir;if (!ifp || !static_branch_unlikely (&fuse_iomap_debug))return ; +if (ifp == ip->im_write) + iodir =WRITE_MAPPING ; +else + iodir =READ_MAPPING ; +fuse_iext_first (ifp, &icur);if (!fuse_iext_get_extent (ifp, &icur, &prev))return ; +trace_fuse_iext_check_mapping (inode, iodir, &prev, _RET_IP_); nr++;fuse_iext_next (ifp, &icur);while (fuse_iext_get_extent (ifp, &icur, &got)) { +trace_fuse_iext_check_mapping (inode, iodir, &got, _RET_IP_);if (got.length ==0 || got.offset < prev.offset + prev.length ||fuse_iomap_can_merge (&prev, &got)) {
@@ -1219,6 +1232,9 @@ fuse_iext_del_mapping(if (got_endoff == del_endoff) state |=FUSE_IEXT_RIGHT_FILLING ; +trace_fuse_iext_del_mapping (VFS_I (ip), state, del); +trace_fuse_iext_del_mapping_got (VFS_I (ip), got); +switch (state & (FUSE_IEXT_LEFT_FILLING |FUSE_IEXT_RIGHT_FILLING )) {case FUSE_IEXT_LEFT_FILLING |FUSE_IEXT_RIGHT_FILLING :/*
@@ -1283,6 +1299,8 @@ fuse_iomap_cache_remove(assert_cache_locked (ip); +trace_fuse_iomap_cache_remove (inode, iodir, start, len, _RET_IP_); +if (!ifp ||fuse_iext_count (ifp) ==0 )return 0 ;
@@ -1427,6 +1445,12 @@ fuse_iext_add_mapping( fuse_iomap_can_merge3(&left, new, &right))) state |=FUSE_IEXT_RIGHT_CONTIG ; +trace_fuse_iext_add_mapping (VFS_I (ip), state, new); +if (state &FUSE_IEXT_LEFT_VALID ) +trace_fuse_iext_add_mapping_left (VFS_I (ip), &left); +if (state &FUSE_IEXT_RIGHT_VALID ) +trace_fuse_iext_add_mapping_right (VFS_I (ip), &right); +/* * Select which case we're in here, and implement it. */
@@ -1495,6 +1519,8 @@ fuse_iomap_cache_add(ASSERT (new->length >0 );ASSERT (new->offset < inode->i_sb->s_maxbytes); +trace_fuse_iomap_cache_add (inode, iodir, new, _RET_IP_); +if (!ifp) { ifp =kzalloc (sizeof (struct fuse_ifork ),GFP_KERNEL | __GFP_NOFAIL);
@@ -1599,6 +1625,8 @@ fuse_iomap_cache_lookup(assert_cache_locked_shared (ip); +trace_fuse_iomap_cache_lookup (inode, iodir, off, len, _RET_IP_); +if (!ifp) {/* * No write fork at all means this filesystem doesn't do out of
@@ -1625,5 +1653,8 @@ fuse_iomap_cache_lookup(/* Found a mapping in the cache, return it */ fuse_iomap_trim (fi, mval, &got, off, len); + +trace_fuse_iomap_cache_lookup_result (inode, iodir, off, len, &got, + mval);return LOOKUP_HIT ; }
PATCH 3/10 fuse: use the iomap cache for iomap_begin
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Look inside the iomap cache to try to satisfy iomap_begin.
--- fs/fuse/iomap_i.h | 5 + fs/fuse/file_iomap.c | 223 ++++++++++++++++++++++++++++++++++++++++++++++++- fs/fuse/iomap_cache.c | 6 + 3 files changed, 228 insertions(+), 6 deletions(-)
fs/fuse/iomap_i.h +5 -0
@@ -145,6 +145,11 @@ static inline bool fuse_iext_peek_prev_extent(struct fuse_ifork *ifp,fuse_iext_get_extent ((ifp), (ext), (got)); \ fuse_iext_next((ifp), (ext))) +/* iomaps that come direct from the fuse server are presumed to be valid */ +#define FUSE_IOMAP_ALWAYS_VALID ((uint64_t)0) +/* set initial iomap cookie value to avoid ALWAYS_VALID */ +#define FUSE_IOMAP_INIT_COOKIE ((uint64_t)1) +static inline uint64_t fuse_iext_read_seq (struct fuse_iomap_cache *ip) {return (uint64_t )READ_ONCE (ip->im_seq);
fs/fuse/file_iomap.c +218 -5
298 lines not on this page · raw message
fs/fuse/iomap_cache.c +5 -1
@@ -660,7 +660,11 @@ fuse_iext_realloc_root(*/ static inline void fuse_iext_inc_seq (struct fuse_iomap_cache *ip) { -WRITE_ONCE (ip->im_seq,READ_ONCE (ip->im_seq) +1 ); +uint64_t new_val =READ_ONCE (ip->im_seq) +1 ; + +if (new_val ==FUSE_IOMAP_ALWAYS_VALID ) + new_val++; +WRITE_ONCE (ip->im_seq, new_val); }void
Re: [PATCH 03/10] fuse: use the iomap cache for iomap_begin
Quoted 4 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Look inside the iomap cache to try to satisfy
iomap_begin.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit 705acf20a7890a2ba0ab24fe6bf523b99c1a1f32
Author: Darrick J. Wong <[email protected]>
fuse: use the iomap cache for iomap_begin
Look inside the iomap cache to try to satisfy iomap_begin.
Quoted 4 lines
fs/fuse/fuse_iomap.c +0
-0
[ ... ]
Quoted 15 lines
fs/fuse/fuse_iomap.c +6
-1@@ -238,9 +239,14 @@ static inline bool fuse_iomap_check_mapping(const struct inode *inode,const unsigned int blocksize =i_blocksize (inode);uint64_t end; -/* Type and flags must be known */ +/* +* Type and flags must be known. Mapping type "retry cache" doesn't +* use any of the other fields. +*/ if (BAD_DATA (!fuse_iomap_check_type (map->type)))return false; +if (map->type ==FUSE_IOMAP_TYPE_RETRY_CACHE ) +return true;if (BAD_DATA (!fuse_iomap_check_flags (map->flags)))return false;
[ ... ]
Quoted 5 lines
fs/fuse/fuse_iomap.c +1
-0@@ -271,6 +277,14 @@ static inline bool fuse_iomap_check_mapping(const struct inode *inode,if (BAD_DATA (map->addr ==FUSE_IOMAP_NULL_ADDR ))return false;break ; +case FUSE_IOMAP_TYPE_RETRY_CACHE :
fs/fuse/fuse_iomap.c static inline bool fuse_iomap_check_mapping(const struct inode *inode, · line 290
Quoted 10 lines
fs/fuse/fuse_iomap.c +7
-0+/* +* We only accept cache retries if we have a cache to query. +* There must not be a device addr. +*/ +if (BAD_DATA (!fuse_inode_caches_iomaps (inode))) +return false; + fallthrough;case FUSE_IOMAP_TYPE_DELALLOC :case FUSE_IOMAP_TYPE_HOLE :case FUSE_IOMAP_TYPE_INLINE :
Does the validation at lines 280-287 ever execute? Looking at fuse_iomap_check_mapping(), the early return at line 248-249 handles FUSE_IOMAP_TYPE_RETRY_CACHE:
if (map->type == FUSE_IOMAP_TYPE_RETRY_CACHE)
return true;This means the function returns before reaching the switch statement that contains the new RETRY_CACHE case. The validation check 'if (BAD_DATA(! fuse_inode_caches_iomaps(inode))) return false;' at lines 284-285 appears to be dead code because RETRY_CACHE mappings are accepted without verifying the inode has a cache, contradicting the comment "We only accept cache retries if we have a cache to query."
Either the fuse_inode_caches_iomaps check should move to the early return path like this:
if (map->type == FUSE_IOMAP_TYPE_RETRY_CACHE) {
if (BAD_DATA(!fuse_inode_caches_iomaps(inode)))
return false;
return true;
}or the early return should be removed to let RETRY_CACHE fall through to the switch statement validation.
Re: [PATCH 03/10] fuse: use the iomap cache for iomap_begin
Quoted 71 lines · Chris Mason
Quoted 4 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Look inside the iomap cache to try to satisfy
iomap_begin.Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit
705acf20a7890a2ba0ab24fe6bf523b99c1a1f32
Author: Darrick J. Wong <[email protected]>fuse: use the iomap cache for
iomap_beginLook inside the iomap cache to try to satisfy
iomap_begin.Quoted 4 lines
fs/fuse/fuse_iomap.c +0
-0[ ... ]
Quoted 15 lines
fs/fuse/fuse_iomap.c +6
-1@@ -238,9 +239,14 @@ static inline bool fuse_iomap_check_mapping(const struct inode *inode,const unsigned int blocksize =i_blocksize (inode);uint64_t end; -/* Type and flags must be known */ +/* +* Type and flags must be known. Mapping type "retry cache" doesn't +* use any of the other fields. +*/ if (BAD_DATA (!fuse_iomap_check_type (map->type)))return false; +if (map->type ==FUSE_IOMAP_TYPE_RETRY_CACHE ) +return true;if (BAD_DATA (!fuse_iomap_check_flags (map->flags)))return false;[ ... ]
Quoted 5 lines
fs/fuse/fuse_iomap.c +1
-0@@ -271,6 +277,14 @@ static inline bool fuse_iomap_check_mapping(const struct inode *inode,if (BAD_DATA (map->addr ==FUSE_IOMAP_NULL_ADDR ))return false;break ; +case FUSE_IOMAP_TYPE_RETRY_CACHE :fs/fuse/fuse_iomap.c static inline bool fuse_iomap_check_mapping(const struct inode *inode, · line 290
Quoted 10 lines
fs/fuse/fuse_iomap.c +7
-0+/* +* We only accept cache retries if we have a cache to query. +* There must not be a device addr. +*/ +if (BAD_DATA (!fuse_inode_caches_iomaps (inode))) +return false; + fallthrough;case FUSE_IOMAP_TYPE_DELALLOC :case FUSE_IOMAP_TYPE_HOLE :case FUSE_IOMAP_TYPE_INLINE :Does the validation at lines 280-287 ever execute? Looking at
fuse_iomap_check_mapping(), the early return at line 248-249 handlesFUSE_IOMAP_TYPE_RETRY_CACHE:
Quoted 2 lines
if (map->type == FUSE_IOMAP_TYPE_RETRY_CACHE)
return true;Indeed, this means we can't ever hit the RETRY_CACHE case in the switch statement.
Quoted 7 lines
This means the function returns before reaching the switch statement that contains the new
RETRY_CACHEcase. The validation check 'if (BAD_DATA(! fuse_inode_caches_iomaps(inode))) return false;' at lines 284-285 appears to be dead code becauseRETRY_CACHEmappings are accepted without verifying the inode has a cache, contradicting the comment "We only accept cache retries if we have a cache to query."
Quoted 8 lines
Either the
fuse_inode_caches_iomapscheck should move to the early return path like this:if (map->type == FUSE_IOMAP_TYPE_RETRY_CACHE) { if (BAD_DATA(!fuse_inode_caches_iomaps(inode))) return false; return true; }
That's the simplest resolution to the problem; I'll go with that.
Quoted 2 lines
or the early return should be removed to let
RETRY_CACHEfall through to the switch statement validation.
Nah, we definitely don't need to proceed with more validation. Good catch!
--D
PATCH 4/10 fuse_trace: use the iomap cache for iomap_begin
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 34 ++++++++++++++++++++++++++++++++++ fs/fuse/file_iomap.c | 7 ++++++- 2 files changed, 40 insertions(+), 1 deletion(-)
fs/fuse/fuse_trace.h +34 -0
@@ -401,6 +401,7 @@ struct fuse_iomap_lookup;#define FUSE_IOMAP_TYPE_STRINGS \ { FUSE_IOMAP_TYPE_PURE_OVERWRITE, "overwrite" }, \ + { FUSE_IOMAP_TYPE_RETRY_CACHE, "retry" }, \ { FUSE_IOMAP_TYPE_HOLE, "hole" }, \ { FUSE_IOMAP_TYPE_DELALLOC, "delalloc" }, \ { FUSE_IOMAP_TYPE_MAPPED, "mapped" }, \
@@ -1474,6 +1475,39 @@ TRACE_EVENT(fuse_iomap_cache_lookup_result,FUSE_IOMAP_MAP_PRINTK_ARGS (got), __entry->validity_cookie) ); + +TRACE_EVENT (fuse_iomap_invalid, + TP_PROTO(const struct inode *inode, const struct iomap *map, + uint64_t validity_cookie), +TP_ARGS (inode, map, validity_cookie), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS + FUSE_IOMAP_MAP_FIELDS(map) + __field(uint64_t, old_validity_cookie) +__field (uint64_t, validity_cookie) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + + __entry->mapoffset = map->offset; + __entry->maplength = map->length; + __entry->maptype = map->type; + __entry->mapflags = map->flags; + __entry->mapaddr = map->addr; + __entry->mapdev = FUSE_IOMAP_DEV_NULL; + + __entry->old_validity_cookie= map->validity_cookie; + __entry->validity_cookie= validity_cookie; + ), + + TP_printk(FUSE_INODE_FMT FUSE_IOMAP_MAP_FMT() " old_cookie 0x%llx new_cookie 0x%llx", +FUSE_INODE_PRINTK_ARGS , +FUSE_IOMAP_MAP_PRINTK_ARGS (map), + __entry->old_validity_cookie, + __entry->validity_cookie) +);#endif /* CONFIG_FUSE_IOMAP */ #endif /* _TRACE_FUSE_H */
fs/fuse/file_iomap.c +6 -1
@@ -1575,7 +1575,12 @@ static bool fuse_iomap_revalidate(struct inode *inode,return true; validity_cookie =fuse_iext_read_seq (&fi->cache); -return iomap->validity_cookie == validity_cookie; +if (unlikely (iomap->validity_cookie != validity_cookie)) { +trace_fuse_iomap_invalid (inode, iomap, validity_cookie); +return false; + } + +return true; }static const struct iomap_write_ops fuse_iomap_write_ops = {
PATCH 5/10 fuse: invalidate iomap cache after file updates
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
The kernel doesn't know what the fuse server might have done in response to truncate, fallocate, or ioend events. Therefore, it must invalidate the mapping cache after those operations to ensure cache coherency.
--- fs/fuse/fuse_i.h | 9 +++++++++ fs/fuse/iomap_i.h | 9 +++++++++ fs/fuse/dir.c | 6 ++++++ fs/fuse/file.c | 26 ++++++++++++++++---------- fs/fuse/file_iomap.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- fs/fuse/iomap_cache.c | 27 +++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 11 deletions(-)
fs/fuse/fuse_i.h +9 -0
@@ -1834,10 +1834,15 @@ int fuse_iomap_mmap(struct file *file, struct vm_area_struct *vma);ssize_t fuse_iomap_buffered_read (struct kiocb *iocb,struct iov_iter *to);ssize_t fuse_iomap_buffered_write (struct kiocb *iocb,struct iov_iter *from);int fuse_iomap_setsize_start (struct inode *inode,loff_t newsize); +int fuse_iomap_setsize_finish (struct inode *inode,loff_t newsize);int fuse_iomap_fallocate (struct file *file,int mode,loff_t offset,loff_t length,loff_t new_size);int fuse_iomap_flush_unmap_range (struct inode *inode,loff_t pos,loff_t endpos); +void fuse_iomap_open_truncate (struct inode *inode); +void fuse_iomap_release_truncate (struct inode *inode); +void fuse_iomap_copied_file_range (struct inode *inode,loff_t offset, +size_t written);int fuse_dev_ioctl_iomap_support (struct file *file,struct fuse_iomap_support __user *argp);
@@ -1879,8 +1884,12 @@ enum fuse_iomap_iodir {# define fuse_iomap_buffered_read (...) (-ENOSYS)# define fuse_iomap_buffered_write (...) (-ENOSYS)# define fuse_iomap_setsize_start (...) (-ENOSYS) +# define fuse_iomap_setsize_finish (...) (-ENOSYS)# define fuse_iomap_fallocate (...) (-ENOSYS)# define fuse_iomap_flush_unmap_range (...) (-ENOSYS) +# define fuse_iomap_open_truncate (...) ((void)0) +# define fuse_iomap_release_truncate (...) ((void)0) +# define fuse_iomap_copied_file_range (...) ((void)0)# define fuse_dev_ioctl_iomap_support (...) (-EOPNOTSUPP)# define fuse_iomap_dev_inval (...) (-ENOSYS)# define fuse_iomap_fadvise NULL
fs/fuse/iomap_i.h +9 -0
@@ -177,6 +177,15 @@ fuse_iomap_cache_lookup(struct inode *inode, enum fuse_iomap_iodir iodir, loff_t off, uint64_t len, struct fuse_iomap_lookup *mval); +int fuse_iomap_cache_invalidate_range (struct inode *inode,loff_t offset, +uint64_t length); +static inline int fuse_iomap_cache_invalidate (struct inode *inode, +loff_t offset) +{ +return fuse_iomap_cache_invalidate_range (inode, offset, +FUSE_IOMAP_INVAL_TO_EOF ); +} +#endif /* CONFIG_FUSE_IOMAP */ #endif /* _FS_FUSE_IOMAP_I_H */
fs/fuse/dir.c +6 -0
@@ -2201,6 +2201,12 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry, goto error; } +if (is_iomap && is_truncate) { + err =fuse_iomap_setsize_finish (inode, outarg.attr.size); +if (err) + goto error; + } +spin_lock (&fi->lock);/* the kernel maintains i_mtime locally */ if (trust_local_cmtime) {
fs/fuse/file.c +16 -10
@@ -100,7 +100,7 @@ static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args,kfree (ra); } -static void fuse_file_put (struct fuse_file *ff,bool sync) +static void fuse_file_put (struct fuse_file *ff,struct inode *inode,bool sync) {if (refcount_dec_and_test (&ff->count)) {struct fuse_release_args *ra = &ff->args->release_args;
@@ -109,6 +109,8 @@ static void fuse_file_put(struct fuse_file *ff, bool sync)if (ra && ra->inode)fuse_file_io_release (ff, ra->inode); +fuse_iomap_release_truncate (inode); +if (!args) {/* Do nothing when server does not implement 'open' */ }else if (sync) {
@@ -279,9 +281,11 @@ static int fuse_open(struct inode *inode, struct file *file)if ((is_wb_truncate || dax_truncate) && !is_iomap)fuse_release_nowrite (inode);if (!err) { -if (is_truncate) +if (is_truncate) {truncate_pagecache (inode,0 ); -else if (!(ff->open_flags &FOPEN_KEEP_CACHE )) +if (is_iomap) +fuse_iomap_open_truncate (inode); + }else if (!(ff->open_flags &FOPEN_KEEP_CACHE ))invalidate_inode_pages2 (inode->i_mapping); }if (dax_truncate)
@@ -367,7 +371,7 @@ void fuse_file_release(struct inode *inode, struct fuse_file *ff,* own ref to the file, the IO completion has to drop the ref, which is * how the fuse server can end up closing its clients' files. */ -fuse_file_put (ff, false); +fuse_file_put (ff, &fi->inode, false); }void fuse_release_common (struct file *file,bool isdir)
@@ -398,7 +402,7 @@ void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, {WARN_ON (refcount_read (&ff->count) >1 );fuse_prepare_release (fi, ff, flags,FUSE_RELEASE , true); -fuse_file_put (ff, true); +fuse_file_put (ff, &fi->inode, true); }EXPORT_SYMBOL_GPL (fuse_sync_release);
@@ -903,7 +907,7 @@ static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,folio_put (ap->folios[i]); }if (ia->ff) -fuse_file_put (ia->ff, false); +fuse_file_put (ia->ff, inode, false);fuse_io_free (ia); }
@@ -1864,7 +1868,7 @@ static void fuse_writepage_free(struct fuse_writepage_args *wpa)if (wpa->bucket)fuse_sync_bucket_dec (wpa->bucket); -fuse_file_put (wpa->ia.ff, false); +fuse_file_put (wpa->ia.ff, wpa->inode, false);kfree (ap->folios);kfree (wpa);
@@ -2020,7 +2024,7 @@ int fuse_write_inode(struct inode *inode, struct writeback_control *wbc) ff =__fuse_write_file_get (fi); err =fuse_flush_times (inode, ff);if (ff) -fuse_file_put (ff, false); +fuse_file_put (ff, inode, false);return err; }
@@ -2238,7 +2242,7 @@ static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc, }if (data->ff) -fuse_file_put (data->ff, false); +fuse_file_put (data->ff, wpc->inode, false);return error; }
@@ -3150,7 +3154,9 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in, goto out; } -if (!is_iomap) +if (is_iomap) +fuse_iomap_copied_file_range (inode_out, pos_out, outarg.size); +else truncate_inode_pages_range (inode_out->i_mapping,ALIGN_DOWN (pos_out,PAGE_SIZE ),ALIGN (pos_out + bytes_copied,PAGE_SIZE ) -1 );
fs/fuse/file_iomap.c +48 -1
84 lines not on this page · raw message
fs/fuse/iomap_cache.c +27 -0
@@ -1412,6 +1412,33 @@ fuse_iomap_cache_remove(return ret; } +int fuse_iomap_cache_invalidate_range (struct inode *inode,loff_t offset, +uint64_t length) +{ +loff_t aligned_offset; +const unsigned int blocksize =i_blocksize (inode); +int ret, ret2; + +if (!fuse_inode_caches_iomaps (inode)) +return 0 ; + + aligned_offset =round_down (offset, blocksize); +if (length !=FUSE_IOMAP_INVAL_TO_EOF ) { + length += offset - aligned_offset; + length =round_up (length, blocksize); + } + +fuse_iomap_cache_lock (inode); + ret =fuse_iomap_cache_remove (inode,READ_MAPPING , + aligned_offset, length); + ret2 =fuse_iomap_cache_remove (inode,WRITE_MAPPING , + aligned_offset, length); +fuse_iomap_cache_unlock (inode); +if (ret) +return ret; +return ret2; +} + static void fuse_iext_add_mapping( struct fuse_iomap_cache *ip,
Re: [PATCH 05/10] fuse: invalidate iomap cache after file updates
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
The kernel doesn't know what the fuse server might have done in response to truncate, fallocate, or ioend events. Therefore, it must invalidate the mapping cache after those operations to ensure cache coherency.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit 8cd55ddf79a7cd6b1c8afbe08b72d8896da67306
Author: Darrick J. Wong <[email protected]>
fuse: invalidate iomap cache after file updates
This commit fixes a cache coherency bug by adding cache invalidation calls after file-modifying operations to prevent stale cached iomap entries from being used.
Quoted 3 lines
fs/fuse/file.c +0
-0
[ ... ]
fs/fuse/file.c struct fuse_copy_file_range_in inarg = { · line 3209
Quoted 7 lines
fs/fuse/file.c +2
-1@@ -3202,7 +3205,9 @@ struct fuse_copy_file_range_in inarg = { goto out; } -if (!is_iomap) +if (is_iomap) +fuse_iomap_copied_file_range (inode_out, pos_out, outarg.size);
In __fuse_copy_file_range(), does this code pass the correct value to fuse_iomap_copied_file_range()?
When using the FUSE_COPY_FILE_RANGE_64 API (fc->no_copy_file_range_64 is false), the response goes into 'outarg_64', not 'outarg'. The 'outarg' struct remains uninitialized in this path.
Looking at the code earlier in the function:
if (fc->no_copy_file_range_64) {
args.out_args[0].size = sizeof(outarg);
args.out_args[0].value = &outarg;
} else {
args.out_args[0].size = sizeof(outarg_64);
args.out_args[0].value = &outarg_64;
}
/* ... */
if (args.out_args[0].value == &outarg_64)
bytes_copied = outarg_64.size;
else
bytes_copied = outarg.size;The 'bytes_copied' variable correctly handles both API versions. The else branch (non-iomap path) already uses 'bytes_copied' for truncate_inode_pages_range().
When the 64-bit API succeeds, could the cache invalidation use garbage/zero data for the range instead of the actual bytes copied, potentially leaving stale mappings in the cache?
Quoted 4 lines
fs/fuse/file.c +1
-0+else truncate_inode_pages_range (inode_out->i_mapping,ALIGN_DOWN (pos_out,PAGE_SIZE ),ALIGN (pos_out + bytes_copied,PAGE_SIZE ) -1 );
Re: [PATCH 05/10] fuse: invalidate iomap cache after file updates
Quoted 68 lines · Chris Mason
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
The kernel doesn't know what the fuse server might have done in response to truncate, fallocate, or ioend events. Therefore, it must invalidate the mapping cache after those operations to ensure cache coherency.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit
8cd55ddf79a7cd6b1c8afbe08b72d8896da67306
Author: Darrick J. Wong <[email protected]>fuse: invalidate iomap cache after file updates
This commit fixes a cache coherency bug by adding cache invalidation calls after file-modifying operations to prevent stale cached iomap entries from being used.
Quoted 3 lines
fs/fuse/file.c +0
-0[ ... ]
fs/fuse/file.c struct fuse_copy_file_range_in inarg = { · line 3209
Quoted 7 lines
fs/fuse/file.c +2
-1@@ -3202,7 +3205,9 @@ struct fuse_copy_file_range_in inarg = { goto out; } -if (!is_iomap) +if (is_iomap) +fuse_iomap_copied_file_range (inode_out, pos_out, outarg.size);In
__fuse_copy_file_range(), does this code pass the correct value tofuse_iomap_copied_file_range()?When using the
FUSE_COPY_FILE_RANGE_64API (fc->no_copy_file_range_64is false), the response goes into 'outarg_64', not 'outarg'. The 'outarg' struct remains uninitialized in this path.Looking at the code earlier in the function:
if (fc->no_copy_file_range_64) { args.out_args[0].size = sizeof(outarg); args.out_args[0].value = &outarg; } else { args.out_args[0].size = sizeof(outarg_64); args.out_args[0].value = &outarg_64; } /* ... */ if (args.out_args[0].value == &outarg_64) bytes_copied = outarg_64.size; else bytes_copied = outarg.size;
Quoted 7 lines
The '
bytes_copied' variable correctly handles both API versions. The else branch (non-iomap path) already uses 'bytes_copied' fortruncate_inode_pages_range().When the 64-bit API succeeds, could the cache invalidation use garbage/zero data for the range instead of the actual bytes copied, potentially leaving stale mappings in the cache?
Yes, fuse_iomap_copied_file_range should have its @written parameter changed to u64. I forgot to make that amendment after FUSE_COPY_FILE_RANGE_64 was introduced last cycle. It is a very good sign that your AI prompts can catch bitrot during development!
--D
Quoted 7 lines
Quoted 5 lines
fs/fuse/file.c +1
-0+else truncate_inode_pages_range (inode_out->i_mapping,ALIGN_DOWN (pos_out,PAGE_SIZE ),ALIGN (pos_out + bytes_copied,PAGE_SIZE ) -1 );
PATCH 6/10 fuse_trace: invalidate iomap cache after file updates
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 43 +++++++++++++++++++++++++++++++++++++++++++ fs/fuse/file_iomap.c | 6 ++++++ fs/fuse/iomap_cache.c | 2 ++ 3 files changed, 51 insertions(+)
fs/fuse/fuse_trace.h +43 -0
@@ -1076,6 +1076,7 @@ DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT(fuse_iomap_truncate_down);DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT (fuse_iomap_punch_range);DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT (fuse_iomap_setsize);DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT (fuse_iomap_flush_unmap_range); +DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT (fuse_iomap_cache_invalidate_range); TRACE_EVENT(fuse_iomap_fallocate, TP_PROTO(const struct inode *inode, int mode, loff_t offset,
@@ -1213,6 +1214,48 @@ DEFINE_FUSE_IOMAP_INLINE_EVENT(fuse_iomap_inline_write);DEFINE_FUSE_IOMAP_INLINE_EVENT (fuse_iomap_set_inline_iomap);DEFINE_FUSE_IOMAP_INLINE_EVENT (fuse_iomap_set_inline_srcmap); +DECLARE_EVENT_CLASS (fuse_iomap_inode_class, + TP_PROTO(const struct inode *inode), + +TP_ARGS (inode), + +TP_STRUCT__entry ( +FUSE_INODE_FIELDS + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + ), + +TP_printk (FUSE_INODE_FMT , +FUSE_INODE_PRINTK_ARGS ) +); +#define DEFINE_FUSE_IOMAP_INODE_EVENT (name) \ +DEFINE_EVENT(fuse_iomap_inode_class, name, \ + TP_PROTO(const struct inode *inode), \ + TP_ARGS(inode)) +DEFINE_FUSE_IOMAP_INODE_EVENT (fuse_iomap_open_truncate); +DEFINE_FUSE_IOMAP_INODE_EVENT (fuse_iomap_release_truncate); + +TRACE_EVENT (fuse_iomap_copied_file_range, + TP_PROTO(const struct inode *inode, loff_t offset, + size_t written), +TP_ARGS (inode, offset, written), + +TP_STRUCT__entry ( +FUSE_IO_RANGE_FIELDS () + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->offset = offset; + __entry->length = written; + ), + +TP_printk (FUSE_IO_RANGE_FMT (), +FUSE_IO_RANGE_PRINTK_ARGS ()) +); + DECLARE_EVENT_CLASS(fuse_iext_class, TP_PROTO(const struct inode *inode, const struct fuse_iext_cursor *cur, int state, unsigned long caller_ip),
fs/fuse/file_iomap.c +6 -0
@@ -2503,6 +2503,8 @@ void fuse_iomap_open_truncate(struct inode *inode) {ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_open_truncate (inode); +fuse_iomap_cache_invalidate (inode,0 ); }
@@ -2510,6 +2512,8 @@ void fuse_iomap_release_truncate(struct inode *inode) {ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_release_truncate (inode); +fuse_iomap_cache_invalidate (inode,0 ); }
@@ -2518,5 +2522,7 @@ void fuse_iomap_copied_file_range(struct inode *inode, loff_t offset, {ASSERT (fuse_inode_has_iomap (inode)); +trace_fuse_iomap_copied_file_range (inode, offset, written); +fuse_iomap_cache_invalidate_range (inode, offset, written); }
fs/fuse/iomap_cache.c +2 -0
@@ -1422,6 +1422,8 @@ int fuse_iomap_cache_invalidate_range(struct inode *inode, loff_t offset,if (!fuse_inode_caches_iomaps (inode))return 0 ; +trace_fuse_iomap_cache_invalidate_range (inode, offset, length); + aligned_offset =round_down (offset, blocksize);if (length !=FUSE_IOMAP_INVAL_TO_EOF ) { length += offset - aligned_offset;
PATCH 7/10 fuse: enable iomap cache management
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Provide a means for the fuse server to upload iomappings to the kernel and invalidate them. This is how we enable iomap caching for better performance. This is also required for correct synchronization between pagecache writes and writeback.
--- fs/fuse/fuse_i.h | 7 + include/uapi/linux/fuse.h | 28 +++++ fs/fuse/dev.c | 44 ++++++++ fs/fuse/file_iomap.c | 239 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 314 insertions(+), 4 deletions(-)
fs/fuse/fuse_i.h +7 -0
@@ -1862,6 +1862,11 @@ enum fuse_iomap_iodir {READ_MAPPING , WRITE_MAPPING, }; + +int fuse_iomap_upsert (struct fuse_conn *fc, +const struct fuse_iomap_upsert_out *outarg); +int fuse_iomap_inval (struct fuse_conn *fc, +const struct fuse_iomap_inval_out *outarg);#else # define fuse_iomap_enabled (...) (false)# define fuse_has_iomap (...) (false)
@@ -1894,6 +1899,8 @@ enum fuse_iomap_iodir {# define fuse_iomap_dev_inval (...) (-ENOSYS)# define fuse_iomap_fadvise NULL# define fuse_inode_caches_iomaps (...) (false) +# define fuse_iomap_upsert (...) (-ENOSYS) +# define fuse_iomap_inval (...) (-ENOSYS)#endif #endif /* _FS_FUSE_I_H */
include/uapi/linux/fuse.h +28 -0
@@ -249,6 +249,8 @@* - add FUSE_ATTR_ATOMIC for single-fsblock atomic write support * - add FUSE_ATTR_{SYNC,IMMUTABLE,APPEND} for VFS enforcement of file * attributes +* - add FUSE_NOTIFY_IOMAP_UPSERT and FUSE_NOTIFY_IOMAP_INVAL so fuse servers +* can cache iomappings in the kernel */ #ifndef _LINUX_FUSE_H
@@ -726,6 +728,8 @@ enum fuse_notify_code {FUSE_NOTIFY_INC_EPOCH =8 ,FUSE_NOTIFY_PRUNE =9 ,FUSE_NOTIFY_IOMAP_DEV_INVAL =99 , +FUSE_NOTIFY_IOMAP_UPSERT =100 , +FUSE_NOTIFY_IOMAP_INVAL =101 , FUSE_NOTIFY_CODE_MAX, };
@@ -1390,6 +1394,8 @@ struct fuse_uring_cmd_req {#define FUSE_IOMAP_TYPE_PURE_OVERWRITE (255)/* fuse-specific mapping type saying the server has populated the cache */ #define FUSE_IOMAP_TYPE_RETRY_CACHE (254) +/* do not upsert this mapping */ +#define FUSE_IOMAP_TYPE_NOCACHE (253)#define FUSE_IOMAP_DEV_NULL (0U)/* null device cookie */
@@ -1540,4 +1546,26 @@ struct fuse_iomap_dev_inval_out {/* invalidate all cached iomap mappings up to EOF */ #define FUSE_IOMAP_INVAL_TO_EOF (~0ULL) +struct fuse_iomap_inval_out { +uint64_t nodeid;/* Inode ID */ +uint64_t attr_ino;/* matches fuse_attr:ino */ + +uint64_t read_offset;/* range to invalidate read iomaps, bytes */ +uint64_t read_length;/* can be FUSE_IOMAP_INVAL_TO_EOF */ + +uint64_t write_offset;/* range to invalidate write iomaps, bytes */ +uint64_t write_length;/* can be FUSE_IOMAP_INVAL_TO_EOF */ +}; + +struct fuse_iomap_upsert_out { +uint64_t nodeid;/* Inode ID */ +uint64_t attr_ino;/* matches fuse_attr:ino */ + +/* read file data from here */ +struct fuse_iomap_io read; + +/* write file data to here, if applicable */ +struct fuse_iomap_io write; +}; +#endif /* _LINUX_FUSE_H */
fs/fuse/dev.c +44 -0
56 lines not on this page · raw message
fs/fuse/file_iomap.c +235 -4
277 lines not on this page · raw message
Re: [PATCH 07/10] fuse: enable iomap cache management
Quoted 7 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Provide a means for the fuse server to upload iomappings to the kernel and invalidate them. This is how we enable iomap caching for better performance. This is also required for correct synchronization between pagecache writes and writeback.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit 84c726dd312b652c0fd2d4f43d65eefd16bfa901
Author: Darrick J. Wong <[email protected]>
fuse: enable iomap cache management
This commit provides a means for the fuse server to upload iomappings to the kernel and invalidate them, enabling iomap caching for better performance and ensuring correct synchronization between pagecache writes and writeback.
Quoted 4 lines
fs/fuse/fuse_iomap.c +0
-0
[ ... ]
Quoted 41 lines
fs/fuse/fuse_iomap.c +41
-0+int fuse_iomap_inval (struct fuse_conn *fc, +const struct fuse_iomap_inval_out *outarg) +{ +struct inode *inode; +struct fuse_inode *fi; +int ret =0 , ret2 =0 ; + +if (!fc->iomap) +return -EINVAL ; + +down_read (&fc->killsb); + inode =fuse_ilookup (fc, outarg->nodeid,NULL ); +if (!inode) { + ret = -ESTALE ; + goto out_sb; + } + + fi =get_fuse_inode (inode); +if (BAD_DATA (fi->orig_ino != outarg->attr_ino)) { + ret = -EINVAL ; + goto out_inode; + } + +if (fuse_is_bad (inode)) { + ret = -EIO ; + goto out_inode; + } + +if (!fuse_iomap_inval_validate (inode, outarg->write_offset, + outarg->write_length)) { + ret = -EFSCORRUPTED ; + goto out_inode; + } + +if (!fuse_iomap_inval_validate (inode, outarg->read_offset, + outarg->read_length)) { + ret = -EFSCORRUPTED ; + goto out_inode; + } + +fuse_iomap_cache_lock (inode);
fs/fuse/fuse_iomap.c
Quoted 10 lines
fs/fuse/fuse_iomap.c +9
-0+if (outarg->read_length) + ret2 =fuse_iomap_cache_remove (inode,READ_MAPPING , + outarg->read_offset, + outarg->read_length); +if (outarg->write_length) + ret =fuse_iomap_cache_remove (inode,WRITE_MAPPING , + outarg->write_offset, + outarg->write_length); +fuse_iomap_cache_unlock (inode); ^^^^
Can fuse_iomap_cache_lock() dereference a NULL pointer here? Looking at fuse_iomap_upsert() above, there's a check for fuse_inode_caches_iomaps() before accessing the cache and allocating if necessary. In fuse_iomap_inval(), there's no such check before calling fuse_iomap_cache_lock().
fuse_iomap_cache_lock() accesses fi->cache and calls down_write(&ic->ic_lock), which will crash if fi->cache is NULL. This could happen when a fuse server sends FUSE_NOTIFY_IOMAP_INVAL for an inode that never had mappings upserted to it.
Should fuse_iomap_inval() check fuse_inode_caches_iomaps(inode) before calling fuse_iomap_cache_lock()? If the cache doesn't exist, there's nothing to invalidate, so returning early (perhaps with 0 or -EINVAL) would be appropriate.
Re: [PATCH 07/10] fuse: enable iomap cache management
Quoted 84 lines · Chris Mason
Quoted 7 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Provide a means for the fuse server to upload iomappings to the kernel and invalidate them. This is how we enable iomap caching for better performance. This is also required for correct synchronization between pagecache writes and writeback.
Hi everyone,
I'm trying out my AI review prompts on a few more trees, and I ran it on the fuse-iomap-cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
This was the latest email lore could find, apologies if I grabbed the wrong one.
Hopefully the review below is useful, please let me know if there are places it has gone wildly wrong. I kept the header to make it obvious exactly which commit I'd run through:
commit
84c726dd312b652c0fd2d4f43d65eefd16bfa901
Author: Darrick J. Wong <[email protected]>fuse: enable iomap cache management
This commit provides a means for the fuse server to upload iomappings to the kernel and invalidate them, enabling iomap caching for better performance and ensuring correct synchronization between pagecache writes and writeback.
Quoted 4 lines
fs/fuse/fuse_iomap.c +0
-0[ ... ]
Quoted 45 lines
fs/fuse/fuse_iomap.c +45
-0+int fuse_iomap_inval (struct fuse_conn *fc, +const struct fuse_iomap_inval_out *outarg) +{ +struct inode *inode; +struct fuse_inode *fi; +int ret =0 , ret2 =0 ; + +if (!fc->iomap) +return -EINVAL ; + +down_read (&fc->killsb); + inode =fuse_ilookup (fc, outarg->nodeid,NULL ); +if (!inode) { + ret = -ESTALE ; + goto out_sb; + } + + fi =get_fuse_inode (inode); +if (BAD_DATA (fi->orig_ino != outarg->attr_ino)) { + ret = -EINVAL ; + goto out_inode; + } + +if (fuse_is_bad (inode)) { + ret = -EIO ; + goto out_inode; + } + +if (!fuse_iomap_inval_validate (inode, outarg->write_offset, + outarg->write_length)) { + ret = -EFSCORRUPTED ; + goto out_inode; + } + +if (!fuse_iomap_inval_validate (inode, outarg->read_offset, + outarg->read_length)) { + ret = -EFSCORRUPTED ; + goto out_inode; + } + +fuse_iomap_cache_lock (inode); +if (outarg->read_length) + ret2 =fuse_iomap_cache_remove (inode,READ_MAPPING , + outarg->read_offset, + outarg->read_length);
Quoted 8 lines
fs/fuse/fuse_iomap.c
Quoted 6 lines
fs/fuse/fuse_iomap.c +5
-0+if (outarg->write_length) + ret =fuse_iomap_cache_remove (inode,WRITE_MAPPING , + outarg->write_offset, + outarg->write_length); +fuse_iomap_cache_unlock (inode); ^^^^Can
fuse_iomap_cache_lock()dereference a NULL pointer here? Looking at
Yes. For those of you reading the parent message you might be confused because the 29 Oct posting embedded the iomap cache directly in struct fuse_inode so there was no possibility of a NULL pointer dereference.
Based on feedback from Joanne I changed fuse_inode to point to a fuse_iomap_cache object, which the AI has now caught. Excellent!
Quoted 9 lines
fuse_iomap_upsert()above, there's a check forfuse_inode_caches_iomaps()before accessing the cache and allocating if necessary. Infuse_iomap_inval(), there's no such check before callingfuse_iomap_cache_lock().
fuse_iomap_cache_lock()accessesfi->cacheand callsdown_write(&ic->ic_lock), which will crash iffi->cacheis NULL. This could happen when a fuse server sendsFUSE_NOTIFY_IOMAP_INVALfor an inode that never had mappings upserted to it.
Quoted 4 lines
Should
fuse_iomap_inval()checkfuse_inode_caches_iomaps(inode)before callingfuse_iomap_cache_lock()? If the cache doesn't exist, there's nothing to invalidate, so returning early (perhaps with 0 or-EINVAL) would be appropriate.
I'll have it return 0 because no cache means cache invalidation is a nop. Thanks for your review help, Chris!
--D
PATCH 8/10 fuse_trace: enable iomap cache management
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Add tracepoints for the previous patch.
--- fs/fuse/fuse_trace.h | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/file_iomap.c | 7 ++++- 2 files changed, 74 insertions(+), 1 deletion(-)
fs/fuse/fuse_trace.h +68 -0
@@ -402,6 +402,7 @@ struct fuse_iomap_lookup;#define FUSE_IOMAP_TYPE_STRINGS \ { FUSE_IOMAP_TYPE_PURE_OVERWRITE, "overwrite" }, \ { FUSE_IOMAP_TYPE_RETRY_CACHE, "retry" }, \ + { FUSE_IOMAP_TYPE_NOCACHE, "nocache" }, \ { FUSE_IOMAP_TYPE_HOLE, "hole" }, \ { FUSE_IOMAP_TYPE_DELALLOC, "delalloc" }, \ { FUSE_IOMAP_TYPE_MAPPED, "mapped" }, \
@@ -745,6 +746,7 @@ DEFINE_EVENT(fuse_inode_state_class, name, \ TP_ARGS(inode))DEFINE_FUSE_INODE_STATE_EVENT (fuse_iomap_init_inode);DEFINE_FUSE_INODE_STATE_EVENT (fuse_iomap_evict_inode); +DEFINE_FUSE_INODE_STATE_EVENT (fuse_iomap_cache_enable); TRACE_EVENT(fuse_iomap_fiemap, TP_PROTO(const struct inode *inode, u64 start, u64 count,
@@ -1551,6 +1553,72 @@ TRACE_EVENT(fuse_iomap_invalid, __entry->old_validity_cookie, __entry->validity_cookie) ); + +TRACE_EVENT (fuse_iomap_upsert, + TP_PROTO(const struct inode *inode, + const struct fuse_iomap_upsert_out *outarg), +TP_ARGS (inode, outarg), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS + __field(uint64_t, attr_ino) + + FUSE_IOMAP_MAP_FIELDS(read) +FUSE_IOMAP_MAP_FIELDS (write) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->attr_ino = outarg->attr_ino; + __entry->readoffset = outarg->read.offset; + __entry->readlength = outarg->read.length; + __entry->readaddr = outarg->read.addr; + __entry->readtype = outarg->read.type; + __entry->readflags = outarg->read.flags; + __entry->readdev = outarg->read.dev; + __entry->writeoffset = outarg->write.offset; + __entry->writelength = outarg->write.length; + __entry->writeaddr = outarg->write.addr; + __entry->writetype = outarg->write.type; + __entry->writeflags = outarg->write.flags; + __entry->writedev = outarg->write.dev; + ), + + TP_printk(FUSE_INODE_FMT " attr_ino 0x%llx" FUSE_IOMAP_MAP_FMT("read") FUSE_IOMAP_MAP_FMT("write"), +FUSE_INODE_PRINTK_ARGS , + __entry->attr_ino, +FUSE_IOMAP_MAP_PRINTK_ARGS (read), +FUSE_IOMAP_MAP_PRINTK_ARGS (write)) +); + +TRACE_EVENT (fuse_iomap_inval, + TP_PROTO(const struct inode *inode, + const struct fuse_iomap_inval_out *outarg), +TP_ARGS (inode, outarg), + +TP_STRUCT__entry ( + FUSE_INODE_FIELDS + __field(uint64_t, attr_ino) + + FUSE_FILE_RANGE_FIELDS(read) +FUSE_FILE_RANGE_FIELDS (write) + ), + +TP_fast_assign ( + FUSE_INODE_ASSIGN(inode, fi, fm); + __entry->attr_ino = outarg->attr_ino; + __entry->readoffset = outarg->read_offset; + __entry->readlength = outarg->read_length; + __entry->writeoffset = outarg->write_offset; + __entry->writelength = outarg->write_length; + ), + + TP_printk(FUSE_INODE_FMT " attr_ino 0x%llx" FUSE_FILE_RANGE_FMT("read") FUSE_FILE_RANGE_FMT("write"), +FUSE_INODE_PRINTK_ARGS , + __entry->attr_ino, +FUSE_FILE_RANGE_PRINTK_ARGS (read), +FUSE_FILE_RANGE_PRINTK_ARGS (write)) +);#endif /* CONFIG_FUSE_IOMAP */ #endif /* _TRACE_FUSE_H */
fs/fuse/file_iomap.c +6 -1
@@ -2636,6 +2636,8 @@ int fuse_iomap_upsert(struct fuse_conn *fc, goto out_sb; } +trace_fuse_iomap_upsert (inode, outarg); + fi =get_fuse_inode (inode);if (BAD_DATA (fi->orig_ino != outarg->attr_ino)) { ret = -EINVAL ;
@@ -2653,7 +2655,8 @@ int fuse_iomap_upsert(struct fuse_conn *fc,fuse_iomap_cache_lock (inode); -set_bit (FUSE_I_IOMAP_CACHE , &fi->state); +if (!test_and_set_bit (FUSE_I_IOMAP_CACHE , &fi->state)) +trace_fuse_iomap_cache_enable (inode);if (outarg->read.type !=FUSE_IOMAP_TYPE_NOCACHE ) { ret = fuse_iomap_cache_upsert(inode, READ_MAPPING,
@@ -2717,6 +2720,8 @@ int fuse_iomap_inval(struct fuse_conn *fc, goto out_sb; } +trace_fuse_iomap_inval (inode, outarg); + fi =get_fuse_inode (inode);if (BAD_DATA (fi->orig_ino != outarg->attr_ino)) { ret = -EINVAL ;