2
0

job.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * Background jobs (long-running operations)
  3. *
  4. * Copyright (c) 2011 IBM Corp.
  5. * Copyright (c) 2012, 2018 Red Hat, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qapi/error.h"
  27. #include "qemu/job.h"
  28. #include "qemu/id.h"
  29. #include "qemu/main-loop.h"
  30. #include "block/aio-wait.h"
  31. #include "trace-root.h"
  32. #include "qapi/qapi-events-job.h"
  33. static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
  34. /* Job State Transition Table */
  35. bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = {
  36. /* U, C, R, P, Y, S, W, D, X, E, N */
  37. /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  38. /* C: */ [JOB_STATUS_CREATED] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
  39. /* R: */ [JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
  40. /* P: */ [JOB_STATUS_PAUSED] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
  41. /* Y: */ [JOB_STATUS_READY] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
  42. /* S: */ [JOB_STATUS_STANDBY] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
  43. /* W: */ [JOB_STATUS_WAITING] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
  44. /* D: */ [JOB_STATUS_PENDING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
  45. /* X: */ [JOB_STATUS_ABORTING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
  46. /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  47. /* N: */ [JOB_STATUS_NULL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  48. };
  49. bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = {
  50. /* U, C, R, P, Y, S, W, D, X, E, N */
  51. [JOB_VERB_CANCEL] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
  52. [JOB_VERB_PAUSE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
  53. [JOB_VERB_RESUME] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
  54. [JOB_VERB_SET_SPEED] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
  55. [JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
  56. [JOB_VERB_FINALIZE] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
  57. [JOB_VERB_DISMISS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  58. };
  59. /* Transactional group of jobs */
  60. struct JobTxn {
  61. /* Is this txn being cancelled? */
  62. bool aborting;
  63. /* List of jobs */
  64. QLIST_HEAD(, Job) jobs;
  65. /* Reference count */
  66. int refcnt;
  67. };
  68. /* Right now, this mutex is only needed to synchronize accesses to job->busy
  69. * and job->sleep_timer, such as concurrent calls to job_do_yield and
  70. * job_enter. */
  71. static QemuMutex job_mutex;
  72. static void job_lock(void)
  73. {
  74. qemu_mutex_lock(&job_mutex);
  75. }
  76. static void job_unlock(void)
  77. {
  78. qemu_mutex_unlock(&job_mutex);
  79. }
  80. static void __attribute__((__constructor__)) job_init(void)
  81. {
  82. qemu_mutex_init(&job_mutex);
  83. }
  84. JobTxn *job_txn_new(void)
  85. {
  86. JobTxn *txn = g_new0(JobTxn, 1);
  87. QLIST_INIT(&txn->jobs);
  88. txn->refcnt = 1;
  89. return txn;
  90. }
  91. static void job_txn_ref(JobTxn *txn)
  92. {
  93. txn->refcnt++;
  94. }
  95. void job_txn_unref(JobTxn *txn)
  96. {
  97. if (txn && --txn->refcnt == 0) {
  98. g_free(txn);
  99. }
  100. }
  101. void job_txn_add_job(JobTxn *txn, Job *job)
  102. {
  103. if (!txn) {
  104. return;
  105. }
  106. assert(!job->txn);
  107. job->txn = txn;
  108. QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
  109. job_txn_ref(txn);
  110. }
  111. static void job_txn_del_job(Job *job)
  112. {
  113. if (job->txn) {
  114. QLIST_REMOVE(job, txn_list);
  115. job_txn_unref(job->txn);
  116. job->txn = NULL;
  117. }
  118. }
  119. static int job_txn_apply(Job *job, int fn(Job *))
  120. {
  121. AioContext *inner_ctx;
  122. Job *other_job, *next;
  123. JobTxn *txn = job->txn;
  124. int rc = 0;
  125. /*
  126. * Similar to job_completed_txn_abort, we take each job's lock before
  127. * applying fn, but since we assume that outer_ctx is held by the caller,
  128. * we need to release it here to avoid holding the lock twice - which would
  129. * break AIO_WAIT_WHILE from within fn.
  130. */
  131. job_ref(job);
  132. aio_context_release(job->aio_context);
  133. QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
  134. inner_ctx = other_job->aio_context;
  135. aio_context_acquire(inner_ctx);
  136. rc = fn(other_job);
  137. aio_context_release(inner_ctx);
  138. if (rc) {
  139. break;
  140. }
  141. }
  142. /*
  143. * Note that job->aio_context might have been changed by calling fn, so we
  144. * can't use a local variable to cache it.
  145. */
  146. aio_context_acquire(job->aio_context);
  147. job_unref(job);
  148. return rc;
  149. }
  150. bool job_is_internal(Job *job)
  151. {
  152. return (job->id == NULL);
  153. }
  154. static void job_state_transition(Job *job, JobStatus s1)
  155. {
  156. JobStatus s0 = job->status;
  157. assert(s1 >= 0 && s1 < JOB_STATUS__MAX);
  158. trace_job_state_transition(job, job->ret,
  159. JobSTT[s0][s1] ? "allowed" : "disallowed",
  160. JobStatus_str(s0), JobStatus_str(s1));
  161. assert(JobSTT[s0][s1]);
  162. job->status = s1;
  163. if (!job_is_internal(job) && s1 != s0) {
  164. qapi_event_send_job_status_change(job->id, job->status);
  165. }
  166. }
  167. int job_apply_verb(Job *job, JobVerb verb, Error **errp)
  168. {
  169. JobStatus s0 = job->status;
  170. assert(verb >= 0 && verb < JOB_VERB__MAX);
  171. trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb),
  172. JobVerbTable[verb][s0] ? "allowed" : "prohibited");
  173. if (JobVerbTable[verb][s0]) {
  174. return 0;
  175. }
  176. error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'",
  177. job->id, JobStatus_str(s0), JobVerb_str(verb));
  178. return -EPERM;
  179. }
  180. JobType job_type(const Job *job)
  181. {
  182. return job->driver->job_type;
  183. }
  184. const char *job_type_str(const Job *job)
  185. {
  186. return JobType_str(job_type(job));
  187. }
  188. bool job_is_cancelled(Job *job)
  189. {
  190. return job->cancelled;
  191. }
  192. bool job_is_ready(Job *job)
  193. {
  194. switch (job->status) {
  195. case JOB_STATUS_UNDEFINED:
  196. case JOB_STATUS_CREATED:
  197. case JOB_STATUS_RUNNING:
  198. case JOB_STATUS_PAUSED:
  199. case JOB_STATUS_WAITING:
  200. case JOB_STATUS_PENDING:
  201. case JOB_STATUS_ABORTING:
  202. case JOB_STATUS_CONCLUDED:
  203. case JOB_STATUS_NULL:
  204. return false;
  205. case JOB_STATUS_READY:
  206. case JOB_STATUS_STANDBY:
  207. return true;
  208. default:
  209. g_assert_not_reached();
  210. }
  211. return false;
  212. }
  213. bool job_is_completed(Job *job)
  214. {
  215. switch (job->status) {
  216. case JOB_STATUS_UNDEFINED:
  217. case JOB_STATUS_CREATED:
  218. case JOB_STATUS_RUNNING:
  219. case JOB_STATUS_PAUSED:
  220. case JOB_STATUS_READY:
  221. case JOB_STATUS_STANDBY:
  222. return false;
  223. case JOB_STATUS_WAITING:
  224. case JOB_STATUS_PENDING:
  225. case JOB_STATUS_ABORTING:
  226. case JOB_STATUS_CONCLUDED:
  227. case JOB_STATUS_NULL:
  228. return true;
  229. default:
  230. g_assert_not_reached();
  231. }
  232. return false;
  233. }
  234. static bool job_started(Job *job)
  235. {
  236. return job->co;
  237. }
  238. static bool job_should_pause(Job *job)
  239. {
  240. return job->pause_count > 0;
  241. }
  242. Job *job_next(Job *job)
  243. {
  244. if (!job) {
  245. return QLIST_FIRST(&jobs);
  246. }
  247. return QLIST_NEXT(job, job_list);
  248. }
  249. Job *job_get(const char *id)
  250. {
  251. Job *job;
  252. QLIST_FOREACH(job, &jobs, job_list) {
  253. if (job->id && !strcmp(id, job->id)) {
  254. return job;
  255. }
  256. }
  257. return NULL;
  258. }
  259. static void job_sleep_timer_cb(void *opaque)
  260. {
  261. Job *job = opaque;
  262. job_enter(job);
  263. }
  264. void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn,
  265. AioContext *ctx, int flags, BlockCompletionFunc *cb,
  266. void *opaque, Error **errp)
  267. {
  268. Job *job;
  269. if (job_id) {
  270. if (flags & JOB_INTERNAL) {
  271. error_setg(errp, "Cannot specify job ID for internal job");
  272. return NULL;
  273. }
  274. if (!id_wellformed(job_id)) {
  275. error_setg(errp, "Invalid job ID '%s'", job_id);
  276. return NULL;
  277. }
  278. if (job_get(job_id)) {
  279. error_setg(errp, "Job ID '%s' already in use", job_id);
  280. return NULL;
  281. }
  282. } else if (!(flags & JOB_INTERNAL)) {
  283. error_setg(errp, "An explicit job ID is required");
  284. return NULL;
  285. }
  286. job = g_malloc0(driver->instance_size);
  287. job->driver = driver;
  288. job->id = g_strdup(job_id);
  289. job->refcnt = 1;
  290. job->aio_context = ctx;
  291. job->busy = false;
  292. job->paused = true;
  293. job->pause_count = 1;
  294. job->auto_finalize = !(flags & JOB_MANUAL_FINALIZE);
  295. job->auto_dismiss = !(flags & JOB_MANUAL_DISMISS);
  296. job->cb = cb;
  297. job->opaque = opaque;
  298. notifier_list_init(&job->on_finalize_cancelled);
  299. notifier_list_init(&job->on_finalize_completed);
  300. notifier_list_init(&job->on_pending);
  301. notifier_list_init(&job->on_ready);
  302. job_state_transition(job, JOB_STATUS_CREATED);
  303. aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
  304. QEMU_CLOCK_REALTIME, SCALE_NS,
  305. job_sleep_timer_cb, job);
  306. QLIST_INSERT_HEAD(&jobs, job, job_list);
  307. /* Single jobs are modeled as single-job transactions for sake of
  308. * consolidating the job management logic */
  309. if (!txn) {
  310. txn = job_txn_new();
  311. job_txn_add_job(txn, job);
  312. job_txn_unref(txn);
  313. } else {
  314. job_txn_add_job(txn, job);
  315. }
  316. return job;
  317. }
  318. void job_ref(Job *job)
  319. {
  320. ++job->refcnt;
  321. }
  322. void job_unref(Job *job)
  323. {
  324. if (--job->refcnt == 0) {
  325. assert(job->status == JOB_STATUS_NULL);
  326. assert(!timer_pending(&job->sleep_timer));
  327. assert(!job->txn);
  328. if (job->driver->free) {
  329. job->driver->free(job);
  330. }
  331. QLIST_REMOVE(job, job_list);
  332. error_free(job->err);
  333. g_free(job->id);
  334. g_free(job);
  335. }
  336. }
  337. void job_progress_update(Job *job, uint64_t done)
  338. {
  339. progress_work_done(&job->progress, done);
  340. }
  341. void job_progress_set_remaining(Job *job, uint64_t remaining)
  342. {
  343. progress_set_remaining(&job->progress, remaining);
  344. }
  345. void job_progress_increase_remaining(Job *job, uint64_t delta)
  346. {
  347. progress_increase_remaining(&job->progress, delta);
  348. }
  349. void job_event_cancelled(Job *job)
  350. {
  351. notifier_list_notify(&job->on_finalize_cancelled, job);
  352. }
  353. void job_event_completed(Job *job)
  354. {
  355. notifier_list_notify(&job->on_finalize_completed, job);
  356. }
  357. static void job_event_pending(Job *job)
  358. {
  359. notifier_list_notify(&job->on_pending, job);
  360. }
  361. static void job_event_ready(Job *job)
  362. {
  363. notifier_list_notify(&job->on_ready, job);
  364. }
  365. static void job_event_idle(Job *job)
  366. {
  367. notifier_list_notify(&job->on_idle, job);
  368. }
  369. void job_enter_cond(Job *job, bool(*fn)(Job *job))
  370. {
  371. if (!job_started(job)) {
  372. return;
  373. }
  374. if (job->deferred_to_main_loop) {
  375. return;
  376. }
  377. job_lock();
  378. if (job->busy) {
  379. job_unlock();
  380. return;
  381. }
  382. if (fn && !fn(job)) {
  383. job_unlock();
  384. return;
  385. }
  386. assert(!job->deferred_to_main_loop);
  387. timer_del(&job->sleep_timer);
  388. job->busy = true;
  389. job_unlock();
  390. aio_co_enter(job->aio_context, job->co);
  391. }
  392. void job_enter(Job *job)
  393. {
  394. job_enter_cond(job, NULL);
  395. }
  396. /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
  397. * Reentering the job coroutine with job_enter() before the timer has expired
  398. * is allowed and cancels the timer.
  399. *
  400. * If @ns is (uint64_t) -1, no timer is scheduled and job_enter() must be
  401. * called explicitly. */
  402. static void coroutine_fn job_do_yield(Job *job, uint64_t ns)
  403. {
  404. job_lock();
  405. if (ns != -1) {
  406. timer_mod(&job->sleep_timer, ns);
  407. }
  408. job->busy = false;
  409. job_event_idle(job);
  410. job_unlock();
  411. qemu_coroutine_yield();
  412. /* Set by job_enter_cond() before re-entering the coroutine. */
  413. assert(job->busy);
  414. }
  415. void coroutine_fn job_pause_point(Job *job)
  416. {
  417. assert(job && job_started(job));
  418. if (!job_should_pause(job)) {
  419. return;
  420. }
  421. if (job_is_cancelled(job)) {
  422. return;
  423. }
  424. if (job->driver->pause) {
  425. job->driver->pause(job);
  426. }
  427. if (job_should_pause(job) && !job_is_cancelled(job)) {
  428. JobStatus status = job->status;
  429. job_state_transition(job, status == JOB_STATUS_READY
  430. ? JOB_STATUS_STANDBY
  431. : JOB_STATUS_PAUSED);
  432. job->paused = true;
  433. job_do_yield(job, -1);
  434. job->paused = false;
  435. job_state_transition(job, status);
  436. }
  437. if (job->driver->resume) {
  438. job->driver->resume(job);
  439. }
  440. }
  441. void job_yield(Job *job)
  442. {
  443. assert(job->busy);
  444. /* Check cancellation *before* setting busy = false, too! */
  445. if (job_is_cancelled(job)) {
  446. return;
  447. }
  448. if (!job_should_pause(job)) {
  449. job_do_yield(job, -1);
  450. }
  451. job_pause_point(job);
  452. }
  453. void coroutine_fn job_sleep_ns(Job *job, int64_t ns)
  454. {
  455. assert(job->busy);
  456. /* Check cancellation *before* setting busy = false, too! */
  457. if (job_is_cancelled(job)) {
  458. return;
  459. }
  460. if (!job_should_pause(job)) {
  461. job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
  462. }
  463. job_pause_point(job);
  464. }
  465. /* Assumes the block_job_mutex is held */
  466. static bool job_timer_not_pending(Job *job)
  467. {
  468. return !timer_pending(&job->sleep_timer);
  469. }
  470. void job_pause(Job *job)
  471. {
  472. job->pause_count++;
  473. }
  474. void job_resume(Job *job)
  475. {
  476. assert(job->pause_count > 0);
  477. job->pause_count--;
  478. if (job->pause_count) {
  479. return;
  480. }
  481. /* kick only if no timer is pending */
  482. job_enter_cond(job, job_timer_not_pending);
  483. }
  484. void job_user_pause(Job *job, Error **errp)
  485. {
  486. if (job_apply_verb(job, JOB_VERB_PAUSE, errp)) {
  487. return;
  488. }
  489. if (job->user_paused) {
  490. error_setg(errp, "Job is already paused");
  491. return;
  492. }
  493. job->user_paused = true;
  494. job_pause(job);
  495. }
  496. bool job_user_paused(Job *job)
  497. {
  498. return job->user_paused;
  499. }
  500. void job_user_resume(Job *job, Error **errp)
  501. {
  502. assert(job);
  503. if (!job->user_paused || job->pause_count <= 0) {
  504. error_setg(errp, "Can't resume a job that was not paused");
  505. return;
  506. }
  507. if (job_apply_verb(job, JOB_VERB_RESUME, errp)) {
  508. return;
  509. }
  510. if (job->driver->user_resume) {
  511. job->driver->user_resume(job);
  512. }
  513. job->user_paused = false;
  514. job_resume(job);
  515. }
  516. static void job_do_dismiss(Job *job)
  517. {
  518. assert(job);
  519. job->busy = false;
  520. job->paused = false;
  521. job->deferred_to_main_loop = true;
  522. job_txn_del_job(job);
  523. job_state_transition(job, JOB_STATUS_NULL);
  524. job_unref(job);
  525. }
  526. void job_dismiss(Job **jobptr, Error **errp)
  527. {
  528. Job *job = *jobptr;
  529. /* similarly to _complete, this is QMP-interface only. */
  530. assert(job->id);
  531. if (job_apply_verb(job, JOB_VERB_DISMISS, errp)) {
  532. return;
  533. }
  534. job_do_dismiss(job);
  535. *jobptr = NULL;
  536. }
  537. void job_early_fail(Job *job)
  538. {
  539. assert(job->status == JOB_STATUS_CREATED);
  540. job_do_dismiss(job);
  541. }
  542. static void job_conclude(Job *job)
  543. {
  544. job_state_transition(job, JOB_STATUS_CONCLUDED);
  545. if (job->auto_dismiss || !job_started(job)) {
  546. job_do_dismiss(job);
  547. }
  548. }
  549. static void job_update_rc(Job *job)
  550. {
  551. if (!job->ret && job_is_cancelled(job)) {
  552. job->ret = -ECANCELED;
  553. }
  554. if (job->ret) {
  555. if (!job->err) {
  556. error_setg(&job->err, "%s", strerror(-job->ret));
  557. }
  558. job_state_transition(job, JOB_STATUS_ABORTING);
  559. }
  560. }
  561. static void job_commit(Job *job)
  562. {
  563. assert(!job->ret);
  564. if (job->driver->commit) {
  565. job->driver->commit(job);
  566. }
  567. }
  568. static void job_abort(Job *job)
  569. {
  570. assert(job->ret);
  571. if (job->driver->abort) {
  572. job->driver->abort(job);
  573. }
  574. }
  575. static void job_clean(Job *job)
  576. {
  577. if (job->driver->clean) {
  578. job->driver->clean(job);
  579. }
  580. }
  581. static int job_finalize_single(Job *job)
  582. {
  583. assert(job_is_completed(job));
  584. /* Ensure abort is called for late-transactional failures */
  585. job_update_rc(job);
  586. if (!job->ret) {
  587. job_commit(job);
  588. } else {
  589. job_abort(job);
  590. }
  591. job_clean(job);
  592. if (job->cb) {
  593. job->cb(job->opaque, job->ret);
  594. }
  595. /* Emit events only if we actually started */
  596. if (job_started(job)) {
  597. if (job_is_cancelled(job)) {
  598. job_event_cancelled(job);
  599. } else {
  600. job_event_completed(job);
  601. }
  602. }
  603. job_txn_del_job(job);
  604. job_conclude(job);
  605. return 0;
  606. }
  607. static void job_cancel_async(Job *job, bool force)
  608. {
  609. if (job->user_paused) {
  610. /* Do not call job_enter here, the caller will handle it. */
  611. if (job->driver->user_resume) {
  612. job->driver->user_resume(job);
  613. }
  614. job->user_paused = false;
  615. assert(job->pause_count > 0);
  616. job->pause_count--;
  617. }
  618. job->cancelled = true;
  619. /* To prevent 'force == false' overriding a previous 'force == true' */
  620. job->force_cancel |= force;
  621. }
  622. static void job_completed_txn_abort(Job *job)
  623. {
  624. AioContext *outer_ctx = job->aio_context;
  625. AioContext *ctx;
  626. JobTxn *txn = job->txn;
  627. Job *other_job;
  628. if (txn->aborting) {
  629. /*
  630. * We are cancelled by another job, which will handle everything.
  631. */
  632. return;
  633. }
  634. txn->aborting = true;
  635. job_txn_ref(txn);
  636. /* We can only hold the single job's AioContext lock while calling
  637. * job_finalize_single() because the finalization callbacks can involve
  638. * calls of AIO_WAIT_WHILE(), which could deadlock otherwise. */
  639. aio_context_release(outer_ctx);
  640. /* Other jobs are effectively cancelled by us, set the status for
  641. * them; this job, however, may or may not be cancelled, depending
  642. * on the caller, so leave it. */
  643. QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
  644. if (other_job != job) {
  645. ctx = other_job->aio_context;
  646. aio_context_acquire(ctx);
  647. job_cancel_async(other_job, false);
  648. aio_context_release(ctx);
  649. }
  650. }
  651. while (!QLIST_EMPTY(&txn->jobs)) {
  652. other_job = QLIST_FIRST(&txn->jobs);
  653. ctx = other_job->aio_context;
  654. aio_context_acquire(ctx);
  655. if (!job_is_completed(other_job)) {
  656. assert(job_is_cancelled(other_job));
  657. job_finish_sync(other_job, NULL, NULL);
  658. }
  659. job_finalize_single(other_job);
  660. aio_context_release(ctx);
  661. }
  662. aio_context_acquire(outer_ctx);
  663. job_txn_unref(txn);
  664. }
  665. static int job_prepare(Job *job)
  666. {
  667. if (job->ret == 0 && job->driver->prepare) {
  668. job->ret = job->driver->prepare(job);
  669. job_update_rc(job);
  670. }
  671. return job->ret;
  672. }
  673. static int job_needs_finalize(Job *job)
  674. {
  675. return !job->auto_finalize;
  676. }
  677. static void job_do_finalize(Job *job)
  678. {
  679. int rc;
  680. assert(job && job->txn);
  681. /* prepare the transaction to complete */
  682. rc = job_txn_apply(job, job_prepare);
  683. if (rc) {
  684. job_completed_txn_abort(job);
  685. } else {
  686. job_txn_apply(job, job_finalize_single);
  687. }
  688. }
  689. void job_finalize(Job *job, Error **errp)
  690. {
  691. assert(job && job->id);
  692. if (job_apply_verb(job, JOB_VERB_FINALIZE, errp)) {
  693. return;
  694. }
  695. job_do_finalize(job);
  696. }
  697. static int job_transition_to_pending(Job *job)
  698. {
  699. job_state_transition(job, JOB_STATUS_PENDING);
  700. if (!job->auto_finalize) {
  701. job_event_pending(job);
  702. }
  703. return 0;
  704. }
  705. void job_transition_to_ready(Job *job)
  706. {
  707. job_state_transition(job, JOB_STATUS_READY);
  708. job_event_ready(job);
  709. }
  710. static void job_completed_txn_success(Job *job)
  711. {
  712. JobTxn *txn = job->txn;
  713. Job *other_job;
  714. job_state_transition(job, JOB_STATUS_WAITING);
  715. /*
  716. * Successful completion, see if there are other running jobs in this
  717. * txn.
  718. */
  719. QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
  720. if (!job_is_completed(other_job)) {
  721. return;
  722. }
  723. assert(other_job->ret == 0);
  724. }
  725. job_txn_apply(job, job_transition_to_pending);
  726. /* If no jobs need manual finalization, automatically do so */
  727. if (job_txn_apply(job, job_needs_finalize) == 0) {
  728. job_do_finalize(job);
  729. }
  730. }
  731. static void job_completed(Job *job)
  732. {
  733. assert(job && job->txn && !job_is_completed(job));
  734. job_update_rc(job);
  735. trace_job_completed(job, job->ret);
  736. if (job->ret) {
  737. job_completed_txn_abort(job);
  738. } else {
  739. job_completed_txn_success(job);
  740. }
  741. }
  742. /** Useful only as a type shim for aio_bh_schedule_oneshot. */
  743. static void job_exit(void *opaque)
  744. {
  745. Job *job = (Job *)opaque;
  746. AioContext *ctx;
  747. job_ref(job);
  748. aio_context_acquire(job->aio_context);
  749. /* This is a lie, we're not quiescent, but still doing the completion
  750. * callbacks. However, completion callbacks tend to involve operations that
  751. * drain block nodes, and if .drained_poll still returned true, we would
  752. * deadlock. */
  753. job->busy = false;
  754. job_event_idle(job);
  755. job_completed(job);
  756. /*
  757. * Note that calling job_completed can move the job to a different
  758. * aio_context, so we cannot cache from above. job_txn_apply takes care of
  759. * acquiring the new lock, and we ref/unref to avoid job_completed freeing
  760. * the job underneath us.
  761. */
  762. ctx = job->aio_context;
  763. job_unref(job);
  764. aio_context_release(ctx);
  765. }
  766. /**
  767. * All jobs must allow a pause point before entering their job proper. This
  768. * ensures that jobs can be paused prior to being started, then resumed later.
  769. */
  770. static void coroutine_fn job_co_entry(void *opaque)
  771. {
  772. Job *job = opaque;
  773. assert(job && job->driver && job->driver->run);
  774. job_pause_point(job);
  775. job->ret = job->driver->run(job, &job->err);
  776. job->deferred_to_main_loop = true;
  777. job->busy = true;
  778. aio_bh_schedule_oneshot(qemu_get_aio_context(), job_exit, job);
  779. }
  780. void job_start(Job *job)
  781. {
  782. assert(job && !job_started(job) && job->paused &&
  783. job->driver && job->driver->run);
  784. job->co = qemu_coroutine_create(job_co_entry, job);
  785. job->pause_count--;
  786. job->busy = true;
  787. job->paused = false;
  788. job_state_transition(job, JOB_STATUS_RUNNING);
  789. aio_co_enter(job->aio_context, job->co);
  790. }
  791. void job_cancel(Job *job, bool force)
  792. {
  793. if (job->status == JOB_STATUS_CONCLUDED) {
  794. job_do_dismiss(job);
  795. return;
  796. }
  797. job_cancel_async(job, force);
  798. if (!job_started(job)) {
  799. job_completed(job);
  800. } else if (job->deferred_to_main_loop) {
  801. job_completed_txn_abort(job);
  802. } else {
  803. job_enter(job);
  804. }
  805. }
  806. void job_user_cancel(Job *job, bool force, Error **errp)
  807. {
  808. if (job_apply_verb(job, JOB_VERB_CANCEL, errp)) {
  809. return;
  810. }
  811. job_cancel(job, force);
  812. }
  813. /* A wrapper around job_cancel() taking an Error ** parameter so it may be
  814. * used with job_finish_sync() without the need for (rather nasty) function
  815. * pointer casts there. */
  816. static void job_cancel_err(Job *job, Error **errp)
  817. {
  818. job_cancel(job, false);
  819. }
  820. int job_cancel_sync(Job *job)
  821. {
  822. return job_finish_sync(job, &job_cancel_err, NULL);
  823. }
  824. void job_cancel_sync_all(void)
  825. {
  826. Job *job;
  827. AioContext *aio_context;
  828. while ((job = job_next(NULL))) {
  829. aio_context = job->aio_context;
  830. aio_context_acquire(aio_context);
  831. job_cancel_sync(job);
  832. aio_context_release(aio_context);
  833. }
  834. }
  835. int job_complete_sync(Job *job, Error **errp)
  836. {
  837. return job_finish_sync(job, job_complete, errp);
  838. }
  839. void job_complete(Job *job, Error **errp)
  840. {
  841. /* Should not be reachable via external interface for internal jobs */
  842. assert(job->id);
  843. if (job_apply_verb(job, JOB_VERB_COMPLETE, errp)) {
  844. return;
  845. }
  846. if (job->pause_count || job_is_cancelled(job) || !job->driver->complete) {
  847. error_setg(errp, "The active block job '%s' cannot be completed",
  848. job->id);
  849. return;
  850. }
  851. job->driver->complete(job, errp);
  852. }
  853. int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp)
  854. {
  855. Error *local_err = NULL;
  856. int ret;
  857. job_ref(job);
  858. if (finish) {
  859. finish(job, &local_err);
  860. }
  861. if (local_err) {
  862. error_propagate(errp, local_err);
  863. job_unref(job);
  864. return -EBUSY;
  865. }
  866. AIO_WAIT_WHILE(job->aio_context,
  867. (job_enter(job), !job_is_completed(job)));
  868. ret = (job_is_cancelled(job) && job->ret == 0) ? -ECANCELED : job->ret;
  869. job_unref(job);
  870. return ret;
  871. }