]> git.proxmox.com Git - mirror_qemu.git/blame - block/backup.c
CODING_STYLE: specify the indent rule for multiline code
[mirror_qemu.git] / block / backup.c
CommitLineData
98d2c6f2
DM
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
80c71a24 14#include "qemu/osdep.h"
98d2c6f2
DM
15
16#include "trace.h"
17#include "block/block.h"
18#include "block/block_int.h"
c87621ea 19#include "block/blockjob_int.h"
49d3e828 20#include "block/block_backup.h"
da34e65c 21#include "qapi/error.h"
cc7a8ea7 22#include "qapi/qmp/qerror.h"
98d2c6f2 23#include "qemu/ratelimit.h"
f348b6d1 24#include "qemu/cutils.h"
373340b2 25#include "sysemu/block-backend.h"
b2f56462 26#include "qemu/bitmap.h"
a410a7f1 27#include "qemu/error-report.h"
98d2c6f2 28
16096a4d 29#define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
98d2c6f2 30
3a75187f
VSO
31typedef struct CowRequest {
32 int64_t start_byte;
33 int64_t end_byte;
34 QLIST_ENTRY(CowRequest) list;
35 CoQueue wait_queue; /* coroutines blocked on this request */
36} CowRequest;
37
98d2c6f2
DM
38typedef struct BackupBlockJob {
39 BlockJob common;
5c438bc6 40 BlockBackend *target;
4b80ab2b 41 /* bitmap for sync=incremental */
d58d8453 42 BdrvDirtyBitmap *sync_bitmap;
fc5d3f84 43 MirrorSyncMode sync_mode;
98d2c6f2
DM
44 BlockdevOnError on_source_error;
45 BlockdevOnError on_target_error;
46 CoRwlock flush_rwlock;
05df8a6a 47 uint64_t len;
cf79cdf6 48 uint64_t bytes_read;
16096a4d 49 int64_t cluster_size;
13b9414b 50 bool compress;
12b3e52e 51 NotifierWithReturn before_write;
98d2c6f2 52 QLIST_HEAD(, CowRequest) inflight_reqs;
a193b0f0
VSO
53
54 HBitmap *copy_bitmap;
9ded4a01
FZ
55 bool use_copy_range;
56 int64_t copy_range_size;
f8d59dfb
VSO
57
58 bool serialize_target_writes;
98d2c6f2
DM
59} BackupBlockJob;
60
bd21935b
KW
61static const BlockJobDriver backup_job_driver;
62
98d2c6f2
DM
63/* See if in-flight requests overlap and wait for them to complete */
64static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job,
65 int64_t start,
66 int64_t end)
67{
68 CowRequest *req;
69 bool retry;
70
71 do {
72 retry = false;
73 QLIST_FOREACH(req, &job->inflight_reqs, list) {
f6ac2078 74 if (end > req->start_byte && start < req->end_byte) {
1ace7cea 75 qemu_co_queue_wait(&req->wait_queue, NULL);
98d2c6f2
DM
76 retry = true;
77 break;
78 }
79 }
80 } while (retry);
81}
82
83/* Keep track of an in-flight request */
84static void cow_request_begin(CowRequest *req, BackupBlockJob *job,
f6ac2078 85 int64_t start, int64_t end)
98d2c6f2 86{
f6ac2078
EB
87 req->start_byte = start;
88 req->end_byte = end;
98d2c6f2
DM
89 qemu_co_queue_init(&req->wait_queue);
90 QLIST_INSERT_HEAD(&job->inflight_reqs, req, list);
91}
92
93/* Forget about a completed request */
94static void cow_request_end(CowRequest *req)
95{
96 QLIST_REMOVE(req, list);
97 qemu_co_queue_restart_all(&req->wait_queue);
98}
99
9ded4a01 100/* Copy range to target with a bounce buffer and return the bytes copied. If
50d6a8a3 101 * error occurred, return a negative error number */
9ded4a01
FZ
102static int coroutine_fn backup_cow_with_bounce_buffer(BackupBlockJob *job,
103 int64_t start,
104 int64_t end,
105 bool is_write_notifier,
106 bool *error_is_read,
107 void **bounce_buffer)
108{
109 int ret;
9ded4a01
FZ
110 BlockBackend *blk = job->common.blk;
111 int nbytes;
f8d59dfb
VSO
112 int read_flags = is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0;
113 int write_flags = job->serialize_target_writes ? BDRV_REQ_SERIALISING : 0;
9ded4a01
FZ
114
115 hbitmap_reset(job->copy_bitmap, start / job->cluster_size, 1);
116 nbytes = MIN(job->cluster_size, job->len - start);
117 if (!*bounce_buffer) {
118 *bounce_buffer = blk_blockalign(blk, job->cluster_size);
119 }
9ded4a01 120
607dbdc4 121 ret = blk_co_pread(blk, start, nbytes, *bounce_buffer, read_flags);
9ded4a01
FZ
122 if (ret < 0) {
123 trace_backup_do_cow_read_fail(job, start, ret);
124 if (error_is_read) {
125 *error_is_read = true;
126 }
127 goto fail;
128 }
129
607dbdc4 130 if (buffer_is_zero(*bounce_buffer, nbytes)) {
9ded4a01 131 ret = blk_co_pwrite_zeroes(job->target, start,
607dbdc4 132 nbytes, write_flags | BDRV_REQ_MAY_UNMAP);
9ded4a01 133 } else {
607dbdc4
VSO
134 ret = blk_co_pwrite(job->target, start,
135 nbytes, *bounce_buffer, write_flags |
136 (job->compress ? BDRV_REQ_WRITE_COMPRESSED : 0));
9ded4a01
FZ
137 }
138 if (ret < 0) {
139 trace_backup_do_cow_write_fail(job, start, ret);
140 if (error_is_read) {
141 *error_is_read = false;
142 }
143 goto fail;
144 }
145
146 return nbytes;
147fail:
148 hbitmap_set(job->copy_bitmap, start / job->cluster_size, 1);
149 return ret;
150
151}
152
50d6a8a3 153/* Copy range to target and return the bytes copied. If error occurred, return a
9ded4a01
FZ
154 * negative error number. */
155static int coroutine_fn backup_cow_with_offload(BackupBlockJob *job,
156 int64_t start,
157 int64_t end,
158 bool is_write_notifier)
159{
160 int ret;
161 int nr_clusters;
162 BlockBackend *blk = job->common.blk;
163 int nbytes;
f8d59dfb
VSO
164 int read_flags = is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0;
165 int write_flags = job->serialize_target_writes ? BDRV_REQ_SERIALISING : 0;
9ded4a01
FZ
166
167 assert(QEMU_IS_ALIGNED(job->copy_range_size, job->cluster_size));
168 nbytes = MIN(job->copy_range_size, end - start);
169 nr_clusters = DIV_ROUND_UP(nbytes, job->cluster_size);
170 hbitmap_reset(job->copy_bitmap, start / job->cluster_size,
171 nr_clusters);
172 ret = blk_co_copy_range(blk, start, job->target, start, nbytes,
f8d59dfb 173 read_flags, write_flags);
9ded4a01
FZ
174 if (ret < 0) {
175 trace_backup_do_cow_copy_range_fail(job, start, ret);
176 hbitmap_set(job->copy_bitmap, start / job->cluster_size,
177 nr_clusters);
178 return ret;
179 }
180
181 return nbytes;
182}
183
8543c274 184static int coroutine_fn backup_do_cow(BackupBlockJob *job,
03f5d60b 185 int64_t offset, uint64_t bytes,
06c3916b
WC
186 bool *error_is_read,
187 bool is_write_notifier)
98d2c6f2 188{
98d2c6f2 189 CowRequest cow_request;
98d2c6f2 190 int ret = 0;
03f5d60b 191 int64_t start, end; /* bytes */
9ded4a01 192 void *bounce_buffer = NULL;
98d2c6f2
DM
193
194 qemu_co_rwlock_rdlock(&job->flush_rwlock);
195
03f5d60b
EB
196 start = QEMU_ALIGN_DOWN(offset, job->cluster_size);
197 end = QEMU_ALIGN_UP(bytes + offset, job->cluster_size);
98d2c6f2 198
03f5d60b 199 trace_backup_do_cow_enter(job, start, offset, bytes);
98d2c6f2 200
03f5d60b
EB
201 wait_for_overlapping_requests(job, start, end);
202 cow_request_begin(&cow_request, job, start, end);
98d2c6f2 203
9ded4a01 204 while (start < end) {
a193b0f0 205 if (!hbitmap_get(job->copy_bitmap, start / job->cluster_size)) {
03f5d60b 206 trace_backup_do_cow_skip(job, start);
9ded4a01 207 start += job->cluster_size;
98d2c6f2
DM
208 continue; /* already copied */
209 }
210
03f5d60b 211 trace_backup_do_cow_process(job, start);
98d2c6f2 212
9ded4a01
FZ
213 if (job->use_copy_range) {
214 ret = backup_cow_with_offload(job, start, end, is_write_notifier);
215 if (ret < 0) {
216 job->use_copy_range = false;
98d2c6f2 217 }
98d2c6f2 218 }
9ded4a01
FZ
219 if (!job->use_copy_range) {
220 ret = backup_cow_with_bounce_buffer(job, start, end, is_write_notifier,
221 error_is_read, &bounce_buffer);
98d2c6f2
DM
222 }
223 if (ret < 0) {
9ded4a01 224 break;
98d2c6f2
DM
225 }
226
98d2c6f2
DM
227 /* Publish progress, guest I/O counts as progress too. Note that the
228 * offset field is an opaque progress value, it is not a disk offset.
229 */
9ded4a01
FZ
230 start += ret;
231 job->bytes_read += ret;
232 job_progress_update(&job->common.job, ret);
233 ret = 0;
98d2c6f2
DM
234 }
235
98d2c6f2
DM
236 if (bounce_buffer) {
237 qemu_vfree(bounce_buffer);
238 }
239
240 cow_request_end(&cow_request);
241
03f5d60b 242 trace_backup_do_cow_return(job, offset, bytes, ret);
98d2c6f2
DM
243
244 qemu_co_rwlock_unlock(&job->flush_rwlock);
245
246 return ret;
247}
248
249static int coroutine_fn backup_before_write_notify(
250 NotifierWithReturn *notifier,
251 void *opaque)
252{
12b3e52e 253 BackupBlockJob *job = container_of(notifier, BackupBlockJob, before_write);
98d2c6f2
DM
254 BdrvTrackedRequest *req = opaque;
255
5c438bc6 256 assert(req->bs == blk_bs(job->common.blk));
03f5d60b
EB
257 assert(QEMU_IS_ALIGNED(req->offset, BDRV_SECTOR_SIZE));
258 assert(QEMU_IS_ALIGNED(req->bytes, BDRV_SECTOR_SIZE));
793ed47a 259
03f5d60b 260 return backup_do_cow(job, req->offset, req->bytes, NULL, true);
98d2c6f2
DM
261}
262
b976ea3c
FZ
263static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
264{
265 BdrvDirtyBitmap *bm;
5c438bc6 266 BlockDriverState *bs = blk_bs(job->common.blk);
b976ea3c 267
35d6b368 268 if (ret < 0) {
b976ea3c
FZ
269 /* Merge the successor back into the parent, delete nothing. */
270 bm = bdrv_reclaim_dirty_bitmap(bs, job->sync_bitmap, NULL);
271 assert(bm);
272 } else {
273 /* Everything is fine, delete this bitmap and install the backup. */
274 bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL);
275 assert(bm);
276 }
277}
278
4ad35181 279static void backup_commit(Job *job)
c347b2c6 280{
4ad35181 281 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
c347b2c6
JS
282 if (s->sync_bitmap) {
283 backup_cleanup_sync_bitmap(s, 0);
284 }
285}
286
4ad35181 287static void backup_abort(Job *job)
c347b2c6 288{
4ad35181 289 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
c347b2c6
JS
290 if (s->sync_bitmap) {
291 backup_cleanup_sync_bitmap(s, -1);
292 }
293}
294
4ad35181 295static void backup_clean(Job *job)
e8a40bf7 296{
4ad35181 297 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
e8a40bf7
JS
298 assert(s->target);
299 blk_unref(s->target);
300 s->target = NULL;
301}
302
5ab4b69c
SH
303static void backup_attached_aio_context(BlockJob *job, AioContext *aio_context)
304{
305 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
306
307 blk_set_aio_context(s->target, aio_context);
308}
309
49d3e828
WC
310void backup_do_checkpoint(BlockJob *job, Error **errp)
311{
312 BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
313 int64_t len;
314
bd21935b 315 assert(block_job_driver(job) == &backup_job_driver);
49d3e828
WC
316
317 if (backup_job->sync_mode != MIRROR_SYNC_MODE_NONE) {
318 error_setg(errp, "The backup job only supports block checkpoint in"
319 " sync=none mode");
320 return;
321 }
322
05df8a6a 323 len = DIV_ROUND_UP(backup_job->len, backup_job->cluster_size);
a193b0f0 324 hbitmap_set(backup_job->copy_bitmap, 0, len);
49d3e828
WC
325}
326
bae8196d
PB
327static void backup_drain(BlockJob *job)
328{
329 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
330
331 /* Need to keep a reference in case blk_drain triggers execution
332 * of backup_complete...
333 */
334 if (s->target) {
335 BlockBackend *target = s->target;
336 blk_ref(target);
337 blk_drain(target);
338 blk_unref(target);
339 }
340}
341
98d2c6f2
DM
342static BlockErrorAction backup_error_action(BackupBlockJob *job,
343 bool read, int error)
344{
345 if (read) {
81e254dc
KW
346 return block_job_error_action(&job->common, job->on_source_error,
347 true, error);
98d2c6f2 348 } else {
81e254dc
KW
349 return block_job_error_action(&job->common, job->on_target_error,
350 false, error);
98d2c6f2
DM
351 }
352}
353
d58d8453
JS
354static bool coroutine_fn yield_and_check(BackupBlockJob *job)
355{
dee81d51
KW
356 uint64_t delay_ns;
357
daa7f2f9 358 if (job_is_cancelled(&job->common.job)) {
d58d8453
JS
359 return true;
360 }
361
dee81d51
KW
362 /* We need to yield even for delay_ns = 0 so that bdrv_drain_all() can
363 * return. Without a yield, the VM would not reboot. */
364 delay_ns = block_job_ratelimit_get_delay(&job->common, job->bytes_read);
365 job->bytes_read = 0;
5d43e86e 366 job_sleep_ns(&job->common.job, delay_ns);
d58d8453 367
daa7f2f9 368 if (job_is_cancelled(&job->common.job)) {
d58d8453
JS
369 return true;
370 }
371
372 return false;
373}
374
375static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
376{
53f1c879 377 int ret;
d58d8453 378 bool error_is_read;
d58d8453 379 int64_t cluster;
53f1c879 380 HBitmapIter hbi;
d58d8453 381
53f1c879 382 hbitmap_iter_init(&hbi, job->copy_bitmap, 0);
19c021e1 383 while ((cluster = hbitmap_iter_next(&hbi)) != -1) {
53f1c879
VSO
384 do {
385 if (yield_and_check(job)) {
386 return 0;
387 }
388 ret = backup_do_cow(job, cluster * job->cluster_size,
389 job->cluster_size, &error_is_read, false);
390 if (ret < 0 && backup_error_action(job, error_is_read, -ret) ==
391 BLOCK_ERROR_ACTION_REPORT)
392 {
393 return ret;
394 }
395 } while (ret < 0);
d58d8453
JS
396 }
397
53f1c879 398 return 0;
d58d8453
JS
399}
400
8cc6dc62
VSO
401/* init copy_bitmap from sync_bitmap */
402static void backup_incremental_init_copy_bitmap(BackupBlockJob *job)
403{
404 BdrvDirtyBitmapIter *dbi;
405 int64_t offset;
406 int64_t end = DIV_ROUND_UP(bdrv_dirty_bitmap_size(job->sync_bitmap),
407 job->cluster_size);
408
409 dbi = bdrv_dirty_iter_new(job->sync_bitmap);
410 while ((offset = bdrv_dirty_iter_next(dbi)) != -1) {
411 int64_t cluster = offset / job->cluster_size;
412 int64_t next_cluster;
413
414 offset += bdrv_dirty_bitmap_granularity(job->sync_bitmap);
415 if (offset >= bdrv_dirty_bitmap_size(job->sync_bitmap)) {
416 hbitmap_set(job->copy_bitmap, cluster, end - cluster);
417 break;
418 }
419
76d570dc
VSO
420 offset = bdrv_dirty_bitmap_next_zero(job->sync_bitmap, offset,
421 UINT64_MAX);
8cc6dc62
VSO
422 if (offset == -1) {
423 hbitmap_set(job->copy_bitmap, cluster, end - cluster);
424 break;
425 }
426
427 next_cluster = DIV_ROUND_UP(offset, job->cluster_size);
428 hbitmap_set(job->copy_bitmap, cluster, next_cluster - cluster);
429 if (next_cluster >= end) {
430 break;
431 }
432
433 bdrv_set_dirty_iter(dbi, next_cluster * job->cluster_size);
434 }
435
30a5c887
KW
436 /* TODO job_progress_set_remaining() would make more sense */
437 job_progress_update(&job->common.job,
05df8a6a 438 job->len - hbitmap_count(job->copy_bitmap) * job->cluster_size);
085bd08e 439
8cc6dc62
VSO
440 bdrv_dirty_iter_free(dbi);
441}
442
68702775 443static int coroutine_fn backup_run(Job *job, Error **errp)
98d2c6f2 444{
68702775
JS
445 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
446 BlockDriverState *bs = blk_bs(s->common.blk);
a193b0f0 447 int64_t offset, nb_clusters;
98d2c6f2
DM
448 int ret = 0;
449
68702775
JS
450 QLIST_INIT(&s->inflight_reqs);
451 qemu_co_rwlock_init(&s->flush_rwlock);
98d2c6f2 452
68702775
JS
453 nb_clusters = DIV_ROUND_UP(s->len, s->cluster_size);
454 job_progress_set_remaining(job, s->len);
05df8a6a 455
68702775
JS
456 s->copy_bitmap = hbitmap_alloc(nb_clusters, 0);
457 if (s->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
458 backup_incremental_init_copy_bitmap(s);
8cc6dc62 459 } else {
68702775 460 hbitmap_set(s->copy_bitmap, 0, nb_clusters);
8cc6dc62
VSO
461 }
462
98d2c6f2 463
68702775
JS
464 s->before_write.notify = backup_before_write_notify;
465 bdrv_add_before_write_notifier(bs, &s->before_write);
98d2c6f2 466
68702775 467 if (s->sync_mode == MIRROR_SYNC_MODE_NONE) {
a193b0f0
VSO
468 /* All bits are set in copy_bitmap to allow any cluster to be copied.
469 * This does not actually require them to be copied. */
68702775 470 while (!job_is_cancelled(job)) {
fc5d3f84
IM
471 /* Yield until the job is cancelled. We just let our before_write
472 * notify callback service CoW requests. */
68702775 473 job_yield(job);
98d2c6f2 474 }
68702775
JS
475 } else if (s->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
476 ret = backup_run_incremental(s);
fc5d3f84
IM
477 } else {
478 /* Both FULL and TOP SYNC_MODE's require copying.. */
68702775
JS
479 for (offset = 0; offset < s->len;
480 offset += s->cluster_size) {
fc5d3f84 481 bool error_is_read;
666a9543
EB
482 int alloced = 0;
483
68702775 484 if (yield_and_check(s)) {
98d2c6f2 485 break;
fc5d3f84
IM
486 }
487
68702775 488 if (s->sync_mode == MIRROR_SYNC_MODE_TOP) {
d6a644bb
EB
489 int i;
490 int64_t n;
fc5d3f84
IM
491
492 /* Check to see if these blocks are already in the
493 * backing file. */
494
68702775 495 for (i = 0; i < s->cluster_size;) {
bdad13b9 496 /* bdrv_is_allocated() only returns true/false based
4c293dc6 497 * on the first set of sectors it comes across that
fc5d3f84
IM
498 * are are all in the same state.
499 * For that reason we must verify each sector in the
500 * backup cluster length. We end up copying more than
501 * needed but at some point that is always the case. */
502 alloced =
d6a644bb 503 bdrv_is_allocated(bs, offset + i,
68702775 504 s->cluster_size - i, &n);
fc5d3f84
IM
505 i += n;
506
666a9543 507 if (alloced || n == 0) {
fc5d3f84
IM
508 break;
509 }
510 }
511
512 /* If the above loop never found any sectors that are in
513 * the topmost image, skip this backup. */
514 if (alloced == 0) {
515 continue;
516 }
517 }
518 /* FULL sync mode we copy the whole drive. */
666a9543
EB
519 if (alloced < 0) {
520 ret = alloced;
521 } else {
68702775 522 ret = backup_do_cow(s, offset, s->cluster_size,
6f8e35e2 523 &error_is_read, false);
666a9543 524 }
fc5d3f84
IM
525 if (ret < 0) {
526 /* Depending on error action, fail now or retry cluster */
527 BlockErrorAction action =
68702775 528 backup_error_action(s, error_is_read, -ret);
a589569f 529 if (action == BLOCK_ERROR_ACTION_REPORT) {
fc5d3f84
IM
530 break;
531 } else {
68702775 532 offset -= s->cluster_size;
fc5d3f84
IM
533 continue;
534 }
98d2c6f2
DM
535 }
536 }
537 }
538
68702775 539 notifier_with_return_remove(&s->before_write);
98d2c6f2
DM
540
541 /* wait until pending backup_do_cow() calls have completed */
68702775
JS
542 qemu_co_rwlock_wrlock(&s->flush_rwlock);
543 qemu_co_rwlock_unlock(&s->flush_rwlock);
544 hbitmap_free(s->copy_bitmap);
98d2c6f2 545
f67432a2 546 return ret;
98d2c6f2
DM
547}
548
a7815a76 549static const BlockJobDriver backup_job_driver = {
33e9e9bd
KW
550 .job_driver = {
551 .instance_size = sizeof(BackupBlockJob),
252291ea 552 .job_type = JOB_TYPE_BACKUP,
80fa2c75 553 .free = block_job_free,
b15de828 554 .user_resume = block_job_user_resume,
b69f777d 555 .drain = block_job_drain,
f67432a2 556 .run = backup_run,
4ad35181
KW
557 .commit = backup_commit,
558 .abort = backup_abort,
559 .clean = backup_clean,
33e9e9bd 560 },
a7815a76
JS
561 .attached_aio_context = backup_attached_aio_context,
562 .drain = backup_drain,
563};
564
111049a4 565BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
70559d49
AG
566 BlockDriverState *target, int64_t speed,
567 MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap,
13b9414b 568 bool compress,
98d2c6f2
DM
569 BlockdevOnError on_source_error,
570 BlockdevOnError on_target_error,
47970dfb 571 int creation_flags,
097310b5 572 BlockCompletionFunc *cb, void *opaque,
62c9e416 573 JobTxn *txn, Error **errp)
98d2c6f2
DM
574{
575 int64_t len;
4c9bca7e 576 BlockDriverInfo bdi;
91ab6883 577 BackupBlockJob *job = NULL;
4c9bca7e 578 int ret;
98d2c6f2
DM
579
580 assert(bs);
581 assert(target);
98d2c6f2 582
c29c1dd3
FZ
583 if (bs == target) {
584 error_setg(errp, "Source and target cannot be the same");
111049a4 585 return NULL;
c29c1dd3
FZ
586 }
587
c29c1dd3
FZ
588 if (!bdrv_is_inserted(bs)) {
589 error_setg(errp, "Device is not inserted: %s",
590 bdrv_get_device_name(bs));
111049a4 591 return NULL;
c29c1dd3
FZ
592 }
593
594 if (!bdrv_is_inserted(target)) {
595 error_setg(errp, "Device is not inserted: %s",
596 bdrv_get_device_name(target));
111049a4 597 return NULL;
c29c1dd3
FZ
598 }
599
13b9414b
PB
600 if (compress && target->drv->bdrv_co_pwritev_compressed == NULL) {
601 error_setg(errp, "Compression is not supported for this drive %s",
602 bdrv_get_device_name(target));
111049a4 603 return NULL;
13b9414b
PB
604 }
605
c29c1dd3 606 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
111049a4 607 return NULL;
c29c1dd3
FZ
608 }
609
610 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
111049a4 611 return NULL;
c29c1dd3
FZ
612 }
613
4b80ab2b 614 if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
d58d8453
JS
615 if (!sync_bitmap) {
616 error_setg(errp, "must provide a valid bitmap name for "
4b80ab2b 617 "\"incremental\" sync mode");
111049a4 618 return NULL;
d58d8453
JS
619 }
620
621 /* Create a new bitmap, and freeze/disable this one. */
622 if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) {
111049a4 623 return NULL;
d58d8453
JS
624 }
625 } else if (sync_bitmap) {
626 error_setg(errp,
627 "a sync_bitmap was provided to backup_run, "
628 "but received an incompatible sync_mode (%s)",
977c736f 629 MirrorSyncMode_str(sync_mode));
111049a4 630 return NULL;
d58d8453
JS
631 }
632
98d2c6f2
DM
633 len = bdrv_getlength(bs);
634 if (len < 0) {
635 error_setg_errno(errp, -len, "unable to get length for '%s'",
636 bdrv_get_device_name(bs));
d58d8453 637 goto error;
98d2c6f2
DM
638 }
639
05df8a6a 640 /* job->len is fixed, so we can't allow resize */
75859b94 641 job = block_job_create(job_id, &backup_job_driver, txn, bs,
4e9e4323
KW
642 BLK_PERM_CONSISTENT_READ,
643 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE |
644 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_GRAPH_MOD,
c6cc12bf 645 speed, creation_flags, cb, opaque, errp);
98d2c6f2 646 if (!job) {
d58d8453 647 goto error;
98d2c6f2
DM
648 }
649
4e9e4323
KW
650 /* The target must match the source in size, so no resize here either */
651 job->target = blk_new(BLK_PERM_WRITE,
652 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE |
653 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_GRAPH_MOD);
d7086422
KW
654 ret = blk_insert_bs(job->target, target, errp);
655 if (ret < 0) {
656 goto error;
657 }
5c438bc6 658
98d2c6f2
DM
659 job->on_source_error = on_source_error;
660 job->on_target_error = on_target_error;
fc5d3f84 661 job->sync_mode = sync_mode;
4b80ab2b 662 job->sync_bitmap = sync_mode == MIRROR_SYNC_MODE_INCREMENTAL ?
d58d8453 663 sync_bitmap : NULL;
13b9414b 664 job->compress = compress;
4c9bca7e 665
f8d59dfb
VSO
666 /* Detect image-fleecing (and similar) schemes */
667 job->serialize_target_writes = bdrv_chain_contains(target, bs);
668
4c9bca7e
JS
669 /* If there is no backing file on the target, we cannot rely on COW if our
670 * backup cluster size is smaller than the target cluster size. Even for
671 * targets with a backing file, try to avoid COW if possible. */
5c438bc6 672 ret = bdrv_get_info(target, &bdi);
a410a7f1
VSO
673 if (ret == -ENOTSUP && !target->backing) {
674 /* Cluster size is not defined */
3dc6f869
AF
675 warn_report("The target block device doesn't provide "
676 "information about the block size and it doesn't have a "
677 "backing file. The default block size of %u bytes is "
678 "used. If the actual block size of the target exceeds "
679 "this default, the backup may be unusable",
680 BACKUP_CLUSTER_SIZE_DEFAULT);
a410a7f1
VSO
681 job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
682 } else if (ret < 0 && !target->backing) {
4c9bca7e
JS
683 error_setg_errno(errp, -ret,
684 "Couldn't determine the cluster size of the target image, "
685 "which has no backing file");
686 error_append_hint(errp,
687 "Aborting, since this may create an unusable destination image\n");
688 goto error;
689 } else if (ret < 0 && target->backing) {
690 /* Not fatal; just trudge on ahead. */
691 job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
692 } else {
693 job->cluster_size = MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
694 }
9ded4a01
FZ
695 job->use_copy_range = true;
696 job->copy_range_size = MIN_NON_ZERO(blk_get_max_transfer(job->common.blk),
697 blk_get_max_transfer(job->target));
698 job->copy_range_size = MAX(job->cluster_size,
699 QEMU_ALIGN_UP(job->copy_range_size,
700 job->cluster_size));
4c9bca7e 701
4e9e4323 702 /* Required permissions are already taken with target's blk_new() */
76d554e2
KW
703 block_job_add_bdrv(&job->common, "target", target, 0, BLK_PERM_ALL,
704 &error_abort);
05df8a6a 705 job->len = len;
111049a4
JS
706
707 return &job->common;
d58d8453
JS
708
709 error:
710 if (sync_bitmap) {
711 bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
712 }
91ab6883 713 if (job) {
4ad35181
KW
714 backup_clean(&job->common.job);
715 job_early_fail(&job->common.job);
91ab6883 716 }
111049a4
JS
717
718 return NULL;
98d2c6f2 719}