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