From patchwork Mon Apr 1 10:01:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 232719 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8F23D2C00CE for ; Mon, 1 Apr 2013 21:04:42 +1100 (EST) Received: from localhost ([::1]:60556 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMbbU-0002X7-7T for incoming@patchwork.ozlabs.org; Mon, 01 Apr 2013 06:04:40 -0400 Received: from eggs.gnu.org ([208.118.235.92]:59456) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMbau-0002Rk-98 for qemu-devel@nongnu.org; Mon, 01 Apr 2013 06:04:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UMbap-0002ZC-5p for qemu-devel@nongnu.org; Mon, 01 Apr 2013 06:04:04 -0400 Received: from e28smtp07.in.ibm.com ([122.248.162.7]:35956) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMbao-0002YW-HE for qemu-devel@nongnu.org; Mon, 01 Apr 2013 06:03:59 -0400 Received: from /spool/local by e28smtp07.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 1 Apr 2013 15:29:43 +0530 Received: from d28dlp03.in.ibm.com (9.184.220.128) by e28smtp07.in.ibm.com (192.168.1.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 1 Apr 2013 15:29:39 +0530 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by d28dlp03.in.ibm.com (Postfix) with ESMTP id A97C3125804F for ; Mon, 1 Apr 2013 15:35:07 +0530 (IST) Received: from d28av03.in.ibm.com (d28av03.in.ibm.com [9.184.220.65]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r31A3hv81769812 for ; Mon, 1 Apr 2013 15:33:44 +0530 Received: from d28av03.in.ibm.com (loopback [127.0.0.1]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r31A3kGZ028712 for ; Mon, 1 Apr 2013 21:03:48 +1100 Received: from RH63Wenchao (wenchaox.cn.ibm.com [9.115.122.29]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r31A1YhY016168; Mon, 1 Apr 2013 21:03:45 +1100 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Mon, 1 Apr 2013 18:01:29 +0800 Message-Id: <1364810491-21404-2-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1364810491-21404-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1364810491-21404-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13040109-8878-0000-0000-000006874376 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.7 Cc: kwolf@redhat.com, pbonzini@redhat.com, Wenchao Xia , dietmar@proxmox.com, stefanha@gmail.com Subject: [Qemu-devel] [PATCH 1/3] block: add function deappend() 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 This function should be used to remove contents of active *bs on the top of backing chain, when top *bs was committed to bs->backing_hd or *bs was just appended. Signed-off-by: Wenchao Xia --- block.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ include/block/block.h | 1 + 2 files changed, 67 insertions(+), 0 deletions(-) diff --git a/block.c b/block.c index 0ae2e93..0cacc2f 100644 --- a/block.c +++ b/block.c @@ -1536,6 +1536,72 @@ void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) bs_new->drv ? bs_new->drv->format_name : ""); } +/* + * Remove top bs contents in the image backing chain while the chain is live. + * Required fields on the top layer will be kept to let qemu continue use + * @bs_top. + * + * This will modify the BlockDriverState fields, and swap contents between + * bs_top and bs_top->backing_hd. Both bs_top and bs_top->backing_hd are + * modified, and bs_top->backing_hd will be deleted. + * + * This function does not delete any image files. If @bs_top have no backing + * image, the function will do nothing. + * + * Warning: Caller must guarantee @bs_top have same contents with its backing + * image, that is, when @bs_top was just appended or committed to + * bs_top->backing_hd. + */ +void bdrv_deappend(BlockDriverState *bs_top) +{ + BlockDriverState tmp; + BlockDriverState *bs_backing = bs_top->backing_hd; + + if (!bs_backing) { + return; + } + + BlockDriverState *bs_bottom = bs_backing->backing_hd; + /* Now only support deappend when bs_top is in following conditions. */ + g_assert(bs_top->device_name[0] != '\0'); + g_assert(bs_top->dev != NULL); + + g_assert(bs_top->dirty_bitmap == NULL); + g_assert(bs_top->job == NULL); + g_assert(bs_top->in_use == 0); + g_assert(bs_top->io_limits_enabled == false); + g_assert(bs_top->block_timer == NULL); + + + tmp = *bs_top; + *bs_top = *bs_backing; + *bs_backing = tmp; + + /* there are some fields which should not be swapped, move them back */ + bdrv_move_feature_fields(&tmp, bs_top); + bdrv_move_feature_fields(bs_top, bs_backing); + bdrv_move_feature_fields(bs_backing, &tmp); + + /* bs_top should keep device attached */ + g_assert(bs_top->device_name[0] != '\0'); + g_assert(bs_top->dev != NULL); + + /* check a few fields that should remain */ + g_assert(bs_top->job == NULL); + g_assert(bs_top->in_use == 0); + g_assert(bs_top->io_limits_enabled == false); + g_assert(bs_top->block_timer == NULL); + g_assert(bs_top->backing_hd == bs_bottom); + + /* delete bs_backing */ + bs_backing->backing_hd = NULL; + bs_backing->backing_file[0] = 0; + bs_backing->backing_format[0] = 0; + bdrv_delete(bs_backing); + + bdrv_rebind(bs_top); +} + void bdrv_delete(BlockDriverState *bs) { assert(!bs->dev); diff --git a/include/block/block.h b/include/block/block.h index 9dc6aad..4450121 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -132,6 +132,7 @@ BlockDriverState *bdrv_new(const char *device_name); void bdrv_make_anon(BlockDriverState *bs); void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old); void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top); +void bdrv_deappend(BlockDriverState *bs_top); void bdrv_delete(BlockDriverState *bs); int bdrv_parse_cache_flags(const char *mode, int *flags); int bdrv_parse_discard_flags(const char *mode, int *flags);