block: Move bdrv_attach_child() calls up the call chain
Let the callers of bdrv_open_inherit() call bdrv_attach_child(). It
needs to be called in all cases where bdrv_open_inherit() succeeds (i.e.
returns 0) and a child_role is given.
bdrv_attach_child() is moved upwards to avoid a forward declaration.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
diff --git a/block.c b/block.c
index 5e80336..0398bff 100644
--- a/block.c
+++ b/block.c
@@ -1102,6 +1102,19 @@
return 0;
}
+static void bdrv_attach_child(BlockDriverState *parent_bs,
+ BlockDriverState *child_bs,
+ const BdrvChildRole *child_role)
+{
+ BdrvChild *child = g_new(BdrvChild, 1);
+ *child = (BdrvChild) {
+ .bs = child_bs,
+ .role = child_role,
+ };
+
+ QLIST_INSERT_HEAD(&parent_bs->children, child, next);
+}
+
void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
{
@@ -1202,6 +1215,8 @@
error_free(local_err);
goto free_exit;
}
+
+ bdrv_attach_child(bs, backing_hd, &child_backing);
bdrv_set_backing_hd(bs, backing_hd);
free_exit:
@@ -1237,6 +1252,7 @@
assert(pbs);
assert(*pbs == NULL);
+ assert(child_role != NULL);
bdref_key_dot = g_strdup_printf("%s.", bdref_key);
qdict_extract_subqdict(options, &image_options, bdref_key_dot);
@@ -1257,6 +1273,11 @@
ret = bdrv_open_inherit(pbs, filename, reference, image_options, 0,
parent, child_role, NULL, errp);
+ if (ret < 0) {
+ goto done;
+ }
+
+ bdrv_attach_child(parent, *pbs, child_role);
done:
qdict_del(options, bdref_key);
@@ -1328,19 +1349,6 @@
return ret;
}
-static void bdrv_attach_child(BlockDriverState *parent_bs,
- BlockDriverState *child_bs,
- const BdrvChildRole *child_role)
-{
- BdrvChild *child = g_new(BdrvChild, 1);
- *child = (BdrvChild) {
- .bs = child_bs,
- .role = child_role,
- };
-
- QLIST_INSERT_HEAD(&parent_bs->children, child, next);
-}
-
/*
* Opens a disk image (raw, qcow2, vmdk, ...)
*
@@ -1393,9 +1401,6 @@
return -ENODEV;
}
bdrv_ref(bs);
- if (child_role) {
- bdrv_attach_child(parent, bs, child_role);
- }
*pbs = bs;
return 0;
}
@@ -1540,10 +1545,6 @@
goto close_and_fail;
}
- if (child_role) {
- bdrv_attach_child(parent, bs, child_role);
- }
-
QDECREF(options);
*pbs = bs;
return 0;