From nobody Mon May 6 22:52:33 2024 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 1520952786778130.40868850551772; Tue, 13 Mar 2018 07:53:06 -0700 (PDT) Received: from localhost ([::1]:40300 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlIX-0001qJ-PO for importer@patchew.org; Tue, 13 Mar 2018 10:53:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57887) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlDS-0005vh-7q for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evlDQ-000723-7u for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:50 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:34678 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 1evlDK-0006xr-PG; Tue, 13 Mar 2018 10:47:42 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 634B0401DEA7; Tue, 13 Mar 2018 14:47:39 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-214.ams2.redhat.com [10.36.117.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id 150DC202322A; Tue, 13 Mar 2018 14:47:37 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Mar 2018 15:47:23 +0100 Message-Id: <20180313144730.2184-2-kwolf@redhat.com> In-Reply-To: <20180313144730.2184-1-kwolf@redhat.com> References: <20180313144730.2184-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 13 Mar 2018 14:47:39 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 13 Mar 2018 14:47:39 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 v2 1/8] 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: Max Reitz Reviewed-by: Jeff Cody --- 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 6211b8222c..e0ab01d92d 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3625,6 +3625,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) @@ -3826,7 +3842,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 Mon May 6 22:52:33 2024 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 1520952644697387.8307139923296; Tue, 13 Mar 2018 07:50:44 -0700 (PDT) Received: from localhost ([::1]:40284 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlGF-00087w-U0 for importer@patchew.org; Tue, 13 Mar 2018 10:50:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57827) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlDQ-0005ts-7x for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evlDP-00071J-Aw for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:48 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:52386 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 1evlDK-0006xp-5F; Tue, 13 Mar 2018 10:47:42 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CB9AC4023BB3; Tue, 13 Mar 2018 14:47:40 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-214.ams2.redhat.com [10.36.117.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9DDDE2026DFD; Tue, 13 Mar 2018 14:47:39 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Mar 2018 15:47:24 +0100 Message-Id: <20180313144730.2184-3-kwolf@redhat.com> In-Reply-To: <20180313144730.2184-1-kwolf@redhat.com> References: <20180313144730.2184-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 13 Mar 2018 14:47:40 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 13 Mar 2018 14:47:40 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 v2 2/8] 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: Max Reitz Reviewed-by: Jeff Cody --- 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 Mon May 6 22:52:33 2024 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 1520952776790775.8242221199885; Tue, 13 Mar 2018 07:52:56 -0700 (PDT) Received: from localhost ([::1]:40299 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlIN-0001i2-R4 for importer@patchew.org; Tue, 13 Mar 2018 10:52:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57928) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlDU-0005zr-BA for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evlDS-00073u-Kc for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:52 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:52388 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 1evlDK-0006yN-ON; Tue, 13 Mar 2018 10:47:42 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 429E7406E8B9; Tue, 13 Mar 2018 14:47:42 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-214.ams2.redhat.com [10.36.117.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id 122A7202322A; Tue, 13 Mar 2018 14:47:40 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Mar 2018 15:47:25 +0100 Message-Id: <20180313144730.2184-4-kwolf@redhat.com> In-Reply-To: <20180313144730.2184-1-kwolf@redhat.com> References: <20180313144730.2184-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 13 Mar 2018 14:47:42 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 13 Mar 2018 14:47:42 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 v2 3/8] 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: Max Reitz Reviewed-by: Jeff Cody --- 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 e0ab01d92d..7b7d5a01fd 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3641,6 +3641,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) @@ -3843,8 +3862,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 Mon May 6 22:52:33 2024 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 152095296228062.093747594215074; Tue, 13 Mar 2018 07:56:02 -0700 (PDT) Received: from localhost ([::1]:40321 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlLA-0004fz-Is for importer@patchew.org; Tue, 13 Mar 2018 10:55:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57981) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlDa-00063E-Bi for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:48:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evlDU-000767-05 for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:58 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33526 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 1evlDM-0006zI-6F; Tue, 13 Mar 2018 10:47:44 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id ABE6F4040073; Tue, 13 Mar 2018 14:47:43 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-214.ams2.redhat.com [10.36.117.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7E199202322A; Tue, 13 Mar 2018 14:47:42 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Mar 2018 15:47:26 +0100 Message-Id: <20180313144730.2184-5-kwolf@redhat.com> In-Reply-To: <20180313144730.2184-1-kwolf@redhat.com> References: <20180313144730.2184-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 13 Mar 2018 14:47:43 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 13 Mar 2018 14:47:43 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 v2 4/8] 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 7b7d5a01fd..d091817855 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3703,6 +3703,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. @@ -3864,7 +3887,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 Mon May 6 22:52:33 2024 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 1520952621104287.28144830258805; Tue, 13 Mar 2018 07:50:21 -0700 (PDT) Received: from localhost ([::1]:40281 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlFj-0007fK-ID for importer@patchew.org; Tue, 13 Mar 2018 10:50:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57848) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlDQ-0005uA-T9 for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evlDP-00071l-UO for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:48 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:34686 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 1evlDN-000700-KT; Tue, 13 Mar 2018 10:47:45 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2055F401DEA7; Tue, 13 Mar 2018 14:47:45 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-214.ams2.redhat.com [10.36.117.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id E69A7202322A; Tue, 13 Mar 2018 14:47:43 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Mar 2018 15:47:27 +0100 Message-Id: <20180313144730.2184-6-kwolf@redhat.com> In-Reply-To: <20180313144730.2184-1-kwolf@redhat.com> References: <20180313144730.2184-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 13 Mar 2018 14:47:45 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 13 Mar 2018 14:47:45 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 v2 5/8] vdi: Make comments consistent with other drivers 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 makes the .bdrv_co_create(_opts) implementation of vdi look more like the other recently converted block drivers. Signed-off-by: Kevin Wolf Reviewed-by: Max Reitz --- block/vdi.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/block/vdi.c b/block/vdi.c index 8132e3adfe..d939b034c4 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -742,7 +742,7 @@ static int coroutine_fn vdi_co_do_create(BlockdevCreate= Options *create_options, =20 logout("\n"); =20 - /* Read out options. */ + /* Validate options and set default values */ bytes =3D vdi_opts->size; if (vdi_opts->q_static) { image_type =3D VDI_TYPE_STATIC; @@ -772,6 +772,7 @@ static int coroutine_fn vdi_co_do_create(BlockdevCreate= Options *create_options, goto exit; } =20 + /* Create BlockBackend to write to the image */ bs_file =3D bdrv_open_blockdev_ref(vdi_opts->file, errp); if (!bs_file) { ret =3D -EIO; @@ -877,7 +878,9 @@ static int coroutine_fn vdi_co_create_opts(const char *= filename, QemuOpts *opts, Error *local_err =3D NULL; int ret; =20 - /* Since CONFIG_VDI_BLOCK_SIZE is disabled by default, + /* Parse options and convert legacy syntax. + * + * Since CONFIG_VDI_BLOCK_SIZE is disabled by default, * cluster-size is not part of the QAPI schema; therefore we have * to parse it before creating the QAPI object. */ #if defined(CONFIG_VDI_BLOCK_SIZE) @@ -895,6 +898,7 @@ static int coroutine_fn vdi_co_create_opts(const char *= filename, QemuOpts *opts, =20 qdict =3D qemu_opts_to_qdict_filtered(opts, NULL, &vdi_create_opts, tr= ue); =20 + /* Create and open the file (protocol layer) */ ret =3D bdrv_create_file(filename, opts, errp); if (ret < 0) { goto done; @@ -921,10 +925,12 @@ static int coroutine_fn vdi_co_create_opts(const char= *filename, QemuOpts *opts, goto done; } =20 + /* 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); =20 + /* Create the vdi image (format layer) */ ret =3D vdi_co_do_create(create_options, block_size, errp); done: QDECREF(qdict); @@ -981,8 +987,8 @@ 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_opts =3D vdi_co_create_opts, .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, .bdrv_make_empty =3D vdi_make_empty, --=20 2.13.6 From nobody Mon May 6 22:52:33 2024 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 15209529622866.465247310894597; Tue, 13 Mar 2018 07:56:02 -0700 (PDT) Received: from localhost ([::1]:40320 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlL9-0004dt-4N for importer@patchew.org; Tue, 13 Mar 2018 10:55:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57979) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlDa-00062k-2V for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:48:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evlDY-00078A-7Q for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:58 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:48628 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 1evlDO-00070w-WF; Tue, 13 Mar 2018 10:47:47 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 89BE37CBBA; Tue, 13 Mar 2018 14:47:46 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-214.ams2.redhat.com [10.36.117.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5B1BE2026DFD; Tue, 13 Mar 2018 14:47:45 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Mar 2018 15:47:28 +0100 Message-Id: <20180313144730.2184-7-kwolf@redhat.com> In-Reply-To: <20180313144730.2184-1-kwolf@redhat.com> References: <20180313144730.2184-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 13 Mar 2018 14:47:46 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 13 Mar 2018 14:47:46 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 v2 6/8] 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: Max Reitz --- qapi/block-core.json | 40 +++++++++- block/vhdx.c | 216 ++++++++++++++++++++++++++++++++++++++---------= ---- 2 files changed, 203 insertions(+), 53 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index d091817855..350094f46a 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3842,6 +3842,44 @@ '*static': 'bool' } } =20 ## +# @BlockdevVhdxSubformat: +# +# @dynamic: Growing image file +# @fixed: Preallocated fixed-size image file +# +# Since: 2.12 +## +{ 'enum': 'BlockdevVhdxSubformat', + 'data': [ 'dynamic', 'fixed' ] } + +## +# @BlockdevCreateOptionsVhdx: +# +# Driver specific image creation options for vhdx. +# +# @file Node to create the image format on +# @size Size of the virtual disk in bytes +# @log-size Log size in bytes, must be a multiple of 1 MB +# (default: 1 MB) +# @block-size Block size in bytes, must be a multiple of 1 MB and not +# larger than 256 MB (default: automatically choose a bl= ock +# size depending on the image size) +# @subformat vhdx subformat (default: dynamic) +# @block-state-zero Force use of payload blocks of type 'ZERO'. Non-standa= rd, +# but default. Do not set to 'off' when using 'qemu-img +# convert' with subformat=3Ddynamic. +# +# Since: 2.12 +## +{ 'struct': 'BlockdevCreateOptionsVhdx', + 'data': { 'file': 'BlockdevRef', + 'size': 'size', + '*log-size': 'size', + '*block-size': 'size', + '*subformat': 'BlockdevVhdxSubformat', + '*block-state-zero': 'bool' } } + +## # @BlockdevCreateNotSupported: # # This is used for all drivers that don't support creating images. @@ -3896,7 +3934,7 @@ 'ssh': 'BlockdevCreateOptionsSsh', 'throttle': 'BlockdevCreateNotSupported', 'vdi': 'BlockdevCreateOptionsVdi', - 'vhdx': 'BlockdevCreateNotSupported', + 'vhdx': 'BlockdevCreateOptionsVhdx', 'vmdk': 'BlockdevCreateNotSupported', 'vpc': 'BlockdevCreateNotSupported', 'vvfat': 'BlockdevCreateNotSupported', diff --git a/block/vhdx.c b/block/vhdx.c index d82350d07c..f1b97f4b49 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -26,6 +26,9 @@ #include "block/vhdx.h" #include "migration/blocker.h" #include "qemu/uuid.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qapi-visit-block-core.h" =20 /* Options for VHDX creation */ =20 @@ -39,6 +42,8 @@ typedef enum VHDXImageType { VHDX_TYPE_DIFFERENCING, /* Currently unsupported */ } VHDXImageType; =20 +static QemuOptsList vhdx_create_opts; + /* Several metadata and region table data entries are identified by * guids in a MS-specific GUID format. */ =20 @@ -1792,59 +1797,71 @@ exit: * .---- ~ ----------- ~ ------------ ~ ---------------- ~ -----------. * 1MB */ -static int coroutine_fn vhdx_co_create_opts(const char *filename, QemuOpts= *opts, - Error **errp) +static int coroutine_fn vhdx_co_create(BlockdevCreateOptions *opts, + Error **errp) { + BlockdevCreateOptionsVhdx *vhdx_opts; + BlockBackend *blk =3D NULL; + BlockDriverState *bs =3D NULL; + int ret =3D 0; - uint64_t image_size =3D (uint64_t) 2 * GiB; - uint32_t log_size =3D 1 * MiB; - uint32_t block_size =3D 0; + uint64_t image_size; + uint32_t log_size; + uint32_t block_size; uint64_t signature; uint64_t metadata_offset; bool use_zero_blocks =3D false; =20 gunichar2 *creator =3D NULL; glong creator_items; - BlockBackend *blk; - char *type =3D NULL; VHDXImageType image_type; - Error *local_err =3D NULL; =20 - image_size =3D ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), - BDRV_SECTOR_SIZE); - log_size =3D qemu_opt_get_size_del(opts, VHDX_BLOCK_OPT_LOG_SIZE, 0); - block_size =3D qemu_opt_get_size_del(opts, VHDX_BLOCK_OPT_BLOCK_SIZE, = 0); - type =3D qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT); - use_zero_blocks =3D qemu_opt_get_bool_del(opts, VHDX_BLOCK_OPT_ZERO, t= rue); + assert(opts->driver =3D=3D BLOCKDEV_DRIVER_VHDX); + vhdx_opts =3D &opts->u.vhdx; =20 + /* Validate options and set default values */ + image_size =3D vhdx_opts->size; if (image_size > VHDX_MAX_IMAGE_SIZE) { error_setg_errno(errp, EINVAL, "Image size too large; max of 64TB"= ); - ret =3D -EINVAL; - goto exit; + return -EINVAL; } =20 - if (type =3D=3D NULL) { - type =3D g_strdup("dynamic"); + if (!vhdx_opts->has_log_size) { + log_size =3D DEFAULT_LOG_SIZE; + } else { + log_size =3D vhdx_opts->log_size; + } + if (log_size < MiB || (log_size % MiB) !=3D 0) { + error_setg_errno(errp, EINVAL, "Log size must be a multiple of 1 M= B"); + return -EINVAL; } =20 - if (!strcmp(type, "dynamic")) { + if (!vhdx_opts->has_block_state_zero) { + use_zero_blocks =3D true; + } else { + use_zero_blocks =3D vhdx_opts->block_state_zero; + } + + if (!vhdx_opts->has_subformat) { + vhdx_opts->subformat =3D BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC; + } + + switch (vhdx_opts->subformat) { + case BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC: image_type =3D VHDX_TYPE_DYNAMIC; - } else if (!strcmp(type, "fixed")) { + break; + case BLOCKDEV_VHDX_SUBFORMAT_FIXED: image_type =3D VHDX_TYPE_FIXED; - } else if (!strcmp(type, "differencing")) { - error_setg_errno(errp, ENOTSUP, - "Differencing files not yet supported"); - ret =3D -ENOTSUP; - goto exit; - } else { - error_setg(errp, "Invalid subformat '%s'", type); - ret =3D -EINVAL; - goto exit; + break; + default: + g_assert_not_reached(); } =20 /* These are pretty arbitrary, and mainly designed to keep the BAT * size reasonable to load into RAM */ - if (block_size =3D=3D 0) { + if (vhdx_opts->has_block_size) { + block_size =3D vhdx_opts->block_size; + } else { if (image_size > 32 * TiB) { block_size =3D 64 * MiB; } else if (image_size > (uint64_t) 100 * GiB) { @@ -1856,30 +1873,27 @@ static int coroutine_fn vhdx_co_create_opts(const c= har *filename, QemuOpts *opts } } =20 - - /* make the log size close to what was specified, but must be - * min 1MB, and multiple of 1MB */ - log_size =3D ROUND_UP(log_size, MiB); - - block_size =3D ROUND_UP(block_size, MiB); - block_size =3D block_size > VHDX_BLOCK_SIZE_MAX ? VHDX_BLOCK_SIZE_MAX : - block_size; - - ret =3D bdrv_create_file(filename, opts, &local_err); - if (ret < 0) { - error_propagate(errp, local_err); - goto exit; + if (block_size < MiB || (block_size % MiB) !=3D 0) { + error_setg_errno(errp, EINVAL, "Block size must be a multiple of 1= MB"); + return -EINVAL; + } + if (block_size > VHDX_BLOCK_SIZE_MAX) { + error_setg_errno(errp, EINVAL, "Block size must not exceed %d", + VHDX_BLOCK_SIZE_MAX); + return -EINVAL; } =20 - blk =3D blk_new_open(filename, NULL, NULL, - BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, - &local_err); - if (blk =3D=3D NULL) { - error_propagate(errp, local_err); - ret =3D -EIO; - goto exit; + /* Create BlockBackend to write to the image */ + bs =3D bdrv_open_blockdev_ref(vhdx_opts->file, errp); + if (bs =3D=3D NULL) { + return -EIO; } =20 + blk =3D blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); + ret =3D blk_insert_bs(blk, bs, errp); + if (ret < 0) { + goto delete_and_exit; + } blk_set_allow_write_beyond_eof(blk, true); =20 /* Create (A) */ @@ -1931,12 +1945,109 @@ static int coroutine_fn vhdx_co_create_opts(const = char *filename, QemuOpts *opts =20 delete_and_exit: blk_unref(blk); -exit: - g_free(type); + bdrv_unref(bs); g_free(creator); return ret; } =20 +static int coroutine_fn vhdx_co_create_opts(const char *filename, + QemuOpts *opts, + Error **errp) +{ + BlockdevCreateOptions *create_options =3D NULL; + QDict *qdict =3D NULL; + QObject *qobj; + Visitor *v; + BlockDriverState *bs =3D NULL; + Error *local_err =3D NULL; + int ret; + + static const QDictRenames opt_renames[] =3D { + { VHDX_BLOCK_OPT_LOG_SIZE, "log-size" }, + { VHDX_BLOCK_OPT_BLOCK_SIZE, "block-size" }, + { VHDX_BLOCK_OPT_ZERO, "block-state-zero" }, + { NULL, NULL }, + }; + + /* Parse options and convert legacy syntax */ + qdict =3D qemu_opts_to_qdict_filtered(opts, NULL, &vhdx_create_opts, t= rue); + + if (!qdict_rename_keys(qdict, opt_renames, errp)) { + ret =3D -EINVAL; + goto fail; + } + + /* Create and open the file (protocol layer) */ + ret =3D bdrv_create_file(filename, opts, &local_err); + if (ret < 0) { + error_propagate(errp, local_err); + goto fail; + } + + bs =3D bdrv_open(filename, NULL, NULL, + BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); + if (bs =3D=3D NULL) { + ret =3D -EIO; + goto fail; + } + + /* Now get the QAPI type BlockdevCreateOptions */ + qdict_put_str(qdict, "driver", "vhdx"); + qdict_put_str(qdict, "file", bs->node_name); + + qobj =3D qdict_crumple(qdict, errp); + QDECREF(qdict); + qdict =3D qobject_to_qdict(qobj); + if (qdict =3D=3D NULL) { + ret =3D -EINVAL; + goto fail; + } + + v =3D qobject_input_visitor_new_keyval(QOBJECT(qdict)); + visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err); + visit_free(v); + + if (local_err) { + error_propagate(errp, local_err); + ret =3D -EINVAL; + goto fail; + } + + /* Silently round up sizes: + * The image size is rounded to 512 bytes. Make the block and log size + * close to what was specified, but must be at least 1MB, and a multip= le of + * 1 MB. Also respect VHDX_BLOCK_SIZE_MAX for block sizes. block_size = =3D 0 + * means auto, which is represented by a missing key in QAPI. */ + assert(create_options->driver =3D=3D BLOCKDEV_DRIVER_VHDX); + create_options->u.vhdx.size =3D + ROUND_UP(create_options->u.vhdx.size, BDRV_SECTOR_SIZE); + + if (create_options->u.vhdx.has_log_size) { + create_options->u.vhdx.log_size =3D + ROUND_UP(create_options->u.vhdx.log_size, MiB); + } + if (create_options->u.vhdx.has_block_size) { + create_options->u.vhdx.block_size =3D + ROUND_UP(create_options->u.vhdx.block_size, MiB); + + if (create_options->u.vhdx.block_size =3D=3D 0) { + create_options->u.vhdx.has_block_size =3D false; + } + if (create_options->u.vhdx.block_size > VHDX_BLOCK_SIZE_MAX) { + create_options->u.vhdx.block_size =3D VHDX_BLOCK_SIZE_MAX; + } + } + + /* Create the vhdx image (format layer) */ + ret =3D vhdx_co_create(create_options, errp); + +fail: + QDECREF(qdict); + bdrv_unref(bs); + qapi_free_BlockdevCreateOptions(create_options); + return ret; +} + /* If opened r/w, the VHDX driver will automatically replay the log, * if one is present, inside the vhdx_open() call. * @@ -2005,6 +2116,7 @@ static BlockDriver bdrv_vhdx =3D { .bdrv_child_perm =3D bdrv_format_default_perms, .bdrv_co_readv =3D vhdx_co_readv, .bdrv_co_writev =3D vhdx_co_writev, + .bdrv_co_create =3D vhdx_co_create, .bdrv_co_create_opts =3D vhdx_co_create_opts, .bdrv_get_info =3D vhdx_get_info, .bdrv_co_check =3D vhdx_co_check, --=20 2.13.6 From nobody Mon May 6 22:52:33 2024 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 1520952626030187.5683536777933; Tue, 13 Mar 2018 07:50:26 -0700 (PDT) Received: from localhost ([::1]:40282 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlFw-0007tI-S3 for importer@patchew.org; Tue, 13 Mar 2018 10:50:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57954) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlDY-00060w-0J for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evlDU-00076C-1L for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:56 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33530 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 1evlDQ-000720-ES; Tue, 13 Mar 2018 10:47:48 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F18984040073; Tue, 13 Mar 2018 14:47:47 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-214.ams2.redhat.com [10.36.117.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id C3FA4202322A; Tue, 13 Mar 2018 14:47:46 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Mar 2018 15:47:29 +0100 Message-Id: <20180313144730.2184-8-kwolf@redhat.com> In-Reply-To: <20180313144730.2184-1-kwolf@redhat.com> References: <20180313144730.2184-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 13 Mar 2018 14:47:48 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 13 Mar 2018 14:47:48 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 v2 7/8] 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 350094f46a..47ff5f8ce5 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3880,6 +3880,37 @@ '*block-state-zero': 'bool' } } =20 ## +# @BlockdevVpcSubformat: +# +# @dynamic: Growing image file +# @fixed: Preallocated fixed-size image 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. @@ -3936,7 +3967,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 From nobody Mon May 6 22:52:33 2024 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 1520952793413240.99975207962257; Tue, 13 Mar 2018 07:53:13 -0700 (PDT) Received: from localhost ([::1]:40302 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlId-0001v2-Mq for importer@patchew.org; Tue, 13 Mar 2018 10:53:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57953) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evlDY-00060v-08 for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evlDU-00076s-VG for qemu-devel@nongnu.org; Tue, 13 Mar 2018 10:47:56 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:48998 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 1evlDR-000734-Tb; Tue, 13 Mar 2018 10:47:49 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 676FE7B4C8; Tue, 13 Mar 2018 14:47:49 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-214.ams2.redhat.com [10.36.117.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id 38B10202322A; Tue, 13 Mar 2018 14:47:48 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Mar 2018 15:47:30 +0100 Message-Id: <20180313144730.2184-9-kwolf@redhat.com> In-Reply-To: <20180313144730.2184-1-kwolf@redhat.com> References: <20180313144730.2184-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Tue, 13 Mar 2018 14:47:49 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Tue, 13 Mar 2018 14:47:49 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 v2 8/8] vpc: Require aligned size in .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" Perform the rounding to match a CHS geometry only in the legacy code path in .bdrv_co_create_opts. QMP now requires that the user already passes a CHS aligned image size, unless force-size=3Dtrue is given. CHS alignment is required to make the image compatible with Virtual PC, but not for use with newer Microsoft hypervisors. Signed-off-by: Kevin Wolf Reviewed-by: Max Reitz --- block/vpc.c | 113 +++++++++++++++++++++++++++++++++++++++++++-------------= ---- 1 file changed, 82 insertions(+), 31 deletions(-) diff --git a/block/vpc.c b/block/vpc.c index 8824211713..28ffa0d2f8 100644 --- a/block/vpc.c +++ b/block/vpc.c @@ -902,6 +902,62 @@ static int create_fixed_disk(BlockBackend *blk, uint8_= t *buf, return ret; } =20 +static int calculate_rounded_image_size(BlockdevCreateOptionsVpc *vpc_opts, + uint16_t *out_cyls, + uint8_t *out_heads, + uint8_t *out_secs_per_cyl, + int64_t *out_total_sectors, + Error **errp) +{ + int64_t total_size =3D vpc_opts->size; + uint16_t cyls =3D 0; + uint8_t heads =3D 0; + uint8_t secs_per_cyl =3D 0; + int64_t total_sectors; + int i; + + /* + * Calculate matching total_size and geometry. Increase the number of + * sectors requested until we get enough (or fail). This ensures that + * qemu-img convert doesn't truncate images, but rather rounds up. + * + * If the image size can't be represented by a spec conformant CHS geo= metry, + * 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 (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; + secs_per_cyl =3D VHD_CHS_MAX_S; + } else { + total_sectors =3D MIN(VHD_MAX_GEOMETRY, total_size / BDRV_SECTOR_S= IZE); + for (i =3D 0; total_sectors > (int64_t)cyls * heads * secs_per_cyl= ; i++) { + calculate_geometry(total_sectors + i, &cyls, &heads, &secs_per= _cyl); + } + } + + if ((int64_t)cyls * heads * secs_per_cyl =3D=3D VHD_MAX_GEOMETRY) { + total_sectors =3D total_size / BDRV_SECTOR_SIZE; + /* Allow a maximum disk size of 2040 GiB */ + if (total_sectors > VHD_MAX_SECTORS) { + error_setg(errp, "Disk size is too large, max size is 2040 GiB= "); + return -EFBIG; + } + } else { + total_sectors =3D (int64_t) cyls * heads * secs_per_cyl; + } + + *out_total_sectors =3D total_sectors; + if (out_cyls) { + *out_cyls =3D cyls; + *out_heads =3D heads; + *out_secs_per_cyl =3D secs_per_cyl; + } + + return 0; +} + static int coroutine_fn vpc_co_create(BlockdevCreateOptions *opts, Error **errp) { @@ -911,7 +967,6 @@ static int coroutine_fn vpc_co_create(BlockdevCreateOpt= ions *opts, =20 uint8_t buf[1024]; VHDFooter *footer =3D (VHDFooter *) buf; - int i; uint16_t cyls =3D 0; uint8_t heads =3D 0; uint8_t secs_per_cyl =3D 0; @@ -953,38 +1008,22 @@ static int coroutine_fn vpc_co_create(BlockdevCreate= Options *opts, } blk_set_allow_write_beyond_eof(blk, true); =20 - /* - * Calculate matching total_size and geometry. Increase the number of - * sectors requested until we get enough (or fail). This ensures that - * qemu-img convert doesn't truncate images, but rather rounds up. - * - * If the image size can't be represented by a spec conformant CHS geo= metry, - * 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 (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; - secs_per_cyl =3D VHD_CHS_MAX_S; - } else { - total_sectors =3D MIN(VHD_MAX_GEOMETRY, total_size / BDRV_SECTOR_S= IZE); - for (i =3D 0; total_sectors > (int64_t)cyls * heads * secs_per_cyl= ; i++) { - calculate_geometry(total_sectors + i, &cyls, &heads, &secs_per= _cyl); - } + /* Get geometry and check that it matches the image size*/ + ret =3D calculate_rounded_image_size(vpc_opts, &cyls, &heads, &secs_pe= r_cyl, + &total_sectors, errp); + if (ret < 0) { + goto out; } =20 - if ((int64_t)cyls * heads * secs_per_cyl =3D=3D VHD_MAX_GEOMETRY) { - total_sectors =3D total_size / BDRV_SECTOR_SIZE; - /* Allow a maximum disk size of 2040 GiB */ - if (total_sectors > VHD_MAX_SECTORS) { - error_setg(errp, "Disk size is too large, max size is 2040 GiB= "); - ret =3D -EFBIG; - goto out; - } - } else { - total_sectors =3D (int64_t)cyls * heads * secs_per_cyl; - total_size =3D total_sectors * BDRV_SECTOR_SIZE; + if (total_size !=3D total_sectors * BDRV_SECTOR_SIZE) { + error_setg(errp, "The requested image size cannot be represented i= n " + "CHS geometry"); + error_append_hint(errp, "Try size=3D%llu or force-size=3Don (the " + "latter makes the image incompatible with " + "Virtual PC)", + total_sectors * BDRV_SECTOR_SIZE); + ret =3D -EINVAL; + goto out; } =20 /* Prepare the Hard Disk Footer */ @@ -1102,6 +1141,18 @@ static int coroutine_fn vpc_co_create_opts(const cha= r *filename, create_options->u.vpc.size =3D ROUND_UP(create_options->u.vpc.size, BDRV_SECTOR_SIZE); =20 + if (!create_options->u.vpc.force_size) { + int64_t total_sectors; + ret =3D calculate_rounded_image_size(&create_options->u.vpc, NULL,= NULL, + NULL, &total_sectors, errp); + if (ret < 0) { + goto fail; + } + + create_options->u.vpc.size =3D total_sectors * BDRV_SECTOR_SIZE; + } + + /* Create the vpc image (format layer) */ ret =3D vpc_co_create(create_options, errp); =20 --=20 2.13.6