Patchwork qemu-img: find the highest offset in use during check

login
register
mail settings
Submitter Federico Simoncelli
Date Dec. 10, 2012, 9:13 a.m.
Message ID <1355130833-4639-1-git-send-email-fsimonce@redhat.com>
Download mbox | patch
Permalink /patch/204834/
State New
Headers show

Comments

Federico Simoncelli - Dec. 10, 2012, 9:13 a.m.
This patch adds the support for reporting the highest offset in use by
an image. This is particularly useful after a conversion (or a rebase)
where the destination is a block device in order to find the actual
amount of space in use.

Signed-off-by: Federico Simoncelli <fsimonce@redhat.com>
---
 block.h                |    1 +
 block/qcow2-refcount.c |   10 ++++++++--
 qemu-img.c             |    4 ++++
 3 files changed, 13 insertions(+), 2 deletions(-)
Kevin Wolf - Dec. 10, 2012, 12:16 p.m.
Am 10.12.2012 10:13, schrieb Federico Simoncelli:
> This patch adds the support for reporting the highest offset in use by
> an image. This is particularly useful after a conversion (or a rebase)
> where the destination is a block device in order to find the actual
> amount of space in use.
> 
> Signed-off-by: Federico Simoncelli <fsimonce@redhat.com>
> ---
>  block.h                |    1 +
>  block/qcow2-refcount.c |   10 ++++++++--
>  qemu-img.c             |    4 ++++
>  3 files changed, 13 insertions(+), 2 deletions(-)
> 
> diff --git a/block.h b/block.h
> index 722c620..de42e8c 100644
> --- a/block.h
> +++ b/block.h
> @@ -213,6 +213,7 @@ typedef struct BdrvCheckResult {
>      int check_errors;
>      int corruptions_fixed;
>      int leaks_fixed;
> +    int64_t highest_offset;
>      BlockFragInfo bfi;
>  } BdrvCheckResult;
>  
> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
> index 96224d1..017439d 100644
> --- a/block/qcow2-refcount.c
> +++ b/block/qcow2-refcount.c
> @@ -1116,7 +1116,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
>                            BdrvCheckMode fix)
>  {
>      BDRVQcowState *s = bs->opaque;
> -    int64_t size, i;
> +    int64_t size, i, highest_cluster;
>      int nb_clusters, refcount1, refcount2;
>      QCowSnapshot *sn;
>      uint16_t *refcount_table;
> @@ -1154,7 +1154,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
>          s->refcount_table_offset,
>          s->refcount_table_size * sizeof(uint64_t));
>  
> -    for(i = 0; i < s->refcount_table_size; i++) {
> +    for(i = 0, highest_cluster = 0; i < s->refcount_table_size; i++) {
>          uint64_t offset, cluster;
>          offset = s->refcount_table[i];
>          cluster = offset >> s->cluster_bits;
> @@ -1197,6 +1197,11 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
>          }
>  
>          refcount2 = refcount_table[i];
> +
> +        if (refcount1 > 0 || refcount2 > 0) {
> +            highest_cluster = i;
> +        }
> +
>          if (refcount1 != refcount2) {
>  
>              /* Check if we're allowed to fix the mismatch */
> @@ -1231,6 +1236,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
>          }
>      }
>  
> +    res->highest_offset = (highest_cluster + 1) * s->cluster_size;
>      ret = 0;
>  
>  fail:
> diff --git a/qemu-img.c b/qemu-img.c
> index e29e01b..3a8090b 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -470,6 +470,10 @@ static int img_check(int argc, char **argv)
>          result.bfi.fragmented_clusters * 100.0 / result.bfi.allocated_clusters);
>      }
>  
> +    if (result.highest_offset > 0) {
> +        printf("Highest offset in use: %lu\n", result.highest_offset);

highest_offset is not a unsigned long, but int64_t. Please use PRId64
instead of %lu.

I think we also need to change qemu-iotests so that it filters out these
lines, or many test cases would fail now. Or maybe we should think about
introducing a -v switch that enables the messages about fragmentation
and the highest offset.

Kevin

Patch

diff --git a/block.h b/block.h
index 722c620..de42e8c 100644
--- a/block.h
+++ b/block.h
@@ -213,6 +213,7 @@  typedef struct BdrvCheckResult {
     int check_errors;
     int corruptions_fixed;
     int leaks_fixed;
+    int64_t highest_offset;
     BlockFragInfo bfi;
 } BdrvCheckResult;
 
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index 96224d1..017439d 100644
--- a/block/qcow2-refcount.c
+++ b/block/qcow2-refcount.c
@@ -1116,7 +1116,7 @@  int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
                           BdrvCheckMode fix)
 {
     BDRVQcowState *s = bs->opaque;
-    int64_t size, i;
+    int64_t size, i, highest_cluster;
     int nb_clusters, refcount1, refcount2;
     QCowSnapshot *sn;
     uint16_t *refcount_table;
@@ -1154,7 +1154,7 @@  int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
         s->refcount_table_offset,
         s->refcount_table_size * sizeof(uint64_t));
 
-    for(i = 0; i < s->refcount_table_size; i++) {
+    for(i = 0, highest_cluster = 0; i < s->refcount_table_size; i++) {
         uint64_t offset, cluster;
         offset = s->refcount_table[i];
         cluster = offset >> s->cluster_bits;
@@ -1197,6 +1197,11 @@  int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
         }
 
         refcount2 = refcount_table[i];
+
+        if (refcount1 > 0 || refcount2 > 0) {
+            highest_cluster = i;
+        }
+
         if (refcount1 != refcount2) {
 
             /* Check if we're allowed to fix the mismatch */
@@ -1231,6 +1236,7 @@  int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
         }
     }
 
+    res->highest_offset = (highest_cluster + 1) * s->cluster_size;
     ret = 0;
 
 fail:
diff --git a/qemu-img.c b/qemu-img.c
index e29e01b..3a8090b 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -470,6 +470,10 @@  static int img_check(int argc, char **argv)
         result.bfi.fragmented_clusters * 100.0 / result.bfi.allocated_clusters);
     }
 
+    if (result.highest_offset > 0) {
+        printf("Highest offset in use: %lu\n", result.highest_offset);
+    }
+
     bdrv_delete(bs);
 
     if (ret < 0 || result.check_errors) {