Skip to contentSkip to conversation

block/file-posix: allow BLKZEROOUT with -t writeback

PATCH In review Stefan Hajnoczi 2 participants 2 messages Apr 17, 2025 – Apr 21, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/file-posix: allow BLKZEROOUT with -t writeback

The Linux BLKZEROOUT ioctl is only invoked when BDRV_O_NOCACHE is set because old kernels did not invalidate the page cache. In that case mixing BLKZEROOUT with buffered I/O could lead to corruption.

However, Linux 4.9 commit 22dd6d356628 ("block: invalidate the page cache when issuing BLKZEROOUT") made BLKZEROOUT coherent with the page cache.

I have checked that Linux 4.9+ kernels are shipped at least as far back as Debian 10 (buster), openSUSE Leap 15.2, and RHEL/CentOS 8.

Use BLKZEROOUT with buffered I/O, mostly so qemu-img ... -t writeback can offload write zeroes.

Cc Paolo Bonzini <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 block/file-posix.c | 11 -----------
 1 file changed, 11 deletions(-)
block/file-posix.c +0 -11
index 56d1972d15..9ba0b68958 100644
@@ -781,17 +781,6 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
     }
 #endif
 
-    if (S_ISBLK(st.st_mode)) {
-#ifdef __linux__
-        /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache.  Do
-         * not rely on the contents of discarded blocks unless using O_DIRECT.
-         * Same for BLKZEROOUT.
-         */
-        if (!(bs->open_flags & BDRV_O_NOCACHE)) {
-            s->has_write_zeroes = false;
-        }
-#endif
-    }
 #ifdef __FreeBSD__
     if (S_ISCHR(st.st_mode)) {
         /*
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Stefan Hajnoczi

The Linux BLKZEROOUT ioctl is only invoked when BDRV_O_NOCACHE is set because old kernels did not invalidate the page cache. In that case mixing BLKZEROOUT with buffered I/O could lead to corruption.

However, Linux 4.9 commit 22dd6d356628 ("block: invalidate the page cache when issuing BLKZEROOUT") made BLKZEROOUT coherent with the page cache.

I have checked that Linux 4.9+ kernels are shipped at least as far back as Debian 10 (buster), openSUSE Leap 15.2, and RHEL/CentOS 8.

Use BLKZEROOUT with buffered I/O, mostly so qemu-img ... -t writeback can offload write zeroes.

Quoted 6 lines
Cc Paolo Bonzini <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 block/file-posix.c | 11 -----------
 1 file changed, 11 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org