All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, ehabkost@redhat.com, berrange@redhat.com,
	pbonzini@redhat.com, armbru@redhat.com, eblake@redhat.com,
	mreitz@redhat.com, kwolf@redhat.com, vsementsov@virtuozzo.com,
	den@openvz.org, nshirokovskiy@virtuozzo.com, yur@virtuozzo.com,
	dim@virtuozzo.com, igor@virtuozzo.com, pkrempa@redhat.com,
	libvir-list@redhat.com, stefanha@redhat.com
Subject: [PATCH 8/8] qapi: add blockdev-replace command
Date: Mon,  2 Aug 2021 21:54:16 +0300	[thread overview]
Message-ID: <20210802185416.50877-9-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20210802185416.50877-1-vsementsov@virtuozzo.com>

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 <vsementsov@virtuozzo.com>
---
 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(BlockDriverState *bs)
     return bdrv_skip_filters(bdrv_cow_bs(bdrv_skip_filters(bs)));
 }
 
+void qmp_blockdev_replace(BlockdevReplaceList *list, Error **errp)
+{
+    int ret;
+    Transaction *tran = tran_new();
+    g_autoptr(GHashTable) found = NULL;
+    g_autoptr(GSList) refresh_list = NULL;
+    g_autoptr(GSList) touched_list = NULL;
+    GSList *x;
+
+    for ( ; list; list = list->next) {
+        BlockdevReplace *repl = list->value;
+        BlockDriverState *child_bs = NULL, *new_child_bs;
+        BlockdevReplaceParentsMode mode;
+        BdrvChild *child;
+
+        if (repl->has_child) {
+            child_bs = bdrv_find_node(repl->child);
+            if (!child_bs) {
+                error_setg(errp, "Node '%s' not found", repl->child);
+                goto fail;
+            }
+        }
+
+        new_child_bs = 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 = block_find_child(repl->parent, repl->edge, child_bs, errp);
+            if (!child) {
+                goto fail;
+            }
+
+            touched_list = g_slist_prepend(touched_list, child->bs);
+            touched_list = 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 = repl->has_parents_mode ? repl->parents_mode :
+            BLOCKDEV_REPLACE_PARENTS_MODE_EXACTLY_ONE;
+
+        touched_list = g_slist_prepend(touched_list, child_bs);
+        touched_list = g_slist_prepend(touched_list, new_child_bs);
+        ret = bdrv_replace_node_noperm(child_bs, new_child_bs,
+            mode == BLOCKDEV_REPLACE_PARENTS_MODE_AUTO,
+            repl->edge,
+            mode == BLOCKDEV_REPLACE_PARENTS_MODE_EXACTLY_ONE,
+            tran, errp);
+        if (ret < 0) {
+            goto fail;
+        }
+    }
+
+    for (x = touched_list; x; x = x->next) {
+        refresh_list = bdrv_topological_dfs(refresh_list, found, x->data);
+    }
+
+    ret = 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);
-- 
2.29.2



  parent reply	other threads:[~2021-08-02 19:03 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-02 18:54 [PATCH RFC 0/8] blockdev-replace Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 1/8] block-backend: blk_root(): drop const specifier on return type Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 2/8] block: add BlockParentClass class Vladimir Sementsov-Ogievskiy
2021-09-16  8:34   ` Markus Armbruster
2021-09-16 10:12     ` Vladimir Sementsov-Ogievskiy
2021-09-20  5:28   ` Markus Armbruster
2021-08-02 18:54 ` [PATCH 3/8] block: realize BlockParentClass for BlockDriverState Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 4/8] block/export: realize BlockParentClass functionality Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 5/8] qdev: improve find_device_state() to distinguish simple not found case Vladimir Sementsov-Ogievskiy
2021-09-16 10:48   ` Markus Armbruster
2021-09-16 12:54     ` Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 6/8] qdev: realize BlockParentClass Vladimir Sementsov-Ogievskiy
2021-09-20  6:08   ` Markus Armbruster
2021-08-02 18:54 ` [PATCH 7/8] block: improve bdrv_replace_node_noperm() Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` Vladimir Sementsov-Ogievskiy [this message]
2021-09-20  6:44   ` [PATCH 8/8] qapi: add blockdev-replace command Markus Armbruster
2021-09-20 10:02     ` Vladimir Sementsov-Ogievskiy
2021-09-23 10:09       ` Markus Armbruster
2021-09-23 11:54         ` Vladimir Sementsov-Ogievskiy
2021-09-20 11:25   ` Vladimir Sementsov-Ogievskiy
2021-09-02  9:28 ` [PATCH RFC 0/8] blockdev-replace Vladimir Sementsov-Ogievskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210802185416.50877-9-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=den@openvz.org \
    --cc=dim@virtuozzo.com \
    --cc=eblake@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=igor@virtuozzo.com \
    --cc=kwolf@redhat.com \
    --cc=libvir-list@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=nshirokovskiy@virtuozzo.com \
    --cc=pbonzini@redhat.com \
    --cc=pkrempa@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=yur@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.