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