From 65ed9843fcb0095584c551e1633f47d6186a41e8 Mon Sep 17 00:00:00 2001
Message-Id: <65ed9843fcb0095584c551e1633f47d6186a41e8.1350493760.git.minovotn@redhat.com>
In-Reply-To: <c93188b6c6973932d2adaa52e6a4920db13b4e62.1350493760.git.minovotn@redhat.com>
References: <c93188b6c6973932d2adaa52e6a4920db13b4e62.1350493760.git.minovotn@redhat.com>
From: Jeffrey Cody <jcody@redhat.com>
Date: Wed, 17 Oct 2012 05:59:21 +0200
Subject: [PATCH 08/35] stream: tweak usage of bdrv_co_is_allocated

RH-Author: Jeffrey Cody <jcody@redhat.com>
Message-id: <40845eff2bfa12f05092f765a5fd6049360f8923.1350447475.git.jcody@redhat.com>
Patchwork-id: 43263
O-Subject: [RHEL6.4 qemu-kvm PATCH v4 08/35] stream: tweak usage of bdrv_co_is_allocated
Bugzilla: 767233
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Eric Blake <eblake@redhat.com>
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>

From: Paolo Bonzini <pbonzini@redhat.com>

is_allocated_base has complex semantics that are not really usable
outside streaming.  Split the check in two parts, where the allocated
state for the top bs is moved to the caller.  The resulting function
is more generally useful.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
(cherry picked from commit f9749f28b78be36471d3d0f5d4b6eed030f8942e)

Conflicts:
	block/stream.c
Signed-off-by: Jeff Cody <jcody@redhat.com>
---
 block/stream.c | 51 +++++++++++++++++++++++++--------------------------
 1 file changed, 25 insertions(+), 26 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 block/stream.c | 51 +++++++++++++++++++++++++--------------------------
 1 file changed, 25 insertions(+), 26 deletions(-)

diff --git a/block/stream.c b/block/stream.c
index dd07534..de76f76 100644
--- a/block/stream.c
+++ b/block/stream.c
@@ -101,45 +101,33 @@ static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
 /*
  * Given an image chain: [BASE] -> [INTER1] -> [INTER2] -> [TOP]
  *
- * Return true if the given sector is allocated in top.
- * Return false if the given sector is allocated in intermediate images.
- * Return true otherwise.
+ * Return true if the given sector is allocated in any image between
+ * BASE and TOP (inclusive).  BASE can be NULL to check if the given
+ * sector is allocated in any image of the chain.  Return false otherwise.
  *
  * 'pnum' is set to the number of sectors (including and immediately following
  *  the specified sector) that are known to be in the same
  *  allocated/unallocated state.
  *
  */
-static int coroutine_fn is_allocated_base(BlockDriverState *top,
-                                          BlockDriverState *base,
-                                          int64_t sector_num,
-                                          int nb_sectors, int *pnum)
+static int coroutine_fn is_allocated_above(BlockDriverState *top,
+                                           BlockDriverState *base,
+                                           int64_t sector_num,
+                                           int nb_sectors, int *pnum)
 {
     BlockDriverState *intermediate;
-    int ret, n;
-
-    ret = bdrv_co_is_allocated(top, sector_num, nb_sectors, &n);
-    if (ret) {
-        *pnum = n;
-        return ret;
-    }
-
-    /*
-     * Is the unallocated chunk [sector_num, n] also
-     * unallocated between base and top?
-     */
-    intermediate = top->backing_hd;
+    int ret, n = nb_sectors;
 
+    intermediate = top;
     while (intermediate != base) {
         int pnum_inter;
-
         ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors,
                                    &pnum_inter);
         if (ret < 0) {
             return ret;
         } else if (ret) {
             *pnum = pnum_inter;
-            return 0;
+            return 1;
         }
 
         /*
@@ -156,7 +144,7 @@ static int coroutine_fn is_allocated_base(BlockDriverState *top,
     }
 
     *pnum = n;
-    return 1;
+    return 0;
 }
 
 static void coroutine_fn stream_run(void *opaque)
@@ -189,6 +177,7 @@ static void coroutine_fn stream_run(void *opaque)
 
     for (sector_num = 0; sector_num < end; sector_num += n) {
         uint64_t delay_ms = 0;
+        bool copy;
 
 wait:
         /* Note that even when no rate limit is applied we need to yield
@@ -199,10 +188,20 @@ wait:
             break;
         }
 
-        ret = is_allocated_base(bs, base, sector_num,
-                                STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
+        ret = bdrv_co_is_allocated(bs, sector_num,
+                                   STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
+        if (ret == 1) {
+            /* Allocated in the top, no need to copy.  */
+            copy = false;
+        } else {
+            /* Copy if allocated in the intermediate images.  Limit to the
+             * known-unallocated area [sector_num, sector_num+n).  */
+            ret = is_allocated_above(bs->backing_hd, base, sector_num, n, &n);
+            copy = (ret == 1);
+        }
+
         trace_stream_one_iteration(s, sector_num, n, ret);
-        if (ret == 0) {
+        if (ret >= 0 && copy) {
             if (s->common.speed) {
                 delay_ms = ratelimit_calculate_delay(&s->limit, n);
                 if (delay_ms > 0) {
-- 
1.7.11.7

