218e99
From bff9b33a3c4e0757c1e779ebedbffa391d926ecb Mon Sep 17 00:00:00 2001
218e99
From: Max Reitz <mreitz@redhat.com>
218e99
Date: Mon, 4 Nov 2013 22:31:52 +0100
218e99
Subject: [PATCH 01/87] block: drop bs_snapshots global variable
218e99
218e99
RH-Author: Max Reitz <mreitz@redhat.com>
218e99
Message-id: <1383604354-12743-2-git-send-email-mreitz@redhat.com>
218e99
Patchwork-id: 55301
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH 01/43] block: drop bs_snapshots global variable
218e99
Bugzilla: 1026524
218e99
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
218e99
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
218e99
From: Stefan Hajnoczi <stefanha@redhat.com>
218e99
218e99
BZ: 1026524
218e99
218e99
The bs_snapshots global variable points to the BlockDriverState which
218e99
will be used to save vmstate.  This is really a savevm.c concept but was
218e99
moved into block.c:bdrv_snapshots() when it became clear that hotplug
218e99
could result in a dangling pointer.
218e99
218e99
While auditing the block layer's global state I came upon bs_snapshots
218e99
and realized that a variable is not necessary here.  Simply find the
218e99
first BlockDriverState capable of internal snapshots each time this is
218e99
needed.
218e99
218e99
The behavior of bdrv_snapshots() is preserved across hotplug because new
218e99
drives are always appended to the bdrv_states list.  This means that
218e99
calling the new find_vmstate_bs() function is idempotent - it returns
218e99
the same BlockDriverState unless it was hot-unplugged.
218e99
218e99
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
Reviewed-by: Eric Blake <eblake@redhat.com>
218e99
Reviewed-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
218e99
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
(cherry picked from commit 29d782710f87f01991bfc85cd9bef7d15280a5e2)
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
---
218e99
 block.c               | 28 ----------------------------
218e99
 include/block/block.h |  1 -
218e99
 savevm.c              | 19 +++++++++++++++----
218e99
 3 files changed, 15 insertions(+), 33 deletions(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block.c               |   28 ----------------------------
218e99
 include/block/block.h |    1 -
218e99
 savevm.c              |   19 +++++++++++++++----
218e99
 3 files changed, 15 insertions(+), 33 deletions(-)
218e99
218e99
diff --git a/block.c b/block.c
218e99
index fdff92f..84a3a8d 100644
218e99
--- a/block.c
218e99
+++ b/block.c
218e99
@@ -99,9 +99,6 @@ static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
218e99
 static QLIST_HEAD(, BlockDriver) bdrv_drivers =
218e99
     QLIST_HEAD_INITIALIZER(bdrv_drivers);
218e99
 
218e99
-/* The device to use for VM snapshots */
218e99
-static BlockDriverState *bs_snapshots;
218e99
-
218e99
 /* If non-zero, use only whitelisted block drivers */
218e99
 static int use_bdrv_whitelist;
218e99
 
218e99
@@ -1384,9 +1381,6 @@ void bdrv_close(BlockDriverState *bs)
218e99
     notifier_list_notify(&bs->close_notifiers, bs);
218e99
 
218e99
     if (bs->drv) {
218e99
-        if (bs == bs_snapshots) {
218e99
-            bs_snapshots = NULL;
218e99
-        }
218e99
         if (bs->backing_hd) {
218e99
             bdrv_delete(bs->backing_hd);
218e99
             bs->backing_hd = NULL;
218e99
@@ -1619,7 +1613,6 @@ void bdrv_delete(BlockDriverState *bs)
218e99
 
218e99
     bdrv_close(bs);
218e99
 
218e99
-    assert(bs != bs_snapshots);
218e99
     g_free(bs);
218e99
 }
218e99
 
218e99
@@ -1663,9 +1656,6 @@ void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
218e99
 {
218e99
     bs->dev_ops = ops;
218e99
     bs->dev_opaque = opaque;
218e99
-    if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
218e99
-        bs_snapshots = NULL;
218e99
-    }
218e99
 }
218e99
 
218e99
 void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
218e99
@@ -3493,24 +3483,6 @@ int bdrv_is_snapshot(BlockDriverState *bs)
218e99
     return !!(bs->open_flags & BDRV_O_SNAPSHOT);
218e99
 }
218e99
 
218e99
-BlockDriverState *bdrv_snapshots(void)
218e99
-{
218e99
-    BlockDriverState *bs;
218e99
-
218e99
-    if (bs_snapshots) {
218e99
-        return bs_snapshots;
218e99
-    }
218e99
-
218e99
-    bs = NULL;
218e99
-    while ((bs = bdrv_next(bs))) {
218e99
-        if (bdrv_can_snapshot(bs)) {
218e99
-            bs_snapshots = bs;
218e99
-            return bs;
218e99
-        }
218e99
-    }
218e99
-    return NULL;
218e99
-}
218e99
-
218e99
 int bdrv_snapshot_create(BlockDriverState *bs,
218e99
                          QEMUSnapshotInfo *sn_info)
218e99
 {
218e99
diff --git a/include/block/block.h b/include/block/block.h
218e99
index bc1f5f6..3b3f40e 100644
218e99
--- a/include/block/block.h
218e99
+++ b/include/block/block.h
218e99
@@ -362,7 +362,6 @@ BlockInfo *bdrv_query_info(BlockDriverState *s);
218e99
 BlockStats *bdrv_query_stats(const BlockDriverState *bs);
218e99
 int bdrv_can_snapshot(BlockDriverState *bs);
218e99
 int bdrv_is_snapshot(BlockDriverState *bs);
218e99
-BlockDriverState *bdrv_snapshots(void);
218e99
 int bdrv_snapshot_create(BlockDriverState *bs,
218e99
                          QEMUSnapshotInfo *sn_info);
218e99
 int bdrv_snapshot_goto(BlockDriverState *bs,
218e99
diff --git a/savevm.c b/savevm.c
218e99
index 1ed7f05..d9f3787 100644
218e99
--- a/savevm.c
218e99
+++ b/savevm.c
218e99
@@ -2262,6 +2262,17 @@ out:
218e99
     return ret;
218e99
 }
218e99
 
218e99
+static BlockDriverState *find_vmstate_bs(void)
218e99
+{
218e99
+    BlockDriverState *bs = NULL;
218e99
+    while ((bs = bdrv_next(bs))) {
218e99
+        if (bdrv_can_snapshot(bs)) {
218e99
+            return bs;
218e99
+        }
218e99
+    }
218e99
+    return NULL;
218e99
+}
218e99
+
218e99
 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
218e99
                               const char *name)
218e99
 {
218e99
@@ -2338,7 +2349,7 @@ void do_savevm(Monitor *mon, const QDict *qdict)
218e99
         }
218e99
     }
218e99
 
218e99
-    bs = bdrv_snapshots();
218e99
+    bs = find_vmstate_bs();
218e99
     if (!bs) {
218e99
         monitor_printf(mon, "No block device can accept snapshots\n");
218e99
         return;
218e99
@@ -2440,7 +2451,7 @@ int load_vmstate(const char *name)
218e99
     QEMUFile *f;
218e99
     int ret;
218e99
 
218e99
-    bs_vm_state = bdrv_snapshots();
218e99
+    bs_vm_state = find_vmstate_bs();
218e99
     if (!bs_vm_state) {
218e99
         error_report("No block device supports snapshots");
218e99
         return -ENOTSUP;
218e99
@@ -2519,7 +2530,7 @@ void do_delvm(Monitor *mon, const QDict *qdict)
218e99
     int ret;
218e99
     const char *name = qdict_get_str(qdict, "name");
218e99
 
218e99
-    bs = bdrv_snapshots();
218e99
+    bs = find_vmstate_bs();
218e99
     if (!bs) {
218e99
         monitor_printf(mon, "No block device supports snapshots\n");
218e99
         return;
218e99
@@ -2551,7 +2562,7 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict)
218e99
     int *available_snapshots;
218e99
     char buf[256];
218e99
 
218e99
-    bs = bdrv_snapshots();
218e99
+    bs = find_vmstate_bs();
218e99
     if (!bs) {
218e99
         monitor_printf(mon, "No available block device supports snapshots\n");
218e99
         return;
218e99
-- 
218e99
1.7.1
218e99