Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Boris Burkov <boris@bur.io>
To: Josef Bacik <josef@toxicpanda.com>
Cc: linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: Re: [PATCH 04/10] btrfs: cleanup select_reloc_root
Date: Thu, 3 Oct 2024 14:27:43 -0700	[thread overview]
Message-ID: <20241003212743.GB435178@zen.localdomain> (raw)
In-Reply-To: <fe72732de97d83d6f5b649fce1642019e78de981.1727970063.git.josef@toxicpanda.com>

On Thu, Oct 03, 2024 at 11:43:06AM -0400, Josef Bacik wrote:
> We have this setup as a loop, but in reality we will never walk back up
> the backref tree, if we do then it's a bug.  Get rid of the loop and
> handle the case where we have node->new_bytenr set at all.  Previous the
> check was only if node->new_bytenr != root->node->start, but if it did
> then we would hit the WARN_ON() and walk back up the tree.
> 
> Instead we want to just freak out if ->new_bytenr is set, and then do
> the normal updating of the node for the reloc root and carry on.
> 
> Signed-off-by: Josef Bacik <josef@toxicpanda.com>
> ---
>  fs/btrfs/relocation.c | 144 ++++++++++++++++++------------------------
>  1 file changed, 61 insertions(+), 83 deletions(-)
> 
> diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
> index ac3658dce6a3..6b2308671d83 100644
> --- a/fs/btrfs/relocation.c
> +++ b/fs/btrfs/relocation.c

I think this (and most other functions in backref caching) could really
benefit from some description of their invariants. I think that will pay
its weight in gold if we ever have to read this code again in 10
years...

> @@ -2058,97 +2058,75 @@ struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
>  	int index = 0;
>  	int ret;
>  
> -	next = node;
> -	while (1) {
> -		cond_resched();
> -		next = walk_up_backref(next, edges, &index);
> -		root = next->root;
> +	next = walk_up_backref(node, edges, &index);
> +	root = next->root;
>  
> -		/*
> -		 * If there is no root, then our references for this block are
> -		 * incomplete, as we should be able to walk all the way up to a
> -		 * block that is owned by a root.
> -		 *
> -		 * This path is only for SHAREABLE roots, so if we come upon a
> -		 * non-SHAREABLE root then we have backrefs that resolve
> -		 * improperly.
> -		 *
> -		 * Both of these cases indicate file system corruption, or a bug
> -		 * in the backref walking code.
> -		 */
> -		if (!root) {
> -			ASSERT(0);
> -			btrfs_err(trans->fs_info,
> -		"bytenr %llu doesn't have a backref path ending in a root",
> -				  node->bytenr);
> -			return ERR_PTR(-EUCLEAN);
> -		}
> -		if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
> -			ASSERT(0);
> -			btrfs_err(trans->fs_info,
> -	"bytenr %llu has multiple refs with one ending in a non-shareable root",
> -				  node->bytenr);
> -			return ERR_PTR(-EUCLEAN);
> -		}
> +	/*
> +	 * If there is no root, then our references for this block are
> +	 * incomplete, as we should be able to walk all the way up to a block
> +	 * that is owned by a root.
> +	 *
> +	 * This path is only for SHAREABLE roots, so if we come upon a
> +	 * non-SHAREABLE root then we have backrefs that resolve improperly.
> +	 *
> +	 * Both of these cases indicate file system corruption, or a bug in the
> +	 * backref walking code.
> +	 */
> +	if (!root) {
> +		ASSERT(0);
> +		btrfs_err(trans->fs_info,
> +			  "bytenr %llu doesn't have a backref path ending in a root",
> +			  node->bytenr);
> +		return ERR_PTR(-EUCLEAN);
> +	}
> +	if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
> +		ASSERT(0);
> +		btrfs_err(trans->fs_info,
> +			  "bytenr %llu has multiple refs with one ending in a non-shareable root",
> +			  node->bytenr);
> +		return ERR_PTR(-EUCLEAN);
> +	}
>  
> -		if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID) {
> -			ret = record_reloc_root_in_trans(trans, root);
> -			if (ret)
> -				return ERR_PTR(ret);
> -			break;
> -		}
> -
> -		ret = btrfs_record_root_in_trans(trans, root);
> +	if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID) {
> +		ret = record_reloc_root_in_trans(trans, root);
>  		if (ret)
>  			return ERR_PTR(ret);
> -		root = root->reloc_root;
> -
> -		/*
> -		 * We could have raced with another thread which failed, so
> -		 * root->reloc_root may not be set, return ENOENT in this case.
> -		 */
> -		if (!root)
> -			return ERR_PTR(-ENOENT);
> -
> -		if (next->new_bytenr != root->node->start) {
> -			/*
> -			 * We just created the reloc root, so we shouldn't have
> -			 * ->new_bytenr set yet. If it is then we have multiple
> -			 *  roots pointing at the same bytenr which indicates
> -			 *  corruption, or we've made a mistake in the backref
> -			 *  walking code.
> -			 */
> -			ASSERT(next->new_bytenr == 0);
> -			if (next->new_bytenr) {
> -				btrfs_err(trans->fs_info,
> -	"bytenr %llu possibly has multiple roots pointing at the same bytenr %llu",
> -					  node->bytenr, next->bytenr);
> -				return ERR_PTR(-EUCLEAN);
> -			}
> -
> -			next->new_bytenr = root->node->start;
> -			btrfs_put_root(next->root);
> -			next->root = btrfs_grab_root(root);
> -			ASSERT(next->root);
> -			mark_block_processed(rc, next);
> -			break;
> -		}
> -
> -		WARN_ON(1);
> -		root = NULL;
> -		next = walk_down_backref(edges, &index);
> -		if (!next || next->level <= node->level)
> -			break;
> +		goto found;
>  	}
> -	if (!root) {
> -		/*
> -		 * This can happen if there's fs corruption or if there's a bug
> -		 * in the backref lookup code.
> -		 */
> -		ASSERT(0);
> +
> +	ret = btrfs_record_root_in_trans(trans, root);
> +	if (ret)
> +		return ERR_PTR(ret);
> +	root = root->reloc_root;
> +
> +	/*
> +	 * We could have raced with another thread which failed, so
> +	 * root->reloc_root may not be set, return ENOENT in this case.
> +	 */
> +	if (!root)
>  		return ERR_PTR(-ENOENT);
> +
> +	if (next->new_bytenr) {
> +		/*
> +		 * We just created the reloc root, so we shouldn't have
> +		 * ->new_bytenr set yet. If it is then we have multiple
> +		 * roots pointing at the same bytenr which indicates
> +		 * corruption, or we've made a mistake in the backref
> +		 * walking code.
> +		 */
> +		ASSERT(next->new_bytenr == 0);
> +		btrfs_err(trans->fs_info,
> +			  "bytenr %llu possibly has multiple roots pointing at the same bytenr %llu",
> +			  node->bytenr, next->bytenr);
> +		return ERR_PTR(-EUCLEAN);
>  	}
>  
> +	next->new_bytenr = root->node->start;
> +	btrfs_put_root(next->root);
> +	next->root = btrfs_grab_root(root);
> +	ASSERT(next->root);
> +	mark_block_processed(rc, next);
> +found:
>  	next = node;
>  	/* setup backref node path for btrfs_reloc_cow_block */
>  	while (1) {
> -- 
> 2.43.0
> 

  reply	other threads:[~2024-10-03 21:27 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-03 15:43 [PATCH 00/10] btrfs: backref cache cleanups Josef Bacik
2024-10-03 15:43 ` [PATCH 01/10] btrfs: convert BUG_ON in btrfs_reloc_cow_block to proper error handling Josef Bacik
2024-10-08 17:29   ` David Sterba
2024-10-03 15:43 ` [PATCH 02/10] btrfs: remove the changed list for backref cache Josef Bacik
2024-10-03 15:43 ` [PATCH 03/10] btrfs: add a comment for new_bytenr in bacref_cache_node Josef Bacik
2024-10-03 21:34   ` Boris Burkov
2024-10-03 15:43 ` [PATCH 04/10] btrfs: cleanup select_reloc_root Josef Bacik
2024-10-03 21:27   ` Boris Burkov [this message]
2024-10-03 15:43 ` [PATCH 05/10] btrfs: remove clone_backref_node Josef Bacik
2024-10-03 15:43 ` [PATCH 06/10] btrfs: don't build backref tree for cowonly blocks Josef Bacik
2024-10-03 21:36   ` Boris Burkov
2024-10-03 15:43 ` [PATCH 07/10] btrfs: do not handle non-shareable roots in backref cache Josef Bacik
2024-10-03 15:43 ` [PATCH 08/10] btrfs: simplify btrfs_backref_release_cache Josef Bacik
2024-10-03 15:43 ` [PATCH 09/10] btrfs: remove the ->lowest and ->leaves members from backref cache Josef Bacik
2024-10-03 15:43 ` [PATCH 10/10] btrfs: remove detached list from btrfs_backref_cache Josef Bacik
2024-10-03 21:39 ` [PATCH 00/10] btrfs: backref cache cleanups Boris Burkov
2024-12-06 19:38 ` David Sterba
2024-12-09 14:01   ` Josef Bacik
2024-12-10 23:24     ` David Sterba

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=20241003212743.GB435178@zen.localdomain \
    --to=boris@bur.io \
    --cc=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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox