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
;
483 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
484 prealloc
= alloc_extent_state(mask
);
489 spin_lock(&tree
->lock
);
491 * this search will find the extents that end after
494 node
= tree_search(tree
, start
);
497 state
= rb_entry(node
, struct extent_state
, rb_node
);
498 if (state
->start
> end
)
500 WARN_ON(state
->end
< start
);
503 * | ---- desired range ---- |
505 * | ------------- state -------------- |
507 * We need to split the extent we found, and may flip
508 * bits on second half.
510 * If the extent we found extends past our range, we
511 * just split and search again. It'll get split again
512 * the next time though.
514 * If the extent we found is inside our range, we clear
515 * the desired bit on it.
518 if (state
->start
< start
) {
520 prealloc
= alloc_extent_state(GFP_ATOMIC
);
521 err
= split_state(tree
, state
, prealloc
, start
);
522 BUG_ON(err
== -EEXIST
);
526 if (state
->end
<= end
) {
527 start
= state
->end
+ 1;
528 set
|= clear_state_bit(tree
, state
, bits
,
531 start
= state
->start
;
536 * | ---- desired range ---- |
538 * We need to split the extent, and clear the bit
541 if (state
->start
<= end
&& state
->end
> end
) {
543 prealloc
= alloc_extent_state(GFP_ATOMIC
);
544 err
= split_state(tree
, state
, prealloc
, end
+ 1);
545 BUG_ON(err
== -EEXIST
);
549 set
|= clear_state_bit(tree
, prealloc
, bits
,
555 start
= state
->end
+ 1;
556 set
|= clear_state_bit(tree
, state
, bits
, wake
, delete);
560 spin_unlock(&tree
->lock
);
562 free_extent_state(prealloc
);
569 spin_unlock(&tree
->lock
);
570 if (mask
& __GFP_WAIT
)
575 static int wait_on_state(struct extent_io_tree
*tree
,
576 struct extent_state
*state
)
577 __releases(tree
->lock
)
578 __acquires(tree
->lock
)
581 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
582 spin_unlock(&tree
->lock
);
584 spin_lock(&tree
->lock
);
585 finish_wait(&state
->wq
, &wait
);
590 * waits for one or more bits to clear on a range in the state tree.
591 * The range [start, end] is inclusive.
592 * The tree lock is taken by this function
594 int wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
596 struct extent_state
*state
;
597 struct rb_node
*node
;
599 spin_lock(&tree
->lock
);
603 * this search will find all the extents that end after
606 node
= tree_search(tree
, start
);
610 state
= rb_entry(node
, struct extent_state
, rb_node
);
612 if (state
->start
> end
)
615 if (state
->state
& bits
) {
616 start
= state
->start
;
617 atomic_inc(&state
->refs
);
618 wait_on_state(tree
, state
);
619 free_extent_state(state
);
622 start
= state
->end
+ 1;
627 if (need_resched()) {
628 spin_unlock(&tree
->lock
);
630 spin_lock(&tree
->lock
);
634 spin_unlock(&tree
->lock
);
638 static void set_state_bits(struct extent_io_tree
*tree
,
639 struct extent_state
*state
,
642 if ((bits
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
643 u64 range
= state
->end
- state
->start
+ 1;
644 tree
->dirty_bytes
+= range
;
646 set_state_cb(tree
, state
, bits
);
647 state
->state
|= bits
;
651 * set some bits on a range in the tree. This may require allocations
652 * or sleeping, so the gfp mask is used to indicate what is allowed.
654 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
655 * range already has the desired bits set. The start of the existing
656 * range is returned in failed_start in this case.
658 * [start, end] is inclusive
659 * This takes the tree lock.
661 static int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
662 int bits
, int exclusive
, u64
*failed_start
,
665 struct extent_state
*state
;
666 struct extent_state
*prealloc
= NULL
;
667 struct rb_node
*node
;
673 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
674 prealloc
= alloc_extent_state(mask
);
679 spin_lock(&tree
->lock
);
681 * this search will find all the extents that end after
684 node
= tree_search(tree
, start
);
686 err
= insert_state(tree
, prealloc
, start
, end
, bits
);
688 BUG_ON(err
== -EEXIST
);
692 state
= rb_entry(node
, struct extent_state
, rb_node
);
693 last_start
= state
->start
;
694 last_end
= state
->end
;
697 * | ---- desired range ---- |
700 * Just lock what we found and keep going
702 if (state
->start
== start
&& state
->end
<= end
) {
703 set
= state
->state
& bits
;
704 if (set
&& exclusive
) {
705 *failed_start
= state
->start
;
709 set_state_bits(tree
, state
, bits
);
710 start
= state
->end
+ 1;
711 merge_state(tree
, state
);
716 * | ---- desired range ---- |
719 * | ------------- state -------------- |
721 * We need to split the extent we found, and may flip bits on
724 * If the extent we found extends past our
725 * range, we just split and search again. It'll get split
726 * again the next time though.
728 * If the extent we found is inside our range, we set the
731 if (state
->start
< start
) {
732 set
= state
->state
& bits
;
733 if (exclusive
&& set
) {
734 *failed_start
= start
;
738 err
= split_state(tree
, state
, prealloc
, start
);
739 BUG_ON(err
== -EEXIST
);
743 if (state
->end
<= end
) {
744 set_state_bits(tree
, state
, bits
);
745 start
= state
->end
+ 1;
746 merge_state(tree
, state
);
748 start
= state
->start
;
753 * | ---- desired range ---- |
754 * | state | or | state |
756 * There's a hole, we need to insert something in it and
757 * ignore the extent we found.
759 if (state
->start
> start
) {
761 if (end
< last_start
)
764 this_end
= last_start
- 1;
765 err
= insert_state(tree
, prealloc
, start
, this_end
,
768 BUG_ON(err
== -EEXIST
);
771 start
= this_end
+ 1;
775 * | ---- desired range ---- |
777 * We need to split the extent, and set the bit
780 if (state
->start
<= end
&& state
->end
> end
) {
781 set
= state
->state
& bits
;
782 if (exclusive
&& set
) {
783 *failed_start
= start
;
787 err
= split_state(tree
, state
, prealloc
, end
+ 1);
788 BUG_ON(err
== -EEXIST
);
790 set_state_bits(tree
, prealloc
, bits
);
791 merge_state(tree
, prealloc
);
799 spin_unlock(&tree
->lock
);
801 free_extent_state(prealloc
);
808 spin_unlock(&tree
->lock
);
809 if (mask
& __GFP_WAIT
)
814 /* wrappers around set/clear extent bit */
815 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
818 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, 0, NULL
,
822 int set_extent_ordered(struct extent_io_tree
*tree
, u64 start
, u64 end
,
825 return set_extent_bit(tree
, start
, end
, EXTENT_ORDERED
, 0, NULL
, mask
);
828 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
829 int bits
, gfp_t mask
)
831 return set_extent_bit(tree
, start
, end
, bits
, 0, NULL
,
835 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
836 int bits
, gfp_t mask
)
838 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, mask
);
841 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
844 return set_extent_bit(tree
, start
, end
,
845 EXTENT_DELALLOC
| EXTENT_DIRTY
,
849 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
852 return clear_extent_bit(tree
, start
, end
,
853 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0, mask
);
856 int clear_extent_ordered(struct extent_io_tree
*tree
, u64 start
, u64 end
,
859 return clear_extent_bit(tree
, start
, end
, EXTENT_ORDERED
, 1, 0, mask
);
862 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
865 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, NULL
,
869 static int clear_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
872 return clear_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, 0, mask
);
875 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
878 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, NULL
,
882 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
885 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0, mask
);
888 static int set_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
,
891 return set_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
,
895 static int clear_extent_writeback(struct extent_io_tree
*tree
, u64 start
,
898 return clear_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
, 1, 0, mask
);
901 int wait_on_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
903 return wait_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
);
907 * either insert or lock state struct between start and end use mask to tell
908 * us if waiting is desired.
910 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
915 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1,
916 &failed_start
, mask
);
917 if (err
== -EEXIST
&& (mask
& __GFP_WAIT
)) {
918 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
919 start
= failed_start
;
923 WARN_ON(start
> end
);
928 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
934 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1,
935 &failed_start
, mask
);
936 if (err
== -EEXIST
) {
937 if (failed_start
> start
)
938 clear_extent_bit(tree
, start
, failed_start
- 1,
939 EXTENT_LOCKED
, 1, 0, mask
);
945 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
948 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, mask
);
952 * helper function to set pages and extents in the tree dirty
954 int set_range_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
)
956 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
957 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
960 while (index
<= end_index
) {
961 page
= find_get_page(tree
->mapping
, index
);
963 __set_page_dirty_nobuffers(page
);
964 page_cache_release(page
);
967 set_extent_dirty(tree
, start
, end
, GFP_NOFS
);
972 * helper function to set both pages and extents in the tree writeback
974 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
976 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
977 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
980 while (index
<= end_index
) {
981 page
= find_get_page(tree
->mapping
, index
);
983 set_page_writeback(page
);
984 page_cache_release(page
);
987 set_extent_writeback(tree
, start
, end
, GFP_NOFS
);
992 * find the first offset in the io tree with 'bits' set. zero is
993 * returned if we find something, and *start_ret and *end_ret are
994 * set to reflect the state struct that was found.
996 * If nothing was found, 1 is returned, < 0 on error
998 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
999 u64
*start_ret
, u64
*end_ret
, int bits
)
1001 struct rb_node
*node
;
1002 struct extent_state
*state
;
1005 spin_lock(&tree
->lock
);
1007 * this search will find all the extents that end after
1010 node
= tree_search(tree
, start
);
1015 state
= rb_entry(node
, struct extent_state
, rb_node
);
1016 if (state
->end
>= start
&& (state
->state
& bits
)) {
1017 *start_ret
= state
->start
;
1018 *end_ret
= state
->end
;
1022 node
= rb_next(node
);
1027 spin_unlock(&tree
->lock
);
1031 /* find the first state struct with 'bits' set after 'start', and
1032 * return it. tree->lock must be held. NULL will returned if
1033 * nothing was found after 'start'
1035 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1036 u64 start
, int bits
)
1038 struct rb_node
*node
;
1039 struct extent_state
*state
;
1042 * this search will find all the extents that end after
1045 node
= tree_search(tree
, start
);
1050 state
= rb_entry(node
, struct extent_state
, rb_node
);
1051 if (state
->end
>= start
&& (state
->state
& bits
))
1054 node
= rb_next(node
);
1063 * find a contiguous range of bytes in the file marked as delalloc, not
1064 * more than 'max_bytes'. start and end are used to return the range,
1066 * 1 is returned if we find something, 0 if nothing was in the tree
1068 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1069 u64
*start
, u64
*end
, u64 max_bytes
)
1071 struct rb_node
*node
;
1072 struct extent_state
*state
;
1073 u64 cur_start
= *start
;
1075 u64 total_bytes
= 0;
1077 spin_lock(&tree
->lock
);
1080 * this search will find all the extents that end after
1083 node
= tree_search(tree
, cur_start
);
1091 state
= rb_entry(node
, struct extent_state
, rb_node
);
1092 if (found
&& (state
->start
!= cur_start
||
1093 (state
->state
& EXTENT_BOUNDARY
))) {
1096 if (!(state
->state
& EXTENT_DELALLOC
)) {
1102 *start
= state
->start
;
1105 cur_start
= state
->end
+ 1;
1106 node
= rb_next(node
);
1109 total_bytes
+= state
->end
- state
->start
+ 1;
1110 if (total_bytes
>= max_bytes
)
1114 spin_unlock(&tree
->lock
);
1118 static noinline
int __unlock_for_delalloc(struct inode
*inode
,
1119 struct page
*locked_page
,
1123 struct page
*pages
[16];
1124 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1125 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1126 unsigned long nr_pages
= end_index
- index
+ 1;
1129 if (index
== locked_page
->index
&& end_index
== index
)
1132 while (nr_pages
> 0) {
1133 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1134 min_t(unsigned long, nr_pages
,
1135 ARRAY_SIZE(pages
)), pages
);
1136 for (i
= 0; i
< ret
; i
++) {
1137 if (pages
[i
] != locked_page
)
1138 unlock_page(pages
[i
]);
1139 page_cache_release(pages
[i
]);
1148 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1149 struct page
*locked_page
,
1153 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1154 unsigned long start_index
= index
;
1155 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1156 unsigned long pages_locked
= 0;
1157 struct page
*pages
[16];
1158 unsigned long nrpages
;
1162 /* the caller is responsible for locking the start index */
1163 if (index
== locked_page
->index
&& index
== end_index
)
1166 /* skip the page at the start index */
1167 nrpages
= end_index
- index
+ 1;
1168 while (nrpages
> 0) {
1169 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1170 min_t(unsigned long,
1171 nrpages
, ARRAY_SIZE(pages
)), pages
);
1176 /* now we have an array of pages, lock them all */
1177 for (i
= 0; i
< ret
; i
++) {
1179 * the caller is taking responsibility for
1182 if (pages
[i
] != locked_page
) {
1183 lock_page(pages
[i
]);
1184 if (!PageDirty(pages
[i
]) ||
1185 pages
[i
]->mapping
!= inode
->i_mapping
) {
1187 unlock_page(pages
[i
]);
1188 page_cache_release(pages
[i
]);
1192 page_cache_release(pages
[i
]);
1201 if (ret
&& pages_locked
) {
1202 __unlock_for_delalloc(inode
, locked_page
,
1204 ((u64
)(start_index
+ pages_locked
- 1)) <<
1211 * find a contiguous range of bytes in the file marked as delalloc, not
1212 * more than 'max_bytes'. start and end are used to return the range,
1214 * 1 is returned if we find something, 0 if nothing was in the tree
1216 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1217 struct extent_io_tree
*tree
,
1218 struct page
*locked_page
,
1219 u64
*start
, u64
*end
,
1229 /* step one, find a bunch of delalloc bytes starting at start */
1230 delalloc_start
= *start
;
1232 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1234 if (!found
|| delalloc_end
<= *start
) {
1235 *start
= delalloc_start
;
1236 *end
= delalloc_end
;
1241 * start comes from the offset of locked_page. We have to lock
1242 * pages in order, so we can't process delalloc bytes before
1245 if (delalloc_start
< *start
)
1246 delalloc_start
= *start
;
1249 * make sure to limit the number of pages we try to lock down
1252 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1253 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1255 /* step two, lock all the pages after the page that has start */
1256 ret
= lock_delalloc_pages(inode
, locked_page
,
1257 delalloc_start
, delalloc_end
);
1258 if (ret
== -EAGAIN
) {
1259 /* some of the pages are gone, lets avoid looping by
1260 * shortening the size of the delalloc range we're searching
1263 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1264 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1274 /* step three, lock the state bits for the whole range */
1275 lock_extent(tree
, delalloc_start
, delalloc_end
, GFP_NOFS
);
1277 /* then test to make sure it is all still delalloc */
1278 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1279 EXTENT_DELALLOC
, 1);
1281 unlock_extent(tree
, delalloc_start
, delalloc_end
, GFP_NOFS
);
1282 __unlock_for_delalloc(inode
, locked_page
,
1283 delalloc_start
, delalloc_end
);
1287 *start
= delalloc_start
;
1288 *end
= delalloc_end
;
1293 int extent_clear_unlock_delalloc(struct inode
*inode
,
1294 struct extent_io_tree
*tree
,
1295 u64 start
, u64 end
, struct page
*locked_page
,
1298 int clear_delalloc
, int clear_dirty
,
1303 struct page
*pages
[16];
1304 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1305 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1306 unsigned long nr_pages
= end_index
- index
+ 1;
1311 clear_bits
|= EXTENT_LOCKED
;
1313 clear_bits
|= EXTENT_DIRTY
;
1316 clear_bits
|= EXTENT_DELALLOC
;
1318 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, GFP_NOFS
);
1319 if (!(unlock_pages
|| clear_dirty
|| set_writeback
|| end_writeback
))
1322 while (nr_pages
> 0) {
1323 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1324 min_t(unsigned long,
1325 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1326 for (i
= 0; i
< ret
; i
++) {
1327 if (pages
[i
] == locked_page
) {
1328 page_cache_release(pages
[i
]);
1332 clear_page_dirty_for_io(pages
[i
]);
1334 set_page_writeback(pages
[i
]);
1336 end_page_writeback(pages
[i
]);
1338 unlock_page(pages
[i
]);
1339 page_cache_release(pages
[i
]);
1349 * count the number of bytes in the tree that have a given bit(s)
1350 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1351 * cached. The total number found is returned.
1353 u64
count_range_bits(struct extent_io_tree
*tree
,
1354 u64
*start
, u64 search_end
, u64 max_bytes
,
1357 struct rb_node
*node
;
1358 struct extent_state
*state
;
1359 u64 cur_start
= *start
;
1360 u64 total_bytes
= 0;
1363 if (search_end
<= cur_start
) {
1368 spin_lock(&tree
->lock
);
1369 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1370 total_bytes
= tree
->dirty_bytes
;
1374 * this search will find all the extents that end after
1377 node
= tree_search(tree
, cur_start
);
1382 state
= rb_entry(node
, struct extent_state
, rb_node
);
1383 if (state
->start
> search_end
)
1385 if (state
->end
>= cur_start
&& (state
->state
& bits
)) {
1386 total_bytes
+= min(search_end
, state
->end
) + 1 -
1387 max(cur_start
, state
->start
);
1388 if (total_bytes
>= max_bytes
)
1391 *start
= state
->start
;
1395 node
= rb_next(node
);
1400 spin_unlock(&tree
->lock
);
1405 * set the private field for a given byte offset in the tree. If there isn't
1406 * an extent_state there already, this does nothing.
1408 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1410 struct rb_node
*node
;
1411 struct extent_state
*state
;
1414 spin_lock(&tree
->lock
);
1416 * this search will find all the extents that end after
1419 node
= tree_search(tree
, start
);
1424 state
= rb_entry(node
, struct extent_state
, rb_node
);
1425 if (state
->start
!= start
) {
1429 state
->private = private;
1431 spin_unlock(&tree
->lock
);
1435 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1437 struct rb_node
*node
;
1438 struct extent_state
*state
;
1441 spin_lock(&tree
->lock
);
1443 * this search will find all the extents that end after
1446 node
= tree_search(tree
, start
);
1451 state
= rb_entry(node
, struct extent_state
, rb_node
);
1452 if (state
->start
!= start
) {
1456 *private = state
->private;
1458 spin_unlock(&tree
->lock
);
1463 * searches a range in the state tree for a given mask.
1464 * If 'filled' == 1, this returns 1 only if every extent in the tree
1465 * has the bits set. Otherwise, 1 is returned if any bit in the
1466 * range is found set.
1468 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1469 int bits
, int filled
)
1471 struct extent_state
*state
= NULL
;
1472 struct rb_node
*node
;
1475 spin_lock(&tree
->lock
);
1476 node
= tree_search(tree
, start
);
1477 while (node
&& start
<= end
) {
1478 state
= rb_entry(node
, struct extent_state
, rb_node
);
1480 if (filled
&& state
->start
> start
) {
1485 if (state
->start
> end
)
1488 if (state
->state
& bits
) {
1492 } else if (filled
) {
1496 start
= state
->end
+ 1;
1499 node
= rb_next(node
);
1506 spin_unlock(&tree
->lock
);
1511 * helper function to set a given page up to date if all the
1512 * extents in the tree for that page are up to date
1514 static int check_page_uptodate(struct extent_io_tree
*tree
,
1517 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1518 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1519 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1))
1520 SetPageUptodate(page
);
1525 * helper function to unlock a page if all the extents in the tree
1526 * for that page are unlocked
1528 static int check_page_locked(struct extent_io_tree
*tree
,
1531 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1532 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1533 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0))
1539 * helper function to end page writeback if all the extents
1540 * in the tree for that page are done with writeback
1542 static int check_page_writeback(struct extent_io_tree
*tree
,
1545 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1546 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1547 if (!test_range_bit(tree
, start
, end
, EXTENT_WRITEBACK
, 0))
1548 end_page_writeback(page
);
1552 /* lots and lots of room for performance fixes in the end_bio funcs */
1555 * after a writepage IO is done, we need to:
1556 * clear the uptodate bits on error
1557 * clear the writeback bits in the extent tree for this IO
1558 * end_page_writeback if the page has no more pending IO
1560 * Scheduling is not allowed, so the extent state tree is expected
1561 * to have one and only one object corresponding to this IO.
1563 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
1565 int uptodate
= err
== 0;
1566 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1567 struct extent_io_tree
*tree
;
1574 struct page
*page
= bvec
->bv_page
;
1575 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1577 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1579 end
= start
+ bvec
->bv_len
- 1;
1581 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1586 if (--bvec
>= bio
->bi_io_vec
)
1587 prefetchw(&bvec
->bv_page
->flags
);
1588 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
1589 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
1590 end
, NULL
, uptodate
);
1595 if (!uptodate
&& tree
->ops
&&
1596 tree
->ops
->writepage_io_failed_hook
) {
1597 ret
= tree
->ops
->writepage_io_failed_hook(bio
, page
,
1600 uptodate
= (err
== 0);
1606 clear_extent_uptodate(tree
, start
, end
, GFP_ATOMIC
);
1607 ClearPageUptodate(page
);
1611 clear_extent_writeback(tree
, start
, end
, GFP_ATOMIC
);
1614 end_page_writeback(page
);
1616 check_page_writeback(tree
, page
);
1617 } while (bvec
>= bio
->bi_io_vec
);
1623 * after a readpage IO is done, we need to:
1624 * clear the uptodate bits on error
1625 * set the uptodate bits if things worked
1626 * set the page up to date if all extents in the tree are uptodate
1627 * clear the lock bit in the extent tree
1628 * unlock the page if there are no other extents locked for it
1630 * Scheduling is not allowed, so the extent state tree is expected
1631 * to have one and only one object corresponding to this IO.
1633 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
1635 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1636 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1637 struct extent_io_tree
*tree
;
1647 struct page
*page
= bvec
->bv_page
;
1648 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1650 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1652 end
= start
+ bvec
->bv_len
- 1;
1654 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1659 if (--bvec
>= bio
->bi_io_vec
)
1660 prefetchw(&bvec
->bv_page
->flags
);
1662 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
1663 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
1668 if (!uptodate
&& tree
->ops
&&
1669 tree
->ops
->readpage_io_failed_hook
) {
1670 ret
= tree
->ops
->readpage_io_failed_hook(bio
, page
,
1674 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1682 set_extent_uptodate(tree
, start
, end
,
1685 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1689 SetPageUptodate(page
);
1691 ClearPageUptodate(page
);
1697 check_page_uptodate(tree
, page
);
1699 ClearPageUptodate(page
);
1702 check_page_locked(tree
, page
);
1704 } while (bvec
>= bio
->bi_io_vec
);
1710 * IO done from prepare_write is pretty simple, we just unlock
1711 * the structs in the extent tree when done, and set the uptodate bits
1714 static void end_bio_extent_preparewrite(struct bio
*bio
, int err
)
1716 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1717 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1718 struct extent_io_tree
*tree
;
1723 struct page
*page
= bvec
->bv_page
;
1724 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1726 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1728 end
= start
+ bvec
->bv_len
- 1;
1730 if (--bvec
>= bio
->bi_io_vec
)
1731 prefetchw(&bvec
->bv_page
->flags
);
1734 set_extent_uptodate(tree
, start
, end
, GFP_ATOMIC
);
1736 ClearPageUptodate(page
);
1740 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1742 } while (bvec
>= bio
->bi_io_vec
);
1748 extent_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
1753 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1755 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
1756 while (!bio
&& (nr_vecs
/= 2))
1757 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1762 bio
->bi_bdev
= bdev
;
1763 bio
->bi_sector
= first_sector
;
1768 static int submit_one_bio(int rw
, struct bio
*bio
, int mirror_num
,
1769 unsigned long bio_flags
)
1772 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1773 struct page
*page
= bvec
->bv_page
;
1774 struct extent_io_tree
*tree
= bio
->bi_private
;
1778 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
1779 end
= start
+ bvec
->bv_len
- 1;
1781 bio
->bi_private
= NULL
;
1785 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
1786 tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
1787 mirror_num
, bio_flags
);
1789 submit_bio(rw
, bio
);
1790 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
1796 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
1797 struct page
*page
, sector_t sector
,
1798 size_t size
, unsigned long offset
,
1799 struct block_device
*bdev
,
1800 struct bio
**bio_ret
,
1801 unsigned long max_pages
,
1802 bio_end_io_t end_io_func
,
1804 unsigned long prev_bio_flags
,
1805 unsigned long bio_flags
)
1811 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
1812 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
1813 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
1815 if (bio_ret
&& *bio_ret
) {
1818 contig
= bio
->bi_sector
== sector
;
1820 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
1823 if (prev_bio_flags
!= bio_flags
|| !contig
||
1824 (tree
->ops
&& tree
->ops
->merge_bio_hook
&&
1825 tree
->ops
->merge_bio_hook(page
, offset
, page_size
, bio
,
1827 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
1828 ret
= submit_one_bio(rw
, bio
, mirror_num
,
1835 if (this_compressed
)
1838 nr
= bio_get_nr_vecs(bdev
);
1840 bio
= extent_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
1842 bio_add_page(bio
, page
, page_size
, offset
);
1843 bio
->bi_end_io
= end_io_func
;
1844 bio
->bi_private
= tree
;
1849 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
1854 void set_page_extent_mapped(struct page
*page
)
1856 if (!PagePrivate(page
)) {
1857 SetPagePrivate(page
);
1858 page_cache_get(page
);
1859 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
1863 static void set_page_extent_head(struct page
*page
, unsigned long len
)
1865 set_page_private(page
, EXTENT_PAGE_PRIVATE_FIRST_PAGE
| len
<< 2);
1869 * basic readpage implementation. Locked extent state structs are inserted
1870 * into the tree that are removed when the IO is done (by the end_io
1873 static int __extent_read_full_page(struct extent_io_tree
*tree
,
1875 get_extent_t
*get_extent
,
1876 struct bio
**bio
, int mirror_num
,
1877 unsigned long *bio_flags
)
1879 struct inode
*inode
= page
->mapping
->host
;
1880 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1881 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
1885 u64 last_byte
= i_size_read(inode
);
1889 struct extent_map
*em
;
1890 struct block_device
*bdev
;
1893 size_t page_offset
= 0;
1895 size_t disk_io_size
;
1896 size_t blocksize
= inode
->i_sb
->s_blocksize
;
1897 unsigned long this_bio_flag
= 0;
1899 set_page_extent_mapped(page
);
1902 lock_extent(tree
, start
, end
, GFP_NOFS
);
1904 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
1906 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
1909 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
1910 userpage
= kmap_atomic(page
, KM_USER0
);
1911 memset(userpage
+ zero_offset
, 0, iosize
);
1912 flush_dcache_page(page
);
1913 kunmap_atomic(userpage
, KM_USER0
);
1916 while (cur
<= end
) {
1917 if (cur
>= last_byte
) {
1919 iosize
= PAGE_CACHE_SIZE
- page_offset
;
1920 userpage
= kmap_atomic(page
, KM_USER0
);
1921 memset(userpage
+ page_offset
, 0, iosize
);
1922 flush_dcache_page(page
);
1923 kunmap_atomic(userpage
, KM_USER0
);
1924 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
1926 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
1929 em
= get_extent(inode
, page
, page_offset
, cur
,
1931 if (IS_ERR(em
) || !em
) {
1933 unlock_extent(tree
, cur
, end
, GFP_NOFS
);
1936 extent_offset
= cur
- em
->start
;
1937 BUG_ON(extent_map_end(em
) <= cur
);
1940 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
1941 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
1943 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
1944 cur_end
= min(extent_map_end(em
) - 1, end
);
1945 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
1946 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
1947 disk_io_size
= em
->block_len
;
1948 sector
= em
->block_start
>> 9;
1950 sector
= (em
->block_start
+ extent_offset
) >> 9;
1951 disk_io_size
= iosize
;
1954 block_start
= em
->block_start
;
1955 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
1956 block_start
= EXTENT_MAP_HOLE
;
1957 free_extent_map(em
);
1960 /* we've found a hole, just zero and go on */
1961 if (block_start
== EXTENT_MAP_HOLE
) {
1963 userpage
= kmap_atomic(page
, KM_USER0
);
1964 memset(userpage
+ page_offset
, 0, iosize
);
1965 flush_dcache_page(page
);
1966 kunmap_atomic(userpage
, KM_USER0
);
1968 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
1970 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
1972 page_offset
+= iosize
;
1975 /* the get_extent function already copied into the page */
1976 if (test_range_bit(tree
, cur
, cur_end
, EXTENT_UPTODATE
, 1)) {
1977 check_page_uptodate(tree
, page
);
1978 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
1980 page_offset
+= iosize
;
1983 /* we have an inline extent but it didn't get marked up
1984 * to date. Error out
1986 if (block_start
== EXTENT_MAP_INLINE
) {
1988 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
1990 page_offset
+= iosize
;
1995 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
1996 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2000 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2002 ret
= submit_extent_page(READ
, tree
, page
,
2003 sector
, disk_io_size
, page_offset
,
2005 end_bio_extent_readpage
, mirror_num
,
2009 *bio_flags
= this_bio_flag
;
2014 page_offset
+= iosize
;
2017 if (!PageError(page
))
2018 SetPageUptodate(page
);
2024 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2025 get_extent_t
*get_extent
)
2027 struct bio
*bio
= NULL
;
2028 unsigned long bio_flags
= 0;
2031 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, 0,
2034 submit_one_bio(READ
, bio
, 0, bio_flags
);
2038 static noinline
void update_nr_written(struct page
*page
,
2039 struct writeback_control
*wbc
,
2040 unsigned long nr_written
)
2042 wbc
->nr_to_write
-= nr_written
;
2043 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2044 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2045 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2049 * the writepage semantics are similar to regular writepage. extent
2050 * records are inserted to lock ranges in the tree, and as dirty areas
2051 * are found, they are marked writeback. Then the lock bits are removed
2052 * and the end_io handler clears the writeback ranges
2054 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2057 struct inode
*inode
= page
->mapping
->host
;
2058 struct extent_page_data
*epd
= data
;
2059 struct extent_io_tree
*tree
= epd
->tree
;
2060 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2062 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2066 u64 last_byte
= i_size_read(inode
);
2071 struct extent_map
*em
;
2072 struct block_device
*bdev
;
2075 size_t pg_offset
= 0;
2077 loff_t i_size
= i_size_read(inode
);
2078 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2084 unsigned long nr_written
= 0;
2086 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2087 write_flags
= WRITE_SYNC_PLUG
;
2089 write_flags
= WRITE
;
2091 WARN_ON(!PageLocked(page
));
2092 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2093 if (page
->index
> end_index
||
2094 (page
->index
== end_index
&& !pg_offset
)) {
2095 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2100 if (page
->index
== end_index
) {
2103 userpage
= kmap_atomic(page
, KM_USER0
);
2104 memset(userpage
+ pg_offset
, 0,
2105 PAGE_CACHE_SIZE
- pg_offset
);
2106 kunmap_atomic(userpage
, KM_USER0
);
2107 flush_dcache_page(page
);
2111 set_page_extent_mapped(page
);
2113 delalloc_start
= start
;
2116 if (!epd
->extent_locked
) {
2118 * make sure the wbc mapping index is at least updated
2121 update_nr_written(page
, wbc
, 0);
2123 while (delalloc_end
< page_end
) {
2124 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2129 if (nr_delalloc
== 0) {
2130 delalloc_start
= delalloc_end
+ 1;
2133 tree
->ops
->fill_delalloc(inode
, page
, delalloc_start
,
2134 delalloc_end
, &page_started
,
2136 delalloc_start
= delalloc_end
+ 1;
2139 /* did the fill delalloc function already unlock and start
2145 * we've unlocked the page, so we can't update
2146 * the mapping's writeback index, just update
2149 wbc
->nr_to_write
-= nr_written
;
2153 lock_extent(tree
, start
, page_end
, GFP_NOFS
);
2155 unlock_start
= start
;
2157 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2158 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2160 if (ret
== -EAGAIN
) {
2161 unlock_extent(tree
, start
, page_end
, GFP_NOFS
);
2162 redirty_page_for_writepage(wbc
, page
);
2163 update_nr_written(page
, wbc
, nr_written
);
2171 * we don't want to touch the inode after unlocking the page,
2172 * so we update the mapping writeback index now
2174 update_nr_written(page
, wbc
, nr_written
+ 1);
2177 if (test_range_bit(tree
, start
, page_end
, EXTENT_DELALLOC
, 0))
2178 printk(KERN_ERR
"btrfs delalloc bits after lock_extent\n");
2180 if (last_byte
<= start
) {
2181 clear_extent_dirty(tree
, start
, page_end
, GFP_NOFS
);
2182 unlock_extent(tree
, start
, page_end
, GFP_NOFS
);
2183 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2184 tree
->ops
->writepage_end_io_hook(page
, start
,
2186 unlock_start
= page_end
+ 1;
2190 set_extent_uptodate(tree
, start
, page_end
, GFP_NOFS
);
2191 blocksize
= inode
->i_sb
->s_blocksize
;
2193 while (cur
<= end
) {
2194 if (cur
>= last_byte
) {
2195 clear_extent_dirty(tree
, cur
, page_end
, GFP_NOFS
);
2196 unlock_extent(tree
, unlock_start
, page_end
, GFP_NOFS
);
2197 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2198 tree
->ops
->writepage_end_io_hook(page
, cur
,
2200 unlock_start
= page_end
+ 1;
2203 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2205 if (IS_ERR(em
) || !em
) {
2210 extent_offset
= cur
- em
->start
;
2211 BUG_ON(extent_map_end(em
) <= cur
);
2213 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2214 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2215 sector
= (em
->block_start
+ extent_offset
) >> 9;
2217 block_start
= em
->block_start
;
2218 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2219 free_extent_map(em
);
2223 * compressed and inline extents are written through other
2226 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2227 block_start
== EXTENT_MAP_INLINE
) {
2228 clear_extent_dirty(tree
, cur
,
2229 cur
+ iosize
- 1, GFP_NOFS
);
2231 unlock_extent(tree
, unlock_start
, cur
+ iosize
- 1,
2235 * end_io notification does not happen here for
2236 * compressed extents
2238 if (!compressed
&& tree
->ops
&&
2239 tree
->ops
->writepage_end_io_hook
)
2240 tree
->ops
->writepage_end_io_hook(page
, cur
,
2243 else if (compressed
) {
2244 /* we don't want to end_page_writeback on
2245 * a compressed extent. this happens
2252 pg_offset
+= iosize
;
2256 /* leave this out until we have a page_mkwrite call */
2257 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2260 pg_offset
+= iosize
;
2264 clear_extent_dirty(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2265 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2266 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
2274 unsigned long max_nr
= end_index
+ 1;
2276 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
2277 if (!PageWriteback(page
)) {
2278 printk(KERN_ERR
"btrfs warning page %lu not "
2279 "writeback, cur %llu end %llu\n",
2280 page
->index
, (unsigned long long)cur
,
2281 (unsigned long long)end
);
2284 ret
= submit_extent_page(write_flags
, tree
, page
,
2285 sector
, iosize
, pg_offset
,
2286 bdev
, &epd
->bio
, max_nr
,
2287 end_bio_extent_writepage
,
2293 pg_offset
+= iosize
;
2298 /* make sure the mapping tag for page dirty gets cleared */
2299 set_page_writeback(page
);
2300 end_page_writeback(page
);
2302 if (unlock_start
<= page_end
)
2303 unlock_extent(tree
, unlock_start
, page_end
, GFP_NOFS
);
2312 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2313 * @mapping: address space structure to write
2314 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2315 * @writepage: function called for each page
2316 * @data: data passed to writepage function
2318 * If a page is already under I/O, write_cache_pages() skips it, even
2319 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2320 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2321 * and msync() need to guarantee that all the data which was dirty at the time
2322 * the call was made get new I/O started against them. If wbc->sync_mode is
2323 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2324 * existing IO to complete.
2326 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
2327 struct address_space
*mapping
,
2328 struct writeback_control
*wbc
,
2329 writepage_t writepage
, void *data
,
2330 void (*flush_fn
)(void *))
2332 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
2335 struct pagevec pvec
;
2338 pgoff_t end
; /* Inclusive */
2340 int range_whole
= 0;
2342 pagevec_init(&pvec
, 0);
2343 if (wbc
->range_cyclic
) {
2344 index
= mapping
->writeback_index
; /* Start from prev offset */
2347 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2348 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2349 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2354 while (!done
&& (index
<= end
) &&
2355 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
2356 PAGECACHE_TAG_DIRTY
, min(end
- index
,
2357 (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
2361 for (i
= 0; i
< nr_pages
; i
++) {
2362 struct page
*page
= pvec
.pages
[i
];
2365 * At this point we hold neither mapping->tree_lock nor
2366 * lock on the page itself: the page may be truncated or
2367 * invalidated (changing page->mapping to NULL), or even
2368 * swizzled back from swapper_space to tmpfs file
2371 if (tree
->ops
&& tree
->ops
->write_cache_pages_lock_hook
)
2372 tree
->ops
->write_cache_pages_lock_hook(page
);
2376 if (unlikely(page
->mapping
!= mapping
)) {
2381 if (!wbc
->range_cyclic
&& page
->index
> end
) {
2387 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
2388 if (PageWriteback(page
))
2390 wait_on_page_writeback(page
);
2393 if (PageWriteback(page
) ||
2394 !clear_page_dirty_for_io(page
)) {
2399 ret
= (*writepage
)(page
, wbc
, data
);
2401 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
2405 if (ret
|| wbc
->nr_to_write
<= 0)
2407 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
2408 wbc
->encountered_congestion
= 1;
2412 pagevec_release(&pvec
);
2415 if (!scanned
&& !done
) {
2417 * We hit the last page and there is more work to be done: wrap
2418 * back to the start of the file
2427 static void flush_epd_write_bio(struct extent_page_data
*epd
)
2431 submit_one_bio(WRITE_SYNC
, epd
->bio
, 0, 0);
2433 submit_one_bio(WRITE
, epd
->bio
, 0, 0);
2438 static noinline
void flush_write_bio(void *data
)
2440 struct extent_page_data
*epd
= data
;
2441 flush_epd_write_bio(epd
);
2444 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2445 get_extent_t
*get_extent
,
2446 struct writeback_control
*wbc
)
2449 struct address_space
*mapping
= page
->mapping
;
2450 struct extent_page_data epd
= {
2453 .get_extent
= get_extent
,
2455 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2457 struct writeback_control wbc_writepages
= {
2459 .sync_mode
= wbc
->sync_mode
,
2460 .older_than_this
= NULL
,
2462 .range_start
= page_offset(page
) + PAGE_CACHE_SIZE
,
2463 .range_end
= (loff_t
)-1,
2466 ret
= __extent_writepage(page
, wbc
, &epd
);
2468 extent_write_cache_pages(tree
, mapping
, &wbc_writepages
,
2469 __extent_writepage
, &epd
, flush_write_bio
);
2470 flush_epd_write_bio(&epd
);
2474 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
2475 u64 start
, u64 end
, get_extent_t
*get_extent
,
2479 struct address_space
*mapping
= inode
->i_mapping
;
2481 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
2484 struct extent_page_data epd
= {
2487 .get_extent
= get_extent
,
2489 .sync_io
= mode
== WB_SYNC_ALL
,
2491 struct writeback_control wbc_writepages
= {
2492 .bdi
= inode
->i_mapping
->backing_dev_info
,
2494 .older_than_this
= NULL
,
2495 .nr_to_write
= nr_pages
* 2,
2496 .range_start
= start
,
2497 .range_end
= end
+ 1,
2500 while (start
<= end
) {
2501 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
2502 if (clear_page_dirty_for_io(page
))
2503 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
2505 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2506 tree
->ops
->writepage_end_io_hook(page
, start
,
2507 start
+ PAGE_CACHE_SIZE
- 1,
2511 page_cache_release(page
);
2512 start
+= PAGE_CACHE_SIZE
;
2515 flush_epd_write_bio(&epd
);
2519 int extent_writepages(struct extent_io_tree
*tree
,
2520 struct address_space
*mapping
,
2521 get_extent_t
*get_extent
,
2522 struct writeback_control
*wbc
)
2525 struct extent_page_data epd
= {
2528 .get_extent
= get_extent
,
2530 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2533 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
2534 __extent_writepage
, &epd
,
2536 flush_epd_write_bio(&epd
);
2540 int extent_readpages(struct extent_io_tree
*tree
,
2541 struct address_space
*mapping
,
2542 struct list_head
*pages
, unsigned nr_pages
,
2543 get_extent_t get_extent
)
2545 struct bio
*bio
= NULL
;
2547 struct pagevec pvec
;
2548 unsigned long bio_flags
= 0;
2550 pagevec_init(&pvec
, 0);
2551 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
2552 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
2554 prefetchw(&page
->flags
);
2555 list_del(&page
->lru
);
2557 * what we want to do here is call add_to_page_cache_lru,
2558 * but that isn't exported, so we reproduce it here
2560 if (!add_to_page_cache(page
, mapping
,
2561 page
->index
, GFP_KERNEL
)) {
2563 /* open coding of lru_cache_add, also not exported */
2564 page_cache_get(page
);
2565 if (!pagevec_add(&pvec
, page
))
2566 __pagevec_lru_add_file(&pvec
);
2567 __extent_read_full_page(tree
, page
, get_extent
,
2568 &bio
, 0, &bio_flags
);
2570 page_cache_release(page
);
2572 if (pagevec_count(&pvec
))
2573 __pagevec_lru_add_file(&pvec
);
2574 BUG_ON(!list_empty(pages
));
2576 submit_one_bio(READ
, bio
, 0, bio_flags
);
2581 * basic invalidatepage code, this waits on any locked or writeback
2582 * ranges corresponding to the page, and then deletes any extent state
2583 * records from the tree
2585 int extent_invalidatepage(struct extent_io_tree
*tree
,
2586 struct page
*page
, unsigned long offset
)
2588 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
2589 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2590 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
2592 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
2596 lock_extent(tree
, start
, end
, GFP_NOFS
);
2597 wait_on_extent_writeback(tree
, start
, end
);
2598 clear_extent_bit(tree
, start
, end
,
2599 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
,
2605 * simple commit_write call, set_range_dirty is used to mark both
2606 * the pages and the extent records as dirty
2608 int extent_commit_write(struct extent_io_tree
*tree
,
2609 struct inode
*inode
, struct page
*page
,
2610 unsigned from
, unsigned to
)
2612 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
2614 set_page_extent_mapped(page
);
2615 set_page_dirty(page
);
2617 if (pos
> inode
->i_size
) {
2618 i_size_write(inode
, pos
);
2619 mark_inode_dirty(inode
);
2624 int extent_prepare_write(struct extent_io_tree
*tree
,
2625 struct inode
*inode
, struct page
*page
,
2626 unsigned from
, unsigned to
, get_extent_t
*get_extent
)
2628 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2629 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2631 u64 orig_block_start
;
2634 struct extent_map
*em
;
2635 unsigned blocksize
= 1 << inode
->i_blkbits
;
2636 size_t page_offset
= 0;
2637 size_t block_off_start
;
2638 size_t block_off_end
;
2644 set_page_extent_mapped(page
);
2646 block_start
= (page_start
+ from
) & ~((u64
)blocksize
- 1);
2647 block_end
= (page_start
+ to
- 1) | (blocksize
- 1);
2648 orig_block_start
= block_start
;
2650 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2651 while (block_start
<= block_end
) {
2652 em
= get_extent(inode
, page
, page_offset
, block_start
,
2653 block_end
- block_start
+ 1, 1);
2654 if (IS_ERR(em
) || !em
)
2657 cur_end
= min(block_end
, extent_map_end(em
) - 1);
2658 block_off_start
= block_start
& (PAGE_CACHE_SIZE
- 1);
2659 block_off_end
= block_off_start
+ blocksize
;
2660 isnew
= clear_extent_new(tree
, block_start
, cur_end
, GFP_NOFS
);
2662 if (!PageUptodate(page
) && isnew
&&
2663 (block_off_end
> to
|| block_off_start
< from
)) {
2666 kaddr
= kmap_atomic(page
, KM_USER0
);
2667 if (block_off_end
> to
)
2668 memset(kaddr
+ to
, 0, block_off_end
- to
);
2669 if (block_off_start
< from
)
2670 memset(kaddr
+ block_off_start
, 0,
2671 from
- block_off_start
);
2672 flush_dcache_page(page
);
2673 kunmap_atomic(kaddr
, KM_USER0
);
2675 if ((em
->block_start
!= EXTENT_MAP_HOLE
&&
2676 em
->block_start
!= EXTENT_MAP_INLINE
) &&
2677 !isnew
&& !PageUptodate(page
) &&
2678 (block_off_end
> to
|| block_off_start
< from
) &&
2679 !test_range_bit(tree
, block_start
, cur_end
,
2680 EXTENT_UPTODATE
, 1)) {
2682 u64 extent_offset
= block_start
- em
->start
;
2684 sector
= (em
->block_start
+ extent_offset
) >> 9;
2685 iosize
= (cur_end
- block_start
+ blocksize
) &
2686 ~((u64
)blocksize
- 1);
2688 * we've already got the extent locked, but we
2689 * need to split the state such that our end_bio
2690 * handler can clear the lock.
2692 set_extent_bit(tree
, block_start
,
2693 block_start
+ iosize
- 1,
2694 EXTENT_LOCKED
, 0, NULL
, GFP_NOFS
);
2695 ret
= submit_extent_page(READ
, tree
, page
,
2696 sector
, iosize
, page_offset
, em
->bdev
,
2698 end_bio_extent_preparewrite
, 0,
2701 block_start
= block_start
+ iosize
;
2703 set_extent_uptodate(tree
, block_start
, cur_end
,
2705 unlock_extent(tree
, block_start
, cur_end
, GFP_NOFS
);
2706 block_start
= cur_end
+ 1;
2708 page_offset
= block_start
& (PAGE_CACHE_SIZE
- 1);
2709 free_extent_map(em
);
2712 wait_extent_bit(tree
, orig_block_start
,
2713 block_end
, EXTENT_LOCKED
);
2715 check_page_uptodate(tree
, page
);
2717 /* FIXME, zero out newly allocated blocks on error */
2722 * a helper for releasepage, this tests for areas of the page that
2723 * are locked or under IO and drops the related state bits if it is safe
2726 int try_release_extent_state(struct extent_map_tree
*map
,
2727 struct extent_io_tree
*tree
, struct page
*page
,
2730 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2731 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2734 if (test_range_bit(tree
, start
, end
,
2735 EXTENT_IOBITS
| EXTENT_ORDERED
, 0))
2738 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
2740 clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
,
2747 * a helper for releasepage. As long as there are no locked extents
2748 * in the range corresponding to the page, both state records and extent
2749 * map records are removed
2751 int try_release_extent_mapping(struct extent_map_tree
*map
,
2752 struct extent_io_tree
*tree
, struct page
*page
,
2755 struct extent_map
*em
;
2756 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2757 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2759 if ((mask
& __GFP_WAIT
) &&
2760 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
2762 while (start
<= end
) {
2763 len
= end
- start
+ 1;
2764 spin_lock(&map
->lock
);
2765 em
= lookup_extent_mapping(map
, start
, len
);
2766 if (!em
|| IS_ERR(em
)) {
2767 spin_unlock(&map
->lock
);
2770 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
2771 em
->start
!= start
) {
2772 spin_unlock(&map
->lock
);
2773 free_extent_map(em
);
2776 if (!test_range_bit(tree
, em
->start
,
2777 extent_map_end(em
) - 1,
2778 EXTENT_LOCKED
| EXTENT_WRITEBACK
|
2781 remove_extent_mapping(map
, em
);
2782 /* once for the rb tree */
2783 free_extent_map(em
);
2785 start
= extent_map_end(em
);
2786 spin_unlock(&map
->lock
);
2789 free_extent_map(em
);
2792 return try_release_extent_state(map
, tree
, page
, mask
);
2795 sector_t
extent_bmap(struct address_space
*mapping
, sector_t iblock
,
2796 get_extent_t
*get_extent
)
2798 struct inode
*inode
= mapping
->host
;
2799 u64 start
= iblock
<< inode
->i_blkbits
;
2800 sector_t sector
= 0;
2801 size_t blksize
= (1 << inode
->i_blkbits
);
2802 struct extent_map
*em
;
2804 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2806 em
= get_extent(inode
, NULL
, 0, start
, blksize
, 0);
2807 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2809 if (!em
|| IS_ERR(em
))
2812 if (em
->block_start
> EXTENT_MAP_LAST_BYTE
)
2815 sector
= (em
->block_start
+ start
- em
->start
) >> inode
->i_blkbits
;
2817 free_extent_map(em
);
2821 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
2822 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
2826 u64 max
= start
+ len
;
2829 struct extent_map
*em
= NULL
;
2831 u64 em_start
= 0, em_len
= 0;
2832 unsigned long emflags
;
2838 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
2840 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
2848 off
= em
->start
+ em
->len
;
2852 em_start
= em
->start
;
2858 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
2860 flags
|= FIEMAP_EXTENT_LAST
;
2861 } else if (em
->block_start
== EXTENT_MAP_HOLE
) {
2862 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
2863 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
2864 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
2865 FIEMAP_EXTENT_NOT_ALIGNED
);
2866 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
2867 flags
|= (FIEMAP_EXTENT_DELALLOC
|
2868 FIEMAP_EXTENT_UNKNOWN
);
2870 disko
= em
->block_start
;
2872 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
2873 flags
|= FIEMAP_EXTENT_ENCODED
;
2875 emflags
= em
->flags
;
2876 free_extent_map(em
);
2880 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
2887 emflags
= em
->flags
;
2889 if (test_bit(EXTENT_FLAG_VACANCY
, &emflags
)) {
2890 flags
|= FIEMAP_EXTENT_LAST
;
2894 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
2900 free_extent_map(em
);
2902 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
2907 static inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
2911 struct address_space
*mapping
;
2914 return eb
->first_page
;
2915 i
+= eb
->start
>> PAGE_CACHE_SHIFT
;
2916 mapping
= eb
->first_page
->mapping
;
2921 * extent_buffer_page is only called after pinning the page
2922 * by increasing the reference count. So we know the page must
2923 * be in the radix tree.
2926 p
= radix_tree_lookup(&mapping
->page_tree
, i
);
2932 static inline unsigned long num_extent_pages(u64 start
, u64 len
)
2934 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
2935 (start
>> PAGE_CACHE_SHIFT
);
2938 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
2943 struct extent_buffer
*eb
= NULL
;
2945 unsigned long flags
;
2948 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
2951 spin_lock_init(&eb
->lock
);
2952 init_waitqueue_head(&eb
->lock_wq
);
2955 spin_lock_irqsave(&leak_lock
, flags
);
2956 list_add(&eb
->leak_list
, &buffers
);
2957 spin_unlock_irqrestore(&leak_lock
, flags
);
2959 atomic_set(&eb
->refs
, 1);
2964 static void __free_extent_buffer(struct extent_buffer
*eb
)
2967 unsigned long flags
;
2968 spin_lock_irqsave(&leak_lock
, flags
);
2969 list_del(&eb
->leak_list
);
2970 spin_unlock_irqrestore(&leak_lock
, flags
);
2972 kmem_cache_free(extent_buffer_cache
, eb
);
2975 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
2976 u64 start
, unsigned long len
,
2980 unsigned long num_pages
= num_extent_pages(start
, len
);
2982 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
2983 struct extent_buffer
*eb
;
2984 struct extent_buffer
*exists
= NULL
;
2986 struct address_space
*mapping
= tree
->mapping
;
2989 spin_lock(&tree
->buffer_lock
);
2990 eb
= buffer_search(tree
, start
);
2992 atomic_inc(&eb
->refs
);
2993 spin_unlock(&tree
->buffer_lock
);
2994 mark_page_accessed(eb
->first_page
);
2997 spin_unlock(&tree
->buffer_lock
);
2999 eb
= __alloc_extent_buffer(tree
, start
, len
, mask
);
3004 eb
->first_page
= page0
;
3007 page_cache_get(page0
);
3008 mark_page_accessed(page0
);
3009 set_page_extent_mapped(page0
);
3010 set_page_extent_head(page0
, len
);
3011 uptodate
= PageUptodate(page0
);
3015 for (; i
< num_pages
; i
++, index
++) {
3016 p
= find_or_create_page(mapping
, index
, mask
| __GFP_HIGHMEM
);
3021 set_page_extent_mapped(p
);
3022 mark_page_accessed(p
);
3025 set_page_extent_head(p
, len
);
3027 set_page_private(p
, EXTENT_PAGE_PRIVATE
);
3029 if (!PageUptodate(p
))
3034 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3036 spin_lock(&tree
->buffer_lock
);
3037 exists
= buffer_tree_insert(tree
, start
, &eb
->rb_node
);
3039 /* add one reference for the caller */
3040 atomic_inc(&exists
->refs
);
3041 spin_unlock(&tree
->buffer_lock
);
3044 spin_unlock(&tree
->buffer_lock
);
3046 /* add one reference for the tree */
3047 atomic_inc(&eb
->refs
);
3051 if (!atomic_dec_and_test(&eb
->refs
))
3053 for (index
= 1; index
< i
; index
++)
3054 page_cache_release(extent_buffer_page(eb
, index
));
3055 page_cache_release(extent_buffer_page(eb
, 0));
3056 __free_extent_buffer(eb
);
3060 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
3061 u64 start
, unsigned long len
,
3064 struct extent_buffer
*eb
;
3066 spin_lock(&tree
->buffer_lock
);
3067 eb
= buffer_search(tree
, start
);
3069 atomic_inc(&eb
->refs
);
3070 spin_unlock(&tree
->buffer_lock
);
3073 mark_page_accessed(eb
->first_page
);
3078 void free_extent_buffer(struct extent_buffer
*eb
)
3083 if (!atomic_dec_and_test(&eb
->refs
))
3089 int clear_extent_buffer_dirty(struct extent_io_tree
*tree
,
3090 struct extent_buffer
*eb
)
3093 unsigned long num_pages
;
3096 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3098 for (i
= 0; i
< num_pages
; i
++) {
3099 page
= extent_buffer_page(eb
, i
);
3100 if (!PageDirty(page
))
3105 set_page_extent_head(page
, eb
->len
);
3107 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
3109 clear_page_dirty_for_io(page
);
3110 spin_lock_irq(&page
->mapping
->tree_lock
);
3111 if (!PageDirty(page
)) {
3112 radix_tree_tag_clear(&page
->mapping
->page_tree
,
3114 PAGECACHE_TAG_DIRTY
);
3116 spin_unlock_irq(&page
->mapping
->tree_lock
);
3122 int wait_on_extent_buffer_writeback(struct extent_io_tree
*tree
,
3123 struct extent_buffer
*eb
)
3125 return wait_on_extent_writeback(tree
, eb
->start
,
3126 eb
->start
+ eb
->len
- 1);
3129 int set_extent_buffer_dirty(struct extent_io_tree
*tree
,
3130 struct extent_buffer
*eb
)
3133 unsigned long num_pages
;
3136 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
3137 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3138 for (i
= 0; i
< num_pages
; i
++)
3139 __set_page_dirty_nobuffers(extent_buffer_page(eb
, i
));
3143 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3144 struct extent_buffer
*eb
)
3148 unsigned long num_pages
;
3150 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3151 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3153 clear_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3155 for (i
= 0; i
< num_pages
; i
++) {
3156 page
= extent_buffer_page(eb
, i
);
3158 ClearPageUptodate(page
);
3163 int set_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3164 struct extent_buffer
*eb
)
3168 unsigned long num_pages
;
3170 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3172 set_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3174 for (i
= 0; i
< num_pages
; i
++) {
3175 page
= extent_buffer_page(eb
, i
);
3176 if ((i
== 0 && (eb
->start
& (PAGE_CACHE_SIZE
- 1))) ||
3177 ((i
== num_pages
- 1) &&
3178 ((eb
->start
+ eb
->len
) & (PAGE_CACHE_SIZE
- 1)))) {
3179 check_page_uptodate(tree
, page
);
3182 SetPageUptodate(page
);
3187 int extent_range_uptodate(struct extent_io_tree
*tree
,
3192 int pg_uptodate
= 1;
3194 unsigned long index
;
3196 ret
= test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1);
3199 while (start
<= end
) {
3200 index
= start
>> PAGE_CACHE_SHIFT
;
3201 page
= find_get_page(tree
->mapping
, index
);
3202 uptodate
= PageUptodate(page
);
3203 page_cache_release(page
);
3208 start
+= PAGE_CACHE_SIZE
;
3213 int extent_buffer_uptodate(struct extent_io_tree
*tree
,
3214 struct extent_buffer
*eb
)
3217 unsigned long num_pages
;
3220 int pg_uptodate
= 1;
3222 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3225 ret
= test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3226 EXTENT_UPTODATE
, 1);
3230 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3231 for (i
= 0; i
< num_pages
; i
++) {
3232 page
= extent_buffer_page(eb
, i
);
3233 if (!PageUptodate(page
)) {
3241 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
3242 struct extent_buffer
*eb
,
3243 u64 start
, int wait
,
3244 get_extent_t
*get_extent
, int mirror_num
)
3247 unsigned long start_i
;
3251 int locked_pages
= 0;
3252 int all_uptodate
= 1;
3253 int inc_all_pages
= 0;
3254 unsigned long num_pages
;
3255 struct bio
*bio
= NULL
;
3256 unsigned long bio_flags
= 0;
3258 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3261 if (test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3262 EXTENT_UPTODATE
, 1)) {
3267 WARN_ON(start
< eb
->start
);
3268 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
3269 (eb
->start
>> PAGE_CACHE_SHIFT
);
3274 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3275 for (i
= start_i
; i
< num_pages
; i
++) {
3276 page
= extent_buffer_page(eb
, i
);
3278 if (!trylock_page(page
))
3284 if (!PageUptodate(page
))
3289 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3293 for (i
= start_i
; i
< num_pages
; i
++) {
3294 page
= extent_buffer_page(eb
, i
);
3296 page_cache_get(page
);
3297 if (!PageUptodate(page
)) {
3300 ClearPageError(page
);
3301 err
= __extent_read_full_page(tree
, page
,
3303 mirror_num
, &bio_flags
);
3312 submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
3317 for (i
= start_i
; i
< num_pages
; i
++) {
3318 page
= extent_buffer_page(eb
, i
);
3319 wait_on_page_locked(page
);
3320 if (!PageUptodate(page
))
3325 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3330 while (locked_pages
> 0) {
3331 page
= extent_buffer_page(eb
, i
);
3339 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
3340 unsigned long start
,
3347 char *dst
= (char *)dstv
;
3348 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3349 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3351 WARN_ON(start
> eb
->len
);
3352 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3354 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3357 page
= extent_buffer_page(eb
, i
);
3359 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3360 kaddr
= kmap_atomic(page
, KM_USER1
);
3361 memcpy(dst
, kaddr
+ offset
, cur
);
3362 kunmap_atomic(kaddr
, KM_USER1
);
3371 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3372 unsigned long min_len
, char **token
, char **map
,
3373 unsigned long *map_start
,
3374 unsigned long *map_len
, int km
)
3376 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
3379 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3380 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3381 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
3388 offset
= start_offset
;
3392 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
3395 if (start
+ min_len
> eb
->len
) {
3396 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
3397 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
3398 eb
->len
, start
, min_len
);
3402 p
= extent_buffer_page(eb
, i
);
3403 kaddr
= kmap_atomic(p
, km
);
3405 *map
= kaddr
+ offset
;
3406 *map_len
= PAGE_CACHE_SIZE
- offset
;
3410 int map_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3411 unsigned long min_len
,
3412 char **token
, char **map
,
3413 unsigned long *map_start
,
3414 unsigned long *map_len
, int km
)
3418 if (eb
->map_token
) {
3419 unmap_extent_buffer(eb
, eb
->map_token
, km
);
3420 eb
->map_token
= NULL
;
3423 err
= map_private_extent_buffer(eb
, start
, min_len
, token
, map
,
3424 map_start
, map_len
, km
);
3426 eb
->map_token
= *token
;
3428 eb
->map_start
= *map_start
;
3429 eb
->map_len
= *map_len
;
3434 void unmap_extent_buffer(struct extent_buffer
*eb
, char *token
, int km
)
3436 kunmap_atomic(token
, km
);
3439 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
3440 unsigned long start
,
3447 char *ptr
= (char *)ptrv
;
3448 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3449 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3452 WARN_ON(start
> eb
->len
);
3453 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3455 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3458 page
= extent_buffer_page(eb
, i
);
3460 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3462 kaddr
= kmap_atomic(page
, KM_USER0
);
3463 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
3464 kunmap_atomic(kaddr
, KM_USER0
);
3476 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
3477 unsigned long start
, unsigned long len
)
3483 char *src
= (char *)srcv
;
3484 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3485 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3487 WARN_ON(start
> eb
->len
);
3488 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3490 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3493 page
= extent_buffer_page(eb
, i
);
3494 WARN_ON(!PageUptodate(page
));
3496 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3497 kaddr
= kmap_atomic(page
, KM_USER1
);
3498 memcpy(kaddr
+ offset
, src
, cur
);
3499 kunmap_atomic(kaddr
, KM_USER1
);
3508 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
3509 unsigned long start
, unsigned long len
)
3515 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3516 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3518 WARN_ON(start
> eb
->len
);
3519 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3521 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3524 page
= extent_buffer_page(eb
, i
);
3525 WARN_ON(!PageUptodate(page
));
3527 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3528 kaddr
= kmap_atomic(page
, KM_USER0
);
3529 memset(kaddr
+ offset
, c
, cur
);
3530 kunmap_atomic(kaddr
, KM_USER0
);
3538 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
3539 unsigned long dst_offset
, unsigned long src_offset
,
3542 u64 dst_len
= dst
->len
;
3547 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3548 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3550 WARN_ON(src
->len
!= dst_len
);
3552 offset
= (start_offset
+ dst_offset
) &
3553 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3556 page
= extent_buffer_page(dst
, i
);
3557 WARN_ON(!PageUptodate(page
));
3559 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
3561 kaddr
= kmap_atomic(page
, KM_USER0
);
3562 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
3563 kunmap_atomic(kaddr
, KM_USER0
);
3572 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
3573 unsigned long dst_off
, unsigned long src_off
,
3576 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3577 if (dst_page
== src_page
) {
3578 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
3580 char *src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3581 char *p
= dst_kaddr
+ dst_off
+ len
;
3582 char *s
= src_kaddr
+ src_off
+ len
;
3587 kunmap_atomic(src_kaddr
, KM_USER1
);
3589 kunmap_atomic(dst_kaddr
, KM_USER0
);
3592 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
3593 unsigned long dst_off
, unsigned long src_off
,
3596 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3599 if (dst_page
!= src_page
)
3600 src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3602 src_kaddr
= dst_kaddr
;
3604 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
3605 kunmap_atomic(dst_kaddr
, KM_USER0
);
3606 if (dst_page
!= src_page
)
3607 kunmap_atomic(src_kaddr
, KM_USER1
);
3610 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3611 unsigned long src_offset
, unsigned long len
)
3614 size_t dst_off_in_page
;
3615 size_t src_off_in_page
;
3616 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3617 unsigned long dst_i
;
3618 unsigned long src_i
;
3620 if (src_offset
+ len
> dst
->len
) {
3621 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3622 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
3625 if (dst_offset
+ len
> dst
->len
) {
3626 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3627 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
3632 dst_off_in_page
= (start_offset
+ dst_offset
) &
3633 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3634 src_off_in_page
= (start_offset
+ src_offset
) &
3635 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3637 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3638 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
3640 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
3642 cur
= min_t(unsigned long, cur
,
3643 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
3645 copy_pages(extent_buffer_page(dst
, dst_i
),
3646 extent_buffer_page(dst
, src_i
),
3647 dst_off_in_page
, src_off_in_page
, cur
);
3655 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3656 unsigned long src_offset
, unsigned long len
)
3659 size_t dst_off_in_page
;
3660 size_t src_off_in_page
;
3661 unsigned long dst_end
= dst_offset
+ len
- 1;
3662 unsigned long src_end
= src_offset
+ len
- 1;
3663 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3664 unsigned long dst_i
;
3665 unsigned long src_i
;
3667 if (src_offset
+ len
> dst
->len
) {
3668 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3669 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
3672 if (dst_offset
+ len
> dst
->len
) {
3673 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3674 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
3677 if (dst_offset
< src_offset
) {
3678 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
3682 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
3683 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
3685 dst_off_in_page
= (start_offset
+ dst_end
) &
3686 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3687 src_off_in_page
= (start_offset
+ src_end
) &
3688 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3690 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
3691 cur
= min(cur
, dst_off_in_page
+ 1);
3692 move_pages(extent_buffer_page(dst
, dst_i
),
3693 extent_buffer_page(dst
, src_i
),
3694 dst_off_in_page
- cur
+ 1,
3695 src_off_in_page
- cur
+ 1, cur
);
3703 int try_release_extent_buffer(struct extent_io_tree
*tree
, struct page
*page
)
3705 u64 start
= page_offset(page
);
3706 struct extent_buffer
*eb
;
3709 unsigned long num_pages
;
3711 spin_lock(&tree
->buffer_lock
);
3712 eb
= buffer_search(tree
, start
);
3716 if (atomic_read(&eb
->refs
) > 1) {
3720 if (test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3724 /* at this point we can safely release the extent buffer */
3725 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3726 for (i
= 0; i
< num_pages
; i
++)
3727 page_cache_release(extent_buffer_page(eb
, i
));
3728 rb_erase(&eb
->rb_node
, &tree
->buffer
);
3729 __free_extent_buffer(eb
);
3731 spin_unlock(&tree
->buffer_lock
);