Blame SOURCES/kvm-block-commit-refactor-commit-to-use-job-callbacks.patch

1bdc94
From c720832a9aa72b4b9a326ea95c46ed1e2cbd5cad Mon Sep 17 00:00:00 2001
1bdc94
From: John Snow <jsnow@redhat.com>
1bdc94
Date: Mon, 10 Sep 2018 18:17:51 +0200
1bdc94
Subject: [PATCH 13/25] block/commit: refactor commit to use job callbacks
1bdc94
1bdc94
RH-Author: John Snow <jsnow@redhat.com>
1bdc94
Message-id: <20180910181803.11781-14-jsnow@redhat.com>
1bdc94
Patchwork-id: 82092
1bdc94
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 13/25] block/commit: refactor commit to use job callbacks
1bdc94
Bugzilla: 1626061
1bdc94
RH-Acked-by: Max Reitz <mreitz@redhat.com>
1bdc94
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
1bdc94
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
1bdc94
Use the component callbacks; prepare, abort, and clean.
1bdc94
1bdc94
NB: prepare is only called when the job has not yet failed;
1bdc94
and abort can be called after prepare.
1bdc94
1bdc94
complete -> prepare -> abort -> clean
1bdc94
complete -> abort -> clean
1bdc94
1bdc94
During refactor, a potential problem with bdrv_drop_intermediate
1bdc94
was identified, the patched behavior is no worse than the pre-patch
1bdc94
behavior, so leave a FIXME for now to be fixed in a future patch.
1bdc94
1bdc94
Signed-off-by: John Snow <jsnow@redhat.com>
1bdc94
Reviewed-by: Max Reitz <mreitz@redhat.com>
1bdc94
Message-id: 20180906130225.5118-5-jsnow@redhat.com
1bdc94
Reviewed-by: Jeff Cody <jcody@redhat.com>
1bdc94
Signed-off-by: Max Reitz <mreitz@redhat.com>
1bdc94
(cherry picked from commit 38ff0656c24ad88fb08af343ad610c9fc4741c58)
1bdc94
Signed-off-by: John Snow <jsnow@redhat.com>
1bdc94
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1bdc94
---
1bdc94
 block/commit.c | 92 ++++++++++++++++++++++++++++++++--------------------------
1bdc94
 1 file changed, 51 insertions(+), 41 deletions(-)
1bdc94
1bdc94
diff --git a/block/commit.c b/block/commit.c
1bdc94
index c737664..b387765 100644
1bdc94
--- a/block/commit.c
1bdc94
+++ b/block/commit.c
1bdc94
@@ -36,6 +36,7 @@ typedef struct CommitBlockJob {
1bdc94
     BlockDriverState *commit_top_bs;
1bdc94
     BlockBackend *top;
1bdc94
     BlockBackend *base;
1bdc94
+    BlockDriverState *base_bs;
1bdc94
     BlockdevOnError on_error;
1bdc94
     int base_flags;
1bdc94
     char *backing_file_str;
1bdc94
@@ -68,61 +69,67 @@ static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *base,
1bdc94
     return 0;
1bdc94
 }
1bdc94
 
1bdc94
-static void commit_exit(Job *job)
1bdc94
+static int commit_prepare(Job *job)
1bdc94
 {
1bdc94
     CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
1bdc94
-    BlockJob *bjob = &s->common;
1bdc94
-    BlockDriverState *top = blk_bs(s->top);
1bdc94
-    BlockDriverState *base = blk_bs(s->base);
1bdc94
-    BlockDriverState *commit_top_bs = s->commit_top_bs;
1bdc94
-    bool remove_commit_top_bs = false;
1bdc94
-
1bdc94
-    /* Make sure commit_top_bs and top stay around until bdrv_replace_node() */
1bdc94
-    bdrv_ref(top);
1bdc94
-    bdrv_ref(commit_top_bs);
1bdc94
 
1bdc94
     /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
1bdc94
      * the normal backing chain can be restored. */
1bdc94
     blk_unref(s->base);
1bdc94
+    s->base = NULL;
1bdc94
+
1bdc94
+    /* FIXME: bdrv_drop_intermediate treats total failures and partial failures
1bdc94
+     * identically. Further work is needed to disambiguate these cases. */
1bdc94
+    return bdrv_drop_intermediate(s->commit_top_bs, s->base_bs,
1bdc94
+                                  s->backing_file_str);
1bdc94
+}
1bdc94
 
1bdc94
-    if (!job_is_cancelled(job) && job->ret == 0) {
1bdc94
-        /* success */
1bdc94
-        job->ret = bdrv_drop_intermediate(s->commit_top_bs, base,
1bdc94
-                                          s->backing_file_str);
1bdc94
-    } else {
1bdc94
-        /* XXX Can (or should) we somehow keep 'consistent read' blocked even
1bdc94
-         * after the failed/cancelled commit job is gone? If we already wrote
1bdc94
-         * something to base, the intermediate images aren't valid any more. */
1bdc94
-        remove_commit_top_bs = true;
1bdc94
+static void commit_abort(Job *job)
1bdc94
+{
1bdc94
+    CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
1bdc94
+    BlockDriverState *top_bs = blk_bs(s->top);
1bdc94
+
1bdc94
+    /* Make sure commit_top_bs and top stay around until bdrv_replace_node() */
1bdc94
+    bdrv_ref(top_bs);
1bdc94
+    bdrv_ref(s->commit_top_bs);
1bdc94
+
1bdc94
+    if (s->base) {
1bdc94
+        blk_unref(s->base);
1bdc94
     }
1bdc94
 
1bdc94
+    /* free the blockers on the intermediate nodes so that bdrv_replace_nodes
1bdc94
+     * can succeed */
1bdc94
+    block_job_remove_all_bdrv(&s->common);
1bdc94
+
1bdc94
+    /* If bdrv_drop_intermediate() failed (or was not invoked), remove the
1bdc94
+     * commit filter driver from the backing chain now. Do this as the final
1bdc94
+     * step so that the 'consistent read' permission can be granted.
1bdc94
+     *
1bdc94
+     * XXX Can (or should) we somehow keep 'consistent read' blocked even
1bdc94
+     * after the failed/cancelled commit job is gone? If we already wrote
1bdc94
+     * something to base, the intermediate images aren't valid any more. */
1bdc94
+    bdrv_child_try_set_perm(s->commit_top_bs->backing, 0, BLK_PERM_ALL,
1bdc94
+                            &error_abort);
1bdc94
+    bdrv_replace_node(s->commit_top_bs, backing_bs(s->commit_top_bs),
1bdc94
+                      &error_abort);
1bdc94
+
1bdc94
+    bdrv_unref(s->commit_top_bs);
1bdc94
+    bdrv_unref(top_bs);
1bdc94
+}
1bdc94
+
1bdc94
+static void commit_clean(Job *job)
1bdc94
+{
1bdc94
+    CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
1bdc94
+
1bdc94
     /* restore base open flags here if appropriate (e.g., change the base back
1bdc94
      * to r/o). These reopens do not need to be atomic, since we won't abort
1bdc94
      * even on failure here */
1bdc94
-    if (s->base_flags != bdrv_get_flags(base)) {
1bdc94
-        bdrv_reopen(base, s->base_flags, NULL);
1bdc94
+    if (s->base_flags != bdrv_get_flags(s->base_bs)) {
1bdc94
+        bdrv_reopen(s->base_bs, s->base_flags, NULL);
1bdc94
     }
1bdc94
+
1bdc94
     g_free(s->backing_file_str);
1bdc94
     blk_unref(s->top);
1bdc94
-
1bdc94
-    /* If there is more than one reference to the job (e.g. if called from
1bdc94
-     * job_finish_sync()), job_completed() won't free it and therefore the
1bdc94
-     * blockers on the intermediate nodes remain. This would cause
1bdc94
-     * bdrv_set_backing_hd() to fail. */
1bdc94
-    block_job_remove_all_bdrv(bjob);
1bdc94
-
1bdc94
-    /* If bdrv_drop_intermediate() didn't already do that, remove the commit
1bdc94
-     * filter driver from the backing chain. Do this as the final step so that
1bdc94
-     * the 'consistent read' permission can be granted.  */
1bdc94
-    if (remove_commit_top_bs) {
1bdc94
-        bdrv_child_try_set_perm(commit_top_bs->backing, 0, BLK_PERM_ALL,
1bdc94
-                                &error_abort);
1bdc94
-        bdrv_replace_node(commit_top_bs, backing_bs(commit_top_bs),
1bdc94
-                          &error_abort);
1bdc94
-    }
1bdc94
-
1bdc94
-    bdrv_unref(commit_top_bs);
1bdc94
-    bdrv_unref(top);
1bdc94
 }
1bdc94
 
1bdc94
 static int coroutine_fn commit_run(Job *job, Error **errp)
1bdc94
@@ -211,7 +218,9 @@ static const BlockJobDriver commit_job_driver = {
1bdc94
         .user_resume   = block_job_user_resume,
1bdc94
         .drain         = block_job_drain,
1bdc94
         .run           = commit_run,
1bdc94
-        .exit          = commit_exit,
1bdc94
+        .prepare       = commit_prepare,
1bdc94
+        .abort         = commit_abort,
1bdc94
+        .clean         = commit_clean
1bdc94
     },
1bdc94
 };
1bdc94
 
1bdc94
@@ -350,6 +359,7 @@ void commit_start(const char *job_id, BlockDriverState *bs,
1bdc94
     if (ret < 0) {
1bdc94
         goto fail;
1bdc94
     }
1bdc94
+    s->base_bs = base;
1bdc94
 
1bdc94
     /* Required permissions are already taken with block_job_add_bdrv() */
1bdc94
     s->top = blk_new(0, BLK_PERM_ALL);
1bdc94
-- 
1bdc94
1.8.3.1
1bdc94