[PATCH 13/18] libfuse: allow discovery of the kernel's iomap capabilities

Darrick J. Wong
2025-09-16T00:45:10Z

Create a library function so that we can discover the kernel's iomap
capabilities ahead of time.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
include/fuse_common.h | 7 +++++++
include/fuse_kernel.h | 7 +++++++
include/fuse_lowlevel.h | 10 ++++++++++
lib/fuse_lowlevel.c | 19 +++++++++++++++++++
lib/fuse_versionscript | 1 +
5 files changed, 44 insertions(+)

diff --git a/include/fuse_common.h b/include/fuse_common.h
index 191d9749960992..86ae8894d81dbb 100644
--- a/include/fuse_common.h
+++ b/include/fuse_common.h
@@ -534,6 +534,13 @@ struct fuse_loop_config_v1 {

#define FUSE_IOCTL_MAX_IOV 256

+/**
+ * iomap discovery flags
+ *
+ * FUSE_IOMAP_SUPPORT_FILEIO: basic file I/O functionality through iomap
+ */
+#define FUSE_IOMAP_SUPPORT_FILEIO (1ULL << 0)
+
/**
* Connection information, passed to the ->init() method
*
diff --git a/include/fuse_kernel.h b/include/fuse_kernel.h
index 48db6ba118a47c..ea4243d09db53f 100644
--- a/include/fuse_kernel.h
+++ b/include/fuse_kernel.h
@@ -1139,12 +1139,19 @@ struct fuse_backing_map {
uint64_t padding;
};

+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)
#define FUSE_DEV_IOC_BACKING_OPEN _IOW(FUSE_DEV_IOC_MAGIC, 1, \
struct fuse_backing_map)
#define FUSE_DEV_IOC_BACKING_CLOSE _IOW(FUSE_DEV_IOC_MAGIC, 2, uint32_t)
+#define FUSE_DEV_IOC_IOMAP_SUPPORT _IOR(FUSE_DEV_IOC_MAGIC, 99, \
+ struct fuse_iomap_support)

struct fuse_lseek_in {
uint64_t fh;
diff --git a/include/fuse_lowlevel.h b/include/fuse_lowlevel.h
index dfe28d33d8d0b8..a4117efdee6dea 100644
--- a/include/fuse_lowlevel.h
+++ b/include/fuse_lowlevel.h
@@ -2574,6 +2574,16 @@ bool fuse_req_is_uring(fuse_req_t req);
int fuse_req_get_payload(fuse_req_t req, char **payload, size_t *payload_sz,
void **mr);

+
+/**
+ * Discover the kernel's iomap capabilities. Returns FUSE_CAP_IOMAP_* flags.
+ *
+ * @param fd open file descriptor to a fuse device, or -1 if you're running
+ * in the same process that will call mount().
+ * @return FUSE_IOMAP_SUPPORT_* flags
+ */
+uint64_t fuse_lowlevel_discover_iomap(int fd);
+
#ifdef __cplusplus
}
#endif
diff --git a/lib/fuse_lowlevel.c b/lib/fuse_lowlevel.c
index 8654ca263b374d..a20f7fe3752ac5 100644
--- a/lib/fuse_lowlevel.c
+++ b/lib/fuse_lowlevel.c
@@ -4618,3 +4618,22 @@ int fuse_session_exited(struct fuse_session *se)

return exited ? 1 : 0;
}
+
+uint64_t fuse_lowlevel_discover_iomap(int fd)
+{
+ struct fuse_iomap_support ios = { };
+
+ if (fd >= 0) {
+ ioctl(fd, FUSE_DEV_IOC_IOMAP_SUPPORT, &ios);
+ return ios.flags;
+ }
+
+ fd = open("/dev/fuse", O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return 0;
+
+ ioctl(fd, FUSE_DEV_IOC_IOMAP_SUPPORT, &ios);
+ close(fd);
+
+ return ios.flags;
+}
diff --git a/lib/fuse_versionscript b/lib/fuse_versionscript
index 25a3e04c6c5ec7..704e8c2908ec4b 100644
--- a/lib/fuse_versionscript
+++ b/lib/fuse_versionscript
@@ -232,6 +232,7 @@ FUSE_3.99 {
fuse_add_direntry_plus_iflags;
fuse_fs_can_enable_iomap;
fuse_fs_can_enable_iomapx;
+ fuse_lowlevel_discover_iomap;
} FUSE_3.18;