From mboxrd@z Thu Jan 1 00:00:00 1970 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=-9.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,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 9AAAAC04AB4 for ; Fri, 17 May 2019 14:00:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5CE1720873 for ; Fri, 17 May 2019 14:00:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728791AbfEQOAN (ORCPT ); Fri, 17 May 2019 10:00:13 -0400 Received: from mx2.suse.de ([195.135.220.15]:53642 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728351AbfEQOAN (ORCPT ); Fri, 17 May 2019 10:00:13 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 619BAAF59 for ; Fri, 17 May 2019 14:00:12 +0000 (UTC) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH] btrfs-progs: check/lowmem: Reset path in repair mode to avoid incorrect item from being passed to lowmem check. Date: Fri, 17 May 2019 22:00:03 +0800 Message-Id: <20190517140003.32285-1-wqu@suse.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org In lowmem mode, we check fs roots and free space cache by iterating each root item and inode item, using btrfs_next_item() and a path pointing to the root tree. However in repair mode, check_fs_root() can modify the fs root, thus CoWs the tree root, and the old path in check_fs It could lead to strange behavior, e.g. after repairing a fs tree, the path can point to a fs tree. Since no ROOT_ITEM exists in fs tree, all remaining trees are skipped in repair mode. This bug exists from the early time of lowmem mode repair, and is only exposed by recent free space inode check code. (Fs tree inodes are passed to free space inode check, causing false alerts and repair failure). Signed-off-by: Qu Wenruo --- check/mode-lowmem.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/check/mode-lowmem.c b/check/mode-lowmem.c index 6d7ae2bc0549..808d6be8db30 100644 --- a/check/mode-lowmem.c +++ b/check/mode-lowmem.c @@ -5184,6 +5184,28 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) err |= ret; } next: + /* + * In repair mode, our path is no longer reliable as CoW can + * happen. + * We need to reset our path. + */ + if (repair) { + btrfs_release_path(&path); + ret = btrfs_search_slot(NULL, tree_root, &key, &path, + 0, 0); + if (ret < 0) { + if (!err) + err = ret; + goto out; + } + if (ret > 0) { + /* Key not found, but already at next item */ + if (path.slots[0] < + btrfs_header_nritems(path.nodes[0])) + continue; + /* falls through to next leaf */ + } + } ret = btrfs_next_item(tree_root, &path); if (ret > 0) goto out; -- 2.21.0