From patchwork Tue Sep 14 12:24:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vladimir Sementsov-Ogievskiy X-Patchwork-Id: 12493017 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 779EFC433EF for ; Tue, 14 Sep 2021 12:28:36 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BDCBB60F24 for ; Tue, 14 Sep 2021 12:28:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BDCBB60F24 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=virtuozzo.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:43364 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mQ7YA-0004oh-UU for qemu-devel@archiver.kernel.org; Tue, 14 Sep 2021 08:28:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55678) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mQ7V9-0001mi-NX; Tue, 14 Sep 2021 08:25:30 -0400 Received: from mail-eopbgr50116.outbound.protection.outlook.com ([40.107.5.116]:58241 helo=EUR03-VE1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mQ7V7-00065R-PD; Tue, 14 Sep 2021 08:25:27 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UegPbjcE3eLbBfTMVpcpNkvgmnFnyWkUDh/ZjTGcClu3szChMcVlZ81PwfWQdrCnbElfQE8GXAFWZVGXfTB7fcoPD+Vb3zOjzoMa0jF6RQu95CoPc9oyCutfftF7CfEorMEUVqPAz1i1zMsGvhM9+R6v+wV1hXglKPcXkD8uAUXs+XKUN6mIR0LB1Fqk8BK6ipwp5pOwSPEHMLktEqokmTMvwt8wnLC7LMUD4PqgGP+lpokii5Lr+a8pptWmUzp9C+uq9L9EmPns4fH9aXha+RCB6tZHLBCNARmN3AJyA0F/tNyRvZmOS9NTLu2+OzFIeftQOHsrIgaHB/cGXH/xtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=KWpJEs2ax9Fm5ZtED2XL1ytdm+DemfPtHxDHmT+2ZtI=; b=Qj4RosI/tH3n7VnMpETbTPB0KjAB53aGagIGpydMQBZjbOYWdW8LEbqSqMeMeSIYVHatNaz7kp8fxab9zvEXz/xLl3i5ZiyZNRsQqDpQK9ePrZZDb7X6mfRnWbBid3KXLSGGjiukOVYV+dVuoHLtvFUeYNvfWl5F/vKEPbAOzwJVw1vTAbJK/k1xapXOYbc4SoueQa6/ZFbRIh0bCbP83eDYVSeBacYSKRP0Hp5yekhDcZgJnBpu0fG3PARJfFY6oseMmtku5TQNQbjEv3UXIiP5eMr7TCtBrjEuR9rZJoc0VW7Zb49yhoeB4fMOfsZGPktAYjawc++Vkq9fI04FvA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=virtuozzo.com; dmarc=pass action=none header.from=virtuozzo.com; dkim=pass header.d=virtuozzo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KWpJEs2ax9Fm5ZtED2XL1ytdm+DemfPtHxDHmT+2ZtI=; b=kD8EarMCbM1b/VLlvbLgowEtZj3j4GN2y3dBjc27VOVxpcrfcwMIadPsZEtglTQ+AcXTfCO+B8MJ3UkRb6Sr3o5O0V7kFkN8ctSGBrX41QYq6Xqg/Rw1EsLU9d8b1uAbq3jfE36w6444qDOFxuaT+YRu/LTiDqhlR50xKssQIn8= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none;nongnu.org; dmarc=none action=none header.from=virtuozzo.com; Received: from AM7PR08MB5494.eurprd08.prod.outlook.com (2603:10a6:20b:dc::15) by AM6PR08MB3158.eurprd08.prod.outlook.com (2603:10a6:209:44::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.17; Tue, 14 Sep 2021 12:25:21 +0000 Received: from AM7PR08MB5494.eurprd08.prod.outlook.com ([fe80::2817:53b3:f8b4:fe22]) by AM7PR08MB5494.eurprd08.prod.outlook.com ([fe80::2817:53b3:f8b4:fe22%9]) with mapi id 15.20.4500.019; Tue, 14 Sep 2021 12:25:21 +0000 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, hreitz@redhat.com, kwolf@redhat.com, den@openvz.org, ktkhai@virtuozzo.com, eblake@redhat.com, berto@igalia.com, vsementsov@virtuozzo.com Subject: [PATCH v4 04/10] qcow2-refcount: introduce fix_l2_entry_by_zero() Date: Tue, 14 Sep 2021 15:24:48 +0300 Message-Id: <20210914122454.141075-5-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210914122454.141075-1-vsementsov@virtuozzo.com> References: <20210914122454.141075-1-vsementsov@virtuozzo.com> X-ClientProxiedBy: HE1P18901CA0009.EURP189.PROD.OUTLOOK.COM (2603:10a6:3:8b::19) To AM7PR08MB5494.eurprd08.prod.outlook.com (2603:10a6:20b:dc::15) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (185.215.60.196) by HE1P18901CA0009.EURP189.PROD.OUTLOOK.COM (2603:10a6:3:8b::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.14 via Frontend Transport; Tue, 14 Sep 2021 12:25:20 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e58293b7-2648-4c6a-4af0-08d9777ab884 X-MS-TrafficTypeDiagnostic: AM6PR08MB3158: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4941; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JIPvY4zhKN1IbTahf9oJGYsxSf+1jCvz0VEypyaSId+HrtBhyWG//r5CNIAZi2e9moLhlEFT1R8LgkdXpn7B78AXTWy/jPwO0WfHGHSVUwAXa2+P1cAFFpxLKFeiPgaQUZclTEAjdHk6S+DF+1VMiNQyhmOMZv2rJTV/eP0ECzIirp2HqY900uEjHFKDkagJ/zJg5+qKXE4dzsEsKniCq8mboQ7upFqv1bFF2iiWQQSnaTTEEZ3l/zfEwetQuEM5kkx1G4d8jNUtdEMhxorRQh2O4uGD+8ULt22M585y+Y8P8LidIOChDab5XE7rfnT3cj/2isXFkm9/rZht7cpCAY47fxaPSin6Xzm/yItnU9p0nHWmQBXU2YGuwGPi/Gx5+pWJwZ50L5woyJEpHMdVxxgNBooENWxeEeUHalxMBqwbEQTZAk7KFGx8ItxHzKmeO3eSbwW/HeC/ElHeLt+k+Y5ukptqD24Ea2LL211TSruzX3jBu/N8xP8CMDNbt+bWgtzsyvxlPB7Bik4/26/kLaCExu66Fm1+tg3jiFR5q52FHaPSHpFVsn46dldfpTMG3EjFeQzBzM2SwxV3AfhzFJ9wP7+Anl/pxjoXhakKI84CtU3Yhk49kXybyIJbZxWLTKPL80ibKlwk2c232Omv0HE3v5mC+FbgDAdFsi3lHqrVQe/s+3yd3FQ1IyR5Gje+58CkGS33kpUFMFiqr2fMlw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM7PR08MB5494.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(136003)(396003)(376002)(346002)(39850400004)(366004)(26005)(186003)(6506007)(478600001)(36756003)(38100700002)(38350700002)(86362001)(8936002)(1076003)(2616005)(316002)(4326008)(6512007)(107886003)(83380400001)(6486002)(2906002)(956004)(6666004)(66476007)(52116002)(66556008)(8676002)(6916009)(66946007)(5660300002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: T9ynxrHSGiCBeJ26KytEkRYiKFYKamdc3f6iL/934yrEvUvxXZgFN4jtEVNtRhaX5nByPzskx/Im7p5B2Y/tJI2BmdVhhgapnGl5yMyJQh4Trd0UXVGPZbFEhT7Vgp1h7067gZp234wt31Xdf1pZSLWL7+1ZTbHmIagbENDtv2VBpb67t6NQg+VyYXiBeuu9K7HpkjU7ODlH3GyBTCGbK1fDcCsUJL7jt0/2DWsbcVFboYWlQJSgeYxDVEV2S9QJlRRXWo5nahMlvqlbmxaLCvkIhg0b5DXIQGwlvcrxF9cK4AwPFHbEwizd1g8DpHwRHVPS4BS4gfS3Z/K/hvASQLtnd3Cwr9IteJebtfDMg8Ef1pt/MviN9uIp3XVm+bgW/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PR08MB5494.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2021 12:25:21.7408 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: qJPsrDMbrJHeFvjD/W6JI2bav1H2SttajSxRGwEEUJkg2vimipZrd2dUwJVgBVOCJnPcC1sxNB1ZFqu/W/5YCu8aQFou54Tn68lT0yXt/+U= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB3158 Received-SPF: pass client-ip=40.107.5.116; envelope-from=vsementsov@virtuozzo.com; helo=EUR03-VE1-obe.outbound.protection.outlook.com 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, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-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: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Split fix_l2_entry_by_zero() out of check_refcounts_l2() to be reused in further patch. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Reviewed-by: Hanna Reitz --- block/qcow2-refcount.c | 87 +++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 27 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 66cbb94ef9..184b96ad63 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1587,6 +1587,54 @@ enum { CHECK_FRAG_INFO = 0x2, /* update BlockFragInfo counters */ }; +/* + * Fix L2 entry by making it QCOW2_CLUSTER_ZERO_PLAIN. + * + * This function decrements res->corruptions on success, so the caller is + * responsible to increment res->corruptions prior to the call. + * + * On failure in-memory @l2_table may be modified. + */ +static int fix_l2_entry_by_zero(BlockDriverState *bs, BdrvCheckResult *res, + uint64_t l2_offset, + uint64_t *l2_table, int l2_index, bool active, + bool *metadata_overlap) +{ + BDRVQcow2State *s = bs->opaque; + int ret; + int idx = l2_index * (l2_entry_size(s) / sizeof(uint64_t)); + uint64_t l2e_offset = l2_offset + (uint64_t)l2_index * l2_entry_size(s); + int ign = active ? QCOW2_OL_ACTIVE_L2 : QCOW2_OL_INACTIVE_L2; + uint64_t l2_entry = has_subclusters(s) ? 0 : QCOW_OFLAG_ZERO; + + set_l2_entry(s, l2_table, l2_index, l2_entry); + ret = qcow2_pre_write_overlap_check(bs, ign, l2e_offset, l2_entry_size(s), + false); + if (metadata_overlap) { + *metadata_overlap = ret < 0; + } + if (ret < 0) { + fprintf(stderr, "ERROR: Overlap check failed\n"); + goto fail; + } + + ret = bdrv_pwrite_sync(bs->file, l2e_offset, &l2_table[idx], + l2_entry_size(s)); + if (ret < 0) { + fprintf(stderr, "ERROR: Failed to overwrite L2 " + "table entry: %s\n", strerror(-ret)); + goto fail; + } + + res->corruptions--; + res->corruptions_fixed++; + return 0; + +fail: + res->check_errors++; + return ret; +} + /* * Increases the refcount in the given refcount table for the all clusters * referenced in the L2 table. While doing so, performs some checks on L2 @@ -1606,6 +1654,7 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, int i, ret; size_t l2_size_bytes = s->l2_size * l2_entry_size(s); g_autofree uint64_t *l2_table = g_malloc(l2_size_bytes); + bool metadata_overlap; /* Read L2 table from disk */ ret = bdrv_pread(bs->file, l2_offset, l2_table, l2_size_bytes); @@ -1685,19 +1734,10 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", offset); if (fix & BDRV_FIX_ERRORS) { - int idx = i * (l2_entry_size(s) / sizeof(uint64_t)); - uint64_t l2e_offset = - l2_offset + (uint64_t)i * l2_entry_size(s); - int ign = active ? QCOW2_OL_ACTIVE_L2 : - QCOW2_OL_INACTIVE_L2; - - l2_entry = has_subclusters(s) ? 0 : QCOW_OFLAG_ZERO; - set_l2_entry(s, l2_table, i, l2_entry); - ret = qcow2_pre_write_overlap_check(bs, ign, - l2e_offset, l2_entry_size(s), false); - if (ret < 0) { - fprintf(stderr, "ERROR: Overlap check failed\n"); - res->check_errors++; + ret = fix_l2_entry_by_zero(bs, res, l2_offset, + l2_table, i, active, + &metadata_overlap); + if (metadata_overlap) { /* * Something is seriously wrong, so abort checking * this L2 table. @@ -1705,26 +1745,19 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, return ret; } - ret = bdrv_pwrite_sync(bs->file, l2e_offset, - &l2_table[idx], - l2_entry_size(s)); - if (ret < 0) { - fprintf(stderr, "ERROR: Failed to overwrite L2 " - "table entry: %s\n", strerror(-ret)); - res->check_errors++; - /* - * Do not abort, continue checking the rest of this - * L2 table's entries. - */ - } else { - res->corruptions--; - res->corruptions_fixed++; + if (ret == 0) { /* * Skip marking the cluster as used * (it is unused now). */ continue; } + + /* + * Failed to fix. + * Do not abort, continue checking the rest of this + * L2 table's entries. + */ } } else { fprintf(stderr, "ERROR offset=%" PRIx64 ": Data cluster is "