1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/slab.h>
4 #include <trace/events/btrfs.h>
6 #include "extent-io-tree.h"
7 #include "btrfs_inode.h"
10 static struct kmem_cache *extent_state_cache;
12 static inline bool extent_state_in_tree(const struct extent_state *state)
14 return !RB_EMPTY_NODE(&state->rb_node);
17 #ifdef CONFIG_BTRFS_DEBUG
18 static LIST_HEAD(states);
19 static DEFINE_SPINLOCK(leak_lock);
21 static inline void btrfs_leak_debug_add_state(struct extent_state *state)
25 spin_lock_irqsave(&leak_lock, flags);
26 list_add(&state->leak_list, &states);
27 spin_unlock_irqrestore(&leak_lock, flags);
30 static inline void btrfs_leak_debug_del_state(struct extent_state *state)
34 spin_lock_irqsave(&leak_lock, flags);
35 list_del(&state->leak_list);
36 spin_unlock_irqrestore(&leak_lock, flags);
39 static inline void btrfs_extent_state_leak_debug_check(void)
41 struct extent_state *state;
43 while (!list_empty(&states)) {
44 state = list_entry(states.next, struct extent_state, leak_list);
45 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
46 state->start, state->end, state->state,
47 extent_state_in_tree(state),
48 refcount_read(&state->refs));
49 list_del(&state->leak_list);
50 kmem_cache_free(extent_state_cache, state);
54 #define btrfs_debug_check_extent_io_range(tree, start, end) \
55 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
56 static inline void __btrfs_debug_check_extent_io_range(const char *caller,
57 struct extent_io_tree *tree,
60 struct inode *inode = tree->private_data;
66 isize = i_size_read(inode);
67 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
68 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
69 "%s: ino %llu isize %llu odd range [%llu,%llu]",
70 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
74 #define btrfs_leak_debug_add_state(state) do {} while (0)
75 #define btrfs_leak_debug_del_state(state) do {} while (0)
76 #define btrfs_extent_state_leak_debug_check() do {} while (0)
77 #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
81 * For the file_extent_tree, we want to hold the inode lock when we lookup and
82 * update the disk_i_size, but lockdep will complain because our io_tree we hold
83 * the tree lock and get the inode lock when setting delalloc. These two things
84 * are unrelated, so make a class for the file_extent_tree so we don't get the
85 * two locking patterns mixed up.
87 static struct lock_class_key file_extent_tree_class;
92 struct rb_node rb_node;
95 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
96 struct extent_io_tree *tree, unsigned int owner,
99 tree->fs_info = fs_info;
100 tree->state = RB_ROOT;
101 spin_lock_init(&tree->lock);
102 tree->private_data = private_data;
104 if (owner == IO_TREE_INODE_FILE_EXTENT)
105 lockdep_set_class(&tree->lock, &file_extent_tree_class);
108 void extent_io_tree_release(struct extent_io_tree *tree)
110 spin_lock(&tree->lock);
112 * Do a single barrier for the waitqueue_active check here, the state
113 * of the waitqueue should not change once extent_io_tree_release is
117 while (!RB_EMPTY_ROOT(&tree->state)) {
118 struct rb_node *node;
119 struct extent_state *state;
121 node = rb_first(&tree->state);
122 state = rb_entry(node, struct extent_state, rb_node);
123 rb_erase(&state->rb_node, &tree->state);
124 RB_CLEAR_NODE(&state->rb_node);
126 * btree io trees aren't supposed to have tasks waiting for
127 * changes in the flags of extent states ever.
129 ASSERT(!waitqueue_active(&state->wq));
130 free_extent_state(state);
132 cond_resched_lock(&tree->lock);
134 spin_unlock(&tree->lock);
137 static struct extent_state *alloc_extent_state(gfp_t mask)
139 struct extent_state *state;
142 * The given mask might be not appropriate for the slab allocator,
143 * drop the unsupported bits
145 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
146 state = kmem_cache_alloc(extent_state_cache, mask);
150 RB_CLEAR_NODE(&state->rb_node);
151 btrfs_leak_debug_add_state(state);
152 refcount_set(&state->refs, 1);
153 init_waitqueue_head(&state->wq);
154 trace_alloc_extent_state(state, mask, _RET_IP_);
158 static struct extent_state *alloc_extent_state_atomic(struct extent_state *prealloc)
161 prealloc = alloc_extent_state(GFP_ATOMIC);
166 void free_extent_state(struct extent_state *state)
170 if (refcount_dec_and_test(&state->refs)) {
171 WARN_ON(extent_state_in_tree(state));
172 btrfs_leak_debug_del_state(state);
173 trace_free_extent_state(state, _RET_IP_);
174 kmem_cache_free(extent_state_cache, state);
178 static int add_extent_changeset(struct extent_state *state, u32 bits,
179 struct extent_changeset *changeset,
186 if (set && (state->state & bits) == bits)
188 if (!set && (state->state & bits) == 0)
190 changeset->bytes_changed += state->end - state->start + 1;
191 ret = ulist_add(&changeset->range_changed, state->start, state->end,
196 static inline struct extent_state *next_state(struct extent_state *state)
198 struct rb_node *next = rb_next(&state->rb_node);
201 return rb_entry(next, struct extent_state, rb_node);
206 static inline struct extent_state *prev_state(struct extent_state *state)
208 struct rb_node *next = rb_prev(&state->rb_node);
211 return rb_entry(next, struct extent_state, rb_node);
217 * Search @tree for an entry that contains @offset. Such entry would have
218 * entry->start <= offset && entry->end >= offset.
220 * @tree: the tree to search
221 * @offset: offset that should fall within an entry in @tree
222 * @node_ret: pointer where new node should be anchored (used when inserting an
224 * @parent_ret: points to entry which would have been the parent of the entry,
227 * Return a pointer to the entry that contains @offset byte address and don't change
228 * @node_ret and @parent_ret.
230 * If no such entry exists, return pointer to entry that ends before @offset
231 * and fill parameters @node_ret and @parent_ret, ie. does not return NULL.
233 static inline struct extent_state *tree_search_for_insert(struct extent_io_tree *tree,
235 struct rb_node ***node_ret,
236 struct rb_node **parent_ret)
238 struct rb_root *root = &tree->state;
239 struct rb_node **node = &root->rb_node;
240 struct rb_node *prev = NULL;
241 struct extent_state *entry = NULL;
245 entry = rb_entry(prev, struct extent_state, rb_node);
247 if (offset < entry->start)
248 node = &(*node)->rb_left;
249 else if (offset > entry->end)
250 node = &(*node)->rb_right;
260 /* Search neighbors until we find the first one past the end */
261 while (entry && offset > entry->end)
262 entry = next_state(entry);
268 * Search offset in the tree or fill neighbor rbtree node pointers.
270 * @tree: the tree to search
271 * @offset: offset that should fall within an entry in @tree
272 * @next_ret: pointer to the first entry whose range ends after @offset
273 * @prev_ret: pointer to the first entry whose range begins before @offset
275 * Return a pointer to the entry that contains @offset byte address. If no
276 * such entry exists, then return NULL and fill @prev_ret and @next_ret.
277 * Otherwise return the found entry and other pointers are left untouched.
279 static struct extent_state *tree_search_prev_next(struct extent_io_tree *tree,
281 struct extent_state **prev_ret,
282 struct extent_state **next_ret)
284 struct rb_root *root = &tree->state;
285 struct rb_node **node = &root->rb_node;
286 struct extent_state *orig_prev;
287 struct extent_state *entry = NULL;
293 entry = rb_entry(*node, struct extent_state, rb_node);
295 if (offset < entry->start)
296 node = &(*node)->rb_left;
297 else if (offset > entry->end)
298 node = &(*node)->rb_right;
304 while (entry && offset > entry->end)
305 entry = next_state(entry);
309 while (entry && offset < entry->start)
310 entry = prev_state(entry);
317 * Inexact rb-tree search, return the next entry if @offset is not found
319 static inline struct extent_state *tree_search(struct extent_io_tree *tree, u64 offset)
321 return tree_search_for_insert(tree, offset, NULL, NULL);
324 static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
326 btrfs_panic(tree->fs_info, err,
327 "locking error: extent tree was modified by another thread while locked");
331 * Utility function to look for merge candidates inside a given range. Any
332 * extents with matching state are merged together into a single extent in the
333 * tree. Extents with EXTENT_IO in their state field are not merged because
334 * the end_io handlers need to be able to do operations on them without
335 * sleeping (or doing allocations/splits).
337 * This should be called with the tree lock held.
339 static void merge_state(struct extent_io_tree *tree, struct extent_state *state)
341 struct extent_state *other;
343 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
346 other = prev_state(state);
347 if (other && other->end == state->start - 1 &&
348 other->state == state->state) {
349 if (tree->private_data)
350 btrfs_merge_delalloc_extent(tree->private_data,
352 state->start = other->start;
353 rb_erase(&other->rb_node, &tree->state);
354 RB_CLEAR_NODE(&other->rb_node);
355 free_extent_state(other);
357 other = next_state(state);
358 if (other && other->start == state->end + 1 &&
359 other->state == state->state) {
360 if (tree->private_data)
361 btrfs_merge_delalloc_extent(tree->private_data, state,
363 state->end = other->end;
364 rb_erase(&other->rb_node, &tree->state);
365 RB_CLEAR_NODE(&other->rb_node);
366 free_extent_state(other);
370 static void set_state_bits(struct extent_io_tree *tree,
371 struct extent_state *state,
372 u32 bits, struct extent_changeset *changeset)
374 u32 bits_to_set = bits & ~EXTENT_CTLBITS;
377 if (tree->private_data)
378 btrfs_set_delalloc_extent(tree->private_data, state, bits);
380 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
382 state->state |= bits_to_set;
386 * Insert an extent_state struct into the tree. 'bits' are set on the
387 * struct before it is inserted.
389 * This may return -EEXIST if the extent is already there, in which case the
390 * state struct is freed.
392 * The tree lock is not taken internally. This is a utility function and
393 * probably isn't what you want to call (see set/clear_extent_bit).
395 static int insert_state(struct extent_io_tree *tree,
396 struct extent_state *state,
397 u32 bits, struct extent_changeset *changeset)
399 struct rb_node **node;
400 struct rb_node *parent = NULL;
401 const u64 end = state->end;
403 set_state_bits(tree, state, bits, changeset);
405 node = &tree->state.rb_node;
407 struct extent_state *entry;
410 entry = rb_entry(parent, struct extent_state, rb_node);
412 if (end < entry->start) {
413 node = &(*node)->rb_left;
414 } else if (end > entry->end) {
415 node = &(*node)->rb_right;
417 btrfs_err(tree->fs_info,
418 "found node %llu %llu on insert of %llu %llu",
419 entry->start, entry->end, state->start, end);
424 rb_link_node(&state->rb_node, parent, node);
425 rb_insert_color(&state->rb_node, &tree->state);
427 merge_state(tree, state);
432 * Insert state to @tree to the location given by @node and @parent.
434 static void insert_state_fast(struct extent_io_tree *tree,
435 struct extent_state *state, struct rb_node **node,
436 struct rb_node *parent, unsigned bits,
437 struct extent_changeset *changeset)
439 set_state_bits(tree, state, bits, changeset);
440 rb_link_node(&state->rb_node, parent, node);
441 rb_insert_color(&state->rb_node, &tree->state);
442 merge_state(tree, state);
446 * Split a given extent state struct in two, inserting the preallocated
447 * struct 'prealloc' as the newly created second half. 'split' indicates an
448 * offset inside 'orig' where it should be split.
451 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
452 * are two extent state structs in the tree:
453 * prealloc: [orig->start, split - 1]
454 * orig: [ split, orig->end ]
456 * The tree locks are not taken by this function. They need to be held
459 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
460 struct extent_state *prealloc, u64 split)
462 struct rb_node *parent = NULL;
463 struct rb_node **node;
465 if (tree->private_data)
466 btrfs_split_delalloc_extent(tree->private_data, orig, split);
468 prealloc->start = orig->start;
469 prealloc->end = split - 1;
470 prealloc->state = orig->state;
473 parent = &orig->rb_node;
476 struct extent_state *entry;
479 entry = rb_entry(parent, struct extent_state, rb_node);
481 if (prealloc->end < entry->start) {
482 node = &(*node)->rb_left;
483 } else if (prealloc->end > entry->end) {
484 node = &(*node)->rb_right;
486 free_extent_state(prealloc);
491 rb_link_node(&prealloc->rb_node, parent, node);
492 rb_insert_color(&prealloc->rb_node, &tree->state);
498 * Utility function to clear some bits in an extent state struct. It will
499 * optionally wake up anyone waiting on this state (wake == 1).
501 * If no bits are set on the state struct after clearing things, the
502 * struct is freed and removed from the tree
504 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
505 struct extent_state *state,
507 struct extent_changeset *changeset)
509 struct extent_state *next;
510 u32 bits_to_clear = bits & ~EXTENT_CTLBITS;
513 if (tree->private_data)
514 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
516 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
518 state->state &= ~bits_to_clear;
521 if (state->state == 0) {
522 next = next_state(state);
523 if (extent_state_in_tree(state)) {
524 rb_erase(&state->rb_node, &tree->state);
525 RB_CLEAR_NODE(&state->rb_node);
526 free_extent_state(state);
531 merge_state(tree, state);
532 next = next_state(state);
538 * Clear some bits on a range in the tree. This may require splitting or
539 * inserting elements in the tree, so the gfp mask is used to indicate which
540 * allocations or sleeping are allowed.
542 * Pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove the given
543 * range from the tree regardless of state (ie for truncate).
545 * The range [start, end] is inclusive.
547 * This takes the tree lock, and returns 0 on success and < 0 on error.
549 int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
550 u32 bits, struct extent_state **cached_state,
551 gfp_t mask, struct extent_changeset *changeset)
553 struct extent_state *state;
554 struct extent_state *cached;
555 struct extent_state *prealloc = NULL;
560 int delete = (bits & EXTENT_CLEAR_ALL_BITS);
562 btrfs_debug_check_extent_io_range(tree, start, end);
563 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
566 bits |= ~EXTENT_CTLBITS;
568 if (bits & EXTENT_DELALLOC)
569 bits |= EXTENT_NORESERVE;
571 wake = (bits & EXTENT_LOCKED) ? 1 : 0;
572 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
577 * Don't care for allocation failure here because we might end
578 * up not needing the pre-allocated extent state at all, which
579 * is the case if we only have in the tree extent states that
580 * cover our input range and don't cover too any other range.
581 * If we end up needing a new extent state we allocate it later.
583 prealloc = alloc_extent_state(mask);
586 spin_lock(&tree->lock);
588 cached = *cached_state;
591 *cached_state = NULL;
595 if (cached && extent_state_in_tree(cached) &&
596 cached->start <= start && cached->end > start) {
598 refcount_dec(&cached->refs);
603 free_extent_state(cached);
606 /* This search will find the extents that end after our range starts. */
607 state = tree_search(tree, start);
611 if (state->start > end)
613 WARN_ON(state->end < start);
614 last_end = state->end;
616 /* The state doesn't have the wanted bits, go ahead. */
617 if (!(state->state & bits)) {
618 state = next_state(state);
623 * | ---- desired range ---- |
625 * | ------------- state -------------- |
627 * We need to split the extent we found, and may flip bits on second
630 * If the extent we found extends past our range, we just split and
631 * search again. It'll get split again the next time though.
633 * If the extent we found is inside our range, we clear the desired bit
637 if (state->start < start) {
638 prealloc = alloc_extent_state_atomic(prealloc);
641 err = split_state(tree, state, prealloc, start);
643 extent_io_tree_panic(tree, err);
648 if (state->end <= end) {
649 state = clear_state_bit(tree, state, bits, wake, changeset);
655 * | ---- desired range ---- |
657 * We need to split the extent, and clear the bit on the first half.
659 if (state->start <= end && state->end > end) {
660 prealloc = alloc_extent_state_atomic(prealloc);
663 err = split_state(tree, state, prealloc, end + 1);
665 extent_io_tree_panic(tree, err);
670 clear_state_bit(tree, prealloc, bits, wake, changeset);
676 state = clear_state_bit(tree, state, bits, wake, changeset);
678 if (last_end == (u64)-1)
680 start = last_end + 1;
681 if (start <= end && state && !need_resched())
687 spin_unlock(&tree->lock);
688 if (gfpflags_allow_blocking(mask))
693 spin_unlock(&tree->lock);
695 free_extent_state(prealloc);
701 static void wait_on_state(struct extent_io_tree *tree,
702 struct extent_state *state)
703 __releases(tree->lock)
704 __acquires(tree->lock)
707 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
708 spin_unlock(&tree->lock);
710 spin_lock(&tree->lock);
711 finish_wait(&state->wq, &wait);
715 * Wait for one or more bits to clear on a range in the state tree.
716 * The range [start, end] is inclusive.
717 * The tree lock is taken by this function
719 void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits)
721 struct extent_state *state;
723 btrfs_debug_check_extent_io_range(tree, start, end);
725 spin_lock(&tree->lock);
729 * This search will find all the extents that end after our
732 state = tree_search(tree, start);
736 if (state->start > end)
739 if (state->state & bits) {
740 start = state->start;
741 refcount_inc(&state->refs);
742 wait_on_state(tree, state);
743 free_extent_state(state);
746 start = state->end + 1;
751 if (!cond_resched_lock(&tree->lock)) {
752 state = next_state(state);
757 spin_unlock(&tree->lock);
760 static void cache_state_if_flags(struct extent_state *state,
761 struct extent_state **cached_ptr,
764 if (cached_ptr && !(*cached_ptr)) {
765 if (!flags || (state->state & flags)) {
767 refcount_inc(&state->refs);
772 static void cache_state(struct extent_state *state,
773 struct extent_state **cached_ptr)
775 return cache_state_if_flags(state, cached_ptr,
776 EXTENT_LOCKED | EXTENT_BOUNDARY);
780 * Find the first state struct with 'bits' set after 'start', and return it.
781 * tree->lock must be held. NULL will returned if nothing was found after
784 static struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
787 struct extent_state *state;
790 * This search will find all the extents that end after our range
793 state = tree_search(tree, start);
795 if (state->end >= start && (state->state & bits))
797 state = next_state(state);
803 * Find the first offset in the io tree with one or more @bits set.
805 * Note: If there are multiple bits set in @bits, any of them will match.
807 * Return 0 if we find something, and update @start_ret and @end_ret.
808 * Return 1 if we found nothing.
810 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
811 u64 *start_ret, u64 *end_ret, u32 bits,
812 struct extent_state **cached_state)
814 struct extent_state *state;
817 spin_lock(&tree->lock);
818 if (cached_state && *cached_state) {
819 state = *cached_state;
820 if (state->end == start - 1 && extent_state_in_tree(state)) {
821 while ((state = next_state(state)) != NULL) {
822 if (state->state & bits)
825 free_extent_state(*cached_state);
826 *cached_state = NULL;
829 free_extent_state(*cached_state);
830 *cached_state = NULL;
833 state = find_first_extent_bit_state(tree, start, bits);
836 cache_state_if_flags(state, cached_state, 0);
837 *start_ret = state->start;
838 *end_ret = state->end;
842 spin_unlock(&tree->lock);
847 * Find a contiguous area of bits
849 * @tree: io tree to check
850 * @start: offset to start the search from
851 * @start_ret: the first offset we found with the bits set
852 * @end_ret: the final contiguous range of the bits that were set
853 * @bits: bits to look for
855 * set_extent_bit and clear_extent_bit can temporarily split contiguous ranges
856 * to set bits appropriately, and then merge them again. During this time it
857 * will drop the tree->lock, so use this helper if you want to find the actual
858 * contiguous area for given bits. We will search to the first bit we find, and
859 * then walk down the tree until we find a non-contiguous area. The area
860 * returned will be the full contiguous area with the bits set.
862 int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
863 u64 *start_ret, u64 *end_ret, u32 bits)
865 struct extent_state *state;
868 spin_lock(&tree->lock);
869 state = find_first_extent_bit_state(tree, start, bits);
871 *start_ret = state->start;
872 *end_ret = state->end;
873 while ((state = next_state(state)) != NULL) {
874 if (state->start > (*end_ret + 1))
876 *end_ret = state->end;
880 spin_unlock(&tree->lock);
885 * Find a contiguous range of bytes in the file marked as delalloc, not more
886 * than 'max_bytes'. start and end are used to return the range,
888 * True is returned if we find something, false if nothing was in the tree.
890 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
891 u64 *end, u64 max_bytes,
892 struct extent_state **cached_state)
894 struct extent_state *state;
895 u64 cur_start = *start;
899 spin_lock(&tree->lock);
902 * This search will find all the extents that end after our range
905 state = tree_search(tree, cur_start);
912 if (found && (state->start != cur_start ||
913 (state->state & EXTENT_BOUNDARY))) {
916 if (!(state->state & EXTENT_DELALLOC)) {
922 *start = state->start;
923 *cached_state = state;
924 refcount_inc(&state->refs);
928 cur_start = state->end + 1;
929 total_bytes += state->end - state->start + 1;
930 if (total_bytes >= max_bytes)
932 state = next_state(state);
935 spin_unlock(&tree->lock);
940 * Set some bits on a range in the tree. This may require allocations or
941 * sleeping, so the gfp mask is used to indicate what is allowed.
943 * If any of the exclusive bits are set, this will fail with -EEXIST if some
944 * part of the range already has the desired bits set. The start of the
945 * existing range is returned in failed_start in this case.
947 * [start, end] is inclusive This takes the tree lock.
949 static int __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
950 u32 bits, u64 *failed_start,
951 struct extent_state **cached_state,
952 struct extent_changeset *changeset, gfp_t mask)
954 struct extent_state *state;
955 struct extent_state *prealloc = NULL;
957 struct rb_node *parent;
961 u32 exclusive_bits = (bits & EXTENT_LOCKED);
963 btrfs_debug_check_extent_io_range(tree, start, end);
964 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
967 ASSERT(failed_start);
969 ASSERT(failed_start == NULL);
973 * Don't care for allocation failure here because we might end
974 * up not needing the pre-allocated extent state at all, which
975 * is the case if we only have in the tree extent states that
976 * cover our input range and don't cover too any other range.
977 * If we end up needing a new extent state we allocate it later.
979 prealloc = alloc_extent_state(mask);
982 spin_lock(&tree->lock);
983 if (cached_state && *cached_state) {
984 state = *cached_state;
985 if (state->start <= start && state->end > start &&
986 extent_state_in_tree(state))
990 * This search will find all the extents that end after our range
993 state = tree_search_for_insert(tree, start, &p, &parent);
995 prealloc = alloc_extent_state_atomic(prealloc);
998 prealloc->start = start;
1000 insert_state_fast(tree, prealloc, p, parent, bits, changeset);
1001 cache_state(prealloc, cached_state);
1006 last_start = state->start;
1007 last_end = state->end;
1010 * | ---- desired range ---- |
1013 * Just lock what we found and keep going
1015 if (state->start == start && state->end <= end) {
1016 if (state->state & exclusive_bits) {
1017 *failed_start = state->start;
1022 set_state_bits(tree, state, bits, changeset);
1023 cache_state(state, cached_state);
1024 merge_state(tree, state);
1025 if (last_end == (u64)-1)
1027 start = last_end + 1;
1028 state = next_state(state);
1029 if (start < end && state && state->start == start &&
1036 * | ---- desired range ---- |
1039 * | ------------- state -------------- |
1041 * We need to split the extent we found, and may flip bits on second
1044 * If the extent we found extends past our range, we just split and
1045 * search again. It'll get split again the next time though.
1047 * If the extent we found is inside our range, we set the desired bit
1050 if (state->start < start) {
1051 if (state->state & exclusive_bits) {
1052 *failed_start = start;
1058 * If this extent already has all the bits we want set, then
1059 * skip it, not necessary to split it or do anything with it.
1061 if ((state->state & bits) == bits) {
1062 start = state->end + 1;
1063 cache_state(state, cached_state);
1067 prealloc = alloc_extent_state_atomic(prealloc);
1070 err = split_state(tree, state, prealloc, start);
1072 extent_io_tree_panic(tree, err);
1077 if (state->end <= end) {
1078 set_state_bits(tree, state, bits, changeset);
1079 cache_state(state, cached_state);
1080 merge_state(tree, state);
1081 if (last_end == (u64)-1)
1083 start = last_end + 1;
1084 state = next_state(state);
1085 if (start < end && state && state->start == start &&
1092 * | ---- desired range ---- |
1093 * | state | or | state |
1095 * There's a hole, we need to insert something in it and ignore the
1098 if (state->start > start) {
1100 if (end < last_start)
1103 this_end = last_start - 1;
1105 prealloc = alloc_extent_state_atomic(prealloc);
1110 * Avoid to free 'prealloc' if it can be merged with the later
1113 prealloc->start = start;
1114 prealloc->end = this_end;
1115 err = insert_state(tree, prealloc, bits, changeset);
1117 extent_io_tree_panic(tree, err);
1119 cache_state(prealloc, cached_state);
1121 start = this_end + 1;
1125 * | ---- desired range ---- |
1128 * We need to split the extent, and set the bit on the first half
1130 if (state->start <= end && state->end > end) {
1131 if (state->state & exclusive_bits) {
1132 *failed_start = start;
1137 prealloc = alloc_extent_state_atomic(prealloc);
1140 err = split_state(tree, state, prealloc, end + 1);
1142 extent_io_tree_panic(tree, err);
1144 set_state_bits(tree, prealloc, bits, changeset);
1145 cache_state(prealloc, cached_state);
1146 merge_state(tree, prealloc);
1154 spin_unlock(&tree->lock);
1155 if (gfpflags_allow_blocking(mask))
1160 spin_unlock(&tree->lock);
1162 free_extent_state(prealloc);
1168 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1169 u32 bits, struct extent_state **cached_state, gfp_t mask)
1171 return __set_extent_bit(tree, start, end, bits, NULL, cached_state,
1176 * Convert all bits in a given range from one bit to another
1178 * @tree: the io tree to search
1179 * @start: the start offset in bytes
1180 * @end: the end offset in bytes (inclusive)
1181 * @bits: the bits to set in this range
1182 * @clear_bits: the bits to clear in this range
1183 * @cached_state: state that we're going to cache
1185 * This will go through and set bits for the given range. If any states exist
1186 * already in this range they are set with the given bit and cleared of the
1187 * clear_bits. This is only meant to be used by things that are mergeable, ie.
1188 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1189 * boundary bits like LOCK.
1191 * All allocations are done with GFP_NOFS.
1193 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1194 u32 bits, u32 clear_bits,
1195 struct extent_state **cached_state)
1197 struct extent_state *state;
1198 struct extent_state *prealloc = NULL;
1200 struct rb_node *parent;
1204 bool first_iteration = true;
1206 btrfs_debug_check_extent_io_range(tree, start, end);
1207 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1213 * Best effort, don't worry if extent state allocation fails
1214 * here for the first iteration. We might have a cached state
1215 * that matches exactly the target range, in which case no
1216 * extent state allocations are needed. We'll only know this
1217 * after locking the tree.
1219 prealloc = alloc_extent_state(GFP_NOFS);
1220 if (!prealloc && !first_iteration)
1224 spin_lock(&tree->lock);
1225 if (cached_state && *cached_state) {
1226 state = *cached_state;
1227 if (state->start <= start && state->end > start &&
1228 extent_state_in_tree(state))
1233 * This search will find all the extents that end after our range
1236 state = tree_search_for_insert(tree, start, &p, &parent);
1238 prealloc = alloc_extent_state_atomic(prealloc);
1243 prealloc->start = start;
1244 prealloc->end = end;
1245 insert_state_fast(tree, prealloc, p, parent, bits, NULL);
1246 cache_state(prealloc, cached_state);
1251 last_start = state->start;
1252 last_end = state->end;
1255 * | ---- desired range ---- |
1258 * Just lock what we found and keep going.
1260 if (state->start == start && state->end <= end) {
1261 set_state_bits(tree, state, bits, NULL);
1262 cache_state(state, cached_state);
1263 state = clear_state_bit(tree, state, clear_bits, 0, NULL);
1264 if (last_end == (u64)-1)
1266 start = last_end + 1;
1267 if (start < end && state && state->start == start &&
1274 * | ---- desired range ---- |
1277 * | ------------- state -------------- |
1279 * We need to split the extent we found, and may flip bits on second
1282 * If the extent we found extends past our range, we just split and
1283 * search again. It'll get split again the next time though.
1285 * If the extent we found is inside our range, we set the desired bit
1288 if (state->start < start) {
1289 prealloc = alloc_extent_state_atomic(prealloc);
1294 err = split_state(tree, state, prealloc, start);
1296 extent_io_tree_panic(tree, err);
1300 if (state->end <= end) {
1301 set_state_bits(tree, state, bits, NULL);
1302 cache_state(state, cached_state);
1303 state = clear_state_bit(tree, state, clear_bits, 0, NULL);
1304 if (last_end == (u64)-1)
1306 start = last_end + 1;
1307 if (start < end && state && state->start == start &&
1314 * | ---- desired range ---- |
1315 * | state | or | state |
1317 * There's a hole, we need to insert something in it and ignore the
1320 if (state->start > start) {
1322 if (end < last_start)
1325 this_end = last_start - 1;
1327 prealloc = alloc_extent_state_atomic(prealloc);
1334 * Avoid to free 'prealloc' if it can be merged with the later
1337 prealloc->start = start;
1338 prealloc->end = this_end;
1339 err = insert_state(tree, prealloc, bits, NULL);
1341 extent_io_tree_panic(tree, err);
1342 cache_state(prealloc, cached_state);
1344 start = this_end + 1;
1348 * | ---- desired range ---- |
1351 * We need to split the extent, and set the bit on the first half.
1353 if (state->start <= end && state->end > end) {
1354 prealloc = alloc_extent_state_atomic(prealloc);
1360 err = split_state(tree, state, prealloc, end + 1);
1362 extent_io_tree_panic(tree, err);
1364 set_state_bits(tree, prealloc, bits, NULL);
1365 cache_state(prealloc, cached_state);
1366 clear_state_bit(tree, prealloc, clear_bits, 0, NULL);
1374 spin_unlock(&tree->lock);
1376 first_iteration = false;
1380 spin_unlock(&tree->lock);
1382 free_extent_state(prealloc);
1388 * Find the first range that has @bits not set. This range could start before
1391 * @tree: the tree to search
1392 * @start: offset at/after which the found extent should start
1393 * @start_ret: records the beginning of the range
1394 * @end_ret: records the end of the range (inclusive)
1395 * @bits: the set of bits which must be unset
1397 * Since unallocated range is also considered one which doesn't have the bits
1398 * set it's possible that @end_ret contains -1, this happens in case the range
1399 * spans (last_range_end, end of device]. In this case it's up to the caller to
1400 * trim @end_ret to the appropriate size.
1402 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
1403 u64 *start_ret, u64 *end_ret, u32 bits)
1405 struct extent_state *state;
1406 struct extent_state *prev = NULL, *next;
1408 spin_lock(&tree->lock);
1410 /* Find first extent with bits cleared */
1412 state = tree_search_prev_next(tree, start, &prev, &next);
1413 if (!state && !next && !prev) {
1415 * Tree is completely empty, send full range and let
1416 * caller deal with it
1421 } else if (!state && !next) {
1423 * We are past the last allocated chunk, set start at
1424 * the end of the last extent.
1426 *start_ret = prev->end + 1;
1429 } else if (!state) {
1434 * At this point 'state' either contains 'start' or start is
1437 if (in_range(start, state->start, state->end - state->start + 1)) {
1438 if (state->state & bits) {
1440 * |--range with bits sets--|
1444 start = state->end + 1;
1447 * 'start' falls within a range that doesn't
1448 * have the bits set, so take its start as the
1449 * beginning of the desired range
1451 * |--range with bits cleared----|
1455 *start_ret = state->start;
1460 * |---prev range---|---hole/unset---|---node range---|
1466 * |---hole/unset--||--first node--|
1471 *start_ret = prev->end + 1;
1479 * Find the longest stretch from start until an entry which has the
1483 if (state->end >= start && !(state->state & bits)) {
1484 *end_ret = state->end;
1486 *end_ret = state->start - 1;
1489 state = next_state(state);
1492 spin_unlock(&tree->lock);
1496 * Count the number of bytes in the tree that have a given bit(s) set. This
1497 * can be fairly slow, except for EXTENT_DIRTY which is cached. The total
1498 * number found is returned.
1500 u64 count_range_bits(struct extent_io_tree *tree,
1501 u64 *start, u64 search_end, u64 max_bytes,
1502 u32 bits, int contig)
1504 struct extent_state *state;
1505 u64 cur_start = *start;
1506 u64 total_bytes = 0;
1510 if (WARN_ON(search_end < cur_start))
1513 spin_lock(&tree->lock);
1516 * This search will find all the extents that end after our range
1519 state = tree_search(tree, cur_start);
1521 if (state->start > search_end)
1523 if (contig && found && state->start > last + 1)
1525 if (state->end >= cur_start && (state->state & bits) == bits) {
1526 total_bytes += min(search_end, state->end) + 1 -
1527 max(cur_start, state->start);
1528 if (total_bytes >= max_bytes)
1531 *start = max(cur_start, state->start);
1535 } else if (contig && found) {
1538 state = next_state(state);
1540 spin_unlock(&tree->lock);
1545 * Searche a range in the state tree for a given mask. If 'filled' == 1, this
1546 * returns 1 only if every extent in the tree has the bits set. Otherwise, 1
1547 * is returned if any bit in the range is found set.
1549 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1550 u32 bits, int filled, struct extent_state *cached)
1552 struct extent_state *state = NULL;
1555 spin_lock(&tree->lock);
1556 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
1557 cached->end > start)
1560 state = tree_search(tree, start);
1561 while (state && start <= end) {
1562 if (filled && state->start > start) {
1567 if (state->start > end)
1570 if (state->state & bits) {
1574 } else if (filled) {
1579 if (state->end == (u64)-1)
1582 start = state->end + 1;
1585 state = next_state(state);
1588 /* We ran out of states and were still inside of our range. */
1589 if (filled && !state)
1591 spin_unlock(&tree->lock);
1595 /* Wrappers around set/clear extent bit */
1596 int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1597 u32 bits, struct extent_changeset *changeset)
1600 * We don't support EXTENT_LOCKED yet, as current changeset will
1601 * record any bits changed, so for EXTENT_LOCKED case, it will
1602 * either fail with -EEXIST or changeset will record the whole
1605 ASSERT(!(bits & EXTENT_LOCKED));
1607 return __set_extent_bit(tree, start, end, bits, NULL, NULL, changeset,
1611 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1612 u32 bits, struct extent_changeset *changeset)
1615 * Don't support EXTENT_LOCKED case, same reason as
1616 * set_record_extent_bits().
1618 ASSERT(!(bits & EXTENT_LOCKED));
1620 return __clear_extent_bit(tree, start, end, bits, NULL, GFP_NOFS,
1624 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1629 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, &failed_start,
1630 NULL, NULL, GFP_NOFS);
1631 if (err == -EEXIST) {
1632 if (failed_start > start)
1633 clear_extent_bit(tree, start, failed_start - 1,
1634 EXTENT_LOCKED, NULL);
1641 * Either insert or lock state struct between start and end use mask to tell
1642 * us if waiting is desired.
1644 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1645 struct extent_state **cached_state)
1650 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, &failed_start,
1651 cached_state, NULL, GFP_NOFS);
1652 while (err == -EEXIST) {
1653 if (failed_start != start)
1654 clear_extent_bit(tree, start, failed_start - 1,
1655 EXTENT_LOCKED, cached_state);
1657 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1658 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
1659 &failed_start, cached_state, NULL,
1665 void __cold extent_state_free_cachep(void)
1667 btrfs_extent_state_leak_debug_check();
1668 kmem_cache_destroy(extent_state_cache);
1671 int __init extent_state_init_cachep(void)
1673 extent_state_cache = kmem_cache_create("btrfs_extent_state",
1674 sizeof(struct extent_state), 0,
1675 SLAB_MEM_SPREAD, NULL);
1676 if (!extent_state_cache)