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