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