[PATCH 1/2] fuse2fs: enable caching of iomaps

Darrick J. Wong
2025-08-21T01:20:55Z

Cache the iomaps we generate in the kernel for better performance.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
misc/fuse2fs.c | 23 +++++++++++++++++++++++
misc/fuse4fs.c | 24 ++++++++++++++++++++++++
2 files changed, 47 insertions(+)

diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index 04bb96f3438f23..da384b10bc6bc5 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -284,6 +284,7 @@ struct fuse2fs {
#ifdef STATX_WRITE_ATOMIC
unsigned int awu_min, awu_max;
#endif
+ uint8_t iomap_cache;
#endif
unsigned int blockmask;
unsigned long offset;
@@ -5900,6 +5901,23 @@ static int op_iomap_begin(const char *path, uint64_t nodeid, uint64_t attr_ino,
if (opflags & FUSE_IOMAP_OP_ATOMIC)
read->flags |= FUSE_IOMAP_F_ATOMIC_BIO;

+ /*
+ * Cache the mapping in the kernel so that we can reuse them for
+ * subsequent IO.
+ */
+ if (ff->iomap_cache) {
+ ret = fuse_fs_iomap_upsert(nodeid, attr_ino, read, NULL);
+ if (ret) {
+ ret = translate_error(fs, attr_ino, -ret);
+ goto out_unlock;
+ } else {
+ /* Tell the kernel to retry from cache */
+ read->type = FUSE_IOMAP_TYPE_RETRY_CACHE;
+ read->dev = FUSE_IOMAP_DEV_NULL;
+ read->addr = FUSE_IOMAP_NULL_ADDR;
+ }
+ }
+
out_unlock:
fuse2fs_finish(ff, ret);
return ret;
@@ -6718,6 +6736,10 @@ static struct fuse_opt fuse2fs_opts[] = {
FUSE2FS_OPT("timing", timing, 1),
#endif
FUSE2FS_OPT("noblkdev", noblkdev, 1),
+#ifdef HAVE_FUSE_IOMAP
+ FUSE2FS_OPT("iomap_cache", iomap_cache, 1),
+ FUSE2FS_OPT("noiomap_cache", iomap_cache, 0),
+#endif

#ifdef HAVE_FUSE_IOMAP
#ifdef MS_LAZYTIME
@@ -6952,6 +6974,7 @@ int main(int argc, char *argv[])
.iomap_want = FT_DEFAULT,
.iomap_state = IOMAP_UNKNOWN,
.iomap_dev = FUSE_IOMAP_DEV_NULL,
+ .iomap_cache = 1,
#endif
};
errcode_t err;
diff --git a/misc/fuse4fs.c b/misc/fuse4fs.c
index 43fc21149ba564..a2601b5ca94970 100644
--- a/misc/fuse4fs.c
+++ b/misc/fuse4fs.c
@@ -280,6 +280,7 @@ struct fuse4fs {
#ifdef STATX_WRITE_ATOMIC
unsigned int awu_min, awu_max;
#endif
+ uint8_t iomap_cache;
#endif
unsigned int blockmask;
unsigned long offset;
@@ -6224,6 +6225,24 @@ static void op_iomap_begin(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
if (opflags & FUSE_IOMAP_OP_ATOMIC)
read.flags |= FUSE_IOMAP_F_ATOMIC_BIO;

+ /*
+ * Cache the mapping in the kernel so that we can reuse them for
+ * subsequent IO.
+ */
+ if (ff->iomap_cache) {
+ ret = fuse_lowlevel_notify_iomap_upsert(ff->fuse, fino, ino,
+ &read, NULL);
+ if (ret) {
+ ret = translate_error(fs, ino, -ret);
+ goto out_unlock;
+ } else {
+ /* Tell the kernel to retry from cache */
+ read.type = FUSE_IOMAP_TYPE_RETRY_CACHE;
+ read.dev = FUSE_IOMAP_DEV_NULL;
+ read.addr = FUSE_IOMAP_NULL_ADDR;
+ }
+ }
+
out_unlock:
fuse4fs_finish(ff, ret);
if (ret)
@@ -7029,6 +7048,10 @@ static struct fuse_opt fuse4fs_opts[] = {
FUSE4FS_OPT("timing", timing, 1),
#endif
FUSE4FS_OPT("noblkdev", noblkdev, 1),
+#ifdef HAVE_FUSE_IOMAP
+ FUSE4FS_OPT("iomap_cache", iomap_cache, 1),
+ FUSE4FS_OPT("noiomap_cache", iomap_cache, 0),
+#endif

#ifdef HAVE_FUSE_IOMAP
#ifdef MS_LAZYTIME
@@ -7362,6 +7385,7 @@ int main(int argc, char *argv[])
.iomap_want = FT_DEFAULT,
.iomap_state = IOMAP_UNKNOWN,
.iomap_dev = FUSE_IOMAP_DEV_NULL,
+ .iomap_cache = 1,
#endif
.translate_inums = 1,
};