From nobody Thu May 16 13:33:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1587680370; cv=none; d=zohomail.com; s=zohoarc; b=Er7qQlptG5NkOIvICzcfNwSWxN7t4UKKOdKv8/4QYTnl+CNhHkhTTqcESpunOvk8wNVgWlMHxEx2SP6w6vQUlksMbi+Q3sKLjy8gx8c0zokRgdwN1FYXbDrHxMWHxrj2PfaqKRSKR/znIeUO51LohC67t5SuxB7ni4Aa0zFMpEU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1587680370; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=wECKI9TX9peXLf8DLeDr58MjRiSx0mHFD+bV5rYUJ8A=; b=DfteUfWj1mb7O81uoyZWjpab2OZj0rhDjkKsafZEc3zK7uF71AxlUHx+AZqdttJn+NgraUjheYlWQRvhSbeSvuyN2xFnjrAH7ErjG7VqSQNBvR1aoGUvVjTHwzB7WNbVxmE+HQsFma/lnN9FMxBIPttSH2gkJeBG4a0l15pbVd0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1587680370617841.1487794655516; Thu, 23 Apr 2020 15:19:30 -0700 (PDT) Received: from localhost ([::1]:43088 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jRkBq-0001UJ-3r for importer@patchew.org; Thu, 23 Apr 2020 18:19:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49230) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jRkA0-0007br-DW for qemu-devel@nongnu.org; Thu, 23 Apr 2020 18:17:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jRk9v-0006sj-OM for qemu-devel@nongnu.org; Thu, 23 Apr 2020 18:17:32 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:29513 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jRk9u-0006lU-Ci for qemu-devel@nongnu.org; Thu, 23 Apr 2020 18:17:26 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-131-QIDC8n1UMqGeUIhqoLgpFA-1; Thu, 23 Apr 2020 18:17:17 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A5EA41895910; Thu, 23 Apr 2020 22:17:15 +0000 (UTC) Received: from blue.redhat.com (ovpn-116-80.rdu2.redhat.com [10.10.116.80]) by smtp.corp.redhat.com (Postfix) with ESMTP id BB86C1C94D; Thu, 23 Apr 2020 22:17:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1587680241; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wECKI9TX9peXLf8DLeDr58MjRiSx0mHFD+bV5rYUJ8A=; b=ZQix6OYA1XoNMw5xII7Xw6yCRk5i2o1eRjnFmOSVVFKmN/dv93y0Cj6faL35vQEt9ftOBY FkqOOKBcTITDZ6a87WW+SuaBUAR3f/ZL0xuwq0aPVAYr+5mM3GL6naUFMErG/sl9a6Axhe F6LEymNsdcNDnrof9VYv4cTOWFUIo2w= X-MC-Unique: QIDC8n1UMqGeUIhqoLgpFA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PATCH v2 1/3] block: Add blk_new_with_bs() helper Date: Thu, 23 Apr 2020 17:17:05 -0500 Message-Id: <20200423221707.477404-2-eblake@redhat.com> In-Reply-To: <20200423221707.477404-1-eblake@redhat.com> References: <20200423221707.477404-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=eblake@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/23 16:21:37 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Fam Zheng , "open list:Sheepdog" , qemu-block@nongnu.org, Jeff Cody , Stefan Weil , Markus Armbruster , mreitz@redhat.com, Stefan Hajnoczi , Liu Yuan , "Denis V. Lunev" , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" There are several callers that need to create a new block backend from an existing BDS; make the task slightly easier with a common helper routine. Suggested-by: Max Reitz Signed-off-by: Eric Blake Reviewed-by: Max Reitz Reviewed-by: Stefan Hajnoczi --- include/sysemu/block-backend.h | 2 ++ block/block-backend.c | 23 +++++++++++++++++++++++ block/crypto.c | 8 +++----- block/parallels.c | 7 +++---- block/qcow.c | 7 +++---- block/qcow2.c | 15 ++++++--------- block/qed.c | 7 +++---- block/sheepdog.c | 9 ++++----- block/vdi.c | 7 +++---- block/vhdx.c | 7 +++---- block/vmdk.c | 9 ++++----- block/vpc.c | 7 +++---- blockdev.c | 8 +++----- blockjob.c | 7 ++----- 14 files changed, 65 insertions(+), 58 deletions(-) diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h index 9bbdbd63d743..d37c1244dd9c 100644 --- a/include/sysemu/block-backend.h +++ b/include/sysemu/block-backend.h @@ -77,6 +77,8 @@ typedef struct BlockBackendPublic { } BlockBackendPublic; BlockBackend *blk_new(AioContext *ctx, uint64_t perm, uint64_t shared_perm= ); +BlockBackend *blk_new_with_bs(BlockDriverState *bs, uint64_t perm, + uint64_t shared_perm, Error **errp); BlockBackend *blk_new_open(const char *filename, const char *reference, QDict *options, int flags, Error **errp); int blk_get_refcnt(BlockBackend *blk); diff --git a/block/block-backend.c b/block/block-backend.c index 38ae41382652..1e082ce2a0f4 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -355,6 +355,29 @@ BlockBackend *blk_new(AioContext *ctx, uint64_t perm, = uint64_t shared_perm) return blk; } +/* + * Create a new BlockBackend connected to an existing BlockDriverState. + * + * @perm is a bitmasks of BLK_PERM_* constants which describes the permiss= ions + * to request for a block driver node that is attached to this BlockBacken= d. + * @shared_perm is a bitmask which describes which permissions may be gran= ted + * to other users of the attached node. + * Both sets of permissions can be changed later using blk_set_perm(). + * + * Return the new BlockBackend on success, null on failure. + */ +BlockBackend *blk_new_with_bs(BlockDriverState *bs, uint64_t perm, + uint64_t shared_perm, Error **errp) +{ + BlockBackend *blk =3D blk_new(bdrv_get_aio_context(bs), perm, shared_p= erm); + + if (blk_insert_bs(blk, bs, errp) < 0) { + blk_unref(blk); + return NULL; + } + return blk; +} + /* * Creates a new BlockBackend, opens a new BlockDriverState, and connects = both. * The new BlockBackend is in the main AioContext. diff --git a/block/crypto.c b/block/crypto.c index d577f89659fa..1cb8ae17ffde 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -261,11 +261,9 @@ static int block_crypto_co_create_generic(BlockDriverS= tate *bs, QCryptoBlock *crypto =3D NULL; struct BlockCryptoCreateData data; - blk =3D blk_new(bdrv_get_aio_context(bs), - BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); - - ret =3D blk_insert_bs(blk, bs, errp); - if (ret < 0) { + blk =3D blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM= _ALL, + errp); + if (!blk) { goto cleanup; } diff --git a/block/parallels.c b/block/parallels.c index 6d4ed77f165f..4019557ceee3 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -559,10 +559,9 @@ static int coroutine_fn parallels_co_create(BlockdevCr= eateOptions* opts, return -EIO; } - blk =3D blk_new(bdrv_get_aio_context(bs), - BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); - ret =3D blk_insert_bs(blk, bs, errp); - if (ret < 0) { + blk =3D blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM= _ALL, + errp); + if (!blk) { goto out; } blk_set_allow_write_beyond_eof(blk, true); diff --git a/block/qcow.c b/block/qcow.c index 8973e4e565a1..d9f26c515a49 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -849,10 +849,9 @@ static int coroutine_fn qcow_co_create(BlockdevCreateO= ptions *opts, return -EIO; } - qcow_blk =3D blk_new(bdrv_get_aio_context(bs), - BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); - ret =3D blk_insert_bs(qcow_blk, bs, errp); - if (ret < 0) { + qcow_blk =3D blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, + BLK_PERM_ALL, errp); + if (!qcow_blk) { goto exit; } blk_set_allow_write_beyond_eof(qcow_blk, true); diff --git a/block/qcow2.c b/block/qcow2.c index b524b0c53f84..1ce72041978b 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3404,10 +3404,9 @@ qcow2_co_create(BlockdevCreateOptions *create_option= s, Error **errp) } /* Create BlockBackend to write to the image */ - blk =3D blk_new(bdrv_get_aio_context(bs), - BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); - ret =3D blk_insert_bs(blk, bs, errp); - if (ret < 0) { + blk =3D blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM= _ALL, + errp); + if (!blk) { goto out; } blk_set_allow_write_beyond_eof(blk, true); @@ -5359,11 +5358,9 @@ static int qcow2_amend_options(BlockDriverState *bs,= QemuOpts *opts, } if (new_size) { - BlockBackend *blk =3D blk_new(bdrv_get_aio_context(bs), - BLK_PERM_RESIZE, BLK_PERM_ALL); - ret =3D blk_insert_bs(blk, bs, errp); - if (ret < 0) { - blk_unref(blk); + BlockBackend *blk =3D blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PER= M_ALL, + errp); + if (!blk) { return ret; } diff --git a/block/qed.c b/block/qed.c index 1af9b3cb1db1..dd767a5c75c9 100644 --- a/block/qed.c +++ b/block/qed.c @@ -651,10 +651,9 @@ static int coroutine_fn bdrv_qed_co_create(BlockdevCre= ateOptions *opts, return -EIO; } - blk =3D blk_new(bdrv_get_aio_context(bs), - BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); - ret =3D blk_insert_bs(blk, bs, errp); - if (ret < 0) { + blk =3D blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM= _ALL, + errp); + if (!blk) { goto out; } blk_set_allow_write_beyond_eof(blk, true); diff --git a/block/sheepdog.c b/block/sheepdog.c index 59f7ebb1710f..09d75ff78dd8 100644 --- a/block/sheepdog.c +++ b/block/sheepdog.c @@ -1803,12 +1803,11 @@ static int sd_prealloc(BlockDriverState *bs, int64_= t old_size, int64_t new_size, void *buf =3D NULL; int ret; - blk =3D blk_new(bdrv_get_aio_context(bs), - BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_PERM_RES= IZE, - BLK_PERM_ALL); + blk =3D blk_new_with_bs(bs, + BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_= PERM_RESIZE, + BLK_PERM_ALL, errp); - ret =3D blk_insert_bs(blk, bs, errp); - if (ret < 0) { + if (!blk) { goto out_with_err_set; } diff --git a/block/vdi.c b/block/vdi.c index e1a11f2aa097..d1b692741620 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -804,10 +804,9 @@ static int coroutine_fn vdi_co_do_create(BlockdevCreat= eOptions *create_options, goto exit; } - blk =3D blk_new(bdrv_get_aio_context(bs_file), - BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); - ret =3D blk_insert_bs(blk, bs_file, errp); - if (ret < 0) { + blk =3D blk_new_with_bs(bs_file, BLK_PERM_WRITE | BLK_PERM_RESIZE, + BLK_PERM_ALL, errp); + if (!blk) { goto exit; } diff --git a/block/vhdx.c b/block/vhdx.c index 33e57cd6567a..4c908f226f48 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -1984,10 +1984,9 @@ static int coroutine_fn vhdx_co_create(BlockdevCreat= eOptions *opts, return -EIO; } - blk =3D blk_new(bdrv_get_aio_context(bs), - BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); - ret =3D blk_insert_bs(blk, bs, errp); - if (ret < 0) { + blk =3D blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM= _ALL, + errp); + if (!blk) { goto delete_and_exit; } blk_set_allow_write_beyond_eof(blk, true); diff --git a/block/vmdk.c b/block/vmdk.c index 218d9c980059..dff9bd28cc3f 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -2717,11 +2717,10 @@ static BlockBackend *vmdk_co_create_cb(int64_t size= , int idx, if (!bs) { return NULL; } - blk =3D blk_new(bdrv_get_aio_context(bs), - BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_PERM_RES= IZE, - BLK_PERM_ALL); - if (blk_insert_bs(blk, bs, errp)) { - bdrv_unref(bs); + blk =3D blk_new_with_bs(bs, + BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_= PERM_RESIZE, + BLK_PERM_ALL, errp); + if (!blk) { return NULL; } blk_set_allow_write_beyond_eof(blk, true); diff --git a/block/vpc.c b/block/vpc.c index d8141b52da8b..250bb0bdad78 100644 --- a/block/vpc.c +++ b/block/vpc.c @@ -1012,10 +1012,9 @@ static int coroutine_fn vpc_co_create(BlockdevCreate= Options *opts, return -EIO; } - blk =3D blk_new(bdrv_get_aio_context(bs), - BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); - ret =3D blk_insert_bs(blk, bs, errp); - if (ret < 0) { + blk =3D blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM= _ALL, + errp); + if (!blk) { goto out; } blk_set_allow_write_beyond_eof(blk, true); diff --git a/blockdev.c b/blockdev.c index 5faddaa7052f..f43426ed5fbc 100644 --- a/blockdev.c +++ b/blockdev.c @@ -2711,7 +2711,6 @@ void qmp_block_resize(bool has_device, const char *de= vice, BlockBackend *blk =3D NULL; BlockDriverState *bs; AioContext *aio_context; - int ret; bs =3D bdrv_lookup_bs(has_device ? device : NULL, has_node_name ? node_name : NULL, @@ -2734,14 +2733,13 @@ void qmp_block_resize(bool has_device, const char *= device, goto out; } - blk =3D blk_new(bdrv_get_aio_context(bs), BLK_PERM_RESIZE, BLK_PERM_AL= L); - ret =3D blk_insert_bs(blk, bs, errp); - if (ret < 0) { + blk =3D blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL, errp); + if (!blk) { goto out; } bdrv_drained_begin(bs); - ret =3D blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp); + blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp); bdrv_drained_end(bs); out: diff --git a/blockjob.c b/blockjob.c index fc850312c124..2affa1844d4f 100644 --- a/blockjob.c +++ b/blockjob.c @@ -397,16 +397,13 @@ void *block_job_create(const char *job_id, const Bloc= kJobDriver *driver, { BlockBackend *blk; BlockJob *job; - int ret; if (job_id =3D=3D NULL && !(flags & JOB_INTERNAL)) { job_id =3D bdrv_get_device_name(bs); } - blk =3D blk_new(bdrv_get_aio_context(bs), perm, shared_perm); - ret =3D blk_insert_bs(blk, bs, errp); - if (ret < 0) { - blk_unref(blk); + blk =3D blk_new_with_bs(bs, perm, shared_perm, errp); + if (!blk) { return NULL; } --=20 2.26.2 From nobody Thu May 16 13:33:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1587680449; cv=none; d=zohomail.com; s=zohoarc; b=LyA/VSftylYIbOxmPHX+PQ0iAt2tGKJxrW4bITXVHUhB36CsSZG9/TYcQso8ak3pmu7OWHH52PR/Hoemso8z1+c0y6iiSo1+zcZY22ke431EolhwAYb7a/R/yFmRStMdju4vbTMfp+oYTRpJCmvvLiLUc6d1A18dRuXMndso/YU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1587680449; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=75SFsu2SxUdH6Zjc5UfQGuy/xj22RvwwJ2wIvFmAVoA=; b=EQGP6JH0BvLKccvKNRom1fFd9k5QawCl8YcrCB9hs9Y32tDNxmBzcp3xogfCMwRcd+ZkVV7eOsn8rEyIhbFyWgtRbpq0Mq+jVovOiqdWSw1CM9A/hOf/LPzHuRMuoD0aCYSailpW0P8cx7HHgf83vqajEFE+ktMHlr48RAko9sY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1587680449230590.9690569885328; Thu, 23 Apr 2020 15:20:49 -0700 (PDT) Received: from localhost ([::1]:43196 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jRkD9-0003rL-KW for importer@patchew.org; Thu, 23 Apr 2020 18:20:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49270) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jRkA1-0007ch-81 for qemu-devel@nongnu.org; Thu, 23 Apr 2020 18:17:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jRk9v-0006sy-Kj for qemu-devel@nongnu.org; Thu, 23 Apr 2020 18:17:31 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:25589 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jRk9u-0006nL-DJ for qemu-devel@nongnu.org; Thu, 23 Apr 2020 18:17:26 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-106-jZyTgkKqMLOb88EOubjYrQ-1; Thu, 23 Apr 2020 18:17:17 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A4C8A80B709; Thu, 23 Apr 2020 22:17:16 +0000 (UTC) Received: from blue.redhat.com (ovpn-116-80.rdu2.redhat.com [10.10.116.80]) by smtp.corp.redhat.com (Postfix) with ESMTP id EEBEC1C94D; Thu, 23 Apr 2020 22:17:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1587680241; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=75SFsu2SxUdH6Zjc5UfQGuy/xj22RvwwJ2wIvFmAVoA=; b=dpn9eCKOioaqPBq2sLKLgjlm3KuoGN9amyQthwGwF0FPaVOax7gmKlIieAswmoIgd/P0Pt We+MxD/uQyYP489Cc4TgKlRDtQhtvRU9gmh9rYvzx45/wZmgenFygQkvgmtHpYjE1G8OMe 8b5uQts5ebCD7CUN9I7WpVlOLp7NoqA= X-MC-Unique: jZyTgkKqMLOb88EOubjYrQ-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PATCH v2 2/3] qcow2: Allow resize of images with internal snapshots Date: Thu, 23 Apr 2020 17:17:06 -0500 Message-Id: <20200423221707.477404-3-eblake@redhat.com> In-Reply-To: <20200423221707.477404-1-eblake@redhat.com> References: <20200423221707.477404-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.61; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/23 16:25:00 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We originally refused to allow resize of images with internal snapshots because the v2 image format did not require the tracking of snapshot size, making it impossible to safely revert to a snapshot with a different size than the current view of the image. But the snapshot size tracking was rectified in v3, and our recent fixes to qemu-img amend (see 0a85af35) guarantee that we always have a valid snapshot size. Thus, we no longer need to artificially limit image resizes, but it does become one more thing that would prevent a downgrade back to v2. And now that we support different-sized snapshots, it's also easy to fix reverting to a snapshot to apply the new size. Upgrade iotest 61 to cover this (we previously had NO coverage of refusal to resize while snapshots exist). Note that the amend process can fail but still have effects: in particular, since we break things into upgrade, resize, downgrade, a failure during resize does not roll back changes made during upgrade, nor does failure in downgrade roll back a resize. But this situation is pre-existing even without this patch; and without journaling, the best we could do is minimize the chance of partial failure by collecting all changes prior to doing any writes - which adds a lot of complexity but could still fail with EIO. On the other hand, we are careful that even if we have partial modification but then fail, the image is left viable (that is, we are careful to sequence things so that after each successful cluster write, there may be transient leaked clusters but no corrupt metadata). And complicating the code to make it more transaction-like is not worth the effort: a user can always request multiple 'qemu-img amend' changing one thing each, if they need finer-grained control over detecting the first failure than what they get by letting qemu decide how to sequence multiple changes. Signed-off-by: Eric Blake Reviewed-by: Max Reitz --- block/qcow2-snapshot.c | 20 ++++++++++++++++---- block/qcow2.c | 25 ++++++++++++++++++++++--- tests/qemu-iotests/061 | 35 +++++++++++++++++++++++++++++++++++ tests/qemu-iotests/061.out | 28 ++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 7 deletions(-) diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 82c32d4c9b08..df16424fd952 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -23,6 +23,7 @@ */ #include "qemu/osdep.h" +#include "sysemu/block-backend.h" #include "qapi/error.h" #include "qcow2.h" #include "qemu/bswap.h" @@ -775,10 +776,21 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const c= har *snapshot_id) } if (sn->disk_size !=3D bs->total_sectors * BDRV_SECTOR_SIZE) { - error_report("qcow2: Loading snapshots with different disk " - "size is not implemented"); - ret =3D -ENOTSUP; - goto fail; + BlockBackend *blk =3D blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PER= M_ALL, + &local_err); + if (!blk) { + error_report_err(local_err); + ret =3D -ENOTSUP; + goto fail; + } + + ret =3D blk_truncate(blk, sn->disk_size, true, PREALLOC_MODE_OFF, + &local_err); + blk_unref(blk); + if (ret < 0) { + error_report_err(local_err); + goto fail; + } } /* diff --git a/block/qcow2.c b/block/qcow2.c index 1ce72041978b..34888a793354 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3987,9 +3987,12 @@ static int coroutine_fn qcow2_co_truncate(BlockDrive= rState *bs, int64_t offset, qemu_co_mutex_lock(&s->lock); - /* cannot proceed if image has snapshots */ - if (s->nb_snapshots) { - error_setg(errp, "Can't resize an image which has snapshots"); + /* + * Even though we store snapshot size for all images, it was not + * required until v3, so it is not safe to proceed for v2. + */ + if (s->nb_snapshots && s->qcow_version < 3) { + error_setg(errp, "Can't resize a v2 image which has snapshots"); ret =3D -ENOTSUP; goto fail; } @@ -4951,6 +4954,7 @@ static int qcow2_downgrade(BlockDriverState *bs, int = target_version, BDRVQcow2State *s =3D bs->opaque; int current_version =3D s->qcow_version; int ret; + int i; /* This is qcow2_downgrade(), not qcow2_upgrade() */ assert(target_version < current_version); @@ -4968,6 +4972,21 @@ static int qcow2_downgrade(BlockDriverState *bs, int= target_version, return -ENOTSUP; } + /* + * If any internal snapshot has a different size than the current + * image size, or VM state size that exceeds 32 bits, downgrading + * is unsafe. Even though we would still use v3-compliant output + * to preserve that data, other v2 programs might not realize + * those optional fields are important. + */ + for (i =3D 0; i < s->nb_snapshots; i++) { + if (s->snapshots[i].vm_state_size > UINT32_MAX || + s->snapshots[i].disk_size !=3D bs->total_sectors * BDRV_SECTOR= _SIZE) { + error_setg(errp, "Internal snapshots prevent downgrade of imag= e"); + return -ENOTSUP; + } + } + /* clear incompatible features */ if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { ret =3D qcow2_mark_clean(bs); diff --git a/tests/qemu-iotests/061 b/tests/qemu-iotests/061 index ce285d308408..10eb24316461 100755 --- a/tests/qemu-iotests/061 +++ b/tests/qemu-iotests/061 @@ -111,6 +111,41 @@ $PYTHON qcow2.py "$TEST_IMG" dump-header $QEMU_IO -c "read -P 0x2a 42M 64k" "$TEST_IMG" | _filter_qemu_io _check_test_img +echo +echo "=3D=3D=3D Testing resize with snapshots =3D=3D=3D" +echo +_make_test_img -o "compat=3D0.10" 32M +$QEMU_IO -c "write -P 0x2a 24M 64k" "$TEST_IMG" | _filter_qemu_io +$QEMU_IMG snapshot -c foo "$TEST_IMG" +$QEMU_IMG resize "$TEST_IMG" 64M && + echo "unexpected pass" +$PYTHON qcow2.py "$TEST_IMG" dump-header | grep '^\(version\|size\|nb_snap= \)' + +$QEMU_IMG amend -o "compat=3D1.1,size=3D128M" "$TEST_IMG" || + echo "unexpected fail" +$PYTHON qcow2.py "$TEST_IMG" dump-header | grep '^\(version\|size\|nb_snap= \)' + +$QEMU_IMG snapshot -c bar "$TEST_IMG" +$QEMU_IMG resize --shrink "$TEST_IMG" 64M || + echo "unexpected fail" +$PYTHON qcow2.py "$TEST_IMG" dump-header | grep '^\(version\|size\|nb_snap= \)' + +$QEMU_IMG amend -o "compat=3D0.10,size=3D32M" "$TEST_IMG" && + echo "unexpected pass" +$PYTHON qcow2.py "$TEST_IMG" dump-header | grep '^\(version\|size\|nb_snap= \)' + +$QEMU_IMG snapshot -a bar "$TEST_IMG" || + echo "unexpected fail" +$PYTHON qcow2.py "$TEST_IMG" dump-header | grep '^\(version\|size\|nb_snap= \)' + +$QEMU_IMG snapshot -d bar "$TEST_IMG" +$QEMU_IMG amend -o "compat=3D0.10,size=3D32M" "$TEST_IMG" || + echo "unexpected fail" +$PYTHON qcow2.py "$TEST_IMG" dump-header | grep '^\(version\|size\|nb_snap= \)' + +_check_test_img + + echo echo "=3D=3D=3D Testing dirty lazy_refcounts=3Doff =3D=3D=3D" echo diff --git a/tests/qemu-iotests/061.out b/tests/qemu-iotests/061.out index 413cc4e0f4ab..5a8d36d0058a 100644 --- a/tests/qemu-iotests/061.out +++ b/tests/qemu-iotests/061.out @@ -271,6 +271,34 @@ read 65536/65536 bytes at offset 44040192 64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) No errors were found on the image. +=3D=3D=3D Testing resize with snapshots =3D=3D=3D + +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D33554432 +wrote 65536/65536 bytes at offset 25165824 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +qemu-img: Can't resize a v2 image which has snapshots +version 2 +size 33554432 +nb_snapshots 1 +version 3 +size 134217728 +nb_snapshots 1 +Image resized. +version 3 +size 67108864 +nb_snapshots 2 +qemu-img: Internal snapshots prevent downgrade of image +version 3 +size 33554432 +nb_snapshots 2 +version 3 +size 134217728 +nb_snapshots 2 +version 2 +size 33554432 +nb_snapshots 1 +No errors were found on the image. + =3D=3D=3D Testing dirty lazy_refcounts=3Doff =3D=3D=3D Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D67108864 --=20 2.26.2 From nobody Thu May 16 13:33:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1587680334; cv=none; d=zohomail.com; s=zohoarc; b=ns+qpCkORYd+PXb1BsQHL3ONBX8eV9eoHwSrSYBDe6X8quQhJtmKguh1J/6ndm8vYTC8wF6UajdUXs+0V/CH7AmJYc5hogwMmMO7FN22BUJ1mkai1uQbpNx7m1pf2ehxk3wsqEm04KdXReNoX1G2P5tzRfzIrI6kIzisruMoAoY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1587680334; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cG4/wuUjGLtOs3lRBBHGqT0ewVHOwKkACcYzE9xIwJU=; b=R0hj1maEey1KRHqe04OLaSSVtq6vi1PXAiZlM2n9SEQVOoL14OxiFkhrmyBJs0AksnjI4Do0DHGkXucisWFOsKu7mAMg6J+RfsQPouMczpnYtCKpzkuLKxZPNvMUluWtJbaRftkspMpMcG+fMG+ev/gMTVnfXnu7wbGNPSHlI9w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1587680334643843.2273716158188; Thu, 23 Apr 2020 15:18:54 -0700 (PDT) Received: from localhost ([::1]:43060 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jRkBJ-0000rI-65 for importer@patchew.org; Thu, 23 Apr 2020 18:18:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49088) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jRk9w-0007Yt-Kg for qemu-devel@nongnu.org; Thu, 23 Apr 2020 18:17:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jRk9u-0006pz-Fh for qemu-devel@nongnu.org; Thu, 23 Apr 2020 18:17:28 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:53287 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jRk9q-0006iO-4i for qemu-devel@nongnu.org; Thu, 23 Apr 2020 18:17:22 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-255-0kwcC8SsMmOcgj_nhLWDDA-1; Thu, 23 Apr 2020 18:17:18 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9D8E480B70A; Thu, 23 Apr 2020 22:17:17 +0000 (UTC) Received: from blue.redhat.com (ovpn-116-80.rdu2.redhat.com [10.10.116.80]) by smtp.corp.redhat.com (Postfix) with ESMTP id E791A1C94D; Thu, 23 Apr 2020 22:17:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1587680240; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cG4/wuUjGLtOs3lRBBHGqT0ewVHOwKkACcYzE9xIwJU=; b=J2KZE0BimAThLEDc987wSY2SdYQcrOXNNetESQ9JATHiEKPLuObAYJe0RDC/1p5M7EMS7y vc5NOwIp4tobRwf/DaFLl3GinPO6uSEbBr5Et6eK3+3I43lbACIdarcXsNB96f6qdLncMl yUtxCMUBKuaTMfhJNXIbxHJ+qmksaPM= X-MC-Unique: 0kwcC8SsMmOcgj_nhLWDDA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PATCH v2 3/3] qcow2: Tweak comment about bitmaps vs. resize Date: Thu, 23 Apr 2020 17:17:07 -0500 Message-Id: <20200423221707.477404-4-eblake@redhat.com> In-Reply-To: <20200423221707.477404-1-eblake@redhat.com> References: <20200423221707.477404-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=eblake@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/23 16:21:37 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Our comment did not actually match the code. Rewrite the comment to be less sensitive to any future changes to qcow2-bitmap.c that might implement scenarios that we currently reject. Signed-off-by: Eric Blake Reviewed-by: Max Reitz --- block/qcow2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/qcow2.c b/block/qcow2.c index 34888a793354..6b6d1c3fa8b9 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3997,7 +3997,7 @@ static int coroutine_fn qcow2_co_truncate(BlockDriver= State *bs, int64_t offset, goto fail; } - /* cannot proceed if image has bitmaps */ + /* See qcow2-bitmap.c for which bitmap scenarios prevent a resize. */ if (qcow2_truncate_bitmaps_check(bs, errp)) { ret =3D -ENOTSUP; goto fail; --=20 2.26.2