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,
1260 int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1262 unsigned long index = start >> PAGE_CACHE_SHIFT;
1263 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1266 while (index <= end_index) {
1267 page = find_get_page(inode->i_mapping, index);
1268 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1269 clear_page_dirty_for_io(page);
1270 page_cache_release(page);
1276 int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1278 unsigned long index = start >> PAGE_CACHE_SHIFT;
1279 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1282 while (index <= end_index) {
1283 page = find_get_page(inode->i_mapping, index);
1284 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1285 account_page_redirty(page);
1286 __set_page_dirty_nobuffers(page);
1287 page_cache_release(page);
1294 * helper function to set both pages and extents in the tree writeback
1296 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1298 unsigned long index = start >> PAGE_CACHE_SHIFT;
1299 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1302 while (index <= end_index) {
1303 page = find_get_page(tree->mapping, index);
1304 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1305 set_page_writeback(page);
1306 page_cache_release(page);
1312 /* find the first state struct with 'bits' set after 'start', and
1313 * return it. tree->lock must be held. NULL will returned if
1314 * nothing was found after 'start'
1316 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1317 u64 start, int bits)
1319 struct rb_node *node;
1320 struct extent_state *state;
1323 * this search will find all the extents that end after
1326 node = tree_search(tree, start);
1331 state = rb_entry(node, struct extent_state, rb_node);
1332 if (state->end >= start && (state->state & bits))
1335 node = rb_next(node);
1344 * find the first offset in the io tree with 'bits' set. zero is
1345 * returned if we find something, and *start_ret and *end_ret are
1346 * set to reflect the state struct that was found.
1348 * If nothing was found, 1 is returned. If found something, return 0.
1350 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1351 u64 *start_ret, u64 *end_ret, int bits,
1352 struct extent_state **cached_state)
1354 struct extent_state *state;
1358 spin_lock(&tree->lock);
1359 if (cached_state && *cached_state) {
1360 state = *cached_state;
1361 if (state->end == start - 1 && state->tree) {
1362 n = rb_next(&state->rb_node);
1364 state = rb_entry(n, struct extent_state,
1366 if (state->state & bits)
1370 free_extent_state(*cached_state);
1371 *cached_state = NULL;
1374 free_extent_state(*cached_state);
1375 *cached_state = NULL;
1378 state = find_first_extent_bit_state(tree, start, bits);
1381 cache_state(state, cached_state);
1382 *start_ret = state->start;
1383 *end_ret = state->end;
1387 spin_unlock(&tree->lock);
1392 * find a contiguous range of bytes in the file marked as delalloc, not
1393 * more than 'max_bytes'. start and end are used to return the range,
1395 * 1 is returned if we find something, 0 if nothing was in the tree
1397 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1398 u64 *start, u64 *end, u64 max_bytes,
1399 struct extent_state **cached_state)
1401 struct rb_node *node;
1402 struct extent_state *state;
1403 u64 cur_start = *start;
1405 u64 total_bytes = 0;
1407 spin_lock(&tree->lock);
1410 * this search will find all the extents that end after
1413 node = tree_search(tree, cur_start);
1421 state = rb_entry(node, struct extent_state, rb_node);
1422 if (found && (state->start != cur_start ||
1423 (state->state & EXTENT_BOUNDARY))) {
1426 if (!(state->state & EXTENT_DELALLOC)) {
1432 *start = state->start;
1433 *cached_state = state;
1434 atomic_inc(&state->refs);
1438 cur_start = state->end + 1;
1439 node = rb_next(node);
1442 total_bytes += state->end - state->start + 1;
1443 if (total_bytes >= max_bytes)
1447 spin_unlock(&tree->lock);
1451 static noinline void __unlock_for_delalloc(struct inode *inode,
1452 struct page *locked_page,
1456 struct page *pages[16];
1457 unsigned long index = start >> PAGE_CACHE_SHIFT;
1458 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1459 unsigned long nr_pages = end_index - index + 1;
1462 if (index == locked_page->index && end_index == index)
1465 while (nr_pages > 0) {
1466 ret = find_get_pages_contig(inode->i_mapping, index,
1467 min_t(unsigned long, nr_pages,
1468 ARRAY_SIZE(pages)), pages);
1469 for (i = 0; i < ret; i++) {
1470 if (pages[i] != locked_page)
1471 unlock_page(pages[i]);
1472 page_cache_release(pages[i]);
1480 static noinline int lock_delalloc_pages(struct inode *inode,
1481 struct page *locked_page,
1485 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1486 unsigned long start_index = index;
1487 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1488 unsigned long pages_locked = 0;
1489 struct page *pages[16];
1490 unsigned long nrpages;
1494 /* the caller is responsible for locking the start index */
1495 if (index == locked_page->index && index == end_index)
1498 /* skip the page at the start index */
1499 nrpages = end_index - index + 1;
1500 while (nrpages > 0) {
1501 ret = find_get_pages_contig(inode->i_mapping, index,
1502 min_t(unsigned long,
1503 nrpages, ARRAY_SIZE(pages)), pages);
1508 /* now we have an array of pages, lock them all */
1509 for (i = 0; i < ret; i++) {
1511 * the caller is taking responsibility for
1514 if (pages[i] != locked_page) {
1515 lock_page(pages[i]);
1516 if (!PageDirty(pages[i]) ||
1517 pages[i]->mapping != inode->i_mapping) {
1519 unlock_page(pages[i]);
1520 page_cache_release(pages[i]);
1524 page_cache_release(pages[i]);
1533 if (ret && pages_locked) {
1534 __unlock_for_delalloc(inode, locked_page,
1536 ((u64)(start_index + pages_locked - 1)) <<
1543 * find a contiguous range of bytes in the file marked as delalloc, not
1544 * more than 'max_bytes'. start and end are used to return the range,
1546 * 1 is returned if we find something, 0 if nothing was in the tree
1548 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1549 struct extent_io_tree *tree,
1550 struct page *locked_page,
1551 u64 *start, u64 *end,
1557 struct extent_state *cached_state = NULL;
1562 /* step one, find a bunch of delalloc bytes starting at start */
1563 delalloc_start = *start;
1565 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1566 max_bytes, &cached_state);
1567 if (!found || delalloc_end <= *start) {
1568 *start = delalloc_start;
1569 *end = delalloc_end;
1570 free_extent_state(cached_state);
1575 * start comes from the offset of locked_page. We have to lock
1576 * pages in order, so we can't process delalloc bytes before
1579 if (delalloc_start < *start)
1580 delalloc_start = *start;
1583 * make sure to limit the number of pages we try to lock down
1586 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1587 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1589 /* step two, lock all the pages after the page that has start */
1590 ret = lock_delalloc_pages(inode, locked_page,
1591 delalloc_start, delalloc_end);
1592 if (ret == -EAGAIN) {
1593 /* some of the pages are gone, lets avoid looping by
1594 * shortening the size of the delalloc range we're searching
1596 free_extent_state(cached_state);
1598 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1599 max_bytes = PAGE_CACHE_SIZE - offset;
1607 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
1609 /* step three, lock the state bits for the whole range */
1610 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
1612 /* then test to make sure it is all still delalloc */
1613 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1614 EXTENT_DELALLOC, 1, cached_state);
1616 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1617 &cached_state, GFP_NOFS);
1618 __unlock_for_delalloc(inode, locked_page,
1619 delalloc_start, delalloc_end);
1623 free_extent_state(cached_state);
1624 *start = delalloc_start;
1625 *end = delalloc_end;
1630 int extent_clear_unlock_delalloc(struct inode *inode,
1631 struct extent_io_tree *tree,
1632 u64 start, u64 end, struct page *locked_page,
1636 struct page *pages[16];
1637 unsigned long index = start >> PAGE_CACHE_SHIFT;
1638 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1639 unsigned long nr_pages = end_index - index + 1;
1643 if (op & EXTENT_CLEAR_UNLOCK)
1644 clear_bits |= EXTENT_LOCKED;
1645 if (op & EXTENT_CLEAR_DIRTY)
1646 clear_bits |= EXTENT_DIRTY;
1648 if (op & EXTENT_CLEAR_DELALLOC)
1649 clear_bits |= EXTENT_DELALLOC;
1651 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1652 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1653 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1654 EXTENT_SET_PRIVATE2)))
1657 while (nr_pages > 0) {
1658 ret = find_get_pages_contig(inode->i_mapping, index,
1659 min_t(unsigned long,
1660 nr_pages, ARRAY_SIZE(pages)), pages);
1661 for (i = 0; i < ret; i++) {
1663 if (op & EXTENT_SET_PRIVATE2)
1664 SetPagePrivate2(pages[i]);
1666 if (pages[i] == locked_page) {
1667 page_cache_release(pages[i]);
1670 if (op & EXTENT_CLEAR_DIRTY)
1671 clear_page_dirty_for_io(pages[i]);
1672 if (op & EXTENT_SET_WRITEBACK)
1673 set_page_writeback(pages[i]);
1674 if (op & EXTENT_END_WRITEBACK)
1675 end_page_writeback(pages[i]);
1676 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1677 unlock_page(pages[i]);
1678 page_cache_release(pages[i]);
1688 * count the number of bytes in the tree that have a given bit(s)
1689 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1690 * cached. The total number found is returned.
1692 u64 count_range_bits(struct extent_io_tree *tree,
1693 u64 *start, u64 search_end, u64 max_bytes,
1694 unsigned long bits, int contig)
1696 struct rb_node *node;
1697 struct extent_state *state;
1698 u64 cur_start = *start;
1699 u64 total_bytes = 0;
1703 if (search_end <= cur_start) {
1708 spin_lock(&tree->lock);
1709 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1710 total_bytes = tree->dirty_bytes;
1714 * this search will find all the extents that end after
1717 node = tree_search(tree, cur_start);
1722 state = rb_entry(node, struct extent_state, rb_node);
1723 if (state->start > search_end)
1725 if (contig && found && state->start > last + 1)
1727 if (state->end >= cur_start && (state->state & bits) == bits) {
1728 total_bytes += min(search_end, state->end) + 1 -
1729 max(cur_start, state->start);
1730 if (total_bytes >= max_bytes)
1733 *start = max(cur_start, state->start);
1737 } else if (contig && found) {
1740 node = rb_next(node);
1745 spin_unlock(&tree->lock);
1750 * set the private field for a given byte offset in the tree. If there isn't
1751 * an extent_state there already, this does nothing.
1753 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1755 struct rb_node *node;
1756 struct extent_state *state;
1759 spin_lock(&tree->lock);
1761 * this search will find all the extents that end after
1764 node = tree_search(tree, start);
1769 state = rb_entry(node, struct extent_state, rb_node);
1770 if (state->start != start) {
1774 state->private = private;
1776 spin_unlock(&tree->lock);
1780 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1782 struct rb_node *node;
1783 struct extent_state *state;
1786 spin_lock(&tree->lock);
1788 * this search will find all the extents that end after
1791 node = tree_search(tree, start);
1796 state = rb_entry(node, struct extent_state, rb_node);
1797 if (state->start != start) {
1801 *private = state->private;
1803 spin_unlock(&tree->lock);
1808 * searches a range in the state tree for a given mask.
1809 * If 'filled' == 1, this returns 1 only if every extent in the tree
1810 * has the bits set. Otherwise, 1 is returned if any bit in the
1811 * range is found set.
1813 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1814 int bits, int filled, struct extent_state *cached)
1816 struct extent_state *state = NULL;
1817 struct rb_node *node;
1820 spin_lock(&tree->lock);
1821 if (cached && cached->tree && cached->start <= start &&
1822 cached->end > start)
1823 node = &cached->rb_node;
1825 node = tree_search(tree, start);
1826 while (node && start <= end) {
1827 state = rb_entry(node, struct extent_state, rb_node);
1829 if (filled && state->start > start) {
1834 if (state->start > end)
1837 if (state->state & bits) {
1841 } else if (filled) {
1846 if (state->end == (u64)-1)
1849 start = state->end + 1;
1852 node = rb_next(node);
1859 spin_unlock(&tree->lock);
1864 * helper function to set a given page up to date if all the
1865 * extents in the tree for that page are up to date
1867 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
1869 u64 start = page_offset(page);
1870 u64 end = start + PAGE_CACHE_SIZE - 1;
1871 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1872 SetPageUptodate(page);
1876 * helper function to unlock a page if all the extents in the tree
1877 * for that page are unlocked
1879 static void check_page_locked(struct extent_io_tree *tree, struct page *page)
1881 u64 start = page_offset(page);
1882 u64 end = start + PAGE_CACHE_SIZE - 1;
1883 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1888 * helper function to end page writeback if all the extents
1889 * in the tree for that page are done with writeback
1891 static void check_page_writeback(struct extent_io_tree *tree,
1894 end_page_writeback(page);
1898 * When IO fails, either with EIO or csum verification fails, we
1899 * try other mirrors that might have a good copy of the data. This
1900 * io_failure_record is used to record state as we go through all the
1901 * mirrors. If another mirror has good data, the page is set up to date
1902 * and things continue. If a good mirror can't be found, the original
1903 * bio end_io callback is called to indicate things have failed.
1905 struct io_failure_record {
1910 unsigned long bio_flags;
1916 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1921 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1923 set_state_private(failure_tree, rec->start, 0);
1924 ret = clear_extent_bits(failure_tree, rec->start,
1925 rec->start + rec->len - 1,
1926 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1930 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1931 rec->start + rec->len - 1,
1932 EXTENT_DAMAGED, GFP_NOFS);
1940 static void repair_io_failure_callback(struct bio *bio, int err)
1942 complete(bio->bi_private);
1946 * this bypasses the standard btrfs submit functions deliberately, as
1947 * the standard behavior is to write all copies in a raid setup. here we only
1948 * want to write the one bad copy. so we do the mapping for ourselves and issue
1949 * submit_bio directly.
1950 * to avoid any synchronization issues, wait for the data after writing, which
1951 * actually prevents the read that triggered the error from finishing.
1952 * currently, there can be no more than two copies of every data bit. thus,
1953 * exactly one rewrite is required.
1955 int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
1956 u64 length, u64 logical, struct page *page,
1960 struct btrfs_device *dev;
1961 DECLARE_COMPLETION_ONSTACK(compl);
1964 struct btrfs_bio *bbio = NULL;
1965 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
1968 BUG_ON(!mirror_num);
1970 /* we can't repair anything in raid56 yet */
1971 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
1974 bio = bio_alloc(GFP_NOFS, 1);
1977 bio->bi_private = &compl;
1978 bio->bi_end_io = repair_io_failure_callback;
1980 map_length = length;
1982 ret = btrfs_map_block(fs_info, WRITE, logical,
1983 &map_length, &bbio, mirror_num);
1988 BUG_ON(mirror_num != bbio->mirror_num);
1989 sector = bbio->stripes[mirror_num-1].physical >> 9;
1990 bio->bi_sector = sector;
1991 dev = bbio->stripes[mirror_num-1].dev;
1993 if (!dev || !dev->bdev || !dev->writeable) {
1997 bio->bi_bdev = dev->bdev;
1998 bio_add_page(bio, page, length, start - page_offset(page));
1999 btrfsic_submit_bio(WRITE_SYNC, bio);
2000 wait_for_completion(&compl);
2002 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2003 /* try to remap that extent elsewhere? */
2005 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
2009 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
2010 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2011 start, rcu_str_deref(dev->name), sector);
2017 int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2020 u64 start = eb->start;
2021 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
2024 for (i = 0; i < num_pages; i++) {
2025 struct page *p = extent_buffer_page(eb, i);
2026 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
2027 start, p, mirror_num);
2030 start += PAGE_CACHE_SIZE;
2037 * each time an IO finishes, we do a fast check in the IO failure tree
2038 * to see if we need to process or clean up an io_failure_record
2040 static int clean_io_failure(u64 start, struct page *page)
2043 u64 private_failure;
2044 struct io_failure_record *failrec;
2045 struct btrfs_fs_info *fs_info;
2046 struct extent_state *state;
2050 struct inode *inode = page->mapping->host;
2053 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2054 (u64)-1, 1, EXTENT_DIRTY, 0);
2058 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2063 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2064 BUG_ON(!failrec->this_mirror);
2066 if (failrec->in_validation) {
2067 /* there was no real error, just free the record */
2068 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2074 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2075 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2078 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2080 if (state && state->start == failrec->start) {
2081 fs_info = BTRFS_I(inode)->root->fs_info;
2082 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2084 if (num_copies > 1) {
2085 ret = repair_io_failure(fs_info, start, failrec->len,
2086 failrec->logical, page,
2087 failrec->failed_mirror);
2095 ret = free_io_failure(inode, failrec, did_repair);
2101 * this is a generic handler for readpage errors (default
2102 * readpage_io_failed_hook). if other copies exist, read those and write back
2103 * good data to the failed position. does not investigate in remapping the
2104 * failed extent elsewhere, hoping the device will be smart enough to do this as
2108 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2109 u64 start, u64 end, int failed_mirror,
2110 struct extent_state *state)
2112 struct io_failure_record *failrec = NULL;
2114 struct extent_map *em;
2115 struct inode *inode = page->mapping->host;
2116 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2117 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2118 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2125 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2127 ret = get_state_private(failure_tree, start, &private);
2129 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2132 failrec->start = start;
2133 failrec->len = end - start + 1;
2134 failrec->this_mirror = 0;
2135 failrec->bio_flags = 0;
2136 failrec->in_validation = 0;
2138 read_lock(&em_tree->lock);
2139 em = lookup_extent_mapping(em_tree, start, failrec->len);
2141 read_unlock(&em_tree->lock);
2146 if (em->start > start || em->start + em->len < start) {
2147 free_extent_map(em);
2150 read_unlock(&em_tree->lock);
2156 logical = start - em->start;
2157 logical = em->block_start + logical;
2158 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2159 logical = em->block_start;
2160 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2161 extent_set_compress_type(&failrec->bio_flags,
2164 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2165 "len=%llu\n", logical, start, failrec->len);
2166 failrec->logical = logical;
2167 free_extent_map(em);
2169 /* set the bits in the private failure tree */
2170 ret = set_extent_bits(failure_tree, start, end,
2171 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2173 ret = set_state_private(failure_tree, start,
2174 (u64)(unsigned long)failrec);
2175 /* set the bits in the inode's tree */
2177 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2184 failrec = (struct io_failure_record *)(unsigned long)private;
2185 pr_debug("bio_readpage_error: (found) logical=%llu, "
2186 "start=%llu, len=%llu, validation=%d\n",
2187 failrec->logical, failrec->start, failrec->len,
2188 failrec->in_validation);
2190 * when data can be on disk more than twice, add to failrec here
2191 * (e.g. with a list for failed_mirror) to make
2192 * clean_io_failure() clean all those errors at once.
2195 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2196 failrec->logical, failrec->len);
2197 if (num_copies == 1) {
2199 * we only have a single copy of the data, so don't bother with
2200 * all the retry and error correction code that follows. no
2201 * matter what the error is, it is very likely to persist.
2203 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2204 "state=%p, num_copies=%d, next_mirror %d, "
2205 "failed_mirror %d\n", state, num_copies,
2206 failrec->this_mirror, failed_mirror);
2207 free_io_failure(inode, failrec, 0);
2212 spin_lock(&tree->lock);
2213 state = find_first_extent_bit_state(tree, failrec->start,
2215 if (state && state->start != failrec->start)
2217 spin_unlock(&tree->lock);
2221 * there are two premises:
2222 * a) deliver good data to the caller
2223 * b) correct the bad sectors on disk
2225 if (failed_bio->bi_vcnt > 1) {
2227 * to fulfill b), we need to know the exact failing sectors, as
2228 * we don't want to rewrite any more than the failed ones. thus,
2229 * we need separate read requests for the failed bio
2231 * if the following BUG_ON triggers, our validation request got
2232 * merged. we need separate requests for our algorithm to work.
2234 BUG_ON(failrec->in_validation);
2235 failrec->in_validation = 1;
2236 failrec->this_mirror = failed_mirror;
2237 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2240 * we're ready to fulfill a) and b) alongside. get a good copy
2241 * of the failed sector and if we succeed, we have setup
2242 * everything for repair_io_failure to do the rest for us.
2244 if (failrec->in_validation) {
2245 BUG_ON(failrec->this_mirror != failed_mirror);
2246 failrec->in_validation = 0;
2247 failrec->this_mirror = 0;
2249 failrec->failed_mirror = failed_mirror;
2250 failrec->this_mirror++;
2251 if (failrec->this_mirror == failed_mirror)
2252 failrec->this_mirror++;
2253 read_mode = READ_SYNC;
2256 if (!state || failrec->this_mirror > num_copies) {
2257 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2258 "next_mirror %d, failed_mirror %d\n", state,
2259 num_copies, failrec->this_mirror, failed_mirror);
2260 free_io_failure(inode, failrec, 0);
2264 bio = bio_alloc(GFP_NOFS, 1);
2266 free_io_failure(inode, failrec, 0);
2269 bio->bi_private = state;
2270 bio->bi_end_io = failed_bio->bi_end_io;
2271 bio->bi_sector = failrec->logical >> 9;
2272 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2275 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2277 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2278 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2279 failrec->this_mirror, num_copies, failrec->in_validation);
2281 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2282 failrec->this_mirror,
2283 failrec->bio_flags, 0);
2287 /* lots and lots of room for performance fixes in the end_bio funcs */
2289 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2291 int uptodate = (err == 0);
2292 struct extent_io_tree *tree;
2295 tree = &BTRFS_I(page->mapping->host)->io_tree;
2297 if (tree->ops && tree->ops->writepage_end_io_hook) {
2298 ret = tree->ops->writepage_end_io_hook(page, start,
2299 end, NULL, uptodate);
2305 ClearPageUptodate(page);
2312 * after a writepage IO is done, we need to:
2313 * clear the uptodate bits on error
2314 * clear the writeback bits in the extent tree for this IO
2315 * end_page_writeback if the page has no more pending IO
2317 * Scheduling is not allowed, so the extent state tree is expected
2318 * to have one and only one object corresponding to this IO.
2320 static void end_bio_extent_writepage(struct bio *bio, int err)
2322 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2323 struct extent_io_tree *tree;
2329 struct page *page = bvec->bv_page;
2330 tree = &BTRFS_I(page->mapping->host)->io_tree;
2332 start = page_offset(page) + bvec->bv_offset;
2333 end = start + bvec->bv_len - 1;
2335 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2340 if (--bvec >= bio->bi_io_vec)
2341 prefetchw(&bvec->bv_page->flags);
2343 if (end_extent_writepage(page, err, start, end))
2347 end_page_writeback(page);
2349 check_page_writeback(tree, page);
2350 } while (bvec >= bio->bi_io_vec);
2356 * after a readpage IO is done, we need to:
2357 * clear the uptodate bits on error
2358 * set the uptodate bits if things worked
2359 * set the page up to date if all extents in the tree are uptodate
2360 * clear the lock bit in the extent tree
2361 * unlock the page if there are no other extents locked for it
2363 * Scheduling is not allowed, so the extent state tree is expected
2364 * to have one and only one object corresponding to this IO.
2366 static void end_bio_extent_readpage(struct bio *bio, int err)
2368 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2369 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2370 struct bio_vec *bvec = bio->bi_io_vec;
2371 struct extent_io_tree *tree;
2382 struct page *page = bvec->bv_page;
2383 struct extent_state *cached = NULL;
2384 struct extent_state *state;
2386 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2387 "mirror=%ld\n", (u64)bio->bi_sector, err,
2388 (long int)bio->bi_bdev);
2389 tree = &BTRFS_I(page->mapping->host)->io_tree;
2391 start = page_offset(page) + bvec->bv_offset;
2392 end = start + bvec->bv_len - 1;
2394 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2399 if (++bvec <= bvec_end)
2400 prefetchw(&bvec->bv_page->flags);
2402 spin_lock(&tree->lock);
2403 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2404 if (state && state->start == start) {
2406 * take a reference on the state, unlock will drop
2409 cache_state(state, &cached);
2411 spin_unlock(&tree->lock);
2413 mirror = (int)(unsigned long)bio->bi_bdev;
2414 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2415 ret = tree->ops->readpage_end_io_hook(page, start, end,
2420 clean_io_failure(start, page);
2423 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
2424 ret = tree->ops->readpage_io_failed_hook(page, mirror);
2426 test_bit(BIO_UPTODATE, &bio->bi_flags))
2428 } else if (!uptodate) {
2430 * The generic bio_readpage_error handles errors the
2431 * following way: If possible, new read requests are
2432 * created and submitted and will end up in
2433 * end_bio_extent_readpage as well (if we're lucky, not
2434 * in the !uptodate case). In that case it returns 0 and
2435 * we just go on with the next page in our bio. If it
2436 * can't handle the error it will return -EIO and we
2437 * remain responsible for that page.
2439 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
2442 test_bit(BIO_UPTODATE, &bio->bi_flags);
2445 uncache_state(&cached);
2450 if (uptodate && tree->track_uptodate) {
2451 set_extent_uptodate(tree, start, end, &cached,
2454 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2458 SetPageUptodate(page);
2460 ClearPageUptodate(page);
2466 check_page_uptodate(tree, page);
2468 ClearPageUptodate(page);
2471 check_page_locked(tree, page);
2473 } while (bvec <= bvec_end);
2479 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2484 bio = bio_alloc(gfp_flags, nr_vecs);
2486 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2487 while (!bio && (nr_vecs /= 2))
2488 bio = bio_alloc(gfp_flags, nr_vecs);
2493 bio->bi_bdev = bdev;
2494 bio->bi_sector = first_sector;
2499 static int __must_check submit_one_bio(int rw, struct bio *bio,
2500 int mirror_num, unsigned long bio_flags)
2503 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2504 struct page *page = bvec->bv_page;
2505 struct extent_io_tree *tree = bio->bi_private;
2508 start = page_offset(page) + bvec->bv_offset;
2510 bio->bi_private = NULL;
2514 if (tree->ops && tree->ops->submit_bio_hook)
2515 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2516 mirror_num, bio_flags, start);
2518 btrfsic_submit_bio(rw, bio);
2520 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2526 static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
2527 unsigned long offset, size_t size, struct bio *bio,
2528 unsigned long bio_flags)
2531 if (tree->ops && tree->ops->merge_bio_hook)
2532 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
2539 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2540 struct page *page, sector_t sector,
2541 size_t size, unsigned long offset,
2542 struct block_device *bdev,
2543 struct bio **bio_ret,
2544 unsigned long max_pages,
2545 bio_end_io_t end_io_func,
2547 unsigned long prev_bio_flags,
2548 unsigned long bio_flags)
2554 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2555 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2556 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2558 if (bio_ret && *bio_ret) {
2561 contig = bio->bi_sector == sector;
2563 contig = bio_end_sector(bio) == sector;
2565 if (prev_bio_flags != bio_flags || !contig ||
2566 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
2567 bio_add_page(bio, page, page_size, offset) < page_size) {
2568 ret = submit_one_bio(rw, bio, mirror_num,
2577 if (this_compressed)
2580 nr = bio_get_nr_vecs(bdev);
2582 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2586 bio_add_page(bio, page, page_size, offset);
2587 bio->bi_end_io = end_io_func;
2588 bio->bi_private = tree;
2593 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2598 void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2600 if (!PagePrivate(page)) {
2601 SetPagePrivate(page);
2602 page_cache_get(page);
2603 set_page_private(page, (unsigned long)eb);
2605 WARN_ON(page->private != (unsigned long)eb);
2609 void set_page_extent_mapped(struct page *page)
2611 if (!PagePrivate(page)) {
2612 SetPagePrivate(page);
2613 page_cache_get(page);
2614 set_page_private(page, EXTENT_PAGE_PRIVATE);
2619 * basic readpage implementation. Locked extent state structs are inserted
2620 * into the tree that are removed when the IO is done (by the end_io
2622 * XXX JDM: This needs looking at to ensure proper page locking
2624 static int __extent_read_full_page(struct extent_io_tree *tree,
2626 get_extent_t *get_extent,
2627 struct bio **bio, int mirror_num,
2628 unsigned long *bio_flags)
2630 struct inode *inode = page->mapping->host;
2631 u64 start = page_offset(page);
2632 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2636 u64 last_byte = i_size_read(inode);
2640 struct extent_map *em;
2641 struct block_device *bdev;
2642 struct btrfs_ordered_extent *ordered;
2645 size_t pg_offset = 0;
2647 size_t disk_io_size;
2648 size_t blocksize = inode->i_sb->s_blocksize;
2649 unsigned long this_bio_flag = 0;
2651 set_page_extent_mapped(page);
2653 if (!PageUptodate(page)) {
2654 if (cleancache_get_page(page) == 0) {
2655 BUG_ON(blocksize != PAGE_SIZE);
2662 lock_extent(tree, start, end);
2663 ordered = btrfs_lookup_ordered_extent(inode, start);
2666 unlock_extent(tree, start, end);
2667 btrfs_start_ordered_extent(inode, ordered, 1);
2668 btrfs_put_ordered_extent(ordered);
2671 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2673 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2676 iosize = PAGE_CACHE_SIZE - zero_offset;
2677 userpage = kmap_atomic(page);
2678 memset(userpage + zero_offset, 0, iosize);
2679 flush_dcache_page(page);
2680 kunmap_atomic(userpage);
2683 while (cur <= end) {
2684 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2686 if (cur >= last_byte) {
2688 struct extent_state *cached = NULL;
2690 iosize = PAGE_CACHE_SIZE - pg_offset;
2691 userpage = kmap_atomic(page);
2692 memset(userpage + pg_offset, 0, iosize);
2693 flush_dcache_page(page);
2694 kunmap_atomic(userpage);
2695 set_extent_uptodate(tree, cur, cur + iosize - 1,
2697 unlock_extent_cached(tree, cur, cur + iosize - 1,
2701 em = get_extent(inode, page, pg_offset, cur,
2703 if (IS_ERR_OR_NULL(em)) {
2705 unlock_extent(tree, cur, end);
2708 extent_offset = cur - em->start;
2709 BUG_ON(extent_map_end(em) <= cur);
2712 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2713 this_bio_flag = EXTENT_BIO_COMPRESSED;
2714 extent_set_compress_type(&this_bio_flag,
2718 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2719 cur_end = min(extent_map_end(em) - 1, end);
2720 iosize = ALIGN(iosize, blocksize);
2721 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2722 disk_io_size = em->block_len;
2723 sector = em->block_start >> 9;
2725 sector = (em->block_start + extent_offset) >> 9;
2726 disk_io_size = iosize;
2729 block_start = em->block_start;
2730 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2731 block_start = EXTENT_MAP_HOLE;
2732 free_extent_map(em);
2735 /* we've found a hole, just zero and go on */
2736 if (block_start == EXTENT_MAP_HOLE) {
2738 struct extent_state *cached = NULL;
2740 userpage = kmap_atomic(page);
2741 memset(userpage + pg_offset, 0, iosize);
2742 flush_dcache_page(page);
2743 kunmap_atomic(userpage);
2745 set_extent_uptodate(tree, cur, cur + iosize - 1,
2747 unlock_extent_cached(tree, cur, cur + iosize - 1,
2750 pg_offset += iosize;
2753 /* the get_extent function already copied into the page */
2754 if (test_range_bit(tree, cur, cur_end,
2755 EXTENT_UPTODATE, 1, NULL)) {
2756 check_page_uptodate(tree, page);
2757 unlock_extent(tree, cur, cur + iosize - 1);
2759 pg_offset += iosize;
2762 /* we have an inline extent but it didn't get marked up
2763 * to date. Error out
2765 if (block_start == EXTENT_MAP_INLINE) {
2767 unlock_extent(tree, cur, cur + iosize - 1);
2769 pg_offset += iosize;
2774 ret = submit_extent_page(READ, tree, page,
2775 sector, disk_io_size, pg_offset,
2777 end_bio_extent_readpage, mirror_num,
2782 *bio_flags = this_bio_flag;
2785 unlock_extent(tree, cur, cur + iosize - 1);
2788 pg_offset += iosize;
2792 if (!PageError(page))
2793 SetPageUptodate(page);
2799 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2800 get_extent_t *get_extent, int mirror_num)
2802 struct bio *bio = NULL;
2803 unsigned long bio_flags = 0;
2806 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2809 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2813 static noinline void update_nr_written(struct page *page,
2814 struct writeback_control *wbc,
2815 unsigned long nr_written)
2817 wbc->nr_to_write -= nr_written;
2818 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2819 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2820 page->mapping->writeback_index = page->index + nr_written;
2824 * the writepage semantics are similar to regular writepage. extent
2825 * records are inserted to lock ranges in the tree, and as dirty areas
2826 * are found, they are marked writeback. Then the lock bits are removed
2827 * and the end_io handler clears the writeback ranges
2829 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2832 struct inode *inode = page->mapping->host;
2833 struct extent_page_data *epd = data;
2834 struct extent_io_tree *tree = epd->tree;
2835 u64 start = page_offset(page);
2837 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2841 u64 last_byte = i_size_read(inode);
2845 struct extent_state *cached_state = NULL;
2846 struct extent_map *em;
2847 struct block_device *bdev;
2850 size_t pg_offset = 0;
2852 loff_t i_size = i_size_read(inode);
2853 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2859 unsigned long nr_written = 0;
2860 bool fill_delalloc = true;
2862 if (wbc->sync_mode == WB_SYNC_ALL)
2863 write_flags = WRITE_SYNC;
2865 write_flags = WRITE;
2867 trace___extent_writepage(page, inode, wbc);
2869 WARN_ON(!PageLocked(page));
2871 ClearPageError(page);
2873 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2874 if (page->index > end_index ||
2875 (page->index == end_index && !pg_offset)) {
2876 page->mapping->a_ops->invalidatepage(page, 0);
2881 if (page->index == end_index) {
2884 userpage = kmap_atomic(page);
2885 memset(userpage + pg_offset, 0,
2886 PAGE_CACHE_SIZE - pg_offset);
2887 kunmap_atomic(userpage);
2888 flush_dcache_page(page);
2892 set_page_extent_mapped(page);
2894 if (!tree->ops || !tree->ops->fill_delalloc)
2895 fill_delalloc = false;
2897 delalloc_start = start;
2900 if (!epd->extent_locked && fill_delalloc) {
2901 u64 delalloc_to_write = 0;
2903 * make sure the wbc mapping index is at least updated
2906 update_nr_written(page, wbc, 0);
2908 while (delalloc_end < page_end) {
2909 nr_delalloc = find_lock_delalloc_range(inode, tree,
2914 if (nr_delalloc == 0) {
2915 delalloc_start = delalloc_end + 1;
2918 ret = tree->ops->fill_delalloc(inode, page,
2923 /* File system has been set read-only */
2929 * delalloc_end is already one less than the total
2930 * length, so we don't subtract one from
2933 delalloc_to_write += (delalloc_end - delalloc_start +
2936 delalloc_start = delalloc_end + 1;
2938 if (wbc->nr_to_write < delalloc_to_write) {
2941 if (delalloc_to_write < thresh * 2)
2942 thresh = delalloc_to_write;
2943 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2947 /* did the fill delalloc function already unlock and start
2953 * we've unlocked the page, so we can't update
2954 * the mapping's writeback index, just update
2957 wbc->nr_to_write -= nr_written;
2961 if (tree->ops && tree->ops->writepage_start_hook) {
2962 ret = tree->ops->writepage_start_hook(page, start,
2965 /* Fixup worker will requeue */
2967 wbc->pages_skipped++;
2969 redirty_page_for_writepage(wbc, page);
2970 update_nr_written(page, wbc, nr_written);
2978 * we don't want to touch the inode after unlocking the page,
2979 * so we update the mapping writeback index now
2981 update_nr_written(page, wbc, nr_written + 1);
2984 if (last_byte <= start) {
2985 if (tree->ops && tree->ops->writepage_end_io_hook)
2986 tree->ops->writepage_end_io_hook(page, start,
2991 blocksize = inode->i_sb->s_blocksize;
2993 while (cur <= end) {
2994 if (cur >= last_byte) {
2995 if (tree->ops && tree->ops->writepage_end_io_hook)
2996 tree->ops->writepage_end_io_hook(page, cur,
3000 em = epd->get_extent(inode, page, pg_offset, cur,
3002 if (IS_ERR_OR_NULL(em)) {
3007 extent_offset = cur - em->start;
3008 BUG_ON(extent_map_end(em) <= cur);
3010 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3011 iosize = ALIGN(iosize, blocksize);
3012 sector = (em->block_start + extent_offset) >> 9;
3014 block_start = em->block_start;
3015 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3016 free_extent_map(em);
3020 * compressed and inline extents are written through other
3023 if (compressed || block_start == EXTENT_MAP_HOLE ||
3024 block_start == EXTENT_MAP_INLINE) {
3026 * end_io notification does not happen here for
3027 * compressed extents
3029 if (!compressed && tree->ops &&
3030 tree->ops->writepage_end_io_hook)
3031 tree->ops->writepage_end_io_hook(page, cur,
3034 else if (compressed) {
3035 /* we don't want to end_page_writeback on
3036 * a compressed extent. this happens
3043 pg_offset += iosize;
3046 /* leave this out until we have a page_mkwrite call */
3047 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
3048 EXTENT_DIRTY, 0, NULL)) {
3050 pg_offset += iosize;
3054 if (tree->ops && tree->ops->writepage_io_hook) {
3055 ret = tree->ops->writepage_io_hook(page, cur,
3063 unsigned long max_nr = end_index + 1;
3065 set_range_writeback(tree, cur, cur + iosize - 1);
3066 if (!PageWriteback(page)) {
3067 printk(KERN_ERR "btrfs warning page %lu not "
3068 "writeback, cur %llu end %llu\n",
3069 page->index, (unsigned long long)cur,
3070 (unsigned long long)end);
3073 ret = submit_extent_page(write_flags, tree, page,
3074 sector, iosize, pg_offset,
3075 bdev, &epd->bio, max_nr,
3076 end_bio_extent_writepage,
3082 pg_offset += iosize;
3087 /* make sure the mapping tag for page dirty gets cleared */
3088 set_page_writeback(page);
3089 end_page_writeback(page);
3095 /* drop our reference on any cached states */
3096 free_extent_state(cached_state);
3100 static int eb_wait(void *word)
3106 static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3108 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3109 TASK_UNINTERRUPTIBLE);
3112 static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3113 struct btrfs_fs_info *fs_info,
3114 struct extent_page_data *epd)
3116 unsigned long i, num_pages;
3120 if (!btrfs_try_tree_write_lock(eb)) {
3122 flush_write_bio(epd);
3123 btrfs_tree_lock(eb);
3126 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3127 btrfs_tree_unlock(eb);
3131 flush_write_bio(epd);
3135 wait_on_extent_buffer_writeback(eb);
3136 btrfs_tree_lock(eb);
3137 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3139 btrfs_tree_unlock(eb);
3144 * We need to do this to prevent races in people who check if the eb is
3145 * under IO since we can end up having no IO bits set for a short period
3148 spin_lock(&eb->refs_lock);
3149 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3150 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3151 spin_unlock(&eb->refs_lock);
3152 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3153 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3155 fs_info->dirty_metadata_batch);
3158 spin_unlock(&eb->refs_lock);
3161 btrfs_tree_unlock(eb);
3166 num_pages = num_extent_pages(eb->start, eb->len);
3167 for (i = 0; i < num_pages; i++) {
3168 struct page *p = extent_buffer_page(eb, i);
3170 if (!trylock_page(p)) {
3172 flush_write_bio(epd);
3182 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3184 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3185 smp_mb__after_clear_bit();
3186 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3189 static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3191 int uptodate = err == 0;
3192 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3193 struct extent_buffer *eb;
3197 struct page *page = bvec->bv_page;
3200 eb = (struct extent_buffer *)page->private;
3202 done = atomic_dec_and_test(&eb->io_pages);
3204 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3205 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3206 ClearPageUptodate(page);
3210 end_page_writeback(page);
3215 end_extent_buffer_writeback(eb);
3216 } while (bvec >= bio->bi_io_vec);
3222 static int write_one_eb(struct extent_buffer *eb,
3223 struct btrfs_fs_info *fs_info,
3224 struct writeback_control *wbc,
3225 struct extent_page_data *epd)
3227 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3228 u64 offset = eb->start;
3229 unsigned long i, num_pages;
3230 unsigned long bio_flags = 0;
3231 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3234 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3235 num_pages = num_extent_pages(eb->start, eb->len);
3236 atomic_set(&eb->io_pages, num_pages);
3237 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3238 bio_flags = EXTENT_BIO_TREE_LOG;
3240 for (i = 0; i < num_pages; i++) {
3241 struct page *p = extent_buffer_page(eb, i);
3243 clear_page_dirty_for_io(p);
3244 set_page_writeback(p);
3245 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3246 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3247 -1, end_bio_extent_buffer_writepage,
3248 0, epd->bio_flags, bio_flags);
3249 epd->bio_flags = bio_flags;
3251 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3253 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3254 end_extent_buffer_writeback(eb);
3258 offset += PAGE_CACHE_SIZE;
3259 update_nr_written(p, wbc, 1);
3263 if (unlikely(ret)) {
3264 for (; i < num_pages; i++) {
3265 struct page *p = extent_buffer_page(eb, i);
3273 int btree_write_cache_pages(struct address_space *mapping,
3274 struct writeback_control *wbc)
3276 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3277 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3278 struct extent_buffer *eb, *prev_eb = NULL;
3279 struct extent_page_data epd = {
3283 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3288 int nr_to_write_done = 0;
3289 struct pagevec pvec;
3292 pgoff_t end; /* Inclusive */
3296 pagevec_init(&pvec, 0);
3297 if (wbc->range_cyclic) {
3298 index = mapping->writeback_index; /* Start from prev offset */
3301 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3302 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3305 if (wbc->sync_mode == WB_SYNC_ALL)
3306 tag = PAGECACHE_TAG_TOWRITE;
3308 tag = PAGECACHE_TAG_DIRTY;
3310 if (wbc->sync_mode == WB_SYNC_ALL)
3311 tag_pages_for_writeback(mapping, index, end);
3312 while (!done && !nr_to_write_done && (index <= end) &&
3313 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3314 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3318 for (i = 0; i < nr_pages; i++) {
3319 struct page *page = pvec.pages[i];
3321 if (!PagePrivate(page))
3324 if (!wbc->range_cyclic && page->index > end) {
3329 spin_lock(&mapping->private_lock);
3330 if (!PagePrivate(page)) {
3331 spin_unlock(&mapping->private_lock);
3335 eb = (struct extent_buffer *)page->private;
3338 * Shouldn't happen and normally this would be a BUG_ON
3339 * but no sense in crashing the users box for something
3340 * we can survive anyway.
3343 spin_unlock(&mapping->private_lock);
3348 if (eb == prev_eb) {
3349 spin_unlock(&mapping->private_lock);
3353 ret = atomic_inc_not_zero(&eb->refs);
3354 spin_unlock(&mapping->private_lock);
3359 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3361 free_extent_buffer(eb);
3365 ret = write_one_eb(eb, fs_info, wbc, &epd);
3368 free_extent_buffer(eb);
3371 free_extent_buffer(eb);
3374 * the filesystem may choose to bump up nr_to_write.
3375 * We have to make sure to honor the new nr_to_write
3378 nr_to_write_done = wbc->nr_to_write <= 0;
3380 pagevec_release(&pvec);
3383 if (!scanned && !done) {
3385 * We hit the last page and there is more work to be done: wrap
3386 * back to the start of the file
3392 flush_write_bio(&epd);
3397 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3398 * @mapping: address space structure to write
3399 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3400 * @writepage: function called for each page
3401 * @data: data passed to writepage function
3403 * If a page is already under I/O, write_cache_pages() skips it, even
3404 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3405 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3406 * and msync() need to guarantee that all the data which was dirty at the time
3407 * the call was made get new I/O started against them. If wbc->sync_mode is
3408 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3409 * existing IO to complete.
3411 static int extent_write_cache_pages(struct extent_io_tree *tree,
3412 struct address_space *mapping,
3413 struct writeback_control *wbc,
3414 writepage_t writepage, void *data,
3415 void (*flush_fn)(void *))
3417 struct inode *inode = mapping->host;
3420 int nr_to_write_done = 0;
3421 struct pagevec pvec;
3424 pgoff_t end; /* Inclusive */
3429 * We have to hold onto the inode so that ordered extents can do their
3430 * work when the IO finishes. The alternative to this is failing to add
3431 * an ordered extent if the igrab() fails there and that is a huge pain
3432 * to deal with, so instead just hold onto the inode throughout the
3433 * writepages operation. If it fails here we are freeing up the inode
3434 * anyway and we'd rather not waste our time writing out stuff that is
3435 * going to be truncated anyway.
3440 pagevec_init(&pvec, 0);
3441 if (wbc->range_cyclic) {
3442 index = mapping->writeback_index; /* Start from prev offset */
3445 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3446 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3449 if (wbc->sync_mode == WB_SYNC_ALL)
3450 tag = PAGECACHE_TAG_TOWRITE;
3452 tag = PAGECACHE_TAG_DIRTY;
3454 if (wbc->sync_mode == WB_SYNC_ALL)
3455 tag_pages_for_writeback(mapping, index, end);
3456 while (!done && !nr_to_write_done && (index <= end) &&
3457 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3458 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3462 for (i = 0; i < nr_pages; i++) {
3463 struct page *page = pvec.pages[i];
3466 * At this point we hold neither mapping->tree_lock nor
3467 * lock on the page itself: the page may be truncated or
3468 * invalidated (changing page->mapping to NULL), or even
3469 * swizzled back from swapper_space to tmpfs file
3472 if (!trylock_page(page)) {
3477 if (unlikely(page->mapping != mapping)) {
3482 if (!wbc->range_cyclic && page->index > end) {
3488 if (wbc->sync_mode != WB_SYNC_NONE) {
3489 if (PageWriteback(page))
3491 wait_on_page_writeback(page);
3494 if (PageWriteback(page) ||
3495 !clear_page_dirty_for_io(page)) {
3500 ret = (*writepage)(page, wbc, data);
3502 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3510 * the filesystem may choose to bump up nr_to_write.
3511 * We have to make sure to honor the new nr_to_write
3514 nr_to_write_done = wbc->nr_to_write <= 0;
3516 pagevec_release(&pvec);
3519 if (!scanned && !done) {
3521 * We hit the last page and there is more work to be done: wrap
3522 * back to the start of the file
3528 btrfs_add_delayed_iput(inode);
3532 static void flush_epd_write_bio(struct extent_page_data *epd)
3541 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
3542 BUG_ON(ret < 0); /* -ENOMEM */
3547 static noinline void flush_write_bio(void *data)
3549 struct extent_page_data *epd = data;
3550 flush_epd_write_bio(epd);
3553 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3554 get_extent_t *get_extent,
3555 struct writeback_control *wbc)
3558 struct extent_page_data epd = {
3561 .get_extent = get_extent,
3563 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3567 ret = __extent_writepage(page, wbc, &epd);
3569 flush_epd_write_bio(&epd);
3573 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3574 u64 start, u64 end, get_extent_t *get_extent,
3578 struct address_space *mapping = inode->i_mapping;
3580 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3583 struct extent_page_data epd = {
3586 .get_extent = get_extent,
3588 .sync_io = mode == WB_SYNC_ALL,
3591 struct writeback_control wbc_writepages = {
3593 .nr_to_write = nr_pages * 2,
3594 .range_start = start,
3595 .range_end = end + 1,
3598 while (start <= end) {
3599 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3600 if (clear_page_dirty_for_io(page))
3601 ret = __extent_writepage(page, &wbc_writepages, &epd);
3603 if (tree->ops && tree->ops->writepage_end_io_hook)
3604 tree->ops->writepage_end_io_hook(page, start,
3605 start + PAGE_CACHE_SIZE - 1,
3609 page_cache_release(page);
3610 start += PAGE_CACHE_SIZE;
3613 flush_epd_write_bio(&epd);
3617 int extent_writepages(struct extent_io_tree *tree,
3618 struct address_space *mapping,
3619 get_extent_t *get_extent,
3620 struct writeback_control *wbc)
3623 struct extent_page_data epd = {
3626 .get_extent = get_extent,
3628 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3632 ret = extent_write_cache_pages(tree, mapping, wbc,
3633 __extent_writepage, &epd,
3635 flush_epd_write_bio(&epd);
3639 int extent_readpages(struct extent_io_tree *tree,
3640 struct address_space *mapping,
3641 struct list_head *pages, unsigned nr_pages,
3642 get_extent_t get_extent)
3644 struct bio *bio = NULL;
3646 unsigned long bio_flags = 0;
3647 struct page *pagepool[16];
3652 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3653 page = list_entry(pages->prev, struct page, lru);
3655 prefetchw(&page->flags);
3656 list_del(&page->lru);
3657 if (add_to_page_cache_lru(page, mapping,
3658 page->index, GFP_NOFS)) {
3659 page_cache_release(page);
3663 pagepool[nr++] = page;
3664 if (nr < ARRAY_SIZE(pagepool))
3666 for (i = 0; i < nr; i++) {
3667 __extent_read_full_page(tree, pagepool[i], get_extent,
3668 &bio, 0, &bio_flags);
3669 page_cache_release(pagepool[i]);
3673 for (i = 0; i < nr; i++) {
3674 __extent_read_full_page(tree, pagepool[i], get_extent,
3675 &bio, 0, &bio_flags);
3676 page_cache_release(pagepool[i]);
3679 BUG_ON(!list_empty(pages));
3681 return submit_one_bio(READ, bio, 0, bio_flags);
3686 * basic invalidatepage code, this waits on any locked or writeback
3687 * ranges corresponding to the page, and then deletes any extent state
3688 * records from the tree
3690 int extent_invalidatepage(struct extent_io_tree *tree,
3691 struct page *page, unsigned long offset)
3693 struct extent_state *cached_state = NULL;
3694 u64 start = page_offset(page);
3695 u64 end = start + PAGE_CACHE_SIZE - 1;
3696 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3698 start += ALIGN(offset, blocksize);
3702 lock_extent_bits(tree, start, end, 0, &cached_state);
3703 wait_on_page_writeback(page);
3704 clear_extent_bit(tree, start, end,
3705 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3706 EXTENT_DO_ACCOUNTING,
3707 1, 1, &cached_state, GFP_NOFS);
3712 * a helper for releasepage, this tests for areas of the page that
3713 * are locked or under IO and drops the related state bits if it is safe
3716 int try_release_extent_state(struct extent_map_tree *map,
3717 struct extent_io_tree *tree, struct page *page,
3720 u64 start = page_offset(page);
3721 u64 end = start + PAGE_CACHE_SIZE - 1;
3724 if (test_range_bit(tree, start, end,
3725 EXTENT_IOBITS, 0, NULL))
3728 if ((mask & GFP_NOFS) == GFP_NOFS)
3731 * at this point we can safely clear everything except the
3732 * locked bit and the nodatasum bit
3734 ret = clear_extent_bit(tree, start, end,
3735 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3738 /* if clear_extent_bit failed for enomem reasons,
3739 * we can't allow the release to continue.
3750 * a helper for releasepage. As long as there are no locked extents
3751 * in the range corresponding to the page, both state records and extent
3752 * map records are removed
3754 int try_release_extent_mapping(struct extent_map_tree *map,
3755 struct extent_io_tree *tree, struct page *page,
3758 struct extent_map *em;
3759 u64 start = page_offset(page);
3760 u64 end = start + PAGE_CACHE_SIZE - 1;
3762 if ((mask & __GFP_WAIT) &&
3763 page->mapping->host->i_size > 16 * 1024 * 1024) {
3765 while (start <= end) {
3766 len = end - start + 1;
3767 write_lock(&map->lock);
3768 em = lookup_extent_mapping(map, start, len);
3770 write_unlock(&map->lock);
3773 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3774 em->start != start) {
3775 write_unlock(&map->lock);
3776 free_extent_map(em);
3779 if (!test_range_bit(tree, em->start,
3780 extent_map_end(em) - 1,
3781 EXTENT_LOCKED | EXTENT_WRITEBACK,
3783 remove_extent_mapping(map, em);
3784 /* once for the rb tree */
3785 free_extent_map(em);
3787 start = extent_map_end(em);
3788 write_unlock(&map->lock);
3791 free_extent_map(em);
3794 return try_release_extent_state(map, tree, page, mask);
3798 * helper function for fiemap, which doesn't want to see any holes.
3799 * This maps until we find something past 'last'
3801 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3804 get_extent_t *get_extent)
3806 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3807 struct extent_map *em;
3814 len = last - offset;
3817 len = ALIGN(len, sectorsize);
3818 em = get_extent(inode, NULL, 0, offset, len, 0);
3819 if (IS_ERR_OR_NULL(em))
3822 /* if this isn't a hole return it */
3823 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3824 em->block_start != EXTENT_MAP_HOLE) {
3828 /* this is a hole, advance to the next extent */
3829 offset = extent_map_end(em);
3830 free_extent_map(em);
3837 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3838 __u64 start, __u64 len, get_extent_t *get_extent)
3842 u64 max = start + len;
3846 u64 last_for_get_extent = 0;
3848 u64 isize = i_size_read(inode);
3849 struct btrfs_key found_key;
3850 struct extent_map *em = NULL;
3851 struct extent_state *cached_state = NULL;
3852 struct btrfs_path *path;
3853 struct btrfs_file_extent_item *item;
3858 unsigned long emflags;
3863 path = btrfs_alloc_path();
3866 path->leave_spinning = 1;
3868 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3869 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3872 * lookup the last file extent. We're not using i_size here
3873 * because there might be preallocation past i_size
3875 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3876 path, btrfs_ino(inode), -1, 0);
3878 btrfs_free_path(path);
3883 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3884 struct btrfs_file_extent_item);
3885 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3886 found_type = btrfs_key_type(&found_key);
3888 /* No extents, but there might be delalloc bits */
3889 if (found_key.objectid != btrfs_ino(inode) ||
3890 found_type != BTRFS_EXTENT_DATA_KEY) {
3891 /* have to trust i_size as the end */
3893 last_for_get_extent = isize;
3896 * remember the start of the last extent. There are a
3897 * bunch of different factors that go into the length of the
3898 * extent, so its much less complex to remember where it started
3900 last = found_key.offset;
3901 last_for_get_extent = last + 1;
3903 btrfs_free_path(path);
3906 * we might have some extents allocated but more delalloc past those
3907 * extents. so, we trust isize unless the start of the last extent is
3912 last_for_get_extent = isize;
3915 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3918 em = get_extent_skip_holes(inode, start, last_for_get_extent,
3928 u64 offset_in_extent;
3930 /* break if the extent we found is outside the range */
3931 if (em->start >= max || extent_map_end(em) < off)
3935 * get_extent may return an extent that starts before our
3936 * requested range. We have to make sure the ranges
3937 * we return to fiemap always move forward and don't
3938 * overlap, so adjust the offsets here
3940 em_start = max(em->start, off);
3943 * record the offset from the start of the extent
3944 * for adjusting the disk offset below
3946 offset_in_extent = em_start - em->start;
3947 em_end = extent_map_end(em);
3948 em_len = em_end - em_start;
3949 emflags = em->flags;
3954 * bump off for our next call to get_extent
3956 off = extent_map_end(em);
3960 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
3962 flags |= FIEMAP_EXTENT_LAST;
3963 } else if (em->block_start == EXTENT_MAP_INLINE) {
3964 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3965 FIEMAP_EXTENT_NOT_ALIGNED);
3966 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
3967 flags |= (FIEMAP_EXTENT_DELALLOC |
3968 FIEMAP_EXTENT_UNKNOWN);
3970 disko = em->block_start + offset_in_extent;
3972 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3973 flags |= FIEMAP_EXTENT_ENCODED;
3975 free_extent_map(em);
3977 if ((em_start >= last) || em_len == (u64)-1 ||
3978 (last == (u64)-1 && isize <= em_end)) {
3979 flags |= FIEMAP_EXTENT_LAST;
3983 /* now scan forward to see if this is really the last extent. */
3984 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3991 flags |= FIEMAP_EXTENT_LAST;
3994 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4000 free_extent_map(em);
4002 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
4003 &cached_state, GFP_NOFS);
4007 static void __free_extent_buffer(struct extent_buffer *eb)
4010 unsigned long flags;
4011 spin_lock_irqsave(&leak_lock, flags);
4012 list_del(&eb->leak_list);
4013 spin_unlock_irqrestore(&leak_lock, flags);
4015 kmem_cache_free(extent_buffer_cache, eb);
4018 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4023 struct extent_buffer *eb = NULL;
4025 unsigned long flags;
4028 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
4035 rwlock_init(&eb->lock);
4036 atomic_set(&eb->write_locks, 0);
4037 atomic_set(&eb->read_locks, 0);
4038 atomic_set(&eb->blocking_readers, 0);
4039 atomic_set(&eb->blocking_writers, 0);
4040 atomic_set(&eb->spinning_readers, 0);
4041 atomic_set(&eb->spinning_writers, 0);
4042 eb->lock_nested = 0;
4043 init_waitqueue_head(&eb->write_lock_wq);
4044 init_waitqueue_head(&eb->read_lock_wq);
4047 spin_lock_irqsave(&leak_lock, flags);
4048 list_add(&eb->leak_list, &buffers);
4049 spin_unlock_irqrestore(&leak_lock, flags);
4051 spin_lock_init(&eb->refs_lock);
4052 atomic_set(&eb->refs, 1);
4053 atomic_set(&eb->io_pages, 0);
4056 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4058 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4059 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4060 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4065 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4069 struct extent_buffer *new;
4070 unsigned long num_pages = num_extent_pages(src->start, src->len);
4072 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4076 for (i = 0; i < num_pages; i++) {
4077 p = alloc_page(GFP_ATOMIC);
4079 attach_extent_buffer_page(new, p);
4080 WARN_ON(PageDirty(p));
4085 copy_extent_buffer(new, src, 0, 0, src->len);
4086 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4087 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4092 struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4094 struct extent_buffer *eb;
4095 unsigned long num_pages = num_extent_pages(0, len);
4098 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4102 for (i = 0; i < num_pages; i++) {
4103 eb->pages[i] = alloc_page(GFP_ATOMIC);
4107 set_extent_buffer_uptodate(eb);
4108 btrfs_set_header_nritems(eb, 0);
4109 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4114 __free_page(eb->pages[i - 1]);
4115 __free_extent_buffer(eb);
4119 static int extent_buffer_under_io(struct extent_buffer *eb)
4121 return (atomic_read(&eb->io_pages) ||
4122 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4123 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4127 * Helper for releasing extent buffer page.
4129 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4130 unsigned long start_idx)
4132 unsigned long index;
4133 unsigned long num_pages;
4135 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4137 BUG_ON(extent_buffer_under_io(eb));
4139 num_pages = num_extent_pages(eb->start, eb->len);
4140 index = start_idx + num_pages;
4141 if (start_idx >= index)
4146 page = extent_buffer_page(eb, index);
4147 if (page && mapped) {
4148 spin_lock(&page->mapping->private_lock);
4150 * We do this since we'll remove the pages after we've
4151 * removed the eb from the radix tree, so we could race
4152 * and have this page now attached to the new eb. So
4153 * only clear page_private if it's still connected to
4156 if (PagePrivate(page) &&
4157 page->private == (unsigned long)eb) {
4158 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4159 BUG_ON(PageDirty(page));
4160 BUG_ON(PageWriteback(page));
4162 * We need to make sure we haven't be attached
4165 ClearPagePrivate(page);
4166 set_page_private(page, 0);
4167 /* One for the page private */
4168 page_cache_release(page);
4170 spin_unlock(&page->mapping->private_lock);
4174 /* One for when we alloced the page */
4175 page_cache_release(page);
4177 } while (index != start_idx);
4181 * Helper for releasing the extent buffer.
4183 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4185 btrfs_release_extent_buffer_page(eb, 0);
4186 __free_extent_buffer(eb);
4189 static void check_buffer_tree_ref(struct extent_buffer *eb)
4192 /* the ref bit is tricky. We have to make sure it is set
4193 * if we have the buffer dirty. Otherwise the
4194 * code to free a buffer can end up dropping a dirty
4197 * Once the ref bit is set, it won't go away while the
4198 * buffer is dirty or in writeback, and it also won't
4199 * go away while we have the reference count on the
4202 * We can't just set the ref bit without bumping the
4203 * ref on the eb because free_extent_buffer might
4204 * see the ref bit and try to clear it. If this happens
4205 * free_extent_buffer might end up dropping our original
4206 * ref by mistake and freeing the page before we are able
4207 * to add one more ref.
4209 * So bump the ref count first, then set the bit. If someone
4210 * beat us to it, drop the ref we added.
4212 refs = atomic_read(&eb->refs);
4213 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4216 spin_lock(&eb->refs_lock);
4217 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4218 atomic_inc(&eb->refs);
4219 spin_unlock(&eb->refs_lock);
4222 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4224 unsigned long num_pages, i;
4226 check_buffer_tree_ref(eb);
4228 num_pages = num_extent_pages(eb->start, eb->len);
4229 for (i = 0; i < num_pages; i++) {
4230 struct page *p = extent_buffer_page(eb, i);
4231 mark_page_accessed(p);
4235 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
4236 u64 start, unsigned long len)
4238 unsigned long num_pages = num_extent_pages(start, len);
4240 unsigned long index = start >> PAGE_CACHE_SHIFT;
4241 struct extent_buffer *eb;
4242 struct extent_buffer *exists = NULL;
4244 struct address_space *mapping = tree->mapping;
4249 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4250 if (eb && atomic_inc_not_zero(&eb->refs)) {
4252 mark_extent_buffer_accessed(eb);
4257 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
4261 for (i = 0; i < num_pages; i++, index++) {
4262 p = find_or_create_page(mapping, index, GFP_NOFS);
4266 spin_lock(&mapping->private_lock);
4267 if (PagePrivate(p)) {
4269 * We could have already allocated an eb for this page
4270 * and attached one so lets see if we can get a ref on
4271 * the existing eb, and if we can we know it's good and
4272 * we can just return that one, else we know we can just
4273 * overwrite page->private.
4275 exists = (struct extent_buffer *)p->private;
4276 if (atomic_inc_not_zero(&exists->refs)) {
4277 spin_unlock(&mapping->private_lock);
4279 page_cache_release(p);
4280 mark_extent_buffer_accessed(exists);
4285 * Do this so attach doesn't complain and we need to
4286 * drop the ref the old guy had.
4288 ClearPagePrivate(p);
4289 WARN_ON(PageDirty(p));
4290 page_cache_release(p);
4292 attach_extent_buffer_page(eb, p);
4293 spin_unlock(&mapping->private_lock);
4294 WARN_ON(PageDirty(p));
4295 mark_page_accessed(p);
4297 if (!PageUptodate(p))
4301 * see below about how we avoid a nasty race with release page
4302 * and why we unlock later
4306 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4308 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4312 spin_lock(&tree->buffer_lock);
4313 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4314 if (ret == -EEXIST) {
4315 exists = radix_tree_lookup(&tree->buffer,
4316 start >> PAGE_CACHE_SHIFT);
4317 if (!atomic_inc_not_zero(&exists->refs)) {
4318 spin_unlock(&tree->buffer_lock);
4319 radix_tree_preload_end();
4323 spin_unlock(&tree->buffer_lock);
4324 radix_tree_preload_end();
4325 mark_extent_buffer_accessed(exists);
4328 /* add one reference for the tree */
4329 check_buffer_tree_ref(eb);
4330 spin_unlock(&tree->buffer_lock);
4331 radix_tree_preload_end();
4334 * there is a race where release page may have
4335 * tried to find this extent buffer in the radix
4336 * but failed. It will tell the VM it is safe to
4337 * reclaim the, and it will clear the page private bit.
4338 * We must make sure to set the page private bit properly
4339 * after the extent buffer is in the radix tree so
4340 * it doesn't get lost
4342 SetPageChecked(eb->pages[0]);
4343 for (i = 1; i < num_pages; i++) {
4344 p = extent_buffer_page(eb, i);
4345 ClearPageChecked(p);
4348 unlock_page(eb->pages[0]);
4352 for (i = 0; i < num_pages; i++) {
4354 unlock_page(eb->pages[i]);
4357 WARN_ON(!atomic_dec_and_test(&eb->refs));
4358 btrfs_release_extent_buffer(eb);
4362 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
4363 u64 start, unsigned long len)
4365 struct extent_buffer *eb;
4368 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4369 if (eb && atomic_inc_not_zero(&eb->refs)) {
4371 mark_extent_buffer_accessed(eb);
4379 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4381 struct extent_buffer *eb =
4382 container_of(head, struct extent_buffer, rcu_head);
4384 __free_extent_buffer(eb);
4387 /* Expects to have eb->eb_lock already held */
4388 static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4390 WARN_ON(atomic_read(&eb->refs) == 0);
4391 if (atomic_dec_and_test(&eb->refs)) {
4392 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4393 spin_unlock(&eb->refs_lock);
4395 struct extent_io_tree *tree = eb->tree;
4397 spin_unlock(&eb->refs_lock);
4399 spin_lock(&tree->buffer_lock);
4400 radix_tree_delete(&tree->buffer,
4401 eb->start >> PAGE_CACHE_SHIFT);
4402 spin_unlock(&tree->buffer_lock);
4405 /* Should be safe to release our pages at this point */
4406 btrfs_release_extent_buffer_page(eb, 0);
4407 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4410 spin_unlock(&eb->refs_lock);
4415 void free_extent_buffer(struct extent_buffer *eb)
4423 refs = atomic_read(&eb->refs);
4426 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4431 spin_lock(&eb->refs_lock);
4432 if (atomic_read(&eb->refs) == 2 &&
4433 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4434 atomic_dec(&eb->refs);
4436 if (atomic_read(&eb->refs) == 2 &&
4437 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
4438 !extent_buffer_under_io(eb) &&
4439 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4440 atomic_dec(&eb->refs);
4443 * I know this is terrible, but it's temporary until we stop tracking
4444 * the uptodate bits and such for the extent buffers.
4446 release_extent_buffer(eb, GFP_ATOMIC);
4449 void free_extent_buffer_stale(struct extent_buffer *eb)
4454 spin_lock(&eb->refs_lock);
4455 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4457 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
4458 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4459 atomic_dec(&eb->refs);
4460 release_extent_buffer(eb, GFP_NOFS);
4463 void clear_extent_buffer_dirty(struct extent_buffer *eb)
4466 unsigned long num_pages;
4469 num_pages = num_extent_pages(eb->start, eb->len);
4471 for (i = 0; i < num_pages; i++) {
4472 page = extent_buffer_page(eb, i);
4473 if (!PageDirty(page))
4477 WARN_ON(!PagePrivate(page));
4479 clear_page_dirty_for_io(page);
4480 spin_lock_irq(&page->mapping->tree_lock);
4481 if (!PageDirty(page)) {
4482 radix_tree_tag_clear(&page->mapping->page_tree,
4484 PAGECACHE_TAG_DIRTY);
4486 spin_unlock_irq(&page->mapping->tree_lock);
4487 ClearPageError(page);
4490 WARN_ON(atomic_read(&eb->refs) == 0);
4493 int set_extent_buffer_dirty(struct extent_buffer *eb)
4496 unsigned long num_pages;
4499 check_buffer_tree_ref(eb);
4501 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4503 num_pages = num_extent_pages(eb->start, eb->len);
4504 WARN_ON(atomic_read(&eb->refs) == 0);
4505 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4507 for (i = 0; i < num_pages; i++)
4508 set_page_dirty(extent_buffer_page(eb, i));
4512 static int range_straddles_pages(u64 start, u64 len)
4514 if (len < PAGE_CACHE_SIZE)
4516 if (start & (PAGE_CACHE_SIZE - 1))
4518 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4523 int clear_extent_buffer_uptodate(struct extent_buffer *eb)
4527 unsigned long num_pages;
4529 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4530 num_pages = num_extent_pages(eb->start, eb->len);
4531 for (i = 0; i < num_pages; i++) {
4532 page = extent_buffer_page(eb, i);
4534 ClearPageUptodate(page);
4539 int set_extent_buffer_uptodate(struct extent_buffer *eb)
4543 unsigned long num_pages;
4545 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4546 num_pages = num_extent_pages(eb->start, eb->len);
4547 for (i = 0; i < num_pages; i++) {
4548 page = extent_buffer_page(eb, i);
4549 SetPageUptodate(page);
4554 int extent_range_uptodate(struct extent_io_tree *tree,
4559 int pg_uptodate = 1;
4561 unsigned long index;
4563 if (range_straddles_pages(start, end - start + 1)) {
4564 ret = test_range_bit(tree, start, end,
4565 EXTENT_UPTODATE, 1, NULL);
4569 while (start <= end) {
4570 index = start >> PAGE_CACHE_SHIFT;
4571 page = find_get_page(tree->mapping, index);
4574 uptodate = PageUptodate(page);
4575 page_cache_release(page);
4580 start += PAGE_CACHE_SIZE;
4585 int extent_buffer_uptodate(struct extent_buffer *eb)
4587 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4590 int read_extent_buffer_pages(struct extent_io_tree *tree,
4591 struct extent_buffer *eb, u64 start, int wait,
4592 get_extent_t *get_extent, int mirror_num)
4595 unsigned long start_i;
4599 int locked_pages = 0;
4600 int all_uptodate = 1;
4601 unsigned long num_pages;
4602 unsigned long num_reads = 0;
4603 struct bio *bio = NULL;
4604 unsigned long bio_flags = 0;
4606 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4610 WARN_ON(start < eb->start);
4611 start_i = (start >> PAGE_CACHE_SHIFT) -
4612 (eb->start >> PAGE_CACHE_SHIFT);
4617 num_pages = num_extent_pages(eb->start, eb->len);
4618 for (i = start_i; i < num_pages; i++) {
4619 page = extent_buffer_page(eb, i);
4620 if (wait == WAIT_NONE) {
4621 if (!trylock_page(page))
4627 if (!PageUptodate(page)) {
4634 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4638 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
4639 eb->read_mirror = 0;
4640 atomic_set(&eb->io_pages, num_reads);
4641 for (i = start_i; i < num_pages; i++) {
4642 page = extent_buffer_page(eb, i);
4643 if (!PageUptodate(page)) {
4644 ClearPageError(page);
4645 err = __extent_read_full_page(tree, page,
4647 mirror_num, &bio_flags);
4656 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
4661 if (ret || wait != WAIT_COMPLETE)
4664 for (i = start_i; i < num_pages; i++) {
4665 page = extent_buffer_page(eb, i);
4666 wait_on_page_locked(page);
4667 if (!PageUptodate(page))
4675 while (locked_pages > 0) {
4676 page = extent_buffer_page(eb, i);
4684 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4685 unsigned long start,
4692 char *dst = (char *)dstv;
4693 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4694 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4696 WARN_ON(start > eb->len);
4697 WARN_ON(start + len > eb->start + eb->len);
4699 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4702 page = extent_buffer_page(eb, i);
4704 cur = min(len, (PAGE_CACHE_SIZE - offset));
4705 kaddr = page_address(page);
4706 memcpy(dst, kaddr + offset, cur);
4715 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4716 unsigned long min_len, char **map,
4717 unsigned long *map_start,
4718 unsigned long *map_len)
4720 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4723 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4724 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4725 unsigned long end_i = (start_offset + start + min_len - 1) >>
4732 offset = start_offset;
4736 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4739 if (start + min_len > eb->len) {
4740 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4741 "wanted %lu %lu\n", (unsigned long long)eb->start,
4742 eb->len, start, min_len);
4746 p = extent_buffer_page(eb, i);
4747 kaddr = page_address(p);
4748 *map = kaddr + offset;
4749 *map_len = PAGE_CACHE_SIZE - offset;
4753 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4754 unsigned long start,
4761 char *ptr = (char *)ptrv;
4762 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4763 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4766 WARN_ON(start > eb->len);
4767 WARN_ON(start + len > eb->start + eb->len);
4769 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4772 page = extent_buffer_page(eb, i);
4774 cur = min(len, (PAGE_CACHE_SIZE - offset));
4776 kaddr = page_address(page);
4777 ret = memcmp(ptr, kaddr + offset, cur);
4789 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4790 unsigned long start, unsigned long len)
4796 char *src = (char *)srcv;
4797 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4798 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4800 WARN_ON(start > eb->len);
4801 WARN_ON(start + len > eb->start + eb->len);
4803 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4806 page = extent_buffer_page(eb, i);
4807 WARN_ON(!PageUptodate(page));
4809 cur = min(len, PAGE_CACHE_SIZE - offset);
4810 kaddr = page_address(page);
4811 memcpy(kaddr + offset, src, cur);
4820 void memset_extent_buffer(struct extent_buffer *eb, char c,
4821 unsigned long start, unsigned long len)
4827 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4828 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4830 WARN_ON(start > eb->len);
4831 WARN_ON(start + len > eb->start + eb->len);
4833 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4836 page = extent_buffer_page(eb, i);
4837 WARN_ON(!PageUptodate(page));
4839 cur = min(len, PAGE_CACHE_SIZE - offset);
4840 kaddr = page_address(page);
4841 memset(kaddr + offset, c, cur);
4849 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4850 unsigned long dst_offset, unsigned long src_offset,
4853 u64 dst_len = dst->len;
4858 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4859 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4861 WARN_ON(src->len != dst_len);
4863 offset = (start_offset + dst_offset) &
4864 ((unsigned long)PAGE_CACHE_SIZE - 1);
4867 page = extent_buffer_page(dst, i);
4868 WARN_ON(!PageUptodate(page));
4870 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4872 kaddr = page_address(page);
4873 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4882 static void move_pages(struct page *dst_page, struct page *src_page,
4883 unsigned long dst_off, unsigned long src_off,
4886 char *dst_kaddr = page_address(dst_page);
4887 if (dst_page == src_page) {
4888 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4890 char *src_kaddr = page_address(src_page);
4891 char *p = dst_kaddr + dst_off + len;
4892 char *s = src_kaddr + src_off + len;
4899 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4901 unsigned long distance = (src > dst) ? src - dst : dst - src;
4902 return distance < len;
4905 static void copy_pages(struct page *dst_page, struct page *src_page,
4906 unsigned long dst_off, unsigned long src_off,
4909 char *dst_kaddr = page_address(dst_page);
4911 int must_memmove = 0;
4913 if (dst_page != src_page) {
4914 src_kaddr = page_address(src_page);
4916 src_kaddr = dst_kaddr;
4917 if (areas_overlap(src_off, dst_off, len))
4922 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4924 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
4927 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4928 unsigned long src_offset, unsigned long len)
4931 size_t dst_off_in_page;
4932 size_t src_off_in_page;
4933 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4934 unsigned long dst_i;
4935 unsigned long src_i;
4937 if (src_offset + len > dst->len) {
4938 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4939 "len %lu dst len %lu\n", src_offset, len, dst->len);
4942 if (dst_offset + len > dst->len) {
4943 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4944 "len %lu dst len %lu\n", dst_offset, len, dst->len);
4949 dst_off_in_page = (start_offset + dst_offset) &
4950 ((unsigned long)PAGE_CACHE_SIZE - 1);
4951 src_off_in_page = (start_offset + src_offset) &
4952 ((unsigned long)PAGE_CACHE_SIZE - 1);
4954 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4955 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4957 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4959 cur = min_t(unsigned long, cur,
4960 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4962 copy_pages(extent_buffer_page(dst, dst_i),
4963 extent_buffer_page(dst, src_i),
4964 dst_off_in_page, src_off_in_page, cur);
4972 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4973 unsigned long src_offset, unsigned long len)
4976 size_t dst_off_in_page;
4977 size_t src_off_in_page;
4978 unsigned long dst_end = dst_offset + len - 1;
4979 unsigned long src_end = src_offset + len - 1;
4980 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4981 unsigned long dst_i;
4982 unsigned long src_i;
4984 if (src_offset + len > dst->len) {
4985 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4986 "len %lu len %lu\n", src_offset, len, dst->len);
4989 if (dst_offset + len > dst->len) {
4990 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4991 "len %lu len %lu\n", dst_offset, len, dst->len);
4994 if (dst_offset < src_offset) {
4995 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4999 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5000 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5002 dst_off_in_page = (start_offset + dst_end) &
5003 ((unsigned long)PAGE_CACHE_SIZE - 1);
5004 src_off_in_page = (start_offset + src_end) &
5005 ((unsigned long)PAGE_CACHE_SIZE - 1);
5007 cur = min_t(unsigned long, len, src_off_in_page + 1);
5008 cur = min(cur, dst_off_in_page + 1);
5009 move_pages(extent_buffer_page(dst, dst_i),
5010 extent_buffer_page(dst, src_i),
5011 dst_off_in_page - cur + 1,
5012 src_off_in_page - cur + 1, cur);
5020 int try_release_extent_buffer(struct page *page, gfp_t mask)
5022 struct extent_buffer *eb;
5025 * We need to make sure noboody is attaching this page to an eb right
5028 spin_lock(&page->mapping->private_lock);
5029 if (!PagePrivate(page)) {
5030 spin_unlock(&page->mapping->private_lock);
5034 eb = (struct extent_buffer *)page->private;
5038 * This is a little awful but should be ok, we need to make sure that
5039 * the eb doesn't disappear out from under us while we're looking at
5042 spin_lock(&eb->refs_lock);
5043 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
5044 spin_unlock(&eb->refs_lock);
5045 spin_unlock(&page->mapping->private_lock);
5048 spin_unlock(&page->mapping->private_lock);
5050 if ((mask & GFP_NOFS) == GFP_NOFS)
5054 * If tree ref isn't set then we know the ref on this eb is a real ref,
5055 * so just return, this page will likely be freed soon anyway.
5057 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5058 spin_unlock(&eb->refs_lock);
5062 return release_extent_buffer(eb, mask);