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