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