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"
22 static struct kmem_cache
*extent_state_cache
;
23 static struct kmem_cache
*extent_buffer_cache
;
25 static LIST_HEAD(buffers
);
26 static LIST_HEAD(states
);
30 static DEFINE_SPINLOCK(leak_lock
);
33 #define BUFFER_LRU_MAX 64
38 struct rb_node rb_node
;
41 struct extent_page_data
{
43 struct extent_io_tree
*tree
;
44 get_extent_t
*get_extent
;
46 /* tells writepage not to lock the state bits for this range
47 * it still does the unlocking
49 unsigned int extent_locked
:1;
51 /* tells the submit_bio code to use a WRITE_SYNC */
52 unsigned int sync_io
:1;
55 int __init
extent_io_init(void)
57 extent_state_cache
= kmem_cache_create("extent_state",
58 sizeof(struct extent_state
), 0,
59 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
60 if (!extent_state_cache
)
63 extent_buffer_cache
= kmem_cache_create("extent_buffers",
64 sizeof(struct extent_buffer
), 0,
65 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
66 if (!extent_buffer_cache
)
67 goto free_state_cache
;
71 kmem_cache_destroy(extent_state_cache
);
75 void extent_io_exit(void)
77 struct extent_state
*state
;
78 struct extent_buffer
*eb
;
80 while (!list_empty(&states
)) {
81 state
= list_entry(states
.next
, struct extent_state
, leak_list
);
82 printk(KERN_ERR
"btrfs state leak: start %llu end %llu "
83 "state %lu in tree %p refs %d\n",
84 (unsigned long long)state
->start
,
85 (unsigned long long)state
->end
,
86 state
->state
, state
->tree
, atomic_read(&state
->refs
));
87 list_del(&state
->leak_list
);
88 kmem_cache_free(extent_state_cache
, state
);
92 while (!list_empty(&buffers
)) {
93 eb
= list_entry(buffers
.next
, struct extent_buffer
, leak_list
);
94 printk(KERN_ERR
"btrfs buffer leak start %llu len %lu "
95 "refs %d\n", (unsigned long long)eb
->start
,
96 eb
->len
, atomic_read(&eb
->refs
));
97 list_del(&eb
->leak_list
);
98 kmem_cache_free(extent_buffer_cache
, eb
);
100 if (extent_state_cache
)
101 kmem_cache_destroy(extent_state_cache
);
102 if (extent_buffer_cache
)
103 kmem_cache_destroy(extent_buffer_cache
);
106 void extent_io_tree_init(struct extent_io_tree
*tree
,
107 struct address_space
*mapping
)
109 tree
->state
= RB_ROOT
;
110 INIT_RADIX_TREE(&tree
->buffer
, GFP_ATOMIC
);
112 tree
->dirty_bytes
= 0;
113 spin_lock_init(&tree
->lock
);
114 spin_lock_init(&tree
->buffer_lock
);
115 tree
->mapping
= mapping
;
118 static struct extent_state
*alloc_extent_state(gfp_t mask
)
120 struct extent_state
*state
;
125 state
= kmem_cache_alloc(extent_state_cache
, mask
);
132 spin_lock_irqsave(&leak_lock
, flags
);
133 list_add(&state
->leak_list
, &states
);
134 spin_unlock_irqrestore(&leak_lock
, flags
);
136 atomic_set(&state
->refs
, 1);
137 init_waitqueue_head(&state
->wq
);
141 void free_extent_state(struct extent_state
*state
)
145 if (atomic_dec_and_test(&state
->refs
)) {
149 WARN_ON(state
->tree
);
151 spin_lock_irqsave(&leak_lock
, flags
);
152 list_del(&state
->leak_list
);
153 spin_unlock_irqrestore(&leak_lock
, flags
);
155 kmem_cache_free(extent_state_cache
, state
);
159 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
160 struct rb_node
*node
)
162 struct rb_node
**p
= &root
->rb_node
;
163 struct rb_node
*parent
= NULL
;
164 struct tree_entry
*entry
;
168 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
170 if (offset
< entry
->start
)
172 else if (offset
> entry
->end
)
178 entry
= rb_entry(node
, struct tree_entry
, rb_node
);
179 rb_link_node(node
, parent
, p
);
180 rb_insert_color(node
, root
);
184 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
185 struct rb_node
**prev_ret
,
186 struct rb_node
**next_ret
)
188 struct rb_root
*root
= &tree
->state
;
189 struct rb_node
*n
= root
->rb_node
;
190 struct rb_node
*prev
= NULL
;
191 struct rb_node
*orig_prev
= NULL
;
192 struct tree_entry
*entry
;
193 struct tree_entry
*prev_entry
= NULL
;
196 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
200 if (offset
< entry
->start
)
202 else if (offset
> entry
->end
)
210 while (prev
&& offset
> prev_entry
->end
) {
211 prev
= rb_next(prev
);
212 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
219 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
220 while (prev
&& offset
< prev_entry
->start
) {
221 prev
= rb_prev(prev
);
222 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
229 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
232 struct rb_node
*prev
= NULL
;
235 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
241 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
242 struct extent_state
*other
)
244 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
245 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
250 * utility function to look for merge candidates inside a given range.
251 * Any extents with matching state are merged together into a single
252 * extent in the tree. Extents with EXTENT_IO in their state field
253 * are not merged because the end_io handlers need to be able to do
254 * operations on them without sleeping (or doing allocations/splits).
256 * This should be called with the tree lock held.
258 static void merge_state(struct extent_io_tree
*tree
,
259 struct extent_state
*state
)
261 struct extent_state
*other
;
262 struct rb_node
*other_node
;
264 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
267 other_node
= rb_prev(&state
->rb_node
);
269 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
270 if (other
->end
== state
->start
- 1 &&
271 other
->state
== state
->state
) {
272 merge_cb(tree
, state
, other
);
273 state
->start
= other
->start
;
275 rb_erase(&other
->rb_node
, &tree
->state
);
276 free_extent_state(other
);
279 other_node
= rb_next(&state
->rb_node
);
281 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
282 if (other
->start
== state
->end
+ 1 &&
283 other
->state
== state
->state
) {
284 merge_cb(tree
, state
, other
);
285 state
->end
= other
->end
;
287 rb_erase(&other
->rb_node
, &tree
->state
);
288 free_extent_state(other
);
293 static void set_state_cb(struct extent_io_tree
*tree
,
294 struct extent_state
*state
, int *bits
)
296 if (tree
->ops
&& tree
->ops
->set_bit_hook
)
297 tree
->ops
->set_bit_hook(tree
->mapping
->host
, state
, bits
);
300 static void clear_state_cb(struct extent_io_tree
*tree
,
301 struct extent_state
*state
, int *bits
)
303 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
304 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
, bits
);
307 static void set_state_bits(struct extent_io_tree
*tree
,
308 struct extent_state
*state
, int *bits
);
311 * insert an extent_state struct into the tree. 'bits' are set on the
312 * struct before it is inserted.
314 * This may return -EEXIST if the extent is already there, in which case the
315 * state struct is freed.
317 * The tree lock is not taken internally. This is a utility function and
318 * probably isn't what you want to call (see set/clear_extent_bit).
320 static int insert_state(struct extent_io_tree
*tree
,
321 struct extent_state
*state
, u64 start
, u64 end
,
324 struct rb_node
*node
;
327 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
328 (unsigned long long)end
,
329 (unsigned long long)start
);
332 state
->start
= start
;
335 set_state_bits(tree
, state
, bits
);
337 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
339 struct extent_state
*found
;
340 found
= rb_entry(node
, struct extent_state
, rb_node
);
341 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
342 "%llu %llu\n", (unsigned long long)found
->start
,
343 (unsigned long long)found
->end
,
344 (unsigned long long)start
, (unsigned long long)end
);
348 merge_state(tree
, state
);
352 static void split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
355 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
356 tree
->ops
->split_extent_hook(tree
->mapping
->host
, orig
, split
);
360 * split a given extent state struct in two, inserting the preallocated
361 * struct 'prealloc' as the newly created second half. 'split' indicates an
362 * offset inside 'orig' where it should be split.
365 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
366 * are two extent state structs in the tree:
367 * prealloc: [orig->start, split - 1]
368 * orig: [ split, orig->end ]
370 * The tree locks are not taken by this function. They need to be held
373 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
374 struct extent_state
*prealloc
, u64 split
)
376 struct rb_node
*node
;
378 split_cb(tree
, orig
, split
);
380 prealloc
->start
= orig
->start
;
381 prealloc
->end
= split
- 1;
382 prealloc
->state
= orig
->state
;
385 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
387 free_extent_state(prealloc
);
390 prealloc
->tree
= tree
;
395 * utility function to clear some bits in an extent state struct.
396 * it will optionally wake up any one waiting on this state (wake == 1), or
397 * forcibly remove the state from the tree (delete == 1).
399 * If no bits are set on the state struct after clearing things, the
400 * struct is freed and removed from the tree
402 static int clear_state_bit(struct extent_io_tree
*tree
,
403 struct extent_state
*state
,
406 int bits_to_clear
= *bits
& ~EXTENT_CTLBITS
;
407 int ret
= state
->state
& bits_to_clear
;
409 if ((bits_to_clear
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
410 u64 range
= state
->end
- state
->start
+ 1;
411 WARN_ON(range
> tree
->dirty_bytes
);
412 tree
->dirty_bytes
-= range
;
414 clear_state_cb(tree
, state
, bits
);
415 state
->state
&= ~bits_to_clear
;
418 if (state
->state
== 0) {
420 rb_erase(&state
->rb_node
, &tree
->state
);
422 free_extent_state(state
);
427 merge_state(tree
, state
);
432 static struct extent_state
*
433 alloc_extent_state_atomic(struct extent_state
*prealloc
)
436 prealloc
= alloc_extent_state(GFP_ATOMIC
);
442 * clear some bits on a range in the tree. This may require splitting
443 * or inserting elements in the tree, so the gfp mask is used to
444 * indicate which allocations or sleeping are allowed.
446 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
447 * the given range from the tree regardless of state (ie for truncate).
449 * the range [start, end] is inclusive.
451 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
452 * bits were already set, or zero if none of the bits were already set.
454 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
455 int bits
, int wake
, int delete,
456 struct extent_state
**cached_state
,
459 struct extent_state
*state
;
460 struct extent_state
*cached
;
461 struct extent_state
*prealloc
= NULL
;
462 struct rb_node
*next_node
;
463 struct rb_node
*node
;
470 bits
|= ~EXTENT_CTLBITS
;
471 bits
|= EXTENT_FIRST_DELALLOC
;
473 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
476 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
477 prealloc
= alloc_extent_state(mask
);
482 spin_lock(&tree
->lock
);
484 cached
= *cached_state
;
487 *cached_state
= NULL
;
491 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
492 cached
->end
> start
) {
494 atomic_dec(&cached
->refs
);
499 free_extent_state(cached
);
502 * this search will find the extents that end after
505 node
= tree_search(tree
, start
);
508 state
= rb_entry(node
, struct extent_state
, rb_node
);
510 if (state
->start
> end
)
512 WARN_ON(state
->end
< start
);
513 last_end
= state
->end
;
516 * | ---- desired range ---- |
518 * | ------------- state -------------- |
520 * We need to split the extent we found, and may flip
521 * bits on second half.
523 * If the extent we found extends past our range, we
524 * just split and search again. It'll get split again
525 * the next time though.
527 * If the extent we found is inside our range, we clear
528 * the desired bit on it.
531 if (state
->start
< start
) {
532 prealloc
= alloc_extent_state_atomic(prealloc
);
534 err
= split_state(tree
, state
, prealloc
, start
);
535 BUG_ON(err
== -EEXIST
);
539 if (state
->end
<= end
) {
540 set
|= clear_state_bit(tree
, state
, &bits
, wake
);
541 if (last_end
== (u64
)-1)
543 start
= last_end
+ 1;
548 * | ---- desired range ---- |
550 * We need to split the extent, and clear the bit
553 if (state
->start
<= end
&& state
->end
> end
) {
554 prealloc
= alloc_extent_state_atomic(prealloc
);
556 err
= split_state(tree
, state
, prealloc
, end
+ 1);
557 BUG_ON(err
== -EEXIST
);
561 set
|= clear_state_bit(tree
, prealloc
, &bits
, wake
);
567 if (state
->end
< end
&& prealloc
&& !need_resched())
568 next_node
= rb_next(&state
->rb_node
);
572 set
|= clear_state_bit(tree
, state
, &bits
, wake
);
573 if (last_end
== (u64
)-1)
575 start
= last_end
+ 1;
576 if (start
<= end
&& next_node
) {
577 state
= rb_entry(next_node
, struct extent_state
,
579 if (state
->start
== start
)
585 spin_unlock(&tree
->lock
);
587 free_extent_state(prealloc
);
594 spin_unlock(&tree
->lock
);
595 if (mask
& __GFP_WAIT
)
600 static int wait_on_state(struct extent_io_tree
*tree
,
601 struct extent_state
*state
)
602 __releases(tree
->lock
)
603 __acquires(tree
->lock
)
606 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
607 spin_unlock(&tree
->lock
);
609 spin_lock(&tree
->lock
);
610 finish_wait(&state
->wq
, &wait
);
615 * waits for one or more bits to clear on a range in the state tree.
616 * The range [start, end] is inclusive.
617 * The tree lock is taken by this function
619 int wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
621 struct extent_state
*state
;
622 struct rb_node
*node
;
624 spin_lock(&tree
->lock
);
628 * this search will find all the extents that end after
631 node
= tree_search(tree
, start
);
635 state
= rb_entry(node
, struct extent_state
, rb_node
);
637 if (state
->start
> end
)
640 if (state
->state
& bits
) {
641 start
= state
->start
;
642 atomic_inc(&state
->refs
);
643 wait_on_state(tree
, state
);
644 free_extent_state(state
);
647 start
= state
->end
+ 1;
652 cond_resched_lock(&tree
->lock
);
655 spin_unlock(&tree
->lock
);
659 static void set_state_bits(struct extent_io_tree
*tree
,
660 struct extent_state
*state
,
663 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
665 set_state_cb(tree
, state
, bits
);
666 if ((bits_to_set
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
667 u64 range
= state
->end
- state
->start
+ 1;
668 tree
->dirty_bytes
+= range
;
670 state
->state
|= bits_to_set
;
673 static void cache_state(struct extent_state
*state
,
674 struct extent_state
**cached_ptr
)
676 if (cached_ptr
&& !(*cached_ptr
)) {
677 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
679 atomic_inc(&state
->refs
);
684 static void uncache_state(struct extent_state
**cached_ptr
)
686 if (cached_ptr
&& (*cached_ptr
)) {
687 struct extent_state
*state
= *cached_ptr
;
689 free_extent_state(state
);
694 * set some bits on a range in the tree. This may require allocations or
695 * sleeping, so the gfp mask is used to indicate what is allowed.
697 * If any of the exclusive bits are set, this will fail with -EEXIST if some
698 * part of the range already has the desired bits set. The start of the
699 * existing range is returned in failed_start in this case.
701 * [start, end] is inclusive This takes the tree lock.
704 int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
705 int bits
, int exclusive_bits
, u64
*failed_start
,
706 struct extent_state
**cached_state
, gfp_t mask
)
708 struct extent_state
*state
;
709 struct extent_state
*prealloc
= NULL
;
710 struct rb_node
*node
;
715 bits
|= EXTENT_FIRST_DELALLOC
;
717 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
718 prealloc
= alloc_extent_state(mask
);
722 spin_lock(&tree
->lock
);
723 if (cached_state
&& *cached_state
) {
724 state
= *cached_state
;
725 if (state
->start
<= start
&& state
->end
> start
&&
727 node
= &state
->rb_node
;
732 * this search will find all the extents that end after
735 node
= tree_search(tree
, start
);
737 prealloc
= alloc_extent_state_atomic(prealloc
);
739 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
741 BUG_ON(err
== -EEXIST
);
744 state
= rb_entry(node
, struct extent_state
, rb_node
);
746 last_start
= state
->start
;
747 last_end
= state
->end
;
750 * | ---- desired range ---- |
753 * Just lock what we found and keep going
755 if (state
->start
== start
&& state
->end
<= end
) {
756 struct rb_node
*next_node
;
757 if (state
->state
& exclusive_bits
) {
758 *failed_start
= state
->start
;
763 set_state_bits(tree
, state
, &bits
);
765 cache_state(state
, cached_state
);
766 merge_state(tree
, state
);
767 if (last_end
== (u64
)-1)
770 start
= last_end
+ 1;
771 next_node
= rb_next(&state
->rb_node
);
772 if (next_node
&& start
< end
&& prealloc
&& !need_resched()) {
773 state
= rb_entry(next_node
, struct extent_state
,
775 if (state
->start
== start
)
782 * | ---- desired range ---- |
785 * | ------------- state -------------- |
787 * We need to split the extent we found, and may flip bits on
790 * If the extent we found extends past our
791 * range, we just split and search again. It'll get split
792 * again the next time though.
794 * If the extent we found is inside our range, we set the
797 if (state
->start
< start
) {
798 if (state
->state
& exclusive_bits
) {
799 *failed_start
= start
;
804 prealloc
= alloc_extent_state_atomic(prealloc
);
806 err
= split_state(tree
, state
, prealloc
, start
);
807 BUG_ON(err
== -EEXIST
);
811 if (state
->end
<= end
) {
812 set_state_bits(tree
, state
, &bits
);
813 cache_state(state
, cached_state
);
814 merge_state(tree
, state
);
815 if (last_end
== (u64
)-1)
817 start
= last_end
+ 1;
822 * | ---- desired range ---- |
823 * | state | or | state |
825 * There's a hole, we need to insert something in it and
826 * ignore the extent we found.
828 if (state
->start
> start
) {
830 if (end
< last_start
)
833 this_end
= last_start
- 1;
835 prealloc
= alloc_extent_state_atomic(prealloc
);
839 * Avoid to free 'prealloc' if it can be merged with
842 err
= insert_state(tree
, prealloc
, start
, this_end
,
844 BUG_ON(err
== -EEXIST
);
846 free_extent_state(prealloc
);
850 cache_state(prealloc
, cached_state
);
852 start
= this_end
+ 1;
856 * | ---- desired range ---- |
858 * We need to split the extent, and set the bit
861 if (state
->start
<= end
&& state
->end
> end
) {
862 if (state
->state
& exclusive_bits
) {
863 *failed_start
= start
;
868 prealloc
= alloc_extent_state_atomic(prealloc
);
870 err
= split_state(tree
, state
, prealloc
, end
+ 1);
871 BUG_ON(err
== -EEXIST
);
873 set_state_bits(tree
, prealloc
, &bits
);
874 cache_state(prealloc
, cached_state
);
875 merge_state(tree
, prealloc
);
883 spin_unlock(&tree
->lock
);
885 free_extent_state(prealloc
);
892 spin_unlock(&tree
->lock
);
893 if (mask
& __GFP_WAIT
)
899 * convert_extent - convert all bits in a given range from one bit to another
900 * @tree: the io tree to search
901 * @start: the start offset in bytes
902 * @end: the end offset in bytes (inclusive)
903 * @bits: the bits to set in this range
904 * @clear_bits: the bits to clear in this range
905 * @mask: the allocation mask
907 * This will go through and set bits for the given range. If any states exist
908 * already in this range they are set with the given bit and cleared of the
909 * clear_bits. This is only meant to be used by things that are mergeable, ie
910 * converting from say DELALLOC to DIRTY. This is not meant to be used with
911 * boundary bits like LOCK.
913 int convert_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
914 int bits
, int clear_bits
, gfp_t mask
)
916 struct extent_state
*state
;
917 struct extent_state
*prealloc
= NULL
;
918 struct rb_node
*node
;
924 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
925 prealloc
= alloc_extent_state(mask
);
930 spin_lock(&tree
->lock
);
932 * this search will find all the extents that end after
935 node
= tree_search(tree
, start
);
937 prealloc
= alloc_extent_state_atomic(prealloc
);
942 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
944 BUG_ON(err
== -EEXIST
);
947 state
= rb_entry(node
, struct extent_state
, rb_node
);
949 last_start
= state
->start
;
950 last_end
= state
->end
;
953 * | ---- desired range ---- |
956 * Just lock what we found and keep going
958 if (state
->start
== start
&& state
->end
<= end
) {
959 struct rb_node
*next_node
;
961 set_state_bits(tree
, state
, &bits
);
962 clear_state_bit(tree
, state
, &clear_bits
, 0);
964 merge_state(tree
, state
);
965 if (last_end
== (u64
)-1)
968 start
= last_end
+ 1;
969 next_node
= rb_next(&state
->rb_node
);
970 if (next_node
&& start
< end
&& prealloc
&& !need_resched()) {
971 state
= rb_entry(next_node
, struct extent_state
,
973 if (state
->start
== start
)
980 * | ---- desired range ---- |
983 * | ------------- state -------------- |
985 * We need to split the extent we found, and may flip bits on
988 * If the extent we found extends past our
989 * range, we just split and search again. It'll get split
990 * again the next time though.
992 * If the extent we found is inside our range, we set the
995 if (state
->start
< start
) {
996 prealloc
= alloc_extent_state_atomic(prealloc
);
1001 err
= split_state(tree
, state
, prealloc
, start
);
1002 BUG_ON(err
== -EEXIST
);
1006 if (state
->end
<= end
) {
1007 set_state_bits(tree
, state
, &bits
);
1008 clear_state_bit(tree
, state
, &clear_bits
, 0);
1009 merge_state(tree
, state
);
1010 if (last_end
== (u64
)-1)
1012 start
= last_end
+ 1;
1017 * | ---- desired range ---- |
1018 * | state | or | state |
1020 * There's a hole, we need to insert something in it and
1021 * ignore the extent we found.
1023 if (state
->start
> start
) {
1025 if (end
< last_start
)
1028 this_end
= last_start
- 1;
1030 prealloc
= alloc_extent_state_atomic(prealloc
);
1037 * Avoid to free 'prealloc' if it can be merged with
1040 err
= insert_state(tree
, prealloc
, start
, this_end
,
1042 BUG_ON(err
== -EEXIST
);
1044 free_extent_state(prealloc
);
1049 start
= this_end
+ 1;
1053 * | ---- desired range ---- |
1055 * We need to split the extent, and set the bit
1058 if (state
->start
<= end
&& state
->end
> end
) {
1059 prealloc
= alloc_extent_state_atomic(prealloc
);
1065 err
= split_state(tree
, state
, prealloc
, end
+ 1);
1066 BUG_ON(err
== -EEXIST
);
1068 set_state_bits(tree
, prealloc
, &bits
);
1069 clear_state_bit(tree
, prealloc
, &clear_bits
, 0);
1071 merge_state(tree
, prealloc
);
1079 spin_unlock(&tree
->lock
);
1081 free_extent_state(prealloc
);
1088 spin_unlock(&tree
->lock
);
1089 if (mask
& __GFP_WAIT
)
1094 /* wrappers around set/clear extent bit */
1095 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1098 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, 0, NULL
,
1102 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1103 int bits
, gfp_t mask
)
1105 return set_extent_bit(tree
, start
, end
, bits
, 0, NULL
,
1109 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1110 int bits
, gfp_t mask
)
1112 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
1115 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1116 struct extent_state
**cached_state
, gfp_t mask
)
1118 return set_extent_bit(tree
, start
, end
,
1119 EXTENT_DELALLOC
| EXTENT_UPTODATE
,
1120 0, NULL
, cached_state
, mask
);
1123 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1126 return clear_extent_bit(tree
, start
, end
,
1127 EXTENT_DIRTY
| EXTENT_DELALLOC
|
1128 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, mask
);
1131 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1134 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, NULL
,
1138 int set_extent_uptodate(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
, EXTENT_UPTODATE
, 0,
1142 NULL
, cached_state
, mask
);
1145 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
1146 u64 end
, struct extent_state
**cached_state
,
1149 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
1150 cached_state
, mask
);
1154 * either insert or lock state struct between start and end use mask to tell
1155 * us if waiting is desired.
1157 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1158 int bits
, struct extent_state
**cached_state
, gfp_t mask
)
1163 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
1164 EXTENT_LOCKED
, &failed_start
,
1165 cached_state
, mask
);
1166 if (err
== -EEXIST
&& (mask
& __GFP_WAIT
)) {
1167 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
1168 start
= failed_start
;
1172 WARN_ON(start
> end
);
1177 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
1179 return lock_extent_bits(tree
, start
, end
, 0, NULL
, mask
);
1182 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1188 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1189 &failed_start
, NULL
, mask
);
1190 if (err
== -EEXIST
) {
1191 if (failed_start
> start
)
1192 clear_extent_bit(tree
, start
, failed_start
- 1,
1193 EXTENT_LOCKED
, 1, 0, NULL
, mask
);
1199 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1200 struct extent_state
**cached
, gfp_t mask
)
1202 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1206 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
1208 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1212 int extent_range_clear_dirty_for_io(struct inode
*inode
, u64 start
, u64 end
)
1214 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1215 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1218 while (index
<= end_index
) {
1219 page
= find_get_page(inode
->i_mapping
, index
);
1220 BUG_ON(!page
); /* Pages should be in the extent_io_tree */
1221 clear_page_dirty_for_io(page
);
1222 page_cache_release(page
);
1228 int extent_range_redirty_for_io(struct inode
*inode
, u64 start
, u64 end
)
1230 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1231 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1234 while (index
<= end_index
) {
1235 page
= find_get_page(inode
->i_mapping
, index
);
1236 BUG_ON(!page
); /* Pages should be in the extent_io_tree */
1237 account_page_redirty(page
);
1238 __set_page_dirty_nobuffers(page
);
1239 page_cache_release(page
);
1246 * helper function to set both pages and extents in the tree writeback
1248 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1250 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1251 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1254 while (index
<= end_index
) {
1255 page
= find_get_page(tree
->mapping
, index
);
1257 set_page_writeback(page
);
1258 page_cache_release(page
);
1264 /* find the first state struct with 'bits' set after 'start', and
1265 * return it. tree->lock must be held. NULL will returned if
1266 * nothing was found after 'start'
1268 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1269 u64 start
, int bits
)
1271 struct rb_node
*node
;
1272 struct extent_state
*state
;
1275 * this search will find all the extents that end after
1278 node
= tree_search(tree
, start
);
1283 state
= rb_entry(node
, struct extent_state
, rb_node
);
1284 if (state
->end
>= start
&& (state
->state
& bits
))
1287 node
= rb_next(node
);
1296 * find the first offset in the io tree with 'bits' set. zero is
1297 * returned if we find something, and *start_ret and *end_ret are
1298 * set to reflect the state struct that was found.
1300 * If nothing was found, 1 is returned, < 0 on error
1302 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1303 u64
*start_ret
, u64
*end_ret
, int bits
)
1305 struct extent_state
*state
;
1308 spin_lock(&tree
->lock
);
1309 state
= find_first_extent_bit_state(tree
, start
, bits
);
1311 *start_ret
= state
->start
;
1312 *end_ret
= state
->end
;
1315 spin_unlock(&tree
->lock
);
1320 * find a contiguous range of bytes in the file marked as delalloc, not
1321 * more than 'max_bytes'. start and end are used to return the range,
1323 * 1 is returned if we find something, 0 if nothing was in the tree
1325 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1326 u64
*start
, u64
*end
, u64 max_bytes
,
1327 struct extent_state
**cached_state
)
1329 struct rb_node
*node
;
1330 struct extent_state
*state
;
1331 u64 cur_start
= *start
;
1333 u64 total_bytes
= 0;
1335 spin_lock(&tree
->lock
);
1338 * this search will find all the extents that end after
1341 node
= tree_search(tree
, cur_start
);
1349 state
= rb_entry(node
, struct extent_state
, rb_node
);
1350 if (found
&& (state
->start
!= cur_start
||
1351 (state
->state
& EXTENT_BOUNDARY
))) {
1354 if (!(state
->state
& EXTENT_DELALLOC
)) {
1360 *start
= state
->start
;
1361 *cached_state
= state
;
1362 atomic_inc(&state
->refs
);
1366 cur_start
= state
->end
+ 1;
1367 node
= rb_next(node
);
1370 total_bytes
+= state
->end
- state
->start
+ 1;
1371 if (total_bytes
>= max_bytes
)
1375 spin_unlock(&tree
->lock
);
1379 static noinline
int __unlock_for_delalloc(struct inode
*inode
,
1380 struct page
*locked_page
,
1384 struct page
*pages
[16];
1385 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1386 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1387 unsigned long nr_pages
= end_index
- index
+ 1;
1390 if (index
== locked_page
->index
&& end_index
== index
)
1393 while (nr_pages
> 0) {
1394 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1395 min_t(unsigned long, nr_pages
,
1396 ARRAY_SIZE(pages
)), pages
);
1397 for (i
= 0; i
< ret
; i
++) {
1398 if (pages
[i
] != locked_page
)
1399 unlock_page(pages
[i
]);
1400 page_cache_release(pages
[i
]);
1409 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1410 struct page
*locked_page
,
1414 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1415 unsigned long start_index
= index
;
1416 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1417 unsigned long pages_locked
= 0;
1418 struct page
*pages
[16];
1419 unsigned long nrpages
;
1423 /* the caller is responsible for locking the start index */
1424 if (index
== locked_page
->index
&& index
== end_index
)
1427 /* skip the page at the start index */
1428 nrpages
= end_index
- index
+ 1;
1429 while (nrpages
> 0) {
1430 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1431 min_t(unsigned long,
1432 nrpages
, ARRAY_SIZE(pages
)), pages
);
1437 /* now we have an array of pages, lock them all */
1438 for (i
= 0; i
< ret
; i
++) {
1440 * the caller is taking responsibility for
1443 if (pages
[i
] != locked_page
) {
1444 lock_page(pages
[i
]);
1445 if (!PageDirty(pages
[i
]) ||
1446 pages
[i
]->mapping
!= inode
->i_mapping
) {
1448 unlock_page(pages
[i
]);
1449 page_cache_release(pages
[i
]);
1453 page_cache_release(pages
[i
]);
1462 if (ret
&& pages_locked
) {
1463 __unlock_for_delalloc(inode
, locked_page
,
1465 ((u64
)(start_index
+ pages_locked
- 1)) <<
1472 * find a contiguous range of bytes in the file marked as delalloc, not
1473 * more than 'max_bytes'. start and end are used to return the range,
1475 * 1 is returned if we find something, 0 if nothing was in the tree
1477 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1478 struct extent_io_tree
*tree
,
1479 struct page
*locked_page
,
1480 u64
*start
, u64
*end
,
1486 struct extent_state
*cached_state
= NULL
;
1491 /* step one, find a bunch of delalloc bytes starting at start */
1492 delalloc_start
= *start
;
1494 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1495 max_bytes
, &cached_state
);
1496 if (!found
|| delalloc_end
<= *start
) {
1497 *start
= delalloc_start
;
1498 *end
= delalloc_end
;
1499 free_extent_state(cached_state
);
1504 * start comes from the offset of locked_page. We have to lock
1505 * pages in order, so we can't process delalloc bytes before
1508 if (delalloc_start
< *start
)
1509 delalloc_start
= *start
;
1512 * make sure to limit the number of pages we try to lock down
1515 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1516 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1518 /* step two, lock all the pages after the page that has start */
1519 ret
= lock_delalloc_pages(inode
, locked_page
,
1520 delalloc_start
, delalloc_end
);
1521 if (ret
== -EAGAIN
) {
1522 /* some of the pages are gone, lets avoid looping by
1523 * shortening the size of the delalloc range we're searching
1525 free_extent_state(cached_state
);
1526 cached_state
= NULL
;
1528 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1529 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1539 /* step three, lock the state bits for the whole range */
1540 lock_extent_bits(tree
, delalloc_start
, delalloc_end
,
1541 0, &cached_state
, GFP_NOFS
);
1543 /* then test to make sure it is all still delalloc */
1544 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1545 EXTENT_DELALLOC
, 1, cached_state
);
1547 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1548 &cached_state
, GFP_NOFS
);
1549 __unlock_for_delalloc(inode
, locked_page
,
1550 delalloc_start
, delalloc_end
);
1554 free_extent_state(cached_state
);
1555 *start
= delalloc_start
;
1556 *end
= delalloc_end
;
1561 int extent_clear_unlock_delalloc(struct inode
*inode
,
1562 struct extent_io_tree
*tree
,
1563 u64 start
, u64 end
, struct page
*locked_page
,
1567 struct page
*pages
[16];
1568 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1569 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1570 unsigned long nr_pages
= end_index
- index
+ 1;
1574 if (op
& EXTENT_CLEAR_UNLOCK
)
1575 clear_bits
|= EXTENT_LOCKED
;
1576 if (op
& EXTENT_CLEAR_DIRTY
)
1577 clear_bits
|= EXTENT_DIRTY
;
1579 if (op
& EXTENT_CLEAR_DELALLOC
)
1580 clear_bits
|= EXTENT_DELALLOC
;
1582 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1583 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1584 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1585 EXTENT_SET_PRIVATE2
)))
1588 while (nr_pages
> 0) {
1589 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1590 min_t(unsigned long,
1591 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1592 for (i
= 0; i
< ret
; i
++) {
1594 if (op
& EXTENT_SET_PRIVATE2
)
1595 SetPagePrivate2(pages
[i
]);
1597 if (pages
[i
] == locked_page
) {
1598 page_cache_release(pages
[i
]);
1601 if (op
& EXTENT_CLEAR_DIRTY
)
1602 clear_page_dirty_for_io(pages
[i
]);
1603 if (op
& EXTENT_SET_WRITEBACK
)
1604 set_page_writeback(pages
[i
]);
1605 if (op
& EXTENT_END_WRITEBACK
)
1606 end_page_writeback(pages
[i
]);
1607 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1608 unlock_page(pages
[i
]);
1609 page_cache_release(pages
[i
]);
1619 * count the number of bytes in the tree that have a given bit(s)
1620 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1621 * cached. The total number found is returned.
1623 u64
count_range_bits(struct extent_io_tree
*tree
,
1624 u64
*start
, u64 search_end
, u64 max_bytes
,
1625 unsigned long bits
, int contig
)
1627 struct rb_node
*node
;
1628 struct extent_state
*state
;
1629 u64 cur_start
= *start
;
1630 u64 total_bytes
= 0;
1634 if (search_end
<= cur_start
) {
1639 spin_lock(&tree
->lock
);
1640 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1641 total_bytes
= tree
->dirty_bytes
;
1645 * this search will find all the extents that end after
1648 node
= tree_search(tree
, cur_start
);
1653 state
= rb_entry(node
, struct extent_state
, rb_node
);
1654 if (state
->start
> search_end
)
1656 if (contig
&& found
&& state
->start
> last
+ 1)
1658 if (state
->end
>= cur_start
&& (state
->state
& bits
) == bits
) {
1659 total_bytes
+= min(search_end
, state
->end
) + 1 -
1660 max(cur_start
, state
->start
);
1661 if (total_bytes
>= max_bytes
)
1664 *start
= max(cur_start
, state
->start
);
1668 } else if (contig
&& found
) {
1671 node
= rb_next(node
);
1676 spin_unlock(&tree
->lock
);
1681 * set the private field for a given byte offset in the tree. If there isn't
1682 * an extent_state there already, this does nothing.
1684 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1686 struct rb_node
*node
;
1687 struct extent_state
*state
;
1690 spin_lock(&tree
->lock
);
1692 * this search will find all the extents that end after
1695 node
= tree_search(tree
, start
);
1700 state
= rb_entry(node
, struct extent_state
, rb_node
);
1701 if (state
->start
!= start
) {
1705 state
->private = private;
1707 spin_unlock(&tree
->lock
);
1711 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1713 struct rb_node
*node
;
1714 struct extent_state
*state
;
1717 spin_lock(&tree
->lock
);
1719 * this search will find all the extents that end after
1722 node
= tree_search(tree
, start
);
1727 state
= rb_entry(node
, struct extent_state
, rb_node
);
1728 if (state
->start
!= start
) {
1732 *private = state
->private;
1734 spin_unlock(&tree
->lock
);
1739 * searches a range in the state tree for a given mask.
1740 * If 'filled' == 1, this returns 1 only if every extent in the tree
1741 * has the bits set. Otherwise, 1 is returned if any bit in the
1742 * range is found set.
1744 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1745 int bits
, int filled
, struct extent_state
*cached
)
1747 struct extent_state
*state
= NULL
;
1748 struct rb_node
*node
;
1751 spin_lock(&tree
->lock
);
1752 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
1753 cached
->end
> start
)
1754 node
= &cached
->rb_node
;
1756 node
= tree_search(tree
, start
);
1757 while (node
&& start
<= end
) {
1758 state
= rb_entry(node
, struct extent_state
, rb_node
);
1760 if (filled
&& state
->start
> start
) {
1765 if (state
->start
> end
)
1768 if (state
->state
& bits
) {
1772 } else if (filled
) {
1777 if (state
->end
== (u64
)-1)
1780 start
= state
->end
+ 1;
1783 node
= rb_next(node
);
1790 spin_unlock(&tree
->lock
);
1795 * helper function to set a given page up to date if all the
1796 * extents in the tree for that page are up to date
1798 static int check_page_uptodate(struct extent_io_tree
*tree
,
1801 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1802 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1803 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1804 SetPageUptodate(page
);
1809 * helper function to unlock a page if all the extents in the tree
1810 * for that page are unlocked
1812 static int check_page_locked(struct extent_io_tree
*tree
,
1815 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1816 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1817 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1823 * helper function to end page writeback if all the extents
1824 * in the tree for that page are done with writeback
1826 static int check_page_writeback(struct extent_io_tree
*tree
,
1829 end_page_writeback(page
);
1834 * When IO fails, either with EIO or csum verification fails, we
1835 * try other mirrors that might have a good copy of the data. This
1836 * io_failure_record is used to record state as we go through all the
1837 * mirrors. If another mirror has good data, the page is set up to date
1838 * and things continue. If a good mirror can't be found, the original
1839 * bio end_io callback is called to indicate things have failed.
1841 struct io_failure_record
{
1846 unsigned long bio_flags
;
1852 static int free_io_failure(struct inode
*inode
, struct io_failure_record
*rec
,
1857 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1859 set_state_private(failure_tree
, rec
->start
, 0);
1860 ret
= clear_extent_bits(failure_tree
, rec
->start
,
1861 rec
->start
+ rec
->len
- 1,
1862 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1867 ret
= clear_extent_bits(&BTRFS_I(inode
)->io_tree
, rec
->start
,
1868 rec
->start
+ rec
->len
- 1,
1869 EXTENT_DAMAGED
, GFP_NOFS
);
1878 static void repair_io_failure_callback(struct bio
*bio
, int err
)
1880 complete(bio
->bi_private
);
1884 * this bypasses the standard btrfs submit functions deliberately, as
1885 * the standard behavior is to write all copies in a raid setup. here we only
1886 * want to write the one bad copy. so we do the mapping for ourselves and issue
1887 * submit_bio directly.
1888 * to avoid any synchonization issues, wait for the data after writing, which
1889 * actually prevents the read that triggered the error from finishing.
1890 * currently, there can be no more than two copies of every data bit. thus,
1891 * exactly one rewrite is required.
1893 int repair_io_failure(struct btrfs_mapping_tree
*map_tree
, u64 start
,
1894 u64 length
, u64 logical
, struct page
*page
,
1898 struct btrfs_device
*dev
;
1899 DECLARE_COMPLETION_ONSTACK(compl);
1902 struct btrfs_bio
*bbio
= NULL
;
1905 BUG_ON(!mirror_num
);
1907 bio
= bio_alloc(GFP_NOFS
, 1);
1910 bio
->bi_private
= &compl;
1911 bio
->bi_end_io
= repair_io_failure_callback
;
1913 map_length
= length
;
1915 ret
= btrfs_map_block(map_tree
, WRITE
, logical
,
1916 &map_length
, &bbio
, mirror_num
);
1921 BUG_ON(mirror_num
!= bbio
->mirror_num
);
1922 sector
= bbio
->stripes
[mirror_num
-1].physical
>> 9;
1923 bio
->bi_sector
= sector
;
1924 dev
= bbio
->stripes
[mirror_num
-1].dev
;
1926 if (!dev
|| !dev
->bdev
|| !dev
->writeable
) {
1930 bio
->bi_bdev
= dev
->bdev
;
1931 bio_add_page(bio
, page
, length
, start
-page_offset(page
));
1932 submit_bio(WRITE_SYNC
, bio
);
1933 wait_for_completion(&compl);
1935 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
1936 /* try to remap that extent elsewhere? */
1941 printk(KERN_INFO
"btrfs read error corrected: ino %lu off %llu (dev %s "
1942 "sector %llu)\n", page
->mapping
->host
->i_ino
, start
,
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);
2178 bio
->bi_private
= state
;
2179 bio
->bi_end_io
= failed_bio
->bi_end_io
;
2180 bio
->bi_sector
= failrec
->logical
>> 9;
2181 bio
->bi_bdev
= BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
;
2184 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
2186 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2187 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode
,
2188 failrec
->this_mirror
, num_copies
, failrec
->in_validation
);
2190 tree
->ops
->submit_bio_hook(inode
, read_mode
, bio
, failrec
->this_mirror
,
2191 failrec
->bio_flags
, 0);
2195 /* lots and lots of room for performance fixes in the end_bio funcs */
2198 * after a writepage IO is done, we need to:
2199 * clear the uptodate bits on error
2200 * clear the writeback bits in the extent tree for this IO
2201 * end_page_writeback if the page has no more pending IO
2203 * Scheduling is not allowed, so the extent state tree is expected
2204 * to have one and only one object corresponding to this IO.
2206 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
2208 int uptodate
= err
== 0;
2209 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2210 struct extent_io_tree
*tree
;
2217 struct page
*page
= bvec
->bv_page
;
2218 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2220 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
2222 end
= start
+ bvec
->bv_len
- 1;
2224 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2229 if (--bvec
>= bio
->bi_io_vec
)
2230 prefetchw(&bvec
->bv_page
->flags
);
2231 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
2232 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
2233 end
, NULL
, uptodate
);
2238 if (!uptodate
&& tree
->ops
&&
2239 tree
->ops
->writepage_io_failed_hook
) {
2240 ret
= tree
->ops
->writepage_io_failed_hook(bio
, page
,
2243 uptodate
= (err
== 0);
2249 clear_extent_uptodate(tree
, start
, end
, NULL
, GFP_NOFS
);
2250 ClearPageUptodate(page
);
2255 end_page_writeback(page
);
2257 check_page_writeback(tree
, page
);
2258 } while (bvec
>= bio
->bi_io_vec
);
2264 * after a readpage IO is done, we need to:
2265 * clear the uptodate bits on error
2266 * set the uptodate bits if things worked
2267 * set the page up to date if all extents in the tree are uptodate
2268 * clear the lock bit in the extent tree
2269 * unlock the page if there are no other extents locked for it
2271 * Scheduling is not allowed, so the extent state tree is expected
2272 * to have one and only one object corresponding to this IO.
2274 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
2276 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2277 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2278 struct bio_vec
*bvec
= bio
->bi_io_vec
;
2279 struct extent_io_tree
*tree
;
2289 struct page
*page
= bvec
->bv_page
;
2290 struct extent_state
*cached
= NULL
;
2291 struct extent_state
*state
;
2293 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2294 "mirror=%ld\n", bio
->bi_vcnt
, bio
->bi_idx
, err
,
2295 (long int)bio
->bi_bdev
);
2296 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2298 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
2300 end
= start
+ bvec
->bv_len
- 1;
2302 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
2307 if (++bvec
<= bvec_end
)
2308 prefetchw(&bvec
->bv_page
->flags
);
2310 spin_lock(&tree
->lock
);
2311 state
= find_first_extent_bit_state(tree
, start
, EXTENT_LOCKED
);
2312 if (state
&& state
->start
== start
) {
2314 * take a reference on the state, unlock will drop
2317 cache_state(state
, &cached
);
2319 spin_unlock(&tree
->lock
);
2321 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
2322 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
2327 clean_io_failure(start
, page
);
2331 failed_mirror
= (int)(unsigned long)bio
->bi_bdev
;
2333 * The generic bio_readpage_error handles errors the
2334 * following way: If possible, new read requests are
2335 * created and submitted and will end up in
2336 * end_bio_extent_readpage as well (if we're lucky, not
2337 * in the !uptodate case). In that case it returns 0 and
2338 * we just go on with the next page in our bio. If it
2339 * can't handle the error it will return -EIO and we
2340 * remain responsible for that page.
2342 ret
= bio_readpage_error(bio
, page
, start
, end
,
2343 failed_mirror
, NULL
);
2347 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2350 uncache_state(&cached
);
2353 if (tree
->ops
&& tree
->ops
->readpage_io_failed_hook
) {
2354 ret
= tree
->ops
->readpage_io_failed_hook(
2355 bio
, page
, start
, end
,
2356 failed_mirror
, state
);
2363 set_extent_uptodate(tree
, start
, end
, &cached
,
2366 unlock_extent_cached(tree
, start
, end
, &cached
, GFP_ATOMIC
);
2370 SetPageUptodate(page
);
2372 ClearPageUptodate(page
);
2378 check_page_uptodate(tree
, page
);
2380 ClearPageUptodate(page
);
2383 check_page_locked(tree
, page
);
2385 } while (bvec
<= bvec_end
);
2391 btrfs_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
2396 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2398 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
2399 while (!bio
&& (nr_vecs
/= 2))
2400 bio
= bio_alloc(gfp_flags
, nr_vecs
);
2405 bio
->bi_bdev
= bdev
;
2406 bio
->bi_sector
= first_sector
;
2411 static int submit_one_bio(int rw
, struct bio
*bio
, int mirror_num
,
2412 unsigned long bio_flags
)
2415 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
2416 struct page
*page
= bvec
->bv_page
;
2417 struct extent_io_tree
*tree
= bio
->bi_private
;
2420 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
2422 bio
->bi_private
= NULL
;
2426 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
2427 ret
= tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
2428 mirror_num
, bio_flags
, start
);
2430 submit_bio(rw
, bio
);
2432 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
2438 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
2439 struct page
*page
, sector_t sector
,
2440 size_t size
, unsigned long offset
,
2441 struct block_device
*bdev
,
2442 struct bio
**bio_ret
,
2443 unsigned long max_pages
,
2444 bio_end_io_t end_io_func
,
2446 unsigned long prev_bio_flags
,
2447 unsigned long bio_flags
)
2453 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
2454 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
2455 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
2457 if (bio_ret
&& *bio_ret
) {
2460 contig
= bio
->bi_sector
== sector
;
2462 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
2465 if (prev_bio_flags
!= bio_flags
|| !contig
||
2466 (tree
->ops
&& tree
->ops
->merge_bio_hook
&&
2467 tree
->ops
->merge_bio_hook(page
, offset
, page_size
, bio
,
2469 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
2470 ret
= submit_one_bio(rw
, bio
, mirror_num
,
2477 if (this_compressed
)
2480 nr
= bio_get_nr_vecs(bdev
);
2482 bio
= btrfs_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
2486 bio_add_page(bio
, page
, page_size
, offset
);
2487 bio
->bi_end_io
= end_io_func
;
2488 bio
->bi_private
= tree
;
2493 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
2498 void set_page_extent_mapped(struct page
*page
)
2500 if (!PagePrivate(page
)) {
2501 SetPagePrivate(page
);
2502 page_cache_get(page
);
2503 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
2507 static void set_page_extent_head(struct page
*page
, unsigned long len
)
2509 WARN_ON(!PagePrivate(page
));
2510 set_page_private(page
, EXTENT_PAGE_PRIVATE_FIRST_PAGE
| len
<< 2);
2514 * basic readpage implementation. Locked extent state structs are inserted
2515 * into the tree that are removed when the IO is done (by the end_io
2518 static int __extent_read_full_page(struct extent_io_tree
*tree
,
2520 get_extent_t
*get_extent
,
2521 struct bio
**bio
, int mirror_num
,
2522 unsigned long *bio_flags
)
2524 struct inode
*inode
= page
->mapping
->host
;
2525 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2526 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2530 u64 last_byte
= i_size_read(inode
);
2534 struct extent_map
*em
;
2535 struct block_device
*bdev
;
2536 struct btrfs_ordered_extent
*ordered
;
2539 size_t pg_offset
= 0;
2541 size_t disk_io_size
;
2542 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2543 unsigned long this_bio_flag
= 0;
2545 set_page_extent_mapped(page
);
2547 if (!PageUptodate(page
)) {
2548 if (cleancache_get_page(page
) == 0) {
2549 BUG_ON(blocksize
!= PAGE_SIZE
);
2556 lock_extent(tree
, start
, end
, GFP_NOFS
);
2557 ordered
= btrfs_lookup_ordered_extent(inode
, start
);
2560 unlock_extent(tree
, start
, end
, GFP_NOFS
);
2561 btrfs_start_ordered_extent(inode
, ordered
, 1);
2562 btrfs_put_ordered_extent(ordered
);
2565 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
2567 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
2570 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
2571 userpage
= kmap_atomic(page
, KM_USER0
);
2572 memset(userpage
+ zero_offset
, 0, iosize
);
2573 flush_dcache_page(page
);
2574 kunmap_atomic(userpage
, KM_USER0
);
2577 while (cur
<= end
) {
2578 if (cur
>= last_byte
) {
2580 struct extent_state
*cached
= NULL
;
2582 iosize
= PAGE_CACHE_SIZE
- pg_offset
;
2583 userpage
= kmap_atomic(page
, KM_USER0
);
2584 memset(userpage
+ pg_offset
, 0, iosize
);
2585 flush_dcache_page(page
);
2586 kunmap_atomic(userpage
, KM_USER0
);
2587 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2589 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2593 em
= get_extent(inode
, page
, pg_offset
, cur
,
2595 if (IS_ERR_OR_NULL(em
)) {
2597 unlock_extent(tree
, cur
, end
, GFP_NOFS
);
2600 extent_offset
= cur
- em
->start
;
2601 BUG_ON(extent_map_end(em
) <= cur
);
2604 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2605 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2606 extent_set_compress_type(&this_bio_flag
,
2610 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2611 cur_end
= min(extent_map_end(em
) - 1, end
);
2612 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2613 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2614 disk_io_size
= em
->block_len
;
2615 sector
= em
->block_start
>> 9;
2617 sector
= (em
->block_start
+ extent_offset
) >> 9;
2618 disk_io_size
= iosize
;
2621 block_start
= em
->block_start
;
2622 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2623 block_start
= EXTENT_MAP_HOLE
;
2624 free_extent_map(em
);
2627 /* we've found a hole, just zero and go on */
2628 if (block_start
== EXTENT_MAP_HOLE
) {
2630 struct extent_state
*cached
= NULL
;
2632 userpage
= kmap_atomic(page
, KM_USER0
);
2633 memset(userpage
+ pg_offset
, 0, iosize
);
2634 flush_dcache_page(page
);
2635 kunmap_atomic(userpage
, KM_USER0
);
2637 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2639 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2642 pg_offset
+= iosize
;
2645 /* the get_extent function already copied into the page */
2646 if (test_range_bit(tree
, cur
, cur_end
,
2647 EXTENT_UPTODATE
, 1, NULL
)) {
2648 check_page_uptodate(tree
, page
);
2649 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2651 pg_offset
+= iosize
;
2654 /* we have an inline extent but it didn't get marked up
2655 * to date. Error out
2657 if (block_start
== EXTENT_MAP_INLINE
) {
2659 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2661 pg_offset
+= iosize
;
2666 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2667 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2671 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2673 ret
= submit_extent_page(READ
, tree
, page
,
2674 sector
, disk_io_size
, pg_offset
,
2676 end_bio_extent_readpage
, mirror_num
,
2680 *bio_flags
= this_bio_flag
;
2685 pg_offset
+= iosize
;
2689 if (!PageError(page
))
2690 SetPageUptodate(page
);
2696 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2697 get_extent_t
*get_extent
, int mirror_num
)
2699 struct bio
*bio
= NULL
;
2700 unsigned long bio_flags
= 0;
2703 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, mirror_num
,
2706 ret
= submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
2710 static noinline
void update_nr_written(struct page
*page
,
2711 struct writeback_control
*wbc
,
2712 unsigned long nr_written
)
2714 wbc
->nr_to_write
-= nr_written
;
2715 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2716 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2717 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2721 * the writepage semantics are similar to regular writepage. extent
2722 * records are inserted to lock ranges in the tree, and as dirty areas
2723 * are found, they are marked writeback. Then the lock bits are removed
2724 * and the end_io handler clears the writeback ranges
2726 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2729 struct inode
*inode
= page
->mapping
->host
;
2730 struct extent_page_data
*epd
= data
;
2731 struct extent_io_tree
*tree
= epd
->tree
;
2732 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2734 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2738 u64 last_byte
= i_size_read(inode
);
2742 struct extent_state
*cached_state
= NULL
;
2743 struct extent_map
*em
;
2744 struct block_device
*bdev
;
2747 size_t pg_offset
= 0;
2749 loff_t i_size
= i_size_read(inode
);
2750 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2756 unsigned long nr_written
= 0;
2757 bool fill_delalloc
= true;
2759 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2760 write_flags
= WRITE_SYNC
;
2762 write_flags
= WRITE
;
2764 trace___extent_writepage(page
, inode
, wbc
);
2766 WARN_ON(!PageLocked(page
));
2768 ClearPageError(page
);
2770 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2771 if (page
->index
> end_index
||
2772 (page
->index
== end_index
&& !pg_offset
)) {
2773 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2778 if (page
->index
== end_index
) {
2781 userpage
= kmap_atomic(page
, KM_USER0
);
2782 memset(userpage
+ pg_offset
, 0,
2783 PAGE_CACHE_SIZE
- pg_offset
);
2784 kunmap_atomic(userpage
, KM_USER0
);
2785 flush_dcache_page(page
);
2789 set_page_extent_mapped(page
);
2791 if (!tree
->ops
|| !tree
->ops
->fill_delalloc
)
2792 fill_delalloc
= false;
2794 delalloc_start
= start
;
2797 if (!epd
->extent_locked
&& fill_delalloc
) {
2798 u64 delalloc_to_write
= 0;
2800 * make sure the wbc mapping index is at least updated
2803 update_nr_written(page
, wbc
, 0);
2805 while (delalloc_end
< page_end
) {
2806 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2811 if (nr_delalloc
== 0) {
2812 delalloc_start
= delalloc_end
+ 1;
2815 tree
->ops
->fill_delalloc(inode
, page
, delalloc_start
,
2816 delalloc_end
, &page_started
,
2819 * delalloc_end is already one less than the total
2820 * length, so we don't subtract one from
2823 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2826 delalloc_start
= delalloc_end
+ 1;
2828 if (wbc
->nr_to_write
< delalloc_to_write
) {
2831 if (delalloc_to_write
< thresh
* 2)
2832 thresh
= delalloc_to_write
;
2833 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2837 /* did the fill delalloc function already unlock and start
2843 * we've unlocked the page, so we can't update
2844 * the mapping's writeback index, just update
2847 wbc
->nr_to_write
-= nr_written
;
2851 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2852 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2854 if (ret
== -EAGAIN
) {
2855 redirty_page_for_writepage(wbc
, page
);
2856 update_nr_written(page
, wbc
, nr_written
);
2864 * we don't want to touch the inode after unlocking the page,
2865 * so we update the mapping writeback index now
2867 update_nr_written(page
, wbc
, nr_written
+ 1);
2870 if (last_byte
<= start
) {
2871 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2872 tree
->ops
->writepage_end_io_hook(page
, start
,
2877 blocksize
= inode
->i_sb
->s_blocksize
;
2879 while (cur
<= end
) {
2880 if (cur
>= last_byte
) {
2881 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2882 tree
->ops
->writepage_end_io_hook(page
, cur
,
2886 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2888 if (IS_ERR_OR_NULL(em
)) {
2893 extent_offset
= cur
- em
->start
;
2894 BUG_ON(extent_map_end(em
) <= cur
);
2896 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2897 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2898 sector
= (em
->block_start
+ extent_offset
) >> 9;
2900 block_start
= em
->block_start
;
2901 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2902 free_extent_map(em
);
2906 * compressed and inline extents are written through other
2909 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2910 block_start
== EXTENT_MAP_INLINE
) {
2912 * end_io notification does not happen here for
2913 * compressed extents
2915 if (!compressed
&& tree
->ops
&&
2916 tree
->ops
->writepage_end_io_hook
)
2917 tree
->ops
->writepage_end_io_hook(page
, cur
,
2920 else if (compressed
) {
2921 /* we don't want to end_page_writeback on
2922 * a compressed extent. this happens
2929 pg_offset
+= iosize
;
2932 /* leave this out until we have a page_mkwrite call */
2933 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2934 EXTENT_DIRTY
, 0, NULL
)) {
2936 pg_offset
+= iosize
;
2940 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2941 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
2949 unsigned long max_nr
= end_index
+ 1;
2951 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
2952 if (!PageWriteback(page
)) {
2953 printk(KERN_ERR
"btrfs warning page %lu not "
2954 "writeback, cur %llu end %llu\n",
2955 page
->index
, (unsigned long long)cur
,
2956 (unsigned long long)end
);
2959 ret
= submit_extent_page(write_flags
, tree
, page
,
2960 sector
, iosize
, pg_offset
,
2961 bdev
, &epd
->bio
, max_nr
,
2962 end_bio_extent_writepage
,
2968 pg_offset
+= iosize
;
2973 /* make sure the mapping tag for page dirty gets cleared */
2974 set_page_writeback(page
);
2975 end_page_writeback(page
);
2981 /* drop our reference on any cached states */
2982 free_extent_state(cached_state
);
2987 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2988 * @mapping: address space structure to write
2989 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2990 * @writepage: function called for each page
2991 * @data: data passed to writepage function
2993 * If a page is already under I/O, write_cache_pages() skips it, even
2994 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2995 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2996 * and msync() need to guarantee that all the data which was dirty at the time
2997 * the call was made get new I/O started against them. If wbc->sync_mode is
2998 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2999 * existing IO to complete.
3001 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
3002 struct address_space
*mapping
,
3003 struct writeback_control
*wbc
,
3004 writepage_t writepage
, void *data
,
3005 void (*flush_fn
)(void *))
3009 int nr_to_write_done
= 0;
3010 struct pagevec pvec
;
3013 pgoff_t end
; /* Inclusive */
3017 pagevec_init(&pvec
, 0);
3018 if (wbc
->range_cyclic
) {
3019 index
= mapping
->writeback_index
; /* Start from prev offset */
3022 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
3023 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
3026 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3027 tag
= PAGECACHE_TAG_TOWRITE
;
3029 tag
= PAGECACHE_TAG_DIRTY
;
3031 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3032 tag_pages_for_writeback(mapping
, index
, end
);
3033 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
3034 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
3035 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
3039 for (i
= 0; i
< nr_pages
; i
++) {
3040 struct page
*page
= pvec
.pages
[i
];
3043 * At this point we hold neither mapping->tree_lock nor
3044 * lock on the page itself: the page may be truncated or
3045 * invalidated (changing page->mapping to NULL), or even
3046 * swizzled back from swapper_space to tmpfs file
3050 tree
->ops
->write_cache_pages_lock_hook
) {
3051 tree
->ops
->write_cache_pages_lock_hook(page
,
3054 if (!trylock_page(page
)) {
3060 if (unlikely(page
->mapping
!= mapping
)) {
3065 if (!wbc
->range_cyclic
&& page
->index
> end
) {
3071 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3072 if (PageWriteback(page
))
3074 wait_on_page_writeback(page
);
3077 if (PageWriteback(page
) ||
3078 !clear_page_dirty_for_io(page
)) {
3083 ret
= (*writepage
)(page
, wbc
, data
);
3085 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
3093 * the filesystem may choose to bump up nr_to_write.
3094 * We have to make sure to honor the new nr_to_write
3097 nr_to_write_done
= wbc
->nr_to_write
<= 0;
3099 pagevec_release(&pvec
);
3102 if (!scanned
&& !done
) {
3104 * We hit the last page and there is more work to be done: wrap
3105 * back to the start of the file
3114 static void flush_epd_write_bio(struct extent_page_data
*epd
)
3118 submit_one_bio(WRITE_SYNC
, epd
->bio
, 0, 0);
3120 submit_one_bio(WRITE
, epd
->bio
, 0, 0);
3125 static noinline
void flush_write_bio(void *data
)
3127 struct extent_page_data
*epd
= data
;
3128 flush_epd_write_bio(epd
);
3131 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
3132 get_extent_t
*get_extent
,
3133 struct writeback_control
*wbc
)
3136 struct extent_page_data epd
= {
3139 .get_extent
= get_extent
,
3141 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3144 ret
= __extent_writepage(page
, wbc
, &epd
);
3146 flush_epd_write_bio(&epd
);
3150 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
3151 u64 start
, u64 end
, get_extent_t
*get_extent
,
3155 struct address_space
*mapping
= inode
->i_mapping
;
3157 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
3160 struct extent_page_data epd
= {
3163 .get_extent
= get_extent
,
3165 .sync_io
= mode
== WB_SYNC_ALL
,
3167 struct writeback_control wbc_writepages
= {
3169 .nr_to_write
= nr_pages
* 2,
3170 .range_start
= start
,
3171 .range_end
= end
+ 1,
3174 while (start
<= end
) {
3175 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
3176 if (clear_page_dirty_for_io(page
))
3177 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
3179 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
3180 tree
->ops
->writepage_end_io_hook(page
, start
,
3181 start
+ PAGE_CACHE_SIZE
- 1,
3185 page_cache_release(page
);
3186 start
+= PAGE_CACHE_SIZE
;
3189 flush_epd_write_bio(&epd
);
3193 int extent_writepages(struct extent_io_tree
*tree
,
3194 struct address_space
*mapping
,
3195 get_extent_t
*get_extent
,
3196 struct writeback_control
*wbc
)
3199 struct extent_page_data epd
= {
3202 .get_extent
= get_extent
,
3204 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
3207 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
3208 __extent_writepage
, &epd
,
3210 flush_epd_write_bio(&epd
);
3214 int extent_readpages(struct extent_io_tree
*tree
,
3215 struct address_space
*mapping
,
3216 struct list_head
*pages
, unsigned nr_pages
,
3217 get_extent_t get_extent
)
3219 struct bio
*bio
= NULL
;
3221 unsigned long bio_flags
= 0;
3223 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
3224 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
3226 prefetchw(&page
->flags
);
3227 list_del(&page
->lru
);
3228 if (!add_to_page_cache_lru(page
, mapping
,
3229 page
->index
, GFP_NOFS
)) {
3230 __extent_read_full_page(tree
, page
, get_extent
,
3231 &bio
, 0, &bio_flags
);
3233 page_cache_release(page
);
3235 BUG_ON(!list_empty(pages
));
3237 submit_one_bio(READ
, bio
, 0, bio_flags
);
3242 * basic invalidatepage code, this waits on any locked or writeback
3243 * ranges corresponding to the page, and then deletes any extent state
3244 * records from the tree
3246 int extent_invalidatepage(struct extent_io_tree
*tree
,
3247 struct page
*page
, unsigned long offset
)
3249 struct extent_state
*cached_state
= NULL
;
3250 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
3251 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3252 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
3254 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
3258 lock_extent_bits(tree
, start
, end
, 0, &cached_state
, GFP_NOFS
);
3259 wait_on_page_writeback(page
);
3260 clear_extent_bit(tree
, start
, end
,
3261 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
3262 EXTENT_DO_ACCOUNTING
,
3263 1, 1, &cached_state
, GFP_NOFS
);
3268 * a helper for releasepage, this tests for areas of the page that
3269 * are locked or under IO and drops the related state bits if it is safe
3272 int try_release_extent_state(struct extent_map_tree
*map
,
3273 struct extent_io_tree
*tree
, struct page
*page
,
3276 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3277 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3280 if (test_range_bit(tree
, start
, end
,
3281 EXTENT_IOBITS
, 0, NULL
))
3284 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
3287 * at this point we can safely clear everything except the
3288 * locked bit and the nodatasum bit
3290 ret
= clear_extent_bit(tree
, start
, end
,
3291 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
3294 /* if clear_extent_bit failed for enomem reasons,
3295 * we can't allow the release to continue.
3306 * a helper for releasepage. As long as there are no locked extents
3307 * in the range corresponding to the page, both state records and extent
3308 * map records are removed
3310 int try_release_extent_mapping(struct extent_map_tree
*map
,
3311 struct extent_io_tree
*tree
, struct page
*page
,
3314 struct extent_map
*em
;
3315 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
3316 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
3318 if ((mask
& __GFP_WAIT
) &&
3319 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
3321 while (start
<= end
) {
3322 len
= end
- start
+ 1;
3323 write_lock(&map
->lock
);
3324 em
= lookup_extent_mapping(map
, start
, len
);
3325 if (IS_ERR_OR_NULL(em
)) {
3326 write_unlock(&map
->lock
);
3329 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
3330 em
->start
!= start
) {
3331 write_unlock(&map
->lock
);
3332 free_extent_map(em
);
3335 if (!test_range_bit(tree
, em
->start
,
3336 extent_map_end(em
) - 1,
3337 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
3339 remove_extent_mapping(map
, em
);
3340 /* once for the rb tree */
3341 free_extent_map(em
);
3343 start
= extent_map_end(em
);
3344 write_unlock(&map
->lock
);
3347 free_extent_map(em
);
3350 return try_release_extent_state(map
, tree
, page
, mask
);
3354 * helper function for fiemap, which doesn't want to see any holes.
3355 * This maps until we find something past 'last'
3357 static struct extent_map
*get_extent_skip_holes(struct inode
*inode
,
3360 get_extent_t
*get_extent
)
3362 u64 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
3363 struct extent_map
*em
;
3370 len
= last
- offset
;
3373 len
= (len
+ sectorsize
- 1) & ~(sectorsize
- 1);
3374 em
= get_extent(inode
, NULL
, 0, offset
, len
, 0);
3375 if (IS_ERR_OR_NULL(em
))
3378 /* if this isn't a hole return it */
3379 if (!test_bit(EXTENT_FLAG_VACANCY
, &em
->flags
) &&
3380 em
->block_start
!= EXTENT_MAP_HOLE
) {
3384 /* this is a hole, advance to the next extent */
3385 offset
= extent_map_end(em
);
3386 free_extent_map(em
);
3393 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3394 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
3398 u64 max
= start
+ len
;
3402 u64 last_for_get_extent
= 0;
3404 u64 isize
= i_size_read(inode
);
3405 struct btrfs_key found_key
;
3406 struct extent_map
*em
= NULL
;
3407 struct extent_state
*cached_state
= NULL
;
3408 struct btrfs_path
*path
;
3409 struct btrfs_file_extent_item
*item
;
3414 unsigned long emflags
;
3419 path
= btrfs_alloc_path();
3422 path
->leave_spinning
= 1;
3424 start
= ALIGN(start
, BTRFS_I(inode
)->root
->sectorsize
);
3425 len
= ALIGN(len
, BTRFS_I(inode
)->root
->sectorsize
);
3428 * lookup the last file extent. We're not using i_size here
3429 * because there might be preallocation past i_size
3431 ret
= btrfs_lookup_file_extent(NULL
, BTRFS_I(inode
)->root
,
3432 path
, btrfs_ino(inode
), -1, 0);
3434 btrfs_free_path(path
);
3439 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3440 struct btrfs_file_extent_item
);
3441 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, path
->slots
[0]);
3442 found_type
= btrfs_key_type(&found_key
);
3444 /* No extents, but there might be delalloc bits */
3445 if (found_key
.objectid
!= btrfs_ino(inode
) ||
3446 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
3447 /* have to trust i_size as the end */
3449 last_for_get_extent
= isize
;
3452 * remember the start of the last extent. There are a
3453 * bunch of different factors that go into the length of the
3454 * extent, so its much less complex to remember where it started
3456 last
= found_key
.offset
;
3457 last_for_get_extent
= last
+ 1;
3459 btrfs_free_path(path
);
3462 * we might have some extents allocated but more delalloc past those
3463 * extents. so, we trust isize unless the start of the last extent is
3468 last_for_get_extent
= isize
;
3471 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
3472 &cached_state
, GFP_NOFS
);
3474 em
= get_extent_skip_holes(inode
, start
, last_for_get_extent
,
3484 u64 offset_in_extent
;
3486 /* break if the extent we found is outside the range */
3487 if (em
->start
>= max
|| extent_map_end(em
) < off
)
3491 * get_extent may return an extent that starts before our
3492 * requested range. We have to make sure the ranges
3493 * we return to fiemap always move forward and don't
3494 * overlap, so adjust the offsets here
3496 em_start
= max(em
->start
, off
);
3499 * record the offset from the start of the extent
3500 * for adjusting the disk offset below
3502 offset_in_extent
= em_start
- em
->start
;
3503 em_end
= extent_map_end(em
);
3504 em_len
= em_end
- em_start
;
3505 emflags
= em
->flags
;
3510 * bump off for our next call to get_extent
3512 off
= extent_map_end(em
);
3516 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
3518 flags
|= FIEMAP_EXTENT_LAST
;
3519 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
3520 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
3521 FIEMAP_EXTENT_NOT_ALIGNED
);
3522 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
3523 flags
|= (FIEMAP_EXTENT_DELALLOC
|
3524 FIEMAP_EXTENT_UNKNOWN
);
3526 disko
= em
->block_start
+ offset_in_extent
;
3528 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
3529 flags
|= FIEMAP_EXTENT_ENCODED
;
3531 free_extent_map(em
);
3533 if ((em_start
>= last
) || em_len
== (u64
)-1 ||
3534 (last
== (u64
)-1 && isize
<= em_end
)) {
3535 flags
|= FIEMAP_EXTENT_LAST
;
3539 /* now scan forward to see if this is really the last extent. */
3540 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
,
3547 flags
|= FIEMAP_EXTENT_LAST
;
3550 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3556 free_extent_map(em
);
3558 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3559 &cached_state
, GFP_NOFS
);
3563 inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
3567 struct address_space
*mapping
;
3570 return eb
->first_page
;
3571 i
+= eb
->start
>> PAGE_CACHE_SHIFT
;
3572 mapping
= eb
->first_page
->mapping
;
3577 * extent_buffer_page is only called after pinning the page
3578 * by increasing the reference count. So we know the page must
3579 * be in the radix tree.
3582 p
= radix_tree_lookup(&mapping
->page_tree
, i
);
3588 inline unsigned long num_extent_pages(u64 start
, u64 len
)
3590 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
3591 (start
>> PAGE_CACHE_SHIFT
);
3594 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3599 struct extent_buffer
*eb
= NULL
;
3601 unsigned long flags
;
3604 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
3609 rwlock_init(&eb
->lock
);
3610 atomic_set(&eb
->write_locks
, 0);
3611 atomic_set(&eb
->read_locks
, 0);
3612 atomic_set(&eb
->blocking_readers
, 0);
3613 atomic_set(&eb
->blocking_writers
, 0);
3614 atomic_set(&eb
->spinning_readers
, 0);
3615 atomic_set(&eb
->spinning_writers
, 0);
3616 init_waitqueue_head(&eb
->write_lock_wq
);
3617 init_waitqueue_head(&eb
->read_lock_wq
);
3620 spin_lock_irqsave(&leak_lock
, flags
);
3621 list_add(&eb
->leak_list
, &buffers
);
3622 spin_unlock_irqrestore(&leak_lock
, flags
);
3624 atomic_set(&eb
->refs
, 1);
3629 static void __free_extent_buffer(struct extent_buffer
*eb
)
3632 unsigned long flags
;
3633 spin_lock_irqsave(&leak_lock
, flags
);
3634 list_del(&eb
->leak_list
);
3635 spin_unlock_irqrestore(&leak_lock
, flags
);
3637 kmem_cache_free(extent_buffer_cache
, eb
);
3641 * Helper for releasing extent buffer page.
3643 static void btrfs_release_extent_buffer_page(struct extent_buffer
*eb
,
3644 unsigned long start_idx
)
3646 unsigned long index
;
3649 if (!eb
->first_page
)
3652 index
= num_extent_pages(eb
->start
, eb
->len
);
3653 if (start_idx
>= index
)
3658 page
= extent_buffer_page(eb
, index
);
3660 page_cache_release(page
);
3661 } while (index
!= start_idx
);
3665 * Helper for releasing the extent buffer.
3667 static inline void btrfs_release_extent_buffer(struct extent_buffer
*eb
)
3669 btrfs_release_extent_buffer_page(eb
, 0);
3670 __free_extent_buffer(eb
);
3673 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
3674 u64 start
, unsigned long len
,
3677 unsigned long num_pages
= num_extent_pages(start
, len
);
3679 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
3680 struct extent_buffer
*eb
;
3681 struct extent_buffer
*exists
= NULL
;
3683 struct address_space
*mapping
= tree
->mapping
;
3688 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3689 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
3691 mark_page_accessed(eb
->first_page
);
3696 eb
= __alloc_extent_buffer(tree
, start
, len
, GFP_NOFS
);
3701 eb
->first_page
= page0
;
3704 page_cache_get(page0
);
3705 mark_page_accessed(page0
);
3706 set_page_extent_mapped(page0
);
3707 set_page_extent_head(page0
, len
);
3708 uptodate
= PageUptodate(page0
);
3712 for (; i
< num_pages
; i
++, index
++) {
3713 p
= find_or_create_page(mapping
, index
, GFP_NOFS
);
3718 set_page_extent_mapped(p
);
3719 mark_page_accessed(p
);
3722 set_page_extent_head(p
, len
);
3724 set_page_private(p
, EXTENT_PAGE_PRIVATE
);
3726 if (!PageUptodate(p
))
3730 * see below about how we avoid a nasty race with release page
3731 * and why we unlock later
3737 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3739 ret
= radix_tree_preload(GFP_NOFS
& ~__GFP_HIGHMEM
);
3743 spin_lock(&tree
->buffer_lock
);
3744 ret
= radix_tree_insert(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
, eb
);
3745 if (ret
== -EEXIST
) {
3746 exists
= radix_tree_lookup(&tree
->buffer
,
3747 start
>> PAGE_CACHE_SHIFT
);
3748 /* add one reference for the caller */
3749 atomic_inc(&exists
->refs
);
3750 spin_unlock(&tree
->buffer_lock
);
3751 radix_tree_preload_end();
3754 /* add one reference for the tree */
3755 atomic_inc(&eb
->refs
);
3756 spin_unlock(&tree
->buffer_lock
);
3757 radix_tree_preload_end();
3760 * there is a race where release page may have
3761 * tried to find this extent buffer in the radix
3762 * but failed. It will tell the VM it is safe to
3763 * reclaim the, and it will clear the page private bit.
3764 * We must make sure to set the page private bit properly
3765 * after the extent buffer is in the radix tree so
3766 * it doesn't get lost
3768 set_page_extent_mapped(eb
->first_page
);
3769 set_page_extent_head(eb
->first_page
, eb
->len
);
3771 unlock_page(eb
->first_page
);
3775 if (eb
->first_page
&& !page0
)
3776 unlock_page(eb
->first_page
);
3778 if (!atomic_dec_and_test(&eb
->refs
))
3780 btrfs_release_extent_buffer(eb
);
3784 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
3785 u64 start
, unsigned long len
)
3787 struct extent_buffer
*eb
;
3790 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3791 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
3793 mark_page_accessed(eb
->first_page
);
3801 void free_extent_buffer(struct extent_buffer
*eb
)
3806 if (!atomic_dec_and_test(&eb
->refs
))
3812 int clear_extent_buffer_dirty(struct extent_io_tree
*tree
,
3813 struct extent_buffer
*eb
)
3816 unsigned long num_pages
;
3819 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3821 for (i
= 0; i
< num_pages
; i
++) {
3822 page
= extent_buffer_page(eb
, i
);
3823 if (!PageDirty(page
))
3827 WARN_ON(!PagePrivate(page
));
3829 set_page_extent_mapped(page
);
3831 set_page_extent_head(page
, eb
->len
);
3833 clear_page_dirty_for_io(page
);
3834 spin_lock_irq(&page
->mapping
->tree_lock
);
3835 if (!PageDirty(page
)) {
3836 radix_tree_tag_clear(&page
->mapping
->page_tree
,
3838 PAGECACHE_TAG_DIRTY
);
3840 spin_unlock_irq(&page
->mapping
->tree_lock
);
3841 ClearPageError(page
);
3847 int set_extent_buffer_dirty(struct extent_io_tree
*tree
,
3848 struct extent_buffer
*eb
)
3851 unsigned long num_pages
;
3854 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
3855 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3856 for (i
= 0; i
< num_pages
; i
++)
3857 __set_page_dirty_nobuffers(extent_buffer_page(eb
, i
));
3861 static int __eb_straddles_pages(u64 start
, u64 len
)
3863 if (len
< PAGE_CACHE_SIZE
)
3865 if (start
& (PAGE_CACHE_SIZE
- 1))
3867 if ((start
+ len
) & (PAGE_CACHE_SIZE
- 1))
3872 static int eb_straddles_pages(struct extent_buffer
*eb
)
3874 return __eb_straddles_pages(eb
->start
, eb
->len
);
3877 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3878 struct extent_buffer
*eb
,
3879 struct extent_state
**cached_state
)
3883 unsigned long num_pages
;
3885 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3886 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3888 if (eb_straddles_pages(eb
)) {
3889 clear_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3890 cached_state
, GFP_NOFS
);
3892 for (i
= 0; i
< num_pages
; i
++) {
3893 page
= extent_buffer_page(eb
, i
);
3895 ClearPageUptodate(page
);
3900 int set_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3901 struct extent_buffer
*eb
)
3905 unsigned long num_pages
;
3907 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3909 if (eb_straddles_pages(eb
)) {
3910 set_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3913 for (i
= 0; i
< num_pages
; i
++) {
3914 page
= extent_buffer_page(eb
, i
);
3915 if ((i
== 0 && (eb
->start
& (PAGE_CACHE_SIZE
- 1))) ||
3916 ((i
== num_pages
- 1) &&
3917 ((eb
->start
+ eb
->len
) & (PAGE_CACHE_SIZE
- 1)))) {
3918 check_page_uptodate(tree
, page
);
3921 SetPageUptodate(page
);
3926 int extent_range_uptodate(struct extent_io_tree
*tree
,
3931 int pg_uptodate
= 1;
3933 unsigned long index
;
3935 if (__eb_straddles_pages(start
, end
- start
+ 1)) {
3936 ret
= test_range_bit(tree
, start
, end
,
3937 EXTENT_UPTODATE
, 1, NULL
);
3941 while (start
<= end
) {
3942 index
= start
>> PAGE_CACHE_SHIFT
;
3943 page
= find_get_page(tree
->mapping
, index
);
3944 uptodate
= PageUptodate(page
);
3945 page_cache_release(page
);
3950 start
+= PAGE_CACHE_SIZE
;
3955 int extent_buffer_uptodate(struct extent_io_tree
*tree
,
3956 struct extent_buffer
*eb
,
3957 struct extent_state
*cached_state
)
3960 unsigned long num_pages
;
3963 int pg_uptodate
= 1;
3965 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3968 if (eb_straddles_pages(eb
)) {
3969 ret
= test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3970 EXTENT_UPTODATE
, 1, cached_state
);
3975 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3976 for (i
= 0; i
< num_pages
; i
++) {
3977 page
= extent_buffer_page(eb
, i
);
3978 if (!PageUptodate(page
)) {
3986 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
3987 struct extent_buffer
*eb
, u64 start
, int wait
,
3988 get_extent_t
*get_extent
, int mirror_num
)
3991 unsigned long start_i
;
3995 int locked_pages
= 0;
3996 int all_uptodate
= 1;
3997 int inc_all_pages
= 0;
3998 unsigned long num_pages
;
3999 struct bio
*bio
= NULL
;
4000 unsigned long bio_flags
= 0;
4002 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
4005 if (eb_straddles_pages(eb
)) {
4006 if (test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
4007 EXTENT_UPTODATE
, 1, NULL
)) {
4013 WARN_ON(start
< eb
->start
);
4014 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
4015 (eb
->start
>> PAGE_CACHE_SHIFT
);
4020 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
4021 for (i
= start_i
; i
< num_pages
; i
++) {
4022 page
= extent_buffer_page(eb
, i
);
4023 if (wait
== WAIT_NONE
) {
4024 if (!trylock_page(page
))
4030 if (!PageUptodate(page
))
4035 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4039 for (i
= start_i
; i
< num_pages
; i
++) {
4040 page
= extent_buffer_page(eb
, i
);
4042 WARN_ON(!PagePrivate(page
));
4044 set_page_extent_mapped(page
);
4046 set_page_extent_head(page
, eb
->len
);
4049 page_cache_get(page
);
4050 if (!PageUptodate(page
)) {
4053 ClearPageError(page
);
4054 err
= __extent_read_full_page(tree
, page
,
4056 mirror_num
, &bio_flags
);
4065 submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
4067 if (ret
|| wait
!= WAIT_COMPLETE
)
4070 for (i
= start_i
; i
< num_pages
; i
++) {
4071 page
= extent_buffer_page(eb
, i
);
4072 wait_on_page_locked(page
);
4073 if (!PageUptodate(page
))
4078 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
4083 while (locked_pages
> 0) {
4084 page
= extent_buffer_page(eb
, i
);
4092 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
4093 unsigned long start
,
4100 char *dst
= (char *)dstv
;
4101 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4102 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4104 WARN_ON(start
> eb
->len
);
4105 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4107 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4110 page
= extent_buffer_page(eb
, i
);
4112 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4113 kaddr
= page_address(page
);
4114 memcpy(dst
, kaddr
+ offset
, cur
);
4123 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
4124 unsigned long min_len
, char **map
,
4125 unsigned long *map_start
,
4126 unsigned long *map_len
)
4128 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
4131 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4132 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4133 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
4140 offset
= start_offset
;
4144 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
4147 if (start
+ min_len
> eb
->len
) {
4148 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
4149 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
4150 eb
->len
, start
, min_len
);
4155 p
= extent_buffer_page(eb
, i
);
4156 kaddr
= page_address(p
);
4157 *map
= kaddr
+ offset
;
4158 *map_len
= PAGE_CACHE_SIZE
- offset
;
4162 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
4163 unsigned long start
,
4170 char *ptr
= (char *)ptrv
;
4171 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4172 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4175 WARN_ON(start
> eb
->len
);
4176 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4178 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4181 page
= extent_buffer_page(eb
, i
);
4183 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
4185 kaddr
= page_address(page
);
4186 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
4198 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
4199 unsigned long start
, unsigned long len
)
4205 char *src
= (char *)srcv
;
4206 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4207 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4209 WARN_ON(start
> eb
->len
);
4210 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4212 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4215 page
= extent_buffer_page(eb
, i
);
4216 WARN_ON(!PageUptodate(page
));
4218 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4219 kaddr
= page_address(page
);
4220 memcpy(kaddr
+ offset
, src
, cur
);
4229 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
4230 unsigned long start
, unsigned long len
)
4236 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4237 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
4239 WARN_ON(start
> eb
->len
);
4240 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
4242 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
4245 page
= extent_buffer_page(eb
, i
);
4246 WARN_ON(!PageUptodate(page
));
4248 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
4249 kaddr
= page_address(page
);
4250 memset(kaddr
+ offset
, c
, cur
);
4258 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
4259 unsigned long dst_offset
, unsigned long src_offset
,
4262 u64 dst_len
= dst
->len
;
4267 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4268 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4270 WARN_ON(src
->len
!= dst_len
);
4272 offset
= (start_offset
+ dst_offset
) &
4273 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4276 page
= extent_buffer_page(dst
, i
);
4277 WARN_ON(!PageUptodate(page
));
4279 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
4281 kaddr
= page_address(page
);
4282 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
4291 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
4292 unsigned long dst_off
, unsigned long src_off
,
4295 char *dst_kaddr
= page_address(dst_page
);
4296 if (dst_page
== src_page
) {
4297 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
4299 char *src_kaddr
= page_address(src_page
);
4300 char *p
= dst_kaddr
+ dst_off
+ len
;
4301 char *s
= src_kaddr
+ src_off
+ len
;
4308 static inline bool areas_overlap(unsigned long src
, unsigned long dst
, unsigned long len
)
4310 unsigned long distance
= (src
> dst
) ? src
- dst
: dst
- src
;
4311 return distance
< len
;
4314 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
4315 unsigned long dst_off
, unsigned long src_off
,
4318 char *dst_kaddr
= page_address(dst_page
);
4321 if (dst_page
!= src_page
) {
4322 src_kaddr
= page_address(src_page
);
4324 src_kaddr
= dst_kaddr
;
4325 BUG_ON(areas_overlap(src_off
, dst_off
, len
));
4328 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
4331 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4332 unsigned long src_offset
, unsigned long len
)
4335 size_t dst_off_in_page
;
4336 size_t src_off_in_page
;
4337 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4338 unsigned long dst_i
;
4339 unsigned long src_i
;
4341 if (src_offset
+ len
> dst
->len
) {
4342 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4343 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
4346 if (dst_offset
+ len
> dst
->len
) {
4347 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4348 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
4353 dst_off_in_page
= (start_offset
+ dst_offset
) &
4354 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4355 src_off_in_page
= (start_offset
+ src_offset
) &
4356 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4358 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
4359 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
4361 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
4363 cur
= min_t(unsigned long, cur
,
4364 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
4366 copy_pages(extent_buffer_page(dst
, dst_i
),
4367 extent_buffer_page(dst
, src_i
),
4368 dst_off_in_page
, src_off_in_page
, cur
);
4376 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4377 unsigned long src_offset
, unsigned long len
)
4380 size_t dst_off_in_page
;
4381 size_t src_off_in_page
;
4382 unsigned long dst_end
= dst_offset
+ len
- 1;
4383 unsigned long src_end
= src_offset
+ len
- 1;
4384 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4385 unsigned long dst_i
;
4386 unsigned long src_i
;
4388 if (src_offset
+ len
> dst
->len
) {
4389 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4390 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
4393 if (dst_offset
+ len
> dst
->len
) {
4394 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4395 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
4398 if (!areas_overlap(src_offset
, dst_offset
, len
)) {
4399 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
4403 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
4404 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
4406 dst_off_in_page
= (start_offset
+ dst_end
) &
4407 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4408 src_off_in_page
= (start_offset
+ src_end
) &
4409 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4411 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
4412 cur
= min(cur
, dst_off_in_page
+ 1);
4413 move_pages(extent_buffer_page(dst
, dst_i
),
4414 extent_buffer_page(dst
, src_i
),
4415 dst_off_in_page
- cur
+ 1,
4416 src_off_in_page
- cur
+ 1, cur
);
4424 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head
*head
)
4426 struct extent_buffer
*eb
=
4427 container_of(head
, struct extent_buffer
, rcu_head
);
4429 btrfs_release_extent_buffer(eb
);
4432 int try_release_extent_buffer(struct extent_io_tree
*tree
, struct page
*page
)
4434 u64 start
= page_offset(page
);
4435 struct extent_buffer
*eb
;
4438 spin_lock(&tree
->buffer_lock
);
4439 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4441 spin_unlock(&tree
->buffer_lock
);
4445 if (test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
4451 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4454 if (atomic_cmpxchg(&eb
->refs
, 1, 0) != 1) {
4459 radix_tree_delete(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4461 spin_unlock(&tree
->buffer_lock
);
4463 /* at this point we can safely release the extent buffer */
4464 if (atomic_read(&eb
->refs
) == 0)
4465 call_rcu(&eb
->rcu_head
, btrfs_release_extent_buffer_rcu
);