1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/module.h>
8 #include <linux/spinlock.h>
9 #include <linux/blkdev.h>
10 #include <linux/swap.h>
11 #include <linux/writeback.h>
12 #include <linux/pagevec.h>
13 #include <linux/prefetch.h>
14 #include <linux/cleancache.h>
15 #include "extent_io.h"
16 #include "extent_map.h"
19 #include "btrfs_inode.h"
21 #include "check-integrity.h"
23 #include "rcu-string.h"
25 static struct kmem_cache
*extent_state_cache
;
26 static struct kmem_cache
*extent_buffer_cache
;
28 static LIST_HEAD(buffers
);
29 static LIST_HEAD(states
);
33 static DEFINE_SPINLOCK(leak_lock
);
36 #define BUFFER_LRU_MAX 64
41 struct rb_node rb_node
;
44 struct extent_page_data
{
46 struct extent_io_tree
*tree
;
47 get_extent_t
*get_extent
;
48 unsigned long bio_flags
;
50 /* tells writepage not to lock the state bits for this range
51 * it still does the unlocking
53 unsigned int extent_locked
:1;
55 /* tells the submit_bio code to use a WRITE_SYNC */
56 unsigned int sync_io
:1;
59 static noinline
void flush_write_bio(void *data
);
60 static inline struct btrfs_fs_info
*
61 tree_fs_info(struct extent_io_tree
*tree
)
63 return btrfs_sb(tree
->mapping
->host
->i_sb
);
66 int __init
extent_io_init(void)
68 extent_state_cache
= kmem_cache_create("btrfs_extent_state",
69 sizeof(struct extent_state
), 0,
70 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
71 if (!extent_state_cache
)
74 extent_buffer_cache
= kmem_cache_create("btrfs_extent_buffer",
75 sizeof(struct extent_buffer
), 0,
76 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
77 if (!extent_buffer_cache
)
78 goto free_state_cache
;
82 kmem_cache_destroy(extent_state_cache
);
86 void extent_io_exit(void)
88 struct extent_state
*state
;
89 struct extent_buffer
*eb
;
91 while (!list_empty(&states
)) {
92 state
= list_entry(states
.next
, struct extent_state
, leak_list
);
93 printk(KERN_ERR
"btrfs state leak: start %llu end %llu "
94 "state %lu in tree %p refs %d\n",
95 (unsigned long long)state
->start
,
96 (unsigned long long)state
->end
,
97 state
->state
, state
->tree
, atomic_read(&state
->refs
));
98 list_del(&state
->leak_list
);
99 kmem_cache_free(extent_state_cache
, state
);
103 while (!list_empty(&buffers
)) {
104 eb
= list_entry(buffers
.next
, struct extent_buffer
, leak_list
);
105 printk(KERN_ERR
"btrfs buffer leak start %llu len %lu "
106 "refs %d\n", (unsigned long long)eb
->start
,
107 eb
->len
, atomic_read(&eb
->refs
));
108 list_del(&eb
->leak_list
);
109 kmem_cache_free(extent_buffer_cache
, eb
);
113 * Make sure all delayed rcu free are flushed before we
117 if (extent_state_cache
)
118 kmem_cache_destroy(extent_state_cache
);
119 if (extent_buffer_cache
)
120 kmem_cache_destroy(extent_buffer_cache
);
123 void extent_io_tree_init(struct extent_io_tree
*tree
,
124 struct address_space
*mapping
)
126 tree
->state
= RB_ROOT
;
127 INIT_RADIX_TREE(&tree
->buffer
, GFP_ATOMIC
);
129 tree
->dirty_bytes
= 0;
130 spin_lock_init(&tree
->lock
);
131 spin_lock_init(&tree
->buffer_lock
);
132 tree
->mapping
= mapping
;
135 static struct extent_state
*alloc_extent_state(gfp_t mask
)
137 struct extent_state
*state
;
142 state
= kmem_cache_alloc(extent_state_cache
, mask
);
149 spin_lock_irqsave(&leak_lock
, flags
);
150 list_add(&state
->leak_list
, &states
);
151 spin_unlock_irqrestore(&leak_lock
, flags
);
153 atomic_set(&state
->refs
, 1);
154 init_waitqueue_head(&state
->wq
);
155 trace_alloc_extent_state(state
, mask
, _RET_IP_
);
159 void free_extent_state(struct extent_state
*state
)
163 if (atomic_dec_and_test(&state
->refs
)) {
167 WARN_ON(state
->tree
);
169 spin_lock_irqsave(&leak_lock
, flags
);
170 list_del(&state
->leak_list
);
171 spin_unlock_irqrestore(&leak_lock
, flags
);
173 trace_free_extent_state(state
, _RET_IP_
);
174 kmem_cache_free(extent_state_cache
, state
);
178 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
179 struct rb_node
*node
)
181 struct rb_node
**p
= &root
->rb_node
;
182 struct rb_node
*parent
= NULL
;
183 struct tree_entry
*entry
;
187 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
189 if (offset
< entry
->start
)
191 else if (offset
> entry
->end
)
197 rb_link_node(node
, parent
, p
);
198 rb_insert_color(node
, root
);
202 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
203 struct rb_node
**prev_ret
,
204 struct rb_node
**next_ret
)
206 struct rb_root
*root
= &tree
->state
;
207 struct rb_node
*n
= root
->rb_node
;
208 struct rb_node
*prev
= NULL
;
209 struct rb_node
*orig_prev
= NULL
;
210 struct tree_entry
*entry
;
211 struct tree_entry
*prev_entry
= NULL
;
214 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
218 if (offset
< entry
->start
)
220 else if (offset
> entry
->end
)
228 while (prev
&& offset
> prev_entry
->end
) {
229 prev
= rb_next(prev
);
230 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
237 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
238 while (prev
&& offset
< prev_entry
->start
) {
239 prev
= rb_prev(prev
);
240 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
247 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
250 struct rb_node
*prev
= NULL
;
253 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
259 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
260 struct extent_state
*other
)
262 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
263 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
268 * utility function to look for merge candidates inside a given range.
269 * Any extents with matching state are merged together into a single
270 * extent in the tree. Extents with EXTENT_IO in their state field
271 * are not merged because the end_io handlers need to be able to do
272 * operations on them without sleeping (or doing allocations/splits).
274 * This should be called with the tree lock held.
276 static void merge_state(struct extent_io_tree
*tree
,
277 struct extent_state
*state
)
279 struct extent_state
*other
;
280 struct rb_node
*other_node
;
282 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
285 other_node
= rb_prev(&state
->rb_node
);
287 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
288 if (other
->end
== state
->start
- 1 &&
289 other
->state
== state
->state
) {
290 merge_cb(tree
, state
, other
);
291 state
->start
= other
->start
;
293 rb_erase(&other
->rb_node
, &tree
->state
);
294 free_extent_state(other
);
297 other_node
= rb_next(&state
->rb_node
);
299 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
300 if (other
->start
== state
->end
+ 1 &&
301 other
->state
== state
->state
) {
302 merge_cb(tree
, state
, other
);
303 state
->end
= other
->end
;
305 rb_erase(&other
->rb_node
, &tree
->state
);
306 free_extent_state(other
);
311 static void set_state_cb(struct extent_io_tree
*tree
,
312 struct extent_state
*state
, int *bits
)
314 if (tree
->ops
&& tree
->ops
->set_bit_hook
)
315 tree
->ops
->set_bit_hook(tree
->mapping
->host
, state
, bits
);
318 static void clear_state_cb(struct extent_io_tree
*tree
,
319 struct extent_state
*state
, int *bits
)
321 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
322 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
, bits
);
325 static void set_state_bits(struct extent_io_tree
*tree
,
326 struct extent_state
*state
, int *bits
);
329 * insert an extent_state struct into the tree. 'bits' are set on the
330 * struct before it is inserted.
332 * This may return -EEXIST if the extent is already there, in which case the
333 * state struct is freed.
335 * The tree lock is not taken internally. This is a utility function and
336 * probably isn't what you want to call (see set/clear_extent_bit).
338 static int insert_state(struct extent_io_tree
*tree
,
339 struct extent_state
*state
, u64 start
, u64 end
,
342 struct rb_node
*node
;
345 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
346 (unsigned long long)end
,
347 (unsigned long long)start
);
350 state
->start
= start
;
353 set_state_bits(tree
, state
, bits
);
355 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
357 struct extent_state
*found
;
358 found
= rb_entry(node
, struct extent_state
, rb_node
);
359 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
360 "%llu %llu\n", (unsigned long long)found
->start
,
361 (unsigned long long)found
->end
,
362 (unsigned long long)start
, (unsigned long long)end
);
366 merge_state(tree
, state
);
370 static void split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
373 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
374 tree
->ops
->split_extent_hook(tree
->mapping
->host
, orig
, split
);
378 * split a given extent state struct in two, inserting the preallocated
379 * struct 'prealloc' as the newly created second half. 'split' indicates an
380 * offset inside 'orig' where it should be split.
383 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
384 * are two extent state structs in the tree:
385 * prealloc: [orig->start, split - 1]
386 * orig: [ split, orig->end ]
388 * The tree locks are not taken by this function. They need to be held
391 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
392 struct extent_state
*prealloc
, u64 split
)
394 struct rb_node
*node
;
396 split_cb(tree
, orig
, split
);
398 prealloc
->start
= orig
->start
;
399 prealloc
->end
= split
- 1;
400 prealloc
->state
= orig
->state
;
403 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
405 free_extent_state(prealloc
);
408 prealloc
->tree
= tree
;
412 static struct extent_state
*next_state(struct extent_state
*state
)
414 struct rb_node
*next
= rb_next(&state
->rb_node
);
416 return rb_entry(next
, struct extent_state
, rb_node
);
422 * utility function to clear some bits in an extent state struct.
423 * it will optionally wake up any one waiting on this state (wake == 1).
425 * If no bits are set on the state struct after clearing things, the
426 * struct is freed and removed from the tree
428 static struct extent_state
*clear_state_bit(struct extent_io_tree
*tree
,
429 struct extent_state
*state
,
432 struct extent_state
*next
;
433 int bits_to_clear
= *bits
& ~EXTENT_CTLBITS
;
435 if ((bits_to_clear
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
436 u64 range
= state
->end
- state
->start
+ 1;
437 WARN_ON(range
> tree
->dirty_bytes
);
438 tree
->dirty_bytes
-= range
;
440 clear_state_cb(tree
, state
, bits
);
441 state
->state
&= ~bits_to_clear
;
444 if (state
->state
== 0) {
445 next
= next_state(state
);
447 rb_erase(&state
->rb_node
, &tree
->state
);
449 free_extent_state(state
);
454 merge_state(tree
, state
);
455 next
= next_state(state
);
460 static struct extent_state
*
461 alloc_extent_state_atomic(struct extent_state
*prealloc
)
464 prealloc
= alloc_extent_state(GFP_ATOMIC
);
469 void extent_io_tree_panic(struct extent_io_tree
*tree
, int err
)
471 btrfs_panic(tree_fs_info(tree
), err
, "Locking error: "
472 "Extent tree was modified by another "
473 "thread while locked.");
477 * clear some bits on a range in the tree. This may require splitting
478 * or inserting elements in the tree, so the gfp mask is used to
479 * indicate which allocations or sleeping are allowed.
481 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
482 * the given range from the tree regardless of state (ie for truncate).
484 * the range [start, end] is inclusive.
486 * This takes the tree lock, and returns 0 on success and < 0 on error.
488 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
489 int bits
, int wake
, int delete,
490 struct extent_state
**cached_state
,
493 struct extent_state
*state
;
494 struct extent_state
*cached
;
495 struct extent_state
*prealloc
= NULL
;
496 struct rb_node
*node
;
502 bits
|= ~EXTENT_CTLBITS
;
503 bits
|= EXTENT_FIRST_DELALLOC
;
505 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
508 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
509 prealloc
= alloc_extent_state(mask
);
514 spin_lock(&tree
->lock
);
516 cached
= *cached_state
;
519 *cached_state
= NULL
;
523 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
524 cached
->end
> start
) {
526 atomic_dec(&cached
->refs
);
531 free_extent_state(cached
);
534 * this search will find the extents that end after
537 node
= tree_search(tree
, start
);
540 state
= rb_entry(node
, struct extent_state
, rb_node
);
542 if (state
->start
> end
)
544 WARN_ON(state
->end
< start
);
545 last_end
= state
->end
;
547 /* the state doesn't have the wanted bits, go ahead */
548 if (!(state
->state
& bits
)) {
549 state
= next_state(state
);
554 * | ---- desired range ---- |
556 * | ------------- state -------------- |
558 * We need to split the extent we found, and may flip
559 * bits on second half.
561 * If the extent we found extends past our range, we
562 * just split and search again. It'll get split again
563 * the next time though.
565 * If the extent we found is inside our range, we clear
566 * the desired bit on it.
569 if (state
->start
< start
) {
570 prealloc
= alloc_extent_state_atomic(prealloc
);
572 err
= split_state(tree
, state
, prealloc
, start
);
574 extent_io_tree_panic(tree
, err
);
579 if (state
->end
<= end
) {
580 state
= clear_state_bit(tree
, state
, &bits
, wake
);
586 * | ---- desired range ---- |
588 * We need to split the extent, and clear the bit
591 if (state
->start
<= end
&& state
->end
> end
) {
592 prealloc
= alloc_extent_state_atomic(prealloc
);
594 err
= split_state(tree
, state
, prealloc
, end
+ 1);
596 extent_io_tree_panic(tree
, err
);
601 clear_state_bit(tree
, prealloc
, &bits
, wake
);
607 state
= clear_state_bit(tree
, state
, &bits
, wake
);
609 if (last_end
== (u64
)-1)
611 start
= last_end
+ 1;
612 if (start
<= end
&& state
&& !need_resched())
617 spin_unlock(&tree
->lock
);
619 free_extent_state(prealloc
);
626 spin_unlock(&tree
->lock
);
627 if (mask
& __GFP_WAIT
)
632 static void wait_on_state(struct extent_io_tree
*tree
,
633 struct extent_state
*state
)
634 __releases(tree
->lock
)
635 __acquires(tree
->lock
)
638 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
639 spin_unlock(&tree
->lock
);
641 spin_lock(&tree
->lock
);
642 finish_wait(&state
->wq
, &wait
);
646 * waits for one or more bits to clear on a range in the state tree.
647 * The range [start, end] is inclusive.
648 * The tree lock is taken by this function
650 void wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
652 struct extent_state
*state
;
653 struct rb_node
*node
;
655 spin_lock(&tree
->lock
);
659 * this search will find all the extents that end after
662 node
= tree_search(tree
, start
);
666 state
= rb_entry(node
, struct extent_state
, rb_node
);
668 if (state
->start
> end
)
671 if (state
->state
& bits
) {
672 start
= state
->start
;
673 atomic_inc(&state
->refs
);
674 wait_on_state(tree
, state
);
675 free_extent_state(state
);
678 start
= state
->end
+ 1;
683 cond_resched_lock(&tree
->lock
);
686 spin_unlock(&tree
->lock
);
689 static void set_state_bits(struct extent_io_tree
*tree
,
690 struct extent_state
*state
,
693 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
695 set_state_cb(tree
, state
, bits
);
696 if ((bits_to_set
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
697 u64 range
= state
->end
- state
->start
+ 1;
698 tree
->dirty_bytes
+= range
;
700 state
->state
|= bits_to_set
;
703 static void cache_state(struct extent_state
*state
,
704 struct extent_state
**cached_ptr
)
706 if (cached_ptr
&& !(*cached_ptr
)) {
707 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
709 atomic_inc(&state
->refs
);
714 static void uncache_state(struct extent_state
**cached_ptr
)
716 if (cached_ptr
&& (*cached_ptr
)) {
717 struct extent_state
*state
= *cached_ptr
;
719 free_extent_state(state
);
724 * set some bits on a range in the tree. This may require allocations or
725 * sleeping, so the gfp mask is used to indicate what is allowed.
727 * If any of the exclusive bits are set, this will fail with -EEXIST if some
728 * part of the range already has the desired bits set. The start of the
729 * existing range is returned in failed_start in this case.
731 * [start, end] is inclusive This takes the tree lock.
734 static int __must_check
735 __set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
736 int bits
, int exclusive_bits
, u64
*failed_start
,
737 struct extent_state
**cached_state
, gfp_t mask
)
739 struct extent_state
*state
;
740 struct extent_state
*prealloc
= NULL
;
741 struct rb_node
*node
;
746 bits
|= EXTENT_FIRST_DELALLOC
;
748 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
749 prealloc
= alloc_extent_state(mask
);
753 spin_lock(&tree
->lock
);
754 if (cached_state
&& *cached_state
) {
755 state
= *cached_state
;
756 if (state
->start
<= start
&& state
->end
> start
&&
758 node
= &state
->rb_node
;
763 * this search will find all the extents that end after
766 node
= tree_search(tree
, start
);
768 prealloc
= alloc_extent_state_atomic(prealloc
);
770 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
772 extent_io_tree_panic(tree
, err
);
777 state
= rb_entry(node
, struct extent_state
, rb_node
);
779 last_start
= state
->start
;
780 last_end
= state
->end
;
783 * | ---- desired range ---- |
786 * Just lock what we found and keep going
788 if (state
->start
== start
&& state
->end
<= end
) {
789 if (state
->state
& exclusive_bits
) {
790 *failed_start
= state
->start
;
795 set_state_bits(tree
, state
, &bits
);
796 cache_state(state
, cached_state
);
797 merge_state(tree
, state
);
798 if (last_end
== (u64
)-1)
800 start
= last_end
+ 1;
801 state
= next_state(state
);
802 if (start
< end
&& state
&& state
->start
== start
&&
809 * | ---- desired range ---- |
812 * | ------------- state -------------- |
814 * We need to split the extent we found, and may flip bits on
817 * If the extent we found extends past our
818 * range, we just split and search again. It'll get split
819 * again the next time though.
821 * If the extent we found is inside our range, we set the
824 if (state
->start
< start
) {
825 if (state
->state
& exclusive_bits
) {
826 *failed_start
= start
;
831 prealloc
= alloc_extent_state_atomic(prealloc
);
833 err
= split_state(tree
, state
, prealloc
, start
);
835 extent_io_tree_panic(tree
, err
);
840 if (state
->end
<= end
) {
841 set_state_bits(tree
, state
, &bits
);
842 cache_state(state
, cached_state
);
843 merge_state(tree
, state
);
844 if (last_end
== (u64
)-1)
846 start
= last_end
+ 1;
847 state
= next_state(state
);
848 if (start
< end
&& state
&& state
->start
== start
&&
855 * | ---- desired range ---- |
856 * | state | or | state |
858 * There's a hole, we need to insert something in it and
859 * ignore the extent we found.
861 if (state
->start
> start
) {
863 if (end
< last_start
)
866 this_end
= last_start
- 1;
868 prealloc
= alloc_extent_state_atomic(prealloc
);
872 * Avoid to free 'prealloc' if it can be merged with
875 err
= insert_state(tree
, prealloc
, start
, this_end
,
878 extent_io_tree_panic(tree
, err
);
880 cache_state(prealloc
, cached_state
);
882 start
= this_end
+ 1;
886 * | ---- desired range ---- |
888 * We need to split the extent, and set the bit
891 if (state
->start
<= end
&& state
->end
> end
) {
892 if (state
->state
& exclusive_bits
) {
893 *failed_start
= start
;
898 prealloc
= alloc_extent_state_atomic(prealloc
);
900 err
= split_state(tree
, state
, prealloc
, end
+ 1);
902 extent_io_tree_panic(tree
, err
);
904 set_state_bits(tree
, prealloc
, &bits
);
905 cache_state(prealloc
, cached_state
);
906 merge_state(tree
, prealloc
);
914 spin_unlock(&tree
->lock
);
916 free_extent_state(prealloc
);
923 spin_unlock(&tree
->lock
);
924 if (mask
& __GFP_WAIT
)
929 int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
,
930 u64
*failed_start
, struct extent_state
**cached_state
,
933 return __set_extent_bit(tree
, start
, end
, bits
, 0, failed_start
,
939 * convert_extent_bit - convert all bits in a given range from one bit to
941 * @tree: the io tree to search
942 * @start: the start offset in bytes
943 * @end: the end offset in bytes (inclusive)
944 * @bits: the bits to set in this range
945 * @clear_bits: the bits to clear in this range
946 * @cached_state: state that we're going to cache
947 * @mask: the allocation mask
949 * This will go through and set bits for the given range. If any states exist
950 * already in this range they are set with the given bit and cleared of the
951 * clear_bits. This is only meant to be used by things that are mergeable, ie
952 * converting from say DELALLOC to DIRTY. This is not meant to be used with
953 * boundary bits like LOCK.
955 int convert_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
956 int bits
, int clear_bits
,
957 struct extent_state
**cached_state
, gfp_t mask
)
959 struct extent_state
*state
;
960 struct extent_state
*prealloc
= NULL
;
961 struct rb_node
*node
;
967 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
968 prealloc
= alloc_extent_state(mask
);
973 spin_lock(&tree
->lock
);
974 if (cached_state
&& *cached_state
) {
975 state
= *cached_state
;
976 if (state
->start
<= start
&& state
->end
> start
&&
978 node
= &state
->rb_node
;
984 * this search will find all the extents that end after
987 node
= tree_search(tree
, start
);
989 prealloc
= alloc_extent_state_atomic(prealloc
);
994 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
997 extent_io_tree_panic(tree
, err
);
1000 state
= rb_entry(node
, struct extent_state
, rb_node
);
1002 last_start
= state
->start
;
1003 last_end
= state
->end
;
1006 * | ---- desired range ---- |
1009 * Just lock what we found and keep going
1011 if (state
->start
== start
&& state
->end
<= end
) {
1012 set_state_bits(tree
, state
, &bits
);
1013 cache_state(state
, cached_state
);
1014 state
= clear_state_bit(tree
, state
, &clear_bits
, 0);
1015 if (last_end
== (u64
)-1)
1017 start
= last_end
+ 1;
1018 if (start
< end
&& state
&& state
->start
== start
&&
1025 * | ---- desired range ---- |
1028 * | ------------- state -------------- |
1030 * We need to split the extent we found, and may flip bits on
1033 * If the extent we found extends past our
1034 * range, we just split and search again. It'll get split
1035 * again the next time though.
1037 * If the extent we found is inside our range, we set the
1038 * desired bit on it.
1040 if (state
->start
< start
) {
1041 prealloc
= alloc_extent_state_atomic(prealloc
);
1046 err
= split_state(tree
, state
, prealloc
, start
);
1048 extent_io_tree_panic(tree
, err
);
1052 if (state
->end
<= end
) {
1053 set_state_bits(tree
, state
, &bits
);
1054 cache_state(state
, cached_state
);
1055 state
= clear_state_bit(tree
, state
, &clear_bits
, 0);
1056 if (last_end
== (u64
)-1)
1058 start
= last_end
+ 1;
1059 if (start
< end
&& state
&& state
->start
== start
&&
1066 * | ---- desired range ---- |
1067 * | state | or | state |
1069 * There's a hole, we need to insert something in it and
1070 * ignore the extent we found.
1072 if (state
->start
> start
) {
1074 if (end
< last_start
)
1077 this_end
= last_start
- 1;
1079 prealloc
= alloc_extent_state_atomic(prealloc
);
1086 * Avoid to free 'prealloc' if it can be merged with
1089 err
= insert_state(tree
, prealloc
, start
, this_end
,
1092 extent_io_tree_panic(tree
, err
);
1093 cache_state(prealloc
, cached_state
);
1095 start
= this_end
+ 1;
1099 * | ---- desired range ---- |
1101 * We need to split the extent, and set the bit
1104 if (state
->start
<= end
&& state
->end
> end
) {
1105 prealloc
= alloc_extent_state_atomic(prealloc
);
1111 err
= split_state(tree
, state
, prealloc
, end
+ 1);
1113 extent_io_tree_panic(tree
, err
);
1115 set_state_bits(tree
, prealloc
, &bits
);
1116 cache_state(prealloc
, cached_state
);
1117 clear_state_bit(tree
, prealloc
, &clear_bits
, 0);
1125 spin_unlock(&tree
->lock
);
1127 free_extent_state(prealloc
);
1134 spin_unlock(&tree
->lock
);
1135 if (mask
& __GFP_WAIT
)
1140 /* wrappers around set/clear extent bit */
1141 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1144 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, NULL
,
1148 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1149 int bits
, gfp_t mask
)
1151 return set_extent_bit(tree
, start
, end
, bits
, NULL
,
1155 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1156 int bits
, gfp_t mask
)
1158 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
1161 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1162 struct extent_state
**cached_state
, gfp_t mask
)
1164 return set_extent_bit(tree
, start
, end
,
1165 EXTENT_DELALLOC
| EXTENT_UPTODATE
,
1166 NULL
, cached_state
, mask
);
1169 int set_extent_defrag(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
,
1173 EXTENT_DELALLOC
| EXTENT_UPTODATE
| EXTENT_DEFRAG
,
1174 NULL
, cached_state
, mask
);
1177 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1180 return clear_extent_bit(tree
, start
, end
,
1181 EXTENT_DIRTY
| EXTENT_DELALLOC
|
1182 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, mask
);
1185 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1188 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, NULL
,
1192 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1193 struct extent_state
**cached_state
, gfp_t mask
)
1195 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0,
1196 cached_state
, mask
);
1199 int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1200 struct extent_state
**cached_state
, gfp_t mask
)
1202 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
1203 cached_state
, mask
);
1207 * either insert or lock state struct between start and end use mask to tell
1208 * us if waiting is desired.
1210 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1211 int bits
, struct extent_state
**cached_state
)
1216 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
1217 EXTENT_LOCKED
, &failed_start
,
1218 cached_state
, GFP_NOFS
);
1219 if (err
== -EEXIST
) {
1220 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
1221 start
= failed_start
;
1224 WARN_ON(start
> end
);
1229 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1231 return lock_extent_bits(tree
, start
, end
, 0, NULL
);
1234 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1239 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1240 &failed_start
, NULL
, GFP_NOFS
);
1241 if (err
== -EEXIST
) {
1242 if (failed_start
> start
)
1243 clear_extent_bit(tree
, start
, failed_start
- 1,
1244 EXTENT_LOCKED
, 1, 0, NULL
, GFP_NOFS
);
1250 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1251 struct extent_state
**cached
, gfp_t mask
)
1253 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1257 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1259 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1264 * helper function to set both pages and extents in the tree writeback
1266 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1268 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1269 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1272 while (index
<= end_index
) {
1273 page
= find_get_page(tree
->mapping
, index
);
1274 BUG_ON(!page
); /* Pages should be in the extent_io_tree */
1275 set_page_writeback(page
);
1276 page_cache_release(page
);
1282 /* find the first state struct with 'bits' set after 'start', and
1283 * return it. tree->lock must be held. NULL will returned if
1284 * nothing was found after 'start'
1286 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1287 u64 start
, int bits
)
1289 struct rb_node
*node
;
1290 struct extent_state
*state
;
1293 * this search will find all the extents that end after
1296 node
= tree_search(tree
, start
);
1301 state
= rb_entry(node
, struct extent_state
, rb_node
);
1302 if (state
->end
>= start
&& (state
->state
& bits
))
1305 node
= rb_next(node
);
1314 * find the first offset in the io tree with 'bits' set. zero is
1315 * returned if we find something, and *start_ret and *end_ret are
1316 * set to reflect the state struct that was found.
1318 * If nothing was found, 1 is returned. If found something, return 0.
1320 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1321 u64
*start_ret
, u64
*end_ret
, int bits
,
1322 struct extent_state
**cached_state
)
1324 struct extent_state
*state
;
1328 spin_lock(&tree
->lock
);
1329 if (cached_state
&& *cached_state
) {
1330 state
= *cached_state
;
1331 if (state
->end
== start
- 1 && state
->tree
) {
1332 n
= rb_next(&state
->rb_node
);
1334 state
= rb_entry(n
, struct extent_state
,
1336 if (state
->state
& bits
)
1340 free_extent_state(*cached_state
);
1341 *cached_state
= NULL
;
1344 free_extent_state(*cached_state
);
1345 *cached_state
= NULL
;
1348 state
= find_first_extent_bit_state(tree
, start
, bits
);
1351 cache_state(state
, cached_state
);
1352 *start_ret
= state
->start
;
1353 *end_ret
= state
->end
;
1357 spin_unlock(&tree
->lock
);
1362 * find a contiguous range of bytes in the file marked as delalloc, not
1363 * more than 'max_bytes'. start and end are used to return the range,
1365 * 1 is returned if we find something, 0 if nothing was in the tree
1367 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1368 u64
*start
, u64
*end
, u64 max_bytes
,
1369 struct extent_state
**cached_state
)
1371 struct rb_node
*node
;
1372 struct extent_state
*state
;
1373 u64 cur_start
= *start
;
1375 u64 total_bytes
= 0;
1377 spin_lock(&tree
->lock
);
1380 * this search will find all the extents that end after
1383 node
= tree_search(tree
, cur_start
);
1391 state
= rb_entry(node
, struct extent_state
, rb_node
);
1392 if (found
&& (state
->start
!= cur_start
||
1393 (state
->state
& EXTENT_BOUNDARY
))) {
1396 if (!(state
->state
& EXTENT_DELALLOC
)) {
1402 *start
= state
->start
;
1403 *cached_state
= state
;
1404 atomic_inc(&state
->refs
);
1408 cur_start
= state
->end
+ 1;
1409 node
= rb_next(node
);
1412 total_bytes
+= state
->end
- state
->start
+ 1;
1413 if (total_bytes
>= max_bytes
)
1417 spin_unlock(&tree
->lock
);
1421 static noinline
void __unlock_for_delalloc(struct inode
*inode
,
1422 struct page
*locked_page
,
1426 struct page
*pages
[16];
1427 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1428 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1429 unsigned long nr_pages
= end_index
- index
+ 1;
1432 if (index
== locked_page
->index
&& end_index
== index
)
1435 while (nr_pages
> 0) {
1436 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1437 min_t(unsigned long, nr_pages
,
1438 ARRAY_SIZE(pages
)), pages
);
1439 for (i
= 0; i
< ret
; i
++) {
1440 if (pages
[i
] != locked_page
)
1441 unlock_page(pages
[i
]);
1442 page_cache_release(pages
[i
]);
1450 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1451 struct page
*locked_page
,
1455 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1456 unsigned long start_index
= index
;
1457 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1458 unsigned long pages_locked
= 0;
1459 struct page
*pages
[16];
1460 unsigned long nrpages
;
1464 /* the caller is responsible for locking the start index */
1465 if (index
== locked_page
->index
&& index
== end_index
)
1468 /* skip the page at the start index */
1469 nrpages
= end_index
- index
+ 1;
1470 while (nrpages
> 0) {
1471 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1472 min_t(unsigned long,
1473 nrpages
, ARRAY_SIZE(pages
)), pages
);
1478 /* now we have an array of pages, lock them all */
1479 for (i
= 0; i
< ret
; i
++) {
1481 * the caller is taking responsibility for
1484 if (pages
[i
] != locked_page
) {
1485 lock_page(pages
[i
]);
1486 if (!PageDirty(pages
[i
]) ||
1487 pages
[i
]->mapping
!= inode
->i_mapping
) {
1489 unlock_page(pages
[i
]);
1490 page_cache_release(pages
[i
]);
1494 page_cache_release(pages
[i
]);
1503 if (ret
&& pages_locked
) {
1504 __unlock_for_delalloc(inode
, locked_page
,
1506 ((u64
)(start_index
+ pages_locked
- 1)) <<
1513 * find a contiguous range of bytes in the file marked as delalloc, not
1514 * more than 'max_bytes'. start and end are used to return the range,
1516 * 1 is returned if we find something, 0 if nothing was in the tree
1518 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1519 struct extent_io_tree
*tree
,
1520 struct page
*locked_page
,
1521 u64
*start
, u64
*end
,
1527 struct extent_state
*cached_state
= NULL
;
1532 /* step one, find a bunch of delalloc bytes starting at start */
1533 delalloc_start
= *start
;
1535 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1536 max_bytes
, &cached_state
);
1537 if (!found
|| delalloc_end
<= *start
) {
1538 *start
= delalloc_start
;
1539 *end
= delalloc_end
;
1540 free_extent_state(cached_state
);
1545 * start comes from the offset of locked_page. We have to lock
1546 * pages in order, so we can't process delalloc bytes before
1549 if (delalloc_start
< *start
)
1550 delalloc_start
= *start
;
1553 * make sure to limit the number of pages we try to lock down
1556 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1557 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1559 /* step two, lock all the pages after the page that has start */
1560 ret
= lock_delalloc_pages(inode
, locked_page
,
1561 delalloc_start
, delalloc_end
);
1562 if (ret
== -EAGAIN
) {
1563 /* some of the pages are gone, lets avoid looping by
1564 * shortening the size of the delalloc range we're searching
1566 free_extent_state(cached_state
);
1568 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1569 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1577 BUG_ON(ret
); /* Only valid values are 0 and -EAGAIN */
1579 /* step three, lock the state bits for the whole range */
1580 lock_extent_bits(tree
, delalloc_start
, delalloc_end
, 0, &cached_state
);
1582 /* then test to make sure it is all still delalloc */
1583 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1584 EXTENT_DELALLOC
, 1, cached_state
);
1586 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1587 &cached_state
, GFP_NOFS
);
1588 __unlock_for_delalloc(inode
, locked_page
,
1589 delalloc_start
, delalloc_end
);
1593 free_extent_state(cached_state
);
1594 *start
= delalloc_start
;
1595 *end
= delalloc_end
;
1600 int extent_clear_unlock_delalloc(struct inode
*inode
,
1601 struct extent_io_tree
*tree
,
1602 u64 start
, u64 end
, struct page
*locked_page
,
1606 struct page
*pages
[16];
1607 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1608 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1609 unsigned long nr_pages
= end_index
- index
+ 1;
1613 if (op
& EXTENT_CLEAR_UNLOCK
)
1614 clear_bits
|= EXTENT_LOCKED
;
1615 if (op
& EXTENT_CLEAR_DIRTY
)
1616 clear_bits
|= EXTENT_DIRTY
;
1618 if (op
& EXTENT_CLEAR_DELALLOC
)
1619 clear_bits
|= EXTENT_DELALLOC
;
1621 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1622 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1623 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1624 EXTENT_SET_PRIVATE2
)))
1627 while (nr_pages
> 0) {
1628 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1629 min_t(unsigned long,
1630 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1631 for (i
= 0; i
< ret
; i
++) {
1633 if (op
& EXTENT_SET_PRIVATE2
)
1634 SetPagePrivate2(pages
[i
]);
1636 if (pages
[i
] == locked_page
) {
1637 page_cache_release(pages
[i
]);
1640 if (op
& EXTENT_CLEAR_DIRTY
)
1641 clear_page_dirty_for_io(pages
[i
]);
1642 if (op
& EXTENT_SET_WRITEBACK
)
1643 set_page_writeback(pages
[i
]);
1644 if (op
& EXTENT_END_WRITEBACK
)
1645 end_page_writeback(pages
[i
]);
1646 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1647 unlock_page(pages
[i
]);
1648 page_cache_release(pages
[i
]);
1658 * count the number of bytes in the tree that have a given bit(s)
1659 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1660 * cached. The total number found is returned.
1662 u64
count_range_bits(struct extent_io_tree
*tree
,
1663 u64
*start
, u64 search_end
, u64 max_bytes
,
1664 unsigned long bits
, int contig
)
1666 struct rb_node
*node
;
1667 struct extent_state
*state
;
1668 u64 cur_start
= *start
;
1669 u64 total_bytes
= 0;
1673 if (search_end
<= cur_start
) {
1678 spin_lock(&tree
->lock
);
1679 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1680 total_bytes
= tree
->dirty_bytes
;
1684 * this search will find all the extents that end after
1687 node
= tree_search(tree
, cur_start
);
1692 state
= rb_entry(node
, struct extent_state
, rb_node
);
1693 if (state
->start
> search_end
)
1695 if (contig
&& found
&& state
->start
> last
+ 1)
1697 if (state
->end
>= cur_start
&& (state
->state
& bits
) == bits
) {
1698 total_bytes
+= min(search_end
, state
->end
) + 1 -
1699 max(cur_start
, state
->start
);
1700 if (total_bytes
>= max_bytes
)
1703 *start
= max(cur_start
, state
->start
);
1707 } else if (contig
&& found
) {
1710 node
= rb_next(node
);
1715 spin_unlock(&tree
->lock
);
1720 * set the private field for a given byte offset in the tree. If there isn't
1721 * an extent_state there already, this does nothing.
1723 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1725 struct rb_node
*node
;
1726 struct extent_state
*state
;
1729 spin_lock(&tree
->lock
);
1731 * this search will find all the extents that end after
1734 node
= tree_search(tree
, start
);
1739 state
= rb_entry(node
, struct extent_state
, rb_node
);
1740 if (state
->start
!= start
) {
1744 state
->private = private;
1746 spin_unlock(&tree
->lock
);
1750 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1752 struct rb_node
*node
;
1753 struct extent_state
*state
;
1756 spin_lock(&tree
->lock
);
1758 * this search will find all the extents that end after
1761 node
= tree_search(tree
, start
);
1766 state
= rb_entry(node
, struct extent_state
, rb_node
);
1767 if (state
->start
!= start
) {
1771 *private = state
->private;
1773 spin_unlock(&tree
->lock
);
1778 * searches a range in the state tree for a given mask.
1779 * If 'filled' == 1, this returns 1 only if every extent in the tree
1780 * has the bits set. Otherwise, 1 is returned if any bit in the
1781 * range is found set.
1783 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1784 int bits
, int filled
, struct extent_state
*cached
)
1786 struct extent_state
*state
= NULL
;
1787 struct rb_node
*node
;
1790 spin_lock(&tree
->lock
);
1791 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
1792 cached
->end
> start
)
1793 node
= &cached
->rb_node
;
1795 node
= tree_search(tree
, start
);
1796 while (node
&& start
<= end
) {
1797 state
= rb_entry(node
, struct extent_state
, rb_node
);
1799 if (filled
&& state
->start
> start
) {
1804 if (state
->start
> end
)
1807 if (state
->state
& bits
) {
1811 } else if (filled
) {
1816 if (state
->end
== (u64
)-1)
1819 start
= state
->end
+ 1;
1822 node
= rb_next(node
);
1829 spin_unlock(&tree
->lock
);
1834 * helper function to set a given page up to date if all the
1835 * extents in the tree for that page are up to date
1837 static void check_page_uptodate(struct extent_io_tree
*tree
, struct page
*page
)
1839 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1840 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1841 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1842 SetPageUptodate(page
);
1846 * helper function to unlock a page if all the extents in the tree
1847 * for that page are unlocked
1849 static void check_page_locked(struct extent_io_tree
*tree
, struct page
*page
)
1851 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1852 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1853 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1858 * helper function to end page writeback if all the extents
1859 * in the tree for that page are done with writeback
1861 static void check_page_writeback(struct extent_io_tree
*tree
,
1864 end_page_writeback(page
);
1868 * When IO fails, either with EIO or csum verification fails, we
1869 * try other mirrors that might have a good copy of the data. This
1870 * io_failure_record is used to record state as we go through all the
1871 * mirrors. If another mirror has good data, the page is set up to date
1872 * and things continue. If a good mirror can't be found, the original
1873 * bio end_io callback is called to indicate things have failed.
1875 struct io_failure_record
{
1880 unsigned long bio_flags
;
1886 static int free_io_failure(struct inode
*inode
, struct io_failure_record
*rec
,
1891 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1893 set_state_private(failure_tree
, rec
->start
, 0);
1894 ret
= clear_extent_bits(failure_tree
, rec
->start
,
1895 rec
->start
+ rec
->len
- 1,
1896 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1901 ret
= clear_extent_bits(&BTRFS_I(inode
)->io_tree
, rec
->start
,
1902 rec
->start
+ rec
->len
- 1,
1903 EXTENT_DAMAGED
, GFP_NOFS
);
1912 static void repair_io_failure_callback(struct bio
*bio
, int err
)
1914 complete(bio
->bi_private
);
1918 * this bypasses the standard btrfs submit functions deliberately, as
1919 * the standard behavior is to write all copies in a raid setup. here we only
1920 * want to write the one bad copy. so we do the mapping for ourselves and issue
1921 * submit_bio directly.
1922 * to avoid any synchonization issues, wait for the data after writing, which
1923 * actually prevents the read that triggered the error from finishing.
1924 * currently, there can be no more than two copies of every data bit. thus,
1925 * exactly one rewrite is required.
1927 int repair_io_failure(struct btrfs_mapping_tree
*map_tree
, u64 start
,
1928 u64 length
, u64 logical
, struct page
*page
,
1932 struct btrfs_device
*dev
;
1933 DECLARE_COMPLETION_ONSTACK(compl);
1936 struct btrfs_bio
*bbio
= NULL
;
1939 BUG_ON(!mirror_num
);
1941 bio
= bio_alloc(GFP_NOFS
, 1);
1944 bio
->bi_private
= &compl;
1945 bio
->bi_end_io
= repair_io_failure_callback
;
1947 map_length
= length
;
1949 ret
= btrfs_map_block(map_tree
, WRITE
, logical
,
1950 &map_length
, &bbio
, mirror_num
);
1955 BUG_ON(mirror_num
!= bbio
->mirror_num
);
1956 sector
= bbio
->stripes
[mirror_num
-1].physical
>> 9;
1957 bio
->bi_sector
= sector
;
1958 dev
= bbio
->stripes
[mirror_num
-1].dev
;
1960 if (!dev
|| !dev
->bdev
|| !dev
->writeable
) {
1964 bio
->bi_bdev
= dev
->bdev
;
1965 bio_add_page(bio
, page
, length
, start
-page_offset(page
));
1966 btrfsic_submit_bio(WRITE_SYNC
, bio
);
1967 wait_for_completion(&compl);
1969 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
1970 /* try to remap that extent elsewhere? */
1972 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_WRITE_ERRS
);
1976 printk_ratelimited_in_rcu(KERN_INFO
"btrfs read error corrected: ino %lu off %llu "
1977 "(dev %s sector %llu)\n", page
->mapping
->host
->i_ino
,
1978 start
, rcu_str_deref(dev
->name
), sector
);
1984 int repair_eb_io_failure(struct btrfs_root
*root
, struct extent_buffer
*eb
,
1987 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1988 u64 start
= eb
->start
;
1989 unsigned long i
, num_pages
= num_extent_pages(eb
->start
, eb
->len
);
1992 for (i
= 0; i
< num_pages
; i
++) {
1993 struct page
*p
= extent_buffer_page(eb
, i
);
1994 ret
= repair_io_failure(map_tree
, start
, PAGE_CACHE_SIZE
,
1995 start
, p
, mirror_num
);
1998 start
+= PAGE_CACHE_SIZE
;
2005 * each time an IO finishes, we do a fast check in the IO failure tree
2006 * to see if we need to process or clean up an io_failure_record
2008 static int clean_io_failure(u64 start
, struct page
*page
)
2011 u64 private_failure
;
2012 struct io_failure_record
*failrec
;
2013 struct btrfs_mapping_tree
*map_tree
;
2014 struct extent_state
*state
;
2018 struct inode
*inode
= page
->mapping
->host
;
2021 ret
= count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
2022 (u64
)-1, 1, EXTENT_DIRTY
, 0);
2026 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
, start
,
2031 failrec
= (struct io_failure_record
*)(unsigned long) private_failure
;
2032 BUG_ON(!failrec
->this_mirror
);
2034 if (failrec
->in_validation
) {
2035 /* there was no real error, just free the record */
2036 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2042 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
2043 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
2046 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
2048 if (state
&& state
->start
== failrec
->start
) {
2049 map_tree
= &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
;
2050 num_copies
= btrfs_num_copies(map_tree
, failrec
->logical
,
2052 if (num_copies
> 1) {
2053 ret
= repair_io_failure(map_tree
, start
, failrec
->len
,
2054 failrec
->logical
, page
,
2055 failrec
->failed_mirror
);
2062 ret
= free_io_failure(inode
, failrec
, did_repair
);
2068 * this is a generic handler for readpage errors (default
2069 * readpage_io_failed_hook). if other copies exist, read those and write back
2070 * good data to the failed position. does not investigate in remapping the
2071 * failed extent elsewhere, hoping the device will be smart enough to do this as
2075 static int bio_readpage_error(struct bio
*failed_bio
, struct page
*page
,
2076 u64 start
, u64 end
, int failed_mirror
,
2077 struct extent_state
*state
)
2079 struct io_failure_record
*failrec
= NULL
;
2081 struct extent_map
*em
;
2082 struct inode
*inode
= page
->mapping
->host
;
2083 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
2084 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2085 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2092 BUG_ON(failed_bio
->bi_rw
& REQ_WRITE
);
2094 ret
= get_state_private(failure_tree
, start
, &private);
2096 failrec
= kzalloc(sizeof(*failrec
), GFP_NOFS
);
2099 failrec
->start
= start
;
2100 failrec
->len
= end
- start
+ 1;
2101 failrec
->this_mirror
= 0;
2102 failrec
->bio_flags
= 0;
2103 failrec
->in_validation
= 0;
2105 read_lock(&em_tree
->lock
);
2106 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
2108 read_unlock(&em_tree
->lock
);
2113 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
2114 free_extent_map(em
);
2117 read_unlock(&em_tree
->lock
);
2123 logical
= start
- em
->start
;
2124 logical
= em
->block_start
+ logical
;
2125 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2126 logical
= em
->block_start
;
2127 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
2128 extent_set_compress_type(&failrec
->bio_flags
,
2131 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2132 "len=%llu\n", logical
, start
, failrec
->len
);
2133 failrec
->logical
= logical
;
2134 free_extent_map(em
);
2136 /* set the bits in the private failure tree */
2137 ret
= set_extent_bits(failure_tree
, start
, end
,
2138 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
2140 ret
= set_state_private(failure_tree
, start
,
2141 (u64
)(unsigned long)failrec
);
2142 /* set the bits in the inode's tree */
2144 ret
= set_extent_bits(tree
, start
, end
, EXTENT_DAMAGED
,
2151 failrec
= (struct io_failure_record
*)(unsigned long)private;
2152 pr_debug("bio_readpage_error: (found) logical=%llu, "
2153 "start=%llu, len=%llu, validation=%d\n",
2154 failrec
->logical
, failrec
->start
, failrec
->len
,
2155 failrec
->in_validation
);
2157 * when data can be on disk more than twice, add to failrec here
2158 * (e.g. with a list for failed_mirror) to make
2159 * clean_io_failure() clean all those errors at once.
2162 num_copies
= btrfs_num_copies(
2163 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
2164 failrec
->logical
, failrec
->len
);
2165 if (num_copies
== 1) {
2167 * we only have a single copy of the data, so don't bother with
2168 * all the retry and error correction code that follows. no
2169 * matter what the error is, it is very likely to persist.
2171 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2172 "state=%p, num_copies=%d, next_mirror %d, "
2173 "failed_mirror %d\n", state
, num_copies
,
2174 failrec
->this_mirror
, failed_mirror
);
2175 free_io_failure(inode
, failrec
, 0);
2180 spin_lock(&tree
->lock
);
2181 state
= find_first_extent_bit_state(tree
, failrec
->start
,
2183 if (state
&& state
->start
!= failrec
->start
)
2185 spin_unlock(&tree
->lock
);
2189 * there are two premises:
2190 * a) deliver good data to the caller
2191 * b) correct the bad sectors on disk
2193 if (failed_bio
->bi_vcnt
> 1) {
2195 * to fulfill b), we need to know the exact failing sectors, as
2196 * we don't want to rewrite any more than the failed ones. thus,
2197 * we need separate read requests for the failed bio
2199 * if the following BUG_ON triggers, our validation request got
2200 * merged. we need separate requests for our algorithm to work.
2202 BUG_ON(failrec
->in_validation
);
2203 failrec
->in_validation
= 1;
2204 failrec
->this_mirror
= failed_mirror
;
2205 read_mode
= READ_SYNC
| REQ_FAILFAST_DEV
;
2208 * we're ready to fulfill a) and b) alongside. get a good copy
2209 * of the failed sector and if we succeed, we have setup
2210 * everything for repair_io_failure to do the rest for us.
2212 if (failrec
->in_validation
) {
2213 BUG_ON(failrec
->this_mirror
!= failed_mirror
);
2214 failrec
->in_validation
= 0;
2215 failrec
->this_mirror
= 0;
2217 failrec
->failed_mirror
= failed_mirror
;
2218 failrec
->this_mirror
++;
2219 if (failrec
->this_mirror
== failed_mirror
)
2220 failrec
->this_mirror
++;
2221 read_mode
= READ_SYNC
;
2224 if (!state
|| failrec
->this_mirror
> num_copies
) {
2225 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2226 "next_mirror %d, failed_mirror %d\n", state
,
2227 num_copies
, failrec
->this_mirror
, failed_mirror
);
2228 free_io_failure(inode
, failrec
, 0);
2232 bio
= bio_alloc(GFP_NOFS
, 1);
2234 free_io_failure(inode
, failrec
, 0);
2237 bio
->bi_private
= state
;
2238 bio
->bi_end_io
= failed_bio
->bi_end_io
;
2239 bio
->bi_sector
= failrec
->logical
>> 9;
2240 bio
->bi_bdev
= BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
;
2243 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
2245 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2246 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode
,
2247 failrec
->this_mirror
, num_copies
, failrec
->in_validation
);
2249 ret
= tree
->ops
->submit_bio_hook(inode
, read_mode
, bio
,
2250 failrec
->this_mirror
,
2251 failrec
->bio_flags
, 0);
2255 /* lots and lots of room for performance fixes in the end_bio funcs */
2257 int end_extent_writepage(struct page
*page
, int err
, u64 start
, u64 end
)
2259 int uptodate
= (err
== 0);
2260 struct extent_io_tree
*tree
;
2263 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2265 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
2266 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
2267 end
, NULL
, uptodate
);
2273 ClearPageUptodate(page
);
2280 * after a writepage IO is done, we need to:
2281 * clear the uptodate bits on error
2282 * clear the writeback bits in the extent tree for this IO
2283 * end_page_writeback if the page has no more pending IO
2285 * Scheduling is not allowed, so the extent state tree is expected
2286 * to have one and only one object corresponding to this IO.
2288 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
2290 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2291 struct extent_io_tree
*tree
;
2297 struct page
*page
= bvec
->bv_page
;
2298 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2300 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
2302 end
= start
+ bvec
->bv_len
- 1;
2304 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2309 if (--bvec
>= bio
->bi_io_vec
)
2310 prefetchw(&bvec
->bv_page
->flags
);
2312 if (end_extent_writepage(page
, err
, start
, end
))
2316 end_page_writeback(page
);
2318 check_page_writeback(tree
, page
);
2319 } while (bvec
>= bio
->bi_io_vec
);
2325 * after a readpage IO is done, we need to:
2326 * clear the uptodate bits on error
2327 * set the uptodate bits if things worked
2328 * set the page up to date if all extents in the tree are uptodate
2329 * clear the lock bit in the extent tree
2330 * unlock the page if there are no other extents locked for it
2332 * Scheduling is not allowed, so the extent state tree is expected
2333 * to have one and only one object corresponding to this IO.
2335 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
2337 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2338 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2339 struct bio_vec
*bvec
= bio
->bi_io_vec
;
2340 struct extent_io_tree
*tree
;
2351 struct page
*page
= bvec
->bv_page
;
2352 struct extent_state
*cached
= NULL
;
2353 struct extent_state
*state
;
2355 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2356 "mirror=%ld\n", (u64
)bio
->bi_sector
, err
,
2357 (long int)bio
->bi_bdev
);
2358 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2360 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
2362 end
= start
+ bvec
->bv_len
- 1;
2364 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2369 if (++bvec
<= bvec_end
)
2370 prefetchw(&bvec
->bv_page
->flags
);
2372 spin_lock(&tree
->lock
);
2373 state
= find_first_extent_bit_state(tree
, start
, EXTENT_LOCKED
);
2374 if (state
&& state
->start
== start
) {
2376 * take a reference on the state, unlock will drop
2379 cache_state(state
, &cached
);
2381 spin_unlock(&tree
->lock
);
2383 mirror
= (int)(unsigned long)bio
->bi_bdev
;
2384 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
2385 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
2390 clean_io_failure(start
, page
);
2393 if (!uptodate
&& tree
->ops
&& tree
->ops
->readpage_io_failed_hook
) {
2394 ret
= tree
->ops
->readpage_io_failed_hook(page
, mirror
);
2396 test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
2398 } else if (!uptodate
) {
2400 * The generic bio_readpage_error handles errors the
2401 * following way: If possible, new read requests are
2402 * created and submitted and will end up in
2403 * end_bio_extent_readpage as well (if we're lucky, not
2404 * in the !uptodate case). In that case it returns 0 and
2405 * we just go on with the next page in our bio. If it
2406 * can't handle the error it will return -EIO and we
2407 * remain responsible for that page.
2409 ret
= bio_readpage_error(bio
, page
, start
, end
, mirror
, NULL
);
2412 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2415 uncache_state(&cached
);
2420 if (uptodate
&& tree
->track_uptodate
) {
2421 set_extent_uptodate(tree
, start
, end
, &cached
,
2424 unlock_extent_cached(tree
, start
, end
, &cached
, GFP_ATOMIC
);
2428 SetPageUptodate(page
);
2430 ClearPageUptodate(page
);
2436 check_page_uptodate(tree
, page
);
2438 ClearPageUptodate(page
);
2441 check_page_locked(tree
, page
);
2443 } while (bvec
<= bvec_end
);
2449 btrfs_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
2454 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2456 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
2457 while (!bio
&& (nr_vecs
/= 2))
2458 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2463 bio
->bi_bdev
= bdev
;
2464 bio
->bi_sector
= first_sector
;
2470 * Since writes are async, they will only return -ENOMEM.
2471 * Reads can return the full range of I/O error conditions.
2473 static int __must_check
submit_one_bio(int rw
, struct bio
*bio
,
2474 int mirror_num
, unsigned long bio_flags
)
2477 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2478 struct page
*page
= bvec
->bv_page
;
2479 struct extent_io_tree
*tree
= bio
->bi_private
;
2482 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
2484 bio
->bi_private
= NULL
;
2488 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
2489 ret
= tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
2490 mirror_num
, bio_flags
, start
);
2492 btrfsic_submit_bio(rw
, bio
);
2494 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
2500 static int merge_bio(struct extent_io_tree
*tree
, struct page
*page
,
2501 unsigned long offset
, size_t size
, struct bio
*bio
,
2502 unsigned long bio_flags
)
2505 if (tree
->ops
&& tree
->ops
->merge_bio_hook
)
2506 ret
= tree
->ops
->merge_bio_hook(page
, offset
, size
, bio
,
2513 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
2514 struct page
*page
, sector_t sector
,
2515 size_t size
, unsigned long offset
,
2516 struct block_device
*bdev
,
2517 struct bio
**bio_ret
,
2518 unsigned long max_pages
,
2519 bio_end_io_t end_io_func
,
2521 unsigned long prev_bio_flags
,
2522 unsigned long bio_flags
)
2528 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
2529 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
2530 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
2532 if (bio_ret
&& *bio_ret
) {
2535 contig
= bio
->bi_sector
== sector
;
2537 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
2540 if (prev_bio_flags
!= bio_flags
|| !contig
||
2541 merge_bio(tree
, page
, offset
, page_size
, bio
, bio_flags
) ||
2542 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
2543 ret
= submit_one_bio(rw
, bio
, mirror_num
,
2552 if (this_compressed
)
2555 nr
= bio_get_nr_vecs(bdev
);
2557 bio
= btrfs_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
2561 bio_add_page(bio
, page
, page_size
, offset
);
2562 bio
->bi_end_io
= end_io_func
;
2563 bio
->bi_private
= tree
;
2568 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
2573 void attach_extent_buffer_page(struct extent_buffer
*eb
, struct page
*page
)
2575 if (!PagePrivate(page
)) {
2576 SetPagePrivate(page
);
2577 page_cache_get(page
);
2578 set_page_private(page
, (unsigned long)eb
);
2580 WARN_ON(page
->private != (unsigned long)eb
);
2584 void set_page_extent_mapped(struct page
*page
)
2586 if (!PagePrivate(page
)) {
2587 SetPagePrivate(page
);
2588 page_cache_get(page
);
2589 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
2594 * basic readpage implementation. Locked extent state structs are inserted
2595 * into the tree that are removed when the IO is done (by the end_io
2597 * XXX JDM: This needs looking at to ensure proper page locking
2599 static int __extent_read_full_page(struct extent_io_tree
*tree
,
2601 get_extent_t
*get_extent
,
2602 struct bio
**bio
, int mirror_num
,
2603 unsigned long *bio_flags
)
2605 struct inode
*inode
= page
->mapping
->host
;
2606 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2607 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2611 u64 last_byte
= i_size_read(inode
);
2615 struct extent_map
*em
;
2616 struct block_device
*bdev
;
2617 struct btrfs_ordered_extent
*ordered
;
2620 size_t pg_offset
= 0;
2622 size_t disk_io_size
;
2623 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2624 unsigned long this_bio_flag
= 0;
2626 set_page_extent_mapped(page
);
2628 if (!PageUptodate(page
)) {
2629 if (cleancache_get_page(page
) == 0) {
2630 BUG_ON(blocksize
!= PAGE_SIZE
);
2637 lock_extent(tree
, start
, end
);
2638 ordered
= btrfs_lookup_ordered_extent(inode
, start
);
2641 unlock_extent(tree
, start
, end
);
2642 btrfs_start_ordered_extent(inode
, ordered
, 1);
2643 btrfs_put_ordered_extent(ordered
);
2646 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
2648 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
2651 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
2652 userpage
= kmap_atomic(page
);
2653 memset(userpage
+ zero_offset
, 0, iosize
);
2654 flush_dcache_page(page
);
2655 kunmap_atomic(userpage
);
2658 while (cur
<= end
) {
2659 if (cur
>= last_byte
) {
2661 struct extent_state
*cached
= NULL
;
2663 iosize
= PAGE_CACHE_SIZE
- pg_offset
;
2664 userpage
= kmap_atomic(page
);
2665 memset(userpage
+ pg_offset
, 0, iosize
);
2666 flush_dcache_page(page
);
2667 kunmap_atomic(userpage
);
2668 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2670 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2674 em
= get_extent(inode
, page
, pg_offset
, cur
,
2676 if (IS_ERR_OR_NULL(em
)) {
2678 unlock_extent(tree
, cur
, end
);
2681 extent_offset
= cur
- em
->start
;
2682 BUG_ON(extent_map_end(em
) <= cur
);
2685 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2686 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2687 extent_set_compress_type(&this_bio_flag
,
2691 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2692 cur_end
= min(extent_map_end(em
) - 1, end
);
2693 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2694 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2695 disk_io_size
= em
->block_len
;
2696 sector
= em
->block_start
>> 9;
2698 sector
= (em
->block_start
+ extent_offset
) >> 9;
2699 disk_io_size
= iosize
;
2702 block_start
= em
->block_start
;
2703 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2704 block_start
= EXTENT_MAP_HOLE
;
2705 free_extent_map(em
);
2708 /* we've found a hole, just zero and go on */
2709 if (block_start
== EXTENT_MAP_HOLE
) {
2711 struct extent_state
*cached
= NULL
;
2713 userpage
= kmap_atomic(page
);
2714 memset(userpage
+ pg_offset
, 0, iosize
);
2715 flush_dcache_page(page
);
2716 kunmap_atomic(userpage
);
2718 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2720 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2723 pg_offset
+= iosize
;
2726 /* the get_extent function already copied into the page */
2727 if (test_range_bit(tree
, cur
, cur_end
,
2728 EXTENT_UPTODATE
, 1, NULL
)) {
2729 check_page_uptodate(tree
, page
);
2730 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2732 pg_offset
+= iosize
;
2735 /* we have an inline extent but it didn't get marked up
2736 * to date. Error out
2738 if (block_start
== EXTENT_MAP_INLINE
) {
2740 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2742 pg_offset
+= iosize
;
2747 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2748 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2752 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2754 ret
= submit_extent_page(READ
, tree
, page
,
2755 sector
, disk_io_size
, pg_offset
,
2757 end_bio_extent_readpage
, mirror_num
,
2762 *bio_flags
= this_bio_flag
;
2767 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2770 pg_offset
+= iosize
;
2774 if (!PageError(page
))
2775 SetPageUptodate(page
);
2781 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2782 get_extent_t
*get_extent
, int mirror_num
)
2784 struct bio
*bio
= NULL
;
2785 unsigned long bio_flags
= 0;
2788 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, mirror_num
,
2791 ret
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
2795 static noinline
void update_nr_written(struct page
*page
,
2796 struct writeback_control
*wbc
,
2797 unsigned long nr_written
)
2799 wbc
->nr_to_write
-= nr_written
;
2800 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2801 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2802 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2806 * the writepage semantics are similar to regular writepage. extent
2807 * records are inserted to lock ranges in the tree, and as dirty areas
2808 * are found, they are marked writeback. Then the lock bits are removed
2809 * and the end_io handler clears the writeback ranges
2811 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2814 struct inode
*inode
= page
->mapping
->host
;
2815 struct extent_page_data
*epd
= data
;
2816 struct extent_io_tree
*tree
= epd
->tree
;
2817 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2819 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2823 u64 last_byte
= i_size_read(inode
);
2827 struct extent_state
*cached_state
= NULL
;
2828 struct extent_map
*em
;
2829 struct block_device
*bdev
;
2832 size_t pg_offset
= 0;
2834 loff_t i_size
= i_size_read(inode
);
2835 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2841 unsigned long nr_written
= 0;
2842 bool fill_delalloc
= true;
2844 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2845 write_flags
= WRITE_SYNC
;
2847 write_flags
= WRITE
;
2849 trace___extent_writepage(page
, inode
, wbc
);
2851 WARN_ON(!PageLocked(page
));
2853 ClearPageError(page
);
2855 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2856 if (page
->index
> end_index
||
2857 (page
->index
== end_index
&& !pg_offset
)) {
2858 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2863 if (page
->index
== end_index
) {
2866 userpage
= kmap_atomic(page
);
2867 memset(userpage
+ pg_offset
, 0,
2868 PAGE_CACHE_SIZE
- pg_offset
);
2869 kunmap_atomic(userpage
);
2870 flush_dcache_page(page
);
2874 set_page_extent_mapped(page
);
2876 if (!tree
->ops
|| !tree
->ops
->fill_delalloc
)
2877 fill_delalloc
= false;
2879 delalloc_start
= start
;
2882 if (!epd
->extent_locked
&& fill_delalloc
) {
2883 u64 delalloc_to_write
= 0;
2885 * make sure the wbc mapping index is at least updated
2888 update_nr_written(page
, wbc
, 0);
2890 while (delalloc_end
< page_end
) {
2891 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2896 if (nr_delalloc
== 0) {
2897 delalloc_start
= delalloc_end
+ 1;
2900 ret
= tree
->ops
->fill_delalloc(inode
, page
,
2905 /* File system has been set read-only */
2911 * delalloc_end is already one less than the total
2912 * length, so we don't subtract one from
2915 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2918 delalloc_start
= delalloc_end
+ 1;
2920 if (wbc
->nr_to_write
< delalloc_to_write
) {
2923 if (delalloc_to_write
< thresh
* 2)
2924 thresh
= delalloc_to_write
;
2925 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2929 /* did the fill delalloc function already unlock and start
2935 * we've unlocked the page, so we can't update
2936 * the mapping's writeback index, just update
2939 wbc
->nr_to_write
-= nr_written
;
2943 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2944 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2947 /* Fixup worker will requeue */
2949 wbc
->pages_skipped
++;
2951 redirty_page_for_writepage(wbc
, page
);
2952 update_nr_written(page
, wbc
, nr_written
);
2960 * we don't want to touch the inode after unlocking the page,
2961 * so we update the mapping writeback index now
2963 update_nr_written(page
, wbc
, nr_written
+ 1);
2966 if (last_byte
<= start
) {
2967 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2968 tree
->ops
->writepage_end_io_hook(page
, start
,
2973 blocksize
= inode
->i_sb
->s_blocksize
;
2975 while (cur
<= end
) {
2976 if (cur
>= last_byte
) {
2977 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2978 tree
->ops
->writepage_end_io_hook(page
, cur
,
2982 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2984 if (IS_ERR_OR_NULL(em
)) {
2989 extent_offset
= cur
- em
->start
;
2990 BUG_ON(extent_map_end(em
) <= cur
);
2992 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2993 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2994 sector
= (em
->block_start
+ extent_offset
) >> 9;
2996 block_start
= em
->block_start
;
2997 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2998 free_extent_map(em
);
3002 * compressed and inline extents are written through other
3005 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
3006 block_start
== EXTENT_MAP_INLINE
) {
3008 * end_io notification does not happen here for
3009 * compressed extents
3011 if (!compressed
&& tree
->ops
&&
3012 tree
->ops
->writepage_end_io_hook
)
3013 tree
->ops
->writepage_end_io_hook(page
, cur
,
3016 else if (compressed
) {
3017 /* we don't want to end_page_writeback on
3018 * a compressed extent. this happens
3025 pg_offset
+= iosize
;
3028 /* leave this out until we have a page_mkwrite call */
3029 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
3030 EXTENT_DIRTY
, 0, NULL
)) {
3032 pg_offset
+= iosize
;
3036 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
3037 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
3045 unsigned long max_nr
= end_index
+ 1;
3047 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
3048 if (!PageWriteback(page
)) {
3049 printk(KERN_ERR
"btrfs warning page %lu not "
3050 "writeback, cur %llu end %llu\n",
3051 page
->index
, (unsigned long long)cur
,
3052 (unsigned long long)end
);
3055 ret
= submit_extent_page(write_flags
, tree
, page
,
3056 sector
, iosize
, pg_offset
,
3057 bdev
, &epd
->bio
, max_nr
,
3058 end_bio_extent_writepage
,
3064 pg_offset
+= iosize
;
3069 /* make sure the mapping tag for page dirty gets cleared */
3070 set_page_writeback(page
);
3071 end_page_writeback(page
);
3077 /* drop our reference on any cached states */
3078 free_extent_state(cached_state
);
3082 static int eb_wait(void *word
)
3088 static void wait_on_extent_buffer_writeback(struct extent_buffer
*eb
)
3090 wait_on_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
, eb_wait
,
3091 TASK_UNINTERRUPTIBLE
);
3094 static int lock_extent_buffer_for_io(struct extent_buffer
*eb
,
3095 struct btrfs_fs_info
*fs_info
,
3096 struct extent_page_data
*epd
)
3098 unsigned long i
, num_pages
;
3102 if (!btrfs_try_tree_write_lock(eb
)) {
3104 flush_write_bio(epd
);
3105 btrfs_tree_lock(eb
);
3108 if (test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
)) {
3109 btrfs_tree_unlock(eb
);
3113 flush_write_bio(epd
);
3117 wait_on_extent_buffer_writeback(eb
);
3118 btrfs_tree_lock(eb
);
3119 if (!test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
))
3121 btrfs_tree_unlock(eb
);
3126 * We need to do this to prevent races in people who check if the eb is
3127 * under IO since we can end up having no IO bits set for a short period
3130 spin_lock(&eb
->refs_lock
);
3131 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3132 set_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3133 spin_unlock(&eb
->refs_lock
);
3134 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
3135 spin_lock(&fs_info
->delalloc_lock
);
3136 if (fs_info
->dirty_metadata_bytes
>= eb
->len
)
3137 fs_info
->dirty_metadata_bytes
-= eb
->len
;
3140 spin_unlock(&fs_info
->delalloc_lock
);
3143 spin_unlock(&eb
->refs_lock
);
3146 btrfs_tree_unlock(eb
);
3151 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3152 for (i
= 0; i
< num_pages
; i
++) {
3153 struct page
*p
= extent_buffer_page(eb
, i
);
3155 if (!trylock_page(p
)) {
3157 flush_write_bio(epd
);
3167 static void end_extent_buffer_writeback(struct extent_buffer
*eb
)
3169 clear_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3170 smp_mb__after_clear_bit();
3171 wake_up_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
);
3174 static void end_bio_extent_buffer_writepage(struct bio
*bio
, int err
)
3176 int uptodate
= err
== 0;
3177 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
3178 struct extent_buffer
*eb
;
3182 struct page
*page
= bvec
->bv_page
;
3185 eb
= (struct extent_buffer
*)page
->private;
3187 done
= atomic_dec_and_test(&eb
->io_pages
);
3189 if (!uptodate
|| test_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
)) {
3190 set_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3191 ClearPageUptodate(page
);
3195 end_page_writeback(page
);
3200 end_extent_buffer_writeback(eb
);
3201 } while (bvec
>= bio
->bi_io_vec
);
3207 static int write_one_eb(struct extent_buffer
*eb
,
3208 struct btrfs_fs_info
*fs_info
,
3209 struct writeback_control
*wbc
,
3210 struct extent_page_data
*epd
)
3212 struct block_device
*bdev
= fs_info
->fs_devices
->latest_bdev
;
3213 u64 offset
= eb
->start
;
3214 unsigned long i
, num_pages
;
3215 unsigned long bio_flags
= 0;
3216 int rw
= (epd
->sync_io
? WRITE_SYNC
: WRITE
);
3219 clear_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3220 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3221 atomic_set(&eb
->io_pages
, num_pages
);
3222 if (btrfs_header_owner(eb
) == BTRFS_TREE_LOG_OBJECTID
)
3223 bio_flags
= EXTENT_BIO_TREE_LOG
;
3225 for (i
= 0; i
< num_pages
; i
++) {
3226 struct page
*p
= extent_buffer_page(eb
, i
);
3228 clear_page_dirty_for_io(p
);
3229 set_page_writeback(p
);
3230 ret
= submit_extent_page(rw
, eb
->tree
, p
, offset
>> 9,
3231 PAGE_CACHE_SIZE
, 0, bdev
, &epd
->bio
,
3232 -1, end_bio_extent_buffer_writepage
,
3233 0, epd
->bio_flags
, bio_flags
);
3234 epd
->bio_flags
= bio_flags
;
3236 set_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3238 if (atomic_sub_and_test(num_pages
- i
, &eb
->io_pages
))
3239 end_extent_buffer_writeback(eb
);
3243 offset
+= PAGE_CACHE_SIZE
;
3244 update_nr_written(p
, wbc
, 1);
3248 if (unlikely(ret
)) {
3249 for (; i
< num_pages
; i
++) {
3250 struct page
*p
= extent_buffer_page(eb
, i
);
3258 int btree_write_cache_pages(struct address_space
*mapping
,
3259 struct writeback_control
*wbc
)
3261 struct extent_io_tree
*tree
= &BTRFS_I(mapping
->host
)->io_tree
;
3262 struct btrfs_fs_info
*fs_info
= BTRFS_I(mapping
->host
)->root
->fs_info
;
3263 struct extent_buffer
*eb
, *prev_eb
= NULL
;
3264 struct extent_page_data epd
= {
3268 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3273 int nr_to_write_done
= 0;
3274 struct pagevec pvec
;
3277 pgoff_t end
; /* Inclusive */
3281 pagevec_init(&pvec
, 0);
3282 if (wbc
->range_cyclic
) {
3283 index
= mapping
->writeback_index
; /* Start from prev offset */
3286 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3287 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3290 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3291 tag
= PAGECACHE_TAG_TOWRITE
;
3293 tag
= PAGECACHE_TAG_DIRTY
;
3295 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3296 tag_pages_for_writeback(mapping
, index
, end
);
3297 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3298 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3299 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3303 for (i
= 0; i
< nr_pages
; i
++) {
3304 struct page
*page
= pvec
.pages
[i
];
3306 if (!PagePrivate(page
))
3309 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3314 spin_lock(&mapping
->private_lock
);
3315 if (!PagePrivate(page
)) {
3316 spin_unlock(&mapping
->private_lock
);
3320 eb
= (struct extent_buffer
*)page
->private;
3323 * Shouldn't happen and normally this would be a BUG_ON
3324 * but no sense in crashing the users box for something
3325 * we can survive anyway.
3328 spin_unlock(&mapping
->private_lock
);
3333 if (eb
== prev_eb
) {
3334 spin_unlock(&mapping
->private_lock
);
3338 ret
= atomic_inc_not_zero(&eb
->refs
);
3339 spin_unlock(&mapping
->private_lock
);
3344 ret
= lock_extent_buffer_for_io(eb
, fs_info
, &epd
);
3346 free_extent_buffer(eb
);
3350 ret
= write_one_eb(eb
, fs_info
, wbc
, &epd
);
3353 free_extent_buffer(eb
);
3356 free_extent_buffer(eb
);
3359 * the filesystem may choose to bump up nr_to_write.
3360 * We have to make sure to honor the new nr_to_write
3363 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3365 pagevec_release(&pvec
);
3368 if (!scanned
&& !done
) {
3370 * We hit the last page and there is more work to be done: wrap
3371 * back to the start of the file
3377 flush_write_bio(&epd
);
3382 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3383 * @mapping: address space structure to write
3384 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3385 * @writepage: function called for each page
3386 * @data: data passed to writepage function
3388 * If a page is already under I/O, write_cache_pages() skips it, even
3389 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3390 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3391 * and msync() need to guarantee that all the data which was dirty at the time
3392 * the call was made get new I/O started against them. If wbc->sync_mode is
3393 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3394 * existing IO to complete.
3396 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
3397 struct address_space
*mapping
,
3398 struct writeback_control
*wbc
,
3399 writepage_t writepage
, void *data
,
3400 void (*flush_fn
)(void *))
3402 struct inode
*inode
= mapping
->host
;
3405 int nr_to_write_done
= 0;
3406 struct pagevec pvec
;
3409 pgoff_t end
; /* Inclusive */
3414 * We have to hold onto the inode so that ordered extents can do their
3415 * work when the IO finishes. The alternative to this is failing to add
3416 * an ordered extent if the igrab() fails there and that is a huge pain
3417 * to deal with, so instead just hold onto the inode throughout the
3418 * writepages operation. If it fails here we are freeing up the inode
3419 * anyway and we'd rather not waste our time writing out stuff that is
3420 * going to be truncated anyway.
3425 pagevec_init(&pvec
, 0);
3426 if (wbc
->range_cyclic
) {
3427 index
= mapping
->writeback_index
; /* Start from prev offset */
3430 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3431 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3434 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3435 tag
= PAGECACHE_TAG_TOWRITE
;
3437 tag
= PAGECACHE_TAG_DIRTY
;
3439 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3440 tag_pages_for_writeback(mapping
, index
, end
);
3441 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3442 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3443 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3447 for (i
= 0; i
< nr_pages
; i
++) {
3448 struct page
*page
= pvec
.pages
[i
];
3451 * At this point we hold neither mapping->tree_lock nor
3452 * lock on the page itself: the page may be truncated or
3453 * invalidated (changing page->mapping to NULL), or even
3454 * swizzled back from swapper_space to tmpfs file
3458 tree
->ops
->write_cache_pages_lock_hook
) {
3459 tree
->ops
->write_cache_pages_lock_hook(page
,
3462 if (!trylock_page(page
)) {
3468 if (unlikely(page
->mapping
!= mapping
)) {
3473 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3479 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3480 if (PageWriteback(page
))
3482 wait_on_page_writeback(page
);
3485 if (PageWriteback(page
) ||
3486 !clear_page_dirty_for_io(page
)) {
3491 ret
= (*writepage
)(page
, wbc
, data
);
3493 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
3501 * the filesystem may choose to bump up nr_to_write.
3502 * We have to make sure to honor the new nr_to_write
3505 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3507 pagevec_release(&pvec
);
3510 if (!scanned
&& !done
) {
3512 * We hit the last page and there is more work to be done: wrap
3513 * back to the start of the file
3519 btrfs_add_delayed_iput(inode
);
3523 static void flush_epd_write_bio(struct extent_page_data
*epd
)
3532 ret
= submit_one_bio(rw
, epd
->bio
, 0, epd
->bio_flags
);
3533 BUG_ON(ret
< 0); /* -ENOMEM */
3538 static noinline
void flush_write_bio(void *data
)
3540 struct extent_page_data
*epd
= data
;
3541 flush_epd_write_bio(epd
);
3544 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
3545 get_extent_t
*get_extent
,
3546 struct writeback_control
*wbc
)
3549 struct extent_page_data epd
= {
3552 .get_extent
= get_extent
,
3554 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3558 ret
= __extent_writepage(page
, wbc
, &epd
);
3560 flush_epd_write_bio(&epd
);
3564 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
3565 u64 start
, u64 end
, get_extent_t
*get_extent
,
3569 struct address_space
*mapping
= inode
->i_mapping
;
3571 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
3574 struct extent_page_data epd
= {
3577 .get_extent
= get_extent
,
3579 .sync_io
= mode
== WB_SYNC_ALL
,
3582 struct writeback_control wbc_writepages
= {
3584 .nr_to_write
= nr_pages
* 2,
3585 .range_start
= start
,
3586 .range_end
= end
+ 1,
3589 while (start
<= end
) {
3590 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
3591 if (clear_page_dirty_for_io(page
))
3592 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
3594 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
3595 tree
->ops
->writepage_end_io_hook(page
, start
,
3596 start
+ PAGE_CACHE_SIZE
- 1,
3600 page_cache_release(page
);
3601 start
+= PAGE_CACHE_SIZE
;
3604 flush_epd_write_bio(&epd
);
3608 int extent_writepages(struct extent_io_tree
*tree
,
3609 struct address_space
*mapping
,
3610 get_extent_t
*get_extent
,
3611 struct writeback_control
*wbc
)
3614 struct extent_page_data epd
= {
3617 .get_extent
= get_extent
,
3619 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3623 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
3624 __extent_writepage
, &epd
,
3626 flush_epd_write_bio(&epd
);
3630 int extent_readpages(struct extent_io_tree
*tree
,
3631 struct address_space
*mapping
,
3632 struct list_head
*pages
, unsigned nr_pages
,
3633 get_extent_t get_extent
)
3635 struct bio
*bio
= NULL
;
3637 unsigned long bio_flags
= 0;
3638 struct page
*pagepool
[16];
3643 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
3644 page
= list_entry(pages
->prev
, struct page
, lru
);
3646 prefetchw(&page
->flags
);
3647 list_del(&page
->lru
);
3648 if (add_to_page_cache_lru(page
, mapping
,
3649 page
->index
, GFP_NOFS
)) {
3650 page_cache_release(page
);
3654 pagepool
[nr
++] = page
;
3655 if (nr
< ARRAY_SIZE(pagepool
))
3657 for (i
= 0; i
< nr
; i
++) {
3658 __extent_read_full_page(tree
, pagepool
[i
], get_extent
,
3659 &bio
, 0, &bio_flags
);
3660 page_cache_release(pagepool
[i
]);
3664 for (i
= 0; i
< nr
; i
++) {
3665 __extent_read_full_page(tree
, pagepool
[i
], get_extent
,
3666 &bio
, 0, &bio_flags
);
3667 page_cache_release(pagepool
[i
]);
3670 BUG_ON(!list_empty(pages
));
3672 return submit_one_bio(READ
, bio
, 0, bio_flags
);
3677 * basic invalidatepage code, this waits on any locked or writeback
3678 * ranges corresponding to the page, and then deletes any extent state
3679 * records from the tree
3681 int extent_invalidatepage(struct extent_io_tree
*tree
,
3682 struct page
*page
, unsigned long offset
)
3684 struct extent_state
*cached_state
= NULL
;
3685 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
3686 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3687 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
3689 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
3693 lock_extent_bits(tree
, start
, end
, 0, &cached_state
);
3694 wait_on_page_writeback(page
);
3695 clear_extent_bit(tree
, start
, end
,
3696 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
3697 EXTENT_DO_ACCOUNTING
,
3698 1, 1, &cached_state
, GFP_NOFS
);
3703 * a helper for releasepage, this tests for areas of the page that
3704 * are locked or under IO and drops the related state bits if it is safe
3707 int try_release_extent_state(struct extent_map_tree
*map
,
3708 struct extent_io_tree
*tree
, struct page
*page
,
3711 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3712 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3715 if (test_range_bit(tree
, start
, end
,
3716 EXTENT_IOBITS
, 0, NULL
))
3719 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
3722 * at this point we can safely clear everything except the
3723 * locked bit and the nodatasum bit
3725 ret
= clear_extent_bit(tree
, start
, end
,
3726 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
3729 /* if clear_extent_bit failed for enomem reasons,
3730 * we can't allow the release to continue.
3741 * a helper for releasepage. As long as there are no locked extents
3742 * in the range corresponding to the page, both state records and extent
3743 * map records are removed
3745 int try_release_extent_mapping(struct extent_map_tree
*map
,
3746 struct extent_io_tree
*tree
, struct page
*page
,
3749 struct extent_map
*em
;
3750 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3751 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3753 if ((mask
& __GFP_WAIT
) &&
3754 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
3756 while (start
<= end
) {
3757 len
= end
- start
+ 1;
3758 write_lock(&map
->lock
);
3759 em
= lookup_extent_mapping(map
, start
, len
);
3761 write_unlock(&map
->lock
);
3764 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
3765 em
->start
!= start
) {
3766 write_unlock(&map
->lock
);
3767 free_extent_map(em
);
3770 if (!test_range_bit(tree
, em
->start
,
3771 extent_map_end(em
) - 1,
3772 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
3774 remove_extent_mapping(map
, em
);
3775 /* once for the rb tree */
3776 free_extent_map(em
);
3778 start
= extent_map_end(em
);
3779 write_unlock(&map
->lock
);
3782 free_extent_map(em
);
3785 return try_release_extent_state(map
, tree
, page
, mask
);
3789 * helper function for fiemap, which doesn't want to see any holes.
3790 * This maps until we find something past 'last'
3792 static struct extent_map
*get_extent_skip_holes(struct inode
*inode
,
3795 get_extent_t
*get_extent
)
3797 u64 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
3798 struct extent_map
*em
;
3805 len
= last
- offset
;
3808 len
= (len
+ sectorsize
- 1) & ~(sectorsize
- 1);
3809 em
= get_extent(inode
, NULL
, 0, offset
, len
, 0);
3810 if (IS_ERR_OR_NULL(em
))
3813 /* if this isn't a hole return it */
3814 if (!test_bit(EXTENT_FLAG_VACANCY
, &em
->flags
) &&
3815 em
->block_start
!= EXTENT_MAP_HOLE
) {
3819 /* this is a hole, advance to the next extent */
3820 offset
= extent_map_end(em
);
3821 free_extent_map(em
);
3828 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3829 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
3833 u64 max
= start
+ len
;
3837 u64 last_for_get_extent
= 0;
3839 u64 isize
= i_size_read(inode
);
3840 struct btrfs_key found_key
;
3841 struct extent_map
*em
= NULL
;
3842 struct extent_state
*cached_state
= NULL
;
3843 struct btrfs_path
*path
;
3844 struct btrfs_file_extent_item
*item
;
3849 unsigned long emflags
;
3854 path
= btrfs_alloc_path();
3857 path
->leave_spinning
= 1;
3859 start
= ALIGN(start
, BTRFS_I(inode
)->root
->sectorsize
);
3860 len
= ALIGN(len
, BTRFS_I(inode
)->root
->sectorsize
);
3863 * lookup the last file extent. We're not using i_size here
3864 * because there might be preallocation past i_size
3866 ret
= btrfs_lookup_file_extent(NULL
, BTRFS_I(inode
)->root
,
3867 path
, btrfs_ino(inode
), -1, 0);
3869 btrfs_free_path(path
);
3874 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3875 struct btrfs_file_extent_item
);
3876 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, path
->slots
[0]);
3877 found_type
= btrfs_key_type(&found_key
);
3879 /* No extents, but there might be delalloc bits */
3880 if (found_key
.objectid
!= btrfs_ino(inode
) ||
3881 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
3882 /* have to trust i_size as the end */
3884 last_for_get_extent
= isize
;
3887 * remember the start of the last extent. There are a
3888 * bunch of different factors that go into the length of the
3889 * extent, so its much less complex to remember where it started
3891 last
= found_key
.offset
;
3892 last_for_get_extent
= last
+ 1;
3894 btrfs_free_path(path
);
3897 * we might have some extents allocated but more delalloc past those
3898 * extents. so, we trust isize unless the start of the last extent is
3903 last_for_get_extent
= isize
;
3906 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
3909 em
= get_extent_skip_holes(inode
, start
, last_for_get_extent
,
3919 u64 offset_in_extent
;
3921 /* break if the extent we found is outside the range */
3922 if (em
->start
>= max
|| extent_map_end(em
) < off
)
3926 * get_extent may return an extent that starts before our
3927 * requested range. We have to make sure the ranges
3928 * we return to fiemap always move forward and don't
3929 * overlap, so adjust the offsets here
3931 em_start
= max(em
->start
, off
);
3934 * record the offset from the start of the extent
3935 * for adjusting the disk offset below
3937 offset_in_extent
= em_start
- em
->start
;
3938 em_end
= extent_map_end(em
);
3939 em_len
= em_end
- em_start
;
3940 emflags
= em
->flags
;
3945 * bump off for our next call to get_extent
3947 off
= extent_map_end(em
);
3951 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
3953 flags
|= FIEMAP_EXTENT_LAST
;
3954 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
3955 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
3956 FIEMAP_EXTENT_NOT_ALIGNED
);
3957 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
3958 flags
|= (FIEMAP_EXTENT_DELALLOC
|
3959 FIEMAP_EXTENT_UNKNOWN
);
3961 disko
= em
->block_start
+ offset_in_extent
;
3963 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
3964 flags
|= FIEMAP_EXTENT_ENCODED
;
3966 free_extent_map(em
);
3968 if ((em_start
>= last
) || em_len
== (u64
)-1 ||
3969 (last
== (u64
)-1 && isize
<= em_end
)) {
3970 flags
|= FIEMAP_EXTENT_LAST
;
3974 /* now scan forward to see if this is really the last extent. */
3975 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
,
3982 flags
|= FIEMAP_EXTENT_LAST
;
3985 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3991 free_extent_map(em
);
3993 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3994 &cached_state
, GFP_NOFS
);
3998 static void __free_extent_buffer(struct extent_buffer
*eb
)
4001 unsigned long flags
;
4002 spin_lock_irqsave(&leak_lock
, flags
);
4003 list_del(&eb
->leak_list
);
4004 spin_unlock_irqrestore(&leak_lock
, flags
);
4006 if (eb
->pages
&& eb
->pages
!= eb
->inline_pages
)
4008 kmem_cache_free(extent_buffer_cache
, eb
);
4011 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
4016 struct extent_buffer
*eb
= NULL
;
4018 unsigned long flags
;
4021 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
4028 rwlock_init(&eb
->lock
);
4029 atomic_set(&eb
->write_locks
, 0);
4030 atomic_set(&eb
->read_locks
, 0);
4031 atomic_set(&eb
->blocking_readers
, 0);
4032 atomic_set(&eb
->blocking_writers
, 0);
4033 atomic_set(&eb
->spinning_readers
, 0);
4034 atomic_set(&eb
->spinning_writers
, 0);
4035 eb
->lock_nested
= 0;
4036 init_waitqueue_head(&eb
->write_lock_wq
);
4037 init_waitqueue_head(&eb
->read_lock_wq
);
4040 spin_lock_irqsave(&leak_lock
, flags
);
4041 list_add(&eb
->leak_list
, &buffers
);
4042 spin_unlock_irqrestore(&leak_lock
, flags
);
4044 spin_lock_init(&eb
->refs_lock
);
4045 atomic_set(&eb
->refs
, 1);
4046 atomic_set(&eb
->io_pages
, 0);
4048 if (len
> MAX_INLINE_EXTENT_BUFFER_SIZE
) {
4049 struct page
**pages
;
4050 int num_pages
= (len
+ PAGE_CACHE_SIZE
- 1) >>
4052 pages
= kzalloc(num_pages
, mask
);
4054 __free_extent_buffer(eb
);
4059 eb
->pages
= eb
->inline_pages
;
4065 struct extent_buffer
*btrfs_clone_extent_buffer(struct extent_buffer
*src
)
4069 struct extent_buffer
*new;
4070 unsigned long num_pages
= num_extent_pages(src
->start
, src
->len
);
4072 new = __alloc_extent_buffer(NULL
, src
->start
, src
->len
, GFP_ATOMIC
);
4076 for (i
= 0; i
< num_pages
; i
++) {
4077 p
= alloc_page(GFP_ATOMIC
);
4079 attach_extent_buffer_page(new, p
);
4080 WARN_ON(PageDirty(p
));
4085 copy_extent_buffer(new, src
, 0, 0, src
->len
);
4086 set_bit(EXTENT_BUFFER_UPTODATE
, &new->bflags
);
4087 set_bit(EXTENT_BUFFER_DUMMY
, &new->bflags
);
4092 struct extent_buffer
*alloc_dummy_extent_buffer(u64 start
, unsigned long len
)
4094 struct extent_buffer
*eb
;
4095 unsigned long num_pages
= num_extent_pages(0, len
);
4098 eb
= __alloc_extent_buffer(NULL
, start
, len
, GFP_ATOMIC
);
4102 for (i
= 0; i
< num_pages
; i
++) {
4103 eb
->pages
[i
] = alloc_page(GFP_ATOMIC
);
4107 set_extent_buffer_uptodate(eb
);
4108 btrfs_set_header_nritems(eb
, 0);
4109 set_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
);
4113 for (i
--; i
>= 0; i
--)
4114 __free_page(eb
->pages
[i
]);
4115 __free_extent_buffer(eb
);
4119 static int extent_buffer_under_io(struct extent_buffer
*eb
)
4121 return (atomic_read(&eb
->io_pages
) ||
4122 test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
) ||
4123 test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4127 * Helper for releasing extent buffer page.
4129 static void btrfs_release_extent_buffer_page(struct extent_buffer
*eb
,
4130 unsigned long start_idx
)
4132 unsigned long index
;
4133 unsigned long num_pages
;
4135 int mapped
= !test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
);
4137 BUG_ON(extent_buffer_under_io(eb
));
4139 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4140 index
= start_idx
+ num_pages
;
4141 if (start_idx
>= index
)
4146 page
= extent_buffer_page(eb
, index
);
4147 if (page
&& mapped
) {
4148 spin_lock(&page
->mapping
->private_lock
);
4150 * We do this since we'll remove the pages after we've
4151 * removed the eb from the radix tree, so we could race
4152 * and have this page now attached to the new eb. So
4153 * only clear page_private if it's still connected to
4156 if (PagePrivate(page
) &&
4157 page
->private == (unsigned long)eb
) {
4158 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4159 BUG_ON(PageDirty(page
));
4160 BUG_ON(PageWriteback(page
));
4162 * We need to make sure we haven't be attached
4165 ClearPagePrivate(page
);
4166 set_page_private(page
, 0);
4167 /* One for the page private */
4168 page_cache_release(page
);
4170 spin_unlock(&page
->mapping
->private_lock
);
4174 /* One for when we alloced the page */
4175 page_cache_release(page
);
4177 } while (index
!= start_idx
);
4181 * Helper for releasing the extent buffer.
4183 static inline void btrfs_release_extent_buffer(struct extent_buffer
*eb
)
4185 btrfs_release_extent_buffer_page(eb
, 0);
4186 __free_extent_buffer(eb
);
4189 static void check_buffer_tree_ref(struct extent_buffer
*eb
)
4191 /* the ref bit is tricky. We have to make sure it is set
4192 * if we have the buffer dirty. Otherwise the
4193 * code to free a buffer can end up dropping a dirty
4196 * Once the ref bit is set, it won't go away while the
4197 * buffer is dirty or in writeback, and it also won't
4198 * go away while we have the reference count on the
4201 * We can't just set the ref bit without bumping the
4202 * ref on the eb because free_extent_buffer might
4203 * see the ref bit and try to clear it. If this happens
4204 * free_extent_buffer might end up dropping our original
4205 * ref by mistake and freeing the page before we are able
4206 * to add one more ref.
4208 * So bump the ref count first, then set the bit. If someone
4209 * beat us to it, drop the ref we added.
4211 spin_lock(&eb
->refs_lock
);
4212 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4213 atomic_inc(&eb
->refs
);
4214 spin_unlock(&eb
->refs_lock
);
4217 static void mark_extent_buffer_accessed(struct extent_buffer
*eb
)
4219 unsigned long num_pages
, i
;
4221 check_buffer_tree_ref(eb
);
4223 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4224 for (i
= 0; i
< num_pages
; i
++) {
4225 struct page
*p
= extent_buffer_page(eb
, i
);
4226 mark_page_accessed(p
);
4230 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
4231 u64 start
, unsigned long len
)
4233 unsigned long num_pages
= num_extent_pages(start
, len
);
4235 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
4236 struct extent_buffer
*eb
;
4237 struct extent_buffer
*exists
= NULL
;
4239 struct address_space
*mapping
= tree
->mapping
;
4244 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4245 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4247 mark_extent_buffer_accessed(eb
);
4252 eb
= __alloc_extent_buffer(tree
, start
, len
, GFP_NOFS
);
4256 for (i
= 0; i
< num_pages
; i
++, index
++) {
4257 p
= find_or_create_page(mapping
, index
, GFP_NOFS
);
4261 spin_lock(&mapping
->private_lock
);
4262 if (PagePrivate(p
)) {
4264 * We could have already allocated an eb for this page
4265 * and attached one so lets see if we can get a ref on
4266 * the existing eb, and if we can we know it's good and
4267 * we can just return that one, else we know we can just
4268 * overwrite page->private.
4270 exists
= (struct extent_buffer
*)p
->private;
4271 if (atomic_inc_not_zero(&exists
->refs
)) {
4272 spin_unlock(&mapping
->private_lock
);
4274 page_cache_release(p
);
4275 mark_extent_buffer_accessed(exists
);
4280 * Do this so attach doesn't complain and we need to
4281 * drop the ref the old guy had.
4283 ClearPagePrivate(p
);
4284 WARN_ON(PageDirty(p
));
4285 page_cache_release(p
);
4287 attach_extent_buffer_page(eb
, p
);
4288 spin_unlock(&mapping
->private_lock
);
4289 WARN_ON(PageDirty(p
));
4290 mark_page_accessed(p
);
4292 if (!PageUptodate(p
))
4296 * see below about how we avoid a nasty race with release page
4297 * and why we unlock later
4301 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4303 ret
= radix_tree_preload(GFP_NOFS
& ~__GFP_HIGHMEM
);
4307 spin_lock(&tree
->buffer_lock
);
4308 ret
= radix_tree_insert(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
, eb
);
4309 if (ret
== -EEXIST
) {
4310 exists
= radix_tree_lookup(&tree
->buffer
,
4311 start
>> PAGE_CACHE_SHIFT
);
4312 if (!atomic_inc_not_zero(&exists
->refs
)) {
4313 spin_unlock(&tree
->buffer_lock
);
4314 radix_tree_preload_end();
4318 spin_unlock(&tree
->buffer_lock
);
4319 radix_tree_preload_end();
4320 mark_extent_buffer_accessed(exists
);
4323 /* add one reference for the tree */
4324 check_buffer_tree_ref(eb
);
4325 spin_unlock(&tree
->buffer_lock
);
4326 radix_tree_preload_end();
4329 * there is a race where release page may have
4330 * tried to find this extent buffer in the radix
4331 * but failed. It will tell the VM it is safe to
4332 * reclaim the, and it will clear the page private bit.
4333 * We must make sure to set the page private bit properly
4334 * after the extent buffer is in the radix tree so
4335 * it doesn't get lost
4337 SetPageChecked(eb
->pages
[0]);
4338 for (i
= 1; i
< num_pages
; i
++) {
4339 p
= extent_buffer_page(eb
, i
);
4340 ClearPageChecked(p
);
4343 unlock_page(eb
->pages
[0]);
4347 for (i
= 0; i
< num_pages
; i
++) {
4349 unlock_page(eb
->pages
[i
]);
4352 WARN_ON(!atomic_dec_and_test(&eb
->refs
));
4353 btrfs_release_extent_buffer(eb
);
4357 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
4358 u64 start
, unsigned long len
)
4360 struct extent_buffer
*eb
;
4363 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4364 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4366 mark_extent_buffer_accessed(eb
);
4374 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head
*head
)
4376 struct extent_buffer
*eb
=
4377 container_of(head
, struct extent_buffer
, rcu_head
);
4379 __free_extent_buffer(eb
);
4382 /* Expects to have eb->eb_lock already held */
4383 static int release_extent_buffer(struct extent_buffer
*eb
, gfp_t mask
)
4385 WARN_ON(atomic_read(&eb
->refs
) == 0);
4386 if (atomic_dec_and_test(&eb
->refs
)) {
4387 if (test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
)) {
4388 spin_unlock(&eb
->refs_lock
);
4390 struct extent_io_tree
*tree
= eb
->tree
;
4392 spin_unlock(&eb
->refs_lock
);
4394 spin_lock(&tree
->buffer_lock
);
4395 radix_tree_delete(&tree
->buffer
,
4396 eb
->start
>> PAGE_CACHE_SHIFT
);
4397 spin_unlock(&tree
->buffer_lock
);
4400 /* Should be safe to release our pages at this point */
4401 btrfs_release_extent_buffer_page(eb
, 0);
4402 call_rcu(&eb
->rcu_head
, btrfs_release_extent_buffer_rcu
);
4405 spin_unlock(&eb
->refs_lock
);
4410 void free_extent_buffer(struct extent_buffer
*eb
)
4415 spin_lock(&eb
->refs_lock
);
4416 if (atomic_read(&eb
->refs
) == 2 &&
4417 test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
))
4418 atomic_dec(&eb
->refs
);
4420 if (atomic_read(&eb
->refs
) == 2 &&
4421 test_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
) &&
4422 !extent_buffer_under_io(eb
) &&
4423 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4424 atomic_dec(&eb
->refs
);
4427 * I know this is terrible, but it's temporary until we stop tracking
4428 * the uptodate bits and such for the extent buffers.
4430 release_extent_buffer(eb
, GFP_ATOMIC
);
4433 void free_extent_buffer_stale(struct extent_buffer
*eb
)
4438 spin_lock(&eb
->refs_lock
);
4439 set_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
);
4441 if (atomic_read(&eb
->refs
) == 2 && !extent_buffer_under_io(eb
) &&
4442 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4443 atomic_dec(&eb
->refs
);
4444 release_extent_buffer(eb
, GFP_NOFS
);
4447 void clear_extent_buffer_dirty(struct extent_buffer
*eb
)
4450 unsigned long num_pages
;
4453 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4455 for (i
= 0; i
< num_pages
; i
++) {
4456 page
= extent_buffer_page(eb
, i
);
4457 if (!PageDirty(page
))
4461 WARN_ON(!PagePrivate(page
));
4463 clear_page_dirty_for_io(page
);
4464 spin_lock_irq(&page
->mapping
->tree_lock
);
4465 if (!PageDirty(page
)) {
4466 radix_tree_tag_clear(&page
->mapping
->page_tree
,
4468 PAGECACHE_TAG_DIRTY
);
4470 spin_unlock_irq(&page
->mapping
->tree_lock
);
4471 ClearPageError(page
);
4474 WARN_ON(atomic_read(&eb
->refs
) == 0);
4477 int set_extent_buffer_dirty(struct extent_buffer
*eb
)
4480 unsigned long num_pages
;
4483 check_buffer_tree_ref(eb
);
4485 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
4487 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4488 WARN_ON(atomic_read(&eb
->refs
) == 0);
4489 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
));
4491 for (i
= 0; i
< num_pages
; i
++)
4492 set_page_dirty(extent_buffer_page(eb
, i
));
4496 static int range_straddles_pages(u64 start
, u64 len
)
4498 if (len
< PAGE_CACHE_SIZE
)
4500 if (start
& (PAGE_CACHE_SIZE
- 1))
4502 if ((start
+ len
) & (PAGE_CACHE_SIZE
- 1))
4507 int clear_extent_buffer_uptodate(struct extent_buffer
*eb
)
4511 unsigned long num_pages
;
4513 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4514 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4515 for (i
= 0; i
< num_pages
; i
++) {
4516 page
= extent_buffer_page(eb
, i
);
4518 ClearPageUptodate(page
);
4523 int set_extent_buffer_uptodate(struct extent_buffer
*eb
)
4527 unsigned long num_pages
;
4529 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4530 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4531 for (i
= 0; i
< num_pages
; i
++) {
4532 page
= extent_buffer_page(eb
, i
);
4533 SetPageUptodate(page
);
4538 int extent_range_uptodate(struct extent_io_tree
*tree
,
4543 int pg_uptodate
= 1;
4545 unsigned long index
;
4547 if (range_straddles_pages(start
, end
- start
+ 1)) {
4548 ret
= test_range_bit(tree
, start
, end
,
4549 EXTENT_UPTODATE
, 1, NULL
);
4553 while (start
<= end
) {
4554 index
= start
>> PAGE_CACHE_SHIFT
;
4555 page
= find_get_page(tree
->mapping
, index
);
4558 uptodate
= PageUptodate(page
);
4559 page_cache_release(page
);
4564 start
+= PAGE_CACHE_SIZE
;
4569 int extent_buffer_uptodate(struct extent_buffer
*eb
)
4571 return test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4574 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
4575 struct extent_buffer
*eb
, u64 start
, int wait
,
4576 get_extent_t
*get_extent
, int mirror_num
)
4579 unsigned long start_i
;
4583 int locked_pages
= 0;
4584 int all_uptodate
= 1;
4585 unsigned long num_pages
;
4586 unsigned long num_reads
= 0;
4587 struct bio
*bio
= NULL
;
4588 unsigned long bio_flags
= 0;
4590 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
4594 WARN_ON(start
< eb
->start
);
4595 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
4596 (eb
->start
>> PAGE_CACHE_SHIFT
);
4601 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4602 for (i
= start_i
; i
< num_pages
; i
++) {
4603 page
= extent_buffer_page(eb
, i
);
4604 if (wait
== WAIT_NONE
) {
4605 if (!trylock_page(page
))
4611 if (!PageUptodate(page
)) {
4618 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4622 clear_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
4623 eb
->read_mirror
= 0;
4624 atomic_set(&eb
->io_pages
, num_reads
);
4625 for (i
= start_i
; i
< num_pages
; i
++) {
4626 page
= extent_buffer_page(eb
, i
);
4627 if (!PageUptodate(page
)) {
4628 ClearPageError(page
);
4629 err
= __extent_read_full_page(tree
, page
,
4631 mirror_num
, &bio_flags
);
4640 err
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
4645 if (ret
|| wait
!= WAIT_COMPLETE
)
4648 for (i
= start_i
; i
< num_pages
; i
++) {
4649 page
= extent_buffer_page(eb
, i
);
4650 wait_on_page_locked(page
);
4651 if (!PageUptodate(page
))
4659 while (locked_pages
> 0) {
4660 page
= extent_buffer_page(eb
, i
);
4668 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
4669 unsigned long start
,
4676 char *dst
= (char *)dstv
;
4677 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4678 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4680 WARN_ON(start
> eb
->len
);
4681 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4683 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4686 page
= extent_buffer_page(eb
, i
);
4688 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4689 kaddr
= page_address(page
);
4690 memcpy(dst
, kaddr
+ offset
, cur
);
4699 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
4700 unsigned long min_len
, char **map
,
4701 unsigned long *map_start
,
4702 unsigned long *map_len
)
4704 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
4707 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4708 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4709 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
4716 offset
= start_offset
;
4720 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
4723 if (start
+ min_len
> eb
->len
) {
4724 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
4725 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
4726 eb
->len
, start
, min_len
);
4731 p
= extent_buffer_page(eb
, i
);
4732 kaddr
= page_address(p
);
4733 *map
= kaddr
+ offset
;
4734 *map_len
= PAGE_CACHE_SIZE
- offset
;
4738 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
4739 unsigned long start
,
4746 char *ptr
= (char *)ptrv
;
4747 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4748 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4751 WARN_ON(start
> eb
->len
);
4752 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4754 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4757 page
= extent_buffer_page(eb
, i
);
4759 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4761 kaddr
= page_address(page
);
4762 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
4774 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
4775 unsigned long start
, unsigned long len
)
4781 char *src
= (char *)srcv
;
4782 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4783 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4785 WARN_ON(start
> eb
->len
);
4786 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4788 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4791 page
= extent_buffer_page(eb
, i
);
4792 WARN_ON(!PageUptodate(page
));
4794 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4795 kaddr
= page_address(page
);
4796 memcpy(kaddr
+ offset
, src
, cur
);
4805 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
4806 unsigned long start
, unsigned long len
)
4812 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4813 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4815 WARN_ON(start
> eb
->len
);
4816 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4818 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4821 page
= extent_buffer_page(eb
, i
);
4822 WARN_ON(!PageUptodate(page
));
4824 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4825 kaddr
= page_address(page
);
4826 memset(kaddr
+ offset
, c
, cur
);
4834 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
4835 unsigned long dst_offset
, unsigned long src_offset
,
4838 u64 dst_len
= dst
->len
;
4843 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4844 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4846 WARN_ON(src
->len
!= dst_len
);
4848 offset
= (start_offset
+ dst_offset
) &
4849 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4852 page
= extent_buffer_page(dst
, i
);
4853 WARN_ON(!PageUptodate(page
));
4855 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
4857 kaddr
= page_address(page
);
4858 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
4867 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
4868 unsigned long dst_off
, unsigned long src_off
,
4871 char *dst_kaddr
= page_address(dst_page
);
4872 if (dst_page
== src_page
) {
4873 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
4875 char *src_kaddr
= page_address(src_page
);
4876 char *p
= dst_kaddr
+ dst_off
+ len
;
4877 char *s
= src_kaddr
+ src_off
+ len
;
4884 static inline bool areas_overlap(unsigned long src
, unsigned long dst
, unsigned long len
)
4886 unsigned long distance
= (src
> dst
) ? src
- dst
: dst
- src
;
4887 return distance
< len
;
4890 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
4891 unsigned long dst_off
, unsigned long src_off
,
4894 char *dst_kaddr
= page_address(dst_page
);
4896 int must_memmove
= 0;
4898 if (dst_page
!= src_page
) {
4899 src_kaddr
= page_address(src_page
);
4901 src_kaddr
= dst_kaddr
;
4902 if (areas_overlap(src_off
, dst_off
, len
))
4907 memmove(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4909 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4912 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4913 unsigned long src_offset
, unsigned long len
)
4916 size_t dst_off_in_page
;
4917 size_t src_off_in_page
;
4918 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4919 unsigned long dst_i
;
4920 unsigned long src_i
;
4922 if (src_offset
+ len
> dst
->len
) {
4923 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4924 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
4927 if (dst_offset
+ len
> dst
->len
) {
4928 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4929 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
4934 dst_off_in_page
= (start_offset
+ dst_offset
) &
4935 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4936 src_off_in_page
= (start_offset
+ src_offset
) &
4937 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4939 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4940 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
4942 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
4944 cur
= min_t(unsigned long, cur
,
4945 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
4947 copy_pages(extent_buffer_page(dst
, dst_i
),
4948 extent_buffer_page(dst
, src_i
),
4949 dst_off_in_page
, src_off_in_page
, cur
);
4957 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4958 unsigned long src_offset
, unsigned long len
)
4961 size_t dst_off_in_page
;
4962 size_t src_off_in_page
;
4963 unsigned long dst_end
= dst_offset
+ len
- 1;
4964 unsigned long src_end
= src_offset
+ len
- 1;
4965 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4966 unsigned long dst_i
;
4967 unsigned long src_i
;
4969 if (src_offset
+ len
> dst
->len
) {
4970 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4971 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
4974 if (dst_offset
+ len
> dst
->len
) {
4975 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4976 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
4979 if (dst_offset
< src_offset
) {
4980 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
4984 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
4985 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
4987 dst_off_in_page
= (start_offset
+ dst_end
) &
4988 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4989 src_off_in_page
= (start_offset
+ src_end
) &
4990 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4992 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
4993 cur
= min(cur
, dst_off_in_page
+ 1);
4994 move_pages(extent_buffer_page(dst
, dst_i
),
4995 extent_buffer_page(dst
, src_i
),
4996 dst_off_in_page
- cur
+ 1,
4997 src_off_in_page
- cur
+ 1, cur
);
5005 int try_release_extent_buffer(struct page
*page
, gfp_t mask
)
5007 struct extent_buffer
*eb
;
5010 * We need to make sure noboody is attaching this page to an eb right
5013 spin_lock(&page
->mapping
->private_lock
);
5014 if (!PagePrivate(page
)) {
5015 spin_unlock(&page
->mapping
->private_lock
);
5019 eb
= (struct extent_buffer
*)page
->private;
5023 * This is a little awful but should be ok, we need to make sure that
5024 * the eb doesn't disappear out from under us while we're looking at
5027 spin_lock(&eb
->refs_lock
);
5028 if (atomic_read(&eb
->refs
) != 1 || extent_buffer_under_io(eb
)) {
5029 spin_unlock(&eb
->refs_lock
);
5030 spin_unlock(&page
->mapping
->private_lock
);
5033 spin_unlock(&page
->mapping
->private_lock
);
5035 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
5039 * If tree ref isn't set then we know the ref on this eb is a real ref,
5040 * so just return, this page will likely be freed soon anyway.
5042 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
)) {
5043 spin_unlock(&eb
->refs_lock
);
5047 return release_extent_buffer(eb
, mask
);