static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,

        int nb_sectors)

{

    int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8;

    uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE;

    bool first = true;

    int sector_bits;



    for ( ; nb_sectors;

            bitnum += sector_bits,

            nb_sectors -= sector_bits,

            offset += BDRV_SECTOR_SIZE) {

        int ret, set;

        uint8_t bitmap[BDRV_SECTOR_SIZE];



        bitnum &= BITS_PER_BITMAP_SECTOR - 1;

        sector_bits = MIN(nb_sectors, BITS_PER_BITMAP_SECTOR - bitnum);



        ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));

        if (ret < 0) {

            return ret;

        }



        /* Skip over any already set bits */

        set = cow_find_streak(bitmap, 1, bitnum, sector_bits);

        bitnum += set;

        sector_bits -= set;

        nb_sectors -= set;

        if (!sector_bits) {

            continue;

        }



        if (first) {

            ret = bdrv_flush(bs->file);

            if (ret < 0) {

                return ret;

            }

            first = false;

        }



        cow_set_bits(bitmap, bitnum, sector_bits);



        ret = bdrv_pwrite(bs->file, offset, &bitmap, sizeof(bitmap));

        if (ret < 0) {

            return ret;

        }

    }



    return 0;

}

static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,

        int nb_sectors)

{

    int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8;

    uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE;

    bool first = true;

    int sector_bits;



    for ( ; nb_sectors;

            bitnum += sector_bits,

            nb_sectors -= sector_bits,

            offset += BDRV_SECTOR_SIZE) {

        int ret, set;

        uint8_t bitmap[BDRV_SECTOR_SIZE];



        bitnum &= BITS_PER_BITMAP_SECTOR - 1;

        sector_bits = MIN(nb_sectors, BITS_PER_BITMAP_SECTOR - bitnum);



        ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));

        if (ret < 0) {

            return ret;

        }



        

        set = cow_find_streak(bitmap, 1, bitnum, sector_bits);

        bitnum += set;

        sector_bits -= set;

        nb_sectors -= set;

        if (!sector_bits) {

            continue;

        }



        if (first) {

            ret = bdrv_flush(bs->file);

            if (ret < 0) {

                return ret;

            }

            first = false;

        }



        cow_set_bits(bitmap, bitnum, sector_bits);



        ret = bdrv_pwrite(bs->file, offset, &bitmap, sizeof(bitmap));

        if (ret < 0) {

            return ret;

        }

    }



    return 0;

}

static int FUNC_0(BlockDriverState *VAR_0, int64_t VAR_1,

        int VAR_2)

{

    int64_t bitnum = VAR_1 + sizeof(struct cow_header_v2) * 8;

    uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE;

    bool first = true;

    int VAR_3;



    for ( ; VAR_2;

            bitnum += VAR_3,

            VAR_2 -= VAR_3,

            offset += BDRV_SECTOR_SIZE) {

        int VAR_4, VAR_5;

        uint8_t bitmap[BDRV_SECTOR_SIZE];



        bitnum &= BITS_PER_BITMAP_SECTOR - 1;

        VAR_3 = MIN(VAR_2, BITS_PER_BITMAP_SECTOR - bitnum);



        VAR_4 = bdrv_pread(VAR_0->file, offset, &bitmap, sizeof(bitmap));

        if (VAR_4 < 0) {

            return VAR_4;

        }



        

        VAR_5 = cow_find_streak(bitmap, 1, bitnum, VAR_3);

        bitnum += VAR_5;

        VAR_3 -= VAR_5;

        VAR_2 -= VAR_5;

        if (!VAR_3) {

            continue;

        }



        if (first) {

            VAR_4 = bdrv_flush(VAR_0->file);

            if (VAR_4 < 0) {

                return VAR_4;

            }

            first = false;

        }



        cow_set_bits(bitmap, bitnum, VAR_3);



        VAR_4 = bdrv_pwrite(VAR_0->file, offset, &bitmap, sizeof(bitmap));

        if (VAR_4 < 0) {

            return VAR_4;

        }

    }



    return 0;

}

static int FUNC_0(BlockDriverState *VAR_0, int64_t VAR_1,
int VAR_2)
{ int64_t bitnum = VAR_1 + sizeof(struct cow_header_v2) * 8; uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE; bool first = true; int VAR_3; for ( ; VAR_2; bitnum += VAR_3,
VAR_2 -= VAR_3,
offset += BDRV_SECTOR_SIZE) { int VAR_4, VAR_5; uint8_t bitmap[BDRV_SECTOR_SIZE]; bitnum &= BITS_PER_BITMAP_SECTOR - 1; VAR_3 = MIN(VAR_2, BITS_PER_BITMAP_SECTOR - bitnum); VAR_4 = bdrv_pread(VAR_0->file, offset, &bitmap, sizeof(bitmap)); if (VAR_4 < 0) { return VAR_4; } VAR_5 = cow_find_streak(bitmap, 1, bitnum, VAR_3); bitnum += VAR_5; VAR_3 -= VAR_5; VAR_2 -= VAR_5; if (!VAR_3) { continue; } if (first) { VAR_4 = bdrv_flush(VAR_0->file); if (VAR_4 < 0) { return VAR_4; } first = false; } cow_set_bits(bitmap, bitnum, VAR_3); VAR_4 = bdrv_pwrite(VAR_0->file, offset, &bitmap, sizeof(bitmap)); if (VAR_4 < 0) { return VAR_4; } } return 0; }