block.c 26.0 KB
Newer Older
L
lirans@il.ibm.com 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * QEMU live block migration
 *
 * Copyright IBM, Corp. 2009
 *
 * Authors:
 *  Liran Schour   <lirans@il.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
12 13
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
L
lirans@il.ibm.com 已提交
14 15
 */

P
Peter Maydell 已提交
16
#include "qemu/osdep.h"
17
#include "qapi/error.h"
L
lirans@il.ibm.com 已提交
18
#include "qemu-common.h"
19 20 21
#include "block/block.h"
#include "qemu/error-report.h"
#include "qemu/main-loop.h"
L
lirans@il.ibm.com 已提交
22
#include "hw/hw.h"
23 24
#include "qemu/queue.h"
#include "qemu/timer.h"
25 26
#include "migration/block.h"
#include "migration/migration.h"
27
#include "sysemu/blockdev.h"
28
#include "sysemu/block-backend.h"
L
lirans@il.ibm.com 已提交
29

30 31
#define BLOCK_SIZE                       (1 << 20)
#define BDRV_SECTORS_PER_DIRTY_CHUNK     (BLOCK_SIZE >> BDRV_SECTOR_BITS)
L
lirans@il.ibm.com 已提交
32 33 34

#define BLK_MIG_FLAG_DEVICE_BLOCK       0x01
#define BLK_MIG_FLAG_EOS                0x02
35
#define BLK_MIG_FLAG_PROGRESS           0x04
36
#define BLK_MIG_FLAG_ZERO_BLOCK         0x08
L
lirans@il.ibm.com 已提交
37 38 39

#define MAX_IS_ALLOCATED_SEARCH 65536

40 41
#define MAX_INFLIGHT_IO 512

L
lirans@il.ibm.com 已提交
42 43 44
//#define DEBUG_BLK_MIGRATION

#ifdef DEBUG_BLK_MIGRATION
M
malc 已提交
45
#define DPRINTF(fmt, ...) \
L
lirans@il.ibm.com 已提交
46 47
    do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
#else
M
malc 已提交
48
#define DPRINTF(fmt, ...) \
L
lirans@il.ibm.com 已提交
49 50 51
    do { } while (0)
#endif

52
typedef struct BlkMigDevState {
53
    /* Written during setup phase.  Can be read without a lock.  */
54 55 56
    BlockDriverState *bs;
    int shared_base;
    int64_t total_sectors;
57
    QSIMPLEQ_ENTRY(BlkMigDevState) entry;
58
    Error *blocker;
59 60 61 62 63 64

    /* Only used by migration thread.  Does not need a lock.  */
    int bulk_completed;
    int64_t cur_sector;
    int64_t cur_dirty;

65 66 67
    /* Data in the aio_bitmap is protected by block migration lock.
     * Allocation and free happen during setup and cleanup respectively.
     */
68
    unsigned long *aio_bitmap;
69 70

    /* Protected by block migration lock.  */
71
    int64_t completed_sectors;
72 73 74 75

    /* During migration this is protected by iothread lock / AioContext.
     * Allocation and free happen during setup and cleanup respectively.
     */
F
Fam Zheng 已提交
76
    BdrvDirtyBitmap *dirty_bitmap;
77 78
} BlkMigDevState;

L
lirans@il.ibm.com 已提交
79
typedef struct BlkMigBlock {
80
    /* Only used by migration thread.  */
L
lirans@il.ibm.com 已提交
81 82 83
    uint8_t *buf;
    BlkMigDevState *bmds;
    int64_t sector;
84
    int nr_sectors;
L
lirans@il.ibm.com 已提交
85 86
    struct iovec iov;
    QEMUIOVector qiov;
87
    BlockAIOCB *aiocb;
88

P
Paolo Bonzini 已提交
89
    /* Protected by block migration lock.  */
L
lirans@il.ibm.com 已提交
90
    int ret;
91
    QSIMPLEQ_ENTRY(BlkMigBlock) entry;
L
lirans@il.ibm.com 已提交
92 93 94
} BlkMigBlock;

typedef struct BlkMigState {
95
    /* Written during setup phase.  Can be read without a lock.  */
L
lirans@il.ibm.com 已提交
96 97
    int blk_enable;
    int shared_base;
98
    QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
99
    int64_t total_sector_sum;
100
    bool zero_blocks;
101

P
Paolo Bonzini 已提交
102
    /* Protected by lock.  */
103
    QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
L
lirans@il.ibm.com 已提交
104 105
    int submitted;
    int read_done;
106 107

    /* Only used by migration thread.  Does not need a lock.  */
L
lirans@il.ibm.com 已提交
108
    int transferred;
109
    int prev_progress;
L
Liran Schour 已提交
110
    int bulk_completed;
P
Paolo Bonzini 已提交
111

112
    /* Lock must be taken _inside_ the iothread lock and any AioContexts.  */
P
Paolo Bonzini 已提交
113
    QemuMutex lock;
L
lirans@il.ibm.com 已提交
114 115
} BlkMigState;

116
static BlkMigState block_mig_state;
L
lirans@il.ibm.com 已提交
117

P
Paolo Bonzini 已提交
118 119 120 121 122 123 124 125 126 127
static void blk_mig_lock(void)
{
    qemu_mutex_lock(&block_mig_state.lock);
}

static void blk_mig_unlock(void)
{
    qemu_mutex_unlock(&block_mig_state.lock);
}

128 129 130 131
/* Must run outside of the iothread lock during the bulk phase,
 * or the VM will stall.
 */

132 133 134
static void blk_send(QEMUFile *f, BlkMigBlock * blk)
{
    int len;
135 136 137 138 139 140
    uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK;

    if (block_mig_state.zero_blocks &&
        buffer_is_zero(blk->buf, BLOCK_SIZE)) {
        flags |= BLK_MIG_FLAG_ZERO_BLOCK;
    }
141 142 143

    /* sector number and flags */
    qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
144
                     | flags);
145 146

    /* device name */
147
    len = strlen(bdrv_get_device_name(blk->bmds->bs));
148
    qemu_put_byte(f, len);
149
    qemu_put_buffer(f, (uint8_t *)bdrv_get_device_name(blk->bmds->bs), len);
150

151 152 153 154 155 156 157 158
    /* 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 & BLK_MIG_FLAG_ZERO_BLOCK) {
        qemu_fflush(f);
        return;
    }

159 160 161
    qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
}

162 163 164 165 166 167 168 169 170 171
int blk_mig_active(void)
{
    return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
}

uint64_t blk_mig_bytes_transferred(void)
{
    BlkMigDevState *bmds;
    uint64_t sum = 0;

P
Paolo Bonzini 已提交
172
    blk_mig_lock();
173 174 175
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
        sum += bmds->completed_sectors;
    }
P
Paolo Bonzini 已提交
176
    blk_mig_unlock();
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    return sum << BDRV_SECTOR_BITS;
}

uint64_t blk_mig_bytes_remaining(void)
{
    return blk_mig_bytes_total() - blk_mig_bytes_transferred();
}

uint64_t blk_mig_bytes_total(void)
{
    BlkMigDevState *bmds;
    uint64_t sum = 0;

    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
        sum += bmds->total_sectors;
    }
    return sum << BDRV_SECTOR_BITS;
}

P
Paolo Bonzini 已提交
196 197 198

/* Called with migration lock held.  */

199 200 201 202
static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
{
    int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;

203
    if (sector < bdrv_nb_sectors(bmds->bs)) {
204 205 206 207 208 209 210
        return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
            (1UL << (chunk % (sizeof(unsigned long) * 8))));
    } else {
        return 0;
    }
}

P
Paolo Bonzini 已提交
211 212
/* Called with migration lock held.  */

213 214 215 216 217 218 219 220 221 222 223 224 225 226
static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
                             int nb_sectors, int set)
{
    int64_t start, end;
    unsigned long val, idx, bit;

    start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
    end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;

    for (; start <= end; start++) {
        idx = start / (sizeof(unsigned long) * 8);
        bit = start % (sizeof(unsigned long) * 8);
        val = bmds->aio_bitmap[idx];
        if (set) {
227
            val |= 1UL << bit;
228
        } else {
229
            val &= ~(1UL << bit);
230 231 232 233 234 235 236 237 238 239
        }
        bmds->aio_bitmap[idx] = val;
    }
}

static void alloc_aio_bitmap(BlkMigDevState *bmds)
{
    BlockDriverState *bs = bmds->bs;
    int64_t bitmap_size;

240
    bitmap_size = bdrv_nb_sectors(bs) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
241 242
    bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;

243
    bmds->aio_bitmap = g_malloc0(bitmap_size);
244 245
}

P
Paolo Bonzini 已提交
246 247
/* Never hold migration lock when yielding to the main loop!  */

L
lirans@il.ibm.com 已提交
248 249 250
static void blk_mig_read_cb(void *opaque, int ret)
{
    BlkMigBlock *blk = opaque;
251

P
Paolo Bonzini 已提交
252
    blk_mig_lock();
L
lirans@il.ibm.com 已提交
253
    blk->ret = ret;
254

255
    QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
256
    bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
257

258 259 260
    block_mig_state.submitted--;
    block_mig_state.read_done++;
    assert(block_mig_state.submitted >= 0);
P
Paolo Bonzini 已提交
261
    blk_mig_unlock();
L
lirans@il.ibm.com 已提交
262 263
}

264 265
/* Called with no lock taken.  */

266
static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
267
{
268 269 270
    int64_t total_sectors = bmds->total_sectors;
    int64_t cur_sector = bmds->cur_sector;
    BlockDriverState *bs = bmds->bs;
L
lirans@il.ibm.com 已提交
271
    BlkMigBlock *blk;
272
    int nr_sectors;
273

274
    if (bmds->shared_base) {
275
        qemu_mutex_lock_iothread();
276
        aio_context_acquire(bdrv_get_aio_context(bs));
277
        while (cur_sector < total_sectors &&
278 279
               !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
                                  &nr_sectors)) {
L
lirans@il.ibm.com 已提交
280 281
            cur_sector += nr_sectors;
        }
282
        aio_context_release(bdrv_get_aio_context(bs));
283
        qemu_mutex_unlock_iothread();
L
lirans@il.ibm.com 已提交
284
    }
285 286

    if (cur_sector >= total_sectors) {
287
        bmds->cur_sector = bmds->completed_sectors = total_sectors;
L
lirans@il.ibm.com 已提交
288 289
        return 1;
    }
290

291
    bmds->completed_sectors = cur_sector;
292

293 294
    cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);

J
Jan Kiszka 已提交
295 296
    /* we are going to transfer a full block even if it is not allocated */
    nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
L
lirans@il.ibm.com 已提交
297

J
Jan Kiszka 已提交
298
    if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
299
        nr_sectors = total_sectors - cur_sector;
L
lirans@il.ibm.com 已提交
300
    }
301

302
    blk = g_new(BlkMigBlock, 1);
303
    blk->buf = g_malloc(BLOCK_SIZE);
304 305
    blk->bmds = bmds;
    blk->sector = cur_sector;
306
    blk->nr_sectors = nr_sectors;
307

L
Liran Schour 已提交
308 309 310
    blk->iov.iov_base = blk->buf;
    blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
    qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
311

P
Paolo Bonzini 已提交
312
    blk_mig_lock();
313
    block_mig_state.submitted++;
P
Paolo Bonzini 已提交
314
    blk_mig_unlock();
315

316 317 318 319 320 321 322 323
    /* We do not know if bs is under the main thread (and thus does
     * not acquire the AioContext when doing AIO) or rather under
     * dataplane.  Thus acquire both the iothread mutex and the
     * AioContext.
     *
     * This is ugly and will disappear when we make bdrv_* thread-safe,
     * without the need to acquire the AioContext.
     */
324
    qemu_mutex_lock_iothread();
325
    aio_context_acquire(bdrv_get_aio_context(bmds->bs));
L
Liran Schour 已提交
326 327
    blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
                                nr_sectors, blk_mig_read_cb, blk);
328

329
    bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector, nr_sectors);
330
    aio_context_release(bdrv_get_aio_context(bmds->bs));
331
    qemu_mutex_unlock_iothread();
332

333
    bmds->cur_sector = cur_sector + nr_sectors;
334
    return (bmds->cur_sector >= total_sectors);
L
lirans@il.ibm.com 已提交
335 336
}

337 338
/* Called with iothread lock taken.  */

339
static int set_dirty_tracking(void)
L
lirans@il.ibm.com 已提交
340 341
{
    BlkMigDevState *bmds;
342 343 344
    int ret;

    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
345
        aio_context_acquire(bdrv_get_aio_context(bmds->bs));
346
        bmds->dirty_bitmap = bdrv_create_dirty_bitmap(bmds->bs, BLOCK_SIZE,
347
                                                      NULL, NULL);
348
        aio_context_release(bdrv_get_aio_context(bmds->bs));
349 350 351 352 353 354
        if (!bmds->dirty_bitmap) {
            ret = -errno;
            goto fail;
        }
    }
    return 0;
355

356
fail:
357
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
358
        if (bmds->dirty_bitmap) {
359
            aio_context_acquire(bdrv_get_aio_context(bmds->bs));
360
            bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
361
            aio_context_release(bdrv_get_aio_context(bmds->bs));
362
        }
F
Fam Zheng 已提交
363
    }
364
    return ret;
F
Fam Zheng 已提交
365 366
}

367 368
/* Called with iothread lock taken.  */

F
Fam Zheng 已提交
369 370 371 372 373
static void unset_dirty_tracking(void)
{
    BlkMigDevState *bmds;

    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
374
        aio_context_acquire(bdrv_get_aio_context(bmds->bs));
F
Fam Zheng 已提交
375
        bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
376
        aio_context_release(bdrv_get_aio_context(bmds->bs));
L
lirans@il.ibm.com 已提交
377 378 379
    }
}

380
static void init_blk_migration(QEMUFile *f)
L
lirans@il.ibm.com 已提交
381
{
382
    BlockDriverState *bs;
383
    BlkMigDevState *bmds;
384
    int64_t sectors;
385

386 387 388 389 390 391 392 393 394 395 396 397 398
    block_mig_state.submitted = 0;
    block_mig_state.read_done = 0;
    block_mig_state.transferred = 0;
    block_mig_state.total_sector_sum = 0;
    block_mig_state.prev_progress = -1;
    block_mig_state.bulk_completed = 0;
    block_mig_state.zero_blocks = migrate_zero_blocks();

    for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
        if (bdrv_is_read_only(bs)) {
            continue;
        }

399
        sectors = bdrv_nb_sectors(bs);
400
        if (sectors <= 0) {
401 402 403
            return;
        }

404
        bmds = g_new0(BlkMigDevState, 1);
405 406 407 408 409
        bmds->bs = bs;
        bmds->bulk_completed = 0;
        bmds->total_sectors = sectors;
        bmds->completed_sectors = 0;
        bmds->shared_base = block_mig_state.shared_base;
410
        alloc_aio_bitmap(bmds);
411 412
        error_setg(&bmds->blocker, "block device is in use by migration");
        bdrv_op_block_all(bs, bmds->blocker);
413
        bdrv_ref(bs);
414 415 416 417

        block_mig_state.total_sector_sum += sectors;

        if (bmds->shared_base) {
418
            DPRINTF("Start migration for %s with shared base image\n",
419
                    bdrv_get_device_name(bs));
420
        } else {
421
            DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs));
422 423 424 425 426 427
        }

        QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
    }
}

428 429
/* Called with no lock taken.  */

430
static int blk_mig_save_bulked_block(QEMUFile *f)
L
lirans@il.ibm.com 已提交
431
{
432
    int64_t completed_sector_sum = 0;
L
lirans@il.ibm.com 已提交
433
    BlkMigDevState *bmds;
434
    int progress;
435
    int ret = 0;
L
lirans@il.ibm.com 已提交
436

437
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
438
        if (bmds->bulk_completed == 0) {
439
            if (mig_save_device_bulk(f, bmds) == 1) {
440 441
                /* completed bulk section for this device */
                bmds->bulk_completed = 1;
L
lirans@il.ibm.com 已提交
442
            }
443 444 445 446 447
            completed_sector_sum += bmds->completed_sectors;
            ret = 1;
            break;
        } else {
            completed_sector_sum += bmds->completed_sectors;
L
lirans@il.ibm.com 已提交
448 449
        }
    }
450

451 452 453 454 455 456
    if (block_mig_state.total_sector_sum != 0) {
        progress = completed_sector_sum * 100 /
                   block_mig_state.total_sector_sum;
    } else {
        progress = 100;
    }
457 458 459 460
    if (progress != block_mig_state.prev_progress) {
        block_mig_state.prev_progress = progress;
        qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
                         | BLK_MIG_FLAG_PROGRESS);
461
        DPRINTF("Completed %d %%\r", progress);
462 463 464
    }

    return ret;
L
lirans@il.ibm.com 已提交
465 466
}

467
static void blk_mig_reset_dirty_cursor(void)
L
lirans@il.ibm.com 已提交
468 469
{
    BlkMigDevState *bmds;
470 471 472 473 474 475

    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
        bmds->cur_dirty = 0;
    }
}

476
/* Called with iothread lock and AioContext taken.  */
477

478 479
static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
                                 int is_async)
480 481 482
{
    BlkMigBlock *blk;
    int64_t total_sectors = bmds->total_sectors;
L
lirans@il.ibm.com 已提交
483
    int64_t sector;
484
    int nr_sectors;
485
    int ret = -EIO;
486

487
    for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
P
Paolo Bonzini 已提交
488
        blk_mig_lock();
489
        if (bmds_aio_inflight(bmds, sector)) {
P
Paolo Bonzini 已提交
490
            blk_mig_unlock();
491
            bdrv_drain(bmds->bs);
P
Paolo Bonzini 已提交
492 493
        } else {
            blk_mig_unlock();
494
        }
F
Fam Zheng 已提交
495
        if (bdrv_get_dirty(bmds->bs, bmds->dirty_bitmap, sector)) {
496

497 498 499 500 501
            if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
                nr_sectors = total_sectors - sector;
            } else {
                nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
            }
502
            blk = g_new(BlkMigBlock, 1);
503
            blk->buf = g_malloc(BLOCK_SIZE);
504 505
            blk->bmds = bmds;
            blk->sector = sector;
506
            blk->nr_sectors = nr_sectors;
507

508
            if (is_async) {
509 510 511 512 513 514
                blk->iov.iov_base = blk->buf;
                blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
                qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);

                blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
                                            nr_sectors, blk_mig_read_cb, blk);
P
Paolo Bonzini 已提交
515 516

                blk_mig_lock();
517
                block_mig_state.submitted++;
518
                bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
P
Paolo Bonzini 已提交
519
                blk_mig_unlock();
520
            } else {
521 522
                ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
                if (ret < 0) {
523
                    goto error;
L
lirans@il.ibm.com 已提交
524
                }
525
                blk_send(f, blk);
526

527 528
                g_free(blk->buf);
                g_free(blk);
529
            }
530

531
            bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, sector, nr_sectors);
532
            break;
L
lirans@il.ibm.com 已提交
533
        }
534 535
        sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
        bmds->cur_dirty = sector;
L
lirans@il.ibm.com 已提交
536
    }
537

538 539
    return (bmds->cur_dirty >= bmds->total_sectors);

540
error:
541
    DPRINTF("Error reading sector %" PRId64 "\n", sector);
542 543
    g_free(blk->buf);
    g_free(blk);
544
    return ret;
545 546
}

547 548 549
/* Called with iothread lock taken.
 *
 * return value:
550 551 552
 * 0: too much data for max_downtime
 * 1: few enough data for max_downtime
*/
553
static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
554 555
{
    BlkMigDevState *bmds;
556
    int ret = 1;
557 558

    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
559
        aio_context_acquire(bdrv_get_aio_context(bmds->bs));
560
        ret = mig_save_device_dirty(f, bmds, is_async);
561
        aio_context_release(bdrv_get_aio_context(bmds->bs));
562
        if (ret <= 0) {
563 564 565 566 567
            break;
        }
    }

    return ret;
L
lirans@il.ibm.com 已提交
568 569
}

570 571
/* Called with no locks taken.  */

572
static int flush_blks(QEMUFile *f)
L
lirans@il.ibm.com 已提交
573
{
574
    BlkMigBlock *blk;
575
    int ret = 0;
576

M
malc 已提交
577
    DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
578 579
            __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
            block_mig_state.transferred);
580

P
Paolo Bonzini 已提交
581
    blk_mig_lock();
582 583 584 585
    while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
        if (qemu_file_rate_limit(f)) {
            break;
        }
586
        if (blk->ret < 0) {
587
            ret = blk->ret;
588 589
            break;
        }
590

591
        QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
P
Paolo Bonzini 已提交
592
        blk_mig_unlock();
593
        blk_send(f, blk);
P
Paolo Bonzini 已提交
594
        blk_mig_lock();
595

596 597
        g_free(blk->buf);
        g_free(blk);
598

599 600 601
        block_mig_state.read_done--;
        block_mig_state.transferred++;
        assert(block_mig_state.read_done >= 0);
L
lirans@il.ibm.com 已提交
602
    }
P
Paolo Bonzini 已提交
603
    blk_mig_unlock();
L
lirans@il.ibm.com 已提交
604

M
malc 已提交
605
    DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
606 607
            block_mig_state.submitted, block_mig_state.read_done,
            block_mig_state.transferred);
608
    return ret;
L
lirans@il.ibm.com 已提交
609 610
}

611 612
/* Called with iothread lock taken.  */

613 614 615 616 617 618
static int64_t get_remaining_dirty(void)
{
    BlkMigDevState *bmds;
    int64_t dirty = 0;

    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
619
        aio_context_acquire(bdrv_get_aio_context(bmds->bs));
620
        dirty += bdrv_get_dirty_count(bmds->dirty_bitmap);
621
        aio_context_release(bdrv_get_aio_context(bmds->bs));
622 623
    }

624
    return dirty << BDRV_SECTOR_BITS;
625 626
}

627 628
/* Called with iothread lock taken.  */

L
Liang Li 已提交
629
static void block_migration_cleanup(void *opaque)
630
{
631 632
    BlkMigDevState *bmds;
    BlkMigBlock *blk;
633
    AioContext *ctx;
634

635 636
    bdrv_drain_all();

F
Fam Zheng 已提交
637
    unset_dirty_tracking();
638

639 640
    while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
        QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
641 642
        bdrv_op_unblock_all(bmds->bs, bmds->blocker);
        error_free(bmds->blocker);
643 644 645 646

        /* Save ctx, because bmds->bs can disappear during bdrv_unref.  */
        ctx = bdrv_get_aio_context(bmds->bs);
        aio_context_acquire(ctx);
647
        bdrv_unref(bmds->bs);
648 649
        aio_context_release(ctx);

650 651
        g_free(bmds->aio_bitmap);
        g_free(bmds);
652 653
    }

654
    blk_mig_lock();
655 656
    while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
        QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
657 658
        g_free(blk->buf);
        g_free(blk);
659
    }
P
Paolo Bonzini 已提交
660
    blk_mig_unlock();
661 662
}

663
static int block_save_setup(QEMUFile *f, void *opaque)
L
lirans@il.ibm.com 已提交
664
{
665 666
    int ret;

667 668
    DPRINTF("Enter save live setup submitted %d transferred %d\n",
            block_mig_state.submitted, block_mig_state.transferred);
669

670
    qemu_mutex_lock_iothread();
671
    init_blk_migration(f);
672 673

    /* start track dirty blocks */
674 675
    ret = set_dirty_tracking();

676 677
    qemu_mutex_unlock_iothread();

678 679 680 681
    if (ret) {
        return ret;
    }

682
    ret = flush_blks(f);
683 684 685
    blk_mig_reset_dirty_cursor();
    qemu_put_be64(f, BLK_MIG_FLAG_EOS);

686
    return ret;
687 688
}

689
static int block_save_iterate(QEMUFile *f, void *opaque)
690 691
{
    int ret;
692
    int64_t last_ftell = qemu_ftell(f);
G
Gary R Hook 已提交
693
    int64_t delta_ftell;
694

695 696
    DPRINTF("Enter save live iterate submitted %d transferred %d\n",
            block_mig_state.submitted, block_mig_state.transferred);
697

698
    ret = flush_blks(f);
699 700
    if (ret) {
        return ret;
701 702
    }

703 704
    blk_mig_reset_dirty_cursor();

705
    /* control the rate of transfer */
P
Paolo Bonzini 已提交
706
    blk_mig_lock();
707 708
    while ((block_mig_state.submitted +
            block_mig_state.read_done) * BLOCK_SIZE <
709 710 711 712
           qemu_file_get_rate_limit(f) &&
           (block_mig_state.submitted +
            block_mig_state.read_done) <
           MAX_INFLIGHT_IO) {
P
Paolo Bonzini 已提交
713
        blk_mig_unlock();
714 715 716 717 718 719
        if (block_mig_state.bulk_completed == 0) {
            /* first finish the bulk phase */
            if (blk_mig_save_bulked_block(f) == 0) {
                /* finished saving bulk on all devices */
                block_mig_state.bulk_completed = 1;
            }
720
            ret = 0;
721
        } else {
722 723 724 725
            /* Always called with iothread lock taken for
             * simplicity, block_save_complete also calls it.
             */
            qemu_mutex_lock_iothread();
726
            ret = blk_mig_save_dirty_block(f, 1);
727
            qemu_mutex_unlock_iothread();
728 729 730 731
        }
        if (ret < 0) {
            return ret;
        }
P
Paolo Bonzini 已提交
732
        blk_mig_lock();
733 734 735
        if (ret != 0) {
            /* no more dirty blocks */
            break;
736
        }
737
    }
P
Paolo Bonzini 已提交
738
    blk_mig_unlock();
739

740
    ret = flush_blks(f);
741 742
    if (ret) {
        return ret;
743 744
    }

745
    qemu_put_be64(f, BLK_MIG_FLAG_EOS);
G
Gary R Hook 已提交
746 747 748 749 750 751 752 753
    delta_ftell = qemu_ftell(f) - last_ftell;
    if (delta_ftell > 0) {
        return 1;
    } else if (delta_ftell < 0) {
        return -1;
    } else {
        return 0;
    }
754 755
}

756 757
/* Called with iothread lock taken.  */

758 759 760 761 762 763 764
static int block_save_complete(QEMUFile *f, void *opaque)
{
    int ret;

    DPRINTF("Enter save live complete submitted %d transferred %d\n",
            block_mig_state.submitted, block_mig_state.transferred);

765
    ret = flush_blks(f);
766 767 768
    if (ret) {
        return ret;
    }
769

770
    blk_mig_reset_dirty_cursor();
771

772 773
    /* we know for sure that save bulk is completed and
       all async read completed */
P
Paolo Bonzini 已提交
774
    blk_mig_lock();
775
    assert(block_mig_state.submitted == 0);
P
Paolo Bonzini 已提交
776
    blk_mig_unlock();
777

778 779
    do {
        ret = blk_mig_save_dirty_block(f, 0);
780 781 782
        if (ret < 0) {
            return ret;
        }
783
    } while (ret == 0);
784

785 786
    /* report completion */
    qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
787

788 789
    DPRINTF("Block migration completed\n");

790 791
    qemu_put_be64(f, BLK_MIG_FLAG_EOS);

792
    return 0;
L
lirans@il.ibm.com 已提交
793 794
}

795 796 797
static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
                               uint64_t *non_postcopiable_pending,
                               uint64_t *postcopiable_pending)
798
{
799
    /* Estimate pending number of bytes to send */
800 801
    uint64_t pending;

802
    qemu_mutex_lock_iothread();
803 804 805
    pending = get_remaining_dirty();
    qemu_mutex_unlock_iothread();

P
Paolo Bonzini 已提交
806
    blk_mig_lock();
807 808 809
    pending += block_mig_state.submitted * BLOCK_SIZE +
               block_mig_state.read_done * BLOCK_SIZE;
    blk_mig_unlock();
810 811

    /* Report at least one block pending during bulk phase */
812 813
    if (pending <= max_size && !block_mig_state.bulk_completed) {
        pending = max_size + BLOCK_SIZE;
814
    }
815

816
    DPRINTF("Enter save live pending  %" PRIu64 "\n", pending);
817 818
    /* We don't do postcopy */
    *non_postcopiable_pending += pending;
819 820
}

L
lirans@il.ibm.com 已提交
821 822
static int block_load(QEMUFile *f, void *opaque, int version_id)
{
823
    static int banner_printed;
L
lirans@il.ibm.com 已提交
824 825 826
    int len, flags;
    char device_name[256];
    int64_t addr;
827
    BlockDriverState *bs, *bs_prev = NULL;
828
    BlockBackend *blk;
829
    Error *local_err = NULL;
L
lirans@il.ibm.com 已提交
830
    uint8_t *buf;
831 832
    int64_t total_sectors = 0;
    int nr_sectors;
833
    int ret;
834

L
lirans@il.ibm.com 已提交
835 836
    do {
        addr = qemu_get_be64(f);
837

J
Jan Kiszka 已提交
838 839
        flags = addr & ~BDRV_SECTOR_MASK;
        addr >>= BDRV_SECTOR_BITS;
840 841

        if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
L
lirans@il.ibm.com 已提交
842 843 844 845
            /* get device name */
            len = qemu_get_byte(f);
            qemu_get_buffer(f, (uint8_t *)device_name, len);
            device_name[len] = '\0';
846

847 848
            blk = blk_by_name(device_name);
            if (!blk) {
849 850 851 852
                fprintf(stderr, "Error unknown block device %s\n",
                        device_name);
                return -EINVAL;
            }
853
            bs = blk_bs(blk);
M
Max Reitz 已提交
854 855 856 857 858
            if (!bs) {
                fprintf(stderr, "Block device %s has no medium\n",
                        device_name);
                return -EINVAL;
            }
859

860 861
            if (bs != bs_prev) {
                bs_prev = bs;
862
                total_sectors = bdrv_nb_sectors(bs);
863
                if (total_sectors <= 0) {
864
                    error_report("Error getting length of block device %s",
865 866 867
                                 device_name);
                    return -EINVAL;
                }
868 869 870 871 872 873

                bdrv_invalidate_cache(bs, &local_err);
                if (local_err) {
                    error_report_err(local_err);
                    return -EINVAL;
                }
874 875 876 877 878 879 880 881
            }

            if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
                nr_sectors = total_sectors - addr;
            } else {
                nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
            }

882
            if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
883 884
                ret = bdrv_write_zeroes(bs, addr, nr_sectors,
                                        BDRV_REQ_MAY_UNMAP);
885 886 887 888 889 890
            } else {
                buf = g_malloc(BLOCK_SIZE);
                qemu_get_buffer(f, buf, BLOCK_SIZE);
                ret = bdrv_write(bs, addr, buf, nr_sectors);
                g_free(buf);
            }
891

892 893 894
            if (ret < 0) {
                return ret;
            }
895 896 897 898 899 900 901 902
        } else if (flags & BLK_MIG_FLAG_PROGRESS) {
            if (!banner_printed) {
                printf("Receiving block device images\n");
                banner_printed = 1;
            }
            printf("Completed %d %%%c", (int)addr,
                   (addr == 100) ? '\n' : '\r');
            fflush(stdout);
903
        } else if (!(flags & BLK_MIG_FLAG_EOS)) {
904
            fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
905 906
            return -EINVAL;
        }
907 908 909
        ret = qemu_file_get_error(f);
        if (ret != 0) {
            return ret;
L
lirans@il.ibm.com 已提交
910
        }
911 912
    } while (!(flags & BLK_MIG_FLAG_EOS));

L
lirans@il.ibm.com 已提交
913 914 915
    return 0;
}

I
Isaku Yamahata 已提交
916
static void block_set_params(const MigrationParams *params, void *opaque)
L
lirans@il.ibm.com 已提交
917
{
I
Isaku Yamahata 已提交
918 919
    block_mig_state.blk_enable = params->blk;
    block_mig_state.shared_base = params->shared;
920

L
lirans@il.ibm.com 已提交
921
    /* shared base means that blk_enable = 1 */
I
Isaku Yamahata 已提交
922
    block_mig_state.blk_enable |= params->shared;
L
lirans@il.ibm.com 已提交
923 924
}

925 926 927 928 929
static bool block_is_active(void *opaque)
{
    return block_mig_state.blk_enable == 1;
}

930
static SaveVMHandlers savevm_block_handlers = {
931
    .set_params = block_set_params,
932
    .save_live_setup = block_save_setup,
933
    .save_live_iterate = block_save_iterate,
934
    .save_live_complete_precopy = block_save_complete,
935
    .save_live_pending = block_save_pending,
936
    .load_state = block_load,
L
Liang Li 已提交
937
    .cleanup = block_migration_cleanup,
938
    .is_active = block_is_active,
939 940
};

L
lirans@il.ibm.com 已提交
941
void blk_mig_init(void)
942
{
943 944
    QSIMPLEQ_INIT(&block_mig_state.bmds_list);
    QSIMPLEQ_INIT(&block_mig_state.blk_list);
P
Paolo Bonzini 已提交
945
    qemu_mutex_init(&block_mig_state.lock);
946

947 948
    register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
                         &block_mig_state);
L
lirans@il.ibm.com 已提交
949
}