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/cleancache.h>
14 #include "extent_io.h"
15 #include "extent_map.h"
18 #include "btrfs_inode.h"
20 static struct kmem_cache
*extent_state_cache
;
21 static struct kmem_cache
*extent_buffer_cache
;
23 static LIST_HEAD(buffers
);
24 static LIST_HEAD(states
);
28 static DEFINE_SPINLOCK(leak_lock
);
31 #define BUFFER_LRU_MAX 64
36 struct rb_node rb_node
;
39 struct extent_page_data
{
41 struct extent_io_tree
*tree
;
42 get_extent_t
*get_extent
;
44 /* tells writepage not to lock the state bits for this range
45 * it still does the unlocking
47 unsigned int extent_locked
:1;
49 /* tells the submit_bio code to use a WRITE_SYNC */
50 unsigned int sync_io
:1;
53 int __init
extent_io_init(void)
55 extent_state_cache
= kmem_cache_create("extent_state",
56 sizeof(struct extent_state
), 0,
57 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
58 if (!extent_state_cache
)
61 extent_buffer_cache
= kmem_cache_create("extent_buffers",
62 sizeof(struct extent_buffer
), 0,
63 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
64 if (!extent_buffer_cache
)
65 goto free_state_cache
;
69 kmem_cache_destroy(extent_state_cache
);
73 void extent_io_exit(void)
75 struct extent_state
*state
;
76 struct extent_buffer
*eb
;
78 while (!list_empty(&states
)) {
79 state
= list_entry(states
.next
, struct extent_state
, leak_list
);
80 printk(KERN_ERR
"btrfs state leak: start %llu end %llu "
81 "state %lu in tree %p refs %d\n",
82 (unsigned long long)state
->start
,
83 (unsigned long long)state
->end
,
84 state
->state
, state
->tree
, atomic_read(&state
->refs
));
85 list_del(&state
->leak_list
);
86 kmem_cache_free(extent_state_cache
, state
);
90 while (!list_empty(&buffers
)) {
91 eb
= list_entry(buffers
.next
, struct extent_buffer
, leak_list
);
92 printk(KERN_ERR
"btrfs buffer leak start %llu len %lu "
93 "refs %d\n", (unsigned long long)eb
->start
,
94 eb
->len
, atomic_read(&eb
->refs
));
95 list_del(&eb
->leak_list
);
96 kmem_cache_free(extent_buffer_cache
, eb
);
98 if (extent_state_cache
)
99 kmem_cache_destroy(extent_state_cache
);
100 if (extent_buffer_cache
)
101 kmem_cache_destroy(extent_buffer_cache
);
104 void extent_io_tree_init(struct extent_io_tree
*tree
,
105 struct address_space
*mapping
, gfp_t mask
)
107 tree
->state
= RB_ROOT
;
108 INIT_RADIX_TREE(&tree
->buffer
, GFP_ATOMIC
);
110 tree
->dirty_bytes
= 0;
111 spin_lock_init(&tree
->lock
);
112 spin_lock_init(&tree
->buffer_lock
);
113 tree
->mapping
= mapping
;
116 static struct extent_state
*alloc_extent_state(gfp_t mask
)
118 struct extent_state
*state
;
123 state
= kmem_cache_alloc(extent_state_cache
, mask
);
130 spin_lock_irqsave(&leak_lock
, flags
);
131 list_add(&state
->leak_list
, &states
);
132 spin_unlock_irqrestore(&leak_lock
, flags
);
134 atomic_set(&state
->refs
, 1);
135 init_waitqueue_head(&state
->wq
);
139 void free_extent_state(struct extent_state
*state
)
143 if (atomic_dec_and_test(&state
->refs
)) {
147 WARN_ON(state
->tree
);
149 spin_lock_irqsave(&leak_lock
, flags
);
150 list_del(&state
->leak_list
);
151 spin_unlock_irqrestore(&leak_lock
, flags
);
153 kmem_cache_free(extent_state_cache
, state
);
157 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
158 struct rb_node
*node
)
160 struct rb_node
**p
= &root
->rb_node
;
161 struct rb_node
*parent
= NULL
;
162 struct tree_entry
*entry
;
166 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
168 if (offset
< entry
->start
)
170 else if (offset
> entry
->end
)
176 entry
= rb_entry(node
, struct tree_entry
, rb_node
);
177 rb_link_node(node
, parent
, p
);
178 rb_insert_color(node
, root
);
182 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
183 struct rb_node
**prev_ret
,
184 struct rb_node
**next_ret
)
186 struct rb_root
*root
= &tree
->state
;
187 struct rb_node
*n
= root
->rb_node
;
188 struct rb_node
*prev
= NULL
;
189 struct rb_node
*orig_prev
= NULL
;
190 struct tree_entry
*entry
;
191 struct tree_entry
*prev_entry
= NULL
;
194 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
198 if (offset
< entry
->start
)
200 else if (offset
> entry
->end
)
208 while (prev
&& offset
> prev_entry
->end
) {
209 prev
= rb_next(prev
);
210 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
217 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
218 while (prev
&& offset
< prev_entry
->start
) {
219 prev
= rb_prev(prev
);
220 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
227 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
230 struct rb_node
*prev
= NULL
;
233 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
239 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
240 struct extent_state
*other
)
242 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
243 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
248 * utility function to look for merge candidates inside a given range.
249 * Any extents with matching state are merged together into a single
250 * extent in the tree. Extents with EXTENT_IO in their state field
251 * are not merged because the end_io handlers need to be able to do
252 * operations on them without sleeping (or doing allocations/splits).
254 * This should be called with the tree lock held.
256 static int merge_state(struct extent_io_tree
*tree
,
257 struct extent_state
*state
)
259 struct extent_state
*other
;
260 struct rb_node
*other_node
;
262 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
265 other_node
= rb_prev(&state
->rb_node
);
267 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
268 if (other
->end
== state
->start
- 1 &&
269 other
->state
== state
->state
) {
270 merge_cb(tree
, state
, other
);
271 state
->start
= other
->start
;
273 rb_erase(&other
->rb_node
, &tree
->state
);
274 free_extent_state(other
);
277 other_node
= rb_next(&state
->rb_node
);
279 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
280 if (other
->start
== state
->end
+ 1 &&
281 other
->state
== state
->state
) {
282 merge_cb(tree
, state
, other
);
283 other
->start
= state
->start
;
285 rb_erase(&state
->rb_node
, &tree
->state
);
286 free_extent_state(state
);
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
);
353 free_extent_state(state
);
357 merge_state(tree
, state
);
361 static int split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
364 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
365 return tree
->ops
->split_extent_hook(tree
->mapping
->host
,
371 * split a given extent state struct in two, inserting the preallocated
372 * struct 'prealloc' as the newly created second half. 'split' indicates an
373 * offset inside 'orig' where it should be split.
376 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
377 * are two extent state structs in the tree:
378 * prealloc: [orig->start, split - 1]
379 * orig: [ split, orig->end ]
381 * The tree locks are not taken by this function. They need to be held
384 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
385 struct extent_state
*prealloc
, u64 split
)
387 struct rb_node
*node
;
389 split_cb(tree
, orig
, split
);
391 prealloc
->start
= orig
->start
;
392 prealloc
->end
= split
- 1;
393 prealloc
->state
= orig
->state
;
396 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
398 free_extent_state(prealloc
);
401 prealloc
->tree
= tree
;
406 * utility function to clear some bits in an extent state struct.
407 * it will optionally wake up any one waiting on this state (wake == 1), or
408 * forcibly remove the state from the tree (delete == 1).
410 * If no bits are set on the state struct after clearing things, the
411 * struct is freed and removed from the tree
413 static int clear_state_bit(struct extent_io_tree
*tree
,
414 struct extent_state
*state
,
417 int bits_to_clear
= *bits
& ~EXTENT_CTLBITS
;
418 int ret
= state
->state
& bits_to_clear
;
420 if ((bits_to_clear
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
421 u64 range
= state
->end
- state
->start
+ 1;
422 WARN_ON(range
> tree
->dirty_bytes
);
423 tree
->dirty_bytes
-= range
;
425 clear_state_cb(tree
, state
, bits
);
426 state
->state
&= ~bits_to_clear
;
429 if (state
->state
== 0) {
431 rb_erase(&state
->rb_node
, &tree
->state
);
433 free_extent_state(state
);
438 merge_state(tree
, state
);
444 * clear some bits on a range in the tree. This may require splitting
445 * or inserting elements in the tree, so the gfp mask is used to
446 * indicate which allocations or sleeping are allowed.
448 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
449 * the given range from the tree regardless of state (ie for truncate).
451 * the range [start, end] is inclusive.
453 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
454 * bits were already set, or zero if none of the bits were already set.
456 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
457 int bits
, int wake
, int delete,
458 struct extent_state
**cached_state
,
461 struct extent_state
*state
;
462 struct extent_state
*cached
;
463 struct extent_state
*prealloc
= NULL
;
464 struct rb_node
*next_node
;
465 struct rb_node
*node
;
472 bits
|= ~EXTENT_CTLBITS
;
473 bits
|= EXTENT_FIRST_DELALLOC
;
475 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
478 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
479 prealloc
= alloc_extent_state(mask
);
484 spin_lock(&tree
->lock
);
486 cached
= *cached_state
;
489 *cached_state
= NULL
;
493 if (cached
&& cached
->tree
&& cached
->start
== start
) {
495 atomic_dec(&cached
->refs
);
500 free_extent_state(cached
);
503 * this search will find the extents that end after
506 node
= tree_search(tree
, start
);
509 state
= rb_entry(node
, struct extent_state
, rb_node
);
511 if (state
->start
> end
)
513 WARN_ON(state
->end
< start
);
514 last_end
= state
->end
;
517 * | ---- desired range ---- |
519 * | ------------- state -------------- |
521 * We need to split the extent we found, and may flip
522 * bits on second half.
524 * If the extent we found extends past our range, we
525 * just split and search again. It'll get split again
526 * the next time though.
528 * If the extent we found is inside our range, we clear
529 * the desired bit on it.
532 if (state
->start
< start
) {
534 prealloc
= alloc_extent_state(GFP_ATOMIC
);
535 err
= split_state(tree
, state
, prealloc
, start
);
536 BUG_ON(err
== -EEXIST
);
540 if (state
->end
<= end
) {
541 set
|= clear_state_bit(tree
, state
, &bits
, wake
);
542 if (last_end
== (u64
)-1)
544 start
= last_end
+ 1;
549 * | ---- desired range ---- |
551 * We need to split the extent, and clear the bit
554 if (state
->start
<= end
&& state
->end
> end
) {
556 prealloc
= alloc_extent_state(GFP_ATOMIC
);
557 err
= split_state(tree
, state
, prealloc
, end
+ 1);
558 BUG_ON(err
== -EEXIST
);
562 set
|= clear_state_bit(tree
, prealloc
, &bits
, wake
);
568 if (state
->end
< end
&& prealloc
&& !need_resched())
569 next_node
= rb_next(&state
->rb_node
);
573 set
|= clear_state_bit(tree
, state
, &bits
, wake
);
574 if (last_end
== (u64
)-1)
576 start
= last_end
+ 1;
577 if (start
<= end
&& next_node
) {
578 state
= rb_entry(next_node
, struct extent_state
,
580 if (state
->start
== start
)
586 spin_unlock(&tree
->lock
);
588 free_extent_state(prealloc
);
595 spin_unlock(&tree
->lock
);
596 if (mask
& __GFP_WAIT
)
601 static int wait_on_state(struct extent_io_tree
*tree
,
602 struct extent_state
*state
)
603 __releases(tree
->lock
)
604 __acquires(tree
->lock
)
607 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
608 spin_unlock(&tree
->lock
);
610 spin_lock(&tree
->lock
);
611 finish_wait(&state
->wq
, &wait
);
616 * waits for one or more bits to clear on a range in the state tree.
617 * The range [start, end] is inclusive.
618 * The tree lock is taken by this function
620 int wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
622 struct extent_state
*state
;
623 struct rb_node
*node
;
625 spin_lock(&tree
->lock
);
629 * this search will find all the extents that end after
632 node
= tree_search(tree
, start
);
636 state
= rb_entry(node
, struct extent_state
, rb_node
);
638 if (state
->start
> end
)
641 if (state
->state
& bits
) {
642 start
= state
->start
;
643 atomic_inc(&state
->refs
);
644 wait_on_state(tree
, state
);
645 free_extent_state(state
);
648 start
= state
->end
+ 1;
653 if (need_resched()) {
654 spin_unlock(&tree
->lock
);
656 spin_lock(&tree
->lock
);
660 spin_unlock(&tree
->lock
);
664 static int set_state_bits(struct extent_io_tree
*tree
,
665 struct extent_state
*state
,
669 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
671 ret
= set_state_cb(tree
, state
, bits
);
674 if ((bits_to_set
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
675 u64 range
= state
->end
- state
->start
+ 1;
676 tree
->dirty_bytes
+= range
;
678 state
->state
|= bits_to_set
;
683 static void cache_state(struct extent_state
*state
,
684 struct extent_state
**cached_ptr
)
686 if (cached_ptr
&& !(*cached_ptr
)) {
687 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
689 atomic_inc(&state
->refs
);
694 static void uncache_state(struct extent_state
**cached_ptr
)
696 if (cached_ptr
&& (*cached_ptr
)) {
697 struct extent_state
*state
= *cached_ptr
;
699 free_extent_state(state
);
704 * set some bits on a range in the tree. This may require allocations or
705 * sleeping, so the gfp mask is used to indicate what is allowed.
707 * If any of the exclusive bits are set, this will fail with -EEXIST if some
708 * part of the range already has the desired bits set. The start of the
709 * existing range is returned in failed_start in this case.
711 * [start, end] is inclusive This takes the tree lock.
714 int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
715 int bits
, int exclusive_bits
, u64
*failed_start
,
716 struct extent_state
**cached_state
, gfp_t mask
)
718 struct extent_state
*state
;
719 struct extent_state
*prealloc
= NULL
;
720 struct rb_node
*node
;
725 bits
|= EXTENT_FIRST_DELALLOC
;
727 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
728 prealloc
= alloc_extent_state(mask
);
733 spin_lock(&tree
->lock
);
734 if (cached_state
&& *cached_state
) {
735 state
= *cached_state
;
736 if (state
->start
== start
&& state
->tree
) {
737 node
= &state
->rb_node
;
742 * this search will find all the extents that end after
745 node
= tree_search(tree
, start
);
747 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
749 BUG_ON(err
== -EEXIST
);
752 state
= rb_entry(node
, struct extent_state
, rb_node
);
754 last_start
= state
->start
;
755 last_end
= state
->end
;
758 * | ---- desired range ---- |
761 * Just lock what we found and keep going
763 if (state
->start
== start
&& state
->end
<= end
) {
764 struct rb_node
*next_node
;
765 if (state
->state
& exclusive_bits
) {
766 *failed_start
= state
->start
;
771 err
= set_state_bits(tree
, state
, &bits
);
775 cache_state(state
, cached_state
);
776 merge_state(tree
, state
);
777 if (last_end
== (u64
)-1)
780 start
= last_end
+ 1;
781 if (start
< end
&& prealloc
&& !need_resched()) {
782 next_node
= rb_next(node
);
784 state
= rb_entry(next_node
, struct extent_state
,
786 if (state
->start
== start
)
794 * | ---- desired range ---- |
797 * | ------------- state -------------- |
799 * We need to split the extent we found, and may flip bits on
802 * If the extent we found extends past our
803 * range, we just split and search again. It'll get split
804 * again the next time though.
806 * If the extent we found is inside our range, we set the
809 if (state
->start
< start
) {
810 if (state
->state
& exclusive_bits
) {
811 *failed_start
= start
;
815 err
= split_state(tree
, state
, prealloc
, start
);
816 BUG_ON(err
== -EEXIST
);
820 if (state
->end
<= end
) {
821 err
= set_state_bits(tree
, state
, &bits
);
824 cache_state(state
, cached_state
);
825 merge_state(tree
, state
);
826 if (last_end
== (u64
)-1)
828 start
= last_end
+ 1;
833 * | ---- desired range ---- |
834 * | state | or | state |
836 * There's a hole, we need to insert something in it and
837 * ignore the extent we found.
839 if (state
->start
> start
) {
841 if (end
< last_start
)
844 this_end
= last_start
- 1;
845 err
= insert_state(tree
, prealloc
, start
, this_end
,
847 BUG_ON(err
== -EEXIST
);
852 cache_state(prealloc
, cached_state
);
854 start
= this_end
+ 1;
858 * | ---- desired range ---- |
860 * We need to split the extent, and set the bit
863 if (state
->start
<= end
&& state
->end
> end
) {
864 if (state
->state
& exclusive_bits
) {
865 *failed_start
= start
;
869 err
= split_state(tree
, state
, prealloc
, end
+ 1);
870 BUG_ON(err
== -EEXIST
);
872 err
= set_state_bits(tree
, prealloc
, &bits
);
877 cache_state(prealloc
, cached_state
);
878 merge_state(tree
, prealloc
);
886 spin_unlock(&tree
->lock
);
888 free_extent_state(prealloc
);
895 spin_unlock(&tree
->lock
);
896 if (mask
& __GFP_WAIT
)
901 /* wrappers around set/clear extent bit */
902 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
905 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, 0, NULL
,
909 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
910 int bits
, gfp_t mask
)
912 return set_extent_bit(tree
, start
, end
, bits
, 0, NULL
,
916 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
917 int bits
, gfp_t mask
)
919 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
922 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
923 struct extent_state
**cached_state
, gfp_t mask
)
925 return set_extent_bit(tree
, start
, end
,
926 EXTENT_DELALLOC
| EXTENT_DIRTY
| EXTENT_UPTODATE
,
927 0, NULL
, cached_state
, mask
);
930 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
933 return clear_extent_bit(tree
, start
, end
,
934 EXTENT_DIRTY
| EXTENT_DELALLOC
|
935 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, mask
);
938 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
941 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, NULL
,
945 static int clear_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
948 return clear_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, 0,
952 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
953 struct extent_state
**cached_state
, gfp_t mask
)
955 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0,
956 NULL
, cached_state
, mask
);
959 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
960 u64 end
, struct extent_state
**cached_state
,
963 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
967 int wait_on_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
969 return wait_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
);
973 * either insert or lock state struct between start and end use mask to tell
974 * us if waiting is desired.
976 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
977 int bits
, struct extent_state
**cached_state
, gfp_t mask
)
982 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
983 EXTENT_LOCKED
, &failed_start
,
985 if (err
== -EEXIST
&& (mask
& __GFP_WAIT
)) {
986 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
987 start
= failed_start
;
991 WARN_ON(start
> end
);
996 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
998 return lock_extent_bits(tree
, start
, end
, 0, NULL
, mask
);
1001 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1007 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1008 &failed_start
, NULL
, mask
);
1009 if (err
== -EEXIST
) {
1010 if (failed_start
> start
)
1011 clear_extent_bit(tree
, start
, failed_start
- 1,
1012 EXTENT_LOCKED
, 1, 0, NULL
, mask
);
1018 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1019 struct extent_state
**cached
, gfp_t mask
)
1021 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1025 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
1027 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1032 * helper function to set pages and extents in the tree dirty
1034 int set_range_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1036 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1037 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1040 while (index
<= end_index
) {
1041 page
= find_get_page(tree
->mapping
, index
);
1043 __set_page_dirty_nobuffers(page
);
1044 page_cache_release(page
);
1051 * helper function to set both pages and extents in the tree writeback
1053 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1055 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1056 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1059 while (index
<= end_index
) {
1060 page
= find_get_page(tree
->mapping
, index
);
1062 set_page_writeback(page
);
1063 page_cache_release(page
);
1070 * find the first offset in the io tree with 'bits' set. zero is
1071 * returned if we find something, and *start_ret and *end_ret are
1072 * set to reflect the state struct that was found.
1074 * If nothing was found, 1 is returned, < 0 on error
1076 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1077 u64
*start_ret
, u64
*end_ret
, int bits
)
1079 struct rb_node
*node
;
1080 struct extent_state
*state
;
1083 spin_lock(&tree
->lock
);
1085 * this search will find all the extents that end after
1088 node
= tree_search(tree
, start
);
1093 state
= rb_entry(node
, struct extent_state
, rb_node
);
1094 if (state
->end
>= start
&& (state
->state
& bits
)) {
1095 *start_ret
= state
->start
;
1096 *end_ret
= state
->end
;
1100 node
= rb_next(node
);
1105 spin_unlock(&tree
->lock
);
1109 /* find the first state struct with 'bits' set after 'start', and
1110 * return it. tree->lock must be held. NULL will returned if
1111 * nothing was found after 'start'
1113 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1114 u64 start
, int bits
)
1116 struct rb_node
*node
;
1117 struct extent_state
*state
;
1120 * this search will find all the extents that end after
1123 node
= tree_search(tree
, start
);
1128 state
= rb_entry(node
, struct extent_state
, rb_node
);
1129 if (state
->end
>= start
&& (state
->state
& bits
))
1132 node
= rb_next(node
);
1141 * find a contiguous range of bytes in the file marked as delalloc, not
1142 * more than 'max_bytes'. start and end are used to return the range,
1144 * 1 is returned if we find something, 0 if nothing was in the tree
1146 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1147 u64
*start
, u64
*end
, u64 max_bytes
,
1148 struct extent_state
**cached_state
)
1150 struct rb_node
*node
;
1151 struct extent_state
*state
;
1152 u64 cur_start
= *start
;
1154 u64 total_bytes
= 0;
1156 spin_lock(&tree
->lock
);
1159 * this search will find all the extents that end after
1162 node
= tree_search(tree
, cur_start
);
1170 state
= rb_entry(node
, struct extent_state
, rb_node
);
1171 if (found
&& (state
->start
!= cur_start
||
1172 (state
->state
& EXTENT_BOUNDARY
))) {
1175 if (!(state
->state
& EXTENT_DELALLOC
)) {
1181 *start
= state
->start
;
1182 *cached_state
= state
;
1183 atomic_inc(&state
->refs
);
1187 cur_start
= state
->end
+ 1;
1188 node
= rb_next(node
);
1191 total_bytes
+= state
->end
- state
->start
+ 1;
1192 if (total_bytes
>= max_bytes
)
1196 spin_unlock(&tree
->lock
);
1200 static noinline
int __unlock_for_delalloc(struct inode
*inode
,
1201 struct page
*locked_page
,
1205 struct page
*pages
[16];
1206 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1207 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1208 unsigned long nr_pages
= end_index
- index
+ 1;
1211 if (index
== locked_page
->index
&& end_index
== index
)
1214 while (nr_pages
> 0) {
1215 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1216 min_t(unsigned long, nr_pages
,
1217 ARRAY_SIZE(pages
)), pages
);
1218 for (i
= 0; i
< ret
; i
++) {
1219 if (pages
[i
] != locked_page
)
1220 unlock_page(pages
[i
]);
1221 page_cache_release(pages
[i
]);
1230 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1231 struct page
*locked_page
,
1235 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1236 unsigned long start_index
= index
;
1237 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1238 unsigned long pages_locked
= 0;
1239 struct page
*pages
[16];
1240 unsigned long nrpages
;
1244 /* the caller is responsible for locking the start index */
1245 if (index
== locked_page
->index
&& index
== end_index
)
1248 /* skip the page at the start index */
1249 nrpages
= end_index
- index
+ 1;
1250 while (nrpages
> 0) {
1251 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1252 min_t(unsigned long,
1253 nrpages
, ARRAY_SIZE(pages
)), pages
);
1258 /* now we have an array of pages, lock them all */
1259 for (i
= 0; i
< ret
; i
++) {
1261 * the caller is taking responsibility for
1264 if (pages
[i
] != locked_page
) {
1265 lock_page(pages
[i
]);
1266 if (!PageDirty(pages
[i
]) ||
1267 pages
[i
]->mapping
!= inode
->i_mapping
) {
1269 unlock_page(pages
[i
]);
1270 page_cache_release(pages
[i
]);
1274 page_cache_release(pages
[i
]);
1283 if (ret
&& pages_locked
) {
1284 __unlock_for_delalloc(inode
, locked_page
,
1286 ((u64
)(start_index
+ pages_locked
- 1)) <<
1293 * find a contiguous range of bytes in the file marked as delalloc, not
1294 * more than 'max_bytes'. start and end are used to return the range,
1296 * 1 is returned if we find something, 0 if nothing was in the tree
1298 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1299 struct extent_io_tree
*tree
,
1300 struct page
*locked_page
,
1301 u64
*start
, u64
*end
,
1307 struct extent_state
*cached_state
= NULL
;
1312 /* step one, find a bunch of delalloc bytes starting at start */
1313 delalloc_start
= *start
;
1315 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1316 max_bytes
, &cached_state
);
1317 if (!found
|| delalloc_end
<= *start
) {
1318 *start
= delalloc_start
;
1319 *end
= delalloc_end
;
1320 free_extent_state(cached_state
);
1325 * start comes from the offset of locked_page. We have to lock
1326 * pages in order, so we can't process delalloc bytes before
1329 if (delalloc_start
< *start
)
1330 delalloc_start
= *start
;
1333 * make sure to limit the number of pages we try to lock down
1336 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1337 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1339 /* step two, lock all the pages after the page that has start */
1340 ret
= lock_delalloc_pages(inode
, locked_page
,
1341 delalloc_start
, delalloc_end
);
1342 if (ret
== -EAGAIN
) {
1343 /* some of the pages are gone, lets avoid looping by
1344 * shortening the size of the delalloc range we're searching
1346 free_extent_state(cached_state
);
1348 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1349 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1359 /* step three, lock the state bits for the whole range */
1360 lock_extent_bits(tree
, delalloc_start
, delalloc_end
,
1361 0, &cached_state
, GFP_NOFS
);
1363 /* then test to make sure it is all still delalloc */
1364 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1365 EXTENT_DELALLOC
, 1, cached_state
);
1367 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1368 &cached_state
, GFP_NOFS
);
1369 __unlock_for_delalloc(inode
, locked_page
,
1370 delalloc_start
, delalloc_end
);
1374 free_extent_state(cached_state
);
1375 *start
= delalloc_start
;
1376 *end
= delalloc_end
;
1381 int extent_clear_unlock_delalloc(struct inode
*inode
,
1382 struct extent_io_tree
*tree
,
1383 u64 start
, u64 end
, struct page
*locked_page
,
1387 struct page
*pages
[16];
1388 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1389 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1390 unsigned long nr_pages
= end_index
- index
+ 1;
1394 if (op
& EXTENT_CLEAR_UNLOCK
)
1395 clear_bits
|= EXTENT_LOCKED
;
1396 if (op
& EXTENT_CLEAR_DIRTY
)
1397 clear_bits
|= EXTENT_DIRTY
;
1399 if (op
& EXTENT_CLEAR_DELALLOC
)
1400 clear_bits
|= EXTENT_DELALLOC
;
1402 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1403 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1404 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1405 EXTENT_SET_PRIVATE2
)))
1408 while (nr_pages
> 0) {
1409 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1410 min_t(unsigned long,
1411 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1412 for (i
= 0; i
< ret
; i
++) {
1414 if (op
& EXTENT_SET_PRIVATE2
)
1415 SetPagePrivate2(pages
[i
]);
1417 if (pages
[i
] == locked_page
) {
1418 page_cache_release(pages
[i
]);
1421 if (op
& EXTENT_CLEAR_DIRTY
)
1422 clear_page_dirty_for_io(pages
[i
]);
1423 if (op
& EXTENT_SET_WRITEBACK
)
1424 set_page_writeback(pages
[i
]);
1425 if (op
& EXTENT_END_WRITEBACK
)
1426 end_page_writeback(pages
[i
]);
1427 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1428 unlock_page(pages
[i
]);
1429 page_cache_release(pages
[i
]);
1439 * count the number of bytes in the tree that have a given bit(s)
1440 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1441 * cached. The total number found is returned.
1443 u64
count_range_bits(struct extent_io_tree
*tree
,
1444 u64
*start
, u64 search_end
, u64 max_bytes
,
1445 unsigned long bits
, int contig
)
1447 struct rb_node
*node
;
1448 struct extent_state
*state
;
1449 u64 cur_start
= *start
;
1450 u64 total_bytes
= 0;
1454 if (search_end
<= cur_start
) {
1459 spin_lock(&tree
->lock
);
1460 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1461 total_bytes
= tree
->dirty_bytes
;
1465 * this search will find all the extents that end after
1468 node
= tree_search(tree
, cur_start
);
1473 state
= rb_entry(node
, struct extent_state
, rb_node
);
1474 if (state
->start
> search_end
)
1476 if (contig
&& found
&& state
->start
> last
+ 1)
1478 if (state
->end
>= cur_start
&& (state
->state
& bits
) == bits
) {
1479 total_bytes
+= min(search_end
, state
->end
) + 1 -
1480 max(cur_start
, state
->start
);
1481 if (total_bytes
>= max_bytes
)
1484 *start
= state
->start
;
1488 } else if (contig
&& found
) {
1491 node
= rb_next(node
);
1496 spin_unlock(&tree
->lock
);
1501 * set the private field for a given byte offset in the tree. If there isn't
1502 * an extent_state there already, this does nothing.
1504 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1506 struct rb_node
*node
;
1507 struct extent_state
*state
;
1510 spin_lock(&tree
->lock
);
1512 * this search will find all the extents that end after
1515 node
= tree_search(tree
, start
);
1520 state
= rb_entry(node
, struct extent_state
, rb_node
);
1521 if (state
->start
!= start
) {
1525 state
->private = private;
1527 spin_unlock(&tree
->lock
);
1531 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1533 struct rb_node
*node
;
1534 struct extent_state
*state
;
1537 spin_lock(&tree
->lock
);
1539 * this search will find all the extents that end after
1542 node
= tree_search(tree
, start
);
1547 state
= rb_entry(node
, struct extent_state
, rb_node
);
1548 if (state
->start
!= start
) {
1552 *private = state
->private;
1554 spin_unlock(&tree
->lock
);
1559 * searches a range in the state tree for a given mask.
1560 * If 'filled' == 1, this returns 1 only if every extent in the tree
1561 * has the bits set. Otherwise, 1 is returned if any bit in the
1562 * range is found set.
1564 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1565 int bits
, int filled
, struct extent_state
*cached
)
1567 struct extent_state
*state
= NULL
;
1568 struct rb_node
*node
;
1571 spin_lock(&tree
->lock
);
1572 if (cached
&& cached
->tree
&& cached
->start
== start
)
1573 node
= &cached
->rb_node
;
1575 node
= tree_search(tree
, start
);
1576 while (node
&& start
<= end
) {
1577 state
= rb_entry(node
, struct extent_state
, rb_node
);
1579 if (filled
&& state
->start
> start
) {
1584 if (state
->start
> end
)
1587 if (state
->state
& bits
) {
1591 } else if (filled
) {
1596 if (state
->end
== (u64
)-1)
1599 start
= state
->end
+ 1;
1602 node
= rb_next(node
);
1609 spin_unlock(&tree
->lock
);
1614 * helper function to set a given page up to date if all the
1615 * extents in the tree for that page are up to date
1617 static int check_page_uptodate(struct extent_io_tree
*tree
,
1620 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1621 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1622 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1623 SetPageUptodate(page
);
1628 * helper function to unlock a page if all the extents in the tree
1629 * for that page are unlocked
1631 static int check_page_locked(struct extent_io_tree
*tree
,
1634 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1635 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1636 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1642 * helper function to end page writeback if all the extents
1643 * in the tree for that page are done with writeback
1645 static int check_page_writeback(struct extent_io_tree
*tree
,
1648 end_page_writeback(page
);
1652 /* lots and lots of room for performance fixes in the end_bio funcs */
1655 * after a writepage IO is done, we need to:
1656 * clear the uptodate bits on error
1657 * clear the writeback bits in the extent tree for this IO
1658 * end_page_writeback if the page has no more pending IO
1660 * Scheduling is not allowed, so the extent state tree is expected
1661 * to have one and only one object corresponding to this IO.
1663 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
1665 int uptodate
= err
== 0;
1666 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1667 struct extent_io_tree
*tree
;
1674 struct page
*page
= bvec
->bv_page
;
1675 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1677 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1679 end
= start
+ bvec
->bv_len
- 1;
1681 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1686 if (--bvec
>= bio
->bi_io_vec
)
1687 prefetchw(&bvec
->bv_page
->flags
);
1688 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
1689 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
1690 end
, NULL
, uptodate
);
1695 if (!uptodate
&& tree
->ops
&&
1696 tree
->ops
->writepage_io_failed_hook
) {
1697 ret
= tree
->ops
->writepage_io_failed_hook(bio
, page
,
1700 uptodate
= (err
== 0);
1706 clear_extent_uptodate(tree
, start
, end
, NULL
, GFP_NOFS
);
1707 ClearPageUptodate(page
);
1712 end_page_writeback(page
);
1714 check_page_writeback(tree
, page
);
1715 } while (bvec
>= bio
->bi_io_vec
);
1721 * after a readpage IO is done, we need to:
1722 * clear the uptodate bits on error
1723 * set the uptodate bits if things worked
1724 * set the page up to date if all extents in the tree are uptodate
1725 * clear the lock bit in the extent tree
1726 * unlock the page if there are no other extents locked for it
1728 * Scheduling is not allowed, so the extent state tree is expected
1729 * to have one and only one object corresponding to this IO.
1731 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
1733 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1734 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1735 struct bio_vec
*bvec
= bio
->bi_io_vec
;
1736 struct extent_io_tree
*tree
;
1746 struct page
*page
= bvec
->bv_page
;
1747 struct extent_state
*cached
= NULL
;
1748 struct extent_state
*state
;
1750 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1752 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1754 end
= start
+ bvec
->bv_len
- 1;
1756 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1761 if (++bvec
<= bvec_end
)
1762 prefetchw(&bvec
->bv_page
->flags
);
1764 spin_lock(&tree
->lock
);
1765 state
= find_first_extent_bit_state(tree
, start
, EXTENT_LOCKED
);
1766 if (state
&& state
->start
== start
) {
1768 * take a reference on the state, unlock will drop
1771 cache_state(state
, &cached
);
1773 spin_unlock(&tree
->lock
);
1775 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
1776 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
1781 if (!uptodate
&& tree
->ops
&&
1782 tree
->ops
->readpage_io_failed_hook
) {
1783 ret
= tree
->ops
->readpage_io_failed_hook(bio
, page
,
1787 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1790 uncache_state(&cached
);
1796 set_extent_uptodate(tree
, start
, end
, &cached
,
1799 unlock_extent_cached(tree
, start
, end
, &cached
, GFP_ATOMIC
);
1803 SetPageUptodate(page
);
1805 ClearPageUptodate(page
);
1811 check_page_uptodate(tree
, page
);
1813 ClearPageUptodate(page
);
1816 check_page_locked(tree
, page
);
1818 } while (bvec
<= bvec_end
);
1824 * IO done from prepare_write is pretty simple, we just unlock
1825 * the structs in the extent tree when done, and set the uptodate bits
1828 static void end_bio_extent_preparewrite(struct bio
*bio
, int err
)
1830 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1831 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1832 struct extent_io_tree
*tree
;
1837 struct page
*page
= bvec
->bv_page
;
1838 struct extent_state
*cached
= NULL
;
1839 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1841 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1843 end
= start
+ bvec
->bv_len
- 1;
1845 if (--bvec
>= bio
->bi_io_vec
)
1846 prefetchw(&bvec
->bv_page
->flags
);
1849 set_extent_uptodate(tree
, start
, end
, &cached
,
1852 ClearPageUptodate(page
);
1856 unlock_extent_cached(tree
, start
, end
, &cached
, GFP_ATOMIC
);
1858 } while (bvec
>= bio
->bi_io_vec
);
1864 btrfs_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
1869 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1871 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
1872 while (!bio
&& (nr_vecs
/= 2))
1873 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1878 bio
->bi_bdev
= bdev
;
1879 bio
->bi_sector
= first_sector
;
1884 static int submit_one_bio(int rw
, struct bio
*bio
, int mirror_num
,
1885 unsigned long bio_flags
)
1888 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1889 struct page
*page
= bvec
->bv_page
;
1890 struct extent_io_tree
*tree
= bio
->bi_private
;
1893 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
1895 bio
->bi_private
= NULL
;
1899 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
1900 ret
= tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
1901 mirror_num
, bio_flags
, start
);
1903 submit_bio(rw
, bio
);
1904 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
1910 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
1911 struct page
*page
, sector_t sector
,
1912 size_t size
, unsigned long offset
,
1913 struct block_device
*bdev
,
1914 struct bio
**bio_ret
,
1915 unsigned long max_pages
,
1916 bio_end_io_t end_io_func
,
1918 unsigned long prev_bio_flags
,
1919 unsigned long bio_flags
)
1925 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
1926 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
1927 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
1929 if (bio_ret
&& *bio_ret
) {
1932 contig
= bio
->bi_sector
== sector
;
1934 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
1937 if (prev_bio_flags
!= bio_flags
|| !contig
||
1938 (tree
->ops
&& tree
->ops
->merge_bio_hook
&&
1939 tree
->ops
->merge_bio_hook(page
, offset
, page_size
, bio
,
1941 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
1942 ret
= submit_one_bio(rw
, bio
, mirror_num
,
1949 if (this_compressed
)
1952 nr
= bio_get_nr_vecs(bdev
);
1954 bio
= btrfs_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
1958 bio_add_page(bio
, page
, page_size
, offset
);
1959 bio
->bi_end_io
= end_io_func
;
1960 bio
->bi_private
= tree
;
1965 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
1970 void set_page_extent_mapped(struct page
*page
)
1972 if (!PagePrivate(page
)) {
1973 SetPagePrivate(page
);
1974 page_cache_get(page
);
1975 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
1979 static void set_page_extent_head(struct page
*page
, unsigned long len
)
1981 WARN_ON(!PagePrivate(page
));
1982 set_page_private(page
, EXTENT_PAGE_PRIVATE_FIRST_PAGE
| len
<< 2);
1986 * basic readpage implementation. Locked extent state structs are inserted
1987 * into the tree that are removed when the IO is done (by the end_io
1990 static int __extent_read_full_page(struct extent_io_tree
*tree
,
1992 get_extent_t
*get_extent
,
1993 struct bio
**bio
, int mirror_num
,
1994 unsigned long *bio_flags
)
1996 struct inode
*inode
= page
->mapping
->host
;
1997 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1998 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2002 u64 last_byte
= i_size_read(inode
);
2006 struct extent_map
*em
;
2007 struct block_device
*bdev
;
2008 struct btrfs_ordered_extent
*ordered
;
2011 size_t page_offset
= 0;
2013 size_t disk_io_size
;
2014 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2015 unsigned long this_bio_flag
= 0;
2017 set_page_extent_mapped(page
);
2019 if (!PageUptodate(page
)) {
2020 if (cleancache_get_page(page
) == 0) {
2021 BUG_ON(blocksize
!= PAGE_SIZE
);
2028 lock_extent(tree
, start
, end
, GFP_NOFS
);
2029 ordered
= btrfs_lookup_ordered_extent(inode
, start
);
2032 unlock_extent(tree
, start
, end
, GFP_NOFS
);
2033 btrfs_start_ordered_extent(inode
, ordered
, 1);
2034 btrfs_put_ordered_extent(ordered
);
2037 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
2039 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
2042 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
2043 userpage
= kmap_atomic(page
, KM_USER0
);
2044 memset(userpage
+ zero_offset
, 0, iosize
);
2045 flush_dcache_page(page
);
2046 kunmap_atomic(userpage
, KM_USER0
);
2049 while (cur
<= end
) {
2050 if (cur
>= last_byte
) {
2052 struct extent_state
*cached
= NULL
;
2054 iosize
= PAGE_CACHE_SIZE
- page_offset
;
2055 userpage
= kmap_atomic(page
, KM_USER0
);
2056 memset(userpage
+ page_offset
, 0, iosize
);
2057 flush_dcache_page(page
);
2058 kunmap_atomic(userpage
, KM_USER0
);
2059 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2061 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2065 em
= get_extent(inode
, page
, page_offset
, cur
,
2067 if (IS_ERR(em
) || !em
) {
2069 unlock_extent(tree
, cur
, end
, GFP_NOFS
);
2072 extent_offset
= cur
- em
->start
;
2073 BUG_ON(extent_map_end(em
) <= cur
);
2076 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2077 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2078 extent_set_compress_type(&this_bio_flag
,
2082 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2083 cur_end
= min(extent_map_end(em
) - 1, end
);
2084 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2085 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2086 disk_io_size
= em
->block_len
;
2087 sector
= em
->block_start
>> 9;
2089 sector
= (em
->block_start
+ extent_offset
) >> 9;
2090 disk_io_size
= iosize
;
2093 block_start
= em
->block_start
;
2094 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2095 block_start
= EXTENT_MAP_HOLE
;
2096 free_extent_map(em
);
2099 /* we've found a hole, just zero and go on */
2100 if (block_start
== EXTENT_MAP_HOLE
) {
2102 struct extent_state
*cached
= NULL
;
2104 userpage
= kmap_atomic(page
, KM_USER0
);
2105 memset(userpage
+ page_offset
, 0, iosize
);
2106 flush_dcache_page(page
);
2107 kunmap_atomic(userpage
, KM_USER0
);
2109 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2111 unlock_extent_cached(tree
, cur
, cur
+ iosize
- 1,
2114 page_offset
+= iosize
;
2117 /* the get_extent function already copied into the page */
2118 if (test_range_bit(tree
, cur
, cur_end
,
2119 EXTENT_UPTODATE
, 1, NULL
)) {
2120 check_page_uptodate(tree
, page
);
2121 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2123 page_offset
+= iosize
;
2126 /* we have an inline extent but it didn't get marked up
2127 * to date. Error out
2129 if (block_start
== EXTENT_MAP_INLINE
) {
2131 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2133 page_offset
+= iosize
;
2138 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2139 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2143 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2145 ret
= submit_extent_page(READ
, tree
, page
,
2146 sector
, disk_io_size
, page_offset
,
2148 end_bio_extent_readpage
, mirror_num
,
2152 *bio_flags
= this_bio_flag
;
2157 page_offset
+= iosize
;
2161 if (!PageError(page
))
2162 SetPageUptodate(page
);
2168 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2169 get_extent_t
*get_extent
)
2171 struct bio
*bio
= NULL
;
2172 unsigned long bio_flags
= 0;
2175 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, 0,
2178 ret
= submit_one_bio(READ
, bio
, 0, bio_flags
);
2182 static noinline
void update_nr_written(struct page
*page
,
2183 struct writeback_control
*wbc
,
2184 unsigned long nr_written
)
2186 wbc
->nr_to_write
-= nr_written
;
2187 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2188 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2189 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2193 * the writepage semantics are similar to regular writepage. extent
2194 * records are inserted to lock ranges in the tree, and as dirty areas
2195 * are found, they are marked writeback. Then the lock bits are removed
2196 * and the end_io handler clears the writeback ranges
2198 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2201 struct inode
*inode
= page
->mapping
->host
;
2202 struct extent_page_data
*epd
= data
;
2203 struct extent_io_tree
*tree
= epd
->tree
;
2204 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2206 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2210 u64 last_byte
= i_size_read(inode
);
2214 struct extent_state
*cached_state
= NULL
;
2215 struct extent_map
*em
;
2216 struct block_device
*bdev
;
2219 size_t pg_offset
= 0;
2221 loff_t i_size
= i_size_read(inode
);
2222 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2228 unsigned long nr_written
= 0;
2230 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2231 write_flags
= WRITE_SYNC
;
2233 write_flags
= WRITE
;
2235 trace___extent_writepage(page
, inode
, wbc
);
2237 WARN_ON(!PageLocked(page
));
2238 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2239 if (page
->index
> end_index
||
2240 (page
->index
== end_index
&& !pg_offset
)) {
2241 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2246 if (page
->index
== end_index
) {
2249 userpage
= kmap_atomic(page
, KM_USER0
);
2250 memset(userpage
+ pg_offset
, 0,
2251 PAGE_CACHE_SIZE
- pg_offset
);
2252 kunmap_atomic(userpage
, KM_USER0
);
2253 flush_dcache_page(page
);
2257 set_page_extent_mapped(page
);
2259 delalloc_start
= start
;
2262 if (!epd
->extent_locked
) {
2263 u64 delalloc_to_write
= 0;
2265 * make sure the wbc mapping index is at least updated
2268 update_nr_written(page
, wbc
, 0);
2270 while (delalloc_end
< page_end
) {
2271 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2276 if (nr_delalloc
== 0) {
2277 delalloc_start
= delalloc_end
+ 1;
2280 tree
->ops
->fill_delalloc(inode
, page
, delalloc_start
,
2281 delalloc_end
, &page_started
,
2284 * delalloc_end is already one less than the total
2285 * length, so we don't subtract one from
2288 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2291 delalloc_start
= delalloc_end
+ 1;
2293 if (wbc
->nr_to_write
< delalloc_to_write
) {
2296 if (delalloc_to_write
< thresh
* 2)
2297 thresh
= delalloc_to_write
;
2298 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2302 /* did the fill delalloc function already unlock and start
2308 * we've unlocked the page, so we can't update
2309 * the mapping's writeback index, just update
2312 wbc
->nr_to_write
-= nr_written
;
2316 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2317 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2319 if (ret
== -EAGAIN
) {
2320 redirty_page_for_writepage(wbc
, page
);
2321 update_nr_written(page
, wbc
, nr_written
);
2329 * we don't want to touch the inode after unlocking the page,
2330 * so we update the mapping writeback index now
2332 update_nr_written(page
, wbc
, nr_written
+ 1);
2335 if (last_byte
<= start
) {
2336 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2337 tree
->ops
->writepage_end_io_hook(page
, start
,
2342 blocksize
= inode
->i_sb
->s_blocksize
;
2344 while (cur
<= end
) {
2345 if (cur
>= last_byte
) {
2346 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2347 tree
->ops
->writepage_end_io_hook(page
, cur
,
2351 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2353 if (IS_ERR(em
) || !em
) {
2358 extent_offset
= cur
- em
->start
;
2359 BUG_ON(extent_map_end(em
) <= cur
);
2361 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2362 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2363 sector
= (em
->block_start
+ extent_offset
) >> 9;
2365 block_start
= em
->block_start
;
2366 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2367 free_extent_map(em
);
2371 * compressed and inline extents are written through other
2374 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2375 block_start
== EXTENT_MAP_INLINE
) {
2377 * end_io notification does not happen here for
2378 * compressed extents
2380 if (!compressed
&& tree
->ops
&&
2381 tree
->ops
->writepage_end_io_hook
)
2382 tree
->ops
->writepage_end_io_hook(page
, cur
,
2385 else if (compressed
) {
2386 /* we don't want to end_page_writeback on
2387 * a compressed extent. this happens
2394 pg_offset
+= iosize
;
2397 /* leave this out until we have a page_mkwrite call */
2398 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2399 EXTENT_DIRTY
, 0, NULL
)) {
2401 pg_offset
+= iosize
;
2405 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2406 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
2414 unsigned long max_nr
= end_index
+ 1;
2416 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
2417 if (!PageWriteback(page
)) {
2418 printk(KERN_ERR
"btrfs warning page %lu not "
2419 "writeback, cur %llu end %llu\n",
2420 page
->index
, (unsigned long long)cur
,
2421 (unsigned long long)end
);
2424 ret
= submit_extent_page(write_flags
, tree
, page
,
2425 sector
, iosize
, pg_offset
,
2426 bdev
, &epd
->bio
, max_nr
,
2427 end_bio_extent_writepage
,
2433 pg_offset
+= iosize
;
2438 /* make sure the mapping tag for page dirty gets cleared */
2439 set_page_writeback(page
);
2440 end_page_writeback(page
);
2446 /* drop our reference on any cached states */
2447 free_extent_state(cached_state
);
2452 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2453 * @mapping: address space structure to write
2454 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2455 * @writepage: function called for each page
2456 * @data: data passed to writepage function
2458 * If a page is already under I/O, write_cache_pages() skips it, even
2459 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2460 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2461 * and msync() need to guarantee that all the data which was dirty at the time
2462 * the call was made get new I/O started against them. If wbc->sync_mode is
2463 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2464 * existing IO to complete.
2466 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
2467 struct address_space
*mapping
,
2468 struct writeback_control
*wbc
,
2469 writepage_t writepage
, void *data
,
2470 void (*flush_fn
)(void *))
2474 int nr_to_write_done
= 0;
2475 struct pagevec pvec
;
2478 pgoff_t end
; /* Inclusive */
2481 pagevec_init(&pvec
, 0);
2482 if (wbc
->range_cyclic
) {
2483 index
= mapping
->writeback_index
; /* Start from prev offset */
2486 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2487 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2491 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
2492 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
2493 PAGECACHE_TAG_DIRTY
, min(end
- index
,
2494 (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
2498 for (i
= 0; i
< nr_pages
; i
++) {
2499 struct page
*page
= pvec
.pages
[i
];
2502 * At this point we hold neither mapping->tree_lock nor
2503 * lock on the page itself: the page may be truncated or
2504 * invalidated (changing page->mapping to NULL), or even
2505 * swizzled back from swapper_space to tmpfs file
2508 if (tree
->ops
&& tree
->ops
->write_cache_pages_lock_hook
)
2509 tree
->ops
->write_cache_pages_lock_hook(page
);
2513 if (unlikely(page
->mapping
!= mapping
)) {
2518 if (!wbc
->range_cyclic
&& page
->index
> end
) {
2524 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
2525 if (PageWriteback(page
))
2527 wait_on_page_writeback(page
);
2530 if (PageWriteback(page
) ||
2531 !clear_page_dirty_for_io(page
)) {
2536 ret
= (*writepage
)(page
, wbc
, data
);
2538 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
2546 * the filesystem may choose to bump up nr_to_write.
2547 * We have to make sure to honor the new nr_to_write
2550 nr_to_write_done
= wbc
->nr_to_write
<= 0;
2552 pagevec_release(&pvec
);
2555 if (!scanned
&& !done
) {
2557 * We hit the last page and there is more work to be done: wrap
2558 * back to the start of the file
2567 static void flush_epd_write_bio(struct extent_page_data
*epd
)
2571 submit_one_bio(WRITE_SYNC
, epd
->bio
, 0, 0);
2573 submit_one_bio(WRITE
, epd
->bio
, 0, 0);
2578 static noinline
void flush_write_bio(void *data
)
2580 struct extent_page_data
*epd
= data
;
2581 flush_epd_write_bio(epd
);
2584 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2585 get_extent_t
*get_extent
,
2586 struct writeback_control
*wbc
)
2589 struct address_space
*mapping
= page
->mapping
;
2590 struct extent_page_data epd
= {
2593 .get_extent
= get_extent
,
2595 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2597 struct writeback_control wbc_writepages
= {
2598 .sync_mode
= wbc
->sync_mode
,
2599 .older_than_this
= NULL
,
2601 .range_start
= page_offset(page
) + PAGE_CACHE_SIZE
,
2602 .range_end
= (loff_t
)-1,
2605 ret
= __extent_writepage(page
, wbc
, &epd
);
2607 extent_write_cache_pages(tree
, mapping
, &wbc_writepages
,
2608 __extent_writepage
, &epd
, flush_write_bio
);
2609 flush_epd_write_bio(&epd
);
2613 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
2614 u64 start
, u64 end
, get_extent_t
*get_extent
,
2618 struct address_space
*mapping
= inode
->i_mapping
;
2620 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
2623 struct extent_page_data epd
= {
2626 .get_extent
= get_extent
,
2628 .sync_io
= mode
== WB_SYNC_ALL
,
2630 struct writeback_control wbc_writepages
= {
2632 .older_than_this
= NULL
,
2633 .nr_to_write
= nr_pages
* 2,
2634 .range_start
= start
,
2635 .range_end
= end
+ 1,
2638 while (start
<= end
) {
2639 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
2640 if (clear_page_dirty_for_io(page
))
2641 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
2643 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2644 tree
->ops
->writepage_end_io_hook(page
, start
,
2645 start
+ PAGE_CACHE_SIZE
- 1,
2649 page_cache_release(page
);
2650 start
+= PAGE_CACHE_SIZE
;
2653 flush_epd_write_bio(&epd
);
2657 int extent_writepages(struct extent_io_tree
*tree
,
2658 struct address_space
*mapping
,
2659 get_extent_t
*get_extent
,
2660 struct writeback_control
*wbc
)
2663 struct extent_page_data epd
= {
2666 .get_extent
= get_extent
,
2668 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2671 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
2672 __extent_writepage
, &epd
,
2674 flush_epd_write_bio(&epd
);
2678 int extent_readpages(struct extent_io_tree
*tree
,
2679 struct address_space
*mapping
,
2680 struct list_head
*pages
, unsigned nr_pages
,
2681 get_extent_t get_extent
)
2683 struct bio
*bio
= NULL
;
2685 unsigned long bio_flags
= 0;
2687 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
2688 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
2690 prefetchw(&page
->flags
);
2691 list_del(&page
->lru
);
2692 if (!add_to_page_cache_lru(page
, mapping
,
2693 page
->index
, GFP_KERNEL
)) {
2694 __extent_read_full_page(tree
, page
, get_extent
,
2695 &bio
, 0, &bio_flags
);
2697 page_cache_release(page
);
2699 BUG_ON(!list_empty(pages
));
2701 submit_one_bio(READ
, bio
, 0, bio_flags
);
2706 * basic invalidatepage code, this waits on any locked or writeback
2707 * ranges corresponding to the page, and then deletes any extent state
2708 * records from the tree
2710 int extent_invalidatepage(struct extent_io_tree
*tree
,
2711 struct page
*page
, unsigned long offset
)
2713 struct extent_state
*cached_state
= NULL
;
2714 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
2715 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2716 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
2718 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
2722 lock_extent_bits(tree
, start
, end
, 0, &cached_state
, GFP_NOFS
);
2723 wait_on_page_writeback(page
);
2724 clear_extent_bit(tree
, start
, end
,
2725 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
2726 EXTENT_DO_ACCOUNTING
,
2727 1, 1, &cached_state
, GFP_NOFS
);
2732 * simple commit_write call, set_range_dirty is used to mark both
2733 * the pages and the extent records as dirty
2735 int extent_commit_write(struct extent_io_tree
*tree
,
2736 struct inode
*inode
, struct page
*page
,
2737 unsigned from
, unsigned to
)
2739 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
2741 set_page_extent_mapped(page
);
2742 set_page_dirty(page
);
2744 if (pos
> inode
->i_size
) {
2745 i_size_write(inode
, pos
);
2746 mark_inode_dirty(inode
);
2751 int extent_prepare_write(struct extent_io_tree
*tree
,
2752 struct inode
*inode
, struct page
*page
,
2753 unsigned from
, unsigned to
, get_extent_t
*get_extent
)
2755 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2756 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2758 u64 orig_block_start
;
2761 struct extent_map
*em
;
2762 unsigned blocksize
= 1 << inode
->i_blkbits
;
2763 size_t page_offset
= 0;
2764 size_t block_off_start
;
2765 size_t block_off_end
;
2771 set_page_extent_mapped(page
);
2773 block_start
= (page_start
+ from
) & ~((u64
)blocksize
- 1);
2774 block_end
= (page_start
+ to
- 1) | (blocksize
- 1);
2775 orig_block_start
= block_start
;
2777 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2778 while (block_start
<= block_end
) {
2779 em
= get_extent(inode
, page
, page_offset
, block_start
,
2780 block_end
- block_start
+ 1, 1);
2781 if (IS_ERR(em
) || !em
)
2784 cur_end
= min(block_end
, extent_map_end(em
) - 1);
2785 block_off_start
= block_start
& (PAGE_CACHE_SIZE
- 1);
2786 block_off_end
= block_off_start
+ blocksize
;
2787 isnew
= clear_extent_new(tree
, block_start
, cur_end
, GFP_NOFS
);
2789 if (!PageUptodate(page
) && isnew
&&
2790 (block_off_end
> to
|| block_off_start
< from
)) {
2793 kaddr
= kmap_atomic(page
, KM_USER0
);
2794 if (block_off_end
> to
)
2795 memset(kaddr
+ to
, 0, block_off_end
- to
);
2796 if (block_off_start
< from
)
2797 memset(kaddr
+ block_off_start
, 0,
2798 from
- block_off_start
);
2799 flush_dcache_page(page
);
2800 kunmap_atomic(kaddr
, KM_USER0
);
2802 if ((em
->block_start
!= EXTENT_MAP_HOLE
&&
2803 em
->block_start
!= EXTENT_MAP_INLINE
) &&
2804 !isnew
&& !PageUptodate(page
) &&
2805 (block_off_end
> to
|| block_off_start
< from
) &&
2806 !test_range_bit(tree
, block_start
, cur_end
,
2807 EXTENT_UPTODATE
, 1, NULL
)) {
2809 u64 extent_offset
= block_start
- em
->start
;
2811 sector
= (em
->block_start
+ extent_offset
) >> 9;
2812 iosize
= (cur_end
- block_start
+ blocksize
) &
2813 ~((u64
)blocksize
- 1);
2815 * we've already got the extent locked, but we
2816 * need to split the state such that our end_bio
2817 * handler can clear the lock.
2819 set_extent_bit(tree
, block_start
,
2820 block_start
+ iosize
- 1,
2821 EXTENT_LOCKED
, 0, NULL
, NULL
, GFP_NOFS
);
2822 ret
= submit_extent_page(READ
, tree
, page
,
2823 sector
, iosize
, page_offset
, em
->bdev
,
2825 end_bio_extent_preparewrite
, 0,
2830 block_start
= block_start
+ iosize
;
2832 struct extent_state
*cached
= NULL
;
2834 set_extent_uptodate(tree
, block_start
, cur_end
, &cached
,
2836 unlock_extent_cached(tree
, block_start
, cur_end
,
2838 block_start
= cur_end
+ 1;
2840 page_offset
= block_start
& (PAGE_CACHE_SIZE
- 1);
2841 free_extent_map(em
);
2844 wait_extent_bit(tree
, orig_block_start
,
2845 block_end
, EXTENT_LOCKED
);
2847 check_page_uptodate(tree
, page
);
2849 /* FIXME, zero out newly allocated blocks on error */
2854 * a helper for releasepage, this tests for areas of the page that
2855 * are locked or under IO and drops the related state bits if it is safe
2858 int try_release_extent_state(struct extent_map_tree
*map
,
2859 struct extent_io_tree
*tree
, struct page
*page
,
2862 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2863 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2866 if (test_range_bit(tree
, start
, end
,
2867 EXTENT_IOBITS
, 0, NULL
))
2870 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
2873 * at this point we can safely clear everything except the
2874 * locked bit and the nodatasum bit
2876 ret
= clear_extent_bit(tree
, start
, end
,
2877 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
2880 /* if clear_extent_bit failed for enomem reasons,
2881 * we can't allow the release to continue.
2892 * a helper for releasepage. As long as there are no locked extents
2893 * in the range corresponding to the page, both state records and extent
2894 * map records are removed
2896 int try_release_extent_mapping(struct extent_map_tree
*map
,
2897 struct extent_io_tree
*tree
, struct page
*page
,
2900 struct extent_map
*em
;
2901 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2902 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2904 if ((mask
& __GFP_WAIT
) &&
2905 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
2907 while (start
<= end
) {
2908 len
= end
- start
+ 1;
2909 write_lock(&map
->lock
);
2910 em
= lookup_extent_mapping(map
, start
, len
);
2911 if (!em
|| IS_ERR(em
)) {
2912 write_unlock(&map
->lock
);
2915 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
2916 em
->start
!= start
) {
2917 write_unlock(&map
->lock
);
2918 free_extent_map(em
);
2921 if (!test_range_bit(tree
, em
->start
,
2922 extent_map_end(em
) - 1,
2923 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
2925 remove_extent_mapping(map
, em
);
2926 /* once for the rb tree */
2927 free_extent_map(em
);
2929 start
= extent_map_end(em
);
2930 write_unlock(&map
->lock
);
2933 free_extent_map(em
);
2936 return try_release_extent_state(map
, tree
, page
, mask
);
2939 sector_t
extent_bmap(struct address_space
*mapping
, sector_t iblock
,
2940 get_extent_t
*get_extent
)
2942 struct inode
*inode
= mapping
->host
;
2943 struct extent_state
*cached_state
= NULL
;
2944 u64 start
= iblock
<< inode
->i_blkbits
;
2945 sector_t sector
= 0;
2946 size_t blksize
= (1 << inode
->i_blkbits
);
2947 struct extent_map
*em
;
2949 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2950 0, &cached_state
, GFP_NOFS
);
2951 em
= get_extent(inode
, NULL
, 0, start
, blksize
, 0);
2952 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
,
2953 start
+ blksize
- 1, &cached_state
, GFP_NOFS
);
2954 if (!em
|| IS_ERR(em
))
2957 if (em
->block_start
> EXTENT_MAP_LAST_BYTE
)
2960 sector
= (em
->block_start
+ start
- em
->start
) >> inode
->i_blkbits
;
2962 free_extent_map(em
);
2967 * helper function for fiemap, which doesn't want to see any holes.
2968 * This maps until we find something past 'last'
2970 static struct extent_map
*get_extent_skip_holes(struct inode
*inode
,
2973 get_extent_t
*get_extent
)
2975 u64 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
2976 struct extent_map
*em
;
2983 len
= last
- offset
;
2986 len
= (len
+ sectorsize
- 1) & ~(sectorsize
- 1);
2987 em
= get_extent(inode
, NULL
, 0, offset
, len
, 0);
2988 if (!em
|| IS_ERR(em
))
2991 /* if this isn't a hole return it */
2992 if (!test_bit(EXTENT_FLAG_VACANCY
, &em
->flags
) &&
2993 em
->block_start
!= EXTENT_MAP_HOLE
) {
2997 /* this is a hole, advance to the next extent */
2998 offset
= extent_map_end(em
);
2999 free_extent_map(em
);
3006 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3007 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
3011 u64 max
= start
+ len
;
3015 u64 last_for_get_extent
= 0;
3017 u64 isize
= i_size_read(inode
);
3018 struct btrfs_key found_key
;
3019 struct extent_map
*em
= NULL
;
3020 struct extent_state
*cached_state
= NULL
;
3021 struct btrfs_path
*path
;
3022 struct btrfs_file_extent_item
*item
;
3027 unsigned long emflags
;
3032 path
= btrfs_alloc_path();
3035 path
->leave_spinning
= 1;
3038 * lookup the last file extent. We're not using i_size here
3039 * because there might be preallocation past i_size
3041 ret
= btrfs_lookup_file_extent(NULL
, BTRFS_I(inode
)->root
,
3042 path
, inode
->i_ino
, -1, 0);
3044 btrfs_free_path(path
);
3049 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3050 struct btrfs_file_extent_item
);
3051 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, path
->slots
[0]);
3052 found_type
= btrfs_key_type(&found_key
);
3054 /* No extents, but there might be delalloc bits */
3055 if (found_key
.objectid
!= inode
->i_ino
||
3056 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
3057 /* have to trust i_size as the end */
3059 last_for_get_extent
= isize
;
3062 * remember the start of the last extent. There are a
3063 * bunch of different factors that go into the length of the
3064 * extent, so its much less complex to remember where it started
3066 last
= found_key
.offset
;
3067 last_for_get_extent
= last
+ 1;
3069 btrfs_free_path(path
);
3072 * we might have some extents allocated but more delalloc past those
3073 * extents. so, we trust isize unless the start of the last extent is
3078 last_for_get_extent
= isize
;
3081 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
3082 &cached_state
, GFP_NOFS
);
3084 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
,
3094 u64 offset_in_extent
;
3096 /* break if the extent we found is outside the range */
3097 if (em
->start
>= max
|| extent_map_end(em
) < off
)
3101 * get_extent may return an extent that starts before our
3102 * requested range. We have to make sure the ranges
3103 * we return to fiemap always move forward and don't
3104 * overlap, so adjust the offsets here
3106 em_start
= max(em
->start
, off
);
3109 * record the offset from the start of the extent
3110 * for adjusting the disk offset below
3112 offset_in_extent
= em_start
- em
->start
;
3113 em_end
= extent_map_end(em
);
3114 em_len
= em_end
- em_start
;
3115 emflags
= em
->flags
;
3120 * bump off for our next call to get_extent
3122 off
= extent_map_end(em
);
3126 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
3128 flags
|= FIEMAP_EXTENT_LAST
;
3129 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
3130 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
3131 FIEMAP_EXTENT_NOT_ALIGNED
);
3132 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
3133 flags
|= (FIEMAP_EXTENT_DELALLOC
|
3134 FIEMAP_EXTENT_UNKNOWN
);
3136 disko
= em
->block_start
+ offset_in_extent
;
3138 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
3139 flags
|= FIEMAP_EXTENT_ENCODED
;
3141 free_extent_map(em
);
3143 if ((em_start
>= last
) || em_len
== (u64
)-1 ||
3144 (last
== (u64
)-1 && isize
<= em_end
)) {
3145 flags
|= FIEMAP_EXTENT_LAST
;
3149 /* now scan forward to see if this is really the last extent. */
3150 em
= get_extent_skip_holes(inode
, off
, last_for_get_extent
,
3157 flags
|= FIEMAP_EXTENT_LAST
;
3160 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3166 free_extent_map(em
);
3168 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3169 &cached_state
, GFP_NOFS
);
3173 static inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
3177 struct address_space
*mapping
;
3180 return eb
->first_page
;
3181 i
+= eb
->start
>> PAGE_CACHE_SHIFT
;
3182 mapping
= eb
->first_page
->mapping
;
3187 * extent_buffer_page is only called after pinning the page
3188 * by increasing the reference count. So we know the page must
3189 * be in the radix tree.
3192 p
= radix_tree_lookup(&mapping
->page_tree
, i
);
3198 static inline unsigned long num_extent_pages(u64 start
, u64 len
)
3200 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
3201 (start
>> PAGE_CACHE_SHIFT
);
3204 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3209 struct extent_buffer
*eb
= NULL
;
3211 unsigned long flags
;
3214 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
3219 spin_lock_init(&eb
->lock
);
3220 init_waitqueue_head(&eb
->lock_wq
);
3223 spin_lock_irqsave(&leak_lock
, flags
);
3224 list_add(&eb
->leak_list
, &buffers
);
3225 spin_unlock_irqrestore(&leak_lock
, flags
);
3227 atomic_set(&eb
->refs
, 1);
3232 static void __free_extent_buffer(struct extent_buffer
*eb
)
3235 unsigned long flags
;
3236 spin_lock_irqsave(&leak_lock
, flags
);
3237 list_del(&eb
->leak_list
);
3238 spin_unlock_irqrestore(&leak_lock
, flags
);
3240 kmem_cache_free(extent_buffer_cache
, eb
);
3244 * Helper for releasing extent buffer page.
3246 static void btrfs_release_extent_buffer_page(struct extent_buffer
*eb
,
3247 unsigned long start_idx
)
3249 unsigned long index
;
3252 if (!eb
->first_page
)
3255 index
= num_extent_pages(eb
->start
, eb
->len
);
3256 if (start_idx
>= index
)
3261 page
= extent_buffer_page(eb
, index
);
3263 page_cache_release(page
);
3264 } while (index
!= start_idx
);
3268 * Helper for releasing the extent buffer.
3270 static inline void btrfs_release_extent_buffer(struct extent_buffer
*eb
)
3272 btrfs_release_extent_buffer_page(eb
, 0);
3273 __free_extent_buffer(eb
);
3276 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
3277 u64 start
, unsigned long len
,
3281 unsigned long num_pages
= num_extent_pages(start
, len
);
3283 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
3284 struct extent_buffer
*eb
;
3285 struct extent_buffer
*exists
= NULL
;
3287 struct address_space
*mapping
= tree
->mapping
;
3292 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3293 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
3295 mark_page_accessed(eb
->first_page
);
3300 eb
= __alloc_extent_buffer(tree
, start
, len
, mask
);
3305 eb
->first_page
= page0
;
3308 page_cache_get(page0
);
3309 mark_page_accessed(page0
);
3310 set_page_extent_mapped(page0
);
3311 set_page_extent_head(page0
, len
);
3312 uptodate
= PageUptodate(page0
);
3316 for (; i
< num_pages
; i
++, index
++) {
3317 p
= find_or_create_page(mapping
, index
, mask
| __GFP_HIGHMEM
);
3322 set_page_extent_mapped(p
);
3323 mark_page_accessed(p
);
3326 set_page_extent_head(p
, len
);
3328 set_page_private(p
, EXTENT_PAGE_PRIVATE
);
3330 if (!PageUptodate(p
))
3334 * see below about how we avoid a nasty race with release page
3335 * and why we unlock later
3341 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3343 ret
= radix_tree_preload(GFP_NOFS
& ~__GFP_HIGHMEM
);
3347 spin_lock(&tree
->buffer_lock
);
3348 ret
= radix_tree_insert(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
, eb
);
3349 if (ret
== -EEXIST
) {
3350 exists
= radix_tree_lookup(&tree
->buffer
,
3351 start
>> PAGE_CACHE_SHIFT
);
3352 /* add one reference for the caller */
3353 atomic_inc(&exists
->refs
);
3354 spin_unlock(&tree
->buffer_lock
);
3355 radix_tree_preload_end();
3358 /* add one reference for the tree */
3359 atomic_inc(&eb
->refs
);
3360 spin_unlock(&tree
->buffer_lock
);
3361 radix_tree_preload_end();
3364 * there is a race where release page may have
3365 * tried to find this extent buffer in the radix
3366 * but failed. It will tell the VM it is safe to
3367 * reclaim the, and it will clear the page private bit.
3368 * We must make sure to set the page private bit properly
3369 * after the extent buffer is in the radix tree so
3370 * it doesn't get lost
3372 set_page_extent_mapped(eb
->first_page
);
3373 set_page_extent_head(eb
->first_page
, eb
->len
);
3375 unlock_page(eb
->first_page
);
3379 if (eb
->first_page
&& !page0
)
3380 unlock_page(eb
->first_page
);
3382 if (!atomic_dec_and_test(&eb
->refs
))
3384 btrfs_release_extent_buffer(eb
);
3388 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
3389 u64 start
, unsigned long len
,
3392 struct extent_buffer
*eb
;
3395 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3396 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
3398 mark_page_accessed(eb
->first_page
);
3406 void free_extent_buffer(struct extent_buffer
*eb
)
3411 if (!atomic_dec_and_test(&eb
->refs
))
3417 int clear_extent_buffer_dirty(struct extent_io_tree
*tree
,
3418 struct extent_buffer
*eb
)
3421 unsigned long num_pages
;
3424 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3426 for (i
= 0; i
< num_pages
; i
++) {
3427 page
= extent_buffer_page(eb
, i
);
3428 if (!PageDirty(page
))
3432 WARN_ON(!PagePrivate(page
));
3434 set_page_extent_mapped(page
);
3436 set_page_extent_head(page
, eb
->len
);
3438 clear_page_dirty_for_io(page
);
3439 spin_lock_irq(&page
->mapping
->tree_lock
);
3440 if (!PageDirty(page
)) {
3441 radix_tree_tag_clear(&page
->mapping
->page_tree
,
3443 PAGECACHE_TAG_DIRTY
);
3445 spin_unlock_irq(&page
->mapping
->tree_lock
);
3451 int wait_on_extent_buffer_writeback(struct extent_io_tree
*tree
,
3452 struct extent_buffer
*eb
)
3454 return wait_on_extent_writeback(tree
, eb
->start
,
3455 eb
->start
+ eb
->len
- 1);
3458 int set_extent_buffer_dirty(struct extent_io_tree
*tree
,
3459 struct extent_buffer
*eb
)
3462 unsigned long num_pages
;
3465 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
3466 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3467 for (i
= 0; i
< num_pages
; i
++)
3468 __set_page_dirty_nobuffers(extent_buffer_page(eb
, i
));
3472 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3473 struct extent_buffer
*eb
,
3474 struct extent_state
**cached_state
)
3478 unsigned long num_pages
;
3480 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3481 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3483 clear_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3484 cached_state
, GFP_NOFS
);
3485 for (i
= 0; i
< num_pages
; i
++) {
3486 page
= extent_buffer_page(eb
, i
);
3488 ClearPageUptodate(page
);
3493 int set_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3494 struct extent_buffer
*eb
)
3498 unsigned long num_pages
;
3500 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3502 set_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3504 for (i
= 0; i
< num_pages
; i
++) {
3505 page
= extent_buffer_page(eb
, i
);
3506 if ((i
== 0 && (eb
->start
& (PAGE_CACHE_SIZE
- 1))) ||
3507 ((i
== num_pages
- 1) &&
3508 ((eb
->start
+ eb
->len
) & (PAGE_CACHE_SIZE
- 1)))) {
3509 check_page_uptodate(tree
, page
);
3512 SetPageUptodate(page
);
3517 int extent_range_uptodate(struct extent_io_tree
*tree
,
3522 int pg_uptodate
= 1;
3524 unsigned long index
;
3526 ret
= test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
);
3529 while (start
<= end
) {
3530 index
= start
>> PAGE_CACHE_SHIFT
;
3531 page
= find_get_page(tree
->mapping
, index
);
3532 uptodate
= PageUptodate(page
);
3533 page_cache_release(page
);
3538 start
+= PAGE_CACHE_SIZE
;
3543 int extent_buffer_uptodate(struct extent_io_tree
*tree
,
3544 struct extent_buffer
*eb
,
3545 struct extent_state
*cached_state
)
3548 unsigned long num_pages
;
3551 int pg_uptodate
= 1;
3553 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3556 ret
= test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3557 EXTENT_UPTODATE
, 1, cached_state
);
3561 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3562 for (i
= 0; i
< num_pages
; i
++) {
3563 page
= extent_buffer_page(eb
, i
);
3564 if (!PageUptodate(page
)) {
3572 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
3573 struct extent_buffer
*eb
,
3574 u64 start
, int wait
,
3575 get_extent_t
*get_extent
, int mirror_num
)
3578 unsigned long start_i
;
3582 int locked_pages
= 0;
3583 int all_uptodate
= 1;
3584 int inc_all_pages
= 0;
3585 unsigned long num_pages
;
3586 struct bio
*bio
= NULL
;
3587 unsigned long bio_flags
= 0;
3589 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3592 if (test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3593 EXTENT_UPTODATE
, 1, NULL
)) {
3598 WARN_ON(start
< eb
->start
);
3599 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
3600 (eb
->start
>> PAGE_CACHE_SHIFT
);
3605 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3606 for (i
= start_i
; i
< num_pages
; i
++) {
3607 page
= extent_buffer_page(eb
, i
);
3609 if (!trylock_page(page
))
3615 if (!PageUptodate(page
))
3620 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3624 for (i
= start_i
; i
< num_pages
; i
++) {
3625 page
= extent_buffer_page(eb
, i
);
3627 WARN_ON(!PagePrivate(page
));
3629 set_page_extent_mapped(page
);
3631 set_page_extent_head(page
, eb
->len
);
3634 page_cache_get(page
);
3635 if (!PageUptodate(page
)) {
3638 ClearPageError(page
);
3639 err
= __extent_read_full_page(tree
, page
,
3641 mirror_num
, &bio_flags
);
3650 submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
3655 for (i
= start_i
; i
< num_pages
; i
++) {
3656 page
= extent_buffer_page(eb
, i
);
3657 wait_on_page_locked(page
);
3658 if (!PageUptodate(page
))
3663 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3668 while (locked_pages
> 0) {
3669 page
= extent_buffer_page(eb
, i
);
3677 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
3678 unsigned long start
,
3685 char *dst
= (char *)dstv
;
3686 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3687 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3689 WARN_ON(start
> eb
->len
);
3690 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3692 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3695 page
= extent_buffer_page(eb
, i
);
3697 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3698 kaddr
= kmap_atomic(page
, KM_USER1
);
3699 memcpy(dst
, kaddr
+ offset
, cur
);
3700 kunmap_atomic(kaddr
, KM_USER1
);
3709 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3710 unsigned long min_len
, char **token
, char **map
,
3711 unsigned long *map_start
,
3712 unsigned long *map_len
, int km
)
3714 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
3717 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3718 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3719 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
3726 offset
= start_offset
;
3730 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
3733 if (start
+ min_len
> eb
->len
) {
3734 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
3735 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
3736 eb
->len
, start
, min_len
);
3741 p
= extent_buffer_page(eb
, i
);
3742 kaddr
= kmap_atomic(p
, km
);
3744 *map
= kaddr
+ offset
;
3745 *map_len
= PAGE_CACHE_SIZE
- offset
;
3749 int map_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3750 unsigned long min_len
,
3751 char **token
, char **map
,
3752 unsigned long *map_start
,
3753 unsigned long *map_len
, int km
)
3757 if (eb
->map_token
) {
3758 unmap_extent_buffer(eb
, eb
->map_token
, km
);
3759 eb
->map_token
= NULL
;
3762 err
= map_private_extent_buffer(eb
, start
, min_len
, token
, map
,
3763 map_start
, map_len
, km
);
3765 eb
->map_token
= *token
;
3767 eb
->map_start
= *map_start
;
3768 eb
->map_len
= *map_len
;
3773 void unmap_extent_buffer(struct extent_buffer
*eb
, char *token
, int km
)
3775 kunmap_atomic(token
, km
);
3778 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
3779 unsigned long start
,
3786 char *ptr
= (char *)ptrv
;
3787 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3788 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3791 WARN_ON(start
> eb
->len
);
3792 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3794 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3797 page
= extent_buffer_page(eb
, i
);
3799 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3801 kaddr
= kmap_atomic(page
, KM_USER0
);
3802 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
3803 kunmap_atomic(kaddr
, KM_USER0
);
3815 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
3816 unsigned long start
, unsigned long len
)
3822 char *src
= (char *)srcv
;
3823 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3824 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3826 WARN_ON(start
> eb
->len
);
3827 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3829 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3832 page
= extent_buffer_page(eb
, i
);
3833 WARN_ON(!PageUptodate(page
));
3835 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3836 kaddr
= kmap_atomic(page
, KM_USER1
);
3837 memcpy(kaddr
+ offset
, src
, cur
);
3838 kunmap_atomic(kaddr
, KM_USER1
);
3847 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
3848 unsigned long start
, unsigned long len
)
3854 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3855 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3857 WARN_ON(start
> eb
->len
);
3858 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3860 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3863 page
= extent_buffer_page(eb
, i
);
3864 WARN_ON(!PageUptodate(page
));
3866 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3867 kaddr
= kmap_atomic(page
, KM_USER0
);
3868 memset(kaddr
+ offset
, c
, cur
);
3869 kunmap_atomic(kaddr
, KM_USER0
);
3877 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
3878 unsigned long dst_offset
, unsigned long src_offset
,
3881 u64 dst_len
= dst
->len
;
3886 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3887 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3889 WARN_ON(src
->len
!= dst_len
);
3891 offset
= (start_offset
+ dst_offset
) &
3892 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3895 page
= extent_buffer_page(dst
, i
);
3896 WARN_ON(!PageUptodate(page
));
3898 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
3900 kaddr
= kmap_atomic(page
, KM_USER0
);
3901 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
3902 kunmap_atomic(kaddr
, KM_USER0
);
3911 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
3912 unsigned long dst_off
, unsigned long src_off
,
3915 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3916 if (dst_page
== src_page
) {
3917 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
3919 char *src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3920 char *p
= dst_kaddr
+ dst_off
+ len
;
3921 char *s
= src_kaddr
+ src_off
+ len
;
3926 kunmap_atomic(src_kaddr
, KM_USER1
);
3928 kunmap_atomic(dst_kaddr
, KM_USER0
);
3931 static inline bool areas_overlap(unsigned long src
, unsigned long dst
, unsigned long len
)
3933 unsigned long distance
= (src
> dst
) ? src
- dst
: dst
- src
;
3934 return distance
< len
;
3937 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
3938 unsigned long dst_off
, unsigned long src_off
,
3941 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3944 if (dst_page
!= src_page
) {
3945 src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3947 src_kaddr
= dst_kaddr
;
3948 BUG_ON(areas_overlap(src_off
, dst_off
, len
));
3951 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
3952 kunmap_atomic(dst_kaddr
, KM_USER0
);
3953 if (dst_page
!= src_page
)
3954 kunmap_atomic(src_kaddr
, KM_USER1
);
3957 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3958 unsigned long src_offset
, unsigned long len
)
3961 size_t dst_off_in_page
;
3962 size_t src_off_in_page
;
3963 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3964 unsigned long dst_i
;
3965 unsigned long src_i
;
3967 if (src_offset
+ len
> dst
->len
) {
3968 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3969 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
3972 if (dst_offset
+ len
> dst
->len
) {
3973 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3974 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
3979 dst_off_in_page
= (start_offset
+ dst_offset
) &
3980 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3981 src_off_in_page
= (start_offset
+ src_offset
) &
3982 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3984 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3985 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
3987 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
3989 cur
= min_t(unsigned long, cur
,
3990 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
3992 copy_pages(extent_buffer_page(dst
, dst_i
),
3993 extent_buffer_page(dst
, src_i
),
3994 dst_off_in_page
, src_off_in_page
, cur
);
4002 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
4003 unsigned long src_offset
, unsigned long len
)
4006 size_t dst_off_in_page
;
4007 size_t src_off_in_page
;
4008 unsigned long dst_end
= dst_offset
+ len
- 1;
4009 unsigned long src_end
= src_offset
+ len
- 1;
4010 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
4011 unsigned long dst_i
;
4012 unsigned long src_i
;
4014 if (src_offset
+ len
> dst
->len
) {
4015 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
4016 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
4019 if (dst_offset
+ len
> dst
->len
) {
4020 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
4021 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
4024 if (!areas_overlap(src_offset
, dst_offset
, len
)) {
4025 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
4029 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
4030 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
4032 dst_off_in_page
= (start_offset
+ dst_end
) &
4033 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4034 src_off_in_page
= (start_offset
+ src_end
) &
4035 ((unsigned long)PAGE_CACHE_SIZE
- 1);
4037 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
4038 cur
= min(cur
, dst_off_in_page
+ 1);
4039 move_pages(extent_buffer_page(dst
, dst_i
),
4040 extent_buffer_page(dst
, src_i
),
4041 dst_off_in_page
- cur
+ 1,
4042 src_off_in_page
- cur
+ 1, cur
);
4050 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head
*head
)
4052 struct extent_buffer
*eb
=
4053 container_of(head
, struct extent_buffer
, rcu_head
);
4055 btrfs_release_extent_buffer(eb
);
4058 int try_release_extent_buffer(struct extent_io_tree
*tree
, struct page
*page
)
4060 u64 start
= page_offset(page
);
4061 struct extent_buffer
*eb
;
4064 spin_lock(&tree
->buffer_lock
);
4065 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4067 spin_unlock(&tree
->buffer_lock
);
4071 if (test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
4077 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4080 if (atomic_cmpxchg(&eb
->refs
, 1, 0) != 1) {
4085 radix_tree_delete(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
4087 spin_unlock(&tree
->buffer_lock
);
4089 /* at this point we can safely release the extent buffer */
4090 if (atomic_read(&eb
->refs
) == 0)
4091 call_rcu(&eb
->rcu_head
, btrfs_release_extent_buffer_rcu
);