xref: /openbmc/linux/fs/btrfs/extent_io.c (revision d9fd5a71)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/bitops.h>
4 #include <linux/slab.h>
5 #include <linux/bio.h>
6 #include <linux/mm.h>
7 #include <linux/pagemap.h>
8 #include <linux/page-flags.h>
9 #include <linux/spinlock.h>
10 #include <linux/blkdev.h>
11 #include <linux/swap.h>
12 #include <linux/writeback.h>
13 #include <linux/pagevec.h>
14 #include <linux/prefetch.h>
15 #include <linux/cleancache.h>
16 #include "extent_io.h"
17 #include "extent-io-tree.h"
18 #include "extent_map.h"
19 #include "ctree.h"
20 #include "btrfs_inode.h"
21 #include "volumes.h"
22 #include "check-integrity.h"
23 #include "locking.h"
24 #include "rcu-string.h"
25 #include "backref.h"
26 #include "disk-io.h"
27 #include "subpage.h"
28 #include "zoned.h"
29 #include "block-group.h"
30 
31 static struct kmem_cache *extent_state_cache;
32 static struct kmem_cache *extent_buffer_cache;
33 static struct bio_set btrfs_bioset;
34 
35 static inline bool extent_state_in_tree(const struct extent_state *state)
36 {
37 	return !RB_EMPTY_NODE(&state->rb_node);
38 }
39 
40 #ifdef CONFIG_BTRFS_DEBUG
41 static LIST_HEAD(states);
42 static DEFINE_SPINLOCK(leak_lock);
43 
44 static inline void btrfs_leak_debug_add(spinlock_t *lock,
45 					struct list_head *new,
46 					struct list_head *head)
47 {
48 	unsigned long flags;
49 
50 	spin_lock_irqsave(lock, flags);
51 	list_add(new, head);
52 	spin_unlock_irqrestore(lock, flags);
53 }
54 
55 static inline void btrfs_leak_debug_del(spinlock_t *lock,
56 					struct list_head *entry)
57 {
58 	unsigned long flags;
59 
60 	spin_lock_irqsave(lock, flags);
61 	list_del(entry);
62 	spin_unlock_irqrestore(lock, flags);
63 }
64 
65 void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info)
66 {
67 	struct extent_buffer *eb;
68 	unsigned long flags;
69 
70 	/*
71 	 * If we didn't get into open_ctree our allocated_ebs will not be
72 	 * initialized, so just skip this.
73 	 */
74 	if (!fs_info->allocated_ebs.next)
75 		return;
76 
77 	spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
78 	while (!list_empty(&fs_info->allocated_ebs)) {
79 		eb = list_first_entry(&fs_info->allocated_ebs,
80 				      struct extent_buffer, leak_list);
81 		pr_err(
82 	"BTRFS: buffer leak start %llu len %lu refs %d bflags %lu owner %llu\n",
83 		       eb->start, eb->len, atomic_read(&eb->refs), eb->bflags,
84 		       btrfs_header_owner(eb));
85 		list_del(&eb->leak_list);
86 		kmem_cache_free(extent_buffer_cache, eb);
87 	}
88 	spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
89 }
90 
91 static inline void btrfs_extent_state_leak_debug_check(void)
92 {
93 	struct extent_state *state;
94 
95 	while (!list_empty(&states)) {
96 		state = list_entry(states.next, struct extent_state, leak_list);
97 		pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
98 		       state->start, state->end, state->state,
99 		       extent_state_in_tree(state),
100 		       refcount_read(&state->refs));
101 		list_del(&state->leak_list);
102 		kmem_cache_free(extent_state_cache, state);
103 	}
104 }
105 
106 #define btrfs_debug_check_extent_io_range(tree, start, end)		\
107 	__btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
108 static inline void __btrfs_debug_check_extent_io_range(const char *caller,
109 		struct extent_io_tree *tree, u64 start, u64 end)
110 {
111 	struct inode *inode = tree->private_data;
112 	u64 isize;
113 
114 	if (!inode || !is_data_inode(inode))
115 		return;
116 
117 	isize = i_size_read(inode);
118 	if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
119 		btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
120 		    "%s: ino %llu isize %llu odd range [%llu,%llu]",
121 			caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
122 	}
123 }
124 #else
125 #define btrfs_leak_debug_add(lock, new, head)	do {} while (0)
126 #define btrfs_leak_debug_del(lock, entry)	do {} while (0)
127 #define btrfs_extent_state_leak_debug_check()	do {} while (0)
128 #define btrfs_debug_check_extent_io_range(c, s, e)	do {} while (0)
129 #endif
130 
131 struct tree_entry {
132 	u64 start;
133 	u64 end;
134 	struct rb_node rb_node;
135 };
136 
137 struct extent_page_data {
138 	struct bio *bio;
139 	/* tells writepage not to lock the state bits for this range
140 	 * it still does the unlocking
141 	 */
142 	unsigned int extent_locked:1;
143 
144 	/* tells the submit_bio code to use REQ_SYNC */
145 	unsigned int sync_io:1;
146 };
147 
148 static int add_extent_changeset(struct extent_state *state, u32 bits,
149 				 struct extent_changeset *changeset,
150 				 int set)
151 {
152 	int ret;
153 
154 	if (!changeset)
155 		return 0;
156 	if (set && (state->state & bits) == bits)
157 		return 0;
158 	if (!set && (state->state & bits) == 0)
159 		return 0;
160 	changeset->bytes_changed += state->end - state->start + 1;
161 	ret = ulist_add(&changeset->range_changed, state->start, state->end,
162 			GFP_ATOMIC);
163 	return ret;
164 }
165 
166 int __must_check submit_one_bio(struct bio *bio, int mirror_num,
167 				unsigned long bio_flags)
168 {
169 	blk_status_t ret = 0;
170 	struct extent_io_tree *tree = bio->bi_private;
171 
172 	bio->bi_private = NULL;
173 
174 	if (is_data_inode(tree->private_data))
175 		ret = btrfs_submit_data_bio(tree->private_data, bio, mirror_num,
176 					    bio_flags);
177 	else
178 		ret = btrfs_submit_metadata_bio(tree->private_data, bio,
179 						mirror_num, bio_flags);
180 
181 	return blk_status_to_errno(ret);
182 }
183 
184 /* Cleanup unsubmitted bios */
185 static void end_write_bio(struct extent_page_data *epd, int ret)
186 {
187 	if (epd->bio) {
188 		epd->bio->bi_status = errno_to_blk_status(ret);
189 		bio_endio(epd->bio);
190 		epd->bio = NULL;
191 	}
192 }
193 
194 /*
195  * Submit bio from extent page data via submit_one_bio
196  *
197  * Return 0 if everything is OK.
198  * Return <0 for error.
199  */
200 static int __must_check flush_write_bio(struct extent_page_data *epd)
201 {
202 	int ret = 0;
203 
204 	if (epd->bio) {
205 		ret = submit_one_bio(epd->bio, 0, 0);
206 		/*
207 		 * Clean up of epd->bio is handled by its endio function.
208 		 * And endio is either triggered by successful bio execution
209 		 * or the error handler of submit bio hook.
210 		 * So at this point, no matter what happened, we don't need
211 		 * to clean up epd->bio.
212 		 */
213 		epd->bio = NULL;
214 	}
215 	return ret;
216 }
217 
218 int __init extent_state_cache_init(void)
219 {
220 	extent_state_cache = kmem_cache_create("btrfs_extent_state",
221 			sizeof(struct extent_state), 0,
222 			SLAB_MEM_SPREAD, NULL);
223 	if (!extent_state_cache)
224 		return -ENOMEM;
225 	return 0;
226 }
227 
228 int __init extent_io_init(void)
229 {
230 	extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
231 			sizeof(struct extent_buffer), 0,
232 			SLAB_MEM_SPREAD, NULL);
233 	if (!extent_buffer_cache)
234 		return -ENOMEM;
235 
236 	if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
237 			offsetof(struct btrfs_io_bio, bio),
238 			BIOSET_NEED_BVECS))
239 		goto free_buffer_cache;
240 
241 	if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
242 		goto free_bioset;
243 
244 	return 0;
245 
246 free_bioset:
247 	bioset_exit(&btrfs_bioset);
248 
249 free_buffer_cache:
250 	kmem_cache_destroy(extent_buffer_cache);
251 	extent_buffer_cache = NULL;
252 	return -ENOMEM;
253 }
254 
255 void __cold extent_state_cache_exit(void)
256 {
257 	btrfs_extent_state_leak_debug_check();
258 	kmem_cache_destroy(extent_state_cache);
259 }
260 
261 void __cold extent_io_exit(void)
262 {
263 	/*
264 	 * Make sure all delayed rcu free are flushed before we
265 	 * destroy caches.
266 	 */
267 	rcu_barrier();
268 	kmem_cache_destroy(extent_buffer_cache);
269 	bioset_exit(&btrfs_bioset);
270 }
271 
272 /*
273  * For the file_extent_tree, we want to hold the inode lock when we lookup and
274  * update the disk_i_size, but lockdep will complain because our io_tree we hold
275  * the tree lock and get the inode lock when setting delalloc.  These two things
276  * are unrelated, so make a class for the file_extent_tree so we don't get the
277  * two locking patterns mixed up.
278  */
279 static struct lock_class_key file_extent_tree_class;
280 
281 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
282 			 struct extent_io_tree *tree, unsigned int owner,
283 			 void *private_data)
284 {
285 	tree->fs_info = fs_info;
286 	tree->state = RB_ROOT;
287 	tree->dirty_bytes = 0;
288 	spin_lock_init(&tree->lock);
289 	tree->private_data = private_data;
290 	tree->owner = owner;
291 	if (owner == IO_TREE_INODE_FILE_EXTENT)
292 		lockdep_set_class(&tree->lock, &file_extent_tree_class);
293 }
294 
295 void extent_io_tree_release(struct extent_io_tree *tree)
296 {
297 	spin_lock(&tree->lock);
298 	/*
299 	 * Do a single barrier for the waitqueue_active check here, the state
300 	 * of the waitqueue should not change once extent_io_tree_release is
301 	 * called.
302 	 */
303 	smp_mb();
304 	while (!RB_EMPTY_ROOT(&tree->state)) {
305 		struct rb_node *node;
306 		struct extent_state *state;
307 
308 		node = rb_first(&tree->state);
309 		state = rb_entry(node, struct extent_state, rb_node);
310 		rb_erase(&state->rb_node, &tree->state);
311 		RB_CLEAR_NODE(&state->rb_node);
312 		/*
313 		 * btree io trees aren't supposed to have tasks waiting for
314 		 * changes in the flags of extent states ever.
315 		 */
316 		ASSERT(!waitqueue_active(&state->wq));
317 		free_extent_state(state);
318 
319 		cond_resched_lock(&tree->lock);
320 	}
321 	spin_unlock(&tree->lock);
322 }
323 
324 static struct extent_state *alloc_extent_state(gfp_t mask)
325 {
326 	struct extent_state *state;
327 
328 	/*
329 	 * The given mask might be not appropriate for the slab allocator,
330 	 * drop the unsupported bits
331 	 */
332 	mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
333 	state = kmem_cache_alloc(extent_state_cache, mask);
334 	if (!state)
335 		return state;
336 	state->state = 0;
337 	state->failrec = NULL;
338 	RB_CLEAR_NODE(&state->rb_node);
339 	btrfs_leak_debug_add(&leak_lock, &state->leak_list, &states);
340 	refcount_set(&state->refs, 1);
341 	init_waitqueue_head(&state->wq);
342 	trace_alloc_extent_state(state, mask, _RET_IP_);
343 	return state;
344 }
345 
346 void free_extent_state(struct extent_state *state)
347 {
348 	if (!state)
349 		return;
350 	if (refcount_dec_and_test(&state->refs)) {
351 		WARN_ON(extent_state_in_tree(state));
352 		btrfs_leak_debug_del(&leak_lock, &state->leak_list);
353 		trace_free_extent_state(state, _RET_IP_);
354 		kmem_cache_free(extent_state_cache, state);
355 	}
356 }
357 
358 static struct rb_node *tree_insert(struct rb_root *root,
359 				   struct rb_node *search_start,
360 				   u64 offset,
361 				   struct rb_node *node,
362 				   struct rb_node ***p_in,
363 				   struct rb_node **parent_in)
364 {
365 	struct rb_node **p;
366 	struct rb_node *parent = NULL;
367 	struct tree_entry *entry;
368 
369 	if (p_in && parent_in) {
370 		p = *p_in;
371 		parent = *parent_in;
372 		goto do_insert;
373 	}
374 
375 	p = search_start ? &search_start : &root->rb_node;
376 	while (*p) {
377 		parent = *p;
378 		entry = rb_entry(parent, struct tree_entry, rb_node);
379 
380 		if (offset < entry->start)
381 			p = &(*p)->rb_left;
382 		else if (offset > entry->end)
383 			p = &(*p)->rb_right;
384 		else
385 			return parent;
386 	}
387 
388 do_insert:
389 	rb_link_node(node, parent, p);
390 	rb_insert_color(node, root);
391 	return NULL;
392 }
393 
394 /**
395  * Search @tree for an entry that contains @offset. Such entry would have
396  * entry->start <= offset && entry->end >= offset.
397  *
398  * @tree:       the tree to search
399  * @offset:     offset that should fall within an entry in @tree
400  * @next_ret:   pointer to the first entry whose range ends after @offset
401  * @prev_ret:   pointer to the first entry whose range begins before @offset
402  * @p_ret:      pointer where new node should be anchored (used when inserting an
403  *	        entry in the tree)
404  * @parent_ret: points to entry which would have been the parent of the entry,
405  *               containing @offset
406  *
407  * This function returns a pointer to the entry that contains @offset byte
408  * address. If no such entry exists, then NULL is returned and the other
409  * pointer arguments to the function are filled, otherwise the found entry is
410  * returned and other pointers are left untouched.
411  */
412 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
413 				      struct rb_node **next_ret,
414 				      struct rb_node **prev_ret,
415 				      struct rb_node ***p_ret,
416 				      struct rb_node **parent_ret)
417 {
418 	struct rb_root *root = &tree->state;
419 	struct rb_node **n = &root->rb_node;
420 	struct rb_node *prev = NULL;
421 	struct rb_node *orig_prev = NULL;
422 	struct tree_entry *entry;
423 	struct tree_entry *prev_entry = NULL;
424 
425 	while (*n) {
426 		prev = *n;
427 		entry = rb_entry(prev, struct tree_entry, rb_node);
428 		prev_entry = entry;
429 
430 		if (offset < entry->start)
431 			n = &(*n)->rb_left;
432 		else if (offset > entry->end)
433 			n = &(*n)->rb_right;
434 		else
435 			return *n;
436 	}
437 
438 	if (p_ret)
439 		*p_ret = n;
440 	if (parent_ret)
441 		*parent_ret = prev;
442 
443 	if (next_ret) {
444 		orig_prev = prev;
445 		while (prev && offset > prev_entry->end) {
446 			prev = rb_next(prev);
447 			prev_entry = rb_entry(prev, struct tree_entry, rb_node);
448 		}
449 		*next_ret = prev;
450 		prev = orig_prev;
451 	}
452 
453 	if (prev_ret) {
454 		prev_entry = rb_entry(prev, struct tree_entry, rb_node);
455 		while (prev && offset < prev_entry->start) {
456 			prev = rb_prev(prev);
457 			prev_entry = rb_entry(prev, struct tree_entry, rb_node);
458 		}
459 		*prev_ret = prev;
460 	}
461 	return NULL;
462 }
463 
464 static inline struct rb_node *
465 tree_search_for_insert(struct extent_io_tree *tree,
466 		       u64 offset,
467 		       struct rb_node ***p_ret,
468 		       struct rb_node **parent_ret)
469 {
470 	struct rb_node *next= NULL;
471 	struct rb_node *ret;
472 
473 	ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
474 	if (!ret)
475 		return next;
476 	return ret;
477 }
478 
479 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
480 					  u64 offset)
481 {
482 	return tree_search_for_insert(tree, offset, NULL, NULL);
483 }
484 
485 /*
486  * utility function to look for merge candidates inside a given range.
487  * Any extents with matching state are merged together into a single
488  * extent in the tree.  Extents with EXTENT_IO in their state field
489  * are not merged because the end_io handlers need to be able to do
490  * operations on them without sleeping (or doing allocations/splits).
491  *
492  * This should be called with the tree lock held.
493  */
494 static void merge_state(struct extent_io_tree *tree,
495 		        struct extent_state *state)
496 {
497 	struct extent_state *other;
498 	struct rb_node *other_node;
499 
500 	if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
501 		return;
502 
503 	other_node = rb_prev(&state->rb_node);
504 	if (other_node) {
505 		other = rb_entry(other_node, struct extent_state, rb_node);
506 		if (other->end == state->start - 1 &&
507 		    other->state == state->state) {
508 			if (tree->private_data &&
509 			    is_data_inode(tree->private_data))
510 				btrfs_merge_delalloc_extent(tree->private_data,
511 							    state, other);
512 			state->start = other->start;
513 			rb_erase(&other->rb_node, &tree->state);
514 			RB_CLEAR_NODE(&other->rb_node);
515 			free_extent_state(other);
516 		}
517 	}
518 	other_node = rb_next(&state->rb_node);
519 	if (other_node) {
520 		other = rb_entry(other_node, struct extent_state, rb_node);
521 		if (other->start == state->end + 1 &&
522 		    other->state == state->state) {
523 			if (tree->private_data &&
524 			    is_data_inode(tree->private_data))
525 				btrfs_merge_delalloc_extent(tree->private_data,
526 							    state, other);
527 			state->end = other->end;
528 			rb_erase(&other->rb_node, &tree->state);
529 			RB_CLEAR_NODE(&other->rb_node);
530 			free_extent_state(other);
531 		}
532 	}
533 }
534 
535 static void set_state_bits(struct extent_io_tree *tree,
536 			   struct extent_state *state, u32 *bits,
537 			   struct extent_changeset *changeset);
538 
539 /*
540  * insert an extent_state struct into the tree.  'bits' are set on the
541  * struct before it is inserted.
542  *
543  * This may return -EEXIST if the extent is already there, in which case the
544  * state struct is freed.
545  *
546  * The tree lock is not taken internally.  This is a utility function and
547  * probably isn't what you want to call (see set/clear_extent_bit).
548  */
549 static int insert_state(struct extent_io_tree *tree,
550 			struct extent_state *state, u64 start, u64 end,
551 			struct rb_node ***p,
552 			struct rb_node **parent,
553 			u32 *bits, struct extent_changeset *changeset)
554 {
555 	struct rb_node *node;
556 
557 	if (end < start) {
558 		btrfs_err(tree->fs_info,
559 			"insert state: end < start %llu %llu", end, start);
560 		WARN_ON(1);
561 	}
562 	state->start = start;
563 	state->end = end;
564 
565 	set_state_bits(tree, state, bits, changeset);
566 
567 	node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
568 	if (node) {
569 		struct extent_state *found;
570 		found = rb_entry(node, struct extent_state, rb_node);
571 		btrfs_err(tree->fs_info,
572 		       "found node %llu %llu on insert of %llu %llu",
573 		       found->start, found->end, start, end);
574 		return -EEXIST;
575 	}
576 	merge_state(tree, state);
577 	return 0;
578 }
579 
580 /*
581  * split a given extent state struct in two, inserting the preallocated
582  * struct 'prealloc' as the newly created second half.  'split' indicates an
583  * offset inside 'orig' where it should be split.
584  *
585  * Before calling,
586  * the tree has 'orig' at [orig->start, orig->end].  After calling, there
587  * are two extent state structs in the tree:
588  * prealloc: [orig->start, split - 1]
589  * orig: [ split, orig->end ]
590  *
591  * The tree locks are not taken by this function. They need to be held
592  * by the caller.
593  */
594 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
595 		       struct extent_state *prealloc, u64 split)
596 {
597 	struct rb_node *node;
598 
599 	if (tree->private_data && is_data_inode(tree->private_data))
600 		btrfs_split_delalloc_extent(tree->private_data, orig, split);
601 
602 	prealloc->start = orig->start;
603 	prealloc->end = split - 1;
604 	prealloc->state = orig->state;
605 	orig->start = split;
606 
607 	node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
608 			   &prealloc->rb_node, NULL, NULL);
609 	if (node) {
610 		free_extent_state(prealloc);
611 		return -EEXIST;
612 	}
613 	return 0;
614 }
615 
616 static struct extent_state *next_state(struct extent_state *state)
617 {
618 	struct rb_node *next = rb_next(&state->rb_node);
619 	if (next)
620 		return rb_entry(next, struct extent_state, rb_node);
621 	else
622 		return NULL;
623 }
624 
625 /*
626  * utility function to clear some bits in an extent state struct.
627  * it will optionally wake up anyone waiting on this state (wake == 1).
628  *
629  * If no bits are set on the state struct after clearing things, the
630  * struct is freed and removed from the tree
631  */
632 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
633 					    struct extent_state *state,
634 					    u32 *bits, int wake,
635 					    struct extent_changeset *changeset)
636 {
637 	struct extent_state *next;
638 	u32 bits_to_clear = *bits & ~EXTENT_CTLBITS;
639 	int ret;
640 
641 	if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
642 		u64 range = state->end - state->start + 1;
643 		WARN_ON(range > tree->dirty_bytes);
644 		tree->dirty_bytes -= range;
645 	}
646 
647 	if (tree->private_data && is_data_inode(tree->private_data))
648 		btrfs_clear_delalloc_extent(tree->private_data, state, bits);
649 
650 	ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
651 	BUG_ON(ret < 0);
652 	state->state &= ~bits_to_clear;
653 	if (wake)
654 		wake_up(&state->wq);
655 	if (state->state == 0) {
656 		next = next_state(state);
657 		if (extent_state_in_tree(state)) {
658 			rb_erase(&state->rb_node, &tree->state);
659 			RB_CLEAR_NODE(&state->rb_node);
660 			free_extent_state(state);
661 		} else {
662 			WARN_ON(1);
663 		}
664 	} else {
665 		merge_state(tree, state);
666 		next = next_state(state);
667 	}
668 	return next;
669 }
670 
671 static struct extent_state *
672 alloc_extent_state_atomic(struct extent_state *prealloc)
673 {
674 	if (!prealloc)
675 		prealloc = alloc_extent_state(GFP_ATOMIC);
676 
677 	return prealloc;
678 }
679 
680 static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
681 {
682 	btrfs_panic(tree->fs_info, err,
683 	"locking error: extent tree was modified by another thread while locked");
684 }
685 
686 /*
687  * clear some bits on a range in the tree.  This may require splitting
688  * or inserting elements in the tree, so the gfp mask is used to
689  * indicate which allocations or sleeping are allowed.
690  *
691  * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
692  * the given range from the tree regardless of state (ie for truncate).
693  *
694  * the range [start, end] is inclusive.
695  *
696  * This takes the tree lock, and returns 0 on success and < 0 on error.
697  */
698 int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
699 		       u32 bits, int wake, int delete,
700 		       struct extent_state **cached_state,
701 		       gfp_t mask, struct extent_changeset *changeset)
702 {
703 	struct extent_state *state;
704 	struct extent_state *cached;
705 	struct extent_state *prealloc = NULL;
706 	struct rb_node *node;
707 	u64 last_end;
708 	int err;
709 	int clear = 0;
710 
711 	btrfs_debug_check_extent_io_range(tree, start, end);
712 	trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
713 
714 	if (bits & EXTENT_DELALLOC)
715 		bits |= EXTENT_NORESERVE;
716 
717 	if (delete)
718 		bits |= ~EXTENT_CTLBITS;
719 
720 	if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
721 		clear = 1;
722 again:
723 	if (!prealloc && gfpflags_allow_blocking(mask)) {
724 		/*
725 		 * Don't care for allocation failure here because we might end
726 		 * up not needing the pre-allocated extent state at all, which
727 		 * is the case if we only have in the tree extent states that
728 		 * cover our input range and don't cover too any other range.
729 		 * If we end up needing a new extent state we allocate it later.
730 		 */
731 		prealloc = alloc_extent_state(mask);
732 	}
733 
734 	spin_lock(&tree->lock);
735 	if (cached_state) {
736 		cached = *cached_state;
737 
738 		if (clear) {
739 			*cached_state = NULL;
740 			cached_state = NULL;
741 		}
742 
743 		if (cached && extent_state_in_tree(cached) &&
744 		    cached->start <= start && cached->end > start) {
745 			if (clear)
746 				refcount_dec(&cached->refs);
747 			state = cached;
748 			goto hit_next;
749 		}
750 		if (clear)
751 			free_extent_state(cached);
752 	}
753 	/*
754 	 * this search will find the extents that end after
755 	 * our range starts
756 	 */
757 	node = tree_search(tree, start);
758 	if (!node)
759 		goto out;
760 	state = rb_entry(node, struct extent_state, rb_node);
761 hit_next:
762 	if (state->start > end)
763 		goto out;
764 	WARN_ON(state->end < start);
765 	last_end = state->end;
766 
767 	/* the state doesn't have the wanted bits, go ahead */
768 	if (!(state->state & bits)) {
769 		state = next_state(state);
770 		goto next;
771 	}
772 
773 	/*
774 	 *     | ---- desired range ---- |
775 	 *  | state | or
776 	 *  | ------------- state -------------- |
777 	 *
778 	 * We need to split the extent we found, and may flip
779 	 * bits on second half.
780 	 *
781 	 * If the extent we found extends past our range, we
782 	 * just split and search again.  It'll get split again
783 	 * the next time though.
784 	 *
785 	 * If the extent we found is inside our range, we clear
786 	 * the desired bit on it.
787 	 */
788 
789 	if (state->start < start) {
790 		prealloc = alloc_extent_state_atomic(prealloc);
791 		BUG_ON(!prealloc);
792 		err = split_state(tree, state, prealloc, start);
793 		if (err)
794 			extent_io_tree_panic(tree, err);
795 
796 		prealloc = NULL;
797 		if (err)
798 			goto out;
799 		if (state->end <= end) {
800 			state = clear_state_bit(tree, state, &bits, wake,
801 						changeset);
802 			goto next;
803 		}
804 		goto search_again;
805 	}
806 	/*
807 	 * | ---- desired range ---- |
808 	 *                        | state |
809 	 * We need to split the extent, and clear the bit
810 	 * on the first half
811 	 */
812 	if (state->start <= end && state->end > end) {
813 		prealloc = alloc_extent_state_atomic(prealloc);
814 		BUG_ON(!prealloc);
815 		err = split_state(tree, state, prealloc, end + 1);
816 		if (err)
817 			extent_io_tree_panic(tree, err);
818 
819 		if (wake)
820 			wake_up(&state->wq);
821 
822 		clear_state_bit(tree, prealloc, &bits, wake, changeset);
823 
824 		prealloc = NULL;
825 		goto out;
826 	}
827 
828 	state = clear_state_bit(tree, state, &bits, wake, changeset);
829 next:
830 	if (last_end == (u64)-1)
831 		goto out;
832 	start = last_end + 1;
833 	if (start <= end && state && !need_resched())
834 		goto hit_next;
835 
836 search_again:
837 	if (start > end)
838 		goto out;
839 	spin_unlock(&tree->lock);
840 	if (gfpflags_allow_blocking(mask))
841 		cond_resched();
842 	goto again;
843 
844 out:
845 	spin_unlock(&tree->lock);
846 	if (prealloc)
847 		free_extent_state(prealloc);
848 
849 	return 0;
850 
851 }
852 
853 static void wait_on_state(struct extent_io_tree *tree,
854 			  struct extent_state *state)
855 		__releases(tree->lock)
856 		__acquires(tree->lock)
857 {
858 	DEFINE_WAIT(wait);
859 	prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
860 	spin_unlock(&tree->lock);
861 	schedule();
862 	spin_lock(&tree->lock);
863 	finish_wait(&state->wq, &wait);
864 }
865 
866 /*
867  * waits for one or more bits to clear on a range in the state tree.
868  * The range [start, end] is inclusive.
869  * The tree lock is taken by this function
870  */
871 static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
872 			    u32 bits)
873 {
874 	struct extent_state *state;
875 	struct rb_node *node;
876 
877 	btrfs_debug_check_extent_io_range(tree, start, end);
878 
879 	spin_lock(&tree->lock);
880 again:
881 	while (1) {
882 		/*
883 		 * this search will find all the extents that end after
884 		 * our range starts
885 		 */
886 		node = tree_search(tree, start);
887 process_node:
888 		if (!node)
889 			break;
890 
891 		state = rb_entry(node, struct extent_state, rb_node);
892 
893 		if (state->start > end)
894 			goto out;
895 
896 		if (state->state & bits) {
897 			start = state->start;
898 			refcount_inc(&state->refs);
899 			wait_on_state(tree, state);
900 			free_extent_state(state);
901 			goto again;
902 		}
903 		start = state->end + 1;
904 
905 		if (start > end)
906 			break;
907 
908 		if (!cond_resched_lock(&tree->lock)) {
909 			node = rb_next(node);
910 			goto process_node;
911 		}
912 	}
913 out:
914 	spin_unlock(&tree->lock);
915 }
916 
917 static void set_state_bits(struct extent_io_tree *tree,
918 			   struct extent_state *state,
919 			   u32 *bits, struct extent_changeset *changeset)
920 {
921 	u32 bits_to_set = *bits & ~EXTENT_CTLBITS;
922 	int ret;
923 
924 	if (tree->private_data && is_data_inode(tree->private_data))
925 		btrfs_set_delalloc_extent(tree->private_data, state, bits);
926 
927 	if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
928 		u64 range = state->end - state->start + 1;
929 		tree->dirty_bytes += range;
930 	}
931 	ret = add_extent_changeset(state, bits_to_set, changeset, 1);
932 	BUG_ON(ret < 0);
933 	state->state |= bits_to_set;
934 }
935 
936 static void cache_state_if_flags(struct extent_state *state,
937 				 struct extent_state **cached_ptr,
938 				 unsigned flags)
939 {
940 	if (cached_ptr && !(*cached_ptr)) {
941 		if (!flags || (state->state & flags)) {
942 			*cached_ptr = state;
943 			refcount_inc(&state->refs);
944 		}
945 	}
946 }
947 
948 static void cache_state(struct extent_state *state,
949 			struct extent_state **cached_ptr)
950 {
951 	return cache_state_if_flags(state, cached_ptr,
952 				    EXTENT_LOCKED | EXTENT_BOUNDARY);
953 }
954 
955 /*
956  * set some bits on a range in the tree.  This may require allocations or
957  * sleeping, so the gfp mask is used to indicate what is allowed.
958  *
959  * If any of the exclusive bits are set, this will fail with -EEXIST if some
960  * part of the range already has the desired bits set.  The start of the
961  * existing range is returned in failed_start in this case.
962  *
963  * [start, end] is inclusive This takes the tree lock.
964  */
965 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
966 		   u32 exclusive_bits, u64 *failed_start,
967 		   struct extent_state **cached_state, gfp_t mask,
968 		   struct extent_changeset *changeset)
969 {
970 	struct extent_state *state;
971 	struct extent_state *prealloc = NULL;
972 	struct rb_node *node;
973 	struct rb_node **p;
974 	struct rb_node *parent;
975 	int err = 0;
976 	u64 last_start;
977 	u64 last_end;
978 
979 	btrfs_debug_check_extent_io_range(tree, start, end);
980 	trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
981 
982 	if (exclusive_bits)
983 		ASSERT(failed_start);
984 	else
985 		ASSERT(failed_start == NULL);
986 again:
987 	if (!prealloc && gfpflags_allow_blocking(mask)) {
988 		/*
989 		 * Don't care for allocation failure here because we might end
990 		 * up not needing the pre-allocated extent state at all, which
991 		 * is the case if we only have in the tree extent states that
992 		 * cover our input range and don't cover too any other range.
993 		 * If we end up needing a new extent state we allocate it later.
994 		 */
995 		prealloc = alloc_extent_state(mask);
996 	}
997 
998 	spin_lock(&tree->lock);
999 	if (cached_state && *cached_state) {
1000 		state = *cached_state;
1001 		if (state->start <= start && state->end > start &&
1002 		    extent_state_in_tree(state)) {
1003 			node = &state->rb_node;
1004 			goto hit_next;
1005 		}
1006 	}
1007 	/*
1008 	 * this search will find all the extents that end after
1009 	 * our range starts.
1010 	 */
1011 	node = tree_search_for_insert(tree, start, &p, &parent);
1012 	if (!node) {
1013 		prealloc = alloc_extent_state_atomic(prealloc);
1014 		BUG_ON(!prealloc);
1015 		err = insert_state(tree, prealloc, start, end,
1016 				   &p, &parent, &bits, changeset);
1017 		if (err)
1018 			extent_io_tree_panic(tree, err);
1019 
1020 		cache_state(prealloc, cached_state);
1021 		prealloc = NULL;
1022 		goto out;
1023 	}
1024 	state = rb_entry(node, struct extent_state, rb_node);
1025 hit_next:
1026 	last_start = state->start;
1027 	last_end = state->end;
1028 
1029 	/*
1030 	 * | ---- desired range ---- |
1031 	 * | state |
1032 	 *
1033 	 * Just lock what we found and keep going
1034 	 */
1035 	if (state->start == start && state->end <= end) {
1036 		if (state->state & exclusive_bits) {
1037 			*failed_start = state->start;
1038 			err = -EEXIST;
1039 			goto out;
1040 		}
1041 
1042 		set_state_bits(tree, state, &bits, changeset);
1043 		cache_state(state, cached_state);
1044 		merge_state(tree, state);
1045 		if (last_end == (u64)-1)
1046 			goto out;
1047 		start = last_end + 1;
1048 		state = next_state(state);
1049 		if (start < end && state && state->start == start &&
1050 		    !need_resched())
1051 			goto hit_next;
1052 		goto search_again;
1053 	}
1054 
1055 	/*
1056 	 *     | ---- desired range ---- |
1057 	 * | state |
1058 	 *   or
1059 	 * | ------------- state -------------- |
1060 	 *
1061 	 * We need to split the extent we found, and may flip bits on
1062 	 * second half.
1063 	 *
1064 	 * If the extent we found extends past our
1065 	 * range, we just split and search again.  It'll get split
1066 	 * again the next time though.
1067 	 *
1068 	 * If the extent we found is inside our range, we set the
1069 	 * desired bit on it.
1070 	 */
1071 	if (state->start < start) {
1072 		if (state->state & exclusive_bits) {
1073 			*failed_start = start;
1074 			err = -EEXIST;
1075 			goto out;
1076 		}
1077 
1078 		/*
1079 		 * If this extent already has all the bits we want set, then
1080 		 * skip it, not necessary to split it or do anything with it.
1081 		 */
1082 		if ((state->state & bits) == bits) {
1083 			start = state->end + 1;
1084 			cache_state(state, cached_state);
1085 			goto search_again;
1086 		}
1087 
1088 		prealloc = alloc_extent_state_atomic(prealloc);
1089 		BUG_ON(!prealloc);
1090 		err = split_state(tree, state, prealloc, start);
1091 		if (err)
1092 			extent_io_tree_panic(tree, err);
1093 
1094 		prealloc = NULL;
1095 		if (err)
1096 			goto out;
1097 		if (state->end <= end) {
1098 			set_state_bits(tree, state, &bits, changeset);
1099 			cache_state(state, cached_state);
1100 			merge_state(tree, state);
1101 			if (last_end == (u64)-1)
1102 				goto out;
1103 			start = last_end + 1;
1104 			state = next_state(state);
1105 			if (start < end && state && state->start == start &&
1106 			    !need_resched())
1107 				goto hit_next;
1108 		}
1109 		goto search_again;
1110 	}
1111 	/*
1112 	 * | ---- desired range ---- |
1113 	 *     | state | or               | state |
1114 	 *
1115 	 * There's a hole, we need to insert something in it and
1116 	 * ignore the extent we found.
1117 	 */
1118 	if (state->start > start) {
1119 		u64 this_end;
1120 		if (end < last_start)
1121 			this_end = end;
1122 		else
1123 			this_end = last_start - 1;
1124 
1125 		prealloc = alloc_extent_state_atomic(prealloc);
1126 		BUG_ON(!prealloc);
1127 
1128 		/*
1129 		 * Avoid to free 'prealloc' if it can be merged with
1130 		 * the later extent.
1131 		 */
1132 		err = insert_state(tree, prealloc, start, this_end,
1133 				   NULL, NULL, &bits, changeset);
1134 		if (err)
1135 			extent_io_tree_panic(tree, err);
1136 
1137 		cache_state(prealloc, cached_state);
1138 		prealloc = NULL;
1139 		start = this_end + 1;
1140 		goto search_again;
1141 	}
1142 	/*
1143 	 * | ---- desired range ---- |
1144 	 *                        | state |
1145 	 * We need to split the extent, and set the bit
1146 	 * on the first half
1147 	 */
1148 	if (state->start <= end && state->end > end) {
1149 		if (state->state & exclusive_bits) {
1150 			*failed_start = start;
1151 			err = -EEXIST;
1152 			goto out;
1153 		}
1154 
1155 		prealloc = alloc_extent_state_atomic(prealloc);
1156 		BUG_ON(!prealloc);
1157 		err = split_state(tree, state, prealloc, end + 1);
1158 		if (err)
1159 			extent_io_tree_panic(tree, err);
1160 
1161 		set_state_bits(tree, prealloc, &bits, changeset);
1162 		cache_state(prealloc, cached_state);
1163 		merge_state(tree, prealloc);
1164 		prealloc = NULL;
1165 		goto out;
1166 	}
1167 
1168 search_again:
1169 	if (start > end)
1170 		goto out;
1171 	spin_unlock(&tree->lock);
1172 	if (gfpflags_allow_blocking(mask))
1173 		cond_resched();
1174 	goto again;
1175 
1176 out:
1177 	spin_unlock(&tree->lock);
1178 	if (prealloc)
1179 		free_extent_state(prealloc);
1180 
1181 	return err;
1182 
1183 }
1184 
1185 /**
1186  * convert_extent_bit - convert all bits in a given range from one bit to
1187  * 			another
1188  * @tree:	the io tree to search
1189  * @start:	the start offset in bytes
1190  * @end:	the end offset in bytes (inclusive)
1191  * @bits:	the bits to set in this range
1192  * @clear_bits:	the bits to clear in this range
1193  * @cached_state:	state that we're going to cache
1194  *
1195  * This will go through and set bits for the given range.  If any states exist
1196  * already in this range they are set with the given bit and cleared of the
1197  * clear_bits.  This is only meant to be used by things that are mergeable, ie
1198  * converting from say DELALLOC to DIRTY.  This is not meant to be used with
1199  * boundary bits like LOCK.
1200  *
1201  * All allocations are done with GFP_NOFS.
1202  */
1203 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1204 		       u32 bits, u32 clear_bits,
1205 		       struct extent_state **cached_state)
1206 {
1207 	struct extent_state *state;
1208 	struct extent_state *prealloc = NULL;
1209 	struct rb_node *node;
1210 	struct rb_node **p;
1211 	struct rb_node *parent;
1212 	int err = 0;
1213 	u64 last_start;
1214 	u64 last_end;
1215 	bool first_iteration = true;
1216 
1217 	btrfs_debug_check_extent_io_range(tree, start, end);
1218 	trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1219 				       clear_bits);
1220 
1221 again:
1222 	if (!prealloc) {
1223 		/*
1224 		 * Best effort, don't worry if extent state allocation fails
1225 		 * here for the first iteration. We might have a cached state
1226 		 * that matches exactly the target range, in which case no
1227 		 * extent state allocations are needed. We'll only know this
1228 		 * after locking the tree.
1229 		 */
1230 		prealloc = alloc_extent_state(GFP_NOFS);
1231 		if (!prealloc && !first_iteration)
1232 			return -ENOMEM;
1233 	}
1234 
1235 	spin_lock(&tree->lock);
1236 	if (cached_state && *cached_state) {
1237 		state = *cached_state;
1238 		if (state->start <= start && state->end > start &&
1239 		    extent_state_in_tree(state)) {
1240 			node = &state->rb_node;
1241 			goto hit_next;
1242 		}
1243 	}
1244 
1245 	/*
1246 	 * this search will find all the extents that end after
1247 	 * our range starts.
1248 	 */
1249 	node = tree_search_for_insert(tree, start, &p, &parent);
1250 	if (!node) {
1251 		prealloc = alloc_extent_state_atomic(prealloc);
1252 		if (!prealloc) {
1253 			err = -ENOMEM;
1254 			goto out;
1255 		}
1256 		err = insert_state(tree, prealloc, start, end,
1257 				   &p, &parent, &bits, NULL);
1258 		if (err)
1259 			extent_io_tree_panic(tree, err);
1260 		cache_state(prealloc, cached_state);
1261 		prealloc = NULL;
1262 		goto out;
1263 	}
1264 	state = rb_entry(node, struct extent_state, rb_node);
1265 hit_next:
1266 	last_start = state->start;
1267 	last_end = state->end;
1268 
1269 	/*
1270 	 * | ---- desired range ---- |
1271 	 * | state |
1272 	 *
1273 	 * Just lock what we found and keep going
1274 	 */
1275 	if (state->start == start && state->end <= end) {
1276 		set_state_bits(tree, state, &bits, NULL);
1277 		cache_state(state, cached_state);
1278 		state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
1279 		if (last_end == (u64)-1)
1280 			goto out;
1281 		start = last_end + 1;
1282 		if (start < end && state && state->start == start &&
1283 		    !need_resched())
1284 			goto hit_next;
1285 		goto search_again;
1286 	}
1287 
1288 	/*
1289 	 *     | ---- desired range ---- |
1290 	 * | state |
1291 	 *   or
1292 	 * | ------------- state -------------- |
1293 	 *
1294 	 * We need to split the extent we found, and may flip bits on
1295 	 * second half.
1296 	 *
1297 	 * If the extent we found extends past our
1298 	 * range, we just split and search again.  It'll get split
1299 	 * again the next time though.
1300 	 *
1301 	 * If the extent we found is inside our range, we set the
1302 	 * desired bit on it.
1303 	 */
1304 	if (state->start < start) {
1305 		prealloc = alloc_extent_state_atomic(prealloc);
1306 		if (!prealloc) {
1307 			err = -ENOMEM;
1308 			goto out;
1309 		}
1310 		err = split_state(tree, state, prealloc, start);
1311 		if (err)
1312 			extent_io_tree_panic(tree, err);
1313 		prealloc = NULL;
1314 		if (err)
1315 			goto out;
1316 		if (state->end <= end) {
1317 			set_state_bits(tree, state, &bits, NULL);
1318 			cache_state(state, cached_state);
1319 			state = clear_state_bit(tree, state, &clear_bits, 0,
1320 						NULL);
1321 			if (last_end == (u64)-1)
1322 				goto out;
1323 			start = last_end + 1;
1324 			if (start < end && state && state->start == start &&
1325 			    !need_resched())
1326 				goto hit_next;
1327 		}
1328 		goto search_again;
1329 	}
1330 	/*
1331 	 * | ---- desired range ---- |
1332 	 *     | state | or               | state |
1333 	 *
1334 	 * There's a hole, we need to insert something in it and
1335 	 * ignore the extent we found.
1336 	 */
1337 	if (state->start > start) {
1338 		u64 this_end;
1339 		if (end < last_start)
1340 			this_end = end;
1341 		else
1342 			this_end = last_start - 1;
1343 
1344 		prealloc = alloc_extent_state_atomic(prealloc);
1345 		if (!prealloc) {
1346 			err = -ENOMEM;
1347 			goto out;
1348 		}
1349 
1350 		/*
1351 		 * Avoid to free 'prealloc' if it can be merged with
1352 		 * the later extent.
1353 		 */
1354 		err = insert_state(tree, prealloc, start, this_end,
1355 				   NULL, NULL, &bits, NULL);
1356 		if (err)
1357 			extent_io_tree_panic(tree, err);
1358 		cache_state(prealloc, cached_state);
1359 		prealloc = NULL;
1360 		start = this_end + 1;
1361 		goto search_again;
1362 	}
1363 	/*
1364 	 * | ---- desired range ---- |
1365 	 *                        | state |
1366 	 * We need to split the extent, and set the bit
1367 	 * on the first half
1368 	 */
1369 	if (state->start <= end && state->end > end) {
1370 		prealloc = alloc_extent_state_atomic(prealloc);
1371 		if (!prealloc) {
1372 			err = -ENOMEM;
1373 			goto out;
1374 		}
1375 
1376 		err = split_state(tree, state, prealloc, end + 1);
1377 		if (err)
1378 			extent_io_tree_panic(tree, err);
1379 
1380 		set_state_bits(tree, prealloc, &bits, NULL);
1381 		cache_state(prealloc, cached_state);
1382 		clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
1383 		prealloc = NULL;
1384 		goto out;
1385 	}
1386 
1387 search_again:
1388 	if (start > end)
1389 		goto out;
1390 	spin_unlock(&tree->lock);
1391 	cond_resched();
1392 	first_iteration = false;
1393 	goto again;
1394 
1395 out:
1396 	spin_unlock(&tree->lock);
1397 	if (prealloc)
1398 		free_extent_state(prealloc);
1399 
1400 	return err;
1401 }
1402 
1403 /* wrappers around set/clear extent bit */
1404 int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1405 			   u32 bits, struct extent_changeset *changeset)
1406 {
1407 	/*
1408 	 * We don't support EXTENT_LOCKED yet, as current changeset will
1409 	 * record any bits changed, so for EXTENT_LOCKED case, it will
1410 	 * either fail with -EEXIST or changeset will record the whole
1411 	 * range.
1412 	 */
1413 	BUG_ON(bits & EXTENT_LOCKED);
1414 
1415 	return set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
1416 			      changeset);
1417 }
1418 
1419 int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
1420 			   u32 bits)
1421 {
1422 	return set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
1423 			      GFP_NOWAIT, NULL);
1424 }
1425 
1426 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1427 		     u32 bits, int wake, int delete,
1428 		     struct extent_state **cached)
1429 {
1430 	return __clear_extent_bit(tree, start, end, bits, wake, delete,
1431 				  cached, GFP_NOFS, NULL);
1432 }
1433 
1434 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1435 		u32 bits, struct extent_changeset *changeset)
1436 {
1437 	/*
1438 	 * Don't support EXTENT_LOCKED case, same reason as
1439 	 * set_record_extent_bits().
1440 	 */
1441 	BUG_ON(bits & EXTENT_LOCKED);
1442 
1443 	return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
1444 				  changeset);
1445 }
1446 
1447 /*
1448  * either insert or lock state struct between start and end use mask to tell
1449  * us if waiting is desired.
1450  */
1451 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1452 		     struct extent_state **cached_state)
1453 {
1454 	int err;
1455 	u64 failed_start;
1456 
1457 	while (1) {
1458 		err = set_extent_bit(tree, start, end, EXTENT_LOCKED,
1459 				     EXTENT_LOCKED, &failed_start,
1460 				     cached_state, GFP_NOFS, NULL);
1461 		if (err == -EEXIST) {
1462 			wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1463 			start = failed_start;
1464 		} else
1465 			break;
1466 		WARN_ON(start > end);
1467 	}
1468 	return err;
1469 }
1470 
1471 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1472 {
1473 	int err;
1474 	u64 failed_start;
1475 
1476 	err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1477 			     &failed_start, NULL, GFP_NOFS, NULL);
1478 	if (err == -EEXIST) {
1479 		if (failed_start > start)
1480 			clear_extent_bit(tree, start, failed_start - 1,
1481 					 EXTENT_LOCKED, 1, 0, NULL);
1482 		return 0;
1483 	}
1484 	return 1;
1485 }
1486 
1487 void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1488 {
1489 	unsigned long index = start >> PAGE_SHIFT;
1490 	unsigned long end_index = end >> PAGE_SHIFT;
1491 	struct page *page;
1492 
1493 	while (index <= end_index) {
1494 		page = find_get_page(inode->i_mapping, index);
1495 		BUG_ON(!page); /* Pages should be in the extent_io_tree */
1496 		clear_page_dirty_for_io(page);
1497 		put_page(page);
1498 		index++;
1499 	}
1500 }
1501 
1502 void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1503 {
1504 	unsigned long index = start >> PAGE_SHIFT;
1505 	unsigned long end_index = end >> PAGE_SHIFT;
1506 	struct page *page;
1507 
1508 	while (index <= end_index) {
1509 		page = find_get_page(inode->i_mapping, index);
1510 		BUG_ON(!page); /* Pages should be in the extent_io_tree */
1511 		__set_page_dirty_nobuffers(page);
1512 		account_page_redirty(page);
1513 		put_page(page);
1514 		index++;
1515 	}
1516 }
1517 
1518 /* find the first state struct with 'bits' set after 'start', and
1519  * return it.  tree->lock must be held.  NULL will returned if
1520  * nothing was found after 'start'
1521  */
1522 static struct extent_state *
1523 find_first_extent_bit_state(struct extent_io_tree *tree, u64 start, u32 bits)
1524 {
1525 	struct rb_node *node;
1526 	struct extent_state *state;
1527 
1528 	/*
1529 	 * this search will find all the extents that end after
1530 	 * our range starts.
1531 	 */
1532 	node = tree_search(tree, start);
1533 	if (!node)
1534 		goto out;
1535 
1536 	while (1) {
1537 		state = rb_entry(node, struct extent_state, rb_node);
1538 		if (state->end >= start && (state->state & bits))
1539 			return state;
1540 
1541 		node = rb_next(node);
1542 		if (!node)
1543 			break;
1544 	}
1545 out:
1546 	return NULL;
1547 }
1548 
1549 /*
1550  * Find the first offset in the io tree with one or more @bits set.
1551  *
1552  * Note: If there are multiple bits set in @bits, any of them will match.
1553  *
1554  * Return 0 if we find something, and update @start_ret and @end_ret.
1555  * Return 1 if we found nothing.
1556  */
1557 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1558 			  u64 *start_ret, u64 *end_ret, u32 bits,
1559 			  struct extent_state **cached_state)
1560 {
1561 	struct extent_state *state;
1562 	int ret = 1;
1563 
1564 	spin_lock(&tree->lock);
1565 	if (cached_state && *cached_state) {
1566 		state = *cached_state;
1567 		if (state->end == start - 1 && extent_state_in_tree(state)) {
1568 			while ((state = next_state(state)) != NULL) {
1569 				if (state->state & bits)
1570 					goto got_it;
1571 			}
1572 			free_extent_state(*cached_state);
1573 			*cached_state = NULL;
1574 			goto out;
1575 		}
1576 		free_extent_state(*cached_state);
1577 		*cached_state = NULL;
1578 	}
1579 
1580 	state = find_first_extent_bit_state(tree, start, bits);
1581 got_it:
1582 	if (state) {
1583 		cache_state_if_flags(state, cached_state, 0);
1584 		*start_ret = state->start;
1585 		*end_ret = state->end;
1586 		ret = 0;
1587 	}
1588 out:
1589 	spin_unlock(&tree->lock);
1590 	return ret;
1591 }
1592 
1593 /**
1594  * Find a contiguous area of bits
1595  *
1596  * @tree:      io tree to check
1597  * @start:     offset to start the search from
1598  * @start_ret: the first offset we found with the bits set
1599  * @end_ret:   the final contiguous range of the bits that were set
1600  * @bits:      bits to look for
1601  *
1602  * set_extent_bit and clear_extent_bit can temporarily split contiguous ranges
1603  * to set bits appropriately, and then merge them again.  During this time it
1604  * will drop the tree->lock, so use this helper if you want to find the actual
1605  * contiguous area for given bits.  We will search to the first bit we find, and
1606  * then walk down the tree until we find a non-contiguous area.  The area
1607  * returned will be the full contiguous area with the bits set.
1608  */
1609 int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
1610 			       u64 *start_ret, u64 *end_ret, u32 bits)
1611 {
1612 	struct extent_state *state;
1613 	int ret = 1;
1614 
1615 	spin_lock(&tree->lock);
1616 	state = find_first_extent_bit_state(tree, start, bits);
1617 	if (state) {
1618 		*start_ret = state->start;
1619 		*end_ret = state->end;
1620 		while ((state = next_state(state)) != NULL) {
1621 			if (state->start > (*end_ret + 1))
1622 				break;
1623 			*end_ret = state->end;
1624 		}
1625 		ret = 0;
1626 	}
1627 	spin_unlock(&tree->lock);
1628 	return ret;
1629 }
1630 
1631 /**
1632  * Find the first range that has @bits not set. This range could start before
1633  * @start.
1634  *
1635  * @tree:      the tree to search
1636  * @start:     offset at/after which the found extent should start
1637  * @start_ret: records the beginning of the range
1638  * @end_ret:   records the end of the range (inclusive)
1639  * @bits:      the set of bits which must be unset
1640  *
1641  * Since unallocated range is also considered one which doesn't have the bits
1642  * set it's possible that @end_ret contains -1, this happens in case the range
1643  * spans (last_range_end, end of device]. In this case it's up to the caller to
1644  * trim @end_ret to the appropriate size.
1645  */
1646 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
1647 				 u64 *start_ret, u64 *end_ret, u32 bits)
1648 {
1649 	struct extent_state *state;
1650 	struct rb_node *node, *prev = NULL, *next;
1651 
1652 	spin_lock(&tree->lock);
1653 
1654 	/* Find first extent with bits cleared */
1655 	while (1) {
1656 		node = __etree_search(tree, start, &next, &prev, NULL, NULL);
1657 		if (!node && !next && !prev) {
1658 			/*
1659 			 * Tree is completely empty, send full range and let
1660 			 * caller deal with it
1661 			 */
1662 			*start_ret = 0;
1663 			*end_ret = -1;
1664 			goto out;
1665 		} else if (!node && !next) {
1666 			/*
1667 			 * We are past the last allocated chunk, set start at
1668 			 * the end of the last extent.
1669 			 */
1670 			state = rb_entry(prev, struct extent_state, rb_node);
1671 			*start_ret = state->end + 1;
1672 			*end_ret = -1;
1673 			goto out;
1674 		} else if (!node) {
1675 			node = next;
1676 		}
1677 		/*
1678 		 * At this point 'node' either contains 'start' or start is
1679 		 * before 'node'
1680 		 */
1681 		state = rb_entry(node, struct extent_state, rb_node);
1682 
1683 		if (in_range(start, state->start, state->end - state->start + 1)) {
1684 			if (state->state & bits) {
1685 				/*
1686 				 * |--range with bits sets--|
1687 				 *    |
1688 				 *    start
1689 				 */
1690 				start = state->end + 1;
1691 			} else {
1692 				/*
1693 				 * 'start' falls within a range that doesn't
1694 				 * have the bits set, so take its start as
1695 				 * the beginning of the desired range
1696 				 *
1697 				 * |--range with bits cleared----|
1698 				 *      |
1699 				 *      start
1700 				 */
1701 				*start_ret = state->start;
1702 				break;
1703 			}
1704 		} else {
1705 			/*
1706 			 * |---prev range---|---hole/unset---|---node range---|
1707 			 *                          |
1708 			 *                        start
1709 			 *
1710 			 *                        or
1711 			 *
1712 			 * |---hole/unset--||--first node--|
1713 			 * 0   |
1714 			 *    start
1715 			 */
1716 			if (prev) {
1717 				state = rb_entry(prev, struct extent_state,
1718 						 rb_node);
1719 				*start_ret = state->end + 1;
1720 			} else {
1721 				*start_ret = 0;
1722 			}
1723 			break;
1724 		}
1725 	}
1726 
1727 	/*
1728 	 * Find the longest stretch from start until an entry which has the
1729 	 * bits set
1730 	 */
1731 	while (1) {
1732 		state = rb_entry(node, struct extent_state, rb_node);
1733 		if (state->end >= start && !(state->state & bits)) {
1734 			*end_ret = state->end;
1735 		} else {
1736 			*end_ret = state->start - 1;
1737 			break;
1738 		}
1739 
1740 		node = rb_next(node);
1741 		if (!node)
1742 			break;
1743 	}
1744 out:
1745 	spin_unlock(&tree->lock);
1746 }
1747 
1748 /*
1749  * find a contiguous range of bytes in the file marked as delalloc, not
1750  * more than 'max_bytes'.  start and end are used to return the range,
1751  *
1752  * true is returned if we find something, false if nothing was in the tree
1753  */
1754 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
1755 			       u64 *end, u64 max_bytes,
1756 			       struct extent_state **cached_state)
1757 {
1758 	struct rb_node *node;
1759 	struct extent_state *state;
1760 	u64 cur_start = *start;
1761 	bool found = false;
1762 	u64 total_bytes = 0;
1763 
1764 	spin_lock(&tree->lock);
1765 
1766 	/*
1767 	 * this search will find all the extents that end after
1768 	 * our range starts.
1769 	 */
1770 	node = tree_search(tree, cur_start);
1771 	if (!node) {
1772 		*end = (u64)-1;
1773 		goto out;
1774 	}
1775 
1776 	while (1) {
1777 		state = rb_entry(node, struct extent_state, rb_node);
1778 		if (found && (state->start != cur_start ||
1779 			      (state->state & EXTENT_BOUNDARY))) {
1780 			goto out;
1781 		}
1782 		if (!(state->state & EXTENT_DELALLOC)) {
1783 			if (!found)
1784 				*end = state->end;
1785 			goto out;
1786 		}
1787 		if (!found) {
1788 			*start = state->start;
1789 			*cached_state = state;
1790 			refcount_inc(&state->refs);
1791 		}
1792 		found = true;
1793 		*end = state->end;
1794 		cur_start = state->end + 1;
1795 		node = rb_next(node);
1796 		total_bytes += state->end - state->start + 1;
1797 		if (total_bytes >= max_bytes)
1798 			break;
1799 		if (!node)
1800 			break;
1801 	}
1802 out:
1803 	spin_unlock(&tree->lock);
1804 	return found;
1805 }
1806 
1807 static int __process_pages_contig(struct address_space *mapping,
1808 				  struct page *locked_page,
1809 				  pgoff_t start_index, pgoff_t end_index,
1810 				  unsigned long page_ops, pgoff_t *index_ret);
1811 
1812 static noinline void __unlock_for_delalloc(struct inode *inode,
1813 					   struct page *locked_page,
1814 					   u64 start, u64 end)
1815 {
1816 	unsigned long index = start >> PAGE_SHIFT;
1817 	unsigned long end_index = end >> PAGE_SHIFT;
1818 
1819 	ASSERT(locked_page);
1820 	if (index == locked_page->index && end_index == index)
1821 		return;
1822 
1823 	__process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1824 			       PAGE_UNLOCK, NULL);
1825 }
1826 
1827 static noinline int lock_delalloc_pages(struct inode *inode,
1828 					struct page *locked_page,
1829 					u64 delalloc_start,
1830 					u64 delalloc_end)
1831 {
1832 	unsigned long index = delalloc_start >> PAGE_SHIFT;
1833 	unsigned long index_ret = index;
1834 	unsigned long end_index = delalloc_end >> PAGE_SHIFT;
1835 	int ret;
1836 
1837 	ASSERT(locked_page);
1838 	if (index == locked_page->index && index == end_index)
1839 		return 0;
1840 
1841 	ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1842 				     end_index, PAGE_LOCK, &index_ret);
1843 	if (ret == -EAGAIN)
1844 		__unlock_for_delalloc(inode, locked_page, delalloc_start,
1845 				      (u64)index_ret << PAGE_SHIFT);
1846 	return ret;
1847 }
1848 
1849 /*
1850  * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1851  * more than @max_bytes.  @Start and @end are used to return the range,
1852  *
1853  * Return: true if we find something
1854  *         false if nothing was in the tree
1855  */
1856 EXPORT_FOR_TESTS
1857 noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
1858 				    struct page *locked_page, u64 *start,
1859 				    u64 *end)
1860 {
1861 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
1862 	u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
1863 	u64 delalloc_start;
1864 	u64 delalloc_end;
1865 	bool found;
1866 	struct extent_state *cached_state = NULL;
1867 	int ret;
1868 	int loops = 0;
1869 
1870 again:
1871 	/* step one, find a bunch of delalloc bytes starting at start */
1872 	delalloc_start = *start;
1873 	delalloc_end = 0;
1874 	found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1875 					  max_bytes, &cached_state);
1876 	if (!found || delalloc_end <= *start) {
1877 		*start = delalloc_start;
1878 		*end = delalloc_end;
1879 		free_extent_state(cached_state);
1880 		return false;
1881 	}
1882 
1883 	/*
1884 	 * start comes from the offset of locked_page.  We have to lock
1885 	 * pages in order, so we can't process delalloc bytes before
1886 	 * locked_page
1887 	 */
1888 	if (delalloc_start < *start)
1889 		delalloc_start = *start;
1890 
1891 	/*
1892 	 * make sure to limit the number of pages we try to lock down
1893 	 */
1894 	if (delalloc_end + 1 - delalloc_start > max_bytes)
1895 		delalloc_end = delalloc_start + max_bytes - 1;
1896 
1897 	/* step two, lock all the pages after the page that has start */
1898 	ret = lock_delalloc_pages(inode, locked_page,
1899 				  delalloc_start, delalloc_end);
1900 	ASSERT(!ret || ret == -EAGAIN);
1901 	if (ret == -EAGAIN) {
1902 		/* some of the pages are gone, lets avoid looping by
1903 		 * shortening the size of the delalloc range we're searching
1904 		 */
1905 		free_extent_state(cached_state);
1906 		cached_state = NULL;
1907 		if (!loops) {
1908 			max_bytes = PAGE_SIZE;
1909 			loops = 1;
1910 			goto again;
1911 		} else {
1912 			found = false;
1913 			goto out_failed;
1914 		}
1915 	}
1916 
1917 	/* step three, lock the state bits for the whole range */
1918 	lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
1919 
1920 	/* then test to make sure it is all still delalloc */
1921 	ret = test_range_bit(tree, delalloc_start, delalloc_end,
1922 			     EXTENT_DELALLOC, 1, cached_state);
1923 	if (!ret) {
1924 		unlock_extent_cached(tree, delalloc_start, delalloc_end,
1925 				     &cached_state);
1926 		__unlock_for_delalloc(inode, locked_page,
1927 			      delalloc_start, delalloc_end);
1928 		cond_resched();
1929 		goto again;
1930 	}
1931 	free_extent_state(cached_state);
1932 	*start = delalloc_start;
1933 	*end = delalloc_end;
1934 out_failed:
1935 	return found;
1936 }
1937 
1938 static int __process_pages_contig(struct address_space *mapping,
1939 				  struct page *locked_page,
1940 				  pgoff_t start_index, pgoff_t end_index,
1941 				  unsigned long page_ops, pgoff_t *index_ret)
1942 {
1943 	unsigned long nr_pages = end_index - start_index + 1;
1944 	unsigned long pages_processed = 0;
1945 	pgoff_t index = start_index;
1946 	struct page *pages[16];
1947 	unsigned ret;
1948 	int err = 0;
1949 	int i;
1950 
1951 	if (page_ops & PAGE_LOCK) {
1952 		ASSERT(page_ops == PAGE_LOCK);
1953 		ASSERT(index_ret && *index_ret == start_index);
1954 	}
1955 
1956 	if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
1957 		mapping_set_error(mapping, -EIO);
1958 
1959 	while (nr_pages > 0) {
1960 		ret = find_get_pages_contig(mapping, index,
1961 				     min_t(unsigned long,
1962 				     nr_pages, ARRAY_SIZE(pages)), pages);
1963 		if (ret == 0) {
1964 			/*
1965 			 * Only if we're going to lock these pages,
1966 			 * can we find nothing at @index.
1967 			 */
1968 			ASSERT(page_ops & PAGE_LOCK);
1969 			err = -EAGAIN;
1970 			goto out;
1971 		}
1972 
1973 		for (i = 0; i < ret; i++) {
1974 			if (page_ops & PAGE_SET_PRIVATE2)
1975 				SetPagePrivate2(pages[i]);
1976 
1977 			if (locked_page && pages[i] == locked_page) {
1978 				put_page(pages[i]);
1979 				pages_processed++;
1980 				continue;
1981 			}
1982 			if (page_ops & PAGE_START_WRITEBACK) {
1983 				clear_page_dirty_for_io(pages[i]);
1984 				set_page_writeback(pages[i]);
1985 			}
1986 			if (page_ops & PAGE_SET_ERROR)
1987 				SetPageError(pages[i]);
1988 			if (page_ops & PAGE_END_WRITEBACK)
1989 				end_page_writeback(pages[i]);
1990 			if (page_ops & PAGE_UNLOCK)
1991 				unlock_page(pages[i]);
1992 			if (page_ops & PAGE_LOCK) {
1993 				lock_page(pages[i]);
1994 				if (!PageDirty(pages[i]) ||
1995 				    pages[i]->mapping != mapping) {
1996 					unlock_page(pages[i]);
1997 					for (; i < ret; i++)
1998 						put_page(pages[i]);
1999 					err = -EAGAIN;
2000 					goto out;
2001 				}
2002 			}
2003 			put_page(pages[i]);
2004 			pages_processed++;
2005 		}
2006 		nr_pages -= ret;
2007 		index += ret;
2008 		cond_resched();
2009 	}
2010 out:
2011 	if (err && index_ret)
2012 		*index_ret = start_index + pages_processed - 1;
2013 	return err;
2014 }
2015 
2016 void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
2017 				  struct page *locked_page,
2018 				  u32 clear_bits, unsigned long page_ops)
2019 {
2020 	clear_extent_bit(&inode->io_tree, start, end, clear_bits, 1, 0, NULL);
2021 
2022 	__process_pages_contig(inode->vfs_inode.i_mapping, locked_page,
2023 			       start >> PAGE_SHIFT, end >> PAGE_SHIFT,
2024 			       page_ops, NULL);
2025 }
2026 
2027 /*
2028  * count the number of bytes in the tree that have a given bit(s)
2029  * set.  This can be fairly slow, except for EXTENT_DIRTY which is
2030  * cached.  The total number found is returned.
2031  */
2032 u64 count_range_bits(struct extent_io_tree *tree,
2033 		     u64 *start, u64 search_end, u64 max_bytes,
2034 		     u32 bits, int contig)
2035 {
2036 	struct rb_node *node;
2037 	struct extent_state *state;
2038 	u64 cur_start = *start;
2039 	u64 total_bytes = 0;
2040 	u64 last = 0;
2041 	int found = 0;
2042 
2043 	if (WARN_ON(search_end <= cur_start))
2044 		return 0;
2045 
2046 	spin_lock(&tree->lock);
2047 	if (cur_start == 0 && bits == EXTENT_DIRTY) {
2048 		total_bytes = tree->dirty_bytes;
2049 		goto out;
2050 	}
2051 	/*
2052 	 * this search will find all the extents that end after
2053 	 * our range starts.
2054 	 */
2055 	node = tree_search(tree, cur_start);
2056 	if (!node)
2057 		goto out;
2058 
2059 	while (1) {
2060 		state = rb_entry(node, struct extent_state, rb_node);
2061 		if (state->start > search_end)
2062 			break;
2063 		if (contig && found && state->start > last + 1)
2064 			break;
2065 		if (state->end >= cur_start && (state->state & bits) == bits) {
2066 			total_bytes += min(search_end, state->end) + 1 -
2067 				       max(cur_start, state->start);
2068 			if (total_bytes >= max_bytes)
2069 				break;
2070 			if (!found) {
2071 				*start = max(cur_start, state->start);
2072 				found = 1;
2073 			}
2074 			last = state->end;
2075 		} else if (contig && found) {
2076 			break;
2077 		}
2078 		node = rb_next(node);
2079 		if (!node)
2080 			break;
2081 	}
2082 out:
2083 	spin_unlock(&tree->lock);
2084 	return total_bytes;
2085 }
2086 
2087 /*
2088  * set the private field for a given byte offset in the tree.  If there isn't
2089  * an extent_state there already, this does nothing.
2090  */
2091 int set_state_failrec(struct extent_io_tree *tree, u64 start,
2092 		      struct io_failure_record *failrec)
2093 {
2094 	struct rb_node *node;
2095 	struct extent_state *state;
2096 	int ret = 0;
2097 
2098 	spin_lock(&tree->lock);
2099 	/*
2100 	 * this search will find all the extents that end after
2101 	 * our range starts.
2102 	 */
2103 	node = tree_search(tree, start);
2104 	if (!node) {
2105 		ret = -ENOENT;
2106 		goto out;
2107 	}
2108 	state = rb_entry(node, struct extent_state, rb_node);
2109 	if (state->start != start) {
2110 		ret = -ENOENT;
2111 		goto out;
2112 	}
2113 	state->failrec = failrec;
2114 out:
2115 	spin_unlock(&tree->lock);
2116 	return ret;
2117 }
2118 
2119 struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start)
2120 {
2121 	struct rb_node *node;
2122 	struct extent_state *state;
2123 	struct io_failure_record *failrec;
2124 
2125 	spin_lock(&tree->lock);
2126 	/*
2127 	 * this search will find all the extents that end after
2128 	 * our range starts.
2129 	 */
2130 	node = tree_search(tree, start);
2131 	if (!node) {
2132 		failrec = ERR_PTR(-ENOENT);
2133 		goto out;
2134 	}
2135 	state = rb_entry(node, struct extent_state, rb_node);
2136 	if (state->start != start) {
2137 		failrec = ERR_PTR(-ENOENT);
2138 		goto out;
2139 	}
2140 
2141 	failrec = state->failrec;
2142 out:
2143 	spin_unlock(&tree->lock);
2144 	return failrec;
2145 }
2146 
2147 /*
2148  * searches a range in the state tree for a given mask.
2149  * If 'filled' == 1, this returns 1 only if every extent in the tree
2150  * has the bits set.  Otherwise, 1 is returned if any bit in the
2151  * range is found set.
2152  */
2153 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
2154 		   u32 bits, int filled, struct extent_state *cached)
2155 {
2156 	struct extent_state *state = NULL;
2157 	struct rb_node *node;
2158 	int bitset = 0;
2159 
2160 	spin_lock(&tree->lock);
2161 	if (cached && extent_state_in_tree(cached) && cached->start <= start &&
2162 	    cached->end > start)
2163 		node = &cached->rb_node;
2164 	else
2165 		node = tree_search(tree, start);
2166 	while (node && start <= end) {
2167 		state = rb_entry(node, struct extent_state, rb_node);
2168 
2169 		if (filled && state->start > start) {
2170 			bitset = 0;
2171 			break;
2172 		}
2173 
2174 		if (state->start > end)
2175 			break;
2176 
2177 		if (state->state & bits) {
2178 			bitset = 1;
2179 			if (!filled)
2180 				break;
2181 		} else if (filled) {
2182 			bitset = 0;
2183 			break;
2184 		}
2185 
2186 		if (state->end == (u64)-1)
2187 			break;
2188 
2189 		start = state->end + 1;
2190 		if (start > end)
2191 			break;
2192 		node = rb_next(node);
2193 		if (!node) {
2194 			if (filled)
2195 				bitset = 0;
2196 			break;
2197 		}
2198 	}
2199 	spin_unlock(&tree->lock);
2200 	return bitset;
2201 }
2202 
2203 /*
2204  * helper function to set a given page up to date if all the
2205  * extents in the tree for that page are up to date
2206  */
2207 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
2208 {
2209 	u64 start = page_offset(page);
2210 	u64 end = start + PAGE_SIZE - 1;
2211 	if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
2212 		SetPageUptodate(page);
2213 }
2214 
2215 int free_io_failure(struct extent_io_tree *failure_tree,
2216 		    struct extent_io_tree *io_tree,
2217 		    struct io_failure_record *rec)
2218 {
2219 	int ret;
2220 	int err = 0;
2221 
2222 	set_state_failrec(failure_tree, rec->start, NULL);
2223 	ret = clear_extent_bits(failure_tree, rec->start,
2224 				rec->start + rec->len - 1,
2225 				EXTENT_LOCKED | EXTENT_DIRTY);
2226 	if (ret)
2227 		err = ret;
2228 
2229 	ret = clear_extent_bits(io_tree, rec->start,
2230 				rec->start + rec->len - 1,
2231 				EXTENT_DAMAGED);
2232 	if (ret && !err)
2233 		err = ret;
2234 
2235 	kfree(rec);
2236 	return err;
2237 }
2238 
2239 /*
2240  * this bypasses the standard btrfs submit functions deliberately, as
2241  * the standard behavior is to write all copies in a raid setup. here we only
2242  * want to write the one bad copy. so we do the mapping for ourselves and issue
2243  * submit_bio directly.
2244  * to avoid any synchronization issues, wait for the data after writing, which
2245  * actually prevents the read that triggered the error from finishing.
2246  * currently, there can be no more than two copies of every data bit. thus,
2247  * exactly one rewrite is required.
2248  */
2249 int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2250 		      u64 length, u64 logical, struct page *page,
2251 		      unsigned int pg_offset, int mirror_num)
2252 {
2253 	struct bio *bio;
2254 	struct btrfs_device *dev;
2255 	u64 map_length = 0;
2256 	u64 sector;
2257 	struct btrfs_bio *bbio = NULL;
2258 	int ret;
2259 
2260 	ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
2261 	BUG_ON(!mirror_num);
2262 
2263 	if (btrfs_is_zoned(fs_info))
2264 		return btrfs_repair_one_zone(fs_info, logical);
2265 
2266 	bio = btrfs_io_bio_alloc(1);
2267 	bio->bi_iter.bi_size = 0;
2268 	map_length = length;
2269 
2270 	/*
2271 	 * Avoid races with device replace and make sure our bbio has devices
2272 	 * associated to its stripes that don't go away while we are doing the
2273 	 * read repair operation.
2274 	 */
2275 	btrfs_bio_counter_inc_blocked(fs_info);
2276 	if (btrfs_is_parity_mirror(fs_info, logical, length)) {
2277 		/*
2278 		 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2279 		 * to update all raid stripes, but here we just want to correct
2280 		 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2281 		 * stripe's dev and sector.
2282 		 */
2283 		ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2284 				      &map_length, &bbio, 0);
2285 		if (ret) {
2286 			btrfs_bio_counter_dec(fs_info);
2287 			bio_put(bio);
2288 			return -EIO;
2289 		}
2290 		ASSERT(bbio->mirror_num == 1);
2291 	} else {
2292 		ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2293 				      &map_length, &bbio, mirror_num);
2294 		if (ret) {
2295 			btrfs_bio_counter_dec(fs_info);
2296 			bio_put(bio);
2297 			return -EIO;
2298 		}
2299 		BUG_ON(mirror_num != bbio->mirror_num);
2300 	}
2301 
2302 	sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
2303 	bio->bi_iter.bi_sector = sector;
2304 	dev = bbio->stripes[bbio->mirror_num - 1].dev;
2305 	btrfs_put_bbio(bbio);
2306 	if (!dev || !dev->bdev ||
2307 	    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
2308 		btrfs_bio_counter_dec(fs_info);
2309 		bio_put(bio);
2310 		return -EIO;
2311 	}
2312 	bio_set_dev(bio, dev->bdev);
2313 	bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
2314 	bio_add_page(bio, page, length, pg_offset);
2315 
2316 	if (btrfsic_submit_bio_wait(bio)) {
2317 		/* try to remap that extent elsewhere? */
2318 		btrfs_bio_counter_dec(fs_info);
2319 		bio_put(bio);
2320 		btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
2321 		return -EIO;
2322 	}
2323 
2324 	btrfs_info_rl_in_rcu(fs_info,
2325 		"read error corrected: ino %llu off %llu (dev %s sector %llu)",
2326 				  ino, start,
2327 				  rcu_str_deref(dev->name), sector);
2328 	btrfs_bio_counter_dec(fs_info);
2329 	bio_put(bio);
2330 	return 0;
2331 }
2332 
2333 int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num)
2334 {
2335 	struct btrfs_fs_info *fs_info = eb->fs_info;
2336 	u64 start = eb->start;
2337 	int i, num_pages = num_extent_pages(eb);
2338 	int ret = 0;
2339 
2340 	if (sb_rdonly(fs_info->sb))
2341 		return -EROFS;
2342 
2343 	for (i = 0; i < num_pages; i++) {
2344 		struct page *p = eb->pages[i];
2345 
2346 		ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
2347 					start - page_offset(p), mirror_num);
2348 		if (ret)
2349 			break;
2350 		start += PAGE_SIZE;
2351 	}
2352 
2353 	return ret;
2354 }
2355 
2356 /*
2357  * each time an IO finishes, we do a fast check in the IO failure tree
2358  * to see if we need to process or clean up an io_failure_record
2359  */
2360 int clean_io_failure(struct btrfs_fs_info *fs_info,
2361 		     struct extent_io_tree *failure_tree,
2362 		     struct extent_io_tree *io_tree, u64 start,
2363 		     struct page *page, u64 ino, unsigned int pg_offset)
2364 {
2365 	u64 private;
2366 	struct io_failure_record *failrec;
2367 	struct extent_state *state;
2368 	int num_copies;
2369 	int ret;
2370 
2371 	private = 0;
2372 	ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2373 			       EXTENT_DIRTY, 0);
2374 	if (!ret)
2375 		return 0;
2376 
2377 	failrec = get_state_failrec(failure_tree, start);
2378 	if (IS_ERR(failrec))
2379 		return 0;
2380 
2381 	BUG_ON(!failrec->this_mirror);
2382 
2383 	if (failrec->in_validation) {
2384 		/* there was no real error, just free the record */
2385 		btrfs_debug(fs_info,
2386 			"clean_io_failure: freeing dummy error at %llu",
2387 			failrec->start);
2388 		goto out;
2389 	}
2390 	if (sb_rdonly(fs_info->sb))
2391 		goto out;
2392 
2393 	spin_lock(&io_tree->lock);
2394 	state = find_first_extent_bit_state(io_tree,
2395 					    failrec->start,
2396 					    EXTENT_LOCKED);
2397 	spin_unlock(&io_tree->lock);
2398 
2399 	if (state && state->start <= failrec->start &&
2400 	    state->end >= failrec->start + failrec->len - 1) {
2401 		num_copies = btrfs_num_copies(fs_info, failrec->logical,
2402 					      failrec->len);
2403 		if (num_copies > 1)  {
2404 			repair_io_failure(fs_info, ino, start, failrec->len,
2405 					  failrec->logical, page, pg_offset,
2406 					  failrec->failed_mirror);
2407 		}
2408 	}
2409 
2410 out:
2411 	free_io_failure(failure_tree, io_tree, failrec);
2412 
2413 	return 0;
2414 }
2415 
2416 /*
2417  * Can be called when
2418  * - hold extent lock
2419  * - under ordered extent
2420  * - the inode is freeing
2421  */
2422 void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
2423 {
2424 	struct extent_io_tree *failure_tree = &inode->io_failure_tree;
2425 	struct io_failure_record *failrec;
2426 	struct extent_state *state, *next;
2427 
2428 	if (RB_EMPTY_ROOT(&failure_tree->state))
2429 		return;
2430 
2431 	spin_lock(&failure_tree->lock);
2432 	state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2433 	while (state) {
2434 		if (state->start > end)
2435 			break;
2436 
2437 		ASSERT(state->end <= end);
2438 
2439 		next = next_state(state);
2440 
2441 		failrec = state->failrec;
2442 		free_extent_state(state);
2443 		kfree(failrec);
2444 
2445 		state = next;
2446 	}
2447 	spin_unlock(&failure_tree->lock);
2448 }
2449 
2450 static struct io_failure_record *btrfs_get_io_failure_record(struct inode *inode,
2451 							     u64 start, u64 end)
2452 {
2453 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2454 	struct io_failure_record *failrec;
2455 	struct extent_map *em;
2456 	struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2457 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2458 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2459 	int ret;
2460 	u64 logical;
2461 
2462 	failrec = get_state_failrec(failure_tree, start);
2463 	if (!IS_ERR(failrec)) {
2464 		btrfs_debug(fs_info,
2465 			"Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2466 			failrec->logical, failrec->start, failrec->len,
2467 			failrec->in_validation);
2468 		/*
2469 		 * when data can be on disk more than twice, add to failrec here
2470 		 * (e.g. with a list for failed_mirror) to make
2471 		 * clean_io_failure() clean all those errors at once.
2472 		 */
2473 
2474 		return failrec;
2475 	}
2476 
2477 	failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2478 	if (!failrec)
2479 		return ERR_PTR(-ENOMEM);
2480 
2481 	failrec->start = start;
2482 	failrec->len = end - start + 1;
2483 	failrec->this_mirror = 0;
2484 	failrec->bio_flags = 0;
2485 	failrec->in_validation = 0;
2486 
2487 	read_lock(&em_tree->lock);
2488 	em = lookup_extent_mapping(em_tree, start, failrec->len);
2489 	if (!em) {
2490 		read_unlock(&em_tree->lock);
2491 		kfree(failrec);
2492 		return ERR_PTR(-EIO);
2493 	}
2494 
2495 	if (em->start > start || em->start + em->len <= start) {
2496 		free_extent_map(em);
2497 		em = NULL;
2498 	}
2499 	read_unlock(&em_tree->lock);
2500 	if (!em) {
2501 		kfree(failrec);
2502 		return ERR_PTR(-EIO);
2503 	}
2504 
2505 	logical = start - em->start;
2506 	logical = em->block_start + logical;
2507 	if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2508 		logical = em->block_start;
2509 		failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2510 		extent_set_compress_type(&failrec->bio_flags, em->compress_type);
2511 	}
2512 
2513 	btrfs_debug(fs_info,
2514 		    "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2515 		    logical, start, failrec->len);
2516 
2517 	failrec->logical = logical;
2518 	free_extent_map(em);
2519 
2520 	/* Set the bits in the private failure tree */
2521 	ret = set_extent_bits(failure_tree, start, end,
2522 			      EXTENT_LOCKED | EXTENT_DIRTY);
2523 	if (ret >= 0) {
2524 		ret = set_state_failrec(failure_tree, start, failrec);
2525 		/* Set the bits in the inode's tree */
2526 		ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
2527 	} else if (ret < 0) {
2528 		kfree(failrec);
2529 		return ERR_PTR(ret);
2530 	}
2531 
2532 	return failrec;
2533 }
2534 
2535 static bool btrfs_check_repairable(struct inode *inode, bool needs_validation,
2536 				   struct io_failure_record *failrec,
2537 				   int failed_mirror)
2538 {
2539 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2540 	int num_copies;
2541 
2542 	num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
2543 	if (num_copies == 1) {
2544 		/*
2545 		 * we only have a single copy of the data, so don't bother with
2546 		 * all the retry and error correction code that follows. no
2547 		 * matter what the error is, it is very likely to persist.
2548 		 */
2549 		btrfs_debug(fs_info,
2550 			"Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2551 			num_copies, failrec->this_mirror, failed_mirror);
2552 		return false;
2553 	}
2554 
2555 	/*
2556 	 * there are two premises:
2557 	 *	a) deliver good data to the caller
2558 	 *	b) correct the bad sectors on disk
2559 	 */
2560 	if (needs_validation) {
2561 		/*
2562 		 * to fulfill b), we need to know the exact failing sectors, as
2563 		 * we don't want to rewrite any more than the failed ones. thus,
2564 		 * we need separate read requests for the failed bio
2565 		 *
2566 		 * if the following BUG_ON triggers, our validation request got
2567 		 * merged. we need separate requests for our algorithm to work.
2568 		 */
2569 		BUG_ON(failrec->in_validation);
2570 		failrec->in_validation = 1;
2571 		failrec->this_mirror = failed_mirror;
2572 	} else {
2573 		/*
2574 		 * we're ready to fulfill a) and b) alongside. get a good copy
2575 		 * of the failed sector and if we succeed, we have setup
2576 		 * everything for repair_io_failure to do the rest for us.
2577 		 */
2578 		if (failrec->in_validation) {
2579 			BUG_ON(failrec->this_mirror != failed_mirror);
2580 			failrec->in_validation = 0;
2581 			failrec->this_mirror = 0;
2582 		}
2583 		failrec->failed_mirror = failed_mirror;
2584 		failrec->this_mirror++;
2585 		if (failrec->this_mirror == failed_mirror)
2586 			failrec->this_mirror++;
2587 	}
2588 
2589 	if (failrec->this_mirror > num_copies) {
2590 		btrfs_debug(fs_info,
2591 			"Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2592 			num_copies, failrec->this_mirror, failed_mirror);
2593 		return false;
2594 	}
2595 
2596 	return true;
2597 }
2598 
2599 static bool btrfs_io_needs_validation(struct inode *inode, struct bio *bio)
2600 {
2601 	u64 len = 0;
2602 	const u32 blocksize = inode->i_sb->s_blocksize;
2603 
2604 	/*
2605 	 * If bi_status is BLK_STS_OK, then this was a checksum error, not an
2606 	 * I/O error. In this case, we already know exactly which sector was
2607 	 * bad, so we don't need to validate.
2608 	 */
2609 	if (bio->bi_status == BLK_STS_OK)
2610 		return false;
2611 
2612 	/*
2613 	 * We need to validate each sector individually if the failed I/O was
2614 	 * for multiple sectors.
2615 	 *
2616 	 * There are a few possible bios that can end up here:
2617 	 * 1. A buffered read bio, which is not cloned.
2618 	 * 2. A direct I/O read bio, which is cloned.
2619 	 * 3. A (buffered or direct) repair bio, which is not cloned.
2620 	 *
2621 	 * For cloned bios (case 2), we can get the size from
2622 	 * btrfs_io_bio->iter; for non-cloned bios (cases 1 and 3), we can get
2623 	 * it from the bvecs.
2624 	 */
2625 	if (bio_flagged(bio, BIO_CLONED)) {
2626 		if (btrfs_io_bio(bio)->iter.bi_size > blocksize)
2627 			return true;
2628 	} else {
2629 		struct bio_vec *bvec;
2630 		int i;
2631 
2632 		bio_for_each_bvec_all(bvec, bio, i) {
2633 			len += bvec->bv_len;
2634 			if (len > blocksize)
2635 				return true;
2636 		}
2637 	}
2638 	return false;
2639 }
2640 
2641 blk_status_t btrfs_submit_read_repair(struct inode *inode,
2642 				      struct bio *failed_bio, u32 bio_offset,
2643 				      struct page *page, unsigned int pgoff,
2644 				      u64 start, u64 end, int failed_mirror,
2645 				      submit_bio_hook_t *submit_bio_hook)
2646 {
2647 	struct io_failure_record *failrec;
2648 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2649 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2650 	struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2651 	struct btrfs_io_bio *failed_io_bio = btrfs_io_bio(failed_bio);
2652 	const int icsum = bio_offset >> fs_info->sectorsize_bits;
2653 	bool need_validation;
2654 	struct bio *repair_bio;
2655 	struct btrfs_io_bio *repair_io_bio;
2656 	blk_status_t status;
2657 
2658 	btrfs_debug(fs_info,
2659 		   "repair read error: read error at %llu", start);
2660 
2661 	BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
2662 
2663 	failrec = btrfs_get_io_failure_record(inode, start, end);
2664 	if (IS_ERR(failrec))
2665 		return errno_to_blk_status(PTR_ERR(failrec));
2666 
2667 	need_validation = btrfs_io_needs_validation(inode, failed_bio);
2668 
2669 	if (!btrfs_check_repairable(inode, need_validation, failrec,
2670 				    failed_mirror)) {
2671 		free_io_failure(failure_tree, tree, failrec);
2672 		return BLK_STS_IOERR;
2673 	}
2674 
2675 	repair_bio = btrfs_io_bio_alloc(1);
2676 	repair_io_bio = btrfs_io_bio(repair_bio);
2677 	repair_bio->bi_opf = REQ_OP_READ;
2678 	if (need_validation)
2679 		repair_bio->bi_opf |= REQ_FAILFAST_DEV;
2680 	repair_bio->bi_end_io = failed_bio->bi_end_io;
2681 	repair_bio->bi_iter.bi_sector = failrec->logical >> 9;
2682 	repair_bio->bi_private = failed_bio->bi_private;
2683 
2684 	if (failed_io_bio->csum) {
2685 		const u32 csum_size = fs_info->csum_size;
2686 
2687 		repair_io_bio->csum = repair_io_bio->csum_inline;
2688 		memcpy(repair_io_bio->csum,
2689 		       failed_io_bio->csum + csum_size * icsum, csum_size);
2690 	}
2691 
2692 	bio_add_page(repair_bio, page, failrec->len, pgoff);
2693 	repair_io_bio->logical = failrec->start;
2694 	repair_io_bio->iter = repair_bio->bi_iter;
2695 
2696 	btrfs_debug(btrfs_sb(inode->i_sb),
2697 "repair read error: submitting new read to mirror %d, in_validation=%d",
2698 		    failrec->this_mirror, failrec->in_validation);
2699 
2700 	status = submit_bio_hook(inode, repair_bio, failrec->this_mirror,
2701 				 failrec->bio_flags);
2702 	if (status) {
2703 		free_io_failure(failure_tree, tree, failrec);
2704 		bio_put(repair_bio);
2705 	}
2706 	return status;
2707 }
2708 
2709 /* lots and lots of room for performance fixes in the end_bio funcs */
2710 
2711 void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2712 {
2713 	int uptodate = (err == 0);
2714 	int ret = 0;
2715 
2716 	btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
2717 
2718 	if (!uptodate) {
2719 		ClearPageUptodate(page);
2720 		SetPageError(page);
2721 		ret = err < 0 ? err : -EIO;
2722 		mapping_set_error(page->mapping, ret);
2723 	}
2724 }
2725 
2726 /*
2727  * after a writepage IO is done, we need to:
2728  * clear the uptodate bits on error
2729  * clear the writeback bits in the extent tree for this IO
2730  * end_page_writeback if the page has no more pending IO
2731  *
2732  * Scheduling is not allowed, so the extent state tree is expected
2733  * to have one and only one object corresponding to this IO.
2734  */
2735 static void end_bio_extent_writepage(struct bio *bio)
2736 {
2737 	int error = blk_status_to_errno(bio->bi_status);
2738 	struct bio_vec *bvec;
2739 	u64 start;
2740 	u64 end;
2741 	struct bvec_iter_all iter_all;
2742 	bool first_bvec = true;
2743 
2744 	ASSERT(!bio_flagged(bio, BIO_CLONED));
2745 	bio_for_each_segment_all(bvec, bio, iter_all) {
2746 		struct page *page = bvec->bv_page;
2747 		struct inode *inode = page->mapping->host;
2748 		struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2749 
2750 		/* We always issue full-page reads, but if some block
2751 		 * in a page fails to read, blk_update_request() will
2752 		 * advance bv_offset and adjust bv_len to compensate.
2753 		 * Print a warning for nonzero offsets, and an error
2754 		 * if they don't add up to a full page.  */
2755 		if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2756 			if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
2757 				btrfs_err(fs_info,
2758 				   "partial page write in btrfs with offset %u and length %u",
2759 					bvec->bv_offset, bvec->bv_len);
2760 			else
2761 				btrfs_info(fs_info,
2762 				   "incomplete page write in btrfs with offset %u and length %u",
2763 					bvec->bv_offset, bvec->bv_len);
2764 		}
2765 
2766 		start = page_offset(page);
2767 		end = start + bvec->bv_offset + bvec->bv_len - 1;
2768 
2769 		if (first_bvec) {
2770 			btrfs_record_physical_zoned(inode, start, bio);
2771 			first_bvec = false;
2772 		}
2773 
2774 		end_extent_writepage(page, error, start, end);
2775 		end_page_writeback(page);
2776 	}
2777 
2778 	bio_put(bio);
2779 }
2780 
2781 /*
2782  * Record previously processed extent range
2783  *
2784  * For endio_readpage_release_extent() to handle a full extent range, reducing
2785  * the extent io operations.
2786  */
2787 struct processed_extent {
2788 	struct btrfs_inode *inode;
2789 	/* Start of the range in @inode */
2790 	u64 start;
2791 	/* End of the range in @inode */
2792 	u64 end;
2793 	bool uptodate;
2794 };
2795 
2796 /*
2797  * Try to release processed extent range
2798  *
2799  * May not release the extent range right now if the current range is
2800  * contiguous to processed extent.
2801  *
2802  * Will release processed extent when any of @inode, @uptodate, the range is
2803  * no longer contiguous to the processed range.
2804  *
2805  * Passing @inode == NULL will force processed extent to be released.
2806  */
2807 static void endio_readpage_release_extent(struct processed_extent *processed,
2808 			      struct btrfs_inode *inode, u64 start, u64 end,
2809 			      bool uptodate)
2810 {
2811 	struct extent_state *cached = NULL;
2812 	struct extent_io_tree *tree;
2813 
2814 	/* The first extent, initialize @processed */
2815 	if (!processed->inode)
2816 		goto update;
2817 
2818 	/*
2819 	 * Contiguous to processed extent, just uptodate the end.
2820 	 *
2821 	 * Several things to notice:
2822 	 *
2823 	 * - bio can be merged as long as on-disk bytenr is contiguous
2824 	 *   This means we can have page belonging to other inodes, thus need to
2825 	 *   check if the inode still matches.
2826 	 * - bvec can contain range beyond current page for multi-page bvec
2827 	 *   Thus we need to do processed->end + 1 >= start check
2828 	 */
2829 	if (processed->inode == inode && processed->uptodate == uptodate &&
2830 	    processed->end + 1 >= start && end >= processed->end) {
2831 		processed->end = end;
2832 		return;
2833 	}
2834 
2835 	tree = &processed->inode->io_tree;
2836 	/*
2837 	 * Now we don't have range contiguous to the processed range, release
2838 	 * the processed range now.
2839 	 */
2840 	if (processed->uptodate && tree->track_uptodate)
2841 		set_extent_uptodate(tree, processed->start, processed->end,
2842 				    &cached, GFP_ATOMIC);
2843 	unlock_extent_cached_atomic(tree, processed->start, processed->end,
2844 				    &cached);
2845 
2846 update:
2847 	/* Update processed to current range */
2848 	processed->inode = inode;
2849 	processed->start = start;
2850 	processed->end = end;
2851 	processed->uptodate = uptodate;
2852 }
2853 
2854 static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page)
2855 {
2856 	ASSERT(PageLocked(page));
2857 	if (fs_info->sectorsize == PAGE_SIZE)
2858 		return;
2859 
2860 	ASSERT(PagePrivate(page));
2861 	btrfs_subpage_start_reader(fs_info, page, page_offset(page), PAGE_SIZE);
2862 }
2863 
2864 static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len)
2865 {
2866 	struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
2867 
2868 	ASSERT(page_offset(page) <= start &&
2869 		start + len <= page_offset(page) + PAGE_SIZE);
2870 
2871 	if (uptodate) {
2872 		btrfs_page_set_uptodate(fs_info, page, start, len);
2873 	} else {
2874 		btrfs_page_clear_uptodate(fs_info, page, start, len);
2875 		btrfs_page_set_error(fs_info, page, start, len);
2876 	}
2877 
2878 	if (fs_info->sectorsize == PAGE_SIZE)
2879 		unlock_page(page);
2880 	else if (is_data_inode(page->mapping->host))
2881 		/*
2882 		 * For subpage data, unlock the page if we're the last reader.
2883 		 * For subpage metadata, page lock is not utilized for read.
2884 		 */
2885 		btrfs_subpage_end_reader(fs_info, page, start, len);
2886 }
2887 
2888 /*
2889  * after a readpage IO is done, we need to:
2890  * clear the uptodate bits on error
2891  * set the uptodate bits if things worked
2892  * set the page up to date if all extents in the tree are uptodate
2893  * clear the lock bit in the extent tree
2894  * unlock the page if there are no other extents locked for it
2895  *
2896  * Scheduling is not allowed, so the extent state tree is expected
2897  * to have one and only one object corresponding to this IO.
2898  */
2899 static void end_bio_extent_readpage(struct bio *bio)
2900 {
2901 	struct bio_vec *bvec;
2902 	int uptodate = !bio->bi_status;
2903 	struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
2904 	struct extent_io_tree *tree, *failure_tree;
2905 	struct processed_extent processed = { 0 };
2906 	/*
2907 	 * The offset to the beginning of a bio, since one bio can never be
2908 	 * larger than UINT_MAX, u32 here is enough.
2909 	 */
2910 	u32 bio_offset = 0;
2911 	int mirror;
2912 	int ret;
2913 	struct bvec_iter_all iter_all;
2914 
2915 	ASSERT(!bio_flagged(bio, BIO_CLONED));
2916 	bio_for_each_segment_all(bvec, bio, iter_all) {
2917 		struct page *page = bvec->bv_page;
2918 		struct inode *inode = page->mapping->host;
2919 		struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2920 		const u32 sectorsize = fs_info->sectorsize;
2921 		u64 start;
2922 		u64 end;
2923 		u32 len;
2924 
2925 		btrfs_debug(fs_info,
2926 			"end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
2927 			bio->bi_iter.bi_sector, bio->bi_status,
2928 			io_bio->mirror_num);
2929 		tree = &BTRFS_I(inode)->io_tree;
2930 		failure_tree = &BTRFS_I(inode)->io_failure_tree;
2931 
2932 		/*
2933 		 * We always issue full-sector reads, but if some block in a
2934 		 * page fails to read, blk_update_request() will advance
2935 		 * bv_offset and adjust bv_len to compensate.  Print a warning
2936 		 * for unaligned offsets, and an error if they don't add up to
2937 		 * a full sector.
2938 		 */
2939 		if (!IS_ALIGNED(bvec->bv_offset, sectorsize))
2940 			btrfs_err(fs_info,
2941 		"partial page read in btrfs with offset %u and length %u",
2942 				  bvec->bv_offset, bvec->bv_len);
2943 		else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len,
2944 				     sectorsize))
2945 			btrfs_info(fs_info,
2946 		"incomplete page read with offset %u and length %u",
2947 				   bvec->bv_offset, bvec->bv_len);
2948 
2949 		start = page_offset(page) + bvec->bv_offset;
2950 		end = start + bvec->bv_len - 1;
2951 		len = bvec->bv_len;
2952 
2953 		mirror = io_bio->mirror_num;
2954 		if (likely(uptodate)) {
2955 			if (is_data_inode(inode))
2956 				ret = btrfs_verify_data_csum(io_bio,
2957 						bio_offset, page, start, end,
2958 						mirror);
2959 			else
2960 				ret = btrfs_validate_metadata_buffer(io_bio,
2961 					page, start, end, mirror);
2962 			if (ret)
2963 				uptodate = 0;
2964 			else
2965 				clean_io_failure(BTRFS_I(inode)->root->fs_info,
2966 						 failure_tree, tree, start,
2967 						 page,
2968 						 btrfs_ino(BTRFS_I(inode)), 0);
2969 		}
2970 
2971 		if (likely(uptodate))
2972 			goto readpage_ok;
2973 
2974 		if (is_data_inode(inode)) {
2975 
2976 			/*
2977 			 * The generic bio_readpage_error handles errors the
2978 			 * following way: If possible, new read requests are
2979 			 * created and submitted and will end up in
2980 			 * end_bio_extent_readpage as well (if we're lucky,
2981 			 * not in the !uptodate case). In that case it returns
2982 			 * 0 and we just go on with the next page in our bio.
2983 			 * If it can't handle the error it will return -EIO and
2984 			 * we remain responsible for that page.
2985 			 */
2986 			if (!btrfs_submit_read_repair(inode, bio, bio_offset,
2987 						page,
2988 						start - page_offset(page),
2989 						start, end, mirror,
2990 						btrfs_submit_data_bio)) {
2991 				uptodate = !bio->bi_status;
2992 				ASSERT(bio_offset + len > bio_offset);
2993 				bio_offset += len;
2994 				continue;
2995 			}
2996 		} else {
2997 			struct extent_buffer *eb;
2998 
2999 			eb = (struct extent_buffer *)page->private;
3000 			set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
3001 			eb->read_mirror = mirror;
3002 			atomic_dec(&eb->io_pages);
3003 			if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
3004 					       &eb->bflags))
3005 				btree_readahead_hook(eb, -EIO);
3006 		}
3007 readpage_ok:
3008 		if (likely(uptodate)) {
3009 			loff_t i_size = i_size_read(inode);
3010 			pgoff_t end_index = i_size >> PAGE_SHIFT;
3011 			unsigned off;
3012 
3013 			/* Zero out the end if this page straddles i_size */
3014 			off = offset_in_page(i_size);
3015 			if (page->index == end_index && off)
3016 				zero_user_segment(page, off, PAGE_SIZE);
3017 		}
3018 		ASSERT(bio_offset + len > bio_offset);
3019 		bio_offset += len;
3020 
3021 		/* Update page status and unlock */
3022 		end_page_read(page, uptodate, start, len);
3023 		endio_readpage_release_extent(&processed, BTRFS_I(inode),
3024 					      start, end, uptodate);
3025 	}
3026 	/* Release the last extent */
3027 	endio_readpage_release_extent(&processed, NULL, 0, 0, false);
3028 	btrfs_io_bio_free_csum(io_bio);
3029 	bio_put(bio);
3030 }
3031 
3032 /*
3033  * Initialize the members up to but not including 'bio'. Use after allocating a
3034  * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
3035  * 'bio' because use of __GFP_ZERO is not supported.
3036  */
3037 static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
3038 {
3039 	memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
3040 }
3041 
3042 /*
3043  * The following helpers allocate a bio. As it's backed by a bioset, it'll
3044  * never fail.  We're returning a bio right now but you can call btrfs_io_bio
3045  * for the appropriate container_of magic
3046  */
3047 struct bio *btrfs_bio_alloc(u64 first_byte)
3048 {
3049 	struct bio *bio;
3050 
3051 	bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
3052 	bio->bi_iter.bi_sector = first_byte >> 9;
3053 	btrfs_io_bio_init(btrfs_io_bio(bio));
3054 	return bio;
3055 }
3056 
3057 struct bio *btrfs_bio_clone(struct bio *bio)
3058 {
3059 	struct btrfs_io_bio *btrfs_bio;
3060 	struct bio *new;
3061 
3062 	/* Bio allocation backed by a bioset does not fail */
3063 	new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
3064 	btrfs_bio = btrfs_io_bio(new);
3065 	btrfs_io_bio_init(btrfs_bio);
3066 	btrfs_bio->iter = bio->bi_iter;
3067 	return new;
3068 }
3069 
3070 struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
3071 {
3072 	struct bio *bio;
3073 
3074 	/* Bio allocation backed by a bioset does not fail */
3075 	bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
3076 	btrfs_io_bio_init(btrfs_io_bio(bio));
3077 	return bio;
3078 }
3079 
3080 struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
3081 {
3082 	struct bio *bio;
3083 	struct btrfs_io_bio *btrfs_bio;
3084 
3085 	/* this will never fail when it's backed by a bioset */
3086 	bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
3087 	ASSERT(bio);
3088 
3089 	btrfs_bio = btrfs_io_bio(bio);
3090 	btrfs_io_bio_init(btrfs_bio);
3091 
3092 	bio_trim(bio, offset >> 9, size >> 9);
3093 	btrfs_bio->iter = bio->bi_iter;
3094 	return bio;
3095 }
3096 
3097 /**
3098  * Attempt to add a page to bio
3099  *
3100  * @bio:	destination bio
3101  * @page:	page to add to the bio
3102  * @disk_bytenr:  offset of the new bio or to check whether we are adding
3103  *                a contiguous page to the previous one
3104  * @pg_offset:	starting offset in the page
3105  * @size:	portion of page that we want to write
3106  * @prev_bio_flags:  flags of previous bio to see if we can merge the current one
3107  * @bio_flags:	flags of the current bio to see if we can merge them
3108  * @return:	true if page was added, false otherwise
3109  *
3110  * Attempt to add a page to bio considering stripe alignment etc.
3111  *
3112  * Return true if successfully page added. Otherwise, return false.
3113  */
3114 static bool btrfs_bio_add_page(struct bio *bio, struct page *page,
3115 			       u64 disk_bytenr, unsigned int size,
3116 			       unsigned int pg_offset,
3117 			       unsigned long prev_bio_flags,
3118 			       unsigned long bio_flags)
3119 {
3120 	const sector_t sector = disk_bytenr >> SECTOR_SHIFT;
3121 	bool contig;
3122 	int ret;
3123 
3124 	if (prev_bio_flags != bio_flags)
3125 		return false;
3126 
3127 	if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
3128 		contig = bio->bi_iter.bi_sector == sector;
3129 	else
3130 		contig = bio_end_sector(bio) == sector;
3131 	if (!contig)
3132 		return false;
3133 
3134 	if (btrfs_bio_fits_in_stripe(page, size, bio, bio_flags))
3135 		return false;
3136 
3137 	if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
3138 		struct page *first_page = bio_first_bvec_all(bio)->bv_page;
3139 
3140 		if (!btrfs_bio_fits_in_ordered_extent(first_page, bio, size))
3141 			return false;
3142 		ret = bio_add_zone_append_page(bio, page, size, pg_offset);
3143 	} else {
3144 		ret = bio_add_page(bio, page, size, pg_offset);
3145 	}
3146 
3147 	return ret == size;
3148 }
3149 
3150 /*
3151  * @opf:	bio REQ_OP_* and REQ_* flags as one value
3152  * @wbc:	optional writeback control for io accounting
3153  * @page:	page to add to the bio
3154  * @disk_bytenr: logical bytenr where the write will be
3155  * @size:	portion of page that we want to write to
3156  * @pg_offset:	offset of the new bio or to check whether we are adding
3157  *              a contiguous page to the previous one
3158  * @bio_ret:	must be valid pointer, newly allocated bio will be stored there
3159  * @end_io_func:     end_io callback for new bio
3160  * @mirror_num:	     desired mirror to read/write
3161  * @prev_bio_flags:  flags of previous bio to see if we can merge the current one
3162  * @bio_flags:	flags of the current bio to see if we can merge them
3163  */
3164 static int submit_extent_page(unsigned int opf,
3165 			      struct writeback_control *wbc,
3166 			      struct page *page, u64 disk_bytenr,
3167 			      size_t size, unsigned long pg_offset,
3168 			      struct bio **bio_ret,
3169 			      bio_end_io_t end_io_func,
3170 			      int mirror_num,
3171 			      unsigned long prev_bio_flags,
3172 			      unsigned long bio_flags,
3173 			      bool force_bio_submit)
3174 {
3175 	int ret = 0;
3176 	struct bio *bio;
3177 	size_t io_size = min_t(size_t, size, PAGE_SIZE);
3178 	struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
3179 	struct extent_io_tree *tree = &inode->io_tree;
3180 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
3181 
3182 	ASSERT(bio_ret);
3183 
3184 	if (*bio_ret) {
3185 		bio = *bio_ret;
3186 		if (force_bio_submit ||
3187 		    !btrfs_bio_add_page(bio, page, disk_bytenr, io_size,
3188 					pg_offset, prev_bio_flags, bio_flags)) {
3189 			ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
3190 			if (ret < 0) {
3191 				*bio_ret = NULL;
3192 				return ret;
3193 			}
3194 			bio = NULL;
3195 		} else {
3196 			if (wbc)
3197 				wbc_account_cgroup_owner(wbc, page, io_size);
3198 			return 0;
3199 		}
3200 	}
3201 
3202 	bio = btrfs_bio_alloc(disk_bytenr);
3203 	bio_add_page(bio, page, io_size, pg_offset);
3204 	bio->bi_end_io = end_io_func;
3205 	bio->bi_private = tree;
3206 	bio->bi_write_hint = page->mapping->host->i_write_hint;
3207 	bio->bi_opf = opf;
3208 	if (wbc) {
3209 		struct block_device *bdev;
3210 
3211 		bdev = fs_info->fs_devices->latest_bdev;
3212 		bio_set_dev(bio, bdev);
3213 		wbc_init_bio(wbc, bio);
3214 		wbc_account_cgroup_owner(wbc, page, io_size);
3215 	}
3216 	if (btrfs_is_zoned(fs_info) && bio_op(bio) == REQ_OP_ZONE_APPEND) {
3217 		struct extent_map *em;
3218 		struct map_lookup *map;
3219 
3220 		em = btrfs_get_chunk_map(fs_info, disk_bytenr, io_size);
3221 		if (IS_ERR(em))
3222 			return PTR_ERR(em);
3223 
3224 		map = em->map_lookup;
3225 		/* We only support single profile for now */
3226 		ASSERT(map->num_stripes == 1);
3227 		btrfs_io_bio(bio)->device = map->stripes[0].dev;
3228 
3229 		free_extent_map(em);
3230 	}
3231 
3232 	*bio_ret = bio;
3233 
3234 	return ret;
3235 }
3236 
3237 static int attach_extent_buffer_page(struct extent_buffer *eb,
3238 				     struct page *page,
3239 				     struct btrfs_subpage *prealloc)
3240 {
3241 	struct btrfs_fs_info *fs_info = eb->fs_info;
3242 	int ret = 0;
3243 
3244 	/*
3245 	 * If the page is mapped to btree inode, we should hold the private
3246 	 * lock to prevent race.
3247 	 * For cloned or dummy extent buffers, their pages are not mapped and
3248 	 * will not race with any other ebs.
3249 	 */
3250 	if (page->mapping)
3251 		lockdep_assert_held(&page->mapping->private_lock);
3252 
3253 	if (fs_info->sectorsize == PAGE_SIZE) {
3254 		if (!PagePrivate(page))
3255 			attach_page_private(page, eb);
3256 		else
3257 			WARN_ON(page->private != (unsigned long)eb);
3258 		return 0;
3259 	}
3260 
3261 	/* Already mapped, just free prealloc */
3262 	if (PagePrivate(page)) {
3263 		btrfs_free_subpage(prealloc);
3264 		return 0;
3265 	}
3266 
3267 	if (prealloc)
3268 		/* Has preallocated memory for subpage */
3269 		attach_page_private(page, prealloc);
3270 	else
3271 		/* Do new allocation to attach subpage */
3272 		ret = btrfs_attach_subpage(fs_info, page,
3273 					   BTRFS_SUBPAGE_METADATA);
3274 	return ret;
3275 }
3276 
3277 int set_page_extent_mapped(struct page *page)
3278 {
3279 	struct btrfs_fs_info *fs_info;
3280 
3281 	ASSERT(page->mapping);
3282 
3283 	if (PagePrivate(page))
3284 		return 0;
3285 
3286 	fs_info = btrfs_sb(page->mapping->host->i_sb);
3287 
3288 	if (fs_info->sectorsize < PAGE_SIZE)
3289 		return btrfs_attach_subpage(fs_info, page, BTRFS_SUBPAGE_DATA);
3290 
3291 	attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE);
3292 	return 0;
3293 }
3294 
3295 void clear_page_extent_mapped(struct page *page)
3296 {
3297 	struct btrfs_fs_info *fs_info;
3298 
3299 	ASSERT(page->mapping);
3300 
3301 	if (!PagePrivate(page))
3302 		return;
3303 
3304 	fs_info = btrfs_sb(page->mapping->host->i_sb);
3305 	if (fs_info->sectorsize < PAGE_SIZE)
3306 		return btrfs_detach_subpage(fs_info, page);
3307 
3308 	detach_page_private(page);
3309 }
3310 
3311 static struct extent_map *
3312 __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
3313 		 u64 start, u64 len, struct extent_map **em_cached)
3314 {
3315 	struct extent_map *em;
3316 
3317 	if (em_cached && *em_cached) {
3318 		em = *em_cached;
3319 		if (extent_map_in_tree(em) && start >= em->start &&
3320 		    start < extent_map_end(em)) {
3321 			refcount_inc(&em->refs);
3322 			return em;
3323 		}
3324 
3325 		free_extent_map(em);
3326 		*em_cached = NULL;
3327 	}
3328 
3329 	em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, start, len);
3330 	if (em_cached && !IS_ERR_OR_NULL(em)) {
3331 		BUG_ON(*em_cached);
3332 		refcount_inc(&em->refs);
3333 		*em_cached = em;
3334 	}
3335 	return em;
3336 }
3337 /*
3338  * basic readpage implementation.  Locked extent state structs are inserted
3339  * into the tree that are removed when the IO is done (by the end_io
3340  * handlers)
3341  * XXX JDM: This needs looking at to ensure proper page locking
3342  * return 0 on success, otherwise return error
3343  */
3344 int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
3345 		      struct bio **bio, unsigned long *bio_flags,
3346 		      unsigned int read_flags, u64 *prev_em_start)
3347 {
3348 	struct inode *inode = page->mapping->host;
3349 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3350 	u64 start = page_offset(page);
3351 	const u64 end = start + PAGE_SIZE - 1;
3352 	u64 cur = start;
3353 	u64 extent_offset;
3354 	u64 last_byte = i_size_read(inode);
3355 	u64 block_start;
3356 	u64 cur_end;
3357 	struct extent_map *em;
3358 	int ret = 0;
3359 	int nr = 0;
3360 	size_t pg_offset = 0;
3361 	size_t iosize;
3362 	size_t blocksize = inode->i_sb->s_blocksize;
3363 	unsigned long this_bio_flag = 0;
3364 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
3365 
3366 	ret = set_page_extent_mapped(page);
3367 	if (ret < 0) {
3368 		unlock_extent(tree, start, end);
3369 		btrfs_page_set_error(fs_info, page, start, PAGE_SIZE);
3370 		unlock_page(page);
3371 		goto out;
3372 	}
3373 
3374 	if (!PageUptodate(page)) {
3375 		if (cleancache_get_page(page) == 0) {
3376 			BUG_ON(blocksize != PAGE_SIZE);
3377 			unlock_extent(tree, start, end);
3378 			unlock_page(page);
3379 			goto out;
3380 		}
3381 	}
3382 
3383 	if (page->index == last_byte >> PAGE_SHIFT) {
3384 		char *userpage;
3385 		size_t zero_offset = offset_in_page(last_byte);
3386 
3387 		if (zero_offset) {
3388 			iosize = PAGE_SIZE - zero_offset;
3389 			userpage = kmap_atomic(page);
3390 			memset(userpage + zero_offset, 0, iosize);
3391 			flush_dcache_page(page);
3392 			kunmap_atomic(userpage);
3393 		}
3394 	}
3395 	begin_page_read(fs_info, page);
3396 	while (cur <= end) {
3397 		bool force_bio_submit = false;
3398 		u64 disk_bytenr;
3399 
3400 		if (cur >= last_byte) {
3401 			char *userpage;
3402 			struct extent_state *cached = NULL;
3403 
3404 			iosize = PAGE_SIZE - pg_offset;
3405 			userpage = kmap_atomic(page);
3406 			memset(userpage + pg_offset, 0, iosize);
3407 			flush_dcache_page(page);
3408 			kunmap_atomic(userpage);
3409 			set_extent_uptodate(tree, cur, cur + iosize - 1,
3410 					    &cached, GFP_NOFS);
3411 			unlock_extent_cached(tree, cur,
3412 					     cur + iosize - 1, &cached);
3413 			end_page_read(page, true, cur, iosize);
3414 			break;
3415 		}
3416 		em = __get_extent_map(inode, page, pg_offset, cur,
3417 				      end - cur + 1, em_cached);
3418 		if (IS_ERR_OR_NULL(em)) {
3419 			unlock_extent(tree, cur, end);
3420 			end_page_read(page, false, cur, end + 1 - cur);
3421 			break;
3422 		}
3423 		extent_offset = cur - em->start;
3424 		BUG_ON(extent_map_end(em) <= cur);
3425 		BUG_ON(end < cur);
3426 
3427 		if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
3428 			this_bio_flag |= EXTENT_BIO_COMPRESSED;
3429 			extent_set_compress_type(&this_bio_flag,
3430 						 em->compress_type);
3431 		}
3432 
3433 		iosize = min(extent_map_end(em) - cur, end - cur + 1);
3434 		cur_end = min(extent_map_end(em) - 1, end);
3435 		iosize = ALIGN(iosize, blocksize);
3436 		if (this_bio_flag & EXTENT_BIO_COMPRESSED)
3437 			disk_bytenr = em->block_start;
3438 		else
3439 			disk_bytenr = em->block_start + extent_offset;
3440 		block_start = em->block_start;
3441 		if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3442 			block_start = EXTENT_MAP_HOLE;
3443 
3444 		/*
3445 		 * If we have a file range that points to a compressed extent
3446 		 * and it's followed by a consecutive file range that points
3447 		 * to the same compressed extent (possibly with a different
3448 		 * offset and/or length, so it either points to the whole extent
3449 		 * or only part of it), we must make sure we do not submit a
3450 		 * single bio to populate the pages for the 2 ranges because
3451 		 * this makes the compressed extent read zero out the pages
3452 		 * belonging to the 2nd range. Imagine the following scenario:
3453 		 *
3454 		 *  File layout
3455 		 *  [0 - 8K]                     [8K - 24K]
3456 		 *    |                               |
3457 		 *    |                               |
3458 		 * points to extent X,         points to extent X,
3459 		 * offset 4K, length of 8K     offset 0, length 16K
3460 		 *
3461 		 * [extent X, compressed length = 4K uncompressed length = 16K]
3462 		 *
3463 		 * If the bio to read the compressed extent covers both ranges,
3464 		 * it will decompress extent X into the pages belonging to the
3465 		 * first range and then it will stop, zeroing out the remaining
3466 		 * pages that belong to the other range that points to extent X.
3467 		 * So here we make sure we submit 2 bios, one for the first
3468 		 * range and another one for the third range. Both will target
3469 		 * the same physical extent from disk, but we can't currently
3470 		 * make the compressed bio endio callback populate the pages
3471 		 * for both ranges because each compressed bio is tightly
3472 		 * coupled with a single extent map, and each range can have
3473 		 * an extent map with a different offset value relative to the
3474 		 * uncompressed data of our extent and different lengths. This
3475 		 * is a corner case so we prioritize correctness over
3476 		 * non-optimal behavior (submitting 2 bios for the same extent).
3477 		 */
3478 		if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3479 		    prev_em_start && *prev_em_start != (u64)-1 &&
3480 		    *prev_em_start != em->start)
3481 			force_bio_submit = true;
3482 
3483 		if (prev_em_start)
3484 			*prev_em_start = em->start;
3485 
3486 		free_extent_map(em);
3487 		em = NULL;
3488 
3489 		/* we've found a hole, just zero and go on */
3490 		if (block_start == EXTENT_MAP_HOLE) {
3491 			char *userpage;
3492 			struct extent_state *cached = NULL;
3493 
3494 			userpage = kmap_atomic(page);
3495 			memset(userpage + pg_offset, 0, iosize);
3496 			flush_dcache_page(page);
3497 			kunmap_atomic(userpage);
3498 
3499 			set_extent_uptodate(tree, cur, cur + iosize - 1,
3500 					    &cached, GFP_NOFS);
3501 			unlock_extent_cached(tree, cur,
3502 					     cur + iosize - 1, &cached);
3503 			end_page_read(page, true, cur, iosize);
3504 			cur = cur + iosize;
3505 			pg_offset += iosize;
3506 			continue;
3507 		}
3508 		/* the get_extent function already copied into the page */
3509 		if (test_range_bit(tree, cur, cur_end,
3510 				   EXTENT_UPTODATE, 1, NULL)) {
3511 			check_page_uptodate(tree, page);
3512 			unlock_extent(tree, cur, cur + iosize - 1);
3513 			end_page_read(page, true, cur, iosize);
3514 			cur = cur + iosize;
3515 			pg_offset += iosize;
3516 			continue;
3517 		}
3518 		/* we have an inline extent but it didn't get marked up
3519 		 * to date.  Error out
3520 		 */
3521 		if (block_start == EXTENT_MAP_INLINE) {
3522 			unlock_extent(tree, cur, cur + iosize - 1);
3523 			end_page_read(page, false, cur, iosize);
3524 			cur = cur + iosize;
3525 			pg_offset += iosize;
3526 			continue;
3527 		}
3528 
3529 		ret = submit_extent_page(REQ_OP_READ | read_flags, NULL,
3530 					 page, disk_bytenr, iosize,
3531 					 pg_offset, bio,
3532 					 end_bio_extent_readpage, 0,
3533 					 *bio_flags,
3534 					 this_bio_flag,
3535 					 force_bio_submit);
3536 		if (!ret) {
3537 			nr++;
3538 			*bio_flags = this_bio_flag;
3539 		} else {
3540 			unlock_extent(tree, cur, cur + iosize - 1);
3541 			end_page_read(page, false, cur, iosize);
3542 			goto out;
3543 		}
3544 		cur = cur + iosize;
3545 		pg_offset += iosize;
3546 	}
3547 out:
3548 	return ret;
3549 }
3550 
3551 static inline void contiguous_readpages(struct page *pages[], int nr_pages,
3552 					     u64 start, u64 end,
3553 					     struct extent_map **em_cached,
3554 					     struct bio **bio,
3555 					     unsigned long *bio_flags,
3556 					     u64 *prev_em_start)
3557 {
3558 	struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);
3559 	int index;
3560 
3561 	btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
3562 
3563 	for (index = 0; index < nr_pages; index++) {
3564 		btrfs_do_readpage(pages[index], em_cached, bio, bio_flags,
3565 				  REQ_RAHEAD, prev_em_start);
3566 		put_page(pages[index]);
3567 	}
3568 }
3569 
3570 static void update_nr_written(struct writeback_control *wbc,
3571 			      unsigned long nr_written)
3572 {
3573 	wbc->nr_to_write -= nr_written;
3574 }
3575 
3576 /*
3577  * helper for __extent_writepage, doing all of the delayed allocation setup.
3578  *
3579  * This returns 1 if btrfs_run_delalloc_range function did all the work required
3580  * to write the page (copy into inline extent).  In this case the IO has
3581  * been started and the page is already unlocked.
3582  *
3583  * This returns 0 if all went well (page still locked)
3584  * This returns < 0 if there were errors (page still locked)
3585  */
3586 static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
3587 		struct page *page, struct writeback_control *wbc,
3588 		u64 delalloc_start, unsigned long *nr_written)
3589 {
3590 	u64 page_end = delalloc_start + PAGE_SIZE - 1;
3591 	bool found;
3592 	u64 delalloc_to_write = 0;
3593 	u64 delalloc_end = 0;
3594 	int ret;
3595 	int page_started = 0;
3596 
3597 
3598 	while (delalloc_end < page_end) {
3599 		found = find_lock_delalloc_range(&inode->vfs_inode, page,
3600 					       &delalloc_start,
3601 					       &delalloc_end);
3602 		if (!found) {
3603 			delalloc_start = delalloc_end + 1;
3604 			continue;
3605 		}
3606 		ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3607 				delalloc_end, &page_started, nr_written, wbc);
3608 		if (ret) {
3609 			SetPageError(page);
3610 			/*
3611 			 * btrfs_run_delalloc_range should return < 0 for error
3612 			 * but just in case, we use > 0 here meaning the IO is
3613 			 * started, so we don't want to return > 0 unless
3614 			 * things are going well.
3615 			 */
3616 			return ret < 0 ? ret : -EIO;
3617 		}
3618 		/*
3619 		 * delalloc_end is already one less than the total length, so
3620 		 * we don't subtract one from PAGE_SIZE
3621 		 */
3622 		delalloc_to_write += (delalloc_end - delalloc_start +
3623 				      PAGE_SIZE) >> PAGE_SHIFT;
3624 		delalloc_start = delalloc_end + 1;
3625 	}
3626 	if (wbc->nr_to_write < delalloc_to_write) {
3627 		int thresh = 8192;
3628 
3629 		if (delalloc_to_write < thresh * 2)
3630 			thresh = delalloc_to_write;
3631 		wbc->nr_to_write = min_t(u64, delalloc_to_write,
3632 					 thresh);
3633 	}
3634 
3635 	/* did the fill delalloc function already unlock and start
3636 	 * the IO?
3637 	 */
3638 	if (page_started) {
3639 		/*
3640 		 * we've unlocked the page, so we can't update
3641 		 * the mapping's writeback index, just update
3642 		 * nr_to_write.
3643 		 */
3644 		wbc->nr_to_write -= *nr_written;
3645 		return 1;
3646 	}
3647 
3648 	return 0;
3649 }
3650 
3651 /*
3652  * helper for __extent_writepage.  This calls the writepage start hooks,
3653  * and does the loop to map the page into extents and bios.
3654  *
3655  * We return 1 if the IO is started and the page is unlocked,
3656  * 0 if all went well (page still locked)
3657  * < 0 if there were errors (page still locked)
3658  */
3659 static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
3660 				 struct page *page,
3661 				 struct writeback_control *wbc,
3662 				 struct extent_page_data *epd,
3663 				 loff_t i_size,
3664 				 unsigned long nr_written,
3665 				 int *nr_ret)
3666 {
3667 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
3668 	struct extent_io_tree *tree = &inode->io_tree;
3669 	u64 start = page_offset(page);
3670 	u64 end = start + PAGE_SIZE - 1;
3671 	u64 cur = start;
3672 	u64 extent_offset;
3673 	u64 block_start;
3674 	struct extent_map *em;
3675 	int ret = 0;
3676 	int nr = 0;
3677 	u32 opf = REQ_OP_WRITE;
3678 	const unsigned int write_flags = wbc_to_write_flags(wbc);
3679 	bool compressed;
3680 
3681 	ret = btrfs_writepage_cow_fixup(page, start, end);
3682 	if (ret) {
3683 		/* Fixup worker will requeue */
3684 		redirty_page_for_writepage(wbc, page);
3685 		update_nr_written(wbc, nr_written);
3686 		unlock_page(page);
3687 		return 1;
3688 	}
3689 
3690 	/*
3691 	 * we don't want to touch the inode after unlocking the page,
3692 	 * so we update the mapping writeback index now
3693 	 */
3694 	update_nr_written(wbc, nr_written + 1);
3695 
3696 	while (cur <= end) {
3697 		u64 disk_bytenr;
3698 		u64 em_end;
3699 		u32 iosize;
3700 
3701 		if (cur >= i_size) {
3702 			btrfs_writepage_endio_finish_ordered(page, cur, end, 1);
3703 			break;
3704 		}
3705 		em = btrfs_get_extent(inode, NULL, 0, cur, end - cur + 1);
3706 		if (IS_ERR_OR_NULL(em)) {
3707 			SetPageError(page);
3708 			ret = PTR_ERR_OR_ZERO(em);
3709 			break;
3710 		}
3711 
3712 		extent_offset = cur - em->start;
3713 		em_end = extent_map_end(em);
3714 		ASSERT(cur <= em_end);
3715 		ASSERT(cur < end);
3716 		ASSERT(IS_ALIGNED(em->start, fs_info->sectorsize));
3717 		ASSERT(IS_ALIGNED(em->len, fs_info->sectorsize));
3718 		block_start = em->block_start;
3719 		compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3720 		disk_bytenr = em->block_start + extent_offset;
3721 
3722 		/* Note that em_end from extent_map_end() is exclusive */
3723 		iosize = min(em_end, end + 1) - cur;
3724 
3725 		if (btrfs_use_zone_append(inode, em))
3726 			opf = REQ_OP_ZONE_APPEND;
3727 
3728 		free_extent_map(em);
3729 		em = NULL;
3730 
3731 		/*
3732 		 * compressed and inline extents are written through other
3733 		 * paths in the FS
3734 		 */
3735 		if (compressed || block_start == EXTENT_MAP_HOLE ||
3736 		    block_start == EXTENT_MAP_INLINE) {
3737 			if (compressed)
3738 				nr++;
3739 			else
3740 				btrfs_writepage_endio_finish_ordered(page, cur,
3741 							cur + iosize - 1, 1);
3742 			cur += iosize;
3743 			continue;
3744 		}
3745 
3746 		btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
3747 		if (!PageWriteback(page)) {
3748 			btrfs_err(inode->root->fs_info,
3749 				   "page %lu not writeback, cur %llu end %llu",
3750 			       page->index, cur, end);
3751 		}
3752 
3753 		ret = submit_extent_page(opf | write_flags, wbc, page,
3754 					 disk_bytenr, iosize,
3755 					 cur - page_offset(page), &epd->bio,
3756 					 end_bio_extent_writepage,
3757 					 0, 0, 0, false);
3758 		if (ret) {
3759 			SetPageError(page);
3760 			if (PageWriteback(page))
3761 				end_page_writeback(page);
3762 		}
3763 
3764 		cur += iosize;
3765 		nr++;
3766 	}
3767 	*nr_ret = nr;
3768 	return ret;
3769 }
3770 
3771 /*
3772  * the writepage semantics are similar to regular writepage.  extent
3773  * records are inserted to lock ranges in the tree, and as dirty areas
3774  * are found, they are marked writeback.  Then the lock bits are removed
3775  * and the end_io handler clears the writeback ranges
3776  *
3777  * Return 0 if everything goes well.
3778  * Return <0 for error.
3779  */
3780 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3781 			      struct extent_page_data *epd)
3782 {
3783 	struct inode *inode = page->mapping->host;
3784 	u64 start = page_offset(page);
3785 	u64 page_end = start + PAGE_SIZE - 1;
3786 	int ret;
3787 	int nr = 0;
3788 	size_t pg_offset;
3789 	loff_t i_size = i_size_read(inode);
3790 	unsigned long end_index = i_size >> PAGE_SHIFT;
3791 	unsigned long nr_written = 0;
3792 
3793 	trace___extent_writepage(page, inode, wbc);
3794 
3795 	WARN_ON(!PageLocked(page));
3796 
3797 	ClearPageError(page);
3798 
3799 	pg_offset = offset_in_page(i_size);
3800 	if (page->index > end_index ||
3801 	   (page->index == end_index && !pg_offset)) {
3802 		page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
3803 		unlock_page(page);
3804 		return 0;
3805 	}
3806 
3807 	if (page->index == end_index) {
3808 		char *userpage;
3809 
3810 		userpage = kmap_atomic(page);
3811 		memset(userpage + pg_offset, 0,
3812 		       PAGE_SIZE - pg_offset);
3813 		kunmap_atomic(userpage);
3814 		flush_dcache_page(page);
3815 	}
3816 
3817 	ret = set_page_extent_mapped(page);
3818 	if (ret < 0) {
3819 		SetPageError(page);
3820 		goto done;
3821 	}
3822 
3823 	if (!epd->extent_locked) {
3824 		ret = writepage_delalloc(BTRFS_I(inode), page, wbc, start,
3825 					 &nr_written);
3826 		if (ret == 1)
3827 			return 0;
3828 		if (ret)
3829 			goto done;
3830 	}
3831 
3832 	ret = __extent_writepage_io(BTRFS_I(inode), page, wbc, epd, i_size,
3833 				    nr_written, &nr);
3834 	if (ret == 1)
3835 		return 0;
3836 
3837 done:
3838 	if (nr == 0) {
3839 		/* make sure the mapping tag for page dirty gets cleared */
3840 		set_page_writeback(page);
3841 		end_page_writeback(page);
3842 	}
3843 	if (PageError(page)) {
3844 		ret = ret < 0 ? ret : -EIO;
3845 		end_extent_writepage(page, ret, start, page_end);
3846 	}
3847 	unlock_page(page);
3848 	ASSERT(ret <= 0);
3849 	return ret;
3850 }
3851 
3852 void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3853 {
3854 	wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3855 		       TASK_UNINTERRUPTIBLE);
3856 }
3857 
3858 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3859 {
3860 	clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3861 	smp_mb__after_atomic();
3862 	wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3863 }
3864 
3865 /*
3866  * Lock extent buffer status and pages for writeback.
3867  *
3868  * May try to flush write bio if we can't get the lock.
3869  *
3870  * Return  0 if the extent buffer doesn't need to be submitted.
3871  *           (E.g. the extent buffer is not dirty)
3872  * Return >0 is the extent buffer is submitted to bio.
3873  * Return <0 if something went wrong, no page is locked.
3874  */
3875 static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
3876 			  struct extent_page_data *epd)
3877 {
3878 	struct btrfs_fs_info *fs_info = eb->fs_info;
3879 	int i, num_pages, failed_page_nr;
3880 	int flush = 0;
3881 	int ret = 0;
3882 
3883 	if (!btrfs_try_tree_write_lock(eb)) {
3884 		ret = flush_write_bio(epd);
3885 		if (ret < 0)
3886 			return ret;
3887 		flush = 1;
3888 		btrfs_tree_lock(eb);
3889 	}
3890 
3891 	if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3892 		btrfs_tree_unlock(eb);
3893 		if (!epd->sync_io)
3894 			return 0;
3895 		if (!flush) {
3896 			ret = flush_write_bio(epd);
3897 			if (ret < 0)
3898 				return ret;
3899 			flush = 1;
3900 		}
3901 		while (1) {
3902 			wait_on_extent_buffer_writeback(eb);
3903 			btrfs_tree_lock(eb);
3904 			if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3905 				break;
3906 			btrfs_tree_unlock(eb);
3907 		}
3908 	}
3909 
3910 	/*
3911 	 * We need to do this to prevent races in people who check if the eb is
3912 	 * under IO since we can end up having no IO bits set for a short period
3913 	 * of time.
3914 	 */
3915 	spin_lock(&eb->refs_lock);
3916 	if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3917 		set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3918 		spin_unlock(&eb->refs_lock);
3919 		btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3920 		percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3921 					 -eb->len,
3922 					 fs_info->dirty_metadata_batch);
3923 		ret = 1;
3924 	} else {
3925 		spin_unlock(&eb->refs_lock);
3926 	}
3927 
3928 	btrfs_tree_unlock(eb);
3929 
3930 	if (!ret)
3931 		return ret;
3932 
3933 	num_pages = num_extent_pages(eb);
3934 	for (i = 0; i < num_pages; i++) {
3935 		struct page *p = eb->pages[i];
3936 
3937 		if (!trylock_page(p)) {
3938 			if (!flush) {
3939 				int err;
3940 
3941 				err = flush_write_bio(epd);
3942 				if (err < 0) {
3943 					ret = err;
3944 					failed_page_nr = i;
3945 					goto err_unlock;
3946 				}
3947 				flush = 1;
3948 			}
3949 			lock_page(p);
3950 		}
3951 	}
3952 
3953 	return ret;
3954 err_unlock:
3955 	/* Unlock already locked pages */
3956 	for (i = 0; i < failed_page_nr; i++)
3957 		unlock_page(eb->pages[i]);
3958 	/*
3959 	 * Clear EXTENT_BUFFER_WRITEBACK and wake up anyone waiting on it.
3960 	 * Also set back EXTENT_BUFFER_DIRTY so future attempts to this eb can
3961 	 * be made and undo everything done before.
3962 	 */
3963 	btrfs_tree_lock(eb);
3964 	spin_lock(&eb->refs_lock);
3965 	set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3966 	end_extent_buffer_writeback(eb);
3967 	spin_unlock(&eb->refs_lock);
3968 	percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, eb->len,
3969 				 fs_info->dirty_metadata_batch);
3970 	btrfs_clear_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3971 	btrfs_tree_unlock(eb);
3972 	return ret;
3973 }
3974 
3975 static void set_btree_ioerr(struct page *page)
3976 {
3977 	struct extent_buffer *eb = (struct extent_buffer *)page->private;
3978 	struct btrfs_fs_info *fs_info;
3979 
3980 	SetPageError(page);
3981 	if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3982 		return;
3983 
3984 	/*
3985 	 * If we error out, we should add back the dirty_metadata_bytes
3986 	 * to make it consistent.
3987 	 */
3988 	fs_info = eb->fs_info;
3989 	percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3990 				 eb->len, fs_info->dirty_metadata_batch);
3991 
3992 	/*
3993 	 * If writeback for a btree extent that doesn't belong to a log tree
3994 	 * failed, increment the counter transaction->eb_write_errors.
3995 	 * We do this because while the transaction is running and before it's
3996 	 * committing (when we call filemap_fdata[write|wait]_range against
3997 	 * the btree inode), we might have
3998 	 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3999 	 * returns an error or an error happens during writeback, when we're
4000 	 * committing the transaction we wouldn't know about it, since the pages
4001 	 * can be no longer dirty nor marked anymore for writeback (if a
4002 	 * subsequent modification to the extent buffer didn't happen before the
4003 	 * transaction commit), which makes filemap_fdata[write|wait]_range not
4004 	 * able to find the pages tagged with SetPageError at transaction
4005 	 * commit time. So if this happens we must abort the transaction,
4006 	 * otherwise we commit a super block with btree roots that point to
4007 	 * btree nodes/leafs whose content on disk is invalid - either garbage
4008 	 * or the content of some node/leaf from a past generation that got
4009 	 * cowed or deleted and is no longer valid.
4010 	 *
4011 	 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
4012 	 * not be enough - we need to distinguish between log tree extents vs
4013 	 * non-log tree extents, and the next filemap_fdatawait_range() call
4014 	 * will catch and clear such errors in the mapping - and that call might
4015 	 * be from a log sync and not from a transaction commit. Also, checking
4016 	 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
4017 	 * not done and would not be reliable - the eb might have been released
4018 	 * from memory and reading it back again means that flag would not be
4019 	 * set (since it's a runtime flag, not persisted on disk).
4020 	 *
4021 	 * Using the flags below in the btree inode also makes us achieve the
4022 	 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
4023 	 * writeback for all dirty pages and before filemap_fdatawait_range()
4024 	 * is called, the writeback for all dirty pages had already finished
4025 	 * with errors - because we were not using AS_EIO/AS_ENOSPC,
4026 	 * filemap_fdatawait_range() would return success, as it could not know
4027 	 * that writeback errors happened (the pages were no longer tagged for
4028 	 * writeback).
4029 	 */
4030 	switch (eb->log_index) {
4031 	case -1:
4032 		set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
4033 		break;
4034 	case 0:
4035 		set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
4036 		break;
4037 	case 1:
4038 		set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
4039 		break;
4040 	default:
4041 		BUG(); /* unexpected, logic error */
4042 	}
4043 }
4044 
4045 static void end_bio_extent_buffer_writepage(struct bio *bio)
4046 {
4047 	struct bio_vec *bvec;
4048 	struct extent_buffer *eb;
4049 	int done;
4050 	struct bvec_iter_all iter_all;
4051 
4052 	ASSERT(!bio_flagged(bio, BIO_CLONED));
4053 	bio_for_each_segment_all(bvec, bio, iter_all) {
4054 		struct page *page = bvec->bv_page;
4055 
4056 		eb = (struct extent_buffer *)page->private;
4057 		BUG_ON(!eb);
4058 		done = atomic_dec_and_test(&eb->io_pages);
4059 
4060 		if (bio->bi_status ||
4061 		    test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
4062 			ClearPageUptodate(page);
4063 			set_btree_ioerr(page);
4064 		}
4065 
4066 		end_page_writeback(page);
4067 
4068 		if (!done)
4069 			continue;
4070 
4071 		end_extent_buffer_writeback(eb);
4072 	}
4073 
4074 	bio_put(bio);
4075 }
4076 
4077 static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
4078 			struct writeback_control *wbc,
4079 			struct extent_page_data *epd)
4080 {
4081 	u64 disk_bytenr = eb->start;
4082 	u32 nritems;
4083 	int i, num_pages;
4084 	unsigned long start, end;
4085 	unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
4086 	int ret = 0;
4087 
4088 	clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
4089 	num_pages = num_extent_pages(eb);
4090 	atomic_set(&eb->io_pages, num_pages);
4091 
4092 	/* set btree blocks beyond nritems with 0 to avoid stale content. */
4093 	nritems = btrfs_header_nritems(eb);
4094 	if (btrfs_header_level(eb) > 0) {
4095 		end = btrfs_node_key_ptr_offset(nritems);
4096 
4097 		memzero_extent_buffer(eb, end, eb->len - end);
4098 	} else {
4099 		/*
4100 		 * leaf:
4101 		 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
4102 		 */
4103 		start = btrfs_item_nr_offset(nritems);
4104 		end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
4105 		memzero_extent_buffer(eb, start, end - start);
4106 	}
4107 
4108 	for (i = 0; i < num_pages; i++) {
4109 		struct page *p = eb->pages[i];
4110 
4111 		clear_page_dirty_for_io(p);
4112 		set_page_writeback(p);
4113 		ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
4114 					 p, disk_bytenr, PAGE_SIZE, 0,
4115 					 &epd->bio,
4116 					 end_bio_extent_buffer_writepage,
4117 					 0, 0, 0, false);
4118 		if (ret) {
4119 			set_btree_ioerr(p);
4120 			if (PageWriteback(p))
4121 				end_page_writeback(p);
4122 			if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
4123 				end_extent_buffer_writeback(eb);
4124 			ret = -EIO;
4125 			break;
4126 		}
4127 		disk_bytenr += PAGE_SIZE;
4128 		update_nr_written(wbc, 1);
4129 		unlock_page(p);
4130 	}
4131 
4132 	if (unlikely(ret)) {
4133 		for (; i < num_pages; i++) {
4134 			struct page *p = eb->pages[i];
4135 			clear_page_dirty_for_io(p);
4136 			unlock_page(p);
4137 		}
4138 	}
4139 
4140 	return ret;
4141 }
4142 
4143 /*
4144  * Submit all page(s) of one extent buffer.
4145  *
4146  * @page:	the page of one extent buffer
4147  * @eb_context:	to determine if we need to submit this page, if current page
4148  *		belongs to this eb, we don't need to submit
4149  *
4150  * The caller should pass each page in their bytenr order, and here we use
4151  * @eb_context to determine if we have submitted pages of one extent buffer.
4152  *
4153  * If we have, we just skip until we hit a new page that doesn't belong to
4154  * current @eb_context.
4155  *
4156  * If not, we submit all the page(s) of the extent buffer.
4157  *
4158  * Return >0 if we have submitted the extent buffer successfully.
4159  * Return 0 if we don't need to submit the page, as it's already submitted by
4160  * previous call.
4161  * Return <0 for fatal error.
4162  */
4163 static int submit_eb_page(struct page *page, struct writeback_control *wbc,
4164 			  struct extent_page_data *epd,
4165 			  struct extent_buffer **eb_context)
4166 {
4167 	struct address_space *mapping = page->mapping;
4168 	struct btrfs_block_group *cache = NULL;
4169 	struct extent_buffer *eb;
4170 	int ret;
4171 
4172 	if (!PagePrivate(page))
4173 		return 0;
4174 
4175 	spin_lock(&mapping->private_lock);
4176 	if (!PagePrivate(page)) {
4177 		spin_unlock(&mapping->private_lock);
4178 		return 0;
4179 	}
4180 
4181 	eb = (struct extent_buffer *)page->private;
4182 
4183 	/*
4184 	 * Shouldn't happen and normally this would be a BUG_ON but no point
4185 	 * crashing the machine for something we can survive anyway.
4186 	 */
4187 	if (WARN_ON(!eb)) {
4188 		spin_unlock(&mapping->private_lock);
4189 		return 0;
4190 	}
4191 
4192 	if (eb == *eb_context) {
4193 		spin_unlock(&mapping->private_lock);
4194 		return 0;
4195 	}
4196 	ret = atomic_inc_not_zero(&eb->refs);
4197 	spin_unlock(&mapping->private_lock);
4198 	if (!ret)
4199 		return 0;
4200 
4201 	if (!btrfs_check_meta_write_pointer(eb->fs_info, eb, &cache)) {
4202 		/*
4203 		 * If for_sync, this hole will be filled with
4204 		 * trasnsaction commit.
4205 		 */
4206 		if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
4207 			ret = -EAGAIN;
4208 		else
4209 			ret = 0;
4210 		free_extent_buffer(eb);
4211 		return ret;
4212 	}
4213 
4214 	*eb_context = eb;
4215 
4216 	ret = lock_extent_buffer_for_io(eb, epd);
4217 	if (ret <= 0) {
4218 		btrfs_revert_meta_write_pointer(cache, eb);
4219 		if (cache)
4220 			btrfs_put_block_group(cache);
4221 		free_extent_buffer(eb);
4222 		return ret;
4223 	}
4224 	if (cache)
4225 		btrfs_put_block_group(cache);
4226 	ret = write_one_eb(eb, wbc, epd);
4227 	free_extent_buffer(eb);
4228 	if (ret < 0)
4229 		return ret;
4230 	return 1;
4231 }
4232 
4233 int btree_write_cache_pages(struct address_space *mapping,
4234 				   struct writeback_control *wbc)
4235 {
4236 	struct extent_buffer *eb_context = NULL;
4237 	struct extent_page_data epd = {
4238 		.bio = NULL,
4239 		.extent_locked = 0,
4240 		.sync_io = wbc->sync_mode == WB_SYNC_ALL,
4241 	};
4242 	struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
4243 	int ret = 0;
4244 	int done = 0;
4245 	int nr_to_write_done = 0;
4246 	struct pagevec pvec;
4247 	int nr_pages;
4248 	pgoff_t index;
4249 	pgoff_t end;		/* Inclusive */
4250 	int scanned = 0;
4251 	xa_mark_t tag;
4252 
4253 	pagevec_init(&pvec);
4254 	if (wbc->range_cyclic) {
4255 		index = mapping->writeback_index; /* Start from prev offset */
4256 		end = -1;
4257 		/*
4258 		 * Start from the beginning does not need to cycle over the
4259 		 * range, mark it as scanned.
4260 		 */
4261 		scanned = (index == 0);
4262 	} else {
4263 		index = wbc->range_start >> PAGE_SHIFT;
4264 		end = wbc->range_end >> PAGE_SHIFT;
4265 		scanned = 1;
4266 	}
4267 	if (wbc->sync_mode == WB_SYNC_ALL)
4268 		tag = PAGECACHE_TAG_TOWRITE;
4269 	else
4270 		tag = PAGECACHE_TAG_DIRTY;
4271 	btrfs_zoned_meta_io_lock(fs_info);
4272 retry:
4273 	if (wbc->sync_mode == WB_SYNC_ALL)
4274 		tag_pages_for_writeback(mapping, index, end);
4275 	while (!done && !nr_to_write_done && (index <= end) &&
4276 	       (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
4277 			tag))) {
4278 		unsigned i;
4279 
4280 		for (i = 0; i < nr_pages; i++) {
4281 			struct page *page = pvec.pages[i];
4282 
4283 			ret = submit_eb_page(page, wbc, &epd, &eb_context);
4284 			if (ret == 0)
4285 				continue;
4286 			if (ret < 0) {
4287 				done = 1;
4288 				break;
4289 			}
4290 
4291 			/*
4292 			 * the filesystem may choose to bump up nr_to_write.
4293 			 * We have to make sure to honor the new nr_to_write
4294 			 * at any time
4295 			 */
4296 			nr_to_write_done = wbc->nr_to_write <= 0;
4297 		}
4298 		pagevec_release(&pvec);
4299 		cond_resched();
4300 	}
4301 	if (!scanned && !done) {
4302 		/*
4303 		 * We hit the last page and there is more work to be done: wrap
4304 		 * back to the start of the file
4305 		 */
4306 		scanned = 1;
4307 		index = 0;
4308 		goto retry;
4309 	}
4310 	if (ret < 0) {
4311 		end_write_bio(&epd, ret);
4312 		goto out;
4313 	}
4314 	/*
4315 	 * If something went wrong, don't allow any metadata write bio to be
4316 	 * submitted.
4317 	 *
4318 	 * This would prevent use-after-free if we had dirty pages not
4319 	 * cleaned up, which can still happen by fuzzed images.
4320 	 *
4321 	 * - Bad extent tree
4322 	 *   Allowing existing tree block to be allocated for other trees.
4323 	 *
4324 	 * - Log tree operations
4325 	 *   Exiting tree blocks get allocated to log tree, bumps its
4326 	 *   generation, then get cleaned in tree re-balance.
4327 	 *   Such tree block will not be written back, since it's clean,
4328 	 *   thus no WRITTEN flag set.
4329 	 *   And after log writes back, this tree block is not traced by
4330 	 *   any dirty extent_io_tree.
4331 	 *
4332 	 * - Offending tree block gets re-dirtied from its original owner
4333 	 *   Since it has bumped generation, no WRITTEN flag, it can be
4334 	 *   reused without COWing. This tree block will not be traced
4335 	 *   by btrfs_transaction::dirty_pages.
4336 	 *
4337 	 *   Now such dirty tree block will not be cleaned by any dirty
4338 	 *   extent io tree. Thus we don't want to submit such wild eb
4339 	 *   if the fs already has error.
4340 	 */
4341 	if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
4342 		ret = flush_write_bio(&epd);
4343 	} else {
4344 		ret = -EROFS;
4345 		end_write_bio(&epd, ret);
4346 	}
4347 out:
4348 	btrfs_zoned_meta_io_unlock(fs_info);
4349 	return ret;
4350 }
4351 
4352 /**
4353  * Walk the list of dirty pages of the given address space and write all of them.
4354  *
4355  * @mapping: address space structure to write
4356  * @wbc:     subtract the number of written pages from *@wbc->nr_to_write
4357  * @epd:     holds context for the write, namely the bio
4358  *
4359  * If a page is already under I/O, write_cache_pages() skips it, even
4360  * if it's dirty.  This is desirable behaviour for memory-cleaning writeback,
4361  * but it is INCORRECT for data-integrity system calls such as fsync().  fsync()
4362  * and msync() need to guarantee that all the data which was dirty at the time
4363  * the call was made get new I/O started against them.  If wbc->sync_mode is
4364  * WB_SYNC_ALL then we were called for data integrity and we must wait for
4365  * existing IO to complete.
4366  */
4367 static int extent_write_cache_pages(struct address_space *mapping,
4368 			     struct writeback_control *wbc,
4369 			     struct extent_page_data *epd)
4370 {
4371 	struct inode *inode = mapping->host;
4372 	int ret = 0;
4373 	int done = 0;
4374 	int nr_to_write_done = 0;
4375 	struct pagevec pvec;
4376 	int nr_pages;
4377 	pgoff_t index;
4378 	pgoff_t end;		/* Inclusive */
4379 	pgoff_t done_index;
4380 	int range_whole = 0;
4381 	int scanned = 0;
4382 	xa_mark_t tag;
4383 
4384 	/*
4385 	 * We have to hold onto the inode so that ordered extents can do their
4386 	 * work when the IO finishes.  The alternative to this is failing to add
4387 	 * an ordered extent if the igrab() fails there and that is a huge pain
4388 	 * to deal with, so instead just hold onto the inode throughout the
4389 	 * writepages operation.  If it fails here we are freeing up the inode
4390 	 * anyway and we'd rather not waste our time writing out stuff that is
4391 	 * going to be truncated anyway.
4392 	 */
4393 	if (!igrab(inode))
4394 		return 0;
4395 
4396 	pagevec_init(&pvec);
4397 	if (wbc->range_cyclic) {
4398 		index = mapping->writeback_index; /* Start from prev offset */
4399 		end = -1;
4400 		/*
4401 		 * Start from the beginning does not need to cycle over the
4402 		 * range, mark it as scanned.
4403 		 */
4404 		scanned = (index == 0);
4405 	} else {
4406 		index = wbc->range_start >> PAGE_SHIFT;
4407 		end = wbc->range_end >> PAGE_SHIFT;
4408 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
4409 			range_whole = 1;
4410 		scanned = 1;
4411 	}
4412 
4413 	/*
4414 	 * We do the tagged writepage as long as the snapshot flush bit is set
4415 	 * and we are the first one who do the filemap_flush() on this inode.
4416 	 *
4417 	 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
4418 	 * not race in and drop the bit.
4419 	 */
4420 	if (range_whole && wbc->nr_to_write == LONG_MAX &&
4421 	    test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
4422 			       &BTRFS_I(inode)->runtime_flags))
4423 		wbc->tagged_writepages = 1;
4424 
4425 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
4426 		tag = PAGECACHE_TAG_TOWRITE;
4427 	else
4428 		tag = PAGECACHE_TAG_DIRTY;
4429 retry:
4430 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
4431 		tag_pages_for_writeback(mapping, index, end);
4432 	done_index = index;
4433 	while (!done && !nr_to_write_done && (index <= end) &&
4434 			(nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
4435 						&index, end, tag))) {
4436 		unsigned i;
4437 
4438 		for (i = 0; i < nr_pages; i++) {
4439 			struct page *page = pvec.pages[i];
4440 
4441 			done_index = page->index + 1;
4442 			/*
4443 			 * At this point we hold neither the i_pages lock nor
4444 			 * the page lock: the page may be truncated or
4445 			 * invalidated (changing page->mapping to NULL),
4446 			 * or even swizzled back from swapper_space to
4447 			 * tmpfs file mapping
4448 			 */
4449 			if (!trylock_page(page)) {
4450 				ret = flush_write_bio(epd);
4451 				BUG_ON(ret < 0);
4452 				lock_page(page);
4453 			}
4454 
4455 			if (unlikely(page->mapping != mapping)) {
4456 				unlock_page(page);
4457 				continue;
4458 			}
4459 
4460 			if (wbc->sync_mode != WB_SYNC_NONE) {
4461 				if (PageWriteback(page)) {
4462 					ret = flush_write_bio(epd);
4463 					BUG_ON(ret < 0);
4464 				}
4465 				wait_on_page_writeback(page);
4466 			}
4467 
4468 			if (PageWriteback(page) ||
4469 			    !clear_page_dirty_for_io(page)) {
4470 				unlock_page(page);
4471 				continue;
4472 			}
4473 
4474 			ret = __extent_writepage(page, wbc, epd);
4475 			if (ret < 0) {
4476 				done = 1;
4477 				break;
4478 			}
4479 
4480 			/*
4481 			 * the filesystem may choose to bump up nr_to_write.
4482 			 * We have to make sure to honor the new nr_to_write
4483 			 * at any time
4484 			 */
4485 			nr_to_write_done = wbc->nr_to_write <= 0;
4486 		}
4487 		pagevec_release(&pvec);
4488 		cond_resched();
4489 	}
4490 	if (!scanned && !done) {
4491 		/*
4492 		 * We hit the last page and there is more work to be done: wrap
4493 		 * back to the start of the file
4494 		 */
4495 		scanned = 1;
4496 		index = 0;
4497 
4498 		/*
4499 		 * If we're looping we could run into a page that is locked by a
4500 		 * writer and that writer could be waiting on writeback for a
4501 		 * page in our current bio, and thus deadlock, so flush the
4502 		 * write bio here.
4503 		 */
4504 		ret = flush_write_bio(epd);
4505 		if (!ret)
4506 			goto retry;
4507 	}
4508 
4509 	if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4510 		mapping->writeback_index = done_index;
4511 
4512 	btrfs_add_delayed_iput(inode);
4513 	return ret;
4514 }
4515 
4516 int extent_write_full_page(struct page *page, struct writeback_control *wbc)
4517 {
4518 	int ret;
4519 	struct extent_page_data epd = {
4520 		.bio = NULL,
4521 		.extent_locked = 0,
4522 		.sync_io = wbc->sync_mode == WB_SYNC_ALL,
4523 	};
4524 
4525 	ret = __extent_writepage(page, wbc, &epd);
4526 	ASSERT(ret <= 0);
4527 	if (ret < 0) {
4528 		end_write_bio(&epd, ret);
4529 		return ret;
4530 	}
4531 
4532 	ret = flush_write_bio(&epd);
4533 	ASSERT(ret <= 0);
4534 	return ret;
4535 }
4536 
4537 int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
4538 			      int mode)
4539 {
4540 	int ret = 0;
4541 	struct address_space *mapping = inode->i_mapping;
4542 	struct page *page;
4543 	unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4544 		PAGE_SHIFT;
4545 
4546 	struct extent_page_data epd = {
4547 		.bio = NULL,
4548 		.extent_locked = 1,
4549 		.sync_io = mode == WB_SYNC_ALL,
4550 	};
4551 	struct writeback_control wbc_writepages = {
4552 		.sync_mode	= mode,
4553 		.nr_to_write	= nr_pages * 2,
4554 		.range_start	= start,
4555 		.range_end	= end + 1,
4556 		/* We're called from an async helper function */
4557 		.punt_to_cgroup	= 1,
4558 		.no_cgroup_owner = 1,
4559 	};
4560 
4561 	wbc_attach_fdatawrite_inode(&wbc_writepages, inode);
4562 	while (start <= end) {
4563 		page = find_get_page(mapping, start >> PAGE_SHIFT);
4564 		if (clear_page_dirty_for_io(page))
4565 			ret = __extent_writepage(page, &wbc_writepages, &epd);
4566 		else {
4567 			btrfs_writepage_endio_finish_ordered(page, start,
4568 						    start + PAGE_SIZE - 1, 1);
4569 			unlock_page(page);
4570 		}
4571 		put_page(page);
4572 		start += PAGE_SIZE;
4573 	}
4574 
4575 	ASSERT(ret <= 0);
4576 	if (ret == 0)
4577 		ret = flush_write_bio(&epd);
4578 	else
4579 		end_write_bio(&epd, ret);
4580 
4581 	wbc_detach_inode(&wbc_writepages);
4582 	return ret;
4583 }
4584 
4585 int extent_writepages(struct address_space *mapping,
4586 		      struct writeback_control *wbc)
4587 {
4588 	int ret = 0;
4589 	struct extent_page_data epd = {
4590 		.bio = NULL,
4591 		.extent_locked = 0,
4592 		.sync_io = wbc->sync_mode == WB_SYNC_ALL,
4593 	};
4594 
4595 	ret = extent_write_cache_pages(mapping, wbc, &epd);
4596 	ASSERT(ret <= 0);
4597 	if (ret < 0) {
4598 		end_write_bio(&epd, ret);
4599 		return ret;
4600 	}
4601 	ret = flush_write_bio(&epd);
4602 	return ret;
4603 }
4604 
4605 void extent_readahead(struct readahead_control *rac)
4606 {
4607 	struct bio *bio = NULL;
4608 	unsigned long bio_flags = 0;
4609 	struct page *pagepool[16];
4610 	struct extent_map *em_cached = NULL;
4611 	u64 prev_em_start = (u64)-1;
4612 	int nr;
4613 
4614 	while ((nr = readahead_page_batch(rac, pagepool))) {
4615 		u64 contig_start = page_offset(pagepool[0]);
4616 		u64 contig_end = page_offset(pagepool[nr - 1]) + PAGE_SIZE - 1;
4617 
4618 		ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4619 
4620 		contiguous_readpages(pagepool, nr, contig_start, contig_end,
4621 				&em_cached, &bio, &bio_flags, &prev_em_start);
4622 	}
4623 
4624 	if (em_cached)
4625 		free_extent_map(em_cached);
4626 
4627 	if (bio) {
4628 		if (submit_one_bio(bio, 0, bio_flags))
4629 			return;
4630 	}
4631 }
4632 
4633 /*
4634  * basic invalidatepage code, this waits on any locked or writeback
4635  * ranges corresponding to the page, and then deletes any extent state
4636  * records from the tree
4637  */
4638 int extent_invalidatepage(struct extent_io_tree *tree,
4639 			  struct page *page, unsigned long offset)
4640 {
4641 	struct extent_state *cached_state = NULL;
4642 	u64 start = page_offset(page);
4643 	u64 end = start + PAGE_SIZE - 1;
4644 	size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4645 
4646 	/* This function is only called for the btree inode */
4647 	ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO);
4648 
4649 	start += ALIGN(offset, blocksize);
4650 	if (start > end)
4651 		return 0;
4652 
4653 	lock_extent_bits(tree, start, end, &cached_state);
4654 	wait_on_page_writeback(page);
4655 
4656 	/*
4657 	 * Currently for btree io tree, only EXTENT_LOCKED is utilized,
4658 	 * so here we only need to unlock the extent range to free any
4659 	 * existing extent state.
4660 	 */
4661 	unlock_extent_cached(tree, start, end, &cached_state);
4662 	return 0;
4663 }
4664 
4665 /*
4666  * a helper for releasepage, this tests for areas of the page that
4667  * are locked or under IO and drops the related state bits if it is safe
4668  * to drop the page.
4669  */
4670 static int try_release_extent_state(struct extent_io_tree *tree,
4671 				    struct page *page, gfp_t mask)
4672 {
4673 	u64 start = page_offset(page);
4674 	u64 end = start + PAGE_SIZE - 1;
4675 	int ret = 1;
4676 
4677 	if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
4678 		ret = 0;
4679 	} else {
4680 		/*
4681 		 * At this point we can safely clear everything except the
4682 		 * locked bit, the nodatasum bit and the delalloc new bit.
4683 		 * The delalloc new bit will be cleared by ordered extent
4684 		 * completion.
4685 		 */
4686 		ret = __clear_extent_bit(tree, start, end,
4687 			 ~(EXTENT_LOCKED | EXTENT_NODATASUM | EXTENT_DELALLOC_NEW),
4688 			 0, 0, NULL, mask, NULL);
4689 
4690 		/* if clear_extent_bit failed for enomem reasons,
4691 		 * we can't allow the release to continue.
4692 		 */
4693 		if (ret < 0)
4694 			ret = 0;
4695 		else
4696 			ret = 1;
4697 	}
4698 	return ret;
4699 }
4700 
4701 /*
4702  * a helper for releasepage.  As long as there are no locked extents
4703  * in the range corresponding to the page, both state records and extent
4704  * map records are removed
4705  */
4706 int try_release_extent_mapping(struct page *page, gfp_t mask)
4707 {
4708 	struct extent_map *em;
4709 	u64 start = page_offset(page);
4710 	u64 end = start + PAGE_SIZE - 1;
4711 	struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4712 	struct extent_io_tree *tree = &btrfs_inode->io_tree;
4713 	struct extent_map_tree *map = &btrfs_inode->extent_tree;
4714 
4715 	if (gfpflags_allow_blocking(mask) &&
4716 	    page->mapping->host->i_size > SZ_16M) {
4717 		u64 len;
4718 		while (start <= end) {
4719 			struct btrfs_fs_info *fs_info;
4720 			u64 cur_gen;
4721 
4722 			len = end - start + 1;
4723 			write_lock(&map->lock);
4724 			em = lookup_extent_mapping(map, start, len);
4725 			if (!em) {
4726 				write_unlock(&map->lock);
4727 				break;
4728 			}
4729 			if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4730 			    em->start != start) {
4731 				write_unlock(&map->lock);
4732 				free_extent_map(em);
4733 				break;
4734 			}
4735 			if (test_range_bit(tree, em->start,
4736 					   extent_map_end(em) - 1,
4737 					   EXTENT_LOCKED, 0, NULL))
4738 				goto next;
4739 			/*
4740 			 * If it's not in the list of modified extents, used
4741 			 * by a fast fsync, we can remove it. If it's being
4742 			 * logged we can safely remove it since fsync took an
4743 			 * extra reference on the em.
4744 			 */
4745 			if (list_empty(&em->list) ||
4746 			    test_bit(EXTENT_FLAG_LOGGING, &em->flags))
4747 				goto remove_em;
4748 			/*
4749 			 * If it's in the list of modified extents, remove it
4750 			 * only if its generation is older then the current one,
4751 			 * in which case we don't need it for a fast fsync.
4752 			 * Otherwise don't remove it, we could be racing with an
4753 			 * ongoing fast fsync that could miss the new extent.
4754 			 */
4755 			fs_info = btrfs_inode->root->fs_info;
4756 			spin_lock(&fs_info->trans_lock);
4757 			cur_gen = fs_info->generation;
4758 			spin_unlock(&fs_info->trans_lock);
4759 			if (em->generation >= cur_gen)
4760 				goto next;
4761 remove_em:
4762 			/*
4763 			 * We only remove extent maps that are not in the list of
4764 			 * modified extents or that are in the list but with a
4765 			 * generation lower then the current generation, so there
4766 			 * is no need to set the full fsync flag on the inode (it
4767 			 * hurts the fsync performance for workloads with a data
4768 			 * size that exceeds or is close to the system's memory).
4769 			 */
4770 			remove_extent_mapping(map, em);
4771 			/* once for the rb tree */
4772 			free_extent_map(em);
4773 next:
4774 			start = extent_map_end(em);
4775 			write_unlock(&map->lock);
4776 
4777 			/* once for us */
4778 			free_extent_map(em);
4779 
4780 			cond_resched(); /* Allow large-extent preemption. */
4781 		}
4782 	}
4783 	return try_release_extent_state(tree, page, mask);
4784 }
4785 
4786 /*
4787  * helper function for fiemap, which doesn't want to see any holes.
4788  * This maps until we find something past 'last'
4789  */
4790 static struct extent_map *get_extent_skip_holes(struct btrfs_inode *inode,
4791 						u64 offset, u64 last)
4792 {
4793 	u64 sectorsize = btrfs_inode_sectorsize(inode);
4794 	struct extent_map *em;
4795 	u64 len;
4796 
4797 	if (offset >= last)
4798 		return NULL;
4799 
4800 	while (1) {
4801 		len = last - offset;
4802 		if (len == 0)
4803 			break;
4804 		len = ALIGN(len, sectorsize);
4805 		em = btrfs_get_extent_fiemap(inode, offset, len);
4806 		if (IS_ERR_OR_NULL(em))
4807 			return em;
4808 
4809 		/* if this isn't a hole return it */
4810 		if (em->block_start != EXTENT_MAP_HOLE)
4811 			return em;
4812 
4813 		/* this is a hole, advance to the next extent */
4814 		offset = extent_map_end(em);
4815 		free_extent_map(em);
4816 		if (offset >= last)
4817 			break;
4818 	}
4819 	return NULL;
4820 }
4821 
4822 /*
4823  * To cache previous fiemap extent
4824  *
4825  * Will be used for merging fiemap extent
4826  */
4827 struct fiemap_cache {
4828 	u64 offset;
4829 	u64 phys;
4830 	u64 len;
4831 	u32 flags;
4832 	bool cached;
4833 };
4834 
4835 /*
4836  * Helper to submit fiemap extent.
4837  *
4838  * Will try to merge current fiemap extent specified by @offset, @phys,
4839  * @len and @flags with cached one.
4840  * And only when we fails to merge, cached one will be submitted as
4841  * fiemap extent.
4842  *
4843  * Return value is the same as fiemap_fill_next_extent().
4844  */
4845 static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4846 				struct fiemap_cache *cache,
4847 				u64 offset, u64 phys, u64 len, u32 flags)
4848 {
4849 	int ret = 0;
4850 
4851 	if (!cache->cached)
4852 		goto assign;
4853 
4854 	/*
4855 	 * Sanity check, extent_fiemap() should have ensured that new
4856 	 * fiemap extent won't overlap with cached one.
4857 	 * Not recoverable.
4858 	 *
4859 	 * NOTE: Physical address can overlap, due to compression
4860 	 */
4861 	if (cache->offset + cache->len > offset) {
4862 		WARN_ON(1);
4863 		return -EINVAL;
4864 	}
4865 
4866 	/*
4867 	 * Only merges fiemap extents if
4868 	 * 1) Their logical addresses are continuous
4869 	 *
4870 	 * 2) Their physical addresses are continuous
4871 	 *    So truly compressed (physical size smaller than logical size)
4872 	 *    extents won't get merged with each other
4873 	 *
4874 	 * 3) Share same flags except FIEMAP_EXTENT_LAST
4875 	 *    So regular extent won't get merged with prealloc extent
4876 	 */
4877 	if (cache->offset + cache->len  == offset &&
4878 	    cache->phys + cache->len == phys  &&
4879 	    (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4880 			(flags & ~FIEMAP_EXTENT_LAST)) {
4881 		cache->len += len;
4882 		cache->flags |= flags;
4883 		goto try_submit_last;
4884 	}
4885 
4886 	/* Not mergeable, need to submit cached one */
4887 	ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4888 				      cache->len, cache->flags);
4889 	cache->cached = false;
4890 	if (ret)
4891 		return ret;
4892 assign:
4893 	cache->cached = true;
4894 	cache->offset = offset;
4895 	cache->phys = phys;
4896 	cache->len = len;
4897 	cache->flags = flags;
4898 try_submit_last:
4899 	if (cache->flags & FIEMAP_EXTENT_LAST) {
4900 		ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4901 				cache->phys, cache->len, cache->flags);
4902 		cache->cached = false;
4903 	}
4904 	return ret;
4905 }
4906 
4907 /*
4908  * Emit last fiemap cache
4909  *
4910  * The last fiemap cache may still be cached in the following case:
4911  * 0		      4k		    8k
4912  * |<- Fiemap range ->|
4913  * |<------------  First extent ----------->|
4914  *
4915  * In this case, the first extent range will be cached but not emitted.
4916  * So we must emit it before ending extent_fiemap().
4917  */
4918 static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
4919 				  struct fiemap_cache *cache)
4920 {
4921 	int ret;
4922 
4923 	if (!cache->cached)
4924 		return 0;
4925 
4926 	ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4927 				      cache->len, cache->flags);
4928 	cache->cached = false;
4929 	if (ret > 0)
4930 		ret = 0;
4931 	return ret;
4932 }
4933 
4934 int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo,
4935 		  u64 start, u64 len)
4936 {
4937 	int ret = 0;
4938 	u64 off = start;
4939 	u64 max = start + len;
4940 	u32 flags = 0;
4941 	u32 found_type;
4942 	u64 last;
4943 	u64 last_for_get_extent = 0;
4944 	u64 disko = 0;
4945 	u64 isize = i_size_read(&inode->vfs_inode);
4946 	struct btrfs_key found_key;
4947 	struct extent_map *em = NULL;
4948 	struct extent_state *cached_state = NULL;
4949 	struct btrfs_path *path;
4950 	struct btrfs_root *root = inode->root;
4951 	struct fiemap_cache cache = { 0 };
4952 	struct ulist *roots;
4953 	struct ulist *tmp_ulist;
4954 	int end = 0;
4955 	u64 em_start = 0;
4956 	u64 em_len = 0;
4957 	u64 em_end = 0;
4958 
4959 	if (len == 0)
4960 		return -EINVAL;
4961 
4962 	path = btrfs_alloc_path();
4963 	if (!path)
4964 		return -ENOMEM;
4965 
4966 	roots = ulist_alloc(GFP_KERNEL);
4967 	tmp_ulist = ulist_alloc(GFP_KERNEL);
4968 	if (!roots || !tmp_ulist) {
4969 		ret = -ENOMEM;
4970 		goto out_free_ulist;
4971 	}
4972 
4973 	start = round_down(start, btrfs_inode_sectorsize(inode));
4974 	len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
4975 
4976 	/*
4977 	 * lookup the last file extent.  We're not using i_size here
4978 	 * because there might be preallocation past i_size
4979 	 */
4980 	ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
4981 				       0);
4982 	if (ret < 0) {
4983 		goto out_free_ulist;
4984 	} else {
4985 		WARN_ON(!ret);
4986 		if (ret == 1)
4987 			ret = 0;
4988 	}
4989 
4990 	path->slots[0]--;
4991 	btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
4992 	found_type = found_key.type;
4993 
4994 	/* No extents, but there might be delalloc bits */
4995 	if (found_key.objectid != btrfs_ino(inode) ||
4996 	    found_type != BTRFS_EXTENT_DATA_KEY) {
4997 		/* have to trust i_size as the end */
4998 		last = (u64)-1;
4999 		last_for_get_extent = isize;
5000 	} else {
5001 		/*
5002 		 * remember the start of the last extent.  There are a
5003 		 * bunch of different factors that go into the length of the
5004 		 * extent, so its much less complex to remember where it started
5005 		 */
5006 		last = found_key.offset;
5007 		last_for_get_extent = last + 1;
5008 	}
5009 	btrfs_release_path(path);
5010 
5011 	/*
5012 	 * we might have some extents allocated but more delalloc past those
5013 	 * extents.  so, we trust isize unless the start of the last extent is
5014 	 * beyond isize
5015 	 */
5016 	if (last < isize) {
5017 		last = (u64)-1;
5018 		last_for_get_extent = isize;
5019 	}
5020 
5021 	lock_extent_bits(&inode->io_tree, start, start + len - 1,
5022 			 &cached_state);
5023 
5024 	em = get_extent_skip_holes(inode, start, last_for_get_extent);
5025 	if (!em)
5026 		goto out;
5027 	if (IS_ERR(em)) {
5028 		ret = PTR_ERR(em);
5029 		goto out;
5030 	}
5031 
5032 	while (!end) {
5033 		u64 offset_in_extent = 0;
5034 
5035 		/* break if the extent we found is outside the range */
5036 		if (em->start >= max || extent_map_end(em) < off)
5037 			break;
5038 
5039 		/*
5040 		 * get_extent may return an extent that starts before our
5041 		 * requested range.  We have to make sure the ranges
5042 		 * we return to fiemap always move forward and don't
5043 		 * overlap, so adjust the offsets here
5044 		 */
5045 		em_start = max(em->start, off);
5046 
5047 		/*
5048 		 * record the offset from the start of the extent
5049 		 * for adjusting the disk offset below.  Only do this if the
5050 		 * extent isn't compressed since our in ram offset may be past
5051 		 * what we have actually allocated on disk.
5052 		 */
5053 		if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
5054 			offset_in_extent = em_start - em->start;
5055 		em_end = extent_map_end(em);
5056 		em_len = em_end - em_start;
5057 		flags = 0;
5058 		if (em->block_start < EXTENT_MAP_LAST_BYTE)
5059 			disko = em->block_start + offset_in_extent;
5060 		else
5061 			disko = 0;
5062 
5063 		/*
5064 		 * bump off for our next call to get_extent
5065 		 */
5066 		off = extent_map_end(em);
5067 		if (off >= max)
5068 			end = 1;
5069 
5070 		if (em->block_start == EXTENT_MAP_LAST_BYTE) {
5071 			end = 1;
5072 			flags |= FIEMAP_EXTENT_LAST;
5073 		} else if (em->block_start == EXTENT_MAP_INLINE) {
5074 			flags |= (FIEMAP_EXTENT_DATA_INLINE |
5075 				  FIEMAP_EXTENT_NOT_ALIGNED);
5076 		} else if (em->block_start == EXTENT_MAP_DELALLOC) {
5077 			flags |= (FIEMAP_EXTENT_DELALLOC |
5078 				  FIEMAP_EXTENT_UNKNOWN);
5079 		} else if (fieinfo->fi_extents_max) {
5080 			u64 bytenr = em->block_start -
5081 				(em->start - em->orig_start);
5082 
5083 			/*
5084 			 * As btrfs supports shared space, this information
5085 			 * can be exported to userspace tools via
5086 			 * flag FIEMAP_EXTENT_SHARED.  If fi_extents_max == 0
5087 			 * then we're just getting a count and we can skip the
5088 			 * lookup stuff.
5089 			 */
5090 			ret = btrfs_check_shared(root, btrfs_ino(inode),
5091 						 bytenr, roots, tmp_ulist);
5092 			if (ret < 0)
5093 				goto out_free;
5094 			if (ret)
5095 				flags |= FIEMAP_EXTENT_SHARED;
5096 			ret = 0;
5097 		}
5098 		if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
5099 			flags |= FIEMAP_EXTENT_ENCODED;
5100 		if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5101 			flags |= FIEMAP_EXTENT_UNWRITTEN;
5102 
5103 		free_extent_map(em);
5104 		em = NULL;
5105 		if ((em_start >= last) || em_len == (u64)-1 ||
5106 		   (last == (u64)-1 && isize <= em_end)) {
5107 			flags |= FIEMAP_EXTENT_LAST;
5108 			end = 1;
5109 		}
5110 
5111 		/* now scan forward to see if this is really the last extent. */
5112 		em = get_extent_skip_holes(inode, off, last_for_get_extent);
5113 		if (IS_ERR(em)) {
5114 			ret = PTR_ERR(em);
5115 			goto out;
5116 		}
5117 		if (!em) {
5118 			flags |= FIEMAP_EXTENT_LAST;
5119 			end = 1;
5120 		}
5121 		ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
5122 					   em_len, flags);
5123 		if (ret) {
5124 			if (ret == 1)
5125 				ret = 0;
5126 			goto out_free;
5127 		}
5128 	}
5129 out_free:
5130 	if (!ret)
5131 		ret = emit_last_fiemap_cache(fieinfo, &cache);
5132 	free_extent_map(em);
5133 out:
5134 	unlock_extent_cached(&inode->io_tree, start, start + len - 1,
5135 			     &cached_state);
5136 
5137 out_free_ulist:
5138 	btrfs_free_path(path);
5139 	ulist_free(roots);
5140 	ulist_free(tmp_ulist);
5141 	return ret;
5142 }
5143 
5144 static void __free_extent_buffer(struct extent_buffer *eb)
5145 {
5146 	kmem_cache_free(extent_buffer_cache, eb);
5147 }
5148 
5149 int extent_buffer_under_io(const struct extent_buffer *eb)
5150 {
5151 	return (atomic_read(&eb->io_pages) ||
5152 		test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
5153 		test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5154 }
5155 
5156 static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page)
5157 {
5158 	struct btrfs_subpage *subpage;
5159 
5160 	lockdep_assert_held(&page->mapping->private_lock);
5161 
5162 	if (PagePrivate(page)) {
5163 		subpage = (struct btrfs_subpage *)page->private;
5164 		if (atomic_read(&subpage->eb_refs))
5165 			return true;
5166 	}
5167 	return false;
5168 }
5169 
5170 static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
5171 {
5172 	struct btrfs_fs_info *fs_info = eb->fs_info;
5173 	const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
5174 
5175 	/*
5176 	 * For mapped eb, we're going to change the page private, which should
5177 	 * be done under the private_lock.
5178 	 */
5179 	if (mapped)
5180 		spin_lock(&page->mapping->private_lock);
5181 
5182 	if (!PagePrivate(page)) {
5183 		if (mapped)
5184 			spin_unlock(&page->mapping->private_lock);
5185 		return;
5186 	}
5187 
5188 	if (fs_info->sectorsize == PAGE_SIZE) {
5189 		/*
5190 		 * We do this since we'll remove the pages after we've
5191 		 * removed the eb from the radix tree, so we could race
5192 		 * and have this page now attached to the new eb.  So
5193 		 * only clear page_private if it's still connected to
5194 		 * this eb.
5195 		 */
5196 		if (PagePrivate(page) &&
5197 		    page->private == (unsigned long)eb) {
5198 			BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5199 			BUG_ON(PageDirty(page));
5200 			BUG_ON(PageWriteback(page));
5201 			/*
5202 			 * We need to make sure we haven't be attached
5203 			 * to a new eb.
5204 			 */
5205 			detach_page_private(page);
5206 		}
5207 		if (mapped)
5208 			spin_unlock(&page->mapping->private_lock);
5209 		return;
5210 	}
5211 
5212 	/*
5213 	 * For subpage, we can have dummy eb with page private.  In this case,
5214 	 * we can directly detach the private as such page is only attached to
5215 	 * one dummy eb, no sharing.
5216 	 */
5217 	if (!mapped) {
5218 		btrfs_detach_subpage(fs_info, page);
5219 		return;
5220 	}
5221 
5222 	btrfs_page_dec_eb_refs(fs_info, page);
5223 
5224 	/*
5225 	 * We can only detach the page private if there are no other ebs in the
5226 	 * page range.
5227 	 */
5228 	if (!page_range_has_eb(fs_info, page))
5229 		btrfs_detach_subpage(fs_info, page);
5230 
5231 	spin_unlock(&page->mapping->private_lock);
5232 }
5233 
5234 /* Release all pages attached to the extent buffer */
5235 static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
5236 {
5237 	int i;
5238 	int num_pages;
5239 
5240 	ASSERT(!extent_buffer_under_io(eb));
5241 
5242 	num_pages = num_extent_pages(eb);
5243 	for (i = 0; i < num_pages; i++) {
5244 		struct page *page = eb->pages[i];
5245 
5246 		if (!page)
5247 			continue;
5248 
5249 		detach_extent_buffer_page(eb, page);
5250 
5251 		/* One for when we allocated the page */
5252 		put_page(page);
5253 	}
5254 }
5255 
5256 /*
5257  * Helper for releasing the extent buffer.
5258  */
5259 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
5260 {
5261 	btrfs_release_extent_buffer_pages(eb);
5262 	btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
5263 	__free_extent_buffer(eb);
5264 }
5265 
5266 static struct extent_buffer *
5267 __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
5268 		      unsigned long len)
5269 {
5270 	struct extent_buffer *eb = NULL;
5271 
5272 	eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
5273 	eb->start = start;
5274 	eb->len = len;
5275 	eb->fs_info = fs_info;
5276 	eb->bflags = 0;
5277 	init_rwsem(&eb->lock);
5278 
5279 	btrfs_leak_debug_add(&fs_info->eb_leak_lock, &eb->leak_list,
5280 			     &fs_info->allocated_ebs);
5281 	INIT_LIST_HEAD(&eb->release_list);
5282 
5283 	spin_lock_init(&eb->refs_lock);
5284 	atomic_set(&eb->refs, 1);
5285 	atomic_set(&eb->io_pages, 0);
5286 
5287 	ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE);
5288 
5289 	return eb;
5290 }
5291 
5292 struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
5293 {
5294 	int i;
5295 	struct page *p;
5296 	struct extent_buffer *new;
5297 	int num_pages = num_extent_pages(src);
5298 
5299 	new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
5300 	if (new == NULL)
5301 		return NULL;
5302 
5303 	/*
5304 	 * Set UNMAPPED before calling btrfs_release_extent_buffer(), as
5305 	 * btrfs_release_extent_buffer() have different behavior for
5306 	 * UNMAPPED subpage extent buffer.
5307 	 */
5308 	set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
5309 
5310 	for (i = 0; i < num_pages; i++) {
5311 		int ret;
5312 
5313 		p = alloc_page(GFP_NOFS);
5314 		if (!p) {
5315 			btrfs_release_extent_buffer(new);
5316 			return NULL;
5317 		}
5318 		ret = attach_extent_buffer_page(new, p, NULL);
5319 		if (ret < 0) {
5320 			put_page(p);
5321 			btrfs_release_extent_buffer(new);
5322 			return NULL;
5323 		}
5324 		WARN_ON(PageDirty(p));
5325 		new->pages[i] = p;
5326 		copy_page(page_address(p), page_address(src->pages[i]));
5327 	}
5328 	set_extent_buffer_uptodate(new);
5329 
5330 	return new;
5331 }
5332 
5333 struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
5334 						  u64 start, unsigned long len)
5335 {
5336 	struct extent_buffer *eb;
5337 	int num_pages;
5338 	int i;
5339 
5340 	eb = __alloc_extent_buffer(fs_info, start, len);
5341 	if (!eb)
5342 		return NULL;
5343 
5344 	num_pages = num_extent_pages(eb);
5345 	for (i = 0; i < num_pages; i++) {
5346 		int ret;
5347 
5348 		eb->pages[i] = alloc_page(GFP_NOFS);
5349 		if (!eb->pages[i])
5350 			goto err;
5351 		ret = attach_extent_buffer_page(eb, eb->pages[i], NULL);
5352 		if (ret < 0)
5353 			goto err;
5354 	}
5355 	set_extent_buffer_uptodate(eb);
5356 	btrfs_set_header_nritems(eb, 0);
5357 	set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
5358 
5359 	return eb;
5360 err:
5361 	for (; i > 0; i--) {
5362 		detach_extent_buffer_page(eb, eb->pages[i - 1]);
5363 		__free_page(eb->pages[i - 1]);
5364 	}
5365 	__free_extent_buffer(eb);
5366 	return NULL;
5367 }
5368 
5369 struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
5370 						u64 start)
5371 {
5372 	return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
5373 }
5374 
5375 static void check_buffer_tree_ref(struct extent_buffer *eb)
5376 {
5377 	int refs;
5378 	/*
5379 	 * The TREE_REF bit is first set when the extent_buffer is added
5380 	 * to the radix tree. It is also reset, if unset, when a new reference
5381 	 * is created by find_extent_buffer.
5382 	 *
5383 	 * It is only cleared in two cases: freeing the last non-tree
5384 	 * reference to the extent_buffer when its STALE bit is set or
5385 	 * calling releasepage when the tree reference is the only reference.
5386 	 *
5387 	 * In both cases, care is taken to ensure that the extent_buffer's
5388 	 * pages are not under io. However, releasepage can be concurrently
5389 	 * called with creating new references, which is prone to race
5390 	 * conditions between the calls to check_buffer_tree_ref in those
5391 	 * codepaths and clearing TREE_REF in try_release_extent_buffer.
5392 	 *
5393 	 * The actual lifetime of the extent_buffer in the radix tree is
5394 	 * adequately protected by the refcount, but the TREE_REF bit and
5395 	 * its corresponding reference are not. To protect against this
5396 	 * class of races, we call check_buffer_tree_ref from the codepaths
5397 	 * which trigger io after they set eb->io_pages. Note that once io is
5398 	 * initiated, TREE_REF can no longer be cleared, so that is the
5399 	 * moment at which any such race is best fixed.
5400 	 */
5401 	refs = atomic_read(&eb->refs);
5402 	if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5403 		return;
5404 
5405 	spin_lock(&eb->refs_lock);
5406 	if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5407 		atomic_inc(&eb->refs);
5408 	spin_unlock(&eb->refs_lock);
5409 }
5410 
5411 static void mark_extent_buffer_accessed(struct extent_buffer *eb,
5412 		struct page *accessed)
5413 {
5414 	int num_pages, i;
5415 
5416 	check_buffer_tree_ref(eb);
5417 
5418 	num_pages = num_extent_pages(eb);
5419 	for (i = 0; i < num_pages; i++) {
5420 		struct page *p = eb->pages[i];
5421 
5422 		if (p != accessed)
5423 			mark_page_accessed(p);
5424 	}
5425 }
5426 
5427 struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
5428 					 u64 start)
5429 {
5430 	struct extent_buffer *eb;
5431 
5432 	rcu_read_lock();
5433 	eb = radix_tree_lookup(&fs_info->buffer_radix,
5434 			       start >> fs_info->sectorsize_bits);
5435 	if (eb && atomic_inc_not_zero(&eb->refs)) {
5436 		rcu_read_unlock();
5437 		/*
5438 		 * Lock our eb's refs_lock to avoid races with
5439 		 * free_extent_buffer. When we get our eb it might be flagged
5440 		 * with EXTENT_BUFFER_STALE and another task running
5441 		 * free_extent_buffer might have seen that flag set,
5442 		 * eb->refs == 2, that the buffer isn't under IO (dirty and
5443 		 * writeback flags not set) and it's still in the tree (flag
5444 		 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
5445 		 * of decrementing the extent buffer's reference count twice.
5446 		 * So here we could race and increment the eb's reference count,
5447 		 * clear its stale flag, mark it as dirty and drop our reference
5448 		 * before the other task finishes executing free_extent_buffer,
5449 		 * which would later result in an attempt to free an extent
5450 		 * buffer that is dirty.
5451 		 */
5452 		if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
5453 			spin_lock(&eb->refs_lock);
5454 			spin_unlock(&eb->refs_lock);
5455 		}
5456 		mark_extent_buffer_accessed(eb, NULL);
5457 		return eb;
5458 	}
5459 	rcu_read_unlock();
5460 
5461 	return NULL;
5462 }
5463 
5464 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5465 struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
5466 					u64 start)
5467 {
5468 	struct extent_buffer *eb, *exists = NULL;
5469 	int ret;
5470 
5471 	eb = find_extent_buffer(fs_info, start);
5472 	if (eb)
5473 		return eb;
5474 	eb = alloc_dummy_extent_buffer(fs_info, start);
5475 	if (!eb)
5476 		return ERR_PTR(-ENOMEM);
5477 	eb->fs_info = fs_info;
5478 again:
5479 	ret = radix_tree_preload(GFP_NOFS);
5480 	if (ret) {
5481 		exists = ERR_PTR(ret);
5482 		goto free_eb;
5483 	}
5484 	spin_lock(&fs_info->buffer_lock);
5485 	ret = radix_tree_insert(&fs_info->buffer_radix,
5486 				start >> fs_info->sectorsize_bits, eb);
5487 	spin_unlock(&fs_info->buffer_lock);
5488 	radix_tree_preload_end();
5489 	if (ret == -EEXIST) {
5490 		exists = find_extent_buffer(fs_info, start);
5491 		if (exists)
5492 			goto free_eb;
5493 		else
5494 			goto again;
5495 	}
5496 	check_buffer_tree_ref(eb);
5497 	set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
5498 
5499 	return eb;
5500 free_eb:
5501 	btrfs_release_extent_buffer(eb);
5502 	return exists;
5503 }
5504 #endif
5505 
5506 static struct extent_buffer *grab_extent_buffer(
5507 		struct btrfs_fs_info *fs_info, struct page *page)
5508 {
5509 	struct extent_buffer *exists;
5510 
5511 	/*
5512 	 * For subpage case, we completely rely on radix tree to ensure we
5513 	 * don't try to insert two ebs for the same bytenr.  So here we always
5514 	 * return NULL and just continue.
5515 	 */
5516 	if (fs_info->sectorsize < PAGE_SIZE)
5517 		return NULL;
5518 
5519 	/* Page not yet attached to an extent buffer */
5520 	if (!PagePrivate(page))
5521 		return NULL;
5522 
5523 	/*
5524 	 * We could have already allocated an eb for this page and attached one
5525 	 * so lets see if we can get a ref on the existing eb, and if we can we
5526 	 * know it's good and we can just return that one, else we know we can
5527 	 * just overwrite page->private.
5528 	 */
5529 	exists = (struct extent_buffer *)page->private;
5530 	if (atomic_inc_not_zero(&exists->refs))
5531 		return exists;
5532 
5533 	WARN_ON(PageDirty(page));
5534 	detach_page_private(page);
5535 	return NULL;
5536 }
5537 
5538 struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
5539 					  u64 start, u64 owner_root, int level)
5540 {
5541 	unsigned long len = fs_info->nodesize;
5542 	int num_pages;
5543 	int i;
5544 	unsigned long index = start >> PAGE_SHIFT;
5545 	struct extent_buffer *eb;
5546 	struct extent_buffer *exists = NULL;
5547 	struct page *p;
5548 	struct address_space *mapping = fs_info->btree_inode->i_mapping;
5549 	int uptodate = 1;
5550 	int ret;
5551 
5552 	if (!IS_ALIGNED(start, fs_info->sectorsize)) {
5553 		btrfs_err(fs_info, "bad tree block start %llu", start);
5554 		return ERR_PTR(-EINVAL);
5555 	}
5556 
5557 	if (fs_info->sectorsize < PAGE_SIZE &&
5558 	    offset_in_page(start) + len > PAGE_SIZE) {
5559 		btrfs_err(fs_info,
5560 		"tree block crosses page boundary, start %llu nodesize %lu",
5561 			  start, len);
5562 		return ERR_PTR(-EINVAL);
5563 	}
5564 
5565 	eb = find_extent_buffer(fs_info, start);
5566 	if (eb)
5567 		return eb;
5568 
5569 	eb = __alloc_extent_buffer(fs_info, start, len);
5570 	if (!eb)
5571 		return ERR_PTR(-ENOMEM);
5572 	btrfs_set_buffer_lockdep_class(owner_root, eb, level);
5573 
5574 	num_pages = num_extent_pages(eb);
5575 	for (i = 0; i < num_pages; i++, index++) {
5576 		struct btrfs_subpage *prealloc = NULL;
5577 
5578 		p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
5579 		if (!p) {
5580 			exists = ERR_PTR(-ENOMEM);
5581 			goto free_eb;
5582 		}
5583 
5584 		/*
5585 		 * Preallocate page->private for subpage case, so that we won't
5586 		 * allocate memory with private_lock hold.  The memory will be
5587 		 * freed by attach_extent_buffer_page() or freed manually if
5588 		 * we exit earlier.
5589 		 *
5590 		 * Although we have ensured one subpage eb can only have one
5591 		 * page, but it may change in the future for 16K page size
5592 		 * support, so we still preallocate the memory in the loop.
5593 		 */
5594 		ret = btrfs_alloc_subpage(fs_info, &prealloc,
5595 					  BTRFS_SUBPAGE_METADATA);
5596 		if (ret < 0) {
5597 			unlock_page(p);
5598 			put_page(p);
5599 			exists = ERR_PTR(ret);
5600 			goto free_eb;
5601 		}
5602 
5603 		spin_lock(&mapping->private_lock);
5604 		exists = grab_extent_buffer(fs_info, p);
5605 		if (exists) {
5606 			spin_unlock(&mapping->private_lock);
5607 			unlock_page(p);
5608 			put_page(p);
5609 			mark_extent_buffer_accessed(exists, p);
5610 			btrfs_free_subpage(prealloc);
5611 			goto free_eb;
5612 		}
5613 		/* Should not fail, as we have preallocated the memory */
5614 		ret = attach_extent_buffer_page(eb, p, prealloc);
5615 		ASSERT(!ret);
5616 		/*
5617 		 * To inform we have extra eb under allocation, so that
5618 		 * detach_extent_buffer_page() won't release the page private
5619 		 * when the eb hasn't yet been inserted into radix tree.
5620 		 *
5621 		 * The ref will be decreased when the eb released the page, in
5622 		 * detach_extent_buffer_page().
5623 		 * Thus needs no special handling in error path.
5624 		 */
5625 		btrfs_page_inc_eb_refs(fs_info, p);
5626 		spin_unlock(&mapping->private_lock);
5627 
5628 		WARN_ON(PageDirty(p));
5629 		eb->pages[i] = p;
5630 		if (!PageUptodate(p))
5631 			uptodate = 0;
5632 
5633 		/*
5634 		 * We can't unlock the pages just yet since the extent buffer
5635 		 * hasn't been properly inserted in the radix tree, this
5636 		 * opens a race with btree_releasepage which can free a page
5637 		 * while we are still filling in all pages for the buffer and
5638 		 * we could crash.
5639 		 */
5640 	}
5641 	if (uptodate)
5642 		set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
5643 again:
5644 	ret = radix_tree_preload(GFP_NOFS);
5645 	if (ret) {
5646 		exists = ERR_PTR(ret);
5647 		goto free_eb;
5648 	}
5649 
5650 	spin_lock(&fs_info->buffer_lock);
5651 	ret = radix_tree_insert(&fs_info->buffer_radix,
5652 				start >> fs_info->sectorsize_bits, eb);
5653 	spin_unlock(&fs_info->buffer_lock);
5654 	radix_tree_preload_end();
5655 	if (ret == -EEXIST) {
5656 		exists = find_extent_buffer(fs_info, start);
5657 		if (exists)
5658 			goto free_eb;
5659 		else
5660 			goto again;
5661 	}
5662 	/* add one reference for the tree */
5663 	check_buffer_tree_ref(eb);
5664 	set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
5665 
5666 	/*
5667 	 * Now it's safe to unlock the pages because any calls to
5668 	 * btree_releasepage will correctly detect that a page belongs to a
5669 	 * live buffer and won't free them prematurely.
5670 	 */
5671 	for (i = 0; i < num_pages; i++)
5672 		unlock_page(eb->pages[i]);
5673 	return eb;
5674 
5675 free_eb:
5676 	WARN_ON(!atomic_dec_and_test(&eb->refs));
5677 	for (i = 0; i < num_pages; i++) {
5678 		if (eb->pages[i])
5679 			unlock_page(eb->pages[i]);
5680 	}
5681 
5682 	btrfs_release_extent_buffer(eb);
5683 	return exists;
5684 }
5685 
5686 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5687 {
5688 	struct extent_buffer *eb =
5689 			container_of(head, struct extent_buffer, rcu_head);
5690 
5691 	__free_extent_buffer(eb);
5692 }
5693 
5694 static int release_extent_buffer(struct extent_buffer *eb)
5695 	__releases(&eb->refs_lock)
5696 {
5697 	lockdep_assert_held(&eb->refs_lock);
5698 
5699 	WARN_ON(atomic_read(&eb->refs) == 0);
5700 	if (atomic_dec_and_test(&eb->refs)) {
5701 		if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
5702 			struct btrfs_fs_info *fs_info = eb->fs_info;
5703 
5704 			spin_unlock(&eb->refs_lock);
5705 
5706 			spin_lock(&fs_info->buffer_lock);
5707 			radix_tree_delete(&fs_info->buffer_radix,
5708 					  eb->start >> fs_info->sectorsize_bits);
5709 			spin_unlock(&fs_info->buffer_lock);
5710 		} else {
5711 			spin_unlock(&eb->refs_lock);
5712 		}
5713 
5714 		btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
5715 		/* Should be safe to release our pages at this point */
5716 		btrfs_release_extent_buffer_pages(eb);
5717 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5718 		if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
5719 			__free_extent_buffer(eb);
5720 			return 1;
5721 		}
5722 #endif
5723 		call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
5724 		return 1;
5725 	}
5726 	spin_unlock(&eb->refs_lock);
5727 
5728 	return 0;
5729 }
5730 
5731 void free_extent_buffer(struct extent_buffer *eb)
5732 {
5733 	int refs;
5734 	int old;
5735 	if (!eb)
5736 		return;
5737 
5738 	while (1) {
5739 		refs = atomic_read(&eb->refs);
5740 		if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5741 		    || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5742 			refs == 1))
5743 			break;
5744 		old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5745 		if (old == refs)
5746 			return;
5747 	}
5748 
5749 	spin_lock(&eb->refs_lock);
5750 	if (atomic_read(&eb->refs) == 2 &&
5751 	    test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
5752 	    !extent_buffer_under_io(eb) &&
5753 	    test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5754 		atomic_dec(&eb->refs);
5755 
5756 	/*
5757 	 * I know this is terrible, but it's temporary until we stop tracking
5758 	 * the uptodate bits and such for the extent buffers.
5759 	 */
5760 	release_extent_buffer(eb);
5761 }
5762 
5763 void free_extent_buffer_stale(struct extent_buffer *eb)
5764 {
5765 	if (!eb)
5766 		return;
5767 
5768 	spin_lock(&eb->refs_lock);
5769 	set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5770 
5771 	if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
5772 	    test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5773 		atomic_dec(&eb->refs);
5774 	release_extent_buffer(eb);
5775 }
5776 
5777 void clear_extent_buffer_dirty(const struct extent_buffer *eb)
5778 {
5779 	int i;
5780 	int num_pages;
5781 	struct page *page;
5782 
5783 	num_pages = num_extent_pages(eb);
5784 
5785 	for (i = 0; i < num_pages; i++) {
5786 		page = eb->pages[i];
5787 		if (!PageDirty(page))
5788 			continue;
5789 
5790 		lock_page(page);
5791 		WARN_ON(!PagePrivate(page));
5792 
5793 		clear_page_dirty_for_io(page);
5794 		xa_lock_irq(&page->mapping->i_pages);
5795 		if (!PageDirty(page))
5796 			__xa_clear_mark(&page->mapping->i_pages,
5797 					page_index(page), PAGECACHE_TAG_DIRTY);
5798 		xa_unlock_irq(&page->mapping->i_pages);
5799 		ClearPageError(page);
5800 		unlock_page(page);
5801 	}
5802 	WARN_ON(atomic_read(&eb->refs) == 0);
5803 }
5804 
5805 bool set_extent_buffer_dirty(struct extent_buffer *eb)
5806 {
5807 	int i;
5808 	int num_pages;
5809 	bool was_dirty;
5810 
5811 	check_buffer_tree_ref(eb);
5812 
5813 	was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
5814 
5815 	num_pages = num_extent_pages(eb);
5816 	WARN_ON(atomic_read(&eb->refs) == 0);
5817 	WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5818 
5819 	if (!was_dirty)
5820 		for (i = 0; i < num_pages; i++)
5821 			set_page_dirty(eb->pages[i]);
5822 
5823 #ifdef CONFIG_BTRFS_DEBUG
5824 	for (i = 0; i < num_pages; i++)
5825 		ASSERT(PageDirty(eb->pages[i]));
5826 #endif
5827 
5828 	return was_dirty;
5829 }
5830 
5831 void clear_extent_buffer_uptodate(struct extent_buffer *eb)
5832 {
5833 	struct btrfs_fs_info *fs_info = eb->fs_info;
5834 	struct page *page;
5835 	int num_pages;
5836 	int i;
5837 
5838 	clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
5839 	num_pages = num_extent_pages(eb);
5840 	for (i = 0; i < num_pages; i++) {
5841 		page = eb->pages[i];
5842 		if (page)
5843 			btrfs_page_clear_uptodate(fs_info, page,
5844 						  eb->start, eb->len);
5845 	}
5846 }
5847 
5848 void set_extent_buffer_uptodate(struct extent_buffer *eb)
5849 {
5850 	struct btrfs_fs_info *fs_info = eb->fs_info;
5851 	struct page *page;
5852 	int num_pages;
5853 	int i;
5854 
5855 	set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
5856 	num_pages = num_extent_pages(eb);
5857 	for (i = 0; i < num_pages; i++) {
5858 		page = eb->pages[i];
5859 		btrfs_page_set_uptodate(fs_info, page, eb->start, eb->len);
5860 	}
5861 }
5862 
5863 static int read_extent_buffer_subpage(struct extent_buffer *eb, int wait,
5864 				      int mirror_num)
5865 {
5866 	struct btrfs_fs_info *fs_info = eb->fs_info;
5867 	struct extent_io_tree *io_tree;
5868 	struct page *page = eb->pages[0];
5869 	struct bio *bio = NULL;
5870 	int ret = 0;
5871 
5872 	ASSERT(!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags));
5873 	ASSERT(PagePrivate(page));
5874 	io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
5875 
5876 	if (wait == WAIT_NONE) {
5877 		ret = try_lock_extent(io_tree, eb->start,
5878 				      eb->start + eb->len - 1);
5879 		if (ret <= 0)
5880 			return ret;
5881 	} else {
5882 		ret = lock_extent(io_tree, eb->start, eb->start + eb->len - 1);
5883 		if (ret < 0)
5884 			return ret;
5885 	}
5886 
5887 	ret = 0;
5888 	if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags) ||
5889 	    PageUptodate(page) ||
5890 	    btrfs_subpage_test_uptodate(fs_info, page, eb->start, eb->len)) {
5891 		set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
5892 		unlock_extent(io_tree, eb->start, eb->start + eb->len - 1);
5893 		return ret;
5894 	}
5895 
5896 	clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
5897 	eb->read_mirror = 0;
5898 	atomic_set(&eb->io_pages, 1);
5899 	check_buffer_tree_ref(eb);
5900 	btrfs_subpage_clear_error(fs_info, page, eb->start, eb->len);
5901 
5902 	ret = submit_extent_page(REQ_OP_READ | REQ_META, NULL, page, eb->start,
5903 				 eb->len, eb->start - page_offset(page), &bio,
5904 				 end_bio_extent_readpage, mirror_num, 0, 0,
5905 				 true);
5906 	if (ret) {
5907 		/*
5908 		 * In the endio function, if we hit something wrong we will
5909 		 * increase the io_pages, so here we need to decrease it for
5910 		 * error path.
5911 		 */
5912 		atomic_dec(&eb->io_pages);
5913 	}
5914 	if (bio) {
5915 		int tmp;
5916 
5917 		tmp = submit_one_bio(bio, mirror_num, 0);
5918 		if (tmp < 0)
5919 			return tmp;
5920 	}
5921 	if (ret || wait != WAIT_COMPLETE)
5922 		return ret;
5923 
5924 	wait_extent_bit(io_tree, eb->start, eb->start + eb->len - 1, EXTENT_LOCKED);
5925 	if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
5926 		ret = -EIO;
5927 	return ret;
5928 }
5929 
5930 int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
5931 {
5932 	int i;
5933 	struct page *page;
5934 	int err;
5935 	int ret = 0;
5936 	int locked_pages = 0;
5937 	int all_uptodate = 1;
5938 	int num_pages;
5939 	unsigned long num_reads = 0;
5940 	struct bio *bio = NULL;
5941 	unsigned long bio_flags = 0;
5942 
5943 	if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
5944 		return 0;
5945 
5946 	if (eb->fs_info->sectorsize < PAGE_SIZE)
5947 		return read_extent_buffer_subpage(eb, wait, mirror_num);
5948 
5949 	num_pages = num_extent_pages(eb);
5950 	for (i = 0; i < num_pages; i++) {
5951 		page = eb->pages[i];
5952 		if (wait == WAIT_NONE) {
5953 			/*
5954 			 * WAIT_NONE is only utilized by readahead. If we can't
5955 			 * acquire the lock atomically it means either the eb
5956 			 * is being read out or under modification.
5957 			 * Either way the eb will be or has been cached,
5958 			 * readahead can exit safely.
5959 			 */
5960 			if (!trylock_page(page))
5961 				goto unlock_exit;
5962 		} else {
5963 			lock_page(page);
5964 		}
5965 		locked_pages++;
5966 	}
5967 	/*
5968 	 * We need to firstly lock all pages to make sure that
5969 	 * the uptodate bit of our pages won't be affected by
5970 	 * clear_extent_buffer_uptodate().
5971 	 */
5972 	for (i = 0; i < num_pages; i++) {
5973 		page = eb->pages[i];
5974 		if (!PageUptodate(page)) {
5975 			num_reads++;
5976 			all_uptodate = 0;
5977 		}
5978 	}
5979 
5980 	if (all_uptodate) {
5981 		set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
5982 		goto unlock_exit;
5983 	}
5984 
5985 	clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
5986 	eb->read_mirror = 0;
5987 	atomic_set(&eb->io_pages, num_reads);
5988 	/*
5989 	 * It is possible for releasepage to clear the TREE_REF bit before we
5990 	 * set io_pages. See check_buffer_tree_ref for a more detailed comment.
5991 	 */
5992 	check_buffer_tree_ref(eb);
5993 	for (i = 0; i < num_pages; i++) {
5994 		page = eb->pages[i];
5995 
5996 		if (!PageUptodate(page)) {
5997 			if (ret) {
5998 				atomic_dec(&eb->io_pages);
5999 				unlock_page(page);
6000 				continue;
6001 			}
6002 
6003 			ClearPageError(page);
6004 			err = submit_extent_page(REQ_OP_READ | REQ_META, NULL,
6005 					 page, page_offset(page), PAGE_SIZE, 0,
6006 					 &bio, end_bio_extent_readpage,
6007 					 mirror_num, 0, 0, false);
6008 			if (err) {
6009 				/*
6010 				 * We failed to submit the bio so it's the
6011 				 * caller's responsibility to perform cleanup
6012 				 * i.e unlock page/set error bit.
6013 				 */
6014 				ret = err;
6015 				SetPageError(page);
6016 				unlock_page(page);
6017 				atomic_dec(&eb->io_pages);
6018 			}
6019 		} else {
6020 			unlock_page(page);
6021 		}
6022 	}
6023 
6024 	if (bio) {
6025 		err = submit_one_bio(bio, mirror_num, bio_flags);
6026 		if (err)
6027 			return err;
6028 	}
6029 
6030 	if (ret || wait != WAIT_COMPLETE)
6031 		return ret;
6032 
6033 	for (i = 0; i < num_pages; i++) {
6034 		page = eb->pages[i];
6035 		wait_on_page_locked(page);
6036 		if (!PageUptodate(page))
6037 			ret = -EIO;
6038 	}
6039 
6040 	return ret;
6041 
6042 unlock_exit:
6043 	while (locked_pages > 0) {
6044 		locked_pages--;
6045 		page = eb->pages[locked_pages];
6046 		unlock_page(page);
6047 	}
6048 	return ret;
6049 }
6050 
6051 static bool report_eb_range(const struct extent_buffer *eb, unsigned long start,
6052 			    unsigned long len)
6053 {
6054 	btrfs_warn(eb->fs_info,
6055 		"access to eb bytenr %llu len %lu out of range start %lu len %lu",
6056 		eb->start, eb->len, start, len);
6057 	WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
6058 
6059 	return true;
6060 }
6061 
6062 /*
6063  * Check if the [start, start + len) range is valid before reading/writing
6064  * the eb.
6065  * NOTE: @start and @len are offset inside the eb, not logical address.
6066  *
6067  * Caller should not touch the dst/src memory if this function returns error.
6068  */
6069 static inline int check_eb_range(const struct extent_buffer *eb,
6070 				 unsigned long start, unsigned long len)
6071 {
6072 	unsigned long offset;
6073 
6074 	/* start, start + len should not go beyond eb->len nor overflow */
6075 	if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len))
6076 		return report_eb_range(eb, start, len);
6077 
6078 	return false;
6079 }
6080 
6081 void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
6082 			unsigned long start, unsigned long len)
6083 {
6084 	size_t cur;
6085 	size_t offset;
6086 	struct page *page;
6087 	char *kaddr;
6088 	char *dst = (char *)dstv;
6089 	unsigned long i = get_eb_page_index(start);
6090 
6091 	if (check_eb_range(eb, start, len))
6092 		return;
6093 
6094 	offset = get_eb_offset_in_page(eb, start);
6095 
6096 	while (len > 0) {
6097 		page = eb->pages[i];
6098 
6099 		cur = min(len, (PAGE_SIZE - offset));
6100 		kaddr = page_address(page);
6101 		memcpy(dst, kaddr + offset, cur);
6102 
6103 		dst += cur;
6104 		len -= cur;
6105 		offset = 0;
6106 		i++;
6107 	}
6108 }
6109 
6110 int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
6111 				       void __user *dstv,
6112 				       unsigned long start, unsigned long len)
6113 {
6114 	size_t cur;
6115 	size_t offset;
6116 	struct page *page;
6117 	char *kaddr;
6118 	char __user *dst = (char __user *)dstv;
6119 	unsigned long i = get_eb_page_index(start);
6120 	int ret = 0;
6121 
6122 	WARN_ON(start > eb->len);
6123 	WARN_ON(start + len > eb->start + eb->len);
6124 
6125 	offset = get_eb_offset_in_page(eb, start);
6126 
6127 	while (len > 0) {
6128 		page = eb->pages[i];
6129 
6130 		cur = min(len, (PAGE_SIZE - offset));
6131 		kaddr = page_address(page);
6132 		if (copy_to_user_nofault(dst, kaddr + offset, cur)) {
6133 			ret = -EFAULT;
6134 			break;
6135 		}
6136 
6137 		dst += cur;
6138 		len -= cur;
6139 		offset = 0;
6140 		i++;
6141 	}
6142 
6143 	return ret;
6144 }
6145 
6146 int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
6147 			 unsigned long start, unsigned long len)
6148 {
6149 	size_t cur;
6150 	size_t offset;
6151 	struct page *page;
6152 	char *kaddr;
6153 	char *ptr = (char *)ptrv;
6154 	unsigned long i = get_eb_page_index(start);
6155 	int ret = 0;
6156 
6157 	if (check_eb_range(eb, start, len))
6158 		return -EINVAL;
6159 
6160 	offset = get_eb_offset_in_page(eb, start);
6161 
6162 	while (len > 0) {
6163 		page = eb->pages[i];
6164 
6165 		cur = min(len, (PAGE_SIZE - offset));
6166 
6167 		kaddr = page_address(page);
6168 		ret = memcmp(ptr, kaddr + offset, cur);
6169 		if (ret)
6170 			break;
6171 
6172 		ptr += cur;
6173 		len -= cur;
6174 		offset = 0;
6175 		i++;
6176 	}
6177 	return ret;
6178 }
6179 
6180 void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb,
6181 		const void *srcv)
6182 {
6183 	char *kaddr;
6184 
6185 	WARN_ON(!PageUptodate(eb->pages[0]));
6186 	kaddr = page_address(eb->pages[0]) + get_eb_offset_in_page(eb, 0);
6187 	memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
6188 			BTRFS_FSID_SIZE);
6189 }
6190 
6191 void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *srcv)
6192 {
6193 	char *kaddr;
6194 
6195 	WARN_ON(!PageUptodate(eb->pages[0]));
6196 	kaddr = page_address(eb->pages[0]) + get_eb_offset_in_page(eb, 0);
6197 	memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
6198 			BTRFS_FSID_SIZE);
6199 }
6200 
6201 void write_extent_buffer(const struct extent_buffer *eb, const void *srcv,
6202 			 unsigned long start, unsigned long len)
6203 {
6204 	size_t cur;
6205 	size_t offset;
6206 	struct page *page;
6207 	char *kaddr;
6208 	char *src = (char *)srcv;
6209 	unsigned long i = get_eb_page_index(start);
6210 
6211 	WARN_ON(test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags));
6212 
6213 	if (check_eb_range(eb, start, len))
6214 		return;
6215 
6216 	offset = get_eb_offset_in_page(eb, start);
6217 
6218 	while (len > 0) {
6219 		page = eb->pages[i];
6220 		WARN_ON(!PageUptodate(page));
6221 
6222 		cur = min(len, PAGE_SIZE - offset);
6223 		kaddr = page_address(page);
6224 		memcpy(kaddr + offset, src, cur);
6225 
6226 		src += cur;
6227 		len -= cur;
6228 		offset = 0;
6229 		i++;
6230 	}
6231 }
6232 
6233 void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
6234 		unsigned long len)
6235 {
6236 	size_t cur;
6237 	size_t offset;
6238 	struct page *page;
6239 	char *kaddr;
6240 	unsigned long i = get_eb_page_index(start);
6241 
6242 	if (check_eb_range(eb, start, len))
6243 		return;
6244 
6245 	offset = get_eb_offset_in_page(eb, start);
6246 
6247 	while (len > 0) {
6248 		page = eb->pages[i];
6249 		WARN_ON(!PageUptodate(page));
6250 
6251 		cur = min(len, PAGE_SIZE - offset);
6252 		kaddr = page_address(page);
6253 		memset(kaddr + offset, 0, cur);
6254 
6255 		len -= cur;
6256 		offset = 0;
6257 		i++;
6258 	}
6259 }
6260 
6261 void copy_extent_buffer_full(const struct extent_buffer *dst,
6262 			     const struct extent_buffer *src)
6263 {
6264 	int i;
6265 	int num_pages;
6266 
6267 	ASSERT(dst->len == src->len);
6268 
6269 	if (dst->fs_info->sectorsize == PAGE_SIZE) {
6270 		num_pages = num_extent_pages(dst);
6271 		for (i = 0; i < num_pages; i++)
6272 			copy_page(page_address(dst->pages[i]),
6273 				  page_address(src->pages[i]));
6274 	} else {
6275 		size_t src_offset = get_eb_offset_in_page(src, 0);
6276 		size_t dst_offset = get_eb_offset_in_page(dst, 0);
6277 
6278 		ASSERT(src->fs_info->sectorsize < PAGE_SIZE);
6279 		memcpy(page_address(dst->pages[0]) + dst_offset,
6280 		       page_address(src->pages[0]) + src_offset,
6281 		       src->len);
6282 	}
6283 }
6284 
6285 void copy_extent_buffer(const struct extent_buffer *dst,
6286 			const struct extent_buffer *src,
6287 			unsigned long dst_offset, unsigned long src_offset,
6288 			unsigned long len)
6289 {
6290 	u64 dst_len = dst->len;
6291 	size_t cur;
6292 	size_t offset;
6293 	struct page *page;
6294 	char *kaddr;
6295 	unsigned long i = get_eb_page_index(dst_offset);
6296 
6297 	if (check_eb_range(dst, dst_offset, len) ||
6298 	    check_eb_range(src, src_offset, len))
6299 		return;
6300 
6301 	WARN_ON(src->len != dst_len);
6302 
6303 	offset = get_eb_offset_in_page(dst, dst_offset);
6304 
6305 	while (len > 0) {
6306 		page = dst->pages[i];
6307 		WARN_ON(!PageUptodate(page));
6308 
6309 		cur = min(len, (unsigned long)(PAGE_SIZE - offset));
6310 
6311 		kaddr = page_address(page);
6312 		read_extent_buffer(src, kaddr + offset, src_offset, cur);
6313 
6314 		src_offset += cur;
6315 		len -= cur;
6316 		offset = 0;
6317 		i++;
6318 	}
6319 }
6320 
6321 /*
6322  * eb_bitmap_offset() - calculate the page and offset of the byte containing the
6323  * given bit number
6324  * @eb: the extent buffer
6325  * @start: offset of the bitmap item in the extent buffer
6326  * @nr: bit number
6327  * @page_index: return index of the page in the extent buffer that contains the
6328  * given bit number
6329  * @page_offset: return offset into the page given by page_index
6330  *
6331  * This helper hides the ugliness of finding the byte in an extent buffer which
6332  * contains a given bit.
6333  */
6334 static inline void eb_bitmap_offset(const struct extent_buffer *eb,
6335 				    unsigned long start, unsigned long nr,
6336 				    unsigned long *page_index,
6337 				    size_t *page_offset)
6338 {
6339 	size_t byte_offset = BIT_BYTE(nr);
6340 	size_t offset;
6341 
6342 	/*
6343 	 * The byte we want is the offset of the extent buffer + the offset of
6344 	 * the bitmap item in the extent buffer + the offset of the byte in the
6345 	 * bitmap item.
6346 	 */
6347 	offset = start + offset_in_page(eb->start) + byte_offset;
6348 
6349 	*page_index = offset >> PAGE_SHIFT;
6350 	*page_offset = offset_in_page(offset);
6351 }
6352 
6353 /**
6354  * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
6355  * @eb: the extent buffer
6356  * @start: offset of the bitmap item in the extent buffer
6357  * @nr: bit number to test
6358  */
6359 int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
6360 			   unsigned long nr)
6361 {
6362 	u8 *kaddr;
6363 	struct page *page;
6364 	unsigned long i;
6365 	size_t offset;
6366 
6367 	eb_bitmap_offset(eb, start, nr, &i, &offset);
6368 	page = eb->pages[i];
6369 	WARN_ON(!PageUptodate(page));
6370 	kaddr = page_address(page);
6371 	return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
6372 }
6373 
6374 /**
6375  * extent_buffer_bitmap_set - set an area of a bitmap
6376  * @eb: the extent buffer
6377  * @start: offset of the bitmap item in the extent buffer
6378  * @pos: bit number of the first bit
6379  * @len: number of bits to set
6380  */
6381 void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
6382 			      unsigned long pos, unsigned long len)
6383 {
6384 	u8 *kaddr;
6385 	struct page *page;
6386 	unsigned long i;
6387 	size_t offset;
6388 	const unsigned int size = pos + len;
6389 	int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
6390 	u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
6391 
6392 	eb_bitmap_offset(eb, start, pos, &i, &offset);
6393 	page = eb->pages[i];
6394 	WARN_ON(!PageUptodate(page));
6395 	kaddr = page_address(page);
6396 
6397 	while (len >= bits_to_set) {
6398 		kaddr[offset] |= mask_to_set;
6399 		len -= bits_to_set;
6400 		bits_to_set = BITS_PER_BYTE;
6401 		mask_to_set = ~0;
6402 		if (++offset >= PAGE_SIZE && len > 0) {
6403 			offset = 0;
6404 			page = eb->pages[++i];
6405 			WARN_ON(!PageUptodate(page));
6406 			kaddr = page_address(page);
6407 		}
6408 	}
6409 	if (len) {
6410 		mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
6411 		kaddr[offset] |= mask_to_set;
6412 	}
6413 }
6414 
6415 
6416 /**
6417  * extent_buffer_bitmap_clear - clear an area of a bitmap
6418  * @eb: the extent buffer
6419  * @start: offset of the bitmap item in the extent buffer
6420  * @pos: bit number of the first bit
6421  * @len: number of bits to clear
6422  */
6423 void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
6424 				unsigned long start, unsigned long pos,
6425 				unsigned long len)
6426 {
6427 	u8 *kaddr;
6428 	struct page *page;
6429 	unsigned long i;
6430 	size_t offset;
6431 	const unsigned int size = pos + len;
6432 	int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
6433 	u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
6434 
6435 	eb_bitmap_offset(eb, start, pos, &i, &offset);
6436 	page = eb->pages[i];
6437 	WARN_ON(!PageUptodate(page));
6438 	kaddr = page_address(page);
6439 
6440 	while (len >= bits_to_clear) {
6441 		kaddr[offset] &= ~mask_to_clear;
6442 		len -= bits_to_clear;
6443 		bits_to_clear = BITS_PER_BYTE;
6444 		mask_to_clear = ~0;
6445 		if (++offset >= PAGE_SIZE && len > 0) {
6446 			offset = 0;
6447 			page = eb->pages[++i];
6448 			WARN_ON(!PageUptodate(page));
6449 			kaddr = page_address(page);
6450 		}
6451 	}
6452 	if (len) {
6453 		mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
6454 		kaddr[offset] &= ~mask_to_clear;
6455 	}
6456 }
6457 
6458 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
6459 {
6460 	unsigned long distance = (src > dst) ? src - dst : dst - src;
6461 	return distance < len;
6462 }
6463 
6464 static void copy_pages(struct page *dst_page, struct page *src_page,
6465 		       unsigned long dst_off, unsigned long src_off,
6466 		       unsigned long len)
6467 {
6468 	char *dst_kaddr = page_address(dst_page);
6469 	char *src_kaddr;
6470 	int must_memmove = 0;
6471 
6472 	if (dst_page != src_page) {
6473 		src_kaddr = page_address(src_page);
6474 	} else {
6475 		src_kaddr = dst_kaddr;
6476 		if (areas_overlap(src_off, dst_off, len))
6477 			must_memmove = 1;
6478 	}
6479 
6480 	if (must_memmove)
6481 		memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
6482 	else
6483 		memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
6484 }
6485 
6486 void memcpy_extent_buffer(const struct extent_buffer *dst,
6487 			  unsigned long dst_offset, unsigned long src_offset,
6488 			  unsigned long len)
6489 {
6490 	size_t cur;
6491 	size_t dst_off_in_page;
6492 	size_t src_off_in_page;
6493 	unsigned long dst_i;
6494 	unsigned long src_i;
6495 
6496 	if (check_eb_range(dst, dst_offset, len) ||
6497 	    check_eb_range(dst, src_offset, len))
6498 		return;
6499 
6500 	while (len > 0) {
6501 		dst_off_in_page = get_eb_offset_in_page(dst, dst_offset);
6502 		src_off_in_page = get_eb_offset_in_page(dst, src_offset);
6503 
6504 		dst_i = get_eb_page_index(dst_offset);
6505 		src_i = get_eb_page_index(src_offset);
6506 
6507 		cur = min(len, (unsigned long)(PAGE_SIZE -
6508 					       src_off_in_page));
6509 		cur = min_t(unsigned long, cur,
6510 			(unsigned long)(PAGE_SIZE - dst_off_in_page));
6511 
6512 		copy_pages(dst->pages[dst_i], dst->pages[src_i],
6513 			   dst_off_in_page, src_off_in_page, cur);
6514 
6515 		src_offset += cur;
6516 		dst_offset += cur;
6517 		len -= cur;
6518 	}
6519 }
6520 
6521 void memmove_extent_buffer(const struct extent_buffer *dst,
6522 			   unsigned long dst_offset, unsigned long src_offset,
6523 			   unsigned long len)
6524 {
6525 	size_t cur;
6526 	size_t dst_off_in_page;
6527 	size_t src_off_in_page;
6528 	unsigned long dst_end = dst_offset + len - 1;
6529 	unsigned long src_end = src_offset + len - 1;
6530 	unsigned long dst_i;
6531 	unsigned long src_i;
6532 
6533 	if (check_eb_range(dst, dst_offset, len) ||
6534 	    check_eb_range(dst, src_offset, len))
6535 		return;
6536 	if (dst_offset < src_offset) {
6537 		memcpy_extent_buffer(dst, dst_offset, src_offset, len);
6538 		return;
6539 	}
6540 	while (len > 0) {
6541 		dst_i = get_eb_page_index(dst_end);
6542 		src_i = get_eb_page_index(src_end);
6543 
6544 		dst_off_in_page = get_eb_offset_in_page(dst, dst_end);
6545 		src_off_in_page = get_eb_offset_in_page(dst, src_end);
6546 
6547 		cur = min_t(unsigned long, len, src_off_in_page + 1);
6548 		cur = min(cur, dst_off_in_page + 1);
6549 		copy_pages(dst->pages[dst_i], dst->pages[src_i],
6550 			   dst_off_in_page - cur + 1,
6551 			   src_off_in_page - cur + 1, cur);
6552 
6553 		dst_end -= cur;
6554 		src_end -= cur;
6555 		len -= cur;
6556 	}
6557 }
6558 
6559 static struct extent_buffer *get_next_extent_buffer(
6560 		struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
6561 {
6562 	struct extent_buffer *gang[BTRFS_SUBPAGE_BITMAP_SIZE];
6563 	struct extent_buffer *found = NULL;
6564 	u64 page_start = page_offset(page);
6565 	int ret;
6566 	int i;
6567 
6568 	ASSERT(in_range(bytenr, page_start, PAGE_SIZE));
6569 	ASSERT(PAGE_SIZE / fs_info->nodesize <= BTRFS_SUBPAGE_BITMAP_SIZE);
6570 	lockdep_assert_held(&fs_info->buffer_lock);
6571 
6572 	ret = radix_tree_gang_lookup(&fs_info->buffer_radix, (void **)gang,
6573 			bytenr >> fs_info->sectorsize_bits,
6574 			PAGE_SIZE / fs_info->nodesize);
6575 	for (i = 0; i < ret; i++) {
6576 		/* Already beyond page end */
6577 		if (gang[i]->start >= page_start + PAGE_SIZE)
6578 			break;
6579 		/* Found one */
6580 		if (gang[i]->start >= bytenr) {
6581 			found = gang[i];
6582 			break;
6583 		}
6584 	}
6585 	return found;
6586 }
6587 
6588 static int try_release_subpage_extent_buffer(struct page *page)
6589 {
6590 	struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
6591 	u64 cur = page_offset(page);
6592 	const u64 end = page_offset(page) + PAGE_SIZE;
6593 	int ret;
6594 
6595 	while (cur < end) {
6596 		struct extent_buffer *eb = NULL;
6597 
6598 		/*
6599 		 * Unlike try_release_extent_buffer() which uses page->private
6600 		 * to grab buffer, for subpage case we rely on radix tree, thus
6601 		 * we need to ensure radix tree consistency.
6602 		 *
6603 		 * We also want an atomic snapshot of the radix tree, thus go
6604 		 * with spinlock rather than RCU.
6605 		 */
6606 		spin_lock(&fs_info->buffer_lock);
6607 		eb = get_next_extent_buffer(fs_info, page, cur);
6608 		if (!eb) {
6609 			/* No more eb in the page range after or at cur */
6610 			spin_unlock(&fs_info->buffer_lock);
6611 			break;
6612 		}
6613 		cur = eb->start + eb->len;
6614 
6615 		/*
6616 		 * The same as try_release_extent_buffer(), to ensure the eb
6617 		 * won't disappear out from under us.
6618 		 */
6619 		spin_lock(&eb->refs_lock);
6620 		if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
6621 			spin_unlock(&eb->refs_lock);
6622 			spin_unlock(&fs_info->buffer_lock);
6623 			break;
6624 		}
6625 		spin_unlock(&fs_info->buffer_lock);
6626 
6627 		/*
6628 		 * If tree ref isn't set then we know the ref on this eb is a
6629 		 * real ref, so just return, this eb will likely be freed soon
6630 		 * anyway.
6631 		 */
6632 		if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
6633 			spin_unlock(&eb->refs_lock);
6634 			break;
6635 		}
6636 
6637 		/*
6638 		 * Here we don't care about the return value, we will always
6639 		 * check the page private at the end.  And
6640 		 * release_extent_buffer() will release the refs_lock.
6641 		 */
6642 		release_extent_buffer(eb);
6643 	}
6644 	/*
6645 	 * Finally to check if we have cleared page private, as if we have
6646 	 * released all ebs in the page, the page private should be cleared now.
6647 	 */
6648 	spin_lock(&page->mapping->private_lock);
6649 	if (!PagePrivate(page))
6650 		ret = 1;
6651 	else
6652 		ret = 0;
6653 	spin_unlock(&page->mapping->private_lock);
6654 	return ret;
6655 
6656 }
6657 
6658 int try_release_extent_buffer(struct page *page)
6659 {
6660 	struct extent_buffer *eb;
6661 
6662 	if (btrfs_sb(page->mapping->host->i_sb)->sectorsize < PAGE_SIZE)
6663 		return try_release_subpage_extent_buffer(page);
6664 
6665 	/*
6666 	 * We need to make sure nobody is changing page->private, as we rely on
6667 	 * page->private as the pointer to extent buffer.
6668 	 */
6669 	spin_lock(&page->mapping->private_lock);
6670 	if (!PagePrivate(page)) {
6671 		spin_unlock(&page->mapping->private_lock);
6672 		return 1;
6673 	}
6674 
6675 	eb = (struct extent_buffer *)page->private;
6676 	BUG_ON(!eb);
6677 
6678 	/*
6679 	 * This is a little awful but should be ok, we need to make sure that
6680 	 * the eb doesn't disappear out from under us while we're looking at
6681 	 * this page.
6682 	 */
6683 	spin_lock(&eb->refs_lock);
6684 	if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
6685 		spin_unlock(&eb->refs_lock);
6686 		spin_unlock(&page->mapping->private_lock);
6687 		return 0;
6688 	}
6689 	spin_unlock(&page->mapping->private_lock);
6690 
6691 	/*
6692 	 * If tree ref isn't set then we know the ref on this eb is a real ref,
6693 	 * so just return, this page will likely be freed soon anyway.
6694 	 */
6695 	if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
6696 		spin_unlock(&eb->refs_lock);
6697 		return 0;
6698 	}
6699 
6700 	return release_extent_buffer(eb);
6701 }
6702 
6703 /*
6704  * btrfs_readahead_tree_block - attempt to readahead a child block
6705  * @fs_info:	the fs_info
6706  * @bytenr:	bytenr to read
6707  * @owner_root: objectid of the root that owns this eb
6708  * @gen:	generation for the uptodate check, can be 0
6709  * @level:	level for the eb
6710  *
6711  * Attempt to readahead a tree block at @bytenr.  If @gen is 0 then we do a
6712  * normal uptodate check of the eb, without checking the generation.  If we have
6713  * to read the block we will not block on anything.
6714  */
6715 void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
6716 				u64 bytenr, u64 owner_root, u64 gen, int level)
6717 {
6718 	struct extent_buffer *eb;
6719 	int ret;
6720 
6721 	eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level);
6722 	if (IS_ERR(eb))
6723 		return;
6724 
6725 	if (btrfs_buffer_uptodate(eb, gen, 1)) {
6726 		free_extent_buffer(eb);
6727 		return;
6728 	}
6729 
6730 	ret = read_extent_buffer_pages(eb, WAIT_NONE, 0);
6731 	if (ret < 0)
6732 		free_extent_buffer_stale(eb);
6733 	else
6734 		free_extent_buffer(eb);
6735 }
6736 
6737 /*
6738  * btrfs_readahead_node_child - readahead a node's child block
6739  * @node:	parent node we're reading from
6740  * @slot:	slot in the parent node for the child we want to read
6741  *
6742  * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at
6743  * the slot in the node provided.
6744  */
6745 void btrfs_readahead_node_child(struct extent_buffer *node, int slot)
6746 {
6747 	btrfs_readahead_tree_block(node->fs_info,
6748 				   btrfs_node_blockptr(node, slot),
6749 				   btrfs_header_owner(node),
6750 				   btrfs_node_ptr_generation(node, slot),
6751 				   btrfs_header_level(node) - 1);
6752 }
6753