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

    QDict *options, int flags, BlockDriver *drv)

{

    int ret, open_flags;

    const char *filename;



    assert(drv != NULL);

    assert(bs->file == NULL);

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



    trace_bdrv_open_common(bs, filename, flags, drv->format_name);



    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {

        return -ENOTSUP;

    }



    /* bdrv_open() with directly using a protocol as drv. This layer is already

     * opened, so assign it to bs (while file becomes a closed BlockDriverState)

     * and return immediately. */

    if (file != NULL && drv->bdrv_file_open) {

        bdrv_swap(file, bs);

        return 0;

    }



    bs->open_flags = flags;

    bs->buffer_alignment = 512;



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

    if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) {

        bdrv_enable_copy_on_read(bs);

    }



    if (file != NULL) {

        filename = file->filename;

    } else {

        filename = qdict_get_try_str(options, "filename");

    }



    if (filename != NULL) {

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

    } else {

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

    }



    bs->drv = drv;

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



    bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);

    open_flags = bdrv_open_flags(bs, flags);



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



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

    if (drv->bdrv_file_open) {

        assert(file == NULL);

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

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

    } else {

        if (file == NULL) {

            qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't use '%s' as a "

                          "block driver for the protocol level",

                          drv->format_name);

            ret = -EINVAL;

            goto free_and_fail;

        }

        assert(file != NULL);

        bs->file = file;

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

    }



    if (ret < 0) {

        goto free_and_fail;

    }



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

    if (ret < 0) {

        goto free_and_fail;

    }



#ifndef _WIN32

    if (bs->is_temporary) {

        assert(filename != NULL);

        unlink(filename);

    }

#endif

    return 0;



free_and_fail:

    bs->file = NULL;

    g_free(bs->opaque);

    bs->opaque = NULL;

    bs->drv = NULL;

    return ret;

}

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

    QDict *options, int flags, BlockDriver *drv)

{

    int ret, open_flags;

    const char *filename;



    assert(drv != NULL);

    assert(bs->file == NULL);

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



    trace_bdrv_open_common(bs, filename, flags, drv->format_name);



    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {

        return -ENOTSUP;

    }



    





    if (file != NULL && drv->bdrv_file_open) {

        bdrv_swap(file, bs);

        return 0;

    }



    bs->open_flags = flags;

    bs->buffer_alignment = 512;



    assert(bs->copy_on_read == 0); 

    if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) {

        bdrv_enable_copy_on_read(bs);

    }



    if (file != NULL) {

        filename = file->filename;

    } else {

        filename = qdict_get_try_str(options, "filename");

    }



    if (filename != NULL) {

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

    } else {

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

    }



    bs->drv = drv;

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



    bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);

    open_flags = bdrv_open_flags(bs, flags);



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



    

    if (drv->bdrv_file_open) {

        assert(file == NULL);

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

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

    } else {

        if (file == NULL) {

            qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't use '%s' as a "

                          "block driver for the protocol level",

                          drv->format_name);

            ret = -EINVAL;

            goto free_and_fail;

        }

        assert(file != NULL);

        bs->file = file;

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

    }



    if (ret < 0) {

        goto free_and_fail;

    }



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

    if (ret < 0) {

        goto free_and_fail;

    }



#ifndef _WIN32

    if (bs->is_temporary) {

        assert(filename != NULL);

        unlink(filename);

    }

#endif

    return 0;



free_and_fail:

    bs->file = NULL;

    g_free(bs->opaque);

    bs->opaque = NULL;

    bs->drv = NULL;

    return ret;

}

    trace_bdrv_open_common(bs, filename, flags, drv->format_name);     if (file != NULL) {         filename = file->filename;     } else {         filename = qdict_get_try_str(options, "filename");
static int FUNC_0(BlockDriverState *VAR_0, BlockDriverState *VAR_1,

    QDict *VAR_2, int VAR_3, BlockDriver *VAR_4)

{

    int VAR_5, VAR_6;

    const char *VAR_7;



    assert(VAR_4 != NULL);

    assert(VAR_0->VAR_1 == NULL);

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



    trace_bdrv_open_common(VAR_0, VAR_7, VAR_3, VAR_4->format_name);



    if (use_bdrv_whitelist && !bdrv_is_whitelisted(VAR_4)) {

        return -ENOTSUP;

    }



    





    if (VAR_1 != NULL && VAR_4->bdrv_file_open) {

        bdrv_swap(VAR_1, VAR_0);

        return 0;

    }



    VAR_0->VAR_6 = VAR_3;

    VAR_0->buffer_alignment = 512;



    assert(VAR_0->copy_on_read == 0); 

    if ((VAR_3 & BDRV_O_RDWR) && (VAR_3 & BDRV_O_COPY_ON_READ)) {

        bdrv_enable_copy_on_read(VAR_0);

    }



    if (VAR_1 != NULL) {

        VAR_7 = VAR_1->VAR_7;

    } else {

        VAR_7 = qdict_get_try_str(VAR_2, "VAR_7");

    }



    if (VAR_7 != NULL) {

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

    } else {

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

    }



    VAR_0->VAR_4 = VAR_4;

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



    VAR_0->enable_write_cache = !!(VAR_3 & BDRV_O_CACHE_WB);

    VAR_6 = bdrv_open_flags(VAR_0, VAR_3);



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



    

    if (VAR_4->bdrv_file_open) {

        assert(VAR_1 == NULL);

        assert(VAR_4->bdrv_parse_filename || VAR_7 != NULL);

        VAR_5 = VAR_4->bdrv_file_open(VAR_0, VAR_2, VAR_6);

    } else {

        if (VAR_1 == NULL) {

            qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't use '%s' as a "

                          "block driver for the protocol level",

                          VAR_4->format_name);

            VAR_5 = -EINVAL;

            goto free_and_fail;

        }

        assert(VAR_1 != NULL);

        VAR_0->VAR_1 = VAR_1;

        VAR_5 = VAR_4->bdrv_open(VAR_0, VAR_2, VAR_6);

    }



    if (VAR_5 < 0) {

        goto free_and_fail;

    }



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

    if (VAR_5 < 0) {

        goto free_and_fail;

    }



#ifndef _WIN32

    if (VAR_0->is_temporary) {

        assert(VAR_7 != NULL);

        unlink(VAR_7);

    }

#endif

    return 0;



free_and_fail:

    VAR_0->VAR_1 = NULL;

    g_free(VAR_0->opaque);

    VAR_0->opaque = NULL;

    VAR_0->VAR_4 = NULL;

    return VAR_5;

}

static int FUNC_0(BlockDriverState *VAR_0, BlockDriverState *VAR_1,
QDict *VAR_2, int VAR_3, BlockDriver *VAR_4)
{ int VAR_5, VAR_6; const char *VAR_7; assert(VAR_4 != NULL); assert(VAR_0->VAR_1 == NULL); assert(VAR_2 != NULL && VAR_0->VAR_2 != VAR_2); trace_bdrv_open_common(VAR_0, VAR_7, VAR_3, VAR_4->format_name); if (use_bdrv_whitelist && !bdrv_is_whitelisted(VAR_4)) { return -ENOTSUP; } if (VAR_1 != NULL && VAR_4->bdrv_file_open) { bdrv_swap(VAR_1, VAR_0); return 0; } VAR_0->VAR_6 = VAR_3; VAR_0->buffer_alignment = 512; assert(VAR_0->copy_on_read == 0); if ((VAR_3 & BDRV_O_RDWR) && (VAR_3 & BDRV_O_COPY_ON_READ)) { bdrv_enable_copy_on_read(VAR_0); } if (VAR_1 != NULL) { VAR_7 = VAR_1->VAR_7; } else { VAR_7 = qdict_get_try_str(VAR_2, "VAR_7"); } if (VAR_7 != NULL) { pstrcpy(VAR_0->VAR_7, sizeof(VAR_0->VAR_7), VAR_7); } else { VAR_0->VAR_7[0] = '\0'; } VAR_0->VAR_4 = VAR_4; VAR_0->opaque = g_malloc0(VAR_4->instance_size); VAR_0->enable_write_cache = !!(VAR_3 & BDRV_O_CACHE_WB); VAR_6 = bdrv_open_flags(VAR_0, VAR_3); VAR_0->read_only = !(VAR_6 & BDRV_O_RDWR); if (VAR_4->bdrv_file_open) { assert(VAR_1 == NULL); assert(VAR_4->bdrv_parse_filename || VAR_7 != NULL); VAR_5 = VAR_4->bdrv_file_open(VAR_0, VAR_2, VAR_6); } else { if (VAR_1 == NULL) { qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't use '%s' as a "
"block driver for the protocol level",
VAR_4->format_name); VAR_5 = -EINVAL; goto free_and_fail; } assert(VAR_1 != NULL); VAR_0->VAR_1 = VAR_1; VAR_5 = VAR_4->bdrv_open(VAR_0, VAR_2, VAR_6); } if (VAR_5 < 0) { goto free_and_fail; } VAR_5 = refresh_total_sectors(VAR_0, VAR_0->total_sectors); if (VAR_5 < 0) { goto free_and_fail; } #ifndef _WIN32
if (VAR_0->is_temporary) { assert(VAR_7 != NULL); unlink(VAR_7); } #endif
return 0; free_and_fail:
VAR_0->VAR_1 = NULL; g_free(VAR_0->opaque); VAR_0->opaque = NULL; VAR_0->VAR_4 = NULL; return VAR_5; }