From patchwork Fri Dec 27 03:05:54 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hu Tao X-Patchwork-Id: 305342 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)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 111E02C009F for ; Fri, 27 Dec 2013 14:08:57 +1100 (EST) Received: from localhost ([::1]:47720 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VwNnC-0006yS-0B for incoming@patchwork.ozlabs.org; Thu, 26 Dec 2013 22:08:54 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49237) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VwNmg-0006su-Bd for qemu-devel@nongnu.org; Thu, 26 Dec 2013 22:08:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VwNmX-0003tm-Qd for qemu-devel@nongnu.org; Thu, 26 Dec 2013 22:08:22 -0500 Received: from [222.73.24.84] (port=61560 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VwNmX-0003pz-FT for qemu-devel@nongnu.org; Thu, 26 Dec 2013 22:08:13 -0500 X-IronPort-AV: E=Sophos;i="4.95,558,1384272000"; d="scan'208";a="9334147" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 27 Dec 2013 11:04:23 +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 rBR37tDi016971; Fri, 27 Dec 2013 11:07:57 +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 2013122711071170-736421 ; Fri, 27 Dec 2013 11:07:11 +0800 From: Hu Tao To: qemu-devel@nongnu.org Date: Fri, 27 Dec 2013 11:05:54 +0800 Message-Id: <25565d3a53f29829dc2d97480cb19ba34be49895.1388112645.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/12/27 11:07:11, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2013/12/27 11:07:15, Serialize complete at 2013/12/27 11:07:15 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 222.73.24.84 Cc: Kevin Wolf , Fam Zheng , Peter Lieven , Stefan Hajnoczi Subject: [Qemu-devel] [RFC PATCH v4 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 | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index f0a8d87..f601089 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1448,6 +1448,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, QEMUOptionParameter *options, int version, Error **errp) { + QEMUOptionParameter *alloc_options = NULL; /* Calculate cluster_bits */ int cluster_bits; cluster_bits = ffs(cluster_size) - 1; @@ -1477,16 +1478,53 @@ static int qcow2_create2(const char *filename, int64_t total_size, Error *local_err = NULL; int ret; + if (prealloc == PREALLOC_MODE_FULL) { + int64_t meta_size = 0; + unsigned nrefe, nl2e; + BlockDriver *drv; + + drv = bdrv_find_protocol(filename, true); + if (drv == NULL) { + error_setg(errp, "Could not find protocol for file '%s'", filename); + return -ENOENT; + } + + alloc_options = append_option_parameters(alloc_options, + drv->create_options); + alloc_options = append_option_parameters(alloc_options, options); + + /* header: 1 cluster */ + meta_size += cluster_size; + /* total size of refblocks */ + nrefe = (total_size / cluster_size); + nrefe = align_offset(nrefe, cluster_size / sizeof(uint16_t)); + meta_size += nrefe * sizeof(uint16_t); + /* total size of reftables */ + meta_size += nrefe * sizeof(uint16_t) * sizeof(uint16_t) / cluster_size; + /* total size of L2 tables */ + nl2e = total_size / cluster_size; + nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t)); + meta_size += nl2e * sizeof(uint64_t); + /* total size of L1 tables */ + meta_size += nl2e * sizeof(uint64_t) * sizeof(uint64_t) / cluster_size; + + set_option_parameter_int(alloc_options, BLOCK_OPT_SIZE, + total_size + meta_size); + set_option_parameter(alloc_options, BLOCK_OPT_PREALLOC, "full"); + + options = alloc_options; + } + ret = bdrv_create_file(filename, options, &local_err); if (ret < 0) { error_propagate(errp, local_err); - return ret; + goto out_options; } ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err); if (ret < 0) { error_propagate(errp, local_err); - return ret; + goto out_options; } /* Write the header */ @@ -1603,6 +1641,8 @@ static int qcow2_create2(const char *filename, int64_t total_size, ret = 0; out: bdrv_unref(bs); +out_options: + free_option_parameters(alloc_options); return ret; } @@ -1638,6 +1678,8 @@ static int qcow2_create(const char *filename, QEMUOptionParameter *options, prealloc = PREALLOC_MODE_OFF; } else if (!strcmp(options->value.s, "metadata")) { prealloc = PREALLOC_MODE_METADATA; + } else if (!strcmp(options->value.s, "full")) { + prealloc = PREALLOC_MODE_FULL; } else { error_setg(errp, "Invalid preallocation mode: '%s'", options->value.s); @@ -2223,7 +2265,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,