blob: 9a40edd45710501974ff8c1faf70f97edc3d143c [file] [log] [blame]
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001/*
2 * QEMU live block migration
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Liran Schour <lirans@il.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020014 */
15
16#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010017#include "block/block_int.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020018#include "hw/hw.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/queue.h"
20#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010021#include "migration/block.h"
22#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010023#include "sysemu/blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020024#include <assert.h>
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020025
Paolo Bonzini50717e92013-01-21 17:09:45 +010026#define BLOCK_SIZE (1 << 20)
27#define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020028
29#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
30#define BLK_MIG_FLAG_EOS 0x02
Jan Kiszka01e61e22009-12-01 15:20:17 +010031#define BLK_MIG_FLAG_PROGRESS 0x04
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020032
33#define MAX_IS_ALLOCATED_SEARCH 65536
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020034
35//#define DEBUG_BLK_MIGRATION
36
37#ifdef DEBUG_BLK_MIGRATION
malcd0f2c4c2010-02-07 02:03:50 +030038#define DPRINTF(fmt, ...) \
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020039 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
40#else
malcd0f2c4c2010-02-07 02:03:50 +030041#define DPRINTF(fmt, ...) \
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020042 do { } while (0)
43#endif
44
Jan Kiszkaa55eb922009-11-30 18:21:19 +010045typedef struct BlkMigDevState {
46 BlockDriverState *bs;
47 int bulk_completed;
48 int shared_base;
Jan Kiszkaa55eb922009-11-30 18:21:19 +010049 int64_t cur_sector;
Liran Schourd76cac72010-01-26 14:04:11 +020050 int64_t cur_dirty;
Jan Kiszka82801d82009-11-30 18:21:21 +010051 int64_t completed_sectors;
Jan Kiszkaa55eb922009-11-30 18:21:19 +010052 int64_t total_sectors;
Jan Kiszka5e5328b2009-11-30 18:21:20 +010053 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
Marcelo Tosatti33656af2010-11-08 17:02:56 -020054 unsigned long *aio_bitmap;
Jan Kiszkaa55eb922009-11-30 18:21:19 +010055} BlkMigDevState;
56
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020057typedef struct BlkMigBlock {
58 uint8_t *buf;
59 BlkMigDevState *bmds;
60 int64_t sector;
Marcelo Tosatti33656af2010-11-08 17:02:56 -020061 int nr_sectors;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020062 struct iovec iov;
63 QEMUIOVector qiov;
64 BlockDriverAIOCB *aiocb;
65 int ret;
Jan Kiszka5e5328b2009-11-30 18:21:20 +010066 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020067} BlkMigBlock;
68
69typedef struct BlkMigState {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020070 int blk_enable;
71 int shared_base;
Jan Kiszka5e5328b2009-11-30 18:21:20 +010072 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
73 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020074 int submitted;
75 int read_done;
76 int transferred;
Jan Kiszka82801d82009-11-30 18:21:21 +010077 int64_t total_sector_sum;
Jan Kiszka01e61e22009-12-01 15:20:17 +010078 int prev_progress;
Liran Schoure970ec02010-01-26 10:31:45 +020079 int bulk_completed;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020080} BlkMigState;
81
Jan Kiszkad11ecd32009-11-30 18:21:20 +010082static BlkMigState block_mig_state;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020083
Jan Kiszka13f0b672009-11-30 18:21:21 +010084static void blk_send(QEMUFile *f, BlkMigBlock * blk)
85{
86 int len;
87
88 /* sector number and flags */
89 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
90 | BLK_MIG_FLAG_DEVICE_BLOCK);
91
92 /* device name */
93 len = strlen(blk->bmds->bs->device_name);
94 qemu_put_byte(f, len);
95 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
96
97 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
98}
99
Jan Kiszka25f23642009-11-30 18:21:21 +0100100int blk_mig_active(void)
101{
102 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
103}
104
105uint64_t blk_mig_bytes_transferred(void)
106{
107 BlkMigDevState *bmds;
108 uint64_t sum = 0;
109
110 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
111 sum += bmds->completed_sectors;
112 }
113 return sum << BDRV_SECTOR_BITS;
114}
115
116uint64_t blk_mig_bytes_remaining(void)
117{
118 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
119}
120
121uint64_t blk_mig_bytes_total(void)
122{
123 BlkMigDevState *bmds;
124 uint64_t sum = 0;
125
126 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
127 sum += bmds->total_sectors;
128 }
129 return sum << BDRV_SECTOR_BITS;
130}
131
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200132static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
133{
134 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
135
Marcelo Tosatti62155e22010-11-12 16:07:50 -0200136 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200137 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
138 (1UL << (chunk % (sizeof(unsigned long) * 8))));
139 } else {
140 return 0;
141 }
142}
143
144static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
145 int nb_sectors, int set)
146{
147 int64_t start, end;
148 unsigned long val, idx, bit;
149
150 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
151 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
152
153 for (; start <= end; start++) {
154 idx = start / (sizeof(unsigned long) * 8);
155 bit = start % (sizeof(unsigned long) * 8);
156 val = bmds->aio_bitmap[idx];
157 if (set) {
Marcelo Tosatti62155e22010-11-12 16:07:50 -0200158 val |= 1UL << bit;
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200159 } else {
Marcelo Tosatti62155e22010-11-12 16:07:50 -0200160 val &= ~(1UL << bit);
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200161 }
162 bmds->aio_bitmap[idx] = val;
163 }
164}
165
166static void alloc_aio_bitmap(BlkMigDevState *bmds)
167{
168 BlockDriverState *bs = bmds->bs;
169 int64_t bitmap_size;
170
171 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
172 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
173 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
174
Anthony Liguori7267c092011-08-20 22:09:37 -0500175 bmds->aio_bitmap = g_malloc0(bitmap_size);
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200176}
177
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200178static void blk_mig_read_cb(void *opaque, int ret)
179{
180 BlkMigBlock *blk = opaque;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100181
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200182 blk->ret = ret;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100183
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100184 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200185 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100186
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100187 block_mig_state.submitted--;
188 block_mig_state.read_done++;
189 assert(block_mig_state.submitted >= 0);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200190}
191
Luiz Capitulino539de122011-12-05 14:06:56 -0200192static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100193{
Jan Kiszka57cce122009-11-30 18:21:20 +0100194 int64_t total_sectors = bmds->total_sectors;
195 int64_t cur_sector = bmds->cur_sector;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200196 BlockDriverState *bs = bmds->bs;
Jan Kiszka57cce122009-11-30 18:21:20 +0100197 BlkMigBlock *blk;
Jan Kiszka13f0b672009-11-30 18:21:21 +0100198 int nr_sectors;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100199
200 if (bmds->shared_base) {
Jan Kiszkab1d10852009-11-30 18:21:20 +0100201 while (cur_sector < total_sectors &&
Jan Kiszka57cce122009-11-30 18:21:20 +0100202 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
203 &nr_sectors)) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200204 cur_sector += nr_sectors;
205 }
206 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100207
208 if (cur_sector >= total_sectors) {
Jan Kiszka82801d82009-11-30 18:21:21 +0100209 bmds->cur_sector = bmds->completed_sectors = total_sectors;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200210 return 1;
211 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100212
Jan Kiszka82801d82009-11-30 18:21:21 +0100213 bmds->completed_sectors = cur_sector;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100214
Jan Kiszka6ea44302009-11-30 18:21:19 +0100215 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100216
Jan Kiszka6ea44302009-11-30 18:21:19 +0100217 /* we are going to transfer a full block even if it is not allocated */
218 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100219
Jan Kiszka6ea44302009-11-30 18:21:19 +0100220 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
Jan Kiszka57cce122009-11-30 18:21:20 +0100221 nr_sectors = total_sectors - cur_sector;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200222 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100223
Anthony Liguori7267c092011-08-20 22:09:37 -0500224 blk = g_malloc(sizeof(BlkMigBlock));
225 blk->buf = g_malloc(BLOCK_SIZE);
Jan Kiszka13f0b672009-11-30 18:21:21 +0100226 blk->bmds = bmds;
227 blk->sector = cur_sector;
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200228 blk->nr_sectors = nr_sectors;
Jan Kiszka13f0b672009-11-30 18:21:21 +0100229
Liran Schoure970ec02010-01-26 10:31:45 +0200230 blk->iov.iov_base = blk->buf;
231 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
232 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
Jan Kiszka57cce122009-11-30 18:21:20 +0100233
Paolo Bonzini13197e32013-02-22 17:36:23 +0100234 block_mig_state.submitted++;
235
Liran Schoure970ec02010-01-26 10:31:45 +0200236 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
237 nr_sectors, blk_mig_read_cb, blk);
Liran Schourd76cac72010-01-26 14:04:11 +0200238
Jan Kiszka13f0b672009-11-30 18:21:21 +0100239 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
240 bmds->cur_sector = cur_sector + nr_sectors;
241
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200242 return (bmds->cur_sector >= total_sectors);
243}
244
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200245static void set_dirty_tracking(int enable)
246{
247 BlkMigDevState *bmds;
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100248
249 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
Paolo Bonzini50717e92013-01-21 17:09:45 +0100250 bdrv_set_dirty_tracking(bmds->bs, enable ? BLOCK_SIZE : 0);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200251 }
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200252}
253
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100254static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200255{
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100256 BlkMigDevState *bmds;
Jan Kiszka792773b2009-11-30 20:34:55 +0100257 int64_t sectors;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100258
Markus Armbrusterd2466732010-06-28 10:45:02 +0200259 if (!bdrv_is_read_only(bs)) {
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100260 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
Shahar Havivi31f54f22010-07-10 18:59:06 +0300261 if (sectors <= 0) {
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100262 return;
263 }
264
Anthony Liguori7267c092011-08-20 22:09:37 -0500265 bmds = g_malloc0(sizeof(BlkMigDevState));
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100266 bmds->bs = bs;
267 bmds->bulk_completed = 0;
268 bmds->total_sectors = sectors;
269 bmds->completed_sectors = 0;
270 bmds->shared_base = block_mig_state.shared_base;
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200271 alloc_aio_bitmap(bmds);
Marcelo Tosattif48905d2011-01-26 12:12:33 -0200272 drive_get_ref(drive_get_by_blockdev(bs));
Marcelo Tosatti85916752011-01-26 12:12:35 -0200273 bdrv_set_in_use(bs, 1);
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100274
275 block_mig_state.total_sector_sum += sectors;
276
277 if (bmds->shared_base) {
Luiz Capitulino539de122011-12-05 14:06:56 -0200278 DPRINTF("Start migration for %s with shared base image\n",
279 bs->device_name);
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100280 } else {
Luiz Capitulino539de122011-12-05 14:06:56 -0200281 DPRINTF("Start full migration for %s\n", bs->device_name);
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100282 }
283
284 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
285 }
286}
287
Luiz Capitulino539de122011-12-05 14:06:56 -0200288static void init_blk_migration(QEMUFile *f)
Stefan Hajnoczib66460e2010-04-09 15:22:13 +0100289{
Jan Kiszka69d63a92009-11-30 18:21:20 +0100290 block_mig_state.submitted = 0;
291 block_mig_state.read_done = 0;
292 block_mig_state.transferred = 0;
Jan Kiszka82801d82009-11-30 18:21:21 +0100293 block_mig_state.total_sector_sum = 0;
Jan Kiszka01e61e22009-12-01 15:20:17 +0100294 block_mig_state.prev_progress = -1;
Liran Schoure970ec02010-01-26 10:31:45 +0200295 block_mig_state.bulk_completed = 0;
Jan Kiszka69d63a92009-11-30 18:21:20 +0100296
Luiz Capitulino539de122011-12-05 14:06:56 -0200297 bdrv_iterate(init_blk_migration_it, NULL);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200298}
299
Luiz Capitulino539de122011-12-05 14:06:56 -0200300static int blk_mig_save_bulked_block(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200301{
Jan Kiszka82801d82009-11-30 18:21:21 +0100302 int64_t completed_sector_sum = 0;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200303 BlkMigDevState *bmds;
Jan Kiszka01e61e22009-12-01 15:20:17 +0100304 int progress;
Jan Kiszka82801d82009-11-30 18:21:21 +0100305 int ret = 0;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200306
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100307 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100308 if (bmds->bulk_completed == 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -0200309 if (mig_save_device_bulk(f, bmds) == 1) {
Jan Kiszka57cce122009-11-30 18:21:20 +0100310 /* completed bulk section for this device */
311 bmds->bulk_completed = 1;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200312 }
Jan Kiszka82801d82009-11-30 18:21:21 +0100313 completed_sector_sum += bmds->completed_sectors;
314 ret = 1;
315 break;
316 } else {
317 completed_sector_sum += bmds->completed_sectors;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200318 }
319 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100320
Pierre Riteau8b6b2af2011-01-12 14:41:00 +0100321 if (block_mig_state.total_sector_sum != 0) {
322 progress = completed_sector_sum * 100 /
323 block_mig_state.total_sector_sum;
324 } else {
325 progress = 100;
326 }
Jan Kiszka01e61e22009-12-01 15:20:17 +0100327 if (progress != block_mig_state.prev_progress) {
328 block_mig_state.prev_progress = progress;
329 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
330 | BLK_MIG_FLAG_PROGRESS);
Luiz Capitulino539de122011-12-05 14:06:56 -0200331 DPRINTF("Completed %d %%\r", progress);
Jan Kiszka82801d82009-11-30 18:21:21 +0100332 }
333
334 return ret;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200335}
336
Liran Schourd76cac72010-01-26 14:04:11 +0200337static void blk_mig_reset_dirty_cursor(void)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200338{
339 BlkMigDevState *bmds;
Jan Kiszka575a58d2009-11-30 18:21:20 +0100340
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100341 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
Liran Schourd76cac72010-01-26 14:04:11 +0200342 bmds->cur_dirty = 0;
343 }
344}
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100345
Luiz Capitulino539de122011-12-05 14:06:56 -0200346static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
347 int is_async)
Liran Schourd76cac72010-01-26 14:04:11 +0200348{
349 BlkMigBlock *blk;
350 int64_t total_sectors = bmds->total_sectors;
351 int64_t sector;
352 int nr_sectors;
Juan Quinteladcd1d222011-09-21 23:01:54 +0200353 int ret = -EIO;
Liran Schourd76cac72010-01-26 14:04:11 +0200354
355 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
Marcelo Tosatti62155e22010-11-12 16:07:50 -0200356 if (bmds_aio_inflight(bmds, sector)) {
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000357 bdrv_drain_all();
Marcelo Tosatti62155e22010-11-12 16:07:50 -0200358 }
Liran Schourd76cac72010-01-26 14:04:11 +0200359 if (bdrv_get_dirty(bmds->bs, sector)) {
360
361 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
362 nr_sectors = total_sectors - sector;
363 } else {
364 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100365 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500366 blk = g_malloc(sizeof(BlkMigBlock));
367 blk->buf = g_malloc(BLOCK_SIZE);
Liran Schourd76cac72010-01-26 14:04:11 +0200368 blk->bmds = bmds;
369 blk->sector = sector;
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200370 blk->nr_sectors = nr_sectors;
Liran Schourd76cac72010-01-26 14:04:11 +0200371
Liran Schour889ae392010-01-26 10:31:49 +0200372 if (is_async) {
Liran Schourd76cac72010-01-26 14:04:11 +0200373 blk->iov.iov_base = blk->buf;
374 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
375 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
376
377 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
378 nr_sectors, blk_mig_read_cb, blk);
Liran Schourd76cac72010-01-26 14:04:11 +0200379 block_mig_state.submitted++;
Marcelo Tosatti33656af2010-11-08 17:02:56 -0200380 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
Liran Schourd76cac72010-01-26 14:04:11 +0200381 } else {
Juan Quinteladcd1d222011-09-21 23:01:54 +0200382 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
383 if (ret < 0) {
Liran Schourd76cac72010-01-26 14:04:11 +0200384 goto error;
385 }
386 blk_send(f, blk);
387
Anthony Liguori7267c092011-08-20 22:09:37 -0500388 g_free(blk->buf);
389 g_free(blk);
Liran Schourd76cac72010-01-26 14:04:11 +0200390 }
391
392 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
393 break;
394 }
395 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
396 bmds->cur_dirty = sector;
397 }
398
399 return (bmds->cur_dirty >= bmds->total_sectors);
400
Liran Schour889ae392010-01-26 10:31:49 +0200401error:
Luiz Capitulino539de122011-12-05 14:06:56 -0200402 DPRINTF("Error reading sector %" PRId64 "\n", sector);
Anthony Liguori7267c092011-08-20 22:09:37 -0500403 g_free(blk->buf);
404 g_free(blk);
Juan Quintela43be3a22012-08-29 21:59:22 +0200405 return ret;
Liran Schourd76cac72010-01-26 14:04:11 +0200406}
407
Juan Quintelaceb2bd02012-08-29 21:37:14 +0200408/* return value:
409 * 0: too much data for max_downtime
410 * 1: few enough data for max_downtime
411*/
Luiz Capitulino539de122011-12-05 14:06:56 -0200412static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
Liran Schourd76cac72010-01-26 14:04:11 +0200413{
414 BlkMigDevState *bmds;
Juan Quintelaceb2bd02012-08-29 21:37:14 +0200415 int ret = 1;
Liran Schourd76cac72010-01-26 14:04:11 +0200416
417 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
Juan Quintelaceb2bd02012-08-29 21:37:14 +0200418 ret = mig_save_device_dirty(f, bmds, is_async);
Juan Quintela43be3a22012-08-29 21:59:22 +0200419 if (ret <= 0) {
Liran Schourd76cac72010-01-26 14:04:11 +0200420 break;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200421 }
422 }
Jan Kiszka575a58d2009-11-30 18:21:20 +0100423
Liran Schourd76cac72010-01-26 14:04:11 +0200424 return ret;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200425}
426
Juan Quintela59feec42012-08-29 20:17:13 +0200427static int flush_blks(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200428{
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100429 BlkMigBlock *blk;
Juan Quintela59feec42012-08-29 20:17:13 +0200430 int ret = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100431
malcd0f2c4c2010-02-07 02:03:50 +0300432 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100433 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
434 block_mig_state.transferred);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100435
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100436 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
437 if (qemu_file_rate_limit(f)) {
438 break;
439 }
Jan Kiszka4b640362009-11-30 18:21:21 +0100440 if (blk->ret < 0) {
Juan Quintela59feec42012-08-29 20:17:13 +0200441 ret = blk->ret;
Jan Kiszka4b640362009-11-30 18:21:21 +0100442 break;
443 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100444
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100445 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
Paolo Bonzini13197e32013-02-22 17:36:23 +0100446 blk_send(f, blk);
447
Anthony Liguori7267c092011-08-20 22:09:37 -0500448 g_free(blk->buf);
449 g_free(blk);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100450
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100451 block_mig_state.read_done--;
452 block_mig_state.transferred++;
453 assert(block_mig_state.read_done >= 0);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200454 }
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200455
malcd0f2c4c2010-02-07 02:03:50 +0300456 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
Jan Kiszkad11ecd32009-11-30 18:21:20 +0100457 block_mig_state.submitted, block_mig_state.read_done,
458 block_mig_state.transferred);
Juan Quintela59feec42012-08-29 20:17:13 +0200459 return ret;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200460}
461
Liran Schour889ae392010-01-26 10:31:49 +0200462static int64_t get_remaining_dirty(void)
463{
464 BlkMigDevState *bmds;
465 int64_t dirty = 0;
466
467 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
468 dirty += bdrv_get_dirty_count(bmds->bs);
469 }
470
Paolo Bonziniacc906c2013-01-21 17:09:44 +0100471 return dirty << BDRV_SECTOR_BITS;
Liran Schour889ae392010-01-26 10:31:49 +0200472}
473
Luiz Capitulino539de122011-12-05 14:06:56 -0200474static void blk_mig_cleanup(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100475{
Jan Kiszka82801d82009-11-30 18:21:21 +0100476 BlkMigDevState *bmds;
477 BlkMigBlock *blk;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100478
Kevin Wolf946d58b2012-09-25 15:47:36 +0200479 bdrv_drain_all();
480
Marcelo Tosatti8f794c52011-01-26 12:12:31 -0200481 set_dirty_tracking(0);
482
Jan Kiszka82801d82009-11-30 18:21:21 +0100483 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
484 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
Marcelo Tosatti85916752011-01-26 12:12:35 -0200485 bdrv_set_in_use(bmds->bs, 0);
Marcelo Tosattif48905d2011-01-26 12:12:33 -0200486 drive_put_ref(drive_get_by_blockdev(bmds->bs));
Anthony Liguori7267c092011-08-20 22:09:37 -0500487 g_free(bmds->aio_bitmap);
488 g_free(bmds);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100489 }
490
Jan Kiszka82801d82009-11-30 18:21:21 +0100491 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
492 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -0500493 g_free(blk->buf);
494 g_free(blk);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100495 }
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100496}
497
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200498static void block_migration_cancel(void *opaque)
499{
500 blk_mig_cleanup();
501}
502
Juan Quintelad1315aa2012-06-28 15:11:57 +0200503static int block_save_setup(QEMUFile *f, void *opaque)
504{
505 int ret;
506
507 DPRINTF("Enter save live setup submitted %d transferred %d\n",
508 block_mig_state.submitted, block_mig_state.transferred);
509
510 init_blk_migration(f);
511
512 /* start track dirty blocks */
513 set_dirty_tracking(1);
514
Juan Quintela59feec42012-08-29 20:17:13 +0200515 ret = flush_blks(f);
Juan Quintelad1315aa2012-06-28 15:11:57 +0200516 blk_mig_reset_dirty_cursor();
Juan Quintelad1315aa2012-06-28 15:11:57 +0200517 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
518
Paolo Bonzinid418cf52013-02-22 17:36:11 +0100519 return ret;
Juan Quintelad1315aa2012-06-28 15:11:57 +0200520}
521
Juan Quintela16310a32012-06-28 15:31:37 +0200522static int block_save_iterate(QEMUFile *f, void *opaque)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200523{
Juan Quintela29757252011-10-19 15:22:18 +0200524 int ret;
Stefan Hajnoczi6aaa9da2013-02-12 10:37:15 +0100525 int64_t last_ftell = qemu_ftell(f);
Juan Quintela29757252011-10-19 15:22:18 +0200526
Juan Quintela16310a32012-06-28 15:31:37 +0200527 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
528 block_mig_state.submitted, block_mig_state.transferred);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100529
Juan Quintela59feec42012-08-29 20:17:13 +0200530 ret = flush_blks(f);
Juan Quintela29757252011-10-19 15:22:18 +0200531 if (ret) {
Juan Quintela29757252011-10-19 15:22:18 +0200532 return ret;
Jan Kiszka4b640362009-11-30 18:21:21 +0100533 }
534
Liran Schourd76cac72010-01-26 14:04:11 +0200535 blk_mig_reset_dirty_cursor();
536
Juan Quintela16310a32012-06-28 15:31:37 +0200537 /* control the rate of transfer */
538 while ((block_mig_state.submitted +
539 block_mig_state.read_done) * BLOCK_SIZE <
540 qemu_file_get_rate_limit(f)) {
541 if (block_mig_state.bulk_completed == 0) {
542 /* first finish the bulk phase */
543 if (blk_mig_save_bulked_block(f) == 0) {
544 /* finished saving bulk on all devices */
545 block_mig_state.bulk_completed = 1;
Liran Schourd76cac72010-01-26 14:04:11 +0200546 }
Paolo Bonzini13197e32013-02-22 17:36:23 +0100547 ret = 0;
Juan Quintela16310a32012-06-28 15:31:37 +0200548 } else {
Juan Quintela43be3a22012-08-29 21:59:22 +0200549 ret = blk_mig_save_dirty_block(f, 1);
Paolo Bonzini13197e32013-02-22 17:36:23 +0100550 }
551 if (ret < 0) {
552 return ret;
553 }
554 if (ret != 0) {
555 /* no more dirty blocks */
556 break;
Liran Schourd76cac72010-01-26 14:04:11 +0200557 }
Jan Kiszka4b640362009-11-30 18:21:21 +0100558 }
559
Juan Quintela59feec42012-08-29 20:17:13 +0200560 ret = flush_blks(f);
Juan Quintela16310a32012-06-28 15:31:37 +0200561 if (ret) {
Juan Quintela16310a32012-06-28 15:31:37 +0200562 return ret;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200563 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100564
565 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
Stefan Hajnoczi6aaa9da2013-02-12 10:37:15 +0100566 return qemu_ftell(f) - last_ftell;
Juan Quintela16310a32012-06-28 15:31:37 +0200567}
568
569static int block_save_complete(QEMUFile *f, void *opaque)
570{
571 int ret;
572
573 DPRINTF("Enter save live complete submitted %d transferred %d\n",
574 block_mig_state.submitted, block_mig_state.transferred);
575
Juan Quintela59feec42012-08-29 20:17:13 +0200576 ret = flush_blks(f);
Juan Quintela16310a32012-06-28 15:31:37 +0200577 if (ret) {
Juan Quintela16310a32012-06-28 15:31:37 +0200578 return ret;
579 }
580
581 blk_mig_reset_dirty_cursor();
582
583 /* we know for sure that save bulk is completed and
584 all async read completed */
585 assert(block_mig_state.submitted == 0);
586
Juan Quintela43be3a22012-08-29 21:59:22 +0200587 do {
588 ret = blk_mig_save_dirty_block(f, 0);
Paolo Bonzinid418cf52013-02-22 17:36:11 +0100589 if (ret < 0) {
590 return ret;
591 }
Juan Quintela43be3a22012-08-29 21:59:22 +0200592 } while (ret == 0);
593
Juan Quintela43be3a22012-08-29 21:59:22 +0200594 /* report completion */
595 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
Juan Quintela16310a32012-06-28 15:31:37 +0200596
597 DPRINTF("Block migration completed\n");
598
599 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
600
Paolo Bonzinid418cf52013-02-22 17:36:11 +0100601 blk_mig_cleanup();
Juan Quintela16310a32012-06-28 15:31:37 +0200602 return 0;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200603}
604
Juan Quintelae4ed1542012-09-21 11:18:18 +0200605static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
606{
Stefan Hajnoczi6aaa9da2013-02-12 10:37:15 +0100607 /* Estimate pending number of bytes to send */
Paolo Bonzini13197e32013-02-22 17:36:23 +0100608 uint64_t pending;
609
610 pending = get_remaining_dirty() +
Stefan Hajnoczi6aaa9da2013-02-12 10:37:15 +0100611 block_mig_state.submitted * BLOCK_SIZE +
612 block_mig_state.read_done * BLOCK_SIZE;
Juan Quintelae4ed1542012-09-21 11:18:18 +0200613
Stefan Hajnoczi6aaa9da2013-02-12 10:37:15 +0100614 /* Report at least one block pending during bulk phase */
615 if (pending == 0 && !block_mig_state.bulk_completed) {
616 pending = BLOCK_SIZE;
617 }
Juan Quintelae4ed1542012-09-21 11:18:18 +0200618
Stefan Hajnoczi6aaa9da2013-02-12 10:37:15 +0100619 DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
620 return pending;
Juan Quintelae4ed1542012-09-21 11:18:18 +0200621}
622
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200623static int block_load(QEMUFile *f, void *opaque, int version_id)
624{
Jan Kiszka01e61e22009-12-01 15:20:17 +0100625 static int banner_printed;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200626 int len, flags;
627 char device_name[256];
628 int64_t addr;
Pierre Riteau77358b52011-01-21 12:42:30 +0100629 BlockDriverState *bs, *bs_prev = NULL;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200630 uint8_t *buf;
Pierre Riteau77358b52011-01-21 12:42:30 +0100631 int64_t total_sectors = 0;
632 int nr_sectors;
Juan Quintela42802d42011-10-05 01:14:46 +0200633 int ret;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100634
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200635 do {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200636 addr = qemu_get_be64(f);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100637
Jan Kiszka6ea44302009-11-30 18:21:19 +0100638 flags = addr & ~BDRV_SECTOR_MASK;
639 addr >>= BDRV_SECTOR_BITS;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100640
641 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200642 /* get device name */
643 len = qemu_get_byte(f);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200644 qemu_get_buffer(f, (uint8_t *)device_name, len);
645 device_name[len] = '\0';
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100646
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200647 bs = bdrv_find(device_name);
Jan Kiszka4b640362009-11-30 18:21:21 +0100648 if (!bs) {
649 fprintf(stderr, "Error unknown block device %s\n",
650 device_name);
651 return -EINVAL;
652 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100653
Pierre Riteau77358b52011-01-21 12:42:30 +0100654 if (bs != bs_prev) {
655 bs_prev = bs;
656 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
657 if (total_sectors <= 0) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200658 error_report("Error getting length of block device %s",
Pierre Riteau77358b52011-01-21 12:42:30 +0100659 device_name);
660 return -EINVAL;
661 }
662 }
663
664 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
665 nr_sectors = total_sectors - addr;
666 } else {
667 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
668 }
669
Anthony Liguori7267c092011-08-20 22:09:37 -0500670 buf = g_malloc(BLOCK_SIZE);
Jan Kiszka575a58d2009-11-30 18:21:20 +0100671
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100672 qemu_get_buffer(f, buf, BLOCK_SIZE);
Pierre Riteau77358b52011-01-21 12:42:30 +0100673 ret = bdrv_write(bs, addr, buf, nr_sectors);
Jan Kiszka575a58d2009-11-30 18:21:20 +0100674
Anthony Liguori7267c092011-08-20 22:09:37 -0500675 g_free(buf);
Yoshiaki Tamurab02bea32010-07-20 18:19:00 +0900676 if (ret < 0) {
677 return ret;
678 }
Jan Kiszka01e61e22009-12-01 15:20:17 +0100679 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
680 if (!banner_printed) {
681 printf("Receiving block device images\n");
682 banner_printed = 1;
683 }
684 printf("Completed %d %%%c", (int)addr,
685 (addr == 100) ? '\n' : '\r');
686 fflush(stdout);
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100687 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
Stefan Hajnoczid5f1f282013-02-10 23:12:44 +0100688 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
Jan Kiszka4b640362009-11-30 18:21:21 +0100689 return -EINVAL;
690 }
Juan Quintela42802d42011-10-05 01:14:46 +0200691 ret = qemu_file_get_error(f);
692 if (ret != 0) {
693 return ret;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200694 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100695 } while (!(flags & BLK_MIG_FLAG_EOS));
696
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200697 return 0;
698}
699
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300700static void block_set_params(const MigrationParams *params, void *opaque)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200701{
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300702 block_mig_state.blk_enable = params->blk;
703 block_mig_state.shared_base = params->shared;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100704
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200705 /* shared base means that blk_enable = 1 */
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300706 block_mig_state.blk_enable |= params->shared;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200707}
708
Juan Quintela6bd68782012-06-27 10:59:15 +0200709static bool block_is_active(void *opaque)
710{
711 return block_mig_state.blk_enable == 1;
712}
713
Juan Quintela7908c782012-06-26 18:46:10 +0200714SaveVMHandlers savevm_block_handlers = {
715 .set_params = block_set_params,
Juan Quintelad1315aa2012-06-28 15:11:57 +0200716 .save_live_setup = block_save_setup,
Juan Quintela16310a32012-06-28 15:31:37 +0200717 .save_live_iterate = block_save_iterate,
718 .save_live_complete = block_save_complete,
Juan Quintelae4ed1542012-09-21 11:18:18 +0200719 .save_live_pending = block_save_pending,
Juan Quintela7908c782012-06-26 18:46:10 +0200720 .load_state = block_load,
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200721 .cancel = block_migration_cancel,
Juan Quintela6bd68782012-06-27 10:59:15 +0200722 .is_active = block_is_active,
Juan Quintela7908c782012-06-26 18:46:10 +0200723};
724
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200725void blk_mig_init(void)
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100726{
Jan Kiszka5e5328b2009-11-30 18:21:20 +0100727 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
728 QSIMPLEQ_INIT(&block_mig_state.blk_list);
729
Juan Quintela7908c782012-06-26 18:46:10 +0200730 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
731 &block_mig_state);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200732}