static int cow_open(BlockDriverState *bs, QDict *options, int flags,

                    Error **errp)

{

    BDRVCowState *s = bs->opaque;

    struct cow_header_v2 cow_header;

    int bitmap_size;

    int64_t size;

    int ret;



    /* see if it is a cow image */

    ret = bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header));

    if (ret < 0) {

        goto fail;

    }



    if (be32_to_cpu(cow_header.magic) != COW_MAGIC) {

        error_setg(errp, "Image not in COW format");

        ret = -EINVAL;

        goto fail;

    }



    if (be32_to_cpu(cow_header.version) != COW_VERSION) {

        char version[64];

        snprintf(version, sizeof(version),

               "COW version %" PRIu32, cow_header.version);

        error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,

            bs->device_name, "cow", version);

        ret = -ENOTSUP;

        goto fail;

    }



    /* cow image found */

    size = be64_to_cpu(cow_header.size);

    bs->total_sectors = size / 512;



    pstrcpy(bs->backing_file, sizeof(bs->backing_file),

            cow_header.backing_file);



    bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);

    s->cow_sectors_offset = (bitmap_size + 511) & ~511;

    qemu_co_mutex_init(&s->lock);

    return 0;

 fail:

    return ret;

}

static int cow_open(BlockDriverState *bs, QDict *options, int flags,

                    Error **errp)

{

    BDRVCowState *s = bs->opaque;

    struct cow_header_v2 cow_header;

    int bitmap_size;

    int64_t size;

    int ret;



    

    ret = bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header));

    if (ret < 0) {

        goto fail;

    }



    if (be32_to_cpu(cow_header.magic) != COW_MAGIC) {

        error_setg(errp, "Image not in COW format");

        ret = -EINVAL;

        goto fail;

    }



    if (be32_to_cpu(cow_header.version) != COW_VERSION) {

        char version[64];

        snprintf(version, sizeof(version),

               "COW version %" PRIu32, cow_header.version);

        error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,

            bs->device_name, "cow", version);

        ret = -ENOTSUP;

        goto fail;

    }



    

    size = be64_to_cpu(cow_header.size);

    bs->total_sectors = size / 512;



    pstrcpy(bs->backing_file, sizeof(bs->backing_file),

            cow_header.backing_file);



    bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);

    s->cow_sectors_offset = (bitmap_size + 511) & ~511;

    qemu_co_mutex_init(&s->lock);

    return 0;

 fail:

    return ret;

}

static int FUNC_0(BlockDriverState *VAR_0, QDict *VAR_1, int VAR_2,

                    Error **VAR_3)

{

    BDRVCowState *s = VAR_0->opaque;

    struct cow_header_v2 VAR_4;

    int VAR_5;

    int64_t size;

    int VAR_6;



    

    VAR_6 = bdrv_pread(VAR_0->file, 0, &VAR_4, sizeof(VAR_4));

    if (VAR_6 < 0) {

        goto fail;

    }



    if (be32_to_cpu(VAR_4.magic) != COW_MAGIC) {

        error_setg(VAR_3, "Image not in COW format");

        VAR_6 = -EINVAL;

        goto fail;

    }



    if (be32_to_cpu(VAR_4.VAR_7) != COW_VERSION) {

        char VAR_7[64];

        snprintf(VAR_7, sizeof(VAR_7),

               "COW VAR_7 %" PRIu32, VAR_4.VAR_7);

        error_set(VAR_3, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,

            VAR_0->device_name, "cow", VAR_7);

        VAR_6 = -ENOTSUP;

        goto fail;

    }



    

    size = be64_to_cpu(VAR_4.size);

    VAR_0->total_sectors = size / 512;



    pstrcpy(VAR_0->backing_file, sizeof(VAR_0->backing_file),

            VAR_4.backing_file);



    VAR_5 = ((VAR_0->total_sectors + 7) >> 3) + sizeof(VAR_4);

    s->cow_sectors_offset = (VAR_5 + 511) & ~511;

    qemu_co_mutex_init(&s->lock);

    return 0;

 fail:

    return VAR_6;

}

static int FUNC_0(BlockDriverState *VAR_0, QDict *VAR_1, int VAR_2,
Error **VAR_3)
{ BDRVCowState *s = VAR_0->opaque; struct cow_header_v2 VAR_4; int VAR_5; int64_t size; int VAR_6; VAR_6 = bdrv_pread(VAR_0->file, 0, &VAR_4, sizeof(VAR_4)); if (VAR_6 < 0) { goto fail; } if (be32_to_cpu(VAR_4.magic) != COW_MAGIC) { error_setg(VAR_3, "Image not in COW format"); VAR_6 = -EINVAL; goto fail; } if (be32_to_cpu(VAR_4.VAR_7) != COW_VERSION) { char VAR_7[64]; snprintf(VAR_7, sizeof(VAR_7),
"COW VAR_7 %" PRIu32, VAR_4.VAR_7); error_set(VAR_3, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
VAR_0->device_name, "cow", VAR_7); VAR_6 = -ENOTSUP; goto fail; } size = be64_to_cpu(VAR_4.size); VAR_0->total_sectors = size / 512; pstrcpy(VAR_0->backing_file, sizeof(VAR_0->backing_file),
VAR_4.backing_file); VAR_5 = ((VAR_0->total_sectors + 7) >> 3) + sizeof(VAR_4); s->cow_sectors_offset = (VAR_5 + 511) & ~511; qemu_co_mutex_init(&s->lock); return 0; fail:
return VAR_6; }