qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PATCH 5/8] block/qcow2: Implement status CB for amend
Date: Fri, 25 Jul 2014 20:07:42 +0200	[thread overview]
Message-ID: <1406311665-2814-6-git-send-email-mreitz@redhat.com> (raw)
In-Reply-To: <1406311665-2814-1-git-send-email-mreitz@redhat.com>

The only really time-consuming operation potentially performed by
qcow2_amend_options() is zero cluster expansion when downgrading qcow2
images from compat=1.1 to compat=0.10, so report status of that
operation and that operation only through the status CB.

For this, count the number of L2 zero entries, use this as the basis for
the total "amend job length" and increase the current offset by the
cluster size multiplied by the refcount of the L2 table when expanding
zero clusters.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/qcow2-cluster.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++--
 block/qcow2.c         |   9 ++--
 block/qcow2.h         |   3 +-
 3 files changed, 147 insertions(+), 10 deletions(-)

diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 4208dc0..0f52ef6 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -1548,10 +1548,20 @@ fail:
  * zero expansion (i.e., has been filled with zeroes and is referenced from an
  * L2 table). nb_clusters contains the total cluster count of the image file,
  * i.e., the number of bits in expanded_clusters.
+ *
+ * zero_clusters_count and *expanded_count are used to keep track of progress
+ * for status_cb(). zero_clusters_count contains the total number of L2 zero
+ * cluster entries. Accordingly, *expanded_count counts all visited L2 zero
+ * cluster entries; shared L2 tables are counted accordingly, that is, for each
+ * L2 zero cluster entry the count is increased by the refcount of the L2 table
+ * cluster.
  */
 static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
                                       int l1_size, uint8_t **expanded_clusters,
-                                      uint64_t *nb_clusters)
+                                      uint64_t *nb_clusters,
+                                      int64_t *expanded_count,
+                                      int64_t zero_clusters_count,
+                                      BlockDriverAmendStatusCB *status_cb)
 {
     BDRVQcowState *s = bs->opaque;
     bool is_active_l1 = (l1_table == s->l1_table);
@@ -1568,6 +1578,7 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
     for (i = 0; i < l1_size; i++) {
         uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
         bool l2_dirty = false;
+        int l2_refcount;
 
         if (!l2_offset) {
             /* unallocated */
@@ -1587,6 +1598,12 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
             goto fail;
         }
 
+        l2_refcount = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits);
+        if (l2_refcount < 0) {
+            ret = l2_refcount;
+            goto fail;
+        }
+
         for (j = 0; j < s->l2_size; j++) {
             uint64_t l2_entry = be64_to_cpu(l2_table[j]);
             int64_t offset = l2_entry & L2E_OFFSET_MASK, cluster_index;
@@ -1617,6 +1634,8 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
                 continue;
             }
 
+            *expanded_count += l2_refcount;
+
             if (!preallocated) {
                 if (!bs->backing_hd) {
                     /* not backed; therefore we can simply deallocate the
@@ -1703,6 +1722,11 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
                 }
             }
         }
+
+        if (status_cb) {
+            status_cb(bs, *expanded_count << s->cluster_bits,
+                      zero_clusters_count << s->cluster_bits);
+        }
     }
 
     ret = 0;
@@ -1724,26 +1748,137 @@ fail:
 }
 
 /*
+ * Counts all zero clusters in a specific L1 table.
+ */
+static int64_t count_zero_clusters_in_l1(BlockDriverState *bs,
+                                         uint64_t *l1_table, int l1_size)
+{
+    BDRVQcowState *s = bs->opaque;
+    bool is_active_l1 = (l1_table == s->l1_table);
+    uint64_t *l2_table = NULL;
+    int64_t count = 0;
+    int ret;
+    int i, j;
+
+    if (!is_active_l1) {
+        l2_table = qemu_blockalign(bs, s->cluster_size);
+    }
+
+    for (i = 0; i < l1_size; i++) {
+        uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
+
+        if (!l2_offset) {
+            continue;
+        }
+
+        if (is_active_l1) {
+            ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset,
+                                  (void **)&l2_table);
+        } else {
+            ret = bdrv_read(bs->file, l2_offset / BDRV_SECTOR_SIZE,
+                            (void *)l2_table, s->cluster_sectors);
+        }
+        if (ret < 0) {
+            goto fail;
+        }
+
+        for (j = 0; j < s->l2_size; j++) {
+            if (qcow2_get_cluster_type(be64_to_cpu(l2_table[j]))
+                == QCOW2_CLUSTER_ZERO)
+            {
+                count++;
+            }
+        }
+
+        if (is_active_l1) {
+            ret = qcow2_cache_put(bs, s->l2_table_cache, (void **)&l2_table);
+            l2_table = NULL;
+            if (ret < 0) {
+                goto fail;
+            }
+        }
+    }
+
+    ret = 0;
+
+fail:
+    if (l2_table) {
+        if (!is_active_l1) {
+            qemu_vfree(l2_table);
+        } else {
+            if (ret < 0) {
+                qcow2_cache_put(bs, s->l2_table_cache, (void **)&l2_table);
+            } else {
+                ret = qcow2_cache_put(bs, s->l2_table_cache,
+                                      (void **)&l2_table);
+            }
+        }
+    }
+
+    return ret < 0 ? ret : count;
+}
+
+/*
  * For backed images, expands all zero clusters on the image. For non-backed
  * images, deallocates all non-pre-allocated zero clusters (and claims the
  * allocation for pre-allocated ones). This is important for downgrading to a
  * qcow2 version which doesn't yet support metadata zero clusters.
  */
-int qcow2_expand_zero_clusters(BlockDriverState *bs)
+int qcow2_expand_zero_clusters(BlockDriverState *bs,
+                               BlockDriverAmendStatusCB *status_cb)
 {
     BDRVQcowState *s = bs->opaque;
     uint64_t *l1_table = NULL;
     uint64_t nb_clusters;
+    int64_t zero_cluster_count = 0, expanded_count = 0;
     uint8_t *expanded_clusters;
     int ret;
     int i, j;
 
+    if (status_cb) {
+        int64_t l1_zero_count;
+
+        l1_zero_count = count_zero_clusters_in_l1(bs, s->l1_table, s->l1_size);
+        if (l1_zero_count < 0) {
+            ret = l1_zero_count;
+            goto fail;
+        }
+        zero_cluster_count += l1_zero_count;
+
+        for (i = 0; i < s->nb_snapshots; i++) {
+            int l1_sectors = DIV_ROUND_UP(s->snapshots[i].l1_size *
+                                          sizeof(uint64_t), BDRV_SECTOR_SIZE);
+
+            l1_table = g_realloc(l1_table, l1_sectors * BDRV_SECTOR_SIZE);
+
+            ret = bdrv_read(bs->file, s->snapshots[i].l1_table_offset /
+                            BDRV_SECTOR_SIZE, (void *)l1_table, l1_sectors);
+            if (ret < 0) {
+                goto fail;
+            }
+
+            for (j = 0; j < s->snapshots[i].l1_size; j++) {
+                be64_to_cpus(&l1_table[j]);
+            }
+
+            l1_zero_count = count_zero_clusters_in_l1(bs, l1_table,
+                                                      s->snapshots[i].l1_size);
+            if (l1_zero_count < 0) {
+                ret = l1_zero_count;
+                goto fail;
+            }
+            zero_cluster_count += l1_zero_count;
+        }
+    }
+
     nb_clusters = size_to_clusters(s, bs->file->total_sectors *
                                    BDRV_SECTOR_SIZE);
     expanded_clusters = g_malloc0((nb_clusters + 7) / 8);
 
     ret = expand_zero_clusters_in_l1(bs, s->l1_table, s->l1_size,
-                                     &expanded_clusters, &nb_clusters);
+                                     &expanded_clusters, &nb_clusters,
+                                     &expanded_count, zero_cluster_count,
+                                     status_cb);
     if (ret < 0) {
         goto fail;
     }
@@ -1777,7 +1912,9 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs)
         }
 
         ret = expand_zero_clusters_in_l1(bs, l1_table, s->snapshots[i].l1_size,
-                                         &expanded_clusters, &nb_clusters);
+                                         &expanded_clusters, &nb_clusters,
+                                         &expanded_count, zero_cluster_count,
+                                         status_cb);
         if (ret < 0) {
             goto fail;
         }
diff --git a/block/qcow2.c b/block/qcow2.c
index 757f890..6e8c8ab 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2147,7 +2147,8 @@ static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
  * Downgrades an image's version. To achieve this, any incompatible features
  * have to be removed.
  */
-static int qcow2_downgrade(BlockDriverState *bs, int target_version)
+static int qcow2_downgrade(BlockDriverState *bs, int target_version,
+                           BlockDriverAmendStatusCB *status_cb)
 {
     BDRVQcowState *s = bs->opaque;
     int current_version = s->qcow_version;
@@ -2196,7 +2197,7 @@ static int qcow2_downgrade(BlockDriverState *bs, int target_version)
     /* clearing autoclear features is trivial */
     s->autoclear_features = 0;
 
-    ret = qcow2_expand_zero_clusters(bs);
+    ret = qcow2_expand_zero_clusters(bs, status_cb);
     if (ret < 0) {
         return ret;
     }
@@ -2224,8 +2225,6 @@ static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
     int ret;
     QemuOptDesc *desc = opts->list->desc;
 
-    (void)status_cb;
-
     while (desc && desc->name) {
         if (!qemu_opt_find(opts, desc->name)) {
             /* only change explicitly defined options */
@@ -2291,7 +2290,7 @@ static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
                 return ret;
             }
         } else {
-            ret = qcow2_downgrade(bs, new_version);
+            ret = qcow2_downgrade(bs, new_version, status_cb);
             if (ret < 0) {
                 return ret;
             }
diff --git a/block/qcow2.h b/block/qcow2.h
index b423b71..c0e1b7b 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -524,7 +524,8 @@ int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
     int nb_sectors, enum qcow2_discard_type type);
 int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
 
-int qcow2_expand_zero_clusters(BlockDriverState *bs);
+int qcow2_expand_zero_clusters(BlockDriverState *bs,
+                               BlockDriverAmendStatusCB *status_cb);
 
 /* qcow2-snapshot.c functions */
 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
-- 
2.0.1

  parent reply	other threads:[~2014-07-25 18:07 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-25 18:07 [Qemu-devel] [PATCH 0/8] block/qcow2: Improve (?) zero cluster expansion Max Reitz
2014-07-25 18:07 ` [Qemu-devel] [PATCH 1/8] block: Add status callback to bdrv_amend_options() Max Reitz
2014-07-30 14:50   ` Eric Blake
2014-07-25 18:07 ` [Qemu-devel] [PATCH 2/8] qemu-img: Add progress output for amend Max Reitz
2014-07-30 14:55   ` Eric Blake
2014-07-30 20:20     ` Max Reitz
2014-07-25 18:07 ` [Qemu-devel] [PATCH 3/8] qemu-img: Fix insignifcant memleak Max Reitz
2014-07-30 14:56   ` Eric Blake
2014-07-25 18:07 ` [Qemu-devel] [PATCH 4/8] block/qcow2: Make get_refcount() global Max Reitz
2014-07-30 15:04   ` Eric Blake
2014-07-25 18:07 ` Max Reitz [this message]
2014-07-30 15:23   ` [Qemu-devel] [PATCH 5/8] block/qcow2: Implement status CB for amend Eric Blake
2014-07-25 18:07 ` [Qemu-devel] [PATCH 6/8] block/qcow2: Simplify shared L2 handling in amend Max Reitz
2014-07-30 15:36   ` Eric Blake
2014-07-25 18:07 ` [Qemu-devel] [PATCH 7/8] block/qcow2: Speed up zero cluster expansion Max Reitz
2014-07-30 16:14   ` Eric Blake
2014-07-30 20:31     ` Max Reitz
2014-07-30 20:31     ` Eric Blake
2014-07-30 20:41       ` Max Reitz
2014-07-25 18:07 ` [Qemu-devel] [PATCH 8/8] iotests: Expand test 061 Max Reitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1406311665-2814-6-git-send-email-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).