From nobody Mon Feb 9 07:22:28 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=quarantine dis=quarantine) header.from=virtuozzo.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 16279305522581015.2691275038022; Mon, 2 Aug 2021 11:55:52 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-78-Sq9nWVvvPUqOYhByQQqWog-1; Mon, 02 Aug 2021 14:55:44 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F1ACB1006C98; Mon, 2 Aug 2021 18:55:38 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C832E17C0B; Mon, 2 Aug 2021 18:55:38 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 928034A70A; Mon, 2 Aug 2021 18:55:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 172It2Ac006424 for ; Mon, 2 Aug 2021 14:55:02 -0400 Received: by smtp.corp.redhat.com (Postfix) id 428C5114B2F1; Mon, 2 Aug 2021 18:55:02 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast02.extmail.prod.ext.rdu2.redhat.com [10.11.55.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3D092114B2EE for ; Mon, 2 Aug 2021 18:54:57 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A12CF800B28 for ; Mon, 2 Aug 2021 18:54:57 +0000 (UTC) Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-eopbgr70090.outbound.protection.outlook.com [40.107.7.90]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-515-RpgQVRFEPG-MjXOkRzoIoQ-5; Mon, 02 Aug 2021 14:54:53 -0400 Received: from AM7PR08MB5494.eurprd08.prod.outlook.com (2603:10a6:20b:dc::15) by AM6PR08MB4278.eurprd08.prod.outlook.com (2603:10a6:20b:71::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18; Mon, 2 Aug 2021 18:54:49 +0000 Received: from AM7PR08MB5494.eurprd08.prod.outlook.com ([fe80::44b9:68ac:bdc7:e23c]) by AM7PR08MB5494.eurprd08.prod.outlook.com ([fe80::44b9:68ac:bdc7:e23c%8]) with mapi id 15.20.4373.026; Mon, 2 Aug 2021 18:54:49 +0000 Received: from localhost.localdomain (185.215.60.211) by HE1PR05CA0240.eurprd05.prod.outlook.com (2603:10a6:3:fb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18 via Frontend Transport; Mon, 2 Aug 2021 18:54:47 +0000 X-MC-Unique: Sq9nWVvvPUqOYhByQQqWog-1 X-MC-Unique: RpgQVRFEPG-MjXOkRzoIoQ-5 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH 8/8] qapi: add blockdev-replace command Date: Mon, 2 Aug 2021 21:54:16 +0300 Message-ID: <20210802185416.50877-9-vsementsov@virtuozzo.com> In-Reply-To: <20210802185416.50877-1-vsementsov@virtuozzo.com> References: <20210802185416.50877-1-vsementsov@virtuozzo.com> X-ClientProxiedBy: HE1PR05CA0240.eurprd05.prod.outlook.com (2603:10a6:3:fb::16) To AM7PR08MB5494.eurprd08.prod.outlook.com (2603:10a6:20b:dc::15) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 744c51ee-23db-4818-ebc9-08d955e7010e X-MS-TrafficTypeDiagnostic: AM6PR08MB4278: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6108 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0 X-Microsoft-Antispam-Message-Info: liuBp3cPwGarTRKSf+7WxYnt0MUTuCbY/QFz2xtXrfRMblWRWbjmtEj0dlZwVRPhMps6Sa33gcR/ySaxX7SXbf08XHE9Ff8s7TqnBYpTVn4JuZjQDtmlTieSp/Tw5OK05Sjt9cXZxsoS040pD6D6sbdbDQ0oduhq2YibRLNF6nptKKAJ/XbbSiEYy3M1vVWryKooiEIIO52s7otlgk+5/s+4Cfs/ump2+dbVIpfQfrSxWddlO1t2iiojQH55ep2DHvWHeR7BfXYfN9bMWR2yR97dDGCXGg6tPHcm3EfbMk9jFW+H/B81vU9BtG37Y5+wVrMkj1LQ5AdpUIPrYAoIh6gPuWvlNTC0W6Z/1WhurUkgu+nrSPe0/RZUZV3vkAfGdSoil+SsnWFh+Vdkavvm/pVu7SHaFzi0CYGvlHr+jileaAPBgxm9Owbx92BvhSC/tf0E8o2T5B4aSl/0oPTf2sgDRCYplYr3JRzWnB/peC5GnqwAsi+9jaKm9U54MAyxDO3zKw96VZwQpiu4gCMvBHxMP/5sCArESIegieLnlWT/FT/1G0gOTuCH5JGTsZG6FTx7NTG3iku7seHYBwURdufmhfoqTjLVcidznHa+MElhdw/UX8UUidhWX1vidz0lE4RKnP15W8U+gae//P57Zjc/lswewW+/lp2Y+9X4cIqa9XOJEkIuyIpE1VK/ygkSwsDsejDB4Kr76Vowh43Pow== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM7PR08MB5494.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(346002)(376002)(39840400004)(136003)(396003)(366004)(186003)(6916009)(52116002)(1076003)(83380400001)(8676002)(6512007)(38350700002)(38100700002)(2906002)(7416002)(6506007)(2616005)(478600001)(6666004)(36756003)(5660300002)(8936002)(956004)(4326008)(86362001)(26005)(316002)(6486002)(66946007)(66556008)(66476007); DIR:OUT; SFP:1102 X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?hQstUbTND79yJQIZCvORcxT2qptP8pWqdtshlzYFsUjerFVuVPNX7Rq3ssJH?= =?us-ascii?Q?IaiB1DhsYRQuY0t5bJUgGteeSNjyvKamPOOz06bUNtYPsXzeH88WVuvnVWr4?= =?us-ascii?Q?I3KmjUZ0/0rQQmcdfw2NfAKMXgmhdraGGbKhQzHb4TnZHjydFaci88dIdtSu?= =?us-ascii?Q?lXD0VngJBa+u24bs3OZoc2Z+BenbLaqQzjND5eErZwhhiSqvYIDPUDuT3md6?= =?us-ascii?Q?AIm6QmbOM4/6lBicwP4jgPxOBjbgtfc5EFYKBdC9kQpsZOl5+SPUt0NM83Ey?= =?us-ascii?Q?LHSgnXJY6/QjuIsJ0EiIJTuWXZ1lC1J7BN3L4tj/9PvJ3cWpn8y8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O6M6I13E01a8USEKUt1GOnjnrFNX?= =?us-ascii?Q?l9tfj8d5c5JVoq+EtYeP+v4qs9DPrAPyP8JjoJrLRT3TuC9mSjShW/e7C9gM?= =?us-ascii?Q?7VxqWalCpJhdbarjKWp50TdQ?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 744c51ee-23db-4818-ebc9-08d955e7010e X-MS-Exchange-CrossTenant-AuthSource: AM7PR08MB5494.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Aug 2021 18:54:49.4850 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Mhfll8Q729NFmwyxYFb8TErY1chtBJO6cbPLtp+LV/kucs0PgGH3Mpg2stTZ7yfRhXWn+XrTg51wy1cNvoh9UwCIoDkT++0Cm5hm5+P2KlQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB4278 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 172It2Ac006424 X-loop: libvir-list@redhat.com Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, ehabkost@redhat.com, libvir-list@redhat.com, dim@virtuozzo.com, igor@virtuozzo.com, qemu-devel@nongnu.org, armbru@redhat.com, pkrempa@redhat.com, yur@virtuozzo.com, nshirokovskiy@virtuozzo.com, stefanha@redhat.com, den@openvz.org, pbonzini@redhat.com, mreitz@redhat.com, eblake@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1627930552978100001 Content-Type: text/plain; charset="utf-8" Add command that can add and remove filters. Key points of functionality: What the command does is simply replace some BdrvChild.bs by some other nodes. The tricky thing is selecting there BdrvChild objects. To be able to select any kind of BdrvChild we use a generic parent_id, which may be a node-name, or qdev id or block export id. In future we may support block jobs. Any kind of ambiguity leads to error. If we have both device named device0 and block export named device0 and they both point to same BDS, user can't replace root child of one of these parents. So, to be able to do replacements, user should avoid duplicating names in different parent namespaces. So, command allows to replace any single child in the graph. On the other hand we want to realize a kind of bdrv_replace_node(), which works well when we want to replace all parents of some node. For this kind of task @parents-mode argument implemented. Signed-off-by: Vladimir Sementsov-Ogievskiy --- qapi/block-core.json | 78 +++++++++++++++++++++++++++++++++++++++++ block.c | 82 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) diff --git a/qapi/block-core.json b/qapi/block-core.json index 675d8265eb..8059b96341 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -5433,3 +5433,81 @@ { 'command': 'blockdev-snapshot-delete-internal-sync', 'data': { 'device': 'str', '*id': 'str', '*name': 'str'}, 'returns': 'SnapshotInfo' } + +## +# @BlockdevReplaceParentsMode: +# +# Alternative (to directly set @parent) way to chose parents in +# @blockdev-replace +# +# @exactly-one: Exactly one parent should match a condition, otherwise +# @blockdev-replace fails. +# +# @all: All matching parents are taken into account. If replacing lead +# to loops in block graph, @blockdev-replace fails. +# +# @auto: Same as @all, but automatically skip replacing parents if it +# leads to loops in block graph. +# +# Since: 6.2 +## +{ 'enum': 'BlockdevReplaceParentsMode', + 'data': ['exactly-one', 'all', 'auto'] } + +## +# @BlockdevReplace: +# +# Declaration of one replacement. +# +# @parent: id of parent. It may be qdev or block export or simple +# node-name. If id is ambiguous (for example node-name of +# some BDS equals to block export name), blockdev-replace +# fails. If not specified, blockdev-replace goes through +# @parents-mode scenario, see below. Note, that @parent and +# @parents-mode can't be specified simultaneously. +# If @parent is specified, only one edge is selected. If +# several edges match the condition, blockdev-replace fails. +# +# @edge: name of the child. If omitted, any child name matches. +# +# @child: node-name of the child. If omitted, any child matches. +# Must be present if @parent is not specified. +# +# @parents-mode: declares how to select edge (or edges) when @parent +# is omitted. Default is 'one'. +# +# Since: 6.2 +# +# Examples: +# +# 1. Change root node of some device. +# +# Note, that @edge name is omitted, as +# devices always has only one child. As well, no need in specifying +# old @child. +# +# -> { "parent": "device0", "new-child": "some-node-name" } +# +# 2. Insert copy-before-write filter. +# +# Assume, after blockdev-add we have block-node 'source', with several +# writing parents and one copy-before-write 'filter' parent. And we want +# to actually insert the filter. We do: +# +# -> { "child": "source", "parent-mode": "auto", "new-child": "filter" } +# +# All parents of source would be switched to 'filter' node, except for +# 'filter' node itself (otherwise, it will make a loop in block-graph). +## +{ 'struct': 'BlockdevReplace', + 'data': { '*parent': 'str', '*edge': 'str', '*child': 'str', + '*parents-mode': 'BlockdevReplaceParentsMode', + 'new-child': 'str' } } + +## +# @blockdev-replace: +# +# Do one or several replacements transactionally. +## +{ 'command': 'blockdev-replace', + 'data': { 'replacements': ['BlockdevReplace'] } } diff --git a/block.c b/block.c index ae8c8c4032..3bcb3152f1 100644 --- a/block.c +++ b/block.c @@ -41,6 +41,7 @@ #include "qapi/qmp/qnull.h" #include "qapi/qmp/qstring.h" #include "qapi/qobject-output-visitor.h" +#include "qapi/qapi-commands-block.h" #include "qapi/qapi-visit-block-core.h" #include "sysemu/block-backend.h" #include "qemu/notify.h" @@ -7717,6 +7718,87 @@ BlockDriverState *bdrv_backing_chain_next(BlockDrive= rState *bs) return bdrv_skip_filters(bdrv_cow_bs(bdrv_skip_filters(bs))); } =20 +void qmp_blockdev_replace(BlockdevReplaceList *list, Error **errp) +{ + int ret; + Transaction *tran =3D tran_new(); + g_autoptr(GHashTable) found =3D NULL; + g_autoptr(GSList) refresh_list =3D NULL; + g_autoptr(GSList) touched_list =3D NULL; + GSList *x; + + for ( ; list; list =3D list->next) { + BlockdevReplace *repl =3D list->value; + BlockDriverState *child_bs =3D NULL, *new_child_bs; + BlockdevReplaceParentsMode mode; + BdrvChild *child; + + if (repl->has_child) { + child_bs =3D bdrv_find_node(repl->child); + if (!child_bs) { + error_setg(errp, "Node '%s' not found", repl->child); + goto fail; + } + } + + new_child_bs =3D bdrv_find_node(repl->new_child); + if (!new_child_bs) { + error_setg(errp, "Node '%s' not found", repl->new_child); + goto fail; + } + + if (repl->has_parent) { + if (repl->has_parents_mode) { + error_setg(errp, "parent and parents-mode field must " + "not be set simultaneously."); + goto fail; + } + + child =3D block_find_child(repl->parent, repl->edge, child_bs,= errp); + if (!child) { + goto fail; + } + + touched_list =3D g_slist_prepend(touched_list, child->bs); + touched_list =3D g_slist_prepend(touched_list, new_child_bs); + bdrv_replace_child_tran(child, new_child_bs, tran); + continue; + } + + if (!repl->has_child) { + error_setg(errp, "At least one of parent and child fields " + "should be specified."); + goto fail; + } + + mode =3D repl->has_parents_mode ? repl->parents_mode : + BLOCKDEV_REPLACE_PARENTS_MODE_EXACTLY_ONE; + + touched_list =3D g_slist_prepend(touched_list, child_bs); + touched_list =3D g_slist_prepend(touched_list, new_child_bs); + ret =3D bdrv_replace_node_noperm(child_bs, new_child_bs, + mode =3D=3D BLOCKDEV_REPLACE_PARENTS_MODE_AUTO, + repl->edge, + mode =3D=3D BLOCKDEV_REPLACE_PARENTS_MODE_EXACTLY_ONE, + tran, errp); + if (ret < 0) { + goto fail; + } + } + + for (x =3D touched_list; x; x =3D x->next) { + refresh_list =3D bdrv_topological_dfs(refresh_list, found, x->data= ); + } + + ret =3D bdrv_list_refresh_perms(refresh_list, NULL, tran, errp); + + tran_finalize(tran, ret); + return; + +fail: + tran_abort(tran); +} + static void block_c_init(void) { block_parent_class_register(&block_parent_bdrv); --=20 2.29.2