1 #include <linux/bitops.h>
2 #include <linux/slab.h>
6 #include <linux/pagemap.h>
7 #include <linux/page-flags.h>
8 #include <linux/module.h>
9 #include <linux/spinlock.h>
10 #include <linux/blkdev.h>
11 #include <linux/swap.h>
12 #include <linux/writeback.h>
13 #include <linux/pagevec.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 tree
->buffer
= RB_ROOT
;
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 static 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 struct extent_buffer
*buffer_tree_insert(struct extent_io_tree
*tree
,
240 u64 offset
, struct rb_node
*node
)
242 struct rb_root
*root
= &tree
->buffer
;
243 struct rb_node
**p
= &root
->rb_node
;
244 struct rb_node
*parent
= NULL
;
245 struct extent_buffer
*eb
;
249 eb
= rb_entry(parent
, struct extent_buffer
, rb_node
);
251 if (offset
< eb
->start
)
253 else if (offset
> eb
->start
)
259 rb_link_node(node
, parent
, p
);
260 rb_insert_color(node
, root
);
264 static struct extent_buffer
*buffer_search(struct extent_io_tree
*tree
,
267 struct rb_root
*root
= &tree
->buffer
;
268 struct rb_node
*n
= root
->rb_node
;
269 struct extent_buffer
*eb
;
272 eb
= rb_entry(n
, struct extent_buffer
, rb_node
);
273 if (offset
< eb
->start
)
275 else if (offset
> eb
->start
)
283 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
284 struct extent_state
*other
)
286 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
287 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
292 * utility function to look for merge candidates inside a given range.
293 * Any extents with matching state are merged together into a single
294 * extent in the tree. Extents with EXTENT_IO in their state field
295 * are not merged because the end_io handlers need to be able to do
296 * operations on them without sleeping (or doing allocations/splits).
298 * This should be called with the tree lock held.
300 static int merge_state(struct extent_io_tree
*tree
,
301 struct extent_state
*state
)
303 struct extent_state
*other
;
304 struct rb_node
*other_node
;
306 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
309 other_node
= rb_prev(&state
->rb_node
);
311 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
312 if (other
->end
== state
->start
- 1 &&
313 other
->state
== state
->state
) {
314 merge_cb(tree
, state
, other
);
315 state
->start
= other
->start
;
317 rb_erase(&other
->rb_node
, &tree
->state
);
318 free_extent_state(other
);
321 other_node
= rb_next(&state
->rb_node
);
323 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
324 if (other
->start
== state
->end
+ 1 &&
325 other
->state
== state
->state
) {
326 merge_cb(tree
, state
, other
);
327 other
->start
= state
->start
;
329 rb_erase(&state
->rb_node
, &tree
->state
);
330 free_extent_state(state
);
338 static int set_state_cb(struct extent_io_tree
*tree
,
339 struct extent_state
*state
,
342 if (tree
->ops
&& tree
->ops
->set_bit_hook
) {
343 return tree
->ops
->set_bit_hook(tree
->mapping
->host
,
344 state
->start
, state
->end
,
351 static void clear_state_cb(struct extent_io_tree
*tree
,
352 struct extent_state
*state
,
355 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
356 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
, bits
);
360 * insert an extent_state struct into the tree. 'bits' are set on the
361 * struct before it is inserted.
363 * This may return -EEXIST if the extent is already there, in which case the
364 * state struct is freed.
366 * The tree lock is not taken internally. This is a utility function and
367 * probably isn't what you want to call (see set/clear_extent_bit).
369 static int insert_state(struct extent_io_tree
*tree
,
370 struct extent_state
*state
, u64 start
, u64 end
,
373 struct rb_node
*node
;
377 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
378 (unsigned long long)end
,
379 (unsigned long long)start
);
382 state
->start
= start
;
384 ret
= set_state_cb(tree
, state
, bits
);
388 if (bits
& EXTENT_DIRTY
)
389 tree
->dirty_bytes
+= end
- start
+ 1;
390 state
->state
|= bits
;
391 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
393 struct extent_state
*found
;
394 found
= rb_entry(node
, struct extent_state
, rb_node
);
395 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
396 "%llu %llu\n", (unsigned long long)found
->start
,
397 (unsigned long long)found
->end
,
398 (unsigned long long)start
, (unsigned long long)end
);
399 free_extent_state(state
);
403 merge_state(tree
, state
);
407 static int split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
410 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
411 return tree
->ops
->split_extent_hook(tree
->mapping
->host
,
417 * split a given extent state struct in two, inserting the preallocated
418 * struct 'prealloc' as the newly created second half. 'split' indicates an
419 * offset inside 'orig' where it should be split.
422 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
423 * are two extent state structs in the tree:
424 * prealloc: [orig->start, split - 1]
425 * orig: [ split, orig->end ]
427 * The tree locks are not taken by this function. They need to be held
430 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
431 struct extent_state
*prealloc
, u64 split
)
433 struct rb_node
*node
;
435 split_cb(tree
, orig
, split
);
437 prealloc
->start
= orig
->start
;
438 prealloc
->end
= split
- 1;
439 prealloc
->state
= orig
->state
;
442 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
444 free_extent_state(prealloc
);
447 prealloc
->tree
= tree
;
452 * utility function to clear some bits in an extent state struct.
453 * it will optionally wake up any one waiting on this state (wake == 1), or
454 * forcibly remove the state from the tree (delete == 1).
456 * If no bits are set on the state struct after clearing things, the
457 * struct is freed and removed from the tree
459 static int clear_state_bit(struct extent_io_tree
*tree
,
460 struct extent_state
*state
, int bits
, int wake
,
463 int bits_to_clear
= bits
& ~EXTENT_DO_ACCOUNTING
;
464 int ret
= state
->state
& bits_to_clear
;
466 if ((bits
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
467 u64 range
= state
->end
- state
->start
+ 1;
468 WARN_ON(range
> tree
->dirty_bytes
);
469 tree
->dirty_bytes
-= range
;
471 clear_state_cb(tree
, state
, bits
);
472 state
->state
&= ~bits_to_clear
;
475 if (delete || state
->state
== 0) {
477 clear_state_cb(tree
, state
, state
->state
);
478 rb_erase(&state
->rb_node
, &tree
->state
);
480 free_extent_state(state
);
485 merge_state(tree
, state
);
491 * clear some bits on a range in the tree. This may require splitting
492 * or inserting elements in the tree, so the gfp mask is used to
493 * indicate which allocations or sleeping are allowed.
495 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
496 * the given range from the tree regardless of state (ie for truncate).
498 * the range [start, end] is inclusive.
500 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
501 * bits were already set, or zero if none of the bits were already set.
503 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
504 int bits
, int wake
, int delete,
505 struct extent_state
**cached_state
,
508 struct extent_state
*state
;
509 struct extent_state
*cached
;
510 struct extent_state
*prealloc
= NULL
;
511 struct rb_node
*next_node
;
512 struct rb_node
*node
;
518 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
521 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
522 prealloc
= alloc_extent_state(mask
);
527 spin_lock(&tree
->lock
);
529 cached
= *cached_state
;
532 *cached_state
= NULL
;
536 if (cached
&& cached
->tree
&& cached
->start
== start
) {
538 atomic_dec(&cached
->refs
);
543 free_extent_state(cached
);
546 * this search will find the extents that end after
549 node
= tree_search(tree
, start
);
552 state
= rb_entry(node
, struct extent_state
, rb_node
);
554 if (state
->start
> end
)
556 WARN_ON(state
->end
< start
);
557 last_end
= state
->end
;
560 * | ---- desired range ---- |
562 * | ------------- state -------------- |
564 * We need to split the extent we found, and may flip
565 * bits on second half.
567 * If the extent we found extends past our range, we
568 * just split and search again. It'll get split again
569 * the next time though.
571 * If the extent we found is inside our range, we clear
572 * the desired bit on it.
575 if (state
->start
< start
) {
577 prealloc
= alloc_extent_state(GFP_ATOMIC
);
578 err
= split_state(tree
, state
, prealloc
, start
);
579 BUG_ON(err
== -EEXIST
);
583 if (state
->end
<= end
) {
584 set
|= clear_state_bit(tree
, state
, bits
, wake
,
586 if (last_end
== (u64
)-1)
588 start
= last_end
+ 1;
593 * | ---- desired range ---- |
595 * We need to split the extent, and clear the bit
598 if (state
->start
<= end
&& state
->end
> end
) {
600 prealloc
= alloc_extent_state(GFP_ATOMIC
);
601 err
= split_state(tree
, state
, prealloc
, end
+ 1);
602 BUG_ON(err
== -EEXIST
);
606 set
|= clear_state_bit(tree
, prealloc
, bits
, wake
, delete);
612 if (state
->end
< end
&& prealloc
&& !need_resched())
613 next_node
= rb_next(&state
->rb_node
);
617 set
|= clear_state_bit(tree
, state
, bits
, wake
, delete);
618 if (last_end
== (u64
)-1)
620 start
= last_end
+ 1;
621 if (start
<= end
&& next_node
) {
622 state
= rb_entry(next_node
, struct extent_state
,
624 if (state
->start
== start
)
630 spin_unlock(&tree
->lock
);
632 free_extent_state(prealloc
);
639 spin_unlock(&tree
->lock
);
640 if (mask
& __GFP_WAIT
)
645 static int wait_on_state(struct extent_io_tree
*tree
,
646 struct extent_state
*state
)
647 __releases(tree
->lock
)
648 __acquires(tree
->lock
)
651 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
652 spin_unlock(&tree
->lock
);
654 spin_lock(&tree
->lock
);
655 finish_wait(&state
->wq
, &wait
);
660 * waits for one or more bits to clear on a range in the state tree.
661 * The range [start, end] is inclusive.
662 * The tree lock is taken by this function
664 int wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
666 struct extent_state
*state
;
667 struct rb_node
*node
;
669 spin_lock(&tree
->lock
);
673 * this search will find all the extents that end after
676 node
= tree_search(tree
, start
);
680 state
= rb_entry(node
, struct extent_state
, rb_node
);
682 if (state
->start
> end
)
685 if (state
->state
& bits
) {
686 start
= state
->start
;
687 atomic_inc(&state
->refs
);
688 wait_on_state(tree
, state
);
689 free_extent_state(state
);
692 start
= state
->end
+ 1;
697 if (need_resched()) {
698 spin_unlock(&tree
->lock
);
700 spin_lock(&tree
->lock
);
704 spin_unlock(&tree
->lock
);
708 static int set_state_bits(struct extent_io_tree
*tree
,
709 struct extent_state
*state
,
714 ret
= set_state_cb(tree
, state
, bits
);
718 if ((bits
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
719 u64 range
= state
->end
- state
->start
+ 1;
720 tree
->dirty_bytes
+= range
;
722 state
->state
|= bits
;
727 static void cache_state(struct extent_state
*state
,
728 struct extent_state
**cached_ptr
)
730 if (cached_ptr
&& !(*cached_ptr
)) {
731 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
733 atomic_inc(&state
->refs
);
739 * set some bits on a range in the tree. This may require allocations or
740 * sleeping, so the gfp mask is used to indicate what is allowed.
742 * If any of the exclusive bits are set, this will fail with -EEXIST if some
743 * part of the range already has the desired bits set. The start of the
744 * existing range is returned in failed_start in this case.
746 * [start, end] is inclusive This takes the tree lock.
749 static int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
750 int bits
, int exclusive_bits
, u64
*failed_start
,
751 struct extent_state
**cached_state
,
754 struct extent_state
*state
;
755 struct extent_state
*prealloc
= NULL
;
756 struct rb_node
*node
;
762 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
763 prealloc
= alloc_extent_state(mask
);
768 spin_lock(&tree
->lock
);
769 if (cached_state
&& *cached_state
) {
770 state
= *cached_state
;
771 if (state
->start
== start
&& state
->tree
) {
772 node
= &state
->rb_node
;
777 * this search will find all the extents that end after
780 node
= tree_search(tree
, start
);
782 err
= insert_state(tree
, prealloc
, start
, end
, bits
);
784 BUG_ON(err
== -EEXIST
);
787 state
= rb_entry(node
, struct extent_state
, rb_node
);
789 last_start
= state
->start
;
790 last_end
= state
->end
;
793 * | ---- desired range ---- |
796 * Just lock what we found and keep going
798 if (state
->start
== start
&& state
->end
<= end
) {
799 struct rb_node
*next_node
;
800 if (state
->state
& exclusive_bits
) {
801 *failed_start
= state
->start
;
806 err
= set_state_bits(tree
, state
, bits
);
810 cache_state(state
, cached_state
);
811 merge_state(tree
, state
);
812 if (last_end
== (u64
)-1)
815 start
= last_end
+ 1;
816 if (start
< end
&& prealloc
&& !need_resched()) {
817 next_node
= rb_next(node
);
819 state
= rb_entry(next_node
, struct extent_state
,
821 if (state
->start
== start
)
829 * | ---- desired range ---- |
832 * | ------------- state -------------- |
834 * We need to split the extent we found, and may flip bits on
837 * If the extent we found extends past our
838 * range, we just split and search again. It'll get split
839 * again the next time though.
841 * If the extent we found is inside our range, we set the
844 if (state
->start
< start
) {
845 if (state
->state
& exclusive_bits
) {
846 *failed_start
= start
;
850 err
= split_state(tree
, state
, prealloc
, start
);
851 BUG_ON(err
== -EEXIST
);
855 if (state
->end
<= end
) {
856 err
= set_state_bits(tree
, state
, bits
);
859 cache_state(state
, cached_state
);
860 merge_state(tree
, state
);
861 if (last_end
== (u64
)-1)
863 start
= last_end
+ 1;
868 * | ---- desired range ---- |
869 * | state | or | state |
871 * There's a hole, we need to insert something in it and
872 * ignore the extent we found.
874 if (state
->start
> start
) {
876 if (end
< last_start
)
879 this_end
= last_start
- 1;
880 err
= insert_state(tree
, prealloc
, start
, this_end
,
882 BUG_ON(err
== -EEXIST
);
887 cache_state(prealloc
, cached_state
);
889 start
= this_end
+ 1;
893 * | ---- desired range ---- |
895 * We need to split the extent, and set the bit
898 if (state
->start
<= end
&& state
->end
> end
) {
899 if (state
->state
& exclusive_bits
) {
900 *failed_start
= start
;
904 err
= split_state(tree
, state
, prealloc
, end
+ 1);
905 BUG_ON(err
== -EEXIST
);
907 err
= set_state_bits(tree
, prealloc
, bits
);
912 cache_state(prealloc
, cached_state
);
913 merge_state(tree
, prealloc
);
921 spin_unlock(&tree
->lock
);
923 free_extent_state(prealloc
);
930 spin_unlock(&tree
->lock
);
931 if (mask
& __GFP_WAIT
)
936 /* wrappers around set/clear extent bit */
937 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
940 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, 0, NULL
,
944 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
945 int bits
, gfp_t mask
)
947 return set_extent_bit(tree
, start
, end
, bits
, 0, NULL
,
951 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
952 int bits
, gfp_t mask
)
954 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
957 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
958 struct extent_state
**cached_state
, gfp_t mask
)
960 return set_extent_bit(tree
, start
, end
,
961 EXTENT_DELALLOC
| EXTENT_DIRTY
| EXTENT_UPTODATE
,
962 0, NULL
, cached_state
, mask
);
965 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
968 return clear_extent_bit(tree
, start
, end
,
969 EXTENT_DIRTY
| EXTENT_DELALLOC
|
970 EXTENT_DO_ACCOUNTING
, 0, 0,
974 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
977 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, NULL
,
981 static int clear_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
984 return clear_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, 0,
988 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
991 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, NULL
,
995 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
996 u64 end
, struct extent_state
**cached_state
,
999 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
1000 cached_state
, mask
);
1003 int wait_on_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1005 return wait_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
);
1009 * either insert or lock state struct between start and end use mask to tell
1010 * us if waiting is desired.
1012 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1013 int bits
, struct extent_state
**cached_state
, gfp_t mask
)
1018 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
1019 EXTENT_LOCKED
, &failed_start
,
1020 cached_state
, mask
);
1021 if (err
== -EEXIST
&& (mask
& __GFP_WAIT
)) {
1022 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
1023 start
= failed_start
;
1027 WARN_ON(start
> end
);
1032 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
1034 return lock_extent_bits(tree
, start
, end
, 0, NULL
, mask
);
1037 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1043 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1044 &failed_start
, NULL
, mask
);
1045 if (err
== -EEXIST
) {
1046 if (failed_start
> start
)
1047 clear_extent_bit(tree
, start
, failed_start
- 1,
1048 EXTENT_LOCKED
, 1, 0, NULL
, mask
);
1054 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1055 struct extent_state
**cached
, gfp_t mask
)
1057 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1061 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1064 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1069 * helper function to set pages and extents in the tree dirty
1071 int set_range_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1073 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1074 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1077 while (index
<= end_index
) {
1078 page
= find_get_page(tree
->mapping
, index
);
1080 __set_page_dirty_nobuffers(page
);
1081 page_cache_release(page
);
1088 * helper function to set both pages and extents in the tree writeback
1090 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1092 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1093 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1096 while (index
<= end_index
) {
1097 page
= find_get_page(tree
->mapping
, index
);
1099 set_page_writeback(page
);
1100 page_cache_release(page
);
1107 * find the first offset in the io tree with 'bits' set. zero is
1108 * returned if we find something, and *start_ret and *end_ret are
1109 * set to reflect the state struct that was found.
1111 * If nothing was found, 1 is returned, < 0 on error
1113 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1114 u64
*start_ret
, u64
*end_ret
, int bits
)
1116 struct rb_node
*node
;
1117 struct extent_state
*state
;
1120 spin_lock(&tree
->lock
);
1122 * this search will find all the extents that end after
1125 node
= tree_search(tree
, start
);
1130 state
= rb_entry(node
, struct extent_state
, rb_node
);
1131 if (state
->end
>= start
&& (state
->state
& bits
)) {
1132 *start_ret
= state
->start
;
1133 *end_ret
= state
->end
;
1137 node
= rb_next(node
);
1142 spin_unlock(&tree
->lock
);
1146 /* find the first state struct with 'bits' set after 'start', and
1147 * return it. tree->lock must be held. NULL will returned if
1148 * nothing was found after 'start'
1150 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1151 u64 start
, int bits
)
1153 struct rb_node
*node
;
1154 struct extent_state
*state
;
1157 * this search will find all the extents that end after
1160 node
= tree_search(tree
, start
);
1165 state
= rb_entry(node
, struct extent_state
, rb_node
);
1166 if (state
->end
>= start
&& (state
->state
& bits
))
1169 node
= rb_next(node
);
1178 * find a contiguous range of bytes in the file marked as delalloc, not
1179 * more than 'max_bytes'. start and end are used to return the range,
1181 * 1 is returned if we find something, 0 if nothing was in the tree
1183 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1184 u64
*start
, u64
*end
, u64 max_bytes
,
1185 struct extent_state
**cached_state
)
1187 struct rb_node
*node
;
1188 struct extent_state
*state
;
1189 u64 cur_start
= *start
;
1191 u64 total_bytes
= 0;
1193 spin_lock(&tree
->lock
);
1196 * this search will find all the extents that end after
1199 node
= tree_search(tree
, cur_start
);
1207 state
= rb_entry(node
, struct extent_state
, rb_node
);
1208 if (found
&& (state
->start
!= cur_start
||
1209 (state
->state
& EXTENT_BOUNDARY
))) {
1212 if (!(state
->state
& EXTENT_DELALLOC
)) {
1218 *start
= state
->start
;
1219 *cached_state
= state
;
1220 atomic_inc(&state
->refs
);
1224 cur_start
= state
->end
+ 1;
1225 node
= rb_next(node
);
1228 total_bytes
+= state
->end
- state
->start
+ 1;
1229 if (total_bytes
>= max_bytes
)
1233 spin_unlock(&tree
->lock
);
1237 static noinline
int __unlock_for_delalloc(struct inode
*inode
,
1238 struct page
*locked_page
,
1242 struct page
*pages
[16];
1243 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1244 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1245 unsigned long nr_pages
= end_index
- index
+ 1;
1248 if (index
== locked_page
->index
&& end_index
== index
)
1251 while (nr_pages
> 0) {
1252 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1253 min_t(unsigned long, nr_pages
,
1254 ARRAY_SIZE(pages
)), pages
);
1255 for (i
= 0; i
< ret
; i
++) {
1256 if (pages
[i
] != locked_page
)
1257 unlock_page(pages
[i
]);
1258 page_cache_release(pages
[i
]);
1267 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1268 struct page
*locked_page
,
1272 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1273 unsigned long start_index
= index
;
1274 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1275 unsigned long pages_locked
= 0;
1276 struct page
*pages
[16];
1277 unsigned long nrpages
;
1281 /* the caller is responsible for locking the start index */
1282 if (index
== locked_page
->index
&& index
== end_index
)
1285 /* skip the page at the start index */
1286 nrpages
= end_index
- index
+ 1;
1287 while (nrpages
> 0) {
1288 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1289 min_t(unsigned long,
1290 nrpages
, ARRAY_SIZE(pages
)), pages
);
1295 /* now we have an array of pages, lock them all */
1296 for (i
= 0; i
< ret
; i
++) {
1298 * the caller is taking responsibility for
1301 if (pages
[i
] != locked_page
) {
1302 lock_page(pages
[i
]);
1303 if (!PageDirty(pages
[i
]) ||
1304 pages
[i
]->mapping
!= inode
->i_mapping
) {
1306 unlock_page(pages
[i
]);
1307 page_cache_release(pages
[i
]);
1311 page_cache_release(pages
[i
]);
1320 if (ret
&& pages_locked
) {
1321 __unlock_for_delalloc(inode
, locked_page
,
1323 ((u64
)(start_index
+ pages_locked
- 1)) <<
1330 * find a contiguous range of bytes in the file marked as delalloc, not
1331 * more than 'max_bytes'. start and end are used to return the range,
1333 * 1 is returned if we find something, 0 if nothing was in the tree
1335 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1336 struct extent_io_tree
*tree
,
1337 struct page
*locked_page
,
1338 u64
*start
, u64
*end
,
1344 struct extent_state
*cached_state
= NULL
;
1349 /* step one, find a bunch of delalloc bytes starting at start */
1350 delalloc_start
= *start
;
1352 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1353 max_bytes
, &cached_state
);
1354 if (!found
|| delalloc_end
<= *start
) {
1355 *start
= delalloc_start
;
1356 *end
= delalloc_end
;
1357 free_extent_state(cached_state
);
1362 * start comes from the offset of locked_page. We have to lock
1363 * pages in order, so we can't process delalloc bytes before
1366 if (delalloc_start
< *start
)
1367 delalloc_start
= *start
;
1370 * make sure to limit the number of pages we try to lock down
1373 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1374 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1376 /* step two, lock all the pages after the page that has start */
1377 ret
= lock_delalloc_pages(inode
, locked_page
,
1378 delalloc_start
, delalloc_end
);
1379 if (ret
== -EAGAIN
) {
1380 /* some of the pages are gone, lets avoid looping by
1381 * shortening the size of the delalloc range we're searching
1383 free_extent_state(cached_state
);
1385 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1386 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1396 /* step three, lock the state bits for the whole range */
1397 lock_extent_bits(tree
, delalloc_start
, delalloc_end
,
1398 0, &cached_state
, GFP_NOFS
);
1400 /* then test to make sure it is all still delalloc */
1401 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1402 EXTENT_DELALLOC
, 1, cached_state
);
1404 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1405 &cached_state
, GFP_NOFS
);
1406 __unlock_for_delalloc(inode
, locked_page
,
1407 delalloc_start
, delalloc_end
);
1411 free_extent_state(cached_state
);
1412 *start
= delalloc_start
;
1413 *end
= delalloc_end
;
1418 int extent_clear_unlock_delalloc(struct inode
*inode
,
1419 struct extent_io_tree
*tree
,
1420 u64 start
, u64 end
, struct page
*locked_page
,
1424 struct page
*pages
[16];
1425 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1426 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1427 unsigned long nr_pages
= end_index
- index
+ 1;
1431 if (op
& EXTENT_CLEAR_UNLOCK
)
1432 clear_bits
|= EXTENT_LOCKED
;
1433 if (op
& EXTENT_CLEAR_DIRTY
)
1434 clear_bits
|= EXTENT_DIRTY
;
1436 if (op
& EXTENT_CLEAR_DELALLOC
)
1437 clear_bits
|= EXTENT_DELALLOC
;
1439 if (op
& EXTENT_CLEAR_ACCOUNTING
)
1440 clear_bits
|= EXTENT_DO_ACCOUNTING
;
1442 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1443 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1444 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1445 EXTENT_SET_PRIVATE2
)))
1448 while (nr_pages
> 0) {
1449 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1450 min_t(unsigned long,
1451 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1452 for (i
= 0; i
< ret
; i
++) {
1454 if (op
& EXTENT_SET_PRIVATE2
)
1455 SetPagePrivate2(pages
[i
]);
1457 if (pages
[i
] == locked_page
) {
1458 page_cache_release(pages
[i
]);
1461 if (op
& EXTENT_CLEAR_DIRTY
)
1462 clear_page_dirty_for_io(pages
[i
]);
1463 if (op
& EXTENT_SET_WRITEBACK
)
1464 set_page_writeback(pages
[i
]);
1465 if (op
& EXTENT_END_WRITEBACK
)
1466 end_page_writeback(pages
[i
]);
1467 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1468 unlock_page(pages
[i
]);
1469 page_cache_release(pages
[i
]);
1479 * count the number of bytes in the tree that have a given bit(s)
1480 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1481 * cached. The total number found is returned.
1483 u64
count_range_bits(struct extent_io_tree
*tree
,
1484 u64
*start
, u64 search_end
, u64 max_bytes
,
1487 struct rb_node
*node
;
1488 struct extent_state
*state
;
1489 u64 cur_start
= *start
;
1490 u64 total_bytes
= 0;
1493 if (search_end
<= cur_start
) {
1498 spin_lock(&tree
->lock
);
1499 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1500 total_bytes
= tree
->dirty_bytes
;
1504 * this search will find all the extents that end after
1507 node
= tree_search(tree
, cur_start
);
1512 state
= rb_entry(node
, struct extent_state
, rb_node
);
1513 if (state
->start
> search_end
)
1515 if (state
->end
>= cur_start
&& (state
->state
& bits
)) {
1516 total_bytes
+= min(search_end
, state
->end
) + 1 -
1517 max(cur_start
, state
->start
);
1518 if (total_bytes
>= max_bytes
)
1521 *start
= state
->start
;
1525 node
= rb_next(node
);
1530 spin_unlock(&tree
->lock
);
1535 * set the private field for a given byte offset in the tree. If there isn't
1536 * an extent_state there already, this does nothing.
1538 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1540 struct rb_node
*node
;
1541 struct extent_state
*state
;
1544 spin_lock(&tree
->lock
);
1546 * this search will find all the extents that end after
1549 node
= tree_search(tree
, start
);
1554 state
= rb_entry(node
, struct extent_state
, rb_node
);
1555 if (state
->start
!= start
) {
1559 state
->private = private;
1561 spin_unlock(&tree
->lock
);
1565 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1567 struct rb_node
*node
;
1568 struct extent_state
*state
;
1571 spin_lock(&tree
->lock
);
1573 * this search will find all the extents that end after
1576 node
= tree_search(tree
, start
);
1581 state
= rb_entry(node
, struct extent_state
, rb_node
);
1582 if (state
->start
!= start
) {
1586 *private = state
->private;
1588 spin_unlock(&tree
->lock
);
1593 * searches a range in the state tree for a given mask.
1594 * If 'filled' == 1, this returns 1 only if every extent in the tree
1595 * has the bits set. Otherwise, 1 is returned if any bit in the
1596 * range is found set.
1598 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1599 int bits
, int filled
, struct extent_state
*cached
)
1601 struct extent_state
*state
= NULL
;
1602 struct rb_node
*node
;
1605 spin_lock(&tree
->lock
);
1606 if (cached
&& cached
->tree
&& cached
->start
== start
)
1607 node
= &cached
->rb_node
;
1609 node
= tree_search(tree
, start
);
1610 while (node
&& start
<= end
) {
1611 state
= rb_entry(node
, struct extent_state
, rb_node
);
1613 if (filled
&& state
->start
> start
) {
1618 if (state
->start
> end
)
1621 if (state
->state
& bits
) {
1625 } else if (filled
) {
1630 if (state
->end
== (u64
)-1)
1633 start
= state
->end
+ 1;
1636 node
= rb_next(node
);
1643 spin_unlock(&tree
->lock
);
1648 * helper function to set a given page up to date if all the
1649 * extents in the tree for that page are up to date
1651 static int check_page_uptodate(struct extent_io_tree
*tree
,
1654 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1655 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1656 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1657 SetPageUptodate(page
);
1662 * helper function to unlock a page if all the extents in the tree
1663 * for that page are unlocked
1665 static int check_page_locked(struct extent_io_tree
*tree
,
1668 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1669 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1670 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1676 * helper function to end page writeback if all the extents
1677 * in the tree for that page are done with writeback
1679 static int check_page_writeback(struct extent_io_tree
*tree
,
1682 end_page_writeback(page
);
1686 /* lots and lots of room for performance fixes in the end_bio funcs */
1689 * after a writepage IO is done, we need to:
1690 * clear the uptodate bits on error
1691 * clear the writeback bits in the extent tree for this IO
1692 * end_page_writeback if the page has no more pending IO
1694 * Scheduling is not allowed, so the extent state tree is expected
1695 * to have one and only one object corresponding to this IO.
1697 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
1699 int uptodate
= err
== 0;
1700 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1701 struct extent_io_tree
*tree
;
1708 struct page
*page
= bvec
->bv_page
;
1709 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1711 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1713 end
= start
+ bvec
->bv_len
- 1;
1715 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1720 if (--bvec
>= bio
->bi_io_vec
)
1721 prefetchw(&bvec
->bv_page
->flags
);
1722 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
1723 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
1724 end
, NULL
, uptodate
);
1729 if (!uptodate
&& tree
->ops
&&
1730 tree
->ops
->writepage_io_failed_hook
) {
1731 ret
= tree
->ops
->writepage_io_failed_hook(bio
, page
,
1734 uptodate
= (err
== 0);
1740 clear_extent_uptodate(tree
, start
, end
, NULL
, GFP_NOFS
);
1741 ClearPageUptodate(page
);
1746 end_page_writeback(page
);
1748 check_page_writeback(tree
, page
);
1749 } while (bvec
>= bio
->bi_io_vec
);
1755 * after a readpage IO is done, we need to:
1756 * clear the uptodate bits on error
1757 * set the uptodate bits if things worked
1758 * set the page up to date if all extents in the tree are uptodate
1759 * clear the lock bit in the extent tree
1760 * unlock the page if there are no other extents locked for it
1762 * Scheduling is not allowed, so the extent state tree is expected
1763 * to have one and only one object corresponding to this IO.
1765 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
1767 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1768 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1769 struct bio_vec
*bvec
= bio
->bi_io_vec
;
1770 struct extent_io_tree
*tree
;
1780 struct page
*page
= bvec
->bv_page
;
1781 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1783 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1785 end
= start
+ bvec
->bv_len
- 1;
1787 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1792 if (++bvec
<= bvec_end
)
1793 prefetchw(&bvec
->bv_page
->flags
);
1795 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
1796 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
1801 if (!uptodate
&& tree
->ops
&&
1802 tree
->ops
->readpage_io_failed_hook
) {
1803 ret
= tree
->ops
->readpage_io_failed_hook(bio
, page
,
1807 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1815 set_extent_uptodate(tree
, start
, end
,
1818 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1822 SetPageUptodate(page
);
1824 ClearPageUptodate(page
);
1830 check_page_uptodate(tree
, page
);
1832 ClearPageUptodate(page
);
1835 check_page_locked(tree
, page
);
1837 } while (bvec
<= bvec_end
);
1843 * IO done from prepare_write is pretty simple, we just unlock
1844 * the structs in the extent tree when done, and set the uptodate bits
1847 static void end_bio_extent_preparewrite(struct bio
*bio
, int err
)
1849 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1850 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1851 struct extent_io_tree
*tree
;
1856 struct page
*page
= bvec
->bv_page
;
1857 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1859 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1861 end
= start
+ bvec
->bv_len
- 1;
1863 if (--bvec
>= bio
->bi_io_vec
)
1864 prefetchw(&bvec
->bv_page
->flags
);
1867 set_extent_uptodate(tree
, start
, end
, GFP_ATOMIC
);
1869 ClearPageUptodate(page
);
1873 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1875 } while (bvec
>= bio
->bi_io_vec
);
1881 extent_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
1886 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1888 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
1889 while (!bio
&& (nr_vecs
/= 2))
1890 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1895 bio
->bi_bdev
= bdev
;
1896 bio
->bi_sector
= first_sector
;
1901 static int submit_one_bio(int rw
, struct bio
*bio
, int mirror_num
,
1902 unsigned long bio_flags
)
1905 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1906 struct page
*page
= bvec
->bv_page
;
1907 struct extent_io_tree
*tree
= bio
->bi_private
;
1911 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
1912 end
= start
+ bvec
->bv_len
- 1;
1914 bio
->bi_private
= NULL
;
1918 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
1919 tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
1920 mirror_num
, bio_flags
);
1922 submit_bio(rw
, bio
);
1923 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
1929 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
1930 struct page
*page
, sector_t sector
,
1931 size_t size
, unsigned long offset
,
1932 struct block_device
*bdev
,
1933 struct bio
**bio_ret
,
1934 unsigned long max_pages
,
1935 bio_end_io_t end_io_func
,
1937 unsigned long prev_bio_flags
,
1938 unsigned long bio_flags
)
1944 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
1945 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
1946 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
1948 if (bio_ret
&& *bio_ret
) {
1951 contig
= bio
->bi_sector
== sector
;
1953 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
1956 if (prev_bio_flags
!= bio_flags
|| !contig
||
1957 (tree
->ops
&& tree
->ops
->merge_bio_hook
&&
1958 tree
->ops
->merge_bio_hook(page
, offset
, page_size
, bio
,
1960 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
1961 ret
= submit_one_bio(rw
, bio
, mirror_num
,
1968 if (this_compressed
)
1971 nr
= bio_get_nr_vecs(bdev
);
1973 bio
= extent_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
1975 bio_add_page(bio
, page
, page_size
, offset
);
1976 bio
->bi_end_io
= end_io_func
;
1977 bio
->bi_private
= tree
;
1982 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
1987 void set_page_extent_mapped(struct page
*page
)
1989 if (!PagePrivate(page
)) {
1990 SetPagePrivate(page
);
1991 page_cache_get(page
);
1992 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
1996 static void set_page_extent_head(struct page
*page
, unsigned long len
)
1998 set_page_private(page
, EXTENT_PAGE_PRIVATE_FIRST_PAGE
| len
<< 2);
2002 * basic readpage implementation. Locked extent state structs are inserted
2003 * into the tree that are removed when the IO is done (by the end_io
2006 static int __extent_read_full_page(struct extent_io_tree
*tree
,
2008 get_extent_t
*get_extent
,
2009 struct bio
**bio
, int mirror_num
,
2010 unsigned long *bio_flags
)
2012 struct inode
*inode
= page
->mapping
->host
;
2013 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2014 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2018 u64 last_byte
= i_size_read(inode
);
2022 struct extent_map
*em
;
2023 struct block_device
*bdev
;
2026 size_t page_offset
= 0;
2028 size_t disk_io_size
;
2029 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2030 unsigned long this_bio_flag
= 0;
2032 set_page_extent_mapped(page
);
2035 lock_extent(tree
, start
, end
, GFP_NOFS
);
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 iosize
= PAGE_CACHE_SIZE
- page_offset
;
2053 userpage
= kmap_atomic(page
, KM_USER0
);
2054 memset(userpage
+ page_offset
, 0, iosize
);
2055 flush_dcache_page(page
);
2056 kunmap_atomic(userpage
, KM_USER0
);
2057 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2059 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2062 em
= get_extent(inode
, page
, page_offset
, cur
,
2064 if (IS_ERR(em
) || !em
) {
2066 unlock_extent(tree
, cur
, end
, GFP_NOFS
);
2069 extent_offset
= cur
- em
->start
;
2070 BUG_ON(extent_map_end(em
) <= cur
);
2073 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
2074 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2076 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2077 cur_end
= min(extent_map_end(em
) - 1, end
);
2078 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2079 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2080 disk_io_size
= em
->block_len
;
2081 sector
= em
->block_start
>> 9;
2083 sector
= (em
->block_start
+ extent_offset
) >> 9;
2084 disk_io_size
= iosize
;
2087 block_start
= em
->block_start
;
2088 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2089 block_start
= EXTENT_MAP_HOLE
;
2090 free_extent_map(em
);
2093 /* we've found a hole, just zero and go on */
2094 if (block_start
== EXTENT_MAP_HOLE
) {
2096 userpage
= kmap_atomic(page
, KM_USER0
);
2097 memset(userpage
+ page_offset
, 0, iosize
);
2098 flush_dcache_page(page
);
2099 kunmap_atomic(userpage
, KM_USER0
);
2101 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2103 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2105 page_offset
+= iosize
;
2108 /* the get_extent function already copied into the page */
2109 if (test_range_bit(tree
, cur
, cur_end
,
2110 EXTENT_UPTODATE
, 1, NULL
)) {
2111 check_page_uptodate(tree
, page
);
2112 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2114 page_offset
+= iosize
;
2117 /* we have an inline extent but it didn't get marked up
2118 * to date. Error out
2120 if (block_start
== EXTENT_MAP_INLINE
) {
2122 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2124 page_offset
+= iosize
;
2129 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2130 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2134 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2136 ret
= submit_extent_page(READ
, tree
, page
,
2137 sector
, disk_io_size
, page_offset
,
2139 end_bio_extent_readpage
, mirror_num
,
2143 *bio_flags
= this_bio_flag
;
2148 page_offset
+= iosize
;
2151 if (!PageError(page
))
2152 SetPageUptodate(page
);
2158 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2159 get_extent_t
*get_extent
)
2161 struct bio
*bio
= NULL
;
2162 unsigned long bio_flags
= 0;
2165 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, 0,
2168 submit_one_bio(READ
, bio
, 0, bio_flags
);
2172 static noinline
void update_nr_written(struct page
*page
,
2173 struct writeback_control
*wbc
,
2174 unsigned long nr_written
)
2176 wbc
->nr_to_write
-= nr_written
;
2177 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2178 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2179 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2183 * the writepage semantics are similar to regular writepage. extent
2184 * records are inserted to lock ranges in the tree, and as dirty areas
2185 * are found, they are marked writeback. Then the lock bits are removed
2186 * and the end_io handler clears the writeback ranges
2188 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2191 struct inode
*inode
= page
->mapping
->host
;
2192 struct extent_page_data
*epd
= data
;
2193 struct extent_io_tree
*tree
= epd
->tree
;
2194 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2196 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2200 u64 last_byte
= i_size_read(inode
);
2205 struct extent_state
*cached_state
= NULL
;
2206 struct extent_map
*em
;
2207 struct block_device
*bdev
;
2210 size_t pg_offset
= 0;
2212 loff_t i_size
= i_size_read(inode
);
2213 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2219 unsigned long nr_written
= 0;
2221 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2222 write_flags
= WRITE_SYNC_PLUG
;
2224 write_flags
= WRITE
;
2226 WARN_ON(!PageLocked(page
));
2227 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2228 if (page
->index
> end_index
||
2229 (page
->index
== end_index
&& !pg_offset
)) {
2230 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2235 if (page
->index
== end_index
) {
2238 userpage
= kmap_atomic(page
, KM_USER0
);
2239 memset(userpage
+ pg_offset
, 0,
2240 PAGE_CACHE_SIZE
- pg_offset
);
2241 kunmap_atomic(userpage
, KM_USER0
);
2242 flush_dcache_page(page
);
2246 set_page_extent_mapped(page
);
2248 delalloc_start
= start
;
2251 if (!epd
->extent_locked
) {
2252 u64 delalloc_to_write
= 0;
2254 * make sure the wbc mapping index is at least updated
2257 update_nr_written(page
, wbc
, 0);
2259 while (delalloc_end
< page_end
) {
2260 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2265 if (nr_delalloc
== 0) {
2266 delalloc_start
= delalloc_end
+ 1;
2269 tree
->ops
->fill_delalloc(inode
, page
, delalloc_start
,
2270 delalloc_end
, &page_started
,
2273 * delalloc_end is already one less than the total
2274 * length, so we don't subtract one from
2277 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2280 delalloc_start
= delalloc_end
+ 1;
2282 if (wbc
->nr_to_write
< delalloc_to_write
) {
2285 if (delalloc_to_write
< thresh
* 2)
2286 thresh
= delalloc_to_write
;
2287 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2291 /* did the fill delalloc function already unlock and start
2297 * we've unlocked the page, so we can't update
2298 * the mapping's writeback index, just update
2301 wbc
->nr_to_write
-= nr_written
;
2305 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2306 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2308 if (ret
== -EAGAIN
) {
2309 redirty_page_for_writepage(wbc
, page
);
2310 update_nr_written(page
, wbc
, nr_written
);
2318 * we don't want to touch the inode after unlocking the page,
2319 * so we update the mapping writeback index now
2321 update_nr_written(page
, wbc
, nr_written
+ 1);
2324 if (last_byte
<= start
) {
2325 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2326 tree
->ops
->writepage_end_io_hook(page
, start
,
2328 unlock_start
= page_end
+ 1;
2332 blocksize
= inode
->i_sb
->s_blocksize
;
2334 while (cur
<= end
) {
2335 if (cur
>= last_byte
) {
2336 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2337 tree
->ops
->writepage_end_io_hook(page
, cur
,
2339 unlock_start
= page_end
+ 1;
2342 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2344 if (IS_ERR(em
) || !em
) {
2349 extent_offset
= cur
- em
->start
;
2350 BUG_ON(extent_map_end(em
) <= cur
);
2352 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2353 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2354 sector
= (em
->block_start
+ extent_offset
) >> 9;
2356 block_start
= em
->block_start
;
2357 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2358 free_extent_map(em
);
2362 * compressed and inline extents are written through other
2365 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2366 block_start
== EXTENT_MAP_INLINE
) {
2368 * end_io notification does not happen here for
2369 * compressed extents
2371 if (!compressed
&& tree
->ops
&&
2372 tree
->ops
->writepage_end_io_hook
)
2373 tree
->ops
->writepage_end_io_hook(page
, cur
,
2376 else if (compressed
) {
2377 /* we don't want to end_page_writeback on
2378 * a compressed extent. this happens
2385 pg_offset
+= iosize
;
2389 /* leave this out until we have a page_mkwrite call */
2390 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2391 EXTENT_DIRTY
, 0, NULL
)) {
2393 pg_offset
+= iosize
;
2397 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2398 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
2406 unsigned long max_nr
= end_index
+ 1;
2408 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
2409 if (!PageWriteback(page
)) {
2410 printk(KERN_ERR
"btrfs warning page %lu not "
2411 "writeback, cur %llu end %llu\n",
2412 page
->index
, (unsigned long long)cur
,
2413 (unsigned long long)end
);
2416 ret
= submit_extent_page(write_flags
, tree
, page
,
2417 sector
, iosize
, pg_offset
,
2418 bdev
, &epd
->bio
, max_nr
,
2419 end_bio_extent_writepage
,
2425 pg_offset
+= iosize
;
2430 /* make sure the mapping tag for page dirty gets cleared */
2431 set_page_writeback(page
);
2432 end_page_writeback(page
);
2438 /* drop our reference on any cached states */
2439 free_extent_state(cached_state
);
2444 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2445 * @mapping: address space structure to write
2446 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2447 * @writepage: function called for each page
2448 * @data: data passed to writepage function
2450 * If a page is already under I/O, write_cache_pages() skips it, even
2451 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2452 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2453 * and msync() need to guarantee that all the data which was dirty at the time
2454 * the call was made get new I/O started against them. If wbc->sync_mode is
2455 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2456 * existing IO to complete.
2458 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
2459 struct address_space
*mapping
,
2460 struct writeback_control
*wbc
,
2461 writepage_t writepage
, void *data
,
2462 void (*flush_fn
)(void *))
2466 int nr_to_write_done
= 0;
2467 struct pagevec pvec
;
2470 pgoff_t end
; /* Inclusive */
2472 int range_whole
= 0;
2474 pagevec_init(&pvec
, 0);
2475 if (wbc
->range_cyclic
) {
2476 index
= mapping
->writeback_index
; /* Start from prev offset */
2479 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2480 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2481 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2486 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
2487 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
2488 PAGECACHE_TAG_DIRTY
, min(end
- index
,
2489 (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
2493 for (i
= 0; i
< nr_pages
; i
++) {
2494 struct page
*page
= pvec
.pages
[i
];
2497 * At this point we hold neither mapping->tree_lock nor
2498 * lock on the page itself: the page may be truncated or
2499 * invalidated (changing page->mapping to NULL), or even
2500 * swizzled back from swapper_space to tmpfs file
2503 if (tree
->ops
&& tree
->ops
->write_cache_pages_lock_hook
)
2504 tree
->ops
->write_cache_pages_lock_hook(page
);
2508 if (unlikely(page
->mapping
!= mapping
)) {
2513 if (!wbc
->range_cyclic
&& page
->index
> end
) {
2519 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
2520 if (PageWriteback(page
))
2522 wait_on_page_writeback(page
);
2525 if (PageWriteback(page
) ||
2526 !clear_page_dirty_for_io(page
)) {
2531 ret
= (*writepage
)(page
, wbc
, data
);
2533 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
2541 * the filesystem may choose to bump up nr_to_write.
2542 * We have to make sure to honor the new nr_to_write
2545 nr_to_write_done
= wbc
->nr_to_write
<= 0;
2547 pagevec_release(&pvec
);
2550 if (!scanned
&& !done
) {
2552 * We hit the last page and there is more work to be done: wrap
2553 * back to the start of the file
2562 static void flush_epd_write_bio(struct extent_page_data
*epd
)
2566 submit_one_bio(WRITE_SYNC
, epd
->bio
, 0, 0);
2568 submit_one_bio(WRITE
, epd
->bio
, 0, 0);
2573 static noinline
void flush_write_bio(void *data
)
2575 struct extent_page_data
*epd
= data
;
2576 flush_epd_write_bio(epd
);
2579 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2580 get_extent_t
*get_extent
,
2581 struct writeback_control
*wbc
)
2584 struct address_space
*mapping
= page
->mapping
;
2585 struct extent_page_data epd
= {
2588 .get_extent
= get_extent
,
2590 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2592 struct writeback_control wbc_writepages
= {
2594 .sync_mode
= wbc
->sync_mode
,
2595 .older_than_this
= NULL
,
2597 .range_start
= page_offset(page
) + PAGE_CACHE_SIZE
,
2598 .range_end
= (loff_t
)-1,
2601 ret
= __extent_writepage(page
, wbc
, &epd
);
2603 extent_write_cache_pages(tree
, mapping
, &wbc_writepages
,
2604 __extent_writepage
, &epd
, flush_write_bio
);
2605 flush_epd_write_bio(&epd
);
2609 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
2610 u64 start
, u64 end
, get_extent_t
*get_extent
,
2614 struct address_space
*mapping
= inode
->i_mapping
;
2616 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
2619 struct extent_page_data epd
= {
2622 .get_extent
= get_extent
,
2624 .sync_io
= mode
== WB_SYNC_ALL
,
2626 struct writeback_control wbc_writepages
= {
2627 .bdi
= inode
->i_mapping
->backing_dev_info
,
2629 .older_than_this
= NULL
,
2630 .nr_to_write
= nr_pages
* 2,
2631 .range_start
= start
,
2632 .range_end
= end
+ 1,
2635 while (start
<= end
) {
2636 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
2637 if (clear_page_dirty_for_io(page
))
2638 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
2640 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2641 tree
->ops
->writepage_end_io_hook(page
, start
,
2642 start
+ PAGE_CACHE_SIZE
- 1,
2646 page_cache_release(page
);
2647 start
+= PAGE_CACHE_SIZE
;
2650 flush_epd_write_bio(&epd
);
2654 int extent_writepages(struct extent_io_tree
*tree
,
2655 struct address_space
*mapping
,
2656 get_extent_t
*get_extent
,
2657 struct writeback_control
*wbc
)
2660 struct extent_page_data epd
= {
2663 .get_extent
= get_extent
,
2665 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2668 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
2669 __extent_writepage
, &epd
,
2671 flush_epd_write_bio(&epd
);
2675 int extent_readpages(struct extent_io_tree
*tree
,
2676 struct address_space
*mapping
,
2677 struct list_head
*pages
, unsigned nr_pages
,
2678 get_extent_t get_extent
)
2680 struct bio
*bio
= NULL
;
2682 struct pagevec pvec
;
2683 unsigned long bio_flags
= 0;
2685 pagevec_init(&pvec
, 0);
2686 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
2687 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
2689 prefetchw(&page
->flags
);
2690 list_del(&page
->lru
);
2692 * what we want to do here is call add_to_page_cache_lru,
2693 * but that isn't exported, so we reproduce it here
2695 if (!add_to_page_cache(page
, mapping
,
2696 page
->index
, GFP_KERNEL
)) {
2698 /* open coding of lru_cache_add, also not exported */
2699 page_cache_get(page
);
2700 if (!pagevec_add(&pvec
, page
))
2701 __pagevec_lru_add_file(&pvec
);
2702 __extent_read_full_page(tree
, page
, get_extent
,
2703 &bio
, 0, &bio_flags
);
2705 page_cache_release(page
);
2707 if (pagevec_count(&pvec
))
2708 __pagevec_lru_add_file(&pvec
);
2709 BUG_ON(!list_empty(pages
));
2711 submit_one_bio(READ
, bio
, 0, bio_flags
);
2716 * basic invalidatepage code, this waits on any locked or writeback
2717 * ranges corresponding to the page, and then deletes any extent state
2718 * records from the tree
2720 int extent_invalidatepage(struct extent_io_tree
*tree
,
2721 struct page
*page
, unsigned long offset
)
2723 struct extent_state
*cached_state
= NULL
;
2724 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
2725 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2726 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
2728 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
2732 lock_extent_bits(tree
, start
, end
, 0, &cached_state
, GFP_NOFS
);
2733 wait_on_page_writeback(page
);
2734 clear_extent_bit(tree
, start
, end
,
2735 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
2736 EXTENT_DO_ACCOUNTING
,
2737 1, 1, &cached_state
, GFP_NOFS
);
2742 * simple commit_write call, set_range_dirty is used to mark both
2743 * the pages and the extent records as dirty
2745 int extent_commit_write(struct extent_io_tree
*tree
,
2746 struct inode
*inode
, struct page
*page
,
2747 unsigned from
, unsigned to
)
2749 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
2751 set_page_extent_mapped(page
);
2752 set_page_dirty(page
);
2754 if (pos
> inode
->i_size
) {
2755 i_size_write(inode
, pos
);
2756 mark_inode_dirty(inode
);
2761 int extent_prepare_write(struct extent_io_tree
*tree
,
2762 struct inode
*inode
, struct page
*page
,
2763 unsigned from
, unsigned to
, get_extent_t
*get_extent
)
2765 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2766 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2768 u64 orig_block_start
;
2771 struct extent_map
*em
;
2772 unsigned blocksize
= 1 << inode
->i_blkbits
;
2773 size_t page_offset
= 0;
2774 size_t block_off_start
;
2775 size_t block_off_end
;
2781 set_page_extent_mapped(page
);
2783 block_start
= (page_start
+ from
) & ~((u64
)blocksize
- 1);
2784 block_end
= (page_start
+ to
- 1) | (blocksize
- 1);
2785 orig_block_start
= block_start
;
2787 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2788 while (block_start
<= block_end
) {
2789 em
= get_extent(inode
, page
, page_offset
, block_start
,
2790 block_end
- block_start
+ 1, 1);
2791 if (IS_ERR(em
) || !em
)
2794 cur_end
= min(block_end
, extent_map_end(em
) - 1);
2795 block_off_start
= block_start
& (PAGE_CACHE_SIZE
- 1);
2796 block_off_end
= block_off_start
+ blocksize
;
2797 isnew
= clear_extent_new(tree
, block_start
, cur_end
, GFP_NOFS
);
2799 if (!PageUptodate(page
) && isnew
&&
2800 (block_off_end
> to
|| block_off_start
< from
)) {
2803 kaddr
= kmap_atomic(page
, KM_USER0
);
2804 if (block_off_end
> to
)
2805 memset(kaddr
+ to
, 0, block_off_end
- to
);
2806 if (block_off_start
< from
)
2807 memset(kaddr
+ block_off_start
, 0,
2808 from
- block_off_start
);
2809 flush_dcache_page(page
);
2810 kunmap_atomic(kaddr
, KM_USER0
);
2812 if ((em
->block_start
!= EXTENT_MAP_HOLE
&&
2813 em
->block_start
!= EXTENT_MAP_INLINE
) &&
2814 !isnew
&& !PageUptodate(page
) &&
2815 (block_off_end
> to
|| block_off_start
< from
) &&
2816 !test_range_bit(tree
, block_start
, cur_end
,
2817 EXTENT_UPTODATE
, 1, NULL
)) {
2819 u64 extent_offset
= block_start
- em
->start
;
2821 sector
= (em
->block_start
+ extent_offset
) >> 9;
2822 iosize
= (cur_end
- block_start
+ blocksize
) &
2823 ~((u64
)blocksize
- 1);
2825 * we've already got the extent locked, but we
2826 * need to split the state such that our end_bio
2827 * handler can clear the lock.
2829 set_extent_bit(tree
, block_start
,
2830 block_start
+ iosize
- 1,
2831 EXTENT_LOCKED
, 0, NULL
, NULL
, GFP_NOFS
);
2832 ret
= submit_extent_page(READ
, tree
, page
,
2833 sector
, iosize
, page_offset
, em
->bdev
,
2835 end_bio_extent_preparewrite
, 0,
2838 block_start
= block_start
+ iosize
;
2840 set_extent_uptodate(tree
, block_start
, cur_end
,
2842 unlock_extent(tree
, block_start
, cur_end
, GFP_NOFS
);
2843 block_start
= cur_end
+ 1;
2845 page_offset
= block_start
& (PAGE_CACHE_SIZE
- 1);
2846 free_extent_map(em
);
2849 wait_extent_bit(tree
, orig_block_start
,
2850 block_end
, EXTENT_LOCKED
);
2852 check_page_uptodate(tree
, page
);
2854 /* FIXME, zero out newly allocated blocks on error */
2859 * a helper for releasepage, this tests for areas of the page that
2860 * are locked or under IO and drops the related state bits if it is safe
2863 int try_release_extent_state(struct extent_map_tree
*map
,
2864 struct extent_io_tree
*tree
, struct page
*page
,
2867 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2868 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2871 if (test_range_bit(tree
, start
, end
,
2872 EXTENT_IOBITS
, 0, NULL
))
2875 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
2878 * at this point we can safely clear everything except the
2879 * locked bit and the nodatasum bit
2881 clear_extent_bit(tree
, start
, end
,
2882 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
2889 * a helper for releasepage. As long as there are no locked extents
2890 * in the range corresponding to the page, both state records and extent
2891 * map records are removed
2893 int try_release_extent_mapping(struct extent_map_tree
*map
,
2894 struct extent_io_tree
*tree
, struct page
*page
,
2897 struct extent_map
*em
;
2898 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2899 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2901 if ((mask
& __GFP_WAIT
) &&
2902 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
2904 while (start
<= end
) {
2905 len
= end
- start
+ 1;
2906 write_lock(&map
->lock
);
2907 em
= lookup_extent_mapping(map
, start
, len
);
2908 if (!em
|| IS_ERR(em
)) {
2909 write_unlock(&map
->lock
);
2912 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
2913 em
->start
!= start
) {
2914 write_unlock(&map
->lock
);
2915 free_extent_map(em
);
2918 if (!test_range_bit(tree
, em
->start
,
2919 extent_map_end(em
) - 1,
2920 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
2922 remove_extent_mapping(map
, em
);
2923 /* once for the rb tree */
2924 free_extent_map(em
);
2926 start
= extent_map_end(em
);
2927 write_unlock(&map
->lock
);
2930 free_extent_map(em
);
2933 return try_release_extent_state(map
, tree
, page
, mask
);
2936 sector_t
extent_bmap(struct address_space
*mapping
, sector_t iblock
,
2937 get_extent_t
*get_extent
)
2939 struct inode
*inode
= mapping
->host
;
2940 struct extent_state
*cached_state
= NULL
;
2941 u64 start
= iblock
<< inode
->i_blkbits
;
2942 sector_t sector
= 0;
2943 size_t blksize
= (1 << inode
->i_blkbits
);
2944 struct extent_map
*em
;
2946 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2947 0, &cached_state
, GFP_NOFS
);
2948 em
= get_extent(inode
, NULL
, 0, start
, blksize
, 0);
2949 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
,
2950 start
+ blksize
- 1, &cached_state
, GFP_NOFS
);
2951 if (!em
|| IS_ERR(em
))
2954 if (em
->block_start
> EXTENT_MAP_LAST_BYTE
)
2957 sector
= (em
->block_start
+ start
- em
->start
) >> inode
->i_blkbits
;
2959 free_extent_map(em
);
2963 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
2964 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
2968 u64 max
= start
+ len
;
2971 struct extent_map
*em
= NULL
;
2972 struct extent_state
*cached_state
= NULL
;
2974 u64 em_start
= 0, em_len
= 0;
2975 unsigned long emflags
;
2981 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
2982 &cached_state
, GFP_NOFS
);
2983 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
2991 off
= em
->start
+ em
->len
;
2995 em_start
= em
->start
;
3001 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
3003 flags
|= FIEMAP_EXTENT_LAST
;
3004 } else if (em
->block_start
== EXTENT_MAP_HOLE
) {
3005 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
3006 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
3007 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
3008 FIEMAP_EXTENT_NOT_ALIGNED
);
3009 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
3010 flags
|= (FIEMAP_EXTENT_DELALLOC
|
3011 FIEMAP_EXTENT_UNKNOWN
);
3013 disko
= em
->block_start
;
3015 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
3016 flags
|= FIEMAP_EXTENT_ENCODED
;
3018 emflags
= em
->flags
;
3019 free_extent_map(em
);
3023 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
3030 emflags
= em
->flags
;
3032 if (test_bit(EXTENT_FLAG_VACANCY
, &emflags
)) {
3033 flags
|= FIEMAP_EXTENT_LAST
;
3037 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3043 free_extent_map(em
);
3045 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3046 &cached_state
, GFP_NOFS
);
3050 static inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
3054 struct address_space
*mapping
;
3057 return eb
->first_page
;
3058 i
+= eb
->start
>> PAGE_CACHE_SHIFT
;
3059 mapping
= eb
->first_page
->mapping
;
3064 * extent_buffer_page is only called after pinning the page
3065 * by increasing the reference count. So we know the page must
3066 * be in the radix tree.
3069 p
= radix_tree_lookup(&mapping
->page_tree
, i
);
3075 static inline unsigned long num_extent_pages(u64 start
, u64 len
)
3077 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
3078 (start
>> PAGE_CACHE_SHIFT
);
3081 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3086 struct extent_buffer
*eb
= NULL
;
3088 unsigned long flags
;
3091 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
3094 spin_lock_init(&eb
->lock
);
3095 init_waitqueue_head(&eb
->lock_wq
);
3098 spin_lock_irqsave(&leak_lock
, flags
);
3099 list_add(&eb
->leak_list
, &buffers
);
3100 spin_unlock_irqrestore(&leak_lock
, flags
);
3102 atomic_set(&eb
->refs
, 1);
3107 static void __free_extent_buffer(struct extent_buffer
*eb
)
3110 unsigned long flags
;
3111 spin_lock_irqsave(&leak_lock
, flags
);
3112 list_del(&eb
->leak_list
);
3113 spin_unlock_irqrestore(&leak_lock
, flags
);
3115 kmem_cache_free(extent_buffer_cache
, eb
);
3118 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
3119 u64 start
, unsigned long len
,
3123 unsigned long num_pages
= num_extent_pages(start
, len
);
3125 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
3126 struct extent_buffer
*eb
;
3127 struct extent_buffer
*exists
= NULL
;
3129 struct address_space
*mapping
= tree
->mapping
;
3132 spin_lock(&tree
->buffer_lock
);
3133 eb
= buffer_search(tree
, start
);
3135 atomic_inc(&eb
->refs
);
3136 spin_unlock(&tree
->buffer_lock
);
3137 mark_page_accessed(eb
->first_page
);
3140 spin_unlock(&tree
->buffer_lock
);
3142 eb
= __alloc_extent_buffer(tree
, start
, len
, mask
);
3147 eb
->first_page
= page0
;
3150 page_cache_get(page0
);
3151 mark_page_accessed(page0
);
3152 set_page_extent_mapped(page0
);
3153 set_page_extent_head(page0
, len
);
3154 uptodate
= PageUptodate(page0
);
3158 for (; i
< num_pages
; i
++, index
++) {
3159 p
= find_or_create_page(mapping
, index
, mask
| __GFP_HIGHMEM
);
3164 set_page_extent_mapped(p
);
3165 mark_page_accessed(p
);
3168 set_page_extent_head(p
, len
);
3170 set_page_private(p
, EXTENT_PAGE_PRIVATE
);
3172 if (!PageUptodate(p
))
3177 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3179 spin_lock(&tree
->buffer_lock
);
3180 exists
= buffer_tree_insert(tree
, start
, &eb
->rb_node
);
3182 /* add one reference for the caller */
3183 atomic_inc(&exists
->refs
);
3184 spin_unlock(&tree
->buffer_lock
);
3187 /* add one reference for the tree */
3188 atomic_inc(&eb
->refs
);
3189 spin_unlock(&tree
->buffer_lock
);
3193 if (!atomic_dec_and_test(&eb
->refs
))
3195 for (index
= 1; index
< i
; index
++)
3196 page_cache_release(extent_buffer_page(eb
, index
));
3197 page_cache_release(extent_buffer_page(eb
, 0));
3198 __free_extent_buffer(eb
);
3202 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
3203 u64 start
, unsigned long len
,
3206 struct extent_buffer
*eb
;
3208 spin_lock(&tree
->buffer_lock
);
3209 eb
= buffer_search(tree
, start
);
3211 atomic_inc(&eb
->refs
);
3212 spin_unlock(&tree
->buffer_lock
);
3215 mark_page_accessed(eb
->first_page
);
3220 void free_extent_buffer(struct extent_buffer
*eb
)
3225 if (!atomic_dec_and_test(&eb
->refs
))
3231 int clear_extent_buffer_dirty(struct extent_io_tree
*tree
,
3232 struct extent_buffer
*eb
)
3235 unsigned long num_pages
;
3238 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3240 for (i
= 0; i
< num_pages
; i
++) {
3241 page
= extent_buffer_page(eb
, i
);
3242 if (!PageDirty(page
))
3247 set_page_extent_head(page
, eb
->len
);
3249 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
3251 clear_page_dirty_for_io(page
);
3252 spin_lock_irq(&page
->mapping
->tree_lock
);
3253 if (!PageDirty(page
)) {
3254 radix_tree_tag_clear(&page
->mapping
->page_tree
,
3256 PAGECACHE_TAG_DIRTY
);
3258 spin_unlock_irq(&page
->mapping
->tree_lock
);
3264 int wait_on_extent_buffer_writeback(struct extent_io_tree
*tree
,
3265 struct extent_buffer
*eb
)
3267 return wait_on_extent_writeback(tree
, eb
->start
,
3268 eb
->start
+ eb
->len
- 1);
3271 int set_extent_buffer_dirty(struct extent_io_tree
*tree
,
3272 struct extent_buffer
*eb
)
3275 unsigned long num_pages
;
3278 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
3279 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3280 for (i
= 0; i
< num_pages
; i
++)
3281 __set_page_dirty_nobuffers(extent_buffer_page(eb
, i
));
3285 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3286 struct extent_buffer
*eb
,
3287 struct extent_state
**cached_state
)
3291 unsigned long num_pages
;
3293 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3294 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3296 clear_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3297 cached_state
, GFP_NOFS
);
3298 for (i
= 0; i
< num_pages
; i
++) {
3299 page
= extent_buffer_page(eb
, i
);
3301 ClearPageUptodate(page
);
3306 int set_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3307 struct extent_buffer
*eb
)
3311 unsigned long num_pages
;
3313 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3315 set_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3317 for (i
= 0; i
< num_pages
; i
++) {
3318 page
= extent_buffer_page(eb
, i
);
3319 if ((i
== 0 && (eb
->start
& (PAGE_CACHE_SIZE
- 1))) ||
3320 ((i
== num_pages
- 1) &&
3321 ((eb
->start
+ eb
->len
) & (PAGE_CACHE_SIZE
- 1)))) {
3322 check_page_uptodate(tree
, page
);
3325 SetPageUptodate(page
);
3330 int extent_range_uptodate(struct extent_io_tree
*tree
,
3335 int pg_uptodate
= 1;
3337 unsigned long index
;
3339 ret
= test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
);
3342 while (start
<= end
) {
3343 index
= start
>> PAGE_CACHE_SHIFT
;
3344 page
= find_get_page(tree
->mapping
, index
);
3345 uptodate
= PageUptodate(page
);
3346 page_cache_release(page
);
3351 start
+= PAGE_CACHE_SIZE
;
3356 int extent_buffer_uptodate(struct extent_io_tree
*tree
,
3357 struct extent_buffer
*eb
,
3358 struct extent_state
*cached_state
)
3361 unsigned long num_pages
;
3364 int pg_uptodate
= 1;
3366 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3369 ret
= test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3370 EXTENT_UPTODATE
, 1, cached_state
);
3374 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3375 for (i
= 0; i
< num_pages
; i
++) {
3376 page
= extent_buffer_page(eb
, i
);
3377 if (!PageUptodate(page
)) {
3385 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
3386 struct extent_buffer
*eb
,
3387 u64 start
, int wait
,
3388 get_extent_t
*get_extent
, int mirror_num
)
3391 unsigned long start_i
;
3395 int locked_pages
= 0;
3396 int all_uptodate
= 1;
3397 int inc_all_pages
= 0;
3398 unsigned long num_pages
;
3399 struct bio
*bio
= NULL
;
3400 unsigned long bio_flags
= 0;
3402 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3405 if (test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3406 EXTENT_UPTODATE
, 1, NULL
)) {
3411 WARN_ON(start
< eb
->start
);
3412 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
3413 (eb
->start
>> PAGE_CACHE_SHIFT
);
3418 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3419 for (i
= start_i
; i
< num_pages
; i
++) {
3420 page
= extent_buffer_page(eb
, i
);
3422 if (!trylock_page(page
))
3428 if (!PageUptodate(page
))
3433 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3437 for (i
= start_i
; i
< num_pages
; i
++) {
3438 page
= extent_buffer_page(eb
, i
);
3440 page_cache_get(page
);
3441 if (!PageUptodate(page
)) {
3444 ClearPageError(page
);
3445 err
= __extent_read_full_page(tree
, page
,
3447 mirror_num
, &bio_flags
);
3456 submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
3461 for (i
= start_i
; i
< num_pages
; i
++) {
3462 page
= extent_buffer_page(eb
, i
);
3463 wait_on_page_locked(page
);
3464 if (!PageUptodate(page
))
3469 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3474 while (locked_pages
> 0) {
3475 page
= extent_buffer_page(eb
, i
);
3483 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
3484 unsigned long start
,
3491 char *dst
= (char *)dstv
;
3492 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3493 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3495 WARN_ON(start
> eb
->len
);
3496 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3498 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3501 page
= extent_buffer_page(eb
, i
);
3503 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3504 kaddr
= kmap_atomic(page
, KM_USER1
);
3505 memcpy(dst
, kaddr
+ offset
, cur
);
3506 kunmap_atomic(kaddr
, KM_USER1
);
3515 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3516 unsigned long min_len
, char **token
, char **map
,
3517 unsigned long *map_start
,
3518 unsigned long *map_len
, int km
)
3520 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
3523 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3524 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3525 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
3532 offset
= start_offset
;
3536 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
3539 if (start
+ min_len
> eb
->len
) {
3540 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
3541 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
3542 eb
->len
, start
, min_len
);
3546 p
= extent_buffer_page(eb
, i
);
3547 kaddr
= kmap_atomic(p
, km
);
3549 *map
= kaddr
+ offset
;
3550 *map_len
= PAGE_CACHE_SIZE
- offset
;
3554 int map_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3555 unsigned long min_len
,
3556 char **token
, char **map
,
3557 unsigned long *map_start
,
3558 unsigned long *map_len
, int km
)
3562 if (eb
->map_token
) {
3563 unmap_extent_buffer(eb
, eb
->map_token
, km
);
3564 eb
->map_token
= NULL
;
3567 err
= map_private_extent_buffer(eb
, start
, min_len
, token
, map
,
3568 map_start
, map_len
, km
);
3570 eb
->map_token
= *token
;
3572 eb
->map_start
= *map_start
;
3573 eb
->map_len
= *map_len
;
3578 void unmap_extent_buffer(struct extent_buffer
*eb
, char *token
, int km
)
3580 kunmap_atomic(token
, km
);
3583 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
3584 unsigned long start
,
3591 char *ptr
= (char *)ptrv
;
3592 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3593 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3596 WARN_ON(start
> eb
->len
);
3597 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3599 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3602 page
= extent_buffer_page(eb
, i
);
3604 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3606 kaddr
= kmap_atomic(page
, KM_USER0
);
3607 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
3608 kunmap_atomic(kaddr
, KM_USER0
);
3620 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
3621 unsigned long start
, unsigned long len
)
3627 char *src
= (char *)srcv
;
3628 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3629 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3631 WARN_ON(start
> eb
->len
);
3632 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3634 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3637 page
= extent_buffer_page(eb
, i
);
3638 WARN_ON(!PageUptodate(page
));
3640 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3641 kaddr
= kmap_atomic(page
, KM_USER1
);
3642 memcpy(kaddr
+ offset
, src
, cur
);
3643 kunmap_atomic(kaddr
, KM_USER1
);
3652 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
3653 unsigned long start
, unsigned long len
)
3659 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3660 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3662 WARN_ON(start
> eb
->len
);
3663 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3665 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3668 page
= extent_buffer_page(eb
, i
);
3669 WARN_ON(!PageUptodate(page
));
3671 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3672 kaddr
= kmap_atomic(page
, KM_USER0
);
3673 memset(kaddr
+ offset
, c
, cur
);
3674 kunmap_atomic(kaddr
, KM_USER0
);
3682 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
3683 unsigned long dst_offset
, unsigned long src_offset
,
3686 u64 dst_len
= dst
->len
;
3691 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3692 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3694 WARN_ON(src
->len
!= dst_len
);
3696 offset
= (start_offset
+ dst_offset
) &
3697 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3700 page
= extent_buffer_page(dst
, i
);
3701 WARN_ON(!PageUptodate(page
));
3703 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
3705 kaddr
= kmap_atomic(page
, KM_USER0
);
3706 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
3707 kunmap_atomic(kaddr
, KM_USER0
);
3716 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
3717 unsigned long dst_off
, unsigned long src_off
,
3720 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3721 if (dst_page
== src_page
) {
3722 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
3724 char *src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3725 char *p
= dst_kaddr
+ dst_off
+ len
;
3726 char *s
= src_kaddr
+ src_off
+ len
;
3731 kunmap_atomic(src_kaddr
, KM_USER1
);
3733 kunmap_atomic(dst_kaddr
, KM_USER0
);
3736 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
3737 unsigned long dst_off
, unsigned long src_off
,
3740 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3743 if (dst_page
!= src_page
)
3744 src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3746 src_kaddr
= dst_kaddr
;
3748 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
3749 kunmap_atomic(dst_kaddr
, KM_USER0
);
3750 if (dst_page
!= src_page
)
3751 kunmap_atomic(src_kaddr
, KM_USER1
);
3754 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3755 unsigned long src_offset
, unsigned long len
)
3758 size_t dst_off_in_page
;
3759 size_t src_off_in_page
;
3760 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3761 unsigned long dst_i
;
3762 unsigned long src_i
;
3764 if (src_offset
+ len
> dst
->len
) {
3765 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3766 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
3769 if (dst_offset
+ len
> dst
->len
) {
3770 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3771 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
3776 dst_off_in_page
= (start_offset
+ dst_offset
) &
3777 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3778 src_off_in_page
= (start_offset
+ src_offset
) &
3779 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3781 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3782 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
3784 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
3786 cur
= min_t(unsigned long, cur
,
3787 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
3789 copy_pages(extent_buffer_page(dst
, dst_i
),
3790 extent_buffer_page(dst
, src_i
),
3791 dst_off_in_page
, src_off_in_page
, cur
);
3799 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3800 unsigned long src_offset
, unsigned long len
)
3803 size_t dst_off_in_page
;
3804 size_t src_off_in_page
;
3805 unsigned long dst_end
= dst_offset
+ len
- 1;
3806 unsigned long src_end
= src_offset
+ len
- 1;
3807 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3808 unsigned long dst_i
;
3809 unsigned long src_i
;
3811 if (src_offset
+ len
> dst
->len
) {
3812 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3813 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
3816 if (dst_offset
+ len
> dst
->len
) {
3817 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3818 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
3821 if (dst_offset
< src_offset
) {
3822 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
3826 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
3827 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
3829 dst_off_in_page
= (start_offset
+ dst_end
) &
3830 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3831 src_off_in_page
= (start_offset
+ src_end
) &
3832 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3834 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
3835 cur
= min(cur
, dst_off_in_page
+ 1);
3836 move_pages(extent_buffer_page(dst
, dst_i
),
3837 extent_buffer_page(dst
, src_i
),
3838 dst_off_in_page
- cur
+ 1,
3839 src_off_in_page
- cur
+ 1, cur
);
3847 int try_release_extent_buffer(struct extent_io_tree
*tree
, struct page
*page
)
3849 u64 start
= page_offset(page
);
3850 struct extent_buffer
*eb
;
3853 unsigned long num_pages
;
3855 spin_lock(&tree
->buffer_lock
);
3856 eb
= buffer_search(tree
, start
);
3860 if (atomic_read(&eb
->refs
) > 1) {
3864 if (test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3868 /* at this point we can safely release the extent buffer */
3869 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3870 for (i
= 0; i
< num_pages
; i
++)
3871 page_cache_release(extent_buffer_page(eb
, i
));
3872 rb_erase(&eb
->rb_node
, &tree
->buffer
);
3873 __free_extent_buffer(eb
);
3875 spin_unlock(&tree
->buffer_lock
);