Patchwork [6/6] Merge mig_save_device_bulk and mig_read_device_bulk

login
register
mail settings
Submitter Pierre Riteau
Date Nov. 26, 2009, 6:44 p.m.
Message ID <1259261046-32650-7-git-send-email-Pierre.Riteau@irisa.fr>
Download mbox | patch
Permalink /patch/39582/
State New
Headers show

Comments

Pierre Riteau - Nov. 26, 2009, 6:44 p.m.
Signed-off-by: Pierre Riteau <Pierre.Riteau@irisa.fr>
---
 block-migration.c |  138 ++++++++++++++++------------------------------------
 1 files changed, 43 insertions(+), 95 deletions(-)

Patch

diff --git a/block-migration.c b/block-migration.c
index e09db78..558890b 100644
--- a/block-migration.c
+++ b/block-migration.c
@@ -85,83 +85,12 @@  static void blk_mig_read_cb(void *opaque, int ret)
     return;
 }
 
-static int mig_read_device_bulk(QEMUFile *f, BlkMigDevState *bms)
+static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds, int is_async)
 {
     int nr_sectors;
-    int64_t total_sectors, cur_sector = 0;
-    BlockDriverState *bs = bms->bs;
-    BlkMigBlock *blk;
-
-    blk = qemu_malloc(sizeof(BlkMigBlock));
-    blk->buf = qemu_malloc(BLOCK_SIZE);
-
-    cur_sector = bms->cur_sector;
-    total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
-
-    if (bms->shared_base) {
-        while (cur_sector < bms->total_sectors &&
-              !bdrv_is_allocated(bms->bs, cur_sector,
-                                 MAX_IS_ALLOCATED_SEARCH, &nr_sectors)) {
-            cur_sector += nr_sectors;
-        }
-    }
-
-    if (cur_sector >= total_sectors) {
-        bms->cur_sector = total_sectors;
-        qemu_free(blk->buf);
-        qemu_free(blk);
-        return 1;
-    }
-
-    if (cur_sector >= block_mig_state->print_completion) {
-        printf("Completed %" PRId64 " %%\r", cur_sector * 100 / total_sectors);
-        fflush(stdout);
-        block_mig_state->print_completion +=
-            (block_mig_state->sectors_per_block * 10000);
-    }
-
-    /* we going to transfer BLOCK_SIZE any way even if it is not allocated */
-    nr_sectors = block_mig_state->sectors_per_block;
-
-    cur_sector &= ~((int64_t)block_mig_state->sectors_per_block - 1);
-
-    if (total_sectors - cur_sector < block_mig_state->sectors_per_block) {
-        nr_sectors = (total_sectors - cur_sector);
-    }
-
-    bms->cur_sector = cur_sector + nr_sectors;
-    blk->sector = cur_sector;
-    blk->bmds = bms;
-
-    blk->iov.iov_base = blk->buf;
-    blk->iov.iov_len = nr_sectors * SECTOR_SIZE;
-    qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
-
-    blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
-                                nr_sectors, blk_mig_read_cb, blk);
-
-    if (!blk->aiocb) {
-        printf("Error reading sector %" PRId64 "\n", cur_sector);
-        qemu_free(blk->buf);
-        qemu_free(blk);
-        return 0;
-    }
-
-    bdrv_reset_dirty(bms->bs, cur_sector, nr_sectors);
-    block_mig_state->submitted++;
-
-    return (bms->cur_sector >= total_sectors);
-}
-
-static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
-{
-    int len, nr_sectors;
     int64_t total_sectors = bmds->total_sectors, cur_sector = 0;
-    uint8_t *tmp_buf = NULL;
     BlockDriverState *bs = bmds->bs;
 
-    tmp_buf = qemu_malloc(BLOCK_SIZE);
-
     cur_sector = bmds->cur_sector;
 
     if (bmds->shared_base) {
@@ -174,7 +103,6 @@  static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
 
     if (cur_sector >= total_sectors) {
         bmds->cur_sector = total_sectors;
-        qemu_free(tmp_buf);
         return 1;
     }
 
@@ -194,24 +122,51 @@  static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
         nr_sectors = (total_sectors - cur_sector);
     }
 
-    if (bdrv_read(bs, cur_sector, tmp_buf, nr_sectors) < 0) {
-        printf("Error reading sector %" PRId64 "\n", cur_sector);
-    }
-
     bdrv_reset_dirty(bs, cur_sector, nr_sectors);
 
-    /* Device name */
-    qemu_put_be64(f, (cur_sector << SECTOR_BITS) | BLK_MIG_FLAG_DEVICE_BLOCK);
+    if (is_async) {
+        BlkMigBlock *blk = qemu_malloc(sizeof(BlkMigBlock));
+        blk->buf = qemu_malloc(BLOCK_SIZE);
 
-    len = strlen(bs->device_name);
-    qemu_put_byte(f, len);
-    qemu_put_buffer(f, (uint8_t *)bs->device_name, len);
+        blk->sector = cur_sector;
+        blk->bmds = bmds;
+
+        blk->iov.iov_base = blk->buf;
+        blk->iov.iov_len = nr_sectors * SECTOR_SIZE;
+        qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
+
+        blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
+                nr_sectors, blk_mig_read_cb, blk);
+
+        if (!blk->aiocb) {
+            printf("Error reading sector %" PRId64 "\n", cur_sector);
+            qemu_free(blk->buf);
+            qemu_free(blk);
+            return 0;
+        }
+
+        block_mig_state->submitted++;
+    } else {
+        int len;
+        uint8_t *tmp_buf = qemu_malloc(BLOCK_SIZE);
+
+        if (bdrv_read(bs, cur_sector, tmp_buf, nr_sectors) < 0) {
+            printf("Error reading sector %" PRId64 "\n", cur_sector);
+        }
+
+        /* Device name */
+        qemu_put_be64(f, (cur_sector << SECTOR_BITS) | BLK_MIG_FLAG_DEVICE_BLOCK);
 
-    qemu_put_buffer(f, tmp_buf, BLOCK_SIZE);
+        len = strlen(bs->device_name);
+        qemu_put_byte(f, len);
+        qemu_put_buffer(f, (uint8_t *)bs->device_name, len);
 
-    bmds->cur_sector = cur_sector + block_mig_state->sectors_per_block;
+        qemu_put_buffer(f, tmp_buf, BLOCK_SIZE);
+
+        qemu_free(tmp_buf);
+    }
 
-    qemu_free(tmp_buf);
+    bmds->cur_sector = cur_sector + nr_sectors;
 
     return (bmds->cur_sector >= total_sectors);
 }
@@ -282,16 +237,9 @@  static int blk_mig_save_bulked_block(QEMUFile *f, int is_async)
 
     QSIMPLEQ_FOREACH(bmds, &block_mig_state->bmds_list, entries) {
         if (bmds->bulk_completed == 0) {
-            if (is_async) {
-                if (mig_read_device_bulk(f, bmds) == 1) {
-                    /* completed bulk section for this device */
-                    bmds->bulk_completed = 1;
-                }
-            } else {
-                if (mig_save_device_bulk(f, bmds) == 1) {
-                    /* completed bulk section for this device */
-                    bmds->bulk_completed = 1;
-                }
+            if (mig_save_device_bulk(f, bmds, is_async) == 1) {
+                /* completed bulk section for this device */
+                bmds->bulk_completed = 1;
             }
             return 1;
         }