lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 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 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 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. |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
Peter Maydell | 1393a48 | 2016-01-26 18:16:54 +0000 | [diff] [blame] | 16 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 17 | #include "qapi/error.h" |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 18 | #include "qemu-common.h" |
Markus Armbruster | bfb197e | 2014-10-07 13:59:11 +0200 | [diff] [blame] | 19 | #include "block/block.h" |
| 20 | #include "qemu/error-report.h" |
| 21 | #include "qemu/main-loop.h" |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 22 | #include "hw/hw.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 23 | #include "qemu/cutils.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 24 | #include "qemu/queue.h" |
| 25 | #include "qemu/timer.h" |
Paolo Bonzini | caf71f8 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 26 | #include "migration/block.h" |
| 27 | #include "migration/migration.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 28 | #include "sysemu/blockdev.h" |
Fam Zheng | c9ebaf7 | 2015-03-02 19:36:47 +0800 | [diff] [blame] | 29 | #include "sysemu/block-backend.h" |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 30 | |
Paolo Bonzini | 50717e9 | 2013-01-21 17:09:45 +0100 | [diff] [blame] | 31 | #define BLOCK_SIZE (1 << 20) |
| 32 | #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 33 | |
| 34 | #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01 |
| 35 | #define BLK_MIG_FLAG_EOS 0x02 |
Jan Kiszka | 01e61e2 | 2009-12-01 15:20:17 +0100 | [diff] [blame] | 36 | #define BLK_MIG_FLAG_PROGRESS 0x04 |
Peter Lieven | 323004a | 2013-07-18 09:48:50 +0200 | [diff] [blame] | 37 | #define BLK_MIG_FLAG_ZERO_BLOCK 0x08 |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 38 | |
| 39 | #define MAX_IS_ALLOCATED_SEARCH 65536 |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 40 | |
Wen Congyang | f77dcdb | 2015-11-20 17:37:13 +0800 | [diff] [blame] | 41 | #define MAX_INFLIGHT_IO 512 |
| 42 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 43 | //#define DEBUG_BLK_MIGRATION |
| 44 | |
| 45 | #ifdef DEBUG_BLK_MIGRATION |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 46 | #define DPRINTF(fmt, ...) \ |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 47 | do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0) |
| 48 | #else |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 49 | #define DPRINTF(fmt, ...) \ |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 50 | do { } while (0) |
| 51 | #endif |
| 52 | |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 53 | typedef struct BlkMigDevState { |
Paolo Bonzini | 323920c | 2013-02-22 17:36:24 +0100 | [diff] [blame] | 54 | /* Written during setup phase. Can be read without a lock. */ |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 55 | BlockDriverState *bs; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 56 | int shared_base; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 57 | int64_t total_sectors; |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 58 | QSIMPLEQ_ENTRY(BlkMigDevState) entry; |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 59 | Error *blocker; |
Paolo Bonzini | 323920c | 2013-02-22 17:36:24 +0100 | [diff] [blame] | 60 | |
| 61 | /* Only used by migration thread. Does not need a lock. */ |
| 62 | int bulk_completed; |
| 63 | int64_t cur_sector; |
| 64 | int64_t cur_dirty; |
| 65 | |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 66 | /* Data in the aio_bitmap is protected by block migration lock. |
| 67 | * Allocation and free happen during setup and cleanup respectively. |
| 68 | */ |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 69 | unsigned long *aio_bitmap; |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 70 | |
| 71 | /* Protected by block migration lock. */ |
Paolo Bonzini | 323920c | 2013-02-22 17:36:24 +0100 | [diff] [blame] | 72 | int64_t completed_sectors; |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 73 | |
| 74 | /* During migration this is protected by iothread lock / AioContext. |
| 75 | * Allocation and free happen during setup and cleanup respectively. |
| 76 | */ |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 77 | BdrvDirtyBitmap *dirty_bitmap; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 78 | } BlkMigDevState; |
| 79 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 80 | typedef struct BlkMigBlock { |
Paolo Bonzini | 323920c | 2013-02-22 17:36:24 +0100 | [diff] [blame] | 81 | /* Only used by migration thread. */ |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 82 | uint8_t *buf; |
| 83 | BlkMigDevState *bmds; |
| 84 | int64_t sector; |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 85 | int nr_sectors; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 86 | struct iovec iov; |
| 87 | QEMUIOVector qiov; |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 88 | BlockAIOCB *aiocb; |
Paolo Bonzini | 323920c | 2013-02-22 17:36:24 +0100 | [diff] [blame] | 89 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 90 | /* Protected by block migration lock. */ |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 91 | int ret; |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 92 | QSIMPLEQ_ENTRY(BlkMigBlock) entry; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 93 | } BlkMigBlock; |
| 94 | |
| 95 | typedef struct BlkMigState { |
Paolo Bonzini | 323920c | 2013-02-22 17:36:24 +0100 | [diff] [blame] | 96 | /* Written during setup phase. Can be read without a lock. */ |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 97 | int blk_enable; |
| 98 | int shared_base; |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 99 | QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list; |
Paolo Bonzini | 323920c | 2013-02-22 17:36:24 +0100 | [diff] [blame] | 100 | int64_t total_sector_sum; |
Peter Lieven | 323004a | 2013-07-18 09:48:50 +0200 | [diff] [blame] | 101 | bool zero_blocks; |
Paolo Bonzini | 323920c | 2013-02-22 17:36:24 +0100 | [diff] [blame] | 102 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 103 | /* Protected by lock. */ |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 104 | QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 105 | int submitted; |
| 106 | int read_done; |
Paolo Bonzini | 323920c | 2013-02-22 17:36:24 +0100 | [diff] [blame] | 107 | |
| 108 | /* Only used by migration thread. Does not need a lock. */ |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 109 | int transferred; |
Jan Kiszka | 01e61e2 | 2009-12-01 15:20:17 +0100 | [diff] [blame] | 110 | int prev_progress; |
Liran Schour | e970ec0 | 2010-01-26 10:31:45 +0200 | [diff] [blame] | 111 | int bulk_completed; |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 112 | |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 113 | /* Lock must be taken _inside_ the iothread lock and any AioContexts. */ |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 114 | QemuMutex lock; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 115 | } BlkMigState; |
| 116 | |
Jan Kiszka | d11ecd3 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 117 | static BlkMigState block_mig_state; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 118 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 119 | static void blk_mig_lock(void) |
| 120 | { |
| 121 | qemu_mutex_lock(&block_mig_state.lock); |
| 122 | } |
| 123 | |
| 124 | static void blk_mig_unlock(void) |
| 125 | { |
| 126 | qemu_mutex_unlock(&block_mig_state.lock); |
| 127 | } |
| 128 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 129 | /* Must run outside of the iothread lock during the bulk phase, |
| 130 | * or the VM will stall. |
| 131 | */ |
| 132 | |
Jan Kiszka | 13f0b67 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 133 | static void blk_send(QEMUFile *f, BlkMigBlock * blk) |
| 134 | { |
| 135 | int len; |
Peter Lieven | 323004a | 2013-07-18 09:48:50 +0200 | [diff] [blame] | 136 | uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK; |
| 137 | |
| 138 | if (block_mig_state.zero_blocks && |
| 139 | buffer_is_zero(blk->buf, BLOCK_SIZE)) { |
| 140 | flags |= BLK_MIG_FLAG_ZERO_BLOCK; |
| 141 | } |
Jan Kiszka | 13f0b67 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 142 | |
| 143 | /* sector number and flags */ |
| 144 | qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS) |
Peter Lieven | 323004a | 2013-07-18 09:48:50 +0200 | [diff] [blame] | 145 | | flags); |
Jan Kiszka | 13f0b67 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 146 | |
| 147 | /* device name */ |
Markus Armbruster | bfb197e | 2014-10-07 13:59:11 +0200 | [diff] [blame] | 148 | len = strlen(bdrv_get_device_name(blk->bmds->bs)); |
Jan Kiszka | 13f0b67 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 149 | qemu_put_byte(f, len); |
Markus Armbruster | bfb197e | 2014-10-07 13:59:11 +0200 | [diff] [blame] | 150 | qemu_put_buffer(f, (uint8_t *)bdrv_get_device_name(blk->bmds->bs), len); |
Jan Kiszka | 13f0b67 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 151 | |
Peter Lieven | 323004a | 2013-07-18 09:48:50 +0200 | [diff] [blame] | 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; |
| 158 | } |
| 159 | |
Jan Kiszka | 13f0b67 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 160 | qemu_put_buffer(f, blk->buf, BLOCK_SIZE); |
| 161 | } |
| 162 | |
Jan Kiszka | 25f2364 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 163 | int blk_mig_active(void) |
| 164 | { |
| 165 | return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list); |
| 166 | } |
| 167 | |
| 168 | uint64_t blk_mig_bytes_transferred(void) |
| 169 | { |
| 170 | BlkMigDevState *bmds; |
| 171 | uint64_t sum = 0; |
| 172 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 173 | blk_mig_lock(); |
Jan Kiszka | 25f2364 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 174 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
| 175 | sum += bmds->completed_sectors; |
| 176 | } |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 177 | blk_mig_unlock(); |
Jan Kiszka | 25f2364 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 178 | return sum << BDRV_SECTOR_BITS; |
| 179 | } |
| 180 | |
| 181 | uint64_t blk_mig_bytes_remaining(void) |
| 182 | { |
| 183 | return blk_mig_bytes_total() - blk_mig_bytes_transferred(); |
| 184 | } |
| 185 | |
| 186 | uint64_t blk_mig_bytes_total(void) |
| 187 | { |
| 188 | BlkMigDevState *bmds; |
| 189 | uint64_t sum = 0; |
| 190 | |
| 191 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
| 192 | sum += bmds->total_sectors; |
| 193 | } |
| 194 | return sum << BDRV_SECTOR_BITS; |
| 195 | } |
| 196 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 197 | |
| 198 | /* Called with migration lock held. */ |
| 199 | |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 200 | static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector) |
| 201 | { |
| 202 | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
| 203 | |
Markus Armbruster | 57322b7 | 2014-06-26 13:23:22 +0200 | [diff] [blame] | 204 | if (sector < bdrv_nb_sectors(bmds->bs)) { |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 205 | return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
| 206 | (1UL << (chunk % (sizeof(unsigned long) * 8)))); |
| 207 | } else { |
| 208 | return 0; |
| 209 | } |
| 210 | } |
| 211 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 212 | /* Called with migration lock held. */ |
| 213 | |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 214 | static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num, |
| 215 | int nb_sectors, int set) |
| 216 | { |
| 217 | int64_t start, end; |
| 218 | unsigned long val, idx, bit; |
| 219 | |
| 220 | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
| 221 | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; |
| 222 | |
| 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) { |
Marcelo Tosatti | 62155e2 | 2010-11-12 16:07:50 -0200 | [diff] [blame] | 228 | val |= 1UL << bit; |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 229 | } else { |
Marcelo Tosatti | 62155e2 | 2010-11-12 16:07:50 -0200 | [diff] [blame] | 230 | val &= ~(1UL << bit); |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 231 | } |
| 232 | bmds->aio_bitmap[idx] = val; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | static void alloc_aio_bitmap(BlkMigDevState *bmds) |
| 237 | { |
| 238 | BlockDriverState *bs = bmds->bs; |
| 239 | int64_t bitmap_size; |
| 240 | |
Markus Armbruster | 57322b7 | 2014-06-26 13:23:22 +0200 | [diff] [blame] | 241 | bitmap_size = bdrv_nb_sectors(bs) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 242 | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; |
| 243 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 244 | bmds->aio_bitmap = g_malloc0(bitmap_size); |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 245 | } |
| 246 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 247 | /* Never hold migration lock when yielding to the main loop! */ |
| 248 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 249 | static void blk_mig_read_cb(void *opaque, int ret) |
| 250 | { |
| 251 | BlkMigBlock *blk = opaque; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 252 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 253 | blk_mig_lock(); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 254 | blk->ret = ret; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 255 | |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 256 | QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry); |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 257 | bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 258 | |
Jan Kiszka | d11ecd3 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 259 | block_mig_state.submitted--; |
| 260 | block_mig_state.read_done++; |
| 261 | assert(block_mig_state.submitted >= 0); |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 262 | blk_mig_unlock(); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 265 | /* Called with no lock taken. */ |
| 266 | |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 267 | static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 268 | { |
Jan Kiszka | 57cce12 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 269 | int64_t total_sectors = bmds->total_sectors; |
| 270 | int64_t cur_sector = bmds->cur_sector; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 271 | BlockDriverState *bs = bmds->bs; |
Jan Kiszka | 57cce12 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 272 | BlkMigBlock *blk; |
Jan Kiszka | 13f0b67 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 273 | int nr_sectors; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 274 | |
| 275 | if (bmds->shared_base) { |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 276 | qemu_mutex_lock_iothread(); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 277 | aio_context_acquire(bdrv_get_aio_context(bs)); |
Jan Kiszka | b1d1085 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 278 | while (cur_sector < total_sectors && |
Jan Kiszka | 57cce12 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 279 | !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH, |
| 280 | &nr_sectors)) { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 281 | cur_sector += nr_sectors; |
| 282 | } |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 283 | aio_context_release(bdrv_get_aio_context(bs)); |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 284 | qemu_mutex_unlock_iothread(); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 285 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 286 | |
| 287 | if (cur_sector >= total_sectors) { |
Jan Kiszka | 82801d8 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 288 | bmds->cur_sector = bmds->completed_sectors = total_sectors; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 289 | return 1; |
| 290 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 291 | |
Jan Kiszka | 82801d8 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 292 | bmds->completed_sectors = cur_sector; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 293 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 294 | cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 295 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 296 | /* we are going to transfer a full block even if it is not allocated */ |
| 297 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 298 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 299 | if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) { |
Jan Kiszka | 57cce12 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 300 | nr_sectors = total_sectors - cur_sector; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 301 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 302 | |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 303 | blk = g_new(BlkMigBlock, 1); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 304 | blk->buf = g_malloc(BLOCK_SIZE); |
Jan Kiszka | 13f0b67 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 305 | blk->bmds = bmds; |
| 306 | blk->sector = cur_sector; |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 307 | blk->nr_sectors = nr_sectors; |
Jan Kiszka | 13f0b67 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 308 | |
Liran Schour | e970ec0 | 2010-01-26 10:31:45 +0200 | [diff] [blame] | 309 | blk->iov.iov_base = blk->buf; |
| 310 | blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE; |
| 311 | qemu_iovec_init_external(&blk->qiov, &blk->iov, 1); |
Jan Kiszka | 57cce12 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 312 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 313 | blk_mig_lock(); |
Paolo Bonzini | 13197e3 | 2013-02-22 17:36:23 +0100 | [diff] [blame] | 314 | block_mig_state.submitted++; |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 315 | blk_mig_unlock(); |
Paolo Bonzini | 13197e3 | 2013-02-22 17:36:23 +0100 | [diff] [blame] | 316 | |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 317 | /* We do not know if bs is under the main thread (and thus does |
| 318 | * not acquire the AioContext when doing AIO) or rather under |
| 319 | * dataplane. Thus acquire both the iothread mutex and the |
| 320 | * AioContext. |
| 321 | * |
| 322 | * This is ugly and will disappear when we make bdrv_* thread-safe, |
| 323 | * without the need to acquire the AioContext. |
| 324 | */ |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 325 | qemu_mutex_lock_iothread(); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 326 | aio_context_acquire(bdrv_get_aio_context(bmds->bs)); |
Liran Schour | e970ec0 | 2010-01-26 10:31:45 +0200 | [diff] [blame] | 327 | blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov, |
| 328 | nr_sectors, blk_mig_read_cb, blk); |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 329 | |
John Snow | 20dca81 | 2015-04-17 19:50:02 -0400 | [diff] [blame] | 330 | bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector, nr_sectors); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 331 | aio_context_release(bdrv_get_aio_context(bmds->bs)); |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 332 | qemu_mutex_unlock_iothread(); |
Jan Kiszka | 13f0b67 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 333 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 334 | bmds->cur_sector = cur_sector + nr_sectors; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 335 | return (bmds->cur_sector >= total_sectors); |
| 336 | } |
| 337 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 338 | /* Called with iothread lock taken. */ |
| 339 | |
Fam Zheng | b8afb52 | 2014-04-16 09:34:30 +0800 | [diff] [blame] | 340 | static int set_dirty_tracking(void) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 341 | { |
| 342 | BlkMigDevState *bmds; |
Fam Zheng | b8afb52 | 2014-04-16 09:34:30 +0800 | [diff] [blame] | 343 | int ret; |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 344 | |
| 345 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 346 | aio_context_acquire(bdrv_get_aio_context(bmds->bs)); |
Fam Zheng | b8afb52 | 2014-04-16 09:34:30 +0800 | [diff] [blame] | 347 | bmds->dirty_bitmap = bdrv_create_dirty_bitmap(bmds->bs, BLOCK_SIZE, |
Fam Zheng | 0db6e54 | 2015-04-17 19:49:50 -0400 | [diff] [blame] | 348 | NULL, NULL); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 349 | aio_context_release(bdrv_get_aio_context(bmds->bs)); |
Fam Zheng | b8afb52 | 2014-04-16 09:34:30 +0800 | [diff] [blame] | 350 | if (!bmds->dirty_bitmap) { |
| 351 | ret = -errno; |
| 352 | goto fail; |
| 353 | } |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 354 | } |
Fam Zheng | b8afb52 | 2014-04-16 09:34:30 +0800 | [diff] [blame] | 355 | return 0; |
| 356 | |
| 357 | fail: |
| 358 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
| 359 | if (bmds->dirty_bitmap) { |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 360 | aio_context_acquire(bdrv_get_aio_context(bmds->bs)); |
Fam Zheng | b8afb52 | 2014-04-16 09:34:30 +0800 | [diff] [blame] | 361 | bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 362 | aio_context_release(bdrv_get_aio_context(bmds->bs)); |
Fam Zheng | b8afb52 | 2014-04-16 09:34:30 +0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | return ret; |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 366 | } |
| 367 | |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 368 | /* Called with iothread lock taken. */ |
| 369 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 370 | static void unset_dirty_tracking(void) |
| 371 | { |
| 372 | BlkMigDevState *bmds; |
| 373 | |
| 374 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 375 | aio_context_acquire(bdrv_get_aio_context(bmds->bs)); |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 376 | bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 377 | aio_context_release(bdrv_get_aio_context(bmds->bs)); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 378 | } |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Markus Armbruster | fea68bb | 2014-10-07 13:59:10 +0200 | [diff] [blame] | 381 | static void init_blk_migration(QEMUFile *f) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 382 | { |
Markus Armbruster | fea68bb | 2014-10-07 13:59:10 +0200 | [diff] [blame] | 383 | BlockDriverState *bs; |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 384 | BlkMigDevState *bmds; |
Jan Kiszka | 792773b | 2009-11-30 20:34:55 +0100 | [diff] [blame] | 385 | int64_t sectors; |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 386 | BdrvNextIterator it; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 387 | |
Markus Armbruster | fea68bb | 2014-10-07 13:59:10 +0200 | [diff] [blame] | 388 | block_mig_state.submitted = 0; |
| 389 | block_mig_state.read_done = 0; |
| 390 | block_mig_state.transferred = 0; |
| 391 | block_mig_state.total_sector_sum = 0; |
| 392 | block_mig_state.prev_progress = -1; |
| 393 | block_mig_state.bulk_completed = 0; |
| 394 | block_mig_state.zero_blocks = migrate_zero_blocks(); |
| 395 | |
Kevin Wolf | 7c8eece | 2016-03-22 18:58:50 +0100 | [diff] [blame] | 396 | |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 397 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
Markus Armbruster | fea68bb | 2014-10-07 13:59:10 +0200 | [diff] [blame] | 398 | if (bdrv_is_read_only(bs)) { |
| 399 | continue; |
| 400 | } |
| 401 | |
Markus Armbruster | 57322b7 | 2014-06-26 13:23:22 +0200 | [diff] [blame] | 402 | sectors = bdrv_nb_sectors(bs); |
Shahar Havivi | 31f54f2 | 2010-07-10 18:59:06 +0300 | [diff] [blame] | 403 | if (sectors <= 0) { |
Stefan Hajnoczi | b66460e | 2010-04-09 15:22:13 +0100 | [diff] [blame] | 404 | return; |
| 405 | } |
| 406 | |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 407 | bmds = g_new0(BlkMigDevState, 1); |
Stefan Hajnoczi | b66460e | 2010-04-09 15:22:13 +0100 | [diff] [blame] | 408 | bmds->bs = bs; |
| 409 | bmds->bulk_completed = 0; |
| 410 | bmds->total_sectors = sectors; |
| 411 | bmds->completed_sectors = 0; |
| 412 | bmds->shared_base = block_mig_state.shared_base; |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 413 | alloc_aio_bitmap(bmds); |
Fam Zheng | 3718d8a | 2014-05-23 21:29:43 +0800 | [diff] [blame] | 414 | error_setg(&bmds->blocker, "block device is in use by migration"); |
| 415 | bdrv_op_block_all(bs, bmds->blocker); |
Fam Zheng | 8442cfd | 2013-08-23 09:14:48 +0800 | [diff] [blame] | 416 | bdrv_ref(bs); |
Stefan Hajnoczi | b66460e | 2010-04-09 15:22:13 +0100 | [diff] [blame] | 417 | |
| 418 | block_mig_state.total_sector_sum += sectors; |
| 419 | |
| 420 | if (bmds->shared_base) { |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 421 | DPRINTF("Start migration for %s with shared base image\n", |
Markus Armbruster | bfb197e | 2014-10-07 13:59:11 +0200 | [diff] [blame] | 422 | bdrv_get_device_name(bs)); |
Stefan Hajnoczi | b66460e | 2010-04-09 15:22:13 +0100 | [diff] [blame] | 423 | } else { |
Markus Armbruster | bfb197e | 2014-10-07 13:59:11 +0200 | [diff] [blame] | 424 | DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs)); |
Stefan Hajnoczi | b66460e | 2010-04-09 15:22:13 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry); |
| 428 | } |
| 429 | } |
| 430 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 431 | /* Called with no lock taken. */ |
| 432 | |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 433 | static int blk_mig_save_bulked_block(QEMUFile *f) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 434 | { |
Jan Kiszka | 82801d8 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 435 | int64_t completed_sector_sum = 0; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 436 | BlkMigDevState *bmds; |
Jan Kiszka | 01e61e2 | 2009-12-01 15:20:17 +0100 | [diff] [blame] | 437 | int progress; |
Jan Kiszka | 82801d8 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 438 | int ret = 0; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 439 | |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 440 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 441 | if (bmds->bulk_completed == 0) { |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 442 | if (mig_save_device_bulk(f, bmds) == 1) { |
Jan Kiszka | 57cce12 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 443 | /* completed bulk section for this device */ |
| 444 | bmds->bulk_completed = 1; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 445 | } |
Jan Kiszka | 82801d8 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 446 | completed_sector_sum += bmds->completed_sectors; |
| 447 | ret = 1; |
| 448 | break; |
| 449 | } else { |
| 450 | completed_sector_sum += bmds->completed_sectors; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 451 | } |
| 452 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 453 | |
Pierre Riteau | 8b6b2af | 2011-01-12 14:41:00 +0100 | [diff] [blame] | 454 | if (block_mig_state.total_sector_sum != 0) { |
| 455 | progress = completed_sector_sum * 100 / |
| 456 | block_mig_state.total_sector_sum; |
| 457 | } else { |
| 458 | progress = 100; |
| 459 | } |
Jan Kiszka | 01e61e2 | 2009-12-01 15:20:17 +0100 | [diff] [blame] | 460 | if (progress != block_mig_state.prev_progress) { |
| 461 | block_mig_state.prev_progress = progress; |
| 462 | qemu_put_be64(f, (progress << BDRV_SECTOR_BITS) |
| 463 | | BLK_MIG_FLAG_PROGRESS); |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 464 | DPRINTF("Completed %d %%\r", progress); |
Jan Kiszka | 82801d8 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | return ret; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 470 | static void blk_mig_reset_dirty_cursor(void) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 471 | { |
| 472 | BlkMigDevState *bmds; |
Jan Kiszka | 575a58d | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 473 | |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 474 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 475 | bmds->cur_dirty = 0; |
| 476 | } |
| 477 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 478 | |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 479 | /* Called with iothread lock and AioContext taken. */ |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 480 | |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 481 | static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds, |
| 482 | int is_async) |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 483 | { |
| 484 | BlkMigBlock *blk; |
| 485 | int64_t total_sectors = bmds->total_sectors; |
| 486 | int64_t sector; |
| 487 | int nr_sectors; |
Juan Quintela | dcd1d22 | 2011-09-21 23:01:54 +0200 | [diff] [blame] | 488 | int ret = -EIO; |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 489 | |
| 490 | for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) { |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 491 | blk_mig_lock(); |
Marcelo Tosatti | 62155e2 | 2010-11-12 16:07:50 -0200 | [diff] [blame] | 492 | if (bmds_aio_inflight(bmds, sector)) { |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 493 | blk_mig_unlock(); |
Fam Zheng | 53ec73e | 2015-05-29 18:53:14 +0800 | [diff] [blame] | 494 | bdrv_drain(bmds->bs); |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 495 | } else { |
| 496 | blk_mig_unlock(); |
Marcelo Tosatti | 62155e2 | 2010-11-12 16:07:50 -0200 | [diff] [blame] | 497 | } |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 498 | if (bdrv_get_dirty(bmds->bs, bmds->dirty_bitmap, sector)) { |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 499 | |
| 500 | if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) { |
| 501 | nr_sectors = total_sectors - sector; |
| 502 | } else { |
| 503 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 504 | } |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 505 | blk = g_new(BlkMigBlock, 1); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 506 | blk->buf = g_malloc(BLOCK_SIZE); |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 507 | blk->bmds = bmds; |
| 508 | blk->sector = sector; |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 509 | blk->nr_sectors = nr_sectors; |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 510 | |
Liran Schour | 889ae39 | 2010-01-26 10:31:49 +0200 | [diff] [blame] | 511 | if (is_async) { |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 512 | blk->iov.iov_base = blk->buf; |
| 513 | blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE; |
| 514 | qemu_iovec_init_external(&blk->qiov, &blk->iov, 1); |
| 515 | |
| 516 | blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov, |
| 517 | nr_sectors, blk_mig_read_cb, blk); |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 518 | |
| 519 | blk_mig_lock(); |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 520 | block_mig_state.submitted++; |
Marcelo Tosatti | 33656af | 2010-11-08 17:02:56 -0200 | [diff] [blame] | 521 | bmds_set_aio_inflight(bmds, sector, nr_sectors, 1); |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 522 | blk_mig_unlock(); |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 523 | } else { |
Juan Quintela | dcd1d22 | 2011-09-21 23:01:54 +0200 | [diff] [blame] | 524 | ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors); |
| 525 | if (ret < 0) { |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 526 | goto error; |
| 527 | } |
| 528 | blk_send(f, blk); |
| 529 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 530 | g_free(blk->buf); |
| 531 | g_free(blk); |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 532 | } |
| 533 | |
John Snow | 20dca81 | 2015-04-17 19:50:02 -0400 | [diff] [blame] | 534 | bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, sector, nr_sectors); |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 535 | break; |
| 536 | } |
| 537 | sector += BDRV_SECTORS_PER_DIRTY_CHUNK; |
| 538 | bmds->cur_dirty = sector; |
| 539 | } |
| 540 | |
| 541 | return (bmds->cur_dirty >= bmds->total_sectors); |
| 542 | |
Liran Schour | 889ae39 | 2010-01-26 10:31:49 +0200 | [diff] [blame] | 543 | error: |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 544 | DPRINTF("Error reading sector %" PRId64 "\n", sector); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 545 | g_free(blk->buf); |
| 546 | g_free(blk); |
Juan Quintela | 43be3a2 | 2012-08-29 21:59:22 +0200 | [diff] [blame] | 547 | return ret; |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 550 | /* Called with iothread lock taken. |
| 551 | * |
| 552 | * return value: |
Juan Quintela | ceb2bd0 | 2012-08-29 21:37:14 +0200 | [diff] [blame] | 553 | * 0: too much data for max_downtime |
| 554 | * 1: few enough data for max_downtime |
| 555 | */ |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 556 | static int blk_mig_save_dirty_block(QEMUFile *f, int is_async) |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 557 | { |
| 558 | BlkMigDevState *bmds; |
Juan Quintela | ceb2bd0 | 2012-08-29 21:37:14 +0200 | [diff] [blame] | 559 | int ret = 1; |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 560 | |
| 561 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 562 | aio_context_acquire(bdrv_get_aio_context(bmds->bs)); |
Juan Quintela | ceb2bd0 | 2012-08-29 21:37:14 +0200 | [diff] [blame] | 563 | ret = mig_save_device_dirty(f, bmds, is_async); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 564 | aio_context_release(bdrv_get_aio_context(bmds->bs)); |
Juan Quintela | 43be3a2 | 2012-08-29 21:59:22 +0200 | [diff] [blame] | 565 | if (ret <= 0) { |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 566 | break; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 567 | } |
| 568 | } |
Jan Kiszka | 575a58d | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 569 | |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 570 | return ret; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 571 | } |
| 572 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 573 | /* Called with no locks taken. */ |
| 574 | |
Juan Quintela | 59feec4 | 2012-08-29 20:17:13 +0200 | [diff] [blame] | 575 | static int flush_blks(QEMUFile *f) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 576 | { |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 577 | BlkMigBlock *blk; |
Juan Quintela | 59feec4 | 2012-08-29 20:17:13 +0200 | [diff] [blame] | 578 | int ret = 0; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 579 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 580 | DPRINTF("%s Enter submitted %d read_done %d transferred %d\n", |
Jan Kiszka | d11ecd3 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 581 | __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done, |
| 582 | block_mig_state.transferred); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 583 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 584 | blk_mig_lock(); |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 585 | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
| 586 | if (qemu_file_rate_limit(f)) { |
| 587 | break; |
| 588 | } |
Jan Kiszka | 4b64036 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 589 | if (blk->ret < 0) { |
Juan Quintela | 59feec4 | 2012-08-29 20:17:13 +0200 | [diff] [blame] | 590 | ret = blk->ret; |
Jan Kiszka | 4b64036 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 591 | break; |
| 592 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 593 | |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 594 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 595 | blk_mig_unlock(); |
Paolo Bonzini | 13197e3 | 2013-02-22 17:36:23 +0100 | [diff] [blame] | 596 | blk_send(f, blk); |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 597 | blk_mig_lock(); |
Paolo Bonzini | 13197e3 | 2013-02-22 17:36:23 +0100 | [diff] [blame] | 598 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 599 | g_free(blk->buf); |
| 600 | g_free(blk); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 601 | |
Jan Kiszka | d11ecd3 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 602 | block_mig_state.read_done--; |
| 603 | block_mig_state.transferred++; |
| 604 | assert(block_mig_state.read_done >= 0); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 605 | } |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 606 | blk_mig_unlock(); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 607 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 608 | DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__, |
Jan Kiszka | d11ecd3 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 609 | block_mig_state.submitted, block_mig_state.read_done, |
| 610 | block_mig_state.transferred); |
Juan Quintela | 59feec4 | 2012-08-29 20:17:13 +0200 | [diff] [blame] | 611 | return ret; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 614 | /* Called with iothread lock taken. */ |
| 615 | |
Liran Schour | 889ae39 | 2010-01-26 10:31:49 +0200 | [diff] [blame] | 616 | static int64_t get_remaining_dirty(void) |
| 617 | { |
| 618 | BlkMigDevState *bmds; |
| 619 | int64_t dirty = 0; |
| 620 | |
| 621 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 622 | aio_context_acquire(bdrv_get_aio_context(bmds->bs)); |
John Snow | 20dca81 | 2015-04-17 19:50:02 -0400 | [diff] [blame] | 623 | dirty += bdrv_get_dirty_count(bmds->dirty_bitmap); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 624 | aio_context_release(bdrv_get_aio_context(bmds->bs)); |
Liran Schour | 889ae39 | 2010-01-26 10:31:49 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Paolo Bonzini | acc906c | 2013-01-21 17:09:44 +0100 | [diff] [blame] | 627 | return dirty << BDRV_SECTOR_BITS; |
Liran Schour | 889ae39 | 2010-01-26 10:31:49 +0200 | [diff] [blame] | 628 | } |
| 629 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 630 | /* Called with iothread lock taken. */ |
| 631 | |
Liang Li | 6ad2a21 | 2015-11-02 15:37:03 +0800 | [diff] [blame] | 632 | static void block_migration_cleanup(void *opaque) |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 633 | { |
Jan Kiszka | 82801d8 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 634 | BlkMigDevState *bmds; |
| 635 | BlkMigBlock *blk; |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 636 | AioContext *ctx; |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 637 | |
Kevin Wolf | 946d58b | 2012-09-25 15:47:36 +0200 | [diff] [blame] | 638 | bdrv_drain_all(); |
| 639 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 640 | unset_dirty_tracking(); |
Marcelo Tosatti | 8f794c5 | 2011-01-26 12:12:31 -0200 | [diff] [blame] | 641 | |
Jan Kiszka | 82801d8 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 642 | while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) { |
| 643 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry); |
Fam Zheng | 3718d8a | 2014-05-23 21:29:43 +0800 | [diff] [blame] | 644 | bdrv_op_unblock_all(bmds->bs, bmds->blocker); |
| 645 | error_free(bmds->blocker); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 646 | |
| 647 | /* Save ctx, because bmds->bs can disappear during bdrv_unref. */ |
| 648 | ctx = bdrv_get_aio_context(bmds->bs); |
| 649 | aio_context_acquire(ctx); |
Fam Zheng | 8442cfd | 2013-08-23 09:14:48 +0800 | [diff] [blame] | 650 | bdrv_unref(bmds->bs); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 651 | aio_context_release(ctx); |
| 652 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 653 | g_free(bmds->aio_bitmap); |
| 654 | g_free(bmds); |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 655 | } |
| 656 | |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 657 | blk_mig_lock(); |
Jan Kiszka | 82801d8 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 658 | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
| 659 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 660 | g_free(blk->buf); |
| 661 | g_free(blk); |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 662 | } |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 663 | blk_mig_unlock(); |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Juan Quintela | d1315aa | 2012-06-28 15:11:57 +0200 | [diff] [blame] | 666 | static int block_save_setup(QEMUFile *f, void *opaque) |
| 667 | { |
| 668 | int ret; |
| 669 | |
| 670 | DPRINTF("Enter save live setup submitted %d transferred %d\n", |
| 671 | block_mig_state.submitted, block_mig_state.transferred); |
| 672 | |
Paolo Bonzini | 9b09503 | 2013-02-22 17:36:28 +0100 | [diff] [blame] | 673 | qemu_mutex_lock_iothread(); |
chai wen | 1ac362c | 2014-06-04 11:47:37 +0800 | [diff] [blame] | 674 | init_blk_migration(f); |
Juan Quintela | d1315aa | 2012-06-28 15:11:57 +0200 | [diff] [blame] | 675 | |
| 676 | /* start track dirty blocks */ |
Fam Zheng | b8afb52 | 2014-04-16 09:34:30 +0800 | [diff] [blame] | 677 | ret = set_dirty_tracking(); |
| 678 | |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 679 | qemu_mutex_unlock_iothread(); |
| 680 | |
Fam Zheng | b8afb52 | 2014-04-16 09:34:30 +0800 | [diff] [blame] | 681 | if (ret) { |
Fam Zheng | b8afb52 | 2014-04-16 09:34:30 +0800 | [diff] [blame] | 682 | return ret; |
| 683 | } |
| 684 | |
Juan Quintela | 59feec4 | 2012-08-29 20:17:13 +0200 | [diff] [blame] | 685 | ret = flush_blks(f); |
Juan Quintela | d1315aa | 2012-06-28 15:11:57 +0200 | [diff] [blame] | 686 | blk_mig_reset_dirty_cursor(); |
Juan Quintela | d1315aa | 2012-06-28 15:11:57 +0200 | [diff] [blame] | 687 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
| 688 | |
Paolo Bonzini | d418cf5 | 2013-02-22 17:36:11 +0100 | [diff] [blame] | 689 | return ret; |
Juan Quintela | d1315aa | 2012-06-28 15:11:57 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 692 | static int block_save_iterate(QEMUFile *f, void *opaque) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 693 | { |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 694 | int ret; |
Stefan Hajnoczi | 6aaa9da | 2013-02-12 10:37:15 +0100 | [diff] [blame] | 695 | int64_t last_ftell = qemu_ftell(f); |
Gary R Hook | ebd9fbd | 2014-11-25 17:30:02 -0600 | [diff] [blame] | 696 | int64_t delta_ftell; |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 697 | |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 698 | DPRINTF("Enter save live iterate submitted %d transferred %d\n", |
| 699 | block_mig_state.submitted, block_mig_state.transferred); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 700 | |
Juan Quintela | 59feec4 | 2012-08-29 20:17:13 +0200 | [diff] [blame] | 701 | ret = flush_blks(f); |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 702 | if (ret) { |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 703 | return ret; |
Jan Kiszka | 4b64036 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 706 | blk_mig_reset_dirty_cursor(); |
| 707 | |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 708 | /* control the rate of transfer */ |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 709 | blk_mig_lock(); |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 710 | while ((block_mig_state.submitted + |
| 711 | block_mig_state.read_done) * BLOCK_SIZE < |
Wen Congyang | f77dcdb | 2015-11-20 17:37:13 +0800 | [diff] [blame] | 712 | qemu_file_get_rate_limit(f) && |
| 713 | (block_mig_state.submitted + |
| 714 | block_mig_state.read_done) < |
| 715 | MAX_INFLIGHT_IO) { |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 716 | blk_mig_unlock(); |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 717 | if (block_mig_state.bulk_completed == 0) { |
| 718 | /* first finish the bulk phase */ |
| 719 | if (blk_mig_save_bulked_block(f) == 0) { |
| 720 | /* finished saving bulk on all devices */ |
| 721 | block_mig_state.bulk_completed = 1; |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 722 | } |
Paolo Bonzini | 13197e3 | 2013-02-22 17:36:23 +0100 | [diff] [blame] | 723 | ret = 0; |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 724 | } else { |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 725 | /* Always called with iothread lock taken for |
| 726 | * simplicity, block_save_complete also calls it. |
| 727 | */ |
| 728 | qemu_mutex_lock_iothread(); |
Juan Quintela | 43be3a2 | 2012-08-29 21:59:22 +0200 | [diff] [blame] | 729 | ret = blk_mig_save_dirty_block(f, 1); |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 730 | qemu_mutex_unlock_iothread(); |
Paolo Bonzini | 13197e3 | 2013-02-22 17:36:23 +0100 | [diff] [blame] | 731 | } |
| 732 | if (ret < 0) { |
| 733 | return ret; |
| 734 | } |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 735 | blk_mig_lock(); |
Paolo Bonzini | 13197e3 | 2013-02-22 17:36:23 +0100 | [diff] [blame] | 736 | if (ret != 0) { |
| 737 | /* no more dirty blocks */ |
| 738 | break; |
Liran Schour | d76cac7 | 2010-01-26 14:04:11 +0200 | [diff] [blame] | 739 | } |
Jan Kiszka | 4b64036 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 740 | } |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 741 | blk_mig_unlock(); |
Jan Kiszka | 4b64036 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 742 | |
Juan Quintela | 59feec4 | 2012-08-29 20:17:13 +0200 | [diff] [blame] | 743 | ret = flush_blks(f); |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 744 | if (ret) { |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 745 | return ret; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 746 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 747 | |
| 748 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
Gary R Hook | ebd9fbd | 2014-11-25 17:30:02 -0600 | [diff] [blame] | 749 | delta_ftell = qemu_ftell(f) - last_ftell; |
| 750 | if (delta_ftell > 0) { |
| 751 | return 1; |
| 752 | } else if (delta_ftell < 0) { |
| 753 | return -1; |
| 754 | } else { |
| 755 | return 0; |
| 756 | } |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 759 | /* Called with iothread lock taken. */ |
| 760 | |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 761 | static int block_save_complete(QEMUFile *f, void *opaque) |
| 762 | { |
| 763 | int ret; |
| 764 | |
| 765 | DPRINTF("Enter save live complete submitted %d transferred %d\n", |
| 766 | block_mig_state.submitted, block_mig_state.transferred); |
| 767 | |
Juan Quintela | 59feec4 | 2012-08-29 20:17:13 +0200 | [diff] [blame] | 768 | ret = flush_blks(f); |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 769 | if (ret) { |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | blk_mig_reset_dirty_cursor(); |
| 774 | |
| 775 | /* we know for sure that save bulk is completed and |
| 776 | all async read completed */ |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 777 | blk_mig_lock(); |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 778 | assert(block_mig_state.submitted == 0); |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 779 | blk_mig_unlock(); |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 780 | |
Juan Quintela | 43be3a2 | 2012-08-29 21:59:22 +0200 | [diff] [blame] | 781 | do { |
| 782 | ret = blk_mig_save_dirty_block(f, 0); |
Paolo Bonzini | d418cf5 | 2013-02-22 17:36:11 +0100 | [diff] [blame] | 783 | if (ret < 0) { |
| 784 | return ret; |
| 785 | } |
Juan Quintela | 43be3a2 | 2012-08-29 21:59:22 +0200 | [diff] [blame] | 786 | } while (ret == 0); |
| 787 | |
Juan Quintela | 43be3a2 | 2012-08-29 21:59:22 +0200 | [diff] [blame] | 788 | /* report completion */ |
| 789 | qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS); |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 790 | |
| 791 | DPRINTF("Block migration completed\n"); |
| 792 | |
| 793 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
| 794 | |
| 795 | return 0; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 796 | } |
| 797 | |
Dr. David Alan Gilbert | c31b098 | 2015-11-05 18:10:54 +0000 | [diff] [blame] | 798 | static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size, |
| 799 | uint64_t *non_postcopiable_pending, |
| 800 | uint64_t *postcopiable_pending) |
Juan Quintela | e4ed154 | 2012-09-21 11:18:18 +0200 | [diff] [blame] | 801 | { |
Stefan Hajnoczi | 6aaa9da | 2013-02-12 10:37:15 +0100 | [diff] [blame] | 802 | /* Estimate pending number of bytes to send */ |
Paolo Bonzini | 13197e3 | 2013-02-22 17:36:23 +0100 | [diff] [blame] | 803 | uint64_t pending; |
| 804 | |
Paolo Bonzini | 32c835b | 2013-02-22 17:36:27 +0100 | [diff] [blame] | 805 | qemu_mutex_lock_iothread(); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 806 | pending = get_remaining_dirty(); |
| 807 | qemu_mutex_unlock_iothread(); |
| 808 | |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 809 | blk_mig_lock(); |
Paolo Bonzini | ef0716d | 2016-02-14 18:17:04 +0100 | [diff] [blame] | 810 | pending += block_mig_state.submitted * BLOCK_SIZE + |
| 811 | block_mig_state.read_done * BLOCK_SIZE; |
| 812 | blk_mig_unlock(); |
Juan Quintela | e4ed154 | 2012-09-21 11:18:18 +0200 | [diff] [blame] | 813 | |
Stefan Hajnoczi | 6aaa9da | 2013-02-12 10:37:15 +0100 | [diff] [blame] | 814 | /* Report at least one block pending during bulk phase */ |
Vladimir Sementsov-Ogievskiy | 04636dc | 2014-12-30 13:04:16 +0300 | [diff] [blame] | 815 | if (pending <= max_size && !block_mig_state.bulk_completed) { |
| 816 | pending = max_size + BLOCK_SIZE; |
Stefan Hajnoczi | 6aaa9da | 2013-02-12 10:37:15 +0100 | [diff] [blame] | 817 | } |
Juan Quintela | e4ed154 | 2012-09-21 11:18:18 +0200 | [diff] [blame] | 818 | |
Stefan Hajnoczi | 6aaa9da | 2013-02-12 10:37:15 +0100 | [diff] [blame] | 819 | DPRINTF("Enter save live pending %" PRIu64 "\n", pending); |
Dr. David Alan Gilbert | c31b098 | 2015-11-05 18:10:54 +0000 | [diff] [blame] | 820 | /* We don't do postcopy */ |
| 821 | *non_postcopiable_pending += pending; |
Juan Quintela | e4ed154 | 2012-09-21 11:18:18 +0200 | [diff] [blame] | 822 | } |
| 823 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 824 | static int block_load(QEMUFile *f, void *opaque, int version_id) |
| 825 | { |
Jan Kiszka | 01e61e2 | 2009-12-01 15:20:17 +0100 | [diff] [blame] | 826 | static int banner_printed; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 827 | int len, flags; |
| 828 | char device_name[256]; |
| 829 | int64_t addr; |
Pierre Riteau | 77358b5 | 2011-01-21 12:42:30 +0100 | [diff] [blame] | 830 | BlockDriverState *bs, *bs_prev = NULL; |
Fam Zheng | c9ebaf7 | 2015-03-02 19:36:47 +0800 | [diff] [blame] | 831 | BlockBackend *blk; |
Kevin Wolf | 9bd9c7f | 2016-02-22 10:21:15 +0100 | [diff] [blame] | 832 | Error *local_err = NULL; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 833 | uint8_t *buf; |
Pierre Riteau | 77358b5 | 2011-01-21 12:42:30 +0100 | [diff] [blame] | 834 | int64_t total_sectors = 0; |
| 835 | int nr_sectors; |
Juan Quintela | 42802d4 | 2011-10-05 01:14:46 +0200 | [diff] [blame] | 836 | int ret; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 837 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 838 | do { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 839 | addr = qemu_get_be64(f); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 840 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 841 | flags = addr & ~BDRV_SECTOR_MASK; |
| 842 | addr >>= BDRV_SECTOR_BITS; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 843 | |
| 844 | if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 845 | /* get device name */ |
| 846 | len = qemu_get_byte(f); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 847 | qemu_get_buffer(f, (uint8_t *)device_name, len); |
| 848 | device_name[len] = '\0'; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 849 | |
Fam Zheng | c9ebaf7 | 2015-03-02 19:36:47 +0800 | [diff] [blame] | 850 | blk = blk_by_name(device_name); |
| 851 | if (!blk) { |
Jan Kiszka | 4b64036 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 852 | fprintf(stderr, "Error unknown block device %s\n", |
| 853 | device_name); |
| 854 | return -EINVAL; |
| 855 | } |
Fam Zheng | c9ebaf7 | 2015-03-02 19:36:47 +0800 | [diff] [blame] | 856 | bs = blk_bs(blk); |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 857 | if (!bs) { |
| 858 | fprintf(stderr, "Block device %s has no medium\n", |
| 859 | device_name); |
| 860 | return -EINVAL; |
| 861 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 862 | |
Pierre Riteau | 77358b5 | 2011-01-21 12:42:30 +0100 | [diff] [blame] | 863 | if (bs != bs_prev) { |
| 864 | bs_prev = bs; |
Markus Armbruster | 57322b7 | 2014-06-26 13:23:22 +0200 | [diff] [blame] | 865 | total_sectors = bdrv_nb_sectors(bs); |
Pierre Riteau | 77358b5 | 2011-01-21 12:42:30 +0100 | [diff] [blame] | 866 | if (total_sectors <= 0) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 867 | error_report("Error getting length of block device %s", |
Pierre Riteau | 77358b5 | 2011-01-21 12:42:30 +0100 | [diff] [blame] | 868 | device_name); |
| 869 | return -EINVAL; |
| 870 | } |
Kevin Wolf | 9bd9c7f | 2016-02-22 10:21:15 +0100 | [diff] [blame] | 871 | |
| 872 | bdrv_invalidate_cache(bs, &local_err); |
| 873 | if (local_err) { |
| 874 | error_report_err(local_err); |
| 875 | return -EINVAL; |
| 876 | } |
Pierre Riteau | 77358b5 | 2011-01-21 12:42:30 +0100 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) { |
| 880 | nr_sectors = total_sectors - addr; |
| 881 | } else { |
| 882 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; |
| 883 | } |
| 884 | |
Peter Lieven | 323004a | 2013-07-18 09:48:50 +0200 | [diff] [blame] | 885 | if (flags & BLK_MIG_FLAG_ZERO_BLOCK) { |
Peter Lieven | d32f35c | 2013-10-24 12:06:52 +0200 | [diff] [blame] | 886 | ret = bdrv_write_zeroes(bs, addr, nr_sectors, |
| 887 | BDRV_REQ_MAY_UNMAP); |
Peter Lieven | 323004a | 2013-07-18 09:48:50 +0200 | [diff] [blame] | 888 | } else { |
| 889 | buf = g_malloc(BLOCK_SIZE); |
| 890 | qemu_get_buffer(f, buf, BLOCK_SIZE); |
| 891 | ret = bdrv_write(bs, addr, buf, nr_sectors); |
| 892 | g_free(buf); |
| 893 | } |
Jan Kiszka | 575a58d | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 894 | |
Yoshiaki Tamura | b02bea3 | 2010-07-20 18:19:00 +0900 | [diff] [blame] | 895 | if (ret < 0) { |
| 896 | return ret; |
| 897 | } |
Jan Kiszka | 01e61e2 | 2009-12-01 15:20:17 +0100 | [diff] [blame] | 898 | } else if (flags & BLK_MIG_FLAG_PROGRESS) { |
| 899 | if (!banner_printed) { |
| 900 | printf("Receiving block device images\n"); |
| 901 | banner_printed = 1; |
| 902 | } |
| 903 | printf("Completed %d %%%c", (int)addr, |
| 904 | (addr == 100) ? '\n' : '\r'); |
| 905 | fflush(stdout); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 906 | } else if (!(flags & BLK_MIG_FLAG_EOS)) { |
Stefan Hajnoczi | d5f1f28 | 2013-02-10 23:12:44 +0100 | [diff] [blame] | 907 | fprintf(stderr, "Unknown block migration flags: %#x\n", flags); |
Jan Kiszka | 4b64036 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 908 | return -EINVAL; |
| 909 | } |
Juan Quintela | 42802d4 | 2011-10-05 01:14:46 +0200 | [diff] [blame] | 910 | ret = qemu_file_get_error(f); |
| 911 | if (ret != 0) { |
| 912 | return ret; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 913 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 914 | } while (!(flags & BLK_MIG_FLAG_EOS)); |
| 915 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 916 | return 0; |
| 917 | } |
| 918 | |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 919 | static void block_set_params(const MigrationParams *params, void *opaque) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 920 | { |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 921 | block_mig_state.blk_enable = params->blk; |
| 922 | block_mig_state.shared_base = params->shared; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 923 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 924 | /* shared base means that blk_enable = 1 */ |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 925 | block_mig_state.blk_enable |= params->shared; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Juan Quintela | 6bd6878 | 2012-06-27 10:59:15 +0200 | [diff] [blame] | 928 | static bool block_is_active(void *opaque) |
| 929 | { |
| 930 | return block_mig_state.blk_enable == 1; |
| 931 | } |
| 932 | |
Stefan Weil | 7a46d04 | 2014-07-07 21:09:30 +0200 | [diff] [blame] | 933 | static SaveVMHandlers savevm_block_handlers = { |
Juan Quintela | 7908c78 | 2012-06-26 18:46:10 +0200 | [diff] [blame] | 934 | .set_params = block_set_params, |
Juan Quintela | d1315aa | 2012-06-28 15:11:57 +0200 | [diff] [blame] | 935 | .save_live_setup = block_save_setup, |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 936 | .save_live_iterate = block_save_iterate, |
Dr. David Alan Gilbert | a3e06c3 | 2015-11-05 18:10:41 +0000 | [diff] [blame] | 937 | .save_live_complete_precopy = block_save_complete, |
Juan Quintela | e4ed154 | 2012-09-21 11:18:18 +0200 | [diff] [blame] | 938 | .save_live_pending = block_save_pending, |
Juan Quintela | 7908c78 | 2012-06-26 18:46:10 +0200 | [diff] [blame] | 939 | .load_state = block_load, |
Liang Li | 6ad2a21 | 2015-11-02 15:37:03 +0800 | [diff] [blame] | 940 | .cleanup = block_migration_cleanup, |
Juan Quintela | 6bd6878 | 2012-06-27 10:59:15 +0200 | [diff] [blame] | 941 | .is_active = block_is_active, |
Juan Quintela | 7908c78 | 2012-06-26 18:46:10 +0200 | [diff] [blame] | 942 | }; |
| 943 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 944 | void blk_mig_init(void) |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 945 | { |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 946 | QSIMPLEQ_INIT(&block_mig_state.bmds_list); |
| 947 | QSIMPLEQ_INIT(&block_mig_state.blk_list); |
Paolo Bonzini | 52e850d | 2013-02-22 17:36:25 +0100 | [diff] [blame] | 948 | qemu_mutex_init(&block_mig_state.lock); |
Jan Kiszka | 5e5328b | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 949 | |
Juan Quintela | 7908c78 | 2012-06-26 18:46:10 +0200 | [diff] [blame] | 950 | register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers, |
| 951 | &block_mig_state); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 952 | } |