From d63b3c58bce32b976231923f5e186ebd55259c98 Mon Sep 17 00:00:00 2001
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
Date: Fri, 1 Jul 2016 15:47:29 +0200
Subject: [PATCH 37/48] vma: use BlockBackend on extract

As we else rely on bdrv_close_all() do clean up, which was rewritten
in ca9bd24cf1d53775169ba9adc17e265554d1afed and fails on "dangling"
BDS pointers, such a pointer exists with *bs.
Use the BlockBackend to get our BDS and just unref the BlockBackend
when done, it handles the rest for us.

The other two calls to bdrv_close_all() happen in verify_content()
and dump_config(), both do not have a BDS so no need to change here.
---
 vma.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/vma.c b/vma.c
index f71e5a5..ad51090 100644
--- a/vma.c
+++ b/vma.c
@@ -20,6 +20,7 @@
 #include "qemu/main-loop.h"
 #include "qapi/qmp/qstring.h"
 #include "sysemu/char.h" /* qstring_from_str */
+#include "sysemu/block-backend.h"
 
 static void help(void)
 {
@@ -264,6 +265,8 @@ static int extract_content(int argc, char **argv)
     int vmstate_fd = -1;
     guint8 vmstate_stream = 0;
 
+    BlockBackend *blk = NULL;
+
     for (i = 1; i < 255; i++) {
         VmaDeviceInfo *di = vma_reader_get_device_info(vmar, i);
         if (di && (strcmp(di->devname, "vmstate") == 0)) {
@@ -308,8 +311,6 @@ static int extract_content(int argc, char **argv)
                 write_zero = false;
             }
 
-            BlockDriverState *bs = bdrv_new();
-
 	    size_t devlen = strlen(devfn);
 	    QDict *options = NULL;
             if (format) {
@@ -327,10 +328,14 @@ static int extract_content(int argc, char **argv)
 		qdict_put(options, "driver", qstring_from_str("raw"));
 	    }
 
-	    if (errp || bdrv_open(&bs, devfn, NULL, options, flags, &errp)) {
+
+	    if (errp || !(blk = blk_new_open(devfn, NULL, options, flags, &errp))) {
                 g_error("can't open file %s - %s", devfn,
                         error_get_pretty(errp));
             }
+
+	    BlockDriverState *bs = blk_bs(blk);
+
             if (vma_reader_register_bs(vmar, i, bs, write_zero, &errp) < 0) {
                 g_error("%s", error_get_pretty(errp));
             }
@@ -363,6 +368,8 @@ static int extract_content(int argc, char **argv)
 
     vma_reader_destroy(vmar);
 
+    blk_unref(blk);
+
     bdrv_close_all();
 
     return ret;
-- 
2.1.4

