From nobody Wed Dec 17 21:48:18 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1631730490769228.80938456832075; Wed, 15 Sep 2021 11:28:10 -0700 (PDT) Received: from localhost ([::1]:43250 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mQZdh-0004aK-J0 for importer@patchew.org; Wed, 15 Sep 2021 14:28:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52382) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mQZ77-0007pI-Np for qemu-devel@nongnu.org; Wed, 15 Sep 2021 13:54:29 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:59307) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mQZ75-0007HM-T4 for qemu-devel@nongnu.org; Wed, 15 Sep 2021 13:54:29 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-121-bEWG2NzAOqqA6cJg5DKalA-1; Wed, 15 Sep 2021 13:54:23 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 962BD8064A5; Wed, 15 Sep 2021 17:54:22 +0000 (UTC) Received: from localhost (unknown [10.39.192.101]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3AC9110074FD; Wed, 15 Sep 2021 17:54:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1631728467; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BZQPTEYCOY/RoJosPIYDTQ+iWISPbSjxflDa2pjzPDU=; b=Ra0DHcBolgCVJitOLqd9a6TOzypW6fM9HYKfzkUz4VHgBp127poURescIK1H2BgMCCdRGt B+Pi5CKq0s3xg/aioWJfqsTJ02OpNH9B5DhZvLUVPzsvi7Spw/FV0T5DN6V/BlTF+kktI9 W41EVnqcU5Bi6d6En+iTyMPYojKweu4= X-MC-Unique: bEWG2NzAOqqA6cJg5DKalA-1 From: Hanna Reitz To: qemu-block@nongnu.org Subject: [PULL 29/32] qcow2-refcount: improve style of check_refcounts_l1() Date: Wed, 15 Sep 2021 19:53:15 +0200 Message-Id: <20210915175318.853225-30-hreitz@redhat.com> In-Reply-To: <20210915175318.853225-1-hreitz@redhat.com> References: <20210915175318.853225-1-hreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=hreitz@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=216.205.24.124; envelope-from=hreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.39, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 , Peter Maydell , Hanna Reitz , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1631730493289100001 Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy - 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 Reviewed-by: Hanna Reitz Message-Id: <20210914122454.141075-9-vsementsov@virtuozzo.com> Signed-off-by: Hanna Reitz --- block/qcow2-refcount.c | 98 +++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index bdac7b1780..3c89e09599 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1862,71 +1862,73 @@ static int check_refcounts_l1(BlockDriverState *bs, int flags, BdrvCheckMode fix, bool active) { BDRVQcow2State *s =3D bs->opaque; - uint64_t *l1_table =3D NULL, l2_offset, l1_size2; + size_t l1_size_bytes =3D l1_size * L1E_SIZE; + g_autofree uint64_t *l1_table =3D NULL; + uint64_t l2_offset; int i, ret; =20 - l1_size2 =3D l1_size * L1E_SIZE; + if (!l1_size) { + return 0; + } =20 /* Mark L1 table as used */ ret =3D qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_tab= le_size, - l1_table_offset, l1_size2); + l1_table_offset, l1_size_bytes); if (ret < 0) { - goto fail; + return ret; + } + + l1_table =3D g_try_malloc(l1_size_bytes); + if (l1_table =3D=3D NULL) { + res->check_errors++; + return -ENOMEM; } =20 /* Read L1 table entries from disk */ - if (l1_size2 > 0) { - l1_table =3D g_try_malloc(l1_size2); - if (l1_table =3D=3D NULL) { - ret =3D -ENOMEM; - res->check_errors++; - goto fail; - } - ret =3D bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size2); - if (ret < 0) { - fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n"); - res->check_errors++; - goto fail; - } - for(i =3D 0;i < l1_size; i++) - be64_to_cpus(&l1_table[i]); + ret =3D bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size_bytes); + if (ret < 0) { + fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n"); + res->check_errors++; + return ret; + } + + for (i =3D 0; i < l1_size; i++) { + be64_to_cpus(&l1_table[i]); } =20 /* Do the actual checks */ - for(i =3D 0; i < l1_size; i++) { - l2_offset =3D l1_table[i]; - if (l2_offset) { - /* Mark L2 table as used */ - l2_offset &=3D L1E_OFFSET_MASK; - ret =3D qcow2_inc_refcounts_imrt(bs, res, - refcount_table, refcount_table_= size, - l2_offset, s->cluster_size); - if (ret < 0) { - goto fail; - } + for (i =3D 0; i < l1_size; i++) { + if (!l1_table[i]) { + continue; + } =20 - /* L2 tables are cluster aligned */ - if (offset_into_cluster(s, l2_offset)) { - fprintf(stderr, "ERROR l2_offset=3D%" PRIx64 ": Table is n= ot " - "cluster aligned; L1 entry corrupted\n", l2_offset); - res->corruptions++; - } + l2_offset =3D l1_table[i] & L1E_OFFSET_MASK; =20 - /* Process and check L2 entries */ - ret =3D check_refcounts_l2(bs, res, refcount_table, - refcount_table_size, l2_offset, flags, - fix, active); - if (ret < 0) { - goto fail; - } + /* Mark L2 table as used */ + ret =3D qcow2_inc_refcounts_imrt(bs, res, + refcount_table, refcount_table_size, + l2_offset, s->cluster_size); + if (ret < 0) { + return ret; + } + + /* L2 tables are cluster aligned */ + if (offset_into_cluster(s, l2_offset)) { + fprintf(stderr, "ERROR l2_offset=3D%" PRIx64 ": Table is not " + "cluster aligned; L1 entry corrupted\n", l2_offset); + res->corruptions++; + } + + /* Process and check L2 entries */ + ret =3D check_refcounts_l2(bs, res, refcount_table, + refcount_table_size, l2_offset, flags, + fix, active); + if (ret < 0) { + return ret; } } - g_free(l1_table); - return 0; =20 -fail: - g_free(l1_table); - return ret; + return 0; } =20 /* --=20 2.31.1