qcow2: Some fixes
cover qcow2: Some fixes
This series contains some fixes for issues reported by Eric.
Max Reitz (4):
qcow2: Fix preallocation size formula
qcow2: Reuse preallocated zero clusters
qcow2: Discard preallocated zero clusters
iotests: Extend test 066
block/qcow2.h | 3 ++ block/qcow2-cluster.c | 83 ++++++++++++++++++++--------- block/qcow2.c | 9 ++-- tests/qemu-iotests/066 | 128 ++++++++++++++++++++++++++++++++++++++++++++- tests/qemu-iotests/066.out | 46 ++++++++++++++++ 5 files changed, 239 insertions(+), 30 deletions(-)
Signature · 1 line
2.12.2
PATCH 1/4 qcow2: Fix preallocation size formula
When calculating the number of reftable entries, we should actually use the number of refblocks and not (wrongly[1]) re-calculate it.
[1] "Wrongly" means: Dividing the number of clusters by the number of entries per refblock and rounding down instead of up.
--- block/qcow2.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
block/qcow2.c +5 -4
@@ -2139,7 +2139,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,* too, as long as the bulk is allocated here). Therefore, using * floating point arithmetic is fine. */ int64_t meta_size =0 ; -uint64_t nreftablee, nrefblocke, nl1e, nl2e; +uint64_t nreftablee, nrefblocke, nl1e, nl2e, refblock_count;int64_t aligned_total_size =align_offset (total_size, cluster_size);int refblock_bits, refblock_size;/* refcount entry size in bytes */
@@ -2182,11 +2182,12 @@ static int qcow2_create2(const char *filename, int64_t total_size, nrefblocke = (aligned_total_size + meta_size + cluster_size) / (cluster_size - rces - rces *sizeof (uint64_t ) / cluster_size); - meta_size +=DIV_ROUND_UP (nrefblocke, refblock_size) * cluster_size; + refblock_count =DIV_ROUND_UP (nrefblocke, refblock_size); + meta_size += refblock_count * cluster_size;/* total size of refcount tables */ - nreftablee = nrefblocke / refblock_size; - nreftablee =align_offset (nreftablee, cluster_size /sizeof (uint64_t )); + nreftablee =align_offset (refblock_count, + cluster_size /sizeof (uint64_t )); meta_size += nreftablee *sizeof (uint64_t ); qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
Signature · 1 line
2.12.2
Re: [Qemu-devel] [PATCH 1/4] qcow2: Fix preallocation size formula
Quoted 12 lines · Max Reitz
When calculating the number of reftable entries, we should actually use the number of refblocks and not (wrongly[1]) re-calculate it.
[1] "Wrongly" means: Dividing the number of clusters by the number of entries per refblock and rounding down instead of up.
Reported-by Eric Blake <[email protected]>Signed-off-by Max Reitz <[email protected]>--- block/qcow2.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 2/4 qcow2: Reuse preallocated zero clusters
In reply to Max ReitzInstead of just freeing preallocated zero clusters and completely allocating them from scratch, reuse them.
We cannot do this in handle_copied(), however, since this is a COW operation. Therefore, we have to add the new logic to handle_alloc() and simply return the existing offset if it exists. The only catch is that we have to convince qcow2_alloc_cluster_link_l2() not to free the old clusters (because we have reused them).
--- block/qcow2.h | 3 ++ block/qcow2-cluster.c | 80 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 24 deletions(-)
block/qcow2.h +3 -0
@@ -322,6 +322,9 @@ typedef struct QCowL2Meta/** Number of newly allocated clusters */ int nb_clusters; +/** Do not free the old clusters */ +bool keep_old_clusters; +/** * Requests that overlap with this allocation and wait to be restarted * when the allocating request has completed.
block/qcow2-cluster.c +56 -24
122 lines not on this page · raw message
Signature · 1 line
2.12.2
Re: [Qemu-devel] [PATCH 2/4] qcow2: Reuse preallocated zero clusters
Quoted 18 lines · Max Reitz
Instead of just freeing preallocated zero clusters and completely allocating them from scratch, reuse them.
We cannot do this in
handle_copied(), however, since this is a COW operation. Therefore, we have to add the new logic tohandle_alloc()and simply return the existing offset if it exists. The only catch is that we have to convinceqcow2_alloc_cluster_link_l2()not to free the old clusters (because we have reused them).Reported-by Eric Blake <[email protected]>Signed-off-by Max Reitz <[email protected]>--- block/qcow2.h | 3 ++ block/qcow2-cluster.c | 80 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 24 deletions(-)
block/qcow2-cluster.c static int count_contiguous_clusters(int nb_clusters, int cluster_size, · line 325
Quoted 6 lines
block/qcow2-cluster.c +5
-1-assert (qcow2_get_cluster_type (first_entry) ==QCOW2_CLUSTER_NORMAL ); +/* must be allocated */ + first_cluster_type =qcow2_get_cluster_type (first_entry); +assert (first_cluster_type ==QCOW2_CLUSTER_NORMAL || + (first_cluster_type ==QCOW2_CLUSTER_ZERO && + (first_entry &L2E_OFFSET_MASK ) !=0 ));
I wonder if we should have separate QCOW2_CLUSTER_ZERO_PLAIN and QCOW2_CLUSTER_ZERO_ALLOCATED, so we don't have to keep checking first_entry & L2E_OFFSET_MASK. But I think that can be a separate followup if we want it.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 3/4 qcow2: Discard preallocated zero clusters
In reply to Max ReitzIn discard_single_l2(), we completely discard normal clusters instead of simply turning them into preallocated zero clusters. That means we should probably do the same with such preallocated zero clusters:
Discard them instead of keeping them allocated.
--- block/qcow2-cluster.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
block/qcow2-cluster.c +2 -1
@@ -1511,7 +1511,8 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset,break ;case QCOW2_CLUSTER_ZERO : -if (!full_discard) { +/* Preallocated zero clusters should be discarded in any case */ +if (!full_discard && (old_l2_entry &L2E_OFFSET_MASK ) ==0 ) {continue ; }break ;
Signature · 1 line
2.12.2
Re: [Qemu-devel] [PATCH 3/4] qcow2: Discard preallocated zero clusters
Quoted 10 lines · Max Reitz
In
discard_single_l2(), we completely discard normal clusters instead of simply turning them into preallocated zero clusters. That means we should probably do the same with such preallocated zero clusters:
Discard them instead of keeping them allocated.Reported-by Eric Blake <[email protected]>Signed-off-by Max Reitz <[email protected]>--- block/qcow2-cluster.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
block/qcow2-cluster.c static int discard_single_l2(BlockDriverState *bs, uint64_t offset, · line 1515
Quoted 12 lines
block/qcow2-cluster.c +2
-1@@ -1511,7 +1511,8 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset,break ;case QCOW2_CLUSTER_ZERO : -if (!full_discard) { +/* Preallocated zero clusters should be discarded in any case */ +if (!full_discard && (old_l2_entry &L2E_OFFSET_MASK ) ==0 ) {
Again, if we had a separate QCOW2_CLUSTER_* for plain vs. pre-allocated zero, this might be easier to write.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 4/4 iotests: Extend test 066
In reply to Max Reitz066 was supposed to be a test "for discarding preallocated zero clusters", but it did so incompletely: While it did check the image file's integrity after the operation, it did not confirm that the clusters are indeed freed. This patch adds this test.
In addition, new cases for writing to preallocated zero clusters are added.
--- tests/qemu-iotests/066 | 128 ++++++++++++++++++++++++++++++++++++++++++++- tests/qemu-iotests/066.out | 46 ++++++++++++++++ 2 files changed, 173 insertions(+), 1 deletion(-)
tests/qemu-iotests/066 +127 -1
@@ -1,6 +1,6 @@ #!/bin/bash # -# Test case for discarding preallocated zero clusters in qcow2 +# Test case for preallocated zero clusters in qcow2 # # Copyright (C) 2013 Red Hat, Inc. #
@@ -55,8 +55,134 @@ _make_test_img $IMG_SIZE $QEMU_IO -c "write 0 256k" -c "write -z 0 256k" -c "write 64M 512" \ -c "discard 0 $IMG_SIZE" -c "read -P 0 0 $IMG_SIZE" "$TEST_IMG" \ | _filter_qemu_io + # Check the image (there shouldn't be any leaks) _check_test_img +# Map the image (we want all clusters to be gone) +$QEMU_IMG map "$TEST_IMG" + +_cleanup_test_img + + +echo +echo '=== Writing to preallocated zero clusters ===' +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 +orig_map=$($QEMU_IMG map --output=json "$TEST_IMG") + +# Convert the data clusters to preallocated zero clusters +$QEMU_IO -c 'write -z 1M 256k' "$TEST_IMG" | _filter_qemu_io + +# Now write to them (with a COW needed for the head and tail) +$QEMU_IO -c "write -P 23 $(((1024 + 32) * 1024)) 192k" "$TEST_IMG" \ + | _filter_qemu_io + +# Check metadata correctness +_check_test_img + +# Check data correctness +$QEMU_IO -c "read -P 0 $(( 1024 * 1024)) 32k" \ + -c "read -P 23 $(((1024 + 32) * 1024)) 192k" \ + -c "read -P 0 $(((1024 + 32 + 192) * 1024)) 32k" \ + "$TEST_IMG" \ + | _filter_qemu_io + +# Check that we have actually reused the original area +new_map=$($QEMU_IMG map --output=json "$TEST_IMG") +if [ "$new_map" = "$orig_map" ]; then + echo 'Successfully reused original clusters.' +else + echo 'Failed to reuse original clusters.' + echo 'Original map:' + echo "$orig_map" + echo 'New map:' + echo "$new_map" +fi + +_cleanup_test_img + + +echo +echo '=== Writing to a snapshotted preallocated zero cluster ===' +echo + +_make_test_img 64k + +# Create a preallocated zero cluster +$QEMU_IO -c 'write -P 42 0 64k' -c 'write -z 0 64k' "$TEST_IMG" \ + | _filter_qemu_io + +# Snapshot it +$QEMU_IMG snapshot -c foo "$TEST_IMG" + +# Write to the cluster +$QEMU_IO -c 'write -P 23 0 64k' "$TEST_IMG" | _filter_qemu_io + +# Check metadata correctness +_check_test_img + +# Check data correctness +$QEMU_IO -c 'read -P 23 0 64k' "$TEST_IMG" | _filter_qemu_io +$QEMU_IMG snapshot -a foo "$TEST_IMG" +$QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io + +_cleanup_test_img + + +echo +echo '=== Consecutive write to a preallocated zero cluster ===' +echo + +_make_test_img 192k + +# Create three normal clusters +$QEMU_IO -c 'write -P 42 0 192k' "$TEST_IMG" | _filter_qemu_io +orig_map=$($QEMU_IMG map --output=json "$TEST_IMG") + +# Make the middle cluster a preallocated zero cluster +$QEMU_IO -c 'write -z 64k 64k' "$TEST_IMG" | _filter_qemu_io + +# Try to overwrite everything: This should reuse the whole range. To test that +# this only issues a single continuous write request, use blkdebug. +$QEMU_IO -c 'write -P 42 0 192k' \ + "json:{ + 'driver': '$IMGFMT', + 'file': { + 'driver': 'blkdebug', + 'image.filename': '$TEST_IMG', + 'set-state': [{ + 'event': 'write_aio', + 'new_state': 2 + }], + 'inject-error': [{ + 'event': 'write_aio', + 'state': 2 + }] + } + }" \ + | _filter_qemu_io + +# Check metadata correctness +_check_test_img + +# Check that we have actually reused the original area +new_map=$($QEMU_IMG map --output=json "$TEST_IMG") +if [ "$new_map" = "$orig_map" ]; then + echo 'Successfully reused original clusters.' +else + echo 'Failed to reuse original clusters.' + echo 'Original map:' + echo "$orig_map" + echo 'New map:' + echo "$new_map" +fi + +_cleanup_test_img + # success, all done echo "*** done"
tests/qemu-iotests/066.out +46 -0
50 lines not on this page · raw message
Signature · 1 line
2.12.2
Re: [Qemu-devel] [PATCH 4/4] iotests: Extend test 066
Quoted 15 lines · Max Reitz
066 was supposed to be a test "for discarding preallocated zero clusters", but it did so incompletely: While it did check the image file's integrity after the operation, it did not confirm that the clusters are indeed freed. This patch adds this test.
In addition, new cases for writing to preallocated zero clusters are added.
Signed-off-by Max Reitz <[email protected]>--- tests/qemu-iotests/066 | 128 ++++++++++++++++++++++++++++++++++++++++++++- tests/qemu-iotests/066.out | 46 ++++++++++++++++ 2 files changed, 173 insertions(+), 1 deletion(-)
tests/qemu-iotests/066 _make_test_img $IMG_SIZE · line 62
Quoted 8 lines
tests/qemu-iotests/066 +3
-0@@ -55,8 +55,134 @@ _make_test_img $IMG_SIZE $QEMU_IO -c "write 0 256k" -c "write -z 0 256k" -c "write 64M 512" \ -c "discard 0 $IMG_SIZE" -c "read -P 0 0 $IMG_SIZE" "$TEST_IMG" \ | _filter_qemu_io + # Check the image (there shouldn't be any leaks) _check_test_img +# Map the image (we want all clusters to be gone) +$QEMU_IMG map "$TEST_IMG"
The human-readable qemu-img map ignores allocated but reads-as-zeros clusters. Maybe it shouldn't, but only --output=json does the right thing (actually, even then it doesn't, without my pending patch that I am rebasing on top of your series [1]).
But I think this is sufficient for test 66, since I'm adding more tests in my pending series.
[1] https://lists.gnu.org/archive/html/qemu-devel/2017-04/msg05224.html
Quoted 16 lines
tests/qemu-iotests/066 +16
-0+# Create three normal clusters +$QEMU_IO -c 'write -P 42 0 192k' "$TEST_IMG" | _filter_qemu_io +orig_map=$($QEMU_IMG map --output=json "$TEST_IMG") + +# Make the middle cluster a preallocated zero cluster +$QEMU_IO -c 'write -z 64k 64k' "$TEST_IMG" | _filter_qemu_io + +# Try to overwrite everything: This should reuse the whole range. To test that +# this only issues a single continuous write request, use blkdebug. +$QEMU_IO -c 'write -P 42 0 192k' \ + "json:{ + 'driver': '$IMGFMT', + 'file': { + 'driver': 'blkdebug', + 'image.filename': '$TEST_IMG', + 'set-state': [{
tests/qemu-iotests/066 _make_test_img $IMG_SIZE · line 167
Quoted 10 lines
tests/qemu-iotests/066 +10
-0+ 'event': 'write_aio', + 'new_state': 2 + }], + 'inject-error': [{ + 'event': 'write_aio', + 'state': 2 + }] + } + }" \ + | _filter_qemu_io
blkdebug is cool!
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH 4/4] iotests: Extend test 066
In reply to Max ReitzAm 04.05.2017 um 01:11 hat Max Reitz geschrieben:
Quoted 23 lines
066 was supposed to be a test "for discarding preallocated zero clusters", but it did so incompletely: While it did check the image file's integrity after the operation, it did not confirm that the clusters are indeed freed. This patch adds this test.
In addition, new cases for writing to preallocated zero clusters are added.
Signed-off-by Max Reitz <[email protected]>tests/qemu-iotests/066 +13
-0+echo +echo '=== Writing to a snapshotted preallocated zero cluster ===' +echo + +_make_test_img 64k + +# Create a preallocated zero cluster +$QEMU_IO -c 'write -P 42 0 64k' -c 'write -z 0 64k' "$TEST_IMG" \ + | _filter_qemu_io + +# Snapshot it +$QEMU_IMG snapshot -c foo "$TEST_IMG" +
tests/qemu-iotests/066 _make_test_img $IMG_SIZE · line 131
Quoted 10 lines
tests/qemu-iotests/066 +10
-0+# Write to the cluster +$QEMU_IO -c 'write -P 23 0 64k' "$TEST_IMG" | _filter_qemu_io + +# Check metadata correctness +_check_test_img + +# Check data correctness +$QEMU_IO -c 'read -P 23 0 64k' "$TEST_IMG" | _filter_qemu_io +$QEMU_IMG snapshot -a foo "$TEST_IMG" +$QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io
This doesn't actually test that we allocated a new cluster, just that the zero flag from the snapshot L2 table still takes effect. If we wanted to test this, we could either add some more snapshotting and writes in order to cause a second COW of the zero cluster, or at least do a 'qemu-img map'.
Of course, all of this is perfectly fine as a follow-up.
Kevin
Kevin Wolf
AppliedAm 04.05.2017 um 01:11 hat Max Reitz geschrieben:
Quoted 1 line
This series contains some fixes for issues reported by Eric.
Thanks, applied to the block branch (though I think it's a new feature rather than a fix for the most part).
Kevin