* [PATCH 1/4] btrfs: fix documentation for tree_search_for_insert()
2025-04-02 14:54 [PATCH 0/4] btrfs: some more cleanups for the io tree code fdmanana
@ 2025-04-02 14:54 ` fdmanana
2025-04-02 14:54 ` [PATCH 2/4] btrfs: remove redundant check at find_first_extent_bit_state() fdmanana
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: fdmanana @ 2025-04-02 14:54 UTC (permalink / raw)
To: linux-btrfs
From: Filipe Manana <fdmanana@suse.com>
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.
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
fs/btrfs/extent-io-tree.c | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/fs/btrfs/extent-io-tree.c b/fs/btrfs/extent-io-tree.c
index 3c138e6c2397..355c24449776 100644
--- a/fs/btrfs/extent-io-tree.c
+++ b/fs/btrfs/extent-io-tree.c
@@ -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);
--
2.45.2
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH 2/4] btrfs: remove redundant check at find_first_extent_bit_state()
2025-04-02 14:54 [PATCH 0/4] btrfs: some more cleanups for the io tree code fdmanana
2025-04-02 14:54 ` [PATCH 1/4] btrfs: fix documentation for tree_search_for_insert() fdmanana
@ 2025-04-02 14:54 ` fdmanana
2025-04-02 14:54 ` [PATCH 3/4] btrfs: simplify last record detection at test_range_bit() fdmanana
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: fdmanana @ 2025-04-02 14:54 UTC (permalink / raw)
To: linux-btrfs
From: Filipe Manana <fdmanana@suse.com>
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.
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
fs/btrfs/extent-io-tree.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/btrfs/extent-io-tree.c b/fs/btrfs/extent-io-tree.c
index 355c24449776..18b10e7ed815 100644
--- a/fs/btrfs/extent-io-tree.c
+++ b/fs/btrfs/extent-io-tree.c
@@ -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);
}
--
2.45.2
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH 3/4] btrfs: simplify last record detection at test_range_bit()
2025-04-02 14:54 [PATCH 0/4] btrfs: some more cleanups for the io tree code fdmanana
2025-04-02 14:54 ` [PATCH 1/4] btrfs: fix documentation for tree_search_for_insert() fdmanana
2025-04-02 14:54 ` [PATCH 2/4] btrfs: remove redundant check at find_first_extent_bit_state() fdmanana
@ 2025-04-02 14:54 ` fdmanana
2025-04-02 14:54 ` [PATCH 4/4] btrfs: remove redudant record start offset check " fdmanana
2025-04-02 22:56 ` [PATCH 0/4] btrfs: some more cleanups for the io tree code David Sterba
4 siblings, 0 replies; 6+ messages in thread
From: fdmanana @ 2025-04-02 14:54 UTC (permalink / raw)
To: linux-btrfs
From: Filipe Manana <fdmanana@suse.com>
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").
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
fs/btrfs/extent-io-tree.c | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/fs/btrfs/extent-io-tree.c b/fs/btrfs/extent-io-tree.c
index 18b10e7ed815..b321f826d008 100644
--- a/fs/btrfs/extent-io-tree.c
+++ b/fs/btrfs/extent-io-tree.c
@@ -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);
}
--
2.45.2
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH 4/4] btrfs: remove redudant record start offset check at test_range_bit()
2025-04-02 14:54 [PATCH 0/4] btrfs: some more cleanups for the io tree code fdmanana
` (2 preceding siblings ...)
2025-04-02 14:54 ` [PATCH 3/4] btrfs: simplify last record detection at test_range_bit() fdmanana
@ 2025-04-02 14:54 ` fdmanana
2025-04-02 22:56 ` [PATCH 0/4] btrfs: some more cleanups for the io tree code David Sterba
4 siblings, 0 replies; 6+ messages in thread
From: fdmanana @ 2025-04-02 14:54 UTC (permalink / raw)
To: linux-btrfs
From: Filipe Manana <fdmanana@suse.com>
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.
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
fs/btrfs/extent-io-tree.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/fs/btrfs/extent-io-tree.c b/fs/btrfs/extent-io-tree.c
index b321f826d008..d833ab2d69a1 100644
--- a/fs/btrfs/extent-io-tree.c
+++ b/fs/btrfs/extent-io-tree.c
@@ -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;
--
2.45.2
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH 0/4] btrfs: some more cleanups for the io tree code
2025-04-02 14:54 [PATCH 0/4] btrfs: some more cleanups for the io tree code fdmanana
` (3 preceding siblings ...)
2025-04-02 14:54 ` [PATCH 4/4] btrfs: remove redudant record start offset check " fdmanana
@ 2025-04-02 22:56 ` David Sterba
4 siblings, 0 replies; 6+ messages in thread
From: David Sterba @ 2025-04-02 22:56 UTC (permalink / raw)
To: fdmanana; +Cc: linux-btrfs
On Wed, Apr 02, 2025 at 03:54:08PM +0100, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
>
> 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()
Reviewed-by: David Sterba <dsterba@suse.com>
^ permalink raw reply [flat|nested] 6+ messages in thread