diff --git a/fs/btrfs/extent-io-tree.c b/fs/btrfs/extent-io-tree.c index 689ba1a2b2d5..dd36322fbf6b 100644 --- a/fs/btrfs/extent-io-tree.c +++ b/fs/btrfs/extent-io-tree.c @@ -1226,6 +1226,22 @@ static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, if (inserted_state == prealloc) prealloc = NULL; start = inserted_state->end + 1; + + /* Beyond target range, stop. */ + if (start > end) + goto out; + + if (need_resched()) + goto search_again; + + state = next_search_state(inserted_state, end); + /* + * If there's a next state, whether contiguous or not, we don't + * need to unlock and start search agian. If it's not contiguous + * we will end up here and try to allocate a prealloc state and insert. + */ + if (state) + goto hit_next; goto search_again; } /*