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" 31a50c2ab8SKevin Wolf #include "trace-root.h" 3233e9e9bdSKevin Wolf 33e7c1d78bSKevin Wolf static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs); 34e7c1d78bSKevin Wolf 35a50c2ab8SKevin Wolf /* Job State Transition Table */ 36a50c2ab8SKevin Wolf bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = { 37a50c2ab8SKevin Wolf /* U, C, R, P, Y, S, W, D, X, E, N */ 38a50c2ab8SKevin Wolf /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 39a50c2ab8SKevin Wolf /* C: */ [JOB_STATUS_CREATED] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}, 40a50c2ab8SKevin Wolf /* R: */ [JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0}, 41a50c2ab8SKevin Wolf /* P: */ [JOB_STATUS_PAUSED] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 42a50c2ab8SKevin Wolf /* Y: */ [JOB_STATUS_READY] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0}, 43a50c2ab8SKevin Wolf /* S: */ [JOB_STATUS_STANDBY] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 44a50c2ab8SKevin Wolf /* W: */ [JOB_STATUS_WAITING] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0}, 45a50c2ab8SKevin Wolf /* D: */ [JOB_STATUS_PENDING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, 46a50c2ab8SKevin Wolf /* X: */ [JOB_STATUS_ABORTING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, 47a50c2ab8SKevin Wolf /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 48a50c2ab8SKevin Wolf /* N: */ [JOB_STATUS_NULL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 49a50c2ab8SKevin Wolf }; 50a50c2ab8SKevin Wolf 51a50c2ab8SKevin Wolf bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = { 52a50c2ab8SKevin Wolf /* U, C, R, P, Y, S, W, D, X, E, N */ 53a50c2ab8SKevin Wolf [JOB_VERB_CANCEL] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, 54a50c2ab8SKevin Wolf [JOB_VERB_PAUSE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 55a50c2ab8SKevin Wolf [JOB_VERB_RESUME] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 56a50c2ab8SKevin Wolf [JOB_VERB_SET_SPEED] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 57a50c2ab8SKevin Wolf [JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 58a50c2ab8SKevin Wolf [JOB_VERB_FINALIZE] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, 59a50c2ab8SKevin Wolf [JOB_VERB_DISMISS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, 60a50c2ab8SKevin Wolf }; 61a50c2ab8SKevin Wolf 62a50c2ab8SKevin Wolf /* TODO Make static once the whole state machine is in job.c */ 63a50c2ab8SKevin Wolf void job_state_transition(Job *job, JobStatus s1) 64a50c2ab8SKevin Wolf { 65a50c2ab8SKevin Wolf JobStatus s0 = job->status; 66a50c2ab8SKevin Wolf assert(s1 >= 0 && s1 <= JOB_STATUS__MAX); 67a50c2ab8SKevin Wolf trace_job_state_transition(job, /* TODO re-enable: job->ret */ 0, 68a50c2ab8SKevin Wolf JobSTT[s0][s1] ? "allowed" : "disallowed", 69a50c2ab8SKevin Wolf JobStatus_str(s0), JobStatus_str(s1)); 70a50c2ab8SKevin Wolf assert(JobSTT[s0][s1]); 71a50c2ab8SKevin Wolf job->status = s1; 72a50c2ab8SKevin Wolf } 73a50c2ab8SKevin Wolf 74a50c2ab8SKevin Wolf int job_apply_verb(Job *job, JobVerb verb, Error **errp) 75a50c2ab8SKevin Wolf { 76a50c2ab8SKevin Wolf JobStatus s0 = job->status; 77a50c2ab8SKevin Wolf assert(verb >= 0 && verb <= JOB_VERB__MAX); 78a50c2ab8SKevin Wolf trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb), 79a50c2ab8SKevin Wolf JobVerbTable[verb][s0] ? "allowed" : "prohibited"); 80a50c2ab8SKevin Wolf if (JobVerbTable[verb][s0]) { 81a50c2ab8SKevin Wolf return 0; 82a50c2ab8SKevin Wolf } 83a50c2ab8SKevin Wolf error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'", 84a50c2ab8SKevin Wolf job->id, JobStatus_str(s0), JobVerb_str(verb)); 85a50c2ab8SKevin Wolf return -EPERM; 86a50c2ab8SKevin Wolf } 87a50c2ab8SKevin Wolf 88252291eaSKevin Wolf JobType job_type(const Job *job) 89252291eaSKevin Wolf { 90252291eaSKevin Wolf return job->driver->job_type; 91252291eaSKevin Wolf } 92252291eaSKevin Wolf 93252291eaSKevin Wolf const char *job_type_str(const Job *job) 94252291eaSKevin Wolf { 95252291eaSKevin Wolf return JobType_str(job_type(job)); 96252291eaSKevin Wolf } 97252291eaSKevin Wolf 98e7c1d78bSKevin Wolf Job *job_next(Job *job) 99e7c1d78bSKevin Wolf { 100e7c1d78bSKevin Wolf if (!job) { 101e7c1d78bSKevin Wolf return QLIST_FIRST(&jobs); 102e7c1d78bSKevin Wolf } 103e7c1d78bSKevin Wolf return QLIST_NEXT(job, job_list); 104e7c1d78bSKevin Wolf } 105e7c1d78bSKevin Wolf 106e7c1d78bSKevin Wolf Job *job_get(const char *id) 107e7c1d78bSKevin Wolf { 108e7c1d78bSKevin Wolf Job *job; 109e7c1d78bSKevin Wolf 110e7c1d78bSKevin Wolf QLIST_FOREACH(job, &jobs, job_list) { 111e7c1d78bSKevin Wolf if (job->id && !strcmp(id, job->id)) { 112e7c1d78bSKevin Wolf return job; 113e7c1d78bSKevin Wolf } 114e7c1d78bSKevin Wolf } 115e7c1d78bSKevin Wolf 116e7c1d78bSKevin Wolf return NULL; 117e7c1d78bSKevin Wolf } 118e7c1d78bSKevin Wolf 11933e9e9bdSKevin Wolf void *job_create(const char *job_id, const JobDriver *driver, Error **errp) 12033e9e9bdSKevin Wolf { 12133e9e9bdSKevin Wolf Job *job; 12233e9e9bdSKevin Wolf 12333e9e9bdSKevin Wolf if (job_id) { 12433e9e9bdSKevin Wolf if (!id_wellformed(job_id)) { 12533e9e9bdSKevin Wolf error_setg(errp, "Invalid job ID '%s'", job_id); 12633e9e9bdSKevin Wolf return NULL; 12733e9e9bdSKevin Wolf } 128e7c1d78bSKevin Wolf if (job_get(job_id)) { 129e7c1d78bSKevin Wolf error_setg(errp, "Job ID '%s' already in use", job_id); 130e7c1d78bSKevin Wolf return NULL; 131e7c1d78bSKevin Wolf } 13233e9e9bdSKevin Wolf } 13333e9e9bdSKevin Wolf 13433e9e9bdSKevin Wolf job = g_malloc0(driver->instance_size); 13533e9e9bdSKevin Wolf job->driver = driver; 13633e9e9bdSKevin Wolf job->id = g_strdup(job_id); 137*80fa2c75SKevin Wolf job->refcnt = 1; 13833e9e9bdSKevin Wolf 139a50c2ab8SKevin Wolf job_state_transition(job, JOB_STATUS_CREATED); 140a50c2ab8SKevin Wolf 141e7c1d78bSKevin Wolf QLIST_INSERT_HEAD(&jobs, job, job_list); 142e7c1d78bSKevin Wolf 14333e9e9bdSKevin Wolf return job; 14433e9e9bdSKevin Wolf } 145fd61a701SKevin Wolf 146*80fa2c75SKevin Wolf void job_ref(Job *job) 147fd61a701SKevin Wolf { 148*80fa2c75SKevin Wolf ++job->refcnt; 149*80fa2c75SKevin Wolf } 150*80fa2c75SKevin Wolf 151*80fa2c75SKevin Wolf void job_unref(Job *job) 152*80fa2c75SKevin Wolf { 153*80fa2c75SKevin Wolf if (--job->refcnt == 0) { 154*80fa2c75SKevin Wolf assert(job->status == JOB_STATUS_NULL); 155*80fa2c75SKevin Wolf 156*80fa2c75SKevin Wolf if (job->driver->free) { 157*80fa2c75SKevin Wolf job->driver->free(job); 158*80fa2c75SKevin Wolf } 159*80fa2c75SKevin Wolf 160e7c1d78bSKevin Wolf QLIST_REMOVE(job, job_list); 161e7c1d78bSKevin Wolf 162fd61a701SKevin Wolf g_free(job->id); 163fd61a701SKevin Wolf g_free(job); 164fd61a701SKevin Wolf } 165*80fa2c75SKevin Wolf } 166