xref: /openbmc/qemu/block/backup.c (revision 397d30e9)
1 /*
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 
14 #include "qemu/osdep.h"
15 
16 #include "trace.h"
17 #include "block/block.h"
18 #include "block/block_int.h"
19 #include "block/blockjob.h"
20 #include "block/block_backup.h"
21 #include "qapi/error.h"
22 #include "qapi/qmp/qerror.h"
23 #include "qemu/ratelimit.h"
24 #include "qemu/cutils.h"
25 #include "sysemu/block-backend.h"
26 #include "qemu/bitmap.h"
27 
28 #define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
29 #define SLICE_TIME 100000000ULL /* ns */
30 
31 typedef struct BackupBlockJob {
32     BlockJob common;
33     BlockBackend *target;
34     /* bitmap for sync=incremental */
35     BdrvDirtyBitmap *sync_bitmap;
36     MirrorSyncMode sync_mode;
37     RateLimit limit;
38     BlockdevOnError on_source_error;
39     BlockdevOnError on_target_error;
40     CoRwlock flush_rwlock;
41     uint64_t sectors_read;
42     unsigned long *done_bitmap;
43     int64_t cluster_size;
44     bool compress;
45     NotifierWithReturn before_write;
46     QLIST_HEAD(, CowRequest) inflight_reqs;
47 } BackupBlockJob;
48 
49 /* Size of a cluster in sectors, instead of bytes. */
50 static inline int64_t cluster_size_sectors(BackupBlockJob *job)
51 {
52   return job->cluster_size / BDRV_SECTOR_SIZE;
53 }
54 
55 /* See if in-flight requests overlap and wait for them to complete */
56 static 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) {
67                 qemu_co_queue_wait(&req->wait_queue);
68                 retry = true;
69                 break;
70             }
71         }
72     } while (retry);
73 }
74 
75 /* Keep track of an in-flight request */
76 static 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 */
86 static void cow_request_end(CowRequest *req)
87 {
88     QLIST_REMOVE(req, list);
89     qemu_co_queue_restart_all(&req->wait_queue);
90 }
91 
92 static int coroutine_fn backup_do_cow(BackupBlockJob *job,
93                                       int64_t sector_num, int nb_sectors,
94                                       bool *error_is_read,
95                                       bool is_write_notifier)
96 {
97     BlockBackend *blk = job->common.blk;
98     CowRequest cow_request;
99     struct iovec iov;
100     QEMUIOVector bounce_qiov;
101     void *bounce_buffer = NULL;
102     int ret = 0;
103     int64_t sectors_per_cluster = cluster_size_sectors(job);
104     int64_t start, end;
105     int n;
106 
107     qemu_co_rwlock_rdlock(&job->flush_rwlock);
108 
109     start = sector_num / sectors_per_cluster;
110     end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
111 
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++) {
118         if (test_bit(start, job->done_bitmap)) {
119             trace_backup_do_cow_skip(job, start);
120             continue; /* already copied */
121         }
122 
123         trace_backup_do_cow_process(job, start);
124 
125         n = MIN(sectors_per_cluster,
126                 job->common.len / BDRV_SECTOR_SIZE -
127                 start * sectors_per_cluster);
128 
129         if (!bounce_buffer) {
130             bounce_buffer = blk_blockalign(blk, job->cluster_size);
131         }
132         iov.iov_base = bounce_buffer;
133         iov.iov_len = n * BDRV_SECTOR_SIZE;
134         qemu_iovec_init_external(&bounce_qiov, &iov, 1);
135 
136         ret = blk_co_preadv(blk, start * job->cluster_size,
137                             bounce_qiov.size, &bounce_qiov,
138                             is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0);
139         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)) {
148             ret = blk_co_pwrite_zeroes(job->target, start * job->cluster_size,
149                                        bounce_qiov.size, BDRV_REQ_MAY_UNMAP);
150         } else {
151             ret = blk_co_pwritev(job->target, start * job->cluster_size,
152                                  bounce_qiov.size, &bounce_qiov,
153                                  job->compress ? BDRV_REQ_WRITE_COMPRESSED : 0);
154         }
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 
163         set_bit(start, job->done_bitmap);
164 
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 
172 out:
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 
186 static int coroutine_fn backup_before_write_notify(
187         NotifierWithReturn *notifier,
188         void *opaque)
189 {
190     BackupBlockJob *job = container_of(notifier, BackupBlockJob, before_write);
191     BdrvTrackedRequest *req = opaque;
192     int64_t sector_num = req->offset >> BDRV_SECTOR_BITS;
193     int nb_sectors = req->bytes >> BDRV_SECTOR_BITS;
194 
195     assert(req->bs == blk_bs(job->common.blk));
196     assert((req->offset & (BDRV_SECTOR_SIZE - 1)) == 0);
197     assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
198 
199     return backup_do_cow(job, sector_num, nb_sectors, NULL, true);
200 }
201 
202 static 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) {
207         error_setg(errp, QERR_INVALID_PARAMETER, "speed");
208         return;
209     }
210     ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
211 }
212 
213 static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
214 {
215     BdrvDirtyBitmap *bm;
216     BlockDriverState *bs = blk_bs(job->common.blk);
217 
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 
229 static 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 
237 static 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 
245 static void backup_attached_aio_context(BlockJob *job, AioContext *aio_context)
246 {
247     BackupBlockJob *s = container_of(job, BackupBlockJob, common);
248 
249     blk_set_aio_context(s->target, aio_context);
250 }
251 
252 void backup_do_checkpoint(BlockJob *job, Error **errp)
253 {
254     BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
255     int64_t len;
256 
257     assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP);
258 
259     if (backup_job->sync_mode != MIRROR_SYNC_MODE_NONE) {
260         error_setg(errp, "The backup job only supports block checkpoint in"
261                    " sync=none mode");
262         return;
263     }
264 
265     len = DIV_ROUND_UP(backup_job->common.len, backup_job->cluster_size);
266     bitmap_zero(backup_job->done_bitmap, len);
267 }
268 
269 void backup_wait_for_overlapping_requests(BlockJob *job, int64_t sector_num,
270                                           int nb_sectors)
271 {
272     BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
273     int64_t sectors_per_cluster = cluster_size_sectors(backup_job);
274     int64_t start, end;
275 
276     assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP);
277 
278     start = sector_num / sectors_per_cluster;
279     end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
280     wait_for_overlapping_requests(backup_job, start, end);
281 }
282 
283 void backup_cow_request_begin(CowRequest *req, BlockJob *job,
284                               int64_t sector_num,
285                               int nb_sectors)
286 {
287     BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
288     int64_t sectors_per_cluster = cluster_size_sectors(backup_job);
289     int64_t start, end;
290 
291     assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP);
292 
293     start = sector_num / sectors_per_cluster;
294     end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
295     cow_request_begin(req, backup_job, start, end);
296 }
297 
298 void backup_cow_request_end(CowRequest *req)
299 {
300     cow_request_end(req);
301 }
302 
303 static void backup_drain(BlockJob *job)
304 {
305     BackupBlockJob *s = container_of(job, BackupBlockJob, common);
306 
307     /* Need to keep a reference in case blk_drain triggers execution
308      * of backup_complete...
309      */
310     if (s->target) {
311         BlockBackend *target = s->target;
312         blk_ref(target);
313         blk_drain(target);
314         blk_unref(target);
315     }
316 }
317 
318 static const BlockJobDriver backup_job_driver = {
319     .instance_size          = sizeof(BackupBlockJob),
320     .job_type               = BLOCK_JOB_TYPE_BACKUP,
321     .set_speed              = backup_set_speed,
322     .commit                 = backup_commit,
323     .abort                  = backup_abort,
324     .attached_aio_context   = backup_attached_aio_context,
325     .drain                  = backup_drain,
326 };
327 
328 static BlockErrorAction backup_error_action(BackupBlockJob *job,
329                                             bool read, int error)
330 {
331     if (read) {
332         return block_job_error_action(&job->common, job->on_source_error,
333                                       true, error);
334     } else {
335         return block_job_error_action(&job->common, job->on_target_error,
336                                       false, error);
337     }
338 }
339 
340 typedef struct {
341     int ret;
342 } BackupCompleteData;
343 
344 static void backup_complete(BlockJob *job, void *opaque)
345 {
346     BackupBlockJob *s = container_of(job, BackupBlockJob, common);
347     BackupCompleteData *data = opaque;
348 
349     blk_unref(s->target);
350     s->target = NULL;
351 
352     block_job_completed(job, data->ret);
353     g_free(data);
354 }
355 
356 static bool coroutine_fn yield_and_check(BackupBlockJob *job)
357 {
358     if (block_job_is_cancelled(&job->common)) {
359         return true;
360     }
361 
362     /* we need to yield so that bdrv_drain_all() returns.
363      * (without, VM does not reboot)
364      */
365     if (job->common.speed) {
366         uint64_t delay_ns = ratelimit_calculate_delay(&job->limit,
367                                                       job->sectors_read);
368         job->sectors_read = 0;
369         block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns);
370     } else {
371         block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0);
372     }
373 
374     if (block_job_is_cancelled(&job->common)) {
375         return true;
376     }
377 
378     return false;
379 }
380 
381 static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
382 {
383     bool error_is_read;
384     int ret = 0;
385     int clusters_per_iter;
386     uint32_t granularity;
387     int64_t sector;
388     int64_t cluster;
389     int64_t end;
390     int64_t last_cluster = -1;
391     int64_t sectors_per_cluster = cluster_size_sectors(job);
392     BdrvDirtyBitmapIter *dbi;
393 
394     granularity = bdrv_dirty_bitmap_granularity(job->sync_bitmap);
395     clusters_per_iter = MAX((granularity / job->cluster_size), 1);
396     dbi = bdrv_dirty_iter_new(job->sync_bitmap, 0);
397 
398     /* Find the next dirty sector(s) */
399     while ((sector = bdrv_dirty_iter_next(dbi)) != -1) {
400         cluster = sector / sectors_per_cluster;
401 
402         /* Fake progress updates for any clusters we skipped */
403         if (cluster != last_cluster + 1) {
404             job->common.offset += ((cluster - last_cluster - 1) *
405                                    job->cluster_size);
406         }
407 
408         for (end = cluster + clusters_per_iter; cluster < end; cluster++) {
409             do {
410                 if (yield_and_check(job)) {
411                     goto out;
412                 }
413                 ret = backup_do_cow(job, cluster * sectors_per_cluster,
414                                     sectors_per_cluster, &error_is_read,
415                                     false);
416                 if ((ret < 0) &&
417                     backup_error_action(job, error_is_read, -ret) ==
418                     BLOCK_ERROR_ACTION_REPORT) {
419                     goto out;
420                 }
421             } while (ret < 0);
422         }
423 
424         /* If the bitmap granularity is smaller than the backup granularity,
425          * we need to advance the iterator pointer to the next cluster. */
426         if (granularity < job->cluster_size) {
427             bdrv_set_dirty_iter(dbi, cluster * sectors_per_cluster);
428         }
429 
430         last_cluster = cluster - 1;
431     }
432 
433     /* Play some final catchup with the progress meter */
434     end = DIV_ROUND_UP(job->common.len, job->cluster_size);
435     if (last_cluster + 1 < end) {
436         job->common.offset += ((end - last_cluster - 1) * job->cluster_size);
437     }
438 
439 out:
440     bdrv_dirty_iter_free(dbi);
441     return ret;
442 }
443 
444 static void coroutine_fn backup_run(void *opaque)
445 {
446     BackupBlockJob *job = opaque;
447     BackupCompleteData *data;
448     BlockDriverState *bs = blk_bs(job->common.blk);
449     int64_t start, end;
450     int64_t sectors_per_cluster = cluster_size_sectors(job);
451     int ret = 0;
452 
453     QLIST_INIT(&job->inflight_reqs);
454     qemu_co_rwlock_init(&job->flush_rwlock);
455 
456     start = 0;
457     end = DIV_ROUND_UP(job->common.len, job->cluster_size);
458 
459     job->done_bitmap = bitmap_new(end);
460 
461     job->before_write.notify = backup_before_write_notify;
462     bdrv_add_before_write_notifier(bs, &job->before_write);
463 
464     if (job->sync_mode == MIRROR_SYNC_MODE_NONE) {
465         while (!block_job_is_cancelled(&job->common)) {
466             /* Yield until the job is cancelled.  We just let our before_write
467              * notify callback service CoW requests. */
468             block_job_yield(&job->common);
469         }
470     } else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
471         ret = backup_run_incremental(job);
472     } else {
473         /* Both FULL and TOP SYNC_MODE's require copying.. */
474         for (; start < end; start++) {
475             bool error_is_read;
476             if (yield_and_check(job)) {
477                 break;
478             }
479 
480             if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
481                 int i, n;
482                 int alloced = 0;
483 
484                 /* Check to see if these blocks are already in the
485                  * backing file. */
486 
487                 for (i = 0; i < sectors_per_cluster;) {
488                     /* bdrv_is_allocated() only returns true/false based
489                      * on the first set of sectors it comes across that
490                      * are are all in the same state.
491                      * For that reason we must verify each sector in the
492                      * backup cluster length.  We end up copying more than
493                      * needed but at some point that is always the case. */
494                     alloced =
495                         bdrv_is_allocated(bs,
496                                 start * sectors_per_cluster + i,
497                                 sectors_per_cluster - i, &n);
498                     i += n;
499 
500                     if (alloced == 1 || n == 0) {
501                         break;
502                     }
503                 }
504 
505                 /* If the above loop never found any sectors that are in
506                  * the topmost image, skip this backup. */
507                 if (alloced == 0) {
508                     continue;
509                 }
510             }
511             /* FULL sync mode we copy the whole drive. */
512             ret = backup_do_cow(job, start * sectors_per_cluster,
513                                 sectors_per_cluster, &error_is_read, false);
514             if (ret < 0) {
515                 /* Depending on error action, fail now or retry cluster */
516                 BlockErrorAction action =
517                     backup_error_action(job, error_is_read, -ret);
518                 if (action == BLOCK_ERROR_ACTION_REPORT) {
519                     break;
520                 } else {
521                     start--;
522                     continue;
523                 }
524             }
525         }
526     }
527 
528     notifier_with_return_remove(&job->before_write);
529 
530     /* wait until pending backup_do_cow() calls have completed */
531     qemu_co_rwlock_wrlock(&job->flush_rwlock);
532     qemu_co_rwlock_unlock(&job->flush_rwlock);
533     g_free(job->done_bitmap);
534 
535     data = g_malloc(sizeof(*data));
536     data->ret = ret;
537     block_job_defer_to_main_loop(&job->common, backup_complete, data);
538 }
539 
540 void backup_start(const char *job_id, BlockDriverState *bs,
541                   BlockDriverState *target, int64_t speed,
542                   MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap,
543                   bool compress,
544                   BlockdevOnError on_source_error,
545                   BlockdevOnError on_target_error,
546                   BlockCompletionFunc *cb, void *opaque,
547                   BlockJobTxn *txn, Error **errp)
548 {
549     int64_t len;
550     BlockDriverInfo bdi;
551     BackupBlockJob *job = NULL;
552     int ret;
553 
554     assert(bs);
555     assert(target);
556 
557     if (bs == target) {
558         error_setg(errp, "Source and target cannot be the same");
559         return;
560     }
561 
562     if (!bdrv_is_inserted(bs)) {
563         error_setg(errp, "Device is not inserted: %s",
564                    bdrv_get_device_name(bs));
565         return;
566     }
567 
568     if (!bdrv_is_inserted(target)) {
569         error_setg(errp, "Device is not inserted: %s",
570                    bdrv_get_device_name(target));
571         return;
572     }
573 
574     if (compress && target->drv->bdrv_co_pwritev_compressed == NULL) {
575         error_setg(errp, "Compression is not supported for this drive %s",
576                    bdrv_get_device_name(target));
577         return;
578     }
579 
580     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
581         return;
582     }
583 
584     if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
585         return;
586     }
587 
588     if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
589         if (!sync_bitmap) {
590             error_setg(errp, "must provide a valid bitmap name for "
591                              "\"incremental\" sync mode");
592             return;
593         }
594 
595         /* Create a new bitmap, and freeze/disable this one. */
596         if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) {
597             return;
598         }
599     } else if (sync_bitmap) {
600         error_setg(errp,
601                    "a sync_bitmap was provided to backup_run, "
602                    "but received an incompatible sync_mode (%s)",
603                    MirrorSyncMode_lookup[sync_mode]);
604         return;
605     }
606 
607     len = bdrv_getlength(bs);
608     if (len < 0) {
609         error_setg_errno(errp, -len, "unable to get length for '%s'",
610                          bdrv_get_device_name(bs));
611         goto error;
612     }
613 
614     job = block_job_create(job_id, &backup_job_driver, bs, speed,
615                            cb, opaque, errp);
616     if (!job) {
617         goto error;
618     }
619 
620     job->target = blk_new();
621     blk_insert_bs(job->target, target);
622 
623     job->on_source_error = on_source_error;
624     job->on_target_error = on_target_error;
625     job->sync_mode = sync_mode;
626     job->sync_bitmap = sync_mode == MIRROR_SYNC_MODE_INCREMENTAL ?
627                        sync_bitmap : NULL;
628     job->compress = compress;
629 
630     /* If there is no backing file on the target, we cannot rely on COW if our
631      * backup cluster size is smaller than the target cluster size. Even for
632      * targets with a backing file, try to avoid COW if possible. */
633     ret = bdrv_get_info(target, &bdi);
634     if (ret < 0 && !target->backing) {
635         error_setg_errno(errp, -ret,
636             "Couldn't determine the cluster size of the target image, "
637             "which has no backing file");
638         error_append_hint(errp,
639             "Aborting, since this may create an unusable destination image\n");
640         goto error;
641     } else if (ret < 0 && target->backing) {
642         /* Not fatal; just trudge on ahead. */
643         job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
644     } else {
645         job->cluster_size = MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
646     }
647 
648     block_job_add_bdrv(&job->common, target);
649     job->common.len = len;
650     job->common.co = qemu_coroutine_create(backup_run, job);
651     block_job_txn_add_job(txn, &job->common);
652     qemu_coroutine_enter(job->common.co);
653     return;
654 
655  error:
656     if (sync_bitmap) {
657         bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
658     }
659     if (job) {
660         blk_unref(job->target);
661         block_job_unref(&job->common);
662     }
663 }
664