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