static int check_refcounts_l1(BlockDriverState *bs,
                              uint16_t *refcount_table,
                              int refcount_table_size,
                              int64_t l1_table_offset, int l1_size,
                              int check_copied)
{
    BDRVQcowState *s = bs->opaque;
    uint64_t *l1_table, l2_offset, l1_size2;
    int i, refcount, ret;
    int errors = 0;
    l1_size2 = l1_size * sizeof(uint64_t);
    /* Mark L1 table as used */
    errors += inc_refcounts(bs, refcount_table, refcount_table_size,
                  l1_table_offset, l1_size2);
    /* Read L1 table entries from disk */
    l1_table = qemu_malloc(l1_size2);
    if (bdrv_pread(s->hd, l1_table_offset,
                   l1_table, l1_size2) != l1_size2)
        goto fail;
    for(i = 0;i < l1_size; i++)
        be64_to_cpus(&l1_table[i]);
    /* Do the actual checks */
    for(i = 0; i < l1_size; i++) {
        l2_offset = l1_table[i];
        if (l2_offset) {
            /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
            if (check_copied) {
                refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
                    >> s->cluster_bits);
                if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
                    fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
                        " refcount=%d\n", l2_offset, refcount);
            /* Mark L2 table as used */
            l2_offset &= ~QCOW_OFLAG_COPIED;
            errors += inc_refcounts(bs, refcount_table,
                          refcount_table_size,
                          l2_offset,
                          s->cluster_size);
            /* Process and check L2 entries */
            ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
                l2_offset, check_copied);
            if (ret < 0) {
                goto fail;
            errors += ret;
    qemu_free(l1_table);
    return errors;
fail:
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
    qemu_free(l1_table);
    return -EIO;
static int check_refcounts_l1(BlockDriverState *bs,
                              uint16_t *refcount_table,
                              int refcount_table_size,
                              int64_t l1_table_offset, int l1_size,
                              int check_copied)
{
    BDRVQcowState *s = bs->opaque;
    uint64_t *l1_table, l2_offset, l1_size2;
    int i, refcount, ret;
    int errors = 0;
    l1_size2 = l1_size * sizeof(uint64_t);
    
    errors += inc_refcounts(bs, refcount_table, refcount_table_size,
                  l1_table_offset, l1_size2);
    
    l1_table = qemu_malloc(l1_size2);
    if (bdrv_pread(s->hd, l1_table_offset,
                   l1_table, l1_size2) != l1_size2)
        goto fail;
    for(i = 0;i < l1_size; i++)
        be64_to_cpus(&l1_table[i]);
    
    for(i = 0; i < l1_size; i++) {
        l2_offset = l1_table[i];
        if (l2_offset) {
            
            if (check_copied) {
                refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
                    >> s->cluster_bits);
                if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
                    fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
                        " refcount=%d\n", l2_offset, refcount);
            
            l2_offset &= ~QCOW_OFLAG_COPIED;
            errors += inc_refcounts(bs, refcount_table,
                          refcount_table_size,
                          l2_offset,
                          s->cluster_size);
            
            ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
                l2_offset, check_copied);
            if (ret < 0) {
                goto fail;
            errors += ret;
    qemu_free(l1_table);
    return errors;
fail:
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
    qemu_free(l1_table);
    return -EIO;
static int FUNC_0(BlockDriverState *VAR_0,
                              uint16_t *VAR_1,
                              int VAR_2,
                              int64_t VAR_3, int VAR_4,
                              int VAR_5)
{
    BDRVQcowState *s = VAR_0->opaque;
    uint64_t *l1_table, l2_offset, l1_size2;
    int VAR_6, VAR_7, VAR_8;
    int VAR_9 = 0;
    l1_size2 = VAR_4 * sizeof(uint64_t);
    
    VAR_9 += inc_refcounts(VAR_0, VAR_1, VAR_2,
                  VAR_3, l1_size2);
    
    l1_table = qemu_malloc(l1_size2);
    if (bdrv_pread(s->hd, VAR_3,
                   l1_table, l1_size2) != l1_size2)
        goto fail;
    for(VAR_6 = 0;VAR_6 < VAR_4; VAR_6++)
        be64_to_cpus(&l1_table[VAR_6]);
    
    for(VAR_6 = 0; VAR_6 < VAR_4; VAR_6++) {
        l2_offset = l1_table[VAR_6];
        if (l2_offset) {
            
            if (VAR_5) {
                VAR_7 = get_refcount(VAR_0, (l2_offset & ~QCOW_OFLAG_COPIED)
                    >> s->cluster_bits);
                if ((VAR_7 == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
                    fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
                        " VAR_7=%d\n", l2_offset, VAR_7);
            
            l2_offset &= ~QCOW_OFLAG_COPIED;
            VAR_9 += inc_refcounts(VAR_0, VAR_1,
                          VAR_2,
                          l2_offset,
                          s->cluster_size);
            
            VAR_8 = check_refcounts_l2(VAR_0, VAR_1, VAR_2,
                l2_offset, VAR_5);
            if (VAR_8 < 0) {
                goto fail;
            VAR_9 += VAR_8;
    qemu_free(l1_table);
    return VAR_9;
fail:
    fprintf(stderr, "ERROR: I/O error in FUNC_0\n");
    qemu_free(l1_table);
    return -EIO;
static int FUNC_0(BlockDriverState *VAR_0,
uint16_t *VAR_1,
int VAR_2,
int64_t VAR_3, int VAR_4,
int VAR_5)
{ BDRVQcowState *s = VAR_0->opaque; uint64_t *l1_table, l2_offset, l1_size2; int VAR_6, VAR_7, VAR_8; int VAR_9 = 0; l1_size2 = VAR_4 * sizeof(uint64_t); VAR_9 += inc_refcounts(VAR_0, VAR_1, VAR_2,
VAR_3, l1_size2); l1_table = qemu_malloc(l1_size2); if (bdrv_pread(s->hd, VAR_3,
l1_table, l1_size2) != l1_size2)
goto fail; for(VAR_6 = 0;VAR_6 < VAR_4; VAR_6++) be64_to_cpus(&l1_table[VAR_6]); for(VAR_6 = 0; VAR_6 < VAR_4; VAR_6++) { l2_offset = l1_table[VAR_6]; if (l2_offset) { if (VAR_5) { VAR_7 = get_refcount(VAR_0, (l2_offset & ~QCOW_OFLAG_COPIED)
>> s->cluster_bits); if ((VAR_7 == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) { fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
" VAR_7=%d\n", l2_offset, VAR_7); l2_offset &= ~QCOW_OFLAG_COPIED; VAR_9 += inc_refcounts(VAR_0, VAR_1,
VAR_2,
l2_offset,
s->cluster_size); VAR_8 = check_refcounts_l2(VAR_0, VAR_1, VAR_2,
l2_offset, VAR_5); if (VAR_8 < 0) { goto fail; VAR_9 += VAR_8; qemu_free(l1_table); return VAR_9; fail:
fprintf(stderr, "ERROR: I/O error in FUNC_0\n"); qemu_free(l1_table); return -EIO;