From patchwork Tue Dec 9 08:27:30 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 5460911 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 4AE96BEEA8 for ; Tue, 9 Dec 2014 08:28:21 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 659992017D for ; Tue, 9 Dec 2014 08:28:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6C1162012B for ; Tue, 9 Dec 2014 08:28:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754092AbaLII2J (ORCPT ); Tue, 9 Dec 2014 03:28:09 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:33270 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1754075AbaLII2H (ORCPT ); Tue, 9 Dec 2014 03:28:07 -0500 X-IronPort-AV: E=Sophos;i="5.04,848,1406563200"; d="scan'208";a="44726107" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 09 Dec 2014 16:24:29 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id sB98ROoY013498 for ; Tue, 9 Dec 2014 16:27:25 +0800 Received: from localhost.localdomain (10.167.226.33) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Tue, 9 Dec 2014 16:27:51 +0800 From: Qu Wenruo To: Subject: [PATCH v4 11/13] btrfs-progs: record and report leaf/node corruption in fs/subvol tree Date: Tue, 9 Dec 2014 16:27:30 +0800 Message-ID: <1418113652-25088-12-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.1.3 In-Reply-To: <1418113652-25088-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1418113652-25088-1-git-send-email-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.33] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When leaf/node is corrupted in fs/subvolume root, btrfsck can ignore it without much pain except some stderr messages complaining about it. But this works fine doing read-only works, if we want to do deeper recovery like rebuild missing inodes in the b+tree, it will cause problem. At least, info user that there is something wrong in the btree, and this patch provides the base for later btree repair. Signed-off-by: Qu Wenruo --- v2: Newly introduced. v3~v4: No change. --- cmds-check.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/cmds-check.c b/cmds-check.c index 69142ef..2245cea 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -1474,6 +1474,15 @@ static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path, next = read_tree_block(root, bytenr, blocksize, ptr_gen); if (!next) { + struct btrfs_key node_key; + + btrfs_node_key_to_cpu(path->nodes[*level], + &node_key, + path->slots[*level]); + btrfs_add_corrupt_extent_record(root->fs_info, + &node_key, + path->nodes[*level]->start, + root->leafsize, *level); err = -EIO; goto out; } @@ -2628,6 +2637,16 @@ static int process_root_ref(struct extent_buffer *eb, int slot, return 0; } +static void free_corrupt_block(struct cache_extent *cache) +{ + struct btrfs_corrupt_block *corrupt; + + corrupt = container_of(cache, struct btrfs_corrupt_block, cache); + free(corrupt); +} + +FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block); + static int check_fs_root(struct btrfs_root *root, struct cache_tree *root_cache, struct walk_control *wc) @@ -2640,8 +2659,17 @@ static int check_fs_root(struct btrfs_root *root, struct shared_node root_node; struct root_record *rec; struct btrfs_root_item *root_item = &root->root_item; + struct cache_tree corrupt_blocks; enum btrfs_tree_block_status status; + /* + * Reuse the corrupt_block cache tree to record corrupted tree block + * + * Unlike the usage in extent tree check, here we do it in a per + * fs/subvol tree base. + */ + cache_tree_init(&corrupt_blocks); + root->fs_info->corrupt_blocks = &corrupt_blocks; if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { rec = get_root_rec(root_cache, root->root_key.objectid); if (btrfs_root_refs(root_item) > 0) @@ -2704,6 +2732,25 @@ static int check_fs_root(struct btrfs_root *root, skip_walking: btrfs_release_path(&path); + if (!cache_tree_empty(&corrupt_blocks)) { + struct cache_extent *cache; + struct btrfs_corrupt_block *corrupt; + + printf("The following tree block(s) is corrupted in tree %llu:\n", + root->root_key.objectid); + cache = first_cache_extent(&corrupt_blocks); + while (cache) { + corrupt = container_of(cache, + struct btrfs_corrupt_block, + cache); + printf("\ttree block bytenr: %llu, level: %d, node key: (%llu, %u, %llu)\n", + cache->start, corrupt->level, + corrupt->key.objectid, corrupt->key.type, + corrupt->key.offset); + cache = next_cache_extent(cache); + } + } + err = merge_root_recs(root, &root_node.root_cache, root_cache); if (err < 0) ret = err; @@ -2717,6 +2764,9 @@ skip_walking: err = check_inode_recs(root, &root_node.inode_cache); if (!ret) ret = err; + + free_corrupt_blocks_tree(&corrupt_blocks); + root->fs_info->corrupt_blocks = NULL; return ret; } @@ -6288,16 +6338,6 @@ static int prune_corrupt_blocks(struct btrfs_trans_handle *trans, return 0; } -static void free_corrupt_block(struct cache_extent *cache) -{ - struct btrfs_corrupt_block *corrupt; - - corrupt = container_of(cache, struct btrfs_corrupt_block, cache); - free(corrupt); -} - -FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block); - static void reset_cached_block_groups(struct btrfs_fs_info *fs_info) { struct btrfs_block_group_cache *cache;