From patchwork Tue Jul 2 05:59:46 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 256283 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5C3CA2C0089 for ; Tue, 2 Jul 2013 16:04:34 +1000 (EST) Received: from localhost ([::1]:44336 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UtthY-0002sA-Ct for incoming@patchwork.ozlabs.org; Tue, 02 Jul 2013 02:04:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37981) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UttfA-0007eY-RZ for qemu-devel@nongnu.org; Tue, 02 Jul 2013 02:02:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Uttf8-0000ca-5c for qemu-devel@nongnu.org; Tue, 02 Jul 2013 02:02:04 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52631) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uttf7-0000cV-Ui for qemu-devel@nongnu.org; Tue, 02 Jul 2013 02:02:02 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r62621nj004627 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 2 Jul 2013 02:02:01 -0400 Received: from t430s.redhat.com ([10.66.6.13]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r6261C6R010989; Tue, 2 Jul 2013 02:01:53 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Tue, 2 Jul 2013 13:59:46 +0800 Message-Id: <1372744789-997-5-git-send-email-famz@redhat.com> In-Reply-To: <1372744789-997-1-git-send-email-famz@redhat.com> References: <1372744789-997-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, famz@redhat.com, obarenbo@redhat.com, roliveri@redhat.com, hbrock@redhat.com, rjones@redhat.com, armbru@redhat.com, pmyers@redhat.com, imain@redhat.com, stefanha@redhat.com, pbonzini@redhat.com Subject: [Qemu-devel] [PATCH 4/7] block: simplify bdrv_drop_intermediate X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Fam Zheng --- block.c | 71 ++++++++++------------------------------------------------------- 1 file changed, 11 insertions(+), 60 deletions(-) diff --git a/block.c b/block.c index d1ce570..ae5de17 100644 --- a/block.c +++ b/block.c @@ -2015,12 +2015,6 @@ BlockDriverState *bdrv_find_overlay(BlockDriverState *active, return overlay; } -typedef struct BlkIntermediateStates { - BlockDriverState *bs; - QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; -} BlkIntermediateStates; - - /* * Drops images above 'base' up to and including 'top', and sets the image * above 'top' to have base as its backing file. @@ -2050,15 +2044,9 @@ typedef struct BlkIntermediateStates { int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, BlockDriverState *base) { - BlockDriverState *intermediate; - BlockDriverState *base_bs = NULL; BlockDriverState *new_top_bs = NULL; - BlkIntermediateStates *intermediate_state, *next; int ret = -EIO; - QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; - QSIMPLEQ_INIT(&states_to_delete); - if (!top->drv || !base->drv) { goto exit; } @@ -2070,58 +2058,21 @@ int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, goto exit; } - /* special case of new_top_bs->backing_hd already pointing to base - nothing - * to do, no intermediate images */ - if (new_top_bs->backing_hd == base) { - ret = 0; - goto exit; - } - - intermediate = top; - - /* now we will go down through the list, and add each BDS we find - * into our deletion queue, until we hit the 'base' - */ - while (intermediate) { - intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); - intermediate_state->bs = intermediate; - QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); - - if (intermediate->backing_hd == base) { - base_bs = intermediate->backing_hd; - break; + while (new_top_bs->backing_hd && new_top_bs->backing_hd != base) { + BlockDriverState *backing = new_top_bs->backing_hd; + if (backing == NULL) { + goto exit; } - intermediate = intermediate->backing_hd; - } - if (base_bs == NULL) { - /* something went wrong, we did not end at the base. safely - * unravel everything, and exit with error */ - goto exit; + new_top_bs->backing_hd = backing->backing_hd; + /* break backing_hd chain before releasing bs, so we don't free all the + * way up the backing chain */ + backing->backing_hd = NULL; + bdrv_put_ref(backing); } - /* success - we can delete the intermediate states, and link top->base */ - ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, - base_bs->drv ? base_bs->drv->format_name : ""); - if (ret) { - goto exit; - } - if (new_top_bs->backing_hd) { - bdrv_put_ref(new_top_bs->backing_hd); - } - new_top_bs->backing_hd = base_bs; - bdrv_get_ref(base_bs); - - QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { - /* so that bdrv_close() does not recursively close the chain */ - intermediate_state->bs->backing_hd = NULL; - bdrv_put_ref(intermediate_state->bs); - } - ret = 0; - + ret = bdrv_change_backing_file(new_top_bs, base->filename, + base->drv ? base->drv->format_name : ""); exit: - QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { - g_free(intermediate_state); - } return ret; }