static void do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, Error **errp)

{

    BlockDriverState *bs;

    BlockDriverState *target_bs;

    BlockDriverState *source = NULL;

    BdrvDirtyBitmap *bmap = NULL;

    AioContext *aio_context;

    QDict *options = NULL;

    Error *local_err = NULL;

    int flags;

    int64_t size;



    if (!backup->has_speed) {

        backup->speed = 0;

    }

    if (!backup->has_on_source_error) {

        backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!backup->has_on_target_error) {

        backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!backup->has_mode) {

        backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;

    }

    if (!backup->has_job_id) {

        backup->job_id = NULL;

    }



    bs = qmp_get_root_bs(backup->device, errp);

    if (!bs) {

        return;

    }



    aio_context = bdrv_get_aio_context(bs);

    aio_context_acquire(aio_context);



    if (!backup->has_format) {

        backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?

                         NULL : (char*) bs->drv->format_name;

    }



    /* Early check to avoid creating target */

    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {

        goto out;

    }



    flags = bs->open_flags | BDRV_O_RDWR;



    /* See if we have a backing HD we can use to create our new image

     * on top of. */

    if (backup->sync == MIRROR_SYNC_MODE_TOP) {

        source = backing_bs(bs);

        if (!source) {

            backup->sync = MIRROR_SYNC_MODE_FULL;

        }

    }

    if (backup->sync == MIRROR_SYNC_MODE_NONE) {

        source = bs;

    }



    size = bdrv_getlength(bs);

    if (size < 0) {

        error_setg_errno(errp, -size, "bdrv_getlength failed");

        goto out;

    }



    if (backup->mode != NEW_IMAGE_MODE_EXISTING) {

        assert(backup->format);

        if (source) {

            bdrv_img_create(backup->target, backup->format, source->filename,

                            source->drv->format_name, NULL,

                            size, flags, &local_err, false);

        } else {

            bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,

                            size, flags, &local_err, false);

        }

    }



    if (local_err) {

        error_propagate(errp, local_err);

        goto out;

    }



    if (backup->format) {

        options = qdict_new();

        qdict_put(options, "driver", qstring_from_str(backup->format));

    }



    target_bs = bdrv_open(backup->target, NULL, options, flags, errp);

    if (!target_bs) {

        goto out;

    }



    bdrv_set_aio_context(target_bs, aio_context);



    if (backup->has_bitmap) {

        bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);

        if (!bmap) {

            error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);

            bdrv_unref(target_bs);

            goto out;

        }

    }



    backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync,

                 bmap, backup->on_source_error, backup->on_target_error,

                 block_job_cb, bs, txn, &local_err);

    bdrv_unref(target_bs);

    if (local_err != NULL) {

        error_propagate(errp, local_err);

        goto out;

    }



out:

    aio_context_release(aio_context);

}

static void do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, Error **errp)

{

    BlockDriverState *bs;

    BlockDriverState *target_bs;

    BlockDriverState *source = NULL;

    BdrvDirtyBitmap *bmap = NULL;

    AioContext *aio_context;

    QDict *options = NULL;

    Error *local_err = NULL;

    int flags;

    int64_t size;



    if (!backup->has_speed) {

        backup->speed = 0;

    }

    if (!backup->has_on_source_error) {

        backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!backup->has_on_target_error) {

        backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!backup->has_mode) {

        backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;

    }

    if (!backup->has_job_id) {

        backup->job_id = NULL;

    }



    bs = qmp_get_root_bs(backup->device, errp);

    if (!bs) {

        return;

    }



    aio_context = bdrv_get_aio_context(bs);

    aio_context_acquire(aio_context);



    if (!backup->has_format) {

        backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?

                         NULL : (char*) bs->drv->format_name;

    }



    

    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {

        goto out;

    }



    flags = bs->open_flags | BDRV_O_RDWR;



    



    if (backup->sync == MIRROR_SYNC_MODE_TOP) {

        source = backing_bs(bs);

        if (!source) {

            backup->sync = MIRROR_SYNC_MODE_FULL;

        }

    }

    if (backup->sync == MIRROR_SYNC_MODE_NONE) {

        source = bs;

    }



    size = bdrv_getlength(bs);

    if (size < 0) {

        error_setg_errno(errp, -size, "bdrv_getlength failed");

        goto out;

    }



    if (backup->mode != NEW_IMAGE_MODE_EXISTING) {

        assert(backup->format);

        if (source) {

            bdrv_img_create(backup->target, backup->format, source->filename,

                            source->drv->format_name, NULL,

                            size, flags, &local_err, false);

        } else {

            bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,

                            size, flags, &local_err, false);

        }

    }



    if (local_err) {

        error_propagate(errp, local_err);

        goto out;

    }



    if (backup->format) {

        options = qdict_new();

        qdict_put(options, "driver", qstring_from_str(backup->format));

    }



    target_bs = bdrv_open(backup->target, NULL, options, flags, errp);

    if (!target_bs) {

        goto out;

    }



    bdrv_set_aio_context(target_bs, aio_context);



    if (backup->has_bitmap) {

        bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);

        if (!bmap) {

            error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);

            bdrv_unref(target_bs);

            goto out;

        }

    }



    backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync,

                 bmap, backup->on_source_error, backup->on_target_error,

                 block_job_cb, bs, txn, &local_err);

    bdrv_unref(target_bs);

    if (local_err != NULL) {

        error_propagate(errp, local_err);

        goto out;

    }



out:

    aio_context_release(aio_context);

}

static void FUNC_0(DriveBackup *VAR_0, BlockJobTxn *VAR_1, Error **VAR_2)

{

    BlockDriverState *bs;

    BlockDriverState *target_bs;

    BlockDriverState *source = NULL;

    BdrvDirtyBitmap *bmap = NULL;

    AioContext *aio_context;

    QDict *options = NULL;

    Error *local_err = NULL;

    int VAR_3;

    int64_t size;



    if (!VAR_0->has_speed) {

        VAR_0->speed = 0;

    }

    if (!VAR_0->has_on_source_error) {

        VAR_0->on_source_error = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!VAR_0->has_on_target_error) {

        VAR_0->on_target_error = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!VAR_0->has_mode) {

        VAR_0->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;

    }

    if (!VAR_0->has_job_id) {

        VAR_0->job_id = NULL;

    }



    bs = qmp_get_root_bs(VAR_0->device, VAR_2);

    if (!bs) {

        return;

    }



    aio_context = bdrv_get_aio_context(bs);

    aio_context_acquire(aio_context);



    if (!VAR_0->has_format) {

        VAR_0->format = VAR_0->mode == NEW_IMAGE_MODE_EXISTING ?

                         NULL : (char*) bs->drv->format_name;

    }



    

    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, VAR_2)) {

        goto out;

    }



    VAR_3 = bs->open_flags | BDRV_O_RDWR;



    



    if (VAR_0->sync == MIRROR_SYNC_MODE_TOP) {

        source = backing_bs(bs);

        if (!source) {

            VAR_0->sync = MIRROR_SYNC_MODE_FULL;

        }

    }

    if (VAR_0->sync == MIRROR_SYNC_MODE_NONE) {

        source = bs;

    }



    size = bdrv_getlength(bs);

    if (size < 0) {

        error_setg_errno(VAR_2, -size, "bdrv_getlength failed");

        goto out;

    }



    if (VAR_0->mode != NEW_IMAGE_MODE_EXISTING) {

        assert(VAR_0->format);

        if (source) {

            bdrv_img_create(VAR_0->target, VAR_0->format, source->filename,

                            source->drv->format_name, NULL,

                            size, VAR_3, &local_err, false);

        } else {

            bdrv_img_create(VAR_0->target, VAR_0->format, NULL, NULL, NULL,

                            size, VAR_3, &local_err, false);

        }

    }



    if (local_err) {

        error_propagate(VAR_2, local_err);

        goto out;

    }



    if (VAR_0->format) {

        options = qdict_new();

        qdict_put(options, "driver", qstring_from_str(VAR_0->format));

    }



    target_bs = bdrv_open(VAR_0->target, NULL, options, VAR_3, VAR_2);

    if (!target_bs) {

        goto out;

    }



    bdrv_set_aio_context(target_bs, aio_context);



    if (VAR_0->has_bitmap) {

        bmap = bdrv_find_dirty_bitmap(bs, VAR_0->bitmap);

        if (!bmap) {

            error_setg(VAR_2, "Bitmap '%s' could not be found", VAR_0->bitmap);

            bdrv_unref(target_bs);

            goto out;

        }

    }



    backup_start(VAR_0->job_id, bs, target_bs, VAR_0->speed, VAR_0->sync,

                 bmap, VAR_0->on_source_error, VAR_0->on_target_error,

                 block_job_cb, bs, VAR_1, &local_err);

    bdrv_unref(target_bs);

    if (local_err != NULL) {

        error_propagate(VAR_2, local_err);

        goto out;

    }



out:

    aio_context_release(aio_context);

}

static void FUNC_0(DriveBackup *VAR_0, BlockJobTxn *VAR_1, Error **VAR_2)
{ BlockDriverState *bs; BlockDriverState *target_bs; BlockDriverState *source = NULL; BdrvDirtyBitmap *bmap = NULL; AioContext *aio_context; QDict *options = NULL; Error *local_err = NULL; int VAR_3; int64_t size; if (!VAR_0->has_speed) { VAR_0->speed = 0; } if (!VAR_0->has_on_source_error) { VAR_0->on_source_error = BLOCKDEV_ON_ERROR_REPORT; } if (!VAR_0->has_on_target_error) { VAR_0->on_target_error = BLOCKDEV_ON_ERROR_REPORT; } if (!VAR_0->has_mode) { VAR_0->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; } if (!VAR_0->has_job_id) { VAR_0->job_id = NULL; } bs = qmp_get_root_bs(VAR_0->device, VAR_2); if (!bs) { return; } aio_context = bdrv_get_aio_context(bs); aio_context_acquire(aio_context); if (!VAR_0->has_format) { VAR_0->format = VAR_0->mode == NEW_IMAGE_MODE_EXISTING ?
NULL : (char*) bs->drv->format_name; } if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, VAR_2)) { goto out; } VAR_3 = bs->open_flags | BDRV_O_RDWR; if (VAR_0->sync == MIRROR_SYNC_MODE_TOP) { source = backing_bs(bs); if (!source) { VAR_0->sync = MIRROR_SYNC_MODE_FULL; } } if (VAR_0->sync == MIRROR_SYNC_MODE_NONE) { source = bs; } size = bdrv_getlength(bs); if (size < 0) { error_setg_errno(VAR_2, -size, "bdrv_getlength failed"); goto out; } if (VAR_0->mode != NEW_IMAGE_MODE_EXISTING) { assert(VAR_0->format); if (source) { bdrv_img_create(VAR_0->target, VAR_0->format, source->filename,
source->drv->format_name, NULL,
size, VAR_3, &local_err, false); } else { bdrv_img_create(VAR_0->target, VAR_0->format, NULL, NULL, NULL,
size, VAR_3, &local_err, false); } } if (local_err) { error_propagate(VAR_2, local_err); goto out; } if (VAR_0->format) { options = qdict_new(); qdict_put(options, "driver", qstring_from_str(VAR_0->format)); } target_bs = bdrv_open(VAR_0->target, NULL, options, VAR_3, VAR_2); if (!target_bs) { goto out; } bdrv_set_aio_context(target_bs, aio_context); if (VAR_0->has_bitmap) { bmap = bdrv_find_dirty_bitmap(bs, VAR_0->bitmap); if (!bmap) { error_setg(VAR_2, "Bitmap '%s' could not be found", VAR_0->bitmap); bdrv_unref(target_bs); goto out; } } backup_start(VAR_0->job_id, bs, target_bs, VAR_0->speed, VAR_0->sync,
bmap, VAR_0->on_source_error, VAR_0->on_target_error,
block_job_cb, bs, VAR_1, &local_err); bdrv_unref(target_bs); if (local_err != NULL) { error_propagate(VAR_2, local_err); goto out; } out:
aio_context_release(aio_context); }