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

Darrick J. Wong
2026-04-29T15:01:29Z

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

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

diff --git a/fuse4fs/fuse4fs.c b/fuse4fs/fuse4fs.c
index 9f7fe365ccdf87..56a79dd48c9f43 100644
--- a/fuse4fs/fuse4fs.c
+++ b/fuse4fs/fuse4fs.c
@@ -304,6 +304,8 @@ struct fuse4fs {
#ifdef STATX_WRITE_ATOMIC
unsigned int awu_min, awu_max;
#endif
+ /* options set by fuse_opt_parse must be of type int */
+ int iomap_cache;
#endif
unsigned int blockmask;
unsigned long offset;
@@ -2314,6 +2316,7 @@ static void fuse4fs_iomap_enable(struct fuse_conn_info *conn,
err_printf(ff, "%s\n", _("Could not enable iomap."));
if (ff->iomap_passthrough_options)
err_printf(ff, "%s\n", _("Some mount options require iomap."));
+ ff->iomap_cache = 0;
return;
}
}
@@ -7169,6 +7172,28 @@ 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;

+ /*
+ * For real IO operations, cache the mapping in the kernel so that we
+ * can reuse them for subsequent IO to the same regions. Don't let
+ * FIEMAP thrash the cache.
+ */
+ if (!(opflags & FUSE_IOMAP_OP_REPORT) && ff->iomap_cache) {
+ ret = fuse_lowlevel_iomap_upsert_mappings(ff->fuse, fino, ino,
+ &read, NULL);
+ if (ret) {
+ /*
+ * Log the cache upsert error, but we can still return
+ * the mapping via the reply. EINVAL is the magic code
+ * for the kernel declining to cache the mapping.
+ */
+ if (ret != -ENOMEM && ret != -EINVAL)
+ translate_error(fs, ino, -ret);
+ goto out_unlock;
+ }
+
+ fuse_file_iomap_retry_cache(&read);
+ }
+
out_unlock:
fuse4fs_finish(ff, ret);
if (ret)
@@ -7993,6 +8018,10 @@ static struct fuse_opt fuse4fs_opts[] = {
#ifdef HAVE_CLOCK_MONOTONIC
FUSE4FS_OPT("timing", timing, 1),
#endif
+#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
@@ -8487,6 +8516,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
#ifdef HAVE_FUSE_LOOPDEV
.loop_fd = -1,
diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index 6e4780121d5c83..e485c38bda41e8 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -285,6 +285,8 @@ struct fuse2fs {
#ifdef STATX_WRITE_ATOMIC
unsigned int awu_min, awu_max;
#endif
+ /* options set by fuse_opt_parse must be of type int */
+ int iomap_cache;
#endif
unsigned int blockmask;
unsigned long offset;
@@ -1870,6 +1872,7 @@ static void fuse2fs_iomap_enable(struct fuse_conn_info *conn,
err_printf(ff, "%s\n", _("Could not enable iomap."));
if (ff->iomap_passthrough_options)
err_printf(ff, "%s\n", _("Some mount options require iomap."));
+ ff->iomap_cache = 0;
return;
}
}
@@ -6453,6 +6456,27 @@ 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;

+ /*
+ * For real IO operations, cache the mapping in the kernel so that we
+ * can reuse them for subsequent IO to the same regions. Don't let
+ * FIEMAP thrash the cache.
+ */
+ if (!(opflags & FUSE_IOMAP_OP_REPORT) && ff->iomap_cache) {
+ ret = fuse_fs_iomap_upsert(nodeid, attr_ino, read, NULL);
+ if (ret) {
+ /*
+ * Log the cache upsert error, but we can still return
+ * the mapping via the reply. EINVAL is the magic code
+ * for the kernel declining to cache the mapping.
+ */
+ if (ret != -ENOMEM && ret != -EINVAL)
+ translate_error(fs, attr_ino, -ret);
+ goto out_unlock;
+ }
+
+ fuse_file_iomap_retry_cache(read);
+ }
+
out_unlock:
fuse2fs_finish(ff, ret);
return ret;
@@ -7259,6 +7283,10 @@ static struct fuse_opt fuse2fs_opts[] = {
#ifdef HAVE_CLOCK_MONOTONIC
FUSE2FS_OPT("timing", timing, 1),
#endif
+#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
@@ -7539,6 +7567,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
#ifdef HAVE_FUSE_LOOPDEV
.loop_fd = -1,