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