From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49504) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5cnW-0005uI-EO for qemu-devel@nongnu.org; Wed, 25 May 2016 13:40:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5cnV-0004Zz-Dr for qemu-devel@nongnu.org; Wed, 25 May 2016 13:40:46 -0400 From: Kevin Wolf Date: Wed, 25 May 2016 19:39:36 +0200 Message-Id: <1464197996-4581-12-git-send-email-kwolf@redhat.com> In-Reply-To: <1464197996-4581-1-git-send-email-kwolf@redhat.com> References: <1464197996-4581-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PULL 11/31] block: Introduce bdrv_replace_child() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org This adds a common function that is called when attaching a new child to a parent, removing a child from a parent and when reconfiguring the graph so that an existing child points to a different node now. Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake Reviewed-by: Fam Zheng --- block.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/block.c b/block.c index 38df365..351344e 100644 --- a/block.c +++ b/block.c @@ -1150,18 +1150,32 @@ static int bdrv_fill_options(QDict **options, const char *filename, return 0; } +static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs) +{ + BlockDriverState *old_bs = child->bs; + + if (old_bs) { + QLIST_REMOVE(child, next_parent); + } + if (new_bs) { + QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent); + } + + child->bs = new_bs; +} + BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, const char *child_name, const BdrvChildRole *child_role) { BdrvChild *child = g_new(BdrvChild, 1); *child = (BdrvChild) { - .bs = child_bs, + .bs = NULL, .name = g_strdup(child_name), .role = child_role, }; - QLIST_INSERT_HEAD(&child_bs->parents, child, next_parent); + bdrv_replace_child(child, child_bs); return child; } @@ -1182,7 +1196,9 @@ static void bdrv_detach_child(BdrvChild *child) QLIST_REMOVE(child, next); child->next.le_prev = NULL; } - QLIST_REMOVE(child, next_parent); + + bdrv_replace_child(child, NULL); + g_free(child->name); g_free(child); } @@ -2203,10 +2219,8 @@ static void change_parent_backing_link(BlockDriverState *from, QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) { assert(c->role != &child_backing); - c->bs = to; - QLIST_REMOVE(c, next_parent); - QLIST_INSERT_HEAD(&to->parents, c, next_parent); bdrv_ref(to); + bdrv_replace_child(c, to); bdrv_unref(from); } } -- 1.8.3.1