From patchwork Mon May 23 16:55:12 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 9132163 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4BACA607D5 for ; Mon, 23 May 2016 16:55:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4131F28245 for ; Mon, 23 May 2016 16:55:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3634128247; Mon, 23 May 2016 16:55:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E2D4928245 for ; Mon, 23 May 2016 16:55:58 +0000 (UTC) Received: from localhost ([::1]:49369 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b4t94-0004DO-3C for patchwork-qemu-devel@patchwork.kernel.org; Mon, 23 May 2016 12:55:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35792) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b4t8g-00049z-5S for qemu-devel@nongnu.org; Mon, 23 May 2016 12:55:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b4t8f-0007JI-AJ for qemu-devel@nongnu.org; Mon, 23 May 2016 12:55:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53353) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b4t8T-0007H8-ND; Mon, 23 May 2016 12:55:21 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 283E1811CA; Mon, 23 May 2016 16:55:21 +0000 (UTC) Received: from noname.redhat.com (ovpn-116-74.ams2.redhat.com [10.36.116.74]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u4NGtGhQ018979; Mon, 23 May 2016 12:55:19 -0400 From: Kevin Wolf To: qemu-block@nongnu.org Date: Mon, 23 May 2016 18:55:12 +0200 Message-Id: <1464022515-11390-2-git-send-email-kwolf@redhat.com> In-Reply-To: <1464022515-11390-1-git-send-email-kwolf@redhat.com> References: <1464022515-11390-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 23 May 2016 16:55:21 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/4] block: Introduce bdrv_replace_child() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, pbonzini@redhat.com, famz@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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); } }