]> git.ipfire.org Git - thirdparty/qemu.git/blame - block-migration.c
target-ppc: enable virtio endian ambivalent support
[thirdparty/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
62155e2b 189 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(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
226 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
227 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
228 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
229
7267c094 230 bmds->aio_bitmap = g_malloc0(bitmap_size);
33656af7
MT
231}
232
52e850de
PB
233/* Never hold migration lock when yielding to the main loop! */
234
c163b5ca
LS
235static void blk_mig_read_cb(void *opaque, int ret)
236{
237 BlkMigBlock *blk = opaque;
a55eb92c 238
52e850de 239 blk_mig_lock();
c163b5ca 240 blk->ret = ret;
a55eb92c 241
5e5328be 242 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
33656af7 243 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
a55eb92c 244
d11ecd3d
JK
245 block_mig_state.submitted--;
246 block_mig_state.read_done++;
247 assert(block_mig_state.submitted >= 0);
52e850de 248 blk_mig_unlock();
c163b5ca
LS
249}
250
32c835ba
PB
251/* Called with no lock taken. */
252
539de124 253static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
a55eb92c 254{
57cce12d
JK
255 int64_t total_sectors = bmds->total_sectors;
256 int64_t cur_sector = bmds->cur_sector;
257 BlockDriverState *bs = bmds->bs;
c163b5ca 258 BlkMigBlock *blk;
13f0b67f 259 int nr_sectors;
a55eb92c 260
57cce12d 261 if (bmds->shared_base) {
32c835ba 262 qemu_mutex_lock_iothread();
b1d10856 263 while (cur_sector < total_sectors &&
57cce12d
JK
264 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
265 &nr_sectors)) {
c163b5ca
LS
266 cur_sector += nr_sectors;
267 }
32c835ba 268 qemu_mutex_unlock_iothread();
c163b5ca 269 }
a55eb92c
JK
270
271 if (cur_sector >= total_sectors) {
82801d8f 272 bmds->cur_sector = bmds->completed_sectors = total_sectors;
c163b5ca
LS
273 return 1;
274 }
a55eb92c 275
82801d8f 276 bmds->completed_sectors = cur_sector;
a55eb92c 277
57cce12d
JK
278 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
279
6ea44308
JK
280 /* we are going to transfer a full block even if it is not allocated */
281 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
c163b5ca 282
6ea44308 283 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
57cce12d 284 nr_sectors = total_sectors - cur_sector;
c163b5ca 285 }
a55eb92c 286
7267c094
AL
287 blk = g_malloc(sizeof(BlkMigBlock));
288 blk->buf = g_malloc(BLOCK_SIZE);
13f0b67f
JK
289 blk->bmds = bmds;
290 blk->sector = cur_sector;
33656af7 291 blk->nr_sectors = nr_sectors;
a55eb92c 292
e970ec0b
LS
293 blk->iov.iov_base = blk->buf;
294 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
295 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
a55eb92c 296
52e850de 297 blk_mig_lock();
13197e3c 298 block_mig_state.submitted++;
52e850de 299 blk_mig_unlock();
13197e3c 300
32c835ba 301 qemu_mutex_lock_iothread();
e970ec0b
LS
302 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
303 nr_sectors, blk_mig_read_cb, blk);
d76cac7d 304
13f0b67f 305 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
32c835ba 306 qemu_mutex_unlock_iothread();
a55eb92c 307
32c835ba 308 bmds->cur_sector = cur_sector + nr_sectors;
13f0b67f 309 return (bmds->cur_sector >= total_sectors);
c163b5ca
LS
310}
311
32c835ba
PB
312/* Called with iothread lock taken. */
313
b8afb520 314static int set_dirty_tracking(void)
c163b5ca
LS
315{
316 BlkMigDevState *bmds;
b8afb520
FZ
317 int ret;
318
319 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
320 bmds->dirty_bitmap = bdrv_create_dirty_bitmap(bmds->bs, BLOCK_SIZE,
321 NULL);
322 if (!bmds->dirty_bitmap) {
323 ret = -errno;
324 goto fail;
325 }
326 }
327 return 0;
5e5328be 328
b8afb520 329fail:
5e5328be 330 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
b8afb520
FZ
331 if (bmds->dirty_bitmap) {
332 bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
333 }
e4654d2d 334 }
b8afb520 335 return ret;
e4654d2d
FZ
336}
337
338static void unset_dirty_tracking(void)
339{
340 BlkMigDevState *bmds;
341
342 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
343 bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
c163b5ca 344 }
c163b5ca
LS
345}
346
b66460e4 347static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
c163b5ca 348{
5e5328be 349 BlkMigDevState *bmds;
792773b2 350 int64_t sectors;
a55eb92c 351
d246673d 352 if (!bdrv_is_read_only(bs)) {
b66460e4 353 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
31f54f24 354 if (sectors <= 0) {
b66460e4
SH
355 return;
356 }
357
7267c094 358 bmds = g_malloc0(sizeof(BlkMigDevState));
b66460e4
SH
359 bmds->bs = bs;
360 bmds->bulk_completed = 0;
361 bmds->total_sectors = sectors;
362 bmds->completed_sectors = 0;
363 bmds->shared_base = block_mig_state.shared_base;
33656af7 364 alloc_aio_bitmap(bmds);
3718d8ab
FZ
365 error_setg(&bmds->blocker, "block device is in use by migration");
366 bdrv_op_block_all(bs, bmds->blocker);
8442cfd0 367 bdrv_ref(bs);
b66460e4
SH
368
369 block_mig_state.total_sector_sum += sectors;
370
371 if (bmds->shared_base) {
539de124
LC
372 DPRINTF("Start migration for %s with shared base image\n",
373 bs->device_name);
b66460e4 374 } else {
539de124 375 DPRINTF("Start full migration for %s\n", bs->device_name);
b66460e4
SH
376 }
377
378 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
379 }
380}
381
539de124 382static void init_blk_migration(QEMUFile *f)
b66460e4 383{
69d63a97
JK
384 block_mig_state.submitted = 0;
385 block_mig_state.read_done = 0;
386 block_mig_state.transferred = 0;
82801d8f 387 block_mig_state.total_sector_sum = 0;
01e61e2d 388 block_mig_state.prev_progress = -1;
e970ec0b 389 block_mig_state.bulk_completed = 0;
323004a3 390 block_mig_state.zero_blocks = migrate_zero_blocks();
69d63a97 391
539de124 392 bdrv_iterate(init_blk_migration_it, NULL);
c163b5ca
LS
393}
394
32c835ba
PB
395/* Called with no lock taken. */
396
539de124 397static int blk_mig_save_bulked_block(QEMUFile *f)
c163b5ca 398{
82801d8f 399 int64_t completed_sector_sum = 0;
c163b5ca 400 BlkMigDevState *bmds;
01e61e2d 401 int progress;
82801d8f 402 int ret = 0;
c163b5ca 403
5e5328be 404 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 405 if (bmds->bulk_completed == 0) {
539de124 406 if (mig_save_device_bulk(f, bmds) == 1) {
57cce12d
JK
407 /* completed bulk section for this device */
408 bmds->bulk_completed = 1;
c163b5ca 409 }
82801d8f
JK
410 completed_sector_sum += bmds->completed_sectors;
411 ret = 1;
412 break;
413 } else {
414 completed_sector_sum += bmds->completed_sectors;
c163b5ca
LS
415 }
416 }
a55eb92c 417
8b6b2afc
PR
418 if (block_mig_state.total_sector_sum != 0) {
419 progress = completed_sector_sum * 100 /
420 block_mig_state.total_sector_sum;
421 } else {
422 progress = 100;
423 }
01e61e2d
JK
424 if (progress != block_mig_state.prev_progress) {
425 block_mig_state.prev_progress = progress;
426 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
427 | BLK_MIG_FLAG_PROGRESS);
539de124 428 DPRINTF("Completed %d %%\r", progress);
82801d8f
JK
429 }
430
431 return ret;
c163b5ca
LS
432}
433
d76cac7d 434static void blk_mig_reset_dirty_cursor(void)
c163b5ca
LS
435{
436 BlkMigDevState *bmds;
d76cac7d
LS
437
438 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
439 bmds->cur_dirty = 0;
440 }
441}
442
32c835ba
PB
443/* Called with iothread lock taken. */
444
539de124
LC
445static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
446 int is_async)
d76cac7d
LS
447{
448 BlkMigBlock *blk;
449 int64_t total_sectors = bmds->total_sectors;
c163b5ca 450 int64_t sector;
d76cac7d 451 int nr_sectors;
dcd1d224 452 int ret = -EIO;
a55eb92c 453
d76cac7d 454 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
52e850de 455 blk_mig_lock();
62155e2b 456 if (bmds_aio_inflight(bmds, sector)) {
52e850de 457 blk_mig_unlock();
922453bc 458 bdrv_drain_all();
52e850de
PB
459 } else {
460 blk_mig_unlock();
62155e2b 461 }
e4654d2d 462 if (bdrv_get_dirty(bmds->bs, bmds->dirty_bitmap, sector)) {
575a58d7 463
d76cac7d
LS
464 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
465 nr_sectors = total_sectors - sector;
466 } else {
467 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
468 }
7267c094
AL
469 blk = g_malloc(sizeof(BlkMigBlock));
470 blk->buf = g_malloc(BLOCK_SIZE);
d76cac7d
LS
471 blk->bmds = bmds;
472 blk->sector = sector;
33656af7 473 blk->nr_sectors = nr_sectors;
d76cac7d 474
889ae39c 475 if (is_async) {
d76cac7d
LS
476 blk->iov.iov_base = blk->buf;
477 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
478 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
479
480 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
481 nr_sectors, blk_mig_read_cb, blk);
52e850de
PB
482
483 blk_mig_lock();
d76cac7d 484 block_mig_state.submitted++;
33656af7 485 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
52e850de 486 blk_mig_unlock();
d76cac7d 487 } else {
dcd1d224
JQ
488 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
489 if (ret < 0) {
d76cac7d 490 goto error;
c163b5ca 491 }
d76cac7d 492 blk_send(f, blk);
a55eb92c 493
7267c094
AL
494 g_free(blk->buf);
495 g_free(blk);
a55eb92c 496 }
d76cac7d
LS
497
498 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
499 break;
c163b5ca 500 }
d76cac7d
LS
501 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
502 bmds->cur_dirty = sector;
c163b5ca 503 }
575a58d7 504
d76cac7d
LS
505 return (bmds->cur_dirty >= bmds->total_sectors);
506
889ae39c 507error:
539de124 508 DPRINTF("Error reading sector %" PRId64 "\n", sector);
7267c094
AL
509 g_free(blk->buf);
510 g_free(blk);
43be3a25 511 return ret;
d76cac7d
LS
512}
513
32c835ba
PB
514/* Called with iothread lock taken.
515 *
516 * return value:
ceb2bd09
JQ
517 * 0: too much data for max_downtime
518 * 1: few enough data for max_downtime
519*/
539de124 520static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
d76cac7d
LS
521{
522 BlkMigDevState *bmds;
ceb2bd09 523 int ret = 1;
d76cac7d
LS
524
525 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
ceb2bd09 526 ret = mig_save_device_dirty(f, bmds, is_async);
43be3a25 527 if (ret <= 0) {
d76cac7d
LS
528 break;
529 }
530 }
531
532 return ret;
c163b5ca
LS
533}
534
32c835ba
PB
535/* Called with no locks taken. */
536
59feec42 537static int flush_blks(QEMUFile *f)
c163b5ca 538{
5e5328be 539 BlkMigBlock *blk;
59feec42 540 int ret = 0;
a55eb92c 541
d0f2c4c6 542 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
d11ecd3d
JK
543 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
544 block_mig_state.transferred);
a55eb92c 545
52e850de 546 blk_mig_lock();
5e5328be
JK
547 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
548 if (qemu_file_rate_limit(f)) {
549 break;
550 }
4b640365 551 if (blk->ret < 0) {
59feec42 552 ret = blk->ret;
4b640365
JK
553 break;
554 }
a55eb92c 555
5e5328be 556 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
52e850de 557 blk_mig_unlock();
13197e3c 558 blk_send(f, blk);
52e850de 559 blk_mig_lock();
13197e3c 560
7267c094
AL
561 g_free(blk->buf);
562 g_free(blk);
a55eb92c 563
d11ecd3d
JK
564 block_mig_state.read_done--;
565 block_mig_state.transferred++;
566 assert(block_mig_state.read_done >= 0);
c163b5ca 567 }
52e850de 568 blk_mig_unlock();
c163b5ca 569
d0f2c4c6 570 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
d11ecd3d
JK
571 block_mig_state.submitted, block_mig_state.read_done,
572 block_mig_state.transferred);
59feec42 573 return ret;
c163b5ca
LS
574}
575
32c835ba
PB
576/* Called with iothread lock taken. */
577
889ae39c
LS
578static int64_t get_remaining_dirty(void)
579{
580 BlkMigDevState *bmds;
581 int64_t dirty = 0;
582
583 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
e4654d2d 584 dirty += bdrv_get_dirty_count(bmds->bs, bmds->dirty_bitmap);
889ae39c
LS
585 }
586
acc906c6 587 return dirty << BDRV_SECTOR_BITS;
889ae39c
LS
588}
589
32c835ba
PB
590/* Called with iothread lock taken. */
591
539de124 592static void blk_mig_cleanup(void)
4ec7fcc7 593{
82801d8f
JK
594 BlkMigDevState *bmds;
595 BlkMigBlock *blk;
4ec7fcc7 596
946d58be
KW
597 bdrv_drain_all();
598
e4654d2d 599 unset_dirty_tracking();
8f794c55 600
52e850de 601 blk_mig_lock();
82801d8f
JK
602 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
603 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
3718d8ab
FZ
604 bdrv_op_unblock_all(bmds->bs, bmds->blocker);
605 error_free(bmds->blocker);
8442cfd0 606 bdrv_unref(bmds->bs);
7267c094
AL
607 g_free(bmds->aio_bitmap);
608 g_free(bmds);
4ec7fcc7
JK
609 }
610
82801d8f
JK
611 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
612 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
7267c094
AL
613 g_free(blk->buf);
614 g_free(blk);
4ec7fcc7 615 }
52e850de 616 blk_mig_unlock();
4ec7fcc7
JK
617}
618
9b5bfab0
JQ
619static void block_migration_cancel(void *opaque)
620{
621 blk_mig_cleanup();
622}
623
d1315aac 624static int block_save_setup(QEMUFile *f, void *opaque)
c163b5ca 625{
2975725f
JQ
626 int ret;
627
d1315aac
JQ
628 DPRINTF("Enter save live setup submitted %d transferred %d\n",
629 block_mig_state.submitted, block_mig_state.transferred);
a55eb92c 630
9b095037 631 qemu_mutex_lock_iothread();
1ac362cd 632 init_blk_migration(f);
d1315aac
JQ
633
634 /* start track dirty blocks */
b8afb520
FZ
635 ret = set_dirty_tracking();
636
637 if (ret) {
638 qemu_mutex_unlock_iothread();
639 return ret;
640 }
641
9b095037 642 qemu_mutex_unlock_iothread();
d1315aac 643
59feec42 644 ret = flush_blks(f);
d1315aac 645 blk_mig_reset_dirty_cursor();
d1315aac
JQ
646 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
647
d418cf57 648 return ret;
d1315aac
JQ
649}
650
16310a3c 651static int block_save_iterate(QEMUFile *f, void *opaque)
d1315aac
JQ
652{
653 int ret;
6aaa9dae 654 int64_t last_ftell = qemu_ftell(f);
d1315aac 655
16310a3c
JQ
656 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
657 block_mig_state.submitted, block_mig_state.transferred);
d1315aac 658
59feec42 659 ret = flush_blks(f);
2975725f 660 if (ret) {
2975725f 661 return ret;
4b640365
JK
662 }
663
d76cac7d
LS
664 blk_mig_reset_dirty_cursor();
665
16310a3c 666 /* control the rate of transfer */
52e850de 667 blk_mig_lock();
16310a3c
JQ
668 while ((block_mig_state.submitted +
669 block_mig_state.read_done) * BLOCK_SIZE <
670 qemu_file_get_rate_limit(f)) {
52e850de 671 blk_mig_unlock();
16310a3c
JQ
672 if (block_mig_state.bulk_completed == 0) {
673 /* first finish the bulk phase */
674 if (blk_mig_save_bulked_block(f) == 0) {
675 /* finished saving bulk on all devices */
676 block_mig_state.bulk_completed = 1;
677 }
13197e3c 678 ret = 0;
16310a3c 679 } else {
32c835ba
PB
680 /* Always called with iothread lock taken for
681 * simplicity, block_save_complete also calls it.
682 */
683 qemu_mutex_lock_iothread();
43be3a25 684 ret = blk_mig_save_dirty_block(f, 1);
32c835ba 685 qemu_mutex_unlock_iothread();
13197e3c
PB
686 }
687 if (ret < 0) {
688 return ret;
689 }
52e850de 690 blk_mig_lock();
13197e3c
PB
691 if (ret != 0) {
692 /* no more dirty blocks */
693 break;
a55eb92c 694 }
16310a3c 695 }
52e850de 696 blk_mig_unlock();
a55eb92c 697
59feec42 698 ret = flush_blks(f);
16310a3c 699 if (ret) {
16310a3c 700 return ret;
4b640365
JK
701 }
702
16310a3c 703 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
6aaa9dae 704 return qemu_ftell(f) - last_ftell;
16310a3c
JQ
705}
706
32c835ba
PB
707/* Called with iothread lock taken. */
708
16310a3c
JQ
709static int block_save_complete(QEMUFile *f, void *opaque)
710{
711 int ret;
712
713 DPRINTF("Enter save live complete submitted %d transferred %d\n",
714 block_mig_state.submitted, block_mig_state.transferred);
715
59feec42 716 ret = flush_blks(f);
16310a3c 717 if (ret) {
16310a3c
JQ
718 return ret;
719 }
a55eb92c 720
16310a3c 721 blk_mig_reset_dirty_cursor();
01e61e2d 722
16310a3c
JQ
723 /* we know for sure that save bulk is completed and
724 all async read completed */
52e850de 725 blk_mig_lock();
16310a3c 726 assert(block_mig_state.submitted == 0);
52e850de 727 blk_mig_unlock();
16310a3c 728
43be3a25
JQ
729 do {
730 ret = blk_mig_save_dirty_block(f, 0);
d418cf57
PB
731 if (ret < 0) {
732 return ret;
733 }
43be3a25 734 } while (ret == 0);
4b640365 735
43be3a25
JQ
736 /* report completion */
737 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
a55eb92c 738
16310a3c
JQ
739 DPRINTF("Block migration completed\n");
740
a55eb92c
JK
741 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
742
d418cf57 743 blk_mig_cleanup();
16310a3c 744 return 0;
c163b5ca
LS
745}
746
e4ed1541
JQ
747static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
748{
6aaa9dae 749 /* Estimate pending number of bytes to send */
13197e3c
PB
750 uint64_t pending;
751
32c835ba 752 qemu_mutex_lock_iothread();
52e850de 753 blk_mig_lock();
13197e3c 754 pending = get_remaining_dirty() +
6aaa9dae
SH
755 block_mig_state.submitted * BLOCK_SIZE +
756 block_mig_state.read_done * BLOCK_SIZE;
757
758 /* Report at least one block pending during bulk phase */
759 if (pending == 0 && !block_mig_state.bulk_completed) {
760 pending = BLOCK_SIZE;
761 }
52e850de 762 blk_mig_unlock();
32c835ba 763 qemu_mutex_unlock_iothread();
e4ed1541 764
6aaa9dae
SH
765 DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
766 return pending;
e4ed1541
JQ
767}
768
c163b5ca
LS
769static int block_load(QEMUFile *f, void *opaque, int version_id)
770{
01e61e2d 771 static int banner_printed;
c163b5ca
LS
772 int len, flags;
773 char device_name[256];
774 int64_t addr;
77358b59 775 BlockDriverState *bs, *bs_prev = NULL;
c163b5ca 776 uint8_t *buf;
77358b59
PR
777 int64_t total_sectors = 0;
778 int nr_sectors;
42802d47 779 int ret;
a55eb92c 780
c163b5ca 781 do {
c163b5ca 782 addr = qemu_get_be64(f);
a55eb92c 783
6ea44308
JK
784 flags = addr & ~BDRV_SECTOR_MASK;
785 addr >>= BDRV_SECTOR_BITS;
a55eb92c
JK
786
787 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
c163b5ca
LS
788 /* get device name */
789 len = qemu_get_byte(f);
c163b5ca
LS
790 qemu_get_buffer(f, (uint8_t *)device_name, len);
791 device_name[len] = '\0';
a55eb92c 792
c163b5ca 793 bs = bdrv_find(device_name);
4b640365
JK
794 if (!bs) {
795 fprintf(stderr, "Error unknown block device %s\n",
796 device_name);
797 return -EINVAL;
798 }
a55eb92c 799
77358b59
PR
800 if (bs != bs_prev) {
801 bs_prev = bs;
802 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
803 if (total_sectors <= 0) {
6daf194d 804 error_report("Error getting length of block device %s",
77358b59
PR
805 device_name);
806 return -EINVAL;
807 }
808 }
809
810 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
811 nr_sectors = total_sectors - addr;
812 } else {
813 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
814 }
815
323004a3 816 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
d32f35cb
PL
817 ret = bdrv_write_zeroes(bs, addr, nr_sectors,
818 BDRV_REQ_MAY_UNMAP);
323004a3
PL
819 } else {
820 buf = g_malloc(BLOCK_SIZE);
821 qemu_get_buffer(f, buf, BLOCK_SIZE);
822 ret = bdrv_write(bs, addr, buf, nr_sectors);
823 g_free(buf);
824 }
575a58d7 825
b02bea3a
YT
826 if (ret < 0) {
827 return ret;
828 }
01e61e2d
JK
829 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
830 if (!banner_printed) {
831 printf("Receiving block device images\n");
832 banner_printed = 1;
833 }
834 printf("Completed %d %%%c", (int)addr,
835 (addr == 100) ? '\n' : '\r');
836 fflush(stdout);
a55eb92c 837 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
d5f1f286 838 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
4b640365
JK
839 return -EINVAL;
840 }
42802d47
JQ
841 ret = qemu_file_get_error(f);
842 if (ret != 0) {
843 return ret;
c163b5ca 844 }
a55eb92c
JK
845 } while (!(flags & BLK_MIG_FLAG_EOS));
846
c163b5ca
LS
847 return 0;
848}
849
6607ae23 850static void block_set_params(const MigrationParams *params, void *opaque)
c163b5ca 851{
6607ae23
IY
852 block_mig_state.blk_enable = params->blk;
853 block_mig_state.shared_base = params->shared;
a55eb92c 854
c163b5ca 855 /* shared base means that blk_enable = 1 */
6607ae23 856 block_mig_state.blk_enable |= params->shared;
c163b5ca
LS
857}
858
6bd68781
JQ
859static bool block_is_active(void *opaque)
860{
861 return block_mig_state.blk_enable == 1;
862}
863
7908c78d
JQ
864SaveVMHandlers savevm_block_handlers = {
865 .set_params = block_set_params,
d1315aac 866 .save_live_setup = block_save_setup,
16310a3c
JQ
867 .save_live_iterate = block_save_iterate,
868 .save_live_complete = block_save_complete,
e4ed1541 869 .save_live_pending = block_save_pending,
7908c78d 870 .load_state = block_load,
9b5bfab0 871 .cancel = block_migration_cancel,
6bd68781 872 .is_active = block_is_active,
7908c78d
JQ
873};
874
c163b5ca 875void blk_mig_init(void)
a55eb92c 876{
5e5328be
JK
877 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
878 QSIMPLEQ_INIT(&block_mig_state.blk_list);
52e850de 879 qemu_mutex_init(&block_mig_state.lock);
5e5328be 880
7908c78d
JQ
881 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
882 &block_mig_state);
c163b5ca 883}