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 if (tree
->ops
&& tree
->ops
->check_extent_io_range
)
93 tree
->ops
->check_extent_io_range(tree
->private_data
, caller
,
97 #define btrfs_leak_debug_add(new, head) do {} while (0)
98 #define btrfs_leak_debug_del(entry) do {} while (0)
99 #define btrfs_leak_debug_check() do {} while (0)
100 #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
103 #define BUFFER_LRU_MAX 64
108 struct rb_node rb_node
;
111 struct extent_page_data
{
113 struct extent_io_tree
*tree
;
114 /* tells writepage not to lock the state bits for this range
115 * it still does the unlocking
117 unsigned int extent_locked
:1;
119 /* tells the submit_bio code to use REQ_SYNC */
120 unsigned int sync_io
:1;
123 static int add_extent_changeset(struct extent_state
*state
, unsigned bits
,
124 struct extent_changeset
*changeset
,
131 if (set
&& (state
->state
& bits
) == bits
)
133 if (!set
&& (state
->state
& bits
) == 0)
135 changeset
->bytes_changed
+= state
->end
- state
->start
+ 1;
136 ret
= ulist_add(&changeset
->range_changed
, state
->start
, state
->end
,
141 static void flush_write_bio(struct extent_page_data
*epd
);
143 int __init
extent_io_init(void)
145 extent_state_cache
= kmem_cache_create("btrfs_extent_state",
146 sizeof(struct extent_state
), 0,
147 SLAB_MEM_SPREAD
, NULL
);
148 if (!extent_state_cache
)
151 extent_buffer_cache
= kmem_cache_create("btrfs_extent_buffer",
152 sizeof(struct extent_buffer
), 0,
153 SLAB_MEM_SPREAD
, NULL
);
154 if (!extent_buffer_cache
)
155 goto free_state_cache
;
157 if (bioset_init(&btrfs_bioset
, BIO_POOL_SIZE
,
158 offsetof(struct btrfs_io_bio
, bio
),
160 goto free_buffer_cache
;
162 if (bioset_integrity_create(&btrfs_bioset
, BIO_POOL_SIZE
))
168 bioset_exit(&btrfs_bioset
);
171 kmem_cache_destroy(extent_buffer_cache
);
172 extent_buffer_cache
= NULL
;
175 kmem_cache_destroy(extent_state_cache
);
176 extent_state_cache
= NULL
;
180 void __cold
extent_io_exit(void)
182 btrfs_leak_debug_check();
185 * Make sure all delayed rcu free are flushed before we
189 kmem_cache_destroy(extent_state_cache
);
190 kmem_cache_destroy(extent_buffer_cache
);
191 bioset_exit(&btrfs_bioset
);
194 void extent_io_tree_init(struct extent_io_tree
*tree
,
197 tree
->state
= RB_ROOT
;
199 tree
->dirty_bytes
= 0;
200 spin_lock_init(&tree
->lock
);
201 tree
->private_data
= private_data
;
204 static struct extent_state
*alloc_extent_state(gfp_t mask
)
206 struct extent_state
*state
;
209 * The given mask might be not appropriate for the slab allocator,
210 * drop the unsupported bits
212 mask
&= ~(__GFP_DMA32
|__GFP_HIGHMEM
);
213 state
= kmem_cache_alloc(extent_state_cache
, mask
);
217 state
->failrec
= NULL
;
218 RB_CLEAR_NODE(&state
->rb_node
);
219 btrfs_leak_debug_add(&state
->leak_list
, &states
);
220 refcount_set(&state
->refs
, 1);
221 init_waitqueue_head(&state
->wq
);
222 trace_alloc_extent_state(state
, mask
, _RET_IP_
);
226 void free_extent_state(struct extent_state
*state
)
230 if (refcount_dec_and_test(&state
->refs
)) {
231 WARN_ON(extent_state_in_tree(state
));
232 btrfs_leak_debug_del(&state
->leak_list
);
233 trace_free_extent_state(state
, _RET_IP_
);
234 kmem_cache_free(extent_state_cache
, state
);
238 static struct rb_node
*tree_insert(struct rb_root
*root
,
239 struct rb_node
*search_start
,
241 struct rb_node
*node
,
242 struct rb_node
***p_in
,
243 struct rb_node
**parent_in
)
246 struct rb_node
*parent
= NULL
;
247 struct tree_entry
*entry
;
249 if (p_in
&& parent_in
) {
255 p
= search_start
? &search_start
: &root
->rb_node
;
258 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
260 if (offset
< entry
->start
)
262 else if (offset
> entry
->end
)
269 rb_link_node(node
, parent
, p
);
270 rb_insert_color(node
, root
);
274 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
275 struct rb_node
**prev_ret
,
276 struct rb_node
**next_ret
,
277 struct rb_node
***p_ret
,
278 struct rb_node
**parent_ret
)
280 struct rb_root
*root
= &tree
->state
;
281 struct rb_node
**n
= &root
->rb_node
;
282 struct rb_node
*prev
= NULL
;
283 struct rb_node
*orig_prev
= NULL
;
284 struct tree_entry
*entry
;
285 struct tree_entry
*prev_entry
= NULL
;
289 entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
292 if (offset
< entry
->start
)
294 else if (offset
> entry
->end
)
307 while (prev
&& offset
> prev_entry
->end
) {
308 prev
= rb_next(prev
);
309 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
316 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
317 while (prev
&& offset
< prev_entry
->start
) {
318 prev
= rb_prev(prev
);
319 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
326 static inline struct rb_node
*
327 tree_search_for_insert(struct extent_io_tree
*tree
,
329 struct rb_node
***p_ret
,
330 struct rb_node
**parent_ret
)
332 struct rb_node
*prev
= NULL
;
335 ret
= __etree_search(tree
, offset
, &prev
, NULL
, p_ret
, parent_ret
);
341 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
344 return tree_search_for_insert(tree
, offset
, NULL
, NULL
);
347 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
348 struct extent_state
*other
)
350 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
351 tree
->ops
->merge_extent_hook(tree
->private_data
, new, other
);
355 * utility function to look for merge candidates inside a given range.
356 * Any extents with matching state are merged together into a single
357 * extent in the tree. Extents with EXTENT_IO in their state field
358 * are not merged because the end_io handlers need to be able to do
359 * operations on them without sleeping (or doing allocations/splits).
361 * This should be called with the tree lock held.
363 static void merge_state(struct extent_io_tree
*tree
,
364 struct extent_state
*state
)
366 struct extent_state
*other
;
367 struct rb_node
*other_node
;
369 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
372 other_node
= rb_prev(&state
->rb_node
);
374 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
375 if (other
->end
== state
->start
- 1 &&
376 other
->state
== state
->state
) {
377 merge_cb(tree
, state
, other
);
378 state
->start
= other
->start
;
379 rb_erase(&other
->rb_node
, &tree
->state
);
380 RB_CLEAR_NODE(&other
->rb_node
);
381 free_extent_state(other
);
384 other_node
= rb_next(&state
->rb_node
);
386 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
387 if (other
->start
== state
->end
+ 1 &&
388 other
->state
== state
->state
) {
389 merge_cb(tree
, state
, other
);
390 state
->end
= other
->end
;
391 rb_erase(&other
->rb_node
, &tree
->state
);
392 RB_CLEAR_NODE(&other
->rb_node
);
393 free_extent_state(other
);
398 static void set_state_cb(struct extent_io_tree
*tree
,
399 struct extent_state
*state
, unsigned *bits
)
401 if (tree
->ops
&& tree
->ops
->set_bit_hook
)
402 tree
->ops
->set_bit_hook(tree
->private_data
, state
, bits
);
405 static void clear_state_cb(struct extent_io_tree
*tree
,
406 struct extent_state
*state
, unsigned *bits
)
408 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
409 tree
->ops
->clear_bit_hook(tree
->private_data
, state
, bits
);
412 static void set_state_bits(struct extent_io_tree
*tree
,
413 struct extent_state
*state
, unsigned *bits
,
414 struct extent_changeset
*changeset
);
417 * insert an extent_state struct into the tree. 'bits' are set on the
418 * struct before it is inserted.
420 * This may return -EEXIST if the extent is already there, in which case the
421 * state struct is freed.
423 * The tree lock is not taken internally. This is a utility function and
424 * probably isn't what you want to call (see set/clear_extent_bit).
426 static int insert_state(struct extent_io_tree
*tree
,
427 struct extent_state
*state
, u64 start
, u64 end
,
429 struct rb_node
**parent
,
430 unsigned *bits
, struct extent_changeset
*changeset
)
432 struct rb_node
*node
;
435 WARN(1, KERN_ERR
"BTRFS: end < start %llu %llu\n",
437 state
->start
= start
;
440 set_state_bits(tree
, state
, bits
, changeset
);
442 node
= tree_insert(&tree
->state
, NULL
, end
, &state
->rb_node
, p
, parent
);
444 struct extent_state
*found
;
445 found
= rb_entry(node
, struct extent_state
, rb_node
);
446 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
447 found
->start
, found
->end
, start
, end
);
450 merge_state(tree
, state
);
454 static void split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
457 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
458 tree
->ops
->split_extent_hook(tree
->private_data
, orig
, split
);
462 * split a given extent state struct in two, inserting the preallocated
463 * struct 'prealloc' as the newly created second half. 'split' indicates an
464 * offset inside 'orig' where it should be split.
467 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
468 * are two extent state structs in the tree:
469 * prealloc: [orig->start, split - 1]
470 * orig: [ split, orig->end ]
472 * The tree locks are not taken by this function. They need to be held
475 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
476 struct extent_state
*prealloc
, u64 split
)
478 struct rb_node
*node
;
480 split_cb(tree
, orig
, split
);
482 prealloc
->start
= orig
->start
;
483 prealloc
->end
= split
- 1;
484 prealloc
->state
= orig
->state
;
487 node
= tree_insert(&tree
->state
, &orig
->rb_node
, prealloc
->end
,
488 &prealloc
->rb_node
, NULL
, NULL
);
490 free_extent_state(prealloc
);
496 static struct extent_state
*next_state(struct extent_state
*state
)
498 struct rb_node
*next
= rb_next(&state
->rb_node
);
500 return rb_entry(next
, struct extent_state
, rb_node
);
506 * utility function to clear some bits in an extent state struct.
507 * it will optionally wake up any one waiting on this state (wake == 1).
509 * If no bits are set on the state struct after clearing things, the
510 * struct is freed and removed from the tree
512 static struct extent_state
*clear_state_bit(struct extent_io_tree
*tree
,
513 struct extent_state
*state
,
514 unsigned *bits
, int wake
,
515 struct extent_changeset
*changeset
)
517 struct extent_state
*next
;
518 unsigned bits_to_clear
= *bits
& ~EXTENT_CTLBITS
;
521 if ((bits_to_clear
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
522 u64 range
= state
->end
- state
->start
+ 1;
523 WARN_ON(range
> tree
->dirty_bytes
);
524 tree
->dirty_bytes
-= range
;
526 clear_state_cb(tree
, state
, bits
);
527 ret
= add_extent_changeset(state
, bits_to_clear
, changeset
, 0);
529 state
->state
&= ~bits_to_clear
;
532 if (state
->state
== 0) {
533 next
= next_state(state
);
534 if (extent_state_in_tree(state
)) {
535 rb_erase(&state
->rb_node
, &tree
->state
);
536 RB_CLEAR_NODE(&state
->rb_node
);
537 free_extent_state(state
);
542 merge_state(tree
, state
);
543 next
= next_state(state
);
548 static struct extent_state
*
549 alloc_extent_state_atomic(struct extent_state
*prealloc
)
552 prealloc
= alloc_extent_state(GFP_ATOMIC
);
557 static void extent_io_tree_panic(struct extent_io_tree
*tree
, int err
)
559 struct inode
*inode
= tree
->private_data
;
561 btrfs_panic(btrfs_sb(inode
->i_sb
), err
,
562 "locking error: extent tree was modified by another thread while locked");
566 * clear some bits on a range in the tree. This may require splitting
567 * or inserting elements in the tree, so the gfp mask is used to
568 * indicate which allocations or sleeping are allowed.
570 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
571 * the given range from the tree regardless of state (ie for truncate).
573 * the range [start, end] is inclusive.
575 * This takes the tree lock, and returns 0 on success and < 0 on error.
577 int __clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
578 unsigned bits
, int wake
, int delete,
579 struct extent_state
**cached_state
,
580 gfp_t mask
, struct extent_changeset
*changeset
)
582 struct extent_state
*state
;
583 struct extent_state
*cached
;
584 struct extent_state
*prealloc
= NULL
;
585 struct rb_node
*node
;
590 btrfs_debug_check_extent_io_range(tree
, start
, end
);
592 if (bits
& EXTENT_DELALLOC
)
593 bits
|= EXTENT_NORESERVE
;
596 bits
|= ~EXTENT_CTLBITS
;
597 bits
|= EXTENT_FIRST_DELALLOC
;
599 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
602 if (!prealloc
&& gfpflags_allow_blocking(mask
)) {
604 * Don't care for allocation failure here because we might end
605 * up not needing the pre-allocated extent state at all, which
606 * is the case if we only have in the tree extent states that
607 * cover our input range and don't cover too any other range.
608 * If we end up needing a new extent state we allocate it later.
610 prealloc
= alloc_extent_state(mask
);
613 spin_lock(&tree
->lock
);
615 cached
= *cached_state
;
618 *cached_state
= NULL
;
622 if (cached
&& extent_state_in_tree(cached
) &&
623 cached
->start
<= start
&& cached
->end
> start
) {
625 refcount_dec(&cached
->refs
);
630 free_extent_state(cached
);
633 * this search will find the extents that end after
636 node
= tree_search(tree
, start
);
639 state
= rb_entry(node
, struct extent_state
, rb_node
);
641 if (state
->start
> end
)
643 WARN_ON(state
->end
< start
);
644 last_end
= state
->end
;
646 /* the state doesn't have the wanted bits, go ahead */
647 if (!(state
->state
& bits
)) {
648 state
= next_state(state
);
653 * | ---- desired range ---- |
655 * | ------------- state -------------- |
657 * We need to split the extent we found, and may flip
658 * bits on second half.
660 * If the extent we found extends past our range, we
661 * just split and search again. It'll get split again
662 * the next time though.
664 * If the extent we found is inside our range, we clear
665 * the desired bit on it.
668 if (state
->start
< start
) {
669 prealloc
= alloc_extent_state_atomic(prealloc
);
671 err
= split_state(tree
, state
, prealloc
, start
);
673 extent_io_tree_panic(tree
, err
);
678 if (state
->end
<= end
) {
679 state
= clear_state_bit(tree
, state
, &bits
, wake
,
686 * | ---- desired range ---- |
688 * We need to split the extent, and clear the bit
691 if (state
->start
<= end
&& state
->end
> end
) {
692 prealloc
= alloc_extent_state_atomic(prealloc
);
694 err
= split_state(tree
, state
, prealloc
, end
+ 1);
696 extent_io_tree_panic(tree
, err
);
701 clear_state_bit(tree
, prealloc
, &bits
, wake
, changeset
);
707 state
= clear_state_bit(tree
, state
, &bits
, wake
, changeset
);
709 if (last_end
== (u64
)-1)
711 start
= last_end
+ 1;
712 if (start
<= end
&& state
&& !need_resched())
718 spin_unlock(&tree
->lock
);
719 if (gfpflags_allow_blocking(mask
))
724 spin_unlock(&tree
->lock
);
726 free_extent_state(prealloc
);
732 static void wait_on_state(struct extent_io_tree
*tree
,
733 struct extent_state
*state
)
734 __releases(tree
->lock
)
735 __acquires(tree
->lock
)
738 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
739 spin_unlock(&tree
->lock
);
741 spin_lock(&tree
->lock
);
742 finish_wait(&state
->wq
, &wait
);
746 * waits for one or more bits to clear on a range in the state tree.
747 * The range [start, end] is inclusive.
748 * The tree lock is taken by this function
750 static void wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
753 struct extent_state
*state
;
754 struct rb_node
*node
;
756 btrfs_debug_check_extent_io_range(tree
, start
, end
);
758 spin_lock(&tree
->lock
);
762 * this search will find all the extents that end after
765 node
= tree_search(tree
, start
);
770 state
= rb_entry(node
, struct extent_state
, rb_node
);
772 if (state
->start
> end
)
775 if (state
->state
& bits
) {
776 start
= state
->start
;
777 refcount_inc(&state
->refs
);
778 wait_on_state(tree
, state
);
779 free_extent_state(state
);
782 start
= state
->end
+ 1;
787 if (!cond_resched_lock(&tree
->lock
)) {
788 node
= rb_next(node
);
793 spin_unlock(&tree
->lock
);
796 static void set_state_bits(struct extent_io_tree
*tree
,
797 struct extent_state
*state
,
798 unsigned *bits
, struct extent_changeset
*changeset
)
800 unsigned bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
803 set_state_cb(tree
, state
, bits
);
804 if ((bits_to_set
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
805 u64 range
= state
->end
- state
->start
+ 1;
806 tree
->dirty_bytes
+= range
;
808 ret
= add_extent_changeset(state
, bits_to_set
, changeset
, 1);
810 state
->state
|= bits_to_set
;
813 static void cache_state_if_flags(struct extent_state
*state
,
814 struct extent_state
**cached_ptr
,
817 if (cached_ptr
&& !(*cached_ptr
)) {
818 if (!flags
|| (state
->state
& flags
)) {
820 refcount_inc(&state
->refs
);
825 static void cache_state(struct extent_state
*state
,
826 struct extent_state
**cached_ptr
)
828 return cache_state_if_flags(state
, cached_ptr
,
829 EXTENT_IOBITS
| EXTENT_BOUNDARY
);
833 * set some bits on a range in the tree. This may require allocations or
834 * sleeping, so the gfp mask is used to indicate what is allowed.
836 * If any of the exclusive bits are set, this will fail with -EEXIST if some
837 * part of the range already has the desired bits set. The start of the
838 * existing range is returned in failed_start in this case.
840 * [start, end] is inclusive This takes the tree lock.
843 static int __must_check
844 __set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
845 unsigned bits
, unsigned exclusive_bits
,
846 u64
*failed_start
, struct extent_state
**cached_state
,
847 gfp_t mask
, struct extent_changeset
*changeset
)
849 struct extent_state
*state
;
850 struct extent_state
*prealloc
= NULL
;
851 struct rb_node
*node
;
853 struct rb_node
*parent
;
858 btrfs_debug_check_extent_io_range(tree
, start
, end
);
860 bits
|= EXTENT_FIRST_DELALLOC
;
862 if (!prealloc
&& gfpflags_allow_blocking(mask
)) {
864 * Don't care for allocation failure here because we might end
865 * up not needing the pre-allocated extent state at all, which
866 * is the case if we only have in the tree extent states that
867 * cover our input range and don't cover too any other range.
868 * If we end up needing a new extent state we allocate it later.
870 prealloc
= alloc_extent_state(mask
);
873 spin_lock(&tree
->lock
);
874 if (cached_state
&& *cached_state
) {
875 state
= *cached_state
;
876 if (state
->start
<= start
&& state
->end
> start
&&
877 extent_state_in_tree(state
)) {
878 node
= &state
->rb_node
;
883 * this search will find all the extents that end after
886 node
= tree_search_for_insert(tree
, start
, &p
, &parent
);
888 prealloc
= alloc_extent_state_atomic(prealloc
);
890 err
= insert_state(tree
, prealloc
, start
, end
,
891 &p
, &parent
, &bits
, changeset
);
893 extent_io_tree_panic(tree
, err
);
895 cache_state(prealloc
, cached_state
);
899 state
= rb_entry(node
, struct extent_state
, rb_node
);
901 last_start
= state
->start
;
902 last_end
= state
->end
;
905 * | ---- desired range ---- |
908 * Just lock what we found and keep going
910 if (state
->start
== start
&& state
->end
<= end
) {
911 if (state
->state
& exclusive_bits
) {
912 *failed_start
= state
->start
;
917 set_state_bits(tree
, state
, &bits
, changeset
);
918 cache_state(state
, cached_state
);
919 merge_state(tree
, state
);
920 if (last_end
== (u64
)-1)
922 start
= last_end
+ 1;
923 state
= next_state(state
);
924 if (start
< end
&& state
&& state
->start
== start
&&
931 * | ---- desired range ---- |
934 * | ------------- state -------------- |
936 * We need to split the extent we found, and may flip bits on
939 * If the extent we found extends past our
940 * range, we just split and search again. It'll get split
941 * again the next time though.
943 * If the extent we found is inside our range, we set the
946 if (state
->start
< start
) {
947 if (state
->state
& exclusive_bits
) {
948 *failed_start
= start
;
953 prealloc
= alloc_extent_state_atomic(prealloc
);
955 err
= split_state(tree
, state
, prealloc
, start
);
957 extent_io_tree_panic(tree
, err
);
962 if (state
->end
<= end
) {
963 set_state_bits(tree
, state
, &bits
, changeset
);
964 cache_state(state
, cached_state
);
965 merge_state(tree
, state
);
966 if (last_end
== (u64
)-1)
968 start
= last_end
+ 1;
969 state
= next_state(state
);
970 if (start
< end
&& state
&& state
->start
== start
&&
977 * | ---- desired range ---- |
978 * | state | or | state |
980 * There's a hole, we need to insert something in it and
981 * ignore the extent we found.
983 if (state
->start
> start
) {
985 if (end
< last_start
)
988 this_end
= last_start
- 1;
990 prealloc
= alloc_extent_state_atomic(prealloc
);
994 * Avoid to free 'prealloc' if it can be merged with
997 err
= insert_state(tree
, prealloc
, start
, this_end
,
998 NULL
, NULL
, &bits
, changeset
);
1000 extent_io_tree_panic(tree
, err
);
1002 cache_state(prealloc
, cached_state
);
1004 start
= this_end
+ 1;
1008 * | ---- desired range ---- |
1010 * We need to split the extent, and set the bit
1013 if (state
->start
<= end
&& state
->end
> end
) {
1014 if (state
->state
& exclusive_bits
) {
1015 *failed_start
= start
;
1020 prealloc
= alloc_extent_state_atomic(prealloc
);
1022 err
= split_state(tree
, state
, prealloc
, end
+ 1);
1024 extent_io_tree_panic(tree
, err
);
1026 set_state_bits(tree
, prealloc
, &bits
, changeset
);
1027 cache_state(prealloc
, cached_state
);
1028 merge_state(tree
, prealloc
);
1036 spin_unlock(&tree
->lock
);
1037 if (gfpflags_allow_blocking(mask
))
1042 spin_unlock(&tree
->lock
);
1044 free_extent_state(prealloc
);
1050 int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1051 unsigned bits
, u64
* failed_start
,
1052 struct extent_state
**cached_state
, gfp_t mask
)
1054 return __set_extent_bit(tree
, start
, end
, bits
, 0, failed_start
,
1055 cached_state
, mask
, NULL
);
1060 * convert_extent_bit - convert all bits in a given range from one bit to
1062 * @tree: the io tree to search
1063 * @start: the start offset in bytes
1064 * @end: the end offset in bytes (inclusive)
1065 * @bits: the bits to set in this range
1066 * @clear_bits: the bits to clear in this range
1067 * @cached_state: state that we're going to cache
1069 * This will go through and set bits for the given range. If any states exist
1070 * already in this range they are set with the given bit and cleared of the
1071 * clear_bits. This is only meant to be used by things that are mergeable, ie
1072 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1073 * boundary bits like LOCK.
1075 * All allocations are done with GFP_NOFS.
1077 int convert_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1078 unsigned bits
, unsigned clear_bits
,
1079 struct extent_state
**cached_state
)
1081 struct extent_state
*state
;
1082 struct extent_state
*prealloc
= NULL
;
1083 struct rb_node
*node
;
1085 struct rb_node
*parent
;
1089 bool first_iteration
= true;
1091 btrfs_debug_check_extent_io_range(tree
, start
, end
);
1096 * Best effort, don't worry if extent state allocation fails
1097 * here for the first iteration. We might have a cached state
1098 * that matches exactly the target range, in which case no
1099 * extent state allocations are needed. We'll only know this
1100 * after locking the tree.
1102 prealloc
= alloc_extent_state(GFP_NOFS
);
1103 if (!prealloc
&& !first_iteration
)
1107 spin_lock(&tree
->lock
);
1108 if (cached_state
&& *cached_state
) {
1109 state
= *cached_state
;
1110 if (state
->start
<= start
&& state
->end
> start
&&
1111 extent_state_in_tree(state
)) {
1112 node
= &state
->rb_node
;
1118 * this search will find all the extents that end after
1121 node
= tree_search_for_insert(tree
, start
, &p
, &parent
);
1123 prealloc
= alloc_extent_state_atomic(prealloc
);
1128 err
= insert_state(tree
, prealloc
, start
, end
,
1129 &p
, &parent
, &bits
, NULL
);
1131 extent_io_tree_panic(tree
, err
);
1132 cache_state(prealloc
, cached_state
);
1136 state
= rb_entry(node
, struct extent_state
, rb_node
);
1138 last_start
= state
->start
;
1139 last_end
= state
->end
;
1142 * | ---- desired range ---- |
1145 * Just lock what we found and keep going
1147 if (state
->start
== start
&& state
->end
<= end
) {
1148 set_state_bits(tree
, state
, &bits
, NULL
);
1149 cache_state(state
, cached_state
);
1150 state
= clear_state_bit(tree
, state
, &clear_bits
, 0, NULL
);
1151 if (last_end
== (u64
)-1)
1153 start
= last_end
+ 1;
1154 if (start
< end
&& state
&& state
->start
== start
&&
1161 * | ---- desired range ---- |
1164 * | ------------- state -------------- |
1166 * We need to split the extent we found, and may flip bits on
1169 * If the extent we found extends past our
1170 * range, we just split and search again. It'll get split
1171 * again the next time though.
1173 * If the extent we found is inside our range, we set the
1174 * desired bit on it.
1176 if (state
->start
< start
) {
1177 prealloc
= alloc_extent_state_atomic(prealloc
);
1182 err
= split_state(tree
, state
, prealloc
, start
);
1184 extent_io_tree_panic(tree
, err
);
1188 if (state
->end
<= end
) {
1189 set_state_bits(tree
, state
, &bits
, NULL
);
1190 cache_state(state
, cached_state
);
1191 state
= clear_state_bit(tree
, state
, &clear_bits
, 0,
1193 if (last_end
== (u64
)-1)
1195 start
= last_end
+ 1;
1196 if (start
< end
&& state
&& state
->start
== start
&&
1203 * | ---- desired range ---- |
1204 * | state | or | state |
1206 * There's a hole, we need to insert something in it and
1207 * ignore the extent we found.
1209 if (state
->start
> start
) {
1211 if (end
< last_start
)
1214 this_end
= last_start
- 1;
1216 prealloc
= alloc_extent_state_atomic(prealloc
);
1223 * Avoid to free 'prealloc' if it can be merged with
1226 err
= insert_state(tree
, prealloc
, start
, this_end
,
1227 NULL
, NULL
, &bits
, NULL
);
1229 extent_io_tree_panic(tree
, err
);
1230 cache_state(prealloc
, cached_state
);
1232 start
= this_end
+ 1;
1236 * | ---- desired range ---- |
1238 * We need to split the extent, and set the bit
1241 if (state
->start
<= end
&& state
->end
> end
) {
1242 prealloc
= alloc_extent_state_atomic(prealloc
);
1248 err
= split_state(tree
, state
, prealloc
, end
+ 1);
1250 extent_io_tree_panic(tree
, err
);
1252 set_state_bits(tree
, prealloc
, &bits
, NULL
);
1253 cache_state(prealloc
, cached_state
);
1254 clear_state_bit(tree
, prealloc
, &clear_bits
, 0, NULL
);
1262 spin_unlock(&tree
->lock
);
1264 first_iteration
= false;
1268 spin_unlock(&tree
->lock
);
1270 free_extent_state(prealloc
);
1275 /* wrappers around set/clear extent bit */
1276 int set_record_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1277 unsigned bits
, struct extent_changeset
*changeset
)
1280 * We don't support EXTENT_LOCKED yet, as current changeset will
1281 * record any bits changed, so for EXTENT_LOCKED case, it will
1282 * either fail with -EEXIST or changeset will record the whole
1285 BUG_ON(bits
& EXTENT_LOCKED
);
1287 return __set_extent_bit(tree
, start
, end
, bits
, 0, NULL
, NULL
, GFP_NOFS
,
1291 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1292 unsigned bits
, int wake
, int delete,
1293 struct extent_state
**cached
)
1295 return __clear_extent_bit(tree
, start
, end
, bits
, wake
, delete,
1296 cached
, GFP_NOFS
, NULL
);
1299 int clear_record_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1300 unsigned bits
, struct extent_changeset
*changeset
)
1303 * Don't support EXTENT_LOCKED case, same reason as
1304 * set_record_extent_bits().
1306 BUG_ON(bits
& EXTENT_LOCKED
);
1308 return __clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, GFP_NOFS
,
1313 * either insert or lock state struct between start and end use mask to tell
1314 * us if waiting is desired.
1316 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1317 struct extent_state
**cached_state
)
1323 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
,
1324 EXTENT_LOCKED
, &failed_start
,
1325 cached_state
, GFP_NOFS
, NULL
);
1326 if (err
== -EEXIST
) {
1327 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
1328 start
= failed_start
;
1331 WARN_ON(start
> end
);
1336 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1341 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1342 &failed_start
, NULL
, GFP_NOFS
, NULL
);
1343 if (err
== -EEXIST
) {
1344 if (failed_start
> start
)
1345 clear_extent_bit(tree
, start
, failed_start
- 1,
1346 EXTENT_LOCKED
, 1, 0, NULL
);
1352 void extent_range_clear_dirty_for_io(struct inode
*inode
, u64 start
, u64 end
)
1354 unsigned long index
= start
>> PAGE_SHIFT
;
1355 unsigned long end_index
= end
>> PAGE_SHIFT
;
1358 while (index
<= end_index
) {
1359 page
= find_get_page(inode
->i_mapping
, index
);
1360 BUG_ON(!page
); /* Pages should be in the extent_io_tree */
1361 clear_page_dirty_for_io(page
);
1367 void extent_range_redirty_for_io(struct inode
*inode
, u64 start
, u64 end
)
1369 unsigned long index
= start
>> PAGE_SHIFT
;
1370 unsigned long end_index
= end
>> PAGE_SHIFT
;
1373 while (index
<= end_index
) {
1374 page
= find_get_page(inode
->i_mapping
, index
);
1375 BUG_ON(!page
); /* Pages should be in the extent_io_tree */
1376 __set_page_dirty_nobuffers(page
);
1377 account_page_redirty(page
);
1383 /* find the first state struct with 'bits' set after 'start', and
1384 * return it. tree->lock must be held. NULL will returned if
1385 * nothing was found after 'start'
1387 static struct extent_state
*
1388 find_first_extent_bit_state(struct extent_io_tree
*tree
,
1389 u64 start
, unsigned bits
)
1391 struct rb_node
*node
;
1392 struct extent_state
*state
;
1395 * this search will find all the extents that end after
1398 node
= tree_search(tree
, start
);
1403 state
= rb_entry(node
, struct extent_state
, rb_node
);
1404 if (state
->end
>= start
&& (state
->state
& bits
))
1407 node
= rb_next(node
);
1416 * find the first offset in the io tree with 'bits' set. zero is
1417 * returned if we find something, and *start_ret and *end_ret are
1418 * set to reflect the state struct that was found.
1420 * If nothing was found, 1 is returned. If found something, return 0.
1422 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1423 u64
*start_ret
, u64
*end_ret
, unsigned bits
,
1424 struct extent_state
**cached_state
)
1426 struct extent_state
*state
;
1429 spin_lock(&tree
->lock
);
1430 if (cached_state
&& *cached_state
) {
1431 state
= *cached_state
;
1432 if (state
->end
== start
- 1 && extent_state_in_tree(state
)) {
1433 while ((state
= next_state(state
)) != NULL
) {
1434 if (state
->state
& bits
)
1437 free_extent_state(*cached_state
);
1438 *cached_state
= NULL
;
1441 free_extent_state(*cached_state
);
1442 *cached_state
= NULL
;
1445 state
= find_first_extent_bit_state(tree
, start
, bits
);
1448 cache_state_if_flags(state
, cached_state
, 0);
1449 *start_ret
= state
->start
;
1450 *end_ret
= state
->end
;
1454 spin_unlock(&tree
->lock
);
1459 * find a contiguous range of bytes in the file marked as delalloc, not
1460 * more than 'max_bytes'. start and end are used to return the range,
1462 * 1 is returned if we find something, 0 if nothing was in the tree
1464 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1465 u64
*start
, u64
*end
, u64 max_bytes
,
1466 struct extent_state
**cached_state
)
1468 struct rb_node
*node
;
1469 struct extent_state
*state
;
1470 u64 cur_start
= *start
;
1472 u64 total_bytes
= 0;
1474 spin_lock(&tree
->lock
);
1477 * this search will find all the extents that end after
1480 node
= tree_search(tree
, cur_start
);
1488 state
= rb_entry(node
, struct extent_state
, rb_node
);
1489 if (found
&& (state
->start
!= cur_start
||
1490 (state
->state
& EXTENT_BOUNDARY
))) {
1493 if (!(state
->state
& EXTENT_DELALLOC
)) {
1499 *start
= state
->start
;
1500 *cached_state
= state
;
1501 refcount_inc(&state
->refs
);
1505 cur_start
= state
->end
+ 1;
1506 node
= rb_next(node
);
1507 total_bytes
+= state
->end
- state
->start
+ 1;
1508 if (total_bytes
>= max_bytes
)
1514 spin_unlock(&tree
->lock
);
1518 static int __process_pages_contig(struct address_space
*mapping
,
1519 struct page
*locked_page
,
1520 pgoff_t start_index
, pgoff_t end_index
,
1521 unsigned long page_ops
, pgoff_t
*index_ret
);
1523 static noinline
void __unlock_for_delalloc(struct inode
*inode
,
1524 struct page
*locked_page
,
1527 unsigned long index
= start
>> PAGE_SHIFT
;
1528 unsigned long end_index
= end
>> PAGE_SHIFT
;
1530 ASSERT(locked_page
);
1531 if (index
== locked_page
->index
&& end_index
== index
)
1534 __process_pages_contig(inode
->i_mapping
, locked_page
, index
, end_index
,
1538 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1539 struct page
*locked_page
,
1543 unsigned long index
= delalloc_start
>> PAGE_SHIFT
;
1544 unsigned long index_ret
= index
;
1545 unsigned long end_index
= delalloc_end
>> PAGE_SHIFT
;
1548 ASSERT(locked_page
);
1549 if (index
== locked_page
->index
&& index
== end_index
)
1552 ret
= __process_pages_contig(inode
->i_mapping
, locked_page
, index
,
1553 end_index
, PAGE_LOCK
, &index_ret
);
1555 __unlock_for_delalloc(inode
, locked_page
, delalloc_start
,
1556 (u64
)index_ret
<< PAGE_SHIFT
);
1561 * find a contiguous range of bytes in the file marked as delalloc, not
1562 * more than 'max_bytes'. start and end are used to return the range,
1564 * 1 is returned if we find something, 0 if nothing was in the tree
1566 static noinline_for_stack u64
find_lock_delalloc_range(struct inode
*inode
,
1567 struct extent_io_tree
*tree
,
1568 struct page
*locked_page
, u64
*start
,
1569 u64
*end
, u64 max_bytes
)
1574 struct extent_state
*cached_state
= NULL
;
1579 /* step one, find a bunch of delalloc bytes starting at start */
1580 delalloc_start
= *start
;
1582 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1583 max_bytes
, &cached_state
);
1584 if (!found
|| delalloc_end
<= *start
) {
1585 *start
= delalloc_start
;
1586 *end
= delalloc_end
;
1587 free_extent_state(cached_state
);
1592 * start comes from the offset of locked_page. We have to lock
1593 * pages in order, so we can't process delalloc bytes before
1596 if (delalloc_start
< *start
)
1597 delalloc_start
= *start
;
1600 * make sure to limit the number of pages we try to lock down
1602 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
)
1603 delalloc_end
= delalloc_start
+ max_bytes
- 1;
1605 /* step two, lock all the pages after the page that has start */
1606 ret
= lock_delalloc_pages(inode
, locked_page
,
1607 delalloc_start
, delalloc_end
);
1608 if (ret
== -EAGAIN
) {
1609 /* some of the pages are gone, lets avoid looping by
1610 * shortening the size of the delalloc range we're searching
1612 free_extent_state(cached_state
);
1613 cached_state
= NULL
;
1615 max_bytes
= PAGE_SIZE
;
1623 BUG_ON(ret
); /* Only valid values are 0 and -EAGAIN */
1625 /* step three, lock the state bits for the whole range */
1626 lock_extent_bits(tree
, delalloc_start
, delalloc_end
, &cached_state
);
1628 /* then test to make sure it is all still delalloc */
1629 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1630 EXTENT_DELALLOC
, 1, cached_state
);
1632 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1634 __unlock_for_delalloc(inode
, locked_page
,
1635 delalloc_start
, delalloc_end
);
1639 free_extent_state(cached_state
);
1640 *start
= delalloc_start
;
1641 *end
= delalloc_end
;
1646 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1647 u64
btrfs_find_lock_delalloc_range(struct inode
*inode
,
1648 struct extent_io_tree
*tree
,
1649 struct page
*locked_page
, u64
*start
,
1650 u64
*end
, u64 max_bytes
)
1652 return find_lock_delalloc_range(inode
, tree
, locked_page
, start
, end
,
1657 static int __process_pages_contig(struct address_space
*mapping
,
1658 struct page
*locked_page
,
1659 pgoff_t start_index
, pgoff_t end_index
,
1660 unsigned long page_ops
, pgoff_t
*index_ret
)
1662 unsigned long nr_pages
= end_index
- start_index
+ 1;
1663 unsigned long pages_locked
= 0;
1664 pgoff_t index
= start_index
;
1665 struct page
*pages
[16];
1670 if (page_ops
& PAGE_LOCK
) {
1671 ASSERT(page_ops
== PAGE_LOCK
);
1672 ASSERT(index_ret
&& *index_ret
== start_index
);
1675 if ((page_ops
& PAGE_SET_ERROR
) && nr_pages
> 0)
1676 mapping_set_error(mapping
, -EIO
);
1678 while (nr_pages
> 0) {
1679 ret
= find_get_pages_contig(mapping
, index
,
1680 min_t(unsigned long,
1681 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1684 * Only if we're going to lock these pages,
1685 * can we find nothing at @index.
1687 ASSERT(page_ops
& PAGE_LOCK
);
1692 for (i
= 0; i
< ret
; i
++) {
1693 if (page_ops
& PAGE_SET_PRIVATE2
)
1694 SetPagePrivate2(pages
[i
]);
1696 if (pages
[i
] == locked_page
) {
1701 if (page_ops
& PAGE_CLEAR_DIRTY
)
1702 clear_page_dirty_for_io(pages
[i
]);
1703 if (page_ops
& PAGE_SET_WRITEBACK
)
1704 set_page_writeback(pages
[i
]);
1705 if (page_ops
& PAGE_SET_ERROR
)
1706 SetPageError(pages
[i
]);
1707 if (page_ops
& PAGE_END_WRITEBACK
)
1708 end_page_writeback(pages
[i
]);
1709 if (page_ops
& PAGE_UNLOCK
)
1710 unlock_page(pages
[i
]);
1711 if (page_ops
& PAGE_LOCK
) {
1712 lock_page(pages
[i
]);
1713 if (!PageDirty(pages
[i
]) ||
1714 pages
[i
]->mapping
!= mapping
) {
1715 unlock_page(pages
[i
]);
1729 if (err
&& index_ret
)
1730 *index_ret
= start_index
+ pages_locked
- 1;
1734 void extent_clear_unlock_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1735 u64 delalloc_end
, struct page
*locked_page
,
1736 unsigned clear_bits
,
1737 unsigned long page_ops
)
1739 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, clear_bits
, 1, 0,
1742 __process_pages_contig(inode
->i_mapping
, locked_page
,
1743 start
>> PAGE_SHIFT
, end
>> PAGE_SHIFT
,
1748 * count the number of bytes in the tree that have a given bit(s)
1749 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1750 * cached. The total number found is returned.
1752 u64
count_range_bits(struct extent_io_tree
*tree
,
1753 u64
*start
, u64 search_end
, u64 max_bytes
,
1754 unsigned bits
, int contig
)
1756 struct rb_node
*node
;
1757 struct extent_state
*state
;
1758 u64 cur_start
= *start
;
1759 u64 total_bytes
= 0;
1763 if (WARN_ON(search_end
<= cur_start
))
1766 spin_lock(&tree
->lock
);
1767 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1768 total_bytes
= tree
->dirty_bytes
;
1772 * this search will find all the extents that end after
1775 node
= tree_search(tree
, cur_start
);
1780 state
= rb_entry(node
, struct extent_state
, rb_node
);
1781 if (state
->start
> search_end
)
1783 if (contig
&& found
&& state
->start
> last
+ 1)
1785 if (state
->end
>= cur_start
&& (state
->state
& bits
) == bits
) {
1786 total_bytes
+= min(search_end
, state
->end
) + 1 -
1787 max(cur_start
, state
->start
);
1788 if (total_bytes
>= max_bytes
)
1791 *start
= max(cur_start
, state
->start
);
1795 } else if (contig
&& found
) {
1798 node
= rb_next(node
);
1803 spin_unlock(&tree
->lock
);
1808 * set the private field for a given byte offset in the tree. If there isn't
1809 * an extent_state there already, this does nothing.
1811 static noinline
int set_state_failrec(struct extent_io_tree
*tree
, u64 start
,
1812 struct io_failure_record
*failrec
)
1814 struct rb_node
*node
;
1815 struct extent_state
*state
;
1818 spin_lock(&tree
->lock
);
1820 * this search will find all the extents that end after
1823 node
= tree_search(tree
, start
);
1828 state
= rb_entry(node
, struct extent_state
, rb_node
);
1829 if (state
->start
!= start
) {
1833 state
->failrec
= failrec
;
1835 spin_unlock(&tree
->lock
);
1839 static noinline
int get_state_failrec(struct extent_io_tree
*tree
, u64 start
,
1840 struct io_failure_record
**failrec
)
1842 struct rb_node
*node
;
1843 struct extent_state
*state
;
1846 spin_lock(&tree
->lock
);
1848 * this search will find all the extents that end after
1851 node
= tree_search(tree
, start
);
1856 state
= rb_entry(node
, struct extent_state
, rb_node
);
1857 if (state
->start
!= start
) {
1861 *failrec
= state
->failrec
;
1863 spin_unlock(&tree
->lock
);
1868 * searches a range in the state tree for a given mask.
1869 * If 'filled' == 1, this returns 1 only if every extent in the tree
1870 * has the bits set. Otherwise, 1 is returned if any bit in the
1871 * range is found set.
1873 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1874 unsigned bits
, int filled
, struct extent_state
*cached
)
1876 struct extent_state
*state
= NULL
;
1877 struct rb_node
*node
;
1880 spin_lock(&tree
->lock
);
1881 if (cached
&& extent_state_in_tree(cached
) && cached
->start
<= start
&&
1882 cached
->end
> start
)
1883 node
= &cached
->rb_node
;
1885 node
= tree_search(tree
, start
);
1886 while (node
&& start
<= end
) {
1887 state
= rb_entry(node
, struct extent_state
, rb_node
);
1889 if (filled
&& state
->start
> start
) {
1894 if (state
->start
> end
)
1897 if (state
->state
& bits
) {
1901 } else if (filled
) {
1906 if (state
->end
== (u64
)-1)
1909 start
= state
->end
+ 1;
1912 node
= rb_next(node
);
1919 spin_unlock(&tree
->lock
);
1924 * helper function to set a given page up to date if all the
1925 * extents in the tree for that page are up to date
1927 static void check_page_uptodate(struct extent_io_tree
*tree
, struct page
*page
)
1929 u64 start
= page_offset(page
);
1930 u64 end
= start
+ PAGE_SIZE
- 1;
1931 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1932 SetPageUptodate(page
);
1935 int free_io_failure(struct extent_io_tree
*failure_tree
,
1936 struct extent_io_tree
*io_tree
,
1937 struct io_failure_record
*rec
)
1942 set_state_failrec(failure_tree
, rec
->start
, NULL
);
1943 ret
= clear_extent_bits(failure_tree
, rec
->start
,
1944 rec
->start
+ rec
->len
- 1,
1945 EXTENT_LOCKED
| EXTENT_DIRTY
);
1949 ret
= clear_extent_bits(io_tree
, rec
->start
,
1950 rec
->start
+ rec
->len
- 1,
1960 * this bypasses the standard btrfs submit functions deliberately, as
1961 * the standard behavior is to write all copies in a raid setup. here we only
1962 * want to write the one bad copy. so we do the mapping for ourselves and issue
1963 * submit_bio directly.
1964 * to avoid any synchronization issues, wait for the data after writing, which
1965 * actually prevents the read that triggered the error from finishing.
1966 * currently, there can be no more than two copies of every data bit. thus,
1967 * exactly one rewrite is required.
1969 int repair_io_failure(struct btrfs_fs_info
*fs_info
, u64 ino
, u64 start
,
1970 u64 length
, u64 logical
, struct page
*page
,
1971 unsigned int pg_offset
, int mirror_num
)
1974 struct btrfs_device
*dev
;
1977 struct btrfs_bio
*bbio
= NULL
;
1980 ASSERT(!(fs_info
->sb
->s_flags
& SB_RDONLY
));
1981 BUG_ON(!mirror_num
);
1983 bio
= btrfs_io_bio_alloc(1);
1984 bio
->bi_iter
.bi_size
= 0;
1985 map_length
= length
;
1988 * Avoid races with device replace and make sure our bbio has devices
1989 * associated to its stripes that don't go away while we are doing the
1990 * read repair operation.
1992 btrfs_bio_counter_inc_blocked(fs_info
);
1993 if (btrfs_is_parity_mirror(fs_info
, logical
, length
)) {
1995 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
1996 * to update all raid stripes, but here we just want to correct
1997 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
1998 * stripe's dev and sector.
2000 ret
= btrfs_map_block(fs_info
, BTRFS_MAP_READ
, logical
,
2001 &map_length
, &bbio
, 0);
2003 btrfs_bio_counter_dec(fs_info
);
2007 ASSERT(bbio
->mirror_num
== 1);
2009 ret
= btrfs_map_block(fs_info
, BTRFS_MAP_WRITE
, logical
,
2010 &map_length
, &bbio
, mirror_num
);
2012 btrfs_bio_counter_dec(fs_info
);
2016 BUG_ON(mirror_num
!= bbio
->mirror_num
);
2019 sector
= bbio
->stripes
[bbio
->mirror_num
- 1].physical
>> 9;
2020 bio
->bi_iter
.bi_sector
= sector
;
2021 dev
= bbio
->stripes
[bbio
->mirror_num
- 1].dev
;
2022 btrfs_put_bbio(bbio
);
2023 if (!dev
|| !dev
->bdev
||
2024 !test_bit(BTRFS_DEV_STATE_WRITEABLE
, &dev
->dev_state
)) {
2025 btrfs_bio_counter_dec(fs_info
);
2029 bio_set_dev(bio
, dev
->bdev
);
2030 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
;
2031 bio_add_page(bio
, page
, length
, pg_offset
);
2033 if (btrfsic_submit_bio_wait(bio
)) {
2034 /* try to remap that extent elsewhere? */
2035 btrfs_bio_counter_dec(fs_info
);
2037 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_WRITE_ERRS
);
2041 btrfs_info_rl_in_rcu(fs_info
,
2042 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
2044 rcu_str_deref(dev
->name
), sector
);
2045 btrfs_bio_counter_dec(fs_info
);
2050 int repair_eb_io_failure(struct btrfs_fs_info
*fs_info
,
2051 struct extent_buffer
*eb
, int mirror_num
)
2053 u64 start
= eb
->start
;
2054 int i
, num_pages
= num_extent_pages(eb
);
2057 if (sb_rdonly(fs_info
->sb
))
2060 for (i
= 0; i
< num_pages
; i
++) {
2061 struct page
*p
= eb
->pages
[i
];
2063 ret
= repair_io_failure(fs_info
, 0, start
, PAGE_SIZE
, start
, p
,
2064 start
- page_offset(p
), mirror_num
);
2074 * each time an IO finishes, we do a fast check in the IO failure tree
2075 * to see if we need to process or clean up an io_failure_record
2077 int clean_io_failure(struct btrfs_fs_info
*fs_info
,
2078 struct extent_io_tree
*failure_tree
,
2079 struct extent_io_tree
*io_tree
, u64 start
,
2080 struct page
*page
, u64 ino
, unsigned int pg_offset
)
2083 struct io_failure_record
*failrec
;
2084 struct extent_state
*state
;
2089 ret
= count_range_bits(failure_tree
, &private, (u64
)-1, 1,
2094 ret
= get_state_failrec(failure_tree
, start
, &failrec
);
2098 BUG_ON(!failrec
->this_mirror
);
2100 if (failrec
->in_validation
) {
2101 /* there was no real error, just free the record */
2102 btrfs_debug(fs_info
,
2103 "clean_io_failure: freeing dummy error at %llu",
2107 if (sb_rdonly(fs_info
->sb
))
2110 spin_lock(&io_tree
->lock
);
2111 state
= find_first_extent_bit_state(io_tree
,
2114 spin_unlock(&io_tree
->lock
);
2116 if (state
&& state
->start
<= failrec
->start
&&
2117 state
->end
>= failrec
->start
+ failrec
->len
- 1) {
2118 num_copies
= btrfs_num_copies(fs_info
, failrec
->logical
,
2120 if (num_copies
> 1) {
2121 repair_io_failure(fs_info
, ino
, start
, failrec
->len
,
2122 failrec
->logical
, page
, pg_offset
,
2123 failrec
->failed_mirror
);
2128 free_io_failure(failure_tree
, io_tree
, failrec
);
2134 * Can be called when
2135 * - hold extent lock
2136 * - under ordered extent
2137 * - the inode is freeing
2139 void btrfs_free_io_failure_record(struct btrfs_inode
*inode
, u64 start
, u64 end
)
2141 struct extent_io_tree
*failure_tree
= &inode
->io_failure_tree
;
2142 struct io_failure_record
*failrec
;
2143 struct extent_state
*state
, *next
;
2145 if (RB_EMPTY_ROOT(&failure_tree
->state
))
2148 spin_lock(&failure_tree
->lock
);
2149 state
= find_first_extent_bit_state(failure_tree
, start
, EXTENT_DIRTY
);
2151 if (state
->start
> end
)
2154 ASSERT(state
->end
<= end
);
2156 next
= next_state(state
);
2158 failrec
= state
->failrec
;
2159 free_extent_state(state
);
2164 spin_unlock(&failure_tree
->lock
);
2167 int btrfs_get_io_failure_record(struct inode
*inode
, u64 start
, u64 end
,
2168 struct io_failure_record
**failrec_ret
)
2170 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2171 struct io_failure_record
*failrec
;
2172 struct extent_map
*em
;
2173 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
2174 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2175 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2179 ret
= get_state_failrec(failure_tree
, start
, &failrec
);
2181 failrec
= kzalloc(sizeof(*failrec
), GFP_NOFS
);
2185 failrec
->start
= start
;
2186 failrec
->len
= end
- start
+ 1;
2187 failrec
->this_mirror
= 0;
2188 failrec
->bio_flags
= 0;
2189 failrec
->in_validation
= 0;
2191 read_lock(&em_tree
->lock
);
2192 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
2194 read_unlock(&em_tree
->lock
);
2199 if (em
->start
> start
|| em
->start
+ em
->len
<= start
) {
2200 free_extent_map(em
);
2203 read_unlock(&em_tree
->lock
);
2209 logical
= start
- em
->start
;
2210 logical
= em
->block_start
+ logical
;
2211 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2212 logical
= em
->block_start
;
2213 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
2214 extent_set_compress_type(&failrec
->bio_flags
,
2218 btrfs_debug(fs_info
,
2219 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2220 logical
, start
, failrec
->len
);
2222 failrec
->logical
= logical
;
2223 free_extent_map(em
);
2225 /* set the bits in the private failure tree */
2226 ret
= set_extent_bits(failure_tree
, start
, end
,
2227 EXTENT_LOCKED
| EXTENT_DIRTY
);
2229 ret
= set_state_failrec(failure_tree
, start
, failrec
);
2230 /* set the bits in the inode's tree */
2232 ret
= set_extent_bits(tree
, start
, end
, EXTENT_DAMAGED
);
2238 btrfs_debug(fs_info
,
2239 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2240 failrec
->logical
, failrec
->start
, failrec
->len
,
2241 failrec
->in_validation
);
2243 * when data can be on disk more than twice, add to failrec here
2244 * (e.g. with a list for failed_mirror) to make
2245 * clean_io_failure() clean all those errors at once.
2249 *failrec_ret
= failrec
;
2254 bool btrfs_check_repairable(struct inode
*inode
, unsigned failed_bio_pages
,
2255 struct io_failure_record
*failrec
, int failed_mirror
)
2257 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2260 num_copies
= btrfs_num_copies(fs_info
, failrec
->logical
, failrec
->len
);
2261 if (num_copies
== 1) {
2263 * we only have a single copy of the data, so don't bother with
2264 * all the retry and error correction code that follows. no
2265 * matter what the error is, it is very likely to persist.
2267 btrfs_debug(fs_info
,
2268 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2269 num_copies
, failrec
->this_mirror
, failed_mirror
);
2274 * there are two premises:
2275 * a) deliver good data to the caller
2276 * b) correct the bad sectors on disk
2278 if (failed_bio_pages
> 1) {
2280 * to fulfill b), we need to know the exact failing sectors, as
2281 * we don't want to rewrite any more than the failed ones. thus,
2282 * we need separate read requests for the failed bio
2284 * if the following BUG_ON triggers, our validation request got
2285 * merged. we need separate requests for our algorithm to work.
2287 BUG_ON(failrec
->in_validation
);
2288 failrec
->in_validation
= 1;
2289 failrec
->this_mirror
= failed_mirror
;
2292 * we're ready to fulfill a) and b) alongside. get a good copy
2293 * of the failed sector and if we succeed, we have setup
2294 * everything for repair_io_failure to do the rest for us.
2296 if (failrec
->in_validation
) {
2297 BUG_ON(failrec
->this_mirror
!= failed_mirror
);
2298 failrec
->in_validation
= 0;
2299 failrec
->this_mirror
= 0;
2301 failrec
->failed_mirror
= failed_mirror
;
2302 failrec
->this_mirror
++;
2303 if (failrec
->this_mirror
== failed_mirror
)
2304 failrec
->this_mirror
++;
2307 if (failrec
->this_mirror
> num_copies
) {
2308 btrfs_debug(fs_info
,
2309 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2310 num_copies
, failrec
->this_mirror
, failed_mirror
);
2318 struct bio
*btrfs_create_repair_bio(struct inode
*inode
, struct bio
*failed_bio
,
2319 struct io_failure_record
*failrec
,
2320 struct page
*page
, int pg_offset
, int icsum
,
2321 bio_end_io_t
*endio_func
, void *data
)
2323 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2325 struct btrfs_io_bio
*btrfs_failed_bio
;
2326 struct btrfs_io_bio
*btrfs_bio
;
2328 bio
= btrfs_io_bio_alloc(1);
2329 bio
->bi_end_io
= endio_func
;
2330 bio
->bi_iter
.bi_sector
= failrec
->logical
>> 9;
2331 bio_set_dev(bio
, fs_info
->fs_devices
->latest_bdev
);
2332 bio
->bi_iter
.bi_size
= 0;
2333 bio
->bi_private
= data
;
2335 btrfs_failed_bio
= btrfs_io_bio(failed_bio
);
2336 if (btrfs_failed_bio
->csum
) {
2337 u16 csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
2339 btrfs_bio
= btrfs_io_bio(bio
);
2340 btrfs_bio
->csum
= btrfs_bio
->csum_inline
;
2342 memcpy(btrfs_bio
->csum
, btrfs_failed_bio
->csum
+ icsum
,
2346 bio_add_page(bio
, page
, failrec
->len
, pg_offset
);
2352 * this is a generic handler for readpage errors (default
2353 * readpage_io_failed_hook). if other copies exist, read those and write back
2354 * good data to the failed position. does not investigate in remapping the
2355 * failed extent elsewhere, hoping the device will be smart enough to do this as
2359 static int bio_readpage_error(struct bio
*failed_bio
, u64 phy_offset
,
2360 struct page
*page
, u64 start
, u64 end
,
2363 struct io_failure_record
*failrec
;
2364 struct inode
*inode
= page
->mapping
->host
;
2365 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2366 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
2369 blk_status_t status
;
2371 unsigned failed_bio_pages
= bio_pages_all(failed_bio
);
2373 BUG_ON(bio_op(failed_bio
) == REQ_OP_WRITE
);
2375 ret
= btrfs_get_io_failure_record(inode
, start
, end
, &failrec
);
2379 if (!btrfs_check_repairable(inode
, failed_bio_pages
, failrec
,
2381 free_io_failure(failure_tree
, tree
, failrec
);
2385 if (failed_bio_pages
> 1)
2386 read_mode
|= REQ_FAILFAST_DEV
;
2388 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
2389 bio
= btrfs_create_repair_bio(inode
, failed_bio
, failrec
, page
,
2390 start
- page_offset(page
),
2391 (int)phy_offset
, failed_bio
->bi_end_io
,
2393 bio
->bi_opf
= REQ_OP_READ
| read_mode
;
2395 btrfs_debug(btrfs_sb(inode
->i_sb
),
2396 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2397 read_mode
, failrec
->this_mirror
, failrec
->in_validation
);
2399 status
= tree
->ops
->submit_bio_hook(tree
->private_data
, bio
, failrec
->this_mirror
,
2400 failrec
->bio_flags
, 0);
2402 free_io_failure(failure_tree
, tree
, failrec
);
2404 ret
= blk_status_to_errno(status
);
2410 /* lots and lots of room for performance fixes in the end_bio funcs */
2412 void end_extent_writepage(struct page
*page
, int err
, u64 start
, u64 end
)
2414 int uptodate
= (err
== 0);
2415 struct extent_io_tree
*tree
;
2418 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2420 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2421 tree
->ops
->writepage_end_io_hook(page
, start
, end
, NULL
,
2425 ClearPageUptodate(page
);
2427 ret
= err
< 0 ? err
: -EIO
;
2428 mapping_set_error(page
->mapping
, ret
);
2433 * after a writepage IO is done, we need to:
2434 * clear the uptodate bits on error
2435 * clear the writeback bits in the extent tree for this IO
2436 * end_page_writeback if the page has no more pending IO
2438 * Scheduling is not allowed, so the extent state tree is expected
2439 * to have one and only one object corresponding to this IO.
2441 static void end_bio_extent_writepage(struct bio
*bio
)
2443 int error
= blk_status_to_errno(bio
->bi_status
);
2444 struct bio_vec
*bvec
;
2449 ASSERT(!bio_flagged(bio
, BIO_CLONED
));
2450 bio_for_each_segment_all(bvec
, bio
, i
) {
2451 struct page
*page
= bvec
->bv_page
;
2452 struct inode
*inode
= page
->mapping
->host
;
2453 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2455 /* We always issue full-page reads, but if some block
2456 * in a page fails to read, blk_update_request() will
2457 * advance bv_offset and adjust bv_len to compensate.
2458 * Print a warning for nonzero offsets, and an error
2459 * if they don't add up to a full page. */
2460 if (bvec
->bv_offset
|| bvec
->bv_len
!= PAGE_SIZE
) {
2461 if (bvec
->bv_offset
+ bvec
->bv_len
!= PAGE_SIZE
)
2463 "partial page write in btrfs with offset %u and length %u",
2464 bvec
->bv_offset
, bvec
->bv_len
);
2467 "incomplete page write in btrfs with offset %u and length %u",
2468 bvec
->bv_offset
, bvec
->bv_len
);
2471 start
= page_offset(page
);
2472 end
= start
+ bvec
->bv_offset
+ bvec
->bv_len
- 1;
2474 end_extent_writepage(page
, error
, start
, end
);
2475 end_page_writeback(page
);
2482 endio_readpage_release_extent(struct extent_io_tree
*tree
, u64 start
, u64 len
,
2485 struct extent_state
*cached
= NULL
;
2486 u64 end
= start
+ len
- 1;
2488 if (uptodate
&& tree
->track_uptodate
)
2489 set_extent_uptodate(tree
, start
, end
, &cached
, GFP_ATOMIC
);
2490 unlock_extent_cached_atomic(tree
, start
, end
, &cached
);
2494 * after a readpage IO is done, we need to:
2495 * clear the uptodate bits on error
2496 * set the uptodate bits if things worked
2497 * set the page up to date if all extents in the tree are uptodate
2498 * clear the lock bit in the extent tree
2499 * unlock the page if there are no other extents locked for it
2501 * Scheduling is not allowed, so the extent state tree is expected
2502 * to have one and only one object corresponding to this IO.
2504 static void end_bio_extent_readpage(struct bio
*bio
)
2506 struct bio_vec
*bvec
;
2507 int uptodate
= !bio
->bi_status
;
2508 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
2509 struct extent_io_tree
*tree
, *failure_tree
;
2514 u64 extent_start
= 0;
2520 ASSERT(!bio_flagged(bio
, BIO_CLONED
));
2521 bio_for_each_segment_all(bvec
, bio
, i
) {
2522 struct page
*page
= bvec
->bv_page
;
2523 struct inode
*inode
= page
->mapping
->host
;
2524 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2526 btrfs_debug(fs_info
,
2527 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
2528 (u64
)bio
->bi_iter
.bi_sector
, bio
->bi_status
,
2529 io_bio
->mirror_num
);
2530 tree
= &BTRFS_I(inode
)->io_tree
;
2531 failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
2533 /* We always issue full-page reads, but if some block
2534 * in a page fails to read, blk_update_request() will
2535 * advance bv_offset and adjust bv_len to compensate.
2536 * Print a warning for nonzero offsets, and an error
2537 * if they don't add up to a full page. */
2538 if (bvec
->bv_offset
|| bvec
->bv_len
!= PAGE_SIZE
) {
2539 if (bvec
->bv_offset
+ bvec
->bv_len
!= PAGE_SIZE
)
2541 "partial page read in btrfs with offset %u and length %u",
2542 bvec
->bv_offset
, bvec
->bv_len
);
2545 "incomplete page read in btrfs with offset %u and length %u",
2546 bvec
->bv_offset
, bvec
->bv_len
);
2549 start
= page_offset(page
);
2550 end
= start
+ bvec
->bv_offset
+ bvec
->bv_len
- 1;
2553 mirror
= io_bio
->mirror_num
;
2554 if (likely(uptodate
&& tree
->ops
)) {
2555 ret
= tree
->ops
->readpage_end_io_hook(io_bio
, offset
,
2561 clean_io_failure(BTRFS_I(inode
)->root
->fs_info
,
2562 failure_tree
, tree
, start
,
2564 btrfs_ino(BTRFS_I(inode
)), 0);
2567 if (likely(uptodate
))
2571 ret
= tree
->ops
->readpage_io_failed_hook(page
, mirror
);
2572 if (ret
== -EAGAIN
) {
2574 * Data inode's readpage_io_failed_hook() always
2577 * The generic bio_readpage_error handles errors
2578 * the following way: If possible, new read
2579 * requests are created and submitted and will
2580 * end up in end_bio_extent_readpage as well (if
2581 * we're lucky, not in the !uptodate case). In
2582 * that case it returns 0 and we just go on with
2583 * the next page in our bio. If it can't handle
2584 * the error it will return -EIO and we remain
2585 * responsible for that page.
2587 ret
= bio_readpage_error(bio
, offset
, page
,
2588 start
, end
, mirror
);
2590 uptodate
= !bio
->bi_status
;
2597 * metadata's readpage_io_failed_hook() always returns
2598 * -EIO and fixes nothing. -EIO is also returned if
2599 * data inode error could not be fixed.
2601 ASSERT(ret
== -EIO
);
2604 if (likely(uptodate
)) {
2605 loff_t i_size
= i_size_read(inode
);
2606 pgoff_t end_index
= i_size
>> PAGE_SHIFT
;
2609 /* Zero out the end if this page straddles i_size */
2610 off
= i_size
& (PAGE_SIZE
-1);
2611 if (page
->index
== end_index
&& off
)
2612 zero_user_segment(page
, off
, PAGE_SIZE
);
2613 SetPageUptodate(page
);
2615 ClearPageUptodate(page
);
2621 if (unlikely(!uptodate
)) {
2623 endio_readpage_release_extent(tree
,
2629 endio_readpage_release_extent(tree
, start
,
2630 end
- start
+ 1, 0);
2631 } else if (!extent_len
) {
2632 extent_start
= start
;
2633 extent_len
= end
+ 1 - start
;
2634 } else if (extent_start
+ extent_len
== start
) {
2635 extent_len
+= end
+ 1 - start
;
2637 endio_readpage_release_extent(tree
, extent_start
,
2638 extent_len
, uptodate
);
2639 extent_start
= start
;
2640 extent_len
= end
+ 1 - start
;
2645 endio_readpage_release_extent(tree
, extent_start
, extent_len
,
2648 io_bio
->end_io(io_bio
, blk_status_to_errno(bio
->bi_status
));
2653 * Initialize the members up to but not including 'bio'. Use after allocating a
2654 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2655 * 'bio' because use of __GFP_ZERO is not supported.
2657 static inline void btrfs_io_bio_init(struct btrfs_io_bio
*btrfs_bio
)
2659 memset(btrfs_bio
, 0, offsetof(struct btrfs_io_bio
, bio
));
2663 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2664 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2665 * for the appropriate container_of magic
2667 struct bio
*btrfs_bio_alloc(struct block_device
*bdev
, u64 first_byte
)
2671 bio
= bio_alloc_bioset(GFP_NOFS
, BIO_MAX_PAGES
, &btrfs_bioset
);
2672 bio_set_dev(bio
, bdev
);
2673 bio
->bi_iter
.bi_sector
= first_byte
>> 9;
2674 btrfs_io_bio_init(btrfs_io_bio(bio
));
2678 struct bio
*btrfs_bio_clone(struct bio
*bio
)
2680 struct btrfs_io_bio
*btrfs_bio
;
2683 /* Bio allocation backed by a bioset does not fail */
2684 new = bio_clone_fast(bio
, GFP_NOFS
, &btrfs_bioset
);
2685 btrfs_bio
= btrfs_io_bio(new);
2686 btrfs_io_bio_init(btrfs_bio
);
2687 btrfs_bio
->iter
= bio
->bi_iter
;
2691 struct bio
*btrfs_io_bio_alloc(unsigned int nr_iovecs
)
2695 /* Bio allocation backed by a bioset does not fail */
2696 bio
= bio_alloc_bioset(GFP_NOFS
, nr_iovecs
, &btrfs_bioset
);
2697 btrfs_io_bio_init(btrfs_io_bio(bio
));
2701 struct bio
*btrfs_bio_clone_partial(struct bio
*orig
, int offset
, int size
)
2704 struct btrfs_io_bio
*btrfs_bio
;
2706 /* this will never fail when it's backed by a bioset */
2707 bio
= bio_clone_fast(orig
, GFP_NOFS
, &btrfs_bioset
);
2710 btrfs_bio
= btrfs_io_bio(bio
);
2711 btrfs_io_bio_init(btrfs_bio
);
2713 bio_trim(bio
, offset
>> 9, size
>> 9);
2714 btrfs_bio
->iter
= bio
->bi_iter
;
2718 static int __must_check
submit_one_bio(struct bio
*bio
, int mirror_num
,
2719 unsigned long bio_flags
)
2721 blk_status_t ret
= 0;
2722 struct bio_vec
*bvec
= bio_last_bvec_all(bio
);
2723 struct page
*page
= bvec
->bv_page
;
2724 struct extent_io_tree
*tree
= bio
->bi_private
;
2727 start
= page_offset(page
) + bvec
->bv_offset
;
2729 bio
->bi_private
= NULL
;
2732 ret
= tree
->ops
->submit_bio_hook(tree
->private_data
, bio
,
2733 mirror_num
, bio_flags
, start
);
2735 btrfsic_submit_bio(bio
);
2737 return blk_status_to_errno(ret
);
2741 * @opf: bio REQ_OP_* and REQ_* flags as one value
2742 * @tree: tree so we can call our merge_bio hook
2743 * @wbc: optional writeback control for io accounting
2744 * @page: page to add to the bio
2745 * @pg_offset: offset of the new bio or to check whether we are adding
2746 * a contiguous page to the previous one
2747 * @size: portion of page that we want to write
2748 * @offset: starting offset in the page
2749 * @bdev: attach newly created bios to this bdev
2750 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
2751 * @end_io_func: end_io callback for new bio
2752 * @mirror_num: desired mirror to read/write
2753 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2754 * @bio_flags: flags of the current bio to see if we can merge them
2756 static int submit_extent_page(unsigned int opf
, struct extent_io_tree
*tree
,
2757 struct writeback_control
*wbc
,
2758 struct page
*page
, u64 offset
,
2759 size_t size
, unsigned long pg_offset
,
2760 struct block_device
*bdev
,
2761 struct bio
**bio_ret
,
2762 bio_end_io_t end_io_func
,
2764 unsigned long prev_bio_flags
,
2765 unsigned long bio_flags
,
2766 bool force_bio_submit
)
2770 size_t page_size
= min_t(size_t, size
, PAGE_SIZE
);
2771 sector_t sector
= offset
>> 9;
2777 bool can_merge
= true;
2780 if (prev_bio_flags
& EXTENT_BIO_COMPRESSED
)
2781 contig
= bio
->bi_iter
.bi_sector
== sector
;
2783 contig
= bio_end_sector(bio
) == sector
;
2785 if (tree
->ops
&& btrfs_merge_bio_hook(page
, offset
, page_size
,
2789 if (prev_bio_flags
!= bio_flags
|| !contig
|| !can_merge
||
2791 bio_add_page(bio
, page
, page_size
, pg_offset
) < page_size
) {
2792 ret
= submit_one_bio(bio
, mirror_num
, prev_bio_flags
);
2800 wbc_account_io(wbc
, page
, page_size
);
2805 bio
= btrfs_bio_alloc(bdev
, offset
);
2806 bio_add_page(bio
, page
, page_size
, pg_offset
);
2807 bio
->bi_end_io
= end_io_func
;
2808 bio
->bi_private
= tree
;
2809 bio
->bi_write_hint
= page
->mapping
->host
->i_write_hint
;
2812 wbc_init_bio(wbc
, bio
);
2813 wbc_account_io(wbc
, page
, page_size
);
2821 static void attach_extent_buffer_page(struct extent_buffer
*eb
,
2824 if (!PagePrivate(page
)) {
2825 SetPagePrivate(page
);
2827 set_page_private(page
, (unsigned long)eb
);
2829 WARN_ON(page
->private != (unsigned long)eb
);
2833 void set_page_extent_mapped(struct page
*page
)
2835 if (!PagePrivate(page
)) {
2836 SetPagePrivate(page
);
2838 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
2842 static struct extent_map
*
2843 __get_extent_map(struct inode
*inode
, struct page
*page
, size_t pg_offset
,
2844 u64 start
, u64 len
, get_extent_t
*get_extent
,
2845 struct extent_map
**em_cached
)
2847 struct extent_map
*em
;
2849 if (em_cached
&& *em_cached
) {
2851 if (extent_map_in_tree(em
) && start
>= em
->start
&&
2852 start
< extent_map_end(em
)) {
2853 refcount_inc(&em
->refs
);
2857 free_extent_map(em
);
2861 em
= get_extent(BTRFS_I(inode
), page
, pg_offset
, start
, len
, 0);
2862 if (em_cached
&& !IS_ERR_OR_NULL(em
)) {
2864 refcount_inc(&em
->refs
);
2870 * basic readpage implementation. Locked extent state structs are inserted
2871 * into the tree that are removed when the IO is done (by the end_io
2873 * XXX JDM: This needs looking at to ensure proper page locking
2874 * return 0 on success, otherwise return error
2876 static int __do_readpage(struct extent_io_tree
*tree
,
2878 get_extent_t
*get_extent
,
2879 struct extent_map
**em_cached
,
2880 struct bio
**bio
, int mirror_num
,
2881 unsigned long *bio_flags
, unsigned int read_flags
,
2884 struct inode
*inode
= page
->mapping
->host
;
2885 u64 start
= page_offset(page
);
2886 const u64 end
= start
+ PAGE_SIZE
- 1;
2889 u64 last_byte
= i_size_read(inode
);
2892 struct extent_map
*em
;
2893 struct block_device
*bdev
;
2896 size_t pg_offset
= 0;
2898 size_t disk_io_size
;
2899 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2900 unsigned long this_bio_flag
= 0;
2902 set_page_extent_mapped(page
);
2904 if (!PageUptodate(page
)) {
2905 if (cleancache_get_page(page
) == 0) {
2906 BUG_ON(blocksize
!= PAGE_SIZE
);
2907 unlock_extent(tree
, start
, end
);
2912 if (page
->index
== last_byte
>> PAGE_SHIFT
) {
2914 size_t zero_offset
= last_byte
& (PAGE_SIZE
- 1);
2917 iosize
= PAGE_SIZE
- zero_offset
;
2918 userpage
= kmap_atomic(page
);
2919 memset(userpage
+ zero_offset
, 0, iosize
);
2920 flush_dcache_page(page
);
2921 kunmap_atomic(userpage
);
2924 while (cur
<= end
) {
2925 bool force_bio_submit
= false;
2928 if (cur
>= last_byte
) {
2930 struct extent_state
*cached
= NULL
;
2932 iosize
= PAGE_SIZE
- pg_offset
;
2933 userpage
= kmap_atomic(page
);
2934 memset(userpage
+ pg_offset
, 0, iosize
);
2935 flush_dcache_page(page
);
2936 kunmap_atomic(userpage
);
2937 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2939 unlock_extent_cached(tree
, cur
,
2940 cur
+ iosize
- 1, &cached
);
2943 em
= __get_extent_map(inode
, page
, pg_offset
, cur
,
2944 end
- cur
+ 1, get_extent
, em_cached
);
2945 if (IS_ERR_OR_NULL(em
)) {
2947 unlock_extent(tree
, cur
, end
);
2950 extent_offset
= cur
- em
->start
;
2951 BUG_ON(extent_map_end(em
) <= cur
);
2954 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2955 this_bio_flag
|= EXTENT_BIO_COMPRESSED
;
2956 extent_set_compress_type(&this_bio_flag
,
2960 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2961 cur_end
= min(extent_map_end(em
) - 1, end
);
2962 iosize
= ALIGN(iosize
, blocksize
);
2963 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2964 disk_io_size
= em
->block_len
;
2965 offset
= em
->block_start
;
2967 offset
= em
->block_start
+ extent_offset
;
2968 disk_io_size
= iosize
;
2971 block_start
= em
->block_start
;
2972 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2973 block_start
= EXTENT_MAP_HOLE
;
2976 * If we have a file range that points to a compressed extent
2977 * and it's followed by a consecutive file range that points to
2978 * to the same compressed extent (possibly with a different
2979 * offset and/or length, so it either points to the whole extent
2980 * or only part of it), we must make sure we do not submit a
2981 * single bio to populate the pages for the 2 ranges because
2982 * this makes the compressed extent read zero out the pages
2983 * belonging to the 2nd range. Imagine the following scenario:
2986 * [0 - 8K] [8K - 24K]
2989 * points to extent X, points to extent X,
2990 * offset 4K, length of 8K offset 0, length 16K
2992 * [extent X, compressed length = 4K uncompressed length = 16K]
2994 * If the bio to read the compressed extent covers both ranges,
2995 * it will decompress extent X into the pages belonging to the
2996 * first range and then it will stop, zeroing out the remaining
2997 * pages that belong to the other range that points to extent X.
2998 * So here we make sure we submit 2 bios, one for the first
2999 * range and another one for the third range. Both will target
3000 * the same physical extent from disk, but we can't currently
3001 * make the compressed bio endio callback populate the pages
3002 * for both ranges because each compressed bio is tightly
3003 * coupled with a single extent map, and each range can have
3004 * an extent map with a different offset value relative to the
3005 * uncompressed data of our extent and different lengths. This
3006 * is a corner case so we prioritize correctness over
3007 * non-optimal behavior (submitting 2 bios for the same extent).
3009 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) &&
3010 prev_em_start
&& *prev_em_start
!= (u64
)-1 &&
3011 *prev_em_start
!= em
->orig_start
)
3012 force_bio_submit
= true;
3015 *prev_em_start
= em
->orig_start
;
3017 free_extent_map(em
);
3020 /* we've found a hole, just zero and go on */
3021 if (block_start
== EXTENT_MAP_HOLE
) {
3023 struct extent_state
*cached
= NULL
;
3025 userpage
= kmap_atomic(page
);
3026 memset(userpage
+ pg_offset
, 0, iosize
);
3027 flush_dcache_page(page
);
3028 kunmap_atomic(userpage
);
3030 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
3032 unlock_extent_cached(tree
, cur
,
3033 cur
+ iosize
- 1, &cached
);
3035 pg_offset
+= iosize
;
3038 /* the get_extent function already copied into the page */
3039 if (test_range_bit(tree
, cur
, cur_end
,
3040 EXTENT_UPTODATE
, 1, NULL
)) {
3041 check_page_uptodate(tree
, page
);
3042 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
3044 pg_offset
+= iosize
;
3047 /* we have an inline extent but it didn't get marked up
3048 * to date. Error out
3050 if (block_start
== EXTENT_MAP_INLINE
) {
3052 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
3054 pg_offset
+= iosize
;
3058 ret
= submit_extent_page(REQ_OP_READ
| read_flags
, tree
, NULL
,
3059 page
, offset
, disk_io_size
,
3060 pg_offset
, bdev
, bio
,
3061 end_bio_extent_readpage
, mirror_num
,
3067 *bio_flags
= this_bio_flag
;
3070 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
3074 pg_offset
+= iosize
;
3078 if (!PageError(page
))
3079 SetPageUptodate(page
);
3085 static inline void __do_contiguous_readpages(struct extent_io_tree
*tree
,
3086 struct page
*pages
[], int nr_pages
,
3088 struct extent_map
**em_cached
,
3090 unsigned long *bio_flags
,
3093 struct inode
*inode
;
3094 struct btrfs_ordered_extent
*ordered
;
3097 inode
= pages
[0]->mapping
->host
;
3099 lock_extent(tree
, start
, end
);
3100 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), start
,
3104 unlock_extent(tree
, start
, end
);
3105 btrfs_start_ordered_extent(inode
, ordered
, 1);
3106 btrfs_put_ordered_extent(ordered
);
3109 for (index
= 0; index
< nr_pages
; index
++) {
3110 __do_readpage(tree
, pages
[index
], btrfs_get_extent
, em_cached
,
3111 bio
, 0, bio_flags
, REQ_RAHEAD
, prev_em_start
);
3112 put_page(pages
[index
]);
3116 static void __extent_readpages(struct extent_io_tree
*tree
,
3117 struct page
*pages
[],
3119 struct extent_map
**em_cached
,
3120 struct bio
**bio
, unsigned long *bio_flags
,
3127 int first_index
= 0;
3129 for (index
= 0; index
< nr_pages
; index
++) {
3130 page_start
= page_offset(pages
[index
]);
3133 end
= start
+ PAGE_SIZE
- 1;
3134 first_index
= index
;
3135 } else if (end
+ 1 == page_start
) {
3138 __do_contiguous_readpages(tree
, &pages
[first_index
],
3139 index
- first_index
, start
,
3144 end
= start
+ PAGE_SIZE
- 1;
3145 first_index
= index
;
3150 __do_contiguous_readpages(tree
, &pages
[first_index
],
3151 index
- first_index
, start
,
3152 end
, em_cached
, bio
,
3153 bio_flags
, prev_em_start
);
3156 static int __extent_read_full_page(struct extent_io_tree
*tree
,
3158 get_extent_t
*get_extent
,
3159 struct bio
**bio
, int mirror_num
,
3160 unsigned long *bio_flags
,
3161 unsigned int read_flags
)
3163 struct inode
*inode
= page
->mapping
->host
;
3164 struct btrfs_ordered_extent
*ordered
;
3165 u64 start
= page_offset(page
);
3166 u64 end
= start
+ PAGE_SIZE
- 1;
3170 lock_extent(tree
, start
, end
);
3171 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), start
,
3175 unlock_extent(tree
, start
, end
);
3176 btrfs_start_ordered_extent(inode
, ordered
, 1);
3177 btrfs_put_ordered_extent(ordered
);
3180 ret
= __do_readpage(tree
, page
, get_extent
, NULL
, bio
, mirror_num
,
3181 bio_flags
, read_flags
, NULL
);
3185 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
3186 get_extent_t
*get_extent
, int mirror_num
)
3188 struct bio
*bio
= NULL
;
3189 unsigned long bio_flags
= 0;
3192 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, mirror_num
,
3195 ret
= submit_one_bio(bio
, mirror_num
, bio_flags
);
3199 static void update_nr_written(struct writeback_control
*wbc
,
3200 unsigned long nr_written
)
3202 wbc
->nr_to_write
-= nr_written
;
3206 * helper for __extent_writepage, doing all of the delayed allocation setup.
3208 * This returns 1 if our fill_delalloc function did all the work required
3209 * to write the page (copy into inline extent). In this case the IO has
3210 * been started and the page is already unlocked.
3212 * This returns 0 if all went well (page still locked)
3213 * This returns < 0 if there were errors (page still locked)
3215 static noinline_for_stack
int writepage_delalloc(struct inode
*inode
,
3216 struct page
*page
, struct writeback_control
*wbc
,
3217 struct extent_page_data
*epd
,
3219 unsigned long *nr_written
)
3221 struct extent_io_tree
*tree
= epd
->tree
;
3222 u64 page_end
= delalloc_start
+ PAGE_SIZE
- 1;
3224 u64 delalloc_to_write
= 0;
3225 u64 delalloc_end
= 0;
3227 int page_started
= 0;
3229 if (epd
->extent_locked
|| !tree
->ops
|| !tree
->ops
->fill_delalloc
)
3232 while (delalloc_end
< page_end
) {
3233 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
3237 BTRFS_MAX_EXTENT_SIZE
);
3238 if (nr_delalloc
== 0) {
3239 delalloc_start
= delalloc_end
+ 1;
3242 ret
= tree
->ops
->fill_delalloc(inode
, page
,
3247 /* File system has been set read-only */
3250 /* fill_delalloc should be return < 0 for error
3251 * but just in case, we use > 0 here meaning the
3252 * IO is started, so we don't want to return > 0
3253 * unless things are going well.
3255 ret
= ret
< 0 ? ret
: -EIO
;
3259 * delalloc_end is already one less than the total length, so
3260 * we don't subtract one from PAGE_SIZE
3262 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
3263 PAGE_SIZE
) >> PAGE_SHIFT
;
3264 delalloc_start
= delalloc_end
+ 1;
3266 if (wbc
->nr_to_write
< delalloc_to_write
) {
3269 if (delalloc_to_write
< thresh
* 2)
3270 thresh
= delalloc_to_write
;
3271 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
3275 /* did the fill delalloc function already unlock and start
3280 * we've unlocked the page, so we can't update
3281 * the mapping's writeback index, just update
3284 wbc
->nr_to_write
-= *nr_written
;
3295 * helper for __extent_writepage. This calls the writepage start hooks,
3296 * and does the loop to map the page into extents and bios.
3298 * We return 1 if the IO is started and the page is unlocked,
3299 * 0 if all went well (page still locked)
3300 * < 0 if there were errors (page still locked)
3302 static noinline_for_stack
int __extent_writepage_io(struct inode
*inode
,
3304 struct writeback_control
*wbc
,
3305 struct extent_page_data
*epd
,
3307 unsigned long nr_written
,
3308 unsigned int write_flags
, int *nr_ret
)
3310 struct extent_io_tree
*tree
= epd
->tree
;
3311 u64 start
= page_offset(page
);
3312 u64 page_end
= start
+ PAGE_SIZE
- 1;
3318 struct extent_map
*em
;
3319 struct block_device
*bdev
;
3320 size_t pg_offset
= 0;
3326 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
3327 ret
= tree
->ops
->writepage_start_hook(page
, start
,
3330 /* Fixup worker will requeue */
3332 wbc
->pages_skipped
++;
3334 redirty_page_for_writepage(wbc
, page
);
3336 update_nr_written(wbc
, nr_written
);
3343 * we don't want to touch the inode after unlocking the page,
3344 * so we update the mapping writeback index now
3346 update_nr_written(wbc
, nr_written
+ 1);
3349 if (i_size
<= start
) {
3350 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
3351 tree
->ops
->writepage_end_io_hook(page
, start
,
3356 blocksize
= inode
->i_sb
->s_blocksize
;
3358 while (cur
<= end
) {
3362 if (cur
>= i_size
) {
3363 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
3364 tree
->ops
->writepage_end_io_hook(page
, cur
,
3368 em
= btrfs_get_extent(BTRFS_I(inode
), page
, pg_offset
, cur
,
3370 if (IS_ERR_OR_NULL(em
)) {
3372 ret
= PTR_ERR_OR_ZERO(em
);
3376 extent_offset
= cur
- em
->start
;
3377 em_end
= extent_map_end(em
);
3378 BUG_ON(em_end
<= cur
);
3380 iosize
= min(em_end
- cur
, end
- cur
+ 1);
3381 iosize
= ALIGN(iosize
, blocksize
);
3382 offset
= em
->block_start
+ extent_offset
;
3384 block_start
= em
->block_start
;
3385 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
3386 free_extent_map(em
);
3390 * compressed and inline extents are written through other
3393 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
3394 block_start
== EXTENT_MAP_INLINE
) {
3396 * end_io notification does not happen here for
3397 * compressed extents
3399 if (!compressed
&& tree
->ops
&&
3400 tree
->ops
->writepage_end_io_hook
)
3401 tree
->ops
->writepage_end_io_hook(page
, cur
,
3404 else if (compressed
) {
3405 /* we don't want to end_page_writeback on
3406 * a compressed extent. this happens
3413 pg_offset
+= iosize
;
3417 btrfs_set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
3418 if (!PageWriteback(page
)) {
3419 btrfs_err(BTRFS_I(inode
)->root
->fs_info
,
3420 "page %lu not writeback, cur %llu end %llu",
3421 page
->index
, cur
, end
);
3424 ret
= submit_extent_page(REQ_OP_WRITE
| write_flags
, tree
, wbc
,
3425 page
, offset
, iosize
, pg_offset
,
3427 end_bio_extent_writepage
,
3431 if (PageWriteback(page
))
3432 end_page_writeback(page
);
3436 pg_offset
+= iosize
;
3445 * the writepage semantics are similar to regular writepage. extent
3446 * records are inserted to lock ranges in the tree, and as dirty areas
3447 * are found, they are marked writeback. Then the lock bits are removed
3448 * and the end_io handler clears the writeback ranges
3450 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
3451 struct extent_page_data
*epd
)
3453 struct inode
*inode
= page
->mapping
->host
;
3454 u64 start
= page_offset(page
);
3455 u64 page_end
= start
+ PAGE_SIZE
- 1;
3458 size_t pg_offset
= 0;
3459 loff_t i_size
= i_size_read(inode
);
3460 unsigned long end_index
= i_size
>> PAGE_SHIFT
;
3461 unsigned int write_flags
= 0;
3462 unsigned long nr_written
= 0;
3464 write_flags
= wbc_to_write_flags(wbc
);
3466 trace___extent_writepage(page
, inode
, wbc
);
3468 WARN_ON(!PageLocked(page
));
3470 ClearPageError(page
);
3472 pg_offset
= i_size
& (PAGE_SIZE
- 1);
3473 if (page
->index
> end_index
||
3474 (page
->index
== end_index
&& !pg_offset
)) {
3475 page
->mapping
->a_ops
->invalidatepage(page
, 0, PAGE_SIZE
);
3480 if (page
->index
== end_index
) {
3483 userpage
= kmap_atomic(page
);
3484 memset(userpage
+ pg_offset
, 0,
3485 PAGE_SIZE
- pg_offset
);
3486 kunmap_atomic(userpage
);
3487 flush_dcache_page(page
);
3492 set_page_extent_mapped(page
);
3494 ret
= writepage_delalloc(inode
, page
, wbc
, epd
, start
, &nr_written
);
3500 ret
= __extent_writepage_io(inode
, page
, wbc
, epd
,
3501 i_size
, nr_written
, write_flags
, &nr
);
3507 /* make sure the mapping tag for page dirty gets cleared */
3508 set_page_writeback(page
);
3509 end_page_writeback(page
);
3511 if (PageError(page
)) {
3512 ret
= ret
< 0 ? ret
: -EIO
;
3513 end_extent_writepage(page
, ret
, start
, page_end
);
3522 void wait_on_extent_buffer_writeback(struct extent_buffer
*eb
)
3524 wait_on_bit_io(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
,
3525 TASK_UNINTERRUPTIBLE
);
3528 static noinline_for_stack
int
3529 lock_extent_buffer_for_io(struct extent_buffer
*eb
,
3530 struct btrfs_fs_info
*fs_info
,
3531 struct extent_page_data
*epd
)
3537 if (!btrfs_try_tree_write_lock(eb
)) {
3539 flush_write_bio(epd
);
3540 btrfs_tree_lock(eb
);
3543 if (test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
)) {
3544 btrfs_tree_unlock(eb
);
3548 flush_write_bio(epd
);
3552 wait_on_extent_buffer_writeback(eb
);
3553 btrfs_tree_lock(eb
);
3554 if (!test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
))
3556 btrfs_tree_unlock(eb
);
3561 * We need to do this to prevent races in people who check if the eb is
3562 * under IO since we can end up having no IO bits set for a short period
3565 spin_lock(&eb
->refs_lock
);
3566 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3567 set_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3568 spin_unlock(&eb
->refs_lock
);
3569 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
3570 percpu_counter_add_batch(&fs_info
->dirty_metadata_bytes
,
3572 fs_info
->dirty_metadata_batch
);
3575 spin_unlock(&eb
->refs_lock
);
3578 btrfs_tree_unlock(eb
);
3583 num_pages
= num_extent_pages(eb
);
3584 for (i
= 0; i
< num_pages
; i
++) {
3585 struct page
*p
= eb
->pages
[i
];
3587 if (!trylock_page(p
)) {
3589 flush_write_bio(epd
);
3599 static void end_extent_buffer_writeback(struct extent_buffer
*eb
)
3601 clear_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3602 smp_mb__after_atomic();
3603 wake_up_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
);
3606 static void set_btree_ioerr(struct page
*page
)
3608 struct extent_buffer
*eb
= (struct extent_buffer
*)page
->private;
3611 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR
, &eb
->bflags
))
3615 * If writeback for a btree extent that doesn't belong to a log tree
3616 * failed, increment the counter transaction->eb_write_errors.
3617 * We do this because while the transaction is running and before it's
3618 * committing (when we call filemap_fdata[write|wait]_range against
3619 * the btree inode), we might have
3620 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3621 * returns an error or an error happens during writeback, when we're
3622 * committing the transaction we wouldn't know about it, since the pages
3623 * can be no longer dirty nor marked anymore for writeback (if a
3624 * subsequent modification to the extent buffer didn't happen before the
3625 * transaction commit), which makes filemap_fdata[write|wait]_range not
3626 * able to find the pages tagged with SetPageError at transaction
3627 * commit time. So if this happens we must abort the transaction,
3628 * otherwise we commit a super block with btree roots that point to
3629 * btree nodes/leafs whose content on disk is invalid - either garbage
3630 * or the content of some node/leaf from a past generation that got
3631 * cowed or deleted and is no longer valid.
3633 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3634 * not be enough - we need to distinguish between log tree extents vs
3635 * non-log tree extents, and the next filemap_fdatawait_range() call
3636 * will catch and clear such errors in the mapping - and that call might
3637 * be from a log sync and not from a transaction commit. Also, checking
3638 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3639 * not done and would not be reliable - the eb might have been released
3640 * from memory and reading it back again means that flag would not be
3641 * set (since it's a runtime flag, not persisted on disk).
3643 * Using the flags below in the btree inode also makes us achieve the
3644 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3645 * writeback for all dirty pages and before filemap_fdatawait_range()
3646 * is called, the writeback for all dirty pages had already finished
3647 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3648 * filemap_fdatawait_range() would return success, as it could not know
3649 * that writeback errors happened (the pages were no longer tagged for
3652 switch (eb
->log_index
) {
3654 set_bit(BTRFS_FS_BTREE_ERR
, &eb
->fs_info
->flags
);
3657 set_bit(BTRFS_FS_LOG1_ERR
, &eb
->fs_info
->flags
);
3660 set_bit(BTRFS_FS_LOG2_ERR
, &eb
->fs_info
->flags
);
3663 BUG(); /* unexpected, logic error */
3667 static void end_bio_extent_buffer_writepage(struct bio
*bio
)
3669 struct bio_vec
*bvec
;
3670 struct extent_buffer
*eb
;
3673 ASSERT(!bio_flagged(bio
, BIO_CLONED
));
3674 bio_for_each_segment_all(bvec
, bio
, i
) {
3675 struct page
*page
= bvec
->bv_page
;
3677 eb
= (struct extent_buffer
*)page
->private;
3679 done
= atomic_dec_and_test(&eb
->io_pages
);
3681 if (bio
->bi_status
||
3682 test_bit(EXTENT_BUFFER_WRITE_ERR
, &eb
->bflags
)) {
3683 ClearPageUptodate(page
);
3684 set_btree_ioerr(page
);
3687 end_page_writeback(page
);
3692 end_extent_buffer_writeback(eb
);
3698 static noinline_for_stack
int write_one_eb(struct extent_buffer
*eb
,
3699 struct btrfs_fs_info
*fs_info
,
3700 struct writeback_control
*wbc
,
3701 struct extent_page_data
*epd
)
3703 struct block_device
*bdev
= fs_info
->fs_devices
->latest_bdev
;
3704 struct extent_io_tree
*tree
= &BTRFS_I(fs_info
->btree_inode
)->io_tree
;
3705 u64 offset
= eb
->start
;
3708 unsigned long start
, end
;
3709 unsigned int write_flags
= wbc_to_write_flags(wbc
) | REQ_META
;
3712 clear_bit(EXTENT_BUFFER_WRITE_ERR
, &eb
->bflags
);
3713 num_pages
= num_extent_pages(eb
);
3714 atomic_set(&eb
->io_pages
, num_pages
);
3716 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3717 nritems
= btrfs_header_nritems(eb
);
3718 if (btrfs_header_level(eb
) > 0) {
3719 end
= btrfs_node_key_ptr_offset(nritems
);
3721 memzero_extent_buffer(eb
, end
, eb
->len
- end
);
3725 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3727 start
= btrfs_item_nr_offset(nritems
);
3728 end
= BTRFS_LEAF_DATA_OFFSET
+ leaf_data_end(fs_info
, eb
);
3729 memzero_extent_buffer(eb
, start
, end
- start
);
3732 for (i
= 0; i
< num_pages
; i
++) {
3733 struct page
*p
= eb
->pages
[i
];
3735 clear_page_dirty_for_io(p
);
3736 set_page_writeback(p
);
3737 ret
= submit_extent_page(REQ_OP_WRITE
| write_flags
, tree
, wbc
,
3738 p
, offset
, PAGE_SIZE
, 0, bdev
,
3740 end_bio_extent_buffer_writepage
,
3744 if (PageWriteback(p
))
3745 end_page_writeback(p
);
3746 if (atomic_sub_and_test(num_pages
- i
, &eb
->io_pages
))
3747 end_extent_buffer_writeback(eb
);
3751 offset
+= PAGE_SIZE
;
3752 update_nr_written(wbc
, 1);
3756 if (unlikely(ret
)) {
3757 for (; i
< num_pages
; i
++) {
3758 struct page
*p
= eb
->pages
[i
];
3759 clear_page_dirty_for_io(p
);
3767 int btree_write_cache_pages(struct address_space
*mapping
,
3768 struct writeback_control
*wbc
)
3770 struct extent_io_tree
*tree
= &BTRFS_I(mapping
->host
)->io_tree
;
3771 struct btrfs_fs_info
*fs_info
= BTRFS_I(mapping
->host
)->root
->fs_info
;
3772 struct extent_buffer
*eb
, *prev_eb
= NULL
;
3773 struct extent_page_data epd
= {
3777 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3781 int nr_to_write_done
= 0;
3782 struct pagevec pvec
;
3785 pgoff_t end
; /* Inclusive */
3789 pagevec_init(&pvec
);
3790 if (wbc
->range_cyclic
) {
3791 index
= mapping
->writeback_index
; /* Start from prev offset */
3794 index
= wbc
->range_start
>> PAGE_SHIFT
;
3795 end
= wbc
->range_end
>> PAGE_SHIFT
;
3798 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3799 tag
= PAGECACHE_TAG_TOWRITE
;
3801 tag
= PAGECACHE_TAG_DIRTY
;
3803 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3804 tag_pages_for_writeback(mapping
, index
, end
);
3805 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3806 (nr_pages
= pagevec_lookup_range_tag(&pvec
, mapping
, &index
, end
,
3811 for (i
= 0; i
< nr_pages
; i
++) {
3812 struct page
*page
= pvec
.pages
[i
];
3814 if (!PagePrivate(page
))
3817 spin_lock(&mapping
->private_lock
);
3818 if (!PagePrivate(page
)) {
3819 spin_unlock(&mapping
->private_lock
);
3823 eb
= (struct extent_buffer
*)page
->private;
3826 * Shouldn't happen and normally this would be a BUG_ON
3827 * but no sense in crashing the users box for something
3828 * we can survive anyway.
3831 spin_unlock(&mapping
->private_lock
);
3835 if (eb
== prev_eb
) {
3836 spin_unlock(&mapping
->private_lock
);
3840 ret
= atomic_inc_not_zero(&eb
->refs
);
3841 spin_unlock(&mapping
->private_lock
);
3846 ret
= lock_extent_buffer_for_io(eb
, fs_info
, &epd
);
3848 free_extent_buffer(eb
);
3852 ret
= write_one_eb(eb
, fs_info
, wbc
, &epd
);
3855 free_extent_buffer(eb
);
3858 free_extent_buffer(eb
);
3861 * the filesystem may choose to bump up nr_to_write.
3862 * We have to make sure to honor the new nr_to_write
3865 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3867 pagevec_release(&pvec
);
3870 if (!scanned
&& !done
) {
3872 * We hit the last page and there is more work to be done: wrap
3873 * back to the start of the file
3879 flush_write_bio(&epd
);
3884 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3885 * @mapping: address space structure to write
3886 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3887 * @data: data passed to __extent_writepage function
3889 * If a page is already under I/O, write_cache_pages() skips it, even
3890 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3891 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3892 * and msync() need to guarantee that all the data which was dirty at the time
3893 * the call was made get new I/O started against them. If wbc->sync_mode is
3894 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3895 * existing IO to complete.
3897 static int extent_write_cache_pages(struct address_space
*mapping
,
3898 struct writeback_control
*wbc
,
3899 struct extent_page_data
*epd
)
3901 struct inode
*inode
= mapping
->host
;
3904 int nr_to_write_done
= 0;
3905 struct pagevec pvec
;
3908 pgoff_t end
; /* Inclusive */
3910 int range_whole
= 0;
3915 * We have to hold onto the inode so that ordered extents can do their
3916 * work when the IO finishes. The alternative to this is failing to add
3917 * an ordered extent if the igrab() fails there and that is a huge pain
3918 * to deal with, so instead just hold onto the inode throughout the
3919 * writepages operation. If it fails here we are freeing up the inode
3920 * anyway and we'd rather not waste our time writing out stuff that is
3921 * going to be truncated anyway.
3926 pagevec_init(&pvec
);
3927 if (wbc
->range_cyclic
) {
3928 index
= mapping
->writeback_index
; /* Start from prev offset */
3931 index
= wbc
->range_start
>> PAGE_SHIFT
;
3932 end
= wbc
->range_end
>> PAGE_SHIFT
;
3933 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
3937 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3938 tag
= PAGECACHE_TAG_TOWRITE
;
3940 tag
= PAGECACHE_TAG_DIRTY
;
3942 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3943 tag_pages_for_writeback(mapping
, index
, end
);
3945 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3946 (nr_pages
= pagevec_lookup_range_tag(&pvec
, mapping
,
3947 &index
, end
, tag
))) {
3951 for (i
= 0; i
< nr_pages
; i
++) {
3952 struct page
*page
= pvec
.pages
[i
];
3954 done_index
= page
->index
;
3956 * At this point we hold neither the i_pages lock nor
3957 * the page lock: the page may be truncated or
3958 * invalidated (changing page->mapping to NULL),
3959 * or even swizzled back from swapper_space to
3960 * tmpfs file mapping
3962 if (!trylock_page(page
)) {
3963 flush_write_bio(epd
);
3967 if (unlikely(page
->mapping
!= mapping
)) {
3972 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3973 if (PageWriteback(page
))
3974 flush_write_bio(epd
);
3975 wait_on_page_writeback(page
);
3978 if (PageWriteback(page
) ||
3979 !clear_page_dirty_for_io(page
)) {
3984 ret
= __extent_writepage(page
, wbc
, epd
);
3986 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
3992 * done_index is set past this page,
3993 * so media errors will not choke
3994 * background writeout for the entire
3995 * file. This has consequences for
3996 * range_cyclic semantics (ie. it may
3997 * not be suitable for data integrity
4000 done_index
= page
->index
+ 1;
4006 * the filesystem may choose to bump up nr_to_write.
4007 * We have to make sure to honor the new nr_to_write
4010 nr_to_write_done
= wbc
->nr_to_write
<= 0;
4012 pagevec_release(&pvec
);
4015 if (!scanned
&& !done
) {
4017 * We hit the last page and there is more work to be done: wrap
4018 * back to the start of the file
4025 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 && range_whole
))
4026 mapping
->writeback_index
= done_index
;
4028 btrfs_add_delayed_iput(inode
);
4032 static void flush_write_bio(struct extent_page_data
*epd
)
4037 ret
= submit_one_bio(epd
->bio
, 0, 0);
4038 BUG_ON(ret
< 0); /* -ENOMEM */
4043 int extent_write_full_page(struct page
*page
, struct writeback_control
*wbc
)
4046 struct extent_page_data epd
= {
4048 .tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
,
4050 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
4053 ret
= __extent_writepage(page
, wbc
, &epd
);
4055 flush_write_bio(&epd
);
4059 int extent_write_locked_range(struct inode
*inode
, u64 start
, u64 end
,
4063 struct address_space
*mapping
= inode
->i_mapping
;
4064 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
4066 unsigned long nr_pages
= (end
- start
+ PAGE_SIZE
) >>
4069 struct extent_page_data epd
= {
4073 .sync_io
= mode
== WB_SYNC_ALL
,
4075 struct writeback_control wbc_writepages
= {
4077 .nr_to_write
= nr_pages
* 2,
4078 .range_start
= start
,
4079 .range_end
= end
+ 1,
4082 while (start
<= end
) {
4083 page
= find_get_page(mapping
, start
>> PAGE_SHIFT
);
4084 if (clear_page_dirty_for_io(page
))
4085 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
4087 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
4088 tree
->ops
->writepage_end_io_hook(page
, start
,
4089 start
+ PAGE_SIZE
- 1,
4097 flush_write_bio(&epd
);
4101 int extent_writepages(struct address_space
*mapping
,
4102 struct writeback_control
*wbc
)
4105 struct extent_page_data epd
= {
4107 .tree
= &BTRFS_I(mapping
->host
)->io_tree
,
4109 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
4112 ret
= extent_write_cache_pages(mapping
, wbc
, &epd
);
4113 flush_write_bio(&epd
);
4117 int extent_readpages(struct address_space
*mapping
, struct list_head
*pages
,
4120 struct bio
*bio
= NULL
;
4122 unsigned long bio_flags
= 0;
4123 struct page
*pagepool
[16];
4125 struct extent_map
*em_cached
= NULL
;
4126 struct extent_io_tree
*tree
= &BTRFS_I(mapping
->host
)->io_tree
;
4128 u64 prev_em_start
= (u64
)-1;
4130 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
4131 page
= list_entry(pages
->prev
, struct page
, lru
);
4133 prefetchw(&page
->flags
);
4134 list_del(&page
->lru
);
4135 if (add_to_page_cache_lru(page
, mapping
,
4137 readahead_gfp_mask(mapping
))) {
4142 pagepool
[nr
++] = page
;
4143 if (nr
< ARRAY_SIZE(pagepool
))
4145 __extent_readpages(tree
, pagepool
, nr
, &em_cached
, &bio
,
4146 &bio_flags
, &prev_em_start
);
4150 __extent_readpages(tree
, pagepool
, nr
, &em_cached
, &bio
,
4151 &bio_flags
, &prev_em_start
);
4154 free_extent_map(em_cached
);
4156 BUG_ON(!list_empty(pages
));
4158 return submit_one_bio(bio
, 0, bio_flags
);
4163 * basic invalidatepage code, this waits on any locked or writeback
4164 * ranges corresponding to the page, and then deletes any extent state
4165 * records from the tree
4167 int extent_invalidatepage(struct extent_io_tree
*tree
,
4168 struct page
*page
, unsigned long offset
)
4170 struct extent_state
*cached_state
= NULL
;
4171 u64 start
= page_offset(page
);
4172 u64 end
= start
+ PAGE_SIZE
- 1;
4173 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
4175 start
+= ALIGN(offset
, blocksize
);
4179 lock_extent_bits(tree
, start
, end
, &cached_state
);
4180 wait_on_page_writeback(page
);
4181 clear_extent_bit(tree
, start
, end
,
4182 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
4183 EXTENT_DO_ACCOUNTING
,
4184 1, 1, &cached_state
);
4189 * a helper for releasepage, this tests for areas of the page that
4190 * are locked or under IO and drops the related state bits if it is safe
4193 static int try_release_extent_state(struct extent_io_tree
*tree
,
4194 struct page
*page
, gfp_t mask
)
4196 u64 start
= page_offset(page
);
4197 u64 end
= start
+ PAGE_SIZE
- 1;
4200 if (test_range_bit(tree
, start
, end
,
4201 EXTENT_IOBITS
, 0, NULL
))
4205 * at this point we can safely clear everything except the
4206 * locked bit and the nodatasum bit
4208 ret
= __clear_extent_bit(tree
, start
, end
,
4209 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
4210 0, 0, NULL
, mask
, NULL
);
4212 /* if clear_extent_bit failed for enomem reasons,
4213 * we can't allow the release to continue.
4224 * a helper for releasepage. As long as there are no locked extents
4225 * in the range corresponding to the page, both state records and extent
4226 * map records are removed
4228 int try_release_extent_mapping(struct page
*page
, gfp_t mask
)
4230 struct extent_map
*em
;
4231 u64 start
= page_offset(page
);
4232 u64 end
= start
+ PAGE_SIZE
- 1;
4233 struct btrfs_inode
*btrfs_inode
= BTRFS_I(page
->mapping
->host
);
4234 struct extent_io_tree
*tree
= &btrfs_inode
->io_tree
;
4235 struct extent_map_tree
*map
= &btrfs_inode
->extent_tree
;
4237 if (gfpflags_allow_blocking(mask
) &&
4238 page
->mapping
->host
->i_size
> SZ_16M
) {
4240 while (start
<= end
) {
4241 len
= end
- start
+ 1;
4242 write_lock(&map
->lock
);
4243 em
= lookup_extent_mapping(map
, start
, len
);
4245 write_unlock(&map
->lock
);
4248 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
4249 em
->start
!= start
) {
4250 write_unlock(&map
->lock
);
4251 free_extent_map(em
);
4254 if (!test_range_bit(tree
, em
->start
,
4255 extent_map_end(em
) - 1,
4256 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
4258 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4259 &btrfs_inode
->runtime_flags
);
4260 remove_extent_mapping(map
, em
);
4261 /* once for the rb tree */
4262 free_extent_map(em
);
4264 start
= extent_map_end(em
);
4265 write_unlock(&map
->lock
);
4268 free_extent_map(em
);
4271 return try_release_extent_state(tree
, page
, mask
);
4275 * helper function for fiemap, which doesn't want to see any holes.
4276 * This maps until we find something past 'last'
4278 static struct extent_map
*get_extent_skip_holes(struct inode
*inode
,
4279 u64 offset
, u64 last
)
4281 u64 sectorsize
= btrfs_inode_sectorsize(inode
);
4282 struct extent_map
*em
;
4289 len
= last
- offset
;
4292 len
= ALIGN(len
, sectorsize
);
4293 em
= btrfs_get_extent_fiemap(BTRFS_I(inode
), NULL
, 0, offset
,
4295 if (IS_ERR_OR_NULL(em
))
4298 /* if this isn't a hole return it */
4299 if (em
->block_start
!= EXTENT_MAP_HOLE
)
4302 /* this is a hole, advance to the next extent */
4303 offset
= extent_map_end(em
);
4304 free_extent_map(em
);
4312 * To cache previous fiemap extent
4314 * Will be used for merging fiemap extent
4316 struct fiemap_cache
{
4325 * Helper to submit fiemap extent.
4327 * Will try to merge current fiemap extent specified by @offset, @phys,
4328 * @len and @flags with cached one.
4329 * And only when we fails to merge, cached one will be submitted as
4332 * Return value is the same as fiemap_fill_next_extent().
4334 static int emit_fiemap_extent(struct fiemap_extent_info
*fieinfo
,
4335 struct fiemap_cache
*cache
,
4336 u64 offset
, u64 phys
, u64 len
, u32 flags
)
4344 * Sanity check, extent_fiemap() should have ensured that new
4345 * fiemap extent won't overlap with cahced one.
4348 * NOTE: Physical address can overlap, due to compression
4350 if (cache
->offset
+ cache
->len
> offset
) {
4356 * Only merges fiemap extents if
4357 * 1) Their logical addresses are continuous
4359 * 2) Their physical addresses are continuous
4360 * So truly compressed (physical size smaller than logical size)
4361 * extents won't get merged with each other
4363 * 3) Share same flags except FIEMAP_EXTENT_LAST
4364 * So regular extent won't get merged with prealloc extent
4366 if (cache
->offset
+ cache
->len
== offset
&&
4367 cache
->phys
+ cache
->len
== phys
&&
4368 (cache
->flags
& ~FIEMAP_EXTENT_LAST
) ==
4369 (flags
& ~FIEMAP_EXTENT_LAST
)) {
4371 cache
->flags
|= flags
;
4372 goto try_submit_last
;
4375 /* Not mergeable, need to submit cached one */
4376 ret
= fiemap_fill_next_extent(fieinfo
, cache
->offset
, cache
->phys
,
4377 cache
->len
, cache
->flags
);
4378 cache
->cached
= false;
4382 cache
->cached
= true;
4383 cache
->offset
= offset
;
4386 cache
->flags
= flags
;
4388 if (cache
->flags
& FIEMAP_EXTENT_LAST
) {
4389 ret
= fiemap_fill_next_extent(fieinfo
, cache
->offset
,
4390 cache
->phys
, cache
->len
, cache
->flags
);
4391 cache
->cached
= false;
4397 * Emit last fiemap cache
4399 * The last fiemap cache may still be cached in the following case:
4401 * |<- Fiemap range ->|
4402 * |<------------ First extent ----------->|
4404 * In this case, the first extent range will be cached but not emitted.
4405 * So we must emit it before ending extent_fiemap().
4407 static int emit_last_fiemap_cache(struct btrfs_fs_info
*fs_info
,
4408 struct fiemap_extent_info
*fieinfo
,
4409 struct fiemap_cache
*cache
)
4416 ret
= fiemap_fill_next_extent(fieinfo
, cache
->offset
, cache
->phys
,
4417 cache
->len
, cache
->flags
);
4418 cache
->cached
= false;
4424 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
4425 __u64 start
, __u64 len
)
4429 u64 max
= start
+ len
;
4433 u64 last_for_get_extent
= 0;
4435 u64 isize
= i_size_read(inode
);
4436 struct btrfs_key found_key
;
4437 struct extent_map
*em
= NULL
;
4438 struct extent_state
*cached_state
= NULL
;
4439 struct btrfs_path
*path
;
4440 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4441 struct fiemap_cache cache
= { 0 };
4450 path
= btrfs_alloc_path();
4453 path
->leave_spinning
= 1;
4455 start
= round_down(start
, btrfs_inode_sectorsize(inode
));
4456 len
= round_up(max
, btrfs_inode_sectorsize(inode
)) - start
;
4459 * lookup the last file extent. We're not using i_size here
4460 * because there might be preallocation past i_size
4462 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
4463 btrfs_ino(BTRFS_I(inode
)), -1, 0);
4465 btrfs_free_path(path
);
4474 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, path
->slots
[0]);
4475 found_type
= found_key
.type
;
4477 /* No extents, but there might be delalloc bits */
4478 if (found_key
.objectid
!= btrfs_ino(BTRFS_I(inode
)) ||
4479 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
4480 /* have to trust i_size as the end */
4482 last_for_get_extent
= isize
;
4485 * remember the start of the last extent. There are a
4486 * bunch of different factors that go into the length of the
4487 * extent, so its much less complex to remember where it started
4489 last
= found_key
.offset
;
4490 last_for_get_extent
= last
+ 1;
4492 btrfs_release_path(path
);
4495 * we might have some extents allocated but more delalloc past those
4496 * extents. so, we trust isize unless the start of the last extent is
4501 last_for_get_extent
= isize
;
4504 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
- 1,
4507 em
= get_extent_skip_holes(inode
, start
, last_for_get_extent
);
4516 u64 offset_in_extent
= 0;
4518 /* break if the extent we found is outside the range */
4519 if (em
->start
>= max
|| extent_map_end(em
) < off
)
4523 * get_extent may return an extent that starts before our
4524 * requested range. We have to make sure the ranges
4525 * we return to fiemap always move forward and don't
4526 * overlap, so adjust the offsets here
4528 em_start
= max(em
->start
, off
);
4531 * record the offset from the start of the extent
4532 * for adjusting the disk offset below. Only do this if the
4533 * extent isn't compressed since our in ram offset may be past
4534 * what we have actually allocated on disk.
4536 if (!test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
4537 offset_in_extent
= em_start
- em
->start
;
4538 em_end
= extent_map_end(em
);
4539 em_len
= em_end
- em_start
;
4541 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
)
4542 disko
= em
->block_start
+ offset_in_extent
;
4547 * bump off for our next call to get_extent
4549 off
= extent_map_end(em
);
4553 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
4555 flags
|= FIEMAP_EXTENT_LAST
;
4556 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
4557 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
4558 FIEMAP_EXTENT_NOT_ALIGNED
);
4559 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
4560 flags
|= (FIEMAP_EXTENT_DELALLOC
|
4561 FIEMAP_EXTENT_UNKNOWN
);
4562 } else if (fieinfo
->fi_extents_max
) {
4563 u64 bytenr
= em
->block_start
-
4564 (em
->start
- em
->orig_start
);
4567 * As btrfs supports shared space, this information
4568 * can be exported to userspace tools via
4569 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4570 * then we're just getting a count and we can skip the
4573 ret
= btrfs_check_shared(root
,
4574 btrfs_ino(BTRFS_I(inode
)),
4579 flags
|= FIEMAP_EXTENT_SHARED
;
4582 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
4583 flags
|= FIEMAP_EXTENT_ENCODED
;
4584 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
4585 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
4587 free_extent_map(em
);
4589 if ((em_start
>= last
) || em_len
== (u64
)-1 ||
4590 (last
== (u64
)-1 && isize
<= em_end
)) {
4591 flags
|= FIEMAP_EXTENT_LAST
;
4595 /* now scan forward to see if this is really the last extent. */
4596 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
);
4602 flags
|= FIEMAP_EXTENT_LAST
;
4605 ret
= emit_fiemap_extent(fieinfo
, &cache
, em_start
, disko
,
4615 ret
= emit_last_fiemap_cache(root
->fs_info
, fieinfo
, &cache
);
4616 free_extent_map(em
);
4618 btrfs_free_path(path
);
4619 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
- 1,
4624 static void __free_extent_buffer(struct extent_buffer
*eb
)
4626 btrfs_leak_debug_del(&eb
->leak_list
);
4627 kmem_cache_free(extent_buffer_cache
, eb
);
4630 int extent_buffer_under_io(struct extent_buffer
*eb
)
4632 return (atomic_read(&eb
->io_pages
) ||
4633 test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
) ||
4634 test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4638 * Release all pages attached to the extent buffer.
4640 static void btrfs_release_extent_buffer_pages(struct extent_buffer
*eb
)
4644 int mapped
= !test_bit(EXTENT_BUFFER_UNMAPPED
, &eb
->bflags
);
4646 BUG_ON(extent_buffer_under_io(eb
));
4648 num_pages
= num_extent_pages(eb
);
4649 for (i
= 0; i
< num_pages
; i
++) {
4650 struct page
*page
= eb
->pages
[i
];
4655 spin_lock(&page
->mapping
->private_lock
);
4657 * We do this since we'll remove the pages after we've
4658 * removed the eb from the radix tree, so we could race
4659 * and have this page now attached to the new eb. So
4660 * only clear page_private if it's still connected to
4663 if (PagePrivate(page
) &&
4664 page
->private == (unsigned long)eb
) {
4665 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4666 BUG_ON(PageDirty(page
));
4667 BUG_ON(PageWriteback(page
));
4669 * We need to make sure we haven't be attached
4672 ClearPagePrivate(page
);
4673 set_page_private(page
, 0);
4674 /* One for the page private */
4679 spin_unlock(&page
->mapping
->private_lock
);
4681 /* One for when we allocated the page */
4687 * Helper for releasing the extent buffer.
4689 static inline void btrfs_release_extent_buffer(struct extent_buffer
*eb
)
4691 btrfs_release_extent_buffer_pages(eb
);
4692 __free_extent_buffer(eb
);
4695 static struct extent_buffer
*
4696 __alloc_extent_buffer(struct btrfs_fs_info
*fs_info
, u64 start
,
4699 struct extent_buffer
*eb
= NULL
;
4701 eb
= kmem_cache_zalloc(extent_buffer_cache
, GFP_NOFS
|__GFP_NOFAIL
);
4704 eb
->fs_info
= fs_info
;
4706 rwlock_init(&eb
->lock
);
4707 atomic_set(&eb
->write_locks
, 0);
4708 atomic_set(&eb
->read_locks
, 0);
4709 atomic_set(&eb
->blocking_readers
, 0);
4710 atomic_set(&eb
->blocking_writers
, 0);
4711 atomic_set(&eb
->spinning_readers
, 0);
4712 atomic_set(&eb
->spinning_writers
, 0);
4713 eb
->lock_nested
= 0;
4714 init_waitqueue_head(&eb
->write_lock_wq
);
4715 init_waitqueue_head(&eb
->read_lock_wq
);
4717 btrfs_leak_debug_add(&eb
->leak_list
, &buffers
);
4719 spin_lock_init(&eb
->refs_lock
);
4720 atomic_set(&eb
->refs
, 1);
4721 atomic_set(&eb
->io_pages
, 0);
4724 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4726 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4727 > MAX_INLINE_EXTENT_BUFFER_SIZE
);
4728 BUG_ON(len
> MAX_INLINE_EXTENT_BUFFER_SIZE
);
4733 struct extent_buffer
*btrfs_clone_extent_buffer(struct extent_buffer
*src
)
4737 struct extent_buffer
*new;
4738 int num_pages
= num_extent_pages(src
);
4740 new = __alloc_extent_buffer(src
->fs_info
, src
->start
, src
->len
);
4744 for (i
= 0; i
< num_pages
; i
++) {
4745 p
= alloc_page(GFP_NOFS
);
4747 btrfs_release_extent_buffer(new);
4750 attach_extent_buffer_page(new, p
);
4751 WARN_ON(PageDirty(p
));
4754 copy_page(page_address(p
), page_address(src
->pages
[i
]));
4757 set_bit(EXTENT_BUFFER_UPTODATE
, &new->bflags
);
4758 set_bit(EXTENT_BUFFER_UNMAPPED
, &new->bflags
);
4763 struct extent_buffer
*__alloc_dummy_extent_buffer(struct btrfs_fs_info
*fs_info
,
4764 u64 start
, unsigned long len
)
4766 struct extent_buffer
*eb
;
4770 eb
= __alloc_extent_buffer(fs_info
, start
, len
);
4774 num_pages
= num_extent_pages(eb
);
4775 for (i
= 0; i
< num_pages
; i
++) {
4776 eb
->pages
[i
] = alloc_page(GFP_NOFS
);
4780 set_extent_buffer_uptodate(eb
);
4781 btrfs_set_header_nritems(eb
, 0);
4782 set_bit(EXTENT_BUFFER_UNMAPPED
, &eb
->bflags
);
4787 __free_page(eb
->pages
[i
- 1]);
4788 __free_extent_buffer(eb
);
4792 struct extent_buffer
*alloc_dummy_extent_buffer(struct btrfs_fs_info
*fs_info
,
4795 return __alloc_dummy_extent_buffer(fs_info
, start
, fs_info
->nodesize
);
4798 static void check_buffer_tree_ref(struct extent_buffer
*eb
)
4801 /* the ref bit is tricky. We have to make sure it is set
4802 * if we have the buffer dirty. Otherwise the
4803 * code to free a buffer can end up dropping a dirty
4806 * Once the ref bit is set, it won't go away while the
4807 * buffer is dirty or in writeback, and it also won't
4808 * go away while we have the reference count on the
4811 * We can't just set the ref bit without bumping the
4812 * ref on the eb because free_extent_buffer might
4813 * see the ref bit and try to clear it. If this happens
4814 * free_extent_buffer might end up dropping our original
4815 * ref by mistake and freeing the page before we are able
4816 * to add one more ref.
4818 * So bump the ref count first, then set the bit. If someone
4819 * beat us to it, drop the ref we added.
4821 refs
= atomic_read(&eb
->refs
);
4822 if (refs
>= 2 && test_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4825 spin_lock(&eb
->refs_lock
);
4826 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4827 atomic_inc(&eb
->refs
);
4828 spin_unlock(&eb
->refs_lock
);
4831 static void mark_extent_buffer_accessed(struct extent_buffer
*eb
,
4832 struct page
*accessed
)
4836 check_buffer_tree_ref(eb
);
4838 num_pages
= num_extent_pages(eb
);
4839 for (i
= 0; i
< num_pages
; i
++) {
4840 struct page
*p
= eb
->pages
[i
];
4843 mark_page_accessed(p
);
4847 struct extent_buffer
*find_extent_buffer(struct btrfs_fs_info
*fs_info
,
4850 struct extent_buffer
*eb
;
4853 eb
= radix_tree_lookup(&fs_info
->buffer_radix
,
4854 start
>> PAGE_SHIFT
);
4855 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4858 * Lock our eb's refs_lock to avoid races with
4859 * free_extent_buffer. When we get our eb it might be flagged
4860 * with EXTENT_BUFFER_STALE and another task running
4861 * free_extent_buffer might have seen that flag set,
4862 * eb->refs == 2, that the buffer isn't under IO (dirty and
4863 * writeback flags not set) and it's still in the tree (flag
4864 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4865 * of decrementing the extent buffer's reference count twice.
4866 * So here we could race and increment the eb's reference count,
4867 * clear its stale flag, mark it as dirty and drop our reference
4868 * before the other task finishes executing free_extent_buffer,
4869 * which would later result in an attempt to free an extent
4870 * buffer that is dirty.
4872 if (test_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
)) {
4873 spin_lock(&eb
->refs_lock
);
4874 spin_unlock(&eb
->refs_lock
);
4876 mark_extent_buffer_accessed(eb
, NULL
);
4884 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4885 struct extent_buffer
*alloc_test_extent_buffer(struct btrfs_fs_info
*fs_info
,
4888 struct extent_buffer
*eb
, *exists
= NULL
;
4891 eb
= find_extent_buffer(fs_info
, start
);
4894 eb
= alloc_dummy_extent_buffer(fs_info
, start
);
4897 eb
->fs_info
= fs_info
;
4899 ret
= radix_tree_preload(GFP_NOFS
);
4902 spin_lock(&fs_info
->buffer_lock
);
4903 ret
= radix_tree_insert(&fs_info
->buffer_radix
,
4904 start
>> PAGE_SHIFT
, eb
);
4905 spin_unlock(&fs_info
->buffer_lock
);
4906 radix_tree_preload_end();
4907 if (ret
== -EEXIST
) {
4908 exists
= find_extent_buffer(fs_info
, start
);
4914 check_buffer_tree_ref(eb
);
4915 set_bit(EXTENT_BUFFER_IN_TREE
, &eb
->bflags
);
4918 * We will free dummy extent buffer's if they come into
4919 * free_extent_buffer with a ref count of 2, but if we are using this we
4920 * want the buffers to stay in memory until we're done with them, so
4921 * bump the ref count again.
4923 atomic_inc(&eb
->refs
);
4926 btrfs_release_extent_buffer(eb
);
4931 struct extent_buffer
*alloc_extent_buffer(struct btrfs_fs_info
*fs_info
,
4934 unsigned long len
= fs_info
->nodesize
;
4937 unsigned long index
= start
>> PAGE_SHIFT
;
4938 struct extent_buffer
*eb
;
4939 struct extent_buffer
*exists
= NULL
;
4941 struct address_space
*mapping
= fs_info
->btree_inode
->i_mapping
;
4945 if (!IS_ALIGNED(start
, fs_info
->sectorsize
)) {
4946 btrfs_err(fs_info
, "bad tree block start %llu", start
);
4947 return ERR_PTR(-EINVAL
);
4950 eb
= find_extent_buffer(fs_info
, start
);
4954 eb
= __alloc_extent_buffer(fs_info
, start
, len
);
4956 return ERR_PTR(-ENOMEM
);
4958 num_pages
= num_extent_pages(eb
);
4959 for (i
= 0; i
< num_pages
; i
++, index
++) {
4960 p
= find_or_create_page(mapping
, index
, GFP_NOFS
|__GFP_NOFAIL
);
4962 exists
= ERR_PTR(-ENOMEM
);
4966 spin_lock(&mapping
->private_lock
);
4967 if (PagePrivate(p
)) {
4969 * We could have already allocated an eb for this page
4970 * and attached one so lets see if we can get a ref on
4971 * the existing eb, and if we can we know it's good and
4972 * we can just return that one, else we know we can just
4973 * overwrite page->private.
4975 exists
= (struct extent_buffer
*)p
->private;
4976 if (atomic_inc_not_zero(&exists
->refs
)) {
4977 spin_unlock(&mapping
->private_lock
);
4980 mark_extent_buffer_accessed(exists
, p
);
4986 * Do this so attach doesn't complain and we need to
4987 * drop the ref the old guy had.
4989 ClearPagePrivate(p
);
4990 WARN_ON(PageDirty(p
));
4993 attach_extent_buffer_page(eb
, p
);
4994 spin_unlock(&mapping
->private_lock
);
4995 WARN_ON(PageDirty(p
));
4997 if (!PageUptodate(p
))
5001 * We can't unlock the pages just yet since the extent buffer
5002 * hasn't been properly inserted in the radix tree, this
5003 * opens a race with btree_releasepage which can free a page
5004 * while we are still filling in all pages for the buffer and
5009 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
5011 ret
= radix_tree_preload(GFP_NOFS
);
5013 exists
= ERR_PTR(ret
);
5017 spin_lock(&fs_info
->buffer_lock
);
5018 ret
= radix_tree_insert(&fs_info
->buffer_radix
,
5019 start
>> PAGE_SHIFT
, eb
);
5020 spin_unlock(&fs_info
->buffer_lock
);
5021 radix_tree_preload_end();
5022 if (ret
== -EEXIST
) {
5023 exists
= find_extent_buffer(fs_info
, start
);
5029 /* add one reference for the tree */
5030 check_buffer_tree_ref(eb
);
5031 set_bit(EXTENT_BUFFER_IN_TREE
, &eb
->bflags
);
5034 * Now it's safe to unlock the pages because any calls to
5035 * btree_releasepage will correctly detect that a page belongs to a
5036 * live buffer and won't free them prematurely.
5038 for (i
= 0; i
< num_pages
; i
++)
5039 unlock_page(eb
->pages
[i
]);
5043 WARN_ON(!atomic_dec_and_test(&eb
->refs
));
5044 for (i
= 0; i
< num_pages
; i
++) {
5046 unlock_page(eb
->pages
[i
]);
5049 btrfs_release_extent_buffer(eb
);
5053 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head
*head
)
5055 struct extent_buffer
*eb
=
5056 container_of(head
, struct extent_buffer
, rcu_head
);
5058 __free_extent_buffer(eb
);
5061 static int release_extent_buffer(struct extent_buffer
*eb
)
5063 lockdep_assert_held(&eb
->refs_lock
);
5065 WARN_ON(atomic_read(&eb
->refs
) == 0);
5066 if (atomic_dec_and_test(&eb
->refs
)) {
5067 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE
, &eb
->bflags
)) {
5068 struct btrfs_fs_info
*fs_info
= eb
->fs_info
;
5070 spin_unlock(&eb
->refs_lock
);
5072 spin_lock(&fs_info
->buffer_lock
);
5073 radix_tree_delete(&fs_info
->buffer_radix
,
5074 eb
->start
>> PAGE_SHIFT
);
5075 spin_unlock(&fs_info
->buffer_lock
);
5077 spin_unlock(&eb
->refs_lock
);
5080 /* Should be safe to release our pages at this point */
5081 btrfs_release_extent_buffer_pages(eb
);
5082 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5083 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED
, &eb
->bflags
))) {
5084 __free_extent_buffer(eb
);
5088 call_rcu(&eb
->rcu_head
, btrfs_release_extent_buffer_rcu
);
5091 spin_unlock(&eb
->refs_lock
);
5096 void free_extent_buffer(struct extent_buffer
*eb
)
5104 refs
= atomic_read(&eb
->refs
);
5107 old
= atomic_cmpxchg(&eb
->refs
, refs
, refs
- 1);
5112 spin_lock(&eb
->refs_lock
);
5113 if (atomic_read(&eb
->refs
) == 2 &&
5114 test_bit(EXTENT_BUFFER_UNMAPPED
, &eb
->bflags
))
5115 atomic_dec(&eb
->refs
);
5117 if (atomic_read(&eb
->refs
) == 2 &&
5118 test_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
) &&
5119 !extent_buffer_under_io(eb
) &&
5120 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
5121 atomic_dec(&eb
->refs
);
5124 * I know this is terrible, but it's temporary until we stop tracking
5125 * the uptodate bits and such for the extent buffers.
5127 release_extent_buffer(eb
);
5130 void free_extent_buffer_stale(struct extent_buffer
*eb
)
5135 spin_lock(&eb
->refs_lock
);
5136 set_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
);
5138 if (atomic_read(&eb
->refs
) == 2 && !extent_buffer_under_io(eb
) &&
5139 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
5140 atomic_dec(&eb
->refs
);
5141 release_extent_buffer(eb
);
5144 void clear_extent_buffer_dirty(struct extent_buffer
*eb
)
5150 num_pages
= num_extent_pages(eb
);
5152 for (i
= 0; i
< num_pages
; i
++) {
5153 page
= eb
->pages
[i
];
5154 if (!PageDirty(page
))
5158 WARN_ON(!PagePrivate(page
));
5160 clear_page_dirty_for_io(page
);
5161 xa_lock_irq(&page
->mapping
->i_pages
);
5162 if (!PageDirty(page
))
5163 __xa_clear_mark(&page
->mapping
->i_pages
,
5164 page_index(page
), PAGECACHE_TAG_DIRTY
);
5165 xa_unlock_irq(&page
->mapping
->i_pages
);
5166 ClearPageError(page
);
5169 WARN_ON(atomic_read(&eb
->refs
) == 0);
5172 bool set_extent_buffer_dirty(struct extent_buffer
*eb
)
5178 check_buffer_tree_ref(eb
);
5180 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
5182 num_pages
= num_extent_pages(eb
);
5183 WARN_ON(atomic_read(&eb
->refs
) == 0);
5184 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
));
5187 for (i
= 0; i
< num_pages
; i
++)
5188 set_page_dirty(eb
->pages
[i
]);
5190 #ifdef CONFIG_BTRFS_DEBUG
5191 for (i
= 0; i
< num_pages
; i
++)
5192 ASSERT(PageDirty(eb
->pages
[i
]));
5198 void clear_extent_buffer_uptodate(struct extent_buffer
*eb
)
5204 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
5205 num_pages
= num_extent_pages(eb
);
5206 for (i
= 0; i
< num_pages
; i
++) {
5207 page
= eb
->pages
[i
];
5209 ClearPageUptodate(page
);
5213 void set_extent_buffer_uptodate(struct extent_buffer
*eb
)
5219 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
5220 num_pages
= num_extent_pages(eb
);
5221 for (i
= 0; i
< num_pages
; i
++) {
5222 page
= eb
->pages
[i
];
5223 SetPageUptodate(page
);
5227 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
5228 struct extent_buffer
*eb
, int wait
, int mirror_num
)
5234 int locked_pages
= 0;
5235 int all_uptodate
= 1;
5237 unsigned long num_reads
= 0;
5238 struct bio
*bio
= NULL
;
5239 unsigned long bio_flags
= 0;
5241 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
5244 num_pages
= num_extent_pages(eb
);
5245 for (i
= 0; i
< num_pages
; i
++) {
5246 page
= eb
->pages
[i
];
5247 if (wait
== WAIT_NONE
) {
5248 if (!trylock_page(page
))
5256 * We need to firstly lock all pages to make sure that
5257 * the uptodate bit of our pages won't be affected by
5258 * clear_extent_buffer_uptodate().
5260 for (i
= 0; i
< num_pages
; i
++) {
5261 page
= eb
->pages
[i
];
5262 if (!PageUptodate(page
)) {
5269 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
5273 clear_bit(EXTENT_BUFFER_READ_ERR
, &eb
->bflags
);
5274 eb
->read_mirror
= 0;
5275 atomic_set(&eb
->io_pages
, num_reads
);
5276 for (i
= 0; i
< num_pages
; i
++) {
5277 page
= eb
->pages
[i
];
5279 if (!PageUptodate(page
)) {
5281 atomic_dec(&eb
->io_pages
);
5286 ClearPageError(page
);
5287 err
= __extent_read_full_page(tree
, page
,
5288 btree_get_extent
, &bio
,
5289 mirror_num
, &bio_flags
,
5294 * We use &bio in above __extent_read_full_page,
5295 * so we ensure that if it returns error, the
5296 * current page fails to add itself to bio and
5297 * it's been unlocked.
5299 * We must dec io_pages by ourselves.
5301 atomic_dec(&eb
->io_pages
);
5309 err
= submit_one_bio(bio
, mirror_num
, bio_flags
);
5314 if (ret
|| wait
!= WAIT_COMPLETE
)
5317 for (i
= 0; i
< num_pages
; i
++) {
5318 page
= eb
->pages
[i
];
5319 wait_on_page_locked(page
);
5320 if (!PageUptodate(page
))
5327 while (locked_pages
> 0) {
5329 page
= eb
->pages
[locked_pages
];
5335 void read_extent_buffer(const struct extent_buffer
*eb
, void *dstv
,
5336 unsigned long start
, unsigned long len
)
5342 char *dst
= (char *)dstv
;
5343 size_t start_offset
= eb
->start
& ((u64
)PAGE_SIZE
- 1);
5344 unsigned long i
= (start_offset
+ start
) >> PAGE_SHIFT
;
5346 if (start
+ len
> eb
->len
) {
5347 WARN(1, KERN_ERR
"btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5348 eb
->start
, eb
->len
, start
, len
);
5349 memset(dst
, 0, len
);
5353 offset
= (start_offset
+ start
) & (PAGE_SIZE
- 1);
5356 page
= eb
->pages
[i
];
5358 cur
= min(len
, (PAGE_SIZE
- offset
));
5359 kaddr
= page_address(page
);
5360 memcpy(dst
, kaddr
+ offset
, cur
);
5369 int read_extent_buffer_to_user(const struct extent_buffer
*eb
,
5371 unsigned long start
, unsigned long len
)
5377 char __user
*dst
= (char __user
*)dstv
;
5378 size_t start_offset
= eb
->start
& ((u64
)PAGE_SIZE
- 1);
5379 unsigned long i
= (start_offset
+ start
) >> PAGE_SHIFT
;
5382 WARN_ON(start
> eb
->len
);
5383 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
5385 offset
= (start_offset
+ start
) & (PAGE_SIZE
- 1);
5388 page
= eb
->pages
[i
];
5390 cur
= min(len
, (PAGE_SIZE
- offset
));
5391 kaddr
= page_address(page
);
5392 if (copy_to_user(dst
, kaddr
+ offset
, cur
)) {
5407 * return 0 if the item is found within a page.
5408 * return 1 if the item spans two pages.
5409 * return -EINVAL otherwise.
5411 int map_private_extent_buffer(const struct extent_buffer
*eb
,
5412 unsigned long start
, unsigned long min_len
,
5413 char **map
, unsigned long *map_start
,
5414 unsigned long *map_len
)
5416 size_t offset
= start
& (PAGE_SIZE
- 1);
5419 size_t start_offset
= eb
->start
& ((u64
)PAGE_SIZE
- 1);
5420 unsigned long i
= (start_offset
+ start
) >> PAGE_SHIFT
;
5421 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
5424 if (start
+ min_len
> eb
->len
) {
5425 WARN(1, KERN_ERR
"btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5426 eb
->start
, eb
->len
, start
, min_len
);
5434 offset
= start_offset
;
5438 *map_start
= ((u64
)i
<< PAGE_SHIFT
) - start_offset
;
5442 kaddr
= page_address(p
);
5443 *map
= kaddr
+ offset
;
5444 *map_len
= PAGE_SIZE
- offset
;
5448 int memcmp_extent_buffer(const struct extent_buffer
*eb
, const void *ptrv
,
5449 unsigned long start
, unsigned long len
)
5455 char *ptr
= (char *)ptrv
;
5456 size_t start_offset
= eb
->start
& ((u64
)PAGE_SIZE
- 1);
5457 unsigned long i
= (start_offset
+ start
) >> PAGE_SHIFT
;
5460 WARN_ON(start
> eb
->len
);
5461 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
5463 offset
= (start_offset
+ start
) & (PAGE_SIZE
- 1);
5466 page
= eb
->pages
[i
];
5468 cur
= min(len
, (PAGE_SIZE
- offset
));
5470 kaddr
= page_address(page
);
5471 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
5483 void write_extent_buffer_chunk_tree_uuid(struct extent_buffer
*eb
,
5488 WARN_ON(!PageUptodate(eb
->pages
[0]));
5489 kaddr
= page_address(eb
->pages
[0]);
5490 memcpy(kaddr
+ offsetof(struct btrfs_header
, chunk_tree_uuid
), srcv
,
5494 void write_extent_buffer_fsid(struct extent_buffer
*eb
, const void *srcv
)
5498 WARN_ON(!PageUptodate(eb
->pages
[0]));
5499 kaddr
= page_address(eb
->pages
[0]);
5500 memcpy(kaddr
+ offsetof(struct btrfs_header
, fsid
), srcv
,
5504 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
5505 unsigned long start
, unsigned long len
)
5511 char *src
= (char *)srcv
;
5512 size_t start_offset
= eb
->start
& ((u64
)PAGE_SIZE
- 1);
5513 unsigned long i
= (start_offset
+ start
) >> PAGE_SHIFT
;
5515 WARN_ON(start
> eb
->len
);
5516 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
5518 offset
= (start_offset
+ start
) & (PAGE_SIZE
- 1);
5521 page
= eb
->pages
[i
];
5522 WARN_ON(!PageUptodate(page
));
5524 cur
= min(len
, PAGE_SIZE
- offset
);
5525 kaddr
= page_address(page
);
5526 memcpy(kaddr
+ offset
, src
, cur
);
5535 void memzero_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
5542 size_t start_offset
= eb
->start
& ((u64
)PAGE_SIZE
- 1);
5543 unsigned long i
= (start_offset
+ start
) >> PAGE_SHIFT
;
5545 WARN_ON(start
> eb
->len
);
5546 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
5548 offset
= (start_offset
+ start
) & (PAGE_SIZE
- 1);
5551 page
= eb
->pages
[i
];
5552 WARN_ON(!PageUptodate(page
));
5554 cur
= min(len
, PAGE_SIZE
- offset
);
5555 kaddr
= page_address(page
);
5556 memset(kaddr
+ offset
, 0, cur
);
5564 void copy_extent_buffer_full(struct extent_buffer
*dst
,
5565 struct extent_buffer
*src
)
5570 ASSERT(dst
->len
== src
->len
);
5572 num_pages
= num_extent_pages(dst
);
5573 for (i
= 0; i
< num_pages
; i
++)
5574 copy_page(page_address(dst
->pages
[i
]),
5575 page_address(src
->pages
[i
]));
5578 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
5579 unsigned long dst_offset
, unsigned long src_offset
,
5582 u64 dst_len
= dst
->len
;
5587 size_t start_offset
= dst
->start
& ((u64
)PAGE_SIZE
- 1);
5588 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_SHIFT
;
5590 WARN_ON(src
->len
!= dst_len
);
5592 offset
= (start_offset
+ dst_offset
) &
5596 page
= dst
->pages
[i
];
5597 WARN_ON(!PageUptodate(page
));
5599 cur
= min(len
, (unsigned long)(PAGE_SIZE
- offset
));
5601 kaddr
= page_address(page
);
5602 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
5612 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5614 * @eb: the extent buffer
5615 * @start: offset of the bitmap item in the extent buffer
5617 * @page_index: return index of the page in the extent buffer that contains the
5619 * @page_offset: return offset into the page given by page_index
5621 * This helper hides the ugliness of finding the byte in an extent buffer which
5622 * contains a given bit.
5624 static inline void eb_bitmap_offset(struct extent_buffer
*eb
,
5625 unsigned long start
, unsigned long nr
,
5626 unsigned long *page_index
,
5627 size_t *page_offset
)
5629 size_t start_offset
= eb
->start
& ((u64
)PAGE_SIZE
- 1);
5630 size_t byte_offset
= BIT_BYTE(nr
);
5634 * The byte we want is the offset of the extent buffer + the offset of
5635 * the bitmap item in the extent buffer + the offset of the byte in the
5638 offset
= start_offset
+ start
+ byte_offset
;
5640 *page_index
= offset
>> PAGE_SHIFT
;
5641 *page_offset
= offset
& (PAGE_SIZE
- 1);
5645 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5646 * @eb: the extent buffer
5647 * @start: offset of the bitmap item in the extent buffer
5648 * @nr: bit number to test
5650 int extent_buffer_test_bit(struct extent_buffer
*eb
, unsigned long start
,
5658 eb_bitmap_offset(eb
, start
, nr
, &i
, &offset
);
5659 page
= eb
->pages
[i
];
5660 WARN_ON(!PageUptodate(page
));
5661 kaddr
= page_address(page
);
5662 return 1U & (kaddr
[offset
] >> (nr
& (BITS_PER_BYTE
- 1)));
5666 * extent_buffer_bitmap_set - set an area of a bitmap
5667 * @eb: the extent buffer
5668 * @start: offset of the bitmap item in the extent buffer
5669 * @pos: bit number of the first bit
5670 * @len: number of bits to set
5672 void extent_buffer_bitmap_set(struct extent_buffer
*eb
, unsigned long start
,
5673 unsigned long pos
, unsigned long len
)
5679 const unsigned int size
= pos
+ len
;
5680 int bits_to_set
= BITS_PER_BYTE
- (pos
% BITS_PER_BYTE
);
5681 u8 mask_to_set
= BITMAP_FIRST_BYTE_MASK(pos
);
5683 eb_bitmap_offset(eb
, start
, pos
, &i
, &offset
);
5684 page
= eb
->pages
[i
];
5685 WARN_ON(!PageUptodate(page
));
5686 kaddr
= page_address(page
);
5688 while (len
>= bits_to_set
) {
5689 kaddr
[offset
] |= mask_to_set
;
5691 bits_to_set
= BITS_PER_BYTE
;
5693 if (++offset
>= PAGE_SIZE
&& len
> 0) {
5695 page
= eb
->pages
[++i
];
5696 WARN_ON(!PageUptodate(page
));
5697 kaddr
= page_address(page
);
5701 mask_to_set
&= BITMAP_LAST_BYTE_MASK(size
);
5702 kaddr
[offset
] |= mask_to_set
;
5708 * extent_buffer_bitmap_clear - clear an area of a bitmap
5709 * @eb: the extent buffer
5710 * @start: offset of the bitmap item in the extent buffer
5711 * @pos: bit number of the first bit
5712 * @len: number of bits to clear
5714 void extent_buffer_bitmap_clear(struct extent_buffer
*eb
, unsigned long start
,
5715 unsigned long pos
, unsigned long len
)
5721 const unsigned int size
= pos
+ len
;
5722 int bits_to_clear
= BITS_PER_BYTE
- (pos
% BITS_PER_BYTE
);
5723 u8 mask_to_clear
= BITMAP_FIRST_BYTE_MASK(pos
);
5725 eb_bitmap_offset(eb
, start
, pos
, &i
, &offset
);
5726 page
= eb
->pages
[i
];
5727 WARN_ON(!PageUptodate(page
));
5728 kaddr
= page_address(page
);
5730 while (len
>= bits_to_clear
) {
5731 kaddr
[offset
] &= ~mask_to_clear
;
5732 len
-= bits_to_clear
;
5733 bits_to_clear
= BITS_PER_BYTE
;
5735 if (++offset
>= PAGE_SIZE
&& len
> 0) {
5737 page
= eb
->pages
[++i
];
5738 WARN_ON(!PageUptodate(page
));
5739 kaddr
= page_address(page
);
5743 mask_to_clear
&= BITMAP_LAST_BYTE_MASK(size
);
5744 kaddr
[offset
] &= ~mask_to_clear
;
5748 static inline bool areas_overlap(unsigned long src
, unsigned long dst
, unsigned long len
)
5750 unsigned long distance
= (src
> dst
) ? src
- dst
: dst
- src
;
5751 return distance
< len
;
5754 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
5755 unsigned long dst_off
, unsigned long src_off
,
5758 char *dst_kaddr
= page_address(dst_page
);
5760 int must_memmove
= 0;
5762 if (dst_page
!= src_page
) {
5763 src_kaddr
= page_address(src_page
);
5765 src_kaddr
= dst_kaddr
;
5766 if (areas_overlap(src_off
, dst_off
, len
))
5771 memmove(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
5773 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
5776 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
5777 unsigned long src_offset
, unsigned long len
)
5779 struct btrfs_fs_info
*fs_info
= dst
->fs_info
;
5781 size_t dst_off_in_page
;
5782 size_t src_off_in_page
;
5783 size_t start_offset
= dst
->start
& ((u64
)PAGE_SIZE
- 1);
5784 unsigned long dst_i
;
5785 unsigned long src_i
;
5787 if (src_offset
+ len
> dst
->len
) {
5789 "memmove bogus src_offset %lu move len %lu dst len %lu",
5790 src_offset
, len
, dst
->len
);
5793 if (dst_offset
+ len
> dst
->len
) {
5795 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5796 dst_offset
, len
, dst
->len
);
5801 dst_off_in_page
= (start_offset
+ dst_offset
) &
5803 src_off_in_page
= (start_offset
+ src_offset
) &
5806 dst_i
= (start_offset
+ dst_offset
) >> PAGE_SHIFT
;
5807 src_i
= (start_offset
+ src_offset
) >> PAGE_SHIFT
;
5809 cur
= min(len
, (unsigned long)(PAGE_SIZE
-
5811 cur
= min_t(unsigned long, cur
,
5812 (unsigned long)(PAGE_SIZE
- dst_off_in_page
));
5814 copy_pages(dst
->pages
[dst_i
], dst
->pages
[src_i
],
5815 dst_off_in_page
, src_off_in_page
, cur
);
5823 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
5824 unsigned long src_offset
, unsigned long len
)
5826 struct btrfs_fs_info
*fs_info
= dst
->fs_info
;
5828 size_t dst_off_in_page
;
5829 size_t src_off_in_page
;
5830 unsigned long dst_end
= dst_offset
+ len
- 1;
5831 unsigned long src_end
= src_offset
+ len
- 1;
5832 size_t start_offset
= dst
->start
& ((u64
)PAGE_SIZE
- 1);
5833 unsigned long dst_i
;
5834 unsigned long src_i
;
5836 if (src_offset
+ len
> dst
->len
) {
5838 "memmove bogus src_offset %lu move len %lu len %lu",
5839 src_offset
, len
, dst
->len
);
5842 if (dst_offset
+ len
> dst
->len
) {
5844 "memmove bogus dst_offset %lu move len %lu len %lu",
5845 dst_offset
, len
, dst
->len
);
5848 if (dst_offset
< src_offset
) {
5849 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
5853 dst_i
= (start_offset
+ dst_end
) >> PAGE_SHIFT
;
5854 src_i
= (start_offset
+ src_end
) >> PAGE_SHIFT
;
5856 dst_off_in_page
= (start_offset
+ dst_end
) &
5858 src_off_in_page
= (start_offset
+ src_end
) &
5861 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
5862 cur
= min(cur
, dst_off_in_page
+ 1);
5863 copy_pages(dst
->pages
[dst_i
], dst
->pages
[src_i
],
5864 dst_off_in_page
- cur
+ 1,
5865 src_off_in_page
- cur
+ 1, cur
);
5873 int try_release_extent_buffer(struct page
*page
)
5875 struct extent_buffer
*eb
;
5878 * We need to make sure nobody is attaching this page to an eb right
5881 spin_lock(&page
->mapping
->private_lock
);
5882 if (!PagePrivate(page
)) {
5883 spin_unlock(&page
->mapping
->private_lock
);
5887 eb
= (struct extent_buffer
*)page
->private;
5891 * This is a little awful but should be ok, we need to make sure that
5892 * the eb doesn't disappear out from under us while we're looking at
5895 spin_lock(&eb
->refs_lock
);
5896 if (atomic_read(&eb
->refs
) != 1 || extent_buffer_under_io(eb
)) {
5897 spin_unlock(&eb
->refs_lock
);
5898 spin_unlock(&page
->mapping
->private_lock
);
5901 spin_unlock(&page
->mapping
->private_lock
);
5904 * If tree ref isn't set then we know the ref on this eb is a real ref,
5905 * so just return, this page will likely be freed soon anyway.
5907 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
)) {
5908 spin_unlock(&eb
->refs_lock
);
5912 return release_extent_buffer(eb
);