blob: bd3614ce707084e332576d11655bdb317fe0b884 [file] [log] [blame]
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +02001/*
2 * QEMU backup
3 *
4 * Copyright (C) 2013 Proxmox Server Solutions
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +03005 * Copyright (c) 2019 Virtuozzo International GmbH.
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +02006 *
7 * Authors:
8 * Dietmar Maurer (dietmar@proxmox.com)
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
12 *
13 */
14
Peter Maydell80c71a22016-01-18 18:01:42 +000015#include "qemu/osdep.h"
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020016
17#include "trace.h"
18#include "block/block.h"
19#include "block/block_int.h"
John Snowc87621e2016-10-27 12:07:00 -040020#include "block/blockjob_int.h"
Wen Congyang49d3e822016-07-27 15:01:43 +080021#include "block/block_backup.h"
Vladimir Sementsov-Ogievskiybeb5f542019-09-20 17:20:48 +030022#include "block/block-copy.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010023#include "qapi/error.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010024#include "qapi/qmp/qerror.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020025#include "qemu/cutils.h"
Max Reitz373340b2015-10-19 17:53:22 +020026#include "sysemu/block-backend.h"
Fam Zhengb2f56462016-03-08 12:44:52 +080027#include "qemu/bitmap.h"
Vladimir Sementsov-Ogievskiya410a7f2017-02-28 22:33:40 +030028#include "qemu/error-report.h"
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020029
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +030030#include "block/backup-top.h"
31
John Snow16096a42016-02-25 15:58:29 -050032#define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020033
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020034typedef struct BackupBlockJob {
35 BlockJob common;
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +030036 BlockDriverState *backup_top;
Vladimir Sementsov-Ogievskiy2c8074c2019-09-20 17:20:46 +030037 BlockDriverState *source_bs;
Vladimir Sementsov-Ogievskiyff789bf2021-02-05 19:37:19 +030038 BlockDriverState *target_bs;
John Snow62aa1fb2019-07-29 16:35:53 -040039
John Snowd58d8452015-04-17 19:49:58 -040040 BdrvDirtyBitmap *sync_bitmap;
John Snow62aa1fb2019-07-29 16:35:53 -040041
Ian Mainfc5d3f82013-07-26 11:39:04 -070042 MirrorSyncMode sync_mode;
John Snowc8b56502019-07-29 16:35:52 -040043 BitmapSyncMode bitmap_mode;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020044 BlockdevOnError on_source_error;
45 BlockdevOnError on_target_error;
Kevin Wolf05df8a62018-01-18 18:08:22 +010046 uint64_t len;
John Snow16096a42016-02-25 15:58:29 -050047 int64_t cluster_size;
Vladimir Sementsov-Ogievskiy86c6a3b2021-01-17 00:46:43 +030048 BackupPerf perf;
Vladimir Sementsov-Ogievskiya193b0f2017-10-12 16:53:10 +030049
Vladimir Sementsov-Ogievskiy2c8074c2019-09-20 17:20:46 +030050 BlockCopyState *bcs;
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +030051
52 bool wait;
53 BlockCopyCallState *bg_bcs_call;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +020054} BackupBlockJob;
55
Kevin Wolfbd219352018-01-19 15:54:40 +010056static const BlockJobDriver backup_job_driver;
57
Fam Zhengb976ea32015-11-05 18:13:10 -050058static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
59{
60 BdrvDirtyBitmap *bm;
John Snowc23909e2019-07-29 16:35:53 -040061 bool sync = (((ret == 0) || (job->bitmap_mode == BITMAP_SYNC_MODE_ALWAYS)) \
62 && (job->bitmap_mode != BITMAP_SYNC_MODE_NEVER));
Fam Zhengb976ea32015-11-05 18:13:10 -050063
John Snowc23909e2019-07-29 16:35:53 -040064 if (sync) {
John Snowcf0cd292019-07-29 16:35:53 -040065 /*
John Snowc23909e2019-07-29 16:35:53 -040066 * We succeeded, or we always intended to sync the bitmap.
67 * Delete this bitmap and install the child.
68 */
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +030069 bm = bdrv_dirty_bitmap_abdicate(job->sync_bitmap, NULL);
John Snowc23909e2019-07-29 16:35:53 -040070 } else {
71 /*
72 * We failed, or we never intended to sync the bitmap anyway.
73 * Merge the successor back into the parent, keeping all data.
John Snowcf0cd292019-07-29 16:35:53 -040074 */
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +030075 bm = bdrv_reclaim_dirty_bitmap(job->sync_bitmap, NULL);
John Snowc23909e2019-07-29 16:35:53 -040076 }
77
78 assert(bm);
79
80 if (ret < 0 && job->bitmap_mode == BITMAP_SYNC_MODE_ALWAYS) {
81 /* If we failed and synced, merge in the bits we didn't copy: */
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +030082 bdrv_dirty_bitmap_merge_internal(bm, block_copy_dirty_bitmap(job->bcs),
John Snowc23909e2019-07-29 16:35:53 -040083 NULL, true);
Fam Zhengb976ea32015-11-05 18:13:10 -050084 }
85}
86
Kevin Wolf4ad35182018-04-19 17:30:16 +020087static void backup_commit(Job *job)
John Snowc347b2c2015-11-05 18:13:16 -050088{
Kevin Wolf4ad35182018-04-19 17:30:16 +020089 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
John Snowc347b2c2015-11-05 18:13:16 -050090 if (s->sync_bitmap) {
91 backup_cleanup_sync_bitmap(s, 0);
92 }
93}
94
Kevin Wolf4ad35182018-04-19 17:30:16 +020095static void backup_abort(Job *job)
John Snowc347b2c2015-11-05 18:13:16 -050096{
Kevin Wolf4ad35182018-04-19 17:30:16 +020097 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
John Snowc347b2c2015-11-05 18:13:16 -050098 if (s->sync_bitmap) {
99 backup_cleanup_sync_bitmap(s, -1);
100 }
101}
102
Kevin Wolf4ad35182018-04-19 17:30:16 +0200103static void backup_clean(Job *job)
John Snowe8a40bf2016-11-08 01:50:35 -0500104{
Kevin Wolf4ad35182018-04-19 17:30:16 +0200105 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
Max Reitzbdc4c4c2021-02-19 16:33:46 +0100106 block_job_remove_all_bdrv(&s->common);
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +0300107 bdrv_backup_top_drop(s->backup_top);
John Snowe8a40bf2016-11-08 01:50:35 -0500108}
109
Wen Congyang49d3e822016-07-27 15:01:43 +0800110void backup_do_checkpoint(BlockJob *job, Error **errp)
111{
112 BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
Wen Congyang49d3e822016-07-27 15:01:43 +0800113
Kevin Wolfbd219352018-01-19 15:54:40 +0100114 assert(block_job_driver(job) == &backup_job_driver);
Wen Congyang49d3e822016-07-27 15:01:43 +0800115
116 if (backup_job->sync_mode != MIRROR_SYNC_MODE_NONE) {
117 error_setg(errp, "The backup job only supports block checkpoint in"
118 " sync=none mode");
119 return;
120 }
121
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +0300122 bdrv_set_dirty_bitmap(block_copy_dirty_bitmap(backup_job->bcs), 0,
123 backup_job->len);
Wen Congyang49d3e822016-07-27 15:01:43 +0800124}
125
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200126static BlockErrorAction backup_error_action(BackupBlockJob *job,
127 bool read, int error)
128{
129 if (read) {
Kevin Wolf81e254d2016-04-18 11:36:38 +0200130 return block_job_error_action(&job->common, job->on_source_error,
131 true, error);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200132 } else {
Kevin Wolf81e254d2016-04-18 11:36:38 +0200133 return block_job_error_action(&job->common, job->on_target_error,
134 false, error);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200135 }
136}
137
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300138static void coroutine_fn backup_block_copy_callback(void *opaque)
John Snowd58d8452015-04-17 19:49:58 -0400139{
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300140 BackupBlockJob *s = opaque;
Kevin Wolfdee81d52018-01-18 21:19:38 +0100141
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300142 if (s->wait) {
143 s->wait = false;
144 aio_co_wake(s->common.job.co);
145 } else {
146 job_enter(&s->common.job);
John Snowd58d8452015-04-17 19:49:58 -0400147 }
John Snowd58d8452015-04-17 19:49:58 -0400148}
149
Vladimir Sementsov-Ogievskiyc334e892019-04-29 12:08:41 +0300150static int coroutine_fn backup_loop(BackupBlockJob *job)
John Snowd58d8452015-04-17 19:49:58 -0400151{
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300152 BlockCopyCallState *s = NULL;
John Snow62aa1fb2019-07-29 16:35:53 -0400153 int ret = 0;
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300154 bool error_is_read;
155 BlockErrorAction act;
John Snowd58d8452015-04-17 19:49:58 -0400156
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300157 while (true) { /* retry loop */
158 job->bg_bcs_call = s = block_copy_async(job->bcs, 0,
159 QEMU_ALIGN_UP(job->len, job->cluster_size),
160 job->perf.max_workers, job->perf.max_chunk,
161 backup_block_copy_callback, job);
162
163 while (!block_copy_call_finished(s) &&
164 !job_is_cancelled(&job->common.job))
165 {
166 job_yield(&job->common.job);
167 }
168
169 if (!block_copy_call_finished(s)) {
170 assert(job_is_cancelled(&job->common.job));
171 /*
172 * Note that we can't use job_yield() here, as it doesn't work for
173 * cancelled job.
174 */
175 block_copy_call_cancel(s);
176 job->wait = true;
177 qemu_coroutine_yield();
178 assert(block_copy_call_finished(s));
179 ret = 0;
180 goto out;
181 }
182
183 if (job_is_cancelled(&job->common.job) ||
184 block_copy_call_succeeded(s))
185 {
186 ret = 0;
187 goto out;
188 }
189
190 if (block_copy_call_cancelled(s)) {
191 /*
192 * Job is not cancelled but only block-copy call. This is possible
193 * after job pause. Now the pause is finished, start new block-copy
194 * iteration.
195 */
196 block_copy_call_free(s);
197 continue;
198 }
199
200 /* The only remaining case is failed block-copy call. */
201 assert(block_copy_call_failed(s));
202
203 ret = block_copy_call_status(s, &error_is_read);
204 act = backup_error_action(job, error_is_read, -ret);
205 switch (act) {
206 case BLOCK_ERROR_ACTION_REPORT:
207 goto out;
208 case BLOCK_ERROR_ACTION_STOP:
209 /*
210 * Go to pause prior to starting new block-copy call on the next
211 * iteration.
212 */
213 job_pause_point(&job->common.job);
214 break;
215 case BLOCK_ERROR_ACTION_IGNORE:
216 /* Proceed to new block-copy call to retry. */
217 break;
218 default:
219 abort();
220 }
221
222 block_copy_call_free(s);
John Snowd58d8452015-04-17 19:49:58 -0400223 }
224
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300225out:
226 block_copy_call_free(s);
227 job->bg_bcs_call = NULL;
John Snow62aa1fb2019-07-29 16:35:53 -0400228 return ret;
John Snowd58d8452015-04-17 19:49:58 -0400229}
230
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +0300231static void backup_init_bcs_bitmap(BackupBlockJob *job)
Vladimir Sementsov-Ogievskiy8cc6dc62017-10-12 16:53:11 +0300232{
John Snow141cdcd2019-07-29 16:35:55 -0400233 bool ret;
234 uint64_t estimate;
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +0300235 BdrvDirtyBitmap *bcs_bitmap = block_copy_dirty_bitmap(job->bcs);
Vladimir Sementsov-Ogievskiy8cc6dc62017-10-12 16:53:11 +0300236
John Snow141cdcd2019-07-29 16:35:55 -0400237 if (job->sync_mode == MIRROR_SYNC_MODE_BITMAP) {
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +0300238 ret = bdrv_dirty_bitmap_merge_internal(bcs_bitmap, job->sync_bitmap,
John Snow141cdcd2019-07-29 16:35:55 -0400239 NULL, true);
240 assert(ret);
241 } else {
John Snow7e30dd62019-07-29 16:35:55 -0400242 if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
243 /*
244 * We can't hog the coroutine to initialize this thoroughly.
245 * Set a flag and resume work when we are able to yield safely.
246 */
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +0300247 block_copy_set_skip_unallocated(job->bcs, true);
John Snow7e30dd62019-07-29 16:35:55 -0400248 }
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +0300249 bdrv_set_dirty_bitmap(bcs_bitmap, 0, job->len);
John Snow141cdcd2019-07-29 16:35:55 -0400250 }
251
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +0300252 estimate = bdrv_get_dirty_count(bcs_bitmap);
John Snow141cdcd2019-07-29 16:35:55 -0400253 job_progress_set_remaining(&job->common.job, estimate);
Vladimir Sementsov-Ogievskiy8cc6dc62017-10-12 16:53:11 +0300254}
255
John Snow68702772018-08-29 21:57:32 -0400256static int coroutine_fn backup_run(Job *job, Error **errp)
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200257{
John Snow68702772018-08-29 21:57:32 -0400258 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
Vladimir Sementsov-Ogievskiy511e7d32021-01-17 00:46:58 +0300259 int ret;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200260
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +0300261 backup_init_bcs_bitmap(s);
Vladimir Sementsov-Ogievskiy8cc6dc62017-10-12 16:53:11 +0300262
John Snow7e30dd62019-07-29 16:35:55 -0400263 if (s->sync_mode == MIRROR_SYNC_MODE_TOP) {
264 int64_t offset = 0;
265 int64_t count;
266
267 for (offset = 0; offset < s->len; ) {
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300268 if (job_is_cancelled(job)) {
269 return -ECANCELED;
270 }
271
272 job_pause_point(job);
273
274 if (job_is_cancelled(job)) {
Vladimir Sementsov-Ogievskiy511e7d32021-01-17 00:46:58 +0300275 return -ECANCELED;
John Snow7e30dd62019-07-29 16:35:55 -0400276 }
277
Vladimir Sementsov-Ogievskiy2c8074c2019-09-20 17:20:46 +0300278 ret = block_copy_reset_unallocated(s->bcs, offset, &count);
John Snow7e30dd62019-07-29 16:35:55 -0400279 if (ret < 0) {
Vladimir Sementsov-Ogievskiy511e7d32021-01-17 00:46:58 +0300280 return ret;
John Snow7e30dd62019-07-29 16:35:55 -0400281 }
282
283 offset += count;
284 }
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +0300285 block_copy_set_skip_unallocated(s->bcs, false);
John Snow7e30dd62019-07-29 16:35:55 -0400286 }
287
John Snow68702772018-08-29 21:57:32 -0400288 if (s->sync_mode == MIRROR_SYNC_MODE_NONE) {
Vladimir Sementsov-Ogievskiy0e23e382019-09-20 17:20:47 +0300289 /*
Vladimir Sementsov-Ogievskiy397f4e92020-03-11 13:30:04 +0300290 * All bits are set in bcs bitmap to allow any cluster to be copied.
Vladimir Sementsov-Ogievskiy0e23e382019-09-20 17:20:47 +0300291 * This does not actually require them to be copied.
292 */
John Snow68702772018-08-29 21:57:32 -0400293 while (!job_is_cancelled(job)) {
Vladimir Sementsov-Ogievskiy0e23e382019-09-20 17:20:47 +0300294 /*
295 * Yield until the job is cancelled. We just let our before_write
296 * notify callback service CoW requests.
297 */
John Snow68702772018-08-29 21:57:32 -0400298 job_yield(job);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200299 }
Ian Mainfc5d3f82013-07-26 11:39:04 -0700300 } else {
Vladimir Sementsov-Ogievskiy511e7d32021-01-17 00:46:58 +0300301 return backup_loop(s);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200302 }
303
Vladimir Sementsov-Ogievskiy511e7d32021-01-17 00:46:58 +0300304 return 0;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200305}
306
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300307static void coroutine_fn backup_pause(Job *job)
308{
309 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
310
311 if (s->bg_bcs_call && !block_copy_call_finished(s->bg_bcs_call)) {
312 block_copy_call_cancel(s->bg_bcs_call);
313 s->wait = true;
314 qemu_coroutine_yield();
315 }
316}
317
318static void coroutine_fn backup_set_speed(BlockJob *job, int64_t speed)
319{
320 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
321
322 /*
323 * block_job_set_speed() is called first from block_job_create(), when we
324 * don't yet have s->bcs.
325 */
326 if (s->bcs) {
327 block_copy_set_speed(s->bcs, speed);
328 if (s->bg_bcs_call) {
329 block_copy_kick(s->bg_bcs_call);
330 }
331 }
332}
333
Vladimir Sementsov-Ogievskiy9c785cd2021-04-21 10:58:58 +0300334static void backup_cancel(Job *job, bool force)
Vladimir Sementsov-Ogievskiyff789bf2021-02-05 19:37:19 +0300335{
336 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
337
338 bdrv_cancel_in_flight(s->target_bs);
339}
340
John Snowa7815a72016-11-08 01:50:36 -0500341static const BlockJobDriver backup_job_driver = {
Kevin Wolf33e9e9b2018-04-12 17:29:59 +0200342 .job_driver = {
343 .instance_size = sizeof(BackupBlockJob),
Kevin Wolf252291e2018-04-12 17:57:08 +0200344 .job_type = JOB_TYPE_BACKUP,
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200345 .free = block_job_free,
Kevin Wolfb15de822018-04-18 17:10:26 +0200346 .user_resume = block_job_user_resume,
John Snowf67432a2018-08-29 21:57:26 -0400347 .run = backup_run,
Kevin Wolf4ad35182018-04-19 17:30:16 +0200348 .commit = backup_commit,
349 .abort = backup_abort,
350 .clean = backup_clean,
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300351 .pause = backup_pause,
Vladimir Sementsov-Ogievskiyff789bf2021-02-05 19:37:19 +0300352 .cancel = backup_cancel,
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300353 },
354 .set_speed = backup_set_speed,
John Snowa7815a72016-11-08 01:50:36 -0500355};
356
Vladimir Sementsov-Ogievskiyae6b12f2019-04-29 12:08:42 +0300357static int64_t backup_calculate_cluster_size(BlockDriverState *target,
358 Error **errp)
359{
360 int ret;
361 BlockDriverInfo bdi;
Max Reitz2b088c62019-06-12 17:46:45 +0200362 bool target_does_cow = bdrv_backing_chain_next(target);
Vladimir Sementsov-Ogievskiyae6b12f2019-04-29 12:08:42 +0300363
364 /*
365 * If there is no backing file on the target, we cannot rely on COW if our
366 * backup cluster size is smaller than the target cluster size. Even for
367 * targets with a backing file, try to avoid COW if possible.
368 */
369 ret = bdrv_get_info(target, &bdi);
Max Reitz2b088c62019-06-12 17:46:45 +0200370 if (ret == -ENOTSUP && !target_does_cow) {
Vladimir Sementsov-Ogievskiyae6b12f2019-04-29 12:08:42 +0300371 /* Cluster size is not defined */
372 warn_report("The target block device doesn't provide "
373 "information about the block size and it doesn't have a "
374 "backing file. The default block size of %u bytes is "
375 "used. If the actual block size of the target exceeds "
376 "this default, the backup may be unusable",
377 BACKUP_CLUSTER_SIZE_DEFAULT);
378 return BACKUP_CLUSTER_SIZE_DEFAULT;
Max Reitz2b088c62019-06-12 17:46:45 +0200379 } else if (ret < 0 && !target_does_cow) {
Vladimir Sementsov-Ogievskiyae6b12f2019-04-29 12:08:42 +0300380 error_setg_errno(errp, -ret,
381 "Couldn't determine the cluster size of the target image, "
382 "which has no backing file");
383 error_append_hint(errp,
384 "Aborting, since this may create an unusable destination image\n");
385 return ret;
Max Reitz2b088c62019-06-12 17:46:45 +0200386 } else if (ret < 0 && target_does_cow) {
Vladimir Sementsov-Ogievskiyae6b12f2019-04-29 12:08:42 +0300387 /* Not fatal; just trudge on ahead. */
388 return BACKUP_CLUSTER_SIZE_DEFAULT;
389 }
390
391 return MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
392}
393
John Snow111049a2016-11-08 01:50:38 -0500394BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
Alberto Garcia70559d42016-07-05 17:28:58 +0300395 BlockDriverState *target, int64_t speed,
396 MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap,
John Snowc8b56502019-07-29 16:35:52 -0400397 BitmapSyncMode bitmap_mode,
Pavel Butsykin13b94142016-07-22 11:17:52 +0300398 bool compress,
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +0300399 const char *filter_node_name,
Vladimir Sementsov-Ogievskiy86c6a3b2021-01-17 00:46:43 +0300400 BackupPerf *perf,
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200401 BlockdevOnError on_source_error,
402 BlockdevOnError on_target_error,
John Snow47970df2016-10-27 12:06:57 -0400403 int creation_flags,
Markus Armbruster097310b2014-10-07 13:59:15 +0200404 BlockCompletionFunc *cb, void *opaque,
Kevin Wolf62c9e412018-04-19 16:09:52 +0200405 JobTxn *txn, Error **errp)
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200406{
Kevin Wolf958a04b2020-04-30 16:27:54 +0200407 int64_t len, target_len;
Kevin Wolf91ab6882016-04-14 12:59:55 +0200408 BackupBlockJob *job = NULL;
Vladimir Sementsov-Ogievskiyae6b12f2019-04-29 12:08:42 +0300409 int64_t cluster_size;
Vladimir Sementsov-Ogievskiy2c8074c2019-09-20 17:20:46 +0300410 BdrvRequestFlags write_flags;
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +0300411 BlockDriverState *backup_top = NULL;
412 BlockCopyState *bcs = NULL;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200413
414 assert(bs);
415 assert(target);
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200416
John Snowa6c93652019-07-29 16:35:55 -0400417 /* QMP interface protects us from these cases */
418 assert(sync_mode != MIRROR_SYNC_MODE_INCREMENTAL);
419 assert(sync_bitmap || sync_mode != MIRROR_SYNC_MODE_BITMAP);
420
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800421 if (bs == target) {
422 error_setg(errp, "Source and target cannot be the same");
John Snow111049a2016-11-08 01:50:38 -0500423 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800424 }
425
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800426 if (!bdrv_is_inserted(bs)) {
427 error_setg(errp, "Device is not inserted: %s",
428 bdrv_get_device_name(bs));
John Snow111049a2016-11-08 01:50:38 -0500429 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800430 }
431
432 if (!bdrv_is_inserted(target)) {
433 error_setg(errp, "Device is not inserted: %s",
434 bdrv_get_device_name(target));
John Snow111049a2016-11-08 01:50:38 -0500435 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800436 }
437
Max Reitz2b088c62019-06-12 17:46:45 +0200438 if (compress && !bdrv_supports_compressed_writes(target)) {
Pavel Butsykin13b94142016-07-22 11:17:52 +0300439 error_setg(errp, "Compression is not supported for this drive %s",
440 bdrv_get_device_name(target));
John Snow111049a2016-11-08 01:50:38 -0500441 return NULL;
Pavel Butsykin13b94142016-07-22 11:17:52 +0300442 }
443
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800444 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
John Snow111049a2016-11-08 01:50:38 -0500445 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800446 }
447
448 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
John Snow111049a2016-11-08 01:50:38 -0500449 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +0800450 }
451
Vladimir Sementsov-Ogievskiy2c59fd82021-01-17 00:46:52 +0300452 cluster_size = backup_calculate_cluster_size(target, errp);
453 if (cluster_size < 0) {
454 goto error;
455 }
456
457 if (perf->max_workers < 1) {
458 error_setg(errp, "max-workers must be greater than zero");
459 return NULL;
460 }
461
462 if (perf->max_chunk < 0) {
463 error_setg(errp, "max-chunk must be zero (which means no limit) or "
464 "positive");
465 return NULL;
466 }
467
468 if (perf->max_chunk && perf->max_chunk < cluster_size) {
469 error_setg(errp, "Required max-chunk (%" PRIi64 ") is less than backup "
470 "cluster size (%" PRIi64 ")", perf->max_chunk, cluster_size);
471 return NULL;
472 }
473
474
John Snow1a2b8b42019-07-29 16:35:55 -0400475 if (sync_bitmap) {
John Snowb30ffbe2019-07-29 16:35:54 -0400476 /* If we need to write to this bitmap, check that we can: */
477 if (bitmap_mode != BITMAP_SYNC_MODE_NEVER &&
478 bdrv_dirty_bitmap_check(sync_bitmap, BDRV_BITMAP_DEFAULT, errp)) {
479 return NULL;
480 }
481
John Snowd58d8452015-04-17 19:49:58 -0400482 /* Create a new bitmap, and freeze/disable this one. */
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +0300483 if (bdrv_dirty_bitmap_create_successor(sync_bitmap, errp) < 0) {
John Snow111049a2016-11-08 01:50:38 -0500484 return NULL;
John Snowd58d8452015-04-17 19:49:58 -0400485 }
John Snowd58d8452015-04-17 19:49:58 -0400486 }
487
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200488 len = bdrv_getlength(bs);
489 if (len < 0) {
Kevin Wolf58226632020-04-30 16:27:53 +0200490 error_setg_errno(errp, -len, "Unable to get length for '%s'",
491 bdrv_get_device_or_node_name(bs));
John Snowd58d8452015-04-17 19:49:58 -0400492 goto error;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200493 }
494
Kevin Wolf958a04b2020-04-30 16:27:54 +0200495 target_len = bdrv_getlength(target);
496 if (target_len < 0) {
497 error_setg_errno(errp, -target_len, "Unable to get length for '%s'",
498 bdrv_get_device_or_node_name(bs));
499 goto error;
500 }
501
502 if (target_len != len) {
503 error_setg(errp, "Source and target image have different sizes");
504 goto error;
505 }
506
Vladimir Sementsov-Ogievskiya1ed82b2019-07-30 19:32:51 +0300507 /*
Vladimir Sementsov-Ogievskiy372c67e2019-09-20 17:20:45 +0300508 * If source is in backing chain of target assume that target is going to be
509 * used for "image fleecing", i.e. it should represent a kind of snapshot of
510 * source at backup-start point in time. And target is going to be read by
511 * somebody (for example, used as NBD export) during backup job.
512 *
513 * In this case, we need to add BDRV_REQ_SERIALISING write flag to avoid
514 * intersection of backup writes and third party reads from target,
515 * otherwise reading from target we may occasionally read already updated by
516 * guest data.
517 *
518 * For more information see commit f8d59dfb40bb and test
519 * tests/qemu-iotests/222
Vladimir Sementsov-Ogievskiya1ed82b2019-07-30 19:32:51 +0300520 */
Vladimir Sementsov-Ogievskiy2c8074c2019-09-20 17:20:46 +0300521 write_flags = (bdrv_chain_contains(target, bs) ? BDRV_REQ_SERIALISING : 0) |
522 (compress ? BDRV_REQ_WRITE_COMPRESSED : 0),
523
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +0300524 backup_top = bdrv_backup_top_append(bs, target, filter_node_name,
Vladimir Sementsov-Ogievskiy86c6a3b2021-01-17 00:46:43 +0300525 cluster_size, perf,
526 write_flags, &bcs, errp);
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +0300527 if (!backup_top) {
528 goto error;
529 }
530
Vladimir Sementsov-Ogievskiy843670f2019-10-01 16:14:06 +0300531 /* job->len is fixed, so we can't allow resize */
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +0300532 job = block_job_create(job_id, &backup_job_driver, txn, backup_top,
533 0, BLK_PERM_ALL,
Vladimir Sementsov-Ogievskiy843670f2019-10-01 16:14:06 +0300534 speed, creation_flags, cb, opaque, errp);
535 if (!job) {
536 goto error;
537 }
538
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +0300539 job->backup_top = backup_top;
Vladimir Sementsov-Ogievskiy843670f2019-10-01 16:14:06 +0300540 job->source_bs = bs;
Vladimir Sementsov-Ogievskiyff789bf2021-02-05 19:37:19 +0300541 job->target_bs = target;
Vladimir Sementsov-Ogievskiy843670f2019-10-01 16:14:06 +0300542 job->on_source_error = on_source_error;
543 job->on_target_error = on_target_error;
544 job->sync_mode = sync_mode;
545 job->sync_bitmap = sync_bitmap;
546 job->bitmap_mode = bitmap_mode;
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +0300547 job->bcs = bcs;
Vladimir Sementsov-Ogievskiyae6b12f2019-04-29 12:08:42 +0300548 job->cluster_size = cluster_size;
Vladimir Sementsov-Ogievskiy843670f2019-10-01 16:14:06 +0300549 job->len = len;
Vladimir Sementsov-Ogievskiy86c6a3b2021-01-17 00:46:43 +0300550 job->perf = *perf;
John Snow4c9bca72016-02-25 15:58:30 -0500551
Vladimir Sementsov-Ogievskiyd0ebeca2020-03-11 13:29:57 +0300552 block_copy_set_progress_meter(bcs, &job->common.job.progress);
Vladimir Sementsov-Ogievskiy71eed4c2021-01-17 00:46:59 +0300553 block_copy_set_speed(bcs, speed);
Vladimir Sementsov-Ogievskiy0f4b02b2019-10-01 16:14:07 +0300554
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +0300555 /* Required permissions are already taken by backup-top target */
Kevin Wolf76d554e2017-01-17 11:56:42 +0100556 block_job_add_bdrv(&job->common, "target", target, 0, BLK_PERM_ALL,
557 &error_abort);
John Snow111049a2016-11-08 01:50:38 -0500558
559 return &job->common;
John Snowd58d8452015-04-17 19:49:58 -0400560
561 error:
562 if (sync_bitmap) {
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +0300563 bdrv_reclaim_dirty_bitmap(sync_bitmap, NULL);
John Snowd58d8452015-04-17 19:49:58 -0400564 }
Vladimir Sementsov-Ogievskiy8ccf4582019-10-17 17:21:22 +0300565 if (backup_top) {
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +0300566 bdrv_backup_top_drop(backup_top);
Kevin Wolf91ab6882016-04-14 12:59:55 +0200567 }
John Snow111049a2016-11-08 01:50:38 -0500568
569 return NULL;
Dietmar Maurer98d2c6f2013-06-24 17:13:11 +0200570}