From nobody Sat Oct 25 09:04:19 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 1520632542923197.17671588014264; Fri, 9 Mar 2018 13:55:42 -0800 (PST) Received: from localhost ([::1]:48258 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPzJ-0005R4-N9 for importer@patchew.org; Fri, 09 Mar 2018 16:55:41 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35879) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPqe-0007KB-Mb for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euPqZ-0000pi-Es for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:44 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:51742 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 1euPqV-0000ip-MP; Fri, 09 Mar 2018 16:46:35 -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 A0198406802D; Fri, 9 Mar 2018 21:46:30 +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 4DA5D2156617; Fri, 9 Mar 2018 21:46:29 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 9 Mar 2018 22:46:05 +0100 Message-Id: <20180309214611.19122-2-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:30 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 09 Mar 2018 21:46:30 +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 1/7] parallels: 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 parallels, which enables image creation over QMP. Signed-off-by: Kevin Wolf Reviewed-by: Jeff Cody Reviewed-by: Max Reitz --- qapi/block-core.json | 18 ++++- block/parallels.c | 199 ++++++++++++++++++++++++++++++++++++++---------= ---- 2 files changed, 168 insertions(+), 49 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index 07039bfe9c..d38058eeab 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3481,6 +3481,22 @@ 'size': 'size' } } =20 ## +# @BlockdevCreateOptionsParallels: +# +# Driver specific image creation options for parallels. +# +# @file Node to create the image format on +# @size Size of the virtual disk in bytes +# @cluster-size Cluster size in bytes (default: 1 MB) +# +# Since: 2.12 +## +{ 'struct': 'BlockdevCreateOptionsParallels', + 'data': { 'file': 'BlockdevRef', + 'size': 'size', + '*cluster-size': 'size' } } + +## # @BlockdevQcow2Version: # # @v2: The original QCOW2 format as introduced in qemu 0.10 (version 2) @@ -3664,7 +3680,7 @@ 'null-aio': 'BlockdevCreateNotSupported', 'null-co': 'BlockdevCreateNotSupported', 'nvme': 'BlockdevCreateNotSupported', - 'parallels': 'BlockdevCreateNotSupported', + 'parallels': 'BlockdevCreateOptionsParallels', 'qcow2': 'BlockdevCreateOptionsQcow2', 'qcow': 'BlockdevCreateNotSupported', 'qed': 'BlockdevCreateNotSupported', diff --git a/block/parallels.c b/block/parallels.c index c13cb619e6..2da5e56a9d 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -34,6 +34,9 @@ #include "sysemu/block-backend.h" #include "qemu/module.h" #include "qemu/option.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qapi-visit-block-core.h" #include "qemu/bswap.h" #include "qemu/bitmap.h" #include "migration/blocker.h" @@ -79,6 +82,25 @@ static QemuOptsList parallels_runtime_opts =3D { }, }; =20 +static QemuOptsList parallels_create_opts =3D { + .name =3D "parallels-create-opts", + .head =3D QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head), + .desc =3D { + { + .name =3D BLOCK_OPT_SIZE, + .type =3D QEMU_OPT_SIZE, + .help =3D "Virtual disk size", + }, + { + .name =3D BLOCK_OPT_CLUSTER_SIZE, + .type =3D QEMU_OPT_SIZE, + .help =3D "Parallels image cluster size", + .def_value_str =3D stringify(DEFAULT_CLUSTER_SIZE), + }, + { /* end of list */ } + } +}; + =20 static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx) { @@ -480,46 +502,62 @@ out: } =20 =20 -static int coroutine_fn parallels_co_create_opts(const char *filename, - QemuOpts *opts, - Error **errp) +static int coroutine_fn parallels_co_create(BlockdevCreateOptions* opts, + Error **errp) { + BlockdevCreateOptionsParallels *parallels_opts; + BlockDriverState *bs; + BlockBackend *blk; int64_t total_size, cl_size; - uint8_t tmp[BDRV_SECTOR_SIZE]; - Error *local_err =3D NULL; - BlockBackend *file; uint32_t bat_entries, bat_sectors; ParallelsHeader header; + uint8_t tmp[BDRV_SECTOR_SIZE]; int ret; =20 - total_size =3D ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), - BDRV_SECTOR_SIZE); - cl_size =3D ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZ= E, - DEFAULT_CLUSTER_SIZE), BDRV_SECTOR_SIZE); + assert(opts->driver =3D=3D BLOCKDEV_DRIVER_PARALLELS); + parallels_opts =3D &opts->u.parallels; + + /* Sanity checks */ + total_size =3D parallels_opts->size; + + if (parallels_opts->has_cluster_size) { + cl_size =3D parallels_opts->cluster_size; + } else { + cl_size =3D DEFAULT_CLUSTER_SIZE; + } + if (total_size >=3D MAX_PARALLELS_IMAGE_FACTOR * cl_size) { - error_propagate(errp, local_err); + error_setg(errp, "Image size is too large for this cluster size"); return -E2BIG; } =20 - ret =3D bdrv_create_file(filename, opts, &local_err); - if (ret < 0) { - error_propagate(errp, local_err); - return ret; + if (!QEMU_IS_ALIGNED(total_size, BDRV_SECTOR_SIZE)) { + error_setg(errp, "Image size must be a multiple of 512 bytes"); + return -EINVAL; } =20 - file =3D blk_new_open(filename, NULL, NULL, - BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, - &local_err); - if (file =3D=3D NULL) { - error_propagate(errp, local_err); + if (!QEMU_IS_ALIGNED(cl_size, BDRV_SECTOR_SIZE)) { + error_setg(errp, "Cluster size must be a multiple of 512 bytes"); + return -EINVAL; + } + + /* Create BlockBackend to write to the image */ + bs =3D bdrv_open_blockdev_ref(parallels_opts->file, errp); + if (bs =3D=3D NULL) { return -EIO; } =20 - blk_set_allow_write_beyond_eof(file, true); + 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 - ret =3D blk_truncate(file, 0, PREALLOC_MODE_OFF, errp); + /* Create image format */ + ret =3D blk_truncate(blk, 0, PREALLOC_MODE_OFF, errp); if (ret < 0) { - goto exit; + goto out; } =20 bat_entries =3D DIV_ROUND_UP(total_size, cl_size); @@ -542,24 +580,107 @@ static int coroutine_fn parallels_co_create_opts(con= st char *filename, memset(tmp, 0, sizeof(tmp)); memcpy(tmp, &header, sizeof(header)); =20 - ret =3D blk_pwrite(file, 0, tmp, BDRV_SECTOR_SIZE, 0); + ret =3D blk_pwrite(blk, 0, tmp, BDRV_SECTOR_SIZE, 0); if (ret < 0) { goto exit; } - ret =3D blk_pwrite_zeroes(file, BDRV_SECTOR_SIZE, + ret =3D blk_pwrite_zeroes(blk, BDRV_SECTOR_SIZE, (bat_sectors - 1) << BDRV_SECTOR_BITS, 0); if (ret < 0) { goto exit; } - ret =3D 0; =20 -done: - blk_unref(file); + ret =3D 0; +out: + blk_unref(blk); + bdrv_unref(bs); return ret; =20 exit: error_setg_errno(errp, -ret, "Failed to create Parallels image"); - goto done; + goto out; +} + +static int coroutine_fn parallels_co_create_opts(const char *filename, + QemuOpts *opts, + Error **errp) +{ + BlockdevCreateOptions *create_options =3D NULL; + Error *local_err =3D NULL; + BlockDriverState *bs =3D NULL; + QDict *qdict =3D NULL; + QObject *qobj; + Visitor *v; + int ret; + + static const QDictRenames opt_renames[] =3D { + { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" }, + { NULL, NULL }, + }; + + /* Parse options and convert legacy syntax */ + qdict =3D qemu_opts_to_qdict_filtered(opts, NULL, ¶llels_create_op= ts, + true); + + if (!qdict_rename_keys(qdict, opt_renames, errp)) { + ret =3D -EINVAL; + goto done; + } + + /* 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 done; + } + + 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 done; + } + + /* Now get the QAPI type BlockdevCreateOptions */ + qdict_put_str(qdict, "driver", "parallels"); + 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 done; + } + + 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 done; + } + + /* Silently round up sizes */ + create_options->u.parallels.size =3D + ROUND_UP(create_options->u.parallels.size, BDRV_SECTOR_SIZE); + create_options->u.parallels.cluster_size =3D + ROUND_UP(create_options->u.parallels.cluster_size, BDRV_SECTOR_SIZ= E); + + /* Create the Parallels image (format layer) */ + ret =3D parallels_co_create(create_options, errp); + if (ret < 0) { + goto done; + } + ret =3D 0; + +done: + QDECREF(qdict); + bdrv_unref(bs); + qapi_free_BlockdevCreateOptions(create_options); + return ret; } =20 =20 @@ -771,25 +892,6 @@ static void parallels_close(BlockDriverState *bs) error_free(s->migration_blocker); } =20 -static QemuOptsList parallels_create_opts =3D { - .name =3D "parallels-create-opts", - .head =3D QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head), - .desc =3D { - { - .name =3D BLOCK_OPT_SIZE, - .type =3D QEMU_OPT_SIZE, - .help =3D "Virtual disk size", - }, - { - .name =3D BLOCK_OPT_CLUSTER_SIZE, - .type =3D QEMU_OPT_SIZE, - .help =3D "Parallels image cluster size", - .def_value_str =3D stringify(DEFAULT_CLUSTER_SIZE), - }, - { /* end of list */ } - } -}; - static BlockDriver bdrv_parallels =3D { .format_name =3D "parallels", .instance_size =3D sizeof(BDRVParallelsState), @@ -803,6 +905,7 @@ static BlockDriver bdrv_parallels =3D { .bdrv_co_readv =3D parallels_co_readv, .bdrv_co_writev =3D parallels_co_writev, .supports_backing =3D true, + .bdrv_co_create =3D parallels_co_create, .bdrv_co_create_opts =3D parallels_co_create_opts, .bdrv_co_check =3D parallels_co_check, .create_opts =3D ¶llels_create_opts, --=20 2.13.6 From nobody Sat Oct 25 09:04:19 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 1520632171060753.0016994667727; Fri, 9 Mar 2018 13:49:31 -0800 (PST) Received: from localhost ([::1]:48215 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPtK-0000NJ-9r for importer@patchew.org; Fri, 09 Mar 2018 16:49:30 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35824) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPqZ-0007Gp-J0 for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euPqY-0000pC-P3 for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:39 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:52308 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 1euPqV-0000ir-N0; Fri, 09 Mar 2018 16:46:35 -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 17C4E4129343; Fri, 9 Mar 2018 21:46:32 +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 DA11521449DB; Fri, 9 Mar 2018 21:46:30 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 9 Mar 2018 22:46:06 +0100 Message-Id: <20180309214611.19122-3-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:32 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 09 Mar 2018 21:46:32 +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 2/7] qemu-iotests: Enable write tests for parallels 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" Originally we added parallels as a read-only format to qemu-iotests where we did just some tests with a binary image. Since then, write and image creation support has been added to the driver, so we can now enable it in _supported_fmt generic. The driver doesn't support migration yet, though, so we need to add it to the list of exceptions in 181. Signed-off-by: Kevin Wolf Reviewed-by: Jeff Cody Reviewed-by: Max Reitz --- tests/qemu-iotests/181 | 2 +- tests/qemu-iotests/check | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/qemu-iotests/181 b/tests/qemu-iotests/181 index 0c91e8f9de..5e767c6195 100755 --- a/tests/qemu-iotests/181 +++ b/tests/qemu-iotests/181 @@ -44,7 +44,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 =20 _supported_fmt generic # Formats that do not support live migration -_unsupported_fmt qcow vdi vhdx vmdk vpc vvfat +_unsupported_fmt qcow vdi vhdx vmdk vpc vvfat parallels _supported_proto generic _supported_os Linux =20 diff --git a/tests/qemu-iotests/check b/tests/qemu-iotests/check index e6b6ff7a04..469142cd58 100755 --- a/tests/qemu-iotests/check +++ b/tests/qemu-iotests/check @@ -284,7 +284,6 @@ testlist options =20 -parallels) IMGFMT=3Dparallels - IMGFMT_GENERIC=3Dfalse xpand=3Dfalse ;; =20 --=20 2.13.6 From nobody Sat Oct 25 09:04:19 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520632298974233.79596195058036; Fri, 9 Mar 2018 13:51:38 -0800 (PST) Received: from localhost ([::1]:48232 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPvJ-0001yd-1S for importer@patchew.org; Fri, 09 Mar 2018 16:51:33 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35878) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPqe-0007KA-Md 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 1euPqZ-0000pu-Hx for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:44 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:42024 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 1euPqV-0000iq-Mo; Fri, 09 Mar 2018 16:46:35 -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 84ED6406E8B3; Fri, 9 Mar 2018 21:46:33 +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 521D6214138C; Fri, 9 Mar 2018 21:46:32 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 9 Mar 2018 22:46:07 +0100 Message-Id: <20180309214611.19122-4-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.7]); Fri, 09 Mar 2018 21:46:33 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Fri, 09 Mar 2018 21:46:33 +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 3/7] qcow: 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 qcow, which enables image creation over QMP. Signed-off-by: Kevin Wolf Reviewed-by: Jeff Cody Reviewed-by: Max Reitz --- qapi/block-core.json | 21 +++++- block/qcow.c | 196 ++++++++++++++++++++++++++++++++++-------------= ---- 2 files changed, 150 insertions(+), 67 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index d38058eeab..c81677c434 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3497,6 +3497,25 @@ '*cluster-size': 'size' } } =20 ## +# @BlockdevCreateOptionsQcow: +# +# Driver specific image creation options for qcow. +# +# @file Node to create the image format on +# @size Size of the virtual disk in bytes +# @backing-file File name of the backing file if a backing file +# should be used +# @encrypt Encryption options if the image should be encrypted +# +# Since: 2.12 +## +{ 'struct': 'BlockdevCreateOptionsQcow', + 'data': { 'file': 'BlockdevRef', + 'size': 'size', + '*backing-file': 'str', + '*encrypt': 'QCryptoBlockCreateOptions' } } + +## # @BlockdevQcow2Version: # # @v2: The original QCOW2 format as introduced in qemu 0.10 (version 2) @@ -3681,8 +3700,8 @@ 'null-co': 'BlockdevCreateNotSupported', 'nvme': 'BlockdevCreateNotSupported', 'parallels': 'BlockdevCreateOptionsParallels', + 'qcow': 'BlockdevCreateOptionsQcow', 'qcow2': 'BlockdevCreateOptionsQcow2', - 'qcow': 'BlockdevCreateNotSupported', 'qed': 'BlockdevCreateNotSupported', 'quorum': 'BlockdevCreateNotSupported', 'raw': 'BlockdevCreateNotSupported', diff --git a/block/qcow.c b/block/qcow.c index 47a18d9a3a..2e3770ca63 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -33,6 +33,8 @@ #include #include "qapi/qmp/qdict.h" #include "qapi/qmp/qstring.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qapi-visit-block-core.h" #include "crypto/block.h" #include "migration/blocker.h" #include "block/crypto.h" @@ -86,6 +88,8 @@ typedef struct BDRVQcowState { Error *migration_blocker; } BDRVQcowState; =20 +static QemuOptsList qcow_create_opts; + static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offse= t); =20 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filena= me) @@ -810,62 +814,50 @@ static void qcow_close(BlockDriverState *bs) error_free(s->migration_blocker); } =20 -static int coroutine_fn qcow_co_create_opts(const char *filename, QemuOpts= *opts, - Error **errp) +static int coroutine_fn qcow_co_create(BlockdevCreateOptions *opts, + Error **errp) { + BlockdevCreateOptionsQcow *qcow_opts; int header_size, backing_filename_len, l1_size, shift, i; QCowHeader header; uint8_t *tmp; int64_t total_size =3D 0; - char *backing_file =3D NULL; - Error *local_err =3D NULL; int ret; + BlockDriverState *bs; BlockBackend *qcow_blk; - char *encryptfmt =3D NULL; - QDict *options; - QDict *encryptopts =3D NULL; - QCryptoBlockCreateOptions *crypto_opts =3D NULL; QCryptoBlock *crypto =3D NULL; =20 - /* Read out options */ - total_size =3D ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), - BDRV_SECTOR_SIZE); + assert(opts->driver =3D=3D BLOCKDEV_DRIVER_QCOW); + qcow_opts =3D &opts->u.qcow; + + /* Sanity checks */ + total_size =3D qcow_opts->size; if (total_size =3D=3D 0) { error_setg(errp, "Image size is too small, cannot be zero length"); - ret =3D -EINVAL; - goto cleanup; + return -EINVAL; } =20 - backing_file =3D qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); - encryptfmt =3D qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT); - if (encryptfmt) { - if (qemu_opt_get(opts, BLOCK_OPT_ENCRYPT)) { - error_setg(errp, "Options " BLOCK_OPT_ENCRYPT " and " - BLOCK_OPT_ENCRYPT_FORMAT " are mutually exclusive"); - ret =3D -EINVAL; - goto cleanup; - } - } else if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) { - encryptfmt =3D g_strdup("aes"); + if (qcow_opts->has_encrypt && + qcow_opts->encrypt->format !=3D Q_CRYPTO_BLOCK_FORMAT_QCOW) + { + error_setg(errp, "Unsupported encryption format"); + return -EINVAL; } =20 - ret =3D bdrv_create_file(filename, opts, &local_err); - if (ret < 0) { - error_propagate(errp, local_err); - goto cleanup; + /* Create BlockBackend to write to the image */ + bs =3D bdrv_open_blockdev_ref(qcow_opts->file, errp); + if (bs =3D=3D NULL) { + return -EIO; } =20 - qcow_blk =3D blk_new_open(filename, NULL, NULL, - BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, - &local_err); - if (qcow_blk =3D=3D NULL) { - error_propagate(errp, local_err); - ret =3D -EIO; - goto cleanup; + qcow_blk =3D blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); + ret =3D blk_insert_bs(qcow_blk, bs, errp); + if (ret < 0) { + goto exit; } - blk_set_allow_write_beyond_eof(qcow_blk, true); =20 + /* Create image format */ ret =3D blk_truncate(qcow_blk, 0, PREALLOC_MODE_OFF, errp); if (ret < 0) { goto exit; @@ -877,16 +869,15 @@ static int coroutine_fn qcow_co_create_opts(const cha= r *filename, QemuOpts *opts header.size =3D cpu_to_be64(total_size); header_size =3D sizeof(header); backing_filename_len =3D 0; - if (backing_file) { - if (strcmp(backing_file, "fat:")) { + if (qcow_opts->has_backing_file) { + if (strcmp(qcow_opts->backing_file, "fat:")) { header.backing_file_offset =3D cpu_to_be64(header_size); - backing_filename_len =3D strlen(backing_file); + backing_filename_len =3D strlen(qcow_opts->backing_file); header.backing_file_size =3D cpu_to_be32(backing_filename_len); header_size +=3D backing_filename_len; } else { /* special backing file for vvfat */ - g_free(backing_file); - backing_file =3D NULL; + qcow_opts->has_backing_file =3D false; } header.cluster_bits =3D 9; /* 512 byte cluster to avoid copying unmodified sectors */ @@ -901,26 +892,10 @@ static int coroutine_fn qcow_co_create_opts(const cha= r *filename, QemuOpts *opts =20 header.l1_table_offset =3D cpu_to_be64(header_size); =20 - options =3D qemu_opts_to_qdict(opts, NULL); - qdict_extract_subqdict(options, &encryptopts, "encrypt."); - QDECREF(options); - if (encryptfmt) { - if (!g_str_equal(encryptfmt, "aes")) { - error_setg(errp, "Unknown encryption format '%s', expected 'ae= s'", - encryptfmt); - ret =3D -EINVAL; - goto exit; - } + if (qcow_opts->has_encrypt) { header.crypt_method =3D cpu_to_be32(QCOW_CRYPT_AES); =20 - crypto_opts =3D block_crypto_create_opts_init( - Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp); - if (!crypto_opts) { - ret =3D -EINVAL; - goto exit; - } - - crypto =3D qcrypto_block_create(crypto_opts, "encrypt.", + crypto =3D qcrypto_block_create(qcow_opts->encrypt, "encrypt.", NULL, NULL, NULL, errp); if (!crypto) { ret =3D -EINVAL; @@ -936,9 +911,9 @@ static int coroutine_fn qcow_co_create_opts(const char = *filename, QemuOpts *opts goto exit; } =20 - if (backing_file) { + if (qcow_opts->has_backing_file) { ret =3D blk_pwrite(qcow_blk, sizeof(header), - backing_file, backing_filename_len, 0); + qcow_opts->backing_file, backing_filename_len, 0); if (ret !=3D backing_filename_len) { goto exit; } @@ -959,12 +934,100 @@ static int coroutine_fn qcow_co_create_opts(const ch= ar *filename, QemuOpts *opts ret =3D 0; exit: blk_unref(qcow_blk); -cleanup: - QDECREF(encryptopts); - g_free(encryptfmt); qcrypto_block_free(crypto); - qapi_free_QCryptoBlockCreateOptions(crypto_opts); - g_free(backing_file); + return ret; +} + +static int coroutine_fn qcow_co_create_opts(const char *filename, + QemuOpts *opts, Error **errp) +{ + BlockdevCreateOptions *create_options =3D NULL; + BlockDriverState *bs =3D NULL; + QDict *qdict =3D NULL; + QObject *qobj; + Visitor *v; + const char *val; + Error *local_err =3D NULL; + int ret; + + static const QDictRenames opt_renames[] =3D { + { BLOCK_OPT_BACKING_FILE, "backing-file" }, + { BLOCK_OPT_ENCRYPT, BLOCK_OPT_ENCRYPT_FORMAT }, + { NULL, NULL }, + }; + + /* Parse options and convert legacy syntax */ + qdict =3D qemu_opts_to_qdict_filtered(opts, NULL, &qcow_create_opts, t= rue); + + val =3D qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT); + if (val && !strcmp(val, "on")) { + qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow"); + } else if (val && !strcmp(val, "off")) { + qdict_del(qdict, BLOCK_OPT_ENCRYPT); + } + + val =3D qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT); + if (val && !strcmp(val, "aes")) { + qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow"); + } + + 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", "qcow"); + 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_QCOW); + create_options->u.qcow.size =3D + ROUND_UP(create_options->u.qcow.size, BDRV_SECTOR_SIZE); + + /* Create the qcow image (format layer) */ + ret =3D qcow_co_create(create_options, errp); + if (ret < 0) { + goto fail; + } + + ret =3D 0; +fail: + QDECREF(qdict); + bdrv_unref(bs); + qapi_free_BlockdevCreateOptions(create_options); return ret; } =20 @@ -1128,6 +1191,7 @@ static BlockDriver bdrv_qcow =3D { .bdrv_close =3D qcow_close, .bdrv_child_perm =3D bdrv_format_default_perms, .bdrv_reopen_prepare =3D qcow_reopen_prepare, + .bdrv_co_create =3D qcow_co_create, .bdrv_co_create_opts =3D qcow_co_create_opts, .bdrv_has_zero_init =3D bdrv_has_zero_init_1, .supports_backing =3D true, --=20 2.13.6 From nobody Sat Oct 25 09:04:19 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 152063240001230.544791257818588; Fri, 9 Mar 2018 13:53:20 -0800 (PST) Received: from localhost ([::1]:48244 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPx0-0003Oa-Ut for importer@patchew.org; Fri, 09 Mar 2018 16:53:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35881) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPqe-0007KD-Mf for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euPqZ-0000qF-Ku for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:44 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:42026 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 1euPqV-0000iu-Mm; Fri, 09 Mar 2018 16:46:35 -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 F2878406E8B9; Fri, 9 Mar 2018 21:46:34 +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 C052A214138C; Fri, 9 Mar 2018 21:46:33 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 9 Mar 2018 22:46:08 +0100 Message-Id: <20180309214611.19122-5-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.7]); Fri, 09 Mar 2018 21:46:35 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Fri, 09 Mar 2018 21:46:35 +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 4/7] qed: 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 qed, which enables image creation over QMP. Signed-off-by: Kevin Wolf Reviewed-by: Max Reitz --- qapi/block-core.json | 25 ++++++- block/qed.c | 204 ++++++++++++++++++++++++++++++++++-------------= ---- 2 files changed, 162 insertions(+), 67 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index c81677c434..1e2edbc063 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3559,6 +3559,29 @@ '*refcount-bits': 'int' } } =20 ## +# @BlockdevCreateOptionsQed: +# +# Driver specific image creation options for qed. +# +# @file Node to create the image format on +# @size Size of the virtual disk in bytes +# @backing-file File name of the backing file if a backing file +# should be used +# @backing-fmt Name of the block driver to use for the backing file +# @cluster-size Cluster size in bytes (default: 65536) +# @table-size L1/L2 table size (in clusters) +# +# Since: 2.12 +## +{ 'struct': 'BlockdevCreateOptionsQed', + 'data': { 'file': 'BlockdevRef', + 'size': 'size', + '*backing-file': 'str', + '*backing-fmt': 'BlockdevDriver', + '*cluster-size': 'size', + '*table-size': 'int' } } + +## # @BlockdevCreateOptionsRbd: # # Driver specific image creation options for rbd/Ceph. @@ -3702,7 +3725,7 @@ 'parallels': 'BlockdevCreateOptionsParallels', 'qcow': 'BlockdevCreateOptionsQcow', 'qcow2': 'BlockdevCreateOptionsQcow2', - 'qed': 'BlockdevCreateNotSupported', + 'qed': 'BlockdevCreateOptionsQed', 'quorum': 'BlockdevCreateNotSupported', 'raw': 'BlockdevCreateNotSupported', 'rbd': 'BlockdevCreateOptionsRbd', diff --git a/block/qed.c b/block/qed.c index 5e6a6bfaa0..46a84beeed 100644 --- a/block/qed.c +++ b/block/qed.c @@ -20,6 +20,11 @@ #include "trace.h" #include "qed.h" #include "sysemu/block-backend.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qapi-visit-block-core.h" + +static QemuOptsList qed_create_opts; =20 static int bdrv_qed_probe(const uint8_t *buf, int buf_size, const char *filename) @@ -594,57 +599,95 @@ static void bdrv_qed_close(BlockDriverState *bs) qemu_vfree(s->l1_table); } =20 -static int qed_create(const char *filename, uint32_t cluster_size, - uint64_t image_size, uint32_t table_size, - const char *backing_file, const char *backing_fmt, - QemuOpts *opts, Error **errp) +static int coroutine_fn bdrv_qed_co_create(BlockdevCreateOptions *opts, + Error **errp) { - QEDHeader header =3D { - .magic =3D QED_MAGIC, - .cluster_size =3D cluster_size, - .table_size =3D table_size, - .header_size =3D 1, - .features =3D 0, - .compat_features =3D 0, - .l1_table_offset =3D cluster_size, - .image_size =3D image_size, - }; + BlockdevCreateOptionsQed *qed_opts; + BlockBackend *blk =3D NULL; + BlockDriverState *bs =3D NULL; + + QEDHeader header; QEDHeader le_header; uint8_t *l1_table =3D NULL; - size_t l1_size =3D header.cluster_size * header.table_size; - Error *local_err =3D NULL; + size_t l1_size; int ret =3D 0; - BlockBackend *blk; =20 - ret =3D bdrv_create_file(filename, opts, &local_err); - if (ret < 0) { - error_propagate(errp, local_err); - return ret; + assert(opts->driver =3D=3D BLOCKDEV_DRIVER_QED); + qed_opts =3D &opts->u.qed; + + /* Validate options and set default values */ + if (!qed_opts->has_cluster_size) { + qed_opts->cluster_size =3D QED_DEFAULT_CLUSTER_SIZE; + } + if (!qed_opts->has_table_size) { + qed_opts->table_size =3D QED_DEFAULT_TABLE_SIZE; } =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); + if (!qed_is_cluster_size_valid(qed_opts->cluster_size)) { + error_setg(errp, "QED cluster size must be within range [%u, %u] " + "and power of 2", + QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); + return -EINVAL; + } + if (!qed_is_table_size_valid(qed_opts->table_size)) { + error_setg(errp, "QED table size must be within range [%u, %u] " + "and power of 2", + QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); + return -EINVAL; + } + if (!qed_is_image_size_valid(qed_opts->size, qed_opts->cluster_size, + qed_opts->table_size)) + { + error_setg(errp, "QED image size must be a non-zero multiple of " + "cluster size and less than %" PRIu64 " bytes", + qed_max_image_size(qed_opts->cluster_size, + qed_opts->table_size)); + return -EINVAL; + } + + /* Create BlockBackend to write to the image */ + bs =3D bdrv_open_blockdev_ref(qed_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 out; + } blk_set_allow_write_beyond_eof(blk, true); =20 + /* Prepare image format */ + header =3D (QEDHeader) { + .magic =3D QED_MAGIC, + .cluster_size =3D qed_opts->cluster_size, + .table_size =3D qed_opts->table_size, + .header_size =3D 1, + .features =3D 0, + .compat_features =3D 0, + .l1_table_offset =3D qed_opts->cluster_size, + .image_size =3D qed_opts->size, + }; + + l1_size =3D header.cluster_size * header.table_size; + /* File must start empty and grow, check truncate is supported */ ret =3D blk_truncate(blk, 0, PREALLOC_MODE_OFF, errp); if (ret < 0) { goto out; } =20 - if (backing_file) { + if (qed_opts->has_backing_file) { header.features |=3D QED_F_BACKING_FILE; header.backing_filename_offset =3D sizeof(le_header); - header.backing_filename_size =3D strlen(backing_file); + header.backing_filename_size =3D strlen(qed_opts->backing_file); =20 - if (qed_fmt_is_raw(backing_fmt)) { - header.features |=3D QED_F_BACKING_FORMAT_NO_PROBE; + if (qed_opts->has_backing_fmt) { + const char *backing_fmt =3D BlockdevDriver_str(qed_opts->backi= ng_fmt); + if (qed_fmt_is_raw(backing_fmt)) { + header.features |=3D QED_F_BACKING_FORMAT_NO_PROBE; + } } } =20 @@ -653,7 +696,7 @@ static int qed_create(const char *filename, uint32_t cl= uster_size, if (ret < 0) { goto out; } - ret =3D blk_pwrite(blk, sizeof(le_header), backing_file, + ret =3D blk_pwrite(blk, sizeof(le_header), qed_opts->backing_file, header.backing_filename_size, 0); if (ret < 0) { goto out; @@ -669,6 +712,7 @@ static int qed_create(const char *filename, uint32_t cl= uster_size, out: g_free(l1_table); blk_unref(blk); + bdrv_unref(bs); return ret; } =20 @@ -676,51 +720,78 @@ static int coroutine_fn bdrv_qed_co_create_opts(const= char *filename, QemuOpts *opts, Error **errp) { - uint64_t image_size =3D 0; - uint32_t cluster_size =3D QED_DEFAULT_CLUSTER_SIZE; - uint32_t table_size =3D QED_DEFAULT_TABLE_SIZE; - char *backing_file =3D NULL; - char *backing_fmt =3D NULL; + BlockdevCreateOptions *create_options =3D NULL; + QDict *qdict =3D NULL; + QObject *qobj; + Visitor *v; + BlockDriverState *bs =3D NULL; + Error *local_err =3D NULL; int ret; =20 - image_size =3D ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), - BDRV_SECTOR_SIZE); - backing_file =3D qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); - backing_fmt =3D qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); - cluster_size =3D qemu_opt_get_size_del(opts, - BLOCK_OPT_CLUSTER_SIZE, - QED_DEFAULT_CLUSTER_SIZE); - table_size =3D qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE, - QED_DEFAULT_TABLE_SIZE); - - if (!qed_is_cluster_size_valid(cluster_size)) { - error_setg(errp, "QED cluster size must be within range [%u, %u] " - "and power of 2", - QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); + static const QDictRenames opt_renames[] =3D { + { BLOCK_OPT_BACKING_FILE, "backing-file" }, + { BLOCK_OPT_BACKING_FMT, "backing-fmt" }, + { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" }, + { BLOCK_OPT_TABLE_SIZE, "table-size" }, + { NULL, NULL }, + }; + + /* Parse options and convert legacy syntax */ + qdict =3D qemu_opts_to_qdict_filtered(opts, NULL, &qed_create_opts, tr= ue); + + if (!qdict_rename_keys(qdict, opt_renames, errp)) { ret =3D -EINVAL; - goto finish; + goto fail; } - if (!qed_is_table_size_valid(table_size)) { - error_setg(errp, "QED table size must be within range [%u, %u] " - "and power of 2", - QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); + + /* 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", "qed"); + 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 finish; + goto fail; } - if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { - error_setg(errp, "QED image size must be a non-zero multiple of " - "cluster size and less than %" PRIu64 " bytes", - qed_max_image_size(cluster_size, table_size)); + + 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 finish; + goto fail; } =20 - ret =3D qed_create(filename, cluster_size, image_size, table_size, - backing_file, backing_fmt, opts, errp); + /* Silently round up size */ + assert(create_options->driver =3D=3D BLOCKDEV_DRIVER_QED); + create_options->u.qed.size =3D + ROUND_UP(create_options->u.qed.size, BDRV_SECTOR_SIZE); + + /* Create the qed image (format layer) */ + ret =3D bdrv_qed_co_create(create_options, errp); =20 -finish: - g_free(backing_file); - g_free(backing_fmt); +fail: + QDECREF(qdict); + bdrv_unref(bs); + qapi_free_BlockdevCreateOptions(create_options); return ret; } =20 @@ -1602,6 +1673,7 @@ static BlockDriver bdrv_qed =3D { .bdrv_close =3D bdrv_qed_close, .bdrv_reopen_prepare =3D bdrv_qed_reopen_prepare, .bdrv_child_perm =3D bdrv_format_default_perms, + .bdrv_co_create =3D bdrv_qed_co_create, .bdrv_co_create_opts =3D bdrv_qed_co_create_opts, .bdrv_has_zero_init =3D bdrv_has_zero_init_1, .bdrv_co_block_status =3D bdrv_qed_co_block_status, --=20 2.13.6 From nobody Sat Oct 25 09:04:19 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 15206326987821019.1745494251359; Fri, 9 Mar 2018 13:58:18 -0800 (PST) Received: from localhost ([::1]:48276 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euQ1n-0007i2-Oj for importer@patchew.org; Fri, 09 Mar 2018 16:58:15 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35882) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPqe-0007KE-Mn for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euPqa-0000qy-Qf for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:44 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:36972 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 1euPqW-0000lJ-Ta; Fri, 09 Mar 2018 16:46:37 -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 6CF178D6EC; Fri, 9 Mar 2018 21:46:36 +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 39743218CC07; Fri, 9 Mar 2018 21:46:35 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 9 Mar 2018 22:46:09 +0100 Message-Id: <20180309214611.19122-6-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.2]); Fri, 09 Mar 2018 21:46:36 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 09 Mar 2018 21:46:36 +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 5/7] vdi: 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 vdi, which enables image creation over QMP. Signed-off-by: Kevin Wolf --- qapi/block-core.json | 21 ++++++- block/vdi.c | 169 ++++++++++++++++++++++++++++++++++++++---------= ---- 2 files changed, 148 insertions(+), 42 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index 1e2edbc063..2eba0eef7e 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3680,6 +3680,25 @@ 'size': 'size' } } =20 ## +# @BlockdevCreateOptionsVdi: +# +# Driver specific image creation options for vdi. +# +# @file Node to create the image format on +# @size Size of the virtual disk in bytes +# @cluster-size Cluster size in bytes (default: 1 MB) +# @static Whether to create a static (preallocated) image +# (default: false) +# +# Since: 2.12 +## +{ 'struct': 'BlockdevCreateOptionsVdi', + 'data': { 'file': 'BlockdevRef', + 'size': 'size', + '*cluster-size': 'size', + '*static': 'bool' } } + +## # @BlockdevCreateNotSupported: # # This is used for all drivers that don't support creating images. @@ -3733,7 +3752,7 @@ 'sheepdog': 'BlockdevCreateOptionsSheepdog', 'ssh': 'BlockdevCreateOptionsSsh', 'throttle': 'BlockdevCreateNotSupported', - 'vdi': 'BlockdevCreateNotSupported', + 'vdi': 'BlockdevCreateOptionsVdi', 'vhdx': 'BlockdevCreateNotSupported', 'vmdk': 'BlockdevCreateNotSupported', 'vpc': 'BlockdevCreateNotSupported', diff --git a/block/vdi.c b/block/vdi.c index 2b5ddd0666..c60ddc58c0 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -60,6 +60,9 @@ #include "qemu/coroutine.h" #include "qemu/cutils.h" #include "qemu/uuid.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qapi-visit-block-core.h" =20 /* Code configuration options. */ =20 @@ -182,6 +185,8 @@ typedef struct { Error *migration_blocker; } BDRVVdiState; =20 +static QemuOptsList vdi_create_opts; + static void vdi_header_to_cpu(VdiHeader *header) { le32_to_cpus(&header->signature); @@ -716,67 +721,72 @@ nonallocating_write: return ret; } =20 -static int coroutine_fn vdi_co_create_opts(const char *filename, QemuOpts = *opts, - Error **errp) +static int coroutine_fn vdi_co_create(BlockdevCreateOptions *opts, + Error **errp) { + BlockdevCreateOptionsVdi *vdi_opts; + BlockBackend *blk =3D NULL; + BlockDriverState *bs =3D NULL; + int ret =3D 0; - uint64_t bytes =3D 0; uint32_t blocks; - size_t block_size =3D DEFAULT_CLUSTER_SIZE; uint32_t image_type =3D VDI_TYPE_DYNAMIC; VdiHeader header; size_t i; size_t bmap_size; int64_t offset =3D 0; - Error *local_err =3D NULL; - BlockBackend *blk =3D NULL; uint32_t *bmap =3D NULL; =20 logout("\n"); =20 - /* Read out options. */ - bytes =3D 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 =3D qemu_opt_get_size_del(opts, - BLOCK_OPT_CLUSTER_SIZE, - DEFAULT_CLUSTER_SIZE); -#endif -#if defined(CONFIG_VDI_STATIC_IMAGE) - if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) { - image_type =3D VDI_TYPE_STATIC; + assert(opts->driver =3D=3D BLOCKDEV_DRIVER_VDI); + vdi_opts =3D &opts->u.vdi; + + /* Validate options and set default values */ + if (!vdi_opts->has_cluster_size) { + vdi_opts->cluster_size =3D DEFAULT_CLUSTER_SIZE; } -#endif =20 - if (bytes > VDI_DISK_SIZE_MAX) { - ret =3D -ENOTSUP; + if (vdi_opts->size > VDI_DISK_SIZE_MAX) { error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64 ", max supported is 0x%" PRIx64 ")", - bytes, VDI_DISK_SIZE_MAX); - goto exit; + vdi_opts->size, VDI_DISK_SIZE_MAX); + return -ENOTSUP; } =20 - ret =3D bdrv_create_file(filename, opts, &local_err); - if (ret < 0) { - error_propagate(errp, local_err); - goto exit; +#if !defined(CONFIG_VDI_BLOCK_SIZE) + if (vdi_opts->has_cluster_size) { + error_setg(errp, "Non-default cluster size not supported"); + return -ENOTSUP; + } +#endif +#if !defined(CONFIG_VDI_STATIC_IMAGE) + if (vdi_opts->has_static) { + error_setg(errp, "Static images not supported"); + return -ENOTSUP; + } +#else + if (vdi_opts->q_static) { + image_type =3D VDI_TYPE_STATIC; + } +#endif + + /* Create BlockBackend to write to the image */ + bs =3D bdrv_open_blockdev_ref(vdi_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 exit; } - blk_set_allow_write_beyond_eof(blk, true); =20 /* We need enough blocks to store the given disk size, so always round up. */ - blocks =3D DIV_ROUND_UP(bytes, block_size); + blocks =3D DIV_ROUND_UP(vdi_opts->size, vdi_opts->cluster_size); =20 bmap_size =3D blocks * sizeof(uint32_t); bmap_size =3D ROUND_UP(bmap_size, SECTOR_SIZE); @@ -790,8 +800,8 @@ static int coroutine_fn vdi_co_create_opts(const char *= filename, QemuOpts *opts, header.offset_bmap =3D 0x200; header.offset_data =3D 0x200 + bmap_size; header.sector_size =3D SECTOR_SIZE; - header.disk_size =3D bytes; - header.block_size =3D block_size; + header.disk_size =3D vdi_opts->size; + header.block_size =3D vdi_opts->cluster_size; header.blocks_in_image =3D blocks; if (image_type =3D=3D VDI_TYPE_STATIC) { header.blocks_allocated =3D blocks; @@ -805,7 +815,7 @@ static int coroutine_fn vdi_co_create_opts(const char *= filename, QemuOpts *opts, vdi_header_to_le(&header); ret =3D blk_pwrite(blk, offset, &header, sizeof(header), 0); if (ret < 0) { - error_setg(errp, "Error writing header to %s", filename); + error_setg(errp, "Error writing header"); goto exit; } offset +=3D sizeof(header); @@ -826,27 +836,103 @@ static int coroutine_fn vdi_co_create_opts(const cha= r *filename, QemuOpts *opts, } ret =3D blk_pwrite(blk, offset, bmap, bmap_size, 0); if (ret < 0) { - error_setg(errp, "Error writing bmap to %s", filename); + error_setg(errp, "Error writing bmap"); goto exit; } offset +=3D bmap_size; } =20 if (image_type =3D=3D VDI_TYPE_STATIC) { - ret =3D blk_truncate(blk, offset + blocks * block_size, + ret =3D blk_truncate(blk, offset + blocks * vdi_opts->cluster_size, PREALLOC_MODE_OFF, errp); if (ret < 0) { - error_prepend(errp, "Failed to statically allocate %s", filena= me); + error_prepend(errp, "Failed to statically allocate image"); goto exit; } } =20 exit: blk_unref(blk); + bdrv_unref(bs); g_free(bmap); return ret; } =20 +static int coroutine_fn vdi_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 { + { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" }, + { NULL, NULL }, + }; + + /* Parse options and convert legacy syntax */ + qdict =3D qemu_opts_to_qdict_filtered(opts, NULL, &vdi_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", "vdi"); + 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_VDI); + create_options->u.vdi.size =3D + ROUND_UP(create_options->u.vdi.size, BDRV_SECTOR_SIZE); + + /* Create the vdi image (format layer) */ + ret =3D vdi_co_create(create_options, errp); + +fail: + QDECREF(qdict); + bdrv_unref(bs); + qapi_free_BlockdevCreateOptions(create_options); + return ret; +} + static void vdi_close(BlockDriverState *bs) { BDRVVdiState *s =3D bs->opaque; @@ -895,6 +981,7 @@ static BlockDriver bdrv_vdi =3D { .bdrv_close =3D vdi_close, .bdrv_reopen_prepare =3D vdi_reopen_prepare, .bdrv_child_perm =3D bdrv_format_default_perms, + .bdrv_co_create =3D vdi_co_create, .bdrv_co_create_opts =3D vdi_co_create_opts, .bdrv_has_zero_init =3D bdrv_has_zero_init_1, .bdrv_co_block_status =3D vdi_co_block_status, --=20 2.13.6 From nobody Sat Oct 25 09:04:19 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 From nobody Sat Oct 25 09:04:19 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