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
;
49 /* tells writepage not to lock the state bits for this range
50 * it still does the unlocking
52 unsigned int extent_locked
:1;
54 /* tells the submit_bio code to use a WRITE_SYNC */
55 unsigned int sync_io
:1;
58 static noinline
void flush_write_bio(void *data
);
59 static inline struct btrfs_fs_info
*
60 tree_fs_info(struct extent_io_tree
*tree
)
62 return btrfs_sb(tree
->mapping
->host
->i_sb
);
65 int __init
extent_io_init(void)
67 extent_state_cache
= kmem_cache_create("extent_state",
68 sizeof(struct extent_state
), 0,
69 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
70 if (!extent_state_cache
)
73 extent_buffer_cache
= kmem_cache_create("extent_buffers",
74 sizeof(struct extent_buffer
), 0,
75 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
76 if (!extent_buffer_cache
)
77 goto free_state_cache
;
81 kmem_cache_destroy(extent_state_cache
);
85 void extent_io_exit(void)
87 struct extent_state
*state
;
88 struct extent_buffer
*eb
;
90 while (!list_empty(&states
)) {
91 state
= list_entry(states
.next
, struct extent_state
, leak_list
);
92 printk(KERN_ERR
"btrfs state leak: start %llu end %llu "
93 "state %lu in tree %p refs %d\n",
94 (unsigned long long)state
->start
,
95 (unsigned long long)state
->end
,
96 state
->state
, state
->tree
, atomic_read(&state
->refs
));
97 list_del(&state
->leak_list
);
98 kmem_cache_free(extent_state_cache
, state
);
102 while (!list_empty(&buffers
)) {
103 eb
= list_entry(buffers
.next
, struct extent_buffer
, leak_list
);
104 printk(KERN_ERR
"btrfs buffer leak start %llu len %lu "
105 "refs %d\n", (unsigned long long)eb
->start
,
106 eb
->len
, atomic_read(&eb
->refs
));
107 list_del(&eb
->leak_list
);
108 kmem_cache_free(extent_buffer_cache
, eb
);
110 if (extent_state_cache
)
111 kmem_cache_destroy(extent_state_cache
);
112 if (extent_buffer_cache
)
113 kmem_cache_destroy(extent_buffer_cache
);
116 void extent_io_tree_init(struct extent_io_tree
*tree
,
117 struct address_space
*mapping
)
119 tree
->state
= RB_ROOT
;
120 INIT_RADIX_TREE(&tree
->buffer
, GFP_ATOMIC
);
122 tree
->dirty_bytes
= 0;
123 spin_lock_init(&tree
->lock
);
124 spin_lock_init(&tree
->buffer_lock
);
125 tree
->mapping
= mapping
;
128 static struct extent_state
*alloc_extent_state(gfp_t mask
)
130 struct extent_state
*state
;
135 state
= kmem_cache_alloc(extent_state_cache
, mask
);
142 spin_lock_irqsave(&leak_lock
, flags
);
143 list_add(&state
->leak_list
, &states
);
144 spin_unlock_irqrestore(&leak_lock
, flags
);
146 atomic_set(&state
->refs
, 1);
147 init_waitqueue_head(&state
->wq
);
148 trace_alloc_extent_state(state
, mask
, _RET_IP_
);
152 void free_extent_state(struct extent_state
*state
)
156 if (atomic_dec_and_test(&state
->refs
)) {
160 WARN_ON(state
->tree
);
162 spin_lock_irqsave(&leak_lock
, flags
);
163 list_del(&state
->leak_list
);
164 spin_unlock_irqrestore(&leak_lock
, flags
);
166 trace_free_extent_state(state
, _RET_IP_
);
167 kmem_cache_free(extent_state_cache
, state
);
171 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
172 struct rb_node
*node
)
174 struct rb_node
**p
= &root
->rb_node
;
175 struct rb_node
*parent
= NULL
;
176 struct tree_entry
*entry
;
180 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
182 if (offset
< entry
->start
)
184 else if (offset
> entry
->end
)
190 rb_link_node(node
, parent
, p
);
191 rb_insert_color(node
, root
);
195 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
196 struct rb_node
**prev_ret
,
197 struct rb_node
**next_ret
)
199 struct rb_root
*root
= &tree
->state
;
200 struct rb_node
*n
= root
->rb_node
;
201 struct rb_node
*prev
= NULL
;
202 struct rb_node
*orig_prev
= NULL
;
203 struct tree_entry
*entry
;
204 struct tree_entry
*prev_entry
= NULL
;
207 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
211 if (offset
< entry
->start
)
213 else if (offset
> entry
->end
)
221 while (prev
&& offset
> prev_entry
->end
) {
222 prev
= rb_next(prev
);
223 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
230 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
231 while (prev
&& offset
< prev_entry
->start
) {
232 prev
= rb_prev(prev
);
233 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
240 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
243 struct rb_node
*prev
= NULL
;
246 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
252 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
253 struct extent_state
*other
)
255 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
256 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
261 * utility function to look for merge candidates inside a given range.
262 * Any extents with matching state are merged together into a single
263 * extent in the tree. Extents with EXTENT_IO in their state field
264 * are not merged because the end_io handlers need to be able to do
265 * operations on them without sleeping (or doing allocations/splits).
267 * This should be called with the tree lock held.
269 static void merge_state(struct extent_io_tree
*tree
,
270 struct extent_state
*state
)
272 struct extent_state
*other
;
273 struct rb_node
*other_node
;
275 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
278 other_node
= rb_prev(&state
->rb_node
);
280 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
281 if (other
->end
== state
->start
- 1 &&
282 other
->state
== state
->state
) {
283 merge_cb(tree
, state
, other
);
284 state
->start
= other
->start
;
286 rb_erase(&other
->rb_node
, &tree
->state
);
287 free_extent_state(other
);
290 other_node
= rb_next(&state
->rb_node
);
292 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
293 if (other
->start
== state
->end
+ 1 &&
294 other
->state
== state
->state
) {
295 merge_cb(tree
, state
, other
);
296 state
->end
= other
->end
;
298 rb_erase(&other
->rb_node
, &tree
->state
);
299 free_extent_state(other
);
304 static void set_state_cb(struct extent_io_tree
*tree
,
305 struct extent_state
*state
, int *bits
)
307 if (tree
->ops
&& tree
->ops
->set_bit_hook
)
308 tree
->ops
->set_bit_hook(tree
->mapping
->host
, state
, bits
);
311 static void clear_state_cb(struct extent_io_tree
*tree
,
312 struct extent_state
*state
, int *bits
)
314 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
315 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
, bits
);
318 static void set_state_bits(struct extent_io_tree
*tree
,
319 struct extent_state
*state
, int *bits
);
322 * insert an extent_state struct into the tree. 'bits' are set on the
323 * struct before it is inserted.
325 * This may return -EEXIST if the extent is already there, in which case the
326 * state struct is freed.
328 * The tree lock is not taken internally. This is a utility function and
329 * probably isn't what you want to call (see set/clear_extent_bit).
331 static int insert_state(struct extent_io_tree
*tree
,
332 struct extent_state
*state
, u64 start
, u64 end
,
335 struct rb_node
*node
;
338 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
339 (unsigned long long)end
,
340 (unsigned long long)start
);
343 state
->start
= start
;
346 set_state_bits(tree
, state
, bits
);
348 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
350 struct extent_state
*found
;
351 found
= rb_entry(node
, struct extent_state
, rb_node
);
352 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
353 "%llu %llu\n", (unsigned long long)found
->start
,
354 (unsigned long long)found
->end
,
355 (unsigned long long)start
, (unsigned long long)end
);
359 merge_state(tree
, state
);
363 static void split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
366 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
367 tree
->ops
->split_extent_hook(tree
->mapping
->host
, orig
, split
);
371 * split a given extent state struct in two, inserting the preallocated
372 * struct 'prealloc' as the newly created second half. 'split' indicates an
373 * offset inside 'orig' where it should be split.
376 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
377 * are two extent state structs in the tree:
378 * prealloc: [orig->start, split - 1]
379 * orig: [ split, orig->end ]
381 * The tree locks are not taken by this function. They need to be held
384 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
385 struct extent_state
*prealloc
, u64 split
)
387 struct rb_node
*node
;
389 split_cb(tree
, orig
, split
);
391 prealloc
->start
= orig
->start
;
392 prealloc
->end
= split
- 1;
393 prealloc
->state
= orig
->state
;
396 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
398 free_extent_state(prealloc
);
401 prealloc
->tree
= tree
;
405 static struct extent_state
*next_state(struct extent_state
*state
)
407 struct rb_node
*next
= rb_next(&state
->rb_node
);
409 return rb_entry(next
, struct extent_state
, rb_node
);
415 * utility function to clear some bits in an extent state struct.
416 * it will optionally wake up any one waiting on this state (wake == 1).
418 * If no bits are set on the state struct after clearing things, the
419 * struct is freed and removed from the tree
421 static struct extent_state
*clear_state_bit(struct extent_io_tree
*tree
,
422 struct extent_state
*state
,
425 struct extent_state
*next
;
426 int bits_to_clear
= *bits
& ~EXTENT_CTLBITS
;
428 if ((bits_to_clear
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
429 u64 range
= state
->end
- state
->start
+ 1;
430 WARN_ON(range
> tree
->dirty_bytes
);
431 tree
->dirty_bytes
-= range
;
433 clear_state_cb(tree
, state
, bits
);
434 state
->state
&= ~bits_to_clear
;
437 if (state
->state
== 0) {
438 next
= next_state(state
);
440 rb_erase(&state
->rb_node
, &tree
->state
);
442 free_extent_state(state
);
447 merge_state(tree
, state
);
448 next
= next_state(state
);
453 static struct extent_state
*
454 alloc_extent_state_atomic(struct extent_state
*prealloc
)
457 prealloc
= alloc_extent_state(GFP_ATOMIC
);
462 void extent_io_tree_panic(struct extent_io_tree
*tree
, int err
)
464 btrfs_panic(tree_fs_info(tree
), err
, "Locking error: "
465 "Extent tree was modified by another "
466 "thread while locked.");
470 * clear some bits on a range in the tree. This may require splitting
471 * or inserting elements in the tree, so the gfp mask is used to
472 * indicate which allocations or sleeping are allowed.
474 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
475 * the given range from the tree regardless of state (ie for truncate).
477 * the range [start, end] is inclusive.
479 * This takes the tree lock, and returns 0 on success and < 0 on error.
481 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
482 int bits
, int wake
, int delete,
483 struct extent_state
**cached_state
,
486 struct extent_state
*state
;
487 struct extent_state
*cached
;
488 struct extent_state
*prealloc
= NULL
;
489 struct rb_node
*node
;
495 bits
|= ~EXTENT_CTLBITS
;
496 bits
|= EXTENT_FIRST_DELALLOC
;
498 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
501 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
502 prealloc
= alloc_extent_state(mask
);
507 spin_lock(&tree
->lock
);
509 cached
= *cached_state
;
512 *cached_state
= NULL
;
516 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
517 cached
->end
> start
) {
519 atomic_dec(&cached
->refs
);
524 free_extent_state(cached
);
527 * this search will find the extents that end after
530 node
= tree_search(tree
, start
);
533 state
= rb_entry(node
, struct extent_state
, rb_node
);
535 if (state
->start
> end
)
537 WARN_ON(state
->end
< start
);
538 last_end
= state
->end
;
540 /* the state doesn't have the wanted bits, go ahead */
541 if (!(state
->state
& bits
)) {
542 state
= next_state(state
);
547 * | ---- desired range ---- |
549 * | ------------- state -------------- |
551 * We need to split the extent we found, and may flip
552 * bits on second half.
554 * If the extent we found extends past our range, we
555 * just split and search again. It'll get split again
556 * the next time though.
558 * If the extent we found is inside our range, we clear
559 * the desired bit on it.
562 if (state
->start
< start
) {
563 prealloc
= alloc_extent_state_atomic(prealloc
);
565 err
= split_state(tree
, state
, prealloc
, start
);
567 extent_io_tree_panic(tree
, err
);
572 if (state
->end
<= end
) {
573 state
= clear_state_bit(tree
, state
, &bits
, wake
);
579 * | ---- desired range ---- |
581 * We need to split the extent, and clear the bit
584 if (state
->start
<= end
&& state
->end
> end
) {
585 prealloc
= alloc_extent_state_atomic(prealloc
);
587 err
= split_state(tree
, state
, prealloc
, end
+ 1);
589 extent_io_tree_panic(tree
, err
);
594 clear_state_bit(tree
, prealloc
, &bits
, wake
);
600 state
= clear_state_bit(tree
, state
, &bits
, wake
);
602 if (last_end
== (u64
)-1)
604 start
= last_end
+ 1;
605 if (start
<= end
&& state
&& !need_resched())
610 spin_unlock(&tree
->lock
);
612 free_extent_state(prealloc
);
619 spin_unlock(&tree
->lock
);
620 if (mask
& __GFP_WAIT
)
625 static void wait_on_state(struct extent_io_tree
*tree
,
626 struct extent_state
*state
)
627 __releases(tree
->lock
)
628 __acquires(tree
->lock
)
631 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
632 spin_unlock(&tree
->lock
);
634 spin_lock(&tree
->lock
);
635 finish_wait(&state
->wq
, &wait
);
639 * waits for one or more bits to clear on a range in the state tree.
640 * The range [start, end] is inclusive.
641 * The tree lock is taken by this function
643 void wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
645 struct extent_state
*state
;
646 struct rb_node
*node
;
648 spin_lock(&tree
->lock
);
652 * this search will find all the extents that end after
655 node
= tree_search(tree
, start
);
659 state
= rb_entry(node
, struct extent_state
, rb_node
);
661 if (state
->start
> end
)
664 if (state
->state
& bits
) {
665 start
= state
->start
;
666 atomic_inc(&state
->refs
);
667 wait_on_state(tree
, state
);
668 free_extent_state(state
);
671 start
= state
->end
+ 1;
676 cond_resched_lock(&tree
->lock
);
679 spin_unlock(&tree
->lock
);
682 static void set_state_bits(struct extent_io_tree
*tree
,
683 struct extent_state
*state
,
686 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
688 set_state_cb(tree
, state
, bits
);
689 if ((bits_to_set
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
690 u64 range
= state
->end
- state
->start
+ 1;
691 tree
->dirty_bytes
+= range
;
693 state
->state
|= bits_to_set
;
696 static void cache_state(struct extent_state
*state
,
697 struct extent_state
**cached_ptr
)
699 if (cached_ptr
&& !(*cached_ptr
)) {
700 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
702 atomic_inc(&state
->refs
);
707 static void uncache_state(struct extent_state
**cached_ptr
)
709 if (cached_ptr
&& (*cached_ptr
)) {
710 struct extent_state
*state
= *cached_ptr
;
712 free_extent_state(state
);
717 * set some bits on a range in the tree. This may require allocations or
718 * sleeping, so the gfp mask is used to indicate what is allowed.
720 * If any of the exclusive bits are set, this will fail with -EEXIST if some
721 * part of the range already has the desired bits set. The start of the
722 * existing range is returned in failed_start in this case.
724 * [start, end] is inclusive This takes the tree lock.
727 static int __must_check
728 __set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
729 int bits
, int exclusive_bits
, u64
*failed_start
,
730 struct extent_state
**cached_state
, gfp_t mask
)
732 struct extent_state
*state
;
733 struct extent_state
*prealloc
= NULL
;
734 struct rb_node
*node
;
739 bits
|= EXTENT_FIRST_DELALLOC
;
741 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
742 prealloc
= alloc_extent_state(mask
);
746 spin_lock(&tree
->lock
);
747 if (cached_state
&& *cached_state
) {
748 state
= *cached_state
;
749 if (state
->start
<= start
&& state
->end
> start
&&
751 node
= &state
->rb_node
;
756 * this search will find all the extents that end after
759 node
= tree_search(tree
, start
);
761 prealloc
= alloc_extent_state_atomic(prealloc
);
763 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
765 extent_io_tree_panic(tree
, err
);
770 state
= rb_entry(node
, struct extent_state
, rb_node
);
772 last_start
= state
->start
;
773 last_end
= state
->end
;
776 * | ---- desired range ---- |
779 * Just lock what we found and keep going
781 if (state
->start
== start
&& state
->end
<= end
) {
782 if (state
->state
& exclusive_bits
) {
783 *failed_start
= state
->start
;
788 set_state_bits(tree
, state
, &bits
);
789 cache_state(state
, cached_state
);
790 merge_state(tree
, state
);
791 if (last_end
== (u64
)-1)
793 start
= last_end
+ 1;
794 state
= next_state(state
);
795 if (start
< end
&& state
&& state
->start
== start
&&
802 * | ---- desired range ---- |
805 * | ------------- state -------------- |
807 * We need to split the extent we found, and may flip bits on
810 * If the extent we found extends past our
811 * range, we just split and search again. It'll get split
812 * again the next time though.
814 * If the extent we found is inside our range, we set the
817 if (state
->start
< start
) {
818 if (state
->state
& exclusive_bits
) {
819 *failed_start
= start
;
824 prealloc
= alloc_extent_state_atomic(prealloc
);
826 err
= split_state(tree
, state
, prealloc
, start
);
828 extent_io_tree_panic(tree
, err
);
833 if (state
->end
<= end
) {
834 set_state_bits(tree
, state
, &bits
);
835 cache_state(state
, cached_state
);
836 merge_state(tree
, state
);
837 if (last_end
== (u64
)-1)
839 start
= last_end
+ 1;
840 state
= next_state(state
);
841 if (start
< end
&& state
&& state
->start
== start
&&
848 * | ---- desired range ---- |
849 * | state | or | state |
851 * There's a hole, we need to insert something in it and
852 * ignore the extent we found.
854 if (state
->start
> start
) {
856 if (end
< last_start
)
859 this_end
= last_start
- 1;
861 prealloc
= alloc_extent_state_atomic(prealloc
);
865 * Avoid to free 'prealloc' if it can be merged with
868 err
= insert_state(tree
, prealloc
, start
, this_end
,
871 extent_io_tree_panic(tree
, err
);
873 cache_state(prealloc
, cached_state
);
875 start
= this_end
+ 1;
879 * | ---- desired range ---- |
881 * We need to split the extent, and set the bit
884 if (state
->start
<= end
&& state
->end
> end
) {
885 if (state
->state
& exclusive_bits
) {
886 *failed_start
= start
;
891 prealloc
= alloc_extent_state_atomic(prealloc
);
893 err
= split_state(tree
, state
, prealloc
, end
+ 1);
895 extent_io_tree_panic(tree
, err
);
897 set_state_bits(tree
, prealloc
, &bits
);
898 cache_state(prealloc
, cached_state
);
899 merge_state(tree
, prealloc
);
907 spin_unlock(&tree
->lock
);
909 free_extent_state(prealloc
);
916 spin_unlock(&tree
->lock
);
917 if (mask
& __GFP_WAIT
)
922 int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
,
923 u64
*failed_start
, struct extent_state
**cached_state
,
926 return __set_extent_bit(tree
, start
, end
, bits
, 0, failed_start
,
932 * convert_extent - convert all bits in a given range from one bit to another
933 * @tree: the io tree to search
934 * @start: the start offset in bytes
935 * @end: the end offset in bytes (inclusive)
936 * @bits: the bits to set in this range
937 * @clear_bits: the bits to clear in this range
938 * @mask: the allocation mask
940 * This will go through and set bits for the given range. If any states exist
941 * already in this range they are set with the given bit and cleared of the
942 * clear_bits. This is only meant to be used by things that are mergeable, ie
943 * converting from say DELALLOC to DIRTY. This is not meant to be used with
944 * boundary bits like LOCK.
946 int convert_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
947 int bits
, int clear_bits
, gfp_t mask
)
949 struct extent_state
*state
;
950 struct extent_state
*prealloc
= NULL
;
951 struct rb_node
*node
;
957 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
958 prealloc
= alloc_extent_state(mask
);
963 spin_lock(&tree
->lock
);
965 * this search will find all the extents that end after
968 node
= tree_search(tree
, start
);
970 prealloc
= alloc_extent_state_atomic(prealloc
);
975 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
978 extent_io_tree_panic(tree
, err
);
981 state
= rb_entry(node
, struct extent_state
, rb_node
);
983 last_start
= state
->start
;
984 last_end
= state
->end
;
987 * | ---- desired range ---- |
990 * Just lock what we found and keep going
992 if (state
->start
== start
&& state
->end
<= end
) {
993 set_state_bits(tree
, state
, &bits
);
994 state
= clear_state_bit(tree
, state
, &clear_bits
, 0);
995 if (last_end
== (u64
)-1)
997 start
= last_end
+ 1;
998 if (start
< end
&& state
&& state
->start
== start
&&
1005 * | ---- desired range ---- |
1008 * | ------------- state -------------- |
1010 * We need to split the extent we found, and may flip bits on
1013 * If the extent we found extends past our
1014 * range, we just split and search again. It'll get split
1015 * again the next time though.
1017 * If the extent we found is inside our range, we set the
1018 * desired bit on it.
1020 if (state
->start
< start
) {
1021 prealloc
= alloc_extent_state_atomic(prealloc
);
1026 err
= split_state(tree
, state
, prealloc
, start
);
1028 extent_io_tree_panic(tree
, err
);
1032 if (state
->end
<= end
) {
1033 set_state_bits(tree
, state
, &bits
);
1034 state
= clear_state_bit(tree
, state
, &clear_bits
, 0);
1035 if (last_end
== (u64
)-1)
1037 start
= last_end
+ 1;
1038 if (start
< end
&& state
&& state
->start
== start
&&
1045 * | ---- desired range ---- |
1046 * | state | or | state |
1048 * There's a hole, we need to insert something in it and
1049 * ignore the extent we found.
1051 if (state
->start
> start
) {
1053 if (end
< last_start
)
1056 this_end
= last_start
- 1;
1058 prealloc
= alloc_extent_state_atomic(prealloc
);
1065 * Avoid to free 'prealloc' if it can be merged with
1068 err
= insert_state(tree
, prealloc
, start
, this_end
,
1071 extent_io_tree_panic(tree
, err
);
1073 start
= this_end
+ 1;
1077 * | ---- desired range ---- |
1079 * We need to split the extent, and set the bit
1082 if (state
->start
<= end
&& state
->end
> end
) {
1083 prealloc
= alloc_extent_state_atomic(prealloc
);
1089 err
= split_state(tree
, state
, prealloc
, end
+ 1);
1091 extent_io_tree_panic(tree
, err
);
1093 set_state_bits(tree
, prealloc
, &bits
);
1094 clear_state_bit(tree
, prealloc
, &clear_bits
, 0);
1102 spin_unlock(&tree
->lock
);
1104 free_extent_state(prealloc
);
1111 spin_unlock(&tree
->lock
);
1112 if (mask
& __GFP_WAIT
)
1117 /* wrappers around set/clear extent bit */
1118 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1121 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, NULL
,
1125 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1126 int bits
, gfp_t mask
)
1128 return set_extent_bit(tree
, start
, end
, bits
, NULL
,
1132 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1133 int bits
, gfp_t mask
)
1135 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
1138 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1139 struct extent_state
**cached_state
, gfp_t mask
)
1141 return set_extent_bit(tree
, start
, end
,
1142 EXTENT_DELALLOC
| EXTENT_UPTODATE
,
1143 NULL
, cached_state
, mask
);
1146 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1149 return clear_extent_bit(tree
, start
, end
,
1150 EXTENT_DIRTY
| EXTENT_DELALLOC
|
1151 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, mask
);
1154 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1157 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, NULL
,
1161 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1162 struct extent_state
**cached_state
, gfp_t mask
)
1164 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0,
1165 cached_state
, mask
);
1168 int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1169 struct extent_state
**cached_state
, gfp_t mask
)
1171 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
1172 cached_state
, mask
);
1176 * either insert or lock state struct between start and end use mask to tell
1177 * us if waiting is desired.
1179 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1180 int bits
, struct extent_state
**cached_state
)
1185 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
1186 EXTENT_LOCKED
, &failed_start
,
1187 cached_state
, GFP_NOFS
);
1188 if (err
== -EEXIST
) {
1189 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
1190 start
= failed_start
;
1193 WARN_ON(start
> end
);
1198 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1200 return lock_extent_bits(tree
, start
, end
, 0, NULL
);
1203 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1208 err
= __set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1209 &failed_start
, NULL
, GFP_NOFS
);
1210 if (err
== -EEXIST
) {
1211 if (failed_start
> start
)
1212 clear_extent_bit(tree
, start
, failed_start
- 1,
1213 EXTENT_LOCKED
, 1, 0, NULL
, GFP_NOFS
);
1219 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1220 struct extent_state
**cached
, gfp_t mask
)
1222 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1226 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1228 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1233 * helper function to set both pages and extents in the tree writeback
1235 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1237 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1238 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1241 while (index
<= end_index
) {
1242 page
= find_get_page(tree
->mapping
, index
);
1243 BUG_ON(!page
); /* Pages should be in the extent_io_tree */
1244 set_page_writeback(page
);
1245 page_cache_release(page
);
1251 /* find the first state struct with 'bits' set after 'start', and
1252 * return it. tree->lock must be held. NULL will returned if
1253 * nothing was found after 'start'
1255 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1256 u64 start
, int bits
)
1258 struct rb_node
*node
;
1259 struct extent_state
*state
;
1262 * this search will find all the extents that end after
1265 node
= tree_search(tree
, start
);
1270 state
= rb_entry(node
, struct extent_state
, rb_node
);
1271 if (state
->end
>= start
&& (state
->state
& bits
))
1274 node
= rb_next(node
);
1283 * find the first offset in the io tree with 'bits' set. zero is
1284 * returned if we find something, and *start_ret and *end_ret are
1285 * set to reflect the state struct that was found.
1287 * If nothing was found, 1 is returned. If found something, return 0.
1289 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1290 u64
*start_ret
, u64
*end_ret
, int bits
)
1292 struct extent_state
*state
;
1295 spin_lock(&tree
->lock
);
1296 state
= find_first_extent_bit_state(tree
, start
, bits
);
1298 *start_ret
= state
->start
;
1299 *end_ret
= state
->end
;
1302 spin_unlock(&tree
->lock
);
1307 * find a contiguous range of bytes in the file marked as delalloc, not
1308 * more than 'max_bytes'. start and end are used to return the range,
1310 * 1 is returned if we find something, 0 if nothing was in the tree
1312 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1313 u64
*start
, u64
*end
, u64 max_bytes
,
1314 struct extent_state
**cached_state
)
1316 struct rb_node
*node
;
1317 struct extent_state
*state
;
1318 u64 cur_start
= *start
;
1320 u64 total_bytes
= 0;
1322 spin_lock(&tree
->lock
);
1325 * this search will find all the extents that end after
1328 node
= tree_search(tree
, cur_start
);
1336 state
= rb_entry(node
, struct extent_state
, rb_node
);
1337 if (found
&& (state
->start
!= cur_start
||
1338 (state
->state
& EXTENT_BOUNDARY
))) {
1341 if (!(state
->state
& EXTENT_DELALLOC
)) {
1347 *start
= state
->start
;
1348 *cached_state
= state
;
1349 atomic_inc(&state
->refs
);
1353 cur_start
= state
->end
+ 1;
1354 node
= rb_next(node
);
1357 total_bytes
+= state
->end
- state
->start
+ 1;
1358 if (total_bytes
>= max_bytes
)
1362 spin_unlock(&tree
->lock
);
1366 static noinline
void __unlock_for_delalloc(struct inode
*inode
,
1367 struct page
*locked_page
,
1371 struct page
*pages
[16];
1372 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1373 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1374 unsigned long nr_pages
= end_index
- index
+ 1;
1377 if (index
== locked_page
->index
&& end_index
== index
)
1380 while (nr_pages
> 0) {
1381 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1382 min_t(unsigned long, nr_pages
,
1383 ARRAY_SIZE(pages
)), pages
);
1384 for (i
= 0; i
< ret
; i
++) {
1385 if (pages
[i
] != locked_page
)
1386 unlock_page(pages
[i
]);
1387 page_cache_release(pages
[i
]);
1395 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1396 struct page
*locked_page
,
1400 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1401 unsigned long start_index
= index
;
1402 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1403 unsigned long pages_locked
= 0;
1404 struct page
*pages
[16];
1405 unsigned long nrpages
;
1409 /* the caller is responsible for locking the start index */
1410 if (index
== locked_page
->index
&& index
== end_index
)
1413 /* skip the page at the start index */
1414 nrpages
= end_index
- index
+ 1;
1415 while (nrpages
> 0) {
1416 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1417 min_t(unsigned long,
1418 nrpages
, ARRAY_SIZE(pages
)), pages
);
1423 /* now we have an array of pages, lock them all */
1424 for (i
= 0; i
< ret
; i
++) {
1426 * the caller is taking responsibility for
1429 if (pages
[i
] != locked_page
) {
1430 lock_page(pages
[i
]);
1431 if (!PageDirty(pages
[i
]) ||
1432 pages
[i
]->mapping
!= inode
->i_mapping
) {
1434 unlock_page(pages
[i
]);
1435 page_cache_release(pages
[i
]);
1439 page_cache_release(pages
[i
]);
1448 if (ret
&& pages_locked
) {
1449 __unlock_for_delalloc(inode
, locked_page
,
1451 ((u64
)(start_index
+ pages_locked
- 1)) <<
1458 * find a contiguous range of bytes in the file marked as delalloc, not
1459 * more than 'max_bytes'. start and end are used to return the range,
1461 * 1 is returned if we find something, 0 if nothing was in the tree
1463 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1464 struct extent_io_tree
*tree
,
1465 struct page
*locked_page
,
1466 u64
*start
, u64
*end
,
1472 struct extent_state
*cached_state
= NULL
;
1477 /* step one, find a bunch of delalloc bytes starting at start */
1478 delalloc_start
= *start
;
1480 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1481 max_bytes
, &cached_state
);
1482 if (!found
|| delalloc_end
<= *start
) {
1483 *start
= delalloc_start
;
1484 *end
= delalloc_end
;
1485 free_extent_state(cached_state
);
1490 * start comes from the offset of locked_page. We have to lock
1491 * pages in order, so we can't process delalloc bytes before
1494 if (delalloc_start
< *start
)
1495 delalloc_start
= *start
;
1498 * make sure to limit the number of pages we try to lock down
1501 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1502 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1504 /* step two, lock all the pages after the page that has start */
1505 ret
= lock_delalloc_pages(inode
, locked_page
,
1506 delalloc_start
, delalloc_end
);
1507 if (ret
== -EAGAIN
) {
1508 /* some of the pages are gone, lets avoid looping by
1509 * shortening the size of the delalloc range we're searching
1511 free_extent_state(cached_state
);
1513 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1514 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1522 BUG_ON(ret
); /* Only valid values are 0 and -EAGAIN */
1524 /* step three, lock the state bits for the whole range */
1525 lock_extent_bits(tree
, delalloc_start
, delalloc_end
, 0, &cached_state
);
1527 /* then test to make sure it is all still delalloc */
1528 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1529 EXTENT_DELALLOC
, 1, cached_state
);
1531 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1532 &cached_state
, GFP_NOFS
);
1533 __unlock_for_delalloc(inode
, locked_page
,
1534 delalloc_start
, delalloc_end
);
1538 free_extent_state(cached_state
);
1539 *start
= delalloc_start
;
1540 *end
= delalloc_end
;
1545 int extent_clear_unlock_delalloc(struct inode
*inode
,
1546 struct extent_io_tree
*tree
,
1547 u64 start
, u64 end
, struct page
*locked_page
,
1551 struct page
*pages
[16];
1552 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1553 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1554 unsigned long nr_pages
= end_index
- index
+ 1;
1558 if (op
& EXTENT_CLEAR_UNLOCK
)
1559 clear_bits
|= EXTENT_LOCKED
;
1560 if (op
& EXTENT_CLEAR_DIRTY
)
1561 clear_bits
|= EXTENT_DIRTY
;
1563 if (op
& EXTENT_CLEAR_DELALLOC
)
1564 clear_bits
|= EXTENT_DELALLOC
;
1566 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1567 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1568 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1569 EXTENT_SET_PRIVATE2
)))
1572 while (nr_pages
> 0) {
1573 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1574 min_t(unsigned long,
1575 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1576 for (i
= 0; i
< ret
; i
++) {
1578 if (op
& EXTENT_SET_PRIVATE2
)
1579 SetPagePrivate2(pages
[i
]);
1581 if (pages
[i
] == locked_page
) {
1582 page_cache_release(pages
[i
]);
1585 if (op
& EXTENT_CLEAR_DIRTY
)
1586 clear_page_dirty_for_io(pages
[i
]);
1587 if (op
& EXTENT_SET_WRITEBACK
)
1588 set_page_writeback(pages
[i
]);
1589 if (op
& EXTENT_END_WRITEBACK
)
1590 end_page_writeback(pages
[i
]);
1591 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1592 unlock_page(pages
[i
]);
1593 page_cache_release(pages
[i
]);
1603 * count the number of bytes in the tree that have a given bit(s)
1604 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1605 * cached. The total number found is returned.
1607 u64
count_range_bits(struct extent_io_tree
*tree
,
1608 u64
*start
, u64 search_end
, u64 max_bytes
,
1609 unsigned long bits
, int contig
)
1611 struct rb_node
*node
;
1612 struct extent_state
*state
;
1613 u64 cur_start
= *start
;
1614 u64 total_bytes
= 0;
1618 if (search_end
<= cur_start
) {
1623 spin_lock(&tree
->lock
);
1624 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1625 total_bytes
= tree
->dirty_bytes
;
1629 * this search will find all the extents that end after
1632 node
= tree_search(tree
, cur_start
);
1637 state
= rb_entry(node
, struct extent_state
, rb_node
);
1638 if (state
->start
> search_end
)
1640 if (contig
&& found
&& state
->start
> last
+ 1)
1642 if (state
->end
>= cur_start
&& (state
->state
& bits
) == bits
) {
1643 total_bytes
+= min(search_end
, state
->end
) + 1 -
1644 max(cur_start
, state
->start
);
1645 if (total_bytes
>= max_bytes
)
1648 *start
= max(cur_start
, state
->start
);
1652 } else if (contig
&& found
) {
1655 node
= rb_next(node
);
1660 spin_unlock(&tree
->lock
);
1665 * set the private field for a given byte offset in the tree. If there isn't
1666 * an extent_state there already, this does nothing.
1668 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1670 struct rb_node
*node
;
1671 struct extent_state
*state
;
1674 spin_lock(&tree
->lock
);
1676 * this search will find all the extents that end after
1679 node
= tree_search(tree
, start
);
1684 state
= rb_entry(node
, struct extent_state
, rb_node
);
1685 if (state
->start
!= start
) {
1689 state
->private = private;
1691 spin_unlock(&tree
->lock
);
1695 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1697 struct rb_node
*node
;
1698 struct extent_state
*state
;
1701 spin_lock(&tree
->lock
);
1703 * this search will find all the extents that end after
1706 node
= tree_search(tree
, start
);
1711 state
= rb_entry(node
, struct extent_state
, rb_node
);
1712 if (state
->start
!= start
) {
1716 *private = state
->private;
1718 spin_unlock(&tree
->lock
);
1723 * searches a range in the state tree for a given mask.
1724 * If 'filled' == 1, this returns 1 only if every extent in the tree
1725 * has the bits set. Otherwise, 1 is returned if any bit in the
1726 * range is found set.
1728 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1729 int bits
, int filled
, struct extent_state
*cached
)
1731 struct extent_state
*state
= NULL
;
1732 struct rb_node
*node
;
1735 spin_lock(&tree
->lock
);
1736 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
1737 cached
->end
> start
)
1738 node
= &cached
->rb_node
;
1740 node
= tree_search(tree
, start
);
1741 while (node
&& start
<= end
) {
1742 state
= rb_entry(node
, struct extent_state
, rb_node
);
1744 if (filled
&& state
->start
> start
) {
1749 if (state
->start
> end
)
1752 if (state
->state
& bits
) {
1756 } else if (filled
) {
1761 if (state
->end
== (u64
)-1)
1764 start
= state
->end
+ 1;
1767 node
= rb_next(node
);
1774 spin_unlock(&tree
->lock
);
1779 * helper function to set a given page up to date if all the
1780 * extents in the tree for that page are up to date
1782 static void check_page_uptodate(struct extent_io_tree
*tree
, struct page
*page
)
1784 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1785 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1786 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1787 SetPageUptodate(page
);
1791 * helper function to unlock a page if all the extents in the tree
1792 * for that page are unlocked
1794 static void check_page_locked(struct extent_io_tree
*tree
, struct page
*page
)
1796 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1797 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1798 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1803 * helper function to end page writeback if all the extents
1804 * in the tree for that page are done with writeback
1806 static void check_page_writeback(struct extent_io_tree
*tree
,
1809 end_page_writeback(page
);
1813 * When IO fails, either with EIO or csum verification fails, we
1814 * try other mirrors that might have a good copy of the data. This
1815 * io_failure_record is used to record state as we go through all the
1816 * mirrors. If another mirror has good data, the page is set up to date
1817 * and things continue. If a good mirror can't be found, the original
1818 * bio end_io callback is called to indicate things have failed.
1820 struct io_failure_record
{
1825 unsigned long bio_flags
;
1831 static int free_io_failure(struct inode
*inode
, struct io_failure_record
*rec
,
1836 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1838 set_state_private(failure_tree
, rec
->start
, 0);
1839 ret
= clear_extent_bits(failure_tree
, rec
->start
,
1840 rec
->start
+ rec
->len
- 1,
1841 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1846 ret
= clear_extent_bits(&BTRFS_I(inode
)->io_tree
, rec
->start
,
1847 rec
->start
+ rec
->len
- 1,
1848 EXTENT_DAMAGED
, GFP_NOFS
);
1857 static void repair_io_failure_callback(struct bio
*bio
, int err
)
1859 complete(bio
->bi_private
);
1863 * this bypasses the standard btrfs submit functions deliberately, as
1864 * the standard behavior is to write all copies in a raid setup. here we only
1865 * want to write the one bad copy. so we do the mapping for ourselves and issue
1866 * submit_bio directly.
1867 * to avoid any synchonization issues, wait for the data after writing, which
1868 * actually prevents the read that triggered the error from finishing.
1869 * currently, there can be no more than two copies of every data bit. thus,
1870 * exactly one rewrite is required.
1872 int repair_io_failure(struct btrfs_mapping_tree
*map_tree
, u64 start
,
1873 u64 length
, u64 logical
, struct page
*page
,
1877 struct btrfs_device
*dev
;
1878 DECLARE_COMPLETION_ONSTACK(compl);
1881 struct btrfs_bio
*bbio
= NULL
;
1884 BUG_ON(!mirror_num
);
1886 bio
= bio_alloc(GFP_NOFS
, 1);
1889 bio
->bi_private
= &compl;
1890 bio
->bi_end_io
= repair_io_failure_callback
;
1892 map_length
= length
;
1894 ret
= btrfs_map_block(map_tree
, WRITE
, logical
,
1895 &map_length
, &bbio
, mirror_num
);
1900 BUG_ON(mirror_num
!= bbio
->mirror_num
);
1901 sector
= bbio
->stripes
[mirror_num
-1].physical
>> 9;
1902 bio
->bi_sector
= sector
;
1903 dev
= bbio
->stripes
[mirror_num
-1].dev
;
1905 if (!dev
|| !dev
->bdev
|| !dev
->writeable
) {
1909 bio
->bi_bdev
= dev
->bdev
;
1910 bio_add_page(bio
, page
, length
, start
-page_offset(page
));
1911 btrfsic_submit_bio(WRITE_SYNC
, bio
);
1912 wait_for_completion(&compl);
1914 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
1915 /* try to remap that extent elsewhere? */
1917 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_WRITE_ERRS
);
1921 printk_in_rcu(KERN_INFO
"btrfs read error corrected: ino %lu off %llu "
1922 "(dev %s sector %llu)\n", page
->mapping
->host
->i_ino
,
1923 start
, rcu_str_deref(dev
->name
), sector
);
1929 int repair_eb_io_failure(struct btrfs_root
*root
, struct extent_buffer
*eb
,
1932 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1933 u64 start
= eb
->start
;
1934 unsigned long i
, num_pages
= num_extent_pages(eb
->start
, eb
->len
);
1937 for (i
= 0; i
< num_pages
; i
++) {
1938 struct page
*p
= extent_buffer_page(eb
, i
);
1939 ret
= repair_io_failure(map_tree
, start
, PAGE_CACHE_SIZE
,
1940 start
, p
, mirror_num
);
1943 start
+= PAGE_CACHE_SIZE
;
1950 * each time an IO finishes, we do a fast check in the IO failure tree
1951 * to see if we need to process or clean up an io_failure_record
1953 static int clean_io_failure(u64 start
, struct page
*page
)
1956 u64 private_failure
;
1957 struct io_failure_record
*failrec
;
1958 struct btrfs_mapping_tree
*map_tree
;
1959 struct extent_state
*state
;
1963 struct inode
*inode
= page
->mapping
->host
;
1966 ret
= count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1967 (u64
)-1, 1, EXTENT_DIRTY
, 0);
1971 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
, start
,
1976 failrec
= (struct io_failure_record
*)(unsigned long) private_failure
;
1977 BUG_ON(!failrec
->this_mirror
);
1979 if (failrec
->in_validation
) {
1980 /* there was no real error, just free the record */
1981 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1987 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1988 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1991 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
1993 if (state
&& state
->start
== failrec
->start
) {
1994 map_tree
= &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
;
1995 num_copies
= btrfs_num_copies(map_tree
, failrec
->logical
,
1997 if (num_copies
> 1) {
1998 ret
= repair_io_failure(map_tree
, start
, failrec
->len
,
1999 failrec
->logical
, page
,
2000 failrec
->failed_mirror
);
2007 ret
= free_io_failure(inode
, failrec
, did_repair
);
2013 * this is a generic handler for readpage errors (default
2014 * readpage_io_failed_hook). if other copies exist, read those and write back
2015 * good data to the failed position. does not investigate in remapping the
2016 * failed extent elsewhere, hoping the device will be smart enough to do this as
2020 static int bio_readpage_error(struct bio
*failed_bio
, struct page
*page
,
2021 u64 start
, u64 end
, int failed_mirror
,
2022 struct extent_state
*state
)
2024 struct io_failure_record
*failrec
= NULL
;
2026 struct extent_map
*em
;
2027 struct inode
*inode
= page
->mapping
->host
;
2028 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
2029 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
2030 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2037 BUG_ON(failed_bio
->bi_rw
& REQ_WRITE
);
2039 ret
= get_state_private(failure_tree
, start
, &private);
2041 failrec
= kzalloc(sizeof(*failrec
), GFP_NOFS
);
2044 failrec
->start
= start
;
2045 failrec
->len
= end
- start
+ 1;
2046 failrec
->this_mirror
= 0;
2047 failrec
->bio_flags
= 0;
2048 failrec
->in_validation
= 0;
2050 read_lock(&em_tree
->lock
);
2051 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
2053 read_unlock(&em_tree
->lock
);
2058 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
2059 free_extent_map(em
);
2062 read_unlock(&em_tree
->lock
);
2064 if (!em
|| IS_ERR(em
)) {
2068 logical
= start
- em
->start
;
2069 logical
= em
->block_start
+ logical
;
2070 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2071 logical
= em
->block_start
;
2072 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
2073 extent_set_compress_type(&failrec
->bio_flags
,
2076 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2077 "len=%llu\n", logical
, start
, failrec
->len
);
2078 failrec
->logical
= logical
;
2079 free_extent_map(em
);
2081 /* set the bits in the private failure tree */
2082 ret
= set_extent_bits(failure_tree
, start
, end
,
2083 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
2085 ret
= set_state_private(failure_tree
, start
,
2086 (u64
)(unsigned long)failrec
);
2087 /* set the bits in the inode's tree */
2089 ret
= set_extent_bits(tree
, start
, end
, EXTENT_DAMAGED
,
2096 failrec
= (struct io_failure_record
*)(unsigned long)private;
2097 pr_debug("bio_readpage_error: (found) logical=%llu, "
2098 "start=%llu, len=%llu, validation=%d\n",
2099 failrec
->logical
, failrec
->start
, failrec
->len
,
2100 failrec
->in_validation
);
2102 * when data can be on disk more than twice, add to failrec here
2103 * (e.g. with a list for failed_mirror) to make
2104 * clean_io_failure() clean all those errors at once.
2107 num_copies
= btrfs_num_copies(
2108 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
2109 failrec
->logical
, failrec
->len
);
2110 if (num_copies
== 1) {
2112 * we only have a single copy of the data, so don't bother with
2113 * all the retry and error correction code that follows. no
2114 * matter what the error is, it is very likely to persist.
2116 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2117 "state=%p, num_copies=%d, next_mirror %d, "
2118 "failed_mirror %d\n", state
, num_copies
,
2119 failrec
->this_mirror
, failed_mirror
);
2120 free_io_failure(inode
, failrec
, 0);
2125 spin_lock(&tree
->lock
);
2126 state
= find_first_extent_bit_state(tree
, failrec
->start
,
2128 if (state
&& state
->start
!= failrec
->start
)
2130 spin_unlock(&tree
->lock
);
2134 * there are two premises:
2135 * a) deliver good data to the caller
2136 * b) correct the bad sectors on disk
2138 if (failed_bio
->bi_vcnt
> 1) {
2140 * to fulfill b), we need to know the exact failing sectors, as
2141 * we don't want to rewrite any more than the failed ones. thus,
2142 * we need separate read requests for the failed bio
2144 * if the following BUG_ON triggers, our validation request got
2145 * merged. we need separate requests for our algorithm to work.
2147 BUG_ON(failrec
->in_validation
);
2148 failrec
->in_validation
= 1;
2149 failrec
->this_mirror
= failed_mirror
;
2150 read_mode
= READ_SYNC
| REQ_FAILFAST_DEV
;
2153 * we're ready to fulfill a) and b) alongside. get a good copy
2154 * of the failed sector and if we succeed, we have setup
2155 * everything for repair_io_failure to do the rest for us.
2157 if (failrec
->in_validation
) {
2158 BUG_ON(failrec
->this_mirror
!= failed_mirror
);
2159 failrec
->in_validation
= 0;
2160 failrec
->this_mirror
= 0;
2162 failrec
->failed_mirror
= failed_mirror
;
2163 failrec
->this_mirror
++;
2164 if (failrec
->this_mirror
== failed_mirror
)
2165 failrec
->this_mirror
++;
2166 read_mode
= READ_SYNC
;
2169 if (!state
|| failrec
->this_mirror
> num_copies
) {
2170 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2171 "next_mirror %d, failed_mirror %d\n", state
,
2172 num_copies
, failrec
->this_mirror
, failed_mirror
);
2173 free_io_failure(inode
, failrec
, 0);
2177 bio
= bio_alloc(GFP_NOFS
, 1);
2179 free_io_failure(inode
, failrec
, 0);
2182 bio
->bi_private
= state
;
2183 bio
->bi_end_io
= failed_bio
->bi_end_io
;
2184 bio
->bi_sector
= failrec
->logical
>> 9;
2185 bio
->bi_bdev
= BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
;
2188 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
2190 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2191 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode
,
2192 failrec
->this_mirror
, num_copies
, failrec
->in_validation
);
2194 ret
= tree
->ops
->submit_bio_hook(inode
, read_mode
, bio
,
2195 failrec
->this_mirror
,
2196 failrec
->bio_flags
, 0);
2200 /* lots and lots of room for performance fixes in the end_bio funcs */
2202 int end_extent_writepage(struct page
*page
, int err
, u64 start
, u64 end
)
2204 int uptodate
= (err
== 0);
2205 struct extent_io_tree
*tree
;
2208 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2210 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
2211 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
2212 end
, NULL
, uptodate
);
2218 ClearPageUptodate(page
);
2225 * after a writepage IO is done, we need to:
2226 * clear the uptodate bits on error
2227 * clear the writeback bits in the extent tree for this IO
2228 * end_page_writeback if the page has no more pending IO
2230 * Scheduling is not allowed, so the extent state tree is expected
2231 * to have one and only one object corresponding to this IO.
2233 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
2235 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2236 struct extent_io_tree
*tree
;
2242 struct page
*page
= bvec
->bv_page
;
2243 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2245 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
2247 end
= start
+ bvec
->bv_len
- 1;
2249 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2254 if (--bvec
>= bio
->bi_io_vec
)
2255 prefetchw(&bvec
->bv_page
->flags
);
2257 if (end_extent_writepage(page
, err
, start
, end
))
2261 end_page_writeback(page
);
2263 check_page_writeback(tree
, page
);
2264 } while (bvec
>= bio
->bi_io_vec
);
2270 * after a readpage IO is done, we need to:
2271 * clear the uptodate bits on error
2272 * set the uptodate bits if things worked
2273 * set the page up to date if all extents in the tree are uptodate
2274 * clear the lock bit in the extent tree
2275 * unlock the page if there are no other extents locked for it
2277 * Scheduling is not allowed, so the extent state tree is expected
2278 * to have one and only one object corresponding to this IO.
2280 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
2282 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2283 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2284 struct bio_vec
*bvec
= bio
->bi_io_vec
;
2285 struct extent_io_tree
*tree
;
2296 struct page
*page
= bvec
->bv_page
;
2297 struct extent_state
*cached
= NULL
;
2298 struct extent_state
*state
;
2300 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2301 "mirror=%ld\n", bio
->bi_vcnt
, bio
->bi_idx
, err
,
2302 (long int)bio
->bi_bdev
);
2303 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2305 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
2307 end
= start
+ bvec
->bv_len
- 1;
2309 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2314 if (++bvec
<= bvec_end
)
2315 prefetchw(&bvec
->bv_page
->flags
);
2317 spin_lock(&tree
->lock
);
2318 state
= find_first_extent_bit_state(tree
, start
, EXTENT_LOCKED
);
2319 if (state
&& state
->start
== start
) {
2321 * take a reference on the state, unlock will drop
2324 cache_state(state
, &cached
);
2326 spin_unlock(&tree
->lock
);
2328 mirror
= (int)(unsigned long)bio
->bi_bdev
;
2329 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
2330 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
2333 /* no IO indicated but software detected errors
2334 * in the block, either checksum errors or
2335 * issues with the contents */
2336 struct btrfs_root
*root
=
2337 BTRFS_I(page
->mapping
->host
)->root
;
2338 struct btrfs_device
*device
;
2341 device
= btrfs_find_device_for_logical(
2342 root
, start
, mirror
);
2344 btrfs_dev_stat_inc_and_print(device
,
2345 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
2347 clean_io_failure(start
, page
);
2351 if (!uptodate
&& tree
->ops
&& tree
->ops
->readpage_io_failed_hook
) {
2352 ret
= tree
->ops
->readpage_io_failed_hook(page
, mirror
);
2354 test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
2356 } else if (!uptodate
) {
2358 * The generic bio_readpage_error handles errors the
2359 * following way: If possible, new read requests are
2360 * created and submitted and will end up in
2361 * end_bio_extent_readpage as well (if we're lucky, not
2362 * in the !uptodate case). In that case it returns 0 and
2363 * we just go on with the next page in our bio. If it
2364 * can't handle the error it will return -EIO and we
2365 * remain responsible for that page.
2367 ret
= bio_readpage_error(bio
, page
, start
, end
, mirror
, NULL
);
2370 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2373 uncache_state(&cached
);
2378 if (uptodate
&& tree
->track_uptodate
) {
2379 set_extent_uptodate(tree
, start
, end
, &cached
,
2382 unlock_extent_cached(tree
, start
, end
, &cached
, GFP_ATOMIC
);
2386 SetPageUptodate(page
);
2388 ClearPageUptodate(page
);
2394 check_page_uptodate(tree
, page
);
2396 ClearPageUptodate(page
);
2399 check_page_locked(tree
, page
);
2401 } while (bvec
<= bvec_end
);
2407 btrfs_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
2412 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2414 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
2415 while (!bio
&& (nr_vecs
/= 2))
2416 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2421 bio
->bi_bdev
= bdev
;
2422 bio
->bi_sector
= first_sector
;
2428 * Since writes are async, they will only return -ENOMEM.
2429 * Reads can return the full range of I/O error conditions.
2431 static int __must_check
submit_one_bio(int rw
, struct bio
*bio
,
2432 int mirror_num
, unsigned long bio_flags
)
2435 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2436 struct page
*page
= bvec
->bv_page
;
2437 struct extent_io_tree
*tree
= bio
->bi_private
;
2440 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
2442 bio
->bi_private
= NULL
;
2446 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
2447 ret
= tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
2448 mirror_num
, bio_flags
, start
);
2450 btrfsic_submit_bio(rw
, bio
);
2452 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
2458 static int merge_bio(struct extent_io_tree
*tree
, struct page
*page
,
2459 unsigned long offset
, size_t size
, struct bio
*bio
,
2460 unsigned long bio_flags
)
2463 if (tree
->ops
&& tree
->ops
->merge_bio_hook
)
2464 ret
= tree
->ops
->merge_bio_hook(page
, offset
, size
, bio
,
2471 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
2472 struct page
*page
, sector_t sector
,
2473 size_t size
, unsigned long offset
,
2474 struct block_device
*bdev
,
2475 struct bio
**bio_ret
,
2476 unsigned long max_pages
,
2477 bio_end_io_t end_io_func
,
2479 unsigned long prev_bio_flags
,
2480 unsigned long bio_flags
)
2486 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
2487 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
2488 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
2490 if (bio_ret
&& *bio_ret
) {
2493 contig
= bio
->bi_sector
== sector
;
2495 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
2498 if (prev_bio_flags
!= bio_flags
|| !contig
||
2499 merge_bio(tree
, page
, offset
, page_size
, bio
, bio_flags
) ||
2500 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
2501 ret
= submit_one_bio(rw
, bio
, mirror_num
,
2510 if (this_compressed
)
2513 nr
= bio_get_nr_vecs(bdev
);
2515 bio
= btrfs_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
2519 bio_add_page(bio
, page
, page_size
, offset
);
2520 bio
->bi_end_io
= end_io_func
;
2521 bio
->bi_private
= tree
;
2526 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
2531 void attach_extent_buffer_page(struct extent_buffer
*eb
, struct page
*page
)
2533 if (!PagePrivate(page
)) {
2534 SetPagePrivate(page
);
2535 page_cache_get(page
);
2536 set_page_private(page
, (unsigned long)eb
);
2538 WARN_ON(page
->private != (unsigned long)eb
);
2542 void set_page_extent_mapped(struct page
*page
)
2544 if (!PagePrivate(page
)) {
2545 SetPagePrivate(page
);
2546 page_cache_get(page
);
2547 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
2552 * basic readpage implementation. Locked extent state structs are inserted
2553 * into the tree that are removed when the IO is done (by the end_io
2555 * XXX JDM: This needs looking at to ensure proper page locking
2557 static int __extent_read_full_page(struct extent_io_tree
*tree
,
2559 get_extent_t
*get_extent
,
2560 struct bio
**bio
, int mirror_num
,
2561 unsigned long *bio_flags
)
2563 struct inode
*inode
= page
->mapping
->host
;
2564 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2565 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2569 u64 last_byte
= i_size_read(inode
);
2573 struct extent_map
*em
;
2574 struct block_device
*bdev
;
2575 struct btrfs_ordered_extent
*ordered
;
2578 size_t pg_offset
= 0;
2580 size_t disk_io_size
;
2581 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2582 unsigned long this_bio_flag
= 0;
2584 set_page_extent_mapped(page
);
2586 if (!PageUptodate(page
)) {
2587 if (cleancache_get_page(page
) == 0) {
2588 BUG_ON(blocksize
!= PAGE_SIZE
);
2595 lock_extent(tree
, start
, end
);
2596 ordered
= btrfs_lookup_ordered_extent(inode
, start
);
2599 unlock_extent(tree
, start
, end
);
2600 btrfs_start_ordered_extent(inode
, ordered
, 1);
2601 btrfs_put_ordered_extent(ordered
);
2604 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
2606 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
2609 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
2610 userpage
= kmap_atomic(page
);
2611 memset(userpage
+ zero_offset
, 0, iosize
);
2612 flush_dcache_page(page
);
2613 kunmap_atomic(userpage
);
2616 while (cur
<= end
) {
2617 if (cur
>= last_byte
) {
2619 struct extent_state
*cached
= NULL
;
2621 iosize
= PAGE_CACHE_SIZE
- pg_offset
;
2622 userpage
= kmap_atomic(page
);
2623 memset(userpage
+ pg_offset
, 0, iosize
);
2624 flush_dcache_page(page
);
2625 kunmap_atomic(userpage
);
2626 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2628 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2632 em
= get_extent(inode
, page
, pg_offset
, cur
,
2634 if (IS_ERR_OR_NULL(em
)) {
2636 unlock_extent(tree
, cur
, end
);
2639 extent_offset
= cur
- em
->start
;
2640 BUG_ON(extent_map_end(em
) <= cur
);
2643 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2644 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2645 extent_set_compress_type(&this_bio_flag
,
2649 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2650 cur_end
= min(extent_map_end(em
) - 1, end
);
2651 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2652 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2653 disk_io_size
= em
->block_len
;
2654 sector
= em
->block_start
>> 9;
2656 sector
= (em
->block_start
+ extent_offset
) >> 9;
2657 disk_io_size
= iosize
;
2660 block_start
= em
->block_start
;
2661 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2662 block_start
= EXTENT_MAP_HOLE
;
2663 free_extent_map(em
);
2666 /* we've found a hole, just zero and go on */
2667 if (block_start
== EXTENT_MAP_HOLE
) {
2669 struct extent_state
*cached
= NULL
;
2671 userpage
= kmap_atomic(page
);
2672 memset(userpage
+ pg_offset
, 0, iosize
);
2673 flush_dcache_page(page
);
2674 kunmap_atomic(userpage
);
2676 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2678 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2681 pg_offset
+= iosize
;
2684 /* the get_extent function already copied into the page */
2685 if (test_range_bit(tree
, cur
, cur_end
,
2686 EXTENT_UPTODATE
, 1, NULL
)) {
2687 check_page_uptodate(tree
, page
);
2688 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2690 pg_offset
+= iosize
;
2693 /* we have an inline extent but it didn't get marked up
2694 * to date. Error out
2696 if (block_start
== EXTENT_MAP_INLINE
) {
2698 unlock_extent(tree
, cur
, cur
+ iosize
- 1);
2700 pg_offset
+= iosize
;
2705 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2706 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2710 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2712 ret
= submit_extent_page(READ
, tree
, page
,
2713 sector
, disk_io_size
, pg_offset
,
2715 end_bio_extent_readpage
, mirror_num
,
2718 BUG_ON(ret
== -ENOMEM
);
2720 *bio_flags
= this_bio_flag
;
2725 pg_offset
+= iosize
;
2729 if (!PageError(page
))
2730 SetPageUptodate(page
);
2736 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2737 get_extent_t
*get_extent
, int mirror_num
)
2739 struct bio
*bio
= NULL
;
2740 unsigned long bio_flags
= 0;
2743 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, mirror_num
,
2746 ret
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
2750 static noinline
void update_nr_written(struct page
*page
,
2751 struct writeback_control
*wbc
,
2752 unsigned long nr_written
)
2754 wbc
->nr_to_write
-= nr_written
;
2755 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2756 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2757 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2761 * the writepage semantics are similar to regular writepage. extent
2762 * records are inserted to lock ranges in the tree, and as dirty areas
2763 * are found, they are marked writeback. Then the lock bits are removed
2764 * and the end_io handler clears the writeback ranges
2766 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2769 struct inode
*inode
= page
->mapping
->host
;
2770 struct extent_page_data
*epd
= data
;
2771 struct extent_io_tree
*tree
= epd
->tree
;
2772 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2774 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2778 u64 last_byte
= i_size_read(inode
);
2782 struct extent_state
*cached_state
= NULL
;
2783 struct extent_map
*em
;
2784 struct block_device
*bdev
;
2787 size_t pg_offset
= 0;
2789 loff_t i_size
= i_size_read(inode
);
2790 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2796 unsigned long nr_written
= 0;
2797 bool fill_delalloc
= true;
2799 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2800 write_flags
= WRITE_SYNC
;
2802 write_flags
= WRITE
;
2804 trace___extent_writepage(page
, inode
, wbc
);
2806 WARN_ON(!PageLocked(page
));
2808 ClearPageError(page
);
2810 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2811 if (page
->index
> end_index
||
2812 (page
->index
== end_index
&& !pg_offset
)) {
2813 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2818 if (page
->index
== end_index
) {
2821 userpage
= kmap_atomic(page
);
2822 memset(userpage
+ pg_offset
, 0,
2823 PAGE_CACHE_SIZE
- pg_offset
);
2824 kunmap_atomic(userpage
);
2825 flush_dcache_page(page
);
2829 set_page_extent_mapped(page
);
2831 if (!tree
->ops
|| !tree
->ops
->fill_delalloc
)
2832 fill_delalloc
= false;
2834 delalloc_start
= start
;
2837 if (!epd
->extent_locked
&& fill_delalloc
) {
2838 u64 delalloc_to_write
= 0;
2840 * make sure the wbc mapping index is at least updated
2843 update_nr_written(page
, wbc
, 0);
2845 while (delalloc_end
< page_end
) {
2846 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2851 if (nr_delalloc
== 0) {
2852 delalloc_start
= delalloc_end
+ 1;
2855 ret
= tree
->ops
->fill_delalloc(inode
, page
,
2860 /* File system has been set read-only */
2866 * delalloc_end is already one less than the total
2867 * length, so we don't subtract one from
2870 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2873 delalloc_start
= delalloc_end
+ 1;
2875 if (wbc
->nr_to_write
< delalloc_to_write
) {
2878 if (delalloc_to_write
< thresh
* 2)
2879 thresh
= delalloc_to_write
;
2880 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2884 /* did the fill delalloc function already unlock and start
2890 * we've unlocked the page, so we can't update
2891 * the mapping's writeback index, just update
2894 wbc
->nr_to_write
-= nr_written
;
2898 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2899 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2902 /* Fixup worker will requeue */
2904 wbc
->pages_skipped
++;
2906 redirty_page_for_writepage(wbc
, page
);
2907 update_nr_written(page
, wbc
, nr_written
);
2915 * we don't want to touch the inode after unlocking the page,
2916 * so we update the mapping writeback index now
2918 update_nr_written(page
, wbc
, nr_written
+ 1);
2921 if (last_byte
<= start
) {
2922 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2923 tree
->ops
->writepage_end_io_hook(page
, start
,
2928 blocksize
= inode
->i_sb
->s_blocksize
;
2930 while (cur
<= end
) {
2931 if (cur
>= last_byte
) {
2932 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2933 tree
->ops
->writepage_end_io_hook(page
, cur
,
2937 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2939 if (IS_ERR_OR_NULL(em
)) {
2944 extent_offset
= cur
- em
->start
;
2945 BUG_ON(extent_map_end(em
) <= cur
);
2947 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2948 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2949 sector
= (em
->block_start
+ extent_offset
) >> 9;
2951 block_start
= em
->block_start
;
2952 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2953 free_extent_map(em
);
2957 * compressed and inline extents are written through other
2960 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2961 block_start
== EXTENT_MAP_INLINE
) {
2963 * end_io notification does not happen here for
2964 * compressed extents
2966 if (!compressed
&& tree
->ops
&&
2967 tree
->ops
->writepage_end_io_hook
)
2968 tree
->ops
->writepage_end_io_hook(page
, cur
,
2971 else if (compressed
) {
2972 /* we don't want to end_page_writeback on
2973 * a compressed extent. this happens
2980 pg_offset
+= iosize
;
2983 /* leave this out until we have a page_mkwrite call */
2984 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2985 EXTENT_DIRTY
, 0, NULL
)) {
2987 pg_offset
+= iosize
;
2991 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2992 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
3000 unsigned long max_nr
= end_index
+ 1;
3002 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
3003 if (!PageWriteback(page
)) {
3004 printk(KERN_ERR
"btrfs warning page %lu not "
3005 "writeback, cur %llu end %llu\n",
3006 page
->index
, (unsigned long long)cur
,
3007 (unsigned long long)end
);
3010 ret
= submit_extent_page(write_flags
, tree
, page
,
3011 sector
, iosize
, pg_offset
,
3012 bdev
, &epd
->bio
, max_nr
,
3013 end_bio_extent_writepage
,
3019 pg_offset
+= iosize
;
3024 /* make sure the mapping tag for page dirty gets cleared */
3025 set_page_writeback(page
);
3026 end_page_writeback(page
);
3032 /* drop our reference on any cached states */
3033 free_extent_state(cached_state
);
3037 static int eb_wait(void *word
)
3043 static void wait_on_extent_buffer_writeback(struct extent_buffer
*eb
)
3045 wait_on_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
, eb_wait
,
3046 TASK_UNINTERRUPTIBLE
);
3049 static int lock_extent_buffer_for_io(struct extent_buffer
*eb
,
3050 struct btrfs_fs_info
*fs_info
,
3051 struct extent_page_data
*epd
)
3053 unsigned long i
, num_pages
;
3057 if (!btrfs_try_tree_write_lock(eb
)) {
3059 flush_write_bio(epd
);
3060 btrfs_tree_lock(eb
);
3063 if (test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
)) {
3064 btrfs_tree_unlock(eb
);
3068 flush_write_bio(epd
);
3072 wait_on_extent_buffer_writeback(eb
);
3073 btrfs_tree_lock(eb
);
3074 if (!test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
))
3076 btrfs_tree_unlock(eb
);
3080 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3081 set_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3082 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
3083 spin_lock(&fs_info
->delalloc_lock
);
3084 if (fs_info
->dirty_metadata_bytes
>= eb
->len
)
3085 fs_info
->dirty_metadata_bytes
-= eb
->len
;
3088 spin_unlock(&fs_info
->delalloc_lock
);
3092 btrfs_tree_unlock(eb
);
3097 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3098 for (i
= 0; i
< num_pages
; i
++) {
3099 struct page
*p
= extent_buffer_page(eb
, i
);
3101 if (!trylock_page(p
)) {
3103 flush_write_bio(epd
);
3113 static void end_extent_buffer_writeback(struct extent_buffer
*eb
)
3115 clear_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
);
3116 smp_mb__after_clear_bit();
3117 wake_up_bit(&eb
->bflags
, EXTENT_BUFFER_WRITEBACK
);
3120 static void end_bio_extent_buffer_writepage(struct bio
*bio
, int err
)
3122 int uptodate
= err
== 0;
3123 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
3124 struct extent_buffer
*eb
;
3128 struct page
*page
= bvec
->bv_page
;
3131 eb
= (struct extent_buffer
*)page
->private;
3133 done
= atomic_dec_and_test(&eb
->io_pages
);
3135 if (!uptodate
|| test_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
)) {
3136 set_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3137 ClearPageUptodate(page
);
3141 end_page_writeback(page
);
3146 end_extent_buffer_writeback(eb
);
3147 } while (bvec
>= bio
->bi_io_vec
);
3153 static int write_one_eb(struct extent_buffer
*eb
,
3154 struct btrfs_fs_info
*fs_info
,
3155 struct writeback_control
*wbc
,
3156 struct extent_page_data
*epd
)
3158 struct block_device
*bdev
= fs_info
->fs_devices
->latest_bdev
;
3159 u64 offset
= eb
->start
;
3160 unsigned long i
, num_pages
;
3161 int rw
= (epd
->sync_io
? WRITE_SYNC
: WRITE
);
3164 clear_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3165 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3166 atomic_set(&eb
->io_pages
, num_pages
);
3167 for (i
= 0; i
< num_pages
; i
++) {
3168 struct page
*p
= extent_buffer_page(eb
, i
);
3170 clear_page_dirty_for_io(p
);
3171 set_page_writeback(p
);
3172 ret
= submit_extent_page(rw
, eb
->tree
, p
, offset
>> 9,
3173 PAGE_CACHE_SIZE
, 0, bdev
, &epd
->bio
,
3174 -1, end_bio_extent_buffer_writepage
,
3177 set_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
3179 if (atomic_sub_and_test(num_pages
- i
, &eb
->io_pages
))
3180 end_extent_buffer_writeback(eb
);
3184 offset
+= PAGE_CACHE_SIZE
;
3185 update_nr_written(p
, wbc
, 1);
3189 if (unlikely(ret
)) {
3190 for (; i
< num_pages
; i
++) {
3191 struct page
*p
= extent_buffer_page(eb
, i
);
3199 int btree_write_cache_pages(struct address_space
*mapping
,
3200 struct writeback_control
*wbc
)
3202 struct extent_io_tree
*tree
= &BTRFS_I(mapping
->host
)->io_tree
;
3203 struct btrfs_fs_info
*fs_info
= BTRFS_I(mapping
->host
)->root
->fs_info
;
3204 struct extent_buffer
*eb
, *prev_eb
= NULL
;
3205 struct extent_page_data epd
= {
3209 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3213 int nr_to_write_done
= 0;
3214 struct pagevec pvec
;
3217 pgoff_t end
; /* Inclusive */
3221 pagevec_init(&pvec
, 0);
3222 if (wbc
->range_cyclic
) {
3223 index
= mapping
->writeback_index
; /* Start from prev offset */
3226 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3227 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3230 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3231 tag
= PAGECACHE_TAG_TOWRITE
;
3233 tag
= PAGECACHE_TAG_DIRTY
;
3235 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3236 tag_pages_for_writeback(mapping
, index
, end
);
3237 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3238 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3239 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3243 for (i
= 0; i
< nr_pages
; i
++) {
3244 struct page
*page
= pvec
.pages
[i
];
3246 if (!PagePrivate(page
))
3249 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3254 eb
= (struct extent_buffer
*)page
->private;
3263 if (!atomic_inc_not_zero(&eb
->refs
)) {
3269 ret
= lock_extent_buffer_for_io(eb
, fs_info
, &epd
);
3271 free_extent_buffer(eb
);
3275 ret
= write_one_eb(eb
, fs_info
, wbc
, &epd
);
3278 free_extent_buffer(eb
);
3281 free_extent_buffer(eb
);
3284 * the filesystem may choose to bump up nr_to_write.
3285 * We have to make sure to honor the new nr_to_write
3288 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3290 pagevec_release(&pvec
);
3293 if (!scanned
&& !done
) {
3295 * We hit the last page and there is more work to be done: wrap
3296 * back to the start of the file
3302 flush_write_bio(&epd
);
3307 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3308 * @mapping: address space structure to write
3309 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3310 * @writepage: function called for each page
3311 * @data: data passed to writepage function
3313 * If a page is already under I/O, write_cache_pages() skips it, even
3314 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3315 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3316 * and msync() need to guarantee that all the data which was dirty at the time
3317 * the call was made get new I/O started against them. If wbc->sync_mode is
3318 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3319 * existing IO to complete.
3321 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
3322 struct address_space
*mapping
,
3323 struct writeback_control
*wbc
,
3324 writepage_t writepage
, void *data
,
3325 void (*flush_fn
)(void *))
3329 int nr_to_write_done
= 0;
3330 struct pagevec pvec
;
3333 pgoff_t end
; /* Inclusive */
3337 pagevec_init(&pvec
, 0);
3338 if (wbc
->range_cyclic
) {
3339 index
= mapping
->writeback_index
; /* Start from prev offset */
3342 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3343 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3346 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3347 tag
= PAGECACHE_TAG_TOWRITE
;
3349 tag
= PAGECACHE_TAG_DIRTY
;
3351 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3352 tag_pages_for_writeback(mapping
, index
, end
);
3353 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3354 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3355 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3359 for (i
= 0; i
< nr_pages
; i
++) {
3360 struct page
*page
= pvec
.pages
[i
];
3363 * At this point we hold neither mapping->tree_lock nor
3364 * lock on the page itself: the page may be truncated or
3365 * invalidated (changing page->mapping to NULL), or even
3366 * swizzled back from swapper_space to tmpfs file
3370 tree
->ops
->write_cache_pages_lock_hook
) {
3371 tree
->ops
->write_cache_pages_lock_hook(page
,
3374 if (!trylock_page(page
)) {
3380 if (unlikely(page
->mapping
!= mapping
)) {
3385 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3391 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3392 if (PageWriteback(page
))
3394 wait_on_page_writeback(page
);
3397 if (PageWriteback(page
) ||
3398 !clear_page_dirty_for_io(page
)) {
3403 ret
= (*writepage
)(page
, wbc
, data
);
3405 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
3413 * the filesystem may choose to bump up nr_to_write.
3414 * We have to make sure to honor the new nr_to_write
3417 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3419 pagevec_release(&pvec
);
3422 if (!scanned
&& !done
) {
3424 * We hit the last page and there is more work to be done: wrap
3425 * back to the start of the file
3434 static void flush_epd_write_bio(struct extent_page_data
*epd
)
3443 ret
= submit_one_bio(rw
, epd
->bio
, 0, 0);
3444 BUG_ON(ret
< 0); /* -ENOMEM */
3449 static noinline
void flush_write_bio(void *data
)
3451 struct extent_page_data
*epd
= data
;
3452 flush_epd_write_bio(epd
);
3455 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
3456 get_extent_t
*get_extent
,
3457 struct writeback_control
*wbc
)
3460 struct extent_page_data epd
= {
3463 .get_extent
= get_extent
,
3465 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3468 ret
= __extent_writepage(page
, wbc
, &epd
);
3470 flush_epd_write_bio(&epd
);
3474 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
3475 u64 start
, u64 end
, get_extent_t
*get_extent
,
3479 struct address_space
*mapping
= inode
->i_mapping
;
3481 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
3484 struct extent_page_data epd
= {
3487 .get_extent
= get_extent
,
3489 .sync_io
= mode
== WB_SYNC_ALL
,
3491 struct writeback_control wbc_writepages
= {
3493 .nr_to_write
= nr_pages
* 2,
3494 .range_start
= start
,
3495 .range_end
= end
+ 1,
3498 while (start
<= end
) {
3499 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
3500 if (clear_page_dirty_for_io(page
))
3501 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
3503 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
3504 tree
->ops
->writepage_end_io_hook(page
, start
,
3505 start
+ PAGE_CACHE_SIZE
- 1,
3509 page_cache_release(page
);
3510 start
+= PAGE_CACHE_SIZE
;
3513 flush_epd_write_bio(&epd
);
3517 int extent_writepages(struct extent_io_tree
*tree
,
3518 struct address_space
*mapping
,
3519 get_extent_t
*get_extent
,
3520 struct writeback_control
*wbc
)
3523 struct extent_page_data epd
= {
3526 .get_extent
= get_extent
,
3528 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3531 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
3532 __extent_writepage
, &epd
,
3534 flush_epd_write_bio(&epd
);
3538 int extent_readpages(struct extent_io_tree
*tree
,
3539 struct address_space
*mapping
,
3540 struct list_head
*pages
, unsigned nr_pages
,
3541 get_extent_t get_extent
)
3543 struct bio
*bio
= NULL
;
3545 unsigned long bio_flags
= 0;
3547 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
3548 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
3550 prefetchw(&page
->flags
);
3551 list_del(&page
->lru
);
3552 if (!add_to_page_cache_lru(page
, mapping
,
3553 page
->index
, GFP_NOFS
)) {
3554 __extent_read_full_page(tree
, page
, get_extent
,
3555 &bio
, 0, &bio_flags
);
3557 page_cache_release(page
);
3559 BUG_ON(!list_empty(pages
));
3561 return submit_one_bio(READ
, bio
, 0, bio_flags
);
3566 * basic invalidatepage code, this waits on any locked or writeback
3567 * ranges corresponding to the page, and then deletes any extent state
3568 * records from the tree
3570 int extent_invalidatepage(struct extent_io_tree
*tree
,
3571 struct page
*page
, unsigned long offset
)
3573 struct extent_state
*cached_state
= NULL
;
3574 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
3575 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3576 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
3578 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
3582 lock_extent_bits(tree
, start
, end
, 0, &cached_state
);
3583 wait_on_page_writeback(page
);
3584 clear_extent_bit(tree
, start
, end
,
3585 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
3586 EXTENT_DO_ACCOUNTING
,
3587 1, 1, &cached_state
, GFP_NOFS
);
3592 * a helper for releasepage, this tests for areas of the page that
3593 * are locked or under IO and drops the related state bits if it is safe
3596 int try_release_extent_state(struct extent_map_tree
*map
,
3597 struct extent_io_tree
*tree
, struct page
*page
,
3600 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3601 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3604 if (test_range_bit(tree
, start
, end
,
3605 EXTENT_IOBITS
, 0, NULL
))
3608 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
3611 * at this point we can safely clear everything except the
3612 * locked bit and the nodatasum bit
3614 ret
= clear_extent_bit(tree
, start
, end
,
3615 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
3618 /* if clear_extent_bit failed for enomem reasons,
3619 * we can't allow the release to continue.
3630 * a helper for releasepage. As long as there are no locked extents
3631 * in the range corresponding to the page, both state records and extent
3632 * map records are removed
3634 int try_release_extent_mapping(struct extent_map_tree
*map
,
3635 struct extent_io_tree
*tree
, struct page
*page
,
3638 struct extent_map
*em
;
3639 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3640 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3642 if ((mask
& __GFP_WAIT
) &&
3643 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
3645 while (start
<= end
) {
3646 len
= end
- start
+ 1;
3647 write_lock(&map
->lock
);
3648 em
= lookup_extent_mapping(map
, start
, len
);
3650 write_unlock(&map
->lock
);
3653 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
3654 em
->start
!= start
) {
3655 write_unlock(&map
->lock
);
3656 free_extent_map(em
);
3659 if (!test_range_bit(tree
, em
->start
,
3660 extent_map_end(em
) - 1,
3661 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
3663 remove_extent_mapping(map
, em
);
3664 /* once for the rb tree */
3665 free_extent_map(em
);
3667 start
= extent_map_end(em
);
3668 write_unlock(&map
->lock
);
3671 free_extent_map(em
);
3674 return try_release_extent_state(map
, tree
, page
, mask
);
3678 * helper function for fiemap, which doesn't want to see any holes.
3679 * This maps until we find something past 'last'
3681 static struct extent_map
*get_extent_skip_holes(struct inode
*inode
,
3684 get_extent_t
*get_extent
)
3686 u64 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
3687 struct extent_map
*em
;
3694 len
= last
- offset
;
3697 len
= (len
+ sectorsize
- 1) & ~(sectorsize
- 1);
3698 em
= get_extent(inode
, NULL
, 0, offset
, len
, 0);
3699 if (IS_ERR_OR_NULL(em
))
3702 /* if this isn't a hole return it */
3703 if (!test_bit(EXTENT_FLAG_VACANCY
, &em
->flags
) &&
3704 em
->block_start
!= EXTENT_MAP_HOLE
) {
3708 /* this is a hole, advance to the next extent */
3709 offset
= extent_map_end(em
);
3710 free_extent_map(em
);
3717 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3718 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
3722 u64 max
= start
+ len
;
3726 u64 last_for_get_extent
= 0;
3728 u64 isize
= i_size_read(inode
);
3729 struct btrfs_key found_key
;
3730 struct extent_map
*em
= NULL
;
3731 struct extent_state
*cached_state
= NULL
;
3732 struct btrfs_path
*path
;
3733 struct btrfs_file_extent_item
*item
;
3738 unsigned long emflags
;
3743 path
= btrfs_alloc_path();
3746 path
->leave_spinning
= 1;
3748 start
= ALIGN(start
, BTRFS_I(inode
)->root
->sectorsize
);
3749 len
= ALIGN(len
, BTRFS_I(inode
)->root
->sectorsize
);
3752 * lookup the last file extent. We're not using i_size here
3753 * because there might be preallocation past i_size
3755 ret
= btrfs_lookup_file_extent(NULL
, BTRFS_I(inode
)->root
,
3756 path
, btrfs_ino(inode
), -1, 0);
3758 btrfs_free_path(path
);
3763 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3764 struct btrfs_file_extent_item
);
3765 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, path
->slots
[0]);
3766 found_type
= btrfs_key_type(&found_key
);
3768 /* No extents, but there might be delalloc bits */
3769 if (found_key
.objectid
!= btrfs_ino(inode
) ||
3770 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
3771 /* have to trust i_size as the end */
3773 last_for_get_extent
= isize
;
3776 * remember the start of the last extent. There are a
3777 * bunch of different factors that go into the length of the
3778 * extent, so its much less complex to remember where it started
3780 last
= found_key
.offset
;
3781 last_for_get_extent
= last
+ 1;
3783 btrfs_free_path(path
);
3786 * we might have some extents allocated but more delalloc past those
3787 * extents. so, we trust isize unless the start of the last extent is
3792 last_for_get_extent
= isize
;
3795 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
3798 em
= get_extent_skip_holes(inode
, start
, last_for_get_extent
,
3808 u64 offset_in_extent
;
3810 /* break if the extent we found is outside the range */
3811 if (em
->start
>= max
|| extent_map_end(em
) < off
)
3815 * get_extent may return an extent that starts before our
3816 * requested range. We have to make sure the ranges
3817 * we return to fiemap always move forward and don't
3818 * overlap, so adjust the offsets here
3820 em_start
= max(em
->start
, off
);
3823 * record the offset from the start of the extent
3824 * for adjusting the disk offset below
3826 offset_in_extent
= em_start
- em
->start
;
3827 em_end
= extent_map_end(em
);
3828 em_len
= em_end
- em_start
;
3829 emflags
= em
->flags
;
3834 * bump off for our next call to get_extent
3836 off
= extent_map_end(em
);
3840 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
3842 flags
|= FIEMAP_EXTENT_LAST
;
3843 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
3844 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
3845 FIEMAP_EXTENT_NOT_ALIGNED
);
3846 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
3847 flags
|= (FIEMAP_EXTENT_DELALLOC
|
3848 FIEMAP_EXTENT_UNKNOWN
);
3850 disko
= em
->block_start
+ offset_in_extent
;
3852 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
3853 flags
|= FIEMAP_EXTENT_ENCODED
;
3855 free_extent_map(em
);
3857 if ((em_start
>= last
) || em_len
== (u64
)-1 ||
3858 (last
== (u64
)-1 && isize
<= em_end
)) {
3859 flags
|= FIEMAP_EXTENT_LAST
;
3863 /* now scan forward to see if this is really the last extent. */
3864 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
,
3871 flags
|= FIEMAP_EXTENT_LAST
;
3874 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3880 free_extent_map(em
);
3882 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3883 &cached_state
, GFP_NOFS
);
3887 inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
3890 return eb
->pages
[i
];
3893 inline unsigned long num_extent_pages(u64 start
, u64 len
)
3895 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
3896 (start
>> PAGE_CACHE_SHIFT
);
3899 static void __free_extent_buffer(struct extent_buffer
*eb
)
3902 unsigned long flags
;
3903 spin_lock_irqsave(&leak_lock
, flags
);
3904 list_del(&eb
->leak_list
);
3905 spin_unlock_irqrestore(&leak_lock
, flags
);
3907 if (eb
->pages
&& eb
->pages
!= eb
->inline_pages
)
3909 kmem_cache_free(extent_buffer_cache
, eb
);
3912 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3917 struct extent_buffer
*eb
= NULL
;
3919 unsigned long flags
;
3922 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
3929 rwlock_init(&eb
->lock
);
3930 atomic_set(&eb
->write_locks
, 0);
3931 atomic_set(&eb
->read_locks
, 0);
3932 atomic_set(&eb
->blocking_readers
, 0);
3933 atomic_set(&eb
->blocking_writers
, 0);
3934 atomic_set(&eb
->spinning_readers
, 0);
3935 atomic_set(&eb
->spinning_writers
, 0);
3936 eb
->lock_nested
= 0;
3937 init_waitqueue_head(&eb
->write_lock_wq
);
3938 init_waitqueue_head(&eb
->read_lock_wq
);
3941 spin_lock_irqsave(&leak_lock
, flags
);
3942 list_add(&eb
->leak_list
, &buffers
);
3943 spin_unlock_irqrestore(&leak_lock
, flags
);
3945 spin_lock_init(&eb
->refs_lock
);
3946 atomic_set(&eb
->refs
, 1);
3947 atomic_set(&eb
->io_pages
, 0);
3949 if (len
> MAX_INLINE_EXTENT_BUFFER_SIZE
) {
3950 struct page
**pages
;
3951 int num_pages
= (len
+ PAGE_CACHE_SIZE
- 1) >>
3953 pages
= kzalloc(num_pages
, mask
);
3955 __free_extent_buffer(eb
);
3960 eb
->pages
= eb
->inline_pages
;
3966 struct extent_buffer
*btrfs_clone_extent_buffer(struct extent_buffer
*src
)
3970 struct extent_buffer
*new;
3971 unsigned long num_pages
= num_extent_pages(src
->start
, src
->len
);
3973 new = __alloc_extent_buffer(NULL
, src
->start
, src
->len
, GFP_ATOMIC
);
3977 for (i
= 0; i
< num_pages
; i
++) {
3978 p
= alloc_page(GFP_ATOMIC
);
3980 attach_extent_buffer_page(new, p
);
3981 WARN_ON(PageDirty(p
));
3986 copy_extent_buffer(new, src
, 0, 0, src
->len
);
3987 set_bit(EXTENT_BUFFER_UPTODATE
, &new->bflags
);
3988 set_bit(EXTENT_BUFFER_DUMMY
, &new->bflags
);
3993 struct extent_buffer
*alloc_dummy_extent_buffer(u64 start
, unsigned long len
)
3995 struct extent_buffer
*eb
;
3996 unsigned long num_pages
= num_extent_pages(0, len
);
3999 eb
= __alloc_extent_buffer(NULL
, start
, len
, GFP_ATOMIC
);
4003 for (i
= 0; i
< num_pages
; i
++) {
4004 eb
->pages
[i
] = alloc_page(GFP_ATOMIC
);
4008 set_extent_buffer_uptodate(eb
);
4009 btrfs_set_header_nritems(eb
, 0);
4010 set_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
);
4014 for (i
--; i
> 0; i
--)
4015 __free_page(eb
->pages
[i
]);
4016 __free_extent_buffer(eb
);
4020 static int extent_buffer_under_io(struct extent_buffer
*eb
)
4022 return (atomic_read(&eb
->io_pages
) ||
4023 test_bit(EXTENT_BUFFER_WRITEBACK
, &eb
->bflags
) ||
4024 test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4028 * Helper for releasing extent buffer page.
4030 static void btrfs_release_extent_buffer_page(struct extent_buffer
*eb
,
4031 unsigned long start_idx
)
4033 unsigned long index
;
4034 unsigned long num_pages
;
4036 int mapped
= !test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
);
4038 BUG_ON(extent_buffer_under_io(eb
));
4040 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4041 index
= start_idx
+ num_pages
;
4042 if (start_idx
>= index
)
4047 page
= extent_buffer_page(eb
, index
);
4048 if (page
&& mapped
) {
4049 spin_lock(&page
->mapping
->private_lock
);
4051 * We do this since we'll remove the pages after we've
4052 * removed the eb from the radix tree, so we could race
4053 * and have this page now attached to the new eb. So
4054 * only clear page_private if it's still connected to
4057 if (PagePrivate(page
) &&
4058 page
->private == (unsigned long)eb
) {
4059 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
));
4060 BUG_ON(PageDirty(page
));
4061 BUG_ON(PageWriteback(page
));
4063 * We need to make sure we haven't be attached
4066 ClearPagePrivate(page
);
4067 set_page_private(page
, 0);
4068 /* One for the page private */
4069 page_cache_release(page
);
4071 spin_unlock(&page
->mapping
->private_lock
);
4075 /* One for when we alloced the page */
4076 page_cache_release(page
);
4078 } while (index
!= start_idx
);
4082 * Helper for releasing the extent buffer.
4084 static inline void btrfs_release_extent_buffer(struct extent_buffer
*eb
)
4086 btrfs_release_extent_buffer_page(eb
, 0);
4087 __free_extent_buffer(eb
);
4090 static void check_buffer_tree_ref(struct extent_buffer
*eb
)
4092 /* the ref bit is tricky. We have to make sure it is set
4093 * if we have the buffer dirty. Otherwise the
4094 * code to free a buffer can end up dropping a dirty
4097 * Once the ref bit is set, it won't go away while the
4098 * buffer is dirty or in writeback, and it also won't
4099 * go away while we have the reference count on the
4102 * We can't just set the ref bit without bumping the
4103 * ref on the eb because free_extent_buffer might
4104 * see the ref bit and try to clear it. If this happens
4105 * free_extent_buffer might end up dropping our original
4106 * ref by mistake and freeing the page before we are able
4107 * to add one more ref.
4109 * So bump the ref count first, then set the bit. If someone
4110 * beat us to it, drop the ref we added.
4112 if (!test_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
)) {
4113 atomic_inc(&eb
->refs
);
4114 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4115 atomic_dec(&eb
->refs
);
4119 static void mark_extent_buffer_accessed(struct extent_buffer
*eb
)
4121 unsigned long num_pages
, i
;
4123 check_buffer_tree_ref(eb
);
4125 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4126 for (i
= 0; i
< num_pages
; i
++) {
4127 struct page
*p
= extent_buffer_page(eb
, i
);
4128 mark_page_accessed(p
);
4132 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
4133 u64 start
, unsigned long len
)
4135 unsigned long num_pages
= num_extent_pages(start
, len
);
4137 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
4138 struct extent_buffer
*eb
;
4139 struct extent_buffer
*exists
= NULL
;
4141 struct address_space
*mapping
= tree
->mapping
;
4146 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4147 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4149 mark_extent_buffer_accessed(eb
);
4154 eb
= __alloc_extent_buffer(tree
, start
, len
, GFP_NOFS
);
4158 for (i
= 0; i
< num_pages
; i
++, index
++) {
4159 p
= find_or_create_page(mapping
, index
, GFP_NOFS
);
4165 spin_lock(&mapping
->private_lock
);
4166 if (PagePrivate(p
)) {
4168 * We could have already allocated an eb for this page
4169 * and attached one so lets see if we can get a ref on
4170 * the existing eb, and if we can we know it's good and
4171 * we can just return that one, else we know we can just
4172 * overwrite page->private.
4174 exists
= (struct extent_buffer
*)p
->private;
4175 if (atomic_inc_not_zero(&exists
->refs
)) {
4176 spin_unlock(&mapping
->private_lock
);
4178 page_cache_release(p
);
4179 mark_extent_buffer_accessed(exists
);
4184 * Do this so attach doesn't complain and we need to
4185 * drop the ref the old guy had.
4187 ClearPagePrivate(p
);
4188 WARN_ON(PageDirty(p
));
4189 page_cache_release(p
);
4191 attach_extent_buffer_page(eb
, p
);
4192 spin_unlock(&mapping
->private_lock
);
4193 WARN_ON(PageDirty(p
));
4194 mark_page_accessed(p
);
4196 if (!PageUptodate(p
))
4200 * see below about how we avoid a nasty race with release page
4201 * and why we unlock later
4205 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4207 ret
= radix_tree_preload(GFP_NOFS
& ~__GFP_HIGHMEM
);
4211 spin_lock(&tree
->buffer_lock
);
4212 ret
= radix_tree_insert(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
, eb
);
4213 if (ret
== -EEXIST
) {
4214 exists
= radix_tree_lookup(&tree
->buffer
,
4215 start
>> PAGE_CACHE_SHIFT
);
4216 if (!atomic_inc_not_zero(&exists
->refs
)) {
4217 spin_unlock(&tree
->buffer_lock
);
4218 radix_tree_preload_end();
4222 spin_unlock(&tree
->buffer_lock
);
4223 radix_tree_preload_end();
4224 mark_extent_buffer_accessed(exists
);
4227 /* add one reference for the tree */
4228 spin_lock(&eb
->refs_lock
);
4229 check_buffer_tree_ref(eb
);
4230 spin_unlock(&eb
->refs_lock
);
4231 spin_unlock(&tree
->buffer_lock
);
4232 radix_tree_preload_end();
4235 * there is a race where release page may have
4236 * tried to find this extent buffer in the radix
4237 * but failed. It will tell the VM it is safe to
4238 * reclaim the, and it will clear the page private bit.
4239 * We must make sure to set the page private bit properly
4240 * after the extent buffer is in the radix tree so
4241 * it doesn't get lost
4243 SetPageChecked(eb
->pages
[0]);
4244 for (i
= 1; i
< num_pages
; i
++) {
4245 p
= extent_buffer_page(eb
, i
);
4246 ClearPageChecked(p
);
4249 unlock_page(eb
->pages
[0]);
4253 for (i
= 0; i
< num_pages
; i
++) {
4255 unlock_page(eb
->pages
[i
]);
4258 WARN_ON(!atomic_dec_and_test(&eb
->refs
));
4259 btrfs_release_extent_buffer(eb
);
4263 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
4264 u64 start
, unsigned long len
)
4266 struct extent_buffer
*eb
;
4269 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4270 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
4272 mark_extent_buffer_accessed(eb
);
4280 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head
*head
)
4282 struct extent_buffer
*eb
=
4283 container_of(head
, struct extent_buffer
, rcu_head
);
4285 __free_extent_buffer(eb
);
4288 /* Expects to have eb->eb_lock already held */
4289 static void release_extent_buffer(struct extent_buffer
*eb
, gfp_t mask
)
4291 WARN_ON(atomic_read(&eb
->refs
) == 0);
4292 if (atomic_dec_and_test(&eb
->refs
)) {
4293 if (test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
)) {
4294 spin_unlock(&eb
->refs_lock
);
4296 struct extent_io_tree
*tree
= eb
->tree
;
4298 spin_unlock(&eb
->refs_lock
);
4300 spin_lock(&tree
->buffer_lock
);
4301 radix_tree_delete(&tree
->buffer
,
4302 eb
->start
>> PAGE_CACHE_SHIFT
);
4303 spin_unlock(&tree
->buffer_lock
);
4306 /* Should be safe to release our pages at this point */
4307 btrfs_release_extent_buffer_page(eb
, 0);
4309 call_rcu(&eb
->rcu_head
, btrfs_release_extent_buffer_rcu
);
4312 spin_unlock(&eb
->refs_lock
);
4315 void free_extent_buffer(struct extent_buffer
*eb
)
4320 spin_lock(&eb
->refs_lock
);
4321 if (atomic_read(&eb
->refs
) == 2 &&
4322 test_bit(EXTENT_BUFFER_DUMMY
, &eb
->bflags
))
4323 atomic_dec(&eb
->refs
);
4325 if (atomic_read(&eb
->refs
) == 2 &&
4326 test_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
) &&
4327 !extent_buffer_under_io(eb
) &&
4328 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4329 atomic_dec(&eb
->refs
);
4332 * I know this is terrible, but it's temporary until we stop tracking
4333 * the uptodate bits and such for the extent buffers.
4335 release_extent_buffer(eb
, GFP_ATOMIC
);
4338 void free_extent_buffer_stale(struct extent_buffer
*eb
)
4343 spin_lock(&eb
->refs_lock
);
4344 set_bit(EXTENT_BUFFER_STALE
, &eb
->bflags
);
4346 if (atomic_read(&eb
->refs
) == 2 && !extent_buffer_under_io(eb
) &&
4347 test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
))
4348 atomic_dec(&eb
->refs
);
4349 release_extent_buffer(eb
, GFP_NOFS
);
4352 void clear_extent_buffer_dirty(struct extent_buffer
*eb
)
4355 unsigned long num_pages
;
4358 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4360 for (i
= 0; i
< num_pages
; i
++) {
4361 page
= extent_buffer_page(eb
, i
);
4362 if (!PageDirty(page
))
4366 WARN_ON(!PagePrivate(page
));
4368 clear_page_dirty_for_io(page
);
4369 spin_lock_irq(&page
->mapping
->tree_lock
);
4370 if (!PageDirty(page
)) {
4371 radix_tree_tag_clear(&page
->mapping
->page_tree
,
4373 PAGECACHE_TAG_DIRTY
);
4375 spin_unlock_irq(&page
->mapping
->tree_lock
);
4376 ClearPageError(page
);
4379 WARN_ON(atomic_read(&eb
->refs
) == 0);
4382 int set_extent_buffer_dirty(struct extent_buffer
*eb
)
4385 unsigned long num_pages
;
4388 check_buffer_tree_ref(eb
);
4390 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
4392 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4393 WARN_ON(atomic_read(&eb
->refs
) == 0);
4394 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
));
4396 for (i
= 0; i
< num_pages
; i
++)
4397 set_page_dirty(extent_buffer_page(eb
, i
));
4401 static int range_straddles_pages(u64 start
, u64 len
)
4403 if (len
< PAGE_CACHE_SIZE
)
4405 if (start
& (PAGE_CACHE_SIZE
- 1))
4407 if ((start
+ len
) & (PAGE_CACHE_SIZE
- 1))
4412 int clear_extent_buffer_uptodate(struct extent_buffer
*eb
)
4416 unsigned long num_pages
;
4418 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4419 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4420 for (i
= 0; i
< num_pages
; i
++) {
4421 page
= extent_buffer_page(eb
, i
);
4423 ClearPageUptodate(page
);
4428 int set_extent_buffer_uptodate(struct extent_buffer
*eb
)
4432 unsigned long num_pages
;
4434 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4435 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4436 for (i
= 0; i
< num_pages
; i
++) {
4437 page
= extent_buffer_page(eb
, i
);
4438 SetPageUptodate(page
);
4443 int extent_range_uptodate(struct extent_io_tree
*tree
,
4448 int pg_uptodate
= 1;
4450 unsigned long index
;
4452 if (range_straddles_pages(start
, end
- start
+ 1)) {
4453 ret
= test_range_bit(tree
, start
, end
,
4454 EXTENT_UPTODATE
, 1, NULL
);
4458 while (start
<= end
) {
4459 index
= start
>> PAGE_CACHE_SHIFT
;
4460 page
= find_get_page(tree
->mapping
, index
);
4463 uptodate
= PageUptodate(page
);
4464 page_cache_release(page
);
4469 start
+= PAGE_CACHE_SIZE
;
4474 int extent_buffer_uptodate(struct extent_buffer
*eb
)
4476 return test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4479 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
4480 struct extent_buffer
*eb
, u64 start
, int wait
,
4481 get_extent_t
*get_extent
, int mirror_num
)
4484 unsigned long start_i
;
4488 int locked_pages
= 0;
4489 int all_uptodate
= 1;
4490 unsigned long num_pages
;
4491 unsigned long num_reads
= 0;
4492 struct bio
*bio
= NULL
;
4493 unsigned long bio_flags
= 0;
4495 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
4499 WARN_ON(start
< eb
->start
);
4500 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
4501 (eb
->start
>> PAGE_CACHE_SHIFT
);
4506 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4507 for (i
= start_i
; i
< num_pages
; i
++) {
4508 page
= extent_buffer_page(eb
, i
);
4509 if (wait
== WAIT_NONE
) {
4510 if (!trylock_page(page
))
4516 if (!PageUptodate(page
)) {
4523 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4527 clear_bit(EXTENT_BUFFER_IOERR
, &eb
->bflags
);
4528 eb
->read_mirror
= 0;
4529 atomic_set(&eb
->io_pages
, num_reads
);
4530 for (i
= start_i
; i
< num_pages
; i
++) {
4531 page
= extent_buffer_page(eb
, i
);
4532 if (!PageUptodate(page
)) {
4533 ClearPageError(page
);
4534 err
= __extent_read_full_page(tree
, page
,
4536 mirror_num
, &bio_flags
);
4545 err
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
4550 if (ret
|| wait
!= WAIT_COMPLETE
)
4553 for (i
= start_i
; i
< num_pages
; i
++) {
4554 page
= extent_buffer_page(eb
, i
);
4555 wait_on_page_locked(page
);
4556 if (!PageUptodate(page
))
4564 while (locked_pages
> 0) {
4565 page
= extent_buffer_page(eb
, i
);
4573 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
4574 unsigned long start
,
4581 char *dst
= (char *)dstv
;
4582 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4583 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4585 WARN_ON(start
> eb
->len
);
4586 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4588 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4591 page
= extent_buffer_page(eb
, i
);
4593 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4594 kaddr
= page_address(page
);
4595 memcpy(dst
, kaddr
+ offset
, cur
);
4604 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
4605 unsigned long min_len
, char **map
,
4606 unsigned long *map_start
,
4607 unsigned long *map_len
)
4609 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
4612 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4613 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4614 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
4621 offset
= start_offset
;
4625 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
4628 if (start
+ min_len
> eb
->len
) {
4629 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
4630 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
4631 eb
->len
, start
, min_len
);
4636 p
= extent_buffer_page(eb
, i
);
4637 kaddr
= page_address(p
);
4638 *map
= kaddr
+ offset
;
4639 *map_len
= PAGE_CACHE_SIZE
- offset
;
4643 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
4644 unsigned long start
,
4651 char *ptr
= (char *)ptrv
;
4652 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4653 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4656 WARN_ON(start
> eb
->len
);
4657 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4659 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4662 page
= extent_buffer_page(eb
, i
);
4664 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4666 kaddr
= page_address(page
);
4667 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
4679 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
4680 unsigned long start
, unsigned long len
)
4686 char *src
= (char *)srcv
;
4687 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4688 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4690 WARN_ON(start
> eb
->len
);
4691 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4693 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4696 page
= extent_buffer_page(eb
, i
);
4697 WARN_ON(!PageUptodate(page
));
4699 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4700 kaddr
= page_address(page
);
4701 memcpy(kaddr
+ offset
, src
, cur
);
4710 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
4711 unsigned long start
, unsigned long len
)
4717 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4718 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4720 WARN_ON(start
> eb
->len
);
4721 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4723 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4726 page
= extent_buffer_page(eb
, i
);
4727 WARN_ON(!PageUptodate(page
));
4729 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4730 kaddr
= page_address(page
);
4731 memset(kaddr
+ offset
, c
, cur
);
4739 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
4740 unsigned long dst_offset
, unsigned long src_offset
,
4743 u64 dst_len
= dst
->len
;
4748 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4749 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4751 WARN_ON(src
->len
!= dst_len
);
4753 offset
= (start_offset
+ dst_offset
) &
4754 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4757 page
= extent_buffer_page(dst
, i
);
4758 WARN_ON(!PageUptodate(page
));
4760 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
4762 kaddr
= page_address(page
);
4763 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
4772 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
4773 unsigned long dst_off
, unsigned long src_off
,
4776 char *dst_kaddr
= page_address(dst_page
);
4777 if (dst_page
== src_page
) {
4778 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
4780 char *src_kaddr
= page_address(src_page
);
4781 char *p
= dst_kaddr
+ dst_off
+ len
;
4782 char *s
= src_kaddr
+ src_off
+ len
;
4789 static inline bool areas_overlap(unsigned long src
, unsigned long dst
, unsigned long len
)
4791 unsigned long distance
= (src
> dst
) ? src
- dst
: dst
- src
;
4792 return distance
< len
;
4795 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
4796 unsigned long dst_off
, unsigned long src_off
,
4799 char *dst_kaddr
= page_address(dst_page
);
4801 int must_memmove
= 0;
4803 if (dst_page
!= src_page
) {
4804 src_kaddr
= page_address(src_page
);
4806 src_kaddr
= dst_kaddr
;
4807 if (areas_overlap(src_off
, dst_off
, len
))
4812 memmove(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4814 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4817 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4818 unsigned long src_offset
, unsigned long len
)
4821 size_t dst_off_in_page
;
4822 size_t src_off_in_page
;
4823 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4824 unsigned long dst_i
;
4825 unsigned long src_i
;
4827 if (src_offset
+ len
> dst
->len
) {
4828 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4829 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
4832 if (dst_offset
+ len
> dst
->len
) {
4833 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4834 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
4839 dst_off_in_page
= (start_offset
+ dst_offset
) &
4840 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4841 src_off_in_page
= (start_offset
+ src_offset
) &
4842 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4844 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4845 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
4847 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
4849 cur
= min_t(unsigned long, cur
,
4850 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
4852 copy_pages(extent_buffer_page(dst
, dst_i
),
4853 extent_buffer_page(dst
, src_i
),
4854 dst_off_in_page
, src_off_in_page
, cur
);
4862 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4863 unsigned long src_offset
, unsigned long len
)
4866 size_t dst_off_in_page
;
4867 size_t src_off_in_page
;
4868 unsigned long dst_end
= dst_offset
+ len
- 1;
4869 unsigned long src_end
= src_offset
+ len
- 1;
4870 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4871 unsigned long dst_i
;
4872 unsigned long src_i
;
4874 if (src_offset
+ len
> dst
->len
) {
4875 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4876 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
4879 if (dst_offset
+ len
> dst
->len
) {
4880 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4881 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
4884 if (dst_offset
< src_offset
) {
4885 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
4889 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
4890 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
4892 dst_off_in_page
= (start_offset
+ dst_end
) &
4893 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4894 src_off_in_page
= (start_offset
+ src_end
) &
4895 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4897 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
4898 cur
= min(cur
, dst_off_in_page
+ 1);
4899 move_pages(extent_buffer_page(dst
, dst_i
),
4900 extent_buffer_page(dst
, src_i
),
4901 dst_off_in_page
- cur
+ 1,
4902 src_off_in_page
- cur
+ 1, cur
);
4910 int try_release_extent_buffer(struct page
*page
, gfp_t mask
)
4912 struct extent_buffer
*eb
;
4915 * We need to make sure noboody is attaching this page to an eb right
4918 spin_lock(&page
->mapping
->private_lock
);
4919 if (!PagePrivate(page
)) {
4920 spin_unlock(&page
->mapping
->private_lock
);
4924 eb
= (struct extent_buffer
*)page
->private;
4928 * This is a little awful but should be ok, we need to make sure that
4929 * the eb doesn't disappear out from under us while we're looking at
4932 spin_lock(&eb
->refs_lock
);
4933 if (atomic_read(&eb
->refs
) != 1 || extent_buffer_under_io(eb
)) {
4934 spin_unlock(&eb
->refs_lock
);
4935 spin_unlock(&page
->mapping
->private_lock
);
4938 spin_unlock(&page
->mapping
->private_lock
);
4940 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
4944 * If tree ref isn't set then we know the ref on this eb is a real ref,
4945 * so just return, this page will likely be freed soon anyway.
4947 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF
, &eb
->bflags
)) {
4948 spin_unlock(&eb
->refs_lock
);
4951 release_extent_buffer(eb
, mask
);