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