1*1a90bc81SKevin Wolf /* 2*1a90bc81SKevin Wolf * QMP interface for background jobs 3*1a90bc81SKevin Wolf * 4*1a90bc81SKevin Wolf * Copyright (c) 2011 IBM Corp. 5*1a90bc81SKevin Wolf * Copyright (c) 2012, 2018 Red Hat, Inc. 6*1a90bc81SKevin Wolf * 7*1a90bc81SKevin Wolf * Permission is hereby granted, free of charge, to any person obtaining a copy 8*1a90bc81SKevin Wolf * of this software and associated documentation files (the "Software"), to deal 9*1a90bc81SKevin Wolf * in the Software without restriction, including without limitation the rights 10*1a90bc81SKevin Wolf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11*1a90bc81SKevin Wolf * copies of the Software, and to permit persons to whom the Software is 12*1a90bc81SKevin Wolf * furnished to do so, subject to the following conditions: 13*1a90bc81SKevin Wolf * 14*1a90bc81SKevin Wolf * The above copyright notice and this permission notice shall be included in 15*1a90bc81SKevin Wolf * all copies or substantial portions of the Software. 16*1a90bc81SKevin Wolf * 17*1a90bc81SKevin Wolf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18*1a90bc81SKevin Wolf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19*1a90bc81SKevin Wolf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20*1a90bc81SKevin Wolf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21*1a90bc81SKevin Wolf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22*1a90bc81SKevin Wolf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23*1a90bc81SKevin Wolf * THE SOFTWARE. 24*1a90bc81SKevin Wolf */ 25*1a90bc81SKevin Wolf 26*1a90bc81SKevin Wolf #include "qemu/osdep.h" 27*1a90bc81SKevin Wolf #include "qemu-common.h" 28*1a90bc81SKevin Wolf #include "qemu/job.h" 29*1a90bc81SKevin Wolf #include "qapi/qapi-commands-job.h" 30*1a90bc81SKevin Wolf #include "qapi/error.h" 31*1a90bc81SKevin Wolf #include "trace-root.h" 32*1a90bc81SKevin Wolf 33*1a90bc81SKevin Wolf /* Get a job using its ID and acquire its AioContext */ 34*1a90bc81SKevin Wolf static Job *find_job(const char *id, AioContext **aio_context, Error **errp) 35*1a90bc81SKevin Wolf { 36*1a90bc81SKevin Wolf Job *job; 37*1a90bc81SKevin Wolf 38*1a90bc81SKevin Wolf *aio_context = NULL; 39*1a90bc81SKevin Wolf 40*1a90bc81SKevin Wolf job = job_get(id); 41*1a90bc81SKevin Wolf if (!job) { 42*1a90bc81SKevin Wolf error_setg(errp, "Job not found"); 43*1a90bc81SKevin Wolf return NULL; 44*1a90bc81SKevin Wolf } 45*1a90bc81SKevin Wolf 46*1a90bc81SKevin Wolf *aio_context = job->aio_context; 47*1a90bc81SKevin Wolf aio_context_acquire(*aio_context); 48*1a90bc81SKevin Wolf 49*1a90bc81SKevin Wolf return job; 50*1a90bc81SKevin Wolf } 51*1a90bc81SKevin Wolf 52*1a90bc81SKevin Wolf void qmp_job_cancel(const char *id, Error **errp) 53*1a90bc81SKevin Wolf { 54*1a90bc81SKevin Wolf AioContext *aio_context; 55*1a90bc81SKevin Wolf Job *job = find_job(id, &aio_context, errp); 56*1a90bc81SKevin Wolf 57*1a90bc81SKevin Wolf if (!job) { 58*1a90bc81SKevin Wolf return; 59*1a90bc81SKevin Wolf } 60*1a90bc81SKevin Wolf 61*1a90bc81SKevin Wolf trace_qmp_job_cancel(job); 62*1a90bc81SKevin Wolf job_user_cancel(job, true, errp); 63*1a90bc81SKevin Wolf aio_context_release(aio_context); 64*1a90bc81SKevin Wolf } 65*1a90bc81SKevin Wolf 66*1a90bc81SKevin Wolf void qmp_job_pause(const char *id, Error **errp) 67*1a90bc81SKevin Wolf { 68*1a90bc81SKevin Wolf AioContext *aio_context; 69*1a90bc81SKevin Wolf Job *job = find_job(id, &aio_context, errp); 70*1a90bc81SKevin Wolf 71*1a90bc81SKevin Wolf if (!job) { 72*1a90bc81SKevin Wolf return; 73*1a90bc81SKevin Wolf } 74*1a90bc81SKevin Wolf 75*1a90bc81SKevin Wolf trace_qmp_job_pause(job); 76*1a90bc81SKevin Wolf job_user_pause(job, errp); 77*1a90bc81SKevin Wolf aio_context_release(aio_context); 78*1a90bc81SKevin Wolf } 79*1a90bc81SKevin Wolf 80*1a90bc81SKevin Wolf void qmp_job_resume(const char *id, Error **errp) 81*1a90bc81SKevin Wolf { 82*1a90bc81SKevin Wolf AioContext *aio_context; 83*1a90bc81SKevin Wolf Job *job = find_job(id, &aio_context, errp); 84*1a90bc81SKevin Wolf 85*1a90bc81SKevin Wolf if (!job) { 86*1a90bc81SKevin Wolf return; 87*1a90bc81SKevin Wolf } 88*1a90bc81SKevin Wolf 89*1a90bc81SKevin Wolf trace_qmp_job_resume(job); 90*1a90bc81SKevin Wolf job_user_resume(job, errp); 91*1a90bc81SKevin Wolf aio_context_release(aio_context); 92*1a90bc81SKevin Wolf } 93*1a90bc81SKevin Wolf 94*1a90bc81SKevin Wolf void qmp_job_complete(const char *id, Error **errp) 95*1a90bc81SKevin Wolf { 96*1a90bc81SKevin Wolf AioContext *aio_context; 97*1a90bc81SKevin Wolf Job *job = find_job(id, &aio_context, errp); 98*1a90bc81SKevin Wolf 99*1a90bc81SKevin Wolf if (!job) { 100*1a90bc81SKevin Wolf return; 101*1a90bc81SKevin Wolf } 102*1a90bc81SKevin Wolf 103*1a90bc81SKevin Wolf trace_qmp_job_complete(job); 104*1a90bc81SKevin Wolf job_complete(job, errp); 105*1a90bc81SKevin Wolf aio_context_release(aio_context); 106*1a90bc81SKevin Wolf } 107*1a90bc81SKevin Wolf 108*1a90bc81SKevin Wolf void qmp_job_finalize(const char *id, Error **errp) 109*1a90bc81SKevin Wolf { 110*1a90bc81SKevin Wolf AioContext *aio_context; 111*1a90bc81SKevin Wolf Job *job = find_job(id, &aio_context, errp); 112*1a90bc81SKevin Wolf 113*1a90bc81SKevin Wolf if (!job) { 114*1a90bc81SKevin Wolf return; 115*1a90bc81SKevin Wolf } 116*1a90bc81SKevin Wolf 117*1a90bc81SKevin Wolf trace_qmp_job_finalize(job); 118*1a90bc81SKevin Wolf job_finalize(job, errp); 119*1a90bc81SKevin Wolf aio_context_release(aio_context); 120*1a90bc81SKevin Wolf } 121*1a90bc81SKevin Wolf 122*1a90bc81SKevin Wolf void qmp_job_dismiss(const char *id, Error **errp) 123*1a90bc81SKevin Wolf { 124*1a90bc81SKevin Wolf AioContext *aio_context; 125*1a90bc81SKevin Wolf Job *job = find_job(id, &aio_context, errp); 126*1a90bc81SKevin Wolf 127*1a90bc81SKevin Wolf if (!job) { 128*1a90bc81SKevin Wolf return; 129*1a90bc81SKevin Wolf } 130*1a90bc81SKevin Wolf 131*1a90bc81SKevin Wolf trace_qmp_job_dismiss(job); 132*1a90bc81SKevin Wolf job_dismiss(&job, errp); 133*1a90bc81SKevin Wolf aio_context_release(aio_context); 134*1a90bc81SKevin Wolf } 135