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