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