Skip to contentSkip to conversation

qcow2: Don't strand clusters near 2G intervals during commit

PATCH Superseded Eric Blake 2 participants 3 messages Dec 3, 2016 – Dec 5, 2016 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 qcow2: Don't strand clusters near 2G intervals during commit

The qcow2_make_empty() function is reached during 'qemu-img commit', in order to clear out ALL clusters of an image. However, if the image cannot use the fast code path (true if the image is format 0.10, or if the image contains a snapshot), the cluster size is larger than 512, and the image is larger than 2G in size, then our choice of sector_step causes problems. Since it is not cluster aligned, but qcow2_discard_clusters() silently ignores an unaligned head or tail, we are leaving clusters allocated.

Enhance the testsuite to expose the flaw, and patch the problem by ensuring our step size is aligned.

[qcow2_discard_clusters() is a GROSS interface: it takes a mix of byte offset and sector count to perform cluster operations. But fixing it to use a saner byte/byte rather than byte/sector interface, and/or asserting that the counts are now aligned thanks to both this patch and commit 3482b9b, is material for another day.]

---
 block/qcow2.c              |   3 +-
 tests/qemu-iotests/097     |  41 +++++---
 tests/qemu-iotests/097.out | 249 +++++++++++++++++++++++++++++++++------------
 3 files changed, 210 insertions(+), 83 deletions(-)
block/qcow2.c +2 -1
index ed9e0f3..369b542 100644
@@ -2808,7 +2808,8 @@ static int qcow2_make_empty(BlockDriverState *bs)
 {
     BDRVQcow2State *s = bs->opaque;
     uint64_t start_sector;
-    int sector_step = INT_MAX / BDRV_SECTOR_SIZE;
+    int sector_step = QEMU_ALIGN_DOWN(INT_MAX / BDRV_SECTOR_SIZE,
+                                      s->cluster_size);
     int l1_clusters, ret = 0;
 
     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
tests/qemu-iotests/097 +25 -16

81 lines not on this page · raw message

tests/qemu-iotests/097.out +183 -66

302 lines not on this page · raw message

Signature · 1 line
2.9.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Eric Blake

The qcow2_make_empty() function is reached during 'qemu-img commit', in order to clear out ALL clusters of an image. However, if the image cannot use the fast code path (true if the image is format 0.10, or if the image contains a snapshot), the cluster size is larger than 512, and the image is larger than 2G in size, then our choice of sector_step causes problems. Since it is not cluster aligned, but qcow2_discard_clusters() silently ignores an unaligned head or tail, we are leaving clusters allocated.

Enhance the testsuite to expose the flaw, and patch the problem by ensuring our step size is aligned.

[qcow2_discard_clusters() is a GROSS interface: it takes a mix of byte offset and sector count to perform cluster operations. But fixing it to use a saner byte/byte rather than byte/sector interface, and/or asserting that the counts are now aligned thanks to both this patch and commit 3482b9b, is material for another day.]

Quoted 6 lines
---
 block/qcow2.c              |   3 +-
 tests/qemu-iotests/097     |  41 +++++---
 tests/qemu-iotests/097.out | 249 +++++++++++++++++++++++++++++++++------------
 3 files changed, 210 insertions(+), 83 deletions(-)
author
#2 lore raw
In reply to Eric Blake
Quoted 27 lines · Eric Blake

The qcow2_make_empty() function is reached during 'qemu-img commit', in order to clear out ALL clusters of an image. However, if the image cannot use the fast code path (true if the image is format 0.10, or if the image contains a snapshot), the cluster size is larger than 512, and the image is larger than 2G in size, then our choice of sector_step causes problems. Since it is not cluster aligned, but qcow2_discard_clusters() silently ignores an unaligned head or tail, we are leaving clusters allocated.

Enhance the testsuite to expose the flaw, and patch the problem by ensuring our step size is aligned.

[qcow2_discard_clusters() is a GROSS interface: it takes a mix of byte offset and sector count to perform cluster operations. But fixing it to use a saner byte/byte rather than byte/sector interface, and/or asserting that the counts are now aligned thanks to both this patch and commit 3482b9b, is material for another day.]

---
 block/qcow2.c              |   3 +-
 tests/qemu-iotests/097     |  41 +++++---
 tests/qemu-iotests/097.out | 249 +++++++++++++++++++++++++++++++++------------
 3 files changed, 210 insertions(+), 83 deletions(-)

+++ b/block/qcow2.c

block/qcow2.c static int qcow2_make_empty(BlockDriverState *bs) · line 2812

Quoted 6 lines
block/qcow2.c +2 -1
@@ -2808,7 +2808,8 @@ static int qcow2_make_empty(BlockDriverState *bs)
 {
     BDRVQcow2State *s = bs->opaque;
     uint64_t start_sector;
-    int sector_step = INT_MAX / BDRV_SECTOR_SIZE;
+    int sector_step = QEMU_ALIGN_DOWN(INT_MAX / BDRV_SECTOR_SIZE,
+                                      s->cluster_size);

Oh shoot. I got the units wrong, and made the slow path do more loop iterations than necessary (rounding sectors to cluster size in bytes is inappropriate - either the rounding has to occur before division, or the rounding needs to be by secotrs instead of bytes). I'll send a v2 that gets the math right.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org