diff mbox series

[v4,31/45] qapi: block: add blockdev-add transaction action

Message ID 20220329204107.411011-32-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:40 p.m. UTC
Use new flag to avoid permission updates where possible during
blockdev_add, so that a bunch of add/del (and soon, new 'replace')
command may be done before actual permission update to avoid
intermediate permission conflicts.

Signed-off-by: Vladimir Sementsov-Ogievskiy <v.sementsov-og@mail.ru>
---
 blockdev.c            | 55 ++++++++++++++++++++++++++++++++++++++++---
 qapi/transaction.json | 10 +++++++-
 2 files changed, 61 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/blockdev.c b/blockdev.c
index 3569b0e6ee..d95e1e6922 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -63,6 +63,9 @@ 
 #include "qemu/main-loop.h"
 #include "qemu/throttle-options.h"
 
+static int blockdev_add(BlockdevOptions *options, GSList **refresh_list,
+                        Transaction *tran, Error **errp);
+
 QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
     QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
 
@@ -2196,6 +2199,10 @@  static void transaction_action(TransactionAction *act, JobTxn *block_job_txn,
         blockdev_del(act->u.blockdev_del.data->node_name,
                      refresh_list, tran, errp);
         return;
+    case TRANSACTION_ACTION_KIND_BLOCKDEV_ADD:
+        blockdev_add(act->u.blockdev_add.data,
+                     refresh_list, tran, errp);
+        return;
     /*
      * Where are transactions for MIRROR, COMMIT and STREAM?
      * Although these blockjobs use transaction callbacks like the backup job,
@@ -2259,7 +2266,8 @@  void qmp_transaction(TransactionActionList *actions,
         TransactionActionKind type = act->value->type;
 
         if (refresh_list &&
-            type != TRANSACTION_ACTION_KIND_BLOCKDEV_DEL)
+            type != TRANSACTION_ACTION_KIND_BLOCKDEV_DEL &&
+            type != TRANSACTION_ACTION_KIND_BLOCKDEV_ADD)
         {
             ret = bdrv_list_refresh_perms(refresh_list, NULL, tran, errp);
             if (ret < 0) {
@@ -3430,7 +3438,21 @@  out:
     aio_context_release(aio_context);
 }
 
-void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
+static void blockdev_add_abort(void *opaque)
+{
+    BlockDriverState *bs = opaque;
+
+    QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
+    bdrv_unref(bs);
+}
+
+TransactionActionDrv blockdev_add_drv = {
+    .abort = blockdev_add_abort,
+};
+
+/* Caller is responsible to refresh permissions */
+static int blockdev_add(BlockdevOptions *options, GSList **refresh_list,
+                        Transaction *tran, Error **errp)
 {
     BlockDriverState *bs;
     QObject *obj;
@@ -3448,15 +3470,42 @@  void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
         goto fail;
     }
 
-    bs = bds_tree_init(qdict, 0, errp);
+    bs = bds_tree_init(qdict, BDRV_O_NOPERM, errp);
     if (!bs) {
         goto fail;
     }
 
+    *refresh_list = g_slist_prepend(*refresh_list, bs);
+    tran_add(tran, &blockdev_add_drv, bs);
+
     bdrv_set_monitor_owned(bs);
 
+    visit_free(v);
+    return 0;
+
 fail:
     visit_free(v);
+    return -EINVAL;
+}
+
+void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
+{
+    int ret;
+    Transaction *tran = tran_new();
+    g_autoptr(GSList) refresh_list = NULL;
+
+    ret = blockdev_add(options, &refresh_list, tran, errp);
+    if (ret < 0) {
+        goto out;
+    }
+
+    ret = bdrv_list_refresh_perms(refresh_list, NULL, tran, errp);
+    if (ret < 0) {
+        goto out;
+    }
+
+out:
+    tran_finalize(tran, ret);
 }
 
 void qmp_blockdev_reopen(BlockdevOptionsList *reopen_list, Error **errp)
diff --git a/qapi/transaction.json b/qapi/transaction.json
index ea20df770c..000dd16bb7 100644
--- a/qapi/transaction.json
+++ b/qapi/transaction.json
@@ -67,7 +67,7 @@ 
             'block-dirty-bitmap-disable', 'block-dirty-bitmap-merge',
             'blockdev-backup', 'blockdev-snapshot',
             'blockdev-snapshot-internal-sync', 'blockdev-snapshot-sync',
-            'blockdev-del',
+            'blockdev-del', 'blockdev-add',
             { 'name': 'drive-backup', 'features': [ 'deprecated' ] } ] }
 
 ##
@@ -150,6 +150,13 @@ 
 { 'struct': 'BlockdevDelWrapper',
   'data': { 'data': 'BlockdevDel' } }
 
+##
+# @BlockdevAddWrapper:
+#
+# Since: 7.1
+##
+{ 'struct': 'BlockdevAddWrapper',
+  'data': { 'data': 'BlockdevOptions' } }
 
 ##
 # @TransactionAction:
@@ -175,6 +182,7 @@ 
        'blockdev-snapshot-internal-sync': 'BlockdevSnapshotInternalWrapper',
        'blockdev-snapshot-sync': 'BlockdevSnapshotSyncWrapper',
        'blockdev-del': 'BlockdevDelWrapper',
+       'blockdev-add': 'BlockdevAddWrapper',
        'drive-backup': 'DriveBackupWrapper'
    } }