btrfs: track reloc roots based on their commit root bytenr
authorJosef Bacik <josef@toxicpanda.com>
Fri, 13 Mar 2020 21:17:08 +0000 (17:17 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 24 Apr 2020 05:58:49 +0000 (07:58 +0200)
[ Upstream commit ea287ab157c2816bf12aad4cece41372f9d146b4 ]

We always search the commit root of the extent tree for looking up back
references, however we track the reloc roots based on their current
bytenr.

This is wrong, if we commit the transaction between relocating tree
blocks we could end up in this code in build_backref_tree

  if (key.objectid == key.offset) {
  /*
   * Only root blocks of reloc trees use backref
   * pointing to itself.
   */
  root = find_reloc_root(rc, cur->bytenr);
  ASSERT(root);
  cur->root = root;
  break;
  }

find_reloc_root() is looking based on the bytenr we had in the commit
root, but if we've COWed this reloc root we will not find that bytenr,
and we will trip over the ASSERT(root).

Fix this by using the commit_root->start bytenr for indexing the commit
root.  Then we change the __update_reloc_root() caller to be used when
we switch the commit root for the reloc root during commit.

This fixes the panic I was seeing when we started throttling relocation
for delayed refs.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/btrfs/relocation.c

index 97bc85ca508c208735ce6024a1601d16e2ab8eda..8d98e8e248b7a49d7d737b02cddb3fa007733a63 100644 (file)
@@ -1296,7 +1296,7 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
        if (!node)
                return -ENOMEM;
 
-       node->bytenr = root->node->start;
+       node->bytenr = root->commit_root->start;
        node->data = root;
 
        spin_lock(&rc->reloc_root_tree.lock);
@@ -1328,10 +1328,11 @@ static void __del_reloc_root(struct btrfs_root *root)
        if (rc && root->node) {
                spin_lock(&rc->reloc_root_tree.lock);
                rb_node = tree_search(&rc->reloc_root_tree.rb_root,
-                                     root->node->start);
+                                     root->commit_root->start);
                if (rb_node) {
                        node = rb_entry(rb_node, struct mapping_node, rb_node);
                        rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
+                       RB_CLEAR_NODE(&node->rb_node);
                }
                spin_unlock(&rc->reloc_root_tree.lock);
                if (!node)
@@ -1349,7 +1350,7 @@ static void __del_reloc_root(struct btrfs_root *root)
  * helper to update the 'address of tree root -> reloc tree'
  * mapping
  */
-static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
+static int __update_reloc_root(struct btrfs_root *root)
 {
        struct rb_node *rb_node;
        struct mapping_node *node = NULL;
@@ -1357,7 +1358,7 @@ static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
 
        spin_lock(&rc->reloc_root_tree.lock);
        rb_node = tree_search(&rc->reloc_root_tree.rb_root,
-                             root->node->start);
+                             root->commit_root->start);
        if (rb_node) {
                node = rb_entry(rb_node, struct mapping_node, rb_node);
                rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
@@ -1369,7 +1370,7 @@ static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
        BUG_ON((struct btrfs_root *)node->data != root);
 
        spin_lock(&rc->reloc_root_tree.lock);
-       node->bytenr = new_bytenr;
+       node->bytenr = root->node->start;
        rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
                              node->bytenr, &node->rb_node);
        spin_unlock(&rc->reloc_root_tree.lock);
@@ -1519,6 +1520,7 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
        }
 
        if (reloc_root->commit_root != reloc_root->node) {
+               __update_reloc_root(reloc_root);
                btrfs_set_root_node(root_item, reloc_root->node);
                free_extent_buffer(reloc_root->commit_root);
                reloc_root->commit_root = btrfs_root_node(reloc_root);
@@ -4717,11 +4719,6 @@ int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
        BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
               root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
 
-       if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
-               if (buf == root->node)
-                       __update_reloc_root(root, cow->start);
-       }
-
        level = btrfs_header_level(buf);
        if (btrfs_header_generation(buf) <=
            btrfs_root_last_snapshot(&root->root_item))