* [PATCH 1/3] block/export/fuse: use struct fuse_init_in
2026-05-06 14:49 [PATCH 0/3] block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag to fix regression Fiona Ebner
@ 2026-05-06 14:49 ` Fiona Ebner
2026-05-06 14:49 ` [PATCH 2/3] block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag to fix regression Fiona Ebner
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Fiona Ebner @ 2026-05-06 14:49 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-block, hreitz, kwolf, qemu-stable
The code is switched to use the current 'struct fuse_init_in' in
preparation to use the FUSE_DIRECT_IO_ALLOW_MMAP feature, which is
part of the flags2 member that got added in protocol version 5.36.
To not break compatibility with older kernels, the check for whether
the full header of an operation was read in co_read_from_fuse_fd()
needs to be adapted. In particular, for a FUSE_INIT operation, the
protocol version must be considered, because the length of the header
changed with protocol version 7.36. Always using the length of the
old, shorter struct was inaccurate, since for newer protocol versions
this might mean accepting a truncated read for FUSE_INIT.
Users of the init header that want to use parts of the extended
structure must check with the using_old_fuse_init_in() helper function
if they may do so.
Cc: qemu-stable@nongnu.org
Fixes: a94a1d7699 ("fuse: Manually process requests (without libfuse)")
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
block/export/fuse.c | 56 +++++++++++++++++++++++++++++++++------------
1 file changed, 42 insertions(+), 14 deletions(-)
diff --git a/block/export/fuse.c b/block/export/fuse.c
index a2a478d293..35218e3197 100644
--- a/block/export/fuse.c
+++ b/block/export/fuse.c
@@ -51,23 +51,16 @@
#define FUSE_MAX_READ_BYTES (MIN(BDRV_REQUEST_MAX_BYTES, 1 * 1024 * 1024))
#define FUSE_MAX_WRITE_BYTES (64 * 1024)
-/*
- * fuse_init_in structure before 7.36. We don't need the flags2 field added
- * there, so we can work with the smaller older structure to stay compatible
- * with older kernels.
- */
-struct fuse_init_in_compat {
- uint32_t major;
- uint32_t minor;
- uint32_t max_readahead;
- uint32_t flags;
-};
-
typedef struct FuseRequestInHeader {
struct fuse_in_header common;
/* All supported requests */
union {
- struct fuse_init_in_compat init;
+ /*
+ * When using_old_fuse_init_in() is true, then the smaller older struct
+ * is used by the kernel. The flags2 member and other new members must
+ * be treated as absent then.
+ */
+ struct fuse_init_in init;
struct fuse_open_in open;
struct fuse_setattr_in setattr;
struct fuse_read_in read;
@@ -629,6 +622,16 @@ static int clone_fuse_fd(int fd, Error **errp)
return new_fd;
}
+/**
+ * Check whether the smaller older fuse_init_in structure from before protocol
+ * version 7.36 is used. The flags2 member and other new members must be treated
+ * as absent then.
+ */
+static bool using_old_fuse_init_in(const struct fuse_init_in *in)
+{
+ return in->major < 7 || (in->major == 7 && in->minor < 36);
+}
+
/**
* Try to read a single request from the FUSE FD.
* Takes a FuseQueue pointer in `opaque`.
@@ -693,6 +696,31 @@ static void coroutine_fn co_read_from_fuse_fd(void *opaque)
goto no_request;
}
+ /*
+ * If the request is of type FUSE_INIT, need to check the version to
+ * actually determine the length of the fuse_init_in structure used by the
+ * kernel. In protocol version 7.36, the structure was extended.
+ */
+ if (in_hdr->common.opcode == FUSE_INIT) {
+ /* Length of the fuse_init_in structure before 7.36. */
+ size_t old_init_hdr_len = 16;
+
+ /*
+ * Expect at least the size of the smaller older structure to ensure the
+ * version can be checked.
+ */
+ if (unlikely(ret < sizeof(in_hdr->common) + old_init_hdr_len)) {
+ error_report("FUSE_INIT request truncated, read only %zi bytes",
+ ret);
+ fuse_write_err(fuse_fd, &in_hdr->common, -EINVAL);
+ goto no_request;
+ }
+
+ if (using_old_fuse_init_in(&in_hdr->init)) {
+ op_hdr_len = old_init_hdr_len;
+ }
+ }
+
if (unlikely(ret < sizeof(in_hdr->common) + op_hdr_len)) {
error_report("FUSE request truncated, expected %zu bytes, read %zi "
"bytes",
@@ -826,7 +854,7 @@ static bool is_regular_file(const char *path, Error **errp)
*/
static ssize_t coroutine_fn GRAPH_RDLOCK
fuse_co_init(FuseExport *exp, struct fuse_init_out *out,
- const struct fuse_init_in_compat *in)
+ const struct fuse_init_in *in)
{
const uint32_t supported_flags = FUSE_ASYNC_READ | FUSE_ASYNC_DIO;
--
2.47.3
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 2/3] block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag to fix regression
2026-05-06 14:49 [PATCH 0/3] block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag to fix regression Fiona Ebner
2026-05-06 14:49 ` [PATCH 1/3] block/export/fuse: use struct fuse_init_in Fiona Ebner
@ 2026-05-06 14:49 ` Fiona Ebner
2026-05-06 14:49 ` [PATCH 3/3] iotests: test shared mmap for fuse export Fiona Ebner
2026-06-05 15:33 ` [PATCH 0/3] block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag to fix regression Kevin Wolf
3 siblings, 0 replies; 5+ messages in thread
From: Fiona Ebner @ 2026-05-06 14:49 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-block, hreitz, kwolf, qemu-stable
Commit 8599559580 ("fuse: Set direct_io and parallel_direct_writes")
broke use cases that require mmap() with MAP_SHARED on the export. In
particular, swtpm_setup using its 'file://' protocol requires this.
From the kernel documentation [0]:
> To allow shared mmap, the FUSE_DIRECT_IO_ALLOW_MMAP flag may be
> enabled in the FUSE_INIT reply.
Set the FUSE_DIRECT_IO_ALLOW_MMAP flag to restore compatibility with
users requiring shared mmap. The FUSE_INIT_EXT flag needs to be set
for the flags2 member to have an effect.
[0]: https://www.kernel.org/doc/html/next/filesystems/fuse/fuse-io.html
Cc: qemu-stable@nongnu.org
Fixes: 8599559580 ("fuse: Set direct_io and parallel_direct_writes")
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
block/export/fuse.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/block/export/fuse.c b/block/export/fuse.c
index 35218e3197..c0e8dfb643 100644
--- a/block/export/fuse.c
+++ b/block/export/fuse.c
@@ -856,7 +856,8 @@ static ssize_t coroutine_fn GRAPH_RDLOCK
fuse_co_init(FuseExport *exp, struct fuse_init_out *out,
const struct fuse_init_in *in)
{
- const uint32_t supported_flags = FUSE_ASYNC_READ | FUSE_ASYNC_DIO;
+ uint32_t supported_flags = FUSE_ASYNC_READ | FUSE_ASYNC_DIO;
+ uint32_t flags2 = 0;
if (in->major != 7) {
error_report("FUSE major version mismatch: We have 7, but kernel has %"
@@ -871,13 +872,21 @@ fuse_co_init(FuseExport *exp, struct fuse_init_out *out,
return -EINVAL;
}
+ if (!using_old_fuse_init_in(in)) {
+ /* The flags2 flags must be shifted down by 32 bits. */
+ const uint32_t supported_flags2 = FUSE_DIRECT_IO_ALLOW_MMAP >> 32;
+ /* flags2 is only considered if FUSE_INIT_EXT is set. */
+ supported_flags = supported_flags | FUSE_INIT_EXT;
+ flags2 = in->flags2 & supported_flags2;
+ }
+
*out = (struct fuse_init_out) {
.major = 7,
.minor = MIN(FUSE_KERNEL_MINOR_VERSION, in->minor),
.max_readahead = in->max_readahead,
.max_write = FUSE_MAX_WRITE_BYTES,
.flags = in->flags & supported_flags,
- .flags2 = 0,
+ .flags2 = flags2,
/* libfuse maximum: 2^16 - 1 */
.max_background = UINT16_MAX,
--
2.47.3
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 3/3] iotests: test shared mmap for fuse export
2026-05-06 14:49 [PATCH 0/3] block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag to fix regression Fiona Ebner
2026-05-06 14:49 ` [PATCH 1/3] block/export/fuse: use struct fuse_init_in Fiona Ebner
2026-05-06 14:49 ` [PATCH 2/3] block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag to fix regression Fiona Ebner
@ 2026-05-06 14:49 ` Fiona Ebner
2026-06-05 15:33 ` [PATCH 0/3] block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag to fix regression Kevin Wolf
3 siblings, 0 replies; 5+ messages in thread
From: Fiona Ebner @ 2026-05-06 14:49 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-block, hreitz, kwolf, qemu-stable
This test would have worked before commit 8599559580 ("fuse: Set
direct_io and parallel_direct_writes") and is working again since
commit HEAD~1 ("block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag
to fix regression").
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
tests/qemu-iotests/tests/fuse-mmap-shared | 103 ++++++++++++++++++
tests/qemu-iotests/tests/fuse-mmap-shared.out | 5 +
2 files changed, 108 insertions(+)
create mode 100755 tests/qemu-iotests/tests/fuse-mmap-shared
create mode 100644 tests/qemu-iotests/tests/fuse-mmap-shared.out
diff --git a/tests/qemu-iotests/tests/fuse-mmap-shared b/tests/qemu-iotests/tests/fuse-mmap-shared
new file mode 100755
index 0000000000..a0a10cea6a
--- /dev/null
+++ b/tests/qemu-iotests/tests/fuse-mmap-shared
@@ -0,0 +1,103 @@
+#!/usr/bin/env python3
+# group: rw
+#
+# Test that a FUSE export can be mmap()-ed with MAP_SHARED
+#
+# Copyright (C) 2026 Proxmox Server Solutions GmbH
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import os
+import itertools
+import mmap
+from mmap import MAP_SHARED
+from pathlib import Path
+
+import iotests
+from iotests import qemu_img, qemu_io, QemuStorageDaemon
+
+def test_fuse_support(mount_point):
+ test_qsd = QemuStorageDaemon('--blockdev', 'null-co,node-name=node0',
+ qmp=True)
+ res = test_qsd.qmp('block-export-add', {
+ 'id': 'exp0',
+ 'type': 'fuse',
+ 'node-name': 'node0',
+ 'mountpoint': mount_point,
+ 'allow-other': 'off'
+ })
+ test_qsd.stop()
+ if 'error' in res:
+ assert (res['error']['desc'] ==
+ "Parameter 'type' does not accept value 'fuse'")
+ iotests.notrun('No FUSE support')
+
+# Shared mmap when using direct IO is only supported for Linux kernels >= 6.6
+# with commit e78662e818f94 ("fuse: add a new fuse init flag to relax
+# estrictions in no cache mode").
+def test_linux_kernel_support():
+ [major, minor] = map(int, os.uname().release.split('.')[:2])
+ if major < 6 or (major == 6 and minor < 6):
+ iotests.notrun('No kernel support for shared mmap with direct IO')
+
+image_size = 1 * 1024 * 1024
+image = os.path.join(iotests.test_dir, 'image.' + iotests.imgfmt)
+fuse_mount_point = os.path.join(iotests.test_dir, 'export.fuse')
+Path(fuse_mount_point).touch()
+
+test_fuse_support(fuse_mount_point)
+test_linux_kernel_support()
+
+class TestMmapShared(iotests.QMPTestCase):
+
+ def setUp(self):
+ qemu_img('create', '-f', iotests.imgfmt, image, str(image_size))
+ qemu_io(image, '-c', f'write -P 23 0 {image_size}')
+
+ self.qsd = QemuStorageDaemon(qmp=True)
+
+ self.qsd.cmd('blockdev-add', {
+ 'node-name': 'node0',
+ 'driver': iotests.imgfmt,
+ 'file': {
+ 'driver': 'file',
+ 'filename': image
+ }
+ })
+
+ self.qsd.cmd('block-export-add', {
+ 'id': 'exp0',
+ 'type': 'fuse',
+ 'node-name': 'node0',
+ 'mountpoint': fuse_mount_point,
+ 'writable': True,
+ 'allow-other': 'off'
+ })
+
+ def tearDown(self):
+ self.stop_qsd()
+ os.remove(image)
+ os.remove(fuse_mount_point)
+
+ def stop_qsd(self):
+ if self.qsd:
+ self.qsd.stop()
+ self.qsd = None
+
+ def test_mmap_shared(self):
+ with open(fuse_mount_point, 'r+b') as file:
+ with mmap.mmap(file.fileno(), image_size, flags=MAP_SHARED) as mm:
+ buf = bytearray(image_size)
+ buf[:] = itertools.repeat(23, image_size)
+ assert mm.read(image_size) == buf
+ buf[:] = itertools.repeat(42, image_size)
+ mm.seek(0)
+ mm.write(buf)
+ mm.flush()
+ self.stop_qsd()
+ qemu_io(image, '-c', f'read -P 42 0 {image_size}')
+
+if __name__ == '__main__':
+ iotests.main(supported_fmts=['generic'],
+ supported_protocols=['file'],
+ supported_platforms=['linux'])
diff --git a/tests/qemu-iotests/tests/fuse-mmap-shared.out b/tests/qemu-iotests/tests/fuse-mmap-shared.out
new file mode 100644
index 0000000000..ae1213e6f8
--- /dev/null
+++ b/tests/qemu-iotests/tests/fuse-mmap-shared.out
@@ -0,0 +1,5 @@
+.
+----------------------------------------------------------------------
+Ran 1 tests
+
+OK
--
2.47.3
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH 0/3] block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag to fix regression
2026-05-06 14:49 [PATCH 0/3] block/export/fuse: set FUSE_DIRECT_IO_ALLOW_MMAP flag to fix regression Fiona Ebner
` (2 preceding siblings ...)
2026-05-06 14:49 ` [PATCH 3/3] iotests: test shared mmap for fuse export Fiona Ebner
@ 2026-06-05 15:33 ` Kevin Wolf
3 siblings, 0 replies; 5+ messages in thread
From: Kevin Wolf @ 2026-06-05 15:33 UTC (permalink / raw)
To: Fiona Ebner; +Cc: qemu-devel, qemu-block, hreitz, qemu-stable
Am 06.05.2026 um 16:49 hat Fiona Ebner geschrieben:
> Commit 8599559580 ("fuse: Set direct_io and parallel_direct_writes")
> broke use cases that require mmap() with MAP_SHARED on the export. In
> particular, swtpm_setup using its 'file://' protocol requires this.
> This is mentioned in the kernel documentation [0]:
>
> > To allow shared mmap, the FUSE_DIRECT_IO_ALLOW_MMAP flag may be
> > enabled in the FUSE_INIT reply.
>
> [0]: https://www.kernel.org/doc/html/next/filesystems/fuse/fuse-io.html
>
> Set the FUSE_DIRECT_IO_ALLOW_MMAP flag to restore compatibility with
> users requiring shared mmap.
Thanks, applied to the block branch.
Kevin
^ permalink raw reply [flat|nested] 5+ messages in thread