1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitops.h>
4 #include <linux/slab.h>
7 #include <linux/pagemap.h>
8 #include <linux/page-flags.h>
9 #include <linux/spinlock.h>
10 #include <linux/blkdev.h>
11 #include <linux/swap.h>
12 #include <linux/writeback.h>
13 #include <linux/pagevec.h>
14 #include <linux/prefetch.h>
15 #include <linux/cleancache.h>
16 #include "extent_io.h"
17 #include "extent_map.h"
19 #include "btrfs_inode.h"
21 #include "check-integrity.h"
23 #include "rcu-string.h"
27 static struct kmem_cache *extent_state_cache;
28 static struct kmem_cache *extent_buffer_cache;
29 static struct bio_set btrfs_bioset;
31 static inline bool extent_state_in_tree(const struct extent_state *state)
33 return !RB_EMPTY_NODE(&state->rb_node);
36 #ifdef CONFIG_BTRFS_DEBUG
37 static LIST_HEAD(buffers);
38 static LIST_HEAD(states);
40 static DEFINE_SPINLOCK(leak_lock);
43 void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
47 spin_lock_irqsave(&leak_lock, flags);
49 spin_unlock_irqrestore(&leak_lock, flags);
53 void btrfs_leak_debug_del(struct list_head *entry)
57 spin_lock_irqsave(&leak_lock, flags);
59 spin_unlock_irqrestore(&leak_lock, flags);
63 void btrfs_leak_debug_check(void)
65 struct extent_state *state;
66 struct extent_buffer *eb;
68 while (!list_empty(&states)) {
69 state = list_entry(states.next, struct extent_state, leak_list);
70 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
71 state->start, state->end, state->state,
72 extent_state_in_tree(state),
73 refcount_read(&state->refs));
74 list_del(&state->leak_list);
75 kmem_cache_free(extent_state_cache, state);
78 while (!list_empty(&buffers)) {
79 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
80 pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n",
81 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags);
82 list_del(&eb->leak_list);
83 kmem_cache_free(extent_buffer_cache, eb);
87 #define btrfs_debug_check_extent_io_range(tree, start, end) \
88 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
89 static inline void __btrfs_debug_check_extent_io_range(const char *caller,
90 struct extent_io_tree *tree, u64 start, u64 end)
92 struct inode *inode = tree->private_data;
95 if (!inode || !is_data_inode(inode))
98 isize = i_size_read(inode);
99 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
100 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
101 "%s: ino %llu isize %llu odd range [%llu,%llu]",
102 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
106 #define btrfs_leak_debug_add(new, head) do {} while (0)
107 #define btrfs_leak_debug_del(entry) do {} while (0)
108 #define btrfs_leak_debug_check() do {} while (0)
109 #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
115 struct rb_node rb_node;
118 struct extent_page_data {
120 struct extent_io_tree *tree;
121 /* tells writepage not to lock the state bits for this range
122 * it still does the unlocking
124 unsigned int extent_locked:1;
126 /* tells the submit_bio code to use REQ_SYNC */
127 unsigned int sync_io:1;
130 static int add_extent_changeset(struct extent_state *state, unsigned bits,
131 struct extent_changeset *changeset,
138 if (set && (state->state & bits) == bits)
140 if (!set && (state->state & bits) == 0)
142 changeset->bytes_changed += state->end - state->start + 1;
143 ret = ulist_add(&changeset->range_changed, state->start, state->end,
148 static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
149 unsigned long bio_flags)
151 blk_status_t ret = 0;
152 struct extent_io_tree *tree = bio->bi_private;
154 bio->bi_private = NULL;
157 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
158 mirror_num, bio_flags);
160 btrfsic_submit_bio(bio);
162 return blk_status_to_errno(ret);
165 /* Cleanup unsubmitted bios */
166 static void end_write_bio(struct extent_page_data *epd, int ret)
169 epd->bio->bi_status = errno_to_blk_status(ret);
176 * Submit bio from extent page data via submit_one_bio
178 * Return 0 if everything is OK.
179 * Return <0 for error.
181 static int __must_check flush_write_bio(struct extent_page_data *epd)
186 ret = submit_one_bio(epd->bio, 0, 0);
188 * Clean up of epd->bio is handled by its endio function.
189 * And endio is either triggered by successful bio execution
190 * or the error handler of submit bio hook.
191 * So at this point, no matter what happened, we don't need
192 * to clean up epd->bio.
199 int __init extent_io_init(void)
201 extent_state_cache = kmem_cache_create("btrfs_extent_state",
202 sizeof(struct extent_state), 0,
203 SLAB_MEM_SPREAD, NULL);
204 if (!extent_state_cache)
207 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
208 sizeof(struct extent_buffer), 0,
209 SLAB_MEM_SPREAD, NULL);
210 if (!extent_buffer_cache)
211 goto free_state_cache;
213 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
214 offsetof(struct btrfs_io_bio, bio),
216 goto free_buffer_cache;
218 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
224 bioset_exit(&btrfs_bioset);
227 kmem_cache_destroy(extent_buffer_cache);
228 extent_buffer_cache = NULL;
231 kmem_cache_destroy(extent_state_cache);
232 extent_state_cache = NULL;
236 void __cold extent_io_exit(void)
238 btrfs_leak_debug_check();
241 * Make sure all delayed rcu free are flushed before we
245 kmem_cache_destroy(extent_state_cache);
246 kmem_cache_destroy(extent_buffer_cache);
247 bioset_exit(&btrfs_bioset);
250 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
251 struct extent_io_tree *tree, unsigned int owner,
254 tree->fs_info = fs_info;
255 tree->state = RB_ROOT;
257 tree->dirty_bytes = 0;
258 spin_lock_init(&tree->lock);
259 tree->private_data = private_data;
263 void extent_io_tree_release(struct extent_io_tree *tree)
265 spin_lock(&tree->lock);
267 * Do a single barrier for the waitqueue_active check here, the state
268 * of the waitqueue should not change once extent_io_tree_release is
272 while (!RB_EMPTY_ROOT(&tree->state)) {
273 struct rb_node *node;
274 struct extent_state *state;
276 node = rb_first(&tree->state);
277 state = rb_entry(node, struct extent_state, rb_node);
278 rb_erase(&state->rb_node, &tree->state);
279 RB_CLEAR_NODE(&state->rb_node);
281 * btree io trees aren't supposed to have tasks waiting for
282 * changes in the flags of extent states ever.
284 ASSERT(!waitqueue_active(&state->wq));
285 free_extent_state(state);
287 cond_resched_lock(&tree->lock);
289 spin_unlock(&tree->lock);
292 static struct extent_state *alloc_extent_state(gfp_t mask)
294 struct extent_state *state;
297 * The given mask might be not appropriate for the slab allocator,
298 * drop the unsupported bits
300 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
301 state = kmem_cache_alloc(extent_state_cache, mask);
305 state->failrec = NULL;
306 RB_CLEAR_NODE(&state->rb_node);
307 btrfs_leak_debug_add(&state->leak_list, &states);
308 refcount_set(&state->refs, 1);
309 init_waitqueue_head(&state->wq);
310 trace_alloc_extent_state(state, mask, _RET_IP_);
314 void free_extent_state(struct extent_state *state)
318 if (refcount_dec_and_test(&state->refs)) {
319 WARN_ON(extent_state_in_tree(state));
320 btrfs_leak_debug_del(&state->leak_list);
321 trace_free_extent_state(state, _RET_IP_);
322 kmem_cache_free(extent_state_cache, state);
326 static struct rb_node *tree_insert(struct rb_root *root,
327 struct rb_node *search_start,
329 struct rb_node *node,
330 struct rb_node ***p_in,
331 struct rb_node **parent_in)
334 struct rb_node *parent = NULL;
335 struct tree_entry *entry;
337 if (p_in && parent_in) {
343 p = search_start ? &search_start : &root->rb_node;
346 entry = rb_entry(parent, struct tree_entry, rb_node);
348 if (offset < entry->start)
350 else if (offset > entry->end)
357 rb_link_node(node, parent, p);
358 rb_insert_color(node, root);
363 * __etree_search - searche @tree for an entry that contains @offset. Such
364 * entry would have entry->start <= offset && entry->end >= offset.
366 * @tree - the tree to search
367 * @offset - offset that should fall within an entry in @tree
368 * @next_ret - pointer to the first entry whose range ends after @offset
369 * @prev - pointer to the first entry whose range begins before @offset
370 * @p_ret - pointer where new node should be anchored (used when inserting an
372 * @parent_ret - points to entry which would have been the parent of the entry,
375 * This function returns a pointer to the entry that contains @offset byte
376 * address. If no such entry exists, then NULL is returned and the other
377 * pointer arguments to the function are filled, otherwise the found entry is
378 * returned and other pointers are left untouched.
380 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
381 struct rb_node **next_ret,
382 struct rb_node **prev_ret,
383 struct rb_node ***p_ret,
384 struct rb_node **parent_ret)
386 struct rb_root *root = &tree->state;
387 struct rb_node **n = &root->rb_node;
388 struct rb_node *prev = NULL;
389 struct rb_node *orig_prev = NULL;
390 struct tree_entry *entry;
391 struct tree_entry *prev_entry = NULL;
395 entry = rb_entry(prev, struct tree_entry, rb_node);
398 if (offset < entry->start)
400 else if (offset > entry->end)
413 while (prev && offset > prev_entry->end) {
414 prev = rb_next(prev);
415 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
422 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
423 while (prev && offset < prev_entry->start) {
424 prev = rb_prev(prev);
425 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
432 static inline struct rb_node *
433 tree_search_for_insert(struct extent_io_tree *tree,
435 struct rb_node ***p_ret,
436 struct rb_node **parent_ret)
438 struct rb_node *next= NULL;
441 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
447 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
450 return tree_search_for_insert(tree, offset, NULL, NULL);
454 * utility function to look for merge candidates inside a given range.
455 * Any extents with matching state are merged together into a single
456 * extent in the tree. Extents with EXTENT_IO in their state field
457 * are not merged because the end_io handlers need to be able to do
458 * operations on them without sleeping (or doing allocations/splits).
460 * This should be called with the tree lock held.
462 static void merge_state(struct extent_io_tree *tree,
463 struct extent_state *state)
465 struct extent_state *other;
466 struct rb_node *other_node;
468 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
471 other_node = rb_prev(&state->rb_node);
473 other = rb_entry(other_node, struct extent_state, rb_node);
474 if (other->end == state->start - 1 &&
475 other->state == state->state) {
476 if (tree->private_data &&
477 is_data_inode(tree->private_data))
478 btrfs_merge_delalloc_extent(tree->private_data,
480 state->start = other->start;
481 rb_erase(&other->rb_node, &tree->state);
482 RB_CLEAR_NODE(&other->rb_node);
483 free_extent_state(other);
486 other_node = rb_next(&state->rb_node);
488 other = rb_entry(other_node, struct extent_state, rb_node);
489 if (other->start == state->end + 1 &&
490 other->state == state->state) {
491 if (tree->private_data &&
492 is_data_inode(tree->private_data))
493 btrfs_merge_delalloc_extent(tree->private_data,
495 state->end = other->end;
496 rb_erase(&other->rb_node, &tree->state);
497 RB_CLEAR_NODE(&other->rb_node);
498 free_extent_state(other);
503 static void set_state_bits(struct extent_io_tree *tree,
504 struct extent_state *state, unsigned *bits,
505 struct extent_changeset *changeset);
508 * insert an extent_state struct into the tree. 'bits' are set on the
509 * struct before it is inserted.
511 * This may return -EEXIST if the extent is already there, in which case the
512 * state struct is freed.
514 * The tree lock is not taken internally. This is a utility function and
515 * probably isn't what you want to call (see set/clear_extent_bit).
517 static int insert_state(struct extent_io_tree *tree,
518 struct extent_state *state, u64 start, u64 end,
520 struct rb_node **parent,
521 unsigned *bits, struct extent_changeset *changeset)
523 struct rb_node *node;
526 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
528 state->start = start;
531 set_state_bits(tree, state, bits, changeset);
533 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
535 struct extent_state *found;
536 found = rb_entry(node, struct extent_state, rb_node);
537 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
538 found->start, found->end, start, end);
541 merge_state(tree, state);
546 * split a given extent state struct in two, inserting the preallocated
547 * struct 'prealloc' as the newly created second half. 'split' indicates an
548 * offset inside 'orig' where it should be split.
551 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
552 * are two extent state structs in the tree:
553 * prealloc: [orig->start, split - 1]
554 * orig: [ split, orig->end ]
556 * The tree locks are not taken by this function. They need to be held
559 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
560 struct extent_state *prealloc, u64 split)
562 struct rb_node *node;
564 if (tree->private_data && is_data_inode(tree->private_data))
565 btrfs_split_delalloc_extent(tree->private_data, orig, split);
567 prealloc->start = orig->start;
568 prealloc->end = split - 1;
569 prealloc->state = orig->state;
572 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
573 &prealloc->rb_node, NULL, NULL);
575 free_extent_state(prealloc);
581 static struct extent_state *next_state(struct extent_state *state)
583 struct rb_node *next = rb_next(&state->rb_node);
585 return rb_entry(next, struct extent_state, rb_node);
591 * utility function to clear some bits in an extent state struct.
592 * it will optionally wake up anyone waiting on this state (wake == 1).
594 * If no bits are set on the state struct after clearing things, the
595 * struct is freed and removed from the tree
597 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
598 struct extent_state *state,
599 unsigned *bits, int wake,
600 struct extent_changeset *changeset)
602 struct extent_state *next;
603 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
606 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
607 u64 range = state->end - state->start + 1;
608 WARN_ON(range > tree->dirty_bytes);
609 tree->dirty_bytes -= range;
612 if (tree->private_data && is_data_inode(tree->private_data))
613 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
615 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
617 state->state &= ~bits_to_clear;
620 if (state->state == 0) {
621 next = next_state(state);
622 if (extent_state_in_tree(state)) {
623 rb_erase(&state->rb_node, &tree->state);
624 RB_CLEAR_NODE(&state->rb_node);
625 free_extent_state(state);
630 merge_state(tree, state);
631 next = next_state(state);
636 static struct extent_state *
637 alloc_extent_state_atomic(struct extent_state *prealloc)
640 prealloc = alloc_extent_state(GFP_ATOMIC);
645 static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
647 struct inode *inode = tree->private_data;
649 btrfs_panic(btrfs_sb(inode->i_sb), err,
650 "locking error: extent tree was modified by another thread while locked");
654 * clear some bits on a range in the tree. This may require splitting
655 * or inserting elements in the tree, so the gfp mask is used to
656 * indicate which allocations or sleeping are allowed.
658 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
659 * the given range from the tree regardless of state (ie for truncate).
661 * the range [start, end] is inclusive.
663 * This takes the tree lock, and returns 0 on success and < 0 on error.
665 int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
666 unsigned bits, int wake, int delete,
667 struct extent_state **cached_state,
668 gfp_t mask, struct extent_changeset *changeset)
670 struct extent_state *state;
671 struct extent_state *cached;
672 struct extent_state *prealloc = NULL;
673 struct rb_node *node;
678 btrfs_debug_check_extent_io_range(tree, start, end);
679 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
681 if (bits & EXTENT_DELALLOC)
682 bits |= EXTENT_NORESERVE;
685 bits |= ~EXTENT_CTLBITS;
687 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
690 if (!prealloc && gfpflags_allow_blocking(mask)) {
692 * Don't care for allocation failure here because we might end
693 * up not needing the pre-allocated extent state at all, which
694 * is the case if we only have in the tree extent states that
695 * cover our input range and don't cover too any other range.
696 * If we end up needing a new extent state we allocate it later.
698 prealloc = alloc_extent_state(mask);
701 spin_lock(&tree->lock);
703 cached = *cached_state;
706 *cached_state = NULL;
710 if (cached && extent_state_in_tree(cached) &&
711 cached->start <= start && cached->end > start) {
713 refcount_dec(&cached->refs);
718 free_extent_state(cached);
721 * this search will find the extents that end after
724 node = tree_search(tree, start);
727 state = rb_entry(node, struct extent_state, rb_node);
729 if (state->start > end)
731 WARN_ON(state->end < start);
732 last_end = state->end;
734 /* the state doesn't have the wanted bits, go ahead */
735 if (!(state->state & bits)) {
736 state = next_state(state);
741 * | ---- desired range ---- |
743 * | ------------- state -------------- |
745 * We need to split the extent we found, and may flip
746 * bits on second half.
748 * If the extent we found extends past our range, we
749 * just split and search again. It'll get split again
750 * the next time though.
752 * If the extent we found is inside our range, we clear
753 * the desired bit on it.
756 if (state->start < start) {
757 prealloc = alloc_extent_state_atomic(prealloc);
759 err = split_state(tree, state, prealloc, start);
761 extent_io_tree_panic(tree, err);
766 if (state->end <= end) {
767 state = clear_state_bit(tree, state, &bits, wake,
774 * | ---- desired range ---- |
776 * We need to split the extent, and clear the bit
779 if (state->start <= end && state->end > end) {
780 prealloc = alloc_extent_state_atomic(prealloc);
782 err = split_state(tree, state, prealloc, end + 1);
784 extent_io_tree_panic(tree, err);
789 clear_state_bit(tree, prealloc, &bits, wake, changeset);
795 state = clear_state_bit(tree, state, &bits, wake, changeset);
797 if (last_end == (u64)-1)
799 start = last_end + 1;
800 if (start <= end && state && !need_resched())
806 spin_unlock(&tree->lock);
807 if (gfpflags_allow_blocking(mask))
812 spin_unlock(&tree->lock);
814 free_extent_state(prealloc);
820 static void wait_on_state(struct extent_io_tree *tree,
821 struct extent_state *state)
822 __releases(tree->lock)
823 __acquires(tree->lock)
826 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
827 spin_unlock(&tree->lock);
829 spin_lock(&tree->lock);
830 finish_wait(&state->wq, &wait);
834 * waits for one or more bits to clear on a range in the state tree.
835 * The range [start, end] is inclusive.
836 * The tree lock is taken by this function
838 static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
841 struct extent_state *state;
842 struct rb_node *node;
844 btrfs_debug_check_extent_io_range(tree, start, end);
846 spin_lock(&tree->lock);
850 * this search will find all the extents that end after
853 node = tree_search(tree, start);
858 state = rb_entry(node, struct extent_state, rb_node);
860 if (state->start > end)
863 if (state->state & bits) {
864 start = state->start;
865 refcount_inc(&state->refs);
866 wait_on_state(tree, state);
867 free_extent_state(state);
870 start = state->end + 1;
875 if (!cond_resched_lock(&tree->lock)) {
876 node = rb_next(node);
881 spin_unlock(&tree->lock);
884 static void set_state_bits(struct extent_io_tree *tree,
885 struct extent_state *state,
886 unsigned *bits, struct extent_changeset *changeset)
888 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
891 if (tree->private_data && is_data_inode(tree->private_data))
892 btrfs_set_delalloc_extent(tree->private_data, state, bits);
894 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
895 u64 range = state->end - state->start + 1;
896 tree->dirty_bytes += range;
898 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
900 state->state |= bits_to_set;
903 static void cache_state_if_flags(struct extent_state *state,
904 struct extent_state **cached_ptr,
907 if (cached_ptr && !(*cached_ptr)) {
908 if (!flags || (state->state & flags)) {
910 refcount_inc(&state->refs);
915 static void cache_state(struct extent_state *state,
916 struct extent_state **cached_ptr)
918 return cache_state_if_flags(state, cached_ptr,
919 EXTENT_LOCKED | EXTENT_BOUNDARY);
923 * set some bits on a range in the tree. This may require allocations or
924 * sleeping, so the gfp mask is used to indicate what is allowed.
926 * If any of the exclusive bits are set, this will fail with -EEXIST if some
927 * part of the range already has the desired bits set. The start of the
928 * existing range is returned in failed_start in this case.
930 * [start, end] is inclusive This takes the tree lock.
933 static int __must_check
934 __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
935 unsigned bits, unsigned exclusive_bits,
936 u64 *failed_start, struct extent_state **cached_state,
937 gfp_t mask, struct extent_changeset *changeset)
939 struct extent_state *state;
940 struct extent_state *prealloc = NULL;
941 struct rb_node *node;
943 struct rb_node *parent;
948 btrfs_debug_check_extent_io_range(tree, start, end);
949 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
952 if (!prealloc && gfpflags_allow_blocking(mask)) {
954 * Don't care for allocation failure here because we might end
955 * up not needing the pre-allocated extent state at all, which
956 * is the case if we only have in the tree extent states that
957 * cover our input range and don't cover too any other range.
958 * If we end up needing a new extent state we allocate it later.
960 prealloc = alloc_extent_state(mask);
963 spin_lock(&tree->lock);
964 if (cached_state && *cached_state) {
965 state = *cached_state;
966 if (state->start <= start && state->end > start &&
967 extent_state_in_tree(state)) {
968 node = &state->rb_node;
973 * this search will find all the extents that end after
976 node = tree_search_for_insert(tree, start, &p, &parent);
978 prealloc = alloc_extent_state_atomic(prealloc);
980 err = insert_state(tree, prealloc, start, end,
981 &p, &parent, &bits, changeset);
983 extent_io_tree_panic(tree, err);
985 cache_state(prealloc, cached_state);
989 state = rb_entry(node, struct extent_state, rb_node);
991 last_start = state->start;
992 last_end = state->end;
995 * | ---- desired range ---- |
998 * Just lock what we found and keep going
1000 if (state->start == start && state->end <= end) {
1001 if (state->state & exclusive_bits) {
1002 *failed_start = state->start;
1007 set_state_bits(tree, state, &bits, changeset);
1008 cache_state(state, cached_state);
1009 merge_state(tree, state);
1010 if (last_end == (u64)-1)
1012 start = last_end + 1;
1013 state = next_state(state);
1014 if (start < end && state && state->start == start &&
1021 * | ---- desired range ---- |
1024 * | ------------- state -------------- |
1026 * We need to split the extent we found, and may flip bits on
1029 * If the extent we found extends past our
1030 * range, we just split and search again. It'll get split
1031 * again the next time though.
1033 * If the extent we found is inside our range, we set the
1034 * desired bit on it.
1036 if (state->start < start) {
1037 if (state->state & exclusive_bits) {
1038 *failed_start = start;
1043 prealloc = alloc_extent_state_atomic(prealloc);
1045 err = split_state(tree, state, prealloc, start);
1047 extent_io_tree_panic(tree, err);
1052 if (state->end <= end) {
1053 set_state_bits(tree, state, &bits, changeset);
1054 cache_state(state, cached_state);
1055 merge_state(tree, state);
1056 if (last_end == (u64)-1)
1058 start = last_end + 1;
1059 state = next_state(state);
1060 if (start < end && state && state->start == start &&
1067 * | ---- desired range ---- |
1068 * | state | or | state |
1070 * There's a hole, we need to insert something in it and
1071 * ignore the extent we found.
1073 if (state->start > start) {
1075 if (end < last_start)
1078 this_end = last_start - 1;
1080 prealloc = alloc_extent_state_atomic(prealloc);
1084 * Avoid to free 'prealloc' if it can be merged with
1087 err = insert_state(tree, prealloc, start, this_end,
1088 NULL, NULL, &bits, changeset);
1090 extent_io_tree_panic(tree, err);
1092 cache_state(prealloc, cached_state);
1094 start = this_end + 1;
1098 * | ---- desired range ---- |
1100 * We need to split the extent, and set the bit
1103 if (state->start <= end && state->end > end) {
1104 if (state->state & exclusive_bits) {
1105 *failed_start = start;
1110 prealloc = alloc_extent_state_atomic(prealloc);
1112 err = split_state(tree, state, prealloc, end + 1);
1114 extent_io_tree_panic(tree, err);
1116 set_state_bits(tree, prealloc, &bits, changeset);
1117 cache_state(prealloc, cached_state);
1118 merge_state(tree, prealloc);
1126 spin_unlock(&tree->lock);
1127 if (gfpflags_allow_blocking(mask))
1132 spin_unlock(&tree->lock);
1134 free_extent_state(prealloc);
1140 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1141 unsigned bits, u64 * failed_start,
1142 struct extent_state **cached_state, gfp_t mask)
1144 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
1145 cached_state, mask, NULL);
1150 * convert_extent_bit - convert all bits in a given range from one bit to
1152 * @tree: the io tree to search
1153 * @start: the start offset in bytes
1154 * @end: the end offset in bytes (inclusive)
1155 * @bits: the bits to set in this range
1156 * @clear_bits: the bits to clear in this range
1157 * @cached_state: state that we're going to cache
1159 * This will go through and set bits for the given range. If any states exist
1160 * already in this range they are set with the given bit and cleared of the
1161 * clear_bits. This is only meant to be used by things that are mergeable, ie
1162 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1163 * boundary bits like LOCK.
1165 * All allocations are done with GFP_NOFS.
1167 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1168 unsigned bits, unsigned clear_bits,
1169 struct extent_state **cached_state)
1171 struct extent_state *state;
1172 struct extent_state *prealloc = NULL;
1173 struct rb_node *node;
1175 struct rb_node *parent;
1179 bool first_iteration = true;
1181 btrfs_debug_check_extent_io_range(tree, start, end);
1182 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1188 * Best effort, don't worry if extent state allocation fails
1189 * here for the first iteration. We might have a cached state
1190 * that matches exactly the target range, in which case no
1191 * extent state allocations are needed. We'll only know this
1192 * after locking the tree.
1194 prealloc = alloc_extent_state(GFP_NOFS);
1195 if (!prealloc && !first_iteration)
1199 spin_lock(&tree->lock);
1200 if (cached_state && *cached_state) {
1201 state = *cached_state;
1202 if (state->start <= start && state->end > start &&
1203 extent_state_in_tree(state)) {
1204 node = &state->rb_node;
1210 * this search will find all the extents that end after
1213 node = tree_search_for_insert(tree, start, &p, &parent);
1215 prealloc = alloc_extent_state_atomic(prealloc);
1220 err = insert_state(tree, prealloc, start, end,
1221 &p, &parent, &bits, NULL);
1223 extent_io_tree_panic(tree, err);
1224 cache_state(prealloc, cached_state);
1228 state = rb_entry(node, struct extent_state, rb_node);
1230 last_start = state->start;
1231 last_end = state->end;
1234 * | ---- desired range ---- |
1237 * Just lock what we found and keep going
1239 if (state->start == start && state->end <= end) {
1240 set_state_bits(tree, state, &bits, NULL);
1241 cache_state(state, cached_state);
1242 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
1243 if (last_end == (u64)-1)
1245 start = last_end + 1;
1246 if (start < end && state && state->start == start &&
1253 * | ---- desired range ---- |
1256 * | ------------- state -------------- |
1258 * We need to split the extent we found, and may flip bits on
1261 * If the extent we found extends past our
1262 * range, we just split and search again. It'll get split
1263 * again the next time though.
1265 * If the extent we found is inside our range, we set the
1266 * desired bit on it.
1268 if (state->start < start) {
1269 prealloc = alloc_extent_state_atomic(prealloc);
1274 err = split_state(tree, state, prealloc, start);
1276 extent_io_tree_panic(tree, err);
1280 if (state->end <= end) {
1281 set_state_bits(tree, state, &bits, NULL);
1282 cache_state(state, cached_state);
1283 state = clear_state_bit(tree, state, &clear_bits, 0,
1285 if (last_end == (u64)-1)
1287 start = last_end + 1;
1288 if (start < end && state && state->start == start &&
1295 * | ---- desired range ---- |
1296 * | state | or | state |
1298 * There's a hole, we need to insert something in it and
1299 * ignore the extent we found.
1301 if (state->start > start) {
1303 if (end < last_start)
1306 this_end = last_start - 1;
1308 prealloc = alloc_extent_state_atomic(prealloc);
1315 * Avoid to free 'prealloc' if it can be merged with
1318 err = insert_state(tree, prealloc, start, this_end,
1319 NULL, NULL, &bits, NULL);
1321 extent_io_tree_panic(tree, err);
1322 cache_state(prealloc, cached_state);
1324 start = this_end + 1;
1328 * | ---- desired range ---- |
1330 * We need to split the extent, and set the bit
1333 if (state->start <= end && state->end > end) {
1334 prealloc = alloc_extent_state_atomic(prealloc);
1340 err = split_state(tree, state, prealloc, end + 1);
1342 extent_io_tree_panic(tree, err);
1344 set_state_bits(tree, prealloc, &bits, NULL);
1345 cache_state(prealloc, cached_state);
1346 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
1354 spin_unlock(&tree->lock);
1356 first_iteration = false;
1360 spin_unlock(&tree->lock);
1362 free_extent_state(prealloc);
1367 /* wrappers around set/clear extent bit */
1368 int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1369 unsigned bits, struct extent_changeset *changeset)
1372 * We don't support EXTENT_LOCKED yet, as current changeset will
1373 * record any bits changed, so for EXTENT_LOCKED case, it will
1374 * either fail with -EEXIST or changeset will record the whole
1377 BUG_ON(bits & EXTENT_LOCKED);
1379 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
1383 int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
1386 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
1390 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1391 unsigned bits, int wake, int delete,
1392 struct extent_state **cached)
1394 return __clear_extent_bit(tree, start, end, bits, wake, delete,
1395 cached, GFP_NOFS, NULL);
1398 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1399 unsigned bits, struct extent_changeset *changeset)
1402 * Don't support EXTENT_LOCKED case, same reason as
1403 * set_record_extent_bits().
1405 BUG_ON(bits & EXTENT_LOCKED);
1407 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
1412 * either insert or lock state struct between start and end use mask to tell
1413 * us if waiting is desired.
1415 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1416 struct extent_state **cached_state)
1422 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
1423 EXTENT_LOCKED, &failed_start,
1424 cached_state, GFP_NOFS, NULL);
1425 if (err == -EEXIST) {
1426 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1427 start = failed_start;
1430 WARN_ON(start > end);
1435 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1440 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1441 &failed_start, NULL, GFP_NOFS, NULL);
1442 if (err == -EEXIST) {
1443 if (failed_start > start)
1444 clear_extent_bit(tree, start, failed_start - 1,
1445 EXTENT_LOCKED, 1, 0, NULL);
1451 void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1453 unsigned long index = start >> PAGE_SHIFT;
1454 unsigned long end_index = end >> PAGE_SHIFT;
1457 while (index <= end_index) {
1458 page = find_get_page(inode->i_mapping, index);
1459 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1460 clear_page_dirty_for_io(page);
1466 void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1468 unsigned long index = start >> PAGE_SHIFT;
1469 unsigned long end_index = end >> PAGE_SHIFT;
1472 while (index <= end_index) {
1473 page = find_get_page(inode->i_mapping, index);
1474 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1475 __set_page_dirty_nobuffers(page);
1476 account_page_redirty(page);
1482 /* find the first state struct with 'bits' set after 'start', and
1483 * return it. tree->lock must be held. NULL will returned if
1484 * nothing was found after 'start'
1486 static struct extent_state *
1487 find_first_extent_bit_state(struct extent_io_tree *tree,
1488 u64 start, unsigned bits)
1490 struct rb_node *node;
1491 struct extent_state *state;
1494 * this search will find all the extents that end after
1497 node = tree_search(tree, start);
1502 state = rb_entry(node, struct extent_state, rb_node);
1503 if (state->end >= start && (state->state & bits))
1506 node = rb_next(node);
1515 * find the first offset in the io tree with 'bits' set. zero is
1516 * returned if we find something, and *start_ret and *end_ret are
1517 * set to reflect the state struct that was found.
1519 * If nothing was found, 1 is returned. If found something, return 0.
1521 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1522 u64 *start_ret, u64 *end_ret, unsigned bits,
1523 struct extent_state **cached_state)
1525 struct extent_state *state;
1528 spin_lock(&tree->lock);
1529 if (cached_state && *cached_state) {
1530 state = *cached_state;
1531 if (state->end == start - 1 && extent_state_in_tree(state)) {
1532 while ((state = next_state(state)) != NULL) {
1533 if (state->state & bits)
1536 free_extent_state(*cached_state);
1537 *cached_state = NULL;
1540 free_extent_state(*cached_state);
1541 *cached_state = NULL;
1544 state = find_first_extent_bit_state(tree, start, bits);
1547 cache_state_if_flags(state, cached_state, 0);
1548 *start_ret = state->start;
1549 *end_ret = state->end;
1553 spin_unlock(&tree->lock);
1558 * find_first_clear_extent_bit - find the first range that has @bits not set.
1559 * This range could start before @start.
1561 * @tree - the tree to search
1562 * @start - the offset at/after which the found extent should start
1563 * @start_ret - records the beginning of the range
1564 * @end_ret - records the end of the range (inclusive)
1565 * @bits - the set of bits which must be unset
1567 * Since unallocated range is also considered one which doesn't have the bits
1568 * set it's possible that @end_ret contains -1, this happens in case the range
1569 * spans (last_range_end, end of device]. In this case it's up to the caller to
1570 * trim @end_ret to the appropriate size.
1572 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
1573 u64 *start_ret, u64 *end_ret, unsigned bits)
1575 struct extent_state *state;
1576 struct rb_node *node, *prev = NULL, *next;
1578 spin_lock(&tree->lock);
1580 /* Find first extent with bits cleared */
1582 node = __etree_search(tree, start, &next, &prev, NULL, NULL);
1587 * We are past the last allocated chunk,
1588 * set start at the end of the last extent. The
1589 * device alloc tree should never be empty so
1590 * prev is always set.
1593 state = rb_entry(prev, struct extent_state, rb_node);
1594 *start_ret = state->end + 1;
1600 * At this point 'node' either contains 'start' or start is
1603 state = rb_entry(node, struct extent_state, rb_node);
1605 if (in_range(start, state->start, state->end - state->start + 1)) {
1606 if (state->state & bits) {
1608 * |--range with bits sets--|
1612 start = state->end + 1;
1615 * 'start' falls within a range that doesn't
1616 * have the bits set, so take its start as
1617 * the beginning of the desired range
1619 * |--range with bits cleared----|
1623 *start_ret = state->start;
1628 * |---prev range---|---hole/unset---|---node range---|
1634 * |---hole/unset--||--first node--|
1639 state = rb_entry(prev, struct extent_state,
1641 *start_ret = state->end + 1;
1650 * Find the longest stretch from start until an entry which has the
1654 state = rb_entry(node, struct extent_state, rb_node);
1655 if (state->end >= start && !(state->state & bits)) {
1656 *end_ret = state->end;
1658 *end_ret = state->start - 1;
1662 node = rb_next(node);
1667 spin_unlock(&tree->lock);
1671 * find a contiguous range of bytes in the file marked as delalloc, not
1672 * more than 'max_bytes'. start and end are used to return the range,
1674 * true is returned if we find something, false if nothing was in the tree
1676 static noinline bool find_delalloc_range(struct extent_io_tree *tree,
1677 u64 *start, u64 *end, u64 max_bytes,
1678 struct extent_state **cached_state)
1680 struct rb_node *node;
1681 struct extent_state *state;
1682 u64 cur_start = *start;
1684 u64 total_bytes = 0;
1686 spin_lock(&tree->lock);
1689 * this search will find all the extents that end after
1692 node = tree_search(tree, cur_start);
1699 state = rb_entry(node, struct extent_state, rb_node);
1700 if (found && (state->start != cur_start ||
1701 (state->state & EXTENT_BOUNDARY))) {
1704 if (!(state->state & EXTENT_DELALLOC)) {
1710 *start = state->start;
1711 *cached_state = state;
1712 refcount_inc(&state->refs);
1716 cur_start = state->end + 1;
1717 node = rb_next(node);
1718 total_bytes += state->end - state->start + 1;
1719 if (total_bytes >= max_bytes)
1725 spin_unlock(&tree->lock);
1729 static int __process_pages_contig(struct address_space *mapping,
1730 struct page *locked_page,
1731 pgoff_t start_index, pgoff_t end_index,
1732 unsigned long page_ops, pgoff_t *index_ret);
1734 static noinline void __unlock_for_delalloc(struct inode *inode,
1735 struct page *locked_page,
1738 unsigned long index = start >> PAGE_SHIFT;
1739 unsigned long end_index = end >> PAGE_SHIFT;
1741 ASSERT(locked_page);
1742 if (index == locked_page->index && end_index == index)
1745 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1749 static noinline int lock_delalloc_pages(struct inode *inode,
1750 struct page *locked_page,
1754 unsigned long index = delalloc_start >> PAGE_SHIFT;
1755 unsigned long index_ret = index;
1756 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
1759 ASSERT(locked_page);
1760 if (index == locked_page->index && index == end_index)
1763 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1764 end_index, PAGE_LOCK, &index_ret);
1766 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1767 (u64)index_ret << PAGE_SHIFT);
1772 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1773 * more than @max_bytes. @Start and @end are used to return the range,
1775 * Return: true if we find something
1776 * false if nothing was in the tree
1779 noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
1780 struct extent_io_tree *tree,
1781 struct page *locked_page, u64 *start,
1784 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
1788 struct extent_state *cached_state = NULL;
1793 /* step one, find a bunch of delalloc bytes starting at start */
1794 delalloc_start = *start;
1796 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1797 max_bytes, &cached_state);
1798 if (!found || delalloc_end <= *start) {
1799 *start = delalloc_start;
1800 *end = delalloc_end;
1801 free_extent_state(cached_state);
1806 * start comes from the offset of locked_page. We have to lock
1807 * pages in order, so we can't process delalloc bytes before
1810 if (delalloc_start < *start)
1811 delalloc_start = *start;
1814 * make sure to limit the number of pages we try to lock down
1816 if (delalloc_end + 1 - delalloc_start > max_bytes)
1817 delalloc_end = delalloc_start + max_bytes - 1;
1819 /* step two, lock all the pages after the page that has start */
1820 ret = lock_delalloc_pages(inode, locked_page,
1821 delalloc_start, delalloc_end);
1822 ASSERT(!ret || ret == -EAGAIN);
1823 if (ret == -EAGAIN) {
1824 /* some of the pages are gone, lets avoid looping by
1825 * shortening the size of the delalloc range we're searching
1827 free_extent_state(cached_state);
1828 cached_state = NULL;
1830 max_bytes = PAGE_SIZE;
1839 /* step three, lock the state bits for the whole range */
1840 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
1842 /* then test to make sure it is all still delalloc */
1843 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1844 EXTENT_DELALLOC, 1, cached_state);
1846 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1848 __unlock_for_delalloc(inode, locked_page,
1849 delalloc_start, delalloc_end);
1853 free_extent_state(cached_state);
1854 *start = delalloc_start;
1855 *end = delalloc_end;
1860 static int __process_pages_contig(struct address_space *mapping,
1861 struct page *locked_page,
1862 pgoff_t start_index, pgoff_t end_index,
1863 unsigned long page_ops, pgoff_t *index_ret)
1865 unsigned long nr_pages = end_index - start_index + 1;
1866 unsigned long pages_locked = 0;
1867 pgoff_t index = start_index;
1868 struct page *pages[16];
1873 if (page_ops & PAGE_LOCK) {
1874 ASSERT(page_ops == PAGE_LOCK);
1875 ASSERT(index_ret && *index_ret == start_index);
1878 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
1879 mapping_set_error(mapping, -EIO);
1881 while (nr_pages > 0) {
1882 ret = find_get_pages_contig(mapping, index,
1883 min_t(unsigned long,
1884 nr_pages, ARRAY_SIZE(pages)), pages);
1887 * Only if we're going to lock these pages,
1888 * can we find nothing at @index.
1890 ASSERT(page_ops & PAGE_LOCK);
1895 for (i = 0; i < ret; i++) {
1896 if (page_ops & PAGE_SET_PRIVATE2)
1897 SetPagePrivate2(pages[i]);
1899 if (pages[i] == locked_page) {
1904 if (page_ops & PAGE_CLEAR_DIRTY)
1905 clear_page_dirty_for_io(pages[i]);
1906 if (page_ops & PAGE_SET_WRITEBACK)
1907 set_page_writeback(pages[i]);
1908 if (page_ops & PAGE_SET_ERROR)
1909 SetPageError(pages[i]);
1910 if (page_ops & PAGE_END_WRITEBACK)
1911 end_page_writeback(pages[i]);
1912 if (page_ops & PAGE_UNLOCK)
1913 unlock_page(pages[i]);
1914 if (page_ops & PAGE_LOCK) {
1915 lock_page(pages[i]);
1916 if (!PageDirty(pages[i]) ||
1917 pages[i]->mapping != mapping) {
1918 unlock_page(pages[i]);
1932 if (err && index_ret)
1933 *index_ret = start_index + pages_locked - 1;
1937 void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1938 u64 delalloc_end, struct page *locked_page,
1939 unsigned clear_bits,
1940 unsigned long page_ops)
1942 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
1945 __process_pages_contig(inode->i_mapping, locked_page,
1946 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
1951 * count the number of bytes in the tree that have a given bit(s)
1952 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1953 * cached. The total number found is returned.
1955 u64 count_range_bits(struct extent_io_tree *tree,
1956 u64 *start, u64 search_end, u64 max_bytes,
1957 unsigned bits, int contig)
1959 struct rb_node *node;
1960 struct extent_state *state;
1961 u64 cur_start = *start;
1962 u64 total_bytes = 0;
1966 if (WARN_ON(search_end <= cur_start))
1969 spin_lock(&tree->lock);
1970 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1971 total_bytes = tree->dirty_bytes;
1975 * this search will find all the extents that end after
1978 node = tree_search(tree, cur_start);
1983 state = rb_entry(node, struct extent_state, rb_node);
1984 if (state->start > search_end)
1986 if (contig && found && state->start > last + 1)
1988 if (state->end >= cur_start && (state->state & bits) == bits) {
1989 total_bytes += min(search_end, state->end) + 1 -
1990 max(cur_start, state->start);
1991 if (total_bytes >= max_bytes)
1994 *start = max(cur_start, state->start);
1998 } else if (contig && found) {
2001 node = rb_next(node);
2006 spin_unlock(&tree->lock);
2011 * set the private field for a given byte offset in the tree. If there isn't
2012 * an extent_state there already, this does nothing.
2014 static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
2015 struct io_failure_record *failrec)
2017 struct rb_node *node;
2018 struct extent_state *state;
2021 spin_lock(&tree->lock);
2023 * this search will find all the extents that end after
2026 node = tree_search(tree, start);
2031 state = rb_entry(node, struct extent_state, rb_node);
2032 if (state->start != start) {
2036 state->failrec = failrec;
2038 spin_unlock(&tree->lock);
2042 static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
2043 struct io_failure_record **failrec)
2045 struct rb_node *node;
2046 struct extent_state *state;
2049 spin_lock(&tree->lock);
2051 * this search will find all the extents that end after
2054 node = tree_search(tree, start);
2059 state = rb_entry(node, struct extent_state, rb_node);
2060 if (state->start != start) {
2064 *failrec = state->failrec;
2066 spin_unlock(&tree->lock);
2071 * searches a range in the state tree for a given mask.
2072 * If 'filled' == 1, this returns 1 only if every extent in the tree
2073 * has the bits set. Otherwise, 1 is returned if any bit in the
2074 * range is found set.
2076 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
2077 unsigned bits, int filled, struct extent_state *cached)
2079 struct extent_state *state = NULL;
2080 struct rb_node *node;
2083 spin_lock(&tree->lock);
2084 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
2085 cached->end > start)
2086 node = &cached->rb_node;
2088 node = tree_search(tree, start);
2089 while (node && start <= end) {
2090 state = rb_entry(node, struct extent_state, rb_node);
2092 if (filled && state->start > start) {
2097 if (state->start > end)
2100 if (state->state & bits) {
2104 } else if (filled) {
2109 if (state->end == (u64)-1)
2112 start = state->end + 1;
2115 node = rb_next(node);
2122 spin_unlock(&tree->lock);
2127 * helper function to set a given page up to date if all the
2128 * extents in the tree for that page are up to date
2130 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
2132 u64 start = page_offset(page);
2133 u64 end = start + PAGE_SIZE - 1;
2134 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
2135 SetPageUptodate(page);
2138 int free_io_failure(struct extent_io_tree *failure_tree,
2139 struct extent_io_tree *io_tree,
2140 struct io_failure_record *rec)
2145 set_state_failrec(failure_tree, rec->start, NULL);
2146 ret = clear_extent_bits(failure_tree, rec->start,
2147 rec->start + rec->len - 1,
2148 EXTENT_LOCKED | EXTENT_DIRTY);
2152 ret = clear_extent_bits(io_tree, rec->start,
2153 rec->start + rec->len - 1,
2163 * this bypasses the standard btrfs submit functions deliberately, as
2164 * the standard behavior is to write all copies in a raid setup. here we only
2165 * want to write the one bad copy. so we do the mapping for ourselves and issue
2166 * submit_bio directly.
2167 * to avoid any synchronization issues, wait for the data after writing, which
2168 * actually prevents the read that triggered the error from finishing.
2169 * currently, there can be no more than two copies of every data bit. thus,
2170 * exactly one rewrite is required.
2172 int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2173 u64 length, u64 logical, struct page *page,
2174 unsigned int pg_offset, int mirror_num)
2177 struct btrfs_device *dev;
2180 struct btrfs_bio *bbio = NULL;
2183 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
2184 BUG_ON(!mirror_num);
2186 bio = btrfs_io_bio_alloc(1);
2187 bio->bi_iter.bi_size = 0;
2188 map_length = length;
2191 * Avoid races with device replace and make sure our bbio has devices
2192 * associated to its stripes that don't go away while we are doing the
2193 * read repair operation.
2195 btrfs_bio_counter_inc_blocked(fs_info);
2196 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
2198 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2199 * to update all raid stripes, but here we just want to correct
2200 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2201 * stripe's dev and sector.
2203 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2204 &map_length, &bbio, 0);
2206 btrfs_bio_counter_dec(fs_info);
2210 ASSERT(bbio->mirror_num == 1);
2212 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2213 &map_length, &bbio, mirror_num);
2215 btrfs_bio_counter_dec(fs_info);
2219 BUG_ON(mirror_num != bbio->mirror_num);
2222 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
2223 bio->bi_iter.bi_sector = sector;
2224 dev = bbio->stripes[bbio->mirror_num - 1].dev;
2225 btrfs_put_bbio(bbio);
2226 if (!dev || !dev->bdev ||
2227 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
2228 btrfs_bio_counter_dec(fs_info);
2232 bio_set_dev(bio, dev->bdev);
2233 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
2234 bio_add_page(bio, page, length, pg_offset);
2236 if (btrfsic_submit_bio_wait(bio)) {
2237 /* try to remap that extent elsewhere? */
2238 btrfs_bio_counter_dec(fs_info);
2240 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
2244 btrfs_info_rl_in_rcu(fs_info,
2245 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
2247 rcu_str_deref(dev->name), sector);
2248 btrfs_bio_counter_dec(fs_info);
2253 int btrfs_repair_eb_io_failure(struct extent_buffer *eb, int mirror_num)
2255 struct btrfs_fs_info *fs_info = eb->fs_info;
2256 u64 start = eb->start;
2257 int i, num_pages = num_extent_pages(eb);
2260 if (sb_rdonly(fs_info->sb))
2263 for (i = 0; i < num_pages; i++) {
2264 struct page *p = eb->pages[i];
2266 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
2267 start - page_offset(p), mirror_num);
2277 * each time an IO finishes, we do a fast check in the IO failure tree
2278 * to see if we need to process or clean up an io_failure_record
2280 int clean_io_failure(struct btrfs_fs_info *fs_info,
2281 struct extent_io_tree *failure_tree,
2282 struct extent_io_tree *io_tree, u64 start,
2283 struct page *page, u64 ino, unsigned int pg_offset)
2286 struct io_failure_record *failrec;
2287 struct extent_state *state;
2292 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2297 ret = get_state_failrec(failure_tree, start, &failrec);
2301 BUG_ON(!failrec->this_mirror);
2303 if (failrec->in_validation) {
2304 /* there was no real error, just free the record */
2305 btrfs_debug(fs_info,
2306 "clean_io_failure: freeing dummy error at %llu",
2310 if (sb_rdonly(fs_info->sb))
2313 spin_lock(&io_tree->lock);
2314 state = find_first_extent_bit_state(io_tree,
2317 spin_unlock(&io_tree->lock);
2319 if (state && state->start <= failrec->start &&
2320 state->end >= failrec->start + failrec->len - 1) {
2321 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2323 if (num_copies > 1) {
2324 repair_io_failure(fs_info, ino, start, failrec->len,
2325 failrec->logical, page, pg_offset,
2326 failrec->failed_mirror);
2331 free_io_failure(failure_tree, io_tree, failrec);
2337 * Can be called when
2338 * - hold extent lock
2339 * - under ordered extent
2340 * - the inode is freeing
2342 void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
2344 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
2345 struct io_failure_record *failrec;
2346 struct extent_state *state, *next;
2348 if (RB_EMPTY_ROOT(&failure_tree->state))
2351 spin_lock(&failure_tree->lock);
2352 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2354 if (state->start > end)
2357 ASSERT(state->end <= end);
2359 next = next_state(state);
2361 failrec = state->failrec;
2362 free_extent_state(state);
2367 spin_unlock(&failure_tree->lock);
2370 int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
2371 struct io_failure_record **failrec_ret)
2373 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2374 struct io_failure_record *failrec;
2375 struct extent_map *em;
2376 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2377 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2378 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2382 ret = get_state_failrec(failure_tree, start, &failrec);
2384 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2388 failrec->start = start;
2389 failrec->len = end - start + 1;
2390 failrec->this_mirror = 0;
2391 failrec->bio_flags = 0;
2392 failrec->in_validation = 0;
2394 read_lock(&em_tree->lock);
2395 em = lookup_extent_mapping(em_tree, start, failrec->len);
2397 read_unlock(&em_tree->lock);
2402 if (em->start > start || em->start + em->len <= start) {
2403 free_extent_map(em);
2406 read_unlock(&em_tree->lock);
2412 logical = start - em->start;
2413 logical = em->block_start + logical;
2414 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2415 logical = em->block_start;
2416 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2417 extent_set_compress_type(&failrec->bio_flags,
2421 btrfs_debug(fs_info,
2422 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2423 logical, start, failrec->len);
2425 failrec->logical = logical;
2426 free_extent_map(em);
2428 /* set the bits in the private failure tree */
2429 ret = set_extent_bits(failure_tree, start, end,
2430 EXTENT_LOCKED | EXTENT_DIRTY);
2432 ret = set_state_failrec(failure_tree, start, failrec);
2433 /* set the bits in the inode's tree */
2435 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
2441 btrfs_debug(fs_info,
2442 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2443 failrec->logical, failrec->start, failrec->len,
2444 failrec->in_validation);
2446 * when data can be on disk more than twice, add to failrec here
2447 * (e.g. with a list for failed_mirror) to make
2448 * clean_io_failure() clean all those errors at once.
2452 *failrec_ret = failrec;
2457 bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
2458 struct io_failure_record *failrec, int failed_mirror)
2460 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2463 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
2464 if (num_copies == 1) {
2466 * we only have a single copy of the data, so don't bother with
2467 * all the retry and error correction code that follows. no
2468 * matter what the error is, it is very likely to persist.
2470 btrfs_debug(fs_info,
2471 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2472 num_copies, failrec->this_mirror, failed_mirror);
2477 * there are two premises:
2478 * a) deliver good data to the caller
2479 * b) correct the bad sectors on disk
2481 if (failed_bio_pages > 1) {
2483 * to fulfill b), we need to know the exact failing sectors, as
2484 * we don't want to rewrite any more than the failed ones. thus,
2485 * we need separate read requests for the failed bio
2487 * if the following BUG_ON triggers, our validation request got
2488 * merged. we need separate requests for our algorithm to work.
2490 BUG_ON(failrec->in_validation);
2491 failrec->in_validation = 1;
2492 failrec->this_mirror = failed_mirror;
2495 * we're ready to fulfill a) and b) alongside. get a good copy
2496 * of the failed sector and if we succeed, we have setup
2497 * everything for repair_io_failure to do the rest for us.
2499 if (failrec->in_validation) {
2500 BUG_ON(failrec->this_mirror != failed_mirror);
2501 failrec->in_validation = 0;
2502 failrec->this_mirror = 0;
2504 failrec->failed_mirror = failed_mirror;
2505 failrec->this_mirror++;
2506 if (failrec->this_mirror == failed_mirror)
2507 failrec->this_mirror++;
2510 if (failrec->this_mirror > num_copies) {
2511 btrfs_debug(fs_info,
2512 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2513 num_copies, failrec->this_mirror, failed_mirror);
2521 struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2522 struct io_failure_record *failrec,
2523 struct page *page, int pg_offset, int icsum,
2524 bio_end_io_t *endio_func, void *data)
2526 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2528 struct btrfs_io_bio *btrfs_failed_bio;
2529 struct btrfs_io_bio *btrfs_bio;
2531 bio = btrfs_io_bio_alloc(1);
2532 bio->bi_end_io = endio_func;
2533 bio->bi_iter.bi_sector = failrec->logical >> 9;
2534 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
2535 bio->bi_iter.bi_size = 0;
2536 bio->bi_private = data;
2538 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2539 if (btrfs_failed_bio->csum) {
2540 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2542 btrfs_bio = btrfs_io_bio(bio);
2543 btrfs_bio->csum = btrfs_bio->csum_inline;
2545 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
2549 bio_add_page(bio, page, failrec->len, pg_offset);
2555 * This is a generic handler for readpage errors. If other copies exist, read
2556 * those and write back good data to the failed position. Does not investigate
2557 * in remapping the failed extent elsewhere, hoping the device will be smart
2558 * enough to do this as needed
2560 static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2561 struct page *page, u64 start, u64 end,
2564 struct io_failure_record *failrec;
2565 struct inode *inode = page->mapping->host;
2566 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2567 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2570 blk_status_t status;
2572 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
2574 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
2576 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2580 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
2582 free_io_failure(failure_tree, tree, failrec);
2586 if (failed_bio_pages > 1)
2587 read_mode |= REQ_FAILFAST_DEV;
2589 phy_offset >>= inode->i_sb->s_blocksize_bits;
2590 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2591 start - page_offset(page),
2592 (int)phy_offset, failed_bio->bi_end_io,
2594 bio->bi_opf = REQ_OP_READ | read_mode;
2596 btrfs_debug(btrfs_sb(inode->i_sb),
2597 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2598 read_mode, failrec->this_mirror, failrec->in_validation);
2600 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
2601 failrec->bio_flags);
2603 free_io_failure(failure_tree, tree, failrec);
2605 ret = blk_status_to_errno(status);
2611 /* lots and lots of room for performance fixes in the end_bio funcs */
2613 void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2615 int uptodate = (err == 0);
2618 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
2621 ClearPageUptodate(page);
2623 ret = err < 0 ? err : -EIO;
2624 mapping_set_error(page->mapping, ret);
2629 * after a writepage IO is done, we need to:
2630 * clear the uptodate bits on error
2631 * clear the writeback bits in the extent tree for this IO
2632 * end_page_writeback if the page has no more pending IO
2634 * Scheduling is not allowed, so the extent state tree is expected
2635 * to have one and only one object corresponding to this IO.
2637 static void end_bio_extent_writepage(struct bio *bio)
2639 int error = blk_status_to_errno(bio->bi_status);
2640 struct bio_vec *bvec;
2643 struct bvec_iter_all iter_all;
2645 ASSERT(!bio_flagged(bio, BIO_CLONED));
2646 bio_for_each_segment_all(bvec, bio, iter_all) {
2647 struct page *page = bvec->bv_page;
2648 struct inode *inode = page->mapping->host;
2649 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2651 /* We always issue full-page reads, but if some block
2652 * in a page fails to read, blk_update_request() will
2653 * advance bv_offset and adjust bv_len to compensate.
2654 * Print a warning for nonzero offsets, and an error
2655 * if they don't add up to a full page. */
2656 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2657 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
2659 "partial page write in btrfs with offset %u and length %u",
2660 bvec->bv_offset, bvec->bv_len);
2663 "incomplete page write in btrfs with offset %u and length %u",
2664 bvec->bv_offset, bvec->bv_len);
2667 start = page_offset(page);
2668 end = start + bvec->bv_offset + bvec->bv_len - 1;
2670 end_extent_writepage(page, error, start, end);
2671 end_page_writeback(page);
2678 endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2681 struct extent_state *cached = NULL;
2682 u64 end = start + len - 1;
2684 if (uptodate && tree->track_uptodate)
2685 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2686 unlock_extent_cached_atomic(tree, start, end, &cached);
2690 * after a readpage IO is done, we need to:
2691 * clear the uptodate bits on error
2692 * set the uptodate bits if things worked
2693 * set the page up to date if all extents in the tree are uptodate
2694 * clear the lock bit in the extent tree
2695 * unlock the page if there are no other extents locked for it
2697 * Scheduling is not allowed, so the extent state tree is expected
2698 * to have one and only one object corresponding to this IO.
2700 static void end_bio_extent_readpage(struct bio *bio)
2702 struct bio_vec *bvec;
2703 int uptodate = !bio->bi_status;
2704 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
2705 struct extent_io_tree *tree, *failure_tree;
2710 u64 extent_start = 0;
2714 struct bvec_iter_all iter_all;
2716 ASSERT(!bio_flagged(bio, BIO_CLONED));
2717 bio_for_each_segment_all(bvec, bio, iter_all) {
2718 struct page *page = bvec->bv_page;
2719 struct inode *inode = page->mapping->host;
2720 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2721 bool data_inode = btrfs_ino(BTRFS_I(inode))
2722 != BTRFS_BTREE_INODE_OBJECTID;
2724 btrfs_debug(fs_info,
2725 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
2726 (u64)bio->bi_iter.bi_sector, bio->bi_status,
2727 io_bio->mirror_num);
2728 tree = &BTRFS_I(inode)->io_tree;
2729 failure_tree = &BTRFS_I(inode)->io_failure_tree;
2731 /* We always issue full-page reads, but if some block
2732 * in a page fails to read, blk_update_request() will
2733 * advance bv_offset and adjust bv_len to compensate.
2734 * Print a warning for nonzero offsets, and an error
2735 * if they don't add up to a full page. */
2736 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2737 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
2739 "partial page read in btrfs with offset %u and length %u",
2740 bvec->bv_offset, bvec->bv_len);
2743 "incomplete page read in btrfs with offset %u and length %u",
2744 bvec->bv_offset, bvec->bv_len);
2747 start = page_offset(page);
2748 end = start + bvec->bv_offset + bvec->bv_len - 1;
2751 mirror = io_bio->mirror_num;
2752 if (likely(uptodate)) {
2753 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2759 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2760 failure_tree, tree, start,
2762 btrfs_ino(BTRFS_I(inode)), 0);
2765 if (likely(uptodate))
2771 * The generic bio_readpage_error handles errors the
2772 * following way: If possible, new read requests are
2773 * created and submitted and will end up in
2774 * end_bio_extent_readpage as well (if we're lucky,
2775 * not in the !uptodate case). In that case it returns
2776 * 0 and we just go on with the next page in our bio.
2777 * If it can't handle the error it will return -EIO and
2778 * we remain responsible for that page.
2780 ret = bio_readpage_error(bio, offset, page, start, end,
2783 uptodate = !bio->bi_status;
2788 struct extent_buffer *eb;
2790 eb = (struct extent_buffer *)page->private;
2791 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2792 eb->read_mirror = mirror;
2793 atomic_dec(&eb->io_pages);
2794 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2796 btree_readahead_hook(eb, -EIO);
2799 if (likely(uptodate)) {
2800 loff_t i_size = i_size_read(inode);
2801 pgoff_t end_index = i_size >> PAGE_SHIFT;
2804 /* Zero out the end if this page straddles i_size */
2805 off = offset_in_page(i_size);
2806 if (page->index == end_index && off)
2807 zero_user_segment(page, off, PAGE_SIZE);
2808 SetPageUptodate(page);
2810 ClearPageUptodate(page);
2816 if (unlikely(!uptodate)) {
2818 endio_readpage_release_extent(tree,
2824 endio_readpage_release_extent(tree, start,
2825 end - start + 1, 0);
2826 } else if (!extent_len) {
2827 extent_start = start;
2828 extent_len = end + 1 - start;
2829 } else if (extent_start + extent_len == start) {
2830 extent_len += end + 1 - start;
2832 endio_readpage_release_extent(tree, extent_start,
2833 extent_len, uptodate);
2834 extent_start = start;
2835 extent_len = end + 1 - start;
2840 endio_readpage_release_extent(tree, extent_start, extent_len,
2842 btrfs_io_bio_free_csum(io_bio);
2847 * Initialize the members up to but not including 'bio'. Use after allocating a
2848 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2849 * 'bio' because use of __GFP_ZERO is not supported.
2851 static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
2853 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2857 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2858 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2859 * for the appropriate container_of magic
2861 struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
2865 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
2866 bio_set_dev(bio, bdev);
2867 bio->bi_iter.bi_sector = first_byte >> 9;
2868 btrfs_io_bio_init(btrfs_io_bio(bio));
2872 struct bio *btrfs_bio_clone(struct bio *bio)
2874 struct btrfs_io_bio *btrfs_bio;
2877 /* Bio allocation backed by a bioset does not fail */
2878 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
2879 btrfs_bio = btrfs_io_bio(new);
2880 btrfs_io_bio_init(btrfs_bio);
2881 btrfs_bio->iter = bio->bi_iter;
2885 struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
2889 /* Bio allocation backed by a bioset does not fail */
2890 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
2891 btrfs_io_bio_init(btrfs_io_bio(bio));
2895 struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
2898 struct btrfs_io_bio *btrfs_bio;
2900 /* this will never fail when it's backed by a bioset */
2901 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
2904 btrfs_bio = btrfs_io_bio(bio);
2905 btrfs_io_bio_init(btrfs_bio);
2907 bio_trim(bio, offset >> 9, size >> 9);
2908 btrfs_bio->iter = bio->bi_iter;
2913 * @opf: bio REQ_OP_* and REQ_* flags as one value
2914 * @tree: tree so we can call our merge_bio hook
2915 * @wbc: optional writeback control for io accounting
2916 * @page: page to add to the bio
2917 * @pg_offset: offset of the new bio or to check whether we are adding
2918 * a contiguous page to the previous one
2919 * @size: portion of page that we want to write
2920 * @offset: starting offset in the page
2921 * @bdev: attach newly created bios to this bdev
2922 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
2923 * @end_io_func: end_io callback for new bio
2924 * @mirror_num: desired mirror to read/write
2925 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2926 * @bio_flags: flags of the current bio to see if we can merge them
2928 static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
2929 struct writeback_control *wbc,
2930 struct page *page, u64 offset,
2931 size_t size, unsigned long pg_offset,
2932 struct block_device *bdev,
2933 struct bio **bio_ret,
2934 bio_end_io_t end_io_func,
2936 unsigned long prev_bio_flags,
2937 unsigned long bio_flags,
2938 bool force_bio_submit)
2942 size_t page_size = min_t(size_t, size, PAGE_SIZE);
2943 sector_t sector = offset >> 9;
2949 bool can_merge = true;
2952 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
2953 contig = bio->bi_iter.bi_sector == sector;
2955 contig = bio_end_sector(bio) == sector;
2958 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
2961 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
2963 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
2964 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
2972 wbc_account_io(wbc, page, page_size);
2977 bio = btrfs_bio_alloc(bdev, offset);
2978 bio_add_page(bio, page, page_size, pg_offset);
2979 bio->bi_end_io = end_io_func;
2980 bio->bi_private = tree;
2981 bio->bi_write_hint = page->mapping->host->i_write_hint;
2984 wbc_init_bio(wbc, bio);
2985 wbc_account_io(wbc, page, page_size);
2993 static void attach_extent_buffer_page(struct extent_buffer *eb,
2996 if (!PagePrivate(page)) {
2997 SetPagePrivate(page);
2999 set_page_private(page, (unsigned long)eb);
3001 WARN_ON(page->private != (unsigned long)eb);
3005 void set_page_extent_mapped(struct page *page)
3007 if (!PagePrivate(page)) {
3008 SetPagePrivate(page);
3010 set_page_private(page, EXTENT_PAGE_PRIVATE);
3014 static struct extent_map *
3015 __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
3016 u64 start, u64 len, get_extent_t *get_extent,
3017 struct extent_map **em_cached)
3019 struct extent_map *em;
3021 if (em_cached && *em_cached) {
3023 if (extent_map_in_tree(em) && start >= em->start &&
3024 start < extent_map_end(em)) {
3025 refcount_inc(&em->refs);
3029 free_extent_map(em);
3033 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
3034 if (em_cached && !IS_ERR_OR_NULL(em)) {
3036 refcount_inc(&em->refs);
3042 * basic readpage implementation. Locked extent state structs are inserted
3043 * into the tree that are removed when the IO is done (by the end_io
3045 * XXX JDM: This needs looking at to ensure proper page locking
3046 * return 0 on success, otherwise return error
3048 static int __do_readpage(struct extent_io_tree *tree,
3050 get_extent_t *get_extent,
3051 struct extent_map **em_cached,
3052 struct bio **bio, int mirror_num,
3053 unsigned long *bio_flags, unsigned int read_flags,
3056 struct inode *inode = page->mapping->host;
3057 u64 start = page_offset(page);
3058 const u64 end = start + PAGE_SIZE - 1;
3061 u64 last_byte = i_size_read(inode);
3064 struct extent_map *em;
3065 struct block_device *bdev;
3068 size_t pg_offset = 0;
3070 size_t disk_io_size;
3071 size_t blocksize = inode->i_sb->s_blocksize;
3072 unsigned long this_bio_flag = 0;
3074 set_page_extent_mapped(page);
3076 if (!PageUptodate(page)) {
3077 if (cleancache_get_page(page) == 0) {
3078 BUG_ON(blocksize != PAGE_SIZE);
3079 unlock_extent(tree, start, end);
3084 if (page->index == last_byte >> PAGE_SHIFT) {
3086 size_t zero_offset = offset_in_page(last_byte);
3089 iosize = PAGE_SIZE - zero_offset;
3090 userpage = kmap_atomic(page);
3091 memset(userpage + zero_offset, 0, iosize);
3092 flush_dcache_page(page);
3093 kunmap_atomic(userpage);
3096 while (cur <= end) {
3097 bool force_bio_submit = false;
3100 if (cur >= last_byte) {
3102 struct extent_state *cached = NULL;
3104 iosize = PAGE_SIZE - pg_offset;
3105 userpage = kmap_atomic(page);
3106 memset(userpage + pg_offset, 0, iosize);
3107 flush_dcache_page(page);
3108 kunmap_atomic(userpage);
3109 set_extent_uptodate(tree, cur, cur + iosize - 1,
3111 unlock_extent_cached(tree, cur,
3112 cur + iosize - 1, &cached);
3115 em = __get_extent_map(inode, page, pg_offset, cur,
3116 end - cur + 1, get_extent, em_cached);
3117 if (IS_ERR_OR_NULL(em)) {
3119 unlock_extent(tree, cur, end);
3122 extent_offset = cur - em->start;
3123 BUG_ON(extent_map_end(em) <= cur);
3126 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
3127 this_bio_flag |= EXTENT_BIO_COMPRESSED;
3128 extent_set_compress_type(&this_bio_flag,
3132 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3133 cur_end = min(extent_map_end(em) - 1, end);
3134 iosize = ALIGN(iosize, blocksize);