Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp2422441rwi; Tue, 11 Oct 2022 08:34:58 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4eN5gWYft1KA7hXySFiUFOTdbX2+uYbpYNI+8VkGdrF5/SZy9mfLzJxbZ3g5k+BAbDKOMZ X-Received: by 2002:a63:c1f:0:b0:41a:9b73:a89e with SMTP id b31-20020a630c1f000000b0041a9b73a89emr21247299pgl.342.1665502497907; Tue, 11 Oct 2022 08:34:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665502497; cv=none; d=google.com; s=arc-20160816; b=Dl9XmUiU+1xnq3kXO6Iq1UAVkYR0uNMl84+Bwko+OBe2jfz8LIHOhxgIKPmtlNANpV n2h4ZecISO6GbBLY+asThMfTWBdNRmk6VDvnwHjIRsmWsRrKlQNr+3DjN7jJmGR1kf9W Ytn5MBNQIXUvqP5LugsPGmIjMCL96m1C01s0J7jsS6l2fWH5haOnByoXRyMlLsUOBTzN zx6ZQXH4pxf0NIL+gcRk1qIux3gBi2dZzi7B/iVtBVhbqr/MK1D14xBcb/qtGAuz5LmK F7j4y+znnLdsKp+GNgXAktRR8JgmObQ6rtzwDvBL+figZm7608722DSBgPkd0YnH/oIS zR5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SL3I5WekqbkgBSZdN8GbXtK2fwiYQbEoPatA3eBYnTQ=; b=ozSP25VdtnWrs+ac6BD8TiU/uODdWF68He1Ge9/wAQQQcG/9VElOr97b02zhvLewpy Z3IbWQTFxeIsN3t17CGlaKTZnT0AtjoyCh5PqtJLKQtw9M+IW4seqi8tTNBGdx2Nv0Wn DPikq78sU0y7GgFKc/HMKspQLSV4dmzAIfs3dKPqsqM/H5mDvldVYgTCDpEYs6pvDQ2/ KuNRueoEH/p0TVU4q+P/UZirIYXuqtUijAR+GZ7YwWxhH4dECykFo8IByxUcJtGBLUfl za1h6cAQL6UhRUI5X0whE5kHhgGxYmnFxY8d9sjnW+wdTVKMzPe9DSLhhgvfcbTwGwGy Hoew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rUBG4PX6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d17-20020a056a00199100b00556c1c66b61si7906825pfl.143.2022.10.11.08.34.33; Tue, 11 Oct 2022 08:34:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rUBG4PX6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230226AbiJKOxq (ORCPT + 99 others); Tue, 11 Oct 2022 10:53:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230200AbiJKOw1 (ORCPT ); Tue, 11 Oct 2022 10:52:27 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDBEA816AC; Tue, 11 Oct 2022 07:51:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 04CCC611C3; Tue, 11 Oct 2022 14:51:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85ED6C43141; Tue, 11 Oct 2022 14:51:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665499866; bh=qUjyoVvOS3Ad4+C90cegODPyGWIqsREsIRdqhCf699w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rUBG4PX6kX5/bqjqGoi2A0fCpkoXzO1tK92vJ43YT91jTyqhzOZVTdgAFVL6O0+07 70hdnSMx27TUtK+m/RboPq7OOhI2HigOhlbIuo3YNMGag9uGWhSB5Se7em+dGLzl5Y zeqrvMW5b2ictfoaB/meDZnnBnhKi4n5tq8KieYe5JODDlCLw4x7mOL/BHvDC4Y3ia epvi7MRXqrFUhBH/BvqJug8/aFXB7DEL+UouNwi6t5jmtUzchbHzxcp4FDQaprObOD GhlTP6xeGDpgUAbUTlq87ytxQDOjFqs4zI6avn6eFrf/wONrsgGj/XpKFFUxrCGp5i tRv7qbyFxxTPg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Qu Wenruo , David Sterba , Sasha Levin , clm@fb.com, josef@toxicpanda.com, linux-btrfs@vger.kernel.org Subject: [PATCH AUTOSEL 6.0 32/46] btrfs: scrub: try to fix super block errors Date: Tue, 11 Oct 2022 10:50:00 -0400 Message-Id: <20221011145015.1622882-32-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221011145015.1622882-1-sashal@kernel.org> References: <20221011145015.1622882-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo [ Upstream commit f9eab5f0bba76742af654f33d517bf62a0db8f12 ] [BUG] The following script shows that, although scrub can detect super block errors, it never tries to fix it: mkfs.btrfs -f -d raid1 -m raid1 $dev1 $dev2 xfs_io -c "pwrite 67108864 4k" $dev2 mount $dev1 $mnt btrfs scrub start -B $dev2 btrfs scrub start -Br $dev2 umount $mnt The first scrub reports the super error correctly: scrub done for f3289218-abd3-41ac-a630-202f766c0859 Scrub started: Tue Aug 2 14:44:11 2022 Status: finished Duration: 0:00:00 Total to scrub: 1.26GiB Rate: 0.00B/s Error summary: super=1 Corrected: 0 Uncorrectable: 0 Unverified: 0 But the second read-only scrub still reports the same super error: Scrub started: Tue Aug 2 14:44:11 2022 Status: finished Duration: 0:00:00 Total to scrub: 1.26GiB Rate: 0.00B/s Error summary: super=1 Corrected: 0 Uncorrectable: 0 Unverified: 0 [CAUSE] The comments already shows that super block can be easily fixed by committing a transaction: /* * If we find an error in a super block, we just report it. * They will get written with the next transaction commit * anyway */ But the truth is, such assumption is not always true, and since scrub should try to repair every error it found (except for read-only scrub), we should really actively commit a transaction to fix this. [FIX] Just commit a transaction if we found any super block errors, after everything else is done. We cannot do this just after scrub_supers(), as btrfs_commit_transaction() will try to pause and wait for the running scrub, thus we can not call it with scrub_lock hold. Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/scrub.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c index 0fe7c4882e1f..7d9b09e3ca70 100644 --- a/fs/btrfs/scrub.c +++ b/fs/btrfs/scrub.c @@ -4093,6 +4093,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, int ret; struct btrfs_device *dev; unsigned int nofs_flag; + bool need_commit = false; if (btrfs_fs_closing(fs_info)) return -EAGAIN; @@ -4196,6 +4197,12 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, */ nofs_flag = memalloc_nofs_save(); if (!is_dev_replace) { + u64 old_super_errors; + + spin_lock(&sctx->stat_lock); + old_super_errors = sctx->stat.super_errors; + spin_unlock(&sctx->stat_lock); + btrfs_info(fs_info, "scrub: started on devid %llu", devid); /* * by holding device list mutex, we can @@ -4204,6 +4211,16 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, mutex_lock(&fs_info->fs_devices->device_list_mutex); ret = scrub_supers(sctx, dev); mutex_unlock(&fs_info->fs_devices->device_list_mutex); + + spin_lock(&sctx->stat_lock); + /* + * Super block errors found, but we can not commit transaction + * at current context, since btrfs_commit_transaction() needs + * to pause the current running scrub (hold by ourselves). + */ + if (sctx->stat.super_errors > old_super_errors && !sctx->readonly) + need_commit = true; + spin_unlock(&sctx->stat_lock); } if (!ret) @@ -4230,6 +4247,25 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, scrub_workers_put(fs_info); scrub_put_ctx(sctx); + /* + * We found some super block errors before, now try to force a + * transaction commit, as scrub has finished. + */ + if (need_commit) { + struct btrfs_trans_handle *trans; + + trans = btrfs_start_transaction(fs_info->tree_root, 0); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + btrfs_err(fs_info, + "scrub: failed to start transaction to fix super block errors: %d", ret); + return ret; + } + ret = btrfs_commit_transaction(trans); + if (ret < 0) + btrfs_err(fs_info, + "scrub: failed to commit transaction to fix super block errors: %d", ret); + } return ret; out: scrub_workers_put(fs_info); -- 2.35.1