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