From patchwork Thu Jan 27 15:52:14 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 80709 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 6BBF7B7124 for ; Fri, 28 Jan 2011 02:52:56 +1100 (EST) Received: from localhost ([127.0.0.1]:59312 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PiU9V-0008F6-HC for incoming@patchwork.ozlabs.org; Thu, 27 Jan 2011 10:52:53 -0500 Received: from [140.186.70.92] (port=59107 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PiU7K-0006ZW-Ig for qemu-devel@nongnu.org; Thu, 27 Jan 2011 10:50:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PiU7J-0000lV-AZ for qemu-devel@nongnu.org; Thu, 27 Jan 2011 10:50:38 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41685) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PiU7J-0000lQ-2L for qemu-devel@nongnu.org; Thu, 27 Jan 2011 10:50:37 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p0RFoaDN017326 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 27 Jan 2011 10:50:36 -0500 Received: from dhcp-5-188.str.redhat.com (dhcp-5-175.str.redhat.com [10.32.5.175]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p0RFoY7V005320; Thu, 27 Jan 2011 10:50:35 -0500 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Thu, 27 Jan 2011 16:52:14 +0100 Message-Id: <1296143534-13495-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: kwolf@redhat.com Subject: [Qemu-devel] [PATCH] qcow2: Add full image preallocation option X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This adds a preallocation=full mode to qcow2 image creation, which does not only allocate metadata for the whole image, but also writes zeros to it, creating a non-sparse image file. Signed-off-by: Kevin Wolf --- block/qcow2.c | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 40 insertions(+), 5 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index a1773e4..90cf2ca 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -838,7 +838,15 @@ static int qcow2_change_backing_file(BlockDriverState *bs, return qcow2_update_ext_header(bs, backing_file, backing_fmt); } -static int preallocate(BlockDriverState *bs) +enum prealloc_mode { + PREALLOC_OFF = 0, + PREALLOC_METADATA, + PREALLOC_FULL, +}; + +#define IO_BUF_SIZE (2 * 1024 * 1024) + +static int preallocate(BlockDriverState *bs, enum prealloc_mode mode) { uint64_t nb_sectors; uint64_t offset; @@ -846,11 +854,14 @@ static int preallocate(BlockDriverState *bs) int ret; QCowL2Meta meta; + assert(mode != PREALLOC_OFF); + nb_sectors = bdrv_getlength(bs) >> 9; offset = 0; QLIST_INIT(&meta.dependent_requests); meta.cluster_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, &meta); @@ -874,6 +885,28 @@ static int preallocate(BlockDriverState *bs) offset += num << 9; } + /* Then write zeros to the cluster data, if requested */ + if (mode == PREALLOC_FULL) { + void *buf = qemu_mallocz(IO_BUF_SIZE); + + nb_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; + offset = 0; + + while (nb_sectors) { + num = MIN(nb_sectors, IO_BUF_SIZE / BDRV_SECTOR_SIZE); + ret = bdrv_write(bs, offset >> BDRV_SECTOR_BITS, buf, num); + if (ret < 0) { + qemu_free(buf); + return ret; + } + + nb_sectors -= num; + offset += num << 9; + } + + qemu_free(buf); + } + /* * It is expected that the image file is large enough to actually contain * all of the allocated clusters (otherwise we get failing reads after @@ -1006,7 +1039,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, /* And if we're supposed to preallocate metadata, do that now */ if (prealloc) { - ret = preallocate(bs); + ret = preallocate(bs, prealloc); if (ret < 0) { goto out; } @@ -1043,9 +1076,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 { fprintf(stderr, "Invalid preallocation mode: '%s'\n", options->value.s); @@ -1336,7 +1371,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)" }, { NULL } };