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