cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
218e99
From 2b05ad88980acb91357598f5ed041ec33aec0de6 Mon Sep 17 00:00:00 2001
218e99
From: Kevin Wolf <kwolf@redhat.com>
218e99
Date: Mon, 9 Sep 2013 14:27:56 +0200
218e99
Subject: [PATCH 05/38] block: make all steps in qmp_transaction() as callback
218e99
218e99
RH-Author: Kevin Wolf <kwolf@redhat.com>
218e99
Message-id: <1378736903-18489-6-git-send-email-kwolf@redhat.com>
218e99
Patchwork-id: 54192
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH 05/32] block: make all steps in qmp_transaction() as callback
218e99
Bugzilla: 1005818
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Max Reitz <mreitz@redhat.com>
218e99
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
218e99
From: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
218e99
218e99
Bugzilla: 1005818
218e99
218e99
Make it easier to add other operations to qmp_transaction() by using
218e99
callbacks, with external snapshots serving as an example implementation
218e99
of the callbacks.
218e99
218e99
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
218e99
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
218e99
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
Reviewed-by: Eric Blake <eblake@redhat.com>
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
(cherry picked from commit ba0c86a34e29b31ef360feda74c94200a5403fdd)
218e99
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
---
218e99
 blockdev.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++----------------
218e99
 1 file changed, 71 insertions(+), 24 deletions(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 blockdev.c |   95 ++++++++++++++++++++++++++++++++++++++++++++---------------
218e99
 1 files changed, 71 insertions(+), 24 deletions(-)
218e99
218e99
diff --git a/blockdev.c b/blockdev.c
218e99
index b040f0f..b8521c7 100644
218e99
--- a/blockdev.c
218e99
+++ b/blockdev.c
218e99
@@ -779,14 +779,43 @@ void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
218e99
 
218e99
 
218e99
 /* New and old BlockDriverState structs for group snapshots */
218e99
-typedef struct BlkTransactionStates {
218e99
+
218e99
+typedef struct BlkTransactionStates BlkTransactionStates;
218e99
+
218e99
+/* Only prepare() may fail. In a single transaction, only one of commit() or
218e99
+   abort() will be called, clean() will always be called if it present. */
218e99
+typedef struct BdrvActionOps {
218e99
+    /* Size of state struct, in bytes. */
218e99
+    size_t instance_size;
218e99
+    /* Prepare the work, must NOT be NULL. */
218e99
+    void (*prepare)(BlkTransactionStates *common, Error **errp);
218e99
+    /* Commit the changes, must NOT be NULL. */
218e99
+    void (*commit)(BlkTransactionStates *common);
218e99
+    /* Abort the changes on fail, can be NULL. */
218e99
+    void (*abort)(BlkTransactionStates *common);
218e99
+    /* Clean up resource in the end, can be NULL. */
218e99
+    void (*clean)(BlkTransactionStates *common);
218e99
+} BdrvActionOps;
218e99
+
218e99
+/*
218e99
+ * This structure must be arranged as first member in child type, assuming
218e99
+ * that compiler will also arrange it to the same address with parent instance.
218e99
+ * Later it will be used in free().
218e99
+ */
218e99
+struct BlkTransactionStates {
218e99
+    BlockdevAction *action;
218e99
+    const BdrvActionOps *ops;
218e99
+    QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
218e99
+};
218e99
+
218e99
+/* external snapshot private data */
218e99
+typedef struct ExternalSnapshotStates {
218e99
+    BlkTransactionStates common;
218e99
     BlockDriverState *old_bs;
218e99
     BlockDriverState *new_bs;
218e99
-    QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
218e99
-} BlkTransactionStates;
218e99
+} ExternalSnapshotStates;
218e99
 
218e99
-static void external_snapshot_prepare(BlockdevAction *action,
218e99
-                                      BlkTransactionStates *states,
218e99
+static void external_snapshot_prepare(BlkTransactionStates *common,
218e99
                                       Error **errp)
218e99
 {
218e99
     BlockDriver *proto_drv;
218e99
@@ -797,6 +826,9 @@ static void external_snapshot_prepare(BlockdevAction *action,
218e99
     const char *new_image_file;
218e99
     const char *format = "qcow2";
218e99
     enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
218e99
+    ExternalSnapshotStates *states =
218e99
+                             DO_UPCAST(ExternalSnapshotStates, common, common);
218e99
+    BlockdevAction *action = common->action;
218e99
 
218e99
     /* get parameters */
218e99
     g_assert(action->kind == BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
218e99
@@ -871,8 +903,11 @@ static void external_snapshot_prepare(BlockdevAction *action,
218e99
     }
218e99
 }
218e99
 
218e99
-static void external_snapshot_commit(BlkTransactionStates *states)
218e99
+static void external_snapshot_commit(BlkTransactionStates *common)
218e99
 {
218e99
+    ExternalSnapshotStates *states =
218e99
+                             DO_UPCAST(ExternalSnapshotStates, common, common);
218e99
+
218e99
     /* This removes our old bs from the bdrv_states, and adds the new bs */
218e99
     bdrv_append(states->new_bs, states->old_bs);
218e99
     /* We don't need (or want) to use the transactional
218e99
@@ -882,13 +917,24 @@ static void external_snapshot_commit(BlkTransactionStates *states)
218e99
                 NULL);
218e99
 }
218e99
 
218e99
-static void external_snapshot_abort(BlkTransactionStates *states)
218e99
+static void external_snapshot_abort(BlkTransactionStates *common)
218e99
 {
218e99
+    ExternalSnapshotStates *states =
218e99
+                             DO_UPCAST(ExternalSnapshotStates, common, common);
218e99
     if (states->new_bs) {
218e99
         bdrv_delete(states->new_bs);
218e99
     }
218e99
 }
218e99
 
218e99
+static const BdrvActionOps actions[] = {
218e99
+    [BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
218e99
+        .instance_size = sizeof(ExternalSnapshotStates),
218e99
+        .prepare  = external_snapshot_prepare,
218e99
+        .commit   = external_snapshot_commit,
218e99
+        .abort = external_snapshot_abort,
218e99
+    },
218e99
+};
218e99
+
218e99
 /*
218e99
  * 'Atomic' group snapshots.  The snapshots are taken as a set, and if any fail
218e99
  *  then we do not pivot any of the devices in the group, and abandon the
218e99
@@ -909,32 +955,28 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
218e99
     /* We don't do anything in this loop that commits us to the snapshot */
218e99
     while (NULL != dev_entry) {
218e99
         BlockdevAction *dev_info = NULL;
218e99
+        const BdrvActionOps *ops;
218e99
 
218e99
         dev_info = dev_entry->value;
218e99
         dev_entry = dev_entry->next;
218e99
 
218e99
-        states = g_malloc0(sizeof(BlkTransactionStates));
218e99
+        assert(dev_info->kind < ARRAY_SIZE(actions));
218e99
+
218e99
+        ops = &actions[dev_info->kind];
218e99
+        states = g_malloc0(ops->instance_size);
218e99
+        states->ops = ops;
218e99
+        states->action = dev_info;
218e99
         QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
218e99
 
218e99
-        switch (dev_info->kind) {
218e99
-        case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
218e99
-            external_snapshot_prepare(dev_info, states, errp);
218e99
-            if (error_is_set(&local_err)) {
218e99
-                error_propagate(errp, local_err);
218e99
-                goto delete_and_fail;
218e99
-            }
218e99
-            break;
218e99
-        default:
218e99
-            abort();
218e99
+        states->ops->prepare(states, &local_err);
218e99
+        if (error_is_set(&local_err)) {
218e99
+            error_propagate(errp, local_err);
218e99
+            goto delete_and_fail;
218e99
         }
218e99
-
218e99
     }
218e99
 
218e99
-
218e99
-    /* Now we are going to do the actual pivot.  Everything up to this point
218e99
-     * is reversible, but we are committed at this point */
218e99
     QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
218e99
-        external_snapshot_commit(states);
218e99
+        states->ops->commit(states);
218e99
     }
218e99
 
218e99
     /* success */
218e99
@@ -946,10 +988,15 @@ delete_and_fail:
218e99
     * the original bs for all images
218e99
     */
218e99
     QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
218e99
-        external_snapshot_abort(states);
218e99
+        if (states->ops->abort) {
218e99
+            states->ops->abort(states);
218e99
+        }
218e99
     }
218e99
 exit:
218e99
     QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
218e99
+        if (states->ops->clean) {
218e99
+            states->ops->clean(states);
218e99
+        }
218e99
         g_free(states);
218e99
     }
218e99
 }
218e99
-- 
218e99
1.7.1
218e99