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