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