]>
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 | ||
80c71a24 | 14 | #include "qemu/osdep.h" |
98d2c6f2 DM |
15 | |
16 | #include "trace.h" | |
17 | #include "block/block.h" | |
18 | #include "block/block_int.h" | |
19 | #include "block/blockjob.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" |
98d2c6f2 | 27 | |
16096a4d | 28 | #define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16) |
98d2c6f2 DM |
29 | #define SLICE_TIME 100000000ULL /* ns */ |
30 | ||
98d2c6f2 DM |
31 | typedef 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 | RateLimit limit; |
38 | BlockdevOnError on_source_error; | |
39 | BlockdevOnError on_target_error; | |
40 | CoRwlock flush_rwlock; | |
41 | uint64_t sectors_read; | |
b2f56462 | 42 | unsigned long *done_bitmap; |
16096a4d | 43 | int64_t cluster_size; |
13b9414b | 44 | bool compress; |
12b3e52e | 45 | NotifierWithReturn before_write; |
98d2c6f2 DM |
46 | QLIST_HEAD(, CowRequest) inflight_reqs; |
47 | } BackupBlockJob; | |
48 | ||
16096a4d JS |
49 | /* Size of a cluster in sectors, instead of bytes. */ |
50 | static inline int64_t cluster_size_sectors(BackupBlockJob *job) | |
51 | { | |
52 | return job->cluster_size / BDRV_SECTOR_SIZE; | |
53 | } | |
54 | ||
98d2c6f2 DM |
55 | /* See if in-flight requests overlap and wait for them to complete */ |
56 | static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job, | |
57 | int64_t start, | |
58 | int64_t end) | |
59 | { | |
60 | CowRequest *req; | |
61 | bool retry; | |
62 | ||
63 | do { | |
64 | retry = false; | |
65 | QLIST_FOREACH(req, &job->inflight_reqs, list) { | |
66 | if (end > req->start && start < req->end) { | |
67 | qemu_co_queue_wait(&req->wait_queue); | |
68 | retry = true; | |
69 | break; | |
70 | } | |
71 | } | |
72 | } while (retry); | |
73 | } | |
74 | ||
75 | /* Keep track of an in-flight request */ | |
76 | static void cow_request_begin(CowRequest *req, BackupBlockJob *job, | |
77 | int64_t start, int64_t end) | |
78 | { | |
79 | req->start = start; | |
80 | req->end = end; | |
81 | qemu_co_queue_init(&req->wait_queue); | |
82 | QLIST_INSERT_HEAD(&job->inflight_reqs, req, list); | |
83 | } | |
84 | ||
85 | /* Forget about a completed request */ | |
86 | static void cow_request_end(CowRequest *req) | |
87 | { | |
88 | QLIST_REMOVE(req, list); | |
89 | qemu_co_queue_restart_all(&req->wait_queue); | |
90 | } | |
91 | ||
8543c274 | 92 | static int coroutine_fn backup_do_cow(BackupBlockJob *job, |
98d2c6f2 | 93 | int64_t sector_num, int nb_sectors, |
06c3916b WC |
94 | bool *error_is_read, |
95 | bool is_write_notifier) | |
98d2c6f2 | 96 | { |
5c438bc6 | 97 | BlockBackend *blk = job->common.blk; |
98d2c6f2 DM |
98 | CowRequest cow_request; |
99 | struct iovec iov; | |
100 | QEMUIOVector bounce_qiov; | |
101 | void *bounce_buffer = NULL; | |
102 | int ret = 0; | |
16096a4d | 103 | int64_t sectors_per_cluster = cluster_size_sectors(job); |
98d2c6f2 DM |
104 | int64_t start, end; |
105 | int n; | |
106 | ||
107 | qemu_co_rwlock_rdlock(&job->flush_rwlock); | |
108 | ||
16096a4d JS |
109 | start = sector_num / sectors_per_cluster; |
110 | end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster); | |
98d2c6f2 DM |
111 | |
112 | trace_backup_do_cow_enter(job, start, sector_num, nb_sectors); | |
113 | ||
114 | wait_for_overlapping_requests(job, start, end); | |
115 | cow_request_begin(&cow_request, job, start, end); | |
116 | ||
117 | for (; start < end; start++) { | |
b2f56462 | 118 | if (test_bit(start, job->done_bitmap)) { |
98d2c6f2 DM |
119 | trace_backup_do_cow_skip(job, start); |
120 | continue; /* already copied */ | |
121 | } | |
122 | ||
123 | trace_backup_do_cow_process(job, start); | |
124 | ||
16096a4d | 125 | n = MIN(sectors_per_cluster, |
98d2c6f2 | 126 | job->common.len / BDRV_SECTOR_SIZE - |
16096a4d | 127 | start * sectors_per_cluster); |
98d2c6f2 DM |
128 | |
129 | if (!bounce_buffer) { | |
5c438bc6 | 130 | bounce_buffer = blk_blockalign(blk, job->cluster_size); |
98d2c6f2 DM |
131 | } |
132 | iov.iov_base = bounce_buffer; | |
133 | iov.iov_len = n * BDRV_SECTOR_SIZE; | |
134 | qemu_iovec_init_external(&bounce_qiov, &iov, 1); | |
135 | ||
5c438bc6 KW |
136 | ret = blk_co_preadv(blk, start * job->cluster_size, |
137 | bounce_qiov.size, &bounce_qiov, | |
138 | is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0); | |
98d2c6f2 DM |
139 | if (ret < 0) { |
140 | trace_backup_do_cow_read_fail(job, start, ret); | |
141 | if (error_is_read) { | |
142 | *error_is_read = true; | |
143 | } | |
144 | goto out; | |
145 | } | |
146 | ||
147 | if (buffer_is_zero(iov.iov_base, iov.iov_len)) { | |
5c438bc6 KW |
148 | ret = blk_co_pwrite_zeroes(job->target, start * job->cluster_size, |
149 | bounce_qiov.size, BDRV_REQ_MAY_UNMAP); | |
98d2c6f2 | 150 | } else { |
5c438bc6 | 151 | ret = blk_co_pwritev(job->target, start * job->cluster_size, |
13b9414b PB |
152 | bounce_qiov.size, &bounce_qiov, |
153 | job->compress ? BDRV_REQ_WRITE_COMPRESSED : 0); | |
98d2c6f2 DM |
154 | } |
155 | if (ret < 0) { | |
156 | trace_backup_do_cow_write_fail(job, start, ret); | |
157 | if (error_is_read) { | |
158 | *error_is_read = false; | |
159 | } | |
160 | goto out; | |
161 | } | |
162 | ||
b2f56462 | 163 | set_bit(start, job->done_bitmap); |
98d2c6f2 DM |
164 | |
165 | /* Publish progress, guest I/O counts as progress too. Note that the | |
166 | * offset field is an opaque progress value, it is not a disk offset. | |
167 | */ | |
168 | job->sectors_read += n; | |
169 | job->common.offset += n * BDRV_SECTOR_SIZE; | |
170 | } | |
171 | ||
172 | out: | |
173 | if (bounce_buffer) { | |
174 | qemu_vfree(bounce_buffer); | |
175 | } | |
176 | ||
177 | cow_request_end(&cow_request); | |
178 | ||
179 | trace_backup_do_cow_return(job, sector_num, nb_sectors, ret); | |
180 | ||
181 | qemu_co_rwlock_unlock(&job->flush_rwlock); | |
182 | ||
183 | return ret; | |
184 | } | |
185 | ||
186 | static int coroutine_fn backup_before_write_notify( | |
187 | NotifierWithReturn *notifier, | |
188 | void *opaque) | |
189 | { | |
12b3e52e | 190 | BackupBlockJob *job = container_of(notifier, BackupBlockJob, before_write); |
98d2c6f2 | 191 | BdrvTrackedRequest *req = opaque; |
793ed47a KW |
192 | int64_t sector_num = req->offset >> BDRV_SECTOR_BITS; |
193 | int nb_sectors = req->bytes >> BDRV_SECTOR_BITS; | |
98d2c6f2 | 194 | |
5c438bc6 | 195 | assert(req->bs == blk_bs(job->common.blk)); |
793ed47a KW |
196 | assert((req->offset & (BDRV_SECTOR_SIZE - 1)) == 0); |
197 | assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) == 0); | |
198 | ||
8543c274 | 199 | return backup_do_cow(job, sector_num, nb_sectors, NULL, true); |
98d2c6f2 DM |
200 | } |
201 | ||
202 | static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp) | |
203 | { | |
204 | BackupBlockJob *s = container_of(job, BackupBlockJob, common); | |
205 | ||
206 | if (speed < 0) { | |
c6bd8c70 | 207 | error_setg(errp, QERR_INVALID_PARAMETER, "speed"); |
98d2c6f2 DM |
208 | return; |
209 | } | |
210 | ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); | |
211 | } | |
212 | ||
b976ea3c FZ |
213 | static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret) |
214 | { | |
215 | BdrvDirtyBitmap *bm; | |
5c438bc6 | 216 | BlockDriverState *bs = blk_bs(job->common.blk); |
b976ea3c FZ |
217 | |
218 | if (ret < 0 || block_job_is_cancelled(&job->common)) { | |
219 | /* Merge the successor back into the parent, delete nothing. */ | |
220 | bm = bdrv_reclaim_dirty_bitmap(bs, job->sync_bitmap, NULL); | |
221 | assert(bm); | |
222 | } else { | |
223 | /* Everything is fine, delete this bitmap and install the backup. */ | |
224 | bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL); | |
225 | assert(bm); | |
226 | } | |
227 | } | |
228 | ||
c347b2c6 JS |
229 | static void backup_commit(BlockJob *job) |
230 | { | |
231 | BackupBlockJob *s = container_of(job, BackupBlockJob, common); | |
232 | if (s->sync_bitmap) { | |
233 | backup_cleanup_sync_bitmap(s, 0); | |
234 | } | |
235 | } | |
236 | ||
237 | static void backup_abort(BlockJob *job) | |
238 | { | |
239 | BackupBlockJob *s = container_of(job, BackupBlockJob, common); | |
240 | if (s->sync_bitmap) { | |
241 | backup_cleanup_sync_bitmap(s, -1); | |
242 | } | |
243 | } | |
244 | ||
5ab4b69c SH |
245 | static void backup_attached_aio_context(BlockJob *job, AioContext *aio_context) |
246 | { | |
247 | BackupBlockJob *s = container_of(job, BackupBlockJob, common); | |
248 | ||
249 | blk_set_aio_context(s->target, aio_context); | |
250 | } | |
251 | ||
49d3e828 WC |
252 | void backup_do_checkpoint(BlockJob *job, Error **errp) |
253 | { | |
254 | BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common); | |
255 | int64_t len; | |
256 | ||
257 | assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP); | |
258 | ||
259 | if (backup_job->sync_mode != MIRROR_SYNC_MODE_NONE) { | |
260 | error_setg(errp, "The backup job only supports block checkpoint in" | |
261 | " sync=none mode"); | |
262 | return; | |
263 | } | |
264 | ||
265 | len = DIV_ROUND_UP(backup_job->common.len, backup_job->cluster_size); | |
266 | bitmap_zero(backup_job->done_bitmap, len); | |
267 | } | |
268 | ||
a8bbee0e CX |
269 | void backup_wait_for_overlapping_requests(BlockJob *job, int64_t sector_num, |
270 | int nb_sectors) | |
271 | { | |
272 | BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common); | |
273 | int64_t sectors_per_cluster = cluster_size_sectors(backup_job); | |
274 | int64_t start, end; | |
275 | ||
276 | assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP); | |
277 | ||
278 | start = sector_num / sectors_per_cluster; | |
279 | end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster); | |
280 | wait_for_overlapping_requests(backup_job, start, end); | |
281 | } | |
282 | ||
283 | void backup_cow_request_begin(CowRequest *req, BlockJob *job, | |
284 | int64_t sector_num, | |
285 | int nb_sectors) | |
286 | { | |
287 | BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common); | |
288 | int64_t sectors_per_cluster = cluster_size_sectors(backup_job); | |
289 | int64_t start, end; | |
290 | ||
291 | assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP); | |
292 | ||
293 | start = sector_num / sectors_per_cluster; | |
294 | end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster); | |
295 | cow_request_begin(req, backup_job, start, end); | |
296 | } | |
297 | ||
298 | void backup_cow_request_end(CowRequest *req) | |
299 | { | |
300 | cow_request_end(req); | |
301 | } | |
302 | ||
3fc4b10a | 303 | static const BlockJobDriver backup_job_driver = { |
5ab4b69c SH |
304 | .instance_size = sizeof(BackupBlockJob), |
305 | .job_type = BLOCK_JOB_TYPE_BACKUP, | |
306 | .set_speed = backup_set_speed, | |
307 | .commit = backup_commit, | |
308 | .abort = backup_abort, | |
309 | .attached_aio_context = backup_attached_aio_context, | |
98d2c6f2 DM |
310 | }; |
311 | ||
312 | static BlockErrorAction backup_error_action(BackupBlockJob *job, | |
313 | bool read, int error) | |
314 | { | |
315 | if (read) { | |
81e254dc KW |
316 | return block_job_error_action(&job->common, job->on_source_error, |
317 | true, error); | |
98d2c6f2 | 318 | } else { |
81e254dc KW |
319 | return block_job_error_action(&job->common, job->on_target_error, |
320 | false, error); | |
98d2c6f2 DM |
321 | } |
322 | } | |
323 | ||
761731b1 SH |
324 | typedef struct { |
325 | int ret; | |
326 | } BackupCompleteData; | |
327 | ||
328 | static void backup_complete(BlockJob *job, void *opaque) | |
329 | { | |
330 | BackupBlockJob *s = container_of(job, BackupBlockJob, common); | |
331 | BackupCompleteData *data = opaque; | |
332 | ||
5c438bc6 | 333 | blk_unref(s->target); |
761731b1 SH |
334 | |
335 | block_job_completed(job, data->ret); | |
336 | g_free(data); | |
337 | } | |
338 | ||
d58d8453 JS |
339 | static bool coroutine_fn yield_and_check(BackupBlockJob *job) |
340 | { | |
341 | if (block_job_is_cancelled(&job->common)) { | |
342 | return true; | |
343 | } | |
344 | ||
345 | /* we need to yield so that bdrv_drain_all() returns. | |
346 | * (without, VM does not reboot) | |
347 | */ | |
348 | if (job->common.speed) { | |
349 | uint64_t delay_ns = ratelimit_calculate_delay(&job->limit, | |
350 | job->sectors_read); | |
351 | job->sectors_read = 0; | |
352 | block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns); | |
353 | } else { | |
354 | block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0); | |
355 | } | |
356 | ||
357 | if (block_job_is_cancelled(&job->common)) { | |
358 | return true; | |
359 | } | |
360 | ||
361 | return false; | |
362 | } | |
363 | ||
364 | static int coroutine_fn backup_run_incremental(BackupBlockJob *job) | |
365 | { | |
366 | bool error_is_read; | |
367 | int ret = 0; | |
368 | int clusters_per_iter; | |
369 | uint32_t granularity; | |
370 | int64_t sector; | |
371 | int64_t cluster; | |
372 | int64_t end; | |
373 | int64_t last_cluster = -1; | |
16096a4d | 374 | int64_t sectors_per_cluster = cluster_size_sectors(job); |
dc162c8e | 375 | BdrvDirtyBitmapIter *dbi; |
d58d8453 JS |
376 | |
377 | granularity = bdrv_dirty_bitmap_granularity(job->sync_bitmap); | |
16096a4d | 378 | clusters_per_iter = MAX((granularity / job->cluster_size), 1); |
dc162c8e | 379 | dbi = bdrv_dirty_iter_new(job->sync_bitmap, 0); |
d58d8453 JS |
380 | |
381 | /* Find the next dirty sector(s) */ | |
dc162c8e | 382 | while ((sector = bdrv_dirty_iter_next(dbi)) != -1) { |
16096a4d | 383 | cluster = sector / sectors_per_cluster; |
d58d8453 JS |
384 | |
385 | /* Fake progress updates for any clusters we skipped */ | |
386 | if (cluster != last_cluster + 1) { | |
387 | job->common.offset += ((cluster - last_cluster - 1) * | |
16096a4d | 388 | job->cluster_size); |
d58d8453 JS |
389 | } |
390 | ||
391 | for (end = cluster + clusters_per_iter; cluster < end; cluster++) { | |
392 | do { | |
393 | if (yield_and_check(job)) { | |
dc162c8e | 394 | goto out; |
d58d8453 | 395 | } |
8543c274 | 396 | ret = backup_do_cow(job, cluster * sectors_per_cluster, |
16096a4d | 397 | sectors_per_cluster, &error_is_read, |
06c3916b | 398 | false); |
d58d8453 JS |
399 | if ((ret < 0) && |
400 | backup_error_action(job, error_is_read, -ret) == | |
401 | BLOCK_ERROR_ACTION_REPORT) { | |
dc162c8e | 402 | goto out; |
d58d8453 JS |
403 | } |
404 | } while (ret < 0); | |
405 | } | |
406 | ||
407 | /* If the bitmap granularity is smaller than the backup granularity, | |
408 | * we need to advance the iterator pointer to the next cluster. */ | |
16096a4d | 409 | if (granularity < job->cluster_size) { |
dc162c8e | 410 | bdrv_set_dirty_iter(dbi, cluster * sectors_per_cluster); |
d58d8453 JS |
411 | } |
412 | ||
413 | last_cluster = cluster - 1; | |
414 | } | |
415 | ||
416 | /* Play some final catchup with the progress meter */ | |
16096a4d | 417 | end = DIV_ROUND_UP(job->common.len, job->cluster_size); |
d58d8453 | 418 | if (last_cluster + 1 < end) { |
16096a4d | 419 | job->common.offset += ((end - last_cluster - 1) * job->cluster_size); |
d58d8453 JS |
420 | } |
421 | ||
dc162c8e FZ |
422 | out: |
423 | bdrv_dirty_iter_free(dbi); | |
d58d8453 JS |
424 | return ret; |
425 | } | |
426 | ||
98d2c6f2 DM |
427 | static void coroutine_fn backup_run(void *opaque) |
428 | { | |
429 | BackupBlockJob *job = opaque; | |
761731b1 | 430 | BackupCompleteData *data; |
5c438bc6 KW |
431 | BlockDriverState *bs = blk_bs(job->common.blk); |
432 | BlockBackend *target = job->target; | |
98d2c6f2 | 433 | int64_t start, end; |
16096a4d | 434 | int64_t sectors_per_cluster = cluster_size_sectors(job); |
98d2c6f2 DM |
435 | int ret = 0; |
436 | ||
437 | QLIST_INIT(&job->inflight_reqs); | |
438 | qemu_co_rwlock_init(&job->flush_rwlock); | |
439 | ||
440 | start = 0; | |
16096a4d | 441 | end = DIV_ROUND_UP(job->common.len, job->cluster_size); |
98d2c6f2 | 442 | |
b2f56462 | 443 | job->done_bitmap = bitmap_new(end); |
98d2c6f2 | 444 | |
12b3e52e JS |
445 | job->before_write.notify = backup_before_write_notify; |
446 | bdrv_add_before_write_notifier(bs, &job->before_write); | |
98d2c6f2 | 447 | |
fc5d3f84 IM |
448 | if (job->sync_mode == MIRROR_SYNC_MODE_NONE) { |
449 | while (!block_job_is_cancelled(&job->common)) { | |
450 | /* Yield until the job is cancelled. We just let our before_write | |
451 | * notify callback service CoW requests. */ | |
5ab4b69c | 452 | block_job_yield(&job->common); |
98d2c6f2 | 453 | } |
4b80ab2b | 454 | } else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) { |
d58d8453 | 455 | ret = backup_run_incremental(job); |
fc5d3f84 IM |
456 | } else { |
457 | /* Both FULL and TOP SYNC_MODE's require copying.. */ | |
458 | for (; start < end; start++) { | |
459 | bool error_is_read; | |
d58d8453 | 460 | if (yield_and_check(job)) { |
98d2c6f2 | 461 | break; |
fc5d3f84 IM |
462 | } |
463 | ||
464 | if (job->sync_mode == MIRROR_SYNC_MODE_TOP) { | |
465 | int i, n; | |
466 | int alloced = 0; | |
467 | ||
468 | /* Check to see if these blocks are already in the | |
469 | * backing file. */ | |
470 | ||
16096a4d | 471 | for (i = 0; i < sectors_per_cluster;) { |
bdad13b9 | 472 | /* bdrv_is_allocated() only returns true/false based |
4c293dc6 | 473 | * on the first set of sectors it comes across that |
fc5d3f84 IM |
474 | * are are all in the same state. |
475 | * For that reason we must verify each sector in the | |
476 | * backup cluster length. We end up copying more than | |
477 | * needed but at some point that is always the case. */ | |
478 | alloced = | |
bdad13b9 | 479 | bdrv_is_allocated(bs, |
16096a4d JS |
480 | start * sectors_per_cluster + i, |
481 | sectors_per_cluster - i, &n); | |
fc5d3f84 IM |
482 | i += n; |
483 | ||
d40593dd | 484 | if (alloced == 1 || n == 0) { |
fc5d3f84 IM |
485 | break; |
486 | } | |
487 | } | |
488 | ||
489 | /* If the above loop never found any sectors that are in | |
490 | * the topmost image, skip this backup. */ | |
491 | if (alloced == 0) { | |
492 | continue; | |
493 | } | |
494 | } | |
495 | /* FULL sync mode we copy the whole drive. */ | |
8543c274 | 496 | ret = backup_do_cow(job, start * sectors_per_cluster, |
16096a4d | 497 | sectors_per_cluster, &error_is_read, false); |
fc5d3f84 IM |
498 | if (ret < 0) { |
499 | /* Depending on error action, fail now or retry cluster */ | |
500 | BlockErrorAction action = | |
501 | backup_error_action(job, error_is_read, -ret); | |
a589569f | 502 | if (action == BLOCK_ERROR_ACTION_REPORT) { |
fc5d3f84 IM |
503 | break; |
504 | } else { | |
505 | start--; | |
506 | continue; | |
507 | } | |
98d2c6f2 DM |
508 | } |
509 | } | |
510 | } | |
511 | ||
12b3e52e | 512 | notifier_with_return_remove(&job->before_write); |
98d2c6f2 DM |
513 | |
514 | /* wait until pending backup_do_cow() calls have completed */ | |
515 | qemu_co_rwlock_wrlock(&job->flush_rwlock); | |
516 | qemu_co_rwlock_unlock(&job->flush_rwlock); | |
b2f56462 | 517 | g_free(job->done_bitmap); |
98d2c6f2 | 518 | |
5c438bc6 | 519 | bdrv_op_unblock_all(blk_bs(target), job->common.blocker); |
98d2c6f2 | 520 | |
761731b1 SH |
521 | data = g_malloc(sizeof(*data)); |
522 | data->ret = ret; | |
523 | block_job_defer_to_main_loop(&job->common, backup_complete, data); | |
98d2c6f2 DM |
524 | } |
525 | ||
70559d49 AG |
526 | void backup_start(const char *job_id, BlockDriverState *bs, |
527 | BlockDriverState *target, int64_t speed, | |
528 | MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap, | |
13b9414b | 529 | bool compress, |
98d2c6f2 DM |
530 | BlockdevOnError on_source_error, |
531 | BlockdevOnError on_target_error, | |
097310b5 | 532 | BlockCompletionFunc *cb, void *opaque, |
78f51fde | 533 | BlockJobTxn *txn, Error **errp) |
98d2c6f2 DM |
534 | { |
535 | int64_t len; | |
4c9bca7e | 536 | BlockDriverInfo bdi; |
91ab6883 | 537 | BackupBlockJob *job = NULL; |
4c9bca7e | 538 | int ret; |
98d2c6f2 DM |
539 | |
540 | assert(bs); | |
541 | assert(target); | |
98d2c6f2 | 542 | |
c29c1dd3 FZ |
543 | if (bs == target) { |
544 | error_setg(errp, "Source and target cannot be the same"); | |
545 | return; | |
546 | } | |
547 | ||
c29c1dd3 FZ |
548 | if (!bdrv_is_inserted(bs)) { |
549 | error_setg(errp, "Device is not inserted: %s", | |
550 | bdrv_get_device_name(bs)); | |
551 | return; | |
552 | } | |
553 | ||
554 | if (!bdrv_is_inserted(target)) { | |
555 | error_setg(errp, "Device is not inserted: %s", | |
556 | bdrv_get_device_name(target)); | |
557 | return; | |
558 | } | |
559 | ||
13b9414b PB |
560 | if (compress && target->drv->bdrv_co_pwritev_compressed == NULL) { |
561 | error_setg(errp, "Compression is not supported for this drive %s", | |
562 | bdrv_get_device_name(target)); | |
563 | return; | |
564 | } | |
565 | ||
c29c1dd3 FZ |
566 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { |
567 | return; | |
568 | } | |
569 | ||
570 | if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) { | |
571 | return; | |
572 | } | |
573 | ||
4b80ab2b | 574 | if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) { |
d58d8453 JS |
575 | if (!sync_bitmap) { |
576 | error_setg(errp, "must provide a valid bitmap name for " | |
4b80ab2b | 577 | "\"incremental\" sync mode"); |
d58d8453 JS |
578 | return; |
579 | } | |
580 | ||
581 | /* Create a new bitmap, and freeze/disable this one. */ | |
582 | if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) { | |
583 | return; | |
584 | } | |
585 | } else if (sync_bitmap) { | |
586 | error_setg(errp, | |
587 | "a sync_bitmap was provided to backup_run, " | |
588 | "but received an incompatible sync_mode (%s)", | |
589 | MirrorSyncMode_lookup[sync_mode]); | |
590 | return; | |
591 | } | |
592 | ||
98d2c6f2 DM |
593 | len = bdrv_getlength(bs); |
594 | if (len < 0) { | |
595 | error_setg_errno(errp, -len, "unable to get length for '%s'", | |
596 | bdrv_get_device_name(bs)); | |
d58d8453 | 597 | goto error; |
98d2c6f2 DM |
598 | } |
599 | ||
70559d49 | 600 | job = block_job_create(job_id, &backup_job_driver, bs, speed, |
7f0317cf | 601 | cb, opaque, errp); |
98d2c6f2 | 602 | if (!job) { |
d58d8453 | 603 | goto error; |
98d2c6f2 DM |
604 | } |
605 | ||
5c438bc6 KW |
606 | job->target = blk_new(); |
607 | blk_insert_bs(job->target, target); | |
608 | ||
98d2c6f2 DM |
609 | job->on_source_error = on_source_error; |
610 | job->on_target_error = on_target_error; | |
fc5d3f84 | 611 | job->sync_mode = sync_mode; |
4b80ab2b | 612 | job->sync_bitmap = sync_mode == MIRROR_SYNC_MODE_INCREMENTAL ? |
d58d8453 | 613 | sync_bitmap : NULL; |
13b9414b | 614 | job->compress = compress; |
4c9bca7e JS |
615 | |
616 | /* If there is no backing file on the target, we cannot rely on COW if our | |
617 | * backup cluster size is smaller than the target cluster size. Even for | |
618 | * targets with a backing file, try to avoid COW if possible. */ | |
5c438bc6 | 619 | ret = bdrv_get_info(target, &bdi); |
4c9bca7e JS |
620 | if (ret < 0 && !target->backing) { |
621 | error_setg_errno(errp, -ret, | |
622 | "Couldn't determine the cluster size of the target image, " | |
623 | "which has no backing file"); | |
624 | error_append_hint(errp, | |
625 | "Aborting, since this may create an unusable destination image\n"); | |
626 | goto error; | |
627 | } else if (ret < 0 && target->backing) { | |
628 | /* Not fatal; just trudge on ahead. */ | |
629 | job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT; | |
630 | } else { | |
631 | job->cluster_size = MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size); | |
632 | } | |
633 | ||
634 | bdrv_op_block_all(target, job->common.blocker); | |
98d2c6f2 | 635 | job->common.len = len; |
0b8b8753 | 636 | job->common.co = qemu_coroutine_create(backup_run, job); |
78f51fde | 637 | block_job_txn_add_job(txn, &job->common); |
0b8b8753 | 638 | qemu_coroutine_enter(job->common.co); |
d58d8453 JS |
639 | return; |
640 | ||
641 | error: | |
642 | if (sync_bitmap) { | |
643 | bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL); | |
644 | } | |
91ab6883 | 645 | if (job) { |
5c438bc6 | 646 | blk_unref(job->target); |
91ab6883 KW |
647 | block_job_unref(&job->common); |
648 | } | |
98d2c6f2 | 649 | } |