From 466c80f21fa2a69eb5cff8048b824c4812179b8c Mon Sep 17 00:00:00 2001 From: Chrysostomos Nanakos Date: Tue, 9 Sep 2014 20:38:50 +0300 Subject: qemu-iotests: Run 025 for Archipelago block driver Run resize grow test to ensure that existing data is not lost during grow and new space is zeroed. Signed-off-by: Chrysostomos Nanakos Signed-off-by: Kevin Wolf --- tests/qemu-iotests/025 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/qemu-iotests/025 b/tests/qemu-iotests/025 index a5f45b454c..467a4b7090 100755 --- a/tests/qemu-iotests/025 +++ b/tests/qemu-iotests/025 @@ -40,7 +40,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.pattern _supported_fmt raw qcow2 qed -_supported_proto file sheepdog rbd nfs +_supported_proto file sheepdog rbd nfs archipelago _supported_os Linux echo "=== Creating image" -- cgit v1.2.3-55-g7522 From be2bfb9dbde59de27684623b9d8261262c5ad076 Mon Sep 17 00:00:00 2001 From: Stratos Psomadakis Date: Thu, 11 Sep 2014 18:19:29 +0300 Subject: iotests: Send the correct fd in socket_scm_helper Make sure to pass the correct fd via SCM_RIGHTS in socket_scm_helper.c (i.e. fd_to_send, not socket-fd). Signed-off-by: Stratos Psomadakis Signed-off-by: Dimitris Aragiorgis Signed-off-by: Kevin Wolf --- tests/qemu-iotests/socket_scm_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/qemu-iotests/socket_scm_helper.c b/tests/qemu-iotests/socket_scm_helper.c index 0e2b2859af..81959835eb 100644 --- a/tests/qemu-iotests/socket_scm_helper.c +++ b/tests/qemu-iotests/socket_scm_helper.c @@ -52,7 +52,7 @@ static int send_fd(int fd, int fd_to_send) cmsg->cmsg_len = CMSG_LEN(sizeof(int)); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; - memcpy(CMSG_DATA(cmsg), &fd, sizeof(int)); + memcpy(CMSG_DATA(cmsg), &fd_to_send, sizeof(int)); do { ret = sendmsg(fd, &msg, 0); -- cgit v1.2.3-55-g7522 From c2eb918e3299f930fd0d0ae48d002cf2599de250 Mon Sep 17 00:00:00 2001 From: Hu Tao Date: Wed, 10 Sep 2014 17:05:45 +0800 Subject: block: round up file size to nearest sector Currently the file size requested by user is rounded down to nearest sector, causing the actual file size could be a bit less than the size user requested. Since some formats (like qcow2) record virtual disk size in bytes, this can make the last few bytes cannot be accessed. This patch fixes it by rounding up file size to nearest sector so that the actual file size is no less than the requested file size. Signed-off-by: Hu Tao Reviewed-by: Kevin Wolf Reviewed-by: Eric Blake Reviewed-by: Max Reitz Signed-off-by: Kevin Wolf --- block/archipelago.c | 3 ++- block/cow.c | 3 ++- block/gluster.c | 4 +-- block/iscsi.c | 4 +-- block/nfs.c | 3 ++- block/qcow.c | 3 ++- block/qcow2.c | 3 ++- block/qed.c | 3 ++- block/raw-posix.c | 8 +++--- block/raw-win32.c | 4 +-- block/rbd.c | 3 ++- block/sheepdog.c | 3 ++- block/ssh.c | 3 ++- block/vdi.c | 3 ++- block/vhdx.c | 3 ++- block/vmdk.c | 3 ++- block/vpc.c | 3 ++- tests/qemu-iotests/104 | 57 ++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/104.out | 12 +++++++++ tests/qemu-iotests/common.filter | 21 +++++++++++++++ tests/qemu-iotests/group | 1 + 21 files changed, 127 insertions(+), 23 deletions(-) create mode 100755 tests/qemu-iotests/104 create mode 100644 tests/qemu-iotests/104.out (limited to 'tests') diff --git a/block/archipelago.c b/block/archipelago.c index 40e5f76ae6..93fb7c0634 100644 --- a/block/archipelago.c +++ b/block/archipelago.c @@ -708,7 +708,8 @@ static int qemu_archipelago_create(const char *filename, parse_filename_opts(filename, errp, &volname, &segment_name, &mport, &vport); - total_size = qemu_opt_get_size_del(options, BLOCK_OPT_SIZE, 0); + total_size = ROUND_UP(qemu_opt_get_size_del(options, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); if (segment_name == NULL) { segment_name = g_strdup("archipelago"); diff --git a/block/cow.c b/block/cow.c index 6ee483327f..c3769fe03b 100644 --- a/block/cow.c +++ b/block/cow.c @@ -335,7 +335,8 @@ static int cow_create(const char *filename, QemuOpts *opts, Error **errp) BlockDriverState *cow_bs = NULL; /* Read out options */ - image_sectors = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / 512; + image_sectors = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); image_filename = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); ret = bdrv_create_file(filename, opts, &local_err); diff --git a/block/gluster.c b/block/gluster.c index 1912cf9d07..65c7a58062 100644 --- a/block/gluster.c +++ b/block/gluster.c @@ -494,8 +494,8 @@ static int qemu_gluster_create(const char *filename, goto out; } - total_size = - qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZE; + total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); if (!tmp || !strcmp(tmp, "off")) { diff --git a/block/iscsi.c b/block/iscsi.c index 3e19202488..84bcae89fa 100644 --- a/block/iscsi.c +++ b/block/iscsi.c @@ -1531,8 +1531,8 @@ static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp) bs = bdrv_new("", &error_abort); /* Read out options */ - total_size = - qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZE; + total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); bs->opaque = g_new0(struct IscsiLun, 1); iscsilun = bs->opaque; diff --git a/block/nfs.c b/block/nfs.c index 194f301501..c76e368b95 100644 --- a/block/nfs.c +++ b/block/nfs.c @@ -418,7 +418,8 @@ static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp) client->aio_context = qemu_get_aio_context(); /* Read out options */ - total_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); ret = nfs_client_open(client, url, O_CREAT, errp); if (ret < 0) { diff --git a/block/qcow.c b/block/qcow.c index 67c237fe7d..041af26ce3 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -725,7 +725,8 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp) BlockDriverState *qcow_bs; /* Read out options */ - total_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / 512; + total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) { flags |= BLOCK_FLAG_ENCRYPT; diff --git a/block/qcow2.c b/block/qcow2.c index f9e045ff2b..c8050e527c 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1921,7 +1921,8 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp) int ret; /* Read out options */ - sectors = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / 512; + sectors = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) { diff --git a/block/qed.c b/block/qed.c index ba395af76a..f8d9e12263 100644 --- a/block/qed.c +++ b/block/qed.c @@ -648,7 +648,8 @@ static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp) char *backing_fmt = NULL; int ret; - image_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); cluster_size = qemu_opt_get_size_del(opts, diff --git a/block/raw-posix.c b/block/raw-posix.c index d737f3a0c5..9c22e3f45d 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -1369,8 +1369,8 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp) strstart(filename, "file:", &filename); /* Read out options */ - total_size = - qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZE; + total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false); fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, @@ -1966,8 +1966,8 @@ static int hdev_create(const char *filename, QemuOpts *opts, (void)has_prefix; /* Read out options */ - total_size = - qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZE; + total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); fd = qemu_open(filename, O_WRONLY | O_BINARY); if (fd < 0) { diff --git a/block/raw-win32.c b/block/raw-win32.c index 902eab6100..1e1880d528 100644 --- a/block/raw-win32.c +++ b/block/raw-win32.c @@ -511,8 +511,8 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp) strstart(filename, "file:", &filename); /* Read out options */ - total_size = - qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / 512; + total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); diff --git a/block/rbd.c b/block/rbd.c index ea969e7beb..b7f7d5ff30 100644 --- a/block/rbd.c +++ b/block/rbd.c @@ -314,7 +314,8 @@ static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp) } /* Read out options */ - bytes = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0); if (objsize) { if ((objsize - 1) & objsize) { /* not a power of 2? */ diff --git a/block/sheepdog.c b/block/sheepdog.c index f91afc3a5b..7da36e1f9a 100644 --- a/block/sheepdog.c +++ b/block/sheepdog.c @@ -1702,7 +1702,8 @@ static int sd_create(const char *filename, QemuOpts *opts, goto out; } - s->inode.vdi_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + s->inode.vdi_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); if (!buf || !strcmp(buf, "off")) { diff --git a/block/ssh.c b/block/ssh.c index cd2fd751fe..cf43bc0f89 100644 --- a/block/ssh.c +++ b/block/ssh.c @@ -700,7 +700,8 @@ static int ssh_create(const char *filename, QemuOpts *opts, Error **errp) ssh_state_init(&s); /* Get desired file size. */ - total_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); DPRINTF("total_size=%" PRIi64, total_size); uri_options = qdict_new(); diff --git a/block/vdi.c b/block/vdi.c index 4b10aacc3b..cfa08b0b93 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -700,7 +700,8 @@ static int vdi_create(const char *filename, QemuOpts *opts, Error **errp) logout("\n"); /* Read out options. */ - bytes = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); #if defined(CONFIG_VDI_BLOCK_SIZE) /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */ block_size = qemu_opt_get_size_del(opts, diff --git a/block/vhdx.c b/block/vhdx.c index 87c99fc260..796b7bd884 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -1766,7 +1766,8 @@ static int vhdx_create(const char *filename, QemuOpts *opts, Error **errp) VHDXImageType image_type; Error *local_err = NULL; - image_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); log_size = qemu_opt_get_size_del(opts, VHDX_BLOCK_OPT_LOG_SIZE, 0); block_size = qemu_opt_get_size_del(opts, VHDX_BLOCK_OPT_BLOCK_SIZE, 0); type = qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT); diff --git a/block/vmdk.c b/block/vmdk.c index a1cb91131e..afdea1a8b6 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1807,7 +1807,8 @@ static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp) goto exit; } /* Read out options */ - total_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); adapter_type = qemu_opt_get_del(opts, BLOCK_OPT_ADAPTER_TYPE); backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); if (qemu_opt_get_bool_del(opts, BLOCK_OPT_COMPAT6, false)) { diff --git a/block/vpc.c b/block/vpc.c index c024b4cf3f..4947369d48 100644 --- a/block/vpc.c +++ b/block/vpc.c @@ -757,7 +757,8 @@ static int vpc_create(const char *filename, QemuOpts *opts, Error **errp) BlockDriverState *bs = NULL; /* Read out options */ - total_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); disk_type_param = qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT); if (disk_type_param) { if (!strcmp(disk_type_param, "dynamic")) { diff --git a/tests/qemu-iotests/104 b/tests/qemu-iotests/104 new file mode 100755 index 0000000000..b471aa5bb1 --- /dev/null +++ b/tests/qemu-iotests/104 @@ -0,0 +1,57 @@ +#!/bin/bash +# +# Test image creation with aligned and unaligned sizes +# +# Copyright (C) 2014 Fujitsu. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# creator +owner=hutao@cn.fujitsu.com + +seq=`basename $0` +echo "QA output created by $seq" + +here=`pwd` +tmp=/tmp/$$ +status=1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +_supported_fmt generic +_supported_proto generic +_supported_os Linux + +echo "=== Check qemu-img info output ===" +echo +image_sizes="1024 1234" + +for s in $image_sizes; do + _make_test_img $s | _filter_img_create + _img_info | _filter_img_info +done + +# success, all done +echo "*** done" +rm -f $seq.full +status=0 diff --git a/tests/qemu-iotests/104.out b/tests/qemu-iotests/104.out new file mode 100644 index 0000000000..de27852e35 --- /dev/null +++ b/tests/qemu-iotests/104.out @@ -0,0 +1,12 @@ +QA output created by 104 +=== Check qemu-img info output === + +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1024 +image: TEST_DIR/t.IMGFMT +file format: IMGFMT +virtual size: 1.0K (1024 bytes) +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1234 +image: TEST_DIR/t.IMGFMT +file format: IMGFMT +virtual size: 1.5K (1536 bytes) +***done diff --git a/tests/qemu-iotests/common.filter b/tests/qemu-iotests/common.filter index 51192c8511..f69cb6b916 100644 --- a/tests/qemu-iotests/common.filter +++ b/tests/qemu-iotests/common.filter @@ -192,5 +192,26 @@ _filter_img_create() -e "s/archipelago:a/TEST_DIR\//g" } +_filter_img_info() +{ + sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \ + -e "s#$TEST_DIR#TEST_DIR#g" \ + -e "s#$IMGFMT#IMGFMT#g" \ + -e "/encrypted: yes/d" \ + -e "/cluster_size: [0-9]\\+/d" \ + -e "/table_size: [0-9]\\+/d" \ + -e "/compat: '[^']*'/d" \ + -e "/compat6: \\(on\\|off\\)/d" \ + -e "/static: \\(on\\|off\\)/d" \ + -e "/zeroed_grain: \\(on\\|off\\)/d" \ + -e "/subformat: '[^']*'/d" \ + -e "/adapter_type: '[^']*'/d" \ + -e "/lazy_refcounts: \\(on\\|off\\)/d" \ + -e "/block_size: [0-9]\\+/d" \ + -e "/block_state_zero: \\(on\\|off\\)/d" \ + -e "/log_size: [0-9]\\+/d" \ + -e "s/archipelago:a/TEST_DIR\//g" +} + # make sure this script returns success /bin/true diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index 0920b28db4..622685e94c 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -104,3 +104,4 @@ 100 rw auto quick 101 rw auto quick 103 rw auto quick +104 rw auto -- cgit v1.2.3-55-g7522 From ffeaac9b4e23a3033e8120cc34bacadc09487f1b Mon Sep 17 00:00:00 2001 From: Hu Tao Date: Wed, 10 Sep 2014 17:05:47 +0800 Subject: qapi: introduce PreallocMode and new PreallocModes full and falloc. This patch prepares for the subsequent patches. Signed-off-by: Hu Tao Reviewed-by: Max Reitz Reviewed-by: Kevin Wolf Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block/qcow2.c | 28 ++++++++++++++++++---------- qapi/block-core.json | 17 +++++++++++++++++ tests/qemu-iotests/049.out | 2 +- 3 files changed, 36 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/block/qcow2.c b/block/qcow2.c index cf27c3f422..2d68b51f14 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -30,6 +30,7 @@ #include "qemu/error-report.h" #include "qapi/qmp/qerror.h" #include "qapi/qmp/qbool.h" +#include "qapi/util.h" #include "trace.h" #include "qemu/option_int.h" @@ -1738,7 +1739,7 @@ static int preallocate(BlockDriverState *bs) static int qcow2_create2(const char *filename, int64_t total_size, const char *backing_file, const char *backing_format, - int flags, size_t cluster_size, int prealloc, + int flags, size_t cluster_size, PreallocMode prealloc, QemuOpts *opts, int version, Error **errp) { @@ -1876,7 +1877,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) { + if (prealloc == PREALLOC_MODE_METADATA) { BDRVQcowState *s = bs->opaque; qemu_co_mutex_lock(&s->lock); ret = preallocate(bs); @@ -1915,7 +1916,7 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp) uint64_t size = 0; int flags = 0; size_t cluster_size = DEFAULT_CLUSTER_SIZE; - int prealloc = 0; + PreallocMode prealloc; int version = 3; Error *local_err = NULL; int ret; @@ -1931,12 +1932,11 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp) cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, DEFAULT_CLUSTER_SIZE); buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); - if (!buf || !strcmp(buf, "off")) { - prealloc = 0; - } else if (!strcmp(buf, "metadata")) { - prealloc = 1; - } else { - error_setg(errp, "Invalid preallocation mode: '%s'", buf); + prealloc = qapi_enum_parse(PreallocMode_lookup, buf, + PREALLOC_MODE_MAX, PREALLOC_MODE_OFF, + &local_err); + if (local_err) { + error_propagate(errp, local_err); ret = -EINVAL; goto finish; } @@ -1958,7 +1958,15 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp) flags |= BLOCK_FLAG_LAZY_REFCOUNTS; } - if (backing_file && prealloc) { + if (prealloc != PREALLOC_MODE_OFF && + prealloc != PREALLOC_MODE_METADATA) { + ret = -EINVAL; + error_setg(errp, "Unsupported preallocate mode: %s", + PreallocMode_lookup[prealloc]); + goto finish; + } + + if (backing_file && prealloc != PREALLOC_MODE_OFF) { error_setg(errp, "Backing file and preallocation cannot be used at " "the same time"); ret = -EINVAL; diff --git a/qapi/block-core.json b/qapi/block-core.json index 4064d5bf7d..95dcd81ed4 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -1708,3 +1708,20 @@ 'len' : 'int', 'offset': 'int', 'speed' : 'int' } } + +# @PreallocMode +# +# Preallocation mode of QEMU image file +# +# @off: no preallocation +# @metadata: preallocate only for metadata +# @falloc: like @full preallocation but allocate disk space by +# posix_fallocate() rather than writing zeros. +# @full: preallocate all data by writing zeros to device to ensure disk +# space is really available. @full preallocation also sets up +# metadata correctly. +# +# Since 2.2 +## +{ 'enum': 'PreallocMode', + 'data': [ 'off', 'metadata', 'falloc', 'full' ] } diff --git a/tests/qemu-iotests/049.out b/tests/qemu-iotests/049.out index 71ca44d76b..09ca0aed49 100644 --- a/tests/qemu-iotests/049.out +++ b/tests/qemu-iotests/049.out @@ -179,7 +179,7 @@ qemu-img create -f qcow2 -o preallocation=metadata TEST_DIR/t.qcow2 64M Formatting 'TEST_DIR/t.qcow2', fmt=qcow2 size=67108864 encryption=off cluster_size=65536 preallocation='metadata' lazy_refcounts=off qemu-img create -f qcow2 -o preallocation=1234 TEST_DIR/t.qcow2 64M -qemu-img: TEST_DIR/t.qcow2: Invalid preallocation mode: '1234' +qemu-img: TEST_DIR/t.qcow2: invalid parameter value: 1234 Formatting 'TEST_DIR/t.qcow2', fmt=qcow2 size=67108864 encryption=off cluster_size=65536 preallocation='1234' lazy_refcounts=off == Check encryption option == -- cgit v1.2.3-55-g7522 From 0e4271b711a8ea766d29824c844e268b91ac3ae5 Mon Sep 17 00:00:00 2001 From: Hu Tao Date: Wed, 10 Sep 2014 17:05:49 +0800 Subject: qcow2: Add falloc and full preallocation option preallocation=falloc allocates disk space by posix_fallocate(), preallocation=full allocates disk space by writing zeros to disk. Both modes imply preallocation=metadata. Signed-off-by: Hu Tao Reviewed-by: Max Reitz Signed-off-by: Kevin Wolf --- block/qcow2.c | 63 ++++++++++++++++++++++++++++++++++++++-------- qemu-doc.texi | 8 +++--- qemu-img.texi | 8 +++--- tests/qemu-iotests/082.out | 54 +++++++++++++++++++-------------------- 4 files changed, 90 insertions(+), 43 deletions(-) (limited to 'tests') diff --git a/block/qcow2.c b/block/qcow2.c index 2d68b51f14..0daf25cb58 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1772,6 +1772,56 @@ static int qcow2_create2(const char *filename, int64_t total_size, Error *local_err = NULL; int ret; + if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) { + int64_t meta_size = 0; + uint64_t nreftablee, nrefblocke, nl1e, nl2e; + int64_t aligned_total_size = align_offset(total_size, cluster_size); + + /* header: 1 cluster */ + meta_size += cluster_size; + + /* total size of L2 tables */ + nl2e = aligned_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 = (aligned_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); + + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, + aligned_total_size + meta_size); + qemu_opt_set(opts, BLOCK_OPT_PREALLOC, PreallocMode_lookup[prealloc]); + } + ret = bdrv_create_file(filename, opts, &local_err); if (ret < 0) { error_propagate(errp, local_err); @@ -1877,7 +1927,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 == PREALLOC_MODE_METADATA) { + if (prealloc != PREALLOC_MODE_OFF) { BDRVQcowState *s = bs->opaque; qemu_co_mutex_lock(&s->lock); ret = preallocate(bs); @@ -1958,14 +2008,6 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp) flags |= BLOCK_FLAG_LAZY_REFCOUNTS; } - if (prealloc != PREALLOC_MODE_OFF && - prealloc != PREALLOC_MODE_METADATA) { - ret = -EINVAL; - error_setg(errp, "Unsupported preallocate mode: %s", - PreallocMode_lookup[prealloc]); - goto finish; - } - if (backing_file && prealloc != PREALLOC_MODE_OFF) { error_setg(errp, "Backing file and preallocation cannot be used at " "the same time"); @@ -2526,7 +2568,8 @@ static QemuOptsList qcow2_create_opts = { { .name = BLOCK_OPT_PREALLOC, .type = QEMU_OPT_STRING, - .help = "Preallocation mode (allowed values: off, metadata)" + .help = "Preallocation mode (allowed values: off, metadata, " + "falloc, full)" }, { .name = BLOCK_OPT_LAZY_REFCOUNTS, diff --git a/qemu-doc.texi b/qemu-doc.texi index 1f289d6a08..ef3be72ae2 100644 --- a/qemu-doc.texi +++ b/qemu-doc.texi @@ -584,9 +584,11 @@ sizes can improve the image file size whereas larger cluster sizes generally provide better performance. @item preallocation -Preallocation mode (allowed values: off, metadata). An image with preallocated -metadata is initially larger but can improve performance when the image needs -to grow. +Preallocation mode (allowed values: @code{off}, @code{metadata}, @code{falloc}, +@code{full}). An image with preallocated metadata is initially larger but can +improve performance when the image needs to grow. @code{falloc} and @code{full} +preallocations are like the same options of @code{raw} format, but sets up +metadata also. @item lazy_refcounts If this option is set to @code{on}, reference count updates are postponed with diff --git a/qemu-img.texi b/qemu-img.texi index d64d05edda..50d2cca7a0 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -476,9 +476,11 @@ sizes can improve the image file size whereas larger cluster sizes generally provide better performance. @item preallocation -Preallocation mode (allowed values: off, metadata). An image with preallocated -metadata is initially larger but can improve performance when the image needs -to grow. +Preallocation mode (allowed values: @code{off}, @code{metadata}, @code{falloc}, +@code{full}). An image with preallocated metadata is initially larger but can +improve performance when the image needs to grow. @code{falloc} and @code{full} +preallocations are like the same options of @code{raw} format, but sets up +metadata also. @item lazy_refcounts If this option is set to @code{on}, reference count updates are postponed with diff --git a/tests/qemu-iotests/082.out b/tests/qemu-iotests/082.out index 413e7ef391..90c21c893b 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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -76,7 +76,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -88,7 +88,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -100,7 +100,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -112,7 +112,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -124,7 +124,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -136,7 +136,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -148,7 +148,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -175,7 +175,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, falloc, full) lazy_refcounts Postpone refcount updates Testing: create -o help @@ -253,7 +253,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -265,7 +265,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -277,7 +277,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -301,7 +301,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -313,7 +313,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -325,7 +325,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -337,7 +337,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -364,7 +364,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, falloc, full) lazy_refcounts Postpone refcount updates Testing: convert -o help @@ -431,7 +431,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -443,7 +443,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -455,7 +455,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -467,7 +467,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -479,7 +479,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -491,7 +491,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -503,7 +503,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -515,7 +515,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, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -544,7 +544,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, falloc, full) lazy_refcounts Postpone refcount updates Testing: convert -o help -- cgit v1.2.3-55-g7522