From patchwork Tue Feb 11 07:07:10 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hu Tao X-Patchwork-Id: 319159 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 777902C00B9 for ; Tue, 11 Feb 2014 18:09:50 +1100 (EST) Received: from localhost ([::1]:59941 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WD7TX-0006uT-UU for incoming@patchwork.ozlabs.org; Tue, 11 Feb 2014 02:09:47 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37205) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WD7Sz-0006sw-5D for qemu-devel@nongnu.org; Tue, 11 Feb 2014 02:09:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WD7Ss-0003iX-P2 for qemu-devel@nongnu.org; Tue, 11 Feb 2014 02:09:13 -0500 Received: from [222.73.24.84] (port=62390 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WD7Ss-0003gj-0d for qemu-devel@nongnu.org; Tue, 11 Feb 2014 02:09:06 -0500 X-IronPort-AV: E=Sophos;i="4.95,824,1384272000"; d="scan'208";a="9505138" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 11 Feb 2014 15:05:13 +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 s1B78uX7026070; Tue, 11 Feb 2014 15:08:59 +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 2014021115070094-1728899 ; Tue, 11 Feb 2014 15:07:00 +0800 From: Hu Tao To: qemu-devel@nongnu.org Date: Tue, 11 Feb 2014 15:07:10 +0800 Message-Id: <71ae41bbde58d21e1091abc3e2891e074d029a89.1392102900.git.hutao@cn.fujitsu.com> X-Mailer: git-send-email 1.8.5.2.229.g4448466 In-Reply-To: References: X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2014/02/11 15:07:01, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2014/02/11 15:07:05, Serialize complete at 2014/02/11 15:07:05 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 222.73.24.84 Cc: Kevin Wolf , Stefan Hajnoczi , Fam Zheng , Peter Lieven Subject: [Qemu-devel] [PATCH v5 RESEND 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 | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index e4bab70..4b113b7 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1456,6 +1456,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; @@ -1485,16 +1486,80 @@ 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 nreftablee, nrefblocke, nl1e, nl2e; + BlockDriver *drv; + + total_size = align_offset(total_size, cluster_size); + + 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 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 */ + nl1e = nl2e * sizeof(uint64_t) / cluster_size; + nl1e = align_offset(nl1e, cluster_size / sizeof(uint64_t)); + meta_size += nl1e * sizeof(uint64_t); + + /* total size of refcount blocks + * + * note: every host cluster is reference-counted, including metadata + * (even refcount blocks are recursively included). + * Let: + * a = total_size (this is the guest disk size) + * m = meta size not including refcount blocks and refcount tables + * c = cluster size + * y1 = number of refcount blocks entries + * y2 = meta size including everything + * then, + * y1 = (y2 + a)/c + * y2 = y1 * sizeof(u16) + y1 * sizeof(u16) * sizeof(u64) / c + m + * we can get y1: + * y1 = (a + m) / (c - sizeof(u16) - sizeof(u16) * sizeof(u64) / c) + */ + nrefblocke = (total_size + meta_size + cluster_size) / + (cluster_size - sizeof(uint16_t) - + 1.0 * sizeof(uint16_t) * sizeof(uint64_t) / cluster_size); + nrefblocke = align_offset(nrefblocke, cluster_size / sizeof(uint16_t)); + meta_size += nrefblocke * sizeof(uint16_t); + + /* total size of refcount tables */ + nreftablee = nrefblocke * sizeof(uint16_t) / cluster_size; + nreftablee = align_offset(nreftablee, cluster_size / sizeof(uint64_t)); + meta_size += nreftablee * sizeof(uint64_t); + + 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, NULL, BDRV_O_RDWR, &local_err); if (ret < 0) { error_propagate(errp, local_err); - return ret; + goto out_options; } /* Write the header */ @@ -1611,6 +1676,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; } @@ -1646,6 +1713,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); @@ -2211,7 +2280,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,