1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/module.h>
8 #include <linux/spinlock.h>
9 #include <linux/blkdev.h>
10 #include <linux/swap.h>
11 #include <linux/writeback.h>
12 #include <linux/pagevec.h>
13 #include <linux/prefetch.h>
14 #include <linux/cleancache.h>
15 #include "extent_io.h"
16 #include "extent_map.h"
19 #include "btrfs_inode.h"
21 #include "check-integrity.h"
23 #include "rcu-string.h"
25 static struct kmem_cache
*extent_state_cache
;
26 static struct kmem_cache
*extent_buffer_cache
;
28 static LIST_HEAD(buffers
);
29 static LIST_HEAD(states
);
33 static DEFINE_SPINLOCK(leak_lock
);
36 #define BUFFER_LRU_MAX 64
41 struct rb_node rb_node
;
44 struct extent_page_data
{
46 struct extent_io_tree
*tree
;
47 get_extent_t
*get_extent
;
48 unsigned long bio_flags
;
50 /* tells writepage not to lock the state bits for this range
51 * it still does the unlocking
53 unsigned int extent_locked
:1;
55 /* tells the submit_bio code to use a WRITE_SYNC */
56 unsigned int sync_io
:1;
59 static noinline
void flush_write_bio(void *data
);
60 static inline struct btrfs_fs_info
*
61 tree_fs_info(struct extent_io_tree
*tree
)
63 return btrfs_sb(tree
->mapping
->host
->i_sb
);
66 int __init
extent_io_init(void)
68 extent_state_cache
= kmem_cache_create("btrfs_extent_state",
69 sizeof(struct extent_state
), 0,
70 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
71 if (!extent_state_cache
)
74 extent_buffer_cache
= kmem_cache_create("btrfs_extent_buffer",
75 sizeof(struct extent_buffer
), 0,
76 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
77 if (!extent_buffer_cache
)
78 goto free_state_cache
;
82 kmem_cache_destroy(extent_state_cache
);
86 void extent_io_exit(void)
88 struct extent_state
*state
;
89 struct extent_buffer
*eb
;
91 while (!list_empty(&states
)) {
92 state
= list_entry(states
.next
, struct extent_state
, leak_list
);
93 printk(KERN_ERR
"btrfs state leak: start %llu end %llu "
94 "state %lu in tree %p refs %d\n",
95 (unsigned long long)state
->start
,
96 (unsigned long long)state
->end
,
97 state
->state
, state
->tree
, atomic_read(&state
->refs
));
98 list_del(&state
->leak_list
);
99 kmem_cache_free(extent_state_cache
, state
);
103 while (!list_empty(&buffers
)) {
104 eb
= list_entry(buffers
.next
, struct extent_buffer
, leak_list
);
105 printk(KERN_ERR
"btrfs buffer leak start %llu len %lu "
106 "refs %d\n", (unsigned long long)eb
->start
,
107 eb
->len
, atomic_read(&eb
->refs
));
108 list_del(&eb
->leak_list
);
109 kmem_cache_free(extent_buffer_cache
, eb
);
113 * Make sure all delayed rcu free are flushed before we
117 if (extent_state_cache
)
118 kmem_cache_destroy(extent_state_cache
);
119 if (extent_buffer_cache
)
120 kmem_cache_destroy(extent_buffer_cache
);
123 void extent_io_tree_init(struct extent_io_tree
*tree
,
124 struct address_space
*mapping
)
126 tree
->state
= RB_ROOT
;
127 INIT_RADIX_TREE(&tree
->buffer
, GFP_ATOMIC
);
129 tree
->dirty_bytes
= 0;
130 spin_lock_init(&tree
->lock
);
131 spin_lock_init(&tree
->buffer_lock
);
132 tree
->mapping
= mapping
;
135 static struct extent_state
*alloc_extent_state(gfp_t mask
)
137 struct extent_state
*state
;
142 state
= kmem_cache_alloc(extent_state_cache
, mask
);
149 spin_lock_irqsave(&leak_lock
, flags
);
150 list_add(&state
->leak_list
, &states
);
151 spin_unlock_irqrestore(&leak_lock
, flags
);
153 atomic_set(&state
->refs
, 1);
154 init_waitqueue_head(&state
->wq
);
155 trace_alloc_extent_state(state
, mask
, _RET_IP_
);
159 void free_extent_state(struct extent_state
*state
)
163 if (atomic_dec_and_test(&state
->refs
)) {
167 WARN_ON(state
->tree
);
169 spin_lock_irqsave(&leak_lock
, flags
);
170 list_del(&state
->leak_list
);
171 spin_unlock_irqrestore(&leak_lock
, flags
);
173 trace_free_extent_state(state
, _RET_IP_
);
174 kmem_cache_free(extent_state_cache
, state
);
178 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
179 struct rb_node
*node
)
181 struct rb_node
**p
= &root
->rb_node
;
182 struct rb_node
*parent
= NULL
;
183 struct tree_entry
*entry
;
187 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
189 if (offset
< entry
->start
)
191 else if (offset
> entry
->end
)
197 rb_link_node(node
, parent
, p
);
198 rb_insert_color(node
, root
);
202 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
203 struct rb_node
**prev_ret
,
204 struct rb_node
**next_ret
)
206 struct rb_root
*root
= &tree
->state
;
207 struct rb_node
*n
= root
->rb_node
;
208 struct rb_node
*prev
= NULL
;
209 struct rb_node
*orig_prev
= NULL
;
210 struct tree_entry
*entry
;
211 struct tree_entry
*prev_entry
= NULL
;
214 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
218 if (offset
< entry
->start
)
220 else if (offset
> entry
->end
)
228 while (prev
&& offset
> prev_entry
->end
) {
229 prev
= rb_next(prev
);
230 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
237 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
238 while (prev
&& offset
< prev_entry
->start
) {
239 prev
= rb_prev(prev
);
240 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
247 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
250 struct rb_node
*prev
= NULL
;
253 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
259 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
260 struct extent_state
*other
)
262 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
263 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
268 * utility function to look for merge candidates inside a given range.
269 * Any extents with matching state are merged together into a single
270 * extent in the tree. Extents with EXTENT_IO in their state field
271 * are not merged because the end_io handlers need to be able to do
272 * operations on them without sleeping (or doing allocations/splits).
274 * This should be called with the tree lock held.
276 static void merge_state(struct extent_io_tree
*tree
,
277 struct extent_state
*state
)
279 struct extent_state
*other
;
280 struct rb_node
*other_node
;
282 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
285 other_node
= rb_prev(&state
->rb_node
);
287 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
288 if (other
->end
== state
->start
- 1 &&
289 other
->state
== state
->state
) {
290 merge_cb(tree
, state
, other
);
291 state
->start
= other
->start
;
293 rb_erase(&other
->rb_node
, &tree
->state
);
294 free_extent_state(other
);
297 other_node
= rb_next(&state
->rb_node
);
299 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
300 if (other
->start
== state
->end
+ 1 &&
301 other
->state
== state
->state
) {
302 merge_cb(tree
, state
, other
);
303 state
->end
= other
->end
;
305 rb_erase(&other
->rb_node
, &tree
->state
);
306 free_extent_state(other
);
311 static void set_state_cb(struct extent_io_tree
*tree
,
312 struct extent_state
*state
, int *bits
)
314 if (tree
->ops
&& tree
->ops
->set_bit_hook
)
315 tree
->ops
->set_bit_hook(tree
->mapping
->host
, state
, bits
);
318 static void clear_state_cb(struct extent_io_tree
*tree
,
319 struct extent_state
*state
, int *bits
)
321 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
322 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
, bits
);
325 static void set_state_bits(struct extent_io_tree
*tree
,
326 struct extent_state
*state
, int *bits
);
329 * insert an extent_state struct into the tree. 'bits' are set on the
330 * struct before it is inserted.
332 * This may return -EEXIST if the extent is already there, in which case the
333 * state struct is freed.
335 * The tree lock is not taken internally. This is a utility function and
336 * probably isn't what you want to call (see set/clear_extent_bit).
338 static int insert_state(struct extent_io_tree
*tree
,
339 struct extent_state
*state
, u64 start
, u64 end
,
342 struct rb_node
*node
;
345 WARN(1, KERN_ERR
"btrfs end < start %llu %llu\n",
346 (unsigned long long)end
,
347 (unsigned long long)start
);
348 state
->start
= start
;
351 set_state_bits(tree
, state
, bits
);
353 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
355 struct extent_state
*found
;
356 found
= rb_entry(node
, struct extent_state
, rb_node
);
357 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
358 "%llu %llu\n", (unsigned long long)found
->start
,
359 (unsigned long long)found
->end
,
360 (unsigned long long)start
, (unsigned long long)end
);
364 merge_state(tree
, state
);
368 static void split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
371 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
372 tree
->ops
->split_extent_hook(tree
->mapping
->host
, orig
, split
);
376 * split a given extent state struct in two, inserting the preallocated
377 * struct 'prealloc' as the newly created second half. 'split' indicates an
378 * offset inside 'orig' where it should be split.
381 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
382 * are two extent state structs in the tree:
383 * prealloc: [orig->start, split - 1]
384 * orig: [ split, orig->end ]
386 * The tree locks are not taken by this function. They need to be held
389 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
390 struct extent_state
*prealloc
, u64 split
)
392 struct rb_node
*node
;
394 split_cb(tree
, orig
, split
);
396 prealloc
->start
= orig
->start
;
397 prealloc
->end
= split
- 1;
398 prealloc
->state
= orig
->state
;
401 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
403 free_extent_state(prealloc
);
406 prealloc
->tree
= tree
;
410 static struct extent_state
*next_state(struct extent_state
*state
)
412 struct rb_node
*next
= rb_next(&state
->rb_node
);
414 return rb_entry(next
, struct extent_state
, rb_node
);
420 * utility function to clear some bits in an extent state struct.
421 * it will optionally wake up any one waiting on this state (wake == 1).
423 * If no bits are set on the state struct after clearing things, the
424 * struct is freed and removed from the tree
426 static struct extent_state
*clear_state_bit(struct extent_io_tree
*tree
,
427 struct extent_state
*state
,
430 struct extent_state
*next
;
431 int bits_to_clear
= *bits
& ~EXTENT_CTLBITS
;
433 if ((bits_to_clear
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
434 u64 range
= state
->end
- state
->start
+ 1;
435 WARN_ON(range
> tree
->dirty_bytes
);
436 tree
->dirty_bytes
-= range
;
438 clear_state_cb(tree
, state
, bits
);
439 state
->state
&= ~bits_to_clear
;
442 if (state
->state
== 0) {
443 next
= next_state(state
);
445 rb_erase(&state
->rb_node
, &tree
->state
);
447 free_extent_state(state
);
452 merge_state(tree
, state
);
453 next
= next_state(state
);
458 static struct extent_state
*
459 alloc_extent_state_atomic(struct extent_state
*prealloc
)
462 prealloc
= alloc_extent_state(GFP_ATOMIC
);
467 void extent_io_tree_panic(struct extent_io_tree
*tree
, int err
)
469 btrfs_panic(tree_fs_info(tree
), err
, "Locking error: "
470 "Extent tree was modified by another "
471 "thread while locked.");
475 * clear some bits on a range in the tree. This may require splitting
476 * or inserting elements in the tree, so the gfp mask is used to
477 * indicate which allocations or sleeping are allowed.
479 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
480 * the given range from the tree regardless of state (ie for truncate).
482 * the range [start, end] is inclusive.
484 * This takes the tree lock, and returns 0 on success and < 0 on error.
486 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
487 int bits
, int wake
, int delete,
488 struct extent_state
**cached_state
,
491 struct extent_state
*state
;
492 struct extent_state
*cached
;
493 struct extent_state
*prealloc
= NULL
;
494 struct rb_node
*node
;
500 bits
|= ~EXTENT_CTLBITS
;
501 bits
|= EXTENT_FIRST_DELALLOC
;
503 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
506 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
507 prealloc
= alloc_extent_state(mask
);
512 spin_lock(&tree
->lock
);
514 cached
= *cached_state
;
517 *cached_state
= NULL
;
521 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
522 cached
->end
> start
) {
524 atomic_dec(&cached
->refs
);
529 free_extent_state(cached
);
532 * this search will find the extents that end after
535 node
= tree_search(tree
, start
);
538 state
= rb_entry(node
, struct extent_state
, rb_node
);
540 if (state
->start
> end
)
542 WARN_ON(state
->end
< start
);
543 last_end
= state
->end
;
545 /* the state doesn't have the wanted bits, go ahead */
546 if (!(state
->state
& bits
)) {
547 state
= next_state(state
);
552 * | ---- desired range ---- |
554 * | ------------- state -------------- |
556 * We need to split the extent we found, and may flip
557 * bits on second half.
559 * If the extent we found extends past our range, we
560 * just split and search again. It'll get split again
561 * the next time though.
563 * If the extent we found is inside our range, we clear
564 * the desired bit on it.
567 if (state
->start
< start
) {
568 prealloc
= alloc_extent_state_atomic(prealloc
);
570 err
= split_state(tree
, state
, prealloc
, start
);
572 extent_io_tree_panic(tree
, err
);
577 if (state
->end
<= end
) {
578 state
= clear_state_bit(tree
, state
, &bits
, wake
);
584 * | ---- desired range ---- |
586 * We need to split the extent, and clear the bit
589 if (state
->start
<= end
&& state
->end
> end
) {
590 prealloc
= alloc_extent_state_atomic(prealloc
);
592 err
= split_state(tree
, state
, prealloc
, end
+ 1);
594 extent_io_tree_panic(tree
, err
);
599 clear_state_bit(tree
, prealloc
, &bits
, wake
);
605 state
= clear_state_bit(tree
, state
, &bits
, wake
);
607 if (last_end
== (u64
)-1)
609 start
= last_end
+ 1;
610 if (start
<= end
&& state
&& !need_resched())
615 spin_unlock(&tree
->lock
);
617 free_extent_state(prealloc
);
624 spin_unlock(&tree
->lock
);
625 if (mask
& __GFP_WAIT
)
630 static void wait_on_state(struct extent_io_tree
*tree
,
631 struct extent_state
*state
)
632 __releases(tree
->lock
)
633 __acquires(tree
->lock
)
636 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
637 spin_unlock(&tree
->lock
);
639 spin_lock(&tree
->lock
);
640 finish_wait(&state
->wq
, &wait
);
644 * waits for one or more bits to clear on a range in the state tree.
645 * The range [start, end] is inclusive.
646 * The tree lock is taken by this function
648 void wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
650 struct extent_state
*state
;
651 struct rb_node
*node
;
653 spin_lock(&tree
->lock
);
657 * this search will find all the extents that end after
660 node
= tree_search(tree
, start
);
664 state
= rb_entry(node
, struct extent_state
, rb_node
);
666 if (state
->start
> end
)
669 if (state
->state
& bits
) {
670 start
= state
->start
;
671 atomic_inc(&state
->refs
);
672 wait_on_state(tree
, state
);
673 free_extent_state(state
);
676 start
= state
->end
+ 1;
681 cond_resched_lock(&tree
->lock
);
684 spin_unlock(&tree
->lock
);
687 static void set_state_bits(struct extent_io_tree
*tree
,
688 struct extent_state
*state
,
691 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
693 set_state_cb(tree
, state
, bits
);
694 if ((bits_to_set
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
695 u64 range
= state
->end
- state
->start
+ 1;
696 tree
->dirty_bytes
+= range
;
698 state
->state
|= bits_to_set
;
701 static void cache_state(struct extent_state
*state
,
702 struct extent_state
**cached_ptr
)
704 if (cached_ptr
&& !(*cached_ptr
)) {
705 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
707 atomic_inc(&state
->refs
);
712 static void uncache_state(struct extent_state
**cached_ptr
)
714 if (cached_ptr
&& (*cached_ptr
)) {
715 struct extent_state
*state
= *cached_ptr
;
717 free_extent_state(state
);
722 * set some bits on a range in the tree. This may require allocations or
723 * sleeping, so the gfp mask is used to indicate what is allowed.
725 * If any of the exclusive bits are set, this will fail with -EEXIST if some
726 * part of the range already has the desired bits set. The start of the
727 * existing range is returned in failed_start in this case.
729 * [start, end] is inclusive This takes the tree lock.
732 static int __must_check
733 __set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
734 int bits
, int exclusive_bits
, u64
*failed_start
,
735 struct extent_state
**cached_state
, gfp_t mask
)
737 struct extent_state
*state
;
738 struct extent_state
*prealloc
= NULL
;
739 struct rb_node
*node
;
744 bits
|= EXTENT_FIRST_DELALLOC
;
746 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
747 prealloc
= alloc_extent_state(mask
);
751 spin_lock(&tree
->lock
);
752 if (cached_state
&& *cached_state
) {
753 state
= *cached_state
;
754 if (state
->start
<= start
&& state
->end
> start
&&
756 node
= &state
->rb_node
;
761 * this search will find all the extents that end after
764 node
= tree_search(tree
, start
);
766 prealloc
= alloc_extent_state_atomic(prealloc
);
768 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
770 extent_io_tree_panic(tree
, err
);
775 state
= rb_entry(node
, struct extent_state
, rb_node
);
777 last_start
= state
->start
;
778 last_end
= state
->end
;
781 * | ---- desired range ---- |
784 * Just lock what we found and keep going
786 if (state
->start
== start
&& state
->end
<= end
) {
787 if (state
->state
& exclusive_bits
) {
788 *failed_start
= state
->start
;
793 set_state_bits(tree
, state
, &bits
);
794 cache_state(state
, cached_state
);
795 merge_state(tree
, state
);
796 if (last_end
== (u64
)-1)
798 start
= last_end
+ 1;
799 state
= next_state(state
);
800 if (start
< end
&& state
&& state
->start
== start
&&
807 * | ---- desired range ---- |
810 * | ------------- state -------------- |
812 * We need to split the extent we found, and may flip bits on
815 * If the extent we found extends past our
816 * range, we just split and search again. It'll get split
817 * again the next time though.
819 * If the extent we found is inside our range, we set the
822 if (state
->start
< start
) {
823 if (state
->state
& exclusive_bits
) {
824 *failed_start
= start
;
829 prealloc
= alloc_extent_state_atomic(prealloc
);
831 err
= split_state(tree
, state
, prealloc
, start
);
833 extent_io_tree_panic(tree
, err
);
838 if (state
->end
<= end
) {
839 set_state_bits(tree
, state
, &bits
);
840 cache_state(state
, cached_state
);
841 merge_state(tree
, state
);
842 if (last_end
== (u64
)-1)
844 start
= last_end
+ 1;
845 state
= next_state(state
);
846 if (start
< end
&& state
&& state
->start
== start
&&
853 * | ---- desired range ---- |
854 * | state | or | state |
856 * There's a hole, we need to insert something in it and
857 * ignore the extent we found.
859 if (state
->start
> start
) {
861 if (end
< last_start
)
864 this_end
= last_start
- 1;
866 prealloc
= alloc_extent_state_atomic(prealloc
);
870 * Avoid to free 'prealloc' if it can be merged with
873 err
= insert_state(tree
, prealloc
, start
, this_end
,
876 extent_io_tree_panic(tree
, err
);
878 cache_state(prealloc
, cached_state
);
880 start
= this_end
+ 1;
884 * | ---- desired range ---- |
886 * We need to split the extent, and set the bit
889 if (state
->start
<= end
&& state
->end
> end
) {
890 if (state
->state
& exclusive_bits
) {
891 *failed_start
= start
;
896 prealloc
= alloc_extent_state_atomic(prealloc
);
898 err
= split_state(tree
, state
, prealloc
, end
+ 1);
900 extent_io_tree_panic(tree
, err
);
902 set_state_bits(tree
, prealloc
, &bits
);
903 cache_state(prealloc
, cached_state
);
904 merge_state(tree
, prealloc
);
912 spin_unlock(&tree
->lock
);
914 free_extent_state(prealloc
);
921 spin_unlock(&tree
->lock
);
922 if (mask
& __GFP_WAIT
)
927 int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
,
928 u64
*failed_start
, struct extent_state
**cached_state
,
931 return __set_extent_bit(tree
, start
, end
, bits
, 0, failed_start
,
937 * convert_extent_bit - convert all bits in a given range from one bit to
939 * @tree: the io tree to search
940 * @start: the start offset in bytes
941 * @end: the end offset in bytes (inclusive)
942 * @bits: the bits to set in this range
943 * @clear_bits: the bits to clear in this range
944 * @cached_state: state that we're going to cache
945 * @mask: the allocation mask
947 * This will go through and set bits for the given range. If any states exist
948 * already in this range they are set with the given bit and cleared of the
949 * clear_bits. This is only meant to be used by things that are mergeable, ie
950 * converting from say DELALLOC to DIRTY. This is not meant to be used with
951 * boundary bits like LOCK.
953 int convert_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
954 int bits
, int clear_bits
,
955 struct extent_state
**cached_state
, gfp_t mask
)
957 struct extent_state
*state
;
958 struct extent_state
*prealloc
= NULL
;
959 struct rb_node
*node
;
965 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
966 prealloc
= alloc_extent_state(mask
);
971 spin_lock(&tree
->lock
);
972 if (cached_state
&& *cached_state
) {
973 state
= *cached_state
;
974 if (state
->start
<= start
&& state
->end
> start
&&
976 node
= &state
->rb_node
;
982 * this search will find all the extents that end after
985 node
= tree_search(tree
, start
);
987 prealloc
= alloc_extent_state_atomic(prealloc
);
992 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
995 extent_io_tree_panic(tree
, err
);
998 state
= rb_entry(node
, struct extent_state
, rb_node
);
1000 last_start
= state
->start
;
1001 last_end
= state
->end
;
1004 * | ---- desired range ---- |
1007 * Just lock what we found and keep going
1009 if (state
->start
== start
&& state
->end
<= end
) {
1010 set_state_bits(tree
, state
, &bits
);
1011 cache_state(state
, cached_state
);
1012 state
= clear_state_bit(tree
, state
, &clear_bits
, 0);
1013 if (last_end
== (u64
)-1)
1015 start
= last_end
+ 1;
1016 if (start
< end
&& state
&& state
->start
== start
&&
1023 * | ---- desired range ---- |
1026 * | ------------- state -------------- |
1028 * We need to split the extent we found, and may flip bits on
1031 * If the extent we found extends past our
1032 * range, we just split and search again. It'll get split
1033 * again the next time though.
1035 * If the extent we found is inside our range, we set the
1036 * desired bit on it.
1038 if (state
->start
< start
) {
1039 prealloc
= alloc_extent_state_atomic(prealloc
);
1044 err
= split_state(tree
, state
, prealloc
, start
);
1046 extent_io_tree_panic(tree
, err
);
1050 if (state
->end
<= end
) {
1051 set_state_bits(tree
, state
, &bits
);
1052 cache_state(state
, cached_state
);
1053 state
= clear_state_bit(tree
, state
, &clear_bits
, 0);
1054 if (last_end
== (u64
)-1)
1056 start
= last_end
+ 1;
1057 if (start
< end
&& state
&& state
->start
== start
&&
1064 * | ---- desired range ---- |
1065 * | state | or | state |
1067 * There's a hole, we need to insert something in it and
1068 * ignore the extent we found.
1070 if (state
->start
> start
) {
1072 if (end
< last_start
)
1075 this_end
= last_start
- 1;
1077 prealloc
= alloc_extent_state_atomic(prealloc
);
1084 * Avoid to free 'prealloc' if it can be merged with
1087 err
= insert_state(tree
, prealloc
, start
, this_end
,
1090 extent_io_tree_panic(tree
, err
);
1091 cache_state(prealloc
, cached_state
);
1093 start
= this_end
+ 1;
1097 * | ---- desired range ---- |
1099 * We need to split the extent, and set the bit
1102 if (state
->start
<= end
&& state
->end
> end
) {
1103 prealloc
= alloc_extent_state_atomic(prealloc
);
1109 err
= split_state(tree
, state
, prealloc
, end
+ 1);
1111 extent_io_tree_panic(tree
, err
);
1113 set_state_bits(tree
, prealloc
, &bits
);
1114 cache_state(prealloc
, cached_state
);
1115 clear_state_bit(tree
, prealloc
, &clear_bits
, 0);
1123 spin_unlock(&tree
->lock
);
1125 free_extent_state(prealloc
);
1132 spin_unlock(&tree
->lock
);
1133 if (mask
& __GFP_WAIT
)
1138 /* wrappers around set/clear extent bit */
1139 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1142 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, NULL
,
1146 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1147 int bits
, gfp_t mask
)
1149 return set_extent_bit(tree
, start
, end
, bits
, NULL
,
1153 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1154 int bits
, gfp_t mask
)
1156 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
1159 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1160 struct extent_state
**cached_state
, gfp_t mask
)
1162 return set_extent_bit(tree
, start
, end
,
1163 EXTENT_DELALLOC
| EXTENT_UPTODATE
,
1164 NULL
, cached_state
, mask
);
1167 int set_extent_defrag(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1168 struct extent_state
**cached_state
, gfp_t mask
)
1170 return set_extent_bit(tree
, start
, end
,
1171 EXTENT_DELALLOC
| EXTENT_UPTODATE
| EXTENT_DEFRAG
,
1172 NULL
, cached_state
, mask
);
1175 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1178 return clear_extent_bit(tree
, start
, end
,
1179 EXTENT_DIRTY
| EXTENT_DELALLOC
|
1180 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, mask
);
1183 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1186 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, NULL
,
1190 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1191 struct extent_state
**cached_state
, gfp_t mask
)
1193 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0,
1194 cached_state
, mask
);
1197 int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1198 struct extent_state
**cached_state
, gfp_t mask
)
1200 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
1201 cached_state
, mask
);
1205 * either insert or lock state struct between start and end use mask to tell
1206 * us if waiting is desired.
1208 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1209 int bits
, struct extent_state
**cached_state
)
1214 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
1215 EXTENT_LOCKED
, &failed_start
,
1216 cached_state
, GFP_NOFS
);
1217 if (err
== -EEXIST
) {
1218 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
1219 start
= failed_start
;
1222 WARN_ON(start
> end
);
1227 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1229 return lock_extent_bits(tree
, start
, end
, 0, NULL
);
1232 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1237 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1238 &failed_start
, NULL
, GFP_NOFS
);
1239 if (err
== -EEXIST
) {
1240 if (failed_start
> start
)
1241 clear_extent_bit(tree
, start
, failed_start
- 1,
1242 EXTENT_LOCKED
, 1, 0, NULL
, GFP_NOFS
);
1248 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1249 struct extent_state
**cached
, gfp_t mask
)
1251 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1255 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1257 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1262 * helper function to set both pages and extents in the tree writeback
1264 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1266 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1267 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1270 while (index
<= end_index
) {
1271 page
= find_get_page(tree
->mapping
, index
);
1272 BUG_ON(!page
); /* Pages should be in the extent_io_tree */
1273 set_page_writeback(page
);
1274 page_cache_release(page
);
1280 /* find the first state struct with 'bits' set after 'start', and
1281 * return it. tree->lock must be held. NULL will returned if
1282 * nothing was found after 'start'
1284 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1285 u64 start
, int bits
)
1287 struct rb_node
*node
;
1288 struct extent_state
*state
;
1291 * this search will find all the extents that end after
1294 node
= tree_search(tree
, start
);
1299 state
= rb_entry(node
, struct extent_state
, rb_node
);
1300 if (state
->end
>= start
&& (state
->state
& bits
))
1303 node
= rb_next(node
);
1312 * find the first offset in the io tree with 'bits' set. zero is
1313 * returned if we find something, and *start_ret and *end_ret are
1314 * set to reflect the state struct that was found.
1316 * If nothing was found, 1 is returned. If found something, return 0.
1318 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1319 u64
*start_ret
, u64
*end_ret
, int bits
,
1320 struct extent_state
**cached_state
)
1322 struct extent_state
*state
;
1326 spin_lock(&tree
->lock
);
1327 if (cached_state
&& *cached_state
) {
1328 state
= *cached_state
;
1329 if (state
->end
== start
- 1 && state
->tree
) {
1330 n
= rb_next(&state
->rb_node
);
1332 state
= rb_entry(n
, struct extent_state
,
1334 if (state
->state
& bits
)
1338 free_extent_state(*cached_state
);
1339 *cached_state
= NULL
;
1342 free_extent_state(*cached_state
);
1343 *cached_state
= NULL
;
1346 state
= find_first_extent_bit_state(tree
, start
, bits
);
1349 cache_state(state
, cached_state
);
1350 *start_ret
= state
->start
;
1351 *end_ret
= state
->end
;
1355 spin_unlock(&tree
->lock
);
1360 * find a contiguous range of bytes in the file marked as delalloc, not
1361 * more than 'max_bytes'. start and end are used to return the range,
1363 * 1 is returned if we find something, 0 if nothing was in the tree
1365 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1366 u64
*start
, u64
*end
, u64 max_bytes
,
1367 struct extent_state
**cached_state
)
1369 struct rb_node
*node
;
1370 struct extent_state
*state
;
1371 u64 cur_start
= *start
;
1373 u64 total_bytes
= 0;
1375 spin_lock(&tree
->lock
);
1378 * this search will find all the extents that end after
1381 node
= tree_search(tree
, cur_start
);
1389 state
= rb_entry(node
, struct extent_state
, rb_node
);
1390 if (found
&& (state
->start
!= cur_start
||
1391 (state
->state
& EXTENT_BOUNDARY
))) {
1394 if (!(state
->state
& EXTENT_DELALLOC
)) {
1400 *start
= state
->start
;
1401 *cached_state
= state
;
1402 atomic_inc(&state
->refs
);
1406 cur_start
= state
->end
+ 1;
1407 node
= rb_next(node
);
1410 total_bytes
+= state
->end
- state
->start
+ 1;
1411 if (total_bytes
>= max_bytes
)
1415 spin_unlock(&tree
->lock
);
1419 static noinline
void __unlock_for_delalloc(struct inode
*inode
,
1420 struct page
*locked_page
,
1424 struct page
*pages
[16];
1425 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1426 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1427 unsigned long nr_pages
= end_index
- index
+ 1;
1430 if (index
== locked_page
->index
&& end_index
== index
)
1433 while (nr_pages
> 0) {
1434 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1435 min_t(unsigned long, nr_pages
,
1436 ARRAY_SIZE(pages
)), pages
);
1437 for (i
= 0; i
< ret
; i
++) {
1438 if (pages
[i
] != locked_page
)
1439 unlock_page(pages
[i
]);
1440 page_cache_release(pages
[i
]);
1448 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1449 struct page
*locked_page
,
1453 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1454 unsigned long start_index
= index
;
1455 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1456 unsigned long pages_locked
= 0;
1457 struct page
*pages
[16];
1458 unsigned long nrpages
;
1462 /* the caller is responsible for locking the start index */
1463 if (index
== locked_page
->index
&& index
== end_index
)
1466 /* skip the page at the start index */
1467 nrpages
= end_index
- index
+ 1;
1468 while (nrpages
> 0) {
1469 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1470 min_t(unsigned long,
1471 nrpages
, ARRAY_SIZE(pages
)), pages
);
1476 /* now we have an array of pages, lock them all */
1477 for (i
= 0; i
< ret
; i
++) {
1479 * the caller is taking responsibility for
1482 if (pages
[i
] != locked_page
) {
1483 lock_page(pages
[i
]);
1484 if (!PageDirty(pages
[i
]) ||
1485 pages
[i
]->mapping
!= inode
->i_mapping
) {
1487 unlock_page(pages
[i
]);
1488 page_cache_release(pages
[i
]);
1492 page_cache_release(pages
[i
]);
1501 if (ret
&& pages_locked
) {
1502 __unlock_for_delalloc(inode
, locked_page
,
1504 ((u64
)(start_index
+ pages_locked
- 1)) <<
1511 * find a contiguous range of bytes in the file marked as delalloc, not
1512 * more than 'max_bytes'. start and end are used to return the range,
1514 * 1 is returned if we find something, 0 if nothing was in the tree
1516 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1517 struct extent_io_tree
*tree
,
1518 struct page
*locked_page
,
1519 u64
*start
, u64
*end
,
1525 struct extent_state
*cached_state
= NULL
;
1530 /* step one, find a bunch of delalloc bytes starting at start */
1531 delalloc_start
= *start
;
1533 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1534 max_bytes
, &cached_state
);
1535 if (!found
|| delalloc_end
<= *start
) {
1536 *start
= delalloc_start
;
1537 *end
= delalloc_end
;
1538 free_extent_state(cached_state
);
1543 * start comes from the offset of locked_page. We have to lock
1544 * pages in order, so we can't process delalloc bytes before
1547 if (delalloc_start
< *start
)
1548 delalloc_start
= *start
;
1551 * make sure to limit the number of pages we try to lock down
1554 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1555 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1557 /* step two, lock all the pages after the page that has start */
1558 ret
= lock_delalloc_pages(inode
, locked_page
,
1559 delalloc_start
, delalloc_end
);
1560 if (ret
== -EAGAIN
) {
1561 /* some of the pages are gone, lets avoid looping by
1562 * shortening the size of the delalloc range we're searching
1564 free_extent_state(cached_state
);
1566 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1567 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1575 BUG_ON(ret
); /* Only valid values are 0 and -EAGAIN */
1577 /* step three, lock the state bits for the whole range */
1578 lock_extent_bits(tree
, delalloc_start
, delalloc_end
, 0, &cached_state
);
1580 /* then test to make sure it is all still delalloc */
1581 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1582 EXTENT_DELALLOC
, 1, cached_state
);
1584 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1585 &cached_state
, GFP_NOFS
);
1586 __unlock_for_delalloc(inode
, locked_page
,
1587 delalloc_start
, delalloc_end
);
1591 free_extent_state(cached_state
);
1592 *start
= delalloc_start
;
1593 *end
= delalloc_end
;
1598 int extent_clear_unlock_delalloc(struct inode
*inode
,
1599 struct extent_io_tree
*tree
,
1600 u64 start
, u64 end
, struct page
*locked_page
,
1604 struct page
*pages
[16];
1605 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1606 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1607 unsigned long nr_pages
= end_index
- index
+ 1;
1611 if (op
& EXTENT_CLEAR_UNLOCK
)
1612 clear_bits
|= EXTENT_LOCKED
;
1613 if (op
& EXTENT_CLEAR_DIRTY
)
1614 clear_bits
|= EXTENT_DIRTY
;
1616 if (op
& EXTENT_CLEAR_DELALLOC
)
1617 clear_bits
|= EXTENT_DELALLOC
;
1619 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1620 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1621 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1622 EXTENT_SET_PRIVATE2
)))
1625 while (nr_pages
> 0) {
1626 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1627 min_t(unsigned long,
1628 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1629 for (i
= 0; i
< ret
; i
++) {
1631 if (op
& EXTENT_SET_PRIVATE2
)
1632 SetPagePrivate2(pages
[i
]);
1634 if (pages
[i
] == locked_page
) {
1635 page_cache_release(pages
[i
]);
1638 if (op
& EXTENT_CLEAR_DIRTY
)
1639 clear_page_dirty_for_io(pages
[i
]);
1640 if (op
& EXTENT_SET_WRITEBACK
)
1641 set_page_writeback(pages
[i
]);
1642 if (op
& EXTENT_END_WRITEBACK
)
1643 end_page_writeback(pages
[i
]);
1644 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1645 unlock_page(pages
[i
]);
1646 page_cache_release(pages
[i
]);
1656 * count the number of bytes in the tree that have a given bit(s)
1657 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1658 * cached. The total number found is returned.
1660 u64
count_range_bits(struct extent_io_tree
*tree
,
1661 u64
*start
, u64 search_end
, u64 max_bytes
,
1662 unsigned long bits
, int contig
)
1664 struct rb_node
*node
;
1665 struct extent_state
*state
;
1666 u64 cur_start
= *start
;
1667 u64 total_bytes
= 0;
1671 if (search_end
<= cur_start
) {
1676 spin_lock(&tree
->lock
);
1677 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1678 total_bytes
= tree
->dirty_bytes
;
1682 * this search will find all the extents that end after
1685 node
= tree_search(tree
, cur_start
);
1690 state
= rb_entry(node
, struct extent_state
, rb_node
);
1691 if (state
->start
> search_end
)
1693 if (contig
&& found
&& state
->start
> last
+ 1)
1695 if (state
->end
>= cur_start
&& (state
->state
& bits
) == bits
) {
1696 total_bytes
+= min(search_end
, state
->end
) + 1 -
1697 max(cur_start
, state
->start
);
1698 if (total_bytes
>= max_bytes
)
1701 *start
= max(cur_start
, state
->start
);
1705 } else if (contig
&& found
) {
1708 node
= rb_next(node
);
1713 spin_unlock(&tree
->lock
);
1718 * set the private field for a given byte offset in the tree. If there isn't
1719 * an extent_state there already, this does nothing.
1721 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1723 struct rb_node
*node
;
1724 struct extent_state
*state
;
1727 spin_lock(&tree
->lock
);
1729 * this search will find all the extents that end after
1732 node
= tree_search(tree
, start
);
1737 state
= rb_entry(node
, struct extent_state
, rb_node
);
1738 if (state
->start
!= start
) {
1742 state
->private = private;
1744 spin_unlock(&tree
->lock
);
1748 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1750 struct rb_node
*node
;
1751 struct extent_state
*state
;
1754 spin_lock(&tree
->lock
);
1756 * this search will find all the extents that end after
1759 node
= tree_search(tree
, start
);
1764 state
= rb_entry(node
, struct extent_state
, rb_node
);
1765 if (state
->start
!= start
) {
1769 *private = state
->private;
1771 spin_unlock(&tree
->lock
);
1776 * searches a range in the state tree for a given mask.
1777 * If 'filled' == 1, this returns 1 only if every extent in the tree
1778 * has the bits set. Otherwise, 1 is returned if any bit in the
1779 * range is found set.
1781 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1782 int bits
, int filled
, struct extent_state
*cached
)
1784 struct extent_state
*state
= NULL
;
1785 struct rb_node
*node
;
1788 spin_lock(&tree
->lock
);
1789 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
1790 cached
->end
> start
)
1791 node
= &cached
->rb_node
;
1793 node
= tree_search(tree
, start
);
1794 while (node
&& start
<= end
) {
1795 state
= rb_entry(node
, struct extent_state
, rb_node
);
1797 if (filled
&& state
->start
> start
) {
1802 if (state
->start
> end
)
1805 if (state
->state
& bits
) {
1809 } else if (filled
) {
1814 if (state
->end
== (u64
)-1)
1817 start
= state
->end
+ 1;
1820 node
= rb_next(node
);
1827 spin_unlock(&tree
->lock
);
1832 * helper function to set a given page up to date if all the
1833 * extents in the tree for that page are up to date
1835 static void check_page_uptodate(struct extent_io_tree
*tree
, struct page
*page
)
1837 u64 start
= page_offset(page
);
1838 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1839 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1840 SetPageUptodate(page
);
1844 * helper function to unlock a page if all the extents in the tree
1845 * for that page are unlocked
1847 static void check_page_locked(struct extent_io_tree
*tree
, struct page
*page
)
1849 u64 start
= page_offset(page
);
1850 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1851 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1856 * helper function to end page writeback if all the extents
1857 * in the tree for that page are done with writeback
1859 static void check_page_writeback(struct extent_io_tree
*tree
,
1862 end_page_writeback(page
);
1866 * When IO fails, either with EIO or csum verification fails, we
1867 * try other mirrors that might have a good copy of the data. This
1868 * io_failure_record is used to record state as we go through all the
1869 * mirrors. If another mirror has good data, the page is set up to date
1870 * and things continue. If a good mirror can't be found, the original
1871 * bio end_io callback is called to indicate things have failed.
1873 struct io_failure_record
{
1878 unsigned long bio_flags
;
1884 static int free_io_failure(struct inode
*inode
, struct io_failure_record
*rec
,
1889 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1891 set_state_private(failure_tree
, rec
->start
, 0);
1892 ret
= clear_extent_bits(failure_tree
, rec
->start
,
1893 rec
->start
+ rec
->len
- 1,
1894 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1898 ret
= clear_extent_bits(&BTRFS_I(inode
)->io_tree
, rec
->start
,
1899 rec
->start
+ rec
->len
- 1,
1900 EXTENT_DAMAGED
, GFP_NOFS
);
1908 static void repair_io_failure_callback(struct bio
*bio
, int err
)
1910 complete(bio
->bi_private
);
1914 * this bypasses the standard btrfs submit functions deliberately, as
1915 * the standard behavior is to write all copies in a raid setup. here we only
1916 * want to write the one bad copy. so we do the mapping for ourselves and issue
1917 * submit_bio directly.
1918 * to avoid any synchronization issues, wait for the data after writing, which
1919 * actually prevents the read that triggered the error from finishing.
1920 * currently, there can be no more than two copies of every data bit. thus,
1921 * exactly one rewrite is required.
1923 int repair_io_failure(struct btrfs_fs_info
*fs_info
, u64 start
,
1924 u64 length
, u64 logical
, struct page
*page
,
1928 struct btrfs_device
*dev
;
1929 DECLARE_COMPLETION_ONSTACK(compl);
1932 struct btrfs_bio
*bbio
= NULL
;
1933 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
1936 BUG_ON(!mirror_num
);
1938 /* we can't repair anything in raid56 yet */
1939 if (btrfs_is_parity_mirror(map_tree
, logical
, length
, mirror_num
))
1942 bio
= bio_alloc(GFP_NOFS
, 1);
1945 bio
->bi_private
= &compl;
1946 bio
->bi_end_io
= repair_io_failure_callback
;
1948 map_length
= length
;
1950 ret
= btrfs_map_block(fs_info
, WRITE
, logical
,
1951 &map_length
, &bbio
, mirror_num
);
1956 BUG_ON(mirror_num
!= bbio
->mirror_num
);
1957 sector
= bbio
->stripes
[mirror_num
-1].physical
>> 9;
1958 bio
->bi_sector
= sector
;
1959 dev
= bbio
->stripes
[mirror_num
-1].dev
;
1961 if (!dev
|| !dev
->bdev
|| !dev
->writeable
) {
1965 bio
->bi_bdev
= dev
->bdev
;
1966 bio_add_page(bio
, page
, length
, start
- page_offset(page
));
1967 btrfsic_submit_bio(WRITE_SYNC
, bio
);
1968 wait_for_completion(&compl);
1970 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
1971 /* try to remap that extent elsewhere? */
1973 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_WRITE_ERRS
);
1977 printk_ratelimited_in_rcu(KERN_INFO
"btrfs read error corrected: ino %lu off %llu "
1978 "(dev %s sector %llu)\n", page
->mapping
->host
->i_ino
,
1979 start
, rcu_str_deref(dev
->name
), sector
);
1985 int repair_eb_io_failure(struct btrfs_root
*root
, struct extent_buffer
*eb
,
1988 u64 start
= eb
->start
;
1989 unsigned long i
, num_pages
= num_extent_pages(eb
->start
, eb
->len
);
1992 for (i
= 0; i
< num_pages
; i
++) {
1993 struct page
*p
= extent_buffer_page(eb
, i
);
1994 ret
= repair_io_failure(root
->fs_info
, start
, PAGE_CACHE_SIZE
,
1995 start
, p
, mirror_num
);
1998 start
+= PAGE_CACHE_SIZE
;
2005 * each time an IO finishes, we do a fast check in the IO failure tree
2006 * to see if we need to process or clean up an io_failure_record
2008 static int clean_io_failure(u64 start
, struct page
*page
)
2011 u64 private_failure
;
2012 struct io_failure_record
*failrec
;
2013 struct btrfs_fs_info
*fs_info
;
2014 struct extent_state
*state
;
2018 struct inode
*inode
= page
->mapping
->host
;
2021 ret
= count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
2022 (u64
)-1, 1, EXTENT_DIRTY
, 0);
2026 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
, start
,
2031 failrec
= (struct io_failure_record
*)(unsigned long) private_failure
;
2032 BUG_ON(!failrec
->this_mirror
);
2034 if (failrec
->in_validation
) {
2035 /* there was no real error, just free the record */
2036 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2042 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
2043 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
2046 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
2048 if (state
&& state
->start
== failrec
->start
) {
2049 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2050 num_copies
= btrfs_num_copies(fs_info
, failrec
->logical
,
2052 if (num_copies
> 1) {
2053 ret
= repair_io_failure(fs_info
, start
, failrec
->len
,
2054 failrec
->logical
, page
,
2055 failrec
->failed_mirror
);
2063 ret
= free_io_failure(inode
, failrec
, did_repair
);
2069 * this is a generic handler for readpage errors (default
2070 * readpage_io_failed_hook). if other copies exist, read those and write back
2071 * good data to the failed position. does not investigate in remapping the
2072 * failed extent elsewhere, hoping the device will be smart enough to do this as
2076 static int bio_readpage_error(struct bio
*failed_bio
, struct page
*page
,
2077 u64 start
, u64 end
, int failed_mirror
,
2078 struct extent_state
*state
)
2080 struct io_failure_record
*failrec
= NULL
;
2082 struct extent_map
*em
;
2083 struct inode
*inode
= page
->mapping
->host
;
2084 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
2085 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2086 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2093 BUG_ON(failed_bio
->bi_rw
& REQ_WRITE
);
2095 ret
= get_state_private(failure_tree
, start
, &private);
2097 failrec
= kzalloc(sizeof(*failrec
), GFP_NOFS
);
2100 failrec
->start
= start
;
2101 failrec
->len
= end
- start
+ 1;
2102 failrec
->this_mirror
= 0;
2103 failrec
->bio_flags
= 0;
2104 failrec
->in_validation
= 0;
2106 read_lock(&em_tree
->lock
);
2107 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
2109 read_unlock(&em_tree
->lock
);
2114 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
2115 free_extent_map(em
);
2118 read_unlock(&em_tree
->lock
);
2124 logical
= start
- em
->start
;
2125 logical
= em
->block_start
+ logical
;
2126 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2127 logical
= em
->block_start
;
2128 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
2129 extent_set_compress_type(&failrec
->bio_flags
,
2132 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2133 "len=%llu\n", logical
, start
, failrec
->len
);
2134 failrec
->logical
= logical
;
2135 free_extent_map(em
);
2137 /* set the bits in the private failure tree */
2138 ret
= set_extent_bits(failure_tree
, start
, end
,
2139 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
2141 ret
= set_state_private(failure_tree
, start
,
2142 (u64
)(unsigned long)failrec
);
2143 /* set the bits in the inode's tree */
2145 ret
= set_extent_bits(tree
, start
, end
, EXTENT_DAMAGED
,
2152 failrec
= (struct io_failure_record
*)(unsigned long)private;
2153 pr_debug("bio_readpage_error: (found) logical=%llu, "
2154 "start=%llu, len=%llu, validation=%d\n",
2155 failrec
->logical
, failrec
->start
, failrec
->len
,
2156 failrec
->in_validation
);
2158 * when data can be on disk more than twice, add to failrec here
2159 * (e.g. with a list for failed_mirror) to make
2160 * clean_io_failure() clean all those errors at once.
2163 num_copies
= btrfs_num_copies(BTRFS_I(inode
)->root
->fs_info
,
2164 failrec
->logical
, failrec
->len
);
2165 if (num_copies
== 1) {
2167 * we only have a single copy of the data, so don't bother with
2168 * all the retry and error correction code that follows. no
2169 * matter what the error is, it is very likely to persist.
2171 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2172 "state=%p, num_copies=%d, next_mirror %d, "
2173 "failed_mirror %d\n", state
, num_copies
,
2174 failrec
->this_mirror
, failed_mirror
);
2175 free_io_failure(inode
, failrec
, 0);
2180 spin_lock(&tree
->lock
);
2181 state
= find_first_extent_bit_state(tree
, failrec
->start
,
2183 if (state
&& state
->start
!= failrec
->start
)
2185 spin_unlock(&tree
->lock
);
2189 * there are two premises:
2190 * a) deliver good data to the caller
2191 * b) correct the bad sectors on disk
2193 if (failed_bio
->bi_vcnt
> 1) {
2195 * to fulfill b), we need to know the exact failing sectors, as
2196 * we don't want to rewrite any more than the failed ones. thus,
2197 * we need separate read requests for the failed bio
2199 * if the following BUG_ON triggers, our validation request got
2200 * merged. we need separate requests for our algorithm to work.
2202 BUG_ON(failrec
->in_validation
);
2203 failrec
->in_validation
= 1;
2204 failrec
->this_mirror
= failed_mirror
;
2205 read_mode
= READ_SYNC
| REQ_FAILFAST_DEV
;
2208 * we're ready to fulfill a) and b) alongside. get a good copy
2209 * of the failed sector and if we succeed, we have setup
2210 * everything for repair_io_failure to do the rest for us.
2212 if (failrec
->in_validation
) {
2213 BUG_ON(failrec
->this_mirror
!= failed_mirror
);
2214 failrec
->in_validation
= 0;
2215 failrec
->this_mirror
= 0;
2217 failrec
->failed_mirror
= failed_mirror
;
2218 failrec
->this_mirror
++;
2219 if (failrec
->this_mirror
== failed_mirror
)
2220 failrec
->this_mirror
++;
2221 read_mode
= READ_SYNC
;
2224 if (!state
|| failrec
->this_mirror
> num_copies
) {
2225 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2226 "next_mirror %d, failed_mirror %d\n", state
,
2227 num_copies
, failrec
->this_mirror
, failed_mirror
);
2228 free_io_failure(inode
, failrec
, 0);
2232 bio
= bio_alloc(GFP_NOFS
, 1);
2234 free_io_failure(inode
, failrec
, 0);
2237 bio
->bi_private
= state
;
2238 bio
->bi_end_io
= failed_bio
->bi_end_io
;
2239 bio
->bi_sector
= failrec
->logical
>> 9;
2240 bio
->bi_bdev
= BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
;
2243 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
2245 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2246 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode
,
2247 failrec
->this_mirror
, num_copies
, failrec
->in_validation
);
2249 ret
= tree
->ops
->submit_bio_hook(inode
, read_mode
, bio
,
2250 failrec
->this_mirror
,
2251 failrec
->bio_flags
, 0);
2255 /* lots and lots of room for performance fixes in the end_bio funcs */
2257 int end_extent_writepage(struct page
*page
, int err
, u64 start
, u64 end
)
2259 int uptodate
= (err
== 0);
2260 struct extent_io_tree
*tree
;
2263 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2265 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
2266 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
2267 end
, NULL
, uptodate
);
2273 ClearPageUptodate(page
);
2280 * after a writepage IO is done, we need to:
2281 * clear the uptodate bits on error
2282 * clear the writeback bits in the extent tree for this IO
2283 * end_page_writeback if the page has no more pending IO
2285 * Scheduling is not allowed, so the extent state tree is expected
2286 * to have one and only one object corresponding to this IO.
2288 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
2290 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2291 struct extent_io_tree
*tree
;
2297 struct page
*page
= bvec
->bv_page
;
2298 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2300 start
= page_offset(page
) + bvec
->bv_offset
;
2301 end
= start
+ bvec
->bv_len
- 1;
2303 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2308 if (--bvec
>= bio
->bi_io_vec
)
2309 prefetchw(&bvec
->bv_page
->flags
);
2311 if (end_extent_writepage(page
, err
, start
, end
))
2315 end_page_writeback(page
);
2317 check_page_writeback(tree
, page
);
2318 } while (bvec
>= bio
->bi_io_vec
);
2324 * after a readpage IO is done, we need to:
2325 * clear the uptodate bits on error
2326 * set the uptodate bits if things worked
2327 * set the page up to date if all extents in the tree are uptodate
2328 * clear the lock bit in the extent tree
2329 * unlock the page if there are no other extents locked for it
2331 * Scheduling is not allowed, so the extent state tree is expected
2332 * to have one and only one object corresponding to this IO.
2334 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
2336 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2337 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2338 struct bio_vec
*bvec
= bio
->bi_io_vec
;
2339 struct extent_io_tree
*tree
;
2350 struct page
*page
= bvec
->bv_page
;
2351 struct extent_state
*cached
= NULL
;
2352 struct extent_state
*state
;
2354 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2355 "mirror=%ld\n", (u64
)bio
->bi_sector
, err
,
2356 (long int)bio
->bi_bdev
);
2357 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2359 start
= page_offset(page
) + bvec
->bv_offset
;
2360 end
= start
+ bvec
->bv_len
- 1;
2362 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2367 if (++bvec
<= bvec_end
)
2368 prefetchw(&bvec
->bv_page
->flags
);
2370 spin_lock(&tree
->lock
);
2371 state
= find_first_extent_bit_state(tree
, start
, EXTENT_LOCKED
);
2372 if (state
&& state
->start
== start
) {
2374 * take a reference on the state, unlock will drop
2377 cache_state(state
, &cached
);
2379 spin_unlock(&tree
->lock
);
2381 mirror
= (int)(unsigned long)bio
->bi_bdev
;
2382 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
2383 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
2388 clean_io_failure(start
, page
);
2391 if (!uptodate
&& tree
->ops
&& tree
->ops
->readpage_io_failed_hook
) {
2392 ret
= tree
->ops
->readpage_io_failed_hook(page
, mirror
);
2394 test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
2396 } else if (!uptodate
) {
2398 * The generic bio_readpage_error handles errors the
2399 * following way: If possible, new read requests are
2400 * created and submitted and will end up in
2401 * end_bio_extent_readpage as well (if we're lucky, not
2402 * in the !uptodate case). In that case it returns 0 and
2403 * we just go on with the next page in our bio. If it
2404 * can't handle the error it will return -EIO and we
2405 * remain responsible for that page.
2407 ret
= bio_readpage_error(bio
, page
, start
, end
, mirror
, NULL
);
2410 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2413 uncache_state(&cached
);
2418 if (uptodate
&& tree
->track_uptodate
) {
2419 set_extent_uptodate(tree
, start
, end
, &cached
,
2422 unlock_extent_cached(tree
, start
, end
, &cached
, GFP_ATOMIC
);
2426 SetPageUptodate(page
);
2428 ClearPageUptodate(page
);
2434 check_page_uptodate(tree
, page
);
2436 ClearPageUptodate(page
);
2439 check_page_locked(tree
, page
);
2441 } while (bvec
<= bvec_end
);
2447 btrfs_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
2452 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2454 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
2455 while (!bio
&& (nr_vecs
/= 2))
2456 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2461 bio
->bi_bdev
= bdev
;
2462 bio
->bi_sector
= first_sector
;
2467 static int __must_check
submit_one_bio(int rw
, struct bio
*bio
,
2468 int mirror_num
, unsigned long bio_flags
)
2471 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2472 struct page
*page
= bvec
->bv_page
;
2473 struct extent_io_tree
*tree
= bio
->bi_private
;
2476 start
= page_offset(page
) + bvec
->bv_offset
;
2478 bio
->bi_private
= NULL
;
2482 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
2483 ret
= tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
2484 mirror_num
, bio_flags
, start
);
2486 btrfsic_submit_bio(rw
, bio
);
2488 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
2494 static int merge_bio(int rw
, struct extent_io_tree
*tree
, struct page
*page
,
2495 unsigned long offset
, size_t size
, struct bio
*bio
,
2496 unsigned long bio_flags
)
2499 if (tree
->ops
&& tree
->ops
->merge_bio_hook
)
2500 ret
= tree
->ops
->merge_bio_hook(rw
, page
, offset
, size
, bio
,
2507 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
2508 struct page
*page
, sector_t sector
,
2509 size_t size
, unsigned long offset
,
2510 struct block_device
*bdev
,
2511 struct bio
**bio_ret
,
2512 unsigned long max_pages
,
2513 bio_end_io_t end_io_func
,
2515 unsigned long prev_bio_flags
,
2516 unsigned long bio_flags
)
2522 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
2523 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
2524 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
2526 if (bio_ret
&& *bio_ret
) {
2529 contig
= bio
->bi_sector
== sector
;
2531 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
2534 if (prev_bio_flags
!= bio_flags
|| !contig
||
2535 merge_bio(rw
, tree
, page
, offset
, page_size
, bio
, bio_flags
) ||
2536 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
2537 ret
= submit_one_bio(rw
, bio
, mirror_num
,
2546 if (this_compressed
)
2549 nr
= bio_get_nr_vecs(bdev
);
2551 bio
= btrfs_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
2555 bio_add_page(bio
, page
, page_size
, offset
);
2556 bio
->bi_end_io
= end_io_func
;
2557 bio
->bi_private
= tree
;
2562 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
2567 void attach_extent_buffer_page(struct extent_buffer
*eb
, struct page
*page
)
2569 if (!PagePrivate(page
)) {
2570 SetPagePrivate(page
);
2571 page_cache_get(page
);
2572 set_page_private(page
, (unsigned long)eb
);
2574 WARN_ON(page
->private != (unsigned long)eb
);
2578 void set_page_extent_mapped(struct page
*page
)
2580 if (!PagePrivate(page
)) {
2581 SetPagePrivate(page
);
2582 page_cache_get(page
);
2583 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
2588 * basic readpage implementation. Locked extent state structs are inserted
2589 * into the tree that are removed when the IO is done (by the end_io
2591 * XXX JDM: This needs looking at to ensure proper page locking
2593 static int __extent_read_full_page(struct extent_io_tree
*tree
,
2595 get_extent_t
*get_extent
,
2596 struct bio
**bio
, int mirror_num
,
2597 unsigned long *bio_flags
)
2599 struct inode
*inode
= page
->mapping
->host
;
2600 u64 start
= page_offset(page
);
2601 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2605 u64 last_byte
= i_size_read(inode
);
2609 struct extent_map
*em
;
2610 struct block_device
*bdev
;
2611 struct btrfs_ordered_extent
*ordered
;
2614 size_t pg_offset
= 0;
2616 size_t disk_io_size
;
2617 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2618 unsigned long this_bio_flag
= 0;
2620 set_page_extent_mapped(page
);
2622 if (!PageUptodate(page
)) {
2623 if (cleancache_get_page(page
) == 0) {
2624 BUG_ON(blocksize
!= PAGE_SIZE
);
2631 lock_extent(tree
, start
, end
);
2632 ordered
= btrfs_lookup_ordered_extent(inode
, start
);
2635 unlock_extent(tree
, start
, end
);
2636 btrfs_start_ordered_extent(inode
, ordered
, 1);
2637 btrfs_put_ordered_extent(ordered
);
2640 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
2642 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
2645 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
2646 userpage
= kmap_atomic(page
);
2647 memset(userpage
+ zero_offset
, 0, iosize
);
2648 flush_dcache_page(page
);
2649 kunmap_atomic(userpage
);
2652 while (cur
<= end
) {
2653 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2655 if (cur
>= last_byte
) {
2657 struct extent_state
*cached
= NULL
;
2659 iosize
= PAGE_CACHE_SIZE
- pg_offset
;
2660 userpage
= kmap_atomic(page
);
2661 memset(userpage
+ pg_offset
, 0, iosize
);
2662 flush_dcache_page(page
);
2663 kunmap_atomic(userpage
);
2664 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2666 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2670 em
= get_extent(inode
, page
, pg_offset
, cur
,
2672 if (IS_ERR_OR_NULL(em
)) {
2674 unlock_extent(tree
, cur
, end
);
2677 extent_offset
= cur
- em
->start
;
2678 BUG_ON(extent_map_end(em
) <= cur
);
2681 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2682 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2683 extent_set_compress_type(&this_bio_flag
,
2687 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2688 cur_end
= min(extent_map_end(em
) - 1, end
);
2689 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2690 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2691 disk_io_size
= em
->block_len
;
2692 sector
= em
->block_start
>> 9;
2694 sector
= (em
->block_start
+ extent_offset
) >> 9;
2695 disk_io_size
= iosize
;
2698 block_start
= em
->block_start
;
2699 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2700 block_start
= EXTENT_MAP_HOLE
;
2701 free_extent_map(em
);
2704 /* we've found a hole, just zero and go on */
2705 if (block_start
== EXTENT_MAP_HOLE
) {
2707 struct extent_state
*cached
= NULL
;
2709 userpage
= kmap_atomic(page
);
2710 memset(userpage
+ pg_offset
, 0, iosize
);
2711 flush_dcache_page(page
);
2712 kunmap_atomic(userpage
);
2714 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2716 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2719 pg_offset
+= iosize
;
2722 /* the get_extent function already copied into the page */
2723 if (test_range_bit(tree
, cur
, cur_end
,
2724 EXTENT_UPTODATE
, 1, NULL
)) {
2725 check_page_uptodate(tree
, page
);
2726 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2728 pg_offset
+= iosize
;
2731 /* we have an inline extent but it didn't get marked up
2732 * to date. Error out
2734 if (block_start
== EXTENT_MAP_INLINE
) {
2736 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2738 pg_offset
+= iosize
;
2743 ret
= submit_extent_page(READ
, tree
, page
,
2744 sector
, disk_io_size
, pg_offset
,
2746 end_bio_extent_readpage
, mirror_num
,
2751 *bio_flags
= this_bio_flag
;
2754 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2757 pg_offset
+= iosize
;
2761 if (!PageError(page
))
2762 SetPageUptodate(page
);
2768 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2769 get_extent_t
*get_extent
, int mirror_num
)
2771 struct bio
*bio
= NULL
;
2772 unsigned long bio_flags
= 0;
2775 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, mirror_num
,
2778 ret
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
2782 static noinline
void update_nr_written(struct page
*page
,
2783 struct writeback_control
*wbc
,
2784 unsigned long nr_written
)
2786 wbc
->nr_to_write
-= nr_written
;
2787 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2788 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2789 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2793 * the writepage semantics are similar to regular writepage. extent
2794 * records are inserted to lock ranges in the tree, and as dirty areas
2795 * are found, they are marked writeback. Then the lock bits are removed
2796 * and the end_io handler clears the writeback ranges
2798 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2801 struct inode
*inode
= page
->mapping
->host
;
2802 struct extent_page_data
*epd
= data
;
2803 struct extent_io_tree
*tree
= epd
->tree
;
2804 u64 start
= page_offset(page
);
2806 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2810 u64 last_byte
= i_size_read(inode
);
2814 struct extent_state
*cached_state
= NULL
;
2815 struct extent_map
*em
;
2816 struct block_device
*bdev
;
2819 size_t pg_offset
= 0;
2821 loff_t i_size
= i_size_read(inode
);
2822 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2828 unsigned long nr_written
= 0;
2829 bool fill_delalloc
= true;
2831 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2832 write_flags
= WRITE_SYNC
;
2834 write_flags
= WRITE
;
2836 trace___extent_writepage(page
, inode
, wbc
);
2838 WARN_ON(!PageLocked(page
));
2840 ClearPageError(page
);
2842 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2843 if (page
->index
> end_index
||
2844 (page
->index
== end_index
&& !pg_offset
)) {
2845 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2850 if (page
->index
== end_index
) {
2853 userpage
= kmap_atomic(page
);
2854 memset(userpage
+ pg_offset
, 0,
2855 PAGE_CACHE_SIZE
- pg_offset
);
2856 kunmap_atomic(userpage
);
2857 flush_dcache_page(page
);
2861 set_page_extent_mapped(page
);
2863 if (!tree
->ops
|| !tree
->ops
->fill_delalloc
)
2864 fill_delalloc
= false;
2866 delalloc_start
= start
;
2869 if (!epd
->extent_locked
&& fill_delalloc
) {
2870 u64 delalloc_to_write
= 0;
2872 * make sure the wbc mapping index is at least updated
2875 update_nr_written(page
, wbc
, 0);
2877 while (delalloc_end
< page_end
) {
2878 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2883 if (nr_delalloc
== 0) {
2884 delalloc_start
= delalloc_end
+ 1;
2887 ret
= tree
->ops
->fill_delalloc(inode
, page
,
2892 /* File system has been set read-only */
2898 * delalloc_end is already one less than the total
2899 * length, so we don't subtract one from
2902 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2905 delalloc_start
= delalloc_end
+ 1;
2907 if (wbc
->nr_to_write
< delalloc_to_write
) {
2910 if (delalloc_to_write
< thresh
* 2)
2911 thresh
= delalloc_to_write
;
2912 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2916 /* did the fill delalloc function already unlock and start
2922 * we've unlocked the page, so we can't update
2923 * the mapping's writeback index, just update
2926 wbc
->nr_to_write
-= nr_written
;
2930 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2931 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2934 /* Fixup worker will requeue */
2936 wbc
->pages_skipped
++;
2938 redirty_page_for_writepage(wbc
, page
);
2939 update_nr_written(page
, wbc
, nr_written
);
2947 * we don't want to touch the inode after unlocking the page,
2948 * so we update the mapping writeback index now
2950 update_nr_written(page
, wbc
, nr_written
+ 1);
2953 if (last_byte
<= start
) {
2954 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2955 tree
->ops
->writepage_end_io_hook(page
, start
,
2960 blocksize
= inode
->i_sb
->s_blocksize
;
2962 while (cur
<= end
) {
2963 if (cur
>= last_byte
) {
2964 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2965 tree
->ops
->writepage_end_io_hook(page
, cur
,
2969 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2971 if (IS_ERR_OR_NULL(em
)) {
2976 extent_offset
= cur
- em
->start
;
2977 BUG_ON(extent_map_end(em
) <= cur
);
2979 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2980 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2981 sector
= (em
->block_start
+ extent_offset
) >> 9;
2983 block_start
= em
->block_start
;
2984 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2985 free_extent_map(em
);
2989 * compressed and inline extents are written through other
2992 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2993 block_start
== EXTENT_MAP_INLINE
) {
2995 * end_io notification does not happen here for
2996 * compressed extents
2998 if (!compressed
&& tree
->ops
&&
2999 tree
->ops
->writepage_end_io_hook
)
3000 tree
->ops
->writepage_end_io_hook(page
, cur
,
3003 else if (compressed
) {
3004 /* we don't want to end_page_writeback on
3005 * a compressed extent. this happens
3012 pg_offset
+= iosize
;
3015 /* leave this out until we have a page_mkwrite call */
3016 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
3017 EXTENT_DIRTY
, 0, NULL
)) {
3019 pg_offset
+= iosize
;
3023 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
3024 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
3032 unsigned long max_nr
= end_index
+ 1;
3034 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
3035 if (!PageWriteback(page
)) {
3036 printk(KERN_ERR
"btrfs warning page %lu not "
3037 "writeback, cur %llu end %llu\n",
3038 page
->index
, (unsigned long long)cur
,
3039 (unsigned long long)end
);
3042 ret
= submit_extent_page(write_flags
, tree
, page
,
3043 sector
, iosize
, pg_offset
,
3044 bdev
, &epd
->bio
, max_nr
,
3045 end_bio_extent_writepage
,
3051 pg_offset
+= iosize
;
3056 /* make sure the mapping tag for page dirty gets cleared */
3057 set_page_writeback(page
);
3058 end_page_writeback(page
);
3064 /* drop our reference on any cached states */
3065 free_extent_state(cached_state
);
3069 static int eb_wait(void *word
)
3075 static void wait_on_extent_buffer_writeback(struct extent_buffer
*eb
)
3077 wait_on_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
, eb_wait
,
3078 TASK_UNINTERRUPTIBLE
);
3081 static int lock_extent_buffer_for_io(struct extent_buffer
*eb
,
3082 struct btrfs_fs_info
*fs_info
,
3083 struct extent_page_data
*epd
)
3085 unsigned long i
, num_pages
;
3089 if (!btrfs_try_tree_write_lock(eb
)) {
3091 flush_write_bio(epd
);
3092 btrfs_tree_lock(eb
);
3095 if (test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
)) {
3096 btrfs_tree_unlock(eb
);
3100 flush_write_bio(epd
);
3104 wait_on_extent_buffer_writeback(eb
);
3105 btrfs_tree_lock(eb
);
3106 if (!test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
))
3108 btrfs_tree_unlock(eb
);
3113 * We need to do this to prevent races in people who check if the eb is
3114 * under IO since we can end up having no IO bits set for a short period
3117 spin_lock(&eb
->refs_lock
);
3118 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3119 set_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3120 spin_unlock(&eb
->refs_lock
);
3121 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
3122 __percpu_counter_add(&fs_info
->dirty_metadata_bytes
,
3124 fs_info
->dirty_metadata_batch
);
3127 spin_unlock(&eb
->refs_lock
);
3130 btrfs_tree_unlock(eb
);
3135 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3136 for (i
= 0; i
< num_pages
; i
++) {
3137 struct page
*p
= extent_buffer_page(eb
, i
);
3139 if (!trylock_page(p
)) {
3141 flush_write_bio(epd
);
3151 static void end_extent_buffer_writeback(struct extent_buffer
*eb
)
3153 clear_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3154 smp_mb__after_clear_bit();
3155 wake_up_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
);
3158 static void end_bio_extent_buffer_writepage(struct bio
*bio
, int err
)
3160 int uptodate
= err
== 0;
3161 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
3162 struct extent_buffer
*eb
;
3166 struct page
*page
= bvec
->bv_page
;
3169 eb
= (struct extent_buffer
*)page
->private;
3171 done
= atomic_dec_and_test(&eb
->io_pages
);
3173 if (!uptodate
|| test_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
)) {
3174 set_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3175 ClearPageUptodate(page
);
3179 end_page_writeback(page
);
3184 end_extent_buffer_writeback(eb
);
3185 } while (bvec
>= bio
->bi_io_vec
);
3191 static int write_one_eb(struct extent_buffer
*eb
,
3192 struct btrfs_fs_info
*fs_info
,
3193 struct writeback_control
*wbc
,
3194 struct extent_page_data
*epd
)
3196 struct block_device
*bdev
= fs_info
->fs_devices
->latest_bdev
;
3197 u64 offset
= eb
->start
;
3198 unsigned long i
, num_pages
;
3199 unsigned long bio_flags
= 0;
3200 int rw
= (epd
->sync_io
? WRITE_SYNC
: WRITE
);
3203 clear_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3204 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3205 atomic_set(&eb
->io_pages
, num_pages
);
3206 if (btrfs_header_owner(eb
) == BTRFS_TREE_LOG_OBJECTID
)
3207 bio_flags
= EXTENT_BIO_TREE_LOG
;
3209 for (i
= 0; i
< num_pages
; i
++) {
3210 struct page
*p
= extent_buffer_page(eb
, i
);
3212 clear_page_dirty_for_io(p
);
3213 set_page_writeback(p
);
3214 ret
= submit_extent_page(rw
, eb
->tree
, p
, offset
>> 9,
3215 PAGE_CACHE_SIZE
, 0, bdev
, &epd
->bio
,
3216 -1, end_bio_extent_buffer_writepage
,
3217 0, epd
->bio_flags
, bio_flags
);
3218 epd
->bio_flags
= bio_flags
;
3220 set_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3222 if (atomic_sub_and_test(num_pages
- i
, &eb
->io_pages
))
3223 end_extent_buffer_writeback(eb
);
3227 offset
+= PAGE_CACHE_SIZE
;
3228 update_nr_written(p
, wbc
, 1);
3232 if (unlikely(ret
)) {
3233 for (; i
< num_pages
; i
++) {
3234 struct page
*p
= extent_buffer_page(eb
, i
);
3242 int btree_write_cache_pages(struct address_space
*mapping
,
3243 struct writeback_control
*wbc
)
3245 struct extent_io_tree
*tree
= &BTRFS_I(mapping
->host
)->io_tree
;
3246 struct btrfs_fs_info
*fs_info
= BTRFS_I(mapping
->host
)->root
->fs_info
;
3247 struct extent_buffer
*eb
, *prev_eb
= NULL
;
3248 struct extent_page_data epd
= {
3252 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3257 int nr_to_write_done
= 0;
3258 struct pagevec pvec
;
3261 pgoff_t end
; /* Inclusive */
3265 pagevec_init(&pvec
, 0);
3266 if (wbc
->range_cyclic
) {
3267 index
= mapping
->writeback_index
; /* Start from prev offset */
3270 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3271 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3274 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3275 tag
= PAGECACHE_TAG_TOWRITE
;
3277 tag
= PAGECACHE_TAG_DIRTY
;
3279 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3280 tag_pages_for_writeback(mapping
, index
, end
);
3281 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3282 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3283 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3287 for (i
= 0; i
< nr_pages
; i
++) {
3288 struct page
*page
= pvec
.pages
[i
];
3290 if (!PagePrivate(page
))
3293 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3298 spin_lock(&mapping
->private_lock
);
3299 if (!PagePrivate(page
)) {
3300 spin_unlock(&mapping
->private_lock
);
3304 eb
= (struct extent_buffer
*)page
->private;
3307 * Shouldn't happen and normally this would be a BUG_ON
3308 * but no sense in crashing the users box for something
3309 * we can survive anyway.
3312 spin_unlock(&mapping
->private_lock
);
3317 if (eb
== prev_eb
) {
3318 spin_unlock(&mapping
->private_lock
);
3322 ret
= atomic_inc_not_zero(&eb
->refs
);
3323 spin_unlock(&mapping
->private_lock
);
3328 ret
= lock_extent_buffer_for_io(eb
, fs_info
, &epd
);
3330 free_extent_buffer(eb
);
3334 ret
= write_one_eb(eb
, fs_info
, wbc
, &epd
);
3337 free_extent_buffer(eb
);
3340 free_extent_buffer(eb
);
3343 * the filesystem may choose to bump up nr_to_write.
3344 * We have to make sure to honor the new nr_to_write
3347 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3349 pagevec_release(&pvec
);
3352 if (!scanned
&& !done
) {
3354 * We hit the last page and there is more work to be done: wrap
3355 * back to the start of the file
3361 flush_write_bio(&epd
);
3366 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3367 * @mapping: address space structure to write
3368 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3369 * @writepage: function called for each page
3370 * @data: data passed to writepage function
3372 * If a page is already under I/O, write_cache_pages() skips it, even
3373 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3374 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3375 * and msync() need to guarantee that all the data which was dirty at the time
3376 * the call was made get new I/O started against them. If wbc->sync_mode is
3377 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3378 * existing IO to complete.
3380 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
3381 struct address_space
*mapping
,
3382 struct writeback_control
*wbc
,
3383 writepage_t writepage
, void *data
,
3384 void (*flush_fn
)(void *))
3386 struct inode
*inode
= mapping
->host
;
3389 int nr_to_write_done
= 0;
3390 struct pagevec pvec
;
3393 pgoff_t end
; /* Inclusive */
3398 * We have to hold onto the inode so that ordered extents can do their
3399 * work when the IO finishes. The alternative to this is failing to add
3400 * an ordered extent if the igrab() fails there and that is a huge pain
3401 * to deal with, so instead just hold onto the inode throughout the
3402 * writepages operation. If it fails here we are freeing up the inode
3403 * anyway and we'd rather not waste our time writing out stuff that is
3404 * going to be truncated anyway.
3409 pagevec_init(&pvec
, 0);
3410 if (wbc
->range_cyclic
) {
3411 index
= mapping
->writeback_index
; /* Start from prev offset */
3414 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3415 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3418 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3419 tag
= PAGECACHE_TAG_TOWRITE
;
3421 tag
= PAGECACHE_TAG_DIRTY
;
3423 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3424 tag_pages_for_writeback(mapping
, index
, end
);
3425 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3426 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3427 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3431 for (i
= 0; i
< nr_pages
; i
++) {
3432 struct page
*page
= pvec
.pages
[i
];
3435 * At this point we hold neither mapping->tree_lock nor
3436 * lock on the page itself: the page may be truncated or
3437 * invalidated (changing page->mapping to NULL), or even
3438 * swizzled back from swapper_space to tmpfs file
3441 if (!trylock_page(page
)) {
3446 if (unlikely(page
->mapping
!= mapping
)) {
3451 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3457 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3458 if (PageWriteback(page
))
3460 wait_on_page_writeback(page
);
3463 if (PageWriteback(page
) ||
3464 !clear_page_dirty_for_io(page
)) {
3469 ret
= (*writepage
)(page
, wbc
, data
);
3471 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
3479 * the filesystem may choose to bump up nr_to_write.
3480 * We have to make sure to honor the new nr_to_write
3483 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3485 pagevec_release(&pvec
);
3488 if (!scanned
&& !done
) {
3490 * We hit the last page and there is more work to be done: wrap
3491 * back to the start of the file
3497 btrfs_add_delayed_iput(inode
);
3501 static void flush_epd_write_bio(struct extent_page_data
*epd
)
3510 ret
= submit_one_bio(rw
, epd
->bio
, 0, epd
->bio_flags
);
3511 BUG_ON(ret
< 0); /* -ENOMEM */
3516 static noinline
void flush_write_bio(void *data
)
3518 struct extent_page_data
*epd
= data
;
3519 flush_epd_write_bio(epd
);
3522 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
3523 get_extent_t
*get_extent
,
3524 struct writeback_control
*wbc
)
3527 struct extent_page_data epd
= {
3530 .get_extent
= get_extent
,
3532 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3536 ret
= __extent_writepage(page
, wbc
, &epd
);
3538 flush_epd_write_bio(&epd
);
3542 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
3543 u64 start
, u64 end
, get_extent_t
*get_extent
,
3547 struct address_space
*mapping
= inode
->i_mapping
;
3549 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
3552 struct extent_page_data epd
= {
3555 .get_extent
= get_extent
,
3557 .sync_io
= mode
== WB_SYNC_ALL
,
3560 struct writeback_control wbc_writepages
= {
3562 .nr_to_write
= nr_pages
* 2,
3563 .range_start
= start
,
3564 .range_end
= end
+ 1,
3567 while (start
<= end
) {
3568 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
3569 if (clear_page_dirty_for_io(page
))
3570 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
3572 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
3573 tree
->ops
->writepage_end_io_hook(page
, start
,
3574 start
+ PAGE_CACHE_SIZE
- 1,
3578 page_cache_release(page
);
3579 start
+= PAGE_CACHE_SIZE
;
3582 flush_epd_write_bio(&epd
);
3586 int extent_writepages(struct extent_io_tree
*tree
,
3587 struct address_space
*mapping
,
3588 get_extent_t
*get_extent
,
3589 struct writeback_control
*wbc
)
3592 struct extent_page_data epd
= {
3595 .get_extent
= get_extent
,
3597 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3601 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
3602 __extent_writepage
, &epd
,
3604 flush_epd_write_bio(&epd
);
3608 int extent_readpages(struct extent_io_tree
*tree
,
3609 struct address_space
*mapping
,
3610 struct list_head
*pages
, unsigned nr_pages
,
3611 get_extent_t get_extent
)
3613 struct bio
*bio
= NULL
;
3615 unsigned long bio_flags
= 0;
3616 struct page
*pagepool
[16];
3621 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
3622 page
= list_entry(pages
->prev
, struct page
, lru
);
3624 prefetchw(&page
->flags
);
3625 list_del(&page
->lru
);
3626 if (add_to_page_cache_lru(page
, mapping
,
3627 page
->index
, GFP_NOFS
)) {
3628 page_cache_release(page
);
3632 pagepool
[nr
++] = page
;
3633 if (nr
< ARRAY_SIZE(pagepool
))
3635 for (i
= 0; i
< nr
; i
++) {
3636 __extent_read_full_page(tree
, pagepool
[i
], get_extent
,
3637 &bio
, 0, &bio_flags
);
3638 page_cache_release(pagepool
[i
]);
3642 for (i
= 0; i
< nr
; i
++) {
3643 __extent_read_full_page(tree
, pagepool
[i
], get_extent
,
3644 &bio
, 0, &bio_flags
);
3645 page_cache_release(pagepool
[i
]);
3648 BUG_ON(!list_empty(pages
));
3650 return submit_one_bio(READ
, bio
, 0, bio_flags
);
3655 * basic invalidatepage code, this waits on any locked or writeback
3656 * ranges corresponding to the page, and then deletes any extent state
3657 * records from the tree
3659 int extent_invalidatepage(struct extent_io_tree
*tree
,
3660 struct page
*page
, unsigned long offset
)
3662 struct extent_state
*cached_state
= NULL
;
3663 u64 start
= page_offset(page
);
3664 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3665 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
3667 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
3671 lock_extent_bits(tree
, start
, end
, 0, &cached_state
);
3672 wait_on_page_writeback(page
);
3673 clear_extent_bit(tree
, start
, end
,
3674 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
3675 EXTENT_DO_ACCOUNTING
,
3676 1, 1, &cached_state
, GFP_NOFS
);
3681 * a helper for releasepage, this tests for areas of the page that
3682 * are locked or under IO and drops the related state bits if it is safe
3685 int try_release_extent_state(struct extent_map_tree
*map
,
3686 struct extent_io_tree
*tree
, struct page
*page
,
3689 u64 start
= page_offset(page
);
3690 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3693 if (test_range_bit(tree
, start
, end
,
3694 EXTENT_IOBITS
, 0, NULL
))
3697 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
3700 * at this point we can safely clear everything except the
3701 * locked bit and the nodatasum bit
3703 ret
= clear_extent_bit(tree
, start
, end
,
3704 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
3707 /* if clear_extent_bit failed for enomem reasons,
3708 * we can't allow the release to continue.
3719 * a helper for releasepage. As long as there are no locked extents
3720 * in the range corresponding to the page, both state records and extent
3721 * map records are removed
3723 int try_release_extent_mapping(struct extent_map_tree
*map
,
3724 struct extent_io_tree
*tree
, struct page
*page
,
3727 struct extent_map
*em
;
3728 u64 start
= page_offset(page
);
3729 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3731 if ((mask
& __GFP_WAIT
) &&
3732 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
3734 while (start
<= end
) {
3735 len
= end
- start
+ 1;
3736 write_lock(&map
->lock
);
3737 em
= lookup_extent_mapping(map
, start
, len
);
3739 write_unlock(&map
->lock
);
3742 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
3743 em
->start
!= start
) {
3744 write_unlock(&map
->lock
);
3745 free_extent_map(em
);
3748 if (!test_range_bit(tree
, em
->start
,
3749 extent_map_end(em
) - 1,
3750 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
3752 remove_extent_mapping(map
, em
);
3753 /* once for the rb tree */
3754 free_extent_map(em
);
3756 start
= extent_map_end(em
);
3757 write_unlock(&map
->lock
);
3760 free_extent_map(em
);
3763 return try_release_extent_state(map
, tree
, page
, mask
);
3767 * helper function for fiemap, which doesn't want to see any holes.
3768 * This maps until we find something past 'last'
3770 static struct extent_map
*get_extent_skip_holes(struct inode
*inode
,
3773 get_extent_t
*get_extent
)
3775 u64 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
3776 struct extent_map
*em
;
3783 len
= last
- offset
;
3786 len
= (len
+ sectorsize
- 1) & ~(sectorsize
- 1);
3787 em
= get_extent(inode
, NULL
, 0, offset
, len
, 0);
3788 if (IS_ERR_OR_NULL(em
))
3791 /* if this isn't a hole return it */
3792 if (!test_bit(EXTENT_FLAG_VACANCY
, &em
->flags
) &&
3793 em
->block_start
!= EXTENT_MAP_HOLE
) {
3797 /* this is a hole, advance to the next extent */
3798 offset
= extent_map_end(em
);
3799 free_extent_map(em
);
3806 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3807 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
3811 u64 max
= start
+ len
;
3815 u64 last_for_get_extent
= 0;
3817 u64 isize
= i_size_read(inode
);
3818 struct btrfs_key found_key
;
3819 struct extent_map
*em
= NULL
;
3820 struct extent_state
*cached_state
= NULL
;
3821 struct btrfs_path
*path
;
3822 struct btrfs_file_extent_item
*item
;
3827 unsigned long emflags
;
3832 path
= btrfs_alloc_path();
3835 path
->leave_spinning
= 1;
3837 start
= ALIGN(start
, BTRFS_I(inode
)->root
->sectorsize
);
3838 len
= ALIGN(len
, BTRFS_I(inode
)->root
->sectorsize
);
3841 * lookup the last file extent. We're not using i_size here
3842 * because there might be preallocation past i_size
3844 ret
= btrfs_lookup_file_extent(NULL
, BTRFS_I(inode
)->root
,
3845 path
, btrfs_ino(inode
), -1, 0);
3847 btrfs_free_path(path
);
3852 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3853 struct btrfs_file_extent_item
);
3854 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, path
->slots
[0]);
3855 found_type
= btrfs_key_type(&found_key
);
3857 /* No extents, but there might be delalloc bits */
3858 if (found_key
.objectid
!= btrfs_ino(inode
) ||
3859 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
3860 /* have to trust i_size as the end */
3862 last_for_get_extent
= isize
;
3865 * remember the start of the last extent. There are a
3866 * bunch of different factors that go into the length of the
3867 * extent, so its much less complex to remember where it started
3869 last
= found_key
.offset
;
3870 last_for_get_extent
= last
+ 1;
3872 btrfs_free_path(path
);
3875 * we might have some extents allocated but more delalloc past those
3876 * extents. so, we trust isize unless the start of the last extent is
3881 last_for_get_extent
= isize
;
3884 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
3887 em
= get_extent_skip_holes(inode
, start
, last_for_get_extent
,
3897 u64 offset_in_extent
;
3899 /* break if the extent we found is outside the range */
3900 if (em
->start
>= max
|| extent_map_end(em
) < off
)
3904 * get_extent may return an extent that starts before our
3905 * requested range. We have to make sure the ranges
3906 * we return to fiemap always move forward and don't
3907 * overlap, so adjust the offsets here
3909 em_start
= max(em
->start
, off
);
3912 * record the offset from the start of the extent
3913 * for adjusting the disk offset below
3915 offset_in_extent
= em_start
- em
->start
;
3916 em_end
= extent_map_end(em
);
3917 em_len
= em_end
- em_start
;
3918 emflags
= em
->flags
;
3923 * bump off for our next call to get_extent
3925 off
= extent_map_end(em
);
3929 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
3931 flags
|= FIEMAP_EXTENT_LAST
;
3932 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
3933 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
3934 FIEMAP_EXTENT_NOT_ALIGNED
);
3935 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
3936 flags
|= (FIEMAP_EXTENT_DELALLOC
|
3937 FIEMAP_EXTENT_UNKNOWN
);
3939 disko
= em
->block_start
+ offset_in_extent
;
3941 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
3942 flags
|= FIEMAP_EXTENT_ENCODED
;
3944 free_extent_map(em
);
3946 if ((em_start
>= last
) || em_len
== (u64
)-1 ||
3947 (last
== (u64
)-1 && isize
<= em_end
)) {
3948 flags
|= FIEMAP_EXTENT_LAST
;
3952 /* now scan forward to see if this is really the last extent. */
3953 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
,
3960 flags
|= FIEMAP_EXTENT_LAST
;
3963 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3969 free_extent_map(em
);
3971 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3972 &cached_state
, GFP_NOFS
);
3976 static void __free_extent_buffer(struct extent_buffer
*eb
)
3979 unsigned long flags
;
3980 spin_lock_irqsave(&leak_lock
, flags
);
3981 list_del(&eb
->leak_list
);
3982 spin_unlock_irqrestore(&leak_lock
, flags
);
3984 if (eb
->pages
&& eb
->pages
!= eb
->inline_pages
)
3986 kmem_cache_free(extent_buffer_cache
, eb
);
3989 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3994 struct extent_buffer
*eb
= NULL
;
3996 unsigned long flags
;
3999 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
4006 rwlock_init(&eb
->lock
);
4007 atomic_set(&eb
->write_locks
, 0);
4008 atomic_set(&eb
->read_locks
, 0);
4009 atomic_set(&eb
->blocking_readers
, 0);
4010 atomic_set(&eb
->blocking_writers
, 0);
4011 atomic_set(&eb
->spinning_readers
, 0);
4012 atomic_set(&eb
->spinning_writers
, 0);
4013 eb
->lock_nested
= 0;
4014 init_waitqueue_head(&eb
->write_lock_wq
);
4015 init_waitqueue_head(&eb
->read_lock_wq
);
4018 spin_lock_irqsave(&leak_lock
, flags
);
4019 list_add(&eb
->leak_list
, &buffers
);
4020 spin_unlock_irqrestore(&leak_lock
, flags
);
4022 spin_lock_init(&eb
->refs_lock
);
4023 atomic_set(&eb
->refs
, 1);
4024 atomic_set(&eb
->io_pages
, 0);
4026 if (len
> MAX_INLINE_EXTENT_BUFFER_SIZE
) {
4027 struct page
**pages
;
4028 int num_pages
= (len
+ PAGE_CACHE_SIZE
- 1) >>
4030 pages
= kzalloc(num_pages
, mask
);
4032 __free_extent_buffer(eb
);
4037 eb
->pages
= eb
->inline_pages
;
4043 struct extent_buffer
*btrfs_clone_extent_buffer(struct extent_buffer
*src
)
4047 struct extent_buffer
*new;
4048 unsigned long num_pages
= num_extent_pages(src
->start
, src
->len
);
4050 new = __alloc_extent_buffer(NULL
, src
->start
, src
->len
, GFP_ATOMIC
);
4054 for (i
= 0; i
< num_pages
; i
++) {
4055 p
= alloc_page(GFP_ATOMIC
);
4057 attach_extent_buffer_page(new, p
);
4058 WARN_ON(PageDirty(p
));
4063 copy_extent_buffer(new, src
, 0, 0, src
->len
);
4064 set_bit(EXTENT_BUFFER_UPTODATE
, &new->bflags
);
4065 set_bit(EXTENT_BUFFER_DUMMY
, &new->bflags
);
4070 struct extent_buffer
*alloc_dummy_extent_buffer(u64 start
, unsigned long len
)
4072 struct extent_buffer
*eb
;
4073 unsigned long num_pages
= num_extent_pages(0, len
);
4076 eb
= __alloc_extent_buffer(NULL
, start
, len
, GFP_ATOMIC
);
4080 for (i
= 0; i
< num_pages
; i
++) {
4081 eb
->pages
[i
] = alloc_page(GFP_ATOMIC
);
4085 set_extent_buffer_uptodate(eb
);
4086 btrfs_set_header_nritems(eb
, 0);
4087 set_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
);
4092 __free_page(eb
->pages
[i
- 1]);
4093 __free_extent_buffer(eb
);
4097 static int extent_buffer_under_io(struct extent_buffer
*eb
)
4099 return (atomic_read(&eb
->io_pages
) ||
4100 test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
) ||
4101 test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4105 * Helper for releasing extent buffer page.
4107 static void btrfs_release_extent_buffer_page(struct extent_buffer
*eb
,
4108 unsigned long start_idx
)
4110 unsigned long index
;
4111 unsigned long num_pages
;
4113 int mapped
= !test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
);
4115 BUG_ON(extent_buffer_under_io(eb
));
4117 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4118 index
= start_idx
+ num_pages
;
4119 if (start_idx
>= index
)
4124 page
= extent_buffer_page(eb
, index
);
4125 if (page
&& mapped
) {
4126 spin_lock(&page
->mapping
->private_lock
);
4128 * We do this since we'll remove the pages after we've
4129 * removed the eb from the radix tree, so we could race
4130 * and have this page now attached to the new eb. So
4131 * only clear page_private if it's still connected to
4134 if (PagePrivate(page
) &&
4135 page
->private == (unsigned long)eb
) {
4136 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4137 BUG_ON(PageDirty(page
));
4138 BUG_ON(PageWriteback(page
));
4140 * We need to make sure we haven't be attached
4143 ClearPagePrivate(page
);
4144 set_page_private(page
, 0);
4145 /* One for the page private */
4146 page_cache_release(page
);
4148 spin_unlock(&page
->mapping
->private_lock
);
4152 /* One for when we alloced the page */
4153 page_cache_release(page
);
4155 } while (index
!= start_idx
);
4159 * Helper for releasing the extent buffer.
4161 static inline void btrfs_release_extent_buffer(struct extent_buffer
*eb
)
4163 btrfs_release_extent_buffer_page(eb
, 0);
4164 __free_extent_buffer(eb
);
4167 static void check_buffer_tree_ref(struct extent_buffer
*eb
)
4170 /* the ref bit is tricky. We have to make sure it is set
4171 * if we have the buffer dirty. Otherwise the
4172 * code to free a buffer can end up dropping a dirty
4175 * Once the ref bit is set, it won't go away while the
4176 * buffer is dirty or in writeback, and it also won't
4177 * go away while we have the reference count on the
4180 * We can't just set the ref bit without bumping the
4181 * ref on the eb because free_extent_buffer might
4182 * see the ref bit and try to clear it. If this happens
4183 * free_extent_buffer might end up dropping our original
4184 * ref by mistake and freeing the page before we are able
4185 * to add one more ref.
4187 * So bump the ref count first, then set the bit. If someone
4188 * beat us to it, drop the ref we added.
4190 refs
= atomic_read(&eb
->refs
);
4191 if (refs
>= 2 && test_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4194 spin_lock(&eb
->refs_lock
);
4195 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4196 atomic_inc(&eb
->refs
);
4197 spin_unlock(&eb
->refs_lock
);
4200 static void mark_extent_buffer_accessed(struct extent_buffer
*eb
)
4202 unsigned long num_pages
, i
;
4204 check_buffer_tree_ref(eb
);
4206 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4207 for (i
= 0; i
< num_pages
; i
++) {
4208 struct page
*p
= extent_buffer_page(eb
, i
);
4209 mark_page_accessed(p
);
4213 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
4214 u64 start
, unsigned long len
)
4216 unsigned long num_pages
= num_extent_pages(start
, len
);
4218 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
4219 struct extent_buffer
*eb
;
4220 struct extent_buffer
*exists
= NULL
;
4222 struct address_space
*mapping
= tree
->mapping
;
4227 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4228 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4230 mark_extent_buffer_accessed(eb
);
4235 eb
= __alloc_extent_buffer(tree
, start
, len
, GFP_NOFS
);
4239 for (i
= 0; i
< num_pages
; i
++, index
++) {
4240 p
= find_or_create_page(mapping
, index
, GFP_NOFS
);
4244 spin_lock(&mapping
->private_lock
);
4245 if (PagePrivate(p
)) {
4247 * We could have already allocated an eb for this page
4248 * and attached one so lets see if we can get a ref on
4249 * the existing eb, and if we can we know it's good and
4250 * we can just return that one, else we know we can just
4251 * overwrite page->private.
4253 exists
= (struct extent_buffer
*)p
->private;
4254 if (atomic_inc_not_zero(&exists
->refs
)) {
4255 spin_unlock(&mapping
->private_lock
);
4257 page_cache_release(p
);
4258 mark_extent_buffer_accessed(exists
);
4263 * Do this so attach doesn't complain and we need to
4264 * drop the ref the old guy had.
4266 ClearPagePrivate(p
);
4267 WARN_ON(PageDirty(p
));
4268 page_cache_release(p
);
4270 attach_extent_buffer_page(eb
, p
);
4271 spin_unlock(&mapping
->private_lock
);
4272 WARN_ON(PageDirty(p
));
4273 mark_page_accessed(p
);
4275 if (!PageUptodate(p
))
4279 * see below about how we avoid a nasty race with release page
4280 * and why we unlock later
4284 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4286 ret
= radix_tree_preload(GFP_NOFS
& ~__GFP_HIGHMEM
);
4290 spin_lock(&tree
->buffer_lock
);
4291 ret
= radix_tree_insert(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
, eb
);
4292 if (ret
== -EEXIST
) {
4293 exists
= radix_tree_lookup(&tree
->buffer
,
4294 start
>> PAGE_CACHE_SHIFT
);
4295 if (!atomic_inc_not_zero(&exists
->refs
)) {
4296 spin_unlock(&tree
->buffer_lock
);
4297 radix_tree_preload_end();
4301 spin_unlock(&tree
->buffer_lock
);
4302 radix_tree_preload_end();
4303 mark_extent_buffer_accessed(exists
);
4306 /* add one reference for the tree */
4307 check_buffer_tree_ref(eb
);
4308 spin_unlock(&tree
->buffer_lock
);
4309 radix_tree_preload_end();
4312 * there is a race where release page may have
4313 * tried to find this extent buffer in the radix
4314 * but failed. It will tell the VM it is safe to
4315 * reclaim the, and it will clear the page private bit.
4316 * We must make sure to set the page private bit properly
4317 * after the extent buffer is in the radix tree so
4318 * it doesn't get lost
4320 SetPageChecked(eb
->pages
[0]);
4321 for (i
= 1; i
< num_pages
; i
++) {
4322 p
= extent_buffer_page(eb
, i
);
4323 ClearPageChecked(p
);
4326 unlock_page(eb
->pages
[0]);
4330 for (i
= 0; i
< num_pages
; i
++) {
4332 unlock_page(eb
->pages
[i
]);
4335 WARN_ON(!atomic_dec_and_test(&eb
->refs
));
4336 btrfs_release_extent_buffer(eb
);
4340 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
4341 u64 start
, unsigned long len
)
4343 struct extent_buffer
*eb
;
4346 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4347 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4349 mark_extent_buffer_accessed(eb
);
4357 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head
*head
)
4359 struct extent_buffer
*eb
=
4360 container_of(head
, struct extent_buffer
, rcu_head
);
4362 __free_extent_buffer(eb
);
4365 /* Expects to have eb->eb_lock already held */
4366 static int release_extent_buffer(struct extent_buffer
*eb
, gfp_t mask
)
4368 WARN_ON(atomic_read(&eb
->refs
) == 0);
4369 if (atomic_dec_and_test(&eb
->refs
)) {
4370 if (test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
)) {
4371 spin_unlock(&eb
->refs_lock
);
4373 struct extent_io_tree
*tree
= eb
->tree
;
4375 spin_unlock(&eb
->refs_lock
);
4377 spin_lock(&tree
->buffer_lock
);
4378 radix_tree_delete(&tree
->buffer
,
4379 eb
->start
>> PAGE_CACHE_SHIFT
);
4380 spin_unlock(&tree
->buffer_lock
);
4383 /* Should be safe to release our pages at this point */
4384 btrfs_release_extent_buffer_page(eb
, 0);
4385 call_rcu(&eb
->rcu_head
, btrfs_release_extent_buffer_rcu
);
4388 spin_unlock(&eb
->refs_lock
);
4393 void free_extent_buffer(struct extent_buffer
*eb
)
4401 refs
= atomic_read(&eb
->refs
);
4404 old
= atomic_cmpxchg(&eb
->refs
, refs
, refs
- 1);
4409 spin_lock(&eb
->refs_lock
);
4410 if (atomic_read(&eb
->refs
) == 2 &&
4411 test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
))
4412 atomic_dec(&eb
->refs
);
4414 if (atomic_read(&eb
->refs
) == 2 &&
4415 test_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
) &&
4416 !extent_buffer_under_io(eb
) &&
4417 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4418 atomic_dec(&eb
->refs
);
4421 * I know this is terrible, but it's temporary until we stop tracking
4422 * the uptodate bits and such for the extent buffers.
4424 release_extent_buffer(eb
, GFP_ATOMIC
);
4427 void free_extent_buffer_stale(struct extent_buffer
*eb
)
4432 spin_lock(&eb
->refs_lock
);
4433 set_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
);
4435 if (atomic_read(&eb
->refs
) == 2 && !extent_buffer_under_io(eb
) &&
4436 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4437 atomic_dec(&eb
->refs
);
4438 release_extent_buffer(eb
, GFP_NOFS
);
4441 void clear_extent_buffer_dirty(struct extent_buffer
*eb
)
4444 unsigned long num_pages
;
4447 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4449 for (i
= 0; i
< num_pages
; i
++) {
4450 page
= extent_buffer_page(eb
, i
);
4451 if (!PageDirty(page
))
4455 WARN_ON(!PagePrivate(page
));
4457 clear_page_dirty_for_io(page
);
4458 spin_lock_irq(&page
->mapping
->tree_lock
);
4459 if (!PageDirty(page
)) {
4460 radix_tree_tag_clear(&page
->mapping
->page_tree
,
4462 PAGECACHE_TAG_DIRTY
);
4464 spin_unlock_irq(&page
->mapping
->tree_lock
);
4465 ClearPageError(page
);
4468 WARN_ON(atomic_read(&eb
->refs
) == 0);
4471 int set_extent_buffer_dirty(struct extent_buffer
*eb
)
4474 unsigned long num_pages
;
4477 check_buffer_tree_ref(eb
);
4479 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
4481 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4482 WARN_ON(atomic_read(&eb
->refs
) == 0);
4483 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
));
4485 for (i
= 0; i
< num_pages
; i
++)
4486 set_page_dirty(extent_buffer_page(eb
, i
));
4490 static int range_straddles_pages(u64 start
, u64 len
)
4492 if (len
< PAGE_CACHE_SIZE
)
4494 if (start
& (PAGE_CACHE_SIZE
- 1))
4496 if ((start
+ len
) & (PAGE_CACHE_SIZE
- 1))
4501 int clear_extent_buffer_uptodate(struct extent_buffer
*eb
)
4505 unsigned long num_pages
;
4507 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4508 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4509 for (i
= 0; i
< num_pages
; i
++) {
4510 page
= extent_buffer_page(eb
, i
);
4512 ClearPageUptodate(page
);
4517 int set_extent_buffer_uptodate(struct extent_buffer
*eb
)
4521 unsigned long num_pages
;
4523 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4524 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4525 for (i
= 0; i
< num_pages
; i
++) {
4526 page
= extent_buffer_page(eb
, i
);
4527 SetPageUptodate(page
);
4532 int extent_range_uptodate(struct extent_io_tree
*tree
,
4537 int pg_uptodate
= 1;
4539 unsigned long index
;
4541 if (range_straddles_pages(start
, end
- start
+ 1)) {
4542 ret
= test_range_bit(tree
, start
, end
,
4543 EXTENT_UPTODATE
, 1, NULL
);
4547 while (start
<= end
) {
4548 index
= start
>> PAGE_CACHE_SHIFT
;
4549 page
= find_get_page(tree
->mapping
, index
);
4552 uptodate
= PageUptodate(page
);
4553 page_cache_release(page
);
4558 start
+= PAGE_CACHE_SIZE
;
4563 int extent_buffer_uptodate(struct extent_buffer
*eb
)
4565 return test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4568 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
4569 struct extent_buffer
*eb
, u64 start
, int wait
,
4570 get_extent_t
*get_extent
, int mirror_num
)
4573 unsigned long start_i
;
4577 int locked_pages
= 0;
4578 int all_uptodate
= 1;
4579 unsigned long num_pages
;
4580 unsigned long num_reads
= 0;
4581 struct bio
*bio
= NULL
;
4582 unsigned long bio_flags
= 0;
4584 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
4588 WARN_ON(start
< eb
->start
);
4589 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
4590 (eb
->start
>> PAGE_CACHE_SHIFT
);
4595 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4596 for (i
= start_i
; i
< num_pages
; i
++) {
4597 page
= extent_buffer_page(eb
, i
);
4598 if (wait
== WAIT_NONE
) {
4599 if (!trylock_page(page
))
4605 if (!PageUptodate(page
)) {
4612 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4616 clear_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
4617 eb
->read_mirror
= 0;
4618 atomic_set(&eb
->io_pages
, num_reads
);
4619 for (i
= start_i
; i
< num_pages
; i
++) {
4620 page
= extent_buffer_page(eb
, i
);
4621 if (!PageUptodate(page
)) {
4622 ClearPageError(page
);
4623 err
= __extent_read_full_page(tree
, page
,
4625 mirror_num
, &bio_flags
);
4634 err
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
4639 if (ret
|| wait
!= WAIT_COMPLETE
)
4642 for (i
= start_i
; i
< num_pages
; i
++) {
4643 page
= extent_buffer_page(eb
, i
);
4644 wait_on_page_locked(page
);
4645 if (!PageUptodate(page
))
4653 while (locked_pages
> 0) {
4654 page
= extent_buffer_page(eb
, i
);
4662 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
4663 unsigned long start
,
4670 char *dst
= (char *)dstv
;
4671 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4672 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4674 WARN_ON(start
> eb
->len
);
4675 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4677 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4680 page
= extent_buffer_page(eb
, i
);
4682 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4683 kaddr
= page_address(page
);
4684 memcpy(dst
, kaddr
+ offset
, cur
);
4693 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
4694 unsigned long min_len
, char **map
,
4695 unsigned long *map_start
,
4696 unsigned long *map_len
)
4698 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
4701 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4702 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4703 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
4710 offset
= start_offset
;
4714 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
4717 if (start
+ min_len
> eb
->len
) {
4718 WARN(1, KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
4719 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
4720 eb
->len
, start
, min_len
);
4724 p
= extent_buffer_page(eb
, i
);
4725 kaddr
= page_address(p
);
4726 *map
= kaddr
+ offset
;
4727 *map_len
= PAGE_CACHE_SIZE
- offset
;
4731 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
4732 unsigned long start
,
4739 char *ptr
= (char *)ptrv
;
4740 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4741 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4744 WARN_ON(start
> eb
->len
);
4745 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4747 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4750 page
= extent_buffer_page(eb
, i
);
4752 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4754 kaddr
= page_address(page
);
4755 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
4767 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
4768 unsigned long start
, unsigned long len
)
4774 char *src
= (char *)srcv
;
4775 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4776 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4778 WARN_ON(start
> eb
->len
);
4779 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4781 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4784 page
= extent_buffer_page(eb
, i
);
4785 WARN_ON(!PageUptodate(page
));
4787 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4788 kaddr
= page_address(page
);
4789 memcpy(kaddr
+ offset
, src
, cur
);
4798 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
4799 unsigned long start
, unsigned long len
)
4805 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4806 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4808 WARN_ON(start
> eb
->len
);
4809 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4811 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4814 page
= extent_buffer_page(eb
, i
);
4815 WARN_ON(!PageUptodate(page
));
4817 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4818 kaddr
= page_address(page
);
4819 memset(kaddr
+ offset
, c
, cur
);
4827 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
4828 unsigned long dst_offset
, unsigned long src_offset
,
4831 u64 dst_len
= dst
->len
;
4836 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4837 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4839 WARN_ON(src
->len
!= dst_len
);
4841 offset
= (start_offset
+ dst_offset
) &
4842 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4845 page
= extent_buffer_page(dst
, i
);
4846 WARN_ON(!PageUptodate(page
));
4848 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
4850 kaddr
= page_address(page
);
4851 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
4860 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
4861 unsigned long dst_off
, unsigned long src_off
,
4864 char *dst_kaddr
= page_address(dst_page
);
4865 if (dst_page
== src_page
) {
4866 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
4868 char *src_kaddr
= page_address(src_page
);
4869 char *p
= dst_kaddr
+ dst_off
+ len
;
4870 char *s
= src_kaddr
+ src_off
+ len
;
4877 static inline bool areas_overlap(unsigned long src
, unsigned long dst
, unsigned long len
)
4879 unsigned long distance
= (src
> dst
) ? src
- dst
: dst
- src
;
4880 return distance
< len
;
4883 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
4884 unsigned long dst_off
, unsigned long src_off
,
4887 char *dst_kaddr
= page_address(dst_page
);
4889 int must_memmove
= 0;
4891 if (dst_page
!= src_page
) {
4892 src_kaddr
= page_address(src_page
);
4894 src_kaddr
= dst_kaddr
;
4895 if (areas_overlap(src_off
, dst_off
, len
))
4900 memmove(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4902 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4905 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4906 unsigned long src_offset
, unsigned long len
)
4909 size_t dst_off_in_page
;
4910 size_t src_off_in_page
;
4911 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4912 unsigned long dst_i
;
4913 unsigned long src_i
;
4915 if (src_offset
+ len
> dst
->len
) {
4916 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4917 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
4920 if (dst_offset
+ len
> dst
->len
) {
4921 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4922 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
4927 dst_off_in_page
= (start_offset
+ dst_offset
) &
4928 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4929 src_off_in_page
= (start_offset
+ src_offset
) &
4930 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4932 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4933 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
4935 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
4937 cur
= min_t(unsigned long, cur
,
4938 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
4940 copy_pages(extent_buffer_page(dst
, dst_i
),
4941 extent_buffer_page(dst
, src_i
),
4942 dst_off_in_page
, src_off_in_page
, cur
);
4950 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4951 unsigned long src_offset
, unsigned long len
)
4954 size_t dst_off_in_page
;
4955 size_t src_off_in_page
;
4956 unsigned long dst_end
= dst_offset
+ len
- 1;
4957 unsigned long src_end
= src_offset
+ len
- 1;
4958 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4959 unsigned long dst_i
;
4960 unsigned long src_i
;
4962 if (src_offset
+ len
> dst
->len
) {
4963 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4964 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
4967 if (dst_offset
+ len
> dst
->len
) {
4968 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4969 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
4972 if (dst_offset
< src_offset
) {
4973 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
4977 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
4978 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
4980 dst_off_in_page
= (start_offset
+ dst_end
) &
4981 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4982 src_off_in_page
= (start_offset
+ src_end
) &
4983 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4985 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
4986 cur
= min(cur
, dst_off_in_page
+ 1);
4987 move_pages(extent_buffer_page(dst
, dst_i
),
4988 extent_buffer_page(dst
, src_i
),
4989 dst_off_in_page
- cur
+ 1,
4990 src_off_in_page
- cur
+ 1, cur
);
4998 int try_release_extent_buffer(struct page
*page
, gfp_t mask
)
5000 struct extent_buffer
*eb
;
5003 * We need to make sure noboody is attaching this page to an eb right
5006 spin_lock(&page
->mapping
->private_lock
);
5007 if (!PagePrivate(page
)) {
5008 spin_unlock(&page
->mapping
->private_lock
);
5012 eb
= (struct extent_buffer
*)page
->private;
5016 * This is a little awful but should be ok, we need to make sure that
5017 * the eb doesn't disappear out from under us while we're looking at
5020 spin_lock(&eb
->refs_lock
);
5021 if (atomic_read(&eb
->refs
) != 1 || extent_buffer_under_io(eb
)) {
5022 spin_unlock(&eb
->refs_lock
);
5023 spin_unlock(&page
->mapping
->private_lock
);
5026 spin_unlock(&page
->mapping
->private_lock
);
5028 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
5032 * If tree ref isn't set then we know the ref on this eb is a real ref,
5033 * so just return, this page will likely be freed soon anyway.
5035 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
)) {
5036 spin_unlock(&eb
->refs_lock
);
5040 return release_extent_buffer(eb
, mask
);