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/sched/mm.h>
10 #include <linux/spinlock.h>
11 #include <linux/blkdev.h>
12 #include <linux/swap.h>
13 #include <linux/writeback.h>
14 #include <linux/pagevec.h>
15 #include <linux/prefetch.h>
16 #include <linux/fsverity.h>
18 #include "extent_io.h"
19 #include "extent-io-tree.h"
20 #include "extent_map.h"
22 #include "btrfs_inode.h"
24 #include "check-integrity.h"
26 #include "rcu-string.h"
31 #include "block-group.h"
32 #include "compression.h"
34 static struct kmem_cache *extent_state_cache;
35 static struct kmem_cache *extent_buffer_cache;
36 static struct bio_set btrfs_bioset;
38 static inline bool extent_state_in_tree(const struct extent_state *state)
40 return !RB_EMPTY_NODE(&state->rb_node);
43 #ifdef CONFIG_BTRFS_DEBUG
44 static LIST_HEAD(states);
45 static DEFINE_SPINLOCK(leak_lock);
47 static inline void btrfs_leak_debug_add(spinlock_t *lock,
48 struct list_head *new,
49 struct list_head *head)
53 spin_lock_irqsave(lock, flags);
55 spin_unlock_irqrestore(lock, flags);
58 static inline void btrfs_leak_debug_del(spinlock_t *lock,
59 struct list_head *entry)
63 spin_lock_irqsave(lock, flags);
65 spin_unlock_irqrestore(lock, flags);
68 void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info)
70 struct extent_buffer *eb;
74 * If we didn't get into open_ctree our allocated_ebs will not be
75 * initialized, so just skip this.
77 if (!fs_info->allocated_ebs.next)
80 WARN_ON(!list_empty(&fs_info->allocated_ebs));
81 spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
82 while (!list_empty(&fs_info->allocated_ebs)) {
83 eb = list_first_entry(&fs_info->allocated_ebs,
84 struct extent_buffer, leak_list);
86 "BTRFS: buffer leak start %llu len %lu refs %d bflags %lu owner %llu\n",
87 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags,
88 btrfs_header_owner(eb));
89 list_del(&eb->leak_list);
90 kmem_cache_free(extent_buffer_cache, eb);
92 spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
95 static inline void btrfs_extent_state_leak_debug_check(void)
97 struct extent_state *state;
99 while (!list_empty(&states)) {
100 state = list_entry(states.next, struct extent_state, leak_list);
101 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
102 state->start, state->end, state->state,
103 extent_state_in_tree(state),
104 refcount_read(&state->refs));
105 list_del(&state->leak_list);
106 kmem_cache_free(extent_state_cache, state);
110 #define btrfs_debug_check_extent_io_range(tree, start, end) \
111 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
112 static inline void __btrfs_debug_check_extent_io_range(const char *caller,
113 struct extent_io_tree *tree, u64 start, u64 end)
115 struct inode *inode = tree->private_data;
118 if (!inode || !is_data_inode(inode))
121 isize = i_size_read(inode);
122 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
123 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
124 "%s: ino %llu isize %llu odd range [%llu,%llu]",
125 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
129 #define btrfs_leak_debug_add(lock, new, head) do {} while (0)
130 #define btrfs_leak_debug_del(lock, entry) do {} while (0)
131 #define btrfs_extent_state_leak_debug_check() do {} while (0)
132 #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
138 struct rb_node rb_node;
142 * Structure to record info about the bio being assembled, and other info like
143 * how many bytes are there before stripe/ordered extent boundary.
145 struct btrfs_bio_ctrl {
147 enum btrfs_compression_type compress_type;
148 u32 len_to_stripe_boundary;
149 u32 len_to_oe_boundary;
152 struct extent_page_data {
153 struct btrfs_bio_ctrl bio_ctrl;
154 /* tells writepage not to lock the state bits for this range
155 * it still does the unlocking
157 unsigned int extent_locked:1;
159 /* tells the submit_bio code to use REQ_SYNC */
160 unsigned int sync_io:1;
163 static int add_extent_changeset(struct extent_state *state, u32 bits,
164 struct extent_changeset *changeset,
171 if (set && (state->state & bits) == bits)
173 if (!set && (state->state & bits) == 0)
175 changeset->bytes_changed += state->end - state->start + 1;
176 ret = ulist_add(&changeset->range_changed, state->start, state->end,
181 static void submit_one_bio(struct bio *bio, int mirror_num,
182 enum btrfs_compression_type compress_type)
184 struct extent_io_tree *tree = bio->bi_private;
186 bio->bi_private = NULL;
188 /* Caller should ensure the bio has at least some range added */
189 ASSERT(bio->bi_iter.bi_size);
191 if (is_data_inode(tree->private_data))
192 btrfs_submit_data_bio(tree->private_data, bio, mirror_num,
195 btrfs_submit_metadata_bio(tree->private_data, bio, mirror_num);
197 * Above submission hooks will handle the error by ending the bio,
198 * which will do the cleanup properly. So here we should not return
199 * any error, or the caller of submit_extent_page() will do cleanup
200 * again, causing problems.
204 /* Cleanup unsubmitted bios */
205 static void end_write_bio(struct extent_page_data *epd, int ret)
207 struct bio *bio = epd->bio_ctrl.bio;
210 bio->bi_status = errno_to_blk_status(ret);
212 epd->bio_ctrl.bio = NULL;
217 * Submit bio from extent page data via submit_one_bio
219 * Return 0 if everything is OK.
220 * Return <0 for error.
222 static void flush_write_bio(struct extent_page_data *epd)
224 struct bio *bio = epd->bio_ctrl.bio;
227 submit_one_bio(bio, 0, 0);
229 * Clean up of epd->bio is handled by its endio function.
230 * And endio is either triggered by successful bio execution
231 * or the error handler of submit bio hook.
232 * So at this point, no matter what happened, we don't need
233 * to clean up epd->bio.
235 epd->bio_ctrl.bio = NULL;
239 int __init extent_state_cache_init(void)
241 extent_state_cache = kmem_cache_create("btrfs_extent_state",
242 sizeof(struct extent_state), 0,
243 SLAB_MEM_SPREAD, NULL);
244 if (!extent_state_cache)
249 int __init extent_io_init(void)
251 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
252 sizeof(struct extent_buffer), 0,
253 SLAB_MEM_SPREAD, NULL);
254 if (!extent_buffer_cache)
257 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
258 offsetof(struct btrfs_bio, bio),
260 goto free_buffer_cache;
262 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
268 bioset_exit(&btrfs_bioset);
271 kmem_cache_destroy(extent_buffer_cache);
272 extent_buffer_cache = NULL;
276 void __cold extent_state_cache_exit(void)
278 btrfs_extent_state_leak_debug_check();
279 kmem_cache_destroy(extent_state_cache);
282 void __cold extent_io_exit(void)
285 * Make sure all delayed rcu free are flushed before we
289 kmem_cache_destroy(extent_buffer_cache);
290 bioset_exit(&btrfs_bioset);
294 * For the file_extent_tree, we want to hold the inode lock when we lookup and
295 * update the disk_i_size, but lockdep will complain because our io_tree we hold
296 * the tree lock and get the inode lock when setting delalloc. These two things
297 * are unrelated, so make a class for the file_extent_tree so we don't get the
298 * two locking patterns mixed up.
300 static struct lock_class_key file_extent_tree_class;
302 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
303 struct extent_io_tree *tree, unsigned int owner,
306 tree->fs_info = fs_info;
307 tree->state = RB_ROOT;
308 tree->dirty_bytes = 0;
309 spin_lock_init(&tree->lock);
310 tree->private_data = private_data;
312 if (owner == IO_TREE_INODE_FILE_EXTENT)
313 lockdep_set_class(&tree->lock, &file_extent_tree_class);
316 void extent_io_tree_release(struct extent_io_tree *tree)
318 spin_lock(&tree->lock);
320 * Do a single barrier for the waitqueue_active check here, the state
321 * of the waitqueue should not change once extent_io_tree_release is
325 while (!RB_EMPTY_ROOT(&tree->state)) {
326 struct rb_node *node;
327 struct extent_state *state;
329 node = rb_first(&tree->state);
330 state = rb_entry(node, struct extent_state, rb_node);
331 rb_erase(&state->rb_node, &tree->state);
332 RB_CLEAR_NODE(&state->rb_node);
334 * btree io trees aren't supposed to have tasks waiting for
335 * changes in the flags of extent states ever.
337 ASSERT(!waitqueue_active(&state->wq));
338 free_extent_state(state);
340 cond_resched_lock(&tree->lock);
342 spin_unlock(&tree->lock);
345 static struct extent_state *alloc_extent_state(gfp_t mask)
347 struct extent_state *state;
350 * The given mask might be not appropriate for the slab allocator,
351 * drop the unsupported bits
353 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
354 state = kmem_cache_alloc(extent_state_cache, mask);
358 state->failrec = NULL;
359 RB_CLEAR_NODE(&state->rb_node);
360 btrfs_leak_debug_add(&leak_lock, &state->leak_list, &states);
361 refcount_set(&state->refs, 1);
362 init_waitqueue_head(&state->wq);
363 trace_alloc_extent_state(state, mask, _RET_IP_);
367 void free_extent_state(struct extent_state *state)
371 if (refcount_dec_and_test(&state->refs)) {
372 WARN_ON(extent_state_in_tree(state));
373 btrfs_leak_debug_del(&leak_lock, &state->leak_list);
374 trace_free_extent_state(state, _RET_IP_);
375 kmem_cache_free(extent_state_cache, state);
379 static struct rb_node *tree_insert(struct rb_root *root,
380 struct rb_node *search_start,
382 struct rb_node *node,
383 struct rb_node ***p_in,
384 struct rb_node **parent_in)
387 struct rb_node *parent = NULL;
388 struct tree_entry *entry;
390 if (p_in && parent_in) {
396 p = search_start ? &search_start : &root->rb_node;
399 entry = rb_entry(parent, struct tree_entry, rb_node);
401 if (offset < entry->start)
403 else if (offset > entry->end)
410 rb_link_node(node, parent, p);
411 rb_insert_color(node, root);
416 * Search @tree for an entry that contains @offset. Such entry would have
417 * entry->start <= offset && entry->end >= offset.
419 * @tree: the tree to search
420 * @offset: offset that should fall within an entry in @tree
421 * @next_ret: pointer to the first entry whose range ends after @offset
422 * @prev_ret: pointer to the first entry whose range begins before @offset
423 * @p_ret: pointer where new node should be anchored (used when inserting an
425 * @parent_ret: points to entry which would have been the parent of the entry,
428 * This function returns a pointer to the entry that contains @offset byte
429 * address. If no such entry exists, then NULL is returned and the other
430 * pointer arguments to the function are filled, otherwise the found entry is
431 * returned and other pointers are left untouched.
433 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
434 struct rb_node **next_ret,
435 struct rb_node **prev_ret,
436 struct rb_node ***p_ret,
437 struct rb_node **parent_ret)
439 struct rb_root *root = &tree->state;
440 struct rb_node **n = &root->rb_node;
441 struct rb_node *prev = NULL;
442 struct rb_node *orig_prev = NULL;
443 struct tree_entry *entry;
444 struct tree_entry *prev_entry = NULL;
448 entry = rb_entry(prev, struct tree_entry, rb_node);
451 if (offset < entry->start)
453 else if (offset > entry->end)
466 while (prev && offset > prev_entry->end) {
467 prev = rb_next(prev);
468 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
475 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
476 while (prev && offset < prev_entry->start) {
477 prev = rb_prev(prev);
478 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
485 static inline struct rb_node *
486 tree_search_for_insert(struct extent_io_tree *tree,
488 struct rb_node ***p_ret,
489 struct rb_node **parent_ret)
491 struct rb_node *next= NULL;
494 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
500 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
503 return tree_search_for_insert(tree, offset, NULL, NULL);
507 * utility function to look for merge candidates inside a given range.
508 * Any extents with matching state are merged together into a single
509 * extent in the tree. Extents with EXTENT_IO in their state field
510 * are not merged because the end_io handlers need to be able to do
511 * operations on them without sleeping (or doing allocations/splits).
513 * This should be called with the tree lock held.
515 static void merge_state(struct extent_io_tree *tree,
516 struct extent_state *state)
518 struct extent_state *other;
519 struct rb_node *other_node;
521 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
524 other_node = rb_prev(&state->rb_node);
526 other = rb_entry(other_node, struct extent_state, rb_node);
527 if (other->end == state->start - 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->start = other->start;
534 rb_erase(&other->rb_node, &tree->state);
535 RB_CLEAR_NODE(&other->rb_node);
536 free_extent_state(other);
539 other_node = rb_next(&state->rb_node);
541 other = rb_entry(other_node, struct extent_state, rb_node);
542 if (other->start == state->end + 1 &&
543 other->state == state->state) {
544 if (tree->private_data &&
545 is_data_inode(tree->private_data))
546 btrfs_merge_delalloc_extent(tree->private_data,
548 state->end = other->end;
549 rb_erase(&other->rb_node, &tree->state);
550 RB_CLEAR_NODE(&other->rb_node);
551 free_extent_state(other);
556 static void set_state_bits(struct extent_io_tree *tree,
557 struct extent_state *state, u32 *bits,
558 struct extent_changeset *changeset);
561 * insert an extent_state struct into the tree. 'bits' are set on the
562 * struct before it is inserted.
564 * This may return -EEXIST if the extent is already there, in which case the
565 * state struct is freed.
567 * The tree lock is not taken internally. This is a utility function and
568 * probably isn't what you want to call (see set/clear_extent_bit).
570 static int insert_state(struct extent_io_tree *tree,
571 struct extent_state *state, u64 start, u64 end,
573 struct rb_node **parent,
574 u32 *bits, struct extent_changeset *changeset)
576 struct rb_node *node;
579 btrfs_err(tree->fs_info,
580 "insert state: end < start %llu %llu", end, start);
583 state->start = start;
586 set_state_bits(tree, state, bits, changeset);
588 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
590 struct extent_state *found;
591 found = rb_entry(node, struct extent_state, rb_node);
592 btrfs_err(tree->fs_info,
593 "found node %llu %llu on insert of %llu %llu",
594 found->start, found->end, start, end);
597 merge_state(tree, state);
602 * split a given extent state struct in two, inserting the preallocated
603 * struct 'prealloc' as the newly created second half. 'split' indicates an
604 * offset inside 'orig' where it should be split.
607 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
608 * are two extent state structs in the tree:
609 * prealloc: [orig->start, split - 1]
610 * orig: [ split, orig->end ]
612 * The tree locks are not taken by this function. They need to be held
615 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
616 struct extent_state *prealloc, u64 split)
618 struct rb_node *node;
620 if (tree->private_data && is_data_inode(tree->private_data))
621 btrfs_split_delalloc_extent(tree->private_data, orig, split);
623 prealloc->start = orig->start;
624 prealloc->end = split - 1;
625 prealloc->state = orig->state;
628 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
629 &prealloc->rb_node, NULL, NULL);
631 free_extent_state(prealloc);
637 static struct extent_state *next_state(struct extent_state *state)
639 struct rb_node *next = rb_next(&state->rb_node);
641 return rb_entry(next, struct extent_state, rb_node);
647 * utility function to clear some bits in an extent state struct.
648 * it will optionally wake up anyone waiting on this state (wake == 1).
650 * If no bits are set on the state struct after clearing things, the
651 * struct is freed and removed from the tree
653 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
654 struct extent_state *state,
656 struct extent_changeset *changeset)
658 struct extent_state *next;
659 u32 bits_to_clear = *bits & ~EXTENT_CTLBITS;
662 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
663 u64 range = state->end - state->start + 1;
664 WARN_ON(range > tree->dirty_bytes);
665 tree->dirty_bytes -= range;
668 if (tree->private_data && is_data_inode(tree->private_data))
669 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
671 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
673 state->state &= ~bits_to_clear;
676 if (state->state == 0) {
677 next = next_state(state);
678 if (extent_state_in_tree(state)) {
679 rb_erase(&state->rb_node, &tree->state);
680 RB_CLEAR_NODE(&state->rb_node);
681 free_extent_state(state);
686 merge_state(tree, state);
687 next = next_state(state);
692 static struct extent_state *
693 alloc_extent_state_atomic(struct extent_state *prealloc)
696 prealloc = alloc_extent_state(GFP_ATOMIC);
701 static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
703 btrfs_panic(tree->fs_info, err,
704 "locking error: extent tree was modified by another thread while locked");
708 * clear some bits on a range in the tree. This may require splitting
709 * or inserting elements in the tree, so the gfp mask is used to
710 * indicate which allocations or sleeping are allowed.
712 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
713 * the given range from the tree regardless of state (ie for truncate).
715 * the range [start, end] is inclusive.
717 * This takes the tree lock, and returns 0 on success and < 0 on error.
719 int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
720 u32 bits, int wake, int delete,
721 struct extent_state **cached_state,
722 gfp_t mask, struct extent_changeset *changeset)
724 struct extent_state *state;
725 struct extent_state *cached;
726 struct extent_state *prealloc = NULL;
727 struct rb_node *node;
732 btrfs_debug_check_extent_io_range(tree, start, end);
733 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
735 if (bits & EXTENT_DELALLOC)
736 bits |= EXTENT_NORESERVE;
739 bits |= ~EXTENT_CTLBITS;
741 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
744 if (!prealloc && gfpflags_allow_blocking(mask)) {
746 * Don't care for allocation failure here because we might end
747 * up not needing the pre-allocated extent state at all, which
748 * is the case if we only have in the tree extent states that
749 * cover our input range and don't cover too any other range.
750 * If we end up needing a new extent state we allocate it later.
752 prealloc = alloc_extent_state(mask);
755 spin_lock(&tree->lock);
757 cached = *cached_state;
760 *cached_state = NULL;
764 if (cached && extent_state_in_tree(cached) &&
765 cached->start <= start && cached->end > start) {
767 refcount_dec(&cached->refs);
772 free_extent_state(cached);
775 * this search will find the extents that end after
778 node = tree_search(tree, start);
781 state = rb_entry(node, struct extent_state, rb_node);
783 if (state->start > end)
785 WARN_ON(state->end < start);
786 last_end = state->end;
788 /* the state doesn't have the wanted bits, go ahead */
789 if (!(state->state & bits)) {
790 state = next_state(state);
795 * | ---- desired range ---- |
797 * | ------------- state -------------- |
799 * We need to split the extent we found, and may flip
800 * bits on second half.
802 * If the extent we found extends past our range, we
803 * just split and search again. It'll get split again
804 * the next time though.
806 * If the extent we found is inside our range, we clear
807 * the desired bit on it.
810 if (state->start < start) {
811 prealloc = alloc_extent_state_atomic(prealloc);
813 err = split_state(tree, state, prealloc, start);
815 extent_io_tree_panic(tree, err);
820 if (state->end <= end) {
821 state = clear_state_bit(tree, state, &bits, wake,
828 * | ---- desired range ---- |
830 * We need to split the extent, and clear the bit
833 if (state->start <= end && state->end > end) {
834 prealloc = alloc_extent_state_atomic(prealloc);
836 err = split_state(tree, state, prealloc, end + 1);
838 extent_io_tree_panic(tree, err);
843 clear_state_bit(tree, prealloc, &bits, wake, changeset);
849 state = clear_state_bit(tree, state, &bits, wake, changeset);
851 if (last_end == (u64)-1)
853 start = last_end + 1;
854 if (start <= end && state && !need_resched())
860 spin_unlock(&tree->lock);
861 if (gfpflags_allow_blocking(mask))
866 spin_unlock(&tree->lock);
868 free_extent_state(prealloc);
874 static void wait_on_state(struct extent_io_tree *tree,
875 struct extent_state *state)
876 __releases(tree->lock)
877 __acquires(tree->lock)
880 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
881 spin_unlock(&tree->lock);
883 spin_lock(&tree->lock);
884 finish_wait(&state->wq, &wait);
888 * waits for one or more bits to clear on a range in the state tree.
889 * The range [start, end] is inclusive.
890 * The tree lock is taken by this function
892 static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
895 struct extent_state *state;
896 struct rb_node *node;
898 btrfs_debug_check_extent_io_range(tree, start, end);
900 spin_lock(&tree->lock);
904 * this search will find all the extents that end after
907 node = tree_search(tree, start);
912 state = rb_entry(node, struct extent_state, rb_node);
914 if (state->start > end)
917 if (state->state & bits) {
918 start = state->start;
919 refcount_inc(&state->refs);
920 wait_on_state(tree, state);
921 free_extent_state(state);
924 start = state->end + 1;
929 if (!cond_resched_lock(&tree->lock)) {
930 node = rb_next(node);
935 spin_unlock(&tree->lock);
938 static void set_state_bits(struct extent_io_tree *tree,
939 struct extent_state *state,
940 u32 *bits, struct extent_changeset *changeset)
942 u32 bits_to_set = *bits & ~EXTENT_CTLBITS;
945 if (tree->private_data && is_data_inode(tree->private_data))
946 btrfs_set_delalloc_extent(tree->private_data, state, bits);
948 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
949 u64 range = state->end - state->start + 1;
950 tree->dirty_bytes += range;
952 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
954 state->state |= bits_to_set;
957 static void cache_state_if_flags(struct extent_state *state,
958 struct extent_state **cached_ptr,
961 if (cached_ptr && !(*cached_ptr)) {
962 if (!flags || (state->state & flags)) {
964 refcount_inc(&state->refs);
969 static void cache_state(struct extent_state *state,
970 struct extent_state **cached_ptr)
972 return cache_state_if_flags(state, cached_ptr,
973 EXTENT_LOCKED | EXTENT_BOUNDARY);
977 * set some bits on a range in the tree. This may require allocations or
978 * sleeping, so the gfp mask is used to indicate what is allowed.
980 * If any of the exclusive bits are set, this will fail with -EEXIST if some
981 * part of the range already has the desired bits set. The start of the
982 * existing range is returned in failed_start in this case.
984 * [start, end] is inclusive This takes the tree lock.
986 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
987 u32 exclusive_bits, u64 *failed_start,
988 struct extent_state **cached_state, gfp_t mask,
989 struct extent_changeset *changeset)
991 struct extent_state *state;
992 struct extent_state *prealloc = NULL;
993 struct rb_node *node;
995 struct rb_node *parent;
1000 btrfs_debug_check_extent_io_range(tree, start, end);
1001 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
1004 ASSERT(failed_start);
1006 ASSERT(failed_start == NULL);
1008 if (!prealloc && gfpflags_allow_blocking(mask)) {
1010 * Don't care for allocation failure here because we might end
1011 * up not needing the pre-allocated extent state at all, which
1012 * is the case if we only have in the tree extent states that
1013 * cover our input range and don't cover too any other range.
1014 * If we end up needing a new extent state we allocate it later.
1016 prealloc = alloc_extent_state(mask);
1019 spin_lock(&tree->lock);
1020 if (cached_state && *cached_state) {
1021 state = *cached_state;
1022 if (state->start <= start && state->end > start &&
1023 extent_state_in_tree(state)) {
1024 node = &state->rb_node;
1029 * this search will find all the extents that end after
1032 node = tree_search_for_insert(tree, start, &p, &parent);
1034 prealloc = alloc_extent_state_atomic(prealloc);
1036 err = insert_state(tree, prealloc, start, end,
1037 &p, &parent, &bits, changeset);
1039 extent_io_tree_panic(tree, err);
1041 cache_state(prealloc, cached_state);
1045 state = rb_entry(node, struct extent_state, rb_node);
1047 last_start = state->start;
1048 last_end = state->end;
1051 * | ---- desired range ---- |
1054 * Just lock what we found and keep going
1056 if (state->start == start && state->end <= end) {
1057 if (state->state & exclusive_bits) {
1058 *failed_start = state->start;
1063 set_state_bits(tree, state, &bits, changeset);
1064 cache_state(state, cached_state);
1065 merge_state(tree, state);
1066 if (last_end == (u64)-1)
1068 start = last_end + 1;
1069 state = next_state(state);
1070 if (start < end && state && state->start == start &&
1077 * | ---- desired range ---- |
1080 * | ------------- state -------------- |
1082 * We need to split the extent we found, and may flip bits on
1085 * If the extent we found extends past our
1086 * range, we just split and search again. It'll get split
1087 * again the next time though.
1089 * If the extent we found is inside our range, we set the
1090 * desired bit on it.
1092 if (state->start < start) {
1093 if (state->state & exclusive_bits) {
1094 *failed_start = start;
1100 * If this extent already has all the bits we want set, then
1101 * skip it, not necessary to split it or do anything with it.
1103 if ((state->state & bits) == bits) {
1104 start = state->end + 1;
1105 cache_state(state, cached_state);
1109 prealloc = alloc_extent_state_atomic(prealloc);
1111 err = split_state(tree, state, prealloc, start);
1113 extent_io_tree_panic(tree, err);
1118 if (state->end <= end) {
1119 set_state_bits(tree, state, &bits, changeset);
1120 cache_state(state, cached_state);
1121 merge_state(tree, state);
1122 if (last_end == (u64)-1)
1124 start = last_end + 1;
1125 state = next_state(state);
1126 if (start < end && state && state->start == start &&
1133 * | ---- desired range ---- |
1134 * | state | or | state |
1136 * There's a hole, we need to insert something in it and
1137 * ignore the extent we found.
1139 if (state->start > start) {
1141 if (end < last_start)
1144 this_end = last_start - 1;
1146 prealloc = alloc_extent_state_atomic(prealloc);
1150 * Avoid to free 'prealloc' if it can be merged with
1153 err = insert_state(tree, prealloc, start, this_end,
1154 NULL, NULL, &bits, changeset);
1156 extent_io_tree_panic(tree, err);
1158 cache_state(prealloc, cached_state);
1160 start = this_end + 1;
1164 * | ---- desired range ---- |
1166 * We need to split the extent, and set the bit
1169 if (state->start <= end && state->end > end) {
1170 if (state->state & exclusive_bits) {
1171 *failed_start = start;
1176 prealloc = alloc_extent_state_atomic(prealloc);
1178 err = split_state(tree, state, prealloc, end + 1);
1180 extent_io_tree_panic(tree, err);
1182 set_state_bits(tree, prealloc, &bits, changeset);
1183 cache_state(prealloc, cached_state);
1184 merge_state(tree, prealloc);
1192 spin_unlock(&tree->lock);
1193 if (gfpflags_allow_blocking(mask))
1198 spin_unlock(&tree->lock);
1200 free_extent_state(prealloc);
1207 * convert_extent_bit - convert all bits in a given range from one bit to
1209 * @tree: the io tree to search
1210 * @start: the start offset in bytes
1211 * @end: the end offset in bytes (inclusive)
1212 * @bits: the bits to set in this range
1213 * @clear_bits: the bits to clear in this range
1214 * @cached_state: state that we're going to cache
1216 * This will go through and set bits for the given range. If any states exist
1217 * already in this range they are set with the given bit and cleared of the
1218 * clear_bits. This is only meant to be used by things that are mergeable, ie
1219 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1220 * boundary bits like LOCK.
1222 * All allocations are done with GFP_NOFS.
1224 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1225 u32 bits, u32 clear_bits,
1226 struct extent_state **cached_state)
1228 struct extent_state *state;
1229 struct extent_state *prealloc = NULL;
1230 struct rb_node *node;
1232 struct rb_node *parent;
1236 bool first_iteration = true;
1238 btrfs_debug_check_extent_io_range(tree, start, end);
1239 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1245 * Best effort, don't worry if extent state allocation fails
1246 * here for the first iteration. We might have a cached state
1247 * that matches exactly the target range, in which case no
1248 * extent state allocations are needed. We'll only know this
1249 * after locking the tree.
1251 prealloc = alloc_extent_state(GFP_NOFS);
1252 if (!prealloc && !first_iteration)
1256 spin_lock(&tree->lock);
1257 if (cached_state && *cached_state) {
1258 state = *cached_state;
1259 if (state->start <= start && state->end > start &&
1260 extent_state_in_tree(state)) {
1261 node = &state->rb_node;
1267 * this search will find all the extents that end after
1270 node = tree_search_for_insert(tree, start, &p, &parent);
1272 prealloc = alloc_extent_state_atomic(prealloc);
1277 err = insert_state(tree, prealloc, start, end,
1278 &p, &parent, &bits, NULL);
1280 extent_io_tree_panic(tree, err);
1281 cache_state(prealloc, cached_state);
1285 state = rb_entry(node, struct extent_state, rb_node);
1287 last_start = state->start;
1288 last_end = state->end;
1291 * | ---- desired range ---- |
1294 * Just lock what we found and keep going
1296 if (state->start == start && state->end <= end) {
1297 set_state_bits(tree, state, &bits, NULL);
1298 cache_state(state, cached_state);
1299 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
1300 if (last_end == (u64)-1)
1302 start = last_end + 1;
1303 if (start < end && state && state->start == start &&
1310 * | ---- desired range ---- |
1313 * | ------------- state -------------- |
1315 * We need to split the extent we found, and may flip bits on
1318 * If the extent we found extends past our
1319 * range, we just split and search again. It'll get split
1320 * again the next time though.
1322 * If the extent we found is inside our range, we set the
1323 * desired bit on it.
1325 if (state->start < start) {
1326 prealloc = alloc_extent_state_atomic(prealloc);
1331 err = split_state(tree, state, prealloc, start);
1333 extent_io_tree_panic(tree, err);
1337 if (state->end <= end) {
1338 set_state_bits(tree, state, &bits, NULL);
1339 cache_state(state, cached_state);
1340 state = clear_state_bit(tree, state, &clear_bits, 0,
1342 if (last_end == (u64)-1)
1344 start = last_end + 1;
1345 if (start < end && state && state->start == start &&
1352 * | ---- desired range ---- |
1353 * | state | or | state |
1355 * There's a hole, we need to insert something in it and
1356 * ignore the extent we found.
1358 if (state->start > start) {
1360 if (end < last_start)
1363 this_end = last_start - 1;
1365 prealloc = alloc_extent_state_atomic(prealloc);
1372 * Avoid to free 'prealloc' if it can be merged with
1375 err = insert_state(tree, prealloc, start, this_end,
1376 NULL, NULL, &bits, NULL);
1378 extent_io_tree_panic(tree, err);
1379 cache_state(prealloc, cached_state);
1381 start = this_end + 1;
1385 * | ---- desired range ---- |
1387 * We need to split the extent, and set the bit
1390 if (state->start <= end && state->end > end) {
1391 prealloc = alloc_extent_state_atomic(prealloc);
1397 err = split_state(tree, state, prealloc, end + 1);
1399 extent_io_tree_panic(tree, err);
1401 set_state_bits(tree, prealloc, &bits, NULL);
1402 cache_state(prealloc, cached_state);
1403 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
1411 spin_unlock(&tree->lock);
1413 first_iteration = false;
1417 spin_unlock(&tree->lock);
1419 free_extent_state(prealloc);
1424 /* wrappers around set/clear extent bit */
1425 int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1426 u32 bits, struct extent_changeset *changeset)
1429 * We don't support EXTENT_LOCKED yet, as current changeset will
1430 * record any bits changed, so for EXTENT_LOCKED case, it will
1431 * either fail with -EEXIST or changeset will record the whole
1434 BUG_ON(bits & EXTENT_LOCKED);
1436 return set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
1440 int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
1443 return set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
1447 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1448 u32 bits, int wake, int delete,
1449 struct extent_state **cached)
1451 return __clear_extent_bit(tree, start, end, bits, wake, delete,
1452 cached, GFP_NOFS, NULL);
1455 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1456 u32 bits, struct extent_changeset *changeset)
1459 * Don't support EXTENT_LOCKED case, same reason as
1460 * set_record_extent_bits().
1462 BUG_ON(bits & EXTENT_LOCKED);
1464 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
1469 * either insert or lock state struct between start and end use mask to tell
1470 * us if waiting is desired.
1472 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1473 struct extent_state **cached_state)
1479 err = set_extent_bit(tree, start, end, EXTENT_LOCKED,
1480 EXTENT_LOCKED, &failed_start,
1481 cached_state, GFP_NOFS, NULL);
1482 if (err == -EEXIST) {
1483 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1484 start = failed_start;
1487 WARN_ON(start > end);
1492 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1497 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1498 &failed_start, NULL, GFP_NOFS, NULL);
1499 if (err == -EEXIST) {
1500 if (failed_start > start)
1501 clear_extent_bit(tree, start, failed_start - 1,
1502 EXTENT_LOCKED, 1, 0, NULL);
1508 void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1510 unsigned long index = start >> PAGE_SHIFT;
1511 unsigned long end_index = end >> PAGE_SHIFT;
1514 while (index <= end_index) {
1515 page = find_get_page(inode->i_mapping, index);
1516 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1517 clear_page_dirty_for_io(page);
1523 void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1525 struct address_space *mapping = inode->i_mapping;
1526 unsigned long index = start >> PAGE_SHIFT;
1527 unsigned long end_index = end >> PAGE_SHIFT;
1528 struct folio *folio;
1530 while (index <= end_index) {
1531 folio = filemap_get_folio(mapping, index);
1532 filemap_dirty_folio(mapping, folio);
1533 folio_account_redirty(folio);
1534 index += folio_nr_pages(folio);
1539 /* find the first state struct with 'bits' set after 'start', and
1540 * return it. tree->lock must be held. NULL will returned if
1541 * nothing was found after 'start'
1543 static struct extent_state *
1544 find_first_extent_bit_state(struct extent_io_tree *tree, u64 start, u32 bits)
1546 struct rb_node *node;
1547 struct extent_state *state;
1550 * this search will find all the extents that end after
1553 node = tree_search(tree, start);
1558 state = rb_entry(node, struct extent_state, rb_node);
1559 if (state->end >= start && (state->state & bits))
1562 node = rb_next(node);
1571 * Find the first offset in the io tree with one or more @bits set.
1573 * Note: If there are multiple bits set in @bits, any of them will match.
1575 * Return 0 if we find something, and update @start_ret and @end_ret.
1576 * Return 1 if we found nothing.
1578 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1579 u64 *start_ret, u64 *end_ret, u32 bits,
1580 struct extent_state **cached_state)
1582 struct extent_state *state;
1585 spin_lock(&tree->lock);
1586 if (cached_state && *cached_state) {
1587 state = *cached_state;
1588 if (state->end == start - 1 && extent_state_in_tree(state)) {
1589 while ((state = next_state(state)) != NULL) {
1590 if (state->state & bits)
1593 free_extent_state(*cached_state);
1594 *cached_state = NULL;
1597 free_extent_state(*cached_state);
1598 *cached_state = NULL;
1601 state = find_first_extent_bit_state(tree, start, bits);
1604 cache_state_if_flags(state, cached_state, 0);
1605 *start_ret = state->start;
1606 *end_ret = state->end;
1610 spin_unlock(&tree->lock);
1615 * Find a contiguous area of bits
1617 * @tree: io tree to check
1618 * @start: offset to start the search from
1619 * @start_ret: the first offset we found with the bits set
1620 * @end_ret: the final contiguous range of the bits that were set
1621 * @bits: bits to look for
1623 * set_extent_bit and clear_extent_bit can temporarily split contiguous ranges
1624 * to set bits appropriately, and then merge them again. During this time it
1625 * will drop the tree->lock, so use this helper if you want to find the actual
1626 * contiguous area for given bits. We will search to the first bit we find, and
1627 * then walk down the tree until we find a non-contiguous area. The area
1628 * returned will be the full contiguous area with the bits set.
1630 int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
1631 u64 *start_ret, u64 *end_ret, u32 bits)
1633 struct extent_state *state;
1636 spin_lock(&tree->lock);
1637 state = find_first_extent_bit_state(tree, start, bits);
1639 *start_ret = state->start;
1640 *end_ret = state->end;
1641 while ((state = next_state(state)) != NULL) {
1642 if (state->start > (*end_ret + 1))
1644 *end_ret = state->end;
1648 spin_unlock(&tree->lock);
1653 * Find the first range that has @bits not set. This range could start before
1656 * @tree: the tree to search
1657 * @start: offset at/after which the found extent should start
1658 * @start_ret: records the beginning of the range
1659 * @end_ret: records the end of the range (inclusive)
1660 * @bits: the set of bits which must be unset
1662 * Since unallocated range is also considered one which doesn't have the bits
1663 * set it's possible that @end_ret contains -1, this happens in case the range
1664 * spans (last_range_end, end of device]. In this case it's up to the caller to
1665 * trim @end_ret to the appropriate size.
1667 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
1668 u64 *start_ret, u64 *end_ret, u32 bits)
1670 struct extent_state *state;
1671 struct rb_node *node, *prev = NULL, *next;
1673 spin_lock(&tree->lock);
1675 /* Find first extent with bits cleared */
1677 node = __etree_search(tree, start, &next, &prev, NULL, NULL);
1678 if (!node && !next && !prev) {
1680 * Tree is completely empty, send full range and let
1681 * caller deal with it
1686 } else if (!node && !next) {
1688 * We are past the last allocated chunk, set start at
1689 * the end of the last extent.
1691 state = rb_entry(prev, struct extent_state, rb_node);
1692 *start_ret = state->end + 1;
1699 * At this point 'node' either contains 'start' or start is
1702 state = rb_entry(node, struct extent_state, rb_node);
1704 if (in_range(start, state->start, state->end - state->start + 1)) {
1705 if (state->state & bits) {
1707 * |--range with bits sets--|
1711 start = state->end + 1;
1714 * 'start' falls within a range that doesn't
1715 * have the bits set, so take its start as
1716 * the beginning of the desired range
1718 * |--range with bits cleared----|
1722 *start_ret = state->start;
1727 * |---prev range---|---hole/unset---|---node range---|
1733 * |---hole/unset--||--first node--|
1738 state = rb_entry(prev, struct extent_state,
1740 *start_ret = state->end + 1;
1749 * Find the longest stretch from start until an entry which has the
1753 state = rb_entry(node, struct extent_state, rb_node);
1754 if (state->end >= start && !(state->state & bits)) {
1755 *end_ret = state->end;
1757 *end_ret = state->start - 1;
1761 node = rb_next(node);
1766 spin_unlock(&tree->lock);
1770 * find a contiguous range of bytes in the file marked as delalloc, not
1771 * more than 'max_bytes'. start and end are used to return the range,
1773 * true is returned if we find something, false if nothing was in the tree
1775 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
1776 u64 *end, u64 max_bytes,
1777 struct extent_state **cached_state)
1779 struct rb_node *node;
1780 struct extent_state *state;
1781 u64 cur_start = *start;
1783 u64 total_bytes = 0;
1785 spin_lock(&tree->lock);
1788 * this search will find all the extents that end after
1791 node = tree_search(tree, cur_start);
1798 state = rb_entry(node, struct extent_state, rb_node);
1799 if (found && (state->start != cur_start ||
1800 (state->state & EXTENT_BOUNDARY))) {
1803 if (!(state->state & EXTENT_DELALLOC)) {
1809 *start = state->start;
1810 *cached_state = state;
1811 refcount_inc(&state->refs);
1815 cur_start = state->end + 1;
1816 node = rb_next(node);
1817 total_bytes += state->end - state->start + 1;
1818 if (total_bytes >= max_bytes)
1824 spin_unlock(&tree->lock);
1829 * Process one page for __process_pages_contig().
1831 * Return >0 if we hit @page == @locked_page.
1832 * Return 0 if we updated the page status.
1833 * Return -EGAIN if the we need to try again.
1834 * (For PAGE_LOCK case but got dirty page or page not belong to mapping)
1836 static int process_one_page(struct btrfs_fs_info *fs_info,
1837 struct address_space *mapping,
1838 struct page *page, struct page *locked_page,
1839 unsigned long page_ops, u64 start, u64 end)
1843 ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX);
1844 len = end + 1 - start;
1846 if (page_ops & PAGE_SET_ORDERED)
1847 btrfs_page_clamp_set_ordered(fs_info, page, start, len);
1848 if (page_ops & PAGE_SET_ERROR)
1849 btrfs_page_clamp_set_error(fs_info, page, start, len);
1850 if (page_ops & PAGE_START_WRITEBACK) {
1851 btrfs_page_clamp_clear_dirty(fs_info, page, start, len);
1852 btrfs_page_clamp_set_writeback(fs_info, page, start, len);
1854 if (page_ops & PAGE_END_WRITEBACK)
1855 btrfs_page_clamp_clear_writeback(fs_info, page, start, len);
1857 if (page == locked_page)
1860 if (page_ops & PAGE_LOCK) {
1863 ret = btrfs_page_start_writer_lock(fs_info, page, start, len);
1866 if (!PageDirty(page) || page->mapping != mapping) {
1867 btrfs_page_end_writer_lock(fs_info, page, start, len);
1871 if (page_ops & PAGE_UNLOCK)
1872 btrfs_page_end_writer_lock(fs_info, page, start, len);
1876 static int __process_pages_contig(struct address_space *mapping,
1877 struct page *locked_page,
1878 u64 start, u64 end, unsigned long page_ops,
1881 struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb);
1882 pgoff_t start_index = start >> PAGE_SHIFT;
1883 pgoff_t end_index = end >> PAGE_SHIFT;
1884 pgoff_t index = start_index;
1885 unsigned long nr_pages = end_index - start_index + 1;
1886 unsigned long pages_processed = 0;
1887 struct page *pages[16];
1891 if (page_ops & PAGE_LOCK) {
1892 ASSERT(page_ops == PAGE_LOCK);
1893 ASSERT(processed_end && *processed_end == start);
1896 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
1897 mapping_set_error(mapping, -EIO);
1899 while (nr_pages > 0) {
1902 found_pages = find_get_pages_contig(mapping, index,
1903 min_t(unsigned long,
1904 nr_pages, ARRAY_SIZE(pages)), pages);
1905 if (found_pages == 0) {
1907 * Only if we're going to lock these pages, we can find
1908 * nothing at @index.
1910 ASSERT(page_ops & PAGE_LOCK);
1915 for (i = 0; i < found_pages; i++) {
1918 process_ret = process_one_page(fs_info, mapping,
1919 pages[i], locked_page, page_ops,
1921 if (process_ret < 0) {
1922 for (; i < found_pages; i++)
1930 nr_pages -= found_pages;
1931 index += found_pages;
1935 if (err && processed_end) {
1937 * Update @processed_end. I know this is awful since it has
1938 * two different return value patterns (inclusive vs exclusive).
1940 * But the exclusive pattern is necessary if @start is 0, or we
1941 * underflow and check against processed_end won't work as
1944 if (pages_processed)
1945 *processed_end = min(end,
1946 ((u64)(start_index + pages_processed) << PAGE_SHIFT) - 1);
1948 *processed_end = start;
1953 static noinline void __unlock_for_delalloc(struct inode *inode,
1954 struct page *locked_page,
1957 unsigned long index = start >> PAGE_SHIFT;
1958 unsigned long end_index = end >> PAGE_SHIFT;
1960 ASSERT(locked_page);
1961 if (index == locked_page->index && end_index == index)
1964 __process_pages_contig(inode->i_mapping, locked_page, start, end,
1968 static noinline int lock_delalloc_pages(struct inode *inode,
1969 struct page *locked_page,
1973 unsigned long index = delalloc_start >> PAGE_SHIFT;
1974 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
1975 u64 processed_end = delalloc_start;
1978 ASSERT(locked_page);
1979 if (index == locked_page->index && index == end_index)
1982 ret = __process_pages_contig(inode->i_mapping, locked_page, delalloc_start,
1983 delalloc_end, PAGE_LOCK, &processed_end);
1984 if (ret == -EAGAIN && processed_end > delalloc_start)
1985 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1991 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1992 * more than @max_bytes.
1994 * @start: The original start bytenr to search.
1995 * Will store the extent range start bytenr.
1996 * @end: The original end bytenr of the search range
1997 * Will store the extent range end bytenr.
1999 * Return true if we find a delalloc range which starts inside the original
2000 * range, and @start/@end will store the delalloc range start/end.
2002 * Return false if we can't find any delalloc range which starts inside the
2003 * original range, and @start/@end will be the non-delalloc range start/end.
2006 noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
2007 struct page *locked_page, u64 *start,
2010 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2011 const u64 orig_start = *start;
2012 const u64 orig_end = *end;
2013 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
2017 struct extent_state *cached_state = NULL;
2021 /* Caller should pass a valid @end to indicate the search range end */
2022 ASSERT(orig_end > orig_start);
2024 /* The range should at least cover part of the page */
2025 ASSERT(!(orig_start >= page_offset(locked_page) + PAGE_SIZE ||
2026 orig_end <= page_offset(locked_page)));
2028 /* step one, find a bunch of delalloc bytes starting at start */
2029 delalloc_start = *start;
2031 found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end,
2032 max_bytes, &cached_state);
2033 if (!found || delalloc_end <= *start || delalloc_start > orig_end) {
2034 *start = delalloc_start;
2036 /* @delalloc_end can be -1, never go beyond @orig_end */
2037 *end = min(delalloc_end, orig_end);
2038 free_extent_state(cached_state);
2043 * start comes from the offset of locked_page. We have to lock
2044 * pages in order, so we can't process delalloc bytes before
2047 if (delalloc_start < *start)
2048 delalloc_start = *start;
2051 * make sure to limit the number of pages we try to lock down
2053 if (delalloc_end + 1 - delalloc_start > max_bytes)
2054 delalloc_end = delalloc_start + max_bytes - 1;
2056 /* step two, lock all the pages after the page that has start */
2057 ret = lock_delalloc_pages(inode, locked_page,
2058 delalloc_start, delalloc_end);
2059 ASSERT(!ret || ret == -EAGAIN);
2060 if (ret == -EAGAIN) {
2061 /* some of the pages are gone, lets avoid looping by
2062 * shortening the size of the delalloc range we're searching
2064 free_extent_state(cached_state);
2065 cached_state = NULL;
2067 max_bytes = PAGE_SIZE;
2076 /* step three, lock the state bits for the whole range */
2077 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
2079 /* then test to make sure it is all still delalloc */
2080 ret = test_range_bit(tree, delalloc_start, delalloc_end,
2081 EXTENT_DELALLOC, 1, cached_state);
2083 unlock_extent_cached(tree, delalloc_start, delalloc_end,
2085 __unlock_for_delalloc(inode, locked_page,
2086 delalloc_start, delalloc_end);
2090 free_extent_state(cached_state);
2091 *start = delalloc_start;
2092 *end = delalloc_end;
2097 void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
2098 struct page *locked_page,
2099 u32 clear_bits, unsigned long page_ops)
2101 clear_extent_bit(&inode->io_tree, start, end, clear_bits, 1, 0, NULL);
2103 __process_pages_contig(inode->vfs_inode.i_mapping, locked_page,
2104 start, end, page_ops, NULL);
2108 * count the number of bytes in the tree that have a given bit(s)
2109 * set. This can be fairly slow, except for EXTENT_DIRTY which is
2110 * cached. The total number found is returned.
2112 u64 count_range_bits(struct extent_io_tree *tree,
2113 u64 *start, u64 search_end, u64 max_bytes,
2114 u32 bits, int contig)
2116 struct rb_node *node;
2117 struct extent_state *state;
2118 u64 cur_start = *start;
2119 u64 total_bytes = 0;
2123 if (WARN_ON(search_end <= cur_start))
2126 spin_lock(&tree->lock);
2127 if (cur_start == 0 && bits == EXTENT_DIRTY) {
2128 total_bytes = tree->dirty_bytes;
2132 * this search will find all the extents that end after
2135 node = tree_search(tree, cur_start);
2140 state = rb_entry(node, struct extent_state, rb_node);
2141 if (state->start > search_end)
2143 if (contig && found && state->start > last + 1)
2145 if (state->end >= cur_start && (state->state & bits) == bits) {
2146 total_bytes += min(search_end, state->end) + 1 -
2147 max(cur_start, state->start);
2148 if (total_bytes >= max_bytes)
2151 *start = max(cur_start, state->start);
2155 } else if (contig && found) {
2158 node = rb_next(node);
2163 spin_unlock(&tree->lock);
2168 * set the private field for a given byte offset in the tree. If there isn't
2169 * an extent_state there already, this does nothing.
2171 int set_state_failrec(struct extent_io_tree *tree, u64 start,
2172 struct io_failure_record *failrec)
2174 struct rb_node *node;
2175 struct extent_state *state;
2178 spin_lock(&tree->lock);
2180 * this search will find all the extents that end after
2183 node = tree_search(tree, start);
2188 state = rb_entry(node, struct extent_state, rb_node);
2189 if (state->start != start) {
2193 state->failrec = failrec;
2195 spin_unlock(&tree->lock);
2199 struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start)
2201 struct rb_node *node;
2202 struct extent_state *state;
2203 struct io_failure_record *failrec;
2205 spin_lock(&tree->lock);
2207 * this search will find all the extents that end after
2210 node = tree_search(tree, start);
2212 failrec = ERR_PTR(-ENOENT);
2215 state = rb_entry(node, struct extent_state, rb_node);
2216 if (state->start != start) {
2217 failrec = ERR_PTR(-ENOENT);
2221 failrec = state->failrec;
2223 spin_unlock(&tree->lock);
2228 * searches a range in the state tree for a given mask.
2229 * If 'filled' == 1, this returns 1 only if every extent in the tree
2230 * has the bits set. Otherwise, 1 is returned if any bit in the
2231 * range is found set.
2233 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
2234 u32 bits, int filled, struct extent_state *cached)
2236 struct extent_state *state = NULL;
2237 struct rb_node *node;
2240 spin_lock(&tree->lock);
2241 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
2242 cached->end > start)
2243 node = &cached->rb_node;
2245 node = tree_search(tree, start);
2246 while (node && start <= end) {
2247 state = rb_entry(node, struct extent_state, rb_node);
2249 if (filled && state->start > start) {
2254 if (state->start > end)
2257 if (state->state & bits) {
2261 } else if (filled) {
2266 if (state->end == (u64)-1)
2269 start = state->end + 1;
2272 node = rb_next(node);
2279 spin_unlock(&tree->lock);
2283 int free_io_failure(struct extent_io_tree *failure_tree,
2284 struct extent_io_tree *io_tree,
2285 struct io_failure_record *rec)
2290 set_state_failrec(failure_tree, rec->start, NULL);
2291 ret = clear_extent_bits(failure_tree, rec->start,
2292 rec->start + rec->len - 1,
2293 EXTENT_LOCKED | EXTENT_DIRTY);
2297 ret = clear_extent_bits(io_tree, rec->start,
2298 rec->start + rec->len - 1,
2308 * this bypasses the standard btrfs submit functions deliberately, as
2309 * the standard behavior is to write all copies in a raid setup. here we only
2310 * want to write the one bad copy. so we do the mapping for ourselves and issue
2311 * submit_bio directly.
2312 * to avoid any synchronization issues, wait for the data after writing, which
2313 * actually prevents the read that triggered the error from finishing.
2314 * currently, there can be no more than two copies of every data bit. thus,
2315 * exactly one rewrite is required.
2317 static int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2318 u64 length, u64 logical, struct page *page,
2319 unsigned int pg_offset, int mirror_num)
2321 struct btrfs_device *dev;
2322 struct bio_vec bvec;
2326 struct btrfs_io_context *bioc = NULL;
2329 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
2330 BUG_ON(!mirror_num);
2332 if (btrfs_repair_one_zone(fs_info, logical))
2335 map_length = length;
2338 * Avoid races with device replace and make sure our bioc has devices
2339 * associated to its stripes that don't go away while we are doing the
2340 * read repair operation.
2342 btrfs_bio_counter_inc_blocked(fs_info);
2343 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
2345 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2346 * to update all raid stripes, but here we just want to correct
2347 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2348 * stripe's dev and sector.
2350 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2351 &map_length, &bioc, 0);
2353 goto out_counter_dec;
2354 ASSERT(bioc->mirror_num == 1);
2356 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2357 &map_length, &bioc, mirror_num);
2359 goto out_counter_dec;
2360 BUG_ON(mirror_num != bioc->mirror_num);
2363 sector = bioc->stripes[bioc->mirror_num - 1].physical >> 9;
2364 dev = bioc->stripes[bioc->mirror_num - 1].dev;
2365 btrfs_put_bioc(bioc);
2367 if (!dev || !dev->bdev ||
2368 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
2370 goto out_counter_dec;
2373 bio_init(&bio, dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);
2374 bio.bi_iter.bi_sector = sector;
2375 __bio_add_page(&bio, page, length, pg_offset);
2377 btrfsic_check_bio(&bio);
2378 ret = submit_bio_wait(&bio);
2380 /* try to remap that extent elsewhere? */
2381 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
2382 goto out_bio_uninit;
2385 btrfs_info_rl_in_rcu(fs_info,
2386 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
2388 rcu_str_deref(dev->name), sector);
2394 btrfs_bio_counter_dec(fs_info);
2398 int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num)
2400 struct btrfs_fs_info *fs_info = eb->fs_info;
2401 u64 start = eb->start;
2402 int i, num_pages = num_extent_pages(eb);
2405 if (sb_rdonly(fs_info->sb))
2408 for (i = 0; i < num_pages; i++) {
2409 struct page *p = eb->pages[i];
2411 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
2412 start - page_offset(p), mirror_num);
2422 * each time an IO finishes, we do a fast check in the IO failure tree
2423 * to see if we need to process or clean up an io_failure_record
2425 int clean_io_failure(struct btrfs_fs_info *fs_info,
2426 struct extent_io_tree *failure_tree,
2427 struct extent_io_tree *io_tree, u64 start,
2428 struct page *page, u64 ino, unsigned int pg_offset)
2431 struct io_failure_record *failrec;
2432 struct extent_state *state;
2437 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2442 failrec = get_state_failrec(failure_tree, start);
2443 if (IS_ERR(failrec))
2446 BUG_ON(!failrec->this_mirror);
2448 if (sb_rdonly(fs_info->sb))
2451 spin_lock(&io_tree->lock);
2452 state = find_first_extent_bit_state(io_tree,
2455 spin_unlock(&io_tree->lock);
2457 if (state && state->start <= failrec->start &&
2458 state->end >= failrec->start + failrec->len - 1) {
2459 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2461 if (num_copies > 1) {
2462 repair_io_failure(fs_info, ino, start, failrec->len,
2463 failrec->logical, page, pg_offset,
2464 failrec->failed_mirror);
2469 free_io_failure(failure_tree, io_tree, failrec);
2475 * Can be called when
2476 * - hold extent lock
2477 * - under ordered extent
2478 * - the inode is freeing
2480 void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
2482 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
2483 struct io_failure_record *failrec;
2484 struct extent_state *state, *next;
2486 if (RB_EMPTY_ROOT(&failure_tree->state))
2489 spin_lock(&failure_tree->lock);
2490 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2492 if (state->start > end)
2495 ASSERT(state->end <= end);
2497 next = next_state(state);
2499 failrec = state->failrec;
2500 free_extent_state(state);
2505 spin_unlock(&failure_tree->lock);
2508 static struct io_failure_record *btrfs_get_io_failure_record(struct inode *inode,
2511 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2512 struct io_failure_record *failrec;
2513 struct extent_map *em;
2514 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2515 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2516 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2517 const u32 sectorsize = fs_info->sectorsize;
2521 failrec = get_state_failrec(failure_tree, start);
2522 if (!IS_ERR(failrec)) {
2523 btrfs_debug(fs_info,
2524 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu",
2525 failrec->logical, failrec->start, failrec->len);
2527 * when data can be on disk more than twice, add to failrec here
2528 * (e.g. with a list for failed_mirror) to make
2529 * clean_io_failure() clean all those errors at once.
2535 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2537 return ERR_PTR(-ENOMEM);
2539 failrec->start = start;
2540 failrec->len = sectorsize;
2541 failrec->this_mirror = 0;
2542 failrec->compress_type = BTRFS_COMPRESS_NONE;
2544 read_lock(&em_tree->lock);
2545 em = lookup_extent_mapping(em_tree, start, failrec->len);
2547 read_unlock(&em_tree->lock);
2549 return ERR_PTR(-EIO);
2552 if (em->start > start || em->start + em->len <= start) {
2553 free_extent_map(em);
2556 read_unlock(&em_tree->lock);
2559 return ERR_PTR(-EIO);
2562 logical = start - em->start;
2563 logical = em->block_start + logical;
2564 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2565 logical = em->block_start;
2566 failrec->compress_type = em->compress_type;
2569 btrfs_debug(fs_info,
2570 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2571 logical, start, failrec->len);
2573 failrec->logical = logical;
2574 free_extent_map(em);
2576 /* Set the bits in the private failure tree */
2577 ret = set_extent_bits(failure_tree, start, start + sectorsize - 1,
2578 EXTENT_LOCKED | EXTENT_DIRTY);
2580 ret = set_state_failrec(failure_tree, start, failrec);
2581 /* Set the bits in the inode's tree */
2582 ret = set_extent_bits(tree, start, start + sectorsize - 1,
2584 } else if (ret < 0) {
2586 return ERR_PTR(ret);
2592 static bool btrfs_check_repairable(struct inode *inode,
2593 struct io_failure_record *failrec,
2596 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2599 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
2600 if (num_copies == 1) {
2602 * we only have a single copy of the data, so don't bother with
2603 * all the retry and error correction code that follows. no
2604 * matter what the error is, it is very likely to persist.
2606 btrfs_debug(fs_info,
2607 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2608 num_copies, failrec->this_mirror, failed_mirror);
2612 /* The failure record should only contain one sector */
2613 ASSERT(failrec->len == fs_info->sectorsize);
2616 * There are two premises:
2617 * a) deliver good data to the caller
2618 * b) correct the bad sectors on disk
2620 * Since we're only doing repair for one sector, we only need to get
2621 * a good copy of the failed sector and if we succeed, we have setup
2622 * everything for repair_io_failure to do the rest for us.
2624 ASSERT(failed_mirror);
2625 failrec->failed_mirror = failed_mirror;
2626 failrec->this_mirror++;
2627 if (failrec->this_mirror == failed_mirror)
2628 failrec->this_mirror++;
2630 if (failrec->this_mirror > num_copies) {
2631 btrfs_debug(fs_info,
2632 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2633 num_copies, failrec->this_mirror, failed_mirror);
2640 int btrfs_repair_one_sector(struct inode *inode,
2641 struct bio *failed_bio, u32 bio_offset,
2642 struct page *page, unsigned int pgoff,
2643 u64 start, int failed_mirror,
2644 submit_bio_hook_t *submit_bio_hook)
2646 struct io_failure_record *failrec;
2647 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2648 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2649 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2650 struct btrfs_bio *failed_bbio = btrfs_bio(failed_bio);
2651 const int icsum = bio_offset >> fs_info->sectorsize_bits;
2652 struct bio *repair_bio;
2653 struct btrfs_bio *repair_bbio;
2655 btrfs_debug(fs_info,
2656 "repair read error: read error at %llu", start);
2658 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
2660 failrec = btrfs_get_io_failure_record(inode, start);
2661 if (IS_ERR(failrec))
2662 return PTR_ERR(failrec);
2665 if (!btrfs_check_repairable(inode, failrec, failed_mirror)) {
2666 free_io_failure(failure_tree, tree, failrec);
2670 repair_bio = btrfs_bio_alloc(1);
2671 repair_bbio = btrfs_bio(repair_bio);
2672 repair_bbio->file_offset = start;
2673 repair_bio->bi_opf = REQ_OP_READ;
2674 repair_bio->bi_end_io = failed_bio->bi_end_io;
2675 repair_bio->bi_iter.bi_sector = failrec->logical >> 9;
2676 repair_bio->bi_private = failed_bio->bi_private;
2678 if (failed_bbio->csum) {
2679 const u32 csum_size = fs_info->csum_size;
2681 repair_bbio->csum = repair_bbio->csum_inline;
2682 memcpy(repair_bbio->csum,
2683 failed_bbio->csum + csum_size * icsum, csum_size);
2686 bio_add_page(repair_bio, page, failrec->len, pgoff);
2687 repair_bbio->iter = repair_bio->bi_iter;
2689 btrfs_debug(btrfs_sb(inode->i_sb),
2690 "repair read error: submitting new read to mirror %d",
2691 failrec->this_mirror);
2694 * At this point we have a bio, so any errors from submit_bio_hook()
2695 * will be handled by the endio on the repair_bio, so we can't return an
2698 submit_bio_hook(inode, repair_bio, failrec->this_mirror, failrec->compress_type);
2702 static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len)
2704 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
2706 ASSERT(page_offset(page) <= start &&
2707 start + len <= page_offset(page) + PAGE_SIZE);
2710 if (fsverity_active(page->mapping->host) &&
2712 !PageUptodate(page) &&
2713 start < i_size_read(page->mapping->host) &&
2714 !fsverity_verify_page(page)) {
2715 btrfs_page_set_error(fs_info, page, start, len);
2717 btrfs_page_set_uptodate(fs_info, page, start, len);
2720 btrfs_page_clear_uptodate(fs_info, page, start, len);
2721 btrfs_page_set_error(fs_info, page, start, len);
2724 if (!btrfs_is_subpage(fs_info, page))
2727 btrfs_subpage_end_reader(fs_info, page, start, len);
2730 static blk_status_t submit_data_read_repair(struct inode *inode,
2731 struct bio *failed_bio,
2732 u32 bio_offset, struct page *page,
2736 unsigned int error_bitmap)
2738 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2739 const u32 sectorsize = fs_info->sectorsize;
2740 const int nr_bits = (end + 1 - start) >> fs_info->sectorsize_bits;
2744 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
2746 /* This repair is only for data */
2747 ASSERT(is_data_inode(inode));
2749 /* We're here because we had some read errors or csum mismatch */
2750 ASSERT(error_bitmap);
2753 * We only get called on buffered IO, thus page must be mapped and bio
2754 * must not be cloned.
2756 ASSERT(page->mapping && !bio_flagged(failed_bio, BIO_CLONED));
2758 /* Iterate through all the sectors in the range */
2759 for (i = 0; i < nr_bits; i++) {
2760 const unsigned int offset = i * sectorsize;
2761 struct extent_state *cached = NULL;
2762 bool uptodate = false;
2765 if (!(error_bitmap & (1U << i))) {
2767 * This sector has no error, just end the page read
2768 * and unlock the range.
2774 ret = btrfs_repair_one_sector(inode, failed_bio,
2775 bio_offset + offset,
2776 page, pgoff + offset, start + offset,
2777 failed_mirror, btrfs_submit_data_bio);
2780 * We have submitted the read repair, the page release
2781 * will be handled by the endio function of the
2782 * submitted repair bio.
2783 * Thus we don't need to do any thing here.
2788 * Repair failed, just record the error but still continue.
2789 * Or the remaining sectors will not be properly unlocked.
2794 end_page_read(page, uptodate, start + offset, sectorsize);
2796 set_extent_uptodate(&BTRFS_I(inode)->io_tree,
2798 start + offset + sectorsize - 1,
2799 &cached, GFP_ATOMIC);
2800 unlock_extent_cached_atomic(&BTRFS_I(inode)->io_tree,
2802 start + offset + sectorsize - 1,
2805 return errno_to_blk_status(error);
2808 /* lots and lots of room for performance fixes in the end_bio funcs */
2810 void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2812 struct btrfs_inode *inode;
2813 const bool uptodate = (err == 0);
2816 ASSERT(page && page->mapping);
2817 inode = BTRFS_I(page->mapping->host);
2818 btrfs_writepage_endio_finish_ordered(inode, page, start, end, uptodate);
2821 const struct btrfs_fs_info *fs_info = inode->root->fs_info;
2824 ASSERT(end + 1 - start <= U32_MAX);
2825 len = end + 1 - start;
2827 btrfs_page_clear_uptodate(fs_info, page, start, len);
2828 btrfs_page_set_error(fs_info, page, start, len);
2829 ret = err < 0 ? err : -EIO;
2830 mapping_set_error(page->mapping, ret);
2835 * after a writepage IO is done, we need to:
2836 * clear the uptodate bits on error
2837 * clear the writeback bits in the extent tree for this IO
2838 * end_page_writeback if the page has no more pending IO
2840 * Scheduling is not allowed, so the extent state tree is expected
2841 * to have one and only one object corresponding to this IO.
2843 static void end_bio_extent_writepage(struct bio *bio)
2845 int error = blk_status_to_errno(bio->bi_status);
2846 struct bio_vec *bvec;
2849 struct bvec_iter_all iter_all;
2850 bool first_bvec = true;
2852 ASSERT(!bio_flagged(bio, BIO_CLONED));
2853 bio_for_each_segment_all(bvec, bio, iter_all) {
2854 struct page *page = bvec->bv_page;
2855 struct inode *inode = page->mapping->host;
2856 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2857 const u32 sectorsize = fs_info->sectorsize;
2859 /* Our read/write should always be sector aligned. */
2860 if (!IS_ALIGNED(bvec->bv_offset, sectorsize))
2862 "partial page write in btrfs with offset %u and length %u",
2863 bvec->bv_offset, bvec->bv_len);
2864 else if (!IS_ALIGNED(bvec->bv_len, sectorsize))
2866 "incomplete page write with offset %u and length %u",
2867 bvec->bv_offset, bvec->bv_len);
2869 start = page_offset(page) + bvec->bv_offset;
2870 end = start + bvec->bv_len - 1;
2873 btrfs_record_physical_zoned(inode, start, bio);
2877 end_extent_writepage(page, error, start, end);
2879 btrfs_page_clear_writeback(fs_info, page, start, bvec->bv_len);
2886 * Record previously processed extent range
2888 * For endio_readpage_release_extent() to handle a full extent range, reducing
2889 * the extent io operations.
2891 struct processed_extent {
2892 struct btrfs_inode *inode;
2893 /* Start of the range in @inode */
2895 /* End of the range in @inode */
2901 * Try to release processed extent range
2903 * May not release the extent range right now if the current range is
2904 * contiguous to processed extent.
2906 * Will release processed extent when any of @inode, @uptodate, the range is
2907 * no longer contiguous to the processed range.
2909 * Passing @inode == NULL will force processed extent to be released.
2911 static void endio_readpage_release_extent(struct processed_extent *processed,
2912 struct btrfs_inode *inode, u64 start, u64 end,
2915 struct extent_state *cached = NULL;
2916 struct extent_io_tree *tree;
2918 /* The first extent, initialize @processed */
2919 if (!processed->inode)
2923 * Contiguous to processed extent, just uptodate the end.
2925 * Several things to notice:
2927 * - bio can be merged as long as on-disk bytenr is contiguous
2928 * This means we can have page belonging to other inodes, thus need to
2929 * check if the inode still matches.
2930 * - bvec can contain range beyond current page for multi-page bvec
2931 * Thus we need to do processed->end + 1 >= start check
2933 if (processed->inode == inode && processed->uptodate == uptodate &&
2934 processed->end + 1 >= start && end >= processed->end) {
2935 processed->end = end;
2939 tree = &processed->inode->io_tree;
2941 * Now we don't have range contiguous to the processed range, release
2942 * the processed range now.
2944 if (processed->uptodate && tree->track_uptodate)
2945 set_extent_uptodate(tree, processed->start, processed->end,
2946 &cached, GFP_ATOMIC);
2947 unlock_extent_cached_atomic(tree, processed->start, processed->end,
2951 /* Update processed to current range */
2952 processed->inode = inode;
2953 processed->start = start;
2954 processed->end = end;
2955 processed->uptodate = uptodate;
2958 static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page)
2960 ASSERT(PageLocked(page));
2961 if (!btrfs_is_subpage(fs_info, page))
2964 ASSERT(PagePrivate(page));
2965 btrfs_subpage_start_reader(fs_info, page, page_offset(page), PAGE_SIZE);
2969 * Find extent buffer for a given bytenr.
2971 * This is for end_bio_extent_readpage(), thus we can't do any unsafe locking
2974 static struct extent_buffer *find_extent_buffer_readpage(
2975 struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
2977 struct extent_buffer *eb;
2980 * For regular sectorsize, we can use page->private to grab extent
2983 if (fs_info->nodesize >= PAGE_SIZE) {
2984 ASSERT(PagePrivate(page) && page->private);
2985 return (struct extent_buffer *)page->private;
2988 /* For subpage case, we need to lookup extent buffer xarray */
2989 eb = xa_load(&fs_info->extent_buffers,
2990 bytenr >> fs_info->sectorsize_bits);
2996 * after a readpage IO is done, we need to:
2997 * clear the uptodate bits on error
2998 * set the uptodate bits if things worked
2999 * set the page up to date if all extents in the tree are uptodate
3000 * clear the lock bit in the extent tree
3001 * unlock the page if there are no other extents locked for it
3003 * Scheduling is not allowed, so the extent state tree is expected
3004 * to have one and only one object corresponding to this IO.
3006 static void end_bio_extent_readpage(struct bio *bio)
3008 struct bio_vec *bvec;
3009 struct btrfs_bio *bbio = btrfs_bio(bio);
3010 struct extent_io_tree *tree, *failure_tree;
3011 struct processed_extent processed = { 0 };
3013 * The offset to the beginning of a bio, since one bio can never be
3014 * larger than UINT_MAX, u32 here is enough.
3019 struct bvec_iter_all iter_all;
3021 ASSERT(!bio_flagged(bio, BIO_CLONED));
3022 bio_for_each_segment_all(bvec, bio, iter_all) {
3023 bool uptodate = !bio->bi_status;
3024 struct page *page = bvec->bv_page;
3025 struct inode *inode = page->mapping->host;
3026 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3027 const u32 sectorsize = fs_info->sectorsize;
3028 unsigned int error_bitmap = (unsigned int)-1;
3033 btrfs_debug(fs_info,
3034 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
3035 bio->bi_iter.bi_sector, bio->bi_status,
3037 tree = &BTRFS_I(inode)->io_tree;
3038 failure_tree = &BTRFS_I(inode)->io_failure_tree;
3041 * We always issue full-sector reads, but if some block in a
3042 * page fails to read, blk_update_request() will advance
3043 * bv_offset and adjust bv_len to compensate. Print a warning
3044 * for unaligned offsets, and an error if they don't add up to
3047 if (!IS_ALIGNED(bvec->bv_offset, sectorsize))
3049 "partial page read in btrfs with offset %u and length %u",
3050 bvec->bv_offset, bvec->bv_len);
3051 else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len,
3054 "incomplete page read with offset %u and length %u",
3055 bvec->bv_offset, bvec->bv_len);
3057 start = page_offset(page) + bvec->bv_offset;
3058 end = start + bvec->bv_len - 1;
3061 mirror = bbio->mirror_num;
3062 if (likely(uptodate)) {
3063 if (is_data_inode(inode)) {
3064 error_bitmap = btrfs_verify_data_csum(bbio,
3065 bio_offset, page, start, end);
3068 ret = btrfs_validate_metadata_buffer(bbio,
3069 page, start, end, mirror);
3074 clean_io_failure(BTRFS_I(inode)->root->fs_info,
3075 failure_tree, tree, start,
3077 btrfs_ino(BTRFS_I(inode)), 0);
3080 if (likely(uptodate))
3083 if (is_data_inode(inode)) {
3085 * If we failed to submit the IO at all we'll have a
3086 * mirror_num == 0, in which case we need to just mark
3087 * the page with an error and unlock it and carry on.
3093 * submit_data_read_repair() will handle all the good
3094 * and bad sectors, we just continue to the next bvec.
3096 submit_data_read_repair(inode, bio, bio_offset, page,
3097 start - page_offset(page),
3101 ASSERT(bio_offset + len > bio_offset);
3105 struct extent_buffer *eb;
3107 eb = find_extent_buffer_readpage(fs_info, page, start);
3108 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
3109 eb->read_mirror = mirror;
3110 atomic_dec(&eb->io_pages);
3113 if (likely(uptodate)) {
3114 loff_t i_size = i_size_read(inode);
3115 pgoff_t end_index = i_size >> PAGE_SHIFT;
3118 * Zero out the remaining part if this range straddles
3121 * Here we should only zero the range inside the bvec,
3122 * not touch anything else.
3124 * NOTE: i_size is exclusive while end is inclusive.
3126 if (page->index == end_index && i_size <= end) {
3127 u32 zero_start = max(offset_in_page(i_size),
3128 offset_in_page(start));
3130 zero_user_segment(page, zero_start,
3131 offset_in_page(end) + 1);
3134 ASSERT(bio_offset + len > bio_offset);
3137 /* Update page status and unlock */
3138 end_page_read(page, uptodate, start, len);
3139 endio_readpage_release_extent(&processed, BTRFS_I(inode),
3140 start, end, PageUptodate(page));
3142 /* Release the last extent */
3143 endio_readpage_release_extent(&processed, NULL, 0, 0, false);
3144 btrfs_bio_free_csum(bbio);
3149 * Populate every free slot in a provided array with pages.
3151 * @nr_pages: number of pages to allocate
3152 * @page_array: the array to fill with pages; any existing non-null entries in
3153 * the array will be skipped
3155 * Return: 0 if all pages were able to be allocated;
3156 * -ENOMEM otherwise, and the caller is responsible for freeing all
3157 * non-null page pointers in the array.
3159 int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array)
3161 unsigned int allocated;
3163 for (allocated = 0; allocated < nr_pages;) {
3164 unsigned int last = allocated;
3166 allocated = alloc_pages_bulk_array(GFP_NOFS, nr_pages, page_array);
3168 if (allocated == nr_pages)
3172 * During this iteration, no page could be allocated, even
3173 * though alloc_pages_bulk_array() falls back to alloc_page()
3174 * if it could not bulk-allocate. So we must be out of memory.
3176 if (allocated == last)
3179 memalloc_retry_wait(GFP_NOFS);
3185 * Initialize the members up to but not including 'bio'. Use after allocating a
3186 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
3187 * 'bio' because use of __GFP_ZERO is not supported.
3189 static inline void btrfs_bio_init(struct btrfs_bio *bbio)
3191 memset(bbio, 0, offsetof(struct btrfs_bio, bio));
3195 * Allocate a btrfs_io_bio, with @nr_iovecs as maximum number of iovecs.
3197 * The bio allocation is backed by bioset and does not fail.
3199 struct bio *btrfs_bio_alloc(unsigned int nr_iovecs)
3203 ASSERT(0 < nr_iovecs && nr_iovecs <= BIO_MAX_VECS);
3204 bio = bio_alloc_bioset(NULL, nr_iovecs, 0, GFP_NOFS, &btrfs_bioset);
3205 btrfs_bio_init(btrfs_bio(bio));
3209 struct bio *btrfs_bio_clone(struct block_device *bdev, struct bio *bio)
3211 struct btrfs_bio *bbio;
3214 /* Bio allocation backed by a bioset does not fail */
3215 new = bio_alloc_clone(bdev, bio, GFP_NOFS, &btrfs_bioset);
3216 bbio = btrfs_bio(new);
3217 btrfs_bio_init(bbio);
3218 bbio->iter = bio->bi_iter;
3222 struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size)
3225 struct btrfs_bio *bbio;
3227 ASSERT(offset <= UINT_MAX && size <= UINT_MAX);
3229 /* this will never fail when it's backed by a bioset */
3230 bio = bio_alloc_clone(orig->bi_bdev, orig, GFP_NOFS, &btrfs_bioset);
3233 bbio = btrfs_bio(bio);
3234 btrfs_bio_init(bbio);
3236 bio_trim(bio, offset >> 9, size >> 9);
3237 bbio->iter = bio->bi_iter;
3242 * Attempt to add a page to bio
3244 * @bio_ctrl: record both the bio, and its bio_flags
3245 * @page: page to add to the bio
3246 * @disk_bytenr: offset of the new bio or to check whether we are adding
3247 * a contiguous page to the previous one
3248 * @size: portion of page that we want to write
3249 * @pg_offset: starting offset in the page
3250 * @compress_type: compression type of the current bio to see if we can merge them
3252 * Attempt to add a page to bio considering stripe alignment etc.
3254 * Return >= 0 for the number of bytes added to the bio.
3255 * Can return 0 if the current bio is already at stripe/zone boundary.
3256 * Return <0 for error.
3258 static int btrfs_bio_add_page(struct btrfs_bio_ctrl *bio_ctrl,
3260 u64 disk_bytenr, unsigned int size,
3261 unsigned int pg_offset,
3262 enum btrfs_compression_type compress_type)
3264 struct bio *bio = bio_ctrl->bio;
3265 u32 bio_size = bio->bi_iter.bi_size;
3267 const sector_t sector = disk_bytenr >> SECTOR_SHIFT;
3272 /* The limit should be calculated when bio_ctrl->bio is allocated */
3273 ASSERT(bio_ctrl->len_to_oe_boundary && bio_ctrl->len_to_stripe_boundary);
3274 if (bio_ctrl->compress_type != compress_type)
3277 if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE)
3278 contig = bio->bi_iter.bi_sector == sector;
3280 contig = bio_end_sector(bio) == sector;
3284 real_size = min(bio_ctrl->len_to_oe_boundary,
3285 bio_ctrl->len_to_stripe_boundary) - bio_size;
3286 real_size = min(real_size, size);
3289 * If real_size is 0, never call bio_add_*_page(), as even size is 0,
3290 * bio will still execute its endio function on the page!
3295 if (bio_op(bio) == REQ_OP_ZONE_APPEND)
3296 ret = bio_add_zone_append_page(bio, page, real_size, pg_offset);
3298 ret = bio_add_page(bio, page, real_size, pg_offset);
3303 static int calc_bio_boundaries(struct btrfs_bio_ctrl *bio_ctrl,
3304 struct btrfs_inode *inode, u64 file_offset)
3306 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3307 struct btrfs_io_geometry geom;
3308 struct btrfs_ordered_extent *ordered;
3309 struct extent_map *em;
3310 u64 logical = (bio_ctrl->bio->bi_iter.bi_sector << SECTOR_SHIFT);
3314 * Pages for compressed extent are never submitted to disk directly,
3315 * thus it has no real boundary, just set them to U32_MAX.
3317 * The split happens for real compressed bio, which happens in
3318 * btrfs_submit_compressed_read/write().
3320 if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) {
3321 bio_ctrl->len_to_oe_boundary = U32_MAX;
3322 bio_ctrl->len_to_stripe_boundary = U32_MAX;
3325 em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
3328 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio_ctrl->bio),
3330 free_extent_map(em);
3334 if (geom.len > U32_MAX)
3335 bio_ctrl->len_to_stripe_boundary = U32_MAX;
3337 bio_ctrl->len_to_stripe_boundary = (u32)geom.len;
3339 if (bio_op(bio_ctrl->bio) != REQ_OP_ZONE_APPEND) {
3340 bio_ctrl->len_to_oe_boundary = U32_MAX;
3344 /* Ordered extent not yet created, so we're good */
3345 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
3347 bio_ctrl->len_to_oe_boundary = U32_MAX;
3351 bio_ctrl->len_to_oe_boundary = min_t(u32, U32_MAX,
3352 ordered->disk_bytenr + ordered->disk_num_bytes - logical);
3353 btrfs_put_ordered_extent(ordered);
3357 static int alloc_new_bio(struct btrfs_inode *inode,
3358 struct btrfs_bio_ctrl *bio_ctrl,
3359 struct writeback_control *wbc,
3361 bio_end_io_t end_io_func,
3362 u64 disk_bytenr, u32 offset, u64 file_offset,
3363 enum btrfs_compression_type compress_type)
3365 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3369 bio = btrfs_bio_alloc(BIO_MAX_VECS);
3371 * For compressed page range, its disk_bytenr is always @disk_bytenr
3372 * passed in, no matter if we have added any range into previous bio.
3374 if (compress_type != BTRFS_COMPRESS_NONE)
3375 bio->bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
3377 bio->bi_iter.bi_sector = (disk_bytenr + offset) >> SECTOR_SHIFT;
3378 bio_ctrl->bio = bio;
3379 bio_ctrl->compress_type = compress_type;
3380 bio->bi_end_io = end_io_func;
3381 bio->bi_private = &inode->io_tree;
3383 ret = calc_bio_boundaries(bio_ctrl, inode, file_offset);
3389 * For Zone append we need the correct block_device that we are
3390 * going to write to set in the bio to be able to respect the
3391 * hardware limitation. Look it up here:
3393 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
3394 struct btrfs_device *dev;
3396 dev = btrfs_zoned_get_device(fs_info, disk_bytenr,
3397 fs_info->sectorsize);
3403 bio_set_dev(bio, dev->bdev);
3406 * Otherwise pick the last added device to support
3407 * cgroup writeback. For multi-device file systems this
3408 * means blk-cgroup policies have to always be set on the
3409 * last added/replaced device. This is a bit odd but has
3410 * been like that for a long time.
3412 bio_set_dev(bio, fs_info->fs_devices->latest_dev->bdev);
3414 wbc_init_bio(wbc, bio);
3416 ASSERT(bio_op(bio) != REQ_OP_ZONE_APPEND);
3420 bio_ctrl->bio = NULL;
3421 bio->bi_status = errno_to_blk_status(ret);
3427 * @opf: bio REQ_OP_* and REQ_* flags as one value
3428 * @wbc: optional writeback control for io accounting
3429 * @page: page to add to the bio
3430 * @disk_bytenr: logical bytenr where the write will be
3431 * @size: portion of page that we want to write to
3432 * @pg_offset: offset of the new bio or to check whether we are adding
3433 * a contiguous page to the previous one
3434 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
3435 * @end_io_func: end_io callback for new bio
3436 * @mirror_num: desired mirror to read/write
3437 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
3438 * @compress_type: compress type for current bio
3440 static int submit_extent_page(unsigned int opf,
3441 struct writeback_control *wbc,
3442 struct btrfs_bio_ctrl *bio_ctrl,
3443 struct page *page, u64 disk_bytenr,
3444 size_t size, unsigned long pg_offset,
3445 bio_end_io_t end_io_func,
3447 enum btrfs_compression_type compress_type,
3448 bool force_bio_submit)
3451 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
3452 unsigned int cur = pg_offset;
3456 ASSERT(pg_offset < PAGE_SIZE && size <= PAGE_SIZE &&
3457 pg_offset + size <= PAGE_SIZE);
3458 if (force_bio_submit && bio_ctrl->bio) {
3459 submit_one_bio(bio_ctrl->bio, mirror_num, bio_ctrl->compress_type);
3460 bio_ctrl->bio = NULL;
3463 while (cur < pg_offset + size) {
3464 u32 offset = cur - pg_offset;
3467 /* Allocate new bio if needed */
3468 if (!bio_ctrl->bio) {
3469 ret = alloc_new_bio(inode, bio_ctrl, wbc, opf,
3470 end_io_func, disk_bytenr, offset,
3471 page_offset(page) + cur,
3477 * We must go through btrfs_bio_add_page() to ensure each
3478 * page range won't cross various boundaries.
3480 if (compress_type != BTRFS_COMPRESS_NONE)
3481 added = btrfs_bio_add_page(bio_ctrl, page, disk_bytenr,
3482 size - offset, pg_offset + offset,
3485 added = btrfs_bio_add_page(bio_ctrl, page,
3486 disk_bytenr + offset, size - offset,
3487 pg_offset + offset, compress_type);
3489 /* Metadata page range should never be split */
3490 if (!is_data_inode(&inode->vfs_inode))
3491 ASSERT(added == 0 || added == size - offset);
3493 /* At least we added some page, update the account */
3495 wbc_account_cgroup_owner(wbc, page, added);
3497 /* We have reached boundary, submit right now */
3498 if (added < size - offset) {
3499 /* The bio should contain some page(s) */
3500 ASSERT(bio_ctrl->bio->bi_iter.bi_size);
3501 submit_one_bio(bio_ctrl->bio, mirror_num, bio_ctrl->compress_type);
3502 bio_ctrl->bio = NULL;
3509 static int attach_extent_buffer_page(struct extent_buffer *eb,
3511 struct btrfs_subpage *prealloc)
3513 struct btrfs_fs_info *fs_info = eb->fs_info;
3517 * If the page is mapped to btree inode, we should hold the private
3518 * lock to prevent race.
3519 * For cloned or dummy extent buffers, their pages are not mapped and
3520 * will not race with any other ebs.
3523 lockdep_assert_held(&page->mapping->private_lock);
3525 if (fs_info->nodesize >= PAGE_SIZE) {
3526 if (!PagePrivate(page))
3527 attach_page_private(page, eb);
3529 WARN_ON(page->private != (unsigned long)eb);
3533 /* Already mapped, just free prealloc */
3534 if (PagePrivate(page)) {
3535 btrfs_free_subpage(prealloc);
3540 /* Has preallocated memory for subpage */
3541 attach_page_private(page, prealloc);
3543 /* Do new allocation to attach subpage */
3544 ret = btrfs_attach_subpage(fs_info, page,
3545 BTRFS_SUBPAGE_METADATA);
3549 int set_page_extent_mapped(struct page *page)
3551 struct btrfs_fs_info *fs_info;
3553 ASSERT(page->mapping);
3555 if (PagePrivate(page))
3558 fs_info = btrfs_sb(page->mapping->host->i_sb);
3560 if (btrfs_is_subpage(fs_info, page))
3561 return btrfs_attach_subpage(fs_info, page, BTRFS_SUBPAGE_DATA);
3563 attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE);
3567 void clear_page_extent_mapped(struct page *page)
3569 struct btrfs_fs_info *fs_info;
3571 ASSERT(page->mapping);
3573 if (!PagePrivate(page))
3576 fs_info = btrfs_sb(page->mapping->host->i_sb);
3577 if (btrfs_is_subpage(fs_info, page))
3578 return btrfs_detach_subpage(fs_info, page);
3580 detach_page_private(page);
3583 static struct extent_map *
3584 __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
3585 u64 start, u64 len, struct extent_map **em_cached)
3587 struct extent_map *em;
3589 if (em_cached && *em_cached) {
3591 if (extent_map_in_tree(em) && start >= em->start &&
3592 start < extent_map_end(em)) {
3593 refcount_inc(&em->refs);
3597 free_extent_map(em);
3601 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, start, len);
3602 if (em_cached && !IS_ERR(em)) {
3604 refcount_inc(&em->refs);
3610 * basic readpage implementation. Locked extent state structs are inserted
3611 * into the tree that are removed when the IO is done (by the end_io
3613 * XXX JDM: This needs looking at to ensure proper page locking
3614 * return 0 on success, otherwise return error
3616 static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
3617 struct btrfs_bio_ctrl *bio_ctrl,
3618 unsigned int read_flags, u64 *prev_em_start)
3620 struct inode *inode = page->mapping->host;
3621 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3622 u64 start = page_offset(page);
3623 const u64 end = start + PAGE_SIZE - 1;
3626 u64 last_byte = i_size_read(inode);
3629 struct extent_map *em;
3631 size_t pg_offset = 0;
3633 size_t blocksize = inode->i_sb->s_blocksize;
3634 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
3636 ret = set_page_extent_mapped(page);
3638 unlock_extent(tree, start, end);
3639 btrfs_page_set_error(fs_info, page, start, PAGE_SIZE);
3644 if (page->index == last_byte >> PAGE_SHIFT) {
3645 size_t zero_offset = offset_in_page(last_byte);
3648 iosize = PAGE_SIZE - zero_offset;
3649 memzero_page(page, zero_offset, iosize);
3650 flush_dcache_page(page);
3653 begin_page_read(fs_info, page);
3654 while (cur <= end) {
3655 unsigned long this_bio_flag = 0;
3656 bool force_bio_submit = false;
3659 ASSERT(IS_ALIGNED(cur, fs_info->sectorsize));
3660 if (cur >= last_byte) {
3661 struct extent_state *cached = NULL;
3663 iosize = PAGE_SIZE - pg_offset;
3664 memzero_page(page, pg_offset, iosize);
3665 flush_dcache_page(page);
3666 set_extent_uptodate(tree, cur, cur + iosize - 1,
3668 unlock_extent_cached(tree, cur,
3669 cur + iosize - 1, &cached);
3670 end_page_read(page, true, cur, iosize);
3673 em = __get_extent_map(inode, page, pg_offset, cur,
3674 end - cur + 1, em_cached);
3676 unlock_extent(tree, cur, end);
3677 end_page_read(page, false, cur, end + 1 - cur);
3681 extent_offset = cur - em->start;
3682 BUG_ON(extent_map_end(em) <= cur);
3685 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3686 this_bio_flag = em->compress_type;
3688 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3689 cur_end = min(extent_map_end(em) - 1, end);
3690 iosize = ALIGN(iosize, blocksize);
3691 if (this_bio_flag != BTRFS_COMPRESS_NONE)
3692 disk_bytenr = em->block_start;
3694 disk_bytenr = em->block_start + extent_offset;
3695 block_start = em->block_start;
3696 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3697 block_start = EXTENT_MAP_HOLE;
3700 * If we have a file range that points to a compressed extent
3701 * and it's followed by a consecutive file range that points
3702 * to the same compressed extent (possibly with a different
3703 * offset and/or length, so it either points to the whole extent
3704 * or only part of it), we must make sure we do not submit a
3705 * single bio to populate the pages for the 2 ranges because
3706 * this makes the compressed extent read zero out the pages
3707 * belonging to the 2nd range. Imagine the following scenario:
3710 * [0 - 8K] [8K - 24K]
3713 * points to extent X, points to extent X,
3714 * offset 4K, length of 8K offset 0, length 16K
3716 * [extent X, compressed length = 4K uncompressed length = 16K]
3718 * If the bio to read the compressed extent covers both ranges,
3719 * it will decompress extent X into the pages belonging to the
3720 * first range and then it will stop, zeroing out the remaining
3721 * pages that belong to the other range that points to extent X.
3722 * So here we make sure we submit 2 bios, one for the first
3723 * range and another one for the third range. Both will target
3724 * the same physical extent from disk, but we can't currently
3725 * make the compressed bio endio callback populate the pages
3726 * for both ranges because each compressed bio is tightly
3727 * coupled with a single extent map, and each range can have
3728 * an extent map with a different offset value relative to the
3729 * uncompressed data of our extent and different lengths. This
3730 * is a corner case so we prioritize correctness over
3731 * non-optimal behavior (submitting 2 bios for the same extent).
3733 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3734 prev_em_start && *prev_em_start != (u64)-1 &&
3735 *prev_em_start != em->start)
3736 force_bio_submit = true;
3739 *prev_em_start = em->start;
3741 free_extent_map(em);
3744 /* we've found a hole, just zero and go on */
3745 if (block_start == EXTENT_MAP_HOLE) {
3746 struct extent_state *cached = NULL;
3748 memzero_page(page, pg_offset, iosize);
3749 flush_dcache_page(page);
3751 set_extent_uptodate(tree, cur, cur + iosize - 1,
3753 unlock_extent_cached(tree, cur,
3754 cur + iosize - 1, &cached);
3755 end_page_read(page, true, cur, iosize);
3757 pg_offset += iosize;
3760 /* the get_extent function already copied into the page */
3761 if (test_range_bit(tree, cur, cur_end,
3762 EXTENT_UPTODATE, 1, NULL)) {
3763 unlock_extent(tree, cur, cur + iosize - 1);
3764 end_page_read(page, true, cur, iosize);
3766 pg_offset += iosize;
3769 /* we have an inline extent but it didn't get marked up
3770 * to date. Error out
3772 if (block_start == EXTENT_MAP_INLINE) {
3773 unlock_extent(tree, cur, cur + iosize - 1);
3774 end_page_read(page, false, cur, iosize);
3776 pg_offset += iosize;
3780 ret = submit_extent_page(REQ_OP_READ | read_flags, NULL,
3781 bio_ctrl, page, disk_bytenr, iosize,
3783 end_bio_extent_readpage, 0,
3788 * We have to unlock the remaining range, or the page
3789 * will never be unlocked.
3791 unlock_extent(tree, cur, end);
3792 end_page_read(page, false, cur, end + 1 - cur);
3796 pg_offset += iosize;
3802 int btrfs_read_folio(struct file *file, struct folio *folio)
3804 struct page *page = &folio->page;
3805 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
3806 u64 start = page_offset(page);
3807 u64 end = start + PAGE_SIZE - 1;
3808 struct btrfs_bio_ctrl bio_ctrl = { 0 };
3811 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
3813 ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL);
3815 * If btrfs_do_readpage() failed we will want to submit the assembled
3816 * bio to do the cleanup.
3819 submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.compress_type);
3823 static inline void contiguous_readpages(struct page *pages[], int nr_pages,
3825 struct extent_map **em_cached,
3826 struct btrfs_bio_ctrl *bio_ctrl,
3829 struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);
3832 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
3834 for (index = 0; index < nr_pages; index++) {
3835 btrfs_do_readpage(pages[index], em_cached, bio_ctrl,
3836 REQ_RAHEAD, prev_em_start);
3837 put_page(pages[index]);
3842 * helper for __extent_writepage, doing all of the delayed allocation setup.
3844 * This returns 1 if btrfs_run_delalloc_range function did all the work required
3845 * to write the page (copy into inline extent). In this case the IO has
3846 * been started and the page is already unlocked.
3848 * This returns 0 if all went well (page still locked)
3849 * This returns < 0 if there were errors (page still locked)
3851 static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
3852 struct page *page, struct writeback_control *wbc)
3854 const u64 page_end = page_offset(page) + PAGE_SIZE - 1;
3855 u64 delalloc_start = page_offset(page);
3856 u64 delalloc_to_write = 0;
3857 /* How many pages are started by btrfs_run_delalloc_range() */
3858 unsigned long nr_written = 0;
3860 int page_started = 0;
3862 while (delalloc_start < page_end) {
3863 u64 delalloc_end = page_end;
3866 found = find_lock_delalloc_range(&inode->vfs_inode, page,
3870 delalloc_start = delalloc_end + 1;
3873 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3874 delalloc_end, &page_started, &nr_written, wbc);
3876 btrfs_page_set_error(inode->root->fs_info, page,
3877 page_offset(page), PAGE_SIZE);
3881 * delalloc_end is already one less than the total length, so
3882 * we don't subtract one from PAGE_SIZE
3884 delalloc_to_write += (delalloc_end - delalloc_start +
3885 PAGE_SIZE) >> PAGE_SHIFT;
3886 delalloc_start = delalloc_end + 1;
3888 if (wbc->nr_to_write < delalloc_to_write) {
3891 if (delalloc_to_write < thresh * 2)
3892 thresh = delalloc_to_write;
3893 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3897 /* Did btrfs_run_dealloc_range() already unlock and start the IO? */
3900 * We've unlocked the page, so we can't update the mapping's
3901 * writeback index, just update nr_to_write.
3903 wbc->nr_to_write -= nr_written;
3911 * Find the first byte we need to write.
3913 * For subpage, one page can contain several sectors, and
3914 * __extent_writepage_io() will just grab all extent maps in the page
3915 * range and try to submit all non-inline/non-compressed extents.
3917 * This is a big problem for subpage, we shouldn't re-submit already written
3919 * This function will lookup subpage dirty bit to find which range we really
3922 * Return the next dirty range in [@start, @end).
3923 * If no dirty range is found, @start will be page_offset(page) + PAGE_SIZE.
3925 static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
3926 struct page *page, u64 *start, u64 *end)
3928 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
3929 struct btrfs_subpage_info *spi = fs_info->subpage_info;
3930 u64 orig_start = *start;
3931 /* Declare as unsigned long so we can use bitmap ops */
3932 unsigned long flags;
3933 int range_start_bit;
3937 * For regular sector size == page size case, since one page only
3938 * contains one sector, we return the page offset directly.
3940 if (!btrfs_is_subpage(fs_info, page)) {
3941 *start = page_offset(page);
3942 *end = page_offset(page) + PAGE_SIZE;
3946 range_start_bit = spi->dirty_offset +
3947 (offset_in_page(orig_start) >> fs_info->sectorsize_bits);
3949 /* We should have the page locked, but just in case */
3950 spin_lock_irqsave(&subpage->lock, flags);
3951 bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit,
3952 spi->dirty_offset + spi->bitmap_nr_bits);
3953 spin_unlock_irqrestore(&subpage->lock, flags);
3955 range_start_bit -= spi->dirty_offset;
3956 range_end_bit -= spi->dirty_offset;
3958 *start = page_offset(page) + range_start_bit * fs_info->sectorsize;
3959 *end = page_offset(page) + range_end_bit * fs_info->sectorsize;
3963 * helper for __extent_writepage. This calls the writepage start hooks,
3964 * and does the loop to map the page into extents and bios.
3966 * We return 1 if the IO is started and the page is unlocked,
3967 * 0 if all went well (page still locked)
3968 * < 0 if there were errors (page still locked)
3970 static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
3972 struct writeback_control *wbc,
3973 struct extent_page_data *epd,
3977 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3978 u64 cur = page_offset(page);
3979 u64 end = cur + PAGE_SIZE - 1;
3982 struct extent_map *em;
3986 u32 opf = REQ_OP_WRITE;
3987 const unsigned int write_flags = wbc_to_write_flags(wbc);
3988 bool has_error = false;
3991 ret = btrfs_writepage_cow_fixup(page);
3993 /* Fixup worker will requeue */
3994 redirty_page_for_writepage(wbc, page);
4000 * we don't want to touch the inode after unlocking the page,
4001 * so we update the mapping writeback index now
4005 while (cur <= end) {
4008 u64 dirty_range_start = cur;
4009 u64 dirty_range_end;
4012 if (cur >= i_size) {
4013 btrfs_writepage_endio_finish_ordered(inode, page, cur,
4016 * This range is beyond i_size, thus we don't need to
4017 * bother writing back.
4018 * But we still need to clear the dirty subpage bit, or
4019 * the next time the page gets dirtied, we will try to
4020 * writeback the sectors with subpage dirty bits,
4021 * causing writeback without ordered extent.
4023 btrfs_page_clear_dirty(fs_info, page, cur, end + 1 - cur);
4027 find_next_dirty_byte(fs_info, page, &dirty_range_start,
4029 if (cur < dirty_range_start) {
4030 cur = dirty_range_start;
4034 em = btrfs_get_extent(inode, NULL, 0, cur, end - cur + 1);
4036 btrfs_page_set_error(fs_info, page, cur, end - cur + 1);
4037 ret = PTR_ERR_OR_ZERO(em);
4044 extent_offset = cur - em->start;
4045 em_end = extent_map_end(em);
4046 ASSERT(cur <= em_end);
4048 ASSERT(IS_ALIGNED(em->start, fs_info->sectorsize));
4049 ASSERT(IS_ALIGNED(em->len, fs_info->sectorsize));
4050 block_start = em->block_start;
4051 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4052 disk_bytenr = em->block_start + extent_offset;
4055 * Note that em_end from extent_map_end() and dirty_range_end from
4056 * find_next_dirty_byte() are all exclusive
4058 iosize = min(min(em_end, end + 1), dirty_range_end) - cur;
4060 if (btrfs_use_zone_append(inode, em->block_start))
4061 opf = REQ_OP_ZONE_APPEND;
4063 free_extent_map(em);
4067 * compressed and inline extents are written through other
4070 if (compressed || block_start == EXTENT_MAP_HOLE ||
4071 block_start == EXTENT_MAP_INLINE) {
4075 btrfs_writepage_endio_finish_ordered(inode,
4076 page, cur, cur + iosize - 1, true);
4077 btrfs_page_clear_dirty(fs_info, page, cur, iosize);
4082 btrfs_set_range_writeback(inode, cur, cur + iosize - 1);
4083 if (!PageWriteback(page)) {
4084 btrfs_err(inode->root->fs_info,
4085 "page %lu not writeback, cur %llu end %llu",
4086 page->index, cur, end);
4090 * Although the PageDirty bit is cleared before entering this
4091 * function, subpage dirty bit is not cleared.
4092 * So clear subpage dirty bit here so next time we won't submit
4093 * page for range already written to disk.
4095 btrfs_page_clear_dirty(fs_info, page, cur, iosize);
4097 ret = submit_extent_page(opf | write_flags, wbc,
4098 &epd->bio_ctrl, page,
4099 disk_bytenr, iosize,
4100 cur - page_offset(page),
4101 end_bio_extent_writepage,
4108 btrfs_page_set_error(fs_info, page, cur, iosize);
4109 if (PageWriteback(page))
4110 btrfs_page_clear_writeback(fs_info, page, cur,
4118 * If we finish without problem, we should not only clear page dirty,
4119 * but also empty subpage dirty bits
4122 btrfs_page_assert_not_dirty(fs_info, page);
4130 * the writepage semantics are similar to regular writepage. extent
4131 * records are inserted to lock ranges in the tree, and as dirty areas
4132 * are found, they are marked writeback. Then the lock bits are removed
4133 * and the end_io handler clears the writeback ranges
4135 * Return 0 if everything goes well.
4136 * Return <0 for error.
4138 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
4139 struct extent_page_data *epd)
4141 struct folio *folio = page_folio(page);
4142 struct inode *inode = page->mapping->host;
4143 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4144 const u64 page_start = page_offset(page);
4145 const u64 page_end = page_start + PAGE_SIZE - 1;
4149 loff_t i_size = i_size_read(inode);
4150 unsigned long end_index = i_size >> PAGE_SHIFT;
4152 trace___extent_writepage(page, inode, wbc);
4154 WARN_ON(!PageLocked(page));
4156 btrfs_page_clear_error(btrfs_sb(inode->i_sb), page,
4157 page_offset(page), PAGE_SIZE);
4159 pg_offset = offset_in_page(i_size);
4160 if (page->index > end_index ||
4161 (page->index == end_index && !pg_offset)) {
4162 folio_invalidate(folio, 0, folio_size(folio));
4163 folio_unlock(folio);
4167 if (page->index == end_index) {
4168 memzero_page(page, pg_offset, PAGE_SIZE - pg_offset);
4169 flush_dcache_page(page);
4172 ret = set_page_extent_mapped(page);
4178 if (!epd->extent_locked) {
4179 ret = writepage_delalloc(BTRFS_I(inode), page, wbc);
4186 ret = __extent_writepage_io(BTRFS_I(inode), page, wbc, epd, i_size,
4193 /* make sure the mapping tag for page dirty gets cleared */
4194 set_page_writeback(page);
4195 end_page_writeback(page);
4198 * Here we used to have a check for PageError() and then set @ret and
4199 * call end_extent_writepage().
4201 * But in fact setting @ret here will cause different error paths
4202 * between subpage and regular sectorsize.
4204 * For regular page size, we never submit current page, but only add
4205 * current page to current bio.
4206 * The bio submission can only happen in next page.
4207 * Thus if we hit the PageError() branch, @ret is already set to
4208 * non-zero value and will not get updated for regular sectorsize.
4210 * But for subpage case, it's possible we submit part of current page,
4211 * thus can get PageError() set by submitted bio of the same page,
4212 * while our @ret is still 0.
4214 * So here we unify the behavior and don't set @ret.
4215 * Error can still be properly passed to higher layer as page will
4216 * be set error, here we just don't handle the IO failure.
4218 * NOTE: This is just a hotfix for subpage.
4219 * The root fix will be properly ending ordered extent when we hit
4220 * an error during writeback.
4222 * But that needs a bigger refactoring, as we not only need to grab the
4223 * submitted OE, but also need to know exactly at which bytenr we hit
4225 * Currently the full page based __extent_writepage_io() is not
4228 if (PageError(page))
4229 end_extent_writepage(page, ret, page_start, page_end);
4230 if (epd->extent_locked) {
4232 * If epd->extent_locked, it's from extent_write_locked_range(),
4233 * the page can either be locked by lock_page() or
4234 * process_one_page().
4235 * Let btrfs_page_unlock_writer() handle both cases.
4238 btrfs_page_unlock_writer(fs_info, page, wbc->range_start,
4239 wbc->range_end + 1 - wbc->range_start);
4247 void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
4249 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
4250 TASK_UNINTERRUPTIBLE);
4253 static void end_extent_buffer_writeback(struct extent_buffer *eb)
4255 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
4256 smp_mb__after_atomic();
4257 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
4261 * Lock extent buffer status and pages for writeback.
4263 * May try to flush write bio if we can't get the lock.
4265 * Return 0 if the extent buffer doesn't need to be submitted.
4266 * (E.g. the extent buffer is not dirty)
4267 * Return >0 is the extent buffer is submitted to bio.
4268 * Return <0 if something went wrong, no page is locked.
4270 static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
4271 struct extent_page_data *epd)
4273 struct btrfs_fs_info *fs_info = eb->fs_info;
4278 if (!btrfs_try_tree_write_lock(eb)) {
4279 flush_write_bio(epd);
4281 btrfs_tree_lock(eb);
4284 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
4285 btrfs_tree_unlock(eb);
4289 flush_write_bio(epd);
4293 wait_on_extent_buffer_writeback(eb);
4294 btrfs_tree_lock(eb);
4295 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
4297 btrfs_tree_unlock(eb);
4302 * We need to do this to prevent races in people who check if the eb is
4303 * under IO since we can end up having no IO bits set for a short period
4306 spin_lock(&eb->refs_lock);
4307 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4308 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
4309 spin_unlock(&eb->refs_lock);
4310 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
4311 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
4313 fs_info->dirty_metadata_batch);
4316 spin_unlock(&eb->refs_lock);
4319 btrfs_tree_unlock(eb);
4322 * Either we don't need to submit any tree block, or we're submitting
4324 * Subpage metadata doesn't use page locking at all, so we can skip
4327 if (!ret || fs_info->nodesize < PAGE_SIZE)
4330 num_pages = num_extent_pages(eb);
4331 for (i = 0; i < num_pages; i++) {
4332 struct page *p = eb->pages[i];
4334 if (!trylock_page(p)) {
4336 flush_write_bio(epd);
4346 static void set_btree_ioerr(struct page *page, struct extent_buffer *eb)
4348 struct btrfs_fs_info *fs_info = eb->fs_info;
4350 btrfs_page_set_error(fs_info, page, eb->start, eb->len);
4351 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
4355 * A read may stumble upon this buffer later, make sure that it gets an
4356 * error and knows there was an error.
4358 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4361 * We need to set the mapping with the io error as well because a write
4362 * error will flip the file system readonly, and then syncfs() will
4363 * return a 0 because we are readonly if we don't modify the err seq for
4366 mapping_set_error(page->mapping, -EIO);
4369 * If we error out, we should add back the dirty_metadata_bytes
4370 * to make it consistent.
4372 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
4373 eb->len, fs_info->dirty_metadata_batch);
4376 * If writeback for a btree extent that doesn't belong to a log tree
4377 * failed, increment the counter transaction->eb_write_errors.
4378 * We do this because while the transaction is running and before it's
4379 * committing (when we call filemap_fdata[write|wait]_range against
4380 * the btree inode), we might have
4381 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
4382 * returns an error or an error happens during writeback, when we're
4383 * committing the transaction we wouldn't know about it, since the pages
4384 * can be no longer dirty nor marked anymore for writeback (if a
4385 * subsequent modification to the extent buffer didn't happen before the
4386 * transaction commit), which makes filemap_fdata[write|wait]_range not
4387 * able to find the pages tagged with SetPageError at transaction
4388 * commit time. So if this happens we must abort the transaction,
4389 * otherwise we commit a super block with btree roots that point to
4390 * btree nodes/leafs whose content on disk is invalid - either garbage
4391 * or the content of some node/leaf from a past generation that got
4392 * cowed or deleted and is no longer valid.
4394 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
4395 * not be enough - we need to distinguish between log tree extents vs
4396 * non-log tree extents, and the next filemap_fdatawait_range() call
4397 * will catch and clear such errors in the mapping - and that call might
4398 * be from a log sync and not from a transaction commit. Also, checking
4399 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
4400 * not done and would not be reliable - the eb might have been released
4401 * from memory and reading it back again means that flag would not be
4402 * set (since it's a runtime flag, not persisted on disk).
4404 * Using the flags below in the btree inode also makes us achieve the
4405 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
4406 * writeback for all dirty pages and before filemap_fdatawait_range()
4407 * is called, the writeback for all dirty pages had already finished
4408 * with errors - because we were not using AS_EIO/AS_ENOSPC,
4409 * filemap_fdatawait_range() would return success, as it could not know
4410 * that writeback errors happened (the pages were no longer tagged for
4413 switch (eb->log_index) {
4415 set_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags);
4418 set_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
4421 set_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
4424 BUG(); /* unexpected, logic error */
4429 * The endio specific version which won't touch any unsafe spinlock in endio
4432 static struct extent_buffer *find_extent_buffer_nolock(
4433 struct btrfs_fs_info *fs_info, u64 start)
4435 struct extent_buffer *eb;
4438 eb = xa_load(&fs_info->extent_buffers,
4439 start >> fs_info->sectorsize_bits);
4440 if (eb && atomic_inc_not_zero(&eb->refs)) {
4449 * The endio function for subpage extent buffer write.
4451 * Unlike end_bio_extent_buffer_writepage(), we only call end_page_writeback()
4452 * after all extent buffers in the page has finished their writeback.
4454 static void end_bio_subpage_eb_writepage(struct bio *bio)
4456 struct btrfs_fs_info *fs_info;
4457 struct bio_vec *bvec;
4458 struct bvec_iter_all iter_all;
4460 fs_info = btrfs_sb(bio_first_page_all(bio)->mapping->host->i_sb);
4461 ASSERT(fs_info->nodesize < PAGE_SIZE);
4463 ASSERT(!bio_flagged(bio, BIO_CLONED));
4464 bio_for_each_segment_all(bvec, bio, iter_all) {
4465 struct page *page = bvec->bv_page;
4466 u64 bvec_start = page_offset(page) + bvec->bv_offset;
4467 u64 bvec_end = bvec_start + bvec->bv_len - 1;
4468 u64 cur_bytenr = bvec_start;
4470 ASSERT(IS_ALIGNED(bvec->bv_len, fs_info->nodesize));
4472 /* Iterate through all extent buffers in the range */
4473 while (cur_bytenr <= bvec_end) {
4474 struct extent_buffer *eb;
4478 * Here we can't use find_extent_buffer(), as it may
4479 * try to lock eb->refs_lock, which is not safe in endio
4482 eb = find_extent_buffer_nolock(fs_info, cur_bytenr);
4485 cur_bytenr = eb->start + eb->len;
4487 ASSERT(test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags));
4488 done = atomic_dec_and_test(&eb->io_pages);
4491 if (bio->bi_status ||
4492 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
4493 ClearPageUptodate(page);
4494 set_btree_ioerr(page, eb);
4497 btrfs_subpage_clear_writeback(fs_info, page, eb->start,
4499 end_extent_buffer_writeback(eb);
4501 * free_extent_buffer() will grab spinlock which is not
4502 * safe in endio context. Thus here we manually dec
4505 atomic_dec(&eb->refs);
4511 static void end_bio_extent_buffer_writepage(struct bio *bio)
4513 struct bio_vec *bvec;
4514 struct extent_buffer *eb;
4516 struct bvec_iter_all iter_all;
4518 ASSERT(!bio_flagged(bio, BIO_CLONED));
4519 bio_for_each_segment_all(bvec, bio, iter_all) {
4520 struct page *page = bvec->bv_page;
4522 eb = (struct extent_buffer *)page->private;
4524 done = atomic_dec_and_test(&eb->io_pages);
4526 if (bio->bi_status ||
4527 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
4528 ClearPageUptodate(page);
4529 set_btree_ioerr(page, eb);
4532 end_page_writeback(page);
4537 end_extent_buffer_writeback(eb);
4543 static void prepare_eb_write(struct extent_buffer *eb)
4546 unsigned long start;
4549 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
4550 atomic_set(&eb->io_pages, num_extent_pages(eb));
4552 /* Set btree blocks beyond nritems with 0 to avoid stale content */
4553 nritems = btrfs_header_nritems(eb);
4554 if (btrfs_header_level(eb) > 0) {
4555 end = btrfs_node_key_ptr_offset(nritems);
4556 memzero_extent_buffer(eb, end, eb->len - end);
4560 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
4562 start = btrfs_item_nr_offset(nritems);
4563 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
4564 memzero_extent_buffer(eb, start, end - start);
4569 * Unlike the work in write_one_eb(), we rely completely on extent locking.
4570 * Page locking is only utilized at minimum to keep the VMM code happy.
4572 static int write_one_subpage_eb(struct extent_buffer *eb,
4573 struct writeback_control *wbc,
4574 struct extent_page_data *epd)
4576 struct btrfs_fs_info *fs_info = eb->fs_info;
4577 struct page *page = eb->pages[0];
4578 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
4579 bool no_dirty_ebs = false;
4582 prepare_eb_write(eb);
4584 /* clear_page_dirty_for_io() in subpage helper needs page locked */
4586 btrfs_subpage_set_writeback(fs_info, page, eb->start, eb->len);
4588 /* Check if this is the last dirty bit to update nr_written */
4589 no_dirty_ebs = btrfs_subpage_clear_and_test_dirty(fs_info, page,
4590 eb->start, eb->len);
4592 clear_page_dirty_for_io(page);
4594 ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
4595 &epd->bio_ctrl, page, eb->start, eb->len,
4596 eb->start - page_offset(page),
4597 end_bio_subpage_eb_writepage, 0, 0, false);
4599 btrfs_subpage_clear_writeback(fs_info, page, eb->start, eb->len);
4600 set_btree_ioerr(page, eb);
4603 if (atomic_dec_and_test(&eb->io_pages))
4604 end_extent_buffer_writeback(eb);
4609 * Submission finished without problem, if no range of the page is
4610 * dirty anymore, we have submitted a page. Update nr_written in wbc.
4617 static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
4618 struct writeback_control *wbc,
4619 struct extent_page_data *epd)
4621 u64 disk_bytenr = eb->start;
4623 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
4626 prepare_eb_write(eb);
4628 num_pages = num_extent_pages(eb);
4629 for (i = 0; i < num_pages; i++) {
4630 struct page *p = eb->pages[i];
4632 clear_page_dirty_for_io(p);
4633 set_page_writeback(p);
4634 ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
4635 &epd->bio_ctrl, p, disk_bytenr,
4637 end_bio_extent_buffer_writepage,
4640 set_btree_ioerr(p, eb);
4641 if (PageWriteback(p))
4642 end_page_writeback(p);
4643 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
4644 end_extent_buffer_writeback(eb);
4648 disk_bytenr += PAGE_SIZE;
4653 if (unlikely(ret)) {
4654 for (; i < num_pages; i++) {
4655 struct page *p = eb->pages[i];
4656 clear_page_dirty_for_io(p);
4665 * Submit one subpage btree page.
4667 * The main difference to submit_eb_page() is:
4669 * For subpage, we don't rely on page locking at all.
4672 * We only flush bio if we may be unable to fit current extent buffers into
4675 * Return >=0 for the number of submitted extent buffers.
4676 * Return <0 for fatal error.
4678 static int submit_eb_subpage(struct page *page,
4679 struct writeback_control *wbc,
4680 struct extent_page_data *epd)
4682 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
4684 u64 page_start = page_offset(page);
4686 int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits;
4689 /* Lock and write each dirty extent buffers in the range */
4690 while (bit_start < fs_info->subpage_info->bitmap_nr_bits) {
4691 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
4692 struct extent_buffer *eb;
4693 unsigned long flags;
4697 * Take private lock to ensure the subpage won't be detached
4700 spin_lock(&page->mapping->private_lock);
4701 if (!PagePrivate(page)) {
4702 spin_unlock(&page->mapping->private_lock);
4705 spin_lock_irqsave(&subpage->lock, flags);
4706 if (!test_bit(bit_start + fs_info->subpage_info->dirty_offset,
4707 subpage->bitmaps)) {
4708 spin_unlock_irqrestore(&subpage->lock, flags);
4709 spin_unlock(&page->mapping->private_lock);
4714 start = page_start + bit_start * fs_info->sectorsize;
4715 bit_start += sectors_per_node;
4718 * Here we just want to grab the eb without touching extra
4719 * spin locks, so call find_extent_buffer_nolock().
4721 eb = find_extent_buffer_nolock(fs_info, start);
4722 spin_unlock_irqrestore(&subpage->lock, flags);
4723 spin_unlock(&page->mapping->private_lock);
4726 * The eb has already reached 0 refs thus find_extent_buffer()
4727 * doesn't return it. We don't need to write back such eb
4733 ret = lock_extent_buffer_for_io(eb, epd);
4735 free_extent_buffer(eb);
4739 free_extent_buffer(eb);
4742 ret = write_one_subpage_eb(eb, wbc, epd);
4743 free_extent_buffer(eb);
4751 /* We hit error, end bio for the submitted extent buffers */
4752 end_write_bio(epd, ret);
4757 * Submit all page(s) of one extent buffer.
4759 * @page: the page of one extent buffer
4760 * @eb_context: to determine if we need to submit this page, if current page
4761 * belongs to this eb, we don't need to submit
4763 * The caller should pass each page in their bytenr order, and here we use
4764 * @eb_context to determine if we have submitted pages of one extent buffer.
4766 * If we have, we just skip until we hit a new page that doesn't belong to
4767 * current @eb_context.
4769 * If not, we submit all the page(s) of the extent buffer.
4771 * Return >0 if we have submitted the extent buffer successfully.
4772 * Return 0 if we don't need to submit the page, as it's already submitted by
4774 * Return <0 for fatal error.
4776 static int submit_eb_page(struct page *page, struct writeback_control *wbc,
4777 struct extent_page_data *epd,
4778 struct extent_buffer **eb_context)
4780 struct address_space *mapping = page->mapping;
4781 struct btrfs_block_group *cache = NULL;
4782 struct extent_buffer *eb;
4785 if (!PagePrivate(page))
4788 if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE)
4789 return submit_eb_subpage(page, wbc, epd);
4791 spin_lock(&mapping->private_lock);
4792 if (!PagePrivate(page)) {
4793 spin_unlock(&mapping->private_lock);
4797 eb = (struct extent_buffer *)page->private;
4800 * Shouldn't happen and normally this would be a BUG_ON but no point
4801 * crashing the machine for something we can survive anyway.
4804 spin_unlock(&mapping->private_lock);
4808 if (eb == *eb_context) {
4809 spin_unlock(&mapping->private_lock);
4812 ret = atomic_inc_not_zero(&eb->refs);
4813 spin_unlock(&mapping->private_lock);
4817 if (!btrfs_check_meta_write_pointer(eb->fs_info, eb, &cache)) {
4819 * If for_sync, this hole will be filled with
4820 * trasnsaction commit.
4822 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
4826 free_extent_buffer(eb);
4832 ret = lock_extent_buffer_for_io(eb, epd);
4834 btrfs_revert_meta_write_pointer(cache, eb);
4836 btrfs_put_block_group(cache);
4837 free_extent_buffer(eb);
4842 * Implies write in zoned mode. Mark the last eb in a block group.
4844 btrfs_schedule_zone_finish_bg(cache, eb);
4845 btrfs_put_block_group(cache);
4847 ret = write_one_eb(eb, wbc, epd);
4848 free_extent_buffer(eb);
4854 int btree_write_cache_pages(struct address_space *mapping,
4855 struct writeback_control *wbc)
4857 struct extent_buffer *eb_context = NULL;
4858 struct extent_page_data epd = {
4861 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
4863 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
4866 int nr_to_write_done = 0;
4867 struct pagevec pvec;
4870 pgoff_t end; /* Inclusive */
4874 pagevec_init(&pvec);
4875 if (wbc->range_cyclic) {
4876 index = mapping->writeback_index; /* Start from prev offset */
4879 * Start from the beginning does not need to cycle over the
4880 * range, mark it as scanned.
4882 scanned = (index == 0);
4884 index = wbc->range_start >> PAGE_SHIFT;
4885 end = wbc->range_end >> PAGE_SHIFT;
4888 if (wbc->sync_mode == WB_SYNC_ALL)
4889 tag = PAGECACHE_TAG_TOWRITE;
4891 tag = PAGECACHE_TAG_DIRTY;
4892 btrfs_zoned_meta_io_lock(fs_info);
4894 if (wbc->sync_mode == WB_SYNC_ALL)
4895 tag_pages_for_writeback(mapping, index, end);
4896 while (!done && !nr_to_write_done && (index <= end) &&
4897 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
4901 for (i = 0; i < nr_pages; i++) {
4902 struct page *page = pvec.pages[i];
4904 ret = submit_eb_page(page, wbc, &epd, &eb_context);
4913 * the filesystem may choose to bump up nr_to_write.
4914 * We have to make sure to honor the new nr_to_write
4917 nr_to_write_done = wbc->nr_to_write <= 0;
4919 pagevec_release(&pvec);
4922 if (!scanned && !done) {
4924 * We hit the last page and there is more work to be done: wrap
4925 * back to the start of the file
4932 end_write_bio(&epd, ret);
4936 * If something went wrong, don't allow any metadata write bio to be
4939 * This would prevent use-after-free if we had dirty pages not
4940 * cleaned up, which can still happen by fuzzed images.
4943 * Allowing existing tree block to be allocated for other trees.
4945 * - Log tree operations
4946 * Exiting tree blocks get allocated to log tree, bumps its
4947 * generation, then get cleaned in tree re-balance.
4948 * Such tree block will not be written back, since it's clean,
4949 * thus no WRITTEN flag set.
4950 * And after log writes back, this tree block is not traced by
4951 * any dirty extent_io_tree.
4953 * - Offending tree block gets re-dirtied from its original owner
4954 * Since it has bumped generation, no WRITTEN flag, it can be
4955 * reused without COWing. This tree block will not be traced
4956 * by btrfs_transaction::dirty_pages.
4958 * Now such dirty tree block will not be cleaned by any dirty
4959 * extent io tree. Thus we don't want to submit such wild eb
4960 * if the fs already has error.
4962 if (!BTRFS_FS_ERROR(fs_info)) {
4963 flush_write_bio(&epd);
4966 end_write_bio(&epd, ret);
4969 btrfs_zoned_meta_io_unlock(fs_info);
4971 * We can get ret > 0 from submit_extent_page() indicating how many ebs
4972 * were submitted. Reset it to 0 to avoid false alerts for the caller.
4980 * Walk the list of dirty pages of the given address space and write all of them.
4982 * @mapping: address space structure to write
4983 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
4984 * @epd: holds context for the write, namely the bio
4986 * If a page is already under I/O, write_cache_pages() skips it, even
4987 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
4988 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
4989 * and msync() need to guarantee that all the data which was dirty at the time
4990 * the call was made get new I/O started against them. If wbc->sync_mode is
4991 * WB_SYNC_ALL then we were called for data integrity and we must wait for
4992 * existing IO to complete.
4994 static int extent_write_cache_pages(struct address_space *mapping,
4995 struct writeback_control *wbc,
4996 struct extent_page_data *epd)
4998 struct inode *inode = mapping->host;
5001 int nr_to_write_done = 0;
5002 struct pagevec pvec;
5005 pgoff_t end; /* Inclusive */
5007 int range_whole = 0;
5012 * We have to hold onto the inode so that ordered extents can do their
5013 * work when the IO finishes. The alternative to this is failing to add
5014 * an ordered extent if the igrab() fails there and that is a huge pain
5015 * to deal with, so instead just hold onto the inode throughout the
5016 * writepages operation. If it fails here we are freeing up the inode
5017 * anyway and we'd rather not waste our time writing out stuff that is
5018 * going to be truncated anyway.
5023 pagevec_init(&pvec);
5024 if (wbc->range_cyclic) {
5025 index = mapping->writeback_index; /* Start from prev offset */
5028 * Start from the beginning does not need to cycle over the
5029 * range, mark it as scanned.
5031 scanned = (index == 0);
5033 index = wbc->range_start >> PAGE_SHIFT;
5034 end = wbc->range_end >> PAGE_SHIFT;
5035 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
5041 * We do the tagged writepage as long as the snapshot flush bit is set
5042 * and we are the first one who do the filemap_flush() on this inode.
5044 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
5045 * not race in and drop the bit.
5047 if (range_whole && wbc->nr_to_write == LONG_MAX &&
5048 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
5049 &BTRFS_I(inode)->runtime_flags))
5050 wbc->tagged_writepages = 1;
5052 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
5053 tag = PAGECACHE_TAG_TOWRITE;
5055 tag = PAGECACHE_TAG_DIRTY;
5057 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
5058 tag_pages_for_writeback(mapping, index, end);
5060 while (!done && !nr_to_write_done && (index <= end) &&
5061 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
5062 &index, end, tag))) {
5065 for (i = 0; i < nr_pages; i++) {
5066 struct page *page = pvec.pages[i];
5068 done_index = page->index + 1;
5070 * At this point we hold neither the i_pages lock nor
5071 * the page lock: the page may be truncated or
5072 * invalidated (changing page->mapping to NULL),
5073 * or even swizzled back from swapper_space to
5074 * tmpfs file mapping
5076 if (!trylock_page(page)) {
5077 flush_write_bio(epd);
5081 if (unlikely(page->mapping != mapping)) {
5086 if (wbc->sync_mode != WB_SYNC_NONE) {
5087 if (PageWriteback(page))
5088 flush_write_bio(epd);
5089 wait_on_page_writeback(page);
5092 if (PageWriteback(page) ||
5093 !clear_page_dirty_for_io(page)) {
5098 ret = __extent_writepage(page, wbc, epd);
5105 * the filesystem may choose to bump up nr_to_write.
5106 * We have to make sure to honor the new nr_to_write
5109 nr_to_write_done = wbc->nr_to_write <= 0;
5111 pagevec_release(&pvec);
5114 if (!scanned && !done) {
5116 * We hit the last page and there is more work to be done: wrap
5117 * back to the start of the file
5123 * If we're looping we could run into a page that is locked by a
5124 * writer and that writer could be waiting on writeback for a
5125 * page in our current bio, and thus deadlock, so flush the
5128 flush_write_bio(epd);
5132 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
5133 mapping->writeback_index = done_index;
5135 btrfs_add_delayed_iput(inode);
5139 int extent_write_full_page(struct page *page, struct writeback_control *wbc)
5142 struct extent_page_data epd = {
5145 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
5148 ret = __extent_writepage(page, wbc, &epd);
5151 end_write_bio(&epd, ret);
5155 flush_write_bio(&epd);
5160 * Submit the pages in the range to bio for call sites which delalloc range has
5161 * already been ran (aka, ordered extent inserted) and all pages are still
5164 int extent_write_locked_range(struct inode *inode, u64 start, u64 end)
5166 bool found_error = false;
5167 int first_error = 0;
5169 struct address_space *mapping = inode->i_mapping;
5172 unsigned long nr_pages;
5173 const u32 sectorsize = btrfs_sb(inode->i_sb)->sectorsize;
5174 struct extent_page_data epd = {
5179 struct writeback_control wbc_writepages = {
5180 .sync_mode = WB_SYNC_ALL,
5181 .range_start = start,
5182 .range_end = end + 1,
5183 /* We're called from an async helper function */
5184 .punt_to_cgroup = 1,
5185 .no_cgroup_owner = 1,
5188 ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(end + 1, sectorsize));
5189 nr_pages = (round_up(end, PAGE_SIZE) - round_down(start, PAGE_SIZE)) >>
5191 wbc_writepages.nr_to_write = nr_pages * 2;
5193 wbc_attach_fdatawrite_inode(&wbc_writepages, inode);
5194 while (cur <= end) {
5195 u64 cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);
5197 page = find_get_page(mapping, cur >> PAGE_SHIFT);
5199 * All pages in the range are locked since
5200 * btrfs_run_delalloc_range(), thus there is no way to clear
5201 * the page dirty flag.
5203 ASSERT(PageLocked(page));
5204 ASSERT(PageDirty(page));
5205 clear_page_dirty_for_io(page);
5206 ret = __extent_writepage(page, &wbc_writepages, &epd);
5217 flush_write_bio(&epd);
5219 end_write_bio(&epd, ret);
5221 wbc_detach_inode(&wbc_writepages);
5227 int extent_writepages(struct address_space *mapping,
5228 struct writeback_control *wbc)
5230 struct inode *inode = mapping->host;
5232 struct extent_page_data epd = {
5235 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
5239 * Allow only a single thread to do the reloc work in zoned mode to
5240 * protect the write pointer updates.
5242 btrfs_zoned_data_reloc_lock(BTRFS_I(inode));
5243 ret = extent_write_cache_pages(mapping, wbc, &epd);
5246 btrfs_zoned_data_reloc_unlock(BTRFS_I(inode));
5247 end_write_bio(&epd, ret);
5250 flush_write_bio(&epd);
5251 btrfs_zoned_data_reloc_unlock(BTRFS_I(inode));
5255 void extent_readahead(struct readahead_control *rac)
5257 struct btrfs_bio_ctrl bio_ctrl = { 0 };
5258 struct page *pagepool[16];
5259 struct extent_map *em_cached = NULL;
5260 u64 prev_em_start = (u64)-1;
5263 while ((nr = readahead_page_batch(rac, pagepool))) {
5264 u64 contig_start = readahead_pos(rac);
5265 u64 contig_end = contig_start + readahead_batch_length(rac) - 1;
5267 contiguous_readpages(pagepool, nr, contig_start, contig_end,
5268 &em_cached, &bio_ctrl, &prev_em_start);
5272 free_extent_map(em_cached);
5275 submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.compress_type);
5279 * basic invalidate_folio code, this waits on any locked or writeback
5280 * ranges corresponding to the folio, and then deletes any extent state
5281 * records from the tree
5283 int extent_invalidate_folio(struct extent_io_tree *tree,
5284 struct folio *folio, size_t offset)
5286 struct extent_state *cached_state = NULL;
5287 u64 start = folio_pos(folio);
5288 u64 end = start + folio_size(folio) - 1;
5289 size_t blocksize = folio->mapping->host->i_sb->s_blocksize;
5291 /* This function is only called for the btree inode */
5292 ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO);
5294 start += ALIGN(offset, blocksize);
5298 lock_extent_bits(tree, start, end, &cached_state);
5299 folio_wait_writeback(folio);
5302 * Currently for btree io tree, only EXTENT_LOCKED is utilized,
5303 * so here we only need to unlock the extent range to free any
5304 * existing extent state.
5306 unlock_extent_cached(tree, start, end, &cached_state);
5311 * a helper for release_folio, this tests for areas of the page that
5312 * are locked or under IO and drops the related state bits if it is safe
5315 static int try_release_extent_state(struct extent_io_tree *tree,
5316 struct page *page, gfp_t mask)
5318 u64 start = page_offset(page);
5319 u64 end = start + PAGE_SIZE - 1;
5322 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
5326 * At this point we can safely clear everything except the
5327 * locked bit, the nodatasum bit and the delalloc new bit.
5328 * The delalloc new bit will be cleared by ordered extent
5331 ret = __clear_extent_bit(tree, start, end,
5332 ~(EXTENT_LOCKED | EXTENT_NODATASUM | EXTENT_DELALLOC_NEW),
5333 0, 0, NULL, mask, NULL);
5335 /* if clear_extent_bit failed for enomem reasons,
5336 * we can't allow the release to continue.
5347 * a helper for release_folio. As long as there are no locked extents
5348 * in the range corresponding to the page, both state records and extent
5349 * map records are removed
5351 int try_release_extent_mapping(struct page *page, gfp_t mask)
5353 struct extent_map *em;
5354 u64 start = page_offset(page);
5355 u64 end = start + PAGE_SIZE - 1;
5356 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
5357 struct extent_io_tree *tree = &btrfs_inode->io_tree;
5358 struct extent_map_tree *map = &btrfs_inode->extent_tree;
5360 if (gfpflags_allow_blocking(mask) &&
5361 page->mapping->host->i_size > SZ_16M) {
5363 while (start <= end) {
5364 struct btrfs_fs_info *fs_info;
5367 len = end - start + 1;
5368 write_lock(&map->lock);
5369 em = lookup_extent_mapping(map, start, len);
5371 write_unlock(&map->lock);
5374 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
5375 em->start != start) {
5376 write_unlock(&map->lock);
5377 free_extent_map(em);
5380 if (test_range_bit(tree, em->start,
5381 extent_map_end(em) - 1,
5382 EXTENT_LOCKED, 0, NULL))
5385 * If it's not in the list of modified extents, used
5386 * by a fast fsync, we can remove it. If it's being
5387 * logged we can safely remove it since fsync took an
5388 * extra reference on the em.
5390 if (list_empty(&em->list) ||
5391 test_bit(EXTENT_FLAG_LOGGING, &em->flags))
5394 * If it's in the list of modified extents, remove it
5395 * only if its generation is older then the current one,
5396 * in which case we don't need it for a fast fsync.
5397 * Otherwise don't remove it, we could be racing with an
5398 * ongoing fast fsync that could miss the new extent.
5400 fs_info = btrfs_inode->root->fs_info;
5401 spin_lock(&fs_info->trans_lock);
5402 cur_gen = fs_info->generation;
5403 spin_unlock(&fs_info->trans_lock);
5404 if (em->generation >= cur_gen)
5408 * We only remove extent maps that are not in the list of
5409 * modified extents or that are in the list but with a
5410 * generation lower then the current generation, so there
5411 * is no need to set the full fsync flag on the inode (it
5412 * hurts the fsync performance for workloads with a data
5413 * size that exceeds or is close to the system's memory).
5415 remove_extent_mapping(map, em);
5416 /* once for the rb tree */
5417 free_extent_map(em);
5419 start = extent_map_end(em);
5420 write_unlock(&map->lock);
5423 free_extent_map(em);
5425 cond_resched(); /* Allow large-extent preemption. */
5428 return try_release_extent_state(tree, page, mask);
5432 * helper function for fiemap, which doesn't want to see any holes.
5433 * This maps until we find something past 'last'
5435 static struct extent_map *get_extent_skip_holes(struct btrfs_inode *inode,
5436 u64 offset, u64 last)
5438 u64 sectorsize = btrfs_inode_sectorsize(inode);
5439 struct extent_map *em;
5446 len = last - offset;
5449 len = ALIGN(len, sectorsize);
5450 em = btrfs_get_extent_fiemap(inode, offset, len);
5454 /* if this isn't a hole return it */
5455 if (em->block_start != EXTENT_MAP_HOLE)
5458 /* this is a hole, advance to the next extent */
5459 offset = extent_map_end(em);
5460 free_extent_map(em);
5468 * To cache previous fiemap extent
5470 * Will be used for merging fiemap extent
5472 struct fiemap_cache {
5481 * Helper to submit fiemap extent.
5483 * Will try to merge current fiemap extent specified by @offset, @phys,
5484 * @len and @flags with cached one.
5485 * And only when we fails to merge, cached one will be submitted as
5488 * Return value is the same as fiemap_fill_next_extent().
5490 static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
5491 struct fiemap_cache *cache,
5492 u64 offset, u64 phys, u64 len, u32 flags)
5500 * Sanity check, extent_fiemap() should have ensured that new
5501 * fiemap extent won't overlap with cached one.
5504 * NOTE: Physical address can overlap, due to compression
5506 if (cache->offset + cache->len > offset) {
5512 * Only merges fiemap extents if
5513 * 1) Their logical addresses are continuous
5515 * 2) Their physical addresses are continuous
5516 * So truly compressed (physical size smaller than logical size)
5517 * extents won't get merged with each other
5519 * 3) Share same flags except FIEMAP_EXTENT_LAST
5520 * So regular extent won't get merged with prealloc extent
5522 if (cache->offset + cache->len == offset &&
5523 cache->phys + cache->len == phys &&
5524 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
5525 (flags & ~FIEMAP_EXTENT_LAST)) {
5527 cache->flags |= flags;
5528 goto try_submit_last;
5531 /* Not mergeable, need to submit cached one */
5532 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
5533 cache->len, cache->flags);
5534 cache->cached = false;
5538 cache->cached = true;
5539 cache->offset = offset;
5542 cache->flags = flags;
5544 if (cache->flags & FIEMAP_EXTENT_LAST) {
5545 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
5546 cache->phys, cache->len, cache->flags);
5547 cache->cached = false;
5553 * Emit last fiemap cache
5555 * The last fiemap cache may still be cached in the following case:
5557 * |<- Fiemap range ->|
5558 * |<------------ First extent ----------->|
5560 * In this case, the first extent range will be cached but not emitted.
5561 * So we must emit it before ending extent_fiemap().
5563 static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
5564 struct fiemap_cache *cache)
5571 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
5572 cache->len, cache->flags);
5573 cache->cached = false;
5579 int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo,
5584 u64 max = start + len;
5588 u64 last_for_get_extent = 0;
5590 u64 isize = i_size_read(&inode->vfs_inode);
5591 struct btrfs_key found_key;
5592 struct extent_map *em = NULL;
5593 struct extent_state *cached_state = NULL;
5594 struct btrfs_path *path;
5595 struct btrfs_root *root = inode->root;
5596 struct fiemap_cache cache = { 0 };
5597 struct ulist *roots;
5598 struct ulist *tmp_ulist;
5607 path = btrfs_alloc_path();
5611 roots = ulist_alloc(GFP_KERNEL);
5612 tmp_ulist = ulist_alloc(GFP_KERNEL);
5613 if (!roots || !tmp_ulist) {
5615 goto out_free_ulist;
5619 * We can't initialize that to 'start' as this could miss extents due
5620 * to extent item merging
5623 start = round_down(start, btrfs_inode_sectorsize(inode));
5624 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
5627 * lookup the last file extent. We're not using i_size here
5628 * because there might be preallocation past i_size
5630 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
5633 goto out_free_ulist;
5641 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
5642 found_type = found_key.type;
5644 /* No extents, but there might be delalloc bits */
5645 if (found_key.objectid != btrfs_ino(inode) ||
5646 found_type != BTRFS_EXTENT_DATA_KEY) {
5647 /* have to trust i_size as the end */
5649 last_for_get_extent = isize;
5652 * remember the start of the last extent. There are a
5653 * bunch of different factors that go into the length of the
5654 * extent, so its much less complex to remember where it started
5656 last = found_key.offset;
5657 last_for_get_extent = last + 1;
5659 btrfs_release_path(path);
5662 * we might have some extents allocated but more delalloc past those
5663 * extents. so, we trust isize unless the start of the last extent is
5668 last_for_get_extent = isize;
5671 lock_extent_bits(&inode->io_tree, start, start + len - 1,
5674 em = get_extent_skip_holes(inode, start, last_for_get_extent);
5683 u64 offset_in_extent = 0;
5685 /* break if the extent we found is outside the range */
5686 if (em->start >= max || extent_map_end(em) < off)
5690 * get_extent may return an extent that starts before our
5691 * requested range. We have to make sure the ranges
5692 * we return to fiemap always move forward and don't
5693 * overlap, so adjust the offsets here
5695 em_start = max(em->start, off);
5698 * record the offset from the start of the extent
5699 * for adjusting the disk offset below. Only do this if the
5700 * extent isn't compressed since our in ram offset may be past
5701 * what we have actually allocated on disk.
5703 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
5704 offset_in_extent = em_start - em->start;
5705 em_end = extent_map_end(em);
5706 em_len = em_end - em_start;
5708 if (em->block_start < EXTENT_MAP_LAST_BYTE)
5709 disko = em->block_start + offset_in_extent;
5714 * bump off for our next call to get_extent
5716 off = extent_map_end(em);
5720 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
5722 flags |= FIEMAP_EXTENT_LAST;
5723 } else if (em->block_start == EXTENT_MAP_INLINE) {
5724 flags |= (FIEMAP_EXTENT_DATA_INLINE |
5725 FIEMAP_EXTENT_NOT_ALIGNED);
5726 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
5727 flags |= (FIEMAP_EXTENT_DELALLOC |
5728 FIEMAP_EXTENT_UNKNOWN);
5729 } else if (fieinfo->fi_extents_max) {
5730 u64 bytenr = em->block_start -
5731 (em->start - em->orig_start);
5734 * As btrfs supports shared space, this information
5735 * can be exported to userspace tools via
5736 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
5737 * then we're just getting a count and we can skip the
5740 ret = btrfs_check_shared(root, btrfs_ino(inode),
5741 bytenr, roots, tmp_ulist);
5745 flags |= FIEMAP_EXTENT_SHARED;
5748 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
5749 flags |= FIEMAP_EXTENT_ENCODED;
5750 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5751 flags |= FIEMAP_EXTENT_UNWRITTEN;
5753 free_extent_map(em);
5755 if ((em_start >= last) || em_len == (u64)-1 ||
5756 (last == (u64)-1 && isize <= em_end)) {
5757 flags |= FIEMAP_EXTENT_LAST;
5761 /* now scan forward to see if this is really the last extent. */
5762 em = get_extent_skip_holes(inode, off, last_for_get_extent);
5768 flags |= FIEMAP_EXTENT_LAST;
5771 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
5781 ret = emit_last_fiemap_cache(fieinfo, &cache);
5782 free_extent_map(em);
5784 unlock_extent_cached(&inode->io_tree, start, start + len - 1,
5788 btrfs_free_path(path);
5790 ulist_free(tmp_ulist);
5794 static void __free_extent_buffer(struct extent_buffer *eb)
5796 kmem_cache_free(extent_buffer_cache, eb);
5799 int extent_buffer_under_io(const struct extent_buffer *eb)
5801 return (atomic_read(&eb->io_pages) ||
5802 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
5803 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5806 static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page)
5808 struct btrfs_subpage *subpage;
5810 lockdep_assert_held(&page->mapping->private_lock);
5812 if (PagePrivate(page)) {
5813 subpage = (struct btrfs_subpage *)page->private;
5814 if (atomic_read(&subpage->eb_refs))
5817 * Even there is no eb refs here, we may still have
5818 * end_page_read() call relying on page::private.
5820 if (atomic_read(&subpage->readers))
5826 static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
5828 struct btrfs_fs_info *fs_info = eb->fs_info;
5829 const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
5832 * For mapped eb, we're going to change the page private, which should
5833 * be done under the private_lock.
5836 spin_lock(&page->mapping->private_lock);
5838 if (!PagePrivate(page)) {
5840 spin_unlock(&page->mapping->private_lock);
5844 if (fs_info->nodesize >= PAGE_SIZE) {
5846 * We do this since we'll remove the pages after we've
5847 * removed the eb from the radix tree, so we could race
5848 * and have this page now attached to the new eb. So
5849 * only clear page_private if it's still connected to
5852 if (PagePrivate(page) &&
5853 page->private == (unsigned long)eb) {
5854 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5855 BUG_ON(PageDirty(page));
5856 BUG_ON(PageWriteback(page));
5858 * We need to make sure we haven't be attached
5861 detach_page_private(page);
5864 spin_unlock(&page->mapping->private_lock);
5869 * For subpage, we can have dummy eb with page private. In this case,
5870 * we can directly detach the private as such page is only attached to
5871 * one dummy eb, no sharing.
5874 btrfs_detach_subpage(fs_info, page);
5878 btrfs_page_dec_eb_refs(fs_info, page);
5881 * We can only detach the page private if there are no other ebs in the
5882 * page range and no unfinished IO.
5884 if (!page_range_has_eb(fs_info, page))
5885 btrfs_detach_subpage(fs_info, page);
5887 spin_unlock(&page->mapping->private_lock);
5890 /* Release all pages attached to the extent buffer */
5891 static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
5896 ASSERT(!extent_buffer_under_io(eb));
5898 num_pages = num_extent_pages(eb);
5899 for (i = 0; i < num_pages; i++) {
5900 struct page *page = eb->pages[i];
5905 detach_extent_buffer_page(eb, page);
5907 /* One for when we allocated the page */
5913 * Helper for releasing the extent buffer.
5915 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
5917 btrfs_release_extent_buffer_pages(eb);
5918 btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
5919 __free_extent_buffer(eb);
5922 static struct extent_buffer *
5923 __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
5926 struct extent_buffer *eb = NULL;
5928 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
5931 eb->fs_info = fs_info;
5933 init_rwsem(&eb->lock);
5935 btrfs_leak_debug_add(&fs_info->eb_leak_lock, &eb->leak_list,
5936 &fs_info->allocated_ebs);
5937 INIT_LIST_HEAD(&eb->release_list);
5939 spin_lock_init(&eb->refs_lock);
5940 atomic_set(&eb->refs, 1);
5941 atomic_set(&eb->io_pages, 0);
5943 ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE);
5948 struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
5951 struct extent_buffer *new;
5952 int num_pages = num_extent_pages(src);
5955 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
5960 * Set UNMAPPED before calling btrfs_release_extent_buffer(), as
5961 * btrfs_release_extent_buffer() have different behavior for
5962 * UNMAPPED subpage extent buffer.
5964 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
5966 memset(new->pages, 0, sizeof(*new->pages) * num_pages);
5967 ret = btrfs_alloc_page_array(num_pages, new->pages);
5969 btrfs_release_extent_buffer(new);
5973 for (i = 0; i < num_pages; i++) {
5975 struct page *p = new->pages[i];
5977 ret = attach_extent_buffer_page(new, p, NULL);
5979 btrfs_release_extent_buffer(new);
5982 WARN_ON(PageDirty(p));
5983 copy_page(page_address(p), page_address(src->pages[i]));
5985 set_extent_buffer_uptodate(new);
5990 struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
5991 u64 start, unsigned long len)
5993 struct extent_buffer *eb;
5998 eb = __alloc_extent_buffer(fs_info, start, len);
6002 num_pages = num_extent_pages(eb);
6003 ret = btrfs_alloc_page_array(num_pages, eb->pages);
6007 for (i = 0; i < num_pages; i++) {
6008 struct page *p = eb->pages[i];
6010 ret = attach_extent_buffer_page(eb, p, NULL);
6015 set_extent_buffer_uptodate(eb);
6016 btrfs_set_header_nritems(eb, 0);
6017 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
6021 for (i = 0; i < num_pages; i++) {
6023 detach_extent_buffer_page(eb, eb->pages[i]);
6024 __free_page(eb->pages[i]);
6027 __free_extent_buffer(eb);
6031 struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
6034 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
6037 static void check_buffer_tree_ref(struct extent_buffer *eb)
6041 * The TREE_REF bit is first set when the extent_buffer is added
6042 * to the radix tree. It is also reset, if unset, when a new reference
6043 * is created by find_extent_buffer.
6045 * It is only cleared in two cases: freeing the last non-tree
6046 * reference to the extent_buffer when its STALE bit is set or
6047 * calling release_folio when the tree reference is the only reference.
6049 * In both cases, care is taken to ensure that the extent_buffer's
6050 * pages are not under io. However, release_folio can be concurrently
6051 * called with creating new references, which is prone to race
6052 * conditions between the calls to check_buffer_tree_ref in those
6053 * codepaths and clearing TREE_REF in try_release_extent_buffer.
6055 * The actual lifetime of the extent_buffer in the radix tree is
6056 * adequately protected by the refcount, but the TREE_REF bit and
6057 * its corresponding reference are not. To protect against this
6058 * class of races, we call check_buffer_tree_ref from the codepaths
6059 * which trigger io after they set eb->io_pages. Note that once io is
6060 * initiated, TREE_REF can no longer be cleared, so that is the
6061 * moment at which any such race is best fixed.
6063 refs = atomic_read(&eb->refs);
6064 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6067 spin_lock(&eb->refs_lock);
6068 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6069 atomic_inc(&eb->refs);
6070 spin_unlock(&eb->refs_lock);
6073 static void mark_extent_buffer_accessed(struct extent_buffer *eb,
6074 struct page *accessed)
6078 check_buffer_tree_ref(eb);
6080 num_pages = num_extent_pages(eb);
6081 for (i = 0; i < num_pages; i++) {
6082 struct page *p = eb->pages[i];
6085 mark_page_accessed(p);
6089 struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
6092 struct extent_buffer *eb;
6094 eb = find_extent_buffer_nolock(fs_info, start);
6098 * Lock our eb's refs_lock to avoid races with free_extent_buffer().
6099 * When we get our eb it might be flagged with EXTENT_BUFFER_STALE and
6100 * another task running free_extent_buffer() might have seen that flag
6101 * set, eb->refs == 2, that the buffer isn't under IO (dirty and
6102 * writeback flags not set) and it's still in the tree (flag
6103 * EXTENT_BUFFER_TREE_REF set), therefore being in the process of
6104 * decrementing the extent buffer's reference count twice. So here we
6105 * could race and increment the eb's reference count, clear its stale
6106 * flag, mark it as dirty and drop our reference before the other task
6107 * finishes executing free_extent_buffer, which would later result in
6108 * an attempt to free an extent buffer that is dirty.
6110 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
6111 spin_lock(&eb->refs_lock);
6112 spin_unlock(&eb->refs_lock);
6114 mark_extent_buffer_accessed(eb, NULL);
6118 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
6119 struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
6122 struct extent_buffer *eb, *exists = NULL;
6125 eb = find_extent_buffer(fs_info, start);
6128 eb = alloc_dummy_extent_buffer(fs_info, start);
6130 return ERR_PTR(-ENOMEM);
6131 eb->fs_info = fs_info;
6134 ret = xa_insert(&fs_info->extent_buffers,
6135 start >> fs_info->sectorsize_bits,
6137 if (ret == -ENOMEM) {
6138 exists = ERR_PTR(ret);
6141 if (ret == -EBUSY) {
6142 exists = find_extent_buffer(fs_info, start);
6148 check_buffer_tree_ref(eb);
6149 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
6153 btrfs_release_extent_buffer(eb);
6158 static struct extent_buffer *grab_extent_buffer(
6159 struct btrfs_fs_info *fs_info, struct page *page)
6161 struct extent_buffer *exists;
6164 * For subpage case, we completely rely on radix tree to ensure we
6165 * don't try to insert two ebs for the same bytenr. So here we always
6166 * return NULL and just continue.
6168 if (fs_info->nodesize < PAGE_SIZE)
6171 /* Page not yet attached to an extent buffer */
6172 if (!PagePrivate(page))
6176 * We could have already allocated an eb for this page and attached one
6177 * so lets see if we can get a ref on the existing eb, and if we can we
6178 * know it's good and we can just return that one, else we know we can
6179 * just overwrite page->private.
6181 exists = (struct extent_buffer *)page->private;
6182 if (atomic_inc_not_zero(&exists->refs))
6185 WARN_ON(PageDirty(page));
6186 detach_page_private(page);
6190 static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start)
6192 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
6193 btrfs_err(fs_info, "bad tree block start %llu", start);
6197 if (fs_info->nodesize < PAGE_SIZE &&
6198 offset_in_page(start) + fs_info->nodesize > PAGE_SIZE) {
6200 "tree block crosses page boundary, start %llu nodesize %u",
6201 start, fs_info->nodesize);
6204 if (fs_info->nodesize >= PAGE_SIZE &&
6205 !IS_ALIGNED(start, PAGE_SIZE)) {
6207 "tree block is not page aligned, start %llu nodesize %u",
6208 start, fs_info->nodesize);
6214 struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
6215 u64 start, u64 owner_root, int level)
6217 unsigned long len = fs_info->nodesize;
6220 unsigned long index = start >> PAGE_SHIFT;
6221 struct extent_buffer *eb;
6222 struct extent_buffer *exists = NULL;
6224 struct address_space *mapping = fs_info->btree_inode->i_mapping;
6228 if (check_eb_alignment(fs_info, start))
6229 return ERR_PTR(-EINVAL);
6231 #if BITS_PER_LONG == 32
6232 if (start >= MAX_LFS_FILESIZE) {
6233 btrfs_err_rl(fs_info,
6234 "extent buffer %llu is beyond 32bit page cache limit", start);
6235 btrfs_err_32bit_limit(fs_info);
6236 return ERR_PTR(-EOVERFLOW);
6238 if (start >= BTRFS_32BIT_EARLY_WARN_THRESHOLD)
6239 btrfs_warn_32bit_limit(fs_info);
6242 eb = find_extent_buffer(fs_info, start);
6246 eb = __alloc_extent_buffer(fs_info, start, len);
6248 return ERR_PTR(-ENOMEM);
6249 btrfs_set_buffer_lockdep_class(owner_root, eb, level);
6251 num_pages = num_extent_pages(eb);
6252 for (i = 0; i < num_pages; i++, index++) {
6253 struct btrfs_subpage *prealloc = NULL;
6255 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
6257 exists = ERR_PTR(-ENOMEM);
6262 * Preallocate page->private for subpage case, so that we won't
6263 * allocate memory with private_lock hold. The memory will be
6264 * freed by attach_extent_buffer_page() or freed manually if
6267 * Although we have ensured one subpage eb can only have one
6268 * page, but it may change in the future for 16K page size
6269 * support, so we still preallocate the memory in the loop.
6271 if (fs_info->nodesize < PAGE_SIZE) {
6272 prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA);
6273 if (IS_ERR(prealloc)) {
6274 ret = PTR_ERR(prealloc);
6277 exists = ERR_PTR(ret);
6282 spin_lock(&mapping->private_lock);
6283 exists = grab_extent_buffer(fs_info, p);
6285 spin_unlock(&mapping->private_lock);
6288 mark_extent_buffer_accessed(exists, p);
6289 btrfs_free_subpage(prealloc);
6292 /* Should not fail, as we have preallocated the memory */
6293 ret = attach_extent_buffer_page(eb, p, prealloc);
6296 * To inform we have extra eb under allocation, so that
6297 * detach_extent_buffer_page() won't release the page private
6298 * when the eb hasn't yet been inserted into radix tree.
6300 * The ref will be decreased when the eb released the page, in
6301 * detach_extent_buffer_page().
6302 * Thus needs no special handling in error path.
6304 btrfs_page_inc_eb_refs(fs_info, p);
6305 spin_unlock(&mapping->private_lock);
6307 WARN_ON(btrfs_page_test_dirty(fs_info, p, eb->start, eb->len));
6309 if (!PageUptodate(p))
6313 * We can't unlock the pages just yet since the extent buffer
6314 * hasn't been properly inserted in the radix tree, this
6315 * opens a race with btree_release_folio which can free a page
6316 * while we are still filling in all pages for the buffer and
6321 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6324 ret = xa_insert(&fs_info->extent_buffers,
6325 start >> fs_info->sectorsize_bits,
6327 if (ret == -ENOMEM) {
6328 exists = ERR_PTR(ret);
6331 if (ret == -EBUSY) {
6332 exists = find_extent_buffer(fs_info, start);
6338 /* add one reference for the tree */
6339 check_buffer_tree_ref(eb);
6340 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
6343 * Now it's safe to unlock the pages because any calls to
6344 * btree_release_folio will correctly detect that a page belongs to a
6345 * live buffer and won't free them prematurely.
6347 for (i = 0; i < num_pages; i++)
6348 unlock_page(eb->pages[i]);
6352 WARN_ON(!atomic_dec_and_test(&eb->refs));
6353 for (i = 0; i < num_pages; i++) {
6355 unlock_page(eb->pages[i]);
6358 btrfs_release_extent_buffer(eb);
6362 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
6364 struct extent_buffer *eb =
6365 container_of(head, struct extent_buffer, rcu_head);
6367 __free_extent_buffer(eb);
6370 static int release_extent_buffer(struct extent_buffer *eb)
6371 __releases(&eb->refs_lock)
6373 lockdep_assert_held(&eb->refs_lock);
6375 WARN_ON(atomic_read(&eb->refs) == 0);
6376 if (atomic_dec_and_test(&eb->refs)) {
6377 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
6378 struct btrfs_fs_info *fs_info = eb->fs_info;
6380 spin_unlock(&eb->refs_lock);
6382 xa_erase(&fs_info->extent_buffers,
6383 eb->start >> fs_info->sectorsize_bits);
6385 spin_unlock(&eb->refs_lock);
6388 btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
6389 /* Should be safe to release our pages at this point */
6390 btrfs_release_extent_buffer_pages(eb);
6391 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
6392 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
6393 __free_extent_buffer(eb);
6397 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
6400 spin_unlock(&eb->refs_lock);
6405 void free_extent_buffer(struct extent_buffer *eb)
6413 refs = atomic_read(&eb->refs);
6414 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
6415 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
6418 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
6423 spin_lock(&eb->refs_lock);
6424 if (atomic_read(&eb->refs) == 2 &&
6425 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
6426 !extent_buffer_under_io(eb) &&
6427 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6428 atomic_dec(&eb->refs);
6431 * I know this is terrible, but it's temporary until we stop tracking
6432 * the uptodate bits and such for the extent buffers.
6434 release_extent_buffer(eb);
6437 void free_extent_buffer_stale(struct extent_buffer *eb)
6442 spin_lock(&eb->refs_lock);
6443 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
6445 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
6446 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6447 atomic_dec(&eb->refs);
6448 release_extent_buffer(eb);
6451 static void btree_clear_page_dirty(struct page *page)
6453 ASSERT(PageDirty(page));
6454 ASSERT(PageLocked(page));
6455 clear_page_dirty_for_io(page);
6456 xa_lock_irq(&page->mapping->i_pages);
6457 if (!PageDirty(page))
6458 __xa_clear_mark(&page->mapping->i_pages,
6459 page_index(page), PAGECACHE_TAG_DIRTY);
6460 xa_unlock_irq(&page->mapping->i_pages);
6463 static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb)
6465 struct btrfs_fs_info *fs_info = eb->fs_info;
6466 struct page *page = eb->pages[0];
6469 /* btree_clear_page_dirty() needs page locked */
6471 last = btrfs_subpage_clear_and_test_dirty(fs_info, page, eb->start,
6474 btree_clear_page_dirty(page);
6476 WARN_ON(atomic_read(&eb->refs) == 0);
6479 void clear_extent_buffer_dirty(const struct extent_buffer *eb)
6485 if (eb->fs_info->nodesize < PAGE_SIZE)
6486 return clear_subpage_extent_buffer_dirty(eb);
6488 num_pages = num_extent_pages(eb);
6490 for (i = 0; i < num_pages; i++) {
6491 page = eb->pages[i];
6492 if (!PageDirty(page))
6495 btree_clear_page_dirty(page);
6496 ClearPageError(page);
6499 WARN_ON(atomic_read(&eb->refs) == 0);
6502 bool set_extent_buffer_dirty(struct extent_buffer *eb)
6508 check_buffer_tree_ref(eb);
6510 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
6512 num_pages = num_extent_pages(eb);
6513 WARN_ON(atomic_read(&eb->refs) == 0);
6514 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
6517 bool subpage = eb->fs_info->nodesize < PAGE_SIZE;
6520 * For subpage case, we can have other extent buffers in the
6521 * same page, and in clear_subpage_extent_buffer_dirty() we
6522 * have to clear page dirty without subpage lock held.
6523 * This can cause race where our page gets dirty cleared after
6526 * Thankfully, clear_subpage_extent_buffer_dirty() has locked
6527 * its page for other reasons, we can use page lock to prevent
6531 lock_page(eb->pages[0]);
6532 for (i = 0; i < num_pages; i++)
6533 btrfs_page_set_dirty(eb->fs_info, eb->pages[i],
6534 eb->start, eb->len);
6536 unlock_page(eb->pages[0]);
6538 #ifdef CONFIG_BTRFS_DEBUG
6539 for (i = 0; i < num_pages; i++)
6540 ASSERT(PageDirty(eb->pages[i]));
6546 void clear_extent_buffer_uptodate(struct extent_buffer *eb)
6548 struct btrfs_fs_info *fs_info = eb->fs_info;
6553 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6554 num_pages = num_extent_pages(eb);
6555 for (i = 0; i < num_pages; i++) {
6556 page = eb->pages[i];
6561 * This is special handling for metadata subpage, as regular
6562 * btrfs_is_subpage() can not handle cloned/dummy metadata.
6564 if (fs_info->nodesize >= PAGE_SIZE)
6565 ClearPageUptodate(page);
6567 btrfs_subpage_clear_uptodate(fs_info, page, eb->start,
6572 void set_extent_buffer_uptodate(struct extent_buffer *eb)
6574 struct btrfs_fs_info *fs_info = eb->fs_info;
6579 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6580 num_pages = num_extent_pages(eb);
6581 for (i = 0; i < num_pages; i++) {
6582 page = eb->pages[i];
6585 * This is special handling for metadata subpage, as regular
6586 * btrfs_is_subpage() can not handle cloned/dummy metadata.
6588 if (fs_info->nodesize >= PAGE_SIZE)
6589 SetPageUptodate(page);
6591 btrfs_subpage_set_uptodate(fs_info, page, eb->start,
6596 static int read_extent_buffer_subpage(struct extent_buffer *eb, int wait,
6599 struct btrfs_fs_info *fs_info = eb->fs_info;
6600 struct extent_io_tree *io_tree;
6601 struct page *page = eb->pages[0];
6602 struct btrfs_bio_ctrl bio_ctrl = { 0 };
6605 ASSERT(!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags));
6606 ASSERT(PagePrivate(page));
6607 io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
6609 if (wait == WAIT_NONE) {
6610 if (!try_lock_extent(io_tree, eb->start, eb->start + eb->len - 1))
6613 ret = lock_extent(io_tree, eb->start, eb->start + eb->len - 1);
6619 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags) ||
6620 PageUptodate(page) ||
6621 btrfs_subpage_test_uptodate(fs_info, page, eb->start, eb->len)) {
6622 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6623 unlock_extent(io_tree, eb->start, eb->start + eb->len - 1);
6627 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
6628 eb->read_mirror = 0;
6629 atomic_set(&eb->io_pages, 1);
6630 check_buffer_tree_ref(eb);
6631 btrfs_subpage_clear_error(fs_info, page, eb->start, eb->len);
6633 btrfs_subpage_start_reader(fs_info, page, eb->start, eb->len);
6634 ret = submit_extent_page(REQ_OP_READ | REQ_META, NULL, &bio_ctrl,
6635 page, eb->start, eb->len,
6636 eb->start - page_offset(page),
6637 end_bio_extent_readpage, mirror_num, 0,
6641 * In the endio function, if we hit something wrong we will
6642 * increase the io_pages, so here we need to decrease it for
6645 atomic_dec(&eb->io_pages);
6648 submit_one_bio(bio_ctrl.bio, mirror_num, 0);
6649 bio_ctrl.bio = NULL;
6651 if (ret || wait != WAIT_COMPLETE)
6654 wait_extent_bit(io_tree, eb->start, eb->start + eb->len - 1, EXTENT_LOCKED);
6655 if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
6660 int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
6666 int locked_pages = 0;
6667 int all_uptodate = 1;
6669 unsigned long num_reads = 0;
6670 struct btrfs_bio_ctrl bio_ctrl = { 0 };
6672 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
6676 * We could have had EXTENT_BUFFER_UPTODATE cleared by the write
6677 * operation, which could potentially still be in flight. In this case
6678 * we simply want to return an error.
6680 if (unlikely(test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)))
6683 if (eb->fs_info->nodesize < PAGE_SIZE)
6684 return read_extent_buffer_subpage(eb, wait, mirror_num);
6686 num_pages = num_extent_pages(eb);
6687 for (i = 0; i < num_pages; i++) {
6688 page = eb->pages[i];
6689 if (wait == WAIT_NONE) {
6691 * WAIT_NONE is only utilized by readahead. If we can't
6692 * acquire the lock atomically it means either the eb
6693 * is being read out or under modification.
6694 * Either way the eb will be or has been cached,
6695 * readahead can exit safely.
6697 if (!trylock_page(page))
6705 * We need to firstly lock all pages to make sure that
6706 * the uptodate bit of our pages won't be affected by
6707 * clear_extent_buffer_uptodate().
6709 for (i = 0; i < num_pages; i++) {
6710 page = eb->pages[i];
6711 if (!PageUptodate(page)) {
6718 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6722 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
6723 eb->read_mirror = 0;
6724 atomic_set(&eb->io_pages, num_reads);
6726 * It is possible for release_folio to clear the TREE_REF bit before we
6727 * set io_pages. See check_buffer_tree_ref for a more detailed comment.
6729 check_buffer_tree_ref(eb);
6730 for (i = 0; i < num_pages; i++) {
6731 page = eb->pages[i];
6733 if (!PageUptodate(page)) {
6735 atomic_dec(&eb->io_pages);
6740 ClearPageError(page);
6741 err = submit_extent_page(REQ_OP_READ | REQ_META, NULL,
6742 &bio_ctrl, page, page_offset(page),
6743 PAGE_SIZE, 0, end_bio_extent_readpage,
6744 mirror_num, 0, false);
6747 * We failed to submit the bio so it's the
6748 * caller's responsibility to perform cleanup
6749 * i.e unlock page/set error bit.
6754 atomic_dec(&eb->io_pages);
6762 submit_one_bio(bio_ctrl.bio, mirror_num, bio_ctrl.compress_type);
6763 bio_ctrl.bio = NULL;
6766 if (ret || wait != WAIT_COMPLETE)
6769 for (i = 0; i < num_pages; i++) {
6770 page = eb->pages[i];
6771 wait_on_page_locked(page);
6772 if (!PageUptodate(page))
6779 while (locked_pages > 0) {
6781 page = eb->pages[locked_pages];
6787 static bool report_eb_range(const struct extent_buffer *eb, unsigned long start,
6790 btrfs_warn(eb->fs_info,
6791 "access to eb bytenr %llu len %lu out of range start %lu len %lu",
6792 eb->start, eb->len, start, len);
6793 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
6799 * Check if the [start, start + len) range is valid before reading/writing
6801 * NOTE: @start and @len are offset inside the eb, not logical address.
6803 * Caller should not touch the dst/src memory if this function returns error.
6805 static inline int check_eb_range(const struct extent_buffer *eb,
6806 unsigned long start, unsigned long len)
6808 unsigned long offset;
6810 /* start, start + len should not go beyond eb->len nor overflow */
6811 if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len))
6812 return report_eb_range(eb, start, len);
6817 void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
6818 unsigned long start, unsigned long len)
6824 char *dst = (char *)dstv;
6825 unsigned long i = get_eb_page_index(start);
6827 if (check_eb_range(eb, start, len))
6830 offset = get_eb_offset_in_page(eb, start);
6833 page = eb->pages[i];
6835 cur = min(len, (PAGE_SIZE - offset));
6836 kaddr = page_address(page);
6837 memcpy(dst, kaddr + offset, cur);
6846 int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
6848 unsigned long start, unsigned long len)
6854 char __user *dst = (char __user *)dstv;
6855 unsigned long i = get_eb_page_index(start);
6858 WARN_ON(start > eb->len);
6859 WARN_ON(start + len > eb->start + eb->len);
6861 offset = get_eb_offset_in_page(eb, start);
6864 page = eb->pages[i];
6866 cur = min(len, (PAGE_SIZE - offset));
6867 kaddr = page_address(page);
6868 if (copy_to_user_nofault(dst, kaddr + offset, cur)) {
6882 int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
6883 unsigned long start, unsigned long len)
6889 char *ptr = (char *)ptrv;
6890 unsigned long i = get_eb_page_index(start);
6893 if (check_eb_range(eb, start, len))
6896 offset = get_eb_offset_in_page(eb, start);
6899 page = eb->pages[i];
6901 cur = min(len, (PAGE_SIZE - offset));
6903 kaddr = page_address(page);
6904 ret = memcmp(ptr, kaddr + offset, cur);
6917 * Check that the extent buffer is uptodate.
6919 * For regular sector size == PAGE_SIZE case, check if @page is uptodate.
6920 * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE.
6922 static void assert_eb_page_uptodate(const struct extent_buffer *eb,
6925 struct btrfs_fs_info *fs_info = eb->fs_info;
6928 * If we are using the commit root we could potentially clear a page
6929 * Uptodate while we're using the extent buffer that we've previously
6930 * looked up. We don't want to complain in this case, as the page was
6931 * valid before, we just didn't write it out. Instead we want to catch
6932 * the case where we didn't actually read the block properly, which
6933 * would have !PageUptodate && !PageError, as we clear PageError before
6936 if (fs_info->nodesize < PAGE_SIZE) {
6937 bool uptodate, error;
6939 uptodate = btrfs_subpage_test_uptodate(fs_info, page,
6940 eb->start, eb->len);
6941 error = btrfs_subpage_test_error(fs_info, page, eb->start, eb->len);
6942 WARN_ON(!uptodate && !error);
6944 WARN_ON(!PageUptodate(page) && !PageError(page));
6948 void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb,
6953 assert_eb_page_uptodate(eb, eb->pages[0]);
6954 kaddr = page_address(eb->pages[0]) +
6955 get_eb_offset_in_page(eb, offsetof(struct btrfs_header,
6957 memcpy(kaddr, srcv, BTRFS_FSID_SIZE);
6960 void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *srcv)
6964 assert_eb_page_uptodate(eb, eb->pages[0]);
6965 kaddr = page_address(eb->pages[0]) +
6966 get_eb_offset_in_page(eb, offsetof(struct btrfs_header, fsid));
6967 memcpy(kaddr, srcv, BTRFS_FSID_SIZE);
6970 void write_extent_buffer(const struct extent_buffer *eb, const void *srcv,
6971 unsigned long start, unsigned long len)
6977 char *src = (char *)srcv;
6978 unsigned long i = get_eb_page_index(start);
6980 WARN_ON(test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags));
6982 if (check_eb_range(eb, start, len))
6985 offset = get_eb_offset_in_page(eb, start);
6988 page = eb->pages[i];
6989 assert_eb_page_uptodate(eb, page);
6991 cur = min(len, PAGE_SIZE - offset);
6992 kaddr = page_address(page);
6993 memcpy(kaddr + offset, src, cur);
7002 void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
7009 unsigned long i = get_eb_page_index(start);
7011 if (check_eb_range(eb, start, len))
7014 offset = get_eb_offset_in_page(eb, start);
7017 page = eb->pages[i];
7018 assert_eb_page_uptodate(eb, page);
7020 cur = min(len, PAGE_SIZE - offset);
7021 kaddr = page_address(page);
7022 memset(kaddr + offset, 0, cur);
7030 void copy_extent_buffer_full(const struct extent_buffer *dst,
7031 const struct extent_buffer *src)
7036 ASSERT(dst->len == src->len);
7038 if (dst->fs_info->nodesize >= PAGE_SIZE) {
7039 num_pages = num_extent_pages(dst);
7040 for (i = 0; i < num_pages; i++)
7041 copy_page(page_address(dst->pages[i]),
7042 page_address(src->pages[i]));
7044 size_t src_offset = get_eb_offset_in_page(src, 0);
7045 size_t dst_offset = get_eb_offset_in_page(dst, 0);
7047 ASSERT(src->fs_info->nodesize < PAGE_SIZE);
7048 memcpy(page_address(dst->pages[0]) + dst_offset,
7049 page_address(src->pages[0]) + src_offset,
7054 void copy_extent_buffer(const struct extent_buffer *dst,
7055 const struct extent_buffer *src,
7056 unsigned long dst_offset, unsigned long src_offset,
7059 u64 dst_len = dst->len;
7064 unsigned long i = get_eb_page_index(dst_offset);
7066 if (check_eb_range(dst, dst_offset, len) ||
7067 check_eb_range(src, src_offset, len))
7070 WARN_ON(src->len != dst_len);
7072 offset = get_eb_offset_in_page(dst, dst_offset);
7075 page = dst->pages[i];
7076 assert_eb_page_uptodate(dst, page);
7078 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
7080 kaddr = page_address(page);
7081 read_extent_buffer(src, kaddr + offset, src_offset, cur);
7091 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
7093 * @eb: the extent buffer
7094 * @start: offset of the bitmap item in the extent buffer
7096 * @page_index: return index of the page in the extent buffer that contains the
7098 * @page_offset: return offset into the page given by page_index
7100 * This helper hides the ugliness of finding the byte in an extent buffer which
7101 * contains a given bit.
7103 static inline void eb_bitmap_offset(const struct extent_buffer *eb,
7104 unsigned long start, unsigned long nr,
7105 unsigned long *page_index,
7106 size_t *page_offset)
7108 size_t byte_offset = BIT_BYTE(nr);
7112 * The byte we want is the offset of the extent buffer + the offset of
7113 * the bitmap item in the extent buffer + the offset of the byte in the
7116 offset = start + offset_in_page(eb->start) + byte_offset;
7118 *page_index = offset >> PAGE_SHIFT;
7119 *page_offset = offset_in_page(offset);
7123 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
7124 * @eb: the extent buffer
7125 * @start: offset of the bitmap item in the extent buffer
7126 * @nr: bit number to test
7128 int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
7136 eb_bitmap_offset(eb, start, nr, &i, &offset);
7137 page = eb->pages[i];
7138 assert_eb_page_uptodate(eb, page);
7139 kaddr = page_address(page);
7140 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
7144 * extent_buffer_bitmap_set - set an area of a bitmap
7145 * @eb: the extent buffer
7146 * @start: offset of the bitmap item in the extent buffer
7147 * @pos: bit number of the first bit
7148 * @len: number of bits to set
7150 void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
7151 unsigned long pos, unsigned long len)
7157 const unsigned int size = pos + len;
7158 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
7159 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
7161 eb_bitmap_offset(eb, start, pos, &i, &offset);
7162 page = eb->pages[i];
7163 assert_eb_page_uptodate(eb, page);
7164 kaddr = page_address(page);
7166 while (len >= bits_to_set) {
7167 kaddr[offset] |= mask_to_set;
7169 bits_to_set = BITS_PER_BYTE;
7171 if (++offset >= PAGE_SIZE && len > 0) {
7173 page = eb->pages[++i];
7174 assert_eb_page_uptodate(eb, page);
7175 kaddr = page_address(page);
7179 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
7180 kaddr[offset] |= mask_to_set;
7186 * extent_buffer_bitmap_clear - clear an area of a bitmap
7187 * @eb: the extent buffer
7188 * @start: offset of the bitmap item in the extent buffer
7189 * @pos: bit number of the first bit
7190 * @len: number of bits to clear
7192 void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
7193 unsigned long start, unsigned long pos,
7200 const unsigned int size = pos + len;
7201 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
7202 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
7204 eb_bitmap_offset(eb, start, pos, &i, &offset);
7205 page = eb->pages[i];
7206 assert_eb_page_uptodate(eb, page);
7207 kaddr = page_address(page);
7209 while (len >= bits_to_clear) {
7210 kaddr[offset] &= ~mask_to_clear;
7211 len -= bits_to_clear;
7212 bits_to_clear = BITS_PER_BYTE;
7214 if (++offset >= PAGE_SIZE && len > 0) {
7216 page = eb->pages[++i];
7217 assert_eb_page_uptodate(eb, page);
7218 kaddr = page_address(page);
7222 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
7223 kaddr[offset] &= ~mask_to_clear;
7227 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
7229 unsigned long distance = (src > dst) ? src - dst : dst - src;
7230 return distance < len;
7233 static void copy_pages(struct page *dst_page, struct page *src_page,
7234 unsigned long dst_off, unsigned long src_off,
7237 char *dst_kaddr = page_address(dst_page);
7239 int must_memmove = 0;
7241 if (dst_page != src_page) {
7242 src_kaddr = page_address(src_page);
7244 src_kaddr = dst_kaddr;
7245 if (areas_overlap(src_off, dst_off, len))
7250 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
7252 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
7255 void memcpy_extent_buffer(const struct extent_buffer *dst,
7256 unsigned long dst_offset, unsigned long src_offset,
7260 size_t dst_off_in_page;
7261 size_t src_off_in_page;
7262 unsigned long dst_i;
7263 unsigned long src_i;
7265 if (check_eb_range(dst, dst_offset, len) ||
7266 check_eb_range(dst, src_offset, len))
7270 dst_off_in_page = get_eb_offset_in_page(dst, dst_offset);
7271 src_off_in_page = get_eb_offset_in_page(dst, src_offset);
7273 dst_i = get_eb_page_index(dst_offset);
7274 src_i = get_eb_page_index(src_offset);
7276 cur = min(len, (unsigned long)(PAGE_SIZE -
7278 cur = min_t(unsigned long, cur,
7279 (unsigned long)(PAGE_SIZE - dst_off_in_page));
7281 copy_pages(dst->pages[dst_i], dst->pages[src_i],
7282 dst_off_in_page, src_off_in_page, cur);
7290 void memmove_extent_buffer(const struct extent_buffer *dst,
7291 unsigned long dst_offset, unsigned long src_offset,
7295 size_t dst_off_in_page;
7296 size_t src_off_in_page;
7297 unsigned long dst_end = dst_offset + len - 1;
7298 unsigned long src_end = src_offset + len - 1;
7299 unsigned long dst_i;
7300 unsigned long src_i;
7302 if (check_eb_range(dst, dst_offset, len) ||
7303 check_eb_range(dst, src_offset, len))
7305 if (dst_offset < src_offset) {
7306 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
7310 dst_i = get_eb_page_index(dst_end);
7311 src_i = get_eb_page_index(src_end);
7313 dst_off_in_page = get_eb_offset_in_page(dst, dst_end);
7314 src_off_in_page = get_eb_offset_in_page(dst, src_end);
7316 cur = min_t(unsigned long, len, src_off_in_page + 1);
7317 cur = min(cur, dst_off_in_page + 1);
7318 copy_pages(dst->pages[dst_i], dst->pages[src_i],
7319 dst_off_in_page - cur + 1,
7320 src_off_in_page - cur + 1, cur);
7328 static struct extent_buffer *get_next_extent_buffer(
7329 struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
7331 struct extent_buffer *eb;
7332 unsigned long index;
7333 u64 page_start = page_offset(page);
7335 ASSERT(in_range(bytenr, page_start, PAGE_SIZE));
7336 lockdep_assert_held(&fs_info->buffer_lock);
7338 xa_for_each_start(&fs_info->extent_buffers, index, eb,
7339 page_start >> fs_info->sectorsize_bits) {
7340 if (in_range(eb->start, page_start, PAGE_SIZE))
7342 else if (eb->start >= page_start + PAGE_SIZE)
7343 /* Already beyond page end */
7349 static int try_release_subpage_extent_buffer(struct page *page)
7351 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
7352 u64 cur = page_offset(page);
7353 const u64 end = page_offset(page) + PAGE_SIZE;
7357 struct extent_buffer *eb = NULL;
7360 * Unlike try_release_extent_buffer() which uses page->private
7361 * to grab buffer, for subpage case we rely on radix tree, thus
7362 * we need to ensure radix tree consistency.
7364 * We also want an atomic snapshot of the radix tree, thus go
7365 * with spinlock rather than RCU.
7367 spin_lock(&fs_info->buffer_lock);
7368 eb = get_next_extent_buffer(fs_info, page, cur);
7370 /* No more eb in the page range after or at cur */
7371 spin_unlock(&fs_info->buffer_lock);
7374 cur = eb->start + eb->len;
7377 * The same as try_release_extent_buffer(), to ensure the eb
7378 * won't disappear out from under us.
7380 spin_lock(&eb->refs_lock);
7381 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
7382 spin_unlock(&eb->refs_lock);
7383 spin_unlock(&fs_info->buffer_lock);
7386 spin_unlock(&fs_info->buffer_lock);
7389 * If tree ref isn't set then we know the ref on this eb is a
7390 * real ref, so just return, this eb will likely be freed soon
7393 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
7394 spin_unlock(&eb->refs_lock);
7399 * Here we don't care about the return value, we will always
7400 * check the page private at the end. And
7401 * release_extent_buffer() will release the refs_lock.
7403 release_extent_buffer(eb);
7406 * Finally to check if we have cleared page private, as if we have
7407 * released all ebs in the page, the page private should be cleared now.
7409 spin_lock(&page->mapping->private_lock);
7410 if (!PagePrivate(page))
7414 spin_unlock(&page->mapping->private_lock);
7419 int try_release_extent_buffer(struct page *page)
7421 struct extent_buffer *eb;
7423 if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE)
7424 return try_release_subpage_extent_buffer(page);
7427 * We need to make sure nobody is changing page->private, as we rely on
7428 * page->private as the pointer to extent buffer.
7430 spin_lock(&page->mapping->private_lock);
7431 if (!PagePrivate(page)) {
7432 spin_unlock(&page->mapping->private_lock);
7436 eb = (struct extent_buffer *)page->private;
7440 * This is a little awful but should be ok, we need to make sure that
7441 * the eb doesn't disappear out from under us while we're looking at
7444 spin_lock(&eb->refs_lock);
7445 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
7446 spin_unlock(&eb->refs_lock);
7447 spin_unlock(&page->mapping->private_lock);
7450 spin_unlock(&page->mapping->private_lock);
7453 * If tree ref isn't set then we know the ref on this eb is a real ref,
7454 * so just return, this page will likely be freed soon anyway.
7456 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
7457 spin_unlock(&eb->refs_lock);
7461 return release_extent_buffer(eb);
7465 * btrfs_readahead_tree_block - attempt to readahead a child block
7466 * @fs_info: the fs_info
7467 * @bytenr: bytenr to read
7468 * @owner_root: objectid of the root that owns this eb
7469 * @gen: generation for the uptodate check, can be 0
7470 * @level: level for the eb
7472 * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a
7473 * normal uptodate check of the eb, without checking the generation. If we have
7474 * to read the block we will not block on anything.
7476 void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
7477 u64 bytenr, u64 owner_root, u64 gen, int level)
7479 struct extent_buffer *eb;
7482 eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level);
7486 if (btrfs_buffer_uptodate(eb, gen, 1)) {
7487 free_extent_buffer(eb);
7491 ret = read_extent_buffer_pages(eb, WAIT_NONE, 0);
7493 free_extent_buffer_stale(eb);
7495 free_extent_buffer(eb);
7499 * btrfs_readahead_node_child - readahead a node's child block
7500 * @node: parent node we're reading from
7501 * @slot: slot in the parent node for the child we want to read
7503 * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at
7504 * the slot in the node provided.
7506 void btrfs_readahead_node_child(struct extent_buffer *node, int slot)
7508 btrfs_readahead_tree_block(node->fs_info,
7509 btrfs_node_blockptr(node, slot),
7510 btrfs_header_owner(node),
7511 btrfs_node_ptr_generation(node, slot),
7512 btrfs_header_level(node) - 1);