block-backend: Add blk_set_bs()
authorKevin Wolf <kwolf@redhat.com>
Thu, 17 Sep 2015 11:01:50 +0000 (13:01 +0200)
committerKevin Wolf <kwolf@redhat.com>
Fri, 16 Oct 2015 13:34:29 +0000 (15:34 +0200)
It allows changing the BlockDriverState that a BlockBackend points to.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Alberto Garcia <berto@igalia.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
block/block-backend.c
include/block/block_int.h

index c2e873292aa197b3b70145365c590164ed4a5b5c..225655126e928492039ff401654eb9a1d2f1eb8e 100644 (file)
@@ -238,6 +238,23 @@ BlockDriverState *blk_bs(BlockBackend *blk)
     return blk->bs;
 }
 
+/*
+ * Changes the BlockDriverState attached to @blk
+ */
+void blk_set_bs(BlockBackend *blk, BlockDriverState *bs)
+{
+    bdrv_ref(bs);
+
+    if (blk->bs) {
+        blk->bs->blk = NULL;
+        bdrv_unref(blk->bs);
+    }
+    assert(bs->blk == NULL);
+
+    blk->bs = bs;
+    bs->blk = blk;
+}
+
 /*
  * Return @blk's DriveInfo if any, else null.
  */
index 4598101086091740bfdec2f7ffa1a96432a50b22..cfcae52d7d91958334254bb2f99c35dd0dab4992 100644 (file)
@@ -659,6 +659,8 @@ void backup_start(BlockDriverState *bs, BlockDriverState *target,
                   BlockCompletionFunc *cb, void *opaque,
                   Error **errp);
 
+void blk_set_bs(BlockBackend *blk, BlockDriverState *bs);
+
 void blk_dev_change_media_cb(BlockBackend *blk, bool load);
 bool blk_dev_has_removable_media(BlockBackend *blk);
 void blk_dev_eject_request(BlockBackend *blk, bool force);