block-dirty-bitmap.c 23.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/*
 * Block dirty bitmap postcopy migration
 *
 * Copyright IBM, Corp. 2009
 * Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
 *
 * Authors:
 *  Liran Schour   <lirans@il.ibm.com>
 *  Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 * This file is derived from migration/block.c, so it's author and IBM copyright
 * are here, although content is quite different.
 *
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
 *
 *                                ***
 *
 * Here postcopy migration of dirty bitmaps is realized. Only QMP-addressable
 * bitmaps are migrated.
 *
 * Bitmap migration implies creating bitmap with the same name and granularity
 * in destination QEMU. If the bitmap with the same name (for the same node)
 * already exists on destination an error will be generated.
 *
 * format of migration:
 *
 * # Header (shared for different chunk types)
 * 1, 2 or 4 bytes: flags (see qemu_{put,put}_flags)
 * [ 1 byte: node name size ] \  flags & DEVICE_NAME
 * [ n bytes: node name     ] /
 * [ 1 byte: bitmap name size ] \  flags & BITMAP_NAME
 * [ n bytes: bitmap name     ] /
 *
 * # Start of bitmap migration (flags & START)
 * header
 * be64: granularity
 * 1 byte: bitmap flags (corresponds to BdrvDirtyBitmap)
 *   bit 0    -  bitmap is enabled
 *   bit 1    -  bitmap is persistent
 *   bit 2    -  bitmap is autoloading
 *   bits 3-7 - reserved, must be zero
 *
 * # Complete of bitmap migration (flags & COMPLETE)
 * header
 *
 * # Data chunk of bitmap migration
 * header
 * be64: start sector
 * be32: number of sectors
 * [ be64: buffer size  ] \ ! (flags & ZEROES)
 * [ n bytes: buffer    ] /
 *
 * The last chunk in stream should contain flags & EOS. The chunk may skip
 * device and/or bitmap names, assuming them to be the same with the previous
 * chunk.
 */

#include "qemu/osdep.h"
#include "block/block.h"
#include "block/block_int.h"
#include "sysemu/block-backend.h"
65
#include "sysemu/runstate.h"
66 67 68 69
#include "qemu/main-loop.h"
#include "qemu/error-report.h"
#include "migration/misc.h"
#include "migration/migration.h"
70
#include "qemu-file.h"
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#include "migration/vmstate.h"
#include "migration/register.h"
#include "qemu/hbitmap.h"
#include "qemu/cutils.h"
#include "qapi/error.h"
#include "trace.h"

#define CHUNK_SIZE     (1 << 10)

/* Flags occupy one, two or four bytes (Big Endian). The size is determined as
 * follows:
 * in first (most significant) byte bit 8 is clear  -->  one byte
 * in first byte bit 8 is set    -->  two or four bytes, depending on second
 *                                    byte:
 *    | in second byte bit 8 is clear  -->  two bytes
 *    | in second byte bit 8 is set    -->  four bytes
 */
#define DIRTY_BITMAP_MIG_FLAG_EOS           0x01
#define DIRTY_BITMAP_MIG_FLAG_ZEROES        0x02
#define DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME   0x04
#define DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME   0x08
#define DIRTY_BITMAP_MIG_FLAG_START         0x10
#define DIRTY_BITMAP_MIG_FLAG_COMPLETE      0x20
#define DIRTY_BITMAP_MIG_FLAG_BITS          0x40

#define DIRTY_BITMAP_MIG_EXTRA_FLAGS        0x80

#define DIRTY_BITMAP_MIG_START_FLAG_ENABLED          0x01
#define DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT       0x02
/* 0x04 was "AUTOLOAD" flags on elder versions, no it is ignored */
#define DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK    0xf8

103 104
/* State of one bitmap during save process */
typedef struct SaveBitmapState {
105 106 107 108 109 110
    /* Written during setup phase. */
    BlockDriverState *bs;
    const char *node_name;
    BdrvDirtyBitmap *bitmap;
    uint64_t total_sectors;
    uint64_t sectors_per_chunk;
111
    QSIMPLEQ_ENTRY(SaveBitmapState) entry;
112 113 114 115 116
    uint8_t flags;

    /* For bulk phase. */
    bool bulk_completed;
    uint64_t cur_sector;
117
} SaveBitmapState;
118

119 120 121
/* State of the dirty bitmap migration (DBM) during save process */
typedef struct DBMSaveState {
    QSIMPLEQ_HEAD(, SaveBitmapState) dbms_list;
122 123 124 125 126 127 128

    bool bulk_completed;
    bool no_bitmaps;

    /* for send_bitmap_bits() */
    BlockDriverState *prev_bs;
    BdrvDirtyBitmap *prev_bitmap;
129
} DBMSaveState;
130

131 132 133 134 135 136
typedef struct LoadBitmapState {
    BlockDriverState *bs;
    BdrvDirtyBitmap *bitmap;
    bool migrated;
} LoadBitmapState;

137 138
/* State of the dirty bitmap migration (DBM) during load process */
typedef struct DBMLoadState {
139 140 141 142 143
    uint32_t flags;
    char node_name[256];
    char bitmap_name[256];
    BlockDriverState *bs;
    BdrvDirtyBitmap *bitmap;
144 145

    GSList *enabled_bitmaps;
146
    QemuMutex lock; /* protect enabled_bitmaps */
147
} DBMLoadState;
148

149 150 151 152
typedef struct DBMState {
    DBMSaveState save;
    DBMLoadState load;
} DBMState;
153

154
static DBMState dbm_state;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

static uint32_t qemu_get_bitmap_flags(QEMUFile *f)
{
    uint8_t flags = qemu_get_byte(f);
    if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
        flags = flags << 8 | qemu_get_byte(f);
        if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
            flags = flags << 16 | qemu_get_be16(f);
        }
    }

    return flags;
}

static void qemu_put_bitmap_flags(QEMUFile *f, uint32_t flags)
{
    /* The code currently do not send flags more than one byte */
    assert(!(flags & (0xffffff00 | DIRTY_BITMAP_MIG_EXTRA_FLAGS)));

    qemu_put_byte(f, flags);
}

177 178
static void send_bitmap_header(QEMUFile *f, DBMSaveState *s,
                               SaveBitmapState *dbms, uint32_t additional_flags)
179 180 181 182 183 184
{
    BlockDriverState *bs = dbms->bs;
    BdrvDirtyBitmap *bitmap = dbms->bitmap;
    uint32_t flags = additional_flags;
    trace_send_bitmap_header_enter();

185 186
    if (bs != s->prev_bs) {
        s->prev_bs = bs;
187 188 189
        flags |= DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME;
    }

190 191
    if (bitmap != s->prev_bitmap) {
        s->prev_bitmap = bitmap;
192 193 194 195 196 197 198 199 200 201 202 203 204 205
        flags |= DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME;
    }

    qemu_put_bitmap_flags(f, flags);

    if (flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
        qemu_put_counted_string(f, dbms->node_name);
    }

    if (flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
        qemu_put_counted_string(f, bdrv_dirty_bitmap_name(bitmap));
    }
}

206 207
static void send_bitmap_start(QEMUFile *f, DBMSaveState *s,
                              SaveBitmapState *dbms)
208
{
209
    send_bitmap_header(f, s, dbms, DIRTY_BITMAP_MIG_FLAG_START);
210 211 212 213
    qemu_put_be32(f, bdrv_dirty_bitmap_granularity(dbms->bitmap));
    qemu_put_byte(f, dbms->flags);
}

214 215
static void send_bitmap_complete(QEMUFile *f, DBMSaveState *s,
                                 SaveBitmapState *dbms)
216
{
217
    send_bitmap_header(f, s, dbms, DIRTY_BITMAP_MIG_FLAG_COMPLETE);
218 219
}

220 221
static void send_bitmap_bits(QEMUFile *f, DBMSaveState *s,
                             SaveBitmapState *dbms,
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
                             uint64_t start_sector, uint32_t nr_sectors)
{
    /* align for buffer_is_zero() */
    uint64_t align = 4 * sizeof(long);
    uint64_t unaligned_size =
        bdrv_dirty_bitmap_serialization_size(
            dbms->bitmap, start_sector << BDRV_SECTOR_BITS,
            (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
    uint64_t buf_size = QEMU_ALIGN_UP(unaligned_size, align);
    uint8_t *buf = g_malloc0(buf_size);
    uint32_t flags = DIRTY_BITMAP_MIG_FLAG_BITS;

    bdrv_dirty_bitmap_serialize_part(
        dbms->bitmap, buf, start_sector << BDRV_SECTOR_BITS,
        (uint64_t)nr_sectors << BDRV_SECTOR_BITS);

    if (buffer_is_zero(buf, buf_size)) {
        g_free(buf);
        buf = NULL;
        flags |= DIRTY_BITMAP_MIG_FLAG_ZEROES;
    }

    trace_send_bitmap_bits(flags, start_sector, nr_sectors, buf_size);

246
    send_bitmap_header(f, s, dbms, flags);
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

    qemu_put_be64(f, start_sector);
    qemu_put_be32(f, nr_sectors);

    /* if a block is zero we need to flush here since the network
     * bandwidth is now a lot higher than the storage device bandwidth.
     * thus if we queue zero blocks we slow down the migration. */
    if (flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
        qemu_fflush(f);
    } else {
        qemu_put_be64(f, buf_size);
        qemu_put_buffer(f, buf, buf_size);
    }

    g_free(buf);
}

/* Called with iothread lock taken.  */
265
static void dirty_bitmap_do_save_cleanup(DBMSaveState *s)
266
{
267
    SaveBitmapState *dbms;
268

269 270
    while ((dbms = QSIMPLEQ_FIRST(&s->dbms_list)) != NULL) {
        QSIMPLEQ_REMOVE_HEAD(&s->dbms_list, entry);
271
        bdrv_dirty_bitmap_set_busy(dbms->bitmap, false);
272 273 274 275 276 277
        bdrv_unref(dbms->bs);
        g_free(dbms);
    }
}

/* Called with iothread lock taken. */
278 279
static int add_bitmaps_to_list(DBMSaveState *s, BlockDriverState *bs,
                               const char *bs_name)
280 281
{
    BdrvDirtyBitmap *bitmap;
282
    SaveBitmapState *dbms;
283
    Error *local_err = NULL;
284

285 286 287 288 289
    FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
        if (bdrv_dirty_bitmap_name(bitmap)) {
            break;
        }
    }
290 291 292 293 294 295 296 297 298 299
    if (!bitmap) {
        return 0;
    }

    if (!bs_name || strcmp(bs_name, "") == 0) {
        error_report("Bitmap '%s' in unnamed node can't be migrated",
                     bdrv_dirty_bitmap_name(bitmap));
        return -1;
    }

300 301 302 303 304 305 306
    if (bs_name[0] == '#') {
        error_report("Bitmap '%s' in a node with auto-generated "
                     "name '%s' can't be migrated",
                     bdrv_dirty_bitmap_name(bitmap), bs_name);
        return -1;
    }

307 308 309 310 311 312 313 314 315 316 317 318 319
    FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
        if (!bdrv_dirty_bitmap_name(bitmap)) {
            continue;
        }

        if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, &local_err)) {
            error_report_err(local_err);
            return -1;
        }

        bdrv_ref(bs);
        bdrv_dirty_bitmap_set_busy(bitmap, true);

320
        dbms = g_new0(SaveBitmapState, 1);
321 322 323 324 325 326 327 328 329 330 331 332 333
        dbms->bs = bs;
        dbms->node_name = bs_name;
        dbms->bitmap = bitmap;
        dbms->total_sectors = bdrv_nb_sectors(bs);
        dbms->sectors_per_chunk = CHUNK_SIZE * 8 *
            bdrv_dirty_bitmap_granularity(bitmap) >> BDRV_SECTOR_BITS;
        if (bdrv_dirty_bitmap_enabled(bitmap)) {
            dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
        }
        if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
            dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT;
        }

334
        QSIMPLEQ_INSERT_TAIL(&s->dbms_list, dbms, entry);
335 336 337 338 339 340
    }

    return 0;
}

/* Called with iothread lock taken. */
341
static int init_dirty_bitmap_migration(DBMSaveState *s)
342 343
{
    BlockDriverState *bs;
344
    SaveBitmapState *dbms;
345 346
    GHashTable *handled_by_blk = g_hash_table_new(NULL, NULL);
    BlockBackend *blk;
347

348 349 350 351
    s->bulk_completed = false;
    s->prev_bs = NULL;
    s->prev_bitmap = NULL;
    s->no_bitmaps = false;
352

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
    /*
     * Use blockdevice name for direct (or filtered) children of named block
     * backends.
     */
    for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
        const char *name = blk_name(blk);

        if (!name || strcmp(name, "") == 0) {
            continue;
        }

        bs = blk_bs(blk);

        /* Skip filters without bitmaps */
        while (bs && bs->drv && bs->drv->is_filter &&
               !bdrv_has_named_bitmaps(bs))
        {
            if (bs->backing) {
                bs = bs->backing->bs;
            } else if (bs->file) {
                bs = bs->file->bs;
            } else {
                bs = NULL;
            }
        }

        if (bs && bs->drv && !bs->drv->is_filter) {
380
            if (add_bitmaps_to_list(s, bs, name)) {
381 382 383 384 385 386
                goto fail;
            }
            g_hash_table_add(handled_by_blk, bs);
        }
    }

387
    for (bs = bdrv_next_all_states(NULL); bs; bs = bdrv_next_all_states(bs)) {
388 389 390 391
        if (g_hash_table_contains(handled_by_blk, bs)) {
            continue;
        }

392
        if (add_bitmaps_to_list(s, bs, bdrv_get_node_name(bs))) {
393
            goto fail;
394 395 396
        }
    }

397
    /* unset migration flags here, to not roll back it */
398
    QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
399
        bdrv_dirty_bitmap_skip_store(dbms->bitmap, true);
400 401
    }

402 403
    if (QSIMPLEQ_EMPTY(&s->dbms_list)) {
        s->no_bitmaps = true;
404 405
    }

406 407
    g_hash_table_destroy(handled_by_blk);

408 409 410
    return 0;

fail:
411
    g_hash_table_destroy(handled_by_blk);
412
    dirty_bitmap_do_save_cleanup(s);
413 414 415 416 417

    return -1;
}

/* Called with no lock taken.  */
418 419
static void bulk_phase_send_chunk(QEMUFile *f, DBMSaveState *s,
                                  SaveBitmapState *dbms)
420 421 422 423
{
    uint32_t nr_sectors = MIN(dbms->total_sectors - dbms->cur_sector,
                             dbms->sectors_per_chunk);

424
    send_bitmap_bits(f, s, dbms, dbms->cur_sector, nr_sectors);
425 426 427 428 429 430 431 432

    dbms->cur_sector += nr_sectors;
    if (dbms->cur_sector >= dbms->total_sectors) {
        dbms->bulk_completed = true;
    }
}

/* Called with no lock taken.  */
433
static void bulk_phase(QEMUFile *f, DBMSaveState *s, bool limit)
434
{
435
    SaveBitmapState *dbms;
436

437
    QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
438
        while (!dbms->bulk_completed) {
439
            bulk_phase_send_chunk(f, s, dbms);
440 441 442 443 444 445
            if (limit && qemu_file_rate_limit(f)) {
                return;
            }
        }
    }

446
    s->bulk_completed = true;
447 448 449 450 451
}

/* for SaveVMHandlers */
static void dirty_bitmap_save_cleanup(void *opaque)
{
452 453 454
    DBMSaveState *s = &((DBMState *)opaque)->save;

    dirty_bitmap_do_save_cleanup(s);
455 456 457 458
}

static int dirty_bitmap_save_iterate(QEMUFile *f, void *opaque)
{
459 460
    DBMSaveState *s = &((DBMState *)opaque)->save;

461 462
    trace_dirty_bitmap_save_iterate(migration_in_postcopy());

463 464
    if (migration_in_postcopy() && !s->bulk_completed) {
        bulk_phase(f, s, true);
465 466 467 468
    }

    qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);

469
    return s->bulk_completed;
470 471 472 473 474 475
}

/* Called with iothread lock taken.  */

static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque)
{
476
    DBMSaveState *s = &((DBMState *)opaque)->save;
477
    SaveBitmapState *dbms;
478 479
    trace_dirty_bitmap_save_complete_enter();

480 481
    if (!s->bulk_completed) {
        bulk_phase(f, s, false);
482 483
    }

484 485
    QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
        send_bitmap_complete(f, s, dbms);
486 487 488 489 490 491
    }

    qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);

    trace_dirty_bitmap_save_complete_finish();

492
    dirty_bitmap_save_cleanup(opaque);
493 494 495 496 497 498 499 500 501
    return 0;
}

static void dirty_bitmap_save_pending(QEMUFile *f, void *opaque,
                                      uint64_t max_size,
                                      uint64_t *res_precopy_only,
                                      uint64_t *res_compatible,
                                      uint64_t *res_postcopy_only)
{
502
    DBMSaveState *s = &((DBMState *)opaque)->save;
503
    SaveBitmapState *dbms;
504 505 506 507
    uint64_t pending = 0;

    qemu_mutex_lock_iothread();

508
    QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
        uint64_t gran = bdrv_dirty_bitmap_granularity(dbms->bitmap);
        uint64_t sectors = dbms->bulk_completed ? 0 :
                           dbms->total_sectors - dbms->cur_sector;

        pending += DIV_ROUND_UP(sectors * BDRV_SECTOR_SIZE, gran);
    }

    qemu_mutex_unlock_iothread();

    trace_dirty_bitmap_save_pending(pending, max_size);

    *res_postcopy_only += pending;
}

/* First occurrence of this bitmap. It should be created if doesn't exist */
524
static int dirty_bitmap_load_start(QEMUFile *f, DBMLoadState *s)
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
{
    Error *local_err = NULL;
    uint32_t granularity = qemu_get_be32(f);
    uint8_t flags = qemu_get_byte(f);

    if (s->bitmap) {
        error_report("Bitmap with the same name ('%s') already exists on "
                     "destination", bdrv_dirty_bitmap_name(s->bitmap));
        return -EINVAL;
    } else {
        s->bitmap = bdrv_create_dirty_bitmap(s->bs, granularity,
                                             s->bitmap_name, &local_err);
        if (!s->bitmap) {
            error_report_err(local_err);
            return -EINVAL;
        }
    }

    if (flags & DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK) {
        error_report("Unknown flags in migrated dirty bitmap header: %x",
                     flags);
        return -EINVAL;
    }

    if (flags & DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT) {
E
Eric Blake 已提交
550
        bdrv_dirty_bitmap_set_persistence(s->bitmap, true);
551 552 553 554
    }

    bdrv_disable_dirty_bitmap(s->bitmap);
    if (flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED) {
555
        LoadBitmapState *b;
556

557
        bdrv_dirty_bitmap_create_successor(s->bitmap, &local_err);
558 559 560 561 562
        if (local_err) {
            error_report_err(local_err);
            return -EINVAL;
        }

563
        b = g_new(LoadBitmapState, 1);
564 565 566
        b->bs = s->bs;
        b->bitmap = s->bitmap;
        b->migrated = false;
567
        s->enabled_bitmaps = g_slist_prepend(s->enabled_bitmaps, b);
568 569 570 571 572 573 574
    }

    return 0;
}

void dirty_bitmap_mig_before_vm_start(void)
{
575
    DBMLoadState *s = &dbm_state.load;
576 577
    GSList *item;

578
    qemu_mutex_lock(&s->lock);
579

580
    for (item = s->enabled_bitmaps; item; item = g_slist_next(item)) {
581
        LoadBitmapState *b = item->data;
582 583

        if (b->migrated) {
584
            bdrv_enable_dirty_bitmap(b->bitmap);
585 586 587 588 589 590 591
        } else {
            bdrv_dirty_bitmap_enable_successor(b->bitmap);
        }

        g_free(b);
    }

592 593
    g_slist_free(s->enabled_bitmaps);
    s->enabled_bitmaps = NULL;
594

595
    qemu_mutex_unlock(&s->lock);
596 597
}

598
static void dirty_bitmap_load_complete(QEMUFile *f, DBMLoadState *s)
599 600 601 602 603
{
    GSList *item;
    trace_dirty_bitmap_load_complete();
    bdrv_dirty_bitmap_deserialize_finish(s->bitmap);

604
    qemu_mutex_lock(&s->lock);
605

606 607 608 609
    if (bdrv_dirty_bitmap_has_successor(s->bitmap)) {
        bdrv_reclaim_dirty_bitmap(s->bitmap, &error_abort);
    }

610
    for (item = s->enabled_bitmaps; item; item = g_slist_next(item)) {
611
        LoadBitmapState *b = item->data;
612 613 614 615 616 617 618

        if (b->bitmap == s->bitmap) {
            b->migrated = true;
            break;
        }
    }

619
    qemu_mutex_unlock(&s->lock);
620 621
}

622
static int dirty_bitmap_load_bits(QEMUFile *f, DBMLoadState *s)
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
{
    uint64_t first_byte = qemu_get_be64(f) << BDRV_SECTOR_BITS;
    uint64_t nr_bytes = (uint64_t)qemu_get_be32(f) << BDRV_SECTOR_BITS;
    trace_dirty_bitmap_load_bits_enter(first_byte >> BDRV_SECTOR_BITS,
                                       nr_bytes >> BDRV_SECTOR_BITS);

    if (s->flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
        trace_dirty_bitmap_load_bits_zeroes();
        bdrv_dirty_bitmap_deserialize_zeroes(s->bitmap, first_byte, nr_bytes,
                                             false);
    } else {
        size_t ret;
        uint8_t *buf;
        uint64_t buf_size = qemu_get_be64(f);
        uint64_t needed_size =
            bdrv_dirty_bitmap_serialization_size(s->bitmap,
                                                 first_byte, nr_bytes);

        if (needed_size > buf_size ||
            buf_size > QEMU_ALIGN_UP(needed_size, 4 * sizeof(long))
             /* Here used same alignment as in send_bitmap_bits */
        ) {
            error_report("Migrated bitmap granularity doesn't "
                         "match the destination bitmap '%s' granularity",
                         bdrv_dirty_bitmap_name(s->bitmap));
            return -EINVAL;
        }

        buf = g_malloc(buf_size);
        ret = qemu_get_buffer(f, buf, buf_size);
        if (ret != buf_size) {
            error_report("Failed to read bitmap bits");
655
            g_free(buf);
656 657 658 659 660 661 662 663 664 665 666
            return -EIO;
        }

        bdrv_dirty_bitmap_deserialize_part(s->bitmap, buf, first_byte, nr_bytes,
                                           false);
        g_free(buf);
    }

    return 0;
}

667
static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s)
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
{
    Error *local_err = NULL;
    bool nothing;
    s->flags = qemu_get_bitmap_flags(f);
    trace_dirty_bitmap_load_header(s->flags);

    nothing = s->flags == (s->flags & DIRTY_BITMAP_MIG_FLAG_EOS);

    if (s->flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
        if (!qemu_get_counted_string(f, s->node_name)) {
            error_report("Unable to read node name string");
            return -EINVAL;
        }
        s->bs = bdrv_lookup_bs(s->node_name, s->node_name, &local_err);
        if (!s->bs) {
            error_report_err(local_err);
            return -EINVAL;
        }
    } else if (!s->bs && !nothing) {
        error_report("Error: block device name is not set");
        return -EINVAL;
    }

    if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
        if (!qemu_get_counted_string(f, s->bitmap_name)) {
            error_report("Unable to read bitmap name string");
            return -EINVAL;
        }
        s->bitmap = bdrv_find_dirty_bitmap(s->bs, s->bitmap_name);

        /* bitmap may be NULL here, it wouldn't be an error if it is the
         * first occurrence of the bitmap */
        if (!s->bitmap && !(s->flags & DIRTY_BITMAP_MIG_FLAG_START)) {
            error_report("Error: unknown dirty bitmap "
                         "'%s' for block device '%s'",
                         s->bitmap_name, s->node_name);
            return -EINVAL;
        }
    } else if (!s->bitmap && !nothing) {
        error_report("Error: block device name is not set");
        return -EINVAL;
    }

    return 0;
}

static int dirty_bitmap_load(QEMUFile *f, void *opaque, int version_id)
{
716
    DBMLoadState *s = &((DBMState *)opaque)->load;
717 718 719 720 721 722 723 724 725
    int ret = 0;

    trace_dirty_bitmap_load_enter();

    if (version_id != 1) {
        return -EINVAL;
    }

    do {
726
        ret = dirty_bitmap_load_header(f, s);
727 728 729
        if (ret < 0) {
            return ret;
        }
730

731 732 733 734 735 736
        if (s->flags & DIRTY_BITMAP_MIG_FLAG_START) {
            ret = dirty_bitmap_load_start(f, s);
        } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) {
            dirty_bitmap_load_complete(f, s);
        } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITS) {
            ret = dirty_bitmap_load_bits(f, s);
737 738 739 740 741 742 743 744 745
        }

        if (!ret) {
            ret = qemu_file_get_error(f);
        }

        if (ret) {
            return ret;
        }
746
    } while (!(s->flags & DIRTY_BITMAP_MIG_FLAG_EOS));
747 748 749 750 751 752 753

    trace_dirty_bitmap_load_success();
    return 0;
}

static int dirty_bitmap_save_setup(QEMUFile *f, void *opaque)
{
754
    DBMSaveState *s = &((DBMState *)opaque)->save;
755
    SaveBitmapState *dbms = NULL;
756
    if (init_dirty_bitmap_migration(s) < 0) {
757 758 759
        return -1;
    }

760 761
    QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
        send_bitmap_start(f, s, dbms);
762 763 764 765 766 767 768 769
    }
    qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);

    return 0;
}

static bool dirty_bitmap_is_active(void *opaque)
{
770 771 772
    DBMSaveState *s = &((DBMState *)opaque)->save;

    return migrate_dirty_bitmaps() && !s->no_bitmaps;
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
}

static bool dirty_bitmap_is_active_iterate(void *opaque)
{
    return dirty_bitmap_is_active(opaque) && !runstate_is_running();
}

static bool dirty_bitmap_has_postcopy(void *opaque)
{
    return true;
}

static SaveVMHandlers savevm_dirty_bitmap_handlers = {
    .save_setup = dirty_bitmap_save_setup,
    .save_live_complete_postcopy = dirty_bitmap_save_complete,
    .save_live_complete_precopy = dirty_bitmap_save_complete,
    .has_postcopy = dirty_bitmap_has_postcopy,
    .save_live_pending = dirty_bitmap_save_pending,
    .save_live_iterate = dirty_bitmap_save_iterate,
    .is_active_iterate = dirty_bitmap_is_active_iterate,
    .load_state = dirty_bitmap_load,
    .save_cleanup = dirty_bitmap_save_cleanup,
    .is_active = dirty_bitmap_is_active,
};

void dirty_bitmap_mig_init(void)
{
800
    QSIMPLEQ_INIT(&dbm_state.save.dbms_list);
801
    qemu_mutex_init(&dbm_state.load.lock);
802

803
    register_savevm_live("dirty-bitmap", 0, 1,
804
                         &savevm_dirty_bitmap_handlers,
805
                         &dbm_state);
806
}