Skip to contentSkip to conversation

iotests: Improve mirror-sparse on various filesystems

PATCH In review Eric Blake 2 participants 2 messages May 22, 2025 – May 23, 2025

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 iotests: Improve mirror-sparse on various filesystems

Fiona reported that an ext4 filesystem on top of LVM can sometimes report over-allocation to du (based on the hueristics the filesystem is making while observing the contents being mirrored); even though the contents and actual size matched, about 50% of the time the size reported by disk_usage was too large by 4k, failing the test.

Similarly, on ZFS where a file is created with preallocation=full, du does not see the full allocation until things have had time to settle; adding a sync call reduces the chance of catching that async window:

| [I] febner@enia ~/qemu/build/tests/qemu-iotests (master)> qemu-img create my.raw 20M -f
|w -o preallocation=full
| Formatting 'my.raw', fmt=raw size=20971520 preallocation=full
| [I] febner@enia ~/qemu/build/tests/qemu-iotests (master)> du --block-size=1 my.raw
| 512 my.raw
| [I] febner@enia ~/qemu/build/tests/qemu-iotests (master)> du --block-size=1 my.raw
| 20980224    my.raw

Fiona also reported that on a compressed ZFS, the filesystem can end up reporting smaller disk_usage if it re-compresses a file, despite a fully-allocating mirror - but since I don't have a compressed ZFS handy for reproducing that test, that may remain a sporadic problem for another day.

Fixes c0ddcb2c ("tests: Add iotest mirror-sparse for recent patches")
---
 tests/qemu-iotests/common.rc           | 2 ++
 tests/qemu-iotests/tests/mirror-sparse | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
tests/qemu-iotests/common.rc +2 -0
index 237f746af88..c3fc0bcf02a 100644
@@ -143,6 +143,8 @@ _optstr_add()
 # report real disk usage for sparse files
 disk_usage()
 {
+    # ZFS has lazy allocation; sync the file first for best results
+    sync "$1"
     du --block-size=1 "$1" | awk '{print $1}'
 }
 
tests/qemu-iotests/tests/mirror-sparse +3 -1
index 8c52a4e2448..338d6cfbb35 100755
@@ -96,10 +96,12 @@ _send_qemu_cmd $h1 '{"execute": "blockdev-del", "arguments":
                 {"node-name": "dst"}}' 'return' \
                   | _filter_block_job_offset | _filter_block_job_len
 $QEMU_IMG compare -U -f $IMGFMT -F raw $TEST_IMG.base $TEST_IMG
+# Some filesystems can fudge allocations for various reasons; rather
+# than expecting precise 2M and 20M images, it is better to allow for slop.
 result=$(disk_usage $TEST_IMG)
 if test $result -lt $((3*1024*1024)); then
     actual=sparse
-elif test $result = $((20*1024*1024)); then
+elif test $result -gt $((19*1024*1024)); then
     actual=full
 else
     actual=unknown
Signature · 1 line
2.49.0
Reviewed-by Tested-by on v1 1/1
#1 lore raw

Am 22.05.25 um 23:14 schrieb Eric Blake:

Quoted 2 lines

Fiona reported that an ext4 filesystem on top of LVM can sometimes report over-allocation to du (based on the hueristics the filesystem

Typo: heuristics

Quoted 22 lines

is making while observing the contents being mirrored); even though the contents and actual size matched, about 50% of the time the size reported by disk_usage was too large by 4k, failing the test.

Similarly, on ZFS where a file is created with preallocation=full, du does not see the full allocation until things have had time to settle; adding a sync call reduces the chance of catching that async window:

| [I] febner@enia ~/qemu/build/tests/qemu-iotests (master)> qemu-img create my.raw 20M -f
|w -o preallocation=full
| Formatting 'my.raw', fmt=raw size=20971520 preallocation=full
| [I] febner@enia ~/qemu/build/tests/qemu-iotests (master)> du --block-size=1 my.raw
| 512 my.raw
| [I] febner@enia ~/qemu/build/tests/qemu-iotests (master)> du --block-size=1 my.raw
| 20980224    my.raw

Fiona also reported that on a compressed ZFS, the filesystem can end up reporting smaller disk_usage if it re-compresses a file, despite a fully-allocating mirror - but since I don't have a compressed ZFS handy for reproducing that test, that may remain a sporadic problem for another day.

Quoted 3 lines
Fixes c0ddcb2c ("tests: Add iotest mirror-sparse for recent patches")

If you drop the hunk with the sync:

Quoted 10 lines
---
 tests/qemu-iotests/common.rc           | 2 ++
 tests/qemu-iotests/tests/mirror-sparse | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
tests/qemu-iotests/common.rc +0 -0
index 237f746af88..c3fc0bcf02a 100644

tests/qemu-iotests/common.rc _optstr_add() · line 147

Quoted 5 lines
tests/qemu-iotests/common.rc +2 -0
@@ -143,6 +143,8 @@ _optstr_add()
 # report real disk usage for sparse files
 disk_usage()
 {
+    # ZFS has lazy allocation; sync the file first for best results
+    sync "$1"

Unfortunately, just syncing the file seems to be not enough. It seems to be necessary to sync the whole filesystem, but that of course is too expensive for the helper here:

[I] febner@enia ~> rm my.raw; qemu-img create my.raw 20M -f raw -o preallocation=full; sync my.raw; du --block-size=1 my.raw Formatting 'my.raw', fmt=raw size=20971520 preallocation=full
512 my.raw
[I] febner@enia ~> rm my.raw; qemu-img create my.raw 20M -f raw -o preallocation=full; sync -f my.raw; du --block-size=1 my.raw Formatting 'my.raw', fmt=raw size=20971520 preallocation=full
20980224 my.raw

There's already quite a few other test failures on ZFS, so I guess it's not worth it right now if there's no easy fix (mirror-sparse also still fails, because the file is sparse again after mirroring, ZFS seems very aggressive trying to reduce allocation):

For -raw:
Failures: 106 109 150 175 221 240 253 308 mirror-sparse write-zeroes-unmap Failed 10 of 86 iotests

tests/qemu-iotests/common.rc _optstr_add() · line 150

Quoted 3 lines
tests/qemu-iotests/common.rc +0 -0
     du --block-size=1 "$1" | awk '{print $1}'
 }
 

Best Regards,
Fiona