]> git.ipfire.org Git - thirdparty/qemu.git/blame - block/mirror.c
block: let mirror blockjob run in BDS AioContext
[thirdparty/qemu.git] / block / mirror.c
CommitLineData
893f7eba
PB
1/*
2 * Image mirroring
3 *
4 * Copyright Red Hat, Inc. 2012
5 *
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
14#include "trace.h"
737e150e
PB
15#include "block/blockjob.h"
16#include "block/block_int.h"
893f7eba 17#include "qemu/ratelimit.h"
b812f671 18#include "qemu/bitmap.h"
893f7eba 19
402a4741
PB
20#define SLICE_TIME 100000000ULL /* ns */
21#define MAX_IN_FLIGHT 16
22
23/* The mirroring buffer is a list of granularity-sized chunks.
24 * Free chunks are organized in a list.
25 */
26typedef struct MirrorBuffer {
27 QSIMPLEQ_ENTRY(MirrorBuffer) next;
28} MirrorBuffer;
893f7eba
PB
29
30typedef struct MirrorBlockJob {
31 BlockJob common;
32 RateLimit limit;
33 BlockDriverState *target;
5bc361b8 34 BlockDriverState *base;
09158f00
BC
35 /* The name of the graph node to replace */
36 char *replaces;
37 /* The BDS to replace */
38 BlockDriverState *to_replace;
39 /* Used to block operations on the drive-mirror-replace target */
40 Error *replace_blocker;
03544a6e 41 bool is_none_mode;
b952b558 42 BlockdevOnError on_source_error, on_target_error;
d63ffd87
PB
43 bool synced;
44 bool should_complete;
893f7eba 45 int64_t sector_num;
eee13dfe 46 int64_t granularity;
b812f671 47 size_t buf_size;
b21c7652 48 int64_t bdev_length;
b812f671 49 unsigned long *cow_bitmap;
e4654d2d 50 BdrvDirtyBitmap *dirty_bitmap;
8f0720ec 51 HBitmapIter hbi;
893f7eba 52 uint8_t *buf;
402a4741
PB
53 QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
54 int buf_free_count;
bd48bde8 55
402a4741 56 unsigned long *in_flight_bitmap;
bd48bde8 57 int in_flight;
b21c7652 58 int sectors_in_flight;
bd48bde8 59 int ret;
893f7eba
PB
60} MirrorBlockJob;
61
bd48bde8
PB
62typedef struct MirrorOp {
63 MirrorBlockJob *s;
64 QEMUIOVector qiov;
bd48bde8
PB
65 int64_t sector_num;
66 int nb_sectors;
67} MirrorOp;
68
b952b558
PB
69static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
70 int error)
71{
72 s->synced = false;
73 if (read) {
74 return block_job_error_action(&s->common, s->common.bs,
75 s->on_source_error, true, error);
76 } else {
77 return block_job_error_action(&s->common, s->target,
78 s->on_target_error, false, error);
79 }
80}
81
bd48bde8
PB
82static void mirror_iteration_done(MirrorOp *op, int ret)
83{
84 MirrorBlockJob *s = op->s;
402a4741 85 struct iovec *iov;
bd48bde8 86 int64_t chunk_num;
402a4741 87 int i, nb_chunks, sectors_per_chunk;
bd48bde8
PB
88
89 trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret);
90
91 s->in_flight--;
b21c7652 92 s->sectors_in_flight -= op->nb_sectors;
402a4741
PB
93 iov = op->qiov.iov;
94 for (i = 0; i < op->qiov.niov; i++) {
95 MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
96 QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next);
97 s->buf_free_count++;
98 }
99
bd48bde8
PB
100 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
101 chunk_num = op->sector_num / sectors_per_chunk;
102 nb_chunks = op->nb_sectors / sectors_per_chunk;
402a4741 103 bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
b21c7652
HR
104 if (ret >= 0) {
105 if (s->cow_bitmap) {
106 bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
107 }
108 s->common.offset += (uint64_t)op->nb_sectors * BDRV_SECTOR_SIZE;
bd48bde8
PB
109 }
110
6df3bf8e 111 qemu_iovec_destroy(&op->qiov);
bd48bde8 112 g_slice_free(MirrorOp, op);
7b770c72
SH
113
114 /* Enter coroutine when it is not sleeping. The coroutine sleeps to
115 * rate-limit itself. The coroutine will eventually resume since there is
116 * a sleep timeout so don't wake it early.
117 */
118 if (s->common.busy) {
119 qemu_coroutine_enter(s->common.co, NULL);
120 }
bd48bde8
PB
121}
122
123static void mirror_write_complete(void *opaque, int ret)
124{
125 MirrorOp *op = opaque;
126 MirrorBlockJob *s = op->s;
127 if (ret < 0) {
128 BlockDriverState *source = s->common.bs;
129 BlockErrorAction action;
130
131 bdrv_set_dirty(source, op->sector_num, op->nb_sectors);
132 action = mirror_error_action(s, false, -ret);
a589569f 133 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
bd48bde8
PB
134 s->ret = ret;
135 }
136 }
137 mirror_iteration_done(op, ret);
138}
139
140static void mirror_read_complete(void *opaque, int ret)
141{
142 MirrorOp *op = opaque;
143 MirrorBlockJob *s = op->s;
144 if (ret < 0) {
145 BlockDriverState *source = s->common.bs;
146 BlockErrorAction action;
147
148 bdrv_set_dirty(source, op->sector_num, op->nb_sectors);
149 action = mirror_error_action(s, true, -ret);
a589569f 150 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
bd48bde8
PB
151 s->ret = ret;
152 }
153
154 mirror_iteration_done(op, ret);
155 return;
156 }
157 bdrv_aio_writev(s->target, op->sector_num, &op->qiov, op->nb_sectors,
158 mirror_write_complete, op);
159}
160
cc8c9d6c 161static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
893f7eba
PB
162{
163 BlockDriverState *source = s->common.bs;
402a4741 164 int nb_sectors, sectors_per_chunk, nb_chunks;
884fea4e 165 int64_t end, sector_num, next_chunk, next_sector, hbitmap_next_sector;
6d0de8eb 166 uint64_t delay_ns = 0;
bd48bde8 167 MirrorOp *op;
893f7eba 168
8f0720ec
PB
169 s->sector_num = hbitmap_iter_next(&s->hbi);
170 if (s->sector_num < 0) {
e4654d2d 171 bdrv_dirty_iter_init(source, s->dirty_bitmap, &s->hbi);
8f0720ec 172 s->sector_num = hbitmap_iter_next(&s->hbi);
e4654d2d
FZ
173 trace_mirror_restart_iter(s,
174 bdrv_get_dirty_count(source, s->dirty_bitmap));
8f0720ec
PB
175 assert(s->sector_num >= 0);
176 }
177
402a4741 178 hbitmap_next_sector = s->sector_num;
884fea4e
PB
179 sector_num = s->sector_num;
180 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
b21c7652 181 end = s->bdev_length / BDRV_SECTOR_SIZE;
402a4741 182
884fea4e
PB
183 /* Extend the QEMUIOVector to include all adjacent blocks that will
184 * be copied in this operation.
b812f671 185 *
884fea4e
PB
186 * We have to do this if we have no backing file yet in the destination,
187 * and the cluster size is very large. Then we need to do COW ourselves.
188 * The first time a cluster is copied, copy it entirely. Note that,
189 * because both the granularity and the cluster size are powers of two,
190 * the number of sectors to copy cannot exceed one cluster.
191 *
192 * We also want to extend the QEMUIOVector to include more adjacent
193 * dirty blocks if possible, to limit the number of I/O operations and
194 * run efficiently even with a small granularity.
b812f671 195 */
884fea4e
PB
196 nb_chunks = 0;
197 nb_sectors = 0;
198 next_sector = sector_num;
199 next_chunk = sector_num / sectors_per_chunk;
402a4741
PB
200
201 /* Wait for I/O to this cluster (from a previous iteration) to be done. */
884fea4e 202 while (test_bit(next_chunk, s->in_flight_bitmap)) {
402a4741
PB
203 trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
204 qemu_coroutine_yield();
b812f671
PB
205 }
206
884fea4e
PB
207 do {
208 int added_sectors, added_chunks;
209
e4654d2d 210 if (!bdrv_get_dirty(source, s->dirty_bitmap, next_sector) ||
884fea4e
PB
211 test_bit(next_chunk, s->in_flight_bitmap)) {
212 assert(nb_sectors > 0);
213 break;
214 }
215
216 added_sectors = sectors_per_chunk;
217 if (s->cow_bitmap && !test_bit(next_chunk, s->cow_bitmap)) {
218 bdrv_round_to_clusters(s->target,
219 next_sector, added_sectors,
220 &next_sector, &added_sectors);
221
222 /* On the first iteration, the rounding may make us copy
223 * sectors before the first dirty one.
224 */
225 if (next_sector < sector_num) {
226 assert(nb_sectors == 0);
227 sector_num = next_sector;
228 next_chunk = next_sector / sectors_per_chunk;
229 }
230 }
231
232 added_sectors = MIN(added_sectors, end - (sector_num + nb_sectors));
233 added_chunks = (added_sectors + sectors_per_chunk - 1) / sectors_per_chunk;
234
235 /* When doing COW, it may happen that there is not enough space for
236 * a full cluster. Wait if that is the case.
237 */
238 while (nb_chunks == 0 && s->buf_free_count < added_chunks) {
239 trace_mirror_yield_buf_busy(s, nb_chunks, s->in_flight);
240 qemu_coroutine_yield();
241 }
242 if (s->buf_free_count < nb_chunks + added_chunks) {
243 trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
244 break;
245 }
246
247 /* We have enough free space to copy these sectors. */
248 bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks);
402a4741 249
884fea4e
PB
250 nb_sectors += added_sectors;
251 nb_chunks += added_chunks;
252 next_sector += added_sectors;
253 next_chunk += added_chunks;
cc8c9d6c
PB
254 if (!s->synced && s->common.speed) {
255 delay_ns = ratelimit_calculate_delay(&s->limit, added_sectors);
cc8c9d6c
PB
256 }
257 } while (delay_ns == 0 && next_sector < end);
bd48bde8
PB
258
259 /* Allocate a MirrorOp that is used as an AIO callback. */
260 op = g_slice_new(MirrorOp);
261 op->s = s;
bd48bde8
PB
262 op->sector_num = sector_num;
263 op->nb_sectors = nb_sectors;
402a4741
PB
264
265 /* Now make a QEMUIOVector taking enough granularity-sized chunks
266 * from s->buf_free.
267 */
268 qemu_iovec_init(&op->qiov, nb_chunks);
269 next_sector = sector_num;
270 while (nb_chunks-- > 0) {
271 MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
5a0f6fd5
KW
272 size_t remaining = (nb_sectors * BDRV_SECTOR_SIZE) - op->qiov.size;
273
402a4741
PB
274 QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
275 s->buf_free_count--;
5a0f6fd5 276 qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
402a4741
PB
277
278 /* Advance the HBitmapIter in parallel, so that we do not examine
279 * the same sector twice.
280 */
e4654d2d
FZ
281 if (next_sector > hbitmap_next_sector
282 && bdrv_get_dirty(source, s->dirty_bitmap, next_sector)) {
402a4741
PB
283 hbitmap_next_sector = hbitmap_iter_next(&s->hbi);
284 }
285
286 next_sector += sectors_per_chunk;
287 }
bd48bde8 288
b812f671 289 bdrv_reset_dirty(source, sector_num, nb_sectors);
893f7eba
PB
290
291 /* Copy the dirty cluster. */
bd48bde8 292 s->in_flight++;
b21c7652 293 s->sectors_in_flight += nb_sectors;
b812f671 294 trace_mirror_one_iteration(s, sector_num, nb_sectors);
bd48bde8
PB
295 bdrv_aio_readv(source, sector_num, &op->qiov, nb_sectors,
296 mirror_read_complete, op);
cc8c9d6c 297 return delay_ns;
bd48bde8 298}
b952b558 299
402a4741
PB
300static void mirror_free_init(MirrorBlockJob *s)
301{
302 int granularity = s->granularity;
303 size_t buf_size = s->buf_size;
304 uint8_t *buf = s->buf;
305
306 assert(s->buf_free_count == 0);
307 QSIMPLEQ_INIT(&s->buf_free);
308 while (buf_size != 0) {
309 MirrorBuffer *cur = (MirrorBuffer *)buf;
310 QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next);
311 s->buf_free_count++;
312 buf_size -= granularity;
313 buf += granularity;
314 }
315}
316
bd48bde8
PB
317static void mirror_drain(MirrorBlockJob *s)
318{
319 while (s->in_flight > 0) {
320 qemu_coroutine_yield();
321 }
893f7eba
PB
322}
323
5a7e7a0b
SH
324typedef struct {
325 int ret;
326} MirrorExitData;
327
328static void mirror_exit(BlockJob *job, void *opaque)
329{
330 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
331 MirrorExitData *data = opaque;
332 AioContext *replace_aio_context = NULL;
333
334 if (s->to_replace) {
335 replace_aio_context = bdrv_get_aio_context(s->to_replace);
336 aio_context_acquire(replace_aio_context);
337 }
338
339 if (s->should_complete && data->ret == 0) {
340 BlockDriverState *to_replace = s->common.bs;
341 if (s->to_replace) {
342 to_replace = s->to_replace;
343 }
344 if (bdrv_get_flags(s->target) != bdrv_get_flags(to_replace)) {
345 bdrv_reopen(s->target, bdrv_get_flags(to_replace), NULL);
346 }
347 bdrv_swap(s->target, to_replace);
348 if (s->common.driver->job_type == BLOCK_JOB_TYPE_COMMIT) {
349 /* drop the bs loop chain formed by the swap: break the loop then
350 * trigger the unref from the top one */
351 BlockDriverState *p = s->base->backing_hd;
352 bdrv_set_backing_hd(s->base, NULL);
353 bdrv_unref(p);
354 }
355 }
356 if (s->to_replace) {
357 bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
358 error_free(s->replace_blocker);
359 bdrv_unref(s->to_replace);
360 }
361 if (replace_aio_context) {
362 aio_context_release(replace_aio_context);
363 }
364 g_free(s->replaces);
365 bdrv_unref(s->target);
366 block_job_completed(&s->common, data->ret);
367 g_free(data);
368}
369
893f7eba
PB
370static void coroutine_fn mirror_run(void *opaque)
371{
372 MirrorBlockJob *s = opaque;
5a7e7a0b 373 MirrorExitData *data;
893f7eba 374 BlockDriverState *bs = s->common.bs;
eee13dfe 375 int64_t sector_num, end, sectors_per_chunk, length;
bd48bde8 376 uint64_t last_pause_ns;
b812f671
PB
377 BlockDriverInfo bdi;
378 char backing_filename[1024];
893f7eba
PB
379 int ret = 0;
380 int n;
893f7eba
PB
381
382 if (block_job_is_cancelled(&s->common)) {
383 goto immediate_exit;
384 }
385
b21c7652
HR
386 s->bdev_length = bdrv_getlength(bs);
387 if (s->bdev_length < 0) {
388 ret = s->bdev_length;
373df5b1 389 goto immediate_exit;
b21c7652 390 } else if (s->bdev_length == 0) {
9e48b025
FZ
391 /* Report BLOCK_JOB_READY and wait for complete. */
392 block_job_event_ready(&s->common);
393 s->synced = true;
394 while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
395 block_job_yield(&s->common);
396 }
397 s->common.cancelled = false;
398 goto immediate_exit;
893f7eba
PB
399 }
400
b21c7652 401 length = DIV_ROUND_UP(s->bdev_length, s->granularity);
402a4741
PB
402 s->in_flight_bitmap = bitmap_new(length);
403
b812f671
PB
404 /* If we have no backing file yet in the destination, we cannot let
405 * the destination do COW. Instead, we copy sectors around the
406 * dirty data if needed. We need a bitmap to do that.
407 */
408 bdrv_get_backing_filename(s->target, backing_filename,
409 sizeof(backing_filename));
410 if (backing_filename[0] && !s->target->backing_hd) {
c3cc95bd
FZ
411 ret = bdrv_get_info(s->target, &bdi);
412 if (ret < 0) {
413 goto immediate_exit;
414 }
eee13dfe 415 if (s->granularity < bdi.cluster_size) {
08e4ed6c 416 s->buf_size = MAX(s->buf_size, bdi.cluster_size);
b812f671
PB
417 s->cow_bitmap = bitmap_new(length);
418 }
419 }
420
b21c7652 421 end = s->bdev_length / BDRV_SECTOR_SIZE;
7504edf4
KW
422 s->buf = qemu_try_blockalign(bs, s->buf_size);
423 if (s->buf == NULL) {
424 ret = -ENOMEM;
425 goto immediate_exit;
426 }
427
eee13dfe 428 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
402a4741 429 mirror_free_init(s);
893f7eba 430
03544a6e 431 if (!s->is_none_mode) {
893f7eba 432 /* First part, loop on the sectors and initialize the dirty bitmap. */
5bc361b8 433 BlockDriverState *base = s->base;
893f7eba 434 for (sector_num = 0; sector_num < end; ) {
eee13dfe 435 int64_t next = (sector_num | (sectors_per_chunk - 1)) + 1;
4f578637
PB
436 ret = bdrv_is_allocated_above(bs, base,
437 sector_num, next - sector_num, &n);
893f7eba
PB
438
439 if (ret < 0) {
440 goto immediate_exit;
441 }
442
443 assert(n > 0);
444 if (ret == 1) {
445 bdrv_set_dirty(bs, sector_num, n);
446 sector_num = next;
447 } else {
448 sector_num += n;
449 }
450 }
451 }
452
e4654d2d 453 bdrv_dirty_iter_init(bs, s->dirty_bitmap, &s->hbi);
bc72ad67 454 last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
893f7eba 455 for (;;) {
cc8c9d6c 456 uint64_t delay_ns = 0;
893f7eba
PB
457 int64_t cnt;
458 bool should_complete;
459
bd48bde8
PB
460 if (s->ret < 0) {
461 ret = s->ret;
462 goto immediate_exit;
463 }
464
e4654d2d 465 cnt = bdrv_get_dirty_count(bs, s->dirty_bitmap);
b21c7652
HR
466 /* s->common.offset contains the number of bytes already processed so
467 * far, cnt is the number of dirty sectors remaining and
468 * s->sectors_in_flight is the number of sectors currently being
469 * processed; together those are the current total operation length */
470 s->common.len = s->common.offset +
471 (cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE;
bd48bde8
PB
472
473 /* Note that even when no rate limit is applied we need to yield
474 * periodically with no pending I/O so that qemu_aio_flush() returns.
475 * We do so every SLICE_TIME nanoseconds, or when there is an error,
476 * or when the source is clean, whichever comes first.
477 */
bc72ad67 478 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - last_pause_ns < SLICE_TIME &&
bd48bde8 479 s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
402a4741
PB
480 if (s->in_flight == MAX_IN_FLIGHT || s->buf_free_count == 0 ||
481 (cnt == 0 && s->in_flight > 0)) {
482 trace_mirror_yield(s, s->in_flight, s->buf_free_count, cnt);
bd48bde8
PB
483 qemu_coroutine_yield();
484 continue;
485 } else if (cnt != 0) {
cc8c9d6c
PB
486 delay_ns = mirror_iteration(s);
487 if (delay_ns == 0) {
488 continue;
489 }
893f7eba 490 }
893f7eba
PB
491 }
492
493 should_complete = false;
bd48bde8 494 if (s->in_flight == 0 && cnt == 0) {
893f7eba
PB
495 trace_mirror_before_flush(s);
496 ret = bdrv_flush(s->target);
497 if (ret < 0) {
a589569f
WX
498 if (mirror_error_action(s, false, -ret) ==
499 BLOCK_ERROR_ACTION_REPORT) {
b952b558
PB
500 goto immediate_exit;
501 }
502 } else {
503 /* We're out of the streaming phase. From now on, if the job
504 * is cancelled we will actually complete all pending I/O and
505 * report completion. This way, block-job-cancel will leave
506 * the target in a consistent state.
507 */
b952b558 508 if (!s->synced) {
bcada37b 509 block_job_event_ready(&s->common);
b952b558
PB
510 s->synced = true;
511 }
512
513 should_complete = s->should_complete ||
514 block_job_is_cancelled(&s->common);
e4654d2d 515 cnt = bdrv_get_dirty_count(bs, s->dirty_bitmap);
d63ffd87 516 }
893f7eba
PB
517 }
518
519 if (cnt == 0 && should_complete) {
520 /* The dirty bitmap is not updated while operations are pending.
521 * If we're about to exit, wait for pending operations before
522 * calling bdrv_get_dirty_count(bs), or we may exit while the
523 * source has dirty data to copy!
524 *
525 * Note that I/O can be submitted by the guest while
526 * mirror_populate runs.
527 */
528 trace_mirror_before_drain(s, cnt);
5a7e7a0b 529 bdrv_drain(bs);
e4654d2d 530 cnt = bdrv_get_dirty_count(bs, s->dirty_bitmap);
893f7eba
PB
531 }
532
533 ret = 0;
cc8c9d6c 534 trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
d63ffd87 535 if (!s->synced) {
7483d1e5 536 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
893f7eba
PB
537 if (block_job_is_cancelled(&s->common)) {
538 break;
539 }
540 } else if (!should_complete) {
bd48bde8 541 delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
7483d1e5 542 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
893f7eba
PB
543 } else if (cnt == 0) {
544 /* The two disks are in sync. Exit and report successful
545 * completion.
546 */
547 assert(QLIST_EMPTY(&bs->tracked_requests));
548 s->common.cancelled = false;
549 break;
550 }
bc72ad67 551 last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
893f7eba
PB
552 }
553
554immediate_exit:
bd48bde8
PB
555 if (s->in_flight > 0) {
556 /* We get here only if something went wrong. Either the job failed,
557 * or it was cancelled prematurely so that we do not guarantee that
558 * the target is a copy of the source.
559 */
560 assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
561 mirror_drain(s);
562 }
563
564 assert(s->in_flight == 0);
7191bf31 565 qemu_vfree(s->buf);
b812f671 566 g_free(s->cow_bitmap);
402a4741 567 g_free(s->in_flight_bitmap);
e4654d2d 568 bdrv_release_dirty_bitmap(bs, s->dirty_bitmap);
b952b558 569 bdrv_iostatus_disable(s->target);
5a7e7a0b
SH
570
571 data = g_malloc(sizeof(*data));
572 data->ret = ret;
573 block_job_defer_to_main_loop(&s->common, mirror_exit, data);
893f7eba
PB
574}
575
576static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
577{
578 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
579
580 if (speed < 0) {
581 error_set(errp, QERR_INVALID_PARAMETER, "speed");
582 return;
583 }
584 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
585}
586
b952b558
PB
587static void mirror_iostatus_reset(BlockJob *job)
588{
589 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
590
591 bdrv_iostatus_reset(s->target);
592}
593
d63ffd87
PB
594static void mirror_complete(BlockJob *job, Error **errp)
595{
596 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
34b5d2c6 597 Error *local_err = NULL;
d63ffd87
PB
598 int ret;
599
34b5d2c6 600 ret = bdrv_open_backing_file(s->target, NULL, &local_err);
d63ffd87 601 if (ret < 0) {
34b5d2c6 602 error_propagate(errp, local_err);
d63ffd87
PB
603 return;
604 }
605 if (!s->synced) {
bfb197e0
MA
606 error_set(errp, QERR_BLOCK_JOB_NOT_READY,
607 bdrv_get_device_name(job->bs));
d63ffd87
PB
608 return;
609 }
610
09158f00
BC
611 /* check the target bs is not blocked and block all operations on it */
612 if (s->replaces) {
5a7e7a0b
SH
613 AioContext *replace_aio_context;
614
09158f00
BC
615 s->to_replace = check_to_replace_node(s->replaces, &local_err);
616 if (!s->to_replace) {
617 error_propagate(errp, local_err);
618 return;
619 }
620
5a7e7a0b
SH
621 replace_aio_context = bdrv_get_aio_context(s->to_replace);
622 aio_context_acquire(replace_aio_context);
623
09158f00
BC
624 error_setg(&s->replace_blocker,
625 "block device is in use by block-job-complete");
626 bdrv_op_block_all(s->to_replace, s->replace_blocker);
627 bdrv_ref(s->to_replace);
5a7e7a0b
SH
628
629 aio_context_release(replace_aio_context);
09158f00
BC
630 }
631
d63ffd87
PB
632 s->should_complete = true;
633 block_job_resume(job);
634}
635
3fc4b10a 636static const BlockJobDriver mirror_job_driver = {
893f7eba 637 .instance_size = sizeof(MirrorBlockJob),
79e14bf7 638 .job_type = BLOCK_JOB_TYPE_MIRROR,
893f7eba 639 .set_speed = mirror_set_speed,
b952b558 640 .iostatus_reset= mirror_iostatus_reset,
d63ffd87 641 .complete = mirror_complete,
893f7eba
PB
642};
643
03544a6e
FZ
644static const BlockJobDriver commit_active_job_driver = {
645 .instance_size = sizeof(MirrorBlockJob),
646 .job_type = BLOCK_JOB_TYPE_COMMIT,
647 .set_speed = mirror_set_speed,
648 .iostatus_reset
649 = mirror_iostatus_reset,
650 .complete = mirror_complete,
651};
652
653static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,
09158f00
BC
654 const char *replaces,
655 int64_t speed, int64_t granularity,
656 int64_t buf_size,
657 BlockdevOnError on_source_error,
658 BlockdevOnError on_target_error,
097310b5 659 BlockCompletionFunc *cb,
09158f00
BC
660 void *opaque, Error **errp,
661 const BlockJobDriver *driver,
662 bool is_none_mode, BlockDriverState *base)
893f7eba
PB
663{
664 MirrorBlockJob *s;
665
eee13dfe
PB
666 if (granularity == 0) {
667 /* Choose the default granularity based on the target file's cluster
668 * size, clamped between 4k and 64k. */
669 BlockDriverInfo bdi;
670 if (bdrv_get_info(target, &bdi) >= 0 && bdi.cluster_size != 0) {
671 granularity = MAX(4096, bdi.cluster_size);
672 granularity = MIN(65536, granularity);
673 } else {
674 granularity = 65536;
675 }
676 }
677
678 assert ((granularity & (granularity - 1)) == 0);
679
b952b558
PB
680 if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
681 on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
682 !bdrv_iostatus_is_enabled(bs)) {
683 error_set(errp, QERR_INVALID_PARAMETER, "on-source-error");
684 return;
685 }
686
5bc361b8 687
03544a6e 688 s = block_job_create(driver, bs, speed, cb, opaque, errp);
893f7eba
PB
689 if (!s) {
690 return;
691 }
692
09158f00 693 s->replaces = g_strdup(replaces);
b952b558
PB
694 s->on_source_error = on_source_error;
695 s->on_target_error = on_target_error;
893f7eba 696 s->target = target;
03544a6e 697 s->is_none_mode = is_none_mode;
5bc361b8 698 s->base = base;
eee13dfe 699 s->granularity = granularity;
08e4ed6c 700 s->buf_size = MAX(buf_size, granularity);
b812f671 701
b8afb520
FZ
702 s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, errp);
703 if (!s->dirty_bitmap) {
704 return;
705 }
893f7eba 706 bdrv_set_enable_write_cache(s->target, true);
b952b558
PB
707 bdrv_set_on_error(s->target, on_target_error, on_target_error);
708 bdrv_iostatus_enable(s->target);
893f7eba
PB
709 s->common.co = qemu_coroutine_create(mirror_run);
710 trace_mirror_start(bs, s, s->common.co, opaque);
711 qemu_coroutine_enter(s->common.co, s);
712}
03544a6e
FZ
713
714void mirror_start(BlockDriverState *bs, BlockDriverState *target,
09158f00 715 const char *replaces,
03544a6e
FZ
716 int64_t speed, int64_t granularity, int64_t buf_size,
717 MirrorSyncMode mode, BlockdevOnError on_source_error,
718 BlockdevOnError on_target_error,
097310b5 719 BlockCompletionFunc *cb,
03544a6e
FZ
720 void *opaque, Error **errp)
721{
722 bool is_none_mode;
723 BlockDriverState *base;
724
725 is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
726 base = mode == MIRROR_SYNC_MODE_TOP ? bs->backing_hd : NULL;
09158f00
BC
727 mirror_start_job(bs, target, replaces,
728 speed, granularity, buf_size,
03544a6e
FZ
729 on_source_error, on_target_error, cb, opaque, errp,
730 &mirror_job_driver, is_none_mode, base);
731}
732
733void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
734 int64_t speed,
735 BlockdevOnError on_error,
097310b5 736 BlockCompletionFunc *cb,
03544a6e
FZ
737 void *opaque, Error **errp)
738{
4da83585
JC
739 int64_t length, base_length;
740 int orig_base_flags;
39a611a3 741 int ret;
cc67f4d1 742 Error *local_err = NULL;
4da83585
JC
743
744 orig_base_flags = bdrv_get_flags(base);
745
20a63d2c
FZ
746 if (bdrv_reopen(base, bs->open_flags, errp)) {
747 return;
748 }
4da83585
JC
749
750 length = bdrv_getlength(bs);
751 if (length < 0) {
39a611a3
JC
752 error_setg_errno(errp, -length,
753 "Unable to determine length of %s", bs->filename);
4da83585
JC
754 goto error_restore_flags;
755 }
756
757 base_length = bdrv_getlength(base);
758 if (base_length < 0) {
39a611a3
JC
759 error_setg_errno(errp, -base_length,
760 "Unable to determine length of %s", base->filename);
4da83585
JC
761 goto error_restore_flags;
762 }
763
764 if (length > base_length) {
39a611a3
JC
765 ret = bdrv_truncate(base, length);
766 if (ret < 0) {
767 error_setg_errno(errp, -ret,
768 "Top image %s is larger than base image %s, and "
4da83585
JC
769 "resize of base image failed",
770 bs->filename, base->filename);
771 goto error_restore_flags;
772 }
773 }
774
20a63d2c 775 bdrv_ref(base);
09158f00 776 mirror_start_job(bs, base, NULL, speed, 0, 0,
cc67f4d1 777 on_error, on_error, cb, opaque, &local_err,
03544a6e 778 &commit_active_job_driver, false, base);
0fb6395c 779 if (local_err) {
cc67f4d1 780 error_propagate(errp, local_err);
4da83585
JC
781 goto error_restore_flags;
782 }
783
784 return;
785
786error_restore_flags:
787 /* ignore error and errp for bdrv_reopen, because we want to propagate
788 * the original error */
789 bdrv_reopen(base, orig_base_flags, NULL);
790 return;
03544a6e 791}