From nobody Wed Dec 17 21:46:05 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520960408377823.7517605768248; Tue, 13 Mar 2018 10:00:08 -0700 (PDT) Received: from localhost ([::1]:41262 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evnHT-0003zE-JT for importer@patchew.org; Tue, 13 Mar 2018 13:00:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60189) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evmdg-00020L-CN for qemu-devel@nongnu.org; Tue, 13 Mar 2018 12:19:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evmda-0004z6-DU for qemu-devel@nongnu.org; Tue, 13 Mar 2018 12:19:00 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:39908 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1evmdU-0004pf-2w; Tue, 13 Mar 2018 12:18:48 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B55B58151D49; Tue, 13 Mar 2018 16:18:47 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-214.ams2.redhat.com [10.36.117.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id E681D215CDAF; Tue, 13 Mar 2018 16:18:46 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Mar 2018 17:18:00 +0100 Message-Id: <20180313161803.1814-39-kwolf@redhat.com> In-Reply-To: <20180313161803.1814-1-kwolf@redhat.com> References: <20180313161803.1814-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 13 Mar 2018 16:18:47 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 13 Mar 2018 16:18:47 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 38/41] vhdx: Support .bdrv_co_create X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This adds the .bdrv_co_create driver callback to vhdx, which enables image creation over QMP. Signed-off-by: Kevin Wolf Reviewed-by: Max Reitz --- qapi/block-core.json | 40 +++++++++- block/vhdx.c | 216 ++++++++++++++++++++++++++++++++++++++---------= ---- 2 files changed, 203 insertions(+), 53 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index d091817855..350094f46a 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3842,6 +3842,44 @@ '*static': 'bool' } } =20 ## +# @BlockdevVhdxSubformat: +# +# @dynamic: Growing image file +# @fixed: Preallocated fixed-size image file +# +# Since: 2.12 +## +{ 'enum': 'BlockdevVhdxSubformat', + 'data': [ 'dynamic', 'fixed' ] } + +## +# @BlockdevCreateOptionsVhdx: +# +# Driver specific image creation options for vhdx. +# +# @file Node to create the image format on +# @size Size of the virtual disk in bytes +# @log-size Log size in bytes, must be a multiple of 1 MB +# (default: 1 MB) +# @block-size Block size in bytes, must be a multiple of 1 MB and not +# larger than 256 MB (default: automatically choose a bl= ock +# size depending on the image size) +# @subformat vhdx subformat (default: dynamic) +# @block-state-zero Force use of payload blocks of type 'ZERO'. Non-standa= rd, +# but default. Do not set to 'off' when using 'qemu-img +# convert' with subformat=3Ddynamic. +# +# Since: 2.12 +## +{ 'struct': 'BlockdevCreateOptionsVhdx', + 'data': { 'file': 'BlockdevRef', + 'size': 'size', + '*log-size': 'size', + '*block-size': 'size', + '*subformat': 'BlockdevVhdxSubformat', + '*block-state-zero': 'bool' } } + +## # @BlockdevCreateNotSupported: # # This is used for all drivers that don't support creating images. @@ -3896,7 +3934,7 @@ 'ssh': 'BlockdevCreateOptionsSsh', 'throttle': 'BlockdevCreateNotSupported', 'vdi': 'BlockdevCreateOptionsVdi', - 'vhdx': 'BlockdevCreateNotSupported', + 'vhdx': 'BlockdevCreateOptionsVhdx', 'vmdk': 'BlockdevCreateNotSupported', 'vpc': 'BlockdevCreateNotSupported', 'vvfat': 'BlockdevCreateNotSupported', diff --git a/block/vhdx.c b/block/vhdx.c index d82350d07c..f1b97f4b49 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -26,6 +26,9 @@ #include "block/vhdx.h" #include "migration/blocker.h" #include "qemu/uuid.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qapi-visit-block-core.h" =20 /* Options for VHDX creation */ =20 @@ -39,6 +42,8 @@ typedef enum VHDXImageType { VHDX_TYPE_DIFFERENCING, /* Currently unsupported */ } VHDXImageType; =20 +static QemuOptsList vhdx_create_opts; + /* Several metadata and region table data entries are identified by * guids in a MS-specific GUID format. */ =20 @@ -1792,59 +1797,71 @@ exit: * .---- ~ ----------- ~ ------------ ~ ---------------- ~ -----------. * 1MB */ -static int coroutine_fn vhdx_co_create_opts(const char *filename, QemuOpts= *opts, - Error **errp) +static int coroutine_fn vhdx_co_create(BlockdevCreateOptions *opts, + Error **errp) { + BlockdevCreateOptionsVhdx *vhdx_opts; + BlockBackend *blk =3D NULL; + BlockDriverState *bs =3D NULL; + int ret =3D 0; - uint64_t image_size =3D (uint64_t) 2 * GiB; - uint32_t log_size =3D 1 * MiB; - uint32_t block_size =3D 0; + uint64_t image_size; + uint32_t log_size; + uint32_t block_size; uint64_t signature; uint64_t metadata_offset; bool use_zero_blocks =3D false; =20 gunichar2 *creator =3D NULL; glong creator_items; - BlockBackend *blk; - char *type =3D NULL; VHDXImageType image_type; - Error *local_err =3D NULL; =20 - image_size =3D ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), - BDRV_SECTOR_SIZE); - log_size =3D qemu_opt_get_size_del(opts, VHDX_BLOCK_OPT_LOG_SIZE, 0); - block_size =3D qemu_opt_get_size_del(opts, VHDX_BLOCK_OPT_BLOCK_SIZE, = 0); - type =3D qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT); - use_zero_blocks =3D qemu_opt_get_bool_del(opts, VHDX_BLOCK_OPT_ZERO, t= rue); + assert(opts->driver =3D=3D BLOCKDEV_DRIVER_VHDX); + vhdx_opts =3D &opts->u.vhdx; =20 + /* Validate options and set default values */ + image_size =3D vhdx_opts->size; if (image_size > VHDX_MAX_IMAGE_SIZE) { error_setg_errno(errp, EINVAL, "Image size too large; max of 64TB"= ); - ret =3D -EINVAL; - goto exit; + return -EINVAL; } =20 - if (type =3D=3D NULL) { - type =3D g_strdup("dynamic"); + if (!vhdx_opts->has_log_size) { + log_size =3D DEFAULT_LOG_SIZE; + } else { + log_size =3D vhdx_opts->log_size; + } + if (log_size < MiB || (log_size % MiB) !=3D 0) { + error_setg_errno(errp, EINVAL, "Log size must be a multiple of 1 M= B"); + return -EINVAL; } =20 - if (!strcmp(type, "dynamic")) { + if (!vhdx_opts->has_block_state_zero) { + use_zero_blocks =3D true; + } else { + use_zero_blocks =3D vhdx_opts->block_state_zero; + } + + if (!vhdx_opts->has_subformat) { + vhdx_opts->subformat =3D BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC; + } + + switch (vhdx_opts->subformat) { + case BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC: image_type =3D VHDX_TYPE_DYNAMIC; - } else if (!strcmp(type, "fixed")) { + break; + case BLOCKDEV_VHDX_SUBFORMAT_FIXED: image_type =3D VHDX_TYPE_FIXED; - } else if (!strcmp(type, "differencing")) { - error_setg_errno(errp, ENOTSUP, - "Differencing files not yet supported"); - ret =3D -ENOTSUP; - goto exit; - } else { - error_setg(errp, "Invalid subformat '%s'", type); - ret =3D -EINVAL; - goto exit; + break; + default: + g_assert_not_reached(); } =20 /* These are pretty arbitrary, and mainly designed to keep the BAT * size reasonable to load into RAM */ - if (block_size =3D=3D 0) { + if (vhdx_opts->has_block_size) { + block_size =3D vhdx_opts->block_size; + } else { if (image_size > 32 * TiB) { block_size =3D 64 * MiB; } else if (image_size > (uint64_t) 100 * GiB) { @@ -1856,30 +1873,27 @@ static int coroutine_fn vhdx_co_create_opts(const c= har *filename, QemuOpts *opts } } =20 - - /* make the log size close to what was specified, but must be - * min 1MB, and multiple of 1MB */ - log_size =3D ROUND_UP(log_size, MiB); - - block_size =3D ROUND_UP(block_size, MiB); - block_size =3D block_size > VHDX_BLOCK_SIZE_MAX ? VHDX_BLOCK_SIZE_MAX : - block_size; - - ret =3D bdrv_create_file(filename, opts, &local_err); - if (ret < 0) { - error_propagate(errp, local_err); - goto exit; + if (block_size < MiB || (block_size % MiB) !=3D 0) { + error_setg_errno(errp, EINVAL, "Block size must be a multiple of 1= MB"); + return -EINVAL; + } + if (block_size > VHDX_BLOCK_SIZE_MAX) { + error_setg_errno(errp, EINVAL, "Block size must not exceed %d", + VHDX_BLOCK_SIZE_MAX); + return -EINVAL; } =20 - blk =3D blk_new_open(filename, NULL, NULL, - BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, - &local_err); - if (blk =3D=3D NULL) { - error_propagate(errp, local_err); - ret =3D -EIO; - goto exit; + /* Create BlockBackend to write to the image */ + bs =3D bdrv_open_blockdev_ref(vhdx_opts->file, errp); + if (bs =3D=3D NULL) { + return -EIO; } =20 + blk =3D blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); + ret =3D blk_insert_bs(blk, bs, errp); + if (ret < 0) { + goto delete_and_exit; + } blk_set_allow_write_beyond_eof(blk, true); =20 /* Create (A) */ @@ -1931,12 +1945,109 @@ static int coroutine_fn vhdx_co_create_opts(const = char *filename, QemuOpts *opts =20 delete_and_exit: blk_unref(blk); -exit: - g_free(type); + bdrv_unref(bs); g_free(creator); return ret; } =20 +static int coroutine_fn vhdx_co_create_opts(const char *filename, + QemuOpts *opts, + Error **errp) +{ + BlockdevCreateOptions *create_options =3D NULL; + QDict *qdict =3D NULL; + QObject *qobj; + Visitor *v; + BlockDriverState *bs =3D NULL; + Error *local_err =3D NULL; + int ret; + + static const QDictRenames opt_renames[] =3D { + { VHDX_BLOCK_OPT_LOG_SIZE, "log-size" }, + { VHDX_BLOCK_OPT_BLOCK_SIZE, "block-size" }, + { VHDX_BLOCK_OPT_ZERO, "block-state-zero" }, + { NULL, NULL }, + }; + + /* Parse options and convert legacy syntax */ + qdict =3D qemu_opts_to_qdict_filtered(opts, NULL, &vhdx_create_opts, t= rue); + + if (!qdict_rename_keys(qdict, opt_renames, errp)) { + ret =3D -EINVAL; + goto fail; + } + + /* Create and open the file (protocol layer) */ + ret =3D bdrv_create_file(filename, opts, &local_err); + if (ret < 0) { + error_propagate(errp, local_err); + goto fail; + } + + bs =3D bdrv_open(filename, NULL, NULL, + BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); + if (bs =3D=3D NULL) { + ret =3D -EIO; + goto fail; + } + + /* Now get the QAPI type BlockdevCreateOptions */ + qdict_put_str(qdict, "driver", "vhdx"); + qdict_put_str(qdict, "file", bs->node_name); + + qobj =3D qdict_crumple(qdict, errp); + QDECREF(qdict); + qdict =3D qobject_to_qdict(qobj); + if (qdict =3D=3D NULL) { + ret =3D -EINVAL; + goto fail; + } + + v =3D qobject_input_visitor_new_keyval(QOBJECT(qdict)); + visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err); + visit_free(v); + + if (local_err) { + error_propagate(errp, local_err); + ret =3D -EINVAL; + goto fail; + } + + /* Silently round up sizes: + * The image size is rounded to 512 bytes. Make the block and log size + * close to what was specified, but must be at least 1MB, and a multip= le of + * 1 MB. Also respect VHDX_BLOCK_SIZE_MAX for block sizes. block_size = =3D 0 + * means auto, which is represented by a missing key in QAPI. */ + assert(create_options->driver =3D=3D BLOCKDEV_DRIVER_VHDX); + create_options->u.vhdx.size =3D + ROUND_UP(create_options->u.vhdx.size, BDRV_SECTOR_SIZE); + + if (create_options->u.vhdx.has_log_size) { + create_options->u.vhdx.log_size =3D + ROUND_UP(create_options->u.vhdx.log_size, MiB); + } + if (create_options->u.vhdx.has_block_size) { + create_options->u.vhdx.block_size =3D + ROUND_UP(create_options->u.vhdx.block_size, MiB); + + if (create_options->u.vhdx.block_size =3D=3D 0) { + create_options->u.vhdx.has_block_size =3D false; + } + if (create_options->u.vhdx.block_size > VHDX_BLOCK_SIZE_MAX) { + create_options->u.vhdx.block_size =3D VHDX_BLOCK_SIZE_MAX; + } + } + + /* Create the vhdx image (format layer) */ + ret =3D vhdx_co_create(create_options, errp); + +fail: + QDECREF(qdict); + bdrv_unref(bs); + qapi_free_BlockdevCreateOptions(create_options); + return ret; +} + /* If opened r/w, the VHDX driver will automatically replay the log, * if one is present, inside the vhdx_open() call. * @@ -2005,6 +2116,7 @@ static BlockDriver bdrv_vhdx =3D { .bdrv_child_perm =3D bdrv_format_default_perms, .bdrv_co_readv =3D vhdx_co_readv, .bdrv_co_writev =3D vhdx_co_writev, + .bdrv_co_create =3D vhdx_co_create, .bdrv_co_create_opts =3D vhdx_co_create_opts, .bdrv_get_info =3D vhdx_get_info, .bdrv_co_check =3D vhdx_co_check, --=20 2.13.6