Skip to content

Commit cd6efd6

Browse files
Vladimir Sementsov-OgievskiyXanClic
authored andcommitted
qcow2-refcount: improve style of check_refcounts_l1()
- use g_autofree for l1_table - better name for size in bytes variable - reduce code blocks nesting - whitespaces, braces, newlines Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]> Reviewed-by: Hanna Reitz <[email protected]> Message-Id: <[email protected]> Signed-off-by: Hanna Reitz <[email protected]>
1 parent 289ef5f commit cd6efd6

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed

block/qcow2-refcount.c

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,71 +1862,73 @@ static int check_refcounts_l1(BlockDriverState *bs,
18621862
int flags, BdrvCheckMode fix, bool active)
18631863
{
18641864
BDRVQcow2State *s = bs->opaque;
1865-
uint64_t *l1_table = NULL, l2_offset, l1_size2;
1865+
size_t l1_size_bytes = l1_size * L1E_SIZE;
1866+
g_autofree uint64_t *l1_table = NULL;
1867+
uint64_t l2_offset;
18661868
int i, ret;
18671869

1868-
l1_size2 = l1_size * L1E_SIZE;
1870+
if (!l1_size) {
1871+
return 0;
1872+
}
18691873

18701874
/* Mark L1 table as used */
18711875
ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size,
1872-
l1_table_offset, l1_size2);
1876+
l1_table_offset, l1_size_bytes);
18731877
if (ret < 0) {
1874-
goto fail;
1878+
return ret;
1879+
}
1880+
1881+
l1_table = g_try_malloc(l1_size_bytes);
1882+
if (l1_table == NULL) {
1883+
res->check_errors++;
1884+
return -ENOMEM;
18751885
}
18761886

18771887
/* Read L1 table entries from disk */
1878-
if (l1_size2 > 0) {
1879-
l1_table = g_try_malloc(l1_size2);
1880-
if (l1_table == NULL) {
1881-
ret = -ENOMEM;
1882-
res->check_errors++;
1883-
goto fail;
1884-
}
1885-
ret = bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size2);
1886-
if (ret < 0) {
1887-
fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1888-
res->check_errors++;
1889-
goto fail;
1890-
}
1891-
for(i = 0;i < l1_size; i++)
1892-
be64_to_cpus(&l1_table[i]);
1888+
ret = bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size_bytes);
1889+
if (ret < 0) {
1890+
fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1891+
res->check_errors++;
1892+
return ret;
1893+
}
1894+
1895+
for (i = 0; i < l1_size; i++) {
1896+
be64_to_cpus(&l1_table[i]);
18931897
}
18941898

18951899
/* Do the actual checks */
1896-
for(i = 0; i < l1_size; i++) {
1897-
l2_offset = l1_table[i];
1898-
if (l2_offset) {
1899-
/* Mark L2 table as used */
1900-
l2_offset &= L1E_OFFSET_MASK;
1901-
ret = qcow2_inc_refcounts_imrt(bs, res,
1902-
refcount_table, refcount_table_size,
1903-
l2_offset, s->cluster_size);
1904-
if (ret < 0) {
1905-
goto fail;
1906-
}
1900+
for (i = 0; i < l1_size; i++) {
1901+
if (!l1_table[i]) {
1902+
continue;
1903+
}
19071904

1908-
/* L2 tables are cluster aligned */
1909-
if (offset_into_cluster(s, l2_offset)) {
1910-
fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1911-
"cluster aligned; L1 entry corrupted\n", l2_offset);
1912-
res->corruptions++;
1913-
}
1905+
l2_offset = l1_table[i] & L1E_OFFSET_MASK;
19141906

1915-
/* Process and check L2 entries */
1916-
ret = check_refcounts_l2(bs, res, refcount_table,
1917-
refcount_table_size, l2_offset, flags,
1918-
fix, active);
1919-
if (ret < 0) {
1920-
goto fail;
1921-
}
1907+
/* Mark L2 table as used */
1908+
ret = qcow2_inc_refcounts_imrt(bs, res,
1909+
refcount_table, refcount_table_size,
1910+
l2_offset, s->cluster_size);
1911+
if (ret < 0) {
1912+
return ret;
1913+
}
1914+
1915+
/* L2 tables are cluster aligned */
1916+
if (offset_into_cluster(s, l2_offset)) {
1917+
fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1918+
"cluster aligned; L1 entry corrupted\n", l2_offset);
1919+
res->corruptions++;
1920+
}
1921+
1922+
/* Process and check L2 entries */
1923+
ret = check_refcounts_l2(bs, res, refcount_table,
1924+
refcount_table_size, l2_offset, flags,
1925+
fix, active);
1926+
if (ret < 0) {
1927+
return ret;
19221928
}
19231929
}
1924-
g_free(l1_table);
1925-
return 0;
19261930

1927-
fail:
1928-
g_free(l1_table);
1929-
return ret;
1931+
return 0;
19301932
}
19311933

19321934
/*

0 commit comments

Comments
 (0)