[PATCH 14/17] fuse2fs: configure block device block size

Darrick J. Wong
2025-09-16T01:02:24Z

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>
---
fuse4fs/fuse4fs.c | 43 +++++++++++++++++++++++++++++++++++++++++++
misc/fuse2fs.c | 43 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 86 insertions(+)

diff --git a/fuse4fs/fuse4fs.c b/fuse4fs/fuse4fs.c
index 37999d864a05e5..6f3ddceea85c27 100644
--- a/fuse4fs/fuse4fs.c
+++ b/fuse4fs/fuse4fs.c
@@ -6247,6 +6247,45 @@ 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;
+ goto out_bad;
+ }
+
+ 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;
@@ -6257,6 +6296,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",
diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index 32fcada0426752..9212235495dc22 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -5777,6 +5777,45 @@ 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;
+ goto out_bad;
+ }
+
+ 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;
@@ -5787,6 +5826,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",