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 #ifdef CONFIG_BTRFS_DEBUG
28 static LIST_HEAD(buffers);
29 static LIST_HEAD(states);
31 static DEFINE_SPINLOCK(leak_lock);
34 void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
38 spin_lock_irqsave(&leak_lock, flags);
40 spin_unlock_irqrestore(&leak_lock, flags);
44 void btrfs_leak_debug_del(struct list_head *entry)
48 spin_lock_irqsave(&leak_lock, flags);
50 spin_unlock_irqrestore(&leak_lock, flags);
54 void btrfs_leak_debug_check(void)
56 struct extent_state *state;
57 struct extent_buffer *eb;
59 while (!list_empty(&states)) {
60 state = list_entry(states.next, struct extent_state, leak_list);
61 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
62 "state %lu in tree %p refs %d\n",
63 (unsigned long long)state->start,
64 (unsigned long long)state->end,
65 state->state, state->tree, atomic_read(&state->refs));
66 list_del(&state->leak_list);
67 kmem_cache_free(extent_state_cache, state);
70 while (!list_empty(&buffers)) {
71 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
72 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
73 "refs %d\n", (unsigned long long)eb->start,
74 eb->len, atomic_read(&eb->refs));
75 list_del(&eb->leak_list);
76 kmem_cache_free(extent_buffer_cache, eb);
80 #define btrfs_leak_debug_add(new, head) do {} while (0)
81 #define btrfs_leak_debug_del(entry) do {} while (0)
82 #define btrfs_leak_debug_check() do {} while (0)
85 #define BUFFER_LRU_MAX 64
90 struct rb_node rb_node;
93 struct extent_page_data {
95 struct extent_io_tree *tree;
96 get_extent_t *get_extent;
97 unsigned long bio_flags;
99 /* tells writepage not to lock the state bits for this range
100 * it still does the unlocking
102 unsigned int extent_locked:1;
104 /* tells the submit_bio code to use a WRITE_SYNC */
105 unsigned int sync_io:1;
108 static noinline void flush_write_bio(void *data);
109 static inline struct btrfs_fs_info *
110 tree_fs_info(struct extent_io_tree *tree)
112 return btrfs_sb(tree->mapping->host->i_sb);
115 int __init extent_io_init(void)
117 extent_state_cache = kmem_cache_create("btrfs_extent_state",
118 sizeof(struct extent_state), 0,
119 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
120 if (!extent_state_cache)
123 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
124 sizeof(struct extent_buffer), 0,
125 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
126 if (!extent_buffer_cache)
127 goto free_state_cache;
131 kmem_cache_destroy(extent_state_cache);
135 void extent_io_exit(void)
137 btrfs_leak_debug_check();
140 * Make sure all delayed rcu free are flushed before we
144 if (extent_state_cache)
145 kmem_cache_destroy(extent_state_cache);
146 if (extent_buffer_cache)
147 kmem_cache_destroy(extent_buffer_cache);
150 void extent_io_tree_init(struct extent_io_tree *tree,
151 struct address_space *mapping)
153 tree->state = RB_ROOT;
154 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
156 tree->dirty_bytes = 0;
157 spin_lock_init(&tree->lock);
158 spin_lock_init(&tree->buffer_lock);
159 tree->mapping = mapping;
162 static struct extent_state *alloc_extent_state(gfp_t mask)
164 struct extent_state *state;
166 state = kmem_cache_alloc(extent_state_cache, mask);
172 btrfs_leak_debug_add(&state->leak_list, &states);
173 atomic_set(&state->refs, 1);
174 init_waitqueue_head(&state->wq);
175 trace_alloc_extent_state(state, mask, _RET_IP_);
179 void free_extent_state(struct extent_state *state)
183 if (atomic_dec_and_test(&state->refs)) {
184 WARN_ON(state->tree);
185 btrfs_leak_debug_del(&state->leak_list);
186 trace_free_extent_state(state, _RET_IP_);
187 kmem_cache_free(extent_state_cache, state);
191 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
192 struct rb_node *node)
194 struct rb_node **p = &root->rb_node;
195 struct rb_node *parent = NULL;
196 struct tree_entry *entry;
200 entry = rb_entry(parent, struct tree_entry, rb_node);
202 if (offset < entry->start)
204 else if (offset > entry->end)
210 rb_link_node(node, parent, p);
211 rb_insert_color(node, root);
215 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
216 struct rb_node **prev_ret,
217 struct rb_node **next_ret)
219 struct rb_root *root = &tree->state;
220 struct rb_node *n = root->rb_node;
221 struct rb_node *prev = NULL;
222 struct rb_node *orig_prev = NULL;
223 struct tree_entry *entry;
224 struct tree_entry *prev_entry = NULL;
227 entry = rb_entry(n, struct tree_entry, rb_node);
231 if (offset < entry->start)
233 else if (offset > entry->end)
241 while (prev && offset > prev_entry->end) {
242 prev = rb_next(prev);
243 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
250 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
251 while (prev && offset < prev_entry->start) {
252 prev = rb_prev(prev);
253 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
260 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
263 struct rb_node *prev = NULL;
266 ret = __etree_search(tree, offset, &prev, NULL);
272 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
273 struct extent_state *other)
275 if (tree->ops && tree->ops->merge_extent_hook)
276 tree->ops->merge_extent_hook(tree->mapping->host, new,
281 * utility function to look for merge candidates inside a given range.
282 * Any extents with matching state are merged together into a single
283 * extent in the tree. Extents with EXTENT_IO in their state field
284 * are not merged because the end_io handlers need to be able to do
285 * operations on them without sleeping (or doing allocations/splits).
287 * This should be called with the tree lock held.
289 static void merge_state(struct extent_io_tree *tree,
290 struct extent_state *state)
292 struct extent_state *other;
293 struct rb_node *other_node;
295 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
298 other_node = rb_prev(&state->rb_node);
300 other = rb_entry(other_node, struct extent_state, rb_node);
301 if (other->end == state->start - 1 &&
302 other->state == state->state) {
303 merge_cb(tree, state, other);
304 state->start = other->start;
306 rb_erase(&other->rb_node, &tree->state);
307 free_extent_state(other);
310 other_node = rb_next(&state->rb_node);
312 other = rb_entry(other_node, struct extent_state, rb_node);
313 if (other->start == state->end + 1 &&
314 other->state == state->state) {
315 merge_cb(tree, state, other);
316 state->end = other->end;
318 rb_erase(&other->rb_node, &tree->state);
319 free_extent_state(other);
324 static void set_state_cb(struct extent_io_tree *tree,
325 struct extent_state *state, int *bits)
327 if (tree->ops && tree->ops->set_bit_hook)
328 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
331 static void clear_state_cb(struct extent_io_tree *tree,
332 struct extent_state *state, int *bits)
334 if (tree->ops && tree->ops->clear_bit_hook)
335 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
338 static void set_state_bits(struct extent_io_tree *tree,
339 struct extent_state *state, int *bits);
342 * insert an extent_state struct into the tree. 'bits' are set on the
343 * struct before it is inserted.
345 * This may return -EEXIST if the extent is already there, in which case the
346 * state struct is freed.
348 * The tree lock is not taken internally. This is a utility function and
349 * probably isn't what you want to call (see set/clear_extent_bit).
351 static int insert_state(struct extent_io_tree *tree,
352 struct extent_state *state, u64 start, u64 end,
355 struct rb_node *node;
358 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
359 (unsigned long long)end,
360 (unsigned long long)start);
361 state->start = start;
364 set_state_bits(tree, state, bits);
366 node = tree_insert(&tree->state, end, &state->rb_node);
368 struct extent_state *found;
369 found = rb_entry(node, struct extent_state, rb_node);
370 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
371 "%llu %llu\n", (unsigned long long)found->start,
372 (unsigned long long)found->end,
373 (unsigned long long)start, (unsigned long long)end);
377 merge_state(tree, state);
381 static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
384 if (tree->ops && tree->ops->split_extent_hook)
385 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
389 * split a given extent state struct in two, inserting the preallocated
390 * struct 'prealloc' as the newly created second half. 'split' indicates an
391 * offset inside 'orig' where it should be split.
394 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
395 * are two extent state structs in the tree:
396 * prealloc: [orig->start, split - 1]
397 * orig: [ split, orig->end ]
399 * The tree locks are not taken by this function. They need to be held
402 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
403 struct extent_state *prealloc, u64 split)
405 struct rb_node *node;
407 split_cb(tree, orig, split);
409 prealloc->start = orig->start;
410 prealloc->end = split - 1;
411 prealloc->state = orig->state;
414 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
416 free_extent_state(prealloc);
419 prealloc->tree = tree;
423 static struct extent_state *next_state(struct extent_state *state)
425 struct rb_node *next = rb_next(&state->rb_node);
427 return rb_entry(next, struct extent_state, rb_node);
433 * utility function to clear some bits in an extent state struct.
434 * it will optionally wake up any one waiting on this state (wake == 1).
436 * If no bits are set on the state struct after clearing things, the
437 * struct is freed and removed from the tree
439 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
440 struct extent_state *state,
443 struct extent_state *next;
444 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
446 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
447 u64 range = state->end - state->start + 1;
448 WARN_ON(range > tree->dirty_bytes);
449 tree->dirty_bytes -= range;
451 clear_state_cb(tree, state, bits);
452 state->state &= ~bits_to_clear;
455 if (state->state == 0) {
456 next = next_state(state);
458 rb_erase(&state->rb_node, &tree->state);
460 free_extent_state(state);
465 merge_state(tree, state);
466 next = next_state(state);
471 static struct extent_state *
472 alloc_extent_state_atomic(struct extent_state *prealloc)
475 prealloc = alloc_extent_state(GFP_ATOMIC);
480 static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
482 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
483 "Extent tree was modified by another "
484 "thread while locked.");
488 * clear some bits on a range in the tree. This may require splitting
489 * or inserting elements in the tree, so the gfp mask is used to
490 * indicate which allocations or sleeping are allowed.
492 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
493 * the given range from the tree regardless of state (ie for truncate).
495 * the range [start, end] is inclusive.
497 * This takes the tree lock, and returns 0 on success and < 0 on error.
499 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
500 int bits, int wake, int delete,
501 struct extent_state **cached_state,
504 struct extent_state *state;
505 struct extent_state *cached;
506 struct extent_state *prealloc = NULL;
507 struct rb_node *node;
513 bits |= ~EXTENT_CTLBITS;
514 bits |= EXTENT_FIRST_DELALLOC;
516 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
519 if (!prealloc && (mask & __GFP_WAIT)) {
520 prealloc = alloc_extent_state(mask);
525 spin_lock(&tree->lock);
527 cached = *cached_state;
530 *cached_state = NULL;
534 if (cached && cached->tree && cached->start <= start &&
535 cached->end > start) {
537 atomic_dec(&cached->refs);
542 free_extent_state(cached);
545 * this search will find the extents that end after
548 node = tree_search(tree, start);
551 state = rb_entry(node, struct extent_state, rb_node);
553 if (state->start > end)
555 WARN_ON(state->end < start);
556 last_end = state->end;
558 /* the state doesn't have the wanted bits, go ahead */
559 if (!(state->state & bits)) {
560 state = next_state(state);
565 * | ---- desired range ---- |
567 * | ------------- state -------------- |
569 * We need to split the extent we found, and may flip
570 * bits on second half.
572 * If the extent we found extends past our range, we
573 * just split and search again. It'll get split again
574 * the next time though.
576 * If the extent we found is inside our range, we clear
577 * the desired bit on it.
580 if (state->start < start) {
581 prealloc = alloc_extent_state_atomic(prealloc);
583 err = split_state(tree, state, prealloc, start);
585 extent_io_tree_panic(tree, err);
590 if (state->end <= end) {
591 state = clear_state_bit(tree, state, &bits, wake);
597 * | ---- desired range ---- |
599 * We need to split the extent, and clear the bit
602 if (state->start <= end && state->end > end) {
603 prealloc = alloc_extent_state_atomic(prealloc);
605 err = split_state(tree, state, prealloc, end + 1);
607 extent_io_tree_panic(tree, err);
612 clear_state_bit(tree, prealloc, &bits, wake);
618 state = clear_state_bit(tree, state, &bits, wake);
620 if (last_end == (u64)-1)
622 start = last_end + 1;
623 if (start <= end && state && !need_resched())
628 spin_unlock(&tree->lock);
630 free_extent_state(prealloc);
637 spin_unlock(&tree->lock);
638 if (mask & __GFP_WAIT)
643 static void wait_on_state(struct extent_io_tree *tree,
644 struct extent_state *state)
645 __releases(tree->lock)
646 __acquires(tree->lock)
649 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
650 spin_unlock(&tree->lock);
652 spin_lock(&tree->lock);
653 finish_wait(&state->wq, &wait);
657 * waits for one or more bits to clear on a range in the state tree.
658 * The range [start, end] is inclusive.
659 * The tree lock is taken by this function
661 static void wait_extent_bit(struct extent_io_tree *tree, u64 start,
664 struct extent_state *state;
665 struct rb_node *node;
667 spin_lock(&tree->lock);
671 * this search will find all the extents that end after
674 node = tree_search(tree, start);
678 state = rb_entry(node, struct extent_state, rb_node);
680 if (state->start > end)
683 if (state->state & bits) {
684 start = state->start;
685 atomic_inc(&state->refs);
686 wait_on_state(tree, state);
687 free_extent_state(state);
690 start = state->end + 1;
695 cond_resched_lock(&tree->lock);
698 spin_unlock(&tree->lock);
701 static void set_state_bits(struct extent_io_tree *tree,
702 struct extent_state *state,
705 int bits_to_set = *bits & ~EXTENT_CTLBITS;
707 set_state_cb(tree, state, bits);
708 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
709 u64 range = state->end - state->start + 1;
710 tree->dirty_bytes += range;
712 state->state |= bits_to_set;
715 static void cache_state(struct extent_state *state,
716 struct extent_state **cached_ptr)
718 if (cached_ptr && !(*cached_ptr)) {
719 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
721 atomic_inc(&state->refs);
726 static void uncache_state(struct extent_state **cached_ptr)
728 if (cached_ptr && (*cached_ptr)) {
729 struct extent_state *state = *cached_ptr;
731 free_extent_state(state);
736 * set some bits on a range in the tree. This may require allocations or
737 * sleeping, so the gfp mask is used to indicate what is allowed.
739 * If any of the exclusive bits are set, this will fail with -EEXIST if some
740 * part of the range already has the desired bits set. The start of the
741 * existing range is returned in failed_start in this case.
743 * [start, end] is inclusive This takes the tree lock.
746 static int __must_check
747 __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
748 int bits, int exclusive_bits, u64 *failed_start,
749 struct extent_state **cached_state, gfp_t mask)
751 struct extent_state *state;
752 struct extent_state *prealloc = NULL;
753 struct rb_node *node;
758 bits |= EXTENT_FIRST_DELALLOC;
760 if (!prealloc && (mask & __GFP_WAIT)) {
761 prealloc = alloc_extent_state(mask);
765 spin_lock(&tree->lock);
766 if (cached_state && *cached_state) {
767 state = *cached_state;
768 if (state->start <= start && state->end > start &&
770 node = &state->rb_node;
775 * this search will find all the extents that end after
778 node = tree_search(tree, start);
780 prealloc = alloc_extent_state_atomic(prealloc);
782 err = insert_state(tree, prealloc, start, end, &bits);
784 extent_io_tree_panic(tree, err);
789 state = rb_entry(node, struct extent_state, rb_node);
791 last_start = state->start;
792 last_end = state->end;
795 * | ---- desired range ---- |
798 * Just lock what we found and keep going
800 if (state->start == start && state->end <= end) {
801 if (state->state & exclusive_bits) {
802 *failed_start = state->start;
807 set_state_bits(tree, state, &bits);
808 cache_state(state, cached_state);
809 merge_state(tree, state);
810 if (last_end == (u64)-1)
812 start = last_end + 1;
813 state = next_state(state);
814 if (start < end && state && state->start == start &&
821 * | ---- desired range ---- |
824 * | ------------- state -------------- |
826 * We need to split the extent we found, and may flip bits on
829 * If the extent we found extends past our
830 * range, we just split and search again. It'll get split
831 * again the next time though.
833 * If the extent we found is inside our range, we set the
836 if (state->start < start) {
837 if (state->state & exclusive_bits) {
838 *failed_start = start;
843 prealloc = alloc_extent_state_atomic(prealloc);
845 err = split_state(tree, state, prealloc, start);
847 extent_io_tree_panic(tree, err);
852 if (state->end <= end) {
853 set_state_bits(tree, state, &bits);
854 cache_state(state, cached_state);
855 merge_state(tree, state);
856 if (last_end == (u64)-1)
858 start = last_end + 1;
859 state = next_state(state);
860 if (start < end && state && state->start == start &&
867 * | ---- desired range ---- |
868 * | state | or | state |
870 * There's a hole, we need to insert something in it and
871 * ignore the extent we found.
873 if (state->start > start) {
875 if (end < last_start)
878 this_end = last_start - 1;
880 prealloc = alloc_extent_state_atomic(prealloc);
884 * Avoid to free 'prealloc' if it can be merged with
887 err = insert_state(tree, prealloc, start, this_end,
890 extent_io_tree_panic(tree, err);
892 cache_state(prealloc, cached_state);
894 start = this_end + 1;
898 * | ---- desired range ---- |
900 * We need to split the extent, and set the bit
903 if (state->start <= end && state->end > end) {
904 if (state->state & exclusive_bits) {
905 *failed_start = start;
910 prealloc = alloc_extent_state_atomic(prealloc);
912 err = split_state(tree, state, prealloc, end + 1);
914 extent_io_tree_panic(tree, err);
916 set_state_bits(tree, prealloc, &bits);
917 cache_state(prealloc, cached_state);
918 merge_state(tree, prealloc);
926 spin_unlock(&tree->lock);
928 free_extent_state(prealloc);
935 spin_unlock(&tree->lock);
936 if (mask & __GFP_WAIT)
941 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
942 u64 *failed_start, struct extent_state **cached_state,
945 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
951 * convert_extent_bit - convert all bits in a given range from one bit to
953 * @tree: the io tree to search
954 * @start: the start offset in bytes
955 * @end: the end offset in bytes (inclusive)
956 * @bits: the bits to set in this range
957 * @clear_bits: the bits to clear in this range
958 * @cached_state: state that we're going to cache
959 * @mask: the allocation mask
961 * This will go through and set bits for the given range. If any states exist
962 * already in this range they are set with the given bit and cleared of the
963 * clear_bits. This is only meant to be used by things that are mergeable, ie
964 * converting from say DELALLOC to DIRTY. This is not meant to be used with
965 * boundary bits like LOCK.
967 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
968 int bits, int clear_bits,
969 struct extent_state **cached_state, gfp_t mask)
971 struct extent_state *state;
972 struct extent_state *prealloc = NULL;
973 struct rb_node *node;
979 if (!prealloc && (mask & __GFP_WAIT)) {
980 prealloc = alloc_extent_state(mask);
985 spin_lock(&tree->lock);
986 if (cached_state && *cached_state) {
987 state = *cached_state;
988 if (state->start <= start && state->end > start &&
990 node = &state->rb_node;
996 * this search will find all the extents that end after
999 node = tree_search(tree, start);
1001 prealloc = alloc_extent_state_atomic(prealloc);
1006 err = insert_state(tree, prealloc, start, end, &bits);
1009 extent_io_tree_panic(tree, err);
1012 state = rb_entry(node, struct extent_state, rb_node);
1014 last_start = state->start;
1015 last_end = state->end;
1018 * | ---- desired range ---- |
1021 * Just lock what we found and keep going
1023 if (state->start == start && state->end <= end) {
1024 set_state_bits(tree, state, &bits);
1025 cache_state(state, cached_state);
1026 state = clear_state_bit(tree, state, &clear_bits, 0);
1027 if (last_end == (u64)-1)
1029 start = last_end + 1;
1030 if (start < end && state && state->start == start &&
1037 * | ---- desired range ---- |
1040 * | ------------- state -------------- |
1042 * We need to split the extent we found, and may flip bits on
1045 * If the extent we found extends past our
1046 * range, we just split and search again. It'll get split
1047 * again the next time though.
1049 * If the extent we found is inside our range, we set the
1050 * desired bit on it.
1052 if (state->start < start) {
1053 prealloc = alloc_extent_state_atomic(prealloc);
1058 err = split_state(tree, state, prealloc, start);
1060 extent_io_tree_panic(tree, err);
1064 if (state->end <= end) {
1065 set_state_bits(tree, state, &bits);
1066 cache_state(state, cached_state);
1067 state = clear_state_bit(tree, state, &clear_bits, 0);
1068 if (last_end == (u64)-1)
1070 start = last_end + 1;
1071 if (start < end && state && state->start == start &&
1078 * | ---- desired range ---- |
1079 * | state | or | state |
1081 * There's a hole, we need to insert something in it and
1082 * ignore the extent we found.
1084 if (state->start > start) {
1086 if (end < last_start)
1089 this_end = last_start - 1;
1091 prealloc = alloc_extent_state_atomic(prealloc);
1098 * Avoid to free 'prealloc' if it can be merged with
1101 err = insert_state(tree, prealloc, start, this_end,
1104 extent_io_tree_panic(tree, err);
1105 cache_state(prealloc, cached_state);
1107 start = this_end + 1;
1111 * | ---- desired range ---- |
1113 * We need to split the extent, and set the bit
1116 if (state->start <= end && state->end > end) {
1117 prealloc = alloc_extent_state_atomic(prealloc);
1123 err = split_state(tree, state, prealloc, end + 1);
1125 extent_io_tree_panic(tree, err);
1127 set_state_bits(tree, prealloc, &bits);
1128 cache_state(prealloc, cached_state);
1129 clear_state_bit(tree, prealloc, &clear_bits, 0);
1137 spin_unlock(&tree->lock);
1139 free_extent_state(prealloc);
1146 spin_unlock(&tree->lock);
1147 if (mask & __GFP_WAIT)
1152 /* wrappers around set/clear extent bit */
1153 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1156 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
1160 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1161 int bits, gfp_t mask)
1163 return set_extent_bit(tree, start, end, bits, NULL,
1167 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1168 int bits, gfp_t mask)
1170 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
1173 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
1174 struct extent_state **cached_state, gfp_t mask)
1176 return set_extent_bit(tree, start, end,
1177 EXTENT_DELALLOC | EXTENT_UPTODATE,
1178 NULL, cached_state, mask);
1181 int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1182 struct extent_state **cached_state, gfp_t mask)
1184 return set_extent_bit(tree, start, end,
1185 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1186 NULL, cached_state, mask);
1189 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1192 return clear_extent_bit(tree, start, end,
1193 EXTENT_DIRTY | EXTENT_DELALLOC |
1194 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
1197 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1200 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
1204 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1205 struct extent_state **cached_state, gfp_t mask)
1207 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
1208 cached_state, mask);
1211 int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1212 struct extent_state **cached_state, gfp_t mask)
1214 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1215 cached_state, mask);
1219 * either insert or lock state struct between start and end use mask to tell
1220 * us if waiting is desired.
1222 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1223 int bits, struct extent_state **cached_state)
1228 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1229 EXTENT_LOCKED, &failed_start,
1230 cached_state, GFP_NOFS);
1231 if (err == -EEXIST) {
1232 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1233 start = failed_start;
1236 WARN_ON(start > end);
1241 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1243 return lock_extent_bits(tree, start, end, 0, NULL);
1246 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1251 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1252 &failed_start, NULL, GFP_NOFS);
1253 if (err == -EEXIST) {
1254 if (failed_start > start)
1255 clear_extent_bit(tree, start, failed_start - 1,
1256 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
1262 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1263 struct extent_state **cached, gfp_t mask)
1265 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1269 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1271 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1275 int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1277 unsigned long index = start >> PAGE_CACHE_SHIFT;
1278 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1281 while (index <= end_index) {
1282 page = find_get_page(inode->i_mapping, index);
1283 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1284 clear_page_dirty_for_io(page);
1285 page_cache_release(page);
1291 int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1293 unsigned long index = start >> PAGE_CACHE_SHIFT;
1294 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1297 while (index <= end_index) {
1298 page = find_get_page(inode->i_mapping, index);
1299 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1300 account_page_redirty(page);
1301 __set_page_dirty_nobuffers(page);
1302 page_cache_release(page);
1309 * helper function to set both pages and extents in the tree writeback
1311 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1313 unsigned long index = start >> PAGE_CACHE_SHIFT;
1314 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1317 while (index <= end_index) {
1318 page = find_get_page(tree->mapping, index);
1319 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1320 set_page_writeback(page);
1321 page_cache_release(page);
1327 /* find the first state struct with 'bits' set after 'start', and
1328 * return it. tree->lock must be held. NULL will returned if
1329 * nothing was found after 'start'
1331 static struct extent_state *
1332 find_first_extent_bit_state(struct extent_io_tree *tree,
1333 u64 start, int bits)
1335 struct rb_node *node;
1336 struct extent_state *state;
1339 * this search will find all the extents that end after
1342 node = tree_search(tree, start);
1347 state = rb_entry(node, struct extent_state, rb_node);
1348 if (state->end >= start && (state->state & bits))
1351 node = rb_next(node);
1360 * find the first offset in the io tree with 'bits' set. zero is
1361 * returned if we find something, and *start_ret and *end_ret are
1362 * set to reflect the state struct that was found.
1364 * If nothing was found, 1 is returned. If found something, return 0.
1366 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1367 u64 *start_ret, u64 *end_ret, int bits,
1368 struct extent_state **cached_state)
1370 struct extent_state *state;
1374 spin_lock(&tree->lock);
1375 if (cached_state && *cached_state) {
1376 state = *cached_state;
1377 if (state->end == start - 1 && state->tree) {
1378 n = rb_next(&state->rb_node);
1380 state = rb_entry(n, struct extent_state,
1382 if (state->state & bits)
1386 free_extent_state(*cached_state);
1387 *cached_state = NULL;
1390 free_extent_state(*cached_state);
1391 *cached_state = NULL;
1394 state = find_first_extent_bit_state(tree, start, bits);
1397 cache_state(state, cached_state);
1398 *start_ret = state->start;
1399 *end_ret = state->end;
1403 spin_unlock(&tree->lock);
1408 * find a contiguous range of bytes in the file marked as delalloc, not
1409 * more than 'max_bytes'. start and end are used to return the range,
1411 * 1 is returned if we find something, 0 if nothing was in the tree
1413 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1414 u64 *start, u64 *end, u64 max_bytes,
1415 struct extent_state **cached_state)
1417 struct rb_node *node;
1418 struct extent_state *state;
1419 u64 cur_start = *start;
1421 u64 total_bytes = 0;
1423 spin_lock(&tree->lock);
1426 * this search will find all the extents that end after
1429 node = tree_search(tree, cur_start);
1437 state = rb_entry(node, struct extent_state, rb_node);
1438 if (found && (state->start != cur_start ||
1439 (state->state & EXTENT_BOUNDARY))) {
1442 if (!(state->state & EXTENT_DELALLOC)) {
1448 *start = state->start;
1449 *cached_state = state;
1450 atomic_inc(&state->refs);
1454 cur_start = state->end + 1;
1455 node = rb_next(node);
1458 total_bytes += state->end - state->start + 1;
1459 if (total_bytes >= max_bytes)
1463 spin_unlock(&tree->lock);
1467 static noinline void __unlock_for_delalloc(struct inode *inode,
1468 struct page *locked_page,
1472 struct page *pages[16];
1473 unsigned long index = start >> PAGE_CACHE_SHIFT;
1474 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1475 unsigned long nr_pages = end_index - index + 1;
1478 if (index == locked_page->index && end_index == index)
1481 while (nr_pages > 0) {
1482 ret = find_get_pages_contig(inode->i_mapping, index,
1483 min_t(unsigned long, nr_pages,
1484 ARRAY_SIZE(pages)), pages);
1485 for (i = 0; i < ret; i++) {
1486 if (pages[i] != locked_page)
1487 unlock_page(pages[i]);
1488 page_cache_release(pages[i]);
1496 static noinline int lock_delalloc_pages(struct inode *inode,
1497 struct page *locked_page,
1501 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1502 unsigned long start_index = index;
1503 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1504 unsigned long pages_locked = 0;
1505 struct page *pages[16];
1506 unsigned long nrpages;
1510 /* the caller is responsible for locking the start index */
1511 if (index == locked_page->index && index == end_index)
1514 /* skip the page at the start index */
1515 nrpages = end_index - index + 1;
1516 while (nrpages > 0) {
1517 ret = find_get_pages_contig(inode->i_mapping, index,
1518 min_t(unsigned long,
1519 nrpages, ARRAY_SIZE(pages)), pages);
1524 /* now we have an array of pages, lock them all */
1525 for (i = 0; i < ret; i++) {
1527 * the caller is taking responsibility for
1530 if (pages[i] != locked_page) {
1531 lock_page(pages[i]);
1532 if (!PageDirty(pages[i]) ||
1533 pages[i]->mapping != inode->i_mapping) {
1535 unlock_page(pages[i]);
1536 page_cache_release(pages[i]);
1540 page_cache_release(pages[i]);
1549 if (ret && pages_locked) {
1550 __unlock_for_delalloc(inode, locked_page,
1552 ((u64)(start_index + pages_locked - 1)) <<
1559 * find a contiguous range of bytes in the file marked as delalloc, not
1560 * more than 'max_bytes'. start and end are used to return the range,
1562 * 1 is returned if we find something, 0 if nothing was in the tree
1564 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1565 struct extent_io_tree *tree,
1566 struct page *locked_page,
1567 u64 *start, u64 *end,
1573 struct extent_state *cached_state = NULL;
1578 /* step one, find a bunch of delalloc bytes starting at start */
1579 delalloc_start = *start;
1581 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1582 max_bytes, &cached_state);
1583 if (!found || delalloc_end <= *start) {
1584 *start = delalloc_start;
1585 *end = delalloc_end;
1586 free_extent_state(cached_state);
1591 * start comes from the offset of locked_page. We have to lock
1592 * pages in order, so we can't process delalloc bytes before
1595 if (delalloc_start < *start)
1596 delalloc_start = *start;
1599 * make sure to limit the number of pages we try to lock down
1602 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1603 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1605 /* step two, lock all the pages after the page that has start */
1606 ret = lock_delalloc_pages(inode, locked_page,
1607 delalloc_start, delalloc_end);
1608 if (ret == -EAGAIN) {
1609 /* some of the pages are gone, lets avoid looping by
1610 * shortening the size of the delalloc range we're searching
1612 free_extent_state(cached_state);
1614 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1615 max_bytes = PAGE_CACHE_SIZE - offset;
1623 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
1625 /* step three, lock the state bits for the whole range */
1626 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
1628 /* then test to make sure it is all still delalloc */
1629 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1630 EXTENT_DELALLOC, 1, cached_state);
1632 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1633 &cached_state, GFP_NOFS);
1634 __unlock_for_delalloc(inode, locked_page,
1635 delalloc_start, delalloc_end);
1639 free_extent_state(cached_state);
1640 *start = delalloc_start;
1641 *end = delalloc_end;
1646 int extent_clear_unlock_delalloc(struct inode *inode,
1647 struct extent_io_tree *tree,
1648 u64 start, u64 end, struct page *locked_page,
1652 struct page *pages[16];
1653 unsigned long index = start >> PAGE_CACHE_SHIFT;
1654 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1655 unsigned long nr_pages = end_index - index + 1;
1659 if (op & EXTENT_CLEAR_UNLOCK)
1660 clear_bits |= EXTENT_LOCKED;
1661 if (op & EXTENT_CLEAR_DIRTY)
1662 clear_bits |= EXTENT_DIRTY;
1664 if (op & EXTENT_CLEAR_DELALLOC)
1665 clear_bits |= EXTENT_DELALLOC;
1667 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1668 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1669 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1670 EXTENT_SET_PRIVATE2)))
1673 while (nr_pages > 0) {
1674 ret = find_get_pages_contig(inode->i_mapping, index,
1675 min_t(unsigned long,
1676 nr_pages, ARRAY_SIZE(pages)), pages);
1677 for (i = 0; i < ret; i++) {
1679 if (op & EXTENT_SET_PRIVATE2)
1680 SetPagePrivate2(pages[i]);
1682 if (pages[i] == locked_page) {
1683 page_cache_release(pages[i]);
1686 if (op & EXTENT_CLEAR_DIRTY)
1687 clear_page_dirty_for_io(pages[i]);
1688 if (op & EXTENT_SET_WRITEBACK)
1689 set_page_writeback(pages[i]);
1690 if (op & EXTENT_END_WRITEBACK)
1691 end_page_writeback(pages[i]);
1692 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1693 unlock_page(pages[i]);
1694 page_cache_release(pages[i]);
1704 * count the number of bytes in the tree that have a given bit(s)
1705 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1706 * cached. The total number found is returned.
1708 u64 count_range_bits(struct extent_io_tree *tree,
1709 u64 *start, u64 search_end, u64 max_bytes,
1710 unsigned long bits, int contig)
1712 struct rb_node *node;
1713 struct extent_state *state;
1714 u64 cur_start = *start;
1715 u64 total_bytes = 0;
1719 if (search_end <= cur_start) {
1724 spin_lock(&tree->lock);
1725 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1726 total_bytes = tree->dirty_bytes;
1730 * this search will find all the extents that end after
1733 node = tree_search(tree, cur_start);
1738 state = rb_entry(node, struct extent_state, rb_node);
1739 if (state->start > search_end)
1741 if (contig && found && state->start > last + 1)
1743 if (state->end >= cur_start && (state->state & bits) == bits) {
1744 total_bytes += min(search_end, state->end) + 1 -
1745 max(cur_start, state->start);
1746 if (total_bytes >= max_bytes)
1749 *start = max(cur_start, state->start);
1753 } else if (contig && found) {
1756 node = rb_next(node);
1761 spin_unlock(&tree->lock);
1766 * set the private field for a given byte offset in the tree. If there isn't
1767 * an extent_state there already, this does nothing.
1769 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1771 struct rb_node *node;
1772 struct extent_state *state;
1775 spin_lock(&tree->lock);
1777 * this search will find all the extents that end after
1780 node = tree_search(tree, start);
1785 state = rb_entry(node, struct extent_state, rb_node);
1786 if (state->start != start) {
1790 state->private = private;
1792 spin_unlock(&tree->lock);
1796 void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 csums[],
1799 struct rb_node *node;
1800 struct extent_state *state;
1802 spin_lock(&tree->lock);
1804 * this search will find all the extents that end after
1807 node = tree_search(tree, start);
1810 state = rb_entry(node, struct extent_state, rb_node);
1811 BUG_ON(state->start != start);
1814 state->private = *csums++;
1816 state = next_state(state);
1818 spin_unlock(&tree->lock);
1821 static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index)
1823 struct bio_vec *bvec = bio->bi_io_vec + bio_index;
1825 return page_offset(bvec->bv_page) + bvec->bv_offset;
1828 void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int bio_index,
1829 u32 csums[], int count)
1831 struct rb_node *node;
1832 struct extent_state *state = NULL;
1835 spin_lock(&tree->lock);
1837 start = __btrfs_get_bio_offset(bio, bio_index);
1838 if (state == NULL || state->start != start) {
1839 node = tree_search(tree, start);
1842 state = rb_entry(node, struct extent_state, rb_node);
1843 BUG_ON(state->start != start);
1845 state->private = *csums++;
1849 state = next_state(state);
1851 spin_unlock(&tree->lock);
1854 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1856 struct rb_node *node;
1857 struct extent_state *state;
1860 spin_lock(&tree->lock);
1862 * this search will find all the extents that end after
1865 node = tree_search(tree, start);
1870 state = rb_entry(node, struct extent_state, rb_node);
1871 if (state->start != start) {
1875 *private = state->private;
1877 spin_unlock(&tree->lock);
1882 * searches a range in the state tree for a given mask.
1883 * If 'filled' == 1, this returns 1 only if every extent in the tree
1884 * has the bits set. Otherwise, 1 is returned if any bit in the
1885 * range is found set.
1887 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1888 int bits, int filled, struct extent_state *cached)
1890 struct extent_state *state = NULL;
1891 struct rb_node *node;
1894 spin_lock(&tree->lock);
1895 if (cached && cached->tree && cached->start <= start &&
1896 cached->end > start)
1897 node = &cached->rb_node;
1899 node = tree_search(tree, start);
1900 while (node && start <= end) {
1901 state = rb_entry(node, struct extent_state, rb_node);
1903 if (filled && state->start > start) {
1908 if (state->start > end)
1911 if (state->state & bits) {
1915 } else if (filled) {
1920 if (state->end == (u64)-1)
1923 start = state->end + 1;
1926 node = rb_next(node);
1933 spin_unlock(&tree->lock);
1938 * helper function to set a given page up to date if all the
1939 * extents in the tree for that page are up to date
1941 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
1943 u64 start = page_offset(page);
1944 u64 end = start + PAGE_CACHE_SIZE - 1;
1945 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1946 SetPageUptodate(page);
1950 * helper function to unlock a page if all the extents in the tree
1951 * for that page are unlocked
1953 static void check_page_locked(struct extent_io_tree *tree, struct page *page)
1955 u64 start = page_offset(page);
1956 u64 end = start + PAGE_CACHE_SIZE - 1;
1957 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1962 * helper function to end page writeback if all the extents
1963 * in the tree for that page are done with writeback
1965 static void check_page_writeback(struct extent_io_tree *tree,
1968 end_page_writeback(page);
1972 * When IO fails, either with EIO or csum verification fails, we
1973 * try other mirrors that might have a good copy of the data. This
1974 * io_failure_record is used to record state as we go through all the
1975 * mirrors. If another mirror has good data, the page is set up to date
1976 * and things continue. If a good mirror can't be found, the original
1977 * bio end_io callback is called to indicate things have failed.
1979 struct io_failure_record {
1984 unsigned long bio_flags;
1990 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1995 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1997 set_state_private(failure_tree, rec->start, 0);
1998 ret = clear_extent_bits(failure_tree, rec->start,
1999 rec->start + rec->len - 1,
2000 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2004 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
2005 rec->start + rec->len - 1,
2006 EXTENT_DAMAGED, GFP_NOFS);
2014 static void repair_io_failure_callback(struct bio *bio, int err)
2016 complete(bio->bi_private);
2020 * this bypasses the standard btrfs submit functions deliberately, as
2021 * the standard behavior is to write all copies in a raid setup. here we only
2022 * want to write the one bad copy. so we do the mapping for ourselves and issue
2023 * submit_bio directly.
2024 * to avoid any synchronization issues, wait for the data after writing, which
2025 * actually prevents the read that triggered the error from finishing.
2026 * currently, there can be no more than two copies of every data bit. thus,
2027 * exactly one rewrite is required.
2029 int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
2030 u64 length, u64 logical, struct page *page,
2034 struct btrfs_device *dev;
2035 DECLARE_COMPLETION_ONSTACK(compl);
2038 struct btrfs_bio *bbio = NULL;
2039 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
2042 BUG_ON(!mirror_num);
2044 /* we can't repair anything in raid56 yet */
2045 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2048 bio = bio_alloc(GFP_NOFS, 1);
2051 bio->bi_private = &compl;
2052 bio->bi_end_io = repair_io_failure_callback;
2054 map_length = length;
2056 ret = btrfs_map_block(fs_info, WRITE, logical,
2057 &map_length, &bbio, mirror_num);
2062 BUG_ON(mirror_num != bbio->mirror_num);
2063 sector = bbio->stripes[mirror_num-1].physical >> 9;
2064 bio->bi_sector = sector;
2065 dev = bbio->stripes[mirror_num-1].dev;
2067 if (!dev || !dev->bdev || !dev->writeable) {
2071 bio->bi_bdev = dev->bdev;
2072 bio_add_page(bio, page, length, start - page_offset(page));
2073 btrfsic_submit_bio(WRITE_SYNC, bio);
2074 wait_for_completion(&compl);
2076 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2077 /* try to remap that extent elsewhere? */
2079 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
2083 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
2084 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2085 start, rcu_str_deref(dev->name), sector);
2091 int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2094 u64 start = eb->start;
2095 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
2098 for (i = 0; i < num_pages; i++) {
2099 struct page *p = extent_buffer_page(eb, i);
2100 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
2101 start, p, mirror_num);
2104 start += PAGE_CACHE_SIZE;
2111 * each time an IO finishes, we do a fast check in the IO failure tree
2112 * to see if we need to process or clean up an io_failure_record
2114 static int clean_io_failure(u64 start, struct page *page)
2117 u64 private_failure;
2118 struct io_failure_record *failrec;
2119 struct btrfs_fs_info *fs_info;
2120 struct extent_state *state;
2124 struct inode *inode = page->mapping->host;
2127 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2128 (u64)-1, 1, EXTENT_DIRTY, 0);
2132 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2137 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2138 BUG_ON(!failrec->this_mirror);
2140 if (failrec->in_validation) {
2141 /* there was no real error, just free the record */
2142 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2148 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2149 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2152 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2154 if (state && state->start == failrec->start) {
2155 fs_info = BTRFS_I(inode)->root->fs_info;
2156 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2158 if (num_copies > 1) {
2159 ret = repair_io_failure(fs_info, start, failrec->len,
2160 failrec->logical, page,
2161 failrec->failed_mirror);
2169 ret = free_io_failure(inode, failrec, did_repair);
2175 * this is a generic handler for readpage errors (default
2176 * readpage_io_failed_hook). if other copies exist, read those and write back
2177 * good data to the failed position. does not investigate in remapping the
2178 * failed extent elsewhere, hoping the device will be smart enough to do this as
2182 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2183 u64 start, u64 end, int failed_mirror,
2184 struct extent_state *state)
2186 struct io_failure_record *failrec = NULL;
2188 struct extent_map *em;
2189 struct inode *inode = page->mapping->host;
2190 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2191 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2192 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2199 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2201 ret = get_state_private(failure_tree, start, &private);
2203 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2206 failrec->start = start;
2207 failrec->len = end - start + 1;
2208 failrec->this_mirror = 0;
2209 failrec->bio_flags = 0;
2210 failrec->in_validation = 0;
2212 read_lock(&em_tree->lock);
2213 em = lookup_extent_mapping(em_tree, start, failrec->len);
2215 read_unlock(&em_tree->lock);
2220 if (em->start > start || em->start + em->len < start) {
2221 free_extent_map(em);
2224 read_unlock(&em_tree->lock);
2230 logical = start - em->start;
2231 logical = em->block_start + logical;
2232 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2233 logical = em->block_start;
2234 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2235 extent_set_compress_type(&failrec->bio_flags,
2238 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2239 "len=%llu\n", logical, start, failrec->len);
2240 failrec->logical = logical;
2241 free_extent_map(em);
2243 /* set the bits in the private failure tree */
2244 ret = set_extent_bits(failure_tree, start, end,
2245 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2247 ret = set_state_private(failure_tree, start,
2248 (u64)(unsigned long)failrec);
2249 /* set the bits in the inode's tree */
2251 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2258 failrec = (struct io_failure_record *)(unsigned long)private;
2259 pr_debug("bio_readpage_error: (found) logical=%llu, "
2260 "start=%llu, len=%llu, validation=%d\n",
2261 failrec->logical, failrec->start, failrec->len,
2262 failrec->in_validation);
2264 * when data can be on disk more than twice, add to failrec here
2265 * (e.g. with a list for failed_mirror) to make
2266 * clean_io_failure() clean all those errors at once.
2269 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2270 failrec->logical, failrec->len);
2271 if (num_copies == 1) {
2273 * we only have a single copy of the data, so don't bother with
2274 * all the retry and error correction code that follows. no
2275 * matter what the error is, it is very likely to persist.
2277 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2278 "state=%p, num_copies=%d, next_mirror %d, "
2279 "failed_mirror %d\n", state, num_copies,
2280 failrec->this_mirror, failed_mirror);
2281 free_io_failure(inode, failrec, 0);
2286 spin_lock(&tree->lock);
2287 state = find_first_extent_bit_state(tree, failrec->start,
2289 if (state && state->start != failrec->start)
2291 spin_unlock(&tree->lock);
2295 * there are two premises:
2296 * a) deliver good data to the caller
2297 * b) correct the bad sectors on disk
2299 if (failed_bio->bi_vcnt > 1) {
2301 * to fulfill b), we need to know the exact failing sectors, as
2302 * we don't want to rewrite any more than the failed ones. thus,
2303 * we need separate read requests for the failed bio
2305 * if the following BUG_ON triggers, our validation request got
2306 * merged. we need separate requests for our algorithm to work.
2308 BUG_ON(failrec->in_validation);
2309 failrec->in_validation = 1;
2310 failrec->this_mirror = failed_mirror;
2311 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2314 * we're ready to fulfill a) and b) alongside. get a good copy
2315 * of the failed sector and if we succeed, we have setup
2316 * everything for repair_io_failure to do the rest for us.
2318 if (failrec->in_validation) {
2319 BUG_ON(failrec->this_mirror != failed_mirror);
2320 failrec->in_validation = 0;
2321 failrec->this_mirror = 0;
2323 failrec->failed_mirror = failed_mirror;
2324 failrec->this_mirror++;
2325 if (failrec->this_mirror == failed_mirror)
2326 failrec->this_mirror++;
2327 read_mode = READ_SYNC;
2330 if (!state || failrec->this_mirror > num_copies) {
2331 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2332 "next_mirror %d, failed_mirror %d\n", state,
2333 num_copies, failrec->this_mirror, failed_mirror);
2334 free_io_failure(inode, failrec, 0);
2338 bio = bio_alloc(GFP_NOFS, 1);
2340 free_io_failure(inode, failrec, 0);
2343 bio->bi_private = state;
2344 bio->bi_end_io = failed_bio->bi_end_io;
2345 bio->bi_sector = failrec->logical >> 9;
2346 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2349 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2351 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2352 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2353 failrec->this_mirror, num_copies, failrec->in_validation);
2355 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2356 failrec->this_mirror,
2357 failrec->bio_flags, 0);
2361 /* lots and lots of room for performance fixes in the end_bio funcs */
2363 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2365 int uptodate = (err == 0);
2366 struct extent_io_tree *tree;
2369 tree = &BTRFS_I(page->mapping->host)->io_tree;
2371 if (tree->ops && tree->ops->writepage_end_io_hook) {
2372 ret = tree->ops->writepage_end_io_hook(page, start,
2373 end, NULL, uptodate);
2379 ClearPageUptodate(page);
2386 * after a writepage IO is done, we need to:
2387 * clear the uptodate bits on error
2388 * clear the writeback bits in the extent tree for this IO
2389 * end_page_writeback if the page has no more pending IO
2391 * Scheduling is not allowed, so the extent state tree is expected
2392 * to have one and only one object corresponding to this IO.
2394 static void end_bio_extent_writepage(struct bio *bio, int err)
2396 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2397 struct extent_io_tree *tree;
2403 struct page *page = bvec->bv_page;
2404 tree = &BTRFS_I(page->mapping->host)->io_tree;
2406 start = page_offset(page) + bvec->bv_offset;
2407 end = start + bvec->bv_len - 1;
2409 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2414 if (--bvec >= bio->bi_io_vec)
2415 prefetchw(&bvec->bv_page->flags);
2417 if (end_extent_writepage(page, err, start, end))
2421 end_page_writeback(page);
2423 check_page_writeback(tree, page);
2424 } while (bvec >= bio->bi_io_vec);
2430 * after a readpage IO is done, we need to:
2431 * clear the uptodate bits on error
2432 * set the uptodate bits if things worked
2433 * set the page up to date if all extents in the tree are uptodate
2434 * clear the lock bit in the extent tree
2435 * unlock the page if there are no other extents locked for it
2437 * Scheduling is not allowed, so the extent state tree is expected
2438 * to have one and only one object corresponding to this IO.
2440 static void end_bio_extent_readpage(struct bio *bio, int err)
2442 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2443 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2444 struct bio_vec *bvec = bio->bi_io_vec;
2445 struct extent_io_tree *tree;
2456 struct page *page = bvec->bv_page;
2457 struct extent_state *cached = NULL;
2458 struct extent_state *state;
2460 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2461 "mirror=%ld\n", (u64)bio->bi_sector, err,
2462 (long int)bio->bi_bdev);
2463 tree = &BTRFS_I(page->mapping->host)->io_tree;
2465 start = page_offset(page) + bvec->bv_offset;
2466 end = start + bvec->bv_len - 1;
2468 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2473 if (++bvec <= bvec_end)
2474 prefetchw(&bvec->bv_page->flags);
2476 spin_lock(&tree->lock);
2477 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2478 if (state && state->start == start) {
2480 * take a reference on the state, unlock will drop
2483 cache_state(state, &cached);
2485 spin_unlock(&tree->lock);
2487 mirror = (int)(unsigned long)bio->bi_bdev;
2488 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2489 ret = tree->ops->readpage_end_io_hook(page, start, end,
2494 clean_io_failure(start, page);
2497 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
2498 ret = tree->ops->readpage_io_failed_hook(page, mirror);
2500 test_bit(BIO_UPTODATE, &bio->bi_flags))
2502 } else if (!uptodate) {
2504 * The generic bio_readpage_error handles errors the
2505 * following way: If possible, new read requests are
2506 * created and submitted and will end up in
2507 * end_bio_extent_readpage as well (if we're lucky, not
2508 * in the !uptodate case). In that case it returns 0 and
2509 * we just go on with the next page in our bio. If it
2510 * can't handle the error it will return -EIO and we
2511 * remain responsible for that page.
2513 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
2516 test_bit(BIO_UPTODATE, &bio->bi_flags);
2519 uncache_state(&cached);
2524 if (uptodate && tree->track_uptodate) {
2525 set_extent_uptodate(tree, start, end, &cached,
2528 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2532 SetPageUptodate(page);
2534 ClearPageUptodate(page);
2540 check_page_uptodate(tree, page);
2542 ClearPageUptodate(page);
2545 check_page_locked(tree, page);
2547 } while (bvec <= bvec_end);
2553 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2558 bio = bio_alloc(gfp_flags, nr_vecs);
2560 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2561 while (!bio && (nr_vecs /= 2))
2562 bio = bio_alloc(gfp_flags, nr_vecs);
2567 bio->bi_bdev = bdev;
2568 bio->bi_sector = first_sector;
2573 static int __must_check submit_one_bio(int rw, struct bio *bio,
2574 int mirror_num, unsigned long bio_flags)
2577 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2578 struct page *page = bvec->bv_page;
2579 struct extent_io_tree *tree = bio->bi_private;
2582 start = page_offset(page) + bvec->bv_offset;
2584 bio->bi_private = NULL;
2588 if (tree->ops && tree->ops->submit_bio_hook)
2589 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2590 mirror_num, bio_flags, start);
2592 btrfsic_submit_bio(rw, bio);
2594 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2600 static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
2601 unsigned long offset, size_t size, struct bio *bio,
2602 unsigned long bio_flags)
2605 if (tree->ops && tree->ops->merge_bio_hook)
2606 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
2613 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2614 struct page *page, sector_t sector,
2615 size_t size, unsigned long offset,
2616 struct block_device *bdev,
2617 struct bio **bio_ret,
2618 unsigned long max_pages,
2619 bio_end_io_t end_io_func,
2621 unsigned long prev_bio_flags,
2622 unsigned long bio_flags)
2628 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2629 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2630 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2632 if (bio_ret && *bio_ret) {
2635 contig = bio->bi_sector == sector;
2637 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2640 if (prev_bio_flags != bio_flags || !contig ||
2641 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
2642 bio_add_page(bio, page, page_size, offset) < page_size) {
2643 ret = submit_one_bio(rw, bio, mirror_num,
2652 if (this_compressed)
2655 nr = bio_get_nr_vecs(bdev);
2657 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2661 bio_add_page(bio, page, page_size, offset);
2662 bio->bi_end_io = end_io_func;
2663 bio->bi_private = tree;
2668 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2673 static void attach_extent_buffer_page(struct extent_buffer *eb,
2676 if (!PagePrivate(page)) {
2677 SetPagePrivate(page);
2678 page_cache_get(page);
2679 set_page_private(page, (unsigned long)eb);
2681 WARN_ON(page->private != (unsigned long)eb);
2685 void set_page_extent_mapped(struct page *page)
2687 if (!PagePrivate(page)) {
2688 SetPagePrivate(page);
2689 page_cache_get(page);
2690 set_page_private(page, EXTENT_PAGE_PRIVATE);
2695 * basic readpage implementation. Locked extent state structs are inserted
2696 * into the tree that are removed when the IO is done (by the end_io
2698 * XXX JDM: This needs looking at to ensure proper page locking
2700 static int __extent_read_full_page(struct extent_io_tree *tree,
2702 get_extent_t *get_extent,
2703 struct bio **bio, int mirror_num,
2704 unsigned long *bio_flags, int rw)
2706 struct inode *inode = page->mapping->host;
2707 u64 start = page_offset(page);
2708 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2712 u64 last_byte = i_size_read(inode);
2716 struct extent_map *em;
2717 struct block_device *bdev;
2718 struct btrfs_ordered_extent *ordered;
2721 size_t pg_offset = 0;
2723 size_t disk_io_size;
2724 size_t blocksize = inode->i_sb->s_blocksize;
2725 unsigned long this_bio_flag = 0;
2727 set_page_extent_mapped(page);
2729 if (!PageUptodate(page)) {
2730 if (cleancache_get_page(page) == 0) {
2731 BUG_ON(blocksize != PAGE_SIZE);
2738 lock_extent(tree, start, end);
2739 ordered = btrfs_lookup_ordered_extent(inode, start);
2742 unlock_extent(tree, start, end);
2743 btrfs_start_ordered_extent(inode, ordered, 1);
2744 btrfs_put_ordered_extent(ordered);
2747 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2749 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2752 iosize = PAGE_CACHE_SIZE - zero_offset;
2753 userpage = kmap_atomic(page);
2754 memset(userpage + zero_offset, 0, iosize);
2755 flush_dcache_page(page);
2756 kunmap_atomic(userpage);
2759 while (cur <= end) {
2760 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2762 if (cur >= last_byte) {
2764 struct extent_state *cached = NULL;
2766 iosize = PAGE_CACHE_SIZE - pg_offset;
2767 userpage = kmap_atomic(page);
2768 memset(userpage + pg_offset, 0, iosize);
2769 flush_dcache_page(page);
2770 kunmap_atomic(userpage);
2771 set_extent_uptodate(tree, cur, cur + iosize - 1,
2773 unlock_extent_cached(tree, cur, cur + iosize - 1,
2777 em = get_extent(inode, page, pg_offset, cur,
2779 if (IS_ERR_OR_NULL(em)) {
2781 unlock_extent(tree, cur, end);
2784 extent_offset = cur - em->start;
2785 BUG_ON(extent_map_end(em) <= cur);
2788 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2789 this_bio_flag = EXTENT_BIO_COMPRESSED;
2790 extent_set_compress_type(&this_bio_flag,
2794 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2795 cur_end = min(extent_map_end(em) - 1, end);
2796 iosize = ALIGN(iosize, blocksize);
2797 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2798 disk_io_size = em->block_len;
2799 sector = em->block_start >> 9;
2801 sector = (em->block_start + extent_offset) >> 9;
2802 disk_io_size = iosize;
2805 block_start = em->block_start;
2806 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2807 block_start = EXTENT_MAP_HOLE;
2808 free_extent_map(em);
2811 /* we've found a hole, just zero and go on */
2812 if (block_start == EXTENT_MAP_HOLE) {
2814 struct extent_state *cached = NULL;
2816 userpage = kmap_atomic(page);
2817 memset(userpage + pg_offset, 0, iosize);
2818 flush_dcache_page(page);
2819 kunmap_atomic(userpage);
2821 set_extent_uptodate(tree, cur, cur + iosize - 1,
2823 unlock_extent_cached(tree, cur, cur + iosize - 1,
2826 pg_offset += iosize;
2829 /* the get_extent function already copied into the page */
2830 if (test_range_bit(tree, cur, cur_end,
2831 EXTENT_UPTODATE, 1, NULL)) {
2832 check_page_uptodate(tree, page);
2833 unlock_extent(tree, cur, cur + iosize - 1);
2835 pg_offset += iosize;
2838 /* we have an inline extent but it didn't get marked up
2839 * to date. Error out
2841 if (block_start == EXTENT_MAP_INLINE) {
2843 unlock_extent(tree, cur, cur + iosize - 1);
2845 pg_offset += iosize;
2850 ret = submit_extent_page(rw, tree, page,
2851 sector, disk_io_size, pg_offset,
2853 end_bio_extent_readpage, mirror_num,
2858 *bio_flags = this_bio_flag;
2861 unlock_extent(tree, cur, cur + iosize - 1);
2864 pg_offset += iosize;
2868 if (!PageError(page))
2869 SetPageUptodate(page);
2875 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2876 get_extent_t *get_extent, int mirror_num)
2878 struct bio *bio = NULL;
2879 unsigned long bio_flags = 0;
2882 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2885 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2889 static noinline void update_nr_written(struct page *page,
2890 struct writeback_control *wbc,
2891 unsigned long nr_written)
2893 wbc->nr_to_write -= nr_written;
2894 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2895 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2896 page->mapping->writeback_index = page->index + nr_written;
2900 * the writepage semantics are similar to regular writepage. extent
2901 * records are inserted to lock ranges in the tree, and as dirty areas
2902 * are found, they are marked writeback. Then the lock bits are removed
2903 * and the end_io handler clears the writeback ranges
2905 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2908 struct inode *inode = page->mapping->host;
2909 struct extent_page_data *epd = data;
2910 struct extent_io_tree *tree = epd->tree;
2911 u64 start = page_offset(page);
2913 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2917 u64 last_byte = i_size_read(inode);
2921 struct extent_state *cached_state = NULL;
2922 struct extent_map *em;
2923 struct block_device *bdev;
2926 size_t pg_offset = 0;
2928 loff_t i_size = i_size_read(inode);
2929 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2935 unsigned long nr_written = 0;
2936 bool fill_delalloc = true;
2938 if (wbc->sync_mode == WB_SYNC_ALL)
2939 write_flags = WRITE_SYNC;
2941 write_flags = WRITE;
2943 trace___extent_writepage(page, inode, wbc);
2945 WARN_ON(!PageLocked(page));
2947 ClearPageError(page);
2949 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2950 if (page->index > end_index ||
2951 (page->index == end_index && !pg_offset)) {
2952 page->mapping->a_ops->invalidatepage(page, 0);
2957 if (page->index == end_index) {
2960 userpage = kmap_atomic(page);
2961 memset(userpage + pg_offset, 0,
2962 PAGE_CACHE_SIZE - pg_offset);
2963 kunmap_atomic(userpage);
2964 flush_dcache_page(page);
2968 set_page_extent_mapped(page);
2970 if (!tree->ops || !tree->ops->fill_delalloc)
2971 fill_delalloc = false;
2973 delalloc_start = start;
2976 if (!epd->extent_locked && fill_delalloc) {
2977 u64 delalloc_to_write = 0;
2979 * make sure the wbc mapping index is at least updated
2982 update_nr_written(page, wbc, 0);
2984 while (delalloc_end < page_end) {
2985 nr_delalloc = find_lock_delalloc_range(inode, tree,
2990 if (nr_delalloc == 0) {
2991 delalloc_start = delalloc_end + 1;
2994 ret = tree->ops->fill_delalloc(inode, page,
2999 /* File system has been set read-only */
3005 * delalloc_end is already one less than the total
3006 * length, so we don't subtract one from
3009 delalloc_to_write += (delalloc_end - delalloc_start +
3012 delalloc_start = delalloc_end + 1;
3014 if (wbc->nr_to_write < delalloc_to_write) {
3017 if (delalloc_to_write < thresh * 2)
3018 thresh = delalloc_to_write;
3019 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3023 /* did the fill delalloc function already unlock and start
3029 * we've unlocked the page, so we can't update
3030 * the mapping's writeback index, just update
3033 wbc->nr_to_write -= nr_written;
3037 if (tree->ops && tree->ops->writepage_start_hook) {
3038 ret = tree->ops->writepage_start_hook(page, start,
3041 /* Fixup worker will requeue */
3043 wbc->pages_skipped++;
3045 redirty_page_for_writepage(wbc, page);
3046 update_nr_written(page, wbc, nr_written);
3054 * we don't want to touch the inode after unlocking the page,
3055 * so we update the mapping writeback index now
3057 update_nr_written(page, wbc, nr_written + 1);
3060 if (last_byte <= start) {
3061 if (tree->ops && tree->ops->writepage_end_io_hook)
3062 tree->ops->writepage_end_io_hook(page, start,
3067 blocksize = inode->i_sb->s_blocksize;
3069 while (cur <= end) {
3070 if (cur >= last_byte) {
3071 if (tree->ops && tree->ops->writepage_end_io_hook)
3072 tree->ops->writepage_end_io_hook(page, cur,
3076 em = epd->get_extent(inode, page, pg_offset, cur,
3078 if (IS_ERR_OR_NULL(em)) {
3083 extent_offset = cur - em->start;
3084 BUG_ON(extent_map_end(em) <= cur);
3086 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3087 iosize = ALIGN(iosize, blocksize);
3088 sector = (em->block_start + extent_offset) >> 9;
3090 block_start = em->block_start;
3091 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3092 free_extent_map(em);
3096 * compressed and inline extents are written through other
3099 if (compressed || block_start == EXTENT_MAP_HOLE ||
3100 block_start == EXTENT_MAP_INLINE) {
3102 * end_io notification does not happen here for
3103 * compressed extents
3105 if (!compressed && tree->ops &&
3106 tree->ops->writepage_end_io_hook)
3107 tree->ops->writepage_end_io_hook(page, cur,
3110 else if (compressed) {
3111 /* we don't want to end_page_writeback on
3112 * a compressed extent. this happens
3119 pg_offset += iosize;
3122 /* leave this out until we have a page_mkwrite call */
3123 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
3124 EXTENT_DIRTY, 0, NULL)) {
3126 pg_offset += iosize;
3130 if (tree->ops && tree->ops->writepage_io_hook) {
3131 ret = tree->ops->writepage_io_hook(page, cur,
3139 unsigned long max_nr = end_index + 1;
3141 set_range_writeback(tree, cur, cur + iosize - 1);
3142 if (!PageWriteback(page)) {
3143 printk(KERN_ERR "btrfs warning page %lu not "
3144 "writeback, cur %llu end %llu\n",
3145 page->index, (unsigned long long)cur,
3146 (unsigned long long)end);
3149 ret = submit_extent_page(write_flags, tree, page,
3150 sector, iosize, pg_offset,
3151 bdev, &epd->bio, max_nr,
3152 end_bio_extent_writepage,
3158 pg_offset += iosize;
3163 /* make sure the mapping tag for page dirty gets cleared */
3164 set_page_writeback(page);
3165 end_page_writeback(page);
3171 /* drop our reference on any cached states */
3172 free_extent_state(cached_state);
3176 static int eb_wait(void *word)
3182 void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3184 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3185 TASK_UNINTERRUPTIBLE);
3188 static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3189 struct btrfs_fs_info *fs_info,
3190 struct extent_page_data *epd)
3192 unsigned long i, num_pages;
3196 if (!btrfs_try_tree_write_lock(eb)) {
3198 flush_write_bio(epd);
3199 btrfs_tree_lock(eb);
3202 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3203 btrfs_tree_unlock(eb);
3207 flush_write_bio(epd);
3211 wait_on_extent_buffer_writeback(eb);
3212 btrfs_tree_lock(eb);
3213 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3215 btrfs_tree_unlock(eb);
3220 * We need to do this to prevent races in people who check if the eb is
3221 * under IO since we can end up having no IO bits set for a short period
3224 spin_lock(&eb->refs_lock);
3225 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3226 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3227 spin_unlock(&eb->refs_lock);
3228 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3229 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3231 fs_info->dirty_metadata_batch);
3234 spin_unlock(&eb->refs_lock);
3237 btrfs_tree_unlock(eb);
3242 num_pages = num_extent_pages(eb->start, eb->len);
3243 for (i = 0; i < num_pages; i++) {
3244 struct page *p = extent_buffer_page(eb, i);
3246 if (!trylock_page(p)) {
3248 flush_write_bio(epd);
3258 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3260 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3261 smp_mb__after_clear_bit();
3262 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3265 static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3267 int uptodate = err == 0;
3268 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3269 struct extent_buffer *eb;
3273 struct page *page = bvec->bv_page;
3276 eb = (struct extent_buffer *)page->private;
3278 done = atomic_dec_and_test(&eb->io_pages);
3280 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3281 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3282 ClearPageUptodate(page);
3286 end_page_writeback(page);
3291 end_extent_buffer_writeback(eb);
3292 } while (bvec >= bio->bi_io_vec);
3298 static int write_one_eb(struct extent_buffer *eb,
3299 struct btrfs_fs_info *fs_info,
3300 struct writeback_control *wbc,
3301 struct extent_page_data *epd)
3303 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3304 u64 offset = eb->start;
3305 unsigned long i, num_pages;
3306 unsigned long bio_flags = 0;
3307 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
3310 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3311 num_pages = num_extent_pages(eb->start, eb->len);
3312 atomic_set(&eb->io_pages, num_pages);
3313 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3314 bio_flags = EXTENT_BIO_TREE_LOG;
3316 for (i = 0; i < num_pages; i++) {
3317 struct page *p = extent_buffer_page(eb, i);
3319 clear_page_dirty_for_io(p);
3320 set_page_writeback(p);
3321 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3322 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3323 -1, end_bio_extent_buffer_writepage,
3324 0, epd->bio_flags, bio_flags);
3325 epd->bio_flags = bio_flags;
3327 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3329 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3330 end_extent_buffer_writeback(eb);
3334 offset += PAGE_CACHE_SIZE;
3335 update_nr_written(p, wbc, 1);
3339 if (unlikely(ret)) {
3340 for (; i < num_pages; i++) {
3341 struct page *p = extent_buffer_page(eb, i);
3349 int btree_write_cache_pages(struct address_space *mapping,
3350 struct writeback_control *wbc)
3352 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3353 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3354 struct extent_buffer *eb, *prev_eb = NULL;
3355 struct extent_page_data epd = {
3359 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3364 int nr_to_write_done = 0;
3365 struct pagevec pvec;
3368 pgoff_t end; /* Inclusive */
3372 pagevec_init(&pvec, 0);
3373 if (wbc->range_cyclic) {
3374 index = mapping->writeback_index; /* Start from prev offset */
3377 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3378 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3381 if (wbc->sync_mode == WB_SYNC_ALL)
3382 tag = PAGECACHE_TAG_TOWRITE;
3384 tag = PAGECACHE_TAG_DIRTY;
3386 if (wbc->sync_mode == WB_SYNC_ALL)
3387 tag_pages_for_writeback(mapping, index, end);
3388 while (!done && !nr_to_write_done && (index <= end) &&
3389 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3390 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3394 for (i = 0; i < nr_pages; i++) {
3395 struct page *page = pvec.pages[i];
3397 if (!PagePrivate(page))
3400 if (!wbc->range_cyclic && page->index > end) {
3405 spin_lock(&mapping->private_lock);
3406 if (!PagePrivate(page)) {
3407 spin_unlock(&mapping->private_lock);
3411 eb = (struct extent_buffer *)page->private;
3414 * Shouldn't happen and normally this would be a BUG_ON
3415 * but no sense in crashing the users box for something
3416 * we can survive anyway.
3419 spin_unlock(&mapping->private_lock);
3424 if (eb == prev_eb) {
3425 spin_unlock(&mapping->private_lock);
3429 ret = atomic_inc_not_zero(&eb->refs);
3430 spin_unlock(&mapping->private_lock);
3435 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3437 free_extent_buffer(eb);
3441 ret = write_one_eb(eb, fs_info, wbc, &epd);
3444 free_extent_buffer(eb);
3447 free_extent_buffer(eb);
3450 * the filesystem may choose to bump up nr_to_write.
3451 * We have to make sure to honor the new nr_to_write
3454 nr_to_write_done = wbc->nr_to_write <= 0;
3456 pagevec_release(&pvec);
3459 if (!scanned && !done) {
3461 * We hit the last page and there is more work to be done: wrap
3462 * back to the start of the file
3468 flush_write_bio(&epd);
3473 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3474 * @mapping: address space structure to write
3475 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3476 * @writepage: function called for each page
3477 * @data: data passed to writepage function
3479 * If a page is already under I/O, write_cache_pages() skips it, even
3480 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3481 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3482 * and msync() need to guarantee that all the data which was dirty at the time
3483 * the call was made get new I/O started against them. If wbc->sync_mode is
3484 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3485 * existing IO to complete.
3487 static int extent_write_cache_pages(struct extent_io_tree *tree,
3488 struct address_space *mapping,
3489 struct writeback_control *wbc,
3490 writepage_t writepage, void *data,
3491 void (*flush_fn)(void *))
3493 struct inode *inode = mapping->host;
3496 int nr_to_write_done = 0;
3497 struct pagevec pvec;
3500 pgoff_t end; /* Inclusive */
3505 * We have to hold onto the inode so that ordered extents can do their
3506 * work when the IO finishes. The alternative to this is failing to add
3507 * an ordered extent if the igrab() fails there and that is a huge pain
3508 * to deal with, so instead just hold onto the inode throughout the
3509 * writepages operation. If it fails here we are freeing up the inode
3510 * anyway and we'd rather not waste our time writing out stuff that is
3511 * going to be truncated anyway.
3516 pagevec_init(&pvec, 0);
3517 if (wbc->range_cyclic) {
3518 index = mapping->writeback_index; /* Start from prev offset */
3521 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3522 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3525 if (wbc->sync_mode == WB_SYNC_ALL)
3526 tag = PAGECACHE_TAG_TOWRITE;
3528 tag = PAGECACHE_TAG_DIRTY;
3530 if (wbc->sync_mode == WB_SYNC_ALL)
3531 tag_pages_for_writeback(mapping, index, end);
3532 while (!done && !nr_to_write_done && (index <= end) &&
3533 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3534 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3538 for (i = 0; i < nr_pages; i++) {
3539 struct page *page = pvec.pages[i];
3542 * At this point we hold neither mapping->tree_lock nor
3543 * lock on the page itself: the page may be truncated or
3544 * invalidated (changing page->mapping to NULL), or even
3545 * swizzled back from swapper_space to tmpfs file
3548 if (!trylock_page(page)) {
3553 if (unlikely(page->mapping != mapping)) {
3558 if (!wbc->range_cyclic && page->index > end) {
3564 if (wbc->sync_mode != WB_SYNC_NONE) {
3565 if (PageWriteback(page))
3567 wait_on_page_writeback(page);
3570 if (PageWriteback(page) ||
3571 !clear_page_dirty_for_io(page)) {
3576 ret = (*writepage)(page, wbc, data);
3578 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3586 * the filesystem may choose to bump up nr_to_write.
3587 * We have to make sure to honor the new nr_to_write
3590 nr_to_write_done = wbc->nr_to_write <= 0;
3592 pagevec_release(&pvec);
3595 if (!scanned && !done) {
3597 * We hit the last page and there is more work to be done: wrap
3598 * back to the start of the file
3604 btrfs_add_delayed_iput(inode);
3608 static void flush_epd_write_bio(struct extent_page_data *epd)
3617 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
3618 BUG_ON(ret < 0); /* -ENOMEM */
3623 static noinline void flush_write_bio(void *data)
3625 struct extent_page_data *epd = data;
3626 flush_epd_write_bio(epd);
3629 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3630 get_extent_t *get_extent,
3631 struct writeback_control *wbc)
3634 struct extent_page_data epd = {
3637 .get_extent = get_extent,
3639 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3643 ret = __extent_writepage(page, wbc, &epd);
3645 flush_epd_write_bio(&epd);
3649 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3650 u64 start, u64 end, get_extent_t *get_extent,
3654 struct address_space *mapping = inode->i_mapping;
3656 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3659 struct extent_page_data epd = {
3662 .get_extent = get_extent,
3664 .sync_io = mode == WB_SYNC_ALL,
3667 struct writeback_control wbc_writepages = {
3669 .nr_to_write = nr_pages * 2,
3670 .range_start = start,
3671 .range_end = end + 1,
3674 while (start <= end) {
3675 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3676 if (clear_page_dirty_for_io(page))
3677 ret = __extent_writepage(page, &wbc_writepages, &epd);
3679 if (tree->ops && tree->ops->writepage_end_io_hook)
3680 tree->ops->writepage_end_io_hook(page, start,
3681 start + PAGE_CACHE_SIZE - 1,
3685 page_cache_release(page);
3686 start += PAGE_CACHE_SIZE;
3689 flush_epd_write_bio(&epd);
3693 int extent_writepages(struct extent_io_tree *tree,
3694 struct address_space *mapping,
3695 get_extent_t *get_extent,
3696 struct writeback_control *wbc)
3699 struct extent_page_data epd = {
3702 .get_extent = get_extent,
3704 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3708 ret = extent_write_cache_pages(tree, mapping, wbc,
3709 __extent_writepage, &epd,
3711 flush_epd_write_bio(&epd);
3715 int extent_readpages(struct extent_io_tree *tree,
3716 struct address_space *mapping,
3717 struct list_head *pages, unsigned nr_pages,
3718 get_extent_t get_extent)
3720 struct bio *bio = NULL;
3722 unsigned long bio_flags = 0;
3723 struct page *pagepool[16];
3728 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3729 page = list_entry(pages->prev, struct page, lru);
3731 prefetchw(&page->flags);
3732 list_del(&page->lru);
3733 if (add_to_page_cache_lru(page, mapping,
3734 page->index, GFP_NOFS)) {
3735 page_cache_release(page);
3739 pagepool[nr++] = page;
3740 if (nr < ARRAY_SIZE(pagepool))
3742 for (i = 0; i < nr; i++) {
3743 __extent_read_full_page(tree, pagepool[i], get_extent,
3744 &bio, 0, &bio_flags, READ);
3745 page_cache_release(pagepool[i]);
3749 for (i = 0; i < nr; i++) {
3750 __extent_read_full_page(tree, pagepool[i], get_extent,
3751 &bio, 0, &bio_flags, READ);
3752 page_cache_release(pagepool[i]);
3755 BUG_ON(!list_empty(pages));
3757 return submit_one_bio(READ, bio, 0, bio_flags);
3762 * basic invalidatepage code, this waits on any locked or writeback
3763 * ranges corresponding to the page, and then deletes any extent state
3764 * records from the tree
3766 int extent_invalidatepage(struct extent_io_tree *tree,
3767 struct page *page, unsigned long offset)
3769 struct extent_state *cached_state = NULL;
3770 u64 start = page_offset(page);
3771 u64 end = start + PAGE_CACHE_SIZE - 1;
3772 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3774 start += ALIGN(offset, blocksize);
3778 lock_extent_bits(tree, start, end, 0, &cached_state);
3779 wait_on_page_writeback(page);
3780 clear_extent_bit(tree, start, end,
3781 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3782 EXTENT_DO_ACCOUNTING,
3783 1, 1, &cached_state, GFP_NOFS);
3788 * a helper for releasepage, this tests for areas of the page that
3789 * are locked or under IO and drops the related state bits if it is safe
3792 static int try_release_extent_state(struct extent_map_tree *map,
3793 struct extent_io_tree *tree,
3794 struct page *page, gfp_t mask)
3796 u64 start = page_offset(page);
3797 u64 end = start + PAGE_CACHE_SIZE - 1;
3800 if (test_range_bit(tree, start, end,
3801 EXTENT_IOBITS, 0, NULL))
3804 if ((mask & GFP_NOFS) == GFP_NOFS)
3807 * at this point we can safely clear everything except the
3808 * locked bit and the nodatasum bit
3810 ret = clear_extent_bit(tree, start, end,
3811 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3814 /* if clear_extent_bit failed for enomem reasons,
3815 * we can't allow the release to continue.
3826 * a helper for releasepage. As long as there are no locked extents
3827 * in the range corresponding to the page, both state records and extent
3828 * map records are removed
3830 int try_release_extent_mapping(struct extent_map_tree *map,
3831 struct extent_io_tree *tree, struct page *page,
3834 struct extent_map *em;
3835 u64 start = page_offset(page);
3836 u64 end = start + PAGE_CACHE_SIZE - 1;
3838 if ((mask & __GFP_WAIT) &&
3839 page->mapping->host->i_size > 16 * 1024 * 1024) {
3841 while (start <= end) {
3842 len = end - start + 1;
3843 write_lock(&map->lock);
3844 em = lookup_extent_mapping(map, start, len);
3846 write_unlock(&map->lock);
3849 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3850 em->start != start) {
3851 write_unlock(&map->lock);
3852 free_extent_map(em);
3855 if (!test_range_bit(tree, em->start,
3856 extent_map_end(em) - 1,
3857 EXTENT_LOCKED | EXTENT_WRITEBACK,
3859 remove_extent_mapping(map, em);
3860 /* once for the rb tree */
3861 free_extent_map(em);
3863 start = extent_map_end(em);
3864 write_unlock(&map->lock);
3867 free_extent_map(em);
3870 return try_release_extent_state(map, tree, page, mask);
3874 * helper function for fiemap, which doesn't want to see any holes.
3875 * This maps until we find something past 'last'
3877 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3880 get_extent_t *get_extent)
3882 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3883 struct extent_map *em;
3890 len = last - offset;
3893 len = ALIGN(len, sectorsize);
3894 em = get_extent(inode, NULL, 0, offset, len, 0);
3895 if (IS_ERR_OR_NULL(em))
3898 /* if this isn't a hole return it */
3899 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3900 em->block_start != EXTENT_MAP_HOLE) {
3904 /* this is a hole, advance to the next extent */
3905 offset = extent_map_end(em);
3906 free_extent_map(em);
3913 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3914 __u64 start, __u64 len, get_extent_t *get_extent)
3918 u64 max = start + len;
3922 u64 last_for_get_extent = 0;
3924 u64 isize = i_size_read(inode);
3925 struct btrfs_key found_key;
3926 struct extent_map *em = NULL;
3927 struct extent_state *cached_state = NULL;
3928 struct btrfs_path *path;
3929 struct btrfs_file_extent_item *item;
3934 unsigned long emflags;
3939 path = btrfs_alloc_path();
3942 path->leave_spinning = 1;
3944 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3945 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3948 * lookup the last file extent. We're not using i_size here
3949 * because there might be preallocation past i_size
3951 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3952 path, btrfs_ino(inode), -1, 0);
3954 btrfs_free_path(path);
3959 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3960 struct btrfs_file_extent_item);
3961 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3962 found_type = btrfs_key_type(&found_key);
3964 /* No extents, but there might be delalloc bits */
3965 if (found_key.objectid != btrfs_ino(inode) ||
3966 found_type != BTRFS_EXTENT_DATA_KEY) {
3967 /* have to trust i_size as the end */
3969 last_for_get_extent = isize;
3972 * remember the start of the last extent. There are a
3973 * bunch of different factors that go into the length of the
3974 * extent, so its much less complex to remember where it started
3976 last = found_key.offset;
3977 last_for_get_extent = last + 1;
3979 btrfs_free_path(path);
3982 * we might have some extents allocated but more delalloc past those
3983 * extents. so, we trust isize unless the start of the last extent is
3988 last_for_get_extent = isize;
3991 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3994 em = get_extent_skip_holes(inode, start, last_for_get_extent,
4004 u64 offset_in_extent;
4006 /* break if the extent we found is outside the range */
4007 if (em->start >= max || extent_map_end(em) < off)
4011 * get_extent may return an extent that starts before our
4012 * requested range. We have to make sure the ranges
4013 * we return to fiemap always move forward and don't
4014 * overlap, so adjust the offsets here
4016 em_start = max(em->start, off);
4019 * record the offset from the start of the extent
4020 * for adjusting the disk offset below
4022 offset_in_extent = em_start - em->start;
4023 em_end = extent_map_end(em);
4024 em_len = em_end - em_start;
4025 emflags = em->flags;
4030 * bump off for our next call to get_extent
4032 off = extent_map_end(em);
4036 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
4038 flags |= FIEMAP_EXTENT_LAST;
4039 } else if (em->block_start == EXTENT_MAP_INLINE) {
4040 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4041 FIEMAP_EXTENT_NOT_ALIGNED);
4042 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
4043 flags |= (FIEMAP_EXTENT_DELALLOC |
4044 FIEMAP_EXTENT_UNKNOWN);
4046 disko = em->block_start + offset_in_extent;
4048 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4049 flags |= FIEMAP_EXTENT_ENCODED;
4051 free_extent_map(em);
4053 if ((em_start >= last) || em_len == (u64)-1 ||
4054 (last == (u64)-1 && isize <= em_end)) {
4055 flags |= FIEMAP_EXTENT_LAST;
4059 /* now scan forward to see if this is really the last extent. */
4060 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4067 flags |= FIEMAP_EXTENT_LAST;
4070 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4076 free_extent_map(em);
4078 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
4079 &cached_state, GFP_NOFS);
4083 static void __free_extent_buffer(struct extent_buffer *eb)
4085 btrfs_leak_debug_del(&eb->leak_list);
4086 kmem_cache_free(extent_buffer_cache, eb);
4089 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4094 struct extent_buffer *eb = NULL;
4096 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
4103 rwlock_init(&eb->lock);
4104 atomic_set(&eb->write_locks, 0);
4105 atomic_set(&eb->read_locks, 0);
4106 atomic_set(&eb->blocking_readers, 0);
4107 atomic_set(&eb->blocking_writers, 0);
4108 atomic_set(&eb->spinning_readers, 0);
4109 atomic_set(&eb->spinning_writers, 0);
4110 eb->lock_nested = 0;
4111 init_waitqueue_head(&eb->write_lock_wq);
4112 init_waitqueue_head(&eb->read_lock_wq);
4114 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4116 spin_lock_init(&eb->refs_lock);
4117 atomic_set(&eb->refs, 1);
4118 atomic_set(&eb->io_pages, 0);
4121 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4123 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4124 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4125 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4130 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4134 struct extent_buffer *new;
4135 unsigned long num_pages = num_extent_pages(src->start, src->len);
4137 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4141 for (i = 0; i < num_pages; i++) {
4142 p = alloc_page(GFP_ATOMIC);
4144 attach_extent_buffer_page(new, p);
4145 WARN_ON(PageDirty(p));
4150 copy_extent_buffer(new, src, 0, 0, src->len);
4151 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4152 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4157 struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4159 struct extent_buffer *eb;
4160 unsigned long num_pages = num_extent_pages(0, len);
4163 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4167 for (i = 0; i < num_pages; i++) {
4168 eb->pages[i] = alloc_page(GFP_ATOMIC);
4172 set_extent_buffer_uptodate(eb);
4173 btrfs_set_header_nritems(eb, 0);
4174 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4179 __free_page(eb->pages[i - 1]);
4180 __free_extent_buffer(eb);
4184 static int extent_buffer_under_io(struct extent_buffer *eb)
4186 return (atomic_read(&eb->io_pages) ||
4187 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4188 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4192 * Helper for releasing extent buffer page.
4194 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4195 unsigned long start_idx)
4197 unsigned long index;
4198 unsigned long num_pages;
4200 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4202 BUG_ON(extent_buffer_under_io(eb));
4204 num_pages = num_extent_pages(eb->start, eb->len);
4205 index = start_idx + num_pages;
4206 if (start_idx >= index)
4211 page = extent_buffer_page(eb, index);
4212 if (page && mapped) {
4213 spin_lock(&page->mapping->private_lock);
4215 * We do this since we'll remove the pages after we've
4216 * removed the eb from the radix tree, so we could race
4217 * and have this page now attached to the new eb. So
4218 * only clear page_private if it's still connected to
4221 if (PagePrivate(page) &&
4222 page->private == (unsigned long)eb) {
4223 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4224 BUG_ON(PageDirty(page));
4225 BUG_ON(PageWriteback(page));
4227 * We need to make sure we haven't be attached
4230 ClearPagePrivate(page);
4231 set_page_private(page, 0);
4232 /* One for the page private */
4233 page_cache_release(page);
4235 spin_unlock(&page->mapping->private_lock);
4239 /* One for when we alloced the page */
4240 page_cache_release(page);
4242 } while (index != start_idx);
4246 * Helper for releasing the extent buffer.
4248 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4250 btrfs_release_extent_buffer_page(eb, 0);
4251 __free_extent_buffer(eb);
4254 static void check_buffer_tree_ref(struct extent_buffer *eb)
4257 /* the ref bit is tricky. We have to make sure it is set
4258 * if we have the buffer dirty. Otherwise the
4259 * code to free a buffer can end up dropping a dirty
4262 * Once the ref bit is set, it won't go away while the
4263 * buffer is dirty or in writeback, and it also won't
4264 * go away while we have the reference count on the
4267 * We can't just set the ref bit without bumping the
4268 * ref on the eb because free_extent_buffer might
4269 * see the ref bit and try to clear it. If this happens
4270 * free_extent_buffer might end up dropping our original
4271 * ref by mistake and freeing the page before we are able
4272 * to add one more ref.
4274 * So bump the ref count first, then set the bit. If someone
4275 * beat us to it, drop the ref we added.
4277 refs = atomic_read(&eb->refs);
4278 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4281 spin_lock(&eb->refs_lock);
4282 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4283 atomic_inc(&eb->refs);
4284 spin_unlock(&eb->refs_lock);
4287 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4289 unsigned long num_pages, i;
4291 check_buffer_tree_ref(eb);
4293 num_pages = num_extent_pages(eb->start, eb->len);
4294 for (i = 0; i < num_pages; i++) {
4295 struct page *p = extent_buffer_page(eb, i);
4296 mark_page_accessed(p);
4300 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
4301 u64 start, unsigned long len)
4303 unsigned long num_pages = num_extent_pages(start, len);
4305 unsigned long index = start >> PAGE_CACHE_SHIFT;
4306 struct extent_buffer *eb;
4307 struct extent_buffer *exists = NULL;
4309 struct address_space *mapping = tree->mapping;
4314 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4315 if (eb && atomic_inc_not_zero(&eb->refs)) {
4317 mark_extent_buffer_accessed(eb);
4322 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
4326 for (i = 0; i < num_pages; i++, index++) {
4327 p = find_or_create_page(mapping, index, GFP_NOFS);
4331 spin_lock(&mapping->private_lock);
4332 if (PagePrivate(p)) {
4334 * We could have already allocated an eb for this page
4335 * and attached one so lets see if we can get a ref on
4336 * the existing eb, and if we can we know it's good and
4337 * we can just return that one, else we know we can just
4338 * overwrite page->private.
4340 exists = (struct extent_buffer *)p->private;
4341 if (atomic_inc_not_zero(&exists->refs)) {
4342 spin_unlock(&mapping->private_lock);
4344 page_cache_release(p);
4345 mark_extent_buffer_accessed(exists);
4350 * Do this so attach doesn't complain and we need to
4351 * drop the ref the old guy had.
4353 ClearPagePrivate(p);
4354 WARN_ON(PageDirty(p));
4355 page_cache_release(p);
4357 attach_extent_buffer_page(eb, p);
4358 spin_unlock(&mapping->private_lock);
4359 WARN_ON(PageDirty(p));
4360 mark_page_accessed(p);
4362 if (!PageUptodate(p))
4366 * see below about how we avoid a nasty race with release page
4367 * and why we unlock later
4371 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4373 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4377 spin_lock(&tree->buffer_lock);
4378 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4379 if (ret == -EEXIST) {
4380 exists = radix_tree_lookup(&tree->buffer,
4381 start >> PAGE_CACHE_SHIFT);
4382 if (!atomic_inc_not_zero(&exists->refs)) {
4383 spin_unlock(&tree->buffer_lock);
4384 radix_tree_preload_end();
4388 spin_unlock(&tree->buffer_lock);
4389 radix_tree_preload_end();
4390 mark_extent_buffer_accessed(exists);
4393 /* add one reference for the tree */
4394 check_buffer_tree_ref(eb);
4395 spin_unlock(&tree->buffer_lock);
4396 radix_tree_preload_end();
4399 * there is a race where release page may have
4400 * tried to find this extent buffer in the radix
4401 * but failed. It will tell the VM it is safe to
4402 * reclaim the, and it will clear the page private bit.
4403 * We must make sure to set the page private bit properly
4404 * after the extent buffer is in the radix tree so
4405 * it doesn't get lost
4407 SetPageChecked(eb->pages[0]);
4408 for (i = 1; i < num_pages; i++) {
4409 p = extent_buffer_page(eb, i);
4410 ClearPageChecked(p);
4413 unlock_page(eb->pages[0]);
4417 for (i = 0; i < num_pages; i++) {
4419 unlock_page(eb->pages[i]);
4422 WARN_ON(!atomic_dec_and_test(&eb->refs));
4423 btrfs_release_extent_buffer(eb);
4427 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
4428 u64 start, unsigned long len)
4430 struct extent_buffer *eb;
4433 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4434 if (eb && atomic_inc_not_zero(&eb->refs)) {
4436 mark_extent_buffer_accessed(eb);
4444 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4446 struct extent_buffer *eb =
4447 container_of(head, struct extent_buffer, rcu_head);
4449 __free_extent_buffer(eb);
4452 /* Expects to have eb->eb_lock already held */
4453 static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4455 WARN_ON(atomic_read(&eb->refs) == 0);
4456 if (atomic_dec_and_test(&eb->refs)) {
4457 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4458 spin_unlock(&eb->refs_lock);
4460 struct extent_io_tree *tree = eb->tree;
4462 spin_unlock(&eb->refs_lock);
4464 spin_lock(&tree->buffer_lock);
4465 radix_tree_delete(&tree->buffer,
4466 eb->start >> PAGE_CACHE_SHIFT);
4467 spin_unlock(&tree->buffer_lock);
4470 /* Should be safe to release our pages at this point */
4471 btrfs_release_extent_buffer_page(eb, 0);
4472 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4475 spin_unlock(&eb->refs_lock);
4480 void free_extent_buffer(struct extent_buffer *eb)
4488 refs = atomic_read(&eb->refs);
4491 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4496 spin_lock(&eb->refs_lock);
4497 if (atomic_read(&eb->refs) == 2 &&
4498 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4499 atomic_dec(&eb->refs);
4501 if (atomic_read(&eb->refs) == 2 &&
4502 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
4503 !extent_buffer_under_io(eb) &&
4504 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4505 atomic_dec(&eb->refs);
4508 * I know this is terrible, but it's temporary until we stop tracking
4509 * the uptodate bits and such for the extent buffers.
4511 release_extent_buffer(eb, GFP_ATOMIC);
4514 void free_extent_buffer_stale(struct extent_buffer *eb)
4519 spin_lock(&eb->refs_lock);
4520 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4522 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
4523 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4524 atomic_dec(&eb->refs);
4525 release_extent_buffer(eb, GFP_NOFS);
4528 void clear_extent_buffer_dirty(struct extent_buffer *eb)
4531 unsigned long num_pages;
4534 num_pages = num_extent_pages(eb->start, eb->len);
4536 for (i = 0; i < num_pages; i++) {
4537 page = extent_buffer_page(eb, i);
4538 if (!PageDirty(page))
4542 WARN_ON(!PagePrivate(page));
4544 clear_page_dirty_for_io(page);
4545 spin_lock_irq(&page->mapping->tree_lock);
4546 if (!PageDirty(page)) {
4547 radix_tree_tag_clear(&page->mapping->page_tree,
4549 PAGECACHE_TAG_DIRTY);
4551 spin_unlock_irq(&page->mapping->tree_lock);
4552 ClearPageError(page);
4555 WARN_ON(atomic_read(&eb->refs) == 0);
4558 int set_extent_buffer_dirty(struct extent_buffer *eb)
4561 unsigned long num_pages;
4564 check_buffer_tree_ref(eb);
4566 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4568 num_pages = num_extent_pages(eb->start, eb->len);
4569 WARN_ON(atomic_read(&eb->refs) == 0);
4570 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4572 for (i = 0; i < num_pages; i++)
4573 set_page_dirty(extent_buffer_page(eb, i));
4577 int clear_extent_buffer_uptodate(struct extent_buffer *eb)
4581 unsigned long num_pages;
4583 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4584 num_pages = num_extent_pages(eb->start, eb->len);
4585 for (i = 0; i < num_pages; i++) {
4586 page = extent_buffer_page(eb, i);
4588 ClearPageUptodate(page);
4593 int set_extent_buffer_uptodate(struct extent_buffer *eb)
4597 unsigned long num_pages;
4599 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4600 num_pages = num_extent_pages(eb->start, eb->len);
4601 for (i = 0; i < num_pages; i++) {
4602 page = extent_buffer_page(eb, i);
4603 SetPageUptodate(page);
4608 int extent_buffer_uptodate(struct extent_buffer *eb)
4610 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4613 int read_extent_buffer_pages(struct extent_io_tree *tree,
4614 struct extent_buffer *eb, u64 start, int wait,
4615 get_extent_t *get_extent, int mirror_num)
4618 unsigned long start_i;
4622 int locked_pages = 0;
4623 int all_uptodate = 1;
4624 unsigned long num_pages;
4625 unsigned long num_reads = 0;
4626 struct bio *bio = NULL;
4627 unsigned long bio_flags = 0;
4629 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4633 WARN_ON(start < eb->start);
4634 start_i = (start >> PAGE_CACHE_SHIFT) -
4635 (eb->start >> PAGE_CACHE_SHIFT);
4640 num_pages = num_extent_pages(eb->start, eb->len);
4641 for (i = start_i; i < num_pages; i++) {
4642 page = extent_buffer_page(eb, i);
4643 if (wait == WAIT_NONE) {
4644 if (!trylock_page(page))
4650 if (!PageUptodate(page)) {
4657 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4661 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
4662 eb->read_mirror = 0;
4663 atomic_set(&eb->io_pages, num_reads);
4664 for (i = start_i; i < num_pages; i++) {
4665 page = extent_buffer_page(eb, i);
4666 if (!PageUptodate(page)) {
4667 ClearPageError(page);
4668 err = __extent_read_full_page(tree, page,
4670 mirror_num, &bio_flags,
4680 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
4686 if (ret || wait != WAIT_COMPLETE)
4689 for (i = start_i; i < num_pages; i++) {
4690 page = extent_buffer_page(eb, i);
4691 wait_on_page_locked(page);
4692 if (!PageUptodate(page))
4700 while (locked_pages > 0) {
4701 page = extent_buffer_page(eb, i);
4709 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4710 unsigned long start,
4717 char *dst = (char *)dstv;
4718 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4719 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4721 WARN_ON(start > eb->len);
4722 WARN_ON(start + len > eb->start + eb->len);
4724 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4727 page = extent_buffer_page(eb, i);
4729 cur = min(len, (PAGE_CACHE_SIZE - offset));
4730 kaddr = page_address(page);
4731 memcpy(dst, kaddr + offset, cur);
4740 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4741 unsigned long min_len, char **map,
4742 unsigned long *map_start,
4743 unsigned long *map_len)
4745 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4748 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4749 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4750 unsigned long end_i = (start_offset + start + min_len - 1) >>
4757 offset = start_offset;
4761 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4764 if (start + min_len > eb->len) {
4765 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4766 "wanted %lu %lu\n", (unsigned long long)eb->start,
4767 eb->len, start, min_len);
4771 p = extent_buffer_page(eb, i);
4772 kaddr = page_address(p);
4773 *map = kaddr + offset;
4774 *map_len = PAGE_CACHE_SIZE - offset;
4778 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4779 unsigned long start,
4786 char *ptr = (char *)ptrv;
4787 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4788 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4791 WARN_ON(start > eb->len);
4792 WARN_ON(start + len > eb->start + eb->len);
4794 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4797 page = extent_buffer_page(eb, i);
4799 cur = min(len, (PAGE_CACHE_SIZE - offset));
4801 kaddr = page_address(page);
4802 ret = memcmp(ptr, kaddr + offset, cur);
4814 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4815 unsigned long start, unsigned long len)
4821 char *src = (char *)srcv;
4822 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4823 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4825 WARN_ON(start > eb->len);
4826 WARN_ON(start + len > eb->start + eb->len);
4828 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4831 page = extent_buffer_page(eb, i);
4832 WARN_ON(!PageUptodate(page));
4834 cur = min(len, PAGE_CACHE_SIZE - offset);
4835 kaddr = page_address(page);
4836 memcpy(kaddr + offset, src, cur);
4845 void memset_extent_buffer(struct extent_buffer *eb, char c,
4846 unsigned long start, unsigned long len)
4852 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4853 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4855 WARN_ON(start > eb->len);
4856 WARN_ON(start + len > eb->start + eb->len);
4858 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4861 page = extent_buffer_page(eb, i);
4862 WARN_ON(!PageUptodate(page));
4864 cur = min(len, PAGE_CACHE_SIZE - offset);
4865 kaddr = page_address(page);
4866 memset(kaddr + offset, c, cur);
4874 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4875 unsigned long dst_offset, unsigned long src_offset,
4878 u64 dst_len = dst->len;
4883 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4884 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4886 WARN_ON(src->len != dst_len);
4888 offset = (start_offset + dst_offset) &
4889 ((unsigned long)PAGE_CACHE_SIZE - 1);
4892 page = extent_buffer_page(dst, i);
4893 WARN_ON(!PageUptodate(page));
4895 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4897 kaddr = page_address(page);
4898 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4907 static void move_pages(struct page *dst_page, struct page *src_page,
4908 unsigned long dst_off, unsigned long src_off,
4911 char *dst_kaddr = page_address(dst_page);
4912 if (dst_page == src_page) {
4913 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4915 char *src_kaddr = page_address(src_page);
4916 char *p = dst_kaddr + dst_off + len;
4917 char *s = src_kaddr + src_off + len;
4924 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4926 unsigned long distance = (src > dst) ? src - dst : dst - src;
4927 return distance < len;
4930 static void copy_pages(struct page *dst_page, struct page *src_page,
4931 unsigned long dst_off, unsigned long src_off,
4934 char *dst_kaddr = page_address(dst_page);
4936 int must_memmove = 0;
4938 if (dst_page != src_page) {
4939 src_kaddr = page_address(src_page);
4941 src_kaddr = dst_kaddr;
4942 if (areas_overlap(src_off, dst_off, len))
4947 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4949 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
4952 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4953 unsigned long src_offset, unsigned long len)
4956 size_t dst_off_in_page;
4957 size_t src_off_in_page;
4958 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4959 unsigned long dst_i;
4960 unsigned long src_i;
4962 if (src_offset + len > dst->len) {
4963 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4964 "len %lu dst len %lu\n", src_offset, len, dst->len);
4967 if (dst_offset + len > dst->len) {
4968 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4969 "len %lu dst len %lu\n", dst_offset, len, dst->len);
4974 dst_off_in_page = (start_offset + dst_offset) &
4975 ((unsigned long)PAGE_CACHE_SIZE - 1);
4976 src_off_in_page = (start_offset + src_offset) &
4977 ((unsigned long)PAGE_CACHE_SIZE - 1);
4979 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4980 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4982 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4984 cur = min_t(unsigned long, cur,
4985 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4987 copy_pages(extent_buffer_page(dst, dst_i),
4988 extent_buffer_page(dst, src_i),
4989 dst_off_in_page, src_off_in_page, cur);
4997 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4998 unsigned long src_offset, unsigned long len)
5001 size_t dst_off_in_page;
5002 size_t src_off_in_page;
5003 unsigned long dst_end = dst_offset + len - 1;
5004 unsigned long src_end = src_offset + len - 1;
5005 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5006 unsigned long dst_i;
5007 unsigned long src_i;
5009 if (src_offset + len > dst->len) {
5010 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5011 "len %lu len %lu\n", src_offset, len, dst->len);
5014 if (dst_offset + len > dst->len) {
5015 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5016 "len %lu len %lu\n", dst_offset, len, dst->len);
5019 if (dst_offset < src_offset) {
5020 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5024 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5025 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5027 dst_off_in_page = (start_offset + dst_end) &
5028 ((unsigned long)PAGE_CACHE_SIZE - 1);
5029 src_off_in_page = (start_offset + src_end) &
5030 ((unsigned long)PAGE_CACHE_SIZE - 1);
5032 cur = min_t(unsigned long, len, src_off_in_page + 1);
5033 cur = min(cur, dst_off_in_page + 1);
5034 move_pages(extent_buffer_page(dst, dst_i),
5035 extent_buffer_page(dst, src_i),
5036 dst_off_in_page - cur + 1,
5037 src_off_in_page - cur + 1, cur);
5045 int try_release_extent_buffer(struct page *page, gfp_t mask)
5047 struct extent_buffer *eb;
5050 * We need to make sure noboody is attaching this page to an eb right
5053 spin_lock(&page->mapping->private_lock);
5054 if (!PagePrivate(page)) {
5055 spin_unlock(&page->mapping->private_lock);
5059 eb = (struct extent_buffer *)page->private;
5063 * This is a little awful but should be ok, we need to make sure that
5064 * the eb doesn't disappear out from under us while we're looking at
5067 spin_lock(&eb->refs_lock);
5068 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
5069 spin_unlock(&eb->refs_lock);
5070 spin_unlock(&page->mapping->private_lock);
5073 spin_unlock(&page->mapping->private_lock);
5075 if ((mask & GFP_NOFS) == GFP_NOFS)
5079 * If tree ref isn't set then we know the ref on this eb is a real ref,
5080 * so just return, this page will likely be freed soon anyway.
5082 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5083 spin_unlock(&eb->refs_lock);
5087 return release_extent_buffer(eb, mask);