]>
Commit | Line | Data |
---|---|---|
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 | ||
14 | #include <stdio.h> | |
15 | #include <errno.h> | |
16 | #include <unistd.h> | |
17 | ||
18 | #include "trace.h" | |
19 | #include "block/block.h" | |
20 | #include "block/block_int.h" | |
21 | #include "block/blockjob.h" | |
cc7a8ea7 | 22 | #include "qapi/qmp/qerror.h" |
98d2c6f2 | 23 | #include "qemu/ratelimit.h" |
373340b2 | 24 | #include "sysemu/block-backend.h" |
98d2c6f2 DM |
25 | |
26 | #define BACKUP_CLUSTER_BITS 16 | |
27 | #define BACKUP_CLUSTER_SIZE (1 << BACKUP_CLUSTER_BITS) | |
28 | #define BACKUP_SECTORS_PER_CLUSTER (BACKUP_CLUSTER_SIZE / BDRV_SECTOR_SIZE) | |
29 | ||
30 | #define SLICE_TIME 100000000ULL /* ns */ | |
31 | ||
32 | typedef struct CowRequest { | |
33 | int64_t start; | |
34 | int64_t end; | |
35 | QLIST_ENTRY(CowRequest) list; | |
36 | CoQueue wait_queue; /* coroutines blocked on this request */ | |
37 | } CowRequest; | |
38 | ||
39 | typedef struct BackupBlockJob { | |
40 | BlockJob common; | |
41 | BlockDriverState *target; | |
4b80ab2b | 42 | /* bitmap for sync=incremental */ |
d58d8453 | 43 | BdrvDirtyBitmap *sync_bitmap; |
fc5d3f84 | 44 | MirrorSyncMode sync_mode; |
98d2c6f2 DM |
45 | RateLimit limit; |
46 | BlockdevOnError on_source_error; | |
47 | BlockdevOnError on_target_error; | |
48 | CoRwlock flush_rwlock; | |
49 | uint64_t sectors_read; | |
50 | HBitmap *bitmap; | |
51 | QLIST_HEAD(, CowRequest) inflight_reqs; | |
52 | } BackupBlockJob; | |
53 | ||
54 | /* See if in-flight requests overlap and wait for them to complete */ | |
55 | static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job, | |
56 | int64_t start, | |
57 | int64_t end) | |
58 | { | |
59 | CowRequest *req; | |
60 | bool retry; | |
61 | ||
62 | do { | |
63 | retry = false; | |
64 | QLIST_FOREACH(req, &job->inflight_reqs, list) { | |
65 | if (end > req->start && start < req->end) { | |
66 | qemu_co_queue_wait(&req->wait_queue); | |
67 | retry = true; | |
68 | break; | |
69 | } | |
70 | } | |
71 | } while (retry); | |
72 | } | |
73 | ||
74 | /* Keep track of an in-flight request */ | |
75 | static void cow_request_begin(CowRequest *req, BackupBlockJob *job, | |
76 | int64_t start, int64_t end) | |
77 | { | |
78 | req->start = start; | |
79 | req->end = end; | |
80 | qemu_co_queue_init(&req->wait_queue); | |
81 | QLIST_INSERT_HEAD(&job->inflight_reqs, req, list); | |
82 | } | |
83 | ||
84 | /* Forget about a completed request */ | |
85 | static void cow_request_end(CowRequest *req) | |
86 | { | |
87 | QLIST_REMOVE(req, list); | |
88 | qemu_co_queue_restart_all(&req->wait_queue); | |
89 | } | |
90 | ||
91 | static int coroutine_fn backup_do_cow(BlockDriverState *bs, | |
92 | int64_t sector_num, int nb_sectors, | |
06c3916b WC |
93 | bool *error_is_read, |
94 | bool is_write_notifier) | |
98d2c6f2 DM |
95 | { |
96 | BackupBlockJob *job = (BackupBlockJob *)bs->job; | |
97 | CowRequest cow_request; | |
98 | struct iovec iov; | |
99 | QEMUIOVector bounce_qiov; | |
100 | void *bounce_buffer = NULL; | |
101 | int ret = 0; | |
102 | int64_t start, end; | |
103 | int n; | |
104 | ||
105 | qemu_co_rwlock_rdlock(&job->flush_rwlock); | |
106 | ||
107 | start = sector_num / BACKUP_SECTORS_PER_CLUSTER; | |
108 | end = DIV_ROUND_UP(sector_num + nb_sectors, BACKUP_SECTORS_PER_CLUSTER); | |
109 | ||
110 | trace_backup_do_cow_enter(job, start, sector_num, nb_sectors); | |
111 | ||
112 | wait_for_overlapping_requests(job, start, end); | |
113 | cow_request_begin(&cow_request, job, start, end); | |
114 | ||
115 | for (; start < end; start++) { | |
116 | if (hbitmap_get(job->bitmap, start)) { | |
117 | trace_backup_do_cow_skip(job, start); | |
118 | continue; /* already copied */ | |
119 | } | |
120 | ||
121 | trace_backup_do_cow_process(job, start); | |
122 | ||
123 | n = MIN(BACKUP_SECTORS_PER_CLUSTER, | |
124 | job->common.len / BDRV_SECTOR_SIZE - | |
125 | start * BACKUP_SECTORS_PER_CLUSTER); | |
126 | ||
127 | if (!bounce_buffer) { | |
128 | bounce_buffer = qemu_blockalign(bs, BACKUP_CLUSTER_SIZE); | |
129 | } | |
130 | iov.iov_base = bounce_buffer; | |
131 | iov.iov_len = n * BDRV_SECTOR_SIZE; | |
132 | qemu_iovec_init_external(&bounce_qiov, &iov, 1); | |
133 | ||
06c3916b WC |
134 | if (is_write_notifier) { |
135 | ret = bdrv_co_no_copy_on_readv(bs, | |
136 | start * BACKUP_SECTORS_PER_CLUSTER, | |
137 | n, &bounce_qiov); | |
138 | } else { | |
139 | ret = bdrv_co_readv(bs, start * BACKUP_SECTORS_PER_CLUSTER, n, | |
140 | &bounce_qiov); | |
141 | } | |
98d2c6f2 DM |
142 | if (ret < 0) { |
143 | trace_backup_do_cow_read_fail(job, start, ret); | |
144 | if (error_is_read) { | |
145 | *error_is_read = true; | |
146 | } | |
147 | goto out; | |
148 | } | |
149 | ||
150 | if (buffer_is_zero(iov.iov_base, iov.iov_len)) { | |
151 | ret = bdrv_co_write_zeroes(job->target, | |
aa7bfbff | 152 | start * BACKUP_SECTORS_PER_CLUSTER, |
d32f35cb | 153 | n, BDRV_REQ_MAY_UNMAP); |
98d2c6f2 DM |
154 | } else { |
155 | ret = bdrv_co_writev(job->target, | |
156 | start * BACKUP_SECTORS_PER_CLUSTER, n, | |
157 | &bounce_qiov); | |
158 | } | |
159 | if (ret < 0) { | |
160 | trace_backup_do_cow_write_fail(job, start, ret); | |
161 | if (error_is_read) { | |
162 | *error_is_read = false; | |
163 | } | |
164 | goto out; | |
165 | } | |
166 | ||
167 | hbitmap_set(job->bitmap, start, 1); | |
168 | ||
169 | /* Publish progress, guest I/O counts as progress too. Note that the | |
170 | * offset field is an opaque progress value, it is not a disk offset. | |
171 | */ | |
172 | job->sectors_read += n; | |
173 | job->common.offset += n * BDRV_SECTOR_SIZE; | |
174 | } | |
175 | ||
176 | out: | |
177 | if (bounce_buffer) { | |
178 | qemu_vfree(bounce_buffer); | |
179 | } | |
180 | ||
181 | cow_request_end(&cow_request); | |
182 | ||
183 | trace_backup_do_cow_return(job, sector_num, nb_sectors, ret); | |
184 | ||
185 | qemu_co_rwlock_unlock(&job->flush_rwlock); | |
186 | ||
187 | return ret; | |
188 | } | |
189 | ||
190 | static int coroutine_fn backup_before_write_notify( | |
191 | NotifierWithReturn *notifier, | |
192 | void *opaque) | |
193 | { | |
194 | BdrvTrackedRequest *req = opaque; | |
793ed47a KW |
195 | int64_t sector_num = req->offset >> BDRV_SECTOR_BITS; |
196 | int nb_sectors = req->bytes >> BDRV_SECTOR_BITS; | |
98d2c6f2 | 197 | |
793ed47a KW |
198 | assert((req->offset & (BDRV_SECTOR_SIZE - 1)) == 0); |
199 | assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) == 0); | |
200 | ||
06c3916b | 201 | return backup_do_cow(req->bs, sector_num, nb_sectors, NULL, true); |
98d2c6f2 DM |
202 | } |
203 | ||
204 | static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp) | |
205 | { | |
206 | BackupBlockJob *s = container_of(job, BackupBlockJob, common); | |
207 | ||
208 | if (speed < 0) { | |
c6bd8c70 | 209 | error_setg(errp, QERR_INVALID_PARAMETER, "speed"); |
98d2c6f2 DM |
210 | return; |
211 | } | |
212 | ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); | |
213 | } | |
214 | ||
215 | static void backup_iostatus_reset(BlockJob *job) | |
216 | { | |
217 | BackupBlockJob *s = container_of(job, BackupBlockJob, common); | |
218 | ||
373340b2 HR |
219 | if (s->target->blk) { |
220 | blk_iostatus_reset(s->target->blk); | |
221 | } | |
98d2c6f2 DM |
222 | } |
223 | ||
b976ea3c FZ |
224 | static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret) |
225 | { | |
226 | BdrvDirtyBitmap *bm; | |
227 | BlockDriverState *bs = job->common.bs; | |
228 | ||
229 | if (ret < 0 || block_job_is_cancelled(&job->common)) { | |
230 | /* Merge the successor back into the parent, delete nothing. */ | |
231 | bm = bdrv_reclaim_dirty_bitmap(bs, job->sync_bitmap, NULL); | |
232 | assert(bm); | |
233 | } else { | |
234 | /* Everything is fine, delete this bitmap and install the backup. */ | |
235 | bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL); | |
236 | assert(bm); | |
237 | } | |
238 | } | |
239 | ||
3fc4b10a | 240 | static const BlockJobDriver backup_job_driver = { |
98d2c6f2 | 241 | .instance_size = sizeof(BackupBlockJob), |
79e14bf7 | 242 | .job_type = BLOCK_JOB_TYPE_BACKUP, |
98d2c6f2 DM |
243 | .set_speed = backup_set_speed, |
244 | .iostatus_reset = backup_iostatus_reset, | |
245 | }; | |
246 | ||
247 | static BlockErrorAction backup_error_action(BackupBlockJob *job, | |
248 | bool read, int error) | |
249 | { | |
250 | if (read) { | |
251 | return block_job_error_action(&job->common, job->common.bs, | |
252 | job->on_source_error, true, error); | |
253 | } else { | |
254 | return block_job_error_action(&job->common, job->target, | |
255 | job->on_target_error, false, error); | |
256 | } | |
257 | } | |
258 | ||
761731b1 SH |
259 | typedef struct { |
260 | int ret; | |
261 | } BackupCompleteData; | |
262 | ||
263 | static void backup_complete(BlockJob *job, void *opaque) | |
264 | { | |
265 | BackupBlockJob *s = container_of(job, BackupBlockJob, common); | |
266 | BackupCompleteData *data = opaque; | |
267 | ||
268 | bdrv_unref(s->target); | |
269 | ||
270 | block_job_completed(job, data->ret); | |
271 | g_free(data); | |
272 | } | |
273 | ||
d58d8453 JS |
274 | static bool coroutine_fn yield_and_check(BackupBlockJob *job) |
275 | { | |
276 | if (block_job_is_cancelled(&job->common)) { | |
277 | return true; | |
278 | } | |
279 | ||
280 | /* we need to yield so that bdrv_drain_all() returns. | |
281 | * (without, VM does not reboot) | |
282 | */ | |
283 | if (job->common.speed) { | |
284 | uint64_t delay_ns = ratelimit_calculate_delay(&job->limit, | |
285 | job->sectors_read); | |
286 | job->sectors_read = 0; | |
287 | block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns); | |
288 | } else { | |
289 | block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0); | |
290 | } | |
291 | ||
292 | if (block_job_is_cancelled(&job->common)) { | |
293 | return true; | |
294 | } | |
295 | ||
296 | return false; | |
297 | } | |
298 | ||
299 | static int coroutine_fn backup_run_incremental(BackupBlockJob *job) | |
300 | { | |
301 | bool error_is_read; | |
302 | int ret = 0; | |
303 | int clusters_per_iter; | |
304 | uint32_t granularity; | |
305 | int64_t sector; | |
306 | int64_t cluster; | |
307 | int64_t end; | |
308 | int64_t last_cluster = -1; | |
309 | BlockDriverState *bs = job->common.bs; | |
310 | HBitmapIter hbi; | |
311 | ||
312 | granularity = bdrv_dirty_bitmap_granularity(job->sync_bitmap); | |
313 | clusters_per_iter = MAX((granularity / BACKUP_CLUSTER_SIZE), 1); | |
20dca810 | 314 | bdrv_dirty_iter_init(job->sync_bitmap, &hbi); |
d58d8453 JS |
315 | |
316 | /* Find the next dirty sector(s) */ | |
317 | while ((sector = hbitmap_iter_next(&hbi)) != -1) { | |
318 | cluster = sector / BACKUP_SECTORS_PER_CLUSTER; | |
319 | ||
320 | /* Fake progress updates for any clusters we skipped */ | |
321 | if (cluster != last_cluster + 1) { | |
322 | job->common.offset += ((cluster - last_cluster - 1) * | |
323 | BACKUP_CLUSTER_SIZE); | |
324 | } | |
325 | ||
326 | for (end = cluster + clusters_per_iter; cluster < end; cluster++) { | |
327 | do { | |
328 | if (yield_and_check(job)) { | |
329 | return ret; | |
330 | } | |
331 | ret = backup_do_cow(bs, cluster * BACKUP_SECTORS_PER_CLUSTER, | |
06c3916b WC |
332 | BACKUP_SECTORS_PER_CLUSTER, &error_is_read, |
333 | false); | |
d58d8453 JS |
334 | if ((ret < 0) && |
335 | backup_error_action(job, error_is_read, -ret) == | |
336 | BLOCK_ERROR_ACTION_REPORT) { | |
337 | return ret; | |
338 | } | |
339 | } while (ret < 0); | |
340 | } | |
341 | ||
342 | /* If the bitmap granularity is smaller than the backup granularity, | |
343 | * we need to advance the iterator pointer to the next cluster. */ | |
344 | if (granularity < BACKUP_CLUSTER_SIZE) { | |
345 | bdrv_set_dirty_iter(&hbi, cluster * BACKUP_SECTORS_PER_CLUSTER); | |
346 | } | |
347 | ||
348 | last_cluster = cluster - 1; | |
349 | } | |
350 | ||
351 | /* Play some final catchup with the progress meter */ | |
352 | end = DIV_ROUND_UP(job->common.len, BACKUP_CLUSTER_SIZE); | |
353 | if (last_cluster + 1 < end) { | |
354 | job->common.offset += ((end - last_cluster - 1) * BACKUP_CLUSTER_SIZE); | |
355 | } | |
356 | ||
357 | return ret; | |
358 | } | |
359 | ||
98d2c6f2 DM |
360 | static void coroutine_fn backup_run(void *opaque) |
361 | { | |
362 | BackupBlockJob *job = opaque; | |
761731b1 | 363 | BackupCompleteData *data; |
98d2c6f2 DM |
364 | BlockDriverState *bs = job->common.bs; |
365 | BlockDriverState *target = job->target; | |
366 | BlockdevOnError on_target_error = job->on_target_error; | |
367 | NotifierWithReturn before_write = { | |
368 | .notify = backup_before_write_notify, | |
369 | }; | |
370 | int64_t start, end; | |
371 | int ret = 0; | |
372 | ||
373 | QLIST_INIT(&job->inflight_reqs); | |
374 | qemu_co_rwlock_init(&job->flush_rwlock); | |
375 | ||
376 | start = 0; | |
d58d8453 | 377 | end = DIV_ROUND_UP(job->common.len, BACKUP_CLUSTER_SIZE); |
98d2c6f2 DM |
378 | |
379 | job->bitmap = hbitmap_alloc(end, 0); | |
380 | ||
381 | bdrv_set_enable_write_cache(target, true); | |
373340b2 HR |
382 | if (target->blk) { |
383 | blk_set_on_error(target->blk, on_target_error, on_target_error); | |
384 | blk_iostatus_enable(target->blk); | |
385 | } | |
98d2c6f2 DM |
386 | |
387 | bdrv_add_before_write_notifier(bs, &before_write); | |
388 | ||
fc5d3f84 IM |
389 | if (job->sync_mode == MIRROR_SYNC_MODE_NONE) { |
390 | while (!block_job_is_cancelled(&job->common)) { | |
391 | /* Yield until the job is cancelled. We just let our before_write | |
392 | * notify callback service CoW requests. */ | |
393 | job->common.busy = false; | |
394 | qemu_coroutine_yield(); | |
395 | job->common.busy = true; | |
98d2c6f2 | 396 | } |
4b80ab2b | 397 | } else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) { |
d58d8453 | 398 | ret = backup_run_incremental(job); |
fc5d3f84 IM |
399 | } else { |
400 | /* Both FULL and TOP SYNC_MODE's require copying.. */ | |
401 | for (; start < end; start++) { | |
402 | bool error_is_read; | |
d58d8453 | 403 | if (yield_and_check(job)) { |
98d2c6f2 | 404 | break; |
fc5d3f84 IM |
405 | } |
406 | ||
407 | if (job->sync_mode == MIRROR_SYNC_MODE_TOP) { | |
408 | int i, n; | |
409 | int alloced = 0; | |
410 | ||
411 | /* Check to see if these blocks are already in the | |
412 | * backing file. */ | |
413 | ||
414 | for (i = 0; i < BACKUP_SECTORS_PER_CLUSTER;) { | |
bdad13b9 | 415 | /* bdrv_is_allocated() only returns true/false based |
4c293dc6 | 416 | * on the first set of sectors it comes across that |
fc5d3f84 IM |
417 | * are are all in the same state. |
418 | * For that reason we must verify each sector in the | |
419 | * backup cluster length. We end up copying more than | |
420 | * needed but at some point that is always the case. */ | |
421 | alloced = | |
bdad13b9 | 422 | bdrv_is_allocated(bs, |
fc5d3f84 IM |
423 | start * BACKUP_SECTORS_PER_CLUSTER + i, |
424 | BACKUP_SECTORS_PER_CLUSTER - i, &n); | |
425 | i += n; | |
426 | ||
d40593dd | 427 | if (alloced == 1 || n == 0) { |
fc5d3f84 IM |
428 | break; |
429 | } | |
430 | } | |
431 | ||
432 | /* If the above loop never found any sectors that are in | |
433 | * the topmost image, skip this backup. */ | |
434 | if (alloced == 0) { | |
435 | continue; | |
436 | } | |
437 | } | |
438 | /* FULL sync mode we copy the whole drive. */ | |
439 | ret = backup_do_cow(bs, start * BACKUP_SECTORS_PER_CLUSTER, | |
06c3916b | 440 | BACKUP_SECTORS_PER_CLUSTER, &error_is_read, false); |
fc5d3f84 IM |
441 | if (ret < 0) { |
442 | /* Depending on error action, fail now or retry cluster */ | |
443 | BlockErrorAction action = | |
444 | backup_error_action(job, error_is_read, -ret); | |
a589569f | 445 | if (action == BLOCK_ERROR_ACTION_REPORT) { |
fc5d3f84 IM |
446 | break; |
447 | } else { | |
448 | start--; | |
449 | continue; | |
450 | } | |
98d2c6f2 DM |
451 | } |
452 | } | |
453 | } | |
454 | ||
455 | notifier_with_return_remove(&before_write); | |
456 | ||
457 | /* wait until pending backup_do_cow() calls have completed */ | |
458 | qemu_co_rwlock_wrlock(&job->flush_rwlock); | |
459 | qemu_co_rwlock_unlock(&job->flush_rwlock); | |
460 | ||
d58d8453 | 461 | if (job->sync_bitmap) { |
b976ea3c | 462 | backup_cleanup_sync_bitmap(job, ret); |
d58d8453 | 463 | } |
98d2c6f2 DM |
464 | hbitmap_free(job->bitmap); |
465 | ||
373340b2 HR |
466 | if (target->blk) { |
467 | blk_iostatus_disable(target->blk); | |
468 | } | |
c29c1dd3 | 469 | bdrv_op_unblock_all(target, job->common.blocker); |
98d2c6f2 | 470 | |
761731b1 SH |
471 | data = g_malloc(sizeof(*data)); |
472 | data->ret = ret; | |
473 | block_job_defer_to_main_loop(&job->common, backup_complete, data); | |
98d2c6f2 DM |
474 | } |
475 | ||
476 | void backup_start(BlockDriverState *bs, BlockDriverState *target, | |
fc5d3f84 | 477 | int64_t speed, MirrorSyncMode sync_mode, |
d58d8453 | 478 | BdrvDirtyBitmap *sync_bitmap, |
98d2c6f2 DM |
479 | BlockdevOnError on_source_error, |
480 | BlockdevOnError on_target_error, | |
097310b5 | 481 | BlockCompletionFunc *cb, void *opaque, |
98d2c6f2 DM |
482 | Error **errp) |
483 | { | |
484 | int64_t len; | |
485 | ||
486 | assert(bs); | |
487 | assert(target); | |
488 | assert(cb); | |
489 | ||
c29c1dd3 FZ |
490 | if (bs == target) { |
491 | error_setg(errp, "Source and target cannot be the same"); | |
492 | return; | |
493 | } | |
494 | ||
98d2c6f2 DM |
495 | if ((on_source_error == BLOCKDEV_ON_ERROR_STOP || |
496 | on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) && | |
373340b2 | 497 | (!bs->blk || !blk_iostatus_is_enabled(bs->blk))) { |
c6bd8c70 | 498 | error_setg(errp, QERR_INVALID_PARAMETER, "on-source-error"); |
98d2c6f2 DM |
499 | return; |
500 | } | |
501 | ||
c29c1dd3 FZ |
502 | if (!bdrv_is_inserted(bs)) { |
503 | error_setg(errp, "Device is not inserted: %s", | |
504 | bdrv_get_device_name(bs)); | |
505 | return; | |
506 | } | |
507 | ||
508 | if (!bdrv_is_inserted(target)) { | |
509 | error_setg(errp, "Device is not inserted: %s", | |
510 | bdrv_get_device_name(target)); | |
511 | return; | |
512 | } | |
513 | ||
514 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { | |
515 | return; | |
516 | } | |
517 | ||
518 | if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) { | |
519 | return; | |
520 | } | |
521 | ||
4b80ab2b | 522 | if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) { |
d58d8453 JS |
523 | if (!sync_bitmap) { |
524 | error_setg(errp, "must provide a valid bitmap name for " | |
4b80ab2b | 525 | "\"incremental\" sync mode"); |
d58d8453 JS |
526 | return; |
527 | } | |
528 | ||
529 | /* Create a new bitmap, and freeze/disable this one. */ | |
530 | if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) { | |
531 | return; | |
532 | } | |
533 | } else if (sync_bitmap) { | |
534 | error_setg(errp, | |
535 | "a sync_bitmap was provided to backup_run, " | |
536 | "but received an incompatible sync_mode (%s)", | |
537 | MirrorSyncMode_lookup[sync_mode]); | |
538 | return; | |
539 | } | |
540 | ||
98d2c6f2 DM |
541 | len = bdrv_getlength(bs); |
542 | if (len < 0) { | |
543 | error_setg_errno(errp, -len, "unable to get length for '%s'", | |
544 | bdrv_get_device_name(bs)); | |
d58d8453 | 545 | goto error; |
98d2c6f2 DM |
546 | } |
547 | ||
3fc4b10a | 548 | BackupBlockJob *job = block_job_create(&backup_job_driver, bs, speed, |
98d2c6f2 DM |
549 | cb, opaque, errp); |
550 | if (!job) { | |
d58d8453 | 551 | goto error; |
98d2c6f2 DM |
552 | } |
553 | ||
c29c1dd3 FZ |
554 | bdrv_op_block_all(target, job->common.blocker); |
555 | ||
98d2c6f2 DM |
556 | job->on_source_error = on_source_error; |
557 | job->on_target_error = on_target_error; | |
558 | job->target = target; | |
fc5d3f84 | 559 | job->sync_mode = sync_mode; |
4b80ab2b | 560 | job->sync_bitmap = sync_mode == MIRROR_SYNC_MODE_INCREMENTAL ? |
d58d8453 | 561 | sync_bitmap : NULL; |
98d2c6f2 DM |
562 | job->common.len = len; |
563 | job->common.co = qemu_coroutine_create(backup_run); | |
564 | qemu_coroutine_enter(job->common.co, job); | |
d58d8453 JS |
565 | return; |
566 | ||
567 | error: | |
568 | if (sync_bitmap) { | |
569 | bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL); | |
570 | } | |
98d2c6f2 | 571 | } |