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