diff mbox series

[v4,38/45] qapi: add x-blockdev-replace transaction action

Message ID 20220329204107.411011-39-v.sementsov-og@mail.ru (mailing list archive)
State New, archived
Headers show
Series Transactional block-graph modifying API | expand

Commit Message

Vladimir Sementsov-Ogievskiy March 29, 2022, 8:41 p.m. UTC
Support blockdev-replace in a transaction.

Signed-off-by: Vladimir Sementsov-Ogievskiy <v.sementsov-og@mail.ru>
---
 block.c               |  4 ++--
 blockdev.c            | 29 ++++++++++++++++++++++++-----
 include/block/block.h |  2 ++
 qapi/transaction.json | 15 ++++++++++++++-
 4 files changed, 42 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/block.c b/block.c
index 5717115523..a51be66e0a 100644
--- a/block.c
+++ b/block.c
@@ -2302,8 +2302,8 @@  static TransactionActionDrv bdrv_replace_child_drv = {
 };
 
 /* Caller is responsible to refresh permissions in @refresh_list */
-static void bdrv_replace_child_tran(BdrvChild *child, BlockDriverState *new_bs,
-                                    GSList **refresh_list, Transaction *tran)
+void bdrv_replace_child_tran(BdrvChild *child, BlockDriverState *new_bs,
+                             GSList **refresh_list, Transaction *tran)
 {
     BdrvReplaceChildState *s = g_new(BdrvReplaceChildState, 1);
     *s = (BdrvReplaceChildState) {
diff --git a/blockdev.c b/blockdev.c
index c1ffdbcfa4..2920c33421 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -2147,8 +2147,9 @@  static void abort_commit(void *opaque)
 static int blockdev_del(const char *node_name, GSList **detached,
                         Transaction *tran, Error **errp);
 
-static int blockdev_replace(BlockdevReplace *repl, Transaction *tran,
-                            Error **errp)
+/* Caller is responsible to update permission of nodes added to @update_list */
+static int blockdev_replace(BlockdevReplace *repl, GSList **refresh_list,
+                            Transaction *tran, Error **errp)
 {
     BdrvChild *child = NULL;
     BlockDriverState *new_child_bs;
@@ -2201,14 +2202,27 @@  static int blockdev_replace(BlockdevReplace *repl, Transaction *tran,
         return -EINVAL;
     }
 
-    return bdrv_replace_child_bs(child, new_child_bs, tran, errp);
+    bdrv_replace_child_tran(child, new_child_bs, refresh_list, tran);
+    return 0;
 }
 
 void qmp_x_blockdev_replace(BlockdevReplace *repl, Error **errp)
 {
+    int ret;
     Transaction *tran = tran_new();
-    int ret = blockdev_replace(repl, tran, errp);
+    g_autoptr(GSList) update_list = NULL;
+
+    ret = blockdev_replace(repl, &update_list, tran, errp);
+    if (ret < 0) {
+        goto out;
+    }
 
+    ret = bdrv_list_refresh_perms(update_list, NULL, tran, errp);
+    if (ret < 0) {
+        goto out;
+    }
+
+out:
     tran_finalize(tran, ret);
 }
 
@@ -2268,6 +2282,10 @@  static void transaction_action(TransactionAction *act, JobTxn *block_job_txn,
         blockdev_add(act->u.blockdev_add.data,
                      refresh_list, tran, errp);
         return;
+    case TRANSACTION_ACTION_KIND_X_BLOCKDEV_REPLACE:
+        blockdev_replace(act->u.x_blockdev_replace.data,
+                         refresh_list, tran, errp);
+        return;
     /*
      * Where are transactions for MIRROR, COMMIT and STREAM?
      * Although these blockjobs use transaction callbacks like the backup job,
@@ -2332,7 +2350,8 @@  void qmp_transaction(TransactionActionList *actions,
 
         if (refresh_list &&
             type != TRANSACTION_ACTION_KIND_BLOCKDEV_DEL &&
-            type != TRANSACTION_ACTION_KIND_BLOCKDEV_ADD)
+            type != TRANSACTION_ACTION_KIND_BLOCKDEV_ADD &&
+            type != TRANSACTION_ACTION_KIND_X_BLOCKDEV_REPLACE)
         {
             ret = bdrv_list_refresh_perms(refresh_list, NULL, tran, errp);
             if (ret < 0) {
diff --git a/include/block/block.h b/include/block/block.h
index 182e87ce77..59031c8b03 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -406,6 +406,8 @@  int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,
                       Error **errp);
 int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs,
                           Transaction *tran, Error **errp);
+void bdrv_replace_child_tran(BdrvChild *child, BlockDriverState *new_bs,
+                             GSList **refresh_list, Transaction *tran);
 BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *node_options,
                                    int flags, Error **errp);
 int bdrv_drop_filter(BlockDriverState *bs, Error **errp);
diff --git a/qapi/transaction.json b/qapi/transaction.json
index 000dd16bb7..61cb2d2312 100644
--- a/qapi/transaction.json
+++ b/qapi/transaction.json
@@ -54,10 +54,13 @@ 
 # @blockdev-snapshot-sync: since 1.1
 # @drive-backup: Since 1.6
 # @blockdev-del: since 7.1
+# @blockdev-add: since 7.1
+# @x-blockdev-replace: since 7.1
 #
 # Features:
 # @deprecated: Member @drive-backup is deprecated.  Use member
 #              @blockdev-backup instead.
+# @unstable: Member @x-blockdev-replace is experimental
 #
 # Since: 1.1
 ##
@@ -68,6 +71,7 @@ 
             'blockdev-backup', 'blockdev-snapshot',
             'blockdev-snapshot-internal-sync', 'blockdev-snapshot-sync',
             'blockdev-del', 'blockdev-add',
+            { 'name': 'x-blockdev-replace', 'features': [ 'unstable' ] },
             { 'name': 'drive-backup', 'features': [ 'deprecated' ] } ] }
 
 ##
@@ -158,6 +162,14 @@ 
 { 'struct': 'BlockdevAddWrapper',
   'data': { 'data': 'BlockdevOptions' } }
 
+##
+# @BlockdevReplaceWrapper:
+#
+# Since: 7.1
+##
+{ 'struct': 'BlockdevReplaceWrapper',
+  'data': { 'data': 'BlockdevReplace' } }
+
 ##
 # @TransactionAction:
 #
@@ -183,7 +195,8 @@ 
        'blockdev-snapshot-sync': 'BlockdevSnapshotSyncWrapper',
        'blockdev-del': 'BlockdevDelWrapper',
        'blockdev-add': 'BlockdevAddWrapper',
-       'drive-backup': 'DriveBackupWrapper'
+       'drive-backup': 'DriveBackupWrapper',
+       'x-blockdev-replace': 'BlockdevReplaceWrapper'
    } }
 
 ##