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"
24 static struct kmem_cache
*extent_state_cache
;
25 static struct kmem_cache
*extent_buffer_cache
;
27 static LIST_HEAD(buffers
);
28 static LIST_HEAD(states
);
32 static DEFINE_SPINLOCK(leak_lock
);
35 #define BUFFER_LRU_MAX 64
40 struct rb_node rb_node
;
43 struct extent_page_data
{
45 struct extent_io_tree
*tree
;
46 get_extent_t
*get_extent
;
48 /* tells writepage not to lock the state bits for this range
49 * it still does the unlocking
51 unsigned int extent_locked
:1;
53 /* tells the submit_bio code to use a WRITE_SYNC */
54 unsigned int sync_io
:1;
57 static noinline
void flush_write_bio(void *data
);
58 static inline struct btrfs_fs_info
*
59 tree_fs_info(struct extent_io_tree
*tree
)
61 return btrfs_sb(tree
->mapping
->host
->i_sb
);
64 int __init
extent_io_init(void)
66 extent_state_cache
= kmem_cache_create("extent_state",
67 sizeof(struct extent_state
), 0,
68 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
69 if (!extent_state_cache
)
72 extent_buffer_cache
= kmem_cache_create("extent_buffers",
73 sizeof(struct extent_buffer
), 0,
74 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
75 if (!extent_buffer_cache
)
76 goto free_state_cache
;
80 kmem_cache_destroy(extent_state_cache
);
84 void extent_io_exit(void)
86 struct extent_state
*state
;
87 struct extent_buffer
*eb
;
89 while (!list_empty(&states
)) {
90 state
= list_entry(states
.next
, struct extent_state
, leak_list
);
91 printk(KERN_ERR
"btrfs state leak: start %llu end %llu "
92 "state %lu in tree %p refs %d\n",
93 (unsigned long long)state
->start
,
94 (unsigned long long)state
->end
,
95 state
->state
, state
->tree
, atomic_read(&state
->refs
));
96 list_del(&state
->leak_list
);
97 kmem_cache_free(extent_state_cache
, state
);
101 while (!list_empty(&buffers
)) {
102 eb
= list_entry(buffers
.next
, struct extent_buffer
, leak_list
);
103 printk(KERN_ERR
"btrfs buffer leak start %llu len %lu "
104 "refs %d\n", (unsigned long long)eb
->start
,
105 eb
->len
, atomic_read(&eb
->refs
));
106 list_del(&eb
->leak_list
);
107 kmem_cache_free(extent_buffer_cache
, eb
);
109 if (extent_state_cache
)
110 kmem_cache_destroy(extent_state_cache
);
111 if (extent_buffer_cache
)
112 kmem_cache_destroy(extent_buffer_cache
);
115 void extent_io_tree_init(struct extent_io_tree
*tree
,
116 struct address_space
*mapping
)
118 tree
->state
= RB_ROOT
;
119 INIT_RADIX_TREE(&tree
->buffer
, GFP_ATOMIC
);
121 tree
->dirty_bytes
= 0;
122 spin_lock_init(&tree
->lock
);
123 spin_lock_init(&tree
->buffer_lock
);
124 tree
->mapping
= mapping
;
127 static struct extent_state
*alloc_extent_state(gfp_t mask
)
129 struct extent_state
*state
;
134 state
= kmem_cache_alloc(extent_state_cache
, mask
);
141 spin_lock_irqsave(&leak_lock
, flags
);
142 list_add(&state
->leak_list
, &states
);
143 spin_unlock_irqrestore(&leak_lock
, flags
);
145 atomic_set(&state
->refs
, 1);
146 init_waitqueue_head(&state
->wq
);
147 trace_alloc_extent_state(state
, mask
, _RET_IP_
);
151 void free_extent_state(struct extent_state
*state
)
155 if (atomic_dec_and_test(&state
->refs
)) {
159 WARN_ON(state
->tree
);
161 spin_lock_irqsave(&leak_lock
, flags
);
162 list_del(&state
->leak_list
);
163 spin_unlock_irqrestore(&leak_lock
, flags
);
165 trace_free_extent_state(state
, _RET_IP_
);
166 kmem_cache_free(extent_state_cache
, state
);
170 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
171 struct rb_node
*node
)
173 struct rb_node
**p
= &root
->rb_node
;
174 struct rb_node
*parent
= NULL
;
175 struct tree_entry
*entry
;
179 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
181 if (offset
< entry
->start
)
183 else if (offset
> entry
->end
)
189 entry
= rb_entry(node
, struct tree_entry
, rb_node
);
190 rb_link_node(node
, parent
, p
);
191 rb_insert_color(node
, root
);
195 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
196 struct rb_node
**prev_ret
,
197 struct rb_node
**next_ret
)
199 struct rb_root
*root
= &tree
->state
;
200 struct rb_node
*n
= root
->rb_node
;
201 struct rb_node
*prev
= NULL
;
202 struct rb_node
*orig_prev
= NULL
;
203 struct tree_entry
*entry
;
204 struct tree_entry
*prev_entry
= NULL
;
207 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
211 if (offset
< entry
->start
)
213 else if (offset
> entry
->end
)
221 while (prev
&& offset
> prev_entry
->end
) {
222 prev
= rb_next(prev
);
223 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
230 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
231 while (prev
&& offset
< prev_entry
->start
) {
232 prev
= rb_prev(prev
);
233 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
240 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
243 struct rb_node
*prev
= NULL
;
246 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
252 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
253 struct extent_state
*other
)
255 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
256 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
261 * utility function to look for merge candidates inside a given range.
262 * Any extents with matching state are merged together into a single
263 * extent in the tree. Extents with EXTENT_IO in their state field
264 * are not merged because the end_io handlers need to be able to do
265 * operations on them without sleeping (or doing allocations/splits).
267 * This should be called with the tree lock held.
269 static void merge_state(struct extent_io_tree
*tree
,
270 struct extent_state
*state
)
272 struct extent_state
*other
;
273 struct rb_node
*other_node
;
275 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
278 other_node
= rb_prev(&state
->rb_node
);
280 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
281 if (other
->end
== state
->start
- 1 &&
282 other
->state
== state
->state
) {
283 merge_cb(tree
, state
, other
);
284 state
->start
= other
->start
;
286 rb_erase(&other
->rb_node
, &tree
->state
);
287 free_extent_state(other
);
290 other_node
= rb_next(&state
->rb_node
);
292 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
293 if (other
->start
== state
->end
+ 1 &&
294 other
->state
== state
->state
) {
295 merge_cb(tree
, state
, other
);
296 state
->end
= other
->end
;
298 rb_erase(&other
->rb_node
, &tree
->state
);
299 free_extent_state(other
);
304 static void set_state_cb(struct extent_io_tree
*tree
,
305 struct extent_state
*state
, int *bits
)
307 if (tree
->ops
&& tree
->ops
->set_bit_hook
)
308 tree
->ops
->set_bit_hook(tree
->mapping
->host
, state
, bits
);
311 static void clear_state_cb(struct extent_io_tree
*tree
,
312 struct extent_state
*state
, int *bits
)
314 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
315 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
, bits
);
318 static void set_state_bits(struct extent_io_tree
*tree
,
319 struct extent_state
*state
, int *bits
);
322 * insert an extent_state struct into the tree. 'bits' are set on the
323 * struct before it is inserted.
325 * This may return -EEXIST if the extent is already there, in which case the
326 * state struct is freed.
328 * The tree lock is not taken internally. This is a utility function and
329 * probably isn't what you want to call (see set/clear_extent_bit).
331 static int insert_state(struct extent_io_tree
*tree
,
332 struct extent_state
*state
, u64 start
, u64 end
,
335 struct rb_node
*node
;
338 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
339 (unsigned long long)end
,
340 (unsigned long long)start
);
343 state
->start
= start
;
346 set_state_bits(tree
, state
, bits
);
348 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
350 struct extent_state
*found
;
351 found
= rb_entry(node
, struct extent_state
, rb_node
);
352 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
353 "%llu %llu\n", (unsigned long long)found
->start
,
354 (unsigned long long)found
->end
,
355 (unsigned long long)start
, (unsigned long long)end
);
359 merge_state(tree
, state
);
363 static void split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
366 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
367 tree
->ops
->split_extent_hook(tree
->mapping
->host
, orig
, split
);
371 * split a given extent state struct in two, inserting the preallocated
372 * struct 'prealloc' as the newly created second half. 'split' indicates an
373 * offset inside 'orig' where it should be split.
376 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
377 * are two extent state structs in the tree:
378 * prealloc: [orig->start, split - 1]
379 * orig: [ split, orig->end ]
381 * The tree locks are not taken by this function. They need to be held
384 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
385 struct extent_state
*prealloc
, u64 split
)
387 struct rb_node
*node
;
389 split_cb(tree
, orig
, split
);
391 prealloc
->start
= orig
->start
;
392 prealloc
->end
= split
- 1;
393 prealloc
->state
= orig
->state
;
396 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
398 free_extent_state(prealloc
);
401 prealloc
->tree
= tree
;
405 static struct extent_state
*next_state(struct extent_state
*state
)
407 struct rb_node
*next
= rb_next(&state
->rb_node
);
409 return rb_entry(next
, struct extent_state
, rb_node
);
415 * utility function to clear some bits in an extent state struct.
416 * it will optionally wake up any one waiting on this state (wake == 1)
418 * If no bits are set on the state struct after clearing things, the
419 * struct is freed and removed from the tree
421 static struct extent_state
*clear_state_bit(struct extent_io_tree
*tree
,
422 struct extent_state
*state
,
425 struct extent_state
*next
;
426 int bits_to_clear
= *bits
& ~EXTENT_CTLBITS
;
428 if ((bits_to_clear
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
429 u64 range
= state
->end
- state
->start
+ 1;
430 WARN_ON(range
> tree
->dirty_bytes
);
431 tree
->dirty_bytes
-= range
;
433 clear_state_cb(tree
, state
, bits
);
434 state
->state
&= ~bits_to_clear
;
437 if (state
->state
== 0) {
438 next
= next_state(state
);
440 rb_erase(&state
->rb_node
, &tree
->state
);
442 free_extent_state(state
);
447 merge_state(tree
, state
);
448 next
= next_state(state
);
453 static struct extent_state
*
454 alloc_extent_state_atomic(struct extent_state
*prealloc
)
457 prealloc
= alloc_extent_state(GFP_ATOMIC
);
462 void extent_io_tree_panic(struct extent_io_tree
*tree
, int err
)
464 btrfs_panic(tree_fs_info(tree
), err
, "Locking error: "
465 "Extent tree was modified by another "
466 "thread while locked.");
470 * clear some bits on a range in the tree. This may require splitting
471 * or inserting elements in the tree, so the gfp mask is used to
472 * indicate which allocations or sleeping are allowed.
474 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
475 * the given range from the tree regardless of state (ie for truncate).
477 * the range [start, end] is inclusive.
479 * This takes the tree lock, and returns 0 on success and < 0 on error.
481 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
482 int bits
, int wake
, int delete,
483 struct extent_state
**cached_state
,
486 struct extent_state
*state
;
487 struct extent_state
*cached
;
488 struct extent_state
*prealloc
= NULL
;
489 struct rb_node
*node
;
495 bits
|= ~EXTENT_CTLBITS
;
496 bits
|= EXTENT_FIRST_DELALLOC
;
498 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
501 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
502 prealloc
= alloc_extent_state(mask
);
507 spin_lock(&tree
->lock
);
509 cached
= *cached_state
;
512 *cached_state
= NULL
;
516 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
517 cached
->end
> start
) {
519 atomic_dec(&cached
->refs
);
524 free_extent_state(cached
);
527 * this search will find the extents that end after
530 node
= tree_search(tree
, start
);
533 state
= rb_entry(node
, struct extent_state
, rb_node
);
535 if (state
->start
> end
)
537 WARN_ON(state
->end
< start
);
538 last_end
= state
->end
;
540 /* the state doesn't have the wanted bits, go ahead */
541 if (!(state
->state
& bits
)) {
542 state
= next_state(state
);
547 * | ---- desired range ---- |
549 * | ------------- state -------------- |
551 * We need to split the extent we found, and may flip
552 * bits on second half.
554 * If the extent we found extends past our range, we
555 * just split and search again. It'll get split again
556 * the next time though.
558 * If the extent we found is inside our range, we clear
559 * the desired bit on it.
562 if (state
->start
< start
) {
563 prealloc
= alloc_extent_state_atomic(prealloc
);
565 err
= split_state(tree
, state
, prealloc
, start
);
567 extent_io_tree_panic(tree
, err
);
572 if (state
->end
<= end
) {
573 clear_state_bit(tree
, state
, &bits
, wake
);
574 if (last_end
== (u64
)-1)
576 start
= last_end
+ 1;
581 * | ---- desired range ---- |
583 * We need to split the extent, and clear the bit
586 if (state
->start
<= end
&& state
->end
> end
) {
587 prealloc
= alloc_extent_state_atomic(prealloc
);
589 err
= split_state(tree
, state
, prealloc
, end
+ 1);
591 extent_io_tree_panic(tree
, err
);
596 clear_state_bit(tree
, prealloc
, &bits
, wake
);
602 state
= clear_state_bit(tree
, state
, &bits
, wake
);
604 if (last_end
== (u64
)-1)
606 start
= last_end
+ 1;
607 if (start
<= end
&& state
&& !need_resched())
612 spin_unlock(&tree
->lock
);
614 free_extent_state(prealloc
);
621 spin_unlock(&tree
->lock
);
622 if (mask
& __GFP_WAIT
)
627 static void wait_on_state(struct extent_io_tree
*tree
,
628 struct extent_state
*state
)
629 __releases(tree
->lock
)
630 __acquires(tree
->lock
)
633 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
634 spin_unlock(&tree
->lock
);
636 spin_lock(&tree
->lock
);
637 finish_wait(&state
->wq
, &wait
);
641 * waits for one or more bits to clear on a range in the state tree.
642 * The range [start, end] is inclusive.
643 * The tree lock is taken by this function
645 void wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
647 struct extent_state
*state
;
648 struct rb_node
*node
;
650 spin_lock(&tree
->lock
);
654 * this search will find all the extents that end after
657 node
= tree_search(tree
, start
);
661 state
= rb_entry(node
, struct extent_state
, rb_node
);
663 if (state
->start
> end
)
666 if (state
->state
& bits
) {
667 start
= state
->start
;
668 atomic_inc(&state
->refs
);
669 wait_on_state(tree
, state
);
670 free_extent_state(state
);
673 start
= state
->end
+ 1;
678 cond_resched_lock(&tree
->lock
);
681 spin_unlock(&tree
->lock
);
684 static void set_state_bits(struct extent_io_tree
*tree
,
685 struct extent_state
*state
,
688 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
690 set_state_cb(tree
, state
, bits
);
691 if ((bits_to_set
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
692 u64 range
= state
->end
- state
->start
+ 1;
693 tree
->dirty_bytes
+= range
;
695 state
->state
|= bits_to_set
;
698 static void cache_state(struct extent_state
*state
,
699 struct extent_state
**cached_ptr
)
701 if (cached_ptr
&& !(*cached_ptr
)) {
702 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
704 atomic_inc(&state
->refs
);
709 static void uncache_state(struct extent_state
**cached_ptr
)
711 if (cached_ptr
&& (*cached_ptr
)) {
712 struct extent_state
*state
= *cached_ptr
;
714 free_extent_state(state
);
719 * set some bits on a range in the tree. This may require allocations or
720 * sleeping, so the gfp mask is used to indicate what is allowed.
722 * If any of the exclusive bits are set, this will fail with -EEXIST if some
723 * part of the range already has the desired bits set. The start of the
724 * existing range is returned in failed_start in this case.
726 * [start, end] is inclusive This takes the tree lock.
729 static int __must_check
730 __set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
731 int bits
, int exclusive_bits
, u64
*failed_start
,
732 struct extent_state
**cached_state
, gfp_t mask
)
734 struct extent_state
*state
;
735 struct extent_state
*prealloc
= NULL
;
736 struct rb_node
*node
;
741 bits
|= EXTENT_FIRST_DELALLOC
;
743 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
744 prealloc
= alloc_extent_state(mask
);
748 spin_lock(&tree
->lock
);
749 if (cached_state
&& *cached_state
) {
750 state
= *cached_state
;
751 if (state
->start
<= start
&& state
->end
> start
&&
753 node
= &state
->rb_node
;
758 * this search will find all the extents that end after
761 node
= tree_search(tree
, start
);
763 prealloc
= alloc_extent_state_atomic(prealloc
);
765 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
767 extent_io_tree_panic(tree
, err
);
772 state
= rb_entry(node
, struct extent_state
, rb_node
);
774 last_start
= state
->start
;
775 last_end
= state
->end
;
778 * | ---- desired range ---- |
781 * Just lock what we found and keep going
783 if (state
->start
== start
&& state
->end
<= end
) {
784 struct rb_node
*next_node
;
785 if (state
->state
& exclusive_bits
) {
786 *failed_start
= state
->start
;
791 set_state_bits(tree
, state
, &bits
);
793 cache_state(state
, cached_state
);
794 merge_state(tree
, state
);
795 if (last_end
== (u64
)-1)
798 start
= last_end
+ 1;
799 next_node
= rb_next(&state
->rb_node
);
800 if (next_node
&& start
< end
&& prealloc
&& !need_resched()) {
801 state
= rb_entry(next_node
, struct extent_state
,
803 if (state
->start
== start
)
810 * | ---- desired range ---- |
813 * | ------------- state -------------- |
815 * We need to split the extent we found, and may flip bits on
818 * If the extent we found extends past our
819 * range, we just split and search again. It'll get split
820 * again the next time though.
822 * If the extent we found is inside our range, we set the
825 if (state
->start
< start
) {
826 if (state
->state
& exclusive_bits
) {
827 *failed_start
= start
;
832 prealloc
= alloc_extent_state_atomic(prealloc
);
834 err
= split_state(tree
, state
, prealloc
, start
);
836 extent_io_tree_panic(tree
, err
);
841 if (state
->end
<= end
) {
842 set_state_bits(tree
, state
, &bits
);
843 cache_state(state
, cached_state
);
844 merge_state(tree
, state
);
845 if (last_end
== (u64
)-1)
847 start
= last_end
+ 1;
852 * | ---- desired range ---- |
853 * | state | or | state |
855 * There's a hole, we need to insert something in it and
856 * ignore the extent we found.
858 if (state
->start
> start
) {
860 if (end
< last_start
)
863 this_end
= last_start
- 1;
865 prealloc
= alloc_extent_state_atomic(prealloc
);
869 * Avoid to free 'prealloc' if it can be merged with
872 err
= insert_state(tree
, prealloc
, start
, this_end
,
875 extent_io_tree_panic(tree
, err
);
877 cache_state(prealloc
, cached_state
);
879 start
= this_end
+ 1;
883 * | ---- desired range ---- |
885 * We need to split the extent, and set the bit
888 if (state
->start
<= end
&& state
->end
> end
) {
889 if (state
->state
& exclusive_bits
) {
890 *failed_start
= start
;
895 prealloc
= alloc_extent_state_atomic(prealloc
);
897 err
= split_state(tree
, state
, prealloc
, end
+ 1);
899 extent_io_tree_panic(tree
, err
);
901 set_state_bits(tree
, prealloc
, &bits
);
902 cache_state(prealloc
, cached_state
);
903 merge_state(tree
, prealloc
);
911 spin_unlock(&tree
->lock
);
913 free_extent_state(prealloc
);
920 spin_unlock(&tree
->lock
);
921 if (mask
& __GFP_WAIT
)
926 int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
,
927 u64
*failed_start
, struct extent_state
**cached_state
,
930 return __set_extent_bit(tree
, start
, end
, bits
, 0, failed_start
,
936 * convert_extent - convert all bits in a given range from one bit to another
937 * @tree: the io tree to search
938 * @start: the start offset in bytes
939 * @end: the end offset in bytes (inclusive)
940 * @bits: the bits to set in this range
941 * @clear_bits: the bits to clear in this range
942 * @mask: the allocation mask
944 * This will go through and set bits for the given range. If any states exist
945 * already in this range they are set with the given bit and cleared of the
946 * clear_bits. This is only meant to be used by things that are mergeable, ie
947 * converting from say DELALLOC to DIRTY. This is not meant to be used with
948 * boundary bits like LOCK.
950 int convert_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
951 int bits
, int clear_bits
, gfp_t mask
)
953 struct extent_state
*state
;
954 struct extent_state
*prealloc
= NULL
;
955 struct rb_node
*node
;
961 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
962 prealloc
= alloc_extent_state(mask
);
967 spin_lock(&tree
->lock
);
969 * this search will find all the extents that end after
972 node
= tree_search(tree
, start
);
974 prealloc
= alloc_extent_state_atomic(prealloc
);
979 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
982 extent_io_tree_panic(tree
, err
);
985 state
= rb_entry(node
, struct extent_state
, rb_node
);
987 last_start
= state
->start
;
988 last_end
= state
->end
;
991 * | ---- desired range ---- |
994 * Just lock what we found and keep going
996 if (state
->start
== start
&& state
->end
<= end
) {
997 struct rb_node
*next_node
;
999 set_state_bits(tree
, state
, &bits
);
1000 clear_state_bit(tree
, state
, &clear_bits
, 0);
1001 if (last_end
== (u64
)-1)
1004 start
= last_end
+ 1;
1005 next_node
= rb_next(&state
->rb_node
);
1006 if (next_node
&& start
< end
&& prealloc
&& !need_resched()) {
1007 state
= rb_entry(next_node
, struct extent_state
,
1009 if (state
->start
== start
)
1016 * | ---- desired range ---- |
1019 * | ------------- state -------------- |
1021 * We need to split the extent we found, and may flip bits on
1024 * If the extent we found extends past our
1025 * range, we just split and search again. It'll get split
1026 * again the next time though.
1028 * If the extent we found is inside our range, we set the
1029 * desired bit on it.
1031 if (state
->start
< start
) {
1032 prealloc
= alloc_extent_state_atomic(prealloc
);
1037 err
= split_state(tree
, state
, prealloc
, start
);
1039 extent_io_tree_panic(tree
, err
);
1043 if (state
->end
<= end
) {
1044 set_state_bits(tree
, state
, &bits
);
1045 clear_state_bit(tree
, state
, &clear_bits
, 0);
1046 if (last_end
== (u64
)-1)
1048 start
= last_end
+ 1;
1053 * | ---- desired range ---- |
1054 * | state | or | state |
1056 * There's a hole, we need to insert something in it and
1057 * ignore the extent we found.
1059 if (state
->start
> start
) {
1061 if (end
< last_start
)
1064 this_end
= last_start
- 1;
1066 prealloc
= alloc_extent_state_atomic(prealloc
);
1073 * Avoid to free 'prealloc' if it can be merged with
1076 err
= insert_state(tree
, prealloc
, start
, this_end
,
1079 extent_io_tree_panic(tree
, err
);
1081 start
= this_end
+ 1;
1085 * | ---- desired range ---- |
1087 * We need to split the extent, and set the bit
1090 if (state
->start
<= end
&& state
->end
> end
) {
1091 prealloc
= alloc_extent_state_atomic(prealloc
);
1097 err
= split_state(tree
, state
, prealloc
, end
+ 1);
1099 extent_io_tree_panic(tree
, err
);
1101 set_state_bits(tree
, prealloc
, &bits
);
1102 clear_state_bit(tree
, prealloc
, &clear_bits
, 0);
1110 spin_unlock(&tree
->lock
);
1112 free_extent_state(prealloc
);
1119 spin_unlock(&tree
->lock
);
1120 if (mask
& __GFP_WAIT
)
1125 /* wrappers around set/clear extent bit */
1126 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1129 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, NULL
,
1133 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1134 int bits
, gfp_t mask
)
1136 return set_extent_bit(tree
, start
, end
, bits
, NULL
,
1140 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1141 int bits
, gfp_t mask
)
1143 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
1146 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1147 struct extent_state
**cached_state
, gfp_t mask
)
1149 return set_extent_bit(tree
, start
, end
,
1150 EXTENT_DELALLOC
| EXTENT_UPTODATE
,
1151 NULL
, cached_state
, mask
);
1154 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1157 return clear_extent_bit(tree
, start
, end
,
1158 EXTENT_DIRTY
| EXTENT_DELALLOC
|
1159 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, mask
);
1162 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1165 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, NULL
,
1169 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1170 struct extent_state
**cached_state
, gfp_t mask
)
1172 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0,
1173 cached_state
, mask
);
1176 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
1177 u64 end
, struct extent_state
**cached_state
,
1180 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
1181 cached_state
, mask
);
1185 * either insert or lock state struct between start and end use mask to tell
1186 * us if waiting is desired.
1188 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1189 int bits
, struct extent_state
**cached_state
)
1194 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
1195 EXTENT_LOCKED
, &failed_start
,
1196 cached_state
, GFP_NOFS
);
1197 if (err
== -EEXIST
) {
1198 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
1199 start
= failed_start
;
1202 WARN_ON(start
> end
);
1207 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1209 return lock_extent_bits(tree
, start
, end
, 0, NULL
);
1212 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1217 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1218 &failed_start
, NULL
, GFP_NOFS
);
1219 if (err
== -EEXIST
) {
1220 if (failed_start
> start
)
1221 clear_extent_bit(tree
, start
, failed_start
- 1,
1222 EXTENT_LOCKED
, 1, 0, NULL
, GFP_NOFS
);
1228 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1229 struct extent_state
**cached
, gfp_t mask
)
1231 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1235 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1237 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1242 * helper function to set both pages and extents in the tree writeback
1244 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1246 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1247 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1250 while (index
<= end_index
) {
1251 page
= find_get_page(tree
->mapping
, index
);
1252 BUG_ON(!page
); /* Pages should be in the extent_io_tree */
1253 set_page_writeback(page
);
1254 page_cache_release(page
);
1260 /* find the first state struct with 'bits' set after 'start', and
1261 * return it. tree->lock must be held. NULL will returned if
1262 * nothing was found after 'start'
1264 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1265 u64 start
, int bits
)
1267 struct rb_node
*node
;
1268 struct extent_state
*state
;
1271 * this search will find all the extents that end after
1274 node
= tree_search(tree
, start
);
1279 state
= rb_entry(node
, struct extent_state
, rb_node
);
1280 if (state
->end
>= start
&& (state
->state
& bits
))
1283 node
= rb_next(node
);
1292 * find the first offset in the io tree with 'bits' set. zero is
1293 * returned if we find something, and *start_ret and *end_ret are
1294 * set to reflect the state struct that was found.
1296 * If nothing was found, 1 is returned, < 0 on error
1298 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1299 u64
*start_ret
, u64
*end_ret
, int bits
)
1301 struct extent_state
*state
;
1304 spin_lock(&tree
->lock
);
1305 state
= find_first_extent_bit_state(tree
, start
, bits
);
1307 *start_ret
= state
->start
;
1308 *end_ret
= state
->end
;
1311 spin_unlock(&tree
->lock
);
1316 * find a contiguous range of bytes in the file marked as delalloc, not
1317 * more than 'max_bytes'. start and end are used to return the range,
1319 * 1 is returned if we find something, 0 if nothing was in the tree
1321 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1322 u64
*start
, u64
*end
, u64 max_bytes
,
1323 struct extent_state
**cached_state
)
1325 struct rb_node
*node
;
1326 struct extent_state
*state
;
1327 u64 cur_start
= *start
;
1329 u64 total_bytes
= 0;
1331 spin_lock(&tree
->lock
);
1334 * this search will find all the extents that end after
1337 node
= tree_search(tree
, cur_start
);
1345 state
= rb_entry(node
, struct extent_state
, rb_node
);
1346 if (found
&& (state
->start
!= cur_start
||
1347 (state
->state
& EXTENT_BOUNDARY
))) {
1350 if (!(state
->state
& EXTENT_DELALLOC
)) {
1356 *start
= state
->start
;
1357 *cached_state
= state
;
1358 atomic_inc(&state
->refs
);
1362 cur_start
= state
->end
+ 1;
1363 node
= rb_next(node
);
1366 total_bytes
+= state
->end
- state
->start
+ 1;
1367 if (total_bytes
>= max_bytes
)
1371 spin_unlock(&tree
->lock
);
1375 static noinline
void __unlock_for_delalloc(struct inode
*inode
,
1376 struct page
*locked_page
,
1380 struct page
*pages
[16];
1381 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1382 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1383 unsigned long nr_pages
= end_index
- index
+ 1;
1386 if (index
== locked_page
->index
&& end_index
== index
)
1389 while (nr_pages
> 0) {
1390 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1391 min_t(unsigned long, nr_pages
,
1392 ARRAY_SIZE(pages
)), pages
);
1393 for (i
= 0; i
< ret
; i
++) {
1394 if (pages
[i
] != locked_page
)
1395 unlock_page(pages
[i
]);
1396 page_cache_release(pages
[i
]);
1404 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1405 struct page
*locked_page
,
1409 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1410 unsigned long start_index
= index
;
1411 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1412 unsigned long pages_locked
= 0;
1413 struct page
*pages
[16];
1414 unsigned long nrpages
;
1418 /* the caller is responsible for locking the start index */
1419 if (index
== locked_page
->index
&& index
== end_index
)
1422 /* skip the page at the start index */
1423 nrpages
= end_index
- index
+ 1;
1424 while (nrpages
> 0) {
1425 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1426 min_t(unsigned long,
1427 nrpages
, ARRAY_SIZE(pages
)), pages
);
1432 /* now we have an array of pages, lock them all */
1433 for (i
= 0; i
< ret
; i
++) {
1435 * the caller is taking responsibility for
1438 if (pages
[i
] != locked_page
) {
1439 lock_page(pages
[i
]);
1440 if (!PageDirty(pages
[i
]) ||
1441 pages
[i
]->mapping
!= inode
->i_mapping
) {
1443 unlock_page(pages
[i
]);
1444 page_cache_release(pages
[i
]);
1448 page_cache_release(pages
[i
]);
1457 if (ret
&& pages_locked
) {
1458 __unlock_for_delalloc(inode
, locked_page
,
1460 ((u64
)(start_index
+ pages_locked
- 1)) <<
1467 * find a contiguous range of bytes in the file marked as delalloc, not
1468 * more than 'max_bytes'. start and end are used to return the range,
1470 * 1 is returned if we find something, 0 if nothing was in the tree
1472 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1473 struct extent_io_tree
*tree
,
1474 struct page
*locked_page
,
1475 u64
*start
, u64
*end
,
1481 struct extent_state
*cached_state
= NULL
;
1486 /* step one, find a bunch of delalloc bytes starting at start */
1487 delalloc_start
= *start
;
1489 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1490 max_bytes
, &cached_state
);
1491 if (!found
|| delalloc_end
<= *start
) {
1492 *start
= delalloc_start
;
1493 *end
= delalloc_end
;
1494 free_extent_state(cached_state
);
1499 * start comes from the offset of locked_page. We have to lock
1500 * pages in order, so we can't process delalloc bytes before
1503 if (delalloc_start
< *start
)
1504 delalloc_start
= *start
;
1507 * make sure to limit the number of pages we try to lock down
1510 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1511 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1513 /* step two, lock all the pages after the page that has start */
1514 ret
= lock_delalloc_pages(inode
, locked_page
,
1515 delalloc_start
, delalloc_end
);
1516 if (ret
== -EAGAIN
) {
1517 /* some of the pages are gone, lets avoid looping by
1518 * shortening the size of the delalloc range we're searching
1520 free_extent_state(cached_state
);
1522 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1523 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1531 BUG_ON(ret
); /* Only valid values are 0 and -EAGAIN */
1533 /* step three, lock the state bits for the whole range */
1534 lock_extent_bits(tree
, delalloc_start
, delalloc_end
, 0, &cached_state
);
1536 /* then test to make sure it is all still delalloc */
1537 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1538 EXTENT_DELALLOC
, 1, cached_state
);
1540 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1541 &cached_state
, GFP_NOFS
);
1542 __unlock_for_delalloc(inode
, locked_page
,
1543 delalloc_start
, delalloc_end
);
1547 free_extent_state(cached_state
);
1548 *start
= delalloc_start
;
1549 *end
= delalloc_end
;
1554 int extent_clear_unlock_delalloc(struct inode
*inode
,
1555 struct extent_io_tree
*tree
,
1556 u64 start
, u64 end
, struct page
*locked_page
,
1560 struct page
*pages
[16];
1561 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1562 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1563 unsigned long nr_pages
= end_index
- index
+ 1;
1567 if (op
& EXTENT_CLEAR_UNLOCK
)
1568 clear_bits
|= EXTENT_LOCKED
;
1569 if (op
& EXTENT_CLEAR_DIRTY
)
1570 clear_bits
|= EXTENT_DIRTY
;
1572 if (op
& EXTENT_CLEAR_DELALLOC
)
1573 clear_bits
|= EXTENT_DELALLOC
;
1575 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1576 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1577 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1578 EXTENT_SET_PRIVATE2
)))
1581 while (nr_pages
> 0) {
1582 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1583 min_t(unsigned long,
1584 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1585 for (i
= 0; i
< ret
; i
++) {
1587 if (op
& EXTENT_SET_PRIVATE2
)
1588 SetPagePrivate2(pages
[i
]);
1590 if (pages
[i
] == locked_page
) {
1591 page_cache_release(pages
[i
]);
1594 if (op
& EXTENT_CLEAR_DIRTY
)
1595 clear_page_dirty_for_io(pages
[i
]);
1596 if (op
& EXTENT_SET_WRITEBACK
)
1597 set_page_writeback(pages
[i
]);
1598 if (op
& EXTENT_END_WRITEBACK
)
1599 end_page_writeback(pages
[i
]);
1600 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1601 unlock_page(pages
[i
]);
1602 page_cache_release(pages
[i
]);
1612 * count the number of bytes in the tree that have a given bit(s)
1613 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1614 * cached. The total number found is returned.
1616 u64
count_range_bits(struct extent_io_tree
*tree
,
1617 u64
*start
, u64 search_end
, u64 max_bytes
,
1618 unsigned long bits
, int contig
)
1620 struct rb_node
*node
;
1621 struct extent_state
*state
;
1622 u64 cur_start
= *start
;
1623 u64 total_bytes
= 0;
1627 if (search_end
<= cur_start
) {
1632 spin_lock(&tree
->lock
);
1633 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1634 total_bytes
= tree
->dirty_bytes
;
1638 * this search will find all the extents that end after
1641 node
= tree_search(tree
, cur_start
);
1646 state
= rb_entry(node
, struct extent_state
, rb_node
);
1647 if (state
->start
> search_end
)
1649 if (contig
&& found
&& state
->start
> last
+ 1)
1651 if (state
->end
>= cur_start
&& (state
->state
& bits
) == bits
) {
1652 total_bytes
+= min(search_end
, state
->end
) + 1 -
1653 max(cur_start
, state
->start
);
1654 if (total_bytes
>= max_bytes
)
1657 *start
= max(cur_start
, state
->start
);
1661 } else if (contig
&& found
) {
1664 node
= rb_next(node
);
1669 spin_unlock(&tree
->lock
);
1674 * set the private field for a given byte offset in the tree. If there isn't
1675 * an extent_state there already, this does nothing.
1677 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1679 struct rb_node
*node
;
1680 struct extent_state
*state
;
1683 spin_lock(&tree
->lock
);
1685 * this search will find all the extents that end after
1688 node
= tree_search(tree
, start
);
1693 state
= rb_entry(node
, struct extent_state
, rb_node
);
1694 if (state
->start
!= start
) {
1698 state
->private = private;
1700 spin_unlock(&tree
->lock
);
1704 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1706 struct rb_node
*node
;
1707 struct extent_state
*state
;
1710 spin_lock(&tree
->lock
);
1712 * this search will find all the extents that end after
1715 node
= tree_search(tree
, start
);
1720 state
= rb_entry(node
, struct extent_state
, rb_node
);
1721 if (state
->start
!= start
) {
1725 *private = state
->private;
1727 spin_unlock(&tree
->lock
);
1732 * searches a range in the state tree for a given mask.
1733 * If 'filled' == 1, this returns 1 only if every extent in the tree
1734 * has the bits set. Otherwise, 1 is returned if any bit in the
1735 * range is found set.
1737 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1738 int bits
, int filled
, struct extent_state
*cached
)
1740 struct extent_state
*state
= NULL
;
1741 struct rb_node
*node
;
1744 spin_lock(&tree
->lock
);
1745 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
1746 cached
->end
> start
)
1747 node
= &cached
->rb_node
;
1749 node
= tree_search(tree
, start
);
1750 while (node
&& start
<= end
) {
1751 state
= rb_entry(node
, struct extent_state
, rb_node
);
1753 if (filled
&& state
->start
> start
) {
1758 if (state
->start
> end
)
1761 if (state
->state
& bits
) {
1765 } else if (filled
) {
1770 if (state
->end
== (u64
)-1)
1773 start
= state
->end
+ 1;
1776 node
= rb_next(node
);
1783 spin_unlock(&tree
->lock
);
1788 * helper function to set a given page up to date if all the
1789 * extents in the tree for that page are up to date
1791 static void check_page_uptodate(struct extent_io_tree
*tree
, struct page
*page
)
1793 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1794 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1795 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1796 SetPageUptodate(page
);
1800 * helper function to unlock a page if all the extents in the tree
1801 * for that page are unlocked
1803 static void check_page_locked(struct extent_io_tree
*tree
, struct page
*page
)
1805 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1806 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1807 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1812 * helper function to end page writeback if all the extents
1813 * in the tree for that page are done with writeback
1815 static void check_page_writeback(struct extent_io_tree
*tree
,
1818 end_page_writeback(page
);
1822 * When IO fails, either with EIO or csum verification fails, we
1823 * try other mirrors that might have a good copy of the data. This
1824 * io_failure_record is used to record state as we go through all the
1825 * mirrors. If another mirror has good data, the page is set up to date
1826 * and things continue. If a good mirror can't be found, the original
1827 * bio end_io callback is called to indicate things have failed.
1829 struct io_failure_record
{
1834 unsigned long bio_flags
;
1840 static int free_io_failure(struct inode
*inode
, struct io_failure_record
*rec
,
1845 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1847 set_state_private(failure_tree
, rec
->start
, 0);
1848 ret
= clear_extent_bits(failure_tree
, rec
->start
,
1849 rec
->start
+ rec
->len
- 1,
1850 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1855 ret
= clear_extent_bits(&BTRFS_I(inode
)->io_tree
, rec
->start
,
1856 rec
->start
+ rec
->len
- 1,
1857 EXTENT_DAMAGED
, GFP_NOFS
);
1866 static void repair_io_failure_callback(struct bio
*bio
, int err
)
1868 complete(bio
->bi_private
);
1872 * this bypasses the standard btrfs submit functions deliberately, as
1873 * the standard behavior is to write all copies in a raid setup. here we only
1874 * want to write the one bad copy. so we do the mapping for ourselves and issue
1875 * submit_bio directly.
1876 * to avoid any synchonization issues, wait for the data after writing, which
1877 * actually prevents the read that triggered the error from finishing.
1878 * currently, there can be no more than two copies of every data bit. thus,
1879 * exactly one rewrite is required.
1881 int repair_io_failure(struct btrfs_mapping_tree
*map_tree
, u64 start
,
1882 u64 length
, u64 logical
, struct page
*page
,
1886 struct btrfs_device
*dev
;
1887 DECLARE_COMPLETION_ONSTACK(compl);
1890 struct btrfs_bio
*bbio
= NULL
;
1893 BUG_ON(!mirror_num
);
1895 bio
= bio_alloc(GFP_NOFS
, 1);
1898 bio
->bi_private
= &compl;
1899 bio
->bi_end_io
= repair_io_failure_callback
;
1901 map_length
= length
;
1903 ret
= btrfs_map_block(map_tree
, WRITE
, logical
,
1904 &map_length
, &bbio
, mirror_num
);
1909 BUG_ON(mirror_num
!= bbio
->mirror_num
);
1910 sector
= bbio
->stripes
[mirror_num
-1].physical
>> 9;
1911 bio
->bi_sector
= sector
;
1912 dev
= bbio
->stripes
[mirror_num
-1].dev
;
1914 if (!dev
|| !dev
->bdev
|| !dev
->writeable
) {
1918 bio
->bi_bdev
= dev
->bdev
;
1919 bio_add_page(bio
, page
, length
, start
-page_offset(page
));
1920 btrfsic_submit_bio(WRITE_SYNC
, bio
);
1921 wait_for_completion(&compl);
1923 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
1924 /* try to remap that extent elsewhere? */
1929 printk(KERN_INFO
"btrfs read error corrected: ino %lu off %llu (dev %s "
1930 "sector %llu)\n", page
->mapping
->host
->i_ino
, start
,
1937 int repair_eb_io_failure(struct btrfs_root
*root
, struct extent_buffer
*eb
,
1940 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1941 u64 start
= eb
->start
;
1942 unsigned long i
, num_pages
= num_extent_pages(eb
->start
, eb
->len
);
1945 for (i
= 0; i
< num_pages
; i
++) {
1946 struct page
*p
= extent_buffer_page(eb
, i
);
1947 ret
= repair_io_failure(map_tree
, start
, PAGE_CACHE_SIZE
,
1948 start
, p
, mirror_num
);
1951 start
+= PAGE_CACHE_SIZE
;
1958 * each time an IO finishes, we do a fast check in the IO failure tree
1959 * to see if we need to process or clean up an io_failure_record
1961 static int clean_io_failure(u64 start
, struct page
*page
)
1964 u64 private_failure
;
1965 struct io_failure_record
*failrec
;
1966 struct btrfs_mapping_tree
*map_tree
;
1967 struct extent_state
*state
;
1971 struct inode
*inode
= page
->mapping
->host
;
1974 ret
= count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1975 (u64
)-1, 1, EXTENT_DIRTY
, 0);
1979 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
, start
,
1984 failrec
= (struct io_failure_record
*)(unsigned long) private_failure
;
1985 BUG_ON(!failrec
->this_mirror
);
1987 if (failrec
->in_validation
) {
1988 /* there was no real error, just free the record */
1989 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1995 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1996 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1999 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
2001 if (state
&& state
->start
== failrec
->start
) {
2002 map_tree
= &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
;
2003 num_copies
= btrfs_num_copies(map_tree
, failrec
->logical
,
2005 if (num_copies
> 1) {
2006 ret
= repair_io_failure(map_tree
, start
, failrec
->len
,
2007 failrec
->logical
, page
,
2008 failrec
->failed_mirror
);
2015 ret
= free_io_failure(inode
, failrec
, did_repair
);
2021 * this is a generic handler for readpage errors (default
2022 * readpage_io_failed_hook). if other copies exist, read those and write back
2023 * good data to the failed position. does not investigate in remapping the
2024 * failed extent elsewhere, hoping the device will be smart enough to do this as
2028 static int bio_readpage_error(struct bio
*failed_bio
, struct page
*page
,
2029 u64 start
, u64 end
, int failed_mirror
,
2030 struct extent_state
*state
)
2032 struct io_failure_record
*failrec
= NULL
;
2034 struct extent_map
*em
;
2035 struct inode
*inode
= page
->mapping
->host
;
2036 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
2037 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2038 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2045 BUG_ON(failed_bio
->bi_rw
& REQ_WRITE
);
2047 ret
= get_state_private(failure_tree
, start
, &private);
2049 failrec
= kzalloc(sizeof(*failrec
), GFP_NOFS
);
2052 failrec
->start
= start
;
2053 failrec
->len
= end
- start
+ 1;
2054 failrec
->this_mirror
= 0;
2055 failrec
->bio_flags
= 0;
2056 failrec
->in_validation
= 0;
2058 read_lock(&em_tree
->lock
);
2059 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
2061 read_unlock(&em_tree
->lock
);
2066 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
2067 free_extent_map(em
);
2070 read_unlock(&em_tree
->lock
);
2072 if (!em
|| IS_ERR(em
)) {
2076 logical
= start
- em
->start
;
2077 logical
= em
->block_start
+ logical
;
2078 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2079 logical
= em
->block_start
;
2080 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
2081 extent_set_compress_type(&failrec
->bio_flags
,
2084 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2085 "len=%llu\n", logical
, start
, failrec
->len
);
2086 failrec
->logical
= logical
;
2087 free_extent_map(em
);
2089 /* set the bits in the private failure tree */
2090 ret
= set_extent_bits(failure_tree
, start
, end
,
2091 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
2093 ret
= set_state_private(failure_tree
, start
,
2094 (u64
)(unsigned long)failrec
);
2095 /* set the bits in the inode's tree */
2097 ret
= set_extent_bits(tree
, start
, end
, EXTENT_DAMAGED
,
2104 failrec
= (struct io_failure_record
*)(unsigned long)private;
2105 pr_debug("bio_readpage_error: (found) logical=%llu, "
2106 "start=%llu, len=%llu, validation=%d\n",
2107 failrec
->logical
, failrec
->start
, failrec
->len
,
2108 failrec
->in_validation
);
2110 * when data can be on disk more than twice, add to failrec here
2111 * (e.g. with a list for failed_mirror) to make
2112 * clean_io_failure() clean all those errors at once.
2115 num_copies
= btrfs_num_copies(
2116 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
2117 failrec
->logical
, failrec
->len
);
2118 if (num_copies
== 1) {
2120 * we only have a single copy of the data, so don't bother with
2121 * all the retry and error correction code that follows. no
2122 * matter what the error is, it is very likely to persist.
2124 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2125 "state=%p, num_copies=%d, next_mirror %d, "
2126 "failed_mirror %d\n", state
, num_copies
,
2127 failrec
->this_mirror
, failed_mirror
);
2128 free_io_failure(inode
, failrec
, 0);
2133 spin_lock(&tree
->lock
);
2134 state
= find_first_extent_bit_state(tree
, failrec
->start
,
2136 if (state
&& state
->start
!= failrec
->start
)
2138 spin_unlock(&tree
->lock
);
2142 * there are two premises:
2143 * a) deliver good data to the caller
2144 * b) correct the bad sectors on disk
2146 if (failed_bio
->bi_vcnt
> 1) {
2148 * to fulfill b), we need to know the exact failing sectors, as
2149 * we don't want to rewrite any more than the failed ones. thus,
2150 * we need separate read requests for the failed bio
2152 * if the following BUG_ON triggers, our validation request got
2153 * merged. we need separate requests for our algorithm to work.
2155 BUG_ON(failrec
->in_validation
);
2156 failrec
->in_validation
= 1;
2157 failrec
->this_mirror
= failed_mirror
;
2158 read_mode
= READ_SYNC
| REQ_FAILFAST_DEV
;
2161 * we're ready to fulfill a) and b) alongside. get a good copy
2162 * of the failed sector and if we succeed, we have setup
2163 * everything for repair_io_failure to do the rest for us.
2165 if (failrec
->in_validation
) {
2166 BUG_ON(failrec
->this_mirror
!= failed_mirror
);
2167 failrec
->in_validation
= 0;
2168 failrec
->this_mirror
= 0;
2170 failrec
->failed_mirror
= failed_mirror
;
2171 failrec
->this_mirror
++;
2172 if (failrec
->this_mirror
== failed_mirror
)
2173 failrec
->this_mirror
++;
2174 read_mode
= READ_SYNC
;
2177 if (!state
|| failrec
->this_mirror
> num_copies
) {
2178 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2179 "next_mirror %d, failed_mirror %d\n", state
,
2180 num_copies
, failrec
->this_mirror
, failed_mirror
);
2181 free_io_failure(inode
, failrec
, 0);
2185 bio
= bio_alloc(GFP_NOFS
, 1);
2187 free_io_failure(inode
, failrec
, 0);
2190 bio
->bi_private
= state
;
2191 bio
->bi_end_io
= failed_bio
->bi_end_io
;
2192 bio
->bi_sector
= failrec
->logical
>> 9;
2193 bio
->bi_bdev
= BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
;
2196 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
2198 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2199 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode
,
2200 failrec
->this_mirror
, num_copies
, failrec
->in_validation
);
2202 ret
= tree
->ops
->submit_bio_hook(inode
, read_mode
, bio
,
2203 failrec
->this_mirror
,
2204 failrec
->bio_flags
, 0);
2208 /* lots and lots of room for performance fixes in the end_bio funcs */
2210 int end_extent_writepage(struct page
*page
, int err
, u64 start
, u64 end
)
2212 int uptodate
= (err
== 0);
2213 struct extent_io_tree
*tree
;
2216 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2218 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
2219 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
2220 end
, NULL
, uptodate
);
2225 if (!uptodate
&& tree
->ops
&&
2226 tree
->ops
->writepage_io_failed_hook
) {
2227 ret
= tree
->ops
->writepage_io_failed_hook(NULL
, page
,
2229 /* Writeback already completed */
2235 clear_extent_uptodate(tree
, start
, end
, NULL
, GFP_NOFS
);
2236 ClearPageUptodate(page
);
2243 * after a writepage IO is done, we need to:
2244 * clear the uptodate bits on error
2245 * clear the writeback bits in the extent tree for this IO
2246 * end_page_writeback if the page has no more pending IO
2248 * Scheduling is not allowed, so the extent state tree is expected
2249 * to have one and only one object corresponding to this IO.
2251 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
2253 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2254 struct extent_io_tree
*tree
;
2260 struct page
*page
= bvec
->bv_page
;
2261 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2263 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
2265 end
= start
+ bvec
->bv_len
- 1;
2267 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2272 if (--bvec
>= bio
->bi_io_vec
)
2273 prefetchw(&bvec
->bv_page
->flags
);
2275 if (end_extent_writepage(page
, err
, start
, end
))
2279 end_page_writeback(page
);
2281 check_page_writeback(tree
, page
);
2282 } while (bvec
>= bio
->bi_io_vec
);
2288 * after a readpage IO is done, we need to:
2289 * clear the uptodate bits on error
2290 * set the uptodate bits if things worked
2291 * set the page up to date if all extents in the tree are uptodate
2292 * clear the lock bit in the extent tree
2293 * unlock the page if there are no other extents locked for it
2295 * Scheduling is not allowed, so the extent state tree is expected
2296 * to have one and only one object corresponding to this IO.
2298 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
2300 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2301 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2302 struct bio_vec
*bvec
= bio
->bi_io_vec
;
2303 struct extent_io_tree
*tree
;
2314 struct page
*page
= bvec
->bv_page
;
2315 struct extent_state
*cached
= NULL
;
2316 struct extent_state
*state
;
2318 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2319 "mirror=%ld\n", bio
->bi_vcnt
, bio
->bi_idx
, err
,
2320 (long int)bio
->bi_bdev
);
2321 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2323 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
2325 end
= start
+ bvec
->bv_len
- 1;
2327 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2332 if (++bvec
<= bvec_end
)
2333 prefetchw(&bvec
->bv_page
->flags
);
2335 spin_lock(&tree
->lock
);
2336 state
= find_first_extent_bit_state(tree
, start
, EXTENT_LOCKED
);
2337 if (state
&& state
->start
== start
) {
2339 * take a reference on the state, unlock will drop
2342 cache_state(state
, &cached
);
2344 spin_unlock(&tree
->lock
);
2346 mirror
= (int)(unsigned long)bio
->bi_bdev
;
2347 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
2348 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
2353 clean_io_failure(start
, page
);
2356 if (!uptodate
&& tree
->ops
&& tree
->ops
->readpage_io_failed_hook
) {
2357 ret
= tree
->ops
->readpage_io_failed_hook(page
, mirror
);
2359 test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
2361 } else if (!uptodate
) {
2363 * The generic bio_readpage_error handles errors the
2364 * following way: If possible, new read requests are
2365 * created and submitted and will end up in
2366 * end_bio_extent_readpage as well (if we're lucky, not
2367 * in the !uptodate case). In that case it returns 0 and
2368 * we just go on with the next page in our bio. If it
2369 * can't handle the error it will return -EIO and we
2370 * remain responsible for that page.
2372 ret
= bio_readpage_error(bio
, page
, start
, end
, mirror
, NULL
);
2375 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2378 uncache_state(&cached
);
2383 if (uptodate
&& tree
->track_uptodate
) {
2384 set_extent_uptodate(tree
, start
, end
, &cached
,
2387 unlock_extent_cached(tree
, start
, end
, &cached
, GFP_ATOMIC
);
2391 SetPageUptodate(page
);
2393 ClearPageUptodate(page
);
2399 check_page_uptodate(tree
, page
);
2401 ClearPageUptodate(page
);
2404 check_page_locked(tree
, page
);
2406 } while (bvec
<= bvec_end
);
2412 btrfs_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
2417 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2419 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
2420 while (!bio
&& (nr_vecs
/= 2))
2421 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2426 bio
->bi_bdev
= bdev
;
2427 bio
->bi_sector
= first_sector
;
2433 * Since writes are async, they will only return -ENOMEM.
2434 * Reads can return the full range of I/O error conditions.
2436 static int __must_check
submit_one_bio(int rw
, struct bio
*bio
,
2437 int mirror_num
, unsigned long bio_flags
)
2440 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2441 struct page
*page
= bvec
->bv_page
;
2442 struct extent_io_tree
*tree
= bio
->bi_private
;
2445 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
2447 bio
->bi_private
= NULL
;
2451 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
2452 ret
= tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
2453 mirror_num
, bio_flags
, start
);
2455 btrfsic_submit_bio(rw
, bio
);
2457 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
2463 static int merge_bio(struct extent_io_tree
*tree
, struct page
*page
,
2464 unsigned long offset
, size_t size
, struct bio
*bio
,
2465 unsigned long bio_flags
)
2468 if (tree
->ops
&& tree
->ops
->merge_bio_hook
)
2469 ret
= tree
->ops
->merge_bio_hook(page
, offset
, size
, bio
,
2476 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
2477 struct page
*page
, sector_t sector
,
2478 size_t size
, unsigned long offset
,
2479 struct block_device
*bdev
,
2480 struct bio
**bio_ret
,
2481 unsigned long max_pages
,
2482 bio_end_io_t end_io_func
,
2484 unsigned long prev_bio_flags
,
2485 unsigned long bio_flags
)
2491 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
2492 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
2493 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
2495 if (bio_ret
&& *bio_ret
) {
2498 contig
= bio
->bi_sector
== sector
;
2500 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
2503 if (prev_bio_flags
!= bio_flags
|| !contig
||
2504 merge_bio(tree
, page
, offset
, page_size
, bio
, bio_flags
) ||
2505 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
2506 ret
= submit_one_bio(rw
, bio
, mirror_num
,
2515 if (this_compressed
)
2518 nr
= bio_get_nr_vecs(bdev
);
2520 bio
= btrfs_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
2524 bio_add_page(bio
, page
, page_size
, offset
);
2525 bio
->bi_end_io
= end_io_func
;
2526 bio
->bi_private
= tree
;
2531 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
2536 void attach_extent_buffer_page(struct extent_buffer
*eb
, struct page
*page
)
2538 if (!PagePrivate(page
)) {
2539 SetPagePrivate(page
);
2540 page_cache_get(page
);
2541 set_page_private(page
, (unsigned long)eb
);
2543 WARN_ON(page
->private != (unsigned long)eb
);
2547 void set_page_extent_mapped(struct page
*page
)
2549 if (!PagePrivate(page
)) {
2550 SetPagePrivate(page
);
2551 page_cache_get(page
);
2552 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
2557 * basic readpage implementation. Locked extent state structs are inserted
2558 * into the tree that are removed when the IO is done (by the end_io
2560 * XXX JDM: This needs looking at to ensure proper page locking
2562 static int __extent_read_full_page(struct extent_io_tree
*tree
,
2564 get_extent_t
*get_extent
,
2565 struct bio
**bio
, int mirror_num
,
2566 unsigned long *bio_flags
)
2568 struct inode
*inode
= page
->mapping
->host
;
2569 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2570 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2574 u64 last_byte
= i_size_read(inode
);
2578 struct extent_map
*em
;
2579 struct block_device
*bdev
;
2580 struct btrfs_ordered_extent
*ordered
;
2583 size_t pg_offset
= 0;
2585 size_t disk_io_size
;
2586 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2587 unsigned long this_bio_flag
= 0;
2589 set_page_extent_mapped(page
);
2591 if (!PageUptodate(page
)) {
2592 if (cleancache_get_page(page
) == 0) {
2593 BUG_ON(blocksize
!= PAGE_SIZE
);
2600 lock_extent(tree
, start
, end
);
2601 ordered
= btrfs_lookup_ordered_extent(inode
, start
);
2604 unlock_extent(tree
, start
, end
);
2605 btrfs_start_ordered_extent(inode
, ordered
, 1);
2606 btrfs_put_ordered_extent(ordered
);
2609 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
2611 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
2614 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
2615 userpage
= kmap_atomic(page
, KM_USER0
);
2616 memset(userpage
+ zero_offset
, 0, iosize
);
2617 flush_dcache_page(page
);
2618 kunmap_atomic(userpage
, KM_USER0
);
2621 while (cur
<= end
) {
2622 if (cur
>= last_byte
) {
2624 struct extent_state
*cached
= NULL
;
2626 iosize
= PAGE_CACHE_SIZE
- pg_offset
;
2627 userpage
= kmap_atomic(page
, KM_USER0
);
2628 memset(userpage
+ pg_offset
, 0, iosize
);
2629 flush_dcache_page(page
);
2630 kunmap_atomic(userpage
, KM_USER0
);
2631 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2633 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2637 em
= get_extent(inode
, page
, pg_offset
, cur
,
2639 if (IS_ERR_OR_NULL(em
)) {
2641 unlock_extent(tree
, cur
, end
);
2644 extent_offset
= cur
- em
->start
;
2645 BUG_ON(extent_map_end(em
) <= cur
);
2648 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2649 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2650 extent_set_compress_type(&this_bio_flag
,
2654 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2655 cur_end
= min(extent_map_end(em
) - 1, end
);
2656 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2657 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2658 disk_io_size
= em
->block_len
;
2659 sector
= em
->block_start
>> 9;
2661 sector
= (em
->block_start
+ extent_offset
) >> 9;
2662 disk_io_size
= iosize
;
2665 block_start
= em
->block_start
;
2666 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2667 block_start
= EXTENT_MAP_HOLE
;
2668 free_extent_map(em
);
2671 /* we've found a hole, just zero and go on */
2672 if (block_start
== EXTENT_MAP_HOLE
) {
2674 struct extent_state
*cached
= NULL
;
2676 userpage
= kmap_atomic(page
, KM_USER0
);
2677 memset(userpage
+ pg_offset
, 0, iosize
);
2678 flush_dcache_page(page
);
2679 kunmap_atomic(userpage
, KM_USER0
);
2681 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2683 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2686 pg_offset
+= iosize
;
2689 /* the get_extent function already copied into the page */
2690 if (test_range_bit(tree
, cur
, cur_end
,
2691 EXTENT_UPTODATE
, 1, NULL
)) {
2692 check_page_uptodate(tree
, page
);
2693 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2695 pg_offset
+= iosize
;
2698 /* we have an inline extent but it didn't get marked up
2699 * to date. Error out
2701 if (block_start
== EXTENT_MAP_INLINE
) {
2703 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2705 pg_offset
+= iosize
;
2710 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2711 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2715 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2717 ret
= submit_extent_page(READ
, tree
, page
,
2718 sector
, disk_io_size
, pg_offset
,
2720 end_bio_extent_readpage
, mirror_num
,
2723 BUG_ON(ret
== -ENOMEM
);
2725 *bio_flags
= this_bio_flag
;
2730 pg_offset
+= iosize
;
2734 if (!PageError(page
))
2735 SetPageUptodate(page
);
2741 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2742 get_extent_t
*get_extent
, int mirror_num
)
2744 struct bio
*bio
= NULL
;
2745 unsigned long bio_flags
= 0;
2748 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, mirror_num
,
2751 ret
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
2755 static noinline
void update_nr_written(struct page
*page
,
2756 struct writeback_control
*wbc
,
2757 unsigned long nr_written
)
2759 wbc
->nr_to_write
-= nr_written
;
2760 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2761 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2762 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2766 * the writepage semantics are similar to regular writepage. extent
2767 * records are inserted to lock ranges in the tree, and as dirty areas
2768 * are found, they are marked writeback. Then the lock bits are removed
2769 * and the end_io handler clears the writeback ranges
2771 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2774 struct inode
*inode
= page
->mapping
->host
;
2775 struct extent_page_data
*epd
= data
;
2776 struct extent_io_tree
*tree
= epd
->tree
;
2777 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2779 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2783 u64 last_byte
= i_size_read(inode
);
2787 struct extent_state
*cached_state
= NULL
;
2788 struct extent_map
*em
;
2789 struct block_device
*bdev
;
2792 size_t pg_offset
= 0;
2794 loff_t i_size
= i_size_read(inode
);
2795 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2801 unsigned long nr_written
= 0;
2802 bool fill_delalloc
= true;
2804 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2805 write_flags
= WRITE_SYNC
;
2807 write_flags
= WRITE
;
2809 trace___extent_writepage(page
, inode
, wbc
);
2811 WARN_ON(!PageLocked(page
));
2813 ClearPageError(page
);
2815 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2816 if (page
->index
> end_index
||
2817 (page
->index
== end_index
&& !pg_offset
)) {
2818 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2823 if (page
->index
== end_index
) {
2826 userpage
= kmap_atomic(page
, KM_USER0
);
2827 memset(userpage
+ pg_offset
, 0,
2828 PAGE_CACHE_SIZE
- pg_offset
);
2829 kunmap_atomic(userpage
, KM_USER0
);
2830 flush_dcache_page(page
);
2834 set_page_extent_mapped(page
);
2836 if (!tree
->ops
|| !tree
->ops
->fill_delalloc
)
2837 fill_delalloc
= false;
2839 delalloc_start
= start
;
2842 if (!epd
->extent_locked
&& fill_delalloc
) {
2843 u64 delalloc_to_write
= 0;
2845 * make sure the wbc mapping index is at least updated
2848 update_nr_written(page
, wbc
, 0);
2850 while (delalloc_end
< page_end
) {
2851 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2856 if (nr_delalloc
== 0) {
2857 delalloc_start
= delalloc_end
+ 1;
2860 ret
= tree
->ops
->fill_delalloc(inode
, page
,
2865 /* File system has been set read-only */
2871 * delalloc_end is already one less than the total
2872 * length, so we don't subtract one from
2875 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2878 delalloc_start
= delalloc_end
+ 1;
2880 if (wbc
->nr_to_write
< delalloc_to_write
) {
2883 if (delalloc_to_write
< thresh
* 2)
2884 thresh
= delalloc_to_write
;
2885 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2889 /* did the fill delalloc function already unlock and start
2895 * we've unlocked the page, so we can't update
2896 * the mapping's writeback index, just update
2899 wbc
->nr_to_write
-= nr_written
;
2903 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2904 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2907 /* Fixup worker will requeue */
2909 wbc
->pages_skipped
++;
2911 redirty_page_for_writepage(wbc
, page
);
2912 update_nr_written(page
, wbc
, nr_written
);
2920 * we don't want to touch the inode after unlocking the page,
2921 * so we update the mapping writeback index now
2923 update_nr_written(page
, wbc
, nr_written
+ 1);
2926 if (last_byte
<= start
) {
2927 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2928 tree
->ops
->writepage_end_io_hook(page
, start
,
2933 blocksize
= inode
->i_sb
->s_blocksize
;
2935 while (cur
<= end
) {
2936 if (cur
>= last_byte
) {
2937 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2938 tree
->ops
->writepage_end_io_hook(page
, cur
,
2942 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2944 if (IS_ERR_OR_NULL(em
)) {
2949 extent_offset
= cur
- em
->start
;
2950 BUG_ON(extent_map_end(em
) <= cur
);
2952 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2953 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2954 sector
= (em
->block_start
+ extent_offset
) >> 9;
2956 block_start
= em
->block_start
;
2957 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2958 free_extent_map(em
);
2962 * compressed and inline extents are written through other
2965 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2966 block_start
== EXTENT_MAP_INLINE
) {
2968 * end_io notification does not happen here for
2969 * compressed extents
2971 if (!compressed
&& tree
->ops
&&
2972 tree
->ops
->writepage_end_io_hook
)
2973 tree
->ops
->writepage_end_io_hook(page
, cur
,
2976 else if (compressed
) {
2977 /* we don't want to end_page_writeback on
2978 * a compressed extent. this happens
2985 pg_offset
+= iosize
;
2988 /* leave this out until we have a page_mkwrite call */
2989 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2990 EXTENT_DIRTY
, 0, NULL
)) {
2992 pg_offset
+= iosize
;
2996 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2997 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
3005 unsigned long max_nr
= end_index
+ 1;
3007 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
3008 if (!PageWriteback(page
)) {
3009 printk(KERN_ERR
"btrfs warning page %lu not "
3010 "writeback, cur %llu end %llu\n",
3011 page
->index
, (unsigned long long)cur
,
3012 (unsigned long long)end
);
3015 ret
= submit_extent_page(write_flags
, tree
, page
,
3016 sector
, iosize
, pg_offset
,
3017 bdev
, &epd
->bio
, max_nr
,
3018 end_bio_extent_writepage
,
3024 pg_offset
+= iosize
;
3029 /* make sure the mapping tag for page dirty gets cleared */
3030 set_page_writeback(page
);
3031 end_page_writeback(page
);
3037 /* drop our reference on any cached states */
3038 free_extent_state(cached_state
);
3042 static int eb_wait(void *word
)
3048 static void wait_on_extent_buffer_writeback(struct extent_buffer
*eb
)
3050 wait_on_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
, eb_wait
,
3051 TASK_UNINTERRUPTIBLE
);
3054 static int lock_extent_buffer_for_io(struct extent_buffer
*eb
,
3055 struct btrfs_fs_info
*fs_info
,
3056 struct extent_page_data
*epd
)
3058 unsigned long i
, num_pages
;
3062 if (!btrfs_try_tree_write_lock(eb
)) {
3064 flush_write_bio(epd
);
3065 btrfs_tree_lock(eb
);
3068 if (test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
)) {
3069 btrfs_tree_unlock(eb
);
3073 flush_write_bio(epd
);
3077 wait_on_extent_buffer_writeback(eb
);
3078 btrfs_tree_lock(eb
);
3079 if (!test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
))
3081 btrfs_tree_unlock(eb
);
3085 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3086 set_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3087 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
3088 spin_lock(&fs_info
->delalloc_lock
);
3089 if (fs_info
->dirty_metadata_bytes
>= eb
->len
)
3090 fs_info
->dirty_metadata_bytes
-= eb
->len
;
3093 spin_unlock(&fs_info
->delalloc_lock
);
3097 btrfs_tree_unlock(eb
);
3102 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3103 for (i
= 0; i
< num_pages
; i
++) {
3104 struct page
*p
= extent_buffer_page(eb
, i
);
3106 if (!trylock_page(p
)) {
3108 flush_write_bio(epd
);
3118 static void end_extent_buffer_writeback(struct extent_buffer
*eb
)
3120 clear_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3121 smp_mb__after_clear_bit();
3122 wake_up_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
);
3125 static void end_bio_extent_buffer_writepage(struct bio
*bio
, int err
)
3127 int uptodate
= err
== 0;
3128 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
3129 struct extent_buffer
*eb
;
3133 struct page
*page
= bvec
->bv_page
;
3136 eb
= (struct extent_buffer
*)page
->private;
3138 done
= atomic_dec_and_test(&eb
->io_pages
);
3140 if (!uptodate
|| test_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
)) {
3141 set_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3142 ClearPageUptodate(page
);
3146 end_page_writeback(page
);
3151 end_extent_buffer_writeback(eb
);
3152 } while (bvec
>= bio
->bi_io_vec
);
3158 static int write_one_eb(struct extent_buffer
*eb
,
3159 struct btrfs_fs_info
*fs_info
,
3160 struct writeback_control
*wbc
,
3161 struct extent_page_data
*epd
)
3163 struct block_device
*bdev
= fs_info
->fs_devices
->latest_bdev
;
3164 u64 offset
= eb
->start
;
3165 unsigned long i
, num_pages
;
3166 int rw
= (epd
->sync_io
? WRITE_SYNC
: WRITE
);
3169 clear_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3170 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3171 atomic_set(&eb
->io_pages
, num_pages
);
3172 for (i
= 0; i
< num_pages
; i
++) {
3173 struct page
*p
= extent_buffer_page(eb
, i
);
3175 clear_page_dirty_for_io(p
);
3176 set_page_writeback(p
);
3177 ret
= submit_extent_page(rw
, eb
->tree
, p
, offset
>> 9,
3178 PAGE_CACHE_SIZE
, 0, bdev
, &epd
->bio
,
3179 -1, end_bio_extent_buffer_writepage
,
3182 set_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3184 if (atomic_sub_and_test(num_pages
- i
, &eb
->io_pages
))
3185 end_extent_buffer_writeback(eb
);
3189 offset
+= PAGE_CACHE_SIZE
;
3190 update_nr_written(p
, wbc
, 1);
3194 if (unlikely(ret
)) {
3195 for (; i
< num_pages
; i
++) {
3196 struct page
*p
= extent_buffer_page(eb
, i
);
3204 int btree_write_cache_pages(struct address_space
*mapping
,
3205 struct writeback_control
*wbc
)
3207 struct extent_io_tree
*tree
= &BTRFS_I(mapping
->host
)->io_tree
;
3208 struct btrfs_fs_info
*fs_info
= BTRFS_I(mapping
->host
)->root
->fs_info
;
3209 struct extent_buffer
*eb
, *prev_eb
= NULL
;
3210 struct extent_page_data epd
= {
3214 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3218 int nr_to_write_done
= 0;
3219 struct pagevec pvec
;
3222 pgoff_t end
; /* Inclusive */
3226 pagevec_init(&pvec
, 0);
3227 if (wbc
->range_cyclic
) {
3228 index
= mapping
->writeback_index
; /* Start from prev offset */
3231 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3232 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3235 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3236 tag
= PAGECACHE_TAG_TOWRITE
;
3238 tag
= PAGECACHE_TAG_DIRTY
;
3240 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3241 tag_pages_for_writeback(mapping
, index
, end
);
3242 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3243 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3244 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3248 for (i
= 0; i
< nr_pages
; i
++) {
3249 struct page
*page
= pvec
.pages
[i
];
3251 if (!PagePrivate(page
))
3254 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3259 eb
= (struct extent_buffer
*)page
->private;
3268 if (!atomic_inc_not_zero(&eb
->refs
)) {
3274 ret
= lock_extent_buffer_for_io(eb
, fs_info
, &epd
);
3276 free_extent_buffer(eb
);
3280 ret
= write_one_eb(eb
, fs_info
, wbc
, &epd
);
3283 free_extent_buffer(eb
);
3286 free_extent_buffer(eb
);
3289 * the filesystem may choose to bump up nr_to_write.
3290 * We have to make sure to honor the new nr_to_write
3293 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3295 pagevec_release(&pvec
);
3298 if (!scanned
&& !done
) {
3300 * We hit the last page and there is more work to be done: wrap
3301 * back to the start of the file
3307 flush_write_bio(&epd
);
3312 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3313 * @mapping: address space structure to write
3314 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3315 * @writepage: function called for each page
3316 * @data: data passed to writepage function
3318 * If a page is already under I/O, write_cache_pages() skips it, even
3319 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3320 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3321 * and msync() need to guarantee that all the data which was dirty at the time
3322 * the call was made get new I/O started against them. If wbc->sync_mode is
3323 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3324 * existing IO to complete.
3326 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
3327 struct address_space
*mapping
,
3328 struct writeback_control
*wbc
,
3329 writepage_t writepage
, void *data
,
3330 void (*flush_fn
)(void *))
3334 int nr_to_write_done
= 0;
3335 struct pagevec pvec
;
3338 pgoff_t end
; /* Inclusive */
3342 pagevec_init(&pvec
, 0);
3343 if (wbc
->range_cyclic
) {
3344 index
= mapping
->writeback_index
; /* Start from prev offset */
3347 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3348 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3351 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3352 tag
= PAGECACHE_TAG_TOWRITE
;
3354 tag
= PAGECACHE_TAG_DIRTY
;
3356 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3357 tag_pages_for_writeback(mapping
, index
, end
);
3358 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3359 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3360 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3364 for (i
= 0; i
< nr_pages
; i
++) {
3365 struct page
*page
= pvec
.pages
[i
];
3368 * At this point we hold neither mapping->tree_lock nor
3369 * lock on the page itself: the page may be truncated or
3370 * invalidated (changing page->mapping to NULL), or even
3371 * swizzled back from swapper_space to tmpfs file
3375 tree
->ops
->write_cache_pages_lock_hook
) {
3376 tree
->ops
->write_cache_pages_lock_hook(page
,
3379 if (!trylock_page(page
)) {
3385 if (unlikely(page
->mapping
!= mapping
)) {
3390 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3396 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3397 if (PageWriteback(page
))
3399 wait_on_page_writeback(page
);
3402 if (PageWriteback(page
) ||
3403 !clear_page_dirty_for_io(page
)) {
3408 ret
= (*writepage
)(page
, wbc
, data
);
3410 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
3418 * the filesystem may choose to bump up nr_to_write.
3419 * We have to make sure to honor the new nr_to_write
3422 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3424 pagevec_release(&pvec
);
3427 if (!scanned
&& !done
) {
3429 * We hit the last page and there is more work to be done: wrap
3430 * back to the start of the file
3439 static void flush_epd_write_bio(struct extent_page_data
*epd
)
3448 ret
= submit_one_bio(rw
, epd
->bio
, 0, 0);
3449 BUG_ON(ret
< 0); /* -ENOMEM */
3454 static noinline
void flush_write_bio(void *data
)
3456 struct extent_page_data
*epd
= data
;
3457 flush_epd_write_bio(epd
);
3460 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
3461 get_extent_t
*get_extent
,
3462 struct writeback_control
*wbc
)
3465 struct extent_page_data epd
= {
3468 .get_extent
= get_extent
,
3470 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3473 ret
= __extent_writepage(page
, wbc
, &epd
);
3475 flush_epd_write_bio(&epd
);
3479 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
3480 u64 start
, u64 end
, get_extent_t
*get_extent
,
3484 struct address_space
*mapping
= inode
->i_mapping
;
3486 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
3489 struct extent_page_data epd
= {
3492 .get_extent
= get_extent
,
3494 .sync_io
= mode
== WB_SYNC_ALL
,
3496 struct writeback_control wbc_writepages
= {
3498 .nr_to_write
= nr_pages
* 2,
3499 .range_start
= start
,
3500 .range_end
= end
+ 1,
3503 while (start
<= end
) {
3504 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
3505 if (clear_page_dirty_for_io(page
))
3506 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
3508 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
3509 tree
->ops
->writepage_end_io_hook(page
, start
,
3510 start
+ PAGE_CACHE_SIZE
- 1,
3514 page_cache_release(page
);
3515 start
+= PAGE_CACHE_SIZE
;
3518 flush_epd_write_bio(&epd
);
3522 int extent_writepages(struct extent_io_tree
*tree
,
3523 struct address_space
*mapping
,
3524 get_extent_t
*get_extent
,
3525 struct writeback_control
*wbc
)
3528 struct extent_page_data epd
= {
3531 .get_extent
= get_extent
,
3533 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3536 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
3537 __extent_writepage
, &epd
,
3539 flush_epd_write_bio(&epd
);
3543 int extent_readpages(struct extent_io_tree
*tree
,
3544 struct address_space
*mapping
,
3545 struct list_head
*pages
, unsigned nr_pages
,
3546 get_extent_t get_extent
)
3548 struct bio
*bio
= NULL
;
3550 unsigned long bio_flags
= 0;
3552 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
3553 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
3555 prefetchw(&page
->flags
);
3556 list_del(&page
->lru
);
3557 if (!add_to_page_cache_lru(page
, mapping
,
3558 page
->index
, GFP_NOFS
)) {
3559 __extent_read_full_page(tree
, page
, get_extent
,
3560 &bio
, 0, &bio_flags
);
3562 page_cache_release(page
);
3564 BUG_ON(!list_empty(pages
));
3566 return submit_one_bio(READ
, bio
, 0, bio_flags
);
3571 * basic invalidatepage code, this waits on any locked or writeback
3572 * ranges corresponding to the page, and then deletes any extent state
3573 * records from the tree
3575 int extent_invalidatepage(struct extent_io_tree
*tree
,
3576 struct page
*page
, unsigned long offset
)
3578 struct extent_state
*cached_state
= NULL
;
3579 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
3580 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3581 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
3583 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
3587 lock_extent_bits(tree
, start
, end
, 0, &cached_state
);
3588 wait_on_page_writeback(page
);
3589 clear_extent_bit(tree
, start
, end
,
3590 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
3591 EXTENT_DO_ACCOUNTING
,
3592 1, 1, &cached_state
, GFP_NOFS
);
3597 * a helper for releasepage, this tests for areas of the page that
3598 * are locked or under IO and drops the related state bits if it is safe
3601 int try_release_extent_state(struct extent_map_tree
*map
,
3602 struct extent_io_tree
*tree
, struct page
*page
,
3605 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3606 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3609 if (test_range_bit(tree
, start
, end
,
3610 EXTENT_IOBITS
, 0, NULL
))
3613 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
3616 * at this point we can safely clear everything except the
3617 * locked bit and the nodatasum bit
3619 ret
= clear_extent_bit(tree
, start
, end
,
3620 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
3623 /* if clear_extent_bit failed for enomem reasons,
3624 * we can't allow the release to continue.
3635 * a helper for releasepage. As long as there are no locked extents
3636 * in the range corresponding to the page, both state records and extent
3637 * map records are removed
3639 int try_release_extent_mapping(struct extent_map_tree
*map
,
3640 struct extent_io_tree
*tree
, struct page
*page
,
3643 struct extent_map
*em
;
3644 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3645 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3647 if ((mask
& __GFP_WAIT
) &&
3648 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
3650 while (start
<= end
) {
3651 len
= end
- start
+ 1;
3652 write_lock(&map
->lock
);
3653 em
= lookup_extent_mapping(map
, start
, len
);
3655 write_unlock(&map
->lock
);
3658 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
3659 em
->start
!= start
) {
3660 write_unlock(&map
->lock
);
3661 free_extent_map(em
);
3664 if (!test_range_bit(tree
, em
->start
,
3665 extent_map_end(em
) - 1,
3666 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
3668 remove_extent_mapping(map
, em
);
3669 /* once for the rb tree */
3670 free_extent_map(em
);
3672 start
= extent_map_end(em
);
3673 write_unlock(&map
->lock
);
3676 free_extent_map(em
);
3679 return try_release_extent_state(map
, tree
, page
, mask
);
3683 * helper function for fiemap, which doesn't want to see any holes.
3684 * This maps until we find something past 'last'
3686 static struct extent_map
*get_extent_skip_holes(struct inode
*inode
,
3689 get_extent_t
*get_extent
)
3691 u64 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
3692 struct extent_map
*em
;
3699 len
= last
- offset
;
3702 len
= (len
+ sectorsize
- 1) & ~(sectorsize
- 1);
3703 em
= get_extent(inode
, NULL
, 0, offset
, len
, 0);
3704 if (IS_ERR_OR_NULL(em
))
3707 /* if this isn't a hole return it */
3708 if (!test_bit(EXTENT_FLAG_VACANCY
, &em
->flags
) &&
3709 em
->block_start
!= EXTENT_MAP_HOLE
) {
3713 /* this is a hole, advance to the next extent */
3714 offset
= extent_map_end(em
);
3715 free_extent_map(em
);
3722 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3723 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
3727 u64 max
= start
+ len
;
3731 u64 last_for_get_extent
= 0;
3733 u64 isize
= i_size_read(inode
);
3734 struct btrfs_key found_key
;
3735 struct extent_map
*em
= NULL
;
3736 struct extent_state
*cached_state
= NULL
;
3737 struct btrfs_path
*path
;
3738 struct btrfs_file_extent_item
*item
;
3743 unsigned long emflags
;
3748 path
= btrfs_alloc_path();
3751 path
->leave_spinning
= 1;
3753 start
= ALIGN(start
, BTRFS_I(inode
)->root
->sectorsize
);
3754 len
= ALIGN(len
, BTRFS_I(inode
)->root
->sectorsize
);
3757 * lookup the last file extent. We're not using i_size here
3758 * because there might be preallocation past i_size
3760 ret
= btrfs_lookup_file_extent(NULL
, BTRFS_I(inode
)->root
,
3761 path
, btrfs_ino(inode
), -1, 0);
3763 btrfs_free_path(path
);
3768 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3769 struct btrfs_file_extent_item
);
3770 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, path
->slots
[0]);
3771 found_type
= btrfs_key_type(&found_key
);
3773 /* No extents, but there might be delalloc bits */
3774 if (found_key
.objectid
!= btrfs_ino(inode
) ||
3775 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
3776 /* have to trust i_size as the end */
3778 last_for_get_extent
= isize
;
3781 * remember the start of the last extent. There are a
3782 * bunch of different factors that go into the length of the
3783 * extent, so its much less complex to remember where it started
3785 last
= found_key
.offset
;
3786 last_for_get_extent
= last
+ 1;
3788 btrfs_free_path(path
);
3791 * we might have some extents allocated but more delalloc past those
3792 * extents. so, we trust isize unless the start of the last extent is
3797 last_for_get_extent
= isize
;
3800 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
3803 em
= get_extent_skip_holes(inode
, start
, last_for_get_extent
,
3813 u64 offset_in_extent
;
3815 /* break if the extent we found is outside the range */
3816 if (em
->start
>= max
|| extent_map_end(em
) < off
)
3820 * get_extent may return an extent that starts before our
3821 * requested range. We have to make sure the ranges
3822 * we return to fiemap always move forward and don't
3823 * overlap, so adjust the offsets here
3825 em_start
= max(em
->start
, off
);
3828 * record the offset from the start of the extent
3829 * for adjusting the disk offset below
3831 offset_in_extent
= em_start
- em
->start
;
3832 em_end
= extent_map_end(em
);
3833 em_len
= em_end
- em_start
;
3834 emflags
= em
->flags
;
3839 * bump off for our next call to get_extent
3841 off
= extent_map_end(em
);
3845 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
3847 flags
|= FIEMAP_EXTENT_LAST
;
3848 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
3849 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
3850 FIEMAP_EXTENT_NOT_ALIGNED
);
3851 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
3852 flags
|= (FIEMAP_EXTENT_DELALLOC
|
3853 FIEMAP_EXTENT_UNKNOWN
);
3855 disko
= em
->block_start
+ offset_in_extent
;
3857 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
3858 flags
|= FIEMAP_EXTENT_ENCODED
;
3860 free_extent_map(em
);
3862 if ((em_start
>= last
) || em_len
== (u64
)-1 ||
3863 (last
== (u64
)-1 && isize
<= em_end
)) {
3864 flags
|= FIEMAP_EXTENT_LAST
;
3868 /* now scan forward to see if this is really the last extent. */
3869 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
,
3876 flags
|= FIEMAP_EXTENT_LAST
;
3879 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3885 free_extent_map(em
);
3887 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3888 &cached_state
, GFP_NOFS
);
3892 inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
3895 return eb
->pages
[i
];
3898 inline unsigned long num_extent_pages(u64 start
, u64 len
)
3900 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
3901 (start
>> PAGE_CACHE_SHIFT
);
3904 static void __free_extent_buffer(struct extent_buffer
*eb
)
3907 unsigned long flags
;
3908 spin_lock_irqsave(&leak_lock
, flags
);
3909 list_del(&eb
->leak_list
);
3910 spin_unlock_irqrestore(&leak_lock
, flags
);
3912 if (eb
->pages
&& eb
->pages
!= eb
->inline_pages
)
3914 kmem_cache_free(extent_buffer_cache
, eb
);
3917 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3922 struct extent_buffer
*eb
= NULL
;
3924 unsigned long flags
;
3927 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
3933 rwlock_init(&eb
->lock
);
3934 atomic_set(&eb
->write_locks
, 0);
3935 atomic_set(&eb
->read_locks
, 0);
3936 atomic_set(&eb
->blocking_readers
, 0);
3937 atomic_set(&eb
->blocking_writers
, 0);
3938 atomic_set(&eb
->spinning_readers
, 0);
3939 atomic_set(&eb
->spinning_writers
, 0);
3940 eb
->lock_nested
= 0;
3941 init_waitqueue_head(&eb
->write_lock_wq
);
3942 init_waitqueue_head(&eb
->read_lock_wq
);
3945 spin_lock_irqsave(&leak_lock
, flags
);
3946 list_add(&eb
->leak_list
, &buffers
);
3947 spin_unlock_irqrestore(&leak_lock
, flags
);
3949 spin_lock_init(&eb
->refs_lock
);
3950 atomic_set(&eb
->refs
, 1);
3951 atomic_set(&eb
->io_pages
, 0);
3953 if (len
> MAX_INLINE_EXTENT_BUFFER_SIZE
) {
3954 struct page
**pages
;
3955 int num_pages
= (len
+ PAGE_CACHE_SIZE
- 1) >>
3957 pages
= kzalloc(num_pages
, mask
);
3959 __free_extent_buffer(eb
);
3964 eb
->pages
= eb
->inline_pages
;
3970 static int extent_buffer_under_io(struct extent_buffer
*eb
)
3972 return (atomic_read(&eb
->io_pages
) ||
3973 test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
) ||
3974 test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
3978 * Helper for releasing extent buffer page.
3980 static void btrfs_release_extent_buffer_page(struct extent_buffer
*eb
,
3981 unsigned long start_idx
)
3983 unsigned long index
;
3986 BUG_ON(extent_buffer_under_io(eb
));
3988 index
= num_extent_pages(eb
->start
, eb
->len
);
3989 if (start_idx
>= index
)
3994 page
= extent_buffer_page(eb
, index
);
3996 spin_lock(&page
->mapping
->private_lock
);
3998 * We do this since we'll remove the pages after we've
3999 * removed the eb from the radix tree, so we could race
4000 * and have this page now attached to the new eb. So
4001 * only clear page_private if it's still connected to
4004 if (PagePrivate(page
) &&
4005 page
->private == (unsigned long)eb
) {
4006 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4007 BUG_ON(PageDirty(page
));
4008 BUG_ON(PageWriteback(page
));
4010 * We need to make sure we haven't be attached
4013 ClearPagePrivate(page
);
4014 set_page_private(page
, 0);
4015 /* One for the page private */
4016 page_cache_release(page
);
4018 spin_unlock(&page
->mapping
->private_lock
);
4020 /* One for when we alloced the page */
4021 page_cache_release(page
);
4023 } while (index
!= start_idx
);
4027 * Helper for releasing the extent buffer.
4029 static inline void btrfs_release_extent_buffer(struct extent_buffer
*eb
)
4031 btrfs_release_extent_buffer_page(eb
, 0);
4032 __free_extent_buffer(eb
);
4035 static void check_buffer_tree_ref(struct extent_buffer
*eb
)
4037 /* the ref bit is tricky. We have to make sure it is set
4038 * if we have the buffer dirty. Otherwise the
4039 * code to free a buffer can end up dropping a dirty
4042 * Once the ref bit is set, it won't go away while the
4043 * buffer is dirty or in writeback, and it also won't
4044 * go away while we have the reference count on the
4047 * We can't just set the ref bit without bumping the
4048 * ref on the eb because free_extent_buffer might
4049 * see the ref bit and try to clear it. If this happens
4050 * free_extent_buffer might end up dropping our original
4051 * ref by mistake and freeing the page before we are able
4052 * to add one more ref.
4054 * So bump the ref count first, then set the bit. If someone
4055 * beat us to it, drop the ref we added.
4057 if (!test_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
)) {
4058 atomic_inc(&eb
->refs
);
4059 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4060 atomic_dec(&eb
->refs
);
4064 static void mark_extent_buffer_accessed(struct extent_buffer
*eb
)
4066 unsigned long num_pages
, i
;
4068 check_buffer_tree_ref(eb
);
4070 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4071 for (i
= 0; i
< num_pages
; i
++) {
4072 struct page
*p
= extent_buffer_page(eb
, i
);
4073 mark_page_accessed(p
);
4077 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
4078 u64 start
, unsigned long len
)
4080 unsigned long num_pages
= num_extent_pages(start
, len
);
4082 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
4083 struct extent_buffer
*eb
;
4084 struct extent_buffer
*exists
= NULL
;
4086 struct address_space
*mapping
= tree
->mapping
;
4091 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4092 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4094 mark_extent_buffer_accessed(eb
);
4099 eb
= __alloc_extent_buffer(tree
, start
, len
, GFP_NOFS
);
4103 for (i
= 0; i
< num_pages
; i
++, index
++) {
4104 p
= find_or_create_page(mapping
, index
, GFP_NOFS
);
4110 spin_lock(&mapping
->private_lock
);
4111 if (PagePrivate(p
)) {
4113 * We could have already allocated an eb for this page
4114 * and attached one so lets see if we can get a ref on
4115 * the existing eb, and if we can we know it's good and
4116 * we can just return that one, else we know we can just
4117 * overwrite page->private.
4119 exists
= (struct extent_buffer
*)p
->private;
4120 if (atomic_inc_not_zero(&exists
->refs
)) {
4121 spin_unlock(&mapping
->private_lock
);
4123 mark_extent_buffer_accessed(exists
);
4128 * Do this so attach doesn't complain and we need to
4129 * drop the ref the old guy had.
4131 ClearPagePrivate(p
);
4132 WARN_ON(PageDirty(p
));
4133 page_cache_release(p
);
4135 attach_extent_buffer_page(eb
, p
);
4136 spin_unlock(&mapping
->private_lock
);
4137 WARN_ON(PageDirty(p
));
4138 mark_page_accessed(p
);
4140 if (!PageUptodate(p
))
4144 * see below about how we avoid a nasty race with release page
4145 * and why we unlock later
4149 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4151 ret
= radix_tree_preload(GFP_NOFS
& ~__GFP_HIGHMEM
);
4155 spin_lock(&tree
->buffer_lock
);
4156 ret
= radix_tree_insert(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
, eb
);
4157 if (ret
== -EEXIST
) {
4158 exists
= radix_tree_lookup(&tree
->buffer
,
4159 start
>> PAGE_CACHE_SHIFT
);
4160 if (!atomic_inc_not_zero(&exists
->refs
)) {
4161 spin_unlock(&tree
->buffer_lock
);
4162 radix_tree_preload_end();
4166 spin_unlock(&tree
->buffer_lock
);
4167 radix_tree_preload_end();
4168 mark_extent_buffer_accessed(exists
);
4171 /* add one reference for the tree */
4172 spin_lock(&eb
->refs_lock
);
4173 check_buffer_tree_ref(eb
);
4174 spin_unlock(&eb
->refs_lock
);
4175 spin_unlock(&tree
->buffer_lock
);
4176 radix_tree_preload_end();
4179 * there is a race where release page may have
4180 * tried to find this extent buffer in the radix
4181 * but failed. It will tell the VM it is safe to
4182 * reclaim the, and it will clear the page private bit.
4183 * We must make sure to set the page private bit properly
4184 * after the extent buffer is in the radix tree so
4185 * it doesn't get lost
4187 SetPageChecked(eb
->pages
[0]);
4188 for (i
= 1; i
< num_pages
; i
++) {
4189 p
= extent_buffer_page(eb
, i
);
4190 ClearPageChecked(p
);
4193 unlock_page(eb
->pages
[0]);
4197 for (i
= 0; i
< num_pages
; i
++) {
4199 unlock_page(eb
->pages
[i
]);
4202 if (!atomic_dec_and_test(&eb
->refs
))
4204 btrfs_release_extent_buffer(eb
);
4208 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
4209 u64 start
, unsigned long len
)
4211 struct extent_buffer
*eb
;
4214 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4215 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4217 mark_extent_buffer_accessed(eb
);
4225 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head
*head
)
4227 struct extent_buffer
*eb
=
4228 container_of(head
, struct extent_buffer
, rcu_head
);
4230 __free_extent_buffer(eb
);
4233 /* Expects to have eb->eb_lock already held */
4234 static void release_extent_buffer(struct extent_buffer
*eb
, gfp_t mask
)
4236 WARN_ON(atomic_read(&eb
->refs
) == 0);
4237 if (atomic_dec_and_test(&eb
->refs
)) {
4238 struct extent_io_tree
*tree
= eb
->tree
;
4240 spin_unlock(&eb
->refs_lock
);
4242 spin_lock(&tree
->buffer_lock
);
4243 radix_tree_delete(&tree
->buffer
,
4244 eb
->start
>> PAGE_CACHE_SHIFT
);
4245 spin_unlock(&tree
->buffer_lock
);
4247 /* Should be safe to release our pages at this point */
4248 btrfs_release_extent_buffer_page(eb
, 0);
4250 call_rcu(&eb
->rcu_head
, btrfs_release_extent_buffer_rcu
);
4253 spin_unlock(&eb
->refs_lock
);
4256 void free_extent_buffer(struct extent_buffer
*eb
)
4261 spin_lock(&eb
->refs_lock
);
4262 if (atomic_read(&eb
->refs
) == 2 &&
4263 test_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
) &&
4264 !extent_buffer_under_io(eb
) &&
4265 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4266 atomic_dec(&eb
->refs
);
4269 * I know this is terrible, but it's temporary until we stop tracking
4270 * the uptodate bits and such for the extent buffers.
4272 release_extent_buffer(eb
, GFP_ATOMIC
);
4275 void free_extent_buffer_stale(struct extent_buffer
*eb
)
4280 spin_lock(&eb
->refs_lock
);
4281 set_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
);
4283 if (atomic_read(&eb
->refs
) == 2 && !extent_buffer_under_io(eb
) &&
4284 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4285 atomic_dec(&eb
->refs
);
4286 release_extent_buffer(eb
, GFP_NOFS
);
4289 void clear_extent_buffer_dirty(struct extent_buffer
*eb
)
4292 unsigned long num_pages
;
4295 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4297 for (i
= 0; i
< num_pages
; i
++) {
4298 page
= extent_buffer_page(eb
, i
);
4299 if (!PageDirty(page
))
4303 WARN_ON(!PagePrivate(page
));
4305 clear_page_dirty_for_io(page
);
4306 spin_lock_irq(&page
->mapping
->tree_lock
);
4307 if (!PageDirty(page
)) {
4308 radix_tree_tag_clear(&page
->mapping
->page_tree
,
4310 PAGECACHE_TAG_DIRTY
);
4312 spin_unlock_irq(&page
->mapping
->tree_lock
);
4313 ClearPageError(page
);
4316 WARN_ON(atomic_read(&eb
->refs
) == 0);
4319 int set_extent_buffer_dirty(struct extent_buffer
*eb
)
4322 unsigned long num_pages
;
4325 check_buffer_tree_ref(eb
);
4327 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
4329 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4330 WARN_ON(atomic_read(&eb
->refs
) == 0);
4331 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
));
4333 for (i
= 0; i
< num_pages
; i
++)
4334 set_page_dirty(extent_buffer_page(eb
, i
));
4338 static int range_straddles_pages(u64 start
, u64 len
)
4340 if (len
< PAGE_CACHE_SIZE
)
4342 if (start
& (PAGE_CACHE_SIZE
- 1))
4344 if ((start
+ len
) & (PAGE_CACHE_SIZE
- 1))
4349 int clear_extent_buffer_uptodate(struct extent_buffer
*eb
)
4353 unsigned long num_pages
;
4355 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4356 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4357 for (i
= 0; i
< num_pages
; i
++) {
4358 page
= extent_buffer_page(eb
, i
);
4360 ClearPageUptodate(page
);
4365 int set_extent_buffer_uptodate(struct extent_buffer
*eb
)
4369 unsigned long num_pages
;
4371 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4372 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4373 for (i
= 0; i
< num_pages
; i
++) {
4374 page
= extent_buffer_page(eb
, i
);
4375 SetPageUptodate(page
);
4380 int extent_range_uptodate(struct extent_io_tree
*tree
,
4385 int pg_uptodate
= 1;
4387 unsigned long index
;
4389 if (range_straddles_pages(start
, end
- start
+ 1)) {
4390 ret
= test_range_bit(tree
, start
, end
,
4391 EXTENT_UPTODATE
, 1, NULL
);
4395 while (start
<= end
) {
4396 index
= start
>> PAGE_CACHE_SHIFT
;
4397 page
= find_get_page(tree
->mapping
, index
);
4400 uptodate
= PageUptodate(page
);
4401 page_cache_release(page
);
4406 start
+= PAGE_CACHE_SIZE
;
4411 int extent_buffer_uptodate(struct extent_buffer
*eb
)
4413 return test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4416 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
4417 struct extent_buffer
*eb
, u64 start
, int wait
,
4418 get_extent_t
*get_extent
, int mirror_num
)
4421 unsigned long start_i
;
4425 int locked_pages
= 0;
4426 int all_uptodate
= 1;
4427 unsigned long num_pages
;
4428 unsigned long num_reads
= 0;
4429 struct bio
*bio
= NULL
;
4430 unsigned long bio_flags
= 0;
4432 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
4436 WARN_ON(start
< eb
->start
);
4437 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
4438 (eb
->start
>> PAGE_CACHE_SHIFT
);
4443 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4444 for (i
= start_i
; i
< num_pages
; i
++) {
4445 page
= extent_buffer_page(eb
, i
);
4446 if (wait
== WAIT_NONE
) {
4447 if (!trylock_page(page
))
4453 if (!PageUptodate(page
)) {
4460 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4464 clear_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
4465 eb
->read_mirror
= 0;
4466 atomic_set(&eb
->io_pages
, num_reads
);
4467 for (i
= start_i
; i
< num_pages
; i
++) {
4468 page
= extent_buffer_page(eb
, i
);
4469 if (!PageUptodate(page
)) {
4470 ClearPageError(page
);
4471 err
= __extent_read_full_page(tree
, page
,
4473 mirror_num
, &bio_flags
);
4482 err
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
4487 if (ret
|| wait
!= WAIT_COMPLETE
)
4490 for (i
= start_i
; i
< num_pages
; i
++) {
4491 page
= extent_buffer_page(eb
, i
);
4492 wait_on_page_locked(page
);
4493 if (!PageUptodate(page
))
4501 while (locked_pages
> 0) {
4502 page
= extent_buffer_page(eb
, i
);
4510 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
4511 unsigned long start
,
4518 char *dst
= (char *)dstv
;
4519 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4520 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4522 WARN_ON(start
> eb
->len
);
4523 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4525 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4528 page
= extent_buffer_page(eb
, i
);
4530 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4531 kaddr
= page_address(page
);
4532 memcpy(dst
, kaddr
+ offset
, cur
);
4541 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
4542 unsigned long min_len
, char **map
,
4543 unsigned long *map_start
,
4544 unsigned long *map_len
)
4546 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
4549 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4550 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4551 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
4558 offset
= start_offset
;
4562 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
4565 if (start
+ min_len
> eb
->len
) {
4566 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
4567 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
4568 eb
->len
, start
, min_len
);
4573 p
= extent_buffer_page(eb
, i
);
4574 kaddr
= page_address(p
);
4575 *map
= kaddr
+ offset
;
4576 *map_len
= PAGE_CACHE_SIZE
- offset
;
4580 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
4581 unsigned long start
,
4588 char *ptr
= (char *)ptrv
;
4589 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4590 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4593 WARN_ON(start
> eb
->len
);
4594 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4596 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4599 page
= extent_buffer_page(eb
, i
);
4601 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4603 kaddr
= page_address(page
);
4604 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
4616 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
4617 unsigned long start
, unsigned long len
)
4623 char *src
= (char *)srcv
;
4624 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4625 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4627 WARN_ON(start
> eb
->len
);
4628 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4630 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4633 page
= extent_buffer_page(eb
, i
);
4634 WARN_ON(!PageUptodate(page
));
4636 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4637 kaddr
= page_address(page
);
4638 memcpy(kaddr
+ offset
, src
, cur
);
4647 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
4648 unsigned long start
, unsigned long len
)
4654 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4655 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4657 WARN_ON(start
> eb
->len
);
4658 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4660 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4663 page
= extent_buffer_page(eb
, i
);
4664 WARN_ON(!PageUptodate(page
));
4666 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4667 kaddr
= page_address(page
);
4668 memset(kaddr
+ offset
, c
, cur
);
4676 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
4677 unsigned long dst_offset
, unsigned long src_offset
,
4680 u64 dst_len
= dst
->len
;
4685 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4686 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4688 WARN_ON(src
->len
!= dst_len
);
4690 offset
= (start_offset
+ dst_offset
) &
4691 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4694 page
= extent_buffer_page(dst
, i
);
4695 WARN_ON(!PageUptodate(page
));
4697 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
4699 kaddr
= page_address(page
);
4700 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
4709 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
4710 unsigned long dst_off
, unsigned long src_off
,
4713 char *dst_kaddr
= page_address(dst_page
);
4714 if (dst_page
== src_page
) {
4715 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
4717 char *src_kaddr
= page_address(src_page
);
4718 char *p
= dst_kaddr
+ dst_off
+ len
;
4719 char *s
= src_kaddr
+ src_off
+ len
;
4726 static inline bool areas_overlap(unsigned long src
, unsigned long dst
, unsigned long len
)
4728 unsigned long distance
= (src
> dst
) ? src
- dst
: dst
- src
;
4729 return distance
< len
;
4732 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
4733 unsigned long dst_off
, unsigned long src_off
,
4736 char *dst_kaddr
= page_address(dst_page
);
4738 int must_memmove
= 0;
4740 if (dst_page
!= src_page
) {
4741 src_kaddr
= page_address(src_page
);
4743 src_kaddr
= dst_kaddr
;
4744 if (areas_overlap(src_off
, dst_off
, len
))
4749 memmove(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4751 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4754 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4755 unsigned long src_offset
, unsigned long len
)
4758 size_t dst_off_in_page
;
4759 size_t src_off_in_page
;
4760 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4761 unsigned long dst_i
;
4762 unsigned long src_i
;
4764 if (src_offset
+ len
> dst
->len
) {
4765 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4766 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
4769 if (dst_offset
+ len
> dst
->len
) {
4770 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4771 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
4776 dst_off_in_page
= (start_offset
+ dst_offset
) &
4777 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4778 src_off_in_page
= (start_offset
+ src_offset
) &
4779 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4781 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4782 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
4784 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
4786 cur
= min_t(unsigned long, cur
,
4787 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
4789 copy_pages(extent_buffer_page(dst
, dst_i
),
4790 extent_buffer_page(dst
, src_i
),
4791 dst_off_in_page
, src_off_in_page
, cur
);
4799 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4800 unsigned long src_offset
, unsigned long len
)
4803 size_t dst_off_in_page
;
4804 size_t src_off_in_page
;
4805 unsigned long dst_end
= dst_offset
+ len
- 1;
4806 unsigned long src_end
= src_offset
+ len
- 1;
4807 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4808 unsigned long dst_i
;
4809 unsigned long src_i
;
4811 if (src_offset
+ len
> dst
->len
) {
4812 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4813 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
4816 if (dst_offset
+ len
> dst
->len
) {
4817 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4818 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
4821 if (dst_offset
< src_offset
) {
4822 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
4826 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
4827 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
4829 dst_off_in_page
= (start_offset
+ dst_end
) &
4830 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4831 src_off_in_page
= (start_offset
+ src_end
) &
4832 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4834 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
4835 cur
= min(cur
, dst_off_in_page
+ 1);
4836 move_pages(extent_buffer_page(dst
, dst_i
),
4837 extent_buffer_page(dst
, src_i
),
4838 dst_off_in_page
- cur
+ 1,
4839 src_off_in_page
- cur
+ 1, cur
);
4847 int try_release_extent_buffer(struct page
*page
, gfp_t mask
)
4849 struct extent_buffer
*eb
;
4852 * We need to make sure noboody is attaching this page to an eb right
4855 spin_lock(&page
->mapping
->private_lock
);
4856 if (!PagePrivate(page
)) {
4857 spin_unlock(&page
->mapping
->private_lock
);
4861 eb
= (struct extent_buffer
*)page
->private;
4865 * This is a little awful but should be ok, we need to make sure that
4866 * the eb doesn't disappear out from under us while we're looking at
4869 spin_lock(&eb
->refs_lock
);
4870 if (atomic_read(&eb
->refs
) != 1 || extent_buffer_under_io(eb
)) {
4871 spin_unlock(&eb
->refs_lock
);
4872 spin_unlock(&page
->mapping
->private_lock
);
4875 spin_unlock(&page
->mapping
->private_lock
);
4877 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
4881 * If tree ref isn't set then we know the ref on this eb is a real ref,
4882 * so just return, this page will likely be freed soon anyway.
4884 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
)) {
4885 spin_unlock(&eb
->refs_lock
);
4888 release_extent_buffer(eb
, mask
);