]> git.proxmox.com Git - qemu.git/blob - block-migration.c
block migration: Clean up use of total_sectors
[qemu.git] / block-migration.c
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 *
12 */
13
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>
20
21 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
22
23 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
24 #define BLK_MIG_FLAG_EOS 0x02
25
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
30
31 //#define DEBUG_BLK_MIGRATION
32
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
40
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;
50
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;
61
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;
72
73 static BlkMigState block_mig_state;
74
75 static void blk_mig_read_cb(void *opaque, int ret)
76 {
77 BlkMigBlock *blk = opaque;
78
79 blk->ret = ret;
80
81 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
82
83 block_mig_state.submitted--;
84 block_mig_state.read_done++;
85 assert(block_mig_state.submitted >= 0);
86 }
87
88 static int mig_read_device_bulk(QEMUFile *f, BlkMigDevState *bms)
89 {
90 int nr_sectors;
91 int64_t total_sectors, cur_sector = 0;
92 BlockDriverState *bs = bms->bs;
93 BlkMigBlock *blk;
94
95 blk = qemu_malloc(sizeof(BlkMigBlock));
96 blk->buf = qemu_malloc(BLOCK_SIZE);
97
98 cur_sector = bms->cur_sector;
99 total_sectors = bms->total_sectors;
100
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;
106 }
107 }
108
109 if (cur_sector >= total_sectors) {
110 bms->cur_sector = total_sectors;
111 qemu_free(blk->buf);
112 qemu_free(blk);
113 return 1;
114 }
115
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);
121 }
122
123 /* we are going to transfer a full block even if it is not allocated */
124 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
125
126 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
127
128 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
129 nr_sectors = (total_sectors - cur_sector);
130 }
131
132 bms->cur_sector = cur_sector + nr_sectors;
133 blk->sector = cur_sector;
134 blk->bmds = bms;
135
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);
139
140 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
141 nr_sectors, blk_mig_read_cb, blk);
142
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;
148 }
149
150 bdrv_reset_dirty(bms->bs, cur_sector, nr_sectors);
151 block_mig_state.submitted++;
152
153 return (bms->cur_sector >= total_sectors);
154 }
155
156 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
157 {
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;
162
163 tmp_buf = qemu_malloc(BLOCK_SIZE);
164
165 cur_sector = bmds->cur_sector;
166
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;
172 }
173 }
174
175 if (cur_sector >= total_sectors) {
176 bmds->cur_sector = total_sectors;
177 qemu_free(tmp_buf);
178 return 1;
179 }
180
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);
186 }
187
188 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
189
190 /* we are going to transfer a full block even if it is not allocated */
191 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
192
193 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
194 nr_sectors = (total_sectors - cur_sector);
195 }
196
197 if (bdrv_read(bs, cur_sector, tmp_buf, nr_sectors) < 0) {
198 printf("Error reading sector %" PRId64 "\n", cur_sector);
199 }
200
201 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
202
203 /* sector number and flags */
204 qemu_put_be64(f, (cur_sector << BDRV_SECTOR_BITS)
205 | BLK_MIG_FLAG_DEVICE_BLOCK);
206
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);
211
212 qemu_put_buffer(f, tmp_buf, BLOCK_SIZE);
213
214 bmds->cur_sector = cur_sector + BDRV_SECTORS_PER_DIRTY_CHUNK;
215
216 qemu_free(tmp_buf);
217
218 return (bmds->cur_sector >= total_sectors);
219 }
220
221 static void send_blk(QEMUFile *f, BlkMigBlock * blk)
222 {
223 int len;
224
225 /* sector number and flags */
226 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
227 | BLK_MIG_FLAG_DEVICE_BLOCK);
228
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);
233
234 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
235 }
236
237 static void set_dirty_tracking(int enable)
238 {
239 BlkMigDevState *bmds;
240
241 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
242 bdrv_set_dirty_tracking(bmds->bs, enable);
243 }
244 }
245
246 static void init_blk_migration(QEMUFile *f)
247 {
248 BlkMigDevState *bmds;
249 BlockDriverState *bs;
250
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;
255
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;
263
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);
269 }
270
271 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
272 }
273 }
274 }
275
276 static int blk_mig_save_bulked_block(QEMUFile *f, int is_async)
277 {
278 BlkMigDevState *bmds;
279
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;
286 }
287 } else {
288 if (mig_save_device_bulk(f, bmds) == 1) {
289 /* completed bulk section for this device */
290 bmds->bulk_completed = 1;
291 }
292 }
293 return 1;
294 }
295 }
296
297 /* we reached here means bulk is completed */
298 return 0;
299 }
300
301 #define MAX_NUM_BLOCKS 4
302
303 static void blk_mig_save_dirty_blocks(QEMUFile *f)
304 {
305 BlkMigDevState *bmds;
306 uint8_t *buf;
307 int64_t sector;
308 int len;
309
310 buf = qemu_malloc(BLOCK_SIZE);
311
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 */
318 }
319
320 /* sector number and flags */
321 qemu_put_be64(f, (sector << BDRV_SECTOR_BITS)
322 | BLK_MIG_FLAG_DEVICE_BLOCK);
323
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);
328
329 qemu_put_buffer(f, buf, BLOCK_SIZE);
330
331 bdrv_reset_dirty(bmds->bs, sector,
332 BDRV_SECTORS_PER_DIRTY_CHUNK);
333 }
334 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
335 }
336 }
337
338 qemu_free(buf);
339 }
340
341 static void flush_blks(QEMUFile* f)
342 {
343 BlkMigBlock *blk;
344
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);
348
349 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
350 if (qemu_file_rate_limit(f)) {
351 break;
352 }
353 send_blk(f, blk);
354
355 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
356 qemu_free(blk->buf);
357 qemu_free(blk);
358
359 block_mig_state.read_done--;
360 block_mig_state.transferred++;
361 assert(block_mig_state.read_done >= 0);
362 }
363
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);
367 }
368
369 static int is_stage2_completed(void)
370 {
371 BlkMigDevState *bmds;
372
373 if (block_mig_state.submitted > 0) {
374 return 0;
375 }
376
377 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
378 if (bmds->bulk_completed == 0) {
379 return 0;
380 }
381 }
382
383 return 1;
384 }
385
386 static int block_save_live(QEMUFile *f, int stage, void *opaque)
387 {
388 dprintf("Enter save live stage %d submitted %d transferred %d\n",
389 stage, block_mig_state.submitted, block_mig_state.transferred);
390
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;
395 }
396
397 if (stage == 1) {
398 init_blk_migration(f);
399
400 /* start track dirty blocks */
401 set_dirty_tracking(1);
402 }
403
404 flush_blks(f);
405
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;
413 }
414 }
415
416 flush_blks(f);
417
418 if (stage == 3) {
419 while (blk_mig_save_bulked_block(f, 0) != 0) {
420 /* empty */
421 }
422
423 blk_mig_save_dirty_blocks(f);
424
425 /* stop track dirty blocks */
426 set_dirty_tracking(0);
427
428 printf("\nBlock migration completed\n");
429 }
430
431 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
432
433 return ((stage == 2) && is_stage2_completed());
434 }
435
436 static int block_load(QEMUFile *f, void *opaque, int version_id)
437 {
438 int len, flags;
439 char device_name[256];
440 int64_t addr;
441 BlockDriverState *bs;
442 uint8_t *buf;
443
444 do {
445 addr = qemu_get_be64(f);
446
447 flags = addr & ~BDRV_SECTOR_MASK;
448 addr >>= BDRV_SECTOR_BITS;
449
450 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
451 /* get device name */
452 len = qemu_get_byte(f);
453
454 qemu_get_buffer(f, (uint8_t *)device_name, len);
455 device_name[len] = '\0';
456
457 bs = bdrv_find(device_name);
458
459 buf = qemu_malloc(BLOCK_SIZE);
460
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 */
467 }
468
469 qemu_free(buf);
470 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
471 printf("Unknown flags\n");
472 /* FIXME: add error handling */
473 }
474 } while (!(flags & BLK_MIG_FLAG_EOS));
475
476 return 0;
477 }
478
479 static void block_set_params(int blk_enable, int shared_base, void *opaque)
480 {
481 block_mig_state.blk_enable = blk_enable;
482 block_mig_state.shared_base = shared_base;
483
484 /* shared base means that blk_enable = 1 */
485 block_mig_state.blk_enable |= shared_base;
486 }
487
488 void blk_mig_init(void)
489 {
490 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
491 QSIMPLEQ_INIT(&block_mig_state.blk_list);
492
493 register_savevm_live("block", 0, 1, block_set_params, block_save_live,
494 NULL, block_load, &block_mig_state);
495 }