btrfs: open code inexact rbtree search in tree_search
authorDavid Sterba <dsterba@suse.com>
Thu, 25 Jun 2020 16:35:24 +0000 (18:35 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 25 Jul 2022 15:45:35 +0000 (17:45 +0200)
The call chain from

tree_search
  tree_search_for_insert
    __etree_search

can be open coded and allow further simplifications, here we need a tree
search with fallback to the next node in case it's not found. This is
represented as __etree_search parameters next_ret=valid, prev_ret=NULL.

Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/extent_io.c

index 29e6ec7..ee84474 100644 (file)
@@ -453,10 +453,35 @@ tree_search_for_insert(struct extent_io_tree *tree,
        return ret;
 }
 
-static inline struct rb_node *tree_search(struct extent_io_tree *tree,
-                                         u64 offset)
+/*
+ * Inexact rb-tree search, return the next entry if @offset is not found
+ */
+static inline struct rb_node *tree_search(struct extent_io_tree *tree, u64 offset)
 {
-       return tree_search_for_insert(tree, offset, NULL, NULL);
+       struct rb_root *root = &tree->state;
+       struct rb_node **node = &root->rb_node;
+       struct rb_node *prev = NULL;
+       struct tree_entry *entry;
+
+       while (*node) {
+               prev = *node;
+               entry = rb_entry(prev, struct tree_entry, rb_node);
+
+               if (offset < entry->start)
+                       node = &(*node)->rb_left;
+               else if (offset > entry->end)
+                       node = &(*node)->rb_right;
+               else
+                       return *node;
+       }
+
+       /* Search neighbors until we find the first one past the end */
+       while (prev && offset > entry->end) {
+               prev = rb_next(prev);
+               entry = rb_entry(prev, struct tree_entry, rb_node);
+       }
+
+       return prev;
 }
 
 /*