]> Git Repo - qemu.git/blame - migration/block.c
block: introduce dirty_bitmap_mutex
[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"
da34e65c 17#include "qapi/error.h"
bfb197e0 18#include "qemu/error-report.h"
f348b6d1 19#include "qemu/cutils.h"
1de7afc9 20#include "qemu/queue.h"
2c9e6fec
JQ
21#include "block.h"
22#include "migration/misc.h"
6666c96a 23#include "migration.h"
f2a8f0a6 24#include "migration/register.h"
08a0aee1 25#include "qemu-file.h"
987772d9 26#include "migration/vmstate.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. */
ebd2f9e7
KW
53 BlockBackend *blk;
54 char *blk_name;
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 {
5e5328be 95 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
323920c4 96 int64_t total_sector_sum;
323004a3 97 bool zero_blocks;
323920c4 98
52e850de 99 /* Protected by lock. */
5e5328be 100 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
c163b5ca
LS
101 int submitted;
102 int read_done;
323920c4
PB
103
104 /* Only used by migration thread. Does not need a lock. */
c163b5ca 105 int transferred;
01e61e2d 106 int prev_progress;
e970ec0b 107 int bulk_completed;
52e850de 108
ef0716df 109 /* Lock must be taken _inside_ the iothread lock and any AioContexts. */
52e850de 110 QemuMutex lock;
c163b5ca
LS
111} BlkMigState;
112
d11ecd3d 113static BlkMigState block_mig_state;
c163b5ca 114
52e850de
PB
115static void blk_mig_lock(void)
116{
117 qemu_mutex_lock(&block_mig_state.lock);
118}
119
120static void blk_mig_unlock(void)
121{
122 qemu_mutex_unlock(&block_mig_state.lock);
123}
124
32c835ba
PB
125/* Must run outside of the iothread lock during the bulk phase,
126 * or the VM will stall.
127 */
128
13f0b67f
JK
129static void blk_send(QEMUFile *f, BlkMigBlock * blk)
130{
131 int len;
323004a3
PL
132 uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK;
133
134 if (block_mig_state.zero_blocks &&
135 buffer_is_zero(blk->buf, BLOCK_SIZE)) {
136 flags |= BLK_MIG_FLAG_ZERO_BLOCK;
137 }
13f0b67f
JK
138
139 /* sector number and flags */
140 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
323004a3 141 | flags);
13f0b67f
JK
142
143 /* device name */
ebd2f9e7 144 len = strlen(blk->bmds->blk_name);
13f0b67f 145 qemu_put_byte(f, len);
ebd2f9e7 146 qemu_put_buffer(f, (uint8_t *) blk->bmds->blk_name, len);
13f0b67f 147
323004a3
PL
148 /* if a block is zero we need to flush here since the network
149 * bandwidth is now a lot higher than the storage device bandwidth.
150 * thus if we queue zero blocks we slow down the migration */
151 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
152 qemu_fflush(f);
153 return;
154 }
155
13f0b67f
JK
156 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
157}
158
25f23643
JK
159int blk_mig_active(void)
160{
161 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
162}
163
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
ebd2f9e7 200 if (sector < blk_nb_sectors(bmds->blk)) {
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{
ebd2f9e7 234 BlockBackend *bb = bmds->blk;
33656af7
MT
235 int64_t bitmap_size;
236
ebd2f9e7 237 bitmap_size = blk_nb_sectors(bb) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
33656af7
MT
238 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
239
7267c094 240 bmds->aio_bitmap = g_malloc0(bitmap_size);
33656af7
MT
241}
242
52e850de
PB
243/* Never hold migration lock when yielding to the main loop! */
244
c163b5ca
LS
245static void blk_mig_read_cb(void *opaque, int ret)
246{
247 BlkMigBlock *blk = opaque;
a55eb92c 248
52e850de 249 blk_mig_lock();
c163b5ca 250 blk->ret = ret;
a55eb92c 251
5e5328be 252 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
33656af7 253 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
a55eb92c 254
d11ecd3d
JK
255 block_mig_state.submitted--;
256 block_mig_state.read_done++;
257 assert(block_mig_state.submitted >= 0);
52e850de 258 blk_mig_unlock();
c163b5ca
LS
259}
260
32c835ba
PB
261/* Called with no lock taken. */
262
539de124 263static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
a55eb92c 264{
57cce12d
JK
265 int64_t total_sectors = bmds->total_sectors;
266 int64_t cur_sector = bmds->cur_sector;
ebd2f9e7 267 BlockBackend *bb = bmds->blk;
c163b5ca 268 BlkMigBlock *blk;
13f0b67f 269 int nr_sectors;
a55eb92c 270
57cce12d 271 if (bmds->shared_base) {
32c835ba 272 qemu_mutex_lock_iothread();
ebd2f9e7 273 aio_context_acquire(blk_get_aio_context(bb));
7d66b1fb
EB
274 /* Skip unallocated sectors; intentionally treats failure as
275 * an allocated sector */
b1d10856 276 while (cur_sector < total_sectors &&
ebd2f9e7
KW
277 !bdrv_is_allocated(blk_bs(bb), cur_sector,
278 MAX_IS_ALLOCATED_SEARCH, &nr_sectors)) {
c163b5ca
LS
279 cur_sector += nr_sectors;
280 }
ebd2f9e7 281 aio_context_release(blk_get_aio_context(bb));
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();
ebd2f9e7
KW
324 aio_context_acquire(blk_get_aio_context(bmds->blk));
325 blk->aiocb = blk_aio_preadv(bb, cur_sector * BDRV_SECTOR_SIZE, &blk->qiov,
326 0, blk_mig_read_cb, blk);
d76cac7d 327
20dca810 328 bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector, nr_sectors);
ebd2f9e7 329 aio_context_release(blk_get_aio_context(bmds->blk));
32c835ba 330 qemu_mutex_unlock_iothread();
a55eb92c 331
32c835ba 332 bmds->cur_sector = cur_sector + nr_sectors;
13f0b67f 333 return (bmds->cur_sector >= total_sectors);
c163b5ca
LS
334}
335
32c835ba
PB
336/* Called with iothread lock taken. */
337
b8afb520 338static int set_dirty_tracking(void)
c163b5ca
LS
339{
340 BlkMigDevState *bmds;
b8afb520
FZ
341 int ret;
342
343 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
ebd2f9e7
KW
344 bmds->dirty_bitmap = bdrv_create_dirty_bitmap(blk_bs(bmds->blk),
345 BLOCK_SIZE, NULL, NULL);
b8afb520
FZ
346 if (!bmds->dirty_bitmap) {
347 ret = -errno;
348 goto fail;
349 }
350 }
351 return 0;
5e5328be 352
b8afb520 353fail:
5e5328be 354 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
b8afb520 355 if (bmds->dirty_bitmap) {
ebd2f9e7 356 bdrv_release_dirty_bitmap(blk_bs(bmds->blk), bmds->dirty_bitmap);
b8afb520 357 }
e4654d2d 358 }
b8afb520 359 return ret;
e4654d2d
FZ
360}
361
ef0716df
PB
362/* Called with iothread lock taken. */
363
e4654d2d
FZ
364static void unset_dirty_tracking(void)
365{
366 BlkMigDevState *bmds;
367
368 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
ebd2f9e7 369 bdrv_release_dirty_bitmap(blk_bs(bmds->blk), bmds->dirty_bitmap);
c163b5ca 370 }
c163b5ca
LS
371}
372
6f5ef23a 373static int init_blk_migration(QEMUFile *f)
c163b5ca 374{
fea68bb6 375 BlockDriverState *bs;
5e5328be 376 BlkMigDevState *bmds;
792773b2 377 int64_t sectors;
88be7b4b 378 BdrvNextIterator it;
ebd2f9e7
KW
379 int i, num_bs = 0;
380 struct {
381 BlkMigDevState *bmds;
382 BlockDriverState *bs;
383 } *bmds_bs;
6f5ef23a
KW
384 Error *local_err = NULL;
385 int ret;
a55eb92c 386
fea68bb6
MA
387 block_mig_state.submitted = 0;
388 block_mig_state.read_done = 0;
389 block_mig_state.transferred = 0;
390 block_mig_state.total_sector_sum = 0;
391 block_mig_state.prev_progress = -1;
392 block_mig_state.bulk_completed = 0;
393 block_mig_state.zero_blocks = migrate_zero_blocks();
394
88be7b4b 395 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
ebd2f9e7
KW
396 num_bs++;
397 }
398 bmds_bs = g_malloc0(num_bs * sizeof(*bmds_bs));
399
400 for (i = 0, bs = bdrv_first(&it); bs; bs = bdrv_next(&it), i++) {
fea68bb6
MA
401 if (bdrv_is_read_only(bs)) {
402 continue;
403 }
404
57322b78 405 sectors = bdrv_nb_sectors(bs);
31f54f24 406 if (sectors <= 0) {
6f5ef23a 407 ret = sectors;
ebd2f9e7 408 goto out;
b66460e4
SH
409 }
410
5839e53b 411 bmds = g_new0(BlkMigDevState, 1);
6f5ef23a 412 bmds->blk = blk_new(BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
ebd2f9e7 413 bmds->blk_name = g_strdup(bdrv_get_device_name(bs));
b66460e4
SH
414 bmds->bulk_completed = 0;
415 bmds->total_sectors = sectors;
416 bmds->completed_sectors = 0;
ce7c817c 417 bmds->shared_base = migrate_use_block_incremental();
ebd2f9e7
KW
418
419 assert(i < num_bs);
420 bmds_bs[i].bmds = bmds;
421 bmds_bs[i].bs = bs;
b66460e4
SH
422
423 block_mig_state.total_sector_sum += sectors;
424
425 if (bmds->shared_base) {
539de124 426 DPRINTF("Start migration for %s with shared base image\n",
bfb197e0 427 bdrv_get_device_name(bs));
b66460e4 428 } else {
bfb197e0 429 DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs));
b66460e4
SH
430 }
431
432 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
433 }
ebd2f9e7
KW
434
435 /* Can only insert new BDSes now because doing so while iterating block
436 * devices may end up in a deadlock (iterating the new BDSes, too). */
437 for (i = 0; i < num_bs; i++) {
438 BlkMigDevState *bmds = bmds_bs[i].bmds;
439 BlockDriverState *bs = bmds_bs[i].bs;
440
441 if (bmds) {
6f5ef23a
KW
442 ret = blk_insert_bs(bmds->blk, bs, &local_err);
443 if (ret < 0) {
444 error_report_err(local_err);
445 goto out;
446 }
ebd2f9e7
KW
447
448 alloc_aio_bitmap(bmds);
449 error_setg(&bmds->blocker, "block device is in use by migration");
450 bdrv_op_block_all(bs, bmds->blocker);
451 }
452 }
453
6f5ef23a 454 ret = 0;
ebd2f9e7
KW
455out:
456 g_free(bmds_bs);
6f5ef23a 457 return ret;
b66460e4
SH
458}
459
32c835ba
PB
460/* Called with no lock taken. */
461
539de124 462static int blk_mig_save_bulked_block(QEMUFile *f)
c163b5ca 463{
82801d8f 464 int64_t completed_sector_sum = 0;
c163b5ca 465 BlkMigDevState *bmds;
01e61e2d 466 int progress;
82801d8f 467 int ret = 0;
c163b5ca 468
5e5328be 469 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 470 if (bmds->bulk_completed == 0) {
539de124 471 if (mig_save_device_bulk(f, bmds) == 1) {
57cce12d
JK
472 /* completed bulk section for this device */
473 bmds->bulk_completed = 1;
c163b5ca 474 }
82801d8f
JK
475 completed_sector_sum += bmds->completed_sectors;
476 ret = 1;
477 break;
478 } else {
479 completed_sector_sum += bmds->completed_sectors;
c163b5ca
LS
480 }
481 }
a55eb92c 482
8b6b2afc
PR
483 if (block_mig_state.total_sector_sum != 0) {
484 progress = completed_sector_sum * 100 /
485 block_mig_state.total_sector_sum;
486 } else {
487 progress = 100;
488 }
01e61e2d
JK
489 if (progress != block_mig_state.prev_progress) {
490 block_mig_state.prev_progress = progress;
491 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
492 | BLK_MIG_FLAG_PROGRESS);
539de124 493 DPRINTF("Completed %d %%\r", progress);
82801d8f
JK
494 }
495
496 return ret;
c163b5ca
LS
497}
498
d76cac7d 499static void blk_mig_reset_dirty_cursor(void)
c163b5ca
LS
500{
501 BlkMigDevState *bmds;
d76cac7d
LS
502
503 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
504 bmds->cur_dirty = 0;
505 }
506}
507
ef0716df 508/* Called with iothread lock and AioContext taken. */
32c835ba 509
539de124
LC
510static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
511 int is_async)
d76cac7d
LS
512{
513 BlkMigBlock *blk;
ebd2f9e7 514 BlockDriverState *bs = blk_bs(bmds->blk);
d76cac7d 515 int64_t total_sectors = bmds->total_sectors;
c163b5ca 516 int64_t sector;
d76cac7d 517 int nr_sectors;
dcd1d224 518 int ret = -EIO;
a55eb92c 519
d76cac7d 520 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
52e850de 521 blk_mig_lock();
62155e2b 522 if (bmds_aio_inflight(bmds, sector)) {
52e850de 523 blk_mig_unlock();
ebd2f9e7 524 blk_drain(bmds->blk);
52e850de
PB
525 } else {
526 blk_mig_unlock();
62155e2b 527 }
ebd2f9e7 528 if (bdrv_get_dirty(bs, bmds->dirty_bitmap, sector)) {
575a58d7 529
d76cac7d
LS
530 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
531 nr_sectors = total_sectors - sector;
532 } else {
533 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
534 }
5839e53b 535 blk = g_new(BlkMigBlock, 1);
7267c094 536 blk->buf = g_malloc(BLOCK_SIZE);
d76cac7d
LS
537 blk->bmds = bmds;
538 blk->sector = sector;
33656af7 539 blk->nr_sectors = nr_sectors;
d76cac7d 540
889ae39c 541 if (is_async) {
d76cac7d
LS
542 blk->iov.iov_base = blk->buf;
543 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
544 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
545
ebd2f9e7
KW
546 blk->aiocb = blk_aio_preadv(bmds->blk,
547 sector * BDRV_SECTOR_SIZE,
548 &blk->qiov, 0, blk_mig_read_cb,
549 blk);
52e850de
PB
550
551 blk_mig_lock();
d76cac7d 552 block_mig_state.submitted++;
33656af7 553 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
52e850de 554 blk_mig_unlock();
d76cac7d 555 } else {
ebd2f9e7
KW
556 ret = blk_pread(bmds->blk, sector * BDRV_SECTOR_SIZE, blk->buf,
557 nr_sectors * BDRV_SECTOR_SIZE);
dcd1d224 558 if (ret < 0) {
d76cac7d 559 goto error;
c163b5ca 560 }
d76cac7d 561 blk_send(f, blk);
a55eb92c 562
7267c094
AL
563 g_free(blk->buf);
564 g_free(blk);
a55eb92c 565 }
d76cac7d 566
20dca810 567 bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, sector, nr_sectors);
1cf6aa74
LC
568 sector += nr_sectors;
569 bmds->cur_dirty = sector;
570
d76cac7d 571 break;
c163b5ca 572 }
d76cac7d
LS
573 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
574 bmds->cur_dirty = sector;
c163b5ca 575 }
575a58d7 576
d76cac7d
LS
577 return (bmds->cur_dirty >= bmds->total_sectors);
578
889ae39c 579error:
539de124 580 DPRINTF("Error reading sector %" PRId64 "\n", sector);
7267c094
AL
581 g_free(blk->buf);
582 g_free(blk);
43be3a25 583 return ret;
d76cac7d
LS
584}
585
32c835ba
PB
586/* Called with iothread lock taken.
587 *
588 * return value:
ceb2bd09
JQ
589 * 0: too much data for max_downtime
590 * 1: few enough data for max_downtime
591*/
539de124 592static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
d76cac7d
LS
593{
594 BlkMigDevState *bmds;
ceb2bd09 595 int ret = 1;
d76cac7d
LS
596
597 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
ebd2f9e7 598 aio_context_acquire(blk_get_aio_context(bmds->blk));
ceb2bd09 599 ret = mig_save_device_dirty(f, bmds, is_async);
ebd2f9e7 600 aio_context_release(blk_get_aio_context(bmds->blk));
43be3a25 601 if (ret <= 0) {
d76cac7d
LS
602 break;
603 }
604 }
605
606 return ret;
c163b5ca
LS
607}
608
32c835ba
PB
609/* Called with no locks taken. */
610
59feec42 611static int flush_blks(QEMUFile *f)
c163b5ca 612{
5e5328be 613 BlkMigBlock *blk;
59feec42 614 int ret = 0;
a55eb92c 615
d0f2c4c6 616 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
d11ecd3d
JK
617 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
618 block_mig_state.transferred);
a55eb92c 619
52e850de 620 blk_mig_lock();
5e5328be
JK
621 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
622 if (qemu_file_rate_limit(f)) {
623 break;
624 }
4b640365 625 if (blk->ret < 0) {
59feec42 626 ret = blk->ret;
4b640365
JK
627 break;
628 }
a55eb92c 629
5e5328be 630 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
52e850de 631 blk_mig_unlock();
13197e3c 632 blk_send(f, blk);
52e850de 633 blk_mig_lock();
13197e3c 634
7267c094
AL
635 g_free(blk->buf);
636 g_free(blk);
a55eb92c 637
d11ecd3d
JK
638 block_mig_state.read_done--;
639 block_mig_state.transferred++;
640 assert(block_mig_state.read_done >= 0);
c163b5ca 641 }
52e850de 642 blk_mig_unlock();
c163b5ca 643
d0f2c4c6 644 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
d11ecd3d
JK
645 block_mig_state.submitted, block_mig_state.read_done,
646 block_mig_state.transferred);
59feec42 647 return ret;
c163b5ca
LS
648}
649
32c835ba
PB
650/* Called with iothread lock taken. */
651
889ae39c
LS
652static int64_t get_remaining_dirty(void)
653{
654 BlkMigDevState *bmds;
655 int64_t dirty = 0;
656
657 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
ebd2f9e7 658 aio_context_acquire(blk_get_aio_context(bmds->blk));
20dca810 659 dirty += bdrv_get_dirty_count(bmds->dirty_bitmap);
ebd2f9e7 660 aio_context_release(blk_get_aio_context(bmds->blk));
889ae39c
LS
661 }
662
acc906c6 663 return dirty << BDRV_SECTOR_BITS;
889ae39c
LS
664}
665
32c835ba 666
362fdf17
KW
667
668/* Called with iothread lock taken. */
669static void block_migration_cleanup_bmds(void)
4ec7fcc7 670{
82801d8f 671 BlkMigDevState *bmds;
ef0716df 672 AioContext *ctx;
4ec7fcc7 673
e4654d2d 674 unset_dirty_tracking();
8f794c55 675
82801d8f
JK
676 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
677 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
ebd2f9e7 678 bdrv_op_unblock_all(blk_bs(bmds->blk), bmds->blocker);
3718d8ab 679 error_free(bmds->blocker);
ef0716df 680
ebd2f9e7
KW
681 /* Save ctx, because bmds->blk can disappear during blk_unref. */
682 ctx = blk_get_aio_context(bmds->blk);
ef0716df 683 aio_context_acquire(ctx);
ebd2f9e7 684 blk_unref(bmds->blk);
ef0716df
PB
685 aio_context_release(ctx);
686
ebd2f9e7 687 g_free(bmds->blk_name);
7267c094
AL
688 g_free(bmds->aio_bitmap);
689 g_free(bmds);
4ec7fcc7 690 }
362fdf17
KW
691}
692
693/* Called with iothread lock taken. */
694static void block_migration_cleanup(void *opaque)
695{
696 BlkMigBlock *blk;
697
698 bdrv_drain_all();
699
700 block_migration_cleanup_bmds();
4ec7fcc7 701
ef0716df 702 blk_mig_lock();
82801d8f
JK
703 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
704 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
7267c094
AL
705 g_free(blk->buf);
706 g_free(blk);
4ec7fcc7 707 }
52e850de 708 blk_mig_unlock();
4ec7fcc7
JK
709}
710
d1315aac 711static int block_save_setup(QEMUFile *f, void *opaque)
c163b5ca 712{
2975725f
JQ
713 int ret;
714
d1315aac
JQ
715 DPRINTF("Enter save live setup submitted %d transferred %d\n",
716 block_mig_state.submitted, block_mig_state.transferred);
a55eb92c 717
9b095037 718 qemu_mutex_lock_iothread();
6f5ef23a
KW
719 ret = init_blk_migration(f);
720 if (ret < 0) {
721 qemu_mutex_unlock_iothread();
722 return ret;
723 }
d1315aac
JQ
724
725 /* start track dirty blocks */
b8afb520
FZ
726 ret = set_dirty_tracking();
727
ef0716df
PB
728 qemu_mutex_unlock_iothread();
729
b8afb520 730 if (ret) {
b8afb520
FZ
731 return ret;
732 }
733
59feec42 734 ret = flush_blks(f);
d1315aac 735 blk_mig_reset_dirty_cursor();
d1315aac
JQ
736 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
737
d418cf57 738 return ret;
d1315aac
JQ
739}
740
16310a3c 741static int block_save_iterate(QEMUFile *f, void *opaque)
d1315aac
JQ
742{
743 int ret;
6aaa9dae 744 int64_t last_ftell = qemu_ftell(f);
ebd9fbd7 745 int64_t delta_ftell;
d1315aac 746
16310a3c
JQ
747 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
748 block_mig_state.submitted, block_mig_state.transferred);
d1315aac 749
59feec42 750 ret = flush_blks(f);
2975725f 751 if (ret) {
2975725f 752 return ret;
4b640365
JK
753 }
754
d76cac7d
LS
755 blk_mig_reset_dirty_cursor();
756
16310a3c 757 /* control the rate of transfer */
52e850de 758 blk_mig_lock();
16310a3c
JQ
759 while ((block_mig_state.submitted +
760 block_mig_state.read_done) * BLOCK_SIZE <
f77dcdbc
WC
761 qemu_file_get_rate_limit(f) &&
762 (block_mig_state.submitted +
763 block_mig_state.read_done) <
764 MAX_INFLIGHT_IO) {
52e850de 765 blk_mig_unlock();
16310a3c
JQ
766 if (block_mig_state.bulk_completed == 0) {
767 /* first finish the bulk phase */
768 if (blk_mig_save_bulked_block(f) == 0) {
769 /* finished saving bulk on all devices */
770 block_mig_state.bulk_completed = 1;
771 }
13197e3c 772 ret = 0;
16310a3c 773 } else {
32c835ba
PB
774 /* Always called with iothread lock taken for
775 * simplicity, block_save_complete also calls it.
776 */
777 qemu_mutex_lock_iothread();
43be3a25 778 ret = blk_mig_save_dirty_block(f, 1);
32c835ba 779 qemu_mutex_unlock_iothread();
13197e3c
PB
780 }
781 if (ret < 0) {
782 return ret;
783 }
52e850de 784 blk_mig_lock();
13197e3c
PB
785 if (ret != 0) {
786 /* no more dirty blocks */
787 break;
a55eb92c 788 }
16310a3c 789 }
52e850de 790 blk_mig_unlock();
a55eb92c 791
59feec42 792 ret = flush_blks(f);
16310a3c 793 if (ret) {
16310a3c 794 return ret;
4b640365
JK
795 }
796
16310a3c 797 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
ebd9fbd7
GH
798 delta_ftell = qemu_ftell(f) - last_ftell;
799 if (delta_ftell > 0) {
800 return 1;
801 } else if (delta_ftell < 0) {
802 return -1;
803 } else {
804 return 0;
805 }
16310a3c
JQ
806}
807
32c835ba
PB
808/* Called with iothread lock taken. */
809
16310a3c
JQ
810static int block_save_complete(QEMUFile *f, void *opaque)
811{
812 int ret;
813
814 DPRINTF("Enter save live complete submitted %d transferred %d\n",
815 block_mig_state.submitted, block_mig_state.transferred);
816
59feec42 817 ret = flush_blks(f);
16310a3c 818 if (ret) {
16310a3c
JQ
819 return ret;
820 }
a55eb92c 821
16310a3c 822 blk_mig_reset_dirty_cursor();
01e61e2d 823
16310a3c
JQ
824 /* we know for sure that save bulk is completed and
825 all async read completed */
52e850de 826 blk_mig_lock();
16310a3c 827 assert(block_mig_state.submitted == 0);
52e850de 828 blk_mig_unlock();
16310a3c 829
43be3a25
JQ
830 do {
831 ret = blk_mig_save_dirty_block(f, 0);
d418cf57
PB
832 if (ret < 0) {
833 return ret;
834 }
43be3a25 835 } while (ret == 0);
4b640365 836
43be3a25
JQ
837 /* report completion */
838 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
a55eb92c 839
16310a3c
JQ
840 DPRINTF("Block migration completed\n");
841
a55eb92c
JK
842 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
843
362fdf17
KW
844 /* Make sure that our BlockBackends are gone, so that the block driver
845 * nodes can be inactivated. */
846 block_migration_cleanup_bmds();
847
16310a3c 848 return 0;
c163b5ca
LS
849}
850
c31b098f
DDAG
851static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
852 uint64_t *non_postcopiable_pending,
853 uint64_t *postcopiable_pending)
e4ed1541 854{
6aaa9dae 855 /* Estimate pending number of bytes to send */
13197e3c
PB
856 uint64_t pending;
857
32c835ba 858 qemu_mutex_lock_iothread();
ef0716df
PB
859 pending = get_remaining_dirty();
860 qemu_mutex_unlock_iothread();
861
52e850de 862 blk_mig_lock();
ef0716df
PB
863 pending += block_mig_state.submitted * BLOCK_SIZE +
864 block_mig_state.read_done * BLOCK_SIZE;
865 blk_mig_unlock();
6aaa9dae
SH
866
867 /* Report at least one block pending during bulk phase */
04636dc4
VSO
868 if (pending <= max_size && !block_mig_state.bulk_completed) {
869 pending = max_size + BLOCK_SIZE;
6aaa9dae 870 }
e4ed1541 871
6aaa9dae 872 DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
c31b098f
DDAG
873 /* We don't do postcopy */
874 *non_postcopiable_pending += pending;
e4ed1541
JQ
875}
876
c163b5ca
LS
877static int block_load(QEMUFile *f, void *opaque, int version_id)
878{
01e61e2d 879 static int banner_printed;
c163b5ca
LS
880 int len, flags;
881 char device_name[256];
882 int64_t addr;
ad2964b4 883 BlockBackend *blk, *blk_prev = NULL;;
9bd9c7f5 884 Error *local_err = NULL;
c163b5ca 885 uint8_t *buf;
77358b59
PR
886 int64_t total_sectors = 0;
887 int nr_sectors;
42802d47 888 int ret;
3928d50b
LC
889 BlockDriverInfo bdi;
890 int cluster_size = BLOCK_SIZE;
a55eb92c 891
c163b5ca 892 do {
c163b5ca 893 addr = qemu_get_be64(f);
a55eb92c 894
6ea44308
JK
895 flags = addr & ~BDRV_SECTOR_MASK;
896 addr >>= BDRV_SECTOR_BITS;
a55eb92c
JK
897
898 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
c163b5ca
LS
899 /* get device name */
900 len = qemu_get_byte(f);
c163b5ca
LS
901 qemu_get_buffer(f, (uint8_t *)device_name, len);
902 device_name[len] = '\0';
a55eb92c 903
c9ebaf74
FZ
904 blk = blk_by_name(device_name);
905 if (!blk) {
4b640365
JK
906 fprintf(stderr, "Error unknown block device %s\n",
907 device_name);
908 return -EINVAL;
909 }
a55eb92c 910
ad2964b4
KW
911 if (blk != blk_prev) {
912 blk_prev = blk;
913 total_sectors = blk_nb_sectors(blk);
77358b59 914 if (total_sectors <= 0) {
6daf194d 915 error_report("Error getting length of block device %s",
77358b59
PR
916 device_name);
917 return -EINVAL;
918 }
9bd9c7f5 919
ad2964b4 920 blk_invalidate_cache(blk, &local_err);
9bd9c7f5
KW
921 if (local_err) {
922 error_report_err(local_err);
923 return -EINVAL;
924 }
3928d50b
LC
925
926 ret = bdrv_get_info(blk_bs(blk), &bdi);
927 if (ret == 0 && bdi.cluster_size > 0 &&
928 bdi.cluster_size <= BLOCK_SIZE &&
929 BLOCK_SIZE % bdi.cluster_size == 0) {
930 cluster_size = bdi.cluster_size;
931 } else {
932 cluster_size = BLOCK_SIZE;
933 }
77358b59
PR
934 }
935
936 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
937 nr_sectors = total_sectors - addr;
938 } else {
939 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
940 }
941
323004a3 942 if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
ad2964b4
KW
943 ret = blk_pwrite_zeroes(blk, addr * BDRV_SECTOR_SIZE,
944 nr_sectors * BDRV_SECTOR_SIZE,
945 BDRV_REQ_MAY_UNMAP);
323004a3 946 } else {
3928d50b
LC
947 int i;
948 int64_t cur_addr;
949 uint8_t *cur_buf;
950
323004a3
PL
951 buf = g_malloc(BLOCK_SIZE);
952 qemu_get_buffer(f, buf, BLOCK_SIZE);
3928d50b
LC
953 for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
954 cur_addr = addr * BDRV_SECTOR_SIZE + i * cluster_size;
955 cur_buf = buf + i * cluster_size;
956
957 if ((!block_mig_state.zero_blocks ||
958 cluster_size < BLOCK_SIZE) &&
959 buffer_is_zero(cur_buf, cluster_size)) {
960 ret = blk_pwrite_zeroes(blk, cur_addr,
961 cluster_size,
962 BDRV_REQ_MAY_UNMAP);
963 } else {
964 ret = blk_pwrite(blk, cur_addr, cur_buf,
965 cluster_size, 0);
966 }
967 if (ret < 0) {
968 break;
969 }
970 }
323004a3
PL
971 g_free(buf);
972 }
575a58d7 973
b02bea3a
YT
974 if (ret < 0) {
975 return ret;
976 }
01e61e2d
JK
977 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
978 if (!banner_printed) {
979 printf("Receiving block device images\n");
980 banner_printed = 1;
981 }
982 printf("Completed %d %%%c", (int)addr,
983 (addr == 100) ? '\n' : '\r');
984 fflush(stdout);
a55eb92c 985 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
d5f1f286 986 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
4b640365
JK
987 return -EINVAL;
988 }
42802d47
JQ
989 ret = qemu_file_get_error(f);
990 if (ret != 0) {
991 return ret;
c163b5ca 992 }
a55eb92c
JK
993 } while (!(flags & BLK_MIG_FLAG_EOS));
994
c163b5ca
LS
995 return 0;
996}
997
6bd68781
JQ
998static bool block_is_active(void *opaque)
999{
ce7c817c 1000 return migrate_use_block();
6bd68781
JQ
1001}
1002
7a46d042 1003static SaveVMHandlers savevm_block_handlers = {
d1315aac 1004 .save_live_setup = block_save_setup,
16310a3c 1005 .save_live_iterate = block_save_iterate,
a3e06c3d 1006 .save_live_complete_precopy = block_save_complete,
e4ed1541 1007 .save_live_pending = block_save_pending,
7908c78d 1008 .load_state = block_load,
6ad2a215 1009 .cleanup = block_migration_cleanup,
6bd68781 1010 .is_active = block_is_active,
7908c78d
JQ
1011};
1012
c163b5ca 1013void blk_mig_init(void)
a55eb92c 1014{
5e5328be
JK
1015 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
1016 QSIMPLEQ_INIT(&block_mig_state.blk_list);
52e850de 1017 qemu_mutex_init(&block_mig_state.lock);
5e5328be 1018
7908c78d
JQ
1019 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
1020 &block_mig_state);
c163b5ca 1021}
This page took 0.565251 seconds and 4 git commands to generate.