tests: Remove unnecessary glib.h includes
[qemu.git] / blockjob.c
blobc095cc57cbf842a7c0da1809abd7554e4c813539
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 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
64 int64_t speed, BlockCompletionFunc *cb,
65 void *opaque, Error **errp)
67 BlockBackend *blk;
68 BlockJob *job;
70 if (bs->job) {
71 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
72 return NULL;
75 blk = blk_new();
76 blk_insert_bs(blk, bs);
78 job = g_malloc0(driver->instance_size);
79 error_setg(&job->blocker, "block device is in use by block job: %s",
80 BlockJobType_lookup[driver->job_type]);
81 bdrv_op_block_all(bs, job->blocker);
82 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
84 job->driver = driver;
85 job->id = g_strdup(bdrv_get_device_name(bs));
86 job->blk = blk;
87 job->cb = cb;
88 job->opaque = opaque;
89 job->busy = true;
90 job->refcnt = 1;
91 bs->job = job;
93 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
95 /* Only set speed when necessary to avoid NotSupported error */
96 if (speed != 0) {
97 Error *local_err = NULL;
99 block_job_set_speed(job, speed, &local_err);
100 if (local_err) {
101 block_job_unref(job);
102 error_propagate(errp, local_err);
103 return NULL;
106 return job;
109 void block_job_ref(BlockJob *job)
111 ++job->refcnt;
114 void block_job_unref(BlockJob *job)
116 if (--job->refcnt == 0) {
117 BlockDriverState *bs = blk_bs(job->blk);
118 bs->job = NULL;
119 bdrv_op_unblock_all(bs, job->blocker);
120 blk_unref(job->blk);
121 error_free(job->blocker);
122 g_free(job->id);
123 QLIST_REMOVE(job, job_list);
124 g_free(job);
128 static void block_job_completed_single(BlockJob *job)
130 if (!job->ret) {
131 if (job->driver->commit) {
132 job->driver->commit(job);
134 } else {
135 if (job->driver->abort) {
136 job->driver->abort(job);
139 job->cb(job->opaque, job->ret);
140 if (job->txn) {
141 block_job_txn_unref(job->txn);
143 block_job_unref(job);
146 static void block_job_completed_txn_abort(BlockJob *job)
148 AioContext *ctx;
149 BlockJobTxn *txn = job->txn;
150 BlockJob *other_job, *next;
152 if (txn->aborting) {
154 * We are cancelled by another job, which will handle everything.
156 return;
158 txn->aborting = true;
159 /* We are the first failed job. Cancel other jobs. */
160 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
161 ctx = blk_get_aio_context(other_job->blk);
162 aio_context_acquire(ctx);
164 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
165 if (other_job == job || other_job->completed) {
166 /* Other jobs are "effectively" cancelled by us, set the status for
167 * them; this job, however, may or may not be cancelled, depending
168 * on the caller, so leave it. */
169 if (other_job != job) {
170 other_job->cancelled = true;
172 continue;
174 block_job_cancel_sync(other_job);
175 assert(other_job->completed);
177 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
178 ctx = blk_get_aio_context(other_job->blk);
179 block_job_completed_single(other_job);
180 aio_context_release(ctx);
184 static void block_job_completed_txn_success(BlockJob *job)
186 AioContext *ctx;
187 BlockJobTxn *txn = job->txn;
188 BlockJob *other_job, *next;
190 * Successful completion, see if there are other running jobs in this
191 * txn.
193 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
194 if (!other_job->completed) {
195 return;
198 /* We are the last completed job, commit the transaction. */
199 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
200 ctx = blk_get_aio_context(other_job->blk);
201 aio_context_acquire(ctx);
202 assert(other_job->ret == 0);
203 block_job_completed_single(other_job);
204 aio_context_release(ctx);
208 void block_job_completed(BlockJob *job, int ret)
210 assert(blk_bs(job->blk)->job == job);
211 assert(!job->completed);
212 job->completed = true;
213 job->ret = ret;
214 if (!job->txn) {
215 block_job_completed_single(job);
216 } else if (ret < 0 || block_job_is_cancelled(job)) {
217 block_job_completed_txn_abort(job);
218 } else {
219 block_job_completed_txn_success(job);
223 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
225 Error *local_err = NULL;
227 if (!job->driver->set_speed) {
228 error_setg(errp, QERR_UNSUPPORTED);
229 return;
231 job->driver->set_speed(job, speed, &local_err);
232 if (local_err) {
233 error_propagate(errp, local_err);
234 return;
237 job->speed = speed;
240 void block_job_complete(BlockJob *job, Error **errp)
242 if (job->pause_count || job->cancelled || !job->driver->complete) {
243 error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
244 return;
247 job->driver->complete(job, errp);
250 void block_job_pause(BlockJob *job)
252 job->pause_count++;
255 bool block_job_is_paused(BlockJob *job)
257 return job->pause_count > 0;
260 void block_job_resume(BlockJob *job)
262 assert(job->pause_count > 0);
263 job->pause_count--;
264 if (job->pause_count) {
265 return;
267 block_job_enter(job);
270 void block_job_enter(BlockJob *job)
272 block_job_iostatus_reset(job);
273 if (job->co && !job->busy) {
274 qemu_coroutine_enter(job->co, NULL);
278 void block_job_cancel(BlockJob *job)
280 job->cancelled = true;
281 block_job_enter(job);
284 bool block_job_is_cancelled(BlockJob *job)
286 return job->cancelled;
289 void block_job_iostatus_reset(BlockJob *job)
291 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
292 if (job->driver->iostatus_reset) {
293 job->driver->iostatus_reset(job);
297 static int block_job_finish_sync(BlockJob *job,
298 void (*finish)(BlockJob *, Error **errp),
299 Error **errp)
301 Error *local_err = NULL;
302 int ret;
304 assert(blk_bs(job->blk)->job == job);
306 block_job_ref(job);
307 finish(job, &local_err);
308 if (local_err) {
309 error_propagate(errp, local_err);
310 block_job_unref(job);
311 return -EBUSY;
313 while (!job->completed) {
314 aio_poll(job->deferred_to_main_loop ? qemu_get_aio_context() :
315 blk_get_aio_context(job->blk),
316 true);
318 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
319 block_job_unref(job);
320 return ret;
323 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
324 * used with block_job_finish_sync() without the need for (rather nasty)
325 * function pointer casts there. */
326 static void block_job_cancel_err(BlockJob *job, Error **errp)
328 block_job_cancel(job);
331 int block_job_cancel_sync(BlockJob *job)
333 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
336 void block_job_cancel_sync_all(void)
338 BlockJob *job;
339 AioContext *aio_context;
341 while ((job = QLIST_FIRST(&block_jobs))) {
342 aio_context = blk_get_aio_context(job->blk);
343 aio_context_acquire(aio_context);
344 block_job_cancel_sync(job);
345 aio_context_release(aio_context);
349 int block_job_complete_sync(BlockJob *job, Error **errp)
351 return block_job_finish_sync(job, &block_job_complete, errp);
354 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
356 assert(job->busy);
358 /* Check cancellation *before* setting busy = false, too! */
359 if (block_job_is_cancelled(job)) {
360 return;
363 job->busy = false;
364 if (block_job_is_paused(job)) {
365 qemu_coroutine_yield();
366 } else {
367 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
369 job->busy = true;
372 void block_job_yield(BlockJob *job)
374 assert(job->busy);
376 /* Check cancellation *before* setting busy = false, too! */
377 if (block_job_is_cancelled(job)) {
378 return;
381 job->busy = false;
382 qemu_coroutine_yield();
383 job->busy = true;
386 BlockJobInfo *block_job_query(BlockJob *job)
388 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
389 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
390 info->device = g_strdup(job->id);
391 info->len = job->len;
392 info->busy = job->busy;
393 info->paused = job->pause_count > 0;
394 info->offset = job->offset;
395 info->speed = job->speed;
396 info->io_status = job->iostatus;
397 info->ready = job->ready;
398 return info;
401 static void block_job_iostatus_set_err(BlockJob *job, int error)
403 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
404 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
405 BLOCK_DEVICE_IO_STATUS_FAILED;
409 void block_job_event_cancelled(BlockJob *job)
411 qapi_event_send_block_job_cancelled(job->driver->job_type,
412 job->id,
413 job->len,
414 job->offset,
415 job->speed,
416 &error_abort);
419 void block_job_event_completed(BlockJob *job, const char *msg)
421 qapi_event_send_block_job_completed(job->driver->job_type,
422 job->id,
423 job->len,
424 job->offset,
425 job->speed,
426 !!msg,
427 msg,
428 &error_abort);
431 void block_job_event_ready(BlockJob *job)
433 job->ready = true;
435 qapi_event_send_block_job_ready(job->driver->job_type,
436 job->id,
437 job->len,
438 job->offset,
439 job->speed, &error_abort);
442 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
443 int is_read, int error)
445 BlockErrorAction action;
447 switch (on_err) {
448 case BLOCKDEV_ON_ERROR_ENOSPC:
449 action = (error == ENOSPC) ?
450 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
451 break;
452 case BLOCKDEV_ON_ERROR_STOP:
453 action = BLOCK_ERROR_ACTION_STOP;
454 break;
455 case BLOCKDEV_ON_ERROR_REPORT:
456 action = BLOCK_ERROR_ACTION_REPORT;
457 break;
458 case BLOCKDEV_ON_ERROR_IGNORE:
459 action = BLOCK_ERROR_ACTION_IGNORE;
460 break;
461 default:
462 abort();
464 qapi_event_send_block_job_error(job->id,
465 is_read ? IO_OPERATION_TYPE_READ :
466 IO_OPERATION_TYPE_WRITE,
467 action, &error_abort);
468 if (action == BLOCK_ERROR_ACTION_STOP) {
469 /* make the pause user visible, which will be resumed from QMP. */
470 job->user_paused = true;
471 block_job_pause(job);
472 block_job_iostatus_set_err(job, error);
474 return action;
477 typedef struct {
478 BlockJob *job;
479 QEMUBH *bh;
480 AioContext *aio_context;
481 BlockJobDeferToMainLoopFn *fn;
482 void *opaque;
483 } BlockJobDeferToMainLoopData;
485 static void block_job_defer_to_main_loop_bh(void *opaque)
487 BlockJobDeferToMainLoopData *data = opaque;
488 AioContext *aio_context;
490 qemu_bh_delete(data->bh);
492 /* Prevent race with block_job_defer_to_main_loop() */
493 aio_context_acquire(data->aio_context);
495 /* Fetch BDS AioContext again, in case it has changed */
496 aio_context = blk_get_aio_context(data->job->blk);
497 aio_context_acquire(aio_context);
499 data->job->deferred_to_main_loop = false;
500 data->fn(data->job, data->opaque);
502 aio_context_release(aio_context);
504 aio_context_release(data->aio_context);
506 g_free(data);
509 void block_job_defer_to_main_loop(BlockJob *job,
510 BlockJobDeferToMainLoopFn *fn,
511 void *opaque)
513 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
514 data->job = job;
515 data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
516 data->aio_context = blk_get_aio_context(job->blk);
517 data->fn = fn;
518 data->opaque = opaque;
519 job->deferred_to_main_loop = true;
521 qemu_bh_schedule(data->bh);
524 BlockJobTxn *block_job_txn_new(void)
526 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
527 QLIST_INIT(&txn->jobs);
528 txn->refcnt = 1;
529 return txn;
532 static void block_job_txn_ref(BlockJobTxn *txn)
534 txn->refcnt++;
537 void block_job_txn_unref(BlockJobTxn *txn)
539 if (txn && --txn->refcnt == 0) {
540 g_free(txn);
544 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
546 if (!txn) {
547 return;
550 assert(!job->txn);
551 job->txn = txn;
553 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
554 block_job_txn_ref(txn);