From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552410155385741.1108097781678; Tue, 12 Mar 2019 10:02:35 -0700 (PDT) Received: from localhost ([127.0.0.1]:55950 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3knP-0000sg-JK for importer@patchew.org; Tue, 12 Mar 2019 13:02:31 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43451) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kaX-0006d3-4Z for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kaV-0006gl-Ad for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:13 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33712) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kaU-0006dF-LF; Tue, 12 Mar 2019 12:49:11 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006ol-3N; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaE-0007vb-Rq; Tue, 12 Mar 2019 18:48:54 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=Vq6174PlIaAjGqXUyrs/FdmxDwlP11DlUdylBVBWRNk=; b=jk11Ag14y/VTGyiZpRjj0IgEiAlAeRriK0LAHv3wWH0p8qKQEb0dTdeZaF4tJz5HkVXr1nA8RoNAV+PsRHNnNXXxI3Luwu58dNWWTqZbkWS56t5jXXYANnK3obtMZBVpUEKntPazAQESbsin3R2hD+qCtuulHJFEPLrt2STIw6sspqyhSX27gWnAoxF/feSbvFAtD4/87vVwDxt5wFbFVPpzqwU8Mr5/oPlHsnAvFm5GTiqd0t4m6CP9koHjmp9vGhAXpDIIJll/W48z9la506Kv6YplwtfY7+ufLu3mbZEUGXftUfqtFP/Y7YhXkUtdGKvzVtJBa9p/pR+WQ7S0OA==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:40 +0200 Message-Id: <407de9e2b08e0f47b10c48039458fd4b6c143154.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 01/13] block: Allow freezing BdrvChild links 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Our permission system is useful to define what operations are allowed on a certain block node and includes things like BLK_PERM_WRITE or BLK_PERM_RESIZE among others. One of the permissions is BLK_PERM_GRAPH_MOD which allows "changing the node that this BdrvChild points to". The exact meaning of this has never been very clear, but it can be understood as "change any of the links connected to the node". This can be used to prevent changing a backing link, but it's too coarse. This patch adds a new 'frozen' attribute to BdrvChild, which forbids detaching the link from the node it points to, and new API to freeze and unfreeze a backing chain. After this change a few functions can fail, so they need additional checks. Signed-off-by: Alberto Garcia --- block.c | 76 +++++++++++++++++++++++++++++++++++++++++++= ++++ include/block/block.h | 5 ++++ include/block/block_int.h | 6 ++++ 3 files changed, 87 insertions(+) diff --git a/block.c b/block.c index 33804cdcaa..a2bffce38d 100644 --- a/block.c +++ b/block.c @@ -2126,6 +2126,8 @@ static void bdrv_replace_child_noperm(BdrvChild *chil= d, BlockDriverState *old_bs =3D child->bs; int i; =20 + assert(!child->frozen); + if (old_bs && new_bs) { assert(bdrv_get_aio_context(old_bs) =3D=3D bdrv_get_aio_context(ne= w_bs)); } @@ -2342,6 +2344,10 @@ void bdrv_set_backing_hd(BlockDriverState *bs, Block= DriverState *backing_hd, bool update_inherits_from =3D bdrv_chain_contains(bs, backing_hd) && bdrv_inherits_from_recursive(backing_hd, bs); =20 + if (bdrv_is_backing_chain_frozen(bs, backing_bs(bs), errp)) { + return; + } + if (backing_hd) { bdrv_ref(backing_hd); } @@ -3721,6 +3727,11 @@ void bdrv_replace_node(BlockDriverState *from, Block= DriverState *to, if (!should_update_child(c, to)) { continue; } + if (c->frozen) { + error_setg(errp, "Cannot change '%s' link to '%s'", + c->name, from->node_name); + goto out; + } list =3D g_slist_prepend(list, c); perm |=3D c->perm; shared &=3D c->shared_perm; @@ -3933,6 +3944,63 @@ BlockDriverState *bdrv_find_base(BlockDriverState *b= s) } =20 /* + * Return true if at least one of the backing links between @bs and + * @base is frozen. @errp is set if that's the case. + */ +bool bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *= base, + Error **errp) +{ + BlockDriverState *i; + + for (i =3D bs; i !=3D base && i->backing; i =3D backing_bs(i)) { + if (i->backing->frozen) { + error_setg(errp, "Cannot change '%s' link from '%s' to '%s'", + i->backing->name, i->node_name, + backing_bs(i)->node_name); + return true; + } + } + + return false; +} + +/* + * Freeze all backing links between @bs and @base. + * If any of the links is already frozen the operation is aborted and + * none of the links are modified. + * Returns 0 on success. On failure returns < 0 and sets @errp. + */ +int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, + Error **errp) +{ + BlockDriverState *i; + + if (bdrv_is_backing_chain_frozen(bs, base, errp)) { + return -EPERM; + } + + for (i =3D bs; i !=3D base && i->backing; i =3D backing_bs(i)) { + i->backing->frozen =3D true; + } + + return 0; +} + +/* + * Unfreeze all backing links between @bs and @base. The caller must + * ensure that all links are frozen before using this function. + */ +void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *b= ase) +{ + BlockDriverState *i; + + for (i =3D bs; i !=3D base && i->backing; i =3D backing_bs(i)) { + assert(i->backing->frozen); + i->backing->frozen =3D false; + } +} + +/* * Drops images above 'base' up to and including 'top', and sets the image * above 'top' to have base as its backing file. * @@ -3981,6 +4049,14 @@ int bdrv_drop_intermediate(BlockDriverState *top, Bl= ockDriverState *base, goto exit; } =20 + /* This function changes all links that point to top and makes + * them point to base. Check that none of them is frozen. */ + QLIST_FOREACH(c, &top->parents, next_parent) { + if (c->frozen) { + goto exit; + } + } + /* If 'base' recursively inherits from 'top' then we should set * base->inherits_from to top->inherits_from after 'top' and all * other intermediate nodes have been dropped. diff --git a/include/block/block.h b/include/block/block.h index 6a758a76f8..b0c04f16c8 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -353,6 +353,11 @@ int bdrv_drop_intermediate(BlockDriverState *top, Bloc= kDriverState *base, BlockDriverState *bdrv_find_overlay(BlockDriverState *active, BlockDriverState *bs); BlockDriverState *bdrv_find_base(BlockDriverState *bs); +bool bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *= base, + Error **errp); +int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, + Error **errp); +void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *b= ase); =20 =20 typedef struct BdrvCheckResult { diff --git a/include/block/block_int.h b/include/block/block_int.h index a23cabaddd..438feba4e5 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -711,6 +711,12 @@ struct BdrvChild { uint64_t backup_perm; uint64_t backup_shared_perm; =20 + /* + * This link is frozen: the child can neither be replaced nor + * detached from the parent. + */ + bool frozen; + QLIST_ENTRY(BdrvChild) next; QLIST_ENTRY(BdrvChild) next_parent; }; --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 155240980948551.22924086079104; Tue, 12 Mar 2019 09:56:49 -0700 (PDT) Received: from localhost ([127.0.0.1]:55864 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3khm-0004Ir-94 for importer@patchew.org; Tue, 12 Mar 2019 12:56:42 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43429) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kaW-0006cz-Lt for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kaV-0006gQ-99 for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:12 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33715) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kaU-0006dC-JI; Tue, 12 Mar 2019 12:49:11 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006ok-3A; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaE-0007vd-T9; Tue, 12 Mar 2019 18:48:54 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=q8z3oYHnFRa8iSsLkXPgtwzmVq/P1j3Nd/Y2HnzMDxg=; b=P+rbS1URH38v7gh9XRkmFbWdxB4g7POnRBheNSSDTFRIh1myORsGiJMQbBl44rQvaC6krn413pYxiVhB/pSmm6HThWdB7JQYa4Fa7SZQpE5A/gGqsx9phn1TfNEru2YB+reC8s5NhhZGl3jA6ibfvVYxd6hh5/lvbyi/ocAHRb9SFrW7JB/2qbvzL31c7jdFLZwjNSLrw24JHBdiuiHLYVUPJDxa4cVl+fyKJlfdZVcQbhrKnlKI9KIH/lOU1rD8M6PlfrLX33rf2t9E86IQLtIqmDnk3/XOd8eJvEuFMcAvWOfdjLVSFRWoEZ0CFkPoVg3jaas1DNsNE1JZB2I8sg==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:41 +0200 Message-Id: <69c41327e1b5c4a2c8c08e6584786b4c95fa0a84.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 02/13] block: Freeze the backing chain for the duration of the commit job 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Alberto Garcia --- block/commit.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/block/commit.c b/block/commit.c index 3b46ca7f97..ba60fef58a 100644 --- a/block/commit.c +++ b/block/commit.c @@ -39,6 +39,7 @@ typedef struct CommitBlockJob { BlockDriverState *base_bs; BlockdevOnError on_error; bool base_read_only; + bool chain_frozen; char *backing_file_str; } CommitBlockJob; =20 @@ -68,6 +69,9 @@ static int commit_prepare(Job *job) { CommitBlockJob *s =3D container_of(job, CommitBlockJob, common.job); =20 + bdrv_unfreeze_backing_chain(s->commit_top_bs, s->base_bs); + s->chain_frozen =3D false; + /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before * the normal backing chain can be restored. */ blk_unref(s->base); @@ -84,6 +88,10 @@ static void commit_abort(Job *job) CommitBlockJob *s =3D container_of(job, CommitBlockJob, common.job); BlockDriverState *top_bs =3D blk_bs(s->top); =20 + if (s->chain_frozen) { + bdrv_unfreeze_backing_chain(s->commit_top_bs, s->base_bs); + } + /* Make sure commit_top_bs and top stay around until bdrv_replace_node= () */ bdrv_ref(top_bs); bdrv_ref(s->commit_top_bs); @@ -330,6 +338,11 @@ void commit_start(const char *job_id, BlockDriverState= *bs, } } =20 + if (bdrv_freeze_backing_chain(commit_top_bs, base, errp) < 0) { + goto fail; + } + s->chain_frozen =3D true; + ret =3D block_job_add_bdrv(&s->common, "base", base, 0, BLK_PERM_ALL, = errp); if (ret < 0) { goto fail; @@ -362,6 +375,9 @@ void commit_start(const char *job_id, BlockDriverState = *bs, return; =20 fail: + if (s->chain_frozen) { + bdrv_unfreeze_backing_chain(commit_top_bs, base); + } if (s->base) { blk_unref(s->base); } --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552409755311585.4991072570919; Tue, 12 Mar 2019 09:55:55 -0700 (PDT) Received: from localhost ([127.0.0.1]:55858 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kgs-0003aa-T5 for importer@patchew.org; Tue, 12 Mar 2019 12:55:46 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43471) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kaY-0006dN-2z for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kaV-0006hi-QA for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:14 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33714) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kaV-0006dH-DG; Tue, 12 Mar 2019 12:49:11 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006oj-19; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaE-0007vf-UQ; Tue, 12 Mar 2019 18:48:54 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=Q5Y7cvRusZmYGsqO+rjtOfPqU6ww3DCHWSQhPtMUlSQ=; b=bZ5JPU6S+pCSecx7EghXW62ZE2PYQ/st9CAac4TEuuhoi+jF4K2e/v++PdpPcK/aL77v3BLFllGBH51rYlwwtIEwnAGgSiDAok1mcCHtwOYy6olRUyoPvlWG1gMPFMI1z8jKjP86tE1zimn+zAV5DoNsi2FdbL0yePnTC0RPoYjq6BgLx5FN/6Oef8ZmBZS6Mn4KtRqzTmM6bvS844bSw+ihbRHz0ZVz/hgEullmkD9E21s59k+IR2M58V1nNE8auX2VTnGrXG9sH2DYXr0s5VXEkVlbNmtigpEQht3pV9VkHuP6JSpXK5UwS9jDAsyjPIOT5RqxLWaXqw6ilzmrNw==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:42 +0200 Message-Id: <47307229e9c7f5499eadce21a02e59d4b20948b9.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 03/13] block: Freeze the backing chain for the duration of the mirror job 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Alberto Garcia --- block/mirror.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/block/mirror.c b/block/mirror.c index 726d3c27fb..010fdafd79 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -630,6 +630,10 @@ static int mirror_exit_common(Job *job) } s->prepared =3D true; =20 + if (bdrv_chain_contains(src, target_bs)) { + bdrv_unfreeze_backing_chain(mirror_top_bs, target_bs); + } + bdrv_release_dirty_bitmap(src, s->dirty_bitmap); =20 /* Make sure that the source BDS doesn't go away during bdrv_replace_n= ode, @@ -1639,6 +1643,10 @@ static void mirror_start_job(const char *job_id, Blo= ckDriverState *bs, goto fail; } } + + if (bdrv_freeze_backing_chain(mirror_top_bs, target, errp) < 0) { + goto fail; + } } =20 QTAILQ_INIT(&s->ops_in_flight); --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552409978234259.0293542041236; Tue, 12 Mar 2019 09:59:38 -0700 (PDT) Received: from localhost ([127.0.0.1]:55894 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kkW-0006jj-6L for importer@patchew.org; Tue, 12 Mar 2019 12:59:32 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43455) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kaX-0006d4-A0 for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kaV-0006hT-O4 for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:13 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33707) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kaV-0006dA-A7; Tue, 12 Mar 2019 12:49:11 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaR-0006oi-V2; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaE-0007vh-Ve; Tue, 12 Mar 2019 18:48:54 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=Qk4YRgiYEBq6ppTRJ1FbZOnFC6XTTw2F3iNcSSxD/3w=; b=hgJb+znqvWwn0+AzxPYuCg45H/GcESiqfjx3HOkcAPJ+S8VrltVnN3r3wSr85R7gupIMj+I1Ry5Mgwf9lJ5O5wWezj4YbUol8naqhkwMBNk9dOIYxNeGtM8zI9/leAIxOROjg4vbzIlTzs4yc2a8VFAi4jDPwvNsifU8yul0uaStU9kr0GyRgTxMGtNIu+z4G6re04o6ivJzb6m44SU4P41rS++4K/JluwM7wqiobrM4w7ZQ/XTwxr3nxtWfxLkJKNsWmWmRYN/ikNUHw/oTFKyPl3Rh+MydQD4wIRX1Oq0L9es3Us6rYaWQsvDsk7KbAkhbg+2KBfbSifYg/Zb3nw==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:43 +0200 Message-Id: <161ffc7829b553f3189ab32891849627be820f21.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 04/13] block: Freeze the backing chain for the duration of the stream job 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Alberto Garcia --- block/stream.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/block/stream.c b/block/stream.c index e14579ff80..6253c86fae 100644 --- a/block/stream.c +++ b/block/stream.c @@ -35,6 +35,7 @@ typedef struct StreamBlockJob { BlockdevOnError on_error; char *backing_file_str; bool bs_read_only; + bool chain_frozen; } StreamBlockJob; =20 static int coroutine_fn stream_populate(BlockBackend *blk, @@ -49,6 +50,16 @@ static int coroutine_fn stream_populate(BlockBackend *bl= k, return blk_co_preadv(blk, offset, qiov.size, &qiov, BDRV_REQ_COPY_ON_R= EAD); } =20 +static void stream_abort(Job *job) +{ + StreamBlockJob *s =3D container_of(job, StreamBlockJob, common.job); + + if (s->chain_frozen) { + BlockJob *bjob =3D &s->common; + bdrv_unfreeze_backing_chain(blk_bs(bjob->blk), s->base); + } +} + static int stream_prepare(Job *job) { StreamBlockJob *s =3D container_of(job, StreamBlockJob, common.job); @@ -58,6 +69,9 @@ static int stream_prepare(Job *job) Error *local_err =3D NULL; int ret =3D 0; =20 + bdrv_unfreeze_backing_chain(bs, base); + s->chain_frozen =3D false; + if (bs->backing) { const char *base_id =3D NULL, *base_fmt =3D NULL; if (base) { @@ -208,6 +222,7 @@ static const BlockJobDriver stream_job_driver =3D { .free =3D block_job_free, .run =3D stream_run, .prepare =3D stream_prepare, + .abort =3D stream_abort, .clean =3D stream_clean, .user_resume =3D block_job_user_resume, .drain =3D block_job_drain, @@ -254,9 +269,15 @@ void stream_start(const char *job_id, BlockDriverState= *bs, &error_abort); } =20 + if (bdrv_freeze_backing_chain(bs, base, errp) < 0) { + job_early_fail(&s->common.job); + goto fail; + } + s->base =3D base; s->backing_file_str =3D g_strdup(backing_file_str); s->bs_read_only =3D bs_read_only; + s->chain_frozen =3D true; =20 s->on_error =3D on_error; trace_stream_start(bs, base, s); --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552410094799450.344460000945; Tue, 12 Mar 2019 10:01:34 -0700 (PDT) Received: from localhost ([127.0.0.1]:55946 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kmO-0008UT-J0 for importer@patchew.org; Tue, 12 Mar 2019 13:01:28 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43460) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kaX-0006d6-C3 for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kaV-0006gf-AB for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:13 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33716) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kaU-0006dI-JK; Tue, 12 Mar 2019 12:49:11 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006og-0g; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaF-0007vj-0o; Tue, 12 Mar 2019 18:48:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=R16SWgabOaz8PI0QBBSXWmcw9TWXJxvdrGNHmgJIB4c=; b=LbMjWpuv+NQx/Yew4oOFuCfWneZB8BxgKDncRfRspAdjhbWTt52gGMV4SKKXBJZsFyqZANMMBYD/Ua4fidmb9jtTqU9XXh7cDHqRuLsTl724Ygz+Srxxu2WPW+vrEhOoGVxm5/mi6ss/URLq4GgCROJnDqzDnUtoRNm0R/FUE4tCkMp5U1nW7ZrCf9w/+ZrYGAbKbIwv42Imrp9bTyXdB6trn3nb3Iu6bNFUqF29oo7BFsrr4t283wQnqjmtWzXFWl9/ITGvWCroh2kY38oI8mYpu7RHjDne8gJve+cGujmx3d6rG1Qinh8iDZEV+dUcW0PeWDaV9igo5OfsQvHDXg==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:44 +0200 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 05/13] block: Add 'keep_old_opts' parameter to bdrv_reopen_queue() 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The bdrv_reopen_queue() function is used to create a queue with the BDSs that are going to be reopened and their new options. Once the queue is ready bdrv_reopen_multiple() is called to perform the operation. The original options from each one of the BDSs are kept, with the new options passed to bdrv_reopen_queue() applied on top of them. For "x-blockdev-reopen" we want a function that behaves much like "blockdev-add". We want to ignore the previous set of options so that only the ones actually specified by the user are applied, with the rest having their default values. One of the things that we need is a way to tell bdrv_reopen_queue() whether we want to keep the old set of options or not, and that's what this patch does. All current callers are setting this new parameter to true and x-blockdev-reopen will set it to false. Signed-off-by: Alberto Garcia --- block.c | 34 +++++++++++++++++++--------------- block/replication.c | 4 ++-- include/block/block.h | 3 ++- qemu-io-cmds.c | 2 +- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/block.c b/block.c index a2bffce38d..3cd281dbcc 100644 --- a/block.c +++ b/block.c @@ -3010,7 +3010,8 @@ static BlockReopenQueue *bdrv_reopen_queue_child(Bloc= kReopenQueue *bs_queue, QDict *options, const BdrvChildRole *role, QDict *parent_options, - int parent_flags) + int parent_flags, + bool keep_old_opts) { assert(bs !=3D NULL); =20 @@ -3050,13 +3051,13 @@ static BlockReopenQueue *bdrv_reopen_queue_child(Bl= ockReopenQueue *bs_queue, */ =20 /* Old explicitly set values (don't overwrite by inherited value) */ - if (bs_entry) { - old_options =3D qdict_clone_shallow(bs_entry->state.explicit_optio= ns); - } else { - old_options =3D qdict_clone_shallow(bs->explicit_options); + if (bs_entry || keep_old_opts) { + old_options =3D qdict_clone_shallow(bs_entry ? + bs_entry->state.explicit_options= : + bs->explicit_options); + bdrv_join_options(bs, options, old_options); + qobject_unref(old_options); } - bdrv_join_options(bs, options, old_options); - qobject_unref(old_options); =20 explicit_options =3D qdict_clone_shallow(options); =20 @@ -3068,10 +3069,12 @@ static BlockReopenQueue *bdrv_reopen_queue_child(Bl= ockReopenQueue *bs_queue, flags =3D bdrv_get_flags(bs); } =20 - /* Old values are used for options that aren't set yet */ - old_options =3D qdict_clone_shallow(bs->options); - bdrv_join_options(bs, options, old_options); - qobject_unref(old_options); + if (keep_old_opts) { + /* Old values are used for options that aren't set yet */ + old_options =3D qdict_clone_shallow(bs->options); + bdrv_join_options(bs, options, old_options); + qobject_unref(old_options); + } =20 /* We have the final set of options so let's update the flags */ options_copy =3D qdict_clone_shallow(options); @@ -3121,7 +3124,7 @@ static BlockReopenQueue *bdrv_reopen_queue_child(Bloc= kReopenQueue *bs_queue, g_free(child_key_dot); =20 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, - child->role, options, flags); + child->role, options, flags, keep_old_opts= ); } =20 return bs_queue; @@ -3129,9 +3132,10 @@ static BlockReopenQueue *bdrv_reopen_queue_child(Blo= ckReopenQueue *bs_queue, =20 BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, BlockDriverState *bs, - QDict *options) + QDict *options, bool keep_old_opts) { - return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, NULL, 0); + return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, NULL, 0, + keep_old_opts); } =20 /* @@ -3224,7 +3228,7 @@ int bdrv_reopen_set_read_only(BlockDriverState *bs, b= ool read_only, qdict_put_bool(opts, BDRV_OPT_READ_ONLY, read_only); =20 bdrv_subtree_drained_begin(bs); - queue =3D bdrv_reopen_queue(NULL, bs, opts); + queue =3D bdrv_reopen_queue(NULL, bs, opts, true); ret =3D bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, errp); bdrv_subtree_drained_end(bs); =20 diff --git a/block/replication.c b/block/replication.c index 4c80b54daf..a2f3590310 100644 --- a/block/replication.c +++ b/block/replication.c @@ -374,14 +374,14 @@ static void reopen_backing_file(BlockDriverState *bs,= bool writable, QDict *opts =3D qdict_new(); qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable); reopen_queue =3D bdrv_reopen_queue(reopen_queue, s->hidden_disk->b= s, - opts); + opts, true); } =20 if (s->orig_secondary_read_only) { QDict *opts =3D qdict_new(); qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable); reopen_queue =3D bdrv_reopen_queue(reopen_queue, s->secondary_disk= ->bs, - opts); + opts, true); } =20 if (reopen_queue) { diff --git a/include/block/block.h b/include/block/block.h index b0c04f16c8..4b5ffffa1a 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -299,7 +299,8 @@ BlockDriverState *bdrv_open(const char *filename, const= char *reference, BlockDriverState *bdrv_new_open_driver(BlockDriver *drv, const char *node_= name, int flags, Error **errp); BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, - BlockDriverState *bs, QDict *options); + BlockDriverState *bs, QDict *options, + bool keep_old_opts); int bdrv_reopen_multiple(AioContext *ctx, BlockReopenQueue *bs_queue, Erro= r **errp); int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only, Error **errp); diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index b9f189f09b..ff9a5cd80f 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -2080,7 +2080,7 @@ static int reopen_f(BlockBackend *blk, int argc, char= **argv) } =20 bdrv_subtree_drained_begin(bs); - brq =3D bdrv_reopen_queue(NULL, bs, opts); + brq =3D bdrv_reopen_queue(NULL, bs, opts, true); bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err); bdrv_subtree_drained_end(bs); =20 --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552410545641172.85651151982358; Tue, 12 Mar 2019 10:09:05 -0700 (PDT) Received: from localhost ([127.0.0.1]:56051 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kti-0005x6-H2 for importer@patchew.org; Tue, 12 Mar 2019 13:09:02 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43789) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kau-00073J-FR for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kas-00074r-Qv for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:36 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33749) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kas-0006jf-FN; Tue, 12 Mar 2019 12:49:34 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006om-2s; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaF-0007vl-2O; Tue, 12 Mar 2019 18:48:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=gQ9UIjXYF18PBsujJ+8qL41Wr2gGtg5AiyiCfGca7Gs=; b=EspMfLOdKVbZYCUzxF/h5hxFC6/I3sveIOUcpCR0uUrQH497NelE5LjlNP2/DWTVYQaIafyMN7wVPYiteIU2aBAsVvx9dUjjnDc8dk3JQWaa5hH7qMroYFz2REExId487Vps1yl4WZKGJA8mWIhXT4sG/coqcwaoidVzL+hLF33xcLpFYHICJZpW2sJ+qa5cVKJw00VfxUiUBoWhQ0ZMOS93vh76dUKFQ11HX9tVPZG2LAqgccd+mIvodL/+se3gWqGcww82jpZVVnquf1dqWbzKD3jkd0feD/uPCMt8C83trnpJvgpRzkSKID0BLgP1laeCGH7lxXhEflb4xJSdnQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:45 +0200 Message-Id: <888a3568a55a42df1f5ad43676766bd1aad66d45.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 06/13] block: Handle child references in bdrv_reopen_queue() 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Children in QMP are specified with BlockdevRef / BlockdevRefOrNull, which can contain a set of child options, a child reference, or NULL. In optional attributes like "backing" it can also be missing. Only the first case (set of child options) is being handled properly by bdrv_reopen_queue(). This patch deals with all the others. Here's how these cases should be handled when bdrv_reopen_queue() is deciding what to do with each child of a BlockDriverState: 1) Set of child options: if the child was implicitly created (i.e inherits_from points to the parent) then the options are removed from the parent's options QDict and are passed to the child with a recursive bdrv_reopen_queue() call. This case was already working fine. 2) Child reference: there's two possibilites here. 2a) Reference to the current child: if the child was implicitly created then it is put in the reopen queue, keeping its current set of options (since this was a child reference there was no way to specify a different set of options). If the child is not implicit then it keeps its current set of options but it is not reopened (and therefore does not inherit any new option from the parent). 2b) Reference to a different BDS: the current child is not put in the reopen queue at all. Passing a reference to a different BDS can be used to replace a child, although at the moment no driver implements this, so it results in an error. In any case, the current child is not going to be reopened (and might in fact disappear if it's replaced) 3) NULL: This is similar to (2b). Although no driver allows this yet it can be used to detach the current child so it should not be put in the reopen queue. 4) Missing option: at the moment "backing" is the only case where this can happen. With "blockdev-add", leaving "backing" out means that the default backing file is opened. We don't want to open a new image during reopen, so we require that "backing" is always present. We'll relax this requirement a bit in the next patch. If keep_old_opts is true and "backing" is missing then this behaves like 2a (the current child is reopened). Signed-off-by: Alberto Garcia --- block.c | 52 ++++++++++++++++++++++++++++++++++++++++++++---= ---- include/block/block.h | 1 + 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/block.c b/block.c index 3cd281dbcc..9698f2ad44 100644 --- a/block.c +++ b/block.c @@ -3107,9 +3107,21 @@ static BlockReopenQueue *bdrv_reopen_queue_child(Blo= ckReopenQueue *bs_queue, bs_entry->state.perm =3D UINT64_MAX; bs_entry->state.shared_perm =3D 0; =20 + /* + * If keep_old_opts is false then it means that unspecified + * options must be reset to their original value. We don't allow + * resetting 'backing' but we need to know if the option is + * missing in order to decide if we have to return an error. + */ + if (!keep_old_opts) { + bs_entry->state.backing_missing =3D + !qdict_haskey(options, "backing") && + !qdict_haskey(options, "backing.driver"); + } + QLIST_FOREACH(child, &bs->children, next) { - QDict *new_child_options; - char *child_key_dot; + QDict *new_child_options =3D NULL; + bool child_keep_old =3D keep_old_opts; =20 /* reopen can only change the options of block devices that were * implicitly created and inherited options. For other (referenced) @@ -3118,13 +3130,32 @@ static BlockReopenQueue *bdrv_reopen_queue_child(Bl= ockReopenQueue *bs_queue, continue; } =20 - child_key_dot =3D g_strdup_printf("%s.", child->name); - qdict_extract_subqdict(explicit_options, NULL, child_key_dot); - qdict_extract_subqdict(options, &new_child_options, child_key_dot); - g_free(child_key_dot); + /* Check if the options contain a child reference */ + if (qdict_haskey(options, child->name)) { + const char *childref =3D qdict_get_try_str(options, child->nam= e); + /* + * The current child must not be reopened if the child + * reference is null or points to a different node. + */ + if (g_strcmp0(childref, child->bs->node_name)) { + continue; + } + /* + * If the child reference points to the current child then + * reopen it with its existing set of options (note that + * it can still inherit new options from the parent). + */ + child_keep_old =3D true; + } else { + /* Extract child options ("child-name.*") */ + char *child_key_dot =3D g_strdup_printf("%s.", child->name); + qdict_extract_subqdict(explicit_options, NULL, child_key_dot); + qdict_extract_subqdict(options, &new_child_options, child_key_= dot); + g_free(child_key_dot); + } =20 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, - child->role, options, flags, keep_old_opts= ); + child->role, options, flags, child_keep_ol= d); } =20 return bs_queue; @@ -3403,6 +3434,13 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_stat= e, BlockReopenQueue *queue, =20 drv_prepared =3D true; =20 + if (drv->supports_backing && reopen_state->backing_missing) { + error_setg(errp, "backing is missing for '%s'", + reopen_state->bs->node_name); + ret =3D -EINVAL; + goto error; + } + /* Options that are not handled are only okay if they are unchanged * compared to the old state. It is expected that some options are only * used for the initial open, but not reopen (e.g. filename) */ diff --git a/include/block/block.h b/include/block/block.h index 4b5ffffa1a..fcc61a3a19 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -187,6 +187,7 @@ typedef struct BDRVReopenState { BlockDriverState *bs; int flags; BlockdevDetectZeroesOptions detect_zeroes; + bool backing_missing; uint64_t perm, shared_perm; QDict *options; QDict *explicit_options; --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552409922158682.5086892760196; Tue, 12 Mar 2019 09:58:42 -0700 (PDT) Received: from localhost ([127.0.0.1]:55887 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kje-0005yM-4e for importer@patchew.org; Tue, 12 Mar 2019 12:58:38 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43467) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kaX-0006d8-Oa for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kaV-0006hc-Pi for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:13 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33700) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kaV-0006d9-C1; Tue, 12 Mar 2019 12:49:11 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006on-2G; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaF-0007vt-40; Tue, 12 Mar 2019 18:48:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=i4ftbWRP7Rv13wRZkSn2vv69rI+REQqFsZzrrkVwJhk=; b=PUAWlpaBemLI6p7XR8Uv1rTtYyGpNQ/5qmht1Z3ekUcdsXoA6KNJO+8JMpEts0vDuoB40ZAeQiM81IXGzX7OV2gKQeniHubiZOoLMfSOHu4AGKGD6gQjtErArfmKE/r8AbcVJBxPMO4vIwicBNfEmtc7HkGDRud6r8PXsupEa4yBkeiWjmmLRqAP+1Ir/waQhJ11L2zNaGAEvc75A2V6ev4jyehBpU29XLtfR5lWYxGZshrvPuMRUj+s91LEvNRrSoZQ1BLsl3wW/9JMkFao6NetSaiS+Bomaps6ocXH0ZnM5nFHTPUJoeNFQnpP3NNwf0cVma9omlgMsa7AJrgQ9g==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:46 +0200 Message-Id: <83a92d1352323dce74410097c49b3e1178777892.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 07/13] block: Allow omitting the 'backing' option in certain cases 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Of all options of type BlockdevRef used to specify children in BlockdevOptions, 'backing' is the only one that is optional. For "x-blockdev-reopen" we want that if an option is omitted then it must be reset to its default value. The default value of 'backing' means that QEMU opens the backing file specified in the image metadata, but this is not something that we want to support for the reopen operation. Because of this the 'backing' option has to be specified during reopen, pointing to the existing backing file if we want to keep it, or pointing to a different one (or NULL) if we want to replace it (to be implemented in a subsequent patch). In order to simplify things a bit and not to require that the user passes the 'backing' option to every single block device even when it's clearly not necessary, this patch allows omitting this option if the block device being reopened doesn't have a backing file attached _and_ no default backing file is specified in the image metadata. Signed-off-by: Alberto Garcia --- block.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/block.c b/block.c index 9698f2ad44..c043f5eadd 100644 --- a/block.c +++ b/block.c @@ -3434,7 +3434,13 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_stat= e, BlockReopenQueue *queue, =20 drv_prepared =3D true; =20 - if (drv->supports_backing && reopen_state->backing_missing) { + /* + * We must provide the 'backing' option if the BDS has a backing + * file or if the image file has a backing file name as part of + * its metadata. Otherwise the 'backing' option can be omitted. + */ + if (drv->supports_backing && reopen_state->backing_missing && + (backing_bs(reopen_state->bs) || reopen_state->bs->backing_file[0]= )) { error_setg(errp, "backing is missing for '%s'", reopen_state->bs->node_name); ret =3D -EINVAL; --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 155241030381475.99014664918263; Tue, 12 Mar 2019 10:05:03 -0700 (PDT) Received: from localhost ([127.0.0.1]:55972 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kpp-0002nE-Lf for importer@patchew.org; Tue, 12 Mar 2019 13:05:01 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43499) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kaY-0006em-UP for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kaV-0006hO-O4 for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:14 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33713) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kaV-0006dJ-5s; Tue, 12 Mar 2019 12:49:11 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006oo-2f; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaF-0007vw-5c; Tue, 12 Mar 2019 18:48:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=8vsOrY49+F1FPTT1GtTLowlHTxSJoIZTWaZrXpb476k=; b=RvxGALFgtyWFbo81R3luYeLc/aHlumnOyNvAoGHTO5Nuf2/pdF/DmCtYPboRpjpsqsjEOU/n4l0n7fK5l01TwKi0tUWZJBfb/0AxjOcoh8Wrj6D/biU2ZPZY8iO1G7F3mleeNOmXJOfbx1h7D9H2cKWgXE1VShltDhwN0vWL1x/iqFyxqpv7whjaaucR/jFmy14azGsqgsVKV6/NuQobsFWGQSSVgaxEr/MyTaUmlC084v55QXQfny8DGcjqP6wv9YPZdJrdXgKSaMFfkFoc4/uX6KfOOwIRWwn0Bp5y/p5Iu/8xydgHvFlsfUiN22ahriDQsxG4OinI/U7WT5zBjg==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:47 +0200 Message-Id: <3980c9561cf42a11f7723fa60b0e9f8acf0fd971.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 08/13] block: Allow changing the backing file on reopen 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch allows the user to change the backing file of an image that is being reopened. Here's what it does: - In bdrv_reopen_prepare(): check that the value of 'backing' points to an existing node or is null. If it points to an existing node it also needs to make sure that replacing the backing file will not create a cycle in the node graph (i.e. you cannot reach the parent from the new backing file). - In bdrv_reopen_commit(): perform the actual node replacement by calling bdrv_set_backing_hd(). There may be temporary implicit nodes between a BDS and its backing file (e.g. a commit filter node). In these cases bdrv_reopen_prepare() looks for the real (non-implicit) backing file and requires that the 'backing' option points to it. Replacing or detaching a backing file is forbidden if there are implicit nodes in the middle. Although x-blockdev-reopen is meant to be used like blockdev-add, there's an important thing that must be taken into account: the only way to set a new backing file is by using a reference to an existing node (previously added with e.g. blockdev-add). If 'backing' contains a dictionary with a new set of options ({"driver": "qcow2", "file": { ... }}) then it is interpreted that the _existing_ backing file must be reopened with those options. Signed-off-by: Alberto Garcia --- block.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++= ++++ include/block/block.h | 2 + 2 files changed, 168 insertions(+) diff --git a/block.c b/block.c index c043f5eadd..c6786cd35b 100644 --- a/block.c +++ b/block.c @@ -2984,6 +2984,27 @@ BlockDriverState *bdrv_open(const char *filename, co= nst char *reference, } =20 /* + * Returns true if @child can be reached recursively from @bs + */ +static bool bdrv_recurse_has_child(BlockDriverState *bs, + BlockDriverState *child) +{ + BdrvChild *c; + + if (bs =3D=3D child) { + return true; + } + + QLIST_FOREACH(c, &bs->children, next) { + if (bdrv_recurse_has_child(c->bs, child)) { + return true; + } + } + + return false; +} + +/* * Adds a BlockDriverState to a simple queue for an atomic, transactional * reopen of multiple devices. * @@ -3208,6 +3229,19 @@ int bdrv_reopen_multiple(AioContext *ctx, BlockReope= nQueue *bs_queue, Error **er if (ret < 0) { goto cleanup_perm; } + /* Check if new_backing_bs would accept the new permissions */ + if (state->replace_backing_bs && state->new_backing_bs) { + uint64_t nperm, nshared; + bdrv_child_perm(state->bs, state->new_backing_bs, + NULL, &child_backing, bs_queue, + state->perm, state->shared_perm, + &nperm, &nshared); + ret =3D bdrv_check_update_perm(state->new_backing_bs, NULL, + nperm, nshared, NULL, errp); + if (ret < 0) { + goto cleanup_perm; + } + } bs_entry->perms_checked =3D true; } =20 @@ -3231,6 +3265,9 @@ cleanup_perm: bdrv_set_perm(state->bs, state->perm, state->shared_perm); } else { bdrv_abort_perm_update(state->bs); + if (state->replace_backing_bs && state->new_backing_bs) { + bdrv_abort_perm_update(state->new_backing_bs); + } } } cleanup: @@ -3242,6 +3279,9 @@ cleanup: qobject_unref(bs_entry->state.explicit_options); qobject_unref(bs_entry->state.options); } + if (bs_entry->state.new_backing_bs) { + bdrv_unref(bs_entry->state.new_backing_bs); + } g_free(bs_entry); } g_free(bs_queue); @@ -3314,6 +3354,101 @@ static void bdrv_reopen_perm(BlockReopenQueue *q, B= lockDriverState *bs, } =20 /* + * Take a BDRVReopenState and check if the value of 'backing' in the + * reopen_state->options QDict is valid or not. + * + * If 'backing' is missing from the QDict then return 0. + * + * If 'backing' contains the node name of the backing file of + * reopen_state->bs then return 0. + * + * If 'backing' contains a different node name (or is null) then check + * whether the current backing file can be replaced with the new one. + * If that's the case then reopen_state->replace_backing_bs is set to + * true and reopen_state->new_backing_bs contains a pointer to the new + * backing BlockDriverState (or NULL). + * + * Return 0 on success, otherwise return < 0 and set @errp. + */ +static int bdrv_reopen_parse_backing(BDRVReopenState *reopen_state, + Error **errp) +{ + BlockDriverState *bs =3D reopen_state->bs; + BlockDriverState *overlay_bs, *new_backing_bs; + QObject *value; + const char *str; + + value =3D qdict_get(reopen_state->options, "backing"); + if (value =3D=3D NULL) { + return 0; + } + + switch (qobject_type(value)) { + case QTYPE_QNULL: + new_backing_bs =3D NULL; + break; + case QTYPE_QSTRING: + str =3D qobject_get_try_str(value); + new_backing_bs =3D bdrv_lookup_bs(NULL, str, errp); + if (new_backing_bs =3D=3D NULL) { + return -EINVAL; + } else if (bdrv_recurse_has_child(new_backing_bs, bs)) { + error_setg(errp, "Making '%s' a backing file of '%s' " + "would create a cycle", str, bs->node_name); + return -EINVAL; + } + break; + default: + /* 'backing' does not allow any other data type */ + g_assert_not_reached(); + } + + /* + * TODO: before removing the x- prefix from x-blockdev-reopen we + * should move the new backing file into the right AioContext + * instead of returning an error. + */ + if (new_backing_bs) { + if (bdrv_get_aio_context(new_backing_bs) !=3D bdrv_get_aio_context= (bs)) { + error_setg(errp, "Cannot use a new backing file " + "with a different AioContext"); + return -EINVAL; + } + } + + /* + * Find the "actual" backing file by skipping all links that point + * to an implicit node, if any (e.g. a commit filter node). + */ + overlay_bs =3D bs; + while (backing_bs(overlay_bs) && backing_bs(overlay_bs)->implicit) { + overlay_bs =3D backing_bs(overlay_bs); + } + + /* If we want to replace the backing file we need some extra checks */ + if (new_backing_bs !=3D backing_bs(overlay_bs)) { + /* Check for implicit nodes between bs and its backing file */ + if (bs !=3D overlay_bs) { + error_setg(errp, "Cannot change backing link if '%s' has " + "an implicit backing file", bs->node_name); + return -EPERM; + } + /* Check if the backing link that we want to replace is frozen */ + if (bdrv_is_backing_chain_frozen(overlay_bs, backing_bs(overlay_bs= ), + errp)) { + return -EPERM; + } + reopen_state->replace_backing_bs =3D true; + if (new_backing_bs) { + bdrv_ref(new_backing_bs); + reopen_state->new_backing_bs =3D new_backing_bs; + } + } + + return 0; +} + +/* * Prepares a BlockDriverState for reopen. All changes are staged in the * 'opaque' field of the BDRVReopenState, which is used and allocated by * the block driver layer .bdrv_reopen_prepare() @@ -3447,6 +3582,17 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_stat= e, BlockReopenQueue *queue, goto error; } =20 + /* + * Allow changing the 'backing' option. The new value can be + * either a reference to an existing node (using its node name) + * or NULL to simply detach the current backing file. + */ + ret =3D bdrv_reopen_parse_backing(reopen_state, errp); + if (ret < 0) { + goto error; + } + qdict_del(reopen_state->options, "backing"); + /* Options that are not handled are only okay if they are unchanged * compared to the old state. It is expected that some options are only * used for the initial open, but not reopen (e.g. filename) */ @@ -3556,6 +3702,11 @@ void bdrv_reopen_commit(BDRVReopenState *reopen_stat= e) bs->read_only =3D !(reopen_state->flags & BDRV_O_RDWR); bs->detect_zeroes =3D reopen_state->detect_zeroes; =20 + if (reopen_state->replace_backing_bs) { + qdict_del(bs->explicit_options, "backing"); + qdict_del(bs->options, "backing"); + } + /* Remove child references from bs->options and bs->explicit_options. * Child options were already removed in bdrv_reopen_queue_child() */ QLIST_FOREACH(child, &bs->children, next) { @@ -3563,6 +3714,21 @@ void bdrv_reopen_commit(BDRVReopenState *reopen_stat= e) qdict_del(bs->options, child->name); } =20 + /* + * Change the backing file if a new one was specified. We do this + * after updating bs->options, so bdrv_refresh_filename() (called + * from bdrv_set_backing_hd()) has the new values. + */ + if (reopen_state->replace_backing_bs) { + BlockDriverState *old_backing_bs =3D backing_bs(bs); + assert(!old_backing_bs || !old_backing_bs->implicit); + /* Abort the permission update on the backing bs we're detaching */ + if (old_backing_bs) { + bdrv_abort_perm_update(old_backing_bs); + } + bdrv_set_backing_hd(bs, reopen_state->new_backing_bs, &error_abort= ); + } + bdrv_refresh_limits(bs, NULL); =20 new_can_write =3D diff --git a/include/block/block.h b/include/block/block.h index fcc61a3a19..68a3efbb43 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -188,6 +188,8 @@ typedef struct BDRVReopenState { int flags; BlockdevDetectZeroesOptions detect_zeroes; bool backing_missing; + bool replace_backing_bs; /* new_backing_bs is ignored if this is fals= e */ + BlockDriverState *new_backing_bs; /* If NULL then detach the current b= s */ uint64_t perm, shared_perm; QDict *options; QDict *explicit_options; --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552411074056947.2117829753408; Tue, 12 Mar 2019 10:17:54 -0700 (PDT) Received: from localhost ([127.0.0.1]:56232 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3l2D-0005Dv-Uh for importer@patchew.org; Tue, 12 Mar 2019 13:17:49 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43446) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kaW-0006d1-WE for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kaV-0006gs-DG for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:12 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33709) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kaU-0006dE-JI; Tue, 12 Mar 2019 12:49:11 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006op-20; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaF-0007vz-73; Tue, 12 Mar 2019 18:48:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=HHB+ea8TO//cTZ9IPFDAHF+mgiaLjGbqQStxFNCGL8I=; b=XQ4bmsM7Z8qjfmoj3hz0GlkLSI/x5R5suA7Bb58dKxPsh7dWZIptjMsUbqPPyYBpfRNRyi6OrdwTmBnMr5+hmkZrQWGZxWtwlbZql9egBXGGS8/GqqiQSeAf/5t8Fk+vJ0Yzqx1RNIXxXa2DK43jq+DKNDpP+JQUUIxbp9c3XutqrakzZNmripYWCzpYxh+9Z3Vme5TM73RgcGwBSbQ/KkY2r5NL0MzdxcjB47IEmQn2x3fWPrFgFEztVaYqbzXy/4PMDAvJUKnbu6iFcvbfSrIFw79lJKUrMIQBfwHdTv5xwXNI7ZOM5SoLW9uOWohL6W41CiUOfB5swWYDfKrLaA==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:48 +0200 Message-Id: <246d3515f1222374e662ea63e555ccf94febc255.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 09/13] block: Add a 'mutable_opts' field to BlockDriver 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If we reopen a BlockDriverState and there is an option that is present in bs->options but missing from the new set of options then we have to return an error unless the driver is able to reset it to its default value. This patch adds a new 'mutable_opts' field to BlockDriver. This is a list of runtime options that can be modified during reopen. If an option in this list is unspecified on reopen then it must be reset (or return an error). Signed-off-by: Alberto Garcia --- block/file-posix.c | 6 ++++++ block/qcow2.c | 25 +++++++++++++++++++++++++ block/raw-format.c | 3 +++ include/block/block_int.h | 8 ++++++++ 4 files changed, 42 insertions(+) diff --git a/block/file-posix.c b/block/file-posix.c index d41059d139..e9fa6aac48 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -442,6 +442,8 @@ static QemuOptsList raw_runtime_opts =3D { }, }; =20 +static const char *const mutable_opts[] =3D { "x-check-cache-dropped", NUL= L }; + static int raw_open_common(BlockDriverState *bs, QDict *options, int bdrv_flags, int open_flags, bool device, Error **errp) @@ -2870,6 +2872,7 @@ BlockDriver bdrv_file =3D { .bdrv_set_perm =3D raw_set_perm, .bdrv_abort_perm_update =3D raw_abort_perm_update, .create_opts =3D &raw_create_opts, + .mutable_opts =3D mutable_opts, }; =20 /***********************************************/ @@ -3321,6 +3324,7 @@ static BlockDriver bdrv_host_device =3D { .bdrv_reopen_abort =3D raw_reopen_abort, .bdrv_co_create_opts =3D hdev_co_create_opts, .create_opts =3D &raw_create_opts, + .mutable_opts =3D mutable_opts, .bdrv_co_invalidate_cache =3D raw_co_invalidate_cache, .bdrv_co_pwrite_zeroes =3D hdev_co_pwrite_zeroes, =20 @@ -3447,6 +3451,7 @@ static BlockDriver bdrv_host_cdrom =3D { .bdrv_reopen_abort =3D raw_reopen_abort, .bdrv_co_create_opts =3D hdev_co_create_opts, .create_opts =3D &raw_create_opts, + .mutable_opts =3D mutable_opts, .bdrv_co_invalidate_cache =3D raw_co_invalidate_cache, =20 =20 @@ -3580,6 +3585,7 @@ static BlockDriver bdrv_host_cdrom =3D { .bdrv_reopen_abort =3D raw_reopen_abort, .bdrv_co_create_opts =3D hdev_co_create_opts, .create_opts =3D &raw_create_opts, + .mutable_opts =3D mutable_opts, =20 .bdrv_co_preadv =3D raw_co_preadv, .bdrv_co_pwritev =3D raw_co_pwritev, diff --git a/block/qcow2.c b/block/qcow2.c index c4dd876fb4..0fc9b0561e 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -627,6 +627,30 @@ int qcow2_validate_table(BlockDriverState *bs, uint64_= t offset, return 0; } =20 +static const char *const mutable_opts[] =3D { + QCOW2_OPT_LAZY_REFCOUNTS, + QCOW2_OPT_DISCARD_REQUEST, + QCOW2_OPT_DISCARD_SNAPSHOT, + QCOW2_OPT_DISCARD_OTHER, + QCOW2_OPT_OVERLAP, + QCOW2_OPT_OVERLAP_TEMPLATE, + QCOW2_OPT_OVERLAP_MAIN_HEADER, + QCOW2_OPT_OVERLAP_ACTIVE_L1, + QCOW2_OPT_OVERLAP_ACTIVE_L2, + QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, + QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, + QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, + QCOW2_OPT_OVERLAP_INACTIVE_L1, + QCOW2_OPT_OVERLAP_INACTIVE_L2, + QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY, + QCOW2_OPT_CACHE_SIZE, + QCOW2_OPT_L2_CACHE_SIZE, + QCOW2_OPT_L2_CACHE_ENTRY_SIZE, + QCOW2_OPT_REFCOUNT_CACHE_SIZE, + QCOW2_OPT_CACHE_CLEAN_INTERVAL, + NULL +}; + static QemuOptsList qcow2_runtime_opts =3D { .name =3D "qcow2", .head =3D QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head), @@ -5275,6 +5299,7 @@ BlockDriver bdrv_qcow2 =3D { =20 .create_opts =3D &qcow2_create_opts, .strong_runtime_opts =3D qcow2_strong_runtime_opts, + .mutable_opts =3D mutable_opts, .bdrv_co_check =3D qcow2_co_check, .bdrv_amend_options =3D qcow2_amend_options, =20 diff --git a/block/raw-format.c b/block/raw-format.c index e3e5ba2c8a..cec29986cc 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -37,6 +37,8 @@ typedef struct BDRVRawState { bool has_size; } BDRVRawState; =20 +static const char *const mutable_opts[] =3D { "offset", "size", NULL }; + static QemuOptsList raw_runtime_opts =3D { .name =3D "raw", .head =3D QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), @@ -570,6 +572,7 @@ BlockDriver bdrv_raw =3D { .create_opts =3D &raw_create_opts, .bdrv_has_zero_init =3D &raw_has_zero_init, .strong_runtime_opts =3D raw_strong_runtime_opts, + .mutable_opts =3D mutable_opts, }; =20 static void bdrv_raw_init(void) diff --git a/include/block/block_int.h b/include/block/block_int.h index 438feba4e5..01e855a066 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -383,6 +383,14 @@ struct BlockDriver { =20 /* List of options for creating images, terminated by name =3D=3D NULL= */ QemuOptsList *create_opts; + /* + * If this driver supports reopening images this contains a + * NULL-terminated list of the runtime options that can be + * modified. If an option in this list is unspecified during + * reopen then it _must_ be reset to its default value or return + * an error. + */ + const char *const *mutable_opts; =20 /* * Returns 0 for completed check, -errno for internal errors. --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552410162529632.41512503361; Tue, 12 Mar 2019 10:02:42 -0700 (PDT) Received: from localhost ([127.0.0.1]:55952 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3knZ-0000xe-Dx for importer@patchew.org; Tue, 12 Mar 2019 13:02:41 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43791) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kau-00073e-K3 for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kat-00075F-38 for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:36 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33748) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kas-0006ji-MX; Tue, 12 Mar 2019 12:49:35 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006oq-4k; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaF-0007w2-89; Tue, 12 Mar 2019 18:48:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=vIBxG1RNhtszxOh9TaOGF2InNpIsh91oN72Wt6l5QGQ=; b=ZNJmKBEShvT0NVNuaceft14dcrKuXI68/uiP/GiF/La70Fv8YYG9u1rd8n77hWInCWrrbhzY4jy2vqdv0wLqEFfEXy3sQdGnyPe2Qew5+WxnxKnWuKERYTeH/tk4rqK5hlJLFs4rqEl0XYP2hLJ0RN6uDZ7Urqly14943VkT+AuZ+Re2ZcxaU1+40b3uCRc+XJ/aA/iVJ+IsiLlRhjsMdSv/hihmckG7+etCTtwCFfvZOA+QGJ2IB96WeB/uydNfpDcyATvTEOcvZcTU//kbOY5HABLR62TW2OlwK5Ti0oUCSBwbvbnhjspDpfnx/tflzvcTxfPoSsMDEYtFXsHh5A==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:49 +0200 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 10/13] block: Add bdrv_reset_options_allowed() 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" bdrv_reopen_prepare() receives a BDRVReopenState with (among other things) a new set of options to be applied to that BlockDriverState. If an option is missing then it means that we want to reset it to its default value rather than keep the previous one. This way the state of the block device after being reopened is comparable to that of a device added with "blockdev-add" using the same set of options. Not all options from all drivers can be changed this way, however. If the user attempts to reset an immutable option to its default value using this method then we must forbid it. This new function takes a BlockDriverState and a new set of options and checks if there's any option that was previously set but is missing from the new set of options. If the option is present in both sets we don't need to check that they have the same value. The loop at the end of bdrv_reopen_prepare() already takes care of that. Signed-off-by: Alberto Garcia --- block.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/block.c b/block.c index c6786cd35b..a5e69ad81e 100644 --- a/block.c +++ b/block.c @@ -2983,6 +2983,53 @@ BlockDriverState *bdrv_open(const char *filename, co= nst char *reference, NULL, errp); } =20 +/* Return true if the NULL-terminated @list contains @str */ +static bool is_str_in_list(const char *str, const char *const *list) +{ + if (str && list) { + int i; + for (i =3D 0; list[i] !=3D NULL; i++) { + if (!strcmp(str, list[i])) { + return true; + } + } + } + return false; +} + +/* + * Check that every option set in @bs->options is also set in + * @new_opts. + * + * Options listed in the common_options list and in + * @bs->drv->mutable_opts are skipped. + * + * Return 0 on success, otherwise return -EINVAL and set @errp. + */ +static int bdrv_reset_options_allowed(BlockDriverState *bs, + const QDict *new_opts, Error **errp) +{ + const QDictEntry *e; + /* These options are common to all block drivers and are handled + * in bdrv_reopen_prepare() so they can be left out of @new_opts */ + const char *const common_options[] =3D { + "node-name", "discard", "cache.direct", "cache.no-flush", + "read-only", "auto-read-only", "detect-zeroes", NULL + }; + + for (e =3D qdict_first(bs->options); e; e =3D qdict_next(bs->options, = e)) { + if (!qdict_haskey(new_opts, e->key) && + !is_str_in_list(e->key, common_options) && + !is_str_in_list(e->key, bs->drv->mutable_opts)) { + error_setg(errp, "Option '%s' cannot be reset " + "to its default value", e->key); + return -EINVAL; + } + } + + return 0; +} + /* * Returns true if @child can be reached recursively from @bs */ @@ -3546,6 +3593,17 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_stat= e, BlockReopenQueue *queue, } =20 if (drv->bdrv_reopen_prepare) { + /* + * If a driver-specific option is missing, it means that we + * should reset it to its default value. + * But not all options allow that, so we need to check it first. + */ + ret =3D bdrv_reset_options_allowed(reopen_state->bs, + reopen_state->options, errp); + if (ret) { + goto error; + } + ret =3D drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); if (ret) { if (local_err !=3D NULL) { --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552409775570233.5641479645186; Tue, 12 Mar 2019 09:56:15 -0700 (PDT) Received: from localhost ([127.0.0.1]:55860 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3khD-0003oc-DX for importer@patchew.org; Tue, 12 Mar 2019 12:56:07 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43456) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kaX-0006d5-AV for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kaV-0006hB-LX for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:13 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33710) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kaV-0006dG-6q; Tue, 12 Mar 2019 12:49:11 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006or-2t; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaF-0007w5-9a; Tue, 12 Mar 2019 18:48:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=haBIrD5MsmtU8/VhRsC+eHxe6FHDX8lZBLUwHJSuIy8=; b=ABzrmsdx8EDA33c8PDVpxxuOPhJZ8QH7t9ziPLDV+t+mcuruvG1AronTe3iv6g2Fy6qfHQQyYjNAfOFHNkn7PRVNMMO6J4wA5D/iazIHTvvzKn+yq/4WPpQWCQKV33KAcs6i0q0W5o/tedKIOfWQCLfOSrwZjhMjTQnADPGmbTU02ikGvZA6469pXa1XgD0A38B8JZmlLyddBO4vCA22bj85fVi7luu4ZEMRy8TF/ilX1E/P7py1vJ90HghGazv8RdhKHY1SLFhwdNLvY0cg/PDdu8VpJtdzzyNLwdudwmgTTdiGT0PxZnzQG0/YnypxrE/kplNYCxhX772sfs5Cgg==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:50 +0200 Message-Id: <54d3c78c0ac12c4b1b432b09a0c55bce7d0ef3c9.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 11/13] block: Remove the AioContext parameter from bdrv_reopen_multiple() 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This parameter has been unused since 1a63a907507fbbcfaee3f622907ec244b Signed-off-by: Alberto Garcia --- block.c | 4 ++-- block/replication.c | 3 +-- include/block/block.h | 2 +- qemu-io-cmds.c | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/block.c b/block.c index a5e69ad81e..ed9253c786 100644 --- a/block.c +++ b/block.c @@ -3254,7 +3254,7 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue = *bs_queue, * All affected nodes must be drained between bdrv_reopen_queue() and * bdrv_reopen_multiple(). */ -int bdrv_reopen_multiple(AioContext *ctx, BlockReopenQueue *bs_queue, Erro= r **errp) +int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) { int ret =3D -1; BlockReopenQueueEntry *bs_entry, *next; @@ -3347,7 +3347,7 @@ int bdrv_reopen_set_read_only(BlockDriverState *bs, b= ool read_only, =20 bdrv_subtree_drained_begin(bs); queue =3D bdrv_reopen_queue(NULL, bs, opts, true); - ret =3D bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, errp); + ret =3D bdrv_reopen_multiple(queue, errp); bdrv_subtree_drained_end(bs); =20 return ret; diff --git a/block/replication.c b/block/replication.c index a2f3590310..b95bd28802 100644 --- a/block/replication.c +++ b/block/replication.c @@ -385,8 +385,7 @@ static void reopen_backing_file(BlockDriverState *bs, b= ool writable, } =20 if (reopen_queue) { - bdrv_reopen_multiple(bdrv_get_aio_context(bs), - reopen_queue, &local_err); + bdrv_reopen_multiple(reopen_queue, &local_err); error_propagate(errp, local_err); } =20 diff --git a/include/block/block.h b/include/block/block.h index 68a3efbb43..e452988b66 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -304,7 +304,7 @@ BlockDriverState *bdrv_new_open_driver(BlockDriver *drv= , const char *node_name, BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, BlockDriverState *bs, QDict *options, bool keep_old_opts); -int bdrv_reopen_multiple(AioContext *ctx, BlockReopenQueue *bs_queue, Erro= r **errp); +int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp); int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only, Error **errp); int bdrv_reopen_prepare(BDRVReopenState *reopen_state, diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index ff9a5cd80f..35dcdcf413 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -2081,7 +2081,7 @@ static int reopen_f(BlockBackend *blk, int argc, char= **argv) =20 bdrv_subtree_drained_begin(bs); brq =3D bdrv_reopen_queue(NULL, bs, opts, true); - bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err); + bdrv_reopen_multiple(brq, &local_err); bdrv_subtree_drained_end(bs); =20 if (local_err) { --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552410367444610.2764799190137; Tue, 12 Mar 2019 10:06:07 -0700 (PDT) Received: from localhost ([127.0.0.1]:56009 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kqp-0003cr-Cp for importer@patchew.org; Tue, 12 Mar 2019 13:06:03 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43787) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kau-00073H-EX for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kas-000754-TK for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:36 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33745) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kas-0006jd-HM; Tue, 12 Mar 2019 12:49:34 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006os-5M; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaF-0007w8-Av; Tue, 12 Mar 2019 18:48:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=rylGsIMlGGGYij12/+yQQsF6slEWWCyeaULdjZwwjgg=; b=UW0KAcdEUwc3iQt5Fdb+Rvn2V+cM22JNISUoXtBMY8ZyyHzetN7YyW4asYDa88VvFYXnDL7dDcsaSm9Yny9/X3JHP9sMUC/qAAChE4TkxeD4e8VIgQ5ByCFxooIPpXFHLBuRnN6usd0QXaDrluj80fAsuduQj/pmxWF/9PsGubNRK3a8jOFErcp4FKSYXFiOau9g72DwuCjaXvm7nxazrpgR3gVFk0aRSvvQGg1ohKAdIojqJNJ4rt54c2KYMarGUp6HoeUKt160Bjtg4XV7M2AWqwJN2EunDM0viuUp1gUNBL4ALhlpZH4LFQDNAcAkOpobNymazswWrr+Pn2arEg==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:51 +0200 Message-Id: <31c8aabf777e21e4a8ac7acd5acef13d816e4a30.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 12/13] block: Add an 'x-blockdev-reopen' QMP command 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This command allows reopening an arbitrary BlockDriverState with a new set of options. Some options (e.g node-name) cannot be changed and some block drivers don't allow reopening, but otherwise this command is modelled after 'blockdev-add' and the state of the reopened BlockDriverState should generally be the same as if it had just been added by 'blockdev-add' with the same set of options. One notable exception is the 'backing' option: 'x-blockdev-reopen' requires that it is always present unless the BlockDriverState in question doesn't have a current or default backing file. This command allows reconfiguring the graph by using the appropriate options to change the children of a node. At the moment it's possible to change a backing file by setting the 'backing' option to the name of the new node, but it should also be possible to add a similar functionality to other block drivers (e.g. Quorum, blkverify). Although the API is unlikely to change, this command is marked experimental for the time being so there's room to see if the semantics need changes. Signed-off-by: Alberto Garcia --- blockdev.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ qapi/block-core.json | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/blockdev.c b/blockdev.c index 84dd678289..3cc51b2ee7 100644 --- a/blockdev.c +++ b/blockdev.c @@ -4287,6 +4287,53 @@ fail: visit_free(v); } =20 +void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp) +{ + BlockDriverState *bs; + AioContext *ctx; + QObject *obj; + Visitor *v =3D qobject_output_visitor_new(&obj); + Error *local_err =3D NULL; + BlockReopenQueue *queue; + QDict *qdict; + + /* Check for the selected node name */ + if (!options->has_node_name) { + error_setg(errp, "Node name not specified"); + goto fail; + } + + bs =3D bdrv_find_node(options->node_name); + if (!bs) { + error_setg(errp, "Cannot find node named '%s'", options->node_name= ); + goto fail; + } + + /* Put all options in a QDict and flatten it */ + visit_type_BlockdevOptions(v, NULL, &options, &local_err); + if (local_err) { + error_propagate(errp, local_err); + goto fail; + } + + visit_complete(v, &obj); + qdict =3D qobject_to(QDict, obj); + + qdict_flatten(qdict); + + /* Perform the reopen operation */ + ctx =3D bdrv_get_aio_context(bs); + aio_context_acquire(ctx); + bdrv_subtree_drained_begin(bs); + queue =3D bdrv_reopen_queue(NULL, bs, qdict, false); + bdrv_reopen_multiple(queue, errp); + bdrv_subtree_drained_end(bs); + aio_context_release(ctx); + +fail: + visit_free(v); +} + void qmp_blockdev_del(const char *node_name, Error **errp) { AioContext *aio_context; diff --git a/qapi/block-core.json b/qapi/block-core.json index 7377fecb46..474e268c4d 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3998,6 +3998,48 @@ { 'command': 'blockdev-add', 'data': 'BlockdevOptions', 'boxed': true } =20 ## +# @x-blockdev-reopen: +# +# Reopens a block device using the given set of options. Any option +# not specified will be reset to its default value regardless of its +# previous status. If an option cannot be changed or a particular +# driver does not support reopening then the command will return an +# error. +# +# The top-level @node-name option (from BlockdevOptions) must be +# specified and is used to select the block device to be reopened. +# Other @node-name options must be either omitted or set to the +# current name of the appropriate node. This command won't change any +# node name and any attempt to do it will result in an error. +# +# In the case of options that refer to child nodes, the behavior of +# this command depends on the value: +# +# 1) A set of options (BlockdevOptions): the child is reopened with +# the specified set of options. +# +# 2) A reference to the current child: the child is reopened using +# its existing set of options. +# +# 3) A reference to a different node: the current child is replaced +# with the specified one. +# +# 4) NULL: the current child (if any) is detached. +# +# Options (1) and (2) are supported in all cases, but at the moment +# only @backing allows replacing or detaching an existing child. +# +# Unlike with blockdev-add, the @backing option must always be present +# unless the node being reopened does not have a backing file and its +# image does not have a default backing file name as part of its +# metadata. +# +# Since: 4.0 +## +{ 'command': 'x-blockdev-reopen', + 'data': 'BlockdevOptions', 'boxed': true } + +## # @blockdev-del: # # Deletes a block device that has been added using blockdev-add. --=20 2.11.0 From nobody Sun May 5 22:29:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552409978244782.5501825289994; Tue, 12 Mar 2019 09:59:38 -0700 (PDT) Received: from localhost ([127.0.0.1]:55889 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kkY-0006hd-1B for importer@patchew.org; Tue, 12 Mar 2019 12:59:34 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43668) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3kaf-0006li-Nn for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3kaW-0006hr-1z for qemu-devel@nongnu.org; Tue, 12 Mar 2019 12:49:21 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:33708) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3kaV-0006dD-Ap; Tue, 12 Mar 2019 12:49:11 -0400 Received: from 87-100-202-60.bb.dnainternet.fi ([87.100.202.60] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1h3kaS-0006ot-6Y; Tue, 12 Mar 2019 17:49:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1h3kaF-0007wB-By; Tue, 12 Mar 2019 18:48:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=DIoFNjyiqCNuwlCccYoxboPZP5SM6yBd7Y/HSZ0g8cU=; b=IC7kInYoe/+F4u+aIPj+82R6q0CxFegTpw5LhLxMNFtSyVyUoPb2PQ1T/GsiQ/etVsn3inUsDbNRxLVB4OvyAt9LtGQYc2083LUhCrMle9l+a0M2JsQp8txtdux5WXfQ91uVVlBPjGHictHh1+jIO9EvJHwVGNM6mIQzXtMVtq4oopEHdUU802F4ctwR0SyteuNQmVa1+Dr8G++of7WbVhAHw/4Gpbs67omkOdXHjtJiUb1dKFqbCUWGZ0d7NUVQc0UTaEEv6UvaPZvUVUYQrrAWOkXxko297ZJGDamDgUMvuDkKibwfjsEuTWJMo9r+2aWGKnw7kI8tm0IxAvMRBQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 18:48:52 +0200 Message-Id: <44d052dc15afee8dc2e05c094c30cc262cae4b0c.1552409054.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 13/13] qemu-iotests: Test the x-blockdev-reopen QMP command 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds several tests for the x-blockdev-reopen QMP command. Signed-off-by: Alberto Garcia --- tests/qemu-iotests/245 | 991 +++++++++++++++++++++++++++++++++++++++++= ++++ tests/qemu-iotests/245.out | 5 + tests/qemu-iotests/group | 1 + 3 files changed, 997 insertions(+) create mode 100644 tests/qemu-iotests/245 create mode 100644 tests/qemu-iotests/245.out diff --git a/tests/qemu-iotests/245 b/tests/qemu-iotests/245 new file mode 100644 index 0000000000..7891a210c1 --- /dev/null +++ b/tests/qemu-iotests/245 @@ -0,0 +1,991 @@ +#!/usr/bin/env python +# +# Test cases for the QMP 'x-blockdev-reopen' command +# +# Copyright (C) 2018-2019 Igalia, S.L. +# Author: Alberto Garcia +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +import os +import re +import iotests +import copy +import json +from iotests import qemu_img, qemu_io + +hd_path =3D [ + os.path.join(iotests.test_dir, 'hd0.img'), + os.path.join(iotests.test_dir, 'hd1.img'), + os.path.join(iotests.test_dir, 'hd2.img') +] + +def hd_opts(idx): + return {'driver': iotests.imgfmt, + 'node-name': 'hd%d' % idx, + 'file': {'driver': 'file', + 'node-name': 'hd%d-file' % idx, + 'filename': hd_path[idx] } } + +class TestBlockdevReopen(iotests.QMPTestCase): + total_io_cmds =3D 0 + + def setUp(self): + qemu_img('create', '-f', iotests.imgfmt, hd_path[0], '3M') + qemu_img('create', '-f', iotests.imgfmt, '-b', hd_path[0], hd_path= [1]) + qemu_img('create', '-f', iotests.imgfmt, hd_path[2], '3M') + qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa0 0 1M', hd_path= [0]) + qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa1 1M 1M', hd_path= [1]) + qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa2 2M 1M', hd_path= [2]) + self.vm =3D iotests.VM() + self.vm.launch() + + def tearDown(self): + self.vm.shutdown() + self.check_qemu_io_errors() + os.remove(hd_path[0]) + os.remove(hd_path[1]) + os.remove(hd_path[2]) + + # The output of qemu-io is not returned by vm.hmp_qemu_io() but + # it's stored in the log and can only be read when the VM has been + # shut down. This function runs qemu-io and keeps track of the + # number of times it's been called. + def run_qemu_io(self, img, cmd): + result =3D self.vm.hmp_qemu_io(img, cmd) + self.assert_qmp(result, 'return', '') + self.total_io_cmds +=3D 1 + + # Once the VM is shut down we can parse the log and see if qemu-io + # ran without errors. + def check_qemu_io_errors(self): + self.assertFalse(self.vm.is_running()) + found =3D 0 + log =3D self.vm.get_log() + for line in log.split("\n"): + if line.startswith("Pattern verification failed"): + raise Exception("%s (command #%d)" % (line, found)) + if re.match("read .*/.* bytes at offset", line): + found +=3D 1 + self.assertEqual(found, self.total_io_cmds, + "Expected output of %d qemu-io commands, found %d= " % + (found, self.total_io_cmds)) + + # Run x-blockdev-reopen with 'opts' but applying 'newopts' + # on top of it. The original 'opts' dict is unmodified + def reopen(self, opts, newopts =3D {}, errmsg =3D None): + opts =3D copy.deepcopy(opts) + + # Apply the changes from 'newopts' on top of 'opts' + for key in newopts: + value =3D newopts[key] + # If key has the form "foo.bar" then we need to do + # opts["foo"]["bar"] =3D value, not opts["foo.bar"] =3D value + subdict =3D opts + while key.find('.') !=3D -1: + [prefix, key] =3D key.split('.', 1) + subdict =3D opts[prefix] + subdict[key] =3D value + + result =3D self.vm.qmp('x-blockdev-reopen', conv_keys =3D False, *= *opts) + if errmsg: + self.assert_qmp(result, 'error/class', 'GenericError') + self.assert_qmp(result, 'error/desc', errmsg) + else: + self.assert_qmp(result, 'return', {}) + + + # Run query-named-block-nodes and return the specified entry + def get_node(self, node_name): + result =3D self.vm.qmp('query-named-block-nodes') + for node in result['return']: + if node['node-name'] =3D=3D node_name: + return node + return None + + # Run 'query-named-block-nodes' and compare its output with the + # value passed by the user in 'graph' + def check_node_graph(self, graph): + result =3D self.vm.qmp('query-named-block-nodes') + self.assertEqual(json.dumps(graph, sort_keys=3DTrue), + json.dumps(result, sort_keys=3DTrue)) + + # This test opens one single disk image (without backing files) + # and tries to reopen it with illegal / incorrect parameters. + def test_incorrect_parameters_single_file(self): + # Open 'hd0' only (no backing files) + opts =3D hd_opts(0) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + original_graph =3D self.vm.qmp('query-named-block-nodes') + + # We can reopen the image passing the same options + self.reopen(opts) + + # We can also reopen passing a child reference in 'file' + self.reopen(opts, {'file': 'hd0-file'}) + + # We cannot change any of these + self.reopen(opts, {'node-name': 'not-found'}, "Cannot find node na= med 'not-found'") + self.reopen(opts, {'node-name': ''}, "Cannot find node named ''") + self.reopen(opts, {'node-name': None}, "Invalid parameter type for= 'node-name', expected: string") + self.reopen(opts, {'driver': 'raw'}, "Cannot change the option 'dr= iver'") + self.reopen(opts, {'driver': ''}, "Invalid parameter ''") + self.reopen(opts, {'driver': None}, "Invalid parameter type for 'd= river', expected: string") + self.reopen(opts, {'file': 'not-found'}, "Cannot change the option= 'file'") + self.reopen(opts, {'file': ''}, "Cannot change the option 'file'") + self.reopen(opts, {'file': None}, "Invalid parameter type for 'fil= e', expected: BlockdevRef") + self.reopen(opts, {'file.node-name': 'newname'}, "Cannot change th= e option 'node-name'") + self.reopen(opts, {'file.driver': 'host_device'}, "Cannot change t= he option 'driver'") + self.reopen(opts, {'file.filename': hd_path[1]}, "Cannot change th= e option 'filename'") + self.reopen(opts, {'file.aio': 'native'}, "Cannot change the optio= n 'aio'") + self.reopen(opts, {'file.locking': 'off'}, "Cannot change the opti= on 'locking'") + self.reopen(opts, {'file.filename': None}, "Invalid parameter type= for 'file.filename', expected: string") + + # node-name is optional in BlockdevOptions, but x-blockdev-reopen = needs it + del opts['node-name'] + self.reopen(opts, {}, "Node name not specified") + + # Check that nothing has changed + self.check_node_graph(original_graph) + + # Remove the node + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd0') + self.assert_qmp(result, 'return', {}) + + # This test opens an image with a backing file and tries to reopen + # it with illegal / incorrect parameters. + def test_incorrect_parameters_backing_file(self): + # Open hd1 omitting the backing options (hd0 will be opened + # with the default options) + opts =3D hd_opts(1) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + original_graph =3D self.vm.qmp('query-named-block-nodes') + + # We can't reopen the image passing the same options, 'backing' is= mandatory + self.reopen(opts, {}, "backing is missing for 'hd1'") + + # Everything works if we pass 'backing' using the existing node na= me + for node in original_graph['return']: + if node['drv'] =3D=3D iotests.imgfmt and node['file'] =3D=3D h= d_path[0]: + backing_node_name =3D node['node-name'] + self.reopen(opts, {'backing': backing_node_name}) + + # We can't use a non-existing or empty (non-NULL) node as the back= ing image + self.reopen(opts, {'backing': 'not-found'}, "Cannot find device=3D= nor node_name=3Dnot-found") + self.reopen(opts, {'backing': ''}, "Cannot find device=3D nor node= _name=3D") + + # We can reopen the image just fine if we specify the backing opti= ons + opts['backing'] =3D {'driver': iotests.imgfmt, + 'file': {'driver': 'file', + 'filename': hd_path[0]}} + self.reopen(opts) + + # We cannot change any of these options + self.reopen(opts, {'backing.node-name': 'newname'}, "Cannot change= the option 'node-name'") + self.reopen(opts, {'backing.driver': 'raw'}, "Cannot change the op= tion 'driver'") + self.reopen(opts, {'backing.file.node-name': 'newname'}, "Cannot c= hange the option 'node-name'") + self.reopen(opts, {'backing.file.driver': 'host_device'}, "Cannot = change the option 'driver'") + + # Check that nothing has changed since the beginning + self.check_node_graph(original_graph) + + # Remove the node + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd1') + self.assert_qmp(result, 'return', {}) + + # Reopen an image several times changing some of its options + def test_reopen(self): + # Open the hd1 image passing all backing options + opts =3D hd_opts(1) + opts['backing'] =3D hd_opts(0) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + original_graph =3D self.vm.qmp('query-named-block-nodes') + + # We can reopen the image passing the same options + self.reopen(opts) + + # Reopen in read-only mode + self.assert_qmp(self.get_node('hd1'), 'ro', False) + + self.reopen(opts, {'read-only': True}) + self.assert_qmp(self.get_node('hd1'), 'ro', True) + self.reopen(opts) + self.assert_qmp(self.get_node('hd1'), 'ro', False) + + # Change the cache options + self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True) + self.assert_qmp(self.get_node('hd1'), 'cache/direct', False) + self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False) + self.reopen(opts, {'cache': { 'direct': True, 'no-flush': True }}) + self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True) + self.assert_qmp(self.get_node('hd1'), 'cache/direct', True) + self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', True) + + # Reopen again with the original options + self.reopen(opts) + self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True) + self.assert_qmp(self.get_node('hd1'), 'cache/direct', False) + self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False) + + # Change 'detect-zeroes' and 'discard' + self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off') + self.reopen(opts, {'detect-zeroes': 'on'}) + self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on') + self.reopen(opts, {'detect-zeroes': 'unmap'}, + "setting detect-zeroes to unmap is not allowed " + + "without setting discard operation to unmap") + self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on') + self.reopen(opts, {'detect-zeroes': 'unmap', 'discard': 'unmap'}) + self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'unmap') + self.reopen(opts) + self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off') + + # Changing 'force-share' is currently not supported + self.reopen(opts, {'force-share': True}, "Cannot change the option= 'force-share'") + + # Change some qcow2-specific options + # No way to test for success other than checking the return message + if iotests.imgfmt =3D=3D 'qcow2': + self.reopen(opts, {'l2-cache-entry-size': 128 * 1024}, + "L2 cache entry size must be a power of two "+ + "between 512 and the cluster size (65536)") + self.reopen(opts, {'l2-cache-size': 1024 * 1024, + 'cache-size': 512 * 1024}, + "l2-cache-size may not exceed cache-size") + self.reopen(opts, {'l2-cache-size': 4 * 1024 * 1024, + 'refcount-cache-size': 4 * 1024 * 1024, + 'l2-cache-entry-size': 32 * 1024}) + self.reopen(opts, {'pass-discard-request': True}) + + # Check that nothing has changed since the beginning + # (from the parameters that we can check) + self.check_node_graph(original_graph) + + # Check that the node names (other than the top-level one) are opt= ional + del opts['file']['node-name'] + del opts['backing']['node-name'] + del opts['backing']['file']['node-name'] + self.reopen(opts) + self.check_node_graph(original_graph) + + # Reopen setting backing =3D null, this removes the backing image = from the chain + self.reopen(opts, {'backing': None}) + self.assert_qmp_absent(self.get_node('hd1'), 'image/backing-image') + + # Open the 'hd0' image + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **hd_o= pts(0)) + self.assert_qmp(result, 'return', {}) + + # Reopen the hd1 image setting 'hd0' as its backing image + self.reopen(opts, {'backing': 'hd0'}) + self.assert_qmp(self.get_node('hd1'), 'image/backing-image/filenam= e', hd_path[0]) + + # Check that nothing has changed since the beginning + self.reopen(hd_opts(0), {'read-only': True}) + self.check_node_graph(original_graph) + + # The backing file (hd0) is now a reference, we cannot change back= ing.* anymore + self.reopen(opts, {}, "Cannot change the option 'backing.driver'") + + # We can't remove 'hd0' while it's a backing image of 'hd1' + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd0') + self.assert_qmp(result, 'error/class', 'GenericError') + self.assert_qmp(result, 'error/desc', "Node 'hd0' is busy: node is= used as backing hd of 'hd1'") + + # But we can remove both nodes if done in the proper order + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd1') + self.assert_qmp(result, 'return', {}) + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd0') + self.assert_qmp(result, 'return', {}) + + # Reopen a raw image and see the effect of changing the 'offset' option + def test_reopen_raw(self): + opts =3D {'driver': 'raw', 'node-name': 'hd0', + 'file': { 'driver': 'file', + 'filename': hd_path[0], + 'node-name': 'hd0-file' } } + + # First we create a 2MB raw file, and fill each half with a + # different value + qemu_img('create', '-f', 'raw', hd_path[0], '2M') + qemu_io('-f', 'raw', '-c', 'write -P 0xa0 0 1M', hd_path[0]) + qemu_io('-f', 'raw', '-c', 'write -P 0xa1 1M 1M', hd_path[0]) + + # Open the raw file with QEMU + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # Read 1MB from offset 0 + self.run_qemu_io("hd0", "read -P 0xa0 0 1M") + + # Reopen the image with a 1MB offset. + # Now the results are different + self.reopen(opts, {'offset': 1024*1024}) + self.run_qemu_io("hd0", "read -P 0xa1 0 1M") + + # Reopen again with the original options. + # We get the original results again + self.reopen(opts) + self.run_qemu_io("hd0", "read -P 0xa0 0 1M") + + # Remove the block device + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd0') + self.assert_qmp(result, 'return', {}) + + # Omitting an option should reset it to the default value, but if + # an option cannot be changed it shouldn't be possible to reset it + # to its default value either + def test_reset_default_values(self): + opts =3D {'driver': 'qcow2', 'node-name': 'hd0', + 'file': { 'driver': 'file', + 'filename': hd_path[0], + 'x-check-cache-dropped': True, # This one can be= changed + 'locking': 'off', # This one can NO= T be changed + 'node-name': 'hd0-file' } } + + # Open the file with QEMU + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # file.x-check-cache-dropped can be changed... + self.reopen(opts, { 'file.x-check-cache-dropped': False }) + # ...and dropped completely (resetting to the default value) + del opts['file']['x-check-cache-dropped'] + self.reopen(opts) + + # file.locking cannot be changed nor reset to the default value + self.reopen(opts, { 'file.locking': 'on' }, "Cannot change the opt= ion 'locking'") + del opts['file']['locking'] + self.reopen(opts, {}, "Option 'locking' cannot be reset to its def= ault value") + # But we can reopen it if we maintain its previous value + self.reopen(opts, { 'file.locking': 'off' }) + + # Remove the block device + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd0') + self.assert_qmp(result, 'return', {}) + + # This test modifies the node graph a few times by changing the + # 'backing' option on reopen and verifies that the guest data that + # is read afterwards is consistent with the graph changes. + def test_io_with_graph_changes(self): + opts =3D [] + + # Open hd0, hd1 and hd2 without any backing image + for i in range(3): + opts.append(hd_opts(i)) + opts[i]['backing'] =3D None + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **= opts[i]) + self.assert_qmp(result, 'return', {}) + + # hd0 + self.run_qemu_io("hd0", "read -P 0xa0 0 1M") + self.run_qemu_io("hd0", "read -P 0 1M 1M") + self.run_qemu_io("hd0", "read -P 0 2M 1M") + + # hd1 <- hd0 + self.reopen(opts[0], {'backing': 'hd1'}) + + self.run_qemu_io("hd0", "read -P 0xa0 0 1M") + self.run_qemu_io("hd0", "read -P 0xa1 1M 1M") + self.run_qemu_io("hd0", "read -P 0 2M 1M") + + # hd1 <- hd0 , hd1 <- hd2 + self.reopen(opts[2], {'backing': 'hd1'}) + + self.run_qemu_io("hd2", "read -P 0 0 1M") + self.run_qemu_io("hd2", "read -P 0xa1 1M 1M") + self.run_qemu_io("hd2", "read -P 0xa2 2M 1M") + + # hd1 <- hd2 <- hd0 + self.reopen(opts[0], {'backing': 'hd2'}) + + self.run_qemu_io("hd0", "read -P 0xa0 0 1M") + self.run_qemu_io("hd0", "read -P 0xa1 1M 1M") + self.run_qemu_io("hd0", "read -P 0xa2 2M 1M") + + # hd2 <- hd0 + self.reopen(opts[2], {'backing': None}) + + self.run_qemu_io("hd0", "read -P 0xa0 0 1M") + self.run_qemu_io("hd0", "read -P 0 1M 1M") + self.run_qemu_io("hd0", "read -P 0xa2 2M 1M") + + # hd2 <- hd1 <- hd0 + self.reopen(opts[1], {'backing': 'hd2'}) + self.reopen(opts[0], {'backing': 'hd1'}) + + self.run_qemu_io("hd0", "read -P 0xa0 0 1M") + self.run_qemu_io("hd0", "read -P 0xa1 1M 1M") + self.run_qemu_io("hd0", "read -P 0xa2 2M 1M") + + # Illegal operation: hd2 is a child of hd1 + self.reopen(opts[2], {'backing': 'hd1'}, + "Making 'hd1' a backing file of 'hd2' would create a c= ycle") + + # hd2 <- hd0, hd2 <- hd1 + self.reopen(opts[0], {'backing': 'hd2'}) + + self.run_qemu_io("hd1", "read -P 0 0 1M") + self.run_qemu_io("hd1", "read -P 0xa1 1M 1M") + self.run_qemu_io("hd1", "read -P 0xa2 2M 1M") + + # More illegal operations + self.reopen(opts[2], {'backing': 'hd1'}, + "Making 'hd1' a backing file of 'hd2' would create a c= ycle") + self.reopen(opts[2], {'file': 'hd0-file'}, "Cannot change the opti= on 'file'") + + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd2') + self.assert_qmp(result, 'error/class', 'GenericError') + self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is= used as backing hd of 'hd0'") + + # hd1 doesn't have a backing file now + self.reopen(opts[1], {'backing': None}) + + self.run_qemu_io("hd1", "read -P 0 0 1M") + self.run_qemu_io("hd1", "read -P 0xa1 1M 1M") + self.run_qemu_io("hd1", "read -P 0 2M 1M") + + # We can't remove the 'backing' option if the image has a + # default backing file + del opts[1]['backing'] + self.reopen(opts[1], {}, "backing is missing for 'hd1'") + + self.run_qemu_io("hd1", "read -P 0 0 1M") + self.run_qemu_io("hd1", "read -P 0xa1 1M 1M") + self.run_qemu_io("hd1", "read -P 0 2M 1M") + + # This test verifies that we can't change the children of a block + # device during a reopen operation in a way that would create + # cycles in the node graph + def test_graph_cycles(self): + opts =3D [] + + # Open all three images without backing file + for i in range(3): + opts.append(hd_opts(i)) + opts[i]['backing'] =3D None + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **= opts[i]) + self.assert_qmp(result, 'return', {}) + + # hd1 <- hd0, hd1 <- hd2 + self.reopen(opts[0], {'backing': 'hd1'}) + self.reopen(opts[2], {'backing': 'hd1'}) + + # Illegal: hd2 is backed by hd1 + self.reopen(opts[1], {'backing': 'hd2'}, + "Making 'hd2' a backing file of 'hd1' would create a c= ycle") + + # hd1 <- hd0 <- hd2 + self.reopen(opts[2], {'backing': 'hd0'}) + + # Illegal: hd2 is backed by hd0, which is backed by hd1 + self.reopen(opts[1], {'backing': 'hd2'}, + "Making 'hd2' a backing file of 'hd1' would create a c= ycle") + + # Illegal: hd1 cannot point to itself + self.reopen(opts[1], {'backing': 'hd1'}, + "Making 'hd1' a backing file of 'hd1' would create a c= ycle") + + # Remove all backing files + self.reopen(opts[0]) + self.reopen(opts[2]) + + ########################################## + # Add a blkverify node using hd0 and hd1 # + ########################################## + bvopts =3D {'driver': 'blkverify', + 'node-name': 'bv', + 'test': 'hd0', + 'raw': 'hd1'} + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **bvop= ts) + self.assert_qmp(result, 'return', {}) + + # blkverify doesn't currently allow reopening. TODO: implement this + self.reopen(bvopts, {}, "Block format 'blkverify' used by node 'bv= '" + + " does not support reopening files") + + # Illegal: hd0 is a child of the blkverify node + self.reopen(opts[0], {'backing': 'bv'}, + "Making 'bv' a backing file of 'hd0' would create a cy= cle") + + # Delete the blkverify node + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'bv') + self.assert_qmp(result, 'return', {}) + + # Misc reopen tests with different block drivers + def test_misc_drivers(self): + #################### + ###### quorum ###### + #################### + for i in range(3): + opts =3D hd_opts(i) + # Open all three images without backing file + opts['backing'] =3D None + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **= opts) + self.assert_qmp(result, 'return', {}) + + opts =3D {'driver': 'quorum', + 'node-name': 'quorum0', + 'children': ['hd0', 'hd1', 'hd2'], + 'vote-threshold': 2} + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # Quorum doesn't currently allow reopening. TODO: implement this + self.reopen(opts, {}, "Block format 'quorum' used by node 'quorum0= '" + + " does not support reopening files") + + # You can't make quorum0 a backing file of hd0: + # hd0 is already a child of quorum0. + self.reopen(hd_opts(0), {'backing': 'quorum0'}, + "Making 'quorum0' a backing file of 'hd0' would create= a cycle") + + # Delete quorum0 + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'quorum0') + self.assert_qmp(result, 'return', {}) + + # Delete hd0, hd1 and hd2 + for i in range(3): + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, + node_name =3D 'hd%d' % i) + self.assert_qmp(result, 'return', {}) + + ###################### + ###### blkdebug ###### + ###################### + opts =3D {'driver': 'blkdebug', + 'node-name': 'bd', + 'config': '/dev/null', + 'image': hd_opts(0)} + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # blkdebug allows reopening if we keep the same options + self.reopen(opts) + + # but it currently does not allow changes + self.reopen(opts, {'image': 'hd1'}, "Cannot change the option 'ima= ge'") + self.reopen(opts, {'align': 33554432}, "Cannot change the option '= align'") + self.reopen(opts, {'config': '/non/existent'}, "Cannot change the = option 'config'") + del opts['config'] + self.reopen(opts, {}, "Option 'config' cannot be reset to its defa= ult value") + + # Delete the blkdebug node + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'bd') + self.assert_qmp(result, 'return', {}) + + ################## + ###### null ###### + ################## + opts =3D {'driver': 'null-aio', 'node-name': 'root', 'size': 1024} + + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # 1 << 30 is the default value, but we cannot change it explicitly + self.reopen(opts, {'size': (1 << 30)}, "Cannot change the option '= size'") + + # We cannot change 'size' back to its default value either + del opts['size'] + self.reopen(opts, {}, "Option 'size' cannot be reset to its defaul= t value") + + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'root') + self.assert_qmp(result, 'return', {}) + + ################## + ###### file ###### + ################## + opts =3D hd_opts(0) + opts['file']['locking'] =3D 'on' + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # 'locking' cannot be changed + del opts['file']['locking'] + self.reopen(opts, {'file.locking': 'on'}) + self.reopen(opts, {'file.locking': 'off'}, "Cannot change the opti= on 'locking'") + self.reopen(opts, {}, "Option 'locking' cannot be reset to its def= ault value") + + # Trying to reopen the 'file' node directly does not make a differ= ence + opts =3D opts['file'] + self.reopen(opts, {'locking': 'on'}) + self.reopen(opts, {'locking': 'off'}, "Cannot change the option 'l= ocking'") + self.reopen(opts, {}, "Option 'locking' cannot be reset to its def= ault value") + + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd0') + self.assert_qmp(result, 'return', {}) + + ###################### + ###### throttle ###### + ###################### + opts =3D { 'qom-type': 'throttle-group', 'id': 'group0', + 'props': { 'limits': { 'iops-total': 1000 } } } + result =3D self.vm.qmp('object-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + opts =3D { 'qom-type': 'throttle-group', 'id': 'group1', + 'props': { 'limits': { 'iops-total': 2000 } } } + result =3D self.vm.qmp('object-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # Add a throttle filter with group =3D group0 + opts =3D { 'driver': 'throttle', 'node-name': 'throttle0', + 'throttle-group': 'group0', 'file': hd_opts(0) } + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # We can reopen it if we keep the same options + self.reopen(opts) + + # We can also reopen if 'file' is a reference to the child + self.reopen(opts, {'file': 'hd0'}) + + # This is illegal + self.reopen(opts, {'throttle-group': 'notfound'}, "Throttle group = 'notfound' does not exist") + + # But it's possible to change the group to group1 + self.reopen(opts, {'throttle-group': 'group1'}) + + # Now group1 is in use, it cannot be deleted + result =3D self.vm.qmp('object-del', id =3D 'group1') + self.assert_qmp(result, 'error/class', 'GenericError') + self.assert_qmp(result, 'error/desc', "object 'group1' is in use, = can not be deleted") + + # Default options, this switches the group back to group0 + self.reopen(opts) + + # So now we cannot delete group0 + result =3D self.vm.qmp('object-del', id =3D 'group0') + self.assert_qmp(result, 'error/class', 'GenericError') + self.assert_qmp(result, 'error/desc', "object 'group0' is in use, = can not be deleted") + + # But group1 is free this time, and it can be deleted + result =3D self.vm.qmp('object-del', id =3D 'group1') + self.assert_qmp(result, 'return', {}) + + # Let's delete the filter node + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'throttle0') + self.assert_qmp(result, 'return', {}) + + # And we can finally get rid of group0 + result =3D self.vm.qmp('object-del', id =3D 'group0') + self.assert_qmp(result, 'return', {}) + + # If an image has a backing file then the 'backing' option must be + # passed on reopen. We don't allow leaving the option out in this + # case because it's unclear what the correct semantics would be. + def test_missing_backing_options_1(self): + # hd2 + opts =3D hd_opts(2) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # hd0 + opts =3D hd_opts(0) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # hd0 has no backing file: we can omit the 'backing' option + self.reopen(opts) + + # hd2 <- hd0 + self.reopen(opts, {'backing': 'hd2'}) + + # hd0 has a backing file: we must set the 'backing' option + self.reopen(opts, {}, "backing is missing for 'hd0'") + + # hd2 can't be removed because it's the backing file of hd0 + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd2') + self.assert_qmp(result, 'error/class', 'GenericError') + self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is= used as backing hd of 'hd0'") + + # Detach hd2 from hd0. + self.reopen(opts, {'backing': None}) + self.reopen(opts, {}, "backing is missing for 'hd0'") + + # Remove both hd0 and hd2 + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd0') + self.assert_qmp(result, 'return', {}) + + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd2') + self.assert_qmp(result, 'return', {}) + + # If an image has default backing file (as part of its metadata) + # then the 'backing' option must be passed on reopen. We don't + # allow leaving the option out in this case because it's unclear + # what the correct semantics would be. + def test_missing_backing_options_2(self): + # hd0 <- hd1 + # (hd0 is hd1's default backing file) + opts =3D hd_opts(1) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # hd1 has a backing file: we can't omit the 'backing' option + self.reopen(opts, {}, "backing is missing for 'hd1'") + + # Let's detach the backing file + self.reopen(opts, {'backing': None}) + + # No backing file attached to hd1 now, but we still can't omit the= 'backing' option + self.reopen(opts, {}, "backing is missing for 'hd1'") + + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd1') + self.assert_qmp(result, 'return', {}) + + # Test that making 'backing' a reference to an existing child + # keeps its current options + def test_backing_reference(self): + # hd2 <- hd1 <- hd0 + opts =3D hd_opts(0) + opts['backing'] =3D hd_opts(1) + opts['backing']['backing'] =3D hd_opts(2) + # Enable 'detect-zeroes' on all three nodes + opts['detect-zeroes'] =3D 'on' + opts['backing']['detect-zeroes'] =3D 'on' + opts['backing']['backing']['detect-zeroes'] =3D 'on' + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # Reopen the chain passing the minimum amount of required options. + # By making 'backing' a reference to hd1 (instead of a sub-dict) + # we tell QEMU to keep its current set of options. + opts =3D {'driver': iotests.imgfmt, + 'node-name': 'hd0', + 'file': 'hd0-file', + 'backing': 'hd1' } + self.reopen(opts) + + # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2. + self.assert_qmp(self.get_node('hd0'), 'detect_zeroes', 'off') + self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on') + self.assert_qmp(self.get_node('hd2'), 'detect_zeroes', 'on') + + # Test what happens if the graph changes due to other operations + # such as block-stream + def test_block_stream_1(self): + # hd1 <- hd0 + opts =3D hd_opts(0) + opts['backing'] =3D hd_opts(1) + opts['backing']['backing'] =3D None + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # Stream hd1 into hd0 and wait until it's done + result =3D self.vm.qmp('block-stream', conv_keys =3D True, job_id = =3D 'stream0', device =3D 'hd0') + self.assert_qmp(result, 'return', {}) + self.wait_until_completed(drive =3D 'stream0') + + # Now we have only hd0 + self.assertEqual(self.get_node('hd1'), None) + + # We have backing.* options but there's no backing file anymore + self.reopen(opts, {}, "Cannot change the option 'backing.driver'") + + # If we remove the 'backing' option then we can reopen hd0 just fi= ne + del opts['backing'] + self.reopen(opts) + + # We can also reopen hd0 if we set 'backing' to null + self.reopen(opts, {'backing': None}) + + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd0') + self.assert_qmp(result, 'return', {}) + + # Another block_stream test + def test_block_stream_2(self): + # hd2 <- hd1 <- hd0 + opts =3D hd_opts(0) + opts['backing'] =3D hd_opts(1) + opts['backing']['backing'] =3D hd_opts(2) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # Stream hd1 into hd0 and wait until it's done + result =3D self.vm.qmp('block-stream', conv_keys =3D True, job_id = =3D 'stream0', + device =3D 'hd0', base_node =3D 'hd2') + self.assert_qmp(result, 'return', {}) + self.wait_until_completed(drive =3D 'stream0') + + # The chain is hd2 <- hd0 now. hd1 is missing + self.assertEqual(self.get_node('hd1'), None) + + # The backing options in the dict were meant for hd1, but we cannot + # use them with hd2 because hd1 had a backing file while hd2 does = not. + self.reopen(opts, {}, "Cannot change the option 'backing.driver'") + + # If we remove hd1's options from the dict then things work fine + opts['backing'] =3D opts['backing']['backing'] + self.reopen(opts) + + # We can also reopen hd0 if we use a reference to the backing file + self.reopen(opts, {'backing': 'hd2'}) + + # But we cannot leave the option out + del opts['backing'] + self.reopen(opts, {}, "backing is missing for 'hd0'") + + # Now we can delete hd0 (and hd2) + result =3D self.vm.qmp('blockdev-del', conv_keys =3D True, node_na= me =3D 'hd0') + self.assert_qmp(result, 'return', {}) + self.assertEqual(self.get_node('hd2'), None) + + # Reopen the chain during a block-stream job (from hd1 to hd0) + def test_block_stream_3(self): + # hd2 <- hd1 <- hd0 + opts =3D hd_opts(0) + opts['backing'] =3D hd_opts(1) + opts['backing']['backing'] =3D hd_opts(2) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # hd2 <- hd0 + result =3D self.vm.qmp('block-stream', conv_keys =3D True, job_id = =3D 'stream0', + device =3D 'hd0', base_node =3D 'hd2', speed = =3D 512 * 1024) + self.assert_qmp(result, 'return', {}) + + # We can't remove hd2 while the stream job is ongoing + opts['backing']['backing'] =3D None + self.reopen(opts, {}, "Cannot change 'backing' link from 'hd1' to = 'hd2'") + + # We can't remove hd1 while the stream job is ongoing + opts['backing'] =3D None + self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to = 'hd1'") + + self.wait_until_completed(drive =3D 'stream0') + + # Reopen the chain during a block-stream job (from hd2 to hd1) + def test_block_stream_4(self): + # hd2 <- hd1 <- hd0 + opts =3D hd_opts(0) + opts['backing'] =3D hd_opts(1) + opts['backing']['backing'] =3D hd_opts(2) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + # hd1 <- hd0 + result =3D self.vm.qmp('block-stream', conv_keys =3D True, job_id = =3D 'stream0', + device =3D 'hd1', speed =3D 512 * 1024) + self.assert_qmp(result, 'return', {}) + + # We can't reopen with the original options because that would + # make hd1 read-only and block-stream requires it to be read-write + self.reopen(opts, {}, "Can't set node 'hd1' to r/o with copy-on-re= ad enabled") + + # We can't remove hd2 while the stream job is ongoing + opts['backing']['backing'] =3D None + self.reopen(opts, {'backing.read-only': False}, "Cannot change 'ba= cking' link from 'hd1' to 'hd2'") + + # We can detach hd1 from hd0 because it doesn't affect the stream = job + opts['backing'] =3D None + self.reopen(opts) + + self.wait_until_completed(drive =3D 'stream0') + + # Reopen the chain during a block-commit job (from hd0 to hd2) + def test_block_commit_1(self): + # hd2 <- hd1 <- hd0 + opts =3D hd_opts(0) + opts['backing'] =3D hd_opts(1) + opts['backing']['backing'] =3D hd_opts(2) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + result =3D self.vm.qmp('block-commit', conv_keys =3D True, job_id = =3D 'commit0', + device =3D 'hd0', speed =3D 1024 * 1024) + self.assert_qmp(result, 'return', {}) + + # We can't remove hd2 while the commit job is ongoing + opts['backing']['backing'] =3D None + self.reopen(opts, {}, "Cannot change 'backing' link from 'hd1' to = 'hd2'") + + # We can't remove hd1 while the commit job is ongoing + opts['backing'] =3D None + self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to = 'hd1'") + + event =3D self.vm.event_wait(name=3D'BLOCK_JOB_READY') + self.assert_qmp(event, 'data/device', 'commit0') + self.assert_qmp(event, 'data/type', 'commit') + self.assert_qmp_absent(event, 'data/error') + + result =3D self.vm.qmp('block-job-complete', device=3D'commit0') + self.assert_qmp(result, 'return', {}) + + self.wait_until_completed(drive =3D 'commit0') + + # Reopen the chain during a block-commit job (from hd1 to hd2) + def test_block_commit_2(self): + # hd2 <- hd1 <- hd0 + opts =3D hd_opts(0) + opts['backing'] =3D hd_opts(1) + opts['backing']['backing'] =3D hd_opts(2) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + result =3D self.vm.qmp('block-commit', conv_keys =3D True, job_id = =3D 'commit0', + device =3D 'hd0', top_node =3D 'hd1', speed = =3D 1024 * 1024) + self.assert_qmp(result, 'return', {}) + + # We can't remove hd2 while the commit job is ongoing + opts['backing']['backing'] =3D None + self.reopen(opts, {}, "Cannot change the option 'backing.driver'") + + # We can't remove hd1 while the commit job is ongoing + opts['backing'] =3D None + self.reopen(opts, {}, "Cannot change backing link if 'hd0' has an = implicit backing file") + + # hd2 <- hd0 + self.wait_until_completed(drive =3D 'commit0') + + self.assert_qmp(self.get_node('hd0'), 'ro', False) + self.assertEqual(self.get_node('hd1'), None) + self.assert_qmp(self.get_node('hd2'), 'ro', True) + + # We don't allow setting a backing file that uses a different AioConte= xt + def test_iothreads(self): + opts =3D hd_opts(0) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts) + self.assert_qmp(result, 'return', {}) + + opts2 =3D hd_opts(2) + result =3D self.vm.qmp('blockdev-add', conv_keys =3D False, **opts= 2) + self.assert_qmp(result, 'return', {}) + + result =3D self.vm.qmp('object-add', qom_type=3D'iothread', id=3D'= iothread0') + self.assert_qmp(result, 'return', {}) + + result =3D self.vm.qmp('object-add', qom_type=3D'iothread', id=3D'= iothread1') + self.assert_qmp(result, 'return', {}) + + result =3D self.vm.qmp('x-blockdev-set-iothread', node_name=3D'hd0= ', iothread=3D'iothread0') + self.assert_qmp(result, 'return', {}) + + self.reopen(opts, {'backing': 'hd2'}, "Cannot use a new backing fi= le with a different AioContext") + + result =3D self.vm.qmp('x-blockdev-set-iothread', node_name=3D'hd2= ', iothread=3D'iothread1') + self.assert_qmp(result, 'return', {}) + + self.reopen(opts, {'backing': 'hd2'}, "Cannot use a new backing fi= le with a different AioContext") + + result =3D self.vm.qmp('x-blockdev-set-iothread', node_name=3D'hd2= ', iothread=3D'iothread0') + self.assert_qmp(result, 'return', {}) + + self.reopen(opts, {'backing': 'hd2'}) + +if __name__ =3D=3D '__main__': + iotests.main(supported_fmts=3D["qcow2"]) diff --git a/tests/qemu-iotests/245.out b/tests/qemu-iotests/245.out new file mode 100644 index 0000000000..71009c239f --- /dev/null +++ b/tests/qemu-iotests/245.out @@ -0,0 +1,5 @@ +.................. +---------------------------------------------------------------------- +Ran 18 tests + +OK diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index 36100b803c..66ab708d4a 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -243,3 +243,4 @@ 242 rw auto quick 243 rw auto quick 244 rw auto quick +245 rw auto --=20 2.11.0