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