ppc: Fix xsrdpi, xvrdpi and xvrspi rounding
[qemu/kevin.git] / blockjob.c
blob205da9df4e03d0341312bbb77d73f447e8076b50
1 /*
2 * QEMU System Emulator block driver
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
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 "qmp-commands.h"
37 #include "qemu/timer.h"
38 #include "qapi-event.h"
40 /* Transactional group of block jobs */
41 struct BlockJobTxn {
43 /* Is this txn being cancelled? */
44 bool aborting;
46 /* List of jobs */
47 QLIST_HEAD(, BlockJob) jobs;
49 /* Reference count */
50 int refcnt;
53 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
55 BlockJob *block_job_next(BlockJob *job)
57 if (!job) {
58 return QLIST_FIRST(&block_jobs);
60 return QLIST_NEXT(job, job_list);
63 /* Normally the job runs in its BlockBackend's AioContext. The exception is
64 * block_job_defer_to_main_loop() where it runs in the QEMU main loop. Code
65 * that supports both cases uses this helper function.
67 static AioContext *block_job_get_aio_context(BlockJob *job)
69 return job->deferred_to_main_loop ?
70 qemu_get_aio_context() :
71 blk_get_aio_context(job->blk);
74 static void block_job_attached_aio_context(AioContext *new_context,
75 void *opaque)
77 BlockJob *job = opaque;
79 if (job->driver->attached_aio_context) {
80 job->driver->attached_aio_context(job, new_context);
83 block_job_resume(job);
86 static void block_job_detach_aio_context(void *opaque)
88 BlockJob *job = opaque;
90 /* In case the job terminates during aio_poll()... */
91 block_job_ref(job);
93 block_job_pause(job);
95 if (!job->paused) {
96 /* If job is !job->busy this kicks it into the next pause point. */
97 block_job_enter(job);
99 while (!job->paused && !job->completed) {
100 aio_poll(block_job_get_aio_context(job), true);
103 block_job_unref(job);
106 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
107 int64_t speed, BlockCompletionFunc *cb,
108 void *opaque, Error **errp)
110 BlockBackend *blk;
111 BlockJob *job;
113 assert(cb);
114 if (bs->job) {
115 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
116 return NULL;
119 blk = blk_new();
120 blk_insert_bs(blk, bs);
122 job = g_malloc0(driver->instance_size);
123 error_setg(&job->blocker, "block device is in use by block job: %s",
124 BlockJobType_lookup[driver->job_type]);
125 bdrv_op_block_all(bs, job->blocker);
126 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
128 job->driver = driver;
129 job->id = g_strdup(bdrv_get_device_name(bs));
130 job->blk = blk;
131 job->cb = cb;
132 job->opaque = opaque;
133 job->busy = true;
134 job->refcnt = 1;
135 bs->job = job;
137 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
139 blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
140 block_job_detach_aio_context, job);
142 /* Only set speed when necessary to avoid NotSupported error */
143 if (speed != 0) {
144 Error *local_err = NULL;
146 block_job_set_speed(job, speed, &local_err);
147 if (local_err) {
148 block_job_unref(job);
149 error_propagate(errp, local_err);
150 return NULL;
153 return job;
156 void block_job_ref(BlockJob *job)
158 ++job->refcnt;
161 void block_job_unref(BlockJob *job)
163 if (--job->refcnt == 0) {
164 BlockDriverState *bs = blk_bs(job->blk);
165 bs->job = NULL;
166 bdrv_op_unblock_all(bs, job->blocker);
167 blk_remove_aio_context_notifier(job->blk,
168 block_job_attached_aio_context,
169 block_job_detach_aio_context, job);
170 blk_unref(job->blk);
171 error_free(job->blocker);
172 g_free(job->id);
173 QLIST_REMOVE(job, job_list);
174 g_free(job);
178 static void block_job_completed_single(BlockJob *job)
180 if (!job->ret) {
181 if (job->driver->commit) {
182 job->driver->commit(job);
184 } else {
185 if (job->driver->abort) {
186 job->driver->abort(job);
189 job->cb(job->opaque, job->ret);
190 if (job->txn) {
191 block_job_txn_unref(job->txn);
193 block_job_unref(job);
196 static void block_job_completed_txn_abort(BlockJob *job)
198 AioContext *ctx;
199 BlockJobTxn *txn = job->txn;
200 BlockJob *other_job, *next;
202 if (txn->aborting) {
204 * We are cancelled by another job, which will handle everything.
206 return;
208 txn->aborting = true;
209 /* We are the first failed job. Cancel other jobs. */
210 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
211 ctx = blk_get_aio_context(other_job->blk);
212 aio_context_acquire(ctx);
214 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
215 if (other_job == job || other_job->completed) {
216 /* Other jobs are "effectively" cancelled by us, set the status for
217 * them; this job, however, may or may not be cancelled, depending
218 * on the caller, so leave it. */
219 if (other_job != job) {
220 other_job->cancelled = true;
222 continue;
224 block_job_cancel_sync(other_job);
225 assert(other_job->completed);
227 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
228 ctx = blk_get_aio_context(other_job->blk);
229 block_job_completed_single(other_job);
230 aio_context_release(ctx);
234 static void block_job_completed_txn_success(BlockJob *job)
236 AioContext *ctx;
237 BlockJobTxn *txn = job->txn;
238 BlockJob *other_job, *next;
240 * Successful completion, see if there are other running jobs in this
241 * txn.
243 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
244 if (!other_job->completed) {
245 return;
248 /* We are the last completed job, commit the transaction. */
249 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
250 ctx = blk_get_aio_context(other_job->blk);
251 aio_context_acquire(ctx);
252 assert(other_job->ret == 0);
253 block_job_completed_single(other_job);
254 aio_context_release(ctx);
258 void block_job_completed(BlockJob *job, int ret)
260 assert(blk_bs(job->blk)->job == job);
261 assert(!job->completed);
262 job->completed = true;
263 job->ret = ret;
264 if (!job->txn) {
265 block_job_completed_single(job);
266 } else if (ret < 0 || block_job_is_cancelled(job)) {
267 block_job_completed_txn_abort(job);
268 } else {
269 block_job_completed_txn_success(job);
273 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
275 Error *local_err = NULL;
277 if (!job->driver->set_speed) {
278 error_setg(errp, QERR_UNSUPPORTED);
279 return;
281 job->driver->set_speed(job, speed, &local_err);
282 if (local_err) {
283 error_propagate(errp, local_err);
284 return;
287 job->speed = speed;
290 void block_job_complete(BlockJob *job, Error **errp)
292 if (job->pause_count || job->cancelled || !job->driver->complete) {
293 error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
294 return;
297 job->driver->complete(job, errp);
300 void block_job_pause(BlockJob *job)
302 job->pause_count++;
305 static bool block_job_should_pause(BlockJob *job)
307 return job->pause_count > 0;
310 void coroutine_fn block_job_pause_point(BlockJob *job)
312 if (!block_job_should_pause(job)) {
313 return;
315 if (block_job_is_cancelled(job)) {
316 return;
319 if (job->driver->pause) {
320 job->driver->pause(job);
323 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
324 job->paused = true;
325 job->busy = false;
326 qemu_coroutine_yield(); /* wait for block_job_resume() */
327 job->busy = true;
328 job->paused = false;
331 if (job->driver->resume) {
332 job->driver->resume(job);
336 void block_job_resume(BlockJob *job)
338 assert(job->pause_count > 0);
339 job->pause_count--;
340 if (job->pause_count) {
341 return;
343 block_job_enter(job);
346 void block_job_enter(BlockJob *job)
348 if (job->co && !job->busy) {
349 qemu_coroutine_enter(job->co, NULL);
353 void block_job_cancel(BlockJob *job)
355 job->cancelled = true;
356 block_job_iostatus_reset(job);
357 block_job_enter(job);
360 bool block_job_is_cancelled(BlockJob *job)
362 return job->cancelled;
365 void block_job_iostatus_reset(BlockJob *job)
367 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
368 if (job->driver->iostatus_reset) {
369 job->driver->iostatus_reset(job);
373 static int block_job_finish_sync(BlockJob *job,
374 void (*finish)(BlockJob *, Error **errp),
375 Error **errp)
377 Error *local_err = NULL;
378 int ret;
380 assert(blk_bs(job->blk)->job == job);
382 block_job_ref(job);
383 finish(job, &local_err);
384 if (local_err) {
385 error_propagate(errp, local_err);
386 block_job_unref(job);
387 return -EBUSY;
389 while (!job->completed) {
390 aio_poll(block_job_get_aio_context(job), true);
392 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
393 block_job_unref(job);
394 return ret;
397 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
398 * used with block_job_finish_sync() without the need for (rather nasty)
399 * function pointer casts there. */
400 static void block_job_cancel_err(BlockJob *job, Error **errp)
402 block_job_cancel(job);
405 int block_job_cancel_sync(BlockJob *job)
407 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
410 void block_job_cancel_sync_all(void)
412 BlockJob *job;
413 AioContext *aio_context;
415 while ((job = QLIST_FIRST(&block_jobs))) {
416 aio_context = blk_get_aio_context(job->blk);
417 aio_context_acquire(aio_context);
418 block_job_cancel_sync(job);
419 aio_context_release(aio_context);
423 int block_job_complete_sync(BlockJob *job, Error **errp)
425 return block_job_finish_sync(job, &block_job_complete, errp);
428 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
430 assert(job->busy);
432 /* Check cancellation *before* setting busy = false, too! */
433 if (block_job_is_cancelled(job)) {
434 return;
437 job->busy = false;
438 if (!block_job_should_pause(job)) {
439 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
441 job->busy = true;
443 block_job_pause_point(job);
446 void block_job_yield(BlockJob *job)
448 assert(job->busy);
450 /* Check cancellation *before* setting busy = false, too! */
451 if (block_job_is_cancelled(job)) {
452 return;
455 job->busy = false;
456 if (!block_job_should_pause(job)) {
457 qemu_coroutine_yield();
459 job->busy = true;
461 block_job_pause_point(job);
464 BlockJobInfo *block_job_query(BlockJob *job)
466 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
467 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
468 info->device = g_strdup(job->id);
469 info->len = job->len;
470 info->busy = job->busy;
471 info->paused = job->pause_count > 0;
472 info->offset = job->offset;
473 info->speed = job->speed;
474 info->io_status = job->iostatus;
475 info->ready = job->ready;
476 return info;
479 static void block_job_iostatus_set_err(BlockJob *job, int error)
481 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
482 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
483 BLOCK_DEVICE_IO_STATUS_FAILED;
487 void block_job_event_cancelled(BlockJob *job)
489 qapi_event_send_block_job_cancelled(job->driver->job_type,
490 job->id,
491 job->len,
492 job->offset,
493 job->speed,
494 &error_abort);
497 void block_job_event_completed(BlockJob *job, const char *msg)
499 qapi_event_send_block_job_completed(job->driver->job_type,
500 job->id,
501 job->len,
502 job->offset,
503 job->speed,
504 !!msg,
505 msg,
506 &error_abort);
509 void block_job_event_ready(BlockJob *job)
511 job->ready = true;
513 qapi_event_send_block_job_ready(job->driver->job_type,
514 job->id,
515 job->len,
516 job->offset,
517 job->speed, &error_abort);
520 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
521 int is_read, int error)
523 BlockErrorAction action;
525 switch (on_err) {
526 case BLOCKDEV_ON_ERROR_ENOSPC:
527 action = (error == ENOSPC) ?
528 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
529 break;
530 case BLOCKDEV_ON_ERROR_STOP:
531 action = BLOCK_ERROR_ACTION_STOP;
532 break;
533 case BLOCKDEV_ON_ERROR_REPORT:
534 action = BLOCK_ERROR_ACTION_REPORT;
535 break;
536 case BLOCKDEV_ON_ERROR_IGNORE:
537 action = BLOCK_ERROR_ACTION_IGNORE;
538 break;
539 default:
540 abort();
542 qapi_event_send_block_job_error(job->id,
543 is_read ? IO_OPERATION_TYPE_READ :
544 IO_OPERATION_TYPE_WRITE,
545 action, &error_abort);
546 if (action == BLOCK_ERROR_ACTION_STOP) {
547 /* make the pause user visible, which will be resumed from QMP. */
548 job->user_paused = true;
549 block_job_pause(job);
550 block_job_iostatus_set_err(job, error);
552 return action;
555 typedef struct {
556 BlockJob *job;
557 QEMUBH *bh;
558 AioContext *aio_context;
559 BlockJobDeferToMainLoopFn *fn;
560 void *opaque;
561 } BlockJobDeferToMainLoopData;
563 static void block_job_defer_to_main_loop_bh(void *opaque)
565 BlockJobDeferToMainLoopData *data = opaque;
566 AioContext *aio_context;
568 qemu_bh_delete(data->bh);
570 /* Prevent race with block_job_defer_to_main_loop() */
571 aio_context_acquire(data->aio_context);
573 /* Fetch BDS AioContext again, in case it has changed */
574 aio_context = blk_get_aio_context(data->job->blk);
575 aio_context_acquire(aio_context);
577 data->job->deferred_to_main_loop = false;
578 data->fn(data->job, data->opaque);
580 aio_context_release(aio_context);
582 aio_context_release(data->aio_context);
584 g_free(data);
587 void block_job_defer_to_main_loop(BlockJob *job,
588 BlockJobDeferToMainLoopFn *fn,
589 void *opaque)
591 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
592 data->job = job;
593 data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
594 data->aio_context = blk_get_aio_context(job->blk);
595 data->fn = fn;
596 data->opaque = opaque;
597 job->deferred_to_main_loop = true;
599 qemu_bh_schedule(data->bh);
602 BlockJobTxn *block_job_txn_new(void)
604 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
605 QLIST_INIT(&txn->jobs);
606 txn->refcnt = 1;
607 return txn;
610 static void block_job_txn_ref(BlockJobTxn *txn)
612 txn->refcnt++;
615 void block_job_txn_unref(BlockJobTxn *txn)
617 if (txn && --txn->refcnt == 0) {
618 g_free(txn);
622 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
624 if (!txn) {
625 return;
628 assert(!job->txn);
629 job->txn = txn;
631 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
632 block_job_txn_ref(txn);