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