From 6d2e2c68be47549a9773539a32748a3e327d4c58 Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Fri, 28 Jan 2011 16:32:17 -0200 Subject: [RHEL6 qemu-kvm PATCH 12/14] qcow2: Add full image preallocation option RH-Author: Kevin Wolf Message-id: <1296232337-27442-1-git-send-email-kwolf@redhat.com> Patchwork-id: 17219 O-Subject: [RHEL-6.1 qemu-kvm PATCH] qcow2: Add full image preallocation option Bugzilla: 634652 RH-Acked-by: Christoph Hellwig RH-Acked-by: Markus Armbruster RH-Acked-by: Juan Quintela RH-Acked-by: Jes Sorensen Bugzilla: 634652 Upstream status: Submitted 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(-) Signed-off-by: Eduardo Habkost --- block/qcow2.c | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 40 insertions(+), 5 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 948e1e9..f502491 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -837,7 +837,15 @@ static int get_bits_from_size(size_t size) } -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; @@ -845,11 +853,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); @@ -873,6 +884,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 @@ -1120,7 +1153,7 @@ exit: BlockDriverState *bs; bs = bdrv_new(""); bdrv_open(bs, filename, BDRV_O_CACHE_WB | BDRV_O_RDWR, &bdrv_qcow2); - ret = preallocate(bs); + ret = preallocate(bs, prealloc); bdrv_close(bs); } @@ -1152,9 +1185,11 @@ static int qcow_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); @@ -1399,7 +1434,7 @@ static QEMUOptionParameter qcow_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 } }; -- 1.7.3.2