From nobody Wed Dec 17 05:38:22 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 1520960797301593.2217726077465; Tue, 13 Mar 2018 10:06:37 -0700 (PDT) Received: from localhost ([::1]:41308 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evnNk-0000oo-BC for importer@patchew.org; Tue, 13 Mar 2018 13:06:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60318) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evmdl-00024m-TQ for qemu-devel@nongnu.org; Tue, 13 Mar 2018 12:19:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evmdf-00053v-Uq for qemu-devel@nongnu.org; Tue, 13 Mar 2018 12:19:05 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:36442 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 1evmdW-0004sI-5E; Tue, 13 Mar 2018 12:18:50 -0400 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 C7C1C4068031; Tue, 13 Mar 2018 16:18: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 048622166BB2; Tue, 13 Mar 2018 16:18:48 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Mar 2018 17:18:02 +0100 Message-Id: <20180313161803.1814-41-kwolf@redhat.com> In-Reply-To: <20180313161803.1814-1-kwolf@redhat.com> References: <20180313161803.1814-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]); Tue, 13 Mar 2018 16:18:49 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 13 Mar 2018 16:18:49 +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] [PULL 40/41] 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, peter.maydell@linaro.org, qemu-devel@nongnu.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