tcg: Optimize brcond2 and setcond2 ne/eq
[qemu.git] / block-migration.c
blob16562709c853e60d86b3977c947f4e81318a1dca
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-common.h"
17 #include "block/block_int.h"
18 #include "hw/hw.h"
19 #include "qemu/queue.h"
20 #include "qemu/timer.h"
21 #include "migration/block.h"
22 #include "migration/migration.h"
23 #include "sysemu/blockdev.h"
24 #include <assert.h>
26 #define BLOCK_SIZE (1 << 20)
27 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
29 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
30 #define BLK_MIG_FLAG_EOS 0x02
31 #define BLK_MIG_FLAG_PROGRESS 0x04
32 #define BLK_MIG_FLAG_ZERO_BLOCK 0x08
34 #define MAX_IS_ALLOCATED_SEARCH 65536
36 //#define DEBUG_BLK_MIGRATION
38 #ifdef DEBUG_BLK_MIGRATION
39 #define DPRINTF(fmt, ...) \
40 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
41 #else
42 #define DPRINTF(fmt, ...) \
43 do { } while (0)
44 #endif
46 typedef struct BlkMigDevState {
47 /* Written during setup phase. Can be read without a lock. */
48 BlockDriverState *bs;
49 int shared_base;
50 int64_t total_sectors;
51 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
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;
58 /* Protected by block migration lock. */
59 unsigned long *aio_bitmap;
60 int64_t completed_sectors;
61 BdrvDirtyBitmap *dirty_bitmap;
62 Error *blocker;
63 } BlkMigDevState;
65 typedef struct BlkMigBlock {
66 /* Only used by migration thread. */
67 uint8_t *buf;
68 BlkMigDevState *bmds;
69 int64_t sector;
70 int nr_sectors;
71 struct iovec iov;
72 QEMUIOVector qiov;
73 BlockDriverAIOCB *aiocb;
75 /* Protected by block migration lock. */
76 int ret;
77 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
78 } BlkMigBlock;
80 typedef struct BlkMigState {
81 /* Written during setup phase. Can be read without a lock. */
82 int blk_enable;
83 int shared_base;
84 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
85 int64_t total_sector_sum;
86 bool zero_blocks;
88 /* Protected by lock. */
89 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
90 int submitted;
91 int read_done;
93 /* Only used by migration thread. Does not need a lock. */
94 int transferred;
95 int prev_progress;
96 int bulk_completed;
98 /* Lock must be taken _inside_ the iothread lock. */
99 QemuMutex lock;
100 } BlkMigState;
102 static BlkMigState block_mig_state;
104 static void blk_mig_lock(void)
106 qemu_mutex_lock(&block_mig_state.lock);
109 static void blk_mig_unlock(void)
111 qemu_mutex_unlock(&block_mig_state.lock);
114 /* Must run outside of the iothread lock during the bulk phase,
115 * or the VM will stall.
118 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
120 int len;
121 uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK;
123 if (block_mig_state.zero_blocks &&
124 buffer_is_zero(blk->buf, BLOCK_SIZE)) {
125 flags |= BLK_MIG_FLAG_ZERO_BLOCK;
128 /* sector number and flags */
129 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
130 | flags);
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);
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;
145 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
148 int blk_mig_active(void)
150 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
153 uint64_t blk_mig_bytes_transferred(void)
155 BlkMigDevState *bmds;
156 uint64_t sum = 0;
158 blk_mig_lock();
159 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
160 sum += bmds->completed_sectors;
162 blk_mig_unlock();
163 return sum << BDRV_SECTOR_BITS;
166 uint64_t blk_mig_bytes_remaining(void)
168 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
171 uint64_t blk_mig_bytes_total(void)
173 BlkMigDevState *bmds;
174 uint64_t sum = 0;
176 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
177 sum += bmds->total_sectors;
179 return sum << BDRV_SECTOR_BITS;
183 /* Called with migration lock held. */
185 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
187 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
189 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
190 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
191 (1UL << (chunk % (sizeof(unsigned long) * 8))));
192 } else {
193 return 0;
197 /* Called with migration lock held. */
199 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
200 int nb_sectors, int set)
202 int64_t start, end;
203 unsigned long val, idx, bit;
205 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
206 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
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) {
213 val |= 1UL << bit;
214 } else {
215 val &= ~(1UL << bit);
217 bmds->aio_bitmap[idx] = val;
221 static void alloc_aio_bitmap(BlkMigDevState *bmds)
223 BlockDriverState *bs = bmds->bs;
224 int64_t bitmap_size;
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;
230 bmds->aio_bitmap = g_malloc0(bitmap_size);
233 /* Never hold migration lock when yielding to the main loop! */
235 static void blk_mig_read_cb(void *opaque, int ret)
237 BlkMigBlock *blk = opaque;
239 blk_mig_lock();
240 blk->ret = ret;
242 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
243 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
245 block_mig_state.submitted--;
246 block_mig_state.read_done++;
247 assert(block_mig_state.submitted >= 0);
248 blk_mig_unlock();
251 /* Called with no lock taken. */
253 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
255 int64_t total_sectors = bmds->total_sectors;
256 int64_t cur_sector = bmds->cur_sector;
257 BlockDriverState *bs = bmds->bs;
258 BlkMigBlock *blk;
259 int nr_sectors;
261 if (bmds->shared_base) {
262 qemu_mutex_lock_iothread();
263 while (cur_sector < total_sectors &&
264 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
265 &nr_sectors)) {
266 cur_sector += nr_sectors;
268 qemu_mutex_unlock_iothread();
271 if (cur_sector >= total_sectors) {
272 bmds->cur_sector = bmds->completed_sectors = total_sectors;
273 return 1;
276 bmds->completed_sectors = cur_sector;
278 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
280 /* we are going to transfer a full block even if it is not allocated */
281 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
283 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
284 nr_sectors = total_sectors - cur_sector;
287 blk = g_malloc(sizeof(BlkMigBlock));
288 blk->buf = g_malloc(BLOCK_SIZE);
289 blk->bmds = bmds;
290 blk->sector = cur_sector;
291 blk->nr_sectors = nr_sectors;
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);
297 blk_mig_lock();
298 block_mig_state.submitted++;
299 blk_mig_unlock();
301 qemu_mutex_lock_iothread();
302 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
303 nr_sectors, blk_mig_read_cb, blk);
305 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
306 qemu_mutex_unlock_iothread();
308 bmds->cur_sector = cur_sector + nr_sectors;
309 return (bmds->cur_sector >= total_sectors);
312 /* Called with iothread lock taken. */
314 static int set_dirty_tracking(void)
316 BlkMigDevState *bmds;
317 int ret;
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;
327 return 0;
329 fail:
330 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
331 if (bmds->dirty_bitmap) {
332 bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
335 return ret;
338 static void unset_dirty_tracking(void)
340 BlkMigDevState *bmds;
342 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
343 bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
347 static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
349 BlkMigDevState *bmds;
350 int64_t sectors;
352 if (!bdrv_is_read_only(bs)) {
353 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
354 if (sectors <= 0) {
355 return;
358 bmds = g_malloc0(sizeof(BlkMigDevState));
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;
364 alloc_aio_bitmap(bmds);
365 error_setg(&bmds->blocker, "block device is in use by migration");
366 bdrv_op_block_all(bs, bmds->blocker);
367 bdrv_ref(bs);
369 block_mig_state.total_sector_sum += sectors;
371 if (bmds->shared_base) {
372 DPRINTF("Start migration for %s with shared base image\n",
373 bs->device_name);
374 } else {
375 DPRINTF("Start full migration for %s\n", bs->device_name);
378 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
382 static void init_blk_migration(QEMUFile *f)
384 block_mig_state.submitted = 0;
385 block_mig_state.read_done = 0;
386 block_mig_state.transferred = 0;
387 block_mig_state.total_sector_sum = 0;
388 block_mig_state.prev_progress = -1;
389 block_mig_state.bulk_completed = 0;
390 block_mig_state.zero_blocks = migrate_zero_blocks();
392 bdrv_iterate(init_blk_migration_it, NULL);
395 /* Called with no lock taken. */
397 static int blk_mig_save_bulked_block(QEMUFile *f)
399 int64_t completed_sector_sum = 0;
400 BlkMigDevState *bmds;
401 int progress;
402 int ret = 0;
404 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
405 if (bmds->bulk_completed == 0) {
406 if (mig_save_device_bulk(f, bmds) == 1) {
407 /* completed bulk section for this device */
408 bmds->bulk_completed = 1;
410 completed_sector_sum += bmds->completed_sectors;
411 ret = 1;
412 break;
413 } else {
414 completed_sector_sum += bmds->completed_sectors;
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;
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);
428 DPRINTF("Completed %d %%\r", progress);
431 return ret;
434 static void blk_mig_reset_dirty_cursor(void)
436 BlkMigDevState *bmds;
438 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
439 bmds->cur_dirty = 0;
443 /* Called with iothread lock taken. */
445 static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
446 int is_async)
448 BlkMigBlock *blk;
449 int64_t total_sectors = bmds->total_sectors;
450 int64_t sector;
451 int nr_sectors;
452 int ret = -EIO;
454 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
455 blk_mig_lock();
456 if (bmds_aio_inflight(bmds, sector)) {
457 blk_mig_unlock();
458 bdrv_drain_all();
459 } else {
460 blk_mig_unlock();
462 if (bdrv_get_dirty(bmds->bs, bmds->dirty_bitmap, sector)) {
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;
469 blk = g_malloc(sizeof(BlkMigBlock));
470 blk->buf = g_malloc(BLOCK_SIZE);
471 blk->bmds = bmds;
472 blk->sector = sector;
473 blk->nr_sectors = nr_sectors;
475 if (is_async) {
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);
480 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
481 nr_sectors, blk_mig_read_cb, blk);
483 blk_mig_lock();
484 block_mig_state.submitted++;
485 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
486 blk_mig_unlock();
487 } else {
488 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
489 if (ret < 0) {
490 goto error;
492 blk_send(f, blk);
494 g_free(blk->buf);
495 g_free(blk);
498 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
499 break;
501 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
502 bmds->cur_dirty = sector;
505 return (bmds->cur_dirty >= bmds->total_sectors);
507 error:
508 DPRINTF("Error reading sector %" PRId64 "\n", sector);
509 g_free(blk->buf);
510 g_free(blk);
511 return ret;
514 /* Called with iothread lock taken.
516 * return value:
517 * 0: too much data for max_downtime
518 * 1: few enough data for max_downtime
520 static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
522 BlkMigDevState *bmds;
523 int ret = 1;
525 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
526 ret = mig_save_device_dirty(f, bmds, is_async);
527 if (ret <= 0) {
528 break;
532 return ret;
535 /* Called with no locks taken. */
537 static int flush_blks(QEMUFile *f)
539 BlkMigBlock *blk;
540 int ret = 0;
542 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
543 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
544 block_mig_state.transferred);
546 blk_mig_lock();
547 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
548 if (qemu_file_rate_limit(f)) {
549 break;
551 if (blk->ret < 0) {
552 ret = blk->ret;
553 break;
556 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
557 blk_mig_unlock();
558 blk_send(f, blk);
559 blk_mig_lock();
561 g_free(blk->buf);
562 g_free(blk);
564 block_mig_state.read_done--;
565 block_mig_state.transferred++;
566 assert(block_mig_state.read_done >= 0);
568 blk_mig_unlock();
570 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
571 block_mig_state.submitted, block_mig_state.read_done,
572 block_mig_state.transferred);
573 return ret;
576 /* Called with iothread lock taken. */
578 static int64_t get_remaining_dirty(void)
580 BlkMigDevState *bmds;
581 int64_t dirty = 0;
583 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
584 dirty += bdrv_get_dirty_count(bmds->bs, bmds->dirty_bitmap);
587 return dirty << BDRV_SECTOR_BITS;
590 /* Called with iothread lock taken. */
592 static void blk_mig_cleanup(void)
594 BlkMigDevState *bmds;
595 BlkMigBlock *blk;
597 bdrv_drain_all();
599 unset_dirty_tracking();
601 blk_mig_lock();
602 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
603 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
604 bdrv_op_unblock_all(bmds->bs, bmds->blocker);
605 error_free(bmds->blocker);
606 bdrv_unref(bmds->bs);
607 g_free(bmds->aio_bitmap);
608 g_free(bmds);
611 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
612 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
613 g_free(blk->buf);
614 g_free(blk);
616 blk_mig_unlock();
619 static void block_migration_cancel(void *opaque)
621 blk_mig_cleanup();
624 static int block_save_setup(QEMUFile *f, void *opaque)
626 int ret;
628 DPRINTF("Enter save live setup submitted %d transferred %d\n",
629 block_mig_state.submitted, block_mig_state.transferred);
631 qemu_mutex_lock_iothread();
633 /* start track dirty blocks */
634 ret = set_dirty_tracking();
636 if (ret) {
637 qemu_mutex_unlock_iothread();
638 return ret;
641 init_blk_migration(f);
643 qemu_mutex_unlock_iothread();
645 ret = flush_blks(f);
646 blk_mig_reset_dirty_cursor();
647 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
649 return ret;
652 static int block_save_iterate(QEMUFile *f, void *opaque)
654 int ret;
655 int64_t last_ftell = qemu_ftell(f);
657 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
658 block_mig_state.submitted, block_mig_state.transferred);
660 ret = flush_blks(f);
661 if (ret) {
662 return ret;
665 blk_mig_reset_dirty_cursor();
667 /* control the rate of transfer */
668 blk_mig_lock();
669 while ((block_mig_state.submitted +
670 block_mig_state.read_done) * BLOCK_SIZE <
671 qemu_file_get_rate_limit(f)) {
672 blk_mig_unlock();
673 if (block_mig_state.bulk_completed == 0) {
674 /* first finish the bulk phase */
675 if (blk_mig_save_bulked_block(f) == 0) {
676 /* finished saving bulk on all devices */
677 block_mig_state.bulk_completed = 1;
679 ret = 0;
680 } else {
681 /* Always called with iothread lock taken for
682 * simplicity, block_save_complete also calls it.
684 qemu_mutex_lock_iothread();
685 ret = blk_mig_save_dirty_block(f, 1);
686 qemu_mutex_unlock_iothread();
688 if (ret < 0) {
689 return ret;
691 blk_mig_lock();
692 if (ret != 0) {
693 /* no more dirty blocks */
694 break;
697 blk_mig_unlock();
699 ret = flush_blks(f);
700 if (ret) {
701 return ret;
704 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
705 return qemu_ftell(f) - last_ftell;
708 /* Called with iothread lock taken. */
710 static int block_save_complete(QEMUFile *f, void *opaque)
712 int ret;
714 DPRINTF("Enter save live complete submitted %d transferred %d\n",
715 block_mig_state.submitted, block_mig_state.transferred);
717 ret = flush_blks(f);
718 if (ret) {
719 return ret;
722 blk_mig_reset_dirty_cursor();
724 /* we know for sure that save bulk is completed and
725 all async read completed */
726 blk_mig_lock();
727 assert(block_mig_state.submitted == 0);
728 blk_mig_unlock();
730 do {
731 ret = blk_mig_save_dirty_block(f, 0);
732 if (ret < 0) {
733 return ret;
735 } while (ret == 0);
737 /* report completion */
738 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
740 DPRINTF("Block migration completed\n");
742 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
744 blk_mig_cleanup();
745 return 0;
748 static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
750 /* Estimate pending number of bytes to send */
751 uint64_t pending;
753 qemu_mutex_lock_iothread();
754 blk_mig_lock();
755 pending = get_remaining_dirty() +
756 block_mig_state.submitted * BLOCK_SIZE +
757 block_mig_state.read_done * BLOCK_SIZE;
759 /* Report at least one block pending during bulk phase */
760 if (pending == 0 && !block_mig_state.bulk_completed) {
761 pending = BLOCK_SIZE;
763 blk_mig_unlock();
764 qemu_mutex_unlock_iothread();
766 DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
767 return pending;
770 static int block_load(QEMUFile *f, void *opaque, int version_id)
772 static int banner_printed;
773 int len, flags;
774 char device_name[256];
775 int64_t addr;
776 BlockDriverState *bs, *bs_prev = NULL;
777 uint8_t *buf;
778 int64_t total_sectors = 0;
779 int nr_sectors;
780 int ret;
782 do {
783 addr = qemu_get_be64(f);
785 flags = addr & ~BDRV_SECTOR_MASK;
786 addr >>= BDRV_SECTOR_BITS;
788 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
789 /* get device name */
790 len = qemu_get_byte(f);
791 qemu_get_buffer(f, (uint8_t *)device_name, len);
792 device_name[len] = '\0';
794 bs = bdrv_find(device_name);
795 if (!bs) {
796 fprintf(stderr, "Error unknown block device %s\n",
797 device_name);
798 return -EINVAL;
801 if (bs != bs_prev) {
802 bs_prev = bs;
803 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
804 if (total_sectors <= 0) {
805 error_report("Error getting length of block device %s",
806 device_name);
807 return -EINVAL;
811 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
812 nr_sectors = total_sectors - addr;
813 } else {
814 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
817 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
818 ret = bdrv_write_zeroes(bs, addr, nr_sectors,
819 BDRV_REQ_MAY_UNMAP);
820 } else {
821 buf = g_malloc(BLOCK_SIZE);
822 qemu_get_buffer(f, buf, BLOCK_SIZE);
823 ret = bdrv_write(bs, addr, buf, nr_sectors);
824 g_free(buf);
827 if (ret < 0) {
828 return ret;
830 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
831 if (!banner_printed) {
832 printf("Receiving block device images\n");
833 banner_printed = 1;
835 printf("Completed %d %%%c", (int)addr,
836 (addr == 100) ? '\n' : '\r');
837 fflush(stdout);
838 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
839 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
840 return -EINVAL;
842 ret = qemu_file_get_error(f);
843 if (ret != 0) {
844 return ret;
846 } while (!(flags & BLK_MIG_FLAG_EOS));
848 return 0;
851 static void block_set_params(const MigrationParams *params, void *opaque)
853 block_mig_state.blk_enable = params->blk;
854 block_mig_state.shared_base = params->shared;
856 /* shared base means that blk_enable = 1 */
857 block_mig_state.blk_enable |= params->shared;
860 static bool block_is_active(void *opaque)
862 return block_mig_state.blk_enable == 1;
865 SaveVMHandlers savevm_block_handlers = {
866 .set_params = block_set_params,
867 .save_live_setup = block_save_setup,
868 .save_live_iterate = block_save_iterate,
869 .save_live_complete = block_save_complete,
870 .save_live_pending = block_save_pending,
871 .load_state = block_load,
872 .cancel = block_migration_cancel,
873 .is_active = block_is_active,
876 void blk_mig_init(void)
878 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
879 QSIMPLEQ_INIT(&block_mig_state.blk_list);
880 qemu_mutex_init(&block_mig_state.lock);
882 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
883 &block_mig_state);