]> git.proxmox.com Git - mirror_qemu.git/blob - blockjob.c
blockjobs: hide internal jobs from management API
[mirror_qemu.git] / blockjob.c
1 /*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012 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
26 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "trace.h"
29 #include "block/block.h"
30 #include "block/blockjob.h"
31 #include "block/block_int.h"
32 #include "sysemu/block-backend.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu/coroutine.h"
36 #include "qemu/id.h"
37 #include "qmp-commands.h"
38 #include "qemu/timer.h"
39 #include "qapi-event.h"
40
41 /* Transactional group of block jobs */
42 struct BlockJobTxn {
43
44 /* Is this txn being cancelled? */
45 bool aborting;
46
47 /* List of jobs */
48 QLIST_HEAD(, BlockJob) jobs;
49
50 /* Reference count */
51 int refcnt;
52 };
53
54 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
55
56 BlockJob *block_job_next(BlockJob *job)
57 {
58 if (!job) {
59 return QLIST_FIRST(&block_jobs);
60 }
61 return QLIST_NEXT(job, job_list);
62 }
63
64 BlockJob *block_job_get(const char *id)
65 {
66 BlockJob *job;
67
68 QLIST_FOREACH(job, &block_jobs, job_list) {
69 if (job->id && !strcmp(id, job->id)) {
70 return job;
71 }
72 }
73
74 return NULL;
75 }
76
77 static void block_job_attached_aio_context(AioContext *new_context,
78 void *opaque)
79 {
80 BlockJob *job = opaque;
81
82 if (job->driver->attached_aio_context) {
83 job->driver->attached_aio_context(job, new_context);
84 }
85
86 block_job_resume(job);
87 }
88
89 static void block_job_drain(BlockJob *job)
90 {
91 /* If job is !job->busy this kicks it into the next pause point. */
92 block_job_enter(job);
93
94 blk_drain(job->blk);
95 if (job->driver->drain) {
96 job->driver->drain(job);
97 }
98 }
99
100 static void block_job_detach_aio_context(void *opaque)
101 {
102 BlockJob *job = opaque;
103
104 /* In case the job terminates during aio_poll()... */
105 block_job_ref(job);
106
107 block_job_pause(job);
108
109 while (!job->paused && !job->completed) {
110 block_job_drain(job);
111 }
112
113 block_job_unref(job);
114 }
115
116 void block_job_add_bdrv(BlockJob *job, BlockDriverState *bs)
117 {
118 job->nodes = g_slist_prepend(job->nodes, bs);
119 bdrv_ref(bs);
120 bdrv_op_block_all(bs, job->blocker);
121 }
122
123 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
124 BlockDriverState *bs, int64_t speed,
125 BlockCompletionFunc *cb, void *opaque, Error **errp)
126 {
127 BlockBackend *blk;
128 BlockJob *job;
129
130 assert(cb);
131 if (bs->job) {
132 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
133 return NULL;
134 }
135
136 if (job_id == NULL) {
137 job_id = bdrv_get_device_name(bs);
138 if (!*job_id) {
139 error_setg(errp, "An explicit job ID is required for this node");
140 return NULL;
141 }
142 }
143
144 if (!id_wellformed(job_id)) {
145 error_setg(errp, "Invalid job ID '%s'", job_id);
146 return NULL;
147 }
148
149 if (block_job_get(job_id)) {
150 error_setg(errp, "Job ID '%s' already in use", job_id);
151 return NULL;
152 }
153
154 blk = blk_new();
155 blk_insert_bs(blk, bs);
156
157 job = g_malloc0(driver->instance_size);
158 error_setg(&job->blocker, "block device is in use by block job: %s",
159 BlockJobType_lookup[driver->job_type]);
160 block_job_add_bdrv(job, bs);
161 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
162
163 job->driver = driver;
164 job->id = g_strdup(job_id);
165 job->blk = blk;
166 job->cb = cb;
167 job->opaque = opaque;
168 job->busy = true;
169 job->refcnt = 1;
170 bs->job = job;
171
172 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
173
174 blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
175 block_job_detach_aio_context, job);
176
177 /* Only set speed when necessary to avoid NotSupported error */
178 if (speed != 0) {
179 Error *local_err = NULL;
180
181 block_job_set_speed(job, speed, &local_err);
182 if (local_err) {
183 block_job_unref(job);
184 error_propagate(errp, local_err);
185 return NULL;
186 }
187 }
188 return job;
189 }
190
191 bool block_job_is_internal(BlockJob *job)
192 {
193 return (job->id == NULL);
194 }
195
196 void block_job_ref(BlockJob *job)
197 {
198 ++job->refcnt;
199 }
200
201 void block_job_unref(BlockJob *job)
202 {
203 if (--job->refcnt == 0) {
204 GSList *l;
205 BlockDriverState *bs = blk_bs(job->blk);
206 bs->job = NULL;
207 for (l = job->nodes; l; l = l->next) {
208 bs = l->data;
209 bdrv_op_unblock_all(bs, job->blocker);
210 bdrv_unref(bs);
211 }
212 g_slist_free(job->nodes);
213 blk_remove_aio_context_notifier(job->blk,
214 block_job_attached_aio_context,
215 block_job_detach_aio_context, job);
216 blk_unref(job->blk);
217 error_free(job->blocker);
218 g_free(job->id);
219 QLIST_REMOVE(job, job_list);
220 g_free(job);
221 }
222 }
223
224 static void block_job_completed_single(BlockJob *job)
225 {
226 if (!job->ret) {
227 if (job->driver->commit) {
228 job->driver->commit(job);
229 }
230 } else {
231 if (job->driver->abort) {
232 job->driver->abort(job);
233 }
234 }
235 job->cb(job->opaque, job->ret);
236 if (job->txn) {
237 block_job_txn_unref(job->txn);
238 }
239 block_job_unref(job);
240 }
241
242 static void block_job_completed_txn_abort(BlockJob *job)
243 {
244 AioContext *ctx;
245 BlockJobTxn *txn = job->txn;
246 BlockJob *other_job, *next;
247
248 if (txn->aborting) {
249 /*
250 * We are cancelled by another job, which will handle everything.
251 */
252 return;
253 }
254 txn->aborting = true;
255 /* We are the first failed job. Cancel other jobs. */
256 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
257 ctx = blk_get_aio_context(other_job->blk);
258 aio_context_acquire(ctx);
259 }
260 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
261 if (other_job == job || other_job->completed) {
262 /* Other jobs are "effectively" cancelled by us, set the status for
263 * them; this job, however, may or may not be cancelled, depending
264 * on the caller, so leave it. */
265 if (other_job != job) {
266 other_job->cancelled = true;
267 }
268 continue;
269 }
270 block_job_cancel_sync(other_job);
271 assert(other_job->completed);
272 }
273 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
274 ctx = blk_get_aio_context(other_job->blk);
275 block_job_completed_single(other_job);
276 aio_context_release(ctx);
277 }
278 }
279
280 static void block_job_completed_txn_success(BlockJob *job)
281 {
282 AioContext *ctx;
283 BlockJobTxn *txn = job->txn;
284 BlockJob *other_job, *next;
285 /*
286 * Successful completion, see if there are other running jobs in this
287 * txn.
288 */
289 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
290 if (!other_job->completed) {
291 return;
292 }
293 }
294 /* We are the last completed job, commit the transaction. */
295 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
296 ctx = blk_get_aio_context(other_job->blk);
297 aio_context_acquire(ctx);
298 assert(other_job->ret == 0);
299 block_job_completed_single(other_job);
300 aio_context_release(ctx);
301 }
302 }
303
304 void block_job_completed(BlockJob *job, int ret)
305 {
306 assert(blk_bs(job->blk)->job == job);
307 assert(!job->completed);
308 job->completed = true;
309 job->ret = ret;
310 if (!job->txn) {
311 block_job_completed_single(job);
312 } else if (ret < 0 || block_job_is_cancelled(job)) {
313 block_job_completed_txn_abort(job);
314 } else {
315 block_job_completed_txn_success(job);
316 }
317 }
318
319 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
320 {
321 Error *local_err = NULL;
322
323 if (!job->driver->set_speed) {
324 error_setg(errp, QERR_UNSUPPORTED);
325 return;
326 }
327 job->driver->set_speed(job, speed, &local_err);
328 if (local_err) {
329 error_propagate(errp, local_err);
330 return;
331 }
332
333 job->speed = speed;
334 }
335
336 void block_job_complete(BlockJob *job, Error **errp)
337 {
338 /* Should not be reachable via external interface for internal jobs */
339 assert(job->id);
340 if (job->pause_count || job->cancelled || !job->driver->complete) {
341 error_setg(errp, "The active block job '%s' cannot be completed",
342 job->id);
343 return;
344 }
345
346 job->driver->complete(job, errp);
347 }
348
349 void block_job_pause(BlockJob *job)
350 {
351 job->pause_count++;
352 }
353
354 static bool block_job_should_pause(BlockJob *job)
355 {
356 return job->pause_count > 0;
357 }
358
359 void coroutine_fn block_job_pause_point(BlockJob *job)
360 {
361 if (!block_job_should_pause(job)) {
362 return;
363 }
364 if (block_job_is_cancelled(job)) {
365 return;
366 }
367
368 if (job->driver->pause) {
369 job->driver->pause(job);
370 }
371
372 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
373 job->paused = true;
374 job->busy = false;
375 qemu_coroutine_yield(); /* wait for block_job_resume() */
376 job->busy = true;
377 job->paused = false;
378 }
379
380 if (job->driver->resume) {
381 job->driver->resume(job);
382 }
383 }
384
385 void block_job_resume(BlockJob *job)
386 {
387 assert(job->pause_count > 0);
388 job->pause_count--;
389 if (job->pause_count) {
390 return;
391 }
392 block_job_enter(job);
393 }
394
395 void block_job_enter(BlockJob *job)
396 {
397 if (job->co && !job->busy) {
398 qemu_coroutine_enter(job->co);
399 }
400 }
401
402 void block_job_cancel(BlockJob *job)
403 {
404 job->cancelled = true;
405 block_job_iostatus_reset(job);
406 block_job_enter(job);
407 }
408
409 bool block_job_is_cancelled(BlockJob *job)
410 {
411 return job->cancelled;
412 }
413
414 void block_job_iostatus_reset(BlockJob *job)
415 {
416 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
417 if (job->driver->iostatus_reset) {
418 job->driver->iostatus_reset(job);
419 }
420 }
421
422 static int block_job_finish_sync(BlockJob *job,
423 void (*finish)(BlockJob *, Error **errp),
424 Error **errp)
425 {
426 Error *local_err = NULL;
427 int ret;
428
429 assert(blk_bs(job->blk)->job == job);
430
431 block_job_ref(job);
432
433 finish(job, &local_err);
434 if (local_err) {
435 error_propagate(errp, local_err);
436 block_job_unref(job);
437 return -EBUSY;
438 }
439 /* block_job_drain calls block_job_enter, and it should be enough to
440 * induce progress until the job completes or moves to the main thread.
441 */
442 while (!job->deferred_to_main_loop && !job->completed) {
443 block_job_drain(job);
444 }
445 while (!job->completed) {
446 aio_poll(qemu_get_aio_context(), true);
447 }
448 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
449 block_job_unref(job);
450 return ret;
451 }
452
453 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
454 * used with block_job_finish_sync() without the need for (rather nasty)
455 * function pointer casts there. */
456 static void block_job_cancel_err(BlockJob *job, Error **errp)
457 {
458 block_job_cancel(job);
459 }
460
461 int block_job_cancel_sync(BlockJob *job)
462 {
463 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
464 }
465
466 void block_job_cancel_sync_all(void)
467 {
468 BlockJob *job;
469 AioContext *aio_context;
470
471 while ((job = QLIST_FIRST(&block_jobs))) {
472 aio_context = blk_get_aio_context(job->blk);
473 aio_context_acquire(aio_context);
474 block_job_cancel_sync(job);
475 aio_context_release(aio_context);
476 }
477 }
478
479 int block_job_complete_sync(BlockJob *job, Error **errp)
480 {
481 return block_job_finish_sync(job, &block_job_complete, errp);
482 }
483
484 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
485 {
486 assert(job->busy);
487
488 /* Check cancellation *before* setting busy = false, too! */
489 if (block_job_is_cancelled(job)) {
490 return;
491 }
492
493 job->busy = false;
494 if (!block_job_should_pause(job)) {
495 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
496 }
497 job->busy = true;
498
499 block_job_pause_point(job);
500 }
501
502 void block_job_yield(BlockJob *job)
503 {
504 assert(job->busy);
505
506 /* Check cancellation *before* setting busy = false, too! */
507 if (block_job_is_cancelled(job)) {
508 return;
509 }
510
511 job->busy = false;
512 if (!block_job_should_pause(job)) {
513 qemu_coroutine_yield();
514 }
515 job->busy = true;
516
517 block_job_pause_point(job);
518 }
519
520 BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
521 {
522 BlockJobInfo *info;
523
524 if (block_job_is_internal(job)) {
525 error_setg(errp, "Cannot query QEMU internal jobs");
526 return NULL;
527 }
528 info = g_new0(BlockJobInfo, 1);
529 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
530 info->device = g_strdup(job->id);
531 info->len = job->len;
532 info->busy = job->busy;
533 info->paused = job->pause_count > 0;
534 info->offset = job->offset;
535 info->speed = job->speed;
536 info->io_status = job->iostatus;
537 info->ready = job->ready;
538 return info;
539 }
540
541 static void block_job_iostatus_set_err(BlockJob *job, int error)
542 {
543 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
544 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
545 BLOCK_DEVICE_IO_STATUS_FAILED;
546 }
547 }
548
549 void block_job_event_cancelled(BlockJob *job)
550 {
551 if (block_job_is_internal(job)) {
552 return;
553 }
554
555 qapi_event_send_block_job_cancelled(job->driver->job_type,
556 job->id,
557 job->len,
558 job->offset,
559 job->speed,
560 &error_abort);
561 }
562
563 void block_job_event_completed(BlockJob *job, const char *msg)
564 {
565 if (block_job_is_internal(job)) {
566 return;
567 }
568
569 qapi_event_send_block_job_completed(job->driver->job_type,
570 job->id,
571 job->len,
572 job->offset,
573 job->speed,
574 !!msg,
575 msg,
576 &error_abort);
577 }
578
579 void block_job_event_ready(BlockJob *job)
580 {
581 job->ready = true;
582
583 if (block_job_is_internal(job)) {
584 return;
585 }
586
587 qapi_event_send_block_job_ready(job->driver->job_type,
588 job->id,
589 job->len,
590 job->offset,
591 job->speed, &error_abort);
592 }
593
594 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
595 int is_read, int error)
596 {
597 BlockErrorAction action;
598
599 switch (on_err) {
600 case BLOCKDEV_ON_ERROR_ENOSPC:
601 case BLOCKDEV_ON_ERROR_AUTO:
602 action = (error == ENOSPC) ?
603 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
604 break;
605 case BLOCKDEV_ON_ERROR_STOP:
606 action = BLOCK_ERROR_ACTION_STOP;
607 break;
608 case BLOCKDEV_ON_ERROR_REPORT:
609 action = BLOCK_ERROR_ACTION_REPORT;
610 break;
611 case BLOCKDEV_ON_ERROR_IGNORE:
612 action = BLOCK_ERROR_ACTION_IGNORE;
613 break;
614 default:
615 abort();
616 }
617 if (!block_job_is_internal(job)) {
618 qapi_event_send_block_job_error(job->id,
619 is_read ? IO_OPERATION_TYPE_READ :
620 IO_OPERATION_TYPE_WRITE,
621 action, &error_abort);
622 }
623 if (action == BLOCK_ERROR_ACTION_STOP) {
624 /* make the pause user visible, which will be resumed from QMP. */
625 job->user_paused = true;
626 block_job_pause(job);
627 block_job_iostatus_set_err(job, error);
628 }
629 return action;
630 }
631
632 typedef struct {
633 BlockJob *job;
634 AioContext *aio_context;
635 BlockJobDeferToMainLoopFn *fn;
636 void *opaque;
637 } BlockJobDeferToMainLoopData;
638
639 static void block_job_defer_to_main_loop_bh(void *opaque)
640 {
641 BlockJobDeferToMainLoopData *data = opaque;
642 AioContext *aio_context;
643
644 /* Prevent race with block_job_defer_to_main_loop() */
645 aio_context_acquire(data->aio_context);
646
647 /* Fetch BDS AioContext again, in case it has changed */
648 aio_context = blk_get_aio_context(data->job->blk);
649 aio_context_acquire(aio_context);
650
651 data->job->deferred_to_main_loop = false;
652 data->fn(data->job, data->opaque);
653
654 aio_context_release(aio_context);
655
656 aio_context_release(data->aio_context);
657
658 g_free(data);
659 }
660
661 void block_job_defer_to_main_loop(BlockJob *job,
662 BlockJobDeferToMainLoopFn *fn,
663 void *opaque)
664 {
665 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
666 data->job = job;
667 data->aio_context = blk_get_aio_context(job->blk);
668 data->fn = fn;
669 data->opaque = opaque;
670 job->deferred_to_main_loop = true;
671
672 aio_bh_schedule_oneshot(qemu_get_aio_context(),
673 block_job_defer_to_main_loop_bh, data);
674 }
675
676 BlockJobTxn *block_job_txn_new(void)
677 {
678 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
679 QLIST_INIT(&txn->jobs);
680 txn->refcnt = 1;
681 return txn;
682 }
683
684 static void block_job_txn_ref(BlockJobTxn *txn)
685 {
686 txn->refcnt++;
687 }
688
689 void block_job_txn_unref(BlockJobTxn *txn)
690 {
691 if (txn && --txn->refcnt == 0) {
692 g_free(txn);
693 }
694 }
695
696 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
697 {
698 if (!txn) {
699 return;
700 }
701
702 assert(!job->txn);
703 job->txn = txn;
704
705 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
706 block_job_txn_ref(txn);
707 }