s390x/MAINTAINERS: Update my email address
[qemu.git] / migration / block.c
blob7674ae107897e59b00907b5dc24a0c02abbf144b
1 /*
2 * QEMU live block migration
4 * Copyright IBM, Corp. 2009
6 * Authors:
7 * Liran Schour <lirans@il.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
19 #include "qemu/cutils.h"
20 #include "qemu/queue.h"
21 #include "block.h"
22 #include "migration/misc.h"
23 #include "migration.h"
24 #include "migration/register.h"
25 #include "qemu-file.h"
26 #include "migration/vmstate.h"
27 #include "sysemu/block-backend.h"
29 #define BLOCK_SIZE (1 << 20)
30 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
32 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
33 #define BLK_MIG_FLAG_EOS 0x02
34 #define BLK_MIG_FLAG_PROGRESS 0x04
35 #define BLK_MIG_FLAG_ZERO_BLOCK 0x08
37 #define MAX_IS_ALLOCATED_SEARCH 65536
39 #define MAX_INFLIGHT_IO 512
41 //#define DEBUG_BLK_MIGRATION
43 #ifdef DEBUG_BLK_MIGRATION
44 #define DPRINTF(fmt, ...) \
45 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
46 #else
47 #define DPRINTF(fmt, ...) \
48 do { } while (0)
49 #endif
51 typedef struct BlkMigDevState {
52 /* Written during setup phase. Can be read without a lock. */
53 BlockBackend *blk;
54 char *blk_name;
55 int shared_base;
56 int64_t total_sectors;
57 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
58 Error *blocker;
60 /* Only used by migration thread. Does not need a lock. */
61 int bulk_completed;
62 int64_t cur_sector;
63 int64_t cur_dirty;
65 /* Data in the aio_bitmap is protected by block migration lock.
66 * Allocation and free happen during setup and cleanup respectively.
68 unsigned long *aio_bitmap;
70 /* Protected by block migration lock. */
71 int64_t completed_sectors;
73 /* During migration this is protected by iothread lock / AioContext.
74 * Allocation and free happen during setup and cleanup respectively.
76 BdrvDirtyBitmap *dirty_bitmap;
77 } BlkMigDevState;
79 typedef struct BlkMigBlock {
80 /* Only used by migration thread. */
81 uint8_t *buf;
82 BlkMigDevState *bmds;
83 int64_t sector;
84 int nr_sectors;
85 struct iovec iov;
86 QEMUIOVector qiov;
87 BlockAIOCB *aiocb;
89 /* Protected by block migration lock. */
90 int ret;
91 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
92 } BlkMigBlock;
94 typedef struct BlkMigState {
95 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
96 int64_t total_sector_sum;
97 bool zero_blocks;
99 /* Protected by lock. */
100 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
101 int submitted;
102 int read_done;
104 /* Only used by migration thread. Does not need a lock. */
105 int transferred;
106 int prev_progress;
107 int bulk_completed;
109 /* Lock must be taken _inside_ the iothread lock and any AioContexts. */
110 QemuMutex lock;
111 } BlkMigState;
113 static BlkMigState block_mig_state;
115 static void blk_mig_lock(void)
117 qemu_mutex_lock(&block_mig_state.lock);
120 static void blk_mig_unlock(void)
122 qemu_mutex_unlock(&block_mig_state.lock);
125 /* Must run outside of the iothread lock during the bulk phase,
126 * or the VM will stall.
129 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
131 int len;
132 uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK;
134 if (block_mig_state.zero_blocks &&
135 buffer_is_zero(blk->buf, BLOCK_SIZE)) {
136 flags |= BLK_MIG_FLAG_ZERO_BLOCK;
139 /* sector number and flags */
140 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
141 | flags);
143 /* device name */
144 len = strlen(blk->bmds->blk_name);
145 qemu_put_byte(f, len);
146 qemu_put_buffer(f, (uint8_t *) blk->bmds->blk_name, len);
148 /* if a block is zero we need to flush here since the network
149 * bandwidth is now a lot higher than the storage device bandwidth.
150 * thus if we queue zero blocks we slow down the migration */
151 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
152 qemu_fflush(f);
153 return;
156 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
159 int blk_mig_active(void)
161 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
164 uint64_t blk_mig_bytes_transferred(void)
166 BlkMigDevState *bmds;
167 uint64_t sum = 0;
169 blk_mig_lock();
170 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
171 sum += bmds->completed_sectors;
173 blk_mig_unlock();
174 return sum << BDRV_SECTOR_BITS;
177 uint64_t blk_mig_bytes_remaining(void)
179 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
182 uint64_t blk_mig_bytes_total(void)
184 BlkMigDevState *bmds;
185 uint64_t sum = 0;
187 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
188 sum += bmds->total_sectors;
190 return sum << BDRV_SECTOR_BITS;
194 /* Called with migration lock held. */
196 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
198 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
200 if (sector < blk_nb_sectors(bmds->blk)) {
201 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
202 (1UL << (chunk % (sizeof(unsigned long) * 8))));
203 } else {
204 return 0;
208 /* Called with migration lock held. */
210 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
211 int nb_sectors, int set)
213 int64_t start, end;
214 unsigned long val, idx, bit;
216 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
217 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
219 for (; start <= end; start++) {
220 idx = start / (sizeof(unsigned long) * 8);
221 bit = start % (sizeof(unsigned long) * 8);
222 val = bmds->aio_bitmap[idx];
223 if (set) {
224 val |= 1UL << bit;
225 } else {
226 val &= ~(1UL << bit);
228 bmds->aio_bitmap[idx] = val;
232 static void alloc_aio_bitmap(BlkMigDevState *bmds)
234 BlockBackend *bb = bmds->blk;
235 int64_t bitmap_size;
237 bitmap_size = blk_nb_sectors(bb) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
238 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
240 bmds->aio_bitmap = g_malloc0(bitmap_size);
243 /* Never hold migration lock when yielding to the main loop! */
245 static void blk_mig_read_cb(void *opaque, int ret)
247 BlkMigBlock *blk = opaque;
249 blk_mig_lock();
250 blk->ret = ret;
252 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
253 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
255 block_mig_state.submitted--;
256 block_mig_state.read_done++;
257 assert(block_mig_state.submitted >= 0);
258 blk_mig_unlock();
261 /* Called with no lock taken. */
263 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
265 int64_t total_sectors = bmds->total_sectors;
266 int64_t cur_sector = bmds->cur_sector;
267 BlockBackend *bb = bmds->blk;
268 BlkMigBlock *blk;
269 int nr_sectors;
271 if (bmds->shared_base) {
272 qemu_mutex_lock_iothread();
273 aio_context_acquire(blk_get_aio_context(bb));
274 /* Skip unallocated sectors; intentionally treats failure as
275 * an allocated sector */
276 while (cur_sector < total_sectors &&
277 !bdrv_is_allocated(blk_bs(bb), cur_sector,
278 MAX_IS_ALLOCATED_SEARCH, &nr_sectors)) {
279 cur_sector += nr_sectors;
281 aio_context_release(blk_get_aio_context(bb));
282 qemu_mutex_unlock_iothread();
285 if (cur_sector >= total_sectors) {
286 bmds->cur_sector = bmds->completed_sectors = total_sectors;
287 return 1;
290 bmds->completed_sectors = cur_sector;
292 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
294 /* we are going to transfer a full block even if it is not allocated */
295 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
297 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
298 nr_sectors = total_sectors - cur_sector;
301 blk = g_new(BlkMigBlock, 1);
302 blk->buf = g_malloc(BLOCK_SIZE);
303 blk->bmds = bmds;
304 blk->sector = cur_sector;
305 blk->nr_sectors = nr_sectors;
307 blk->iov.iov_base = blk->buf;
308 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
309 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
311 blk_mig_lock();
312 block_mig_state.submitted++;
313 blk_mig_unlock();
315 /* We do not know if bs is under the main thread (and thus does
316 * not acquire the AioContext when doing AIO) or rather under
317 * dataplane. Thus acquire both the iothread mutex and the
318 * AioContext.
320 * This is ugly and will disappear when we make bdrv_* thread-safe,
321 * without the need to acquire the AioContext.
323 qemu_mutex_lock_iothread();
324 aio_context_acquire(blk_get_aio_context(bmds->blk));
325 blk->aiocb = blk_aio_preadv(bb, cur_sector * BDRV_SECTOR_SIZE, &blk->qiov,
326 0, blk_mig_read_cb, blk);
328 bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector, nr_sectors);
329 aio_context_release(blk_get_aio_context(bmds->blk));
330 qemu_mutex_unlock_iothread();
332 bmds->cur_sector = cur_sector + nr_sectors;
333 return (bmds->cur_sector >= total_sectors);
336 /* Called with iothread lock taken. */
338 static int set_dirty_tracking(void)
340 BlkMigDevState *bmds;
341 int ret;
343 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
344 bmds->dirty_bitmap = bdrv_create_dirty_bitmap(blk_bs(bmds->blk),
345 BLOCK_SIZE, NULL, NULL);
346 if (!bmds->dirty_bitmap) {
347 ret = -errno;
348 goto fail;
351 return 0;
353 fail:
354 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
355 if (bmds->dirty_bitmap) {
356 bdrv_release_dirty_bitmap(blk_bs(bmds->blk), bmds->dirty_bitmap);
359 return ret;
362 /* Called with iothread lock taken. */
364 static void unset_dirty_tracking(void)
366 BlkMigDevState *bmds;
368 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
369 bdrv_release_dirty_bitmap(blk_bs(bmds->blk), bmds->dirty_bitmap);
373 static int init_blk_migration(QEMUFile *f)
375 BlockDriverState *bs;
376 BlkMigDevState *bmds;
377 int64_t sectors;
378 BdrvNextIterator it;
379 int i, num_bs = 0;
380 struct {
381 BlkMigDevState *bmds;
382 BlockDriverState *bs;
383 } *bmds_bs;
384 Error *local_err = NULL;
385 int ret;
387 block_mig_state.submitted = 0;
388 block_mig_state.read_done = 0;
389 block_mig_state.transferred = 0;
390 block_mig_state.total_sector_sum = 0;
391 block_mig_state.prev_progress = -1;
392 block_mig_state.bulk_completed = 0;
393 block_mig_state.zero_blocks = migrate_zero_blocks();
395 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
396 num_bs++;
398 bmds_bs = g_malloc0(num_bs * sizeof(*bmds_bs));
400 for (i = 0, bs = bdrv_first(&it); bs; bs = bdrv_next(&it), i++) {
401 if (bdrv_is_read_only(bs)) {
402 continue;
405 sectors = bdrv_nb_sectors(bs);
406 if (sectors <= 0) {
407 ret = sectors;
408 goto out;
411 bmds = g_new0(BlkMigDevState, 1);
412 bmds->blk = blk_new(BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
413 bmds->blk_name = g_strdup(bdrv_get_device_name(bs));
414 bmds->bulk_completed = 0;
415 bmds->total_sectors = sectors;
416 bmds->completed_sectors = 0;
417 bmds->shared_base = migrate_use_block_incremental();
419 assert(i < num_bs);
420 bmds_bs[i].bmds = bmds;
421 bmds_bs[i].bs = bs;
423 block_mig_state.total_sector_sum += sectors;
425 if (bmds->shared_base) {
426 DPRINTF("Start migration for %s with shared base image\n",
427 bdrv_get_device_name(bs));
428 } else {
429 DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs));
432 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
435 /* Can only insert new BDSes now because doing so while iterating block
436 * devices may end up in a deadlock (iterating the new BDSes, too). */
437 for (i = 0; i < num_bs; i++) {
438 BlkMigDevState *bmds = bmds_bs[i].bmds;
439 BlockDriverState *bs = bmds_bs[i].bs;
441 if (bmds) {
442 ret = blk_insert_bs(bmds->blk, bs, &local_err);
443 if (ret < 0) {
444 error_report_err(local_err);
445 goto out;
448 alloc_aio_bitmap(bmds);
449 error_setg(&bmds->blocker, "block device is in use by migration");
450 bdrv_op_block_all(bs, bmds->blocker);
454 ret = 0;
455 out:
456 g_free(bmds_bs);
457 return ret;
460 /* Called with no lock taken. */
462 static int blk_mig_save_bulked_block(QEMUFile *f)
464 int64_t completed_sector_sum = 0;
465 BlkMigDevState *bmds;
466 int progress;
467 int ret = 0;
469 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
470 if (bmds->bulk_completed == 0) {
471 if (mig_save_device_bulk(f, bmds) == 1) {
472 /* completed bulk section for this device */
473 bmds->bulk_completed = 1;
475 completed_sector_sum += bmds->completed_sectors;
476 ret = 1;
477 break;
478 } else {
479 completed_sector_sum += bmds->completed_sectors;
483 if (block_mig_state.total_sector_sum != 0) {
484 progress = completed_sector_sum * 100 /
485 block_mig_state.total_sector_sum;
486 } else {
487 progress = 100;
489 if (progress != block_mig_state.prev_progress) {
490 block_mig_state.prev_progress = progress;
491 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
492 | BLK_MIG_FLAG_PROGRESS);
493 DPRINTF("Completed %d %%\r", progress);
496 return ret;
499 static void blk_mig_reset_dirty_cursor(void)
501 BlkMigDevState *bmds;
503 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
504 bmds->cur_dirty = 0;
508 /* Called with iothread lock and AioContext taken. */
510 static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
511 int is_async)
513 BlkMigBlock *blk;
514 BlockDriverState *bs = blk_bs(bmds->blk);
515 int64_t total_sectors = bmds->total_sectors;
516 int64_t sector;
517 int nr_sectors;
518 int ret = -EIO;
520 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
521 blk_mig_lock();
522 if (bmds_aio_inflight(bmds, sector)) {
523 blk_mig_unlock();
524 blk_drain(bmds->blk);
525 } else {
526 blk_mig_unlock();
528 bdrv_dirty_bitmap_lock(bmds->dirty_bitmap);
529 if (bdrv_get_dirty_locked(bs, bmds->dirty_bitmap, sector)) {
530 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
531 nr_sectors = total_sectors - sector;
532 } else {
533 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
535 bdrv_reset_dirty_bitmap_locked(bmds->dirty_bitmap, sector, nr_sectors);
536 bdrv_dirty_bitmap_unlock(bmds->dirty_bitmap);
538 blk = g_new(BlkMigBlock, 1);
539 blk->buf = g_malloc(BLOCK_SIZE);
540 blk->bmds = bmds;
541 blk->sector = sector;
542 blk->nr_sectors = nr_sectors;
544 if (is_async) {
545 blk->iov.iov_base = blk->buf;
546 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
547 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
549 blk->aiocb = blk_aio_preadv(bmds->blk,
550 sector * BDRV_SECTOR_SIZE,
551 &blk->qiov, 0, blk_mig_read_cb,
552 blk);
554 blk_mig_lock();
555 block_mig_state.submitted++;
556 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
557 blk_mig_unlock();
558 } else {
559 ret = blk_pread(bmds->blk, sector * BDRV_SECTOR_SIZE, blk->buf,
560 nr_sectors * BDRV_SECTOR_SIZE);
561 if (ret < 0) {
562 goto error;
564 blk_send(f, blk);
566 g_free(blk->buf);
567 g_free(blk);
570 sector += nr_sectors;
571 bmds->cur_dirty = sector;
572 break;
575 bdrv_dirty_bitmap_unlock(bmds->dirty_bitmap);
576 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
577 bmds->cur_dirty = sector;
580 return (bmds->cur_dirty >= bmds->total_sectors);
582 error:
583 DPRINTF("Error reading sector %" PRId64 "\n", sector);
584 g_free(blk->buf);
585 g_free(blk);
586 return ret;
589 /* Called with iothread lock taken.
591 * return value:
592 * 0: too much data for max_downtime
593 * 1: few enough data for max_downtime
595 static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
597 BlkMigDevState *bmds;
598 int ret = 1;
600 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
601 aio_context_acquire(blk_get_aio_context(bmds->blk));
602 ret = mig_save_device_dirty(f, bmds, is_async);
603 aio_context_release(blk_get_aio_context(bmds->blk));
604 if (ret <= 0) {
605 break;
609 return ret;
612 /* Called with no locks taken. */
614 static int flush_blks(QEMUFile *f)
616 BlkMigBlock *blk;
617 int ret = 0;
619 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
620 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
621 block_mig_state.transferred);
623 blk_mig_lock();
624 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
625 if (qemu_file_rate_limit(f)) {
626 break;
628 if (blk->ret < 0) {
629 ret = blk->ret;
630 break;
633 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
634 blk_mig_unlock();
635 blk_send(f, blk);
636 blk_mig_lock();
638 g_free(blk->buf);
639 g_free(blk);
641 block_mig_state.read_done--;
642 block_mig_state.transferred++;
643 assert(block_mig_state.read_done >= 0);
645 blk_mig_unlock();
647 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
648 block_mig_state.submitted, block_mig_state.read_done,
649 block_mig_state.transferred);
650 return ret;
653 /* Called with iothread lock taken. */
655 static int64_t get_remaining_dirty(void)
657 BlkMigDevState *bmds;
658 int64_t dirty = 0;
660 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
661 aio_context_acquire(blk_get_aio_context(bmds->blk));
662 dirty += bdrv_get_dirty_count(bmds->dirty_bitmap);
663 aio_context_release(blk_get_aio_context(bmds->blk));
666 return dirty << BDRV_SECTOR_BITS;
671 /* Called with iothread lock taken. */
672 static void block_migration_cleanup_bmds(void)
674 BlkMigDevState *bmds;
675 AioContext *ctx;
677 unset_dirty_tracking();
679 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
680 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
681 bdrv_op_unblock_all(blk_bs(bmds->blk), bmds->blocker);
682 error_free(bmds->blocker);
684 /* Save ctx, because bmds->blk can disappear during blk_unref. */
685 ctx = blk_get_aio_context(bmds->blk);
686 aio_context_acquire(ctx);
687 blk_unref(bmds->blk);
688 aio_context_release(ctx);
690 g_free(bmds->blk_name);
691 g_free(bmds->aio_bitmap);
692 g_free(bmds);
696 /* Called with iothread lock taken. */
697 static void block_migration_cleanup(void *opaque)
699 BlkMigBlock *blk;
701 bdrv_drain_all();
703 block_migration_cleanup_bmds();
705 blk_mig_lock();
706 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
707 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
708 g_free(blk->buf);
709 g_free(blk);
711 blk_mig_unlock();
714 static int block_save_setup(QEMUFile *f, void *opaque)
716 int ret;
718 DPRINTF("Enter save live setup submitted %d transferred %d\n",
719 block_mig_state.submitted, block_mig_state.transferred);
721 qemu_mutex_lock_iothread();
722 ret = init_blk_migration(f);
723 if (ret < 0) {
724 qemu_mutex_unlock_iothread();
725 return ret;
728 /* start track dirty blocks */
729 ret = set_dirty_tracking();
731 qemu_mutex_unlock_iothread();
733 if (ret) {
734 return ret;
737 ret = flush_blks(f);
738 blk_mig_reset_dirty_cursor();
739 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
741 return ret;
744 static int block_save_iterate(QEMUFile *f, void *opaque)
746 int ret;
747 int64_t last_ftell = qemu_ftell(f);
748 int64_t delta_ftell;
750 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
751 block_mig_state.submitted, block_mig_state.transferred);
753 ret = flush_blks(f);
754 if (ret) {
755 return ret;
758 blk_mig_reset_dirty_cursor();
760 /* control the rate of transfer */
761 blk_mig_lock();
762 while ((block_mig_state.submitted +
763 block_mig_state.read_done) * BLOCK_SIZE <
764 qemu_file_get_rate_limit(f) &&
765 (block_mig_state.submitted +
766 block_mig_state.read_done) <
767 MAX_INFLIGHT_IO) {
768 blk_mig_unlock();
769 if (block_mig_state.bulk_completed == 0) {
770 /* first finish the bulk phase */
771 if (blk_mig_save_bulked_block(f) == 0) {
772 /* finished saving bulk on all devices */
773 block_mig_state.bulk_completed = 1;
775 ret = 0;
776 } else {
777 /* Always called with iothread lock taken for
778 * simplicity, block_save_complete also calls it.
780 qemu_mutex_lock_iothread();
781 ret = blk_mig_save_dirty_block(f, 1);
782 qemu_mutex_unlock_iothread();
784 if (ret < 0) {
785 return ret;
787 blk_mig_lock();
788 if (ret != 0) {
789 /* no more dirty blocks */
790 break;
793 blk_mig_unlock();
795 ret = flush_blks(f);
796 if (ret) {
797 return ret;
800 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
801 delta_ftell = qemu_ftell(f) - last_ftell;
802 if (delta_ftell > 0) {
803 return 1;
804 } else if (delta_ftell < 0) {
805 return -1;
806 } else {
807 return 0;
811 /* Called with iothread lock taken. */
813 static int block_save_complete(QEMUFile *f, void *opaque)
815 int ret;
817 DPRINTF("Enter save live complete submitted %d transferred %d\n",
818 block_mig_state.submitted, block_mig_state.transferred);
820 ret = flush_blks(f);
821 if (ret) {
822 return ret;
825 blk_mig_reset_dirty_cursor();
827 /* we know for sure that save bulk is completed and
828 all async read completed */
829 blk_mig_lock();
830 assert(block_mig_state.submitted == 0);
831 blk_mig_unlock();
833 do {
834 ret = blk_mig_save_dirty_block(f, 0);
835 if (ret < 0) {
836 return ret;
838 } while (ret == 0);
840 /* report completion */
841 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
843 DPRINTF("Block migration completed\n");
845 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
847 /* Make sure that our BlockBackends are gone, so that the block driver
848 * nodes can be inactivated. */
849 block_migration_cleanup_bmds();
851 return 0;
854 static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
855 uint64_t *non_postcopiable_pending,
856 uint64_t *postcopiable_pending)
858 /* Estimate pending number of bytes to send */
859 uint64_t pending;
861 qemu_mutex_lock_iothread();
862 pending = get_remaining_dirty();
863 qemu_mutex_unlock_iothread();
865 blk_mig_lock();
866 pending += block_mig_state.submitted * BLOCK_SIZE +
867 block_mig_state.read_done * BLOCK_SIZE;
868 blk_mig_unlock();
870 /* Report at least one block pending during bulk phase */
871 if (pending <= max_size && !block_mig_state.bulk_completed) {
872 pending = max_size + BLOCK_SIZE;
875 DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
876 /* We don't do postcopy */
877 *non_postcopiable_pending += pending;
880 static int block_load(QEMUFile *f, void *opaque, int version_id)
882 static int banner_printed;
883 int len, flags;
884 char device_name[256];
885 int64_t addr;
886 BlockBackend *blk, *blk_prev = NULL;;
887 Error *local_err = NULL;
888 uint8_t *buf;
889 int64_t total_sectors = 0;
890 int nr_sectors;
891 int ret;
892 BlockDriverInfo bdi;
893 int cluster_size = BLOCK_SIZE;
895 do {
896 addr = qemu_get_be64(f);
898 flags = addr & ~BDRV_SECTOR_MASK;
899 addr >>= BDRV_SECTOR_BITS;
901 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
902 /* get device name */
903 len = qemu_get_byte(f);
904 qemu_get_buffer(f, (uint8_t *)device_name, len);
905 device_name[len] = '\0';
907 blk = blk_by_name(device_name);
908 if (!blk) {
909 fprintf(stderr, "Error unknown block device %s\n",
910 device_name);
911 return -EINVAL;
914 if (blk != blk_prev) {
915 blk_prev = blk;
916 total_sectors = blk_nb_sectors(blk);
917 if (total_sectors <= 0) {
918 error_report("Error getting length of block device %s",
919 device_name);
920 return -EINVAL;
923 blk_invalidate_cache(blk, &local_err);
924 if (local_err) {
925 error_report_err(local_err);
926 return -EINVAL;
929 ret = bdrv_get_info(blk_bs(blk), &bdi);
930 if (ret == 0 && bdi.cluster_size > 0 &&
931 bdi.cluster_size <= BLOCK_SIZE &&
932 BLOCK_SIZE % bdi.cluster_size == 0) {
933 cluster_size = bdi.cluster_size;
934 } else {
935 cluster_size = BLOCK_SIZE;
939 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
940 nr_sectors = total_sectors - addr;
941 } else {
942 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
945 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
946 ret = blk_pwrite_zeroes(blk, addr * BDRV_SECTOR_SIZE,
947 nr_sectors * BDRV_SECTOR_SIZE,
948 BDRV_REQ_MAY_UNMAP);
949 } else {
950 int i;
951 int64_t cur_addr;
952 uint8_t *cur_buf;
954 buf = g_malloc(BLOCK_SIZE);
955 qemu_get_buffer(f, buf, BLOCK_SIZE);
956 for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
957 cur_addr = addr * BDRV_SECTOR_SIZE + i * cluster_size;
958 cur_buf = buf + i * cluster_size;
960 if ((!block_mig_state.zero_blocks ||
961 cluster_size < BLOCK_SIZE) &&
962 buffer_is_zero(cur_buf, cluster_size)) {
963 ret = blk_pwrite_zeroes(blk, cur_addr,
964 cluster_size,
965 BDRV_REQ_MAY_UNMAP);
966 } else {
967 ret = blk_pwrite(blk, cur_addr, cur_buf,
968 cluster_size, 0);
970 if (ret < 0) {
971 break;
974 g_free(buf);
977 if (ret < 0) {
978 return ret;
980 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
981 if (!banner_printed) {
982 printf("Receiving block device images\n");
983 banner_printed = 1;
985 printf("Completed %d %%%c", (int)addr,
986 (addr == 100) ? '\n' : '\r');
987 fflush(stdout);
988 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
989 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
990 return -EINVAL;
992 ret = qemu_file_get_error(f);
993 if (ret != 0) {
994 return ret;
996 } while (!(flags & BLK_MIG_FLAG_EOS));
998 return 0;
1001 static bool block_is_active(void *opaque)
1003 return migrate_use_block();
1006 static SaveVMHandlers savevm_block_handlers = {
1007 .save_live_setup = block_save_setup,
1008 .save_live_iterate = block_save_iterate,
1009 .save_live_complete_precopy = block_save_complete,
1010 .save_live_pending = block_save_pending,
1011 .load_state = block_load,
1012 .cleanup = block_migration_cleanup,
1013 .is_active = block_is_active,
1016 void blk_mig_init(void)
1018 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
1019 QSIMPLEQ_INIT(&block_mig_state.blk_list);
1020 qemu_mutex_init(&block_mig_state.lock);
1022 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
1023 &block_mig_state);