]> git.proxmox.com Git - mirror_qemu.git/blame - block-migration.c
block: Use g_new() & friends where that makes obvious sense
[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"
737e150e 17#include "block/block_int.h"
c163b5ca 18#include "hw/hw.h"
1de7afc9
PB
19#include "qemu/queue.h"
20#include "qemu/timer.h"
caf71f86
PB
21#include "migration/block.h"
22#include "migration/migration.h"
9c17d615 23#include "sysemu/blockdev.h"
c163b5ca 24#include <assert.h>
c163b5ca 25
50717e94
PB
26#define BLOCK_SIZE (1 << 20)
27#define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
c163b5ca
LS
28
29#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
30#define BLK_MIG_FLAG_EOS 0x02
01e61e2d 31#define BLK_MIG_FLAG_PROGRESS 0x04
323004a3 32#define BLK_MIG_FLAG_ZERO_BLOCK 0x08
c163b5ca
LS
33
34#define MAX_IS_ALLOCATED_SEARCH 65536
c163b5ca
LS
35
36//#define DEBUG_BLK_MIGRATION
37
38#ifdef DEBUG_BLK_MIGRATION
d0f2c4c6 39#define DPRINTF(fmt, ...) \
c163b5ca
LS
40 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
41#else
d0f2c4c6 42#define DPRINTF(fmt, ...) \
c163b5ca
LS
43 do { } while (0)
44#endif
45
a55eb92c 46typedef struct BlkMigDevState {
323920c4 47 /* Written during setup phase. Can be read without a lock. */
a55eb92c 48 BlockDriverState *bs;
a55eb92c 49 int shared_base;
a55eb92c 50 int64_t total_sectors;
5e5328be 51 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
323920c4
PB
52
53 /* Only used by migration thread. Does not need a lock. */
54 int bulk_completed;
55 int64_t cur_sector;
56 int64_t cur_dirty;
57
52e850de 58 /* Protected by block migration lock. */
33656af7 59 unsigned long *aio_bitmap;
323920c4 60 int64_t completed_sectors;
e4654d2d 61 BdrvDirtyBitmap *dirty_bitmap;
3718d8ab 62 Error *blocker;
a55eb92c
JK
63} BlkMigDevState;
64
c163b5ca 65typedef struct BlkMigBlock {
323920c4 66 /* Only used by migration thread. */
c163b5ca
LS
67 uint8_t *buf;
68 BlkMigDevState *bmds;
69 int64_t sector;
33656af7 70 int nr_sectors;
c163b5ca
LS
71 struct iovec iov;
72 QEMUIOVector qiov;
73 BlockDriverAIOCB *aiocb;
323920c4 74
52e850de 75 /* Protected by block migration lock. */
c163b5ca 76 int ret;
5e5328be 77 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
c163b5ca
LS
78} BlkMigBlock;
79
80typedef struct BlkMigState {
323920c4 81 /* Written during setup phase. Can be read without a lock. */
c163b5ca
LS
82 int blk_enable;
83 int shared_base;
5e5328be 84 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
323920c4 85 int64_t total_sector_sum;
323004a3 86 bool zero_blocks;
323920c4 87
52e850de 88 /* Protected by lock. */
5e5328be 89 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
c163b5ca
LS
90 int submitted;
91 int read_done;
323920c4
PB
92
93 /* Only used by migration thread. Does not need a lock. */
c163b5ca 94 int transferred;
01e61e2d 95 int prev_progress;
e970ec0b 96 int bulk_completed;
52e850de
PB
97
98 /* Lock must be taken _inside_ the iothread lock. */
99 QemuMutex lock;
c163b5ca
LS
100} BlkMigState;
101
d11ecd3d 102static BlkMigState block_mig_state;
c163b5ca 103
52e850de
PB
104static void blk_mig_lock(void)
105{
106 qemu_mutex_lock(&block_mig_state.lock);
107}
108
109static void blk_mig_unlock(void)
110{
111 qemu_mutex_unlock(&block_mig_state.lock);
112}
113
32c835ba
PB
114/* Must run outside of the iothread lock during the bulk phase,
115 * or the VM will stall.
116 */
117
13f0b67f
JK
118static void blk_send(QEMUFile *f, BlkMigBlock * blk)
119{
120 int len;
323004a3
PL
121 uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK;
122
123 if (block_mig_state.zero_blocks &&
124 buffer_is_zero(blk->buf, BLOCK_SIZE)) {
125 flags |= BLK_MIG_FLAG_ZERO_BLOCK;
126 }
13f0b67f
JK
127
128 /* sector number and flags */
129 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
323004a3 130 | flags);
13f0b67f
JK
131
132 /* device name */
133 len = strlen(blk->bmds->bs->device_name);
134 qemu_put_byte(f, len);
135 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
136
323004a3
PL
137 /* if a block is zero we need to flush here since the network
138 * bandwidth is now a lot higher than the storage device bandwidth.
139 * thus if we queue zero blocks we slow down the migration */
140 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
141 qemu_fflush(f);
142 return;
143 }
144
13f0b67f
JK
145 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
146}
147
25f23643
JK
148int blk_mig_active(void)
149{
150 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
151}
152
153uint64_t blk_mig_bytes_transferred(void)
154{
155 BlkMigDevState *bmds;
156 uint64_t sum = 0;
157
52e850de 158 blk_mig_lock();
25f23643
JK
159 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
160 sum += bmds->completed_sectors;
161 }
52e850de 162 blk_mig_unlock();
25f23643
JK
163 return sum << BDRV_SECTOR_BITS;
164}
165
166uint64_t blk_mig_bytes_remaining(void)
167{
168 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
169}
170
171uint64_t blk_mig_bytes_total(void)
172{
173 BlkMigDevState *bmds;
174 uint64_t sum = 0;
175
176 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
177 sum += bmds->total_sectors;
178 }
179 return sum << BDRV_SECTOR_BITS;
180}
181
52e850de
PB
182
183/* Called with migration lock held. */
184
33656af7
MT
185static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
186{
187 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
188
57322b78 189 if (sector < bdrv_nb_sectors(bmds->bs)) {
33656af7
MT
190 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
191 (1UL << (chunk % (sizeof(unsigned long) * 8))));
192 } else {
193 return 0;
194 }
195}
196
52e850de
PB
197/* Called with migration lock held. */
198
33656af7
MT
199static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
200 int nb_sectors, int set)
201{
202 int64_t start, end;
203 unsigned long val, idx, bit;
204
205 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
206 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
207
208 for (; start <= end; start++) {
209 idx = start / (sizeof(unsigned long) * 8);
210 bit = start % (sizeof(unsigned long) * 8);
211 val = bmds->aio_bitmap[idx];
212 if (set) {
62155e2b 213 val |= 1UL << bit;
33656af7 214 } else {
62155e2b 215 val &= ~(1UL << bit);
33656af7
MT
216 }
217 bmds->aio_bitmap[idx] = val;
218 }
219}
220
221static void alloc_aio_bitmap(BlkMigDevState *bmds)
222{
223 BlockDriverState *bs = bmds->bs;
224 int64_t bitmap_size;
225
57322b78 226 bitmap_size = bdrv_nb_sectors(bs) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
33656af7
MT
227 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
228
7267c094 229 bmds->aio_bitmap = g_malloc0(bitmap_size);
33656af7
MT
230}
231
52e850de
PB
232/* Never hold migration lock when yielding to the main loop! */
233
c163b5ca
LS
234static void blk_mig_read_cb(void *opaque, int ret)
235{
236 BlkMigBlock *blk = opaque;
a55eb92c 237
52e850de 238 blk_mig_lock();
c163b5ca 239 blk->ret = ret;
a55eb92c 240
5e5328be 241 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
33656af7 242 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
a55eb92c 243
d11ecd3d
JK
244 block_mig_state.submitted--;
245 block_mig_state.read_done++;
246 assert(block_mig_state.submitted >= 0);
52e850de 247 blk_mig_unlock();
c163b5ca
LS
248}
249
32c835ba
PB
250/* Called with no lock taken. */
251
539de124 252static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
a55eb92c 253{
57cce12d
JK
254 int64_t total_sectors = bmds->total_sectors;
255 int64_t cur_sector = bmds->cur_sector;
256 BlockDriverState *bs = bmds->bs;
c163b5ca 257 BlkMigBlock *blk;
13f0b67f 258 int nr_sectors;
a55eb92c 259
57cce12d 260 if (bmds->shared_base) {
32c835ba 261 qemu_mutex_lock_iothread();
b1d10856 262 while (cur_sector < total_sectors &&
57cce12d
JK
263 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
264 &nr_sectors)) {
c163b5ca
LS
265 cur_sector += nr_sectors;
266 }
32c835ba 267 qemu_mutex_unlock_iothread();
c163b5ca 268 }
a55eb92c
JK
269
270 if (cur_sector >= total_sectors) {
82801d8f 271 bmds->cur_sector = bmds->completed_sectors = total_sectors;
c163b5ca
LS
272 return 1;
273 }
a55eb92c 274
82801d8f 275 bmds->completed_sectors = cur_sector;
a55eb92c 276
57cce12d
JK
277 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
278
6ea44308
JK
279 /* we are going to transfer a full block even if it is not allocated */
280 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
c163b5ca 281
6ea44308 282 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
57cce12d 283 nr_sectors = total_sectors - cur_sector;
c163b5ca 284 }
a55eb92c 285
5839e53b 286 blk = g_new(BlkMigBlock, 1);
7267c094 287 blk->buf = g_malloc(BLOCK_SIZE);
13f0b67f
JK
288 blk->bmds = bmds;
289 blk->sector = cur_sector;
33656af7 290 blk->nr_sectors = nr_sectors;
a55eb92c 291
e970ec0b
LS
292 blk->iov.iov_base = blk->buf;
293 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
294 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
a55eb92c 295
52e850de 296 blk_mig_lock();
13197e3c 297 block_mig_state.submitted++;
52e850de 298 blk_mig_unlock();
13197e3c 299
32c835ba 300 qemu_mutex_lock_iothread();
e970ec0b
LS
301 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
302 nr_sectors, blk_mig_read_cb, blk);
d76cac7d 303
13f0b67f 304 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
32c835ba 305 qemu_mutex_unlock_iothread();
a55eb92c 306
32c835ba 307 bmds->cur_sector = cur_sector + nr_sectors;
13f0b67f 308 return (bmds->cur_sector >= total_sectors);
c163b5ca
LS
309}
310
32c835ba
PB
311/* Called with iothread lock taken. */
312
b8afb520 313static int set_dirty_tracking(void)
c163b5ca
LS
314{
315 BlkMigDevState *bmds;
b8afb520
FZ
316 int ret;
317
318 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
319 bmds->dirty_bitmap = bdrv_create_dirty_bitmap(bmds->bs, BLOCK_SIZE,
320 NULL);
321 if (!bmds->dirty_bitmap) {
322 ret = -errno;
323 goto fail;
324 }
325 }
326 return 0;
5e5328be 327
b8afb520 328fail:
5e5328be 329 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
b8afb520
FZ
330 if (bmds->dirty_bitmap) {
331 bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
332 }
e4654d2d 333 }
b8afb520 334 return ret;
e4654d2d
FZ
335}
336
337static void unset_dirty_tracking(void)
338{
339 BlkMigDevState *bmds;
340
341 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
342 bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
c163b5ca 343 }
c163b5ca
LS
344}
345
b66460e4 346static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
c163b5ca 347{
5e5328be 348 BlkMigDevState *bmds;
792773b2 349 int64_t sectors;
a55eb92c 350
d246673d 351 if (!bdrv_is_read_only(bs)) {
57322b78 352 sectors = bdrv_nb_sectors(bs);
31f54f24 353 if (sectors <= 0) {
b66460e4
SH
354 return;
355 }
356
5839e53b 357 bmds = g_new0(BlkMigDevState, 1);
b66460e4
SH
358 bmds->bs = bs;
359 bmds->bulk_completed = 0;
360 bmds->total_sectors = sectors;
361 bmds->completed_sectors = 0;
362 bmds->shared_base = block_mig_state.shared_base;
33656af7 363 alloc_aio_bitmap(bmds);
3718d8ab
FZ
364 error_setg(&bmds->blocker, "block device is in use by migration");
365 bdrv_op_block_all(bs, bmds->blocker);
8442cfd0 366 bdrv_ref(bs);
b66460e4
SH
367
368 block_mig_state.total_sector_sum += sectors;
369
370 if (bmds->shared_base) {
539de124
LC
371 DPRINTF("Start migration for %s with shared base image\n",
372 bs->device_name);
b66460e4 373 } else {
539de124 374 DPRINTF("Start full migration for %s\n", bs->device_name);
b66460e4
SH
375 }
376
377 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
378 }
379}
380
539de124 381static void init_blk_migration(QEMUFile *f)
b66460e4 382{
69d63a97
JK
383 block_mig_state.submitted = 0;
384 block_mig_state.read_done = 0;
385 block_mig_state.transferred = 0;
82801d8f 386 block_mig_state.total_sector_sum = 0;
01e61e2d 387 block_mig_state.prev_progress = -1;
e970ec0b 388 block_mig_state.bulk_completed = 0;
323004a3 389 block_mig_state.zero_blocks = migrate_zero_blocks();
69d63a97 390
539de124 391 bdrv_iterate(init_blk_migration_it, NULL);
c163b5ca
LS
392}
393
32c835ba
PB
394/* Called with no lock taken. */
395
539de124 396static int blk_mig_save_bulked_block(QEMUFile *f)
c163b5ca 397{
82801d8f 398 int64_t completed_sector_sum = 0;
c163b5ca 399 BlkMigDevState *bmds;
01e61e2d 400 int progress;
82801d8f 401 int ret = 0;
c163b5ca 402
5e5328be 403 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 404 if (bmds->bulk_completed == 0) {
539de124 405 if (mig_save_device_bulk(f, bmds) == 1) {
57cce12d
JK
406 /* completed bulk section for this device */
407 bmds->bulk_completed = 1;
c163b5ca 408 }
82801d8f
JK
409 completed_sector_sum += bmds->completed_sectors;
410 ret = 1;
411 break;
412 } else {
413 completed_sector_sum += bmds->completed_sectors;
c163b5ca
LS
414 }
415 }
a55eb92c 416
8b6b2afc
PR
417 if (block_mig_state.total_sector_sum != 0) {
418 progress = completed_sector_sum * 100 /
419 block_mig_state.total_sector_sum;
420 } else {
421 progress = 100;
422 }
01e61e2d
JK
423 if (progress != block_mig_state.prev_progress) {
424 block_mig_state.prev_progress = progress;
425 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
426 | BLK_MIG_FLAG_PROGRESS);
539de124 427 DPRINTF("Completed %d %%\r", progress);
82801d8f
JK
428 }
429
430 return ret;
c163b5ca
LS
431}
432
d76cac7d 433static void blk_mig_reset_dirty_cursor(void)
c163b5ca
LS
434{
435 BlkMigDevState *bmds;
d76cac7d
LS
436
437 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
438 bmds->cur_dirty = 0;
439 }
440}
441
32c835ba
PB
442/* Called with iothread lock taken. */
443
539de124
LC
444static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
445 int is_async)
d76cac7d
LS
446{
447 BlkMigBlock *blk;
448 int64_t total_sectors = bmds->total_sectors;
c163b5ca 449 int64_t sector;
d76cac7d 450 int nr_sectors;
dcd1d224 451 int ret = -EIO;
a55eb92c 452
d76cac7d 453 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
52e850de 454 blk_mig_lock();
62155e2b 455 if (bmds_aio_inflight(bmds, sector)) {
52e850de 456 blk_mig_unlock();
922453bc 457 bdrv_drain_all();
52e850de
PB
458 } else {
459 blk_mig_unlock();
62155e2b 460 }
e4654d2d 461 if (bdrv_get_dirty(bmds->bs, bmds->dirty_bitmap, sector)) {
575a58d7 462
d76cac7d
LS
463 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
464 nr_sectors = total_sectors - sector;
465 } else {
466 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
467 }
5839e53b 468 blk = g_new(BlkMigBlock, 1);
7267c094 469 blk->buf = g_malloc(BLOCK_SIZE);
d76cac7d
LS
470 blk->bmds = bmds;
471 blk->sector = sector;
33656af7 472 blk->nr_sectors = nr_sectors;
d76cac7d 473
889ae39c 474 if (is_async) {
d76cac7d
LS
475 blk->iov.iov_base = blk->buf;
476 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
477 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
478
479 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
480 nr_sectors, blk_mig_read_cb, blk);
52e850de
PB
481
482 blk_mig_lock();
d76cac7d 483 block_mig_state.submitted++;
33656af7 484 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
52e850de 485 blk_mig_unlock();
d76cac7d 486 } else {
dcd1d224
JQ
487 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
488 if (ret < 0) {
d76cac7d 489 goto error;
c163b5ca 490 }
d76cac7d 491 blk_send(f, blk);
a55eb92c 492
7267c094
AL
493 g_free(blk->buf);
494 g_free(blk);
a55eb92c 495 }
d76cac7d
LS
496
497 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
498 break;
c163b5ca 499 }
d76cac7d
LS
500 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
501 bmds->cur_dirty = sector;
c163b5ca 502 }
575a58d7 503
d76cac7d
LS
504 return (bmds->cur_dirty >= bmds->total_sectors);
505
889ae39c 506error:
539de124 507 DPRINTF("Error reading sector %" PRId64 "\n", sector);
7267c094
AL
508 g_free(blk->buf);
509 g_free(blk);
43be3a25 510 return ret;
d76cac7d
LS
511}
512
32c835ba
PB
513/* Called with iothread lock taken.
514 *
515 * return value:
ceb2bd09
JQ
516 * 0: too much data for max_downtime
517 * 1: few enough data for max_downtime
518*/
539de124 519static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
d76cac7d
LS
520{
521 BlkMigDevState *bmds;
ceb2bd09 522 int ret = 1;
d76cac7d
LS
523
524 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
ceb2bd09 525 ret = mig_save_device_dirty(f, bmds, is_async);
43be3a25 526 if (ret <= 0) {
d76cac7d
LS
527 break;
528 }
529 }
530
531 return ret;
c163b5ca
LS
532}
533
32c835ba
PB
534/* Called with no locks taken. */
535
59feec42 536static int flush_blks(QEMUFile *f)
c163b5ca 537{
5e5328be 538 BlkMigBlock *blk;
59feec42 539 int ret = 0;
a55eb92c 540
d0f2c4c6 541 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
d11ecd3d
JK
542 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
543 block_mig_state.transferred);
a55eb92c 544
52e850de 545 blk_mig_lock();
5e5328be
JK
546 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
547 if (qemu_file_rate_limit(f)) {
548 break;
549 }
4b640365 550 if (blk->ret < 0) {
59feec42 551 ret = blk->ret;
4b640365
JK
552 break;
553 }
a55eb92c 554
5e5328be 555 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
52e850de 556 blk_mig_unlock();
13197e3c 557 blk_send(f, blk);
52e850de 558 blk_mig_lock();
13197e3c 559
7267c094
AL
560 g_free(blk->buf);
561 g_free(blk);
a55eb92c 562
d11ecd3d
JK
563 block_mig_state.read_done--;
564 block_mig_state.transferred++;
565 assert(block_mig_state.read_done >= 0);
c163b5ca 566 }
52e850de 567 blk_mig_unlock();
c163b5ca 568
d0f2c4c6 569 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
d11ecd3d
JK
570 block_mig_state.submitted, block_mig_state.read_done,
571 block_mig_state.transferred);
59feec42 572 return ret;
c163b5ca
LS
573}
574
32c835ba
PB
575/* Called with iothread lock taken. */
576
889ae39c
LS
577static int64_t get_remaining_dirty(void)
578{
579 BlkMigDevState *bmds;
580 int64_t dirty = 0;
581
582 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
e4654d2d 583 dirty += bdrv_get_dirty_count(bmds->bs, bmds->dirty_bitmap);
889ae39c
LS
584 }
585
acc906c6 586 return dirty << BDRV_SECTOR_BITS;
889ae39c
LS
587}
588
32c835ba
PB
589/* Called with iothread lock taken. */
590
539de124 591static void blk_mig_cleanup(void)
4ec7fcc7 592{
82801d8f
JK
593 BlkMigDevState *bmds;
594 BlkMigBlock *blk;
4ec7fcc7 595
946d58be
KW
596 bdrv_drain_all();
597
e4654d2d 598 unset_dirty_tracking();
8f794c55 599
52e850de 600 blk_mig_lock();
82801d8f
JK
601 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
602 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
3718d8ab
FZ
603 bdrv_op_unblock_all(bmds->bs, bmds->blocker);
604 error_free(bmds->blocker);
8442cfd0 605 bdrv_unref(bmds->bs);
7267c094
AL
606 g_free(bmds->aio_bitmap);
607 g_free(bmds);
4ec7fcc7
JK
608 }
609
82801d8f
JK
610 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
611 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
7267c094
AL
612 g_free(blk->buf);
613 g_free(blk);
4ec7fcc7 614 }
52e850de 615 blk_mig_unlock();
4ec7fcc7
JK
616}
617
9b5bfab0
JQ
618static void block_migration_cancel(void *opaque)
619{
620 blk_mig_cleanup();
621}
622
d1315aac 623static int block_save_setup(QEMUFile *f, void *opaque)
c163b5ca 624{
2975725f
JQ
625 int ret;
626
d1315aac
JQ
627 DPRINTF("Enter save live setup submitted %d transferred %d\n",
628 block_mig_state.submitted, block_mig_state.transferred);
a55eb92c 629
9b095037 630 qemu_mutex_lock_iothread();
1ac362cd 631 init_blk_migration(f);
d1315aac
JQ
632
633 /* start track dirty blocks */
b8afb520
FZ
634 ret = set_dirty_tracking();
635
636 if (ret) {
637 qemu_mutex_unlock_iothread();
638 return ret;
639 }
640
9b095037 641 qemu_mutex_unlock_iothread();
d1315aac 642
59feec42 643 ret = flush_blks(f);
d1315aac 644 blk_mig_reset_dirty_cursor();
d1315aac
JQ
645 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
646
d418cf57 647 return ret;
d1315aac
JQ
648}
649
16310a3c 650static int block_save_iterate(QEMUFile *f, void *opaque)
d1315aac
JQ
651{
652 int ret;
6aaa9dae 653 int64_t last_ftell = qemu_ftell(f);
d1315aac 654
16310a3c
JQ
655 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
656 block_mig_state.submitted, block_mig_state.transferred);
d1315aac 657
59feec42 658 ret = flush_blks(f);
2975725f 659 if (ret) {
2975725f 660 return ret;
4b640365
JK
661 }
662
d76cac7d
LS
663 blk_mig_reset_dirty_cursor();
664
16310a3c 665 /* control the rate of transfer */
52e850de 666 blk_mig_lock();
16310a3c
JQ
667 while ((block_mig_state.submitted +
668 block_mig_state.read_done) * BLOCK_SIZE <
669 qemu_file_get_rate_limit(f)) {
52e850de 670 blk_mig_unlock();
16310a3c
JQ
671 if (block_mig_state.bulk_completed == 0) {
672 /* first finish the bulk phase */
673 if (blk_mig_save_bulked_block(f) == 0) {
674 /* finished saving bulk on all devices */
675 block_mig_state.bulk_completed = 1;
676 }
13197e3c 677 ret = 0;
16310a3c 678 } else {
32c835ba
PB
679 /* Always called with iothread lock taken for
680 * simplicity, block_save_complete also calls it.
681 */
682 qemu_mutex_lock_iothread();
43be3a25 683 ret = blk_mig_save_dirty_block(f, 1);
32c835ba 684 qemu_mutex_unlock_iothread();
13197e3c
PB
685 }
686 if (ret < 0) {
687 return ret;
688 }
52e850de 689 blk_mig_lock();
13197e3c
PB
690 if (ret != 0) {
691 /* no more dirty blocks */
692 break;
a55eb92c 693 }
16310a3c 694 }
52e850de 695 blk_mig_unlock();
a55eb92c 696
59feec42 697 ret = flush_blks(f);
16310a3c 698 if (ret) {
16310a3c 699 return ret;
4b640365
JK
700 }
701
16310a3c 702 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
6aaa9dae 703 return qemu_ftell(f) - last_ftell;
16310a3c
JQ
704}
705
32c835ba
PB
706/* Called with iothread lock taken. */
707
16310a3c
JQ
708static int block_save_complete(QEMUFile *f, void *opaque)
709{
710 int ret;
711
712 DPRINTF("Enter save live complete submitted %d transferred %d\n",
713 block_mig_state.submitted, block_mig_state.transferred);
714
59feec42 715 ret = flush_blks(f);
16310a3c 716 if (ret) {
16310a3c
JQ
717 return ret;
718 }
a55eb92c 719
16310a3c 720 blk_mig_reset_dirty_cursor();
01e61e2d 721
16310a3c
JQ
722 /* we know for sure that save bulk is completed and
723 all async read completed */
52e850de 724 blk_mig_lock();
16310a3c 725 assert(block_mig_state.submitted == 0);
52e850de 726 blk_mig_unlock();
16310a3c 727
43be3a25
JQ
728 do {
729 ret = blk_mig_save_dirty_block(f, 0);
d418cf57
PB
730 if (ret < 0) {
731 return ret;
732 }
43be3a25 733 } while (ret == 0);
4b640365 734
43be3a25
JQ
735 /* report completion */
736 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
a55eb92c 737
16310a3c
JQ
738 DPRINTF("Block migration completed\n");
739
a55eb92c
JK
740 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
741
d418cf57 742 blk_mig_cleanup();
16310a3c 743 return 0;
c163b5ca
LS
744}
745
e4ed1541
JQ
746static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
747{
6aaa9dae 748 /* Estimate pending number of bytes to send */
13197e3c
PB
749 uint64_t pending;
750
32c835ba 751 qemu_mutex_lock_iothread();
52e850de 752 blk_mig_lock();
13197e3c 753 pending = get_remaining_dirty() +
6aaa9dae
SH
754 block_mig_state.submitted * BLOCK_SIZE +
755 block_mig_state.read_done * BLOCK_SIZE;
756
757 /* Report at least one block pending during bulk phase */
758 if (pending == 0 && !block_mig_state.bulk_completed) {
759 pending = BLOCK_SIZE;
760 }
52e850de 761 blk_mig_unlock();
32c835ba 762 qemu_mutex_unlock_iothread();
e4ed1541 763
6aaa9dae
SH
764 DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
765 return pending;
e4ed1541
JQ
766}
767
c163b5ca
LS
768static int block_load(QEMUFile *f, void *opaque, int version_id)
769{
01e61e2d 770 static int banner_printed;
c163b5ca
LS
771 int len, flags;
772 char device_name[256];
773 int64_t addr;
77358b59 774 BlockDriverState *bs, *bs_prev = NULL;
c163b5ca 775 uint8_t *buf;
77358b59
PR
776 int64_t total_sectors = 0;
777 int nr_sectors;
42802d47 778 int ret;
a55eb92c 779
c163b5ca 780 do {
c163b5ca 781 addr = qemu_get_be64(f);
a55eb92c 782
6ea44308
JK
783 flags = addr & ~BDRV_SECTOR_MASK;
784 addr >>= BDRV_SECTOR_BITS;
a55eb92c
JK
785
786 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
c163b5ca
LS
787 /* get device name */
788 len = qemu_get_byte(f);
c163b5ca
LS
789 qemu_get_buffer(f, (uint8_t *)device_name, len);
790 device_name[len] = '\0';
a55eb92c 791
c163b5ca 792 bs = bdrv_find(device_name);
4b640365
JK
793 if (!bs) {
794 fprintf(stderr, "Error unknown block device %s\n",
795 device_name);
796 return -EINVAL;
797 }
a55eb92c 798
77358b59
PR
799 if (bs != bs_prev) {
800 bs_prev = bs;
57322b78 801 total_sectors = bdrv_nb_sectors(bs);
77358b59 802 if (total_sectors <= 0) {
6daf194d 803 error_report("Error getting length of block device %s",
77358b59
PR
804 device_name);
805 return -EINVAL;
806 }
807 }
808
809 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
810 nr_sectors = total_sectors - addr;
811 } else {
812 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
813 }
814
323004a3 815 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
d32f35cb
PL
816 ret = bdrv_write_zeroes(bs, addr, nr_sectors,
817 BDRV_REQ_MAY_UNMAP);
323004a3
PL
818 } else {
819 buf = g_malloc(BLOCK_SIZE);
820 qemu_get_buffer(f, buf, BLOCK_SIZE);
821 ret = bdrv_write(bs, addr, buf, nr_sectors);
822 g_free(buf);
823 }
575a58d7 824
b02bea3a
YT
825 if (ret < 0) {
826 return ret;
827 }
01e61e2d
JK
828 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
829 if (!banner_printed) {
830 printf("Receiving block device images\n");
831 banner_printed = 1;
832 }
833 printf("Completed %d %%%c", (int)addr,
834 (addr == 100) ? '\n' : '\r');
835 fflush(stdout);
a55eb92c 836 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
d5f1f286 837 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
4b640365
JK
838 return -EINVAL;
839 }
42802d47
JQ
840 ret = qemu_file_get_error(f);
841 if (ret != 0) {
842 return ret;
c163b5ca 843 }
a55eb92c
JK
844 } while (!(flags & BLK_MIG_FLAG_EOS));
845
c163b5ca
LS
846 return 0;
847}
848
6607ae23 849static void block_set_params(const MigrationParams *params, void *opaque)
c163b5ca 850{
6607ae23
IY
851 block_mig_state.blk_enable = params->blk;
852 block_mig_state.shared_base = params->shared;
a55eb92c 853
c163b5ca 854 /* shared base means that blk_enable = 1 */
6607ae23 855 block_mig_state.blk_enable |= params->shared;
c163b5ca
LS
856}
857
6bd68781
JQ
858static bool block_is_active(void *opaque)
859{
860 return block_mig_state.blk_enable == 1;
861}
862
7a46d042 863static SaveVMHandlers savevm_block_handlers = {
7908c78d 864 .set_params = block_set_params,
d1315aac 865 .save_live_setup = block_save_setup,
16310a3c
JQ
866 .save_live_iterate = block_save_iterate,
867 .save_live_complete = block_save_complete,
e4ed1541 868 .save_live_pending = block_save_pending,
7908c78d 869 .load_state = block_load,
9b5bfab0 870 .cancel = block_migration_cancel,
6bd68781 871 .is_active = block_is_active,
7908c78d
JQ
872};
873
c163b5ca 874void blk_mig_init(void)
a55eb92c 875{
5e5328be
JK
876 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
877 QSIMPLEQ_INIT(&block_mig_state.blk_list);
52e850de 878 qemu_mutex_init(&block_mig_state.lock);
5e5328be 879
7908c78d
JQ
880 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
881 &block_mig_state);
c163b5ca 882}