2018-04-12 23:29:59 +08:00
|
|
|
/*
|
|
|
|
* Background jobs (long-running operations)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 IBM Corp.
|
|
|
|
* Copyright (c) 2012, 2018 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qemu/job.h"
|
|
|
|
#include "qemu/id.h"
|
2018-04-17 22:41:17 +08:00
|
|
|
#include "qemu/main-loop.h"
|
2018-04-13 23:19:31 +08:00
|
|
|
#include "trace-root.h"
|
2018-04-12 23:29:59 +08:00
|
|
|
|
2018-04-12 23:54:37 +08:00
|
|
|
static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
|
|
|
|
|
2018-04-13 23:19:31 +08:00
|
|
|
/* Job State Transition Table */
|
|
|
|
bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = {
|
|
|
|
/* U, C, R, P, Y, S, W, D, X, E, N */
|
|
|
|
/* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
/* C: */ [JOB_STATUS_CREATED] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
|
|
|
|
/* R: */ [JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
|
|
|
|
/* P: */ [JOB_STATUS_PAUSED] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
/* Y: */ [JOB_STATUS_READY] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
|
|
|
|
/* S: */ [JOB_STATUS_STANDBY] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
|
|
|
|
/* W: */ [JOB_STATUS_WAITING] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
|
|
|
|
/* D: */ [JOB_STATUS_PENDING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
|
|
|
|
/* X: */ [JOB_STATUS_ABORTING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
|
|
|
|
/* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
|
|
|
/* N: */ [JOB_STATUS_NULL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = {
|
|
|
|
/* U, C, R, P, Y, S, W, D, X, E, N */
|
|
|
|
[JOB_VERB_CANCEL] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
|
|
|
|
[JOB_VERB_PAUSE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
|
|
|
|
[JOB_VERB_RESUME] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
|
|
|
|
[JOB_VERB_SET_SPEED] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
|
|
|
|
[JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
|
|
|
|
[JOB_VERB_FINALIZE] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
|
|
|
|
[JOB_VERB_DISMISS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
/* TODO Make static once the whole state machine is in job.c */
|
|
|
|
void job_state_transition(Job *job, JobStatus s1)
|
|
|
|
{
|
|
|
|
JobStatus s0 = job->status;
|
|
|
|
assert(s1 >= 0 && s1 <= JOB_STATUS__MAX);
|
|
|
|
trace_job_state_transition(job, /* TODO re-enable: job->ret */ 0,
|
|
|
|
JobSTT[s0][s1] ? "allowed" : "disallowed",
|
|
|
|
JobStatus_str(s0), JobStatus_str(s1));
|
|
|
|
assert(JobSTT[s0][s1]);
|
|
|
|
job->status = s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int job_apply_verb(Job *job, JobVerb verb, Error **errp)
|
|
|
|
{
|
|
|
|
JobStatus s0 = job->status;
|
|
|
|
assert(verb >= 0 && verb <= JOB_VERB__MAX);
|
|
|
|
trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb),
|
|
|
|
JobVerbTable[verb][s0] ? "allowed" : "prohibited");
|
|
|
|
if (JobVerbTable[verb][s0]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'",
|
|
|
|
job->id, JobStatus_str(s0), JobVerb_str(verb));
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
2018-04-12 23:57:08 +08:00
|
|
|
JobType job_type(const Job *job)
|
|
|
|
{
|
|
|
|
return job->driver->job_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *job_type_str(const Job *job)
|
|
|
|
{
|
|
|
|
return JobType_str(job_type(job));
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:56:07 +08:00
|
|
|
bool job_is_cancelled(Job *job)
|
|
|
|
{
|
|
|
|
return job->cancelled;
|
|
|
|
}
|
|
|
|
|
2018-04-12 23:54:37 +08:00
|
|
|
Job *job_next(Job *job)
|
|
|
|
{
|
|
|
|
if (!job) {
|
|
|
|
return QLIST_FIRST(&jobs);
|
|
|
|
}
|
|
|
|
return QLIST_NEXT(job, job_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
Job *job_get(const char *id)
|
|
|
|
{
|
|
|
|
Job *job;
|
|
|
|
|
|
|
|
QLIST_FOREACH(job, &jobs, job_list) {
|
|
|
|
if (job->id && !strcmp(id, job->id)) {
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-17 19:49:33 +08:00
|
|
|
void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
|
|
|
|
Error **errp)
|
2018-04-12 23:29:59 +08:00
|
|
|
{
|
|
|
|
Job *job;
|
|
|
|
|
|
|
|
if (job_id) {
|
|
|
|
if (!id_wellformed(job_id)) {
|
|
|
|
error_setg(errp, "Invalid job ID '%s'", job_id);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-04-12 23:54:37 +08:00
|
|
|
if (job_get(job_id)) {
|
|
|
|
error_setg(errp, "Job ID '%s' already in use", job_id);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-04-12 23:29:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
job = g_malloc0(driver->instance_size);
|
|
|
|
job->driver = driver;
|
|
|
|
job->id = g_strdup(job_id);
|
2018-04-14 00:50:05 +08:00
|
|
|
job->refcnt = 1;
|
2018-04-17 19:49:33 +08:00
|
|
|
job->aio_context = ctx;
|
2018-04-12 23:29:59 +08:00
|
|
|
|
2018-04-13 23:19:31 +08:00
|
|
|
job_state_transition(job, JOB_STATUS_CREATED);
|
|
|
|
|
2018-04-12 23:54:37 +08:00
|
|
|
QLIST_INSERT_HEAD(&jobs, job, job_list);
|
|
|
|
|
2018-04-12 23:29:59 +08:00
|
|
|
return job;
|
|
|
|
}
|
2018-04-13 01:06:53 +08:00
|
|
|
|
2018-04-14 00:50:05 +08:00
|
|
|
void job_ref(Job *job)
|
2018-04-13 01:06:53 +08:00
|
|
|
{
|
2018-04-14 00:50:05 +08:00
|
|
|
++job->refcnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void job_unref(Job *job)
|
|
|
|
{
|
|
|
|
if (--job->refcnt == 0) {
|
|
|
|
assert(job->status == JOB_STATUS_NULL);
|
2018-04-12 23:54:37 +08:00
|
|
|
|
2018-04-14 00:50:05 +08:00
|
|
|
if (job->driver->free) {
|
|
|
|
job->driver->free(job);
|
|
|
|
}
|
|
|
|
|
|
|
|
QLIST_REMOVE(job, job_list);
|
|
|
|
|
|
|
|
g_free(job->id);
|
|
|
|
g_free(job);
|
|
|
|
}
|
2018-04-13 01:06:53 +08:00
|
|
|
}
|
2018-04-17 22:41:17 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Job *job;
|
|
|
|
JobDeferToMainLoopFn *fn;
|
|
|
|
void *opaque;
|
|
|
|
} JobDeferToMainLoopData;
|
|
|
|
|
|
|
|
static void job_defer_to_main_loop_bh(void *opaque)
|
|
|
|
{
|
|
|
|
JobDeferToMainLoopData *data = opaque;
|
|
|
|
Job *job = data->job;
|
|
|
|
AioContext *aio_context = job->aio_context;
|
|
|
|
|
|
|
|
aio_context_acquire(aio_context);
|
|
|
|
data->fn(data->job, data->opaque);
|
|
|
|
aio_context_release(aio_context);
|
|
|
|
|
|
|
|
g_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void job_defer_to_main_loop(Job *job, JobDeferToMainLoopFn *fn, void *opaque)
|
|
|
|
{
|
|
|
|
JobDeferToMainLoopData *data = g_malloc(sizeof(*data));
|
|
|
|
data->job = job;
|
|
|
|
data->fn = fn;
|
|
|
|
data->opaque = opaque;
|
|
|
|
job->deferred_to_main_loop = true;
|
|
|
|
|
|
|
|
aio_bh_schedule_oneshot(qemu_get_aio_context(),
|
|
|
|
job_defer_to_main_loop_bh, data);
|
|
|
|
}
|