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_node
= NULL
;
108 tree
->buffer
.rb_node
= NULL
;
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 ret
= state
->state
& bits
;
465 if ((bits
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
466 u64 range
= state
->end
- state
->start
+ 1;
467 WARN_ON(range
> tree
->dirty_bytes
);
468 tree
->dirty_bytes
-= range
;
470 clear_state_cb(tree
, state
, bits
);
471 state
->state
&= ~bits
;
474 if (delete || state
->state
== 0) {
476 clear_state_cb(tree
, state
, state
->state
);
477 rb_erase(&state
->rb_node
, &tree
->state
);
479 free_extent_state(state
);
484 merge_state(tree
, state
);
490 * clear some bits on a range in the tree. This may require splitting
491 * or inserting elements in the tree, so the gfp mask is used to
492 * indicate which allocations or sleeping are allowed.
494 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
495 * the given range from the tree regardless of state (ie for truncate).
497 * the range [start, end] is inclusive.
499 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
500 * bits were already set, or zero if none of the bits were already set.
502 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
503 int bits
, int wake
, int delete,
504 struct extent_state
**cached_state
,
507 struct extent_state
*state
;
508 struct extent_state
*cached
;
509 struct extent_state
*prealloc
= NULL
;
510 struct rb_node
*next_node
;
511 struct rb_node
*node
;
517 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
518 prealloc
= alloc_extent_state(mask
);
523 spin_lock(&tree
->lock
);
525 cached
= *cached_state
;
526 *cached_state
= NULL
;
528 if (cached
&& cached
->tree
&& cached
->start
== start
) {
529 atomic_dec(&cached
->refs
);
533 free_extent_state(cached
);
536 * this search will find the extents that end after
539 node
= tree_search(tree
, start
);
542 state
= rb_entry(node
, struct extent_state
, rb_node
);
544 if (state
->start
> end
)
546 WARN_ON(state
->end
< start
);
547 last_end
= state
->end
;
550 * | ---- desired range ---- |
552 * | ------------- state -------------- |
554 * We need to split the extent we found, and may flip
555 * bits on second half.
557 * If the extent we found extends past our range, we
558 * just split and search again. It'll get split again
559 * the next time though.
561 * If the extent we found is inside our range, we clear
562 * the desired bit on it.
565 if (state
->start
< start
) {
567 prealloc
= alloc_extent_state(GFP_ATOMIC
);
568 err
= split_state(tree
, state
, prealloc
, start
);
569 BUG_ON(err
== -EEXIST
);
573 if (state
->end
<= end
) {
574 set
|= clear_state_bit(tree
, state
, bits
, wake
,
576 if (last_end
== (u64
)-1)
578 start
= last_end
+ 1;
583 * | ---- desired range ---- |
585 * We need to split the extent, and clear the bit
588 if (state
->start
<= end
&& state
->end
> end
) {
590 prealloc
= alloc_extent_state(GFP_ATOMIC
);
591 err
= split_state(tree
, state
, prealloc
, end
+ 1);
592 BUG_ON(err
== -EEXIST
);
596 set
|= clear_state_bit(tree
, prealloc
, bits
, wake
, delete);
602 if (state
->end
< end
&& prealloc
&& !need_resched())
603 next_node
= rb_next(&state
->rb_node
);
607 set
|= clear_state_bit(tree
, state
, bits
, wake
, delete);
608 if (last_end
== (u64
)-1)
610 start
= last_end
+ 1;
611 if (start
<= end
&& next_node
) {
612 state
= rb_entry(next_node
, struct extent_state
,
614 if (state
->start
== start
)
620 spin_unlock(&tree
->lock
);
622 free_extent_state(prealloc
);
629 spin_unlock(&tree
->lock
);
630 if (mask
& __GFP_WAIT
)
635 static int wait_on_state(struct extent_io_tree
*tree
,
636 struct extent_state
*state
)
637 __releases(tree
->lock
)
638 __acquires(tree
->lock
)
641 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
642 spin_unlock(&tree
->lock
);
644 spin_lock(&tree
->lock
);
645 finish_wait(&state
->wq
, &wait
);
650 * waits for one or more bits to clear on a range in the state tree.
651 * The range [start, end] is inclusive.
652 * The tree lock is taken by this function
654 int wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
656 struct extent_state
*state
;
657 struct rb_node
*node
;
659 spin_lock(&tree
->lock
);
663 * this search will find all the extents that end after
666 node
= tree_search(tree
, start
);
670 state
= rb_entry(node
, struct extent_state
, rb_node
);
672 if (state
->start
> end
)
675 if (state
->state
& bits
) {
676 start
= state
->start
;
677 atomic_inc(&state
->refs
);
678 wait_on_state(tree
, state
);
679 free_extent_state(state
);
682 start
= state
->end
+ 1;
687 if (need_resched()) {
688 spin_unlock(&tree
->lock
);
690 spin_lock(&tree
->lock
);
694 spin_unlock(&tree
->lock
);
698 static int set_state_bits(struct extent_io_tree
*tree
,
699 struct extent_state
*state
,
704 ret
= set_state_cb(tree
, state
, bits
);
708 if ((bits
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
709 u64 range
= state
->end
- state
->start
+ 1;
710 tree
->dirty_bytes
+= range
;
712 state
->state
|= bits
;
717 static void cache_state(struct extent_state
*state
,
718 struct extent_state
**cached_ptr
)
720 if (cached_ptr
&& !(*cached_ptr
)) {
721 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
723 atomic_inc(&state
->refs
);
729 * set some bits on a range in the tree. This may require allocations or
730 * sleeping, so the gfp mask is used to indicate what is allowed.
732 * If any of the exclusive bits are set, this will fail with -EEXIST if some
733 * part of the range already has the desired bits set. The start of the
734 * existing range is returned in failed_start in this case.
736 * [start, end] is inclusive This takes the tree lock.
739 static int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
740 int bits
, int exclusive_bits
, u64
*failed_start
,
741 struct extent_state
**cached_state
,
744 struct extent_state
*state
;
745 struct extent_state
*prealloc
= NULL
;
746 struct rb_node
*node
;
752 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
753 prealloc
= alloc_extent_state(mask
);
758 spin_lock(&tree
->lock
);
759 if (cached_state
&& *cached_state
) {
760 state
= *cached_state
;
761 if (state
->start
== start
&& state
->tree
) {
762 node
= &state
->rb_node
;
767 * this search will find all the extents that end after
770 node
= tree_search(tree
, start
);
772 err
= insert_state(tree
, prealloc
, start
, end
, bits
);
774 BUG_ON(err
== -EEXIST
);
777 state
= rb_entry(node
, struct extent_state
, rb_node
);
779 last_start
= state
->start
;
780 last_end
= state
->end
;
783 * | ---- desired range ---- |
786 * Just lock what we found and keep going
788 if (state
->start
== start
&& state
->end
<= end
) {
789 struct rb_node
*next_node
;
790 if (state
->state
& exclusive_bits
) {
791 *failed_start
= state
->start
;
796 err
= set_state_bits(tree
, state
, bits
);
800 cache_state(state
, cached_state
);
801 merge_state(tree
, state
);
802 if (last_end
== (u64
)-1)
805 start
= last_end
+ 1;
806 if (start
< end
&& prealloc
&& !need_resched()) {
807 next_node
= rb_next(node
);
809 state
= rb_entry(next_node
, struct extent_state
,
811 if (state
->start
== start
)
819 * | ---- desired range ---- |
822 * | ------------- state -------------- |
824 * We need to split the extent we found, and may flip bits on
827 * If the extent we found extends past our
828 * range, we just split and search again. It'll get split
829 * again the next time though.
831 * If the extent we found is inside our range, we set the
834 if (state
->start
< start
) {
835 if (state
->state
& exclusive_bits
) {
836 *failed_start
= start
;
840 err
= split_state(tree
, state
, prealloc
, start
);
841 BUG_ON(err
== -EEXIST
);
845 if (state
->end
<= end
) {
846 err
= set_state_bits(tree
, state
, bits
);
849 cache_state(state
, cached_state
);
850 merge_state(tree
, state
);
851 if (last_end
== (u64
)-1)
853 start
= last_end
+ 1;
858 * | ---- desired range ---- |
859 * | state | or | state |
861 * There's a hole, we need to insert something in it and
862 * ignore the extent we found.
864 if (state
->start
> start
) {
866 if (end
< last_start
)
869 this_end
= last_start
- 1;
870 err
= insert_state(tree
, prealloc
, start
, this_end
,
872 BUG_ON(err
== -EEXIST
);
877 cache_state(prealloc
, cached_state
);
879 start
= this_end
+ 1;
883 * | ---- desired range ---- |
885 * We need to split the extent, and set the bit
888 if (state
->start
<= end
&& state
->end
> end
) {
889 if (state
->state
& exclusive_bits
) {
890 *failed_start
= start
;
894 err
= split_state(tree
, state
, prealloc
, end
+ 1);
895 BUG_ON(err
== -EEXIST
);
897 err
= set_state_bits(tree
, prealloc
, bits
);
902 cache_state(prealloc
, cached_state
);
903 merge_state(tree
, prealloc
);
911 spin_unlock(&tree
->lock
);
913 free_extent_state(prealloc
);
920 spin_unlock(&tree
->lock
);
921 if (mask
& __GFP_WAIT
)
926 /* wrappers around set/clear extent bit */
927 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
930 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, 0, NULL
,
934 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
935 int bits
, gfp_t mask
)
937 return set_extent_bit(tree
, start
, end
, bits
, 0, NULL
,
941 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
942 int bits
, gfp_t mask
)
944 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
947 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
950 return set_extent_bit(tree
, start
, end
,
951 EXTENT_DELALLOC
| EXTENT_DIRTY
| EXTENT_UPTODATE
,
952 0, NULL
, NULL
, mask
);
955 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
958 return clear_extent_bit(tree
, start
, end
,
959 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0,
963 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
966 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, NULL
,
970 static int clear_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
973 return clear_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, 0,
977 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
980 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, NULL
,
984 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
987 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
991 int wait_on_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
993 return wait_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
);
997 * either insert or lock state struct between start and end use mask to tell
998 * us if waiting is desired.
1000 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1001 int bits
, struct extent_state
**cached_state
, gfp_t mask
)
1006 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
1007 EXTENT_LOCKED
, &failed_start
,
1008 cached_state
, mask
);
1009 if (err
== -EEXIST
&& (mask
& __GFP_WAIT
)) {
1010 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
1011 start
= failed_start
;
1015 WARN_ON(start
> end
);
1020 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
1022 return lock_extent_bits(tree
, start
, end
, 0, NULL
, mask
);
1025 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1031 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1032 &failed_start
, NULL
, mask
);
1033 if (err
== -EEXIST
) {
1034 if (failed_start
> start
)
1035 clear_extent_bit(tree
, start
, failed_start
- 1,
1036 EXTENT_LOCKED
, 1, 0, NULL
, mask
);
1042 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1043 struct extent_state
**cached
, gfp_t mask
)
1045 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1049 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1052 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1057 * helper function to set pages and extents in the tree dirty
1059 int set_range_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1061 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1062 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1065 while (index
<= end_index
) {
1066 page
= find_get_page(tree
->mapping
, index
);
1068 __set_page_dirty_nobuffers(page
);
1069 page_cache_release(page
);
1076 * helper function to set both pages and extents in the tree writeback
1078 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1080 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1081 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1084 while (index
<= end_index
) {
1085 page
= find_get_page(tree
->mapping
, index
);
1087 set_page_writeback(page
);
1088 page_cache_release(page
);
1095 * find the first offset in the io tree with 'bits' set. zero is
1096 * returned if we find something, and *start_ret and *end_ret are
1097 * set to reflect the state struct that was found.
1099 * If nothing was found, 1 is returned, < 0 on error
1101 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1102 u64
*start_ret
, u64
*end_ret
, int bits
)
1104 struct rb_node
*node
;
1105 struct extent_state
*state
;
1108 spin_lock(&tree
->lock
);
1110 * this search will find all the extents that end after
1113 node
= tree_search(tree
, start
);
1118 state
= rb_entry(node
, struct extent_state
, rb_node
);
1119 if (state
->end
>= start
&& (state
->state
& bits
)) {
1120 *start_ret
= state
->start
;
1121 *end_ret
= state
->end
;
1125 node
= rb_next(node
);
1130 spin_unlock(&tree
->lock
);
1134 /* find the first state struct with 'bits' set after 'start', and
1135 * return it. tree->lock must be held. NULL will returned if
1136 * nothing was found after 'start'
1138 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1139 u64 start
, int bits
)
1141 struct rb_node
*node
;
1142 struct extent_state
*state
;
1145 * this search will find all the extents that end after
1148 node
= tree_search(tree
, start
);
1153 state
= rb_entry(node
, struct extent_state
, rb_node
);
1154 if (state
->end
>= start
&& (state
->state
& bits
))
1157 node
= rb_next(node
);
1166 * find a contiguous range of bytes in the file marked as delalloc, not
1167 * more than 'max_bytes'. start and end are used to return the range,
1169 * 1 is returned if we find something, 0 if nothing was in the tree
1171 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1172 u64
*start
, u64
*end
, u64 max_bytes
)
1174 struct rb_node
*node
;
1175 struct extent_state
*state
;
1176 u64 cur_start
= *start
;
1178 u64 total_bytes
= 0;
1180 spin_lock(&tree
->lock
);
1183 * this search will find all the extents that end after
1186 node
= tree_search(tree
, cur_start
);
1194 state
= rb_entry(node
, struct extent_state
, rb_node
);
1195 if (found
&& (state
->start
!= cur_start
||
1196 (state
->state
& EXTENT_BOUNDARY
))) {
1199 if (!(state
->state
& EXTENT_DELALLOC
)) {
1205 *start
= state
->start
;
1208 cur_start
= state
->end
+ 1;
1209 node
= rb_next(node
);
1212 total_bytes
+= state
->end
- state
->start
+ 1;
1213 if (total_bytes
>= max_bytes
)
1217 spin_unlock(&tree
->lock
);
1221 static noinline
int __unlock_for_delalloc(struct inode
*inode
,
1222 struct page
*locked_page
,
1226 struct page
*pages
[16];
1227 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1228 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1229 unsigned long nr_pages
= end_index
- index
+ 1;
1232 if (index
== locked_page
->index
&& end_index
== index
)
1235 while (nr_pages
> 0) {
1236 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1237 min_t(unsigned long, nr_pages
,
1238 ARRAY_SIZE(pages
)), pages
);
1239 for (i
= 0; i
< ret
; i
++) {
1240 if (pages
[i
] != locked_page
)
1241 unlock_page(pages
[i
]);
1242 page_cache_release(pages
[i
]);
1251 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1252 struct page
*locked_page
,
1256 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1257 unsigned long start_index
= index
;
1258 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1259 unsigned long pages_locked
= 0;
1260 struct page
*pages
[16];
1261 unsigned long nrpages
;
1265 /* the caller is responsible for locking the start index */
1266 if (index
== locked_page
->index
&& index
== end_index
)
1269 /* skip the page at the start index */
1270 nrpages
= end_index
- index
+ 1;
1271 while (nrpages
> 0) {
1272 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1273 min_t(unsigned long,
1274 nrpages
, ARRAY_SIZE(pages
)), pages
);
1279 /* now we have an array of pages, lock them all */
1280 for (i
= 0; i
< ret
; i
++) {
1282 * the caller is taking responsibility for
1285 if (pages
[i
] != locked_page
) {
1286 lock_page(pages
[i
]);
1287 if (!PageDirty(pages
[i
]) ||
1288 pages
[i
]->mapping
!= inode
->i_mapping
) {
1290 unlock_page(pages
[i
]);
1291 page_cache_release(pages
[i
]);
1295 page_cache_release(pages
[i
]);
1304 if (ret
&& pages_locked
) {
1305 __unlock_for_delalloc(inode
, locked_page
,
1307 ((u64
)(start_index
+ pages_locked
- 1)) <<
1314 * find a contiguous range of bytes in the file marked as delalloc, not
1315 * more than 'max_bytes'. start and end are used to return the range,
1317 * 1 is returned if we find something, 0 if nothing was in the tree
1319 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1320 struct extent_io_tree
*tree
,
1321 struct page
*locked_page
,
1322 u64
*start
, u64
*end
,
1328 struct extent_state
*cached_state
= NULL
;
1333 /* step one, find a bunch of delalloc bytes starting at start */
1334 delalloc_start
= *start
;
1336 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1338 if (!found
|| delalloc_end
<= *start
) {
1339 *start
= delalloc_start
;
1340 *end
= delalloc_end
;
1345 * start comes from the offset of locked_page. We have to lock
1346 * pages in order, so we can't process delalloc bytes before
1349 if (delalloc_start
< *start
)
1350 delalloc_start
= *start
;
1353 * make sure to limit the number of pages we try to lock down
1356 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1357 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1359 /* step two, lock all the pages after the page that has start */
1360 ret
= lock_delalloc_pages(inode
, locked_page
,
1361 delalloc_start
, delalloc_end
);
1362 if (ret
== -EAGAIN
) {
1363 /* some of the pages are gone, lets avoid looping by
1364 * shortening the size of the delalloc range we're searching
1366 free_extent_state(cached_state
);
1368 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1369 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1379 /* step three, lock the state bits for the whole range */
1380 lock_extent_bits(tree
, delalloc_start
, delalloc_end
,
1381 0, &cached_state
, GFP_NOFS
);
1383 /* then test to make sure it is all still delalloc */
1384 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1385 EXTENT_DELALLOC
, 1, cached_state
);
1387 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1388 &cached_state
, GFP_NOFS
);
1389 __unlock_for_delalloc(inode
, locked_page
,
1390 delalloc_start
, delalloc_end
);
1394 free_extent_state(cached_state
);
1395 *start
= delalloc_start
;
1396 *end
= delalloc_end
;
1401 int extent_clear_unlock_delalloc(struct inode
*inode
,
1402 struct extent_io_tree
*tree
,
1403 u64 start
, u64 end
, struct page
*locked_page
,
1406 int clear_delalloc
, int clear_dirty
,
1412 struct page
*pages
[16];
1413 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1414 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1415 unsigned long nr_pages
= end_index
- index
+ 1;
1420 clear_bits
|= EXTENT_LOCKED
;
1422 clear_bits
|= EXTENT_DIRTY
;
1425 clear_bits
|= EXTENT_DELALLOC
;
1427 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1428 if (!(unlock_pages
|| clear_dirty
|| set_writeback
|| end_writeback
||
1432 while (nr_pages
> 0) {
1433 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1434 min_t(unsigned long,
1435 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1436 for (i
= 0; i
< ret
; i
++) {
1439 SetPagePrivate2(pages
[i
]);
1441 if (pages
[i
] == locked_page
) {
1442 page_cache_release(pages
[i
]);
1446 clear_page_dirty_for_io(pages
[i
]);
1448 set_page_writeback(pages
[i
]);
1450 end_page_writeback(pages
[i
]);
1452 unlock_page(pages
[i
]);
1453 page_cache_release(pages
[i
]);
1463 * count the number of bytes in the tree that have a given bit(s)
1464 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1465 * cached. The total number found is returned.
1467 u64
count_range_bits(struct extent_io_tree
*tree
,
1468 u64
*start
, u64 search_end
, u64 max_bytes
,
1471 struct rb_node
*node
;
1472 struct extent_state
*state
;
1473 u64 cur_start
= *start
;
1474 u64 total_bytes
= 0;
1477 if (search_end
<= cur_start
) {
1482 spin_lock(&tree
->lock
);
1483 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1484 total_bytes
= tree
->dirty_bytes
;
1488 * this search will find all the extents that end after
1491 node
= tree_search(tree
, cur_start
);
1496 state
= rb_entry(node
, struct extent_state
, rb_node
);
1497 if (state
->start
> search_end
)
1499 if (state
->end
>= cur_start
&& (state
->state
& bits
)) {
1500 total_bytes
+= min(search_end
, state
->end
) + 1 -
1501 max(cur_start
, state
->start
);
1502 if (total_bytes
>= max_bytes
)
1505 *start
= state
->start
;
1509 node
= rb_next(node
);
1514 spin_unlock(&tree
->lock
);
1519 * set the private field for a given byte offset in the tree. If there isn't
1520 * an extent_state there already, this does nothing.
1522 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1524 struct rb_node
*node
;
1525 struct extent_state
*state
;
1528 spin_lock(&tree
->lock
);
1530 * this search will find all the extents that end after
1533 node
= tree_search(tree
, start
);
1538 state
= rb_entry(node
, struct extent_state
, rb_node
);
1539 if (state
->start
!= start
) {
1543 state
->private = private;
1545 spin_unlock(&tree
->lock
);
1549 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1551 struct rb_node
*node
;
1552 struct extent_state
*state
;
1555 spin_lock(&tree
->lock
);
1557 * this search will find all the extents that end after
1560 node
= tree_search(tree
, start
);
1565 state
= rb_entry(node
, struct extent_state
, rb_node
);
1566 if (state
->start
!= start
) {
1570 *private = state
->private;
1572 spin_unlock(&tree
->lock
);
1577 * searches a range in the state tree for a given mask.
1578 * If 'filled' == 1, this returns 1 only if every extent in the tree
1579 * has the bits set. Otherwise, 1 is returned if any bit in the
1580 * range is found set.
1582 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1583 int bits
, int filled
, struct extent_state
*cached
)
1585 struct extent_state
*state
= NULL
;
1586 struct rb_node
*node
;
1589 spin_lock(&tree
->lock
);
1590 if (cached
&& cached
->tree
&& cached
->start
== start
)
1591 node
= &cached
->rb_node
;
1593 node
= tree_search(tree
, start
);
1594 while (node
&& start
<= end
) {
1595 state
= rb_entry(node
, struct extent_state
, rb_node
);
1597 if (filled
&& state
->start
> start
) {
1602 if (state
->start
> end
)
1605 if (state
->state
& bits
) {
1609 } else if (filled
) {
1614 if (state
->end
== (u64
)-1)
1617 start
= state
->end
+ 1;
1620 node
= rb_next(node
);
1627 spin_unlock(&tree
->lock
);
1632 * helper function to set a given page up to date if all the
1633 * extents in the tree for that page are up to date
1635 static int check_page_uptodate(struct extent_io_tree
*tree
,
1638 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1639 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1640 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1641 SetPageUptodate(page
);
1646 * helper function to unlock a page if all the extents in the tree
1647 * for that page are unlocked
1649 static int check_page_locked(struct extent_io_tree
*tree
,
1652 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1653 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1654 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1660 * helper function to end page writeback if all the extents
1661 * in the tree for that page are done with writeback
1663 static int check_page_writeback(struct extent_io_tree
*tree
,
1666 end_page_writeback(page
);
1670 /* lots and lots of room for performance fixes in the end_bio funcs */
1673 * after a writepage IO is done, we need to:
1674 * clear the uptodate bits on error
1675 * clear the writeback bits in the extent tree for this IO
1676 * end_page_writeback if the page has no more pending IO
1678 * Scheduling is not allowed, so the extent state tree is expected
1679 * to have one and only one object corresponding to this IO.
1681 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
1683 int uptodate
= err
== 0;
1684 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1685 struct extent_io_tree
*tree
;
1692 struct page
*page
= bvec
->bv_page
;
1693 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1695 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1697 end
= start
+ bvec
->bv_len
- 1;
1699 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1704 if (--bvec
>= bio
->bi_io_vec
)
1705 prefetchw(&bvec
->bv_page
->flags
);
1706 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
1707 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
1708 end
, NULL
, uptodate
);
1713 if (!uptodate
&& tree
->ops
&&
1714 tree
->ops
->writepage_io_failed_hook
) {
1715 ret
= tree
->ops
->writepage_io_failed_hook(bio
, page
,
1718 uptodate
= (err
== 0);
1724 clear_extent_uptodate(tree
, start
, end
, GFP_NOFS
);
1725 ClearPageUptodate(page
);
1730 end_page_writeback(page
);
1732 check_page_writeback(tree
, page
);
1733 } while (bvec
>= bio
->bi_io_vec
);
1739 * after a readpage IO is done, we need to:
1740 * clear the uptodate bits on error
1741 * set the uptodate bits if things worked
1742 * set the page up to date if all extents in the tree are uptodate
1743 * clear the lock bit in the extent tree
1744 * unlock the page if there are no other extents locked for it
1746 * Scheduling is not allowed, so the extent state tree is expected
1747 * to have one and only one object corresponding to this IO.
1749 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
1751 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1752 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1753 struct extent_io_tree
*tree
;
1763 struct page
*page
= bvec
->bv_page
;
1764 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1766 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1768 end
= start
+ bvec
->bv_len
- 1;
1770 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1775 if (--bvec
>= bio
->bi_io_vec
)
1776 prefetchw(&bvec
->bv_page
->flags
);
1778 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
1779 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
1784 if (!uptodate
&& tree
->ops
&&
1785 tree
->ops
->readpage_io_failed_hook
) {
1786 ret
= tree
->ops
->readpage_io_failed_hook(bio
, page
,
1790 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1798 set_extent_uptodate(tree
, start
, end
,
1801 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1805 SetPageUptodate(page
);
1807 ClearPageUptodate(page
);
1813 check_page_uptodate(tree
, page
);
1815 ClearPageUptodate(page
);
1818 check_page_locked(tree
, page
);
1820 } while (bvec
>= bio
->bi_io_vec
);
1826 * IO done from prepare_write is pretty simple, we just unlock
1827 * the structs in the extent tree when done, and set the uptodate bits
1830 static void end_bio_extent_preparewrite(struct bio
*bio
, int err
)
1832 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1833 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1834 struct extent_io_tree
*tree
;
1839 struct page
*page
= bvec
->bv_page
;
1840 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1842 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1844 end
= start
+ bvec
->bv_len
- 1;
1846 if (--bvec
>= bio
->bi_io_vec
)
1847 prefetchw(&bvec
->bv_page
->flags
);
1850 set_extent_uptodate(tree
, start
, end
, GFP_ATOMIC
);
1852 ClearPageUptodate(page
);
1856 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1858 } while (bvec
>= bio
->bi_io_vec
);
1864 extent_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
1869 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1871 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
1872 while (!bio
&& (nr_vecs
/= 2))
1873 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1878 bio
->bi_bdev
= bdev
;
1879 bio
->bi_sector
= first_sector
;
1884 static int submit_one_bio(int rw
, struct bio
*bio
, int mirror_num
,
1885 unsigned long bio_flags
)
1888 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1889 struct page
*page
= bvec
->bv_page
;
1890 struct extent_io_tree
*tree
= bio
->bi_private
;
1894 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
1895 end
= start
+ bvec
->bv_len
- 1;
1897 bio
->bi_private
= NULL
;
1901 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
1902 tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
1903 mirror_num
, bio_flags
);
1905 submit_bio(rw
, bio
);
1906 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
1912 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
1913 struct page
*page
, sector_t sector
,
1914 size_t size
, unsigned long offset
,
1915 struct block_device
*bdev
,
1916 struct bio
**bio_ret
,
1917 unsigned long max_pages
,
1918 bio_end_io_t end_io_func
,
1920 unsigned long prev_bio_flags
,
1921 unsigned long bio_flags
)
1927 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
1928 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
1929 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
1931 if (bio_ret
&& *bio_ret
) {
1934 contig
= bio
->bi_sector
== sector
;
1936 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
1939 if (prev_bio_flags
!= bio_flags
|| !contig
||
1940 (tree
->ops
&& tree
->ops
->merge_bio_hook
&&
1941 tree
->ops
->merge_bio_hook(page
, offset
, page_size
, bio
,
1943 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
1944 ret
= submit_one_bio(rw
, bio
, mirror_num
,
1951 if (this_compressed
)
1954 nr
= bio_get_nr_vecs(bdev
);
1956 bio
= extent_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
1958 bio_add_page(bio
, page
, page_size
, offset
);
1959 bio
->bi_end_io
= end_io_func
;
1960 bio
->bi_private
= tree
;
1965 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
1970 void set_page_extent_mapped(struct page
*page
)
1972 if (!PagePrivate(page
)) {
1973 SetPagePrivate(page
);
1974 page_cache_get(page
);
1975 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
1979 static void set_page_extent_head(struct page
*page
, unsigned long len
)
1981 set_page_private(page
, EXTENT_PAGE_PRIVATE_FIRST_PAGE
| len
<< 2);
1985 * basic readpage implementation. Locked extent state structs are inserted
1986 * into the tree that are removed when the IO is done (by the end_io
1989 static int __extent_read_full_page(struct extent_io_tree
*tree
,
1991 get_extent_t
*get_extent
,
1992 struct bio
**bio
, int mirror_num
,
1993 unsigned long *bio_flags
)
1995 struct inode
*inode
= page
->mapping
->host
;
1996 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1997 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2001 u64 last_byte
= i_size_read(inode
);
2005 struct extent_map
*em
;
2006 struct block_device
*bdev
;
2009 size_t page_offset
= 0;
2011 size_t disk_io_size
;
2012 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2013 unsigned long this_bio_flag
= 0;
2015 set_page_extent_mapped(page
);
2018 lock_extent(tree
, start
, end
, GFP_NOFS
);
2020 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
2022 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
2025 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
2026 userpage
= kmap_atomic(page
, KM_USER0
);
2027 memset(userpage
+ zero_offset
, 0, iosize
);
2028 flush_dcache_page(page
);
2029 kunmap_atomic(userpage
, KM_USER0
);
2032 while (cur
<= end
) {
2033 if (cur
>= last_byte
) {
2035 iosize
= PAGE_CACHE_SIZE
- page_offset
;
2036 userpage
= kmap_atomic(page
, KM_USER0
);
2037 memset(userpage
+ page_offset
, 0, iosize
);
2038 flush_dcache_page(page
);
2039 kunmap_atomic(userpage
, KM_USER0
);
2040 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2042 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2045 em
= get_extent(inode
, page
, page_offset
, cur
,
2047 if (IS_ERR(em
) || !em
) {
2049 unlock_extent(tree
, cur
, end
, GFP_NOFS
);
2052 extent_offset
= cur
- em
->start
;
2053 BUG_ON(extent_map_end(em
) <= cur
);
2056 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
2057 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2059 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2060 cur_end
= min(extent_map_end(em
) - 1, end
);
2061 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2062 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2063 disk_io_size
= em
->block_len
;
2064 sector
= em
->block_start
>> 9;
2066 sector
= (em
->block_start
+ extent_offset
) >> 9;
2067 disk_io_size
= iosize
;
2070 block_start
= em
->block_start
;
2071 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2072 block_start
= EXTENT_MAP_HOLE
;
2073 free_extent_map(em
);
2076 /* we've found a hole, just zero and go on */
2077 if (block_start
== EXTENT_MAP_HOLE
) {
2079 userpage
= kmap_atomic(page
, KM_USER0
);
2080 memset(userpage
+ page_offset
, 0, iosize
);
2081 flush_dcache_page(page
);
2082 kunmap_atomic(userpage
, KM_USER0
);
2084 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2086 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2088 page_offset
+= iosize
;
2091 /* the get_extent function already copied into the page */
2092 if (test_range_bit(tree
, cur
, cur_end
,
2093 EXTENT_UPTODATE
, 1, NULL
)) {
2094 check_page_uptodate(tree
, page
);
2095 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2097 page_offset
+= iosize
;
2100 /* we have an inline extent but it didn't get marked up
2101 * to date. Error out
2103 if (block_start
== EXTENT_MAP_INLINE
) {
2105 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2107 page_offset
+= iosize
;
2112 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2113 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2117 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2119 ret
= submit_extent_page(READ
, tree
, page
,
2120 sector
, disk_io_size
, page_offset
,
2122 end_bio_extent_readpage
, mirror_num
,
2126 *bio_flags
= this_bio_flag
;
2131 page_offset
+= iosize
;
2134 if (!PageError(page
))
2135 SetPageUptodate(page
);
2141 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2142 get_extent_t
*get_extent
)
2144 struct bio
*bio
= NULL
;
2145 unsigned long bio_flags
= 0;
2148 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, 0,
2151 submit_one_bio(READ
, bio
, 0, bio_flags
);
2155 static noinline
void update_nr_written(struct page
*page
,
2156 struct writeback_control
*wbc
,
2157 unsigned long nr_written
)
2159 wbc
->nr_to_write
-= nr_written
;
2160 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2161 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2162 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2166 * the writepage semantics are similar to regular writepage. extent
2167 * records are inserted to lock ranges in the tree, and as dirty areas
2168 * are found, they are marked writeback. Then the lock bits are removed
2169 * and the end_io handler clears the writeback ranges
2171 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2174 struct inode
*inode
= page
->mapping
->host
;
2175 struct extent_page_data
*epd
= data
;
2176 struct extent_io_tree
*tree
= epd
->tree
;
2177 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2179 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2183 u64 last_byte
= i_size_read(inode
);
2188 struct extent_state
*cached_state
= NULL
;
2189 struct extent_map
*em
;
2190 struct block_device
*bdev
;
2193 size_t pg_offset
= 0;
2195 loff_t i_size
= i_size_read(inode
);
2196 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2202 unsigned long nr_written
= 0;
2204 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2205 write_flags
= WRITE_SYNC_PLUG
;
2207 write_flags
= WRITE
;
2209 WARN_ON(!PageLocked(page
));
2210 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2211 if (page
->index
> end_index
||
2212 (page
->index
== end_index
&& !pg_offset
)) {
2213 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2218 if (page
->index
== end_index
) {
2221 userpage
= kmap_atomic(page
, KM_USER0
);
2222 memset(userpage
+ pg_offset
, 0,
2223 PAGE_CACHE_SIZE
- pg_offset
);
2224 kunmap_atomic(userpage
, KM_USER0
);
2225 flush_dcache_page(page
);
2229 set_page_extent_mapped(page
);
2231 delalloc_start
= start
;
2234 if (!epd
->extent_locked
) {
2235 u64 delalloc_to_write
= 0;
2237 * make sure the wbc mapping index is at least updated
2240 update_nr_written(page
, wbc
, 0);
2242 while (delalloc_end
< page_end
) {
2243 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2248 if (nr_delalloc
== 0) {
2249 delalloc_start
= delalloc_end
+ 1;
2252 tree
->ops
->fill_delalloc(inode
, page
, delalloc_start
,
2253 delalloc_end
, &page_started
,
2256 * delalloc_end is already one less than the total
2257 * length, so we don't subtract one from
2260 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2263 delalloc_start
= delalloc_end
+ 1;
2265 if (wbc
->nr_to_write
< delalloc_to_write
) {
2268 if (delalloc_to_write
< thresh
* 2)
2269 thresh
= delalloc_to_write
;
2270 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2274 /* did the fill delalloc function already unlock and start
2280 * we've unlocked the page, so we can't update
2281 * the mapping's writeback index, just update
2284 wbc
->nr_to_write
-= nr_written
;
2288 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2289 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2291 if (ret
== -EAGAIN
) {
2292 redirty_page_for_writepage(wbc
, page
);
2293 update_nr_written(page
, wbc
, nr_written
);
2301 * we don't want to touch the inode after unlocking the page,
2302 * so we update the mapping writeback index now
2304 update_nr_written(page
, wbc
, nr_written
+ 1);
2307 if (last_byte
<= start
) {
2308 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2309 tree
->ops
->writepage_end_io_hook(page
, start
,
2311 unlock_start
= page_end
+ 1;
2315 blocksize
= inode
->i_sb
->s_blocksize
;
2317 while (cur
<= end
) {
2318 if (cur
>= last_byte
) {
2319 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2320 tree
->ops
->writepage_end_io_hook(page
, cur
,
2322 unlock_start
= page_end
+ 1;
2325 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2327 if (IS_ERR(em
) || !em
) {
2332 extent_offset
= cur
- em
->start
;
2333 BUG_ON(extent_map_end(em
) <= cur
);
2335 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2336 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2337 sector
= (em
->block_start
+ extent_offset
) >> 9;
2339 block_start
= em
->block_start
;
2340 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2341 free_extent_map(em
);
2345 * compressed and inline extents are written through other
2348 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2349 block_start
== EXTENT_MAP_INLINE
) {
2351 * end_io notification does not happen here for
2352 * compressed extents
2354 if (!compressed
&& tree
->ops
&&
2355 tree
->ops
->writepage_end_io_hook
)
2356 tree
->ops
->writepage_end_io_hook(page
, cur
,
2359 else if (compressed
) {
2360 /* we don't want to end_page_writeback on
2361 * a compressed extent. this happens
2368 pg_offset
+= iosize
;
2372 /* leave this out until we have a page_mkwrite call */
2373 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2374 EXTENT_DIRTY
, 0, NULL
)) {
2376 pg_offset
+= iosize
;
2380 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2381 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
2389 unsigned long max_nr
= end_index
+ 1;
2391 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
2392 if (!PageWriteback(page
)) {
2393 printk(KERN_ERR
"btrfs warning page %lu not "
2394 "writeback, cur %llu end %llu\n",
2395 page
->index
, (unsigned long long)cur
,
2396 (unsigned long long)end
);
2399 ret
= submit_extent_page(write_flags
, tree
, page
,
2400 sector
, iosize
, pg_offset
,
2401 bdev
, &epd
->bio
, max_nr
,
2402 end_bio_extent_writepage
,
2408 pg_offset
+= iosize
;
2413 /* make sure the mapping tag for page dirty gets cleared */
2414 set_page_writeback(page
);
2415 end_page_writeback(page
);
2421 /* drop our reference on any cached states */
2422 free_extent_state(cached_state
);
2427 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2428 * @mapping: address space structure to write
2429 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2430 * @writepage: function called for each page
2431 * @data: data passed to writepage function
2433 * If a page is already under I/O, write_cache_pages() skips it, even
2434 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2435 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2436 * and msync() need to guarantee that all the data which was dirty at the time
2437 * the call was made get new I/O started against them. If wbc->sync_mode is
2438 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2439 * existing IO to complete.
2441 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
2442 struct address_space
*mapping
,
2443 struct writeback_control
*wbc
,
2444 writepage_t writepage
, void *data
,
2445 void (*flush_fn
)(void *))
2449 int nr_to_write_done
= 0;
2450 struct pagevec pvec
;
2453 pgoff_t end
; /* Inclusive */
2455 int range_whole
= 0;
2457 pagevec_init(&pvec
, 0);
2458 if (wbc
->range_cyclic
) {
2459 index
= mapping
->writeback_index
; /* Start from prev offset */
2462 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2463 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2464 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2469 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
2470 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
2471 PAGECACHE_TAG_DIRTY
, min(end
- index
,
2472 (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
2476 for (i
= 0; i
< nr_pages
; i
++) {
2477 struct page
*page
= pvec
.pages
[i
];
2480 * At this point we hold neither mapping->tree_lock nor
2481 * lock on the page itself: the page may be truncated or
2482 * invalidated (changing page->mapping to NULL), or even
2483 * swizzled back from swapper_space to tmpfs file
2486 if (tree
->ops
&& tree
->ops
->write_cache_pages_lock_hook
)
2487 tree
->ops
->write_cache_pages_lock_hook(page
);
2491 if (unlikely(page
->mapping
!= mapping
)) {
2496 if (!wbc
->range_cyclic
&& page
->index
> end
) {
2502 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
2503 if (PageWriteback(page
))
2505 wait_on_page_writeback(page
);
2508 if (PageWriteback(page
) ||
2509 !clear_page_dirty_for_io(page
)) {
2514 ret
= (*writepage
)(page
, wbc
, data
);
2516 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
2524 * the filesystem may choose to bump up nr_to_write.
2525 * We have to make sure to honor the new nr_to_write
2528 nr_to_write_done
= wbc
->nr_to_write
<= 0;
2530 pagevec_release(&pvec
);
2533 if (!scanned
&& !done
) {
2535 * We hit the last page and there is more work to be done: wrap
2536 * back to the start of the file
2545 static void flush_epd_write_bio(struct extent_page_data
*epd
)
2549 submit_one_bio(WRITE_SYNC
, epd
->bio
, 0, 0);
2551 submit_one_bio(WRITE
, epd
->bio
, 0, 0);
2556 static noinline
void flush_write_bio(void *data
)
2558 struct extent_page_data
*epd
= data
;
2559 flush_epd_write_bio(epd
);
2562 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2563 get_extent_t
*get_extent
,
2564 struct writeback_control
*wbc
)
2567 struct address_space
*mapping
= page
->mapping
;
2568 struct extent_page_data epd
= {
2571 .get_extent
= get_extent
,
2573 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2575 struct writeback_control wbc_writepages
= {
2577 .sync_mode
= wbc
->sync_mode
,
2578 .older_than_this
= NULL
,
2580 .range_start
= page_offset(page
) + PAGE_CACHE_SIZE
,
2581 .range_end
= (loff_t
)-1,
2584 ret
= __extent_writepage(page
, wbc
, &epd
);
2586 extent_write_cache_pages(tree
, mapping
, &wbc_writepages
,
2587 __extent_writepage
, &epd
, flush_write_bio
);
2588 flush_epd_write_bio(&epd
);
2592 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
2593 u64 start
, u64 end
, get_extent_t
*get_extent
,
2597 struct address_space
*mapping
= inode
->i_mapping
;
2599 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
2602 struct extent_page_data epd
= {
2605 .get_extent
= get_extent
,
2607 .sync_io
= mode
== WB_SYNC_ALL
,
2609 struct writeback_control wbc_writepages
= {
2610 .bdi
= inode
->i_mapping
->backing_dev_info
,
2612 .older_than_this
= NULL
,
2613 .nr_to_write
= nr_pages
* 2,
2614 .range_start
= start
,
2615 .range_end
= end
+ 1,
2618 while (start
<= end
) {
2619 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
2620 if (clear_page_dirty_for_io(page
))
2621 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
2623 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2624 tree
->ops
->writepage_end_io_hook(page
, start
,
2625 start
+ PAGE_CACHE_SIZE
- 1,
2629 page_cache_release(page
);
2630 start
+= PAGE_CACHE_SIZE
;
2633 flush_epd_write_bio(&epd
);
2637 int extent_writepages(struct extent_io_tree
*tree
,
2638 struct address_space
*mapping
,
2639 get_extent_t
*get_extent
,
2640 struct writeback_control
*wbc
)
2643 struct extent_page_data epd
= {
2646 .get_extent
= get_extent
,
2648 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2651 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
2652 __extent_writepage
, &epd
,
2654 flush_epd_write_bio(&epd
);
2658 int extent_readpages(struct extent_io_tree
*tree
,
2659 struct address_space
*mapping
,
2660 struct list_head
*pages
, unsigned nr_pages
,
2661 get_extent_t get_extent
)
2663 struct bio
*bio
= NULL
;
2665 struct pagevec pvec
;
2666 unsigned long bio_flags
= 0;
2668 pagevec_init(&pvec
, 0);
2669 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
2670 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
2672 prefetchw(&page
->flags
);
2673 list_del(&page
->lru
);
2675 * what we want to do here is call add_to_page_cache_lru,
2676 * but that isn't exported, so we reproduce it here
2678 if (!add_to_page_cache(page
, mapping
,
2679 page
->index
, GFP_KERNEL
)) {
2681 /* open coding of lru_cache_add, also not exported */
2682 page_cache_get(page
);
2683 if (!pagevec_add(&pvec
, page
))
2684 __pagevec_lru_add_file(&pvec
);
2685 __extent_read_full_page(tree
, page
, get_extent
,
2686 &bio
, 0, &bio_flags
);
2688 page_cache_release(page
);
2690 if (pagevec_count(&pvec
))
2691 __pagevec_lru_add_file(&pvec
);
2692 BUG_ON(!list_empty(pages
));
2694 submit_one_bio(READ
, bio
, 0, bio_flags
);
2699 * basic invalidatepage code, this waits on any locked or writeback
2700 * ranges corresponding to the page, and then deletes any extent state
2701 * records from the tree
2703 int extent_invalidatepage(struct extent_io_tree
*tree
,
2704 struct page
*page
, unsigned long offset
)
2706 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
2707 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2708 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
2710 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
2714 lock_extent(tree
, start
, end
, GFP_NOFS
);
2715 wait_on_page_writeback(page
);
2716 clear_extent_bit(tree
, start
, end
,
2717 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
,
2718 1, 1, NULL
, GFP_NOFS
);
2723 * simple commit_write call, set_range_dirty is used to mark both
2724 * the pages and the extent records as dirty
2726 int extent_commit_write(struct extent_io_tree
*tree
,
2727 struct inode
*inode
, struct page
*page
,
2728 unsigned from
, unsigned to
)
2730 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
2732 set_page_extent_mapped(page
);
2733 set_page_dirty(page
);
2735 if (pos
> inode
->i_size
) {
2736 i_size_write(inode
, pos
);
2737 mark_inode_dirty(inode
);
2742 int extent_prepare_write(struct extent_io_tree
*tree
,
2743 struct inode
*inode
, struct page
*page
,
2744 unsigned from
, unsigned to
, get_extent_t
*get_extent
)
2746 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2747 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2749 u64 orig_block_start
;
2752 struct extent_map
*em
;
2753 unsigned blocksize
= 1 << inode
->i_blkbits
;
2754 size_t page_offset
= 0;
2755 size_t block_off_start
;
2756 size_t block_off_end
;
2762 set_page_extent_mapped(page
);
2764 block_start
= (page_start
+ from
) & ~((u64
)blocksize
- 1);
2765 block_end
= (page_start
+ to
- 1) | (blocksize
- 1);
2766 orig_block_start
= block_start
;
2768 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2769 while (block_start
<= block_end
) {
2770 em
= get_extent(inode
, page
, page_offset
, block_start
,
2771 block_end
- block_start
+ 1, 1);
2772 if (IS_ERR(em
) || !em
)
2775 cur_end
= min(block_end
, extent_map_end(em
) - 1);
2776 block_off_start
= block_start
& (PAGE_CACHE_SIZE
- 1);
2777 block_off_end
= block_off_start
+ blocksize
;
2778 isnew
= clear_extent_new(tree
, block_start
, cur_end
, GFP_NOFS
);
2780 if (!PageUptodate(page
) && isnew
&&
2781 (block_off_end
> to
|| block_off_start
< from
)) {
2784 kaddr
= kmap_atomic(page
, KM_USER0
);
2785 if (block_off_end
> to
)
2786 memset(kaddr
+ to
, 0, block_off_end
- to
);
2787 if (block_off_start
< from
)
2788 memset(kaddr
+ block_off_start
, 0,
2789 from
- block_off_start
);
2790 flush_dcache_page(page
);
2791 kunmap_atomic(kaddr
, KM_USER0
);
2793 if ((em
->block_start
!= EXTENT_MAP_HOLE
&&
2794 em
->block_start
!= EXTENT_MAP_INLINE
) &&
2795 !isnew
&& !PageUptodate(page
) &&
2796 (block_off_end
> to
|| block_off_start
< from
) &&
2797 !test_range_bit(tree
, block_start
, cur_end
,
2798 EXTENT_UPTODATE
, 1, NULL
)) {
2800 u64 extent_offset
= block_start
- em
->start
;
2802 sector
= (em
->block_start
+ extent_offset
) >> 9;
2803 iosize
= (cur_end
- block_start
+ blocksize
) &
2804 ~((u64
)blocksize
- 1);
2806 * we've already got the extent locked, but we
2807 * need to split the state such that our end_bio
2808 * handler can clear the lock.
2810 set_extent_bit(tree
, block_start
,
2811 block_start
+ iosize
- 1,
2812 EXTENT_LOCKED
, 0, NULL
, NULL
, GFP_NOFS
);
2813 ret
= submit_extent_page(READ
, tree
, page
,
2814 sector
, iosize
, page_offset
, em
->bdev
,
2816 end_bio_extent_preparewrite
, 0,
2819 block_start
= block_start
+ iosize
;
2821 set_extent_uptodate(tree
, block_start
, cur_end
,
2823 unlock_extent(tree
, block_start
, cur_end
, GFP_NOFS
);
2824 block_start
= cur_end
+ 1;
2826 page_offset
= block_start
& (PAGE_CACHE_SIZE
- 1);
2827 free_extent_map(em
);
2830 wait_extent_bit(tree
, orig_block_start
,
2831 block_end
, EXTENT_LOCKED
);
2833 check_page_uptodate(tree
, page
);
2835 /* FIXME, zero out newly allocated blocks on error */
2840 * a helper for releasepage, this tests for areas of the page that
2841 * are locked or under IO and drops the related state bits if it is safe
2844 int try_release_extent_state(struct extent_map_tree
*map
,
2845 struct extent_io_tree
*tree
, struct page
*page
,
2848 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2849 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2852 if (test_range_bit(tree
, start
, end
,
2853 EXTENT_IOBITS
, 0, NULL
))
2856 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
2859 * at this point we can safely clear everything except the
2860 * locked bit and the nodatasum bit
2862 clear_extent_bit(tree
, start
, end
,
2863 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
2870 * a helper for releasepage. As long as there are no locked extents
2871 * in the range corresponding to the page, both state records and extent
2872 * map records are removed
2874 int try_release_extent_mapping(struct extent_map_tree
*map
,
2875 struct extent_io_tree
*tree
, struct page
*page
,
2878 struct extent_map
*em
;
2879 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2880 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2882 if ((mask
& __GFP_WAIT
) &&
2883 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
2885 while (start
<= end
) {
2886 len
= end
- start
+ 1;
2887 write_lock(&map
->lock
);
2888 em
= lookup_extent_mapping(map
, start
, len
);
2889 if (!em
|| IS_ERR(em
)) {
2890 write_unlock(&map
->lock
);
2893 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
2894 em
->start
!= start
) {
2895 write_unlock(&map
->lock
);
2896 free_extent_map(em
);
2899 if (!test_range_bit(tree
, em
->start
,
2900 extent_map_end(em
) - 1,
2901 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
2903 remove_extent_mapping(map
, em
);
2904 /* once for the rb tree */
2905 free_extent_map(em
);
2907 start
= extent_map_end(em
);
2908 write_unlock(&map
->lock
);
2911 free_extent_map(em
);
2914 return try_release_extent_state(map
, tree
, page
, mask
);
2917 sector_t
extent_bmap(struct address_space
*mapping
, sector_t iblock
,
2918 get_extent_t
*get_extent
)
2920 struct inode
*inode
= mapping
->host
;
2921 u64 start
= iblock
<< inode
->i_blkbits
;
2922 sector_t sector
= 0;
2923 size_t blksize
= (1 << inode
->i_blkbits
);
2924 struct extent_map
*em
;
2926 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2928 em
= get_extent(inode
, NULL
, 0, start
, blksize
, 0);
2929 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2931 if (!em
|| IS_ERR(em
))
2934 if (em
->block_start
> EXTENT_MAP_LAST_BYTE
)
2937 sector
= (em
->block_start
+ start
- em
->start
) >> inode
->i_blkbits
;
2939 free_extent_map(em
);
2943 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
2944 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
2948 u64 max
= start
+ len
;
2951 struct extent_map
*em
= NULL
;
2953 u64 em_start
= 0, em_len
= 0;
2954 unsigned long emflags
;
2960 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
2962 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
2970 off
= em
->start
+ em
->len
;
2974 em_start
= em
->start
;
2980 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
2982 flags
|= FIEMAP_EXTENT_LAST
;
2983 } else if (em
->block_start
== EXTENT_MAP_HOLE
) {
2984 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
2985 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
2986 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
2987 FIEMAP_EXTENT_NOT_ALIGNED
);
2988 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
2989 flags
|= (FIEMAP_EXTENT_DELALLOC
|
2990 FIEMAP_EXTENT_UNKNOWN
);
2992 disko
= em
->block_start
;
2994 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
2995 flags
|= FIEMAP_EXTENT_ENCODED
;
2997 emflags
= em
->flags
;
2998 free_extent_map(em
);
3002 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
3009 emflags
= em
->flags
;
3011 if (test_bit(EXTENT_FLAG_VACANCY
, &emflags
)) {
3012 flags
|= FIEMAP_EXTENT_LAST
;
3016 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3022 free_extent_map(em
);
3024 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3029 static inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
3033 struct address_space
*mapping
;
3036 return eb
->first_page
;
3037 i
+= eb
->start
>> PAGE_CACHE_SHIFT
;
3038 mapping
= eb
->first_page
->mapping
;
3043 * extent_buffer_page is only called after pinning the page
3044 * by increasing the reference count. So we know the page must
3045 * be in the radix tree.
3048 p
= radix_tree_lookup(&mapping
->page_tree
, i
);
3054 static inline unsigned long num_extent_pages(u64 start
, u64 len
)
3056 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
3057 (start
>> PAGE_CACHE_SHIFT
);
3060 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3065 struct extent_buffer
*eb
= NULL
;
3067 unsigned long flags
;
3070 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
3073 spin_lock_init(&eb
->lock
);
3074 init_waitqueue_head(&eb
->lock_wq
);
3077 spin_lock_irqsave(&leak_lock
, flags
);
3078 list_add(&eb
->leak_list
, &buffers
);
3079 spin_unlock_irqrestore(&leak_lock
, flags
);
3081 atomic_set(&eb
->refs
, 1);
3086 static void __free_extent_buffer(struct extent_buffer
*eb
)
3089 unsigned long flags
;
3090 spin_lock_irqsave(&leak_lock
, flags
);
3091 list_del(&eb
->leak_list
);
3092 spin_unlock_irqrestore(&leak_lock
, flags
);
3094 kmem_cache_free(extent_buffer_cache
, eb
);
3097 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
3098 u64 start
, unsigned long len
,
3102 unsigned long num_pages
= num_extent_pages(start
, len
);
3104 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
3105 struct extent_buffer
*eb
;
3106 struct extent_buffer
*exists
= NULL
;
3108 struct address_space
*mapping
= tree
->mapping
;
3111 spin_lock(&tree
->buffer_lock
);
3112 eb
= buffer_search(tree
, start
);
3114 atomic_inc(&eb
->refs
);
3115 spin_unlock(&tree
->buffer_lock
);
3116 mark_page_accessed(eb
->first_page
);
3119 spin_unlock(&tree
->buffer_lock
);
3121 eb
= __alloc_extent_buffer(tree
, start
, len
, mask
);
3126 eb
->first_page
= page0
;
3129 page_cache_get(page0
);
3130 mark_page_accessed(page0
);
3131 set_page_extent_mapped(page0
);
3132 set_page_extent_head(page0
, len
);
3133 uptodate
= PageUptodate(page0
);
3137 for (; i
< num_pages
; i
++, index
++) {
3138 p
= find_or_create_page(mapping
, index
, mask
| __GFP_HIGHMEM
);
3143 set_page_extent_mapped(p
);
3144 mark_page_accessed(p
);
3147 set_page_extent_head(p
, len
);
3149 set_page_private(p
, EXTENT_PAGE_PRIVATE
);
3151 if (!PageUptodate(p
))
3156 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3158 spin_lock(&tree
->buffer_lock
);
3159 exists
= buffer_tree_insert(tree
, start
, &eb
->rb_node
);
3161 /* add one reference for the caller */
3162 atomic_inc(&exists
->refs
);
3163 spin_unlock(&tree
->buffer_lock
);
3166 spin_unlock(&tree
->buffer_lock
);
3168 /* add one reference for the tree */
3169 atomic_inc(&eb
->refs
);
3173 if (!atomic_dec_and_test(&eb
->refs
))
3175 for (index
= 1; index
< i
; index
++)
3176 page_cache_release(extent_buffer_page(eb
, index
));
3177 page_cache_release(extent_buffer_page(eb
, 0));
3178 __free_extent_buffer(eb
);
3182 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
3183 u64 start
, unsigned long len
,
3186 struct extent_buffer
*eb
;
3188 spin_lock(&tree
->buffer_lock
);
3189 eb
= buffer_search(tree
, start
);
3191 atomic_inc(&eb
->refs
);
3192 spin_unlock(&tree
->buffer_lock
);
3195 mark_page_accessed(eb
->first_page
);
3200 void free_extent_buffer(struct extent_buffer
*eb
)
3205 if (!atomic_dec_and_test(&eb
->refs
))
3211 int clear_extent_buffer_dirty(struct extent_io_tree
*tree
,
3212 struct extent_buffer
*eb
)
3215 unsigned long num_pages
;
3218 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3220 for (i
= 0; i
< num_pages
; i
++) {
3221 page
= extent_buffer_page(eb
, i
);
3222 if (!PageDirty(page
))
3227 set_page_extent_head(page
, eb
->len
);
3229 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
3231 clear_page_dirty_for_io(page
);
3232 spin_lock_irq(&page
->mapping
->tree_lock
);
3233 if (!PageDirty(page
)) {
3234 radix_tree_tag_clear(&page
->mapping
->page_tree
,
3236 PAGECACHE_TAG_DIRTY
);
3238 spin_unlock_irq(&page
->mapping
->tree_lock
);
3244 int wait_on_extent_buffer_writeback(struct extent_io_tree
*tree
,
3245 struct extent_buffer
*eb
)
3247 return wait_on_extent_writeback(tree
, eb
->start
,
3248 eb
->start
+ eb
->len
- 1);
3251 int set_extent_buffer_dirty(struct extent_io_tree
*tree
,
3252 struct extent_buffer
*eb
)
3255 unsigned long num_pages
;
3258 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
3259 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3260 for (i
= 0; i
< num_pages
; i
++)
3261 __set_page_dirty_nobuffers(extent_buffer_page(eb
, i
));
3265 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3266 struct extent_buffer
*eb
)
3270 unsigned long num_pages
;
3272 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3273 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3275 clear_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3277 for (i
= 0; i
< num_pages
; i
++) {
3278 page
= extent_buffer_page(eb
, i
);
3280 ClearPageUptodate(page
);
3285 int set_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3286 struct extent_buffer
*eb
)
3290 unsigned long num_pages
;
3292 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3294 set_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3296 for (i
= 0; i
< num_pages
; i
++) {
3297 page
= extent_buffer_page(eb
, i
);
3298 if ((i
== 0 && (eb
->start
& (PAGE_CACHE_SIZE
- 1))) ||
3299 ((i
== num_pages
- 1) &&
3300 ((eb
->start
+ eb
->len
) & (PAGE_CACHE_SIZE
- 1)))) {
3301 check_page_uptodate(tree
, page
);
3304 SetPageUptodate(page
);
3309 int extent_range_uptodate(struct extent_io_tree
*tree
,
3314 int pg_uptodate
= 1;
3316 unsigned long index
;
3318 ret
= test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
);
3321 while (start
<= end
) {
3322 index
= start
>> PAGE_CACHE_SHIFT
;
3323 page
= find_get_page(tree
->mapping
, index
);
3324 uptodate
= PageUptodate(page
);
3325 page_cache_release(page
);
3330 start
+= PAGE_CACHE_SIZE
;
3335 int extent_buffer_uptodate(struct extent_io_tree
*tree
,
3336 struct extent_buffer
*eb
)
3339 unsigned long num_pages
;
3342 int pg_uptodate
= 1;
3344 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3347 ret
= test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3348 EXTENT_UPTODATE
, 1, NULL
);
3352 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3353 for (i
= 0; i
< num_pages
; i
++) {
3354 page
= extent_buffer_page(eb
, i
);
3355 if (!PageUptodate(page
)) {
3363 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
3364 struct extent_buffer
*eb
,
3365 u64 start
, int wait
,
3366 get_extent_t
*get_extent
, int mirror_num
)
3369 unsigned long start_i
;
3373 int locked_pages
= 0;
3374 int all_uptodate
= 1;
3375 int inc_all_pages
= 0;
3376 unsigned long num_pages
;
3377 struct bio
*bio
= NULL
;
3378 unsigned long bio_flags
= 0;
3380 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3383 if (test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3384 EXTENT_UPTODATE
, 1, NULL
)) {
3389 WARN_ON(start
< eb
->start
);
3390 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
3391 (eb
->start
>> PAGE_CACHE_SHIFT
);
3396 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3397 for (i
= start_i
; i
< num_pages
; i
++) {
3398 page
= extent_buffer_page(eb
, i
);
3400 if (!trylock_page(page
))
3406 if (!PageUptodate(page
))
3411 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3415 for (i
= start_i
; i
< num_pages
; i
++) {
3416 page
= extent_buffer_page(eb
, i
);
3418 page_cache_get(page
);
3419 if (!PageUptodate(page
)) {
3422 ClearPageError(page
);
3423 err
= __extent_read_full_page(tree
, page
,
3425 mirror_num
, &bio_flags
);
3434 submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
3439 for (i
= start_i
; i
< num_pages
; i
++) {
3440 page
= extent_buffer_page(eb
, i
);
3441 wait_on_page_locked(page
);
3442 if (!PageUptodate(page
))
3447 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3452 while (locked_pages
> 0) {
3453 page
= extent_buffer_page(eb
, i
);
3461 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
3462 unsigned long start
,
3469 char *dst
= (char *)dstv
;
3470 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3471 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3473 WARN_ON(start
> eb
->len
);
3474 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3476 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3479 page
= extent_buffer_page(eb
, i
);
3481 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3482 kaddr
= kmap_atomic(page
, KM_USER1
);
3483 memcpy(dst
, kaddr
+ offset
, cur
);
3484 kunmap_atomic(kaddr
, KM_USER1
);
3493 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3494 unsigned long min_len
, char **token
, char **map
,
3495 unsigned long *map_start
,
3496 unsigned long *map_len
, int km
)
3498 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
3501 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3502 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3503 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
3510 offset
= start_offset
;
3514 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
3517 if (start
+ min_len
> eb
->len
) {
3518 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
3519 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
3520 eb
->len
, start
, min_len
);
3524 p
= extent_buffer_page(eb
, i
);
3525 kaddr
= kmap_atomic(p
, km
);
3527 *map
= kaddr
+ offset
;
3528 *map_len
= PAGE_CACHE_SIZE
- offset
;
3532 int map_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3533 unsigned long min_len
,
3534 char **token
, char **map
,
3535 unsigned long *map_start
,
3536 unsigned long *map_len
, int km
)
3540 if (eb
->map_token
) {
3541 unmap_extent_buffer(eb
, eb
->map_token
, km
);
3542 eb
->map_token
= NULL
;
3545 err
= map_private_extent_buffer(eb
, start
, min_len
, token
, map
,
3546 map_start
, map_len
, km
);
3548 eb
->map_token
= *token
;
3550 eb
->map_start
= *map_start
;
3551 eb
->map_len
= *map_len
;
3556 void unmap_extent_buffer(struct extent_buffer
*eb
, char *token
, int km
)
3558 kunmap_atomic(token
, km
);
3561 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
3562 unsigned long start
,
3569 char *ptr
= (char *)ptrv
;
3570 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3571 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3574 WARN_ON(start
> eb
->len
);
3575 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3577 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3580 page
= extent_buffer_page(eb
, i
);
3582 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3584 kaddr
= kmap_atomic(page
, KM_USER0
);
3585 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
3586 kunmap_atomic(kaddr
, KM_USER0
);
3598 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
3599 unsigned long start
, unsigned long len
)
3605 char *src
= (char *)srcv
;
3606 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3607 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3609 WARN_ON(start
> eb
->len
);
3610 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3612 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3615 page
= extent_buffer_page(eb
, i
);
3616 WARN_ON(!PageUptodate(page
));
3618 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3619 kaddr
= kmap_atomic(page
, KM_USER1
);
3620 memcpy(kaddr
+ offset
, src
, cur
);
3621 kunmap_atomic(kaddr
, KM_USER1
);
3630 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
3631 unsigned long start
, unsigned long len
)
3637 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3638 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3640 WARN_ON(start
> eb
->len
);
3641 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3643 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3646 page
= extent_buffer_page(eb
, i
);
3647 WARN_ON(!PageUptodate(page
));
3649 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3650 kaddr
= kmap_atomic(page
, KM_USER0
);
3651 memset(kaddr
+ offset
, c
, cur
);
3652 kunmap_atomic(kaddr
, KM_USER0
);
3660 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
3661 unsigned long dst_offset
, unsigned long src_offset
,
3664 u64 dst_len
= dst
->len
;
3669 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3670 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3672 WARN_ON(src
->len
!= dst_len
);
3674 offset
= (start_offset
+ dst_offset
) &
3675 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3678 page
= extent_buffer_page(dst
, i
);
3679 WARN_ON(!PageUptodate(page
));
3681 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
3683 kaddr
= kmap_atomic(page
, KM_USER0
);
3684 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
3685 kunmap_atomic(kaddr
, KM_USER0
);
3694 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
3695 unsigned long dst_off
, unsigned long src_off
,
3698 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3699 if (dst_page
== src_page
) {
3700 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
3702 char *src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3703 char *p
= dst_kaddr
+ dst_off
+ len
;
3704 char *s
= src_kaddr
+ src_off
+ len
;
3709 kunmap_atomic(src_kaddr
, KM_USER1
);
3711 kunmap_atomic(dst_kaddr
, KM_USER0
);
3714 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
3715 unsigned long dst_off
, unsigned long src_off
,
3718 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3721 if (dst_page
!= src_page
)
3722 src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3724 src_kaddr
= dst_kaddr
;
3726 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
3727 kunmap_atomic(dst_kaddr
, KM_USER0
);
3728 if (dst_page
!= src_page
)
3729 kunmap_atomic(src_kaddr
, KM_USER1
);
3732 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3733 unsigned long src_offset
, unsigned long len
)
3736 size_t dst_off_in_page
;
3737 size_t src_off_in_page
;
3738 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3739 unsigned long dst_i
;
3740 unsigned long src_i
;
3742 if (src_offset
+ len
> dst
->len
) {
3743 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3744 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
3747 if (dst_offset
+ len
> dst
->len
) {
3748 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3749 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
3754 dst_off_in_page
= (start_offset
+ dst_offset
) &
3755 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3756 src_off_in_page
= (start_offset
+ src_offset
) &
3757 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3759 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3760 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
3762 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
3764 cur
= min_t(unsigned long, cur
,
3765 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
3767 copy_pages(extent_buffer_page(dst
, dst_i
),
3768 extent_buffer_page(dst
, src_i
),
3769 dst_off_in_page
, src_off_in_page
, cur
);
3777 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3778 unsigned long src_offset
, unsigned long len
)
3781 size_t dst_off_in_page
;
3782 size_t src_off_in_page
;
3783 unsigned long dst_end
= dst_offset
+ len
- 1;
3784 unsigned long src_end
= src_offset
+ len
- 1;
3785 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3786 unsigned long dst_i
;
3787 unsigned long src_i
;
3789 if (src_offset
+ len
> dst
->len
) {
3790 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3791 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
3794 if (dst_offset
+ len
> dst
->len
) {
3795 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3796 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
3799 if (dst_offset
< src_offset
) {
3800 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
3804 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
3805 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
3807 dst_off_in_page
= (start_offset
+ dst_end
) &
3808 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3809 src_off_in_page
= (start_offset
+ src_end
) &
3810 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3812 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
3813 cur
= min(cur
, dst_off_in_page
+ 1);
3814 move_pages(extent_buffer_page(dst
, dst_i
),
3815 extent_buffer_page(dst
, src_i
),
3816 dst_off_in_page
- cur
+ 1,
3817 src_off_in_page
- cur
+ 1, cur
);
3825 int try_release_extent_buffer(struct extent_io_tree
*tree
, struct page
*page
)
3827 u64 start
= page_offset(page
);
3828 struct extent_buffer
*eb
;
3831 unsigned long num_pages
;
3833 spin_lock(&tree
->buffer_lock
);
3834 eb
= buffer_search(tree
, start
);
3838 if (atomic_read(&eb
->refs
) > 1) {
3842 if (test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3846 /* at this point we can safely release the extent buffer */
3847 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3848 for (i
= 0; i
< num_pages
; i
++)
3849 page_cache_release(extent_buffer_page(eb
, i
));
3850 rb_erase(&eb
->rb_node
, &tree
->buffer
);
3851 __free_extent_buffer(eb
);
3853 spin_unlock(&tree
->buffer_lock
);