int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
                          BdrvCheckMode fix)
{
    BDRVQcowState *s = bs->opaque;
    BdrvCheckResult pre_compare_res;
    int64_t size, highest_cluster, nb_clusters;
    uint16_t *refcount_table = NULL;
    bool rebuild = false;
    int ret;
    size = bdrv_getlength(bs->file);
    if (size < 0) {
        res->check_errors++;
        return size;
    nb_clusters = size_to_clusters(s, size);
    if (nb_clusters > INT_MAX) {
        res->check_errors++;
        return -EFBIG;
    res->bfi.total_clusters =
        size_to_clusters(s, bs->total_sectors * BDRV_SECTOR_SIZE);
    ret = calculate_refcounts(bs, res, fix, &rebuild, &refcount_table,
    /* In case we don't need to rebuild the refcount structure (but want to fix
     * something), this function is immediately called again, in which case the
     * result should be ignored */
    pre_compare_res = *res;
    compare_refcounts(bs, res, 0, &rebuild, &highest_cluster, refcount_table,
                      nb_clusters);
    if (rebuild && (fix & BDRV_FIX_ERRORS)) {
        fprintf(stderr, "Rebuilding refcount structure\n");
        ret = rebuild_refcount_structure(bs, res, &refcount_table,
    } else if (fix) {
            fprintf(stderr, "ERROR need to rebuild refcount structures\n");
            res->check_errors++;
            ret = -EIO;
        if (res->leaks || res->corruptions) {
            *res = pre_compare_res;
            compare_refcounts(bs, res, fix, &rebuild, &highest_cluster,
                              refcount_table, nb_clusters);
    /* check OFLAG_COPIED */
    ret = check_oflag_copied(bs, res, fix);
    res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
    ret = 0;
fail:
    g_free(refcount_table);
    return ret;
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
                          BdrvCheckMode fix)
{
    BDRVQcowState *s = bs->opaque;
    BdrvCheckResult pre_compare_res;
    int64_t size, highest_cluster, nb_clusters;
    uint16_t *refcount_table = NULL;
    bool rebuild = false;
    int ret;
    size = bdrv_getlength(bs->file);
    if (size < 0) {
        res->check_errors++;
        return size;
    nb_clusters = size_to_clusters(s, size);
    if (nb_clusters > INT_MAX) {
        res->check_errors++;
        return -EFBIG;
    res->bfi.total_clusters =
        size_to_clusters(s, bs->total_sectors * BDRV_SECTOR_SIZE);
    ret = calculate_refcounts(bs, res, fix, &rebuild, &refcount_table,
    


    pre_compare_res = *res;
    compare_refcounts(bs, res, 0, &rebuild, &highest_cluster, refcount_table,
                      nb_clusters);
    if (rebuild && (fix & BDRV_FIX_ERRORS)) {
        fprintf(stderr, "Rebuilding refcount structure\n");
        ret = rebuild_refcount_structure(bs, res, &refcount_table,
    } else if (fix) {
            fprintf(stderr, "ERROR need to rebuild refcount structures\n");
            res->check_errors++;
            ret = -EIO;
        if (res->leaks || res->corruptions) {
            *res = pre_compare_res;
            compare_refcounts(bs, res, fix, &rebuild, &highest_cluster,
                              refcount_table, nb_clusters);
    
    ret = check_oflag_copied(bs, res, fix);
    res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
    ret = 0;
fail:
    g_free(refcount_table);
    return ret;
int FUNC_0(BlockDriverState *VAR_0, BdrvCheckResult *VAR_1,
                          BdrvCheckMode VAR_2)
{
    BDRVQcowState *s = VAR_0->opaque;
    BdrvCheckResult pre_compare_res;
    int64_t size, highest_cluster, nb_clusters;
    uint16_t *refcount_table = NULL;
    bool rebuild = false;
    int VAR_3;
    size = bdrv_getlength(VAR_0->file);
    if (size < 0) {
        VAR_1->check_errors++;
        return size;
    nb_clusters = size_to_clusters(s, size);
    if (nb_clusters > INT_MAX) {
        VAR_1->check_errors++;
        return -EFBIG;
    VAR_1->bfi.total_clusters =
        size_to_clusters(s, VAR_0->total_sectors * BDRV_SECTOR_SIZE);
    VAR_3 = calculate_refcounts(VAR_0, VAR_1, VAR_2, &rebuild, &refcount_table,
    


    pre_compare_res = *VAR_1;
    compare_refcounts(VAR_0, VAR_1, 0, &rebuild, &highest_cluster, refcount_table,
                      nb_clusters);
    if (rebuild && (VAR_2 & BDRV_FIX_ERRORS)) {
        fprintf(stderr, "Rebuilding refcount structure\n");
        VAR_3 = rebuild_refcount_structure(VAR_0, VAR_1, &refcount_table,
    } else if (VAR_2) {
            fprintf(stderr, "ERROR need to rebuild refcount structures\n");
            VAR_1->check_errors++;
            VAR_3 = -EIO;
        if (VAR_1->leaks || VAR_1->corruptions) {
            *VAR_1 = pre_compare_res;
            compare_refcounts(VAR_0, VAR_1, VAR_2, &rebuild, &highest_cluster,
                              refcount_table, nb_clusters);
    
    VAR_3 = check_oflag_copied(VAR_0, VAR_1, VAR_2);
    VAR_1->image_end_offset = (highest_cluster + 1) * s->cluster_size;
    VAR_3 = 0;
fail:
    g_free(refcount_table);
    return VAR_3;
int FUNC_0(BlockDriverState *VAR_0, BdrvCheckResult *VAR_1,
BdrvCheckMode VAR_2)
{ BDRVQcowState *s = VAR_0->opaque; BdrvCheckResult pre_compare_res; int64_t size, highest_cluster, nb_clusters; uint16_t *refcount_table = NULL; bool rebuild = false; int VAR_3; size = bdrv_getlength(VAR_0->file); if (size < 0) { VAR_1->check_errors++; return size; nb_clusters = size_to_clusters(s, size); if (nb_clusters > INT_MAX) { VAR_1->check_errors++; return -EFBIG; VAR_1->bfi.total_clusters =
size_to_clusters(s, VAR_0->total_sectors * BDRV_SECTOR_SIZE); VAR_3 = calculate_refcounts(VAR_0, VAR_1, VAR_2, &rebuild, &refcount_table,
pre_compare_res = *VAR_1; compare_refcounts(VAR_0, VAR_1, 0, &rebuild, &highest_cluster, refcount_table,
nb_clusters); if (rebuild && (VAR_2 & BDRV_FIX_ERRORS)) { fprintf(stderr, "Rebuilding refcount structure\n"); VAR_3 = rebuild_refcount_structure(VAR_0, VAR_1, &refcount_table,
} else if (VAR_2) { fprintf(stderr, "ERROR need to rebuild refcount structures\n"); VAR_1->check_errors++; VAR_3 = -EIO; if (VAR_1->leaks || VAR_1->corruptions) { *VAR_1 = pre_compare_res; compare_refcounts(VAR_0, VAR_1, VAR_2, &rebuild, &highest_cluster,
refcount_table, nb_clusters); VAR_3 = check_oflag_copied(VAR_0, VAR_1, VAR_2); VAR_1->image_end_offset = (highest_cluster + 1) * s->cluster_size; VAR_3 = 0; fail:
g_free(refcount_table); return VAR_3;