From patchwork Tue Nov 12 07:47:22 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hu Tao X-Patchwork-Id: 290550 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 CAE042C008C for ; Tue, 12 Nov 2013 18:53:10 +1100 (EST) Received: from localhost ([::1]:41456 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vg8ma-00054d-Pu for incoming@patchwork.ozlabs.org; Tue, 12 Nov 2013 02:53:08 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50753) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vg8kF-0001Pf-48 for qemu-devel@nongnu.org; Tue, 12 Nov 2013 02:50:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vg8kA-0006Eo-Hk for qemu-devel@nongnu.org; Tue, 12 Nov 2013 02:50:43 -0500 Received: from [222.73.24.84] (port=30496 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vg8kA-0006E7-5N for qemu-devel@nongnu.org; Tue, 12 Nov 2013 02:50:38 -0500 X-IronPort-AV: E=Sophos;i="4.93,683,1378828800"; d="scan'208";a="9010907" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 12 Nov 2013 15:45:58 +0800 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id rAC7nKor026879; Tue, 12 Nov 2013 15:49:21 +0800 Received: from G08FNSTD100614.fnst.cn.fujitsu.com ([10.167.226.102]) by fnstmail02.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.3) with ESMTP id 2013111215473791-53910 ; Tue, 12 Nov 2013 15:47:37 +0800 From: Hu Tao To: Kevin Wolf , "Daniel P. Berrange" Date: Tue, 12 Nov 2013 15:47:22 +0800 Message-Id: <3bdffb8a85c3563a38f31846f4687b4c435730ab.1384227383.git.hutao@cn.fujitsu.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: References: X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2013/11/12 15:47:37, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2013/11/12 15:47:39, Serialize complete at 2013/11/12 15:47:39 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 222.73.24.84 Cc: qemu-devel@nongnu.org Subject: [Qemu-devel] [RFC PATCH 4/4] qcow2: Add full image preallocation option 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 adds a preallocation=full mode to qcow2 image creation, which creates a non-sparse image file. Signed-off-by: Hu Tao --- block/qcow2.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 359030f..d3ca6cf 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1385,7 +1385,13 @@ static int qcow2_change_backing_file(BlockDriverState *bs, return qcow2_update_header(bs); } -static int preallocate(BlockDriverState *bs) +enum prealloc_mode { + PREALLOC_OFF = 0, + PREALLOC_METADATA, + PREALLOC_FULL, +}; + +static int preallocate(BlockDriverState *bs, enum prealloc_mode mode) { uint64_t nb_sectors; uint64_t offset; @@ -1394,9 +1400,12 @@ static int preallocate(BlockDriverState *bs) int ret; QCowL2Meta *meta; + assert(mode != PREALLOC_OFF); + nb_sectors = bdrv_getlength(bs) >> 9; offset = 0; + /* First allocate metadata in _really_ big chunks */ while (nb_sectors) { num = MIN(nb_sectors, INT_MAX >> 9); ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, @@ -1424,6 +1433,11 @@ static int preallocate(BlockDriverState *bs) offset += num << 9; } + /* Then write zeros to the cluster data, if requested */ + if (mode == PREALLOC_FULL) { + bdrv_zero_init(bs->file, offset, bdrv_getlength(bs)); + } + /* * It is expected that the image file is large enough to actually contain * all of the allocated clusters (otherwise we get failing reads after @@ -1572,11 +1586,11 @@ static int qcow2_create2(const char *filename, int64_t total_size, } } - /* And if we're supposed to preallocate metadata, do that now */ + /* And if we're supposed to preallocate data, do that now */ if (prealloc) { BDRVQcowState *s = bs->opaque; qemu_co_mutex_lock(&s->lock); - ret = preallocate(bs); + ret = preallocate(bs, prealloc); qemu_co_mutex_unlock(&s->lock); if (ret < 0) { error_setg_errno(errp, -ret, "Could not preallocate metadata"); @@ -1629,9 +1643,11 @@ static int qcow2_create(const char *filename, QEMUOptionParameter *options, } } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { if (!options->value.s || !strcmp(options->value.s, "off")) { - prealloc = 0; + prealloc = PREALLOC_OFF; } else if (!strcmp(options->value.s, "metadata")) { - prealloc = 1; + prealloc = PREALLOC_METADATA; + } else if (!strcmp(options->value.s, "full")) { + prealloc = PREALLOC_FULL; } else { error_setg(errp, "Invalid preallocation mode: '%s'", options->value.s); @@ -2221,7 +2237,7 @@ static QEMUOptionParameter qcow2_create_options[] = { { .name = BLOCK_OPT_PREALLOC, .type = OPT_STRING, - .help = "Preallocation mode (allowed values: off, metadata)" + .help = "Preallocation mode (allowed values: off, metadata, full)" }, { .name = BLOCK_OPT_LAZY_REFCOUNTS,