OSDN Git Service

block/backup: drop extra gotos from backup_run()
[qmiga/qemu.git] / block / backup.c
1 /*
2  * QEMU backup
3  *
4  * Copyright (C) 2013 Proxmox Server Solutions
5  * Copyright (c) 2019 Virtuozzo International GmbH.
6  *
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
15 #include "qemu/osdep.h"
16
17 #include "trace.h"
18 #include "block/block.h"
19 #include "block/block_int.h"
20 #include "block/blockjob_int.h"
21 #include "block/block_backup.h"
22 #include "block/block-copy.h"
23 #include "qapi/error.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qemu/ratelimit.h"
26 #include "qemu/cutils.h"
27 #include "sysemu/block-backend.h"
28 #include "qemu/bitmap.h"
29 #include "qemu/error-report.h"
30
31 #include "block/backup-top.h"
32
33 #define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
34
35 typedef struct BackupBlockJob {
36     BlockJob common;
37     BlockDriverState *backup_top;
38     BlockDriverState *source_bs;
39
40     BdrvDirtyBitmap *sync_bitmap;
41
42     MirrorSyncMode sync_mode;
43     BitmapSyncMode bitmap_mode;
44     BlockdevOnError on_source_error;
45     BlockdevOnError on_target_error;
46     uint64_t len;
47     uint64_t bytes_read;
48     int64_t cluster_size;
49     BackupPerf perf;
50
51     BlockCopyState *bcs;
52 } BackupBlockJob;
53
54 static const BlockJobDriver backup_job_driver;
55
56 static void backup_progress_bytes_callback(int64_t bytes, void *opaque)
57 {
58     BackupBlockJob *s = opaque;
59
60     s->bytes_read += bytes;
61 }
62
63 static int coroutine_fn backup_do_cow(BackupBlockJob *job,
64                                       int64_t offset, uint64_t bytes,
65                                       bool *error_is_read)
66 {
67     int ret = 0;
68     int64_t start, end; /* bytes */
69
70     start = QEMU_ALIGN_DOWN(offset, job->cluster_size);
71     end = QEMU_ALIGN_UP(bytes + offset, job->cluster_size);
72
73     trace_backup_do_cow_enter(job, start, offset, bytes);
74
75     ret = block_copy(job->bcs, start, end - start, true, error_is_read);
76
77     trace_backup_do_cow_return(job, offset, bytes, ret);
78
79     return ret;
80 }
81
82 static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
83 {
84     BdrvDirtyBitmap *bm;
85     bool sync = (((ret == 0) || (job->bitmap_mode == BITMAP_SYNC_MODE_ALWAYS)) \
86                  && (job->bitmap_mode != BITMAP_SYNC_MODE_NEVER));
87
88     if (sync) {
89         /*
90          * We succeeded, or we always intended to sync the bitmap.
91          * Delete this bitmap and install the child.
92          */
93         bm = bdrv_dirty_bitmap_abdicate(job->sync_bitmap, NULL);
94     } else {
95         /*
96          * We failed, or we never intended to sync the bitmap anyway.
97          * Merge the successor back into the parent, keeping all data.
98          */
99         bm = bdrv_reclaim_dirty_bitmap(job->sync_bitmap, NULL);
100     }
101
102     assert(bm);
103
104     if (ret < 0 && job->bitmap_mode == BITMAP_SYNC_MODE_ALWAYS) {
105         /* If we failed and synced, merge in the bits we didn't copy: */
106         bdrv_dirty_bitmap_merge_internal(bm, block_copy_dirty_bitmap(job->bcs),
107                                          NULL, true);
108     }
109 }
110
111 static void backup_commit(Job *job)
112 {
113     BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
114     if (s->sync_bitmap) {
115         backup_cleanup_sync_bitmap(s, 0);
116     }
117 }
118
119 static void backup_abort(Job *job)
120 {
121     BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
122     if (s->sync_bitmap) {
123         backup_cleanup_sync_bitmap(s, -1);
124     }
125 }
126
127 static void backup_clean(Job *job)
128 {
129     BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
130     bdrv_backup_top_drop(s->backup_top);
131 }
132
133 void backup_do_checkpoint(BlockJob *job, Error **errp)
134 {
135     BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
136
137     assert(block_job_driver(job) == &backup_job_driver);
138
139     if (backup_job->sync_mode != MIRROR_SYNC_MODE_NONE) {
140         error_setg(errp, "The backup job only supports block checkpoint in"
141                    " sync=none mode");
142         return;
143     }
144
145     bdrv_set_dirty_bitmap(block_copy_dirty_bitmap(backup_job->bcs), 0,
146                           backup_job->len);
147 }
148
149 static BlockErrorAction backup_error_action(BackupBlockJob *job,
150                                             bool read, int error)
151 {
152     if (read) {
153         return block_job_error_action(&job->common, job->on_source_error,
154                                       true, error);
155     } else {
156         return block_job_error_action(&job->common, job->on_target_error,
157                                       false, error);
158     }
159 }
160
161 static bool coroutine_fn yield_and_check(BackupBlockJob *job)
162 {
163     uint64_t delay_ns;
164
165     if (job_is_cancelled(&job->common.job)) {
166         return true;
167     }
168
169     /*
170      * We need to yield even for delay_ns = 0 so that bdrv_drain_all() can
171      * return. Without a yield, the VM would not reboot.
172      */
173     delay_ns = block_job_ratelimit_get_delay(&job->common, job->bytes_read);
174     job->bytes_read = 0;
175     job_sleep_ns(&job->common.job, delay_ns);
176
177     if (job_is_cancelled(&job->common.job)) {
178         return true;
179     }
180
181     return false;
182 }
183
184 static int coroutine_fn backup_loop(BackupBlockJob *job)
185 {
186     bool error_is_read;
187     int64_t offset;
188     BdrvDirtyBitmapIter *bdbi;
189     int ret = 0;
190
191     bdbi = bdrv_dirty_iter_new(block_copy_dirty_bitmap(job->bcs));
192     while ((offset = bdrv_dirty_iter_next(bdbi)) != -1) {
193         do {
194             if (yield_and_check(job)) {
195                 goto out;
196             }
197             ret = backup_do_cow(job, offset, job->cluster_size, &error_is_read);
198             if (ret < 0 && backup_error_action(job, error_is_read, -ret) ==
199                            BLOCK_ERROR_ACTION_REPORT)
200             {
201                 goto out;
202             }
203         } while (ret < 0);
204     }
205
206  out:
207     bdrv_dirty_iter_free(bdbi);
208     return ret;
209 }
210
211 static void backup_init_bcs_bitmap(BackupBlockJob *job)
212 {
213     bool ret;
214     uint64_t estimate;
215     BdrvDirtyBitmap *bcs_bitmap = block_copy_dirty_bitmap(job->bcs);
216
217     if (job->sync_mode == MIRROR_SYNC_MODE_BITMAP) {
218         ret = bdrv_dirty_bitmap_merge_internal(bcs_bitmap, job->sync_bitmap,
219                                                NULL, true);
220         assert(ret);
221     } else {
222         if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
223             /*
224              * We can't hog the coroutine to initialize this thoroughly.
225              * Set a flag and resume work when we are able to yield safely.
226              */
227             block_copy_set_skip_unallocated(job->bcs, true);
228         }
229         bdrv_set_dirty_bitmap(bcs_bitmap, 0, job->len);
230     }
231
232     estimate = bdrv_get_dirty_count(bcs_bitmap);
233     job_progress_set_remaining(&job->common.job, estimate);
234 }
235
236 static int coroutine_fn backup_run(Job *job, Error **errp)
237 {
238     BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
239     int ret;
240
241     backup_init_bcs_bitmap(s);
242
243     if (s->sync_mode == MIRROR_SYNC_MODE_TOP) {
244         int64_t offset = 0;
245         int64_t count;
246
247         for (offset = 0; offset < s->len; ) {
248             if (yield_and_check(s)) {
249                 return -ECANCELED;
250             }
251
252             ret = block_copy_reset_unallocated(s->bcs, offset, &count);
253             if (ret < 0) {
254                 return ret;
255             }
256
257             offset += count;
258         }
259         block_copy_set_skip_unallocated(s->bcs, false);
260     }
261
262     if (s->sync_mode == MIRROR_SYNC_MODE_NONE) {
263         /*
264          * All bits are set in bcs bitmap to allow any cluster to be copied.
265          * This does not actually require them to be copied.
266          */
267         while (!job_is_cancelled(job)) {
268             /*
269              * Yield until the job is cancelled.  We just let our before_write
270              * notify callback service CoW requests.
271              */
272             job_yield(job);
273         }
274     } else {
275         return backup_loop(s);
276     }
277
278     return 0;
279 }
280
281 static const BlockJobDriver backup_job_driver = {
282     .job_driver = {
283         .instance_size          = sizeof(BackupBlockJob),
284         .job_type               = JOB_TYPE_BACKUP,
285         .free                   = block_job_free,
286         .user_resume            = block_job_user_resume,
287         .run                    = backup_run,
288         .commit                 = backup_commit,
289         .abort                  = backup_abort,
290         .clean                  = backup_clean,
291     }
292 };
293
294 static int64_t backup_calculate_cluster_size(BlockDriverState *target,
295                                              Error **errp)
296 {
297     int ret;
298     BlockDriverInfo bdi;
299     bool target_does_cow = bdrv_backing_chain_next(target);
300
301     /*
302      * If there is no backing file on the target, we cannot rely on COW if our
303      * backup cluster size is smaller than the target cluster size. Even for
304      * targets with a backing file, try to avoid COW if possible.
305      */
306     ret = bdrv_get_info(target, &bdi);
307     if (ret == -ENOTSUP && !target_does_cow) {
308         /* Cluster size is not defined */
309         warn_report("The target block device doesn't provide "
310                     "information about the block size and it doesn't have a "
311                     "backing file. The default block size of %u bytes is "
312                     "used. If the actual block size of the target exceeds "
313                     "this default, the backup may be unusable",
314                     BACKUP_CLUSTER_SIZE_DEFAULT);
315         return BACKUP_CLUSTER_SIZE_DEFAULT;
316     } else if (ret < 0 && !target_does_cow) {
317         error_setg_errno(errp, -ret,
318             "Couldn't determine the cluster size of the target image, "
319             "which has no backing file");
320         error_append_hint(errp,
321             "Aborting, since this may create an unusable destination image\n");
322         return ret;
323     } else if (ret < 0 && target_does_cow) {
324         /* Not fatal; just trudge on ahead. */
325         return BACKUP_CLUSTER_SIZE_DEFAULT;
326     }
327
328     return MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
329 }
330
331 BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
332                   BlockDriverState *target, int64_t speed,
333                   MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap,
334                   BitmapSyncMode bitmap_mode,
335                   bool compress,
336                   const char *filter_node_name,
337                   BackupPerf *perf,
338                   BlockdevOnError on_source_error,
339                   BlockdevOnError on_target_error,
340                   int creation_flags,
341                   BlockCompletionFunc *cb, void *opaque,
342                   JobTxn *txn, Error **errp)
343 {
344     int64_t len, target_len;
345     BackupBlockJob *job = NULL;
346     int64_t cluster_size;
347     BdrvRequestFlags write_flags;
348     BlockDriverState *backup_top = NULL;
349     BlockCopyState *bcs = NULL;
350
351     assert(bs);
352     assert(target);
353
354     /* QMP interface protects us from these cases */
355     assert(sync_mode != MIRROR_SYNC_MODE_INCREMENTAL);
356     assert(sync_bitmap || sync_mode != MIRROR_SYNC_MODE_BITMAP);
357
358     if (bs == target) {
359         error_setg(errp, "Source and target cannot be the same");
360         return NULL;
361     }
362
363     if (!bdrv_is_inserted(bs)) {
364         error_setg(errp, "Device is not inserted: %s",
365                    bdrv_get_device_name(bs));
366         return NULL;
367     }
368
369     if (!bdrv_is_inserted(target)) {
370         error_setg(errp, "Device is not inserted: %s",
371                    bdrv_get_device_name(target));
372         return NULL;
373     }
374
375     if (compress && !bdrv_supports_compressed_writes(target)) {
376         error_setg(errp, "Compression is not supported for this drive %s",
377                    bdrv_get_device_name(target));
378         return NULL;
379     }
380
381     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
382         return NULL;
383     }
384
385     if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
386         return NULL;
387     }
388
389     cluster_size = backup_calculate_cluster_size(target, errp);
390     if (cluster_size < 0) {
391         goto error;
392     }
393
394     if (perf->max_workers < 1) {
395         error_setg(errp, "max-workers must be greater than zero");
396         return NULL;
397     }
398
399     if (perf->max_chunk < 0) {
400         error_setg(errp, "max-chunk must be zero (which means no limit) or "
401                    "positive");
402         return NULL;
403     }
404
405     if (perf->max_chunk && perf->max_chunk < cluster_size) {
406         error_setg(errp, "Required max-chunk (%" PRIi64 ") is less than backup "
407                    "cluster size (%" PRIi64 ")", perf->max_chunk, cluster_size);
408         return NULL;
409     }
410
411
412     if (sync_bitmap) {
413         /* If we need to write to this bitmap, check that we can: */
414         if (bitmap_mode != BITMAP_SYNC_MODE_NEVER &&
415             bdrv_dirty_bitmap_check(sync_bitmap, BDRV_BITMAP_DEFAULT, errp)) {
416             return NULL;
417         }
418
419         /* Create a new bitmap, and freeze/disable this one. */
420         if (bdrv_dirty_bitmap_create_successor(sync_bitmap, errp) < 0) {
421             return NULL;
422         }
423     }
424
425     len = bdrv_getlength(bs);
426     if (len < 0) {
427         error_setg_errno(errp, -len, "Unable to get length for '%s'",
428                          bdrv_get_device_or_node_name(bs));
429         goto error;
430     }
431
432     target_len = bdrv_getlength(target);
433     if (target_len < 0) {
434         error_setg_errno(errp, -target_len, "Unable to get length for '%s'",
435                          bdrv_get_device_or_node_name(bs));
436         goto error;
437     }
438
439     if (target_len != len) {
440         error_setg(errp, "Source and target image have different sizes");
441         goto error;
442     }
443
444     /*
445      * If source is in backing chain of target assume that target is going to be
446      * used for "image fleecing", i.e. it should represent a kind of snapshot of
447      * source at backup-start point in time. And target is going to be read by
448      * somebody (for example, used as NBD export) during backup job.
449      *
450      * In this case, we need to add BDRV_REQ_SERIALISING write flag to avoid
451      * intersection of backup writes and third party reads from target,
452      * otherwise reading from target we may occasionally read already updated by
453      * guest data.
454      *
455      * For more information see commit f8d59dfb40bb and test
456      * tests/qemu-iotests/222
457      */
458     write_flags = (bdrv_chain_contains(target, bs) ? BDRV_REQ_SERIALISING : 0) |
459                   (compress ? BDRV_REQ_WRITE_COMPRESSED : 0),
460
461     backup_top = bdrv_backup_top_append(bs, target, filter_node_name,
462                                         cluster_size, perf,
463                                         write_flags, &bcs, errp);
464     if (!backup_top) {
465         goto error;
466     }
467
468     /* job->len is fixed, so we can't allow resize */
469     job = block_job_create(job_id, &backup_job_driver, txn, backup_top,
470                            0, BLK_PERM_ALL,
471                            speed, creation_flags, cb, opaque, errp);
472     if (!job) {
473         goto error;
474     }
475
476     job->backup_top = backup_top;
477     job->source_bs = bs;
478     job->on_source_error = on_source_error;
479     job->on_target_error = on_target_error;
480     job->sync_mode = sync_mode;
481     job->sync_bitmap = sync_bitmap;
482     job->bitmap_mode = bitmap_mode;
483     job->bcs = bcs;
484     job->cluster_size = cluster_size;
485     job->len = len;
486     job->perf = *perf;
487
488     block_copy_set_progress_callback(bcs, backup_progress_bytes_callback, job);
489     block_copy_set_progress_meter(bcs, &job->common.job.progress);
490
491     /* Required permissions are already taken by backup-top target */
492     block_job_add_bdrv(&job->common, "target", target, 0, BLK_PERM_ALL,
493                        &error_abort);
494
495     return &job->common;
496
497  error:
498     if (sync_bitmap) {
499         bdrv_reclaim_dirty_bitmap(sync_bitmap, NULL);
500     }
501     if (backup_top) {
502         bdrv_backup_top_drop(backup_top);
503     }
504
505     return NULL;
506 }