Blame SOURCES/kvm-job-Move-state-transitions-to-Job.patch

383d26
From bb250647bab72a20db342030756d1837b91bc91a Mon Sep 17 00:00:00 2001
383d26
From: Kevin Wolf <kwolf@redhat.com>
383d26
Date: Tue, 26 Jun 2018 09:48:04 +0200
383d26
Subject: [PATCH 35/89] job: Move state transitions to Job
383d26
383d26
RH-Author: Kevin Wolf <kwolf@redhat.com>
383d26
Message-id: <20180626094856.6924-22-kwolf@redhat.com>
383d26
Patchwork-id: 81093
383d26
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 21/73] job: Move state transitions to Job
383d26
Bugzilla: 1513543
383d26
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
383d26
RH-Acked-by: Max Reitz <mreitz@redhat.com>
383d26
RH-Acked-by: Fam Zheng <famz@redhat.com>
383d26
383d26
This moves BlockJob.status and the closely related functions
383d26
(block_)job_state_transition() and (block_)job_apply_verb to Job. The
383d26
two QAPI enums are renamed to JobStatus and JobVerb.
383d26
383d26
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
383d26
Reviewed-by: Max Reitz <mreitz@redhat.com>
383d26
Reviewed-by: John Snow <jsnow@redhat.com>
383d26
Reviewed-by: Eric Blake <eblake@redhat.com>
383d26
(cherry picked from commit a50c2ab858fe613fb805e53b4f6b970ab936706d)
383d26
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 block/trace-events       |   2 -
383d26
 blockjob.c               | 102 +++++++++++------------------------------------
383d26
 include/block/blockjob.h |   3 --
383d26
 include/qemu/job.h       |  13 ++++++
383d26
 job.c                    |  56 ++++++++++++++++++++++++++
383d26
 qapi/block-core.json     |  16 ++++----
383d26
 tests/test-blockjob.c    |  39 +++++++++---------
383d26
 trace-events             |   4 ++
383d26
 8 files changed, 123 insertions(+), 112 deletions(-)
383d26
383d26
diff --git a/block/trace-events b/block/trace-events
383d26
index f8c50b4..93b9279 100644
383d26
--- a/block/trace-events
383d26
+++ b/block/trace-events
383d26
@@ -6,8 +6,6 @@ bdrv_lock_medium(void *bs, bool locked) "bs %p locked %d"
383d26
 
383d26
 # blockjob.c
383d26
 block_job_completed(void *job, int ret, int jret) "job %p ret %d corrected ret %d"
383d26
-block_job_state_transition(void *job,  int ret, const char *legal, const char *s0, const char *s1) "job %p (ret: %d) attempting %s transition (%s-->%s)"
383d26
-block_job_apply_verb(void *job, const char *state, const char *verb, const char *legal) "job %p in state %s; applying verb %s (%s)"
383d26
 
383d26
 # block/block-backend.c
383d26
 blk_co_preadv(void *blk, void *bs, int64_t offset, unsigned int bytes, int flags) "blk %p bs %p offset %"PRId64" bytes %u flags 0x%x"
383d26
diff --git a/blockjob.c b/blockjob.c
383d26
index c69b2e7..0fba01e 100644
383d26
--- a/blockjob.c
383d26
+++ b/blockjob.c
383d26
@@ -41,61 +41,6 @@
383d26
  * block_job_enter. */
383d26
 static QemuMutex block_job_mutex;
383d26
 
383d26
-/* BlockJob State Transition Table */
383d26
-bool BlockJobSTT[BLOCK_JOB_STATUS__MAX][BLOCK_JOB_STATUS__MAX] = {
383d26
-                                          /* U, C, R, P, Y, S, W, D, X, E, N */
383d26
-    /* U: */ [BLOCK_JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
383d26
-    /* C: */ [BLOCK_JOB_STATUS_CREATED]   = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
383d26
-    /* R: */ [BLOCK_JOB_STATUS_RUNNING]   = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
383d26
-    /* P: */ [BLOCK_JOB_STATUS_PAUSED]    = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
383d26
-    /* Y: */ [BLOCK_JOB_STATUS_READY]     = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
383d26
-    /* S: */ [BLOCK_JOB_STATUS_STANDBY]   = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
383d26
-    /* W: */ [BLOCK_JOB_STATUS_WAITING]   = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
383d26
-    /* D: */ [BLOCK_JOB_STATUS_PENDING]   = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
383d26
-    /* X: */ [BLOCK_JOB_STATUS_ABORTING]  = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
383d26
-    /* E: */ [BLOCK_JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
383d26
-    /* N: */ [BLOCK_JOB_STATUS_NULL]      = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
383d26
-};
383d26
-
383d26
-bool BlockJobVerbTable[BLOCK_JOB_VERB__MAX][BLOCK_JOB_STATUS__MAX] = {
383d26
-                                          /* U, C, R, P, Y, S, W, D, X, E, N */
383d26
-    [BLOCK_JOB_VERB_CANCEL]               = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
383d26
-    [BLOCK_JOB_VERB_PAUSE]                = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
383d26
-    [BLOCK_JOB_VERB_RESUME]               = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
383d26
-    [BLOCK_JOB_VERB_SET_SPEED]            = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
383d26
-    [BLOCK_JOB_VERB_COMPLETE]             = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
383d26
-    [BLOCK_JOB_VERB_FINALIZE]             = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
383d26
-    [BLOCK_JOB_VERB_DISMISS]              = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
383d26
-};
383d26
-
383d26
-static void block_job_state_transition(BlockJob *job, BlockJobStatus s1)
383d26
-{
383d26
-    BlockJobStatus s0 = job->status;
383d26
-    assert(s1 >= 0 && s1 <= BLOCK_JOB_STATUS__MAX);
383d26
-    trace_block_job_state_transition(job, job->ret, BlockJobSTT[s0][s1] ?
383d26
-                                     "allowed" : "disallowed",
383d26
-                                     BlockJobStatus_str(s0),
383d26
-                                     BlockJobStatus_str(s1));
383d26
-    assert(BlockJobSTT[s0][s1]);
383d26
-    job->status = s1;
383d26
-}
383d26
-
383d26
-static int block_job_apply_verb(BlockJob *job, BlockJobVerb bv, Error **errp)
383d26
-{
383d26
-    assert(bv >= 0 && bv <= BLOCK_JOB_VERB__MAX);
383d26
-    trace_block_job_apply_verb(job, BlockJobStatus_str(job->status),
383d26
-                               BlockJobVerb_str(bv),
383d26
-                               BlockJobVerbTable[bv][job->status] ?
383d26
-                               "allowed" : "prohibited");
383d26
-    if (BlockJobVerbTable[bv][job->status]) {
383d26
-        return 0;
383d26
-    }
383d26
-    error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'",
383d26
-               job->job.id, BlockJobStatus_str(job->status),
383d26
-               BlockJobVerb_str(bv));
383d26
-    return -EPERM;
383d26
-}
383d26
-
383d26
 static void block_job_lock(void)
383d26
 {
383d26
     qemu_mutex_lock(&block_job_mutex);
383d26
@@ -257,7 +202,7 @@ static void block_job_detach_aio_context(void *opaque);
383d26
 void block_job_unref(BlockJob *job)
383d26
 {
383d26
     if (--job->refcnt == 0) {
383d26
-        assert(job->status == BLOCK_JOB_STATUS_NULL);
383d26
+        assert(job->job.status == JOB_STATUS_NULL);
383d26
         assert(!job->txn);
383d26
         BlockDriverState *bs = blk_bs(job->blk);
383d26
         bs->job = NULL;
383d26
@@ -409,7 +354,7 @@ void block_job_start(BlockJob *job)
383d26
     job->pause_count--;
383d26
     job->busy = true;
383d26
     job->paused = false;
383d26
-    block_job_state_transition(job, BLOCK_JOB_STATUS_RUNNING);
383d26
+    job_state_transition(&job->job, JOB_STATUS_RUNNING);
383d26
     bdrv_coroutine_enter(blk_bs(job->blk), job->co);
383d26
 }
383d26
 
383d26
@@ -421,7 +366,7 @@ static void block_job_decommission(BlockJob *job)
383d26
     job->paused = false;
383d26
     job->deferred_to_main_loop = true;
383d26
     block_job_txn_del_job(job);
383d26
-    block_job_state_transition(job, BLOCK_JOB_STATUS_NULL);
383d26
+    job_state_transition(&job->job, JOB_STATUS_NULL);
383d26
     block_job_unref(job);
383d26
 }
383d26
 
383d26
@@ -432,7 +377,7 @@ static void block_job_do_dismiss(BlockJob *job)
383d26
 
383d26
 static void block_job_conclude(BlockJob *job)
383d26
 {
383d26
-    block_job_state_transition(job, BLOCK_JOB_STATUS_CONCLUDED);
383d26
+    job_state_transition(&job->job, JOB_STATUS_CONCLUDED);
383d26
     if (job->auto_dismiss || !block_job_started(job)) {
383d26
         block_job_do_dismiss(job);
383d26
     }
383d26
@@ -444,7 +389,7 @@ static void block_job_update_rc(BlockJob *job)
383d26
         job->ret = -ECANCELED;
383d26
     }
383d26
     if (job->ret) {
383d26
-        block_job_state_transition(job, BLOCK_JOB_STATUS_ABORTING);
383d26
+        job_state_transition(&job->job, JOB_STATUS_ABORTING);
383d26
     }
383d26
 }
383d26
 
383d26
@@ -652,7 +597,7 @@ static void block_job_completed_txn_success(BlockJob *job)
383d26
     BlockJobTxn *txn = job->txn;
383d26
     BlockJob *other_job;
383d26
 
383d26
-    block_job_state_transition(job, BLOCK_JOB_STATUS_WAITING);
383d26
+    job_state_transition(&job->job, JOB_STATUS_WAITING);
383d26
 
383d26
     /*
383d26
      * Successful completion, see if there are other running jobs in this
383d26
@@ -677,7 +622,7 @@ void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
383d26
 {
383d26
     int64_t old_speed = job->speed;
383d26
 
383d26
-    if (block_job_apply_verb(job, BLOCK_JOB_VERB_SET_SPEED, errp)) {
383d26
+    if (job_apply_verb(&job->job, JOB_VERB_SET_SPEED, errp)) {
383d26
         return;
383d26
     }
383d26
     if (speed < 0) {
383d26
@@ -709,7 +654,7 @@ void block_job_complete(BlockJob *job, Error **errp)
383d26
 {
383d26
     /* Should not be reachable via external interface for internal jobs */
383d26
     assert(job->job.id);
383d26
-    if (block_job_apply_verb(job, BLOCK_JOB_VERB_COMPLETE, errp)) {
383d26
+    if (job_apply_verb(&job->job, JOB_VERB_COMPLETE, errp)) {
383d26
         return;
383d26
     }
383d26
     if (job->pause_count || job->cancelled || !job->driver->complete) {
383d26
@@ -724,7 +669,7 @@ void block_job_complete(BlockJob *job, Error **errp)
383d26
 void block_job_finalize(BlockJob *job, Error **errp)
383d26
 {
383d26
     assert(job && job->job.id);
383d26
-    if (block_job_apply_verb(job, BLOCK_JOB_VERB_FINALIZE, errp)) {
383d26
+    if (job_apply_verb(&job->job, JOB_VERB_FINALIZE, errp)) {
383d26
         return;
383d26
     }
383d26
     block_job_do_finalize(job);
383d26
@@ -735,7 +680,7 @@ void block_job_dismiss(BlockJob **jobptr, Error **errp)
383d26
     BlockJob *job = *jobptr;
383d26
     /* similarly to _complete, this is QMP-interface only. */
383d26
     assert(job->job.id);
383d26
-    if (block_job_apply_verb(job, BLOCK_JOB_VERB_DISMISS, errp)) {
383d26
+    if (job_apply_verb(&job->job, JOB_VERB_DISMISS, errp)) {
383d26
         return;
383d26
     }
383d26
 
383d26
@@ -745,7 +690,7 @@ void block_job_dismiss(BlockJob **jobptr, Error **errp)
383d26
 
383d26
 void block_job_user_pause(BlockJob *job, Error **errp)
383d26
 {
383d26
-    if (block_job_apply_verb(job, BLOCK_JOB_VERB_PAUSE, errp)) {
383d26
+    if (job_apply_verb(&job->job, JOB_VERB_PAUSE, errp)) {
383d26
         return;
383d26
     }
383d26
     if (job->user_paused) {
383d26
@@ -768,7 +713,7 @@ void block_job_user_resume(BlockJob *job, Error **errp)
383d26
         error_setg(errp, "Can't resume a job that was not paused");
383d26
         return;
383d26
     }
383d26
-    if (block_job_apply_verb(job, BLOCK_JOB_VERB_RESUME, errp)) {
383d26
+    if (job_apply_verb(&job->job, JOB_VERB_RESUME, errp)) {
383d26
         return;
383d26
     }
383d26
     block_job_iostatus_reset(job);
383d26
@@ -778,7 +723,7 @@ void block_job_user_resume(BlockJob *job, Error **errp)
383d26
 
383d26
 void block_job_cancel(BlockJob *job, bool force)
383d26
 {
383d26
-    if (job->status == BLOCK_JOB_STATUS_CONCLUDED) {
383d26
+    if (job->job.status == JOB_STATUS_CONCLUDED) {
383d26
         block_job_do_dismiss(job);
383d26
         return;
383d26
     }
383d26
@@ -794,7 +739,7 @@ void block_job_cancel(BlockJob *job, bool force)
383d26
 
383d26
 void block_job_user_cancel(BlockJob *job, bool force, Error **errp)
383d26
 {
383d26
-    if (block_job_apply_verb(job, BLOCK_JOB_VERB_CANCEL, errp)) {
383d26
+    if (job_apply_verb(&job->job, JOB_VERB_CANCEL, errp)) {
383d26
         return;
383d26
     }
383d26
     block_job_cancel(job, force);
383d26
@@ -859,7 +804,7 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
383d26
     info->speed     = job->speed;
383d26
     info->io_status = job->iostatus;
383d26
     info->ready     = job->ready;
383d26
-    info->status    = job->status;
383d26
+    info->status    = job->job.status;
383d26
     info->auto_finalize = job->auto_finalize;
383d26
     info->auto_dismiss  = job->auto_dismiss;
383d26
     info->has_error = job->ret != 0;
383d26
@@ -907,7 +852,7 @@ static void block_job_event_completed(BlockJob *job, const char *msg)
383d26
 
383d26
 static int block_job_event_pending(BlockJob *job)
383d26
 {
383d26
-    block_job_state_transition(job, BLOCK_JOB_STATUS_PENDING);
383d26
+    job_state_transition(&job->job, JOB_STATUS_PENDING);
383d26
     if (!job->auto_finalize && !block_job_is_internal(job)) {
383d26
         qapi_event_send_block_job_pending(job_type(&job->job),
383d26
                                           job->job.id,
383d26
@@ -975,7 +920,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
383d26
     job->refcnt        = 1;
383d26
     job->auto_finalize = !(flags & BLOCK_JOB_MANUAL_FINALIZE);
383d26
     job->auto_dismiss  = !(flags & BLOCK_JOB_MANUAL_DISMISS);
383d26
-    block_job_state_transition(job, BLOCK_JOB_STATUS_CREATED);
383d26
     aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
383d26
                    QEMU_CLOCK_REALTIME, SCALE_NS,
383d26
                    block_job_sleep_timer_cb, job);
383d26
@@ -1017,7 +961,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
383d26
 
383d26
 void block_job_early_fail(BlockJob *job)
383d26
 {
383d26
-    assert(job->status == BLOCK_JOB_STATUS_CREATED);
383d26
+    assert(job->job.status == JOB_STATUS_CREATED);
383d26
     block_job_decommission(job);
383d26
 }
383d26
 
383d26
@@ -1077,14 +1021,14 @@ void coroutine_fn block_job_pause_point(BlockJob *job)
383d26
     }
383d26
 
383d26
     if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
383d26
-        BlockJobStatus status = job->status;
383d26
-        block_job_state_transition(job, status == BLOCK_JOB_STATUS_READY ? \
383d26
-                                   BLOCK_JOB_STATUS_STANDBY :           \
383d26
-                                   BLOCK_JOB_STATUS_PAUSED);
383d26
+        JobStatus status = job->job.status;
383d26
+        job_state_transition(&job->job, status == JOB_STATUS_READY
383d26
+                                        ? JOB_STATUS_STANDBY
383d26
+                                        : JOB_STATUS_PAUSED);
383d26
         job->paused = true;
383d26
         block_job_do_yield(job, -1);
383d26
         job->paused = false;
383d26
-        block_job_state_transition(job, status);
383d26
+        job_state_transition(&job->job, status);
383d26
     }
383d26
 
383d26
     if (job->driver->resume) {
383d26
@@ -1176,7 +1120,7 @@ void block_job_iostatus_reset(BlockJob *job)
383d26
 
383d26
 void block_job_event_ready(BlockJob *job)
383d26
 {
383d26
-    block_job_state_transition(job, BLOCK_JOB_STATUS_READY);
383d26
+    job_state_transition(&job->job, JOB_STATUS_READY);
383d26
     job->ready = true;
383d26
 
383d26
     if (block_job_is_internal(job)) {
383d26
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
383d26
index 10bd9f7..01cdee6 100644
383d26
--- a/include/block/blockjob.h
383d26
+++ b/include/block/blockjob.h
383d26
@@ -147,9 +147,6 @@ typedef struct BlockJob {
383d26
      */
383d26
     QEMUTimer sleep_timer;
383d26
 
383d26
-    /** Current state; See @BlockJobStatus for details. */
383d26
-    BlockJobStatus status;
383d26
-
383d26
     /** True if this job should automatically finalize itself */
383d26
     bool auto_finalize;
383d26
 
383d26
diff --git a/include/qemu/job.h b/include/qemu/job.h
383d26
index bae2b09..0b78778 100644
383d26
--- a/include/qemu/job.h
383d26
+++ b/include/qemu/job.h
383d26
@@ -41,6 +41,9 @@ typedef struct Job {
383d26
     /** The type of this job. */
383d26
     const JobDriver *driver;
383d26
 
383d26
+    /** Current state; See @JobStatus for details. */
383d26
+    JobStatus status;
383d26
+
383d26
     /** Element of the list of jobs */
383d26
     QLIST_ENTRY(Job) job_list;
383d26
 } Job;
383d26
@@ -90,4 +93,14 @@ Job *job_next(Job *job);
383d26
  */
383d26
 Job *job_get(const char *id);
383d26
 
383d26
+/**
383d26
+ * Check whether the verb @verb can be applied to @job in its current state.
383d26
+ * Returns 0 if the verb can be applied; otherwise errp is set and -EPERM
383d26
+ * returned.
383d26
+ */
383d26
+int job_apply_verb(Job *job, JobVerb verb, Error **errp);
383d26
+
383d26
+/* TODO To be removed from the public interface */
383d26
+void job_state_transition(Job *job, JobStatus s1);
383d26
+
383d26
 #endif
383d26
diff --git a/job.c b/job.c
383d26
index e57303c..b049a32 100644
383d26
--- a/job.c
383d26
+++ b/job.c
383d26
@@ -28,9 +28,63 @@
383d26
 #include "qapi/error.h"
383d26
 #include "qemu/job.h"
383d26
 #include "qemu/id.h"
383d26
+#include "trace-root.h"
383d26
 
383d26
 static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
383d26
 
383d26
+/* Job State Transition Table */
383d26
+bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = {
383d26
+                                    /* U, C, R, P, Y, S, W, D, X, E, N */
383d26
+    /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
383d26
+    /* C: */ [JOB_STATUS_CREATED]   = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
383d26
+    /* R: */ [JOB_STATUS_RUNNING]   = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
383d26
+    /* P: */ [JOB_STATUS_PAUSED]    = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
383d26
+    /* Y: */ [JOB_STATUS_READY]     = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
383d26
+    /* S: */ [JOB_STATUS_STANDBY]   = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
383d26
+    /* W: */ [JOB_STATUS_WAITING]   = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
383d26
+    /* D: */ [JOB_STATUS_PENDING]   = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
383d26
+    /* X: */ [JOB_STATUS_ABORTING]  = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
383d26
+    /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
383d26
+    /* N: */ [JOB_STATUS_NULL]      = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
383d26
+};
383d26
+
383d26
+bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = {
383d26
+                                    /* U, C, R, P, Y, S, W, D, X, E, N */
383d26
+    [JOB_VERB_CANCEL]               = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
383d26
+    [JOB_VERB_PAUSE]                = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
383d26
+    [JOB_VERB_RESUME]               = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
383d26
+    [JOB_VERB_SET_SPEED]            = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
383d26
+    [JOB_VERB_COMPLETE]             = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
383d26
+    [JOB_VERB_FINALIZE]             = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
383d26
+    [JOB_VERB_DISMISS]              = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
383d26
+};
383d26
+
383d26
+/* TODO Make static once the whole state machine is in job.c */
383d26
+void job_state_transition(Job *job, JobStatus s1)
383d26
+{
383d26
+    JobStatus s0 = job->status;
383d26
+    assert(s1 >= 0 && s1 <= JOB_STATUS__MAX);
383d26
+    trace_job_state_transition(job, /* TODO re-enable: job->ret */ 0,
383d26
+                               JobSTT[s0][s1] ? "allowed" : "disallowed",
383d26
+                               JobStatus_str(s0), JobStatus_str(s1));
383d26
+    assert(JobSTT[s0][s1]);
383d26
+    job->status = s1;
383d26
+}
383d26
+
383d26
+int job_apply_verb(Job *job, JobVerb verb, Error **errp)
383d26
+{
383d26
+    JobStatus s0 = job->status;
383d26
+    assert(verb >= 0 && verb <= JOB_VERB__MAX);
383d26
+    trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb),
383d26
+                         JobVerbTable[verb][s0] ? "allowed" : "prohibited");
383d26
+    if (JobVerbTable[verb][s0]) {
383d26
+        return 0;
383d26
+    }
383d26
+    error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'",
383d26
+               job->id, JobStatus_str(s0), JobVerb_str(verb));
383d26
+    return -EPERM;
383d26
+}
383d26
+
383d26
 JobType job_type(const Job *job)
383d26
 {
383d26
     return job->driver->job_type;
383d26
@@ -81,6 +135,8 @@ void *job_create(const char *job_id, const JobDriver *driver, Error **errp)
383d26
     job->driver        = driver;
383d26
     job->id            = g_strdup(job_id);
383d26
 
383d26
+    job_state_transition(job, JOB_STATUS_CREATED);
383d26
+
383d26
     QLIST_INSERT_HEAD(&jobs, job, job_list);
383d26
 
383d26
     return job;
383d26
diff --git a/qapi/block-core.json b/qapi/block-core.json
383d26
index 607e5c5..85bf353 100644
383d26
--- a/qapi/block-core.json
383d26
+++ b/qapi/block-core.json
383d26
@@ -1068,9 +1068,9 @@
383d26
   'data': ['commit', 'stream', 'mirror', 'backup'] }
383d26
 
383d26
 ##
383d26
-# @BlockJobVerb:
383d26
+# @JobVerb:
383d26
 #
383d26
-# Represents command verbs that can be applied to a blockjob.
383d26
+# Represents command verbs that can be applied to a job.
383d26
 #
383d26
 # @cancel: see @block-job-cancel
383d26
 #
383d26
@@ -1088,14 +1088,14 @@
383d26
 #
383d26
 # Since: 2.12
383d26
 ##
383d26
-{ 'enum': 'BlockJobVerb',
383d26
+{ 'enum': 'JobVerb',
383d26
   'data': ['cancel', 'pause', 'resume', 'set-speed', 'complete', 'dismiss',
383d26
            'finalize' ] }
383d26
 
383d26
 ##
383d26
-# @BlockJobStatus:
383d26
+# @JobStatus:
383d26
 #
383d26
-# Indicates the present state of a given blockjob in its lifetime.
383d26
+# Indicates the present state of a given job in its lifetime.
383d26
 #
383d26
 # @undefined: Erroneous, default state. Should not ever be visible.
383d26
 #
383d26
@@ -1134,7 +1134,7 @@
383d26
 #
383d26
 # Since: 2.12
383d26
 ##
383d26
-{ 'enum': 'BlockJobStatus',
383d26
+{ 'enum': 'JobStatus',
383d26
   'data': ['undefined', 'created', 'running', 'paused', 'ready', 'standby',
383d26
            'waiting', 'pending', 'aborting', 'concluded', 'null' ] }
383d26
 
383d26
@@ -1184,7 +1184,7 @@
383d26
   'data': {'type': 'str', 'device': 'str', 'len': 'int',
383d26
            'offset': 'int', 'busy': 'bool', 'paused': 'bool', 'speed': 'int',
383d26
            'io-status': 'BlockDeviceIoStatus', 'ready': 'bool',
383d26
-           'status': 'BlockJobStatus',
383d26
+           'status': 'JobStatus',
383d26
            'auto-finalize': 'bool', 'auto-dismiss': 'bool',
383d26
            '*error': 'str' } }
383d26
 
383d26
@@ -2416,7 +2416,7 @@
383d26
 # QEMU 2.12+ job lifetime management semantics.
383d26
 #
383d26
 # This command will refuse to operate on any job that has not yet reached
383d26
-# its terminal state, BLOCK_JOB_STATUS_CONCLUDED. For jobs that make use of
383d26
+# its terminal state, JOB_STATUS_CONCLUDED. For jobs that make use of the
383d26
 # BLOCK_JOB_READY event, block-job-cancel or block-job-complete will still need
383d26
 # to be used as appropriate.
383d26
 #
383d26
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
383d26
index b820261..6ccd585 100644
383d26
--- a/tests/test-blockjob.c
383d26
+++ b/tests/test-blockjob.c
383d26
@@ -211,7 +211,7 @@ static CancelJob *create_common(BlockJob **pjob)
383d26
     job = mk_job(blk, "Steve", &test_cancel_driver, true,
383d26
                  BLOCK_JOB_MANUAL_FINALIZE | BLOCK_JOB_MANUAL_DISMISS);
383d26
     block_job_ref(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_CREATED);
383d26
+    assert(job->job.status == JOB_STATUS_CREATED);
383d26
     s = container_of(job, CancelJob, common);
383d26
     s->blk = blk;
383d26
 
383d26
@@ -223,15 +223,14 @@ static void cancel_common(CancelJob *s)
383d26
 {
383d26
     BlockJob *job = &s->common;
383d26
     BlockBackend *blk = s->blk;
383d26
-    BlockJobStatus sts = job->status;
383d26
+    JobStatus sts = job->job.status;
383d26
 
383d26
     block_job_cancel_sync(job);
383d26
-    if ((sts != BLOCK_JOB_STATUS_CREATED) &&
383d26
-        (sts != BLOCK_JOB_STATUS_CONCLUDED)) {
383d26
+    if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
383d26
         BlockJob *dummy = job;
383d26
         block_job_dismiss(&dummy, &error_abort);
383d26
     }
383d26
-    assert(job->status == BLOCK_JOB_STATUS_NULL);
383d26
+    assert(job->job.status == JOB_STATUS_NULL);
383d26
     block_job_unref(job);
383d26
     destroy_blk(blk);
383d26
 }
383d26
@@ -253,7 +252,7 @@ static void test_cancel_running(void)
383d26
     s = create_common(&job;;
383d26
 
383d26
     block_job_start(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_RUNNING);
383d26
+    assert(job->job.status == JOB_STATUS_RUNNING);
383d26
 
383d26
     cancel_common(s);
383d26
 }
383d26
@@ -266,11 +265,11 @@ static void test_cancel_paused(void)
383d26
     s = create_common(&job;;
383d26
 
383d26
     block_job_start(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_RUNNING);
383d26
+    assert(job->job.status == JOB_STATUS_RUNNING);
383d26
 
383d26
     block_job_user_pause(job, &error_abort);
383d26
     block_job_enter(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_PAUSED);
383d26
+    assert(job->job.status == JOB_STATUS_PAUSED);
383d26
 
383d26
     cancel_common(s);
383d26
 }
383d26
@@ -283,11 +282,11 @@ static void test_cancel_ready(void)
383d26
     s = create_common(&job;;
383d26
 
383d26
     block_job_start(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_RUNNING);
383d26
+    assert(job->job.status == JOB_STATUS_RUNNING);
383d26
 
383d26
     s->should_converge = true;
383d26
     block_job_enter(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_READY);
383d26
+    assert(job->job.status == JOB_STATUS_READY);
383d26
 
383d26
     cancel_common(s);
383d26
 }
383d26
@@ -300,15 +299,15 @@ static void test_cancel_standby(void)
383d26
     s = create_common(&job;;
383d26
 
383d26
     block_job_start(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_RUNNING);
383d26
+    assert(job->job.status == JOB_STATUS_RUNNING);
383d26
 
383d26
     s->should_converge = true;
383d26
     block_job_enter(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_READY);
383d26
+    assert(job->job.status == JOB_STATUS_READY);
383d26
 
383d26
     block_job_user_pause(job, &error_abort);
383d26
     block_job_enter(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_STANDBY);
383d26
+    assert(job->job.status == JOB_STATUS_STANDBY);
383d26
 
383d26
     cancel_common(s);
383d26
 }
383d26
@@ -321,18 +320,18 @@ static void test_cancel_pending(void)
383d26
     s = create_common(&job;;
383d26
 
383d26
     block_job_start(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_RUNNING);
383d26
+    assert(job->job.status == JOB_STATUS_RUNNING);
383d26
 
383d26
     s->should_converge = true;
383d26
     block_job_enter(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_READY);
383d26
+    assert(job->job.status == JOB_STATUS_READY);
383d26
 
383d26
     block_job_complete(job, &error_abort);
383d26
     block_job_enter(job);
383d26
     while (!s->completed) {
383d26
         aio_poll(qemu_get_aio_context(), true);
383d26
     }
383d26
-    assert(job->status == BLOCK_JOB_STATUS_PENDING);
383d26
+    assert(job->job.status == JOB_STATUS_PENDING);
383d26
 
383d26
     cancel_common(s);
383d26
 }
383d26
@@ -345,21 +344,21 @@ static void test_cancel_concluded(void)
383d26
     s = create_common(&job;;
383d26
 
383d26
     block_job_start(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_RUNNING);
383d26
+    assert(job->job.status == JOB_STATUS_RUNNING);
383d26
 
383d26
     s->should_converge = true;
383d26
     block_job_enter(job);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_READY);
383d26
+    assert(job->job.status == JOB_STATUS_READY);
383d26
 
383d26
     block_job_complete(job, &error_abort);
383d26
     block_job_enter(job);
383d26
     while (!s->completed) {
383d26
         aio_poll(qemu_get_aio_context(), true);
383d26
     }
383d26
-    assert(job->status == BLOCK_JOB_STATUS_PENDING);
383d26
+    assert(job->job.status == JOB_STATUS_PENDING);
383d26
 
383d26
     block_job_finalize(job, &error_abort);
383d26
-    assert(job->status == BLOCK_JOB_STATUS_CONCLUDED);
383d26
+    assert(job->job.status == JOB_STATUS_CONCLUDED);
383d26
 
383d26
     cancel_common(s);
383d26
 }
383d26
diff --git a/trace-events b/trace-events
383d26
index ed71f44..2507e13 100644
383d26
--- a/trace-events
383d26
+++ b/trace-events
383d26
@@ -104,6 +104,10 @@ gdbstub_err_invalid_rle(void) "got invalid RLE sequence"
383d26
 gdbstub_err_checksum_invalid(uint8_t ch) "got invalid command checksum digit: 0x%02x"
383d26
 gdbstub_err_checksum_incorrect(uint8_t expected, uint8_t got) "got command packet with incorrect checksum, expected=0x%02x, received=0x%02x"
383d26
 
383d26
+# job.c
383d26
+job_state_transition(void *job,  int ret, const char *legal, const char *s0, const char *s1) "job %p (ret: %d) attempting %s transition (%s-->%s)"
383d26
+job_apply_verb(void *job, const char *state, const char *verb, const char *legal) "job %p in state %s; applying verb %s (%s)"
383d26
+
383d26
 ### Guest events, keep at bottom
383d26
 
383d26
 
383d26
-- 
383d26
1.8.3.1
383d26