From nobody Sat Oct 25 20:23:01 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 1520632351957230.83179995240778; Fri, 9 Mar 2018 13:52:31 -0800 (PST) Received: from localhost ([::1]:48240 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPwA-0002je-Hy for importer@patchew.org; Fri, 09 Mar 2018 16:52:26 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35928) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPqh-0007Mj-CT for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euPqf-0000s7-N7 for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:47 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:51746 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 1euPqY-0000o8-B0; Fri, 09 Mar 2018 16:46:38 -0500 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 DBFDD406802D; Fri, 9 Mar 2018 21:46:37 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-234.ams2.redhat.com [10.36.117.234]) by smtp.corp.redhat.com (Postfix) with ESMTP id A8557218CC07; Fri, 9 Mar 2018 21:46:36 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 9 Mar 2018 22:46:10 +0100 Message-Id: <20180309214611.19122-7-kwolf@redhat.com> In-Reply-To: <20180309214611.19122-1-kwolf@redhat.com> References: <20180309214611.19122-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.5]); Fri, 09 Mar 2018 21:46:37 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 09 Mar 2018 21:46:37 +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] [PATCH 6/7] 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, jcody@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.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: Jeff Cody --- qapi/block-core.json | 37 ++++++++++- block/vhdx.c | 174 ++++++++++++++++++++++++++++++++++++++---------= ---- 2 files changed, 167 insertions(+), 44 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index 2eba0eef7e..3a65909c47 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3699,6 +3699,41 @@ '*static': 'bool' } } =20 ## +# @BlockdevVhdxSubformat: +# +# @dynamic: Growing image file +# @fixed: Preallocated fixed-size imge 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 (default: 1 MB) +# @block-size Block size in bytes (default: 1 MB) +# @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. @@ -3753,7 +3788,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..0ce972381f 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,54 +1797,63 @@ 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; + + /* Validate options and set default values */ + + image_size =3D vhdx_opts->size; + block_size =3D vhdx_opts->block_size; + + if (!vhdx_opts->has_log_size) { + log_size =3D DEFAULT_LOG_SIZE; + } else { + log_size =3D vhdx_opts->log_size; + } + + if (!vhdx_opts->has_block_state_zero) { + use_zero_blocks =3D true; + } else { + use_zero_blocks =3D vhdx_opts->block_state_zero; + } =20 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_subformat) { + vhdx_opts->subformat =3D BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC; } =20 - if (!strcmp(type, "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 @@ -1865,21 +1879,17 @@ static int coroutine_fn vhdx_co_create_opts(const c= har *filename, QemuOpts *opts block_size =3D block_size > VHDX_BLOCK_SIZE_MAX ? VHDX_BLOCK_SIZE_MAX : block_size; =20 - ret =3D bdrv_create_file(filename, opts, &local_err); - if (ret < 0) { - error_propagate(errp, local_err); - 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_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; + 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 +1941,89 @@ static int coroutine_fn vhdx_co_create_opts(const c= har *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 size */ + 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); + + /* 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 +2092,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