Remove unused code
[qemu/ar7.git] / block-migration.c
blob42320b28ebe1de1defc1c32f2a3ad454ba8333c9
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 "monitor.h"
19 #include "block-migration.h"
20 #include <assert.h>
22 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
24 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
25 #define BLK_MIG_FLAG_EOS 0x02
26 #define BLK_MIG_FLAG_PROGRESS 0x04
28 #define MAX_IS_ALLOCATED_SEARCH 65536
30 //#define DEBUG_BLK_MIGRATION
32 #ifdef DEBUG_BLK_MIGRATION
33 #define DPRINTF(fmt, ...) \
34 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
35 #else
36 #define DPRINTF(fmt, ...) \
37 do { } while (0)
38 #endif
40 typedef struct BlkMigDevState {
41 BlockDriverState *bs;
42 int bulk_completed;
43 int shared_base;
44 int64_t cur_sector;
45 int64_t completed_sectors;
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 total_sector_sum;
71 int prev_progress;
72 int bulk_completed;
73 } BlkMigState;
75 static BlkMigState block_mig_state;
77 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
79 int len;
81 /* sector number and flags */
82 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
83 | BLK_MIG_FLAG_DEVICE_BLOCK);
85 /* device name */
86 len = strlen(blk->bmds->bs->device_name);
87 qemu_put_byte(f, len);
88 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
90 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
93 int blk_mig_active(void)
95 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
98 uint64_t blk_mig_bytes_transferred(void)
100 BlkMigDevState *bmds;
101 uint64_t sum = 0;
103 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
104 sum += bmds->completed_sectors;
106 return sum << BDRV_SECTOR_BITS;
109 uint64_t blk_mig_bytes_remaining(void)
111 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
114 uint64_t blk_mig_bytes_total(void)
116 BlkMigDevState *bmds;
117 uint64_t sum = 0;
119 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
120 sum += bmds->total_sectors;
122 return sum << BDRV_SECTOR_BITS;
125 static void blk_mig_read_cb(void *opaque, int ret)
127 BlkMigBlock *blk = opaque;
129 blk->ret = ret;
131 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
133 block_mig_state.submitted--;
134 block_mig_state.read_done++;
135 assert(block_mig_state.submitted >= 0);
138 static int mig_save_device_bulk(Monitor *mon, QEMUFile *f,
139 BlkMigDevState *bmds)
141 int64_t total_sectors = bmds->total_sectors;
142 int64_t cur_sector = bmds->cur_sector;
143 BlockDriverState *bs = bmds->bs;
144 BlkMigBlock *blk;
145 int nr_sectors;
147 if (bmds->shared_base) {
148 while (cur_sector < total_sectors &&
149 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
150 &nr_sectors)) {
151 cur_sector += nr_sectors;
155 if (cur_sector >= total_sectors) {
156 bmds->cur_sector = bmds->completed_sectors = total_sectors;
157 return 1;
160 bmds->completed_sectors = cur_sector;
162 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
164 /* we are going to transfer a full block even if it is not allocated */
165 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
167 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
168 nr_sectors = total_sectors - cur_sector;
171 blk = qemu_malloc(sizeof(BlkMigBlock));
172 blk->buf = qemu_malloc(BLOCK_SIZE);
173 blk->bmds = bmds;
174 blk->sector = cur_sector;
176 blk->iov.iov_base = blk->buf;
177 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
178 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
180 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
181 nr_sectors, blk_mig_read_cb, blk);
182 if (!blk->aiocb) {
183 goto error;
185 block_mig_state.submitted++;
186 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
187 bmds->cur_sector = cur_sector + nr_sectors;
189 return (bmds->cur_sector >= total_sectors);
191 error:
192 monitor_printf(mon, "Error reading sector %" PRId64 "\n", cur_sector);
193 qemu_file_set_error(f);
194 qemu_free(blk->buf);
195 qemu_free(blk);
196 return 0;
199 static void set_dirty_tracking(int enable)
201 BlkMigDevState *bmds;
203 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
204 bdrv_set_dirty_tracking(bmds->bs, enable);
208 static void init_blk_migration(Monitor *mon, QEMUFile *f)
210 BlkMigDevState *bmds;
211 BlockDriverState *bs;
212 int64_t sectors;
214 block_mig_state.submitted = 0;
215 block_mig_state.read_done = 0;
216 block_mig_state.transferred = 0;
217 block_mig_state.total_sector_sum = 0;
218 block_mig_state.prev_progress = -1;
219 block_mig_state.bulk_completed = 0;
221 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
222 if (bs->type == BDRV_TYPE_HD) {
223 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
224 if (sectors == 0) {
225 continue;
228 bmds = qemu_mallocz(sizeof(BlkMigDevState));
229 bmds->bs = bs;
230 bmds->bulk_completed = 0;
231 bmds->total_sectors = sectors;
232 bmds->completed_sectors = 0;
233 bmds->shared_base = block_mig_state.shared_base;
235 block_mig_state.total_sector_sum += sectors;
237 if (bmds->shared_base) {
238 monitor_printf(mon, "Start migration for %s with shared base "
239 "image\n",
240 bs->device_name);
241 } else {
242 monitor_printf(mon, "Start full migration for %s\n",
243 bs->device_name);
246 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
251 static int blk_mig_save_bulked_block(Monitor *mon, QEMUFile *f)
253 int64_t completed_sector_sum = 0;
254 BlkMigDevState *bmds;
255 int progress;
256 int ret = 0;
258 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
259 if (bmds->bulk_completed == 0) {
260 if (mig_save_device_bulk(mon, f, bmds) == 1) {
261 /* completed bulk section for this device */
262 bmds->bulk_completed = 1;
264 completed_sector_sum += bmds->completed_sectors;
265 ret = 1;
266 break;
267 } else {
268 completed_sector_sum += bmds->completed_sectors;
272 progress = completed_sector_sum * 100 / block_mig_state.total_sector_sum;
273 if (progress != block_mig_state.prev_progress) {
274 block_mig_state.prev_progress = progress;
275 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
276 | BLK_MIG_FLAG_PROGRESS);
277 monitor_printf(mon, "Completed %d %%\r", progress);
278 monitor_flush(mon);
281 return ret;
284 #define MAX_NUM_BLOCKS 4
286 static void blk_mig_save_dirty_blocks(Monitor *mon, QEMUFile *f)
288 BlkMigDevState *bmds;
289 BlkMigBlock blk;
290 int64_t sector;
292 blk.buf = qemu_malloc(BLOCK_SIZE);
294 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
295 for (sector = 0; sector < bmds->cur_sector;) {
296 if (bdrv_get_dirty(bmds->bs, sector)) {
297 if (bdrv_read(bmds->bs, sector, blk.buf,
298 BDRV_SECTORS_PER_DIRTY_CHUNK) < 0) {
299 monitor_printf(mon, "Error reading sector %" PRId64 "\n",
300 sector);
301 qemu_file_set_error(f);
302 qemu_free(blk.buf);
303 return;
305 blk.bmds = bmds;
306 blk.sector = sector;
307 blk_send(f, &blk);
309 bdrv_reset_dirty(bmds->bs, sector,
310 BDRV_SECTORS_PER_DIRTY_CHUNK);
312 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
316 qemu_free(blk.buf);
319 static void flush_blks(QEMUFile* f)
321 BlkMigBlock *blk;
323 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
324 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
325 block_mig_state.transferred);
327 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
328 if (qemu_file_rate_limit(f)) {
329 break;
331 if (blk->ret < 0) {
332 qemu_file_set_error(f);
333 break;
335 blk_send(f, blk);
337 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
338 qemu_free(blk->buf);
339 qemu_free(blk);
341 block_mig_state.read_done--;
342 block_mig_state.transferred++;
343 assert(block_mig_state.read_done >= 0);
346 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
347 block_mig_state.submitted, block_mig_state.read_done,
348 block_mig_state.transferred);
351 static int is_stage2_completed(void)
353 return (block_mig_state.submitted == 0 && block_mig_state.bulk_completed);
356 static void blk_mig_cleanup(Monitor *mon)
358 BlkMigDevState *bmds;
359 BlkMigBlock *blk;
361 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
362 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
363 qemu_free(bmds);
366 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
367 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
368 qemu_free(blk->buf);
369 qemu_free(blk);
372 set_dirty_tracking(0);
374 monitor_printf(mon, "\n");
377 static int block_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
379 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
380 stage, block_mig_state.submitted, block_mig_state.transferred);
382 if (stage < 0) {
383 blk_mig_cleanup(mon);
384 return 0;
387 if (block_mig_state.blk_enable != 1) {
388 /* no need to migrate storage */
389 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
390 return 1;
393 if (stage == 1) {
394 init_blk_migration(mon, f);
396 /* start track dirty blocks */
397 set_dirty_tracking(1);
400 flush_blks(f);
402 if (qemu_file_has_error(f)) {
403 blk_mig_cleanup(mon);
404 return 0;
407 /* control the rate of transfer */
408 while ((block_mig_state.submitted +
409 block_mig_state.read_done) * BLOCK_SIZE <
410 qemu_file_get_rate_limit(f)) {
411 if (blk_mig_save_bulked_block(mon, f) == 0) {
412 /* finished saving bulk on all devices */
413 block_mig_state.bulk_completed = 1;
414 break;
418 flush_blks(f);
420 if (qemu_file_has_error(f)) {
421 blk_mig_cleanup(mon);
422 return 0;
425 if (stage == 3) {
426 /* we know for sure that save bulk is completed */
428 blk_mig_save_dirty_blocks(mon, f);
429 blk_mig_cleanup(mon);
431 /* report completion */
432 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
434 if (qemu_file_has_error(f)) {
435 return 0;
438 monitor_printf(mon, "Block migration completed\n");
441 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
443 return ((stage == 2) && is_stage2_completed());
446 static int block_load(QEMUFile *f, void *opaque, int version_id)
448 static int banner_printed;
449 int len, flags;
450 char device_name[256];
451 int64_t addr;
452 BlockDriverState *bs;
453 uint8_t *buf;
455 do {
456 addr = qemu_get_be64(f);
458 flags = addr & ~BDRV_SECTOR_MASK;
459 addr >>= BDRV_SECTOR_BITS;
461 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
462 /* get device name */
463 len = qemu_get_byte(f);
464 qemu_get_buffer(f, (uint8_t *)device_name, len);
465 device_name[len] = '\0';
467 bs = bdrv_find(device_name);
468 if (!bs) {
469 fprintf(stderr, "Error unknown block device %s\n",
470 device_name);
471 return -EINVAL;
474 buf = qemu_malloc(BLOCK_SIZE);
476 qemu_get_buffer(f, buf, BLOCK_SIZE);
477 bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK);
479 qemu_free(buf);
480 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
481 if (!banner_printed) {
482 printf("Receiving block device images\n");
483 banner_printed = 1;
485 printf("Completed %d %%%c", (int)addr,
486 (addr == 100) ? '\n' : '\r');
487 fflush(stdout);
488 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
489 fprintf(stderr, "Unknown flags\n");
490 return -EINVAL;
492 if (qemu_file_has_error(f)) {
493 return -EIO;
495 } while (!(flags & BLK_MIG_FLAG_EOS));
497 return 0;
500 static void block_set_params(int blk_enable, int shared_base, void *opaque)
502 block_mig_state.blk_enable = blk_enable;
503 block_mig_state.shared_base = shared_base;
505 /* shared base means that blk_enable = 1 */
506 block_mig_state.blk_enable |= shared_base;
509 void blk_mig_init(void)
511 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
512 QSIMPLEQ_INIT(&block_mig_state.blk_list);
514 register_savevm_live("block", 0, 1, block_set_params, block_save_live,
515 NULL, block_load, &block_mig_state);