]> git.proxmox.com Git - qemu.git/blame - block-migration.c
cirrus_vga: do not reset videoram
[qemu.git] / block-migration.c
CommitLineData
c163b5ca 1/*
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 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
c163b5ca 14 */
15
16#include "qemu-common.h"
17#include "block_int.h"
18#include "hw/hw.h"
5e5328be 19#include "qemu-queue.h"
889ae39c 20#include "qemu-timer.h"
7184049e 21#include "monitor.h"
c163b5ca 22#include "block-migration.h"
889ae39c 23#include "migration.h"
f48905d4 24#include "blockdev.h"
c163b5ca 25#include <assert.h>
c163b5ca 26
6ea44308 27#define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
c163b5ca 28
29#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
30#define BLK_MIG_FLAG_EOS 0x02
01e61e2d 31#define BLK_MIG_FLAG_PROGRESS 0x04
c163b5ca 32
33#define MAX_IS_ALLOCATED_SEARCH 65536
c163b5ca 34
35//#define DEBUG_BLK_MIGRATION
36
37#ifdef DEBUG_BLK_MIGRATION
d0f2c4c6 38#define DPRINTF(fmt, ...) \
c163b5ca 39 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
40#else
d0f2c4c6 41#define DPRINTF(fmt, ...) \
c163b5ca 42 do { } while (0)
43#endif
44
a55eb92c
JK
45typedef struct BlkMigDevState {
46 BlockDriverState *bs;
47 int bulk_completed;
48 int shared_base;
a55eb92c 49 int64_t cur_sector;
d76cac7d 50 int64_t cur_dirty;
82801d8f 51 int64_t completed_sectors;
a55eb92c
JK
52 int64_t total_sectors;
53 int64_t dirty;
5e5328be 54 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
33656af7 55 unsigned long *aio_bitmap;
a55eb92c
JK
56} BlkMigDevState;
57
c163b5ca 58typedef struct BlkMigBlock {
59 uint8_t *buf;
60 BlkMigDevState *bmds;
61 int64_t sector;
33656af7 62 int nr_sectors;
c163b5ca 63 struct iovec iov;
64 QEMUIOVector qiov;
65 BlockDriverAIOCB *aiocb;
66 int ret;
5e5328be 67 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
c163b5ca 68} BlkMigBlock;
69
70typedef struct BlkMigState {
c163b5ca 71 int blk_enable;
72 int shared_base;
5e5328be
JK
73 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
74 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
c163b5ca 75 int submitted;
76 int read_done;
77 int transferred;
82801d8f 78 int64_t total_sector_sum;
01e61e2d 79 int prev_progress;
e970ec0b 80 int bulk_completed;
889ae39c 81 long double total_time;
ff5c52a3 82 long double prev_time_offset;
889ae39c 83 int reads;
c163b5ca 84} BlkMigState;
85
d11ecd3d 86static BlkMigState block_mig_state;
c163b5ca 87
13f0b67f
JK
88static void blk_send(QEMUFile *f, BlkMigBlock * blk)
89{
90 int len;
91
92 /* sector number and flags */
93 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
94 | BLK_MIG_FLAG_DEVICE_BLOCK);
95
96 /* device name */
97 len = strlen(blk->bmds->bs->device_name);
98 qemu_put_byte(f, len);
99 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
100
101 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
102}
103
25f23643
JK
104int blk_mig_active(void)
105{
106 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
107}
108
109uint64_t blk_mig_bytes_transferred(void)
110{
111 BlkMigDevState *bmds;
112 uint64_t sum = 0;
113
114 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
115 sum += bmds->completed_sectors;
116 }
117 return sum << BDRV_SECTOR_BITS;
118}
119
120uint64_t blk_mig_bytes_remaining(void)
121{
122 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
123}
124
125uint64_t blk_mig_bytes_total(void)
126{
127 BlkMigDevState *bmds;
128 uint64_t sum = 0;
129
130 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
131 sum += bmds->total_sectors;
132 }
133 return sum << BDRV_SECTOR_BITS;
134}
135
889ae39c
LS
136static inline long double compute_read_bwidth(void)
137{
138 assert(block_mig_state.total_time != 0);
155eb9aa 139 return (block_mig_state.reads / block_mig_state.total_time) * BLOCK_SIZE;
889ae39c
LS
140}
141
33656af7
MT
142static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
143{
144 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
145
62155e2b 146 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
33656af7
MT
147 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
148 (1UL << (chunk % (sizeof(unsigned long) * 8))));
149 } else {
150 return 0;
151 }
152}
153
154static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
155 int nb_sectors, int set)
156{
157 int64_t start, end;
158 unsigned long val, idx, bit;
159
160 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
161 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
162
163 for (; start <= end; start++) {
164 idx = start / (sizeof(unsigned long) * 8);
165 bit = start % (sizeof(unsigned long) * 8);
166 val = bmds->aio_bitmap[idx];
167 if (set) {
62155e2b 168 val |= 1UL << bit;
33656af7 169 } else {
62155e2b 170 val &= ~(1UL << bit);
33656af7
MT
171 }
172 bmds->aio_bitmap[idx] = val;
173 }
174}
175
176static void alloc_aio_bitmap(BlkMigDevState *bmds)
177{
178 BlockDriverState *bs = bmds->bs;
179 int64_t bitmap_size;
180
181 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
182 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
183 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
184
7267c094 185 bmds->aio_bitmap = g_malloc0(bitmap_size);
33656af7
MT
186}
187
c163b5ca 188static void blk_mig_read_cb(void *opaque, int ret)
189{
ff5c52a3 190 long double curr_time = qemu_get_clock_ns(rt_clock);
c163b5ca 191 BlkMigBlock *blk = opaque;
a55eb92c 192
c163b5ca 193 blk->ret = ret;
a55eb92c 194
ff5c52a3
AT
195 block_mig_state.reads++;
196 block_mig_state.total_time += (curr_time - block_mig_state.prev_time_offset);
197 block_mig_state.prev_time_offset = curr_time;
889ae39c 198
5e5328be 199 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
33656af7 200 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
a55eb92c 201
d11ecd3d
JK
202 block_mig_state.submitted--;
203 block_mig_state.read_done++;
204 assert(block_mig_state.submitted >= 0);
c163b5ca 205}
206
7184049e 207static int mig_save_device_bulk(Monitor *mon, QEMUFile *f,
e970ec0b 208 BlkMigDevState *bmds)
a55eb92c 209{
57cce12d
JK
210 int64_t total_sectors = bmds->total_sectors;
211 int64_t cur_sector = bmds->cur_sector;
212 BlockDriverState *bs = bmds->bs;
c163b5ca 213 BlkMigBlock *blk;
13f0b67f 214 int nr_sectors;
a55eb92c 215
57cce12d 216 if (bmds->shared_base) {
b1d10856 217 while (cur_sector < total_sectors &&
57cce12d
JK
218 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
219 &nr_sectors)) {
c163b5ca 220 cur_sector += nr_sectors;
221 }
222 }
a55eb92c
JK
223
224 if (cur_sector >= total_sectors) {
82801d8f 225 bmds->cur_sector = bmds->completed_sectors = total_sectors;
c163b5ca 226 return 1;
227 }
a55eb92c 228
82801d8f 229 bmds->completed_sectors = cur_sector;
a55eb92c 230
57cce12d
JK
231 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
232
6ea44308
JK
233 /* we are going to transfer a full block even if it is not allocated */
234 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
c163b5ca 235
6ea44308 236 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
57cce12d 237 nr_sectors = total_sectors - cur_sector;
c163b5ca 238 }
a55eb92c 239
7267c094
AL
240 blk = g_malloc(sizeof(BlkMigBlock));
241 blk->buf = g_malloc(BLOCK_SIZE);
13f0b67f
JK
242 blk->bmds = bmds;
243 blk->sector = cur_sector;
33656af7 244 blk->nr_sectors = nr_sectors;
a55eb92c 245
e970ec0b
LS
246 blk->iov.iov_base = blk->buf;
247 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
248 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
a55eb92c 249
ff5c52a3
AT
250 if (block_mig_state.submitted == 0) {
251 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
252 }
889ae39c 253
e970ec0b
LS
254 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
255 nr_sectors, blk_mig_read_cb, blk);
e970ec0b 256 block_mig_state.submitted++;
d76cac7d 257
13f0b67f
JK
258 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
259 bmds->cur_sector = cur_sector + nr_sectors;
a55eb92c 260
13f0b67f 261 return (bmds->cur_sector >= total_sectors);
c163b5ca 262}
263
c163b5ca 264static void set_dirty_tracking(int enable)
265{
266 BlkMigDevState *bmds;
5e5328be
JK
267
268 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 269 bdrv_set_dirty_tracking(bmds->bs, enable);
c163b5ca 270 }
c163b5ca 271}
272
b66460e4 273static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
c163b5ca 274{
b66460e4 275 Monitor *mon = opaque;
5e5328be 276 BlkMigDevState *bmds;
792773b2 277 int64_t sectors;
a55eb92c 278
d246673d 279 if (!bdrv_is_read_only(bs)) {
b66460e4 280 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
31f54f24 281 if (sectors <= 0) {
b66460e4
SH
282 return;
283 }
284
7267c094 285 bmds = g_malloc0(sizeof(BlkMigDevState));
b66460e4
SH
286 bmds->bs = bs;
287 bmds->bulk_completed = 0;
288 bmds->total_sectors = sectors;
289 bmds->completed_sectors = 0;
290 bmds->shared_base = block_mig_state.shared_base;
33656af7 291 alloc_aio_bitmap(bmds);
f48905d4 292 drive_get_ref(drive_get_by_blockdev(bs));
8591675f 293 bdrv_set_in_use(bs, 1);
b66460e4
SH
294
295 block_mig_state.total_sector_sum += sectors;
296
297 if (bmds->shared_base) {
298 monitor_printf(mon, "Start migration for %s with shared base "
299 "image\n",
300 bs->device_name);
301 } else {
302 monitor_printf(mon, "Start full migration for %s\n",
303 bs->device_name);
304 }
305
306 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
307 }
308}
309
310static void init_blk_migration(Monitor *mon, QEMUFile *f)
311{
69d63a97
JK
312 block_mig_state.submitted = 0;
313 block_mig_state.read_done = 0;
314 block_mig_state.transferred = 0;
82801d8f 315 block_mig_state.total_sector_sum = 0;
01e61e2d 316 block_mig_state.prev_progress = -1;
e970ec0b 317 block_mig_state.bulk_completed = 0;
889ae39c
LS
318 block_mig_state.total_time = 0;
319 block_mig_state.reads = 0;
69d63a97 320
b66460e4 321 bdrv_iterate(init_blk_migration_it, mon);
c163b5ca 322}
323
e970ec0b 324static int blk_mig_save_bulked_block(Monitor *mon, QEMUFile *f)
c163b5ca 325{
82801d8f 326 int64_t completed_sector_sum = 0;
c163b5ca 327 BlkMigDevState *bmds;
01e61e2d 328 int progress;
82801d8f 329 int ret = 0;
c163b5ca 330
5e5328be 331 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 332 if (bmds->bulk_completed == 0) {
e970ec0b 333 if (mig_save_device_bulk(mon, f, bmds) == 1) {
57cce12d
JK
334 /* completed bulk section for this device */
335 bmds->bulk_completed = 1;
c163b5ca 336 }
82801d8f
JK
337 completed_sector_sum += bmds->completed_sectors;
338 ret = 1;
339 break;
340 } else {
341 completed_sector_sum += bmds->completed_sectors;
c163b5ca 342 }
343 }
a55eb92c 344
8b6b2afc
PR
345 if (block_mig_state.total_sector_sum != 0) {
346 progress = completed_sector_sum * 100 /
347 block_mig_state.total_sector_sum;
348 } else {
349 progress = 100;
350 }
01e61e2d
JK
351 if (progress != block_mig_state.prev_progress) {
352 block_mig_state.prev_progress = progress;
353 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
354 | BLK_MIG_FLAG_PROGRESS);
355 monitor_printf(mon, "Completed %d %%\r", progress);
7184049e 356 monitor_flush(mon);
82801d8f
JK
357 }
358
359 return ret;
c163b5ca 360}
361
d76cac7d 362static void blk_mig_reset_dirty_cursor(void)
c163b5ca 363{
364 BlkMigDevState *bmds;
d76cac7d
LS
365
366 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
367 bmds->cur_dirty = 0;
368 }
369}
370
371static int mig_save_device_dirty(Monitor *mon, QEMUFile *f,
372 BlkMigDevState *bmds, int is_async)
373{
374 BlkMigBlock *blk;
375 int64_t total_sectors = bmds->total_sectors;
c163b5ca 376 int64_t sector;
d76cac7d 377 int nr_sectors;
dcd1d224 378 int ret = -EIO;
a55eb92c 379
d76cac7d 380 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
62155e2b 381 if (bmds_aio_inflight(bmds, sector)) {
922453bc 382 bdrv_drain_all();
62155e2b 383 }
d76cac7d 384 if (bdrv_get_dirty(bmds->bs, sector)) {
575a58d7 385
d76cac7d
LS
386 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
387 nr_sectors = total_sectors - sector;
388 } else {
389 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
390 }
7267c094
AL
391 blk = g_malloc(sizeof(BlkMigBlock));
392 blk->buf = g_malloc(BLOCK_SIZE);
d76cac7d
LS
393 blk->bmds = bmds;
394 blk->sector = sector;
33656af7 395 blk->nr_sectors = nr_sectors;
d76cac7d 396
889ae39c 397 if (is_async) {
d76cac7d
LS
398 blk->iov.iov_base = blk->buf;
399 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
400 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
401
ff5c52a3
AT
402 if (block_mig_state.submitted == 0) {
403 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
404 }
889ae39c 405
d76cac7d
LS
406 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
407 nr_sectors, blk_mig_read_cb, blk);
d76cac7d 408 block_mig_state.submitted++;
33656af7 409 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
d76cac7d 410 } else {
dcd1d224
JQ
411 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
412 if (ret < 0) {
d76cac7d 413 goto error;
c163b5ca 414 }
d76cac7d 415 blk_send(f, blk);
a55eb92c 416
7267c094
AL
417 g_free(blk->buf);
418 g_free(blk);
a55eb92c 419 }
d76cac7d
LS
420
421 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
422 break;
c163b5ca 423 }
d76cac7d
LS
424 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
425 bmds->cur_dirty = sector;
c163b5ca 426 }
575a58d7 427
d76cac7d
LS
428 return (bmds->cur_dirty >= bmds->total_sectors);
429
889ae39c 430error:
d76cac7d 431 monitor_printf(mon, "Error reading sector %" PRId64 "\n", sector);
dcd1d224 432 qemu_file_set_error(f, ret);
7267c094
AL
433 g_free(blk->buf);
434 g_free(blk);
d76cac7d
LS
435 return 0;
436}
437
438static int blk_mig_save_dirty_block(Monitor *mon, QEMUFile *f, int is_async)
439{
440 BlkMigDevState *bmds;
441 int ret = 0;
442
443 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
889ae39c 444 if (mig_save_device_dirty(mon, f, bmds, is_async) == 0) {
d76cac7d
LS
445 ret = 1;
446 break;
447 }
448 }
449
450 return ret;
c163b5ca 451}
452
453static void flush_blks(QEMUFile* f)
454{
5e5328be 455 BlkMigBlock *blk;
a55eb92c 456
d0f2c4c6 457 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
d11ecd3d
JK
458 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
459 block_mig_state.transferred);
a55eb92c 460
5e5328be
JK
461 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
462 if (qemu_file_rate_limit(f)) {
463 break;
464 }
4b640365 465 if (blk->ret < 0) {
dcd1d224 466 qemu_file_set_error(f, blk->ret);
4b640365
JK
467 break;
468 }
13f0b67f 469 blk_send(f, blk);
a55eb92c 470
5e5328be 471 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
7267c094
AL
472 g_free(blk->buf);
473 g_free(blk);
a55eb92c 474
d11ecd3d
JK
475 block_mig_state.read_done--;
476 block_mig_state.transferred++;
477 assert(block_mig_state.read_done >= 0);
c163b5ca 478 }
c163b5ca 479
d0f2c4c6 480 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
d11ecd3d
JK
481 block_mig_state.submitted, block_mig_state.read_done,
482 block_mig_state.transferred);
c163b5ca 483}
484
889ae39c
LS
485static int64_t get_remaining_dirty(void)
486{
487 BlkMigDevState *bmds;
488 int64_t dirty = 0;
489
490 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
491 dirty += bdrv_get_dirty_count(bmds->bs);
492 }
493
494 return dirty * BLOCK_SIZE;
495}
496
c163b5ca 497static int is_stage2_completed(void)
498{
889ae39c
LS
499 int64_t remaining_dirty;
500 long double bwidth;
501
502 if (block_mig_state.bulk_completed == 1) {
503
504 remaining_dirty = get_remaining_dirty();
bd0858bb
YT
505 if (remaining_dirty == 0) {
506 return 1;
507 }
889ae39c 508
bd0858bb 509 bwidth = compute_read_bwidth();
889ae39c 510
bd0858bb 511 if ((remaining_dirty / bwidth) <=
889ae39c 512 migrate_max_downtime()) {
4238e264 513 /* finish stage2 because we think that we can finish remaining work
889ae39c
LS
514 below max_downtime */
515
516 return 1;
517 }
518 }
519
520 return 0;
c163b5ca 521}
522
7184049e 523static void blk_mig_cleanup(Monitor *mon)
4ec7fcc7 524{
82801d8f
JK
525 BlkMigDevState *bmds;
526 BlkMigBlock *blk;
4ec7fcc7 527
8f794c55
MT
528 set_dirty_tracking(0);
529
82801d8f
JK
530 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
531 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
8591675f 532 bdrv_set_in_use(bmds->bs, 0);
f48905d4 533 drive_put_ref(drive_get_by_blockdev(bmds->bs));
7267c094
AL
534 g_free(bmds->aio_bitmap);
535 g_free(bmds);
4ec7fcc7
JK
536 }
537
82801d8f
JK
538 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
539 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
7267c094
AL
540 g_free(blk->buf);
541 g_free(blk);
4ec7fcc7
JK
542 }
543
7184049e 544 monitor_printf(mon, "\n");
4ec7fcc7
JK
545}
546
f327aa0c 547static int block_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
c163b5ca 548{
2975725f
JQ
549 int ret;
550
d0f2c4c6 551 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
d11ecd3d 552 stage, block_mig_state.submitted, block_mig_state.transferred);
a55eb92c 553
4ec7fcc7 554 if (stage < 0) {
7184049e 555 blk_mig_cleanup(mon);
4ec7fcc7
JK
556 return 0;
557 }
558
d11ecd3d 559 if (block_mig_state.blk_enable != 1) {
c163b5ca 560 /* no need to migrate storage */
a55eb92c 561 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
c163b5ca 562 return 1;
563 }
a55eb92c
JK
564
565 if (stage == 1) {
7184049e 566 init_blk_migration(mon, f);
a55eb92c 567
c163b5ca 568 /* start track dirty blocks */
569 set_dirty_tracking(1);
c163b5ca 570 }
571
572 flush_blks(f);
a55eb92c 573
2975725f
JQ
574 ret = qemu_file_get_error(f);
575 if (ret) {
7184049e 576 blk_mig_cleanup(mon);
2975725f 577 return ret;
4b640365
JK
578 }
579
d76cac7d
LS
580 blk_mig_reset_dirty_cursor();
581
889ae39c 582 if (stage == 2) {
d76cac7d
LS
583 /* control the rate of transfer */
584 while ((block_mig_state.submitted +
585 block_mig_state.read_done) * BLOCK_SIZE <
586 qemu_file_get_rate_limit(f)) {
587 if (block_mig_state.bulk_completed == 0) {
588 /* first finish the bulk phase */
589 if (blk_mig_save_bulked_block(mon, f) == 0) {
889ae39c 590 /* finished saving bulk on all devices */
d76cac7d
LS
591 block_mig_state.bulk_completed = 1;
592 }
593 } else {
594 if (blk_mig_save_dirty_block(mon, f, 1) == 0) {
595 /* no more dirty blocks */
596 break;
597 }
598 }
a55eb92c 599 }
a55eb92c 600
d76cac7d 601 flush_blks(f);
a55eb92c 602
2975725f
JQ
603 ret = qemu_file_get_error(f);
604 if (ret) {
d76cac7d 605 blk_mig_cleanup(mon);
2975725f 606 return ret;
d76cac7d 607 }
4b640365
JK
608 }
609
a55eb92c 610 if (stage == 3) {
889ae39c
LS
611 /* we know for sure that save bulk is completed and
612 all async read completed */
613 assert(block_mig_state.submitted == 0);
a55eb92c 614
889ae39c 615 while (blk_mig_save_dirty_block(mon, f, 0) != 0);
7184049e 616 blk_mig_cleanup(mon);
a55eb92c 617
01e61e2d
JK
618 /* report completion */
619 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
620
2975725f
JQ
621 ret = qemu_file_get_error(f);
622 if (ret) {
623 return ret;
4b640365
JK
624 }
625
7184049e 626 monitor_printf(mon, "Block migration completed\n");
c163b5ca 627 }
a55eb92c
JK
628
629 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
630
c163b5ca 631 return ((stage == 2) && is_stage2_completed());
632}
633
634static int block_load(QEMUFile *f, void *opaque, int version_id)
635{
01e61e2d 636 static int banner_printed;
c163b5ca 637 int len, flags;
638 char device_name[256];
639 int64_t addr;
77358b59 640 BlockDriverState *bs, *bs_prev = NULL;
c163b5ca 641 uint8_t *buf;
77358b59
PR
642 int64_t total_sectors = 0;
643 int nr_sectors;
42802d47 644 int ret;
a55eb92c 645
c163b5ca 646 do {
c163b5ca 647 addr = qemu_get_be64(f);
a55eb92c 648
6ea44308
JK
649 flags = addr & ~BDRV_SECTOR_MASK;
650 addr >>= BDRV_SECTOR_BITS;
a55eb92c
JK
651
652 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
c163b5ca 653 /* get device name */
654 len = qemu_get_byte(f);
c163b5ca 655 qemu_get_buffer(f, (uint8_t *)device_name, len);
656 device_name[len] = '\0';
a55eb92c 657
c163b5ca 658 bs = bdrv_find(device_name);
4b640365
JK
659 if (!bs) {
660 fprintf(stderr, "Error unknown block device %s\n",
661 device_name);
662 return -EINVAL;
663 }
a55eb92c 664
77358b59
PR
665 if (bs != bs_prev) {
666 bs_prev = bs;
667 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
668 if (total_sectors <= 0) {
6daf194d 669 error_report("Error getting length of block device %s",
77358b59
PR
670 device_name);
671 return -EINVAL;
672 }
673 }
674
675 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
676 nr_sectors = total_sectors - addr;
677 } else {
678 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
679 }
680
7267c094 681 buf = g_malloc(BLOCK_SIZE);
575a58d7 682
a55eb92c 683 qemu_get_buffer(f, buf, BLOCK_SIZE);
77358b59 684 ret = bdrv_write(bs, addr, buf, nr_sectors);
575a58d7 685
7267c094 686 g_free(buf);
b02bea3a
YT
687 if (ret < 0) {
688 return ret;
689 }
01e61e2d
JK
690 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
691 if (!banner_printed) {
692 printf("Receiving block device images\n");
693 banner_printed = 1;
694 }
695 printf("Completed %d %%%c", (int)addr,
696 (addr == 100) ? '\n' : '\r');
697 fflush(stdout);
a55eb92c 698 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
4b640365
JK
699 fprintf(stderr, "Unknown flags\n");
700 return -EINVAL;
701 }
42802d47
JQ
702 ret = qemu_file_get_error(f);
703 if (ret != 0) {
704 return ret;
c163b5ca 705 }
a55eb92c
JK
706 } while (!(flags & BLK_MIG_FLAG_EOS));
707
c163b5ca 708 return 0;
709}
710
711static void block_set_params(int blk_enable, int shared_base, void *opaque)
712{
d11ecd3d
JK
713 block_mig_state.blk_enable = blk_enable;
714 block_mig_state.shared_base = shared_base;
a55eb92c 715
c163b5ca 716 /* shared base means that blk_enable = 1 */
d11ecd3d 717 block_mig_state.blk_enable |= shared_base;
c163b5ca 718}
719
c163b5ca 720void blk_mig_init(void)
a55eb92c 721{
5e5328be
JK
722 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
723 QSIMPLEQ_INIT(&block_mig_state.blk_list);
724
0be71e32
AW
725 register_savevm_live(NULL, "block", 0, 1, block_set_params,
726 block_save_live, NULL, block_load, &block_mig_state);
c163b5ca 727}