nbd: Don't use *_to_cpup() functions
[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 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
366 }
367 /* The job can be paused while sleeping, so check this again */
368 if (block_job_is_paused(job)) {
369 qemu_coroutine_yield();
370 }
371 job->busy = true;
372 }
373
374 void block_job_yield(BlockJob *job)
375 {
376 assert(job->busy);
377
378 /* Check cancellation *before* setting busy = false, too! */
379 if (block_job_is_cancelled(job)) {
380 return;
381 }
382
383 job->busy = false;
384 qemu_coroutine_yield();
385 job->busy = true;
386 }
387
388 BlockJobInfo *block_job_query(BlockJob *job)
389 {
390 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
391 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
392 info->device = g_strdup(job->id);
393 info->len = job->len;
394 info->busy = job->busy;
395 info->paused = job->pause_count > 0;
396 info->offset = job->offset;
397 info->speed = job->speed;
398 info->io_status = job->iostatus;
399 info->ready = job->ready;
400 return info;
401 }
402
403 static void block_job_iostatus_set_err(BlockJob *job, int error)
404 {
405 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
406 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
407 BLOCK_DEVICE_IO_STATUS_FAILED;
408 }
409 }
410
411 void block_job_event_cancelled(BlockJob *job)
412 {
413 qapi_event_send_block_job_cancelled(job->driver->job_type,
414 job->id,
415 job->len,
416 job->offset,
417 job->speed,
418 &error_abort);
419 }
420
421 void block_job_event_completed(BlockJob *job, const char *msg)
422 {
423 qapi_event_send_block_job_completed(job->driver->job_type,
424 job->id,
425 job->len,
426 job->offset,
427 job->speed,
428 !!msg,
429 msg,
430 &error_abort);
431 }
432
433 void block_job_event_ready(BlockJob *job)
434 {
435 job->ready = true;
436
437 qapi_event_send_block_job_ready(job->driver->job_type,
438 job->id,
439 job->len,
440 job->offset,
441 job->speed, &error_abort);
442 }
443
444 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
445 int is_read, int error)
446 {
447 BlockErrorAction action;
448
449 switch (on_err) {
450 case BLOCKDEV_ON_ERROR_ENOSPC:
451 action = (error == ENOSPC) ?
452 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
453 break;
454 case BLOCKDEV_ON_ERROR_STOP:
455 action = BLOCK_ERROR_ACTION_STOP;
456 break;
457 case BLOCKDEV_ON_ERROR_REPORT:
458 action = BLOCK_ERROR_ACTION_REPORT;
459 break;
460 case BLOCKDEV_ON_ERROR_IGNORE:
461 action = BLOCK_ERROR_ACTION_IGNORE;
462 break;
463 default:
464 abort();
465 }
466 qapi_event_send_block_job_error(job->id,
467 is_read ? IO_OPERATION_TYPE_READ :
468 IO_OPERATION_TYPE_WRITE,
469 action, &error_abort);
470 if (action == BLOCK_ERROR_ACTION_STOP) {
471 /* make the pause user visible, which will be resumed from QMP. */
472 job->user_paused = true;
473 block_job_pause(job);
474 block_job_iostatus_set_err(job, error);
475 }
476 return action;
477 }
478
479 typedef struct {
480 BlockJob *job;
481 QEMUBH *bh;
482 AioContext *aio_context;
483 BlockJobDeferToMainLoopFn *fn;
484 void *opaque;
485 } BlockJobDeferToMainLoopData;
486
487 static void block_job_defer_to_main_loop_bh(void *opaque)
488 {
489 BlockJobDeferToMainLoopData *data = opaque;
490 AioContext *aio_context;
491
492 qemu_bh_delete(data->bh);
493
494 /* Prevent race with block_job_defer_to_main_loop() */
495 aio_context_acquire(data->aio_context);
496
497 /* Fetch BDS AioContext again, in case it has changed */
498 aio_context = blk_get_aio_context(data->job->blk);
499 aio_context_acquire(aio_context);
500
501 data->job->deferred_to_main_loop = false;
502 data->fn(data->job, data->opaque);
503
504 aio_context_release(aio_context);
505
506 aio_context_release(data->aio_context);
507
508 g_free(data);
509 }
510
511 void block_job_defer_to_main_loop(BlockJob *job,
512 BlockJobDeferToMainLoopFn *fn,
513 void *opaque)
514 {
515 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
516 data->job = job;
517 data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
518 data->aio_context = blk_get_aio_context(job->blk);
519 data->fn = fn;
520 data->opaque = opaque;
521 job->deferred_to_main_loop = true;
522
523 qemu_bh_schedule(data->bh);
524 }
525
526 BlockJobTxn *block_job_txn_new(void)
527 {
528 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
529 QLIST_INIT(&txn->jobs);
530 txn->refcnt = 1;
531 return txn;
532 }
533
534 static void block_job_txn_ref(BlockJobTxn *txn)
535 {
536 txn->refcnt++;
537 }
538
539 void block_job_txn_unref(BlockJobTxn *txn)
540 {
541 if (txn && --txn->refcnt == 0) {
542 g_free(txn);
543 }
544 }
545
546 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
547 {
548 if (!txn) {
549 return;
550 }
551
552 assert(!job->txn);
553 job->txn = txn;
554
555 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
556 block_job_txn_ref(txn);
557 }