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;
26 static struct bio_set *btrfs_bioset;
28 #ifdef CONFIG_BTRFS_DEBUG
29 static LIST_HEAD(buffers);
30 static LIST_HEAD(states);
32 static DEFINE_SPINLOCK(leak_lock);
35 void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
39 spin_lock_irqsave(&leak_lock, flags);
41 spin_unlock_irqrestore(&leak_lock, flags);
45 void btrfs_leak_debug_del(struct list_head *entry)
49 spin_lock_irqsave(&leak_lock, flags);
51 spin_unlock_irqrestore(&leak_lock, flags);
55 void btrfs_leak_debug_check(void)
57 struct extent_state *state;
58 struct extent_buffer *eb;
60 while (!list_empty(&states)) {
61 state = list_entry(states.next, struct extent_state, leak_list);
62 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
63 "state %lu in tree %p refs %d\n",
64 (unsigned long long)state->start,
65 (unsigned long long)state->end,
66 state->state, state->tree, atomic_read(&state->refs));
67 list_del(&state->leak_list);
68 kmem_cache_free(extent_state_cache, state);
71 while (!list_empty(&buffers)) {
72 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
73 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
74 "refs %d\n", (unsigned long long)eb->start,
75 eb->len, atomic_read(&eb->refs));
76 list_del(&eb->leak_list);
77 kmem_cache_free(extent_buffer_cache, eb);
81 #define btrfs_debug_check_extent_io_range(inode, start, end) \
82 __btrfs_debug_check_extent_io_range(__func__, (inode), (start), (end))
83 static inline void __btrfs_debug_check_extent_io_range(const char *caller,
84 struct inode *inode, u64 start, u64 end)
86 u64 isize = i_size_read(inode);
88 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
89 printk_ratelimited(KERN_DEBUG
90 "btrfs: %s: ino %llu isize %llu odd range [%llu,%llu]\n",
92 (unsigned long long)btrfs_ino(inode),
93 (unsigned long long)isize,
94 (unsigned long long)start,
95 (unsigned long long)end);
99 #define btrfs_leak_debug_add(new, head) do {} while (0)
100 #define btrfs_leak_debug_del(entry) do {} while (0)
101 #define btrfs_leak_debug_check() do {} while (0)
102 #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
105 #define BUFFER_LRU_MAX 64
110 struct rb_node rb_node;
113 struct extent_page_data {
115 struct extent_io_tree *tree;
116 get_extent_t *get_extent;
117 unsigned long bio_flags;
119 /* tells writepage not to lock the state bits for this range
120 * it still does the unlocking
122 unsigned int extent_locked:1;
124 /* tells the submit_bio code to use a WRITE_SYNC */
125 unsigned int sync_io:1;
128 static noinline void flush_write_bio(void *data);
129 static inline struct btrfs_fs_info *
130 tree_fs_info(struct extent_io_tree *tree)
132 return btrfs_sb(tree->mapping->host->i_sb);
135 int __init extent_io_init(void)
137 extent_state_cache = kmem_cache_create("btrfs_extent_state",
138 sizeof(struct extent_state), 0,
139 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
140 if (!extent_state_cache)
143 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
144 sizeof(struct extent_buffer), 0,
145 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
146 if (!extent_buffer_cache)
147 goto free_state_cache;
149 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
150 offsetof(struct btrfs_io_bio, bio));
152 goto free_buffer_cache;
156 kmem_cache_destroy(extent_buffer_cache);
157 extent_buffer_cache = NULL;
160 kmem_cache_destroy(extent_state_cache);
161 extent_state_cache = NULL;
165 void extent_io_exit(void)
167 btrfs_leak_debug_check();
170 * Make sure all delayed rcu free are flushed before we
174 if (extent_state_cache)
175 kmem_cache_destroy(extent_state_cache);
176 if (extent_buffer_cache)
177 kmem_cache_destroy(extent_buffer_cache);
179 bioset_free(btrfs_bioset);
182 void extent_io_tree_init(struct extent_io_tree *tree,
183 struct address_space *mapping)
185 tree->state = RB_ROOT;
186 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
188 tree->dirty_bytes = 0;
189 spin_lock_init(&tree->lock);
190 spin_lock_init(&tree->buffer_lock);
191 tree->mapping = mapping;
194 static struct extent_state *alloc_extent_state(gfp_t mask)
196 struct extent_state *state;
198 state = kmem_cache_alloc(extent_state_cache, mask);
204 btrfs_leak_debug_add(&state->leak_list, &states);
205 atomic_set(&state->refs, 1);
206 init_waitqueue_head(&state->wq);
207 trace_alloc_extent_state(state, mask, _RET_IP_);
211 void free_extent_state(struct extent_state *state)
215 if (atomic_dec_and_test(&state->refs)) {
216 WARN_ON(state->tree);
217 btrfs_leak_debug_del(&state->leak_list);
218 trace_free_extent_state(state, _RET_IP_);
219 kmem_cache_free(extent_state_cache, state);
223 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
224 struct rb_node *node)
226 struct rb_node **p = &root->rb_node;
227 struct rb_node *parent = NULL;
228 struct tree_entry *entry;
232 entry = rb_entry(parent, struct tree_entry, rb_node);
234 if (offset < entry->start)
236 else if (offset > entry->end)
242 rb_link_node(node, parent, p);
243 rb_insert_color(node, root);
247 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
248 struct rb_node **prev_ret,
249 struct rb_node **next_ret)
251 struct rb_root *root = &tree->state;
252 struct rb_node *n = root->rb_node;
253 struct rb_node *prev = NULL;
254 struct rb_node *orig_prev = NULL;
255 struct tree_entry *entry;
256 struct tree_entry *prev_entry = NULL;
259 entry = rb_entry(n, struct tree_entry, rb_node);
263 if (offset < entry->start)
265 else if (offset > entry->end)
273 while (prev && offset > prev_entry->end) {
274 prev = rb_next(prev);
275 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
282 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
283 while (prev && offset < prev_entry->start) {
284 prev = rb_prev(prev);
285 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
292 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
295 struct rb_node *prev = NULL;
298 ret = __etree_search(tree, offset, &prev, NULL);
304 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
305 struct extent_state *other)
307 if (tree->ops && tree->ops->merge_extent_hook)
308 tree->ops->merge_extent_hook(tree->mapping->host, new,
313 * utility function to look for merge candidates inside a given range.
314 * Any extents with matching state are merged together into a single
315 * extent in the tree. Extents with EXTENT_IO in their state field
316 * are not merged because the end_io handlers need to be able to do
317 * operations on them without sleeping (or doing allocations/splits).
319 * This should be called with the tree lock held.
321 static void merge_state(struct extent_io_tree *tree,
322 struct extent_state *state)
324 struct extent_state *other;
325 struct rb_node *other_node;
327 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
330 other_node = rb_prev(&state->rb_node);
332 other = rb_entry(other_node, struct extent_state, rb_node);
333 if (other->end == state->start - 1 &&
334 other->state == state->state) {
335 merge_cb(tree, state, other);
336 state->start = other->start;
338 rb_erase(&other->rb_node, &tree->state);
339 free_extent_state(other);
342 other_node = rb_next(&state->rb_node);
344 other = rb_entry(other_node, struct extent_state, rb_node);
345 if (other->start == state->end + 1 &&
346 other->state == state->state) {
347 merge_cb(tree, state, other);
348 state->end = other->end;
350 rb_erase(&other->rb_node, &tree->state);
351 free_extent_state(other);
356 static void set_state_cb(struct extent_io_tree *tree,
357 struct extent_state *state, unsigned long *bits)
359 if (tree->ops && tree->ops->set_bit_hook)
360 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
363 static void clear_state_cb(struct extent_io_tree *tree,
364 struct extent_state *state, unsigned long *bits)
366 if (tree->ops && tree->ops->clear_bit_hook)
367 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
370 static void set_state_bits(struct extent_io_tree *tree,
371 struct extent_state *state, unsigned long *bits);
374 * insert an extent_state struct into the tree. 'bits' are set on the
375 * struct before it is inserted.
377 * This may return -EEXIST if the extent is already there, in which case the
378 * state struct is freed.
380 * The tree lock is not taken internally. This is a utility function and
381 * probably isn't what you want to call (see set/clear_extent_bit).
383 static int insert_state(struct extent_io_tree *tree,
384 struct extent_state *state, u64 start, u64 end,
387 struct rb_node *node;
390 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
391 (unsigned long long)end,
392 (unsigned long long)start);
393 state->start = start;
396 set_state_bits(tree, state, bits);
398 node = tree_insert(&tree->state, end, &state->rb_node);
400 struct extent_state *found;
401 found = rb_entry(node, struct extent_state, rb_node);
402 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
403 "%llu %llu\n", (unsigned long long)found->start,
404 (unsigned long long)found->end,
405 (unsigned long long)start, (unsigned long long)end);
409 merge_state(tree, state);
413 static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
416 if (tree->ops && tree->ops->split_extent_hook)
417 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
421 * split a given extent state struct in two, inserting the preallocated
422 * struct 'prealloc' as the newly created second half. 'split' indicates an
423 * offset inside 'orig' where it should be split.
426 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
427 * are two extent state structs in the tree:
428 * prealloc: [orig->start, split - 1]
429 * orig: [ split, orig->end ]
431 * The tree locks are not taken by this function. They need to be held
434 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
435 struct extent_state *prealloc, u64 split)
437 struct rb_node *node;
439 split_cb(tree, orig, split);
441 prealloc->start = orig->start;
442 prealloc->end = split - 1;
443 prealloc->state = orig->state;
446 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
448 free_extent_state(prealloc);
451 prealloc->tree = tree;
455 static struct extent_state *next_state(struct extent_state *state)
457 struct rb_node *next = rb_next(&state->rb_node);
459 return rb_entry(next, struct extent_state, rb_node);
465 * utility function to clear some bits in an extent state struct.
466 * it will optionally wake up any one waiting on this state (wake == 1).
468 * If no bits are set on the state struct after clearing things, the
469 * struct is freed and removed from the tree
471 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
472 struct extent_state *state,
473 unsigned long *bits, int wake)
475 struct extent_state *next;
476 unsigned long bits_to_clear = *bits & ~EXTENT_CTLBITS;
478 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
479 u64 range = state->end - state->start + 1;
480 WARN_ON(range > tree->dirty_bytes);
481 tree->dirty_bytes -= range;
483 clear_state_cb(tree, state, bits);
484 state->state &= ~bits_to_clear;
487 if (state->state == 0) {
488 next = next_state(state);
490 rb_erase(&state->rb_node, &tree->state);
492 free_extent_state(state);
497 merge_state(tree, state);
498 next = next_state(state);
503 static struct extent_state *
504 alloc_extent_state_atomic(struct extent_state *prealloc)
507 prealloc = alloc_extent_state(GFP_ATOMIC);
512 static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
514 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
515 "Extent tree was modified by another "
516 "thread while locked.");
520 * clear some bits on a range in the tree. This may require splitting
521 * or inserting elements in the tree, so the gfp mask is used to
522 * indicate which allocations or sleeping are allowed.
524 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
525 * the given range from the tree regardless of state (ie for truncate).
527 * the range [start, end] is inclusive.
529 * This takes the tree lock, and returns 0 on success and < 0 on error.
531 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
532 unsigned long bits, int wake, int delete,
533 struct extent_state **cached_state,
536 struct extent_state *state;
537 struct extent_state *cached;
538 struct extent_state *prealloc = NULL;
539 struct rb_node *node;
544 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
547 bits |= ~EXTENT_CTLBITS;
548 bits |= EXTENT_FIRST_DELALLOC;
550 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
553 if (!prealloc && (mask & __GFP_WAIT)) {
554 prealloc = alloc_extent_state(mask);
559 spin_lock(&tree->lock);
561 cached = *cached_state;
564 *cached_state = NULL;
568 if (cached && cached->tree && cached->start <= start &&
569 cached->end > start) {
571 atomic_dec(&cached->refs);
576 free_extent_state(cached);
579 * this search will find the extents that end after
582 node = tree_search(tree, start);
585 state = rb_entry(node, struct extent_state, rb_node);
587 if (state->start > end)
589 WARN_ON(state->end < start);
590 last_end = state->end;
592 /* the state doesn't have the wanted bits, go ahead */
593 if (!(state->state & bits)) {
594 state = next_state(state);
599 * | ---- desired range ---- |
601 * | ------------- state -------------- |
603 * We need to split the extent we found, and may flip
604 * bits on second half.
606 * If the extent we found extends past our range, we
607 * just split and search again. It'll get split again
608 * the next time though.
610 * If the extent we found is inside our range, we clear
611 * the desired bit on it.
614 if (state->start < start) {
615 prealloc = alloc_extent_state_atomic(prealloc);
617 err = split_state(tree, state, prealloc, start);
619 extent_io_tree_panic(tree, err);
624 if (state->end <= end) {
625 state = clear_state_bit(tree, state, &bits, wake);
631 * | ---- desired range ---- |
633 * We need to split the extent, and clear the bit
636 if (state->start <= end && state->end > end) {
637 prealloc = alloc_extent_state_atomic(prealloc);
639 err = split_state(tree, state, prealloc, end + 1);
641 extent_io_tree_panic(tree, err);
646 clear_state_bit(tree, prealloc, &bits, wake);
652 state = clear_state_bit(tree, state, &bits, wake);
654 if (last_end == (u64)-1)
656 start = last_end + 1;
657 if (start <= end && state && !need_resched())
662 spin_unlock(&tree->lock);
664 free_extent_state(prealloc);
671 spin_unlock(&tree->lock);
672 if (mask & __GFP_WAIT)
677 static void wait_on_state(struct extent_io_tree *tree,
678 struct extent_state *state)
679 __releases(tree->lock)
680 __acquires(tree->lock)
683 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
684 spin_unlock(&tree->lock);
686 spin_lock(&tree->lock);
687 finish_wait(&state->wq, &wait);
691 * waits for one or more bits to clear on a range in the state tree.
692 * The range [start, end] is inclusive.
693 * The tree lock is taken by this function
695 static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
698 struct extent_state *state;
699 struct rb_node *node;
701 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
703 spin_lock(&tree->lock);
707 * this search will find all the extents that end after
710 node = tree_search(tree, start);
714 state = rb_entry(node, struct extent_state, rb_node);
716 if (state->start > end)
719 if (state->state & bits) {
720 start = state->start;
721 atomic_inc(&state->refs);
722 wait_on_state(tree, state);
723 free_extent_state(state);
726 start = state->end + 1;
731 cond_resched_lock(&tree->lock);
734 spin_unlock(&tree->lock);
737 static void set_state_bits(struct extent_io_tree *tree,
738 struct extent_state *state,
741 unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS;
743 set_state_cb(tree, state, bits);
744 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
745 u64 range = state->end - state->start + 1;
746 tree->dirty_bytes += range;
748 state->state |= bits_to_set;
751 static void cache_state(struct extent_state *state,
752 struct extent_state **cached_ptr)
754 if (cached_ptr && !(*cached_ptr)) {
755 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
757 atomic_inc(&state->refs);
762 static void uncache_state(struct extent_state **cached_ptr)
764 if (cached_ptr && (*cached_ptr)) {
765 struct extent_state *state = *cached_ptr;
767 free_extent_state(state);
772 * set some bits on a range in the tree. This may require allocations or
773 * sleeping, so the gfp mask is used to indicate what is allowed.
775 * If any of the exclusive bits are set, this will fail with -EEXIST if some
776 * part of the range already has the desired bits set. The start of the
777 * existing range is returned in failed_start in this case.
779 * [start, end] is inclusive This takes the tree lock.
782 static int __must_check
783 __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
784 unsigned long bits, unsigned long exclusive_bits,
785 u64 *failed_start, struct extent_state **cached_state,
788 struct extent_state *state;
789 struct extent_state *prealloc = NULL;
790 struct rb_node *node;
795 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
797 bits |= EXTENT_FIRST_DELALLOC;
799 if (!prealloc && (mask & __GFP_WAIT)) {
800 prealloc = alloc_extent_state(mask);
804 spin_lock(&tree->lock);
805 if (cached_state && *cached_state) {
806 state = *cached_state;
807 if (state->start <= start && state->end > start &&
809 node = &state->rb_node;
814 * this search will find all the extents that end after
817 node = tree_search(tree, start);
819 prealloc = alloc_extent_state_atomic(prealloc);
821 err = insert_state(tree, prealloc, start, end, &bits);
823 extent_io_tree_panic(tree, err);
828 state = rb_entry(node, struct extent_state, rb_node);
830 last_start = state->start;
831 last_end = state->end;
834 * | ---- desired range ---- |
837 * Just lock what we found and keep going
839 if (state->start == start && state->end <= end) {
840 if (state->state & exclusive_bits) {
841 *failed_start = state->start;
846 set_state_bits(tree, state, &bits);
847 cache_state(state, cached_state);
848 merge_state(tree, state);
849 if (last_end == (u64)-1)
851 start = last_end + 1;
852 state = next_state(state);
853 if (start < end && state && state->start == start &&
860 * | ---- desired range ---- |
863 * | ------------- state -------------- |
865 * We need to split the extent we found, and may flip bits on
868 * If the extent we found extends past our
869 * range, we just split and search again. It'll get split
870 * again the next time though.
872 * If the extent we found is inside our range, we set the
875 if (state->start < start) {
876 if (state->state & exclusive_bits) {
877 *failed_start = start;
882 prealloc = alloc_extent_state_atomic(prealloc);
884 err = split_state(tree, state, prealloc, start);
886 extent_io_tree_panic(tree, err);
891 if (state->end <= end) {
892 set_state_bits(tree, state, &bits);
893 cache_state(state, cached_state);
894 merge_state(tree, state);
895 if (last_end == (u64)-1)
897 start = last_end + 1;
898 state = next_state(state);
899 if (start < end && state && state->start == start &&
906 * | ---- desired range ---- |
907 * | state | or | state |
909 * There's a hole, we need to insert something in it and
910 * ignore the extent we found.
912 if (state->start > start) {
914 if (end < last_start)
917 this_end = last_start - 1;
919 prealloc = alloc_extent_state_atomic(prealloc);
923 * Avoid to free 'prealloc' if it can be merged with
926 err = insert_state(tree, prealloc, start, this_end,
929 extent_io_tree_panic(tree, err);
931 cache_state(prealloc, cached_state);
933 start = this_end + 1;
937 * | ---- desired range ---- |
939 * We need to split the extent, and set the bit
942 if (state->start <= end && state->end > end) {
943 if (state->state & exclusive_bits) {
944 *failed_start = start;
949 prealloc = alloc_extent_state_atomic(prealloc);
951 err = split_state(tree, state, prealloc, end + 1);
953 extent_io_tree_panic(tree, err);
955 set_state_bits(tree, prealloc, &bits);
956 cache_state(prealloc, cached_state);
957 merge_state(tree, prealloc);
965 spin_unlock(&tree->lock);
967 free_extent_state(prealloc);
974 spin_unlock(&tree->lock);
975 if (mask & __GFP_WAIT)
980 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
981 unsigned long bits, u64 * failed_start,
982 struct extent_state **cached_state, gfp_t mask)
984 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
990 * convert_extent_bit - convert all bits in a given range from one bit to
992 * @tree: the io tree to search
993 * @start: the start offset in bytes
994 * @end: the end offset in bytes (inclusive)
995 * @bits: the bits to set in this range
996 * @clear_bits: the bits to clear in this range
997 * @cached_state: state that we're going to cache
998 * @mask: the allocation mask
1000 * This will go through and set bits for the given range. If any states exist
1001 * already in this range they are set with the given bit and cleared of the
1002 * clear_bits. This is only meant to be used by things that are mergeable, ie
1003 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1004 * boundary bits like LOCK.
1006 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1007 unsigned long bits, unsigned long clear_bits,
1008 struct extent_state **cached_state, gfp_t mask)
1010 struct extent_state *state;
1011 struct extent_state *prealloc = NULL;
1012 struct rb_node *node;
1017 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
1020 if (!prealloc && (mask & __GFP_WAIT)) {
1021 prealloc = alloc_extent_state(mask);
1026 spin_lock(&tree->lock);
1027 if (cached_state && *cached_state) {
1028 state = *cached_state;
1029 if (state->start <= start && state->end > start &&
1031 node = &state->rb_node;
1037 * this search will find all the extents that end after
1040 node = tree_search(tree, start);
1042 prealloc = alloc_extent_state_atomic(prealloc);
1047 err = insert_state(tree, prealloc, start, end, &bits);
1050 extent_io_tree_panic(tree, err);
1053 state = rb_entry(node, struct extent_state, rb_node);
1055 last_start = state->start;
1056 last_end = state->end;
1059 * | ---- desired range ---- |
1062 * Just lock what we found and keep going
1064 if (state->start == start && 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 ---- |
1081 * | ------------- state -------------- |
1083 * We need to split the extent we found, and may flip bits on
1086 * If the extent we found extends past our
1087 * range, we just split and search again. It'll get split
1088 * again the next time though.
1090 * If the extent we found is inside our range, we set the
1091 * desired bit on it.
1093 if (state->start < start) {
1094 prealloc = alloc_extent_state_atomic(prealloc);
1099 err = split_state(tree, state, prealloc, start);
1101 extent_io_tree_panic(tree, err);
1105 if (state->end <= end) {
1106 set_state_bits(tree, state, &bits);
1107 cache_state(state, cached_state);
1108 state = clear_state_bit(tree, state, &clear_bits, 0);
1109 if (last_end == (u64)-1)
1111 start = last_end + 1;
1112 if (start < end && state && state->start == start &&
1119 * | ---- desired range ---- |
1120 * | state | or | state |
1122 * There's a hole, we need to insert something in it and
1123 * ignore the extent we found.
1125 if (state->start > start) {
1127 if (end < last_start)
1130 this_end = last_start - 1;
1132 prealloc = alloc_extent_state_atomic(prealloc);
1139 * Avoid to free 'prealloc' if it can be merged with
1142 err = insert_state(tree, prealloc, start, this_end,
1145 extent_io_tree_panic(tree, err);
1146 cache_state(prealloc, cached_state);
1148 start = this_end + 1;
1152 * | ---- desired range ---- |
1154 * We need to split the extent, and set the bit
1157 if (state->start <= end && state->end > end) {
1158 prealloc = alloc_extent_state_atomic(prealloc);
1164 err = split_state(tree, state, prealloc, end + 1);
1166 extent_io_tree_panic(tree, err);
1168 set_state_bits(tree, prealloc, &bits);
1169 cache_state(prealloc, cached_state);
1170 clear_state_bit(tree, prealloc, &clear_bits, 0);
1178 spin_unlock(&tree->lock);
1180 free_extent_state(prealloc);
1187 spin_unlock(&tree->lock);
1188 if (mask & __GFP_WAIT)
1193 /* wrappers around set/clear extent bit */
1194 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1197 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
1201 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1202 unsigned long bits, gfp_t mask)
1204 return set_extent_bit(tree, start, end, bits, NULL,
1208 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1209 unsigned long bits, gfp_t mask)
1211 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
1214 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
1215 struct extent_state **cached_state, gfp_t mask)
1217 return set_extent_bit(tree, start, end,
1218 EXTENT_DELALLOC | EXTENT_UPTODATE,
1219 NULL, cached_state, mask);
1222 int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1223 struct extent_state **cached_state, gfp_t mask)
1225 return set_extent_bit(tree, start, end,
1226 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1227 NULL, cached_state, mask);
1230 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1233 return clear_extent_bit(tree, start, end,
1234 EXTENT_DIRTY | EXTENT_DELALLOC |
1235 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
1238 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1241 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
1245 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1246 struct extent_state **cached_state, gfp_t mask)
1248 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
1249 cached_state, mask);
1252 int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1253 struct extent_state **cached_state, gfp_t mask)
1255 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1256 cached_state, mask);
1260 * either insert or lock state struct between start and end use mask to tell
1261 * us if waiting is desired.
1263 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1264 unsigned long bits, struct extent_state **cached_state)
1269 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1270 EXTENT_LOCKED, &failed_start,
1271 cached_state, GFP_NOFS);
1272 if (err == -EEXIST) {
1273 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1274 start = failed_start;
1277 WARN_ON(start > end);
1282 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1284 return lock_extent_bits(tree, start, end, 0, NULL);
1287 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1292 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1293 &failed_start, NULL, GFP_NOFS);
1294 if (err == -EEXIST) {
1295 if (failed_start > start)
1296 clear_extent_bit(tree, start, failed_start - 1,
1297 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
1303 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1304 struct extent_state **cached, gfp_t mask)
1306 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1310 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1312 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1316 int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1318 unsigned long index = start >> PAGE_CACHE_SHIFT;
1319 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1322 while (index <= end_index) {
1323 page = find_get_page(inode->i_mapping, index);
1324 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1325 clear_page_dirty_for_io(page);
1326 page_cache_release(page);
1332 int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1334 unsigned long index = start >> PAGE_CACHE_SHIFT;
1335 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1338 while (index <= end_index) {
1339 page = find_get_page(inode->i_mapping, index);
1340 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1341 account_page_redirty(page);
1342 __set_page_dirty_nobuffers(page);
1343 page_cache_release(page);
1350 * helper function to set both pages and extents in the tree writeback
1352 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1354 unsigned long index = start >> PAGE_CACHE_SHIFT;
1355 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1358 while (index <= end_index) {
1359 page = find_get_page(tree->mapping, index);
1360 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1361 set_page_writeback(page);
1362 page_cache_release(page);
1368 /* find the first state struct with 'bits' set after 'start', and
1369 * return it. tree->lock must be held. NULL will returned if
1370 * nothing was found after 'start'
1372 static struct extent_state *
1373 find_first_extent_bit_state(struct extent_io_tree *tree,
1374 u64 start, unsigned long bits)
1376 struct rb_node *node;
1377 struct extent_state *state;
1380 * this search will find all the extents that end after
1383 node = tree_search(tree, start);
1388 state = rb_entry(node, struct extent_state, rb_node);
1389 if (state->end >= start && (state->state & bits))
1392 node = rb_next(node);
1401 * find the first offset in the io tree with 'bits' set. zero is
1402 * returned if we find something, and *start_ret and *end_ret are
1403 * set to reflect the state struct that was found.
1405 * If nothing was found, 1 is returned. If found something, return 0.
1407 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1408 u64 *start_ret, u64 *end_ret, unsigned long bits,
1409 struct extent_state **cached_state)
1411 struct extent_state *state;
1415 spin_lock(&tree->lock);
1416 if (cached_state && *cached_state) {
1417 state = *cached_state;
1418 if (state->end == start - 1 && state->tree) {
1419 n = rb_next(&state->rb_node);
1421 state = rb_entry(n, struct extent_state,
1423 if (state->state & bits)
1427 free_extent_state(*cached_state);
1428 *cached_state = NULL;
1431 free_extent_state(*cached_state);
1432 *cached_state = NULL;
1435 state = find_first_extent_bit_state(tree, start, bits);
1438 cache_state(state, cached_state);
1439 *start_ret = state->start;
1440 *end_ret = state->end;
1444 spin_unlock(&tree->lock);
1449 * find a contiguous range of bytes in the file marked as delalloc, not
1450 * more than 'max_bytes'. start and end are used to return the range,
1452 * 1 is returned if we find something, 0 if nothing was in the tree
1454 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1455 u64 *start, u64 *end, u64 max_bytes,
1456 struct extent_state **cached_state)
1458 struct rb_node *node;
1459 struct extent_state *state;
1460 u64 cur_start = *start;
1462 u64 total_bytes = 0;
1464 spin_lock(&tree->lock);
1467 * this search will find all the extents that end after
1470 node = tree_search(tree, cur_start);
1478 state = rb_entry(node, struct extent_state, rb_node);
1479 if (found && (state->start != cur_start ||
1480 (state->state & EXTENT_BOUNDARY))) {
1483 if (!(state->state & EXTENT_DELALLOC)) {
1489 *start = state->start;
1490 *cached_state = state;
1491 atomic_inc(&state->refs);
1495 cur_start = state->end + 1;
1496 node = rb_next(node);
1499 total_bytes += state->end - state->start + 1;
1500 if (total_bytes >= max_bytes)
1504 spin_unlock(&tree->lock);
1508 static noinline void __unlock_for_delalloc(struct inode *inode,
1509 struct page *locked_page,
1513 struct page *pages[16];
1514 unsigned long index = start >> PAGE_CACHE_SHIFT;
1515 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1516 unsigned long nr_pages = end_index - index + 1;
1519 if (index == locked_page->index && end_index == index)
1522 while (nr_pages > 0) {
1523 ret = find_get_pages_contig(inode->i_mapping, index,
1524 min_t(unsigned long, nr_pages,
1525 ARRAY_SIZE(pages)), pages);
1526 for (i = 0; i < ret; i++) {
1527 if (pages[i] != locked_page)
1528 unlock_page(pages[i]);
1529 page_cache_release(pages[i]);
1537 static noinline int lock_delalloc_pages(struct inode *inode,
1538 struct page *locked_page,
1542 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1543 unsigned long start_index = index;
1544 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1545 unsigned long pages_locked = 0;
1546 struct page *pages[16];
1547 unsigned long nrpages;
1551 /* the caller is responsible for locking the start index */
1552 if (index == locked_page->index && index == end_index)
1555 /* skip the page at the start index */
1556 nrpages = end_index - index + 1;
1557 while (nrpages > 0) {
1558 ret = find_get_pages_contig(inode->i_mapping, index,
1559 min_t(unsigned long,
1560 nrpages, ARRAY_SIZE(pages)), pages);
1565 /* now we have an array of pages, lock them all */
1566 for (i = 0; i < ret; i++) {
1568 * the caller is taking responsibility for
1571 if (pages[i] != locked_page) {
1572 lock_page(pages[i]);
1573 if (!PageDirty(pages[i]) ||
1574 pages[i]->mapping != inode->i_mapping) {
1576 unlock_page(pages[i]);
1577 page_cache_release(pages[i]);
1581 page_cache_release(pages[i]);
1590 if (ret && pages_locked) {
1591 __unlock_for_delalloc(inode, locked_page,
1593 ((u64)(start_index + pages_locked - 1)) <<
1600 * find a contiguous range of bytes in the file marked as delalloc, not
1601 * more than 'max_bytes'. start and end are used to return the range,
1603 * 1 is returned if we find something, 0 if nothing was in the tree
1605 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1606 struct extent_io_tree *tree,
1607 struct page *locked_page,
1608 u64 *start, u64 *end,
1614 struct extent_state *cached_state = NULL;
1619 /* step one, find a bunch of delalloc bytes starting at start */
1620 delalloc_start = *start;
1622 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1623 max_bytes, &cached_state);
1624 if (!found || delalloc_end <= *start) {
1625 *start = delalloc_start;
1626 *end = delalloc_end;
1627 free_extent_state(cached_state);
1632 * start comes from the offset of locked_page. We have to lock
1633 * pages in order, so we can't process delalloc bytes before
1636 if (delalloc_start < *start)
1637 delalloc_start = *start;
1640 * make sure to limit the number of pages we try to lock down
1643 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1644 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1646 /* step two, lock all the pages after the page that has start */
1647 ret = lock_delalloc_pages(inode, locked_page,
1648 delalloc_start, delalloc_end);
1649 if (ret == -EAGAIN) {
1650 /* some of the pages are gone, lets avoid looping by
1651 * shortening the size of the delalloc range we're searching
1653 free_extent_state(cached_state);
1655 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1656 max_bytes = PAGE_CACHE_SIZE - offset;
1664 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
1666 /* step three, lock the state bits for the whole range */
1667 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
1669 /* then test to make sure it is all still delalloc */
1670 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1671 EXTENT_DELALLOC, 1, cached_state);
1673 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1674 &cached_state, GFP_NOFS);
1675 __unlock_for_delalloc(inode, locked_page,
1676 delalloc_start, delalloc_end);
1680 free_extent_state(cached_state);
1681 *start = delalloc_start;
1682 *end = delalloc_end;
1687 int extent_clear_unlock_delalloc(struct inode *inode,
1688 struct extent_io_tree *tree,
1689 u64 start, u64 end, struct page *locked_page,
1693 struct page *pages[16];
1694 unsigned long index = start >> PAGE_CACHE_SHIFT;
1695 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1696 unsigned long nr_pages = end_index - index + 1;
1698 unsigned long clear_bits = 0;
1700 if (op & EXTENT_CLEAR_UNLOCK)
1701 clear_bits |= EXTENT_LOCKED;
1702 if (op & EXTENT_CLEAR_DIRTY)
1703 clear_bits |= EXTENT_DIRTY;
1705 if (op & EXTENT_CLEAR_DELALLOC)
1706 clear_bits |= EXTENT_DELALLOC;
1708 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1709 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1710 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1711 EXTENT_SET_PRIVATE2)))
1714 while (nr_pages > 0) {
1715 ret = find_get_pages_contig(inode->i_mapping, index,
1716 min_t(unsigned long,
1717 nr_pages, ARRAY_SIZE(pages)), pages);
1718 for (i = 0; i < ret; i++) {
1720 if (op & EXTENT_SET_PRIVATE2)
1721 SetPagePrivate2(pages[i]);
1723 if (pages[i] == locked_page) {
1724 page_cache_release(pages[i]);
1727 if (op & EXTENT_CLEAR_DIRTY)
1728 clear_page_dirty_for_io(pages[i]);
1729 if (op & EXTENT_SET_WRITEBACK)
1730 set_page_writeback(pages[i]);
1731 if (op & EXTENT_END_WRITEBACK)
1732 end_page_writeback(pages[i]);
1733 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1734 unlock_page(pages[i]);
1735 page_cache_release(pages[i]);
1745 * count the number of bytes in the tree that have a given bit(s)
1746 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1747 * cached. The total number found is returned.
1749 u64 count_range_bits(struct extent_io_tree *tree,
1750 u64 *start, u64 search_end, u64 max_bytes,
1751 unsigned long bits, int contig)
1753 struct rb_node *node;
1754 struct extent_state *state;
1755 u64 cur_start = *start;
1756 u64 total_bytes = 0;
1760 if (search_end <= cur_start) {
1765 spin_lock(&tree->lock);
1766 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1767 total_bytes = tree->dirty_bytes;
1771 * this search will find all the extents that end after
1774 node = tree_search(tree, cur_start);
1779 state = rb_entry(node, struct extent_state, rb_node);
1780 if (state->start > search_end)
1782 if (contig && found && state->start > last + 1)
1784 if (state->end >= cur_start && (state->state & bits) == bits) {
1785 total_bytes += min(search_end, state->end) + 1 -
1786 max(cur_start, state->start);
1787 if (total_bytes >= max_bytes)
1790 *start = max(cur_start, state->start);
1794 } else if (contig && found) {
1797 node = rb_next(node);
1802 spin_unlock(&tree->lock);
1807 * set the private field for a given byte offset in the tree. If there isn't
1808 * an extent_state there already, this does nothing.
1810 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1812 struct rb_node *node;
1813 struct extent_state *state;
1816 spin_lock(&tree->lock);
1818 * this search will find all the extents that end after
1821 node = tree_search(tree, start);
1826 state = rb_entry(node, struct extent_state, rb_node);
1827 if (state->start != start) {
1831 state->private = private;
1833 spin_unlock(&tree->lock);
1837 void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 csums[],
1840 struct rb_node *node;
1841 struct extent_state *state;
1843 spin_lock(&tree->lock);
1845 * this search will find all the extents that end after
1848 node = tree_search(tree, start);
1851 state = rb_entry(node, struct extent_state, rb_node);
1852 BUG_ON(state->start != start);
1855 state->private = *csums++;
1857 state = next_state(state);
1859 spin_unlock(&tree->lock);
1862 static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index)
1864 struct bio_vec *bvec = bio->bi_io_vec + bio_index;
1866 return page_offset(bvec->bv_page) + bvec->bv_offset;
1869 void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int bio_index,
1870 u32 csums[], int count)
1872 struct rb_node *node;
1873 struct extent_state *state = NULL;
1876 spin_lock(&tree->lock);
1878 start = __btrfs_get_bio_offset(bio, bio_index);
1879 if (state == NULL || state->start != start) {
1880 node = tree_search(tree, start);
1883 state = rb_entry(node, struct extent_state, rb_node);
1884 BUG_ON(state->start != start);
1886 state->private = *csums++;
1890 state = next_state(state);
1892 spin_unlock(&tree->lock);
1895 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1897 struct rb_node *node;
1898 struct extent_state *state;
1901 spin_lock(&tree->lock);
1903 * this search will find all the extents that end after
1906 node = tree_search(tree, start);
1911 state = rb_entry(node, struct extent_state, rb_node);
1912 if (state->start != start) {
1916 *private = state->private;
1918 spin_unlock(&tree->lock);
1923 * searches a range in the state tree for a given mask.
1924 * If 'filled' == 1, this returns 1 only if every extent in the tree
1925 * has the bits set. Otherwise, 1 is returned if any bit in the
1926 * range is found set.
1928 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1929 unsigned long bits, int filled, struct extent_state *cached)
1931 struct extent_state *state = NULL;
1932 struct rb_node *node;
1935 spin_lock(&tree->lock);
1936 if (cached && cached->tree && cached->start <= start &&
1937 cached->end > start)
1938 node = &cached->rb_node;
1940 node = tree_search(tree, start);
1941 while (node && start <= end) {
1942 state = rb_entry(node, struct extent_state, rb_node);
1944 if (filled && state->start > start) {
1949 if (state->start > end)
1952 if (state->state & bits) {
1956 } else if (filled) {
1961 if (state->end == (u64)-1)
1964 start = state->end + 1;
1967 node = rb_next(node);
1974 spin_unlock(&tree->lock);
1979 * helper function to set a given page up to date if all the
1980 * extents in the tree for that page are up to date
1982 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
1984 u64 start = page_offset(page);
1985 u64 end = start + PAGE_CACHE_SIZE - 1;
1986 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1987 SetPageUptodate(page);
1991 * When IO fails, either with EIO or csum verification fails, we
1992 * try other mirrors that might have a good copy of the data. This
1993 * io_failure_record is used to record state as we go through all the
1994 * mirrors. If another mirror has good data, the page is set up to date
1995 * and things continue. If a good mirror can't be found, the original
1996 * bio end_io callback is called to indicate things have failed.
1998 struct io_failure_record {
2003 unsigned long bio_flags;
2009 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
2014 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2016 set_state_private(failure_tree, rec->start, 0);
2017 ret = clear_extent_bits(failure_tree, rec->start,
2018 rec->start + rec->len - 1,
2019 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2023 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
2024 rec->start + rec->len - 1,
2025 EXTENT_DAMAGED, GFP_NOFS);
2033 static void repair_io_failure_callback(struct bio *bio, int err)
2035 complete(bio->bi_private);
2039 * this bypasses the standard btrfs submit functions deliberately, as
2040 * the standard behavior is to write all copies in a raid setup. here we only
2041 * want to write the one bad copy. so we do the mapping for ourselves and issue
2042 * submit_bio directly.
2043 * to avoid any synchronization issues, wait for the data after writing, which
2044 * actually prevents the read that triggered the error from finishing.
2045 * currently, there can be no more than two copies of every data bit. thus,
2046 * exactly one rewrite is required.
2048 int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
2049 u64 length, u64 logical, struct page *page,
2053 struct btrfs_device *dev;
2054 DECLARE_COMPLETION_ONSTACK(compl);
2057 struct btrfs_bio *bbio = NULL;
2058 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
2061 BUG_ON(!mirror_num);
2063 /* we can't repair anything in raid56 yet */
2064 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2067 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
2070 bio->bi_private = &compl;
2071 bio->bi_end_io = repair_io_failure_callback;
2073 map_length = length;
2075 ret = btrfs_map_block(fs_info, WRITE, logical,
2076 &map_length, &bbio, mirror_num);
2081 BUG_ON(mirror_num != bbio->mirror_num);
2082 sector = bbio->stripes[mirror_num-1].physical >> 9;
2083 bio->bi_sector = sector;
2084 dev = bbio->stripes[mirror_num-1].dev;
2086 if (!dev || !dev->bdev || !dev->writeable) {
2090 bio->bi_bdev = dev->bdev;
2091 bio_add_page(bio, page, length, start - page_offset(page));
2092 btrfsic_submit_bio(WRITE_SYNC, bio);
2093 wait_for_completion(&compl);
2095 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2096 /* try to remap that extent elsewhere? */
2098 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
2102 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
2103 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2104 start, rcu_str_deref(dev->name), sector);
2110 int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2113 u64 start = eb->start;
2114 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
2117 for (i = 0; i < num_pages; i++) {
2118 struct page *p = extent_buffer_page(eb, i);
2119 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
2120 start, p, mirror_num);
2123 start += PAGE_CACHE_SIZE;
2130 * each time an IO finishes, we do a fast check in the IO failure tree
2131 * to see if we need to process or clean up an io_failure_record
2133 static int clean_io_failure(u64 start, struct page *page)
2136 u64 private_failure;
2137 struct io_failure_record *failrec;
2138 struct btrfs_fs_info *fs_info;
2139 struct extent_state *state;
2143 struct inode *inode = page->mapping->host;
2146 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2147 (u64)-1, 1, EXTENT_DIRTY, 0);
2151 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2156 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2157 BUG_ON(!failrec->this_mirror);
2159 if (failrec->in_validation) {
2160 /* there was no real error, just free the record */
2161 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2167 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2168 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2171 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2173 if (state && state->start == failrec->start) {
2174 fs_info = BTRFS_I(inode)->root->fs_info;
2175 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2177 if (num_copies > 1) {
2178 ret = repair_io_failure(fs_info, start, failrec->len,
2179 failrec->logical, page,
2180 failrec->failed_mirror);
2188 ret = free_io_failure(inode, failrec, did_repair);
2194 * this is a generic handler for readpage errors (default
2195 * readpage_io_failed_hook). if other copies exist, read those and write back
2196 * good data to the failed position. does not investigate in remapping the
2197 * failed extent elsewhere, hoping the device will be smart enough to do this as
2201 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2202 u64 start, u64 end, int failed_mirror,
2203 struct extent_state *state)
2205 struct io_failure_record *failrec = NULL;
2207 struct extent_map *em;
2208 struct inode *inode = page->mapping->host;
2209 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2210 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2211 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2218 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2220 ret = get_state_private(failure_tree, start, &private);
2222 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2225 failrec->start = start;
2226 failrec->len = end - start + 1;
2227 failrec->this_mirror = 0;
2228 failrec->bio_flags = 0;
2229 failrec->in_validation = 0;
2231 read_lock(&em_tree->lock);
2232 em = lookup_extent_mapping(em_tree, start, failrec->len);
2234 read_unlock(&em_tree->lock);
2239 if (em->start > start || em->start + em->len < start) {
2240 free_extent_map(em);
2243 read_unlock(&em_tree->lock);
2249 logical = start - em->start;
2250 logical = em->block_start + logical;
2251 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2252 logical = em->block_start;
2253 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2254 extent_set_compress_type(&failrec->bio_flags,
2257 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2258 "len=%llu\n", logical, start, failrec->len);
2259 failrec->logical = logical;
2260 free_extent_map(em);
2262 /* set the bits in the private failure tree */
2263 ret = set_extent_bits(failure_tree, start, end,
2264 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2266 ret = set_state_private(failure_tree, start,
2267 (u64)(unsigned long)failrec);
2268 /* set the bits in the inode's tree */
2270 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2277 failrec = (struct io_failure_record *)(unsigned long)private;
2278 pr_debug("bio_readpage_error: (found) logical=%llu, "
2279 "start=%llu, len=%llu, validation=%d\n",
2280 failrec->logical, failrec->start, failrec->len,
2281 failrec->in_validation);
2283 * when data can be on disk more than twice, add to failrec here
2284 * (e.g. with a list for failed_mirror) to make
2285 * clean_io_failure() clean all those errors at once.
2288 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2289 failrec->logical, failrec->len);
2290 if (num_copies == 1) {
2292 * we only have a single copy of the data, so don't bother with
2293 * all the retry and error correction code that follows. no
2294 * matter what the error is, it is very likely to persist.
2296 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2297 "state=%p, num_copies=%d, next_mirror %d, "
2298 "failed_mirror %d\n", state, num_copies,
2299 failrec->this_mirror, failed_mirror);
2300 free_io_failure(inode, failrec, 0);
2305 spin_lock(&tree->lock);
2306 state = find_first_extent_bit_state(tree, failrec->start,
2308 if (state && state->start != failrec->start)
2310 spin_unlock(&tree->lock);
2314 * there are two premises:
2315 * a) deliver good data to the caller
2316 * b) correct the bad sectors on disk
2318 if (failed_bio->bi_vcnt > 1) {
2320 * to fulfill b), we need to know the exact failing sectors, as
2321 * we don't want to rewrite any more than the failed ones. thus,
2322 * we need separate read requests for the failed bio
2324 * if the following BUG_ON triggers, our validation request got
2325 * merged. we need separate requests for our algorithm to work.
2327 BUG_ON(failrec->in_validation);
2328 failrec->in_validation = 1;
2329 failrec->this_mirror = failed_mirror;
2330 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2333 * we're ready to fulfill a) and b) alongside. get a good copy
2334 * of the failed sector and if we succeed, we have setup
2335 * everything for repair_io_failure to do the rest for us.
2337 if (failrec->in_validation) {
2338 BUG_ON(failrec->this_mirror != failed_mirror);
2339 failrec->in_validation = 0;
2340 failrec->this_mirror = 0;
2342 failrec->failed_mirror = failed_mirror;
2343 failrec->this_mirror++;
2344 if (failrec->this_mirror == failed_mirror)
2345 failrec->this_mirror++;
2346 read_mode = READ_SYNC;
2349 if (!state || failrec->this_mirror > num_copies) {
2350 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2351 "next_mirror %d, failed_mirror %d\n", state,
2352 num_copies, failrec->this_mirror, failed_mirror);
2353 free_io_failure(inode, failrec, 0);
2357 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
2359 free_io_failure(inode, failrec, 0);
2362 bio->bi_private = state;
2363 bio->bi_end_io = failed_bio->bi_end_io;
2364 bio->bi_sector = failrec->logical >> 9;
2365 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2368 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2370 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2371 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2372 failrec->this_mirror, num_copies, failrec->in_validation);
2374 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2375 failrec->this_mirror,
2376 failrec->bio_flags, 0);
2380 /* lots and lots of room for performance fixes in the end_bio funcs */
2382 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2384 int uptodate = (err == 0);
2385 struct extent_io_tree *tree;
2388 tree = &BTRFS_I(page->mapping->host)->io_tree;
2390 if (tree->ops && tree->ops->writepage_end_io_hook) {
2391 ret = tree->ops->writepage_end_io_hook(page, start,
2392 end, NULL, uptodate);
2398 ClearPageUptodate(page);
2405 * after a writepage IO is done, we need to:
2406 * clear the uptodate bits on error
2407 * clear the writeback bits in the extent tree for this IO
2408 * end_page_writeback if the page has no more pending IO
2410 * Scheduling is not allowed, so the extent state tree is expected
2411 * to have one and only one object corresponding to this IO.
2413 static void end_bio_extent_writepage(struct bio *bio, int err)
2415 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2416 struct extent_io_tree *tree;
2421 struct page *page = bvec->bv_page;
2422 tree = &BTRFS_I(page->mapping->host)->io_tree;
2424 /* We always issue full-page reads, but if some block
2425 * in a page fails to read, blk_update_request() will
2426 * advance bv_offset and adjust bv_len to compensate.
2427 * Print a warning for nonzero offsets, and an error
2428 * if they don't add up to a full page. */
2429 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2430 printk("%s page write in btrfs with offset %u and length %u\n",
2431 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2432 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2433 bvec->bv_offset, bvec->bv_len);
2435 start = page_offset(page);
2436 end = start + bvec->bv_offset + bvec->bv_len - 1;
2438 if (--bvec >= bio->bi_io_vec)
2439 prefetchw(&bvec->bv_page->flags);
2441 if (end_extent_writepage(page, err, start, end))
2444 end_page_writeback(page);
2445 } while (bvec >= bio->bi_io_vec);
2451 * after a readpage IO is done, we need to:
2452 * clear the uptodate bits on error
2453 * set the uptodate bits if things worked
2454 * set the page up to date if all extents in the tree are uptodate
2455 * clear the lock bit in the extent tree
2456 * unlock the page if there are no other extents locked for it
2458 * Scheduling is not allowed, so the extent state tree is expected
2459 * to have one and only one object corresponding to this IO.
2461 static void end_bio_extent_readpage(struct bio *bio, int err)
2463 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2464 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2465 struct bio_vec *bvec = bio->bi_io_vec;
2466 struct extent_io_tree *tree;
2476 struct page *page = bvec->bv_page;
2477 struct extent_state *cached = NULL;
2478 struct extent_state *state;
2479 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
2481 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2482 "mirror=%lu\n", (u64)bio->bi_sector, err,
2483 io_bio->mirror_num);
2484 tree = &BTRFS_I(page->mapping->host)->io_tree;
2486 /* We always issue full-page reads, but if some block
2487 * in a page fails to read, blk_update_request() will
2488 * advance bv_offset and adjust bv_len to compensate.
2489 * Print a warning for nonzero offsets, and an error
2490 * if they don't add up to a full page. */
2491 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2492 printk("%s page read in btrfs with offset %u and length %u\n",
2493 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2494 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2495 bvec->bv_offset, bvec->bv_len);
2497 start = page_offset(page);
2498 end = start + bvec->bv_offset + bvec->bv_len - 1;
2500 if (++bvec <= bvec_end)
2501 prefetchw(&bvec->bv_page->flags);
2503 spin_lock(&tree->lock);
2504 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2505 if (state && state->start == start) {
2507 * take a reference on the state, unlock will drop
2510 cache_state(state, &cached);
2512 spin_unlock(&tree->lock);
2514 mirror = io_bio->mirror_num;
2515 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2516 ret = tree->ops->readpage_end_io_hook(page, start, end,
2521 clean_io_failure(start, page);
2524 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
2525 ret = tree->ops->readpage_io_failed_hook(page, mirror);
2527 test_bit(BIO_UPTODATE, &bio->bi_flags))
2529 } else if (!uptodate) {
2531 * The generic bio_readpage_error handles errors the
2532 * following way: If possible, new read requests are
2533 * created and submitted and will end up in
2534 * end_bio_extent_readpage as well (if we're lucky, not
2535 * in the !uptodate case). In that case it returns 0 and
2536 * we just go on with the next page in our bio. If it
2537 * can't handle the error it will return -EIO and we
2538 * remain responsible for that page.
2540 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
2543 test_bit(BIO_UPTODATE, &bio->bi_flags);
2546 uncache_state(&cached);
2551 if (uptodate && tree->track_uptodate) {
2552 set_extent_uptodate(tree, start, end, &cached,
2555 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2558 SetPageUptodate(page);
2560 ClearPageUptodate(page);
2564 } while (bvec <= bvec_end);
2570 * this allocates from the btrfs_bioset. We're returning a bio right now
2571 * but you can call btrfs_io_bio for the appropriate container_of magic
2574 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2579 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
2581 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2582 while (!bio && (nr_vecs /= 2)) {
2583 bio = bio_alloc_bioset(gfp_flags,
2584 nr_vecs, btrfs_bioset);
2590 bio->bi_bdev = bdev;
2591 bio->bi_sector = first_sector;
2596 struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2598 return bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2602 /* this also allocates from the btrfs_bioset */
2603 struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2605 return bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2609 static int __must_check submit_one_bio(int rw, struct bio *bio,
2610 int mirror_num, unsigned long bio_flags)
2613 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2614 struct page *page = bvec->bv_page;
2615 struct extent_io_tree *tree = bio->bi_private;
2618 start = page_offset(page) + bvec->bv_offset;
2620 bio->bi_private = NULL;
2624 if (tree->ops && tree->ops->submit_bio_hook)
2625 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2626 mirror_num, bio_flags, start);
2628 btrfsic_submit_bio(rw, bio);
2630 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2636 static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
2637 unsigned long offset, size_t size, struct bio *bio,
2638 unsigned long bio_flags)
2641 if (tree->ops && tree->ops->merge_bio_hook)
2642 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
2649 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2650 struct page *page, sector_t sector,
2651 size_t size, unsigned long offset,
2652 struct block_device *bdev,
2653 struct bio **bio_ret,
2654 unsigned long max_pages,
2655 bio_end_io_t end_io_func,
2657 unsigned long prev_bio_flags,
2658 unsigned long bio_flags)
2664 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2665 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2666 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2668 if (bio_ret && *bio_ret) {
2671 contig = bio->bi_sector == sector;
2673 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2676 if (prev_bio_flags != bio_flags || !contig ||
2677 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
2678 bio_add_page(bio, page, page_size, offset) < page_size) {
2679 ret = submit_one_bio(rw, bio, mirror_num,
2688 if (this_compressed)
2691 nr = bio_get_nr_vecs(bdev);
2693 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2697 bio_add_page(bio, page, page_size, offset);
2698 bio->bi_end_io = end_io_func;
2699 bio->bi_private = tree;
2704 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2709 static void attach_extent_buffer_page(struct extent_buffer *eb,
2712 if (!PagePrivate(page)) {
2713 SetPagePrivate(page);
2714 page_cache_get(page);
2715 set_page_private(page, (unsigned long)eb);
2717 WARN_ON(page->private != (unsigned long)eb);
2721 void set_page_extent_mapped(struct page *page)
2723 if (!PagePrivate(page)) {
2724 SetPagePrivate(page);
2725 page_cache_get(page);
2726 set_page_private(page, EXTENT_PAGE_PRIVATE);
2731 * basic readpage implementation. Locked extent state structs are inserted
2732 * into the tree that are removed when the IO is done (by the end_io
2734 * XXX JDM: This needs looking at to ensure proper page locking
2736 static int __extent_read_full_page(struct extent_io_tree *tree,
2738 get_extent_t *get_extent,
2739 struct bio **bio, int mirror_num,
2740 unsigned long *bio_flags, int rw)
2742 struct inode *inode = page->mapping->host;
2743 u64 start = page_offset(page);
2744 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2748 u64 last_byte = i_size_read(inode);
2752 struct extent_map *em;
2753 struct block_device *bdev;
2754 struct btrfs_ordered_extent *ordered;
2757 size_t pg_offset = 0;
2759 size_t disk_io_size;
2760 size_t blocksize = inode->i_sb->s_blocksize;
2761 unsigned long this_bio_flag = 0;
2763 set_page_extent_mapped(page);
2765 if (!PageUptodate(page)) {
2766 if (cleancache_get_page(page) == 0) {
2767 BUG_ON(blocksize != PAGE_SIZE);
2774 lock_extent(tree, start, end);
2775 ordered = btrfs_lookup_ordered_extent(inode, start);
2778 unlock_extent(tree, start, end);
2779 btrfs_start_ordered_extent(inode, ordered, 1);
2780 btrfs_put_ordered_extent(ordered);
2783 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2785 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2788 iosize = PAGE_CACHE_SIZE - zero_offset;
2789 userpage = kmap_atomic(page);
2790 memset(userpage + zero_offset, 0, iosize);
2791 flush_dcache_page(page);
2792 kunmap_atomic(userpage);
2795 while (cur <= end) {
2796 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2798 if (cur >= last_byte) {
2800 struct extent_state *cached = NULL;
2802 iosize = PAGE_CACHE_SIZE - pg_offset;
2803 userpage = kmap_atomic(page);
2804 memset(userpage + pg_offset, 0, iosize);
2805 flush_dcache_page(page);
2806 kunmap_atomic(userpage);
2807 set_extent_uptodate(tree, cur, cur + iosize - 1,
2809 unlock_extent_cached(tree, cur, cur + iosize - 1,
2813 em = get_extent(inode, page, pg_offset, cur,
2815 if (IS_ERR_OR_NULL(em)) {
2817 unlock_extent(tree, cur, end);
2820 extent_offset = cur - em->start;
2821 BUG_ON(extent_map_end(em) <= cur);
2824 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2825 this_bio_flag = EXTENT_BIO_COMPRESSED;
2826 extent_set_compress_type(&this_bio_flag,
2830 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2831 cur_end = min(extent_map_end(em) - 1, end);
2832 iosize = ALIGN(iosize, blocksize);
2833 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2834 disk_io_size = em->block_len;
2835 sector = em->block_start >> 9;
2837 sector = (em->block_start + extent_offset) >> 9;
2838 disk_io_size = iosize;
2841 block_start = em->block_start;
2842 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2843 block_start = EXTENT_MAP_HOLE;
2844 free_extent_map(em);
2847 /* we've found a hole, just zero and go on */
2848 if (block_start == EXTENT_MAP_HOLE) {
2850 struct extent_state *cached = NULL;
2852 userpage = kmap_atomic(page);
2853 memset(userpage + pg_offset, 0, iosize);
2854 flush_dcache_page(page);
2855 kunmap_atomic(userpage);
2857 set_extent_uptodate(tree, cur, cur + iosize - 1,
2859 unlock_extent_cached(tree, cur, cur + iosize - 1,
2862 pg_offset += iosize;
2865 /* the get_extent function already copied into the page */
2866 if (test_range_bit(tree, cur, cur_end,
2867 EXTENT_UPTODATE, 1, NULL)) {
2868 check_page_uptodate(tree, page);
2869 unlock_extent(tree, cur, cur + iosize - 1);
2871 pg_offset += iosize;
2874 /* we have an inline extent but it didn't get marked up
2875 * to date. Error out
2877 if (block_start == EXTENT_MAP_INLINE) {
2879 unlock_extent(tree, cur, cur + iosize - 1);
2881 pg_offset += iosize;
2886 ret = submit_extent_page(rw, tree, page,
2887 sector, disk_io_size, pg_offset,
2889 end_bio_extent_readpage, mirror_num,
2894 *bio_flags = this_bio_flag;
2897 unlock_extent(tree, cur, cur + iosize - 1);
2900 pg_offset += iosize;
2904 if (!PageError(page))
2905 SetPageUptodate(page);
2911 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2912 get_extent_t *get_extent, int mirror_num)
2914 struct bio *bio = NULL;
2915 unsigned long bio_flags = 0;
2918 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2921 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2925 static noinline void update_nr_written(struct page *page,
2926 struct writeback_control *wbc,
2927 unsigned long nr_written)
2929 wbc->nr_to_write -= nr_written;
2930 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2931 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2932 page->mapping->writeback_index = page->index + nr_written;
2936 * the writepage semantics are similar to regular writepage. extent
2937 * records are inserted to lock ranges in the tree, and as dirty areas
2938 * are found, they are marked writeback. Then the lock bits are removed
2939 * and the end_io handler clears the writeback ranges
2941 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2944 struct inode *inode = page->mapping->host;
2945 struct extent_page_data *epd = data;
2946 struct extent_io_tree *tree = epd->tree;
2947 u64 start = page_offset(page);
2949 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2953 u64 last_byte = i_size_read(inode);
2957 struct extent_state *cached_state = NULL;
2958 struct extent_map *em;
2959 struct block_device *bdev;
2962 size_t pg_offset = 0;
2964 loff_t i_size = i_size_read(inode);
2965 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2971 unsigned long nr_written = 0;
2972 bool fill_delalloc = true;
2974 if (wbc->sync_mode == WB_SYNC_ALL)
2975 write_flags = WRITE_SYNC;
2977 write_flags = WRITE;
2979 trace___extent_writepage(page, inode, wbc);
2981 WARN_ON(!PageLocked(page));
2983 ClearPageError(page);
2985 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2986 if (page->index > end_index ||
2987 (page->index == end_index && !pg_offset)) {
2988 page->mapping->a_ops->invalidatepage(page, 0);
2993 if (page->index == end_index) {
2996 userpage = kmap_atomic(page);
2997 memset(userpage + pg_offset, 0,
2998 PAGE_CACHE_SIZE - pg_offset);
2999 kunmap_atomic(userpage);
3000 flush_dcache_page(page);
3004 set_page_extent_mapped(page);
3006 if (!tree->ops || !tree->ops->fill_delalloc)
3007 fill_delalloc = false;
3009 delalloc_start = start;
3012 if (!epd->extent_locked && fill_delalloc) {
3013 u64 delalloc_to_write = 0;
3015 * make sure the wbc mapping index is at least updated
3018 update_nr_written(page, wbc, 0);
3020 while (delalloc_end < page_end) {
3021 nr_delalloc = find_lock_delalloc_range(inode, tree,
3026 if (nr_delalloc == 0) {
3027 delalloc_start = delalloc_end + 1;
3030 ret = tree->ops->fill_delalloc(inode, page,
3035 /* File system has been set read-only */
3041 * delalloc_end is already one less than the total
3042 * length, so we don't subtract one from
3045 delalloc_to_write += (delalloc_end - delalloc_start +
3048 delalloc_start = delalloc_end + 1;
3050 if (wbc->nr_to_write < delalloc_to_write) {
3053 if (delalloc_to_write < thresh * 2)
3054 thresh = delalloc_to_write;
3055 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3059 /* did the fill delalloc function already unlock and start
3065 * we've unlocked the page, so we can't update
3066 * the mapping's writeback index, just update
3069 wbc->nr_to_write -= nr_written;
3073 if (tree->ops && tree->ops->writepage_start_hook) {
3074 ret = tree->ops->writepage_start_hook(page, start,
3077 /* Fixup worker will requeue */
3079 wbc->pages_skipped++;
3081 redirty_page_for_writepage(wbc, page);
3082 update_nr_written(page, wbc, nr_written);
3090 * we don't want to touch the inode after unlocking the page,
3091 * so we update the mapping writeback index now
3093 update_nr_written(page, wbc, nr_written + 1);
3096 if (last_byte <= start) {
3097 if (tree->ops && tree->ops->writepage_end_io_hook)
3098 tree->ops->writepage_end_io_hook(page, start,
3103 blocksize = inode->i_sb->s_blocksize;
3105 while (cur <= end) {
3106 if (cur >= last_byte) {
3107 if (tree->ops && tree->ops->writepage_end_io_hook)
3108 tree->ops->writepage_end_io_hook(page, cur,
3112 em = epd->get_extent(inode, page, pg_offset, cur,
3114 if (IS_ERR_OR_NULL(em)) {
3119 extent_offset = cur - em->start;
3120 BUG_ON(extent_map_end(em) <= cur);
3122 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3123 iosize = ALIGN(iosize, blocksize);
3124 sector = (em->block_start + extent_offset) >> 9;
3126 block_start = em->block_start;
3127 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3128 free_extent_map(em);
3132 * compressed and inline extents are written through other
3135 if (compressed || block_start == EXTENT_MAP_HOLE ||
3136 block_start == EXTENT_MAP_INLINE) {
3138 * end_io notification does not happen here for
3139 * compressed extents
3141 if (!compressed && tree->ops &&
3142 tree->ops->writepage_end_io_hook)
3143 tree->ops->writepage_end_io_hook(page, cur,
3146 else if (compressed) {
3147 /* we don't want to end_page_writeback on
3148 * a compressed extent. this happens
3155 pg_offset += iosize;
3158 /* leave this out until we have a page_mkwrite call */
3159 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
3160 EXTENT_DIRTY, 0, NULL)) {
3162 pg_offset += iosize;
3166 if (tree->ops && tree->ops->writepage_io_hook) {
3167 ret = tree->ops->writepage_io_hook(page, cur,
3175 unsigned long max_nr = end_index + 1;
3177 set_range_writeback(tree, cur, cur + iosize - 1);
3178 if (!PageWriteback(page)) {
3179 printk(KERN_ERR "btrfs warning page %lu not "
3180 "writeback, cur %llu end %llu\n",
3181 page->index, (unsigned long long)cur,
3182 (unsigned long long)end);
3185 ret = submit_extent_page(write_flags, tree, page,
3186 sector, iosize, pg_offset,
3187 bdev, &epd->bio, max_nr,
3188 end_bio_extent_writepage,
3194 pg_offset += iosize;
3199 /* make sure the mapping tag for page dirty gets cleared */
3200 set_page_writeback(page);
3201 end_page_writeback(page);
3207 /* drop our reference on any cached states */
3208 free_extent_state(cached_state);
3212 static int eb_wait(void *word)
3218 void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3220 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3221 TASK_UNINTERRUPTIBLE);
3224 static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3225 struct btrfs_fs_info *fs_info,
3226 struct extent_page_data *epd)
3228 unsigned long i, num_pages;
3232 if (!btrfs_try_tree_write_lock(eb)) {
3234 flush_write_bio(epd);
3235 btrfs_tree_lock(eb);
3238 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3239 btrfs_tree_unlock(eb);
3243 flush_write_bio(epd);
3247 wait_on_extent_buffer_writeback(eb);
3248 btrfs_tree_lock(eb);
3249 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3251 btrfs_tree_unlock(eb);
3256 * We need to do this to prevent races in people who check if the eb is
3257 * under IO since we can end up having no IO bits set for a short period
3260 spin_lock(&eb->refs_lock);
3261 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3262 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3263 spin_unlock(&eb->refs_lock);
3264 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3265 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3267 fs_info->dirty_metadata_batch);
3270 spin_unlock(&eb->refs_lock);
3273 btrfs_tree_unlock(eb);
3278 num_pages = num_extent_pages(eb->start, eb->len);
3279 for (i = 0; i < num_pages; i++) {
3280 struct page *p = extent_buffer_page(eb, i);
3282 if (!trylock_page(p)) {
3284 flush_write_bio(epd);
3294 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3296 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3297 smp_mb__after_clear_bit();
3298 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3301 static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3303 int uptodate = err == 0;
3304 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3305 struct extent_buffer *eb;
3309 struct page *page = bvec->bv_page;
3312 eb = (struct extent_buffer *)page->private;
3314 done = atomic_dec_and_test(&eb->io_pages);
3316 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3317 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3318 ClearPageUptodate(page);
3322 end_page_writeback(page);
3327 end_extent_buffer_writeback(eb);
3328 } while (bvec >= bio->bi_io_vec);
3334 static int write_one_eb(struct extent_buffer *eb,
3335 struct btrfs_fs_info *fs_info,
3336 struct writeback_control *wbc,
3337 struct extent_page_data *epd)
3339 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3340 u64 offset = eb->start;
3341 unsigned long i, num_pages;
3342 unsigned long bio_flags = 0;
3343 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
3346 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3347 num_pages = num_extent_pages(eb->start, eb->len);
3348 atomic_set(&eb->io_pages, num_pages);
3349 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3350 bio_flags = EXTENT_BIO_TREE_LOG;
3352 for (i = 0; i < num_pages; i++) {
3353 struct page *p = extent_buffer_page(eb, i);
3355 clear_page_dirty_for_io(p);
3356 set_page_writeback(p);
3357 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3358 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3359 -1, end_bio_extent_buffer_writepage,
3360 0, epd->bio_flags, bio_flags);
3361 epd->bio_flags = bio_flags;
3363 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3365 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3366 end_extent_buffer_writeback(eb);
3370 offset += PAGE_CACHE_SIZE;
3371 update_nr_written(p, wbc, 1);
3375 if (unlikely(ret)) {
3376 for (; i < num_pages; i++) {
3377 struct page *p = extent_buffer_page(eb, i);
3385 int btree_write_cache_pages(struct address_space *mapping,
3386 struct writeback_control *wbc)
3388 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3389 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3390 struct extent_buffer *eb, *prev_eb = NULL;
3391 struct extent_page_data epd = {
3395 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3400 int nr_to_write_done = 0;
3401 struct pagevec pvec;
3404 pgoff_t end; /* Inclusive */
3408 pagevec_init(&pvec, 0);
3409 if (wbc->range_cyclic) {
3410 index = mapping->writeback_index; /* Start from prev offset */
3413 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3414 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3417 if (wbc->sync_mode == WB_SYNC_ALL)
3418 tag = PAGECACHE_TAG_TOWRITE;
3420 tag = PAGECACHE_TAG_DIRTY;
3422 if (wbc->sync_mode == WB_SYNC_ALL)
3423 tag_pages_for_writeback(mapping, index, end);
3424 while (!done && !nr_to_write_done && (index <= end) &&
3425 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3426 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3430 for (i = 0; i < nr_pages; i++) {
3431 struct page *page = pvec.pages[i];
3433 if (!PagePrivate(page))
3436 if (!wbc->range_cyclic && page->index > end) {
3441 spin_lock(&mapping->private_lock);
3442 if (!PagePrivate(page)) {
3443 spin_unlock(&mapping->private_lock);
3447 eb = (struct extent_buffer *)page->private;
3450 * Shouldn't happen and normally this would be a BUG_ON
3451 * but no sense in crashing the users box for something
3452 * we can survive anyway.
3455 spin_unlock(&mapping->private_lock);
3460 if (eb == prev_eb) {
3461 spin_unlock(&mapping->private_lock);
3465 ret = atomic_inc_not_zero(&eb->refs);
3466 spin_unlock(&mapping->private_lock);
3471 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3473 free_extent_buffer(eb);
3477 ret = write_one_eb(eb, fs_info, wbc, &epd);
3480 free_extent_buffer(eb);
3483 free_extent_buffer(eb);
3486 * the filesystem may choose to bump up nr_to_write.
3487 * We have to make sure to honor the new nr_to_write
3490 nr_to_write_done = wbc->nr_to_write <= 0;
3492 pagevec_release(&pvec);
3495 if (!scanned && !done) {
3497 * We hit the last page and there is more work to be done: wrap
3498 * back to the start of the file
3504 flush_write_bio(&epd);
3509 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3510 * @mapping: address space structure to write
3511 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3512 * @writepage: function called for each page
3513 * @data: data passed to writepage function
3515 * If a page is already under I/O, write_cache_pages() skips it, even
3516 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3517 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3518 * and msync() need to guarantee that all the data which was dirty at the time
3519 * the call was made get new I/O started against them. If wbc->sync_mode is
3520 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3521 * existing IO to complete.
3523 static int extent_write_cache_pages(struct extent_io_tree *tree,
3524 struct address_space *mapping,
3525 struct writeback_control *wbc,
3526 writepage_t writepage, void *data,
3527 void (*flush_fn)(void *))
3529 struct inode *inode = mapping->host;
3532 int nr_to_write_done = 0;
3533 struct pagevec pvec;
3536 pgoff_t end; /* Inclusive */
3541 * We have to hold onto the inode so that ordered extents can do their
3542 * work when the IO finishes. The alternative to this is failing to add
3543 * an ordered extent if the igrab() fails there and that is a huge pain
3544 * to deal with, so instead just hold onto the inode throughout the
3545 * writepages operation. If it fails here we are freeing up the inode
3546 * anyway and we'd rather not waste our time writing out stuff that is
3547 * going to be truncated anyway.
3552 pagevec_init(&pvec, 0);
3553 if (wbc->range_cyclic) {
3554 index = mapping->writeback_index; /* Start from prev offset */
3557 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3558 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3561 if (wbc->sync_mode == WB_SYNC_ALL)
3562 tag = PAGECACHE_TAG_TOWRITE;
3564 tag = PAGECACHE_TAG_DIRTY;
3566 if (wbc->sync_mode == WB_SYNC_ALL)
3567 tag_pages_for_writeback(mapping, index, end);
3568 while (!done && !nr_to_write_done && (index <= end) &&
3569 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3570 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3574 for (i = 0; i < nr_pages; i++) {
3575 struct page *page = pvec.pages[i];
3578 * At this point we hold neither mapping->tree_lock nor
3579 * lock on the page itself: the page may be truncated or
3580 * invalidated (changing page->mapping to NULL), or even
3581 * swizzled back from swapper_space to tmpfs file
3584 if (!trylock_page(page)) {
3589 if (unlikely(page->mapping != mapping)) {
3594 if (!wbc->range_cyclic && page->index > end) {
3600 if (wbc->sync_mode != WB_SYNC_NONE) {
3601 if (PageWriteback(page))
3603 wait_on_page_writeback(page);
3606 if (PageWriteback(page) ||
3607 !clear_page_dirty_for_io(page)) {
3612 ret = (*writepage)(page, wbc, data);
3614 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3622 * the filesystem may choose to bump up nr_to_write.
3623 * We have to make sure to honor the new nr_to_write
3626 nr_to_write_done = wbc->nr_to_write <= 0;
3628 pagevec_release(&pvec);
3631 if (!scanned && !done) {
3633 * We hit the last page and there is more work to be done: wrap
3634 * back to the start of the file
3640 btrfs_add_delayed_iput(inode);
3644 static void flush_epd_write_bio(struct extent_page_data *epd)
3653 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
3654 BUG_ON(ret < 0); /* -ENOMEM */
3659 static noinline void flush_write_bio(void *data)
3661 struct extent_page_data *epd = data;
3662 flush_epd_write_bio(epd);
3665 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3666 get_extent_t *get_extent,
3667 struct writeback_control *wbc)
3670 struct extent_page_data epd = {
3673 .get_extent = get_extent,
3675 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3679 ret = __extent_writepage(page, wbc, &epd);
3681 flush_epd_write_bio(&epd);
3685 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3686 u64 start, u64 end, get_extent_t *get_extent,
3690 struct address_space *mapping = inode->i_mapping;
3692 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3695 struct extent_page_data epd = {
3698 .get_extent = get_extent,
3700 .sync_io = mode == WB_SYNC_ALL,
3703 struct writeback_control wbc_writepages = {
3705 .nr_to_write = nr_pages * 2,
3706 .range_start = start,
3707 .range_end = end + 1,
3710 while (start <= end) {
3711 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3712 if (clear_page_dirty_for_io(page))
3713 ret = __extent_writepage(page, &wbc_writepages, &epd);
3715 if (tree->ops && tree->ops->writepage_end_io_hook)
3716 tree->ops->writepage_end_io_hook(page, start,
3717 start + PAGE_CACHE_SIZE - 1,
3721 page_cache_release(page);
3722 start += PAGE_CACHE_SIZE;
3725 flush_epd_write_bio(&epd);
3729 int extent_writepages(struct extent_io_tree *tree,
3730 struct address_space *mapping,
3731 get_extent_t *get_extent,
3732 struct writeback_control *wbc)
3735 struct extent_page_data epd = {
3738 .get_extent = get_extent,
3740 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3744 ret = extent_write_cache_pages(tree, mapping, wbc,
3745 __extent_writepage, &epd,
3747 flush_epd_write_bio(&epd);
3751 int extent_readpages(struct extent_io_tree *tree,
3752 struct address_space *mapping,
3753 struct list_head *pages, unsigned nr_pages,
3754 get_extent_t get_extent)
3756 struct bio *bio = NULL;
3758 unsigned long bio_flags = 0;
3759 struct page *pagepool[16];
3764 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3765 page = list_entry(pages->prev, struct page, lru);
3767 prefetchw(&page->flags);
3768 list_del(&page->lru);
3769 if (add_to_page_cache_lru(page, mapping,
3770 page->index, GFP_NOFS)) {
3771 page_cache_release(page);
3775 pagepool[nr++] = page;
3776 if (nr < ARRAY_SIZE(pagepool))
3778 for (i = 0; i < nr; i++) {
3779 __extent_read_full_page(tree, pagepool[i], get_extent,
3780 &bio, 0, &bio_flags, READ);
3781 page_cache_release(pagepool[i]);
3785 for (i = 0; i < nr; i++) {
3786 __extent_read_full_page(tree, pagepool[i], get_extent,
3787 &bio, 0, &bio_flags, READ);
3788 page_cache_release(pagepool[i]);
3791 BUG_ON(!list_empty(pages));
3793 return submit_one_bio(READ, bio, 0, bio_flags);
3798 * basic invalidatepage code, this waits on any locked or writeback
3799 * ranges corresponding to the page, and then deletes any extent state
3800 * records from the tree
3802 int extent_invalidatepage(struct extent_io_tree *tree,
3803 struct page *page, unsigned long offset)
3805 struct extent_state *cached_state = NULL;
3806 u64 start = page_offset(page);
3807 u64 end = start + PAGE_CACHE_SIZE - 1;
3808 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3810 start += ALIGN(offset, blocksize);
3814 lock_extent_bits(tree, start, end, 0, &cached_state);
3815 wait_on_page_writeback(page);
3816 clear_extent_bit(tree, start, end,
3817 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3818 EXTENT_DO_ACCOUNTING,
3819 1, 1, &cached_state, GFP_NOFS);
3824 * a helper for releasepage, this tests for areas of the page that
3825 * are locked or under IO and drops the related state bits if it is safe
3828 static int try_release_extent_state(struct extent_map_tree *map,
3829 struct extent_io_tree *tree,
3830 struct page *page, gfp_t mask)
3832 u64 start = page_offset(page);
3833 u64 end = start + PAGE_CACHE_SIZE - 1;
3836 if (test_range_bit(tree, start, end,
3837 EXTENT_IOBITS, 0, NULL))
3840 if ((mask & GFP_NOFS) == GFP_NOFS)
3843 * at this point we can safely clear everything except the
3844 * locked bit and the nodatasum bit
3846 ret = clear_extent_bit(tree, start, end,
3847 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3850 /* if clear_extent_bit failed for enomem reasons,
3851 * we can't allow the release to continue.
3862 * a helper for releasepage. As long as there are no locked extents
3863 * in the range corresponding to the page, both state records and extent
3864 * map records are removed
3866 int try_release_extent_mapping(struct extent_map_tree *map,
3867 struct extent_io_tree *tree, struct page *page,
3870 struct extent_map *em;
3871 u64 start = page_offset(page);
3872 u64 end = start + PAGE_CACHE_SIZE - 1;
3874 if ((mask & __GFP_WAIT) &&
3875 page->mapping->host->i_size > 16 * 1024 * 1024) {
3877 while (start <= end) {
3878 len = end - start + 1;
3879 write_lock(&map->lock);
3880 em = lookup_extent_mapping(map, start, len);
3882 write_unlock(&map->lock);
3885 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3886 em->start != start) {
3887 write_unlock(&map->lock);
3888 free_extent_map(em);
3891 if (!test_range_bit(tree, em->start,
3892 extent_map_end(em) - 1,
3893 EXTENT_LOCKED | EXTENT_WRITEBACK,
3895 remove_extent_mapping(map, em);
3896 /* once for the rb tree */
3897 free_extent_map(em);
3899 start = extent_map_end(em);
3900 write_unlock(&map->lock);
3903 free_extent_map(em);
3906 return try_release_extent_state(map, tree, page, mask);
3910 * helper function for fiemap, which doesn't want to see any holes.
3911 * This maps until we find something past 'last'
3913 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3916 get_extent_t *get_extent)
3918 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3919 struct extent_map *em;
3926 len = last - offset;
3929 len = ALIGN(len, sectorsize);
3930 em = get_extent(inode, NULL, 0, offset, len, 0);
3931 if (IS_ERR_OR_NULL(em))
3934 /* if this isn't a hole return it */
3935 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3936 em->block_start != EXTENT_MAP_HOLE) {
3940 /* this is a hole, advance to the next extent */
3941 offset = extent_map_end(em);
3942 free_extent_map(em);
3949 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3950 __u64 start, __u64 len, get_extent_t *get_extent)
3954 u64 max = start + len;
3958 u64 last_for_get_extent = 0;
3960 u64 isize = i_size_read(inode);
3961 struct btrfs_key found_key;
3962 struct extent_map *em = NULL;
3963 struct extent_state *cached_state = NULL;
3964 struct btrfs_path *path;
3965 struct btrfs_file_extent_item *item;
3970 unsigned long emflags;
3975 path = btrfs_alloc_path();
3978 path->leave_spinning = 1;
3980 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3981 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3984 * lookup the last file extent. We're not using i_size here
3985 * because there might be preallocation past i_size
3987 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3988 path, btrfs_ino(inode), -1, 0);
3990 btrfs_free_path(path);
3995 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3996 struct btrfs_file_extent_item);
3997 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3998 found_type = btrfs_key_type(&found_key);
4000 /* No extents, but there might be delalloc bits */
4001 if (found_key.objectid != btrfs_ino(inode) ||
4002 found_type != BTRFS_EXTENT_DATA_KEY) {
4003 /* have to trust i_size as the end */
4005 last_for_get_extent = isize;
4008 * remember the start of the last extent. There are a
4009 * bunch of different factors that go into the length of the
4010 * extent, so its much less complex to remember where it started
4012 last = found_key.offset;
4013 last_for_get_extent = last + 1;
4015 btrfs_free_path(path);
4018 * we might have some extents allocated but more delalloc past those
4019 * extents. so, we trust isize unless the start of the last extent is
4024 last_for_get_extent = isize;
4027 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
4030 em = get_extent_skip_holes(inode, start, last_for_get_extent,
4040 u64 offset_in_extent;
4042 /* break if the extent we found is outside the range */
4043 if (em->start >= max || extent_map_end(em) < off)
4047 * get_extent may return an extent that starts before our
4048 * requested range. We have to make sure the ranges
4049 * we return to fiemap always move forward and don't
4050 * overlap, so adjust the offsets here
4052 em_start = max(em->start, off);
4055 * record the offset from the start of the extent
4056 * for adjusting the disk offset below
4058 offset_in_extent = em_start - em->start;
4059 em_end = extent_map_end(em);
4060 em_len = em_end - em_start;
4061 emflags = em->flags;
4066 * bump off for our next call to get_extent
4068 off = extent_map_end(em);
4072 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
4074 flags |= FIEMAP_EXTENT_LAST;
4075 } else if (em->block_start == EXTENT_MAP_INLINE) {
4076 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4077 FIEMAP_EXTENT_NOT_ALIGNED);
4078 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
4079 flags |= (FIEMAP_EXTENT_DELALLOC |
4080 FIEMAP_EXTENT_UNKNOWN);
4082 disko = em->block_start + offset_in_extent;
4084 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4085 flags |= FIEMAP_EXTENT_ENCODED;
4087 free_extent_map(em);
4089 if ((em_start >= last) || em_len == (u64)-1 ||
4090 (last == (u64)-1 && isize <= em_end)) {
4091 flags |= FIEMAP_EXTENT_LAST;
4095 /* now scan forward to see if this is really the last extent. */
4096 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4103 flags |= FIEMAP_EXTENT_LAST;
4106 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4112 free_extent_map(em);
4114 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
4115 &cached_state, GFP_NOFS);
4119 static void __free_extent_buffer(struct extent_buffer *eb)
4121 btrfs_leak_debug_del(&eb->leak_list);
4122 kmem_cache_free(extent_buffer_cache, eb);
4125 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4130 struct extent_buffer *eb = NULL;
4132 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
4139 rwlock_init(&eb->lock);
4140 atomic_set(&eb->write_locks, 0);
4141 atomic_set(&eb->read_locks, 0);
4142 atomic_set(&eb->blocking_readers, 0);
4143 atomic_set(&eb->blocking_writers, 0);
4144 atomic_set(&eb->spinning_readers, 0);
4145 atomic_set(&eb->spinning_writers, 0);
4146 eb->lock_nested = 0;
4147 init_waitqueue_head(&eb->write_lock_wq);
4148 init_waitqueue_head(&eb->read_lock_wq);
4150 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4152 spin_lock_init(&eb->refs_lock);
4153 atomic_set(&eb->refs, 1);
4154 atomic_set(&eb->io_pages, 0);
4157 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4159 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4160 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4161 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4166 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4170 struct extent_buffer *new;
4171 unsigned long num_pages = num_extent_pages(src->start, src->len);
4173 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4177 for (i = 0; i < num_pages; i++) {
4178 p = alloc_page(GFP_ATOMIC);
4180 attach_extent_buffer_page(new, p);
4181 WARN_ON(PageDirty(p));
4186 copy_extent_buffer(new, src, 0, 0, src->len);
4187 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4188 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4193 struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4195 struct extent_buffer *eb;
4196 unsigned long num_pages = num_extent_pages(0, len);
4199 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4203 for (i = 0; i < num_pages; i++) {
4204 eb->pages[i] = alloc_page(GFP_ATOMIC);
4208 set_extent_buffer_uptodate(eb);
4209 btrfs_set_header_nritems(eb, 0);
4210 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4215 __free_page(eb->pages[i - 1]);
4216 __free_extent_buffer(eb);
4220 static int extent_buffer_under_io(struct extent_buffer *eb)
4222 return (atomic_read(&eb->io_pages) ||
4223 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4224 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4228 * Helper for releasing extent buffer page.
4230 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4231 unsigned long start_idx)
4233 unsigned long index;
4234 unsigned long num_pages;
4236 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4238 BUG_ON(extent_buffer_under_io(eb));
4240 num_pages = num_extent_pages(eb->start, eb->len);
4241 index = start_idx + num_pages;
4242 if (start_idx >= index)
4247 page = extent_buffer_page(eb, index);
4248 if (page && mapped) {
4249 spin_lock(&page->mapping->private_lock);
4251 * We do this since we'll remove the pages after we've
4252 * removed the eb from the radix tree, so we could race
4253 * and have this page now attached to the new eb. So
4254 * only clear page_private if it's still connected to
4257 if (PagePrivate(page) &&
4258 page->private == (unsigned long)eb) {
4259 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4260 BUG_ON(PageDirty(page));
4261 BUG_ON(PageWriteback(page));
4263 * We need to make sure we haven't be attached
4266 ClearPagePrivate(page);
4267 set_page_private(page, 0);
4268 /* One for the page private */
4269 page_cache_release(page);
4271 spin_unlock(&page->mapping->private_lock);
4275 /* One for when we alloced the page */
4276 page_cache_release(page);
4278 } while (index != start_idx);
4282 * Helper for releasing the extent buffer.
4284 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4286 btrfs_release_extent_buffer_page(eb, 0);
4287 __free_extent_buffer(eb);
4290 static void check_buffer_tree_ref(struct extent_buffer *eb)
4293 /* the ref bit is tricky. We have to make sure it is set
4294 * if we have the buffer dirty. Otherwise the
4295 * code to free a buffer can end up dropping a dirty
4298 * Once the ref bit is set, it won't go away while the
4299 * buffer is dirty or in writeback, and it also won't
4300 * go away while we have the reference count on the
4303 * We can't just set the ref bit without bumping the
4304 * ref on the eb because free_extent_buffer might
4305 * see the ref bit and try to clear it. If this happens
4306 * free_extent_buffer might end up dropping our original
4307 * ref by mistake and freeing the page before we are able
4308 * to add one more ref.
4310 * So bump the ref count first, then set the bit. If someone
4311 * beat us to it, drop the ref we added.
4313 refs = atomic_read(&eb->refs);
4314 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4317 spin_lock(&eb->refs_lock);
4318 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4319 atomic_inc(&eb->refs);
4320 spin_unlock(&eb->refs_lock);
4323 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4325 unsigned long num_pages, i;
4327 check_buffer_tree_ref(eb);
4329 num_pages = num_extent_pages(eb->start, eb->len);
4330 for (i = 0; i < num_pages; i++) {
4331 struct page *p = extent_buffer_page(eb, i);
4332 mark_page_accessed(p);
4336 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
4337 u64 start, unsigned long len)
4339 unsigned long num_pages = num_extent_pages(start, len);
4341 unsigned long index = start >> PAGE_CACHE_SHIFT;
4342 struct extent_buffer *eb;
4343 struct extent_buffer *exists = NULL;
4345 struct address_space *mapping = tree->mapping;
4350 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4351 if (eb && atomic_inc_not_zero(&eb->refs)) {
4353 mark_extent_buffer_accessed(eb);
4358 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
4362 for (i = 0; i < num_pages; i++, index++) {
4363 p = find_or_create_page(mapping, index, GFP_NOFS);
4367 spin_lock(&mapping->private_lock);
4368 if (PagePrivate(p)) {
4370 * We could have already allocated an eb for this page
4371 * and attached one so lets see if we can get a ref on
4372 * the existing eb, and if we can we know it's good and
4373 * we can just return that one, else we know we can just
4374 * overwrite page->private.
4376 exists = (struct extent_buffer *)p->private;
4377 if (atomic_inc_not_zero(&exists->refs)) {
4378 spin_unlock(&mapping->private_lock);
4380 page_cache_release(p);
4381 mark_extent_buffer_accessed(exists);
4386 * Do this so attach doesn't complain and we need to
4387 * drop the ref the old guy had.
4389 ClearPagePrivate(p);
4390 WARN_ON(PageDirty(p));
4391 page_cache_release(p);
4393 attach_extent_buffer_page(eb, p);
4394 spin_unlock(&mapping->private_lock);
4395 WARN_ON(PageDirty(p));
4396 mark_page_accessed(p);
4398 if (!PageUptodate(p))
4402 * see below about how we avoid a nasty race with release page
4403 * and why we unlock later
4407 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4409 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4413 spin_lock(&tree->buffer_lock);
4414 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4415 if (ret == -EEXIST) {
4416 exists = radix_tree_lookup(&tree->buffer,
4417 start >> PAGE_CACHE_SHIFT);
4418 if (!atomic_inc_not_zero(&exists->refs)) {
4419 spin_unlock(&tree->buffer_lock);
4420 radix_tree_preload_end();
4424 spin_unlock(&tree->buffer_lock);
4425 radix_tree_preload_end();
4426 mark_extent_buffer_accessed(exists);
4429 /* add one reference for the tree */
4430 check_buffer_tree_ref(eb);
4431 spin_unlock(&tree->buffer_lock);
4432 radix_tree_preload_end();
4435 * there is a race where release page may have
4436 * tried to find this extent buffer in the radix
4437 * but failed. It will tell the VM it is safe to
4438 * reclaim the, and it will clear the page private bit.
4439 * We must make sure to set the page private bit properly
4440 * after the extent buffer is in the radix tree so
4441 * it doesn't get lost
4443 SetPageChecked(eb->pages[0]);
4444 for (i = 1; i < num_pages; i++) {
4445 p = extent_buffer_page(eb, i);
4446 ClearPageChecked(p);
4449 unlock_page(eb->pages[0]);
4453 for (i = 0; i < num_pages; i++) {
4455 unlock_page(eb->pages[i]);
4458 WARN_ON(!atomic_dec_and_test(&eb->refs));
4459 btrfs_release_extent_buffer(eb);
4463 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
4464 u64 start, unsigned long len)
4466 struct extent_buffer *eb;
4469 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4470 if (eb && atomic_inc_not_zero(&eb->refs)) {
4472 mark_extent_buffer_accessed(eb);
4480 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4482 struct extent_buffer *eb =
4483 container_of(head, struct extent_buffer, rcu_head);
4485 __free_extent_buffer(eb);
4488 /* Expects to have eb->eb_lock already held */
4489 static int release_extent_buffer(struct extent_buffer *eb)
4491 WARN_ON(atomic_read(&eb->refs) == 0);
4492 if (atomic_dec_and_test(&eb->refs)) {
4493 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4494 spin_unlock(&eb->refs_lock);
4496 struct extent_io_tree *tree = eb->tree;
4498 spin_unlock(&eb->refs_lock);
4500 spin_lock(&tree->buffer_lock);
4501 radix_tree_delete(&tree->buffer,
4502 eb->start >> PAGE_CACHE_SHIFT);
4503 spin_unlock(&tree->buffer_lock);
4506 /* Should be safe to release our pages at this point */
4507 btrfs_release_extent_buffer_page(eb, 0);
4508 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4511 spin_unlock(&eb->refs_lock);
4516 void free_extent_buffer(struct extent_buffer *eb)
4524 refs = atomic_read(&eb->refs);
4527 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4532 spin_lock(&eb->refs_lock);
4533 if (atomic_read(&eb->refs) == 2 &&
4534 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4535 atomic_dec(&eb->refs);
4537 if (atomic_read(&eb->refs) == 2 &&
4538 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
4539 !extent_buffer_under_io(eb) &&
4540 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4541 atomic_dec(&eb->refs);
4544 * I know this is terrible, but it's temporary until we stop tracking
4545 * the uptodate bits and such for the extent buffers.
4547 release_extent_buffer(eb);
4550 void free_extent_buffer_stale(struct extent_buffer *eb)
4555 spin_lock(&eb->refs_lock);
4556 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4558 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
4559 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4560 atomic_dec(&eb->refs);
4561 release_extent_buffer(eb);
4564 void clear_extent_buffer_dirty(struct extent_buffer *eb)
4567 unsigned long num_pages;
4570 num_pages = num_extent_pages(eb->start, eb->len);
4572 for (i = 0; i < num_pages; i++) {
4573 page = extent_buffer_page(eb, i);
4574 if (!PageDirty(page))
4578 WARN_ON(!PagePrivate(page));
4580 clear_page_dirty_for_io(page);
4581 spin_lock_irq(&page->mapping->tree_lock);
4582 if (!PageDirty(page)) {
4583 radix_tree_tag_clear(&page->mapping->page_tree,
4585 PAGECACHE_TAG_DIRTY);
4587 spin_unlock_irq(&page->mapping->tree_lock);
4588 ClearPageError(page);
4591 WARN_ON(atomic_read(&eb->refs) == 0);
4594 int set_extent_buffer_dirty(struct extent_buffer *eb)
4597 unsigned long num_pages;
4600 check_buffer_tree_ref(eb);
4602 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4604 num_pages = num_extent_pages(eb->start, eb->len);
4605 WARN_ON(atomic_read(&eb->refs) == 0);
4606 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4608 for (i = 0; i < num_pages; i++)
4609 set_page_dirty(extent_buffer_page(eb, i));
4613 int clear_extent_buffer_uptodate(struct extent_buffer *eb)
4617 unsigned long num_pages;
4619 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4620 num_pages = num_extent_pages(eb->start, eb->len);
4621 for (i = 0; i < num_pages; i++) {
4622 page = extent_buffer_page(eb, i);
4624 ClearPageUptodate(page);
4629 int set_extent_buffer_uptodate(struct extent_buffer *eb)
4633 unsigned long num_pages;
4635 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4636 num_pages = num_extent_pages(eb->start, eb->len);
4637 for (i = 0; i < num_pages; i++) {
4638 page = extent_buffer_page(eb, i);
4639 SetPageUptodate(page);
4644 int extent_buffer_uptodate(struct extent_buffer *eb)
4646 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4649 int read_extent_buffer_pages(struct extent_io_tree *tree,
4650 struct extent_buffer *eb, u64 start, int wait,
4651 get_extent_t *get_extent, int mirror_num)
4654 unsigned long start_i;
4658 int locked_pages = 0;
4659 int all_uptodate = 1;
4660 unsigned long num_pages;
4661 unsigned long num_reads = 0;
4662 struct bio *bio = NULL;
4663 unsigned long bio_flags = 0;
4665 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4669 WARN_ON(start < eb->start);
4670 start_i = (start >> PAGE_CACHE_SHIFT) -
4671 (eb->start >> PAGE_CACHE_SHIFT);
4676 num_pages = num_extent_pages(eb->start, eb->len);
4677 for (i = start_i; i < num_pages; i++) {
4678 page = extent_buffer_page(eb, i);
4679 if (wait == WAIT_NONE) {
4680 if (!trylock_page(page))
4686 if (!PageUptodate(page)) {
4693 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4697 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
4698 eb->read_mirror = 0;
4699 atomic_set(&eb->io_pages, num_reads);
4700 for (i = start_i; i < num_pages; i++) {
4701 page = extent_buffer_page(eb, i);
4702 if (!PageUptodate(page)) {
4703 ClearPageError(page);
4704 err = __extent_read_full_page(tree, page,
4706 mirror_num, &bio_flags,
4716 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
4722 if (ret || wait != WAIT_COMPLETE)
4725 for (i = start_i; i < num_pages; i++) {
4726 page = extent_buffer_page(eb, i);
4727 wait_on_page_locked(page);
4728 if (!PageUptodate(page))
4736 while (locked_pages > 0) {
4737 page = extent_buffer_page(eb, i);
4745 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4746 unsigned long start,
4753 char *dst = (char *)dstv;
4754 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4755 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4757 WARN_ON(start > eb->len);
4758 WARN_ON(start + len > eb->start + eb->len);
4760 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4763 page = extent_buffer_page(eb, i);
4765 cur = min(len, (PAGE_CACHE_SIZE - offset));
4766 kaddr = page_address(page);
4767 memcpy(dst, kaddr + offset, cur);
4776 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4777 unsigned long min_len, char **map,
4778 unsigned long *map_start,
4779 unsigned long *map_len)
4781 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4784 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4785 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4786 unsigned long end_i = (start_offset + start + min_len - 1) >>
4793 offset = start_offset;
4797 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4800 if (start + min_len > eb->len) {
4801 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4802 "wanted %lu %lu\n", (unsigned long long)eb->start,
4803 eb->len, start, min_len);
4807 p = extent_buffer_page(eb, i);
4808 kaddr = page_address(p);
4809 *map = kaddr + offset;
4810 *map_len = PAGE_CACHE_SIZE - offset;
4814 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4815 unsigned long start,
4822 char *ptr = (char *)ptrv;
4823 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4824 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4827 WARN_ON(start > eb->len);
4828 WARN_ON(start + len > eb->start + eb->len);
4830 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4833 page = extent_buffer_page(eb, i);
4835 cur = min(len, (PAGE_CACHE_SIZE - offset));
4837 kaddr = page_address(page);
4838 ret = memcmp(ptr, kaddr + offset, cur);
4850 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4851 unsigned long start, unsigned long len)
4857 char *src = (char *)srcv;
4858 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4859 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4861 WARN_ON(start > eb->len);
4862 WARN_ON(start + len > eb->start + eb->len);
4864 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4867 page = extent_buffer_page(eb, i);
4868 WARN_ON(!PageUptodate(page));
4870 cur = min(len, PAGE_CACHE_SIZE - offset);
4871 kaddr = page_address(page);
4872 memcpy(kaddr + offset, src, cur);
4881 void memset_extent_buffer(struct extent_buffer *eb, char c,
4882 unsigned long start, unsigned long len)
4888 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4889 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4891 WARN_ON(start > eb->len);
4892 WARN_ON(start + len > eb->start + eb->len);
4894 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4897 page = extent_buffer_page(eb, i);
4898 WARN_ON(!PageUptodate(page));
4900 cur = min(len, PAGE_CACHE_SIZE - offset);
4901 kaddr = page_address(page);
4902 memset(kaddr + offset, c, cur);
4910 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4911 unsigned long dst_offset, unsigned long src_offset,
4914 u64 dst_len = dst->len;
4919 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4920 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4922 WARN_ON(src->len != dst_len);
4924 offset = (start_offset + dst_offset) &
4925 ((unsigned long)PAGE_CACHE_SIZE - 1);
4928 page = extent_buffer_page(dst, i);
4929 WARN_ON(!PageUptodate(page));
4931 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4933 kaddr = page_address(page);
4934 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4943 static void move_pages(struct page *dst_page, struct page *src_page,
4944 unsigned long dst_off, unsigned long src_off,
4947 char *dst_kaddr = page_address(dst_page);
4948 if (dst_page == src_page) {
4949 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4951 char *src_kaddr = page_address(src_page);
4952 char *p = dst_kaddr + dst_off + len;
4953 char *s = src_kaddr + src_off + len;
4960 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4962 unsigned long distance = (src > dst) ? src - dst : dst - src;
4963 return distance < len;
4966 static void copy_pages(struct page *dst_page, struct page *src_page,
4967 unsigned long dst_off, unsigned long src_off,
4970 char *dst_kaddr = page_address(dst_page);
4972 int must_memmove = 0;
4974 if (dst_page != src_page) {
4975 src_kaddr = page_address(src_page);
4977 src_kaddr = dst_kaddr;
4978 if (areas_overlap(src_off, dst_off, len))
4983 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4985 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
4988 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4989 unsigned long src_offset, unsigned long len)
4992 size_t dst_off_in_page;
4993 size_t src_off_in_page;
4994 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4995 unsigned long dst_i;
4996 unsigned long src_i;
4998 if (src_offset + len > dst->len) {
4999 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5000 "len %lu dst len %lu\n", src_offset, len, dst->len);
5003 if (dst_offset + len > dst->len) {
5004 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5005 "len %lu dst len %lu\n", dst_offset, len, dst->len);
5010 dst_off_in_page = (start_offset + dst_offset) &
5011 ((unsigned long)PAGE_CACHE_SIZE - 1);
5012 src_off_in_page = (start_offset + src_offset) &
5013 ((unsigned long)PAGE_CACHE_SIZE - 1);
5015 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5016 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
5018 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
5020 cur = min_t(unsigned long, cur,
5021 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
5023 copy_pages(extent_buffer_page(dst, dst_i),
5024 extent_buffer_page(dst, src_i),
5025 dst_off_in_page, src_off_in_page, cur);
5033 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5034 unsigned long src_offset, unsigned long len)
5037 size_t dst_off_in_page;
5038 size_t src_off_in_page;
5039 unsigned long dst_end = dst_offset + len - 1;
5040 unsigned long src_end = src_offset + len - 1;
5041 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5042 unsigned long dst_i;
5043 unsigned long src_i;
5045 if (src_offset + len > dst->len) {
5046 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5047 "len %lu len %lu\n", src_offset, len, dst->len);
5050 if (dst_offset + len > dst->len) {
5051 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5052 "len %lu len %lu\n", dst_offset, len, dst->len);
5055 if (dst_offset < src_offset) {
5056 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5060 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5061 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5063 dst_off_in_page = (start_offset + dst_end) &
5064 ((unsigned long)PAGE_CACHE_SIZE - 1);
5065 src_off_in_page = (start_offset + src_end) &
5066 ((unsigned long)PAGE_CACHE_SIZE - 1);
5068 cur = min_t(unsigned long, len, src_off_in_page + 1);
5069 cur = min(cur, dst_off_in_page + 1);
5070 move_pages(extent_buffer_page(dst, dst_i),
5071 extent_buffer_page(dst, src_i),
5072 dst_off_in_page - cur + 1,
5073 src_off_in_page - cur + 1, cur);
5081 int try_release_extent_buffer(struct page *page)
5083 struct extent_buffer *eb;
5086 * We need to make sure noboody is attaching this page to an eb right
5089 spin_lock(&page->mapping->private_lock);
5090 if (!PagePrivate(page)) {
5091 spin_unlock(&page->mapping->private_lock);
5095 eb = (struct extent_buffer *)page->private;
5099 * This is a little awful but should be ok, we need to make sure that
5100 * the eb doesn't disappear out from under us while we're looking at
5103 spin_lock(&eb->refs_lock);
5104 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
5105 spin_unlock(&eb->refs_lock);
5106 spin_unlock(&page->mapping->private_lock);
5109 spin_unlock(&page->mapping->private_lock);
5112 * If tree ref isn't set then we know the ref on this eb is a real ref,
5113 * so just return, this page will likely be freed soon anyway.
5115 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5116 spin_unlock(&eb->refs_lock);
5120 return release_extent_buffer(eb);