qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: John Snow <jsnow@redhat.com>
To: qemu-devel@nongnu.org, qemu-block@nongnu.org
Cc: kwolf@redhat.com, Jeff Cody <jcody@redhat.com>,
	Max Reitz <mreitz@redhat.com>,
	jtc@redhat.com, Markus Armbruster <armbru@redhat.com>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	Eric Blake <eblake@redhat.com>, John Snow <jsnow@redhat.com>
Subject: [Qemu-devel] [PATCH 16/21] block/commit: refactor commit to use job callbacks
Date: Tue,  7 Aug 2018 00:33:44 -0400	[thread overview]
Message-ID: <20180807043349.27196-17-jsnow@redhat.com> (raw)
In-Reply-To: <20180807043349.27196-1-jsnow@redhat.com>

Use the component callbacks; prepare, commit, abort, and clean.

NB: prepare is only called when the job has not yet failed;
and abort can be called after prepare.

complete -> prepare -> abort -> clean
complete -> abort -> clean

Signed-off-by: John Snow <jsnow@redhat.com>
---
 block/commit.c | 94 +++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 61 insertions(+), 33 deletions(-)

diff --git a/block/commit.c b/block/commit.c
index 1a4a56795f..6673a0544e 100644
--- a/block/commit.c
+++ b/block/commit.c
@@ -35,7 +35,9 @@ typedef struct CommitBlockJob {
     BlockJob common;
     BlockDriverState *commit_top_bs;
     BlockBackend *top;
+    BlockDriverState *top_bs;
     BlockBackend *base;
+    BlockDriverState *base_bs;
     BlockdevOnError on_error;
     int base_flags;
     char *backing_file_str;
@@ -71,37 +73,37 @@ static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *base,
 static void commit_exit(Job *job)
 {
     CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
-    BlockJob *bjob = &s->common;
-    BlockDriverState *top = blk_bs(s->top);
-    BlockDriverState *base = blk_bs(s->base);
-    BlockDriverState *commit_top_bs = s->commit_top_bs;
-    int ret = job->ret;
-    bool remove_commit_top_bs = false;
+
+    s->top_bs = blk_bs(s->top);
+    s->base_bs = blk_bs(s->base);
 
     /* Make sure commit_top_bs and top stay around until bdrv_replace_node() */
-    bdrv_ref(top);
-    bdrv_ref(commit_top_bs);
+    bdrv_ref(s->top_bs);
+    bdrv_ref(s->commit_top_bs);
+}
 
-    /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
-     * the normal backing chain can be restored. */
-    blk_unref(s->base);
+static int commit_prepare(Job *job)
+{
+    CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
 
-    if (!job_is_cancelled(job) && ret == 0) {
-        /* success */
-        ret = bdrv_drop_intermediate(s->commit_top_bs, base,
-                                     s->backing_file_str);
-    } else {
-        /* XXX Can (or should) we somehow keep 'consistent read' blocked even
-         * after the failed/cancelled commit job is gone? If we already wrote
-         * something to base, the intermediate images aren't valid any more. */
-        remove_commit_top_bs = true;
-    }
+     /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
+      * the normal backing chain can be restored. */
+     blk_unref(s->base);
+     s->base = NULL;
+
+     return bdrv_drop_intermediate(s->commit_top_bs, s->base_bs,
+                                   s->backing_file_str);
+}
+
+static void commit_exit_common(Job *job)
+{
+    CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
 
     /* restore base open flags here if appropriate (e.g., change the base back
      * to r/o). These reopens do not need to be atomic, since we won't abort
      * even on failure here */
-    if (s->base_flags != bdrv_get_flags(base)) {
-        bdrv_reopen(base, s->base_flags, NULL);
+    if (s->base_flags != bdrv_get_flags(s->base_bs)) {
+        bdrv_reopen(s->base_bs, s->base_flags, NULL);
     }
     g_free(s->backing_file_str);
     blk_unref(s->top);
@@ -110,21 +112,43 @@ static void commit_exit(Job *job)
      * job_finish_sync()), job_completed() won't free it and therefore the
      * blockers on the intermediate nodes remain. This would cause
      * bdrv_set_backing_hd() to fail. */
-    block_job_remove_all_bdrv(bjob);
+    block_job_remove_all_bdrv(&s->common);
+}
+
+static void commit_commit(Job *job)
+{
+    commit_exit_common(job);
+}
+
+static void commit_abort(Job *job)
+{
+    CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
+
+    if (s->base) {
+        blk_unref(s->base);
+    }
+
+    commit_exit_common(job);
+
+    /* XXX Can (or should) we somehow keep 'consistent read' blocked even
+     * after the failed/cancelled commit job is gone? If we already wrote
+     * something to base, the intermediate images aren't valid any more. */
 
     /* If bdrv_drop_intermediate() didn't already do that, remove the commit
      * filter driver from the backing chain. Do this as the final step so that
      * the 'consistent read' permission can be granted.  */
-    if (remove_commit_top_bs) {
-        bdrv_child_try_set_perm(commit_top_bs->backing, 0, BLK_PERM_ALL,
-                                &error_abort);
-        bdrv_replace_node(commit_top_bs, backing_bs(commit_top_bs),
-                          &error_abort);
-    }
+    bdrv_child_try_set_perm(s->commit_top_bs->backing, 0, BLK_PERM_ALL,
+                            &error_abort);
+    bdrv_replace_node(s->commit_top_bs, backing_bs(s->commit_top_bs),
+                      &error_abort);
+}
 
-    bdrv_unref(commit_top_bs);
-    bdrv_unref(top);
-    job->ret = ret;
+static void commit_clean(Job *job)
+{
+    CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
+
+    bdrv_unref(s->commit_top_bs);
+    bdrv_unref(s->top_bs);
 }
 
 static int coroutine_fn commit_run(Job *job)
@@ -214,6 +238,10 @@ static const BlockJobDriver commit_job_driver = {
         .drain         = block_job_drain,
         .start         = commit_run,
         .exit          = commit_exit,
+        .prepare       = commit_prepare,
+        .commit        = commit_commit,
+        .abort         = commit_abort,
+        .clean         = commit_clean
     },
 };
 
-- 
2.14.4

  parent reply	other threads:[~2018-08-07  4:34 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-07  4:33 [Qemu-devel] [PATCH 00/21] jobs: defer graph changes until finalize John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 01/21] jobs: canonize Error object John Snow
2018-08-08 14:57   ` Kevin Wolf
2018-08-08 15:50     ` John Snow
2018-08-08 16:02       ` Kevin Wolf
2018-08-07  4:33 ` [Qemu-devel] [PATCH 02/21] jobs: add exit shim John Snow
2018-08-08  4:02   ` Jeff Cody
2018-08-08 13:55     ` John Snow
2018-08-08 15:23     ` Kevin Wolf
2018-08-08 15:38       ` John Snow
2018-08-08 15:47         ` Kevin Wolf
2018-08-07  4:33 ` [Qemu-devel] [PATCH 03/21] block/backup: utilize job_exit shim John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 04/21] block/commit: " John Snow
2018-08-08  3:41   ` Jeff Cody
2018-08-08 14:00     ` John Snow
2018-08-08 16:29   ` Kevin Wolf
2018-08-15 20:52     ` John Snow
2018-08-16  6:41       ` Kevin Wolf
2018-08-07  4:33 ` [Qemu-devel] [PATCH 05/21] block/mirror: " John Snow
2018-08-08  3:47   ` Jeff Cody
2018-08-07  4:33 ` [Qemu-devel] [PATCH 06/21] block/stream: " John Snow
2018-08-08  3:47   ` Jeff Cody
2018-08-07  4:33 ` [Qemu-devel] [PATCH 07/21] block/create: " John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 08/21] tests/test-blockjob-txn: " John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 09/21] tests/test-blockjob: " John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 10/21] tests/test-bdrv-drain: " John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 11/21] jobs: remove job_defer_to_main_loop John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 12/21] jobs: allow entrypoints to return status code John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 13/21] block/commit: add block job creation flags John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 14/21] block/mirror: " John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 15/21] block/stream: " John Snow
2018-08-07  4:33 ` John Snow [this message]
2018-08-09 15:12   ` [Qemu-devel] [PATCH 16/21] block/commit: refactor commit to use job callbacks Kevin Wolf
2018-08-09 16:22     ` [Qemu-devel] [Qemu-block] " Kevin Wolf
2018-08-15 21:17     ` [Qemu-devel] " John Snow
2018-08-16  6:52       ` Kevin Wolf
2018-08-07  4:33 ` [Qemu-devel] [PATCH 17/21] block/mirror: conservative mirror_exit refactor John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 18/21] block/commit: refactor stream to use job callbacks John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 19/21] qapi/block-commit: expose new job properties John Snow
2018-08-07 14:52   ` Eric Blake
2018-08-07 18:11     ` John Snow
2018-08-10 11:47     ` Kevin Wolf
2018-08-07  4:33 ` [Qemu-devel] [PATCH 20/21] qapi/block-mirror: " John Snow
2018-08-07  4:33 ` [Qemu-devel] [PATCH 21/21] qapi/block-stream: " John Snow
2018-08-15 14:44 ` [Qemu-devel] [Qemu-block] [PATCH 00/21] jobs: defer graph changes until finalize Peter Krempa
2018-08-15 15:00   ` Kevin Wolf
2018-08-15 15:04     ` Peter Krempa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180807043349.27196-17-jsnow@redhat.com \
    --to=jsnow@redhat.com \
    --cc=armbru@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=jcody@redhat.com \
    --cc=jtc@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).