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