qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PULL 41/53] block/qcow2: Simplify shared L2 handling in amend
Date: Mon,  3 Nov 2014 11:50:44 +0000	[thread overview]
Message-ID: <1415015456-25086-42-git-send-email-stefanha@redhat.com> (raw)
In-Reply-To: <1415015456-25086-1-git-send-email-stefanha@redhat.com>

From: Max Reitz <mreitz@redhat.com>

Currently, we have a bitmap for keeping track of which clusters have
been created during the zero cluster expansion process. This was
necessary because we need to properly increase the refcount for shared
L2 tables.

However, now we can simply take the L2 refcount and use it for the
cluster allocated for expansion. This will be the correct refcount and
therefore we don't have to remember that cluster having been allocated
any more.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Benoît Canet <benoit.canet@nodalink.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Benoit Canet <benoit@irqsave.net>
Message-id: 1414404776-4919-7-git-send-email-mreitz@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 block/qcow2-cluster.c | 94 +++++++++++++++------------------------------------
 1 file changed, 28 insertions(+), 66 deletions(-)

diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 5687a4b..df0b2c9 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -1613,20 +1613,12 @@ fail:
  * Expands all zero clusters in a specific L1 table (or deallocates them, for
  * non-backed non-pre-allocated zero clusters).
  *
- * expanded_clusters is a bitmap where every bit corresponds to one cluster in
- * the image file; a bit gets set if the corresponding cluster has been used for
- * 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.
- *
  * l1_entries and *visited_l1_entries are used to keep track of progress for
  * status_cb(). l1_entries contains the total number of L1 entries and
  * *visited_l1_entries counts all visited L1 entries.
  */
 static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
-                                      int l1_size, uint8_t **expanded_clusters,
-                                      uint64_t *nb_clusters,
-                                      int64_t *visited_l1_entries,
+                                      int l1_size, int64_t *visited_l1_entries,
                                       int64_t l1_entries,
                                       BlockDriverAmendStatusCB *status_cb)
 {
@@ -1648,6 +1640,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 */
@@ -1671,33 +1664,19 @@ 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;
+            int64_t offset = l2_entry & L2E_OFFSET_MASK;
             int cluster_type = qcow2_get_cluster_type(l2_entry);
             bool preallocated = offset != 0;
 
-            if (cluster_type == QCOW2_CLUSTER_NORMAL) {
-                cluster_index = offset >> s->cluster_bits;
-                assert((cluster_index >= 0) && (cluster_index < *nb_clusters));
-                if ((*expanded_clusters)[cluster_index / 8] &
-                    (1 << (cluster_index % 8))) {
-                    /* Probably a shared L2 table; this cluster was a zero
-                     * cluster which has been expanded, its refcount
-                     * therefore most likely requires an update. */
-                    ret = qcow2_update_cluster_refcount(bs, cluster_index, 1,
-                                                        QCOW2_DISCARD_NEVER);
-                    if (ret < 0) {
-                        goto fail;
-                    }
-                    /* Since we just increased the refcount, the COPIED flag may
-                     * no longer be set. */
-                    l2_table[j] = cpu_to_be64(l2_entry & ~QCOW_OFLAG_COPIED);
-                    l2_dirty = true;
-                }
-                continue;
-            }
-            else if (qcow2_get_cluster_type(l2_entry) != QCOW2_CLUSTER_ZERO) {
+            if (cluster_type != QCOW2_CLUSTER_ZERO) {
                 continue;
             }
 
@@ -1715,6 +1694,19 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
                     ret = offset;
                     goto fail;
                 }
+
+                if (l2_refcount > 1) {
+                    /* For shared L2 tables, set the refcount accordingly (it is
+                     * already 1 and needs to be l2_refcount) */
+                    ret = qcow2_update_cluster_refcount(bs,
+                            offset >> s->cluster_bits, l2_refcount - 1,
+                            QCOW2_DISCARD_OTHER);
+                    if (ret < 0) {
+                        qcow2_free_clusters(bs, offset, s->cluster_size,
+                                            QCOW2_DISCARD_OTHER);
+                        goto fail;
+                    }
+                }
             }
 
             ret = qcow2_pre_write_overlap_check(bs, 0, offset, s->cluster_size);
@@ -1736,29 +1728,12 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
                 goto fail;
             }
 
-            l2_table[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
-            l2_dirty = true;
-
-            cluster_index = offset >> s->cluster_bits;
-
-            if (cluster_index >= *nb_clusters) {
-                uint64_t old_bitmap_size = (*nb_clusters + 7) / 8;
-                uint64_t new_bitmap_size;
-                /* The offset may lie beyond the old end of the underlying image
-                 * file for growable files only */
-                assert(bs->file->growable);
-                *nb_clusters = size_to_clusters(s, bs->file->total_sectors *
-                                                BDRV_SECTOR_SIZE);
-                new_bitmap_size = (*nb_clusters + 7) / 8;
-                *expanded_clusters = g_realloc(*expanded_clusters,
-                                               new_bitmap_size);
-                /* clear the newly allocated space */
-                memset(&(*expanded_clusters)[old_bitmap_size], 0,
-                       new_bitmap_size - old_bitmap_size);
+            if (l2_refcount == 1) {
+                l2_table[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
+            } else {
+                l2_table[j] = cpu_to_be64(offset);
             }
-
-            assert((cluster_index >= 0) && (cluster_index < *nb_clusters));
-            (*expanded_clusters)[cluster_index / 8] |= 1 << (cluster_index % 8);
+            l2_dirty = true;
         }
 
         if (is_active_l1) {
@@ -1823,9 +1798,7 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs,
 {
     BDRVQcowState *s = bs->opaque;
     uint64_t *l1_table = NULL;
-    uint64_t nb_clusters;
     int64_t l1_entries = 0, visited_l1_entries = 0;
-    uint8_t *expanded_clusters;
     int ret;
     int i, j;
 
@@ -1836,16 +1809,7 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs,
         }
     }
 
-    nb_clusters = size_to_clusters(s, bs->file->total_sectors *
-                                   BDRV_SECTOR_SIZE);
-    expanded_clusters = g_try_malloc0((nb_clusters + 7) / 8);
-    if (expanded_clusters == NULL) {
-        ret = -ENOMEM;
-        goto fail;
-    }
-
     ret = expand_zero_clusters_in_l1(bs, s->l1_table, s->l1_size,
-                                     &expanded_clusters, &nb_clusters,
                                      &visited_l1_entries, l1_entries,
                                      status_cb);
     if (ret < 0) {
@@ -1881,7 +1845,6 @@ 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,
                                          &visited_l1_entries, l1_entries,
                                          status_cb);
         if (ret < 0) {
@@ -1892,7 +1855,6 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs,
     ret = 0;
 
 fail:
-    g_free(expanded_clusters);
     g_free(l1_table);
     return ret;
 }
-- 
1.9.3

  parent reply	other threads:[~2014-11-03 11:52 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-03 11:50 [Qemu-devel] [PULL 00/53] Block patches Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 01/53] util: introduce MIN_NON_ZERO Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 02/53] BlockLimits: introduce max_transfer_length Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 03/53] block/iscsi: set max_transfer_length Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 04/53] block: avoid creating oversized writes in multiwrite_merge Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 05/53] block/iscsi: use sector_limits_lun2qemu throughout iscsi_refresh_limits Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 06/53] block/iscsi: check for oversized requests Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 07/53] ahci: Correct PIO/D2H FIS responses Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 08/53] ahci: Update byte count after DMA completion Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 09/53] ahci: Fix SDB FIS Construction Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 10/53] snapshot: Reset err to NULL to avoid double free Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 11/53] iotests: replace fake parallels image with authentic one Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 12/53] iotests: add v2 parallels sample image and simple test for it Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 13/53] block/parallels: fix access to not initialized memory in catalog_bitmap Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 14/53] rbd: Add support for bdrv_invalidate_cache Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 15/53] block.c: Fix type of IoOperationType variable in send_qmp_error_event() Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 16/53] snapshot: add bdrv_drain_all() to bdrv_snapshot_delete() to avoid concurrency problem Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 17/53] block/curl: Improve type safety of s->timeout Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 18/53] raw-posix: Fix raw_co_get_block_status() after EOF Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 19/53] raw-posix: raw_co_get_block_status() return value Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 20/53] iotests: Add test for external image truncation Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 21/53] qcow2: Allow "full" discard Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 22/53] qcow2: Implement bdrv_make_empty() Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 23/53] qcow2: Optimize bdrv_make_empty() Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 24/53] blockjob: Introduce block_job_complete_sync() Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 25/53] blockjob: Add "ready" field Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 26/53] iotests: Omit length/offset test in 040 and 041 Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 27/53] block/mirror: Improve progress report Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 28/53] qemu-img: Implement commit like QMP Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 29/53] qemu-img: Empty image after commit Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 30/53] qemu-img: Enable progress output for commit Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 31/53] qemu-img: Specify backing file " Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 32/53] iotests: Add _filter_qemu_img_map Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 33/53] iotests: Add test for backing-chain commits Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 34/53] iotests: Add test for qcow2's bdrv_make_empty Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 35/53] block: qemu-iotest 107 supports NFS Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 36/53] block: Add status callback to bdrv_amend_options() Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 37/53] qemu-img: Add progress output for amend Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 38/53] qemu-img: Fix insignificant memleak Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 39/53] block/qcow2: Implement status CB for amend Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 40/53] block/qcow2: Make get_refcount() global Stefan Hajnoczi
2014-11-03 11:50 ` Stefan Hajnoczi [this message]
2014-11-03 11:50 ` [Qemu-devel] [PULL 42/53] iotests: Expand test 061 Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 43/53] block: acquire AioContext in generic blockjob QMP commands Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 44/53] blockdev: acquire AioContext in do_qmp_query_block_jobs_one() Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 45/53] blockdev: acquire AioContext in blockdev_mark_auto_del() Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 46/53] blockdev: add note that block_job_cb() must be thread-safe Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 47/53] blockjob: add block_job_defer_to_main_loop() Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 48/53] block: add bdrv_drain() Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 49/53] block: let backup blockjob run in BDS AioContext Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 50/53] block: let stream " Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 51/53] block: let mirror " Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 52/53] block: let commit " Stefan Hajnoczi
2014-11-03 11:50 ` [Qemu-devel] [PULL 53/53] block: declare blockjobs and dataplane friends! Stefan Hajnoczi
2014-11-03 20:22 ` [Qemu-devel] [PULL 00/53] Block patches Peter Maydell

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=1415015456-25086-42-git-send-email-stefanha@redhat.com \
    --to=stefanha@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /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).