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
)
284 * utility function to look for merge candidates inside a given range.
285 * Any extents with matching state are merged together into a single
286 * extent in the tree. Extents with EXTENT_IO in their state field
287 * are not merged because the end_io handlers need to be able to do
288 * operations on them without sleeping (or doing allocations/splits).
290 * This should be called with the tree lock held.
292 static int merge_state(struct extent_io_tree
*tree
,
293 struct extent_state
*state
)
295 struct extent_state
*other
;
296 struct rb_node
*other_node
;
298 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
301 other_node
= rb_prev(&state
->rb_node
);
303 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
304 if (other
->end
== state
->start
- 1 &&
305 other
->state
== state
->state
) {
306 state
->start
= other
->start
;
308 rb_erase(&other
->rb_node
, &tree
->state
);
309 free_extent_state(other
);
312 other_node
= rb_next(&state
->rb_node
);
314 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
315 if (other
->start
== state
->end
+ 1 &&
316 other
->state
== state
->state
) {
317 other
->start
= state
->start
;
319 rb_erase(&state
->rb_node
, &tree
->state
);
320 free_extent_state(state
);
326 static void set_state_cb(struct extent_io_tree
*tree
,
327 struct extent_state
*state
,
330 if (tree
->ops
&& tree
->ops
->set_bit_hook
) {
331 tree
->ops
->set_bit_hook(tree
->mapping
->host
, state
->start
,
332 state
->end
, state
->state
, bits
);
336 static void clear_state_cb(struct extent_io_tree
*tree
,
337 struct extent_state
*state
,
340 if (tree
->ops
&& tree
->ops
->clear_bit_hook
) {
341 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
->start
,
342 state
->end
, state
->state
, bits
);
347 * insert an extent_state struct into the tree. 'bits' are set on the
348 * struct before it is inserted.
350 * This may return -EEXIST if the extent is already there, in which case the
351 * state struct is freed.
353 * The tree lock is not taken internally. This is a utility function and
354 * probably isn't what you want to call (see set/clear_extent_bit).
356 static int insert_state(struct extent_io_tree
*tree
,
357 struct extent_state
*state
, u64 start
, u64 end
,
360 struct rb_node
*node
;
363 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
364 (unsigned long long)end
,
365 (unsigned long long)start
);
368 if (bits
& EXTENT_DIRTY
)
369 tree
->dirty_bytes
+= end
- start
+ 1;
370 set_state_cb(tree
, state
, bits
);
371 state
->state
|= bits
;
372 state
->start
= start
;
374 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
376 struct extent_state
*found
;
377 found
= rb_entry(node
, struct extent_state
, rb_node
);
378 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
379 "%llu %llu\n", (unsigned long long)found
->start
,
380 (unsigned long long)found
->end
,
381 (unsigned long long)start
, (unsigned long long)end
);
382 free_extent_state(state
);
386 merge_state(tree
, state
);
391 * split a given extent state struct in two, inserting the preallocated
392 * struct 'prealloc' as the newly created second half. 'split' indicates an
393 * offset inside 'orig' where it should be split.
396 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
397 * are two extent state structs in the tree:
398 * prealloc: [orig->start, split - 1]
399 * orig: [ split, orig->end ]
401 * The tree locks are not taken by this function. They need to be held
404 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
405 struct extent_state
*prealloc
, u64 split
)
407 struct rb_node
*node
;
408 prealloc
->start
= orig
->start
;
409 prealloc
->end
= split
- 1;
410 prealloc
->state
= orig
->state
;
413 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
415 free_extent_state(prealloc
);
418 prealloc
->tree
= tree
;
423 * utility function to clear some bits in an extent state struct.
424 * it will optionally wake up any one waiting on this state (wake == 1), or
425 * forcibly remove the state from the tree (delete == 1).
427 * If no bits are set on the state struct after clearing things, the
428 * struct is freed and removed from the tree
430 static int clear_state_bit(struct extent_io_tree
*tree
,
431 struct extent_state
*state
, int bits
, int wake
,
434 int ret
= state
->state
& bits
;
436 if ((bits
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
437 u64 range
= state
->end
- state
->start
+ 1;
438 WARN_ON(range
> tree
->dirty_bytes
);
439 tree
->dirty_bytes
-= range
;
441 clear_state_cb(tree
, state
, bits
);
442 state
->state
&= ~bits
;
445 if (delete || state
->state
== 0) {
447 clear_state_cb(tree
, state
, state
->state
);
448 rb_erase(&state
->rb_node
, &tree
->state
);
450 free_extent_state(state
);
455 merge_state(tree
, state
);
461 * clear some bits on a range in the tree. This may require splitting
462 * or inserting elements in the tree, so the gfp mask is used to
463 * indicate which allocations or sleeping are allowed.
465 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
466 * the given range from the tree regardless of state (ie for truncate).
468 * the range [start, end] is inclusive.
470 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
471 * bits were already set, or zero if none of the bits were already set.
473 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
474 int bits
, int wake
, int delete, gfp_t mask
)
476 struct extent_state
*state
;
477 struct extent_state
*prealloc
= NULL
;
478 struct rb_node
*node
;
484 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
485 prealloc
= alloc_extent_state(mask
);
490 spin_lock(&tree
->lock
);
492 * this search will find the extents that end after
495 node
= tree_search(tree
, start
);
498 state
= rb_entry(node
, struct extent_state
, rb_node
);
499 if (state
->start
> end
)
501 WARN_ON(state
->end
< start
);
502 last_end
= state
->end
;
505 * | ---- desired range ---- |
507 * | ------------- state -------------- |
509 * We need to split the extent we found, and may flip
510 * bits on second half.
512 * If the extent we found extends past our range, we
513 * just split and search again. It'll get split again
514 * the next time though.
516 * If the extent we found is inside our range, we clear
517 * the desired bit on it.
520 if (state
->start
< start
) {
522 prealloc
= alloc_extent_state(GFP_ATOMIC
);
523 err
= split_state(tree
, state
, prealloc
, start
);
524 BUG_ON(err
== -EEXIST
);
528 if (state
->end
<= end
) {
529 set
|= clear_state_bit(tree
, state
, bits
,
531 if (last_end
== (u64
)-1)
533 start
= last_end
+ 1;
535 start
= state
->start
;
540 * | ---- desired range ---- |
542 * We need to split the extent, and clear the bit
545 if (state
->start
<= end
&& state
->end
> end
) {
547 prealloc
= alloc_extent_state(GFP_ATOMIC
);
548 err
= split_state(tree
, state
, prealloc
, end
+ 1);
549 BUG_ON(err
== -EEXIST
);
553 set
|= clear_state_bit(tree
, prealloc
, bits
,
559 set
|= clear_state_bit(tree
, state
, bits
, wake
, delete);
560 if (last_end
== (u64
)-1)
562 start
= last_end
+ 1;
566 spin_unlock(&tree
->lock
);
568 free_extent_state(prealloc
);
575 spin_unlock(&tree
->lock
);
576 if (mask
& __GFP_WAIT
)
581 static int wait_on_state(struct extent_io_tree
*tree
,
582 struct extent_state
*state
)
583 __releases(tree
->lock
)
584 __acquires(tree
->lock
)
587 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
588 spin_unlock(&tree
->lock
);
590 spin_lock(&tree
->lock
);
591 finish_wait(&state
->wq
, &wait
);
596 * waits for one or more bits to clear on a range in the state tree.
597 * The range [start, end] is inclusive.
598 * The tree lock is taken by this function
600 int wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
602 struct extent_state
*state
;
603 struct rb_node
*node
;
605 spin_lock(&tree
->lock
);
609 * this search will find all the extents that end after
612 node
= tree_search(tree
, start
);
616 state
= rb_entry(node
, struct extent_state
, rb_node
);
618 if (state
->start
> end
)
621 if (state
->state
& bits
) {
622 start
= state
->start
;
623 atomic_inc(&state
->refs
);
624 wait_on_state(tree
, state
);
625 free_extent_state(state
);
628 start
= state
->end
+ 1;
633 if (need_resched()) {
634 spin_unlock(&tree
->lock
);
636 spin_lock(&tree
->lock
);
640 spin_unlock(&tree
->lock
);
644 static void set_state_bits(struct extent_io_tree
*tree
,
645 struct extent_state
*state
,
648 if ((bits
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
649 u64 range
= state
->end
- state
->start
+ 1;
650 tree
->dirty_bytes
+= range
;
652 set_state_cb(tree
, state
, bits
);
653 state
->state
|= bits
;
657 * set some bits on a range in the tree. This may require allocations
658 * or sleeping, so the gfp mask is used to indicate what is allowed.
660 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
661 * range already has the desired bits set. The start of the existing
662 * range is returned in failed_start in this case.
664 * [start, end] is inclusive
665 * This takes the tree lock.
667 static int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
668 int bits
, int exclusive
, u64
*failed_start
,
671 struct extent_state
*state
;
672 struct extent_state
*prealloc
= NULL
;
673 struct rb_node
*node
;
679 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
680 prealloc
= alloc_extent_state(mask
);
685 spin_lock(&tree
->lock
);
687 * this search will find all the extents that end after
690 node
= tree_search(tree
, start
);
692 err
= insert_state(tree
, prealloc
, start
, end
, bits
);
694 BUG_ON(err
== -EEXIST
);
698 state
= rb_entry(node
, struct extent_state
, rb_node
);
699 last_start
= state
->start
;
700 last_end
= state
->end
;
703 * | ---- desired range ---- |
706 * Just lock what we found and keep going
708 if (state
->start
== start
&& state
->end
<= end
) {
709 set
= state
->state
& bits
;
710 if (set
&& exclusive
) {
711 *failed_start
= state
->start
;
715 set_state_bits(tree
, state
, bits
);
716 merge_state(tree
, state
);
717 if (last_end
== (u64
)-1)
719 start
= last_end
+ 1;
724 * | ---- desired range ---- |
727 * | ------------- state -------------- |
729 * We need to split the extent we found, and may flip bits on
732 * If the extent we found extends past our
733 * range, we just split and search again. It'll get split
734 * again the next time though.
736 * If the extent we found is inside our range, we set the
739 if (state
->start
< start
) {
740 set
= state
->state
& bits
;
741 if (exclusive
&& set
) {
742 *failed_start
= start
;
746 err
= split_state(tree
, state
, prealloc
, start
);
747 BUG_ON(err
== -EEXIST
);
751 if (state
->end
<= end
) {
752 set_state_bits(tree
, state
, bits
);
753 merge_state(tree
, state
);
754 if (last_end
== (u64
)-1)
756 start
= last_end
+ 1;
758 start
= state
->start
;
763 * | ---- desired range ---- |
764 * | state | or | state |
766 * There's a hole, we need to insert something in it and
767 * ignore the extent we found.
769 if (state
->start
> start
) {
771 if (end
< last_start
)
774 this_end
= last_start
- 1;
775 err
= insert_state(tree
, prealloc
, start
, this_end
,
778 BUG_ON(err
== -EEXIST
);
781 start
= this_end
+ 1;
785 * | ---- desired range ---- |
787 * We need to split the extent, and set the bit
790 if (state
->start
<= end
&& state
->end
> end
) {
791 set
= state
->state
& bits
;
792 if (exclusive
&& set
) {
793 *failed_start
= start
;
797 err
= split_state(tree
, state
, prealloc
, end
+ 1);
798 BUG_ON(err
== -EEXIST
);
800 set_state_bits(tree
, prealloc
, bits
);
801 merge_state(tree
, prealloc
);
809 spin_unlock(&tree
->lock
);
811 free_extent_state(prealloc
);
818 spin_unlock(&tree
->lock
);
819 if (mask
& __GFP_WAIT
)
824 /* wrappers around set/clear extent bit */
825 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
828 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, 0, NULL
,
832 int set_extent_ordered(struct extent_io_tree
*tree
, u64 start
, u64 end
,
835 return set_extent_bit(tree
, start
, end
, EXTENT_ORDERED
, 0, NULL
, mask
);
838 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
839 int bits
, gfp_t mask
)
841 return set_extent_bit(tree
, start
, end
, bits
, 0, NULL
,
845 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
846 int bits
, gfp_t mask
)
848 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, mask
);
851 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
854 return set_extent_bit(tree
, start
, end
,
855 EXTENT_DELALLOC
| EXTENT_DIRTY
,
859 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
862 return clear_extent_bit(tree
, start
, end
,
863 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0, mask
);
866 int clear_extent_ordered(struct extent_io_tree
*tree
, u64 start
, u64 end
,
869 return clear_extent_bit(tree
, start
, end
, EXTENT_ORDERED
, 1, 0, mask
);
872 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
875 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, NULL
,
879 static int clear_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
882 return clear_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, 0, mask
);
885 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
888 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, NULL
,
892 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
895 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0, mask
);
898 static int set_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
,
901 return set_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
,
905 static int clear_extent_writeback(struct extent_io_tree
*tree
, u64 start
,
908 return clear_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
, 1, 0, mask
);
911 int wait_on_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
913 return wait_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
);
917 * either insert or lock state struct between start and end use mask to tell
918 * us if waiting is desired.
920 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
925 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1,
926 &failed_start
, mask
);
927 if (err
== -EEXIST
&& (mask
& __GFP_WAIT
)) {
928 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
929 start
= failed_start
;
933 WARN_ON(start
> end
);
938 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
944 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1,
945 &failed_start
, mask
);
946 if (err
== -EEXIST
) {
947 if (failed_start
> start
)
948 clear_extent_bit(tree
, start
, failed_start
- 1,
949 EXTENT_LOCKED
, 1, 0, mask
);
955 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
958 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, mask
);
962 * helper function to set pages and extents in the tree dirty
964 int set_range_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
)
966 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
967 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
970 while (index
<= end_index
) {
971 page
= find_get_page(tree
->mapping
, index
);
973 __set_page_dirty_nobuffers(page
);
974 page_cache_release(page
);
977 set_extent_dirty(tree
, start
, end
, GFP_NOFS
);
982 * helper function to set both pages and extents in the tree writeback
984 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
986 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
987 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
990 while (index
<= end_index
) {
991 page
= find_get_page(tree
->mapping
, index
);
993 set_page_writeback(page
);
994 page_cache_release(page
);
997 set_extent_writeback(tree
, start
, end
, GFP_NOFS
);
1002 * find the first offset in the io tree with 'bits' set. zero is
1003 * returned if we find something, and *start_ret and *end_ret are
1004 * set to reflect the state struct that was found.
1006 * If nothing was found, 1 is returned, < 0 on error
1008 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1009 u64
*start_ret
, u64
*end_ret
, int bits
)
1011 struct rb_node
*node
;
1012 struct extent_state
*state
;
1015 spin_lock(&tree
->lock
);
1017 * this search will find all the extents that end after
1020 node
= tree_search(tree
, start
);
1025 state
= rb_entry(node
, struct extent_state
, rb_node
);
1026 if (state
->end
>= start
&& (state
->state
& bits
)) {
1027 *start_ret
= state
->start
;
1028 *end_ret
= state
->end
;
1032 node
= rb_next(node
);
1037 spin_unlock(&tree
->lock
);
1041 /* find the first state struct with 'bits' set after 'start', and
1042 * return it. tree->lock must be held. NULL will returned if
1043 * nothing was found after 'start'
1045 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1046 u64 start
, int bits
)
1048 struct rb_node
*node
;
1049 struct extent_state
*state
;
1052 * this search will find all the extents that end after
1055 node
= tree_search(tree
, start
);
1060 state
= rb_entry(node
, struct extent_state
, rb_node
);
1061 if (state
->end
>= start
&& (state
->state
& bits
))
1064 node
= rb_next(node
);
1073 * find a contiguous range of bytes in the file marked as delalloc, not
1074 * more than 'max_bytes'. start and end are used to return the range,
1076 * 1 is returned if we find something, 0 if nothing was in the tree
1078 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1079 u64
*start
, u64
*end
, u64 max_bytes
)
1081 struct rb_node
*node
;
1082 struct extent_state
*state
;
1083 u64 cur_start
= *start
;
1085 u64 total_bytes
= 0;
1087 spin_lock(&tree
->lock
);
1090 * this search will find all the extents that end after
1093 node
= tree_search(tree
, cur_start
);
1101 state
= rb_entry(node
, struct extent_state
, rb_node
);
1102 if (found
&& (state
->start
!= cur_start
||
1103 (state
->state
& EXTENT_BOUNDARY
))) {
1106 if (!(state
->state
& EXTENT_DELALLOC
)) {
1112 *start
= state
->start
;
1115 cur_start
= state
->end
+ 1;
1116 node
= rb_next(node
);
1119 total_bytes
+= state
->end
- state
->start
+ 1;
1120 if (total_bytes
>= max_bytes
)
1124 spin_unlock(&tree
->lock
);
1128 static noinline
int __unlock_for_delalloc(struct inode
*inode
,
1129 struct page
*locked_page
,
1133 struct page
*pages
[16];
1134 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1135 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1136 unsigned long nr_pages
= end_index
- index
+ 1;
1139 if (index
== locked_page
->index
&& end_index
== index
)
1142 while (nr_pages
> 0) {
1143 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1144 min_t(unsigned long, nr_pages
,
1145 ARRAY_SIZE(pages
)), pages
);
1146 for (i
= 0; i
< ret
; i
++) {
1147 if (pages
[i
] != locked_page
)
1148 unlock_page(pages
[i
]);
1149 page_cache_release(pages
[i
]);
1158 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1159 struct page
*locked_page
,
1163 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1164 unsigned long start_index
= index
;
1165 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1166 unsigned long pages_locked
= 0;
1167 struct page
*pages
[16];
1168 unsigned long nrpages
;
1172 /* the caller is responsible for locking the start index */
1173 if (index
== locked_page
->index
&& index
== end_index
)
1176 /* skip the page at the start index */
1177 nrpages
= end_index
- index
+ 1;
1178 while (nrpages
> 0) {
1179 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1180 min_t(unsigned long,
1181 nrpages
, ARRAY_SIZE(pages
)), pages
);
1186 /* now we have an array of pages, lock them all */
1187 for (i
= 0; i
< ret
; i
++) {
1189 * the caller is taking responsibility for
1192 if (pages
[i
] != locked_page
) {
1193 lock_page(pages
[i
]);
1194 if (!PageDirty(pages
[i
]) ||
1195 pages
[i
]->mapping
!= inode
->i_mapping
) {
1197 unlock_page(pages
[i
]);
1198 page_cache_release(pages
[i
]);
1202 page_cache_release(pages
[i
]);
1211 if (ret
&& pages_locked
) {
1212 __unlock_for_delalloc(inode
, locked_page
,
1214 ((u64
)(start_index
+ pages_locked
- 1)) <<
1221 * find a contiguous range of bytes in the file marked as delalloc, not
1222 * more than 'max_bytes'. start and end are used to return the range,
1224 * 1 is returned if we find something, 0 if nothing was in the tree
1226 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1227 struct extent_io_tree
*tree
,
1228 struct page
*locked_page
,
1229 u64
*start
, u64
*end
,
1239 /* step one, find a bunch of delalloc bytes starting at start */
1240 delalloc_start
= *start
;
1242 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1244 if (!found
|| delalloc_end
<= *start
) {
1245 *start
= delalloc_start
;
1246 *end
= delalloc_end
;
1251 * start comes from the offset of locked_page. We have to lock
1252 * pages in order, so we can't process delalloc bytes before
1255 if (delalloc_start
< *start
)
1256 delalloc_start
= *start
;
1259 * make sure to limit the number of pages we try to lock down
1262 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1263 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1265 /* step two, lock all the pages after the page that has start */
1266 ret
= lock_delalloc_pages(inode
, locked_page
,
1267 delalloc_start
, delalloc_end
);
1268 if (ret
== -EAGAIN
) {
1269 /* some of the pages are gone, lets avoid looping by
1270 * shortening the size of the delalloc range we're searching
1273 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1274 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1284 /* step three, lock the state bits for the whole range */
1285 lock_extent(tree
, delalloc_start
, delalloc_end
, GFP_NOFS
);
1287 /* then test to make sure it is all still delalloc */
1288 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1289 EXTENT_DELALLOC
, 1);
1291 unlock_extent(tree
, delalloc_start
, delalloc_end
, GFP_NOFS
);
1292 __unlock_for_delalloc(inode
, locked_page
,
1293 delalloc_start
, delalloc_end
);
1297 *start
= delalloc_start
;
1298 *end
= delalloc_end
;
1303 int extent_clear_unlock_delalloc(struct inode
*inode
,
1304 struct extent_io_tree
*tree
,
1305 u64 start
, u64 end
, struct page
*locked_page
,
1308 int clear_delalloc
, int clear_dirty
,
1313 struct page
*pages
[16];
1314 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1315 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1316 unsigned long nr_pages
= end_index
- index
+ 1;
1321 clear_bits
|= EXTENT_LOCKED
;
1323 clear_bits
|= EXTENT_DIRTY
;
1326 clear_bits
|= EXTENT_DELALLOC
;
1328 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, GFP_NOFS
);
1329 if (!(unlock_pages
|| clear_dirty
|| set_writeback
|| end_writeback
))
1332 while (nr_pages
> 0) {
1333 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1334 min_t(unsigned long,
1335 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1336 for (i
= 0; i
< ret
; i
++) {
1337 if (pages
[i
] == locked_page
) {
1338 page_cache_release(pages
[i
]);
1342 clear_page_dirty_for_io(pages
[i
]);
1344 set_page_writeback(pages
[i
]);
1346 end_page_writeback(pages
[i
]);
1348 unlock_page(pages
[i
]);
1349 page_cache_release(pages
[i
]);
1359 * count the number of bytes in the tree that have a given bit(s)
1360 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1361 * cached. The total number found is returned.
1363 u64
count_range_bits(struct extent_io_tree
*tree
,
1364 u64
*start
, u64 search_end
, u64 max_bytes
,
1367 struct rb_node
*node
;
1368 struct extent_state
*state
;
1369 u64 cur_start
= *start
;
1370 u64 total_bytes
= 0;
1373 if (search_end
<= cur_start
) {
1378 spin_lock(&tree
->lock
);
1379 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1380 total_bytes
= tree
->dirty_bytes
;
1384 * this search will find all the extents that end after
1387 node
= tree_search(tree
, cur_start
);
1392 state
= rb_entry(node
, struct extent_state
, rb_node
);
1393 if (state
->start
> search_end
)
1395 if (state
->end
>= cur_start
&& (state
->state
& bits
)) {
1396 total_bytes
+= min(search_end
, state
->end
) + 1 -
1397 max(cur_start
, state
->start
);
1398 if (total_bytes
>= max_bytes
)
1401 *start
= state
->start
;
1405 node
= rb_next(node
);
1410 spin_unlock(&tree
->lock
);
1415 * set the private field for a given byte offset in the tree. If there isn't
1416 * an extent_state there already, this does nothing.
1418 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1420 struct rb_node
*node
;
1421 struct extent_state
*state
;
1424 spin_lock(&tree
->lock
);
1426 * this search will find all the extents that end after
1429 node
= tree_search(tree
, start
);
1434 state
= rb_entry(node
, struct extent_state
, rb_node
);
1435 if (state
->start
!= start
) {
1439 state
->private = private;
1441 spin_unlock(&tree
->lock
);
1445 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1447 struct rb_node
*node
;
1448 struct extent_state
*state
;
1451 spin_lock(&tree
->lock
);
1453 * this search will find all the extents that end after
1456 node
= tree_search(tree
, start
);
1461 state
= rb_entry(node
, struct extent_state
, rb_node
);
1462 if (state
->start
!= start
) {
1466 *private = state
->private;
1468 spin_unlock(&tree
->lock
);
1473 * searches a range in the state tree for a given mask.
1474 * If 'filled' == 1, this returns 1 only if every extent in the tree
1475 * has the bits set. Otherwise, 1 is returned if any bit in the
1476 * range is found set.
1478 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1479 int bits
, int filled
)
1481 struct extent_state
*state
= NULL
;
1482 struct rb_node
*node
;
1485 spin_lock(&tree
->lock
);
1486 node
= tree_search(tree
, start
);
1487 while (node
&& start
<= end
) {
1488 state
= rb_entry(node
, struct extent_state
, rb_node
);
1490 if (filled
&& state
->start
> start
) {
1495 if (state
->start
> end
)
1498 if (state
->state
& bits
) {
1502 } else if (filled
) {
1506 start
= state
->end
+ 1;
1509 node
= rb_next(node
);
1516 spin_unlock(&tree
->lock
);
1521 * helper function to set a given page up to date if all the
1522 * extents in the tree for that page are up to date
1524 static int check_page_uptodate(struct extent_io_tree
*tree
,
1527 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1528 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1529 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1))
1530 SetPageUptodate(page
);
1535 * helper function to unlock a page if all the extents in the tree
1536 * for that page are unlocked
1538 static int check_page_locked(struct extent_io_tree
*tree
,
1541 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1542 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1543 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0))
1549 * helper function to end page writeback if all the extents
1550 * in the tree for that page are done with writeback
1552 static int check_page_writeback(struct extent_io_tree
*tree
,
1555 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1556 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1557 if (!test_range_bit(tree
, start
, end
, EXTENT_WRITEBACK
, 0))
1558 end_page_writeback(page
);
1562 /* lots and lots of room for performance fixes in the end_bio funcs */
1565 * after a writepage IO is done, we need to:
1566 * clear the uptodate bits on error
1567 * clear the writeback bits in the extent tree for this IO
1568 * end_page_writeback if the page has no more pending IO
1570 * Scheduling is not allowed, so the extent state tree is expected
1571 * to have one and only one object corresponding to this IO.
1573 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
1575 int uptodate
= err
== 0;
1576 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1577 struct extent_io_tree
*tree
;
1584 struct page
*page
= bvec
->bv_page
;
1585 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1587 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1589 end
= start
+ bvec
->bv_len
- 1;
1591 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1596 if (--bvec
>= bio
->bi_io_vec
)
1597 prefetchw(&bvec
->bv_page
->flags
);
1598 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
1599 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
1600 end
, NULL
, uptodate
);
1605 if (!uptodate
&& tree
->ops
&&
1606 tree
->ops
->writepage_io_failed_hook
) {
1607 ret
= tree
->ops
->writepage_io_failed_hook(bio
, page
,
1610 uptodate
= (err
== 0);
1616 clear_extent_uptodate(tree
, start
, end
, GFP_ATOMIC
);
1617 ClearPageUptodate(page
);
1621 clear_extent_writeback(tree
, start
, end
, GFP_ATOMIC
);
1624 end_page_writeback(page
);
1626 check_page_writeback(tree
, page
);
1627 } while (bvec
>= bio
->bi_io_vec
);
1633 * after a readpage IO is done, we need to:
1634 * clear the uptodate bits on error
1635 * set the uptodate bits if things worked
1636 * set the page up to date if all extents in the tree are uptodate
1637 * clear the lock bit in the extent tree
1638 * unlock the page if there are no other extents locked for it
1640 * Scheduling is not allowed, so the extent state tree is expected
1641 * to have one and only one object corresponding to this IO.
1643 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
1645 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1646 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1647 struct extent_io_tree
*tree
;
1657 struct page
*page
= bvec
->bv_page
;
1658 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1660 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1662 end
= start
+ bvec
->bv_len
- 1;
1664 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1669 if (--bvec
>= bio
->bi_io_vec
)
1670 prefetchw(&bvec
->bv_page
->flags
);
1672 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
1673 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
1678 if (!uptodate
&& tree
->ops
&&
1679 tree
->ops
->readpage_io_failed_hook
) {
1680 ret
= tree
->ops
->readpage_io_failed_hook(bio
, page
,
1684 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1692 set_extent_uptodate(tree
, start
, end
,
1695 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1699 SetPageUptodate(page
);
1701 ClearPageUptodate(page
);
1707 check_page_uptodate(tree
, page
);
1709 ClearPageUptodate(page
);
1712 check_page_locked(tree
, page
);
1714 } while (bvec
>= bio
->bi_io_vec
);
1720 * IO done from prepare_write is pretty simple, we just unlock
1721 * the structs in the extent tree when done, and set the uptodate bits
1724 static void end_bio_extent_preparewrite(struct bio
*bio
, int err
)
1726 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1727 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1728 struct extent_io_tree
*tree
;
1733 struct page
*page
= bvec
->bv_page
;
1734 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1736 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1738 end
= start
+ bvec
->bv_len
- 1;
1740 if (--bvec
>= bio
->bi_io_vec
)
1741 prefetchw(&bvec
->bv_page
->flags
);
1744 set_extent_uptodate(tree
, start
, end
, GFP_ATOMIC
);
1746 ClearPageUptodate(page
);
1750 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1752 } while (bvec
>= bio
->bi_io_vec
);
1758 extent_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
1763 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1765 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
1766 while (!bio
&& (nr_vecs
/= 2))
1767 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1772 bio
->bi_bdev
= bdev
;
1773 bio
->bi_sector
= first_sector
;
1778 static int submit_one_bio(int rw
, struct bio
*bio
, int mirror_num
,
1779 unsigned long bio_flags
)
1782 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1783 struct page
*page
= bvec
->bv_page
;
1784 struct extent_io_tree
*tree
= bio
->bi_private
;
1788 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
1789 end
= start
+ bvec
->bv_len
- 1;
1791 bio
->bi_private
= NULL
;
1795 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
1796 tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
1797 mirror_num
, bio_flags
);
1799 submit_bio(rw
, bio
);
1800 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
1806 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
1807 struct page
*page
, sector_t sector
,
1808 size_t size
, unsigned long offset
,
1809 struct block_device
*bdev
,
1810 struct bio
**bio_ret
,
1811 unsigned long max_pages
,
1812 bio_end_io_t end_io_func
,
1814 unsigned long prev_bio_flags
,
1815 unsigned long bio_flags
)
1821 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
1822 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
1823 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
1825 if (bio_ret
&& *bio_ret
) {
1828 contig
= bio
->bi_sector
== sector
;
1830 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
1833 if (prev_bio_flags
!= bio_flags
|| !contig
||
1834 (tree
->ops
&& tree
->ops
->merge_bio_hook
&&
1835 tree
->ops
->merge_bio_hook(page
, offset
, page_size
, bio
,
1837 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
1838 ret
= submit_one_bio(rw
, bio
, mirror_num
,
1845 if (this_compressed
)
1848 nr
= bio_get_nr_vecs(bdev
);
1850 bio
= extent_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
1852 bio_add_page(bio
, page
, page_size
, offset
);
1853 bio
->bi_end_io
= end_io_func
;
1854 bio
->bi_private
= tree
;
1859 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
1864 void set_page_extent_mapped(struct page
*page
)
1866 if (!PagePrivate(page
)) {
1867 SetPagePrivate(page
);
1868 page_cache_get(page
);
1869 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
1873 static void set_page_extent_head(struct page
*page
, unsigned long len
)
1875 set_page_private(page
, EXTENT_PAGE_PRIVATE_FIRST_PAGE
| len
<< 2);
1879 * basic readpage implementation. Locked extent state structs are inserted
1880 * into the tree that are removed when the IO is done (by the end_io
1883 static int __extent_read_full_page(struct extent_io_tree
*tree
,
1885 get_extent_t
*get_extent
,
1886 struct bio
**bio
, int mirror_num
,
1887 unsigned long *bio_flags
)
1889 struct inode
*inode
= page
->mapping
->host
;
1890 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1891 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
1895 u64 last_byte
= i_size_read(inode
);
1899 struct extent_map
*em
;
1900 struct block_device
*bdev
;
1903 size_t page_offset
= 0;
1905 size_t disk_io_size
;
1906 size_t blocksize
= inode
->i_sb
->s_blocksize
;
1907 unsigned long this_bio_flag
= 0;
1909 set_page_extent_mapped(page
);
1912 lock_extent(tree
, start
, end
, GFP_NOFS
);
1914 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
1916 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
1919 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
1920 userpage
= kmap_atomic(page
, KM_USER0
);
1921 memset(userpage
+ zero_offset
, 0, iosize
);
1922 flush_dcache_page(page
);
1923 kunmap_atomic(userpage
, KM_USER0
);
1926 while (cur
<= end
) {
1927 if (cur
>= last_byte
) {
1929 iosize
= PAGE_CACHE_SIZE
- page_offset
;
1930 userpage
= kmap_atomic(page
, KM_USER0
);
1931 memset(userpage
+ page_offset
, 0, iosize
);
1932 flush_dcache_page(page
);
1933 kunmap_atomic(userpage
, KM_USER0
);
1934 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
1936 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
1939 em
= get_extent(inode
, page
, page_offset
, cur
,
1941 if (IS_ERR(em
) || !em
) {
1943 unlock_extent(tree
, cur
, end
, GFP_NOFS
);
1946 extent_offset
= cur
- em
->start
;
1947 BUG_ON(extent_map_end(em
) <= cur
);
1950 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
1951 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
1953 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
1954 cur_end
= min(extent_map_end(em
) - 1, end
);
1955 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
1956 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
1957 disk_io_size
= em
->block_len
;
1958 sector
= em
->block_start
>> 9;
1960 sector
= (em
->block_start
+ extent_offset
) >> 9;
1961 disk_io_size
= iosize
;
1964 block_start
= em
->block_start
;
1965 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
1966 block_start
= EXTENT_MAP_HOLE
;
1967 free_extent_map(em
);
1970 /* we've found a hole, just zero and go on */
1971 if (block_start
== EXTENT_MAP_HOLE
) {
1973 userpage
= kmap_atomic(page
, KM_USER0
);
1974 memset(userpage
+ page_offset
, 0, iosize
);
1975 flush_dcache_page(page
);
1976 kunmap_atomic(userpage
, KM_USER0
);
1978 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
1980 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
1982 page_offset
+= iosize
;
1985 /* the get_extent function already copied into the page */
1986 if (test_range_bit(tree
, cur
, cur_end
, EXTENT_UPTODATE
, 1)) {
1987 check_page_uptodate(tree
, page
);
1988 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
1990 page_offset
+= iosize
;
1993 /* we have an inline extent but it didn't get marked up
1994 * to date. Error out
1996 if (block_start
== EXTENT_MAP_INLINE
) {
1998 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2000 page_offset
+= iosize
;
2005 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2006 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2010 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2012 ret
= submit_extent_page(READ
, tree
, page
,
2013 sector
, disk_io_size
, page_offset
,
2015 end_bio_extent_readpage
, mirror_num
,
2019 *bio_flags
= this_bio_flag
;
2024 page_offset
+= iosize
;
2027 if (!PageError(page
))
2028 SetPageUptodate(page
);
2034 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2035 get_extent_t
*get_extent
)
2037 struct bio
*bio
= NULL
;
2038 unsigned long bio_flags
= 0;
2041 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, 0,
2044 submit_one_bio(READ
, bio
, 0, bio_flags
);
2048 static noinline
void update_nr_written(struct page
*page
,
2049 struct writeback_control
*wbc
,
2050 unsigned long nr_written
)
2052 wbc
->nr_to_write
-= nr_written
;
2053 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2054 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2055 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2059 * the writepage semantics are similar to regular writepage. extent
2060 * records are inserted to lock ranges in the tree, and as dirty areas
2061 * are found, they are marked writeback. Then the lock bits are removed
2062 * and the end_io handler clears the writeback ranges
2064 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2067 struct inode
*inode
= page
->mapping
->host
;
2068 struct extent_page_data
*epd
= data
;
2069 struct extent_io_tree
*tree
= epd
->tree
;
2070 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2072 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2076 u64 last_byte
= i_size_read(inode
);
2081 struct extent_map
*em
;
2082 struct block_device
*bdev
;
2085 size_t pg_offset
= 0;
2087 loff_t i_size
= i_size_read(inode
);
2088 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2094 unsigned long nr_written
= 0;
2096 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2097 write_flags
= WRITE_SYNC_PLUG
;
2099 write_flags
= WRITE
;
2101 WARN_ON(!PageLocked(page
));
2102 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2103 if (page
->index
> end_index
||
2104 (page
->index
== end_index
&& !pg_offset
)) {
2105 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2110 if (page
->index
== end_index
) {
2113 userpage
= kmap_atomic(page
, KM_USER0
);
2114 memset(userpage
+ pg_offset
, 0,
2115 PAGE_CACHE_SIZE
- pg_offset
);
2116 kunmap_atomic(userpage
, KM_USER0
);
2117 flush_dcache_page(page
);
2121 set_page_extent_mapped(page
);
2123 delalloc_start
= start
;
2126 if (!epd
->extent_locked
) {
2128 * make sure the wbc mapping index is at least updated
2131 update_nr_written(page
, wbc
, 0);
2133 while (delalloc_end
< page_end
) {
2134 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2139 if (nr_delalloc
== 0) {
2140 delalloc_start
= delalloc_end
+ 1;
2143 tree
->ops
->fill_delalloc(inode
, page
, delalloc_start
,
2144 delalloc_end
, &page_started
,
2146 delalloc_start
= delalloc_end
+ 1;
2149 /* did the fill delalloc function already unlock and start
2155 * we've unlocked the page, so we can't update
2156 * the mapping's writeback index, just update
2159 wbc
->nr_to_write
-= nr_written
;
2163 lock_extent(tree
, start
, page_end
, GFP_NOFS
);
2165 unlock_start
= start
;
2167 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2168 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2170 if (ret
== -EAGAIN
) {
2171 unlock_extent(tree
, start
, page_end
, GFP_NOFS
);
2172 redirty_page_for_writepage(wbc
, page
);
2173 update_nr_written(page
, wbc
, nr_written
);
2181 * we don't want to touch the inode after unlocking the page,
2182 * so we update the mapping writeback index now
2184 update_nr_written(page
, wbc
, nr_written
+ 1);
2187 if (test_range_bit(tree
, start
, page_end
, EXTENT_DELALLOC
, 0))
2188 printk(KERN_ERR
"btrfs delalloc bits after lock_extent\n");
2190 if (last_byte
<= start
) {
2191 clear_extent_dirty(tree
, start
, page_end
, GFP_NOFS
);
2192 unlock_extent(tree
, start
, page_end
, GFP_NOFS
);
2193 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2194 tree
->ops
->writepage_end_io_hook(page
, start
,
2196 unlock_start
= page_end
+ 1;
2200 set_extent_uptodate(tree
, start
, page_end
, GFP_NOFS
);
2201 blocksize
= inode
->i_sb
->s_blocksize
;
2203 while (cur
<= end
) {
2204 if (cur
>= last_byte
) {
2205 clear_extent_dirty(tree
, cur
, page_end
, GFP_NOFS
);
2206 unlock_extent(tree
, unlock_start
, page_end
, GFP_NOFS
);
2207 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2208 tree
->ops
->writepage_end_io_hook(page
, cur
,
2210 unlock_start
= page_end
+ 1;
2213 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2215 if (IS_ERR(em
) || !em
) {
2220 extent_offset
= cur
- em
->start
;
2221 BUG_ON(extent_map_end(em
) <= cur
);
2223 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2224 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2225 sector
= (em
->block_start
+ extent_offset
) >> 9;
2227 block_start
= em
->block_start
;
2228 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2229 free_extent_map(em
);
2233 * compressed and inline extents are written through other
2236 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2237 block_start
== EXTENT_MAP_INLINE
) {
2238 clear_extent_dirty(tree
, cur
,
2239 cur
+ iosize
- 1, GFP_NOFS
);
2241 unlock_extent(tree
, unlock_start
, cur
+ iosize
- 1,
2245 * end_io notification does not happen here for
2246 * compressed extents
2248 if (!compressed
&& tree
->ops
&&
2249 tree
->ops
->writepage_end_io_hook
)
2250 tree
->ops
->writepage_end_io_hook(page
, cur
,
2253 else if (compressed
) {
2254 /* we don't want to end_page_writeback on
2255 * a compressed extent. this happens
2262 pg_offset
+= iosize
;
2266 /* leave this out until we have a page_mkwrite call */
2267 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2270 pg_offset
+= iosize
;
2274 clear_extent_dirty(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2275 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2276 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
2284 unsigned long max_nr
= end_index
+ 1;
2286 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
2287 if (!PageWriteback(page
)) {
2288 printk(KERN_ERR
"btrfs warning page %lu not "
2289 "writeback, cur %llu end %llu\n",
2290 page
->index
, (unsigned long long)cur
,
2291 (unsigned long long)end
);
2294 ret
= submit_extent_page(write_flags
, tree
, page
,
2295 sector
, iosize
, pg_offset
,
2296 bdev
, &epd
->bio
, max_nr
,
2297 end_bio_extent_writepage
,
2303 pg_offset
+= iosize
;
2308 /* make sure the mapping tag for page dirty gets cleared */
2309 set_page_writeback(page
);
2310 end_page_writeback(page
);
2312 if (unlock_start
<= page_end
)
2313 unlock_extent(tree
, unlock_start
, page_end
, GFP_NOFS
);
2322 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2323 * @mapping: address space structure to write
2324 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2325 * @writepage: function called for each page
2326 * @data: data passed to writepage function
2328 * If a page is already under I/O, write_cache_pages() skips it, even
2329 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2330 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2331 * and msync() need to guarantee that all the data which was dirty at the time
2332 * the call was made get new I/O started against them. If wbc->sync_mode is
2333 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2334 * existing IO to complete.
2336 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
2337 struct address_space
*mapping
,
2338 struct writeback_control
*wbc
,
2339 writepage_t writepage
, void *data
,
2340 void (*flush_fn
)(void *))
2342 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
2345 struct pagevec pvec
;
2348 pgoff_t end
; /* Inclusive */
2350 int range_whole
= 0;
2352 pagevec_init(&pvec
, 0);
2353 if (wbc
->range_cyclic
) {
2354 index
= mapping
->writeback_index
; /* Start from prev offset */
2357 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2358 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2359 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2364 while (!done
&& (index
<= end
) &&
2365 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
2366 PAGECACHE_TAG_DIRTY
, min(end
- index
,
2367 (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
2371 for (i
= 0; i
< nr_pages
; i
++) {
2372 struct page
*page
= pvec
.pages
[i
];
2375 * At this point we hold neither mapping->tree_lock nor
2376 * lock on the page itself: the page may be truncated or
2377 * invalidated (changing page->mapping to NULL), or even
2378 * swizzled back from swapper_space to tmpfs file
2381 if (tree
->ops
&& tree
->ops
->write_cache_pages_lock_hook
)
2382 tree
->ops
->write_cache_pages_lock_hook(page
);
2386 if (unlikely(page
->mapping
!= mapping
)) {
2391 if (!wbc
->range_cyclic
&& page
->index
> end
) {
2397 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
2398 if (PageWriteback(page
))
2400 wait_on_page_writeback(page
);
2403 if (PageWriteback(page
) ||
2404 !clear_page_dirty_for_io(page
)) {
2409 ret
= (*writepage
)(page
, wbc
, data
);
2411 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
2415 if (ret
|| wbc
->nr_to_write
<= 0)
2417 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
2418 wbc
->encountered_congestion
= 1;
2422 pagevec_release(&pvec
);
2425 if (!scanned
&& !done
) {
2427 * We hit the last page and there is more work to be done: wrap
2428 * back to the start of the file
2437 static void flush_epd_write_bio(struct extent_page_data
*epd
)
2441 submit_one_bio(WRITE_SYNC
, epd
->bio
, 0, 0);
2443 submit_one_bio(WRITE
, epd
->bio
, 0, 0);
2448 static noinline
void flush_write_bio(void *data
)
2450 struct extent_page_data
*epd
= data
;
2451 flush_epd_write_bio(epd
);
2454 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2455 get_extent_t
*get_extent
,
2456 struct writeback_control
*wbc
)
2459 struct address_space
*mapping
= page
->mapping
;
2460 struct extent_page_data epd
= {
2463 .get_extent
= get_extent
,
2465 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2467 struct writeback_control wbc_writepages
= {
2469 .sync_mode
= wbc
->sync_mode
,
2470 .older_than_this
= NULL
,
2472 .range_start
= page_offset(page
) + PAGE_CACHE_SIZE
,
2473 .range_end
= (loff_t
)-1,
2476 ret
= __extent_writepage(page
, wbc
, &epd
);
2478 extent_write_cache_pages(tree
, mapping
, &wbc_writepages
,
2479 __extent_writepage
, &epd
, flush_write_bio
);
2480 flush_epd_write_bio(&epd
);
2484 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
2485 u64 start
, u64 end
, get_extent_t
*get_extent
,
2489 struct address_space
*mapping
= inode
->i_mapping
;
2491 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
2494 struct extent_page_data epd
= {
2497 .get_extent
= get_extent
,
2499 .sync_io
= mode
== WB_SYNC_ALL
,
2501 struct writeback_control wbc_writepages
= {
2502 .bdi
= inode
->i_mapping
->backing_dev_info
,
2504 .older_than_this
= NULL
,
2505 .nr_to_write
= nr_pages
* 2,
2506 .range_start
= start
,
2507 .range_end
= end
+ 1,
2510 while (start
<= end
) {
2511 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
2512 if (clear_page_dirty_for_io(page
))
2513 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
2515 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2516 tree
->ops
->writepage_end_io_hook(page
, start
,
2517 start
+ PAGE_CACHE_SIZE
- 1,
2521 page_cache_release(page
);
2522 start
+= PAGE_CACHE_SIZE
;
2525 flush_epd_write_bio(&epd
);
2529 int extent_writepages(struct extent_io_tree
*tree
,
2530 struct address_space
*mapping
,
2531 get_extent_t
*get_extent
,
2532 struct writeback_control
*wbc
)
2535 struct extent_page_data epd
= {
2538 .get_extent
= get_extent
,
2540 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2543 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
2544 __extent_writepage
, &epd
,
2546 flush_epd_write_bio(&epd
);
2550 int extent_readpages(struct extent_io_tree
*tree
,
2551 struct address_space
*mapping
,
2552 struct list_head
*pages
, unsigned nr_pages
,
2553 get_extent_t get_extent
)
2555 struct bio
*bio
= NULL
;
2557 struct pagevec pvec
;
2558 unsigned long bio_flags
= 0;
2560 pagevec_init(&pvec
, 0);
2561 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
2562 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
2564 prefetchw(&page
->flags
);
2565 list_del(&page
->lru
);
2567 * what we want to do here is call add_to_page_cache_lru,
2568 * but that isn't exported, so we reproduce it here
2570 if (!add_to_page_cache(page
, mapping
,
2571 page
->index
, GFP_KERNEL
)) {
2573 /* open coding of lru_cache_add, also not exported */
2574 page_cache_get(page
);
2575 if (!pagevec_add(&pvec
, page
))
2576 __pagevec_lru_add_file(&pvec
);
2577 __extent_read_full_page(tree
, page
, get_extent
,
2578 &bio
, 0, &bio_flags
);
2580 page_cache_release(page
);
2582 if (pagevec_count(&pvec
))
2583 __pagevec_lru_add_file(&pvec
);
2584 BUG_ON(!list_empty(pages
));
2586 submit_one_bio(READ
, bio
, 0, bio_flags
);
2591 * basic invalidatepage code, this waits on any locked or writeback
2592 * ranges corresponding to the page, and then deletes any extent state
2593 * records from the tree
2595 int extent_invalidatepage(struct extent_io_tree
*tree
,
2596 struct page
*page
, unsigned long offset
)
2598 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
2599 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2600 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
2602 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
2606 lock_extent(tree
, start
, end
, GFP_NOFS
);
2607 wait_on_extent_writeback(tree
, start
, end
);
2608 clear_extent_bit(tree
, start
, end
,
2609 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
,
2615 * simple commit_write call, set_range_dirty is used to mark both
2616 * the pages and the extent records as dirty
2618 int extent_commit_write(struct extent_io_tree
*tree
,
2619 struct inode
*inode
, struct page
*page
,
2620 unsigned from
, unsigned to
)
2622 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
2624 set_page_extent_mapped(page
);
2625 set_page_dirty(page
);
2627 if (pos
> inode
->i_size
) {
2628 i_size_write(inode
, pos
);
2629 mark_inode_dirty(inode
);
2634 int extent_prepare_write(struct extent_io_tree
*tree
,
2635 struct inode
*inode
, struct page
*page
,
2636 unsigned from
, unsigned to
, get_extent_t
*get_extent
)
2638 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2639 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2641 u64 orig_block_start
;
2644 struct extent_map
*em
;
2645 unsigned blocksize
= 1 << inode
->i_blkbits
;
2646 size_t page_offset
= 0;
2647 size_t block_off_start
;
2648 size_t block_off_end
;
2654 set_page_extent_mapped(page
);
2656 block_start
= (page_start
+ from
) & ~((u64
)blocksize
- 1);
2657 block_end
= (page_start
+ to
- 1) | (blocksize
- 1);
2658 orig_block_start
= block_start
;
2660 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2661 while (block_start
<= block_end
) {
2662 em
= get_extent(inode
, page
, page_offset
, block_start
,
2663 block_end
- block_start
+ 1, 1);
2664 if (IS_ERR(em
) || !em
)
2667 cur_end
= min(block_end
, extent_map_end(em
) - 1);
2668 block_off_start
= block_start
& (PAGE_CACHE_SIZE
- 1);
2669 block_off_end
= block_off_start
+ blocksize
;
2670 isnew
= clear_extent_new(tree
, block_start
, cur_end
, GFP_NOFS
);
2672 if (!PageUptodate(page
) && isnew
&&
2673 (block_off_end
> to
|| block_off_start
< from
)) {
2676 kaddr
= kmap_atomic(page
, KM_USER0
);
2677 if (block_off_end
> to
)
2678 memset(kaddr
+ to
, 0, block_off_end
- to
);
2679 if (block_off_start
< from
)
2680 memset(kaddr
+ block_off_start
, 0,
2681 from
- block_off_start
);
2682 flush_dcache_page(page
);
2683 kunmap_atomic(kaddr
, KM_USER0
);
2685 if ((em
->block_start
!= EXTENT_MAP_HOLE
&&
2686 em
->block_start
!= EXTENT_MAP_INLINE
) &&
2687 !isnew
&& !PageUptodate(page
) &&
2688 (block_off_end
> to
|| block_off_start
< from
) &&
2689 !test_range_bit(tree
, block_start
, cur_end
,
2690 EXTENT_UPTODATE
, 1)) {
2692 u64 extent_offset
= block_start
- em
->start
;
2694 sector
= (em
->block_start
+ extent_offset
) >> 9;
2695 iosize
= (cur_end
- block_start
+ blocksize
) &
2696 ~((u64
)blocksize
- 1);
2698 * we've already got the extent locked, but we
2699 * need to split the state such that our end_bio
2700 * handler can clear the lock.
2702 set_extent_bit(tree
, block_start
,
2703 block_start
+ iosize
- 1,
2704 EXTENT_LOCKED
, 0, NULL
, GFP_NOFS
);
2705 ret
= submit_extent_page(READ
, tree
, page
,
2706 sector
, iosize
, page_offset
, em
->bdev
,
2708 end_bio_extent_preparewrite
, 0,
2711 block_start
= block_start
+ iosize
;
2713 set_extent_uptodate(tree
, block_start
, cur_end
,
2715 unlock_extent(tree
, block_start
, cur_end
, GFP_NOFS
);
2716 block_start
= cur_end
+ 1;
2718 page_offset
= block_start
& (PAGE_CACHE_SIZE
- 1);
2719 free_extent_map(em
);
2722 wait_extent_bit(tree
, orig_block_start
,
2723 block_end
, EXTENT_LOCKED
);
2725 check_page_uptodate(tree
, page
);
2727 /* FIXME, zero out newly allocated blocks on error */
2732 * a helper for releasepage, this tests for areas of the page that
2733 * are locked or under IO and drops the related state bits if it is safe
2736 int try_release_extent_state(struct extent_map_tree
*map
,
2737 struct extent_io_tree
*tree
, struct page
*page
,
2740 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2741 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2744 if (test_range_bit(tree
, start
, end
,
2745 EXTENT_IOBITS
| EXTENT_ORDERED
, 0))
2748 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
2750 clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
,
2757 * a helper for releasepage. As long as there are no locked extents
2758 * in the range corresponding to the page, both state records and extent
2759 * map records are removed
2761 int try_release_extent_mapping(struct extent_map_tree
*map
,
2762 struct extent_io_tree
*tree
, struct page
*page
,
2765 struct extent_map
*em
;
2766 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2767 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2769 if ((mask
& __GFP_WAIT
) &&
2770 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
2772 while (start
<= end
) {
2773 len
= end
- start
+ 1;
2774 spin_lock(&map
->lock
);
2775 em
= lookup_extent_mapping(map
, start
, len
);
2776 if (!em
|| IS_ERR(em
)) {
2777 spin_unlock(&map
->lock
);
2780 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
2781 em
->start
!= start
) {
2782 spin_unlock(&map
->lock
);
2783 free_extent_map(em
);
2786 if (!test_range_bit(tree
, em
->start
,
2787 extent_map_end(em
) - 1,
2788 EXTENT_LOCKED
| EXTENT_WRITEBACK
|
2791 remove_extent_mapping(map
, em
);
2792 /* once for the rb tree */
2793 free_extent_map(em
);
2795 start
= extent_map_end(em
);
2796 spin_unlock(&map
->lock
);
2799 free_extent_map(em
);
2802 return try_release_extent_state(map
, tree
, page
, mask
);
2805 sector_t
extent_bmap(struct address_space
*mapping
, sector_t iblock
,
2806 get_extent_t
*get_extent
)
2808 struct inode
*inode
= mapping
->host
;
2809 u64 start
= iblock
<< inode
->i_blkbits
;
2810 sector_t sector
= 0;
2811 size_t blksize
= (1 << inode
->i_blkbits
);
2812 struct extent_map
*em
;
2814 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2816 em
= get_extent(inode
, NULL
, 0, start
, blksize
, 0);
2817 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2819 if (!em
|| IS_ERR(em
))
2822 if (em
->block_start
> EXTENT_MAP_LAST_BYTE
)
2825 sector
= (em
->block_start
+ start
- em
->start
) >> inode
->i_blkbits
;
2827 free_extent_map(em
);
2831 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
2832 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
2836 u64 max
= start
+ len
;
2839 struct extent_map
*em
= NULL
;
2841 u64 em_start
= 0, em_len
= 0;
2842 unsigned long emflags
;
2848 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
2850 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
2858 off
= em
->start
+ em
->len
;
2862 em_start
= em
->start
;
2868 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
2870 flags
|= FIEMAP_EXTENT_LAST
;
2871 } else if (em
->block_start
== EXTENT_MAP_HOLE
) {
2872 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
2873 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
2874 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
2875 FIEMAP_EXTENT_NOT_ALIGNED
);
2876 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
2877 flags
|= (FIEMAP_EXTENT_DELALLOC
|
2878 FIEMAP_EXTENT_UNKNOWN
);
2880 disko
= em
->block_start
;
2882 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
2883 flags
|= FIEMAP_EXTENT_ENCODED
;
2885 emflags
= em
->flags
;
2886 free_extent_map(em
);
2890 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
2897 emflags
= em
->flags
;
2899 if (test_bit(EXTENT_FLAG_VACANCY
, &emflags
)) {
2900 flags
|= FIEMAP_EXTENT_LAST
;
2904 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
2910 free_extent_map(em
);
2912 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
2917 static inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
2921 struct address_space
*mapping
;
2924 return eb
->first_page
;
2925 i
+= eb
->start
>> PAGE_CACHE_SHIFT
;
2926 mapping
= eb
->first_page
->mapping
;
2931 * extent_buffer_page is only called after pinning the page
2932 * by increasing the reference count. So we know the page must
2933 * be in the radix tree.
2936 p
= radix_tree_lookup(&mapping
->page_tree
, i
);
2942 static inline unsigned long num_extent_pages(u64 start
, u64 len
)
2944 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
2945 (start
>> PAGE_CACHE_SHIFT
);
2948 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
2953 struct extent_buffer
*eb
= NULL
;
2955 unsigned long flags
;
2958 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
2961 spin_lock_init(&eb
->lock
);
2962 init_waitqueue_head(&eb
->lock_wq
);
2965 spin_lock_irqsave(&leak_lock
, flags
);
2966 list_add(&eb
->leak_list
, &buffers
);
2967 spin_unlock_irqrestore(&leak_lock
, flags
);
2969 atomic_set(&eb
->refs
, 1);
2974 static void __free_extent_buffer(struct extent_buffer
*eb
)
2977 unsigned long flags
;
2978 spin_lock_irqsave(&leak_lock
, flags
);
2979 list_del(&eb
->leak_list
);
2980 spin_unlock_irqrestore(&leak_lock
, flags
);
2982 kmem_cache_free(extent_buffer_cache
, eb
);
2985 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
2986 u64 start
, unsigned long len
,
2990 unsigned long num_pages
= num_extent_pages(start
, len
);
2992 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
2993 struct extent_buffer
*eb
;
2994 struct extent_buffer
*exists
= NULL
;
2996 struct address_space
*mapping
= tree
->mapping
;
2999 spin_lock(&tree
->buffer_lock
);
3000 eb
= buffer_search(tree
, start
);
3002 atomic_inc(&eb
->refs
);
3003 spin_unlock(&tree
->buffer_lock
);
3004 mark_page_accessed(eb
->first_page
);
3007 spin_unlock(&tree
->buffer_lock
);
3009 eb
= __alloc_extent_buffer(tree
, start
, len
, mask
);
3014 eb
->first_page
= page0
;
3017 page_cache_get(page0
);
3018 mark_page_accessed(page0
);
3019 set_page_extent_mapped(page0
);
3020 set_page_extent_head(page0
, len
);
3021 uptodate
= PageUptodate(page0
);
3025 for (; i
< num_pages
; i
++, index
++) {
3026 p
= find_or_create_page(mapping
, index
, mask
| __GFP_HIGHMEM
);
3031 set_page_extent_mapped(p
);
3032 mark_page_accessed(p
);
3035 set_page_extent_head(p
, len
);
3037 set_page_private(p
, EXTENT_PAGE_PRIVATE
);
3039 if (!PageUptodate(p
))
3044 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3046 spin_lock(&tree
->buffer_lock
);
3047 exists
= buffer_tree_insert(tree
, start
, &eb
->rb_node
);
3049 /* add one reference for the caller */
3050 atomic_inc(&exists
->refs
);
3051 spin_unlock(&tree
->buffer_lock
);
3054 spin_unlock(&tree
->buffer_lock
);
3056 /* add one reference for the tree */
3057 atomic_inc(&eb
->refs
);
3061 if (!atomic_dec_and_test(&eb
->refs
))
3063 for (index
= 1; index
< i
; index
++)
3064 page_cache_release(extent_buffer_page(eb
, index
));
3065 page_cache_release(extent_buffer_page(eb
, 0));
3066 __free_extent_buffer(eb
);
3070 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
3071 u64 start
, unsigned long len
,
3074 struct extent_buffer
*eb
;
3076 spin_lock(&tree
->buffer_lock
);
3077 eb
= buffer_search(tree
, start
);
3079 atomic_inc(&eb
->refs
);
3080 spin_unlock(&tree
->buffer_lock
);
3083 mark_page_accessed(eb
->first_page
);
3088 void free_extent_buffer(struct extent_buffer
*eb
)
3093 if (!atomic_dec_and_test(&eb
->refs
))
3099 int clear_extent_buffer_dirty(struct extent_io_tree
*tree
,
3100 struct extent_buffer
*eb
)
3103 unsigned long num_pages
;
3106 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3108 for (i
= 0; i
< num_pages
; i
++) {
3109 page
= extent_buffer_page(eb
, i
);
3110 if (!PageDirty(page
))
3115 set_page_extent_head(page
, eb
->len
);
3117 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
3119 clear_page_dirty_for_io(page
);
3120 spin_lock_irq(&page
->mapping
->tree_lock
);
3121 if (!PageDirty(page
)) {
3122 radix_tree_tag_clear(&page
->mapping
->page_tree
,
3124 PAGECACHE_TAG_DIRTY
);
3126 spin_unlock_irq(&page
->mapping
->tree_lock
);
3132 int wait_on_extent_buffer_writeback(struct extent_io_tree
*tree
,
3133 struct extent_buffer
*eb
)
3135 return wait_on_extent_writeback(tree
, eb
->start
,
3136 eb
->start
+ eb
->len
- 1);
3139 int set_extent_buffer_dirty(struct extent_io_tree
*tree
,
3140 struct extent_buffer
*eb
)
3143 unsigned long num_pages
;
3146 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
3147 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3148 for (i
= 0; i
< num_pages
; i
++)
3149 __set_page_dirty_nobuffers(extent_buffer_page(eb
, i
));
3153 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3154 struct extent_buffer
*eb
)
3158 unsigned long num_pages
;
3160 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3161 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3163 clear_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3165 for (i
= 0; i
< num_pages
; i
++) {
3166 page
= extent_buffer_page(eb
, i
);
3168 ClearPageUptodate(page
);
3173 int set_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3174 struct extent_buffer
*eb
)
3178 unsigned long num_pages
;
3180 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3182 set_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3184 for (i
= 0; i
< num_pages
; i
++) {
3185 page
= extent_buffer_page(eb
, i
);
3186 if ((i
== 0 && (eb
->start
& (PAGE_CACHE_SIZE
- 1))) ||
3187 ((i
== num_pages
- 1) &&
3188 ((eb
->start
+ eb
->len
) & (PAGE_CACHE_SIZE
- 1)))) {
3189 check_page_uptodate(tree
, page
);
3192 SetPageUptodate(page
);
3197 int extent_range_uptodate(struct extent_io_tree
*tree
,
3202 int pg_uptodate
= 1;
3204 unsigned long index
;
3206 ret
= test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1);
3209 while (start
<= end
) {
3210 index
= start
>> PAGE_CACHE_SHIFT
;
3211 page
= find_get_page(tree
->mapping
, index
);
3212 uptodate
= PageUptodate(page
);
3213 page_cache_release(page
);
3218 start
+= PAGE_CACHE_SIZE
;
3223 int extent_buffer_uptodate(struct extent_io_tree
*tree
,
3224 struct extent_buffer
*eb
)
3227 unsigned long num_pages
;
3230 int pg_uptodate
= 1;
3232 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3235 ret
= test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3236 EXTENT_UPTODATE
, 1);
3240 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3241 for (i
= 0; i
< num_pages
; i
++) {
3242 page
= extent_buffer_page(eb
, i
);
3243 if (!PageUptodate(page
)) {
3251 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
3252 struct extent_buffer
*eb
,
3253 u64 start
, int wait
,
3254 get_extent_t
*get_extent
, int mirror_num
)
3257 unsigned long start_i
;
3261 int locked_pages
= 0;
3262 int all_uptodate
= 1;
3263 int inc_all_pages
= 0;
3264 unsigned long num_pages
;
3265 struct bio
*bio
= NULL
;
3266 unsigned long bio_flags
= 0;
3268 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3271 if (test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3272 EXTENT_UPTODATE
, 1)) {
3277 WARN_ON(start
< eb
->start
);
3278 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
3279 (eb
->start
>> PAGE_CACHE_SHIFT
);
3284 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3285 for (i
= start_i
; i
< num_pages
; i
++) {
3286 page
= extent_buffer_page(eb
, i
);
3288 if (!trylock_page(page
))
3294 if (!PageUptodate(page
))
3299 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3303 for (i
= start_i
; i
< num_pages
; i
++) {
3304 page
= extent_buffer_page(eb
, i
);
3306 page_cache_get(page
);
3307 if (!PageUptodate(page
)) {
3310 ClearPageError(page
);
3311 err
= __extent_read_full_page(tree
, page
,
3313 mirror_num
, &bio_flags
);
3322 submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
3327 for (i
= start_i
; i
< num_pages
; i
++) {
3328 page
= extent_buffer_page(eb
, i
);
3329 wait_on_page_locked(page
);
3330 if (!PageUptodate(page
))
3335 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3340 while (locked_pages
> 0) {
3341 page
= extent_buffer_page(eb
, i
);
3349 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
3350 unsigned long start
,
3357 char *dst
= (char *)dstv
;
3358 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3359 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3361 WARN_ON(start
> eb
->len
);
3362 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3364 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3367 page
= extent_buffer_page(eb
, i
);
3369 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3370 kaddr
= kmap_atomic(page
, KM_USER1
);
3371 memcpy(dst
, kaddr
+ offset
, cur
);
3372 kunmap_atomic(kaddr
, KM_USER1
);
3381 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3382 unsigned long min_len
, char **token
, char **map
,
3383 unsigned long *map_start
,
3384 unsigned long *map_len
, int km
)
3386 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
3389 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3390 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3391 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
3398 offset
= start_offset
;
3402 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
3405 if (start
+ min_len
> eb
->len
) {
3406 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
3407 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
3408 eb
->len
, start
, min_len
);
3412 p
= extent_buffer_page(eb
, i
);
3413 kaddr
= kmap_atomic(p
, km
);
3415 *map
= kaddr
+ offset
;
3416 *map_len
= PAGE_CACHE_SIZE
- offset
;
3420 int map_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3421 unsigned long min_len
,
3422 char **token
, char **map
,
3423 unsigned long *map_start
,
3424 unsigned long *map_len
, int km
)
3428 if (eb
->map_token
) {
3429 unmap_extent_buffer(eb
, eb
->map_token
, km
);
3430 eb
->map_token
= NULL
;
3433 err
= map_private_extent_buffer(eb
, start
, min_len
, token
, map
,
3434 map_start
, map_len
, km
);
3436 eb
->map_token
= *token
;
3438 eb
->map_start
= *map_start
;
3439 eb
->map_len
= *map_len
;
3444 void unmap_extent_buffer(struct extent_buffer
*eb
, char *token
, int km
)
3446 kunmap_atomic(token
, km
);
3449 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
3450 unsigned long start
,
3457 char *ptr
= (char *)ptrv
;
3458 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3459 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3462 WARN_ON(start
> eb
->len
);
3463 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3465 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3468 page
= extent_buffer_page(eb
, i
);
3470 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3472 kaddr
= kmap_atomic(page
, KM_USER0
);
3473 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
3474 kunmap_atomic(kaddr
, KM_USER0
);
3486 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
3487 unsigned long start
, unsigned long len
)
3493 char *src
= (char *)srcv
;
3494 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3495 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3497 WARN_ON(start
> eb
->len
);
3498 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3500 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3503 page
= extent_buffer_page(eb
, i
);
3504 WARN_ON(!PageUptodate(page
));
3506 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3507 kaddr
= kmap_atomic(page
, KM_USER1
);
3508 memcpy(kaddr
+ offset
, src
, cur
);
3509 kunmap_atomic(kaddr
, KM_USER1
);
3518 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
3519 unsigned long start
, unsigned long len
)
3525 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3526 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3528 WARN_ON(start
> eb
->len
);
3529 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3531 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3534 page
= extent_buffer_page(eb
, i
);
3535 WARN_ON(!PageUptodate(page
));
3537 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3538 kaddr
= kmap_atomic(page
, KM_USER0
);
3539 memset(kaddr
+ offset
, c
, cur
);
3540 kunmap_atomic(kaddr
, KM_USER0
);
3548 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
3549 unsigned long dst_offset
, unsigned long src_offset
,
3552 u64 dst_len
= dst
->len
;
3557 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3558 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3560 WARN_ON(src
->len
!= dst_len
);
3562 offset
= (start_offset
+ dst_offset
) &
3563 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3566 page
= extent_buffer_page(dst
, i
);
3567 WARN_ON(!PageUptodate(page
));
3569 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
3571 kaddr
= kmap_atomic(page
, KM_USER0
);
3572 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
3573 kunmap_atomic(kaddr
, KM_USER0
);
3582 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
3583 unsigned long dst_off
, unsigned long src_off
,
3586 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3587 if (dst_page
== src_page
) {
3588 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
3590 char *src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3591 char *p
= dst_kaddr
+ dst_off
+ len
;
3592 char *s
= src_kaddr
+ src_off
+ len
;
3597 kunmap_atomic(src_kaddr
, KM_USER1
);
3599 kunmap_atomic(dst_kaddr
, KM_USER0
);
3602 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
3603 unsigned long dst_off
, unsigned long src_off
,
3606 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3609 if (dst_page
!= src_page
)
3610 src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3612 src_kaddr
= dst_kaddr
;
3614 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
3615 kunmap_atomic(dst_kaddr
, KM_USER0
);
3616 if (dst_page
!= src_page
)
3617 kunmap_atomic(src_kaddr
, KM_USER1
);
3620 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3621 unsigned long src_offset
, unsigned long len
)
3624 size_t dst_off_in_page
;
3625 size_t src_off_in_page
;
3626 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3627 unsigned long dst_i
;
3628 unsigned long src_i
;
3630 if (src_offset
+ len
> dst
->len
) {
3631 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3632 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
3635 if (dst_offset
+ len
> dst
->len
) {
3636 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3637 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
3642 dst_off_in_page
= (start_offset
+ dst_offset
) &
3643 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3644 src_off_in_page
= (start_offset
+ src_offset
) &
3645 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3647 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3648 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
3650 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
3652 cur
= min_t(unsigned long, cur
,
3653 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
3655 copy_pages(extent_buffer_page(dst
, dst_i
),
3656 extent_buffer_page(dst
, src_i
),
3657 dst_off_in_page
, src_off_in_page
, cur
);
3665 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3666 unsigned long src_offset
, unsigned long len
)
3669 size_t dst_off_in_page
;
3670 size_t src_off_in_page
;
3671 unsigned long dst_end
= dst_offset
+ len
- 1;
3672 unsigned long src_end
= src_offset
+ len
- 1;
3673 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3674 unsigned long dst_i
;
3675 unsigned long src_i
;
3677 if (src_offset
+ len
> dst
->len
) {
3678 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3679 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
3682 if (dst_offset
+ len
> dst
->len
) {
3683 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3684 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
3687 if (dst_offset
< src_offset
) {
3688 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
3692 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
3693 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
3695 dst_off_in_page
= (start_offset
+ dst_end
) &
3696 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3697 src_off_in_page
= (start_offset
+ src_end
) &
3698 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3700 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
3701 cur
= min(cur
, dst_off_in_page
+ 1);
3702 move_pages(extent_buffer_page(dst
, dst_i
),
3703 extent_buffer_page(dst
, src_i
),
3704 dst_off_in_page
- cur
+ 1,
3705 src_off_in_page
- cur
+ 1, cur
);
3713 int try_release_extent_buffer(struct extent_io_tree
*tree
, struct page
*page
)
3715 u64 start
= page_offset(page
);
3716 struct extent_buffer
*eb
;
3719 unsigned long num_pages
;
3721 spin_lock(&tree
->buffer_lock
);
3722 eb
= buffer_search(tree
, start
);
3726 if (atomic_read(&eb
->refs
) > 1) {
3730 if (test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3734 /* at this point we can safely release the extent buffer */
3735 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3736 for (i
= 0; i
< num_pages
; i
++)
3737 page_cache_release(extent_buffer_page(eb
, i
));
3738 rb_erase(&eb
->rb_node
, &tree
->buffer
);
3739 __free_extent_buffer(eb
);
3741 spin_unlock(&tree
->buffer_lock
);