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 <quwen...@cn.fujitsu.com>
---
 cmds-check.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 50 insertions(+), 10 deletions(-)

diff --git a/cmds-check.c b/cmds-check.c
index 627b794..02d9b32 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -1475,6 +1475,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;
                        }
@@ -2614,6 +2623,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)
@@ -2626,8 +2645,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)
@@ -2690,6 +2718,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;
@@ -2703,6 +2750,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;
 }
 
@@ -6274,16 +6324,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;
-- 
2.1.3

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to