btrfs: use rb_find_add() in insert_root_entry()
authorYangtao Li <frank.li@vivo.com>
Fri, 16 May 2025 03:03:26 +0000 (11:03 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 21 Jul 2025 21:53:25 +0000 (23:53 +0200)
Use the rb-tree helper so we don't open code the search and insert
code.

Signed-off-by: Yangtao Li <frank.li@vivo.com>
Signed-off-by: Pan Chuang <panchuang@vivo.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ref-verify.c

index 72752089d2cfbe34b829e358e898273fb68949c2..6365cb72711db965ad3a0871be745301c711f1e2 100644 (file)
@@ -125,28 +125,20 @@ static int root_entry_root_objectid_key_cmp(const void *key, const struct rb_nod
        return 0;
 }
 
-static struct root_entry *insert_root_entry(struct rb_root *root,
-                                           struct root_entry *re)
+static int root_entry_root_objectid_cmp(struct rb_node *new, const struct rb_node *existing)
 {
-       struct rb_node **p = &root->rb_node;
-       struct rb_node *parent_node = NULL;
-       struct root_entry *entry;
+       const struct root_entry *new_entry = rb_entry(new, struct root_entry, node);
 
-       while (*p) {
-               parent_node = *p;
-               entry = rb_entry(parent_node, struct root_entry, node);
-               if (entry->root_objectid > re->root_objectid)
-                       p = &(*p)->rb_left;
-               else if (entry->root_objectid < re->root_objectid)
-                       p = &(*p)->rb_right;
-               else
-                       return entry;
-       }
+       return root_entry_root_objectid_key_cmp(&new_entry->root_objectid, existing);
+}
 
-       rb_link_node(&re->node, parent_node, p);
-       rb_insert_color(&re->node, root);
-       return NULL;
+static struct root_entry *insert_root_entry(struct rb_root *root,
+                                           struct root_entry *re)
+{
+       struct rb_node *node;
 
+       node = rb_find_add(&re->node, root, root_entry_root_objectid_cmp);
+       return rb_entry_safe(node, struct root_entry, node);
 }
 
 static int comp_refs(struct ref_entry *ref1, struct ref_entry *ref2)