mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yangtao Li <frank.li@vivo.com>
To: clm@fb.com, josef@toxicpanda.com, dsterba@suse.com
Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yangtao Li <frank.li@vivo.com>
Subject: [PATCH 13/13] btrfs: update btrfs_qgroup_add_swapped_blocks to to use rb helper
Date: Tue, 22 Apr 2025 02:15:04 -0600	[thread overview]
Message-ID: <20250422081504.1998809-13-frank.li@vivo.com> (raw)
In-Reply-To: <20250422081504.1998809-1-frank.li@vivo.com>

Update btrfs_qgroup_add_swapped_blocks() to use rb_find_add().

Suggested-by: David Sterba <dsterba@suse.com>
Signed-off-by: Yangtao Li <frank.li@vivo.com>
---
 fs/btrfs/qgroup.c | 60 +++++++++++++++++++++++------------------------
 1 file changed, 29 insertions(+), 31 deletions(-)

diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index 5b862470fd67..669c3160573a 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -4684,6 +4684,14 @@ static int btrfs_qgroup_swapped_block_key_cmp(const void *k, const struct rb_nod
 	return 0;
 }
 
+static int btrfs_qgroup_swapped_block_cmp(struct rb_node *new, const struct rb_node *exist)
+{
+	const struct btrfs_qgroup_swapped_block *new_block =
+		rb_entry(new, struct btrfs_qgroup_swapped_block, node);
+
+	return btrfs_qgroup_swapped_block_key_cmp(&new_block->subvol_bytenr, exist);
+}
+
 /*
  * Add subtree roots record into @subvol_root.
  *
@@ -4703,8 +4711,7 @@ int btrfs_qgroup_add_swapped_blocks(struct btrfs_root *subvol_root,
 	struct btrfs_fs_info *fs_info = subvol_root->fs_info;
 	struct btrfs_qgroup_swapped_blocks *blocks = &subvol_root->swapped_blocks;
 	struct btrfs_qgroup_swapped_block *block;
-	struct rb_node **cur;
-	struct rb_node *parent = NULL;
+	struct rb_node *exist;
 	int level = btrfs_header_level(subvol_parent) - 1;
 	int ret = 0;
 
@@ -4753,40 +4760,31 @@ int btrfs_qgroup_add_swapped_blocks(struct btrfs_root *subvol_root,
 
 	/* Insert @block into @blocks */
 	spin_lock(&blocks->lock);
-	cur = &blocks->blocks[level].rb_node;
-	while (*cur) {
+	exist = rb_find_add(&block->node, &blocks->blocks[level],
+				btrfs_qgroup_swapped_block_cmp);
+	if (exist) {
 		struct btrfs_qgroup_swapped_block *entry;
 
-		parent = *cur;
-		entry = rb_entry(parent, struct btrfs_qgroup_swapped_block,
+		entry = rb_entry(exist, struct btrfs_qgroup_swapped_block,
 				 node);
-
-		if (entry->subvol_bytenr < block->subvol_bytenr) {
-			cur = &(*cur)->rb_left;
-		} else if (entry->subvol_bytenr > block->subvol_bytenr) {
-			cur = &(*cur)->rb_right;
-		} else {
-			if (entry->subvol_generation !=
-					block->subvol_generation ||
-			    entry->reloc_bytenr != block->reloc_bytenr ||
-			    entry->reloc_generation !=
-					block->reloc_generation) {
-				/*
-				 * Duplicated but mismatch entry found.
-				 * Shouldn't happen.
-				 *
-				 * Marking qgroup inconsistent should be enough
-				 * for end users.
-				 */
-				WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
-				ret = -EEXIST;
-			}
-			kfree(block);
-			goto out_unlock;
+		if (entry->subvol_generation !=
+				block->subvol_generation ||
+		    entry->reloc_bytenr != block->reloc_bytenr ||
+		    entry->reloc_generation !=
+				block->reloc_generation) {
+			/*
+			 * Duplicated but mismatch entry found.
+			 * Shouldn't happen.
+			 *
+			 * Marking qgroup inconsistent should be enough
+			 * for end users.
+			 */
+			WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
+			ret = -EEXIST;
 		}
+		kfree(block);
+		goto out_unlock;
 	}
-	rb_link_node(&block->node, parent, cur);
-	rb_insert_color(&block->node, &blocks->blocks[level]);
 	blocks->swapped = true;
 out_unlock:
 	spin_unlock(&blocks->lock);
-- 
2.39.0


  parent reply	other threads:[~2025-04-22  7:55 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-22  8:14 [PATCH 01/13] btrfs: update btrfs_insert_inode_defrag " Yangtao Li
2025-04-22  8:14 ` [PATCH 02/13] btrfs: update __btrfs_lookup_delayed_item " Yangtao Li
2025-04-22 11:24   ` David Sterba
2025-04-22  8:14 ` [PATCH 03/13] btrfs: update ulist_rbtree_search " Yangtao Li
2025-04-22  8:14 ` [PATCH 04/13] btrfs: update ulist_rbtree_insert " Yangtao Li
2025-04-22  8:14 ` [PATCH 05/13] btrfs: update lookup_block_entry " Yangtao Li
2025-04-22  8:14 ` [PATCH 06/13] btrfs: update insert_block_entry " Yangtao Li
2025-04-22  8:14 ` [PATCH 07/13] btrfs: update lookup_root_entry " Yangtao Li
2025-04-22  8:14 ` [PATCH 08/13] btrfs: update insert_root_entry " Yangtao Li
2025-04-22  8:15 ` [PATCH 09/13] btrfs: update insert_ref_entry " Yangtao Li
2025-04-22  8:15 ` [PATCH 10/13] btrfs: update find_qgroup_rb " Yangtao Li
2025-04-22  8:15 ` [PATCH 11/13] btrfs: update add_qgroup_rb " Yangtao Li
2025-04-22  8:15 ` [PATCH 12/13] btrfs: update btrfs_qgroup_trace_subtree_after_cow " Yangtao Li
2025-04-22  8:15 ` Yangtao Li [this message]
2025-04-22 11:21 ` [PATCH 01/13] btrfs: update btrfs_insert_inode_defrag " David Sterba
2025-05-20  5:14   ` Yangtao Li
2025-05-21  7:06     ` 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=20250422081504.1998809-13-frank.li@vivo.com \
    --to=frank.li@vivo.com \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@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

all inboxes | Powered by JetHome®