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*a50c2ab8SKevin Wolf #include "trace-root.h" 3233e9e9bdSKevin Wolf 33e7c1d78bSKevin Wolf static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs); 34e7c1d78bSKevin Wolf 35*a50c2ab8SKevin Wolf /* Job State Transition Table */ 36*a50c2ab8SKevin Wolf bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = { 37*a50c2ab8SKevin Wolf /* U, C, R, P, Y, S, W, D, X, E, N */ 38*a50c2ab8SKevin Wolf /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 39*a50c2ab8SKevin Wolf /* C: */ [JOB_STATUS_CREATED] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}, 40*a50c2ab8SKevin Wolf /* R: */ [JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0}, 41*a50c2ab8SKevin Wolf /* P: */ [JOB_STATUS_PAUSED] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 42*a50c2ab8SKevin Wolf /* Y: */ [JOB_STATUS_READY] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0}, 43*a50c2ab8SKevin Wolf /* S: */ [JOB_STATUS_STANDBY] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 44*a50c2ab8SKevin Wolf /* W: */ [JOB_STATUS_WAITING] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0}, 45*a50c2ab8SKevin Wolf /* D: */ [JOB_STATUS_PENDING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, 46*a50c2ab8SKevin Wolf /* X: */ [JOB_STATUS_ABORTING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, 47*a50c2ab8SKevin Wolf /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 48*a50c2ab8SKevin Wolf /* N: */ [JOB_STATUS_NULL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 49*a50c2ab8SKevin Wolf }; 50*a50c2ab8SKevin Wolf 51*a50c2ab8SKevin Wolf bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = { 52*a50c2ab8SKevin Wolf /* U, C, R, P, Y, S, W, D, X, E, N */ 53*a50c2ab8SKevin Wolf [JOB_VERB_CANCEL] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, 54*a50c2ab8SKevin Wolf [JOB_VERB_PAUSE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 55*a50c2ab8SKevin Wolf [JOB_VERB_RESUME] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 56*a50c2ab8SKevin Wolf [JOB_VERB_SET_SPEED] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 57*a50c2ab8SKevin Wolf [JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 58*a50c2ab8SKevin Wolf [JOB_VERB_FINALIZE] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, 59*a50c2ab8SKevin Wolf [JOB_VERB_DISMISS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, 60*a50c2ab8SKevin Wolf }; 61*a50c2ab8SKevin Wolf 62*a50c2ab8SKevin Wolf /* TODO Make static once the whole state machine is in job.c */ 63*a50c2ab8SKevin Wolf void job_state_transition(Job *job, JobStatus s1) 64*a50c2ab8SKevin Wolf { 65*a50c2ab8SKevin Wolf JobStatus s0 = job->status; 66*a50c2ab8SKevin Wolf assert(s1 >= 0 && s1 <= JOB_STATUS__MAX); 67*a50c2ab8SKevin Wolf trace_job_state_transition(job, /* TODO re-enable: job->ret */ 0, 68*a50c2ab8SKevin Wolf JobSTT[s0][s1] ? "allowed" : "disallowed", 69*a50c2ab8SKevin Wolf JobStatus_str(s0), JobStatus_str(s1)); 70*a50c2ab8SKevin Wolf assert(JobSTT[s0][s1]); 71*a50c2ab8SKevin Wolf job->status = s1; 72*a50c2ab8SKevin Wolf } 73*a50c2ab8SKevin Wolf 74*a50c2ab8SKevin Wolf int job_apply_verb(Job *job, JobVerb verb, Error **errp) 75*a50c2ab8SKevin Wolf { 76*a50c2ab8SKevin Wolf JobStatus s0 = job->status; 77*a50c2ab8SKevin Wolf assert(verb >= 0 && verb <= JOB_VERB__MAX); 78*a50c2ab8SKevin Wolf trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb), 79*a50c2ab8SKevin Wolf JobVerbTable[verb][s0] ? "allowed" : "prohibited"); 80*a50c2ab8SKevin Wolf if (JobVerbTable[verb][s0]) { 81*a50c2ab8SKevin Wolf return 0; 82*a50c2ab8SKevin Wolf } 83*a50c2ab8SKevin Wolf error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'", 84*a50c2ab8SKevin Wolf job->id, JobStatus_str(s0), JobVerb_str(verb)); 85*a50c2ab8SKevin Wolf return -EPERM; 86*a50c2ab8SKevin Wolf } 87*a50c2ab8SKevin 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); 13733e9e9bdSKevin Wolf 138*a50c2ab8SKevin Wolf job_state_transition(job, JOB_STATUS_CREATED); 139*a50c2ab8SKevin Wolf 140e7c1d78bSKevin Wolf QLIST_INSERT_HEAD(&jobs, job, job_list); 141e7c1d78bSKevin Wolf 14233e9e9bdSKevin Wolf return job; 14333e9e9bdSKevin Wolf } 144fd61a701SKevin Wolf 145fd61a701SKevin Wolf void job_delete(Job *job) 146fd61a701SKevin Wolf { 147e7c1d78bSKevin Wolf QLIST_REMOVE(job, job_list); 148e7c1d78bSKevin Wolf 149fd61a701SKevin Wolf g_free(job->id); 150fd61a701SKevin Wolf g_free(job); 151fd61a701SKevin Wolf } 152