static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,

                            QDict *options, Error **errp)

{

    int ret, open_flags;

    const char *filename;

    const char *driver_name = NULL;

    const char *node_name = NULL;

    QemuOpts *opts;

    BlockDriver *drv;

    Error *local_err = NULL;



    assert(bs->file == NULL);

    assert(options != NULL && bs->options != options);



    opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);

    qemu_opts_absorb_qdict(opts, options, &local_err);

    if (local_err) {

        error_propagate(errp, local_err);

        ret = -EINVAL;

        goto fail_opts;

    }



    driver_name = qemu_opt_get(opts, "driver");

    drv = bdrv_find_format(driver_name);

    assert(drv != NULL);



    if (file != NULL) {

        filename = file->bs->filename;

    } else {

        filename = qdict_get_try_str(options, "filename");

    }



    if (drv->bdrv_needs_filename && !filename) {

        error_setg(errp, "The '%s' block driver requires a file name",

                   drv->format_name);

        ret = -EINVAL;

        goto fail_opts;

    }



    trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,

                           drv->format_name);



    node_name = qemu_opt_get(opts, "node-name");

    bdrv_assign_node_name(bs, node_name, &local_err);

    if (local_err) {

        error_propagate(errp, local_err);

        ret = -EINVAL;

        goto fail_opts;

    }



    bs->read_only = !(bs->open_flags & BDRV_O_RDWR);



    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {

        error_setg(errp,

                   !bs->read_only && bdrv_is_whitelisted(drv, true)

                        ? "Driver '%s' can only be used for read-only devices"

                        : "Driver '%s' is not whitelisted",

                   drv->format_name);

        ret = -ENOTSUP;

        goto fail_opts;

    }



    assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */

    if (bs->open_flags & BDRV_O_COPY_ON_READ) {

        if (!bs->read_only) {

            bdrv_enable_copy_on_read(bs);

        } else {

            error_setg(errp, "Can't use copy-on-read on read-only device");

            ret = -EINVAL;

            goto fail_opts;

        }

    }



    if (filename != NULL) {

        pstrcpy(bs->filename, sizeof(bs->filename), filename);

    } else {

        bs->filename[0] = '\0';

    }

    pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);



    bs->drv = drv;

    bs->opaque = g_malloc0(drv->instance_size);



    /* Apply cache mode options */

    update_flags_from_options(&bs->open_flags, opts);



    /* Open the image, either directly or using a protocol */

    open_flags = bdrv_open_flags(bs, bs->open_flags);

    if (drv->bdrv_file_open) {

        assert(file == NULL);

        assert(!drv->bdrv_needs_filename || filename != NULL);

        ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);

    } else {

        if (file == NULL) {

            error_setg(errp, "Can't use '%s' as a block driver for the "

                       "protocol level", drv->format_name);

            ret = -EINVAL;

            goto free_and_fail;

        }

        bs->file = file;

        ret = drv->bdrv_open(bs, options, open_flags, &local_err);

    }



    if (ret < 0) {

        if (local_err) {

            error_propagate(errp, local_err);

        } else if (bs->filename[0]) {

            error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);

        } else {

            error_setg_errno(errp, -ret, "Could not open image");

        }

        goto free_and_fail;

    }



    ret = refresh_total_sectors(bs, bs->total_sectors);

    if (ret < 0) {

        error_setg_errno(errp, -ret, "Could not refresh total sector count");

        goto free_and_fail;

    }



    bdrv_refresh_limits(bs, &local_err);

    if (local_err) {

        error_propagate(errp, local_err);

        ret = -EINVAL;

        goto free_and_fail;

    }



    assert(bdrv_opt_mem_align(bs) != 0);

    assert(bdrv_min_mem_align(bs) != 0);

    assert(is_power_of_2(bs->request_alignment) || bdrv_is_sg(bs));



    qemu_opts_del(opts);

    return 0;



free_and_fail:

    bs->file = NULL;

    g_free(bs->opaque);

    bs->opaque = NULL;

    bs->drv = NULL;

fail_opts:

    qemu_opts_del(opts);

    return ret;

}

static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,

                            QDict *options, Error **errp)

{

    int ret, open_flags;

    const char *filename;

    const char *driver_name = NULL;

    const char *node_name = NULL;

    QemuOpts *opts;

    BlockDriver *drv;

    Error *local_err = NULL;



    assert(bs->file == NULL);

    assert(options != NULL && bs->options != options);



    opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);

    qemu_opts_absorb_qdict(opts, options, &local_err);

    if (local_err) {

        error_propagate(errp, local_err);

        ret = -EINVAL;

        goto fail_opts;

    }



    driver_name = qemu_opt_get(opts, "driver");

    drv = bdrv_find_format(driver_name);

    assert(drv != NULL);



    if (file != NULL) {

        filename = file->bs->filename;

    } else {

        filename = qdict_get_try_str(options, "filename");

    }



    if (drv->bdrv_needs_filename && !filename) {

        error_setg(errp, "The '%s' block driver requires a file name",

                   drv->format_name);

        ret = -EINVAL;

        goto fail_opts;

    }



    trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,

                           drv->format_name);



    node_name = qemu_opt_get(opts, "node-name");

    bdrv_assign_node_name(bs, node_name, &local_err);

    if (local_err) {

        error_propagate(errp, local_err);

        ret = -EINVAL;

        goto fail_opts;

    }



    bs->read_only = !(bs->open_flags & BDRV_O_RDWR);



    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {

        error_setg(errp,

                   !bs->read_only && bdrv_is_whitelisted(drv, true)

                        ? "Driver '%s' can only be used for read-only devices"

                        : "Driver '%s' is not whitelisted",

                   drv->format_name);

        ret = -ENOTSUP;

        goto fail_opts;

    }



    assert(bs->copy_on_read == 0); 

    if (bs->open_flags & BDRV_O_COPY_ON_READ) {

        if (!bs->read_only) {

            bdrv_enable_copy_on_read(bs);

        } else {

            error_setg(errp, "Can't use copy-on-read on read-only device");

            ret = -EINVAL;

            goto fail_opts;

        }

    }



    if (filename != NULL) {

        pstrcpy(bs->filename, sizeof(bs->filename), filename);

    } else {

        bs->filename[0] = '\0';

    }

    pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);



    bs->drv = drv;

    bs->opaque = g_malloc0(drv->instance_size);



    

    update_flags_from_options(&bs->open_flags, opts);



    

    open_flags = bdrv_open_flags(bs, bs->open_flags);

    if (drv->bdrv_file_open) {

        assert(file == NULL);

        assert(!drv->bdrv_needs_filename || filename != NULL);

        ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);

    } else {

        if (file == NULL) {

            error_setg(errp, "Can't use '%s' as a block driver for the "

                       "protocol level", drv->format_name);

            ret = -EINVAL;

            goto free_and_fail;

        }

        bs->file = file;

        ret = drv->bdrv_open(bs, options, open_flags, &local_err);

    }



    if (ret < 0) {

        if (local_err) {

            error_propagate(errp, local_err);

        } else if (bs->filename[0]) {

            error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);

        } else {

            error_setg_errno(errp, -ret, "Could not open image");

        }

        goto free_and_fail;

    }



    ret = refresh_total_sectors(bs, bs->total_sectors);

    if (ret < 0) {

        error_setg_errno(errp, -ret, "Could not refresh total sector count");

        goto free_and_fail;

    }



    bdrv_refresh_limits(bs, &local_err);

    if (local_err) {

        error_propagate(errp, local_err);

        ret = -EINVAL;

        goto free_and_fail;

    }



    assert(bdrv_opt_mem_align(bs) != 0);

    assert(bdrv_min_mem_align(bs) != 0);

    assert(is_power_of_2(bs->request_alignment) || bdrv_is_sg(bs));



    qemu_opts_del(opts);

    return 0;



free_and_fail:

    bs->file = NULL;

    g_free(bs->opaque);

    bs->opaque = NULL;

    bs->drv = NULL;

fail_opts:

    qemu_opts_del(opts);

    return ret;

}

static int FUNC_0(BlockDriverState *VAR_0, BdrvChild *VAR_1,

                            QDict *VAR_2, Error **VAR_3)

{

    int VAR_4, VAR_5;

    const char *VAR_6;

    const char *VAR_7 = NULL;

    const char *VAR_8 = NULL;

    QemuOpts *opts;

    BlockDriver *drv;

    Error *local_err = NULL;



    assert(VAR_0->VAR_1 == NULL);

    assert(VAR_2 != NULL && VAR_0->VAR_2 != VAR_2);



    opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);

    qemu_opts_absorb_qdict(opts, VAR_2, &local_err);

    if (local_err) {

        error_propagate(VAR_3, local_err);

        VAR_4 = -EINVAL;

        goto fail_opts;

    }



    VAR_7 = qemu_opt_get(opts, "driver");

    drv = bdrv_find_format(VAR_7);

    assert(drv != NULL);



    if (VAR_1 != NULL) {

        VAR_6 = VAR_1->VAR_0->VAR_6;

    } else {

        VAR_6 = qdict_get_try_str(VAR_2, "VAR_6");

    }



    if (drv->bdrv_needs_filename && !VAR_6) {

        error_setg(VAR_3, "The '%s' block driver requires a VAR_1 name",

                   drv->format_name);

        VAR_4 = -EINVAL;

        goto fail_opts;

    }



    trace_bdrv_open_common(VAR_0, VAR_6 ?: "", VAR_0->VAR_5,

                           drv->format_name);



    VAR_8 = qemu_opt_get(opts, "node-name");

    bdrv_assign_node_name(VAR_0, VAR_8, &local_err);

    if (local_err) {

        error_propagate(VAR_3, local_err);

        VAR_4 = -EINVAL;

        goto fail_opts;

    }



    VAR_0->read_only = !(VAR_0->VAR_5 & BDRV_O_RDWR);



    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, VAR_0->read_only)) {

        error_setg(VAR_3,

                   !VAR_0->read_only && bdrv_is_whitelisted(drv, true)

                        ? "Driver '%s' can only be used for read-only devices"

                        : "Driver '%s' is not whitelisted",

                   drv->format_name);

        VAR_4 = -ENOTSUP;

        goto fail_opts;

    }



    assert(VAR_0->copy_on_read == 0); 

    if (VAR_0->VAR_5 & BDRV_O_COPY_ON_READ) {

        if (!VAR_0->read_only) {

            bdrv_enable_copy_on_read(VAR_0);

        } else {

            error_setg(VAR_3, "Can't use copy-on-read on read-only device");

            VAR_4 = -EINVAL;

            goto fail_opts;

        }

    }



    if (VAR_6 != NULL) {

        pstrcpy(VAR_0->VAR_6, sizeof(VAR_0->VAR_6), VAR_6);

    } else {

        VAR_0->VAR_6[0] = '\0';

    }

    pstrcpy(VAR_0->exact_filename, sizeof(VAR_0->exact_filename), VAR_0->VAR_6);



    VAR_0->drv = drv;

    VAR_0->opaque = g_malloc0(drv->instance_size);



    

    update_flags_from_options(&VAR_0->VAR_5, opts);



    

    VAR_5 = bdrv_open_flags(VAR_0, VAR_0->VAR_5);

    if (drv->bdrv_file_open) {

        assert(VAR_1 == NULL);

        assert(!drv->bdrv_needs_filename || VAR_6 != NULL);

        VAR_4 = drv->bdrv_file_open(VAR_0, VAR_2, VAR_5, &local_err);

    } else {

        if (VAR_1 == NULL) {

            error_setg(VAR_3, "Can't use '%s' as a block driver for the "

                       "protocol level", drv->format_name);

            VAR_4 = -EINVAL;

            goto free_and_fail;

        }

        VAR_0->VAR_1 = VAR_1;

        VAR_4 = drv->bdrv_open(VAR_0, VAR_2, VAR_5, &local_err);

    }



    if (VAR_4 < 0) {

        if (local_err) {

            error_propagate(VAR_3, local_err);

        } else if (VAR_0->VAR_6[0]) {

            error_setg_errno(VAR_3, -VAR_4, "Could not open '%s'", VAR_0->VAR_6);

        } else {

            error_setg_errno(VAR_3, -VAR_4, "Could not open image");

        }

        goto free_and_fail;

    }



    VAR_4 = refresh_total_sectors(VAR_0, VAR_0->total_sectors);

    if (VAR_4 < 0) {

        error_setg_errno(VAR_3, -VAR_4, "Could not refresh total sector count");

        goto free_and_fail;

    }



    bdrv_refresh_limits(VAR_0, &local_err);

    if (local_err) {

        error_propagate(VAR_3, local_err);

        VAR_4 = -EINVAL;

        goto free_and_fail;

    }



    assert(bdrv_opt_mem_align(VAR_0) != 0);

    assert(bdrv_min_mem_align(VAR_0) != 0);

    assert(is_power_of_2(VAR_0->request_alignment) || bdrv_is_sg(VAR_0));



    qemu_opts_del(opts);

    return 0;



free_and_fail:

    VAR_0->VAR_1 = NULL;

    g_free(VAR_0->opaque);

    VAR_0->opaque = NULL;

    VAR_0->drv = NULL;

fail_opts:

    qemu_opts_del(opts);

    return VAR_4;

}

static int FUNC_0(BlockDriverState *VAR_0, BdrvChild *VAR_1,
QDict *VAR_2, Error **VAR_3)
{ int VAR_4, VAR_5; const char *VAR_6; const char *VAR_7 = NULL; const char *VAR_8 = NULL; QemuOpts *opts; BlockDriver *drv; Error *local_err = NULL; assert(VAR_0->VAR_1 == NULL); assert(VAR_2 != NULL && VAR_0->VAR_2 != VAR_2); opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); qemu_opts_absorb_qdict(opts, VAR_2, &local_err); if (local_err) { error_propagate(VAR_3, local_err); VAR_4 = -EINVAL; goto fail_opts; } VAR_7 = qemu_opt_get(opts, "driver"); drv = bdrv_find_format(VAR_7); assert(drv != NULL); if (VAR_1 != NULL) { VAR_6 = VAR_1->VAR_0->VAR_6; } else { VAR_6 = qdict_get_try_str(VAR_2, "VAR_6"); } if (drv->bdrv_needs_filename && !VAR_6) { error_setg(VAR_3, "The '%s' block driver requires a VAR_1 name",
drv->format_name); VAR_4 = -EINVAL; goto fail_opts; } trace_bdrv_open_common(VAR_0, VAR_6 ?: "", VAR_0->VAR_5,
drv->format_name); VAR_8 = qemu_opt_get(opts, "node-name"); bdrv_assign_node_name(VAR_0, VAR_8, &local_err); if (local_err) { error_propagate(VAR_3, local_err); VAR_4 = -EINVAL; goto fail_opts; } VAR_0->read_only = !(VAR_0->VAR_5 & BDRV_O_RDWR); if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, VAR_0->read_only)) { error_setg(VAR_3,
!VAR_0->read_only && bdrv_is_whitelisted(drv, true)
? "Driver '%s' can only be used for read-only devices"
: "Driver '%s' is not whitelisted",
drv->format_name); VAR_4 = -ENOTSUP; goto fail_opts; } assert(VAR_0->copy_on_read == 0); if (VAR_0->VAR_5 & BDRV_O_COPY_ON_READ) { if (!VAR_0->read_only) { bdrv_enable_copy_on_read(VAR_0); } else { error_setg(VAR_3, "Can't use copy-on-read on read-only device"); VAR_4 = -EINVAL; goto fail_opts; } } if (VAR_6 != NULL) { pstrcpy(VAR_0->VAR_6, sizeof(VAR_0->VAR_6), VAR_6); } else { VAR_0->VAR_6[0] = '\0'; } pstrcpy(VAR_0->exact_filename, sizeof(VAR_0->exact_filename), VAR_0->VAR_6); VAR_0->drv = drv; VAR_0->opaque = g_malloc0(drv->instance_size); update_flags_from_options(&VAR_0->VAR_5, opts); VAR_5 = bdrv_open_flags(VAR_0, VAR_0->VAR_5); if (drv->bdrv_file_open) { assert(VAR_1 == NULL); assert(!drv->bdrv_needs_filename || VAR_6 != NULL); VAR_4 = drv->bdrv_file_open(VAR_0, VAR_2, VAR_5, &local_err); } else { if (VAR_1 == NULL) { error_setg(VAR_3, "Can't use '%s' as a block driver for the "
"protocol level", drv->format_name); VAR_4 = -EINVAL; goto free_and_fail; } VAR_0->VAR_1 = VAR_1; VAR_4 = drv->bdrv_open(VAR_0, VAR_2, VAR_5, &local_err); } if (VAR_4 < 0) { if (local_err) { error_propagate(VAR_3, local_err); } else if (VAR_0->VAR_6[0]) { error_setg_errno(VAR_3, -VAR_4, "Could not open '%s'", VAR_0->VAR_6); } else { error_setg_errno(VAR_3, -VAR_4, "Could not open image"); } goto free_and_fail; } VAR_4 = refresh_total_sectors(VAR_0, VAR_0->total_sectors); if (VAR_4 < 0) { error_setg_errno(VAR_3, -VAR_4, "Could not refresh total sector count"); goto free_and_fail; } bdrv_refresh_limits(VAR_0, &local_err); if (local_err) { error_propagate(VAR_3, local_err); VAR_4 = -EINVAL; goto free_and_fail; } assert(bdrv_opt_mem_align(VAR_0) != 0); assert(bdrv_min_mem_align(VAR_0) != 0); assert(is_power_of_2(VAR_0->request_alignment) || bdrv_is_sg(VAR_0)); qemu_opts_del(opts); return 0; free_and_fail:
VAR_0->VAR_1 = NULL; g_free(VAR_0->opaque); VAR_0->opaque = NULL; VAR_0->drv = NULL; fail_opts:
qemu_opts_del(opts); return VAR_4; }