[PATCH 15/19] fuse2fs: configure block device block size

Darrick J. Wong
2025-08-21T01:19:37Z

Set the blocksize of the block device to the filesystem blocksize.
This prevents the bdev pagecache from caching file data blocks that
iomap will read and write directly. Cache duplication is dangerous.

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

diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index 9ac9077f3508f7..874fe3bbcc3b9f 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -5862,6 +5862,43 @@ static off_t fuse2fs_max_size(struct fuse2fs *ff, off_t upper_limit)
return res;
}

+/*
+ * Set the block device's blocksize to the fs blocksize.
+ *
+ * This is required to avoid creating uptodate bdev pagecache that aliases file
+ * data blocks because iomap reads and writes directly to file data blocks.
+ */
+static int fuse2fs_set_bdev_blocksize(struct fuse2fs *ff, int fd)
+{
+ int blocksize = ff->fs->blocksize;
+ int set_error;
+ int ret;
+
+ ret = ioctl(fd, BLKBSZSET, &blocksize);
+ if (!ret)
+ return 0;
+
+ /*
+ * Save the original errno so we can report that if the block device
+ * blocksize isn't set in an agreeable way.
+ */
+ set_error = errno;
+
+ ret = ioctl(fd, BLKBSZGET, &blocksize);
+ if (ret)
+ goto out_bad;
+
+ /* Pretend that BLKBSZSET rejected our proposed block size */
+ if (blocksize > ff->fs->blocksize)
+ set_error = EINVAL;
+
+ return 0;
+out_bad:
+ err_printf(ff, "%s: cannot set blocksize %u: %s\n", __func__,
+ blocksize, strerror(set_error));
+ return EIO;
+}
+
static int fuse2fs_iomap_config_devices(struct fuse2fs *ff)
{
errcode_t err;
@@ -5872,6 +5909,10 @@ static int fuse2fs_iomap_config_devices(struct fuse2fs *ff)
if (err)
return translate_error(ff->fs, 0, err);

+ ret = fuse2fs_set_bdev_blocksize(ff, fd);
+ if (ret)
+ return ret;
+
ret = fuse_fs_iomap_device_add(fd, 0);
if (ret < 0) {
dbg_printf(ff, "%s: cannot register iomap dev fd=%d, err=%d\n",
diff --git a/misc/fuse4fs.c b/misc/fuse4fs.c
index 1050238c88632d..304bac191e7c4c 100644
--- a/misc/fuse4fs.c
+++ b/misc/fuse4fs.c
@@ -6182,6 +6182,43 @@ static off_t fuse4fs_max_size(struct fuse4fs *ff, off_t upper_limit)
return res;
}

+/*
+ * Set the block device's blocksize to the fs blocksize.
+ *
+ * This is required to avoid creating uptodate bdev pagecache that aliases file
+ * data blocks because iomap reads and writes directly to file data blocks.
+ */
+static int fuse4fs_set_bdev_blocksize(struct fuse4fs *ff, int fd)
+{
+ int blocksize = ff->fs->blocksize;
+ int set_error;
+ int ret;
+
+ ret = ioctl(fd, BLKBSZSET, &blocksize);
+ if (!ret)
+ return 0;
+
+ /*
+ * Save the original errno so we can report that if the block device
+ * blocksize isn't set in an agreeable way.
+ */
+ set_error = errno;
+
+ ret = ioctl(fd, BLKBSZGET, &blocksize);
+ if (ret)
+ goto out_bad;
+
+ /* Pretend that BLKBSZSET rejected our proposed block size */
+ if (blocksize > ff->fs->blocksize)
+ set_error = EINVAL;
+
+ return 0;
+out_bad:
+ err_printf(ff, "%s: cannot set blocksize %u: %s\n", __func__,
+ blocksize, strerror(set_error));
+ return EIO;
+}
+
static int fuse4fs_iomap_config_devices(struct fuse4fs *ff)
{
errcode_t err;
@@ -6192,6 +6229,10 @@ static int fuse4fs_iomap_config_devices(struct fuse4fs *ff)
if (err)
return translate_error(ff->fs, 0, err);

+ ret = fuse4fs_set_bdev_blocksize(ff, fd);
+ if (ret)
+ return ret;
+
ret = fuse_lowlevel_iomap_device_add(ff->fuse, fd, 0);
if (ret < 0) {
dbg_printf(ff, "%s: cannot register iomap dev fd=%d, err=%d\n",