block migration: Clean up use of total_sectors
[qemu.git] / block-migration.c
blob2f89a4ee9a3fa40fd8e11adbdbc636cbed457fa7
1 /*
2 * QEMU live block migration
4 * Copyright IBM, Corp. 2009
6 * Authors:
7 * Liran Schour <lirans@il.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu-common.h"
15 #include "block_int.h"
16 #include "hw/hw.h"
17 #include "qemu-queue.h"
18 #include "block-migration.h"
19 #include <assert.h>
21 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
23 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
24 #define BLK_MIG_FLAG_EOS 0x02
26 #define MAX_IS_ALLOCATED_SEARCH 65536
27 #define MAX_BLOCKS_READ 10000
28 #define BLOCKS_READ_CHANGE 100
29 #define INITIAL_BLOCKS_READ 100
31 //#define DEBUG_BLK_MIGRATION
33 #ifdef DEBUG_BLK_MIGRATION
34 #define dprintf(fmt, ...) \
35 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
36 #else
37 #define dprintf(fmt, ...) \
38 do { } while (0)
39 #endif
41 typedef struct BlkMigDevState {
42 BlockDriverState *bs;
43 int bulk_completed;
44 int shared_base;
45 int64_t cur_sector;
46 int64_t total_sectors;
47 int64_t dirty;
48 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
49 } BlkMigDevState;
51 typedef struct BlkMigBlock {
52 uint8_t *buf;
53 BlkMigDevState *bmds;
54 int64_t sector;
55 struct iovec iov;
56 QEMUIOVector qiov;
57 BlockDriverAIOCB *aiocb;
58 int ret;
59 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
60 } BlkMigBlock;
62 typedef struct BlkMigState {
63 int blk_enable;
64 int shared_base;
65 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
66 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
67 int submitted;
68 int read_done;
69 int transferred;
70 int64_t print_completion;
71 } BlkMigState;
73 static BlkMigState block_mig_state;
75 static void blk_mig_read_cb(void *opaque, int ret)
77 BlkMigBlock *blk = opaque;
79 blk->ret = ret;
81 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
83 block_mig_state.submitted--;
84 block_mig_state.read_done++;
85 assert(block_mig_state.submitted >= 0);
88 static int mig_read_device_bulk(QEMUFile *f, BlkMigDevState *bms)
90 int nr_sectors;
91 int64_t total_sectors, cur_sector = 0;
92 BlockDriverState *bs = bms->bs;
93 BlkMigBlock *blk;
95 blk = qemu_malloc(sizeof(BlkMigBlock));
96 blk->buf = qemu_malloc(BLOCK_SIZE);
98 cur_sector = bms->cur_sector;
99 total_sectors = bms->total_sectors;
101 if (bms->shared_base) {
102 while (cur_sector < total_sectors &&
103 !bdrv_is_allocated(bms->bs, cur_sector,
104 MAX_IS_ALLOCATED_SEARCH, &nr_sectors)) {
105 cur_sector += nr_sectors;
109 if (cur_sector >= total_sectors) {
110 bms->cur_sector = total_sectors;
111 qemu_free(blk->buf);
112 qemu_free(blk);
113 return 1;
116 if (cur_sector >= block_mig_state.print_completion) {
117 printf("Completed %" PRId64 " %%\r", cur_sector * 100 / total_sectors);
118 fflush(stdout);
119 block_mig_state.print_completion +=
120 (BDRV_SECTORS_PER_DIRTY_CHUNK * 10000);
123 /* we are going to transfer a full block even if it is not allocated */
124 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
126 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
128 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
129 nr_sectors = (total_sectors - cur_sector);
132 bms->cur_sector = cur_sector + nr_sectors;
133 blk->sector = cur_sector;
134 blk->bmds = bms;
136 blk->iov.iov_base = blk->buf;
137 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
138 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
140 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
141 nr_sectors, blk_mig_read_cb, blk);
143 if (!blk->aiocb) {
144 printf("Error reading sector %" PRId64 "\n", cur_sector);
145 qemu_free(blk->buf);
146 qemu_free(blk);
147 return 0;
150 bdrv_reset_dirty(bms->bs, cur_sector, nr_sectors);
151 block_mig_state.submitted++;
153 return (bms->cur_sector >= total_sectors);
156 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
158 int len, nr_sectors;
159 int64_t total_sectors = bmds->total_sectors, cur_sector = 0;
160 uint8_t *tmp_buf = NULL;
161 BlockDriverState *bs = bmds->bs;
163 tmp_buf = qemu_malloc(BLOCK_SIZE);
165 cur_sector = bmds->cur_sector;
167 if (bmds->shared_base) {
168 while (cur_sector < total_sectors &&
169 !bdrv_is_allocated(bmds->bs, cur_sector,
170 MAX_IS_ALLOCATED_SEARCH, &nr_sectors)) {
171 cur_sector += nr_sectors;
175 if (cur_sector >= total_sectors) {
176 bmds->cur_sector = total_sectors;
177 qemu_free(tmp_buf);
178 return 1;
181 if (cur_sector >= block_mig_state.print_completion) {
182 printf("Completed %" PRId64 " %%\r", cur_sector * 100 / total_sectors);
183 fflush(stdout);
184 block_mig_state.print_completion +=
185 (BDRV_SECTORS_PER_DIRTY_CHUNK * 10000);
188 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
190 /* we are going to transfer a full block even if it is not allocated */
191 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
193 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
194 nr_sectors = (total_sectors - cur_sector);
197 if (bdrv_read(bs, cur_sector, tmp_buf, nr_sectors) < 0) {
198 printf("Error reading sector %" PRId64 "\n", cur_sector);
201 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
203 /* sector number and flags */
204 qemu_put_be64(f, (cur_sector << BDRV_SECTOR_BITS)
205 | BLK_MIG_FLAG_DEVICE_BLOCK);
207 /* device name */
208 len = strlen(bs->device_name);
209 qemu_put_byte(f, len);
210 qemu_put_buffer(f, (uint8_t *)bs->device_name, len);
212 qemu_put_buffer(f, tmp_buf, BLOCK_SIZE);
214 bmds->cur_sector = cur_sector + BDRV_SECTORS_PER_DIRTY_CHUNK;
216 qemu_free(tmp_buf);
218 return (bmds->cur_sector >= total_sectors);
221 static void send_blk(QEMUFile *f, BlkMigBlock * blk)
223 int len;
225 /* sector number and flags */
226 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
227 | BLK_MIG_FLAG_DEVICE_BLOCK);
229 /* device name */
230 len = strlen(blk->bmds->bs->device_name);
231 qemu_put_byte(f, len);
232 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
234 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
237 static void set_dirty_tracking(int enable)
239 BlkMigDevState *bmds;
241 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
242 bdrv_set_dirty_tracking(bmds->bs, enable);
246 static void init_blk_migration(QEMUFile *f)
248 BlkMigDevState *bmds;
249 BlockDriverState *bs;
251 block_mig_state.submitted = 0;
252 block_mig_state.read_done = 0;
253 block_mig_state.transferred = 0;
254 block_mig_state.print_completion = 0;
256 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
257 if (bs->type == BDRV_TYPE_HD) {
258 bmds = qemu_mallocz(sizeof(BlkMigDevState));
259 bmds->bs = bs;
260 bmds->bulk_completed = 0;
261 bmds->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
262 bmds->shared_base = block_mig_state.shared_base;
264 if (bmds->shared_base) {
265 printf("Start migration for %s with shared base image\n",
266 bs->device_name);
267 } else {
268 printf("Start full migration for %s\n", bs->device_name);
271 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
276 static int blk_mig_save_bulked_block(QEMUFile *f, int is_async)
278 BlkMigDevState *bmds;
280 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
281 if (bmds->bulk_completed == 0) {
282 if (is_async) {
283 if (mig_read_device_bulk(f, bmds) == 1) {
284 /* completed bulk section for this device */
285 bmds->bulk_completed = 1;
287 } else {
288 if (mig_save_device_bulk(f, bmds) == 1) {
289 /* completed bulk section for this device */
290 bmds->bulk_completed = 1;
293 return 1;
297 /* we reached here means bulk is completed */
298 return 0;
301 #define MAX_NUM_BLOCKS 4
303 static void blk_mig_save_dirty_blocks(QEMUFile *f)
305 BlkMigDevState *bmds;
306 uint8_t *buf;
307 int64_t sector;
308 int len;
310 buf = qemu_malloc(BLOCK_SIZE);
312 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
313 for (sector = 0; sector < bmds->cur_sector;) {
314 if (bdrv_get_dirty(bmds->bs, sector)) {
315 if (bdrv_read(bmds->bs, sector, buf,
316 BDRV_SECTORS_PER_DIRTY_CHUNK) < 0) {
317 /* FIXME: add error handling */
320 /* sector number and flags */
321 qemu_put_be64(f, (sector << BDRV_SECTOR_BITS)
322 | BLK_MIG_FLAG_DEVICE_BLOCK);
324 /* device name */
325 len = strlen(bmds->bs->device_name);
326 qemu_put_byte(f, len);
327 qemu_put_buffer(f, (uint8_t *)bmds->bs->device_name, len);
329 qemu_put_buffer(f, buf, BLOCK_SIZE);
331 bdrv_reset_dirty(bmds->bs, sector,
332 BDRV_SECTORS_PER_DIRTY_CHUNK);
334 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
338 qemu_free(buf);
341 static void flush_blks(QEMUFile* f)
343 BlkMigBlock *blk;
345 dprintf("%s Enter submitted %d read_done %d transferred %d\n",
346 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
347 block_mig_state.transferred);
349 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
350 if (qemu_file_rate_limit(f)) {
351 break;
353 send_blk(f, blk);
355 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
356 qemu_free(blk->buf);
357 qemu_free(blk);
359 block_mig_state.read_done--;
360 block_mig_state.transferred++;
361 assert(block_mig_state.read_done >= 0);
364 dprintf("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
365 block_mig_state.submitted, block_mig_state.read_done,
366 block_mig_state.transferred);
369 static int is_stage2_completed(void)
371 BlkMigDevState *bmds;
373 if (block_mig_state.submitted > 0) {
374 return 0;
377 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
378 if (bmds->bulk_completed == 0) {
379 return 0;
383 return 1;
386 static int block_save_live(QEMUFile *f, int stage, void *opaque)
388 dprintf("Enter save live stage %d submitted %d transferred %d\n",
389 stage, block_mig_state.submitted, block_mig_state.transferred);
391 if (block_mig_state.blk_enable != 1) {
392 /* no need to migrate storage */
393 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
394 return 1;
397 if (stage == 1) {
398 init_blk_migration(f);
400 /* start track dirty blocks */
401 set_dirty_tracking(1);
404 flush_blks(f);
406 /* control the rate of transfer */
407 while ((block_mig_state.submitted +
408 block_mig_state.read_done) * BLOCK_SIZE <
409 qemu_file_get_rate_limit(f)) {
410 if (blk_mig_save_bulked_block(f, 1) == 0) {
411 /* no more bulk blocks for now */
412 break;
416 flush_blks(f);
418 if (stage == 3) {
419 while (blk_mig_save_bulked_block(f, 0) != 0) {
420 /* empty */
423 blk_mig_save_dirty_blocks(f);
425 /* stop track dirty blocks */
426 set_dirty_tracking(0);
428 printf("\nBlock migration completed\n");
431 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
433 return ((stage == 2) && is_stage2_completed());
436 static int block_load(QEMUFile *f, void *opaque, int version_id)
438 int len, flags;
439 char device_name[256];
440 int64_t addr;
441 BlockDriverState *bs;
442 uint8_t *buf;
444 do {
445 addr = qemu_get_be64(f);
447 flags = addr & ~BDRV_SECTOR_MASK;
448 addr >>= BDRV_SECTOR_BITS;
450 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
451 /* get device name */
452 len = qemu_get_byte(f);
454 qemu_get_buffer(f, (uint8_t *)device_name, len);
455 device_name[len] = '\0';
457 bs = bdrv_find(device_name);
459 buf = qemu_malloc(BLOCK_SIZE);
461 qemu_get_buffer(f, buf, BLOCK_SIZE);
462 if (bs != NULL) {
463 bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK);
464 } else {
465 printf("Error unknown block device %s\n", device_name);
466 /* FIXME: add error handling */
469 qemu_free(buf);
470 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
471 printf("Unknown flags\n");
472 /* FIXME: add error handling */
474 } while (!(flags & BLK_MIG_FLAG_EOS));
476 return 0;
479 static void block_set_params(int blk_enable, int shared_base, void *opaque)
481 block_mig_state.blk_enable = blk_enable;
482 block_mig_state.shared_base = shared_base;
484 /* shared base means that blk_enable = 1 */
485 block_mig_state.blk_enable |= shared_base;
488 void blk_mig_init(void)
490 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
491 QSIMPLEQ_INIT(&block_mig_state.blk_list);
493 register_savevm_live("block", 0, 1, block_set_params, block_save_live,
494 NULL, block_load, &block_mig_state);