void qmp_drive_mirror(const char *device, const char *target,

                      bool has_format, const char *format,

                      enum MirrorSyncMode sync,

                      bool has_mode, enum NewImageMode mode,

                      bool has_speed, int64_t speed,

                      bool has_granularity, uint32_t granularity,

                      bool has_buf_size, int64_t buf_size,

                      bool has_on_source_error, BlockdevOnError on_source_error,

                      bool has_on_target_error, BlockdevOnError on_target_error,

                      Error **errp)

{

    BlockDriverState *bs;

    BlockDriverState *source, *target_bs;

    BlockDriver *proto_drv;

    BlockDriver *drv = NULL;

    Error *local_err = NULL;

    int flags;

    uint64_t size;

    int ret;



    if (!has_speed) {

        speed = 0;

    }

    if (!has_on_source_error) {

        on_source_error = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!has_on_target_error) {

        on_target_error = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!has_mode) {

        mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;

    }

    if (!has_granularity) {

        granularity = 0;

    }

    if (!has_buf_size) {

        buf_size = DEFAULT_MIRROR_BUF_SIZE;

    }



    if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {

        error_set(errp, QERR_INVALID_PARAMETER, device);

        return;

    }

    if (granularity & (granularity - 1)) {

        error_set(errp, QERR_INVALID_PARAMETER, device);

        return;

    }



    bs = bdrv_find(device);

    if (!bs) {

        error_set(errp, QERR_DEVICE_NOT_FOUND, device);

        return;

    }



    if (!bdrv_is_inserted(bs)) {

        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);

        return;

    }



    if (!has_format) {

        format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;

    }

    if (format) {

        drv = bdrv_find_format(format);

        if (!drv) {

            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);

            return;

        }

    }



    if (bdrv_in_use(bs)) {

        error_set(errp, QERR_DEVICE_IN_USE, device);

        return;

    }



    flags = bs->open_flags | BDRV_O_RDWR;

    source = bs->backing_hd;

    if (!source && sync == MIRROR_SYNC_MODE_TOP) {

        sync = MIRROR_SYNC_MODE_FULL;

    }



    proto_drv = bdrv_find_protocol(target);

    if (!proto_drv) {

        error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);

        return;

    }



    bdrv_get_geometry(bs, &size);

    size *= 512;

    if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {

        /* create new image w/o backing file */

        assert(format && drv);

        bdrv_img_create(target, format,

                        NULL, NULL, NULL, size, flags, &local_err, false);

    } else {

        switch (mode) {

        case NEW_IMAGE_MODE_EXISTING:

            ret = 0;

            break;

        case NEW_IMAGE_MODE_ABSOLUTE_PATHS:

            /* create new image with backing file */

            bdrv_img_create(target, format,

                            source->filename,

                            source->drv->format_name,

                            NULL, size, flags, &local_err, false);

            break;

        default:

            abort();

        }

    }



    if (error_is_set(&local_err)) {

        error_propagate(errp, local_err);

        return;

    }



    /* Mirroring takes care of copy-on-write using the source's backing

     * file.

     */

    target_bs = bdrv_new("");

    ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv);



    if (ret < 0) {

        bdrv_delete(target_bs);

        error_setg_file_open(errp, -ret, target);

        return;

    }



    mirror_start(bs, target_bs, speed, granularity, buf_size, sync,

                 on_source_error, on_target_error,

                 block_job_cb, bs, &local_err);

    if (local_err != NULL) {

        bdrv_delete(target_bs);

        error_propagate(errp, local_err);

        return;

    }



    /* Grab a reference so hotplug does not delete the BlockDriverState from

     * underneath us.

     */

    drive_get_ref(drive_get_by_blockdev(bs));

}

void qmp_drive_mirror(const char *device, const char *target,

                      bool has_format, const char *format,

                      enum MirrorSyncMode sync,

                      bool has_mode, enum NewImageMode mode,

                      bool has_speed, int64_t speed,

                      bool has_granularity, uint32_t granularity,

                      bool has_buf_size, int64_t buf_size,

                      bool has_on_source_error, BlockdevOnError on_source_error,

                      bool has_on_target_error, BlockdevOnError on_target_error,

                      Error **errp)

{

    BlockDriverState *bs;

    BlockDriverState *source, *target_bs;

    BlockDriver *proto_drv;

    BlockDriver *drv = NULL;

    Error *local_err = NULL;

    int flags;

    uint64_t size;

    int ret;



    if (!has_speed) {

        speed = 0;

    }

    if (!has_on_source_error) {

        on_source_error = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!has_on_target_error) {

        on_target_error = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!has_mode) {

        mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;

    }

    if (!has_granularity) {

        granularity = 0;

    }

    if (!has_buf_size) {

        buf_size = DEFAULT_MIRROR_BUF_SIZE;

    }



    if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {

        error_set(errp, QERR_INVALID_PARAMETER, device);

        return;

    }

    if (granularity & (granularity - 1)) {

        error_set(errp, QERR_INVALID_PARAMETER, device);

        return;

    }



    bs = bdrv_find(device);

    if (!bs) {

        error_set(errp, QERR_DEVICE_NOT_FOUND, device);

        return;

    }



    if (!bdrv_is_inserted(bs)) {

        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);

        return;

    }



    if (!has_format) {

        format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;

    }

    if (format) {

        drv = bdrv_find_format(format);

        if (!drv) {

            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);

            return;

        }

    }



    if (bdrv_in_use(bs)) {

        error_set(errp, QERR_DEVICE_IN_USE, device);

        return;

    }



    flags = bs->open_flags | BDRV_O_RDWR;

    source = bs->backing_hd;

    if (!source && sync == MIRROR_SYNC_MODE_TOP) {

        sync = MIRROR_SYNC_MODE_FULL;

    }



    proto_drv = bdrv_find_protocol(target);

    if (!proto_drv) {

        error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);

        return;

    }



    bdrv_get_geometry(bs, &size);

    size *= 512;

    if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {

        

        assert(format && drv);

        bdrv_img_create(target, format,

                        NULL, NULL, NULL, size, flags, &local_err, false);

    } else {

        switch (mode) {

        case NEW_IMAGE_MODE_EXISTING:

            ret = 0;

            break;

        case NEW_IMAGE_MODE_ABSOLUTE_PATHS:

            

            bdrv_img_create(target, format,

                            source->filename,

                            source->drv->format_name,

                            NULL, size, flags, &local_err, false);

            break;

        default:

            abort();

        }

    }



    if (error_is_set(&local_err)) {

        error_propagate(errp, local_err);

        return;

    }



    





    target_bs = bdrv_new("");

    ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv);



    if (ret < 0) {

        bdrv_delete(target_bs);

        error_setg_file_open(errp, -ret, target);

        return;

    }



    mirror_start(bs, target_bs, speed, granularity, buf_size, sync,

                 on_source_error, on_target_error,

                 block_job_cb, bs, &local_err);

    if (local_err != NULL) {

        bdrv_delete(target_bs);

        error_propagate(errp, local_err);

        return;

    }



    





    drive_get_ref(drive_get_by_blockdev(bs));

}

void FUNC_0(const char *VAR_0, const char *VAR_1,

                      bool VAR_2, const char *VAR_3,

                      enum MirrorSyncMode VAR_4,

                      bool VAR_5, enum NewImageMode VAR_6,

                      bool VAR_7, int64_t VAR_8,

                      bool VAR_9, uint32_t VAR_10,

                      bool VAR_11, int64_t VAR_12,

                      bool VAR_13, BlockdevOnError VAR_14,

                      bool VAR_15, BlockdevOnError VAR_16,

                      Error **VAR_17)

{

    BlockDriverState *bs;

    BlockDriverState *source, *target_bs;

    BlockDriver *proto_drv;

    BlockDriver *drv = NULL;

    Error *local_err = NULL;

    int VAR_18;

    uint64_t size;

    int VAR_19;



    if (!VAR_7) {

        VAR_8 = 0;

    }

    if (!VAR_13) {

        VAR_14 = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!VAR_15) {

        VAR_16 = BLOCKDEV_ON_ERROR_REPORT;

    }

    if (!VAR_5) {

        VAR_6 = NEW_IMAGE_MODE_ABSOLUTE_PATHS;

    }

    if (!VAR_9) {

        VAR_10 = 0;

    }

    if (!VAR_11) {

        VAR_12 = DEFAULT_MIRROR_BUF_SIZE;

    }



    if (VAR_10 != 0 && (VAR_10 < 512 || VAR_10 > 1048576 * 64)) {

        error_set(VAR_17, QERR_INVALID_PARAMETER, VAR_0);

        return;

    }

    if (VAR_10 & (VAR_10 - 1)) {

        error_set(VAR_17, QERR_INVALID_PARAMETER, VAR_0);

        return;

    }



    bs = bdrv_find(VAR_0);

    if (!bs) {

        error_set(VAR_17, QERR_DEVICE_NOT_FOUND, VAR_0);

        return;

    }



    if (!bdrv_is_inserted(bs)) {

        error_set(VAR_17, QERR_DEVICE_HAS_NO_MEDIUM, VAR_0);

        return;

    }



    if (!VAR_2) {

        VAR_3 = VAR_6 == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;

    }

    if (VAR_3) {

        drv = bdrv_find_format(VAR_3);

        if (!drv) {

            error_set(VAR_17, QERR_INVALID_BLOCK_FORMAT, VAR_3);

            return;

        }

    }



    if (bdrv_in_use(bs)) {

        error_set(VAR_17, QERR_DEVICE_IN_USE, VAR_0);

        return;

    }



    VAR_18 = bs->open_flags | BDRV_O_RDWR;

    source = bs->backing_hd;

    if (!source && VAR_4 == MIRROR_SYNC_MODE_TOP) {

        VAR_4 = MIRROR_SYNC_MODE_FULL;

    }



    proto_drv = bdrv_find_protocol(VAR_1);

    if (!proto_drv) {

        error_set(VAR_17, QERR_INVALID_BLOCK_FORMAT, VAR_3);

        return;

    }



    bdrv_get_geometry(bs, &size);

    size *= 512;

    if (VAR_4 == MIRROR_SYNC_MODE_FULL && VAR_6 != NEW_IMAGE_MODE_EXISTING) {

        

        assert(VAR_3 && drv);

        bdrv_img_create(VAR_1, VAR_3,

                        NULL, NULL, NULL, size, VAR_18, &local_err, false);

    } else {

        switch (VAR_6) {

        case NEW_IMAGE_MODE_EXISTING:

            VAR_19 = 0;

            break;

        case NEW_IMAGE_MODE_ABSOLUTE_PATHS:

            

            bdrv_img_create(VAR_1, VAR_3,

                            source->filename,

                            source->drv->format_name,

                            NULL, size, VAR_18, &local_err, false);

            break;

        default:

            abort();

        }

    }



    if (error_is_set(&local_err)) {

        error_propagate(VAR_17, local_err);

        return;

    }



    





    target_bs = bdrv_new("");

    VAR_19 = bdrv_open(target_bs, VAR_1, NULL, VAR_18 | BDRV_O_NO_BACKING, drv);



    if (VAR_19 < 0) {

        bdrv_delete(target_bs);

        error_setg_file_open(VAR_17, -VAR_19, VAR_1);

        return;

    }



    mirror_start(bs, target_bs, VAR_8, VAR_10, VAR_12, VAR_4,

                 VAR_14, VAR_16,

                 block_job_cb, bs, &local_err);

    if (local_err != NULL) {

        bdrv_delete(target_bs);

        error_propagate(VAR_17, local_err);

        return;

    }



    





    drive_get_ref(drive_get_by_blockdev(bs));

}

void FUNC_0(const char *VAR_0, const char *VAR_1,
bool VAR_2, const char *VAR_3,
enum MirrorSyncMode VAR_4,
bool VAR_5, enum NewImageMode VAR_6,
bool VAR_7, int64_t VAR_8,
bool VAR_9, uint32_t VAR_10,
bool VAR_11, int64_t VAR_12,
bool VAR_13, BlockdevOnError VAR_14,
bool VAR_15, BlockdevOnError VAR_16,
Error **VAR_17)
{ BlockDriverState *bs; BlockDriverState *source, *target_bs; BlockDriver *proto_drv; BlockDriver *drv = NULL; Error *local_err = NULL; int VAR_18; uint64_t size; int VAR_19; if (!VAR_7) { VAR_8 = 0; } if (!VAR_13) { VAR_14 = BLOCKDEV_ON_ERROR_REPORT; } if (!VAR_15) { VAR_16 = BLOCKDEV_ON_ERROR_REPORT; } if (!VAR_5) { VAR_6 = NEW_IMAGE_MODE_ABSOLUTE_PATHS; } if (!VAR_9) { VAR_10 = 0; } if (!VAR_11) { VAR_12 = DEFAULT_MIRROR_BUF_SIZE; } if (VAR_10 != 0 && (VAR_10 < 512 || VAR_10 > 1048576 * 64)) { error_set(VAR_17, QERR_INVALID_PARAMETER, VAR_0); return; } if (VAR_10 & (VAR_10 - 1)) { error_set(VAR_17, QERR_INVALID_PARAMETER, VAR_0); return; } bs = bdrv_find(VAR_0); if (!bs) { error_set(VAR_17, QERR_DEVICE_NOT_FOUND, VAR_0); return; } if (!bdrv_is_inserted(bs)) { error_set(VAR_17, QERR_DEVICE_HAS_NO_MEDIUM, VAR_0); return; } if (!VAR_2) { VAR_3 = VAR_6 == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; } if (VAR_3) { drv = bdrv_find_format(VAR_3); if (!drv) { error_set(VAR_17, QERR_INVALID_BLOCK_FORMAT, VAR_3); return; } } if (bdrv_in_use(bs)) { error_set(VAR_17, QERR_DEVICE_IN_USE, VAR_0); return; } VAR_18 = bs->open_flags | BDRV_O_RDWR; source = bs->backing_hd; if (!source && VAR_4 == MIRROR_SYNC_MODE_TOP) { VAR_4 = MIRROR_SYNC_MODE_FULL; } proto_drv = bdrv_find_protocol(VAR_1); if (!proto_drv) { error_set(VAR_17, QERR_INVALID_BLOCK_FORMAT, VAR_3); return; } bdrv_get_geometry(bs, &size); size *= 512; if (VAR_4 == MIRROR_SYNC_MODE_FULL && VAR_6 != NEW_IMAGE_MODE_EXISTING) { assert(VAR_3 && drv); bdrv_img_create(VAR_1, VAR_3,
NULL, NULL, NULL, size, VAR_18, &local_err, false); } else { switch (VAR_6) { case NEW_IMAGE_MODE_EXISTING:
VAR_19 = 0; break; case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
bdrv_img_create(VAR_1, VAR_3,
source->filename,
source->drv->format_name,
NULL, size, VAR_18, &local_err, false); break; default:
abort(); } } if (error_is_set(&local_err)) { error_propagate(VAR_17, local_err); return; } target_bs = bdrv_new(""); VAR_19 = bdrv_open(target_bs, VAR_1, NULL, VAR_18 | BDRV_O_NO_BACKING, drv); if (VAR_19 < 0) { bdrv_delete(target_bs); error_setg_file_open(VAR_17, -VAR_19, VAR_1); return; } mirror_start(bs, target_bs, VAR_8, VAR_10, VAR_12, VAR_4,
VAR_14, VAR_16,
block_job_cb, bs, &local_err); if (local_err != NULL) { bdrv_delete(target_bs); error_propagate(VAR_17, local_err); return; } drive_get_ref(drive_get_by_blockdev(bs)); }