diff options
authorJosef Bacik2013-04-01 19:36:28 -0500
committerGreg Kroah-Hartman2013-05-11 15:54:09 -0500
commit02cfa2ab3ec610411e30dd15b1df2df0e31afde7 (patch)
parentbbc666f0002eb6c97609e59af39fca010ecf8ddd (diff)
Btrfs: compare relevant parts of delayed tree refs
commit 41b0fc42800569f63e029549b75c4c9cb63f2dfd upstream. A user reported a panic while running a balance. What was happening was he was relocating a block, which added the reference to the relocation tree. Then relocation would walk through the relocation tree and drop that reference and free that block, and then it would walk down a snapshot which referenced the same block and add another ref to the block. The problem is this was all happening in the same transaction, so the parent block was free'ed up when we drop our reference which was immediately available for allocation, and then it was used _again_ to add a reference for the same block from a different snapshot. This resulted in something like this in the delayed ref tree add ref to 90234880, parent=2067398656, ref_root 1766, level 1 del ref to 90234880, parent=2067398656, ref_root 18446744073709551608, level 1 add ref to 90234880, parent=2067398656, ref_root 1767, level 1 as you can see the ref_root's don't match, because when we inc the ref we use the header owner, which is the original tree the block belonged to, instead of the data reloc tree. Then when we remove the extent we use the reloc tree objectid. But none of this matters, since it is a shared reference which means only the parent matters. When the delayed ref stuff runs it adds all the increments first, and then does all the drops, to make sure that we don't delete the ref if we net a positive ref count. But tree blocks aren't allowed to have multiple refs from the same block, so this panics when it tries to add the second ref. We need the add and the drop to cancel each other out in memory so we only do the final add. So to fix this we need to adjust how the delayed refs are added to the tree. Only the ref_root matters when it is a normal backref, and only the parent matters when it is a shared backref. So make our decision based on what ref type we have. This allows us to keep the ref_root in memory in case anybody wants to use it for something else, and it allows the delayed refs to be merged properly so we don't end up with this panic. With this patch the users image no longer panics on mount, and it has a clean fsck after a normal mount/umount cycle. Thanks, Reported-by: Roman Mamedov <rm@romanrm.ru> Signed-off-by: Josef Bacik <jbacik@fusionio.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 files changed, 14 insertions, 10 deletions
diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
index ae9411773397..105b265470dd 100644
--- a/fs/btrfs/delayed-ref.c
+++ b/fs/btrfs/delayed-ref.c
@@ -36,16 +36,19 @@
36 * compare two delayed tree backrefs with same bytenr and type 36 * compare two delayed tree backrefs with same bytenr and type
37 */ 37 */
38static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref2, 38static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref2,
39 struct btrfs_delayed_tree_ref *ref1) 39 struct btrfs_delayed_tree_ref *ref1, int type)
40{ 40{
41 if (ref1->root < ref2->root) 41 if (type == BTRFS_TREE_BLOCK_REF_KEY) {
42 return -1; 42 if (ref1->root < ref2->root)
43 if (ref1->root > ref2->root) 43 return -1;
44 return 1; 44 if (ref1->root > ref2->root)
45 if (ref1->parent < ref2->parent) 45 return 1;
46 return -1; 46 } else {
47 if (ref1->parent > ref2->parent) 47 if (ref1->parent < ref2->parent)
48 return 1; 48 return -1;
49 if (ref1->parent > ref2->parent)
50 return 1;
51 }
49 return 0; 52 return 0;
50} 53}
51 54
@@ -109,7 +112,8 @@ static int comp_entry(struct btrfs_delayed_ref_node *ref2,
109 if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY || 112 if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
110 ref1->type == BTRFS_SHARED_BLOCK_REF_KEY) { 113 ref1->type == BTRFS_SHARED_BLOCK_REF_KEY) {
111 return comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref2), 114 return comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref2),
112 btrfs_delayed_node_to_tree_ref(ref1)); 115 btrfs_delayed_node_to_tree_ref(ref1),
116 ref1->type);
113 } else if (ref1->type == BTRFS_EXTENT_DATA_REF_KEY || 117 } else if (ref1->type == BTRFS_EXTENT_DATA_REF_KEY ||
114 ref1->type == BTRFS_SHARED_DATA_REF_KEY) { 118 ref1->type == BTRFS_SHARED_DATA_REF_KEY) {
115 return comp_data_refs(btrfs_delayed_node_to_data_ref(ref2), 119 return comp_data_refs(btrfs_delayed_node_to_data_ref(ref2),