2009-11-02 14:40:58 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
2012-01-13 17:44:23 +01:00
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2009-11-02 14:40:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
2012-12-17 18:19:44 +01:00
|
|
|
#include "block/block_int.h"
|
2009-11-02 14:40:58 +01:00
|
|
|
#include "hw/hw.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/queue.h"
|
|
|
|
#include "qemu/timer.h"
|
2012-12-17 18:19:50 +01:00
|
|
|
#include "migration/block.h"
|
|
|
|
#include "migration/migration.h"
|
2012-12-17 18:20:04 +01:00
|
|
|
#include "sysemu/blockdev.h"
|
2009-11-02 14:40:58 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2013-01-21 17:09:45 +01:00
|
|
|
#define BLOCK_SIZE (1 << 20)
|
|
|
|
#define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
|
2009-11-02 14:40:58 +01:00
|
|
|
|
|
|
|
#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
|
|
|
|
#define BLK_MIG_FLAG_EOS 0x02
|
2009-12-01 15:20:17 +01:00
|
|
|
#define BLK_MIG_FLAG_PROGRESS 0x04
|
2013-07-18 09:48:50 +02:00
|
|
|
#define BLK_MIG_FLAG_ZERO_BLOCK 0x08
|
2009-11-02 14:40:58 +01:00
|
|
|
|
|
|
|
#define MAX_IS_ALLOCATED_SEARCH 65536
|
|
|
|
|
|
|
|
//#define DEBUG_BLK_MIGRATION
|
|
|
|
|
|
|
|
#ifdef DEBUG_BLK_MIGRATION
|
2010-02-07 00:03:50 +01:00
|
|
|
#define DPRINTF(fmt, ...) \
|
2009-11-02 14:40:58 +01:00
|
|
|
do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
|
|
|
|
#else
|
2010-02-07 00:03:50 +01:00
|
|
|
#define DPRINTF(fmt, ...) \
|
2009-11-02 14:40:58 +01:00
|
|
|
do { } while (0)
|
|
|
|
#endif
|
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
typedef struct BlkMigDevState {
|
2013-02-22 17:36:24 +01:00
|
|
|
/* Written during setup phase. Can be read without a lock. */
|
2009-11-30 18:21:19 +01:00
|
|
|
BlockDriverState *bs;
|
|
|
|
int shared_base;
|
|
|
|
int64_t total_sectors;
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_ENTRY(BlkMigDevState) entry;
|
2013-02-22 17:36:24 +01:00
|
|
|
|
|
|
|
/* Only used by migration thread. Does not need a lock. */
|
|
|
|
int bulk_completed;
|
|
|
|
int64_t cur_sector;
|
|
|
|
int64_t cur_dirty;
|
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
/* Protected by block migration lock. */
|
2010-11-08 20:02:56 +01:00
|
|
|
unsigned long *aio_bitmap;
|
2013-02-22 17:36:24 +01:00
|
|
|
int64_t completed_sectors;
|
2009-11-30 18:21:19 +01:00
|
|
|
} BlkMigDevState;
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
typedef struct BlkMigBlock {
|
2013-02-22 17:36:24 +01:00
|
|
|
/* Only used by migration thread. */
|
2009-11-02 14:40:58 +01:00
|
|
|
uint8_t *buf;
|
|
|
|
BlkMigDevState *bmds;
|
|
|
|
int64_t sector;
|
2010-11-08 20:02:56 +01:00
|
|
|
int nr_sectors;
|
2009-11-02 14:40:58 +01:00
|
|
|
struct iovec iov;
|
|
|
|
QEMUIOVector qiov;
|
|
|
|
BlockDriverAIOCB *aiocb;
|
2013-02-22 17:36:24 +01:00
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
/* Protected by block migration lock. */
|
2009-11-02 14:40:58 +01:00
|
|
|
int ret;
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_ENTRY(BlkMigBlock) entry;
|
2009-11-02 14:40:58 +01:00
|
|
|
} BlkMigBlock;
|
|
|
|
|
|
|
|
typedef struct BlkMigState {
|
2013-02-22 17:36:24 +01:00
|
|
|
/* Written during setup phase. Can be read without a lock. */
|
2009-11-02 14:40:58 +01:00
|
|
|
int blk_enable;
|
|
|
|
int shared_base;
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
|
2013-02-22 17:36:24 +01:00
|
|
|
int64_t total_sector_sum;
|
2013-07-18 09:48:50 +02:00
|
|
|
bool zero_blocks;
|
2013-02-22 17:36:24 +01:00
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
/* Protected by lock. */
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
|
2009-11-02 14:40:58 +01:00
|
|
|
int submitted;
|
|
|
|
int read_done;
|
2013-02-22 17:36:24 +01:00
|
|
|
|
|
|
|
/* Only used by migration thread. Does not need a lock. */
|
2009-11-02 14:40:58 +01:00
|
|
|
int transferred;
|
2009-12-01 15:20:17 +01:00
|
|
|
int prev_progress;
|
2010-01-26 09:31:45 +01:00
|
|
|
int bulk_completed;
|
2013-02-22 17:36:25 +01:00
|
|
|
|
|
|
|
/* Lock must be taken _inside_ the iothread lock. */
|
|
|
|
QemuMutex lock;
|
2009-11-02 14:40:58 +01:00
|
|
|
} BlkMigState;
|
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
static BlkMigState block_mig_state;
|
2009-11-02 14:40:58 +01:00
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Must run outside of the iothread lock during the bulk phase,
|
|
|
|
* or the VM will stall.
|
|
|
|
*/
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
static void blk_send(QEMUFile *f, BlkMigBlock * blk)
|
|
|
|
{
|
|
|
|
int len;
|
2013-07-18 09:48:50 +02:00
|
|
|
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;
|
|
|
|
}
|
2009-11-30 18:21:21 +01:00
|
|
|
|
|
|
|
/* sector number and flags */
|
|
|
|
qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
|
2013-07-18 09:48:50 +02:00
|
|
|
| flags);
|
2009-11-30 18:21:21 +01:00
|
|
|
|
|
|
|
/* device name */
|
|
|
|
len = strlen(blk->bmds->bs->device_name);
|
|
|
|
qemu_put_byte(f, len);
|
|
|
|
qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
|
|
|
|
|
2013-07-18 09:48:50 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
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;
|
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2009-11-30 18:21:21 +01:00
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
|
|
|
sum += bmds->completed_sectors;
|
|
|
|
}
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2009-11-30 18:21:21 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
|
|
|
|
/* Called with migration lock held. */
|
|
|
|
|
2010-11-08 20:02:56 +01:00
|
|
|
static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
|
|
|
|
{
|
|
|
|
int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
|
|
|
|
|
2010-11-12 19:07:50 +01:00
|
|
|
if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
|
2010-11-08 20:02:56 +01:00
|
|
|
return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
|
|
|
|
(1UL << (chunk % (sizeof(unsigned long) * 8))));
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
/* Called with migration lock held. */
|
|
|
|
|
2010-11-08 20:02:56 +01:00
|
|
|
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) {
|
2010-11-12 19:07:50 +01:00
|
|
|
val |= 1UL << bit;
|
2010-11-08 20:02:56 +01:00
|
|
|
} else {
|
2010-11-12 19:07:50 +01:00
|
|
|
val &= ~(1UL << bit);
|
2010-11-08 20:02:56 +01:00
|
|
|
}
|
|
|
|
bmds->aio_bitmap[idx] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void alloc_aio_bitmap(BlkMigDevState *bmds)
|
|
|
|
{
|
|
|
|
BlockDriverState *bs = bmds->bs;
|
|
|
|
int64_t bitmap_size;
|
|
|
|
|
|
|
|
bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
|
|
|
|
BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
|
|
|
|
bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
bmds->aio_bitmap = g_malloc0(bitmap_size);
|
2010-11-08 20:02:56 +01:00
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
/* Never hold migration lock when yielding to the main loop! */
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
static void blk_mig_read_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
BlkMigBlock *blk = opaque;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2009-11-02 14:40:58 +01:00
|
|
|
blk->ret = ret;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
|
2010-11-08 20:02:56 +01:00
|
|
|
bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
block_mig_state.submitted--;
|
|
|
|
block_mig_state.read_done++;
|
|
|
|
assert(block_mig_state.submitted >= 0);
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Called with no lock taken. */
|
|
|
|
|
2011-12-05 17:06:56 +01:00
|
|
|
static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
|
2009-11-30 18:21:19 +01:00
|
|
|
{
|
2009-11-30 18:21:20 +01:00
|
|
|
int64_t total_sectors = bmds->total_sectors;
|
|
|
|
int64_t cur_sector = bmds->cur_sector;
|
|
|
|
BlockDriverState *bs = bmds->bs;
|
2009-11-02 14:40:58 +01:00
|
|
|
BlkMigBlock *blk;
|
2009-11-30 18:21:21 +01:00
|
|
|
int nr_sectors;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
if (bmds->shared_base) {
|
2013-02-22 17:36:27 +01:00
|
|
|
qemu_mutex_lock_iothread();
|
2009-11-30 18:21:20 +01:00
|
|
|
while (cur_sector < total_sectors &&
|
2009-11-30 18:21:20 +01:00
|
|
|
!bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
|
|
|
|
&nr_sectors)) {
|
2009-11-02 14:40:58 +01:00
|
|
|
cur_sector += nr_sectors;
|
|
|
|
}
|
2013-02-22 17:36:27 +01:00
|
|
|
qemu_mutex_unlock_iothread();
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
|
|
|
if (cur_sector >= total_sectors) {
|
2009-11-30 18:21:21 +01:00
|
|
|
bmds->cur_sector = bmds->completed_sectors = total_sectors;
|
2009-11-02 14:40:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
bmds->completed_sectors = cur_sector;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
|
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
/* we are going to transfer a full block even if it is not allocated */
|
|
|
|
nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
|
2009-11-02 14:40:58 +01:00
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
|
2009-11-30 18:21:20 +01:00
|
|
|
nr_sectors = total_sectors - cur_sector;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
blk = g_malloc(sizeof(BlkMigBlock));
|
|
|
|
blk->buf = g_malloc(BLOCK_SIZE);
|
2009-11-30 18:21:21 +01:00
|
|
|
blk->bmds = bmds;
|
|
|
|
blk->sector = cur_sector;
|
2010-11-08 20:02:56 +01:00
|
|
|
blk->nr_sectors = nr_sectors;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2010-01-26 09:31:45 +01:00
|
|
|
blk->iov.iov_base = blk->buf;
|
|
|
|
blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
|
|
|
|
qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2013-02-22 17:36:23 +01:00
|
|
|
block_mig_state.submitted++;
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2013-02-22 17:36:23 +01:00
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
qemu_mutex_lock_iothread();
|
2010-01-26 09:31:45 +01:00
|
|
|
blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
|
|
|
|
nr_sectors, blk_mig_read_cb, blk);
|
2010-01-26 13:04:11 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
bdrv_reset_dirty(bs, cur_sector, nr_sectors);
|
2013-02-22 17:36:27 +01:00
|
|
|
qemu_mutex_unlock_iothread();
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
bmds->cur_sector = cur_sector + nr_sectors;
|
2009-11-30 18:21:21 +01:00
|
|
|
return (bmds->cur_sector >= total_sectors);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Called with iothread lock taken. */
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
static void set_dirty_tracking(int enable)
|
|
|
|
{
|
|
|
|
BlkMigDevState *bmds;
|
2009-11-30 18:21:20 +01:00
|
|
|
|
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
2013-01-21 17:09:45 +01:00
|
|
|
bdrv_set_dirty_tracking(bmds->bs, enable ? BLOCK_SIZE : 0);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-09 16:22:13 +02:00
|
|
|
static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
|
2009-11-02 14:40:58 +01:00
|
|
|
{
|
2009-11-30 18:21:20 +01:00
|
|
|
BlkMigDevState *bmds;
|
2009-11-30 20:34:55 +01:00
|
|
|
int64_t sectors;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2010-06-28 10:45:02 +02:00
|
|
|
if (!bdrv_is_read_only(bs)) {
|
2010-04-09 16:22:13 +02:00
|
|
|
sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
|
2010-07-10 17:59:06 +02:00
|
|
|
if (sectors <= 0) {
|
2010-04-09 16:22:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
bmds = g_malloc0(sizeof(BlkMigDevState));
|
2010-04-09 16:22:13 +02:00
|
|
|
bmds->bs = bs;
|
|
|
|
bmds->bulk_completed = 0;
|
|
|
|
bmds->total_sectors = sectors;
|
|
|
|
bmds->completed_sectors = 0;
|
|
|
|
bmds->shared_base = block_mig_state.shared_base;
|
2010-11-08 20:02:56 +01:00
|
|
|
alloc_aio_bitmap(bmds);
|
2011-01-26 15:12:33 +01:00
|
|
|
drive_get_ref(drive_get_by_blockdev(bs));
|
2011-01-26 15:12:35 +01:00
|
|
|
bdrv_set_in_use(bs, 1);
|
2010-04-09 16:22:13 +02:00
|
|
|
|
|
|
|
block_mig_state.total_sector_sum += sectors;
|
|
|
|
|
|
|
|
if (bmds->shared_base) {
|
2011-12-05 17:06:56 +01:00
|
|
|
DPRINTF("Start migration for %s with shared base image\n",
|
|
|
|
bs->device_name);
|
2010-04-09 16:22:13 +02:00
|
|
|
} else {
|
2011-12-05 17:06:56 +01:00
|
|
|
DPRINTF("Start full migration for %s\n", bs->device_name);
|
2010-04-09 16:22:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-05 17:06:56 +01:00
|
|
|
static void init_blk_migration(QEMUFile *f)
|
2010-04-09 16:22:13 +02:00
|
|
|
{
|
2009-11-30 18:21:20 +01:00
|
|
|
block_mig_state.submitted = 0;
|
|
|
|
block_mig_state.read_done = 0;
|
|
|
|
block_mig_state.transferred = 0;
|
2009-11-30 18:21:21 +01:00
|
|
|
block_mig_state.total_sector_sum = 0;
|
2009-12-01 15:20:17 +01:00
|
|
|
block_mig_state.prev_progress = -1;
|
2010-01-26 09:31:45 +01:00
|
|
|
block_mig_state.bulk_completed = 0;
|
2013-07-18 09:48:50 +02:00
|
|
|
block_mig_state.zero_blocks = migrate_zero_blocks();
|
2009-11-30 18:21:20 +01:00
|
|
|
|
2011-12-05 17:06:56 +01:00
|
|
|
bdrv_iterate(init_blk_migration_it, NULL);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Called with no lock taken. */
|
|
|
|
|
2011-12-05 17:06:56 +01:00
|
|
|
static int blk_mig_save_bulked_block(QEMUFile *f)
|
2009-11-02 14:40:58 +01:00
|
|
|
{
|
2009-11-30 18:21:21 +01:00
|
|
|
int64_t completed_sector_sum = 0;
|
2009-11-02 14:40:58 +01:00
|
|
|
BlkMigDevState *bmds;
|
2009-12-01 15:20:17 +01:00
|
|
|
int progress;
|
2009-11-30 18:21:21 +01:00
|
|
|
int ret = 0;
|
2009-11-02 14:40:58 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
2009-11-30 18:21:19 +01:00
|
|
|
if (bmds->bulk_completed == 0) {
|
2011-12-05 17:06:56 +01:00
|
|
|
if (mig_save_device_bulk(f, bmds) == 1) {
|
2009-11-30 18:21:20 +01:00
|
|
|
/* completed bulk section for this device */
|
|
|
|
bmds->bulk_completed = 1;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:21 +01:00
|
|
|
completed_sector_sum += bmds->completed_sectors;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
completed_sector_sum += bmds->completed_sectors;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2011-01-12 14:41:00 +01:00
|
|
|
if (block_mig_state.total_sector_sum != 0) {
|
|
|
|
progress = completed_sector_sum * 100 /
|
|
|
|
block_mig_state.total_sector_sum;
|
|
|
|
} else {
|
|
|
|
progress = 100;
|
|
|
|
}
|
2009-12-01 15:20:17 +01:00
|
|
|
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);
|
2011-12-05 17:06:56 +01:00
|
|
|
DPRINTF("Completed %d %%\r", progress);
|
2009-11-30 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2010-01-26 13:04:11 +01:00
|
|
|
static void blk_mig_reset_dirty_cursor(void)
|
2009-11-02 14:40:58 +01:00
|
|
|
{
|
|
|
|
BlkMigDevState *bmds;
|
2010-01-26 13:04:11 +01:00
|
|
|
|
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
|
|
|
bmds->cur_dirty = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Called with iothread lock taken. */
|
|
|
|
|
2011-12-05 17:06:56 +01:00
|
|
|
static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
|
|
|
|
int is_async)
|
2010-01-26 13:04:11 +01:00
|
|
|
{
|
|
|
|
BlkMigBlock *blk;
|
|
|
|
int64_t total_sectors = bmds->total_sectors;
|
2009-11-02 14:40:58 +01:00
|
|
|
int64_t sector;
|
2010-01-26 13:04:11 +01:00
|
|
|
int nr_sectors;
|
2011-09-21 23:01:54 +02:00
|
|
|
int ret = -EIO;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2010-01-26 13:04:11 +01:00
|
|
|
for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2010-11-12 19:07:50 +01:00
|
|
|
if (bmds_aio_inflight(bmds, sector)) {
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2011-11-30 13:23:43 +01:00
|
|
|
bdrv_drain_all();
|
2013-02-22 17:36:25 +01:00
|
|
|
} else {
|
|
|
|
blk_mig_unlock();
|
2010-11-12 19:07:50 +01:00
|
|
|
}
|
2010-01-26 13:04:11 +01:00
|
|
|
if (bdrv_get_dirty(bmds->bs, sector)) {
|
2009-11-30 18:21:20 +01:00
|
|
|
|
2010-01-26 13:04:11 +01:00
|
|
|
if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
|
|
|
|
nr_sectors = total_sectors - sector;
|
|
|
|
} else {
|
|
|
|
nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
|
|
|
|
}
|
2011-08-21 05:09:37 +02:00
|
|
|
blk = g_malloc(sizeof(BlkMigBlock));
|
|
|
|
blk->buf = g_malloc(BLOCK_SIZE);
|
2010-01-26 13:04:11 +01:00
|
|
|
blk->bmds = bmds;
|
|
|
|
blk->sector = sector;
|
2010-11-08 20:02:56 +01:00
|
|
|
blk->nr_sectors = nr_sectors;
|
2010-01-26 13:04:11 +01:00
|
|
|
|
2010-01-26 09:31:49 +01:00
|
|
|
if (is_async) {
|
2010-01-26 13:04:11 +01:00
|
|
|
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);
|
2013-02-22 17:36:25 +01:00
|
|
|
|
|
|
|
blk_mig_lock();
|
2010-01-26 13:04:11 +01:00
|
|
|
block_mig_state.submitted++;
|
2010-11-08 20:02:56 +01:00
|
|
|
bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2010-01-26 13:04:11 +01:00
|
|
|
} else {
|
2011-09-21 23:01:54 +02:00
|
|
|
ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
|
|
|
|
if (ret < 0) {
|
2010-01-26 13:04:11 +01:00
|
|
|
goto error;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2010-01-26 13:04:11 +01:00
|
|
|
blk_send(f, blk);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(blk->buf);
|
|
|
|
g_free(blk);
|
2009-11-30 18:21:19 +01:00
|
|
|
}
|
2010-01-26 13:04:11 +01:00
|
|
|
|
|
|
|
bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
|
|
|
|
break;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2010-01-26 13:04:11 +01:00
|
|
|
sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
|
|
|
|
bmds->cur_dirty = sector;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:20 +01:00
|
|
|
|
2010-01-26 13:04:11 +01:00
|
|
|
return (bmds->cur_dirty >= bmds->total_sectors);
|
|
|
|
|
2010-01-26 09:31:49 +01:00
|
|
|
error:
|
2011-12-05 17:06:56 +01:00
|
|
|
DPRINTF("Error reading sector %" PRId64 "\n", sector);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(blk->buf);
|
|
|
|
g_free(blk);
|
2012-08-29 21:59:22 +02:00
|
|
|
return ret;
|
2010-01-26 13:04:11 +01:00
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Called with iothread lock taken.
|
|
|
|
*
|
|
|
|
* return value:
|
2012-08-29 21:37:14 +02:00
|
|
|
* 0: too much data for max_downtime
|
|
|
|
* 1: few enough data for max_downtime
|
|
|
|
*/
|
2011-12-05 17:06:56 +01:00
|
|
|
static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
|
2010-01-26 13:04:11 +01:00
|
|
|
{
|
|
|
|
BlkMigDevState *bmds;
|
2012-08-29 21:37:14 +02:00
|
|
|
int ret = 1;
|
2010-01-26 13:04:11 +01:00
|
|
|
|
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
2012-08-29 21:37:14 +02:00
|
|
|
ret = mig_save_device_dirty(f, bmds, is_async);
|
2012-08-29 21:59:22 +02:00
|
|
|
if (ret <= 0) {
|
2010-01-26 13:04:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Called with no locks taken. */
|
|
|
|
|
2012-08-29 20:17:13 +02:00
|
|
|
static int flush_blks(QEMUFile *f)
|
2009-11-02 14:40:58 +01:00
|
|
|
{
|
2009-11-30 18:21:20 +01:00
|
|
|
BlkMigBlock *blk;
|
2012-08-29 20:17:13 +02:00
|
|
|
int ret = 0;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2010-02-07 00:03:50 +01:00
|
|
|
DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
|
2009-11-30 18:21:20 +01:00
|
|
|
__FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
|
|
|
|
block_mig_state.transferred);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2009-11-30 18:21:20 +01:00
|
|
|
while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
|
|
|
|
if (qemu_file_rate_limit(f)) {
|
|
|
|
break;
|
|
|
|
}
|
2009-11-30 18:21:21 +01:00
|
|
|
if (blk->ret < 0) {
|
2012-08-29 20:17:13 +02:00
|
|
|
ret = blk->ret;
|
2009-11-30 18:21:21 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2013-02-22 17:36:23 +01:00
|
|
|
blk_send(f, blk);
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2013-02-22 17:36:23 +01:00
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(blk->buf);
|
|
|
|
g_free(blk);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
block_mig_state.read_done--;
|
|
|
|
block_mig_state.transferred++;
|
|
|
|
assert(block_mig_state.read_done >= 0);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2009-11-02 14:40:58 +01:00
|
|
|
|
2010-02-07 00:03:50 +01:00
|
|
|
DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
|
2009-11-30 18:21:20 +01:00
|
|
|
block_mig_state.submitted, block_mig_state.read_done,
|
|
|
|
block_mig_state.transferred);
|
2012-08-29 20:17:13 +02:00
|
|
|
return ret;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Called with iothread lock taken. */
|
|
|
|
|
2010-01-26 09:31:49 +01:00
|
|
|
static int64_t get_remaining_dirty(void)
|
|
|
|
{
|
|
|
|
BlkMigDevState *bmds;
|
|
|
|
int64_t dirty = 0;
|
|
|
|
|
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
|
|
|
dirty += bdrv_get_dirty_count(bmds->bs);
|
|
|
|
}
|
|
|
|
|
2013-01-21 17:09:44 +01:00
|
|
|
return dirty << BDRV_SECTOR_BITS;
|
2010-01-26 09:31:49 +01:00
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Called with iothread lock taken. */
|
|
|
|
|
2011-12-05 17:06:56 +01:00
|
|
|
static void blk_mig_cleanup(void)
|
2009-11-30 18:21:21 +01:00
|
|
|
{
|
2009-11-30 18:21:21 +01:00
|
|
|
BlkMigDevState *bmds;
|
|
|
|
BlkMigBlock *blk;
|
2009-11-30 18:21:21 +01:00
|
|
|
|
2012-09-25 15:47:36 +02:00
|
|
|
bdrv_drain_all();
|
|
|
|
|
2011-01-26 15:12:31 +01:00
|
|
|
set_dirty_tracking(0);
|
|
|
|
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2009-11-30 18:21:21 +01:00
|
|
|
while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
|
|
|
|
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
|
2011-01-26 15:12:35 +01:00
|
|
|
bdrv_set_in_use(bmds->bs, 0);
|
2011-01-26 15:12:33 +01:00
|
|
|
drive_put_ref(drive_get_by_blockdev(bmds->bs));
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(bmds->aio_bitmap);
|
|
|
|
g_free(bmds);
|
2009-11-30 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
|
|
|
|
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(blk->buf);
|
|
|
|
g_free(blk);
|
2009-11-30 18:21:21 +01:00
|
|
|
}
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2009-11-30 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
2012-06-26 19:26:41 +02:00
|
|
|
static void block_migration_cancel(void *opaque)
|
|
|
|
{
|
|
|
|
blk_mig_cleanup();
|
|
|
|
}
|
|
|
|
|
2012-06-28 15:11:57 +02:00
|
|
|
static int block_save_setup(QEMUFile *f, void *opaque)
|
2009-11-02 14:40:58 +01:00
|
|
|
{
|
2011-10-19 15:22:18 +02:00
|
|
|
int ret;
|
|
|
|
|
2012-06-28 15:11:57 +02:00
|
|
|
DPRINTF("Enter save live setup submitted %d transferred %d\n",
|
|
|
|
block_mig_state.submitted, block_mig_state.transferred);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2013-02-22 17:36:28 +01:00
|
|
|
qemu_mutex_lock_iothread();
|
2012-06-28 15:11:57 +02:00
|
|
|
init_blk_migration(f);
|
|
|
|
|
|
|
|
/* start track dirty blocks */
|
|
|
|
set_dirty_tracking(1);
|
2013-02-22 17:36:28 +01:00
|
|
|
qemu_mutex_unlock_iothread();
|
2012-06-28 15:11:57 +02:00
|
|
|
|
2012-08-29 20:17:13 +02:00
|
|
|
ret = flush_blks(f);
|
2012-06-28 15:11:57 +02:00
|
|
|
blk_mig_reset_dirty_cursor();
|
|
|
|
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
|
|
|
|
2013-02-22 17:36:11 +01:00
|
|
|
return ret;
|
2012-06-28 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2012-06-28 15:31:37 +02:00
|
|
|
static int block_save_iterate(QEMUFile *f, void *opaque)
|
2012-06-28 15:11:57 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2013-02-12 10:37:15 +01:00
|
|
|
int64_t last_ftell = qemu_ftell(f);
|
2012-06-28 15:11:57 +02:00
|
|
|
|
2012-06-28 15:31:37 +02:00
|
|
|
DPRINTF("Enter save live iterate submitted %d transferred %d\n",
|
|
|
|
block_mig_state.submitted, block_mig_state.transferred);
|
2012-06-28 15:11:57 +02:00
|
|
|
|
2012-08-29 20:17:13 +02:00
|
|
|
ret = flush_blks(f);
|
2011-10-19 15:22:18 +02:00
|
|
|
if (ret) {
|
|
|
|
return ret;
|
2009-11-30 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
2010-01-26 13:04:11 +01:00
|
|
|
blk_mig_reset_dirty_cursor();
|
|
|
|
|
2012-06-28 15:31:37 +02:00
|
|
|
/* control the rate of transfer */
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2012-06-28 15:31:37 +02:00
|
|
|
while ((block_mig_state.submitted +
|
|
|
|
block_mig_state.read_done) * BLOCK_SIZE <
|
|
|
|
qemu_file_get_rate_limit(f)) {
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2012-06-28 15:31:37 +02:00
|
|
|
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;
|
|
|
|
}
|
2013-02-22 17:36:23 +01:00
|
|
|
ret = 0;
|
2012-06-28 15:31:37 +02:00
|
|
|
} else {
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Always called with iothread lock taken for
|
|
|
|
* simplicity, block_save_complete also calls it.
|
|
|
|
*/
|
|
|
|
qemu_mutex_lock_iothread();
|
2012-08-29 21:59:22 +02:00
|
|
|
ret = blk_mig_save_dirty_block(f, 1);
|
2013-02-22 17:36:27 +01:00
|
|
|
qemu_mutex_unlock_iothread();
|
2013-02-22 17:36:23 +01:00
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2013-02-22 17:36:23 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
/* no more dirty blocks */
|
|
|
|
break;
|
2009-11-30 18:21:19 +01:00
|
|
|
}
|
2012-06-28 15:31:37 +02:00
|
|
|
}
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2012-08-29 20:17:13 +02:00
|
|
|
ret = flush_blks(f);
|
2012-06-28 15:31:37 +02:00
|
|
|
if (ret) {
|
|
|
|
return ret;
|
2009-11-30 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
2012-06-28 15:31:37 +02:00
|
|
|
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
2013-02-12 10:37:15 +01:00
|
|
|
return qemu_ftell(f) - last_ftell;
|
2012-06-28 15:31:37 +02:00
|
|
|
}
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
/* Called with iothread lock taken. */
|
|
|
|
|
2012-06-28 15:31:37 +02:00
|
|
|
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);
|
|
|
|
|
2012-08-29 20:17:13 +02:00
|
|
|
ret = flush_blks(f);
|
2012-06-28 15:31:37 +02:00
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2012-06-28 15:31:37 +02:00
|
|
|
blk_mig_reset_dirty_cursor();
|
2009-12-01 15:20:17 +01:00
|
|
|
|
2012-06-28 15:31:37 +02:00
|
|
|
/* we know for sure that save bulk is completed and
|
|
|
|
all async read completed */
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2012-06-28 15:31:37 +02:00
|
|
|
assert(block_mig_state.submitted == 0);
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2012-06-28 15:31:37 +02:00
|
|
|
|
2012-08-29 21:59:22 +02:00
|
|
|
do {
|
|
|
|
ret = blk_mig_save_dirty_block(f, 0);
|
2013-02-22 17:36:11 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2012-08-29 21:59:22 +02:00
|
|
|
} while (ret == 0);
|
2009-11-30 18:21:21 +01:00
|
|
|
|
2012-08-29 21:59:22 +02:00
|
|
|
/* report completion */
|
|
|
|
qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2012-06-28 15:31:37 +02:00
|
|
|
DPRINTF("Block migration completed\n");
|
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
|
|
|
|
2013-02-22 17:36:11 +01:00
|
|
|
blk_mig_cleanup();
|
2012-06-28 15:31:37 +02:00
|
|
|
return 0;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2012-09-21 11:18:18 +02:00
|
|
|
static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
|
|
|
|
{
|
2013-02-12 10:37:15 +01:00
|
|
|
/* Estimate pending number of bytes to send */
|
2013-02-22 17:36:23 +01:00
|
|
|
uint64_t pending;
|
|
|
|
|
2013-02-22 17:36:27 +01:00
|
|
|
qemu_mutex_lock_iothread();
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_lock();
|
2013-02-22 17:36:23 +01:00
|
|
|
pending = get_remaining_dirty() +
|
2013-02-12 10:37:15 +01:00
|
|
|
block_mig_state.submitted * BLOCK_SIZE +
|
|
|
|
block_mig_state.read_done * BLOCK_SIZE;
|
|
|
|
|
|
|
|
/* Report at least one block pending during bulk phase */
|
|
|
|
if (pending == 0 && !block_mig_state.bulk_completed) {
|
|
|
|
pending = BLOCK_SIZE;
|
|
|
|
}
|
2013-02-22 17:36:25 +01:00
|
|
|
blk_mig_unlock();
|
2013-02-22 17:36:27 +01:00
|
|
|
qemu_mutex_unlock_iothread();
|
2012-09-21 11:18:18 +02:00
|
|
|
|
2013-02-12 10:37:15 +01:00
|
|
|
DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
|
|
|
|
return pending;
|
2012-09-21 11:18:18 +02:00
|
|
|
}
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
static int block_load(QEMUFile *f, void *opaque, int version_id)
|
|
|
|
{
|
2009-12-01 15:20:17 +01:00
|
|
|
static int banner_printed;
|
2009-11-02 14:40:58 +01:00
|
|
|
int len, flags;
|
|
|
|
char device_name[256];
|
|
|
|
int64_t addr;
|
2011-01-21 12:42:30 +01:00
|
|
|
BlockDriverState *bs, *bs_prev = NULL;
|
2009-11-02 14:40:58 +01:00
|
|
|
uint8_t *buf;
|
2011-01-21 12:42:30 +01:00
|
|
|
int64_t total_sectors = 0;
|
|
|
|
int nr_sectors;
|
2011-10-05 01:14:46 +02:00
|
|
|
int ret;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
do {
|
|
|
|
addr = qemu_get_be64(f);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
flags = addr & ~BDRV_SECTOR_MASK;
|
|
|
|
addr >>= BDRV_SECTOR_BITS;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
|
|
|
if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
|
2009-11-02 14:40:58 +01:00
|
|
|
/* get device name */
|
|
|
|
len = qemu_get_byte(f);
|
|
|
|
qemu_get_buffer(f, (uint8_t *)device_name, len);
|
|
|
|
device_name[len] = '\0';
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
bs = bdrv_find(device_name);
|
2009-11-30 18:21:21 +01:00
|
|
|
if (!bs) {
|
|
|
|
fprintf(stderr, "Error unknown block device %s\n",
|
|
|
|
device_name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2011-01-21 12:42:30 +01:00
|
|
|
if (bs != bs_prev) {
|
|
|
|
bs_prev = bs;
|
|
|
|
total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
|
|
|
|
if (total_sectors <= 0) {
|
2011-06-22 14:03:54 +02:00
|
|
|
error_report("Error getting length of block device %s",
|
2011-01-21 12:42:30 +01:00
|
|
|
device_name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
|
|
|
|
nr_sectors = total_sectors - addr;
|
|
|
|
} else {
|
|
|
|
nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
|
|
|
|
}
|
|
|
|
|
2013-07-18 09:48:50 +02:00
|
|
|
if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
|
|
|
|
ret = bdrv_write_zeroes(bs, addr, nr_sectors);
|
|
|
|
} else {
|
|
|
|
buf = g_malloc(BLOCK_SIZE);
|
|
|
|
qemu_get_buffer(f, buf, BLOCK_SIZE);
|
|
|
|
ret = bdrv_write(bs, addr, buf, nr_sectors);
|
|
|
|
g_free(buf);
|
|
|
|
}
|
2009-11-30 18:21:20 +01:00
|
|
|
|
2010-07-20 11:19:00 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2009-12-01 15:20:17 +01:00
|
|
|
} 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);
|
2009-11-30 18:21:19 +01:00
|
|
|
} else if (!(flags & BLK_MIG_FLAG_EOS)) {
|
2013-02-10 23:12:44 +01:00
|
|
|
fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
|
2009-11-30 18:21:21 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2011-10-05 01:14:46 +02:00
|
|
|
ret = qemu_file_get_error(f);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
} while (!(flags & BLK_MIG_FLAG_EOS));
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-19 17:43:09 +02:00
|
|
|
static void block_set_params(const MigrationParams *params, void *opaque)
|
2009-11-02 14:40:58 +01:00
|
|
|
{
|
2012-06-19 17:43:09 +02:00
|
|
|
block_mig_state.blk_enable = params->blk;
|
|
|
|
block_mig_state.shared_base = params->shared;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
/* shared base means that blk_enable = 1 */
|
2012-06-19 17:43:09 +02:00
|
|
|
block_mig_state.blk_enable |= params->shared;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2012-06-27 10:59:15 +02:00
|
|
|
static bool block_is_active(void *opaque)
|
|
|
|
{
|
|
|
|
return block_mig_state.blk_enable == 1;
|
|
|
|
}
|
|
|
|
|
2012-06-26 18:46:10 +02:00
|
|
|
SaveVMHandlers savevm_block_handlers = {
|
|
|
|
.set_params = block_set_params,
|
2012-06-28 15:11:57 +02:00
|
|
|
.save_live_setup = block_save_setup,
|
2012-06-28 15:31:37 +02:00
|
|
|
.save_live_iterate = block_save_iterate,
|
|
|
|
.save_live_complete = block_save_complete,
|
2012-09-21 11:18:18 +02:00
|
|
|
.save_live_pending = block_save_pending,
|
2012-06-26 18:46:10 +02:00
|
|
|
.load_state = block_load,
|
2012-06-26 19:26:41 +02:00
|
|
|
.cancel = block_migration_cancel,
|
2012-06-27 10:59:15 +02:00
|
|
|
.is_active = block_is_active,
|
2012-06-26 18:46:10 +02:00
|
|
|
};
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
void blk_mig_init(void)
|
2009-11-30 18:21:19 +01:00
|
|
|
{
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_INIT(&block_mig_state.bmds_list);
|
|
|
|
QSIMPLEQ_INIT(&block_mig_state.blk_list);
|
2013-02-22 17:36:25 +01:00
|
|
|
qemu_mutex_init(&block_mig_state.lock);
|
2009-11-30 18:21:20 +01:00
|
|
|
|
2012-06-26 18:46:10 +02:00
|
|
|
register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
|
|
|
|
&block_mig_state);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|