xref: /openbmc/linux/fs/btrfs/extent-io-tree.c (revision c4a7b9b5)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/slab.h>
4 #include <trace/events/btrfs.h>
5 #include "ctree.h"
6 #include "extent-io-tree.h"
7 #include "btrfs_inode.h"
8 #include "misc.h"
9 
10 static struct kmem_cache *extent_state_cache;
11 
12 static inline bool extent_state_in_tree(const struct extent_state *state)
13 {
14 	return !RB_EMPTY_NODE(&state->rb_node);
15 }
16 
17 #ifdef CONFIG_BTRFS_DEBUG
18 static LIST_HEAD(states);
19 static DEFINE_SPINLOCK(leak_lock);
20 
21 static inline void btrfs_leak_debug_add_state(struct extent_state *state)
22 {
23 	unsigned long flags;
24 
25 	spin_lock_irqsave(&leak_lock, flags);
26 	list_add(&state->leak_list, &states);
27 	spin_unlock_irqrestore(&leak_lock, flags);
28 }
29 
30 static inline void btrfs_leak_debug_del_state(struct extent_state *state)
31 {
32 	unsigned long flags;
33 
34 	spin_lock_irqsave(&leak_lock, flags);
35 	list_del(&state->leak_list);
36 	spin_unlock_irqrestore(&leak_lock, flags);
37 }
38 
39 static inline void btrfs_extent_state_leak_debug_check(void)
40 {
41 	struct extent_state *state;
42 
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);
51 	}
52 }
53 
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,
58 						       u64 start, u64 end)
59 {
60 	struct inode *inode = tree->private_data;
61 	u64 isize;
62 
63 	if (!inode)
64 		return;
65 
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);
71 	}
72 }
73 #else
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)
78 #endif
79 
80 /*
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.
86  */
87 static struct lock_class_key file_extent_tree_class;
88 
89 struct tree_entry {
90 	u64 start;
91 	u64 end;
92 	struct rb_node rb_node;
93 };
94 
95 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
96 			 struct extent_io_tree *tree, unsigned int owner,
97 			 void *private_data)
98 {
99 	tree->fs_info = fs_info;
100 	tree->state = RB_ROOT;
101 	spin_lock_init(&tree->lock);
102 	tree->private_data = private_data;
103 	tree->owner = owner;
104 	if (owner == IO_TREE_INODE_FILE_EXTENT)
105 		lockdep_set_class(&tree->lock, &file_extent_tree_class);
106 }
107 
108 void extent_io_tree_release(struct extent_io_tree *tree)
109 {
110 	spin_lock(&tree->lock);
111 	/*
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
114 	 * called.
115 	 */
116 	smp_mb();
117 	while (!RB_EMPTY_ROOT(&tree->state)) {
118 		struct rb_node *node;
119 		struct extent_state *state;
120 
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);
125 		/*
126 		 * btree io trees aren't supposed to have tasks waiting for
127 		 * changes in the flags of extent states ever.
128 		 */
129 		ASSERT(!waitqueue_active(&state->wq));
130 		free_extent_state(state);
131 
132 		cond_resched_lock(&tree->lock);
133 	}
134 	spin_unlock(&tree->lock);
135 }
136 
137 static struct extent_state *alloc_extent_state(gfp_t mask)
138 {
139 	struct extent_state *state;
140 
141 	/*
142 	 * The given mask might be not appropriate for the slab allocator,
143 	 * drop the unsupported bits
144 	 */
145 	mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
146 	state = kmem_cache_alloc(extent_state_cache, mask);
147 	if (!state)
148 		return state;
149 	state->state = 0;
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_);
155 	return state;
156 }
157 
158 static struct extent_state *alloc_extent_state_atomic(struct extent_state *prealloc)
159 {
160 	if (!prealloc)
161 		prealloc = alloc_extent_state(GFP_ATOMIC);
162 
163 	return prealloc;
164 }
165 
166 void free_extent_state(struct extent_state *state)
167 {
168 	if (!state)
169 		return;
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);
175 	}
176 }
177 
178 static int add_extent_changeset(struct extent_state *state, u32 bits,
179 				 struct extent_changeset *changeset,
180 				 int set)
181 {
182 	int ret;
183 
184 	if (!changeset)
185 		return 0;
186 	if (set && (state->state & bits) == bits)
187 		return 0;
188 	if (!set && (state->state & bits) == 0)
189 		return 0;
190 	changeset->bytes_changed += state->end - state->start + 1;
191 	ret = ulist_add(&changeset->range_changed, state->start, state->end,
192 			GFP_ATOMIC);
193 	return ret;
194 }
195 
196 static inline struct extent_state *next_state(struct extent_state *state)
197 {
198 	struct rb_node *next = rb_next(&state->rb_node);
199 
200 	if (next)
201 		return rb_entry(next, struct extent_state, rb_node);
202 	else
203 		return NULL;
204 }
205 
206 static inline struct extent_state *prev_state(struct extent_state *state)
207 {
208 	struct rb_node *next = rb_prev(&state->rb_node);
209 
210 	if (next)
211 		return rb_entry(next, struct extent_state, rb_node);
212 	else
213 		return NULL;
214 }
215 
216 /*
217  * Search @tree for an entry that contains @offset. Such entry would have
218  * entry->start <= offset && entry->end >= offset.
219  *
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
223  *	        entry in the tree)
224  * @parent_ret: points to entry which would have been the parent of the entry,
225  *               containing @offset
226  *
227  * Return a pointer to the entry that contains @offset byte address and don't change
228  * @node_ret and @parent_ret.
229  *
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.
232  */
233 static inline struct extent_state *tree_search_for_insert(struct extent_io_tree *tree,
234 							  u64 offset,
235 							  struct rb_node ***node_ret,
236 							  struct rb_node **parent_ret)
237 {
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;
242 
243 	while (*node) {
244 		prev = *node;
245 		entry = rb_entry(prev, struct extent_state, rb_node);
246 
247 		if (offset < entry->start)
248 			node = &(*node)->rb_left;
249 		else if (offset > entry->end)
250 			node = &(*node)->rb_right;
251 		else
252 			return entry;
253 	}
254 
255 	if (node_ret)
256 		*node_ret = node;
257 	if (parent_ret)
258 		*parent_ret = prev;
259 
260 	/* Search neighbors until we find the first one past the end */
261 	while (entry && offset > entry->end)
262 		entry = next_state(entry);
263 
264 	return entry;
265 }
266 
267 /*
268  * Search offset in the tree or fill neighbor rbtree node pointers.
269  *
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
274  *
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.
278  */
279 static struct extent_state *tree_search_prev_next(struct extent_io_tree *tree,
280 						  u64 offset,
281 						  struct extent_state **prev_ret,
282 						  struct extent_state **next_ret)
283 {
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;
288 
289 	ASSERT(prev_ret);
290 	ASSERT(next_ret);
291 
292 	while (*node) {
293 		entry = rb_entry(*node, struct extent_state, rb_node);
294 
295 		if (offset < entry->start)
296 			node = &(*node)->rb_left;
297 		else if (offset > entry->end)
298 			node = &(*node)->rb_right;
299 		else
300 			return entry;
301 	}
302 
303 	orig_prev = entry;
304 	while (entry && offset > entry->end)
305 		entry = next_state(entry);
306 	*next_ret = entry;
307 	entry = orig_prev;
308 
309 	while (entry && offset < entry->start)
310 		entry = prev_state(entry);
311 	*prev_ret = entry;
312 
313 	return NULL;
314 }
315 
316 /*
317  * Inexact rb-tree search, return the next entry if @offset is not found
318  */
319 static inline struct extent_state *tree_search(struct extent_io_tree *tree, u64 offset)
320 {
321 	return tree_search_for_insert(tree, offset, NULL, NULL);
322 }
323 
324 static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
325 {
326 	btrfs_panic(tree->fs_info, err,
327 	"locking error: extent tree was modified by another thread while locked");
328 }
329 
330 /*
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).
336  *
337  * This should be called with the tree lock held.
338  */
339 static void merge_state(struct extent_io_tree *tree, struct extent_state *state)
340 {
341 	struct extent_state *other;
342 
343 	if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
344 		return;
345 
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,
351 						    state, other);
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);
356 	}
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,
362 						    other);
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);
367 	}
368 }
369 
370 static void set_state_bits(struct extent_io_tree *tree,
371 			   struct extent_state *state,
372 			   u32 bits, struct extent_changeset *changeset)
373 {
374 	u32 bits_to_set = bits & ~EXTENT_CTLBITS;
375 	int ret;
376 
377 	if (tree->private_data)
378 		btrfs_set_delalloc_extent(tree->private_data, state, bits);
379 
380 	ret = add_extent_changeset(state, bits_to_set, changeset, 1);
381 	BUG_ON(ret < 0);
382 	state->state |= bits_to_set;
383 }
384 
385 /*
386  * Insert an extent_state struct into the tree.  'bits' are set on the
387  * struct before it is inserted.
388  *
389  * This may return -EEXIST if the extent is already there, in which case the
390  * state struct is freed.
391  *
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).
394  */
395 static int insert_state(struct extent_io_tree *tree,
396 			struct extent_state *state,
397 			u32 bits, struct extent_changeset *changeset)
398 {
399 	struct rb_node **node;
400 	struct rb_node *parent;
401 	const u64 end = state->end;
402 
403 	set_state_bits(tree, state, bits, changeset);
404 
405 	node = &tree->state.rb_node;
406 	while (*node) {
407 		struct extent_state *entry;
408 
409 		parent = *node;
410 		entry = rb_entry(parent, struct extent_state, rb_node);
411 
412 		if (end < entry->start) {
413 			node = &(*node)->rb_left;
414 		} else if (end > entry->end) {
415 			node = &(*node)->rb_right;
416 		} else {
417 			btrfs_err(tree->fs_info,
418 			       "found node %llu %llu on insert of %llu %llu",
419 			       entry->start, entry->end, state->start, end);
420 			return -EEXIST;
421 		}
422 	}
423 
424 	rb_link_node(&state->rb_node, parent, node);
425 	rb_insert_color(&state->rb_node, &tree->state);
426 
427 	merge_state(tree, state);
428 	return 0;
429 }
430 
431 /*
432  * Insert state to @tree to the location given by @node and @parent.
433  */
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)
438 {
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);
443 }
444 
445 /*
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.
449  *
450  * Before calling,
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 ]
455  *
456  * The tree locks are not taken by this function. They need to be held
457  * by the caller.
458  */
459 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
460 		       struct extent_state *prealloc, u64 split)
461 {
462 	struct rb_node *parent = NULL;
463 	struct rb_node **node;
464 
465 	if (tree->private_data)
466 		btrfs_split_delalloc_extent(tree->private_data, orig, split);
467 
468 	prealloc->start = orig->start;
469 	prealloc->end = split - 1;
470 	prealloc->state = orig->state;
471 	orig->start = split;
472 
473 	parent = &orig->rb_node;
474 	node = &parent;
475 	while (*node) {
476 		struct extent_state *entry;
477 
478 		parent = *node;
479 		entry = rb_entry(parent, struct extent_state, rb_node);
480 
481 		if (prealloc->end < entry->start) {
482 			node = &(*node)->rb_left;
483 		} else if (prealloc->end > entry->end) {
484 			node = &(*node)->rb_right;
485 		} else {
486 			free_extent_state(prealloc);
487 			return -EEXIST;
488 		}
489 	}
490 
491 	rb_link_node(&prealloc->rb_node, parent, node);
492 	rb_insert_color(&prealloc->rb_node, &tree->state);
493 
494 	return 0;
495 }
496 
497 /*
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).
500  *
501  * If no bits are set on the state struct after clearing things, the
502  * struct is freed and removed from the tree
503  */
504 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
505 					    struct extent_state *state,
506 					    u32 bits, int wake,
507 					    struct extent_changeset *changeset)
508 {
509 	struct extent_state *next;
510 	u32 bits_to_clear = bits & ~EXTENT_CTLBITS;
511 	int ret;
512 
513 	if (tree->private_data)
514 		btrfs_clear_delalloc_extent(tree->private_data, state, bits);
515 
516 	ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
517 	BUG_ON(ret < 0);
518 	state->state &= ~bits_to_clear;
519 	if (wake)
520 		wake_up(&state->wq);
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);
527 		} else {
528 			WARN_ON(1);
529 		}
530 	} else {
531 		merge_state(tree, state);
532 		next = next_state(state);
533 	}
534 	return next;
535 }
536 
537 /*
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.
541  *
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).
544  *
545  * The range [start, end] is inclusive.
546  *
547  * This takes the tree lock, and returns 0 on success and < 0 on error.
548  */
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)
552 {
553 	struct extent_state *state;
554 	struct extent_state *cached;
555 	struct extent_state *prealloc = NULL;
556 	u64 last_end;
557 	int err;
558 	int clear = 0;
559 	int wake;
560 	int delete = (bits & EXTENT_CLEAR_ALL_BITS);
561 
562 	btrfs_debug_check_extent_io_range(tree, start, end);
563 	trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
564 
565 	if (delete)
566 		bits |= ~EXTENT_CTLBITS;
567 
568 	if (bits & EXTENT_DELALLOC)
569 		bits |= EXTENT_NORESERVE;
570 
571 	wake = (bits & EXTENT_LOCKED) ? 1 : 0;
572 	if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
573 		clear = 1;
574 again:
575 	if (!prealloc && gfpflags_allow_blocking(mask)) {
576 		/*
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.
582 		 */
583 		prealloc = alloc_extent_state(mask);
584 	}
585 
586 	spin_lock(&tree->lock);
587 	if (cached_state) {
588 		cached = *cached_state;
589 
590 		if (clear) {
591 			*cached_state = NULL;
592 			cached_state = NULL;
593 		}
594 
595 		if (cached && extent_state_in_tree(cached) &&
596 		    cached->start <= start && cached->end > start) {
597 			if (clear)
598 				refcount_dec(&cached->refs);
599 			state = cached;
600 			goto hit_next;
601 		}
602 		if (clear)
603 			free_extent_state(cached);
604 	}
605 
606 	/* This search will find the extents that end after our range starts. */
607 	state = tree_search(tree, start);
608 	if (!state)
609 		goto out;
610 hit_next:
611 	if (state->start > end)
612 		goto out;
613 	WARN_ON(state->end < start);
614 	last_end = state->end;
615 
616 	/* The state doesn't have the wanted bits, go ahead. */
617 	if (!(state->state & bits)) {
618 		state = next_state(state);
619 		goto next;
620 	}
621 
622 	/*
623 	 *     | ---- desired range ---- |
624 	 *  | state | or
625 	 *  | ------------- state -------------- |
626 	 *
627 	 * We need to split the extent we found, and may flip bits on second
628 	 * half.
629 	 *
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.
632 	 *
633 	 * If the extent we found is inside our range, we clear the desired bit
634 	 * on it.
635 	 */
636 
637 	if (state->start < start) {
638 		prealloc = alloc_extent_state_atomic(prealloc);
639 		BUG_ON(!prealloc);
640 		err = split_state(tree, state, prealloc, start);
641 		if (err)
642 			extent_io_tree_panic(tree, err);
643 
644 		prealloc = NULL;
645 		if (err)
646 			goto out;
647 		if (state->end <= end) {
648 			state = clear_state_bit(tree, state, bits, wake, changeset);
649 			goto next;
650 		}
651 		goto search_again;
652 	}
653 	/*
654 	 * | ---- desired range ---- |
655 	 *                        | state |
656 	 * We need to split the extent, and clear the bit on the first half.
657 	 */
658 	if (state->start <= end && state->end > end) {
659 		prealloc = alloc_extent_state_atomic(prealloc);
660 		BUG_ON(!prealloc);
661 		err = split_state(tree, state, prealloc, end + 1);
662 		if (err)
663 			extent_io_tree_panic(tree, err);
664 
665 		if (wake)
666 			wake_up(&state->wq);
667 
668 		clear_state_bit(tree, prealloc, bits, wake, changeset);
669 
670 		prealloc = NULL;
671 		goto out;
672 	}
673 
674 	state = clear_state_bit(tree, state, bits, wake, changeset);
675 next:
676 	if (last_end == (u64)-1)
677 		goto out;
678 	start = last_end + 1;
679 	if (start <= end && state && !need_resched())
680 		goto hit_next;
681 
682 search_again:
683 	if (start > end)
684 		goto out;
685 	spin_unlock(&tree->lock);
686 	if (gfpflags_allow_blocking(mask))
687 		cond_resched();
688 	goto again;
689 
690 out:
691 	spin_unlock(&tree->lock);
692 	if (prealloc)
693 		free_extent_state(prealloc);
694 
695 	return 0;
696 
697 }
698 
699 static void wait_on_state(struct extent_io_tree *tree,
700 			  struct extent_state *state)
701 		__releases(tree->lock)
702 		__acquires(tree->lock)
703 {
704 	DEFINE_WAIT(wait);
705 	prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
706 	spin_unlock(&tree->lock);
707 	schedule();
708 	spin_lock(&tree->lock);
709 	finish_wait(&state->wq, &wait);
710 }
711 
712 /*
713  * Wait for one or more bits to clear on a range in the state tree.
714  * The range [start, end] is inclusive.
715  * The tree lock is taken by this function
716  */
717 void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits)
718 {
719 	struct extent_state *state;
720 
721 	btrfs_debug_check_extent_io_range(tree, start, end);
722 
723 	spin_lock(&tree->lock);
724 again:
725 	while (1) {
726 		/*
727 		 * This search will find all the extents that end after our
728 		 * range starts.
729 		 */
730 		state = tree_search(tree, start);
731 process_node:
732 		if (!state)
733 			break;
734 		if (state->start > end)
735 			goto out;
736 
737 		if (state->state & bits) {
738 			start = state->start;
739 			refcount_inc(&state->refs);
740 			wait_on_state(tree, state);
741 			free_extent_state(state);
742 			goto again;
743 		}
744 		start = state->end + 1;
745 
746 		if (start > end)
747 			break;
748 
749 		if (!cond_resched_lock(&tree->lock)) {
750 			state = next_state(state);
751 			goto process_node;
752 		}
753 	}
754 out:
755 	spin_unlock(&tree->lock);
756 }
757 
758 static void cache_state_if_flags(struct extent_state *state,
759 				 struct extent_state **cached_ptr,
760 				 unsigned flags)
761 {
762 	if (cached_ptr && !(*cached_ptr)) {
763 		if (!flags || (state->state & flags)) {
764 			*cached_ptr = state;
765 			refcount_inc(&state->refs);
766 		}
767 	}
768 }
769 
770 static void cache_state(struct extent_state *state,
771 			struct extent_state **cached_ptr)
772 {
773 	return cache_state_if_flags(state, cached_ptr,
774 				    EXTENT_LOCKED | EXTENT_BOUNDARY);
775 }
776 
777 /*
778  * Find the first state struct with 'bits' set after 'start', and return it.
779  * tree->lock must be held.  NULL will returned if nothing was found after
780  * 'start'.
781  */
782 static struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
783 							u64 start, u32 bits)
784 {
785 	struct extent_state *state;
786 
787 	/*
788 	 * This search will find all the extents that end after our range
789 	 * starts.
790 	 */
791 	state = tree_search(tree, start);
792 	while (state) {
793 		if (state->end >= start && (state->state & bits))
794 			return state;
795 		state = next_state(state);
796 	}
797 	return NULL;
798 }
799 
800 /*
801  * Find the first offset in the io tree with one or more @bits set.
802  *
803  * Note: If there are multiple bits set in @bits, any of them will match.
804  *
805  * Return 0 if we find something, and update @start_ret and @end_ret.
806  * Return 1 if we found nothing.
807  */
808 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
809 			  u64 *start_ret, u64 *end_ret, u32 bits,
810 			  struct extent_state **cached_state)
811 {
812 	struct extent_state *state;
813 	int ret = 1;
814 
815 	spin_lock(&tree->lock);
816 	if (cached_state && *cached_state) {
817 		state = *cached_state;
818 		if (state->end == start - 1 && extent_state_in_tree(state)) {
819 			while ((state = next_state(state)) != NULL) {
820 				if (state->state & bits)
821 					goto got_it;
822 			}
823 			free_extent_state(*cached_state);
824 			*cached_state = NULL;
825 			goto out;
826 		}
827 		free_extent_state(*cached_state);
828 		*cached_state = NULL;
829 	}
830 
831 	state = find_first_extent_bit_state(tree, start, bits);
832 got_it:
833 	if (state) {
834 		cache_state_if_flags(state, cached_state, 0);
835 		*start_ret = state->start;
836 		*end_ret = state->end;
837 		ret = 0;
838 	}
839 out:
840 	spin_unlock(&tree->lock);
841 	return ret;
842 }
843 
844 /*
845  * Find a contiguous area of bits
846  *
847  * @tree:      io tree to check
848  * @start:     offset to start the search from
849  * @start_ret: the first offset we found with the bits set
850  * @end_ret:   the final contiguous range of the bits that were set
851  * @bits:      bits to look for
852  *
853  * set_extent_bit and clear_extent_bit can temporarily split contiguous ranges
854  * to set bits appropriately, and then merge them again.  During this time it
855  * will drop the tree->lock, so use this helper if you want to find the actual
856  * contiguous area for given bits.  We will search to the first bit we find, and
857  * then walk down the tree until we find a non-contiguous area.  The area
858  * returned will be the full contiguous area with the bits set.
859  */
860 int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
861 			       u64 *start_ret, u64 *end_ret, u32 bits)
862 {
863 	struct extent_state *state;
864 	int ret = 1;
865 
866 	spin_lock(&tree->lock);
867 	state = find_first_extent_bit_state(tree, start, bits);
868 	if (state) {
869 		*start_ret = state->start;
870 		*end_ret = state->end;
871 		while ((state = next_state(state)) != NULL) {
872 			if (state->start > (*end_ret + 1))
873 				break;
874 			*end_ret = state->end;
875 		}
876 		ret = 0;
877 	}
878 	spin_unlock(&tree->lock);
879 	return ret;
880 }
881 
882 /*
883  * Find a contiguous range of bytes in the file marked as delalloc, not more
884  * than 'max_bytes'.  start and end are used to return the range,
885  *
886  * True is returned if we find something, false if nothing was in the tree.
887  */
888 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
889 			       u64 *end, u64 max_bytes,
890 			       struct extent_state **cached_state)
891 {
892 	struct extent_state *state;
893 	u64 cur_start = *start;
894 	bool found = false;
895 	u64 total_bytes = 0;
896 
897 	spin_lock(&tree->lock);
898 
899 	/*
900 	 * This search will find all the extents that end after our range
901 	 * starts.
902 	 */
903 	state = tree_search(tree, cur_start);
904 	if (!state) {
905 		*end = (u64)-1;
906 		goto out;
907 	}
908 
909 	while (state) {
910 		if (found && (state->start != cur_start ||
911 			      (state->state & EXTENT_BOUNDARY))) {
912 			goto out;
913 		}
914 		if (!(state->state & EXTENT_DELALLOC)) {
915 			if (!found)
916 				*end = state->end;
917 			goto out;
918 		}
919 		if (!found) {
920 			*start = state->start;
921 			*cached_state = state;
922 			refcount_inc(&state->refs);
923 		}
924 		found = true;
925 		*end = state->end;
926 		cur_start = state->end + 1;
927 		total_bytes += state->end - state->start + 1;
928 		if (total_bytes >= max_bytes)
929 			break;
930 		state = next_state(state);
931 	}
932 out:
933 	spin_unlock(&tree->lock);
934 	return found;
935 }
936 
937 /*
938  * Set some bits on a range in the tree.  This may require allocations or
939  * sleeping, so the gfp mask is used to indicate what is allowed.
940  *
941  * If any of the exclusive bits are set, this will fail with -EEXIST if some
942  * part of the range already has the desired bits set.  The start of the
943  * existing range is returned in failed_start in this case.
944  *
945  * [start, end] is inclusive This takes the tree lock.
946  */
947 static int __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
948 			    u32 bits, u64 *failed_start,
949 			    struct extent_state **cached_state,
950 			    struct extent_changeset *changeset, gfp_t mask)
951 {
952 	struct extent_state *state;
953 	struct extent_state *prealloc = NULL;
954 	struct rb_node **p;
955 	struct rb_node *parent;
956 	int err = 0;
957 	u64 last_start;
958 	u64 last_end;
959 	u32 exclusive_bits = (bits & EXTENT_LOCKED);
960 
961 	btrfs_debug_check_extent_io_range(tree, start, end);
962 	trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
963 
964 	if (exclusive_bits)
965 		ASSERT(failed_start);
966 	else
967 		ASSERT(failed_start == NULL);
968 again:
969 	if (!prealloc && gfpflags_allow_blocking(mask)) {
970 		/*
971 		 * Don't care for allocation failure here because we might end
972 		 * up not needing the pre-allocated extent state at all, which
973 		 * is the case if we only have in the tree extent states that
974 		 * cover our input range and don't cover too any other range.
975 		 * If we end up needing a new extent state we allocate it later.
976 		 */
977 		prealloc = alloc_extent_state(mask);
978 	}
979 
980 	spin_lock(&tree->lock);
981 	if (cached_state && *cached_state) {
982 		state = *cached_state;
983 		if (state->start <= start && state->end > start &&
984 		    extent_state_in_tree(state))
985 			goto hit_next;
986 	}
987 	/*
988 	 * This search will find all the extents that end after our range
989 	 * starts.
990 	 */
991 	state = tree_search_for_insert(tree, start, &p, &parent);
992 	if (!state) {
993 		prealloc = alloc_extent_state_atomic(prealloc);
994 		BUG_ON(!prealloc);
995 		prealloc->start = start;
996 		prealloc->end = end;
997 		insert_state_fast(tree, prealloc, p, parent, bits, changeset);
998 		cache_state(prealloc, cached_state);
999 		prealloc = NULL;
1000 		goto out;
1001 	}
1002 hit_next:
1003 	last_start = state->start;
1004 	last_end = state->end;
1005 
1006 	/*
1007 	 * | ---- desired range ---- |
1008 	 * | state |
1009 	 *
1010 	 * Just lock what we found and keep going
1011 	 */
1012 	if (state->start == start && state->end <= end) {
1013 		if (state->state & exclusive_bits) {
1014 			*failed_start = state->start;
1015 			err = -EEXIST;
1016 			goto out;
1017 		}
1018 
1019 		set_state_bits(tree, state, bits, changeset);
1020 		cache_state(state, cached_state);
1021 		merge_state(tree, state);
1022 		if (last_end == (u64)-1)
1023 			goto out;
1024 		start = last_end + 1;
1025 		state = next_state(state);
1026 		if (start < end && state && state->start == start &&
1027 		    !need_resched())
1028 			goto hit_next;
1029 		goto search_again;
1030 	}
1031 
1032 	/*
1033 	 *     | ---- desired range ---- |
1034 	 * | state |
1035 	 *   or
1036 	 * | ------------- state -------------- |
1037 	 *
1038 	 * We need to split the extent we found, and may flip bits on second
1039 	 * half.
1040 	 *
1041 	 * If the extent we found extends past our range, we just split and
1042 	 * search again.  It'll get split again the next time though.
1043 	 *
1044 	 * If the extent we found is inside our range, we set the desired bit
1045 	 * on it.
1046 	 */
1047 	if (state->start < start) {
1048 		if (state->state & exclusive_bits) {
1049 			*failed_start = start;
1050 			err = -EEXIST;
1051 			goto out;
1052 		}
1053 
1054 		/*
1055 		 * If this extent already has all the bits we want set, then
1056 		 * skip it, not necessary to split it or do anything with it.
1057 		 */
1058 		if ((state->state & bits) == bits) {
1059 			start = state->end + 1;
1060 			cache_state(state, cached_state);
1061 			goto search_again;
1062 		}
1063 
1064 		prealloc = alloc_extent_state_atomic(prealloc);
1065 		BUG_ON(!prealloc);
1066 		err = split_state(tree, state, prealloc, start);
1067 		if (err)
1068 			extent_io_tree_panic(tree, err);
1069 
1070 		prealloc = NULL;
1071 		if (err)
1072 			goto out;
1073 		if (state->end <= end) {
1074 			set_state_bits(tree, state, bits, changeset);
1075 			cache_state(state, cached_state);
1076 			merge_state(tree, state);
1077 			if (last_end == (u64)-1)
1078 				goto out;
1079 			start = last_end + 1;
1080 			state = next_state(state);
1081 			if (start < end && state && state->start == start &&
1082 			    !need_resched())
1083 				goto hit_next;
1084 		}
1085 		goto search_again;
1086 	}
1087 	/*
1088 	 * | ---- desired range ---- |
1089 	 *     | state | or               | state |
1090 	 *
1091 	 * There's a hole, we need to insert something in it and ignore the
1092 	 * extent we found.
1093 	 */
1094 	if (state->start > start) {
1095 		u64 this_end;
1096 		if (end < last_start)
1097 			this_end = end;
1098 		else
1099 			this_end = last_start - 1;
1100 
1101 		prealloc = alloc_extent_state_atomic(prealloc);
1102 		BUG_ON(!prealloc);
1103 
1104 		/*
1105 		 * Avoid to free 'prealloc' if it can be merged with the later
1106 		 * extent.
1107 		 */
1108 		prealloc->start = start;
1109 		prealloc->end = this_end;
1110 		err = insert_state(tree, prealloc, bits, changeset);
1111 		if (err)
1112 			extent_io_tree_panic(tree, err);
1113 
1114 		cache_state(prealloc, cached_state);
1115 		prealloc = NULL;
1116 		start = this_end + 1;
1117 		goto search_again;
1118 	}
1119 	/*
1120 	 * | ---- desired range ---- |
1121 	 *                        | state |
1122 	 *
1123 	 * We need to split the extent, and set the bit on the first half
1124 	 */
1125 	if (state->start <= end && state->end > end) {
1126 		if (state->state & exclusive_bits) {
1127 			*failed_start = start;
1128 			err = -EEXIST;
1129 			goto out;
1130 		}
1131 
1132 		prealloc = alloc_extent_state_atomic(prealloc);
1133 		BUG_ON(!prealloc);
1134 		err = split_state(tree, state, prealloc, end + 1);
1135 		if (err)
1136 			extent_io_tree_panic(tree, err);
1137 
1138 		set_state_bits(tree, prealloc, bits, changeset);
1139 		cache_state(prealloc, cached_state);
1140 		merge_state(tree, prealloc);
1141 		prealloc = NULL;
1142 		goto out;
1143 	}
1144 
1145 search_again:
1146 	if (start > end)
1147 		goto out;
1148 	spin_unlock(&tree->lock);
1149 	if (gfpflags_allow_blocking(mask))
1150 		cond_resched();
1151 	goto again;
1152 
1153 out:
1154 	spin_unlock(&tree->lock);
1155 	if (prealloc)
1156 		free_extent_state(prealloc);
1157 
1158 	return err;
1159 
1160 }
1161 
1162 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1163 		   u32 bits, struct extent_state **cached_state, gfp_t mask)
1164 {
1165 	return __set_extent_bit(tree, start, end, bits, NULL, cached_state,
1166 				NULL, mask);
1167 }
1168 
1169 /*
1170  * Convert all bits in a given range from one bit to another
1171  *
1172  * @tree:	the io tree to search
1173  * @start:	the start offset in bytes
1174  * @end:	the end offset in bytes (inclusive)
1175  * @bits:	the bits to set in this range
1176  * @clear_bits:	the bits to clear in this range
1177  * @cached_state:	state that we're going to cache
1178  *
1179  * This will go through and set bits for the given range.  If any states exist
1180  * already in this range they are set with the given bit and cleared of the
1181  * clear_bits.  This is only meant to be used by things that are mergeable, ie.
1182  * converting from say DELALLOC to DIRTY.  This is not meant to be used with
1183  * boundary bits like LOCK.
1184  *
1185  * All allocations are done with GFP_NOFS.
1186  */
1187 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1188 		       u32 bits, u32 clear_bits,
1189 		       struct extent_state **cached_state)
1190 {
1191 	struct extent_state *state;
1192 	struct extent_state *prealloc = NULL;
1193 	struct rb_node **p;
1194 	struct rb_node *parent;
1195 	int err = 0;
1196 	u64 last_start;
1197 	u64 last_end;
1198 	bool first_iteration = true;
1199 
1200 	btrfs_debug_check_extent_io_range(tree, start, end);
1201 	trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1202 				       clear_bits);
1203 
1204 again:
1205 	if (!prealloc) {
1206 		/*
1207 		 * Best effort, don't worry if extent state allocation fails
1208 		 * here for the first iteration. We might have a cached state
1209 		 * that matches exactly the target range, in which case no
1210 		 * extent state allocations are needed. We'll only know this
1211 		 * after locking the tree.
1212 		 */
1213 		prealloc = alloc_extent_state(GFP_NOFS);
1214 		if (!prealloc && !first_iteration)
1215 			return -ENOMEM;
1216 	}
1217 
1218 	spin_lock(&tree->lock);
1219 	if (cached_state && *cached_state) {
1220 		state = *cached_state;
1221 		if (state->start <= start && state->end > start &&
1222 		    extent_state_in_tree(state))
1223 			goto hit_next;
1224 	}
1225 
1226 	/*
1227 	 * This search will find all the extents that end after our range
1228 	 * starts.
1229 	 */
1230 	state = tree_search_for_insert(tree, start, &p, &parent);
1231 	if (!state) {
1232 		prealloc = alloc_extent_state_atomic(prealloc);
1233 		if (!prealloc) {
1234 			err = -ENOMEM;
1235 			goto out;
1236 		}
1237 		prealloc->start = start;
1238 		prealloc->end = end;
1239 		insert_state_fast(tree, prealloc, p, parent, bits, NULL);
1240 		cache_state(prealloc, cached_state);
1241 		prealloc = NULL;
1242 		goto out;
1243 	}
1244 hit_next:
1245 	last_start = state->start;
1246 	last_end = state->end;
1247 
1248 	/*
1249 	 * | ---- desired range ---- |
1250 	 * | state |
1251 	 *
1252 	 * Just lock what we found and keep going.
1253 	 */
1254 	if (state->start == start && state->end <= end) {
1255 		set_state_bits(tree, state, bits, NULL);
1256 		cache_state(state, cached_state);
1257 		state = clear_state_bit(tree, state, clear_bits, 0, NULL);
1258 		if (last_end == (u64)-1)
1259 			goto out;
1260 		start = last_end + 1;
1261 		if (start < end && state && state->start == start &&
1262 		    !need_resched())
1263 			goto hit_next;
1264 		goto search_again;
1265 	}
1266 
1267 	/*
1268 	 *     | ---- desired range ---- |
1269 	 * | state |
1270 	 *   or
1271 	 * | ------------- state -------------- |
1272 	 *
1273 	 * We need to split the extent we found, and may flip bits on second
1274 	 * half.
1275 	 *
1276 	 * If the extent we found extends past our range, we just split and
1277 	 * search again.  It'll get split again the next time though.
1278 	 *
1279 	 * If the extent we found is inside our range, we set the desired bit
1280 	 * on it.
1281 	 */
1282 	if (state->start < start) {
1283 		prealloc = alloc_extent_state_atomic(prealloc);
1284 		if (!prealloc) {
1285 			err = -ENOMEM;
1286 			goto out;
1287 		}
1288 		err = split_state(tree, state, prealloc, start);
1289 		if (err)
1290 			extent_io_tree_panic(tree, err);
1291 		prealloc = NULL;
1292 		if (err)
1293 			goto out;
1294 		if (state->end <= end) {
1295 			set_state_bits(tree, state, bits, NULL);
1296 			cache_state(state, cached_state);
1297 			state = clear_state_bit(tree, state, clear_bits, 0, NULL);
1298 			if (last_end == (u64)-1)
1299 				goto out;
1300 			start = last_end + 1;
1301 			if (start < end && state && state->start == start &&
1302 			    !need_resched())
1303 				goto hit_next;
1304 		}
1305 		goto search_again;
1306 	}
1307 	/*
1308 	 * | ---- desired range ---- |
1309 	 *     | state | or               | state |
1310 	 *
1311 	 * There's a hole, we need to insert something in it and ignore the
1312 	 * extent we found.
1313 	 */
1314 	if (state->start > start) {
1315 		u64 this_end;
1316 		if (end < last_start)
1317 			this_end = end;
1318 		else
1319 			this_end = last_start - 1;
1320 
1321 		prealloc = alloc_extent_state_atomic(prealloc);
1322 		if (!prealloc) {
1323 			err = -ENOMEM;
1324 			goto out;
1325 		}
1326 
1327 		/*
1328 		 * Avoid to free 'prealloc' if it can be merged with the later
1329 		 * extent.
1330 		 */
1331 		prealloc->start = start;
1332 		prealloc->end = this_end;
1333 		err = insert_state(tree, prealloc, bits, NULL);
1334 		if (err)
1335 			extent_io_tree_panic(tree, err);
1336 		cache_state(prealloc, cached_state);
1337 		prealloc = NULL;
1338 		start = this_end + 1;
1339 		goto search_again;
1340 	}
1341 	/*
1342 	 * | ---- desired range ---- |
1343 	 *                        | state |
1344 	 *
1345 	 * We need to split the extent, and set the bit on the first half.
1346 	 */
1347 	if (state->start <= end && state->end > end) {
1348 		prealloc = alloc_extent_state_atomic(prealloc);
1349 		if (!prealloc) {
1350 			err = -ENOMEM;
1351 			goto out;
1352 		}
1353 
1354 		err = split_state(tree, state, prealloc, end + 1);
1355 		if (err)
1356 			extent_io_tree_panic(tree, err);
1357 
1358 		set_state_bits(tree, prealloc, bits, NULL);
1359 		cache_state(prealloc, cached_state);
1360 		clear_state_bit(tree, prealloc, clear_bits, 0, NULL);
1361 		prealloc = NULL;
1362 		goto out;
1363 	}
1364 
1365 search_again:
1366 	if (start > end)
1367 		goto out;
1368 	spin_unlock(&tree->lock);
1369 	cond_resched();
1370 	first_iteration = false;
1371 	goto again;
1372 
1373 out:
1374 	spin_unlock(&tree->lock);
1375 	if (prealloc)
1376 		free_extent_state(prealloc);
1377 
1378 	return err;
1379 }
1380 
1381 /*
1382  * Find the first range that has @bits not set. This range could start before
1383  * @start.
1384  *
1385  * @tree:      the tree to search
1386  * @start:     offset at/after which the found extent should start
1387  * @start_ret: records the beginning of the range
1388  * @end_ret:   records the end of the range (inclusive)
1389  * @bits:      the set of bits which must be unset
1390  *
1391  * Since unallocated range is also considered one which doesn't have the bits
1392  * set it's possible that @end_ret contains -1, this happens in case the range
1393  * spans (last_range_end, end of device]. In this case it's up to the caller to
1394  * trim @end_ret to the appropriate size.
1395  */
1396 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
1397 				 u64 *start_ret, u64 *end_ret, u32 bits)
1398 {
1399 	struct extent_state *state;
1400 	struct extent_state *prev = NULL, *next;
1401 
1402 	spin_lock(&tree->lock);
1403 
1404 	/* Find first extent with bits cleared */
1405 	while (1) {
1406 		state = tree_search_prev_next(tree, start, &prev, &next);
1407 		if (!state && !next && !prev) {
1408 			/*
1409 			 * Tree is completely empty, send full range and let
1410 			 * caller deal with it
1411 			 */
1412 			*start_ret = 0;
1413 			*end_ret = -1;
1414 			goto out;
1415 		} else if (!state && !next) {
1416 			/*
1417 			 * We are past the last allocated chunk, set start at
1418 			 * the end of the last extent.
1419 			 */
1420 			*start_ret = prev->end + 1;
1421 			*end_ret = -1;
1422 			goto out;
1423 		} else if (!state) {
1424 			state = next;
1425 		}
1426 
1427 		/*
1428 		 * At this point 'state' either contains 'start' or start is
1429 		 * before 'state'
1430 		 */
1431 		if (in_range(start, state->start, state->end - state->start + 1)) {
1432 			if (state->state & bits) {
1433 				/*
1434 				 * |--range with bits sets--|
1435 				 *    |
1436 				 *    start
1437 				 */
1438 				start = state->end + 1;
1439 			} else {
1440 				/*
1441 				 * 'start' falls within a range that doesn't
1442 				 * have the bits set, so take its start as the
1443 				 * beginning of the desired range
1444 				 *
1445 				 * |--range with bits cleared----|
1446 				 *      |
1447 				 *      start
1448 				 */
1449 				*start_ret = state->start;
1450 				break;
1451 			}
1452 		} else {
1453 			/*
1454 			 * |---prev range---|---hole/unset---|---node range---|
1455 			 *                          |
1456 			 *                        start
1457 			 *
1458 			 *                        or
1459 			 *
1460 			 * |---hole/unset--||--first node--|
1461 			 * 0   |
1462 			 *    start
1463 			 */
1464 			if (prev)
1465 				*start_ret = prev->end + 1;
1466 			else
1467 				*start_ret = 0;
1468 			break;
1469 		}
1470 	}
1471 
1472 	/*
1473 	 * Find the longest stretch from start until an entry which has the
1474 	 * bits set
1475 	 */
1476 	while (state) {
1477 		if (state->end >= start && !(state->state & bits)) {
1478 			*end_ret = state->end;
1479 		} else {
1480 			*end_ret = state->start - 1;
1481 			break;
1482 		}
1483 		state = next_state(state);
1484 	}
1485 out:
1486 	spin_unlock(&tree->lock);
1487 }
1488 
1489 /*
1490  * Count the number of bytes in the tree that have a given bit(s) set.  This
1491  * can be fairly slow, except for EXTENT_DIRTY which is cached.  The total
1492  * number found is returned.
1493  */
1494 u64 count_range_bits(struct extent_io_tree *tree,
1495 		     u64 *start, u64 search_end, u64 max_bytes,
1496 		     u32 bits, int contig)
1497 {
1498 	struct extent_state *state;
1499 	u64 cur_start = *start;
1500 	u64 total_bytes = 0;
1501 	u64 last = 0;
1502 	int found = 0;
1503 
1504 	if (WARN_ON(search_end <= cur_start))
1505 		return 0;
1506 
1507 	spin_lock(&tree->lock);
1508 
1509 	/*
1510 	 * This search will find all the extents that end after our range
1511 	 * starts.
1512 	 */
1513 	state = tree_search(tree, cur_start);
1514 	while (state) {
1515 		if (state->start > search_end)
1516 			break;
1517 		if (contig && found && state->start > last + 1)
1518 			break;
1519 		if (state->end >= cur_start && (state->state & bits) == bits) {
1520 			total_bytes += min(search_end, state->end) + 1 -
1521 				       max(cur_start, state->start);
1522 			if (total_bytes >= max_bytes)
1523 				break;
1524 			if (!found) {
1525 				*start = max(cur_start, state->start);
1526 				found = 1;
1527 			}
1528 			last = state->end;
1529 		} else if (contig && found) {
1530 			break;
1531 		}
1532 		state = next_state(state);
1533 	}
1534 	spin_unlock(&tree->lock);
1535 	return total_bytes;
1536 }
1537 
1538 /*
1539  * Searche a range in the state tree for a given mask.  If 'filled' == 1, this
1540  * returns 1 only if every extent in the tree has the bits set.  Otherwise, 1
1541  * is returned if any bit in the range is found set.
1542  */
1543 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1544 		   u32 bits, int filled, struct extent_state *cached)
1545 {
1546 	struct extent_state *state = NULL;
1547 	int bitset = 0;
1548 
1549 	spin_lock(&tree->lock);
1550 	if (cached && extent_state_in_tree(cached) && cached->start <= start &&
1551 	    cached->end > start)
1552 		state = cached;
1553 	else
1554 		state = tree_search(tree, start);
1555 	while (state && start <= end) {
1556 		if (filled && state->start > start) {
1557 			bitset = 0;
1558 			break;
1559 		}
1560 
1561 		if (state->start > end)
1562 			break;
1563 
1564 		if (state->state & bits) {
1565 			bitset = 1;
1566 			if (!filled)
1567 				break;
1568 		} else if (filled) {
1569 			bitset = 0;
1570 			break;
1571 		}
1572 
1573 		if (state->end == (u64)-1)
1574 			break;
1575 
1576 		start = state->end + 1;
1577 		if (start > end)
1578 			break;
1579 		state = next_state(state);
1580 	}
1581 
1582 	/* We ran out of states and were still inside of our range. */
1583 	if (filled && !state)
1584 		bitset = 0;
1585 	spin_unlock(&tree->lock);
1586 	return bitset;
1587 }
1588 
1589 /* Wrappers around set/clear extent bit */
1590 int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1591 			   u32 bits, struct extent_changeset *changeset)
1592 {
1593 	/*
1594 	 * We don't support EXTENT_LOCKED yet, as current changeset will
1595 	 * record any bits changed, so for EXTENT_LOCKED case, it will
1596 	 * either fail with -EEXIST or changeset will record the whole
1597 	 * range.
1598 	 */
1599 	ASSERT(!(bits & EXTENT_LOCKED));
1600 
1601 	return __set_extent_bit(tree, start, end, bits, NULL, NULL, changeset,
1602 				GFP_NOFS);
1603 }
1604 
1605 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1606 			     u32 bits, struct extent_changeset *changeset)
1607 {
1608 	/*
1609 	 * Don't support EXTENT_LOCKED case, same reason as
1610 	 * set_record_extent_bits().
1611 	 */
1612 	ASSERT(!(bits & EXTENT_LOCKED));
1613 
1614 	return __clear_extent_bit(tree, start, end, bits, NULL, GFP_NOFS,
1615 				  changeset);
1616 }
1617 
1618 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1619 {
1620 	int err;
1621 	u64 failed_start;
1622 
1623 	err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, &failed_start,
1624 			       NULL, NULL, GFP_NOFS);
1625 	if (err == -EEXIST) {
1626 		if (failed_start > start)
1627 			clear_extent_bit(tree, start, failed_start - 1,
1628 					 EXTENT_LOCKED, NULL);
1629 		return 0;
1630 	}
1631 	return 1;
1632 }
1633 
1634 /*
1635  * Either insert or lock state struct between start and end use mask to tell
1636  * us if waiting is desired.
1637  */
1638 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1639 		struct extent_state **cached_state)
1640 {
1641 	int err;
1642 	u64 failed_start;
1643 
1644 	while (1) {
1645 		err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
1646 				       &failed_start, cached_state, NULL,
1647 				       GFP_NOFS);
1648 		if (err == -EEXIST) {
1649 			wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1650 			start = failed_start;
1651 		} else
1652 			break;
1653 		WARN_ON(start > end);
1654 	}
1655 	return err;
1656 }
1657 
1658 void __cold extent_state_free_cachep(void)
1659 {
1660 	btrfs_extent_state_leak_debug_check();
1661 	kmem_cache_destroy(extent_state_cache);
1662 }
1663 
1664 int __init extent_state_init_cachep(void)
1665 {
1666 	extent_state_cache = kmem_cache_create("btrfs_extent_state",
1667 			sizeof(struct extent_state), 0,
1668 			SLAB_MEM_SPREAD, NULL);
1669 	if (!extent_state_cache)
1670 		return -ENOMEM;
1671 
1672 	return 0;
1673 }
1674