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