[Qemu-devel] [PATCH v5 14/18] qcow2: Switch qcow2_measure() to byte-based iteration

Eric Blake posted 18 patches 8 years, 7 months ago
There is a newer version of this series
[Qemu-devel] [PATCH v5 14/18] qcow2: Switch qcow2_measure() to byte-based iteration
Posted by Eric Blake 8 years, 7 months ago
This is new code, but it is easier to read if it makes passes over
the image using bytes rather than sectors (and will get easier in
the future when bdrv_get_block_status is converted to byte-based).

While at it, fix a bug in the computation of nb_sectors using MAX
rather than MIN and thus always passing INT_MAX (thankfully, the
bug was harmless, as bdrv_get_block_status_above() clamps its
answers according to image size).

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v5: new patch
---
 block/qcow2.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/block/qcow2.c b/block/qcow2.c
index c144ea5..85ee9b2 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -3662,20 +3662,19 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
              */
             required = virtual_size;
         } else {
-            int cluster_sectors = cluster_size / BDRV_SECTOR_SIZE;
-            int64_t sector_num;
+            int64_t offset;
             int pnum = 0;

-            for (sector_num = 0;
-                 sector_num < ssize / BDRV_SECTOR_SIZE;
-                 sector_num += pnum) {
-                int nb_sectors = MAX(ssize / BDRV_SECTOR_SIZE - sector_num,
-                                     INT_MAX);
+            for (offset = 0; offset < ssize;
+                 offset += pnum * BDRV_SECTOR_SIZE) {
+                int nb_sectors = MIN(ssize - offset,
+                                     INT_MAX) / BDRV_SECTOR_SIZE;
                 BlockDriverState *file;
                 int64_t ret;

                 ret = bdrv_get_block_status_above(in_bs, NULL,
-                                                  sector_num, nb_sectors,
+                                                  offset >> BDRV_SECTOR_BITS,
+                                                  nb_sectors,
                                                   &pnum, &file);
                 if (ret < 0) {
                     error_setg_errno(&local_err, -ret,
@@ -3688,12 +3687,11 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
                 } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) ==
                            (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) {
                     /* Extend pnum to end of cluster for next iteration */
-                    pnum = ROUND_UP(sector_num + pnum, cluster_sectors) -
-                           sector_num;
+                    pnum = (ROUND_UP(offset + pnum * BDRV_SECTOR_SIZE,
+                                 cluster_size) - offset) >> BDRV_SECTOR_BITS;

                     /* Count clusters we've seen */
-                    required += (sector_num % cluster_sectors + pnum) *
-                                BDRV_SECTOR_SIZE;
+                    required += offset % cluster_size + pnum * BDRV_SECTOR_SIZE;
                 }
             }
         }
-- 
2.9.4


Re: [Qemu-devel] [PATCH v5 14/18] qcow2: Switch qcow2_measure() to byte-based iteration
Posted by John Snow 8 years, 7 months ago

On 07/12/2017 09:05 PM, Eric Blake wrote:
> This is new code, but it is easier to read if it makes passes over
> the image using bytes rather than sectors (and will get easier in
> the future when bdrv_get_block_status is converted to byte-based).
> 
> While at it, fix a bug in the computation of nb_sectors using MAX
> rather than MIN and thus always passing INT_MAX (thankfully, the
> bug was harmless, as bdrv_get_block_status_above() clamps its
> answers according to image size).
> 

Well, unless ssize was >= 2^40, and then it wouldn't have chosen
INT_MAX, right!?

> Signed-off-by: Eric Blake <eblake@redhat.com>
> 

Reviewed-by: John Snow <jsnow@redhat.com>

Re: [Qemu-devel] [PATCH v5 14/18] qcow2: Switch qcow2_measure() to byte-based iteration
Posted by Eric Blake 8 years, 6 months ago
On 07/13/2017 01:49 PM, John Snow wrote:
> 
> 
> On 07/12/2017 09:05 PM, Eric Blake wrote:
>> This is new code, but it is easier to read if it makes passes over
>> the image using bytes rather than sectors (and will get easier in
>> the future when bdrv_get_block_status is converted to byte-based).
>>
>> While at it, fix a bug in the computation of nb_sectors using MAX
>> rather than MIN and thus always passing INT_MAX (thankfully, the
>> bug was harmless, as bdrv_get_block_status_above() clamps its
>> answers according to image size).
>>
> 
> Well, unless ssize was >= 2^40, and then it wouldn't have chosen
> INT_MAX, right!?

But then it would have wrapped the value around while assigning it to
'int nb_sectors'.  I suppose for some very-interesting size images, you
could get nb_sectors to turn into a negative value?  At any rate, it's
good the bug was short-lived.

> 
>> Signed-off-by: Eric Blake <eblake@redhat.com>
>>
> 
> Reviewed-by: John Snow <jsnow@redhat.com>
> 

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org