From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50996) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WJDEb-0005Dx-4r for qemu-devel@nongnu.org; Thu, 27 Feb 2014 21:31:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WJDEW-0007d5-H9 for qemu-devel@nongnu.org; Thu, 27 Feb 2014 21:31:33 -0500 Received: from [222.73.24.84] (port=41553 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WJDEV-0007ax-H1 for qemu-devel@nongnu.org; Thu, 27 Feb 2014 21:31:28 -0500 From: Hu Tao Date: Fri, 28 Feb 2014 10:29:33 +0800 Message-Id: <8a4cecdbc5362583c4f45d0ea13356e52c42c2a7.1393554457.git.hutao@cn.fujitsu.com> In-Reply-To: References: Subject: [Qemu-devel] [PATCH v6 4/4] qcow2: Add full image preallocation option List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Fam Zheng , Peter Lieven , Stefan Hajnoczi 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 59c01f7..2aa6efa 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1458,6 +1458,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; @@ -1487,16 +1488,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 */ @@ -1613,6 +1678,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; } @@ -1648,6 +1715,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); @@ -2213,7 +2282,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, -- 1.8.0