static int mig_save_device_dirty(Monitor *mon, QEMUFile *f,

                                 BlkMigDevState *bmds, int is_async)

{

    BlkMigBlock *blk;

    int64_t total_sectors = bmds->total_sectors;

    int64_t sector;

    int nr_sectors;

    int ret = -EIO;



    for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {

        if (bmds_aio_inflight(bmds, sector)) {

            qemu_aio_flush();

        }

        if (bdrv_get_dirty(bmds->bs, sector)) {



            if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {

                nr_sectors = total_sectors - sector;

            } else {

                nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;

            }

            blk = g_malloc(sizeof(BlkMigBlock));

            blk->buf = g_malloc(BLOCK_SIZE);

            blk->bmds = bmds;

            blk->sector = sector;

            blk->nr_sectors = nr_sectors;



            if (is_async) {

                blk->iov.iov_base = blk->buf;

                blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;

                qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);



                if (block_mig_state.submitted == 0) {

                    block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);

                }



                blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,

                                            nr_sectors, blk_mig_read_cb, blk);

                if (!blk->aiocb) {

                    goto error;

                }

                block_mig_state.submitted++;

                bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);

            } else {

                ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);

                if (ret < 0) {

                    goto error;

                }

                blk_send(f, blk);



                g_free(blk->buf);

                g_free(blk);

            }



            bdrv_reset_dirty(bmds->bs, sector, nr_sectors);

            break;

        }

        sector += BDRV_SECTORS_PER_DIRTY_CHUNK;

        bmds->cur_dirty = sector;

    }



    return (bmds->cur_dirty >= bmds->total_sectors);



error:

    monitor_printf(mon, "Error reading sector %" PRId64 "\n", sector);

    qemu_file_set_error(f, ret);

    g_free(blk->buf);

    g_free(blk);

    return 0;

}

static int mig_save_device_dirty(Monitor *mon, QEMUFile *f,

                                 BlkMigDevState *bmds, int is_async)

{

    BlkMigBlock *blk;

    int64_t total_sectors = bmds->total_sectors;

    int64_t sector;

    int nr_sectors;

    int ret = -EIO;



    for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {

        if (bmds_aio_inflight(bmds, sector)) {

            qemu_aio_flush();

        }

        if (bdrv_get_dirty(bmds->bs, sector)) {



            if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {

                nr_sectors = total_sectors - sector;

            } else {

                nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;

            }

            blk = g_malloc(sizeof(BlkMigBlock));

            blk->buf = g_malloc(BLOCK_SIZE);

            blk->bmds = bmds;

            blk->sector = sector;

            blk->nr_sectors = nr_sectors;



            if (is_async) {

                blk->iov.iov_base = blk->buf;

                blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;

                qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);



                if (block_mig_state.submitted == 0) {

                    block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);

                }



                blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,

                                            nr_sectors, blk_mig_read_cb, blk);

                if (!blk->aiocb) {

                    goto error;

                }

                block_mig_state.submitted++;

                bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);

            } else {

                ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);

                if (ret < 0) {

                    goto error;

                }

                blk_send(f, blk);



                g_free(blk->buf);

                g_free(blk);

            }



            bdrv_reset_dirty(bmds->bs, sector, nr_sectors);

            break;

        }

        sector += BDRV_SECTORS_PER_DIRTY_CHUNK;

        bmds->cur_dirty = sector;

    }



    return (bmds->cur_dirty >= bmds->total_sectors);



error:

    monitor_printf(mon, "Error reading sector %" PRId64 "\n", sector);

    qemu_file_set_error(f, ret);

    g_free(blk->buf);

    g_free(blk);

    return 0;

}

static int FUNC_0(Monitor *VAR_0, QEMUFile *VAR_1,

                                 BlkMigDevState *VAR_2, int VAR_3)

{

    BlkMigBlock *blk;

    int64_t total_sectors = VAR_2->total_sectors;

    int64_t sector;

    int VAR_4;

    int VAR_5 = -EIO;



    for (sector = VAR_2->cur_dirty; sector < VAR_2->total_sectors;) {

        if (bmds_aio_inflight(VAR_2, sector)) {

            qemu_aio_flush();

        }

        if (bdrv_get_dirty(VAR_2->bs, sector)) {



            if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {

                VAR_4 = total_sectors - sector;

            } else {

                VAR_4 = BDRV_SECTORS_PER_DIRTY_CHUNK;

            }

            blk = g_malloc(sizeof(BlkMigBlock));

            blk->buf = g_malloc(BLOCK_SIZE);

            blk->VAR_2 = VAR_2;

            blk->sector = sector;

            blk->VAR_4 = VAR_4;



            if (VAR_3) {

                blk->iov.iov_base = blk->buf;

                blk->iov.iov_len = VAR_4 * BDRV_SECTOR_SIZE;

                qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);



                if (block_mig_state.submitted == 0) {

                    block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);

                }



                blk->aiocb = bdrv_aio_readv(VAR_2->bs, sector, &blk->qiov,

                                            VAR_4, blk_mig_read_cb, blk);

                if (!blk->aiocb) {

                    goto error;

                }

                block_mig_state.submitted++;

                bmds_set_aio_inflight(VAR_2, sector, VAR_4, 1);

            } else {

                VAR_5 = bdrv_read(VAR_2->bs, sector, blk->buf, VAR_4);

                if (VAR_5 < 0) {

                    goto error;

                }

                blk_send(VAR_1, blk);



                g_free(blk->buf);

                g_free(blk);

            }



            bdrv_reset_dirty(VAR_2->bs, sector, VAR_4);

            break;

        }

        sector += BDRV_SECTORS_PER_DIRTY_CHUNK;

        VAR_2->cur_dirty = sector;

    }



    return (VAR_2->cur_dirty >= VAR_2->total_sectors);



error:

    monitor_printf(VAR_0, "Error reading sector %" PRId64 "\n", sector);

    qemu_file_set_error(VAR_1, VAR_5);

    g_free(blk->buf);

    g_free(blk);

    return 0;

}

static int FUNC_0(Monitor *VAR_0, QEMUFile *VAR_1,
BlkMigDevState *VAR_2, int VAR_3)
{ BlkMigBlock *blk; int64_t total_sectors = VAR_2->total_sectors; int64_t sector; int VAR_4; int VAR_5 = -EIO; for (sector = VAR_2->cur_dirty; sector < VAR_2->total_sectors;) { if (bmds_aio_inflight(VAR_2, sector)) { qemu_aio_flush(); } if (bdrv_get_dirty(VAR_2->bs, sector)) { if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) { VAR_4 = total_sectors - sector; } else { VAR_4 = BDRV_SECTORS_PER_DIRTY_CHUNK; } blk = g_malloc(sizeof(BlkMigBlock)); blk->buf = g_malloc(BLOCK_SIZE); blk->VAR_2 = VAR_2; blk->sector = sector; blk->VAR_4 = VAR_4; if (VAR_3) { blk->iov.iov_base = blk->buf; blk->iov.iov_len = VAR_4 * BDRV_SECTOR_SIZE; qemu_iovec_init_external(&blk->qiov, &blk->iov, 1); if (block_mig_state.submitted == 0) { block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock); } blk->aiocb = bdrv_aio_readv(VAR_2->bs, sector, &blk->qiov,
VAR_4, blk_mig_read_cb, blk); if (!blk->aiocb) { goto error; } block_mig_state.submitted++; bmds_set_aio_inflight(VAR_2, sector, VAR_4, 1); } else { VAR_5 = bdrv_read(VAR_2->bs, sector, blk->buf, VAR_4); if (VAR_5 < 0) { goto error; } blk_send(VAR_1, blk); g_free(blk->buf); g_free(blk); } bdrv_reset_dirty(VAR_2->bs, sector, VAR_4); break; } sector += BDRV_SECTORS_PER_DIRTY_CHUNK; VAR_2->cur_dirty = sector; } return (VAR_2->cur_dirty >= VAR_2->total_sectors); error:
monitor_printf(VAR_0, "Error reading sector %" PRId64 "\n", sector); qemu_file_set_error(VAR_1, VAR_5); g_free(blk->buf); g_free(blk); return 0; }