1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitops.h>
4 #include <linux/slab.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>
17 #include "extent_io.h"
18 #include "extent-io-tree.h"
19 #include "extent_map.h"
21 #include "btrfs_inode.h"
23 #include "check-integrity.h"
25 #include "rcu-string.h"
30 #include "block-group.h"
32 static struct kmem_cache *extent_state_cache;
33 static struct kmem_cache *extent_buffer_cache;
34 static struct bio_set btrfs_bioset;
36 static inline bool extent_state_in_tree(const struct extent_state *state)
38 return !RB_EMPTY_NODE(&state->rb_node);
41 #ifdef CONFIG_BTRFS_DEBUG
42 static LIST_HEAD(states);
43 static DEFINE_SPINLOCK(leak_lock);
45 static inline void btrfs_leak_debug_add(spinlock_t *lock,
46 struct list_head *new,
47 struct list_head *head)
51 spin_lock_irqsave(lock, flags);
53 spin_unlock_irqrestore(lock, flags);
56 static inline void btrfs_leak_debug_del(spinlock_t *lock,
57 struct list_head *entry)
61 spin_lock_irqsave(lock, flags);
63 spin_unlock_irqrestore(lock, flags);
66 void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info)
68 struct extent_buffer *eb;
72 * If we didn't get into open_ctree our allocated_ebs will not be
73 * initialized, so just skip this.
75 if (!fs_info->allocated_ebs.next)
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);
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);
89 spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
92 static inline void btrfs_extent_state_leak_debug_check(void)
94 struct extent_state *state;
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);
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)
112 struct inode *inode = tree->private_data;
115 if (!inode || !is_data_inode(inode))
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);
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)
135 struct rb_node rb_node;
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
143 unsigned int extent_locked:1;
145 /* tells the submit_bio code to use REQ_SYNC */
146 unsigned int sync_io:1;
149 static int add_extent_changeset(struct extent_state *state, u32 bits,
150 struct extent_changeset *changeset,
157 if (set && (state->state & bits) == bits)
159 if (!set && (state->state & bits) == 0)
161 changeset->bytes_changed += state->end - state->start + 1;
162 ret = ulist_add(&changeset->range_changed, state->start, state->end,
167 int __must_check submit_one_bio(struct bio *bio, int mirror_num,
168 unsigned long bio_flags)
170 blk_status_t ret = 0;
171 struct extent_io_tree *tree = bio->bi_private;
173 bio->bi_private = NULL;
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,
181 ret = btrfs_submit_metadata_bio(tree->private_data, bio,
182 mirror_num, bio_flags);
184 return blk_status_to_errno(ret);
187 /* Cleanup unsubmitted bios */
188 static void end_write_bio(struct extent_page_data *epd, int ret)
190 struct bio *bio = epd->bio_ctrl.bio;
193 bio->bi_status = errno_to_blk_status(ret);
195 epd->bio_ctrl.bio = NULL;
200 * Submit bio from extent page data via submit_one_bio
202 * Return 0 if everything is OK.
203 * Return <0 for error.
205 static int __must_check flush_write_bio(struct extent_page_data *epd)
208 struct bio *bio = epd->bio_ctrl.bio;
211 ret = submit_one_bio(bio, 0, 0);
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.
219 epd->bio_ctrl.bio = NULL;
224 int __init extent_state_cache_init(void)
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)
234 int __init extent_io_init(void)
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)
242 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
243 offsetof(struct btrfs_bio, bio),
245 goto free_buffer_cache;
247 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
253 bioset_exit(&btrfs_bioset);
256 kmem_cache_destroy(extent_buffer_cache);
257 extent_buffer_cache = NULL;
261 void __cold extent_state_cache_exit(void)
263 btrfs_extent_state_leak_debug_check();
264 kmem_cache_destroy(extent_state_cache);
267 void __cold extent_io_exit(void)
270 * Make sure all delayed rcu free are flushed before we
274 kmem_cache_destroy(extent_buffer_cache);
275 bioset_exit(&btrfs_bioset);
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.
285 static struct lock_class_key file_extent_tree_class;
287 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
288 struct extent_io_tree *tree, unsigned int owner,
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;
297 if (owner == IO_TREE_INODE_FILE_EXTENT)
298 lockdep_set_class(&tree->lock, &file_extent_tree_class);
301 void extent_io_tree_release(struct extent_io_tree *tree)
303 spin_lock(&tree->lock);
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
310 while (!RB_EMPTY_ROOT(&tree->state)) {
311 struct rb_node *node;
312 struct extent_state *state;
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);
319 * btree io trees aren't supposed to have tasks waiting for
320 * changes in the flags of extent states ever.
322 ASSERT(!waitqueue_active(&state->wq));
323 free_extent_state(state);
325 cond_resched_lock(&tree->lock);
327 spin_unlock(&tree->lock);
330 static struct extent_state *alloc_extent_state(gfp_t mask)
332 struct extent_state *state;
335 * The given mask might be not appropriate for the slab allocator,
336 * drop the unsupported bits
338 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
339 state = kmem_cache_alloc(extent_state_cache, mask);
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_);
352 void free_extent_state(struct extent_state *state)
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);
364 static struct rb_node *tree_insert(struct rb_root *root,
365 struct rb_node *search_start,
367 struct rb_node *node,
368 struct rb_node ***p_in,
369 struct rb_node **parent_in)
372 struct rb_node *parent = NULL;
373 struct tree_entry *entry;
375 if (p_in && parent_in) {
381 p = search_start ? &search_start : &root->rb_node;
384 entry = rb_entry(parent, struct tree_entry, rb_node);
386 if (offset < entry->start)
388 else if (offset > entry->end)
395 rb_link_node(node, parent, p);
396 rb_insert_color(node, root);
401 * Search @tree for an entry that contains @offset. Such entry would have
402 * entry->start <= offset && entry->end >= offset.
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
410 * @parent_ret: points to entry which would have been the parent of the entry,
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.
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)
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;
433 entry = rb_entry(prev, struct tree_entry, rb_node);
436 if (offset < entry->start)
438 else if (offset > entry->end)
451 while (prev && offset > prev_entry->end) {
452 prev = rb_next(prev);
453 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
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);
470 static inline struct rb_node *
471 tree_search_for_insert(struct extent_io_tree *tree,
473 struct rb_node ***p_ret,
474 struct rb_node **parent_ret)
476 struct rb_node *next= NULL;
479 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
485 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
488 return tree_search_for_insert(tree, offset, NULL, NULL);
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).
498 * This should be called with the tree lock held.
500 static void merge_state(struct extent_io_tree *tree,
501 struct extent_state *state)
503 struct extent_state *other;
504 struct rb_node *other_node;
506 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
509 other_node = rb_prev(&state->rb_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,
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);
524 other_node = rb_next(&state->rb_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,
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);
541 static void set_state_bits(struct extent_io_tree *tree,
542 struct extent_state *state, u32 *bits,
543 struct extent_changeset *changeset);
546 * insert an extent_state struct into the tree. 'bits' are set on the
547 * struct before it is inserted.
549 * This may return -EEXIST if the extent is already there, in which case the
550 * state struct is freed.
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).
555 static int insert_state(struct extent_io_tree *tree,
556 struct extent_state *state, u64 start, u64 end,
558 struct rb_node **parent,
559 u32 *bits, struct extent_changeset *changeset)
561 struct rb_node *node;
564 btrfs_err(tree->fs_info,
565 "insert state: end < start %llu %llu", end, start);
568 state->start = start;
571 set_state_bits(tree, state, bits, changeset);
573 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
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);
582 merge_state(tree, state);
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.
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 ]
597 * The tree locks are not taken by this function. They need to be held
600 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
601 struct extent_state *prealloc, u64 split)
603 struct rb_node *node;
605 if (tree->private_data && is_data_inode(tree->private_data))
606 btrfs_split_delalloc_extent(tree->private_data, orig, split);
608 prealloc->start = orig->start;
609 prealloc->end = split - 1;
610 prealloc->state = orig->state;
613 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
614 &prealloc->rb_node, NULL, NULL);
616 free_extent_state(prealloc);
622 static struct extent_state *next_state(struct extent_state *state)
624 struct rb_node *next = rb_next(&state->rb_node);
626 return rb_entry(next, struct extent_state, rb_node);
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).
635 * If no bits are set on the state struct after clearing things, the
636 * struct is freed and removed from the tree
638 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
639 struct extent_state *state,
641 struct extent_changeset *changeset)
643 struct extent_state *next;
644 u32 bits_to_clear = *bits & ~EXTENT_CTLBITS;
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;
653 if (tree->private_data && is_data_inode(tree->private_data))
654 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
656 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
658 state->state &= ~bits_to_clear;
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);
671 merge_state(tree, state);
672 next = next_state(state);
677 static struct extent_state *
678 alloc_extent_state_atomic(struct extent_state *prealloc)
681 prealloc = alloc_extent_state(GFP_ATOMIC);
686 static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
688 btrfs_panic(tree->fs_info, err,
689 "locking error: extent tree was modified by another thread while locked");
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.
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).
700 * the range [start, end] is inclusive.
702 * This takes the tree lock, and returns 0 on success and < 0 on error.
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)
709 struct extent_state *state;
710 struct extent_state *cached;
711 struct extent_state *prealloc = NULL;
712 struct rb_node *node;
717 btrfs_debug_check_extent_io_range(tree, start, end);
718 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
720 if (bits & EXTENT_DELALLOC)
721 bits |= EXTENT_NORESERVE;
724 bits |= ~EXTENT_CTLBITS;
726 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
729 if (!prealloc && gfpflags_allow_blocking(mask)) {
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.
737 prealloc = alloc_extent_state(mask);
740 spin_lock(&tree->lock);
742 cached = *cached_state;
745 *cached_state = NULL;
749 if (cached && extent_state_in_tree(cached) &&
750 cached->start <= start && cached->end > start) {
752 refcount_dec(&cached->refs);
757 free_extent_state(cached);
760 * this search will find the extents that end after
763 node = tree_search(tree, start);
766 state = rb_entry(node, struct extent_state, rb_node);
768 if (state->start > end)
770 WARN_ON(state->end < start);
771 last_end = state->end;
773 /* the state doesn't have the wanted bits, go ahead */
774 if (!(state->state & bits)) {
775 state = next_state(state);
780 * | ---- desired range ---- |
782 * | ------------- state -------------- |
784 * We need to split the extent we found, and may flip
785 * bits on second half.
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.
791 * If the extent we found is inside our range, we clear
792 * the desired bit on it.
795 if (state->start < start) {
796 prealloc = alloc_extent_state_atomic(prealloc);
798 err = split_state(tree, state, prealloc, start);
800 extent_io_tree_panic(tree, err);
805 if (state->end <= end) {
806 state = clear_state_bit(tree, state, &bits, wake,
813 * | ---- desired range ---- |
815 * We need to split the extent, and clear the bit
818 if (state->start <= end && state->end > end) {
819 prealloc = alloc_extent_state_atomic(prealloc);
821 err = split_state(tree, state, prealloc, end + 1);
823 extent_io_tree_panic(tree, err);
828 clear_state_bit(tree, prealloc, &bits, wake, changeset);
834 state = clear_state_bit(tree, state, &bits, wake, changeset);
836 if (last_end == (u64)-1)
838 start = last_end + 1;
839 if (start <= end && state && !need_resched())
845 spin_unlock(&tree->lock);
846 if (gfpflags_allow_blocking(mask))
851 spin_unlock(&tree->lock);
853 free_extent_state(prealloc);
859 static void wait_on_state(struct extent_io_tree *tree,
860 struct extent_state *state)
861 __releases(tree->lock)
862 __acquires(tree->lock)
865 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
866 spin_unlock(&tree->lock);
868 spin_lock(&tree->lock);
869 finish_wait(&state->wq, &wait);
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
877 static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
880 struct extent_state *state;
881 struct rb_node *node;
883 btrfs_debug_check_extent_io_range(tree, start, end);
885 spin_lock(&tree->lock);
889 * this search will find all the extents that end after
892 node = tree_search(tree, start);
897 state = rb_entry(node, struct extent_state, rb_node);
899 if (state->start > end)
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);
909 start = state->end + 1;
914 if (!cond_resched_lock(&tree->lock)) {
915 node = rb_next(node);
920 spin_unlock(&tree->lock);
923 static void set_state_bits(struct extent_io_tree *tree,
924 struct extent_state *state,
925 u32 *bits, struct extent_changeset *changeset)
927 u32 bits_to_set = *bits & ~EXTENT_CTLBITS;
930 if (tree->private_data && is_data_inode(tree->private_data))
931 btrfs_set_delalloc_extent(tree->private_data, state, bits);
933 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
934 u64 range = state->end - state->start + 1;
935 tree->dirty_bytes += range;
937 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
939 state->state |= bits_to_set;
942 static void cache_state_if_flags(struct extent_state *state,
943 struct extent_state **cached_ptr,
946 if (cached_ptr && !(*cached_ptr)) {
947 if (!flags || (state->state & flags)) {
949 refcount_inc(&state->refs);
954 static void cache_state(struct extent_state *state,
955 struct extent_state **cached_ptr)
957 return cache_state_if_flags(state, cached_ptr,
958 EXTENT_LOCKED | EXTENT_BOUNDARY);
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.
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.
969 * [start, end] is inclusive This takes the tree lock.
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)
976 struct extent_state *state;
977 struct extent_state *prealloc = NULL;
978 struct rb_node *node;
980 struct rb_node *parent;
985 btrfs_debug_check_extent_io_range(tree, start, end);
986 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
989 ASSERT(failed_start);
991 ASSERT(failed_start == NULL);
993 if (!prealloc && gfpflags_allow_blocking(mask)) {
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.
1001 prealloc = alloc_extent_state(mask);
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;
1014 * this search will find all the extents that end after
1017 node = tree_search_for_insert(tree, start, &p, &parent);
1019 prealloc = alloc_extent_state_atomic(prealloc);
1021 err = insert_state(tree, prealloc, start, end,
1022 &p, &parent, &bits, changeset);
1024 extent_io_tree_panic(tree, err);
1026 cache_state(prealloc, cached_state);
1030 state = rb_entry(node, struct extent_state, rb_node);
1032 last_start = state->start;
1033 last_end = state->end;
1036 * | ---- desired range ---- |
1039 * Just lock what we found and keep going
1041 if (state->start == start && state->end <= end) {
1042 if (state->state & exclusive_bits) {
1043 *failed_start = state->start;
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)
1053 start = last_end + 1;
1054 state = next_state(state);
1055 if (start < end && state && state->start == start &&
1062 * | ---- desired range ---- |
1065 * | ------------- state -------------- |
1067 * We need to split the extent we found, and may flip bits on
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.
1074 * If the extent we found is inside our range, we set the
1075 * desired bit on it.
1077 if (state->start < start) {
1078 if (state->state & exclusive_bits) {
1079 *failed_start = start;
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.
1088 if ((state->state & bits) == bits) {
1089 start = state->end + 1;
1090 cache_state(state, cached_state);
1094 prealloc = alloc_extent_state_atomic(prealloc);
1096 err = split_state(tree, state, prealloc, start);
1098 extent_io_tree_panic(tree, err);
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)
1109 start = last_end + 1;
1110 state = next_state(state);
1111 if (start < end && state && state->start == start &&
1118 * | ---- desired range ---- |
1119 * | state | or | state |
1121 * There's a hole, we need to insert something in it and
1122 * ignore the extent we found.
1124 if (state->start > start) {
1126 if (end < last_start)
1129 this_end = last_start - 1;
1131 prealloc = alloc_extent_state_atomic(prealloc);
1135 * Avoid to free 'prealloc' if it can be merged with
1138 err = insert_state(tree, prealloc, start, this_end,
1139 NULL, NULL, &bits, changeset);
1141 extent_io_tree_panic(tree, err);
1143 cache_state(prealloc, cached_state);
1145 start = this_end + 1;
1149 * | ---- desired range ---- |
1151 * We need to split the extent, and set the bit
1154 if (state->start <= end && state->end > end) {
1155 if (state->state & exclusive_bits) {
1156 *failed_start = start;
1161 prealloc = alloc_extent_state_atomic(prealloc);
1163 err = split_state(tree, state, prealloc, end + 1);
1165 extent_io_tree_panic(tree, err);
1167 set_state_bits(tree, prealloc, &bits, changeset);
1168 cache_state(prealloc, cached_state);
1169 merge_state(tree, prealloc);
1177 spin_unlock(&tree->lock);
1178 if (gfpflags_allow_blocking(mask))
1183 spin_unlock(&tree->lock);
1185 free_extent_state(prealloc);
1192 * convert_extent_bit - convert all bits in a given range from one bit to
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
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.
1207 * All allocations are done with GFP_NOFS.
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)
1213 struct extent_state *state;
1214 struct extent_state *prealloc = NULL;
1215 struct rb_node *node;
1217 struct rb_node *parent;
1221 bool first_iteration = true;
1223 btrfs_debug_check_extent_io_range(tree, start, end);
1224 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
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.
1236 prealloc = alloc_extent_state(GFP_NOFS);
1237 if (!prealloc && !first_iteration)
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;
1252 * this search will find all the extents that end after
1255 node = tree_search_for_insert(tree, start, &p, &parent);
1257 prealloc = alloc_extent_state_atomic(prealloc);
1262 err = insert_state(tree, prealloc, start, end,
1263 &p, &parent, &bits, NULL);
1265 extent_io_tree_panic(tree, err);
1266 cache_state(prealloc, cached_state);
1270 state = rb_entry(node, struct extent_state, rb_node);
1272 last_start = state->start;
1273 last_end = state->end;
1276 * | ---- desired range ---- |
1279 * Just lock what we found and keep going
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)
1287 start = last_end + 1;
1288 if (start < end && state && state->start == start &&
1295 * | ---- desired range ---- |
1298 * | ------------- state -------------- |
1300 * We need to split the extent we found, and may flip bits on
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.
1307 * If the extent we found is inside our range, we set the
1308 * desired bit on it.
1310 if (state->start < start) {
1311 prealloc = alloc_extent_state_atomic(prealloc);
1316 err = split_state(tree, state, prealloc, start);
1318 extent_io_tree_panic(tree, err);
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,
1327 if (last_end == (u64)-1)
1329 start = last_end + 1;
1330 if (start < end && state && state->start == start &&
1337 * | ---- desired range ---- |
1338 * | state | or | state |
1340 * There's a hole, we need to insert something in it and
1341 * ignore the extent we found.
1343 if (state->start > start) {
1345 if (end < last_start)
1348 this_end = last_start - 1;
1350 prealloc = alloc_extent_state_atomic(prealloc);
1357 * Avoid to free 'prealloc' if it can be merged with
1360 err = insert_state(tree, prealloc, start, this_end,
1361 NULL, NULL, &bits, NULL);
1363 extent_io_tree_panic(tree, err);
1364 cache_state(prealloc, cached_state);
1366 start = this_end + 1;
1370 * | ---- desired range ---- |
1372 * We need to split the extent, and set the bit
1375 if (state->start <= end && state->end > end) {
1376 prealloc = alloc_extent_state_atomic(prealloc);
1382 err = split_state(tree, state, prealloc, end + 1);
1384 extent_io_tree_panic(tree, err);
1386 set_state_bits(tree, prealloc, &bits, NULL);
1387 cache_state(prealloc, cached_state);
1388 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
1396 spin_unlock(&tree->lock);
1398 first_iteration = false;
1402 spin_unlock(&tree->lock);
1404 free_extent_state(prealloc);
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)
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
1419 BUG_ON(bits & EXTENT_LOCKED);
1421 return set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
1425 int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
1428 return set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
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)
1436 return __clear_extent_bit(tree, start, end, bits, wake, delete,
1437 cached, GFP_NOFS, NULL);
1440 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1441 u32 bits, struct extent_changeset *changeset)
1444 * Don't support EXTENT_LOCKED case, same reason as
1445 * set_record_extent_bits().
1447 BUG_ON(bits & EXTENT_LOCKED);
1449 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
1454 * either insert or lock state struct between start and end use mask to tell
1455 * us if waiting is desired.
1457 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1458 struct extent_state **cached_state)
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;
1472 WARN_ON(start > end);
1477 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
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);
1493 void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1495 unsigned long index = start >> PAGE_SHIFT;
1496 unsigned long end_index = end >> PAGE_SHIFT;
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);
1508 void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
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;
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);
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'
1528 static struct extent_state *
1529 find_first_extent_bit_state(struct extent_io_tree *tree, u64 start, u32 bits)
1531 struct rb_node *node;
1532 struct extent_state *state;
1535 * this search will find all the extents that end after
1538 node = tree_search(tree, start);
1543 state = rb_entry(node, struct extent_state, rb_node);
1544 if (state->end >= start && (state->state & bits))
1547 node = rb_next(node);
1556 * Find the first offset in the io tree with one or more @bits set.
1558 * Note: If there are multiple bits set in @bits, any of them will match.
1560 * Return 0 if we find something, and update @start_ret and @end_ret.
1561 * Return 1 if we found nothing.
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)
1567 struct extent_state *state;
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)
1578 free_extent_state(*cached_state);
1579 *cached_state = NULL;
1582 free_extent_state(*cached_state);
1583 *cached_state = NULL;
1586 state = find_first_extent_bit_state(tree, start, bits);
1589 cache_state_if_flags(state, cached_state, 0);
1590 *start_ret = state->start;
1591 *end_ret = state->end;
1595 spin_unlock(&tree->lock);
1600 * Find a contiguous area of bits
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
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.
1615 int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
1616 u64 *start_ret, u64 *end_ret, u32 bits)
1618 struct extent_state *state;
1621 spin_lock(&tree->lock);
1622 state = find_first_extent_bit_state(tree, start, bits);
1624 *start_ret = state->start;
1625 *end_ret = state->end;
1626 while ((state = next_state(state)) != NULL) {
1627 if (state->start > (*end_ret + 1))
1629 *end_ret = state->end;
1633 spin_unlock(&tree->lock);
1638 * Find the first range that has @bits not set. This range could start before
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
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.
1652 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
1653 u64 *start_ret, u64 *end_ret, u32 bits)
1655 struct extent_state *state;
1656 struct rb_node *node, *prev = NULL, *next;
1658 spin_lock(&tree->lock);
1660 /* Find first extent with bits cleared */
1662 node = __etree_search(tree, start, &next, &prev, NULL, NULL);
1663 if (!node && !next && !prev) {
1665 * Tree is completely empty, send full range and let
1666 * caller deal with it
1671 } else if (!node && !next) {
1673 * We are past the last allocated chunk, set start at
1674 * the end of the last extent.
1676 state = rb_entry(prev, struct extent_state, rb_node);
1677 *start_ret = state->end + 1;
1684 * At this point 'node' either contains 'start' or start is
1687 state = rb_entry(node, struct extent_state, rb_node);
1689 if (in_range(start, state->start, state->end - state->start + 1)) {
1690 if (state->state & bits) {
1692 * |--range with bits sets--|
1696 start = state->end + 1;
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
1703 * |--range with bits cleared----|
1707 *start_ret = state->start;
1712 * |---prev range---|---hole/unset---|---node range---|
1718 * |---hole/unset--||--first node--|
1723 state = rb_entry(prev, struct extent_state,
1725 *start_ret = state->end + 1;
1734 * Find the longest stretch from start until an entry which has the
1738 state = rb_entry(node, struct extent_state, rb_node);
1739 if (state->end >= start && !(state->state & bits)) {
1740 *end_ret = state->end;
1742 *end_ret = state->start - 1;
1746 node = rb_next(node);
1751 spin_unlock(&tree->lock);
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,
1758 * true is returned if we find something, false if nothing was in the tree
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)
1764 struct rb_node *node;
1765 struct extent_state *state;
1766 u64 cur_start = *start;
1768 u64 total_bytes = 0;
1770 spin_lock(&tree->lock);
1773 * this search will find all the extents that end after
1776 node = tree_search(tree, cur_start);
1783 state = rb_entry(node, struct extent_state, rb_node);
1784 if (found && (state->start != cur_start ||
1785 (state->state & EXTENT_BOUNDARY))) {
1788 if (!(state->state & EXTENT_DELALLOC)) {
1794 *start = state->start;
1795 *cached_state = state;
1796 refcount_inc(&state->refs);
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)
1809 spin_unlock(&tree->lock);
1814 * Process one page for __process_pages_contig().
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)
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)
1828 ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX);
1829 len = end + 1 - start;
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);
1839 if (page_ops & PAGE_END_WRITEBACK)
1840 btrfs_page_clamp_clear_writeback(fs_info, page, start, len);
1842 if (page == locked_page)
1845 if (page_ops & PAGE_LOCK) {
1848 ret = btrfs_page_start_writer_lock(fs_info, page, start, len);
1851 if (!PageDirty(page) || page->mapping != mapping) {
1852 btrfs_page_end_writer_lock(fs_info, page, start, len);
1856 if (page_ops & PAGE_UNLOCK)
1857 btrfs_page_end_writer_lock(fs_info, page, start, len);
1861 static int __process_pages_contig(struct address_space *mapping,
1862 struct page *locked_page,
1863 u64 start, u64 end, unsigned long page_ops,
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];
1876 if (page_ops & PAGE_LOCK) {
1877 ASSERT(page_ops == PAGE_LOCK);
1878 ASSERT(processed_end && *processed_end == start);
1881 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
1882 mapping_set_error(mapping, -EIO);
1884 while (nr_pages > 0) {
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) {
1892 * Only if we're going to lock these pages, we can find
1893 * nothing at @index.
1895 ASSERT(page_ops & PAGE_LOCK);
1900 for (i = 0; i < found_pages; i++) {
1903 process_ret = process_one_page(fs_info, mapping,
1904 pages[i], locked_page, page_ops,
1906 if (process_ret < 0) {
1907 for (; i < found_pages; i++)
1915 nr_pages -= found_pages;
1916 index += found_pages;
1920 if (err && processed_end) {
1922 * Update @processed_end. I know this is awful since it has
1923 * two different return value patterns (inclusive vs exclusive).
1925 * But the exclusive pattern is necessary if @start is 0, or we
1926 * underflow and check against processed_end won't work as
1929 if (pages_processed)
1930 *processed_end = min(end,
1931 ((u64)(start_index + pages_processed) << PAGE_SHIFT) - 1);
1933 *processed_end = start;
1938 static noinline void __unlock_for_delalloc(struct inode *inode,
1939 struct page *locked_page,
1942 unsigned long index = start >> PAGE_SHIFT;
1943 unsigned long end_index = end >> PAGE_SHIFT;
1945 ASSERT(locked_page);
1946 if (index == locked_page->index && end_index == index)
1949 __process_pages_contig(inode->i_mapping, locked_page, start, end,
1953 static noinline int lock_delalloc_pages(struct inode *inode,
1954 struct page *locked_page,
1958 unsigned long index = delalloc_start >> PAGE_SHIFT;
1959 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
1960 u64 processed_end = delalloc_start;
1963 ASSERT(locked_page);
1964 if (index == locked_page->index && index == end_index)
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,
1976 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1977 * more than @max_bytes.
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.
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.
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.
1991 noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
1992 struct page *locked_page, u64 *start,
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;
2002 struct extent_state *cached_state = NULL;
2006 /* Caller should pass a valid @end to indicate the search range end */
2007 ASSERT(orig_end > orig_start);
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)));
2013 /* step one, find a bunch of delalloc bytes starting at start */
2014 delalloc_start = *start;
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;
2021 /* @delalloc_end can be -1, never go beyond @orig_end */
2022 *end = min(delalloc_end, orig_end);
2023 free_extent_state(cached_state);
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
2032 if (delalloc_start < *start)
2033 delalloc_start = *start;
2036 * make sure to limit the number of pages we try to lock down
2038 if (delalloc_end + 1 - delalloc_start > max_bytes)
2039 delalloc_end = delalloc_start + max_bytes - 1;
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
2049 free_extent_state(cached_state);
2050 cached_state = NULL;
2052 max_bytes = PAGE_SIZE;
2061 /* step three, lock the state bits for the whole range */
2062 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
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);
2068 unlock_extent_cached(tree, delalloc_start, delalloc_end,
2070 __unlock_for_delalloc(inode, locked_page,
2071 delalloc_start, delalloc_end);
2075 free_extent_state(cached_state);
2076 *start = delalloc_start;
2077 *end = delalloc_end;
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)
2086 clear_extent_bit(&inode->io_tree, start, end, clear_bits, 1, 0, NULL);
2088 __process_pages_contig(inode->vfs_inode.i_mapping, locked_page,
2089 start, end, page_ops, NULL);
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.
2097 u64 count_range_bits(struct extent_io_tree *tree,
2098 u64 *start, u64 search_end, u64 max_bytes,
2099 u32 bits, int contig)
2101 struct rb_node *node;
2102 struct extent_state *state;
2103 u64 cur_start = *start;
2104 u64 total_bytes = 0;
2108 if (WARN_ON(search_end <= cur_start))
2111 spin_lock(&tree->lock);
2112 if (cur_start == 0 && bits == EXTENT_DIRTY) {
2113 total_bytes = tree->dirty_bytes;
2117 * this search will find all the extents that end after
2120 node = tree_search(tree, cur_start);
2125 state = rb_entry(node, struct extent_state, rb_node);
2126 if (state->start > search_end)
2128 if (contig && found && state->start > last + 1)
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)
2136 *start = max(cur_start, state->start);
2140 } else if (contig && found) {
2143 node = rb_next(node);
2148 spin_unlock(&tree->lock);
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.
2156 int set_state_failrec(struct extent_io_tree *tree, u64 start,
2157 struct io_failure_record *failrec)
2159 struct rb_node *node;
2160 struct extent_state *state;
2163 spin_lock(&tree->lock);
2165 * this search will find all the extents that end after
2168 node = tree_search(tree, start);
2173 state = rb_entry(node, struct extent_state, rb_node);
2174 if (state->start != start) {
2178 state->failrec = failrec;
2180 spin_unlock(&tree->lock);
2184 struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start)
2186 struct rb_node *node;
2187 struct extent_state *state;
2188 struct io_failure_record *failrec;
2190 spin_lock(&tree->lock);
2192 * this search will find all the extents that end after
2195 node = tree_search(tree, start);
2197 failrec = ERR_PTR(-ENOENT);
2200 state = rb_entry(node, struct extent_state, rb_node);
2201 if (state->start != start) {
2202 failrec = ERR_PTR(-ENOENT);
2206 failrec = state->failrec;
2208 spin_unlock(&tree->lock);
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.
2218 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
2219 u32 bits, int filled, struct extent_state *cached)
2221 struct extent_state *state = NULL;
2222 struct rb_node *node;
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;
2230 node = tree_search(tree, start);
2231 while (node && start <= end) {
2232 state = rb_entry(node, struct extent_state, rb_node);
2234 if (filled && state->start > start) {
2239 if (state->start > end)
2242 if (state->state & bits) {
2246 } else if (filled) {
2251 if (state->end == (u64)-1)
2254 start = state->end + 1;
2257 node = rb_next(node);
2264 spin_unlock(&tree->lock);
2268 int free_io_failure(struct extent_io_tree *failure_tree,
2269 struct extent_io_tree *io_tree,
2270 struct io_failure_record *rec)
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);
2282 ret = clear_extent_bits(io_tree, rec->start,
2283 rec->start + rec->len - 1,
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.
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)
2307 struct btrfs_device *dev;
2310 struct btrfs_io_context *bioc = NULL;
2313 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
2314 BUG_ON(!mirror_num);
2316 if (btrfs_repair_one_zone(fs_info, logical))
2319 bio = btrfs_bio_alloc(1);
2320 bio->bi_iter.bi_size = 0;
2321 map_length = length;
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.
2328 btrfs_bio_counter_inc_blocked(fs_info);
2329 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
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.
2336 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2337 &map_length, &bioc, 0);
2339 btrfs_bio_counter_dec(fs_info);
2343 ASSERT(bioc->mirror_num == 1);
2345 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2346 &map_length, &bioc, mirror_num);
2348 btrfs_bio_counter_dec(fs_info);
2352 BUG_ON(mirror_num != bioc->mirror_num);
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);
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);
2369 if (btrfsic_submit_bio_wait(bio)) {
2370 /* try to remap that extent elsewhere? */
2371 btrfs_bio_counter_dec(fs_info);
2373 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
2377 btrfs_info_rl_in_rcu(fs_info,
2378 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
2380 rcu_str_deref(dev->name), sector);
2381 btrfs_bio_counter_dec(fs_info);
2386 int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num)
2388 struct btrfs_fs_info *fs_info = eb->fs_info;
2389 u64 start = eb->start;
2390 int i, num_pages = num_extent_pages(eb);
2393 if (sb_rdonly(fs_info->sb))
2396 for (i = 0; i < num_pages; i++) {
2397 struct page *p = eb->pages[i];
2399 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
2400 start - page_offset(p), mirror_num);
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
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)
2419 struct io_failure_record *failrec;
2420 struct extent_state *state;
2425 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2430 failrec = get_state_failrec(failure_tree, start);
2431 if (IS_ERR(failrec))
2434 BUG_ON(!failrec->this_mirror);
2436 if (sb_rdonly(fs_info->sb))
2439 spin_lock(&io_tree->lock);
2440 state = find_first_extent_bit_state(io_tree,
2443 spin_unlock(&io_tree->lock);
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,
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);
2457 free_io_failure(failure_tree, io_tree, failrec);
2463 * Can be called when
2464 * - hold extent lock
2465 * - under ordered extent
2466 * - the inode is freeing
2468 void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
2470 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
2471 struct io_failure_record *failrec;
2472 struct extent_state *state, *next;
2474 if (RB_EMPTY_ROOT(&failure_tree->state))
2477 spin_lock(&failure_tree->lock);
2478 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2480 if (state->start > end)
2483 ASSERT(state->end <= end);
2485 next = next_state(state);
2487 failrec = state->failrec;
2488 free_extent_state(state);
2493 spin_unlock(&failure_tree->lock);
2496 static struct io_failure_record *btrfs_get_io_failure_record(struct inode *inode,
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;
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);
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.
2523 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2525 return ERR_PTR(-ENOMEM);
2527 failrec->start = start;
2528 failrec->len = sectorsize;
2529 failrec->this_mirror = 0;
2530 failrec->bio_flags = 0;
2532 read_lock(&em_tree->lock);
2533 em = lookup_extent_mapping(em_tree, start, failrec->len);
2535 read_unlock(&em_tree->lock);
2537 return ERR_PTR(-EIO);
2540 if (em->start > start || em->start + em->len <= start) {
2541 free_extent_map(em);
2544 read_unlock(&em_tree->lock);
2547 return ERR_PTR(-EIO);
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);
2558 btrfs_debug(fs_info,
2559 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2560 logical, start, failrec->len);
2562 failrec->logical = logical;
2563 free_extent_map(em);
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);
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,
2573 } else if (ret < 0) {
2575 return ERR_PTR(ret);
2581 static bool btrfs_check_repairable(struct inode *inode,
2582 struct io_failure_record *failrec,
2585 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2588 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
2589 if (num_copies == 1) {
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.
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);
2601 /* The failure record should only contain one sector */
2602 ASSERT(failrec->len == fs_info->sectorsize);
2605 * There are two premises:
2606 * a) deliver good data to the caller
2607 * b) correct the bad sectors on disk
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.
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++;
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);
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)
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;
2644 btrfs_debug(fs_info,
2645 "repair read error: read error at %llu", start);
2647 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
2649 failrec = btrfs_get_io_failure_record(inode, start);
2650 if (IS_ERR(failrec))
2651 return PTR_ERR(failrec);
2654 if (!btrfs_check_repairable(inode, failrec, failed_mirror)) {
2655 free_io_failure(failure_tree, tree, failrec);
2659 repair_bio = btrfs_bio_alloc(1);
2660 repair_bbio = btrfs_bio(repair_bio);
2661 repair_bio->bi_opf = REQ_OP_READ;
2662 repair_bio->bi_end_io = failed_bio->bi_end_io;
2663 repair_bio->bi_iter.bi_sector = failrec->logical >> 9;
2664 repair_bio->bi_private = failed_bio->bi_private;
2666 if (failed_bbio->csum) {
2667 const u32 csum_size = fs_info->csum_size;
2669 repair_bbio->csum = repair_bbio->csum_inline;
2670 memcpy(repair_bbio->csum,
2671 failed_bbio->csum + csum_size * icsum, csum_size);
2674 bio_add_page(repair_bio, page, failrec->len, pgoff);
2675 repair_bbio->iter = repair_bio->bi_iter;
2677 btrfs_debug(btrfs_sb(inode->i_sb),
2678 "repair read error: submitting new read to mirror %d",
2679 failrec->this_mirror);
2682 * At this point we have a bio, so any errors from submit_bio_hook()
2683 * will be handled by the endio on the repair_bio, so we can't return an
2686 submit_bio_hook(inode, repair_bio, failrec->this_mirror, failrec->bio_flags);
2690 static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len)
2692 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
2694 ASSERT(page_offset(page) <= start &&
2695 start + len <= page_offset(page) + PAGE_SIZE);
2698 if (fsverity_active(page->mapping->host) &&
2700 !PageUptodate(page) &&
2701 start < i_size_read(page->mapping->host) &&
2702 !fsverity_verify_page(page)) {
2703 btrfs_page_set_error(fs_info, page, start, len);
2705 btrfs_page_set_uptodate(fs_info, page, start, len);
2708 btrfs_page_clear_uptodate(fs_info, page, start, len);
2709 btrfs_page_set_error(fs_info, page, start, len);
2712 if (fs_info->sectorsize == PAGE_SIZE)
2715 btrfs_subpage_end_reader(fs_info, page, start, len);
2718 static blk_status_t submit_read_repair(struct inode *inode,
2719 struct bio *failed_bio, u32 bio_offset,
2720 struct page *page, unsigned int pgoff,
2721 u64 start, u64 end, int failed_mirror,
2722 unsigned int error_bitmap,
2723 submit_bio_hook_t *submit_bio_hook)
2725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2726 const u32 sectorsize = fs_info->sectorsize;
2727 const int nr_bits = (end + 1 - start) >> fs_info->sectorsize_bits;
2731 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
2733 /* We're here because we had some read errors or csum mismatch */
2734 ASSERT(error_bitmap);
2737 * We only get called on buffered IO, thus page must be mapped and bio
2738 * must not be cloned.
2740 ASSERT(page->mapping && !bio_flagged(failed_bio, BIO_CLONED));
2742 /* Iterate through all the sectors in the range */
2743 for (i = 0; i < nr_bits; i++) {
2744 const unsigned int offset = i * sectorsize;
2745 struct extent_state *cached = NULL;
2746 bool uptodate = false;
2749 if (!(error_bitmap & (1U << i))) {
2751 * This sector has no error, just end the page read
2752 * and unlock the range.
2758 ret = btrfs_repair_one_sector(inode, failed_bio,
2759 bio_offset + offset,
2760 page, pgoff + offset, start + offset,
2761 failed_mirror, submit_bio_hook);
2764 * We have submitted the read repair, the page release
2765 * will be handled by the endio function of the
2766 * submitted repair bio.
2767 * Thus we don't need to do any thing here.
2772 * Repair failed, just record the error but still continue.
2773 * Or the remaining sectors will not be properly unlocked.
2778 end_page_read(page, uptodate, start + offset, sectorsize);
2780 set_extent_uptodate(&BTRFS_I(inode)->io_tree,
2782 start + offset + sectorsize - 1,
2783 &cached, GFP_ATOMIC);
2784 unlock_extent_cached_atomic(&BTRFS_I(inode)->io_tree,
2786 start + offset + sectorsize - 1,
2789 return errno_to_blk_status(error);
2792 /* lots and lots of room for performance fixes in the end_bio funcs */
2794 void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2796 struct btrfs_inode *inode;
2797 const bool uptodate = (err == 0);
2800 ASSERT(page && page->mapping);
2801 inode = BTRFS_I(page->mapping->host);
2802 btrfs_writepage_endio_finish_ordered(inode, page, start, end, uptodate);
2805 const struct btrfs_fs_info *fs_info = inode->root->fs_info;
2808 ASSERT(end + 1 - start <= U32_MAX);
2809 len = end + 1 - start;
2811 btrfs_page_clear_uptodate(fs_info, page, start, len);
2812 btrfs_page_set_error(fs_info, page, start, len);
2813 ret = err < 0 ? err : -EIO;
2814 mapping_set_error(page->mapping, ret);
2819 * after a writepage IO is done, we need to:
2820 * clear the uptodate bits on error
2821 * clear the writeback bits in the extent tree for this IO
2822 * end_page_writeback if the page has no more pending IO
2824 * Scheduling is not allowed, so the extent state tree is expected
2825 * to have one and only one object corresponding to this IO.
2827 static void end_bio_extent_writepage(struct bio *bio)
2829 int error = blk_status_to_errno(bio->bi_status);
2830 struct bio_vec *bvec;
2833 struct bvec_iter_all iter_all;
2834 bool first_bvec = true;
2836 ASSERT(!bio_flagged(bio, BIO_CLONED));
2837 bio_for_each_segment_all(bvec, bio, iter_all) {
2838 struct page *page = bvec->bv_page;
2839 struct inode *inode = page->mapping->host;
2840 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2841 const u32 sectorsize = fs_info->sectorsize;
2843 /* Our read/write should always be sector aligned. */
2844 if (!IS_ALIGNED(bvec->bv_offset, sectorsize))
2846 "partial page write in btrfs with offset %u and length %u",
2847 bvec->bv_offset, bvec->bv_len);
2848 else if (!IS_ALIGNED(bvec->bv_len, sectorsize))
2850 "incomplete page write with offset %u and length %u",
2851 bvec->bv_offset, bvec->bv_len);
2853 start = page_offset(page) + bvec->bv_offset;
2854 end = start + bvec->bv_len - 1;
2857 btrfs_record_physical_zoned(inode, start, bio);
2861 end_extent_writepage(page, error, start, end);
2863 btrfs_page_clear_writeback(fs_info, page, start, bvec->bv_len);
2870 * Record previously processed extent range
2872 * For endio_readpage_release_extent() to handle a full extent range, reducing
2873 * the extent io operations.
2875 struct processed_extent {
2876 struct btrfs_inode *inode;
2877 /* Start of the range in @inode */
2879 /* End of the range in @inode */
2885 * Try to release processed extent range
2887 * May not release the extent range right now if the current range is
2888 * contiguous to processed extent.
2890 * Will release processed extent when any of @inode, @uptodate, the range is
2891 * no longer contiguous to the processed range.
2893 * Passing @inode == NULL will force processed extent to be released.
2895 static void endio_readpage_release_extent(struct processed_extent *processed,
2896 struct btrfs_inode *inode, u64 start, u64 end,
2899 struct extent_state *cached = NULL;
2900 struct extent_io_tree *tree;
2902 /* The first extent, initialize @processed */
2903 if (!processed->inode)
2907 * Contiguous to processed extent, just uptodate the end.
2909 * Several things to notice:
2911 * - bio can be merged as long as on-disk bytenr is contiguous
2912 * This means we can have page belonging to other inodes, thus need to
2913 * check if the inode still matches.
2914 * - bvec can contain range beyond current page for multi-page bvec
2915 * Thus we need to do processed->end + 1 >= start check
2917 if (processed->inode == inode && processed->uptodate == uptodate &&
2918 processed->end + 1 >= start && end >= processed->end) {
2919 processed->end = end;
2923 tree = &processed->inode->io_tree;
2925 * Now we don't have range contiguous to the processed range, release
2926 * the processed range now.
2928 if (processed->uptodate && tree->track_uptodate)
2929 set_extent_uptodate(tree, processed->start, processed->end,
2930 &cached, GFP_ATOMIC);
2931 unlock_extent_cached_atomic(tree, processed->start, processed->end,
2935 /* Update processed to current range */
2936 processed->inode = inode;
2937 processed->start = start;
2938 processed->end = end;
2939 processed->uptodate = uptodate;
2942 static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page)
2944 ASSERT(PageLocked(page));
2945 if (fs_info->sectorsize == PAGE_SIZE)
2948 ASSERT(PagePrivate(page));
2949 btrfs_subpage_start_reader(fs_info, page, page_offset(page), PAGE_SIZE);
2953 * Find extent buffer for a givne bytenr.
2955 * This is for end_bio_extent_readpage(), thus we can't do any unsafe locking
2958 static struct extent_buffer *find_extent_buffer_readpage(
2959 struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
2961 struct extent_buffer *eb;
2964 * For regular sectorsize, we can use page->private to grab extent
2967 if (fs_info->sectorsize == PAGE_SIZE) {
2968 ASSERT(PagePrivate(page) && page->private);
2969 return (struct extent_buffer *)page->private;
2972 /* For subpage case, we need to lookup buffer radix tree */
2974 eb = radix_tree_lookup(&fs_info->buffer_radix,
2975 bytenr >> fs_info->sectorsize_bits);
2982 * after a readpage IO is done, we need to:
2983 * clear the uptodate bits on error
2984 * set the uptodate bits if things worked
2985 * set the page up to date if all extents in the tree are uptodate
2986 * clear the lock bit in the extent tree
2987 * unlock the page if there are no other extents locked for it
2989 * Scheduling is not allowed, so the extent state tree is expected
2990 * to have one and only one object corresponding to this IO.
2992 static void end_bio_extent_readpage(struct bio *bio)
2994 struct bio_vec *bvec;
2995 struct btrfs_bio *bbio = btrfs_bio(bio);
2996 struct extent_io_tree *tree, *failure_tree;
2997 struct processed_extent processed = { 0 };
2999 * The offset to the beginning of a bio, since one bio can never be
3000 * larger than UINT_MAX, u32 here is enough.
3005 struct bvec_iter_all iter_all;
3007 ASSERT(!bio_flagged(bio, BIO_CLONED));
3008 bio_for_each_segment_all(bvec, bio, iter_all) {
3009 bool uptodate = !bio->bi_status;
3010 struct page *page = bvec->bv_page;
3011 struct inode *inode = page->mapping->host;
3012 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3013 const u32 sectorsize = fs_info->sectorsize;
3014 unsigned int error_bitmap = (unsigned int)-1;
3019 btrfs_debug(fs_info,
3020 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
3021 bio->bi_iter.bi_sector, bio->bi_status,
3023 tree = &BTRFS_I(inode)->io_tree;
3024 failure_tree = &BTRFS_I(inode)->io_failure_tree;
3027 * We always issue full-sector reads, but if some block in a
3028 * page fails to read, blk_update_request() will advance
3029 * bv_offset and adjust bv_len to compensate. Print a warning
3030 * for unaligned offsets, and an error if they don't add up to
3033 if (!IS_ALIGNED(bvec->bv_offset, sectorsize))
3035 "partial page read in btrfs with offset %u and length %u",
3036 bvec->bv_offset, bvec->bv_len);
3037 else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len,
3040 "incomplete page read with offset %u and length %u",
3041 bvec->bv_offset, bvec->bv_len);
3043 start = page_offset(page) + bvec->bv_offset;
3044 end = start + bvec->bv_len - 1;
3047 mirror = bbio->mirror_num;
3048 if (likely(uptodate)) {
3049 if (is_data_inode(inode)) {
3050 error_bitmap = btrfs_verify_data_csum(bbio,
3051 bio_offset, page, start, end);
3054 ret = btrfs_validate_metadata_buffer(bbio,
3055 page, start, end, mirror);
3060 clean_io_failure(BTRFS_I(inode)->root->fs_info,
3061 failure_tree, tree, start,
3063 btrfs_ino(BTRFS_I(inode)), 0);
3066 if (likely(uptodate))
3069 if (is_data_inode(inode)) {
3071 * If we failed to submit the IO at all we'll have a
3072 * mirror_num == 0, in which case we need to just mark
3073 * the page with an error and unlock it and carry on.
3079 * btrfs_submit_read_repair() will handle all the good
3080 * and bad sectors, we just continue to the next bvec.
3082 submit_read_repair(inode, bio, bio_offset, page,
3083 start - page_offset(page), start,
3084 end, mirror, error_bitmap,
3085 btrfs_submit_data_bio);
3087 ASSERT(bio_offset + len > bio_offset);
3091 struct extent_buffer *eb;
3093 eb = find_extent_buffer_readpage(fs_info, page, start);
3094 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
3095 eb->read_mirror = mirror;
3096 atomic_dec(&eb->io_pages);
3099 if (likely(uptodate)) {
3100 loff_t i_size = i_size_read(inode);
3101 pgoff_t end_index = i_size >> PAGE_SHIFT;
3104 * Zero out the remaining part if this range straddles
3107 * Here we should only zero the range inside the bvec,
3108 * not touch anything else.
3110 * NOTE: i_size is exclusive while end is inclusive.
3112 if (page->index == end_index && i_size <= end) {
3113 u32 zero_start = max(offset_in_page(i_size),
3114 offset_in_page(start));
3116 zero_user_segment(page, zero_start,
3117 offset_in_page(end) + 1);
3120 ASSERT(bio_offset + len > bio_offset);
3123 /* Update page status and unlock */
3124 end_page_read(page, uptodate, start, len);
3125 endio_readpage_release_extent(&processed, BTRFS_I(inode),
3126 start, end, PageUptodate(page));
3128 /* Release the last extent */
3129 endio_readpage_release_extent(&processed, NULL, 0, 0, false);
3130 btrfs_bio_free_csum(bbio);
3135 * Initialize the members up to but not including 'bio'. Use after allocating a
3136 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
3137 * 'bio' because use of __GFP_ZERO is not supported.
3139 static inline void btrfs_bio_init(struct btrfs_bio *bbio)
3141 memset(bbio, 0, offsetof(struct btrfs_bio, bio));
3145 * Allocate a btrfs_io_bio, with @nr_iovecs as maximum number of iovecs.
3147 * The bio allocation is backed by bioset and does not fail.
3149 struct bio *btrfs_bio_alloc(unsigned int nr_iovecs)
3153 ASSERT(0 < nr_iovecs && nr_iovecs <= BIO_MAX_VECS);
3154 bio = bio_alloc_bioset(NULL, nr_iovecs, 0, GFP_NOFS, &btrfs_bioset);
3155 btrfs_bio_init(btrfs_bio(bio));
3159 struct bio *btrfs_bio_clone(struct bio *bio)
3161 struct btrfs_bio *bbio;
3164 /* Bio allocation backed by a bioset does not fail */
3165 new = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOFS, &btrfs_bioset);
3166 bbio = btrfs_bio(new);
3167 btrfs_bio_init(bbio);
3168 bbio->iter = bio->bi_iter;
3172 struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size)
3175 struct btrfs_bio *bbio;
3177 ASSERT(offset <= UINT_MAX && size <= UINT_MAX);
3179 /* this will never fail when it's backed by a bioset */
3180 bio = bio_alloc_clone(orig->bi_bdev, orig, GFP_NOFS, &btrfs_bioset);
3183 bbio = btrfs_bio(bio);
3184 btrfs_bio_init(bbio);
3186 bio_trim(bio, offset >> 9, size >> 9);
3187 bbio->iter = bio->bi_iter;
3192 * Attempt to add a page to bio
3194 * @bio_ctrl: record both the bio, and its bio_flags
3195 * @page: page to add to the bio
3196 * @disk_bytenr: offset of the new bio or to check whether we are adding
3197 * a contiguous page to the previous one
3198 * @size: portion of page that we want to write
3199 * @pg_offset: starting offset in the page
3200 * @bio_flags: flags of the current bio to see if we can merge them
3202 * Attempt to add a page to bio considering stripe alignment etc.
3204 * Return >= 0 for the number of bytes added to the bio.
3205 * Can return 0 if the current bio is already at stripe/zone boundary.
3206 * Return <0 for error.
3208 static int btrfs_bio_add_page(struct btrfs_bio_ctrl *bio_ctrl,
3210 u64 disk_bytenr, unsigned int size,
3211 unsigned int pg_offset,
3212 unsigned long bio_flags)
3214 struct bio *bio = bio_ctrl->bio;
3215 u32 bio_size = bio->bi_iter.bi_size;
3217 const sector_t sector = disk_bytenr >> SECTOR_SHIFT;
3222 /* The limit should be calculated when bio_ctrl->bio is allocated */
3223 ASSERT(bio_ctrl->len_to_oe_boundary && bio_ctrl->len_to_stripe_boundary);
3224 if (bio_ctrl->bio_flags != bio_flags)
3227 if (bio_ctrl->bio_flags & EXTENT_BIO_COMPRESSED)
3228 contig = bio->bi_iter.bi_sector == sector;
3230 contig = bio_end_sector(bio) == sector;
3234 real_size = min(bio_ctrl->len_to_oe_boundary,
3235 bio_ctrl->len_to_stripe_boundary) - bio_size;
3236 real_size = min(real_size, size);
3239 * If real_size is 0, never call bio_add_*_page(), as even size is 0,
3240 * bio will still execute its endio function on the page!
3245 if (bio_op(bio) == REQ_OP_ZONE_APPEND)
3246 ret = bio_add_zone_append_page(bio, page, real_size, pg_offset);
3248 ret = bio_add_page(bio, page, real_size, pg_offset);
3253 static int calc_bio_boundaries(struct btrfs_bio_ctrl *bio_ctrl,
3254 struct btrfs_inode *inode, u64 file_offset)
3256 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3257 struct btrfs_io_geometry geom;
3258 struct btrfs_ordered_extent *ordered;
3259 struct extent_map *em;
3260 u64 logical = (bio_ctrl->bio->bi_iter.bi_sector << SECTOR_SHIFT);
3264 * Pages for compressed extent are never submitted to disk directly,
3265 * thus it has no real boundary, just set them to U32_MAX.
3267 * The split happens for real compressed bio, which happens in
3268 * btrfs_submit_compressed_read/write().
3270 if (bio_ctrl->bio_flags & EXTENT_BIO_COMPRESSED) {
3271 bio_ctrl->len_to_oe_boundary = U32_MAX;
3272 bio_ctrl->len_to_stripe_boundary = U32_MAX;
3275 em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
3278 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio_ctrl->bio),
3280 free_extent_map(em);
3284 if (geom.len > U32_MAX)
3285 bio_ctrl->len_to_stripe_boundary = U32_MAX;
3287 bio_ctrl->len_to_stripe_boundary = (u32)geom.len;
3289 if (bio_op(bio_ctrl->bio) != REQ_OP_ZONE_APPEND) {
3290 bio_ctrl->len_to_oe_boundary = U32_MAX;
3294 /* Ordered extent not yet created, so we're good */
3295 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
3297 bio_ctrl->len_to_oe_boundary = U32_MAX;
3301 bio_ctrl->len_to_oe_boundary = min_t(u32, U32_MAX,
3302 ordered->disk_bytenr + ordered->disk_num_bytes - logical);
3303 btrfs_put_ordered_extent(ordered);
3307 static int alloc_new_bio(struct btrfs_inode *inode,
3308 struct btrfs_bio_ctrl *bio_ctrl,
3309 struct writeback_control *wbc,
3311 bio_end_io_t end_io_func,
3312 u64 disk_bytenr, u32 offset, u64 file_offset,
3313 unsigned long bio_flags)
3315 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3319 bio = btrfs_bio_alloc(BIO_MAX_VECS);
3321 * For compressed page range, its disk_bytenr is always @disk_bytenr
3322 * passed in, no matter if we have added any range into previous bio.
3324 if (bio_flags & EXTENT_BIO_COMPRESSED)
3325 bio->bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
3327 bio->bi_iter.bi_sector = (disk_bytenr + offset) >> SECTOR_SHIFT;
3328 bio_ctrl->bio = bio;
3329 bio_ctrl->bio_flags = bio_flags;
3330 bio->bi_end_io = end_io_func;
3331 bio->bi_private = &inode->io_tree;
3332 bio->bi_write_hint = inode->vfs_inode.i_write_hint;
3334 ret = calc_bio_boundaries(bio_ctrl, inode, file_offset);
3338 struct block_device *bdev;
3340 bdev = fs_info->fs_devices->latest_dev->bdev;
3341 bio_set_dev(bio, bdev);
3342 wbc_init_bio(wbc, bio);
3344 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
3345 struct btrfs_device *device;
3347 device = btrfs_zoned_get_device(fs_info, disk_bytenr,
3348 fs_info->sectorsize);
3349 if (IS_ERR(device)) {
3350 ret = PTR_ERR(device);
3354 btrfs_bio(bio)->device = device;
3358 bio_ctrl->bio = NULL;
3359 bio->bi_status = errno_to_blk_status(ret);
3365 * @opf: bio REQ_OP_* and REQ_* flags as one value
3366 * @wbc: optional writeback control for io accounting
3367 * @page: page to add to the bio
3368 * @disk_bytenr: logical bytenr where the write will be
3369 * @size: portion of page that we want to write to
3370 * @pg_offset: offset of the new bio or to check whether we are adding
3371 * a contiguous page to the previous one
3372 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
3373 * @end_io_func: end_io callback for new bio
3374 * @mirror_num: desired mirror to read/write
3375 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
3376 * @bio_flags: flags of the current bio to see if we can merge them
3378 static int submit_extent_page(unsigned int opf,
3379 struct writeback_control *wbc,
3380 struct btrfs_bio_ctrl *bio_ctrl,
3381 struct page *page, u64 disk_bytenr,
3382 size_t size, unsigned long pg_offset,
3383 bio_end_io_t end_io_func,
3385 unsigned long bio_flags,
3386 bool force_bio_submit)
3389 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
3390 unsigned int cur = pg_offset;
3394 ASSERT(pg_offset < PAGE_SIZE && size <= PAGE_SIZE &&
3395 pg_offset + size <= PAGE_SIZE);
3396 if (force_bio_submit && bio_ctrl->bio) {
3397 ret = submit_one_bio(bio_ctrl->bio, mirror_num, bio_ctrl->bio_flags);
3398 bio_ctrl->bio = NULL;
3403 while (cur < pg_offset + size) {
3404 u32 offset = cur - pg_offset;
3407 /* Allocate new bio if needed */
3408 if (!bio_ctrl->bio) {
3409 ret = alloc_new_bio(inode, bio_ctrl, wbc, opf,
3410 end_io_func, disk_bytenr, offset,
3411 page_offset(page) + cur,
3417 * We must go through btrfs_bio_add_page() to ensure each
3418 * page range won't cross various boundaries.
3420 if (bio_flags & EXTENT_BIO_COMPRESSED)
3421 added = btrfs_bio_add_page(bio_ctrl, page, disk_bytenr,
3422 size - offset, pg_offset + offset,
3425 added = btrfs_bio_add_page(bio_ctrl, page,
3426 disk_bytenr + offset, size - offset,
3427 pg_offset + offset, bio_flags);
3429 /* Metadata page range should never be split */
3430 if (!is_data_inode(&inode->vfs_inode))
3431 ASSERT(added == 0 || added == size - offset);
3433 /* At least we added some page, update the account */
3435 wbc_account_cgroup_owner(wbc, page, added);
3437 /* We have reached boundary, submit right now */
3438 if (added < size - offset) {
3439 /* The bio should contain some page(s) */
3440 ASSERT(bio_ctrl->bio->bi_iter.bi_size);
3441 ret = submit_one_bio(bio_ctrl->bio, mirror_num,
3442 bio_ctrl->bio_flags);
3443 bio_ctrl->bio = NULL;
3452 static int attach_extent_buffer_page(struct extent_buffer *eb,
3454 struct btrfs_subpage *prealloc)
3456 struct btrfs_fs_info *fs_info = eb->fs_info;
3460 * If the page is mapped to btree inode, we should hold the private
3461 * lock to prevent race.
3462 * For cloned or dummy extent buffers, their pages are not mapped and
3463 * will not race with any other ebs.
3466 lockdep_assert_held(&page->mapping->private_lock);
3468 if (fs_info->sectorsize == PAGE_SIZE) {
3469 if (!PagePrivate(page))
3470 attach_page_private(page, eb);
3472 WARN_ON(page->private != (unsigned long)eb);
3476 /* Already mapped, just free prealloc */
3477 if (PagePrivate(page)) {
3478 btrfs_free_subpage(prealloc);
3483 /* Has preallocated memory for subpage */
3484 attach_page_private(page, prealloc);
3486 /* Do new allocation to attach subpage */
3487 ret = btrfs_attach_subpage(fs_info, page,
3488 BTRFS_SUBPAGE_METADATA);
3492 int set_page_extent_mapped(struct page *page)
3494 struct btrfs_fs_info *fs_info;
3496 ASSERT(page->mapping);
3498 if (PagePrivate(page))
3501 fs_info = btrfs_sb(page->mapping->host->i_sb);
3503 if (fs_info->sectorsize < PAGE_SIZE)
3504 return btrfs_attach_subpage(fs_info, page, BTRFS_SUBPAGE_DATA);
3506 attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE);
3510 void clear_page_extent_mapped(struct page *page)
3512 struct btrfs_fs_info *fs_info;
3514 ASSERT(page->mapping);
3516 if (!PagePrivate(page))
3519 fs_info = btrfs_sb(page->mapping->host->i_sb);
3520 if (fs_info->sectorsize < PAGE_SIZE)
3521 return btrfs_detach_subpage(fs_info, page);
3523 detach_page_private(page);
3526 static struct extent_map *
3527 __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
3528 u64 start, u64 len, struct extent_map **em_cached)
3530 struct extent_map *em;
3532 if (em_cached && *em_cached) {
3534 if (extent_map_in_tree(em) && start >= em->start &&
3535 start < extent_map_end(em)) {
3536 refcount_inc(&em->refs);
3540 free_extent_map(em);
3544 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, start, len);
3545 if (em_cached && !IS_ERR(em)) {
3547 refcount_inc(&em->refs);
3553 * basic readpage implementation. Locked extent state structs are inserted
3554 * into the tree that are removed when the IO is done (by the end_io
3556 * XXX JDM: This needs looking at to ensure proper page locking
3557 * return 0 on success, otherwise return error
3559 int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
3560 struct btrfs_bio_ctrl *bio_ctrl,
3561 unsigned int read_flags, u64 *prev_em_start)
3563 struct inode *inode = page->mapping->host;
3564 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3565 u64 start = page_offset(page);
3566 const u64 end = start + PAGE_SIZE - 1;
3569 u64 last_byte = i_size_read(inode);
3572 struct extent_map *em;
3574 size_t pg_offset = 0;
3576 size_t blocksize = inode->i_sb->s_blocksize;
3577 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
3579 ret = set_page_extent_mapped(page);
3581 unlock_extent(tree, start, end);
3582 btrfs_page_set_error(fs_info, page, start, PAGE_SIZE);
3587 if (page->index == last_byte >> PAGE_SHIFT) {
3588 size_t zero_offset = offset_in_page(last_byte);
3591 iosize = PAGE_SIZE - zero_offset;
3592 memzero_page(page, zero_offset, iosize);
3593 flush_dcache_page(page);
3596 begin_page_read(fs_info, page);
3597 while (cur <= end) {
3598 unsigned long this_bio_flag = 0;
3599 bool force_bio_submit = false;
3602 ASSERT(IS_ALIGNED(cur, fs_info->sectorsize));
3603 if (cur >= last_byte) {
3604 struct extent_state *cached = NULL;
3606 iosize = PAGE_SIZE - pg_offset;
3607 memzero_page(page, pg_offset, iosize);
3608 flush_dcache_page(page);
3609 set_extent_uptodate(tree, cur, cur + iosize - 1,
3611 unlock_extent_cached(tree, cur,
3612 cur + iosize - 1, &cached);
3613 end_page_read(page, true, cur, iosize);
3616 em = __get_extent_map(inode, page, pg_offset, cur,
3617 end - cur + 1, em_cached);
3619 unlock_extent(tree, cur, end);
3620 end_page_read(page, false, cur, end + 1 - cur);
3624 extent_offset = cur - em->start;
3625 BUG_ON(extent_map_end(em) <= cur);
3628 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
3629 this_bio_flag |= EXTENT_BIO_COMPRESSED;
3630 extent_set_compress_type(&this_bio_flag,
3634 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3635 cur_end = min(extent_map_end(em) - 1, end);
3636 iosize = ALIGN(iosize, blocksize);
3637 if (this_bio_flag & EXTENT_BIO_COMPRESSED)
3638 disk_bytenr = em->block_start;
3640 disk_bytenr = em->block_start + extent_offset;
3641 block_start = em->block_start;
3642 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3643 block_start = EXTENT_MAP_HOLE;
3646 * If we have a file range that points to a compressed extent
3647 * and it's followed by a consecutive file range that points
3648 * to the same compressed extent (possibly with a different
3649 * offset and/or length, so it either points to the whole extent
3650 * or only part of it), we must make sure we do not submit a
3651 * single bio to populate the pages for the 2 ranges because
3652 * this makes the compressed extent read zero out the pages
3653 * belonging to the 2nd range. Imagine the following scenario:
3656 * [0 - 8K] [8K - 24K]
3659 * points to extent X, points to extent X,
3660 * offset 4K, length of 8K offset 0, length 16K
3662 * [extent X, compressed length = 4K uncompressed length = 16K]
3664 * If the bio to read the compressed extent covers both ranges,
3665 * it will decompress extent X into the pages belonging to the
3666 * first range and then it will stop, zeroing out the remaining
3667 * pages that belong to the other range that points to extent X.
3668 * So here we make sure we submit 2 bios, one for the first
3669 * range and another one for the third range. Both will target
3670 * the same physical extent from disk, but we can't currently
3671 * make the compressed bio endio callback populate the pages
3672 * for both ranges because each compressed bio is tightly
3673 * coupled with a single extent map, and each range can have
3674 * an extent map with a different offset value relative to the
3675 * uncompressed data of our extent and different lengths. This
3676 * is a corner case so we prioritize correctness over
3677 * non-optimal behavior (submitting 2 bios for the same extent).
3679 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3680 prev_em_start && *prev_em_start != (u64)-1 &&
3681 *prev_em_start != em->start)
3682 force_bio_submit = true;
3685 *prev_em_start = em->start;
3687 free_extent_map(em);
3690 /* we've found a hole, just zero and go on */
3691 if (block_start == EXTENT_MAP_HOLE) {
3692 struct extent_state *cached = NULL;
3694 memzero_page(page, pg_offset, iosize);
3695 flush_dcache_page(page);
3697 set_extent_uptodate(tree, cur, cur + iosize - 1,
3699 unlock_extent_cached(tree, cur,
3700 cur + iosize - 1, &cached);
3701 end_page_read(page, true, cur, iosize);
3703 pg_offset += iosize;
3706 /* the get_extent function already copied into the page */
3707 if (test_range_bit(tree, cur, cur_end,
3708 EXTENT_UPTODATE, 1, NULL)) {
3709 unlock_extent(tree, cur, cur + iosize - 1);
3710 end_page_read(page, true, cur, iosize);
3712 pg_offset += iosize;
3715 /* we have an inline extent but it didn't get marked up
3716 * to date. Error out
3718 if (block_start == EXTENT_MAP_INLINE) {
3719 unlock_extent(tree, cur, cur + iosize - 1);
3720 end_page_read(page, false, cur, iosize);
3722 pg_offset += iosize;
3726 ret = submit_extent_page(REQ_OP_READ | read_flags, NULL,
3727 bio_ctrl, page, disk_bytenr, iosize,
3729 end_bio_extent_readpage, 0,
3733 unlock_extent(tree, cur, cur + iosize - 1);
3734 end_page_read(page, false, cur, iosize);
3738 pg_offset += iosize;
3744 static inline void contiguous_readpages(struct page *pages[], int nr_pages,
3746 struct extent_map **em_cached,
3747 struct btrfs_bio_ctrl *bio_ctrl,
3750 struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);
3753 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
3755 for (index = 0; index < nr_pages; index++) {
3756 btrfs_do_readpage(pages[index], em_cached, bio_ctrl,
3757 REQ_RAHEAD, prev_em_start);
3758 put_page(pages[index]);
3762 static void update_nr_written(struct writeback_control *wbc,
3763 unsigned long nr_written)
3765 wbc->nr_to_write -= nr_written;
3769 * helper for __extent_writepage, doing all of the delayed allocation setup.
3771 * This returns 1 if btrfs_run_delalloc_range function did all the work required
3772 * to write the page (copy into inline extent). In this case the IO has
3773 * been started and the page is already unlocked.
3775 * This returns 0 if all went well (page still locked)
3776 * This returns < 0 if there were errors (page still locked)
3778 static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
3779 struct page *page, struct writeback_control *wbc)
3781 const u64 page_end = page_offset(page) + PAGE_SIZE - 1;
3782 u64 delalloc_start = page_offset(page);
3783 u64 delalloc_to_write = 0;
3784 /* How many pages are started by btrfs_run_delalloc_range() */
3785 unsigned long nr_written = 0;
3787 int page_started = 0;
3789 while (delalloc_start < page_end) {
3790 u64 delalloc_end = page_end;
3793 found = find_lock_delalloc_range(&inode->vfs_inode, page,
3797 delalloc_start = delalloc_end + 1;
3800 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3801 delalloc_end, &page_started, &nr_written, wbc);
3803 btrfs_page_set_error(inode->root->fs_info, page,
3804 page_offset(page), PAGE_SIZE);
3808 * delalloc_end is already one less than the total length, so
3809 * we don't subtract one from PAGE_SIZE
3811 delalloc_to_write += (delalloc_end - delalloc_start +
3812 PAGE_SIZE) >> PAGE_SHIFT;
3813 delalloc_start = delalloc_end + 1;
3815 if (wbc->nr_to_write < delalloc_to_write) {
3818 if (delalloc_to_write < thresh * 2)
3819 thresh = delalloc_to_write;
3820 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3824 /* Did btrfs_run_dealloc_range() already unlock and start the IO? */
3827 * We've unlocked the page, so we can't update the mapping's
3828 * writeback index, just update nr_to_write.
3830 wbc->nr_to_write -= nr_written;
3838 * Find the first byte we need to write.
3840 * For subpage, one page can contain several sectors, and
3841 * __extent_writepage_io() will just grab all extent maps in the page
3842 * range and try to submit all non-inline/non-compressed extents.
3844 * This is a big problem for subpage, we shouldn't re-submit already written
3846 * This function will lookup subpage dirty bit to find which range we really
3849 * Return the next dirty range in [@start, @end).
3850 * If no dirty range is found, @start will be page_offset(page) + PAGE_SIZE.
3852 static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
3853 struct page *page, u64 *start, u64 *end)
3855 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
3856 struct btrfs_subpage_info *spi = fs_info->subpage_info;
3857 u64 orig_start = *start;
3858 /* Declare as unsigned long so we can use bitmap ops */
3859 unsigned long flags;
3860 int range_start_bit;
3864 * For regular sector size == page size case, since one page only
3865 * contains one sector, we return the page offset directly.
3867 if (fs_info->sectorsize == PAGE_SIZE) {
3868 *start = page_offset(page);
3869 *end = page_offset(page) + PAGE_SIZE;
3873 range_start_bit = spi->dirty_offset +
3874 (offset_in_page(orig_start) >> fs_info->sectorsize_bits);
3876 /* We should have the page locked, but just in case */
3877 spin_lock_irqsave(&subpage->lock, flags);
3878 bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit,
3879 spi->dirty_offset + spi->bitmap_nr_bits);
3880 spin_unlock_irqrestore(&subpage->lock, flags);
3882 range_start_bit -= spi->dirty_offset;
3883 range_end_bit -= spi->dirty_offset;
3885 *start = page_offset(page) + range_start_bit * fs_info->sectorsize;
3886 *end = page_offset(page) + range_end_bit * fs_info->sectorsize;
3890 * helper for __extent_writepage. This calls the writepage start hooks,
3891 * and does the loop to map the page into extents and bios.
3893 * We return 1 if the IO is started and the page is unlocked,
3894 * 0 if all went well (page still locked)
3895 * < 0 if there were errors (page still locked)
3897 static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
3899 struct writeback_control *wbc,
3900 struct extent_page_data *epd,
3904 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3905 u64 cur = page_offset(page);
3906 u64 end = cur + PAGE_SIZE - 1;
3909 struct extent_map *em;
3912 u32 opf = REQ_OP_WRITE;
3913 const unsigned int write_flags = wbc_to_write_flags(wbc);
3916 ret = btrfs_writepage_cow_fixup(page);
3918 /* Fixup worker will requeue */
3919 redirty_page_for_writepage(wbc, page);
3925 * we don't want to touch the inode after unlocking the page,
3926 * so we update the mapping writeback index now
3928 update_nr_written(wbc, 1);
3930 while (cur <= end) {
3933 u64 dirty_range_start = cur;
3934 u64 dirty_range_end;
3937 if (cur >= i_size) {
3938 btrfs_writepage_endio_finish_ordered(inode, page, cur,
3941 * This range is beyond i_size, thus we don't need to
3942 * bother writing back.
3943 * But we still need to clear the dirty subpage bit, or
3944 * the next time the page gets dirtied, we will try to
3945 * writeback the sectors with subpage dirty bits,
3946 * causing writeback without ordered extent.
3948 btrfs_page_clear_dirty(fs_info, page, cur, end + 1 - cur);
3952 find_next_dirty_byte(fs_info, page, &dirty_range_start,
3954 if (cur < dirty_range_start) {
3955 cur = dirty_range_start;
3959 em = btrfs_get_extent(inode, NULL, 0, cur, end - cur + 1);
3961 btrfs_page_set_error(fs_info, page, cur, end - cur + 1);
3962 ret = PTR_ERR_OR_ZERO(em);
3966 extent_offset = cur - em->start;
3967 em_end = extent_map_end(em);
3968 ASSERT(cur <= em_end);
3970 ASSERT(IS_ALIGNED(em->start, fs_info->sectorsize));
3971 ASSERT(IS_ALIGNED(em->len, fs_info->sectorsize));
3972 block_start = em->block_start;
3973 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3974 disk_bytenr = em->block_start + extent_offset;
3977 * Note that em_end from extent_map_end() and dirty_range_end from
3978 * find_next_dirty_byte() are all exclusive
3980 iosize = min(min(em_end, end + 1), dirty_range_end) - cur;
3982 if (btrfs_use_zone_append(inode, em->block_start))
3983 opf = REQ_OP_ZONE_APPEND;
3985 free_extent_map(em);
3989 * compressed and inline extents are written through other
3992 if (compressed || block_start == EXTENT_MAP_HOLE ||
3993 block_start == EXTENT_MAP_INLINE) {
3997 btrfs_writepage_endio_finish_ordered(inode,
3998 page, cur, cur + iosize - 1, true);
3999 btrfs_page_clear_dirty(fs_info, page, cur, iosize);
4004 btrfs_set_range_writeback(inode, cur, cur + iosize - 1);
4005 if (!PageWriteback(page)) {
4006 btrfs_err(inode->root->fs_info,
4007 "page %lu not writeback, cur %llu end %llu",
4008 page->index, cur, end);
4012 * Although the PageDirty bit is cleared before entering this
4013 * function, subpage dirty bit is not cleared.
4014 * So clear subpage dirty bit here so next time we won't submit
4015 * page for range already written to disk.
4017 btrfs_page_clear_dirty(fs_info, page, cur, iosize);
4019 ret = submit_extent_page(opf | write_flags, wbc,
4020 &epd->bio_ctrl, page,
4021 disk_bytenr, iosize,
4022 cur - page_offset(page),
4023 end_bio_extent_writepage,
4026 btrfs_page_set_error(fs_info, page, cur, iosize);
4027 if (PageWriteback(page))
4028 btrfs_page_clear_writeback(fs_info, page, cur,
4036 * If we finish without problem, we should not only clear page dirty,
4037 * but also empty subpage dirty bits
4040 btrfs_page_assert_not_dirty(fs_info, page);
4046 * the writepage semantics are similar to regular writepage. extent
4047 * records are inserted to lock ranges in the tree, and as dirty areas
4048 * are found, they are marked writeback. Then the lock bits are removed
4049 * and the end_io handler clears the writeback ranges
4051 * Return 0 if everything goes well.
4052 * Return <0 for error.
4054 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
4055 struct extent_page_data *epd)
4057 struct folio *folio = page_folio(page);
4058 struct inode *inode = page->mapping->host;
4059 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4060 const u64 page_start = page_offset(page);
4061 const u64 page_end = page_start + PAGE_SIZE - 1;
4065 loff_t i_size = i_size_read(inode);
4066 unsigned long end_index = i_size >> PAGE_SHIFT;
4068 trace___extent_writepage(page, inode, wbc);
4070 WARN_ON(!PageLocked(page));
4072 btrfs_page_clear_error(btrfs_sb(inode->i_sb), page,
4073 page_offset(page), PAGE_SIZE);
4075 pg_offset = offset_in_page(i_size);
4076 if (page->index > end_index ||
4077 (page->index == end_index && !pg_offset)) {
4078 folio_invalidate(folio, 0, folio_size(folio));
4079 folio_unlock(folio);
4083 if (page->index == end_index) {
4084 memzero_page(page, pg_offset, PAGE_SIZE - pg_offset);
4085 flush_dcache_page(page);
4088 ret = set_page_extent_mapped(page);
4094 if (!epd->extent_locked) {
4095 ret = writepage_delalloc(BTRFS_I(inode), page, wbc);
4102 ret = __extent_writepage_io(BTRFS_I(inode), page, wbc, epd, i_size,
4109 /* make sure the mapping tag for page dirty gets cleared */
4110 set_page_writeback(page);
4111 end_page_writeback(page);
4114 * Here we used to have a check for PageError() and then set @ret and
4115 * call end_extent_writepage().
4117 * But in fact setting @ret here will cause different error paths
4118 * between subpage and regular sectorsize.
4120 * For regular page size, we never submit current page, but only add
4121 * current page to current bio.
4122 * The bio submission can only happen in next page.
4123 * Thus if we hit the PageError() branch, @ret is already set to
4124 * non-zero value and will not get updated for regular sectorsize.
4126 * But for subpage case, it's possible we submit part of current page,
4127 * thus can get PageError() set by submitted bio of the same page,
4128 * while our @ret is still 0.
4130 * So here we unify the behavior and don't set @ret.
4131 * Error can still be properly passed to higher layer as page will
4132 * be set error, here we just don't handle the IO failure.
4134 * NOTE: This is just a hotfix for subpage.
4135 * The root fix will be properly ending ordered extent when we hit
4136 * an error during writeback.
4138 * But that needs a bigger refactoring, as we not only need to grab the
4139 * submitted OE, but also need to know exactly at which bytenr we hit
4141 * Currently the full page based __extent_writepage_io() is not
4144 if (PageError(page))
4145 end_extent_writepage(page, ret, page_start, page_end);
4146 if (epd->extent_locked) {
4148 * If epd->extent_locked, it's from extent_write_locked_range(),
4149 * the page can either be locked by lock_page() or
4150 * process_one_page().
4151 * Let btrfs_page_unlock_writer() handle both cases.
4154 btrfs_page_unlock_writer(fs_info, page, wbc->range_start,
4155 wbc->range_end + 1 - wbc->range_start);
4163 void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
4165 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
4166 TASK_UNINTERRUPTIBLE);
4169 static void end_extent_buffer_writeback(struct extent_buffer *eb)
4171 if (test_bit(EXTENT_BUFFER_ZONE_FINISH, &eb->bflags))
4172 btrfs_zone_finish_endio(eb->fs_info, eb->start, eb->len);
4174 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
4175 smp_mb__after_atomic();
4176 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
4180 * Lock extent buffer status and pages for writeback.
4182 * May try to flush write bio if we can't get the lock.
4184 * Return 0 if the extent buffer doesn't need to be submitted.
4185 * (E.g. the extent buffer is not dirty)
4186 * Return >0 is the extent buffer is submitted to bio.
4187 * Return <0 if something went wrong, no page is locked.
4189 static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
4190 struct extent_page_data *epd)
4192 struct btrfs_fs_info *fs_info = eb->fs_info;
4193 int i, num_pages, failed_page_nr;
4197 if (!btrfs_try_tree_write_lock(eb)) {
4198 ret = flush_write_bio(epd);
4202 btrfs_tree_lock(eb);
4205 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
4206 btrfs_tree_unlock(eb);
4210 ret = flush_write_bio(epd);
4216 wait_on_extent_buffer_writeback(eb);
4217 btrfs_tree_lock(eb);
4218 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
4220 btrfs_tree_unlock(eb);
4225 * We need to do this to prevent races in people who check if the eb is
4226 * under IO since we can end up having no IO bits set for a short period
4229 spin_lock(&eb->refs_lock);
4230 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4231 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
4232 spin_unlock(&eb->refs_lock);
4233 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
4234 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
4236 fs_info->dirty_metadata_batch);
4239 spin_unlock(&eb->refs_lock);
4242 btrfs_tree_unlock(eb);
4245 * Either we don't need to submit any tree block, or we're submitting
4247 * Subpage metadata doesn't use page locking at all, so we can skip
4250 if (!ret || fs_info->sectorsize < PAGE_SIZE)
4253 num_pages = num_extent_pages(eb);
4254 for (i = 0; i < num_pages; i++) {
4255 struct page *p = eb->pages[i];
4257 if (!trylock_page(p)) {
4261 err = flush_write_bio(epd);
4275 /* Unlock already locked pages */
4276 for (i = 0; i < failed_page_nr; i++)
4277 unlock_page(eb->pages[i]);
4279 * Clear EXTENT_BUFFER_WRITEBACK and wake up anyone waiting on it.
4280 * Also set back EXTENT_BUFFER_DIRTY so future attempts to this eb can
4281 * be made and undo everything done before.
4283 btrfs_tree_lock(eb);
4284 spin_lock(&eb->refs_lock);
4285 set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4286 end_extent_buffer_writeback(eb);
4287 spin_unlock(&eb->refs_lock);
4288 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, eb->len,
4289 fs_info->dirty_metadata_batch);
4290 btrfs_clear_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
4291 btrfs_tree_unlock(eb);
4295 static void set_btree_ioerr(struct page *page, struct extent_buffer *eb)
4297 struct btrfs_fs_info *fs_info = eb->fs_info;
4299 btrfs_page_set_error(fs_info, page, eb->start, eb->len);
4300 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
4304 * A read may stumble upon this buffer later, make sure that it gets an
4305 * error and knows there was an error.
4307 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4310 * We need to set the mapping with the io error as well because a write
4311 * error will flip the file system readonly, and then syncfs() will
4312 * return a 0 because we are readonly if we don't modify the err seq for
4315 mapping_set_error(page->mapping, -EIO);
4318 * If we error out, we should add back the dirty_metadata_bytes
4319 * to make it consistent.
4321 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
4322 eb->len, fs_info->dirty_metadata_batch);
4325 * If writeback for a btree extent that doesn't belong to a log tree
4326 * failed, increment the counter transaction->eb_write_errors.
4327 * We do this because while the transaction is running and before it's
4328 * committing (when we call filemap_fdata[write|wait]_range against
4329 * the btree inode), we might have
4330 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
4331 * returns an error or an error happens during writeback, when we're
4332 * committing the transaction we wouldn't know about it, since the pages
4333 * can be no longer dirty nor marked anymore for writeback (if a
4334 * subsequent modification to the extent buffer didn't happen before the
4335 * transaction commit), which makes filemap_fdata[write|wait]_range not
4336 * able to find the pages tagged with SetPageError at transaction
4337 * commit time. So if this happens we must abort the transaction,
4338 * otherwise we commit a super block with btree roots that point to
4339 * btree nodes/leafs whose content on disk is invalid - either garbage
4340 * or the content of some node/leaf from a past generation that got
4341 * cowed or deleted and is no longer valid.
4343 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
4344 * not be enough - we need to distinguish between log tree extents vs
4345 * non-log tree extents, and the next filemap_fdatawait_range() call
4346 * will catch and clear such errors in the mapping - and that call might
4347 * be from a log sync and not from a transaction commit. Also, checking
4348 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
4349 * not done and would not be reliable - the eb might have been released
4350 * from memory and reading it back again means that flag would not be
4351 * set (since it's a runtime flag, not persisted on disk).
4353 * Using the flags below in the btree inode also makes us achieve the
4354 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
4355 * writeback for all dirty pages and before filemap_fdatawait_range()
4356 * is called, the writeback for all dirty pages had already finished
4357 * with errors - because we were not using AS_EIO/AS_ENOSPC,
4358 * filemap_fdatawait_range() would return success, as it could not know
4359 * that writeback errors happened (the pages were no longer tagged for
4362 switch (eb->log_index) {
4364 set_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags);
4367 set_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
4370 set_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
4373 BUG(); /* unexpected, logic error */
4378 * The endio specific version which won't touch any unsafe spinlock in endio
4381 static struct extent_buffer *find_extent_buffer_nolock(
4382 struct btrfs_fs_info *fs_info, u64 start)
4384 struct extent_buffer *eb;
4387 eb = radix_tree_lookup(&fs_info->buffer_radix,
4388 start >> fs_info->sectorsize_bits);
4389 if (eb && atomic_inc_not_zero(&eb->refs)) {
4398 * The endio function for subpage extent buffer write.
4400 * Unlike end_bio_extent_buffer_writepage(), we only call end_page_writeback()
4401 * after all extent buffers in the page has finished their writeback.
4403 static void end_bio_subpage_eb_writepage(struct bio *bio)
4405 struct btrfs_fs_info *fs_info;
4406 struct bio_vec *bvec;
4407 struct bvec_iter_all iter_all;
4409 fs_info = btrfs_sb(bio_first_page_all(bio)->mapping->host->i_sb);
4410 ASSERT(fs_info->sectorsize < PAGE_SIZE);
4412 ASSERT(!bio_flagged(bio, BIO_CLONED));
4413 bio_for_each_segment_all(bvec, bio, iter_all) {
4414 struct page *page = bvec->bv_page;
4415 u64 bvec_start = page_offset(page) + bvec->bv_offset;
4416 u64 bvec_end = bvec_start + bvec->bv_len - 1;
4417 u64 cur_bytenr = bvec_start;
4419 ASSERT(IS_ALIGNED(bvec->bv_len, fs_info->nodesize));
4421 /* Iterate through all extent buffers in the range */
4422 while (cur_bytenr <= bvec_end) {
4423 struct extent_buffer *eb;
4427 * Here we can't use find_extent_buffer(), as it may
4428 * try to lock eb->refs_lock, which is not safe in endio
4431 eb = find_extent_buffer_nolock(fs_info, cur_bytenr);
4434 cur_bytenr = eb->start + eb->len;
4436 ASSERT(test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags));
4437 done = atomic_dec_and_test(&eb->io_pages);
4440 if (bio->bi_status ||
4441 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
4442 ClearPageUptodate(page);
4443 set_btree_ioerr(page, eb);
4446 btrfs_subpage_clear_writeback(fs_info, page, eb->start,
4448 end_extent_buffer_writeback(eb);
4450 * free_extent_buffer() will grab spinlock which is not
4451 * safe in endio context. Thus here we manually dec
4454 atomic_dec(&eb->refs);
4460 static void end_bio_extent_buffer_writepage(struct bio *bio)
4462 struct bio_vec *bvec;
4463 struct extent_buffer *eb;
4465 struct bvec_iter_all iter_all;
4467 ASSERT(!bio_flagged(bio, BIO_CLONED));
4468 bio_for_each_segment_all(bvec, bio, iter_all) {
4469 struct page *page = bvec->bv_page;
4471 eb = (struct extent_buffer *)page->private;
4473 done = atomic_dec_and_test(&eb->io_pages);
4475 if (bio->bi_status ||
4476 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
4477 ClearPageUptodate(page);
4478 set_btree_ioerr(page, eb);
4481 end_page_writeback(page);
4486 end_extent_buffer_writeback(eb);
4492 static void prepare_eb_write(struct extent_buffer *eb)
4495 unsigned long start;
4498 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
4499 atomic_set(&eb->io_pages, num_extent_pages(eb));
4501 /* Set btree blocks beyond nritems with 0 to avoid stale content */
4502 nritems = btrfs_header_nritems(eb);
4503 if (btrfs_header_level(eb) > 0) {
4504 end = btrfs_node_key_ptr_offset(nritems);
4505 memzero_extent_buffer(eb, end, eb->len - end);
4509 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
4511 start = btrfs_item_nr_offset(nritems);
4512 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
4513 memzero_extent_buffer(eb, start, end - start);
4518 * Unlike the work in write_one_eb(), we rely completely on extent locking.
4519 * Page locking is only utilized at minimum to keep the VMM code happy.
4521 static int write_one_subpage_eb(struct extent_buffer *eb,
4522 struct writeback_control *wbc,
4523 struct extent_page_data *epd)
4525 struct btrfs_fs_info *fs_info = eb->fs_info;
4526 struct page *page = eb->pages[0];
4527 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
4528 bool no_dirty_ebs = false;
4531 prepare_eb_write(eb);
4533 /* clear_page_dirty_for_io() in subpage helper needs page locked */
4535 btrfs_subpage_set_writeback(fs_info, page, eb->start, eb->len);
4537 /* Check if this is the last dirty bit to update nr_written */
4538 no_dirty_ebs = btrfs_subpage_clear_and_test_dirty(fs_info, page,
4539 eb->start, eb->len);
4541 clear_page_dirty_for_io(page);
4543 ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
4544 &epd->bio_ctrl, page, eb->start, eb->len,
4545 eb->start - page_offset(page),
4546 end_bio_subpage_eb_writepage, 0, 0, false);
4548 btrfs_subpage_clear_writeback(fs_info, page, eb->start, eb->len);
4549 set_btree_ioerr(page, eb);
4552 if (atomic_dec_and_test(&eb->io_pages))
4553 end_extent_buffer_writeback(eb);
4558 * Submission finished without problem, if no range of the page is
4559 * dirty anymore, we have submitted a page. Update nr_written in wbc.
4562 update_nr_written(wbc, 1);
4566 static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
4567 struct writeback_control *wbc,
4568 struct extent_page_data *epd)
4570 u64 disk_bytenr = eb->start;
4572 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
4575 prepare_eb_write(eb);
4577 num_pages = num_extent_pages(eb);
4578 for (i = 0; i < num_pages; i++) {
4579 struct page *p = eb->pages[i];
4581 clear_page_dirty_for_io(p);
4582 set_page_writeback(p);
4583 ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
4584 &epd->bio_ctrl, p, disk_bytenr,
4586 end_bio_extent_buffer_writepage,
4589 set_btree_ioerr(p, eb);
4590 if (PageWriteback(p))
4591 end_page_writeback(p);
4592 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
4593 end_extent_buffer_writeback(eb);
4597 disk_bytenr += PAGE_SIZE;
4598 update_nr_written(wbc, 1);
4602 if (unlikely(ret)) {
4603 for (; i < num_pages; i++) {
4604 struct page *p = eb->pages[i];
4605 clear_page_dirty_for_io(p);
4614 * Submit one subpage btree page.
4616 * The main difference to submit_eb_page() is:
4618 * For subpage, we don't rely on page locking at all.
4621 * We only flush bio if we may be unable to fit current extent buffers into
4624 * Return >=0 for the number of submitted extent buffers.
4625 * Return <0 for fatal error.
4627 static int submit_eb_subpage(struct page *page,
4628 struct writeback_control *wbc,
4629 struct extent_page_data *epd)
4631 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
4633 u64 page_start = page_offset(page);
4635 int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits;
4638 /* Lock and write each dirty extent buffers in the range */
4639 while (bit_start < fs_info->subpage_info->bitmap_nr_bits) {
4640 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
4641 struct extent_buffer *eb;
4642 unsigned long flags;
4646 * Take private lock to ensure the subpage won't be detached
4649 spin_lock(&page->mapping->private_lock);
4650 if (!PagePrivate(page)) {
4651 spin_unlock(&page->mapping->private_lock);
4654 spin_lock_irqsave(&subpage->lock, flags);
4655 if (!test_bit(bit_start + fs_info->subpage_info->dirty_offset,
4656 subpage->bitmaps)) {
4657 spin_unlock_irqrestore(&subpage->lock, flags);
4658 spin_unlock(&page->mapping->private_lock);
4663 start = page_start + bit_start * fs_info->sectorsize;
4664 bit_start += sectors_per_node;
4667 * Here we just want to grab the eb without touching extra
4668 * spin locks, so call find_extent_buffer_nolock().
4670 eb = find_extent_buffer_nolock(fs_info, start);
4671 spin_unlock_irqrestore(&subpage->lock, flags);
4672 spin_unlock(&page->mapping->private_lock);
4675 * The eb has already reached 0 refs thus find_extent_buffer()
4676 * doesn't return it. We don't need to write back such eb
4682 ret = lock_extent_buffer_for_io(eb, epd);
4684 free_extent_buffer(eb);
4688 free_extent_buffer(eb);
4691 ret = write_one_subpage_eb(eb, wbc, epd);
4692 free_extent_buffer(eb);
4700 /* We hit error, end bio for the submitted extent buffers */
4701 end_write_bio(epd, ret);
4706 * Submit all page(s) of one extent buffer.
4708 * @page: the page of one extent buffer
4709 * @eb_context: to determine if we need to submit this page, if current page
4710 * belongs to this eb, we don't need to submit
4712 * The caller should pass each page in their bytenr order, and here we use
4713 * @eb_context to determine if we have submitted pages of one extent buffer.
4715 * If we have, we just skip until we hit a new page that doesn't belong to
4716 * current @eb_context.
4718 * If not, we submit all the page(s) of the extent buffer.
4720 * Return >0 if we have submitted the extent buffer successfully.
4721 * Return 0 if we don't need to submit the page, as it's already submitted by
4723 * Return <0 for fatal error.
4725 static int submit_eb_page(struct page *page, struct writeback_control *wbc,
4726 struct extent_page_data *epd,
4727 struct extent_buffer **eb_context)
4729 struct address_space *mapping = page->mapping;
4730 struct btrfs_block_group *cache = NULL;
4731 struct extent_buffer *eb;
4734 if (!PagePrivate(page))
4737 if (btrfs_sb(page->mapping->host->i_sb)->sectorsize < PAGE_SIZE)
4738 return submit_eb_subpage(page, wbc, epd);
4740 spin_lock(&mapping->private_lock);
4741 if (!PagePrivate(page)) {
4742 spin_unlock(&mapping->private_lock);
4746 eb = (struct extent_buffer *)page->private;
4749 * Shouldn't happen and normally this would be a BUG_ON but no point
4750 * crashing the machine for something we can survive anyway.
4753 spin_unlock(&mapping->private_lock);
4757 if (eb == *eb_context) {
4758 spin_unlock(&mapping->private_lock);
4761 ret = atomic_inc_not_zero(&eb->refs);
4762 spin_unlock(&mapping->private_lock);
4766 if (!btrfs_check_meta_write_pointer(eb->fs_info, eb, &cache)) {
4768 * If for_sync, this hole will be filled with
4769 * trasnsaction commit.
4771 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
4775 free_extent_buffer(eb);
4781 ret = lock_extent_buffer_for_io(eb, epd);
4783 btrfs_revert_meta_write_pointer(cache, eb);
4785 btrfs_put_block_group(cache);
4786 free_extent_buffer(eb);
4791 * Implies write in zoned mode. Mark the last eb in a block group.
4793 if (cache->seq_zone && eb->start + eb->len == cache->zone_capacity)
4794 set_bit(EXTENT_BUFFER_ZONE_FINISH, &eb->bflags);
4795 btrfs_put_block_group(cache);
4797 ret = write_one_eb(eb, wbc, epd);
4798 free_extent_buffer(eb);
4804 int btree_write_cache_pages(struct address_space *mapping,
4805 struct writeback_control *wbc)
4807 struct extent_buffer *eb_context = NULL;
4808 struct extent_page_data epd = {
4811 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
4813 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
4816 int nr_to_write_done = 0;
4817 struct pagevec pvec;
4820 pgoff_t end; /* Inclusive */
4824 pagevec_init(&pvec);
4825 if (wbc->range_cyclic) {
4826 index = mapping->writeback_index; /* Start from prev offset */
4829 * Start from the beginning does not need to cycle over the
4830 * range, mark it as scanned.
4832 scanned = (index == 0);
4834 index = wbc->range_start >> PAGE_SHIFT;
4835 end = wbc->range_end >> PAGE_SHIFT;
4838 if (wbc->sync_mode == WB_SYNC_ALL)
4839 tag = PAGECACHE_TAG_TOWRITE;
4841 tag = PAGECACHE_TAG_DIRTY;
4842 btrfs_zoned_meta_io_lock(fs_info);
4844 if (wbc->sync_mode == WB_SYNC_ALL)
4845 tag_pages_for_writeback(mapping, index, end);
4846 while (!done && !nr_to_write_done && (index <= end) &&
4847 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
4851 for (i = 0; i < nr_pages; i++) {
4852 struct page *page = pvec.pages[i];
4854 ret = submit_eb_page(page, wbc, &epd, &eb_context);
4863 * the filesystem may choose to bump up nr_to_write.
4864 * We have to make sure to honor the new nr_to_write
4867 nr_to_write_done = wbc->nr_to_write <= 0;
4869 pagevec_release(&pvec);
4872 if (!scanned && !done) {
4874 * We hit the last page and there is more work to be done: wrap
4875 * back to the start of the file
4882 end_write_bio(&epd, ret);
4886 * If something went wrong, don't allow any metadata write bio to be
4889 * This would prevent use-after-free if we had dirty pages not
4890 * cleaned up, which can still happen by fuzzed images.
4893 * Allowing existing tree block to be allocated for other trees.
4895 * - Log tree operations
4896 * Exiting tree blocks get allocated to log tree, bumps its
4897 * generation, then get cleaned in tree re-balance.
4898 * Such tree block will not be written back, since it's clean,
4899 * thus no WRITTEN flag set.
4900 * And after log writes back, this tree block is not traced by
4901 * any dirty extent_io_tree.
4903 * - Offending tree block gets re-dirtied from its original owner
4904 * Since it has bumped generation, no WRITTEN flag, it can be
4905 * reused without COWing. This tree block will not be traced
4906 * by btrfs_transaction::dirty_pages.
4908 * Now such dirty tree block will not be cleaned by any dirty
4909 * extent io tree. Thus we don't want to submit such wild eb
4910 * if the fs already has error.
4912 if (!BTRFS_FS_ERROR(fs_info)) {
4913 ret = flush_write_bio(&epd);
4916 end_write_bio(&epd, ret);
4919 btrfs_zoned_meta_io_unlock(fs_info);
4924 * Walk the list of dirty pages of the given address space and write all of them.
4926 * @mapping: address space structure to write
4927 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
4928 * @epd: holds context for the write, namely the bio
4930 * If a page is already under I/O, write_cache_pages() skips it, even
4931 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
4932 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
4933 * and msync() need to guarantee that all the data which was dirty at the time
4934 * the call was made get new I/O started against them. If wbc->sync_mode is
4935 * WB_SYNC_ALL then we were called for data integrity and we must wait for
4936 * existing IO to complete.
4938 static int extent_write_cache_pages(struct address_space *mapping,
4939 struct writeback_control *wbc,
4940 struct extent_page_data *epd)
4942 struct inode *inode = mapping->host;
4945 int nr_to_write_done = 0;
4946 struct pagevec pvec;
4949 pgoff_t end; /* Inclusive */
4951 int range_whole = 0;
4956 * We have to hold onto the inode so that ordered extents can do their
4957 * work when the IO finishes. The alternative to this is failing to add
4958 * an ordered extent if the igrab() fails there and that is a huge pain
4959 * to deal with, so instead just hold onto the inode throughout the
4960 * writepages operation. If it fails here we are freeing up the inode
4961 * anyway and we'd rather not waste our time writing out stuff that is
4962 * going to be truncated anyway.
4967 pagevec_init(&pvec);
4968 if (wbc->range_cyclic) {
4969 index = mapping->writeback_index; /* Start from prev offset */
4972 * Start from the beginning does not need to cycle over the
4973 * range, mark it as scanned.
4975 scanned = (index == 0);
4977 index = wbc->range_start >> PAGE_SHIFT;
4978 end = wbc->range_end >> PAGE_SHIFT;
4979 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
4985 * We do the tagged writepage as long as the snapshot flush bit is set
4986 * and we are the first one who do the filemap_flush() on this inode.
4988 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
4989 * not race in and drop the bit.
4991 if (range_whole && wbc->nr_to_write == LONG_MAX &&
4992 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
4993 &BTRFS_I(inode)->runtime_flags))
4994 wbc->tagged_writepages = 1;
4996 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
4997 tag = PAGECACHE_TAG_TOWRITE;
4999 tag = PAGECACHE_TAG_DIRTY;
5001 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
5002 tag_pages_for_writeback(mapping, index, end);
5004 while (!done && !nr_to_write_done && (index <= end) &&
5005 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
5006 &index, end, tag))) {
5009 for (i = 0; i < nr_pages; i++) {
5010 struct page *page = pvec.pages[i];
5012 done_index = page->index + 1;
5014 * At this point we hold neither the i_pages lock nor
5015 * the page lock: the page may be truncated or
5016 * invalidated (changing page->mapping to NULL),
5017 * or even swizzled back from swapper_space to
5018 * tmpfs file mapping
5020 if (!trylock_page(page)) {
5021 ret = flush_write_bio(epd);
5026 if (unlikely(page->mapping != mapping)) {
5031 if (wbc->sync_mode != WB_SYNC_NONE) {
5032 if (PageWriteback(page)) {
5033 ret = flush_write_bio(epd);
5036 wait_on_page_writeback(page);
5039 if (PageWriteback(page) ||
5040 !clear_page_dirty_for_io(page)) {
5045 ret = __extent_writepage(page, wbc, epd);
5052 * the filesystem may choose to bump up nr_to_write.
5053 * We have to make sure to honor the new nr_to_write
5056 nr_to_write_done = wbc->nr_to_write <= 0;
5058 pagevec_release(&pvec);
5061 if (!scanned && !done) {
5063 * We hit the last page and there is more work to be done: wrap
5064 * back to the start of the file
5070 * If we're looping we could run into a page that is locked by a
5071 * writer and that writer could be waiting on writeback for a
5072 * page in our current bio, and thus deadlock, so flush the
5075 ret = flush_write_bio(epd);
5080 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
5081 mapping->writeback_index = done_index;
5083 btrfs_add_delayed_iput(inode);
5087 int extent_write_full_page(struct page *page, struct writeback_control *wbc)
5090 struct extent_page_data epd = {
5093 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
5096 ret = __extent_writepage(page, wbc, &epd);
5099 end_write_bio(&epd, ret);
5103 ret = flush_write_bio(&epd);
5109 * Submit the pages in the range to bio for call sites which delalloc range has
5110 * already been ran (aka, ordered extent inserted) and all pages are still
5113 int extent_write_locked_range(struct inode *inode, u64 start, u64 end)
5115 bool found_error = false;
5116 int first_error = 0;
5118 struct address_space *mapping = inode->i_mapping;
5121 unsigned long nr_pages;
5122 const u32 sectorsize = btrfs_sb(inode->i_sb)->sectorsize;
5123 struct extent_page_data epd = {
5128 struct writeback_control wbc_writepages = {
5129 .sync_mode = WB_SYNC_ALL,
5130 .range_start = start,
5131 .range_end = end + 1,
5132 /* We're called from an async helper function */
5133 .punt_to_cgroup = 1,
5134 .no_cgroup_owner = 1,
5137 ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(end + 1, sectorsize));
5138 nr_pages = (round_up(end, PAGE_SIZE) - round_down(start, PAGE_SIZE)) >>
5140 wbc_writepages.nr_to_write = nr_pages * 2;
5142 wbc_attach_fdatawrite_inode(&wbc_writepages, inode);
5143 while (cur <= end) {
5144 u64 cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);
5146 page = find_get_page(mapping, cur >> PAGE_SHIFT);
5148 * All pages in the range are locked since
5149 * btrfs_run_delalloc_range(), thus there is no way to clear
5150 * the page dirty flag.
5152 ASSERT(PageLocked(page));
5153 ASSERT(PageDirty(page));
5154 clear_page_dirty_for_io(page);
5155 ret = __extent_writepage(page, &wbc_writepages, &epd);
5166 ret = flush_write_bio(&epd);
5168 end_write_bio(&epd, ret);
5170 wbc_detach_inode(&wbc_writepages);
5176 int extent_writepages(struct address_space *mapping,
5177 struct writeback_control *wbc)
5179 struct inode *inode = mapping->host;
5181 struct extent_page_data epd = {
5184 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
5188 * Allow only a single thread to do the reloc work in zoned mode to
5189 * protect the write pointer updates.
5191 btrfs_zoned_data_reloc_lock(BTRFS_I(inode));
5192 ret = extent_write_cache_pages(mapping, wbc, &epd);
5193 btrfs_zoned_data_reloc_unlock(BTRFS_I(inode));
5196 end_write_bio(&epd, ret);
5199 ret = flush_write_bio(&epd);
5203 void extent_readahead(struct readahead_control *rac)
5205 struct btrfs_bio_ctrl bio_ctrl = { 0 };
5206 struct page *pagepool[16];
5207 struct extent_map *em_cached = NULL;
5208 u64 prev_em_start = (u64)-1;
5211 while ((nr = readahead_page_batch(rac, pagepool))) {
5212 u64 contig_start = readahead_pos(rac);
5213 u64 contig_end = contig_start + readahead_batch_length(rac) - 1;
5215 contiguous_readpages(pagepool, nr, contig_start, contig_end,
5216 &em_cached, &bio_ctrl, &prev_em_start);
5220 free_extent_map(em_cached);
5223 if (submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags))
5229 * basic invalidate_folio code, this waits on any locked or writeback
5230 * ranges corresponding to the folio, and then deletes any extent state
5231 * records from the tree
5233 int extent_invalidate_folio(struct extent_io_tree *tree,
5234 struct folio *folio, size_t offset)
5236 struct extent_state *cached_state = NULL;
5237 u64 start = folio_pos(folio);
5238 u64 end = start + folio_size(folio) - 1;
5239 size_t blocksize = folio->mapping->host->i_sb->s_blocksize;
5241 /* This function is only called for the btree inode */
5242 ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO);
5244 start += ALIGN(offset, blocksize);
5248 lock_extent_bits(tree, start, end, &cached_state);
5249 folio_wait_writeback(folio);
5252 * Currently for btree io tree, only EXTENT_LOCKED is utilized,
5253 * so here we only need to unlock the extent range to free any
5254 * existing extent state.
5256 unlock_extent_cached(tree, start, end, &cached_state);
5261 * a helper for releasepage, this tests for areas of the page that
5262 * are locked or under IO and drops the related state bits if it is safe
5265 static int try_release_extent_state(struct extent_io_tree *tree,
5266 struct page *page, gfp_t mask)
5268 u64 start = page_offset(page);
5269 u64 end = start + PAGE_SIZE - 1;
5272 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
5276 * At this point we can safely clear everything except the
5277 * locked bit, the nodatasum bit and the delalloc new bit.
5278 * The delalloc new bit will be cleared by ordered extent
5281 ret = __clear_extent_bit(tree, start, end,
5282 ~(EXTENT_LOCKED | EXTENT_NODATASUM | EXTENT_DELALLOC_NEW),
5283 0, 0, NULL, mask, NULL);
5285 /* if clear_extent_bit failed for enomem reasons,
5286 * we can't allow the release to continue.
5297 * a helper for releasepage. As long as there are no locked extents
5298 * in the range corresponding to the page, both state records and extent
5299 * map records are removed
5301 int try_release_extent_mapping(struct page *page, gfp_t mask)
5303 struct extent_map *em;
5304 u64 start = page_offset(page);
5305 u64 end = start + PAGE_SIZE - 1;
5306 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
5307 struct extent_io_tree *tree = &btrfs_inode->io_tree;
5308 struct extent_map_tree *map = &btrfs_inode->extent_tree;
5310 if (gfpflags_allow_blocking(mask) &&
5311 page->mapping->host->i_size > SZ_16M) {
5313 while (start <= end) {
5314 struct btrfs_fs_info *fs_info;
5317 len = end - start + 1;
5318 write_lock(&map->lock);
5319 em = lookup_extent_mapping(map, start, len);
5321 write_unlock(&map->lock);
5324 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
5325 em->start != start) {
5326 write_unlock(&map->lock);
5327 free_extent_map(em);
5330 if (test_range_bit(tree, em->start,
5331 extent_map_end(em) - 1,
5332 EXTENT_LOCKED, 0, NULL))
5335 * If it's not in the list of modified extents, used
5336 * by a fast fsync, we can remove it. If it's being
5337 * logged we can safely remove it since fsync took an
5338 * extra reference on the em.
5340 if (list_empty(&em->list) ||
5341 test_bit(EXTENT_FLAG_LOGGING, &em->flags))
5344 * If it's in the list of modified extents, remove it
5345 * only if its generation is older then the current one,
5346 * in which case we don't need it for a fast fsync.
5347 * Otherwise don't remove it, we could be racing with an
5348 * ongoing fast fsync that could miss the new extent.
5350 fs_info = btrfs_inode->root->fs_info;
5351 spin_lock(&fs_info->trans_lock);
5352 cur_gen = fs_info->generation;
5353 spin_unlock(&fs_info->trans_lock);
5354 if (em->generation >= cur_gen)
5358 * We only remove extent maps that are not in the list of
5359 * modified extents or that are in the list but with a
5360 * generation lower then the current generation, so there
5361 * is no need to set the full fsync flag on the inode (it
5362 * hurts the fsync performance for workloads with a data
5363 * size that exceeds or is close to the system's memory).
5365 remove_extent_mapping(map, em);
5366 /* once for the rb tree */
5367 free_extent_map(em);
5369 start = extent_map_end(em);
5370 write_unlock(&map->lock);
5373 free_extent_map(em);
5375 cond_resched(); /* Allow large-extent preemption. */
5378 return try_release_extent_state(tree, page, mask);
5382 * helper function for fiemap, which doesn't want to see any holes.
5383 * This maps until we find something past 'last'
5385 static struct extent_map *get_extent_skip_holes(struct btrfs_inode *inode,
5386 u64 offset, u64 last)
5388 u64 sectorsize = btrfs_inode_sectorsize(inode);
5389 struct extent_map *em;
5396 len = last - offset;
5399 len = ALIGN(len, sectorsize);
5400 em = btrfs_get_extent_fiemap(inode, offset, len);
5404 /* if this isn't a hole return it */
5405 if (em->block_start != EXTENT_MAP_HOLE)
5408 /* this is a hole, advance to the next extent */
5409 offset = extent_map_end(em);
5410 free_extent_map(em);
5418 * To cache previous fiemap extent
5420 * Will be used for merging fiemap extent
5422 struct fiemap_cache {
5431 * Helper to submit fiemap extent.
5433 * Will try to merge current fiemap extent specified by @offset, @phys,
5434 * @len and @flags with cached one.
5435 * And only when we fails to merge, cached one will be submitted as
5438 * Return value is the same as fiemap_fill_next_extent().
5440 static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
5441 struct fiemap_cache *cache,
5442 u64 offset, u64 phys, u64 len, u32 flags)
5450 * Sanity check, extent_fiemap() should have ensured that new
5451 * fiemap extent won't overlap with cached one.
5454 * NOTE: Physical address can overlap, due to compression
5456 if (cache->offset + cache->len > offset) {
5462 * Only merges fiemap extents if
5463 * 1) Their logical addresses are continuous
5465 * 2) Their physical addresses are continuous
5466 * So truly compressed (physical size smaller than logical size)
5467 * extents won't get merged with each other
5469 * 3) Share same flags except FIEMAP_EXTENT_LAST
5470 * So regular extent won't get merged with prealloc extent
5472 if (cache->offset + cache->len == offset &&
5473 cache->phys + cache->len == phys &&
5474 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
5475 (flags & ~FIEMAP_EXTENT_LAST)) {
5477 cache->flags |= flags;
5478 goto try_submit_last;
5481 /* Not mergeable, need to submit cached one */
5482 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
5483 cache->len, cache->flags);
5484 cache->cached = false;
5488 cache->cached = true;
5489 cache->offset = offset;
5492 cache->flags = flags;
5494 if (cache->flags & FIEMAP_EXTENT_LAST) {
5495 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
5496 cache->phys, cache->len, cache->flags);
5497 cache->cached = false;
5503 * Emit last fiemap cache
5505 * The last fiemap cache may still be cached in the following case:
5507 * |<- Fiemap range ->|
5508 * |<------------ First extent ----------->|
5510 * In this case, the first extent range will be cached but not emitted.
5511 * So we must emit it before ending extent_fiemap().
5513 static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
5514 struct fiemap_cache *cache)
5521 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
5522 cache->len, cache->flags);
5523 cache->cached = false;
5529 int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo,
5534 u64 max = start + len;
5538 u64 last_for_get_extent = 0;
5540 u64 isize = i_size_read(&inode->vfs_inode);
5541 struct btrfs_key found_key;
5542 struct extent_map *em = NULL;
5543 struct extent_state *cached_state = NULL;
5544 struct btrfs_path *path;
5545 struct btrfs_root *root = inode->root;
5546 struct fiemap_cache cache = { 0 };
5547 struct ulist *roots;
5548 struct ulist *tmp_ulist;
5557 path = btrfs_alloc_path();
5561 roots = ulist_alloc(GFP_KERNEL);
5562 tmp_ulist = ulist_alloc(GFP_KERNEL);
5563 if (!roots || !tmp_ulist) {
5565 goto out_free_ulist;
5569 * We can't initialize that to 'start' as this could miss extents due
5570 * to extent item merging
5573 start = round_down(start, btrfs_inode_sectorsize(inode));
5574 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
5577 * lookup the last file extent. We're not using i_size here
5578 * because there might be preallocation past i_size
5580 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
5583 goto out_free_ulist;
5591 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
5592 found_type = found_key.type;
5594 /* No extents, but there might be delalloc bits */
5595 if (found_key.objectid != btrfs_ino(inode) ||
5596 found_type != BTRFS_EXTENT_DATA_KEY) {
5597 /* have to trust i_size as the end */
5599 last_for_get_extent = isize;
5602 * remember the start of the last extent. There are a
5603 * bunch of different factors that go into the length of the
5604 * extent, so its much less complex to remember where it started
5606 last = found_key.offset;
5607 last_for_get_extent = last + 1;
5609 btrfs_release_path(path);
5612 * we might have some extents allocated but more delalloc past those
5613 * extents. so, we trust isize unless the start of the last extent is
5618 last_for_get_extent = isize;
5621 lock_extent_bits(&inode->io_tree, start, start + len - 1,
5624 em = get_extent_skip_holes(inode, start, last_for_get_extent);
5633 u64 offset_in_extent = 0;
5635 /* break if the extent we found is outside the range */
5636 if (em->start >= max || extent_map_end(em) < off)
5640 * get_extent may return an extent that starts before our
5641 * requested range. We have to make sure the ranges
5642 * we return to fiemap always move forward and don't
5643 * overlap, so adjust the offsets here
5645 em_start = max(em->start, off);
5648 * record the offset from the start of the extent
5649 * for adjusting the disk offset below. Only do this if the
5650 * extent isn't compressed since our in ram offset may be past
5651 * what we have actually allocated on disk.
5653 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
5654 offset_in_extent = em_start - em->start;
5655 em_end = extent_map_end(em);
5656 em_len = em_end - em_start;
5658 if (em->block_start < EXTENT_MAP_LAST_BYTE)
5659 disko = em->block_start + offset_in_extent;
5664 * bump off for our next call to get_extent
5666 off = extent_map_end(em);
5670 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
5672 flags |= FIEMAP_EXTENT_LAST;
5673 } else if (em->block_start == EXTENT_MAP_INLINE) {
5674 flags |= (FIEMAP_EXTENT_DATA_INLINE |
5675 FIEMAP_EXTENT_NOT_ALIGNED);
5676 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
5677 flags |= (FIEMAP_EXTENT_DELALLOC |
5678 FIEMAP_EXTENT_UNKNOWN);
5679 } else if (fieinfo->fi_extents_max) {
5680 u64 bytenr = em->block_start -
5681 (em->start - em->orig_start);
5684 * As btrfs supports shared space, this information
5685 * can be exported to userspace tools via
5686 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
5687 * then we're just getting a count and we can skip the
5690 ret = btrfs_check_shared(root, btrfs_ino(inode),
5691 bytenr, roots, tmp_ulist);
5695 flags |= FIEMAP_EXTENT_SHARED;
5698 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
5699 flags |= FIEMAP_EXTENT_ENCODED;
5700 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5701 flags |= FIEMAP_EXTENT_UNWRITTEN;
5703 free_extent_map(em);
5705 if ((em_start >= last) || em_len == (u64)-1 ||
5706 (last == (u64)-1 && isize <= em_end)) {
5707 flags |= FIEMAP_EXTENT_LAST;
5711 /* now scan forward to see if this is really the last extent. */
5712 em = get_extent_skip_holes(inode, off, last_for_get_extent);
5718 flags |= FIEMAP_EXTENT_LAST;
5721 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
5731 ret = emit_last_fiemap_cache(fieinfo, &cache);
5732 free_extent_map(em);
5734 unlock_extent_cached(&inode->io_tree, start, start + len - 1,
5738 btrfs_free_path(path);
5740 ulist_free(tmp_ulist);
5744 static void __free_extent_buffer(struct extent_buffer *eb)
5746 kmem_cache_free(extent_buffer_cache, eb);
5749 int extent_buffer_under_io(const struct extent_buffer *eb)
5751 return (atomic_read(&eb->io_pages) ||
5752 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
5753 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5756 static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page)
5758 struct btrfs_subpage *subpage;
5760 lockdep_assert_held(&page->mapping->private_lock);
5762 if (PagePrivate(page)) {
5763 subpage = (struct btrfs_subpage *)page->private;
5764 if (atomic_read(&subpage->eb_refs))
5767 * Even there is no eb refs here, we may still have
5768 * end_page_read() call relying on page::private.
5770 if (atomic_read(&subpage->readers))
5776 static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
5778 struct btrfs_fs_info *fs_info = eb->fs_info;
5779 const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
5782 * For mapped eb, we're going to change the page private, which should
5783 * be done under the private_lock.
5786 spin_lock(&page->mapping->private_lock);
5788 if (!PagePrivate(page)) {
5790 spin_unlock(&page->mapping->private_lock);
5794 if (fs_info->sectorsize == PAGE_SIZE) {
5796 * We do this since we'll remove the pages after we've
5797 * removed the eb from the radix tree, so we could race
5798 * and have this page now attached to the new eb. So
5799 * only clear page_private if it's still connected to
5802 if (PagePrivate(page) &&
5803 page->private == (unsigned long)eb) {
5804 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5805 BUG_ON(PageDirty(page));
5806 BUG_ON(PageWriteback(page));
5808 * We need to make sure we haven't be attached
5811 detach_page_private(page);
5814 spin_unlock(&page->mapping->private_lock);
5819 * For subpage, we can have dummy eb with page private. In this case,
5820 * we can directly detach the private as such page is only attached to
5821 * one dummy eb, no sharing.
5824 btrfs_detach_subpage(fs_info, page);
5828 btrfs_page_dec_eb_refs(fs_info, page);
5831 * We can only detach the page private if there are no other ebs in the
5832 * page range and no unfinished IO.
5834 if (!page_range_has_eb(fs_info, page))
5835 btrfs_detach_subpage(fs_info, page);
5837 spin_unlock(&page->mapping->private_lock);
5840 /* Release all pages attached to the extent buffer */
5841 static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
5846 ASSERT(!extent_buffer_under_io(eb));
5848 num_pages = num_extent_pages(eb);
5849 for (i = 0; i < num_pages; i++) {
5850 struct page *page = eb->pages[i];
5855 detach_extent_buffer_page(eb, page);
5857 /* One for when we allocated the page */
5863 * Helper for releasing the extent buffer.
5865 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
5867 btrfs_release_extent_buffer_pages(eb);
5868 btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
5869 __free_extent_buffer(eb);
5872 static struct extent_buffer *
5873 __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
5876 struct extent_buffer *eb = NULL;
5878 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
5881 eb->fs_info = fs_info;
5883 init_rwsem(&eb->lock);
5885 btrfs_leak_debug_add(&fs_info->eb_leak_lock, &eb->leak_list,
5886 &fs_info->allocated_ebs);
5887 INIT_LIST_HEAD(&eb->release_list);
5889 spin_lock_init(&eb->refs_lock);
5890 atomic_set(&eb->refs, 1);
5891 atomic_set(&eb->io_pages, 0);
5893 ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE);
5898 struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
5902 struct extent_buffer *new;
5903 int num_pages = num_extent_pages(src);
5905 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
5910 * Set UNMAPPED before calling btrfs_release_extent_buffer(), as
5911 * btrfs_release_extent_buffer() have different behavior for
5912 * UNMAPPED subpage extent buffer.
5914 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
5916 for (i = 0; i < num_pages; i++) {
5919 p = alloc_page(GFP_NOFS);
5921 btrfs_release_extent_buffer(new);
5924 ret = attach_extent_buffer_page(new, p, NULL);
5927 btrfs_release_extent_buffer(new);
5930 WARN_ON(PageDirty(p));
5932 copy_page(page_address(p), page_address(src->pages[i]));
5934 set_extent_buffer_uptodate(new);
5939 struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
5940 u64 start, unsigned long len)
5942 struct extent_buffer *eb;
5946 eb = __alloc_extent_buffer(fs_info, start, len);
5950 num_pages = num_extent_pages(eb);
5951 for (i = 0; i < num_pages; i++) {
5954 eb->pages[i] = alloc_page(GFP_NOFS);
5957 ret = attach_extent_buffer_page(eb, eb->pages[i], NULL);
5961 set_extent_buffer_uptodate(eb);
5962 btrfs_set_header_nritems(eb, 0);
5963 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
5967 for (; i > 0; i--) {
5968 detach_extent_buffer_page(eb, eb->pages[i - 1]);
5969 __free_page(eb->pages[i - 1]);
5971 __free_extent_buffer(eb);
5975 struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
5978 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
5981 static void check_buffer_tree_ref(struct extent_buffer *eb)
5985 * The TREE_REF bit is first set when the extent_buffer is added
5986 * to the radix tree. It is also reset, if unset, when a new reference
5987 * is created by find_extent_buffer.
5989 * It is only cleared in two cases: freeing the last non-tree
5990 * reference to the extent_buffer when its STALE bit is set or
5991 * calling releasepage when the tree reference is the only reference.
5993 * In both cases, care is taken to ensure that the extent_buffer's
5994 * pages are not under io. However, releasepage can be concurrently
5995 * called with creating new references, which is prone to race
5996 * conditions between the calls to check_buffer_tree_ref in those
5997 * codepaths and clearing TREE_REF in try_release_extent_buffer.
5999 * The actual lifetime of the extent_buffer in the radix tree is
6000 * adequately protected by the refcount, but the TREE_REF bit and
6001 * its corresponding reference are not. To protect against this
6002 * class of races, we call check_buffer_tree_ref from the codepaths
6003 * which trigger io after they set eb->io_pages. Note that once io is
6004 * initiated, TREE_REF can no longer be cleared, so that is the
6005 * moment at which any such race is best fixed.
6007 refs = atomic_read(&eb->refs);
6008 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6011 spin_lock(&eb->refs_lock);
6012 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6013 atomic_inc(&eb->refs);
6014 spin_unlock(&eb->refs_lock);
6017 static void mark_extent_buffer_accessed(struct extent_buffer *eb,
6018 struct page *accessed)
6022 check_buffer_tree_ref(eb);
6024 num_pages = num_extent_pages(eb);
6025 for (i = 0; i < num_pages; i++) {
6026 struct page *p = eb->pages[i];
6029 mark_page_accessed(p);
6033 struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
6036 struct extent_buffer *eb;
6038 eb = find_extent_buffer_nolock(fs_info, start);
6042 * Lock our eb's refs_lock to avoid races with free_extent_buffer().
6043 * When we get our eb it might be flagged with EXTENT_BUFFER_STALE and
6044 * another task running free_extent_buffer() might have seen that flag
6045 * set, eb->refs == 2, that the buffer isn't under IO (dirty and
6046 * writeback flags not set) and it's still in the tree (flag
6047 * EXTENT_BUFFER_TREE_REF set), therefore being in the process of
6048 * decrementing the extent buffer's reference count twice. So here we
6049 * could race and increment the eb's reference count, clear its stale
6050 * flag, mark it as dirty and drop our reference before the other task
6051 * finishes executing free_extent_buffer, which would later result in
6052 * an attempt to free an extent buffer that is dirty.
6054 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
6055 spin_lock(&eb->refs_lock);
6056 spin_unlock(&eb->refs_lock);
6058 mark_extent_buffer_accessed(eb, NULL);
6062 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
6063 struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
6066 struct extent_buffer *eb, *exists = NULL;
6069 eb = find_extent_buffer(fs_info, start);
6072 eb = alloc_dummy_extent_buffer(fs_info, start);
6074 return ERR_PTR(-ENOMEM);
6075 eb->fs_info = fs_info;
6077 ret = radix_tree_preload(GFP_NOFS);
6079 exists = ERR_PTR(ret);
6082 spin_lock(&fs_info->buffer_lock);
6083 ret = radix_tree_insert(&fs_info->buffer_radix,
6084 start >> fs_info->sectorsize_bits, eb);
6085 spin_unlock(&fs_info->buffer_lock);
6086 radix_tree_preload_end();
6087 if (ret == -EEXIST) {
6088 exists = find_extent_buffer(fs_info, start);
6094 check_buffer_tree_ref(eb);
6095 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
6099 btrfs_release_extent_buffer(eb);
6104 static struct extent_buffer *grab_extent_buffer(
6105 struct btrfs_fs_info *fs_info, struct page *page)
6107 struct extent_buffer *exists;
6110 * For subpage case, we completely rely on radix tree to ensure we
6111 * don't try to insert two ebs for the same bytenr. So here we always
6112 * return NULL and just continue.
6114 if (fs_info->sectorsize < PAGE_SIZE)
6117 /* Page not yet attached to an extent buffer */
6118 if (!PagePrivate(page))
6122 * We could have already allocated an eb for this page and attached one
6123 * so lets see if we can get a ref on the existing eb, and if we can we
6124 * know it's good and we can just return that one, else we know we can
6125 * just overwrite page->private.
6127 exists = (struct extent_buffer *)page->private;
6128 if (atomic_inc_not_zero(&exists->refs))
6131 WARN_ON(PageDirty(page));
6132 detach_page_private(page);
6136 struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
6137 u64 start, u64 owner_root, int level)
6139 unsigned long len = fs_info->nodesize;
6142 unsigned long index = start >> PAGE_SHIFT;
6143 struct extent_buffer *eb;
6144 struct extent_buffer *exists = NULL;
6146 struct address_space *mapping = fs_info->btree_inode->i_mapping;
6150 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
6151 btrfs_err(fs_info, "bad tree block start %llu", start);
6152 return ERR_PTR(-EINVAL);
6155 #if BITS_PER_LONG == 32
6156 if (start >= MAX_LFS_FILESIZE) {
6157 btrfs_err_rl(fs_info,
6158 "extent buffer %llu is beyond 32bit page cache limit", start);
6159 btrfs_err_32bit_limit(fs_info);
6160 return ERR_PTR(-EOVERFLOW);
6162 if (start >= BTRFS_32BIT_EARLY_WARN_THRESHOLD)
6163 btrfs_warn_32bit_limit(fs_info);
6166 if (fs_info->sectorsize < PAGE_SIZE &&
6167 offset_in_page(start) + len > PAGE_SIZE) {
6169 "tree block crosses page boundary, start %llu nodesize %lu",
6171 return ERR_PTR(-EINVAL);
6174 eb = find_extent_buffer(fs_info, start);
6178 eb = __alloc_extent_buffer(fs_info, start, len);
6180 return ERR_PTR(-ENOMEM);
6181 btrfs_set_buffer_lockdep_class(owner_root, eb, level);
6183 num_pages = num_extent_pages(eb);
6184 for (i = 0; i < num_pages; i++, index++) {
6185 struct btrfs_subpage *prealloc = NULL;
6187 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
6189 exists = ERR_PTR(-ENOMEM);
6194 * Preallocate page->private for subpage case, so that we won't
6195 * allocate memory with private_lock hold. The memory will be
6196 * freed by attach_extent_buffer_page() or freed manually if
6199 * Although we have ensured one subpage eb can only have one
6200 * page, but it may change in the future for 16K page size
6201 * support, so we still preallocate the memory in the loop.
6203 if (fs_info->sectorsize < PAGE_SIZE) {
6204 prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA);
6205 if (IS_ERR(prealloc)) {
6206 ret = PTR_ERR(prealloc);
6209 exists = ERR_PTR(ret);
6214 spin_lock(&mapping->private_lock);
6215 exists = grab_extent_buffer(fs_info, p);
6217 spin_unlock(&mapping->private_lock);
6220 mark_extent_buffer_accessed(exists, p);
6221 btrfs_free_subpage(prealloc);
6224 /* Should not fail, as we have preallocated the memory */
6225 ret = attach_extent_buffer_page(eb, p, prealloc);
6228 * To inform we have extra eb under allocation, so that
6229 * detach_extent_buffer_page() won't release the page private
6230 * when the eb hasn't yet been inserted into radix tree.
6232 * The ref will be decreased when the eb released the page, in
6233 * detach_extent_buffer_page().
6234 * Thus needs no special handling in error path.
6236 btrfs_page_inc_eb_refs(fs_info, p);
6237 spin_unlock(&mapping->private_lock);
6239 WARN_ON(btrfs_page_test_dirty(fs_info, p, eb->start, eb->len));
6241 if (!PageUptodate(p))
6245 * We can't unlock the pages just yet since the extent buffer
6246 * hasn't been properly inserted in the radix tree, this
6247 * opens a race with btree_releasepage which can free a page
6248 * while we are still filling in all pages for the buffer and
6253 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6255 ret = radix_tree_preload(GFP_NOFS);
6257 exists = ERR_PTR(ret);
6261 spin_lock(&fs_info->buffer_lock);
6262 ret = radix_tree_insert(&fs_info->buffer_radix,
6263 start >> fs_info->sectorsize_bits, eb);
6264 spin_unlock(&fs_info->buffer_lock);
6265 radix_tree_preload_end();
6266 if (ret == -EEXIST) {
6267 exists = find_extent_buffer(fs_info, start);
6273 /* add one reference for the tree */
6274 check_buffer_tree_ref(eb);
6275 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
6278 * Now it's safe to unlock the pages because any calls to
6279 * btree_releasepage will correctly detect that a page belongs to a
6280 * live buffer and won't free them prematurely.
6282 for (i = 0; i < num_pages; i++)
6283 unlock_page(eb->pages[i]);
6287 WARN_ON(!atomic_dec_and_test(&eb->refs));
6288 for (i = 0; i < num_pages; i++) {
6290 unlock_page(eb->pages[i]);
6293 btrfs_release_extent_buffer(eb);
6297 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
6299 struct extent_buffer *eb =
6300 container_of(head, struct extent_buffer, rcu_head);
6302 __free_extent_buffer(eb);
6305 static int release_extent_buffer(struct extent_buffer *eb)
6306 __releases(&eb->refs_lock)
6308 lockdep_assert_held(&eb->refs_lock);
6310 WARN_ON(atomic_read(&eb->refs) == 0);
6311 if (atomic_dec_and_test(&eb->refs)) {
6312 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
6313 struct btrfs_fs_info *fs_info = eb->fs_info;
6315 spin_unlock(&eb->refs_lock);
6317 spin_lock(&fs_info->buffer_lock);
6318 radix_tree_delete(&fs_info->buffer_radix,
6319 eb->start >> fs_info->sectorsize_bits);
6320 spin_unlock(&fs_info->buffer_lock);
6322 spin_unlock(&eb->refs_lock);
6325 btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
6326 /* Should be safe to release our pages at this point */
6327 btrfs_release_extent_buffer_pages(eb);
6328 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
6329 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
6330 __free_extent_buffer(eb);
6334 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
6337 spin_unlock(&eb->refs_lock);
6342 void free_extent_buffer(struct extent_buffer *eb)
6350 refs = atomic_read(&eb->refs);
6351 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
6352 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
6355 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
6360 spin_lock(&eb->refs_lock);
6361 if (atomic_read(&eb->refs) == 2 &&
6362 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
6363 !extent_buffer_under_io(eb) &&
6364 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6365 atomic_dec(&eb->refs);
6368 * I know this is terrible, but it's temporary until we stop tracking
6369 * the uptodate bits and such for the extent buffers.
6371 release_extent_buffer(eb);
6374 void free_extent_buffer_stale(struct extent_buffer *eb)
6379 spin_lock(&eb->refs_lock);
6380 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
6382 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
6383 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6384 atomic_dec(&eb->refs);
6385 release_extent_buffer(eb);
6388 static void btree_clear_page_dirty(struct page *page)
6390 ASSERT(PageDirty(page));
6391 ASSERT(PageLocked(page));
6392 clear_page_dirty_for_io(page);
6393 xa_lock_irq(&page->mapping->i_pages);
6394 if (!PageDirty(page))
6395 __xa_clear_mark(&page->mapping->i_pages,
6396 page_index(page), PAGECACHE_TAG_DIRTY);
6397 xa_unlock_irq(&page->mapping->i_pages);
6400 static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb)
6402 struct btrfs_fs_info *fs_info = eb->fs_info;
6403 struct page *page = eb->pages[0];
6406 /* btree_clear_page_dirty() needs page locked */
6408 last = btrfs_subpage_clear_and_test_dirty(fs_info, page, eb->start,
6411 btree_clear_page_dirty(page);
6413 WARN_ON(atomic_read(&eb->refs) == 0);
6416 void clear_extent_buffer_dirty(const struct extent_buffer *eb)
6422 if (eb->fs_info->sectorsize < PAGE_SIZE)
6423 return clear_subpage_extent_buffer_dirty(eb);
6425 num_pages = num_extent_pages(eb);
6427 for (i = 0; i < num_pages; i++) {
6428 page = eb->pages[i];
6429 if (!PageDirty(page))
6432 btree_clear_page_dirty(page);
6433 ClearPageError(page);
6436 WARN_ON(atomic_read(&eb->refs) == 0);
6439 bool set_extent_buffer_dirty(struct extent_buffer *eb)
6445 check_buffer_tree_ref(eb);
6447 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
6449 num_pages = num_extent_pages(eb);
6450 WARN_ON(atomic_read(&eb->refs) == 0);
6451 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
6454 bool subpage = eb->fs_info->sectorsize < PAGE_SIZE;
6457 * For subpage case, we can have other extent buffers in the
6458 * same page, and in clear_subpage_extent_buffer_dirty() we
6459 * have to clear page dirty without subpage lock held.
6460 * This can cause race where our page gets dirty cleared after
6463 * Thankfully, clear_subpage_extent_buffer_dirty() has locked
6464 * its page for other reasons, we can use page lock to prevent
6468 lock_page(eb->pages[0]);
6469 for (i = 0; i < num_pages; i++)
6470 btrfs_page_set_dirty(eb->fs_info, eb->pages[i],
6471 eb->start, eb->len);
6473 unlock_page(eb->pages[0]);
6475 #ifdef CONFIG_BTRFS_DEBUG
6476 for (i = 0; i < num_pages; i++)
6477 ASSERT(PageDirty(eb->pages[i]));
6483 void clear_extent_buffer_uptodate(struct extent_buffer *eb)
6485 struct btrfs_fs_info *fs_info = eb->fs_info;
6490 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6491 num_pages = num_extent_pages(eb);
6492 for (i = 0; i < num_pages; i++) {
6493 page = eb->pages[i];
6495 btrfs_page_clear_uptodate(fs_info, page,
6496 eb->start, eb->len);
6500 void set_extent_buffer_uptodate(struct extent_buffer *eb)
6502 struct btrfs_fs_info *fs_info = eb->fs_info;
6507 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6508 num_pages = num_extent_pages(eb);
6509 for (i = 0; i < num_pages; i++) {
6510 page = eb->pages[i];
6511 btrfs_page_set_uptodate(fs_info, page, eb->start, eb->len);
6515 static int read_extent_buffer_subpage(struct extent_buffer *eb, int wait,
6518 struct btrfs_fs_info *fs_info = eb->fs_info;
6519 struct extent_io_tree *io_tree;
6520 struct page *page = eb->pages[0];
6521 struct btrfs_bio_ctrl bio_ctrl = { 0 };
6524 ASSERT(!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags));
6525 ASSERT(PagePrivate(page));
6526 io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
6528 if (wait == WAIT_NONE) {
6529 if (!try_lock_extent(io_tree, eb->start, eb->start + eb->len - 1))
6532 ret = lock_extent(io_tree, eb->start, eb->start + eb->len - 1);
6538 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags) ||
6539 PageUptodate(page) ||
6540 btrfs_subpage_test_uptodate(fs_info, page, eb->start, eb->len)) {
6541 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6542 unlock_extent(io_tree, eb->start, eb->start + eb->len - 1);
6546 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
6547 eb->read_mirror = 0;
6548 atomic_set(&eb->io_pages, 1);
6549 check_buffer_tree_ref(eb);
6550 btrfs_subpage_clear_error(fs_info, page, eb->start, eb->len);
6552 btrfs_subpage_start_reader(fs_info, page, eb->start, eb->len);
6553 ret = submit_extent_page(REQ_OP_READ | REQ_META, NULL, &bio_ctrl,
6554 page, eb->start, eb->len,
6555 eb->start - page_offset(page),
6556 end_bio_extent_readpage, mirror_num, 0,
6560 * In the endio function, if we hit something wrong we will
6561 * increase the io_pages, so here we need to decrease it for
6564 atomic_dec(&eb->io_pages);
6569 tmp = submit_one_bio(bio_ctrl.bio, mirror_num, 0);
6570 bio_ctrl.bio = NULL;
6574 if (ret || wait != WAIT_COMPLETE)
6577 wait_extent_bit(io_tree, eb->start, eb->start + eb->len - 1, EXTENT_LOCKED);
6578 if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
6583 int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
6589 int locked_pages = 0;
6590 int all_uptodate = 1;
6592 unsigned long num_reads = 0;
6593 struct btrfs_bio_ctrl bio_ctrl = { 0 };
6595 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
6599 * We could have had EXTENT_BUFFER_UPTODATE cleared by the write
6600 * operation, which could potentially still be in flight. In this case
6601 * we simply want to return an error.
6603 if (unlikely(test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)))
6606 if (eb->fs_info->sectorsize < PAGE_SIZE)
6607 return read_extent_buffer_subpage(eb, wait, mirror_num);
6609 num_pages = num_extent_pages(eb);
6610 for (i = 0; i < num_pages; i++) {
6611 page = eb->pages[i];
6612 if (wait == WAIT_NONE) {
6614 * WAIT_NONE is only utilized by readahead. If we can't
6615 * acquire the lock atomically it means either the eb
6616 * is being read out or under modification.
6617 * Either way the eb will be or has been cached,
6618 * readahead can exit safely.
6620 if (!trylock_page(page))
6628 * We need to firstly lock all pages to make sure that
6629 * the uptodate bit of our pages won't be affected by
6630 * clear_extent_buffer_uptodate().
6632 for (i = 0; i < num_pages; i++) {
6633 page = eb->pages[i];
6634 if (!PageUptodate(page)) {
6641 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6645 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
6646 eb->read_mirror = 0;
6647 atomic_set(&eb->io_pages, num_reads);
6649 * It is possible for releasepage to clear the TREE_REF bit before we
6650 * set io_pages. See check_buffer_tree_ref for a more detailed comment.
6652 check_buffer_tree_ref(eb);
6653 for (i = 0; i < num_pages; i++) {
6654 page = eb->pages[i];
6656 if (!PageUptodate(page)) {
6658 atomic_dec(&eb->io_pages);
6663 ClearPageError(page);
6664 err = submit_extent_page(REQ_OP_READ | REQ_META, NULL,
6665 &bio_ctrl, page, page_offset(page),
6666 PAGE_SIZE, 0, end_bio_extent_readpage,
6667 mirror_num, 0, false);
6670 * We failed to submit the bio so it's the
6671 * caller's responsibility to perform cleanup
6672 * i.e unlock page/set error bit.
6677 atomic_dec(&eb->io_pages);
6685 err = submit_one_bio(bio_ctrl.bio, mirror_num, bio_ctrl.bio_flags);
6686 bio_ctrl.bio = NULL;
6691 if (ret || wait != WAIT_COMPLETE)
6694 for (i = 0; i < num_pages; i++) {
6695 page = eb->pages[i];
6696 wait_on_page_locked(page);
6697 if (!PageUptodate(page))
6704 while (locked_pages > 0) {
6706 page = eb->pages[locked_pages];
6712 static bool report_eb_range(const struct extent_buffer *eb, unsigned long start,
6715 btrfs_warn(eb->fs_info,
6716 "access to eb bytenr %llu len %lu out of range start %lu len %lu",
6717 eb->start, eb->len, start, len);
6718 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
6724 * Check if the [start, start + len) range is valid before reading/writing
6726 * NOTE: @start and @len are offset inside the eb, not logical address.
6728 * Caller should not touch the dst/src memory if this function returns error.
6730 static inline int check_eb_range(const struct extent_buffer *eb,
6731 unsigned long start, unsigned long len)
6733 unsigned long offset;
6735 /* start, start + len should not go beyond eb->len nor overflow */
6736 if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len))
6737 return report_eb_range(eb, start, len);
6742 void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
6743 unsigned long start, unsigned long len)
6749 char *dst = (char *)dstv;
6750 unsigned long i = get_eb_page_index(start);
6752 if (check_eb_range(eb, start, len))
6755 offset = get_eb_offset_in_page(eb, start);
6758 page = eb->pages[i];
6760 cur = min(len, (PAGE_SIZE - offset));
6761 kaddr = page_address(page);
6762 memcpy(dst, kaddr + offset, cur);
6771 int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
6773 unsigned long start, unsigned long len)
6779 char __user *dst = (char __user *)dstv;
6780 unsigned long i = get_eb_page_index(start);
6783 WARN_ON(start > eb->len);
6784 WARN_ON(start + len > eb->start + eb->len);
6786 offset = get_eb_offset_in_page(eb, start);
6789 page = eb->pages[i];
6791 cur = min(len, (PAGE_SIZE - offset));
6792 kaddr = page_address(page);
6793 if (copy_to_user_nofault(dst, kaddr + offset, cur)) {
6807 int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
6808 unsigned long start, unsigned long len)
6814 char *ptr = (char *)ptrv;
6815 unsigned long i = get_eb_page_index(start);
6818 if (check_eb_range(eb, start, len))
6821 offset = get_eb_offset_in_page(eb, start);
6824 page = eb->pages[i];
6826 cur = min(len, (PAGE_SIZE - offset));
6828 kaddr = page_address(page);
6829 ret = memcmp(ptr, kaddr + offset, cur);
6842 * Check that the extent buffer is uptodate.
6844 * For regular sector size == PAGE_SIZE case, check if @page is uptodate.
6845 * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE.
6847 static void assert_eb_page_uptodate(const struct extent_buffer *eb,
6850 struct btrfs_fs_info *fs_info = eb->fs_info;
6853 * If we are using the commit root we could potentially clear a page
6854 * Uptodate while we're using the extent buffer that we've previously
6855 * looked up. We don't want to complain in this case, as the page was
6856 * valid before, we just didn't write it out. Instead we want to catch
6857 * the case where we didn't actually read the block properly, which
6858 * would have !PageUptodate && !PageError, as we clear PageError before
6861 if (fs_info->sectorsize < PAGE_SIZE) {
6862 bool uptodate, error;
6864 uptodate = btrfs_subpage_test_uptodate(fs_info, page,
6865 eb->start, eb->len);
6866 error = btrfs_subpage_test_error(fs_info, page, eb->start, eb->len);
6867 WARN_ON(!uptodate && !error);
6869 WARN_ON(!PageUptodate(page) && !PageError(page));
6873 void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb,
6878 assert_eb_page_uptodate(eb, eb->pages[0]);
6879 kaddr = page_address(eb->pages[0]) +
6880 get_eb_offset_in_page(eb, offsetof(struct btrfs_header,
6882 memcpy(kaddr, srcv, BTRFS_FSID_SIZE);
6885 void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *srcv)
6889 assert_eb_page_uptodate(eb, eb->pages[0]);
6890 kaddr = page_address(eb->pages[0]) +
6891 get_eb_offset_in_page(eb, offsetof(struct btrfs_header, fsid));
6892 memcpy(kaddr, srcv, BTRFS_FSID_SIZE);
6895 void write_extent_buffer(const struct extent_buffer *eb, const void *srcv,
6896 unsigned long start, unsigned long len)
6902 char *src = (char *)srcv;
6903 unsigned long i = get_eb_page_index(start);
6905 WARN_ON(test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags));
6907 if (check_eb_range(eb, start, len))
6910 offset = get_eb_offset_in_page(eb, start);
6913 page = eb->pages[i];
6914 assert_eb_page_uptodate(eb, page);
6916 cur = min(len, PAGE_SIZE - offset);
6917 kaddr = page_address(page);
6918 memcpy(kaddr + offset, src, cur);
6927 void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
6934 unsigned long i = get_eb_page_index(start);
6936 if (check_eb_range(eb, start, len))
6939 offset = get_eb_offset_in_page(eb, start);
6942 page = eb->pages[i];
6943 assert_eb_page_uptodate(eb, page);
6945 cur = min(len, PAGE_SIZE - offset);
6946 kaddr = page_address(page);
6947 memset(kaddr + offset, 0, cur);
6955 void copy_extent_buffer_full(const struct extent_buffer *dst,
6956 const struct extent_buffer *src)
6961 ASSERT(dst->len == src->len);
6963 if (dst->fs_info->sectorsize == PAGE_SIZE) {
6964 num_pages = num_extent_pages(dst);
6965 for (i = 0; i < num_pages; i++)
6966 copy_page(page_address(dst->pages[i]),
6967 page_address(src->pages[i]));
6969 size_t src_offset = get_eb_offset_in_page(src, 0);
6970 size_t dst_offset = get_eb_offset_in_page(dst, 0);
6972 ASSERT(src->fs_info->sectorsize < PAGE_SIZE);
6973 memcpy(page_address(dst->pages[0]) + dst_offset,
6974 page_address(src->pages[0]) + src_offset,
6979 void copy_extent_buffer(const struct extent_buffer *dst,
6980 const struct extent_buffer *src,
6981 unsigned long dst_offset, unsigned long src_offset,
6984 u64 dst_len = dst->len;
6989 unsigned long i = get_eb_page_index(dst_offset);
6991 if (check_eb_range(dst, dst_offset, len) ||
6992 check_eb_range(src, src_offset, len))
6995 WARN_ON(src->len != dst_len);
6997 offset = get_eb_offset_in_page(dst, dst_offset);
7000 page = dst->pages[i];
7001 assert_eb_page_uptodate(dst, page);
7003 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
7005 kaddr = page_address(page);
7006 read_extent_buffer(src, kaddr + offset, src_offset, cur);
7016 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
7018 * @eb: the extent buffer
7019 * @start: offset of the bitmap item in the extent buffer
7021 * @page_index: return index of the page in the extent buffer that contains the
7023 * @page_offset: return offset into the page given by page_index
7025 * This helper hides the ugliness of finding the byte in an extent buffer which
7026 * contains a given bit.
7028 static inline void eb_bitmap_offset(const struct extent_buffer *eb,
7029 unsigned long start, unsigned long nr,
7030 unsigned long *page_index,
7031 size_t *page_offset)
7033 size_t byte_offset = BIT_BYTE(nr);
7037 * The byte we want is the offset of the extent buffer + the offset of
7038 * the bitmap item in the extent buffer + the offset of the byte in the
7041 offset = start + offset_in_page(eb->start) + byte_offset;
7043 *page_index = offset >> PAGE_SHIFT;
7044 *page_offset = offset_in_page(offset);
7048 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
7049 * @eb: the extent buffer
7050 * @start: offset of the bitmap item in the extent buffer
7051 * @nr: bit number to test
7053 int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
7061 eb_bitmap_offset(eb, start, nr, &i, &offset);
7062 page = eb->pages[i];
7063 assert_eb_page_uptodate(eb, page);
7064 kaddr = page_address(page);
7065 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
7069 * extent_buffer_bitmap_set - set an area of a bitmap
7070 * @eb: the extent buffer
7071 * @start: offset of the bitmap item in the extent buffer
7072 * @pos: bit number of the first bit
7073 * @len: number of bits to set
7075 void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
7076 unsigned long pos, unsigned long len)
7082 const unsigned int size = pos + len;
7083 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
7084 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
7086 eb_bitmap_offset(eb, start, pos, &i, &offset);
7087 page = eb->pages[i];
7088 assert_eb_page_uptodate(eb, page);
7089 kaddr = page_address(page);
7091 while (len >= bits_to_set) {
7092 kaddr[offset] |= mask_to_set;
7094 bits_to_set = BITS_PER_BYTE;
7096 if (++offset >= PAGE_SIZE && len > 0) {
7098 page = eb->pages[++i];
7099 assert_eb_page_uptodate(eb, page);
7100 kaddr = page_address(page);
7104 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
7105 kaddr[offset] |= mask_to_set;
7111 * extent_buffer_bitmap_clear - clear an area of a bitmap
7112 * @eb: the extent buffer
7113 * @start: offset of the bitmap item in the extent buffer
7114 * @pos: bit number of the first bit
7115 * @len: number of bits to clear
7117 void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
7118 unsigned long start, unsigned long pos,
7125 const unsigned int size = pos + len;
7126 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
7127 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
7129 eb_bitmap_offset(eb, start, pos, &i, &offset);
7130 page = eb->pages[i];
7131 assert_eb_page_uptodate(eb, page);
7132 kaddr = page_address(page);
7134 while (len >= bits_to_clear) {
7135 kaddr[offset] &= ~mask_to_clear;
7136 len -= bits_to_clear;
7137 bits_to_clear = BITS_PER_BYTE;
7139 if (++offset >= PAGE_SIZE && len > 0) {
7141 page = eb->pages[++i];
7142 assert_eb_page_uptodate(eb, page);
7143 kaddr = page_address(page);
7147 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
7148 kaddr[offset] &= ~mask_to_clear;
7152 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
7154 unsigned long distance = (src > dst) ? src - dst : dst - src;
7155 return distance < len;
7158 static void copy_pages(struct page *dst_page, struct page *src_page,
7159 unsigned long dst_off, unsigned long src_off,
7162 char *dst_kaddr = page_address(dst_page);
7164 int must_memmove = 0;
7166 if (dst_page != src_page) {
7167 src_kaddr = page_address(src_page);
7169 src_kaddr = dst_kaddr;
7170 if (areas_overlap(src_off, dst_off, len))
7175 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
7177 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
7180 void memcpy_extent_buffer(const struct extent_buffer *dst,
7181 unsigned long dst_offset, unsigned long src_offset,
7185 size_t dst_off_in_page;
7186 size_t src_off_in_page;
7187 unsigned long dst_i;
7188 unsigned long src_i;
7190 if (check_eb_range(dst, dst_offset, len) ||
7191 check_eb_range(dst, src_offset, len))
7195 dst_off_in_page = get_eb_offset_in_page(dst, dst_offset);
7196 src_off_in_page = get_eb_offset_in_page(dst, src_offset);
7198 dst_i = get_eb_page_index(dst_offset);
7199 src_i = get_eb_page_index(src_offset);
7201 cur = min(len, (unsigned long)(PAGE_SIZE -
7203 cur = min_t(unsigned long, cur,
7204 (unsigned long)(PAGE_SIZE - dst_off_in_page));
7206 copy_pages(dst->pages[dst_i], dst->pages[src_i],
7207 dst_off_in_page, src_off_in_page, cur);
7215 void memmove_extent_buffer(const struct extent_buffer *dst,
7216 unsigned long dst_offset, unsigned long src_offset,
7220 size_t dst_off_in_page;
7221 size_t src_off_in_page;
7222 unsigned long dst_end = dst_offset + len - 1;
7223 unsigned long src_end = src_offset + len - 1;
7224 unsigned long dst_i;
7225 unsigned long src_i;
7227 if (check_eb_range(dst, dst_offset, len) ||
7228 check_eb_range(dst, src_offset, len))
7230 if (dst_offset < src_offset) {
7231 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
7235 dst_i = get_eb_page_index(dst_end);
7236 src_i = get_eb_page_index(src_end);
7238 dst_off_in_page = get_eb_offset_in_page(dst, dst_end);
7239 src_off_in_page = get_eb_offset_in_page(dst, src_end);
7241 cur = min_t(unsigned long, len, src_off_in_page + 1);
7242 cur = min(cur, dst_off_in_page + 1);
7243 copy_pages(dst->pages[dst_i], dst->pages[src_i],
7244 dst_off_in_page - cur + 1,
7245 src_off_in_page - cur + 1, cur);
7253 #define GANG_LOOKUP_SIZE 16
7254 static struct extent_buffer *get_next_extent_buffer(
7255 struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
7257 struct extent_buffer *gang[GANG_LOOKUP_SIZE];
7258 struct extent_buffer *found = NULL;
7259 u64 page_start = page_offset(page);
7260 u64 cur = page_start;
7262 ASSERT(in_range(bytenr, page_start, PAGE_SIZE));
7263 lockdep_assert_held(&fs_info->buffer_lock);
7265 while (cur < page_start + PAGE_SIZE) {
7269 ret = radix_tree_gang_lookup(&fs_info->buffer_radix,
7270 (void **)gang, cur >> fs_info->sectorsize_bits,
7271 min_t(unsigned int, GANG_LOOKUP_SIZE,
7272 PAGE_SIZE / fs_info->nodesize));
7275 for (i = 0; i < ret; i++) {
7276 /* Already beyond page end */
7277 if (gang[i]->start >= page_start + PAGE_SIZE)
7280 if (gang[i]->start >= bytenr) {
7285 cur = gang[ret - 1]->start + gang[ret - 1]->len;
7291 static int try_release_subpage_extent_buffer(struct page *page)
7293 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
7294 u64 cur = page_offset(page);
7295 const u64 end = page_offset(page) + PAGE_SIZE;
7299 struct extent_buffer *eb = NULL;
7302 * Unlike try_release_extent_buffer() which uses page->private
7303 * to grab buffer, for subpage case we rely on radix tree, thus
7304 * we need to ensure radix tree consistency.
7306 * We also want an atomic snapshot of the radix tree, thus go
7307 * with spinlock rather than RCU.
7309 spin_lock(&fs_info->buffer_lock);
7310 eb = get_next_extent_buffer(fs_info, page, cur);
7312 /* No more eb in the page range after or at cur */
7313 spin_unlock(&fs_info->buffer_lock);
7316 cur = eb->start + eb->len;
7319 * The same as try_release_extent_buffer(), to ensure the eb
7320 * won't disappear out from under us.
7322 spin_lock(&eb->refs_lock);
7323 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
7324 spin_unlock(&eb->refs_lock);
7325 spin_unlock(&fs_info->buffer_lock);
7328 spin_unlock(&fs_info->buffer_lock);
7331 * If tree ref isn't set then we know the ref on this eb is a
7332 * real ref, so just return, this eb will likely be freed soon
7335 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
7336 spin_unlock(&eb->refs_lock);
7341 * Here we don't care about the return value, we will always
7342 * check the page private at the end. And
7343 * release_extent_buffer() will release the refs_lock.
7345 release_extent_buffer(eb);
7348 * Finally to check if we have cleared page private, as if we have
7349 * released all ebs in the page, the page private should be cleared now.
7351 spin_lock(&page->mapping->private_lock);
7352 if (!PagePrivate(page))
7356 spin_unlock(&page->mapping->private_lock);
7361 int try_release_extent_buffer(struct page *page)
7363 struct extent_buffer *eb;
7365 if (btrfs_sb(page->mapping->host->i_sb)->sectorsize < PAGE_SIZE)
7366 return try_release_subpage_extent_buffer(page);
7369 * We need to make sure nobody is changing page->private, as we rely on
7370 * page->private as the pointer to extent buffer.
7372 spin_lock(&page->mapping->private_lock);
7373 if (!PagePrivate(page)) {
7374 spin_unlock(&page->mapping->private_lock);
7378 eb = (struct extent_buffer *)page->private;
7382 * This is a little awful but should be ok, we need to make sure that
7383 * the eb doesn't disappear out from under us while we're looking at
7386 spin_lock(&eb->refs_lock);
7387 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
7388 spin_unlock(&eb->refs_lock);
7389 spin_unlock(&page->mapping->private_lock);
7392 spin_unlock(&page->mapping->private_lock);
7395 * If tree ref isn't set then we know the ref on this eb is a real ref,
7396 * so just return, this page will likely be freed soon anyway.
7398 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
7399 spin_unlock(&eb->refs_lock);
7403 return release_extent_buffer(eb);
7407 * btrfs_readahead_tree_block - attempt to readahead a child block
7408 * @fs_info: the fs_info
7409 * @bytenr: bytenr to read
7410 * @owner_root: objectid of the root that owns this eb
7411 * @gen: generation for the uptodate check, can be 0
7412 * @level: level for the eb
7414 * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a
7415 * normal uptodate check of the eb, without checking the generation. If we have
7416 * to read the block we will not block on anything.
7418 void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
7419 u64 bytenr, u64 owner_root, u64 gen, int level)
7421 struct extent_buffer *eb;
7424 eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level);
7428 if (btrfs_buffer_uptodate(eb, gen, 1)) {
7429 free_extent_buffer(eb);
7433 ret = read_extent_buffer_pages(eb, WAIT_NONE, 0);
7435 free_extent_buffer_stale(eb);
7437 free_extent_buffer(eb);
7441 * btrfs_readahead_node_child - readahead a node's child block
7442 * @node: parent node we're reading from
7443 * @slot: slot in the parent node for the child we want to read
7445 * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at
7446 * the slot in the node provided.
7448 void btrfs_readahead_node_child(struct extent_buffer *node, int slot)
7450 btrfs_readahead_tree_block(node->fs_info,
7451 btrfs_node_blockptr(node, slot),
7452 btrfs_header_owner(node),
7453 btrfs_node_ptr_generation(node, slot),
7454 btrfs_header_level(node) - 1);