From patchwork Mon Mar 17 06:53:38 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hu Tao X-Patchwork-Id: 330761 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 9B4332C00CD for ; Mon, 17 Mar 2014 17:56:31 +1100 (EST) Received: from localhost ([::1]:55889 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WPRTJ-0004hA-89 for incoming@patchwork.ozlabs.org; Mon, 17 Mar 2014 02:56:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46913) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WPRSg-0004Ow-GU for qemu-devel@nongnu.org; Mon, 17 Mar 2014 02:55:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WPRSb-0004o1-E8 for qemu-devel@nongnu.org; Mon, 17 Mar 2014 02:55:50 -0400 Received: from [222.73.24.84] (port=54736 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WPRSa-0004nR-56 for qemu-devel@nongnu.org; Mon, 17 Mar 2014 02:55:45 -0400 X-IronPort-AV: E=Sophos;i="4.97,668,1389715200"; d="scan'208";a="9710155" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 17 Mar 2014 14:51:41 +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 s2H6tUAK030511; Mon, 17 Mar 2014 14:55:33 +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 2014031714523302-675415 ; Mon, 17 Mar 2014 14:52:33 +0800 From: Hu Tao To: qemu-devel@nongnu.org Date: Mon, 17 Mar 2014 14:53:38 +0800 Message-Id: <6eff2de18cc1e860f31a1ea9c1afc71b8ee12f48.1395035846.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/03/17 14:52:33, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2014/03/17 14:52:41, Serialize complete at 2014/03/17 14:52:41 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 222.73.24.84 Cc: Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v7 for 2.0 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 ++++++++++++++++++++++++++++++++++++++++++++-- tests/qemu-iotests/082.out | 54 ++++++++++++++++----------------- 2 files changed, 99 insertions(+), 30 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 33ecb8a..c4bb9f5 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1459,6 +1459,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; @@ -1488,10 +1489,74 @@ 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; } bs = NULL; @@ -1499,7 +1564,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, NULL, &local_err); if (ret < 0) { error_propagate(errp, local_err); - return ret; + goto out_options; } /* Write the header */ @@ -1620,6 +1685,8 @@ out: if (bs) { bdrv_unref(bs); } +out_options: + free_option_parameters(alloc_options); return ret; } @@ -1655,6 +1722,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); @@ -2220,7 +2289,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, diff --git a/tests/qemu-iotests/082.out b/tests/qemu-iotests/082.out index 28309a0..5689802 100644 --- a/tests/qemu-iotests/082.out +++ b/tests/qemu-iotests/082.out @@ -64,7 +64,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: create -f qcow2 -o ? TEST_DIR/t.qcow2 128M @@ -75,7 +75,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: create -f qcow2 -o cluster_size=4k,help TEST_DIR/t.qcow2 128M @@ -86,7 +86,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: create -f qcow2 -o cluster_size=4k,? TEST_DIR/t.qcow2 128M @@ -97,7 +97,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: create -f qcow2 -o help,cluster_size=4k TEST_DIR/t.qcow2 128M @@ -108,7 +108,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: create -f qcow2 -o ?,cluster_size=4k TEST_DIR/t.qcow2 128M @@ -119,7 +119,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: create -f qcow2 -o cluster_size=4k -o help TEST_DIR/t.qcow2 128M @@ -130,7 +130,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: create -f qcow2 -o cluster_size=4k -o ? TEST_DIR/t.qcow2 128M @@ -141,7 +141,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: create -f qcow2 -o backing_file=TEST_DIR/t.qcow2,,help TEST_DIR/t.qcow2 128M @@ -167,7 +167,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: create -o help @@ -245,7 +245,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: convert -O qcow2 -o ? TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base @@ -256,7 +256,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: convert -O qcow2 -o cluster_size=4k,help TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base @@ -267,7 +267,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: convert -O qcow2 -o cluster_size=4k,? TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base @@ -278,7 +278,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: convert -O qcow2 -o help,cluster_size=4k TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base @@ -289,7 +289,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: convert -O qcow2 -o ?,cluster_size=4k TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base @@ -300,7 +300,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: convert -O qcow2 -o cluster_size=4k -o help TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base @@ -311,7 +311,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: convert -O qcow2 -o cluster_size=4k -o ? TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base @@ -322,7 +322,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: convert -O qcow2 -o backing_file=TEST_DIR/t.qcow2,,help TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base @@ -348,7 +348,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: convert -o help @@ -415,7 +415,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: amend -f qcow2 -o ? TEST_DIR/t.qcow2 @@ -426,7 +426,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: amend -f qcow2 -o cluster_size=4k,help TEST_DIR/t.qcow2 @@ -437,7 +437,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: amend -f qcow2 -o cluster_size=4k,? TEST_DIR/t.qcow2 @@ -448,7 +448,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: amend -f qcow2 -o help,cluster_size=4k TEST_DIR/t.qcow2 @@ -459,7 +459,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: amend -f qcow2 -o ?,cluster_size=4k TEST_DIR/t.qcow2 @@ -470,7 +470,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: amend -f qcow2 -o cluster_size=4k -o help TEST_DIR/t.qcow2 @@ -481,7 +481,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: amend -f qcow2 -o cluster_size=4k -o ? TEST_DIR/t.qcow2 @@ -492,7 +492,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: amend -f qcow2 -o backing_file=TEST_DIR/t.qcow2,,help TEST_DIR/t.qcow2 @@ -520,7 +520,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, full) lazy_refcounts Postpone refcount updates Testing: convert -o help