static void migration_completion(MigrationState *s, int current_active_state,

                                 bool *old_vm_running,

                                 int64_t *start_time)

{

    int ret;



    if (s->state == MIGRATION_STATUS_ACTIVE) {

        qemu_mutex_lock_iothread();

        *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);

        qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);

        *old_vm_running = runstate_is_running();

        ret = global_state_store();



        if (!ret) {

            ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);

            if (ret >= 0) {

                ret = bdrv_inactivate_all();

            }

            if (ret >= 0) {

                qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);

                qemu_savevm_state_complete_precopy(s->to_dst_file, false);

            }

        }

        qemu_mutex_unlock_iothread();



        if (ret < 0) {

            goto fail;

        }

    } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {

        trace_migration_completion_postcopy_end();



        qemu_savevm_state_complete_postcopy(s->to_dst_file);

        trace_migration_completion_postcopy_end_after_complete();

    }



    /*

     * If rp was opened we must clean up the thread before

     * cleaning everything else up (since if there are no failures

     * it will wait for the destination to send it's status in

     * a SHUT command).

     * Postcopy opens rp if enabled (even if it's not avtivated)

     */

    if (migrate_postcopy_ram()) {

        int rp_error;

        trace_migration_completion_postcopy_end_before_rp();

        rp_error = await_return_path_close_on_source(s);

        trace_migration_completion_postcopy_end_after_rp(rp_error);

        if (rp_error) {

            goto fail_invalidate;

        }

    }



    if (qemu_file_get_error(s->to_dst_file)) {

        trace_migration_completion_file_err();

        goto fail_invalidate;

    }



    migrate_set_state(&s->state, current_active_state,

                      MIGRATION_STATUS_COMPLETED);

    return;



fail_invalidate:

    /* If not doing postcopy, vm_start() will be called: let's regain

     * control on images.

     */

    if (s->state == MIGRATION_STATUS_ACTIVE) {

        Error *local_err = NULL;



        bdrv_invalidate_cache_all(&local_err);

        if (local_err) {

            error_report_err(local_err);

        }

    }



fail:

    migrate_set_state(&s->state, current_active_state,

                      MIGRATION_STATUS_FAILED);

}

static void migration_completion(MigrationState *s, int current_active_state,

                                 bool *old_vm_running,

                                 int64_t *start_time)

{

    int ret;



    if (s->state == MIGRATION_STATUS_ACTIVE) {

        qemu_mutex_lock_iothread();

        *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);

        qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);

        *old_vm_running = runstate_is_running();

        ret = global_state_store();



        if (!ret) {

            ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);

            if (ret >= 0) {

                ret = bdrv_inactivate_all();

            }

            if (ret >= 0) {

                qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);

                qemu_savevm_state_complete_precopy(s->to_dst_file, false);

            }

        }

        qemu_mutex_unlock_iothread();



        if (ret < 0) {

            goto fail;

        }

    } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {

        trace_migration_completion_postcopy_end();



        qemu_savevm_state_complete_postcopy(s->to_dst_file);

        trace_migration_completion_postcopy_end_after_complete();

    }



    













    if (migrate_postcopy_ram()) {

        int rp_error;

        trace_migration_completion_postcopy_end_before_rp();

        rp_error = await_return_path_close_on_source(s);

        trace_migration_completion_postcopy_end_after_rp(rp_error);

        if (rp_error) {

            goto fail_invalidate;

        }

    }



    if (qemu_file_get_error(s->to_dst_file)) {

        trace_migration_completion_file_err();

        goto fail_invalidate;

    }



    migrate_set_state(&s->state, current_active_state,

                      MIGRATION_STATUS_COMPLETED);

    return;



fail_invalidate:

    





    if (s->state == MIGRATION_STATUS_ACTIVE) {

        Error *local_err = NULL;



        bdrv_invalidate_cache_all(&local_err);

        if (local_err) {

            error_report_err(local_err);

        }

    }



fail:

    migrate_set_state(&s->state, current_active_state,

                      MIGRATION_STATUS_FAILED);

}

static void FUNC_0(MigrationState *VAR_0, int VAR_1,

                                 bool *VAR_2,

                                 int64_t *VAR_3)

{

    int VAR_4;



    if (VAR_0->state == MIGRATION_STATUS_ACTIVE) {

        qemu_mutex_lock_iothread();

        *VAR_3 = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);

        qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);

        *VAR_2 = runstate_is_running();

        VAR_4 = global_state_store();



        if (!VAR_4) {

            VAR_4 = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);

            if (VAR_4 >= 0) {

                VAR_4 = bdrv_inactivate_all();

            }

            if (VAR_4 >= 0) {

                qemu_file_set_rate_limit(VAR_0->to_dst_file, INT64_MAX);

                qemu_savevm_state_complete_precopy(VAR_0->to_dst_file, false);

            }

        }

        qemu_mutex_unlock_iothread();



        if (VAR_4 < 0) {

            goto fail;

        }

    } else if (VAR_0->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {

        trace_migration_completion_postcopy_end();



        qemu_savevm_state_complete_postcopy(VAR_0->to_dst_file);

        trace_migration_completion_postcopy_end_after_complete();

    }



    













    if (migrate_postcopy_ram()) {

        int VAR_5;

        trace_migration_completion_postcopy_end_before_rp();

        VAR_5 = await_return_path_close_on_source(VAR_0);

        trace_migration_completion_postcopy_end_after_rp(VAR_5);

        if (VAR_5) {

            goto fail_invalidate;

        }

    }



    if (qemu_file_get_error(VAR_0->to_dst_file)) {

        trace_migration_completion_file_err();

        goto fail_invalidate;

    }



    migrate_set_state(&VAR_0->state, VAR_1,

                      MIGRATION_STATUS_COMPLETED);

    return;



fail_invalidate:

    





    if (VAR_0->state == MIGRATION_STATUS_ACTIVE) {

        Error *local_err = NULL;



        bdrv_invalidate_cache_all(&local_err);

        if (local_err) {

            error_report_err(local_err);

        }

    }



fail:

    migrate_set_state(&VAR_0->state, VAR_1,

                      MIGRATION_STATUS_FAILED);

}

static void FUNC_0(MigrationState *VAR_0, int VAR_1,
bool *VAR_2,
int64_t *VAR_3)
{ int VAR_4; if (VAR_0->state == MIGRATION_STATUS_ACTIVE) { qemu_mutex_lock_iothread(); *VAR_3 = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); *VAR_2 = runstate_is_running(); VAR_4 = global_state_store(); if (!VAR_4) { VAR_4 = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); if (VAR_4 >= 0) { VAR_4 = bdrv_inactivate_all(); } if (VAR_4 >= 0) { qemu_file_set_rate_limit(VAR_0->to_dst_file, INT64_MAX); qemu_savevm_state_complete_precopy(VAR_0->to_dst_file, false); } } qemu_mutex_unlock_iothread(); if (VAR_4 < 0) { goto fail; } } else if (VAR_0->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { trace_migration_completion_postcopy_end(); qemu_savevm_state_complete_postcopy(VAR_0->to_dst_file); trace_migration_completion_postcopy_end_after_complete(); } if (migrate_postcopy_ram()) { int VAR_5; trace_migration_completion_postcopy_end_before_rp(); VAR_5 = await_return_path_close_on_source(VAR_0); trace_migration_completion_postcopy_end_after_rp(VAR_5); if (VAR_5) { goto fail_invalidate; } } if (qemu_file_get_error(VAR_0->to_dst_file)) { trace_migration_completion_file_err(); goto fail_invalidate; } migrate_set_state(&VAR_0->state, VAR_1,
MIGRATION_STATUS_COMPLETED); return; fail_invalidate:
if (VAR_0->state == MIGRATION_STATUS_ACTIVE) { Error *local_err = NULL; bdrv_invalidate_cache_all(&local_err); if (local_err) { error_report_err(local_err); } } fail:
migrate_set_state(&VAR_0->state, VAR_1,
MIGRATION_STATUS_FAILED); }