]>
Commit | Line | Data |
---|---|---|
c163b5ca | 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 | 14 | */ |
15 | ||
16 | #include "qemu-common.h" | |
737e150e | 17 | #include "block/block_int.h" |
c163b5ca | 18 | #include "hw/hw.h" |
1de7afc9 PB |
19 | #include "qemu/queue.h" |
20 | #include "qemu/timer.h" | |
caf71f86 PB |
21 | #include "migration/block.h" |
22 | #include "migration/migration.h" | |
9c17d615 | 23 | #include "sysemu/blockdev.h" |
c163b5ca | 24 | #include <assert.h> |
c163b5ca | 25 | |
6ea44308 | 26 | #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS) |
c163b5ca | 27 | |
28 | #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01 | |
29 | #define BLK_MIG_FLAG_EOS 0x02 | |
01e61e2d | 30 | #define BLK_MIG_FLAG_PROGRESS 0x04 |
c163b5ca | 31 | |
32 | #define MAX_IS_ALLOCATED_SEARCH 65536 | |
c163b5ca | 33 | |
34 | //#define DEBUG_BLK_MIGRATION | |
35 | ||
36 | #ifdef DEBUG_BLK_MIGRATION | |
d0f2c4c6 | 37 | #define DPRINTF(fmt, ...) \ |
c163b5ca | 38 | do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0) |
39 | #else | |
d0f2c4c6 | 40 | #define DPRINTF(fmt, ...) \ |
c163b5ca | 41 | do { } while (0) |
42 | #endif | |
43 | ||
a55eb92c JK |
44 | typedef struct BlkMigDevState { |
45 | BlockDriverState *bs; | |
46 | int bulk_completed; | |
47 | int shared_base; | |
a55eb92c | 48 | int64_t cur_sector; |
d76cac7d | 49 | int64_t cur_dirty; |
82801d8f | 50 | int64_t completed_sectors; |
a55eb92c JK |
51 | int64_t total_sectors; |
52 | int64_t dirty; | |
5e5328be | 53 | QSIMPLEQ_ENTRY(BlkMigDevState) entry; |
33656af7 | 54 | unsigned long *aio_bitmap; |
a55eb92c JK |
55 | } BlkMigDevState; |
56 | ||
c163b5ca | 57 | typedef struct BlkMigBlock { |
58 | uint8_t *buf; | |
59 | BlkMigDevState *bmds; | |
60 | int64_t sector; | |
33656af7 | 61 | int nr_sectors; |
c163b5ca | 62 | struct iovec iov; |
63 | QEMUIOVector qiov; | |
64 | BlockDriverAIOCB *aiocb; | |
65 | int ret; | |
5e5328be | 66 | QSIMPLEQ_ENTRY(BlkMigBlock) entry; |
c163b5ca | 67 | } BlkMigBlock; |
68 | ||
69 | typedef struct BlkMigState { | |
c163b5ca | 70 | int blk_enable; |
71 | int shared_base; | |
5e5328be JK |
72 | QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list; |
73 | QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list; | |
c163b5ca | 74 | int submitted; |
75 | int read_done; | |
76 | int transferred; | |
82801d8f | 77 | int64_t total_sector_sum; |
01e61e2d | 78 | int prev_progress; |
e970ec0b | 79 | int bulk_completed; |
ff5c52a3 | 80 | long double prev_time_offset; |
c163b5ca | 81 | } BlkMigState; |
82 | ||
d11ecd3d | 83 | static BlkMigState block_mig_state; |
c163b5ca | 84 | |
13f0b67f JK |
85 | static void blk_send(QEMUFile *f, BlkMigBlock * blk) |
86 | { | |
87 | int len; | |
88 | ||
89 | /* sector number and flags */ | |
90 | qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS) | |
91 | | BLK_MIG_FLAG_DEVICE_BLOCK); | |
92 | ||
93 | /* device name */ | |
94 | len = strlen(blk->bmds->bs->device_name); | |
95 | qemu_put_byte(f, len); | |
96 | qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len); | |
97 | ||
98 | qemu_put_buffer(f, blk->buf, BLOCK_SIZE); | |
99 | } | |
100 | ||
25f23643 JK |
101 | int blk_mig_active(void) |
102 | { | |
103 | return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list); | |
104 | } | |
105 | ||
106 | uint64_t blk_mig_bytes_transferred(void) | |
107 | { | |
108 | BlkMigDevState *bmds; | |
109 | uint64_t sum = 0; | |
110 | ||
111 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
112 | sum += bmds->completed_sectors; | |
113 | } | |
114 | return sum << BDRV_SECTOR_BITS; | |
115 | } | |
116 | ||
117 | uint64_t blk_mig_bytes_remaining(void) | |
118 | { | |
119 | return blk_mig_bytes_total() - blk_mig_bytes_transferred(); | |
120 | } | |
121 | ||
122 | uint64_t blk_mig_bytes_total(void) | |
123 | { | |
124 | BlkMigDevState *bmds; | |
125 | uint64_t sum = 0; | |
126 | ||
127 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
128 | sum += bmds->total_sectors; | |
129 | } | |
130 | return sum << BDRV_SECTOR_BITS; | |
131 | } | |
132 | ||
33656af7 MT |
133 | static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector) |
134 | { | |
135 | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; | |
136 | ||
62155e2b | 137 | if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) { |
33656af7 MT |
138 | return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
139 | (1UL << (chunk % (sizeof(unsigned long) * 8)))); | |
140 | } else { | |
141 | return 0; | |
142 | } | |
143 | } | |
144 | ||
145 | static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num, | |
146 | int nb_sectors, int set) | |
147 | { | |
148 | int64_t start, end; | |
149 | unsigned long val, idx, bit; | |
150 | ||
151 | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; | |
152 | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; | |
153 | ||
154 | for (; start <= end; start++) { | |
155 | idx = start / (sizeof(unsigned long) * 8); | |
156 | bit = start % (sizeof(unsigned long) * 8); | |
157 | val = bmds->aio_bitmap[idx]; | |
158 | if (set) { | |
62155e2b | 159 | val |= 1UL << bit; |
33656af7 | 160 | } else { |
62155e2b | 161 | val &= ~(1UL << bit); |
33656af7 MT |
162 | } |
163 | bmds->aio_bitmap[idx] = val; | |
164 | } | |
165 | } | |
166 | ||
167 | static void alloc_aio_bitmap(BlkMigDevState *bmds) | |
168 | { | |
169 | BlockDriverState *bs = bmds->bs; | |
170 | int64_t bitmap_size; | |
171 | ||
172 | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + | |
173 | BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; | |
174 | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; | |
175 | ||
7267c094 | 176 | bmds->aio_bitmap = g_malloc0(bitmap_size); |
33656af7 MT |
177 | } |
178 | ||
c163b5ca | 179 | static void blk_mig_read_cb(void *opaque, int ret) |
180 | { | |
ff5c52a3 | 181 | long double curr_time = qemu_get_clock_ns(rt_clock); |
c163b5ca | 182 | BlkMigBlock *blk = opaque; |
a55eb92c | 183 | |
c163b5ca | 184 | blk->ret = ret; |
a55eb92c | 185 | |
ff5c52a3 | 186 | block_mig_state.prev_time_offset = curr_time; |
889ae39c | 187 | |
5e5328be | 188 | QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry); |
33656af7 | 189 | bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0); |
a55eb92c | 190 | |
d11ecd3d JK |
191 | block_mig_state.submitted--; |
192 | block_mig_state.read_done++; | |
193 | assert(block_mig_state.submitted >= 0); | |
c163b5ca | 194 | } |
195 | ||
539de124 | 196 | static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) |
a55eb92c | 197 | { |
57cce12d JK |
198 | int64_t total_sectors = bmds->total_sectors; |
199 | int64_t cur_sector = bmds->cur_sector; | |
200 | BlockDriverState *bs = bmds->bs; | |
c163b5ca | 201 | BlkMigBlock *blk; |
13f0b67f | 202 | int nr_sectors; |
a55eb92c | 203 | |
57cce12d | 204 | if (bmds->shared_base) { |
b1d10856 | 205 | while (cur_sector < total_sectors && |
57cce12d JK |
206 | !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH, |
207 | &nr_sectors)) { | |
c163b5ca | 208 | cur_sector += nr_sectors; |
209 | } | |
210 | } | |
a55eb92c JK |
211 | |
212 | if (cur_sector >= total_sectors) { | |
82801d8f | 213 | bmds->cur_sector = bmds->completed_sectors = total_sectors; |
c163b5ca | 214 | return 1; |
215 | } | |
a55eb92c | 216 | |
82801d8f | 217 | bmds->completed_sectors = cur_sector; |
a55eb92c | 218 | |
57cce12d JK |
219 | cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1); |
220 | ||
6ea44308 JK |
221 | /* we are going to transfer a full block even if it is not allocated */ |
222 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; | |
c163b5ca | 223 | |
6ea44308 | 224 | if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) { |
57cce12d | 225 | nr_sectors = total_sectors - cur_sector; |
c163b5ca | 226 | } |
a55eb92c | 227 | |
7267c094 AL |
228 | blk = g_malloc(sizeof(BlkMigBlock)); |
229 | blk->buf = g_malloc(BLOCK_SIZE); | |
13f0b67f JK |
230 | blk->bmds = bmds; |
231 | blk->sector = cur_sector; | |
33656af7 | 232 | blk->nr_sectors = nr_sectors; |
a55eb92c | 233 | |
e970ec0b LS |
234 | blk->iov.iov_base = blk->buf; |
235 | blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE; | |
236 | qemu_iovec_init_external(&blk->qiov, &blk->iov, 1); | |
a55eb92c | 237 | |
ff5c52a3 AT |
238 | if (block_mig_state.submitted == 0) { |
239 | block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock); | |
240 | } | |
889ae39c | 241 | |
e970ec0b LS |
242 | blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov, |
243 | nr_sectors, blk_mig_read_cb, blk); | |
e970ec0b | 244 | block_mig_state.submitted++; |
d76cac7d | 245 | |
13f0b67f JK |
246 | bdrv_reset_dirty(bs, cur_sector, nr_sectors); |
247 | bmds->cur_sector = cur_sector + nr_sectors; | |
a55eb92c | 248 | |
13f0b67f | 249 | return (bmds->cur_sector >= total_sectors); |
c163b5ca | 250 | } |
251 | ||
c163b5ca | 252 | static void set_dirty_tracking(int enable) |
253 | { | |
254 | BlkMigDevState *bmds; | |
5e5328be JK |
255 | |
256 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
a55eb92c | 257 | bdrv_set_dirty_tracking(bmds->bs, enable); |
c163b5ca | 258 | } |
c163b5ca | 259 | } |
260 | ||
b66460e4 | 261 | static void init_blk_migration_it(void *opaque, BlockDriverState *bs) |
c163b5ca | 262 | { |
5e5328be | 263 | BlkMigDevState *bmds; |
792773b2 | 264 | int64_t sectors; |
a55eb92c | 265 | |
d246673d | 266 | if (!bdrv_is_read_only(bs)) { |
b66460e4 | 267 | sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
31f54f24 | 268 | if (sectors <= 0) { |
b66460e4 SH |
269 | return; |
270 | } | |
271 | ||
7267c094 | 272 | bmds = g_malloc0(sizeof(BlkMigDevState)); |
b66460e4 SH |
273 | bmds->bs = bs; |
274 | bmds->bulk_completed = 0; | |
275 | bmds->total_sectors = sectors; | |
276 | bmds->completed_sectors = 0; | |
277 | bmds->shared_base = block_mig_state.shared_base; | |
33656af7 | 278 | alloc_aio_bitmap(bmds); |
f48905d4 | 279 | drive_get_ref(drive_get_by_blockdev(bs)); |
8591675f | 280 | bdrv_set_in_use(bs, 1); |
b66460e4 SH |
281 | |
282 | block_mig_state.total_sector_sum += sectors; | |
283 | ||
284 | if (bmds->shared_base) { | |
539de124 LC |
285 | DPRINTF("Start migration for %s with shared base image\n", |
286 | bs->device_name); | |
b66460e4 | 287 | } else { |
539de124 | 288 | DPRINTF("Start full migration for %s\n", bs->device_name); |
b66460e4 SH |
289 | } |
290 | ||
291 | QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry); | |
292 | } | |
293 | } | |
294 | ||
539de124 | 295 | static void init_blk_migration(QEMUFile *f) |
b66460e4 | 296 | { |
69d63a97 JK |
297 | block_mig_state.submitted = 0; |
298 | block_mig_state.read_done = 0; | |
299 | block_mig_state.transferred = 0; | |
82801d8f | 300 | block_mig_state.total_sector_sum = 0; |
01e61e2d | 301 | block_mig_state.prev_progress = -1; |
e970ec0b | 302 | block_mig_state.bulk_completed = 0; |
69d63a97 | 303 | |
539de124 | 304 | bdrv_iterate(init_blk_migration_it, NULL); |
c163b5ca | 305 | } |
306 | ||
539de124 | 307 | static int blk_mig_save_bulked_block(QEMUFile *f) |
c163b5ca | 308 | { |
82801d8f | 309 | int64_t completed_sector_sum = 0; |
c163b5ca | 310 | BlkMigDevState *bmds; |
01e61e2d | 311 | int progress; |
82801d8f | 312 | int ret = 0; |
c163b5ca | 313 | |
5e5328be | 314 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { |
a55eb92c | 315 | if (bmds->bulk_completed == 0) { |
539de124 | 316 | if (mig_save_device_bulk(f, bmds) == 1) { |
57cce12d JK |
317 | /* completed bulk section for this device */ |
318 | bmds->bulk_completed = 1; | |
c163b5ca | 319 | } |
82801d8f JK |
320 | completed_sector_sum += bmds->completed_sectors; |
321 | ret = 1; | |
322 | break; | |
323 | } else { | |
324 | completed_sector_sum += bmds->completed_sectors; | |
c163b5ca | 325 | } |
326 | } | |
a55eb92c | 327 | |
8b6b2afc PR |
328 | if (block_mig_state.total_sector_sum != 0) { |
329 | progress = completed_sector_sum * 100 / | |
330 | block_mig_state.total_sector_sum; | |
331 | } else { | |
332 | progress = 100; | |
333 | } | |
01e61e2d JK |
334 | if (progress != block_mig_state.prev_progress) { |
335 | block_mig_state.prev_progress = progress; | |
336 | qemu_put_be64(f, (progress << BDRV_SECTOR_BITS) | |
337 | | BLK_MIG_FLAG_PROGRESS); | |
539de124 | 338 | DPRINTF("Completed %d %%\r", progress); |
82801d8f JK |
339 | } |
340 | ||
341 | return ret; | |
c163b5ca | 342 | } |
343 | ||
d76cac7d | 344 | static void blk_mig_reset_dirty_cursor(void) |
c163b5ca | 345 | { |
346 | BlkMigDevState *bmds; | |
d76cac7d LS |
347 | |
348 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
349 | bmds->cur_dirty = 0; | |
350 | } | |
351 | } | |
352 | ||
539de124 LC |
353 | static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds, |
354 | int is_async) | |
d76cac7d LS |
355 | { |
356 | BlkMigBlock *blk; | |
357 | int64_t total_sectors = bmds->total_sectors; | |
c163b5ca | 358 | int64_t sector; |
d76cac7d | 359 | int nr_sectors; |
dcd1d224 | 360 | int ret = -EIO; |
a55eb92c | 361 | |
d76cac7d | 362 | for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) { |
62155e2b | 363 | if (bmds_aio_inflight(bmds, sector)) { |
922453bc | 364 | bdrv_drain_all(); |
62155e2b | 365 | } |
d76cac7d | 366 | if (bdrv_get_dirty(bmds->bs, sector)) { |
575a58d7 | 367 | |
d76cac7d LS |
368 | if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) { |
369 | nr_sectors = total_sectors - sector; | |
370 | } else { | |
371 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; | |
372 | } | |
7267c094 AL |
373 | blk = g_malloc(sizeof(BlkMigBlock)); |
374 | blk->buf = g_malloc(BLOCK_SIZE); | |
d76cac7d LS |
375 | blk->bmds = bmds; |
376 | blk->sector = sector; | |
33656af7 | 377 | blk->nr_sectors = nr_sectors; |
d76cac7d | 378 | |
889ae39c | 379 | if (is_async) { |
d76cac7d LS |
380 | blk->iov.iov_base = blk->buf; |
381 | blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE; | |
382 | qemu_iovec_init_external(&blk->qiov, &blk->iov, 1); | |
383 | ||
ff5c52a3 AT |
384 | if (block_mig_state.submitted == 0) { |
385 | block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock); | |
386 | } | |
889ae39c | 387 | |
d76cac7d LS |
388 | blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov, |
389 | nr_sectors, blk_mig_read_cb, blk); | |
d76cac7d | 390 | block_mig_state.submitted++; |
33656af7 | 391 | bmds_set_aio_inflight(bmds, sector, nr_sectors, 1); |
d76cac7d | 392 | } else { |
dcd1d224 JQ |
393 | ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors); |
394 | if (ret < 0) { | |
d76cac7d | 395 | goto error; |
c163b5ca | 396 | } |
d76cac7d | 397 | blk_send(f, blk); |
a55eb92c | 398 | |
7267c094 AL |
399 | g_free(blk->buf); |
400 | g_free(blk); | |
a55eb92c | 401 | } |
d76cac7d LS |
402 | |
403 | bdrv_reset_dirty(bmds->bs, sector, nr_sectors); | |
404 | break; | |
c163b5ca | 405 | } |
d76cac7d LS |
406 | sector += BDRV_SECTORS_PER_DIRTY_CHUNK; |
407 | bmds->cur_dirty = sector; | |
c163b5ca | 408 | } |
575a58d7 | 409 | |
d76cac7d LS |
410 | return (bmds->cur_dirty >= bmds->total_sectors); |
411 | ||
889ae39c | 412 | error: |
539de124 | 413 | DPRINTF("Error reading sector %" PRId64 "\n", sector); |
7267c094 AL |
414 | g_free(blk->buf); |
415 | g_free(blk); | |
43be3a25 | 416 | return ret; |
d76cac7d LS |
417 | } |
418 | ||
ceb2bd09 JQ |
419 | /* return value: |
420 | * 0: too much data for max_downtime | |
421 | * 1: few enough data for max_downtime | |
422 | */ | |
539de124 | 423 | static int blk_mig_save_dirty_block(QEMUFile *f, int is_async) |
d76cac7d LS |
424 | { |
425 | BlkMigDevState *bmds; | |
ceb2bd09 | 426 | int ret = 1; |
d76cac7d LS |
427 | |
428 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
ceb2bd09 | 429 | ret = mig_save_device_dirty(f, bmds, is_async); |
43be3a25 | 430 | if (ret <= 0) { |
d76cac7d LS |
431 | break; |
432 | } | |
433 | } | |
434 | ||
435 | return ret; | |
c163b5ca | 436 | } |
437 | ||
59feec42 | 438 | static int flush_blks(QEMUFile *f) |
c163b5ca | 439 | { |
5e5328be | 440 | BlkMigBlock *blk; |
59feec42 | 441 | int ret = 0; |
a55eb92c | 442 | |
d0f2c4c6 | 443 | DPRINTF("%s Enter submitted %d read_done %d transferred %d\n", |
d11ecd3d JK |
444 | __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done, |
445 | block_mig_state.transferred); | |
a55eb92c | 446 | |
5e5328be JK |
447 | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
448 | if (qemu_file_rate_limit(f)) { | |
449 | break; | |
450 | } | |
4b640365 | 451 | if (blk->ret < 0) { |
59feec42 | 452 | ret = blk->ret; |
4b640365 JK |
453 | break; |
454 | } | |
13f0b67f | 455 | blk_send(f, blk); |
a55eb92c | 456 | |
5e5328be | 457 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); |
7267c094 AL |
458 | g_free(blk->buf); |
459 | g_free(blk); | |
a55eb92c | 460 | |
d11ecd3d JK |
461 | block_mig_state.read_done--; |
462 | block_mig_state.transferred++; | |
463 | assert(block_mig_state.read_done >= 0); | |
c163b5ca | 464 | } |
c163b5ca | 465 | |
d0f2c4c6 | 466 | DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__, |
d11ecd3d JK |
467 | block_mig_state.submitted, block_mig_state.read_done, |
468 | block_mig_state.transferred); | |
59feec42 | 469 | return ret; |
c163b5ca | 470 | } |
471 | ||
889ae39c LS |
472 | static int64_t get_remaining_dirty(void) |
473 | { | |
474 | BlkMigDevState *bmds; | |
475 | int64_t dirty = 0; | |
476 | ||
477 | QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { | |
478 | dirty += bdrv_get_dirty_count(bmds->bs); | |
479 | } | |
480 | ||
481 | return dirty * BLOCK_SIZE; | |
482 | } | |
483 | ||
539de124 | 484 | static void blk_mig_cleanup(void) |
4ec7fcc7 | 485 | { |
82801d8f JK |
486 | BlkMigDevState *bmds; |
487 | BlkMigBlock *blk; | |
4ec7fcc7 | 488 | |
946d58be KW |
489 | bdrv_drain_all(); |
490 | ||
8f794c55 MT |
491 | set_dirty_tracking(0); |
492 | ||
82801d8f JK |
493 | while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) { |
494 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry); | |
8591675f | 495 | bdrv_set_in_use(bmds->bs, 0); |
f48905d4 | 496 | drive_put_ref(drive_get_by_blockdev(bmds->bs)); |
7267c094 AL |
497 | g_free(bmds->aio_bitmap); |
498 | g_free(bmds); | |
4ec7fcc7 JK |
499 | } |
500 | ||
82801d8f JK |
501 | while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) { |
502 | QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry); | |
7267c094 AL |
503 | g_free(blk->buf); |
504 | g_free(blk); | |
4ec7fcc7 | 505 | } |
4ec7fcc7 JK |
506 | } |
507 | ||
9b5bfab0 JQ |
508 | static void block_migration_cancel(void *opaque) |
509 | { | |
510 | blk_mig_cleanup(); | |
511 | } | |
512 | ||
d1315aac | 513 | static int block_save_setup(QEMUFile *f, void *opaque) |
c163b5ca | 514 | { |
2975725f JQ |
515 | int ret; |
516 | ||
d1315aac JQ |
517 | DPRINTF("Enter save live setup submitted %d transferred %d\n", |
518 | block_mig_state.submitted, block_mig_state.transferred); | |
a55eb92c | 519 | |
d1315aac JQ |
520 | init_blk_migration(f); |
521 | ||
522 | /* start track dirty blocks */ | |
523 | set_dirty_tracking(1); | |
524 | ||
59feec42 | 525 | ret = flush_blks(f); |
d1315aac JQ |
526 | if (ret) { |
527 | blk_mig_cleanup(); | |
528 | return ret; | |
c163b5ca | 529 | } |
530 | ||
d1315aac JQ |
531 | blk_mig_reset_dirty_cursor(); |
532 | ||
533 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); | |
534 | ||
535 | return 0; | |
536 | } | |
537 | ||
16310a3c | 538 | static int block_save_iterate(QEMUFile *f, void *opaque) |
d1315aac JQ |
539 | { |
540 | int ret; | |
541 | ||
16310a3c JQ |
542 | DPRINTF("Enter save live iterate submitted %d transferred %d\n", |
543 | block_mig_state.submitted, block_mig_state.transferred); | |
d1315aac | 544 | |
59feec42 | 545 | ret = flush_blks(f); |
2975725f | 546 | if (ret) { |
539de124 | 547 | blk_mig_cleanup(); |
2975725f | 548 | return ret; |
4b640365 JK |
549 | } |
550 | ||
d76cac7d LS |
551 | blk_mig_reset_dirty_cursor(); |
552 | ||
16310a3c JQ |
553 | /* control the rate of transfer */ |
554 | while ((block_mig_state.submitted + | |
555 | block_mig_state.read_done) * BLOCK_SIZE < | |
556 | qemu_file_get_rate_limit(f)) { | |
557 | if (block_mig_state.bulk_completed == 0) { | |
558 | /* first finish the bulk phase */ | |
559 | if (blk_mig_save_bulked_block(f) == 0) { | |
560 | /* finished saving bulk on all devices */ | |
561 | block_mig_state.bulk_completed = 1; | |
562 | } | |
563 | } else { | |
43be3a25 JQ |
564 | ret = blk_mig_save_dirty_block(f, 1); |
565 | if (ret != 0) { | |
16310a3c JQ |
566 | /* no more dirty blocks */ |
567 | break; | |
d76cac7d | 568 | } |
a55eb92c | 569 | } |
16310a3c | 570 | } |
43be3a25 JQ |
571 | if (ret) { |
572 | blk_mig_cleanup(); | |
573 | return ret; | |
574 | } | |
a55eb92c | 575 | |
59feec42 | 576 | ret = flush_blks(f); |
16310a3c JQ |
577 | if (ret) { |
578 | blk_mig_cleanup(); | |
579 | return ret; | |
4b640365 JK |
580 | } |
581 | ||
16310a3c JQ |
582 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
583 | ||
e4ed1541 | 584 | return 0; |
16310a3c JQ |
585 | } |
586 | ||
587 | static int block_save_complete(QEMUFile *f, void *opaque) | |
588 | { | |
589 | int ret; | |
590 | ||
591 | DPRINTF("Enter save live complete submitted %d transferred %d\n", | |
592 | block_mig_state.submitted, block_mig_state.transferred); | |
593 | ||
59feec42 | 594 | ret = flush_blks(f); |
16310a3c | 595 | if (ret) { |
539de124 | 596 | blk_mig_cleanup(); |
16310a3c JQ |
597 | return ret; |
598 | } | |
a55eb92c | 599 | |
16310a3c | 600 | blk_mig_reset_dirty_cursor(); |
01e61e2d | 601 | |
16310a3c JQ |
602 | /* we know for sure that save bulk is completed and |
603 | all async read completed */ | |
604 | assert(block_mig_state.submitted == 0); | |
605 | ||
43be3a25 JQ |
606 | do { |
607 | ret = blk_mig_save_dirty_block(f, 0); | |
608 | } while (ret == 0); | |
4b640365 | 609 | |
43be3a25 | 610 | blk_mig_cleanup(); |
16310a3c JQ |
611 | if (ret) { |
612 | return ret; | |
c163b5ca | 613 | } |
43be3a25 JQ |
614 | /* report completion */ |
615 | qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS); | |
a55eb92c | 616 | |
16310a3c JQ |
617 | DPRINTF("Block migration completed\n"); |
618 | ||
a55eb92c JK |
619 | qemu_put_be64(f, BLK_MIG_FLAG_EOS); |
620 | ||
16310a3c | 621 | return 0; |
c163b5ca | 622 | } |
623 | ||
e4ed1541 JQ |
624 | static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size) |
625 | { | |
626 | ||
627 | DPRINTF("Enter save live pending %ld\n", get_remaining_dirty()); | |
628 | ||
629 | return get_remaining_dirty(); | |
630 | } | |
631 | ||
c163b5ca | 632 | static int block_load(QEMUFile *f, void *opaque, int version_id) |
633 | { | |
01e61e2d | 634 | static int banner_printed; |
c163b5ca | 635 | int len, flags; |
636 | char device_name[256]; | |
637 | int64_t addr; | |
77358b59 | 638 | BlockDriverState *bs, *bs_prev = NULL; |
c163b5ca | 639 | uint8_t *buf; |
77358b59 PR |
640 | int64_t total_sectors = 0; |
641 | int nr_sectors; | |
42802d47 | 642 | int ret; |
a55eb92c | 643 | |
c163b5ca | 644 | do { |
c163b5ca | 645 | addr = qemu_get_be64(f); |
a55eb92c | 646 | |
6ea44308 JK |
647 | flags = addr & ~BDRV_SECTOR_MASK; |
648 | addr >>= BDRV_SECTOR_BITS; | |
a55eb92c JK |
649 | |
650 | if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) { | |
c163b5ca | 651 | /* get device name */ |
652 | len = qemu_get_byte(f); | |
c163b5ca | 653 | qemu_get_buffer(f, (uint8_t *)device_name, len); |
654 | device_name[len] = '\0'; | |
a55eb92c | 655 | |
c163b5ca | 656 | bs = bdrv_find(device_name); |
4b640365 JK |
657 | if (!bs) { |
658 | fprintf(stderr, "Error unknown block device %s\n", | |
659 | device_name); | |
660 | return -EINVAL; | |
661 | } | |
a55eb92c | 662 | |
77358b59 PR |
663 | if (bs != bs_prev) { |
664 | bs_prev = bs; | |
665 | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; | |
666 | if (total_sectors <= 0) { | |
6daf194d | 667 | error_report("Error getting length of block device %s", |
77358b59 PR |
668 | device_name); |
669 | return -EINVAL; | |
670 | } | |
671 | } | |
672 | ||
673 | if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) { | |
674 | nr_sectors = total_sectors - addr; | |
675 | } else { | |
676 | nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK; | |
677 | } | |
678 | ||
7267c094 | 679 | buf = g_malloc(BLOCK_SIZE); |
575a58d7 | 680 | |
a55eb92c | 681 | qemu_get_buffer(f, buf, BLOCK_SIZE); |
77358b59 | 682 | ret = bdrv_write(bs, addr, buf, nr_sectors); |
575a58d7 | 683 | |
7267c094 | 684 | g_free(buf); |
b02bea3a YT |
685 | if (ret < 0) { |
686 | return ret; | |
687 | } | |
01e61e2d JK |
688 | } else if (flags & BLK_MIG_FLAG_PROGRESS) { |
689 | if (!banner_printed) { | |
690 | printf("Receiving block device images\n"); | |
691 | banner_printed = 1; | |
692 | } | |
693 | printf("Completed %d %%%c", (int)addr, | |
694 | (addr == 100) ? '\n' : '\r'); | |
695 | fflush(stdout); | |
a55eb92c | 696 | } else if (!(flags & BLK_MIG_FLAG_EOS)) { |
4b640365 JK |
697 | fprintf(stderr, "Unknown flags\n"); |
698 | return -EINVAL; | |
699 | } | |
42802d47 JQ |
700 | ret = qemu_file_get_error(f); |
701 | if (ret != 0) { | |
702 | return ret; | |
c163b5ca | 703 | } |
a55eb92c JK |
704 | } while (!(flags & BLK_MIG_FLAG_EOS)); |
705 | ||
c163b5ca | 706 | return 0; |
707 | } | |
708 | ||
6607ae23 | 709 | static void block_set_params(const MigrationParams *params, void *opaque) |
c163b5ca | 710 | { |
6607ae23 IY |
711 | block_mig_state.blk_enable = params->blk; |
712 | block_mig_state.shared_base = params->shared; | |
a55eb92c | 713 | |
c163b5ca | 714 | /* shared base means that blk_enable = 1 */ |
6607ae23 | 715 | block_mig_state.blk_enable |= params->shared; |
c163b5ca | 716 | } |
717 | ||
6bd68781 JQ |
718 | static bool block_is_active(void *opaque) |
719 | { | |
720 | return block_mig_state.blk_enable == 1; | |
721 | } | |
722 | ||
7908c78d JQ |
723 | SaveVMHandlers savevm_block_handlers = { |
724 | .set_params = block_set_params, | |
d1315aac | 725 | .save_live_setup = block_save_setup, |
16310a3c JQ |
726 | .save_live_iterate = block_save_iterate, |
727 | .save_live_complete = block_save_complete, | |
e4ed1541 | 728 | .save_live_pending = block_save_pending, |
7908c78d | 729 | .load_state = block_load, |
9b5bfab0 | 730 | .cancel = block_migration_cancel, |
6bd68781 | 731 | .is_active = block_is_active, |
7908c78d JQ |
732 | }; |
733 | ||
c163b5ca | 734 | void blk_mig_init(void) |
a55eb92c | 735 | { |
5e5328be JK |
736 | QSIMPLEQ_INIT(&block_mig_state.bmds_list); |
737 | QSIMPLEQ_INIT(&block_mig_state.blk_list); | |
738 | ||
7908c78d JQ |
739 | register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers, |
740 | &block_mig_state); | |
c163b5ca | 741 | } |