blob: fe010e78e33cbb9c232e90a7ac38850bdf6905d4 [file] [log] [blame]
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +02001/*
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
Peter Maydell80c71a22016-01-18 18:01:42 +000014#include "qemu/osdep.h"
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020015
16#include "trace.h"
17#include "block/block.h"
18#include "block/block_int.h"
John Snowc87621e2016-10-27 12:07:00 -040019#include "block/blockjob_int.h"
Wen Congyang49d3e822016-07-27 15:01:43 +080020#include "block/block_backup.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010021#include "qapi/error.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010022#include "qapi/qmp/qerror.h"
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020023#include "qemu/ratelimit.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020024#include "qemu/cutils.h"
Max Reitz373340b2015-10-19 17:53:22 +020025#include "sysemu/block-backend.h"
Fam Zhengb2f56462016-03-08 12:44:52 +080026#include "qemu/bitmap.h"
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020027
John Snow16096a42016-02-25 15:58:29 -050028#define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020029#define SLICE_TIME 100000000ULL /* ns */
30
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020031typedef struct BackupBlockJob {
32 BlockJob common;
Kevin Wolf5c438bc2016-04-14 13:09:53 +020033 BlockBackend *target;
John Snow4b80ab22015-06-04 20:20:34 -040034 /* bitmap for sync=incremental */
John Snowd58d8452015-04-17 19:49:58 -040035 BdrvDirtyBitmap *sync_bitmap;
Ian Mainfc5d3f82013-07-26 11:39:04 -070036 MirrorSyncMode sync_mode;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020037 RateLimit limit;
38 BlockdevOnError on_source_error;
39 BlockdevOnError on_target_error;
40 CoRwlock flush_rwlock;
41 uint64_t sectors_read;
Fam Zhengb2f56462016-03-08 12:44:52 +080042 unsigned long *done_bitmap;
John Snow16096a42016-02-25 15:58:29 -050043 int64_t cluster_size;
Pavel Butsykin13b94142016-07-22 11:17:52 +030044 bool compress;
John Snow12b3e522016-01-26 18:54:58 -050045 NotifierWithReturn before_write;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020046 QLIST_HEAD(, CowRequest) inflight_reqs;
47} BackupBlockJob;
48
John Snow16096a42016-02-25 15:58:29 -050049/* Size of a cluster in sectors, instead of bytes. */
50static inline int64_t cluster_size_sectors(BackupBlockJob *job)
51{
52 return job->cluster_size / BDRV_SECTOR_SIZE;
53}
54
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020055/* See if in-flight requests overlap and wait for them to complete */
56static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job,
57 int64_t start,
58 int64_t end)
59{
60 CowRequest *req;
61 bool retry;
62
63 do {
64 retry = false;
65 QLIST_FOREACH(req, &job->inflight_reqs, list) {
66 if (end > req->start && start < req->end) {
Paolo Bonzini1ace7ce2017-02-13 19:12:43 +010067 qemu_co_queue_wait(&req->wait_queue, NULL);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020068 retry = true;
69 break;
70 }
71 }
72 } while (retry);
73}
74
75/* Keep track of an in-flight request */
76static void cow_request_begin(CowRequest *req, BackupBlockJob *job,
77 int64_t start, int64_t end)
78{
79 req->start = start;
80 req->end = end;
81 qemu_co_queue_init(&req->wait_queue);
82 QLIST_INSERT_HEAD(&job->inflight_reqs, req, list);
83}
84
85/* Forget about a completed request */
86static void cow_request_end(CowRequest *req)
87{
88 QLIST_REMOVE(req, list);
89 qemu_co_queue_restart_all(&req->wait_queue);
90}
91
Kevin Wolf8543c272016-04-14 15:56:02 +020092static int coroutine_fn backup_do_cow(BackupBlockJob *job,
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020093 int64_t sector_num, int nb_sectors,
Wen Congyang06c39162015-09-08 11:28:33 +080094 bool *error_is_read,
95 bool is_write_notifier)
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020096{
Kevin Wolf5c438bc2016-04-14 13:09:53 +020097 BlockBackend *blk = job->common.blk;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020098 CowRequest cow_request;
99 struct iovec iov;
100 QEMUIOVector bounce_qiov;
101 void *bounce_buffer = NULL;
102 int ret = 0;
John Snow16096a42016-02-25 15:58:29 -0500103 int64_t sectors_per_cluster = cluster_size_sectors(job);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200104 int64_t start, end;
105 int n;
106
107 qemu_co_rwlock_rdlock(&job->flush_rwlock);
108
John Snow16096a42016-02-25 15:58:29 -0500109 start = sector_num / sectors_per_cluster;
110 end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200111
112 trace_backup_do_cow_enter(job, start, sector_num, nb_sectors);
113
114 wait_for_overlapping_requests(job, start, end);
115 cow_request_begin(&cow_request, job, start, end);
116
117 for (; start < end; start++) {
Fam Zhengb2f56462016-03-08 12:44:52 +0800118 if (test_bit(start, job->done_bitmap)) {
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200119 trace_backup_do_cow_skip(job, start);
120 continue; /* already copied */
121 }
122
123 trace_backup_do_cow_process(job, start);
124
John Snow16096a42016-02-25 15:58:29 -0500125 n = MIN(sectors_per_cluster,
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200126 job->common.len / BDRV_SECTOR_SIZE -
John Snow16096a42016-02-25 15:58:29 -0500127 start * sectors_per_cluster);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200128
129 if (!bounce_buffer) {
Kevin Wolf5c438bc2016-04-14 13:09:53 +0200130 bounce_buffer = blk_blockalign(blk, job->cluster_size);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200131 }
132 iov.iov_base = bounce_buffer;
133 iov.iov_len = n * BDRV_SECTOR_SIZE;
134 qemu_iovec_init_external(&bounce_qiov, &iov, 1);
135
Kevin Wolf5c438bc2016-04-14 13:09:53 +0200136 ret = blk_co_preadv(blk, start * job->cluster_size,
137 bounce_qiov.size, &bounce_qiov,
138 is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200139 if (ret < 0) {
140 trace_backup_do_cow_read_fail(job, start, ret);
141 if (error_is_read) {
142 *error_is_read = true;
143 }
144 goto out;
145 }
146
147 if (buffer_is_zero(iov.iov_base, iov.iov_len)) {
Kevin Wolf5c438bc2016-04-14 13:09:53 +0200148 ret = blk_co_pwrite_zeroes(job->target, start * job->cluster_size,
149 bounce_qiov.size, BDRV_REQ_MAY_UNMAP);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200150 } else {
Kevin Wolf5c438bc2016-04-14 13:09:53 +0200151 ret = blk_co_pwritev(job->target, start * job->cluster_size,
Pavel Butsykin13b94142016-07-22 11:17:52 +0300152 bounce_qiov.size, &bounce_qiov,
153 job->compress ? BDRV_REQ_WRITE_COMPRESSED : 0);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200154 }
155 if (ret < 0) {
156 trace_backup_do_cow_write_fail(job, start, ret);
157 if (error_is_read) {
158 *error_is_read = false;
159 }
160 goto out;
161 }
162
Fam Zhengb2f56462016-03-08 12:44:52 +0800163 set_bit(start, job->done_bitmap);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200164
165 /* Publish progress, guest I/O counts as progress too. Note that the
166 * offset field is an opaque progress value, it is not a disk offset.
167 */
168 job->sectors_read += n;
169 job->common.offset += n * BDRV_SECTOR_SIZE;
170 }
171
172out:
173 if (bounce_buffer) {
174 qemu_vfree(bounce_buffer);
175 }
176
177 cow_request_end(&cow_request);
178
179 trace_backup_do_cow_return(job, sector_num, nb_sectors, ret);
180
181 qemu_co_rwlock_unlock(&job->flush_rwlock);
182
183 return ret;
184}
185
186static int coroutine_fn backup_before_write_notify(
187 NotifierWithReturn *notifier,
188 void *opaque)
189{
John Snow12b3e522016-01-26 18:54:58 -0500190 BackupBlockJob *job = container_of(notifier, BackupBlockJob, before_write);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200191 BdrvTrackedRequest *req = opaque;
Kevin Wolf793ed472013-12-03 15:31:25 +0100192 int64_t sector_num = req->offset >> BDRV_SECTOR_BITS;
193 int nb_sectors = req->bytes >> BDRV_SECTOR_BITS;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200194
Kevin Wolf5c438bc2016-04-14 13:09:53 +0200195 assert(req->bs == blk_bs(job->common.blk));
Kevin Wolf793ed472013-12-03 15:31:25 +0100196 assert((req->offset & (BDRV_SECTOR_SIZE - 1)) == 0);
197 assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
198
Kevin Wolf8543c272016-04-14 15:56:02 +0200199 return backup_do_cow(job, sector_num, nb_sectors, NULL, true);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200200}
201
202static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp)
203{
204 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
205
206 if (speed < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100207 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200208 return;
209 }
210 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
211}
212
Fam Zhengb976ea32015-11-05 18:13:10 -0500213static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
214{
215 BdrvDirtyBitmap *bm;
Kevin Wolf5c438bc2016-04-14 13:09:53 +0200216 BlockDriverState *bs = blk_bs(job->common.blk);
Fam Zhengb976ea32015-11-05 18:13:10 -0500217
218 if (ret < 0 || block_job_is_cancelled(&job->common)) {
219 /* Merge the successor back into the parent, delete nothing. */
220 bm = bdrv_reclaim_dirty_bitmap(bs, job->sync_bitmap, NULL);
221 assert(bm);
222 } else {
223 /* Everything is fine, delete this bitmap and install the backup. */
224 bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL);
225 assert(bm);
226 }
227}
228
John Snowc347b2c2015-11-05 18:13:16 -0500229static void backup_commit(BlockJob *job)
230{
231 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
232 if (s->sync_bitmap) {
233 backup_cleanup_sync_bitmap(s, 0);
234 }
235}
236
237static void backup_abort(BlockJob *job)
238{
239 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
240 if (s->sync_bitmap) {
241 backup_cleanup_sync_bitmap(s, -1);
242 }
243}
244
John Snowe8a40bf2016-11-08 01:50:35 -0500245static void backup_clean(BlockJob *job)
246{
247 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
248 assert(s->target);
249 blk_unref(s->target);
250 s->target = NULL;
251}
252
Stefan Hajnoczi5ab4b692016-06-16 17:56:29 +0100253static void backup_attached_aio_context(BlockJob *job, AioContext *aio_context)
254{
255 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
256
257 blk_set_aio_context(s->target, aio_context);
258}
259
Wen Congyang49d3e822016-07-27 15:01:43 +0800260void backup_do_checkpoint(BlockJob *job, Error **errp)
261{
262 BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
263 int64_t len;
264
265 assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP);
266
267 if (backup_job->sync_mode != MIRROR_SYNC_MODE_NONE) {
268 error_setg(errp, "The backup job only supports block checkpoint in"
269 " sync=none mode");
270 return;
271 }
272
273 len = DIV_ROUND_UP(backup_job->common.len, backup_job->cluster_size);
274 bitmap_zero(backup_job->done_bitmap, len);
275}
276
Changlong Xiea8bbee02016-07-27 15:01:44 +0800277void backup_wait_for_overlapping_requests(BlockJob *job, int64_t sector_num,
278 int nb_sectors)
279{
280 BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
281 int64_t sectors_per_cluster = cluster_size_sectors(backup_job);
282 int64_t start, end;
283
284 assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP);
285
286 start = sector_num / sectors_per_cluster;
287 end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
288 wait_for_overlapping_requests(backup_job, start, end);
289}
290
291void backup_cow_request_begin(CowRequest *req, BlockJob *job,
292 int64_t sector_num,
293 int nb_sectors)
294{
295 BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
296 int64_t sectors_per_cluster = cluster_size_sectors(backup_job);
297 int64_t start, end;
298
299 assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP);
300
301 start = sector_num / sectors_per_cluster;
302 end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
303 cow_request_begin(req, backup_job, start, end);
304}
305
306void backup_cow_request_end(CowRequest *req)
307{
308 cow_request_end(req);
309}
310
Paolo Bonzinibae81962016-10-27 12:48:50 +0200311static void backup_drain(BlockJob *job)
312{
313 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
314
315 /* Need to keep a reference in case blk_drain triggers execution
316 * of backup_complete...
317 */
318 if (s->target) {
319 BlockBackend *target = s->target;
320 blk_ref(target);
321 blk_drain(target);
322 blk_unref(target);
323 }
324}
325
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200326static BlockErrorAction backup_error_action(BackupBlockJob *job,
327 bool read, int error)
328{
329 if (read) {
Kevin Wolf81e254d2016-04-18 11:36:38 +0200330 return block_job_error_action(&job->common, job->on_source_error,
331 true, error);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200332 } else {
Kevin Wolf81e254d2016-04-18 11:36:38 +0200333 return block_job_error_action(&job->common, job->on_target_error,
334 false, error);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200335 }
336}
337
Stefan Hajnoczi761731b2014-10-21 12:03:56 +0100338typedef struct {
339 int ret;
340} BackupCompleteData;
341
342static void backup_complete(BlockJob *job, void *opaque)
343{
Stefan Hajnoczi761731b2014-10-21 12:03:56 +0100344 BackupCompleteData *data = opaque;
345
Stefan Hajnoczi761731b2014-10-21 12:03:56 +0100346 block_job_completed(job, data->ret);
347 g_free(data);
348}
349
John Snowd58d8452015-04-17 19:49:58 -0400350static bool coroutine_fn yield_and_check(BackupBlockJob *job)
351{
352 if (block_job_is_cancelled(&job->common)) {
353 return true;
354 }
355
356 /* we need to yield so that bdrv_drain_all() returns.
357 * (without, VM does not reboot)
358 */
359 if (job->common.speed) {
360 uint64_t delay_ns = ratelimit_calculate_delay(&job->limit,
361 job->sectors_read);
362 job->sectors_read = 0;
363 block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns);
364 } else {
365 block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0);
366 }
367
368 if (block_job_is_cancelled(&job->common)) {
369 return true;
370 }
371
372 return false;
373}
374
375static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
376{
377 bool error_is_read;
378 int ret = 0;
379 int clusters_per_iter;
380 uint32_t granularity;
381 int64_t sector;
382 int64_t cluster;
383 int64_t end;
384 int64_t last_cluster = -1;
John Snow16096a42016-02-25 15:58:29 -0500385 int64_t sectors_per_cluster = cluster_size_sectors(job);
Fam Zhengdc162c82016-10-13 17:58:21 -0400386 BdrvDirtyBitmapIter *dbi;
John Snowd58d8452015-04-17 19:49:58 -0400387
388 granularity = bdrv_dirty_bitmap_granularity(job->sync_bitmap);
John Snow16096a42016-02-25 15:58:29 -0500389 clusters_per_iter = MAX((granularity / job->cluster_size), 1);
Fam Zhengdc162c82016-10-13 17:58:21 -0400390 dbi = bdrv_dirty_iter_new(job->sync_bitmap, 0);
John Snowd58d8452015-04-17 19:49:58 -0400391
392 /* Find the next dirty sector(s) */
Fam Zhengdc162c82016-10-13 17:58:21 -0400393 while ((sector = bdrv_dirty_iter_next(dbi)) != -1) {
John Snow16096a42016-02-25 15:58:29 -0500394 cluster = sector / sectors_per_cluster;
John Snowd58d8452015-04-17 19:49:58 -0400395
396 /* Fake progress updates for any clusters we skipped */
397 if (cluster != last_cluster + 1) {
398 job->common.offset += ((cluster - last_cluster - 1) *
John Snow16096a42016-02-25 15:58:29 -0500399 job->cluster_size);
John Snowd58d8452015-04-17 19:49:58 -0400400 }
401
402 for (end = cluster + clusters_per_iter; cluster < end; cluster++) {
403 do {
404 if (yield_and_check(job)) {
Fam Zhengdc162c82016-10-13 17:58:21 -0400405 goto out;
John Snowd58d8452015-04-17 19:49:58 -0400406 }
Kevin Wolf8543c272016-04-14 15:56:02 +0200407 ret = backup_do_cow(job, cluster * sectors_per_cluster,
John Snow16096a42016-02-25 15:58:29 -0500408 sectors_per_cluster, &error_is_read,
Wen Congyang06c39162015-09-08 11:28:33 +0800409 false);
John Snowd58d8452015-04-17 19:49:58 -0400410 if ((ret < 0) &&
411 backup_error_action(job, error_is_read, -ret) ==
412 BLOCK_ERROR_ACTION_REPORT) {
Fam Zhengdc162c82016-10-13 17:58:21 -0400413 goto out;
John Snowd58d8452015-04-17 19:49:58 -0400414 }
415 } while (ret < 0);
416 }
417
418 /* If the bitmap granularity is smaller than the backup granularity,
419 * we need to advance the iterator pointer to the next cluster. */
John Snow16096a42016-02-25 15:58:29 -0500420 if (granularity < job->cluster_size) {
Fam Zhengdc162c82016-10-13 17:58:21 -0400421 bdrv_set_dirty_iter(dbi, cluster * sectors_per_cluster);
John Snowd58d8452015-04-17 19:49:58 -0400422 }
423
424 last_cluster = cluster - 1;
425 }
426
427 /* Play some final catchup with the progress meter */
John Snow16096a42016-02-25 15:58:29 -0500428 end = DIV_ROUND_UP(job->common.len, job->cluster_size);
John Snowd58d8452015-04-17 19:49:58 -0400429 if (last_cluster + 1 < end) {
John Snow16096a42016-02-25 15:58:29 -0500430 job->common.offset += ((end - last_cluster - 1) * job->cluster_size);
John Snowd58d8452015-04-17 19:49:58 -0400431 }
432
Fam Zhengdc162c82016-10-13 17:58:21 -0400433out:
434 bdrv_dirty_iter_free(dbi);
John Snowd58d8452015-04-17 19:49:58 -0400435 return ret;
436}
437
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200438static void coroutine_fn backup_run(void *opaque)
439{
440 BackupBlockJob *job = opaque;
Stefan Hajnoczi761731b2014-10-21 12:03:56 +0100441 BackupCompleteData *data;
Kevin Wolf5c438bc2016-04-14 13:09:53 +0200442 BlockDriverState *bs = blk_bs(job->common.blk);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200443 int64_t start, end;
John Snow16096a42016-02-25 15:58:29 -0500444 int64_t sectors_per_cluster = cluster_size_sectors(job);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200445 int ret = 0;
446
447 QLIST_INIT(&job->inflight_reqs);
448 qemu_co_rwlock_init(&job->flush_rwlock);
449
450 start = 0;
John Snow16096a42016-02-25 15:58:29 -0500451 end = DIV_ROUND_UP(job->common.len, job->cluster_size);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200452
Fam Zhengb2f56462016-03-08 12:44:52 +0800453 job->done_bitmap = bitmap_new(end);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200454
John Snow12b3e522016-01-26 18:54:58 -0500455 job->before_write.notify = backup_before_write_notify;
456 bdrv_add_before_write_notifier(bs, &job->before_write);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200457
Ian Mainfc5d3f82013-07-26 11:39:04 -0700458 if (job->sync_mode == MIRROR_SYNC_MODE_NONE) {
459 while (!block_job_is_cancelled(&job->common)) {
460 /* Yield until the job is cancelled. We just let our before_write
461 * notify callback service CoW requests. */
Stefan Hajnoczi5ab4b692016-06-16 17:56:29 +0100462 block_job_yield(&job->common);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200463 }
John Snow4b80ab22015-06-04 20:20:34 -0400464 } else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
John Snowd58d8452015-04-17 19:49:58 -0400465 ret = backup_run_incremental(job);
Ian Mainfc5d3f82013-07-26 11:39:04 -0700466 } else {
467 /* Both FULL and TOP SYNC_MODE's require copying.. */
468 for (; start < end; start++) {
469 bool error_is_read;
John Snowd58d8452015-04-17 19:49:58 -0400470 if (yield_and_check(job)) {
Ian Mainfc5d3f82013-07-26 11:39:04 -0700471 break;
472 }
473
474 if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
475 int i, n;
476 int alloced = 0;
477
478 /* Check to see if these blocks are already in the
479 * backing file. */
480
John Snow16096a42016-02-25 15:58:29 -0500481 for (i = 0; i < sectors_per_cluster;) {
Paolo Bonzinibdad13b2013-09-04 19:00:22 +0200482 /* bdrv_is_allocated() only returns true/false based
Stefan Weil4c293dc2013-08-18 19:40:06 +0200483 * on the first set of sectors it comes across that
Ian Mainfc5d3f82013-07-26 11:39:04 -0700484 * are are all in the same state.
485 * For that reason we must verify each sector in the
486 * backup cluster length. We end up copying more than
487 * needed but at some point that is always the case. */
488 alloced =
Paolo Bonzinibdad13b2013-09-04 19:00:22 +0200489 bdrv_is_allocated(bs,
John Snow16096a42016-02-25 15:58:29 -0500490 start * sectors_per_cluster + i,
491 sectors_per_cluster - i, &n);
Ian Mainfc5d3f82013-07-26 11:39:04 -0700492 i += n;
493
Kevin Wolfd40593d2014-07-07 16:38:58 +0200494 if (alloced == 1 || n == 0) {
Ian Mainfc5d3f82013-07-26 11:39:04 -0700495 break;
496 }
497 }
498
499 /* If the above loop never found any sectors that are in
500 * the topmost image, skip this backup. */
501 if (alloced == 0) {
502 continue;
503 }
504 }
505 /* FULL sync mode we copy the whole drive. */
Kevin Wolf8543c272016-04-14 15:56:02 +0200506 ret = backup_do_cow(job, start * sectors_per_cluster,
John Snow16096a42016-02-25 15:58:29 -0500507 sectors_per_cluster, &error_is_read, false);
Ian Mainfc5d3f82013-07-26 11:39:04 -0700508 if (ret < 0) {
509 /* Depending on error action, fail now or retry cluster */
510 BlockErrorAction action =
511 backup_error_action(job, error_is_read, -ret);
Wenchao Xiaa5895692014-06-18 08:43:30 +0200512 if (action == BLOCK_ERROR_ACTION_REPORT) {
Ian Mainfc5d3f82013-07-26 11:39:04 -0700513 break;
514 } else {
515 start--;
516 continue;
517 }
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200518 }
519 }
520 }
521
John Snow12b3e522016-01-26 18:54:58 -0500522 notifier_with_return_remove(&job->before_write);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200523
524 /* wait until pending backup_do_cow() calls have completed */
525 qemu_co_rwlock_wrlock(&job->flush_rwlock);
526 qemu_co_rwlock_unlock(&job->flush_rwlock);
Fam Zhengb2f56462016-03-08 12:44:52 +0800527 g_free(job->done_bitmap);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200528
Stefan Hajnoczi761731b2014-10-21 12:03:56 +0100529 data = g_malloc(sizeof(*data));
530 data->ret = ret;
531 block_job_defer_to_main_loop(&job->common, backup_complete, data);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200532}
533
John Snowa7815a72016-11-08 01:50:36 -0500534static const BlockJobDriver backup_job_driver = {
535 .instance_size = sizeof(BackupBlockJob),
536 .job_type = BLOCK_JOB_TYPE_BACKUP,
537 .start = backup_run,
538 .set_speed = backup_set_speed,
539 .commit = backup_commit,
540 .abort = backup_abort,
541 .clean = backup_clean,
542 .attached_aio_context = backup_attached_aio_context,
543 .drain = backup_drain,
544};
545
John Snow111049a2016-11-08 01:50:38 -0500546BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
Alberto Garcia70559d42016-07-05 17:28:58 +0300547 BlockDriverState *target, int64_t speed,
548 MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap,
Pavel Butsykin13b94142016-07-22 11:17:52 +0300549 bool compress,
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200550 BlockdevOnError on_source_error,
551 BlockdevOnError on_target_error,
John Snow47970df2016-10-27 12:06:57 -0400552 int creation_flags,
Markus Armbruster097310b2014-10-07 13:59:15 +0200553 BlockCompletionFunc *cb, void *opaque,
John Snow78f51fd2015-11-05 18:13:17 -0500554 BlockJobTxn *txn, Error **errp)
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200555{
556 int64_t len;
John Snow4c9bca72016-02-25 15:58:30 -0500557 BlockDriverInfo bdi;
Kevin Wolf91ab6882016-04-14 12:59:55 +0200558 BackupBlockJob *job = NULL;
John Snow4c9bca72016-02-25 15:58:30 -0500559 int ret;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200560
561 assert(bs);
562 assert(target);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200563
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800564 if (bs == target) {
565 error_setg(errp, "Source and target cannot be the same");
John Snow111049a2016-11-08 01:50:38 -0500566 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800567 }
568
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800569 if (!bdrv_is_inserted(bs)) {
570 error_setg(errp, "Device is not inserted: %s",
571 bdrv_get_device_name(bs));
John Snow111049a2016-11-08 01:50:38 -0500572 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800573 }
574
575 if (!bdrv_is_inserted(target)) {
576 error_setg(errp, "Device is not inserted: %s",
577 bdrv_get_device_name(target));
John Snow111049a2016-11-08 01:50:38 -0500578 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800579 }
580
Pavel Butsykin13b94142016-07-22 11:17:52 +0300581 if (compress && target->drv->bdrv_co_pwritev_compressed == NULL) {
582 error_setg(errp, "Compression is not supported for this drive %s",
583 bdrv_get_device_name(target));
John Snow111049a2016-11-08 01:50:38 -0500584 return NULL;
Pavel Butsykin13b94142016-07-22 11:17:52 +0300585 }
586
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800587 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
John Snow111049a2016-11-08 01:50:38 -0500588 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800589 }
590
591 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
John Snow111049a2016-11-08 01:50:38 -0500592 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800593 }
594
John Snow4b80ab22015-06-04 20:20:34 -0400595 if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
John Snowd58d8452015-04-17 19:49:58 -0400596 if (!sync_bitmap) {
597 error_setg(errp, "must provide a valid bitmap name for "
John Snow4b80ab22015-06-04 20:20:34 -0400598 "\"incremental\" sync mode");
John Snow111049a2016-11-08 01:50:38 -0500599 return NULL;
John Snowd58d8452015-04-17 19:49:58 -0400600 }
601
602 /* Create a new bitmap, and freeze/disable this one. */
603 if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) {
John Snow111049a2016-11-08 01:50:38 -0500604 return NULL;
John Snowd58d8452015-04-17 19:49:58 -0400605 }
606 } else if (sync_bitmap) {
607 error_setg(errp,
608 "a sync_bitmap was provided to backup_run, "
609 "but received an incompatible sync_mode (%s)",
610 MirrorSyncMode_lookup[sync_mode]);
John Snow111049a2016-11-08 01:50:38 -0500611 return NULL;
John Snowd58d8452015-04-17 19:49:58 -0400612 }
613
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200614 len = bdrv_getlength(bs);
615 if (len < 0) {
616 error_setg_errno(errp, -len, "unable to get length for '%s'",
617 bdrv_get_device_name(bs));
John Snowd58d8452015-04-17 19:49:58 -0400618 goto error;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200619 }
620
Alberto Garcia70559d42016-07-05 17:28:58 +0300621 job = block_job_create(job_id, &backup_job_driver, bs, speed,
John Snow47970df2016-10-27 12:06:57 -0400622 creation_flags, cb, opaque, errp);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200623 if (!job) {
John Snowd58d8452015-04-17 19:49:58 -0400624 goto error;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200625 }
626
Kevin Wolf5c438bc2016-04-14 13:09:53 +0200627 job->target = blk_new();
628 blk_insert_bs(job->target, target);
629
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200630 job->on_source_error = on_source_error;
631 job->on_target_error = on_target_error;
Ian Mainfc5d3f82013-07-26 11:39:04 -0700632 job->sync_mode = sync_mode;
John Snow4b80ab22015-06-04 20:20:34 -0400633 job->sync_bitmap = sync_mode == MIRROR_SYNC_MODE_INCREMENTAL ?
John Snowd58d8452015-04-17 19:49:58 -0400634 sync_bitmap : NULL;
Pavel Butsykin13b94142016-07-22 11:17:52 +0300635 job->compress = compress;
John Snow4c9bca72016-02-25 15:58:30 -0500636
637 /* If there is no backing file on the target, we cannot rely on COW if our
638 * backup cluster size is smaller than the target cluster size. Even for
639 * targets with a backing file, try to avoid COW if possible. */
Kevin Wolf5c438bc2016-04-14 13:09:53 +0200640 ret = bdrv_get_info(target, &bdi);
John Snow4c9bca72016-02-25 15:58:30 -0500641 if (ret < 0 && !target->backing) {
642 error_setg_errno(errp, -ret,
643 "Couldn't determine the cluster size of the target image, "
644 "which has no backing file");
645 error_append_hint(errp,
646 "Aborting, since this may create an unusable destination image\n");
647 goto error;
648 } else if (ret < 0 && target->backing) {
649 /* Not fatal; just trudge on ahead. */
650 job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
651 } else {
652 job->cluster_size = MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
653 }
654
Alberto Garciab7340d02016-10-28 10:08:06 +0300655 block_job_add_bdrv(&job->common, target);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200656 job->common.len = len;
John Snow78f51fd2015-11-05 18:13:17 -0500657 block_job_txn_add_job(txn, &job->common);
John Snow111049a2016-11-08 01:50:38 -0500658
659 return &job->common;
John Snowd58d8452015-04-17 19:49:58 -0400660
661 error:
662 if (sync_bitmap) {
663 bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
664 }
Kevin Wolf91ab6882016-04-14 12:59:55 +0200665 if (job) {
John Snowe8a40bf2016-11-08 01:50:35 -0500666 backup_clean(&job->common);
Kevin Wolf91ab6882016-04-14 12:59:55 +0200667 block_job_unref(&job->common);
668 }
John Snow111049a2016-11-08 01:50:38 -0500669
670 return NULL;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200671}