]>
Commit | Line | Data |
---|---|---|
c163b5ca LS |
1 | /* |
2 | * QEMU live block migration | |
3 | * | |
4 | * Copyright IBM, Corp. 2009 | |
5 | * | |
6 | * Authors: | |
7 | * Liran Schour <lirans@il.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
6b620ca3 PB |
12 | * Contributions after 2012-01-13 are licensed under the terms of the |
13 | * GNU GPL, version 2 or (at your option) any later version. | |
c163b5ca LS |
14 | */ |
15 | ||
1393a485 | 16 | #include "qemu/osdep.h" |
da34e65c | 17 | #include "qapi/error.h" |
bfb197e0 | 18 | #include "qemu/error-report.h" |
db725815 | 19 | #include "qemu/main-loop.h" |
f348b6d1 | 20 | #include "qemu/cutils.h" |
1de7afc9 | 21 | #include "qemu/queue.h" |
2c9e6fec | 22 | #include "block.h" |
e2c1c34f | 23 | #include "block/dirty-bitmap.h" |
2c9e6fec | 24 | #include "migration/misc.h" |
6666c96a | 25 | #include "migration.h" |
e1fde0e0 | 26 | #include "migration-stats.h" |
f2a8f0a6 | 27 | #include "migration/register.h" |
08a0aee1 | 28 | #include "qemu-file.h" |
987772d9 | 29 | #include "migration/vmstate.h" |
c9ebaf74 | 30 | #include "sysemu/block-backend.h" |
fe80c024 | 31 | #include "trace.h" |
1f0776f1 | 32 | #include "options.h" |
c163b5ca | 33 | |
4bcb7de0 | 34 | #define BLK_MIG_BLOCK_SIZE (1ULL << 20) |
a152bd00 | 35 | #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLK_MIG_BLOCK_SIZE >> BDRV_SECTOR_BITS) |
c163b5ca LS |
36 | |
37 | #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01 | |
38 | #define BLK_MIG_FLAG_EOS 0x02 | |
01e61e2d | 39 | #define BLK_MIG_FLAG_PROGRESS 0x04 |
323004a3 | 40 | #define BLK_MIG_FLAG_ZERO_BLOCK 0x08 |
c163b5ca | 41 | |
d6a644bb | 42 | #define MAX_IS_ALLOCATED_SEARCH (65536 * BDRV_SECTOR_SIZE) |
c163b5ca | 43 | |
ef9c5160 | 44 | #define MAX_IO_BUFFERS 512 |
44815334 | 45 | #define MAX_PARALLEL_IO 16 |
f77dcdbc | 46 | |
a55eb92c | 47 | typedef struct BlkMigDevState { |
323920c4 | 48 | /* Written during setup phase. Can be read without a lock. */ |
ebd2f9e7 KW |
49 | BlockBackend *blk; |
50 | char *blk_name; | |
a55eb92c | 51 | int shared_base; |
a55eb92c | 52 | int64_t total_sectors; |
5e5328be | 53 | QSIMPLEQ_ENTRY(BlkMigDevState) entry; |
ef0716df | 54 | Error *blocker; |
323920c4 PB |
55 | |
56 | /* Only used by migration thread. Does not need a lock. */ | |
57 | int bulk_completed; | |
58 | int64_t cur_sector; | |
59 | int64_t cur_dirty; | |
60 | ||
ef0716df PB |
61 | /* Data in the aio_bitmap is protected by block migration lock. |
62 | * Allocation and free happen during setup and cleanup respectively. | |
63 | */ | |
33656af7 | 64 | unsigned long *aio_bitmap; |
ef0716df PB |
65 | |
66 | /* Protected by block migration lock. */ | |
323920c4 | 67 | int64_t completed_sectors; |
ef0716df PB |
68 | |
69 | /* During migration this is protected by iothread lock / AioContext. | |
70 | * Allocation and free happen during setup and cleanup respectively. | |
71 | */ | |
e4654d2d | 72 | BdrvDirtyBitmap *dirty_bitmap; |
a55eb92c JK |
73 | } BlkMigDevState; |
74 | ||
c163b5ca | 75 | typedef struct BlkMigBlock { |
323920c4 | 76 | /* Only used by migration thread. */ |
c163b5ca LS |
77 | uint8_t *buf; |
78 | BlkMigDevState *bmds; | |
79 | int64_t sector; | |
33656af7 | 80 | int nr_sectors; |
c163b5ca | 81 | QEMUIOVector qiov; |
7c84b1b8 | 82 | BlockAIOCB *aiocb; |
323920c4 | 83 | |
52e850de | 84 | /* Protected by block migration lock. */ |
c163b5ca | 85 | int ret; |
5e5328be | 86 | QSIMPLEQ_ENTRY(BlkMigBlock) entry; |
c163b5ca LS |
87 | } BlkMigBlock; |
88 | ||
89 | typedef struct BlkMigState { | |
b58deb34 | 90 | QSIMPLEQ_HEAD(, BlkMigDevState) bmds_list; |
323920c4 | 91 | int64_t total_sector_sum; |
323004a3 | 92 | bool zero_blocks; |
323920c4 | 93 | |
52e850de | 94 | /* Protected by lock. */ |
b58deb34 | 95 | QSIMPLEQ_HEAD(, BlkMigBlock) blk_list; |
c163b5ca LS |
96 | int submitted; |
97 | int read_done; | |
323920c4 PB |
98 | |
99 | /* Only used by migration thread. Does not need a lock. */ | |
c163b5ca | 100 | int transferred; |
01e61e2d | 101 | int prev_progress; |
e970ec0b | 102 | int bulk_completed; |
52e850de | 103 | |
ef0716df | 104 | /* Lock must be taken _inside_ the iothread lock and any AioContexts. */ |
52e850de | 105 | QemuMutex lock; |
c163b5ca LS |
106 | } BlkMigState; |
107 | ||
d11ecd3d | 108 | static BlkMigState block_mig_state; |
c163b5ca | 109 | |
52e850de PB |
110 | static void blk_mig_lock(void) |
111 | { | |
112 | qemu_mutex_lock(&block_mig_state.lock); | |
113 | } | |
114 | ||
115 | static void blk_mig_unlock(void) | |
116 | { | |
117 | qemu_mutex_unlock(&block_mig_state.lock); | |
118 | } | |
119 | ||
32c835ba PB |
120 | /* Must run outside of the iothread lock during the bulk phase, |
121 | * or the VM will stall. | |
122 | */ | |
123 | ||
13f0b67f JK |
124 | static void blk_send(QEMUFile *f, BlkMigBlock * blk) |
125 | { | |
126 | int len; | |
323004a3 PL |
127 | uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK; |
128 | ||
129 | if (block_mig_state.zero_blocks && | |
a152bd00 | 130 | buffer_is_zero(blk->buf, BLK_MIG_BLOCK_SIZE)) { |
323004a3 PL |
131 | flags |= BLK_MIG_FLAG_ZERO_BLOCK; |
132 | } | |
13f0b67f JK |
133 | |
134 | /* sector number and flags */ | |
135 | qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS) | |
323004a3 | 136 | | flags); |
13f0b67f JK |
137 | |
138 | /* device name */ | |
ebd2f9e7 | 139 | len = strlen(blk->bmds->blk_name); |
13f0b67f | 140 | qemu_put_byte(f, len); |
ebd2f9e7 | 141 | qemu_put_buffer(f, (uint8_t *) blk->bmds->blk_name, len); |
13f0b67f | 142 | |
323004a3 PL |
143 | /* if a block is zero we need to flush here since the network |
144 | * bandwidth is now a lot higher than the storage device bandwidth. | |
145 | * thus if we queue zero blocks we slow down the migration */ | |
146 | if (flags & BLK_MIG_FLAG_ZERO_BLOCK) { | |
147 | qemu_fflush(f); | |
148 | return; | |
149 | } | |
150 | ||
a152bd00 | 151 | qemu_put_buffer(f, blk->buf, BLK_MIG_BLOCK_SIZE); |
13f0b67f JK |
152 | } |
153 | ||
25f23643 JK |
154 | int blk_mig_active(void) |
155 | { | |
156 | return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list); | |
157 | } | |
158 | ||
9ac78b61 PL |
159 | int blk_mig_bulk_active(void) |
160 | { | |
161 | return blk_mig_active() && !block_mig_state.bulk_completed; | |
162 | } | |
163 | ||
25f23643 JK |
164 | uint64_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 | ||
177 | uint64_t blk_mig_bytes_remaining(void) | |
178 | { | |
179 | return blk_mig_bytes_total() - blk_mig_bytes_transferred(); | |
180 | } | |
181 | ||
182 | uint64_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 |
196 | static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector) |
197 | { | |
198 | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; | |
199 | ||
2c5451ca | 200 | if (sector < bmds->total_sectors) { |
33656af7 MT |
201 | return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
202 | (1UL << (chunk % (sizeof(unsigned long) * 8)))); | |
203 | } else { | |
204 | return 0; | |
205 | } | |
206 | } | |
207 | ||
52e850de PB |
208 | /* Called with migration lock held. */ |
209 | ||
33656af7 MT |
210 | static 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 | ||
232 | static void alloc_aio_bitmap(BlkMigDevState *bmds) | |
233 | { | |
33656af7 MT |
234 | int64_t bitmap_size; |
235 | ||
2c5451ca | 236 | bitmap_size = bmds->total_sectors + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; |
33656af7 MT |
237 | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; |
238 | ||
7267c094 | 239 | bmds->aio_bitmap = g_malloc0(bitmap_size); |
33656af7 MT |
240 | } |
241 | ||
52e850de PB |
242 | /* Never hold migration lock when yielding to the main loop! */ |
243 | ||
c163b5ca LS |
244 | static void blk_mig_read_cb(void *opaque, int ret) |
245 | { | |
246 | BlkMigBlock *blk = opaque; | |
a55eb92c | 247 | |
52e850de | 248 | blk_mig_lock(); |
c163b5ca | 249 | blk->ret = ret; |
a55eb92c | 250 | |
5e5328be | 251 | QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry); |
33656af7 | 252 | bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0); |
a55eb92c | 253 | |
d11ecd3d JK |
254 | block_mig_state.submitted--; |
255 | block_mig_state.read_done++; | |
256 | assert(block_mig_state.submitted >= 0); | |
52e850de | 257 | blk_mig_unlock(); |
c163b5ca LS |
258 | } |
259 | ||
32c835ba PB |
260 | /* Called with no lock taken. */ |
261 | ||
539de124 | 262 | static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) |
a55eb92c | 263 | { |
57cce12d JK |
264 | int64_t total_sectors = bmds->total_sectors; |
265 | int64_t cur_sector = bmds->cur_sector; | |
ebd2f9e7 | 266 | BlockBackend *bb = bmds->blk; |
c163b5ca | 267 | BlkMigBlock *blk; |
13f0b67f | 268 | int nr_sectors; |
d6a644bb | 269 | int64_t count; |
a55eb92c | 270 | |
57cce12d | 271 | if (bmds->shared_base) { |
32c835ba | 272 | qemu_mutex_lock_iothread(); |
ebd2f9e7 | 273 | aio_context_acquire(blk_get_aio_context(bb)); |
d6a644bb EB |
274 | /* Skip unallocated sectors; intentionally treats failure or |
275 | * partial sector as an allocated sector */ | |
b1d10856 | 276 | while (cur_sector < total_sectors && |
d6a644bb EB |
277 | !bdrv_is_allocated(blk_bs(bb), cur_sector * BDRV_SECTOR_SIZE, |
278 | MAX_IS_ALLOCATED_SEARCH, &count)) { | |
279 | if (count < BDRV_SECTOR_SIZE) { | |
280 | break; | |
281 | } | |
282 | cur_sector += count >> BDRV_SECTOR_BITS; | |
c163b5ca | 283 | } |
ebd2f9e7 | 284 | aio_context_release(blk_get_aio_context(bb)); |
32c835ba | 285 | qemu_mutex_unlock_iothread(); |
c163b5ca | 286 | } |
a55eb92c JK |
287 | |
288 | if (cur_sector >= total_sectors) { | |
82801d8f | 289 | bmds->cur_sector = bmds->completed_sectors = total_sectors; |
c163b5ca LS |
290 | return 1; |
291 | } | |
a55eb92c | 292 | |
82801d8f | 293 | bmds->completed_sectors = cur_sector; |
a55eb92c | 294 | |
57cce12d JK |
295 | cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1); |
296 | ||
6ea44308 JK |
297 | /* we are going to transfer a full block even if it is not allocated */ |
298 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; | |
c163b5ca | 299 | |
6ea44308 | 300 | if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) { |
57cce12d | 301 | nr_sectors = total_sectors - cur_sector; |
c163b5ca | 302 | } |
a55eb92c | 303 | |
5839e53b | 304 | blk = g_new(BlkMigBlock, 1); |
a152bd00 | 305 | blk->buf = g_malloc(BLK_MIG_BLOCK_SIZE); |
13f0b67f JK |
306 | blk->bmds = bmds; |
307 | blk->sector = cur_sector; | |
33656af7 | 308 | blk->nr_sectors = nr_sectors; |
a55eb92c | 309 | |
f556f37b | 310 | qemu_iovec_init_buf(&blk->qiov, blk->buf, nr_sectors * BDRV_SECTOR_SIZE); |
a55eb92c | 311 | |
52e850de | 312 | blk_mig_lock(); |
13197e3c | 313 | block_mig_state.submitted++; |
52e850de | 314 | blk_mig_unlock(); |
13197e3c | 315 | |
ef0716df PB |
316 | /* We do not know if bs is under the main thread (and thus does |
317 | * not acquire the AioContext when doing AIO) or rather under | |
318 | * dataplane. Thus acquire both the iothread mutex and the | |
319 | * AioContext. | |
320 | * | |
321 | * This is ugly and will disappear when we make bdrv_* thread-safe, | |
322 | * without the need to acquire the AioContext. | |
323 | */ | |
32c835ba | 324 | qemu_mutex_lock_iothread(); |
ebd2f9e7 | 325 | aio_context_acquire(blk_get_aio_context(bmds->blk)); |
e0d7f73e EB |
326 | bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector * BDRV_SECTOR_SIZE, |
327 | nr_sectors * BDRV_SECTOR_SIZE); | |
86b124bc PL |
328 | blk->aiocb = blk_aio_preadv(bb, cur_sector * BDRV_SECTOR_SIZE, &blk->qiov, |
329 | 0, blk_mig_read_cb, blk); | |
ebd2f9e7 | 330 | aio_context_release(blk_get_aio_context(bmds->blk)); |
32c835ba | 331 | qemu_mutex_unlock_iothread(); |
a55eb92c | 332 | |
32c835ba | 333 | bmds->cur_sector = cur_sector + nr_sectors; |
13f0b67f | 334 | return (bmds->cur_sector >= total_sectors); |
c163b5ca LS |
335 | } |
336 | ||
32c835ba PB |
337 | /* Called with iothread lock taken. */ |
338 | ||
b8afb520 | 339 | static int set_dirty_tracking(void) |
c163b5ca LS |
340 | { |
341 | BlkMigDevState *bmds; | |
b8afb520 FZ |
342 | int ret; |
343 | ||
344 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
ebd2f9e7 | 345 | bmds->dirty_bitmap = bdrv_create_dirty_bitmap(blk_bs(bmds->blk), |
a152bd00 SH |
346 | BLK_MIG_BLOCK_SIZE, |
347 | NULL, NULL); | |
b8afb520 FZ |
348 | if (!bmds->dirty_bitmap) { |
349 | ret = -errno; | |
350 | goto fail; | |
351 | } | |
352 | } | |
353 | return 0; | |
5e5328be | 354 | |
b8afb520 | 355 | fail: |
5e5328be | 356 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
b8afb520 | 357 | if (bmds->dirty_bitmap) { |
5deb6cbd | 358 | bdrv_release_dirty_bitmap(bmds->dirty_bitmap); |
b8afb520 | 359 | } |
e4654d2d | 360 | } |
b8afb520 | 361 | return ret; |
e4654d2d FZ |
362 | } |
363 | ||
ef0716df PB |
364 | /* Called with iothread lock taken. */ |
365 | ||
e4654d2d FZ |
366 | static void unset_dirty_tracking(void) |
367 | { | |
368 | BlkMigDevState *bmds; | |
369 | ||
370 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
f187609f FR |
371 | if (bmds->dirty_bitmap) { |
372 | bdrv_release_dirty_bitmap(bmds->dirty_bitmap); | |
373 | } | |
c163b5ca | 374 | } |
c163b5ca LS |
375 | } |
376 | ||
6f5ef23a | 377 | static int init_blk_migration(QEMUFile *f) |
c163b5ca | 378 | { |
fea68bb6 | 379 | BlockDriverState *bs; |
5e5328be | 380 | BlkMigDevState *bmds; |
792773b2 | 381 | int64_t sectors; |
88be7b4b | 382 | BdrvNextIterator it; |
ebd2f9e7 KW |
383 | int i, num_bs = 0; |
384 | struct { | |
385 | BlkMigDevState *bmds; | |
386 | BlockDriverState *bs; | |
387 | } *bmds_bs; | |
6f5ef23a KW |
388 | Error *local_err = NULL; |
389 | int ret; | |
a55eb92c | 390 | |
2b3912f1 KW |
391 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
392 | ||
fea68bb6 MA |
393 | block_mig_state.submitted = 0; |
394 | block_mig_state.read_done = 0; | |
395 | block_mig_state.transferred = 0; | |
396 | block_mig_state.total_sector_sum = 0; | |
397 | block_mig_state.prev_progress = -1; | |
398 | block_mig_state.bulk_completed = 0; | |
399 | block_mig_state.zero_blocks = migrate_zero_blocks(); | |
400 | ||
88be7b4b | 401 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
ebd2f9e7 KW |
402 | num_bs++; |
403 | } | |
404 | bmds_bs = g_malloc0(num_bs * sizeof(*bmds_bs)); | |
405 | ||
406 | for (i = 0, bs = bdrv_first(&it); bs; bs = bdrv_next(&it), i++) { | |
fea68bb6 MA |
407 | if (bdrv_is_read_only(bs)) { |
408 | continue; | |
409 | } | |
410 | ||
57322b78 | 411 | sectors = bdrv_nb_sectors(bs); |
31f54f24 | 412 | if (sectors <= 0) { |
6f5ef23a | 413 | ret = sectors; |
5e003f17 | 414 | bdrv_next_cleanup(&it); |
ebd2f9e7 | 415 | goto out; |
b66460e4 SH |
416 | } |
417 | ||
5839e53b | 418 | bmds = g_new0(BlkMigDevState, 1); |
d861ab3a KW |
419 | bmds->blk = blk_new(qemu_get_aio_context(), |
420 | BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL); | |
ebd2f9e7 | 421 | bmds->blk_name = g_strdup(bdrv_get_device_name(bs)); |
b66460e4 SH |
422 | bmds->bulk_completed = 0; |
423 | bmds->total_sectors = sectors; | |
424 | bmds->completed_sectors = 0; | |
6f8be708 | 425 | bmds->shared_base = migrate_block_incremental(); |
ebd2f9e7 KW |
426 | |
427 | assert(i < num_bs); | |
428 | bmds_bs[i].bmds = bmds; | |
429 | bmds_bs[i].bs = bs; | |
b66460e4 SH |
430 | |
431 | block_mig_state.total_sector_sum += sectors; | |
432 | ||
433 | if (bmds->shared_base) { | |
fe80c024 | 434 | trace_migration_block_init_shared(bdrv_get_device_name(bs)); |
b66460e4 | 435 | } else { |
fe80c024 | 436 | trace_migration_block_init_full(bdrv_get_device_name(bs)); |
b66460e4 SH |
437 | } |
438 | ||
439 | QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry); | |
440 | } | |
ebd2f9e7 KW |
441 | |
442 | /* Can only insert new BDSes now because doing so while iterating block | |
443 | * devices may end up in a deadlock (iterating the new BDSes, too). */ | |
444 | for (i = 0; i < num_bs; i++) { | |
7f3de3f0 MA |
445 | bmds = bmds_bs[i].bmds; |
446 | bs = bmds_bs[i].bs; | |
ebd2f9e7 KW |
447 | |
448 | if (bmds) { | |
6f5ef23a KW |
449 | ret = blk_insert_bs(bmds->blk, bs, &local_err); |
450 | if (ret < 0) { | |
451 | error_report_err(local_err); | |
452 | goto out; | |
453 | } | |
ebd2f9e7 KW |
454 | |
455 | alloc_aio_bitmap(bmds); | |
456 | error_setg(&bmds->blocker, "block device is in use by migration"); | |
457 | bdrv_op_block_all(bs, bmds->blocker); | |
458 | } | |
459 | } | |
460 | ||
6f5ef23a | 461 | ret = 0; |
ebd2f9e7 KW |
462 | out: |
463 | g_free(bmds_bs); | |
6f5ef23a | 464 | return ret; |
b66460e4 SH |
465 | } |
466 | ||
32c835ba PB |
467 | /* Called with no lock taken. */ |
468 | ||
539de124 | 469 | static int blk_mig_save_bulked_block(QEMUFile *f) |
c163b5ca | 470 | { |
82801d8f | 471 | int64_t completed_sector_sum = 0; |
c163b5ca | 472 | BlkMigDevState *bmds; |
01e61e2d | 473 | int progress; |
82801d8f | 474 | int ret = 0; |
c163b5ca | 475 | |
5e5328be | 476 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
a55eb92c | 477 | if (bmds->bulk_completed == 0) { |
539de124 | 478 | if (mig_save_device_bulk(f, bmds) == 1) { |
57cce12d JK |
479 | /* completed bulk section for this device */ |
480 | bmds->bulk_completed = 1; | |
c163b5ca | 481 | } |
82801d8f JK |
482 | completed_sector_sum += bmds->completed_sectors; |
483 | ret = 1; | |
484 | break; | |
485 | } else { | |
486 | completed_sector_sum += bmds->completed_sectors; | |
c163b5ca LS |
487 | } |
488 | } | |
a55eb92c | 489 | |
8b6b2afc PR |
490 | if (block_mig_state.total_sector_sum != 0) { |
491 | progress = completed_sector_sum * 100 / | |
492 | block_mig_state.total_sector_sum; | |
493 | } else { | |
494 | progress = 100; | |
495 | } | |
01e61e2d JK |
496 | if (progress != block_mig_state.prev_progress) { |
497 | block_mig_state.prev_progress = progress; | |
498 | qemu_put_be64(f, (progress << BDRV_SECTOR_BITS) | |
499 | | BLK_MIG_FLAG_PROGRESS); | |
163b8663 | 500 | trace_migration_block_progression(progress); |
82801d8f JK |
501 | } |
502 | ||
503 | return ret; | |
c163b5ca LS |
504 | } |
505 | ||
d76cac7d | 506 | static void blk_mig_reset_dirty_cursor(void) |
c163b5ca LS |
507 | { |
508 | BlkMigDevState *bmds; | |
d76cac7d LS |
509 | |
510 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
511 | bmds->cur_dirty = 0; | |
512 | } | |
513 | } | |
514 | ||
ef0716df | 515 | /* Called with iothread lock and AioContext taken. */ |
32c835ba | 516 | |
539de124 LC |
517 | static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds, |
518 | int is_async) | |
d76cac7d LS |
519 | { |
520 | BlkMigBlock *blk; | |
521 | int64_t total_sectors = bmds->total_sectors; | |
c163b5ca | 522 | int64_t sector; |
d76cac7d | 523 | int nr_sectors; |
dcd1d224 | 524 | int ret = -EIO; |
a55eb92c | 525 | |
d76cac7d | 526 | for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) { |
52e850de | 527 | blk_mig_lock(); |
62155e2b | 528 | if (bmds_aio_inflight(bmds, sector)) { |
52e850de | 529 | blk_mig_unlock(); |
ebd2f9e7 | 530 | blk_drain(bmds->blk); |
52e850de PB |
531 | } else { |
532 | blk_mig_unlock(); | |
62155e2b | 533 | } |
b64bd51e | 534 | bdrv_dirty_bitmap_lock(bmds->dirty_bitmap); |
28636b82 JS |
535 | if (bdrv_dirty_bitmap_get_locked(bmds->dirty_bitmap, |
536 | sector * BDRV_SECTOR_SIZE)) { | |
d76cac7d LS |
537 | if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) { |
538 | nr_sectors = total_sectors - sector; | |
539 | } else { | |
540 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; | |
541 | } | |
e0d7f73e EB |
542 | bdrv_reset_dirty_bitmap_locked(bmds->dirty_bitmap, |
543 | sector * BDRV_SECTOR_SIZE, | |
544 | nr_sectors * BDRV_SECTOR_SIZE); | |
b64bd51e | 545 | bdrv_dirty_bitmap_unlock(bmds->dirty_bitmap); |
c0bad499 | 546 | |
5839e53b | 547 | blk = g_new(BlkMigBlock, 1); |
a152bd00 | 548 | blk->buf = g_malloc(BLK_MIG_BLOCK_SIZE); |
d76cac7d LS |
549 | blk->bmds = bmds; |
550 | blk->sector = sector; | |
33656af7 | 551 | blk->nr_sectors = nr_sectors; |
d76cac7d | 552 | |
889ae39c | 553 | if (is_async) { |
f556f37b VSO |
554 | qemu_iovec_init_buf(&blk->qiov, blk->buf, |
555 | nr_sectors * BDRV_SECTOR_SIZE); | |
d76cac7d | 556 | |
ebd2f9e7 KW |
557 | blk->aiocb = blk_aio_preadv(bmds->blk, |
558 | sector * BDRV_SECTOR_SIZE, | |
559 | &blk->qiov, 0, blk_mig_read_cb, | |
560 | blk); | |
52e850de PB |
561 | |
562 | blk_mig_lock(); | |
d76cac7d | 563 | block_mig_state.submitted++; |
33656af7 | 564 | bmds_set_aio_inflight(bmds, sector, nr_sectors, 1); |
52e850de | 565 | blk_mig_unlock(); |
d76cac7d | 566 | } else { |
3b35d454 | 567 | ret = blk_pread(bmds->blk, sector * BDRV_SECTOR_SIZE, |
a9262f55 | 568 | nr_sectors * BDRV_SECTOR_SIZE, blk->buf, 0); |
dcd1d224 | 569 | if (ret < 0) { |
d76cac7d | 570 | goto error; |
c163b5ca | 571 | } |
d76cac7d | 572 | blk_send(f, blk); |
a55eb92c | 573 | |
7267c094 AL |
574 | g_free(blk->buf); |
575 | g_free(blk); | |
a55eb92c | 576 | } |
d76cac7d | 577 | |
1cf6aa74 LC |
578 | sector += nr_sectors; |
579 | bmds->cur_dirty = sector; | |
d76cac7d | 580 | break; |
c163b5ca | 581 | } |
b64bd51e PB |
582 | |
583 | bdrv_dirty_bitmap_unlock(bmds->dirty_bitmap); | |
d76cac7d LS |
584 | sector += BDRV_SECTORS_PER_DIRTY_CHUNK; |
585 | bmds->cur_dirty = sector; | |
c163b5ca | 586 | } |
575a58d7 | 587 | |
d76cac7d LS |
588 | return (bmds->cur_dirty >= bmds->total_sectors); |
589 | ||
889ae39c | 590 | error: |
fe80c024 | 591 | trace_migration_block_save_device_dirty(sector); |
7267c094 AL |
592 | g_free(blk->buf); |
593 | g_free(blk); | |
43be3a25 | 594 | return ret; |
d76cac7d LS |
595 | } |
596 | ||
32c835ba PB |
597 | /* Called with iothread lock taken. |
598 | * | |
599 | * return value: | |
ceb2bd09 JQ |
600 | * 0: too much data for max_downtime |
601 | * 1: few enough data for max_downtime | |
602 | */ | |
539de124 | 603 | static int blk_mig_save_dirty_block(QEMUFile *f, int is_async) |
d76cac7d LS |
604 | { |
605 | BlkMigDevState *bmds; | |
ceb2bd09 | 606 | int ret = 1; |
d76cac7d LS |
607 | |
608 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
ebd2f9e7 | 609 | aio_context_acquire(blk_get_aio_context(bmds->blk)); |
ceb2bd09 | 610 | ret = mig_save_device_dirty(f, bmds, is_async); |
ebd2f9e7 | 611 | aio_context_release(blk_get_aio_context(bmds->blk)); |
43be3a25 | 612 | if (ret <= 0) { |
d76cac7d LS |
613 | break; |
614 | } | |
615 | } | |
616 | ||
617 | return ret; | |
c163b5ca LS |
618 | } |
619 | ||
32c835ba PB |
620 | /* Called with no locks taken. */ |
621 | ||
59feec42 | 622 | static int flush_blks(QEMUFile *f) |
c163b5ca | 623 | { |
5e5328be | 624 | BlkMigBlock *blk; |
59feec42 | 625 | int ret = 0; |
a55eb92c | 626 | |
fe80c024 BY |
627 | trace_migration_block_flush_blks("Enter", block_mig_state.submitted, |
628 | block_mig_state.read_done, | |
629 | block_mig_state.transferred); | |
a55eb92c | 630 | |
52e850de | 631 | blk_mig_lock(); |
5e5328be | 632 | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
e1fde0e0 | 633 | if (migration_rate_exceeded(f)) { |
5e5328be JK |
634 | break; |
635 | } | |
4b640365 | 636 | if (blk->ret < 0) { |
59feec42 | 637 | ret = blk->ret; |
4b640365 JK |
638 | break; |
639 | } | |
a55eb92c | 640 | |
5e5328be | 641 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); |
52e850de | 642 | blk_mig_unlock(); |
13197e3c | 643 | blk_send(f, blk); |
52e850de | 644 | blk_mig_lock(); |
13197e3c | 645 | |
7267c094 AL |
646 | g_free(blk->buf); |
647 | g_free(blk); | |
a55eb92c | 648 | |
d11ecd3d JK |
649 | block_mig_state.read_done--; |
650 | block_mig_state.transferred++; | |
651 | assert(block_mig_state.read_done >= 0); | |
c163b5ca | 652 | } |
52e850de | 653 | blk_mig_unlock(); |
c163b5ca | 654 | |
fe80c024 BY |
655 | trace_migration_block_flush_blks("Exit", block_mig_state.submitted, |
656 | block_mig_state.read_done, | |
657 | block_mig_state.transferred); | |
59feec42 | 658 | return ret; |
c163b5ca LS |
659 | } |
660 | ||
32c835ba PB |
661 | /* Called with iothread lock taken. */ |
662 | ||
889ae39c LS |
663 | static int64_t get_remaining_dirty(void) |
664 | { | |
665 | BlkMigDevState *bmds; | |
666 | int64_t dirty = 0; | |
667 | ||
668 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
ebd2f9e7 | 669 | aio_context_acquire(blk_get_aio_context(bmds->blk)); |
20dca810 | 670 | dirty += bdrv_get_dirty_count(bmds->dirty_bitmap); |
ebd2f9e7 | 671 | aio_context_release(blk_get_aio_context(bmds->blk)); |
889ae39c LS |
672 | } |
673 | ||
9a46dba7 | 674 | return dirty; |
889ae39c LS |
675 | } |
676 | ||
32c835ba | 677 | |
362fdf17 KW |
678 | |
679 | /* Called with iothread lock taken. */ | |
680 | static void block_migration_cleanup_bmds(void) | |
4ec7fcc7 | 681 | { |
82801d8f | 682 | BlkMigDevState *bmds; |
f187609f | 683 | BlockDriverState *bs; |
ef0716df | 684 | AioContext *ctx; |
4ec7fcc7 | 685 | |
e4654d2d | 686 | unset_dirty_tracking(); |
8f794c55 | 687 | |
82801d8f JK |
688 | while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) { |
689 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry); | |
f187609f FR |
690 | |
691 | bs = blk_bs(bmds->blk); | |
692 | if (bs) { | |
693 | bdrv_op_unblock_all(bs, bmds->blocker); | |
694 | } | |
3718d8ab | 695 | error_free(bmds->blocker); |
ef0716df | 696 | |
ebd2f9e7 KW |
697 | /* Save ctx, because bmds->blk can disappear during blk_unref. */ |
698 | ctx = blk_get_aio_context(bmds->blk); | |
ef0716df | 699 | aio_context_acquire(ctx); |
ebd2f9e7 | 700 | blk_unref(bmds->blk); |
ef0716df PB |
701 | aio_context_release(ctx); |
702 | ||
ebd2f9e7 | 703 | g_free(bmds->blk_name); |
7267c094 AL |
704 | g_free(bmds->aio_bitmap); |
705 | g_free(bmds); | |
4ec7fcc7 | 706 | } |
362fdf17 KW |
707 | } |
708 | ||
709 | /* Called with iothread lock taken. */ | |
710 | static void block_migration_cleanup(void *opaque) | |
711 | { | |
712 | BlkMigBlock *blk; | |
713 | ||
714 | bdrv_drain_all(); | |
715 | ||
716 | block_migration_cleanup_bmds(); | |
4ec7fcc7 | 717 | |
ef0716df | 718 | blk_mig_lock(); |
82801d8f JK |
719 | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
720 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); | |
7267c094 AL |
721 | g_free(blk->buf); |
722 | g_free(blk); | |
4ec7fcc7 | 723 | } |
52e850de | 724 | blk_mig_unlock(); |
4ec7fcc7 JK |
725 | } |
726 | ||
d1315aac | 727 | static int block_save_setup(QEMUFile *f, void *opaque) |
c163b5ca | 728 | { |
2975725f JQ |
729 | int ret; |
730 | ||
fe80c024 BY |
731 | trace_migration_block_save("setup", block_mig_state.submitted, |
732 | block_mig_state.transferred); | |
a55eb92c | 733 | |
6f5ef23a KW |
734 | ret = init_blk_migration(f); |
735 | if (ret < 0) { | |
6f5ef23a KW |
736 | return ret; |
737 | } | |
d1315aac JQ |
738 | |
739 | /* start track dirty blocks */ | |
b8afb520 | 740 | ret = set_dirty_tracking(); |
b8afb520 | 741 | if (ret) { |
b8afb520 FZ |
742 | return ret; |
743 | } | |
744 | ||
59feec42 | 745 | ret = flush_blks(f); |
d1315aac | 746 | blk_mig_reset_dirty_cursor(); |
d1315aac JQ |
747 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
748 | ||
d418cf57 | 749 | return ret; |
d1315aac JQ |
750 | } |
751 | ||
16310a3c | 752 | static int block_save_iterate(QEMUFile *f, void *opaque) |
d1315aac JQ |
753 | { |
754 | int ret; | |
f16ecfa9 | 755 | uint64_t last_bytes = qemu_file_transferred_noflush(f); |
d1315aac | 756 | |
fe80c024 BY |
757 | trace_migration_block_save("iterate", block_mig_state.submitted, |
758 | block_mig_state.transferred); | |
d1315aac | 759 | |
59feec42 | 760 | ret = flush_blks(f); |
2975725f | 761 | if (ret) { |
2975725f | 762 | return ret; |
4b640365 JK |
763 | } |
764 | ||
d76cac7d LS |
765 | blk_mig_reset_dirty_cursor(); |
766 | ||
16310a3c | 767 | /* control the rate of transfer */ |
52e850de | 768 | blk_mig_lock(); |
a152bd00 | 769 | while (block_mig_state.read_done * BLK_MIG_BLOCK_SIZE < |
e1fde0e0 | 770 | migration_rate_get() && |
44815334 | 771 | block_mig_state.submitted < MAX_PARALLEL_IO && |
ef9c5160 PL |
772 | (block_mig_state.submitted + block_mig_state.read_done) < |
773 | MAX_IO_BUFFERS) { | |
52e850de | 774 | blk_mig_unlock(); |
16310a3c JQ |
775 | if (block_mig_state.bulk_completed == 0) { |
776 | /* first finish the bulk phase */ | |
777 | if (blk_mig_save_bulked_block(f) == 0) { | |
778 | /* finished saving bulk on all devices */ | |
779 | block_mig_state.bulk_completed = 1; | |
780 | } | |
13197e3c | 781 | ret = 0; |
16310a3c | 782 | } else { |
32c835ba PB |
783 | /* Always called with iothread lock taken for |
784 | * simplicity, block_save_complete also calls it. | |
785 | */ | |
786 | qemu_mutex_lock_iothread(); | |
43be3a25 | 787 | ret = blk_mig_save_dirty_block(f, 1); |
32c835ba | 788 | qemu_mutex_unlock_iothread(); |
13197e3c PB |
789 | } |
790 | if (ret < 0) { | |
791 | return ret; | |
792 | } | |
52e850de | 793 | blk_mig_lock(); |
13197e3c PB |
794 | if (ret != 0) { |
795 | /* no more dirty blocks */ | |
796 | break; | |
a55eb92c | 797 | } |
16310a3c | 798 | } |
52e850de | 799 | blk_mig_unlock(); |
a55eb92c | 800 | |
59feec42 | 801 | ret = flush_blks(f); |
16310a3c | 802 | if (ret) { |
16310a3c | 803 | return ret; |
4b640365 JK |
804 | } |
805 | ||
16310a3c | 806 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
f16ecfa9 | 807 | uint64_t delta_bytes = qemu_file_transferred_noflush(f) - last_bytes; |
f3030d34 | 808 | return (delta_bytes > 0); |
16310a3c JQ |
809 | } |
810 | ||
32c835ba PB |
811 | /* Called with iothread lock taken. */ |
812 | ||
16310a3c JQ |
813 | static int block_save_complete(QEMUFile *f, void *opaque) |
814 | { | |
815 | int ret; | |
816 | ||
fe80c024 BY |
817 | trace_migration_block_save("complete", block_mig_state.submitted, |
818 | block_mig_state.transferred); | |
16310a3c | 819 | |
59feec42 | 820 | ret = flush_blks(f); |
16310a3c | 821 | if (ret) { |
16310a3c JQ |
822 | return ret; |
823 | } | |
a55eb92c | 824 | |
16310a3c | 825 | blk_mig_reset_dirty_cursor(); |
01e61e2d | 826 | |
16310a3c JQ |
827 | /* we know for sure that save bulk is completed and |
828 | all async read completed */ | |
52e850de | 829 | blk_mig_lock(); |
16310a3c | 830 | assert(block_mig_state.submitted == 0); |
52e850de | 831 | blk_mig_unlock(); |
16310a3c | 832 | |
43be3a25 JQ |
833 | do { |
834 | ret = blk_mig_save_dirty_block(f, 0); | |
d418cf57 PB |
835 | if (ret < 0) { |
836 | return ret; | |
837 | } | |
43be3a25 | 838 | } while (ret == 0); |
4b640365 | 839 | |
43be3a25 JQ |
840 | /* report completion */ |
841 | qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS); | |
a55eb92c | 842 | |
fe80c024 | 843 | trace_migration_block_save_complete(); |
16310a3c | 844 | |
a55eb92c JK |
845 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
846 | ||
362fdf17 KW |
847 | /* Make sure that our BlockBackends are gone, so that the block driver |
848 | * nodes can be inactivated. */ | |
849 | block_migration_cleanup_bmds(); | |
850 | ||
16310a3c | 851 | return 0; |
c163b5ca LS |
852 | } |
853 | ||
24beea4e JQ |
854 | static void block_state_pending(void *opaque, uint64_t *must_precopy, |
855 | uint64_t *can_postcopy) | |
e4ed1541 | 856 | { |
6aaa9dae | 857 | /* Estimate pending number of bytes to send */ |
13197e3c PB |
858 | uint64_t pending; |
859 | ||
32c835ba | 860 | qemu_mutex_lock_iothread(); |
ef0716df PB |
861 | pending = get_remaining_dirty(); |
862 | qemu_mutex_unlock_iothread(); | |
863 | ||
52e850de | 864 | blk_mig_lock(); |
a152bd00 SH |
865 | pending += block_mig_state.submitted * BLK_MIG_BLOCK_SIZE + |
866 | block_mig_state.read_done * BLK_MIG_BLOCK_SIZE; | |
ef0716df | 867 | blk_mig_unlock(); |
6aaa9dae SH |
868 | |
869 | /* Report at least one block pending during bulk phase */ | |
b5280437 JQ |
870 | if (!pending && !block_mig_state.bulk_completed) { |
871 | pending = BLK_MIG_BLOCK_SIZE; | |
6aaa9dae | 872 | } |
e4ed1541 | 873 | |
c8df4a7a | 874 | trace_migration_block_state_pending(pending); |
c31b098f | 875 | /* We don't do postcopy */ |
24beea4e | 876 | *must_precopy += pending; |
e4ed1541 JQ |
877 | } |
878 | ||
c163b5ca LS |
879 | static int block_load(QEMUFile *f, void *opaque, int version_id) |
880 | { | |
01e61e2d | 881 | static int banner_printed; |
c163b5ca LS |
882 | int len, flags; |
883 | char device_name[256]; | |
884 | int64_t addr; | |
3c254ab8 | 885 | BlockBackend *blk, *blk_prev = NULL; |
9bd9c7f5 | 886 | Error *local_err = NULL; |
c163b5ca | 887 | uint8_t *buf; |
77358b59 PR |
888 | int64_t total_sectors = 0; |
889 | int nr_sectors; | |
42802d47 | 890 | int ret; |
3928d50b | 891 | BlockDriverInfo bdi; |
a152bd00 | 892 | int cluster_size = BLK_MIG_BLOCK_SIZE; |
a55eb92c | 893 | |
c163b5ca | 894 | do { |
c163b5ca | 895 | addr = qemu_get_be64(f); |
a55eb92c | 896 | |
89725715 | 897 | flags = addr & (BDRV_SECTOR_SIZE - 1); |
6ea44308 | 898 | addr >>= BDRV_SECTOR_BITS; |
a55eb92c JK |
899 | |
900 | if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) { | |
c163b5ca LS |
901 | /* get device name */ |
902 | len = qemu_get_byte(f); | |
c163b5ca LS |
903 | qemu_get_buffer(f, (uint8_t *)device_name, len); |
904 | device_name[len] = '\0'; | |
a55eb92c | 905 | |
c9ebaf74 FZ |
906 | blk = blk_by_name(device_name); |
907 | if (!blk) { | |
4b640365 JK |
908 | fprintf(stderr, "Error unknown block device %s\n", |
909 | device_name); | |
910 | return -EINVAL; | |
911 | } | |
a55eb92c | 912 | |
ad2964b4 KW |
913 | if (blk != blk_prev) { |
914 | blk_prev = blk; | |
915 | total_sectors = blk_nb_sectors(blk); | |
77358b59 | 916 | if (total_sectors <= 0) { |
6daf194d | 917 | error_report("Error getting length of block device %s", |
77358b59 PR |
918 | device_name); |
919 | return -EINVAL; | |
920 | } | |
9bd9c7f5 | 921 | |
3b717194 | 922 | blk_activate(blk, &local_err); |
9bd9c7f5 KW |
923 | if (local_err) { |
924 | error_report_err(local_err); | |
925 | return -EINVAL; | |
926 | } | |
3928d50b LC |
927 | |
928 | ret = bdrv_get_info(blk_bs(blk), &bdi); | |
929 | if (ret == 0 && bdi.cluster_size > 0 && | |
a152bd00 SH |
930 | bdi.cluster_size <= BLK_MIG_BLOCK_SIZE && |
931 | BLK_MIG_BLOCK_SIZE % bdi.cluster_size == 0) { | |
3928d50b LC |
932 | cluster_size = bdi.cluster_size; |
933 | } else { | |
a152bd00 | 934 | cluster_size = BLK_MIG_BLOCK_SIZE; |
3928d50b | 935 | } |
77358b59 PR |
936 | } |
937 | ||
938 | if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) { | |
939 | nr_sectors = total_sectors - addr; | |
940 | } else { | |
941 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; | |
942 | } | |
943 | ||
323004a3 | 944 | if (flags & BLK_MIG_FLAG_ZERO_BLOCK) { |
ad2964b4 KW |
945 | ret = blk_pwrite_zeroes(blk, addr * BDRV_SECTOR_SIZE, |
946 | nr_sectors * BDRV_SECTOR_SIZE, | |
947 | BDRV_REQ_MAY_UNMAP); | |
323004a3 | 948 | } else { |
3928d50b LC |
949 | int i; |
950 | int64_t cur_addr; | |
951 | uint8_t *cur_buf; | |
952 | ||
a152bd00 SH |
953 | buf = g_malloc(BLK_MIG_BLOCK_SIZE); |
954 | qemu_get_buffer(f, buf, BLK_MIG_BLOCK_SIZE); | |
955 | for (i = 0; i < BLK_MIG_BLOCK_SIZE / cluster_size; i++) { | |
3928d50b LC |
956 | cur_addr = addr * BDRV_SECTOR_SIZE + i * cluster_size; |
957 | cur_buf = buf + i * cluster_size; | |
958 | ||
959 | if ((!block_mig_state.zero_blocks || | |
a152bd00 | 960 | cluster_size < BLK_MIG_BLOCK_SIZE) && |
3928d50b LC |
961 | buffer_is_zero(cur_buf, cluster_size)) { |
962 | ret = blk_pwrite_zeroes(blk, cur_addr, | |
963 | cluster_size, | |
964 | BDRV_REQ_MAY_UNMAP); | |
965 | } else { | |
a9262f55 AF |
966 | ret = blk_pwrite(blk, cur_addr, cluster_size, cur_buf, |
967 | 0); | |
3928d50b LC |
968 | } |
969 | if (ret < 0) { | |
970 | break; | |
971 | } | |
972 | } | |
323004a3 PL |
973 | g_free(buf); |
974 | } | |
575a58d7 | 975 | |
b02bea3a YT |
976 | if (ret < 0) { |
977 | return ret; | |
978 | } | |
01e61e2d JK |
979 | } else if (flags & BLK_MIG_FLAG_PROGRESS) { |
980 | if (!banner_printed) { | |
981 | printf("Receiving block device images\n"); | |
982 | banner_printed = 1; | |
983 | } | |
984 | printf("Completed %d %%%c", (int)addr, | |
985 | (addr == 100) ? '\n' : '\r'); | |
986 | fflush(stdout); | |
a55eb92c | 987 | } else if (!(flags & BLK_MIG_FLAG_EOS)) { |
29fccade | 988 | fprintf(stderr, "Unknown block migration flags: 0x%x\n", flags); |
4b640365 JK |
989 | return -EINVAL; |
990 | } | |
42802d47 JQ |
991 | ret = qemu_file_get_error(f); |
992 | if (ret != 0) { | |
993 | return ret; | |
c163b5ca | 994 | } |
a55eb92c JK |
995 | } while (!(flags & BLK_MIG_FLAG_EOS)); |
996 | ||
c163b5ca LS |
997 | return 0; |
998 | } | |
999 | ||
6bd68781 JQ |
1000 | static bool block_is_active(void *opaque) |
1001 | { | |
9d4b1e5f | 1002 | return migrate_block(); |
6bd68781 JQ |
1003 | } |
1004 | ||
7a46d042 | 1005 | static SaveVMHandlers savevm_block_handlers = { |
9907e842 | 1006 | .save_setup = block_save_setup, |
16310a3c | 1007 | .save_live_iterate = block_save_iterate, |
a3e06c3d | 1008 | .save_live_complete_precopy = block_save_complete, |
c8df4a7a JQ |
1009 | .state_pending_exact = block_state_pending, |
1010 | .state_pending_estimate = block_state_pending, | |
7908c78d | 1011 | .load_state = block_load, |
70f794fc | 1012 | .save_cleanup = block_migration_cleanup, |
6bd68781 | 1013 | .is_active = block_is_active, |
7908c78d JQ |
1014 | }; |
1015 | ||
c163b5ca | 1016 | void blk_mig_init(void) |
a55eb92c | 1017 | { |
5e5328be JK |
1018 | QSIMPLEQ_INIT(&block_mig_state.bmds_list); |
1019 | QSIMPLEQ_INIT(&block_mig_state.blk_list); | |
52e850de | 1020 | qemu_mutex_init(&block_mig_state.lock); |
5e5328be | 1021 | |
ce62df53 | 1022 | register_savevm_live("block", 0, 1, &savevm_block_handlers, |
7908c78d | 1023 | &block_mig_state); |
c163b5ca | 1024 | } |