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