]> Git Repo - qemu.git/blame - migration/block-dirty-bitmap.c
bitmap: Update count after a merge
[qemu.git] / migration / block-dirty-bitmap.c
CommitLineData
b35ebdf0
VSO
1/*
2 * Block dirty bitmap postcopy migration
3 *
4 * Copyright IBM, Corp. 2009
5 * Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
6 *
7 * Authors:
8 * Liran Schour <[email protected]>
9 * Vladimir Sementsov-Ogievskiy <[email protected]>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 * This file is derived from migration/block.c, so it's author and IBM copyright
14 * are here, although content is quite different.
15 *
16 * Contributions after 2012-01-13 are licensed under the terms of the
17 * GNU GPL, version 2 or (at your option) any later version.
18 *
19 * ***
20 *
21 * Here postcopy migration of dirty bitmaps is realized. Only QMP-addressable
22 * bitmaps are migrated.
23 *
24 * Bitmap migration implies creating bitmap with the same name and granularity
25 * in destination QEMU. If the bitmap with the same name (for the same node)
26 * already exists on destination an error will be generated.
27 *
28 * format of migration:
29 *
30 * # Header (shared for different chunk types)
31 * 1, 2 or 4 bytes: flags (see qemu_{put,put}_flags)
32 * [ 1 byte: node name size ] \ flags & DEVICE_NAME
33 * [ n bytes: node name ] /
34 * [ 1 byte: bitmap name size ] \ flags & BITMAP_NAME
35 * [ n bytes: bitmap name ] /
36 *
37 * # Start of bitmap migration (flags & START)
38 * header
39 * be64: granularity
40 * 1 byte: bitmap flags (corresponds to BdrvDirtyBitmap)
41 * bit 0 - bitmap is enabled
42 * bit 1 - bitmap is persistent
43 * bit 2 - bitmap is autoloading
44 * bits 3-7 - reserved, must be zero
45 *
46 * # Complete of bitmap migration (flags & COMPLETE)
47 * header
48 *
49 * # Data chunk of bitmap migration
50 * header
51 * be64: start sector
52 * be32: number of sectors
53 * [ be64: buffer size ] \ ! (flags & ZEROES)
54 * [ n bytes: buffer ] /
55 *
56 * The last chunk in stream should contain flags & EOS. The chunk may skip
57 * device and/or bitmap names, assuming them to be the same with the previous
58 * chunk.
59 */
60
61#include "qemu/osdep.h"
62#include "block/block.h"
63#include "block/block_int.h"
64#include "sysemu/block-backend.h"
65#include "qemu/main-loop.h"
66#include "qemu/error-report.h"
67#include "migration/misc.h"
68#include "migration/migration.h"
53d37d36 69#include "qemu-file.h"
b35ebdf0
VSO
70#include "migration/vmstate.h"
71#include "migration/register.h"
72#include "qemu/hbitmap.h"
73#include "sysemu/sysemu.h"
74#include "qemu/cutils.h"
75#include "qapi/error.h"
76#include "trace.h"
77
78#define CHUNK_SIZE (1 << 10)
79
80/* Flags occupy one, two or four bytes (Big Endian). The size is determined as
81 * follows:
82 * in first (most significant) byte bit 8 is clear --> one byte
83 * in first byte bit 8 is set --> two or four bytes, depending on second
84 * byte:
85 * | in second byte bit 8 is clear --> two bytes
86 * | in second byte bit 8 is set --> four bytes
87 */
88#define DIRTY_BITMAP_MIG_FLAG_EOS 0x01
89#define DIRTY_BITMAP_MIG_FLAG_ZEROES 0x02
90#define DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME 0x04
91#define DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME 0x08
92#define DIRTY_BITMAP_MIG_FLAG_START 0x10
93#define DIRTY_BITMAP_MIG_FLAG_COMPLETE 0x20
94#define DIRTY_BITMAP_MIG_FLAG_BITS 0x40
95
96#define DIRTY_BITMAP_MIG_EXTRA_FLAGS 0x80
97
98#define DIRTY_BITMAP_MIG_START_FLAG_ENABLED 0x01
99#define DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT 0x02
100/* 0x04 was "AUTOLOAD" flags on elder versions, no it is ignored */
101#define DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK 0xf8
102
103typedef struct DirtyBitmapMigBitmapState {
104 /* Written during setup phase. */
105 BlockDriverState *bs;
106 const char *node_name;
107 BdrvDirtyBitmap *bitmap;
108 uint64_t total_sectors;
109 uint64_t sectors_per_chunk;
110 QSIMPLEQ_ENTRY(DirtyBitmapMigBitmapState) entry;
111 uint8_t flags;
112
113 /* For bulk phase. */
114 bool bulk_completed;
115 uint64_t cur_sector;
116} DirtyBitmapMigBitmapState;
117
118typedef struct DirtyBitmapMigState {
119 QSIMPLEQ_HEAD(dbms_list, DirtyBitmapMigBitmapState) dbms_list;
120
121 bool bulk_completed;
122 bool no_bitmaps;
123
124 /* for send_bitmap_bits() */
125 BlockDriverState *prev_bs;
126 BdrvDirtyBitmap *prev_bitmap;
127} DirtyBitmapMigState;
128
129typedef struct DirtyBitmapLoadState {
130 uint32_t flags;
131 char node_name[256];
132 char bitmap_name[256];
133 BlockDriverState *bs;
134 BdrvDirtyBitmap *bitmap;
135} DirtyBitmapLoadState;
136
137static DirtyBitmapMigState dirty_bitmap_mig_state;
138
139typedef struct DirtyBitmapLoadBitmapState {
140 BlockDriverState *bs;
141 BdrvDirtyBitmap *bitmap;
142 bool migrated;
143} DirtyBitmapLoadBitmapState;
144static GSList *enabled_bitmaps;
145QemuMutex finish_lock;
146
147void init_dirty_bitmap_incoming_migration(void)
148{
149 qemu_mutex_init(&finish_lock);
150}
151
152static uint32_t qemu_get_bitmap_flags(QEMUFile *f)
153{
154 uint8_t flags = qemu_get_byte(f);
155 if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
156 flags = flags << 8 | qemu_get_byte(f);
157 if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
158 flags = flags << 16 | qemu_get_be16(f);
159 }
160 }
161
162 return flags;
163}
164
165static void qemu_put_bitmap_flags(QEMUFile *f, uint32_t flags)
166{
167 /* The code currently do not send flags more than one byte */
168 assert(!(flags & (0xffffff00 | DIRTY_BITMAP_MIG_EXTRA_FLAGS)));
169
170 qemu_put_byte(f, flags);
171}
172
173static void send_bitmap_header(QEMUFile *f, DirtyBitmapMigBitmapState *dbms,
174 uint32_t additional_flags)
175{
176 BlockDriverState *bs = dbms->bs;
177 BdrvDirtyBitmap *bitmap = dbms->bitmap;
178 uint32_t flags = additional_flags;
179 trace_send_bitmap_header_enter();
180
181 if (bs != dirty_bitmap_mig_state.prev_bs) {
182 dirty_bitmap_mig_state.prev_bs = bs;
183 flags |= DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME;
184 }
185
186 if (bitmap != dirty_bitmap_mig_state.prev_bitmap) {
187 dirty_bitmap_mig_state.prev_bitmap = bitmap;
188 flags |= DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME;
189 }
190
191 qemu_put_bitmap_flags(f, flags);
192
193 if (flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
194 qemu_put_counted_string(f, dbms->node_name);
195 }
196
197 if (flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
198 qemu_put_counted_string(f, bdrv_dirty_bitmap_name(bitmap));
199 }
200}
201
202static void send_bitmap_start(QEMUFile *f, DirtyBitmapMigBitmapState *dbms)
203{
204 send_bitmap_header(f, dbms, DIRTY_BITMAP_MIG_FLAG_START);
205 qemu_put_be32(f, bdrv_dirty_bitmap_granularity(dbms->bitmap));
206 qemu_put_byte(f, dbms->flags);
207}
208
209static void send_bitmap_complete(QEMUFile *f, DirtyBitmapMigBitmapState *dbms)
210{
211 send_bitmap_header(f, dbms, DIRTY_BITMAP_MIG_FLAG_COMPLETE);
212}
213
214static void send_bitmap_bits(QEMUFile *f, DirtyBitmapMigBitmapState *dbms,
215 uint64_t start_sector, uint32_t nr_sectors)
216{
217 /* align for buffer_is_zero() */
218 uint64_t align = 4 * sizeof(long);
219 uint64_t unaligned_size =
220 bdrv_dirty_bitmap_serialization_size(
221 dbms->bitmap, start_sector << BDRV_SECTOR_BITS,
222 (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
223 uint64_t buf_size = QEMU_ALIGN_UP(unaligned_size, align);
224 uint8_t *buf = g_malloc0(buf_size);
225 uint32_t flags = DIRTY_BITMAP_MIG_FLAG_BITS;
226
227 bdrv_dirty_bitmap_serialize_part(
228 dbms->bitmap, buf, start_sector << BDRV_SECTOR_BITS,
229 (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
230
231 if (buffer_is_zero(buf, buf_size)) {
232 g_free(buf);
233 buf = NULL;
234 flags |= DIRTY_BITMAP_MIG_FLAG_ZEROES;
235 }
236
237 trace_send_bitmap_bits(flags, start_sector, nr_sectors, buf_size);
238
239 send_bitmap_header(f, dbms, flags);
240
241 qemu_put_be64(f, start_sector);
242 qemu_put_be32(f, nr_sectors);
243
244 /* if a block is zero we need to flush here since the network
245 * bandwidth is now a lot higher than the storage device bandwidth.
246 * thus if we queue zero blocks we slow down the migration. */
247 if (flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
248 qemu_fflush(f);
249 } else {
250 qemu_put_be64(f, buf_size);
251 qemu_put_buffer(f, buf, buf_size);
252 }
253
254 g_free(buf);
255}
256
257/* Called with iothread lock taken. */
258static void dirty_bitmap_mig_cleanup(void)
259{
260 DirtyBitmapMigBitmapState *dbms;
261
262 while ((dbms = QSIMPLEQ_FIRST(&dirty_bitmap_mig_state.dbms_list)) != NULL) {
263 QSIMPLEQ_REMOVE_HEAD(&dirty_bitmap_mig_state.dbms_list, entry);
264 bdrv_dirty_bitmap_set_qmp_locked(dbms->bitmap, false);
265 bdrv_unref(dbms->bs);
266 g_free(dbms);
267 }
268}
269
270/* Called with iothread lock taken. */
271static int init_dirty_bitmap_migration(void)
272{
273 BlockDriverState *bs;
274 BdrvDirtyBitmap *bitmap;
275 DirtyBitmapMigBitmapState *dbms;
276 BdrvNextIterator it;
277
278 dirty_bitmap_mig_state.bulk_completed = false;
279 dirty_bitmap_mig_state.prev_bs = NULL;
280 dirty_bitmap_mig_state.prev_bitmap = NULL;
281 dirty_bitmap_mig_state.no_bitmaps = false;
282
283 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
284 const char *drive_name = bdrv_get_device_or_node_name(bs);
285
286 /* skip automatically inserted nodes */
287 while (bs && bs->drv && bs->implicit) {
288 bs = backing_bs(bs);
289 }
290
291 for (bitmap = bdrv_dirty_bitmap_next(bs, NULL); bitmap;
292 bitmap = bdrv_dirty_bitmap_next(bs, bitmap))
293 {
294 if (!bdrv_dirty_bitmap_name(bitmap)) {
295 continue;
296 }
297
298 if (drive_name == NULL) {
299 error_report("Found bitmap '%s' in unnamed node %p. It can't "
300 "be migrated", bdrv_dirty_bitmap_name(bitmap), bs);
301 goto fail;
302 }
303
993edc0c
JS
304 if (bdrv_dirty_bitmap_user_locked(bitmap)) {
305 error_report("Can't migrate a bitmap that is in use by another operation: '%s'",
b35ebdf0
VSO
306 bdrv_dirty_bitmap_name(bitmap));
307 goto fail;
308 }
309
310 bdrv_ref(bs);
311 bdrv_dirty_bitmap_set_qmp_locked(bitmap, true);
312
313 dbms = g_new0(DirtyBitmapMigBitmapState, 1);
314 dbms->bs = bs;
315 dbms->node_name = drive_name;
316 dbms->bitmap = bitmap;
317 dbms->total_sectors = bdrv_nb_sectors(bs);
318 dbms->sectors_per_chunk = CHUNK_SIZE * 8 *
319 bdrv_dirty_bitmap_granularity(bitmap) >> BDRV_SECTOR_BITS;
320 if (bdrv_dirty_bitmap_enabled(bitmap)) {
321 dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
322 }
323 if (bdrv_dirty_bitmap_get_persistance(bitmap)) {
324 dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT;
325 }
326
327 QSIMPLEQ_INSERT_TAIL(&dirty_bitmap_mig_state.dbms_list,
328 dbms, entry);
329 }
330 }
331
332 /* unset persistance here, to not roll back it */
333 QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
334 bdrv_dirty_bitmap_set_persistance(dbms->bitmap, false);
335 }
336
337 if (QSIMPLEQ_EMPTY(&dirty_bitmap_mig_state.dbms_list)) {
338 dirty_bitmap_mig_state.no_bitmaps = true;
339 }
340
341 return 0;
342
343fail:
344 dirty_bitmap_mig_cleanup();
345
346 return -1;
347}
348
349/* Called with no lock taken. */
350static void bulk_phase_send_chunk(QEMUFile *f, DirtyBitmapMigBitmapState *dbms)
351{
352 uint32_t nr_sectors = MIN(dbms->total_sectors - dbms->cur_sector,
353 dbms->sectors_per_chunk);
354
355 send_bitmap_bits(f, dbms, dbms->cur_sector, nr_sectors);
356
357 dbms->cur_sector += nr_sectors;
358 if (dbms->cur_sector >= dbms->total_sectors) {
359 dbms->bulk_completed = true;
360 }
361}
362
363/* Called with no lock taken. */
364static void bulk_phase(QEMUFile *f, bool limit)
365{
366 DirtyBitmapMigBitmapState *dbms;
367
368 QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
369 while (!dbms->bulk_completed) {
370 bulk_phase_send_chunk(f, dbms);
371 if (limit && qemu_file_rate_limit(f)) {
372 return;
373 }
374 }
375 }
376
377 dirty_bitmap_mig_state.bulk_completed = true;
378}
379
380/* for SaveVMHandlers */
381static void dirty_bitmap_save_cleanup(void *opaque)
382{
383 dirty_bitmap_mig_cleanup();
384}
385
386static int dirty_bitmap_save_iterate(QEMUFile *f, void *opaque)
387{
388 trace_dirty_bitmap_save_iterate(migration_in_postcopy());
389
390 if (migration_in_postcopy() && !dirty_bitmap_mig_state.bulk_completed) {
391 bulk_phase(f, true);
392 }
393
394 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
395
396 return dirty_bitmap_mig_state.bulk_completed;
397}
398
399/* Called with iothread lock taken. */
400
401static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque)
402{
403 DirtyBitmapMigBitmapState *dbms;
404 trace_dirty_bitmap_save_complete_enter();
405
406 if (!dirty_bitmap_mig_state.bulk_completed) {
407 bulk_phase(f, false);
408 }
409
410 QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
411 send_bitmap_complete(f, dbms);
412 }
413
414 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
415
416 trace_dirty_bitmap_save_complete_finish();
417
418 dirty_bitmap_mig_cleanup();
419 return 0;
420}
421
422static void dirty_bitmap_save_pending(QEMUFile *f, void *opaque,
423 uint64_t max_size,
424 uint64_t *res_precopy_only,
425 uint64_t *res_compatible,
426 uint64_t *res_postcopy_only)
427{
428 DirtyBitmapMigBitmapState *dbms;
429 uint64_t pending = 0;
430
431 qemu_mutex_lock_iothread();
432
433 QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
434 uint64_t gran = bdrv_dirty_bitmap_granularity(dbms->bitmap);
435 uint64_t sectors = dbms->bulk_completed ? 0 :
436 dbms->total_sectors - dbms->cur_sector;
437
438 pending += DIV_ROUND_UP(sectors * BDRV_SECTOR_SIZE, gran);
439 }
440
441 qemu_mutex_unlock_iothread();
442
443 trace_dirty_bitmap_save_pending(pending, max_size);
444
445 *res_postcopy_only += pending;
446}
447
448/* First occurrence of this bitmap. It should be created if doesn't exist */
449static int dirty_bitmap_load_start(QEMUFile *f, DirtyBitmapLoadState *s)
450{
451 Error *local_err = NULL;
452 uint32_t granularity = qemu_get_be32(f);
453 uint8_t flags = qemu_get_byte(f);
454
455 if (s->bitmap) {
456 error_report("Bitmap with the same name ('%s') already exists on "
457 "destination", bdrv_dirty_bitmap_name(s->bitmap));
458 return -EINVAL;
459 } else {
460 s->bitmap = bdrv_create_dirty_bitmap(s->bs, granularity,
461 s->bitmap_name, &local_err);
462 if (!s->bitmap) {
463 error_report_err(local_err);
464 return -EINVAL;
465 }
466 }
467
468 if (flags & DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK) {
469 error_report("Unknown flags in migrated dirty bitmap header: %x",
470 flags);
471 return -EINVAL;
472 }
473
474 if (flags & DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT) {
475 bdrv_dirty_bitmap_set_persistance(s->bitmap, true);
476 }
477
478 bdrv_disable_dirty_bitmap(s->bitmap);
479 if (flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED) {
480 DirtyBitmapLoadBitmapState *b;
481
482 bdrv_dirty_bitmap_create_successor(s->bs, s->bitmap, &local_err);
483 if (local_err) {
484 error_report_err(local_err);
485 return -EINVAL;
486 }
487
488 b = g_new(DirtyBitmapLoadBitmapState, 1);
489 b->bs = s->bs;
490 b->bitmap = s->bitmap;
491 b->migrated = false;
492 enabled_bitmaps = g_slist_prepend(enabled_bitmaps, b);
493 }
494
495 return 0;
496}
497
498void dirty_bitmap_mig_before_vm_start(void)
499{
500 GSList *item;
501
502 qemu_mutex_lock(&finish_lock);
503
504 for (item = enabled_bitmaps; item; item = g_slist_next(item)) {
505 DirtyBitmapLoadBitmapState *b = item->data;
506
507 if (b->migrated) {
58f72b96 508 bdrv_enable_dirty_bitmap_locked(b->bitmap);
b35ebdf0
VSO
509 } else {
510 bdrv_dirty_bitmap_enable_successor(b->bitmap);
511 }
512
513 g_free(b);
514 }
515
516 g_slist_free(enabled_bitmaps);
517 enabled_bitmaps = NULL;
518
519 qemu_mutex_unlock(&finish_lock);
520}
521
522static void dirty_bitmap_load_complete(QEMUFile *f, DirtyBitmapLoadState *s)
523{
524 GSList *item;
525 trace_dirty_bitmap_load_complete();
526 bdrv_dirty_bitmap_deserialize_finish(s->bitmap);
527
528 qemu_mutex_lock(&finish_lock);
529
530 for (item = enabled_bitmaps; item; item = g_slist_next(item)) {
531 DirtyBitmapLoadBitmapState *b = item->data;
532
533 if (b->bitmap == s->bitmap) {
534 b->migrated = true;
535 break;
536 }
537 }
538
539 if (bdrv_dirty_bitmap_frozen(s->bitmap)) {
540 bdrv_dirty_bitmap_lock(s->bitmap);
541 if (enabled_bitmaps == NULL) {
542 /* in postcopy */
543 bdrv_reclaim_dirty_bitmap_locked(s->bs, s->bitmap, &error_abort);
58f72b96 544 bdrv_enable_dirty_bitmap_locked(s->bitmap);
b35ebdf0
VSO
545 } else {
546 /* target not started, successor must be empty */
547 int64_t count = bdrv_get_dirty_count(s->bitmap);
548 BdrvDirtyBitmap *ret = bdrv_reclaim_dirty_bitmap_locked(s->bs,
549 s->bitmap,
550 NULL);
551 /* bdrv_reclaim_dirty_bitmap can fail only on no successor (it
552 * must be) or on merge fail, but merge can't fail when second
553 * bitmap is empty
554 */
555 assert(ret == s->bitmap &&
556 count == bdrv_get_dirty_count(s->bitmap));
557 }
558 bdrv_dirty_bitmap_unlock(s->bitmap);
559 }
560
561 qemu_mutex_unlock(&finish_lock);
562}
563
564static int dirty_bitmap_load_bits(QEMUFile *f, DirtyBitmapLoadState *s)
565{
566 uint64_t first_byte = qemu_get_be64(f) << BDRV_SECTOR_BITS;
567 uint64_t nr_bytes = (uint64_t)qemu_get_be32(f) << BDRV_SECTOR_BITS;
568 trace_dirty_bitmap_load_bits_enter(first_byte >> BDRV_SECTOR_BITS,
569 nr_bytes >> BDRV_SECTOR_BITS);
570
571 if (s->flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
572 trace_dirty_bitmap_load_bits_zeroes();
573 bdrv_dirty_bitmap_deserialize_zeroes(s->bitmap, first_byte, nr_bytes,
574 false);
575 } else {
576 size_t ret;
577 uint8_t *buf;
578 uint64_t buf_size = qemu_get_be64(f);
579 uint64_t needed_size =
580 bdrv_dirty_bitmap_serialization_size(s->bitmap,
581 first_byte, nr_bytes);
582
583 if (needed_size > buf_size ||
584 buf_size > QEMU_ALIGN_UP(needed_size, 4 * sizeof(long))
585 /* Here used same alignment as in send_bitmap_bits */
586 ) {
587 error_report("Migrated bitmap granularity doesn't "
588 "match the destination bitmap '%s' granularity",
589 bdrv_dirty_bitmap_name(s->bitmap));
590 return -EINVAL;
591 }
592
593 buf = g_malloc(buf_size);
594 ret = qemu_get_buffer(f, buf, buf_size);
595 if (ret != buf_size) {
596 error_report("Failed to read bitmap bits");
16a22278 597 g_free(buf);
b35ebdf0
VSO
598 return -EIO;
599 }
600
601 bdrv_dirty_bitmap_deserialize_part(s->bitmap, buf, first_byte, nr_bytes,
602 false);
603 g_free(buf);
604 }
605
606 return 0;
607}
608
609static int dirty_bitmap_load_header(QEMUFile *f, DirtyBitmapLoadState *s)
610{
611 Error *local_err = NULL;
612 bool nothing;
613 s->flags = qemu_get_bitmap_flags(f);
614 trace_dirty_bitmap_load_header(s->flags);
615
616 nothing = s->flags == (s->flags & DIRTY_BITMAP_MIG_FLAG_EOS);
617
618 if (s->flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
619 if (!qemu_get_counted_string(f, s->node_name)) {
620 error_report("Unable to read node name string");
621 return -EINVAL;
622 }
623 s->bs = bdrv_lookup_bs(s->node_name, s->node_name, &local_err);
624 if (!s->bs) {
625 error_report_err(local_err);
626 return -EINVAL;
627 }
628 } else if (!s->bs && !nothing) {
629 error_report("Error: block device name is not set");
630 return -EINVAL;
631 }
632
633 if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
634 if (!qemu_get_counted_string(f, s->bitmap_name)) {
635 error_report("Unable to read bitmap name string");
636 return -EINVAL;
637 }
638 s->bitmap = bdrv_find_dirty_bitmap(s->bs, s->bitmap_name);
639
640 /* bitmap may be NULL here, it wouldn't be an error if it is the
641 * first occurrence of the bitmap */
642 if (!s->bitmap && !(s->flags & DIRTY_BITMAP_MIG_FLAG_START)) {
643 error_report("Error: unknown dirty bitmap "
644 "'%s' for block device '%s'",
645 s->bitmap_name, s->node_name);
646 return -EINVAL;
647 }
648 } else if (!s->bitmap && !nothing) {
649 error_report("Error: block device name is not set");
650 return -EINVAL;
651 }
652
653 return 0;
654}
655
656static int dirty_bitmap_load(QEMUFile *f, void *opaque, int version_id)
657{
658 static DirtyBitmapLoadState s;
659 int ret = 0;
660
661 trace_dirty_bitmap_load_enter();
662
663 if (version_id != 1) {
664 return -EINVAL;
665 }
666
667 do {
668 ret = dirty_bitmap_load_header(f, &s);
a36f6ff4
VSO
669 if (ret < 0) {
670 return ret;
671 }
b35ebdf0
VSO
672
673 if (s.flags & DIRTY_BITMAP_MIG_FLAG_START) {
674 ret = dirty_bitmap_load_start(f, &s);
675 } else if (s.flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) {
676 dirty_bitmap_load_complete(f, &s);
677 } else if (s.flags & DIRTY_BITMAP_MIG_FLAG_BITS) {
678 ret = dirty_bitmap_load_bits(f, &s);
679 }
680
681 if (!ret) {
682 ret = qemu_file_get_error(f);
683 }
684
685 if (ret) {
686 return ret;
687 }
688 } while (!(s.flags & DIRTY_BITMAP_MIG_FLAG_EOS));
689
690 trace_dirty_bitmap_load_success();
691 return 0;
692}
693
694static int dirty_bitmap_save_setup(QEMUFile *f, void *opaque)
695{
696 DirtyBitmapMigBitmapState *dbms = NULL;
697 if (init_dirty_bitmap_migration() < 0) {
698 return -1;
699 }
700
701 QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
702 send_bitmap_start(f, dbms);
703 }
704 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
705
706 return 0;
707}
708
709static bool dirty_bitmap_is_active(void *opaque)
710{
711 return migrate_dirty_bitmaps() && !dirty_bitmap_mig_state.no_bitmaps;
712}
713
714static bool dirty_bitmap_is_active_iterate(void *opaque)
715{
716 return dirty_bitmap_is_active(opaque) && !runstate_is_running();
717}
718
719static bool dirty_bitmap_has_postcopy(void *opaque)
720{
721 return true;
722}
723
724static SaveVMHandlers savevm_dirty_bitmap_handlers = {
725 .save_setup = dirty_bitmap_save_setup,
726 .save_live_complete_postcopy = dirty_bitmap_save_complete,
727 .save_live_complete_precopy = dirty_bitmap_save_complete,
728 .has_postcopy = dirty_bitmap_has_postcopy,
729 .save_live_pending = dirty_bitmap_save_pending,
730 .save_live_iterate = dirty_bitmap_save_iterate,
731 .is_active_iterate = dirty_bitmap_is_active_iterate,
732 .load_state = dirty_bitmap_load,
733 .save_cleanup = dirty_bitmap_save_cleanup,
734 .is_active = dirty_bitmap_is_active,
735};
736
737void dirty_bitmap_mig_init(void)
738{
739 QSIMPLEQ_INIT(&dirty_bitmap_mig_state.dbms_list);
740
741 register_savevm_live(NULL, "dirty-bitmap", 0, 1,
742 &savevm_dirty_bitmap_handlers,
743 &dirty_bitmap_mig_state);
744}
This page took 0.121352 seconds and 4 git commands to generate.