133e9e9bdSKevin Wolf /* 233e9e9bdSKevin Wolf * Background jobs (long-running operations) 333e9e9bdSKevin Wolf * 433e9e9bdSKevin Wolf * Copyright (c) 2011 IBM Corp. 533e9e9bdSKevin Wolf * Copyright (c) 2012, 2018 Red Hat, Inc. 633e9e9bdSKevin Wolf * 733e9e9bdSKevin Wolf * Permission is hereby granted, free of charge, to any person obtaining a copy 833e9e9bdSKevin Wolf * of this software and associated documentation files (the "Software"), to deal 933e9e9bdSKevin Wolf * in the Software without restriction, including without limitation the rights 1033e9e9bdSKevin Wolf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1133e9e9bdSKevin Wolf * copies of the Software, and to permit persons to whom the Software is 1233e9e9bdSKevin Wolf * furnished to do so, subject to the following conditions: 1333e9e9bdSKevin Wolf * 1433e9e9bdSKevin Wolf * The above copyright notice and this permission notice shall be included in 1533e9e9bdSKevin Wolf * all copies or substantial portions of the Software. 1633e9e9bdSKevin Wolf * 1733e9e9bdSKevin Wolf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1833e9e9bdSKevin Wolf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1933e9e9bdSKevin Wolf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2033e9e9bdSKevin Wolf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2133e9e9bdSKevin Wolf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2233e9e9bdSKevin Wolf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2333e9e9bdSKevin Wolf * THE SOFTWARE. 2433e9e9bdSKevin Wolf */ 2533e9e9bdSKevin Wolf 2633e9e9bdSKevin Wolf #include "qemu/osdep.h" 2733e9e9bdSKevin Wolf #include "qemu-common.h" 2833e9e9bdSKevin Wolf #include "qapi/error.h" 2933e9e9bdSKevin Wolf #include "qemu/job.h" 3033e9e9bdSKevin Wolf #include "qemu/id.h" 31*1908a559SKevin Wolf #include "qemu/main-loop.h" 32a50c2ab8SKevin Wolf #include "trace-root.h" 3333e9e9bdSKevin Wolf 34e7c1d78bSKevin Wolf static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs); 35e7c1d78bSKevin Wolf 36a50c2ab8SKevin Wolf /* Job State Transition Table */ 37a50c2ab8SKevin Wolf bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = { 38a50c2ab8SKevin Wolf /* U, C, R, P, Y, S, W, D, X, E, N */ 39a50c2ab8SKevin Wolf /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 40a50c2ab8SKevin Wolf /* C: */ [JOB_STATUS_CREATED] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}, 41a50c2ab8SKevin Wolf /* R: */ [JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0}, 42a50c2ab8SKevin Wolf /* P: */ [JOB_STATUS_PAUSED] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 43a50c2ab8SKevin Wolf /* Y: */ [JOB_STATUS_READY] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0}, 44a50c2ab8SKevin Wolf /* S: */ [JOB_STATUS_STANDBY] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 45a50c2ab8SKevin Wolf /* W: */ [JOB_STATUS_WAITING] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0}, 46a50c2ab8SKevin Wolf /* D: */ [JOB_STATUS_PENDING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, 47a50c2ab8SKevin Wolf /* X: */ [JOB_STATUS_ABORTING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, 48a50c2ab8SKevin Wolf /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 49a50c2ab8SKevin Wolf /* N: */ [JOB_STATUS_NULL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 50a50c2ab8SKevin Wolf }; 51a50c2ab8SKevin Wolf 52a50c2ab8SKevin Wolf bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = { 53a50c2ab8SKevin Wolf /* U, C, R, P, Y, S, W, D, X, E, N */ 54a50c2ab8SKevin Wolf [JOB_VERB_CANCEL] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, 55a50c2ab8SKevin Wolf [JOB_VERB_PAUSE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 56a50c2ab8SKevin Wolf [JOB_VERB_RESUME] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 57a50c2ab8SKevin Wolf [JOB_VERB_SET_SPEED] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 58a50c2ab8SKevin Wolf [JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 59a50c2ab8SKevin Wolf [JOB_VERB_FINALIZE] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, 60a50c2ab8SKevin Wolf [JOB_VERB_DISMISS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, 61a50c2ab8SKevin Wolf }; 62a50c2ab8SKevin Wolf 63a50c2ab8SKevin Wolf /* TODO Make static once the whole state machine is in job.c */ 64a50c2ab8SKevin Wolf void job_state_transition(Job *job, JobStatus s1) 65a50c2ab8SKevin Wolf { 66a50c2ab8SKevin Wolf JobStatus s0 = job->status; 67a50c2ab8SKevin Wolf assert(s1 >= 0 && s1 <= JOB_STATUS__MAX); 68a50c2ab8SKevin Wolf trace_job_state_transition(job, /* TODO re-enable: job->ret */ 0, 69a50c2ab8SKevin Wolf JobSTT[s0][s1] ? "allowed" : "disallowed", 70a50c2ab8SKevin Wolf JobStatus_str(s0), JobStatus_str(s1)); 71a50c2ab8SKevin Wolf assert(JobSTT[s0][s1]); 72a50c2ab8SKevin Wolf job->status = s1; 73a50c2ab8SKevin Wolf } 74a50c2ab8SKevin Wolf 75a50c2ab8SKevin Wolf int job_apply_verb(Job *job, JobVerb verb, Error **errp) 76a50c2ab8SKevin Wolf { 77a50c2ab8SKevin Wolf JobStatus s0 = job->status; 78a50c2ab8SKevin Wolf assert(verb >= 0 && verb <= JOB_VERB__MAX); 79a50c2ab8SKevin Wolf trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb), 80a50c2ab8SKevin Wolf JobVerbTable[verb][s0] ? "allowed" : "prohibited"); 81a50c2ab8SKevin Wolf if (JobVerbTable[verb][s0]) { 82a50c2ab8SKevin Wolf return 0; 83a50c2ab8SKevin Wolf } 84a50c2ab8SKevin Wolf error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'", 85a50c2ab8SKevin Wolf job->id, JobStatus_str(s0), JobVerb_str(verb)); 86a50c2ab8SKevin Wolf return -EPERM; 87a50c2ab8SKevin Wolf } 88a50c2ab8SKevin Wolf 89252291eaSKevin Wolf JobType job_type(const Job *job) 90252291eaSKevin Wolf { 91252291eaSKevin Wolf return job->driver->job_type; 92252291eaSKevin Wolf } 93252291eaSKevin Wolf 94252291eaSKevin Wolf const char *job_type_str(const Job *job) 95252291eaSKevin Wolf { 96252291eaSKevin Wolf return JobType_str(job_type(job)); 97252291eaSKevin Wolf } 98252291eaSKevin Wolf 99daa7f2f9SKevin Wolf bool job_is_cancelled(Job *job) 100daa7f2f9SKevin Wolf { 101daa7f2f9SKevin Wolf return job->cancelled; 102daa7f2f9SKevin Wolf } 103daa7f2f9SKevin Wolf 104e7c1d78bSKevin Wolf Job *job_next(Job *job) 105e7c1d78bSKevin Wolf { 106e7c1d78bSKevin Wolf if (!job) { 107e7c1d78bSKevin Wolf return QLIST_FIRST(&jobs); 108e7c1d78bSKevin Wolf } 109e7c1d78bSKevin Wolf return QLIST_NEXT(job, job_list); 110e7c1d78bSKevin Wolf } 111e7c1d78bSKevin Wolf 112e7c1d78bSKevin Wolf Job *job_get(const char *id) 113e7c1d78bSKevin Wolf { 114e7c1d78bSKevin Wolf Job *job; 115e7c1d78bSKevin Wolf 116e7c1d78bSKevin Wolf QLIST_FOREACH(job, &jobs, job_list) { 117e7c1d78bSKevin Wolf if (job->id && !strcmp(id, job->id)) { 118e7c1d78bSKevin Wolf return job; 119e7c1d78bSKevin Wolf } 120e7c1d78bSKevin Wolf } 121e7c1d78bSKevin Wolf 122e7c1d78bSKevin Wolf return NULL; 123e7c1d78bSKevin Wolf } 124e7c1d78bSKevin Wolf 12508be6fe2SKevin Wolf void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx, 12608be6fe2SKevin Wolf Error **errp) 12733e9e9bdSKevin Wolf { 12833e9e9bdSKevin Wolf Job *job; 12933e9e9bdSKevin Wolf 13033e9e9bdSKevin Wolf if (job_id) { 13133e9e9bdSKevin Wolf if (!id_wellformed(job_id)) { 13233e9e9bdSKevin Wolf error_setg(errp, "Invalid job ID '%s'", job_id); 13333e9e9bdSKevin Wolf return NULL; 13433e9e9bdSKevin Wolf } 135e7c1d78bSKevin Wolf if (job_get(job_id)) { 136e7c1d78bSKevin Wolf error_setg(errp, "Job ID '%s' already in use", job_id); 137e7c1d78bSKevin Wolf return NULL; 138e7c1d78bSKevin Wolf } 13933e9e9bdSKevin Wolf } 14033e9e9bdSKevin Wolf 14133e9e9bdSKevin Wolf job = g_malloc0(driver->instance_size); 14233e9e9bdSKevin Wolf job->driver = driver; 14333e9e9bdSKevin Wolf job->id = g_strdup(job_id); 14480fa2c75SKevin Wolf job->refcnt = 1; 14508be6fe2SKevin Wolf job->aio_context = ctx; 14633e9e9bdSKevin Wolf 147a50c2ab8SKevin Wolf job_state_transition(job, JOB_STATUS_CREATED); 148a50c2ab8SKevin Wolf 149e7c1d78bSKevin Wolf QLIST_INSERT_HEAD(&jobs, job, job_list); 150e7c1d78bSKevin Wolf 15133e9e9bdSKevin Wolf return job; 15233e9e9bdSKevin Wolf } 153fd61a701SKevin Wolf 15480fa2c75SKevin Wolf void job_ref(Job *job) 155fd61a701SKevin Wolf { 15680fa2c75SKevin Wolf ++job->refcnt; 15780fa2c75SKevin Wolf } 15880fa2c75SKevin Wolf 15980fa2c75SKevin Wolf void job_unref(Job *job) 16080fa2c75SKevin Wolf { 16180fa2c75SKevin Wolf if (--job->refcnt == 0) { 16280fa2c75SKevin Wolf assert(job->status == JOB_STATUS_NULL); 16380fa2c75SKevin Wolf 16480fa2c75SKevin Wolf if (job->driver->free) { 16580fa2c75SKevin Wolf job->driver->free(job); 16680fa2c75SKevin Wolf } 16780fa2c75SKevin Wolf 168e7c1d78bSKevin Wolf QLIST_REMOVE(job, job_list); 169e7c1d78bSKevin Wolf 170fd61a701SKevin Wolf g_free(job->id); 171fd61a701SKevin Wolf g_free(job); 172fd61a701SKevin Wolf } 17380fa2c75SKevin Wolf } 174*1908a559SKevin Wolf 175*1908a559SKevin Wolf typedef struct { 176*1908a559SKevin Wolf Job *job; 177*1908a559SKevin Wolf JobDeferToMainLoopFn *fn; 178*1908a559SKevin Wolf void *opaque; 179*1908a559SKevin Wolf } JobDeferToMainLoopData; 180*1908a559SKevin Wolf 181*1908a559SKevin Wolf static void job_defer_to_main_loop_bh(void *opaque) 182*1908a559SKevin Wolf { 183*1908a559SKevin Wolf JobDeferToMainLoopData *data = opaque; 184*1908a559SKevin Wolf Job *job = data->job; 185*1908a559SKevin Wolf AioContext *aio_context = job->aio_context; 186*1908a559SKevin Wolf 187*1908a559SKevin Wolf aio_context_acquire(aio_context); 188*1908a559SKevin Wolf data->fn(data->job, data->opaque); 189*1908a559SKevin Wolf aio_context_release(aio_context); 190*1908a559SKevin Wolf 191*1908a559SKevin Wolf g_free(data); 192*1908a559SKevin Wolf } 193*1908a559SKevin Wolf 194*1908a559SKevin Wolf void job_defer_to_main_loop(Job *job, JobDeferToMainLoopFn *fn, void *opaque) 195*1908a559SKevin Wolf { 196*1908a559SKevin Wolf JobDeferToMainLoopData *data = g_malloc(sizeof(*data)); 197*1908a559SKevin Wolf data->job = job; 198*1908a559SKevin Wolf data->fn = fn; 199*1908a559SKevin Wolf data->opaque = opaque; 200*1908a559SKevin Wolf job->deferred_to_main_loop = true; 201*1908a559SKevin Wolf 202*1908a559SKevin Wolf aio_bh_schedule_oneshot(qemu_get_aio_context(), 203*1908a559SKevin Wolf job_defer_to_main_loop_bh, data); 204*1908a559SKevin Wolf } 205