From nobody Sun Nov 16 04:12:35 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1598613108; cv=none; d=zohomail.com; s=zohoarc; b=YUB20rFi3g9es/tFVVsmj5petLVYZfjCuKxYO+5EO9UK0JJWnV6lUHdioul4ArlSt8RzaG3kbyoZ3LNuJ1NlLJthSSS4Reb5lLuTGT+yrBQvgDL1RzQJ1Rg9hY9gI3n5RckpIcwObAVKs2SXw60j3pBqGyO3+aCPc3+BXZ7JEHk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598613108; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=UUR9W5zRzm02Un+Xj9o8GlNCqep1SV9nMZAHxhhJxFo=; b=VWfL4meGBMlSFa4iHF/y4sBIPbW0GitOV97TBYRKeSFrl5q23az6qNTZ4K8Pz1YKDJ2IkXkjgpeojS/PTwL8X9iA7WA+FCKGRWTFJBDfkADOMavv24Q8cHwLBi+a5LE2YyyJecfXRbC3bcFckSJ4Jp2nnZf/YIuf3DzZAH6gVOE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598613107303920.5605480802571; Fri, 28 Aug 2020 04:11:47 -0700 (PDT) Received: from localhost ([::1]:48886 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBcIL-00015t-R1 for importer@patchew.org; Fri, 28 Aug 2020 07:11:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39748) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBcFy-0004ij-4T; Fri, 28 Aug 2020 07:09:18 -0400 Received: from fanzine.igalia.com ([178.60.130.6]:58265) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBcFt-0000Sn-Mb; Fri, 28 Aug 2020 07:09:17 -0400 Received: from 92.red-79-158-239.dynamicip.rima-tde.net ([79.158.239.92] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1kBcFR-0005uV-Ot; Fri, 28 Aug 2020 13:08:46 +0200 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1kBcFE-0003br-AI; Fri, 28 Aug 2020 13:08:32 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From; bh=UUR9W5zRzm02Un+Xj9o8GlNCqep1SV9nMZAHxhhJxFo=; b=KOD+dq8w2UNhrbLBBFdWGWbIidFt4tntSDBjIrd32l2dkoCdroWWetQl3thKWKcms502Zx+6/6tZKWVZvMbBoKRirDLJACQ1XJF8w7xorCaAICK8NpUwU9GMrXnUanJQuWA06R2FN9xm6dwItdsjerwgnjZlA4icbGIVvVTJFKdE7T5otsC91bBwAo4/hJMJ0nk4QhVgWdUEXlskU5qIbUCoDzmB6cHwlxmWg1BJxblAwYf0q+FbxSJDiWMSjipl3Kd97/KF4aOP64pcmwJtrsAXpN95FJT+CLYw7kJjA2rS8GlATHyrBbYcsOzd9g4gZiVXRFL12s9t+L/zVFlZHQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH] qcow2: Use macros for the L1, refcount and bitmap table entry sizes Date: Fri, 28 Aug 2020 13:08:28 +0200 Message-Id: <20200828110828.13833-1-berto@igalia.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=178.60.130.6; envelope-from=berto@igalia.com; helo=fanzine.igalia.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/28 07:08:47 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch replaces instances of sizeof(uint64_t) in the qcow2 driver with macros that indicate what those sizes are actually referring to. Signed-off-by: Alberto Garcia --- block/qcow2.h | 6 +++ block/qcow2-bitmap.c | 11 ++++-- block/qcow2-cluster.c | 24 ++++++------ block/qcow2-refcount.c | 89 ++++++++++++++++++++++-------------------- block/qcow2-snapshot.c | 20 +++++----- block/qcow2.c | 27 ++++++------- 6 files changed, 94 insertions(+), 83 deletions(-) diff --git a/block/qcow2.h b/block/qcow2.h index 065ec3df0b..83cfa0c391 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -99,6 +99,12 @@ #define L2E_SIZE_NORMAL (sizeof(uint64_t)) #define L2E_SIZE_EXTENDED (sizeof(uint64_t) * 2) =20 +/* Size of L1 table entries */ +#define L1E_SIZE (sizeof(uint64_t)) + +/* Size of reftable entries */ +#define REFTABLE_ENTRY_SIZE (sizeof(uint64_t)) + #define MIN_CLUSTER_BITS 9 #define MAX_CLUSTER_BITS 21 =20 diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c index 8c34b2aef7..d7a31a8ddc 100644 --- a/block/qcow2-bitmap.c +++ b/block/qcow2-bitmap.c @@ -42,6 +42,9 @@ #define BME_MIN_GRANULARITY_BITS 9 #define BME_MAX_NAME_SIZE 1023 =20 +/* Size of bitmap table entries */ +#define BME_TABLE_ENTRY_SIZE (sizeof(uint64_t)) + QEMU_BUILD_BUG_ON(BME_MAX_NAME_SIZE !=3D BDRV_BITMAP_MAX_NAME_SIZE); =20 #if BME_MAX_TABLE_SIZE * 8ULL > INT_MAX @@ -232,7 +235,7 @@ static int bitmap_table_load(BlockDriverState *bs, Qcow= 2BitmapTable *tb, =20 assert(tb->size <=3D BME_MAX_TABLE_SIZE); ret =3D bdrv_pread(bs->file, tb->offset, - table, tb->size * sizeof(uint64_t)); + table, tb->size * BME_TABLE_ENTRY_SIZE); if (ret < 0) { goto fail; } @@ -265,7 +268,7 @@ static int free_bitmap_clusters(BlockDriverState *bs, Q= cow2BitmapTable *tb) } =20 clear_bitmap_table(bs, bitmap_table, tb->size); - qcow2_free_clusters(bs, tb->offset, tb->size * sizeof(uint64_t), + qcow2_free_clusters(bs, tb->offset, tb->size * BME_TABLE_ENTRY_SIZE, QCOW2_DISCARD_OTHER); g_free(bitmap_table); =20 @@ -690,7 +693,7 @@ int qcow2_check_bitmaps_refcounts(BlockDriverState *bs,= BdrvCheckResult *res, ret =3D qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size, bm->table.offset, - bm->table.size * sizeof(uint64_t)); + bm->table.size * BME_TABLE_ENTRY_SI= ZE); if (ret < 0) { goto out; } @@ -1797,7 +1800,7 @@ uint64_t qcow2_get_persistent_dirty_bitmap_size(Block= DriverState *in_bs, /* Assume the entire bitmap is allocated */ bitmaps_size +=3D bmclusters * cluster_size; /* Also reserve space for the bitmap table entries */ - bitmaps_size +=3D ROUND_UP(bmclusters * sizeof(uint64_t), + bitmaps_size +=3D ROUND_UP(bmclusters * BME_TABLE_ENTRY_SIZE, cluster_size); /* And space for contribution to bitmap directory size */ bitmap_dir_size +=3D calc_dir_entry_size(strlen(name), 0); diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 996b3314f4..5b58720607 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -47,8 +47,8 @@ int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t = exact_size) =20 BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_WRITE_TABLE); ret =3D bdrv_pwrite_zeroes(bs->file, s->l1_table_offset + - new_l1_size * sizeof(uint64_t), - (s->l1_size - new_l1_size) * sizeof(uint64_t)= , 0); + new_l1_size * L1E_SIZE, + (s->l1_size - new_l1_size) * L1E_SIZE, 0); if (ret < 0) { goto fail; } @@ -76,7 +76,7 @@ fail: * l1_table in memory to avoid possible image corruption. */ memset(s->l1_table + new_l1_size, 0, - (s->l1_size - new_l1_size) * sizeof(uint64_t)); + (s->l1_size - new_l1_size) * L1E_SIZE); return ret; } =20 @@ -96,7 +96,7 @@ int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t mi= n_size, /* Do a sanity check on min_size before trying to calculate new_l1_size * (this prevents overflows during the while loop for the calculation = of * new_l1_size) */ - if (min_size > INT_MAX / sizeof(uint64_t)) { + if (min_size > INT_MAX / L1E_SIZE) { return -EFBIG; } =20 @@ -114,7 +114,7 @@ int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t = min_size, } =20 QEMU_BUILD_BUG_ON(QCOW_MAX_L1_SIZE > INT_MAX); - if (new_l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) { + if (new_l1_size > QCOW_MAX_L1_SIZE / L1E_SIZE) { return -EFBIG; } =20 @@ -123,7 +123,7 @@ int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t = min_size, s->l1_size, new_l1_size); #endif =20 - new_l1_size2 =3D sizeof(uint64_t) * new_l1_size; + new_l1_size2 =3D L1E_SIZE * new_l1_size; new_l1_table =3D qemu_try_blockalign(bs->file->bs, new_l1_size2); if (new_l1_table =3D=3D NULL) { return -ENOMEM; @@ -131,7 +131,7 @@ int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t = min_size, memset(new_l1_table, 0, new_l1_size2); =20 if (s->l1_size) { - memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t)); + memcpy(new_l1_table, s->l1_table, s->l1_size * L1E_SIZE); } =20 /* write new table (align to cluster) */ @@ -180,7 +180,7 @@ int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t = min_size, s->l1_table =3D new_l1_table; old_l1_size =3D s->l1_size; s->l1_size =3D new_l1_size; - qcow2_free_clusters(bs, old_l1_table_offset, old_l1_size * sizeof(uint= 64_t), + qcow2_free_clusters(bs, old_l1_table_offset, old_l1_size * L1E_SIZE, QCOW2_DISCARD_OTHER); return 0; fail: @@ -225,9 +225,9 @@ int qcow2_write_l1_entry(BlockDriverState *bs, int l1_i= ndex) BDRVQcow2State *s =3D bs->opaque; int l1_start_index; int i, ret; - int bufsize =3D MAX(sizeof(uint64_t), + int bufsize =3D MAX(L1E_SIZE, MIN(bs->file->bs->bl.request_alignment, s->cluster_s= ize)); - int nentries =3D bufsize / sizeof(uint64_t); + int nentries =3D bufsize / L1E_SIZE; g_autofree uint64_t *buf =3D g_try_new0(uint64_t, nentries); =20 if (buf =3D=3D NULL) { @@ -2409,7 +2409,7 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs, Error *local_err =3D NULL; =20 ret =3D qcow2_validate_table(bs, s->snapshots[i].l1_table_offset, - s->snapshots[i].l1_size, sizeof(uint64_= t), + s->snapshots[i].l1_size, L1E_SIZE, QCOW_MAX_L1_SIZE, "Snapshot L1 table", &local_err); if (ret < 0) { @@ -2417,7 +2417,7 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs, goto fail; } =20 - l1_size2 =3D s->snapshots[i].l1_size * sizeof(uint64_t); + l1_size2 =3D s->snapshots[i].l1_size * L1E_SIZE; new_l1_table =3D g_try_realloc(l1_table, l1_size2); =20 if (!new_l1_table) { diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index aae52607eb..51e9778ed8 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -105,8 +105,8 @@ int qcow2_refcount_init(BlockDriverState *bs) s->get_refcount =3D get_refcount_funcs[s->refcount_order]; s->set_refcount =3D set_refcount_funcs[s->refcount_order]; =20 - assert(s->refcount_table_size <=3D INT_MAX / sizeof(uint64_t)); - refcount_table_size2 =3D s->refcount_table_size * sizeof(uint64_t); + assert(s->refcount_table_size <=3D INT_MAX / REFTABLE_ENTRY_SIZE); + refcount_table_size2 =3D s->refcount_table_size * REFTABLE_ENTRY_SIZE; s->refcount_table =3D g_try_malloc(refcount_table_size2); =20 if (s->refcount_table_size > 0) { @@ -434,8 +434,8 @@ static int alloc_refcount_block(BlockDriverState *bs, if (refcount_table_index < s->refcount_table_size) { uint64_t data64 =3D cpu_to_be64(new_block); BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP); - ret =3D bdrv_pwrite_sync(bs->file, - s->refcount_table_offset + refcount_table_index * sizeof(uint6= 4_t), + ret =3D bdrv_pwrite_sync(bs->file, s->refcount_table_offset + + refcount_table_index * REFTABLE_ENTRY_SIZE, &data64, sizeof(data64)); if (ret < 0) { goto fail; @@ -562,8 +562,8 @@ int64_t qcow2_refcount_area(BlockDriverState *bs, uint6= 4_t start_offset, DIV_ROUND_UP(total_refblock_count, 2); } /* The qcow2 file can only store the reftable size in number of cluste= rs */ - table_size =3D ROUND_UP(table_size, s->cluster_size / sizeof(uint64_t)= ); - table_clusters =3D (table_size * sizeof(uint64_t)) / s->cluster_size; + table_size =3D ROUND_UP(table_size, s->cluster_size / REFTABLE_ENTRY_S= IZE); + table_clusters =3D (table_size * REFTABLE_ENTRY_SIZE) / s->cluster_siz= e; =20 if (table_size > QCOW_MAX_REFTABLE_SIZE) { return -EFBIG; @@ -581,13 +581,13 @@ int64_t qcow2_refcount_area(BlockDriverState *bs, uin= t64_t start_offset, if (table_size > s->max_refcount_table_index) { /* We're actually growing the reftable */ memcpy(new_table, s->refcount_table, - (s->max_refcount_table_index + 1) * sizeof(uint64_t)); + (s->max_refcount_table_index + 1) * REFTABLE_ENTRY_SIZE); } else { /* Improbable case: We're shrinking the reftable. However, the cal= ler * has assured us that there is only empty space beyond @start_off= set, * so we can simply drop all of the refblocks that won't fit into = the * new reftable. */ - memcpy(new_table, s->refcount_table, table_size * sizeof(uint64_t)= ); + memcpy(new_table, s->refcount_table, table_size * REFTABLE_ENTRY_S= IZE); } =20 if (new_refblock_offset) { @@ -682,7 +682,7 @@ int64_t qcow2_refcount_area(BlockDriverState *bs, uint6= 4_t start_offset, =20 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE); ret =3D bdrv_pwrite_sync(bs->file, table_offset, new_table, - table_size * sizeof(uint64_t)); + table_size * REFTABLE_ENTRY_SIZE); if (ret < 0) { goto fail; } @@ -717,7 +717,8 @@ int64_t qcow2_refcount_area(BlockDriverState *bs, uint6= 4_t start_offset, update_max_refcount_table_index(s); =20 /* Free old table. */ - qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint= 64_t), + qcow2_free_clusters(bs, old_table_offset, + old_table_size * REFTABLE_ENTRY_SIZE, QCOW2_DISCARD_OTHER); =20 return end_offset; @@ -1253,7 +1254,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *= bs, =20 l2_slice =3D NULL; l1_table =3D NULL; - l1_size2 =3D l1_size * sizeof(uint64_t); + l1_size2 =3D l1_size * L1E_SIZE; slice_size2 =3D s->l2_slice_size * l2_entry_size(s); n_slices =3D s->cluster_size / slice_size2; =20 @@ -1784,7 +1785,7 @@ static int check_refcounts_l1(BlockDriverState *bs, uint64_t *l1_table =3D NULL, l2_offset, l1_size2; int i, ret; =20 - l1_size2 =3D l1_size * sizeof(uint64_t); + l1_size2 =3D l1_size * L1E_SIZE; =20 /* Mark L1 table as used */ ret =3D qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_tab= le_size, @@ -2146,7 +2147,7 @@ static int calculate_refcounts(BlockDriverState *bs, = BdrvCheckResult *res, res->corruptions++; continue; } - if (sn->l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) { + if (sn->l1_size > QCOW_MAX_L1_SIZE / L1E_SIZE) { fprintf(stderr, "ERROR snapshot %s (%s) l1_size=3D%#" PRIx32 "= : " "L1 table is too large; snapshot table entry corrupted= \n", sn->id_str, sn->name, sn->l1_size); @@ -2169,7 +2170,8 @@ static int calculate_refcounts(BlockDriverState *bs, = BdrvCheckResult *res, /* refcount data */ ret =3D qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters, s->refcount_table_offset, - s->refcount_table_size * sizeof(uint64_= t)); + s->refcount_table_size * + REFTABLE_ENTRY_SIZE); if (ret < 0) { return ret; } @@ -2390,11 +2392,11 @@ write_refblocks: uint32_t old_reftable_size =3D reftable_size; uint64_t *new_on_disk_reftable; =20 - reftable_size =3D ROUND_UP((refblock_index + 1) * sizeof(uint6= 4_t), - s->cluster_size) / sizeof(uint64_t); + reftable_size =3D ROUND_UP((refblock_index + 1) * REFTABLE_ENT= RY_SIZE, + s->cluster_size) / REFTABLE_ENTRY_SIZ= E; new_on_disk_reftable =3D g_try_realloc(on_disk_reftable, reftable_size * - sizeof(uint64_t)); + REFTABLE_ENTRY_SIZE); if (!new_on_disk_reftable) { res->check_errors++; ret =3D -ENOMEM; @@ -2403,7 +2405,7 @@ write_refblocks: on_disk_reftable =3D new_on_disk_reftable; =20 memset(on_disk_reftable + old_reftable_size, 0, - (reftable_size - old_reftable_size) * sizeof(uint64_t)); + (reftable_size - old_reftable_size) * REFTABLE_ENTRY_SI= ZE); =20 /* The offset we have for the reftable is now no longer valid; * this will leak that range, but we can easily fix that by ru= nning @@ -2420,7 +2422,7 @@ write_refblocks: reftable_offset < 0) { uint64_t reftable_clusters =3D size_to_clusters(s, reftable_si= ze * - sizeof(uint64_t)= ); + REFTABLE_ENTRY_S= IZE); reftable_offset =3D alloc_clusters_imrt(bs, reftable_clusters, refcount_table, nb_clust= ers, &first_free_cluster); @@ -2460,8 +2462,8 @@ write_refblocks: uint64_t post_refblock_start, reftable_clusters; =20 post_refblock_start =3D ROUND_UP(*nb_clusters, s->refcount_block_s= ize); - reftable_clusters =3D size_to_clusters(s, - reftable_size * sizeof(uint64= _t)); + reftable_clusters =3D + size_to_clusters(s, reftable_size * REFTABLE_ENTRY_SIZE); /* Not pretty but simple */ if (first_free_cluster < post_refblock_start) { first_free_cluster =3D post_refblock_start; @@ -2485,16 +2487,16 @@ write_refblocks: } =20 ret =3D qcow2_pre_write_overlap_check(bs, 0, reftable_offset, - reftable_size * sizeof(uint64_t), + reftable_size * REFTABLE_ENTRY_SIZ= E, false); if (ret < 0) { fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret)); goto fail; } =20 - assert(reftable_size < INT_MAX / sizeof(uint64_t)); + assert(reftable_size < INT_MAX / REFTABLE_ENTRY_SIZE); ret =3D bdrv_pwrite(bs->file, reftable_offset, on_disk_reftable, - reftable_size * sizeof(uint64_t)); + reftable_size * REFTABLE_ENTRY_SIZE); if (ret < 0) { fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret)); goto fail; @@ -2503,7 +2505,7 @@ write_refblocks: /* Enter new reftable into the image header */ reftable_offset_and_clusters.reftable_offset =3D cpu_to_be64(reftable_= offset); reftable_offset_and_clusters.reftable_clusters =3D - cpu_to_be32(size_to_clusters(s, reftable_size * sizeof(uint64_t))); + cpu_to_be32(size_to_clusters(s, reftable_size * REFTABLE_ENTRY_SIZ= E)); ret =3D bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, refcount_table_offset), &reftable_offset_and_clusters, @@ -2693,14 +2695,14 @@ int qcow2_check_metadata_overlap(BlockDriverState *= bs, int ign, int64_t offset, offset =3D start_of_cluster(s, offset); =20 if ((chk & QCOW2_OL_ACTIVE_L1) && s->l1_size) { - if (overlaps_with(s->l1_table_offset, s->l1_size * sizeof(uint64_t= ))) { + if (overlaps_with(s->l1_table_offset, s->l1_size * L1E_SIZE)) { return QCOW2_OL_ACTIVE_L1; } } =20 if ((chk & QCOW2_OL_REFCOUNT_TABLE) && s->refcount_table_size) { if (overlaps_with(s->refcount_table_offset, - s->refcount_table_size * sizeof(uint64_t))) { + s->refcount_table_size * REFTABLE_ENTRY_SIZE)) { return QCOW2_OL_REFCOUNT_TABLE; } } @@ -2715,7 +2717,7 @@ int qcow2_check_metadata_overlap(BlockDriverState *bs= , int ign, int64_t offset, for (i =3D 0; i < s->nb_snapshots; i++) { if (s->snapshots[i].l1_size && overlaps_with(s->snapshots[i].l1_table_offset, - s->snapshots[i].l1_size * sizeof(uint64_t))) { + s->snapshots[i].l1_size * L1E_SIZE)) { return QCOW2_OL_INACTIVE_L1; } } @@ -2749,11 +2751,11 @@ int qcow2_check_metadata_overlap(BlockDriverState *= bs, int ign, int64_t offset, for (i =3D 0; i < s->nb_snapshots; i++) { uint64_t l1_ofs =3D s->snapshots[i].l1_table_offset; uint32_t l1_sz =3D s->snapshots[i].l1_size; - uint64_t l1_sz2 =3D l1_sz * sizeof(uint64_t); + uint64_t l1_sz2 =3D l1_sz * L1E_SIZE; uint64_t *l1; int ret; =20 - ret =3D qcow2_validate_table(bs, l1_ofs, l1_sz, sizeof(uint64_= t), + ret =3D qcow2_validate_table(bs, l1_ofs, l1_sz, L1E_SIZE, QCOW_MAX_L1_SIZE, "", NULL); if (ret < 0) { return ret; @@ -2877,8 +2879,8 @@ static int alloc_refblock(BlockDriverState *bs, uint6= 4_t **reftable, uint64_t new_reftable_size; =20 new_reftable_size =3D ROUND_UP(reftable_index + 1, - s->cluster_size / sizeof(uint64_t)); - if (new_reftable_size > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t))= { + s->cluster_size / REFTABLE_ENTRY_SIZE= ); + if (new_reftable_size > QCOW_MAX_REFTABLE_SIZE / REFTABLE_ENTRY_SI= ZE) { error_setg(errp, "This operation would make the refcount table grow " "beyond the maximum size supported by QEMU, abortin= g"); @@ -2886,14 +2888,14 @@ static int alloc_refblock(BlockDriverState *bs, uin= t64_t **reftable, } =20 new_reftable =3D g_try_realloc(*reftable, new_reftable_size * - sizeof(uint64_t)); + REFTABLE_ENTRY_SIZE); if (!new_reftable) { error_setg(errp, "Failed to increase reftable buffer size"); return -ENOMEM; } =20 memset(new_reftable + *reftable_size, 0, - (new_reftable_size - *reftable_size) * sizeof(uint64_t)); + (new_reftable_size - *reftable_size) * REFTABLE_ENTRY_SIZE); =20 *reftable =3D new_reftable; *reftable_size =3D new_reftable_size; @@ -3164,13 +3166,14 @@ int qcow2_change_refcount_order(BlockDriverState *b= s, int refcount_order, =20 if (new_allocation) { if (new_reftable_offset) { - qcow2_free_clusters(bs, new_reftable_offset, - allocated_reftable_size * sizeof(uint6= 4_t), - QCOW2_DISCARD_NEVER); + qcow2_free_clusters( + bs, new_reftable_offset, + allocated_reftable_size * REFTABLE_ENTRY_SIZE, + QCOW2_DISCARD_NEVER); } =20 new_reftable_offset =3D qcow2_alloc_clusters(bs, new_reftable_= size * - sizeof(uint64_t= )); + REFTABLE_ENTRY_= SIZE); if (new_reftable_offset < 0) { error_setg_errno(errp, -new_reftable_offset, "Failed to allocate the new reftable"); @@ -3196,7 +3199,7 @@ int qcow2_change_refcount_order(BlockDriverState *bs,= int refcount_order, =20 /* Write the new reftable */ ret =3D qcow2_pre_write_overlap_check(bs, 0, new_reftable_offset, - new_reftable_size * sizeof(uint64_= t), + new_reftable_size * REFTABLE_ENTRY= _SIZE, false); if (ret < 0) { error_setg_errno(errp, -ret, "Overlap check failed"); @@ -3208,7 +3211,7 @@ int qcow2_change_refcount_order(BlockDriverState *bs,= int refcount_order, } =20 ret =3D bdrv_pwrite(bs->file, new_reftable_offset, new_reftable, - new_reftable_size * sizeof(uint64_t)); + new_reftable_size * REFTABLE_ENTRY_SIZE); =20 for (i =3D 0; i < new_reftable_size; i++) { be64_to_cpus(&new_reftable[i]); @@ -3285,7 +3288,7 @@ done: =20 if (new_reftable_offset > 0) { qcow2_free_clusters(bs, new_reftable_offset, - new_reftable_size * sizeof(uint64_t), + new_reftable_size * REFTABLE_ENTRY_SIZE, QCOW2_DISCARD_OTHER); } } @@ -3374,7 +3377,7 @@ int qcow2_shrink_reftable(BlockDriverState *bs) { BDRVQcow2State *s =3D bs->opaque; uint64_t *reftable_tmp =3D - g_malloc(s->refcount_table_size * sizeof(uint64_t)); + g_malloc(s->refcount_table_size * REFTABLE_ENTRY_SIZE); int i, ret; =20 for (i =3D 0; i < s->refcount_table_size; i++) { @@ -3412,7 +3415,7 @@ int qcow2_shrink_reftable(BlockDriverState *bs) } =20 ret =3D bdrv_pwrite_sync(bs->file, s->refcount_table_offset, reftable_= tmp, - s->refcount_table_size * sizeof(uint64_t)); + s->refcount_table_size * REFTABLE_ENTRY_SIZE); /* * If the write in the reftable failed the image may contain a partial= ly * overwritten reftable. In this case it would be better to clear the diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 2756b37d24..9b68690f56 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -659,7 +659,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSna= pshotInfo *sn_info) sn->extra_data_size =3D sizeof(QCowSnapshotExtraData); =20 /* Allocate the L1 table of the snapshot and copy the current one ther= e. */ - l1_table_offset =3D qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint6= 4_t)); + l1_table_offset =3D qcow2_alloc_clusters(bs, s->l1_size * L1E_SIZE); if (l1_table_offset < 0) { ret =3D l1_table_offset; goto fail; @@ -679,13 +679,13 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUS= napshotInfo *sn_info) } =20 ret =3D qcow2_pre_write_overlap_check(bs, 0, sn->l1_table_offset, - s->l1_size * sizeof(uint64_t), fal= se); + s->l1_size * L1E_SIZE, false); if (ret < 0) { goto fail; } =20 ret =3D bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table, - s->l1_size * sizeof(uint64_t)); + s->l1_size * L1E_SIZE); if (ret < 0) { goto fail; } @@ -768,7 +768,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const cha= r *snapshot_id) sn =3D &s->snapshots[snapshot_index]; =20 ret =3D qcow2_validate_table(bs, sn->l1_table_offset, sn->l1_size, - sizeof(uint64_t), QCOW_MAX_L1_SIZE, + L1E_SIZE, QCOW_MAX_L1_SIZE, "Snapshot L1 table", &local_err); if (ret < 0) { error_report_err(local_err); @@ -803,8 +803,8 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const cha= r *snapshot_id) goto fail; } =20 - cur_l1_bytes =3D s->l1_size * sizeof(uint64_t); - sn_l1_bytes =3D sn->l1_size * sizeof(uint64_t); + cur_l1_bytes =3D s->l1_size * L1E_SIZE; + sn_l1_bytes =3D sn->l1_size * L1E_SIZE; =20 /* * Copy the snapshot L1 table to the current L1 table. @@ -917,7 +917,7 @@ int qcow2_snapshot_delete(BlockDriverState *bs, sn =3D s->snapshots[snapshot_index]; =20 ret =3D qcow2_validate_table(bs, sn.l1_table_offset, sn.l1_size, - sizeof(uint64_t), QCOW_MAX_L1_SIZE, + L1E_SIZE, QCOW_MAX_L1_SIZE, "Snapshot L1 table", errp); if (ret < 0) { return ret; @@ -953,7 +953,7 @@ int qcow2_snapshot_delete(BlockDriverState *bs, error_setg_errno(errp, -ret, "Failed to free the cluster and L1 ta= ble"); return ret; } - qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64= _t), + qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * L1E_SIZE, QCOW2_DISCARD_SNAPSHOT); =20 /* must update the copied flag on the current cluster offsets */ @@ -1030,12 +1030,12 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs, =20 /* Allocate and read in the snapshot's L1 table */ ret =3D qcow2_validate_table(bs, sn->l1_table_offset, sn->l1_size, - sizeof(uint64_t), QCOW_MAX_L1_SIZE, + L1E_SIZE, QCOW_MAX_L1_SIZE, "Snapshot L1 table", errp); if (ret < 0) { return ret; } - new_l1_bytes =3D sn->l1_size * sizeof(uint64_t); + new_l1_bytes =3D sn->l1_size * L1E_SIZE; new_l1_table =3D qemu_try_blockalign(bs->file->bs, new_l1_bytes); if (new_l1_table =3D=3D NULL) { return -ENOMEM; diff --git a/block/qcow2.c b/block/qcow2.c index da56b1a4df..ef9a45e82f 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1543,7 +1543,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverStat= e *bs, QDict *options, =20 /* read the level 1 table */ ret =3D qcow2_validate_table(bs, header.l1_table_offset, - header.l1_size, sizeof(uint64_t), + header.l1_size, L1E_SIZE, QCOW_MAX_L1_SIZE, "Active L1 table", errp); if (ret < 0) { goto fail; @@ -1568,15 +1568,14 @@ static int coroutine_fn qcow2_do_open(BlockDriverSt= ate *bs, QDict *options, } =20 if (s->l1_size > 0) { - s->l1_table =3D qemu_try_blockalign(bs->file->bs, - s->l1_size * sizeof(uint64_t)); + s->l1_table =3D qemu_try_blockalign(bs->file->bs, s->l1_size * L1E= _SIZE); if (s->l1_table =3D=3D NULL) { error_setg(errp, "Could not allocate L1 table"); ret =3D -ENOMEM; goto fail; } ret =3D bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, - s->l1_size * sizeof(uint64_t)); + s->l1_size * L1E_SIZE); if (ret < 0) { error_setg_errno(errp, -ret, "Could not read L1 table"); goto fail; @@ -3213,7 +3212,7 @@ int64_t qcow2_refcount_metadata_size(int64_t clusters= , size_t cluster_size, * where no further refcount blocks or table clusters are required to * reference count every cluster. */ - int64_t blocks_per_table_cluster =3D cluster_size / sizeof(uint64_t); + int64_t blocks_per_table_cluster =3D cluster_size / REFTABLE_ENTRY_SIZ= E; int64_t refcounts_per_block =3D cluster_size * 8 / (1 << refcount_orde= r); int64_t table =3D 0; /* number of refcount table clusters */ int64_t blocks =3D 0; /* number of refcount block clusters */ @@ -3270,8 +3269,8 @@ static int64_t qcow2_calc_prealloc_size(int64_t total= _size, =20 /* total size of L1 tables */ nl1e =3D nl2e * l2e_size / cluster_size; - nl1e =3D ROUND_UP(nl1e, cluster_size / sizeof(uint64_t)); - meta_size +=3D nl1e * sizeof(uint64_t); + nl1e =3D ROUND_UP(nl1e, cluster_size / L1E_SIZE); + meta_size +=3D nl1e * L1E_SIZE; =20 /* total size of refcount table and blocks */ meta_size +=3D qcow2_refcount_metadata_size( @@ -4460,7 +4459,7 @@ static int coroutine_fn qcow2_co_truncate(BlockDriver= State *bs, int64_t offset, /* write updated header.size */ offset =3D cpu_to_be64(offset); ret =3D bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), - &offset, sizeof(uint64_t)); + &offset, sizeof(offset)); if (ret < 0) { error_setg_errno(errp, -ret, "Failed to update the image size"); goto fail; @@ -4700,8 +4699,8 @@ static int make_completely_empty(BlockDriverState *bs) =20 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE); =20 - l1_clusters =3D DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint= 64_t)); - l1_size2 =3D (uint64_t)s->l1_size * sizeof(uint64_t); + l1_clusters =3D DIV_ROUND_UP(s->l1_size, s->cluster_size / L1E_SIZE); + l1_size2 =3D (uint64_t)s->l1_size * L1E_SIZE; =20 /* After this call, neither the in-memory nor the on-disk refcount * information accurately describe the actual references */ @@ -4747,14 +4746,14 @@ static int make_completely_empty(BlockDriverState *= bs) =20 s->l1_table_offset =3D 3 * s->cluster_size; =20 - new_reftable =3D g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_= t)); + new_reftable =3D g_try_new0(uint64_t, s->cluster_size / REFTABLE_ENTRY= _SIZE); if (!new_reftable) { ret =3D -ENOMEM; goto fail_broken_refcounts; } =20 s->refcount_table_offset =3D s->cluster_size; - s->refcount_table_size =3D s->cluster_size / sizeof(uint64_t); + s->refcount_table_size =3D s->cluster_size / REFTABLE_ENTRY_SIZE; s->max_refcount_table_index =3D 0; =20 g_free(s->refcount_table); @@ -4826,7 +4825,7 @@ static int qcow2_make_empty(BlockDriverState *bs) int step =3D QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size); int l1_clusters, ret =3D 0; =20 - l1_clusters =3D DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint= 64_t)); + l1_clusters =3D DIV_ROUND_UP(s->l1_size, s->cluster_size / L1E_SIZE); =20 if (s->qcow_version >=3D 3 && !s->snapshots && !s->nb_bitmaps && 3 + l1_clusters <=3D s->refcount_block_size && @@ -4957,7 +4956,7 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts= , BlockDriverState *in_bs, l2e_size =3D extended_l2 ? L2E_SIZE_EXTENDED : L2E_SIZE_NORMAL; l2_tables =3D DIV_ROUND_UP(virtual_size / cluster_size, cluster_size / l2e_size); - if (l2_tables * sizeof(uint64_t) > QCOW_MAX_L1_SIZE) { + if (l2_tables * L1E_SIZE > QCOW_MAX_L1_SIZE) { error_setg(&local_err, "The image size is too large " "(try using a larger cluster size)"); goto err; --=20 2.20.1