Skip to content

Commit

Permalink
btrfs: Panic on bad rbtree operations
Browse files Browse the repository at this point in the history
The ordered data and relocation trees have BUG_ONs to protect against
bad tree operations.

This patch replaces them with a panic that will report the problem.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
  • Loading branch information
Jeff Mahoney authored and David Sterba committed Mar 22, 2012
1 parent c2d904e commit 43c04fb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
12 changes: 10 additions & 2 deletions fs/btrfs/ordered-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
return NULL;
}

static void ordered_data_tree_panic(struct inode *inode, int errno,
u64 offset)
{
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
btrfs_panic(fs_info, errno, "Inconsistency in ordered tree at offset "
"%llu\n", (unsigned long long)offset);
}

/*
* look for a given offset in the tree, and if it can't be found return the
* first lesser offset
Expand Down Expand Up @@ -207,15 +215,15 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
spin_lock(&tree->lock);
node = tree_insert(&tree->tree, file_offset,
&entry->rb_node);
BUG_ON(node);
if (node)
ordered_data_tree_panic(inode, -EEXIST, file_offset);
spin_unlock(&tree->lock);

spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
list_add_tail(&entry->root_extent_list,
&BTRFS_I(inode)->root->fs_info->ordered_extents);
spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);

BUG_ON(node);
return 0;
}

Expand Down
36 changes: 29 additions & 7 deletions fs/btrfs/relocation.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,19 @@ static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
return NULL;
}

void backref_tree_panic(struct rb_node *rb_node, int errno,
u64 bytenr)
{

struct btrfs_fs_info *fs_info = NULL;
struct backref_node *bnode = rb_entry(rb_node, struct backref_node,
rb_node);
if (bnode->root)
fs_info = bnode->root->fs_info;
btrfs_panic(fs_info, errno, "Inconsistency in backref cache "
"found at offset %llu\n", (unsigned long long)bytenr);
}

/*
* walk up backref nodes until reach node presents tree root
*/
Expand Down Expand Up @@ -452,7 +465,8 @@ static void update_backref_node(struct backref_cache *cache,
rb_erase(&node->rb_node, &cache->rb_root);
node->bytenr = bytenr;
rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
BUG_ON(rb_node);
if (rb_node)
backref_tree_panic(rb_node, -EEXIST, bytenr);
}

/*
Expand Down Expand Up @@ -999,7 +1013,8 @@ struct backref_node *build_backref_tree(struct reloc_control *rc,
if (!cowonly) {
rb_node = tree_insert(&cache->rb_root, node->bytenr,
&node->rb_node);
BUG_ON(rb_node);
if (rb_node)
backref_tree_panic(rb_node, -EEXIST, node->bytenr);
list_add_tail(&node->lower, &cache->leaves);
}

Expand Down Expand Up @@ -1034,7 +1049,9 @@ struct backref_node *build_backref_tree(struct reloc_control *rc,
if (!cowonly) {
rb_node = tree_insert(&cache->rb_root, upper->bytenr,
&upper->rb_node);
BUG_ON(rb_node);
if (rb_node)
backref_tree_panic(rb_node, -EEXIST,
upper->bytenr);
}

list_add_tail(&edge->list[UPPER], &upper->lower);
Expand Down Expand Up @@ -1180,7 +1197,8 @@ static int clone_backref_node(struct btrfs_trans_handle *trans,

rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
&new_node->rb_node);
BUG_ON(rb_node);
if (rb_node)
backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);

if (!new_node->lowest) {
list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
Expand Down Expand Up @@ -1252,7 +1270,8 @@ static int __update_reloc_root(struct btrfs_root *root, int del)
rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
node->bytenr, &node->rb_node);
spin_unlock(&rc->reloc_root_tree.lock);
BUG_ON(rb_node);
if (rb_node)
backref_tree_panic(rb_node, -EEXIST, node->bytenr);
} else {
list_del_init(&root->root_list);
kfree(node);
Expand Down Expand Up @@ -3154,7 +3173,8 @@ static int add_tree_block(struct reloc_control *rc,
block->key_ready = 0;

rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
BUG_ON(rb_node);
if (rb_node)
backref_tree_panic(rb_node, -EEXIST, block->bytenr);

return 0;
}
Expand Down Expand Up @@ -3426,7 +3446,9 @@ static int find_data_references(struct reloc_control *rc,
block->key_ready = 1;
rb_node = tree_insert(blocks, block->bytenr,
&block->rb_node);
BUG_ON(rb_node);
if (rb_node)
backref_tree_panic(rb_node, -EEXIST,
block->bytenr);
}
if (counted)
added = 1;
Expand Down

0 comments on commit 43c04fb

Please sign in to comment.