]> git.proxmox.com Git - mirror_qemu.git/blob - block/backup.c
qapi: add dirty-bitmaps to query-named-block-nodes result
[mirror_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_int.h"
20 #include "block/block_backup.h"
21 #include "qapi/error.h"
22 #include "qapi/qmp/qerror.h"
23 #include "qemu/ratelimit.h"
24 #include "qemu/cutils.h"
25 #include "sysemu/block-backend.h"
26 #include "qemu/bitmap.h"
27 #include "qemu/error-report.h"
28
29 #define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
30
31 typedef 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
38 typedef struct BackupBlockJob {
39 BlockJob common;
40 BlockBackend *target;
41
42 BdrvDirtyBitmap *sync_bitmap;
43 BdrvDirtyBitmap *copy_bitmap;
44
45 MirrorSyncMode sync_mode;
46 BitmapSyncMode bitmap_mode;
47 BlockdevOnError on_source_error;
48 BlockdevOnError on_target_error;
49 CoRwlock flush_rwlock;
50 uint64_t len;
51 uint64_t bytes_read;
52 int64_t cluster_size;
53 bool compress;
54 NotifierWithReturn before_write;
55 QLIST_HEAD(, CowRequest) inflight_reqs;
56
57 bool use_copy_range;
58 int64_t copy_range_size;
59
60 bool serialize_target_writes;
61 bool initializing_bitmap;
62 } BackupBlockJob;
63
64 static const BlockJobDriver backup_job_driver;
65
66 /* See if in-flight requests overlap and wait for them to complete */
67 static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job,
68 int64_t start,
69 int64_t end)
70 {
71 CowRequest *req;
72 bool retry;
73
74 do {
75 retry = false;
76 QLIST_FOREACH(req, &job->inflight_reqs, list) {
77 if (end > req->start_byte && start < req->end_byte) {
78 qemu_co_queue_wait(&req->wait_queue, NULL);
79 retry = true;
80 break;
81 }
82 }
83 } while (retry);
84 }
85
86 /* Keep track of an in-flight request */
87 static void cow_request_begin(CowRequest *req, BackupBlockJob *job,
88 int64_t start, int64_t end)
89 {
90 req->start_byte = start;
91 req->end_byte = end;
92 qemu_co_queue_init(&req->wait_queue);
93 QLIST_INSERT_HEAD(&job->inflight_reqs, req, list);
94 }
95
96 /* Forget about a completed request */
97 static void cow_request_end(CowRequest *req)
98 {
99 QLIST_REMOVE(req, list);
100 qemu_co_queue_restart_all(&req->wait_queue);
101 }
102
103 /* Copy range to target with a bounce buffer and return the bytes copied. If
104 * error occurred, return a negative error number */
105 static int coroutine_fn backup_cow_with_bounce_buffer(BackupBlockJob *job,
106 int64_t start,
107 int64_t end,
108 bool is_write_notifier,
109 bool *error_is_read,
110 void **bounce_buffer)
111 {
112 int ret;
113 BlockBackend *blk = job->common.blk;
114 int nbytes;
115 int read_flags = is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0;
116 int write_flags = job->serialize_target_writes ? BDRV_REQ_SERIALISING : 0;
117
118 assert(QEMU_IS_ALIGNED(start, job->cluster_size));
119 bdrv_reset_dirty_bitmap(job->copy_bitmap, start, job->cluster_size);
120 nbytes = MIN(job->cluster_size, job->len - start);
121 if (!*bounce_buffer) {
122 *bounce_buffer = blk_blockalign(blk, job->cluster_size);
123 }
124
125 ret = blk_co_pread(blk, start, nbytes, *bounce_buffer, read_flags);
126 if (ret < 0) {
127 trace_backup_do_cow_read_fail(job, start, ret);
128 if (error_is_read) {
129 *error_is_read = true;
130 }
131 goto fail;
132 }
133
134 if (buffer_is_zero(*bounce_buffer, nbytes)) {
135 ret = blk_co_pwrite_zeroes(job->target, start,
136 nbytes, write_flags | BDRV_REQ_MAY_UNMAP);
137 } else {
138 ret = blk_co_pwrite(job->target, start,
139 nbytes, *bounce_buffer, write_flags |
140 (job->compress ? BDRV_REQ_WRITE_COMPRESSED : 0));
141 }
142 if (ret < 0) {
143 trace_backup_do_cow_write_fail(job, start, ret);
144 if (error_is_read) {
145 *error_is_read = false;
146 }
147 goto fail;
148 }
149
150 return nbytes;
151 fail:
152 bdrv_set_dirty_bitmap(job->copy_bitmap, start, job->cluster_size);
153 return ret;
154
155 }
156
157 /* Copy range to target and return the bytes copied. If error occurred, return a
158 * negative error number. */
159 static int coroutine_fn backup_cow_with_offload(BackupBlockJob *job,
160 int64_t start,
161 int64_t end,
162 bool is_write_notifier)
163 {
164 int ret;
165 int nr_clusters;
166 BlockBackend *blk = job->common.blk;
167 int nbytes;
168 int read_flags = is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0;
169 int write_flags = job->serialize_target_writes ? BDRV_REQ_SERIALISING : 0;
170
171 assert(QEMU_IS_ALIGNED(job->copy_range_size, job->cluster_size));
172 assert(QEMU_IS_ALIGNED(start, job->cluster_size));
173 nbytes = MIN(job->copy_range_size, end - start);
174 nr_clusters = DIV_ROUND_UP(nbytes, job->cluster_size);
175 bdrv_reset_dirty_bitmap(job->copy_bitmap, start,
176 job->cluster_size * nr_clusters);
177 ret = blk_co_copy_range(blk, start, job->target, start, nbytes,
178 read_flags, write_flags);
179 if (ret < 0) {
180 trace_backup_do_cow_copy_range_fail(job, start, ret);
181 bdrv_set_dirty_bitmap(job->copy_bitmap, start,
182 job->cluster_size * nr_clusters);
183 return ret;
184 }
185
186 return nbytes;
187 }
188
189 /*
190 * Check if the cluster starting at offset is allocated or not.
191 * return via pnum the number of contiguous clusters sharing this allocation.
192 */
193 static int backup_is_cluster_allocated(BackupBlockJob *s, int64_t offset,
194 int64_t *pnum)
195 {
196 BlockDriverState *bs = blk_bs(s->common.blk);
197 int64_t count, total_count = 0;
198 int64_t bytes = s->len - offset;
199 int ret;
200
201 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
202
203 while (true) {
204 ret = bdrv_is_allocated(bs, offset, bytes, &count);
205 if (ret < 0) {
206 return ret;
207 }
208
209 total_count += count;
210
211 if (ret || count == 0) {
212 /*
213 * ret: partial segment(s) are considered allocated.
214 * otherwise: unallocated tail is treated as an entire segment.
215 */
216 *pnum = DIV_ROUND_UP(total_count, s->cluster_size);
217 return ret;
218 }
219
220 /* Unallocated segment(s) with uncertain following segment(s) */
221 if (total_count >= s->cluster_size) {
222 *pnum = total_count / s->cluster_size;
223 return 0;
224 }
225
226 offset += count;
227 bytes -= count;
228 }
229 }
230
231 /**
232 * Reset bits in copy_bitmap starting at offset if they represent unallocated
233 * data in the image. May reset subsequent contiguous bits.
234 * @return 0 when the cluster at @offset was unallocated,
235 * 1 otherwise, and -ret on error.
236 */
237 static int64_t backup_bitmap_reset_unallocated(BackupBlockJob *s,
238 int64_t offset, int64_t *count)
239 {
240 int ret;
241 int64_t clusters, bytes, estimate;
242
243 ret = backup_is_cluster_allocated(s, offset, &clusters);
244 if (ret < 0) {
245 return ret;
246 }
247
248 bytes = clusters * s->cluster_size;
249
250 if (!ret) {
251 bdrv_reset_dirty_bitmap(s->copy_bitmap, offset, bytes);
252 estimate = bdrv_get_dirty_count(s->copy_bitmap);
253 job_progress_set_remaining(&s->common.job, estimate);
254 }
255
256 *count = bytes;
257 return ret;
258 }
259
260 static int coroutine_fn backup_do_cow(BackupBlockJob *job,
261 int64_t offset, uint64_t bytes,
262 bool *error_is_read,
263 bool is_write_notifier)
264 {
265 CowRequest cow_request;
266 int ret = 0;
267 int64_t start, end; /* bytes */
268 void *bounce_buffer = NULL;
269 int64_t status_bytes;
270
271 qemu_co_rwlock_rdlock(&job->flush_rwlock);
272
273 start = QEMU_ALIGN_DOWN(offset, job->cluster_size);
274 end = QEMU_ALIGN_UP(bytes + offset, job->cluster_size);
275
276 trace_backup_do_cow_enter(job, start, offset, bytes);
277
278 wait_for_overlapping_requests(job, start, end);
279 cow_request_begin(&cow_request, job, start, end);
280
281 while (start < end) {
282 int64_t dirty_end;
283
284 if (!bdrv_dirty_bitmap_get(job->copy_bitmap, start)) {
285 trace_backup_do_cow_skip(job, start);
286 start += job->cluster_size;
287 continue; /* already copied */
288 }
289
290 dirty_end = bdrv_dirty_bitmap_next_zero(job->copy_bitmap, start,
291 (end - start));
292 if (dirty_end < 0) {
293 dirty_end = end;
294 }
295
296 if (job->initializing_bitmap) {
297 ret = backup_bitmap_reset_unallocated(job, start, &status_bytes);
298 if (ret == 0) {
299 trace_backup_do_cow_skip_range(job, start, status_bytes);
300 start += status_bytes;
301 continue;
302 }
303 /* Clamp to known allocated region */
304 dirty_end = MIN(dirty_end, start + status_bytes);
305 }
306
307 trace_backup_do_cow_process(job, start);
308
309 if (job->use_copy_range) {
310 ret = backup_cow_with_offload(job, start, dirty_end,
311 is_write_notifier);
312 if (ret < 0) {
313 job->use_copy_range = false;
314 }
315 }
316 if (!job->use_copy_range) {
317 ret = backup_cow_with_bounce_buffer(job, start, dirty_end,
318 is_write_notifier,
319 error_is_read, &bounce_buffer);
320 }
321 if (ret < 0) {
322 break;
323 }
324
325 /* Publish progress, guest I/O counts as progress too. Note that the
326 * offset field is an opaque progress value, it is not a disk offset.
327 */
328 start += ret;
329 job->bytes_read += ret;
330 job_progress_update(&job->common.job, ret);
331 ret = 0;
332 }
333
334 if (bounce_buffer) {
335 qemu_vfree(bounce_buffer);
336 }
337
338 cow_request_end(&cow_request);
339
340 trace_backup_do_cow_return(job, offset, bytes, ret);
341
342 qemu_co_rwlock_unlock(&job->flush_rwlock);
343
344 return ret;
345 }
346
347 static int coroutine_fn backup_before_write_notify(
348 NotifierWithReturn *notifier,
349 void *opaque)
350 {
351 BackupBlockJob *job = container_of(notifier, BackupBlockJob, before_write);
352 BdrvTrackedRequest *req = opaque;
353
354 assert(req->bs == blk_bs(job->common.blk));
355 assert(QEMU_IS_ALIGNED(req->offset, BDRV_SECTOR_SIZE));
356 assert(QEMU_IS_ALIGNED(req->bytes, BDRV_SECTOR_SIZE));
357
358 return backup_do_cow(job, req->offset, req->bytes, NULL, true);
359 }
360
361 static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
362 {
363 BdrvDirtyBitmap *bm;
364 BlockDriverState *bs = blk_bs(job->common.blk);
365 bool sync = (((ret == 0) || (job->bitmap_mode == BITMAP_SYNC_MODE_ALWAYS)) \
366 && (job->bitmap_mode != BITMAP_SYNC_MODE_NEVER));
367
368 if (sync) {
369 /*
370 * We succeeded, or we always intended to sync the bitmap.
371 * Delete this bitmap and install the child.
372 */
373 bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL);
374 } else {
375 /*
376 * We failed, or we never intended to sync the bitmap anyway.
377 * Merge the successor back into the parent, keeping all data.
378 */
379 bm = bdrv_reclaim_dirty_bitmap(bs, job->sync_bitmap, NULL);
380 }
381
382 assert(bm);
383
384 if (ret < 0 && job->bitmap_mode == BITMAP_SYNC_MODE_ALWAYS) {
385 /* If we failed and synced, merge in the bits we didn't copy: */
386 bdrv_dirty_bitmap_merge_internal(bm, job->copy_bitmap,
387 NULL, true);
388 }
389 }
390
391 static void backup_commit(Job *job)
392 {
393 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
394 if (s->sync_bitmap) {
395 backup_cleanup_sync_bitmap(s, 0);
396 }
397 }
398
399 static void backup_abort(Job *job)
400 {
401 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
402 if (s->sync_bitmap) {
403 backup_cleanup_sync_bitmap(s, -1);
404 }
405 }
406
407 static void backup_clean(Job *job)
408 {
409 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
410 BlockDriverState *bs = blk_bs(s->common.blk);
411
412 if (s->copy_bitmap) {
413 bdrv_release_dirty_bitmap(bs, s->copy_bitmap);
414 s->copy_bitmap = NULL;
415 }
416
417 assert(s->target);
418 blk_unref(s->target);
419 s->target = NULL;
420 }
421
422 void backup_do_checkpoint(BlockJob *job, Error **errp)
423 {
424 BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
425
426 assert(block_job_driver(job) == &backup_job_driver);
427
428 if (backup_job->sync_mode != MIRROR_SYNC_MODE_NONE) {
429 error_setg(errp, "The backup job only supports block checkpoint in"
430 " sync=none mode");
431 return;
432 }
433
434 bdrv_set_dirty_bitmap(backup_job->copy_bitmap, 0, backup_job->len);
435 }
436
437 static void backup_drain(BlockJob *job)
438 {
439 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
440
441 /* Need to keep a reference in case blk_drain triggers execution
442 * of backup_complete...
443 */
444 if (s->target) {
445 BlockBackend *target = s->target;
446 blk_ref(target);
447 blk_drain(target);
448 blk_unref(target);
449 }
450 }
451
452 static BlockErrorAction backup_error_action(BackupBlockJob *job,
453 bool read, int error)
454 {
455 if (read) {
456 return block_job_error_action(&job->common, job->on_source_error,
457 true, error);
458 } else {
459 return block_job_error_action(&job->common, job->on_target_error,
460 false, error);
461 }
462 }
463
464 static bool coroutine_fn yield_and_check(BackupBlockJob *job)
465 {
466 uint64_t delay_ns;
467
468 if (job_is_cancelled(&job->common.job)) {
469 return true;
470 }
471
472 /* We need to yield even for delay_ns = 0 so that bdrv_drain_all() can
473 * return. Without a yield, the VM would not reboot. */
474 delay_ns = block_job_ratelimit_get_delay(&job->common, job->bytes_read);
475 job->bytes_read = 0;
476 job_sleep_ns(&job->common.job, delay_ns);
477
478 if (job_is_cancelled(&job->common.job)) {
479 return true;
480 }
481
482 return false;
483 }
484
485 static int coroutine_fn backup_loop(BackupBlockJob *job)
486 {
487 bool error_is_read;
488 int64_t offset;
489 BdrvDirtyBitmapIter *bdbi;
490 int ret = 0;
491
492 bdbi = bdrv_dirty_iter_new(job->copy_bitmap);
493 while ((offset = bdrv_dirty_iter_next(bdbi)) != -1) {
494 do {
495 if (yield_and_check(job)) {
496 goto out;
497 }
498 ret = backup_do_cow(job, offset,
499 job->cluster_size, &error_is_read, false);
500 if (ret < 0 && backup_error_action(job, error_is_read, -ret) ==
501 BLOCK_ERROR_ACTION_REPORT)
502 {
503 goto out;
504 }
505 } while (ret < 0);
506 }
507
508 out:
509 bdrv_dirty_iter_free(bdbi);
510 return ret;
511 }
512
513 static void backup_init_copy_bitmap(BackupBlockJob *job)
514 {
515 bool ret;
516 uint64_t estimate;
517
518 if (job->sync_mode == MIRROR_SYNC_MODE_BITMAP) {
519 ret = bdrv_dirty_bitmap_merge_internal(job->copy_bitmap,
520 job->sync_bitmap,
521 NULL, true);
522 assert(ret);
523 } else {
524 if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
525 /*
526 * We can't hog the coroutine to initialize this thoroughly.
527 * Set a flag and resume work when we are able to yield safely.
528 */
529 job->initializing_bitmap = true;
530 }
531 bdrv_set_dirty_bitmap(job->copy_bitmap, 0, job->len);
532 }
533
534 estimate = bdrv_get_dirty_count(job->copy_bitmap);
535 job_progress_set_remaining(&job->common.job, estimate);
536 }
537
538 static int coroutine_fn backup_run(Job *job, Error **errp)
539 {
540 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
541 BlockDriverState *bs = blk_bs(s->common.blk);
542 int ret = 0;
543
544 QLIST_INIT(&s->inflight_reqs);
545 qemu_co_rwlock_init(&s->flush_rwlock);
546
547 backup_init_copy_bitmap(s);
548
549 s->before_write.notify = backup_before_write_notify;
550 bdrv_add_before_write_notifier(bs, &s->before_write);
551
552 if (s->sync_mode == MIRROR_SYNC_MODE_TOP) {
553 int64_t offset = 0;
554 int64_t count;
555
556 for (offset = 0; offset < s->len; ) {
557 if (yield_and_check(s)) {
558 ret = -ECANCELED;
559 goto out;
560 }
561
562 ret = backup_bitmap_reset_unallocated(s, offset, &count);
563 if (ret < 0) {
564 goto out;
565 }
566
567 offset += count;
568 }
569 s->initializing_bitmap = false;
570 }
571
572 if (s->sync_mode == MIRROR_SYNC_MODE_NONE) {
573 /* All bits are set in copy_bitmap to allow any cluster to be copied.
574 * This does not actually require them to be copied. */
575 while (!job_is_cancelled(job)) {
576 /* Yield until the job is cancelled. We just let our before_write
577 * notify callback service CoW requests. */
578 job_yield(job);
579 }
580 } else {
581 ret = backup_loop(s);
582 }
583
584 out:
585 notifier_with_return_remove(&s->before_write);
586
587 /* wait until pending backup_do_cow() calls have completed */
588 qemu_co_rwlock_wrlock(&s->flush_rwlock);
589 qemu_co_rwlock_unlock(&s->flush_rwlock);
590
591 return ret;
592 }
593
594 static const BlockJobDriver backup_job_driver = {
595 .job_driver = {
596 .instance_size = sizeof(BackupBlockJob),
597 .job_type = JOB_TYPE_BACKUP,
598 .free = block_job_free,
599 .user_resume = block_job_user_resume,
600 .drain = block_job_drain,
601 .run = backup_run,
602 .commit = backup_commit,
603 .abort = backup_abort,
604 .clean = backup_clean,
605 },
606 .drain = backup_drain,
607 };
608
609 static int64_t backup_calculate_cluster_size(BlockDriverState *target,
610 Error **errp)
611 {
612 int ret;
613 BlockDriverInfo bdi;
614
615 /*
616 * If there is no backing file on the target, we cannot rely on COW if our
617 * backup cluster size is smaller than the target cluster size. Even for
618 * targets with a backing file, try to avoid COW if possible.
619 */
620 ret = bdrv_get_info(target, &bdi);
621 if (ret == -ENOTSUP && !target->backing) {
622 /* Cluster size is not defined */
623 warn_report("The target block device doesn't provide "
624 "information about the block size and it doesn't have a "
625 "backing file. The default block size of %u bytes is "
626 "used. If the actual block size of the target exceeds "
627 "this default, the backup may be unusable",
628 BACKUP_CLUSTER_SIZE_DEFAULT);
629 return BACKUP_CLUSTER_SIZE_DEFAULT;
630 } else if (ret < 0 && !target->backing) {
631 error_setg_errno(errp, -ret,
632 "Couldn't determine the cluster size of the target image, "
633 "which has no backing file");
634 error_append_hint(errp,
635 "Aborting, since this may create an unusable destination image\n");
636 return ret;
637 } else if (ret < 0 && target->backing) {
638 /* Not fatal; just trudge on ahead. */
639 return BACKUP_CLUSTER_SIZE_DEFAULT;
640 }
641
642 return MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
643 }
644
645 BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
646 BlockDriverState *target, int64_t speed,
647 MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap,
648 BitmapSyncMode bitmap_mode,
649 bool compress,
650 BlockdevOnError on_source_error,
651 BlockdevOnError on_target_error,
652 int creation_flags,
653 BlockCompletionFunc *cb, void *opaque,
654 JobTxn *txn, Error **errp)
655 {
656 int64_t len;
657 BackupBlockJob *job = NULL;
658 int ret;
659 int64_t cluster_size;
660 BdrvDirtyBitmap *copy_bitmap = NULL;
661
662 assert(bs);
663 assert(target);
664
665 /* QMP interface protects us from these cases */
666 assert(sync_mode != MIRROR_SYNC_MODE_INCREMENTAL);
667 assert(sync_bitmap || sync_mode != MIRROR_SYNC_MODE_BITMAP);
668
669 if (bs == target) {
670 error_setg(errp, "Source and target cannot be the same");
671 return NULL;
672 }
673
674 if (!bdrv_is_inserted(bs)) {
675 error_setg(errp, "Device is not inserted: %s",
676 bdrv_get_device_name(bs));
677 return NULL;
678 }
679
680 if (!bdrv_is_inserted(target)) {
681 error_setg(errp, "Device is not inserted: %s",
682 bdrv_get_device_name(target));
683 return NULL;
684 }
685
686 if (compress && target->drv->bdrv_co_pwritev_compressed == NULL) {
687 error_setg(errp, "Compression is not supported for this drive %s",
688 bdrv_get_device_name(target));
689 return NULL;
690 }
691
692 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
693 return NULL;
694 }
695
696 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
697 return NULL;
698 }
699
700 if (sync_bitmap) {
701 /* If we need to write to this bitmap, check that we can: */
702 if (bitmap_mode != BITMAP_SYNC_MODE_NEVER &&
703 bdrv_dirty_bitmap_check(sync_bitmap, BDRV_BITMAP_DEFAULT, errp)) {
704 return NULL;
705 }
706
707 /* Create a new bitmap, and freeze/disable this one. */
708 if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) {
709 return NULL;
710 }
711 }
712
713 len = bdrv_getlength(bs);
714 if (len < 0) {
715 error_setg_errno(errp, -len, "unable to get length for '%s'",
716 bdrv_get_device_name(bs));
717 goto error;
718 }
719
720 cluster_size = backup_calculate_cluster_size(target, errp);
721 if (cluster_size < 0) {
722 goto error;
723 }
724
725 copy_bitmap = bdrv_create_dirty_bitmap(bs, cluster_size, NULL, errp);
726 if (!copy_bitmap) {
727 goto error;
728 }
729 bdrv_disable_dirty_bitmap(copy_bitmap);
730
731 /* job->len is fixed, so we can't allow resize */
732 job = block_job_create(job_id, &backup_job_driver, txn, bs,
733 BLK_PERM_CONSISTENT_READ,
734 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE |
735 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_GRAPH_MOD,
736 speed, creation_flags, cb, opaque, errp);
737 if (!job) {
738 goto error;
739 }
740
741 /* The target must match the source in size, so no resize here either */
742 job->target = blk_new(job->common.job.aio_context,
743 BLK_PERM_WRITE,
744 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE |
745 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_GRAPH_MOD);
746 ret = blk_insert_bs(job->target, target, errp);
747 if (ret < 0) {
748 goto error;
749 }
750 blk_set_disable_request_queuing(job->target, true);
751
752 job->on_source_error = on_source_error;
753 job->on_target_error = on_target_error;
754 job->sync_mode = sync_mode;
755 job->sync_bitmap = sync_bitmap;
756 job->bitmap_mode = bitmap_mode;
757 job->compress = compress;
758
759 /* Detect image-fleecing (and similar) schemes */
760 job->serialize_target_writes = bdrv_chain_contains(target, bs);
761 job->cluster_size = cluster_size;
762 job->copy_bitmap = copy_bitmap;
763 copy_bitmap = NULL;
764 job->use_copy_range = !compress; /* compression isn't supported for it */
765 job->copy_range_size = MIN_NON_ZERO(blk_get_max_transfer(job->common.blk),
766 blk_get_max_transfer(job->target));
767 job->copy_range_size = MAX(job->cluster_size,
768 QEMU_ALIGN_UP(job->copy_range_size,
769 job->cluster_size));
770
771 /* Required permissions are already taken with target's blk_new() */
772 block_job_add_bdrv(&job->common, "target", target, 0, BLK_PERM_ALL,
773 &error_abort);
774 job->len = len;
775
776 return &job->common;
777
778 error:
779 if (copy_bitmap) {
780 assert(!job || !job->copy_bitmap);
781 bdrv_release_dirty_bitmap(bs, copy_bitmap);
782 }
783 if (sync_bitmap) {
784 bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
785 }
786 if (job) {
787 backup_clean(&job->common.job);
788 job_early_fail(&job->common.job);
789 }
790
791 return NULL;
792 }