]> git.proxmox.com Git - mirror_qemu.git/blame - block-migration.c
usb: Call wakeup when data becomes available for all devices with int eps
[mirror_qemu.git] / block-migration.c
CommitLineData
c163b5ca
LS
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
LS
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"
c163b5ca 21#include "block-migration.h"
889ae39c 22#include "migration.h"
f48905d4 23#include "blockdev.h"
c163b5ca 24#include <assert.h>
c163b5ca 25
6ea44308 26#define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
c163b5ca
LS
27
28#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
29#define BLK_MIG_FLAG_EOS 0x02
01e61e2d 30#define BLK_MIG_FLAG_PROGRESS 0x04
c163b5ca
LS
31
32#define MAX_IS_ALLOCATED_SEARCH 65536
c163b5ca
LS
33
34//#define DEBUG_BLK_MIGRATION
35
36#ifdef DEBUG_BLK_MIGRATION
d0f2c4c6 37#define DPRINTF(fmt, ...) \
c163b5ca
LS
38 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
39#else
d0f2c4c6 40#define DPRINTF(fmt, ...) \
c163b5ca
LS
41 do { } while (0)
42#endif
43
a55eb92c
JK
44typedef struct BlkMigDevState {
45 BlockDriverState *bs;
46 int bulk_completed;
47 int shared_base;
a55eb92c 48 int64_t cur_sector;
d76cac7d 49 int64_t cur_dirty;
82801d8f 50 int64_t completed_sectors;
a55eb92c
JK
51 int64_t total_sectors;
52 int64_t dirty;
5e5328be 53 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
33656af7 54 unsigned long *aio_bitmap;
a55eb92c
JK
55} BlkMigDevState;
56
c163b5ca
LS
57typedef struct BlkMigBlock {
58 uint8_t *buf;
59 BlkMigDevState *bmds;
60 int64_t sector;
33656af7 61 int nr_sectors;
c163b5ca
LS
62 struct iovec iov;
63 QEMUIOVector qiov;
64 BlockDriverAIOCB *aiocb;
65 int ret;
5e5328be 66 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
c163b5ca
LS
67} BlkMigBlock;
68
69typedef struct BlkMigState {
c163b5ca
LS
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
LS
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 80 long double total_time;
ff5c52a3 81 long double prev_time_offset;
889ae39c 82 int reads;
c163b5ca
LS
83} BlkMigState;
84
d11ecd3d 85static BlkMigState block_mig_state;
c163b5ca 86
13f0b67f
JK
87static void blk_send(QEMUFile *f, BlkMigBlock * blk)
88{
89 int len;
90
91 /* sector number and flags */
92 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
93 | BLK_MIG_FLAG_DEVICE_BLOCK);
94
95 /* device name */
96 len = strlen(blk->bmds->bs->device_name);
97 qemu_put_byte(f, len);
98 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
99
100 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
101}
102
25f23643
JK
103int blk_mig_active(void)
104{
105 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
106}
107
108uint64_t blk_mig_bytes_transferred(void)
109{
110 BlkMigDevState *bmds;
111 uint64_t sum = 0;
112
113 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
114 sum += bmds->completed_sectors;
115 }
116 return sum << BDRV_SECTOR_BITS;
117}
118
119uint64_t blk_mig_bytes_remaining(void)
120{
121 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
122}
123
124uint64_t blk_mig_bytes_total(void)
125{
126 BlkMigDevState *bmds;
127 uint64_t sum = 0;
128
129 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
130 sum += bmds->total_sectors;
131 }
132 return sum << BDRV_SECTOR_BITS;
133}
134
889ae39c
LS
135static inline long double compute_read_bwidth(void)
136{
137 assert(block_mig_state.total_time != 0);
155eb9aa 138 return (block_mig_state.reads / block_mig_state.total_time) * BLOCK_SIZE;
889ae39c
LS
139}
140
33656af7
MT
141static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
142{
143 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
144
62155e2b 145 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
33656af7
MT
146 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
147 (1UL << (chunk % (sizeof(unsigned long) * 8))));
148 } else {
149 return 0;
150 }
151}
152
153static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
154 int nb_sectors, int set)
155{
156 int64_t start, end;
157 unsigned long val, idx, bit;
158
159 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
160 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
161
162 for (; start <= end; start++) {
163 idx = start / (sizeof(unsigned long) * 8);
164 bit = start % (sizeof(unsigned long) * 8);
165 val = bmds->aio_bitmap[idx];
166 if (set) {
62155e2b 167 val |= 1UL << bit;
33656af7 168 } else {
62155e2b 169 val &= ~(1UL << bit);
33656af7
MT
170 }
171 bmds->aio_bitmap[idx] = val;
172 }
173}
174
175static void alloc_aio_bitmap(BlkMigDevState *bmds)
176{
177 BlockDriverState *bs = bmds->bs;
178 int64_t bitmap_size;
179
180 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
181 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
182 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
183
7267c094 184 bmds->aio_bitmap = g_malloc0(bitmap_size);
33656af7
MT
185}
186
c163b5ca
LS
187static void blk_mig_read_cb(void *opaque, int ret)
188{
ff5c52a3 189 long double curr_time = qemu_get_clock_ns(rt_clock);
c163b5ca 190 BlkMigBlock *blk = opaque;
a55eb92c 191
c163b5ca 192 blk->ret = ret;
a55eb92c 193
ff5c52a3
AT
194 block_mig_state.reads++;
195 block_mig_state.total_time += (curr_time - block_mig_state.prev_time_offset);
196 block_mig_state.prev_time_offset = curr_time;
889ae39c 197
5e5328be 198 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
33656af7 199 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
a55eb92c 200
d11ecd3d
JK
201 block_mig_state.submitted--;
202 block_mig_state.read_done++;
203 assert(block_mig_state.submitted >= 0);
c163b5ca
LS
204}
205
539de124 206static int mig_save_device_bulk(QEMUFile *f, 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
LS
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
LS
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
LS
260}
261
c163b5ca
LS
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
LS
269}
270
b66460e4 271static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
c163b5ca 272{
5e5328be 273 BlkMigDevState *bmds;
792773b2 274 int64_t sectors;
a55eb92c 275
d246673d 276 if (!bdrv_is_read_only(bs)) {
b66460e4 277 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
31f54f24 278 if (sectors <= 0) {
b66460e4
SH
279 return;
280 }
281
7267c094 282 bmds = g_malloc0(sizeof(BlkMigDevState));
b66460e4
SH
283 bmds->bs = bs;
284 bmds->bulk_completed = 0;
285 bmds->total_sectors = sectors;
286 bmds->completed_sectors = 0;
287 bmds->shared_base = block_mig_state.shared_base;
33656af7 288 alloc_aio_bitmap(bmds);
f48905d4 289 drive_get_ref(drive_get_by_blockdev(bs));
8591675f 290 bdrv_set_in_use(bs, 1);
b66460e4
SH
291
292 block_mig_state.total_sector_sum += sectors;
293
294 if (bmds->shared_base) {
539de124
LC
295 DPRINTF("Start migration for %s with shared base image\n",
296 bs->device_name);
b66460e4 297 } else {
539de124 298 DPRINTF("Start full migration for %s\n", bs->device_name);
b66460e4
SH
299 }
300
301 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
302 }
303}
304
539de124 305static void init_blk_migration(QEMUFile *f)
b66460e4 306{
69d63a97
JK
307 block_mig_state.submitted = 0;
308 block_mig_state.read_done = 0;
309 block_mig_state.transferred = 0;
82801d8f 310 block_mig_state.total_sector_sum = 0;
01e61e2d 311 block_mig_state.prev_progress = -1;
e970ec0b 312 block_mig_state.bulk_completed = 0;
889ae39c
LS
313 block_mig_state.total_time = 0;
314 block_mig_state.reads = 0;
69d63a97 315
539de124 316 bdrv_iterate(init_blk_migration_it, NULL);
c163b5ca
LS
317}
318
539de124 319static int blk_mig_save_bulked_block(QEMUFile *f)
c163b5ca 320{
82801d8f 321 int64_t completed_sector_sum = 0;
c163b5ca 322 BlkMigDevState *bmds;
01e61e2d 323 int progress;
82801d8f 324 int ret = 0;
c163b5ca 325
5e5328be 326 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 327 if (bmds->bulk_completed == 0) {
539de124 328 if (mig_save_device_bulk(f, bmds) == 1) {
57cce12d
JK
329 /* completed bulk section for this device */
330 bmds->bulk_completed = 1;
c163b5ca 331 }
82801d8f
JK
332 completed_sector_sum += bmds->completed_sectors;
333 ret = 1;
334 break;
335 } else {
336 completed_sector_sum += bmds->completed_sectors;
c163b5ca
LS
337 }
338 }
a55eb92c 339
8b6b2afc
PR
340 if (block_mig_state.total_sector_sum != 0) {
341 progress = completed_sector_sum * 100 /
342 block_mig_state.total_sector_sum;
343 } else {
344 progress = 100;
345 }
01e61e2d
JK
346 if (progress != block_mig_state.prev_progress) {
347 block_mig_state.prev_progress = progress;
348 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
349 | BLK_MIG_FLAG_PROGRESS);
539de124 350 DPRINTF("Completed %d %%\r", progress);
82801d8f
JK
351 }
352
353 return ret;
c163b5ca
LS
354}
355
d76cac7d 356static void blk_mig_reset_dirty_cursor(void)
c163b5ca
LS
357{
358 BlkMigDevState *bmds;
d76cac7d
LS
359
360 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
361 bmds->cur_dirty = 0;
362 }
363}
364
539de124
LC
365static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
366 int is_async)
d76cac7d
LS
367{
368 BlkMigBlock *blk;
369 int64_t total_sectors = bmds->total_sectors;
c163b5ca 370 int64_t sector;
d76cac7d 371 int nr_sectors;
dcd1d224 372 int ret = -EIO;
a55eb92c 373
d76cac7d 374 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
62155e2b 375 if (bmds_aio_inflight(bmds, sector)) {
922453bc 376 bdrv_drain_all();
62155e2b 377 }
d76cac7d 378 if (bdrv_get_dirty(bmds->bs, sector)) {
575a58d7 379
d76cac7d
LS
380 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
381 nr_sectors = total_sectors - sector;
382 } else {
383 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
384 }
7267c094
AL
385 blk = g_malloc(sizeof(BlkMigBlock));
386 blk->buf = g_malloc(BLOCK_SIZE);
d76cac7d
LS
387 blk->bmds = bmds;
388 blk->sector = sector;
33656af7 389 blk->nr_sectors = nr_sectors;
d76cac7d 390
889ae39c 391 if (is_async) {
d76cac7d
LS
392 blk->iov.iov_base = blk->buf;
393 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
394 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
395
ff5c52a3
AT
396 if (block_mig_state.submitted == 0) {
397 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
398 }
889ae39c 399
d76cac7d
LS
400 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
401 nr_sectors, blk_mig_read_cb, blk);
d76cac7d 402 block_mig_state.submitted++;
33656af7 403 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
d76cac7d 404 } else {
dcd1d224
JQ
405 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
406 if (ret < 0) {
d76cac7d 407 goto error;
c163b5ca 408 }
d76cac7d 409 blk_send(f, blk);
a55eb92c 410
7267c094
AL
411 g_free(blk->buf);
412 g_free(blk);
a55eb92c 413 }
d76cac7d
LS
414
415 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
416 break;
c163b5ca 417 }
d76cac7d
LS
418 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
419 bmds->cur_dirty = sector;
c163b5ca 420 }
575a58d7 421
d76cac7d
LS
422 return (bmds->cur_dirty >= bmds->total_sectors);
423
889ae39c 424error:
539de124 425 DPRINTF("Error reading sector %" PRId64 "\n", sector);
7267c094
AL
426 g_free(blk->buf);
427 g_free(blk);
43be3a25 428 return ret;
d76cac7d
LS
429}
430
ceb2bd09
JQ
431/* return value:
432 * 0: too much data for max_downtime
433 * 1: few enough data for max_downtime
434*/
539de124 435static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
d76cac7d
LS
436{
437 BlkMigDevState *bmds;
ceb2bd09 438 int ret = 1;
d76cac7d
LS
439
440 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
ceb2bd09 441 ret = mig_save_device_dirty(f, bmds, is_async);
43be3a25 442 if (ret <= 0) {
d76cac7d
LS
443 break;
444 }
445 }
446
447 return ret;
c163b5ca
LS
448}
449
59feec42 450static int flush_blks(QEMUFile *f)
c163b5ca 451{
5e5328be 452 BlkMigBlock *blk;
59feec42 453 int ret = 0;
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) {
59feec42 464 ret = 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);
59feec42 481 return ret;
c163b5ca
LS
482}
483
889ae39c
LS
484static int64_t get_remaining_dirty(void)
485{
486 BlkMigDevState *bmds;
487 int64_t dirty = 0;
488
489 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
490 dirty += bdrv_get_dirty_count(bmds->bs);
491 }
492
493 return dirty * BLOCK_SIZE;
494}
495
c163b5ca
LS
496static int is_stage2_completed(void)
497{
889ae39c
LS
498 int64_t remaining_dirty;
499 long double bwidth;
500
501 if (block_mig_state.bulk_completed == 1) {
502
503 remaining_dirty = get_remaining_dirty();
bd0858bb
YT
504 if (remaining_dirty == 0) {
505 return 1;
506 }
889ae39c 507
bd0858bb 508 bwidth = compute_read_bwidth();
889ae39c 509
bd0858bb 510 if ((remaining_dirty / bwidth) <=
889ae39c 511 migrate_max_downtime()) {
4238e264 512 /* finish stage2 because we think that we can finish remaining work
889ae39c
LS
513 below max_downtime */
514
515 return 1;
516 }
517 }
518
519 return 0;
c163b5ca
LS
520}
521
539de124 522static void blk_mig_cleanup(void)
4ec7fcc7 523{
82801d8f
JK
524 BlkMigDevState *bmds;
525 BlkMigBlock *blk;
4ec7fcc7 526
946d58be
KW
527 bdrv_drain_all();
528
8f794c55
MT
529 set_dirty_tracking(0);
530
82801d8f
JK
531 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
532 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
8591675f 533 bdrv_set_in_use(bmds->bs, 0);
f48905d4 534 drive_put_ref(drive_get_by_blockdev(bmds->bs));
7267c094
AL
535 g_free(bmds->aio_bitmap);
536 g_free(bmds);
4ec7fcc7
JK
537 }
538
82801d8f
JK
539 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
540 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
7267c094
AL
541 g_free(blk->buf);
542 g_free(blk);
4ec7fcc7 543 }
4ec7fcc7
JK
544}
545
9b5bfab0
JQ
546static void block_migration_cancel(void *opaque)
547{
548 blk_mig_cleanup();
549}
550
d1315aac 551static int block_save_setup(QEMUFile *f, void *opaque)
c163b5ca 552{
2975725f
JQ
553 int ret;
554
d1315aac
JQ
555 DPRINTF("Enter save live setup submitted %d transferred %d\n",
556 block_mig_state.submitted, block_mig_state.transferred);
a55eb92c 557
d1315aac
JQ
558 init_blk_migration(f);
559
560 /* start track dirty blocks */
561 set_dirty_tracking(1);
562
59feec42 563 ret = flush_blks(f);
d1315aac
JQ
564 if (ret) {
565 blk_mig_cleanup();
566 return ret;
c163b5ca
LS
567 }
568
d1315aac
JQ
569 blk_mig_reset_dirty_cursor();
570
571 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
572
573 return 0;
574}
575
16310a3c 576static int block_save_iterate(QEMUFile *f, void *opaque)
d1315aac
JQ
577{
578 int ret;
579
16310a3c
JQ
580 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
581 block_mig_state.submitted, block_mig_state.transferred);
d1315aac 582
59feec42 583 ret = flush_blks(f);
2975725f 584 if (ret) {
539de124 585 blk_mig_cleanup();
2975725f 586 return ret;
4b640365
JK
587 }
588
d76cac7d
LS
589 blk_mig_reset_dirty_cursor();
590
16310a3c
JQ
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(f) == 0) {
598 /* finished saving bulk on all devices */
599 block_mig_state.bulk_completed = 1;
600 }
601 } else {
43be3a25
JQ
602 ret = blk_mig_save_dirty_block(f, 1);
603 if (ret != 0) {
16310a3c
JQ
604 /* no more dirty blocks */
605 break;
d76cac7d 606 }
a55eb92c 607 }
16310a3c 608 }
43be3a25
JQ
609 if (ret) {
610 blk_mig_cleanup();
611 return ret;
612 }
a55eb92c 613
59feec42 614 ret = flush_blks(f);
16310a3c
JQ
615 if (ret) {
616 blk_mig_cleanup();
617 return ret;
4b640365
JK
618 }
619
16310a3c
JQ
620 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
621
622 return is_stage2_completed();
623}
624
625static int block_save_complete(QEMUFile *f, void *opaque)
626{
627 int ret;
628
629 DPRINTF("Enter save live complete submitted %d transferred %d\n",
630 block_mig_state.submitted, block_mig_state.transferred);
631
59feec42 632 ret = flush_blks(f);
16310a3c 633 if (ret) {
539de124 634 blk_mig_cleanup();
16310a3c
JQ
635 return ret;
636 }
a55eb92c 637
16310a3c 638 blk_mig_reset_dirty_cursor();
01e61e2d 639
16310a3c
JQ
640 /* we know for sure that save bulk is completed and
641 all async read completed */
642 assert(block_mig_state.submitted == 0);
643
43be3a25
JQ
644 do {
645 ret = blk_mig_save_dirty_block(f, 0);
646 } while (ret == 0);
4b640365 647
43be3a25 648 blk_mig_cleanup();
16310a3c
JQ
649 if (ret) {
650 return ret;
c163b5ca 651 }
43be3a25
JQ
652 /* report completion */
653 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
a55eb92c 654
16310a3c
JQ
655 DPRINTF("Block migration completed\n");
656
a55eb92c
JK
657 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
658
16310a3c 659 return 0;
c163b5ca
LS
660}
661
662static int block_load(QEMUFile *f, void *opaque, int version_id)
663{
01e61e2d 664 static int banner_printed;
c163b5ca
LS
665 int len, flags;
666 char device_name[256];
667 int64_t addr;
77358b59 668 BlockDriverState *bs, *bs_prev = NULL;
c163b5ca 669 uint8_t *buf;
77358b59
PR
670 int64_t total_sectors = 0;
671 int nr_sectors;
42802d47 672 int ret;
a55eb92c 673
c163b5ca 674 do {
c163b5ca 675 addr = qemu_get_be64(f);
a55eb92c 676
6ea44308
JK
677 flags = addr & ~BDRV_SECTOR_MASK;
678 addr >>= BDRV_SECTOR_BITS;
a55eb92c
JK
679
680 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
c163b5ca
LS
681 /* get device name */
682 len = qemu_get_byte(f);
c163b5ca
LS
683 qemu_get_buffer(f, (uint8_t *)device_name, len);
684 device_name[len] = '\0';
a55eb92c 685
c163b5ca 686 bs = bdrv_find(device_name);
4b640365
JK
687 if (!bs) {
688 fprintf(stderr, "Error unknown block device %s\n",
689 device_name);
690 return -EINVAL;
691 }
a55eb92c 692
77358b59
PR
693 if (bs != bs_prev) {
694 bs_prev = bs;
695 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
696 if (total_sectors <= 0) {
6daf194d 697 error_report("Error getting length of block device %s",
77358b59
PR
698 device_name);
699 return -EINVAL;
700 }
701 }
702
703 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
704 nr_sectors = total_sectors - addr;
705 } else {
706 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
707 }
708
7267c094 709 buf = g_malloc(BLOCK_SIZE);
575a58d7 710
a55eb92c 711 qemu_get_buffer(f, buf, BLOCK_SIZE);
77358b59 712 ret = bdrv_write(bs, addr, buf, nr_sectors);
575a58d7 713
7267c094 714 g_free(buf);
b02bea3a
YT
715 if (ret < 0) {
716 return ret;
717 }
01e61e2d
JK
718 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
719 if (!banner_printed) {
720 printf("Receiving block device images\n");
721 banner_printed = 1;
722 }
723 printf("Completed %d %%%c", (int)addr,
724 (addr == 100) ? '\n' : '\r');
725 fflush(stdout);
a55eb92c 726 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
4b640365
JK
727 fprintf(stderr, "Unknown flags\n");
728 return -EINVAL;
729 }
42802d47
JQ
730 ret = qemu_file_get_error(f);
731 if (ret != 0) {
732 return ret;
c163b5ca 733 }
a55eb92c
JK
734 } while (!(flags & BLK_MIG_FLAG_EOS));
735
c163b5ca
LS
736 return 0;
737}
738
6607ae23 739static void block_set_params(const MigrationParams *params, void *opaque)
c163b5ca 740{
6607ae23
IY
741 block_mig_state.blk_enable = params->blk;
742 block_mig_state.shared_base = params->shared;
a55eb92c 743
c163b5ca 744 /* shared base means that blk_enable = 1 */
6607ae23 745 block_mig_state.blk_enable |= params->shared;
c163b5ca
LS
746}
747
6bd68781
JQ
748static bool block_is_active(void *opaque)
749{
750 return block_mig_state.blk_enable == 1;
751}
752
7908c78d
JQ
753SaveVMHandlers savevm_block_handlers = {
754 .set_params = block_set_params,
d1315aac 755 .save_live_setup = block_save_setup,
16310a3c
JQ
756 .save_live_iterate = block_save_iterate,
757 .save_live_complete = block_save_complete,
7908c78d 758 .load_state = block_load,
9b5bfab0 759 .cancel = block_migration_cancel,
6bd68781 760 .is_active = block_is_active,
7908c78d
JQ
761};
762
c163b5ca 763void blk_mig_init(void)
a55eb92c 764{
5e5328be
JK
765 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
766 QSIMPLEQ_INIT(&block_mig_state.blk_list);
767
7908c78d
JQ
768 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
769 &block_mig_state);
c163b5ca 770}