Call blk_is_available() before using blk_bs() to obtain the root
BlockDriverState behind the BlockBackend.

Signed-off-by: Max Reitz <mre...@redhat.com>
---
 blockdev.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/blockdev.c b/blockdev.c
index 4bd52b8..7f4470f 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -2821,7 +2821,8 @@ void qmp_change_backing_file(const char *device,
                              const char *backing_file,
                              Error **errp)
 {
-    BlockDriverState *bs = NULL;
+    BlockBackend *blk;
+    BlockDriverState *bs;
     AioContext *aio_context;
     BlockDriverState *image_bs = NULL;
     Error *local_err = NULL;
@@ -2830,15 +2831,21 @@ void qmp_change_backing_file(const char *device,
     int ret;
 
     /* find the top layer BDS of the chain */
-    bs = bdrv_find(device);
-    if (!bs) {
+    blk = blk_by_name(device);
+    if (!blk) {
         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
         return;
     }
 
-    aio_context = bdrv_get_aio_context(bs);
+    aio_context = blk_get_aio_context(blk);
     aio_context_acquire(aio_context);
 
+    if (!blk_is_available(blk)) {
+        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
+        goto out;
+    }
+    bs = blk_bs(blk);
+
     image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
-- 
2.1.0


Reply via email to