hw/arm/virt-acpi-build: build SLIT when needed
[qemu/ar7.git] / migration / block.c
blob13f90d3f1773233fb465b7f2857694626e5b0648
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-common.h"
19 #include "block/block.h"
20 #include "qemu/error-report.h"
21 #include "qemu/main-loop.h"
22 #include "hw/hw.h"
23 #include "qemu/cutils.h"
24 #include "qemu/queue.h"
25 #include "qemu/timer.h"
26 #include "migration/block.h"
27 #include "migration/migration.h"
28 #include "sysemu/blockdev.h"
29 #include "migration/qemu-file.h"
30 #include "migration/vmstate.h"
31 #include "sysemu/block-backend.h"
33 #define BLOCK_SIZE (1 << 20)
34 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
36 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
37 #define BLK_MIG_FLAG_EOS 0x02
38 #define BLK_MIG_FLAG_PROGRESS 0x04
39 #define BLK_MIG_FLAG_ZERO_BLOCK 0x08
41 #define MAX_IS_ALLOCATED_SEARCH 65536
43 #define MAX_INFLIGHT_IO 512
45 //#define DEBUG_BLK_MIGRATION
47 #ifdef DEBUG_BLK_MIGRATION
48 #define DPRINTF(fmt, ...) \
49 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
50 #else
51 #define DPRINTF(fmt, ...) \
52 do { } while (0)
53 #endif
55 typedef struct BlkMigDevState {
56 /* Written during setup phase. Can be read without a lock. */
57 BlockBackend *blk;
58 char *blk_name;
59 int shared_base;
60 int64_t total_sectors;
61 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
62 Error *blocker;
64 /* Only used by migration thread. Does not need a lock. */
65 int bulk_completed;
66 int64_t cur_sector;
67 int64_t cur_dirty;
69 /* Data in the aio_bitmap is protected by block migration lock.
70 * Allocation and free happen during setup and cleanup respectively.
72 unsigned long *aio_bitmap;
74 /* Protected by block migration lock. */
75 int64_t completed_sectors;
77 /* During migration this is protected by iothread lock / AioContext.
78 * Allocation and free happen during setup and cleanup respectively.
80 BdrvDirtyBitmap *dirty_bitmap;
81 } BlkMigDevState;
83 typedef struct BlkMigBlock {
84 /* Only used by migration thread. */
85 uint8_t *buf;
86 BlkMigDevState *bmds;
87 int64_t sector;
88 int nr_sectors;
89 struct iovec iov;
90 QEMUIOVector qiov;
91 BlockAIOCB *aiocb;
93 /* Protected by block migration lock. */
94 int ret;
95 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
96 } BlkMigBlock;
98 typedef struct BlkMigState {
99 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
100 int64_t total_sector_sum;
101 bool zero_blocks;
103 /* Protected by lock. */
104 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
105 int submitted;
106 int read_done;
108 /* Only used by migration thread. Does not need a lock. */
109 int transferred;
110 int prev_progress;
111 int bulk_completed;
113 /* Lock must be taken _inside_ the iothread lock and any AioContexts. */
114 QemuMutex lock;
115 } BlkMigState;
117 static BlkMigState block_mig_state;
119 static void blk_mig_lock(void)
121 qemu_mutex_lock(&block_mig_state.lock);
124 static void blk_mig_unlock(void)
126 qemu_mutex_unlock(&block_mig_state.lock);
129 /* Must run outside of the iothread lock during the bulk phase,
130 * or the VM will stall.
133 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
135 int len;
136 uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK;
138 if (block_mig_state.zero_blocks &&
139 buffer_is_zero(blk->buf, BLOCK_SIZE)) {
140 flags |= BLK_MIG_FLAG_ZERO_BLOCK;
143 /* sector number and flags */
144 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
145 | flags);
147 /* device name */
148 len = strlen(blk->bmds->blk_name);
149 qemu_put_byte(f, len);
150 qemu_put_buffer(f, (uint8_t *) blk->bmds->blk_name, len);
152 /* if a block is zero we need to flush here since the network
153 * bandwidth is now a lot higher than the storage device bandwidth.
154 * thus if we queue zero blocks we slow down the migration */
155 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
156 qemu_fflush(f);
157 return;
160 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
163 int blk_mig_active(void)
165 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
168 uint64_t blk_mig_bytes_transferred(void)
170 BlkMigDevState *bmds;
171 uint64_t sum = 0;
173 blk_mig_lock();
174 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
175 sum += bmds->completed_sectors;
177 blk_mig_unlock();
178 return sum << BDRV_SECTOR_BITS;
181 uint64_t blk_mig_bytes_remaining(void)
183 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
186 uint64_t blk_mig_bytes_total(void)
188 BlkMigDevState *bmds;
189 uint64_t sum = 0;
191 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
192 sum += bmds->total_sectors;
194 return sum << BDRV_SECTOR_BITS;
198 /* Called with migration lock held. */
200 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
202 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
204 if (sector < blk_nb_sectors(bmds->blk)) {
205 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
206 (1UL << (chunk % (sizeof(unsigned long) * 8))));
207 } else {
208 return 0;
212 /* Called with migration lock held. */
214 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
215 int nb_sectors, int set)
217 int64_t start, end;
218 unsigned long val, idx, bit;
220 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
221 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
223 for (; start <= end; start++) {
224 idx = start / (sizeof(unsigned long) * 8);
225 bit = start % (sizeof(unsigned long) * 8);
226 val = bmds->aio_bitmap[idx];
227 if (set) {
228 val |= 1UL << bit;
229 } else {
230 val &= ~(1UL << bit);
232 bmds->aio_bitmap[idx] = val;
236 static void alloc_aio_bitmap(BlkMigDevState *bmds)
238 BlockBackend *bb = bmds->blk;
239 int64_t bitmap_size;
241 bitmap_size = blk_nb_sectors(bb) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
242 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
244 bmds->aio_bitmap = g_malloc0(bitmap_size);
247 /* Never hold migration lock when yielding to the main loop! */
249 static void blk_mig_read_cb(void *opaque, int ret)
251 BlkMigBlock *blk = opaque;
253 blk_mig_lock();
254 blk->ret = ret;
256 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
257 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
259 block_mig_state.submitted--;
260 block_mig_state.read_done++;
261 assert(block_mig_state.submitted >= 0);
262 blk_mig_unlock();
265 /* Called with no lock taken. */
267 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
269 int64_t total_sectors = bmds->total_sectors;
270 int64_t cur_sector = bmds->cur_sector;
271 BlockBackend *bb = bmds->blk;
272 BlkMigBlock *blk;
273 int nr_sectors;
275 if (bmds->shared_base) {
276 qemu_mutex_lock_iothread();
277 aio_context_acquire(blk_get_aio_context(bb));
278 /* Skip unallocated sectors; intentionally treats failure as
279 * an allocated sector */
280 while (cur_sector < total_sectors &&
281 !bdrv_is_allocated(blk_bs(bb), cur_sector,
282 MAX_IS_ALLOCATED_SEARCH, &nr_sectors)) {
283 cur_sector += nr_sectors;
285 aio_context_release(blk_get_aio_context(bb));
286 qemu_mutex_unlock_iothread();
289 if (cur_sector >= total_sectors) {
290 bmds->cur_sector = bmds->completed_sectors = total_sectors;
291 return 1;
294 bmds->completed_sectors = cur_sector;
296 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
298 /* we are going to transfer a full block even if it is not allocated */
299 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
301 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
302 nr_sectors = total_sectors - cur_sector;
305 blk = g_new(BlkMigBlock, 1);
306 blk->buf = g_malloc(BLOCK_SIZE);
307 blk->bmds = bmds;
308 blk->sector = cur_sector;
309 blk->nr_sectors = nr_sectors;
311 blk->iov.iov_base = blk->buf;
312 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
313 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
315 blk_mig_lock();
316 block_mig_state.submitted++;
317 blk_mig_unlock();
319 /* We do not know if bs is under the main thread (and thus does
320 * not acquire the AioContext when doing AIO) or rather under
321 * dataplane. Thus acquire both the iothread mutex and the
322 * AioContext.
324 * This is ugly and will disappear when we make bdrv_* thread-safe,
325 * without the need to acquire the AioContext.
327 qemu_mutex_lock_iothread();
328 aio_context_acquire(blk_get_aio_context(bmds->blk));
329 blk->aiocb = blk_aio_preadv(bb, cur_sector * BDRV_SECTOR_SIZE, &blk->qiov,
330 0, blk_mig_read_cb, blk);
332 bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector, nr_sectors);
333 aio_context_release(blk_get_aio_context(bmds->blk));
334 qemu_mutex_unlock_iothread();
336 bmds->cur_sector = cur_sector + nr_sectors;
337 return (bmds->cur_sector >= total_sectors);
340 /* Called with iothread lock taken. */
342 static int set_dirty_tracking(void)
344 BlkMigDevState *bmds;
345 int ret;
347 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
348 aio_context_acquire(blk_get_aio_context(bmds->blk));
349 bmds->dirty_bitmap = bdrv_create_dirty_bitmap(blk_bs(bmds->blk),
350 BLOCK_SIZE, NULL, NULL);
351 aio_context_release(blk_get_aio_context(bmds->blk));
352 if (!bmds->dirty_bitmap) {
353 ret = -errno;
354 goto fail;
357 return 0;
359 fail:
360 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
361 if (bmds->dirty_bitmap) {
362 aio_context_acquire(blk_get_aio_context(bmds->blk));
363 bdrv_release_dirty_bitmap(blk_bs(bmds->blk), bmds->dirty_bitmap);
364 aio_context_release(blk_get_aio_context(bmds->blk));
367 return ret;
370 /* Called with iothread lock taken. */
372 static void unset_dirty_tracking(void)
374 BlkMigDevState *bmds;
376 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
377 aio_context_acquire(blk_get_aio_context(bmds->blk));
378 bdrv_release_dirty_bitmap(blk_bs(bmds->blk), bmds->dirty_bitmap);
379 aio_context_release(blk_get_aio_context(bmds->blk));
383 static int init_blk_migration(QEMUFile *f)
385 BlockDriverState *bs;
386 BlkMigDevState *bmds;
387 int64_t sectors;
388 BdrvNextIterator it;
389 int i, num_bs = 0;
390 struct {
391 BlkMigDevState *bmds;
392 BlockDriverState *bs;
393 } *bmds_bs;
394 Error *local_err = NULL;
395 int ret;
397 block_mig_state.submitted = 0;
398 block_mig_state.read_done = 0;
399 block_mig_state.transferred = 0;
400 block_mig_state.total_sector_sum = 0;
401 block_mig_state.prev_progress = -1;
402 block_mig_state.bulk_completed = 0;
403 block_mig_state.zero_blocks = migrate_zero_blocks();
405 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
406 num_bs++;
408 bmds_bs = g_malloc0(num_bs * sizeof(*bmds_bs));
410 for (i = 0, bs = bdrv_first(&it); bs; bs = bdrv_next(&it), i++) {
411 if (bdrv_is_read_only(bs)) {
412 continue;
415 sectors = bdrv_nb_sectors(bs);
416 if (sectors <= 0) {
417 ret = sectors;
418 goto out;
421 bmds = g_new0(BlkMigDevState, 1);
422 bmds->blk = blk_new(BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
423 bmds->blk_name = g_strdup(bdrv_get_device_name(bs));
424 bmds->bulk_completed = 0;
425 bmds->total_sectors = sectors;
426 bmds->completed_sectors = 0;
427 bmds->shared_base = migrate_use_block_incremental();
429 assert(i < num_bs);
430 bmds_bs[i].bmds = bmds;
431 bmds_bs[i].bs = bs;
433 block_mig_state.total_sector_sum += sectors;
435 if (bmds->shared_base) {
436 DPRINTF("Start migration for %s with shared base image\n",
437 bdrv_get_device_name(bs));
438 } else {
439 DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs));
442 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
445 /* Can only insert new BDSes now because doing so while iterating block
446 * devices may end up in a deadlock (iterating the new BDSes, too). */
447 for (i = 0; i < num_bs; i++) {
448 BlkMigDevState *bmds = bmds_bs[i].bmds;
449 BlockDriverState *bs = bmds_bs[i].bs;
451 if (bmds) {
452 ret = blk_insert_bs(bmds->blk, bs, &local_err);
453 if (ret < 0) {
454 error_report_err(local_err);
455 goto out;
458 alloc_aio_bitmap(bmds);
459 error_setg(&bmds->blocker, "block device is in use by migration");
460 bdrv_op_block_all(bs, bmds->blocker);
464 ret = 0;
465 out:
466 g_free(bmds_bs);
467 return ret;
470 /* Called with no lock taken. */
472 static int blk_mig_save_bulked_block(QEMUFile *f)
474 int64_t completed_sector_sum = 0;
475 BlkMigDevState *bmds;
476 int progress;
477 int ret = 0;
479 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
480 if (bmds->bulk_completed == 0) {
481 if (mig_save_device_bulk(f, bmds) == 1) {
482 /* completed bulk section for this device */
483 bmds->bulk_completed = 1;
485 completed_sector_sum += bmds->completed_sectors;
486 ret = 1;
487 break;
488 } else {
489 completed_sector_sum += bmds->completed_sectors;
493 if (block_mig_state.total_sector_sum != 0) {
494 progress = completed_sector_sum * 100 /
495 block_mig_state.total_sector_sum;
496 } else {
497 progress = 100;
499 if (progress != block_mig_state.prev_progress) {
500 block_mig_state.prev_progress = progress;
501 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
502 | BLK_MIG_FLAG_PROGRESS);
503 DPRINTF("Completed %d %%\r", progress);
506 return ret;
509 static void blk_mig_reset_dirty_cursor(void)
511 BlkMigDevState *bmds;
513 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
514 bmds->cur_dirty = 0;
518 /* Called with iothread lock and AioContext taken. */
520 static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
521 int is_async)
523 BlkMigBlock *blk;
524 BlockDriverState *bs = blk_bs(bmds->blk);
525 int64_t total_sectors = bmds->total_sectors;
526 int64_t sector;
527 int nr_sectors;
528 int ret = -EIO;
530 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
531 blk_mig_lock();
532 if (bmds_aio_inflight(bmds, sector)) {
533 blk_mig_unlock();
534 blk_drain(bmds->blk);
535 } else {
536 blk_mig_unlock();
538 if (bdrv_get_dirty(bs, bmds->dirty_bitmap, sector)) {
540 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
541 nr_sectors = total_sectors - sector;
542 } else {
543 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
545 blk = g_new(BlkMigBlock, 1);
546 blk->buf = g_malloc(BLOCK_SIZE);
547 blk->bmds = bmds;
548 blk->sector = sector;
549 blk->nr_sectors = nr_sectors;
551 if (is_async) {
552 blk->iov.iov_base = blk->buf;
553 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
554 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
556 blk->aiocb = blk_aio_preadv(bmds->blk,
557 sector * BDRV_SECTOR_SIZE,
558 &blk->qiov, 0, blk_mig_read_cb,
559 blk);
561 blk_mig_lock();
562 block_mig_state.submitted++;
563 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
564 blk_mig_unlock();
565 } else {
566 ret = blk_pread(bmds->blk, sector * BDRV_SECTOR_SIZE, blk->buf,
567 nr_sectors * BDRV_SECTOR_SIZE);
568 if (ret < 0) {
569 goto error;
571 blk_send(f, blk);
573 g_free(blk->buf);
574 g_free(blk);
577 bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, sector, nr_sectors);
578 sector += nr_sectors;
579 bmds->cur_dirty = sector;
581 break;
583 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
584 bmds->cur_dirty = sector;
587 return (bmds->cur_dirty >= bmds->total_sectors);
589 error:
590 DPRINTF("Error reading sector %" PRId64 "\n", sector);
591 g_free(blk->buf);
592 g_free(blk);
593 return ret;
596 /* Called with iothread lock taken.
598 * return value:
599 * 0: too much data for max_downtime
600 * 1: few enough data for max_downtime
602 static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
604 BlkMigDevState *bmds;
605 int ret = 1;
607 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
608 aio_context_acquire(blk_get_aio_context(bmds->blk));
609 ret = mig_save_device_dirty(f, bmds, is_async);
610 aio_context_release(blk_get_aio_context(bmds->blk));
611 if (ret <= 0) {
612 break;
616 return ret;
619 /* Called with no locks taken. */
621 static int flush_blks(QEMUFile *f)
623 BlkMigBlock *blk;
624 int ret = 0;
626 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
627 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
628 block_mig_state.transferred);
630 blk_mig_lock();
631 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
632 if (qemu_file_rate_limit(f)) {
633 break;
635 if (blk->ret < 0) {
636 ret = blk->ret;
637 break;
640 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
641 blk_mig_unlock();
642 blk_send(f, blk);
643 blk_mig_lock();
645 g_free(blk->buf);
646 g_free(blk);
648 block_mig_state.read_done--;
649 block_mig_state.transferred++;
650 assert(block_mig_state.read_done >= 0);
652 blk_mig_unlock();
654 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
655 block_mig_state.submitted, block_mig_state.read_done,
656 block_mig_state.transferred);
657 return ret;
660 /* Called with iothread lock taken. */
662 static int64_t get_remaining_dirty(void)
664 BlkMigDevState *bmds;
665 int64_t dirty = 0;
667 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
668 aio_context_acquire(blk_get_aio_context(bmds->blk));
669 dirty += bdrv_get_dirty_count(bmds->dirty_bitmap);
670 aio_context_release(blk_get_aio_context(bmds->blk));
673 return dirty << BDRV_SECTOR_BITS;
676 /* Called with iothread lock taken. */
678 static void block_migration_cleanup(void *opaque)
680 BlkMigDevState *bmds;
681 BlkMigBlock *blk;
682 AioContext *ctx;
684 bdrv_drain_all();
686 unset_dirty_tracking();
688 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
689 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
690 bdrv_op_unblock_all(blk_bs(bmds->blk), bmds->blocker);
691 error_free(bmds->blocker);
693 /* Save ctx, because bmds->blk can disappear during blk_unref. */
694 ctx = blk_get_aio_context(bmds->blk);
695 aio_context_acquire(ctx);
696 blk_unref(bmds->blk);
697 aio_context_release(ctx);
699 g_free(bmds->blk_name);
700 g_free(bmds->aio_bitmap);
701 g_free(bmds);
704 blk_mig_lock();
705 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
706 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
707 g_free(blk->buf);
708 g_free(blk);
710 blk_mig_unlock();
713 static int block_save_setup(QEMUFile *f, void *opaque)
715 int ret;
717 DPRINTF("Enter save live setup submitted %d transferred %d\n",
718 block_mig_state.submitted, block_mig_state.transferred);
720 qemu_mutex_lock_iothread();
721 ret = init_blk_migration(f);
722 if (ret < 0) {
723 qemu_mutex_unlock_iothread();
724 return ret;
727 /* start track dirty blocks */
728 ret = set_dirty_tracking();
730 qemu_mutex_unlock_iothread();
732 if (ret) {
733 return ret;
736 ret = flush_blks(f);
737 blk_mig_reset_dirty_cursor();
738 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
740 return ret;
743 static int block_save_iterate(QEMUFile *f, void *opaque)
745 int ret;
746 int64_t last_ftell = qemu_ftell(f);
747 int64_t delta_ftell;
749 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
750 block_mig_state.submitted, block_mig_state.transferred);
752 ret = flush_blks(f);
753 if (ret) {
754 return ret;
757 blk_mig_reset_dirty_cursor();
759 /* control the rate of transfer */
760 blk_mig_lock();
761 while ((block_mig_state.submitted +
762 block_mig_state.read_done) * BLOCK_SIZE <
763 qemu_file_get_rate_limit(f) &&
764 (block_mig_state.submitted +
765 block_mig_state.read_done) <
766 MAX_INFLIGHT_IO) {
767 blk_mig_unlock();
768 if (block_mig_state.bulk_completed == 0) {
769 /* first finish the bulk phase */
770 if (blk_mig_save_bulked_block(f) == 0) {
771 /* finished saving bulk on all devices */
772 block_mig_state.bulk_completed = 1;
774 ret = 0;
775 } else {
776 /* Always called with iothread lock taken for
777 * simplicity, block_save_complete also calls it.
779 qemu_mutex_lock_iothread();
780 ret = blk_mig_save_dirty_block(f, 1);
781 qemu_mutex_unlock_iothread();
783 if (ret < 0) {
784 return ret;
786 blk_mig_lock();
787 if (ret != 0) {
788 /* no more dirty blocks */
789 break;
792 blk_mig_unlock();
794 ret = flush_blks(f);
795 if (ret) {
796 return ret;
799 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
800 delta_ftell = qemu_ftell(f) - last_ftell;
801 if (delta_ftell > 0) {
802 return 1;
803 } else if (delta_ftell < 0) {
804 return -1;
805 } else {
806 return 0;
810 /* Called with iothread lock taken. */
812 static int block_save_complete(QEMUFile *f, void *opaque)
814 int ret;
816 DPRINTF("Enter save live complete submitted %d transferred %d\n",
817 block_mig_state.submitted, block_mig_state.transferred);
819 ret = flush_blks(f);
820 if (ret) {
821 return ret;
824 blk_mig_reset_dirty_cursor();
826 /* we know for sure that save bulk is completed and
827 all async read completed */
828 blk_mig_lock();
829 assert(block_mig_state.submitted == 0);
830 blk_mig_unlock();
832 do {
833 ret = blk_mig_save_dirty_block(f, 0);
834 if (ret < 0) {
835 return ret;
837 } while (ret == 0);
839 /* report completion */
840 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
842 DPRINTF("Block migration completed\n");
844 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
846 return 0;
849 static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
850 uint64_t *non_postcopiable_pending,
851 uint64_t *postcopiable_pending)
853 /* Estimate pending number of bytes to send */
854 uint64_t pending;
856 qemu_mutex_lock_iothread();
857 pending = get_remaining_dirty();
858 qemu_mutex_unlock_iothread();
860 blk_mig_lock();
861 pending += block_mig_state.submitted * BLOCK_SIZE +
862 block_mig_state.read_done * BLOCK_SIZE;
863 blk_mig_unlock();
865 /* Report at least one block pending during bulk phase */
866 if (pending <= max_size && !block_mig_state.bulk_completed) {
867 pending = max_size + BLOCK_SIZE;
870 DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
871 /* We don't do postcopy */
872 *non_postcopiable_pending += pending;
875 static int block_load(QEMUFile *f, void *opaque, int version_id)
877 static int banner_printed;
878 int len, flags;
879 char device_name[256];
880 int64_t addr;
881 BlockBackend *blk, *blk_prev = NULL;;
882 Error *local_err = NULL;
883 uint8_t *buf;
884 int64_t total_sectors = 0;
885 int nr_sectors;
886 int ret;
887 BlockDriverInfo bdi;
888 int cluster_size = BLOCK_SIZE;
890 do {
891 addr = qemu_get_be64(f);
893 flags = addr & ~BDRV_SECTOR_MASK;
894 addr >>= BDRV_SECTOR_BITS;
896 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
897 /* get device name */
898 len = qemu_get_byte(f);
899 qemu_get_buffer(f, (uint8_t *)device_name, len);
900 device_name[len] = '\0';
902 blk = blk_by_name(device_name);
903 if (!blk) {
904 fprintf(stderr, "Error unknown block device %s\n",
905 device_name);
906 return -EINVAL;
909 if (blk != blk_prev) {
910 blk_prev = blk;
911 total_sectors = blk_nb_sectors(blk);
912 if (total_sectors <= 0) {
913 error_report("Error getting length of block device %s",
914 device_name);
915 return -EINVAL;
918 blk_invalidate_cache(blk, &local_err);
919 if (local_err) {
920 error_report_err(local_err);
921 return -EINVAL;
924 ret = bdrv_get_info(blk_bs(blk), &bdi);
925 if (ret == 0 && bdi.cluster_size > 0 &&
926 bdi.cluster_size <= BLOCK_SIZE &&
927 BLOCK_SIZE % bdi.cluster_size == 0) {
928 cluster_size = bdi.cluster_size;
929 } else {
930 cluster_size = BLOCK_SIZE;
934 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
935 nr_sectors = total_sectors - addr;
936 } else {
937 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
940 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
941 ret = blk_pwrite_zeroes(blk, addr * BDRV_SECTOR_SIZE,
942 nr_sectors * BDRV_SECTOR_SIZE,
943 BDRV_REQ_MAY_UNMAP);
944 } else {
945 int i;
946 int64_t cur_addr;
947 uint8_t *cur_buf;
949 buf = g_malloc(BLOCK_SIZE);
950 qemu_get_buffer(f, buf, BLOCK_SIZE);
951 for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
952 cur_addr = addr * BDRV_SECTOR_SIZE + i * cluster_size;
953 cur_buf = buf + i * cluster_size;
955 if ((!block_mig_state.zero_blocks ||
956 cluster_size < BLOCK_SIZE) &&
957 buffer_is_zero(cur_buf, cluster_size)) {
958 ret = blk_pwrite_zeroes(blk, cur_addr,
959 cluster_size,
960 BDRV_REQ_MAY_UNMAP);
961 } else {
962 ret = blk_pwrite(blk, cur_addr, cur_buf,
963 cluster_size, 0);
965 if (ret < 0) {
966 break;
969 g_free(buf);
972 if (ret < 0) {
973 return ret;
975 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
976 if (!banner_printed) {
977 printf("Receiving block device images\n");
978 banner_printed = 1;
980 printf("Completed %d %%%c", (int)addr,
981 (addr == 100) ? '\n' : '\r');
982 fflush(stdout);
983 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
984 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
985 return -EINVAL;
987 ret = qemu_file_get_error(f);
988 if (ret != 0) {
989 return ret;
991 } while (!(flags & BLK_MIG_FLAG_EOS));
993 return 0;
996 static bool block_is_active(void *opaque)
998 return migrate_use_block();
1001 static SaveVMHandlers savevm_block_handlers = {
1002 .save_live_setup = block_save_setup,
1003 .save_live_iterate = block_save_iterate,
1004 .save_live_complete_precopy = block_save_complete,
1005 .save_live_pending = block_save_pending,
1006 .load_state = block_load,
1007 .cleanup = block_migration_cleanup,
1008 .is_active = block_is_active,
1011 void blk_mig_init(void)
1013 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
1014 QSIMPLEQ_INIT(&block_mig_state.blk_list);
1015 qemu_mutex_init(&block_mig_state.lock);
1017 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
1018 &block_mig_state);