qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 5/8] block/qcow2: Implement status CB for amend


From: Max Reitz
Subject: [Qemu-devel] [PATCH 5/8] block/qcow2: Implement status CB for amend
Date: Fri, 25 Jul 2014 20:07:42 +0200

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 <address@hidden>
---
 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]