Signed-off-by: Wen Congyang <we...@cn.fujitsu.com>
Signed-off-by: zhanghailiang <zhang.zhanghaili...@huawei.com>
Signed-off-by: Gonglei <arei.gong...@huawei.com>
---
 block/block-backend.c          | 29 ++++++++++++++++++++++++++++-
 include/sysemu/block-backend.h |  2 ++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/block/block-backend.c b/block/block-backend.c
index c28e240..1bbc078 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -72,6 +72,19 @@ BlockBackend *blk_new(const char *name, Error **errp)
 }
 
 /*
+ * Create a new hidden BlockBackend, with a reference count of one.
+ * Return the new BlockBackend on success, null on failure.
+ */
+BlockBackend *blk_hide_new(void)
+{
+    BlockBackend *blk;
+
+    blk = g_new0(BlockBackend, 1);
+    blk->refcnt = 1;
+    return blk;
+}
+
+/*
  * Create a new BlockBackend with a new BlockDriverState attached.
  * Otherwise just like blk_new(), which see.
  */
@@ -91,6 +104,20 @@ BlockBackend *blk_new_with_bs(const char *name, Error 
**errp)
     return blk;
 }
 
+/*
+ * Create a new hidden BlockBackend with a new BlockDriverState attached.
+ * Otherwise just like blk_hide_new(), which see.
+ */
+BlockBackend *blk_hide_new_with_bs(void)
+{
+    BlockBackend *blk = blk_hide_new();
+    BlockDriverState *bs = bdrv_new();
+
+    blk->bs = bs;
+    bs->blk = blk;
+    return blk;
+}
+
 static void blk_delete(BlockBackend *blk)
 {
     assert(!blk->refcnt);
@@ -102,7 +129,7 @@ static void blk_delete(BlockBackend *blk)
         blk->bs = NULL;
     }
     /* Avoid double-remove after blk_hide_on_behalf_of_do_drive_del() */
-    if (blk->name[0]) {
+    if (blk->name && blk->name[0]) {
         QTAILQ_REMOVE(&blk_backends, blk, link);
     }
     g_free(blk->name);
diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h
index aab12b9..acc50f5 100644
--- a/include/sysemu/block-backend.h
+++ b/include/sysemu/block-backend.h
@@ -61,7 +61,9 @@ typedef struct BlockDevOps {
 } BlockDevOps;
 
 BlockBackend *blk_new(const char *name, Error **errp);
+BlockBackend *blk_hide_new(void);
 BlockBackend *blk_new_with_bs(const char *name, Error **errp);
+BlockBackend *blk_hide_new_with_bs(void);
 void blk_ref(BlockBackend *blk);
 void blk_unref(BlockBackend *blk);
 const char *blk_name(BlockBackend *blk);
-- 
2.1.0


Reply via email to