int bdrv_commit(BlockDriverState *bs)

{

    BlockDriver *drv = bs->drv;

    int64_t sector, total_sectors;

    int n, ro, open_flags;

    int ret = 0;

    uint8_t *buf;

    char filename[1024];



    if (!drv)

        return -ENOMEDIUM;

    

    if (!bs->backing_hd) {

        return -ENOTSUP;

    }



    if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {

        return -EBUSY;

    }



    ro = bs->backing_hd->read_only;

    strncpy(filename, bs->backing_hd->filename, sizeof(filename));

    open_flags =  bs->backing_hd->open_flags;



    if (ro) {

        if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) {

            return -EACCES;

        }

    }



    total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;

    buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);



    for (sector = 0; sector < total_sectors; sector += n) {

        if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {



            if (bdrv_read(bs, sector, buf, n) != 0) {

                ret = -EIO;

                goto ro_cleanup;

            }



            if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {

                ret = -EIO;

                goto ro_cleanup;

            }

        }

    }



    if (drv->bdrv_make_empty) {

        ret = drv->bdrv_make_empty(bs);

        bdrv_flush(bs);

    }



    /*

     * Make sure all data we wrote to the backing device is actually

     * stable on disk.

     */

    if (bs->backing_hd)

        bdrv_flush(bs->backing_hd);



ro_cleanup:

    g_free(buf);



    if (ro) {

        /* ignoring error return here */

        bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL);

    }



    return ret;

}

int bdrv_commit(BlockDriverState *bs)

{

    BlockDriver *drv = bs->drv;

    int64_t sector, total_sectors;

    int n, ro, open_flags;

    int ret = 0;

    uint8_t *buf;

    char filename[1024];



    if (!drv)

        return -ENOMEDIUM;

    

    if (!bs->backing_hd) {

        return -ENOTSUP;

    }



    if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {

        return -EBUSY;

    }



    ro = bs->backing_hd->read_only;

    strncpy(filename, bs->backing_hd->filename, sizeof(filename));

    open_flags =  bs->backing_hd->open_flags;



    if (ro) {

        if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) {

            return -EACCES;

        }

    }



    total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;

    buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);



    for (sector = 0; sector < total_sectors; sector += n) {

        if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {



            if (bdrv_read(bs, sector, buf, n) != 0) {

                ret = -EIO;

                goto ro_cleanup;

            }



            if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {

                ret = -EIO;

                goto ro_cleanup;

            }

        }

    }



    if (drv->bdrv_make_empty) {

        ret = drv->bdrv_make_empty(bs);

        bdrv_flush(bs);

    }



    







    if (bs->backing_hd)

        bdrv_flush(bs->backing_hd);



ro_cleanup:

    g_free(buf);



    if (ro) {

        

        bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL);

    }



    return ret;

}

    char filename[1024];     strncpy(filename, bs->backing_hd->filename, sizeof(filename));
int FUNC_0(BlockDriverState *VAR_0)

{

    BlockDriver *drv = VAR_0->drv;

    int64_t sector, total_sectors;

    int VAR_1, VAR_2, VAR_3;

    int VAR_4 = 0;

    uint8_t *buf;

    char VAR_5[1024];



    if (!drv)

        return -ENOMEDIUM;

    

    if (!VAR_0->backing_hd) {

        return -ENOTSUP;

    }



    if (bdrv_in_use(VAR_0) || bdrv_in_use(VAR_0->backing_hd)) {

        return -EBUSY;

    }



    VAR_2 = VAR_0->backing_hd->read_only;

    strncpy(VAR_5, VAR_0->backing_hd->VAR_5, sizeof(VAR_5));

    VAR_3 =  VAR_0->backing_hd->VAR_3;



    if (VAR_2) {

        if (bdrv_reopen(VAR_0->backing_hd, VAR_3 | BDRV_O_RDWR, NULL)) {

            return -EACCES;

        }

    }



    total_sectors = bdrv_getlength(VAR_0) >> BDRV_SECTOR_BITS;

    buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);



    for (sector = 0; sector < total_sectors; sector += VAR_1) {

        if (bdrv_is_allocated(VAR_0, sector, COMMIT_BUF_SECTORS, &VAR_1)) {



            if (bdrv_read(VAR_0, sector, buf, VAR_1) != 0) {

                VAR_4 = -EIO;

                goto ro_cleanup;

            }



            if (bdrv_write(VAR_0->backing_hd, sector, buf, VAR_1) != 0) {

                VAR_4 = -EIO;

                goto ro_cleanup;

            }

        }

    }



    if (drv->bdrv_make_empty) {

        VAR_4 = drv->bdrv_make_empty(VAR_0);

        bdrv_flush(VAR_0);

    }



    







    if (VAR_0->backing_hd)

        bdrv_flush(VAR_0->backing_hd);



ro_cleanup:

    g_free(buf);



    if (VAR_2) {

        

        bdrv_reopen(VAR_0->backing_hd, VAR_3 & ~BDRV_O_RDWR, NULL);

    }



    return VAR_4;

}

int FUNC_0(BlockDriverState *VAR_0)
{ BlockDriver *drv = VAR_0->drv; int64_t sector, total_sectors; int VAR_1, VAR_2, VAR_3; int VAR_4 = 0; uint8_t *buf; char VAR_5[1024]; if (!drv)
return -ENOMEDIUM; if (!VAR_0->backing_hd) { return -ENOTSUP; } if (bdrv_in_use(VAR_0) || bdrv_in_use(VAR_0->backing_hd)) { return -EBUSY; } VAR_2 = VAR_0->backing_hd->read_only; strncpy(VAR_5, VAR_0->backing_hd->VAR_5, sizeof(VAR_5)); VAR_3 =  VAR_0->backing_hd->VAR_3; if (VAR_2) { if (bdrv_reopen(VAR_0->backing_hd, VAR_3 | BDRV_O_RDWR, NULL)) { return -EACCES; } } total_sectors = bdrv_getlength(VAR_0) >> BDRV_SECTOR_BITS; buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); for (sector = 0; sector < total_sectors; sector += VAR_1) { if (bdrv_is_allocated(VAR_0, sector, COMMIT_BUF_SECTORS, &VAR_1)) { if (bdrv_read(VAR_0, sector, buf, VAR_1) != 0) { VAR_4 = -EIO; goto ro_cleanup; } if (bdrv_write(VAR_0->backing_hd, sector, buf, VAR_1) != 0) { VAR_4 = -EIO; goto ro_cleanup; } } } if (drv->bdrv_make_empty) { VAR_4 = drv->bdrv_make_empty(VAR_0); bdrv_flush(VAR_0); } if (VAR_0->backing_hd)
bdrv_flush(VAR_0->backing_hd); ro_cleanup:
g_free(buf); if (VAR_2) { bdrv_reopen(VAR_0->backing_hd, VAR_3 & ~BDRV_O_RDWR, NULL); } return VAR_4; }