@@ -2037,13 +2037,25 @@ static void handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta)
QCowL2Meta *m;
for (m = l2meta; m != NULL; m = m->next) {
- if (s->prealloc_size && handle_prealloc(bs, m)) {
- if (check_zero_cow(bs, m)) {
- trace_qcow2_skip_cow(qemu_coroutine_self(), m->offset,
- m->nb_clusters);
- m->zero_cow = true;
+ bool preallocated_zeroes = s->prealloc_size && handle_prealloc(bs, m);
+
+ if (!check_zero_cow(bs, m)) {
+ continue;
+ }
+
+ if (!preallocated_zeroes &&
+ (m->cow_start.nb_bytes != 0 || m->cow_end.nb_bytes != 0))
+ {
+ if (bdrv_co_pwrite_zeroes(bs->file, m->alloc_offset,
+ m->nb_clusters * s->cluster_size,
+ BDRV_REQ_ALLOCATE) != 0)
+ {
+ continue;
}
}
+
+ trace_qcow2_skip_cow(qemu_coroutine_self(), m->offset, m->nb_clusters);
+ m->zero_cow = true;
}
}
@@ -160,7 +160,7 @@ poke_file "$TEST_IMG" '131084' "\x00\x00" # 0x2000c
# any unallocated cluster, leading to an attempt to overwrite the second L2
# table. Finally, resume the COW write and see it fail (but not crash).
echo "open -o file.driver=blkdebug $TEST_IMG
-break cow_read 0
+break write_aio 0
aio_write 0k 1k
wait_break 0
write 64k 64k
@@ -114,7 +114,8 @@ qcow2: Marking image as corrupt: Preventing invalid write on metadata (overlaps
blkdebug: Suspended request '0'
write failed: Input/output error
blkdebug: Resuming request '0'
-aio_write failed: No medium found
+wrote 1024/1024 bytes at offset 0
+1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
=== Testing unallocated image header ===
@@ -71,7 +71,7 @@ echo
_make_test_img $IMG_SIZE
# Create data clusters (not aligned to an L2 table)
-$QEMU_IO -c 'write -P 42 1M 256k' "$TEST_IMG" | _filter_qemu_io
+$QEMU_IO -c "write -P 42 $(((1024 + 32) * 1024)) 192k" "$TEST_IMG" | _filter_qemu_io
orig_map=$($QEMU_IMG map --output=json "$TEST_IMG")
# Convert the data clusters to preallocated zero clusters
@@ -22,8 +22,8 @@ Offset Length Mapped to File
=== Writing to preallocated zero clusters ===
Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67109376
-wrote 262144/262144 bytes at offset 1048576
-256 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 196608/196608 bytes at offset 1081344
+192 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
wrote 262144/262144 bytes at offset 1048576
256 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
wrote 196608/196608 bytes at offset 1081344
If COW areas of the newly allocated clusters are zeroes on the backing image: (even if preallocation feature is not used or it cannot detect if the image already reads as zeroes, e.g. writing to a hole / preallocated zero cluster) efficient bdrv_write_zeroes(flags=BDRV_REQ_ALLOCATE) can be used on the whole cluster instead of writing explicit zero buffers later in perform_cow(). iotest 060: write to the discarded cluster does not trigger COW anymore. so, break on write_aio event instead, will work for the test (but write won't fail anymore, so update reference output) iotest 066: cluster-alignment areas that were not really COWed are now detected as zeroes, hence the initial write has to be exactly the same size for the maps to match Signed-off-by: Anton Nefedov <anton.nefedov@virtuozzo.com> --- block/qcow2.c | 22 +++++++++++++++++----- tests/qemu-iotests/060 | 2 +- tests/qemu-iotests/060.out | 3 ++- tests/qemu-iotests/066 | 2 +- tests/qemu-iotests/066.out | 4 ++-- 5 files changed, 23 insertions(+), 10 deletions(-)