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
);
111 if (extent_state_cache
)
112 kmem_cache_destroy(extent_state_cache
);
113 if (extent_buffer_cache
)
114 kmem_cache_destroy(extent_buffer_cache
);
117 void extent_io_tree_init(struct extent_io_tree
*tree
,
118 struct address_space
*mapping
)
120 tree
->state
= RB_ROOT
;
121 INIT_RADIX_TREE(&tree
->buffer
, GFP_ATOMIC
);
123 tree
->dirty_bytes
= 0;
124 spin_lock_init(&tree
->lock
);
125 spin_lock_init(&tree
->buffer_lock
);
126 tree
->mapping
= mapping
;
129 static struct extent_state
*alloc_extent_state(gfp_t mask
)
131 struct extent_state
*state
;
136 state
= kmem_cache_alloc(extent_state_cache
, mask
);
143 spin_lock_irqsave(&leak_lock
, flags
);
144 list_add(&state
->leak_list
, &states
);
145 spin_unlock_irqrestore(&leak_lock
, flags
);
147 atomic_set(&state
->refs
, 1);
148 init_waitqueue_head(&state
->wq
);
149 trace_alloc_extent_state(state
, mask
, _RET_IP_
);
153 void free_extent_state(struct extent_state
*state
)
157 if (atomic_dec_and_test(&state
->refs
)) {
161 WARN_ON(state
->tree
);
163 spin_lock_irqsave(&leak_lock
, flags
);
164 list_del(&state
->leak_list
);
165 spin_unlock_irqrestore(&leak_lock
, flags
);
167 trace_free_extent_state(state
, _RET_IP_
);
168 kmem_cache_free(extent_state_cache
, state
);
172 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
173 struct rb_node
*node
)
175 struct rb_node
**p
= &root
->rb_node
;
176 struct rb_node
*parent
= NULL
;
177 struct tree_entry
*entry
;
181 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
183 if (offset
< entry
->start
)
185 else if (offset
> entry
->end
)
191 rb_link_node(node
, parent
, p
);
192 rb_insert_color(node
, root
);
196 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
197 struct rb_node
**prev_ret
,
198 struct rb_node
**next_ret
)
200 struct rb_root
*root
= &tree
->state
;
201 struct rb_node
*n
= root
->rb_node
;
202 struct rb_node
*prev
= NULL
;
203 struct rb_node
*orig_prev
= NULL
;
204 struct tree_entry
*entry
;
205 struct tree_entry
*prev_entry
= NULL
;
208 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
212 if (offset
< entry
->start
)
214 else if (offset
> entry
->end
)
222 while (prev
&& offset
> prev_entry
->end
) {
223 prev
= rb_next(prev
);
224 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
231 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
232 while (prev
&& offset
< prev_entry
->start
) {
233 prev
= rb_prev(prev
);
234 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
241 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
244 struct rb_node
*prev
= NULL
;
247 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
253 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
254 struct extent_state
*other
)
256 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
257 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
262 * utility function to look for merge candidates inside a given range.
263 * Any extents with matching state are merged together into a single
264 * extent in the tree. Extents with EXTENT_IO in their state field
265 * are not merged because the end_io handlers need to be able to do
266 * operations on them without sleeping (or doing allocations/splits).
268 * This should be called with the tree lock held.
270 static void merge_state(struct extent_io_tree
*tree
,
271 struct extent_state
*state
)
273 struct extent_state
*other
;
274 struct rb_node
*other_node
;
276 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
279 other_node
= rb_prev(&state
->rb_node
);
281 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
282 if (other
->end
== state
->start
- 1 &&
283 other
->state
== state
->state
) {
284 merge_cb(tree
, state
, other
);
285 state
->start
= other
->start
;
287 rb_erase(&other
->rb_node
, &tree
->state
);
288 free_extent_state(other
);
291 other_node
= rb_next(&state
->rb_node
);
293 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
294 if (other
->start
== state
->end
+ 1 &&
295 other
->state
== state
->state
) {
296 merge_cb(tree
, state
, other
);
297 state
->end
= other
->end
;
299 rb_erase(&other
->rb_node
, &tree
->state
);
300 free_extent_state(other
);
305 static void set_state_cb(struct extent_io_tree
*tree
,
306 struct extent_state
*state
, int *bits
)
308 if (tree
->ops
&& tree
->ops
->set_bit_hook
)
309 tree
->ops
->set_bit_hook(tree
->mapping
->host
, state
, bits
);
312 static void clear_state_cb(struct extent_io_tree
*tree
,
313 struct extent_state
*state
, int *bits
)
315 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
316 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
, bits
);
319 static void set_state_bits(struct extent_io_tree
*tree
,
320 struct extent_state
*state
, int *bits
);
323 * insert an extent_state struct into the tree. 'bits' are set on the
324 * struct before it is inserted.
326 * This may return -EEXIST if the extent is already there, in which case the
327 * state struct is freed.
329 * The tree lock is not taken internally. This is a utility function and
330 * probably isn't what you want to call (see set/clear_extent_bit).
332 static int insert_state(struct extent_io_tree
*tree
,
333 struct extent_state
*state
, u64 start
, u64 end
,
336 struct rb_node
*node
;
339 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
340 (unsigned long long)end
,
341 (unsigned long long)start
);
344 state
->start
= start
;
347 set_state_bits(tree
, state
, bits
);
349 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
351 struct extent_state
*found
;
352 found
= rb_entry(node
, struct extent_state
, rb_node
);
353 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
354 "%llu %llu\n", (unsigned long long)found
->start
,
355 (unsigned long long)found
->end
,
356 (unsigned long long)start
, (unsigned long long)end
);
360 merge_state(tree
, state
);
364 static void split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
367 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
368 tree
->ops
->split_extent_hook(tree
->mapping
->host
, orig
, split
);
372 * split a given extent state struct in two, inserting the preallocated
373 * struct 'prealloc' as the newly created second half. 'split' indicates an
374 * offset inside 'orig' where it should be split.
377 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
378 * are two extent state structs in the tree:
379 * prealloc: [orig->start, split - 1]
380 * orig: [ split, orig->end ]
382 * The tree locks are not taken by this function. They need to be held
385 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
386 struct extent_state
*prealloc
, u64 split
)
388 struct rb_node
*node
;
390 split_cb(tree
, orig
, split
);
392 prealloc
->start
= orig
->start
;
393 prealloc
->end
= split
- 1;
394 prealloc
->state
= orig
->state
;
397 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
399 free_extent_state(prealloc
);
402 prealloc
->tree
= tree
;
406 static struct extent_state
*next_state(struct extent_state
*state
)
408 struct rb_node
*next
= rb_next(&state
->rb_node
);
410 return rb_entry(next
, struct extent_state
, rb_node
);
416 * utility function to clear some bits in an extent state struct.
417 * it will optionally wake up any one waiting on this state (wake == 1).
419 * If no bits are set on the state struct after clearing things, the
420 * struct is freed and removed from the tree
422 static struct extent_state
*clear_state_bit(struct extent_io_tree
*tree
,
423 struct extent_state
*state
,
426 struct extent_state
*next
;
427 int bits_to_clear
= *bits
& ~EXTENT_CTLBITS
;
429 if ((bits_to_clear
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
430 u64 range
= state
->end
- state
->start
+ 1;
431 WARN_ON(range
> tree
->dirty_bytes
);
432 tree
->dirty_bytes
-= range
;
434 clear_state_cb(tree
, state
, bits
);
435 state
->state
&= ~bits_to_clear
;
438 if (state
->state
== 0) {
439 next
= next_state(state
);
441 rb_erase(&state
->rb_node
, &tree
->state
);
443 free_extent_state(state
);
448 merge_state(tree
, state
);
449 next
= next_state(state
);
454 static struct extent_state
*
455 alloc_extent_state_atomic(struct extent_state
*prealloc
)
458 prealloc
= alloc_extent_state(GFP_ATOMIC
);
463 void extent_io_tree_panic(struct extent_io_tree
*tree
, int err
)
465 btrfs_panic(tree_fs_info(tree
), err
, "Locking error: "
466 "Extent tree was modified by another "
467 "thread while locked.");
471 * clear some bits on a range in the tree. This may require splitting
472 * or inserting elements in the tree, so the gfp mask is used to
473 * indicate which allocations or sleeping are allowed.
475 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
476 * the given range from the tree regardless of state (ie for truncate).
478 * the range [start, end] is inclusive.
480 * This takes the tree lock, and returns 0 on success and < 0 on error.
482 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
483 int bits
, int wake
, int delete,
484 struct extent_state
**cached_state
,
487 struct extent_state
*state
;
488 struct extent_state
*cached
;
489 struct extent_state
*prealloc
= NULL
;
490 struct rb_node
*node
;
496 bits
|= ~EXTENT_CTLBITS
;
497 bits
|= EXTENT_FIRST_DELALLOC
;
499 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
502 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
503 prealloc
= alloc_extent_state(mask
);
508 spin_lock(&tree
->lock
);
510 cached
= *cached_state
;
513 *cached_state
= NULL
;
517 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
518 cached
->end
> start
) {
520 atomic_dec(&cached
->refs
);
525 free_extent_state(cached
);
528 * this search will find the extents that end after
531 node
= tree_search(tree
, start
);
534 state
= rb_entry(node
, struct extent_state
, rb_node
);
536 if (state
->start
> end
)
538 WARN_ON(state
->end
< start
);
539 last_end
= state
->end
;
541 /* the state doesn't have the wanted bits, go ahead */
542 if (!(state
->state
& bits
)) {
543 state
= next_state(state
);
548 * | ---- desired range ---- |
550 * | ------------- state -------------- |
552 * We need to split the extent we found, and may flip
553 * bits on second half.
555 * If the extent we found extends past our range, we
556 * just split and search again. It'll get split again
557 * the next time though.
559 * If the extent we found is inside our range, we clear
560 * the desired bit on it.
563 if (state
->start
< start
) {
564 prealloc
= alloc_extent_state_atomic(prealloc
);
566 err
= split_state(tree
, state
, prealloc
, start
);
568 extent_io_tree_panic(tree
, err
);
573 if (state
->end
<= end
) {
574 state
= clear_state_bit(tree
, state
, &bits
, wake
);
580 * | ---- desired range ---- |
582 * We need to split the extent, and clear the bit
585 if (state
->start
<= end
&& state
->end
> end
) {
586 prealloc
= alloc_extent_state_atomic(prealloc
);
588 err
= split_state(tree
, state
, prealloc
, end
+ 1);
590 extent_io_tree_panic(tree
, err
);
595 clear_state_bit(tree
, prealloc
, &bits
, wake
);
601 state
= clear_state_bit(tree
, state
, &bits
, wake
);
603 if (last_end
== (u64
)-1)
605 start
= last_end
+ 1;
606 if (start
<= end
&& state
&& !need_resched())
611 spin_unlock(&tree
->lock
);
613 free_extent_state(prealloc
);
620 spin_unlock(&tree
->lock
);
621 if (mask
& __GFP_WAIT
)
626 static void wait_on_state(struct extent_io_tree
*tree
,
627 struct extent_state
*state
)
628 __releases(tree
->lock
)
629 __acquires(tree
->lock
)
632 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
633 spin_unlock(&tree
->lock
);
635 spin_lock(&tree
->lock
);
636 finish_wait(&state
->wq
, &wait
);
640 * waits for one or more bits to clear on a range in the state tree.
641 * The range [start, end] is inclusive.
642 * The tree lock is taken by this function
644 void wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
646 struct extent_state
*state
;
647 struct rb_node
*node
;
649 spin_lock(&tree
->lock
);
653 * this search will find all the extents that end after
656 node
= tree_search(tree
, start
);
660 state
= rb_entry(node
, struct extent_state
, rb_node
);
662 if (state
->start
> end
)
665 if (state
->state
& bits
) {
666 start
= state
->start
;
667 atomic_inc(&state
->refs
);
668 wait_on_state(tree
, state
);
669 free_extent_state(state
);
672 start
= state
->end
+ 1;
677 cond_resched_lock(&tree
->lock
);
680 spin_unlock(&tree
->lock
);
683 static void set_state_bits(struct extent_io_tree
*tree
,
684 struct extent_state
*state
,
687 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
689 set_state_cb(tree
, state
, bits
);
690 if ((bits_to_set
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
691 u64 range
= state
->end
- state
->start
+ 1;
692 tree
->dirty_bytes
+= range
;
694 state
->state
|= bits_to_set
;
697 static void cache_state(struct extent_state
*state
,
698 struct extent_state
**cached_ptr
)
700 if (cached_ptr
&& !(*cached_ptr
)) {
701 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
703 atomic_inc(&state
->refs
);
708 static void uncache_state(struct extent_state
**cached_ptr
)
710 if (cached_ptr
&& (*cached_ptr
)) {
711 struct extent_state
*state
= *cached_ptr
;
713 free_extent_state(state
);
718 * set some bits on a range in the tree. This may require allocations or
719 * sleeping, so the gfp mask is used to indicate what is allowed.
721 * If any of the exclusive bits are set, this will fail with -EEXIST if some
722 * part of the range already has the desired bits set. The start of the
723 * existing range is returned in failed_start in this case.
725 * [start, end] is inclusive This takes the tree lock.
728 static int __must_check
729 __set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
730 int bits
, int exclusive_bits
, u64
*failed_start
,
731 struct extent_state
**cached_state
, gfp_t mask
)
733 struct extent_state
*state
;
734 struct extent_state
*prealloc
= NULL
;
735 struct rb_node
*node
;
740 bits
|= EXTENT_FIRST_DELALLOC
;
742 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
743 prealloc
= alloc_extent_state(mask
);
747 spin_lock(&tree
->lock
);
748 if (cached_state
&& *cached_state
) {
749 state
= *cached_state
;
750 if (state
->start
<= start
&& state
->end
> start
&&
752 node
= &state
->rb_node
;
757 * this search will find all the extents that end after
760 node
= tree_search(tree
, start
);
762 prealloc
= alloc_extent_state_atomic(prealloc
);
764 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
766 extent_io_tree_panic(tree
, err
);
771 state
= rb_entry(node
, struct extent_state
, rb_node
);
773 last_start
= state
->start
;
774 last_end
= state
->end
;
777 * | ---- desired range ---- |
780 * Just lock what we found and keep going
782 if (state
->start
== start
&& state
->end
<= end
) {
783 if (state
->state
& exclusive_bits
) {
784 *failed_start
= state
->start
;
789 set_state_bits(tree
, state
, &bits
);
790 cache_state(state
, cached_state
);
791 merge_state(tree
, state
);
792 if (last_end
== (u64
)-1)
794 start
= last_end
+ 1;
795 state
= next_state(state
);
796 if (start
< end
&& state
&& state
->start
== start
&&
803 * | ---- desired range ---- |
806 * | ------------- state -------------- |
808 * We need to split the extent we found, and may flip bits on
811 * If the extent we found extends past our
812 * range, we just split and search again. It'll get split
813 * again the next time though.
815 * If the extent we found is inside our range, we set the
818 if (state
->start
< start
) {
819 if (state
->state
& exclusive_bits
) {
820 *failed_start
= start
;
825 prealloc
= alloc_extent_state_atomic(prealloc
);
827 err
= split_state(tree
, state
, prealloc
, start
);
829 extent_io_tree_panic(tree
, err
);
834 if (state
->end
<= end
) {
835 set_state_bits(tree
, state
, &bits
);
836 cache_state(state
, cached_state
);
837 merge_state(tree
, state
);
838 if (last_end
== (u64
)-1)
840 start
= last_end
+ 1;
841 state
= next_state(state
);
842 if (start
< end
&& state
&& state
->start
== start
&&
849 * | ---- desired range ---- |
850 * | state | or | state |
852 * There's a hole, we need to insert something in it and
853 * ignore the extent we found.
855 if (state
->start
> start
) {
857 if (end
< last_start
)
860 this_end
= last_start
- 1;
862 prealloc
= alloc_extent_state_atomic(prealloc
);
866 * Avoid to free 'prealloc' if it can be merged with
869 err
= insert_state(tree
, prealloc
, start
, this_end
,
872 extent_io_tree_panic(tree
, err
);
874 cache_state(prealloc
, cached_state
);
876 start
= this_end
+ 1;
880 * | ---- desired range ---- |
882 * We need to split the extent, and set the bit
885 if (state
->start
<= end
&& state
->end
> end
) {
886 if (state
->state
& exclusive_bits
) {
887 *failed_start
= start
;
892 prealloc
= alloc_extent_state_atomic(prealloc
);
894 err
= split_state(tree
, state
, prealloc
, end
+ 1);
896 extent_io_tree_panic(tree
, err
);
898 set_state_bits(tree
, prealloc
, &bits
);
899 cache_state(prealloc
, cached_state
);
900 merge_state(tree
, prealloc
);
908 spin_unlock(&tree
->lock
);
910 free_extent_state(prealloc
);
917 spin_unlock(&tree
->lock
);
918 if (mask
& __GFP_WAIT
)
923 int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
,
924 u64
*failed_start
, struct extent_state
**cached_state
,
927 return __set_extent_bit(tree
, start
, end
, bits
, 0, failed_start
,
933 * convert_extent_bit - convert all bits in a given range from one bit to
935 * @tree: the io tree to search
936 * @start: the start offset in bytes
937 * @end: the end offset in bytes (inclusive)
938 * @bits: the bits to set in this range
939 * @clear_bits: the bits to clear in this range
940 * @cached_state: state that we're going to cache
941 * @mask: the allocation mask
943 * This will go through and set bits for the given range. If any states exist
944 * already in this range they are set with the given bit and cleared of the
945 * clear_bits. This is only meant to be used by things that are mergeable, ie
946 * converting from say DELALLOC to DIRTY. This is not meant to be used with
947 * boundary bits like LOCK.
949 int convert_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
950 int bits
, int clear_bits
,
951 struct extent_state
**cached_state
, gfp_t mask
)
953 struct extent_state
*state
;
954 struct extent_state
*prealloc
= NULL
;
955 struct rb_node
*node
;
961 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
962 prealloc
= alloc_extent_state(mask
);
967 spin_lock(&tree
->lock
);
968 if (cached_state
&& *cached_state
) {
969 state
= *cached_state
;
970 if (state
->start
<= start
&& state
->end
> start
&&
972 node
= &state
->rb_node
;
978 * this search will find all the extents that end after
981 node
= tree_search(tree
, start
);
983 prealloc
= alloc_extent_state_atomic(prealloc
);
988 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
991 extent_io_tree_panic(tree
, err
);
994 state
= rb_entry(node
, struct extent_state
, rb_node
);
996 last_start
= state
->start
;
997 last_end
= state
->end
;
1000 * | ---- desired range ---- |
1003 * Just lock what we found and keep going
1005 if (state
->start
== start
&& state
->end
<= end
) {
1006 set_state_bits(tree
, state
, &bits
);
1007 cache_state(state
, cached_state
);
1008 state
= clear_state_bit(tree
, state
, &clear_bits
, 0);
1009 if (last_end
== (u64
)-1)
1011 start
= last_end
+ 1;
1012 if (start
< end
&& state
&& state
->start
== start
&&
1019 * | ---- desired range ---- |
1022 * | ------------- state -------------- |
1024 * We need to split the extent we found, and may flip bits on
1027 * If the extent we found extends past our
1028 * range, we just split and search again. It'll get split
1029 * again the next time though.
1031 * If the extent we found is inside our range, we set the
1032 * desired bit on it.
1034 if (state
->start
< start
) {
1035 prealloc
= alloc_extent_state_atomic(prealloc
);
1040 err
= split_state(tree
, state
, prealloc
, start
);
1042 extent_io_tree_panic(tree
, err
);
1046 if (state
->end
<= end
) {
1047 set_state_bits(tree
, state
, &bits
);
1048 cache_state(state
, cached_state
);
1049 state
= clear_state_bit(tree
, state
, &clear_bits
, 0);
1050 if (last_end
== (u64
)-1)
1052 start
= last_end
+ 1;
1053 if (start
< end
&& state
&& state
->start
== start
&&
1060 * | ---- desired range ---- |
1061 * | state | or | state |
1063 * There's a hole, we need to insert something in it and
1064 * ignore the extent we found.
1066 if (state
->start
> start
) {
1068 if (end
< last_start
)
1071 this_end
= last_start
- 1;
1073 prealloc
= alloc_extent_state_atomic(prealloc
);
1080 * Avoid to free 'prealloc' if it can be merged with
1083 err
= insert_state(tree
, prealloc
, start
, this_end
,
1086 extent_io_tree_panic(tree
, err
);
1087 cache_state(prealloc
, cached_state
);
1089 start
= this_end
+ 1;
1093 * | ---- desired range ---- |
1095 * We need to split the extent, and set the bit
1098 if (state
->start
<= end
&& state
->end
> end
) {
1099 prealloc
= alloc_extent_state_atomic(prealloc
);
1105 err
= split_state(tree
, state
, prealloc
, end
+ 1);
1107 extent_io_tree_panic(tree
, err
);
1109 set_state_bits(tree
, prealloc
, &bits
);
1110 cache_state(prealloc
, cached_state
);
1111 clear_state_bit(tree
, prealloc
, &clear_bits
, 0);
1119 spin_unlock(&tree
->lock
);
1121 free_extent_state(prealloc
);
1128 spin_unlock(&tree
->lock
);
1129 if (mask
& __GFP_WAIT
)
1134 /* wrappers around set/clear extent bit */
1135 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1138 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, NULL
,
1142 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1143 int bits
, gfp_t mask
)
1145 return set_extent_bit(tree
, start
, end
, bits
, NULL
,
1149 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1150 int bits
, gfp_t mask
)
1152 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
1155 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1156 struct extent_state
**cached_state
, gfp_t mask
)
1158 return set_extent_bit(tree
, start
, end
,
1159 EXTENT_DELALLOC
| EXTENT_UPTODATE
,
1160 NULL
, cached_state
, mask
);
1163 int set_extent_defrag(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1164 struct extent_state
**cached_state
, gfp_t mask
)
1166 return set_extent_bit(tree
, start
, end
,
1167 EXTENT_DELALLOC
| EXTENT_UPTODATE
| EXTENT_DEFRAG
,
1168 NULL
, cached_state
, mask
);
1171 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1174 return clear_extent_bit(tree
, start
, end
,
1175 EXTENT_DIRTY
| EXTENT_DELALLOC
|
1176 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, mask
);
1179 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1182 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, NULL
,
1186 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1187 struct extent_state
**cached_state
, gfp_t mask
)
1189 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0,
1190 cached_state
, mask
);
1193 int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1194 struct extent_state
**cached_state
, gfp_t mask
)
1196 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
1197 cached_state
, mask
);
1201 * either insert or lock state struct between start and end use mask to tell
1202 * us if waiting is desired.
1204 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1205 int bits
, struct extent_state
**cached_state
)
1210 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
1211 EXTENT_LOCKED
, &failed_start
,
1212 cached_state
, GFP_NOFS
);
1213 if (err
== -EEXIST
) {
1214 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
1215 start
= failed_start
;
1218 WARN_ON(start
> end
);
1223 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1225 return lock_extent_bits(tree
, start
, end
, 0, NULL
);
1228 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1233 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1234 &failed_start
, NULL
, GFP_NOFS
);
1235 if (err
== -EEXIST
) {
1236 if (failed_start
> start
)
1237 clear_extent_bit(tree
, start
, failed_start
- 1,
1238 EXTENT_LOCKED
, 1, 0, NULL
, GFP_NOFS
);
1244 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1245 struct extent_state
**cached
, gfp_t mask
)
1247 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1251 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1253 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1258 * helper function to set both pages and extents in the tree writeback
1260 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1262 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1263 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1266 while (index
<= end_index
) {
1267 page
= find_get_page(tree
->mapping
, index
);
1268 BUG_ON(!page
); /* Pages should be in the extent_io_tree */
1269 set_page_writeback(page
);
1270 page_cache_release(page
);
1276 /* find the first state struct with 'bits' set after 'start', and
1277 * return it. tree->lock must be held. NULL will returned if
1278 * nothing was found after 'start'
1280 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1281 u64 start
, int bits
)
1283 struct rb_node
*node
;
1284 struct extent_state
*state
;
1287 * this search will find all the extents that end after
1290 node
= tree_search(tree
, start
);
1295 state
= rb_entry(node
, struct extent_state
, rb_node
);
1296 if (state
->end
>= start
&& (state
->state
& bits
))
1299 node
= rb_next(node
);
1308 * find the first offset in the io tree with 'bits' set. zero is
1309 * returned if we find something, and *start_ret and *end_ret are
1310 * set to reflect the state struct that was found.
1312 * If nothing was found, 1 is returned. If found something, return 0.
1314 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1315 u64
*start_ret
, u64
*end_ret
, int bits
,
1316 struct extent_state
**cached_state
)
1318 struct extent_state
*state
;
1322 spin_lock(&tree
->lock
);
1323 if (cached_state
&& *cached_state
) {
1324 state
= *cached_state
;
1325 if (state
->end
== start
- 1 && state
->tree
) {
1326 n
= rb_next(&state
->rb_node
);
1328 state
= rb_entry(n
, struct extent_state
,
1330 if (state
->state
& bits
)
1334 free_extent_state(*cached_state
);
1335 *cached_state
= NULL
;
1338 free_extent_state(*cached_state
);
1339 *cached_state
= NULL
;
1342 state
= find_first_extent_bit_state(tree
, start
, bits
);
1345 cache_state(state
, cached_state
);
1346 *start_ret
= state
->start
;
1347 *end_ret
= state
->end
;
1351 spin_unlock(&tree
->lock
);
1356 * find a contiguous range of bytes in the file marked as delalloc, not
1357 * more than 'max_bytes'. start and end are used to return the range,
1359 * 1 is returned if we find something, 0 if nothing was in the tree
1361 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1362 u64
*start
, u64
*end
, u64 max_bytes
,
1363 struct extent_state
**cached_state
)
1365 struct rb_node
*node
;
1366 struct extent_state
*state
;
1367 u64 cur_start
= *start
;
1369 u64 total_bytes
= 0;
1371 spin_lock(&tree
->lock
);
1374 * this search will find all the extents that end after
1377 node
= tree_search(tree
, cur_start
);
1385 state
= rb_entry(node
, struct extent_state
, rb_node
);
1386 if (found
&& (state
->start
!= cur_start
||
1387 (state
->state
& EXTENT_BOUNDARY
))) {
1390 if (!(state
->state
& EXTENT_DELALLOC
)) {
1396 *start
= state
->start
;
1397 *cached_state
= state
;
1398 atomic_inc(&state
->refs
);
1402 cur_start
= state
->end
+ 1;
1403 node
= rb_next(node
);
1406 total_bytes
+= state
->end
- state
->start
+ 1;
1407 if (total_bytes
>= max_bytes
)
1411 spin_unlock(&tree
->lock
);
1415 static noinline
void __unlock_for_delalloc(struct inode
*inode
,
1416 struct page
*locked_page
,
1420 struct page
*pages
[16];
1421 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1422 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1423 unsigned long nr_pages
= end_index
- index
+ 1;
1426 if (index
== locked_page
->index
&& end_index
== index
)
1429 while (nr_pages
> 0) {
1430 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1431 min_t(unsigned long, nr_pages
,
1432 ARRAY_SIZE(pages
)), pages
);
1433 for (i
= 0; i
< ret
; i
++) {
1434 if (pages
[i
] != locked_page
)
1435 unlock_page(pages
[i
]);
1436 page_cache_release(pages
[i
]);
1444 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1445 struct page
*locked_page
,
1449 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1450 unsigned long start_index
= index
;
1451 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1452 unsigned long pages_locked
= 0;
1453 struct page
*pages
[16];
1454 unsigned long nrpages
;
1458 /* the caller is responsible for locking the start index */
1459 if (index
== locked_page
->index
&& index
== end_index
)
1462 /* skip the page at the start index */
1463 nrpages
= end_index
- index
+ 1;
1464 while (nrpages
> 0) {
1465 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1466 min_t(unsigned long,
1467 nrpages
, ARRAY_SIZE(pages
)), pages
);
1472 /* now we have an array of pages, lock them all */
1473 for (i
= 0; i
< ret
; i
++) {
1475 * the caller is taking responsibility for
1478 if (pages
[i
] != locked_page
) {
1479 lock_page(pages
[i
]);
1480 if (!PageDirty(pages
[i
]) ||
1481 pages
[i
]->mapping
!= inode
->i_mapping
) {
1483 unlock_page(pages
[i
]);
1484 page_cache_release(pages
[i
]);
1488 page_cache_release(pages
[i
]);
1497 if (ret
&& pages_locked
) {
1498 __unlock_for_delalloc(inode
, locked_page
,
1500 ((u64
)(start_index
+ pages_locked
- 1)) <<
1507 * find a contiguous range of bytes in the file marked as delalloc, not
1508 * more than 'max_bytes'. start and end are used to return the range,
1510 * 1 is returned if we find something, 0 if nothing was in the tree
1512 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1513 struct extent_io_tree
*tree
,
1514 struct page
*locked_page
,
1515 u64
*start
, u64
*end
,
1521 struct extent_state
*cached_state
= NULL
;
1526 /* step one, find a bunch of delalloc bytes starting at start */
1527 delalloc_start
= *start
;
1529 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1530 max_bytes
, &cached_state
);
1531 if (!found
|| delalloc_end
<= *start
) {
1532 *start
= delalloc_start
;
1533 *end
= delalloc_end
;
1534 free_extent_state(cached_state
);
1539 * start comes from the offset of locked_page. We have to lock
1540 * pages in order, so we can't process delalloc bytes before
1543 if (delalloc_start
< *start
)
1544 delalloc_start
= *start
;
1547 * make sure to limit the number of pages we try to lock down
1550 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1551 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1553 /* step two, lock all the pages after the page that has start */
1554 ret
= lock_delalloc_pages(inode
, locked_page
,
1555 delalloc_start
, delalloc_end
);
1556 if (ret
== -EAGAIN
) {
1557 /* some of the pages are gone, lets avoid looping by
1558 * shortening the size of the delalloc range we're searching
1560 free_extent_state(cached_state
);
1562 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1563 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1571 BUG_ON(ret
); /* Only valid values are 0 and -EAGAIN */
1573 /* step three, lock the state bits for the whole range */
1574 lock_extent_bits(tree
, delalloc_start
, delalloc_end
, 0, &cached_state
);
1576 /* then test to make sure it is all still delalloc */
1577 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1578 EXTENT_DELALLOC
, 1, cached_state
);
1580 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1581 &cached_state
, GFP_NOFS
);
1582 __unlock_for_delalloc(inode
, locked_page
,
1583 delalloc_start
, delalloc_end
);
1587 free_extent_state(cached_state
);
1588 *start
= delalloc_start
;
1589 *end
= delalloc_end
;
1594 int extent_clear_unlock_delalloc(struct inode
*inode
,
1595 struct extent_io_tree
*tree
,
1596 u64 start
, u64 end
, struct page
*locked_page
,
1600 struct page
*pages
[16];
1601 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1602 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1603 unsigned long nr_pages
= end_index
- index
+ 1;
1607 if (op
& EXTENT_CLEAR_UNLOCK
)
1608 clear_bits
|= EXTENT_LOCKED
;
1609 if (op
& EXTENT_CLEAR_DIRTY
)
1610 clear_bits
|= EXTENT_DIRTY
;
1612 if (op
& EXTENT_CLEAR_DELALLOC
)
1613 clear_bits
|= EXTENT_DELALLOC
;
1615 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1616 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1617 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1618 EXTENT_SET_PRIVATE2
)))
1621 while (nr_pages
> 0) {
1622 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1623 min_t(unsigned long,
1624 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1625 for (i
= 0; i
< ret
; i
++) {
1627 if (op
& EXTENT_SET_PRIVATE2
)
1628 SetPagePrivate2(pages
[i
]);
1630 if (pages
[i
] == locked_page
) {
1631 page_cache_release(pages
[i
]);
1634 if (op
& EXTENT_CLEAR_DIRTY
)
1635 clear_page_dirty_for_io(pages
[i
]);
1636 if (op
& EXTENT_SET_WRITEBACK
)
1637 set_page_writeback(pages
[i
]);
1638 if (op
& EXTENT_END_WRITEBACK
)
1639 end_page_writeback(pages
[i
]);
1640 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1641 unlock_page(pages
[i
]);
1642 page_cache_release(pages
[i
]);
1652 * count the number of bytes in the tree that have a given bit(s)
1653 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1654 * cached. The total number found is returned.
1656 u64
count_range_bits(struct extent_io_tree
*tree
,
1657 u64
*start
, u64 search_end
, u64 max_bytes
,
1658 unsigned long bits
, int contig
)
1660 struct rb_node
*node
;
1661 struct extent_state
*state
;
1662 u64 cur_start
= *start
;
1663 u64 total_bytes
= 0;
1667 if (search_end
<= cur_start
) {
1672 spin_lock(&tree
->lock
);
1673 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1674 total_bytes
= tree
->dirty_bytes
;
1678 * this search will find all the extents that end after
1681 node
= tree_search(tree
, cur_start
);
1686 state
= rb_entry(node
, struct extent_state
, rb_node
);
1687 if (state
->start
> search_end
)
1689 if (contig
&& found
&& state
->start
> last
+ 1)
1691 if (state
->end
>= cur_start
&& (state
->state
& bits
) == bits
) {
1692 total_bytes
+= min(search_end
, state
->end
) + 1 -
1693 max(cur_start
, state
->start
);
1694 if (total_bytes
>= max_bytes
)
1697 *start
= max(cur_start
, state
->start
);
1701 } else if (contig
&& found
) {
1704 node
= rb_next(node
);
1709 spin_unlock(&tree
->lock
);
1714 * set the private field for a given byte offset in the tree. If there isn't
1715 * an extent_state there already, this does nothing.
1717 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1719 struct rb_node
*node
;
1720 struct extent_state
*state
;
1723 spin_lock(&tree
->lock
);
1725 * this search will find all the extents that end after
1728 node
= tree_search(tree
, start
);
1733 state
= rb_entry(node
, struct extent_state
, rb_node
);
1734 if (state
->start
!= start
) {
1738 state
->private = private;
1740 spin_unlock(&tree
->lock
);
1744 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1746 struct rb_node
*node
;
1747 struct extent_state
*state
;
1750 spin_lock(&tree
->lock
);
1752 * this search will find all the extents that end after
1755 node
= tree_search(tree
, start
);
1760 state
= rb_entry(node
, struct extent_state
, rb_node
);
1761 if (state
->start
!= start
) {
1765 *private = state
->private;
1767 spin_unlock(&tree
->lock
);
1772 * searches a range in the state tree for a given mask.
1773 * If 'filled' == 1, this returns 1 only if every extent in the tree
1774 * has the bits set. Otherwise, 1 is returned if any bit in the
1775 * range is found set.
1777 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1778 int bits
, int filled
, struct extent_state
*cached
)
1780 struct extent_state
*state
= NULL
;
1781 struct rb_node
*node
;
1784 spin_lock(&tree
->lock
);
1785 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
1786 cached
->end
> start
)
1787 node
= &cached
->rb_node
;
1789 node
= tree_search(tree
, start
);
1790 while (node
&& start
<= end
) {
1791 state
= rb_entry(node
, struct extent_state
, rb_node
);
1793 if (filled
&& state
->start
> start
) {
1798 if (state
->start
> end
)
1801 if (state
->state
& bits
) {
1805 } else if (filled
) {
1810 if (state
->end
== (u64
)-1)
1813 start
= state
->end
+ 1;
1816 node
= rb_next(node
);
1823 spin_unlock(&tree
->lock
);
1828 * helper function to set a given page up to date if all the
1829 * extents in the tree for that page are up to date
1831 static void check_page_uptodate(struct extent_io_tree
*tree
, struct page
*page
)
1833 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1834 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1835 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1836 SetPageUptodate(page
);
1840 * helper function to unlock a page if all the extents in the tree
1841 * for that page are unlocked
1843 static void check_page_locked(struct extent_io_tree
*tree
, struct page
*page
)
1845 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1846 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1847 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1852 * helper function to end page writeback if all the extents
1853 * in the tree for that page are done with writeback
1855 static void check_page_writeback(struct extent_io_tree
*tree
,
1858 end_page_writeback(page
);
1862 * When IO fails, either with EIO or csum verification fails, we
1863 * try other mirrors that might have a good copy of the data. This
1864 * io_failure_record is used to record state as we go through all the
1865 * mirrors. If another mirror has good data, the page is set up to date
1866 * and things continue. If a good mirror can't be found, the original
1867 * bio end_io callback is called to indicate things have failed.
1869 struct io_failure_record
{
1874 unsigned long bio_flags
;
1880 static int free_io_failure(struct inode
*inode
, struct io_failure_record
*rec
,
1885 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1887 set_state_private(failure_tree
, rec
->start
, 0);
1888 ret
= clear_extent_bits(failure_tree
, rec
->start
,
1889 rec
->start
+ rec
->len
- 1,
1890 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1895 ret
= clear_extent_bits(&BTRFS_I(inode
)->io_tree
, rec
->start
,
1896 rec
->start
+ rec
->len
- 1,
1897 EXTENT_DAMAGED
, GFP_NOFS
);
1906 static void repair_io_failure_callback(struct bio
*bio
, int err
)
1908 complete(bio
->bi_private
);
1912 * this bypasses the standard btrfs submit functions deliberately, as
1913 * the standard behavior is to write all copies in a raid setup. here we only
1914 * want to write the one bad copy. so we do the mapping for ourselves and issue
1915 * submit_bio directly.
1916 * to avoid any synchonization issues, wait for the data after writing, which
1917 * actually prevents the read that triggered the error from finishing.
1918 * currently, there can be no more than two copies of every data bit. thus,
1919 * exactly one rewrite is required.
1921 int repair_io_failure(struct btrfs_mapping_tree
*map_tree
, u64 start
,
1922 u64 length
, u64 logical
, struct page
*page
,
1926 struct btrfs_device
*dev
;
1927 DECLARE_COMPLETION_ONSTACK(compl);
1930 struct btrfs_bio
*bbio
= NULL
;
1933 BUG_ON(!mirror_num
);
1935 bio
= bio_alloc(GFP_NOFS
, 1);
1938 bio
->bi_private
= &compl;
1939 bio
->bi_end_io
= repair_io_failure_callback
;
1941 map_length
= length
;
1943 ret
= btrfs_map_block(map_tree
, WRITE
, logical
,
1944 &map_length
, &bbio
, mirror_num
);
1949 BUG_ON(mirror_num
!= bbio
->mirror_num
);
1950 sector
= bbio
->stripes
[mirror_num
-1].physical
>> 9;
1951 bio
->bi_sector
= sector
;
1952 dev
= bbio
->stripes
[mirror_num
-1].dev
;
1954 if (!dev
|| !dev
->bdev
|| !dev
->writeable
) {
1958 bio
->bi_bdev
= dev
->bdev
;
1959 bio_add_page(bio
, page
, length
, start
-page_offset(page
));
1960 btrfsic_submit_bio(WRITE_SYNC
, bio
);
1961 wait_for_completion(&compl);
1963 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
1964 /* try to remap that extent elsewhere? */
1966 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_WRITE_ERRS
);
1970 printk_ratelimited_in_rcu(KERN_INFO
"btrfs read error corrected: ino %lu off %llu "
1971 "(dev %s sector %llu)\n", page
->mapping
->host
->i_ino
,
1972 start
, rcu_str_deref(dev
->name
), sector
);
1978 int repair_eb_io_failure(struct btrfs_root
*root
, struct extent_buffer
*eb
,
1981 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1982 u64 start
= eb
->start
;
1983 unsigned long i
, num_pages
= num_extent_pages(eb
->start
, eb
->len
);
1986 for (i
= 0; i
< num_pages
; i
++) {
1987 struct page
*p
= extent_buffer_page(eb
, i
);
1988 ret
= repair_io_failure(map_tree
, start
, PAGE_CACHE_SIZE
,
1989 start
, p
, mirror_num
);
1992 start
+= PAGE_CACHE_SIZE
;
1999 * each time an IO finishes, we do a fast check in the IO failure tree
2000 * to see if we need to process or clean up an io_failure_record
2002 static int clean_io_failure(u64 start
, struct page
*page
)
2005 u64 private_failure
;
2006 struct io_failure_record
*failrec
;
2007 struct btrfs_mapping_tree
*map_tree
;
2008 struct extent_state
*state
;
2012 struct inode
*inode
= page
->mapping
->host
;
2015 ret
= count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
2016 (u64
)-1, 1, EXTENT_DIRTY
, 0);
2020 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
, start
,
2025 failrec
= (struct io_failure_record
*)(unsigned long) private_failure
;
2026 BUG_ON(!failrec
->this_mirror
);
2028 if (failrec
->in_validation
) {
2029 /* there was no real error, just free the record */
2030 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2036 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
2037 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
2040 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
2042 if (state
&& state
->start
== failrec
->start
) {
2043 map_tree
= &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
;
2044 num_copies
= btrfs_num_copies(map_tree
, failrec
->logical
,
2046 if (num_copies
> 1) {
2047 ret
= repair_io_failure(map_tree
, start
, failrec
->len
,
2048 failrec
->logical
, page
,
2049 failrec
->failed_mirror
);
2056 ret
= free_io_failure(inode
, failrec
, did_repair
);
2062 * this is a generic handler for readpage errors (default
2063 * readpage_io_failed_hook). if other copies exist, read those and write back
2064 * good data to the failed position. does not investigate in remapping the
2065 * failed extent elsewhere, hoping the device will be smart enough to do this as
2069 static int bio_readpage_error(struct bio
*failed_bio
, struct page
*page
,
2070 u64 start
, u64 end
, int failed_mirror
,
2071 struct extent_state
*state
)
2073 struct io_failure_record
*failrec
= NULL
;
2075 struct extent_map
*em
;
2076 struct inode
*inode
= page
->mapping
->host
;
2077 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
2078 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2079 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2086 BUG_ON(failed_bio
->bi_rw
& REQ_WRITE
);
2088 ret
= get_state_private(failure_tree
, start
, &private);
2090 failrec
= kzalloc(sizeof(*failrec
), GFP_NOFS
);
2093 failrec
->start
= start
;
2094 failrec
->len
= end
- start
+ 1;
2095 failrec
->this_mirror
= 0;
2096 failrec
->bio_flags
= 0;
2097 failrec
->in_validation
= 0;
2099 read_lock(&em_tree
->lock
);
2100 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
2102 read_unlock(&em_tree
->lock
);
2107 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
2108 free_extent_map(em
);
2111 read_unlock(&em_tree
->lock
);
2117 logical
= start
- em
->start
;
2118 logical
= em
->block_start
+ logical
;
2119 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2120 logical
= em
->block_start
;
2121 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
2122 extent_set_compress_type(&failrec
->bio_flags
,
2125 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2126 "len=%llu\n", logical
, start
, failrec
->len
);
2127 failrec
->logical
= logical
;
2128 free_extent_map(em
);
2130 /* set the bits in the private failure tree */
2131 ret
= set_extent_bits(failure_tree
, start
, end
,
2132 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
2134 ret
= set_state_private(failure_tree
, start
,
2135 (u64
)(unsigned long)failrec
);
2136 /* set the bits in the inode's tree */
2138 ret
= set_extent_bits(tree
, start
, end
, EXTENT_DAMAGED
,
2145 failrec
= (struct io_failure_record
*)(unsigned long)private;
2146 pr_debug("bio_readpage_error: (found) logical=%llu, "
2147 "start=%llu, len=%llu, validation=%d\n",
2148 failrec
->logical
, failrec
->start
, failrec
->len
,
2149 failrec
->in_validation
);
2151 * when data can be on disk more than twice, add to failrec here
2152 * (e.g. with a list for failed_mirror) to make
2153 * clean_io_failure() clean all those errors at once.
2156 num_copies
= btrfs_num_copies(
2157 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
2158 failrec
->logical
, failrec
->len
);
2159 if (num_copies
== 1) {
2161 * we only have a single copy of the data, so don't bother with
2162 * all the retry and error correction code that follows. no
2163 * matter what the error is, it is very likely to persist.
2165 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2166 "state=%p, num_copies=%d, next_mirror %d, "
2167 "failed_mirror %d\n", state
, num_copies
,
2168 failrec
->this_mirror
, failed_mirror
);
2169 free_io_failure(inode
, failrec
, 0);
2174 spin_lock(&tree
->lock
);
2175 state
= find_first_extent_bit_state(tree
, failrec
->start
,
2177 if (state
&& state
->start
!= failrec
->start
)
2179 spin_unlock(&tree
->lock
);
2183 * there are two premises:
2184 * a) deliver good data to the caller
2185 * b) correct the bad sectors on disk
2187 if (failed_bio
->bi_vcnt
> 1) {
2189 * to fulfill b), we need to know the exact failing sectors, as
2190 * we don't want to rewrite any more than the failed ones. thus,
2191 * we need separate read requests for the failed bio
2193 * if the following BUG_ON triggers, our validation request got
2194 * merged. we need separate requests for our algorithm to work.
2196 BUG_ON(failrec
->in_validation
);
2197 failrec
->in_validation
= 1;
2198 failrec
->this_mirror
= failed_mirror
;
2199 read_mode
= READ_SYNC
| REQ_FAILFAST_DEV
;
2202 * we're ready to fulfill a) and b) alongside. get a good copy
2203 * of the failed sector and if we succeed, we have setup
2204 * everything for repair_io_failure to do the rest for us.
2206 if (failrec
->in_validation
) {
2207 BUG_ON(failrec
->this_mirror
!= failed_mirror
);
2208 failrec
->in_validation
= 0;
2209 failrec
->this_mirror
= 0;
2211 failrec
->failed_mirror
= failed_mirror
;
2212 failrec
->this_mirror
++;
2213 if (failrec
->this_mirror
== failed_mirror
)
2214 failrec
->this_mirror
++;
2215 read_mode
= READ_SYNC
;
2218 if (!state
|| failrec
->this_mirror
> num_copies
) {
2219 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2220 "next_mirror %d, failed_mirror %d\n", state
,
2221 num_copies
, failrec
->this_mirror
, failed_mirror
);
2222 free_io_failure(inode
, failrec
, 0);
2226 bio
= bio_alloc(GFP_NOFS
, 1);
2228 free_io_failure(inode
, failrec
, 0);
2231 bio
->bi_private
= state
;
2232 bio
->bi_end_io
= failed_bio
->bi_end_io
;
2233 bio
->bi_sector
= failrec
->logical
>> 9;
2234 bio
->bi_bdev
= BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
;
2237 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
2239 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2240 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode
,
2241 failrec
->this_mirror
, num_copies
, failrec
->in_validation
);
2243 ret
= tree
->ops
->submit_bio_hook(inode
, read_mode
, bio
,
2244 failrec
->this_mirror
,
2245 failrec
->bio_flags
, 0);
2249 /* lots and lots of room for performance fixes in the end_bio funcs */
2251 int end_extent_writepage(struct page
*page
, int err
, u64 start
, u64 end
)
2253 int uptodate
= (err
== 0);
2254 struct extent_io_tree
*tree
;
2257 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2259 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
2260 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
2261 end
, NULL
, uptodate
);
2267 ClearPageUptodate(page
);
2274 * after a writepage IO is done, we need to:
2275 * clear the uptodate bits on error
2276 * clear the writeback bits in the extent tree for this IO
2277 * end_page_writeback if the page has no more pending IO
2279 * Scheduling is not allowed, so the extent state tree is expected
2280 * to have one and only one object corresponding to this IO.
2282 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
2284 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2285 struct extent_io_tree
*tree
;
2291 struct page
*page
= bvec
->bv_page
;
2292 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2294 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
2296 end
= start
+ bvec
->bv_len
- 1;
2298 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2303 if (--bvec
>= bio
->bi_io_vec
)
2304 prefetchw(&bvec
->bv_page
->flags
);
2306 if (end_extent_writepage(page
, err
, start
, end
))
2310 end_page_writeback(page
);
2312 check_page_writeback(tree
, page
);
2313 } while (bvec
>= bio
->bi_io_vec
);
2319 * after a readpage IO is done, we need to:
2320 * clear the uptodate bits on error
2321 * set the uptodate bits if things worked
2322 * set the page up to date if all extents in the tree are uptodate
2323 * clear the lock bit in the extent tree
2324 * unlock the page if there are no other extents locked for it
2326 * Scheduling is not allowed, so the extent state tree is expected
2327 * to have one and only one object corresponding to this IO.
2329 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
2331 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2332 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2333 struct bio_vec
*bvec
= bio
->bi_io_vec
;
2334 struct extent_io_tree
*tree
;
2345 struct page
*page
= bvec
->bv_page
;
2346 struct extent_state
*cached
= NULL
;
2347 struct extent_state
*state
;
2349 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2350 "mirror=%ld\n", (u64
)bio
->bi_sector
, err
,
2351 (long int)bio
->bi_bdev
);
2352 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2354 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
2356 end
= start
+ bvec
->bv_len
- 1;
2358 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2363 if (++bvec
<= bvec_end
)
2364 prefetchw(&bvec
->bv_page
->flags
);
2366 spin_lock(&tree
->lock
);
2367 state
= find_first_extent_bit_state(tree
, start
, EXTENT_LOCKED
);
2368 if (state
&& state
->start
== start
) {
2370 * take a reference on the state, unlock will drop
2373 cache_state(state
, &cached
);
2375 spin_unlock(&tree
->lock
);
2377 mirror
= (int)(unsigned long)bio
->bi_bdev
;
2378 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
2379 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
2384 clean_io_failure(start
, page
);
2387 if (!uptodate
&& tree
->ops
&& tree
->ops
->readpage_io_failed_hook
) {
2388 ret
= tree
->ops
->readpage_io_failed_hook(page
, mirror
);
2390 test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
2392 } else if (!uptodate
) {
2394 * The generic bio_readpage_error handles errors the
2395 * following way: If possible, new read requests are
2396 * created and submitted and will end up in
2397 * end_bio_extent_readpage as well (if we're lucky, not
2398 * in the !uptodate case). In that case it returns 0 and
2399 * we just go on with the next page in our bio. If it
2400 * can't handle the error it will return -EIO and we
2401 * remain responsible for that page.
2403 ret
= bio_readpage_error(bio
, page
, start
, end
, mirror
, NULL
);
2406 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2409 uncache_state(&cached
);
2414 if (uptodate
&& tree
->track_uptodate
) {
2415 set_extent_uptodate(tree
, start
, end
, &cached
,
2418 unlock_extent_cached(tree
, start
, end
, &cached
, GFP_ATOMIC
);
2422 SetPageUptodate(page
);
2424 ClearPageUptodate(page
);
2430 check_page_uptodate(tree
, page
);
2432 ClearPageUptodate(page
);
2435 check_page_locked(tree
, page
);
2437 } while (bvec
<= bvec_end
);
2443 btrfs_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
2448 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2450 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
2451 while (!bio
&& (nr_vecs
/= 2))
2452 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2457 bio
->bi_bdev
= bdev
;
2458 bio
->bi_sector
= first_sector
;
2464 * Since writes are async, they will only return -ENOMEM.
2465 * Reads can return the full range of I/O error conditions.
2467 static int __must_check
submit_one_bio(int rw
, struct bio
*bio
,
2468 int mirror_num
, unsigned long bio_flags
)
2471 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2472 struct page
*page
= bvec
->bv_page
;
2473 struct extent_io_tree
*tree
= bio
->bi_private
;
2476 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
2478 bio
->bi_private
= NULL
;
2482 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
2483 ret
= tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
2484 mirror_num
, bio_flags
, start
);
2486 btrfsic_submit_bio(rw
, bio
);
2488 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
2494 static int merge_bio(struct extent_io_tree
*tree
, struct page
*page
,
2495 unsigned long offset
, size_t size
, struct bio
*bio
,
2496 unsigned long bio_flags
)
2499 if (tree
->ops
&& tree
->ops
->merge_bio_hook
)
2500 ret
= tree
->ops
->merge_bio_hook(page
, offset
, size
, bio
,
2507 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
2508 struct page
*page
, sector_t sector
,
2509 size_t size
, unsigned long offset
,
2510 struct block_device
*bdev
,
2511 struct bio
**bio_ret
,
2512 unsigned long max_pages
,
2513 bio_end_io_t end_io_func
,
2515 unsigned long prev_bio_flags
,
2516 unsigned long bio_flags
)
2522 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
2523 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
2524 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
2526 if (bio_ret
&& *bio_ret
) {
2529 contig
= bio
->bi_sector
== sector
;
2531 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
2534 if (prev_bio_flags
!= bio_flags
|| !contig
||
2535 merge_bio(tree
, page
, offset
, page_size
, bio
, bio_flags
) ||
2536 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
2537 ret
= submit_one_bio(rw
, bio
, mirror_num
,
2546 if (this_compressed
)
2549 nr
= bio_get_nr_vecs(bdev
);
2551 bio
= btrfs_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
2555 bio_add_page(bio
, page
, page_size
, offset
);
2556 bio
->bi_end_io
= end_io_func
;
2557 bio
->bi_private
= tree
;
2562 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
2567 void attach_extent_buffer_page(struct extent_buffer
*eb
, struct page
*page
)
2569 if (!PagePrivate(page
)) {
2570 SetPagePrivate(page
);
2571 page_cache_get(page
);
2572 set_page_private(page
, (unsigned long)eb
);
2574 WARN_ON(page
->private != (unsigned long)eb
);
2578 void set_page_extent_mapped(struct page
*page
)
2580 if (!PagePrivate(page
)) {
2581 SetPagePrivate(page
);
2582 page_cache_get(page
);
2583 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
2588 * basic readpage implementation. Locked extent state structs are inserted
2589 * into the tree that are removed when the IO is done (by the end_io
2591 * XXX JDM: This needs looking at to ensure proper page locking
2593 static int __extent_read_full_page(struct extent_io_tree
*tree
,
2595 get_extent_t
*get_extent
,
2596 struct bio
**bio
, int mirror_num
,
2597 unsigned long *bio_flags
)
2599 struct inode
*inode
= page
->mapping
->host
;
2600 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2601 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2605 u64 last_byte
= i_size_read(inode
);
2609 struct extent_map
*em
;
2610 struct block_device
*bdev
;
2611 struct btrfs_ordered_extent
*ordered
;
2614 size_t pg_offset
= 0;
2616 size_t disk_io_size
;
2617 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2618 unsigned long this_bio_flag
= 0;
2620 set_page_extent_mapped(page
);
2622 if (!PageUptodate(page
)) {
2623 if (cleancache_get_page(page
) == 0) {
2624 BUG_ON(blocksize
!= PAGE_SIZE
);
2631 lock_extent(tree
, start
, end
);
2632 ordered
= btrfs_lookup_ordered_extent(inode
, start
);
2635 unlock_extent(tree
, start
, end
);
2636 btrfs_start_ordered_extent(inode
, ordered
, 1);
2637 btrfs_put_ordered_extent(ordered
);
2640 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
2642 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
2645 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
2646 userpage
= kmap_atomic(page
);
2647 memset(userpage
+ zero_offset
, 0, iosize
);
2648 flush_dcache_page(page
);
2649 kunmap_atomic(userpage
);
2652 while (cur
<= end
) {
2653 if (cur
>= last_byte
) {
2655 struct extent_state
*cached
= NULL
;
2657 iosize
= PAGE_CACHE_SIZE
- pg_offset
;
2658 userpage
= kmap_atomic(page
);
2659 memset(userpage
+ pg_offset
, 0, iosize
);
2660 flush_dcache_page(page
);
2661 kunmap_atomic(userpage
);
2662 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2664 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2668 em
= get_extent(inode
, page
, pg_offset
, cur
,
2670 if (IS_ERR_OR_NULL(em
)) {
2672 unlock_extent(tree
, cur
, end
);
2675 extent_offset
= cur
- em
->start
;
2676 BUG_ON(extent_map_end(em
) <= cur
);
2679 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2680 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2681 extent_set_compress_type(&this_bio_flag
,
2685 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2686 cur_end
= min(extent_map_end(em
) - 1, end
);
2687 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2688 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2689 disk_io_size
= em
->block_len
;
2690 sector
= em
->block_start
>> 9;
2692 sector
= (em
->block_start
+ extent_offset
) >> 9;
2693 disk_io_size
= iosize
;
2696 block_start
= em
->block_start
;
2697 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2698 block_start
= EXTENT_MAP_HOLE
;
2699 free_extent_map(em
);
2702 /* we've found a hole, just zero and go on */
2703 if (block_start
== EXTENT_MAP_HOLE
) {
2705 struct extent_state
*cached
= NULL
;
2707 userpage
= kmap_atomic(page
);
2708 memset(userpage
+ pg_offset
, 0, iosize
);
2709 flush_dcache_page(page
);
2710 kunmap_atomic(userpage
);
2712 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2714 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2717 pg_offset
+= iosize
;
2720 /* the get_extent function already copied into the page */
2721 if (test_range_bit(tree
, cur
, cur_end
,
2722 EXTENT_UPTODATE
, 1, NULL
)) {
2723 check_page_uptodate(tree
, page
);
2724 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2726 pg_offset
+= iosize
;
2729 /* we have an inline extent but it didn't get marked up
2730 * to date. Error out
2732 if (block_start
== EXTENT_MAP_INLINE
) {
2734 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2736 pg_offset
+= iosize
;
2741 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2742 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2746 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2748 ret
= submit_extent_page(READ
, tree
, page
,
2749 sector
, disk_io_size
, pg_offset
,
2751 end_bio_extent_readpage
, mirror_num
,
2756 *bio_flags
= this_bio_flag
;
2761 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2764 pg_offset
+= iosize
;
2768 if (!PageError(page
))
2769 SetPageUptodate(page
);
2775 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2776 get_extent_t
*get_extent
, int mirror_num
)
2778 struct bio
*bio
= NULL
;
2779 unsigned long bio_flags
= 0;
2782 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, mirror_num
,
2785 ret
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
2789 static noinline
void update_nr_written(struct page
*page
,
2790 struct writeback_control
*wbc
,
2791 unsigned long nr_written
)
2793 wbc
->nr_to_write
-= nr_written
;
2794 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2795 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2796 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2800 * the writepage semantics are similar to regular writepage. extent
2801 * records are inserted to lock ranges in the tree, and as dirty areas
2802 * are found, they are marked writeback. Then the lock bits are removed
2803 * and the end_io handler clears the writeback ranges
2805 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2808 struct inode
*inode
= page
->mapping
->host
;
2809 struct extent_page_data
*epd
= data
;
2810 struct extent_io_tree
*tree
= epd
->tree
;
2811 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2813 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2817 u64 last_byte
= i_size_read(inode
);
2821 struct extent_state
*cached_state
= NULL
;
2822 struct extent_map
*em
;
2823 struct block_device
*bdev
;
2826 size_t pg_offset
= 0;
2828 loff_t i_size
= i_size_read(inode
);
2829 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2835 unsigned long nr_written
= 0;
2836 bool fill_delalloc
= true;
2838 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2839 write_flags
= WRITE_SYNC
;
2841 write_flags
= WRITE
;
2843 trace___extent_writepage(page
, inode
, wbc
);
2845 WARN_ON(!PageLocked(page
));
2847 ClearPageError(page
);
2849 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2850 if (page
->index
> end_index
||
2851 (page
->index
== end_index
&& !pg_offset
)) {
2852 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2857 if (page
->index
== end_index
) {
2860 userpage
= kmap_atomic(page
);
2861 memset(userpage
+ pg_offset
, 0,
2862 PAGE_CACHE_SIZE
- pg_offset
);
2863 kunmap_atomic(userpage
);
2864 flush_dcache_page(page
);
2868 set_page_extent_mapped(page
);
2870 if (!tree
->ops
|| !tree
->ops
->fill_delalloc
)
2871 fill_delalloc
= false;
2873 delalloc_start
= start
;
2876 if (!epd
->extent_locked
&& fill_delalloc
) {
2877 u64 delalloc_to_write
= 0;
2879 * make sure the wbc mapping index is at least updated
2882 update_nr_written(page
, wbc
, 0);
2884 while (delalloc_end
< page_end
) {
2885 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2890 if (nr_delalloc
== 0) {
2891 delalloc_start
= delalloc_end
+ 1;
2894 ret
= tree
->ops
->fill_delalloc(inode
, page
,
2899 /* File system has been set read-only */
2905 * delalloc_end is already one less than the total
2906 * length, so we don't subtract one from
2909 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2912 delalloc_start
= delalloc_end
+ 1;
2914 if (wbc
->nr_to_write
< delalloc_to_write
) {
2917 if (delalloc_to_write
< thresh
* 2)
2918 thresh
= delalloc_to_write
;
2919 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2923 /* did the fill delalloc function already unlock and start
2929 * we've unlocked the page, so we can't update
2930 * the mapping's writeback index, just update
2933 wbc
->nr_to_write
-= nr_written
;
2937 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2938 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2941 /* Fixup worker will requeue */
2943 wbc
->pages_skipped
++;
2945 redirty_page_for_writepage(wbc
, page
);
2946 update_nr_written(page
, wbc
, nr_written
);
2954 * we don't want to touch the inode after unlocking the page,
2955 * so we update the mapping writeback index now
2957 update_nr_written(page
, wbc
, nr_written
+ 1);
2960 if (last_byte
<= start
) {
2961 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2962 tree
->ops
->writepage_end_io_hook(page
, start
,
2967 blocksize
= inode
->i_sb
->s_blocksize
;
2969 while (cur
<= end
) {
2970 if (cur
>= last_byte
) {
2971 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2972 tree
->ops
->writepage_end_io_hook(page
, cur
,
2976 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2978 if (IS_ERR_OR_NULL(em
)) {
2983 extent_offset
= cur
- em
->start
;
2984 BUG_ON(extent_map_end(em
) <= cur
);
2986 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2987 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2988 sector
= (em
->block_start
+ extent_offset
) >> 9;
2990 block_start
= em
->block_start
;
2991 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2992 free_extent_map(em
);
2996 * compressed and inline extents are written through other
2999 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
3000 block_start
== EXTENT_MAP_INLINE
) {
3002 * end_io notification does not happen here for
3003 * compressed extents
3005 if (!compressed
&& tree
->ops
&&
3006 tree
->ops
->writepage_end_io_hook
)
3007 tree
->ops
->writepage_end_io_hook(page
, cur
,
3010 else if (compressed
) {
3011 /* we don't want to end_page_writeback on
3012 * a compressed extent. this happens
3019 pg_offset
+= iosize
;
3022 /* leave this out until we have a page_mkwrite call */
3023 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
3024 EXTENT_DIRTY
, 0, NULL
)) {
3026 pg_offset
+= iosize
;
3030 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
3031 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
3039 unsigned long max_nr
= end_index
+ 1;
3041 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
3042 if (!PageWriteback(page
)) {
3043 printk(KERN_ERR
"btrfs warning page %lu not "
3044 "writeback, cur %llu end %llu\n",
3045 page
->index
, (unsigned long long)cur
,
3046 (unsigned long long)end
);
3049 ret
= submit_extent_page(write_flags
, tree
, page
,
3050 sector
, iosize
, pg_offset
,
3051 bdev
, &epd
->bio
, max_nr
,
3052 end_bio_extent_writepage
,
3058 pg_offset
+= iosize
;
3063 /* make sure the mapping tag for page dirty gets cleared */
3064 set_page_writeback(page
);
3065 end_page_writeback(page
);
3071 /* drop our reference on any cached states */
3072 free_extent_state(cached_state
);
3076 static int eb_wait(void *word
)
3082 static void wait_on_extent_buffer_writeback(struct extent_buffer
*eb
)
3084 wait_on_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
, eb_wait
,
3085 TASK_UNINTERRUPTIBLE
);
3088 static int lock_extent_buffer_for_io(struct extent_buffer
*eb
,
3089 struct btrfs_fs_info
*fs_info
,
3090 struct extent_page_data
*epd
)
3092 unsigned long i
, num_pages
;
3096 if (!btrfs_try_tree_write_lock(eb
)) {
3098 flush_write_bio(epd
);
3099 btrfs_tree_lock(eb
);
3102 if (test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
)) {
3103 btrfs_tree_unlock(eb
);
3107 flush_write_bio(epd
);
3111 wait_on_extent_buffer_writeback(eb
);
3112 btrfs_tree_lock(eb
);
3113 if (!test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
))
3115 btrfs_tree_unlock(eb
);
3120 * We need to do this to prevent races in people who check if the eb is
3121 * under IO since we can end up having no IO bits set for a short period
3124 spin_lock(&eb
->refs_lock
);
3125 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3126 set_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3127 spin_unlock(&eb
->refs_lock
);
3128 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
3129 spin_lock(&fs_info
->delalloc_lock
);
3130 if (fs_info
->dirty_metadata_bytes
>= eb
->len
)
3131 fs_info
->dirty_metadata_bytes
-= eb
->len
;
3134 spin_unlock(&fs_info
->delalloc_lock
);
3137 spin_unlock(&eb
->refs_lock
);
3140 btrfs_tree_unlock(eb
);
3145 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3146 for (i
= 0; i
< num_pages
; i
++) {
3147 struct page
*p
= extent_buffer_page(eb
, i
);
3149 if (!trylock_page(p
)) {
3151 flush_write_bio(epd
);
3161 static void end_extent_buffer_writeback(struct extent_buffer
*eb
)
3163 clear_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3164 smp_mb__after_clear_bit();
3165 wake_up_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
);
3168 static void end_bio_extent_buffer_writepage(struct bio
*bio
, int err
)
3170 int uptodate
= err
== 0;
3171 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
3172 struct extent_buffer
*eb
;
3176 struct page
*page
= bvec
->bv_page
;
3179 eb
= (struct extent_buffer
*)page
->private;
3181 done
= atomic_dec_and_test(&eb
->io_pages
);
3183 if (!uptodate
|| test_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
)) {
3184 set_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3185 ClearPageUptodate(page
);
3189 end_page_writeback(page
);
3194 end_extent_buffer_writeback(eb
);
3195 } while (bvec
>= bio
->bi_io_vec
);
3201 static int write_one_eb(struct extent_buffer
*eb
,
3202 struct btrfs_fs_info
*fs_info
,
3203 struct writeback_control
*wbc
,
3204 struct extent_page_data
*epd
)
3206 struct block_device
*bdev
= fs_info
->fs_devices
->latest_bdev
;
3207 u64 offset
= eb
->start
;
3208 unsigned long i
, num_pages
;
3209 unsigned long bio_flags
= 0;
3210 int rw
= (epd
->sync_io
? WRITE_SYNC
: WRITE
);
3213 clear_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3214 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3215 atomic_set(&eb
->io_pages
, num_pages
);
3216 if (btrfs_header_owner(eb
) == BTRFS_TREE_LOG_OBJECTID
)
3217 bio_flags
= EXTENT_BIO_TREE_LOG
;
3219 for (i
= 0; i
< num_pages
; i
++) {
3220 struct page
*p
= extent_buffer_page(eb
, i
);
3222 clear_page_dirty_for_io(p
);
3223 set_page_writeback(p
);
3224 ret
= submit_extent_page(rw
, eb
->tree
, p
, offset
>> 9,
3225 PAGE_CACHE_SIZE
, 0, bdev
, &epd
->bio
,
3226 -1, end_bio_extent_buffer_writepage
,
3227 0, epd
->bio_flags
, bio_flags
);
3228 epd
->bio_flags
= bio_flags
;
3230 set_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3232 if (atomic_sub_and_test(num_pages
- i
, &eb
->io_pages
))
3233 end_extent_buffer_writeback(eb
);
3237 offset
+= PAGE_CACHE_SIZE
;
3238 update_nr_written(p
, wbc
, 1);
3242 if (unlikely(ret
)) {
3243 for (; i
< num_pages
; i
++) {
3244 struct page
*p
= extent_buffer_page(eb
, i
);
3252 int btree_write_cache_pages(struct address_space
*mapping
,
3253 struct writeback_control
*wbc
)
3255 struct extent_io_tree
*tree
= &BTRFS_I(mapping
->host
)->io_tree
;
3256 struct btrfs_fs_info
*fs_info
= BTRFS_I(mapping
->host
)->root
->fs_info
;
3257 struct extent_buffer
*eb
, *prev_eb
= NULL
;
3258 struct extent_page_data epd
= {
3262 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3267 int nr_to_write_done
= 0;
3268 struct pagevec pvec
;
3271 pgoff_t end
; /* Inclusive */
3275 pagevec_init(&pvec
, 0);
3276 if (wbc
->range_cyclic
) {
3277 index
= mapping
->writeback_index
; /* Start from prev offset */
3280 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3281 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3284 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3285 tag
= PAGECACHE_TAG_TOWRITE
;
3287 tag
= PAGECACHE_TAG_DIRTY
;
3289 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3290 tag_pages_for_writeback(mapping
, index
, end
);
3291 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3292 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3293 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3297 for (i
= 0; i
< nr_pages
; i
++) {
3298 struct page
*page
= pvec
.pages
[i
];
3300 if (!PagePrivate(page
))
3303 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3308 spin_lock(&mapping
->private_lock
);
3309 if (!PagePrivate(page
)) {
3310 spin_unlock(&mapping
->private_lock
);
3314 eb
= (struct extent_buffer
*)page
->private;
3317 * Shouldn't happen and normally this would be a BUG_ON
3318 * but no sense in crashing the users box for something
3319 * we can survive anyway.
3322 spin_unlock(&mapping
->private_lock
);
3327 if (eb
== prev_eb
) {
3328 spin_unlock(&mapping
->private_lock
);
3332 ret
= atomic_inc_not_zero(&eb
->refs
);
3333 spin_unlock(&mapping
->private_lock
);
3338 ret
= lock_extent_buffer_for_io(eb
, fs_info
, &epd
);
3340 free_extent_buffer(eb
);
3344 ret
= write_one_eb(eb
, fs_info
, wbc
, &epd
);
3347 free_extent_buffer(eb
);
3350 free_extent_buffer(eb
);
3353 * the filesystem may choose to bump up nr_to_write.
3354 * We have to make sure to honor the new nr_to_write
3357 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3359 pagevec_release(&pvec
);
3362 if (!scanned
&& !done
) {
3364 * We hit the last page and there is more work to be done: wrap
3365 * back to the start of the file
3371 flush_write_bio(&epd
);
3376 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3377 * @mapping: address space structure to write
3378 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3379 * @writepage: function called for each page
3380 * @data: data passed to writepage function
3382 * If a page is already under I/O, write_cache_pages() skips it, even
3383 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3384 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3385 * and msync() need to guarantee that all the data which was dirty at the time
3386 * the call was made get new I/O started against them. If wbc->sync_mode is
3387 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3388 * existing IO to complete.
3390 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
3391 struct address_space
*mapping
,
3392 struct writeback_control
*wbc
,
3393 writepage_t writepage
, void *data
,
3394 void (*flush_fn
)(void *))
3396 struct inode
*inode
= mapping
->host
;
3399 int nr_to_write_done
= 0;
3400 struct pagevec pvec
;
3403 pgoff_t end
; /* Inclusive */
3408 * We have to hold onto the inode so that ordered extents can do their
3409 * work when the IO finishes. The alternative to this is failing to add
3410 * an ordered extent if the igrab() fails there and that is a huge pain
3411 * to deal with, so instead just hold onto the inode throughout the
3412 * writepages operation. If it fails here we are freeing up the inode
3413 * anyway and we'd rather not waste our time writing out stuff that is
3414 * going to be truncated anyway.
3419 pagevec_init(&pvec
, 0);
3420 if (wbc
->range_cyclic
) {
3421 index
= mapping
->writeback_index
; /* Start from prev offset */
3424 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3425 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3428 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3429 tag
= PAGECACHE_TAG_TOWRITE
;
3431 tag
= PAGECACHE_TAG_DIRTY
;
3433 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3434 tag_pages_for_writeback(mapping
, index
, end
);
3435 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3436 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3437 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3441 for (i
= 0; i
< nr_pages
; i
++) {
3442 struct page
*page
= pvec
.pages
[i
];
3445 * At this point we hold neither mapping->tree_lock nor
3446 * lock on the page itself: the page may be truncated or
3447 * invalidated (changing page->mapping to NULL), or even
3448 * swizzled back from swapper_space to tmpfs file
3452 tree
->ops
->write_cache_pages_lock_hook
) {
3453 tree
->ops
->write_cache_pages_lock_hook(page
,
3456 if (!trylock_page(page
)) {
3462 if (unlikely(page
->mapping
!= mapping
)) {
3467 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3473 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3474 if (PageWriteback(page
))
3476 wait_on_page_writeback(page
);
3479 if (PageWriteback(page
) ||
3480 !clear_page_dirty_for_io(page
)) {
3485 ret
= (*writepage
)(page
, wbc
, data
);
3487 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
3495 * the filesystem may choose to bump up nr_to_write.
3496 * We have to make sure to honor the new nr_to_write
3499 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3501 pagevec_release(&pvec
);
3504 if (!scanned
&& !done
) {
3506 * We hit the last page and there is more work to be done: wrap
3507 * back to the start of the file
3513 btrfs_add_delayed_iput(inode
);
3517 static void flush_epd_write_bio(struct extent_page_data
*epd
)
3526 ret
= submit_one_bio(rw
, epd
->bio
, 0, epd
->bio_flags
);
3527 BUG_ON(ret
< 0); /* -ENOMEM */
3532 static noinline
void flush_write_bio(void *data
)
3534 struct extent_page_data
*epd
= data
;
3535 flush_epd_write_bio(epd
);
3538 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
3539 get_extent_t
*get_extent
,
3540 struct writeback_control
*wbc
)
3543 struct extent_page_data epd
= {
3546 .get_extent
= get_extent
,
3548 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3552 ret
= __extent_writepage(page
, wbc
, &epd
);
3554 flush_epd_write_bio(&epd
);
3558 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
3559 u64 start
, u64 end
, get_extent_t
*get_extent
,
3563 struct address_space
*mapping
= inode
->i_mapping
;
3565 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
3568 struct extent_page_data epd
= {
3571 .get_extent
= get_extent
,
3573 .sync_io
= mode
== WB_SYNC_ALL
,
3576 struct writeback_control wbc_writepages
= {
3578 .nr_to_write
= nr_pages
* 2,
3579 .range_start
= start
,
3580 .range_end
= end
+ 1,
3583 while (start
<= end
) {
3584 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
3585 if (clear_page_dirty_for_io(page
))
3586 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
3588 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
3589 tree
->ops
->writepage_end_io_hook(page
, start
,
3590 start
+ PAGE_CACHE_SIZE
- 1,
3594 page_cache_release(page
);
3595 start
+= PAGE_CACHE_SIZE
;
3598 flush_epd_write_bio(&epd
);
3602 int extent_writepages(struct extent_io_tree
*tree
,
3603 struct address_space
*mapping
,
3604 get_extent_t
*get_extent
,
3605 struct writeback_control
*wbc
)
3608 struct extent_page_data epd
= {
3611 .get_extent
= get_extent
,
3613 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3617 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
3618 __extent_writepage
, &epd
,
3620 flush_epd_write_bio(&epd
);
3624 int extent_readpages(struct extent_io_tree
*tree
,
3625 struct address_space
*mapping
,
3626 struct list_head
*pages
, unsigned nr_pages
,
3627 get_extent_t get_extent
)
3629 struct bio
*bio
= NULL
;
3631 unsigned long bio_flags
= 0;
3632 struct page
*pagepool
[16];
3637 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
3638 page
= list_entry(pages
->prev
, struct page
, lru
);
3640 prefetchw(&page
->flags
);
3641 list_del(&page
->lru
);
3642 if (add_to_page_cache_lru(page
, mapping
,
3643 page
->index
, GFP_NOFS
)) {
3644 page_cache_release(page
);
3648 pagepool
[nr
++] = page
;
3649 if (nr
< ARRAY_SIZE(pagepool
))
3651 for (i
= 0; i
< nr
; i
++) {
3652 __extent_read_full_page(tree
, pagepool
[i
], get_extent
,
3653 &bio
, 0, &bio_flags
);
3654 page_cache_release(pagepool
[i
]);
3658 for (i
= 0; i
< nr
; i
++) {
3659 __extent_read_full_page(tree
, pagepool
[i
], get_extent
,
3660 &bio
, 0, &bio_flags
);
3661 page_cache_release(pagepool
[i
]);
3664 BUG_ON(!list_empty(pages
));
3666 return submit_one_bio(READ
, bio
, 0, bio_flags
);
3671 * basic invalidatepage code, this waits on any locked or writeback
3672 * ranges corresponding to the page, and then deletes any extent state
3673 * records from the tree
3675 int extent_invalidatepage(struct extent_io_tree
*tree
,
3676 struct page
*page
, unsigned long offset
)
3678 struct extent_state
*cached_state
= NULL
;
3679 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
3680 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3681 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
3683 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
3687 lock_extent_bits(tree
, start
, end
, 0, &cached_state
);
3688 wait_on_page_writeback(page
);
3689 clear_extent_bit(tree
, start
, end
,
3690 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
3691 EXTENT_DO_ACCOUNTING
,
3692 1, 1, &cached_state
, GFP_NOFS
);
3697 * a helper for releasepage, this tests for areas of the page that
3698 * are locked or under IO and drops the related state bits if it is safe
3701 int try_release_extent_state(struct extent_map_tree
*map
,
3702 struct extent_io_tree
*tree
, struct page
*page
,
3705 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3706 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3709 if (test_range_bit(tree
, start
, end
,
3710 EXTENT_IOBITS
, 0, NULL
))
3713 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
3716 * at this point we can safely clear everything except the
3717 * locked bit and the nodatasum bit
3719 ret
= clear_extent_bit(tree
, start
, end
,
3720 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
3723 /* if clear_extent_bit failed for enomem reasons,
3724 * we can't allow the release to continue.
3735 * a helper for releasepage. As long as there are no locked extents
3736 * in the range corresponding to the page, both state records and extent
3737 * map records are removed
3739 int try_release_extent_mapping(struct extent_map_tree
*map
,
3740 struct extent_io_tree
*tree
, struct page
*page
,
3743 struct extent_map
*em
;
3744 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3745 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3747 if ((mask
& __GFP_WAIT
) &&
3748 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
3750 while (start
<= end
) {
3751 len
= end
- start
+ 1;
3752 write_lock(&map
->lock
);
3753 em
= lookup_extent_mapping(map
, start
, len
);
3755 write_unlock(&map
->lock
);
3758 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
3759 em
->start
!= start
) {
3760 write_unlock(&map
->lock
);
3761 free_extent_map(em
);
3764 if (!test_range_bit(tree
, em
->start
,
3765 extent_map_end(em
) - 1,
3766 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
3768 remove_extent_mapping(map
, em
);
3769 /* once for the rb tree */
3770 free_extent_map(em
);
3772 start
= extent_map_end(em
);
3773 write_unlock(&map
->lock
);
3776 free_extent_map(em
);
3779 return try_release_extent_state(map
, tree
, page
, mask
);
3783 * helper function for fiemap, which doesn't want to see any holes.
3784 * This maps until we find something past 'last'
3786 static struct extent_map
*get_extent_skip_holes(struct inode
*inode
,
3789 get_extent_t
*get_extent
)
3791 u64 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
3792 struct extent_map
*em
;
3799 len
= last
- offset
;
3802 len
= (len
+ sectorsize
- 1) & ~(sectorsize
- 1);
3803 em
= get_extent(inode
, NULL
, 0, offset
, len
, 0);
3804 if (IS_ERR_OR_NULL(em
))
3807 /* if this isn't a hole return it */
3808 if (!test_bit(EXTENT_FLAG_VACANCY
, &em
->flags
) &&
3809 em
->block_start
!= EXTENT_MAP_HOLE
) {
3813 /* this is a hole, advance to the next extent */
3814 offset
= extent_map_end(em
);
3815 free_extent_map(em
);
3822 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3823 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
3827 u64 max
= start
+ len
;
3831 u64 last_for_get_extent
= 0;
3833 u64 isize
= i_size_read(inode
);
3834 struct btrfs_key found_key
;
3835 struct extent_map
*em
= NULL
;
3836 struct extent_state
*cached_state
= NULL
;
3837 struct btrfs_path
*path
;
3838 struct btrfs_file_extent_item
*item
;
3843 unsigned long emflags
;
3848 path
= btrfs_alloc_path();
3851 path
->leave_spinning
= 1;
3853 start
= ALIGN(start
, BTRFS_I(inode
)->root
->sectorsize
);
3854 len
= ALIGN(len
, BTRFS_I(inode
)->root
->sectorsize
);
3857 * lookup the last file extent. We're not using i_size here
3858 * because there might be preallocation past i_size
3860 ret
= btrfs_lookup_file_extent(NULL
, BTRFS_I(inode
)->root
,
3861 path
, btrfs_ino(inode
), -1, 0);
3863 btrfs_free_path(path
);
3868 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3869 struct btrfs_file_extent_item
);
3870 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, path
->slots
[0]);
3871 found_type
= btrfs_key_type(&found_key
);
3873 /* No extents, but there might be delalloc bits */
3874 if (found_key
.objectid
!= btrfs_ino(inode
) ||
3875 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
3876 /* have to trust i_size as the end */
3878 last_for_get_extent
= isize
;
3881 * remember the start of the last extent. There are a
3882 * bunch of different factors that go into the length of the
3883 * extent, so its much less complex to remember where it started
3885 last
= found_key
.offset
;
3886 last_for_get_extent
= last
+ 1;
3888 btrfs_free_path(path
);
3891 * we might have some extents allocated but more delalloc past those
3892 * extents. so, we trust isize unless the start of the last extent is
3897 last_for_get_extent
= isize
;
3900 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
3903 em
= get_extent_skip_holes(inode
, start
, last_for_get_extent
,
3913 u64 offset_in_extent
;
3915 /* break if the extent we found is outside the range */
3916 if (em
->start
>= max
|| extent_map_end(em
) < off
)
3920 * get_extent may return an extent that starts before our
3921 * requested range. We have to make sure the ranges
3922 * we return to fiemap always move forward and don't
3923 * overlap, so adjust the offsets here
3925 em_start
= max(em
->start
, off
);
3928 * record the offset from the start of the extent
3929 * for adjusting the disk offset below
3931 offset_in_extent
= em_start
- em
->start
;
3932 em_end
= extent_map_end(em
);
3933 em_len
= em_end
- em_start
;
3934 emflags
= em
->flags
;
3939 * bump off for our next call to get_extent
3941 off
= extent_map_end(em
);
3945 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
3947 flags
|= FIEMAP_EXTENT_LAST
;
3948 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
3949 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
3950 FIEMAP_EXTENT_NOT_ALIGNED
);
3951 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
3952 flags
|= (FIEMAP_EXTENT_DELALLOC
|
3953 FIEMAP_EXTENT_UNKNOWN
);
3955 disko
= em
->block_start
+ offset_in_extent
;
3957 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
3958 flags
|= FIEMAP_EXTENT_ENCODED
;
3960 free_extent_map(em
);
3962 if ((em_start
>= last
) || em_len
== (u64
)-1 ||
3963 (last
== (u64
)-1 && isize
<= em_end
)) {
3964 flags
|= FIEMAP_EXTENT_LAST
;
3968 /* now scan forward to see if this is really the last extent. */
3969 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
,
3976 flags
|= FIEMAP_EXTENT_LAST
;
3979 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3985 free_extent_map(em
);
3987 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3988 &cached_state
, GFP_NOFS
);
3992 static void __free_extent_buffer(struct extent_buffer
*eb
)
3995 unsigned long flags
;
3996 spin_lock_irqsave(&leak_lock
, flags
);
3997 list_del(&eb
->leak_list
);
3998 spin_unlock_irqrestore(&leak_lock
, flags
);
4000 if (eb
->pages
&& eb
->pages
!= eb
->inline_pages
)
4002 kmem_cache_free(extent_buffer_cache
, eb
);
4005 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
4010 struct extent_buffer
*eb
= NULL
;
4012 unsigned long flags
;
4015 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
4022 rwlock_init(&eb
->lock
);
4023 atomic_set(&eb
->write_locks
, 0);
4024 atomic_set(&eb
->read_locks
, 0);
4025 atomic_set(&eb
->blocking_readers
, 0);
4026 atomic_set(&eb
->blocking_writers
, 0);
4027 atomic_set(&eb
->spinning_readers
, 0);
4028 atomic_set(&eb
->spinning_writers
, 0);
4029 eb
->lock_nested
= 0;
4030 init_waitqueue_head(&eb
->write_lock_wq
);
4031 init_waitqueue_head(&eb
->read_lock_wq
);
4034 spin_lock_irqsave(&leak_lock
, flags
);
4035 list_add(&eb
->leak_list
, &buffers
);
4036 spin_unlock_irqrestore(&leak_lock
, flags
);
4038 spin_lock_init(&eb
->refs_lock
);
4039 atomic_set(&eb
->refs
, 1);
4040 atomic_set(&eb
->io_pages
, 0);
4042 if (len
> MAX_INLINE_EXTENT_BUFFER_SIZE
) {
4043 struct page
**pages
;
4044 int num_pages
= (len
+ PAGE_CACHE_SIZE
- 1) >>
4046 pages
= kzalloc(num_pages
, mask
);
4048 __free_extent_buffer(eb
);
4053 eb
->pages
= eb
->inline_pages
;
4059 struct extent_buffer
*btrfs_clone_extent_buffer(struct extent_buffer
*src
)
4063 struct extent_buffer
*new;
4064 unsigned long num_pages
= num_extent_pages(src
->start
, src
->len
);
4066 new = __alloc_extent_buffer(NULL
, src
->start
, src
->len
, GFP_ATOMIC
);
4070 for (i
= 0; i
< num_pages
; i
++) {
4071 p
= alloc_page(GFP_ATOMIC
);
4073 attach_extent_buffer_page(new, p
);
4074 WARN_ON(PageDirty(p
));
4079 copy_extent_buffer(new, src
, 0, 0, src
->len
);
4080 set_bit(EXTENT_BUFFER_UPTODATE
, &new->bflags
);
4081 set_bit(EXTENT_BUFFER_DUMMY
, &new->bflags
);
4086 struct extent_buffer
*alloc_dummy_extent_buffer(u64 start
, unsigned long len
)
4088 struct extent_buffer
*eb
;
4089 unsigned long num_pages
= num_extent_pages(0, len
);
4092 eb
= __alloc_extent_buffer(NULL
, start
, len
, GFP_ATOMIC
);
4096 for (i
= 0; i
< num_pages
; i
++) {
4097 eb
->pages
[i
] = alloc_page(GFP_ATOMIC
);
4101 set_extent_buffer_uptodate(eb
);
4102 btrfs_set_header_nritems(eb
, 0);
4103 set_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
);
4108 __free_page(eb
->pages
[i
- 1]);
4109 __free_extent_buffer(eb
);
4113 static int extent_buffer_under_io(struct extent_buffer
*eb
)
4115 return (atomic_read(&eb
->io_pages
) ||
4116 test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
) ||
4117 test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4121 * Helper for releasing extent buffer page.
4123 static void btrfs_release_extent_buffer_page(struct extent_buffer
*eb
,
4124 unsigned long start_idx
)
4126 unsigned long index
;
4127 unsigned long num_pages
;
4129 int mapped
= !test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
);
4131 BUG_ON(extent_buffer_under_io(eb
));
4133 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4134 index
= start_idx
+ num_pages
;
4135 if (start_idx
>= index
)
4140 page
= extent_buffer_page(eb
, index
);
4141 if (page
&& mapped
) {
4142 spin_lock(&page
->mapping
->private_lock
);
4144 * We do this since we'll remove the pages after we've
4145 * removed the eb from the radix tree, so we could race
4146 * and have this page now attached to the new eb. So
4147 * only clear page_private if it's still connected to
4150 if (PagePrivate(page
) &&
4151 page
->private == (unsigned long)eb
) {
4152 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4153 BUG_ON(PageDirty(page
));
4154 BUG_ON(PageWriteback(page
));
4156 * We need to make sure we haven't be attached
4159 ClearPagePrivate(page
);
4160 set_page_private(page
, 0);
4161 /* One for the page private */
4162 page_cache_release(page
);
4164 spin_unlock(&page
->mapping
->private_lock
);
4168 /* One for when we alloced the page */
4169 page_cache_release(page
);
4171 } while (index
!= start_idx
);
4175 * Helper for releasing the extent buffer.
4177 static inline void btrfs_release_extent_buffer(struct extent_buffer
*eb
)
4179 btrfs_release_extent_buffer_page(eb
, 0);
4180 __free_extent_buffer(eb
);
4183 static void check_buffer_tree_ref(struct extent_buffer
*eb
)
4185 /* the ref bit is tricky. We have to make sure it is set
4186 * if we have the buffer dirty. Otherwise the
4187 * code to free a buffer can end up dropping a dirty
4190 * Once the ref bit is set, it won't go away while the
4191 * buffer is dirty or in writeback, and it also won't
4192 * go away while we have the reference count on the
4195 * We can't just set the ref bit without bumping the
4196 * ref on the eb because free_extent_buffer might
4197 * see the ref bit and try to clear it. If this happens
4198 * free_extent_buffer might end up dropping our original
4199 * ref by mistake and freeing the page before we are able
4200 * to add one more ref.
4202 * So bump the ref count first, then set the bit. If someone
4203 * beat us to it, drop the ref we added.
4205 spin_lock(&eb
->refs_lock
);
4206 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4207 atomic_inc(&eb
->refs
);
4208 spin_unlock(&eb
->refs_lock
);
4211 static void mark_extent_buffer_accessed(struct extent_buffer
*eb
)
4213 unsigned long num_pages
, i
;
4215 check_buffer_tree_ref(eb
);
4217 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4218 for (i
= 0; i
< num_pages
; i
++) {
4219 struct page
*p
= extent_buffer_page(eb
, i
);
4220 mark_page_accessed(p
);
4224 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
4225 u64 start
, unsigned long len
)
4227 unsigned long num_pages
= num_extent_pages(start
, len
);
4229 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
4230 struct extent_buffer
*eb
;
4231 struct extent_buffer
*exists
= NULL
;
4233 struct address_space
*mapping
= tree
->mapping
;
4238 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4239 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4241 mark_extent_buffer_accessed(eb
);
4246 eb
= __alloc_extent_buffer(tree
, start
, len
, GFP_NOFS
);
4250 for (i
= 0; i
< num_pages
; i
++, index
++) {
4251 p
= find_or_create_page(mapping
, index
, GFP_NOFS
);
4255 spin_lock(&mapping
->private_lock
);
4256 if (PagePrivate(p
)) {
4258 * We could have already allocated an eb for this page
4259 * and attached one so lets see if we can get a ref on
4260 * the existing eb, and if we can we know it's good and
4261 * we can just return that one, else we know we can just
4262 * overwrite page->private.
4264 exists
= (struct extent_buffer
*)p
->private;
4265 if (atomic_inc_not_zero(&exists
->refs
)) {
4266 spin_unlock(&mapping
->private_lock
);
4268 page_cache_release(p
);
4269 mark_extent_buffer_accessed(exists
);
4274 * Do this so attach doesn't complain and we need to
4275 * drop the ref the old guy had.
4277 ClearPagePrivate(p
);
4278 WARN_ON(PageDirty(p
));
4279 page_cache_release(p
);
4281 attach_extent_buffer_page(eb
, p
);
4282 spin_unlock(&mapping
->private_lock
);
4283 WARN_ON(PageDirty(p
));
4284 mark_page_accessed(p
);
4286 if (!PageUptodate(p
))
4290 * see below about how we avoid a nasty race with release page
4291 * and why we unlock later
4295 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4297 ret
= radix_tree_preload(GFP_NOFS
& ~__GFP_HIGHMEM
);
4301 spin_lock(&tree
->buffer_lock
);
4302 ret
= radix_tree_insert(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
, eb
);
4303 if (ret
== -EEXIST
) {
4304 exists
= radix_tree_lookup(&tree
->buffer
,
4305 start
>> PAGE_CACHE_SHIFT
);
4306 if (!atomic_inc_not_zero(&exists
->refs
)) {
4307 spin_unlock(&tree
->buffer_lock
);
4308 radix_tree_preload_end();
4312 spin_unlock(&tree
->buffer_lock
);
4313 radix_tree_preload_end();
4314 mark_extent_buffer_accessed(exists
);
4317 /* add one reference for the tree */
4318 check_buffer_tree_ref(eb
);
4319 spin_unlock(&tree
->buffer_lock
);
4320 radix_tree_preload_end();
4323 * there is a race where release page may have
4324 * tried to find this extent buffer in the radix
4325 * but failed. It will tell the VM it is safe to
4326 * reclaim the, and it will clear the page private bit.
4327 * We must make sure to set the page private bit properly
4328 * after the extent buffer is in the radix tree so
4329 * it doesn't get lost
4331 SetPageChecked(eb
->pages
[0]);
4332 for (i
= 1; i
< num_pages
; i
++) {
4333 p
= extent_buffer_page(eb
, i
);
4334 ClearPageChecked(p
);
4337 unlock_page(eb
->pages
[0]);
4341 for (i
= 0; i
< num_pages
; i
++) {
4343 unlock_page(eb
->pages
[i
]);
4346 WARN_ON(!atomic_dec_and_test(&eb
->refs
));
4347 btrfs_release_extent_buffer(eb
);
4351 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
4352 u64 start
, unsigned long len
)
4354 struct extent_buffer
*eb
;
4357 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4358 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4360 mark_extent_buffer_accessed(eb
);
4368 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head
*head
)
4370 struct extent_buffer
*eb
=
4371 container_of(head
, struct extent_buffer
, rcu_head
);
4373 __free_extent_buffer(eb
);
4376 /* Expects to have eb->eb_lock already held */
4377 static int release_extent_buffer(struct extent_buffer
*eb
, gfp_t mask
)
4379 WARN_ON(atomic_read(&eb
->refs
) == 0);
4380 if (atomic_dec_and_test(&eb
->refs
)) {
4381 if (test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
)) {
4382 spin_unlock(&eb
->refs_lock
);
4384 struct extent_io_tree
*tree
= eb
->tree
;
4386 spin_unlock(&eb
->refs_lock
);
4388 spin_lock(&tree
->buffer_lock
);
4389 radix_tree_delete(&tree
->buffer
,
4390 eb
->start
>> PAGE_CACHE_SHIFT
);
4391 spin_unlock(&tree
->buffer_lock
);
4394 /* Should be safe to release our pages at this point */
4395 btrfs_release_extent_buffer_page(eb
, 0);
4396 call_rcu(&eb
->rcu_head
, btrfs_release_extent_buffer_rcu
);
4399 spin_unlock(&eb
->refs_lock
);
4404 void free_extent_buffer(struct extent_buffer
*eb
)
4409 spin_lock(&eb
->refs_lock
);
4410 if (atomic_read(&eb
->refs
) == 2 &&
4411 test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
))
4412 atomic_dec(&eb
->refs
);
4414 if (atomic_read(&eb
->refs
) == 2 &&
4415 test_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
) &&
4416 !extent_buffer_under_io(eb
) &&
4417 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4418 atomic_dec(&eb
->refs
);
4421 * I know this is terrible, but it's temporary until we stop tracking
4422 * the uptodate bits and such for the extent buffers.
4424 release_extent_buffer(eb
, GFP_ATOMIC
);
4427 void free_extent_buffer_stale(struct extent_buffer
*eb
)
4432 spin_lock(&eb
->refs_lock
);
4433 set_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
);
4435 if (atomic_read(&eb
->refs
) == 2 && !extent_buffer_under_io(eb
) &&
4436 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4437 atomic_dec(&eb
->refs
);
4438 release_extent_buffer(eb
, GFP_NOFS
);
4441 void clear_extent_buffer_dirty(struct extent_buffer
*eb
)
4444 unsigned long num_pages
;
4447 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4449 for (i
= 0; i
< num_pages
; i
++) {
4450 page
= extent_buffer_page(eb
, i
);
4451 if (!PageDirty(page
))
4455 WARN_ON(!PagePrivate(page
));
4457 clear_page_dirty_for_io(page
);
4458 spin_lock_irq(&page
->mapping
->tree_lock
);
4459 if (!PageDirty(page
)) {
4460 radix_tree_tag_clear(&page
->mapping
->page_tree
,
4462 PAGECACHE_TAG_DIRTY
);
4464 spin_unlock_irq(&page
->mapping
->tree_lock
);
4465 ClearPageError(page
);
4468 WARN_ON(atomic_read(&eb
->refs
) == 0);
4471 int set_extent_buffer_dirty(struct extent_buffer
*eb
)
4474 unsigned long num_pages
;
4477 check_buffer_tree_ref(eb
);
4479 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
4481 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4482 WARN_ON(atomic_read(&eb
->refs
) == 0);
4483 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
));
4485 for (i
= 0; i
< num_pages
; i
++)
4486 set_page_dirty(extent_buffer_page(eb
, i
));
4490 static int range_straddles_pages(u64 start
, u64 len
)
4492 if (len
< PAGE_CACHE_SIZE
)
4494 if (start
& (PAGE_CACHE_SIZE
- 1))
4496 if ((start
+ len
) & (PAGE_CACHE_SIZE
- 1))
4501 int clear_extent_buffer_uptodate(struct extent_buffer
*eb
)
4505 unsigned long num_pages
;
4507 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4508 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4509 for (i
= 0; i
< num_pages
; i
++) {
4510 page
= extent_buffer_page(eb
, i
);
4512 ClearPageUptodate(page
);
4517 int set_extent_buffer_uptodate(struct extent_buffer
*eb
)
4521 unsigned long num_pages
;
4523 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4524 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4525 for (i
= 0; i
< num_pages
; i
++) {
4526 page
= extent_buffer_page(eb
, i
);
4527 SetPageUptodate(page
);
4532 int extent_range_uptodate(struct extent_io_tree
*tree
,
4537 int pg_uptodate
= 1;
4539 unsigned long index
;
4541 if (range_straddles_pages(start
, end
- start
+ 1)) {
4542 ret
= test_range_bit(tree
, start
, end
,
4543 EXTENT_UPTODATE
, 1, NULL
);
4547 while (start
<= end
) {
4548 index
= start
>> PAGE_CACHE_SHIFT
;
4549 page
= find_get_page(tree
->mapping
, index
);
4552 uptodate
= PageUptodate(page
);
4553 page_cache_release(page
);
4558 start
+= PAGE_CACHE_SIZE
;
4563 int extent_buffer_uptodate(struct extent_buffer
*eb
)
4565 return test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4568 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
4569 struct extent_buffer
*eb
, u64 start
, int wait
,
4570 get_extent_t
*get_extent
, int mirror_num
)
4573 unsigned long start_i
;
4577 int locked_pages
= 0;
4578 int all_uptodate
= 1;
4579 unsigned long num_pages
;
4580 unsigned long num_reads
= 0;
4581 struct bio
*bio
= NULL
;
4582 unsigned long bio_flags
= 0;
4584 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
4588 WARN_ON(start
< eb
->start
);
4589 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
4590 (eb
->start
>> PAGE_CACHE_SHIFT
);
4595 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4596 for (i
= start_i
; i
< num_pages
; i
++) {
4597 page
= extent_buffer_page(eb
, i
);
4598 if (wait
== WAIT_NONE
) {
4599 if (!trylock_page(page
))
4605 if (!PageUptodate(page
)) {
4612 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4616 clear_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
4617 eb
->read_mirror
= 0;
4618 atomic_set(&eb
->io_pages
, num_reads
);
4619 for (i
= start_i
; i
< num_pages
; i
++) {
4620 page
= extent_buffer_page(eb
, i
);
4621 if (!PageUptodate(page
)) {
4622 ClearPageError(page
);
4623 err
= __extent_read_full_page(tree
, page
,
4625 mirror_num
, &bio_flags
);
4634 err
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
4639 if (ret
|| wait
!= WAIT_COMPLETE
)
4642 for (i
= start_i
; i
< num_pages
; i
++) {
4643 page
= extent_buffer_page(eb
, i
);
4644 wait_on_page_locked(page
);
4645 if (!PageUptodate(page
))
4653 while (locked_pages
> 0) {
4654 page
= extent_buffer_page(eb
, i
);
4662 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
4663 unsigned long start
,
4670 char *dst
= (char *)dstv
;
4671 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4672 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4674 WARN_ON(start
> eb
->len
);
4675 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4677 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4680 page
= extent_buffer_page(eb
, i
);
4682 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4683 kaddr
= page_address(page
);
4684 memcpy(dst
, kaddr
+ offset
, cur
);
4693 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
4694 unsigned long min_len
, char **map
,
4695 unsigned long *map_start
,
4696 unsigned long *map_len
)
4698 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
4701 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4702 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4703 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
4710 offset
= start_offset
;
4714 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
4717 if (start
+ min_len
> eb
->len
) {
4718 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
4719 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
4720 eb
->len
, start
, min_len
);
4725 p
= extent_buffer_page(eb
, i
);
4726 kaddr
= page_address(p
);
4727 *map
= kaddr
+ offset
;
4728 *map_len
= PAGE_CACHE_SIZE
- offset
;
4732 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
4733 unsigned long start
,
4740 char *ptr
= (char *)ptrv
;
4741 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4742 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4745 WARN_ON(start
> eb
->len
);
4746 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4748 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4751 page
= extent_buffer_page(eb
, i
);
4753 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4755 kaddr
= page_address(page
);
4756 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
4768 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
4769 unsigned long start
, unsigned long len
)
4775 char *src
= (char *)srcv
;
4776 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4777 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4779 WARN_ON(start
> eb
->len
);
4780 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4782 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4785 page
= extent_buffer_page(eb
, i
);
4786 WARN_ON(!PageUptodate(page
));
4788 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4789 kaddr
= page_address(page
);
4790 memcpy(kaddr
+ offset
, src
, cur
);
4799 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
4800 unsigned long start
, unsigned long len
)
4806 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4807 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4809 WARN_ON(start
> eb
->len
);
4810 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4812 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4815 page
= extent_buffer_page(eb
, i
);
4816 WARN_ON(!PageUptodate(page
));
4818 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4819 kaddr
= page_address(page
);
4820 memset(kaddr
+ offset
, c
, cur
);
4828 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
4829 unsigned long dst_offset
, unsigned long src_offset
,
4832 u64 dst_len
= dst
->len
;
4837 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4838 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4840 WARN_ON(src
->len
!= dst_len
);
4842 offset
= (start_offset
+ dst_offset
) &
4843 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4846 page
= extent_buffer_page(dst
, i
);
4847 WARN_ON(!PageUptodate(page
));
4849 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
4851 kaddr
= page_address(page
);
4852 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
4861 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
4862 unsigned long dst_off
, unsigned long src_off
,
4865 char *dst_kaddr
= page_address(dst_page
);
4866 if (dst_page
== src_page
) {
4867 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
4869 char *src_kaddr
= page_address(src_page
);
4870 char *p
= dst_kaddr
+ dst_off
+ len
;
4871 char *s
= src_kaddr
+ src_off
+ len
;
4878 static inline bool areas_overlap(unsigned long src
, unsigned long dst
, unsigned long len
)
4880 unsigned long distance
= (src
> dst
) ? src
- dst
: dst
- src
;
4881 return distance
< len
;
4884 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
4885 unsigned long dst_off
, unsigned long src_off
,
4888 char *dst_kaddr
= page_address(dst_page
);
4890 int must_memmove
= 0;
4892 if (dst_page
!= src_page
) {
4893 src_kaddr
= page_address(src_page
);
4895 src_kaddr
= dst_kaddr
;
4896 if (areas_overlap(src_off
, dst_off
, len
))
4901 memmove(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4903 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4906 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4907 unsigned long src_offset
, unsigned long len
)
4910 size_t dst_off_in_page
;
4911 size_t src_off_in_page
;
4912 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4913 unsigned long dst_i
;
4914 unsigned long src_i
;
4916 if (src_offset
+ len
> dst
->len
) {
4917 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4918 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
4921 if (dst_offset
+ len
> dst
->len
) {
4922 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4923 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
4928 dst_off_in_page
= (start_offset
+ dst_offset
) &
4929 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4930 src_off_in_page
= (start_offset
+ src_offset
) &
4931 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4933 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4934 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
4936 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
4938 cur
= min_t(unsigned long, cur
,
4939 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
4941 copy_pages(extent_buffer_page(dst
, dst_i
),
4942 extent_buffer_page(dst
, src_i
),
4943 dst_off_in_page
, src_off_in_page
, cur
);
4951 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4952 unsigned long src_offset
, unsigned long len
)
4955 size_t dst_off_in_page
;
4956 size_t src_off_in_page
;
4957 unsigned long dst_end
= dst_offset
+ len
- 1;
4958 unsigned long src_end
= src_offset
+ len
- 1;
4959 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4960 unsigned long dst_i
;
4961 unsigned long src_i
;
4963 if (src_offset
+ len
> dst
->len
) {
4964 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4965 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
4968 if (dst_offset
+ len
> dst
->len
) {
4969 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4970 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
4973 if (dst_offset
< src_offset
) {
4974 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
4978 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
4979 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
4981 dst_off_in_page
= (start_offset
+ dst_end
) &
4982 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4983 src_off_in_page
= (start_offset
+ src_end
) &
4984 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4986 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
4987 cur
= min(cur
, dst_off_in_page
+ 1);
4988 move_pages(extent_buffer_page(dst
, dst_i
),
4989 extent_buffer_page(dst
, src_i
),
4990 dst_off_in_page
- cur
+ 1,
4991 src_off_in_page
- cur
+ 1, cur
);
4999 int try_release_extent_buffer(struct page
*page
, gfp_t mask
)
5001 struct extent_buffer
*eb
;
5004 * We need to make sure noboody is attaching this page to an eb right
5007 spin_lock(&page
->mapping
->private_lock
);
5008 if (!PagePrivate(page
)) {
5009 spin_unlock(&page
->mapping
->private_lock
);
5013 eb
= (struct extent_buffer
*)page
->private;
5017 * This is a little awful but should be ok, we need to make sure that
5018 * the eb doesn't disappear out from under us while we're looking at
5021 spin_lock(&eb
->refs_lock
);
5022 if (atomic_read(&eb
->refs
) != 1 || extent_buffer_under_io(eb
)) {
5023 spin_unlock(&eb
->refs_lock
);
5024 spin_unlock(&page
->mapping
->private_lock
);
5027 spin_unlock(&page
->mapping
->private_lock
);
5029 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
5033 * If tree ref isn't set then we know the ref on this eb is a real ref,
5034 * so just return, this page will likely be freed soon anyway.
5036 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
)) {
5037 spin_unlock(&eb
->refs_lock
);
5041 return release_extent_buffer(eb
, mask
);