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