1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/module.h>
8 #include <linux/spinlock.h>
9 #include <linux/blkdev.h>
10 #include <linux/swap.h>
11 #include <linux/writeback.h>
12 #include <linux/pagevec.h>
13 #include <linux/prefetch.h>
14 #include <linux/cleancache.h>
15 #include "extent_io.h"
16 #include "extent_map.h"
19 #include "btrfs_inode.h"
21 static struct kmem_cache *extent_state_cache;
22 static struct kmem_cache *extent_buffer_cache;
24 static LIST_HEAD(buffers);
25 static LIST_HEAD(states);
29 static DEFINE_SPINLOCK(leak_lock);
32 #define BUFFER_LRU_MAX 64
37 struct rb_node rb_node;
40 struct extent_page_data {
42 struct extent_io_tree *tree;
43 get_extent_t *get_extent;
45 /* tells writepage not to lock the state bits for this range
46 * it still does the unlocking
48 unsigned int extent_locked:1;
50 /* tells the submit_bio code to use a WRITE_SYNC */
51 unsigned int sync_io:1;
54 int __init extent_io_init(void)
56 extent_state_cache = kmem_cache_create("extent_state",
57 sizeof(struct extent_state), 0,
58 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
59 if (!extent_state_cache)
62 extent_buffer_cache = kmem_cache_create("extent_buffers",
63 sizeof(struct extent_buffer), 0,
64 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
65 if (!extent_buffer_cache)
66 goto free_state_cache;
70 kmem_cache_destroy(extent_state_cache);
74 void extent_io_exit(void)
76 struct extent_state *state;
77 struct extent_buffer *eb;
79 while (!list_empty(&states)) {
80 state = list_entry(states.next, struct extent_state, leak_list);
81 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
82 "state %lu in tree %p refs %d\n",
83 (unsigned long long)state->start,
84 (unsigned long long)state->end,
85 state->state, state->tree, atomic_read(&state->refs));
86 list_del(&state->leak_list);
87 kmem_cache_free(extent_state_cache, state);
91 while (!list_empty(&buffers)) {
92 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
93 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
94 "refs %d\n", (unsigned long long)eb->start,
95 eb->len, atomic_read(&eb->refs));
96 list_del(&eb->leak_list);
97 kmem_cache_free(extent_buffer_cache, eb);
99 if (extent_state_cache)
100 kmem_cache_destroy(extent_state_cache);
101 if (extent_buffer_cache)
102 kmem_cache_destroy(extent_buffer_cache);
105 void extent_io_tree_init(struct extent_io_tree *tree,
106 struct address_space *mapping, gfp_t mask)
108 tree->state = RB_ROOT;
109 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
111 tree->dirty_bytes = 0;
112 spin_lock_init(&tree->lock);
113 spin_lock_init(&tree->buffer_lock);
114 tree->mapping = mapping;
117 static struct extent_state *alloc_extent_state(gfp_t mask)
119 struct extent_state *state;
124 state = kmem_cache_alloc(extent_state_cache, mask);
131 spin_lock_irqsave(&leak_lock, flags);
132 list_add(&state->leak_list, &states);
133 spin_unlock_irqrestore(&leak_lock, flags);
135 atomic_set(&state->refs, 1);
136 init_waitqueue_head(&state->wq);
140 void free_extent_state(struct extent_state *state)
144 if (atomic_dec_and_test(&state->refs)) {
148 WARN_ON(state->tree);
150 spin_lock_irqsave(&leak_lock, flags);
151 list_del(&state->leak_list);
152 spin_unlock_irqrestore(&leak_lock, flags);
154 kmem_cache_free(extent_state_cache, state);
158 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
159 struct rb_node *node)
161 struct rb_node **p = &root->rb_node;
162 struct rb_node *parent = NULL;
163 struct tree_entry *entry;
167 entry = rb_entry(parent, struct tree_entry, rb_node);
169 if (offset < entry->start)
171 else if (offset > entry->end)
177 entry = rb_entry(node, struct tree_entry, rb_node);
178 rb_link_node(node, parent, p);
179 rb_insert_color(node, root);
183 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
184 struct rb_node **prev_ret,
185 struct rb_node **next_ret)
187 struct rb_root *root = &tree->state;
188 struct rb_node *n = root->rb_node;
189 struct rb_node *prev = NULL;
190 struct rb_node *orig_prev = NULL;
191 struct tree_entry *entry;
192 struct tree_entry *prev_entry = NULL;
195 entry = rb_entry(n, struct tree_entry, rb_node);
199 if (offset < entry->start)
201 else if (offset > entry->end)
209 while (prev && offset > prev_entry->end) {
210 prev = rb_next(prev);
211 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
218 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
219 while (prev && offset < prev_entry->start) {
220 prev = rb_prev(prev);
221 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
228 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
231 struct rb_node *prev = NULL;
234 ret = __etree_search(tree, offset, &prev, NULL);
240 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
241 struct extent_state *other)
243 if (tree->ops && tree->ops->merge_extent_hook)
244 tree->ops->merge_extent_hook(tree->mapping->host, new,
249 * utility function to look for merge candidates inside a given range.
250 * Any extents with matching state are merged together into a single
251 * extent in the tree. Extents with EXTENT_IO in their state field
252 * are not merged because the end_io handlers need to be able to do
253 * operations on them without sleeping (or doing allocations/splits).
255 * This should be called with the tree lock held.
257 static int merge_state(struct extent_io_tree *tree,
258 struct extent_state *state)
260 struct extent_state *other;
261 struct rb_node *other_node;
263 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
266 other_node = rb_prev(&state->rb_node);
268 other = rb_entry(other_node, struct extent_state, rb_node);
269 if (other->end == state->start - 1 &&
270 other->state == state->state) {
271 merge_cb(tree, state, other);
272 state->start = other->start;
274 rb_erase(&other->rb_node, &tree->state);
275 free_extent_state(other);
278 other_node = rb_next(&state->rb_node);
280 other = rb_entry(other_node, struct extent_state, rb_node);
281 if (other->start == state->end + 1 &&
282 other->state == state->state) {
283 merge_cb(tree, state, other);
284 other->start = state->start;
286 rb_erase(&state->rb_node, &tree->state);
287 free_extent_state(state);
295 static int set_state_cb(struct extent_io_tree *tree,
296 struct extent_state *state, int *bits)
298 if (tree->ops && tree->ops->set_bit_hook) {
299 return tree->ops->set_bit_hook(tree->mapping->host,
306 static void clear_state_cb(struct extent_io_tree *tree,
307 struct extent_state *state, int *bits)
309 if (tree->ops && tree->ops->clear_bit_hook)
310 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
314 * insert an extent_state struct into the tree. 'bits' are set on the
315 * struct before it is inserted.
317 * This may return -EEXIST if the extent is already there, in which case the
318 * state struct is freed.
320 * The tree lock is not taken internally. This is a utility function and
321 * probably isn't what you want to call (see set/clear_extent_bit).
323 static int insert_state(struct extent_io_tree *tree,
324 struct extent_state *state, u64 start, u64 end,
327 struct rb_node *node;
328 int bits_to_set = *bits & ~EXTENT_CTLBITS;
332 printk(KERN_ERR "btrfs end < start %llu %llu\n",
333 (unsigned long long)end,
334 (unsigned long long)start);
337 state->start = start;
339 ret = set_state_cb(tree, state, bits);
343 if (bits_to_set & EXTENT_DIRTY)
344 tree->dirty_bytes += end - start + 1;
345 state->state |= bits_to_set;
346 node = tree_insert(&tree->state, end, &state->rb_node);
348 struct extent_state *found;
349 found = rb_entry(node, struct extent_state, rb_node);
350 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
351 "%llu %llu\n", (unsigned long long)found->start,
352 (unsigned long long)found->end,
353 (unsigned long long)start, (unsigned long long)end);
354 free_extent_state(state);
358 merge_state(tree, state);
362 static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
365 if (tree->ops && tree->ops->split_extent_hook)
366 return tree->ops->split_extent_hook(tree->mapping->host,
372 * split a given extent state struct in two, inserting the preallocated
373 * struct 'prealloc' as the newly created second half. 'split' indicates an
374 * offset inside 'orig' where it should be split.
377 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
378 * are two extent state structs in the tree:
379 * prealloc: [orig->start, split - 1]
380 * orig: [ split, orig->end ]
382 * The tree locks are not taken by this function. They need to be held
385 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
386 struct extent_state *prealloc, u64 split)
388 struct rb_node *node;
390 split_cb(tree, orig, split);
392 prealloc->start = orig->start;
393 prealloc->end = split - 1;
394 prealloc->state = orig->state;
397 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
399 free_extent_state(prealloc);
402 prealloc->tree = tree;
407 * utility function to clear some bits in an extent state struct.
408 * it will optionally wake up any one waiting on this state (wake == 1), or
409 * forcibly remove the state from the tree (delete == 1).
411 * If no bits are set on the state struct after clearing things, the
412 * struct is freed and removed from the tree
414 static int clear_state_bit(struct extent_io_tree *tree,
415 struct extent_state *state,
418 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
419 int ret = state->state & bits_to_clear;
421 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
422 u64 range = state->end - state->start + 1;
423 WARN_ON(range > tree->dirty_bytes);
424 tree->dirty_bytes -= range;
426 clear_state_cb(tree, state, bits);
427 state->state &= ~bits_to_clear;
430 if (state->state == 0) {
432 rb_erase(&state->rb_node, &tree->state);
434 free_extent_state(state);
439 merge_state(tree, state);
445 * clear some bits on a range in the tree. This may require splitting
446 * or inserting elements in the tree, so the gfp mask is used to
447 * indicate which allocations or sleeping are allowed.
449 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
450 * the given range from the tree regardless of state (ie for truncate).
452 * the range [start, end] is inclusive.
454 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
455 * bits were already set, or zero if none of the bits were already set.
457 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
458 int bits, int wake, int delete,
459 struct extent_state **cached_state,
462 struct extent_state *state;
463 struct extent_state *cached;
464 struct extent_state *prealloc = NULL;
465 struct rb_node *next_node;
466 struct rb_node *node;
473 bits |= ~EXTENT_CTLBITS;
474 bits |= EXTENT_FIRST_DELALLOC;
476 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
479 if (!prealloc && (mask & __GFP_WAIT)) {
480 prealloc = alloc_extent_state(mask);
485 spin_lock(&tree->lock);
487 cached = *cached_state;
490 *cached_state = NULL;
494 if (cached && cached->tree && cached->start == start) {
496 atomic_dec(&cached->refs);
501 free_extent_state(cached);
504 * this search will find the extents that end after
507 node = tree_search(tree, start);
510 state = rb_entry(node, struct extent_state, rb_node);
512 if (state->start > end)
514 WARN_ON(state->end < start);
515 last_end = state->end;
518 * | ---- desired range ---- |
520 * | ------------- state -------------- |
522 * We need to split the extent we found, and may flip
523 * bits on second half.
525 * If the extent we found extends past our range, we
526 * just split and search again. It'll get split again
527 * the next time though.
529 * If the extent we found is inside our range, we clear
530 * the desired bit on it.
533 if (state->start < start) {
535 prealloc = alloc_extent_state(GFP_ATOMIC);
536 err = split_state(tree, state, prealloc, start);
537 BUG_ON(err == -EEXIST);
541 if (state->end <= end) {
542 set |= clear_state_bit(tree, state, &bits, wake);
543 if (last_end == (u64)-1)
545 start = last_end + 1;
550 * | ---- desired range ---- |
552 * We need to split the extent, and clear the bit
555 if (state->start <= end && state->end > end) {
557 prealloc = alloc_extent_state(GFP_ATOMIC);
558 err = split_state(tree, state, prealloc, end + 1);
559 BUG_ON(err == -EEXIST);
563 set |= clear_state_bit(tree, prealloc, &bits, wake);
569 if (state->end < end && prealloc && !need_resched())
570 next_node = rb_next(&state->rb_node);
574 set |= clear_state_bit(tree, state, &bits, wake);
575 if (last_end == (u64)-1)
577 start = last_end + 1;
578 if (start <= end && next_node) {
579 state = rb_entry(next_node, struct extent_state,
581 if (state->start == start)
587 spin_unlock(&tree->lock);
589 free_extent_state(prealloc);
596 spin_unlock(&tree->lock);
597 if (mask & __GFP_WAIT)
602 static int wait_on_state(struct extent_io_tree *tree,
603 struct extent_state *state)
604 __releases(tree->lock)
605 __acquires(tree->lock)
608 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
609 spin_unlock(&tree->lock);
611 spin_lock(&tree->lock);
612 finish_wait(&state->wq, &wait);
617 * waits for one or more bits to clear on a range in the state tree.
618 * The range [start, end] is inclusive.
619 * The tree lock is taken by this function
621 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
623 struct extent_state *state;
624 struct rb_node *node;
626 spin_lock(&tree->lock);
630 * this search will find all the extents that end after
633 node = tree_search(tree, start);
637 state = rb_entry(node, struct extent_state, rb_node);
639 if (state->start > end)
642 if (state->state & bits) {
643 start = state->start;
644 atomic_inc(&state->refs);
645 wait_on_state(tree, state);
646 free_extent_state(state);
649 start = state->end + 1;
654 if (need_resched()) {
655 spin_unlock(&tree->lock);
657 spin_lock(&tree->lock);
661 spin_unlock(&tree->lock);
665 static int set_state_bits(struct extent_io_tree *tree,
666 struct extent_state *state,
670 int bits_to_set = *bits & ~EXTENT_CTLBITS;
672 ret = set_state_cb(tree, state, bits);
675 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
676 u64 range = state->end - state->start + 1;
677 tree->dirty_bytes += range;
679 state->state |= bits_to_set;
684 static void cache_state(struct extent_state *state,
685 struct extent_state **cached_ptr)
687 if (cached_ptr && !(*cached_ptr)) {
688 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
690 atomic_inc(&state->refs);
695 static void uncache_state(struct extent_state **cached_ptr)
697 if (cached_ptr && (*cached_ptr)) {
698 struct extent_state *state = *cached_ptr;
700 free_extent_state(state);
705 * set some bits on a range in the tree. This may require allocations or
706 * sleeping, so the gfp mask is used to indicate what is allowed.
708 * If any of the exclusive bits are set, this will fail with -EEXIST if some
709 * part of the range already has the desired bits set. The start of the
710 * existing range is returned in failed_start in this case.
712 * [start, end] is inclusive This takes the tree lock.
715 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
716 int bits, int exclusive_bits, u64 *failed_start,
717 struct extent_state **cached_state, gfp_t mask)
719 struct extent_state *state;
720 struct extent_state *prealloc = NULL;
721 struct rb_node *node;
726 bits |= EXTENT_FIRST_DELALLOC;
728 if (!prealloc && (mask & __GFP_WAIT)) {
729 prealloc = alloc_extent_state(mask);
734 spin_lock(&tree->lock);
735 if (cached_state && *cached_state) {
736 state = *cached_state;
737 if (state->start == start && state->tree) {
738 node = &state->rb_node;
743 * this search will find all the extents that end after
746 node = tree_search(tree, start);
748 err = insert_state(tree, prealloc, start, end, &bits);
750 BUG_ON(err == -EEXIST);
753 state = rb_entry(node, struct extent_state, rb_node);
755 last_start = state->start;
756 last_end = state->end;
759 * | ---- desired range ---- |
762 * Just lock what we found and keep going
764 if (state->start == start && state->end <= end) {
765 struct rb_node *next_node;
766 if (state->state & exclusive_bits) {
767 *failed_start = state->start;
772 err = set_state_bits(tree, state, &bits);
776 cache_state(state, cached_state);
777 merge_state(tree, state);
778 if (last_end == (u64)-1)
781 start = last_end + 1;
782 if (start < end && prealloc && !need_resched()) {
783 next_node = rb_next(node);
785 state = rb_entry(next_node, struct extent_state,
787 if (state->start == start)
795 * | ---- desired range ---- |
798 * | ------------- state -------------- |
800 * We need to split the extent we found, and may flip bits on
803 * If the extent we found extends past our
804 * range, we just split and search again. It'll get split
805 * again the next time though.
807 * If the extent we found is inside our range, we set the
810 if (state->start < start) {
811 if (state->state & exclusive_bits) {
812 *failed_start = start;
816 err = split_state(tree, state, prealloc, start);
817 BUG_ON(err == -EEXIST);
821 if (state->end <= end) {
822 err = set_state_bits(tree, state, &bits);
825 cache_state(state, cached_state);
826 merge_state(tree, state);
827 if (last_end == (u64)-1)
829 start = last_end + 1;
834 * | ---- desired range ---- |
835 * | state | or | state |
837 * There's a hole, we need to insert something in it and
838 * ignore the extent we found.
840 if (state->start > start) {
842 if (end < last_start)
845 this_end = last_start - 1;
846 err = insert_state(tree, prealloc, start, this_end,
848 BUG_ON(err == -EEXIST);
853 cache_state(prealloc, cached_state);
855 start = this_end + 1;
859 * | ---- desired range ---- |
861 * We need to split the extent, and set the bit
864 if (state->start <= end && state->end > end) {
865 if (state->state & exclusive_bits) {
866 *failed_start = start;
870 err = split_state(tree, state, prealloc, end + 1);
871 BUG_ON(err == -EEXIST);
873 err = set_state_bits(tree, prealloc, &bits);
878 cache_state(prealloc, cached_state);
879 merge_state(tree, prealloc);
887 spin_unlock(&tree->lock);
889 free_extent_state(prealloc);
896 spin_unlock(&tree->lock);
897 if (mask & __GFP_WAIT)
902 /* wrappers around set/clear extent bit */
903 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
906 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
910 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
911 int bits, gfp_t mask)
913 return set_extent_bit(tree, start, end, bits, 0, NULL,
917 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
918 int bits, gfp_t mask)
920 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
923 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
924 struct extent_state **cached_state, gfp_t mask)
926 return set_extent_bit(tree, start, end,
927 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
928 0, NULL, cached_state, mask);
931 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
934 return clear_extent_bit(tree, start, end,
935 EXTENT_DIRTY | EXTENT_DELALLOC |
936 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
939 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
942 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
946 static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
949 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
953 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
954 struct extent_state **cached_state, gfp_t mask)
956 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
957 NULL, cached_state, mask);
960 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
961 u64 end, struct extent_state **cached_state,
964 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
968 int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
970 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
974 * either insert or lock state struct between start and end use mask to tell
975 * us if waiting is desired.
977 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
978 int bits, struct extent_state **cached_state, gfp_t mask)
983 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
984 EXTENT_LOCKED, &failed_start,
986 if (err == -EEXIST && (mask & __GFP_WAIT)) {
987 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
988 start = failed_start;
992 WARN_ON(start > end);
997 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
999 return lock_extent_bits(tree, start, end, 0, NULL, mask);
1002 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1008 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1009 &failed_start, NULL, mask);
1010 if (err == -EEXIST) {
1011 if (failed_start > start)
1012 clear_extent_bit(tree, start, failed_start - 1,
1013 EXTENT_LOCKED, 1, 0, NULL, mask);
1019 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1020 struct extent_state **cached, gfp_t mask)
1022 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1026 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1028 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1033 * helper function to set pages and extents in the tree dirty
1035 int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1037 unsigned long index = start >> PAGE_CACHE_SHIFT;
1038 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1041 while (index <= end_index) {
1042 page = find_get_page(tree->mapping, index);
1044 __set_page_dirty_nobuffers(page);
1045 page_cache_release(page);
1052 * helper function to set both pages and extents in the tree writeback
1054 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1056 unsigned long index = start >> PAGE_CACHE_SHIFT;
1057 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1060 while (index <= end_index) {
1061 page = find_get_page(tree->mapping, index);
1063 set_page_writeback(page);
1064 page_cache_release(page);
1071 * find the first offset in the io tree with 'bits' set. zero is
1072 * returned if we find something, and *start_ret and *end_ret are
1073 * set to reflect the state struct that was found.
1075 * If nothing was found, 1 is returned, < 0 on error
1077 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1078 u64 *start_ret, u64 *end_ret, int bits)
1080 struct rb_node *node;
1081 struct extent_state *state;
1084 spin_lock(&tree->lock);
1086 * this search will find all the extents that end after
1089 node = tree_search(tree, start);
1094 state = rb_entry(node, struct extent_state, rb_node);
1095 if (state->end >= start && (state->state & bits)) {
1096 *start_ret = state->start;
1097 *end_ret = state->end;
1101 node = rb_next(node);
1106 spin_unlock(&tree->lock);
1110 /* find the first state struct with 'bits' set after 'start', and
1111 * return it. tree->lock must be held. NULL will returned if
1112 * nothing was found after 'start'
1114 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1115 u64 start, int bits)
1117 struct rb_node *node;
1118 struct extent_state *state;
1121 * this search will find all the extents that end after
1124 node = tree_search(tree, start);
1129 state = rb_entry(node, struct extent_state, rb_node);
1130 if (state->end >= start && (state->state & bits))
1133 node = rb_next(node);
1142 * find a contiguous range of bytes in the file marked as delalloc, not
1143 * more than 'max_bytes'. start and end are used to return the range,
1145 * 1 is returned if we find something, 0 if nothing was in the tree
1147 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1148 u64 *start, u64 *end, u64 max_bytes,
1149 struct extent_state **cached_state)
1151 struct rb_node *node;
1152 struct extent_state *state;
1153 u64 cur_start = *start;
1155 u64 total_bytes = 0;
1157 spin_lock(&tree->lock);
1160 * this search will find all the extents that end after
1163 node = tree_search(tree, cur_start);
1171 state = rb_entry(node, struct extent_state, rb_node);
1172 if (found && (state->start != cur_start ||
1173 (state->state & EXTENT_BOUNDARY))) {
1176 if (!(state->state & EXTENT_DELALLOC)) {
1182 *start = state->start;
1183 *cached_state = state;
1184 atomic_inc(&state->refs);
1188 cur_start = state->end + 1;
1189 node = rb_next(node);
1192 total_bytes += state->end - state->start + 1;
1193 if (total_bytes >= max_bytes)
1197 spin_unlock(&tree->lock);
1201 static noinline int __unlock_for_delalloc(struct inode *inode,
1202 struct page *locked_page,
1206 struct page *pages[16];
1207 unsigned long index = start >> PAGE_CACHE_SHIFT;
1208 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1209 unsigned long nr_pages = end_index - index + 1;
1212 if (index == locked_page->index && end_index == index)
1215 while (nr_pages > 0) {
1216 ret = find_get_pages_contig(inode->i_mapping, index,
1217 min_t(unsigned long, nr_pages,
1218 ARRAY_SIZE(pages)), pages);
1219 for (i = 0; i < ret; i++) {
1220 if (pages[i] != locked_page)
1221 unlock_page(pages[i]);
1222 page_cache_release(pages[i]);
1231 static noinline int lock_delalloc_pages(struct inode *inode,
1232 struct page *locked_page,
1236 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1237 unsigned long start_index = index;
1238 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1239 unsigned long pages_locked = 0;
1240 struct page *pages[16];
1241 unsigned long nrpages;
1245 /* the caller is responsible for locking the start index */
1246 if (index == locked_page->index && index == end_index)
1249 /* skip the page at the start index */
1250 nrpages = end_index - index + 1;
1251 while (nrpages > 0) {
1252 ret = find_get_pages_contig(inode->i_mapping, index,
1253 min_t(unsigned long,
1254 nrpages, ARRAY_SIZE(pages)), pages);
1259 /* now we have an array of pages, lock them all */
1260 for (i = 0; i < ret; i++) {
1262 * the caller is taking responsibility for
1265 if (pages[i] != locked_page) {
1266 lock_page(pages[i]);
1267 if (!PageDirty(pages[i]) ||
1268 pages[i]->mapping != inode->i_mapping) {
1270 unlock_page(pages[i]);
1271 page_cache_release(pages[i]);
1275 page_cache_release(pages[i]);
1284 if (ret && pages_locked) {
1285 __unlock_for_delalloc(inode, locked_page,
1287 ((u64)(start_index + pages_locked - 1)) <<
1294 * find a contiguous range of bytes in the file marked as delalloc, not
1295 * more than 'max_bytes'. start and end are used to return the range,
1297 * 1 is returned if we find something, 0 if nothing was in the tree
1299 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1300 struct extent_io_tree *tree,
1301 struct page *locked_page,
1302 u64 *start, u64 *end,
1308 struct extent_state *cached_state = NULL;
1313 /* step one, find a bunch of delalloc bytes starting at start */
1314 delalloc_start = *start;
1316 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1317 max_bytes, &cached_state);
1318 if (!found || delalloc_end <= *start) {
1319 *start = delalloc_start;
1320 *end = delalloc_end;
1321 free_extent_state(cached_state);
1326 * start comes from the offset of locked_page. We have to lock
1327 * pages in order, so we can't process delalloc bytes before
1330 if (delalloc_start < *start)
1331 delalloc_start = *start;
1334 * make sure to limit the number of pages we try to lock down
1337 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1338 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1340 /* step two, lock all the pages after the page that has start */
1341 ret = lock_delalloc_pages(inode, locked_page,
1342 delalloc_start, delalloc_end);
1343 if (ret == -EAGAIN) {
1344 /* some of the pages are gone, lets avoid looping by
1345 * shortening the size of the delalloc range we're searching
1347 free_extent_state(cached_state);
1349 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1350 max_bytes = PAGE_CACHE_SIZE - offset;
1360 /* step three, lock the state bits for the whole range */
1361 lock_extent_bits(tree, delalloc_start, delalloc_end,
1362 0, &cached_state, GFP_NOFS);
1364 /* then test to make sure it is all still delalloc */
1365 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1366 EXTENT_DELALLOC, 1, cached_state);
1368 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1369 &cached_state, GFP_NOFS);
1370 __unlock_for_delalloc(inode, locked_page,
1371 delalloc_start, delalloc_end);
1375 free_extent_state(cached_state);
1376 *start = delalloc_start;
1377 *end = delalloc_end;
1382 int extent_clear_unlock_delalloc(struct inode *inode,
1383 struct extent_io_tree *tree,
1384 u64 start, u64 end, struct page *locked_page,
1388 struct page *pages[16];
1389 unsigned long index = start >> PAGE_CACHE_SHIFT;
1390 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1391 unsigned long nr_pages = end_index - index + 1;
1395 if (op & EXTENT_CLEAR_UNLOCK)
1396 clear_bits |= EXTENT_LOCKED;
1397 if (op & EXTENT_CLEAR_DIRTY)
1398 clear_bits |= EXTENT_DIRTY;
1400 if (op & EXTENT_CLEAR_DELALLOC)
1401 clear_bits |= EXTENT_DELALLOC;
1403 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1404 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1405 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1406 EXTENT_SET_PRIVATE2)))
1409 while (nr_pages > 0) {
1410 ret = find_get_pages_contig(inode->i_mapping, index,
1411 min_t(unsigned long,
1412 nr_pages, ARRAY_SIZE(pages)), pages);
1413 for (i = 0; i < ret; i++) {
1415 if (op & EXTENT_SET_PRIVATE2)
1416 SetPagePrivate2(pages[i]);
1418 if (pages[i] == locked_page) {
1419 page_cache_release(pages[i]);
1422 if (op & EXTENT_CLEAR_DIRTY)
1423 clear_page_dirty_for_io(pages[i]);
1424 if (op & EXTENT_SET_WRITEBACK)
1425 set_page_writeback(pages[i]);
1426 if (op & EXTENT_END_WRITEBACK)
1427 end_page_writeback(pages[i]);
1428 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1429 unlock_page(pages[i]);
1430 page_cache_release(pages[i]);
1440 * count the number of bytes in the tree that have a given bit(s)
1441 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1442 * cached. The total number found is returned.
1444 u64 count_range_bits(struct extent_io_tree *tree,
1445 u64 *start, u64 search_end, u64 max_bytes,
1446 unsigned long bits, int contig)
1448 struct rb_node *node;
1449 struct extent_state *state;
1450 u64 cur_start = *start;
1451 u64 total_bytes = 0;
1455 if (search_end <= cur_start) {
1460 spin_lock(&tree->lock);
1461 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1462 total_bytes = tree->dirty_bytes;
1466 * this search will find all the extents that end after
1469 node = tree_search(tree, cur_start);
1474 state = rb_entry(node, struct extent_state, rb_node);
1475 if (state->start > search_end)
1477 if (contig && found && state->start > last + 1)
1479 if (state->end >= cur_start && (state->state & bits) == bits) {
1480 total_bytes += min(search_end, state->end) + 1 -
1481 max(cur_start, state->start);
1482 if (total_bytes >= max_bytes)
1485 *start = state->start;
1489 } else if (contig && found) {
1492 node = rb_next(node);
1497 spin_unlock(&tree->lock);
1502 * set the private field for a given byte offset in the tree. If there isn't
1503 * an extent_state there already, this does nothing.
1505 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1507 struct rb_node *node;
1508 struct extent_state *state;
1511 spin_lock(&tree->lock);
1513 * this search will find all the extents that end after
1516 node = tree_search(tree, start);
1521 state = rb_entry(node, struct extent_state, rb_node);
1522 if (state->start != start) {
1526 state->private = private;
1528 spin_unlock(&tree->lock);
1532 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1534 struct rb_node *node;
1535 struct extent_state *state;
1538 spin_lock(&tree->lock);
1540 * this search will find all the extents that end after
1543 node = tree_search(tree, start);
1548 state = rb_entry(node, struct extent_state, rb_node);
1549 if (state->start != start) {
1553 *private = state->private;
1555 spin_unlock(&tree->lock);
1560 * searches a range in the state tree for a given mask.
1561 * If 'filled' == 1, this returns 1 only if every extent in the tree
1562 * has the bits set. Otherwise, 1 is returned if any bit in the
1563 * range is found set.
1565 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1566 int bits, int filled, struct extent_state *cached)
1568 struct extent_state *state = NULL;
1569 struct rb_node *node;
1572 spin_lock(&tree->lock);
1573 if (cached && cached->tree && cached->start == start)
1574 node = &cached->rb_node;
1576 node = tree_search(tree, start);
1577 while (node && start <= end) {
1578 state = rb_entry(node, struct extent_state, rb_node);
1580 if (filled && state->start > start) {
1585 if (state->start > end)
1588 if (state->state & bits) {
1592 } else if (filled) {
1597 if (state->end == (u64)-1)
1600 start = state->end + 1;
1603 node = rb_next(node);
1610 spin_unlock(&tree->lock);
1615 * helper function to set a given page up to date if all the
1616 * extents in the tree for that page are up to date
1618 static int check_page_uptodate(struct extent_io_tree *tree,
1621 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1622 u64 end = start + PAGE_CACHE_SIZE - 1;
1623 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1624 SetPageUptodate(page);
1629 * helper function to unlock a page if all the extents in the tree
1630 * for that page are unlocked
1632 static int check_page_locked(struct extent_io_tree *tree,
1635 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1636 u64 end = start + PAGE_CACHE_SIZE - 1;
1637 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1643 * helper function to end page writeback if all the extents
1644 * in the tree for that page are done with writeback
1646 static int check_page_writeback(struct extent_io_tree *tree,
1649 end_page_writeback(page);
1653 /* lots and lots of room for performance fixes in the end_bio funcs */
1656 * after a writepage IO is done, we need to:
1657 * clear the uptodate bits on error
1658 * clear the writeback bits in the extent tree for this IO
1659 * end_page_writeback if the page has no more pending IO
1661 * Scheduling is not allowed, so the extent state tree is expected
1662 * to have one and only one object corresponding to this IO.
1664 static void end_bio_extent_writepage(struct bio *bio, int err)
1666 int uptodate = err == 0;
1667 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1668 struct extent_io_tree *tree;
1675 struct page *page = bvec->bv_page;
1676 tree = &BTRFS_I(page->mapping->host)->io_tree;
1678 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1680 end = start + bvec->bv_len - 1;
1682 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1687 if (--bvec >= bio->bi_io_vec)
1688 prefetchw(&bvec->bv_page->flags);
1689 if (tree->ops && tree->ops->writepage_end_io_hook) {
1690 ret = tree->ops->writepage_end_io_hook(page, start,
1691 end, NULL, uptodate);
1696 if (!uptodate && tree->ops &&
1697 tree->ops->writepage_io_failed_hook) {
1698 ret = tree->ops->writepage_io_failed_hook(bio, page,
1701 uptodate = (err == 0);
1707 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
1708 ClearPageUptodate(page);
1713 end_page_writeback(page);
1715 check_page_writeback(tree, page);
1716 } while (bvec >= bio->bi_io_vec);
1722 * after a readpage IO is done, we need to:
1723 * clear the uptodate bits on error
1724 * set the uptodate bits if things worked
1725 * set the page up to date if all extents in the tree are uptodate
1726 * clear the lock bit in the extent tree
1727 * unlock the page if there are no other extents locked for it
1729 * Scheduling is not allowed, so the extent state tree is expected
1730 * to have one and only one object corresponding to this IO.
1732 static void end_bio_extent_readpage(struct bio *bio, int err)
1734 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1735 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1736 struct bio_vec *bvec = bio->bi_io_vec;
1737 struct extent_io_tree *tree;
1747 struct page *page = bvec->bv_page;
1748 struct extent_state *cached = NULL;
1749 struct extent_state *state;
1751 tree = &BTRFS_I(page->mapping->host)->io_tree;
1753 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1755 end = start + bvec->bv_len - 1;
1757 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1762 if (++bvec <= bvec_end)
1763 prefetchw(&bvec->bv_page->flags);
1765 spin_lock(&tree->lock);
1766 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
1767 if (state && state->start == start) {
1769 * take a reference on the state, unlock will drop
1772 cache_state(state, &cached);
1774 spin_unlock(&tree->lock);
1776 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1777 ret = tree->ops->readpage_end_io_hook(page, start, end,
1782 if (!uptodate && tree->ops &&
1783 tree->ops->readpage_io_failed_hook) {
1784 ret = tree->ops->readpage_io_failed_hook(bio, page,
1788 test_bit(BIO_UPTODATE, &bio->bi_flags);
1791 uncache_state(&cached);
1797 set_extent_uptodate(tree, start, end, &cached,
1800 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
1804 SetPageUptodate(page);
1806 ClearPageUptodate(page);
1812 check_page_uptodate(tree, page);
1814 ClearPageUptodate(page);
1817 check_page_locked(tree, page);
1819 } while (bvec <= bvec_end);
1825 * IO done from prepare_write is pretty simple, we just unlock
1826 * the structs in the extent tree when done, and set the uptodate bits
1829 static void end_bio_extent_preparewrite(struct bio *bio, int err)
1831 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1832 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1833 struct extent_io_tree *tree;
1838 struct page *page = bvec->bv_page;
1839 struct extent_state *cached = NULL;
1840 tree = &BTRFS_I(page->mapping->host)->io_tree;
1842 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1844 end = start + bvec->bv_len - 1;
1846 if (--bvec >= bio->bi_io_vec)
1847 prefetchw(&bvec->bv_page->flags);
1850 set_extent_uptodate(tree, start, end, &cached,
1853 ClearPageUptodate(page);
1857 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
1859 } while (bvec >= bio->bi_io_vec);
1865 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1870 bio = bio_alloc(gfp_flags, nr_vecs);
1872 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1873 while (!bio && (nr_vecs /= 2))
1874 bio = bio_alloc(gfp_flags, nr_vecs);
1879 bio->bi_bdev = bdev;
1880 bio->bi_sector = first_sector;
1885 static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1886 unsigned long bio_flags)
1889 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1890 struct page *page = bvec->bv_page;
1891 struct extent_io_tree *tree = bio->bi_private;
1894 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1896 bio->bi_private = NULL;
1900 if (tree->ops && tree->ops->submit_bio_hook)
1901 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1902 mirror_num, bio_flags, start);
1904 submit_bio(rw, bio);
1905 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1911 static int submit_extent_page(int rw, struct extent_io_tree *tree,
1912 struct page *page, sector_t sector,
1913 size_t size, unsigned long offset,
1914 struct block_device *bdev,
1915 struct bio **bio_ret,
1916 unsigned long max_pages,
1917 bio_end_io_t end_io_func,
1919 unsigned long prev_bio_flags,
1920 unsigned long bio_flags)
1926 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1927 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
1928 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
1930 if (bio_ret && *bio_ret) {
1933 contig = bio->bi_sector == sector;
1935 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1938 if (prev_bio_flags != bio_flags || !contig ||
1939 (tree->ops && tree->ops->merge_bio_hook &&
1940 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1942 bio_add_page(bio, page, page_size, offset) < page_size) {
1943 ret = submit_one_bio(rw, bio, mirror_num,
1950 if (this_compressed)
1953 nr = bio_get_nr_vecs(bdev);
1955 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1959 bio_add_page(bio, page, page_size, offset);
1960 bio->bi_end_io = end_io_func;
1961 bio->bi_private = tree;
1966 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
1971 void set_page_extent_mapped(struct page *page)
1973 if (!PagePrivate(page)) {
1974 SetPagePrivate(page);
1975 page_cache_get(page);
1976 set_page_private(page, EXTENT_PAGE_PRIVATE);
1980 static void set_page_extent_head(struct page *page, unsigned long len)
1982 WARN_ON(!PagePrivate(page));
1983 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1987 * basic readpage implementation. Locked extent state structs are inserted
1988 * into the tree that are removed when the IO is done (by the end_io
1991 static int __extent_read_full_page(struct extent_io_tree *tree,
1993 get_extent_t *get_extent,
1994 struct bio **bio, int mirror_num,
1995 unsigned long *bio_flags)
1997 struct inode *inode = page->mapping->host;
1998 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1999 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2003 u64 last_byte = i_size_read(inode);
2007 struct extent_map *em;
2008 struct block_device *bdev;
2009 struct btrfs_ordered_extent *ordered;
2012 size_t page_offset = 0;
2014 size_t disk_io_size;
2015 size_t blocksize = inode->i_sb->s_blocksize;
2016 unsigned long this_bio_flag = 0;
2018 set_page_extent_mapped(page);
2020 if (!PageUptodate(page)) {
2021 if (cleancache_get_page(page) == 0) {
2022 BUG_ON(blocksize != PAGE_SIZE);
2029 lock_extent(tree, start, end, GFP_NOFS);
2030 ordered = btrfs_lookup_ordered_extent(inode, start);
2033 unlock_extent(tree, start, end, GFP_NOFS);
2034 btrfs_start_ordered_extent(inode, ordered, 1);
2035 btrfs_put_ordered_extent(ordered);
2038 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2040 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2043 iosize = PAGE_CACHE_SIZE - zero_offset;
2044 userpage = kmap_atomic(page, KM_USER0);
2045 memset(userpage + zero_offset, 0, iosize);
2046 flush_dcache_page(page);
2047 kunmap_atomic(userpage, KM_USER0);
2050 while (cur <= end) {
2051 if (cur >= last_byte) {
2053 struct extent_state *cached = NULL;
2055 iosize = PAGE_CACHE_SIZE - page_offset;
2056 userpage = kmap_atomic(page, KM_USER0);
2057 memset(userpage + page_offset, 0, iosize);
2058 flush_dcache_page(page);
2059 kunmap_atomic(userpage, KM_USER0);
2060 set_extent_uptodate(tree, cur, cur + iosize - 1,
2062 unlock_extent_cached(tree, cur, cur + iosize - 1,
2066 em = get_extent(inode, page, page_offset, cur,
2068 if (IS_ERR(em) || !em) {
2070 unlock_extent(tree, cur, end, GFP_NOFS);
2073 extent_offset = cur - em->start;
2074 BUG_ON(extent_map_end(em) <= cur);
2077 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2078 this_bio_flag = EXTENT_BIO_COMPRESSED;
2079 extent_set_compress_type(&this_bio_flag,
2083 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2084 cur_end = min(extent_map_end(em) - 1, end);
2085 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2086 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2087 disk_io_size = em->block_len;
2088 sector = em->block_start >> 9;
2090 sector = (em->block_start + extent_offset) >> 9;
2091 disk_io_size = iosize;
2094 block_start = em->block_start;
2095 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2096 block_start = EXTENT_MAP_HOLE;
2097 free_extent_map(em);
2100 /* we've found a hole, just zero and go on */
2101 if (block_start == EXTENT_MAP_HOLE) {
2103 struct extent_state *cached = NULL;
2105 userpage = kmap_atomic(page, KM_USER0);
2106 memset(userpage + page_offset, 0, iosize);
2107 flush_dcache_page(page);
2108 kunmap_atomic(userpage, KM_USER0);
2110 set_extent_uptodate(tree, cur, cur + iosize - 1,
2112 unlock_extent_cached(tree, cur, cur + iosize - 1,
2115 page_offset += iosize;
2118 /* the get_extent function already copied into the page */
2119 if (test_range_bit(tree, cur, cur_end,
2120 EXTENT_UPTODATE, 1, NULL)) {
2121 check_page_uptodate(tree, page);
2122 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2124 page_offset += iosize;
2127 /* we have an inline extent but it didn't get marked up
2128 * to date. Error out
2130 if (block_start == EXTENT_MAP_INLINE) {
2132 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2134 page_offset += iosize;
2139 if (tree->ops && tree->ops->readpage_io_hook) {
2140 ret = tree->ops->readpage_io_hook(page, cur,
2144 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2146 ret = submit_extent_page(READ, tree, page,
2147 sector, disk_io_size, page_offset,
2149 end_bio_extent_readpage, mirror_num,
2153 *bio_flags = this_bio_flag;
2158 page_offset += iosize;
2162 if (!PageError(page))
2163 SetPageUptodate(page);
2169 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2170 get_extent_t *get_extent)
2172 struct bio *bio = NULL;
2173 unsigned long bio_flags = 0;
2176 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2179 ret = submit_one_bio(READ, bio, 0, bio_flags);
2183 static noinline void update_nr_written(struct page *page,
2184 struct writeback_control *wbc,
2185 unsigned long nr_written)
2187 wbc->nr_to_write -= nr_written;
2188 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2189 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2190 page->mapping->writeback_index = page->index + nr_written;
2194 * the writepage semantics are similar to regular writepage. extent
2195 * records are inserted to lock ranges in the tree, and as dirty areas
2196 * are found, they are marked writeback. Then the lock bits are removed
2197 * and the end_io handler clears the writeback ranges
2199 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2202 struct inode *inode = page->mapping->host;
2203 struct extent_page_data *epd = data;
2204 struct extent_io_tree *tree = epd->tree;
2205 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2207 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2211 u64 last_byte = i_size_read(inode);
2215 struct extent_state *cached_state = NULL;
2216 struct extent_map *em;
2217 struct block_device *bdev;
2220 size_t pg_offset = 0;
2222 loff_t i_size = i_size_read(inode);
2223 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2229 unsigned long nr_written = 0;
2231 if (wbc->sync_mode == WB_SYNC_ALL)
2232 write_flags = WRITE_SYNC;
2234 write_flags = WRITE;
2236 trace___extent_writepage(page, inode, wbc);
2238 WARN_ON(!PageLocked(page));
2239 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2240 if (page->index > end_index ||
2241 (page->index == end_index && !pg_offset)) {
2242 page->mapping->a_ops->invalidatepage(page, 0);
2247 if (page->index == end_index) {
2250 userpage = kmap_atomic(page, KM_USER0);
2251 memset(userpage + pg_offset, 0,
2252 PAGE_CACHE_SIZE - pg_offset);
2253 kunmap_atomic(userpage, KM_USER0);
2254 flush_dcache_page(page);
2258 set_page_extent_mapped(page);
2260 delalloc_start = start;
2263 if (!epd->extent_locked) {
2264 u64 delalloc_to_write = 0;
2266 * make sure the wbc mapping index is at least updated
2269 update_nr_written(page, wbc, 0);
2271 while (delalloc_end < page_end) {
2272 nr_delalloc = find_lock_delalloc_range(inode, tree,
2277 if (nr_delalloc == 0) {
2278 delalloc_start = delalloc_end + 1;
2281 tree->ops->fill_delalloc(inode, page, delalloc_start,
2282 delalloc_end, &page_started,
2285 * delalloc_end is already one less than the total
2286 * length, so we don't subtract one from
2289 delalloc_to_write += (delalloc_end - delalloc_start +
2292 delalloc_start = delalloc_end + 1;
2294 if (wbc->nr_to_write < delalloc_to_write) {
2297 if (delalloc_to_write < thresh * 2)
2298 thresh = delalloc_to_write;
2299 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2303 /* did the fill delalloc function already unlock and start
2309 * we've unlocked the page, so we can't update
2310 * the mapping's writeback index, just update
2313 wbc->nr_to_write -= nr_written;
2317 if (tree->ops && tree->ops->writepage_start_hook) {
2318 ret = tree->ops->writepage_start_hook(page, start,
2320 if (ret == -EAGAIN) {
2321 redirty_page_for_writepage(wbc, page);
2322 update_nr_written(page, wbc, nr_written);
2330 * we don't want to touch the inode after unlocking the page,
2331 * so we update the mapping writeback index now
2333 update_nr_written(page, wbc, nr_written + 1);
2336 if (last_byte <= start) {
2337 if (tree->ops && tree->ops->writepage_end_io_hook)
2338 tree->ops->writepage_end_io_hook(page, start,
2343 blocksize = inode->i_sb->s_blocksize;
2345 while (cur <= end) {
2346 if (cur >= last_byte) {
2347 if (tree->ops && tree->ops->writepage_end_io_hook)
2348 tree->ops->writepage_end_io_hook(page, cur,
2352 em = epd->get_extent(inode, page, pg_offset, cur,
2354 if (IS_ERR(em) || !em) {
2359 extent_offset = cur - em->start;
2360 BUG_ON(extent_map_end(em) <= cur);
2362 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2363 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2364 sector = (em->block_start + extent_offset) >> 9;
2366 block_start = em->block_start;
2367 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2368 free_extent_map(em);
2372 * compressed and inline extents are written through other
2375 if (compressed || block_start == EXTENT_MAP_HOLE ||
2376 block_start == EXTENT_MAP_INLINE) {
2378 * end_io notification does not happen here for
2379 * compressed extents
2381 if (!compressed && tree->ops &&
2382 tree->ops->writepage_end_io_hook)
2383 tree->ops->writepage_end_io_hook(page, cur,
2386 else if (compressed) {
2387 /* we don't want to end_page_writeback on
2388 * a compressed extent. this happens
2395 pg_offset += iosize;
2398 /* leave this out until we have a page_mkwrite call */
2399 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2400 EXTENT_DIRTY, 0, NULL)) {
2402 pg_offset += iosize;
2406 if (tree->ops && tree->ops->writepage_io_hook) {
2407 ret = tree->ops->writepage_io_hook(page, cur,
2415 unsigned long max_nr = end_index + 1;
2417 set_range_writeback(tree, cur, cur + iosize - 1);
2418 if (!PageWriteback(page)) {
2419 printk(KERN_ERR "btrfs warning page %lu not "
2420 "writeback, cur %llu end %llu\n",
2421 page->index, (unsigned long long)cur,
2422 (unsigned long long)end);
2425 ret = submit_extent_page(write_flags, tree, page,
2426 sector, iosize, pg_offset,
2427 bdev, &epd->bio, max_nr,
2428 end_bio_extent_writepage,
2434 pg_offset += iosize;
2439 /* make sure the mapping tag for page dirty gets cleared */
2440 set_page_writeback(page);
2441 end_page_writeback(page);
2447 /* drop our reference on any cached states */
2448 free_extent_state(cached_state);
2453 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2454 * @mapping: address space structure to write
2455 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2456 * @writepage: function called for each page
2457 * @data: data passed to writepage function
2459 * If a page is already under I/O, write_cache_pages() skips it, even
2460 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2461 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2462 * and msync() need to guarantee that all the data which was dirty at the time
2463 * the call was made get new I/O started against them. If wbc->sync_mode is
2464 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2465 * existing IO to complete.
2467 static int extent_write_cache_pages(struct extent_io_tree *tree,
2468 struct address_space *mapping,
2469 struct writeback_control *wbc,
2470 writepage_t writepage, void *data,
2471 void (*flush_fn)(void *))
2475 int nr_to_write_done = 0;
2476 struct pagevec pvec;
2479 pgoff_t end; /* Inclusive */
2482 pagevec_init(&pvec, 0);
2483 if (wbc->range_cyclic) {
2484 index = mapping->writeback_index; /* Start from prev offset */
2487 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2488 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2492 while (!done && !nr_to_write_done && (index <= end) &&
2493 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2494 PAGECACHE_TAG_DIRTY, min(end - index,
2495 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2499 for (i = 0; i < nr_pages; i++) {
2500 struct page *page = pvec.pages[i];
2503 * At this point we hold neither mapping->tree_lock nor
2504 * lock on the page itself: the page may be truncated or
2505 * invalidated (changing page->mapping to NULL), or even
2506 * swizzled back from swapper_space to tmpfs file
2509 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2510 tree->ops->write_cache_pages_lock_hook(page);
2514 if (unlikely(page->mapping != mapping)) {
2519 if (!wbc->range_cyclic && page->index > end) {
2525 if (wbc->sync_mode != WB_SYNC_NONE) {
2526 if (PageWriteback(page))
2528 wait_on_page_writeback(page);
2531 if (PageWriteback(page) ||
2532 !clear_page_dirty_for_io(page)) {
2537 ret = (*writepage)(page, wbc, data);
2539 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2547 * the filesystem may choose to bump up nr_to_write.
2548 * We have to make sure to honor the new nr_to_write
2551 nr_to_write_done = wbc->nr_to_write <= 0;
2553 pagevec_release(&pvec);
2556 if (!scanned && !done) {
2558 * We hit the last page and there is more work to be done: wrap
2559 * back to the start of the file
2568 static void flush_epd_write_bio(struct extent_page_data *epd)
2572 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2574 submit_one_bio(WRITE, epd->bio, 0, 0);
2579 static noinline void flush_write_bio(void *data)
2581 struct extent_page_data *epd = data;
2582 flush_epd_write_bio(epd);
2585 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2586 get_extent_t *get_extent,
2587 struct writeback_control *wbc)
2590 struct address_space *mapping = page->mapping;
2591 struct extent_page_data epd = {
2594 .get_extent = get_extent,
2596 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
2598 struct writeback_control wbc_writepages = {
2599 .sync_mode = wbc->sync_mode,
2600 .older_than_this = NULL,
2602 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2603 .range_end = (loff_t)-1,
2606 ret = __extent_writepage(page, wbc, &epd);
2608 extent_write_cache_pages(tree, mapping, &wbc_writepages,
2609 __extent_writepage, &epd, flush_write_bio);
2610 flush_epd_write_bio(&epd);
2614 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2615 u64 start, u64 end, get_extent_t *get_extent,
2619 struct address_space *mapping = inode->i_mapping;
2621 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2624 struct extent_page_data epd = {
2627 .get_extent = get_extent,
2629 .sync_io = mode == WB_SYNC_ALL,
2631 struct writeback_control wbc_writepages = {
2633 .older_than_this = NULL,
2634 .nr_to_write = nr_pages * 2,
2635 .range_start = start,
2636 .range_end = end + 1,
2639 while (start <= end) {
2640 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2641 if (clear_page_dirty_for_io(page))
2642 ret = __extent_writepage(page, &wbc_writepages, &epd);
2644 if (tree->ops && tree->ops->writepage_end_io_hook)
2645 tree->ops->writepage_end_io_hook(page, start,
2646 start + PAGE_CACHE_SIZE - 1,
2650 page_cache_release(page);
2651 start += PAGE_CACHE_SIZE;
2654 flush_epd_write_bio(&epd);
2658 int extent_writepages(struct extent_io_tree *tree,
2659 struct address_space *mapping,
2660 get_extent_t *get_extent,
2661 struct writeback_control *wbc)
2664 struct extent_page_data epd = {
2667 .get_extent = get_extent,
2669 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
2672 ret = extent_write_cache_pages(tree, mapping, wbc,
2673 __extent_writepage, &epd,
2675 flush_epd_write_bio(&epd);
2679 int extent_readpages(struct extent_io_tree *tree,
2680 struct address_space *mapping,
2681 struct list_head *pages, unsigned nr_pages,
2682 get_extent_t get_extent)
2684 struct bio *bio = NULL;
2686 unsigned long bio_flags = 0;
2688 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2689 struct page *page = list_entry(pages->prev, struct page, lru);
2691 prefetchw(&page->flags);
2692 list_del(&page->lru);
2693 if (!add_to_page_cache_lru(page, mapping,
2694 page->index, GFP_NOFS)) {
2695 __extent_read_full_page(tree, page, get_extent,
2696 &bio, 0, &bio_flags);
2698 page_cache_release(page);
2700 BUG_ON(!list_empty(pages));
2702 submit_one_bio(READ, bio, 0, bio_flags);
2707 * basic invalidatepage code, this waits on any locked or writeback
2708 * ranges corresponding to the page, and then deletes any extent state
2709 * records from the tree
2711 int extent_invalidatepage(struct extent_io_tree *tree,
2712 struct page *page, unsigned long offset)
2714 struct extent_state *cached_state = NULL;
2715 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2716 u64 end = start + PAGE_CACHE_SIZE - 1;
2717 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2719 start += (offset + blocksize - 1) & ~(blocksize - 1);
2723 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
2724 wait_on_page_writeback(page);
2725 clear_extent_bit(tree, start, end,
2726 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2727 EXTENT_DO_ACCOUNTING,
2728 1, 1, &cached_state, GFP_NOFS);
2733 * simple commit_write call, set_range_dirty is used to mark both
2734 * the pages and the extent records as dirty
2736 int extent_commit_write(struct extent_io_tree *tree,
2737 struct inode *inode, struct page *page,
2738 unsigned from, unsigned to)
2740 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2742 set_page_extent_mapped(page);
2743 set_page_dirty(page);
2745 if (pos > inode->i_size) {
2746 i_size_write(inode, pos);
2747 mark_inode_dirty(inode);
2752 int extent_prepare_write(struct extent_io_tree *tree,
2753 struct inode *inode, struct page *page,
2754 unsigned from, unsigned to, get_extent_t *get_extent)
2756 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2757 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2759 u64 orig_block_start;
2762 struct extent_map *em;
2763 unsigned blocksize = 1 << inode->i_blkbits;
2764 size_t page_offset = 0;
2765 size_t block_off_start;
2766 size_t block_off_end;
2772 set_page_extent_mapped(page);
2774 block_start = (page_start + from) & ~((u64)blocksize - 1);
2775 block_end = (page_start + to - 1) | (blocksize - 1);
2776 orig_block_start = block_start;
2778 lock_extent(tree, page_start, page_end, GFP_NOFS);
2779 while (block_start <= block_end) {
2780 em = get_extent(inode, page, page_offset, block_start,
2781 block_end - block_start + 1, 1);
2782 if (IS_ERR(em) || !em)
2785 cur_end = min(block_end, extent_map_end(em) - 1);
2786 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2787 block_off_end = block_off_start + blocksize;
2788 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2790 if (!PageUptodate(page) && isnew &&
2791 (block_off_end > to || block_off_start < from)) {
2794 kaddr = kmap_atomic(page, KM_USER0);
2795 if (block_off_end > to)
2796 memset(kaddr + to, 0, block_off_end - to);
2797 if (block_off_start < from)
2798 memset(kaddr + block_off_start, 0,
2799 from - block_off_start);
2800 flush_dcache_page(page);
2801 kunmap_atomic(kaddr, KM_USER0);
2803 if ((em->block_start != EXTENT_MAP_HOLE &&
2804 em->block_start != EXTENT_MAP_INLINE) &&
2805 !isnew && !PageUptodate(page) &&
2806 (block_off_end > to || block_off_start < from) &&
2807 !test_range_bit(tree, block_start, cur_end,
2808 EXTENT_UPTODATE, 1, NULL)) {
2810 u64 extent_offset = block_start - em->start;
2812 sector = (em->block_start + extent_offset) >> 9;
2813 iosize = (cur_end - block_start + blocksize) &
2814 ~((u64)blocksize - 1);
2816 * we've already got the extent locked, but we
2817 * need to split the state such that our end_bio
2818 * handler can clear the lock.
2820 set_extent_bit(tree, block_start,
2821 block_start + iosize - 1,
2822 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
2823 ret = submit_extent_page(READ, tree, page,
2824 sector, iosize, page_offset, em->bdev,
2826 end_bio_extent_preparewrite, 0,
2831 block_start = block_start + iosize;
2833 struct extent_state *cached = NULL;
2835 set_extent_uptodate(tree, block_start, cur_end, &cached,
2837 unlock_extent_cached(tree, block_start, cur_end,
2839 block_start = cur_end + 1;
2841 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2842 free_extent_map(em);
2845 wait_extent_bit(tree, orig_block_start,
2846 block_end, EXTENT_LOCKED);
2848 check_page_uptodate(tree, page);
2850 /* FIXME, zero out newly allocated blocks on error */
2855 * a helper for releasepage, this tests for areas of the page that
2856 * are locked or under IO and drops the related state bits if it is safe
2859 int try_release_extent_state(struct extent_map_tree *map,
2860 struct extent_io_tree *tree, struct page *page,
2863 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2864 u64 end = start + PAGE_CACHE_SIZE - 1;
2867 if (test_range_bit(tree, start, end,
2868 EXTENT_IOBITS, 0, NULL))
2871 if ((mask & GFP_NOFS) == GFP_NOFS)
2874 * at this point we can safely clear everything except the
2875 * locked bit and the nodatasum bit
2877 ret = clear_extent_bit(tree, start, end,
2878 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2881 /* if clear_extent_bit failed for enomem reasons,
2882 * we can't allow the release to continue.
2893 * a helper for releasepage. As long as there are no locked extents
2894 * in the range corresponding to the page, both state records and extent
2895 * map records are removed
2897 int try_release_extent_mapping(struct extent_map_tree *map,
2898 struct extent_io_tree *tree, struct page *page,
2901 struct extent_map *em;
2902 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2903 u64 end = start + PAGE_CACHE_SIZE - 1;
2905 if ((mask & __GFP_WAIT) &&
2906 page->mapping->host->i_size > 16 * 1024 * 1024) {
2908 while (start <= end) {
2909 len = end - start + 1;
2910 write_lock(&map->lock);
2911 em = lookup_extent_mapping(map, start, len);
2912 if (!em || IS_ERR(em)) {
2913 write_unlock(&map->lock);
2916 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2917 em->start != start) {
2918 write_unlock(&map->lock);
2919 free_extent_map(em);
2922 if (!test_range_bit(tree, em->start,
2923 extent_map_end(em) - 1,
2924 EXTENT_LOCKED | EXTENT_WRITEBACK,
2926 remove_extent_mapping(map, em);
2927 /* once for the rb tree */
2928 free_extent_map(em);
2930 start = extent_map_end(em);
2931 write_unlock(&map->lock);
2934 free_extent_map(em);
2937 return try_release_extent_state(map, tree, page, mask);
2940 sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2941 get_extent_t *get_extent)
2943 struct inode *inode = mapping->host;
2944 struct extent_state *cached_state = NULL;
2945 u64 start = iblock << inode->i_blkbits;
2946 sector_t sector = 0;
2947 size_t blksize = (1 << inode->i_blkbits);
2948 struct extent_map *em;
2950 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2951 0, &cached_state, GFP_NOFS);
2952 em = get_extent(inode, NULL, 0, start, blksize, 0);
2953 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2954 start + blksize - 1, &cached_state, GFP_NOFS);
2955 if (!em || IS_ERR(em))
2958 if (em->block_start > EXTENT_MAP_LAST_BYTE)
2961 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2963 free_extent_map(em);
2968 * helper function for fiemap, which doesn't want to see any holes.
2969 * This maps until we find something past 'last'
2971 static struct extent_map *get_extent_skip_holes(struct inode *inode,
2974 get_extent_t *get_extent)
2976 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
2977 struct extent_map *em;
2984 len = last - offset;
2987 len = (len + sectorsize - 1) & ~(sectorsize - 1);
2988 em = get_extent(inode, NULL, 0, offset, len, 0);
2989 if (!em || IS_ERR(em))
2992 /* if this isn't a hole return it */
2993 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
2994 em->block_start != EXTENT_MAP_HOLE) {
2998 /* this is a hole, advance to the next extent */
2999 offset = extent_map_end(em);
3000 free_extent_map(em);
3007 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3008 __u64 start, __u64 len, get_extent_t *get_extent)
3012 u64 max = start + len;
3016 u64 last_for_get_extent = 0;
3018 u64 isize = i_size_read(inode);
3019 struct btrfs_key found_key;
3020 struct extent_map *em = NULL;
3021 struct extent_state *cached_state = NULL;
3022 struct btrfs_path *path;
3023 struct btrfs_file_extent_item *item;
3028 unsigned long emflags;
3033 path = btrfs_alloc_path();
3036 path->leave_spinning = 1;
3039 * lookup the last file extent. We're not using i_size here
3040 * because there might be preallocation past i_size
3042 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3043 path, inode->i_ino, -1, 0);
3045 btrfs_free_path(path);
3050 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3051 struct btrfs_file_extent_item);
3052 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3053 found_type = btrfs_key_type(&found_key);
3055 /* No extents, but there might be delalloc bits */
3056 if (found_key.objectid != inode->i_ino ||
3057 found_type != BTRFS_EXTENT_DATA_KEY) {
3058 /* have to trust i_size as the end */
3060 last_for_get_extent = isize;
3063 * remember the start of the last extent. There are a
3064 * bunch of different factors that go into the length of the
3065 * extent, so its much less complex to remember where it started
3067 last = found_key.offset;
3068 last_for_get_extent = last + 1;
3070 btrfs_free_path(path);
3073 * we might have some extents allocated but more delalloc past those
3074 * extents. so, we trust isize unless the start of the last extent is
3079 last_for_get_extent = isize;
3082 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3083 &cached_state, GFP_NOFS);
3085 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3095 u64 offset_in_extent;
3097 /* break if the extent we found is outside the range */
3098 if (em->start >= max || extent_map_end(em) < off)
3102 * get_extent may return an extent that starts before our
3103 * requested range. We have to make sure the ranges
3104 * we return to fiemap always move forward and don't
3105 * overlap, so adjust the offsets here
3107 em_start = max(em->start, off);
3110 * record the offset from the start of the extent
3111 * for adjusting the disk offset below
3113 offset_in_extent = em_start - em->start;
3114 em_end = extent_map_end(em);
3115 em_len = em_end - em_start;
3116 emflags = em->flags;
3121 * bump off for our next call to get_extent
3123 off = extent_map_end(em);
3127 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
3129 flags |= FIEMAP_EXTENT_LAST;
3130 } else if (em->block_start == EXTENT_MAP_INLINE) {
3131 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3132 FIEMAP_EXTENT_NOT_ALIGNED);
3133 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
3134 flags |= (FIEMAP_EXTENT_DELALLOC |
3135 FIEMAP_EXTENT_UNKNOWN);
3137 disko = em->block_start + offset_in_extent;
3139 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3140 flags |= FIEMAP_EXTENT_ENCODED;
3142 free_extent_map(em);
3144 if ((em_start >= last) || em_len == (u64)-1 ||
3145 (last == (u64)-1 && isize <= em_end)) {
3146 flags |= FIEMAP_EXTENT_LAST;
3150 /* now scan forward to see if this is really the last extent. */
3151 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3158 flags |= FIEMAP_EXTENT_LAST;
3161 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3167 free_extent_map(em);
3169 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3170 &cached_state, GFP_NOFS);
3174 static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3178 struct address_space *mapping;
3181 return eb->first_page;
3182 i += eb->start >> PAGE_CACHE_SHIFT;
3183 mapping = eb->first_page->mapping;
3188 * extent_buffer_page is only called after pinning the page
3189 * by increasing the reference count. So we know the page must
3190 * be in the radix tree.
3193 p = radix_tree_lookup(&mapping->page_tree, i);
3199 static inline unsigned long num_extent_pages(u64 start, u64 len)
3201 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3202 (start >> PAGE_CACHE_SHIFT);
3205 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3210 struct extent_buffer *eb = NULL;
3212 unsigned long flags;
3215 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
3220 spin_lock_init(&eb->lock);
3221 init_waitqueue_head(&eb->lock_wq);
3224 spin_lock_irqsave(&leak_lock, flags);
3225 list_add(&eb->leak_list, &buffers);
3226 spin_unlock_irqrestore(&leak_lock, flags);
3228 atomic_set(&eb->refs, 1);
3233 static void __free_extent_buffer(struct extent_buffer *eb)
3236 unsigned long flags;
3237 spin_lock_irqsave(&leak_lock, flags);
3238 list_del(&eb->leak_list);
3239 spin_unlock_irqrestore(&leak_lock, flags);
3241 kmem_cache_free(extent_buffer_cache, eb);
3245 * Helper for releasing extent buffer page.
3247 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3248 unsigned long start_idx)
3250 unsigned long index;
3253 if (!eb->first_page)
3256 index = num_extent_pages(eb->start, eb->len);
3257 if (start_idx >= index)
3262 page = extent_buffer_page(eb, index);
3264 page_cache_release(page);
3265 } while (index != start_idx);
3269 * Helper for releasing the extent buffer.
3271 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3273 btrfs_release_extent_buffer_page(eb, 0);
3274 __free_extent_buffer(eb);
3277 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3278 u64 start, unsigned long len,
3282 unsigned long num_pages = num_extent_pages(start, len);
3284 unsigned long index = start >> PAGE_CACHE_SHIFT;
3285 struct extent_buffer *eb;
3286 struct extent_buffer *exists = NULL;
3288 struct address_space *mapping = tree->mapping;
3293 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3294 if (eb && atomic_inc_not_zero(&eb->refs)) {
3296 mark_page_accessed(eb->first_page);
3301 eb = __alloc_extent_buffer(tree, start, len, mask);
3306 eb->first_page = page0;
3309 page_cache_get(page0);
3310 mark_page_accessed(page0);
3311 set_page_extent_mapped(page0);
3312 set_page_extent_head(page0, len);
3313 uptodate = PageUptodate(page0);
3317 for (; i < num_pages; i++, index++) {
3318 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3323 set_page_extent_mapped(p);
3324 mark_page_accessed(p);
3327 set_page_extent_head(p, len);
3329 set_page_private(p, EXTENT_PAGE_PRIVATE);
3331 if (!PageUptodate(p))
3335 * see below about how we avoid a nasty race with release page
3336 * and why we unlock later
3342 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3344 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3348 spin_lock(&tree->buffer_lock);
3349 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3350 if (ret == -EEXIST) {
3351 exists = radix_tree_lookup(&tree->buffer,
3352 start >> PAGE_CACHE_SHIFT);
3353 /* add one reference for the caller */
3354 atomic_inc(&exists->refs);
3355 spin_unlock(&tree->buffer_lock);
3356 radix_tree_preload_end();
3359 /* add one reference for the tree */
3360 atomic_inc(&eb->refs);
3361 spin_unlock(&tree->buffer_lock);
3362 radix_tree_preload_end();
3365 * there is a race where release page may have
3366 * tried to find this extent buffer in the radix
3367 * but failed. It will tell the VM it is safe to
3368 * reclaim the, and it will clear the page private bit.
3369 * We must make sure to set the page private bit properly
3370 * after the extent buffer is in the radix tree so
3371 * it doesn't get lost
3373 set_page_extent_mapped(eb->first_page);
3374 set_page_extent_head(eb->first_page, eb->len);
3376 unlock_page(eb->first_page);
3380 if (eb->first_page && !page0)
3381 unlock_page(eb->first_page);
3383 if (!atomic_dec_and_test(&eb->refs))
3385 btrfs_release_extent_buffer(eb);
3389 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3390 u64 start, unsigned long len,
3393 struct extent_buffer *eb;
3396 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3397 if (eb && atomic_inc_not_zero(&eb->refs)) {
3399 mark_page_accessed(eb->first_page);
3407 void free_extent_buffer(struct extent_buffer *eb)
3412 if (!atomic_dec_and_test(&eb->refs))
3418 int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3419 struct extent_buffer *eb)
3422 unsigned long num_pages;
3425 num_pages = num_extent_pages(eb->start, eb->len);
3427 for (i = 0; i < num_pages; i++) {
3428 page = extent_buffer_page(eb, i);
3429 if (!PageDirty(page))
3433 WARN_ON(!PagePrivate(page));
3435 set_page_extent_mapped(page);
3437 set_page_extent_head(page, eb->len);
3439 clear_page_dirty_for_io(page);
3440 spin_lock_irq(&page->mapping->tree_lock);
3441 if (!PageDirty(page)) {
3442 radix_tree_tag_clear(&page->mapping->page_tree,
3444 PAGECACHE_TAG_DIRTY);
3446 spin_unlock_irq(&page->mapping->tree_lock);
3452 int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3453 struct extent_buffer *eb)
3455 return wait_on_extent_writeback(tree, eb->start,
3456 eb->start + eb->len - 1);
3459 int set_extent_buffer_dirty(struct extent_io_tree *tree,
3460 struct extent_buffer *eb)
3463 unsigned long num_pages;
3466 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3467 num_pages = num_extent_pages(eb->start, eb->len);
3468 for (i = 0; i < num_pages; i++)
3469 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
3473 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3474 struct extent_buffer *eb,
3475 struct extent_state **cached_state)
3479 unsigned long num_pages;
3481 num_pages = num_extent_pages(eb->start, eb->len);
3482 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3484 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3485 cached_state, GFP_NOFS);
3486 for (i = 0; i < num_pages; i++) {
3487 page = extent_buffer_page(eb, i);
3489 ClearPageUptodate(page);
3494 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3495 struct extent_buffer *eb)
3499 unsigned long num_pages;
3501 num_pages = num_extent_pages(eb->start, eb->len);
3503 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3505 for (i = 0; i < num_pages; i++) {
3506 page = extent_buffer_page(eb, i);
3507 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3508 ((i == num_pages - 1) &&
3509 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3510 check_page_uptodate(tree, page);
3513 SetPageUptodate(page);
3518 int extent_range_uptodate(struct extent_io_tree *tree,
3523 int pg_uptodate = 1;
3525 unsigned long index;
3527 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
3530 while (start <= end) {
3531 index = start >> PAGE_CACHE_SHIFT;
3532 page = find_get_page(tree->mapping, index);
3533 uptodate = PageUptodate(page);
3534 page_cache_release(page);
3539 start += PAGE_CACHE_SIZE;
3544 int extent_buffer_uptodate(struct extent_io_tree *tree,
3545 struct extent_buffer *eb,
3546 struct extent_state *cached_state)
3549 unsigned long num_pages;
3552 int pg_uptodate = 1;
3554 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3557 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3558 EXTENT_UPTODATE, 1, cached_state);
3562 num_pages = num_extent_pages(eb->start, eb->len);
3563 for (i = 0; i < num_pages; i++) {
3564 page = extent_buffer_page(eb, i);
3565 if (!PageUptodate(page)) {
3573 int read_extent_buffer_pages(struct extent_io_tree *tree,
3574 struct extent_buffer *eb,
3575 u64 start, int wait,
3576 get_extent_t *get_extent, int mirror_num)
3579 unsigned long start_i;
3583 int locked_pages = 0;
3584 int all_uptodate = 1;
3585 int inc_all_pages = 0;
3586 unsigned long num_pages;
3587 struct bio *bio = NULL;
3588 unsigned long bio_flags = 0;
3590 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3593 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3594 EXTENT_UPTODATE, 1, NULL)) {
3599 WARN_ON(start < eb->start);
3600 start_i = (start >> PAGE_CACHE_SHIFT) -
3601 (eb->start >> PAGE_CACHE_SHIFT);
3606 num_pages = num_extent_pages(eb->start, eb->len);
3607 for (i = start_i; i < num_pages; i++) {
3608 page = extent_buffer_page(eb, i);
3610 if (!trylock_page(page))
3616 if (!PageUptodate(page))
3621 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3625 for (i = start_i; i < num_pages; i++) {
3626 page = extent_buffer_page(eb, i);
3628 WARN_ON(!PagePrivate(page));
3630 set_page_extent_mapped(page);
3632 set_page_extent_head(page, eb->len);
3635 page_cache_get(page);
3636 if (!PageUptodate(page)) {
3639 ClearPageError(page);
3640 err = __extent_read_full_page(tree, page,
3642 mirror_num, &bio_flags);
3651 submit_one_bio(READ, bio, mirror_num, bio_flags);
3656 for (i = start_i; i < num_pages; i++) {
3657 page = extent_buffer_page(eb, i);
3658 wait_on_page_locked(page);
3659 if (!PageUptodate(page))
3664 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3669 while (locked_pages > 0) {
3670 page = extent_buffer_page(eb, i);
3678 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3679 unsigned long start,
3686 char *dst = (char *)dstv;
3687 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3688 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3690 WARN_ON(start > eb->len);
3691 WARN_ON(start + len > eb->start + eb->len);
3693 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3696 page = extent_buffer_page(eb, i);
3698 cur = min(len, (PAGE_CACHE_SIZE - offset));
3699 kaddr = kmap_atomic(page, KM_USER1);
3700 memcpy(dst, kaddr + offset, cur);
3701 kunmap_atomic(kaddr, KM_USER1);
3710 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3711 unsigned long min_len, char **token, char **map,
3712 unsigned long *map_start,
3713 unsigned long *map_len, int km)
3715 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3718 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3719 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3720 unsigned long end_i = (start_offset + start + min_len - 1) >>
3727 offset = start_offset;
3731 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3734 if (start + min_len > eb->len) {
3735 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3736 "wanted %lu %lu\n", (unsigned long long)eb->start,
3737 eb->len, start, min_len);
3742 p = extent_buffer_page(eb, i);
3743 kaddr = kmap_atomic(p, km);
3745 *map = kaddr + offset;
3746 *map_len = PAGE_CACHE_SIZE - offset;
3750 int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3751 unsigned long min_len,
3752 char **token, char **map,
3753 unsigned long *map_start,
3754 unsigned long *map_len, int km)
3758 if (eb->map_token) {
3759 unmap_extent_buffer(eb, eb->map_token, km);
3760 eb->map_token = NULL;
3763 err = map_private_extent_buffer(eb, start, min_len, token, map,
3764 map_start, map_len, km);
3766 eb->map_token = *token;
3768 eb->map_start = *map_start;
3769 eb->map_len = *map_len;
3774 void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3776 kunmap_atomic(token, km);
3779 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3780 unsigned long start,
3787 char *ptr = (char *)ptrv;
3788 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3789 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3792 WARN_ON(start > eb->len);
3793 WARN_ON(start + len > eb->start + eb->len);
3795 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3798 page = extent_buffer_page(eb, i);
3800 cur = min(len, (PAGE_CACHE_SIZE - offset));
3802 kaddr = kmap_atomic(page, KM_USER0);
3803 ret = memcmp(ptr, kaddr + offset, cur);
3804 kunmap_atomic(kaddr, KM_USER0);
3816 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3817 unsigned long start, unsigned long len)
3823 char *src = (char *)srcv;
3824 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3825 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3827 WARN_ON(start > eb->len);
3828 WARN_ON(start + len > eb->start + eb->len);
3830 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3833 page = extent_buffer_page(eb, i);
3834 WARN_ON(!PageUptodate(page));
3836 cur = min(len, PAGE_CACHE_SIZE - offset);
3837 kaddr = kmap_atomic(page, KM_USER1);
3838 memcpy(kaddr + offset, src, cur);
3839 kunmap_atomic(kaddr, KM_USER1);
3848 void memset_extent_buffer(struct extent_buffer *eb, char c,
3849 unsigned long start, unsigned long len)
3855 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3856 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3858 WARN_ON(start > eb->len);
3859 WARN_ON(start + len > eb->start + eb->len);
3861 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3864 page = extent_buffer_page(eb, i);
3865 WARN_ON(!PageUptodate(page));
3867 cur = min(len, PAGE_CACHE_SIZE - offset);
3868 kaddr = kmap_atomic(page, KM_USER0);
3869 memset(kaddr + offset, c, cur);
3870 kunmap_atomic(kaddr, KM_USER0);
3878 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3879 unsigned long dst_offset, unsigned long src_offset,
3882 u64 dst_len = dst->len;
3887 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3888 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3890 WARN_ON(src->len != dst_len);
3892 offset = (start_offset + dst_offset) &
3893 ((unsigned long)PAGE_CACHE_SIZE - 1);
3896 page = extent_buffer_page(dst, i);
3897 WARN_ON(!PageUptodate(page));
3899 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3901 kaddr = kmap_atomic(page, KM_USER0);
3902 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3903 kunmap_atomic(kaddr, KM_USER0);
3912 static void move_pages(struct page *dst_page, struct page *src_page,
3913 unsigned long dst_off, unsigned long src_off,
3916 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3917 if (dst_page == src_page) {
3918 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3920 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3921 char *p = dst_kaddr + dst_off + len;
3922 char *s = src_kaddr + src_off + len;
3927 kunmap_atomic(src_kaddr, KM_USER1);
3929 kunmap_atomic(dst_kaddr, KM_USER0);
3932 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
3934 unsigned long distance = (src > dst) ? src - dst : dst - src;
3935 return distance < len;
3938 static void copy_pages(struct page *dst_page, struct page *src_page,
3939 unsigned long dst_off, unsigned long src_off,
3942 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3945 if (dst_page != src_page) {
3946 src_kaddr = kmap_atomic(src_page, KM_USER1);
3948 src_kaddr = dst_kaddr;
3949 BUG_ON(areas_overlap(src_off, dst_off, len));
3952 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3953 kunmap_atomic(dst_kaddr, KM_USER0);
3954 if (dst_page != src_page)
3955 kunmap_atomic(src_kaddr, KM_USER1);
3958 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3959 unsigned long src_offset, unsigned long len)
3962 size_t dst_off_in_page;
3963 size_t src_off_in_page;
3964 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3965 unsigned long dst_i;
3966 unsigned long src_i;
3968 if (src_offset + len > dst->len) {
3969 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3970 "len %lu dst len %lu\n", src_offset, len, dst->len);
3973 if (dst_offset + len > dst->len) {
3974 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3975 "len %lu dst len %lu\n", dst_offset, len, dst->len);
3980 dst_off_in_page = (start_offset + dst_offset) &
3981 ((unsigned long)PAGE_CACHE_SIZE - 1);
3982 src_off_in_page = (start_offset + src_offset) &
3983 ((unsigned long)PAGE_CACHE_SIZE - 1);
3985 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3986 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3988 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3990 cur = min_t(unsigned long, cur,
3991 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3993 copy_pages(extent_buffer_page(dst, dst_i),
3994 extent_buffer_page(dst, src_i),
3995 dst_off_in_page, src_off_in_page, cur);
4003 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4004 unsigned long src_offset, unsigned long len)
4007 size_t dst_off_in_page;
4008 size_t src_off_in_page;
4009 unsigned long dst_end = dst_offset + len - 1;
4010 unsigned long src_end = src_offset + len - 1;
4011 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4012 unsigned long dst_i;
4013 unsigned long src_i;
4015 if (src_offset + len > dst->len) {
4016 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4017 "len %lu len %lu\n", src_offset, len, dst->len);
4020 if (dst_offset + len > dst->len) {
4021 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4022 "len %lu len %lu\n", dst_offset, len, dst->len);
4025 if (!areas_overlap(src_offset, dst_offset, len)) {
4026 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4030 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4031 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4033 dst_off_in_page = (start_offset + dst_end) &
4034 ((unsigned long)PAGE_CACHE_SIZE - 1);
4035 src_off_in_page = (start_offset + src_end) &
4036 ((unsigned long)PAGE_CACHE_SIZE - 1);
4038 cur = min_t(unsigned long, len, src_off_in_page + 1);
4039 cur = min(cur, dst_off_in_page + 1);
4040 move_pages(extent_buffer_page(dst, dst_i),
4041 extent_buffer_page(dst, src_i),
4042 dst_off_in_page - cur + 1,
4043 src_off_in_page - cur + 1, cur);
4051 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4053 struct extent_buffer *eb =
4054 container_of(head, struct extent_buffer, rcu_head);
4056 btrfs_release_extent_buffer(eb);
4059 int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4061 u64 start = page_offset(page);
4062 struct extent_buffer *eb;
4065 spin_lock(&tree->buffer_lock);
4066 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4068 spin_unlock(&tree->buffer_lock);
4072 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4078 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4081 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4086 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4088 spin_unlock(&tree->buffer_lock);
4090 /* at this point we can safely release the extent buffer */
4091 if (atomic_read(&eb->refs) == 0)
4092 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);