1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/spinlock.h>
8 #include <linux/blkdev.h>
9 #include <linux/swap.h>
10 #include <linux/writeback.h>
11 #include <linux/pagevec.h>
12 #include <linux/prefetch.h>
13 #include <linux/cleancache.h>
14 #include "extent_io.h"
15 #include "extent_map.h"
18 #include "btrfs_inode.h"
20 #include "check-integrity.h"
22 #include "rcu-string.h"
24 static struct kmem_cache *extent_state_cache;
25 static struct kmem_cache *extent_buffer_cache;
27 static LIST_HEAD(buffers);
28 static LIST_HEAD(states);
32 static DEFINE_SPINLOCK(leak_lock);
35 #define BUFFER_LRU_MAX 64
40 struct rb_node rb_node;
43 struct extent_page_data {
45 struct extent_io_tree *tree;
46 get_extent_t *get_extent;
47 unsigned long bio_flags;
49 /* tells writepage not to lock the state bits for this range
50 * it still does the unlocking
52 unsigned int extent_locked:1;
54 /* tells the submit_bio code to use a WRITE_SYNC */
55 unsigned int sync_io:1;
58 static noinline void flush_write_bio(void *data);
59 static inline struct btrfs_fs_info *
60 tree_fs_info(struct extent_io_tree *tree)
62 return btrfs_sb(tree->mapping->host->i_sb);
65 int __init extent_io_init(void)
67 extent_state_cache = kmem_cache_create("btrfs_extent_state",
68 sizeof(struct extent_state), 0,
69 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
70 if (!extent_state_cache)
73 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
74 sizeof(struct extent_buffer), 0,
75 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
76 if (!extent_buffer_cache)
77 goto free_state_cache;
81 kmem_cache_destroy(extent_state_cache);
85 void extent_io_exit(void)
87 struct extent_state *state;
88 struct extent_buffer *eb;
90 while (!list_empty(&states)) {
91 state = list_entry(states.next, struct extent_state, leak_list);
92 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
93 "state %lu in tree %p refs %d\n",
94 (unsigned long long)state->start,
95 (unsigned long long)state->end,
96 state->state, state->tree, atomic_read(&state->refs));
97 list_del(&state->leak_list);
98 kmem_cache_free(extent_state_cache, state);
102 while (!list_empty(&buffers)) {
103 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
104 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
105 "refs %d\n", (unsigned long long)eb->start,
106 eb->len, atomic_read(&eb->refs));
107 list_del(&eb->leak_list);
108 kmem_cache_free(extent_buffer_cache, eb);
112 * Make sure all delayed rcu free are flushed before we
116 if (extent_state_cache)
117 kmem_cache_destroy(extent_state_cache);
118 if (extent_buffer_cache)
119 kmem_cache_destroy(extent_buffer_cache);
122 void extent_io_tree_init(struct extent_io_tree *tree,
123 struct address_space *mapping)
125 tree->state = RB_ROOT;
126 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
128 tree->dirty_bytes = 0;
129 spin_lock_init(&tree->lock);
130 spin_lock_init(&tree->buffer_lock);
131 tree->mapping = mapping;
134 static struct extent_state *alloc_extent_state(gfp_t mask)
136 struct extent_state *state;
141 state = kmem_cache_alloc(extent_state_cache, mask);
148 spin_lock_irqsave(&leak_lock, flags);
149 list_add(&state->leak_list, &states);
150 spin_unlock_irqrestore(&leak_lock, flags);
152 atomic_set(&state->refs, 1);
153 init_waitqueue_head(&state->wq);
154 trace_alloc_extent_state(state, mask, _RET_IP_);
158 void free_extent_state(struct extent_state *state)
162 if (atomic_dec_and_test(&state->refs)) {
166 WARN_ON(state->tree);
168 spin_lock_irqsave(&leak_lock, flags);
169 list_del(&state->leak_list);
170 spin_unlock_irqrestore(&leak_lock, flags);
172 trace_free_extent_state(state, _RET_IP_);
173 kmem_cache_free(extent_state_cache, state);
177 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
178 struct rb_node *node)
180 struct rb_node **p = &root->rb_node;
181 struct rb_node *parent = NULL;
182 struct tree_entry *entry;
186 entry = rb_entry(parent, struct tree_entry, rb_node);
188 if (offset < entry->start)
190 else if (offset > entry->end)
196 rb_link_node(node, parent, p);
197 rb_insert_color(node, root);
201 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
202 struct rb_node **prev_ret,
203 struct rb_node **next_ret)
205 struct rb_root *root = &tree->state;
206 struct rb_node *n = root->rb_node;
207 struct rb_node *prev = NULL;
208 struct rb_node *orig_prev = NULL;
209 struct tree_entry *entry;
210 struct tree_entry *prev_entry = NULL;
213 entry = rb_entry(n, struct tree_entry, rb_node);
217 if (offset < entry->start)
219 else if (offset > entry->end)
227 while (prev && offset > prev_entry->end) {
228 prev = rb_next(prev);
229 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
236 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
237 while (prev && offset < prev_entry->start) {
238 prev = rb_prev(prev);
239 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
246 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
249 struct rb_node *prev = NULL;
252 ret = __etree_search(tree, offset, &prev, NULL);
258 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
259 struct extent_state *other)
261 if (tree->ops && tree->ops->merge_extent_hook)
262 tree->ops->merge_extent_hook(tree->mapping->host, new,
267 * utility function to look for merge candidates inside a given range.
268 * Any extents with matching state are merged together into a single
269 * extent in the tree. Extents with EXTENT_IO in their state field
270 * are not merged because the end_io handlers need to be able to do
271 * operations on them without sleeping (or doing allocations/splits).
273 * This should be called with the tree lock held.
275 static void merge_state(struct extent_io_tree *tree,
276 struct extent_state *state)
278 struct extent_state *other;
279 struct rb_node *other_node;
281 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
284 other_node = rb_prev(&state->rb_node);
286 other = rb_entry(other_node, struct extent_state, rb_node);
287 if (other->end == state->start - 1 &&
288 other->state == state->state) {
289 merge_cb(tree, state, other);
290 state->start = other->start;
292 rb_erase(&other->rb_node, &tree->state);
293 free_extent_state(other);
296 other_node = rb_next(&state->rb_node);
298 other = rb_entry(other_node, struct extent_state, rb_node);
299 if (other->start == state->end + 1 &&
300 other->state == state->state) {
301 merge_cb(tree, state, other);
302 state->end = other->end;
304 rb_erase(&other->rb_node, &tree->state);
305 free_extent_state(other);
310 static void set_state_cb(struct extent_io_tree *tree,
311 struct extent_state *state, int *bits)
313 if (tree->ops && tree->ops->set_bit_hook)
314 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
317 static void clear_state_cb(struct extent_io_tree *tree,
318 struct extent_state *state, int *bits)
320 if (tree->ops && tree->ops->clear_bit_hook)
321 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
324 static void set_state_bits(struct extent_io_tree *tree,
325 struct extent_state *state, int *bits);
328 * insert an extent_state struct into the tree. 'bits' are set on the
329 * struct before it is inserted.
331 * This may return -EEXIST if the extent is already there, in which case the
332 * state struct is freed.
334 * The tree lock is not taken internally. This is a utility function and
335 * probably isn't what you want to call (see set/clear_extent_bit).
337 static int insert_state(struct extent_io_tree *tree,
338 struct extent_state *state, u64 start, u64 end,
341 struct rb_node *node;
344 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
345 (unsigned long long)end,
346 (unsigned long long)start);
347 state->start = start;
350 set_state_bits(tree, state, bits);
352 node = tree_insert(&tree->state, end, &state->rb_node);
354 struct extent_state *found;
355 found = rb_entry(node, struct extent_state, rb_node);
356 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
357 "%llu %llu\n", (unsigned long long)found->start,
358 (unsigned long long)found->end,
359 (unsigned long long)start, (unsigned long long)end);
363 merge_state(tree, state);
367 static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
370 if (tree->ops && tree->ops->split_extent_hook)
371 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
375 * split a given extent state struct in two, inserting the preallocated
376 * struct 'prealloc' as the newly created second half. 'split' indicates an
377 * offset inside 'orig' where it should be split.
380 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
381 * are two extent state structs in the tree:
382 * prealloc: [orig->start, split - 1]
383 * orig: [ split, orig->end ]
385 * The tree locks are not taken by this function. They need to be held
388 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
389 struct extent_state *prealloc, u64 split)
391 struct rb_node *node;
393 split_cb(tree, orig, split);
395 prealloc->start = orig->start;
396 prealloc->end = split - 1;
397 prealloc->state = orig->state;
400 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
402 free_extent_state(prealloc);
405 prealloc->tree = tree;
409 static struct extent_state *next_state(struct extent_state *state)
411 struct rb_node *next = rb_next(&state->rb_node);
413 return rb_entry(next, struct extent_state, rb_node);
419 * utility function to clear some bits in an extent state struct.
420 * it will optionally wake up any one waiting on this state (wake == 1).
422 * If no bits are set on the state struct after clearing things, the
423 * struct is freed and removed from the tree
425 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
426 struct extent_state *state,
429 struct extent_state *next;
430 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
432 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
433 u64 range = state->end - state->start + 1;
434 WARN_ON(range > tree->dirty_bytes);
435 tree->dirty_bytes -= range;
437 clear_state_cb(tree, state, bits);
438 state->state &= ~bits_to_clear;
441 if (state->state == 0) {
442 next = next_state(state);
444 rb_erase(&state->rb_node, &tree->state);
446 free_extent_state(state);
451 merge_state(tree, state);
452 next = next_state(state);
457 static struct extent_state *
458 alloc_extent_state_atomic(struct extent_state *prealloc)
461 prealloc = alloc_extent_state(GFP_ATOMIC);
466 void extent_io_tree_panic(struct extent_io_tree *tree, int err)
468 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
469 "Extent tree was modified by another "
470 "thread while locked.");
474 * clear some bits on a range in the tree. This may require splitting
475 * or inserting elements in the tree, so the gfp mask is used to
476 * indicate which allocations or sleeping are allowed.
478 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
479 * the given range from the tree regardless of state (ie for truncate).
481 * the range [start, end] is inclusive.
483 * This takes the tree lock, and returns 0 on success and < 0 on error.
485 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
486 int bits, int wake, int delete,
487 struct extent_state **cached_state,
490 struct extent_state *state;
491 struct extent_state *cached;
492 struct extent_state *prealloc = NULL;
493 struct rb_node *node;
499 bits |= ~EXTENT_CTLBITS;
500 bits |= EXTENT_FIRST_DELALLOC;
502 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
505 if (!prealloc && (mask & __GFP_WAIT)) {
506 prealloc = alloc_extent_state(mask);
511 spin_lock(&tree->lock);
513 cached = *cached_state;
516 *cached_state = NULL;
520 if (cached && cached->tree && cached->start <= start &&
521 cached->end > start) {
523 atomic_dec(&cached->refs);
528 free_extent_state(cached);
531 * this search will find the extents that end after
534 node = tree_search(tree, start);
537 state = rb_entry(node, struct extent_state, rb_node);
539 if (state->start > end)
541 WARN_ON(state->end < start);
542 last_end = state->end;
544 /* the state doesn't have the wanted bits, go ahead */
545 if (!(state->state & bits)) {
546 state = next_state(state);
551 * | ---- desired range ---- |
553 * | ------------- state -------------- |
555 * We need to split the extent we found, and may flip
556 * bits on second half.
558 * If the extent we found extends past our range, we
559 * just split and search again. It'll get split again
560 * the next time though.
562 * If the extent we found is inside our range, we clear
563 * the desired bit on it.
566 if (state->start < start) {
567 prealloc = alloc_extent_state_atomic(prealloc);
569 err = split_state(tree, state, prealloc, start);
571 extent_io_tree_panic(tree, err);
576 if (state->end <= end) {
577 state = clear_state_bit(tree, state, &bits, wake);
583 * | ---- desired range ---- |
585 * We need to split the extent, and clear the bit
588 if (state->start <= end && state->end > end) {
589 prealloc = alloc_extent_state_atomic(prealloc);
591 err = split_state(tree, state, prealloc, end + 1);
593 extent_io_tree_panic(tree, err);
598 clear_state_bit(tree, prealloc, &bits, wake);
604 state = clear_state_bit(tree, state, &bits, wake);
606 if (last_end == (u64)-1)
608 start = last_end + 1;
609 if (start <= end && state && !need_resched())
614 spin_unlock(&tree->lock);
616 free_extent_state(prealloc);
623 spin_unlock(&tree->lock);
624 if (mask & __GFP_WAIT)
629 static void wait_on_state(struct extent_io_tree *tree,
630 struct extent_state *state)
631 __releases(tree->lock)
632 __acquires(tree->lock)
635 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
636 spin_unlock(&tree->lock);
638 spin_lock(&tree->lock);
639 finish_wait(&state->wq, &wait);
643 * waits for one or more bits to clear on a range in the state tree.
644 * The range [start, end] is inclusive.
645 * The tree lock is taken by this function
647 void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
649 struct extent_state *state;
650 struct rb_node *node;
652 spin_lock(&tree->lock);
656 * this search will find all the extents that end after
659 node = tree_search(tree, start);
663 state = rb_entry(node, struct extent_state, rb_node);
665 if (state->start > end)
668 if (state->state & bits) {
669 start = state->start;
670 atomic_inc(&state->refs);
671 wait_on_state(tree, state);
672 free_extent_state(state);
675 start = state->end + 1;
680 cond_resched_lock(&tree->lock);
683 spin_unlock(&tree->lock);
686 static void set_state_bits(struct extent_io_tree *tree,
687 struct extent_state *state,
690 int bits_to_set = *bits & ~EXTENT_CTLBITS;
692 set_state_cb(tree, state, bits);
693 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
694 u64 range = state->end - state->start + 1;
695 tree->dirty_bytes += range;
697 state->state |= bits_to_set;
700 static void cache_state(struct extent_state *state,
701 struct extent_state **cached_ptr)
703 if (cached_ptr && !(*cached_ptr)) {
704 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
706 atomic_inc(&state->refs);
711 static void uncache_state(struct extent_state **cached_ptr)
713 if (cached_ptr && (*cached_ptr)) {
714 struct extent_state *state = *cached_ptr;
716 free_extent_state(state);
721 * set some bits on a range in the tree. This may require allocations or
722 * sleeping, so the gfp mask is used to indicate what is allowed.
724 * If any of the exclusive bits are set, this will fail with -EEXIST if some
725 * part of the range already has the desired bits set. The start of the
726 * existing range is returned in failed_start in this case.
728 * [start, end] is inclusive This takes the tree lock.
731 static int __must_check
732 __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
733 int bits, int exclusive_bits, u64 *failed_start,
734 struct extent_state **cached_state, gfp_t mask)
736 struct extent_state *state;
737 struct extent_state *prealloc = NULL;
738 struct rb_node *node;
743 bits |= EXTENT_FIRST_DELALLOC;
745 if (!prealloc && (mask & __GFP_WAIT)) {
746 prealloc = alloc_extent_state(mask);
750 spin_lock(&tree->lock);
751 if (cached_state && *cached_state) {
752 state = *cached_state;
753 if (state->start <= start && state->end > start &&
755 node = &state->rb_node;
760 * this search will find all the extents that end after
763 node = tree_search(tree, start);
765 prealloc = alloc_extent_state_atomic(prealloc);
767 err = insert_state(tree, prealloc, start, end, &bits);
769 extent_io_tree_panic(tree, err);
774 state = rb_entry(node, struct extent_state, rb_node);
776 last_start = state->start;
777 last_end = state->end;
780 * | ---- desired range ---- |
783 * Just lock what we found and keep going
785 if (state->start == start && state->end <= end) {
786 if (state->state & exclusive_bits) {
787 *failed_start = state->start;
792 set_state_bits(tree, state, &bits);
793 cache_state(state, cached_state);
794 merge_state(tree, state);
795 if (last_end == (u64)-1)
797 start = last_end + 1;
798 state = next_state(state);
799 if (start < end && state && state->start == start &&
806 * | ---- desired range ---- |
809 * | ------------- state -------------- |
811 * We need to split the extent we found, and may flip bits on
814 * If the extent we found extends past our
815 * range, we just split and search again. It'll get split
816 * again the next time though.
818 * If the extent we found is inside our range, we set the
821 if (state->start < start) {
822 if (state->state & exclusive_bits) {
823 *failed_start = start;
828 prealloc = alloc_extent_state_atomic(prealloc);
830 err = split_state(tree, state, prealloc, start);
832 extent_io_tree_panic(tree, err);
837 if (state->end <= end) {
838 set_state_bits(tree, state, &bits);
839 cache_state(state, cached_state);
840 merge_state(tree, state);
841 if (last_end == (u64)-1)
843 start = last_end + 1;
844 state = next_state(state);
845 if (start < end && state && state->start == start &&
852 * | ---- desired range ---- |
853 * | state | or | state |
855 * There's a hole, we need to insert something in it and
856 * ignore the extent we found.
858 if (state->start > start) {
860 if (end < last_start)
863 this_end = last_start - 1;
865 prealloc = alloc_extent_state_atomic(prealloc);
869 * Avoid to free 'prealloc' if it can be merged with
872 err = insert_state(tree, prealloc, start, this_end,
875 extent_io_tree_panic(tree, err);
877 cache_state(prealloc, cached_state);
879 start = this_end + 1;
883 * | ---- desired range ---- |
885 * We need to split the extent, and set the bit
888 if (state->start <= end && state->end > end) {
889 if (state->state & exclusive_bits) {
890 *failed_start = start;
895 prealloc = alloc_extent_state_atomic(prealloc);
897 err = split_state(tree, state, prealloc, end + 1);
899 extent_io_tree_panic(tree, err);
901 set_state_bits(tree, prealloc, &bits);
902 cache_state(prealloc, cached_state);
903 merge_state(tree, prealloc);
911 spin_unlock(&tree->lock);
913 free_extent_state(prealloc);
920 spin_unlock(&tree->lock);
921 if (mask & __GFP_WAIT)
926 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
927 u64 *failed_start, struct extent_state **cached_state,
930 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
936 * convert_extent_bit - convert all bits in a given range from one bit to
938 * @tree: the io tree to search
939 * @start: the start offset in bytes
940 * @end: the end offset in bytes (inclusive)
941 * @bits: the bits to set in this range
942 * @clear_bits: the bits to clear in this range
943 * @cached_state: state that we're going to cache
944 * @mask: the allocation mask
946 * This will go through and set bits for the given range. If any states exist
947 * already in this range they are set with the given bit and cleared of the
948 * clear_bits. This is only meant to be used by things that are mergeable, ie
949 * converting from say DELALLOC to DIRTY. This is not meant to be used with
950 * boundary bits like LOCK.
952 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
953 int bits, int clear_bits,
954 struct extent_state **cached_state, gfp_t mask)
956 struct extent_state *state;
957 struct extent_state *prealloc = NULL;
958 struct rb_node *node;
964 if (!prealloc && (mask & __GFP_WAIT)) {
965 prealloc = alloc_extent_state(mask);
970 spin_lock(&tree->lock);
971 if (cached_state && *cached_state) {
972 state = *cached_state;
973 if (state->start <= start && state->end > start &&
975 node = &state->rb_node;
981 * this search will find all the extents that end after
984 node = tree_search(tree, start);
986 prealloc = alloc_extent_state_atomic(prealloc);
991 err = insert_state(tree, prealloc, start, end, &bits);
994 extent_io_tree_panic(tree, err);
997 state = rb_entry(node, struct extent_state, rb_node);
999 last_start = state->start;
1000 last_end = state->end;
1003 * | ---- desired range ---- |
1006 * Just lock what we found and keep going
1008 if (state->start == start && state->end <= end) {
1009 set_state_bits(tree, state, &bits);
1010 cache_state(state, cached_state);
1011 state = clear_state_bit(tree, state, &clear_bits, 0);
1012 if (last_end == (u64)-1)
1014 start = last_end + 1;
1015 if (start < end && state && state->start == start &&
1022 * | ---- desired range ---- |
1025 * | ------------- state -------------- |
1027 * We need to split the extent we found, and may flip bits on
1030 * If the extent we found extends past our
1031 * range, we just split and search again. It'll get split
1032 * again the next time though.
1034 * If the extent we found is inside our range, we set the
1035 * desired bit on it.
1037 if (state->start < start) {
1038 prealloc = alloc_extent_state_atomic(prealloc);
1043 err = split_state(tree, state, prealloc, start);
1045 extent_io_tree_panic(tree, err);
1049 if (state->end <= end) {
1050 set_state_bits(tree, state, &bits);
1051 cache_state(state, cached_state);
1052 state = clear_state_bit(tree, state, &clear_bits, 0);
1053 if (last_end == (u64)-1)
1055 start = last_end + 1;
1056 if (start < end && state && state->start == start &&
1063 * | ---- desired range ---- |
1064 * | state | or | state |
1066 * There's a hole, we need to insert something in it and
1067 * ignore the extent we found.
1069 if (state->start > start) {
1071 if (end < last_start)
1074 this_end = last_start - 1;
1076 prealloc = alloc_extent_state_atomic(prealloc);
1083 * Avoid to free 'prealloc' if it can be merged with
1086 err = insert_state(tree, prealloc, start, this_end,
1089 extent_io_tree_panic(tree, err);
1090 cache_state(prealloc, cached_state);
1092 start = this_end + 1;
1096 * | ---- desired range ---- |
1098 * We need to split the extent, and set the bit
1101 if (state->start <= end && state->end > end) {
1102 prealloc = alloc_extent_state_atomic(prealloc);
1108 err = split_state(tree, state, prealloc, end + 1);
1110 extent_io_tree_panic(tree, err);
1112 set_state_bits(tree, prealloc, &bits);
1113 cache_state(prealloc, cached_state);
1114 clear_state_bit(tree, prealloc, &clear_bits, 0);
1122 spin_unlock(&tree->lock);
1124 free_extent_state(prealloc);
1131 spin_unlock(&tree->lock);
1132 if (mask & __GFP_WAIT)
1137 /* wrappers around set/clear extent bit */
1138 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1141 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
1145 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1146 int bits, gfp_t mask)
1148 return set_extent_bit(tree, start, end, bits, NULL,
1152 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1153 int bits, gfp_t mask)
1155 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
1158 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
1159 struct extent_state **cached_state, gfp_t mask)
1161 return set_extent_bit(tree, start, end,
1162 EXTENT_DELALLOC | EXTENT_UPTODATE,
1163 NULL, cached_state, mask);
1166 int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1167 struct extent_state **cached_state, gfp_t mask)
1169 return set_extent_bit(tree, start, end,
1170 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1171 NULL, cached_state, mask);
1174 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1177 return clear_extent_bit(tree, start, end,
1178 EXTENT_DIRTY | EXTENT_DELALLOC |
1179 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
1182 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1185 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
1189 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1190 struct extent_state **cached_state, gfp_t mask)
1192 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1193 cached_state, mask);
1196 int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1197 struct extent_state **cached_state, gfp_t mask)
1199 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1200 cached_state, mask);
1204 * either insert or lock state struct between start and end use mask to tell
1205 * us if waiting is desired.
1207 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1208 int bits, struct extent_state **cached_state)
1213 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1214 EXTENT_LOCKED, &failed_start,
1215 cached_state, GFP_NOFS);
1216 if (err == -EEXIST) {
1217 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1218 start = failed_start;
1221 WARN_ON(start > end);
1226 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1228 return lock_extent_bits(tree, start, end, 0, NULL);
1231 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1236 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1237 &failed_start, NULL, GFP_NOFS);
1238 if (err == -EEXIST) {
1239 if (failed_start > start)
1240 clear_extent_bit(tree, start, failed_start - 1,
1241 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
1247 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1248 struct extent_state **cached, gfp_t mask)
1250 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1254 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1256 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1261 * helper function to set both pages and extents in the tree writeback
1263 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1265 unsigned long index = start >> PAGE_CACHE_SHIFT;
1266 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1269 while (index <= end_index) {
1270 page = find_get_page(tree->mapping, index);
1271 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1272 set_page_writeback(page);
1273 page_cache_release(page);
1279 /* find the first state struct with 'bits' set after 'start', and
1280 * return it. tree->lock must be held. NULL will returned if
1281 * nothing was found after 'start'
1283 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1284 u64 start, int bits)
1286 struct rb_node *node;
1287 struct extent_state *state;
1290 * this search will find all the extents that end after
1293 node = tree_search(tree, start);
1298 state = rb_entry(node, struct extent_state, rb_node);
1299 if (state->end >= start && (state->state & bits))
1302 node = rb_next(node);
1311 * find the first offset in the io tree with 'bits' set. zero is
1312 * returned if we find something, and *start_ret and *end_ret are
1313 * set to reflect the state struct that was found.
1315 * If nothing was found, 1 is returned. If found something, return 0.
1317 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1318 u64 *start_ret, u64 *end_ret, int bits,
1319 struct extent_state **cached_state)
1321 struct extent_state *state;
1325 spin_lock(&tree->lock);
1326 if (cached_state && *cached_state) {
1327 state = *cached_state;
1328 if (state->end == start - 1 && state->tree) {
1329 n = rb_next(&state->rb_node);
1331 state = rb_entry(n, struct extent_state,
1333 if (state->state & bits)
1337 free_extent_state(*cached_state);
1338 *cached_state = NULL;
1341 free_extent_state(*cached_state);
1342 *cached_state = NULL;
1345 state = find_first_extent_bit_state(tree, start, bits);
1348 cache_state(state, cached_state);
1349 *start_ret = state->start;
1350 *end_ret = state->end;
1354 spin_unlock(&tree->lock);
1359 * find a contiguous range of bytes in the file marked as delalloc, not
1360 * more than 'max_bytes'. start and end are used to return the range,
1362 * 1 is returned if we find something, 0 if nothing was in the tree
1364 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1365 u64 *start, u64 *end, u64 max_bytes,
1366 struct extent_state **cached_state)
1368 struct rb_node *node;
1369 struct extent_state *state;
1370 u64 cur_start = *start;
1372 u64 total_bytes = 0;
1374 spin_lock(&tree->lock);
1377 * this search will find all the extents that end after
1380 node = tree_search(tree, cur_start);
1388 state = rb_entry(node, struct extent_state, rb_node);
1389 if (found && (state->start != cur_start ||
1390 (state->state & EXTENT_BOUNDARY))) {
1393 if (!(state->state & EXTENT_DELALLOC)) {
1399 *start = state->start;
1400 *cached_state = state;
1401 atomic_inc(&state->refs);
1405 cur_start = state->end + 1;
1406 node = rb_next(node);
1409 total_bytes += state->end - state->start + 1;
1410 if (total_bytes >= max_bytes)
1414 spin_unlock(&tree->lock);
1418 static noinline void __unlock_for_delalloc(struct inode *inode,
1419 struct page *locked_page,
1423 struct page *pages[16];
1424 unsigned long index = start >> PAGE_CACHE_SHIFT;
1425 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1426 unsigned long nr_pages = end_index - index + 1;
1429 if (index == locked_page->index && end_index == index)
1432 while (nr_pages > 0) {
1433 ret = find_get_pages_contig(inode->i_mapping, index,
1434 min_t(unsigned long, nr_pages,
1435 ARRAY_SIZE(pages)), pages);
1436 for (i = 0; i < ret; i++) {
1437 if (pages[i] != locked_page)
1438 unlock_page(pages[i]);
1439 page_cache_release(pages[i]);
1447 static noinline int lock_delalloc_pages(struct inode *inode,
1448 struct page *locked_page,
1452 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1453 unsigned long start_index = index;
1454 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1455 unsigned long pages_locked = 0;
1456 struct page *pages[16];
1457 unsigned long nrpages;
1461 /* the caller is responsible for locking the start index */
1462 if (index == locked_page->index && index == end_index)
1465 /* skip the page at the start index */
1466 nrpages = end_index - index + 1;
1467 while (nrpages > 0) {
1468 ret = find_get_pages_contig(inode->i_mapping, index,
1469 min_t(unsigned long,
1470 nrpages, ARRAY_SIZE(pages)), pages);
1475 /* now we have an array of pages, lock them all */
1476 for (i = 0; i < ret; i++) {
1478 * the caller is taking responsibility for
1481 if (pages[i] != locked_page) {
1482 lock_page(pages[i]);
1483 if (!PageDirty(pages[i]) ||
1484 pages[i]->mapping != inode->i_mapping) {
1486 unlock_page(pages[i]);
1487 page_cache_release(pages[i]);
1491 page_cache_release(pages[i]);
1500 if (ret && pages_locked) {
1501 __unlock_for_delalloc(inode, locked_page,
1503 ((u64)(start_index + pages_locked - 1)) <<
1510 * find a contiguous range of bytes in the file marked as delalloc, not
1511 * more than 'max_bytes'. start and end are used to return the range,
1513 * 1 is returned if we find something, 0 if nothing was in the tree
1515 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1516 struct extent_io_tree *tree,
1517 struct page *locked_page,
1518 u64 *start, u64 *end,
1524 struct extent_state *cached_state = NULL;
1529 /* step one, find a bunch of delalloc bytes starting at start */
1530 delalloc_start = *start;
1532 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1533 max_bytes, &cached_state);
1534 if (!found || delalloc_end <= *start) {
1535 *start = delalloc_start;
1536 *end = delalloc_end;
1537 free_extent_state(cached_state);
1542 * start comes from the offset of locked_page. We have to lock
1543 * pages in order, so we can't process delalloc bytes before
1546 if (delalloc_start < *start)
1547 delalloc_start = *start;
1550 * make sure to limit the number of pages we try to lock down
1553 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1554 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1556 /* step two, lock all the pages after the page that has start */
1557 ret = lock_delalloc_pages(inode, locked_page,
1558 delalloc_start, delalloc_end);
1559 if (ret == -EAGAIN) {
1560 /* some of the pages are gone, lets avoid looping by
1561 * shortening the size of the delalloc range we're searching
1563 free_extent_state(cached_state);
1565 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1566 max_bytes = PAGE_CACHE_SIZE - offset;
1574 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
1576 /* step three, lock the state bits for the whole range */
1577 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
1579 /* then test to make sure it is all still delalloc */
1580 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1581 EXTENT_DELALLOC, 1, cached_state);
1583 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1584 &cached_state, GFP_NOFS);
1585 __unlock_for_delalloc(inode, locked_page,
1586 delalloc_start, delalloc_end);
1590 free_extent_state(cached_state);
1591 *start = delalloc_start;
1592 *end = delalloc_end;
1597 int extent_clear_unlock_delalloc(struct inode *inode,
1598 struct extent_io_tree *tree,
1599 u64 start, u64 end, struct page *locked_page,
1603 struct page *pages[16];
1604 unsigned long index = start >> PAGE_CACHE_SHIFT;
1605 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1606 unsigned long nr_pages = end_index - index + 1;
1610 if (op & EXTENT_CLEAR_UNLOCK)
1611 clear_bits |= EXTENT_LOCKED;
1612 if (op & EXTENT_CLEAR_DIRTY)
1613 clear_bits |= EXTENT_DIRTY;
1615 if (op & EXTENT_CLEAR_DELALLOC)
1616 clear_bits |= EXTENT_DELALLOC;
1618 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1619 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1620 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1621 EXTENT_SET_PRIVATE2)))
1624 while (nr_pages > 0) {
1625 ret = find_get_pages_contig(inode->i_mapping, index,
1626 min_t(unsigned long,
1627 nr_pages, ARRAY_SIZE(pages)), pages);
1628 for (i = 0; i < ret; i++) {
1630 if (op & EXTENT_SET_PRIVATE2)
1631 SetPagePrivate2(pages[i]);
1633 if (pages[i] == locked_page) {
1634 page_cache_release(pages[i]);
1637 if (op & EXTENT_CLEAR_DIRTY)
1638 clear_page_dirty_for_io(pages[i]);
1639 if (op & EXTENT_SET_WRITEBACK)
1640 set_page_writeback(pages[i]);
1641 if (op & EXTENT_END_WRITEBACK)
1642 end_page_writeback(pages[i]);
1643 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1644 unlock_page(pages[i]);
1645 page_cache_release(pages[i]);
1655 * count the number of bytes in the tree that have a given bit(s)
1656 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1657 * cached. The total number found is returned.
1659 u64 count_range_bits(struct extent_io_tree *tree,
1660 u64 *start, u64 search_end, u64 max_bytes,
1661 unsigned long bits, int contig)
1663 struct rb_node *node;
1664 struct extent_state *state;
1665 u64 cur_start = *start;
1666 u64 total_bytes = 0;
1670 if (search_end <= cur_start) {
1675 spin_lock(&tree->lock);
1676 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1677 total_bytes = tree->dirty_bytes;
1681 * this search will find all the extents that end after
1684 node = tree_search(tree, cur_start);
1689 state = rb_entry(node, struct extent_state, rb_node);
1690 if (state->start > search_end)
1692 if (contig && found && state->start > last + 1)
1694 if (state->end >= cur_start && (state->state & bits) == bits) {
1695 total_bytes += min(search_end, state->end) + 1 -
1696 max(cur_start, state->start);
1697 if (total_bytes >= max_bytes)
1700 *start = max(cur_start, state->start);
1704 } else if (contig && found) {
1707 node = rb_next(node);
1712 spin_unlock(&tree->lock);
1717 * set the private field for a given byte offset in the tree. If there isn't
1718 * an extent_state there already, this does nothing.
1720 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1722 struct rb_node *node;
1723 struct extent_state *state;
1726 spin_lock(&tree->lock);
1728 * this search will find all the extents that end after
1731 node = tree_search(tree, start);
1736 state = rb_entry(node, struct extent_state, rb_node);
1737 if (state->start != start) {
1741 state->private = private;
1743 spin_unlock(&tree->lock);
1747 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1749 struct rb_node *node;
1750 struct extent_state *state;
1753 spin_lock(&tree->lock);
1755 * this search will find all the extents that end after
1758 node = tree_search(tree, start);
1763 state = rb_entry(node, struct extent_state, rb_node);
1764 if (state->start != start) {
1768 *private = state->private;
1770 spin_unlock(&tree->lock);
1775 * searches a range in the state tree for a given mask.
1776 * If 'filled' == 1, this returns 1 only if every extent in the tree
1777 * has the bits set. Otherwise, 1 is returned if any bit in the
1778 * range is found set.
1780 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1781 int bits, int filled, struct extent_state *cached)
1783 struct extent_state *state = NULL;
1784 struct rb_node *node;
1787 spin_lock(&tree->lock);
1788 if (cached && cached->tree && cached->start <= start &&
1789 cached->end > start)
1790 node = &cached->rb_node;
1792 node = tree_search(tree, start);
1793 while (node && start <= end) {
1794 state = rb_entry(node, struct extent_state, rb_node);
1796 if (filled && state->start > start) {
1801 if (state->start > end)
1804 if (state->state & bits) {
1808 } else if (filled) {
1813 if (state->end == (u64)-1)
1816 start = state->end + 1;
1819 node = rb_next(node);
1826 spin_unlock(&tree->lock);
1831 * helper function to set a given page up to date if all the
1832 * extents in the tree for that page are up to date
1834 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
1836 u64 start = page_offset(page);
1837 u64 end = start + PAGE_CACHE_SIZE - 1;
1838 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1839 SetPageUptodate(page);
1843 * helper function to unlock a page if all the extents in the tree
1844 * for that page are unlocked
1846 static void check_page_locked(struct extent_io_tree *tree, struct page *page)
1848 u64 start = page_offset(page);
1849 u64 end = start + PAGE_CACHE_SIZE - 1;
1850 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1855 * helper function to end page writeback if all the extents
1856 * in the tree for that page are done with writeback
1858 static void check_page_writeback(struct extent_io_tree *tree,
1861 end_page_writeback(page);
1865 * When IO fails, either with EIO or csum verification fails, we
1866 * try other mirrors that might have a good copy of the data. This
1867 * io_failure_record is used to record state as we go through all the
1868 * mirrors. If another mirror has good data, the page is set up to date
1869 * and things continue. If a good mirror can't be found, the original
1870 * bio end_io callback is called to indicate things have failed.
1872 struct io_failure_record {
1877 unsigned long bio_flags;
1883 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1888 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1890 set_state_private(failure_tree, rec->start, 0);
1891 ret = clear_extent_bits(failure_tree, rec->start,
1892 rec->start + rec->len - 1,
1893 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1897 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1898 rec->start + rec->len - 1,
1899 EXTENT_DAMAGED, GFP_NOFS);
1907 static void repair_io_failure_callback(struct bio *bio, int err)
1909 complete(bio->bi_private);
1913 * this bypasses the standard btrfs submit functions deliberately, as
1914 * the standard behavior is to write all copies in a raid setup. here we only
1915 * want to write the one bad copy. so we do the mapping for ourselves and issue
1916 * submit_bio directly.
1917 * to avoid any synchronization issues, wait for the data after writing, which
1918 * actually prevents the read that triggered the error from finishing.
1919 * currently, there can be no more than two copies of every data bit. thus,
1920 * exactly one rewrite is required.
1922 int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
1923 u64 length, u64 logical, struct page *page,
1927 struct btrfs_device *dev;
1928 DECLARE_COMPLETION_ONSTACK(compl);
1931 struct btrfs_bio *bbio = NULL;
1932 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
1935 BUG_ON(!mirror_num);
1937 /* we can't repair anything in raid56 yet */
1938 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
1941 bio = bio_alloc(GFP_NOFS, 1);
1944 bio->bi_private = &compl;
1945 bio->bi_end_io = repair_io_failure_callback;
1947 map_length = length;
1949 ret = btrfs_map_block(fs_info, WRITE, logical,
1950 &map_length, &bbio, mirror_num);
1955 BUG_ON(mirror_num != bbio->mirror_num);
1956 sector = bbio->stripes[mirror_num-1].physical >> 9;
1957 bio->bi_sector = sector;
1958 dev = bbio->stripes[mirror_num-1].dev;
1960 if (!dev || !dev->bdev || !dev->writeable) {
1964 bio->bi_bdev = dev->bdev;
1965 bio_add_page(bio, page, length, start - page_offset(page));
1966 btrfsic_submit_bio(WRITE_SYNC, bio);
1967 wait_for_completion(&compl);
1969 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1970 /* try to remap that extent elsewhere? */
1972 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
1976 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
1977 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
1978 start, rcu_str_deref(dev->name), sector);
1984 int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1987 u64 start = eb->start;
1988 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
1991 for (i = 0; i < num_pages; i++) {
1992 struct page *p = extent_buffer_page(eb, i);
1993 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
1994 start, p, mirror_num);
1997 start += PAGE_CACHE_SIZE;
2004 * each time an IO finishes, we do a fast check in the IO failure tree
2005 * to see if we need to process or clean up an io_failure_record
2007 static int clean_io_failure(u64 start, struct page *page)
2010 u64 private_failure;
2011 struct io_failure_record *failrec;
2012 struct btrfs_fs_info *fs_info;
2013 struct extent_state *state;
2017 struct inode *inode = page->mapping->host;
2020 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2021 (u64)-1, 1, EXTENT_DIRTY, 0);
2025 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2030 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2031 BUG_ON(!failrec->this_mirror);
2033 if (failrec->in_validation) {
2034 /* there was no real error, just free the record */
2035 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2041 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2042 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2045 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2047 if (state && state->start == failrec->start) {
2048 fs_info = BTRFS_I(inode)->root->fs_info;
2049 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2051 if (num_copies > 1) {
2052 ret = repair_io_failure(fs_info, start, failrec->len,
2053 failrec->logical, page,
2054 failrec->failed_mirror);
2062 ret = free_io_failure(inode, failrec, did_repair);
2068 * this is a generic handler for readpage errors (default
2069 * readpage_io_failed_hook). if other copies exist, read those and write back
2070 * good data to the failed position. does not investigate in remapping the
2071 * failed extent elsewhere, hoping the device will be smart enough to do this as
2075 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2076 u64 start, u64 end, int failed_mirror,
2077 struct extent_state *state)
2079 struct io_failure_record *failrec = NULL;
2081 struct extent_map *em;
2082 struct inode *inode = page->mapping->host;
2083 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2084 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2085 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2092 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2094 ret = get_state_private(failure_tree, start, &private);
2096 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2099 failrec->start = start;
2100 failrec->len = end - start + 1;
2101 failrec->this_mirror = 0;
2102 failrec->bio_flags = 0;
2103 failrec->in_validation = 0;
2105 read_lock(&em_tree->lock);
2106 em = lookup_extent_mapping(em_tree, start, failrec->len);
2108 read_unlock(&em_tree->lock);
2113 if (em->start > start || em->start + em->len < start) {
2114 free_extent_map(em);
2117 read_unlock(&em_tree->lock);
2123 logical = start - em->start;
2124 logical = em->block_start + logical;
2125 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2126 logical = em->block_start;
2127 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2128 extent_set_compress_type(&failrec->bio_flags,
2131 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2132 "len=%llu\n", logical, start, failrec->len);
2133 failrec->logical = logical;
2134 free_extent_map(em);
2136 /* set the bits in the private failure tree */
2137 ret = set_extent_bits(failure_tree, start, end,
2138 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2140 ret = set_state_private(failure_tree, start,
2141 (u64)(unsigned long)failrec);
2142 /* set the bits in the inode's tree */
2144 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2151 failrec = (struct io_failure_record *)(unsigned long)private;
2152 pr_debug("bio_readpage_error: (found) logical=%llu, "
2153 "start=%llu, len=%llu, validation=%d\n",
2154 failrec->logical, failrec->start, failrec->len,
2155 failrec->in_validation);
2157 * when data can be on disk more than twice, add to failrec here
2158 * (e.g. with a list for failed_mirror) to make
2159 * clean_io_failure() clean all those errors at once.
2162 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2163 failrec->logical, failrec->len);
2164 if (num_copies == 1) {
2166 * we only have a single copy of the data, so don't bother with
2167 * all the retry and error correction code that follows. no
2168 * matter what the error is, it is very likely to persist.
2170 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2171 "state=%p, num_copies=%d, next_mirror %d, "
2172 "failed_mirror %d\n", state, num_copies,
2173 failrec->this_mirror, failed_mirror);
2174 free_io_failure(inode, failrec, 0);
2179 spin_lock(&tree->lock);
2180 state = find_first_extent_bit_state(tree, failrec->start,
2182 if (state && state->start != failrec->start)
2184 spin_unlock(&tree->lock);
2188 * there are two premises:
2189 * a) deliver good data to the caller
2190 * b) correct the bad sectors on disk
2192 if (failed_bio->bi_vcnt > 1) {
2194 * to fulfill b), we need to know the exact failing sectors, as
2195 * we don't want to rewrite any more than the failed ones. thus,
2196 * we need separate read requests for the failed bio
2198 * if the following BUG_ON triggers, our validation request got
2199 * merged. we need separate requests for our algorithm to work.
2201 BUG_ON(failrec->in_validation);
2202 failrec->in_validation = 1;
2203 failrec->this_mirror = failed_mirror;
2204 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2207 * we're ready to fulfill a) and b) alongside. get a good copy
2208 * of the failed sector and if we succeed, we have setup
2209 * everything for repair_io_failure to do the rest for us.
2211 if (failrec->in_validation) {
2212 BUG_ON(failrec->this_mirror != failed_mirror);
2213 failrec->in_validation = 0;
2214 failrec->this_mirror = 0;
2216 failrec->failed_mirror = failed_mirror;
2217 failrec->this_mirror++;
2218 if (failrec->this_mirror == failed_mirror)
2219 failrec->this_mirror++;
2220 read_mode = READ_SYNC;
2223 if (!state || failrec->this_mirror > num_copies) {
2224 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2225 "next_mirror %d, failed_mirror %d\n", state,
2226 num_copies, failrec->this_mirror, failed_mirror);
2227 free_io_failure(inode, failrec, 0);
2231 bio = bio_alloc(GFP_NOFS, 1);
2233 free_io_failure(inode, failrec, 0);
2236 bio->bi_private = state;
2237 bio->bi_end_io = failed_bio->bi_end_io;
2238 bio->bi_sector = failrec->logical >> 9;
2239 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2242 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2244 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2245 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2246 failrec->this_mirror, num_copies, failrec->in_validation);
2248 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2249 failrec->this_mirror,
2250 failrec->bio_flags, 0);
2254 /* lots and lots of room for performance fixes in the end_bio funcs */
2256 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2258 int uptodate = (err == 0);
2259 struct extent_io_tree *tree;
2262 tree = &BTRFS_I(page->mapping->host)->io_tree;
2264 if (tree->ops && tree->ops->writepage_end_io_hook) {
2265 ret = tree->ops->writepage_end_io_hook(page, start,
2266 end, NULL, uptodate);
2272 ClearPageUptodate(page);
2279 * after a writepage IO is done, we need to:
2280 * clear the uptodate bits on error
2281 * clear the writeback bits in the extent tree for this IO
2282 * end_page_writeback if the page has no more pending IO
2284 * Scheduling is not allowed, so the extent state tree is expected
2285 * to have one and only one object corresponding to this IO.
2287 static void end_bio_extent_writepage(struct bio *bio, int err)
2289 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2290 struct extent_io_tree *tree;
2296 struct page *page = bvec->bv_page;
2297 tree = &BTRFS_I(page->mapping->host)->io_tree;
2299 start = page_offset(page) + bvec->bv_offset;
2300 end = start + bvec->bv_len - 1;
2302 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2307 if (--bvec >= bio->bi_io_vec)
2308 prefetchw(&bvec->bv_page->flags);
2310 if (end_extent_writepage(page, err, start, end))
2314 end_page_writeback(page);
2316 check_page_writeback(tree, page);
2317 } while (bvec >= bio->bi_io_vec);
2323 * after a readpage IO is done, we need to:
2324 * clear the uptodate bits on error
2325 * set the uptodate bits if things worked
2326 * set the page up to date if all extents in the tree are uptodate
2327 * clear the lock bit in the extent tree
2328 * unlock the page if there are no other extents locked for it
2330 * Scheduling is not allowed, so the extent state tree is expected
2331 * to have one and only one object corresponding to this IO.
2333 static void end_bio_extent_readpage(struct bio *bio, int err)
2335 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2336 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2337 struct bio_vec *bvec = bio->bi_io_vec;
2338 struct extent_io_tree *tree;
2349 struct page *page = bvec->bv_page;
2350 struct extent_state *cached = NULL;
2351 struct extent_state *state;
2353 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2354 "mirror=%ld\n", (u64)bio->bi_sector, err,
2355 (long int)bio->bi_bdev);
2356 tree = &BTRFS_I(page->mapping->host)->io_tree;
2358 start = page_offset(page) + bvec->bv_offset;
2359 end = start + bvec->bv_len - 1;
2361 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2366 if (++bvec <= bvec_end)
2367 prefetchw(&bvec->bv_page->flags);
2369 spin_lock(&tree->lock);
2370 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2371 if (state && state->start == start) {
2373 * take a reference on the state, unlock will drop
2376 cache_state(state, &cached);
2378 spin_unlock(&tree->lock);
2380 mirror = (int)(unsigned long)bio->bi_bdev;
2381 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2382 ret = tree->ops->readpage_end_io_hook(page, start, end,
2387 clean_io_failure(start, page);
2390 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
2391 ret = tree->ops->readpage_io_failed_hook(page, mirror);
2393 test_bit(BIO_UPTODATE, &bio->bi_flags))
2395 } else if (!uptodate) {
2397 * The generic bio_readpage_error handles errors the
2398 * following way: If possible, new read requests are
2399 * created and submitted and will end up in
2400 * end_bio_extent_readpage as well (if we're lucky, not
2401 * in the !uptodate case). In that case it returns 0 and
2402 * we just go on with the next page in our bio. If it
2403 * can't handle the error it will return -EIO and we
2404 * remain responsible for that page.
2406 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
2409 test_bit(BIO_UPTODATE, &bio->bi_flags);
2412 uncache_state(&cached);
2417 if (uptodate && tree->track_uptodate) {
2418 set_extent_uptodate(tree, start, end, &cached,
2421 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2425 SetPageUptodate(page);
2427 ClearPageUptodate(page);
2433 check_page_uptodate(tree, page);
2435 ClearPageUptodate(page);
2438 check_page_locked(tree, page);
2440 } while (bvec <= bvec_end);
2446 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2451 bio = bio_alloc(gfp_flags, nr_vecs);
2453 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2454 while (!bio && (nr_vecs /= 2))
2455 bio = bio_alloc(gfp_flags, nr_vecs);
2460 bio->bi_bdev = bdev;
2461 bio->bi_sector = first_sector;
2466 static int __must_check submit_one_bio(int rw, struct bio *bio,
2467 int mirror_num, unsigned long bio_flags)
2470 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2471 struct page *page = bvec->bv_page;
2472 struct extent_io_tree *tree = bio->bi_private;
2475 start = page_offset(page) + bvec->bv_offset;
2477 bio->bi_private = NULL;
2481 if (tree->ops && tree->ops->submit_bio_hook)
2482 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2483 mirror_num, bio_flags, start);
2485 btrfsic_submit_bio(rw, bio);
2487 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2493 static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
2494 unsigned long offset, size_t size, struct bio *bio,
2495 unsigned long bio_flags)
2498 if (tree->ops && tree->ops->merge_bio_hook)
2499 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
2506 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2507 struct page *page, sector_t sector,
2508 size_t size, unsigned long offset,
2509 struct block_device *bdev,
2510 struct bio **bio_ret,
2511 unsigned long max_pages,
2512 bio_end_io_t end_io_func,
2514 unsigned long prev_bio_flags,
2515 unsigned long bio_flags)
2521 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2522 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2523 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2525 if (bio_ret && *bio_ret) {
2528 contig = bio->bi_sector == sector;
2530 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2533 if (prev_bio_flags != bio_flags || !contig ||
2534 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
2535 bio_add_page(bio, page, page_size, offset) < page_size) {
2536 ret = submit_one_bio(rw, bio, mirror_num,
2545 if (this_compressed)
2548 nr = bio_get_nr_vecs(bdev);
2550 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2554 bio_add_page(bio, page, page_size, offset);
2555 bio->bi_end_io = end_io_func;
2556 bio->bi_private = tree;
2561 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2566 void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2568 if (!PagePrivate(page)) {
2569 SetPagePrivate(page);
2570 page_cache_get(page);
2571 set_page_private(page, (unsigned long)eb);
2573 WARN_ON(page->private != (unsigned long)eb);
2577 void set_page_extent_mapped(struct page *page)
2579 if (!PagePrivate(page)) {
2580 SetPagePrivate(page);
2581 page_cache_get(page);
2582 set_page_private(page, EXTENT_PAGE_PRIVATE);
2587 * basic readpage implementation. Locked extent state structs are inserted
2588 * into the tree that are removed when the IO is done (by the end_io
2590 * XXX JDM: This needs looking at to ensure proper page locking
2592 static int __extent_read_full_page(struct extent_io_tree *tree,
2594 get_extent_t *get_extent,
2595 struct bio **bio, int mirror_num,
2596 unsigned long *bio_flags)
2598 struct inode *inode = page->mapping->host;
2599 u64 start = page_offset(page);
2600 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2604 u64 last_byte = i_size_read(inode);
2608 struct extent_map *em;
2609 struct block_device *bdev;
2610 struct btrfs_ordered_extent *ordered;
2613 size_t pg_offset = 0;
2615 size_t disk_io_size;
2616 size_t blocksize = inode->i_sb->s_blocksize;
2617 unsigned long this_bio_flag = 0;
2619 set_page_extent_mapped(page);
2621 if (!PageUptodate(page)) {
2622 if (cleancache_get_page(page) == 0) {
2623 BUG_ON(blocksize != PAGE_SIZE);
2630 lock_extent(tree, start, end);
2631 ordered = btrfs_lookup_ordered_extent(inode, start);
2634 unlock_extent(tree, start, end);
2635 btrfs_start_ordered_extent(inode, ordered, 1);
2636 btrfs_put_ordered_extent(ordered);
2639 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2641 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2644 iosize = PAGE_CACHE_SIZE - zero_offset;
2645 userpage = kmap_atomic(page);
2646 memset(userpage + zero_offset, 0, iosize);
2647 flush_dcache_page(page);
2648 kunmap_atomic(userpage);
2651 while (cur <= end) {
2652 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2654 if (cur >= last_byte) {
2656 struct extent_state *cached = NULL;
2658 iosize = PAGE_CACHE_SIZE - pg_offset;
2659 userpage = kmap_atomic(page);
2660 memset(userpage + pg_offset, 0, iosize);
2661 flush_dcache_page(page);
2662 kunmap_atomic(userpage);
2663 set_extent_uptodate(tree, cur, cur + iosize - 1,
2665 unlock_extent_cached(tree, cur, cur + iosize - 1,
2669 em = get_extent(inode, page, pg_offset, cur,
2671 if (IS_ERR_OR_NULL(em)) {
2673 unlock_extent(tree, cur, end);
2676 extent_offset = cur - em->start;
2677 BUG_ON(extent_map_end(em) <= cur);
2680 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2681 this_bio_flag = EXTENT_BIO_COMPRESSED;
2682 extent_set_compress_type(&this_bio_flag,
2686 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2687 cur_end = min(extent_map_end(em) - 1, end);
2688 iosize = ALIGN(iosize, blocksize);
2689 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2690 disk_io_size = em->block_len;
2691 sector = em->block_start >> 9;
2693 sector = (em->block_start + extent_offset) >> 9;
2694 disk_io_size = iosize;
2697 block_start = em->block_start;
2698 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2699 block_start = EXTENT_MAP_HOLE;
2700 free_extent_map(em);
2703 /* we've found a hole, just zero and go on */
2704 if (block_start == EXTENT_MAP_HOLE) {
2706 struct extent_state *cached = NULL;
2708 userpage = kmap_atomic(page);
2709 memset(userpage + pg_offset, 0, iosize);
2710 flush_dcache_page(page);
2711 kunmap_atomic(userpage);
2713 set_extent_uptodate(tree, cur, cur + iosize - 1,
2715 unlock_extent_cached(tree, cur, cur + iosize - 1,
2718 pg_offset += iosize;
2721 /* the get_extent function already copied into the page */
2722 if (test_range_bit(tree, cur, cur_end,
2723 EXTENT_UPTODATE, 1, NULL)) {
2724 check_page_uptodate(tree, page);
2725 unlock_extent(tree, cur, cur + iosize - 1);
2727 pg_offset += iosize;
2730 /* we have an inline extent but it didn't get marked up
2731 * to date. Error out
2733 if (block_start == EXTENT_MAP_INLINE) {
2735 unlock_extent(tree, cur, cur + iosize - 1);
2737 pg_offset += iosize;
2742 ret = submit_extent_page(READ, tree, page,
2743 sector, disk_io_size, pg_offset,
2745 end_bio_extent_readpage, mirror_num,
2750 *bio_flags = this_bio_flag;
2753 unlock_extent(tree, cur, cur + iosize - 1);
2756 pg_offset += iosize;
2760 if (!PageError(page))
2761 SetPageUptodate(page);
2767 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2768 get_extent_t *get_extent, int mirror_num)
2770 struct bio *bio = NULL;
2771 unsigned long bio_flags = 0;
2774 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2777 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2781 static noinline void update_nr_written(struct page *page,
2782 struct writeback_control *wbc,
2783 unsigned long nr_written)
2785 wbc->nr_to_write -= nr_written;
2786 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2787 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2788 page->mapping->writeback_index = page->index + nr_written;
2792 * the writepage semantics are similar to regular writepage. extent
2793 * records are inserted to lock ranges in the tree, and as dirty areas
2794 * are found, they are marked writeback. Then the lock bits are removed
2795 * and the end_io handler clears the writeback ranges
2797 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2800 struct inode *inode = page->mapping->host;
2801 struct extent_page_data *epd = data;
2802 struct extent_io_tree *tree = epd->tree;
2803 u64 start = page_offset(page);
2805 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2809 u64 last_byte = i_size_read(inode);
2813 struct extent_state *cached_state = NULL;
2814 struct extent_map *em;
2815 struct block_device *bdev;
2818 size_t pg_offset = 0;
2820 loff_t i_size = i_size_read(inode);
2821 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2827 unsigned long nr_written = 0;
2828 bool fill_delalloc = true;
2830 if (wbc->sync_mode == WB_SYNC_ALL)
2831 write_flags = WRITE_SYNC;
2833 write_flags = WRITE;
2835 trace___extent_writepage(page, inode, wbc);
2837 WARN_ON(!PageLocked(page));
2839 ClearPageError(page);
2841 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2842 if (page->index > end_index ||
2843 (page->index == end_index && !pg_offset)) {
2844 page->mapping->a_ops->invalidatepage(page, 0);
2849 if (page->index == end_index) {
2852 userpage = kmap_atomic(page);
2853 memset(userpage + pg_offset, 0,
2854 PAGE_CACHE_SIZE - pg_offset);
2855 kunmap_atomic(userpage);
2856 flush_dcache_page(page);
2860 set_page_extent_mapped(page);
2862 if (!tree->ops || !tree->ops->fill_delalloc)
2863 fill_delalloc = false;
2865 delalloc_start = start;
2868 if (!epd->extent_locked && fill_delalloc) {
2869 u64 delalloc_to_write = 0;
2871 * make sure the wbc mapping index is at least updated
2874 update_nr_written(page, wbc, 0);
2876 while (delalloc_end < page_end) {
2877 nr_delalloc = find_lock_delalloc_range(inode, tree,
2882 if (nr_delalloc == 0) {
2883 delalloc_start = delalloc_end + 1;
2886 ret = tree->ops->fill_delalloc(inode, page,
2891 /* File system has been set read-only */
2897 * delalloc_end is already one less than the total
2898 * length, so we don't subtract one from
2901 delalloc_to_write += (delalloc_end - delalloc_start +
2904 delalloc_start = delalloc_end + 1;
2906 if (wbc->nr_to_write < delalloc_to_write) {
2909 if (delalloc_to_write < thresh * 2)
2910 thresh = delalloc_to_write;
2911 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2915 /* did the fill delalloc function already unlock and start
2921 * we've unlocked the page, so we can't update
2922 * the mapping's writeback index, just update
2925 wbc->nr_to_write -= nr_written;
2929 if (tree->ops && tree->ops->writepage_start_hook) {
2930 ret = tree->ops->writepage_start_hook(page, start,
2933 /* Fixup worker will requeue */
2935 wbc->pages_skipped++;
2937 redirty_page_for_writepage(wbc, page);
2938 update_nr_written(page, wbc, nr_written);
2946 * we don't want to touch the inode after unlocking the page,
2947 * so we update the mapping writeback index now
2949 update_nr_written(page, wbc, nr_written + 1);
2952 if (last_byte <= start) {
2953 if (tree->ops && tree->ops->writepage_end_io_hook)
2954 tree->ops->writepage_end_io_hook(page, start,
2959 blocksize = inode->i_sb->s_blocksize;
2961 while (cur <= end) {
2962 if (cur >= last_byte) {
2963 if (tree->ops && tree->ops->writepage_end_io_hook)
2964 tree->ops->writepage_end_io_hook(page, cur,
2968 em = epd->get_extent(inode, page, pg_offset, cur,
2970 if (IS_ERR_OR_NULL(em)) {
2975 extent_offset = cur - em->start;
2976 BUG_ON(extent_map_end(em) <= cur);
2978 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2979 iosize = ALIGN(iosize, blocksize);
2980 sector = (em->block_start + extent_offset) >> 9;
2982 block_start = em->block_start;
2983 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2984 free_extent_map(em);
2988 * compressed and inline extents are written through other
2991 if (compressed || block_start == EXTENT_MAP_HOLE ||
2992 block_start == EXTENT_MAP_INLINE) {
2994 * end_io notification does not happen here for
2995 * compressed extents
2997 if (!compressed && tree->ops &&
2998 tree->ops->writepage_end_io_hook)
2999 tree->ops->writepage_end_io_hook(page, cur,
3002 else if (compressed) {
3003 /* we don't want to end_page_writeback on
3004 * a compressed extent. this happens
3011 pg_offset += iosize;
3014 /* leave this out until we have a page_mkwrite call */
3015 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
3016 EXTENT_DIRTY, 0, NULL)) {
3018 pg_offset += iosize;
3022 if (tree->ops && tree->ops->writepage_io_hook) {
3023 ret = tree->ops->writepage_io_hook(page, cur,
3031 unsigned long max_nr = end_index + 1;
3033 set_range_writeback(tree, cur, cur + iosize - 1);
3034 if (!PageWriteback(page)) {
3035 printk(KERN_ERR "btrfs warning page %lu not "
3036 "writeback, cur %llu end %llu\n",
3037 page->index, (unsigned long long)cur,
3038 (unsigned long long)end);
3041 ret = submit_extent_page(write_flags, tree, page,
3042 sector, iosize, pg_offset,
3043 bdev, &epd->bio, max_nr,
3044 end_bio_extent_writepage,
3050 pg_offset += iosize;
3055 /* make sure the mapping tag for page dirty gets cleared */
3056 set_page_writeback(page);
3057 end_page_writeback(page);
3063 /* drop our reference on any cached states */
3064 free_extent_state(cached_state);
3068 static int eb_wait(void *word)
3074 static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3076 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3077 TASK_UNINTERRUPTIBLE);
3080 static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3081 struct btrfs_fs_info *fs_info,
3082 struct extent_page_data *epd)
3084 unsigned long i, num_pages;
3088 if (!btrfs_try_tree_write_lock(eb)) {
3090 flush_write_bio(epd);
3091 btrfs_tree_lock(eb);
3094 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3095 btrfs_tree_unlock(eb);
3099 flush_write_bio(epd);
3103 wait_on_extent_buffer_writeback(eb);
3104 btrfs_tree_lock(eb);
3105 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3107 btrfs_tree_unlock(eb);
3112 * We need to do this to prevent races in people who check if the eb is
3113 * under IO since we can end up having no IO bits set for a short period
3116 spin_lock(&eb->refs_lock);
3117 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3118 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3119 spin_unlock(&eb->refs_lock);
3120 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3121 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3123 fs_info->dirty_metadata_batch);
3126 spin_unlock(&eb->refs_lock);
3129 btrfs_tree_unlock(eb);
3134 num_pages = num_extent_pages(eb->start, eb->len);
3135 for (i = 0; i < num_pages; i++) {
3136 struct page *p = extent_buffer_page(eb, i);
3138 if (!trylock_page(p)) {
3140 flush_write_bio(epd);
3150 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3152 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3153 smp_mb__after_clear_bit();
3154 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3157 static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3159 int uptodate = err == 0;
3160 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3161 struct extent_buffer *eb;
3165 struct page *page = bvec->bv_page;
3168 eb = (struct extent_buffer *)page->private;
3170 done = atomic_dec_and_test(&eb->io_pages);
3172 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3173 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3174 ClearPageUptodate(page);
3178 end_page_writeback(page);
3183 end_extent_buffer_writeback(eb);
3184 } while (bvec >= bio->bi_io_vec);
3190 static int write_one_eb(struct extent_buffer *eb,
3191 struct btrfs_fs_info *fs_info,
3192 struct writeback_control *wbc,
3193 struct extent_page_data *epd)
3195 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3196 u64 offset = eb->start;
3197 unsigned long i, num_pages;
3198 unsigned long bio_flags = 0;
3199 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3202 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3203 num_pages = num_extent_pages(eb->start, eb->len);
3204 atomic_set(&eb->io_pages, num_pages);
3205 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3206 bio_flags = EXTENT_BIO_TREE_LOG;
3208 for (i = 0; i < num_pages; i++) {
3209 struct page *p = extent_buffer_page(eb, i);
3211 clear_page_dirty_for_io(p);
3212 set_page_writeback(p);
3213 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3214 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3215 -1, end_bio_extent_buffer_writepage,
3216 0, epd->bio_flags, bio_flags);
3217 epd->bio_flags = bio_flags;
3219 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3221 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3222 end_extent_buffer_writeback(eb);
3226 offset += PAGE_CACHE_SIZE;
3227 update_nr_written(p, wbc, 1);
3231 if (unlikely(ret)) {
3232 for (; i < num_pages; i++) {
3233 struct page *p = extent_buffer_page(eb, i);
3241 int btree_write_cache_pages(struct address_space *mapping,
3242 struct writeback_control *wbc)
3244 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3245 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3246 struct extent_buffer *eb, *prev_eb = NULL;
3247 struct extent_page_data epd = {
3251 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3256 int nr_to_write_done = 0;
3257 struct pagevec pvec;
3260 pgoff_t end; /* Inclusive */
3264 pagevec_init(&pvec, 0);
3265 if (wbc->range_cyclic) {
3266 index = mapping->writeback_index; /* Start from prev offset */
3269 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3270 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3273 if (wbc->sync_mode == WB_SYNC_ALL)
3274 tag = PAGECACHE_TAG_TOWRITE;
3276 tag = PAGECACHE_TAG_DIRTY;
3278 if (wbc->sync_mode == WB_SYNC_ALL)
3279 tag_pages_for_writeback(mapping, index, end);
3280 while (!done && !nr_to_write_done && (index <= end) &&
3281 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3282 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3286 for (i = 0; i < nr_pages; i++) {
3287 struct page *page = pvec.pages[i];
3289 if (!PagePrivate(page))
3292 if (!wbc->range_cyclic && page->index > end) {
3297 spin_lock(&mapping->private_lock);
3298 if (!PagePrivate(page)) {
3299 spin_unlock(&mapping->private_lock);
3303 eb = (struct extent_buffer *)page->private;
3306 * Shouldn't happen and normally this would be a BUG_ON
3307 * but no sense in crashing the users box for something
3308 * we can survive anyway.
3311 spin_unlock(&mapping->private_lock);
3316 if (eb == prev_eb) {
3317 spin_unlock(&mapping->private_lock);
3321 ret = atomic_inc_not_zero(&eb->refs);
3322 spin_unlock(&mapping->private_lock);
3327 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3329 free_extent_buffer(eb);
3333 ret = write_one_eb(eb, fs_info, wbc, &epd);
3336 free_extent_buffer(eb);
3339 free_extent_buffer(eb);
3342 * the filesystem may choose to bump up nr_to_write.
3343 * We have to make sure to honor the new nr_to_write
3346 nr_to_write_done = wbc->nr_to_write <= 0;
3348 pagevec_release(&pvec);
3351 if (!scanned && !done) {
3353 * We hit the last page and there is more work to be done: wrap
3354 * back to the start of the file
3360 flush_write_bio(&epd);
3365 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3366 * @mapping: address space structure to write
3367 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3368 * @writepage: function called for each page
3369 * @data: data passed to writepage function
3371 * If a page is already under I/O, write_cache_pages() skips it, even
3372 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3373 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3374 * and msync() need to guarantee that all the data which was dirty at the time
3375 * the call was made get new I/O started against them. If wbc->sync_mode is
3376 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3377 * existing IO to complete.
3379 static int extent_write_cache_pages(struct extent_io_tree *tree,
3380 struct address_space *mapping,
3381 struct writeback_control *wbc,
3382 writepage_t writepage, void *data,
3383 void (*flush_fn)(void *))
3385 struct inode *inode = mapping->host;
3388 int nr_to_write_done = 0;
3389 struct pagevec pvec;
3392 pgoff_t end; /* Inclusive */
3397 * We have to hold onto the inode so that ordered extents can do their
3398 * work when the IO finishes. The alternative to this is failing to add
3399 * an ordered extent if the igrab() fails there and that is a huge pain
3400 * to deal with, so instead just hold onto the inode throughout the
3401 * writepages operation. If it fails here we are freeing up the inode
3402 * anyway and we'd rather not waste our time writing out stuff that is
3403 * going to be truncated anyway.
3408 pagevec_init(&pvec, 0);
3409 if (wbc->range_cyclic) {
3410 index = mapping->writeback_index; /* Start from prev offset */
3413 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3414 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3417 if (wbc->sync_mode == WB_SYNC_ALL)
3418 tag = PAGECACHE_TAG_TOWRITE;
3420 tag = PAGECACHE_TAG_DIRTY;
3422 if (wbc->sync_mode == WB_SYNC_ALL)
3423 tag_pages_for_writeback(mapping, index, end);
3424 while (!done && !nr_to_write_done && (index <= end) &&
3425 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3426 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3430 for (i = 0; i < nr_pages; i++) {
3431 struct page *page = pvec.pages[i];
3434 * At this point we hold neither mapping->tree_lock nor
3435 * lock on the page itself: the page may be truncated or
3436 * invalidated (changing page->mapping to NULL), or even
3437 * swizzled back from swapper_space to tmpfs file
3440 if (!trylock_page(page)) {
3445 if (unlikely(page->mapping != mapping)) {
3450 if (!wbc->range_cyclic && page->index > end) {
3456 if (wbc->sync_mode != WB_SYNC_NONE) {
3457 if (PageWriteback(page))
3459 wait_on_page_writeback(page);
3462 if (PageWriteback(page) ||
3463 !clear_page_dirty_for_io(page)) {
3468 ret = (*writepage)(page, wbc, data);
3470 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3478 * the filesystem may choose to bump up nr_to_write.
3479 * We have to make sure to honor the new nr_to_write
3482 nr_to_write_done = wbc->nr_to_write <= 0;
3484 pagevec_release(&pvec);
3487 if (!scanned && !done) {
3489 * We hit the last page and there is more work to be done: wrap
3490 * back to the start of the file
3496 btrfs_add_delayed_iput(inode);
3500 static void flush_epd_write_bio(struct extent_page_data *epd)
3509 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
3510 BUG_ON(ret < 0); /* -ENOMEM */
3515 static noinline void flush_write_bio(void *data)
3517 struct extent_page_data *epd = data;
3518 flush_epd_write_bio(epd);
3521 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3522 get_extent_t *get_extent,
3523 struct writeback_control *wbc)
3526 struct extent_page_data epd = {
3529 .get_extent = get_extent,
3531 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3535 ret = __extent_writepage(page, wbc, &epd);
3537 flush_epd_write_bio(&epd);
3541 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3542 u64 start, u64 end, get_extent_t *get_extent,
3546 struct address_space *mapping = inode->i_mapping;
3548 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3551 struct extent_page_data epd = {
3554 .get_extent = get_extent,
3556 .sync_io = mode == WB_SYNC_ALL,
3559 struct writeback_control wbc_writepages = {
3561 .nr_to_write = nr_pages * 2,
3562 .range_start = start,
3563 .range_end = end + 1,
3566 while (start <= end) {
3567 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3568 if (clear_page_dirty_for_io(page))
3569 ret = __extent_writepage(page, &wbc_writepages, &epd);
3571 if (tree->ops && tree->ops->writepage_end_io_hook)
3572 tree->ops->writepage_end_io_hook(page, start,
3573 start + PAGE_CACHE_SIZE - 1,
3577 page_cache_release(page);
3578 start += PAGE_CACHE_SIZE;
3581 flush_epd_write_bio(&epd);
3585 int extent_writepages(struct extent_io_tree *tree,
3586 struct address_space *mapping,
3587 get_extent_t *get_extent,
3588 struct writeback_control *wbc)
3591 struct extent_page_data epd = {
3594 .get_extent = get_extent,
3596 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3600 ret = extent_write_cache_pages(tree, mapping, wbc,
3601 __extent_writepage, &epd,
3603 flush_epd_write_bio(&epd);
3607 int extent_readpages(struct extent_io_tree *tree,
3608 struct address_space *mapping,
3609 struct list_head *pages, unsigned nr_pages,
3610 get_extent_t get_extent)
3612 struct bio *bio = NULL;
3614 unsigned long bio_flags = 0;
3615 struct page *pagepool[16];
3620 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3621 page = list_entry(pages->prev, struct page, lru);
3623 prefetchw(&page->flags);
3624 list_del(&page->lru);
3625 if (add_to_page_cache_lru(page, mapping,
3626 page->index, GFP_NOFS)) {
3627 page_cache_release(page);
3631 pagepool[nr++] = page;
3632 if (nr < ARRAY_SIZE(pagepool))
3634 for (i = 0; i < nr; i++) {
3635 __extent_read_full_page(tree, pagepool[i], get_extent,
3636 &bio, 0, &bio_flags);
3637 page_cache_release(pagepool[i]);
3641 for (i = 0; i < nr; i++) {
3642 __extent_read_full_page(tree, pagepool[i], get_extent,
3643 &bio, 0, &bio_flags);
3644 page_cache_release(pagepool[i]);
3647 BUG_ON(!list_empty(pages));
3649 return submit_one_bio(READ, bio, 0, bio_flags);
3654 * basic invalidatepage code, this waits on any locked or writeback
3655 * ranges corresponding to the page, and then deletes any extent state
3656 * records from the tree
3658 int extent_invalidatepage(struct extent_io_tree *tree,
3659 struct page *page, unsigned long offset)
3661 struct extent_state *cached_state = NULL;
3662 u64 start = page_offset(page);
3663 u64 end = start + PAGE_CACHE_SIZE - 1;
3664 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3666 start += ALIGN(offset, blocksize);
3670 lock_extent_bits(tree, start, end, 0, &cached_state);
3671 wait_on_page_writeback(page);
3672 clear_extent_bit(tree, start, end,
3673 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3674 EXTENT_DO_ACCOUNTING,
3675 1, 1, &cached_state, GFP_NOFS);
3680 * a helper for releasepage, this tests for areas of the page that
3681 * are locked or under IO and drops the related state bits if it is safe
3684 int try_release_extent_state(struct extent_map_tree *map,
3685 struct extent_io_tree *tree, struct page *page,
3688 u64 start = page_offset(page);
3689 u64 end = start + PAGE_CACHE_SIZE - 1;
3692 if (test_range_bit(tree, start, end,
3693 EXTENT_IOBITS, 0, NULL))
3696 if ((mask & GFP_NOFS) == GFP_NOFS)
3699 * at this point we can safely clear everything except the
3700 * locked bit and the nodatasum bit
3702 ret = clear_extent_bit(tree, start, end,
3703 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3706 /* if clear_extent_bit failed for enomem reasons,
3707 * we can't allow the release to continue.
3718 * a helper for releasepage. As long as there are no locked extents
3719 * in the range corresponding to the page, both state records and extent
3720 * map records are removed
3722 int try_release_extent_mapping(struct extent_map_tree *map,
3723 struct extent_io_tree *tree, struct page *page,
3726 struct extent_map *em;
3727 u64 start = page_offset(page);
3728 u64 end = start + PAGE_CACHE_SIZE - 1;
3730 if ((mask & __GFP_WAIT) &&
3731 page->mapping->host->i_size > 16 * 1024 * 1024) {
3733 while (start <= end) {
3734 len = end - start + 1;
3735 write_lock(&map->lock);
3736 em = lookup_extent_mapping(map, start, len);
3738 write_unlock(&map->lock);
3741 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3742 em->start != start) {
3743 write_unlock(&map->lock);
3744 free_extent_map(em);
3747 if (!test_range_bit(tree, em->start,
3748 extent_map_end(em) - 1,
3749 EXTENT_LOCKED | EXTENT_WRITEBACK,
3751 remove_extent_mapping(map, em);
3752 /* once for the rb tree */
3753 free_extent_map(em);
3755 start = extent_map_end(em);
3756 write_unlock(&map->lock);
3759 free_extent_map(em);
3762 return try_release_extent_state(map, tree, page, mask);
3766 * helper function for fiemap, which doesn't want to see any holes.
3767 * This maps until we find something past 'last'
3769 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3772 get_extent_t *get_extent)
3774 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3775 struct extent_map *em;
3782 len = last - offset;
3785 len = ALIGN(len, sectorsize);
3786 em = get_extent(inode, NULL, 0, offset, len, 0);
3787 if (IS_ERR_OR_NULL(em))
3790 /* if this isn't a hole return it */
3791 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3792 em->block_start != EXTENT_MAP_HOLE) {
3796 /* this is a hole, advance to the next extent */
3797 offset = extent_map_end(em);
3798 free_extent_map(em);
3805 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3806 __u64 start, __u64 len, get_extent_t *get_extent)
3810 u64 max = start + len;
3814 u64 last_for_get_extent = 0;
3816 u64 isize = i_size_read(inode);
3817 struct btrfs_key found_key;
3818 struct extent_map *em = NULL;
3819 struct extent_state *cached_state = NULL;
3820 struct btrfs_path *path;
3821 struct btrfs_file_extent_item *item;
3826 unsigned long emflags;
3831 path = btrfs_alloc_path();
3834 path->leave_spinning = 1;
3836 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3837 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3840 * lookup the last file extent. We're not using i_size here
3841 * because there might be preallocation past i_size
3843 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3844 path, btrfs_ino(inode), -1, 0);
3846 btrfs_free_path(path);
3851 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3852 struct btrfs_file_extent_item);
3853 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3854 found_type = btrfs_key_type(&found_key);
3856 /* No extents, but there might be delalloc bits */
3857 if (found_key.objectid != btrfs_ino(inode) ||
3858 found_type != BTRFS_EXTENT_DATA_KEY) {
3859 /* have to trust i_size as the end */
3861 last_for_get_extent = isize;
3864 * remember the start of the last extent. There are a
3865 * bunch of different factors that go into the length of the
3866 * extent, so its much less complex to remember where it started
3868 last = found_key.offset;
3869 last_for_get_extent = last + 1;
3871 btrfs_free_path(path);
3874 * we might have some extents allocated but more delalloc past those
3875 * extents. so, we trust isize unless the start of the last extent is
3880 last_for_get_extent = isize;
3883 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3886 em = get_extent_skip_holes(inode, start, last_for_get_extent,
3896 u64 offset_in_extent;
3898 /* break if the extent we found is outside the range */
3899 if (em->start >= max || extent_map_end(em) < off)
3903 * get_extent may return an extent that starts before our
3904 * requested range. We have to make sure the ranges
3905 * we return to fiemap always move forward and don't
3906 * overlap, so adjust the offsets here
3908 em_start = max(em->start, off);
3911 * record the offset from the start of the extent
3912 * for adjusting the disk offset below
3914 offset_in_extent = em_start - em->start;
3915 em_end = extent_map_end(em);
3916 em_len = em_end - em_start;
3917 emflags = em->flags;
3922 * bump off for our next call to get_extent
3924 off = extent_map_end(em);
3928 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
3930 flags |= FIEMAP_EXTENT_LAST;
3931 } else if (em->block_start == EXTENT_MAP_INLINE) {
3932 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3933 FIEMAP_EXTENT_NOT_ALIGNED);
3934 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
3935 flags |= (FIEMAP_EXTENT_DELALLOC |
3936 FIEMAP_EXTENT_UNKNOWN);
3938 disko = em->block_start + offset_in_extent;
3940 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3941 flags |= FIEMAP_EXTENT_ENCODED;
3943 free_extent_map(em);
3945 if ((em_start >= last) || em_len == (u64)-1 ||
3946 (last == (u64)-1 && isize <= em_end)) {
3947 flags |= FIEMAP_EXTENT_LAST;
3951 /* now scan forward to see if this is really the last extent. */
3952 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3959 flags |= FIEMAP_EXTENT_LAST;
3962 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3968 free_extent_map(em);
3970 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3971 &cached_state, GFP_NOFS);
3975 static void __free_extent_buffer(struct extent_buffer *eb)
3978 unsigned long flags;
3979 spin_lock_irqsave(&leak_lock, flags);
3980 list_del(&eb->leak_list);
3981 spin_unlock_irqrestore(&leak_lock, flags);
3983 kmem_cache_free(extent_buffer_cache, eb);
3986 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3991 struct extent_buffer *eb = NULL;
3993 unsigned long flags;
3996 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
4003 rwlock_init(&eb->lock);
4004 atomic_set(&eb->write_locks, 0);
4005 atomic_set(&eb->read_locks, 0);
4006 atomic_set(&eb->blocking_readers, 0);
4007 atomic_set(&eb->blocking_writers, 0);
4008 atomic_set(&eb->spinning_readers, 0);
4009 atomic_set(&eb->spinning_writers, 0);
4010 eb->lock_nested = 0;
4011 init_waitqueue_head(&eb->write_lock_wq);
4012 init_waitqueue_head(&eb->read_lock_wq);
4015 spin_lock_irqsave(&leak_lock, flags);
4016 list_add(&eb->leak_list, &buffers);
4017 spin_unlock_irqrestore(&leak_lock, flags);
4019 spin_lock_init(&eb->refs_lock);
4020 atomic_set(&eb->refs, 1);
4021 atomic_set(&eb->io_pages, 0);
4024 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4026 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4027 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4028 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4033 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4037 struct extent_buffer *new;
4038 unsigned long num_pages = num_extent_pages(src->start, src->len);
4040 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4044 for (i = 0; i < num_pages; i++) {
4045 p = alloc_page(GFP_ATOMIC);
4047 attach_extent_buffer_page(new, p);
4048 WARN_ON(PageDirty(p));
4053 copy_extent_buffer(new, src, 0, 0, src->len);
4054 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4055 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4060 struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4062 struct extent_buffer *eb;
4063 unsigned long num_pages = num_extent_pages(0, len);
4066 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4070 for (i = 0; i < num_pages; i++) {
4071 eb->pages[i] = alloc_page(GFP_ATOMIC);
4075 set_extent_buffer_uptodate(eb);
4076 btrfs_set_header_nritems(eb, 0);
4077 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4082 __free_page(eb->pages[i - 1]);
4083 __free_extent_buffer(eb);
4087 static int extent_buffer_under_io(struct extent_buffer *eb)
4089 return (atomic_read(&eb->io_pages) ||
4090 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4091 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4095 * Helper for releasing extent buffer page.
4097 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4098 unsigned long start_idx)
4100 unsigned long index;
4101 unsigned long num_pages;
4103 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4105 BUG_ON(extent_buffer_under_io(eb));
4107 num_pages = num_extent_pages(eb->start, eb->len);
4108 index = start_idx + num_pages;
4109 if (start_idx >= index)
4114 page = extent_buffer_page(eb, index);
4115 if (page && mapped) {
4116 spin_lock(&page->mapping->private_lock);
4118 * We do this since we'll remove the pages after we've
4119 * removed the eb from the radix tree, so we could race
4120 * and have this page now attached to the new eb. So
4121 * only clear page_private if it's still connected to
4124 if (PagePrivate(page) &&
4125 page->private == (unsigned long)eb) {
4126 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4127 BUG_ON(PageDirty(page));
4128 BUG_ON(PageWriteback(page));
4130 * We need to make sure we haven't be attached
4133 ClearPagePrivate(page);
4134 set_page_private(page, 0);
4135 /* One for the page private */
4136 page_cache_release(page);
4138 spin_unlock(&page->mapping->private_lock);
4142 /* One for when we alloced the page */
4143 page_cache_release(page);
4145 } while (index != start_idx);
4149 * Helper for releasing the extent buffer.
4151 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4153 btrfs_release_extent_buffer_page(eb, 0);
4154 __free_extent_buffer(eb);
4157 static void check_buffer_tree_ref(struct extent_buffer *eb)
4160 /* the ref bit is tricky. We have to make sure it is set
4161 * if we have the buffer dirty. Otherwise the
4162 * code to free a buffer can end up dropping a dirty
4165 * Once the ref bit is set, it won't go away while the
4166 * buffer is dirty or in writeback, and it also won't
4167 * go away while we have the reference count on the
4170 * We can't just set the ref bit without bumping the
4171 * ref on the eb because free_extent_buffer might
4172 * see the ref bit and try to clear it. If this happens
4173 * free_extent_buffer might end up dropping our original
4174 * ref by mistake and freeing the page before we are able
4175 * to add one more ref.
4177 * So bump the ref count first, then set the bit. If someone
4178 * beat us to it, drop the ref we added.
4180 refs = atomic_read(&eb->refs);
4181 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4184 spin_lock(&eb->refs_lock);
4185 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4186 atomic_inc(&eb->refs);
4187 spin_unlock(&eb->refs_lock);
4190 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4192 unsigned long num_pages, i;
4194 check_buffer_tree_ref(eb);
4196 num_pages = num_extent_pages(eb->start, eb->len);
4197 for (i = 0; i < num_pages; i++) {
4198 struct page *p = extent_buffer_page(eb, i);
4199 mark_page_accessed(p);
4203 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
4204 u64 start, unsigned long len)
4206 unsigned long num_pages = num_extent_pages(start, len);
4208 unsigned long index = start >> PAGE_CACHE_SHIFT;
4209 struct extent_buffer *eb;
4210 struct extent_buffer *exists = NULL;
4212 struct address_space *mapping = tree->mapping;
4217 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4218 if (eb && atomic_inc_not_zero(&eb->refs)) {
4220 mark_extent_buffer_accessed(eb);
4225 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
4229 for (i = 0; i < num_pages; i++, index++) {
4230 p = find_or_create_page(mapping, index, GFP_NOFS);
4234 spin_lock(&mapping->private_lock);
4235 if (PagePrivate(p)) {
4237 * We could have already allocated an eb for this page
4238 * and attached one so lets see if we can get a ref on
4239 * the existing eb, and if we can we know it's good and
4240 * we can just return that one, else we know we can just
4241 * overwrite page->private.
4243 exists = (struct extent_buffer *)p->private;
4244 if (atomic_inc_not_zero(&exists->refs)) {
4245 spin_unlock(&mapping->private_lock);
4247 page_cache_release(p);
4248 mark_extent_buffer_accessed(exists);
4253 * Do this so attach doesn't complain and we need to
4254 * drop the ref the old guy had.
4256 ClearPagePrivate(p);
4257 WARN_ON(PageDirty(p));
4258 page_cache_release(p);
4260 attach_extent_buffer_page(eb, p);
4261 spin_unlock(&mapping->private_lock);
4262 WARN_ON(PageDirty(p));
4263 mark_page_accessed(p);
4265 if (!PageUptodate(p))
4269 * see below about how we avoid a nasty race with release page
4270 * and why we unlock later
4274 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4276 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4280 spin_lock(&tree->buffer_lock);
4281 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4282 if (ret == -EEXIST) {
4283 exists = radix_tree_lookup(&tree->buffer,
4284 start >> PAGE_CACHE_SHIFT);
4285 if (!atomic_inc_not_zero(&exists->refs)) {
4286 spin_unlock(&tree->buffer_lock);
4287 radix_tree_preload_end();
4291 spin_unlock(&tree->buffer_lock);
4292 radix_tree_preload_end();
4293 mark_extent_buffer_accessed(exists);
4296 /* add one reference for the tree */
4297 check_buffer_tree_ref(eb);
4298 spin_unlock(&tree->buffer_lock);
4299 radix_tree_preload_end();
4302 * there is a race where release page may have
4303 * tried to find this extent buffer in the radix
4304 * but failed. It will tell the VM it is safe to
4305 * reclaim the, and it will clear the page private bit.
4306 * We must make sure to set the page private bit properly
4307 * after the extent buffer is in the radix tree so
4308 * it doesn't get lost
4310 SetPageChecked(eb->pages[0]);
4311 for (i = 1; i < num_pages; i++) {
4312 p = extent_buffer_page(eb, i);
4313 ClearPageChecked(p);
4316 unlock_page(eb->pages[0]);
4320 for (i = 0; i < num_pages; i++) {
4322 unlock_page(eb->pages[i]);
4325 WARN_ON(!atomic_dec_and_test(&eb->refs));
4326 btrfs_release_extent_buffer(eb);
4330 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
4331 u64 start, unsigned long len)
4333 struct extent_buffer *eb;
4336 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4337 if (eb && atomic_inc_not_zero(&eb->refs)) {
4339 mark_extent_buffer_accessed(eb);
4347 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4349 struct extent_buffer *eb =
4350 container_of(head, struct extent_buffer, rcu_head);
4352 __free_extent_buffer(eb);
4355 /* Expects to have eb->eb_lock already held */
4356 static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4358 WARN_ON(atomic_read(&eb->refs) == 0);
4359 if (atomic_dec_and_test(&eb->refs)) {
4360 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4361 spin_unlock(&eb->refs_lock);
4363 struct extent_io_tree *tree = eb->tree;
4365 spin_unlock(&eb->refs_lock);
4367 spin_lock(&tree->buffer_lock);
4368 radix_tree_delete(&tree->buffer,
4369 eb->start >> PAGE_CACHE_SHIFT);
4370 spin_unlock(&tree->buffer_lock);
4373 /* Should be safe to release our pages at this point */
4374 btrfs_release_extent_buffer_page(eb, 0);
4375 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4378 spin_unlock(&eb->refs_lock);
4383 void free_extent_buffer(struct extent_buffer *eb)
4391 refs = atomic_read(&eb->refs);
4394 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4399 spin_lock(&eb->refs_lock);
4400 if (atomic_read(&eb->refs) == 2 &&
4401 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4402 atomic_dec(&eb->refs);
4404 if (atomic_read(&eb->refs) == 2 &&
4405 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
4406 !extent_buffer_under_io(eb) &&
4407 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4408 atomic_dec(&eb->refs);
4411 * I know this is terrible, but it's temporary until we stop tracking
4412 * the uptodate bits and such for the extent buffers.
4414 release_extent_buffer(eb, GFP_ATOMIC);
4417 void free_extent_buffer_stale(struct extent_buffer *eb)
4422 spin_lock(&eb->refs_lock);
4423 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4425 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
4426 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4427 atomic_dec(&eb->refs);
4428 release_extent_buffer(eb, GFP_NOFS);
4431 void clear_extent_buffer_dirty(struct extent_buffer *eb)
4434 unsigned long num_pages;
4437 num_pages = num_extent_pages(eb->start, eb->len);
4439 for (i = 0; i < num_pages; i++) {
4440 page = extent_buffer_page(eb, i);
4441 if (!PageDirty(page))
4445 WARN_ON(!PagePrivate(page));
4447 clear_page_dirty_for_io(page);
4448 spin_lock_irq(&page->mapping->tree_lock);
4449 if (!PageDirty(page)) {
4450 radix_tree_tag_clear(&page->mapping->page_tree,
4452 PAGECACHE_TAG_DIRTY);
4454 spin_unlock_irq(&page->mapping->tree_lock);
4455 ClearPageError(page);
4458 WARN_ON(atomic_read(&eb->refs) == 0);
4461 int set_extent_buffer_dirty(struct extent_buffer *eb)
4464 unsigned long num_pages;
4467 check_buffer_tree_ref(eb);
4469 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4471 num_pages = num_extent_pages(eb->start, eb->len);
4472 WARN_ON(atomic_read(&eb->refs) == 0);
4473 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4475 for (i = 0; i < num_pages; i++)
4476 set_page_dirty(extent_buffer_page(eb, i));
4480 static int range_straddles_pages(u64 start, u64 len)
4482 if (len < PAGE_CACHE_SIZE)
4484 if (start & (PAGE_CACHE_SIZE - 1))
4486 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4491 int clear_extent_buffer_uptodate(struct extent_buffer *eb)
4495 unsigned long num_pages;
4497 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4498 num_pages = num_extent_pages(eb->start, eb->len);
4499 for (i = 0; i < num_pages; i++) {
4500 page = extent_buffer_page(eb, i);
4502 ClearPageUptodate(page);
4507 int set_extent_buffer_uptodate(struct extent_buffer *eb)
4511 unsigned long num_pages;
4513 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4514 num_pages = num_extent_pages(eb->start, eb->len);
4515 for (i = 0; i < num_pages; i++) {
4516 page = extent_buffer_page(eb, i);
4517 SetPageUptodate(page);
4522 int extent_range_uptodate(struct extent_io_tree *tree,
4527 int pg_uptodate = 1;
4529 unsigned long index;
4531 if (range_straddles_pages(start, end - start + 1)) {
4532 ret = test_range_bit(tree, start, end,
4533 EXTENT_UPTODATE, 1, NULL);
4537 while (start <= end) {
4538 index = start >> PAGE_CACHE_SHIFT;
4539 page = find_get_page(tree->mapping, index);
4542 uptodate = PageUptodate(page);
4543 page_cache_release(page);
4548 start += PAGE_CACHE_SIZE;
4553 int extent_buffer_uptodate(struct extent_buffer *eb)
4555 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4558 int read_extent_buffer_pages(struct extent_io_tree *tree,
4559 struct extent_buffer *eb, u64 start, int wait,
4560 get_extent_t *get_extent, int mirror_num)
4563 unsigned long start_i;
4567 int locked_pages = 0;
4568 int all_uptodate = 1;
4569 unsigned long num_pages;
4570 unsigned long num_reads = 0;
4571 struct bio *bio = NULL;
4572 unsigned long bio_flags = 0;
4574 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4578 WARN_ON(start < eb->start);
4579 start_i = (start >> PAGE_CACHE_SHIFT) -
4580 (eb->start >> PAGE_CACHE_SHIFT);
4585 num_pages = num_extent_pages(eb->start, eb->len);
4586 for (i = start_i; i < num_pages; i++) {
4587 page = extent_buffer_page(eb, i);
4588 if (wait == WAIT_NONE) {
4589 if (!trylock_page(page))
4595 if (!PageUptodate(page)) {
4602 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4606 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
4607 eb->read_mirror = 0;
4608 atomic_set(&eb->io_pages, num_reads);
4609 for (i = start_i; i < num_pages; i++) {
4610 page = extent_buffer_page(eb, i);
4611 if (!PageUptodate(page)) {
4612 ClearPageError(page);
4613 err = __extent_read_full_page(tree, page,
4615 mirror_num, &bio_flags);
4624 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
4629 if (ret || wait != WAIT_COMPLETE)
4632 for (i = start_i; i < num_pages; i++) {
4633 page = extent_buffer_page(eb, i);
4634 wait_on_page_locked(page);
4635 if (!PageUptodate(page))
4643 while (locked_pages > 0) {
4644 page = extent_buffer_page(eb, i);
4652 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4653 unsigned long start,
4660 char *dst = (char *)dstv;
4661 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4662 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4664 WARN_ON(start > eb->len);
4665 WARN_ON(start + len > eb->start + eb->len);
4667 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4670 page = extent_buffer_page(eb, i);
4672 cur = min(len, (PAGE_CACHE_SIZE - offset));
4673 kaddr = page_address(page);
4674 memcpy(dst, kaddr + offset, cur);
4683 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4684 unsigned long min_len, char **map,
4685 unsigned long *map_start,
4686 unsigned long *map_len)
4688 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4691 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4692 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4693 unsigned long end_i = (start_offset + start + min_len - 1) >>
4700 offset = start_offset;
4704 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4707 if (start + min_len > eb->len) {
4708 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4709 "wanted %lu %lu\n", (unsigned long long)eb->start,
4710 eb->len, start, min_len);
4714 p = extent_buffer_page(eb, i);
4715 kaddr = page_address(p);
4716 *map = kaddr + offset;
4717 *map_len = PAGE_CACHE_SIZE - offset;
4721 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4722 unsigned long start,
4729 char *ptr = (char *)ptrv;
4730 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4731 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4734 WARN_ON(start > eb->len);
4735 WARN_ON(start + len > eb->start + eb->len);
4737 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4740 page = extent_buffer_page(eb, i);
4742 cur = min(len, (PAGE_CACHE_SIZE - offset));
4744 kaddr = page_address(page);
4745 ret = memcmp(ptr, kaddr + offset, cur);
4757 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4758 unsigned long start, unsigned long len)
4764 char *src = (char *)srcv;
4765 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4766 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4768 WARN_ON(start > eb->len);
4769 WARN_ON(start + len > eb->start + eb->len);
4771 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4774 page = extent_buffer_page(eb, i);
4775 WARN_ON(!PageUptodate(page));
4777 cur = min(len, PAGE_CACHE_SIZE - offset);
4778 kaddr = page_address(page);
4779 memcpy(kaddr + offset, src, cur);
4788 void memset_extent_buffer(struct extent_buffer *eb, char c,
4789 unsigned long start, unsigned long len)
4795 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4796 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4798 WARN_ON(start > eb->len);
4799 WARN_ON(start + len > eb->start + eb->len);
4801 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4804 page = extent_buffer_page(eb, i);
4805 WARN_ON(!PageUptodate(page));
4807 cur = min(len, PAGE_CACHE_SIZE - offset);
4808 kaddr = page_address(page);
4809 memset(kaddr + offset, c, cur);
4817 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4818 unsigned long dst_offset, unsigned long src_offset,
4821 u64 dst_len = dst->len;
4826 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4827 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4829 WARN_ON(src->len != dst_len);
4831 offset = (start_offset + dst_offset) &
4832 ((unsigned long)PAGE_CACHE_SIZE - 1);
4835 page = extent_buffer_page(dst, i);
4836 WARN_ON(!PageUptodate(page));
4838 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4840 kaddr = page_address(page);
4841 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4850 static void move_pages(struct page *dst_page, struct page *src_page,
4851 unsigned long dst_off, unsigned long src_off,
4854 char *dst_kaddr = page_address(dst_page);
4855 if (dst_page == src_page) {
4856 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4858 char *src_kaddr = page_address(src_page);
4859 char *p = dst_kaddr + dst_off + len;
4860 char *s = src_kaddr + src_off + len;
4867 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4869 unsigned long distance = (src > dst) ? src - dst : dst - src;
4870 return distance < len;
4873 static void copy_pages(struct page *dst_page, struct page *src_page,
4874 unsigned long dst_off, unsigned long src_off,
4877 char *dst_kaddr = page_address(dst_page);
4879 int must_memmove = 0;
4881 if (dst_page != src_page) {
4882 src_kaddr = page_address(src_page);
4884 src_kaddr = dst_kaddr;
4885 if (areas_overlap(src_off, dst_off, len))
4890 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4892 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
4895 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4896 unsigned long src_offset, unsigned long len)
4899 size_t dst_off_in_page;
4900 size_t src_off_in_page;
4901 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4902 unsigned long dst_i;
4903 unsigned long src_i;
4905 if (src_offset + len > dst->len) {
4906 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4907 "len %lu dst len %lu\n", src_offset, len, dst->len);
4910 if (dst_offset + len > dst->len) {
4911 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4912 "len %lu dst len %lu\n", dst_offset, len, dst->len);
4917 dst_off_in_page = (start_offset + dst_offset) &
4918 ((unsigned long)PAGE_CACHE_SIZE - 1);
4919 src_off_in_page = (start_offset + src_offset) &
4920 ((unsigned long)PAGE_CACHE_SIZE - 1);
4922 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4923 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4925 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4927 cur = min_t(unsigned long, cur,
4928 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4930 copy_pages(extent_buffer_page(dst, dst_i),
4931 extent_buffer_page(dst, src_i),
4932 dst_off_in_page, src_off_in_page, cur);
4940 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4941 unsigned long src_offset, unsigned long len)
4944 size_t dst_off_in_page;
4945 size_t src_off_in_page;
4946 unsigned long dst_end = dst_offset + len - 1;
4947 unsigned long src_end = src_offset + len - 1;
4948 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4949 unsigned long dst_i;
4950 unsigned long src_i;
4952 if (src_offset + len > dst->len) {
4953 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4954 "len %lu len %lu\n", src_offset, len, dst->len);
4957 if (dst_offset + len > dst->len) {
4958 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4959 "len %lu len %lu\n", dst_offset, len, dst->len);
4962 if (dst_offset < src_offset) {
4963 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4967 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4968 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4970 dst_off_in_page = (start_offset + dst_end) &
4971 ((unsigned long)PAGE_CACHE_SIZE - 1);
4972 src_off_in_page = (start_offset + src_end) &
4973 ((unsigned long)PAGE_CACHE_SIZE - 1);
4975 cur = min_t(unsigned long, len, src_off_in_page + 1);
4976 cur = min(cur, dst_off_in_page + 1);
4977 move_pages(extent_buffer_page(dst, dst_i),
4978 extent_buffer_page(dst, src_i),
4979 dst_off_in_page - cur + 1,
4980 src_off_in_page - cur + 1, cur);
4988 int try_release_extent_buffer(struct page *page, gfp_t mask)
4990 struct extent_buffer *eb;
4993 * We need to make sure noboody is attaching this page to an eb right
4996 spin_lock(&page->mapping->private_lock);
4997 if (!PagePrivate(page)) {
4998 spin_unlock(&page->mapping->private_lock);
5002 eb = (struct extent_buffer *)page->private;
5006 * This is a little awful but should be ok, we need to make sure that
5007 * the eb doesn't disappear out from under us while we're looking at
5010 spin_lock(&eb->refs_lock);
5011 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
5012 spin_unlock(&eb->refs_lock);
5013 spin_unlock(&page->mapping->private_lock);
5016 spin_unlock(&page->mapping->private_lock);
5018 if ((mask & GFP_NOFS) == GFP_NOFS)
5022 * If tree ref isn't set then we know the ref on this eb is a real ref,
5023 * so just return, this page will likely be freed soon anyway.
5025 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5026 spin_unlock(&eb->refs_lock);
5030 return release_extent_buffer(eb, mask);