From nobody Thu Dec 18 17:55:27 2025 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552415373055649.3968196956511; Tue, 12 Mar 2019 11:29:33 -0700 (PDT) Received: from localhost ([127.0.0.1]:57428 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3m9Y-0001ar-RG for importer@patchew.org; Tue, 12 Mar 2019 14:29:28 -0400 Received: from eggs.gnu.org ([209.51.188.92]:56747) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3lF0-0001B0-Or for qemu-devel@nongnu.org; Tue, 12 Mar 2019 13:31:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3lEy-0000FO-T7 for qemu-devel@nongnu.org; Tue, 12 Mar 2019 13:31:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41540) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h3lEt-00008z-G0; Tue, 12 Mar 2019 13:30:55 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A692688287; Tue, 12 Mar 2019 17:30:54 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-117-52.ams2.redhat.com [10.36.117.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id 90A3260C10; Tue, 12 Mar 2019 17:30:53 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 12 Mar 2019 18:30:13 +0100 Message-Id: <20190312173025.3843-17-kwolf@redhat.com> In-Reply-To: <20190312173025.3843-1-kwolf@redhat.com> References: <20190312173025.3843-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 12 Mar 2019 17:30:54 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 16/28] 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: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Alberto Garcia 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 Signed-off-by: Kevin Wolf --- include/block/block.h | 5 +++ include/block/block_int.h | 6 ++++ block.c | 76 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) 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; }; 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; @@ -3932,6 +3943,63 @@ BlockDriverState *bdrv_find_base(BlockDriverState *b= s) return bdrv_find_overlay(bs, NULL); } =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. --=20 2.20.1