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 "extent_io.h"
14 #include "extent_map.h"
17 #include "btrfs_inode.h"
19 static struct kmem_cache
*extent_state_cache
;
20 static struct kmem_cache
*extent_buffer_cache
;
22 static LIST_HEAD(buffers
);
23 static LIST_HEAD(states
);
27 static DEFINE_SPINLOCK(leak_lock
);
30 #define BUFFER_LRU_MAX 64
35 struct rb_node rb_node
;
38 struct extent_page_data
{
40 struct extent_io_tree
*tree
;
41 get_extent_t
*get_extent
;
43 /* tells writepage not to lock the state bits for this range
44 * it still does the unlocking
46 unsigned int extent_locked
:1;
48 /* tells the submit_bio code to use a WRITE_SYNC */
49 unsigned int sync_io
:1;
52 int __init
extent_io_init(void)
54 extent_state_cache
= kmem_cache_create("extent_state",
55 sizeof(struct extent_state
), 0,
56 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
57 if (!extent_state_cache
)
60 extent_buffer_cache
= kmem_cache_create("extent_buffers",
61 sizeof(struct extent_buffer
), 0,
62 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
63 if (!extent_buffer_cache
)
64 goto free_state_cache
;
68 kmem_cache_destroy(extent_state_cache
);
72 void extent_io_exit(void)
74 struct extent_state
*state
;
75 struct extent_buffer
*eb
;
77 while (!list_empty(&states
)) {
78 state
= list_entry(states
.next
, struct extent_state
, leak_list
);
79 printk(KERN_ERR
"btrfs state leak: start %llu end %llu "
80 "state %lu in tree %p refs %d\n",
81 (unsigned long long)state
->start
,
82 (unsigned long long)state
->end
,
83 state
->state
, state
->tree
, atomic_read(&state
->refs
));
84 list_del(&state
->leak_list
);
85 kmem_cache_free(extent_state_cache
, state
);
89 while (!list_empty(&buffers
)) {
90 eb
= list_entry(buffers
.next
, struct extent_buffer
, leak_list
);
91 printk(KERN_ERR
"btrfs buffer leak start %llu len %lu "
92 "refs %d\n", (unsigned long long)eb
->start
,
93 eb
->len
, atomic_read(&eb
->refs
));
94 list_del(&eb
->leak_list
);
95 kmem_cache_free(extent_buffer_cache
, eb
);
97 if (extent_state_cache
)
98 kmem_cache_destroy(extent_state_cache
);
99 if (extent_buffer_cache
)
100 kmem_cache_destroy(extent_buffer_cache
);
103 void extent_io_tree_init(struct extent_io_tree
*tree
,
104 struct address_space
*mapping
, gfp_t mask
)
106 tree
->state
= RB_ROOT
;
107 INIT_RADIX_TREE(&tree
->buffer
, GFP_ATOMIC
);
109 tree
->dirty_bytes
= 0;
110 spin_lock_init(&tree
->lock
);
111 spin_lock_init(&tree
->buffer_lock
);
112 tree
->mapping
= mapping
;
115 static struct extent_state
*alloc_extent_state(gfp_t mask
)
117 struct extent_state
*state
;
122 state
= kmem_cache_alloc(extent_state_cache
, mask
);
129 spin_lock_irqsave(&leak_lock
, flags
);
130 list_add(&state
->leak_list
, &states
);
131 spin_unlock_irqrestore(&leak_lock
, flags
);
133 atomic_set(&state
->refs
, 1);
134 init_waitqueue_head(&state
->wq
);
138 void free_extent_state(struct extent_state
*state
)
142 if (atomic_dec_and_test(&state
->refs
)) {
146 WARN_ON(state
->tree
);
148 spin_lock_irqsave(&leak_lock
, flags
);
149 list_del(&state
->leak_list
);
150 spin_unlock_irqrestore(&leak_lock
, flags
);
152 kmem_cache_free(extent_state_cache
, state
);
156 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
157 struct rb_node
*node
)
159 struct rb_node
**p
= &root
->rb_node
;
160 struct rb_node
*parent
= NULL
;
161 struct tree_entry
*entry
;
165 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
167 if (offset
< entry
->start
)
169 else if (offset
> entry
->end
)
175 entry
= rb_entry(node
, struct tree_entry
, rb_node
);
176 rb_link_node(node
, parent
, p
);
177 rb_insert_color(node
, root
);
181 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
182 struct rb_node
**prev_ret
,
183 struct rb_node
**next_ret
)
185 struct rb_root
*root
= &tree
->state
;
186 struct rb_node
*n
= root
->rb_node
;
187 struct rb_node
*prev
= NULL
;
188 struct rb_node
*orig_prev
= NULL
;
189 struct tree_entry
*entry
;
190 struct tree_entry
*prev_entry
= NULL
;
193 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
197 if (offset
< entry
->start
)
199 else if (offset
> entry
->end
)
207 while (prev
&& offset
> prev_entry
->end
) {
208 prev
= rb_next(prev
);
209 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
216 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
217 while (prev
&& offset
< prev_entry
->start
) {
218 prev
= rb_prev(prev
);
219 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
226 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
229 struct rb_node
*prev
= NULL
;
232 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
238 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
239 struct extent_state
*other
)
241 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
242 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
247 * utility function to look for merge candidates inside a given range.
248 * Any extents with matching state are merged together into a single
249 * extent in the tree. Extents with EXTENT_IO in their state field
250 * are not merged because the end_io handlers need to be able to do
251 * operations on them without sleeping (or doing allocations/splits).
253 * This should be called with the tree lock held.
255 static int merge_state(struct extent_io_tree
*tree
,
256 struct extent_state
*state
)
258 struct extent_state
*other
;
259 struct rb_node
*other_node
;
261 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
264 other_node
= rb_prev(&state
->rb_node
);
266 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
267 if (other
->end
== state
->start
- 1 &&
268 other
->state
== state
->state
) {
269 merge_cb(tree
, state
, other
);
270 state
->start
= other
->start
;
272 rb_erase(&other
->rb_node
, &tree
->state
);
273 free_extent_state(other
);
276 other_node
= rb_next(&state
->rb_node
);
278 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
279 if (other
->start
== state
->end
+ 1 &&
280 other
->state
== state
->state
) {
281 merge_cb(tree
, state
, other
);
282 other
->start
= state
->start
;
284 rb_erase(&state
->rb_node
, &tree
->state
);
285 free_extent_state(state
);
293 static int set_state_cb(struct extent_io_tree
*tree
,
294 struct extent_state
*state
, int *bits
)
296 if (tree
->ops
&& tree
->ops
->set_bit_hook
) {
297 return tree
->ops
->set_bit_hook(tree
->mapping
->host
,
304 static void clear_state_cb(struct extent_io_tree
*tree
,
305 struct extent_state
*state
, int *bits
)
307 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
308 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
, bits
);
312 * insert an extent_state struct into the tree. 'bits' are set on the
313 * struct before it is inserted.
315 * This may return -EEXIST if the extent is already there, in which case the
316 * state struct is freed.
318 * The tree lock is not taken internally. This is a utility function and
319 * probably isn't what you want to call (see set/clear_extent_bit).
321 static int insert_state(struct extent_io_tree
*tree
,
322 struct extent_state
*state
, u64 start
, u64 end
,
325 struct rb_node
*node
;
326 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
330 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
331 (unsigned long long)end
,
332 (unsigned long long)start
);
335 state
->start
= start
;
337 ret
= set_state_cb(tree
, state
, bits
);
341 if (bits_to_set
& EXTENT_DIRTY
)
342 tree
->dirty_bytes
+= end
- start
+ 1;
343 state
->state
|= bits_to_set
;
344 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
346 struct extent_state
*found
;
347 found
= rb_entry(node
, struct extent_state
, rb_node
);
348 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
349 "%llu %llu\n", (unsigned long long)found
->start
,
350 (unsigned long long)found
->end
,
351 (unsigned long long)start
, (unsigned long long)end
);
352 free_extent_state(state
);
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
);
443 * clear some bits on a range in the tree. This may require splitting
444 * or inserting elements in the tree, so the gfp mask is used to
445 * indicate which allocations or sleeping are allowed.
447 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
448 * the given range from the tree regardless of state (ie for truncate).
450 * the range [start, end] is inclusive.
452 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
453 * bits were already set, or zero if none of the bits were already set.
455 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
456 int bits
, int wake
, int delete,
457 struct extent_state
**cached_state
,
460 struct extent_state
*state
;
461 struct extent_state
*cached
;
462 struct extent_state
*prealloc
= NULL
;
463 struct rb_node
*next_node
;
464 struct rb_node
*node
;
471 bits
|= ~EXTENT_CTLBITS
;
472 bits
|= EXTENT_FIRST_DELALLOC
;
474 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
477 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
478 prealloc
= alloc_extent_state(mask
);
483 spin_lock(&tree
->lock
);
485 cached
= *cached_state
;
488 *cached_state
= NULL
;
492 if (cached
&& cached
->tree
&& cached
->start
== start
) {
494 atomic_dec(&cached
->refs
);
499 free_extent_state(cached
);
502 * this search will find the extents that end after
505 node
= tree_search(tree
, start
);
508 state
= rb_entry(node
, struct extent_state
, rb_node
);
510 if (state
->start
> end
)
512 WARN_ON(state
->end
< start
);
513 last_end
= state
->end
;
516 * | ---- desired range ---- |
518 * | ------------- state -------------- |
520 * We need to split the extent we found, and may flip
521 * bits on second half.
523 * If the extent we found extends past our range, we
524 * just split and search again. It'll get split again
525 * the next time though.
527 * If the extent we found is inside our range, we clear
528 * the desired bit on it.
531 if (state
->start
< start
) {
533 prealloc
= alloc_extent_state(GFP_ATOMIC
);
534 err
= split_state(tree
, state
, prealloc
, start
);
535 BUG_ON(err
== -EEXIST
);
539 if (state
->end
<= end
) {
540 set
|= clear_state_bit(tree
, state
, &bits
, wake
);
541 if (last_end
== (u64
)-1)
543 start
= last_end
+ 1;
548 * | ---- desired range ---- |
550 * We need to split the extent, and clear the bit
553 if (state
->start
<= end
&& state
->end
> end
) {
555 prealloc
= alloc_extent_state(GFP_ATOMIC
);
556 err
= split_state(tree
, state
, prealloc
, end
+ 1);
557 BUG_ON(err
== -EEXIST
);
561 set
|= clear_state_bit(tree
, prealloc
, &bits
, wake
);
567 if (state
->end
< end
&& prealloc
&& !need_resched())
568 next_node
= rb_next(&state
->rb_node
);
572 set
|= clear_state_bit(tree
, state
, &bits
, wake
);
573 if (last_end
== (u64
)-1)
575 start
= last_end
+ 1;
576 if (start
<= end
&& next_node
) {
577 state
= rb_entry(next_node
, struct extent_state
,
579 if (state
->start
== start
)
585 spin_unlock(&tree
->lock
);
587 free_extent_state(prealloc
);
594 spin_unlock(&tree
->lock
);
595 if (mask
& __GFP_WAIT
)
600 static int wait_on_state(struct extent_io_tree
*tree
,
601 struct extent_state
*state
)
602 __releases(tree
->lock
)
603 __acquires(tree
->lock
)
606 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
607 spin_unlock(&tree
->lock
);
609 spin_lock(&tree
->lock
);
610 finish_wait(&state
->wq
, &wait
);
615 * waits for one or more bits to clear on a range in the state tree.
616 * The range [start, end] is inclusive.
617 * The tree lock is taken by this function
619 int wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
621 struct extent_state
*state
;
622 struct rb_node
*node
;
624 spin_lock(&tree
->lock
);
628 * this search will find all the extents that end after
631 node
= tree_search(tree
, start
);
635 state
= rb_entry(node
, struct extent_state
, rb_node
);
637 if (state
->start
> end
)
640 if (state
->state
& bits
) {
641 start
= state
->start
;
642 atomic_inc(&state
->refs
);
643 wait_on_state(tree
, state
);
644 free_extent_state(state
);
647 start
= state
->end
+ 1;
652 if (need_resched()) {
653 spin_unlock(&tree
->lock
);
655 spin_lock(&tree
->lock
);
659 spin_unlock(&tree
->lock
);
663 static int set_state_bits(struct extent_io_tree
*tree
,
664 struct extent_state
*state
,
668 int bits_to_set
= *bits
& ~EXTENT_CTLBITS
;
670 ret
= set_state_cb(tree
, state
, bits
);
673 if ((bits_to_set
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
674 u64 range
= state
->end
- state
->start
+ 1;
675 tree
->dirty_bytes
+= range
;
677 state
->state
|= bits_to_set
;
682 static void cache_state(struct extent_state
*state
,
683 struct extent_state
**cached_ptr
)
685 if (cached_ptr
&& !(*cached_ptr
)) {
686 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
688 atomic_inc(&state
->refs
);
694 * set some bits on a range in the tree. This may require allocations or
695 * sleeping, so the gfp mask is used to indicate what is allowed.
697 * If any of the exclusive bits are set, this will fail with -EEXIST if some
698 * part of the range already has the desired bits set. The start of the
699 * existing range is returned in failed_start in this case.
701 * [start, end] is inclusive This takes the tree lock.
704 int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
705 int bits
, int exclusive_bits
, u64
*failed_start
,
706 struct extent_state
**cached_state
, gfp_t mask
)
708 struct extent_state
*state
;
709 struct extent_state
*prealloc
= NULL
;
710 struct rb_node
*node
;
715 bits
|= EXTENT_FIRST_DELALLOC
;
717 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
718 prealloc
= alloc_extent_state(mask
);
723 spin_lock(&tree
->lock
);
724 if (cached_state
&& *cached_state
) {
725 state
= *cached_state
;
726 if (state
->start
== start
&& state
->tree
) {
727 node
= &state
->rb_node
;
732 * this search will find all the extents that end after
735 node
= tree_search(tree
, start
);
737 err
= insert_state(tree
, prealloc
, start
, end
, &bits
);
739 BUG_ON(err
== -EEXIST
);
742 state
= rb_entry(node
, struct extent_state
, rb_node
);
744 last_start
= state
->start
;
745 last_end
= state
->end
;
748 * | ---- desired range ---- |
751 * Just lock what we found and keep going
753 if (state
->start
== start
&& state
->end
<= end
) {
754 struct rb_node
*next_node
;
755 if (state
->state
& exclusive_bits
) {
756 *failed_start
= state
->start
;
761 err
= set_state_bits(tree
, state
, &bits
);
765 cache_state(state
, cached_state
);
766 merge_state(tree
, state
);
767 if (last_end
== (u64
)-1)
770 start
= last_end
+ 1;
771 if (start
< end
&& prealloc
&& !need_resched()) {
772 next_node
= rb_next(node
);
774 state
= rb_entry(next_node
, struct extent_state
,
776 if (state
->start
== start
)
784 * | ---- desired range ---- |
787 * | ------------- state -------------- |
789 * We need to split the extent we found, and may flip bits on
792 * If the extent we found extends past our
793 * range, we just split and search again. It'll get split
794 * again the next time though.
796 * If the extent we found is inside our range, we set the
799 if (state
->start
< start
) {
800 if (state
->state
& exclusive_bits
) {
801 *failed_start
= start
;
805 err
= split_state(tree
, state
, prealloc
, start
);
806 BUG_ON(err
== -EEXIST
);
810 if (state
->end
<= end
) {
811 err
= set_state_bits(tree
, state
, &bits
);
814 cache_state(state
, cached_state
);
815 merge_state(tree
, state
);
816 if (last_end
== (u64
)-1)
818 start
= last_end
+ 1;
823 * | ---- desired range ---- |
824 * | state | or | state |
826 * There's a hole, we need to insert something in it and
827 * ignore the extent we found.
829 if (state
->start
> start
) {
831 if (end
< last_start
)
834 this_end
= last_start
- 1;
835 err
= insert_state(tree
, prealloc
, start
, this_end
,
837 BUG_ON(err
== -EEXIST
);
842 cache_state(prealloc
, cached_state
);
844 start
= this_end
+ 1;
848 * | ---- desired range ---- |
850 * We need to split the extent, and set the bit
853 if (state
->start
<= end
&& state
->end
> end
) {
854 if (state
->state
& exclusive_bits
) {
855 *failed_start
= start
;
859 err
= split_state(tree
, state
, prealloc
, end
+ 1);
860 BUG_ON(err
== -EEXIST
);
862 err
= set_state_bits(tree
, prealloc
, &bits
);
867 cache_state(prealloc
, cached_state
);
868 merge_state(tree
, prealloc
);
876 spin_unlock(&tree
->lock
);
878 free_extent_state(prealloc
);
885 spin_unlock(&tree
->lock
);
886 if (mask
& __GFP_WAIT
)
891 /* wrappers around set/clear extent bit */
892 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
895 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, 0, NULL
,
899 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
900 int bits
, gfp_t mask
)
902 return set_extent_bit(tree
, start
, end
, bits
, 0, NULL
,
906 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
907 int bits
, gfp_t mask
)
909 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
912 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
913 struct extent_state
**cached_state
, gfp_t mask
)
915 return set_extent_bit(tree
, start
, end
,
916 EXTENT_DELALLOC
| EXTENT_DIRTY
| EXTENT_UPTODATE
,
917 0, NULL
, cached_state
, mask
);
920 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
923 return clear_extent_bit(tree
, start
, end
,
924 EXTENT_DIRTY
| EXTENT_DELALLOC
|
925 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, mask
);
928 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
931 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, NULL
,
935 static int clear_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
938 return clear_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, 0,
942 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
945 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, NULL
,
949 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
950 u64 end
, struct extent_state
**cached_state
,
953 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
957 int wait_on_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
959 return wait_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
);
963 * either insert or lock state struct between start and end use mask to tell
964 * us if waiting is desired.
966 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
967 int bits
, struct extent_state
**cached_state
, gfp_t mask
)
972 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
973 EXTENT_LOCKED
, &failed_start
,
975 if (err
== -EEXIST
&& (mask
& __GFP_WAIT
)) {
976 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
977 start
= failed_start
;
981 WARN_ON(start
> end
);
986 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
988 return lock_extent_bits(tree
, start
, end
, 0, NULL
, mask
);
991 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
997 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
998 &failed_start
, NULL
, mask
);
999 if (err
== -EEXIST
) {
1000 if (failed_start
> start
)
1001 clear_extent_bit(tree
, start
, failed_start
- 1,
1002 EXTENT_LOCKED
, 1, 0, NULL
, mask
);
1008 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1009 struct extent_state
**cached
, gfp_t mask
)
1011 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1015 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1018 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1023 * helper function to set pages and extents in the tree dirty
1025 int set_range_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1027 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1028 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1031 while (index
<= end_index
) {
1032 page
= find_get_page(tree
->mapping
, index
);
1034 __set_page_dirty_nobuffers(page
);
1035 page_cache_release(page
);
1042 * helper function to set both pages and extents in the tree writeback
1044 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1046 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1047 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1050 while (index
<= end_index
) {
1051 page
= find_get_page(tree
->mapping
, index
);
1053 set_page_writeback(page
);
1054 page_cache_release(page
);
1061 * find the first offset in the io tree with 'bits' set. zero is
1062 * returned if we find something, and *start_ret and *end_ret are
1063 * set to reflect the state struct that was found.
1065 * If nothing was found, 1 is returned, < 0 on error
1067 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1068 u64
*start_ret
, u64
*end_ret
, int bits
)
1070 struct rb_node
*node
;
1071 struct extent_state
*state
;
1074 spin_lock(&tree
->lock
);
1076 * this search will find all the extents that end after
1079 node
= tree_search(tree
, start
);
1084 state
= rb_entry(node
, struct extent_state
, rb_node
);
1085 if (state
->end
>= start
&& (state
->state
& bits
)) {
1086 *start_ret
= state
->start
;
1087 *end_ret
= state
->end
;
1091 node
= rb_next(node
);
1096 spin_unlock(&tree
->lock
);
1100 /* find the first state struct with 'bits' set after 'start', and
1101 * return it. tree->lock must be held. NULL will returned if
1102 * nothing was found after 'start'
1104 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1105 u64 start
, int bits
)
1107 struct rb_node
*node
;
1108 struct extent_state
*state
;
1111 * this search will find all the extents that end after
1114 node
= tree_search(tree
, start
);
1119 state
= rb_entry(node
, struct extent_state
, rb_node
);
1120 if (state
->end
>= start
&& (state
->state
& bits
))
1123 node
= rb_next(node
);
1132 * find a contiguous range of bytes in the file marked as delalloc, not
1133 * more than 'max_bytes'. start and end are used to return the range,
1135 * 1 is returned if we find something, 0 if nothing was in the tree
1137 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1138 u64
*start
, u64
*end
, u64 max_bytes
,
1139 struct extent_state
**cached_state
)
1141 struct rb_node
*node
;
1142 struct extent_state
*state
;
1143 u64 cur_start
= *start
;
1145 u64 total_bytes
= 0;
1147 spin_lock(&tree
->lock
);
1150 * this search will find all the extents that end after
1153 node
= tree_search(tree
, cur_start
);
1161 state
= rb_entry(node
, struct extent_state
, rb_node
);
1162 if (found
&& (state
->start
!= cur_start
||
1163 (state
->state
& EXTENT_BOUNDARY
))) {
1166 if (!(state
->state
& EXTENT_DELALLOC
)) {
1172 *start
= state
->start
;
1173 *cached_state
= state
;
1174 atomic_inc(&state
->refs
);
1178 cur_start
= state
->end
+ 1;
1179 node
= rb_next(node
);
1182 total_bytes
+= state
->end
- state
->start
+ 1;
1183 if (total_bytes
>= max_bytes
)
1187 spin_unlock(&tree
->lock
);
1191 static noinline
int __unlock_for_delalloc(struct inode
*inode
,
1192 struct page
*locked_page
,
1196 struct page
*pages
[16];
1197 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1198 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1199 unsigned long nr_pages
= end_index
- index
+ 1;
1202 if (index
== locked_page
->index
&& end_index
== index
)
1205 while (nr_pages
> 0) {
1206 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1207 min_t(unsigned long, nr_pages
,
1208 ARRAY_SIZE(pages
)), pages
);
1209 for (i
= 0; i
< ret
; i
++) {
1210 if (pages
[i
] != locked_page
)
1211 unlock_page(pages
[i
]);
1212 page_cache_release(pages
[i
]);
1221 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1222 struct page
*locked_page
,
1226 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1227 unsigned long start_index
= index
;
1228 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1229 unsigned long pages_locked
= 0;
1230 struct page
*pages
[16];
1231 unsigned long nrpages
;
1235 /* the caller is responsible for locking the start index */
1236 if (index
== locked_page
->index
&& index
== end_index
)
1239 /* skip the page at the start index */
1240 nrpages
= end_index
- index
+ 1;
1241 while (nrpages
> 0) {
1242 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1243 min_t(unsigned long,
1244 nrpages
, ARRAY_SIZE(pages
)), pages
);
1249 /* now we have an array of pages, lock them all */
1250 for (i
= 0; i
< ret
; i
++) {
1252 * the caller is taking responsibility for
1255 if (pages
[i
] != locked_page
) {
1256 lock_page(pages
[i
]);
1257 if (!PageDirty(pages
[i
]) ||
1258 pages
[i
]->mapping
!= inode
->i_mapping
) {
1260 unlock_page(pages
[i
]);
1261 page_cache_release(pages
[i
]);
1265 page_cache_release(pages
[i
]);
1274 if (ret
&& pages_locked
) {
1275 __unlock_for_delalloc(inode
, locked_page
,
1277 ((u64
)(start_index
+ pages_locked
- 1)) <<
1284 * find a contiguous range of bytes in the file marked as delalloc, not
1285 * more than 'max_bytes'. start and end are used to return the range,
1287 * 1 is returned if we find something, 0 if nothing was in the tree
1289 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1290 struct extent_io_tree
*tree
,
1291 struct page
*locked_page
,
1292 u64
*start
, u64
*end
,
1298 struct extent_state
*cached_state
= NULL
;
1303 /* step one, find a bunch of delalloc bytes starting at start */
1304 delalloc_start
= *start
;
1306 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1307 max_bytes
, &cached_state
);
1308 if (!found
|| delalloc_end
<= *start
) {
1309 *start
= delalloc_start
;
1310 *end
= delalloc_end
;
1311 free_extent_state(cached_state
);
1316 * start comes from the offset of locked_page. We have to lock
1317 * pages in order, so we can't process delalloc bytes before
1320 if (delalloc_start
< *start
)
1321 delalloc_start
= *start
;
1324 * make sure to limit the number of pages we try to lock down
1327 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1328 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1330 /* step two, lock all the pages after the page that has start */
1331 ret
= lock_delalloc_pages(inode
, locked_page
,
1332 delalloc_start
, delalloc_end
);
1333 if (ret
== -EAGAIN
) {
1334 /* some of the pages are gone, lets avoid looping by
1335 * shortening the size of the delalloc range we're searching
1337 free_extent_state(cached_state
);
1339 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1340 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1350 /* step three, lock the state bits for the whole range */
1351 lock_extent_bits(tree
, delalloc_start
, delalloc_end
,
1352 0, &cached_state
, GFP_NOFS
);
1354 /* then test to make sure it is all still delalloc */
1355 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1356 EXTENT_DELALLOC
, 1, cached_state
);
1358 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1359 &cached_state
, GFP_NOFS
);
1360 __unlock_for_delalloc(inode
, locked_page
,
1361 delalloc_start
, delalloc_end
);
1365 free_extent_state(cached_state
);
1366 *start
= delalloc_start
;
1367 *end
= delalloc_end
;
1372 int extent_clear_unlock_delalloc(struct inode
*inode
,
1373 struct extent_io_tree
*tree
,
1374 u64 start
, u64 end
, struct page
*locked_page
,
1378 struct page
*pages
[16];
1379 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1380 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1381 unsigned long nr_pages
= end_index
- index
+ 1;
1385 if (op
& EXTENT_CLEAR_UNLOCK
)
1386 clear_bits
|= EXTENT_LOCKED
;
1387 if (op
& EXTENT_CLEAR_DIRTY
)
1388 clear_bits
|= EXTENT_DIRTY
;
1390 if (op
& EXTENT_CLEAR_DELALLOC
)
1391 clear_bits
|= EXTENT_DELALLOC
;
1393 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1394 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1395 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1396 EXTENT_SET_PRIVATE2
)))
1399 while (nr_pages
> 0) {
1400 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1401 min_t(unsigned long,
1402 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1403 for (i
= 0; i
< ret
; i
++) {
1405 if (op
& EXTENT_SET_PRIVATE2
)
1406 SetPagePrivate2(pages
[i
]);
1408 if (pages
[i
] == locked_page
) {
1409 page_cache_release(pages
[i
]);
1412 if (op
& EXTENT_CLEAR_DIRTY
)
1413 clear_page_dirty_for_io(pages
[i
]);
1414 if (op
& EXTENT_SET_WRITEBACK
)
1415 set_page_writeback(pages
[i
]);
1416 if (op
& EXTENT_END_WRITEBACK
)
1417 end_page_writeback(pages
[i
]);
1418 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1419 unlock_page(pages
[i
]);
1420 page_cache_release(pages
[i
]);
1430 * count the number of bytes in the tree that have a given bit(s)
1431 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1432 * cached. The total number found is returned.
1434 u64
count_range_bits(struct extent_io_tree
*tree
,
1435 u64
*start
, u64 search_end
, u64 max_bytes
,
1438 struct rb_node
*node
;
1439 struct extent_state
*state
;
1440 u64 cur_start
= *start
;
1441 u64 total_bytes
= 0;
1444 if (search_end
<= cur_start
) {
1449 spin_lock(&tree
->lock
);
1450 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1451 total_bytes
= tree
->dirty_bytes
;
1455 * this search will find all the extents that end after
1458 node
= tree_search(tree
, cur_start
);
1463 state
= rb_entry(node
, struct extent_state
, rb_node
);
1464 if (state
->start
> search_end
)
1466 if (state
->end
>= cur_start
&& (state
->state
& bits
)) {
1467 total_bytes
+= min(search_end
, state
->end
) + 1 -
1468 max(cur_start
, state
->start
);
1469 if (total_bytes
>= max_bytes
)
1472 *start
= state
->start
;
1476 node
= rb_next(node
);
1481 spin_unlock(&tree
->lock
);
1486 * set the private field for a given byte offset in the tree. If there isn't
1487 * an extent_state there already, this does nothing.
1489 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1491 struct rb_node
*node
;
1492 struct extent_state
*state
;
1495 spin_lock(&tree
->lock
);
1497 * this search will find all the extents that end after
1500 node
= tree_search(tree
, start
);
1505 state
= rb_entry(node
, struct extent_state
, rb_node
);
1506 if (state
->start
!= start
) {
1510 state
->private = private;
1512 spin_unlock(&tree
->lock
);
1516 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1518 struct rb_node
*node
;
1519 struct extent_state
*state
;
1522 spin_lock(&tree
->lock
);
1524 * this search will find all the extents that end after
1527 node
= tree_search(tree
, start
);
1532 state
= rb_entry(node
, struct extent_state
, rb_node
);
1533 if (state
->start
!= start
) {
1537 *private = state
->private;
1539 spin_unlock(&tree
->lock
);
1544 * searches a range in the state tree for a given mask.
1545 * If 'filled' == 1, this returns 1 only if every extent in the tree
1546 * has the bits set. Otherwise, 1 is returned if any bit in the
1547 * range is found set.
1549 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1550 int bits
, int filled
, struct extent_state
*cached
)
1552 struct extent_state
*state
= NULL
;
1553 struct rb_node
*node
;
1556 spin_lock(&tree
->lock
);
1557 if (cached
&& cached
->tree
&& cached
->start
== start
)
1558 node
= &cached
->rb_node
;
1560 node
= tree_search(tree
, start
);
1561 while (node
&& start
<= end
) {
1562 state
= rb_entry(node
, struct extent_state
, rb_node
);
1564 if (filled
&& state
->start
> start
) {
1569 if (state
->start
> end
)
1572 if (state
->state
& bits
) {
1576 } else if (filled
) {
1581 if (state
->end
== (u64
)-1)
1584 start
= state
->end
+ 1;
1587 node
= rb_next(node
);
1594 spin_unlock(&tree
->lock
);
1599 * helper function to set a given page up to date if all the
1600 * extents in the tree for that page are up to date
1602 static int check_page_uptodate(struct extent_io_tree
*tree
,
1605 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1606 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1607 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1608 SetPageUptodate(page
);
1613 * helper function to unlock a page if all the extents in the tree
1614 * for that page are unlocked
1616 static int check_page_locked(struct extent_io_tree
*tree
,
1619 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1620 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1621 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1627 * helper function to end page writeback if all the extents
1628 * in the tree for that page are done with writeback
1630 static int check_page_writeback(struct extent_io_tree
*tree
,
1633 end_page_writeback(page
);
1637 /* lots and lots of room for performance fixes in the end_bio funcs */
1640 * after a writepage IO is done, we need to:
1641 * clear the uptodate bits on error
1642 * clear the writeback bits in the extent tree for this IO
1643 * end_page_writeback if the page has no more pending IO
1645 * Scheduling is not allowed, so the extent state tree is expected
1646 * to have one and only one object corresponding to this IO.
1648 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
1650 int uptodate
= err
== 0;
1651 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1652 struct extent_io_tree
*tree
;
1659 struct page
*page
= bvec
->bv_page
;
1660 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1662 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1664 end
= start
+ bvec
->bv_len
- 1;
1666 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1671 if (--bvec
>= bio
->bi_io_vec
)
1672 prefetchw(&bvec
->bv_page
->flags
);
1673 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
1674 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
1675 end
, NULL
, uptodate
);
1680 if (!uptodate
&& tree
->ops
&&
1681 tree
->ops
->writepage_io_failed_hook
) {
1682 ret
= tree
->ops
->writepage_io_failed_hook(bio
, page
,
1685 uptodate
= (err
== 0);
1691 clear_extent_uptodate(tree
, start
, end
, NULL
, GFP_NOFS
);
1692 ClearPageUptodate(page
);
1697 end_page_writeback(page
);
1699 check_page_writeback(tree
, page
);
1700 } while (bvec
>= bio
->bi_io_vec
);
1706 * after a readpage IO is done, we need to:
1707 * clear the uptodate bits on error
1708 * set the uptodate bits if things worked
1709 * set the page up to date if all extents in the tree are uptodate
1710 * clear the lock bit in the extent tree
1711 * unlock the page if there are no other extents locked for it
1713 * Scheduling is not allowed, so the extent state tree is expected
1714 * to have one and only one object corresponding to this IO.
1716 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
1718 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1719 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1720 struct bio_vec
*bvec
= bio
->bi_io_vec
;
1721 struct extent_io_tree
*tree
;
1731 struct page
*page
= bvec
->bv_page
;
1732 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1734 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1736 end
= start
+ bvec
->bv_len
- 1;
1738 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1743 if (++bvec
<= bvec_end
)
1744 prefetchw(&bvec
->bv_page
->flags
);
1746 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
1747 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
1752 if (!uptodate
&& tree
->ops
&&
1753 tree
->ops
->readpage_io_failed_hook
) {
1754 ret
= tree
->ops
->readpage_io_failed_hook(bio
, page
,
1758 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1766 set_extent_uptodate(tree
, start
, end
,
1769 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1773 SetPageUptodate(page
);
1775 ClearPageUptodate(page
);
1781 check_page_uptodate(tree
, page
);
1783 ClearPageUptodate(page
);
1786 check_page_locked(tree
, page
);
1788 } while (bvec
<= bvec_end
);
1794 * IO done from prepare_write is pretty simple, we just unlock
1795 * the structs in the extent tree when done, and set the uptodate bits
1798 static void end_bio_extent_preparewrite(struct bio
*bio
, int err
)
1800 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1801 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1802 struct extent_io_tree
*tree
;
1807 struct page
*page
= bvec
->bv_page
;
1808 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1810 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1812 end
= start
+ bvec
->bv_len
- 1;
1814 if (--bvec
>= bio
->bi_io_vec
)
1815 prefetchw(&bvec
->bv_page
->flags
);
1818 set_extent_uptodate(tree
, start
, end
, GFP_ATOMIC
);
1820 ClearPageUptodate(page
);
1824 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1826 } while (bvec
>= bio
->bi_io_vec
);
1832 btrfs_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
1837 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1839 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
1840 while (!bio
&& (nr_vecs
/= 2))
1841 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1846 bio
->bi_bdev
= bdev
;
1847 bio
->bi_sector
= first_sector
;
1852 static int submit_one_bio(int rw
, struct bio
*bio
, int mirror_num
,
1853 unsigned long bio_flags
)
1856 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1857 struct page
*page
= bvec
->bv_page
;
1858 struct extent_io_tree
*tree
= bio
->bi_private
;
1861 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
1863 bio
->bi_private
= NULL
;
1867 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
1868 ret
= tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
1869 mirror_num
, bio_flags
, start
);
1871 submit_bio(rw
, bio
);
1872 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
1878 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
1879 struct page
*page
, sector_t sector
,
1880 size_t size
, unsigned long offset
,
1881 struct block_device
*bdev
,
1882 struct bio
**bio_ret
,
1883 unsigned long max_pages
,
1884 bio_end_io_t end_io_func
,
1886 unsigned long prev_bio_flags
,
1887 unsigned long bio_flags
)
1893 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
1894 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
1895 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
1897 if (bio_ret
&& *bio_ret
) {
1900 contig
= bio
->bi_sector
== sector
;
1902 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
1905 if (prev_bio_flags
!= bio_flags
|| !contig
||
1906 (tree
->ops
&& tree
->ops
->merge_bio_hook
&&
1907 tree
->ops
->merge_bio_hook(page
, offset
, page_size
, bio
,
1909 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
1910 ret
= submit_one_bio(rw
, bio
, mirror_num
,
1917 if (this_compressed
)
1920 nr
= bio_get_nr_vecs(bdev
);
1922 bio
= btrfs_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
1926 bio_add_page(bio
, page
, page_size
, offset
);
1927 bio
->bi_end_io
= end_io_func
;
1928 bio
->bi_private
= tree
;
1933 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
1938 void set_page_extent_mapped(struct page
*page
)
1940 if (!PagePrivate(page
)) {
1941 SetPagePrivate(page
);
1942 page_cache_get(page
);
1943 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
1947 static void set_page_extent_head(struct page
*page
, unsigned long len
)
1949 set_page_private(page
, EXTENT_PAGE_PRIVATE_FIRST_PAGE
| len
<< 2);
1953 * basic readpage implementation. Locked extent state structs are inserted
1954 * into the tree that are removed when the IO is done (by the end_io
1957 static int __extent_read_full_page(struct extent_io_tree
*tree
,
1959 get_extent_t
*get_extent
,
1960 struct bio
**bio
, int mirror_num
,
1961 unsigned long *bio_flags
)
1963 struct inode
*inode
= page
->mapping
->host
;
1964 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1965 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
1969 u64 last_byte
= i_size_read(inode
);
1973 struct extent_map
*em
;
1974 struct block_device
*bdev
;
1975 struct btrfs_ordered_extent
*ordered
;
1978 size_t page_offset
= 0;
1980 size_t disk_io_size
;
1981 size_t blocksize
= inode
->i_sb
->s_blocksize
;
1982 unsigned long this_bio_flag
= 0;
1984 set_page_extent_mapped(page
);
1988 lock_extent(tree
, start
, end
, GFP_NOFS
);
1989 ordered
= btrfs_lookup_ordered_extent(inode
, start
);
1992 unlock_extent(tree
, start
, end
, GFP_NOFS
);
1993 btrfs_start_ordered_extent(inode
, ordered
, 1);
1994 btrfs_put_ordered_extent(ordered
);
1997 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
1999 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
2002 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
2003 userpage
= kmap_atomic(page
, KM_USER0
);
2004 memset(userpage
+ zero_offset
, 0, iosize
);
2005 flush_dcache_page(page
);
2006 kunmap_atomic(userpage
, KM_USER0
);
2009 while (cur
<= end
) {
2010 if (cur
>= last_byte
) {
2012 iosize
= PAGE_CACHE_SIZE
- page_offset
;
2013 userpage
= kmap_atomic(page
, KM_USER0
);
2014 memset(userpage
+ page_offset
, 0, iosize
);
2015 flush_dcache_page(page
);
2016 kunmap_atomic(userpage
, KM_USER0
);
2017 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2019 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2022 em
= get_extent(inode
, page
, page_offset
, cur
,
2024 if (IS_ERR(em
) || !em
) {
2026 unlock_extent(tree
, cur
, end
, GFP_NOFS
);
2029 extent_offset
= cur
- em
->start
;
2030 BUG_ON(extent_map_end(em
) <= cur
);
2033 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
2034 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2035 extent_set_compress_type(&this_bio_flag
,
2039 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2040 cur_end
= min(extent_map_end(em
) - 1, end
);
2041 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2042 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2043 disk_io_size
= em
->block_len
;
2044 sector
= em
->block_start
>> 9;
2046 sector
= (em
->block_start
+ extent_offset
) >> 9;
2047 disk_io_size
= iosize
;
2050 block_start
= em
->block_start
;
2051 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2052 block_start
= EXTENT_MAP_HOLE
;
2053 free_extent_map(em
);
2056 /* we've found a hole, just zero and go on */
2057 if (block_start
== EXTENT_MAP_HOLE
) {
2059 userpage
= kmap_atomic(page
, KM_USER0
);
2060 memset(userpage
+ page_offset
, 0, iosize
);
2061 flush_dcache_page(page
);
2062 kunmap_atomic(userpage
, KM_USER0
);
2064 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2066 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2068 page_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 page_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 page_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
, page_offset
,
2102 end_bio_extent_readpage
, mirror_num
,
2106 *bio_flags
= this_bio_flag
;
2111 page_offset
+= iosize
;
2114 if (!PageError(page
))
2115 SetPageUptodate(page
);
2121 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2122 get_extent_t
*get_extent
)
2124 struct bio
*bio
= NULL
;
2125 unsigned long bio_flags
= 0;
2128 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, 0,
2131 ret
= submit_one_bio(READ
, bio
, 0, bio_flags
);
2135 static noinline
void update_nr_written(struct page
*page
,
2136 struct writeback_control
*wbc
,
2137 unsigned long nr_written
)
2139 wbc
->nr_to_write
-= nr_written
;
2140 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2141 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2142 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2146 * the writepage semantics are similar to regular writepage. extent
2147 * records are inserted to lock ranges in the tree, and as dirty areas
2148 * are found, they are marked writeback. Then the lock bits are removed
2149 * and the end_io handler clears the writeback ranges
2151 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2154 struct inode
*inode
= page
->mapping
->host
;
2155 struct extent_page_data
*epd
= data
;
2156 struct extent_io_tree
*tree
= epd
->tree
;
2157 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2159 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2163 u64 last_byte
= i_size_read(inode
);
2167 struct extent_state
*cached_state
= NULL
;
2168 struct extent_map
*em
;
2169 struct block_device
*bdev
;
2172 size_t pg_offset
= 0;
2174 loff_t i_size
= i_size_read(inode
);
2175 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2181 unsigned long nr_written
= 0;
2183 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2184 write_flags
= WRITE_SYNC_PLUG
;
2186 write_flags
= WRITE
;
2188 WARN_ON(!PageLocked(page
));
2189 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2190 if (page
->index
> end_index
||
2191 (page
->index
== end_index
&& !pg_offset
)) {
2192 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2197 if (page
->index
== end_index
) {
2200 userpage
= kmap_atomic(page
, KM_USER0
);
2201 memset(userpage
+ pg_offset
, 0,
2202 PAGE_CACHE_SIZE
- pg_offset
);
2203 kunmap_atomic(userpage
, KM_USER0
);
2204 flush_dcache_page(page
);
2208 set_page_extent_mapped(page
);
2210 delalloc_start
= start
;
2213 if (!epd
->extent_locked
) {
2214 u64 delalloc_to_write
= 0;
2216 * make sure the wbc mapping index is at least updated
2219 update_nr_written(page
, wbc
, 0);
2221 while (delalloc_end
< page_end
) {
2222 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2227 if (nr_delalloc
== 0) {
2228 delalloc_start
= delalloc_end
+ 1;
2231 tree
->ops
->fill_delalloc(inode
, page
, delalloc_start
,
2232 delalloc_end
, &page_started
,
2235 * delalloc_end is already one less than the total
2236 * length, so we don't subtract one from
2239 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2242 delalloc_start
= delalloc_end
+ 1;
2244 if (wbc
->nr_to_write
< delalloc_to_write
) {
2247 if (delalloc_to_write
< thresh
* 2)
2248 thresh
= delalloc_to_write
;
2249 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2253 /* did the fill delalloc function already unlock and start
2259 * we've unlocked the page, so we can't update
2260 * the mapping's writeback index, just update
2263 wbc
->nr_to_write
-= nr_written
;
2267 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2268 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2270 if (ret
== -EAGAIN
) {
2271 redirty_page_for_writepage(wbc
, page
);
2272 update_nr_written(page
, wbc
, nr_written
);
2280 * we don't want to touch the inode after unlocking the page,
2281 * so we update the mapping writeback index now
2283 update_nr_written(page
, wbc
, nr_written
+ 1);
2286 if (last_byte
<= start
) {
2287 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2288 tree
->ops
->writepage_end_io_hook(page
, start
,
2293 blocksize
= inode
->i_sb
->s_blocksize
;
2295 while (cur
<= end
) {
2296 if (cur
>= last_byte
) {
2297 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2298 tree
->ops
->writepage_end_io_hook(page
, cur
,
2302 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2304 if (IS_ERR(em
) || !em
) {
2309 extent_offset
= cur
- em
->start
;
2310 BUG_ON(extent_map_end(em
) <= cur
);
2312 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2313 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2314 sector
= (em
->block_start
+ extent_offset
) >> 9;
2316 block_start
= em
->block_start
;
2317 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2318 free_extent_map(em
);
2322 * compressed and inline extents are written through other
2325 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2326 block_start
== EXTENT_MAP_INLINE
) {
2328 * end_io notification does not happen here for
2329 * compressed extents
2331 if (!compressed
&& tree
->ops
&&
2332 tree
->ops
->writepage_end_io_hook
)
2333 tree
->ops
->writepage_end_io_hook(page
, cur
,
2336 else if (compressed
) {
2337 /* we don't want to end_page_writeback on
2338 * a compressed extent. this happens
2345 pg_offset
+= iosize
;
2348 /* leave this out until we have a page_mkwrite call */
2349 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2350 EXTENT_DIRTY
, 0, NULL
)) {
2352 pg_offset
+= iosize
;
2356 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2357 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
2365 unsigned long max_nr
= end_index
+ 1;
2367 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
2368 if (!PageWriteback(page
)) {
2369 printk(KERN_ERR
"btrfs warning page %lu not "
2370 "writeback, cur %llu end %llu\n",
2371 page
->index
, (unsigned long long)cur
,
2372 (unsigned long long)end
);
2375 ret
= submit_extent_page(write_flags
, tree
, page
,
2376 sector
, iosize
, pg_offset
,
2377 bdev
, &epd
->bio
, max_nr
,
2378 end_bio_extent_writepage
,
2384 pg_offset
+= iosize
;
2389 /* make sure the mapping tag for page dirty gets cleared */
2390 set_page_writeback(page
);
2391 end_page_writeback(page
);
2397 /* drop our reference on any cached states */
2398 free_extent_state(cached_state
);
2403 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2404 * @mapping: address space structure to write
2405 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2406 * @writepage: function called for each page
2407 * @data: data passed to writepage function
2409 * If a page is already under I/O, write_cache_pages() skips it, even
2410 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2411 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2412 * and msync() need to guarantee that all the data which was dirty at the time
2413 * the call was made get new I/O started against them. If wbc->sync_mode is
2414 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2415 * existing IO to complete.
2417 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
2418 struct address_space
*mapping
,
2419 struct writeback_control
*wbc
,
2420 writepage_t writepage
, void *data
,
2421 void (*flush_fn
)(void *))
2425 int nr_to_write_done
= 0;
2426 struct pagevec pvec
;
2429 pgoff_t end
; /* Inclusive */
2432 pagevec_init(&pvec
, 0);
2433 if (wbc
->range_cyclic
) {
2434 index
= mapping
->writeback_index
; /* Start from prev offset */
2437 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2438 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2442 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
2443 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
2444 PAGECACHE_TAG_DIRTY
, min(end
- index
,
2445 (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
2449 for (i
= 0; i
< nr_pages
; i
++) {
2450 struct page
*page
= pvec
.pages
[i
];
2453 * At this point we hold neither mapping->tree_lock nor
2454 * lock on the page itself: the page may be truncated or
2455 * invalidated (changing page->mapping to NULL), or even
2456 * swizzled back from swapper_space to tmpfs file
2459 if (tree
->ops
&& tree
->ops
->write_cache_pages_lock_hook
)
2460 tree
->ops
->write_cache_pages_lock_hook(page
);
2464 if (unlikely(page
->mapping
!= mapping
)) {
2469 if (!wbc
->range_cyclic
&& page
->index
> end
) {
2475 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
2476 if (PageWriteback(page
))
2478 wait_on_page_writeback(page
);
2481 if (PageWriteback(page
) ||
2482 !clear_page_dirty_for_io(page
)) {
2487 ret
= (*writepage
)(page
, wbc
, data
);
2489 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
2497 * the filesystem may choose to bump up nr_to_write.
2498 * We have to make sure to honor the new nr_to_write
2501 nr_to_write_done
= wbc
->nr_to_write
<= 0;
2503 pagevec_release(&pvec
);
2506 if (!scanned
&& !done
) {
2508 * We hit the last page and there is more work to be done: wrap
2509 * back to the start of the file
2518 static void flush_epd_write_bio(struct extent_page_data
*epd
)
2522 submit_one_bio(WRITE_SYNC
, epd
->bio
, 0, 0);
2524 submit_one_bio(WRITE
, epd
->bio
, 0, 0);
2529 static noinline
void flush_write_bio(void *data
)
2531 struct extent_page_data
*epd
= data
;
2532 flush_epd_write_bio(epd
);
2535 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2536 get_extent_t
*get_extent
,
2537 struct writeback_control
*wbc
)
2540 struct address_space
*mapping
= page
->mapping
;
2541 struct extent_page_data epd
= {
2544 .get_extent
= get_extent
,
2546 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2548 struct writeback_control wbc_writepages
= {
2549 .sync_mode
= wbc
->sync_mode
,
2550 .older_than_this
= NULL
,
2552 .range_start
= page_offset(page
) + PAGE_CACHE_SIZE
,
2553 .range_end
= (loff_t
)-1,
2556 ret
= __extent_writepage(page
, wbc
, &epd
);
2558 extent_write_cache_pages(tree
, mapping
, &wbc_writepages
,
2559 __extent_writepage
, &epd
, flush_write_bio
);
2560 flush_epd_write_bio(&epd
);
2564 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
2565 u64 start
, u64 end
, get_extent_t
*get_extent
,
2569 struct address_space
*mapping
= inode
->i_mapping
;
2571 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
2574 struct extent_page_data epd
= {
2577 .get_extent
= get_extent
,
2579 .sync_io
= mode
== WB_SYNC_ALL
,
2581 struct writeback_control wbc_writepages
= {
2583 .older_than_this
= NULL
,
2584 .nr_to_write
= nr_pages
* 2,
2585 .range_start
= start
,
2586 .range_end
= end
+ 1,
2589 while (start
<= end
) {
2590 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
2591 if (clear_page_dirty_for_io(page
))
2592 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
2594 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2595 tree
->ops
->writepage_end_io_hook(page
, start
,
2596 start
+ PAGE_CACHE_SIZE
- 1,
2600 page_cache_release(page
);
2601 start
+= PAGE_CACHE_SIZE
;
2604 flush_epd_write_bio(&epd
);
2608 int extent_writepages(struct extent_io_tree
*tree
,
2609 struct address_space
*mapping
,
2610 get_extent_t
*get_extent
,
2611 struct writeback_control
*wbc
)
2614 struct extent_page_data epd
= {
2617 .get_extent
= get_extent
,
2619 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2622 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
2623 __extent_writepage
, &epd
,
2625 flush_epd_write_bio(&epd
);
2629 int extent_readpages(struct extent_io_tree
*tree
,
2630 struct address_space
*mapping
,
2631 struct list_head
*pages
, unsigned nr_pages
,
2632 get_extent_t get_extent
)
2634 struct bio
*bio
= NULL
;
2636 unsigned long bio_flags
= 0;
2638 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
2639 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
2641 prefetchw(&page
->flags
);
2642 list_del(&page
->lru
);
2643 if (!add_to_page_cache_lru(page
, mapping
,
2644 page
->index
, GFP_KERNEL
)) {
2645 __extent_read_full_page(tree
, page
, get_extent
,
2646 &bio
, 0, &bio_flags
);
2648 page_cache_release(page
);
2650 BUG_ON(!list_empty(pages
));
2652 submit_one_bio(READ
, bio
, 0, bio_flags
);
2657 * basic invalidatepage code, this waits on any locked or writeback
2658 * ranges corresponding to the page, and then deletes any extent state
2659 * records from the tree
2661 int extent_invalidatepage(struct extent_io_tree
*tree
,
2662 struct page
*page
, unsigned long offset
)
2664 struct extent_state
*cached_state
= NULL
;
2665 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
2666 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2667 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
2669 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
2673 lock_extent_bits(tree
, start
, end
, 0, &cached_state
, GFP_NOFS
);
2674 wait_on_page_writeback(page
);
2675 clear_extent_bit(tree
, start
, end
,
2676 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
2677 EXTENT_DO_ACCOUNTING
,
2678 1, 1, &cached_state
, GFP_NOFS
);
2683 * simple commit_write call, set_range_dirty is used to mark both
2684 * the pages and the extent records as dirty
2686 int extent_commit_write(struct extent_io_tree
*tree
,
2687 struct inode
*inode
, struct page
*page
,
2688 unsigned from
, unsigned to
)
2690 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
2692 set_page_extent_mapped(page
);
2693 set_page_dirty(page
);
2695 if (pos
> inode
->i_size
) {
2696 i_size_write(inode
, pos
);
2697 mark_inode_dirty(inode
);
2702 int extent_prepare_write(struct extent_io_tree
*tree
,
2703 struct inode
*inode
, struct page
*page
,
2704 unsigned from
, unsigned to
, get_extent_t
*get_extent
)
2706 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2707 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2709 u64 orig_block_start
;
2712 struct extent_map
*em
;
2713 unsigned blocksize
= 1 << inode
->i_blkbits
;
2714 size_t page_offset
= 0;
2715 size_t block_off_start
;
2716 size_t block_off_end
;
2722 set_page_extent_mapped(page
);
2724 block_start
= (page_start
+ from
) & ~((u64
)blocksize
- 1);
2725 block_end
= (page_start
+ to
- 1) | (blocksize
- 1);
2726 orig_block_start
= block_start
;
2728 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2729 while (block_start
<= block_end
) {
2730 em
= get_extent(inode
, page
, page_offset
, block_start
,
2731 block_end
- block_start
+ 1, 1);
2732 if (IS_ERR(em
) || !em
)
2735 cur_end
= min(block_end
, extent_map_end(em
) - 1);
2736 block_off_start
= block_start
& (PAGE_CACHE_SIZE
- 1);
2737 block_off_end
= block_off_start
+ blocksize
;
2738 isnew
= clear_extent_new(tree
, block_start
, cur_end
, GFP_NOFS
);
2740 if (!PageUptodate(page
) && isnew
&&
2741 (block_off_end
> to
|| block_off_start
< from
)) {
2744 kaddr
= kmap_atomic(page
, KM_USER0
);
2745 if (block_off_end
> to
)
2746 memset(kaddr
+ to
, 0, block_off_end
- to
);
2747 if (block_off_start
< from
)
2748 memset(kaddr
+ block_off_start
, 0,
2749 from
- block_off_start
);
2750 flush_dcache_page(page
);
2751 kunmap_atomic(kaddr
, KM_USER0
);
2753 if ((em
->block_start
!= EXTENT_MAP_HOLE
&&
2754 em
->block_start
!= EXTENT_MAP_INLINE
) &&
2755 !isnew
&& !PageUptodate(page
) &&
2756 (block_off_end
> to
|| block_off_start
< from
) &&
2757 !test_range_bit(tree
, block_start
, cur_end
,
2758 EXTENT_UPTODATE
, 1, NULL
)) {
2760 u64 extent_offset
= block_start
- em
->start
;
2762 sector
= (em
->block_start
+ extent_offset
) >> 9;
2763 iosize
= (cur_end
- block_start
+ blocksize
) &
2764 ~((u64
)blocksize
- 1);
2766 * we've already got the extent locked, but we
2767 * need to split the state such that our end_bio
2768 * handler can clear the lock.
2770 set_extent_bit(tree
, block_start
,
2771 block_start
+ iosize
- 1,
2772 EXTENT_LOCKED
, 0, NULL
, NULL
, GFP_NOFS
);
2773 ret
= submit_extent_page(READ
, tree
, page
,
2774 sector
, iosize
, page_offset
, em
->bdev
,
2776 end_bio_extent_preparewrite
, 0,
2781 block_start
= block_start
+ iosize
;
2783 set_extent_uptodate(tree
, block_start
, cur_end
,
2785 unlock_extent(tree
, block_start
, cur_end
, GFP_NOFS
);
2786 block_start
= cur_end
+ 1;
2788 page_offset
= block_start
& (PAGE_CACHE_SIZE
- 1);
2789 free_extent_map(em
);
2792 wait_extent_bit(tree
, orig_block_start
,
2793 block_end
, EXTENT_LOCKED
);
2795 check_page_uptodate(tree
, page
);
2797 /* FIXME, zero out newly allocated blocks on error */
2802 * a helper for releasepage, this tests for areas of the page that
2803 * are locked or under IO and drops the related state bits if it is safe
2806 int try_release_extent_state(struct extent_map_tree
*map
,
2807 struct extent_io_tree
*tree
, struct page
*page
,
2810 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2811 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2814 if (test_range_bit(tree
, start
, end
,
2815 EXTENT_IOBITS
, 0, NULL
))
2818 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
2821 * at this point we can safely clear everything except the
2822 * locked bit and the nodatasum bit
2824 clear_extent_bit(tree
, start
, end
,
2825 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
2832 * a helper for releasepage. As long as there are no locked extents
2833 * in the range corresponding to the page, both state records and extent
2834 * map records are removed
2836 int try_release_extent_mapping(struct extent_map_tree
*map
,
2837 struct extent_io_tree
*tree
, struct page
*page
,
2840 struct extent_map
*em
;
2841 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2842 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2844 if ((mask
& __GFP_WAIT
) &&
2845 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
2847 while (start
<= end
) {
2848 len
= end
- start
+ 1;
2849 write_lock(&map
->lock
);
2850 em
= lookup_extent_mapping(map
, start
, len
);
2851 if (!em
|| IS_ERR(em
)) {
2852 write_unlock(&map
->lock
);
2855 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
2856 em
->start
!= start
) {
2857 write_unlock(&map
->lock
);
2858 free_extent_map(em
);
2861 if (!test_range_bit(tree
, em
->start
,
2862 extent_map_end(em
) - 1,
2863 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
2865 remove_extent_mapping(map
, em
);
2866 /* once for the rb tree */
2867 free_extent_map(em
);
2869 start
= extent_map_end(em
);
2870 write_unlock(&map
->lock
);
2873 free_extent_map(em
);
2876 return try_release_extent_state(map
, tree
, page
, mask
);
2879 sector_t
extent_bmap(struct address_space
*mapping
, sector_t iblock
,
2880 get_extent_t
*get_extent
)
2882 struct inode
*inode
= mapping
->host
;
2883 struct extent_state
*cached_state
= NULL
;
2884 u64 start
= iblock
<< inode
->i_blkbits
;
2885 sector_t sector
= 0;
2886 size_t blksize
= (1 << inode
->i_blkbits
);
2887 struct extent_map
*em
;
2889 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2890 0, &cached_state
, GFP_NOFS
);
2891 em
= get_extent(inode
, NULL
, 0, start
, blksize
, 0);
2892 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
,
2893 start
+ blksize
- 1, &cached_state
, GFP_NOFS
);
2894 if (!em
|| IS_ERR(em
))
2897 if (em
->block_start
> EXTENT_MAP_LAST_BYTE
)
2900 sector
= (em
->block_start
+ start
- em
->start
) >> inode
->i_blkbits
;
2902 free_extent_map(em
);
2906 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
2907 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
2911 u64 max
= start
+ len
;
2916 struct btrfs_key found_key
;
2917 struct extent_map
*em
= NULL
;
2918 struct extent_state
*cached_state
= NULL
;
2919 struct btrfs_path
*path
;
2920 struct btrfs_file_extent_item
*item
;
2922 u64 em_start
= 0, em_len
= 0;
2923 unsigned long emflags
;
2929 path
= btrfs_alloc_path();
2932 path
->leave_spinning
= 1;
2934 ret
= btrfs_lookup_file_extent(NULL
, BTRFS_I(inode
)->root
,
2935 path
, inode
->i_ino
, -1, 0);
2937 btrfs_free_path(path
);
2942 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2943 struct btrfs_file_extent_item
);
2944 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, path
->slots
[0]);
2945 found_type
= btrfs_key_type(&found_key
);
2947 /* No extents, just return */
2948 if (found_key
.objectid
!= inode
->i_ino
||
2949 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2950 btrfs_free_path(path
);
2953 last
= found_key
.offset
;
2954 btrfs_free_path(path
);
2956 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
2957 &cached_state
, GFP_NOFS
);
2958 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
2968 off
= em
->start
+ em
->len
;
2972 if (em
->block_start
== EXTENT_MAP_HOLE
) {
2977 em_start
= em
->start
;
2983 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
2985 flags
|= FIEMAP_EXTENT_LAST
;
2986 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
2987 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
2988 FIEMAP_EXTENT_NOT_ALIGNED
);
2989 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
2990 flags
|= (FIEMAP_EXTENT_DELALLOC
|
2991 FIEMAP_EXTENT_UNKNOWN
);
2993 disko
= em
->block_start
;
2995 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
2996 flags
|= FIEMAP_EXTENT_ENCODED
;
2999 emflags
= em
->flags
;
3000 free_extent_map(em
);
3003 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
3010 emflags
= em
->flags
;
3013 if (test_bit(EXTENT_FLAG_VACANCY
, &emflags
)) {
3014 flags
|= FIEMAP_EXTENT_LAST
;
3018 if (em_start
== last
) {
3019 flags
|= FIEMAP_EXTENT_LAST
;
3024 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3031 free_extent_map(em
);
3033 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3034 &cached_state
, GFP_NOFS
);
3038 static inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
3042 struct address_space
*mapping
;
3045 return eb
->first_page
;
3046 i
+= eb
->start
>> PAGE_CACHE_SHIFT
;
3047 mapping
= eb
->first_page
->mapping
;
3052 * extent_buffer_page is only called after pinning the page
3053 * by increasing the reference count. So we know the page must
3054 * be in the radix tree.
3057 p
= radix_tree_lookup(&mapping
->page_tree
, i
);
3063 static inline unsigned long num_extent_pages(u64 start
, u64 len
)
3065 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
3066 (start
>> PAGE_CACHE_SHIFT
);
3069 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3074 struct extent_buffer
*eb
= NULL
;
3076 unsigned long flags
;
3079 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
3084 spin_lock_init(&eb
->lock
);
3085 init_waitqueue_head(&eb
->lock_wq
);
3088 spin_lock_irqsave(&leak_lock
, flags
);
3089 list_add(&eb
->leak_list
, &buffers
);
3090 spin_unlock_irqrestore(&leak_lock
, flags
);
3092 atomic_set(&eb
->refs
, 1);
3097 static void __free_extent_buffer(struct extent_buffer
*eb
)
3100 unsigned long flags
;
3101 spin_lock_irqsave(&leak_lock
, flags
);
3102 list_del(&eb
->leak_list
);
3103 spin_unlock_irqrestore(&leak_lock
, flags
);
3105 kmem_cache_free(extent_buffer_cache
, eb
);
3109 * Helper for releasing extent buffer page.
3111 static void btrfs_release_extent_buffer_page(struct extent_buffer
*eb
,
3112 unsigned long start_idx
)
3114 unsigned long index
;
3117 if (!eb
->first_page
)
3120 index
= num_extent_pages(eb
->start
, eb
->len
);
3121 if (start_idx
>= index
)
3126 page
= extent_buffer_page(eb
, index
);
3128 page_cache_release(page
);
3129 } while (index
!= start_idx
);
3133 * Helper for releasing the extent buffer.
3135 static inline void btrfs_release_extent_buffer(struct extent_buffer
*eb
)
3137 btrfs_release_extent_buffer_page(eb
, 0);
3138 __free_extent_buffer(eb
);
3141 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
3142 u64 start
, unsigned long len
,
3146 unsigned long num_pages
= num_extent_pages(start
, len
);
3148 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
3149 struct extent_buffer
*eb
;
3150 struct extent_buffer
*exists
= NULL
;
3152 struct address_space
*mapping
= tree
->mapping
;
3157 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3158 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
3160 mark_page_accessed(eb
->first_page
);
3165 eb
= __alloc_extent_buffer(tree
, start
, len
, mask
);
3170 eb
->first_page
= page0
;
3173 page_cache_get(page0
);
3174 mark_page_accessed(page0
);
3175 set_page_extent_mapped(page0
);
3176 set_page_extent_head(page0
, len
);
3177 uptodate
= PageUptodate(page0
);
3181 for (; i
< num_pages
; i
++, index
++) {
3182 p
= find_or_create_page(mapping
, index
, mask
| __GFP_HIGHMEM
);
3187 set_page_extent_mapped(p
);
3188 mark_page_accessed(p
);
3191 set_page_extent_head(p
, len
);
3193 set_page_private(p
, EXTENT_PAGE_PRIVATE
);
3195 if (!PageUptodate(p
))
3200 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3202 ret
= radix_tree_preload(GFP_NOFS
& ~__GFP_HIGHMEM
);
3206 spin_lock(&tree
->buffer_lock
);
3207 ret
= radix_tree_insert(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
, eb
);
3208 if (ret
== -EEXIST
) {
3209 exists
= radix_tree_lookup(&tree
->buffer
,
3210 start
>> PAGE_CACHE_SHIFT
);
3211 /* add one reference for the caller */
3212 atomic_inc(&exists
->refs
);
3213 spin_unlock(&tree
->buffer_lock
);
3214 radix_tree_preload_end();
3217 /* add one reference for the tree */
3218 atomic_inc(&eb
->refs
);
3219 spin_unlock(&tree
->buffer_lock
);
3220 radix_tree_preload_end();
3224 if (!atomic_dec_and_test(&eb
->refs
))
3226 btrfs_release_extent_buffer(eb
);
3230 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
3231 u64 start
, unsigned long len
,
3234 struct extent_buffer
*eb
;
3237 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3238 if (eb
&& atomic_inc_not_zero(&eb
->refs
)) {
3240 mark_page_accessed(eb
->first_page
);
3248 void free_extent_buffer(struct extent_buffer
*eb
)
3253 if (!atomic_dec_and_test(&eb
->refs
))
3259 int clear_extent_buffer_dirty(struct extent_io_tree
*tree
,
3260 struct extent_buffer
*eb
)
3263 unsigned long num_pages
;
3266 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3268 for (i
= 0; i
< num_pages
; i
++) {
3269 page
= extent_buffer_page(eb
, i
);
3270 if (!PageDirty(page
))
3275 set_page_extent_head(page
, eb
->len
);
3277 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
3279 clear_page_dirty_for_io(page
);
3280 spin_lock_irq(&page
->mapping
->tree_lock
);
3281 if (!PageDirty(page
)) {
3282 radix_tree_tag_clear(&page
->mapping
->page_tree
,
3284 PAGECACHE_TAG_DIRTY
);
3286 spin_unlock_irq(&page
->mapping
->tree_lock
);
3292 int wait_on_extent_buffer_writeback(struct extent_io_tree
*tree
,
3293 struct extent_buffer
*eb
)
3295 return wait_on_extent_writeback(tree
, eb
->start
,
3296 eb
->start
+ eb
->len
- 1);
3299 int set_extent_buffer_dirty(struct extent_io_tree
*tree
,
3300 struct extent_buffer
*eb
)
3303 unsigned long num_pages
;
3306 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
3307 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3308 for (i
= 0; i
< num_pages
; i
++)
3309 __set_page_dirty_nobuffers(extent_buffer_page(eb
, i
));
3313 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3314 struct extent_buffer
*eb
,
3315 struct extent_state
**cached_state
)
3319 unsigned long num_pages
;
3321 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3322 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3324 clear_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3325 cached_state
, GFP_NOFS
);
3326 for (i
= 0; i
< num_pages
; i
++) {
3327 page
= extent_buffer_page(eb
, i
);
3329 ClearPageUptodate(page
);
3334 int set_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3335 struct extent_buffer
*eb
)
3339 unsigned long num_pages
;
3341 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3343 set_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3345 for (i
= 0; i
< num_pages
; i
++) {
3346 page
= extent_buffer_page(eb
, i
);
3347 if ((i
== 0 && (eb
->start
& (PAGE_CACHE_SIZE
- 1))) ||
3348 ((i
== num_pages
- 1) &&
3349 ((eb
->start
+ eb
->len
) & (PAGE_CACHE_SIZE
- 1)))) {
3350 check_page_uptodate(tree
, page
);
3353 SetPageUptodate(page
);
3358 int extent_range_uptodate(struct extent_io_tree
*tree
,
3363 int pg_uptodate
= 1;
3365 unsigned long index
;
3367 ret
= test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
);
3370 while (start
<= end
) {
3371 index
= start
>> PAGE_CACHE_SHIFT
;
3372 page
= find_get_page(tree
->mapping
, index
);
3373 uptodate
= PageUptodate(page
);
3374 page_cache_release(page
);
3379 start
+= PAGE_CACHE_SIZE
;
3384 int extent_buffer_uptodate(struct extent_io_tree
*tree
,
3385 struct extent_buffer
*eb
,
3386 struct extent_state
*cached_state
)
3389 unsigned long num_pages
;
3392 int pg_uptodate
= 1;
3394 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3397 ret
= test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3398 EXTENT_UPTODATE
, 1, cached_state
);
3402 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3403 for (i
= 0; i
< num_pages
; i
++) {
3404 page
= extent_buffer_page(eb
, i
);
3405 if (!PageUptodate(page
)) {
3413 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
3414 struct extent_buffer
*eb
,
3415 u64 start
, int wait
,
3416 get_extent_t
*get_extent
, int mirror_num
)
3419 unsigned long start_i
;
3423 int locked_pages
= 0;
3424 int all_uptodate
= 1;
3425 int inc_all_pages
= 0;
3426 unsigned long num_pages
;
3427 struct bio
*bio
= NULL
;
3428 unsigned long bio_flags
= 0;
3430 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3433 if (test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3434 EXTENT_UPTODATE
, 1, NULL
)) {
3439 WARN_ON(start
< eb
->start
);
3440 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
3441 (eb
->start
>> PAGE_CACHE_SHIFT
);
3446 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3447 for (i
= start_i
; i
< num_pages
; i
++) {
3448 page
= extent_buffer_page(eb
, i
);
3450 if (!trylock_page(page
))
3456 if (!PageUptodate(page
))
3461 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3465 for (i
= start_i
; i
< num_pages
; i
++) {
3466 page
= extent_buffer_page(eb
, i
);
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
= kmap_atomic(page
, KM_USER1
);
3533 memcpy(dst
, kaddr
+ offset
, cur
);
3534 kunmap_atomic(kaddr
, KM_USER1
);
3543 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3544 unsigned long min_len
, char **token
, char **map
,
3545 unsigned long *map_start
,
3546 unsigned long *map_len
, int km
)
3548 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
3551 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3552 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3553 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
3560 offset
= start_offset
;
3564 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
3567 if (start
+ min_len
> eb
->len
) {
3568 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
3569 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
3570 eb
->len
, start
, min_len
);
3574 p
= extent_buffer_page(eb
, i
);
3575 kaddr
= kmap_atomic(p
, km
);
3577 *map
= kaddr
+ offset
;
3578 *map_len
= PAGE_CACHE_SIZE
- offset
;
3582 int map_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3583 unsigned long min_len
,
3584 char **token
, char **map
,
3585 unsigned long *map_start
,
3586 unsigned long *map_len
, int km
)
3590 if (eb
->map_token
) {
3591 unmap_extent_buffer(eb
, eb
->map_token
, km
);
3592 eb
->map_token
= NULL
;
3595 err
= map_private_extent_buffer(eb
, start
, min_len
, token
, map
,
3596 map_start
, map_len
, km
);
3598 eb
->map_token
= *token
;
3600 eb
->map_start
= *map_start
;
3601 eb
->map_len
= *map_len
;
3606 void unmap_extent_buffer(struct extent_buffer
*eb
, char *token
, int km
)
3608 kunmap_atomic(token
, km
);
3611 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
3612 unsigned long start
,
3619 char *ptr
= (char *)ptrv
;
3620 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3621 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3624 WARN_ON(start
> eb
->len
);
3625 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3627 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3630 page
= extent_buffer_page(eb
, i
);
3632 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3634 kaddr
= kmap_atomic(page
, KM_USER0
);
3635 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
3636 kunmap_atomic(kaddr
, KM_USER0
);
3648 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
3649 unsigned long start
, unsigned long len
)
3655 char *src
= (char *)srcv
;
3656 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3657 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3659 WARN_ON(start
> eb
->len
);
3660 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3662 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3665 page
= extent_buffer_page(eb
, i
);
3666 WARN_ON(!PageUptodate(page
));
3668 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3669 kaddr
= kmap_atomic(page
, KM_USER1
);
3670 memcpy(kaddr
+ offset
, src
, cur
);
3671 kunmap_atomic(kaddr
, KM_USER1
);
3680 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
3681 unsigned long start
, unsigned long len
)
3687 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3688 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3690 WARN_ON(start
> eb
->len
);
3691 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3693 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3696 page
= extent_buffer_page(eb
, i
);
3697 WARN_ON(!PageUptodate(page
));
3699 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3700 kaddr
= kmap_atomic(page
, KM_USER0
);
3701 memset(kaddr
+ offset
, c
, cur
);
3702 kunmap_atomic(kaddr
, KM_USER0
);
3710 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
3711 unsigned long dst_offset
, unsigned long src_offset
,
3714 u64 dst_len
= dst
->len
;
3719 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3720 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3722 WARN_ON(src
->len
!= dst_len
);
3724 offset
= (start_offset
+ dst_offset
) &
3725 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3728 page
= extent_buffer_page(dst
, i
);
3729 WARN_ON(!PageUptodate(page
));
3731 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
3733 kaddr
= kmap_atomic(page
, KM_USER0
);
3734 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
3735 kunmap_atomic(kaddr
, KM_USER0
);
3744 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
3745 unsigned long dst_off
, unsigned long src_off
,
3748 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3749 if (dst_page
== src_page
) {
3750 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
3752 char *src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3753 char *p
= dst_kaddr
+ dst_off
+ len
;
3754 char *s
= src_kaddr
+ src_off
+ len
;
3759 kunmap_atomic(src_kaddr
, KM_USER1
);
3761 kunmap_atomic(dst_kaddr
, KM_USER0
);
3764 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
3765 unsigned long dst_off
, unsigned long src_off
,
3768 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3771 if (dst_page
!= src_page
)
3772 src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3774 src_kaddr
= dst_kaddr
;
3776 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
3777 kunmap_atomic(dst_kaddr
, KM_USER0
);
3778 if (dst_page
!= src_page
)
3779 kunmap_atomic(src_kaddr
, KM_USER1
);
3782 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3783 unsigned long src_offset
, unsigned long len
)
3786 size_t dst_off_in_page
;
3787 size_t src_off_in_page
;
3788 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3789 unsigned long dst_i
;
3790 unsigned long src_i
;
3792 if (src_offset
+ len
> dst
->len
) {
3793 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3794 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
3797 if (dst_offset
+ len
> dst
->len
) {
3798 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3799 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
3804 dst_off_in_page
= (start_offset
+ dst_offset
) &
3805 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3806 src_off_in_page
= (start_offset
+ src_offset
) &
3807 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3809 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3810 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
3812 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
3814 cur
= min_t(unsigned long, cur
,
3815 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
3817 copy_pages(extent_buffer_page(dst
, dst_i
),
3818 extent_buffer_page(dst
, src_i
),
3819 dst_off_in_page
, src_off_in_page
, cur
);
3827 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3828 unsigned long src_offset
, unsigned long len
)
3831 size_t dst_off_in_page
;
3832 size_t src_off_in_page
;
3833 unsigned long dst_end
= dst_offset
+ len
- 1;
3834 unsigned long src_end
= src_offset
+ len
- 1;
3835 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3836 unsigned long dst_i
;
3837 unsigned long src_i
;
3839 if (src_offset
+ len
> dst
->len
) {
3840 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3841 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
3844 if (dst_offset
+ len
> dst
->len
) {
3845 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3846 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
3849 if (dst_offset
< src_offset
) {
3850 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
3854 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
3855 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
3857 dst_off_in_page
= (start_offset
+ dst_end
) &
3858 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3859 src_off_in_page
= (start_offset
+ src_end
) &
3860 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3862 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
3863 cur
= min(cur
, dst_off_in_page
+ 1);
3864 move_pages(extent_buffer_page(dst
, dst_i
),
3865 extent_buffer_page(dst
, src_i
),
3866 dst_off_in_page
- cur
+ 1,
3867 src_off_in_page
- cur
+ 1, cur
);
3875 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head
*head
)
3877 struct extent_buffer
*eb
=
3878 container_of(head
, struct extent_buffer
, rcu_head
);
3880 btrfs_release_extent_buffer(eb
);
3883 int try_release_extent_buffer(struct extent_io_tree
*tree
, struct page
*page
)
3885 u64 start
= page_offset(page
);
3886 struct extent_buffer
*eb
;
3889 spin_lock(&tree
->buffer_lock
);
3890 eb
= radix_tree_lookup(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3892 spin_unlock(&tree
->buffer_lock
);
3896 if (test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3902 * set @eb->refs to 0 if it is already 1, and then release the @eb.
3905 if (atomic_cmpxchg(&eb
->refs
, 1, 0) != 1) {
3910 radix_tree_delete(&tree
->buffer
, start
>> PAGE_CACHE_SHIFT
);
3912 spin_unlock(&tree
->buffer_lock
);
3914 /* at this point we can safely release the extent buffer */
3915 if (atomic_read(&eb
->refs
) == 0)
3916 call_rcu(&eb
->rcu_head
, btrfs_release_extent_buffer_rcu
);