docker: Add EXTRA_CONFIGURE_OPTS
[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 "qmp-commands.h"
37 #include "qemu/timer.h"
38 #include "qapi-event.h"
39
40 /* Transactional group of block jobs */
41 struct BlockJobTxn {
42
43 /* Is this txn being cancelled? */
44 bool aborting;
45
46 /* List of jobs */
47 QLIST_HEAD(, BlockJob) jobs;
48
49 /* Reference count */
50 int refcnt;
51 };
52
53 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
54
55 BlockJob *block_job_next(BlockJob *job)
56 {
57 if (!job) {
58 return QLIST_FIRST(&block_jobs);
59 }
60 return QLIST_NEXT(job, job_list);
61 }
62
63 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
64 int64_t speed, BlockCompletionFunc *cb,
65 void *opaque, Error **errp)
66 {
67 BlockBackend *blk;
68 BlockJob *job;
69
70 if (bs->job) {
71 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
72 return NULL;
73 }
74
75 blk = blk_new();
76 blk_insert_bs(blk, bs);
77
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);
83
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;
92
93 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
94
95 /* Only set speed when necessary to avoid NotSupported error */
96 if (speed != 0) {
97 Error *local_err = NULL;
98
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;
104 }
105 }
106 return job;
107 }
108
109 void block_job_ref(BlockJob *job)
110 {
111 ++job->refcnt;
112 }
113
114 void block_job_unref(BlockJob *job)
115 {
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);
125 }
126 }
127
128 static void block_job_completed_single(BlockJob *job)
129 {
130 if (!job->ret) {
131 if (job->driver->commit) {
132 job->driver->commit(job);
133 }
134 } else {
135 if (job->driver->abort) {
136 job->driver->abort(job);
137 }
138 }
139 job->cb(job->opaque, job->ret);
140 if (job->txn) {
141 block_job_txn_unref(job->txn);
142 }
143 block_job_unref(job);
144 }
145
146 static void block_job_completed_txn_abort(BlockJob *job)
147 {
148 AioContext *ctx;
149 BlockJobTxn *txn = job->txn;
150 BlockJob *other_job, *next;
151
152 if (txn->aborting) {
153 /*
154 * We are cancelled by another job, which will handle everything.
155 */
156 return;
157 }
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);
163 }
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;
171 }
172 continue;
173 }
174 block_job_cancel_sync(other_job);
175 assert(other_job->completed);
176 }
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);
181 }
182 }
183
184 static void block_job_completed_txn_success(BlockJob *job)
185 {
186 AioContext *ctx;
187 BlockJobTxn *txn = job->txn;
188 BlockJob *other_job, *next;
189 /*
190 * Successful completion, see if there are other running jobs in this
191 * txn.
192 */
193 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
194 if (!other_job->completed) {
195 return;
196 }
197 }
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);
205 }
206 }
207
208 void block_job_completed(BlockJob *job, int ret)
209 {
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);
220 }
221 }
222
223 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
224 {
225 Error *local_err = NULL;
226
227 if (!job->driver->set_speed) {
228 error_setg(errp, QERR_UNSUPPORTED);
229 return;
230 }
231 job->driver->set_speed(job, speed, &local_err);
232 if (local_err) {
233 error_propagate(errp, local_err);
234 return;
235 }
236
237 job->speed = speed;
238 }
239
240 void block_job_complete(BlockJob *job, Error **errp)
241 {
242 if (job->pause_count || job->cancelled || !job->driver->complete) {
243 error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
244 return;
245 }
246
247 job->driver->complete(job, errp);
248 }
249
250 void block_job_pause(BlockJob *job)
251 {
252 job->pause_count++;
253 }
254
255 bool block_job_is_paused(BlockJob *job)
256 {
257 return job->pause_count > 0;
258 }
259
260 void block_job_resume(BlockJob *job)
261 {
262 assert(job->pause_count > 0);
263 job->pause_count--;
264 if (job->pause_count) {
265 return;
266 }
267 block_job_enter(job);
268 }
269
270 void block_job_enter(BlockJob *job)
271 {
272 block_job_iostatus_reset(job);
273 if (job->co && !job->busy) {
274 qemu_coroutine_enter(job->co, NULL);
275 }
276 }
277
278 void block_job_cancel(BlockJob *job)
279 {
280 job->cancelled = true;
281 block_job_enter(job);
282 }
283
284 bool block_job_is_cancelled(BlockJob *job)
285 {
286 return job->cancelled;
287 }
288
289 void block_job_iostatus_reset(BlockJob *job)
290 {
291 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
292 if (job->driver->iostatus_reset) {
293 job->driver->iostatus_reset(job);
294 }
295 }
296
297 static int block_job_finish_sync(BlockJob *job,
298 void (*finish)(BlockJob *, Error **errp),
299 Error **errp)
300 {
301 Error *local_err = NULL;
302 int ret;
303
304 assert(blk_bs(job->blk)->job == job);
305
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;
312 }
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);
317 }
318 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
319 block_job_unref(job);
320 return ret;
321 }
322
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)
327 {
328 block_job_cancel(job);
329 }
330
331 int block_job_cancel_sync(BlockJob *job)
332 {
333 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
334 }
335
336 void block_job_cancel_sync_all(void)
337 {
338 BlockJob *job;
339 AioContext *aio_context;
340
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);
346 }
347 }
348
349 int block_job_complete_sync(BlockJob *job, Error **errp)
350 {
351 return block_job_finish_sync(job, &block_job_complete, errp);
352 }
353
354 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
355 {
356 assert(job->busy);
357
358 /* Check cancellation *before* setting busy = false, too! */
359 if (block_job_is_cancelled(job)) {
360 return;
361 }
362
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);
368 }
369 job->busy = true;
370 }
371
372 void block_job_yield(BlockJob *job)
373 {
374 assert(job->busy);
375
376 /* Check cancellation *before* setting busy = false, too! */
377 if (block_job_is_cancelled(job)) {
378 return;
379 }
380
381 job->busy = false;
382 qemu_coroutine_yield();
383 job->busy = true;
384 }
385
386 BlockJobInfo *block_job_query(BlockJob *job)
387 {
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;
399 }
400
401 static void block_job_iostatus_set_err(BlockJob *job, int error)
402 {
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;
406 }
407 }
408
409 void block_job_event_cancelled(BlockJob *job)
410 {
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);
417 }
418
419 void block_job_event_completed(BlockJob *job, const char *msg)
420 {
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);
429 }
430
431 void block_job_event_ready(BlockJob *job)
432 {
433 job->ready = true;
434
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);
440 }
441
442 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
443 int is_read, int error)
444 {
445 BlockErrorAction action;
446
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();
463 }
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);
473 }
474 return action;
475 }
476
477 typedef struct {
478 BlockJob *job;
479 QEMUBH *bh;
480 AioContext *aio_context;
481 BlockJobDeferToMainLoopFn *fn;
482 void *opaque;
483 } BlockJobDeferToMainLoopData;
484
485 static void block_job_defer_to_main_loop_bh(void *opaque)
486 {
487 BlockJobDeferToMainLoopData *data = opaque;
488 AioContext *aio_context;
489
490 qemu_bh_delete(data->bh);
491
492 /* Prevent race with block_job_defer_to_main_loop() */
493 aio_context_acquire(data->aio_context);
494
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);
498
499 data->job->deferred_to_main_loop = false;
500 data->fn(data->job, data->opaque);
501
502 aio_context_release(aio_context);
503
504 aio_context_release(data->aio_context);
505
506 g_free(data);
507 }
508
509 void block_job_defer_to_main_loop(BlockJob *job,
510 BlockJobDeferToMainLoopFn *fn,
511 void *opaque)
512 {
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;
520
521 qemu_bh_schedule(data->bh);
522 }
523
524 BlockJobTxn *block_job_txn_new(void)
525 {
526 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
527 QLIST_INIT(&txn->jobs);
528 txn->refcnt = 1;
529 return txn;
530 }
531
532 static void block_job_txn_ref(BlockJobTxn *txn)
533 {
534 txn->refcnt++;
535 }
536
537 void block_job_txn_unref(BlockJobTxn *txn)
538 {
539 if (txn && --txn->refcnt == 0) {
540 g_free(txn);
541 }
542 }
543
544 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
545 {
546 if (!txn) {
547 return;
548 }
549
550 assert(!job->txn);
551 job->txn = txn;
552
553 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
554 block_job_txn_ref(txn);
555 }