From nobody Sat Oct 25 20:22:59 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 1520632213194768.2768193867015; Fri, 9 Mar 2018 13:50:13 -0800 (PST) Received: from localhost ([::1]:48220 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPu0-0000qR-7z for importer@patchew.org; Fri, 09 Mar 2018 16:50:12 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35936) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPqh-0007N9-U0 for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euPqg-0000sV-Fr for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:47 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:52312 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 1euPqZ-0000pq-Oh; Fri, 09 Mar 2018 16:46:39 -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 52C3340252F5; Fri, 9 Mar 2018 21:46:39 +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 217D5218CC06; Fri, 9 Mar 2018 21:46:38 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 9 Mar 2018 22:46:11 +0100 Message-Id: <20180309214611.19122-8-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.6]); Fri, 09 Mar 2018 21:46:39 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 09 Mar 2018 21:46:39 +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 7/7] vpc: 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 vpc, which enables image creation over QMP. Signed-off-by: Kevin Wolf Reviewed-by: Max Reitz --- qapi/block-core.json | 33 ++++++++++- block/vpc.c | 152 ++++++++++++++++++++++++++++++++++++++---------= ---- 2 files changed, 147 insertions(+), 38 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index 3a65909c47..ca645a0067 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3734,6 +3734,37 @@ '*block-state-zero': 'bool' } } =20 ## +# @BlockdevVpcSubformat: +# +# @dynamic: Growing image file +# @fixed: Preallocated fixed-size imge file +# +# Since: 2.12 +## +{ 'enum': 'BlockdevVpcSubformat', + 'data': [ 'dynamic', 'fixed' ] } + +## +# @BlockdevCreateOptionsVpc: +# +# Driver specific image creation options for vpc (VHD). +# +# @file Node to create the image format on +# @size Size of the virtual disk in bytes +# @subformat vhdx subformat (default: dynamic) +# @force-size Force use of the exact byte size instead of rounding t= o the +# next size that can be represented in CHS geometry +# (default: false) +# +# Since: 2.12 +## +{ 'struct': 'BlockdevCreateOptionsVpc', + 'data': { 'file': 'BlockdevRef', + 'size': 'size', + '*subformat': 'BlockdevVpcSubformat', + '*force-size': 'bool' } } + +## # @BlockdevCreateNotSupported: # # This is used for all drivers that don't support creating images. @@ -3790,7 +3821,7 @@ 'vdi': 'BlockdevCreateOptionsVdi', 'vhdx': 'BlockdevCreateOptionsVhdx', 'vmdk': 'BlockdevCreateNotSupported', - 'vpc': 'BlockdevCreateNotSupported', + 'vpc': 'BlockdevCreateOptionsVpc', 'vvfat': 'BlockdevCreateNotSupported', 'vxhs': 'BlockdevCreateNotSupported' } } diff --git a/block/vpc.c b/block/vpc.c index b2e2b9ebd4..8824211713 100644 --- a/block/vpc.c +++ b/block/vpc.c @@ -32,6 +32,9 @@ #include "migration/blocker.h" #include "qemu/bswap.h" #include "qemu/uuid.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qapi-visit-block-core.h" =20 /**************************************************************/ =20 @@ -166,6 +169,8 @@ static QemuOptsList vpc_runtime_opts =3D { } }; =20 +static QemuOptsList vpc_create_opts; + static uint32_t vpc_checksum(uint8_t* buf, size_t size) { uint32_t res =3D 0; @@ -897,12 +902,15 @@ static int create_fixed_disk(BlockBackend *blk, uint8= _t *buf, return ret; } =20 -static int coroutine_fn vpc_co_create_opts(const char *filename, QemuOpts = *opts, - Error **errp) +static int coroutine_fn vpc_co_create(BlockdevCreateOptions *opts, + Error **errp) { + BlockdevCreateOptionsVpc *vpc_opts; + BlockBackend *blk =3D NULL; + BlockDriverState *bs =3D NULL; + uint8_t buf[1024]; VHDFooter *footer =3D (VHDFooter *) buf; - char *disk_type_param; int i; uint16_t cyls =3D 0; uint8_t heads =3D 0; @@ -911,45 +919,38 @@ static int coroutine_fn vpc_co_create_opts(const char= *filename, QemuOpts *opts, int64_t total_size; int disk_type; int ret =3D -EIO; - bool force_size; - Error *local_err =3D NULL; - BlockBackend *blk =3D NULL; =20 - /* Read out options */ - total_size =3D ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), - BDRV_SECTOR_SIZE); - disk_type_param =3D qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT); - if (disk_type_param) { - if (!strcmp(disk_type_param, "dynamic")) { - disk_type =3D VHD_DYNAMIC; - } else if (!strcmp(disk_type_param, "fixed")) { - disk_type =3D VHD_FIXED; - } else { - error_setg(errp, "Invalid disk type, %s", disk_type_param); - ret =3D -EINVAL; - goto out; - } - } else { + assert(opts->driver =3D=3D BLOCKDEV_DRIVER_VPC); + vpc_opts =3D &opts->u.vpc; + + /* Validate options and set default values */ + total_size =3D vpc_opts->size; + + if (!vpc_opts->has_subformat) { + vpc_opts->subformat =3D BLOCKDEV_VPC_SUBFORMAT_DYNAMIC; + } + switch (vpc_opts->subformat) { + case BLOCKDEV_VPC_SUBFORMAT_DYNAMIC: disk_type =3D VHD_DYNAMIC; + break; + case BLOCKDEV_VPC_SUBFORMAT_FIXED: + disk_type =3D VHD_FIXED; + break; + default: + g_assert_not_reached(); } =20 - force_size =3D qemu_opt_get_bool_del(opts, VPC_OPT_FORCE_SIZE, false); - - ret =3D bdrv_create_file(filename, opts, &local_err); - if (ret < 0) { - error_propagate(errp, local_err); - goto out; + /* Create BlockBackend to write to the image */ + bs =3D bdrv_open_blockdev_ref(vpc_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; + 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 out; } - blk_set_allow_write_beyond_eof(blk, true); =20 /* @@ -961,7 +962,7 @@ static int coroutine_fn vpc_co_create_opts(const char *= filename, QemuOpts *opts, * we set the geometry to 65535 x 16 x 255 (CxHxS) sectors and use * the image size from the VHD footer to calculate total_sectors. */ - if (force_size) { + if (vpc_opts->force_size) { /* This will force the use of total_size for sector count, below */ cyls =3D VHD_CHS_MAX_C; heads =3D VHD_CHS_MAX_H; @@ -990,7 +991,7 @@ static int coroutine_fn vpc_co_create_opts(const char *= filename, QemuOpts *opts, memset(buf, 0, 1024); =20 memcpy(footer->creator, "conectix", 8); - if (force_size) { + if (vpc_opts->force_size) { memcpy(footer->creator_app, "qem2", 4); } else { memcpy(footer->creator_app, "qemu", 4); @@ -1032,10 +1033,86 @@ static int coroutine_fn vpc_co_create_opts(const ch= ar *filename, QemuOpts *opts, =20 out: blk_unref(blk); - g_free(disk_type_param); + bdrv_unref(bs); + return ret; +} + +static int coroutine_fn vpc_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 { + { VPC_OPT_FORCE_SIZE, "force-size" }, + { NULL, NULL }, + }; + + /* Parse options and convert legacy syntax */ + qdict =3D qemu_opts_to_qdict_filtered(opts, NULL, &vpc_create_opts, tr= ue); + + 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", "vpc"); + 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_VPC); + create_options->u.vpc.size =3D + ROUND_UP(create_options->u.vpc.size, BDRV_SECTOR_SIZE); + + /* Create the vpc image (format layer) */ + ret =3D vpc_co_create(create_options, errp); + +fail: + QDECREF(qdict); + bdrv_unref(bs); + qapi_free_BlockdevCreateOptions(create_options); return ret; } =20 + static int vpc_has_zero_init(BlockDriverState *bs) { BDRVVPCState *s =3D bs->opaque; @@ -1096,6 +1173,7 @@ static BlockDriver bdrv_vpc =3D { .bdrv_close =3D vpc_close, .bdrv_reopen_prepare =3D vpc_reopen_prepare, .bdrv_child_perm =3D bdrv_format_default_perms, + .bdrv_co_create =3D vpc_co_create, .bdrv_co_create_opts =3D vpc_co_create_opts, =20 .bdrv_co_preadv =3D vpc_co_preadv, --=20 2.13.6