cryptospore / rpms / qemu-kvm

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