From b55d68c6f881ce8efe12a867ebab8baaba5bd22b Mon Sep 17 00:00:00 2001
Message-Id: <b55d68c6f881ce8efe12a867ebab8baaba5bd22b.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:48 +0200
Subject: [PATCH 35/35] block: in commit, determine base image from the top
 image

RH-Author: Jeffrey Cody <jcody@redhat.com>
Message-id: <3aedbef972b4021796b75af5aefdd8a3a9484a40.1350447475.git.jcody@redhat.com>
Patchwork-id: 43293
O-Subject: [RHEL6.4 qemu-kvm PATCH v4 35/35] block: in commit, determine base image from the top image
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>

This simplifies some code and error checking, and also fixes a bug.

bdrv_find_backing_image() should only be passed absolute filenames,
or filenames relative to the chain.  In the QMP message handler for
block commit, when looking up the base do so from the determined top
image, so we know it is reachable from top.

Signed-off-by: Jeff Cody <jcody@redhat.com>

Submitted and reviewed upstream, but not accepted yet.
---
 block/commit.c |  7 -------
 blockdev.c     | 21 +++++++++++----------
 2 files changed, 11 insertions(+), 17 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 block/commit.c |  7 -------
 blockdev.c     | 21 +++++++++++----------
 2 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/block/commit.c b/block/commit.c
index 78df11f..68c89e6 100644
--- a/block/commit.c
+++ b/block/commit.c
@@ -237,13 +237,6 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
         return;
     }
 
-    /* top and base may be valid, but let's make sure that base is reachable
-     * from top */
-    if (bdrv_find_backing_image(top, base->filename) != base) {
-        error_set(errp, QERR_BASE_NOT_REACHABLE, base->filename, top->filename);
-        return;
-    }
-
     overlay_bs = bdrv_find_overlay(bs, top);
 
     if (overlay_bs == NULL) {
diff --git a/blockdev.c b/blockdev.c
index cbe67c1..077f887 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -1361,16 +1361,6 @@ void qmp___com_redhat_block_commit(const char *device,
         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
         return;
     }
-    if (base && has_base) {
-        base_bs = bdrv_find_backing_image(bs, base);
-    } else {
-        base_bs = bdrv_find_base(bs);
-    }
-
-    if (base_bs == NULL) {
-        error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
-        return;
-    }
 
     /* default top_bs is the active layer */
     top_bs = bs;
@@ -1386,6 +1376,17 @@ void qmp___com_redhat_block_commit(const char *device,
         return;
     }
 
+    if (has_base && base) {
+        base_bs = bdrv_find_backing_image(top_bs, base);
+    } else {
+        base_bs = bdrv_find_base(top_bs);
+    }
+
+    if (base_bs == NULL) {
+        error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
+        return;
+    }
+
     commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
                 &local_err);
     if (local_err != NULL) {
-- 
1.7.11.7

