Skip to contentSkip to conversation

btrfs: some more cleanups for the io tree code

PATCH In review [email protected] 2 participants 6 messages Apr 2, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: some more cleanups for the io tree code

From: Filipe Manana <[email protected]>

Some simple and short cleanups for the io tree code in preparation for other work. Details in the change logs.

Filipe Manana (4):
btrfs: fix documentation for tree_search_for_insert()
btrfs: remove redundant check at find_first_extent_bit_state()
btrfs: simplify last record detection at test_range_bit()
btrfs: remove redudant record start offset check at test_range_bit()

 fs/btrfs/extent-io-tree.c | 41 +++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 21 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/4 btrfs: fix documentation for tree_search_for_insert()

From: Filipe Manana <[email protected]>

There are several things wrong with the documentation:

1) At the top it's only mentioned that we search for an entry containing the given offset, but when such entry does not exists we search for the first entry that starts and ends after that offset;

2) It mentions that @node_ret and @parent_ret aren't changed if the returned entry contains the given offset - that is true only if the returned entry starts exactly at @offset, otherwise those arguments are changed;

3) It mentions that if no entry containing offset is found then we return the first entry ending before the offset - that is not true, we return the first entry that starts and ends after that offset;

4) It also mentions that NULL is never returned. This is false as in case there's no entry containing offset or any entry that starts and ends after offset, NULL is returned.

So fix the documentation.

---
 fs/btrfs/extent-io-tree.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
fs/btrfs/extent-io-tree.c +14 -8
index 3c138e6c2397..355c24449776 100644
@@ -233,21 +233,23 @@ static inline struct extent_state *prev_state(struct extent_state *state)
 }
 
 /*
- * Search @tree for an entry that contains @offset. Such entry would have
- * entry->start <= offset && entry->end >= offset.
+ * Search @tree for an entry that contains @offset or if none exists for the
+ * first entry that starts and ends after that offset.
  *
  * @tree:       the tree to search
- * @offset:     offset that should fall within an entry in @tree
+ * @offset:     search offset
  * @node_ret:   pointer where new node should be anchored (used when inserting an
  *	        entry in the tree)
  * @parent_ret: points to entry which would have been the parent of the entry,
  *               containing @offset
  *
- * Return a pointer to the entry that contains @offset byte address and don't change
- * @node_ret and @parent_ret.
+ * Return a pointer to the entry that contains @offset byte address.
  *
- * If no such entry exists, return pointer to entry that ends before @offset
- * and fill parameters @node_ret and @parent_ret, ie. does not return NULL.
+ * If no such entry exists, return the first entry that starts and ends after
+ * @offset if one exists, otherwise NULL.
+ *
+ * If the returned entry starts at @offset, then @node_ret and @parent_ret
+ * aren't changed.
  */
 static inline struct extent_state *tree_search_for_insert(struct extent_io_tree *tree,
 							  u64 offset,
@@ -276,7 +278,11 @@ static inline struct extent_state *tree_search_for_insert(struct extent_io_tree
 	if (parent_ret)
 		*parent_ret = prev;
 
-	/* Search neighbors until we find the first one past the end */
+	/*
+	 * Return either the current entry if it contains offset (it ends after
+	 * or at offset) or the first entry that starts and ends after offset if
+	 * one exists, or NULL.
+	 */
 	while (entry && offset > entry->end)
 		entry = next_state(entry);
 
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/4 btrfs: remove redundant check at find_first_extent_bit_state()

In reply to [email protected]

From: Filipe Manana <[email protected]>

The tree_search() function always returns an entry that either contains the search offset or the first entry in the tree that starts after the offset. So checking at find_first_extent_bit_state() if the returned entry ends at or after the search offset is pointless. Remove the check.

---
 fs/btrfs/extent-io-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent-io-tree.c +1 -1
index 355c24449776..18b10e7ed815 100644
@@ -877,7 +877,7 @@ static struct extent_state *find_first_extent_bit_state(struct extent_io_tree *t
 	 */
 	state = tree_search(tree, start);
 	while (state) {
-		if (state->end >= start && (state->state & bits))
+		if (state->state & bits)
 			return state;
 		state = next_state(state);
 	}
Signature · 1 line
2.45.2
#3 lore raw

PATCH 3/4 btrfs: simplify last record detection at test_range_bit()

In reply to [email protected]

From: Filipe Manana <[email protected]>

The overflow detection for the start offset of the next record is not really necessary, we can just stop iterating if the current record ends at or after out end offset. This removes the need to test if the current record end offset is (u64)-1 and to check if adding 1 to the current end offset results in 0.

By testing only if the current record ends at or after the end offset, we also don't need anymore to test the new start offset at the head of the while loop.

This makes both the source code and assembly code simpler, more efficient and shorter (reducing the object text size).

Also remove the pointless initializion to NULL of the state variable, as we don't use it before the first assignment to it. This may help avoid some warnings with clang tools such as the one reported/fixed by commit 966de47ff0c9 ("btrfs: remove redundant initialization of variables in log_new_ancestors").

---
 fs/btrfs/extent-io-tree.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
fs/btrfs/extent-io-tree.c +4 -9
index 18b10e7ed815..b321f826d008 100644
@@ -1790,7 +1790,7 @@ void get_range_bits(struct extent_io_tree *tree, u64 start, u64 end, u32 *bits,
 bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
 		    struct extent_state *cached)
 {
-	struct extent_state *state = NULL;
+	struct extent_state *state;
 	bool bitset = true;
 
 	ASSERT(is_power_of_2(bit));
@@ -1801,7 +1801,7 @@ bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
 		state = cached;
 	else
 		state = tree_search(tree, start);
-	while (state && start <= end) {
+	while (state) {
 		if (state->start > start) {
 			bitset = false;
 			break;
@@ -1815,16 +1815,11 @@ bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
 			break;
 		}
 
-		if (state->end == (u64)-1)
+		if (state->end >= end)
 			break;
 
-		/*
-		 * Last entry (if state->end is (u64)-1 and overflow happens),
-		 * or next entry starts after the range.
-		 */
+		/* Next state must start where this one ends. */
 		start = state->end + 1;
-		if (start > end || start == 0)
-			break;
 		state = next_state(state);
 	}
 
Signature · 1 line
2.45.2
#4 lore raw

PATCH 4/4 btrfs: remove redudant record start offset check at test_range_bit()

In reply to [email protected]

From: Filipe Manana <[email protected]>

It's pointless to check if the current record's start offset is greater than the end offset, as before we just tested if it was greater than the start offset - and if it's not it means it's less than or equal to the start offset, so it can not be greater than the end offset, as our start offset is always smaller than the end offset.

So remove that check and also add an assertion to verify the start offset is smaller then the end offset.

---
 fs/btrfs/extent-io-tree.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/btrfs/extent-io-tree.c +1 -3
index b321f826d008..d833ab2d69a1 100644
@@ -1794,6 +1794,7 @@ bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
 	bool bitset = true;
 
 	ASSERT(is_power_of_2(bit));
+	ASSERT(start < end);
 
 	spin_lock(&tree->lock);
 	if (cached && extent_state_in_tree(cached) && cached->start <= start &&
@@ -1807,9 +1808,6 @@ bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
 			break;
 		}
 
-		if (state->start > end)
-			break;
-
 		if ((state->state & bit) == 0) {
 			bitset = false;
 			break;
Signature · 1 line
2.45.2
Reviewed-by on the v1 series
#5 lore raw
In reply to [email protected]
Quoted 10 lines · On Wed, Apr 02, 2025 at 03:54:08PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

Some simple and short cleanups for the io tree code in preparation for other work. Details in the change logs.

Filipe Manana (4):
btrfs: fix documentation for tree_search_for_insert()
btrfs: remove redundant check at find_first_extent_bit_state()
btrfs: simplify last record detection at test_range_bit()
btrfs: remove redudant record start offset check at test_range_bit()