All of lore.kernel.org
 help / color / mirror / Atom feed
From: Josef Bacik <josef@toxicpanda.com>
To: linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: [PATCH 23/45] btrfs: hold a ref on the root in build_backref_tree
Date: Tue, 17 Dec 2019 10:36:13 -0500	[thread overview]
Message-ID: <20191217153635.44733-24-josef@toxicpanda.com> (raw)
In-Reply-To: <20191217153635.44733-1-josef@toxicpanda.com>

This is trickier than the previous conversions.  We have backref_node's
that need to hold onto their root for their lifetime.  Do the read of
the root and grab the ref.  If at any point we don't use the root we
discard it, however if we use it in our backref node we don't free it
until we free the backref node.  Any time we switch the root's for the
backref node we need to drop our ref on the old root and grab the ref on
the new root, and if we dupe a node we need to get a ref on the root
there as well.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/relocation.c | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 3f11491e01eb..473bbbd58d31 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -256,6 +256,8 @@ static void free_backref_node(struct backref_cache *cache,
 {
 	if (node) {
 		cache->nr_nodes--;
+		if (node->root)
+			btrfs_put_fs_root(node->root);
 		kfree(node);
 	}
 }
@@ -859,6 +861,10 @@ struct backref_node *build_backref_tree(struct reloc_control *rc,
 			err = PTR_ERR(root);
 			goto out;
 		}
+		if (!btrfs_grab_fs_root(root)) {
+			err = -ENOENT;
+			goto out;
+		}
 
 		if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
 			cur->cowonly = 1;
@@ -867,10 +873,12 @@ struct backref_node *build_backref_tree(struct reloc_control *rc,
 			/* tree root */
 			ASSERT(btrfs_root_bytenr(&root->root_item) ==
 			       cur->bytenr);
-			if (should_ignore_root(root))
+			if (should_ignore_root(root)) {
+				btrfs_put_fs_root(root);
 				list_add(&cur->list, &useless);
-			else
+			} else {
 				cur->root = root;
+			}
 			break;
 		}
 
@@ -883,6 +891,7 @@ struct backref_node *build_backref_tree(struct reloc_control *rc,
 		ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
 		path2->lowest_level = 0;
 		if (ret < 0) {
+			btrfs_put_fs_root(root);
 			err = ret;
 			goto out;
 		}
@@ -898,6 +907,7 @@ struct backref_node *build_backref_tree(struct reloc_control *rc,
 				  root->root_key.objectid,
 				  node_key->objectid, node_key->type,
 				  node_key->offset);
+			btrfs_put_fs_root(root);
 			err = -ENOENT;
 			goto out;
 		}
@@ -909,15 +919,18 @@ struct backref_node *build_backref_tree(struct reloc_control *rc,
 			if (!path2->nodes[level]) {
 				ASSERT(btrfs_root_bytenr(&root->root_item) ==
 				       lower->bytenr);
-				if (should_ignore_root(root))
+				if (should_ignore_root(root)) {
+					btrfs_put_fs_root(root);
 					list_add(&lower->list, &useless);
-				else
+				} else {
 					lower->root = root;
+				}
 				break;
 			}
 
 			edge = alloc_backref_edge(cache);
 			if (!edge) {
+				btrfs_put_fs_root(root);
 				err = -ENOMEM;
 				goto out;
 			}
@@ -927,6 +940,7 @@ struct backref_node *build_backref_tree(struct reloc_control *rc,
 			if (!rb_node) {
 				upper = alloc_backref_node(cache);
 				if (!upper) {
+					btrfs_put_fs_root(root);
 					free_backref_edge(cache, edge);
 					err = -ENOMEM;
 					goto out;
@@ -974,8 +988,10 @@ struct backref_node *build_backref_tree(struct reloc_control *rc,
 			edge->node[LOWER] = lower;
 			edge->node[UPPER] = upper;
 
-			if (rb_node)
+			if (rb_node) {
+				btrfs_put_fs_root(root);
 				break;
+			}
 			lower = upper;
 			upper = NULL;
 		}
@@ -1212,7 +1228,8 @@ static int clone_backref_node(struct btrfs_trans_handle *trans,
 	new_node->level = node->level;
 	new_node->lowest = node->lowest;
 	new_node->checked = 1;
-	new_node->root = dest;
+	new_node->root = btrfs_grab_fs_root(dest);
+	ASSERT(new_node->root);
 
 	if (!node->lowest) {
 		list_for_each_entry(edge, &node->lower, list[UPPER]) {
@@ -2581,7 +2598,9 @@ struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
 			BUG_ON(next->new_bytenr);
 			BUG_ON(!list_empty(&next->list));
 			next->new_bytenr = root->node->start;
-			next->root = root;
+			btrfs_put_fs_root(next->root);
+			next->root = btrfs_grab_fs_root(root);
+			ASSERT(next->root);
 			list_add_tail(&next->list,
 				      &rc->backref_cache.changed);
 			__mark_block_processed(rc, next);
@@ -3053,7 +3072,9 @@ static int relocate_tree_block(struct btrfs_trans_handle *trans,
 			btrfs_record_root_in_trans(trans, root);
 			root = root->reloc_root;
 			node->new_bytenr = root->node->start;
-			node->root = root;
+			btrfs_put_fs_root(node->root);
+			node->root = btrfs_grab_fs_root(root);
+			ASSERT(node->root);
 			list_add_tail(&node->list, &rc->backref_cache.changed);
 		} else {
 			path->lowest_level = node->level;
-- 
2.23.0


  parent reply	other threads:[~2019-12-17 15:37 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-17 15:35 [PATCH 0/45][v2] Cleanup how we handle root refs, part 1 Josef Bacik
2019-12-17 15:35 ` [PATCH 01/45] btrfs: push __setup_root into btrfs_alloc_root Josef Bacik
2019-12-17 15:35 ` [PATCH 02/45] btrfs: move fs root init stuff into btrfs_init_fs_root Josef Bacik
2019-12-17 15:35 ` [PATCH 03/45] btrfs: make btrfs_find_orphan_roots use btrfs_get_fs_root Josef Bacik
2019-12-17 15:35 ` [PATCH 04/45] btrfs: export and use btrfs_read_tree_root Josef Bacik
2019-12-17 15:35 ` [PATCH 05/45] btrfs: make relocation use btrfs_read_tree_root() Josef Bacik
2019-12-17 15:35 ` [PATCH 06/45] btrfs: kill btrfs_read_fs_root Josef Bacik
2019-12-17 15:35 ` [PATCH 07/45] btrfs: kill the btrfs_read_fs_root_no_name helper Josef Bacik
2019-12-17 15:35 ` [PATCH 08/45] btrfs: make the fs root init functions static Josef Bacik
2019-12-17 15:35 ` [PATCH 09/45] btrfs: handle NULL roots in btrfs_put/btrfs_grab_fs_root Josef Bacik
2019-12-17 15:36 ` [PATCH 10/45] btrfs: hold a ref on fs roots while they're in the radix tree Josef Bacik
2019-12-17 15:36 ` [PATCH 11/45] btrfs: hold a ref on the root in resolve_indirect_ref Josef Bacik
2019-12-17 15:36 ` [PATCH 12/45] btrfs: hold a root ref in btrfs_get_dentry Josef Bacik
2019-12-17 15:36 ` [PATCH 13/45] btrfs: hold a ref on the root in __btrfs_run_defrag_inode Josef Bacik
2019-12-17 15:36 ` [PATCH 14/45] btrfs: hold a ref for the root in record_one_backref Josef Bacik
2019-12-17 15:36 ` [PATCH 15/45] btrfs: grab a ref on the root in relink_extent_backref Josef Bacik
2019-12-17 15:36 ` [PATCH 16/45] btrfs: hold a ref on the root in fixup_tree_root_location Josef Bacik
2019-12-17 15:36 ` [PATCH 17/45] btrfs: hold a ref on the root in create_subvol Josef Bacik
2019-12-17 15:36 ` [PATCH 18/45] btrfs: hold a ref on the root in search_ioctl Josef Bacik
2019-12-17 15:36 ` [PATCH 19/45] btrfs: hold a ref on the root in btrfs_search_path_in_tree Josef Bacik
2019-12-17 15:36 ` [PATCH 20/45] btrfs: hold a ref on the root in btrfs_search_path_in_tree_user Josef Bacik
2019-12-17 15:36 ` [PATCH 21/45] btrfs: hold a ref on the root in btrfs_ioctl_get_subvol_info Josef Bacik
2019-12-17 15:36 ` [PATCH 22/45] btrfs: hold ref on root in btrfs_ioctl_default_subvol Josef Bacik
2019-12-17 15:36 ` Josef Bacik [this message]
2019-12-17 15:36 ` [PATCH 24/45] btrfs: hold a ref on the root in prepare_to_merge Josef Bacik
2019-12-17 15:36 ` [PATCH 25/45] btrfs: hold a ref on the root in merge_reloc_roots Josef Bacik
2019-12-17 15:36 ` [PATCH 26/45] btrfs: hold a ref on the root in record_reloc_root_in_trans Josef Bacik
2019-12-17 15:36 ` [PATCH 27/45] btrfs: hold a ref on the root in find_data_references Josef Bacik
2019-12-17 15:36 ` [PATCH 28/45] btrfs: hold a ref on the root in create_reloc_inode Josef Bacik
2019-12-17 15:36 ` [PATCH 29/45] btrfs: hold a ref on the root in btrfs_recover_relocation Josef Bacik
2019-12-17 15:36 ` [PATCH 30/45] btrfs: push grab_fs_root into read_fs_root Josef Bacik
2019-12-17 15:36 ` [PATCH 31/45] btrfs: hold a ref for the root in btrfs_find_orphan_roots Josef Bacik
2019-12-17 15:36 ` [PATCH 32/45] btrfs: hold a ref on the root in scrub_print_warning_inode Josef Bacik
2019-12-17 15:36 ` [PATCH 33/45] btrfs: hold a ref on the root in btrfs_ioctl_send Josef Bacik
2019-12-17 15:36 ` [PATCH 34/45] btrfs: hold a ref on the root in get_subvol_name_from_objectid Josef Bacik
2019-12-17 15:36 ` [PATCH 35/45] btrfs: hold a ref on the root in create_pending_snapshot Josef Bacik
2019-12-17 15:36 ` [PATCH 36/45] btrfs: hold a ref on the root in btrfs_recover_log_trees Josef Bacik
2019-12-17 15:36 ` [PATCH 37/45] btrfs: hold a ref on the root in btrfs_check_uuid_tree_entry Josef Bacik
2019-12-17 15:36 ` [PATCH 38/45] btrfs: hold a ref on the root in open_ctree Josef Bacik
2019-12-17 15:36 ` [PATCH 39/45] btrfs: use btrfs_put_fs_root to free roots always Josef Bacik
2019-12-17 15:36 ` [PATCH 40/45] btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root Josef Bacik
2019-12-17 15:36 ` [PATCH 41/45] btrfs: free more things in btrfs_free_fs_info Josef Bacik
2019-12-17 15:36 ` [PATCH 42/45] btrfs: move fs_info init work into it's own helper function Josef Bacik
2019-12-17 15:36 ` [PATCH 43/45] btrfs: make the init of static elements in fs_info separate Josef Bacik
2019-12-17 15:36 ` [PATCH 44/45] btrfs: add a leak check for roots Josef Bacik
2019-12-17 15:36 ` [PATCH 45/45] btrfs: rename btrfs_put_fs_root and btrfs_grab_fs_root Josef Bacik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191217153635.44733-24-josef@toxicpanda.com \
    --to=josef@toxicpanda.com \
    --cc=kernel-team@fb.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.