]> git.proxmox.com Git - mirror_qemu.git/blob - migration/block.c
Merge remote-tracking branch 'cohuck/tags/s390x-20170523' into staging
[mirror_qemu.git] / migration / block.c
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 *
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.
14 */
15
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "qemu-common.h"
19 #include "block/block.h"
20 #include "qemu/error-report.h"
21 #include "qemu/main-loop.h"
22 #include "hw/hw.h"
23 #include "qemu/cutils.h"
24 #include "qemu/queue.h"
25 #include "qemu/timer.h"
26 #include "migration/block.h"
27 #include "migration/migration.h"
28 #include "sysemu/blockdev.h"
29 #include "migration/qemu-file.h"
30 #include "migration/vmstate.h"
31 #include "sysemu/block-backend.h"
32
33 #define BLOCK_SIZE (1 << 20)
34 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
35
36 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
37 #define BLK_MIG_FLAG_EOS 0x02
38 #define BLK_MIG_FLAG_PROGRESS 0x04
39 #define BLK_MIG_FLAG_ZERO_BLOCK 0x08
40
41 #define MAX_IS_ALLOCATED_SEARCH 65536
42
43 #define MAX_INFLIGHT_IO 512
44
45 //#define DEBUG_BLK_MIGRATION
46
47 #ifdef DEBUG_BLK_MIGRATION
48 #define DPRINTF(fmt, ...) \
49 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
50 #else
51 #define DPRINTF(fmt, ...) \
52 do { } while (0)
53 #endif
54
55 typedef struct BlkMigDevState {
56 /* Written during setup phase. Can be read without a lock. */
57 BlockBackend *blk;
58 char *blk_name;
59 int shared_base;
60 int64_t total_sectors;
61 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
62 Error *blocker;
63
64 /* Only used by migration thread. Does not need a lock. */
65 int bulk_completed;
66 int64_t cur_sector;
67 int64_t cur_dirty;
68
69 /* Data in the aio_bitmap is protected by block migration lock.
70 * Allocation and free happen during setup and cleanup respectively.
71 */
72 unsigned long *aio_bitmap;
73
74 /* Protected by block migration lock. */
75 int64_t completed_sectors;
76
77 /* During migration this is protected by iothread lock / AioContext.
78 * Allocation and free happen during setup and cleanup respectively.
79 */
80 BdrvDirtyBitmap *dirty_bitmap;
81 } BlkMigDevState;
82
83 typedef struct BlkMigBlock {
84 /* Only used by migration thread. */
85 uint8_t *buf;
86 BlkMigDevState *bmds;
87 int64_t sector;
88 int nr_sectors;
89 struct iovec iov;
90 QEMUIOVector qiov;
91 BlockAIOCB *aiocb;
92
93 /* Protected by block migration lock. */
94 int ret;
95 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
96 } BlkMigBlock;
97
98 typedef struct BlkMigState {
99 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
100 int64_t total_sector_sum;
101 bool zero_blocks;
102
103 /* Protected by lock. */
104 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
105 int submitted;
106 int read_done;
107
108 /* Only used by migration thread. Does not need a lock. */
109 int transferred;
110 int prev_progress;
111 int bulk_completed;
112
113 /* Lock must be taken _inside_ the iothread lock and any AioContexts. */
114 QemuMutex lock;
115 } BlkMigState;
116
117 static BlkMigState block_mig_state;
118
119 static void blk_mig_lock(void)
120 {
121 qemu_mutex_lock(&block_mig_state.lock);
122 }
123
124 static void blk_mig_unlock(void)
125 {
126 qemu_mutex_unlock(&block_mig_state.lock);
127 }
128
129 /* Must run outside of the iothread lock during the bulk phase,
130 * or the VM will stall.
131 */
132
133 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
134 {
135 int len;
136 uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK;
137
138 if (block_mig_state.zero_blocks &&
139 buffer_is_zero(blk->buf, BLOCK_SIZE)) {
140 flags |= BLK_MIG_FLAG_ZERO_BLOCK;
141 }
142
143 /* sector number and flags */
144 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
145 | flags);
146
147 /* device name */
148 len = strlen(blk->bmds->blk_name);
149 qemu_put_byte(f, len);
150 qemu_put_buffer(f, (uint8_t *) blk->bmds->blk_name, len);
151
152 /* if a block is zero we need to flush here since the network
153 * bandwidth is now a lot higher than the storage device bandwidth.
154 * thus if we queue zero blocks we slow down the migration */
155 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
156 qemu_fflush(f);
157 return;
158 }
159
160 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
161 }
162
163 int blk_mig_active(void)
164 {
165 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
166 }
167
168 uint64_t blk_mig_bytes_transferred(void)
169 {
170 BlkMigDevState *bmds;
171 uint64_t sum = 0;
172
173 blk_mig_lock();
174 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
175 sum += bmds->completed_sectors;
176 }
177 blk_mig_unlock();
178 return sum << BDRV_SECTOR_BITS;
179 }
180
181 uint64_t blk_mig_bytes_remaining(void)
182 {
183 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
184 }
185
186 uint64_t blk_mig_bytes_total(void)
187 {
188 BlkMigDevState *bmds;
189 uint64_t sum = 0;
190
191 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
192 sum += bmds->total_sectors;
193 }
194 return sum << BDRV_SECTOR_BITS;
195 }
196
197
198 /* Called with migration lock held. */
199
200 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
201 {
202 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
203
204 if (sector < blk_nb_sectors(bmds->blk)) {
205 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
206 (1UL << (chunk % (sizeof(unsigned long) * 8))));
207 } else {
208 return 0;
209 }
210 }
211
212 /* Called with migration lock held. */
213
214 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
215 int nb_sectors, int set)
216 {
217 int64_t start, end;
218 unsigned long val, idx, bit;
219
220 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
221 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
222
223 for (; start <= end; start++) {
224 idx = start / (sizeof(unsigned long) * 8);
225 bit = start % (sizeof(unsigned long) * 8);
226 val = bmds->aio_bitmap[idx];
227 if (set) {
228 val |= 1UL << bit;
229 } else {
230 val &= ~(1UL << bit);
231 }
232 bmds->aio_bitmap[idx] = val;
233 }
234 }
235
236 static void alloc_aio_bitmap(BlkMigDevState *bmds)
237 {
238 BlockBackend *bb = bmds->blk;
239 int64_t bitmap_size;
240
241 bitmap_size = blk_nb_sectors(bb) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
242 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
243
244 bmds->aio_bitmap = g_malloc0(bitmap_size);
245 }
246
247 /* Never hold migration lock when yielding to the main loop! */
248
249 static void blk_mig_read_cb(void *opaque, int ret)
250 {
251 BlkMigBlock *blk = opaque;
252
253 blk_mig_lock();
254 blk->ret = ret;
255
256 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
257 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
258
259 block_mig_state.submitted--;
260 block_mig_state.read_done++;
261 assert(block_mig_state.submitted >= 0);
262 blk_mig_unlock();
263 }
264
265 /* Called with no lock taken. */
266
267 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
268 {
269 int64_t total_sectors = bmds->total_sectors;
270 int64_t cur_sector = bmds->cur_sector;
271 BlockBackend *bb = bmds->blk;
272 BlkMigBlock *blk;
273 int nr_sectors;
274
275 if (bmds->shared_base) {
276 qemu_mutex_lock_iothread();
277 aio_context_acquire(blk_get_aio_context(bb));
278 /* Skip unallocated sectors; intentionally treats failure as
279 * an allocated sector */
280 while (cur_sector < total_sectors &&
281 !bdrv_is_allocated(blk_bs(bb), cur_sector,
282 MAX_IS_ALLOCATED_SEARCH, &nr_sectors)) {
283 cur_sector += nr_sectors;
284 }
285 aio_context_release(blk_get_aio_context(bb));
286 qemu_mutex_unlock_iothread();
287 }
288
289 if (cur_sector >= total_sectors) {
290 bmds->cur_sector = bmds->completed_sectors = total_sectors;
291 return 1;
292 }
293
294 bmds->completed_sectors = cur_sector;
295
296 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
297
298 /* we are going to transfer a full block even if it is not allocated */
299 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
300
301 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
302 nr_sectors = total_sectors - cur_sector;
303 }
304
305 blk = g_new(BlkMigBlock, 1);
306 blk->buf = g_malloc(BLOCK_SIZE);
307 blk->bmds = bmds;
308 blk->sector = cur_sector;
309 blk->nr_sectors = nr_sectors;
310
311 blk->iov.iov_base = blk->buf;
312 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
313 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
314
315 blk_mig_lock();
316 block_mig_state.submitted++;
317 blk_mig_unlock();
318
319 /* We do not know if bs is under the main thread (and thus does
320 * not acquire the AioContext when doing AIO) or rather under
321 * dataplane. Thus acquire both the iothread mutex and the
322 * AioContext.
323 *
324 * This is ugly and will disappear when we make bdrv_* thread-safe,
325 * without the need to acquire the AioContext.
326 */
327 qemu_mutex_lock_iothread();
328 aio_context_acquire(blk_get_aio_context(bmds->blk));
329 blk->aiocb = blk_aio_preadv(bb, cur_sector * BDRV_SECTOR_SIZE, &blk->qiov,
330 0, blk_mig_read_cb, blk);
331
332 bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector, nr_sectors);
333 aio_context_release(blk_get_aio_context(bmds->blk));
334 qemu_mutex_unlock_iothread();
335
336 bmds->cur_sector = cur_sector + nr_sectors;
337 return (bmds->cur_sector >= total_sectors);
338 }
339
340 /* Called with iothread lock taken. */
341
342 static int set_dirty_tracking(void)
343 {
344 BlkMigDevState *bmds;
345 int ret;
346
347 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
348 aio_context_acquire(blk_get_aio_context(bmds->blk));
349 bmds->dirty_bitmap = bdrv_create_dirty_bitmap(blk_bs(bmds->blk),
350 BLOCK_SIZE, NULL, NULL);
351 aio_context_release(blk_get_aio_context(bmds->blk));
352 if (!bmds->dirty_bitmap) {
353 ret = -errno;
354 goto fail;
355 }
356 }
357 return 0;
358
359 fail:
360 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
361 if (bmds->dirty_bitmap) {
362 aio_context_acquire(blk_get_aio_context(bmds->blk));
363 bdrv_release_dirty_bitmap(blk_bs(bmds->blk), bmds->dirty_bitmap);
364 aio_context_release(blk_get_aio_context(bmds->blk));
365 }
366 }
367 return ret;
368 }
369
370 /* Called with iothread lock taken. */
371
372 static void unset_dirty_tracking(void)
373 {
374 BlkMigDevState *bmds;
375
376 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
377 aio_context_acquire(blk_get_aio_context(bmds->blk));
378 bdrv_release_dirty_bitmap(blk_bs(bmds->blk), bmds->dirty_bitmap);
379 aio_context_release(blk_get_aio_context(bmds->blk));
380 }
381 }
382
383 static int init_blk_migration(QEMUFile *f)
384 {
385 BlockDriverState *bs;
386 BlkMigDevState *bmds;
387 int64_t sectors;
388 BdrvNextIterator it;
389 int i, num_bs = 0;
390 struct {
391 BlkMigDevState *bmds;
392 BlockDriverState *bs;
393 } *bmds_bs;
394 Error *local_err = NULL;
395 int ret;
396
397 block_mig_state.submitted = 0;
398 block_mig_state.read_done = 0;
399 block_mig_state.transferred = 0;
400 block_mig_state.total_sector_sum = 0;
401 block_mig_state.prev_progress = -1;
402 block_mig_state.bulk_completed = 0;
403 block_mig_state.zero_blocks = migrate_zero_blocks();
404
405 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
406 num_bs++;
407 }
408 bmds_bs = g_malloc0(num_bs * sizeof(*bmds_bs));
409
410 for (i = 0, bs = bdrv_first(&it); bs; bs = bdrv_next(&it), i++) {
411 if (bdrv_is_read_only(bs)) {
412 continue;
413 }
414
415 sectors = bdrv_nb_sectors(bs);
416 if (sectors <= 0) {
417 ret = sectors;
418 goto out;
419 }
420
421 bmds = g_new0(BlkMigDevState, 1);
422 bmds->blk = blk_new(BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
423 bmds->blk_name = g_strdup(bdrv_get_device_name(bs));
424 bmds->bulk_completed = 0;
425 bmds->total_sectors = sectors;
426 bmds->completed_sectors = 0;
427 bmds->shared_base = migrate_use_block_incremental();
428
429 assert(i < num_bs);
430 bmds_bs[i].bmds = bmds;
431 bmds_bs[i].bs = bs;
432
433 block_mig_state.total_sector_sum += sectors;
434
435 if (bmds->shared_base) {
436 DPRINTF("Start migration for %s with shared base image\n",
437 bdrv_get_device_name(bs));
438 } else {
439 DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs));
440 }
441
442 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
443 }
444
445 /* Can only insert new BDSes now because doing so while iterating block
446 * devices may end up in a deadlock (iterating the new BDSes, too). */
447 for (i = 0; i < num_bs; i++) {
448 BlkMigDevState *bmds = bmds_bs[i].bmds;
449 BlockDriverState *bs = bmds_bs[i].bs;
450
451 if (bmds) {
452 ret = blk_insert_bs(bmds->blk, bs, &local_err);
453 if (ret < 0) {
454 error_report_err(local_err);
455 goto out;
456 }
457
458 alloc_aio_bitmap(bmds);
459 error_setg(&bmds->blocker, "block device is in use by migration");
460 bdrv_op_block_all(bs, bmds->blocker);
461 }
462 }
463
464 ret = 0;
465 out:
466 g_free(bmds_bs);
467 return ret;
468 }
469
470 /* Called with no lock taken. */
471
472 static int blk_mig_save_bulked_block(QEMUFile *f)
473 {
474 int64_t completed_sector_sum = 0;
475 BlkMigDevState *bmds;
476 int progress;
477 int ret = 0;
478
479 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
480 if (bmds->bulk_completed == 0) {
481 if (mig_save_device_bulk(f, bmds) == 1) {
482 /* completed bulk section for this device */
483 bmds->bulk_completed = 1;
484 }
485 completed_sector_sum += bmds->completed_sectors;
486 ret = 1;
487 break;
488 } else {
489 completed_sector_sum += bmds->completed_sectors;
490 }
491 }
492
493 if (block_mig_state.total_sector_sum != 0) {
494 progress = completed_sector_sum * 100 /
495 block_mig_state.total_sector_sum;
496 } else {
497 progress = 100;
498 }
499 if (progress != block_mig_state.prev_progress) {
500 block_mig_state.prev_progress = progress;
501 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
502 | BLK_MIG_FLAG_PROGRESS);
503 DPRINTF("Completed %d %%\r", progress);
504 }
505
506 return ret;
507 }
508
509 static void blk_mig_reset_dirty_cursor(void)
510 {
511 BlkMigDevState *bmds;
512
513 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
514 bmds->cur_dirty = 0;
515 }
516 }
517
518 /* Called with iothread lock and AioContext taken. */
519
520 static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
521 int is_async)
522 {
523 BlkMigBlock *blk;
524 BlockDriverState *bs = blk_bs(bmds->blk);
525 int64_t total_sectors = bmds->total_sectors;
526 int64_t sector;
527 int nr_sectors;
528 int ret = -EIO;
529
530 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
531 blk_mig_lock();
532 if (bmds_aio_inflight(bmds, sector)) {
533 blk_mig_unlock();
534 blk_drain(bmds->blk);
535 } else {
536 blk_mig_unlock();
537 }
538 if (bdrv_get_dirty(bs, bmds->dirty_bitmap, sector)) {
539
540 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
541 nr_sectors = total_sectors - sector;
542 } else {
543 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
544 }
545 blk = g_new(BlkMigBlock, 1);
546 blk->buf = g_malloc(BLOCK_SIZE);
547 blk->bmds = bmds;
548 blk->sector = sector;
549 blk->nr_sectors = nr_sectors;
550
551 if (is_async) {
552 blk->iov.iov_base = blk->buf;
553 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
554 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
555
556 blk->aiocb = blk_aio_preadv(bmds->blk,
557 sector * BDRV_SECTOR_SIZE,
558 &blk->qiov, 0, blk_mig_read_cb,
559 blk);
560
561 blk_mig_lock();
562 block_mig_state.submitted++;
563 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
564 blk_mig_unlock();
565 } else {
566 ret = blk_pread(bmds->blk, sector * BDRV_SECTOR_SIZE, blk->buf,
567 nr_sectors * BDRV_SECTOR_SIZE);
568 if (ret < 0) {
569 goto error;
570 }
571 blk_send(f, blk);
572
573 g_free(blk->buf);
574 g_free(blk);
575 }
576
577 bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, sector, nr_sectors);
578 sector += nr_sectors;
579 bmds->cur_dirty = sector;
580
581 break;
582 }
583 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
584 bmds->cur_dirty = sector;
585 }
586
587 return (bmds->cur_dirty >= bmds->total_sectors);
588
589 error:
590 DPRINTF("Error reading sector %" PRId64 "\n", sector);
591 g_free(blk->buf);
592 g_free(blk);
593 return ret;
594 }
595
596 /* Called with iothread lock taken.
597 *
598 * return value:
599 * 0: too much data for max_downtime
600 * 1: few enough data for max_downtime
601 */
602 static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
603 {
604 BlkMigDevState *bmds;
605 int ret = 1;
606
607 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
608 aio_context_acquire(blk_get_aio_context(bmds->blk));
609 ret = mig_save_device_dirty(f, bmds, is_async);
610 aio_context_release(blk_get_aio_context(bmds->blk));
611 if (ret <= 0) {
612 break;
613 }
614 }
615
616 return ret;
617 }
618
619 /* Called with no locks taken. */
620
621 static int flush_blks(QEMUFile *f)
622 {
623 BlkMigBlock *blk;
624 int ret = 0;
625
626 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
627 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
628 block_mig_state.transferred);
629
630 blk_mig_lock();
631 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
632 if (qemu_file_rate_limit(f)) {
633 break;
634 }
635 if (blk->ret < 0) {
636 ret = blk->ret;
637 break;
638 }
639
640 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
641 blk_mig_unlock();
642 blk_send(f, blk);
643 blk_mig_lock();
644
645 g_free(blk->buf);
646 g_free(blk);
647
648 block_mig_state.read_done--;
649 block_mig_state.transferred++;
650 assert(block_mig_state.read_done >= 0);
651 }
652 blk_mig_unlock();
653
654 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
655 block_mig_state.submitted, block_mig_state.read_done,
656 block_mig_state.transferred);
657 return ret;
658 }
659
660 /* Called with iothread lock taken. */
661
662 static int64_t get_remaining_dirty(void)
663 {
664 BlkMigDevState *bmds;
665 int64_t dirty = 0;
666
667 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
668 aio_context_acquire(blk_get_aio_context(bmds->blk));
669 dirty += bdrv_get_dirty_count(bmds->dirty_bitmap);
670 aio_context_release(blk_get_aio_context(bmds->blk));
671 }
672
673 return dirty << BDRV_SECTOR_BITS;
674 }
675
676 /* Called with iothread lock taken. */
677
678 static void block_migration_cleanup(void *opaque)
679 {
680 BlkMigDevState *bmds;
681 BlkMigBlock *blk;
682 AioContext *ctx;
683
684 bdrv_drain_all();
685
686 unset_dirty_tracking();
687
688 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
689 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
690 bdrv_op_unblock_all(blk_bs(bmds->blk), bmds->blocker);
691 error_free(bmds->blocker);
692
693 /* Save ctx, because bmds->blk can disappear during blk_unref. */
694 ctx = blk_get_aio_context(bmds->blk);
695 aio_context_acquire(ctx);
696 blk_unref(bmds->blk);
697 aio_context_release(ctx);
698
699 g_free(bmds->blk_name);
700 g_free(bmds->aio_bitmap);
701 g_free(bmds);
702 }
703
704 blk_mig_lock();
705 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
706 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
707 g_free(blk->buf);
708 g_free(blk);
709 }
710 blk_mig_unlock();
711 }
712
713 static int block_save_setup(QEMUFile *f, void *opaque)
714 {
715 int ret;
716
717 DPRINTF("Enter save live setup submitted %d transferred %d\n",
718 block_mig_state.submitted, block_mig_state.transferred);
719
720 qemu_mutex_lock_iothread();
721 ret = init_blk_migration(f);
722 if (ret < 0) {
723 qemu_mutex_unlock_iothread();
724 return ret;
725 }
726
727 /* start track dirty blocks */
728 ret = set_dirty_tracking();
729
730 qemu_mutex_unlock_iothread();
731
732 if (ret) {
733 return ret;
734 }
735
736 ret = flush_blks(f);
737 blk_mig_reset_dirty_cursor();
738 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
739
740 return ret;
741 }
742
743 static int block_save_iterate(QEMUFile *f, void *opaque)
744 {
745 int ret;
746 int64_t last_ftell = qemu_ftell(f);
747 int64_t delta_ftell;
748
749 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
750 block_mig_state.submitted, block_mig_state.transferred);
751
752 ret = flush_blks(f);
753 if (ret) {
754 return ret;
755 }
756
757 blk_mig_reset_dirty_cursor();
758
759 /* control the rate of transfer */
760 blk_mig_lock();
761 while ((block_mig_state.submitted +
762 block_mig_state.read_done) * BLOCK_SIZE <
763 qemu_file_get_rate_limit(f) &&
764 (block_mig_state.submitted +
765 block_mig_state.read_done) <
766 MAX_INFLIGHT_IO) {
767 blk_mig_unlock();
768 if (block_mig_state.bulk_completed == 0) {
769 /* first finish the bulk phase */
770 if (blk_mig_save_bulked_block(f) == 0) {
771 /* finished saving bulk on all devices */
772 block_mig_state.bulk_completed = 1;
773 }
774 ret = 0;
775 } else {
776 /* Always called with iothread lock taken for
777 * simplicity, block_save_complete also calls it.
778 */
779 qemu_mutex_lock_iothread();
780 ret = blk_mig_save_dirty_block(f, 1);
781 qemu_mutex_unlock_iothread();
782 }
783 if (ret < 0) {
784 return ret;
785 }
786 blk_mig_lock();
787 if (ret != 0) {
788 /* no more dirty blocks */
789 break;
790 }
791 }
792 blk_mig_unlock();
793
794 ret = flush_blks(f);
795 if (ret) {
796 return ret;
797 }
798
799 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
800 delta_ftell = qemu_ftell(f) - last_ftell;
801 if (delta_ftell > 0) {
802 return 1;
803 } else if (delta_ftell < 0) {
804 return -1;
805 } else {
806 return 0;
807 }
808 }
809
810 /* Called with iothread lock taken. */
811
812 static int block_save_complete(QEMUFile *f, void *opaque)
813 {
814 int ret;
815
816 DPRINTF("Enter save live complete submitted %d transferred %d\n",
817 block_mig_state.submitted, block_mig_state.transferred);
818
819 ret = flush_blks(f);
820 if (ret) {
821 return ret;
822 }
823
824 blk_mig_reset_dirty_cursor();
825
826 /* we know for sure that save bulk is completed and
827 all async read completed */
828 blk_mig_lock();
829 assert(block_mig_state.submitted == 0);
830 blk_mig_unlock();
831
832 do {
833 ret = blk_mig_save_dirty_block(f, 0);
834 if (ret < 0) {
835 return ret;
836 }
837 } while (ret == 0);
838
839 /* report completion */
840 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
841
842 DPRINTF("Block migration completed\n");
843
844 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
845
846 return 0;
847 }
848
849 static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
850 uint64_t *non_postcopiable_pending,
851 uint64_t *postcopiable_pending)
852 {
853 /* Estimate pending number of bytes to send */
854 uint64_t pending;
855
856 qemu_mutex_lock_iothread();
857 pending = get_remaining_dirty();
858 qemu_mutex_unlock_iothread();
859
860 blk_mig_lock();
861 pending += block_mig_state.submitted * BLOCK_SIZE +
862 block_mig_state.read_done * BLOCK_SIZE;
863 blk_mig_unlock();
864
865 /* Report at least one block pending during bulk phase */
866 if (pending <= max_size && !block_mig_state.bulk_completed) {
867 pending = max_size + BLOCK_SIZE;
868 }
869
870 DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
871 /* We don't do postcopy */
872 *non_postcopiable_pending += pending;
873 }
874
875 static int block_load(QEMUFile *f, void *opaque, int version_id)
876 {
877 static int banner_printed;
878 int len, flags;
879 char device_name[256];
880 int64_t addr;
881 BlockBackend *blk, *blk_prev = NULL;;
882 Error *local_err = NULL;
883 uint8_t *buf;
884 int64_t total_sectors = 0;
885 int nr_sectors;
886 int ret;
887 BlockDriverInfo bdi;
888 int cluster_size = BLOCK_SIZE;
889
890 do {
891 addr = qemu_get_be64(f);
892
893 flags = addr & ~BDRV_SECTOR_MASK;
894 addr >>= BDRV_SECTOR_BITS;
895
896 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
897 /* get device name */
898 len = qemu_get_byte(f);
899 qemu_get_buffer(f, (uint8_t *)device_name, len);
900 device_name[len] = '\0';
901
902 blk = blk_by_name(device_name);
903 if (!blk) {
904 fprintf(stderr, "Error unknown block device %s\n",
905 device_name);
906 return -EINVAL;
907 }
908
909 if (blk != blk_prev) {
910 blk_prev = blk;
911 total_sectors = blk_nb_sectors(blk);
912 if (total_sectors <= 0) {
913 error_report("Error getting length of block device %s",
914 device_name);
915 return -EINVAL;
916 }
917
918 blk_invalidate_cache(blk, &local_err);
919 if (local_err) {
920 error_report_err(local_err);
921 return -EINVAL;
922 }
923
924 ret = bdrv_get_info(blk_bs(blk), &bdi);
925 if (ret == 0 && bdi.cluster_size > 0 &&
926 bdi.cluster_size <= BLOCK_SIZE &&
927 BLOCK_SIZE % bdi.cluster_size == 0) {
928 cluster_size = bdi.cluster_size;
929 } else {
930 cluster_size = BLOCK_SIZE;
931 }
932 }
933
934 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
935 nr_sectors = total_sectors - addr;
936 } else {
937 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
938 }
939
940 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
941 ret = blk_pwrite_zeroes(blk, addr * BDRV_SECTOR_SIZE,
942 nr_sectors * BDRV_SECTOR_SIZE,
943 BDRV_REQ_MAY_UNMAP);
944 } else {
945 int i;
946 int64_t cur_addr;
947 uint8_t *cur_buf;
948
949 buf = g_malloc(BLOCK_SIZE);
950 qemu_get_buffer(f, buf, BLOCK_SIZE);
951 for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
952 cur_addr = addr * BDRV_SECTOR_SIZE + i * cluster_size;
953 cur_buf = buf + i * cluster_size;
954
955 if ((!block_mig_state.zero_blocks ||
956 cluster_size < BLOCK_SIZE) &&
957 buffer_is_zero(cur_buf, cluster_size)) {
958 ret = blk_pwrite_zeroes(blk, cur_addr,
959 cluster_size,
960 BDRV_REQ_MAY_UNMAP);
961 } else {
962 ret = blk_pwrite(blk, cur_addr, cur_buf,
963 cluster_size, 0);
964 }
965 if (ret < 0) {
966 break;
967 }
968 }
969 g_free(buf);
970 }
971
972 if (ret < 0) {
973 return ret;
974 }
975 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
976 if (!banner_printed) {
977 printf("Receiving block device images\n");
978 banner_printed = 1;
979 }
980 printf("Completed %d %%%c", (int)addr,
981 (addr == 100) ? '\n' : '\r');
982 fflush(stdout);
983 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
984 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
985 return -EINVAL;
986 }
987 ret = qemu_file_get_error(f);
988 if (ret != 0) {
989 return ret;
990 }
991 } while (!(flags & BLK_MIG_FLAG_EOS));
992
993 return 0;
994 }
995
996 static bool block_is_active(void *opaque)
997 {
998 return migrate_use_block();
999 }
1000
1001 static SaveVMHandlers savevm_block_handlers = {
1002 .save_live_setup = block_save_setup,
1003 .save_live_iterate = block_save_iterate,
1004 .save_live_complete_precopy = block_save_complete,
1005 .save_live_pending = block_save_pending,
1006 .load_state = block_load,
1007 .cleanup = block_migration_cleanup,
1008 .is_active = block_is_active,
1009 };
1010
1011 void blk_mig_init(void)
1012 {
1013 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
1014 QSIMPLEQ_INIT(&block_mig_state.blk_list);
1015 qemu_mutex_init(&block_mig_state.lock);
1016
1017 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
1018 &block_mig_state);
1019 }