backup: Use BlockBackend for I/O
[qemu.git] / block / backup.c
1 /*
2 * QEMU backup
3 *
4 * Copyright (C) 2013 Proxmox Server Solutions
5 *
6 * Authors:
7 * Dietmar Maurer (dietmar@proxmox.com)
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 */
13
14 #include "qemu/osdep.h"
15
16 #include "trace.h"
17 #include "block/block.h"
18 #include "block/block_int.h"
19 #include "block/blockjob.h"
20 #include "qapi/error.h"
21 #include "qapi/qmp/qerror.h"
22 #include "qemu/ratelimit.h"
23 #include "qemu/cutils.h"
24 #include "sysemu/block-backend.h"
25 #include "qemu/bitmap.h"
26
27 #define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
28 #define SLICE_TIME 100000000ULL /* ns */
29
30 typedef struct CowRequest {
31 int64_t start;
32 int64_t end;
33 QLIST_ENTRY(CowRequest) list;
34 CoQueue wait_queue; /* coroutines blocked on this request */
35 } CowRequest;
36
37 typedef struct BackupBlockJob {
38 BlockJob common;
39 BlockBackend *target;
40 /* bitmap for sync=incremental */
41 BdrvDirtyBitmap *sync_bitmap;
42 MirrorSyncMode sync_mode;
43 RateLimit limit;
44 BlockdevOnError on_source_error;
45 BlockdevOnError on_target_error;
46 CoRwlock flush_rwlock;
47 uint64_t sectors_read;
48 unsigned long *done_bitmap;
49 int64_t cluster_size;
50 NotifierWithReturn before_write;
51 QLIST_HEAD(, CowRequest) inflight_reqs;
52 } BackupBlockJob;
53
54 /* Size of a cluster in sectors, instead of bytes. */
55 static inline int64_t cluster_size_sectors(BackupBlockJob *job)
56 {
57 return job->cluster_size / BDRV_SECTOR_SIZE;
58 }
59
60 /* See if in-flight requests overlap and wait for them to complete */
61 static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job,
62 int64_t start,
63 int64_t end)
64 {
65 CowRequest *req;
66 bool retry;
67
68 do {
69 retry = false;
70 QLIST_FOREACH(req, &job->inflight_reqs, list) {
71 if (end > req->start && start < req->end) {
72 qemu_co_queue_wait(&req->wait_queue);
73 retry = true;
74 break;
75 }
76 }
77 } while (retry);
78 }
79
80 /* Keep track of an in-flight request */
81 static void cow_request_begin(CowRequest *req, BackupBlockJob *job,
82 int64_t start, int64_t end)
83 {
84 req->start = start;
85 req->end = end;
86 qemu_co_queue_init(&req->wait_queue);
87 QLIST_INSERT_HEAD(&job->inflight_reqs, req, list);
88 }
89
90 /* Forget about a completed request */
91 static void cow_request_end(CowRequest *req)
92 {
93 QLIST_REMOVE(req, list);
94 qemu_co_queue_restart_all(&req->wait_queue);
95 }
96
97 static int coroutine_fn backup_do_cow(BackupBlockJob *job,
98 int64_t sector_num, int nb_sectors,
99 bool *error_is_read,
100 bool is_write_notifier)
101 {
102 BlockBackend *blk = job->common.blk;
103 CowRequest cow_request;
104 struct iovec iov;
105 QEMUIOVector bounce_qiov;
106 void *bounce_buffer = NULL;
107 int ret = 0;
108 int64_t sectors_per_cluster = cluster_size_sectors(job);
109 int64_t start, end;
110 int n;
111
112 qemu_co_rwlock_rdlock(&job->flush_rwlock);
113
114 start = sector_num / sectors_per_cluster;
115 end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
116
117 trace_backup_do_cow_enter(job, start, sector_num, nb_sectors);
118
119 wait_for_overlapping_requests(job, start, end);
120 cow_request_begin(&cow_request, job, start, end);
121
122 for (; start < end; start++) {
123 if (test_bit(start, job->done_bitmap)) {
124 trace_backup_do_cow_skip(job, start);
125 continue; /* already copied */
126 }
127
128 trace_backup_do_cow_process(job, start);
129
130 n = MIN(sectors_per_cluster,
131 job->common.len / BDRV_SECTOR_SIZE -
132 start * sectors_per_cluster);
133
134 if (!bounce_buffer) {
135 bounce_buffer = blk_blockalign(blk, job->cluster_size);
136 }
137 iov.iov_base = bounce_buffer;
138 iov.iov_len = n * BDRV_SECTOR_SIZE;
139 qemu_iovec_init_external(&bounce_qiov, &iov, 1);
140
141 ret = blk_co_preadv(blk, start * job->cluster_size,
142 bounce_qiov.size, &bounce_qiov,
143 is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0);
144 if (ret < 0) {
145 trace_backup_do_cow_read_fail(job, start, ret);
146 if (error_is_read) {
147 *error_is_read = true;
148 }
149 goto out;
150 }
151
152 if (buffer_is_zero(iov.iov_base, iov.iov_len)) {
153 ret = blk_co_pwrite_zeroes(job->target, start * job->cluster_size,
154 bounce_qiov.size, BDRV_REQ_MAY_UNMAP);
155 } else {
156 ret = blk_co_pwritev(job->target, start * job->cluster_size,
157 bounce_qiov.size, &bounce_qiov, 0);
158 }
159 if (ret < 0) {
160 trace_backup_do_cow_write_fail(job, start, ret);
161 if (error_is_read) {
162 *error_is_read = false;
163 }
164 goto out;
165 }
166
167 set_bit(start, job->done_bitmap);
168
169 /* Publish progress, guest I/O counts as progress too. Note that the
170 * offset field is an opaque progress value, it is not a disk offset.
171 */
172 job->sectors_read += n;
173 job->common.offset += n * BDRV_SECTOR_SIZE;
174 }
175
176 out:
177 if (bounce_buffer) {
178 qemu_vfree(bounce_buffer);
179 }
180
181 cow_request_end(&cow_request);
182
183 trace_backup_do_cow_return(job, sector_num, nb_sectors, ret);
184
185 qemu_co_rwlock_unlock(&job->flush_rwlock);
186
187 return ret;
188 }
189
190 static int coroutine_fn backup_before_write_notify(
191 NotifierWithReturn *notifier,
192 void *opaque)
193 {
194 BackupBlockJob *job = container_of(notifier, BackupBlockJob, before_write);
195 BdrvTrackedRequest *req = opaque;
196 int64_t sector_num = req->offset >> BDRV_SECTOR_BITS;
197 int nb_sectors = req->bytes >> BDRV_SECTOR_BITS;
198
199 assert(req->bs == blk_bs(job->common.blk));
200 assert((req->offset & (BDRV_SECTOR_SIZE - 1)) == 0);
201 assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
202
203 return backup_do_cow(job, sector_num, nb_sectors, NULL, true);
204 }
205
206 static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp)
207 {
208 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
209
210 if (speed < 0) {
211 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
212 return;
213 }
214 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
215 }
216
217 static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
218 {
219 BdrvDirtyBitmap *bm;
220 BlockDriverState *bs = blk_bs(job->common.blk);
221
222 if (ret < 0 || block_job_is_cancelled(&job->common)) {
223 /* Merge the successor back into the parent, delete nothing. */
224 bm = bdrv_reclaim_dirty_bitmap(bs, job->sync_bitmap, NULL);
225 assert(bm);
226 } else {
227 /* Everything is fine, delete this bitmap and install the backup. */
228 bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL);
229 assert(bm);
230 }
231 }
232
233 static void backup_commit(BlockJob *job)
234 {
235 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
236 if (s->sync_bitmap) {
237 backup_cleanup_sync_bitmap(s, 0);
238 }
239 }
240
241 static void backup_abort(BlockJob *job)
242 {
243 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
244 if (s->sync_bitmap) {
245 backup_cleanup_sync_bitmap(s, -1);
246 }
247 }
248
249 static const BlockJobDriver backup_job_driver = {
250 .instance_size = sizeof(BackupBlockJob),
251 .job_type = BLOCK_JOB_TYPE_BACKUP,
252 .set_speed = backup_set_speed,
253 .commit = backup_commit,
254 .abort = backup_abort,
255 };
256
257 static BlockErrorAction backup_error_action(BackupBlockJob *job,
258 bool read, int error)
259 {
260 if (read) {
261 return block_job_error_action(&job->common, job->on_source_error,
262 true, error);
263 } else {
264 return block_job_error_action(&job->common, job->on_target_error,
265 false, error);
266 }
267 }
268
269 typedef struct {
270 int ret;
271 } BackupCompleteData;
272
273 static void backup_complete(BlockJob *job, void *opaque)
274 {
275 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
276 BackupCompleteData *data = opaque;
277
278 blk_unref(s->target);
279
280 block_job_completed(job, data->ret);
281 g_free(data);
282 }
283
284 static bool coroutine_fn yield_and_check(BackupBlockJob *job)
285 {
286 if (block_job_is_cancelled(&job->common)) {
287 return true;
288 }
289
290 /* we need to yield so that bdrv_drain_all() returns.
291 * (without, VM does not reboot)
292 */
293 if (job->common.speed) {
294 uint64_t delay_ns = ratelimit_calculate_delay(&job->limit,
295 job->sectors_read);
296 job->sectors_read = 0;
297 block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns);
298 } else {
299 block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0);
300 }
301
302 if (block_job_is_cancelled(&job->common)) {
303 return true;
304 }
305
306 return false;
307 }
308
309 static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
310 {
311 bool error_is_read;
312 int ret = 0;
313 int clusters_per_iter;
314 uint32_t granularity;
315 int64_t sector;
316 int64_t cluster;
317 int64_t end;
318 int64_t last_cluster = -1;
319 int64_t sectors_per_cluster = cluster_size_sectors(job);
320 HBitmapIter hbi;
321
322 granularity = bdrv_dirty_bitmap_granularity(job->sync_bitmap);
323 clusters_per_iter = MAX((granularity / job->cluster_size), 1);
324 bdrv_dirty_iter_init(job->sync_bitmap, &hbi);
325
326 /* Find the next dirty sector(s) */
327 while ((sector = hbitmap_iter_next(&hbi)) != -1) {
328 cluster = sector / sectors_per_cluster;
329
330 /* Fake progress updates for any clusters we skipped */
331 if (cluster != last_cluster + 1) {
332 job->common.offset += ((cluster - last_cluster - 1) *
333 job->cluster_size);
334 }
335
336 for (end = cluster + clusters_per_iter; cluster < end; cluster++) {
337 do {
338 if (yield_and_check(job)) {
339 return ret;
340 }
341 ret = backup_do_cow(job, cluster * sectors_per_cluster,
342 sectors_per_cluster, &error_is_read,
343 false);
344 if ((ret < 0) &&
345 backup_error_action(job, error_is_read, -ret) ==
346 BLOCK_ERROR_ACTION_REPORT) {
347 return ret;
348 }
349 } while (ret < 0);
350 }
351
352 /* If the bitmap granularity is smaller than the backup granularity,
353 * we need to advance the iterator pointer to the next cluster. */
354 if (granularity < job->cluster_size) {
355 bdrv_set_dirty_iter(&hbi, cluster * sectors_per_cluster);
356 }
357
358 last_cluster = cluster - 1;
359 }
360
361 /* Play some final catchup with the progress meter */
362 end = DIV_ROUND_UP(job->common.len, job->cluster_size);
363 if (last_cluster + 1 < end) {
364 job->common.offset += ((end - last_cluster - 1) * job->cluster_size);
365 }
366
367 return ret;
368 }
369
370 static void coroutine_fn backup_run(void *opaque)
371 {
372 BackupBlockJob *job = opaque;
373 BackupCompleteData *data;
374 BlockDriverState *bs = blk_bs(job->common.blk);
375 BlockBackend *target = job->target;
376 int64_t start, end;
377 int64_t sectors_per_cluster = cluster_size_sectors(job);
378 int ret = 0;
379
380 QLIST_INIT(&job->inflight_reqs);
381 qemu_co_rwlock_init(&job->flush_rwlock);
382
383 start = 0;
384 end = DIV_ROUND_UP(job->common.len, job->cluster_size);
385
386 job->done_bitmap = bitmap_new(end);
387
388 job->before_write.notify = backup_before_write_notify;
389 bdrv_add_before_write_notifier(bs, &job->before_write);
390
391 if (job->sync_mode == MIRROR_SYNC_MODE_NONE) {
392 while (!block_job_is_cancelled(&job->common)) {
393 /* Yield until the job is cancelled. We just let our before_write
394 * notify callback service CoW requests. */
395 job->common.busy = false;
396 qemu_coroutine_yield();
397 job->common.busy = true;
398 }
399 } else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
400 ret = backup_run_incremental(job);
401 } else {
402 /* Both FULL and TOP SYNC_MODE's require copying.. */
403 for (; start < end; start++) {
404 bool error_is_read;
405 if (yield_and_check(job)) {
406 break;
407 }
408
409 if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
410 int i, n;
411 int alloced = 0;
412
413 /* Check to see if these blocks are already in the
414 * backing file. */
415
416 for (i = 0; i < sectors_per_cluster;) {
417 /* bdrv_is_allocated() only returns true/false based
418 * on the first set of sectors it comes across that
419 * are are all in the same state.
420 * For that reason we must verify each sector in the
421 * backup cluster length. We end up copying more than
422 * needed but at some point that is always the case. */
423 alloced =
424 bdrv_is_allocated(bs,
425 start * sectors_per_cluster + i,
426 sectors_per_cluster - i, &n);
427 i += n;
428
429 if (alloced == 1 || n == 0) {
430 break;
431 }
432 }
433
434 /* If the above loop never found any sectors that are in
435 * the topmost image, skip this backup. */
436 if (alloced == 0) {
437 continue;
438 }
439 }
440 /* FULL sync mode we copy the whole drive. */
441 ret = backup_do_cow(job, start * sectors_per_cluster,
442 sectors_per_cluster, &error_is_read, false);
443 if (ret < 0) {
444 /* Depending on error action, fail now or retry cluster */
445 BlockErrorAction action =
446 backup_error_action(job, error_is_read, -ret);
447 if (action == BLOCK_ERROR_ACTION_REPORT) {
448 break;
449 } else {
450 start--;
451 continue;
452 }
453 }
454 }
455 }
456
457 notifier_with_return_remove(&job->before_write);
458
459 /* wait until pending backup_do_cow() calls have completed */
460 qemu_co_rwlock_wrlock(&job->flush_rwlock);
461 qemu_co_rwlock_unlock(&job->flush_rwlock);
462 g_free(job->done_bitmap);
463
464 bdrv_op_unblock_all(blk_bs(target), job->common.blocker);
465
466 data = g_malloc(sizeof(*data));
467 data->ret = ret;
468 block_job_defer_to_main_loop(&job->common, backup_complete, data);
469 }
470
471 void backup_start(BlockDriverState *bs, BlockDriverState *target,
472 int64_t speed, MirrorSyncMode sync_mode,
473 BdrvDirtyBitmap *sync_bitmap,
474 BlockdevOnError on_source_error,
475 BlockdevOnError on_target_error,
476 BlockCompletionFunc *cb, void *opaque,
477 BlockJobTxn *txn, Error **errp)
478 {
479 int64_t len;
480 BlockDriverInfo bdi;
481 BackupBlockJob *job = NULL;
482 int ret;
483
484 assert(bs);
485 assert(target);
486 assert(cb);
487
488 if (bs == target) {
489 error_setg(errp, "Source and target cannot be the same");
490 return;
491 }
492
493 if (!bdrv_is_inserted(bs)) {
494 error_setg(errp, "Device is not inserted: %s",
495 bdrv_get_device_name(bs));
496 return;
497 }
498
499 if (!bdrv_is_inserted(target)) {
500 error_setg(errp, "Device is not inserted: %s",
501 bdrv_get_device_name(target));
502 return;
503 }
504
505 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
506 return;
507 }
508
509 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
510 return;
511 }
512
513 if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
514 if (!sync_bitmap) {
515 error_setg(errp, "must provide a valid bitmap name for "
516 "\"incremental\" sync mode");
517 return;
518 }
519
520 /* Create a new bitmap, and freeze/disable this one. */
521 if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) {
522 return;
523 }
524 } else if (sync_bitmap) {
525 error_setg(errp,
526 "a sync_bitmap was provided to backup_run, "
527 "but received an incompatible sync_mode (%s)",
528 MirrorSyncMode_lookup[sync_mode]);
529 return;
530 }
531
532 len = bdrv_getlength(bs);
533 if (len < 0) {
534 error_setg_errno(errp, -len, "unable to get length for '%s'",
535 bdrv_get_device_name(bs));
536 goto error;
537 }
538
539 job = block_job_create(&backup_job_driver, bs, speed, cb, opaque, errp);
540 if (!job) {
541 goto error;
542 }
543
544 job->target = blk_new();
545 blk_insert_bs(job->target, target);
546
547 job->on_source_error = on_source_error;
548 job->on_target_error = on_target_error;
549 job->sync_mode = sync_mode;
550 job->sync_bitmap = sync_mode == MIRROR_SYNC_MODE_INCREMENTAL ?
551 sync_bitmap : NULL;
552
553 /* If there is no backing file on the target, we cannot rely on COW if our
554 * backup cluster size is smaller than the target cluster size. Even for
555 * targets with a backing file, try to avoid COW if possible. */
556 ret = bdrv_get_info(target, &bdi);
557 if (ret < 0 && !target->backing) {
558 error_setg_errno(errp, -ret,
559 "Couldn't determine the cluster size of the target image, "
560 "which has no backing file");
561 error_append_hint(errp,
562 "Aborting, since this may create an unusable destination image\n");
563 goto error;
564 } else if (ret < 0 && target->backing) {
565 /* Not fatal; just trudge on ahead. */
566 job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
567 } else {
568 job->cluster_size = MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
569 }
570
571 bdrv_op_block_all(target, job->common.blocker);
572 job->common.len = len;
573 job->common.co = qemu_coroutine_create(backup_run);
574 block_job_txn_add_job(txn, &job->common);
575 qemu_coroutine_enter(job->common.co, job);
576 return;
577
578 error:
579 if (sync_bitmap) {
580 bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
581 }
582 if (job) {
583 blk_unref(job->target);
584 block_job_unref(&job->common);
585 }
586 }