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