From nobody Sat Oct 25 20:22:59 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 15206326987821019.1745494251359; Fri, 9 Mar 2018 13:58:18 -0800 (PST) Received: from localhost ([::1]:48276 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euQ1n-0007i2-Oj for importer@patchew.org; Fri, 09 Mar 2018 16:58:15 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35882) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euPqe-0007KE-Mn for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euPqa-0000qy-Qf for qemu-devel@nongnu.org; Fri, 09 Mar 2018 16:46:44 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:36972 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1euPqW-0000lJ-Ta; Fri, 09 Mar 2018 16:46:37 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6CF178D6EC; Fri, 9 Mar 2018 21:46:36 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-234.ams2.redhat.com [10.36.117.234]) by smtp.corp.redhat.com (Postfix) with ESMTP id 39743218CC07; Fri, 9 Mar 2018 21:46:35 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 9 Mar 2018 22:46:09 +0100 Message-Id: <20180309214611.19122-6-kwolf@redhat.com> In-Reply-To: <20180309214611.19122-1-kwolf@redhat.com> References: <20180309214611.19122-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 09 Mar 2018 21:46:36 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 09 Mar 2018 21:46:36 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 5/7] vdi: Support .bdrv_co_create X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This adds the .bdrv_co_create driver callback to vdi, which enables image creation over QMP. Signed-off-by: Kevin Wolf --- qapi/block-core.json | 21 ++++++- block/vdi.c | 169 ++++++++++++++++++++++++++++++++++++++---------= ---- 2 files changed, 148 insertions(+), 42 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index 1e2edbc063..2eba0eef7e 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3680,6 +3680,25 @@ 'size': 'size' } } =20 ## +# @BlockdevCreateOptionsVdi: +# +# Driver specific image creation options for vdi. +# +# @file Node to create the image format on +# @size Size of the virtual disk in bytes +# @cluster-size Cluster size in bytes (default: 1 MB) +# @static Whether to create a static (preallocated) image +# (default: false) +# +# Since: 2.12 +## +{ 'struct': 'BlockdevCreateOptionsVdi', + 'data': { 'file': 'BlockdevRef', + 'size': 'size', + '*cluster-size': 'size', + '*static': 'bool' } } + +## # @BlockdevCreateNotSupported: # # This is used for all drivers that don't support creating images. @@ -3733,7 +3752,7 @@ 'sheepdog': 'BlockdevCreateOptionsSheepdog', 'ssh': 'BlockdevCreateOptionsSsh', 'throttle': 'BlockdevCreateNotSupported', - 'vdi': 'BlockdevCreateNotSupported', + 'vdi': 'BlockdevCreateOptionsVdi', 'vhdx': 'BlockdevCreateNotSupported', 'vmdk': 'BlockdevCreateNotSupported', 'vpc': 'BlockdevCreateNotSupported', diff --git a/block/vdi.c b/block/vdi.c index 2b5ddd0666..c60ddc58c0 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -60,6 +60,9 @@ #include "qemu/coroutine.h" #include "qemu/cutils.h" #include "qemu/uuid.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qapi-visit-block-core.h" =20 /* Code configuration options. */ =20 @@ -182,6 +185,8 @@ typedef struct { Error *migration_blocker; } BDRVVdiState; =20 +static QemuOptsList vdi_create_opts; + static void vdi_header_to_cpu(VdiHeader *header) { le32_to_cpus(&header->signature); @@ -716,67 +721,72 @@ nonallocating_write: return ret; } =20 -static int coroutine_fn vdi_co_create_opts(const char *filename, QemuOpts = *opts, - Error **errp) +static int coroutine_fn vdi_co_create(BlockdevCreateOptions *opts, + Error **errp) { + BlockdevCreateOptionsVdi *vdi_opts; + BlockBackend *blk =3D NULL; + BlockDriverState *bs =3D NULL; + int ret =3D 0; - uint64_t bytes =3D 0; uint32_t blocks; - size_t block_size =3D DEFAULT_CLUSTER_SIZE; uint32_t image_type =3D VDI_TYPE_DYNAMIC; VdiHeader header; size_t i; size_t bmap_size; int64_t offset =3D 0; - Error *local_err =3D NULL; - BlockBackend *blk =3D NULL; uint32_t *bmap =3D NULL; =20 logout("\n"); =20 - /* Read out options. */ - bytes =3D ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), - BDRV_SECTOR_SIZE); -#if defined(CONFIG_VDI_BLOCK_SIZE) - /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */ - block_size =3D qemu_opt_get_size_del(opts, - BLOCK_OPT_CLUSTER_SIZE, - DEFAULT_CLUSTER_SIZE); -#endif -#if defined(CONFIG_VDI_STATIC_IMAGE) - if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) { - image_type =3D VDI_TYPE_STATIC; + assert(opts->driver =3D=3D BLOCKDEV_DRIVER_VDI); + vdi_opts =3D &opts->u.vdi; + + /* Validate options and set default values */ + if (!vdi_opts->has_cluster_size) { + vdi_opts->cluster_size =3D DEFAULT_CLUSTER_SIZE; } -#endif =20 - if (bytes > VDI_DISK_SIZE_MAX) { - ret =3D -ENOTSUP; + if (vdi_opts->size > VDI_DISK_SIZE_MAX) { error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64 ", max supported is 0x%" PRIx64 ")", - bytes, VDI_DISK_SIZE_MAX); - goto exit; + vdi_opts->size, VDI_DISK_SIZE_MAX); + return -ENOTSUP; } =20 - ret =3D bdrv_create_file(filename, opts, &local_err); - if (ret < 0) { - error_propagate(errp, local_err); - goto exit; +#if !defined(CONFIG_VDI_BLOCK_SIZE) + if (vdi_opts->has_cluster_size) { + error_setg(errp, "Non-default cluster size not supported"); + return -ENOTSUP; + } +#endif +#if !defined(CONFIG_VDI_STATIC_IMAGE) + if (vdi_opts->has_static) { + error_setg(errp, "Static images not supported"); + return -ENOTSUP; + } +#else + if (vdi_opts->q_static) { + image_type =3D VDI_TYPE_STATIC; + } +#endif + + /* Create BlockBackend to write to the image */ + bs =3D bdrv_open_blockdev_ref(vdi_opts->file, errp); + if (bs =3D=3D NULL) { + return -EIO; } =20 - blk =3D blk_new_open(filename, NULL, NULL, - BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, - &local_err); - if (blk =3D=3D NULL) { - error_propagate(errp, local_err); - ret =3D -EIO; + blk =3D blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); + ret =3D blk_insert_bs(blk, bs, errp); + if (ret < 0) { goto exit; } - blk_set_allow_write_beyond_eof(blk, true); =20 /* We need enough blocks to store the given disk size, so always round up. */ - blocks =3D DIV_ROUND_UP(bytes, block_size); + blocks =3D DIV_ROUND_UP(vdi_opts->size, vdi_opts->cluster_size); =20 bmap_size =3D blocks * sizeof(uint32_t); bmap_size =3D ROUND_UP(bmap_size, SECTOR_SIZE); @@ -790,8 +800,8 @@ static int coroutine_fn vdi_co_create_opts(const char *= filename, QemuOpts *opts, header.offset_bmap =3D 0x200; header.offset_data =3D 0x200 + bmap_size; header.sector_size =3D SECTOR_SIZE; - header.disk_size =3D bytes; - header.block_size =3D block_size; + header.disk_size =3D vdi_opts->size; + header.block_size =3D vdi_opts->cluster_size; header.blocks_in_image =3D blocks; if (image_type =3D=3D VDI_TYPE_STATIC) { header.blocks_allocated =3D blocks; @@ -805,7 +815,7 @@ static int coroutine_fn vdi_co_create_opts(const char *= filename, QemuOpts *opts, vdi_header_to_le(&header); ret =3D blk_pwrite(blk, offset, &header, sizeof(header), 0); if (ret < 0) { - error_setg(errp, "Error writing header to %s", filename); + error_setg(errp, "Error writing header"); goto exit; } offset +=3D sizeof(header); @@ -826,27 +836,103 @@ static int coroutine_fn vdi_co_create_opts(const cha= r *filename, QemuOpts *opts, } ret =3D blk_pwrite(blk, offset, bmap, bmap_size, 0); if (ret < 0) { - error_setg(errp, "Error writing bmap to %s", filename); + error_setg(errp, "Error writing bmap"); goto exit; } offset +=3D bmap_size; } =20 if (image_type =3D=3D VDI_TYPE_STATIC) { - ret =3D blk_truncate(blk, offset + blocks * block_size, + ret =3D blk_truncate(blk, offset + blocks * vdi_opts->cluster_size, PREALLOC_MODE_OFF, errp); if (ret < 0) { - error_prepend(errp, "Failed to statically allocate %s", filena= me); + error_prepend(errp, "Failed to statically allocate image"); goto exit; } } =20 exit: blk_unref(blk); + bdrv_unref(bs); g_free(bmap); return ret; } =20 +static int coroutine_fn vdi_co_create_opts(const char *filename, QemuOpts = *opts, + Error **errp) +{ + BlockdevCreateOptions *create_options =3D NULL; + QDict *qdict =3D NULL; + QObject *qobj; + Visitor *v; + BlockDriverState *bs =3D NULL; + Error *local_err =3D NULL; + int ret; + + static const QDictRenames opt_renames[] =3D { + { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" }, + { NULL, NULL }, + }; + + /* Parse options and convert legacy syntax */ + qdict =3D qemu_opts_to_qdict_filtered(opts, NULL, &vdi_create_opts, tr= ue); + + if (!qdict_rename_keys(qdict, opt_renames, errp)) { + ret =3D -EINVAL; + goto fail; + } + + /* Create and open the file (protocol layer) */ + ret =3D bdrv_create_file(filename, opts, &local_err); + if (ret < 0) { + error_propagate(errp, local_err); + goto fail; + } + + bs =3D bdrv_open(filename, NULL, NULL, + BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); + if (bs =3D=3D NULL) { + ret =3D -EIO; + goto fail; + } + + /* Now get the QAPI type BlockdevCreateOptions */ + qdict_put_str(qdict, "driver", "vdi"); + qdict_put_str(qdict, "file", bs->node_name); + + qobj =3D qdict_crumple(qdict, errp); + QDECREF(qdict); + qdict =3D qobject_to_qdict(qobj); + if (qdict =3D=3D NULL) { + ret =3D -EINVAL; + goto fail; + } + + v =3D qobject_input_visitor_new_keyval(QOBJECT(qdict)); + visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err); + visit_free(v); + + if (local_err) { + error_propagate(errp, local_err); + ret =3D -EINVAL; + goto fail; + } + + /* Silently round up size */ + assert(create_options->driver =3D=3D BLOCKDEV_DRIVER_VDI); + create_options->u.vdi.size =3D + ROUND_UP(create_options->u.vdi.size, BDRV_SECTOR_SIZE); + + /* Create the vdi image (format layer) */ + ret =3D vdi_co_create(create_options, errp); + +fail: + QDECREF(qdict); + bdrv_unref(bs); + qapi_free_BlockdevCreateOptions(create_options); + return ret; +} + static void vdi_close(BlockDriverState *bs) { BDRVVdiState *s =3D bs->opaque; @@ -895,6 +981,7 @@ static BlockDriver bdrv_vdi =3D { .bdrv_close =3D vdi_close, .bdrv_reopen_prepare =3D vdi_reopen_prepare, .bdrv_child_perm =3D bdrv_format_default_perms, + .bdrv_co_create =3D vdi_co_create, .bdrv_co_create_opts =3D vdi_co_create_opts, .bdrv_has_zero_init =3D bdrv_has_zero_init_1, .bdrv_co_block_status =3D vdi_co_block_status, --=20 2.13.6