1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/module.h>
8 #include <linux/spinlock.h>
9 #include <linux/blkdev.h>
10 #include <linux/swap.h>
11 #include <linux/writeback.h>
12 #include <linux/pagevec.h>
13 #include <linux/prefetch.h>
14 #include <linux/cleancache.h>
15 #include "extent_io.h"
16 #include "extent_map.h"
19 #include "btrfs_inode.h"
21 static struct kmem_cache
*extent_state_cache
;
22 static struct kmem_cache
*extent_buffer_cache
;
24 static LIST_HEAD(buffers
);
25 static LIST_HEAD(states
);
29 static DEFINE_SPINLOCK(leak_lock
);
32 #define BUFFER_LRU_MAX 64
37 struct rb_node rb_node
;
40 struct extent_page_data
{
42 struct extent_io_tree
*tree
;
43 get_extent_t
*get_extent
;
45 /* tells writepage not to lock the state bits for this range
46 * it still does the unlocking
48 unsigned int extent_locked
:1;
50 /* tells the submit_bio code to use a WRITE_SYNC */
51 unsigned int sync_io
:1;
54 int __init
extent_io_init(void)
56 extent_state_cache
= kmem_cache_create("extent_state",
57 sizeof(struct extent_state
), 0,
58 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
59 if (!extent_state_cache
)
62 extent_buffer_cache
= kmem_cache_create("extent_buffers",
63 sizeof(struct extent_buffer
), 0,
64 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
65 if (!extent_buffer_cache
)
66 goto free_state_cache
;
70 kmem_cache_destroy(extent_state_cache
);
74 void extent_io_exit(void)
76 struct extent_state
*state
;
77 struct extent_buffer
*eb
;
79 while (!list_empty(&states
)) {
80 state
= list_entry(states
.next
, struct extent_state
, leak_list
);
81 printk(KERN_ERR
"btrfs state leak: start %llu end %llu "
82 "state %lu in tree %p refs %d\n",
83 (unsigned long long)state
->start
,
84 (unsigned long long)state
->end
,
85 state
->state
, state
->tree
, atomic_read(&state
->refs
));
86 list_del(&state
->leak_list
);
87 kmem_cache_free(extent_state_cache
, state
);
91 while (!list_empty(&buffers
)) {
92 eb
= list_entry(buffers
.next
, struct extent_buffer
, leak_list
);
93 printk(KERN_ERR
"btrfs buffer leak start %llu len %lu "
94 "refs %d\n", (unsigned long long)eb
->start
,
95 eb
->len
, atomic_read(&eb
->refs
));
96 list_del(&eb
->leak_list
);
97 kmem_cache_free(extent_buffer_cache
, eb
);
99 if (extent_state_cache
)
100 kmem_cache_destroy(extent_state_cache
);
101 if (extent_buffer_cache
)
102 kmem_cache_destroy(extent_buffer_cache
);
105 void extent_io_tree_init(struct extent_io_tree
*tree
,
106 struct address_space
*mapping
)
108 tree
->state
= RB_ROOT
;
109 INIT_RADIX_TREE(&tree
->buffer
, GFP_ATOMIC
);
111 tree
->dirty_bytes
= 0;
112 spin_lock_init(&tree
->lock
);
113 spin_lock_init(&tree
->buffer_lock
);
114 tree
->mapping
= mapping
;
117 static struct extent_state
*alloc_extent_state(gfp_t mask
)
119 struct extent_state
*state
;
124 state
= kmem_cache_alloc(extent_state_cache
, mask
);
131 spin_lock_irqsave(&leak_lock
, flags
);
132 list_add(&state
->leak_list
, &states
);
133 spin_unlock_irqrestore(&leak_lock
, flags
);
135 atomic_set(&state
->refs
, 1);
136 init_waitqueue_head(&state
->wq
);
140 void free_extent_state(struct extent_state
*state
)
144 if (atomic_dec_and_test(&state
->refs
)) {
148 WARN_ON(state
->tree
);
150 spin_lock_irqsave(&leak_lock
, flags
);
151 list_del(&state
->leak_list
);
152 spin_unlock_irqrestore(&leak_lock
, flags
);
154 kmem_cache_free(extent_state_cache
, state
);
158 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
159 struct rb_node
*node
)
161 struct rb_node
**p
= &root
->rb_node
;
162 struct rb_node
*parent
= NULL
;
163 struct tree_entry
*entry
;
167 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
169 if (offset
< entry
->start
)
171 else if (offset
> entry
->end
)
177 entry
= rb_entry(node
, struct tree_entry
, rb_node
);
178 rb_link_node(node
, parent
, p
);
179 rb_insert_color(node
, root
);
183 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
184 struct rb_node
**prev_ret
,
185 struct rb_node
**next_ret
)
187 struct rb_root
*root
= &tree
->state
;
188 struct rb_node
*n
= root
->rb_node
;
189 struct rb_node
*prev
= NULL
;
190 struct rb_node
*orig_prev
= NULL
;
191 struct tree_entry
*entry
;
192 struct tree_entry
*prev_entry
= NULL
;
195 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
199 if (offset
< entry
->start
)
201 else if (offset
> entry
->end
)
209 while (prev
&& offset
> prev_entry
->end
) {
210 prev
= rb_next(prev
);
211 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
218 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
219 while (prev
&& offset
< prev_entry
->start
) {
220 prev
= rb_prev(prev
);
221 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
228 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
231 struct rb_node
*prev
= NULL
;
234 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
240 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
241 struct extent_state
*other
)
243 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
244 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
249 * utility function to look for merge candidates inside a given range.
250 * Any extents with matching state are merged together into a single
251 * extent in the tree. Extents with EXTENT_IO in their state field
252 * are not merged because the end_io handlers need to be able to do
253 * operations on them without sleeping (or doing allocations/splits).
255 * This should be called with the tree lock held.
257 static int merge_state(struct extent_io_tree
*tree
,
258 struct extent_state
*state
)
260 struct extent_state
*other
;
261 struct rb_node
*other_node
;
263 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
266 other_node
= rb_prev(&state
->rb_node
);
268 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
269 if (other
->end
== state
->start
- 1 &&
270 other
->state
== state
->state
) {
271 merge_cb(tree
, state
, other
);
272 state
->start
= other
->start
;
274 rb_erase(&other
->rb_node
, &tree
->state
);
275 free_extent_state(other
);
278 other_node
= rb_next(&state
->rb_node
);
280 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
281 if (other
->start
== state
->end
+ 1 &&
282 other
->state
== state
->state
) {
283 merge_cb(tree
, state
, other
);
284 state
->end
= other
->end
;
286 rb_erase(&other
->rb_node
, &tree
->state
);
287 free_extent_state(other
);
294 static int set_state_cb(struct extent_io_tree
*tree
,
295 struct extent_state
*state
, int *bits
)
297 if (tree
->ops
&& tree
->ops
->set_bit_hook
) {
298 return tree
->ops
->set_bit_hook(tree
->mapping
->host
,
305 static void clear_state_cb(struct extent_io_tree
*tree
,
306 struct extent_state
*state
, int *bits
)
308 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
309 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
, bits
);
313 * insert an extent_state struct into the tree. 'bits' are set on the
314 * struct before it is inserted.
316 * This may return -EEXIST if the extent is already there, in which case the
317 * state struct is freed.
319 * The tree lock is not taken internally. This is a utility function and
320 * probably isn't what you want to call (see set/clear_extent_bit).
322 static int insert_state(struct extent_io_tree
*tree
,
323 struct extent_state
*state
, u64 start
, u64 end
,
326 struct rb_node
*node
;
327 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
331 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
332 (unsigned long long)end
,
333 (unsigned long long)start
);
336 state
->start
= start
;
338 ret
= set_state_cb(tree
, state
, bits
);
342 if (bits_to_set
& EXTENT_DIRTY
)
343 tree
->dirty_bytes
+= end
- start
+ 1;
344 state
->state
|= bits_to_set
;
345 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
347 struct extent_state
*found
;
348 found
= rb_entry(node
, struct extent_state
, rb_node
);
349 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
350 "%llu %llu\n", (unsigned long long)found
->start
,
351 (unsigned long long)found
->end
,
352 (unsigned long long)start
, (unsigned long long)end
);
356 merge_state(tree
, state
);
360 static int split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
363 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
364 return tree
->ops
->split_extent_hook(tree
->mapping
->host
,
370 * split a given extent state struct in two, inserting the preallocated
371 * struct 'prealloc' as the newly created second half. 'split' indicates an
372 * offset inside 'orig' where it should be split.
375 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
376 * are two extent state structs in the tree:
377 * prealloc: [orig->start, split - 1]
378 * orig: [ split, orig->end ]
380 * The tree locks are not taken by this function. They need to be held
383 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
384 struct extent_state
*prealloc
, u64 split
)
386 struct rb_node
*node
;
388 split_cb(tree
, orig
, split
);
390 prealloc
->start
= orig
->start
;
391 prealloc
->end
= split
- 1;
392 prealloc
->state
= orig
->state
;
395 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
397 free_extent_state(prealloc
);
400 prealloc
->tree
= tree
;
405 * utility function to clear some bits in an extent state struct.
406 * it will optionally wake up any one waiting on this state (wake == 1), or
407 * forcibly remove the state from the tree (delete == 1).
409 * If no bits are set on the state struct after clearing things, the
410 * struct is freed and removed from the tree
412 static int clear_state_bit(struct extent_io_tree
*tree
,
413 struct extent_state
*state
,
416 int bits_to_clear
= *bits
& ~EXTENT_CTLBITS
;
417 int ret
= state
->state
& bits_to_clear
;
419 if ((bits_to_clear
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
420 u64 range
= state
->end
- state
->start
+ 1;
421 WARN_ON(range
> tree
->dirty_bytes
);
422 tree
->dirty_bytes
-= range
;
424 clear_state_cb(tree
, state
, bits
);
425 state
->state
&= ~bits_to_clear
;
428 if (state
->state
== 0) {
430 rb_erase(&state
->rb_node
, &tree
->state
);
432 free_extent_state(state
);
437 merge_state(tree
, state
);
442 static struct extent_state
*
443 alloc_extent_state_atomic(struct extent_state
*prealloc
)
446 prealloc
= alloc_extent_state(GFP_ATOMIC
);
452 * clear some bits on a range in the tree. This may require splitting
453 * or inserting elements in the tree, so the gfp mask is used to
454 * indicate which allocations or sleeping are allowed.
456 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
457 * the given range from the tree regardless of state (ie for truncate).
459 * the range [start, end] is inclusive.
461 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
462 * bits were already set, or zero if none of the bits were already set.
464 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
465 int bits
, int wake
, int delete,
466 struct extent_state
**cached_state
,
469 struct extent_state
*state
;
470 struct extent_state
*cached
;
471 struct extent_state
*prealloc
= NULL
;
472 struct rb_node
*next_node
;
473 struct rb_node
*node
;
480 bits
|= ~EXTENT_CTLBITS
;
481 bits
|= EXTENT_FIRST_DELALLOC
;
483 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
486 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
487 prealloc
= alloc_extent_state(mask
);
492 spin_lock(&tree
->lock
);
494 cached
= *cached_state
;
497 *cached_state
= NULL
;
501 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
502 cached
->end
> start
) {
504 atomic_dec(&cached
->refs
);
509 free_extent_state(cached
);
512 * this search will find the extents that end after
515 node
= tree_search(tree
, start
);
518 state
= rb_entry(node
, struct extent_state
, rb_node
);
520 if (state
->start
> end
)
522 WARN_ON(state
->end
< start
);
523 last_end
= state
->end
;
526 * | ---- desired range ---- |
528 * | ------------- state -------------- |
530 * We need to split the extent we found, and may flip
531 * bits on second half.
533 * If the extent we found extends past our range, we
534 * just split and search again. It'll get split again
535 * the next time though.
537 * If the extent we found is inside our range, we clear
538 * the desired bit on it.
541 if (state
->start
< start
) {
542 prealloc
= alloc_extent_state_atomic(prealloc
);
544 err
= split_state(tree
, state
, prealloc
, start
);
545 BUG_ON(err
== -EEXIST
);
549 if (state
->end
<= end
) {
550 set
|= clear_state_bit(tree
, state
, &bits
, wake
);
551 if (last_end
== (u64
)-1)
553 start
= last_end
+ 1;
558 * | ---- desired range ---- |
560 * We need to split the extent, and clear the bit
563 if (state
->start
<= end
&& state
->end
> end
) {
564 prealloc
= alloc_extent_state_atomic(prealloc
);
566 err
= split_state(tree
, state
, prealloc
, end
+ 1);
567 BUG_ON(err
== -EEXIST
);
571 set
|= clear_state_bit(tree
, prealloc
, &bits
, wake
);
577 if (state
->end
< end
&& prealloc
&& !need_resched())
578 next_node
= rb_next(&state
->rb_node
);
582 set
|= clear_state_bit(tree
, state
, &bits
, wake
);
583 if (last_end
== (u64
)-1)
585 start
= last_end
+ 1;
586 if (start
<= end
&& next_node
) {
587 state
= rb_entry(next_node
, struct extent_state
,
589 if (state
->start
== start
)
595 spin_unlock(&tree
->lock
);
597 free_extent_state(prealloc
);
604 spin_unlock(&tree
->lock
);
605 if (mask
& __GFP_WAIT
)
610 static int wait_on_state(struct extent_io_tree
*tree
,
611 struct extent_state
*state
)
612 __releases(tree
->lock
)
613 __acquires(tree
->lock
)
616 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
617 spin_unlock(&tree
->lock
);
619 spin_lock(&tree
->lock
);
620 finish_wait(&state
->wq
, &wait
);
625 * waits for one or more bits to clear on a range in the state tree.
626 * The range [start, end] is inclusive.
627 * The tree lock is taken by this function
629 int wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
631 struct extent_state
*state
;
632 struct rb_node
*node
;
634 spin_lock(&tree
->lock
);
638 * this search will find all the extents that end after
641 node
= tree_search(tree
, start
);
645 state
= rb_entry(node
, struct extent_state
, rb_node
);
647 if (state
->start
> end
)
650 if (state
->state
& bits
) {
651 start
= state
->start
;
652 atomic_inc(&state
->refs
);
653 wait_on_state(tree
, state
);
654 free_extent_state(state
);
657 start
= state
->end
+ 1;
662 if (need_resched()) {
663 spin_unlock(&tree
->lock
);
665 spin_lock(&tree
->lock
);
669 spin_unlock(&tree
->lock
);
673 static int set_state_bits(struct extent_io_tree
*tree
,
674 struct extent_state
*state
,
678 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
680 ret
= set_state_cb(tree
, state
, bits
);
683 if ((bits_to_set
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
684 u64 range
= state
->end
- state
->start
+ 1;
685 tree
->dirty_bytes
+= range
;
687 state
->state
|= bits_to_set
;
692 static void cache_state(struct extent_state
*state
,
693 struct extent_state
**cached_ptr
)
695 if (cached_ptr
&& !(*cached_ptr
)) {
696 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
698 atomic_inc(&state
->refs
);
703 static void uncache_state(struct extent_state
**cached_ptr
)
705 if (cached_ptr
&& (*cached_ptr
)) {
706 struct extent_state
*state
= *cached_ptr
;
708 free_extent_state(state
);
713 * set some bits on a range in the tree. This may require allocations or
714 * sleeping, so the gfp mask is used to indicate what is allowed.
716 * If any of the exclusive bits are set, this will fail with -EEXIST if some
717 * part of the range already has the desired bits set. The start of the
718 * existing range is returned in failed_start in this case.
720 * [start, end] is inclusive This takes the tree lock.
723 int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
724 int bits
, int exclusive_bits
, u64
*failed_start
,
725 struct extent_state
**cached_state
, gfp_t mask
)
727 struct extent_state
*state
;
728 struct extent_state
*prealloc
= NULL
;
729 struct rb_node
*node
;
734 bits
|= EXTENT_FIRST_DELALLOC
;
736 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
737 prealloc
= alloc_extent_state(mask
);
741 spin_lock(&tree
->lock
);
742 if (cached_state
&& *cached_state
) {
743 state
= *cached_state
;
744 if (state
->start
<= start
&& state
->end
> start
&&
746 node
= &state
->rb_node
;
751 * this search will find all the extents that end after
754 node
= tree_search(tree
, start
);
756 prealloc
= alloc_extent_state_atomic(prealloc
);
758 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
760 BUG_ON(err
== -EEXIST
);
763 state
= rb_entry(node
, struct extent_state
, rb_node
);
765 last_start
= state
->start
;
766 last_end
= state
->end
;
769 * | ---- desired range ---- |
772 * Just lock what we found and keep going
774 if (state
->start
== start
&& state
->end
<= end
) {
775 struct rb_node
*next_node
;
776 if (state
->state
& exclusive_bits
) {
777 *failed_start
= state
->start
;
782 err
= set_state_bits(tree
, state
, &bits
);
786 cache_state(state
, cached_state
);
787 merge_state(tree
, state
);
788 if (last_end
== (u64
)-1)
791 start
= last_end
+ 1;
792 next_node
= rb_next(&state
->rb_node
);
793 if (next_node
&& start
< end
&& prealloc
&& !need_resched()) {
794 state
= rb_entry(next_node
, struct extent_state
,
796 if (state
->start
== start
)
803 * | ---- desired range ---- |
806 * | ------------- state -------------- |
808 * We need to split the extent we found, and may flip bits on
811 * If the extent we found extends past our
812 * range, we just split and search again. It'll get split
813 * again the next time though.
815 * If the extent we found is inside our range, we set the
818 if (state
->start
< start
) {
819 if (state
->state
& exclusive_bits
) {
820 *failed_start
= start
;
825 prealloc
= alloc_extent_state_atomic(prealloc
);
827 err
= split_state(tree
, state
, prealloc
, start
);
828 BUG_ON(err
== -EEXIST
);
832 if (state
->end
<= end
) {
833 err
= set_state_bits(tree
, state
, &bits
);
836 cache_state(state
, cached_state
);
837 merge_state(tree
, state
);
838 if (last_end
== (u64
)-1)
840 start
= last_end
+ 1;
845 * | ---- desired range ---- |
846 * | state | or | state |
848 * There's a hole, we need to insert something in it and
849 * ignore the extent we found.
851 if (state
->start
> start
) {
853 if (end
< last_start
)
856 this_end
= last_start
- 1;
858 prealloc
= alloc_extent_state_atomic(prealloc
);
862 * Avoid to free 'prealloc' if it can be merged with
865 err
= insert_state(tree
, prealloc
, start
, this_end
,
867 BUG_ON(err
== -EEXIST
);
869 free_extent_state(prealloc
);
873 cache_state(prealloc
, cached_state
);
875 start
= this_end
+ 1;
879 * | ---- desired range ---- |
881 * We need to split the extent, and set the bit
884 if (state
->start
<= end
&& state
->end
> end
) {
885 if (state
->state
& exclusive_bits
) {
886 *failed_start
= start
;
891 prealloc
= alloc_extent_state_atomic(prealloc
);
893 err
= split_state(tree
, state
, prealloc
, end
+ 1);
894 BUG_ON(err
== -EEXIST
);
896 err
= set_state_bits(tree
, prealloc
, &bits
);
901 cache_state(prealloc
, cached_state
);
902 merge_state(tree
, prealloc
);
910 spin_unlock(&tree
->lock
);
912 free_extent_state(prealloc
);
919 spin_unlock(&tree
->lock
);
920 if (mask
& __GFP_WAIT
)
925 /* wrappers around set/clear extent bit */
926 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
929 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, 0, NULL
,
933 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
934 int bits
, gfp_t mask
)
936 return set_extent_bit(tree
, start
, end
, bits
, 0, NULL
,
940 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
941 int bits
, gfp_t mask
)
943 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
946 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
947 struct extent_state
**cached_state
, gfp_t mask
)
949 return set_extent_bit(tree
, start
, end
,
950 EXTENT_DELALLOC
| EXTENT_DIRTY
| EXTENT_UPTODATE
,
951 0, NULL
, cached_state
, mask
);
954 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
957 return clear_extent_bit(tree
, start
, end
,
958 EXTENT_DIRTY
| EXTENT_DELALLOC
|
959 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, mask
);
962 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
965 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, NULL
,
969 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
970 struct extent_state
**cached_state
, gfp_t mask
)
972 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0,
973 NULL
, cached_state
, mask
);
976 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
977 u64 end
, struct extent_state
**cached_state
,
980 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
985 * either insert or lock state struct between start and end use mask to tell
986 * us if waiting is desired.
988 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
989 int bits
, struct extent_state
**cached_state
, gfp_t mask
)
994 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
995 EXTENT_LOCKED
, &failed_start
,
997 if (err
== -EEXIST
&& (mask
& __GFP_WAIT
)) {
998 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
999 start
= failed_start
;
1003 WARN_ON(start
> end
);
1008 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
1010 return lock_extent_bits(tree
, start
, end
, 0, NULL
, mask
);
1013 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1019 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1020 &failed_start
, NULL
, mask
);
1021 if (err
== -EEXIST
) {
1022 if (failed_start
> start
)
1023 clear_extent_bit(tree
, start
, failed_start
- 1,
1024 EXTENT_LOCKED
, 1, 0, NULL
, mask
);
1030 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1031 struct extent_state
**cached
, gfp_t mask
)
1033 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1037 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
1039 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1044 * helper function to set both pages and extents in the tree writeback
1046 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1048 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1049 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1052 while (index
<= end_index
) {
1053 page
= find_get_page(tree
->mapping
, index
);
1055 set_page_writeback(page
);
1056 page_cache_release(page
);
1063 * find the first offset in the io tree with 'bits' set. zero is
1064 * returned if we find something, and *start_ret and *end_ret are
1065 * set to reflect the state struct that was found.
1067 * If nothing was found, 1 is returned, < 0 on error
1069 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1070 u64
*start_ret
, u64
*end_ret
, int bits
)
1072 struct rb_node
*node
;
1073 struct extent_state
*state
;
1076 spin_lock(&tree
->lock
);
1078 * this search will find all the extents that end after
1081 node
= tree_search(tree
, start
);
1086 state
= rb_entry(node
, struct extent_state
, rb_node
);
1087 if (state
->end
>= start
&& (state
->state
& bits
)) {
1088 *start_ret
= state
->start
;
1089 *end_ret
= state
->end
;
1093 node
= rb_next(node
);
1098 spin_unlock(&tree
->lock
);
1102 /* find the first state struct with 'bits' set after 'start', and
1103 * return it. tree->lock must be held. NULL will returned if
1104 * nothing was found after 'start'
1106 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1107 u64 start
, int bits
)
1109 struct rb_node
*node
;
1110 struct extent_state
*state
;
1113 * this search will find all the extents that end after
1116 node
= tree_search(tree
, start
);
1121 state
= rb_entry(node
, struct extent_state
, rb_node
);
1122 if (state
->end
>= start
&& (state
->state
& bits
))
1125 node
= rb_next(node
);
1134 * find a contiguous range of bytes in the file marked as delalloc, not
1135 * more than 'max_bytes'. start and end are used to return the range,
1137 * 1 is returned if we find something, 0 if nothing was in the tree
1139 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1140 u64
*start
, u64
*end
, u64 max_bytes
,
1141 struct extent_state
**cached_state
)
1143 struct rb_node
*node
;
1144 struct extent_state
*state
;
1145 u64 cur_start
= *start
;
1147 u64 total_bytes
= 0;
1149 spin_lock(&tree
->lock
);
1152 * this search will find all the extents that end after
1155 node
= tree_search(tree
, cur_start
);
1163 state
= rb_entry(node
, struct extent_state
, rb_node
);
1164 if (found
&& (state
->start
!= cur_start
||
1165 (state
->state
& EXTENT_BOUNDARY
))) {
1168 if (!(state
->state
& EXTENT_DELALLOC
)) {
1174 *start
= state
->start
;
1175 *cached_state
= state
;
1176 atomic_inc(&state
->refs
);
1180 cur_start
= state
->end
+ 1;
1181 node
= rb_next(node
);
1184 total_bytes
+= state
->end
- state
->start
+ 1;
1185 if (total_bytes
>= max_bytes
)
1189 spin_unlock(&tree
->lock
);
1193 static noinline
int __unlock_for_delalloc(struct inode
*inode
,
1194 struct page
*locked_page
,
1198 struct page
*pages
[16];
1199 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1200 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1201 unsigned long nr_pages
= end_index
- index
+ 1;
1204 if (index
== locked_page
->index
&& end_index
== index
)
1207 while (nr_pages
> 0) {
1208 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1209 min_t(unsigned long, nr_pages
,
1210 ARRAY_SIZE(pages
)), pages
);
1211 for (i
= 0; i
< ret
; i
++) {
1212 if (pages
[i
] != locked_page
)
1213 unlock_page(pages
[i
]);
1214 page_cache_release(pages
[i
]);
1223 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1224 struct page
*locked_page
,
1228 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1229 unsigned long start_index
= index
;
1230 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1231 unsigned long pages_locked
= 0;
1232 struct page
*pages
[16];
1233 unsigned long nrpages
;
1237 /* the caller is responsible for locking the start index */
1238 if (index
== locked_page
->index
&& index
== end_index
)
1241 /* skip the page at the start index */
1242 nrpages
= end_index
- index
+ 1;
1243 while (nrpages
> 0) {
1244 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1245 min_t(unsigned long,
1246 nrpages
, ARRAY_SIZE(pages
)), pages
);
1251 /* now we have an array of pages, lock them all */
1252 for (i
= 0; i
< ret
; i
++) {
1254 * the caller is taking responsibility for
1257 if (pages
[i
] != locked_page
) {
1258 lock_page(pages
[i
]);
1259 if (!PageDirty(pages
[i
]) ||
1260 pages
[i
]->mapping
!= inode
->i_mapping
) {
1262 unlock_page(pages
[i
]);
1263 page_cache_release(pages
[i
]);
1267 page_cache_release(pages
[i
]);
1276 if (ret
&& pages_locked
) {
1277 __unlock_for_delalloc(inode
, locked_page
,
1279 ((u64
)(start_index
+ pages_locked
- 1)) <<
1286 * find a contiguous range of bytes in the file marked as delalloc, not
1287 * more than 'max_bytes'. start and end are used to return the range,
1289 * 1 is returned if we find something, 0 if nothing was in the tree
1291 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1292 struct extent_io_tree
*tree
,
1293 struct page
*locked_page
,
1294 u64
*start
, u64
*end
,
1300 struct extent_state
*cached_state
= NULL
;
1305 /* step one, find a bunch of delalloc bytes starting at start */
1306 delalloc_start
= *start
;
1308 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1309 max_bytes
, &cached_state
);
1310 if (!found
|| delalloc_end
<= *start
) {
1311 *start
= delalloc_start
;
1312 *end
= delalloc_end
;
1313 free_extent_state(cached_state
);
1318 * start comes from the offset of locked_page. We have to lock
1319 * pages in order, so we can't process delalloc bytes before
1322 if (delalloc_start
< *start
)
1323 delalloc_start
= *start
;
1326 * make sure to limit the number of pages we try to lock down
1329 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1330 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1332 /* step two, lock all the pages after the page that has start */
1333 ret
= lock_delalloc_pages(inode
, locked_page
,
1334 delalloc_start
, delalloc_end
);
1335 if (ret
== -EAGAIN
) {
1336 /* some of the pages are gone, lets avoid looping by
1337 * shortening the size of the delalloc range we're searching
1339 free_extent_state(cached_state
);
1341 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1342 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1352 /* step three, lock the state bits for the whole range */
1353 lock_extent_bits(tree
, delalloc_start
, delalloc_end
,
1354 0, &cached_state
, GFP_NOFS
);
1356 /* then test to make sure it is all still delalloc */
1357 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1358 EXTENT_DELALLOC
, 1, cached_state
);
1360 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1361 &cached_state
, GFP_NOFS
);
1362 __unlock_for_delalloc(inode
, locked_page
,
1363 delalloc_start
, delalloc_end
);
1367 free_extent_state(cached_state
);
1368 *start
= delalloc_start
;
1369 *end
= delalloc_end
;
1374 int extent_clear_unlock_delalloc(struct inode
*inode
,
1375 struct extent_io_tree
*tree
,
1376 u64 start
, u64 end
, struct page
*locked_page
,
1380 struct page
*pages
[16];
1381 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1382 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1383 unsigned long nr_pages
= end_index
- index
+ 1;
1387 if (op
& EXTENT_CLEAR_UNLOCK
)
1388 clear_bits
|= EXTENT_LOCKED
;
1389 if (op
& EXTENT_CLEAR_DIRTY
)
1390 clear_bits
|= EXTENT_DIRTY
;
1392 if (op
& EXTENT_CLEAR_DELALLOC
)
1393 clear_bits
|= EXTENT_DELALLOC
;
1395 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1396 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1397 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1398 EXTENT_SET_PRIVATE2
)))
1401 while (nr_pages
> 0) {
1402 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1403 min_t(unsigned long,
1404 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1405 for (i
= 0; i
< ret
; i
++) {
1407 if (op
& EXTENT_SET_PRIVATE2
)
1408 SetPagePrivate2(pages
[i
]);
1410 if (pages
[i
] == locked_page
) {
1411 page_cache_release(pages
[i
]);
1414 if (op
& EXTENT_CLEAR_DIRTY
)
1415 clear_page_dirty_for_io(pages
[i
]);
1416 if (op
& EXTENT_SET_WRITEBACK
)
1417 set_page_writeback(pages
[i
]);
1418 if (op
& EXTENT_END_WRITEBACK
)
1419 end_page_writeback(pages
[i
]);
1420 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1421 unlock_page(pages
[i
]);
1422 page_cache_release(pages
[i
]);
1432 * count the number of bytes in the tree that have a given bit(s)
1433 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1434 * cached. The total number found is returned.
1436 u64
count_range_bits(struct extent_io_tree
*tree
,
1437 u64
*start
, u64 search_end
, u64 max_bytes
,
1438 unsigned long bits
, int contig
)
1440 struct rb_node
*node
;
1441 struct extent_state
*state
;
1442 u64 cur_start
= *start
;
1443 u64 total_bytes
= 0;
1447 if (search_end
<= cur_start
) {
1452 spin_lock(&tree
->lock
);
1453 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1454 total_bytes
= tree
->dirty_bytes
;
1458 * this search will find all the extents that end after
1461 node
= tree_search(tree
, cur_start
);
1466 state
= rb_entry(node
, struct extent_state
, rb_node
);
1467 if (state
->start
> search_end
)
1469 if (contig
&& found
&& state
->start
> last
+ 1)
1471 if (state
->end
>= cur_start
&& (state
->state
& bits
) == bits
) {
1472 total_bytes
+= min(search_end
, state
->end
) + 1 -
1473 max(cur_start
, state
->start
);
1474 if (total_bytes
>= max_bytes
)
1477 *start
= max(cur_start
, state
->start
);
1481 } else if (contig
&& found
) {
1484 node
= rb_next(node
);
1489 spin_unlock(&tree
->lock
);
1494 * set the private field for a given byte offset in the tree. If there isn't
1495 * an extent_state there already, this does nothing.
1497 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1499 struct rb_node
*node
;
1500 struct extent_state
*state
;
1503 spin_lock(&tree
->lock
);
1505 * this search will find all the extents that end after
1508 node
= tree_search(tree
, start
);
1513 state
= rb_entry(node
, struct extent_state
, rb_node
);
1514 if (state
->start
!= start
) {
1518 state
->private = private;
1520 spin_unlock(&tree
->lock
);
1524 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1526 struct rb_node
*node
;
1527 struct extent_state
*state
;
1530 spin_lock(&tree
->lock
);
1532 * this search will find all the extents that end after
1535 node
= tree_search(tree
, start
);
1540 state
= rb_entry(node
, struct extent_state
, rb_node
);
1541 if (state
->start
!= start
) {
1545 *private = state
->private;
1547 spin_unlock(&tree
->lock
);
1552 * searches a range in the state tree for a given mask.
1553 * If 'filled' == 1, this returns 1 only if every extent in the tree
1554 * has the bits set. Otherwise, 1 is returned if any bit in the
1555 * range is found set.
1557 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1558 int bits
, int filled
, struct extent_state
*cached
)
1560 struct extent_state
*state
= NULL
;
1561 struct rb_node
*node
;
1564 spin_lock(&tree
->lock
);
1565 if (cached
&& cached
->tree
&& cached
->start
<= start
&&
1566 cached
->end
> start
)
1567 node
= &cached
->rb_node
;
1569 node
= tree_search(tree
, start
);
1570 while (node
&& start
<= end
) {
1571 state
= rb_entry(node
, struct extent_state
, rb_node
);
1573 if (filled
&& state
->start
> start
) {
1578 if (state
->start
> end
)
1581 if (state
->state
& bits
) {
1585 } else if (filled
) {
1590 if (state
->end
== (u64
)-1)
1593 start
= state
->end
+ 1;
1596 node
= rb_next(node
);
1603 spin_unlock(&tree
->lock
);
1608 * helper function to set a given page up to date if all the
1609 * extents in the tree for that page are up to date
1611 static int check_page_uptodate(struct extent_io_tree
*tree
,
1614 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1615 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1616 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1617 SetPageUptodate(page
);
1622 * helper function to unlock a page if all the extents in the tree
1623 * for that page are unlocked
1625 static int check_page_locked(struct extent_io_tree
*tree
,
1628 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1629 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1630 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1636 * helper function to end page writeback if all the extents
1637 * in the tree for that page are done with writeback
1639 static int check_page_writeback(struct extent_io_tree
*tree
,
1642 end_page_writeback(page
);
1646 /* lots and lots of room for performance fixes in the end_bio funcs */
1649 * after a writepage IO is done, we need to:
1650 * clear the uptodate bits on error
1651 * clear the writeback bits in the extent tree for this IO
1652 * end_page_writeback if the page has no more pending IO
1654 * Scheduling is not allowed, so the extent state tree is expected
1655 * to have one and only one object corresponding to this IO.
1657 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
1659 int uptodate
= err
== 0;
1660 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1661 struct extent_io_tree
*tree
;
1668 struct page
*page
= bvec
->bv_page
;
1669 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1671 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1673 end
= start
+ bvec
->bv_len
- 1;
1675 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1680 if (--bvec
>= bio
->bi_io_vec
)
1681 prefetchw(&bvec
->bv_page
->flags
);
1682 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
1683 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
1684 end
, NULL
, uptodate
);
1689 if (!uptodate
&& tree
->ops
&&
1690 tree
->ops
->writepage_io_failed_hook
) {
1691 ret
= tree
->ops
->writepage_io_failed_hook(bio
, page
,
1694 uptodate
= (err
== 0);
1700 clear_extent_uptodate(tree
, start
, end
, NULL
, GFP_NOFS
);
1701 ClearPageUptodate(page
);
1706 end_page_writeback(page
);
1708 check_page_writeback(tree
, page
);
1709 } while (bvec
>= bio
->bi_io_vec
);
1715 * after a readpage IO is done, we need to:
1716 * clear the uptodate bits on error
1717 * set the uptodate bits if things worked
1718 * set the page up to date if all extents in the tree are uptodate
1719 * clear the lock bit in the extent tree
1720 * unlock the page if there are no other extents locked for it
1722 * Scheduling is not allowed, so the extent state tree is expected
1723 * to have one and only one object corresponding to this IO.
1725 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
1727 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1728 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1729 struct bio_vec
*bvec
= bio
->bi_io_vec
;
1730 struct extent_io_tree
*tree
;
1740 struct page
*page
= bvec
->bv_page
;
1741 struct extent_state
*cached
= NULL
;
1742 struct extent_state
*state
;
1744 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1746 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1748 end
= start
+ bvec
->bv_len
- 1;
1750 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1755 if (++bvec
<= bvec_end
)
1756 prefetchw(&bvec
->bv_page
->flags
);
1758 spin_lock(&tree
->lock
);
1759 state
= find_first_extent_bit_state(tree
, start
, EXTENT_LOCKED
);
1760 if (state
&& state
->start
== start
) {
1762 * take a reference on the state, unlock will drop
1765 cache_state(state
, &cached
);
1767 spin_unlock(&tree
->lock
);
1769 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
1770 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
1775 if (!uptodate
&& tree
->ops
&&
1776 tree
->ops
->readpage_io_failed_hook
) {
1777 ret
= tree
->ops
->readpage_io_failed_hook(bio
, page
,
1781 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1784 uncache_state(&cached
);
1790 set_extent_uptodate(tree
, start
, end
, &cached
,
1793 unlock_extent_cached(tree
, start
, end
, &cached
, GFP_ATOMIC
);
1797 SetPageUptodate(page
);
1799 ClearPageUptodate(page
);
1805 check_page_uptodate(tree
, page
);
1807 ClearPageUptodate(page
);
1810 check_page_locked(tree
, page
);
1812 } while (bvec
<= bvec_end
);
1818 btrfs_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
1823 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1825 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
1826 while (!bio
&& (nr_vecs
/= 2))
1827 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1832 bio
->bi_bdev
= bdev
;
1833 bio
->bi_sector
= first_sector
;
1838 static int submit_one_bio(int rw
, struct bio
*bio
, int mirror_num
,
1839 unsigned long bio_flags
)
1842 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1843 struct page
*page
= bvec
->bv_page
;
1844 struct extent_io_tree
*tree
= bio
->bi_private
;
1847 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
1849 bio
->bi_private
= NULL
;
1853 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
1854 ret
= tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
1855 mirror_num
, bio_flags
, start
);
1857 submit_bio(rw
, bio
);
1858 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
1864 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
1865 struct page
*page
, sector_t sector
,
1866 size_t size
, unsigned long offset
,
1867 struct block_device
*bdev
,
1868 struct bio
**bio_ret
,
1869 unsigned long max_pages
,
1870 bio_end_io_t end_io_func
,
1872 unsigned long prev_bio_flags
,
1873 unsigned long bio_flags
)
1879 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
1880 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
1881 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
1883 if (bio_ret
&& *bio_ret
) {
1886 contig
= bio
->bi_sector
== sector
;
1888 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
1891 if (prev_bio_flags
!= bio_flags
|| !contig
||
1892 (tree
->ops
&& tree
->ops
->merge_bio_hook
&&
1893 tree
->ops
->merge_bio_hook(page
, offset
, page_size
, bio
,
1895 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
1896 ret
= submit_one_bio(rw
, bio
, mirror_num
,
1903 if (this_compressed
)
1906 nr
= bio_get_nr_vecs(bdev
);
1908 bio
= btrfs_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
1912 bio_add_page(bio
, page
, page_size
, offset
);
1913 bio
->bi_end_io
= end_io_func
;
1914 bio
->bi_private
= tree
;
1919 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
1924 void set_page_extent_mapped(struct page
*page
)
1926 if (!PagePrivate(page
)) {
1927 SetPagePrivate(page
);
1928 page_cache_get(page
);
1929 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
1933 static void set_page_extent_head(struct page
*page
, unsigned long len
)
1935 WARN_ON(!PagePrivate(page
));
1936 set_page_private(page
, EXTENT_PAGE_PRIVATE_FIRST_PAGE
| len
<< 2);
1940 * basic readpage implementation. Locked extent state structs are inserted
1941 * into the tree that are removed when the IO is done (by the end_io
1944 static int __extent_read_full_page(struct extent_io_tree
*tree
,
1946 get_extent_t
*get_extent
,
1947 struct bio
**bio
, int mirror_num
,
1948 unsigned long *bio_flags
)
1950 struct inode
*inode
= page
->mapping
->host
;
1951 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1952 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
1956 u64 last_byte
= i_size_read(inode
);
1960 struct extent_map
*em
;
1961 struct block_device
*bdev
;
1962 struct btrfs_ordered_extent
*ordered
;
1965 size_t pg_offset
= 0;
1967 size_t disk_io_size
;
1968 size_t blocksize
= inode
->i_sb
->s_blocksize
;
1969 unsigned long this_bio_flag
= 0;
1971 set_page_extent_mapped(page
);
1973 if (!PageUptodate(page
)) {
1974 if (cleancache_get_page(page
) == 0) {
1975 BUG_ON(blocksize
!= PAGE_SIZE
);
1982 lock_extent(tree
, start
, end
, GFP_NOFS
);
1983 ordered
= btrfs_lookup_ordered_extent(inode
, start
);
1986 unlock_extent(tree
, start
, end
, GFP_NOFS
);
1987 btrfs_start_ordered_extent(inode
, ordered
, 1);
1988 btrfs_put_ordered_extent(ordered
);
1991 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
1993 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
1996 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
1997 userpage
= kmap_atomic(page
, KM_USER0
);
1998 memset(userpage
+ zero_offset
, 0, iosize
);
1999 flush_dcache_page(page
);
2000 kunmap_atomic(userpage
, KM_USER0
);
2003 while (cur
<= end
) {
2004 if (cur
>= last_byte
) {
2006 struct extent_state
*cached
= NULL
;
2008 iosize
= PAGE_CACHE_SIZE
- pg_offset
;
2009 userpage
= kmap_atomic(page
, KM_USER0
);
2010 memset(userpage
+ pg_offset
, 0, iosize
);
2011 flush_dcache_page(page
);
2012 kunmap_atomic(userpage
, KM_USER0
);
2013 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2015 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2019 em
= get_extent(inode
, page
, pg_offset
, cur
,
2021 if (IS_ERR_OR_NULL(em
)) {
2023 unlock_extent(tree
, cur
, end
, GFP_NOFS
);
2026 extent_offset
= cur
- em
->start
;
2027 BUG_ON(extent_map_end(em
) <= cur
);
2030 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2031 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2032 extent_set_compress_type(&this_bio_flag
,
2036 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2037 cur_end
= min(extent_map_end(em
) - 1, end
);
2038 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2039 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2040 disk_io_size
= em
->block_len
;
2041 sector
= em
->block_start
>> 9;
2043 sector
= (em
->block_start
+ extent_offset
) >> 9;
2044 disk_io_size
= iosize
;
2047 block_start
= em
->block_start
;
2048 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2049 block_start
= EXTENT_MAP_HOLE
;
2050 free_extent_map(em
);
2053 /* we've found a hole, just zero and go on */
2054 if (block_start
== EXTENT_MAP_HOLE
) {
2056 struct extent_state
*cached
= NULL
;
2058 userpage
= kmap_atomic(page
, KM_USER0
);
2059 memset(userpage
+ pg_offset
, 0, iosize
);
2060 flush_dcache_page(page
);
2061 kunmap_atomic(userpage
, KM_USER0
);
2063 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2065 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2068 pg_offset
+= iosize
;
2071 /* the get_extent function already copied into the page */
2072 if (test_range_bit(tree
, cur
, cur_end
,
2073 EXTENT_UPTODATE
, 1, NULL
)) {
2074 check_page_uptodate(tree
, page
);
2075 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2077 pg_offset
+= iosize
;
2080 /* we have an inline extent but it didn't get marked up
2081 * to date. Error out
2083 if (block_start
== EXTENT_MAP_INLINE
) {
2085 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2087 pg_offset
+= iosize
;
2092 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2093 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2097 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2099 ret
= submit_extent_page(READ
, tree
, page
,
2100 sector
, disk_io_size
, pg_offset
,
2102 end_bio_extent_readpage
, mirror_num
,
2106 *bio_flags
= this_bio_flag
;
2111 pg_offset
+= iosize
;
2115 if (!PageError(page
))
2116 SetPageUptodate(page
);
2122 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2123 get_extent_t
*get_extent
)
2125 struct bio
*bio
= NULL
;
2126 unsigned long bio_flags
= 0;
2129 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, 0,
2132 ret
= submit_one_bio(READ
, bio
, 0, bio_flags
);
2136 static noinline
void update_nr_written(struct page
*page
,
2137 struct writeback_control
*wbc
,
2138 unsigned long nr_written
)
2140 wbc
->nr_to_write
-= nr_written
;
2141 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2142 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2143 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2147 * the writepage semantics are similar to regular writepage. extent
2148 * records are inserted to lock ranges in the tree, and as dirty areas
2149 * are found, they are marked writeback. Then the lock bits are removed
2150 * and the end_io handler clears the writeback ranges
2152 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2155 struct inode
*inode
= page
->mapping
->host
;
2156 struct extent_page_data
*epd
= data
;
2157 struct extent_io_tree
*tree
= epd
->tree
;
2158 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2160 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2164 u64 last_byte
= i_size_read(inode
);
2168 struct extent_state
*cached_state
= NULL
;
2169 struct extent_map
*em
;
2170 struct block_device
*bdev
;
2173 size_t pg_offset
= 0;
2175 loff_t i_size
= i_size_read(inode
);
2176 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2182 unsigned long nr_written
= 0;
2184 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2185 write_flags
= WRITE_SYNC
;
2187 write_flags
= WRITE
;
2189 trace___extent_writepage(page
, inode
, wbc
);
2191 WARN_ON(!PageLocked(page
));
2192 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2193 if (page
->index
> end_index
||
2194 (page
->index
== end_index
&& !pg_offset
)) {
2195 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2200 if (page
->index
== end_index
) {
2203 userpage
= kmap_atomic(page
, KM_USER0
);
2204 memset(userpage
+ pg_offset
, 0,
2205 PAGE_CACHE_SIZE
- pg_offset
);
2206 kunmap_atomic(userpage
, KM_USER0
);
2207 flush_dcache_page(page
);
2211 set_page_extent_mapped(page
);
2213 delalloc_start
= start
;
2216 if (!epd
->extent_locked
) {
2217 u64 delalloc_to_write
= 0;
2219 * make sure the wbc mapping index is at least updated
2222 update_nr_written(page
, wbc
, 0);
2224 while (delalloc_end
< page_end
) {
2225 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2230 if (nr_delalloc
== 0) {
2231 delalloc_start
= delalloc_end
+ 1;
2234 tree
->ops
->fill_delalloc(inode
, page
, delalloc_start
,
2235 delalloc_end
, &page_started
,
2238 * delalloc_end is already one less than the total
2239 * length, so we don't subtract one from
2242 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2245 delalloc_start
= delalloc_end
+ 1;
2247 if (wbc
->nr_to_write
< delalloc_to_write
) {
2250 if (delalloc_to_write
< thresh
* 2)
2251 thresh
= delalloc_to_write
;
2252 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2256 /* did the fill delalloc function already unlock and start
2262 * we've unlocked the page, so we can't update
2263 * the mapping's writeback index, just update
2266 wbc
->nr_to_write
-= nr_written
;
2270 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2271 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2273 if (ret
== -EAGAIN
) {
2274 redirty_page_for_writepage(wbc
, page
);
2275 update_nr_written(page
, wbc
, nr_written
);
2283 * we don't want to touch the inode after unlocking the page,
2284 * so we update the mapping writeback index now
2286 update_nr_written(page
, wbc
, nr_written
+ 1);
2289 if (last_byte
<= start
) {
2290 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2291 tree
->ops
->writepage_end_io_hook(page
, start
,
2296 blocksize
= inode
->i_sb
->s_blocksize
;
2298 while (cur
<= end
) {
2299 if (cur
>= last_byte
) {
2300 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2301 tree
->ops
->writepage_end_io_hook(page
, cur
,
2305 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2307 if (IS_ERR_OR_NULL(em
)) {
2312 extent_offset
= cur
- em
->start
;
2313 BUG_ON(extent_map_end(em
) <= cur
);
2315 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2316 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2317 sector
= (em
->block_start
+ extent_offset
) >> 9;
2319 block_start
= em
->block_start
;
2320 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2321 free_extent_map(em
);
2325 * compressed and inline extents are written through other
2328 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2329 block_start
== EXTENT_MAP_INLINE
) {
2331 * end_io notification does not happen here for
2332 * compressed extents
2334 if (!compressed
&& tree
->ops
&&
2335 tree
->ops
->writepage_end_io_hook
)
2336 tree
->ops
->writepage_end_io_hook(page
, cur
,
2339 else if (compressed
) {
2340 /* we don't want to end_page_writeback on
2341 * a compressed extent. this happens
2348 pg_offset
+= iosize
;
2351 /* leave this out until we have a page_mkwrite call */
2352 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2353 EXTENT_DIRTY
, 0, NULL
)) {
2355 pg_offset
+= iosize
;
2359 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2360 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
2368 unsigned long max_nr
= end_index
+ 1;
2370 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
2371 if (!PageWriteback(page
)) {
2372 printk(KERN_ERR
"btrfs warning page %lu not "
2373 "writeback, cur %llu end %llu\n",
2374 page
->index
, (unsigned long long)cur
,
2375 (unsigned long long)end
);
2378 ret
= submit_extent_page(write_flags
, tree
, page
,
2379 sector
, iosize
, pg_offset
,
2380 bdev
, &epd
->bio
, max_nr
,
2381 end_bio_extent_writepage
,
2387 pg_offset
+= iosize
;
2392 /* make sure the mapping tag for page dirty gets cleared */
2393 set_page_writeback(page
);
2394 end_page_writeback(page
);
2400 /* drop our reference on any cached states */
2401 free_extent_state(cached_state
);
2406 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2407 * @mapping: address space structure to write
2408 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2409 * @writepage: function called for each page
2410 * @data: data passed to writepage function
2412 * If a page is already under I/O, write_cache_pages() skips it, even
2413 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2414 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2415 * and msync() need to guarantee that all the data which was dirty at the time
2416 * the call was made get new I/O started against them. If wbc->sync_mode is
2417 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2418 * existing IO to complete.
2420 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
2421 struct address_space
*mapping
,
2422 struct writeback_control
*wbc
,
2423 writepage_t writepage
, void *data
,
2424 void (*flush_fn
)(void *))
2428 int nr_to_write_done
= 0;
2429 struct pagevec pvec
;
2432 pgoff_t end
; /* Inclusive */
2436 pagevec_init(&pvec
, 0);
2437 if (wbc
->range_cyclic
) {
2438 index
= mapping
->writeback_index
; /* Start from prev offset */
2441 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2442 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2445 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2446 tag
= PAGECACHE_TAG_TOWRITE
;
2448 tag
= PAGECACHE_TAG_DIRTY
;
2450 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2451 tag_pages_for_writeback(mapping
, index
, end
);
2452 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
2453 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
2454 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
2458 for (i
= 0; i
< nr_pages
; i
++) {
2459 struct page
*page
= pvec
.pages
[i
];
2462 * At this point we hold neither mapping->tree_lock nor
2463 * lock on the page itself: the page may be truncated or
2464 * invalidated (changing page->mapping to NULL), or even
2465 * swizzled back from swapper_space to tmpfs file
2468 if (tree
->ops
&& tree
->ops
->write_cache_pages_lock_hook
)
2469 tree
->ops
->write_cache_pages_lock_hook(page
);
2473 if (unlikely(page
->mapping
!= mapping
)) {
2478 if (!wbc
->range_cyclic
&& page
->index
> end
) {
2484 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
2485 if (PageWriteback(page
))
2487 wait_on_page_writeback(page
);
2490 if (PageWriteback(page
) ||
2491 !clear_page_dirty_for_io(page
)) {
2496 ret
= (*writepage
)(page
, wbc
, data
);
2498 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
2506 * the filesystem may choose to bump up nr_to_write.
2507 * We have to make sure to honor the new nr_to_write
2510 nr_to_write_done
= wbc
->nr_to_write
<= 0;
2512 pagevec_release(&pvec
);
2515 if (!scanned
&& !done
) {
2517 * We hit the last page and there is more work to be done: wrap
2518 * back to the start of the file
2527 static void flush_epd_write_bio(struct extent_page_data
*epd
)
2531 submit_one_bio(WRITE_SYNC
, epd
->bio
, 0, 0);
2533 submit_one_bio(WRITE
, epd
->bio
, 0, 0);
2538 static noinline
void flush_write_bio(void *data
)
2540 struct extent_page_data
*epd
= data
;
2541 flush_epd_write_bio(epd
);
2544 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2545 get_extent_t
*get_extent
,
2546 struct writeback_control
*wbc
)
2549 struct address_space
*mapping
= page
->mapping
;
2550 struct extent_page_data epd
= {
2553 .get_extent
= get_extent
,
2555 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2557 struct writeback_control wbc_writepages
= {
2558 .sync_mode
= wbc
->sync_mode
,
2560 .range_start
= page_offset(page
) + PAGE_CACHE_SIZE
,
2561 .range_end
= (loff_t
)-1,
2564 ret
= __extent_writepage(page
, wbc
, &epd
);
2566 extent_write_cache_pages(tree
, mapping
, &wbc_writepages
,
2567 __extent_writepage
, &epd
, flush_write_bio
);
2568 flush_epd_write_bio(&epd
);
2572 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
2573 u64 start
, u64 end
, get_extent_t
*get_extent
,
2577 struct address_space
*mapping
= inode
->i_mapping
;
2579 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
2582 struct extent_page_data epd
= {
2585 .get_extent
= get_extent
,
2587 .sync_io
= mode
== WB_SYNC_ALL
,
2589 struct writeback_control wbc_writepages
= {
2591 .nr_to_write
= nr_pages
* 2,
2592 .range_start
= start
,
2593 .range_end
= end
+ 1,
2596 while (start
<= end
) {
2597 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
2598 if (clear_page_dirty_for_io(page
))
2599 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
2601 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2602 tree
->ops
->writepage_end_io_hook(page
, start
,
2603 start
+ PAGE_CACHE_SIZE
- 1,
2607 page_cache_release(page
);
2608 start
+= PAGE_CACHE_SIZE
;
2611 flush_epd_write_bio(&epd
);
2615 int extent_writepages(struct extent_io_tree
*tree
,
2616 struct address_space
*mapping
,
2617 get_extent_t
*get_extent
,
2618 struct writeback_control
*wbc
)
2621 struct extent_page_data epd
= {
2624 .get_extent
= get_extent
,
2626 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2629 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
2630 __extent_writepage
, &epd
,
2632 flush_epd_write_bio(&epd
);
2636 int extent_readpages(struct extent_io_tree
*tree
,
2637 struct address_space
*mapping
,
2638 struct list_head
*pages
, unsigned nr_pages
,
2639 get_extent_t get_extent
)
2641 struct bio
*bio
= NULL
;
2643 unsigned long bio_flags
= 0;
2645 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
2646 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
2648 prefetchw(&page
->flags
);
2649 list_del(&page
->lru
);
2650 if (!add_to_page_cache_lru(page
, mapping
,
2651 page
->index
, GFP_NOFS
)) {
2652 __extent_read_full_page(tree
, page
, get_extent
,
2653 &bio
, 0, &bio_flags
);
2655 page_cache_release(page
);
2657 BUG_ON(!list_empty(pages
));
2659 submit_one_bio(READ
, bio
, 0, bio_flags
);
2664 * basic invalidatepage code, this waits on any locked or writeback
2665 * ranges corresponding to the page, and then deletes any extent state
2666 * records from the tree
2668 int extent_invalidatepage(struct extent_io_tree
*tree
,
2669 struct page
*page
, unsigned long offset
)
2671 struct extent_state
*cached_state
= NULL
;
2672 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
2673 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2674 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
2676 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
2680 lock_extent_bits(tree
, start
, end
, 0, &cached_state
, GFP_NOFS
);
2681 wait_on_page_writeback(page
);
2682 clear_extent_bit(tree
, start
, end
,
2683 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
2684 EXTENT_DO_ACCOUNTING
,
2685 1, 1, &cached_state
, GFP_NOFS
);
2690 * a helper for releasepage, this tests for areas of the page that
2691 * are locked or under IO and drops the related state bits if it is safe
2694 int try_release_extent_state(struct extent_map_tree
*map
,
2695 struct extent_io_tree
*tree
, struct page
*page
,
2698 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2699 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2702 if (test_range_bit(tree
, start
, end
,
2703 EXTENT_IOBITS
, 0, NULL
))
2706 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
2709 * at this point we can safely clear everything except the
2710 * locked bit and the nodatasum bit
2712 ret
= clear_extent_bit(tree
, start
, end
,
2713 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
2716 /* if clear_extent_bit failed for enomem reasons,
2717 * we can't allow the release to continue.
2728 * a helper for releasepage. As long as there are no locked extents
2729 * in the range corresponding to the page, both state records and extent
2730 * map records are removed
2732 int try_release_extent_mapping(struct extent_map_tree
*map
,
2733 struct extent_io_tree
*tree
, struct page
*page
,
2736 struct extent_map
*em
;
2737 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2738 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2740 if ((mask
& __GFP_WAIT
) &&
2741 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
2743 while (start
<= end
) {
2744 len
= end
- start
+ 1;
2745 write_lock(&map
->lock
);
2746 em
= lookup_extent_mapping(map
, start
, len
);
2747 if (IS_ERR_OR_NULL(em
)) {
2748 write_unlock(&map
->lock
);
2751 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
2752 em
->start
!= start
) {
2753 write_unlock(&map
->lock
);
2754 free_extent_map(em
);
2757 if (!test_range_bit(tree
, em
->start
,
2758 extent_map_end(em
) - 1,
2759 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
2761 remove_extent_mapping(map
, em
);
2762 /* once for the rb tree */
2763 free_extent_map(em
);
2765 start
= extent_map_end(em
);
2766 write_unlock(&map
->lock
);
2769 free_extent_map(em
);
2772 return try_release_extent_state(map
, tree
, page
, mask
);
2776 * helper function for fiemap, which doesn't want to see any holes.
2777 * This maps until we find something past 'last'
2779 static struct extent_map
*get_extent_skip_holes(struct inode
*inode
,
2782 get_extent_t
*get_extent
)
2784 u64 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
2785 struct extent_map
*em
;
2792 len
= last
- offset
;
2795 len
= (len
+ sectorsize
- 1) & ~(sectorsize
- 1);
2796 em
= get_extent(inode
, NULL
, 0, offset
, len
, 0);
2797 if (IS_ERR_OR_NULL(em
))
2800 /* if this isn't a hole return it */
2801 if (!test_bit(EXTENT_FLAG_VACANCY
, &em
->flags
) &&
2802 em
->block_start
!= EXTENT_MAP_HOLE
) {
2806 /* this is a hole, advance to the next extent */
2807 offset
= extent_map_end(em
);
2808 free_extent_map(em
);
2815 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
2816 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
2820 u64 max
= start
+ len
;
2824 u64 last_for_get_extent
= 0;
2826 u64 isize
= i_size_read(inode
);
2827 struct btrfs_key found_key
;
2828 struct extent_map
*em
= NULL
;
2829 struct extent_state
*cached_state
= NULL
;
2830 struct btrfs_path
*path
;
2831 struct btrfs_file_extent_item
*item
;
2836 unsigned long emflags
;
2841 path
= btrfs_alloc_path();
2844 path
->leave_spinning
= 1;
2847 * lookup the last file extent. We're not using i_size here
2848 * because there might be preallocation past i_size
2850 ret
= btrfs_lookup_file_extent(NULL
, BTRFS_I(inode
)->root
,
2851 path
, btrfs_ino(inode
), -1, 0);
2853 btrfs_free_path(path
);
2858 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2859 struct btrfs_file_extent_item
);
2860 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, path
->slots
[0]);
2861 found_type
= btrfs_key_type(&found_key
);
2863 /* No extents, but there might be delalloc bits */
2864 if (found_key
.objectid
!= btrfs_ino(inode
) ||
2865 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2866 /* have to trust i_size as the end */
2868 last_for_get_extent
= isize
;
2871 * remember the start of the last extent. There are a
2872 * bunch of different factors that go into the length of the
2873 * extent, so its much less complex to remember where it started
2875 last
= found_key
.offset
;
2876 last_for_get_extent
= last
+ 1;
2878 btrfs_free_path(path
);
2881 * we might have some extents allocated but more delalloc past those
2882 * extents. so, we trust isize unless the start of the last extent is
2887 last_for_get_extent
= isize
;
2890 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
2891 &cached_state
, GFP_NOFS
);
2893 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
,
2903 u64 offset_in_extent
;
2905 /* break if the extent we found is outside the range */
2906 if (em
->start
>= max
|| extent_map_end(em
) < off
)
2910 * get_extent may return an extent that starts before our
2911 * requested range. We have to make sure the ranges
2912 * we return to fiemap always move forward and don't
2913 * overlap, so adjust the offsets here
2915 em_start
= max(em
->start
, off
);
2918 * record the offset from the start of the extent
2919 * for adjusting the disk offset below
2921 offset_in_extent
= em_start
- em
->start
;
2922 em_end
= extent_map_end(em
);
2923 em_len
= em_end
- em_start
;
2924 emflags
= em
->flags
;
2929 * bump off for our next call to get_extent
2931 off
= extent_map_end(em
);
2935 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
2937 flags
|= FIEMAP_EXTENT_LAST
;
2938 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
2939 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
2940 FIEMAP_EXTENT_NOT_ALIGNED
);
2941 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
2942 flags
|= (FIEMAP_EXTENT_DELALLOC
|
2943 FIEMAP_EXTENT_UNKNOWN
);
2945 disko
= em
->block_start
+ offset_in_extent
;
2947 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
2948 flags
|= FIEMAP_EXTENT_ENCODED
;
2950 free_extent_map(em
);
2952 if ((em_start
>= last
) || em_len
== (u64
)-1 ||
2953 (last
== (u64
)-1 && isize
<= em_end
)) {
2954 flags
|= FIEMAP_EXTENT_LAST
;
2958 /* now scan forward to see if this is really the last extent. */
2959 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
,
2966 flags
|= FIEMAP_EXTENT_LAST
;
2969 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
2975 free_extent_map(em
);
2977 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
2978 &cached_state
, GFP_NOFS
);
2982 static inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
2986 struct address_space
*mapping
;
2989 return eb
->first_page
;
2990 i
+= eb
->start
>> PAGE_CACHE_SHIFT
;
2991 mapping
= eb
->first_page
->mapping
;
2996 * extent_buffer_page is only called after pinning the page
2997 * by increasing the reference count. So we know the page must
2998 * be in the radix tree.
3001 p
= radix_tree_lookup(&mapping
->page_tree
, i
);
3007 static inline unsigned long num_extent_pages(u64 start
, u64 len
)
3009 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
3010 (start
>> PAGE_CACHE_SHIFT
);
3013 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3018 struct extent_buffer
*eb
= NULL
;
3020 unsigned long flags
;
3023 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
3028 rwlock_init(&eb
->lock
);
3029 atomic_set(&eb
->write_locks
, 0);
3030 atomic_set(&eb
->read_locks
, 0);
3031 atomic_set(&eb
->blocking_readers
, 0);
3032 atomic_set(&eb
->blocking_writers
, 0);
3033 atomic_set(&eb
->spinning_readers
, 0);
3034 atomic_set(&eb
->spinning_writers
, 0);
3035 init_waitqueue_head(&eb
->write_lock_wq
);
3036 init_waitqueue_head(&eb
->read_lock_wq
);
3039 spin_lock_irqsave(&leak_lock
, flags
);
3040 list_add(&eb
->leak_list
, &buffers
);
3041 spin_unlock_irqrestore(&leak_lock
, flags
);
3043 atomic_set(&eb
->refs
, 1);
3048 static void __free_extent_buffer(struct extent_buffer
*eb
)
3051 unsigned long flags
;
3052 spin_lock_irqsave(&leak_lock
, flags
);
3053 list_del(&eb
->leak_list
);
3054 spin_unlock_irqrestore(&leak_lock
, flags
);
3056 kmem_cache_free(extent_buffer_cache
, eb
);
3060 * Helper for releasing extent buffer page.
3062 static void btrfs_release_extent_buffer_page(struct extent_buffer
*eb
,
3063 unsigned long start_idx
)
3065 unsigned long index
;
3068 if (!eb
->first_page
)
3071 index
= num_extent_pages(eb
->start
, eb
->len
);
3072 if (start_idx
>= index
)
3077 page
= extent_buffer_page(eb
, index
);
3079 page_cache_release(page
);
3080 } while (index
!= start_idx
);
3084 * Helper for releasing the extent buffer.
3086 static inline void btrfs_release_extent_buffer(struct extent_buffer
*eb
)
3088 btrfs_release_extent_buffer_page(eb
, 0);
3089 __free_extent_buffer(eb
);
3092 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
3093 u64 start
, unsigned long len
,
3096 unsigned long num_pages
= num_extent_pages(start
, len
);
3098 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
3099 struct extent_buffer
*eb
;
3100 struct extent_buffer
*exists
= NULL
;
3102 struct address_space
*mapping
= tree
->mapping
;
3107 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3108 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
3110 mark_page_accessed(eb
->first_page
);
3115 eb
= __alloc_extent_buffer(tree
, start
, len
, GFP_NOFS
);
3120 eb
->first_page
= page0
;
3123 page_cache_get(page0
);
3124 mark_page_accessed(page0
);
3125 set_page_extent_mapped(page0
);
3126 set_page_extent_head(page0
, len
);
3127 uptodate
= PageUptodate(page0
);
3131 for (; i
< num_pages
; i
++, index
++) {
3132 p
= find_or_create_page(mapping
, index
, GFP_NOFS
);
3137 set_page_extent_mapped(p
);
3138 mark_page_accessed(p
);
3141 set_page_extent_head(p
, len
);
3143 set_page_private(p
, EXTENT_PAGE_PRIVATE
);
3145 if (!PageUptodate(p
))
3149 * see below about how we avoid a nasty race with release page
3150 * and why we unlock later
3156 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3158 ret
= radix_tree_preload(GFP_NOFS
& ~__GFP_HIGHMEM
);
3162 spin_lock(&tree
->buffer_lock
);
3163 ret
= radix_tree_insert(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
, eb
);
3164 if (ret
== -EEXIST
) {
3165 exists
= radix_tree_lookup(&tree
->buffer
,
3166 start
>> PAGE_CACHE_SHIFT
);
3167 /* add one reference for the caller */
3168 atomic_inc(&exists
->refs
);
3169 spin_unlock(&tree
->buffer_lock
);
3170 radix_tree_preload_end();
3173 /* add one reference for the tree */
3174 atomic_inc(&eb
->refs
);
3175 spin_unlock(&tree
->buffer_lock
);
3176 radix_tree_preload_end();
3179 * there is a race where release page may have
3180 * tried to find this extent buffer in the radix
3181 * but failed. It will tell the VM it is safe to
3182 * reclaim the, and it will clear the page private bit.
3183 * We must make sure to set the page private bit properly
3184 * after the extent buffer is in the radix tree so
3185 * it doesn't get lost
3187 set_page_extent_mapped(eb
->first_page
);
3188 set_page_extent_head(eb
->first_page
, eb
->len
);
3190 unlock_page(eb
->first_page
);
3194 if (eb
->first_page
&& !page0
)
3195 unlock_page(eb
->first_page
);
3197 if (!atomic_dec_and_test(&eb
->refs
))
3199 btrfs_release_extent_buffer(eb
);
3203 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
3204 u64 start
, unsigned long len
)
3206 struct extent_buffer
*eb
;
3209 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3210 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
3212 mark_page_accessed(eb
->first_page
);
3220 void free_extent_buffer(struct extent_buffer
*eb
)
3225 if (!atomic_dec_and_test(&eb
->refs
))
3231 int clear_extent_buffer_dirty(struct extent_io_tree
*tree
,
3232 struct extent_buffer
*eb
)
3235 unsigned long num_pages
;
3238 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3240 for (i
= 0; i
< num_pages
; i
++) {
3241 page
= extent_buffer_page(eb
, i
);
3242 if (!PageDirty(page
))
3246 WARN_ON(!PagePrivate(page
));
3248 set_page_extent_mapped(page
);
3250 set_page_extent_head(page
, eb
->len
);
3252 clear_page_dirty_for_io(page
);
3253 spin_lock_irq(&page
->mapping
->tree_lock
);
3254 if (!PageDirty(page
)) {
3255 radix_tree_tag_clear(&page
->mapping
->page_tree
,
3257 PAGECACHE_TAG_DIRTY
);
3259 spin_unlock_irq(&page
->mapping
->tree_lock
);
3265 int set_extent_buffer_dirty(struct extent_io_tree
*tree
,
3266 struct extent_buffer
*eb
)
3269 unsigned long num_pages
;
3272 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
3273 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3274 for (i
= 0; i
< num_pages
; i
++)
3275 __set_page_dirty_nobuffers(extent_buffer_page(eb
, i
));
3279 static int __eb_straddles_pages(u64 start
, u64 len
)
3281 if (len
< PAGE_CACHE_SIZE
)
3283 if (start
& (PAGE_CACHE_SIZE
- 1))
3285 if ((start
+ len
) & (PAGE_CACHE_SIZE
- 1))
3290 static int eb_straddles_pages(struct extent_buffer
*eb
)
3292 return __eb_straddles_pages(eb
->start
, eb
->len
);
3295 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3296 struct extent_buffer
*eb
,
3297 struct extent_state
**cached_state
)
3301 unsigned long num_pages
;
3303 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3304 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3306 if (eb_straddles_pages(eb
)) {
3307 clear_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3308 cached_state
, GFP_NOFS
);
3310 for (i
= 0; i
< num_pages
; i
++) {
3311 page
= extent_buffer_page(eb
, i
);
3313 ClearPageUptodate(page
);
3318 int set_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3319 struct extent_buffer
*eb
)
3323 unsigned long num_pages
;
3325 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3327 if (eb_straddles_pages(eb
)) {
3328 set_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3331 for (i
= 0; i
< num_pages
; i
++) {
3332 page
= extent_buffer_page(eb
, i
);
3333 if ((i
== 0 && (eb
->start
& (PAGE_CACHE_SIZE
- 1))) ||
3334 ((i
== num_pages
- 1) &&
3335 ((eb
->start
+ eb
->len
) & (PAGE_CACHE_SIZE
- 1)))) {
3336 check_page_uptodate(tree
, page
);
3339 SetPageUptodate(page
);
3344 int extent_range_uptodate(struct extent_io_tree
*tree
,
3349 int pg_uptodate
= 1;
3351 unsigned long index
;
3353 if (__eb_straddles_pages(start
, end
- start
+ 1)) {
3354 ret
= test_range_bit(tree
, start
, end
,
3355 EXTENT_UPTODATE
, 1, NULL
);
3359 while (start
<= end
) {
3360 index
= start
>> PAGE_CACHE_SHIFT
;
3361 page
= find_get_page(tree
->mapping
, index
);
3362 uptodate
= PageUptodate(page
);
3363 page_cache_release(page
);
3368 start
+= PAGE_CACHE_SIZE
;
3373 int extent_buffer_uptodate(struct extent_io_tree
*tree
,
3374 struct extent_buffer
*eb
,
3375 struct extent_state
*cached_state
)
3378 unsigned long num_pages
;
3381 int pg_uptodate
= 1;
3383 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3386 if (eb_straddles_pages(eb
)) {
3387 ret
= test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3388 EXTENT_UPTODATE
, 1, cached_state
);
3393 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3394 for (i
= 0; i
< num_pages
; i
++) {
3395 page
= extent_buffer_page(eb
, i
);
3396 if (!PageUptodate(page
)) {
3404 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
3405 struct extent_buffer
*eb
,
3406 u64 start
, int wait
,
3407 get_extent_t
*get_extent
, int mirror_num
)
3410 unsigned long start_i
;
3414 int locked_pages
= 0;
3415 int all_uptodate
= 1;
3416 int inc_all_pages
= 0;
3417 unsigned long num_pages
;
3418 struct bio
*bio
= NULL
;
3419 unsigned long bio_flags
= 0;
3421 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3424 if (eb_straddles_pages(eb
)) {
3425 if (test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3426 EXTENT_UPTODATE
, 1, NULL
)) {
3432 WARN_ON(start
< eb
->start
);
3433 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
3434 (eb
->start
>> PAGE_CACHE_SHIFT
);
3439 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3440 for (i
= start_i
; i
< num_pages
; i
++) {
3441 page
= extent_buffer_page(eb
, i
);
3443 if (!trylock_page(page
))
3449 if (!PageUptodate(page
))
3454 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3458 for (i
= start_i
; i
< num_pages
; i
++) {
3459 page
= extent_buffer_page(eb
, i
);
3461 WARN_ON(!PagePrivate(page
));
3463 set_page_extent_mapped(page
);
3465 set_page_extent_head(page
, eb
->len
);
3468 page_cache_get(page
);
3469 if (!PageUptodate(page
)) {
3472 ClearPageError(page
);
3473 err
= __extent_read_full_page(tree
, page
,
3475 mirror_num
, &bio_flags
);
3484 submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
3489 for (i
= start_i
; i
< num_pages
; i
++) {
3490 page
= extent_buffer_page(eb
, i
);
3491 wait_on_page_locked(page
);
3492 if (!PageUptodate(page
))
3497 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3502 while (locked_pages
> 0) {
3503 page
= extent_buffer_page(eb
, i
);
3511 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
3512 unsigned long start
,
3519 char *dst
= (char *)dstv
;
3520 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3521 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3523 WARN_ON(start
> eb
->len
);
3524 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3526 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3529 page
= extent_buffer_page(eb
, i
);
3531 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3532 kaddr
= page_address(page
);
3533 memcpy(dst
, kaddr
+ offset
, cur
);
3542 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3543 unsigned long min_len
, char **map
,
3544 unsigned long *map_start
,
3545 unsigned long *map_len
)
3547 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
3550 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3551 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3552 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
3559 offset
= start_offset
;
3563 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
3566 if (start
+ min_len
> eb
->len
) {
3567 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
3568 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
3569 eb
->len
, start
, min_len
);
3574 p
= extent_buffer_page(eb
, i
);
3575 kaddr
= page_address(p
);
3576 *map
= kaddr
+ offset
;
3577 *map_len
= PAGE_CACHE_SIZE
- offset
;
3581 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
3582 unsigned long start
,
3589 char *ptr
= (char *)ptrv
;
3590 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3591 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3594 WARN_ON(start
> eb
->len
);
3595 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3597 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3600 page
= extent_buffer_page(eb
, i
);
3602 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3604 kaddr
= page_address(page
);
3605 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
3617 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
3618 unsigned long start
, unsigned long len
)
3624 char *src
= (char *)srcv
;
3625 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3626 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3628 WARN_ON(start
> eb
->len
);
3629 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3631 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3634 page
= extent_buffer_page(eb
, i
);
3635 WARN_ON(!PageUptodate(page
));
3637 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3638 kaddr
= page_address(page
);
3639 memcpy(kaddr
+ offset
, src
, cur
);
3648 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
3649 unsigned long start
, unsigned long len
)
3655 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3656 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3658 WARN_ON(start
> eb
->len
);
3659 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3661 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3664 page
= extent_buffer_page(eb
, i
);
3665 WARN_ON(!PageUptodate(page
));
3667 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3668 kaddr
= page_address(page
);
3669 memset(kaddr
+ offset
, c
, cur
);
3677 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
3678 unsigned long dst_offset
, unsigned long src_offset
,
3681 u64 dst_len
= dst
->len
;
3686 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3687 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3689 WARN_ON(src
->len
!= dst_len
);
3691 offset
= (start_offset
+ dst_offset
) &
3692 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3695 page
= extent_buffer_page(dst
, i
);
3696 WARN_ON(!PageUptodate(page
));
3698 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
3700 kaddr
= page_address(page
);
3701 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
3710 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
3711 unsigned long dst_off
, unsigned long src_off
,
3714 char *dst_kaddr
= page_address(dst_page
);
3715 if (dst_page
== src_page
) {
3716 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
3718 char *src_kaddr
= page_address(src_page
);
3719 char *p
= dst_kaddr
+ dst_off
+ len
;
3720 char *s
= src_kaddr
+ src_off
+ len
;
3727 static inline bool areas_overlap(unsigned long src
, unsigned long dst
, unsigned long len
)
3729 unsigned long distance
= (src
> dst
) ? src
- dst
: dst
- src
;
3730 return distance
< len
;
3733 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
3734 unsigned long dst_off
, unsigned long src_off
,
3737 char *dst_kaddr
= page_address(dst_page
);
3740 if (dst_page
!= src_page
) {
3741 src_kaddr
= page_address(src_page
);
3743 src_kaddr
= dst_kaddr
;
3744 BUG_ON(areas_overlap(src_off
, dst_off
, len
));
3747 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
3750 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3751 unsigned long src_offset
, unsigned long len
)
3754 size_t dst_off_in_page
;
3755 size_t src_off_in_page
;
3756 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3757 unsigned long dst_i
;
3758 unsigned long src_i
;
3760 if (src_offset
+ len
> dst
->len
) {
3761 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3762 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
3765 if (dst_offset
+ len
> dst
->len
) {
3766 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3767 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
3772 dst_off_in_page
= (start_offset
+ dst_offset
) &
3773 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3774 src_off_in_page
= (start_offset
+ src_offset
) &
3775 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3777 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3778 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
3780 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
3782 cur
= min_t(unsigned long, cur
,
3783 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
3785 copy_pages(extent_buffer_page(dst
, dst_i
),
3786 extent_buffer_page(dst
, src_i
),
3787 dst_off_in_page
, src_off_in_page
, cur
);
3795 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3796 unsigned long src_offset
, unsigned long len
)
3799 size_t dst_off_in_page
;
3800 size_t src_off_in_page
;
3801 unsigned long dst_end
= dst_offset
+ len
- 1;
3802 unsigned long src_end
= src_offset
+ len
- 1;
3803 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3804 unsigned long dst_i
;
3805 unsigned long src_i
;
3807 if (src_offset
+ len
> dst
->len
) {
3808 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3809 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
3812 if (dst_offset
+ len
> dst
->len
) {
3813 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3814 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
3817 if (!areas_overlap(src_offset
, dst_offset
, len
)) {
3818 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
3822 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
3823 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
3825 dst_off_in_page
= (start_offset
+ dst_end
) &
3826 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3827 src_off_in_page
= (start_offset
+ src_end
) &
3828 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3830 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
3831 cur
= min(cur
, dst_off_in_page
+ 1);
3832 move_pages(extent_buffer_page(dst
, dst_i
),
3833 extent_buffer_page(dst
, src_i
),
3834 dst_off_in_page
- cur
+ 1,
3835 src_off_in_page
- cur
+ 1, cur
);
3843 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head
*head
)
3845 struct extent_buffer
*eb
=
3846 container_of(head
, struct extent_buffer
, rcu_head
);
3848 btrfs_release_extent_buffer(eb
);
3851 int try_release_extent_buffer(struct extent_io_tree
*tree
, struct page
*page
)
3853 u64 start
= page_offset(page
);
3854 struct extent_buffer
*eb
;
3857 spin_lock(&tree
->buffer_lock
);
3858 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3860 spin_unlock(&tree
->buffer_lock
);
3864 if (test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3870 * set @eb->refs to 0 if it is already 1, and then release the @eb.
3873 if (atomic_cmpxchg(&eb
->refs
, 1, 0) != 1) {
3878 radix_tree_delete(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3880 spin_unlock(&tree
->buffer_lock
);
3882 /* at this point we can safely release the extent buffer */
3883 if (atomic_read(&eb
->refs
) == 0)
3884 call_rcu(&eb
->rcu_head
, btrfs_release_extent_buffer_rcu
);