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

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