[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250516030333.3758-14-panchuang@vivo.com>
Date: Fri, 16 May 2025 11:03:31 +0800
From: Pan Chuang <panchuang@...o.com>
To: clm@...com,
josef@...icpanda.com,
dsterba@...e.com,
frank.li@...o.com
Cc: linux-btrfs@...r.kernel.org,
linux-kernel@...r.kernel.org,
Pan Chuang <panchuang@...o.com>
Subject: [PATCH v2 13/15] btrfs: use rb_find_add() in btrfs_qgroup_add_swapped_blocks()
From: Yangtao Li <frank.li@...o.com>
Use the rb-tree helper so we don't open code the search and insert
code.
Signed-off-by: Yangtao Li <frank.li@...o.com>
Signed-off-by: Pan Chuang <panchuang@...o.com>
---
v2:
- Standardize coding style without logical change.
---
fs/btrfs/qgroup.c | 59 +++++++++++++++++++++++------------------------
1 file changed, 29 insertions(+), 30 deletions(-)
diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index 2e8f6ab004e9..e9498c6a55d2 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -4685,6 +4685,14 @@ static int qgroup_swapped_block_bytenr_key_cmp(const void *key, const struct rb_
return 0;
}
+static int qgroup_swapped_block_bytenr_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 qgroup_swapped_block_bytenr_key_cmp(&new_block->subvol_bytenr, exist);
+}
+
/*
* Add subtree roots record into @subvol_root.
*
@@ -4704,8 +4712,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 *node;
int level = btrfs_header_level(subvol_parent) - 1;
int ret = 0;
@@ -4754,40 +4761,32 @@ 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) {
+ node = rb_find_add(&block->node, &blocks->blocks[level],
+ qgroup_swapped_block_bytenr_cmp);
+ if (node) {
struct btrfs_qgroup_swapped_block *entry;
- parent = *cur;
- entry = rb_entry(parent, struct btrfs_qgroup_swapped_block,
+ entry = rb_entry(node, 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.
- */
- DEBUG_WARN("duplicated but mismatched entry found");
- 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
Powered by blists - more mailing lists