From nobody Sat Oct 25 20:23:01 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 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