Update version for v5.1.0 release
[qemu.git] / migration / block-dirty-bitmap.c
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 <lirans@il.ibm.com>
9 * Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
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 "sysemu/runstate.h"
66 #include "qemu/main-loop.h"
67 #include "qemu/error-report.h"
68 #include "migration/misc.h"
69 #include "migration/migration.h"
70 #include "qemu-file.h"
71 #include "migration/vmstate.h"
72 #include "migration/register.h"
73 #include "qemu/hbitmap.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
103 typedef 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
118 typedef struct DirtyBitmapMigState {
119 QSIMPLEQ_HEAD(, 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
129 typedef 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
137 static DirtyBitmapMigState dirty_bitmap_mig_state;
138
139 typedef struct DirtyBitmapLoadBitmapState {
140 BlockDriverState *bs;
141 BdrvDirtyBitmap *bitmap;
142 bool migrated;
143 } DirtyBitmapLoadBitmapState;
144 static GSList *enabled_bitmaps;
145 QemuMutex finish_lock;
146
147 void init_dirty_bitmap_incoming_migration(void)
148 {
149 qemu_mutex_init(&finish_lock);
150 }
151
152 static 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
165 static 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
173 static 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
202 static 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
209 static void send_bitmap_complete(QEMUFile *f, DirtyBitmapMigBitmapState *dbms)
210 {
211 send_bitmap_header(f, dbms, DIRTY_BITMAP_MIG_FLAG_COMPLETE);
212 }
213
214 static 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. */
258 static 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_busy(dbms->bitmap, false);
265 bdrv_unref(dbms->bs);
266 g_free(dbms);
267 }
268 }
269
270 /* Called with iothread lock taken. */
271 static int init_dirty_bitmap_migration(void)
272 {
273 BlockDriverState *bs;
274 BdrvDirtyBitmap *bitmap;
275 DirtyBitmapMigBitmapState *dbms;
276 Error *local_err = NULL;
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_next_all_states(NULL); bs; bs = bdrv_next_all_states(bs)) {
284 const char *name = bdrv_get_device_or_node_name(bs);
285
286 FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
287 if (!bdrv_dirty_bitmap_name(bitmap)) {
288 continue;
289 }
290
291 if (!name || strcmp(name, "") == 0) {
292 error_report("Found bitmap '%s' in unnamed node %p. It can't "
293 "be migrated", bdrv_dirty_bitmap_name(bitmap), bs);
294 goto fail;
295 }
296
297 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT,
298 &local_err)) {
299 error_report_err(local_err);
300 goto fail;
301 }
302
303 bdrv_ref(bs);
304 bdrv_dirty_bitmap_set_busy(bitmap, true);
305
306 dbms = g_new0(DirtyBitmapMigBitmapState, 1);
307 dbms->bs = bs;
308 dbms->node_name = name;
309 dbms->bitmap = bitmap;
310 dbms->total_sectors = bdrv_nb_sectors(bs);
311 dbms->sectors_per_chunk = CHUNK_SIZE * 8 *
312 bdrv_dirty_bitmap_granularity(bitmap) >> BDRV_SECTOR_BITS;
313 if (bdrv_dirty_bitmap_enabled(bitmap)) {
314 dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
315 }
316 if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
317 dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT;
318 }
319
320 QSIMPLEQ_INSERT_TAIL(&dirty_bitmap_mig_state.dbms_list,
321 dbms, entry);
322 }
323 }
324
325 /* unset migration flags here, to not roll back it */
326 QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
327 bdrv_dirty_bitmap_skip_store(dbms->bitmap, true);
328 }
329
330 if (QSIMPLEQ_EMPTY(&dirty_bitmap_mig_state.dbms_list)) {
331 dirty_bitmap_mig_state.no_bitmaps = true;
332 }
333
334 return 0;
335
336 fail:
337 dirty_bitmap_mig_cleanup();
338
339 return -1;
340 }
341
342 /* Called with no lock taken. */
343 static void bulk_phase_send_chunk(QEMUFile *f, DirtyBitmapMigBitmapState *dbms)
344 {
345 uint32_t nr_sectors = MIN(dbms->total_sectors - dbms->cur_sector,
346 dbms->sectors_per_chunk);
347
348 send_bitmap_bits(f, dbms, dbms->cur_sector, nr_sectors);
349
350 dbms->cur_sector += nr_sectors;
351 if (dbms->cur_sector >= dbms->total_sectors) {
352 dbms->bulk_completed = true;
353 }
354 }
355
356 /* Called with no lock taken. */
357 static void bulk_phase(QEMUFile *f, bool limit)
358 {
359 DirtyBitmapMigBitmapState *dbms;
360
361 QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
362 while (!dbms->bulk_completed) {
363 bulk_phase_send_chunk(f, dbms);
364 if (limit && qemu_file_rate_limit(f)) {
365 return;
366 }
367 }
368 }
369
370 dirty_bitmap_mig_state.bulk_completed = true;
371 }
372
373 /* for SaveVMHandlers */
374 static void dirty_bitmap_save_cleanup(void *opaque)
375 {
376 dirty_bitmap_mig_cleanup();
377 }
378
379 static int dirty_bitmap_save_iterate(QEMUFile *f, void *opaque)
380 {
381 trace_dirty_bitmap_save_iterate(migration_in_postcopy());
382
383 if (migration_in_postcopy() && !dirty_bitmap_mig_state.bulk_completed) {
384 bulk_phase(f, true);
385 }
386
387 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
388
389 return dirty_bitmap_mig_state.bulk_completed;
390 }
391
392 /* Called with iothread lock taken. */
393
394 static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque)
395 {
396 DirtyBitmapMigBitmapState *dbms;
397 trace_dirty_bitmap_save_complete_enter();
398
399 if (!dirty_bitmap_mig_state.bulk_completed) {
400 bulk_phase(f, false);
401 }
402
403 QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
404 send_bitmap_complete(f, dbms);
405 }
406
407 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
408
409 trace_dirty_bitmap_save_complete_finish();
410
411 dirty_bitmap_mig_cleanup();
412 return 0;
413 }
414
415 static void dirty_bitmap_save_pending(QEMUFile *f, void *opaque,
416 uint64_t max_size,
417 uint64_t *res_precopy_only,
418 uint64_t *res_compatible,
419 uint64_t *res_postcopy_only)
420 {
421 DirtyBitmapMigBitmapState *dbms;
422 uint64_t pending = 0;
423
424 qemu_mutex_lock_iothread();
425
426 QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
427 uint64_t gran = bdrv_dirty_bitmap_granularity(dbms->bitmap);
428 uint64_t sectors = dbms->bulk_completed ? 0 :
429 dbms->total_sectors - dbms->cur_sector;
430
431 pending += DIV_ROUND_UP(sectors * BDRV_SECTOR_SIZE, gran);
432 }
433
434 qemu_mutex_unlock_iothread();
435
436 trace_dirty_bitmap_save_pending(pending, max_size);
437
438 *res_postcopy_only += pending;
439 }
440
441 /* First occurrence of this bitmap. It should be created if doesn't exist */
442 static int dirty_bitmap_load_start(QEMUFile *f, DirtyBitmapLoadState *s)
443 {
444 Error *local_err = NULL;
445 uint32_t granularity = qemu_get_be32(f);
446 uint8_t flags = qemu_get_byte(f);
447
448 if (s->bitmap) {
449 error_report("Bitmap with the same name ('%s') already exists on "
450 "destination", bdrv_dirty_bitmap_name(s->bitmap));
451 return -EINVAL;
452 } else {
453 s->bitmap = bdrv_create_dirty_bitmap(s->bs, granularity,
454 s->bitmap_name, &local_err);
455 if (!s->bitmap) {
456 error_report_err(local_err);
457 return -EINVAL;
458 }
459 }
460
461 if (flags & DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK) {
462 error_report("Unknown flags in migrated dirty bitmap header: %x",
463 flags);
464 return -EINVAL;
465 }
466
467 if (flags & DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT) {
468 bdrv_dirty_bitmap_set_persistence(s->bitmap, true);
469 }
470
471 bdrv_disable_dirty_bitmap(s->bitmap);
472 if (flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED) {
473 DirtyBitmapLoadBitmapState *b;
474
475 bdrv_dirty_bitmap_create_successor(s->bitmap, &local_err);
476 if (local_err) {
477 error_report_err(local_err);
478 return -EINVAL;
479 }
480
481 b = g_new(DirtyBitmapLoadBitmapState, 1);
482 b->bs = s->bs;
483 b->bitmap = s->bitmap;
484 b->migrated = false;
485 enabled_bitmaps = g_slist_prepend(enabled_bitmaps, b);
486 }
487
488 return 0;
489 }
490
491 void dirty_bitmap_mig_before_vm_start(void)
492 {
493 GSList *item;
494
495 qemu_mutex_lock(&finish_lock);
496
497 for (item = enabled_bitmaps; item; item = g_slist_next(item)) {
498 DirtyBitmapLoadBitmapState *b = item->data;
499
500 if (b->migrated) {
501 bdrv_enable_dirty_bitmap_locked(b->bitmap);
502 } else {
503 bdrv_dirty_bitmap_enable_successor(b->bitmap);
504 }
505
506 g_free(b);
507 }
508
509 g_slist_free(enabled_bitmaps);
510 enabled_bitmaps = NULL;
511
512 qemu_mutex_unlock(&finish_lock);
513 }
514
515 static void dirty_bitmap_load_complete(QEMUFile *f, DirtyBitmapLoadState *s)
516 {
517 GSList *item;
518 trace_dirty_bitmap_load_complete();
519 bdrv_dirty_bitmap_deserialize_finish(s->bitmap);
520
521 qemu_mutex_lock(&finish_lock);
522
523 for (item = enabled_bitmaps; item; item = g_slist_next(item)) {
524 DirtyBitmapLoadBitmapState *b = item->data;
525
526 if (b->bitmap == s->bitmap) {
527 b->migrated = true;
528 break;
529 }
530 }
531
532 if (bdrv_dirty_bitmap_has_successor(s->bitmap)) {
533 bdrv_dirty_bitmap_lock(s->bitmap);
534 if (enabled_bitmaps == NULL) {
535 /* in postcopy */
536 bdrv_reclaim_dirty_bitmap_locked(s->bitmap, &error_abort);
537 bdrv_enable_dirty_bitmap_locked(s->bitmap);
538 } else {
539 /* target not started, successor must be empty */
540 int64_t count = bdrv_get_dirty_count(s->bitmap);
541 BdrvDirtyBitmap *ret = bdrv_reclaim_dirty_bitmap_locked(s->bitmap,
542 NULL);
543 /* bdrv_reclaim_dirty_bitmap can fail only on no successor (it
544 * must be) or on merge fail, but merge can't fail when second
545 * bitmap is empty
546 */
547 assert(ret == s->bitmap &&
548 count == bdrv_get_dirty_count(s->bitmap));
549 }
550 bdrv_dirty_bitmap_unlock(s->bitmap);
551 }
552
553 qemu_mutex_unlock(&finish_lock);
554 }
555
556 static int dirty_bitmap_load_bits(QEMUFile *f, DirtyBitmapLoadState *s)
557 {
558 uint64_t first_byte = qemu_get_be64(f) << BDRV_SECTOR_BITS;
559 uint64_t nr_bytes = (uint64_t)qemu_get_be32(f) << BDRV_SECTOR_BITS;
560 trace_dirty_bitmap_load_bits_enter(first_byte >> BDRV_SECTOR_BITS,
561 nr_bytes >> BDRV_SECTOR_BITS);
562
563 if (s->flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
564 trace_dirty_bitmap_load_bits_zeroes();
565 bdrv_dirty_bitmap_deserialize_zeroes(s->bitmap, first_byte, nr_bytes,
566 false);
567 } else {
568 size_t ret;
569 uint8_t *buf;
570 uint64_t buf_size = qemu_get_be64(f);
571 uint64_t needed_size =
572 bdrv_dirty_bitmap_serialization_size(s->bitmap,
573 first_byte, nr_bytes);
574
575 if (needed_size > buf_size ||
576 buf_size > QEMU_ALIGN_UP(needed_size, 4 * sizeof(long))
577 /* Here used same alignment as in send_bitmap_bits */
578 ) {
579 error_report("Migrated bitmap granularity doesn't "
580 "match the destination bitmap '%s' granularity",
581 bdrv_dirty_bitmap_name(s->bitmap));
582 return -EINVAL;
583 }
584
585 buf = g_malloc(buf_size);
586 ret = qemu_get_buffer(f, buf, buf_size);
587 if (ret != buf_size) {
588 error_report("Failed to read bitmap bits");
589 g_free(buf);
590 return -EIO;
591 }
592
593 bdrv_dirty_bitmap_deserialize_part(s->bitmap, buf, first_byte, nr_bytes,
594 false);
595 g_free(buf);
596 }
597
598 return 0;
599 }
600
601 static int dirty_bitmap_load_header(QEMUFile *f, DirtyBitmapLoadState *s)
602 {
603 Error *local_err = NULL;
604 bool nothing;
605 s->flags = qemu_get_bitmap_flags(f);
606 trace_dirty_bitmap_load_header(s->flags);
607
608 nothing = s->flags == (s->flags & DIRTY_BITMAP_MIG_FLAG_EOS);
609
610 if (s->flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
611 if (!qemu_get_counted_string(f, s->node_name)) {
612 error_report("Unable to read node name string");
613 return -EINVAL;
614 }
615 s->bs = bdrv_lookup_bs(s->node_name, s->node_name, &local_err);
616 if (!s->bs) {
617 error_report_err(local_err);
618 return -EINVAL;
619 }
620 } else if (!s->bs && !nothing) {
621 error_report("Error: block device name is not set");
622 return -EINVAL;
623 }
624
625 if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
626 if (!qemu_get_counted_string(f, s->bitmap_name)) {
627 error_report("Unable to read bitmap name string");
628 return -EINVAL;
629 }
630 s->bitmap = bdrv_find_dirty_bitmap(s->bs, s->bitmap_name);
631
632 /* bitmap may be NULL here, it wouldn't be an error if it is the
633 * first occurrence of the bitmap */
634 if (!s->bitmap && !(s->flags & DIRTY_BITMAP_MIG_FLAG_START)) {
635 error_report("Error: unknown dirty bitmap "
636 "'%s' for block device '%s'",
637 s->bitmap_name, s->node_name);
638 return -EINVAL;
639 }
640 } else if (!s->bitmap && !nothing) {
641 error_report("Error: block device name is not set");
642 return -EINVAL;
643 }
644
645 return 0;
646 }
647
648 static int dirty_bitmap_load(QEMUFile *f, void *opaque, int version_id)
649 {
650 static DirtyBitmapLoadState s;
651 int ret = 0;
652
653 trace_dirty_bitmap_load_enter();
654
655 if (version_id != 1) {
656 return -EINVAL;
657 }
658
659 do {
660 ret = dirty_bitmap_load_header(f, &s);
661 if (ret < 0) {
662 return ret;
663 }
664
665 if (s.flags & DIRTY_BITMAP_MIG_FLAG_START) {
666 ret = dirty_bitmap_load_start(f, &s);
667 } else if (s.flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) {
668 dirty_bitmap_load_complete(f, &s);
669 } else if (s.flags & DIRTY_BITMAP_MIG_FLAG_BITS) {
670 ret = dirty_bitmap_load_bits(f, &s);
671 }
672
673 if (!ret) {
674 ret = qemu_file_get_error(f);
675 }
676
677 if (ret) {
678 return ret;
679 }
680 } while (!(s.flags & DIRTY_BITMAP_MIG_FLAG_EOS));
681
682 trace_dirty_bitmap_load_success();
683 return 0;
684 }
685
686 static int dirty_bitmap_save_setup(QEMUFile *f, void *opaque)
687 {
688 DirtyBitmapMigBitmapState *dbms = NULL;
689 if (init_dirty_bitmap_migration() < 0) {
690 return -1;
691 }
692
693 QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
694 send_bitmap_start(f, dbms);
695 }
696 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
697
698 return 0;
699 }
700
701 static bool dirty_bitmap_is_active(void *opaque)
702 {
703 return migrate_dirty_bitmaps() && !dirty_bitmap_mig_state.no_bitmaps;
704 }
705
706 static bool dirty_bitmap_is_active_iterate(void *opaque)
707 {
708 return dirty_bitmap_is_active(opaque) && !runstate_is_running();
709 }
710
711 static bool dirty_bitmap_has_postcopy(void *opaque)
712 {
713 return true;
714 }
715
716 static SaveVMHandlers savevm_dirty_bitmap_handlers = {
717 .save_setup = dirty_bitmap_save_setup,
718 .save_live_complete_postcopy = dirty_bitmap_save_complete,
719 .save_live_complete_precopy = dirty_bitmap_save_complete,
720 .has_postcopy = dirty_bitmap_has_postcopy,
721 .save_live_pending = dirty_bitmap_save_pending,
722 .save_live_iterate = dirty_bitmap_save_iterate,
723 .is_active_iterate = dirty_bitmap_is_active_iterate,
724 .load_state = dirty_bitmap_load,
725 .save_cleanup = dirty_bitmap_save_cleanup,
726 .is_active = dirty_bitmap_is_active,
727 };
728
729 void dirty_bitmap_mig_init(void)
730 {
731 QSIMPLEQ_INIT(&dirty_bitmap_mig_state.dbms_list);
732
733 register_savevm_live("dirty-bitmap", 0, 1,
734 &savevm_dirty_bitmap_handlers,
735 &dirty_bitmap_mig_state);
736 }