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);
546 if (bits & EXTENT_DELALLOC)
547 bits |= EXTENT_NORESERVE;
550 bits |= ~EXTENT_CTLBITS;
551 bits |= EXTENT_FIRST_DELALLOC;
553 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
556 if (!prealloc && (mask & __GFP_WAIT)) {
557 prealloc = alloc_extent_state(mask);
562 spin_lock(&tree->lock);
564 cached = *cached_state;
567 *cached_state = NULL;
571 if (cached && cached->tree && cached->start <= start &&
572 cached->end > start) {
574 atomic_dec(&cached->refs);
579 free_extent_state(cached);
582 * this search will find the extents that end after
585 node = tree_search(tree, start);
588 state = rb_entry(node, struct extent_state, rb_node);
590 if (state->start > end)
592 WARN_ON(state->end < start);
593 last_end = state->end;
595 /* the state doesn't have the wanted bits, go ahead */
596 if (!(state->state & bits)) {
597 state = next_state(state);
602 * | ---- desired range ---- |
604 * | ------------- state -------------- |
606 * We need to split the extent we found, and may flip
607 * bits on second half.
609 * If the extent we found extends past our range, we
610 * just split and search again. It'll get split again
611 * the next time though.
613 * If the extent we found is inside our range, we clear
614 * the desired bit on it.
617 if (state->start < start) {
618 prealloc = alloc_extent_state_atomic(prealloc);
620 err = split_state(tree, state, prealloc, start);
622 extent_io_tree_panic(tree, err);
627 if (state->end <= end) {
628 state = clear_state_bit(tree, state, &bits, wake);
634 * | ---- desired range ---- |
636 * We need to split the extent, and clear the bit
639 if (state->start <= end && state->end > end) {
640 prealloc = alloc_extent_state_atomic(prealloc);
642 err = split_state(tree, state, prealloc, end + 1);
644 extent_io_tree_panic(tree, err);
649 clear_state_bit(tree, prealloc, &bits, wake);
655 state = clear_state_bit(tree, state, &bits, wake);
657 if (last_end == (u64)-1)
659 start = last_end + 1;
660 if (start <= end && state && !need_resched())
665 spin_unlock(&tree->lock);
667 free_extent_state(prealloc);
674 spin_unlock(&tree->lock);
675 if (mask & __GFP_WAIT)
680 static void wait_on_state(struct extent_io_tree *tree,
681 struct extent_state *state)
682 __releases(tree->lock)
683 __acquires(tree->lock)
686 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
687 spin_unlock(&tree->lock);
689 spin_lock(&tree->lock);
690 finish_wait(&state->wq, &wait);
694 * waits for one or more bits to clear on a range in the state tree.
695 * The range [start, end] is inclusive.
696 * The tree lock is taken by this function
698 static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
701 struct extent_state *state;
702 struct rb_node *node;
704 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
706 spin_lock(&tree->lock);
710 * this search will find all the extents that end after
713 node = tree_search(tree, start);
717 state = rb_entry(node, struct extent_state, rb_node);
719 if (state->start > end)
722 if (state->state & bits) {
723 start = state->start;
724 atomic_inc(&state->refs);
725 wait_on_state(tree, state);
726 free_extent_state(state);
729 start = state->end + 1;
734 cond_resched_lock(&tree->lock);
737 spin_unlock(&tree->lock);
740 static void set_state_bits(struct extent_io_tree *tree,
741 struct extent_state *state,
744 unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS;
746 set_state_cb(tree, state, bits);
747 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
748 u64 range = state->end - state->start + 1;
749 tree->dirty_bytes += range;
751 state->state |= bits_to_set;
754 static void cache_state(struct extent_state *state,
755 struct extent_state **cached_ptr)
757 if (cached_ptr && !(*cached_ptr)) {
758 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
760 atomic_inc(&state->refs);
765 static void uncache_state(struct extent_state **cached_ptr)
767 if (cached_ptr && (*cached_ptr)) {
768 struct extent_state *state = *cached_ptr;
770 free_extent_state(state);
775 * set some bits on a range in the tree. This may require allocations or
776 * sleeping, so the gfp mask is used to indicate what is allowed.
778 * If any of the exclusive bits are set, this will fail with -EEXIST if some
779 * part of the range already has the desired bits set. The start of the
780 * existing range is returned in failed_start in this case.
782 * [start, end] is inclusive This takes the tree lock.
785 static int __must_check
786 __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
787 unsigned long bits, unsigned long exclusive_bits,
788 u64 *failed_start, struct extent_state **cached_state,
791 struct extent_state *state;
792 struct extent_state *prealloc = NULL;
793 struct rb_node *node;
798 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
800 bits |= EXTENT_FIRST_DELALLOC;
802 if (!prealloc && (mask & __GFP_WAIT)) {
803 prealloc = alloc_extent_state(mask);
807 spin_lock(&tree->lock);
808 if (cached_state && *cached_state) {
809 state = *cached_state;
810 if (state->start <= start && state->end > start &&
812 node = &state->rb_node;
817 * this search will find all the extents that end after
820 node = tree_search(tree, start);
822 prealloc = alloc_extent_state_atomic(prealloc);
824 err = insert_state(tree, prealloc, start, end, &bits);
826 extent_io_tree_panic(tree, err);
831 state = rb_entry(node, struct extent_state, rb_node);
833 last_start = state->start;
834 last_end = state->end;
837 * | ---- desired range ---- |
840 * Just lock what we found and keep going
842 if (state->start == start && state->end <= end) {
843 if (state->state & exclusive_bits) {
844 *failed_start = state->start;
849 set_state_bits(tree, state, &bits);
850 cache_state(state, cached_state);
851 merge_state(tree, state);
852 if (last_end == (u64)-1)
854 start = last_end + 1;
855 state = next_state(state);
856 if (start < end && state && state->start == start &&
863 * | ---- desired range ---- |
866 * | ------------- state -------------- |
868 * We need to split the extent we found, and may flip bits on
871 * If the extent we found extends past our
872 * range, we just split and search again. It'll get split
873 * again the next time though.
875 * If the extent we found is inside our range, we set the
878 if (state->start < start) {
879 if (state->state & exclusive_bits) {
880 *failed_start = start;
885 prealloc = alloc_extent_state_atomic(prealloc);
887 err = split_state(tree, state, prealloc, start);
889 extent_io_tree_panic(tree, err);
894 if (state->end <= end) {
895 set_state_bits(tree, state, &bits);
896 cache_state(state, cached_state);
897 merge_state(tree, state);
898 if (last_end == (u64)-1)
900 start = last_end + 1;
901 state = next_state(state);
902 if (start < end && state && state->start == start &&
909 * | ---- desired range ---- |
910 * | state | or | state |
912 * There's a hole, we need to insert something in it and
913 * ignore the extent we found.
915 if (state->start > start) {
917 if (end < last_start)
920 this_end = last_start - 1;
922 prealloc = alloc_extent_state_atomic(prealloc);
926 * Avoid to free 'prealloc' if it can be merged with
929 err = insert_state(tree, prealloc, start, this_end,
932 extent_io_tree_panic(tree, err);
934 cache_state(prealloc, cached_state);
936 start = this_end + 1;
940 * | ---- desired range ---- |
942 * We need to split the extent, and set the bit
945 if (state->start <= end && state->end > end) {
946 if (state->state & exclusive_bits) {
947 *failed_start = start;
952 prealloc = alloc_extent_state_atomic(prealloc);
954 err = split_state(tree, state, prealloc, end + 1);
956 extent_io_tree_panic(tree, err);
958 set_state_bits(tree, prealloc, &bits);
959 cache_state(prealloc, cached_state);
960 merge_state(tree, prealloc);
968 spin_unlock(&tree->lock);
970 free_extent_state(prealloc);
977 spin_unlock(&tree->lock);
978 if (mask & __GFP_WAIT)
983 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
984 unsigned long bits, u64 * failed_start,
985 struct extent_state **cached_state, gfp_t mask)
987 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
993 * convert_extent_bit - convert all bits in a given range from one bit to
995 * @tree: the io tree to search
996 * @start: the start offset in bytes
997 * @end: the end offset in bytes (inclusive)
998 * @bits: the bits to set in this range
999 * @clear_bits: the bits to clear in this range
1000 * @cached_state: state that we're going to cache
1001 * @mask: the allocation mask
1003 * This will go through and set bits for the given range. If any states exist
1004 * already in this range they are set with the given bit and cleared of the
1005 * clear_bits. This is only meant to be used by things that are mergeable, ie
1006 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1007 * boundary bits like LOCK.
1009 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1010 unsigned long bits, unsigned long clear_bits,
1011 struct extent_state **cached_state, gfp_t mask)
1013 struct extent_state *state;
1014 struct extent_state *prealloc = NULL;
1015 struct rb_node *node;
1020 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
1023 if (!prealloc && (mask & __GFP_WAIT)) {
1024 prealloc = alloc_extent_state(mask);
1029 spin_lock(&tree->lock);
1030 if (cached_state && *cached_state) {
1031 state = *cached_state;
1032 if (state->start <= start && state->end > start &&
1034 node = &state->rb_node;
1040 * this search will find all the extents that end after
1043 node = tree_search(tree, start);
1045 prealloc = alloc_extent_state_atomic(prealloc);
1050 err = insert_state(tree, prealloc, start, end, &bits);
1053 extent_io_tree_panic(tree, err);
1056 state = rb_entry(node, struct extent_state, rb_node);
1058 last_start = state->start;
1059 last_end = state->end;
1062 * | ---- desired range ---- |
1065 * Just lock what we found and keep going
1067 if (state->start == start && state->end <= end) {
1068 set_state_bits(tree, state, &bits);
1069 cache_state(state, cached_state);
1070 state = clear_state_bit(tree, state, &clear_bits, 0);
1071 if (last_end == (u64)-1)
1073 start = last_end + 1;
1074 if (start < end && state && state->start == start &&
1081 * | ---- desired range ---- |
1084 * | ------------- state -------------- |
1086 * We need to split the extent we found, and may flip bits on
1089 * If the extent we found extends past our
1090 * range, we just split and search again. It'll get split
1091 * again the next time though.
1093 * If the extent we found is inside our range, we set the
1094 * desired bit on it.
1096 if (state->start < start) {
1097 prealloc = alloc_extent_state_atomic(prealloc);
1102 err = split_state(tree, state, prealloc, start);
1104 extent_io_tree_panic(tree, err);
1108 if (state->end <= end) {
1109 set_state_bits(tree, state, &bits);
1110 cache_state(state, cached_state);
1111 state = clear_state_bit(tree, state, &clear_bits, 0);
1112 if (last_end == (u64)-1)
1114 start = last_end + 1;
1115 if (start < end && state && state->start == start &&
1122 * | ---- desired range ---- |
1123 * | state | or | state |
1125 * There's a hole, we need to insert something in it and
1126 * ignore the extent we found.
1128 if (state->start > start) {
1130 if (end < last_start)
1133 this_end = last_start - 1;
1135 prealloc = alloc_extent_state_atomic(prealloc);
1142 * Avoid to free 'prealloc' if it can be merged with
1145 err = insert_state(tree, prealloc, start, this_end,
1148 extent_io_tree_panic(tree, err);
1149 cache_state(prealloc, cached_state);
1151 start = this_end + 1;
1155 * | ---- desired range ---- |
1157 * We need to split the extent, and set the bit
1160 if (state->start <= end && state->end > end) {
1161 prealloc = alloc_extent_state_atomic(prealloc);
1167 err = split_state(tree, state, prealloc, end + 1);
1169 extent_io_tree_panic(tree, err);
1171 set_state_bits(tree, prealloc, &bits);
1172 cache_state(prealloc, cached_state);
1173 clear_state_bit(tree, prealloc, &clear_bits, 0);
1181 spin_unlock(&tree->lock);
1183 free_extent_state(prealloc);
1190 spin_unlock(&tree->lock);
1191 if (mask & __GFP_WAIT)
1196 /* wrappers around set/clear extent bit */
1197 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1200 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
1204 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1205 unsigned long bits, gfp_t mask)
1207 return set_extent_bit(tree, start, end, bits, NULL,
1211 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1212 unsigned long bits, gfp_t mask)
1214 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
1217 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
1218 struct extent_state **cached_state, gfp_t mask)
1220 return set_extent_bit(tree, start, end,
1221 EXTENT_DELALLOC | EXTENT_UPTODATE,
1222 NULL, cached_state, mask);
1225 int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1226 struct extent_state **cached_state, gfp_t mask)
1228 return set_extent_bit(tree, start, end,
1229 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1230 NULL, cached_state, mask);
1233 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1236 return clear_extent_bit(tree, start, end,
1237 EXTENT_DIRTY | EXTENT_DELALLOC |
1238 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
1241 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1244 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
1248 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1249 struct extent_state **cached_state, gfp_t mask)
1251 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
1252 cached_state, mask);
1255 int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1256 struct extent_state **cached_state, gfp_t mask)
1258 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1259 cached_state, mask);
1263 * either insert or lock state struct between start and end use mask to tell
1264 * us if waiting is desired.
1266 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1267 unsigned long bits, struct extent_state **cached_state)
1272 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1273 EXTENT_LOCKED, &failed_start,
1274 cached_state, GFP_NOFS);
1275 if (err == -EEXIST) {
1276 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1277 start = failed_start;
1280 WARN_ON(start > end);
1285 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1287 return lock_extent_bits(tree, start, end, 0, NULL);
1290 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1295 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1296 &failed_start, NULL, GFP_NOFS);
1297 if (err == -EEXIST) {
1298 if (failed_start > start)
1299 clear_extent_bit(tree, start, failed_start - 1,
1300 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
1306 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1307 struct extent_state **cached, gfp_t mask)
1309 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1313 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1315 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1319 int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1321 unsigned long index = start >> PAGE_CACHE_SHIFT;
1322 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1325 while (index <= end_index) {
1326 page = find_get_page(inode->i_mapping, index);
1327 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1328 clear_page_dirty_for_io(page);
1329 page_cache_release(page);
1335 int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1337 unsigned long index = start >> PAGE_CACHE_SHIFT;
1338 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1341 while (index <= end_index) {
1342 page = find_get_page(inode->i_mapping, index);
1343 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1344 account_page_redirty(page);
1345 __set_page_dirty_nobuffers(page);
1346 page_cache_release(page);
1353 * helper function to set both pages and extents in the tree writeback
1355 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1357 unsigned long index = start >> PAGE_CACHE_SHIFT;
1358 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1361 while (index <= end_index) {
1362 page = find_get_page(tree->mapping, index);
1363 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1364 set_page_writeback(page);
1365 page_cache_release(page);
1371 /* find the first state struct with 'bits' set after 'start', and
1372 * return it. tree->lock must be held. NULL will returned if
1373 * nothing was found after 'start'
1375 static struct extent_state *
1376 find_first_extent_bit_state(struct extent_io_tree *tree,
1377 u64 start, unsigned long bits)
1379 struct rb_node *node;
1380 struct extent_state *state;
1383 * this search will find all the extents that end after
1386 node = tree_search(tree, start);
1391 state = rb_entry(node, struct extent_state, rb_node);
1392 if (state->end >= start && (state->state & bits))
1395 node = rb_next(node);
1404 * find the first offset in the io tree with 'bits' set. zero is
1405 * returned if we find something, and *start_ret and *end_ret are
1406 * set to reflect the state struct that was found.
1408 * If nothing was found, 1 is returned. If found something, return 0.
1410 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1411 u64 *start_ret, u64 *end_ret, unsigned long bits,
1412 struct extent_state **cached_state)
1414 struct extent_state *state;
1418 spin_lock(&tree->lock);
1419 if (cached_state && *cached_state) {
1420 state = *cached_state;
1421 if (state->end == start - 1 && state->tree) {
1422 n = rb_next(&state->rb_node);
1424 state = rb_entry(n, struct extent_state,
1426 if (state->state & bits)
1430 free_extent_state(*cached_state);
1431 *cached_state = NULL;
1434 free_extent_state(*cached_state);
1435 *cached_state = NULL;
1438 state = find_first_extent_bit_state(tree, start, bits);
1441 cache_state(state, cached_state);
1442 *start_ret = state->start;
1443 *end_ret = state->end;
1447 spin_unlock(&tree->lock);
1452 * find a contiguous range of bytes in the file marked as delalloc, not
1453 * more than 'max_bytes'. start and end are used to return the range,
1455 * 1 is returned if we find something, 0 if nothing was in the tree
1457 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1458 u64 *start, u64 *end, u64 max_bytes,
1459 struct extent_state **cached_state)
1461 struct rb_node *node;
1462 struct extent_state *state;
1463 u64 cur_start = *start;
1465 u64 total_bytes = 0;
1467 spin_lock(&tree->lock);
1470 * this search will find all the extents that end after
1473 node = tree_search(tree, cur_start);
1481 state = rb_entry(node, struct extent_state, rb_node);
1482 if (found && (state->start != cur_start ||
1483 (state->state & EXTENT_BOUNDARY))) {
1486 if (!(state->state & EXTENT_DELALLOC)) {
1492 *start = state->start;
1493 *cached_state = state;
1494 atomic_inc(&state->refs);
1498 cur_start = state->end + 1;
1499 node = rb_next(node);
1502 total_bytes += state->end - state->start + 1;
1503 if (total_bytes >= max_bytes)
1507 spin_unlock(&tree->lock);
1511 static noinline void __unlock_for_delalloc(struct inode *inode,
1512 struct page *locked_page,
1516 struct page *pages[16];
1517 unsigned long index = start >> PAGE_CACHE_SHIFT;
1518 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1519 unsigned long nr_pages = end_index - index + 1;
1522 if (index == locked_page->index && end_index == index)
1525 while (nr_pages > 0) {
1526 ret = find_get_pages_contig(inode->i_mapping, index,
1527 min_t(unsigned long, nr_pages,
1528 ARRAY_SIZE(pages)), pages);
1529 for (i = 0; i < ret; i++) {
1530 if (pages[i] != locked_page)
1531 unlock_page(pages[i]);
1532 page_cache_release(pages[i]);
1540 static noinline int lock_delalloc_pages(struct inode *inode,
1541 struct page *locked_page,
1545 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1546 unsigned long start_index = index;
1547 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1548 unsigned long pages_locked = 0;
1549 struct page *pages[16];
1550 unsigned long nrpages;
1554 /* the caller is responsible for locking the start index */
1555 if (index == locked_page->index && index == end_index)
1558 /* skip the page at the start index */
1559 nrpages = end_index - index + 1;
1560 while (nrpages > 0) {
1561 ret = find_get_pages_contig(inode->i_mapping, index,
1562 min_t(unsigned long,
1563 nrpages, ARRAY_SIZE(pages)), pages);
1568 /* now we have an array of pages, lock them all */
1569 for (i = 0; i < ret; i++) {
1571 * the caller is taking responsibility for
1574 if (pages[i] != locked_page) {
1575 lock_page(pages[i]);
1576 if (!PageDirty(pages[i]) ||
1577 pages[i]->mapping != inode->i_mapping) {
1579 unlock_page(pages[i]);
1580 page_cache_release(pages[i]);
1584 page_cache_release(pages[i]);
1593 if (ret && pages_locked) {
1594 __unlock_for_delalloc(inode, locked_page,
1596 ((u64)(start_index + pages_locked - 1)) <<
1603 * find a contiguous range of bytes in the file marked as delalloc, not
1604 * more than 'max_bytes'. start and end are used to return the range,
1606 * 1 is returned if we find something, 0 if nothing was in the tree
1608 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1609 struct extent_io_tree *tree,
1610 struct page *locked_page,
1611 u64 *start, u64 *end,
1617 struct extent_state *cached_state = NULL;
1622 /* step one, find a bunch of delalloc bytes starting at start */
1623 delalloc_start = *start;
1625 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1626 max_bytes, &cached_state);
1627 if (!found || delalloc_end <= *start) {
1628 *start = delalloc_start;
1629 *end = delalloc_end;
1630 free_extent_state(cached_state);
1635 * start comes from the offset of locked_page. We have to lock
1636 * pages in order, so we can't process delalloc bytes before
1639 if (delalloc_start < *start)
1640 delalloc_start = *start;
1643 * make sure to limit the number of pages we try to lock down
1646 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1647 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1649 /* step two, lock all the pages after the page that has start */
1650 ret = lock_delalloc_pages(inode, locked_page,
1651 delalloc_start, delalloc_end);
1652 if (ret == -EAGAIN) {
1653 /* some of the pages are gone, lets avoid looping by
1654 * shortening the size of the delalloc range we're searching
1656 free_extent_state(cached_state);
1658 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1659 max_bytes = PAGE_CACHE_SIZE - offset;
1667 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
1669 /* step three, lock the state bits for the whole range */
1670 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
1672 /* then test to make sure it is all still delalloc */
1673 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1674 EXTENT_DELALLOC, 1, cached_state);
1676 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1677 &cached_state, GFP_NOFS);
1678 __unlock_for_delalloc(inode, locked_page,
1679 delalloc_start, delalloc_end);
1683 free_extent_state(cached_state);
1684 *start = delalloc_start;
1685 *end = delalloc_end;
1690 int extent_clear_unlock_delalloc(struct inode *inode,
1691 struct extent_io_tree *tree,
1692 u64 start, u64 end, struct page *locked_page,
1696 struct page *pages[16];
1697 unsigned long index = start >> PAGE_CACHE_SHIFT;
1698 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1699 unsigned long nr_pages = end_index - index + 1;
1701 unsigned long clear_bits = 0;
1703 if (op & EXTENT_CLEAR_UNLOCK)
1704 clear_bits |= EXTENT_LOCKED;
1705 if (op & EXTENT_CLEAR_DIRTY)
1706 clear_bits |= EXTENT_DIRTY;
1708 if (op & EXTENT_CLEAR_DELALLOC)
1709 clear_bits |= EXTENT_DELALLOC;
1711 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1712 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1713 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1714 EXTENT_SET_PRIVATE2)))
1717 while (nr_pages > 0) {
1718 ret = find_get_pages_contig(inode->i_mapping, index,
1719 min_t(unsigned long,
1720 nr_pages, ARRAY_SIZE(pages)), pages);
1721 for (i = 0; i < ret; i++) {
1723 if (op & EXTENT_SET_PRIVATE2)
1724 SetPagePrivate2(pages[i]);
1726 if (pages[i] == locked_page) {
1727 page_cache_release(pages[i]);
1730 if (op & EXTENT_CLEAR_DIRTY)
1731 clear_page_dirty_for_io(pages[i]);
1732 if (op & EXTENT_SET_WRITEBACK)
1733 set_page_writeback(pages[i]);
1734 if (op & EXTENT_END_WRITEBACK)
1735 end_page_writeback(pages[i]);
1736 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1737 unlock_page(pages[i]);
1738 page_cache_release(pages[i]);
1748 * count the number of bytes in the tree that have a given bit(s)
1749 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1750 * cached. The total number found is returned.
1752 u64 count_range_bits(struct extent_io_tree *tree,
1753 u64 *start, u64 search_end, u64 max_bytes,
1754 unsigned long bits, int contig)
1756 struct rb_node *node;
1757 struct extent_state *state;
1758 u64 cur_start = *start;
1759 u64 total_bytes = 0;
1763 if (search_end <= cur_start) {
1768 spin_lock(&tree->lock);
1769 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1770 total_bytes = tree->dirty_bytes;
1774 * this search will find all the extents that end after
1777 node = tree_search(tree, cur_start);
1782 state = rb_entry(node, struct extent_state, rb_node);
1783 if (state->start > search_end)
1785 if (contig && found && state->start > last + 1)
1787 if (state->end >= cur_start && (state->state & bits) == bits) {
1788 total_bytes += min(search_end, state->end) + 1 -
1789 max(cur_start, state->start);
1790 if (total_bytes >= max_bytes)
1793 *start = max(cur_start, state->start);
1797 } else if (contig && found) {
1800 node = rb_next(node);
1805 spin_unlock(&tree->lock);
1810 * set the private field for a given byte offset in the tree. If there isn't
1811 * an extent_state there already, this does nothing.
1813 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1815 struct rb_node *node;
1816 struct extent_state *state;
1819 spin_lock(&tree->lock);
1821 * this search will find all the extents that end after
1824 node = tree_search(tree, start);
1829 state = rb_entry(node, struct extent_state, rb_node);
1830 if (state->start != start) {
1834 state->private = private;
1836 spin_unlock(&tree->lock);
1840 void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 csums[],
1843 struct rb_node *node;
1844 struct extent_state *state;
1846 spin_lock(&tree->lock);
1848 * this search will find all the extents that end after
1851 node = tree_search(tree, start);
1854 state = rb_entry(node, struct extent_state, rb_node);
1855 BUG_ON(state->start != start);
1858 state->private = *csums++;
1860 state = next_state(state);
1862 spin_unlock(&tree->lock);
1865 static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index)
1867 struct bio_vec *bvec = bio->bi_io_vec + bio_index;
1869 return page_offset(bvec->bv_page) + bvec->bv_offset;
1872 void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int bio_index,
1873 u32 csums[], int count)
1875 struct rb_node *node;
1876 struct extent_state *state = NULL;
1879 spin_lock(&tree->lock);
1881 start = __btrfs_get_bio_offset(bio, bio_index);
1882 if (state == NULL || state->start != start) {
1883 node = tree_search(tree, start);
1886 state = rb_entry(node, struct extent_state, rb_node);
1887 BUG_ON(state->start != start);
1889 state->private = *csums++;
1893 state = next_state(state);
1895 spin_unlock(&tree->lock);
1898 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1900 struct rb_node *node;
1901 struct extent_state *state;
1904 spin_lock(&tree->lock);
1906 * this search will find all the extents that end after
1909 node = tree_search(tree, start);
1914 state = rb_entry(node, struct extent_state, rb_node);
1915 if (state->start != start) {
1919 *private = state->private;
1921 spin_unlock(&tree->lock);
1926 * searches a range in the state tree for a given mask.
1927 * If 'filled' == 1, this returns 1 only if every extent in the tree
1928 * has the bits set. Otherwise, 1 is returned if any bit in the
1929 * range is found set.
1931 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1932 unsigned long bits, int filled, struct extent_state *cached)
1934 struct extent_state *state = NULL;
1935 struct rb_node *node;
1938 spin_lock(&tree->lock);
1939 if (cached && cached->tree && cached->start <= start &&
1940 cached->end > start)
1941 node = &cached->rb_node;
1943 node = tree_search(tree, start);
1944 while (node && start <= end) {
1945 state = rb_entry(node, struct extent_state, rb_node);
1947 if (filled && state->start > start) {
1952 if (state->start > end)
1955 if (state->state & bits) {
1959 } else if (filled) {
1964 if (state->end == (u64)-1)
1967 start = state->end + 1;
1970 node = rb_next(node);
1977 spin_unlock(&tree->lock);
1982 * helper function to set a given page up to date if all the
1983 * extents in the tree for that page are up to date
1985 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
1987 u64 start = page_offset(page);
1988 u64 end = start + PAGE_CACHE_SIZE - 1;
1989 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1990 SetPageUptodate(page);
1994 * When IO fails, either with EIO or csum verification fails, we
1995 * try other mirrors that might have a good copy of the data. This
1996 * io_failure_record is used to record state as we go through all the
1997 * mirrors. If another mirror has good data, the page is set up to date
1998 * and things continue. If a good mirror can't be found, the original
1999 * bio end_io callback is called to indicate things have failed.
2001 struct io_failure_record {
2006 unsigned long bio_flags;
2012 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
2017 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2019 set_state_private(failure_tree, rec->start, 0);
2020 ret = clear_extent_bits(failure_tree, rec->start,
2021 rec->start + rec->len - 1,
2022 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2026 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
2027 rec->start + rec->len - 1,
2028 EXTENT_DAMAGED, GFP_NOFS);
2036 static void repair_io_failure_callback(struct bio *bio, int err)
2038 complete(bio->bi_private);
2042 * this bypasses the standard btrfs submit functions deliberately, as
2043 * the standard behavior is to write all copies in a raid setup. here we only
2044 * want to write the one bad copy. so we do the mapping for ourselves and issue
2045 * submit_bio directly.
2046 * to avoid any synchronization issues, wait for the data after writing, which
2047 * actually prevents the read that triggered the error from finishing.
2048 * currently, there can be no more than two copies of every data bit. thus,
2049 * exactly one rewrite is required.
2051 int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
2052 u64 length, u64 logical, struct page *page,
2056 struct btrfs_device *dev;
2057 DECLARE_COMPLETION_ONSTACK(compl);
2060 struct btrfs_bio *bbio = NULL;
2061 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
2064 BUG_ON(!mirror_num);
2066 /* we can't repair anything in raid56 yet */
2067 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2070 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
2073 bio->bi_private = &compl;
2074 bio->bi_end_io = repair_io_failure_callback;
2076 map_length = length;
2078 ret = btrfs_map_block(fs_info, WRITE, logical,
2079 &map_length, &bbio, mirror_num);
2084 BUG_ON(mirror_num != bbio->mirror_num);
2085 sector = bbio->stripes[mirror_num-1].physical >> 9;
2086 bio->bi_sector = sector;
2087 dev = bbio->stripes[mirror_num-1].dev;
2089 if (!dev || !dev->bdev || !dev->writeable) {
2093 bio->bi_bdev = dev->bdev;
2094 bio_add_page(bio, page, length, start - page_offset(page));
2095 btrfsic_submit_bio(WRITE_SYNC, bio);
2096 wait_for_completion(&compl);
2098 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2099 /* try to remap that extent elsewhere? */
2101 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
2105 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
2106 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2107 start, rcu_str_deref(dev->name), sector);
2113 int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2116 u64 start = eb->start;
2117 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
2120 for (i = 0; i < num_pages; i++) {
2121 struct page *p = extent_buffer_page(eb, i);
2122 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
2123 start, p, mirror_num);
2126 start += PAGE_CACHE_SIZE;
2133 * each time an IO finishes, we do a fast check in the IO failure tree
2134 * to see if we need to process or clean up an io_failure_record
2136 static int clean_io_failure(u64 start, struct page *page)
2139 u64 private_failure;
2140 struct io_failure_record *failrec;
2141 struct btrfs_fs_info *fs_info;
2142 struct extent_state *state;
2146 struct inode *inode = page->mapping->host;
2149 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2150 (u64)-1, 1, EXTENT_DIRTY, 0);
2154 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2159 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2160 BUG_ON(!failrec->this_mirror);
2162 if (failrec->in_validation) {
2163 /* there was no real error, just free the record */
2164 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2170 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2171 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2174 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2176 if (state && state->start == failrec->start) {
2177 fs_info = BTRFS_I(inode)->root->fs_info;
2178 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2180 if (num_copies > 1) {
2181 ret = repair_io_failure(fs_info, start, failrec->len,
2182 failrec->logical, page,
2183 failrec->failed_mirror);
2191 ret = free_io_failure(inode, failrec, did_repair);
2197 * this is a generic handler for readpage errors (default
2198 * readpage_io_failed_hook). if other copies exist, read those and write back
2199 * good data to the failed position. does not investigate in remapping the
2200 * failed extent elsewhere, hoping the device will be smart enough to do this as
2204 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2205 u64 start, u64 end, int failed_mirror,
2206 struct extent_state *state)
2208 struct io_failure_record *failrec = NULL;
2210 struct extent_map *em;
2211 struct inode *inode = page->mapping->host;
2212 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2213 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2214 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2221 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2223 ret = get_state_private(failure_tree, start, &private);
2225 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2228 failrec->start = start;
2229 failrec->len = end - start + 1;
2230 failrec->this_mirror = 0;
2231 failrec->bio_flags = 0;
2232 failrec->in_validation = 0;
2234 read_lock(&em_tree->lock);
2235 em = lookup_extent_mapping(em_tree, start, failrec->len);
2237 read_unlock(&em_tree->lock);
2242 if (em->start > start || em->start + em->len < start) {
2243 free_extent_map(em);
2246 read_unlock(&em_tree->lock);
2252 logical = start - em->start;
2253 logical = em->block_start + logical;
2254 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2255 logical = em->block_start;
2256 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2257 extent_set_compress_type(&failrec->bio_flags,
2260 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2261 "len=%llu\n", logical, start, failrec->len);
2262 failrec->logical = logical;
2263 free_extent_map(em);
2265 /* set the bits in the private failure tree */
2266 ret = set_extent_bits(failure_tree, start, end,
2267 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2269 ret = set_state_private(failure_tree, start,
2270 (u64)(unsigned long)failrec);
2271 /* set the bits in the inode's tree */
2273 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2280 failrec = (struct io_failure_record *)(unsigned long)private;
2281 pr_debug("bio_readpage_error: (found) logical=%llu, "
2282 "start=%llu, len=%llu, validation=%d\n",
2283 failrec->logical, failrec->start, failrec->len,
2284 failrec->in_validation);
2286 * when data can be on disk more than twice, add to failrec here
2287 * (e.g. with a list for failed_mirror) to make
2288 * clean_io_failure() clean all those errors at once.
2291 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2292 failrec->logical, failrec->len);
2293 if (num_copies == 1) {
2295 * we only have a single copy of the data, so don't bother with
2296 * all the retry and error correction code that follows. no
2297 * matter what the error is, it is very likely to persist.
2299 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2300 "state=%p, num_copies=%d, next_mirror %d, "
2301 "failed_mirror %d\n", state, num_copies,
2302 failrec->this_mirror, failed_mirror);
2303 free_io_failure(inode, failrec, 0);
2308 spin_lock(&tree->lock);
2309 state = find_first_extent_bit_state(tree, failrec->start,
2311 if (state && state->start != failrec->start)
2313 spin_unlock(&tree->lock);
2317 * there are two premises:
2318 * a) deliver good data to the caller
2319 * b) correct the bad sectors on disk
2321 if (failed_bio->bi_vcnt > 1) {
2323 * to fulfill b), we need to know the exact failing sectors, as
2324 * we don't want to rewrite any more than the failed ones. thus,
2325 * we need separate read requests for the failed bio
2327 * if the following BUG_ON triggers, our validation request got
2328 * merged. we need separate requests for our algorithm to work.
2330 BUG_ON(failrec->in_validation);
2331 failrec->in_validation = 1;
2332 failrec->this_mirror = failed_mirror;
2333 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2336 * we're ready to fulfill a) and b) alongside. get a good copy
2337 * of the failed sector and if we succeed, we have setup
2338 * everything for repair_io_failure to do the rest for us.
2340 if (failrec->in_validation) {
2341 BUG_ON(failrec->this_mirror != failed_mirror);
2342 failrec->in_validation = 0;
2343 failrec->this_mirror = 0;
2345 failrec->failed_mirror = failed_mirror;
2346 failrec->this_mirror++;
2347 if (failrec->this_mirror == failed_mirror)
2348 failrec->this_mirror++;
2349 read_mode = READ_SYNC;
2352 if (!state || failrec->this_mirror > num_copies) {
2353 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2354 "next_mirror %d, failed_mirror %d\n", state,
2355 num_copies, failrec->this_mirror, failed_mirror);
2356 free_io_failure(inode, failrec, 0);
2360 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
2362 free_io_failure(inode, failrec, 0);
2365 bio->bi_private = state;
2366 bio->bi_end_io = failed_bio->bi_end_io;
2367 bio->bi_sector = failrec->logical >> 9;
2368 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2371 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2373 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2374 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2375 failrec->this_mirror, num_copies, failrec->in_validation);
2377 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2378 failrec->this_mirror,
2379 failrec->bio_flags, 0);
2383 /* lots and lots of room for performance fixes in the end_bio funcs */
2385 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2387 int uptodate = (err == 0);
2388 struct extent_io_tree *tree;
2391 tree = &BTRFS_I(page->mapping->host)->io_tree;
2393 if (tree->ops && tree->ops->writepage_end_io_hook) {
2394 ret = tree->ops->writepage_end_io_hook(page, start,
2395 end, NULL, uptodate);
2401 ClearPageUptodate(page);
2408 * after a writepage IO is done, we need to:
2409 * clear the uptodate bits on error
2410 * clear the writeback bits in the extent tree for this IO
2411 * end_page_writeback if the page has no more pending IO
2413 * Scheduling is not allowed, so the extent state tree is expected
2414 * to have one and only one object corresponding to this IO.
2416 static void end_bio_extent_writepage(struct bio *bio, int err)
2418 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2419 struct extent_io_tree *tree;
2424 struct page *page = bvec->bv_page;
2425 tree = &BTRFS_I(page->mapping->host)->io_tree;
2427 /* We always issue full-page reads, but if some block
2428 * in a page fails to read, blk_update_request() will
2429 * advance bv_offset and adjust bv_len to compensate.
2430 * Print a warning for nonzero offsets, and an error
2431 * if they don't add up to a full page. */
2432 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2433 printk("%s page write in btrfs with offset %u and length %u\n",
2434 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2435 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2436 bvec->bv_offset, bvec->bv_len);
2438 start = page_offset(page);
2439 end = start + bvec->bv_offset + bvec->bv_len - 1;
2441 if (--bvec >= bio->bi_io_vec)
2442 prefetchw(&bvec->bv_page->flags);
2444 if (end_extent_writepage(page, err, start, end))
2447 end_page_writeback(page);
2448 } while (bvec >= bio->bi_io_vec);
2454 * after a readpage IO is done, we need to:
2455 * clear the uptodate bits on error
2456 * set the uptodate bits if things worked
2457 * set the page up to date if all extents in the tree are uptodate
2458 * clear the lock bit in the extent tree
2459 * unlock the page if there are no other extents locked for it
2461 * Scheduling is not allowed, so the extent state tree is expected
2462 * to have one and only one object corresponding to this IO.
2464 static void end_bio_extent_readpage(struct bio *bio, int err)
2466 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2467 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2468 struct bio_vec *bvec = bio->bi_io_vec;
2469 struct extent_io_tree *tree;
2479 struct page *page = bvec->bv_page;
2480 struct extent_state *cached = NULL;
2481 struct extent_state *state;
2482 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
2483 struct inode *inode = page->mapping->host;
2485 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2486 "mirror=%lu\n", (u64)bio->bi_sector, err,
2487 io_bio->mirror_num);
2488 tree = &BTRFS_I(inode)->io_tree;
2490 /* We always issue full-page reads, but if some block
2491 * in a page fails to read, blk_update_request() will
2492 * advance bv_offset and adjust bv_len to compensate.
2493 * Print a warning for nonzero offsets, and an error
2494 * if they don't add up to a full page. */
2495 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2496 printk("%s page read in btrfs with offset %u and length %u\n",
2497 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2498 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2499 bvec->bv_offset, bvec->bv_len);
2501 start = page_offset(page);
2502 end = start + bvec->bv_offset + bvec->bv_len - 1;
2504 if (++bvec <= bvec_end)
2505 prefetchw(&bvec->bv_page->flags);
2507 spin_lock(&tree->lock);
2508 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2509 if (state && state->start == start) {
2511 * take a reference on the state, unlock will drop
2514 cache_state(state, &cached);
2516 spin_unlock(&tree->lock);
2518 mirror = io_bio->mirror_num;
2519 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2520 ret = tree->ops->readpage_end_io_hook(page, start, end,
2525 clean_io_failure(start, page);
2528 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
2529 ret = tree->ops->readpage_io_failed_hook(page, mirror);
2531 test_bit(BIO_UPTODATE, &bio->bi_flags))
2533 } else if (!uptodate) {
2535 * The generic bio_readpage_error handles errors the
2536 * following way: If possible, new read requests are
2537 * created and submitted and will end up in
2538 * end_bio_extent_readpage as well (if we're lucky, not
2539 * in the !uptodate case). In that case it returns 0 and
2540 * we just go on with the next page in our bio. If it
2541 * can't handle the error it will return -EIO and we
2542 * remain responsible for that page.
2544 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
2547 test_bit(BIO_UPTODATE, &bio->bi_flags);
2550 uncache_state(&cached);
2555 if (uptodate && tree->track_uptodate) {
2556 set_extent_uptodate(tree, start, end, &cached,
2559 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2562 loff_t i_size = i_size_read(inode);
2563 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2566 /* Zero out the end if this page straddles i_size */
2567 offset = i_size & (PAGE_CACHE_SIZE-1);
2568 if (page->index == end_index && offset)
2569 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
2570 SetPageUptodate(page);
2572 ClearPageUptodate(page);
2576 } while (bvec <= bvec_end);
2582 * this allocates from the btrfs_bioset. We're returning a bio right now
2583 * but you can call btrfs_io_bio for the appropriate container_of magic
2586 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2591 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
2593 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2594 while (!bio && (nr_vecs /= 2)) {
2595 bio = bio_alloc_bioset(gfp_flags,
2596 nr_vecs, btrfs_bioset);
2602 bio->bi_bdev = bdev;
2603 bio->bi_sector = first_sector;
2608 struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2610 return bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2614 /* this also allocates from the btrfs_bioset */
2615 struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2617 return bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2621 static int __must_check submit_one_bio(int rw, struct bio *bio,
2622 int mirror_num, unsigned long bio_flags)
2625 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2626 struct page *page = bvec->bv_page;
2627 struct extent_io_tree *tree = bio->bi_private;
2630 start = page_offset(page) + bvec->bv_offset;
2632 bio->bi_private = NULL;
2636 if (tree->ops && tree->ops->submit_bio_hook)
2637 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2638 mirror_num, bio_flags, start);
2640 btrfsic_submit_bio(rw, bio);
2642 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2648 static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
2649 unsigned long offset, size_t size, struct bio *bio,
2650 unsigned long bio_flags)
2653 if (tree->ops && tree->ops->merge_bio_hook)
2654 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
2661 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2662 struct page *page, sector_t sector,
2663 size_t size, unsigned long offset,
2664 struct block_device *bdev,
2665 struct bio **bio_ret,
2666 unsigned long max_pages,
2667 bio_end_io_t end_io_func,
2669 unsigned long prev_bio_flags,
2670 unsigned long bio_flags)
2676 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2677 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2678 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2680 if (bio_ret && *bio_ret) {
2683 contig = bio->bi_sector == sector;
2685 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2688 if (prev_bio_flags != bio_flags || !contig ||
2689 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
2690 bio_add_page(bio, page, page_size, offset) < page_size) {
2691 ret = submit_one_bio(rw, bio, mirror_num,
2700 if (this_compressed)
2703 nr = bio_get_nr_vecs(bdev);
2705 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2709 bio_add_page(bio, page, page_size, offset);
2710 bio->bi_end_io = end_io_func;
2711 bio->bi_private = tree;
2716 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2721 static void attach_extent_buffer_page(struct extent_buffer *eb,
2724 if (!PagePrivate(page)) {
2725 SetPagePrivate(page);
2726 page_cache_get(page);
2727 set_page_private(page, (unsigned long)eb);
2729 WARN_ON(page->private != (unsigned long)eb);
2733 void set_page_extent_mapped(struct page *page)
2735 if (!PagePrivate(page)) {
2736 SetPagePrivate(page);
2737 page_cache_get(page);
2738 set_page_private(page, EXTENT_PAGE_PRIVATE);
2743 * basic readpage implementation. Locked extent state structs are inserted
2744 * into the tree that are removed when the IO is done (by the end_io
2746 * XXX JDM: This needs looking at to ensure proper page locking
2748 static int __extent_read_full_page(struct extent_io_tree *tree,
2750 get_extent_t *get_extent,
2751 struct bio **bio, int mirror_num,
2752 unsigned long *bio_flags, int rw)
2754 struct inode *inode = page->mapping->host;
2755 u64 start = page_offset(page);
2756 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2760 u64 last_byte = i_size_read(inode);
2764 struct extent_map *em;
2765 struct block_device *bdev;
2766 struct btrfs_ordered_extent *ordered;
2769 size_t pg_offset = 0;
2771 size_t disk_io_size;
2772 size_t blocksize = inode->i_sb->s_blocksize;
2773 unsigned long this_bio_flag = 0;
2775 set_page_extent_mapped(page);
2777 if (!PageUptodate(page)) {
2778 if (cleancache_get_page(page) == 0) {
2779 BUG_ON(blocksize != PAGE_SIZE);
2786 lock_extent(tree, start, end);
2787 ordered = btrfs_lookup_ordered_extent(inode, start);
2790 unlock_extent(tree, start, end);
2791 btrfs_start_ordered_extent(inode, ordered, 1);
2792 btrfs_put_ordered_extent(ordered);
2795 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2797 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2800 iosize = PAGE_CACHE_SIZE - zero_offset;
2801 userpage = kmap_atomic(page);
2802 memset(userpage + zero_offset, 0, iosize);
2803 flush_dcache_page(page);
2804 kunmap_atomic(userpage);
2807 while (cur <= end) {
2808 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2810 if (cur >= last_byte) {
2812 struct extent_state *cached = NULL;
2814 iosize = PAGE_CACHE_SIZE - pg_offset;
2815 userpage = kmap_atomic(page);
2816 memset(userpage + pg_offset, 0, iosize);
2817 flush_dcache_page(page);
2818 kunmap_atomic(userpage);
2819 set_extent_uptodate(tree, cur, cur + iosize - 1,
2821 unlock_extent_cached(tree, cur, cur + iosize - 1,
2825 em = get_extent(inode, page, pg_offset, cur,
2827 if (IS_ERR_OR_NULL(em)) {
2829 unlock_extent(tree, cur, end);
2832 extent_offset = cur - em->start;
2833 BUG_ON(extent_map_end(em) <= cur);
2836 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2837 this_bio_flag = EXTENT_BIO_COMPRESSED;
2838 extent_set_compress_type(&this_bio_flag,
2842 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2843 cur_end = min(extent_map_end(em) - 1, end);
2844 iosize = ALIGN(iosize, blocksize);
2845 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2846 disk_io_size = em->block_len;
2847 sector = em->block_start >> 9;
2849 sector = (em->block_start + extent_offset) >> 9;
2850 disk_io_size = iosize;
2853 block_start = em->block_start;
2854 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2855 block_start = EXTENT_MAP_HOLE;
2856 free_extent_map(em);
2859 /* we've found a hole, just zero and go on */
2860 if (block_start == EXTENT_MAP_HOLE) {
2862 struct extent_state *cached = NULL;
2864 userpage = kmap_atomic(page);
2865 memset(userpage + pg_offset, 0, iosize);
2866 flush_dcache_page(page);
2867 kunmap_atomic(userpage);
2869 set_extent_uptodate(tree, cur, cur + iosize - 1,
2871 unlock_extent_cached(tree, cur, cur + iosize - 1,
2874 pg_offset += iosize;
2877 /* the get_extent function already copied into the page */
2878 if (test_range_bit(tree, cur, cur_end,
2879 EXTENT_UPTODATE, 1, NULL)) {
2880 check_page_uptodate(tree, page);
2881 unlock_extent(tree, cur, cur + iosize - 1);
2883 pg_offset += iosize;
2886 /* we have an inline extent but it didn't get marked up
2887 * to date. Error out
2889 if (block_start == EXTENT_MAP_INLINE) {
2891 unlock_extent(tree, cur, cur + iosize - 1);
2893 pg_offset += iosize;
2898 ret = submit_extent_page(rw, tree, page,
2899 sector, disk_io_size, pg_offset,
2901 end_bio_extent_readpage, mirror_num,
2906 *bio_flags = this_bio_flag;
2909 unlock_extent(tree, cur, cur + iosize - 1);
2912 pg_offset += iosize;
2916 if (!PageError(page))
2917 SetPageUptodate(page);
2923 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2924 get_extent_t *get_extent, int mirror_num)
2926 struct bio *bio = NULL;
2927 unsigned long bio_flags = 0;
2930 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2933 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2937 static noinline void update_nr_written(struct page *page,
2938 struct writeback_control *wbc,
2939 unsigned long nr_written)
2941 wbc->nr_to_write -= nr_written;
2942 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2943 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2944 page->mapping->writeback_index = page->index + nr_written;
2948 * the writepage semantics are similar to regular writepage. extent
2949 * records are inserted to lock ranges in the tree, and as dirty areas
2950 * are found, they are marked writeback. Then the lock bits are removed
2951 * and the end_io handler clears the writeback ranges
2953 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2956 struct inode *inode = page->mapping->host;
2957 struct extent_page_data *epd = data;
2958 struct extent_io_tree *tree = epd->tree;
2959 u64 start = page_offset(page);
2961 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2965 u64 last_byte = i_size_read(inode);
2969 struct extent_state *cached_state = NULL;
2970 struct extent_map *em;
2971 struct block_device *bdev;
2974 size_t pg_offset = 0;
2976 loff_t i_size = i_size_read(inode);
2977 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2983 unsigned long nr_written = 0;
2984 bool fill_delalloc = true;
2986 if (wbc->sync_mode == WB_SYNC_ALL)
2987 write_flags = WRITE_SYNC;
2989 write_flags = WRITE;
2991 trace___extent_writepage(page, inode, wbc);
2993 WARN_ON(!PageLocked(page));
2995 ClearPageError(page);
2997 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2998 if (page->index > end_index ||
2999 (page->index == end_index && !pg_offset)) {
3000 page->mapping->a_ops->invalidatepage(page, 0);
3005 if (page->index == end_index) {
3008 userpage = kmap_atomic(page);
3009 memset(userpage + pg_offset, 0,
3010 PAGE_CACHE_SIZE - pg_offset);
3011 kunmap_atomic(userpage);
3012 flush_dcache_page(page);
3016 set_page_extent_mapped(page);
3018 if (!tree->ops || !tree->ops->fill_delalloc)
3019 fill_delalloc = false;
3021 delalloc_start = start;
3024 if (!epd->extent_locked && fill_delalloc) {
3025 u64 delalloc_to_write = 0;
3027 * make sure the wbc mapping index is at least updated
3030 update_nr_written(page, wbc, 0);
3032 while (delalloc_end < page_end) {
3033 nr_delalloc = find_lock_delalloc_range(inode, tree,
3038 if (nr_delalloc == 0) {
3039 delalloc_start = delalloc_end + 1;
3042 ret = tree->ops->fill_delalloc(inode, page,
3047 /* File system has been set read-only */
3053 * delalloc_end is already one less than the total
3054 * length, so we don't subtract one from
3057 delalloc_to_write += (delalloc_end - delalloc_start +
3060 delalloc_start = delalloc_end + 1;
3062 if (wbc->nr_to_write < delalloc_to_write) {
3065 if (delalloc_to_write < thresh * 2)
3066 thresh = delalloc_to_write;
3067 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3071 /* did the fill delalloc function already unlock and start
3077 * we've unlocked the page, so we can't update
3078 * the mapping's writeback index, just update
3081 wbc->nr_to_write -= nr_written;
3085 if (tree->ops && tree->ops->writepage_start_hook) {
3086 ret = tree->ops->writepage_start_hook(page, start,
3089 /* Fixup worker will requeue */
3091 wbc->pages_skipped++;
3093 redirty_page_for_writepage(wbc, page);
3094 update_nr_written(page, wbc, nr_written);
3102 * we don't want to touch the inode after unlocking the page,
3103 * so we update the mapping writeback index now
3105 update_nr_written(page, wbc, nr_written + 1);
3108 if (last_byte <= start) {
3109 if (tree->ops && tree->ops->writepage_end_io_hook)
3110 tree->ops->writepage_end_io_hook(page, start,
3115 blocksize = inode->i_sb->s_blocksize;
3117 while (cur <= end) {
3118 if (cur >= last_byte) {
3119 if (tree->ops && tree->ops->writepage_end_io_hook)
3120 tree->ops->writepage_end_io_hook(page, cur,
3124 em = epd->get_extent(inode, page, pg_offset, cur,
3126 if (IS_ERR_OR_NULL(em)) {
3131 extent_offset = cur - em->start;
3132 BUG_ON(extent_map_end(em) <= cur);
3134 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3135 iosize = ALIGN(iosize, blocksize);
3136 sector = (em->block_start + extent_offset) >> 9;
3138 block_start = em->block_start;
3139 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3140 free_extent_map(em);
3144 * compressed and inline extents are written through other
3147 if (compressed || block_start == EXTENT_MAP_HOLE ||
3148 block_start == EXTENT_MAP_INLINE) {
3150 * end_io notification does not happen here for
3151 * compressed extents
3153 if (!compressed && tree->ops &&
3154 tree->ops->writepage_end_io_hook)
3155 tree->ops->writepage_end_io_hook(page, cur,
3158 else if (compressed) {
3159 /* we don't want to end_page_writeback on
3160 * a compressed extent. this happens
3167 pg_offset += iosize;
3170 /* leave this out until we have a page_mkwrite call */
3171 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
3172 EXTENT_DIRTY, 0, NULL)) {
3174 pg_offset += iosize;
3178 if (tree->ops && tree->ops->writepage_io_hook) {
3179 ret = tree->ops->writepage_io_hook(page, cur,
3187 unsigned long max_nr = end_index + 1;
3189 set_range_writeback(tree, cur, cur + iosize - 1);
3190 if (!PageWriteback(page)) {
3191 printk(KERN_ERR "btrfs warning page %lu not "
3192 "writeback, cur %llu end %llu\n",
3193 page->index, (unsigned long long)cur,
3194 (unsigned long long)end);
3197 ret = submit_extent_page(write_flags, tree, page,
3198 sector, iosize, pg_offset,
3199 bdev, &epd->bio, max_nr,
3200 end_bio_extent_writepage,
3206 pg_offset += iosize;
3211 /* make sure the mapping tag for page dirty gets cleared */
3212 set_page_writeback(page);
3213 end_page_writeback(page);
3219 /* drop our reference on any cached states */
3220 free_extent_state(cached_state);
3224 static int eb_wait(void *word)
3230 void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3232 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3233 TASK_UNINTERRUPTIBLE);
3236 static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3237 struct btrfs_fs_info *fs_info,
3238 struct extent_page_data *epd)
3240 unsigned long i, num_pages;
3244 if (!btrfs_try_tree_write_lock(eb)) {
3246 flush_write_bio(epd);
3247 btrfs_tree_lock(eb);
3250 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3251 btrfs_tree_unlock(eb);
3255 flush_write_bio(epd);
3259 wait_on_extent_buffer_writeback(eb);
3260 btrfs_tree_lock(eb);
3261 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3263 btrfs_tree_unlock(eb);
3268 * We need to do this to prevent races in people who check if the eb is
3269 * under IO since we can end up having no IO bits set for a short period
3272 spin_lock(&eb->refs_lock);
3273 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3274 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3275 spin_unlock(&eb->refs_lock);
3276 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3277 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3279 fs_info->dirty_metadata_batch);
3282 spin_unlock(&eb->refs_lock);
3285 btrfs_tree_unlock(eb);
3290 num_pages = num_extent_pages(eb->start, eb->len);
3291 for (i = 0; i < num_pages; i++) {
3292 struct page *p = extent_buffer_page(eb, i);
3294 if (!trylock_page(p)) {
3296 flush_write_bio(epd);
3306 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3308 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3309 smp_mb__after_clear_bit();
3310 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3313 static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3315 int uptodate = err == 0;
3316 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3317 struct extent_buffer *eb;
3321 struct page *page = bvec->bv_page;
3324 eb = (struct extent_buffer *)page->private;
3326 done = atomic_dec_and_test(&eb->io_pages);
3328 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3329 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3330 ClearPageUptodate(page);
3334 end_page_writeback(page);
3339 end_extent_buffer_writeback(eb);
3340 } while (bvec >= bio->bi_io_vec);
3346 static int write_one_eb(struct extent_buffer *eb,
3347 struct btrfs_fs_info *fs_info,
3348 struct writeback_control *wbc,
3349 struct extent_page_data *epd)
3351 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3352 u64 offset = eb->start;
3353 unsigned long i, num_pages;
3354 unsigned long bio_flags = 0;
3355 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
3358 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3359 num_pages = num_extent_pages(eb->start, eb->len);
3360 atomic_set(&eb->io_pages, num_pages);
3361 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3362 bio_flags = EXTENT_BIO_TREE_LOG;
3364 for (i = 0; i < num_pages; i++) {
3365 struct page *p = extent_buffer_page(eb, i);
3367 clear_page_dirty_for_io(p);
3368 set_page_writeback(p);
3369 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3370 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3371 -1, end_bio_extent_buffer_writepage,
3372 0, epd->bio_flags, bio_flags);
3373 epd->bio_flags = bio_flags;
3375 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3377 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3378 end_extent_buffer_writeback(eb);
3382 offset += PAGE_CACHE_SIZE;
3383 update_nr_written(p, wbc, 1);
3387 if (unlikely(ret)) {
3388 for (; i < num_pages; i++) {
3389 struct page *p = extent_buffer_page(eb, i);
3397 int btree_write_cache_pages(struct address_space *mapping,
3398 struct writeback_control *wbc)
3400 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3401 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3402 struct extent_buffer *eb, *prev_eb = NULL;
3403 struct extent_page_data epd = {
3407 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3412 int nr_to_write_done = 0;
3413 struct pagevec pvec;
3416 pgoff_t end; /* Inclusive */
3420 pagevec_init(&pvec, 0);
3421 if (wbc->range_cyclic) {
3422 index = mapping->writeback_index; /* Start from prev offset */
3425 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3426 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3429 if (wbc->sync_mode == WB_SYNC_ALL)
3430 tag = PAGECACHE_TAG_TOWRITE;
3432 tag = PAGECACHE_TAG_DIRTY;
3434 if (wbc->sync_mode == WB_SYNC_ALL)
3435 tag_pages_for_writeback(mapping, index, end);
3436 while (!done && !nr_to_write_done && (index <= end) &&
3437 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3438 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3442 for (i = 0; i < nr_pages; i++) {
3443 struct page *page = pvec.pages[i];
3445 if (!PagePrivate(page))
3448 if (!wbc->range_cyclic && page->index > end) {
3453 spin_lock(&mapping->private_lock);
3454 if (!PagePrivate(page)) {
3455 spin_unlock(&mapping->private_lock);
3459 eb = (struct extent_buffer *)page->private;
3462 * Shouldn't happen and normally this would be a BUG_ON
3463 * but no sense in crashing the users box for something
3464 * we can survive anyway.
3467 spin_unlock(&mapping->private_lock);
3472 if (eb == prev_eb) {
3473 spin_unlock(&mapping->private_lock);
3477 ret = atomic_inc_not_zero(&eb->refs);
3478 spin_unlock(&mapping->private_lock);
3483 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3485 free_extent_buffer(eb);
3489 ret = write_one_eb(eb, fs_info, wbc, &epd);
3492 free_extent_buffer(eb);
3495 free_extent_buffer(eb);
3498 * the filesystem may choose to bump up nr_to_write.
3499 * We have to make sure to honor the new nr_to_write
3502 nr_to_write_done = wbc->nr_to_write <= 0;
3504 pagevec_release(&pvec);
3507 if (!scanned && !done) {
3509 * We hit the last page and there is more work to be done: wrap
3510 * back to the start of the file
3516 flush_write_bio(&epd);
3521 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3522 * @mapping: address space structure to write
3523 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3524 * @writepage: function called for each page
3525 * @data: data passed to writepage function
3527 * If a page is already under I/O, write_cache_pages() skips it, even
3528 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3529 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3530 * and msync() need to guarantee that all the data which was dirty at the time
3531 * the call was made get new I/O started against them. If wbc->sync_mode is
3532 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3533 * existing IO to complete.
3535 static int extent_write_cache_pages(struct extent_io_tree *tree,
3536 struct address_space *mapping,
3537 struct writeback_control *wbc,
3538 writepage_t writepage, void *data,
3539 void (*flush_fn)(void *))
3541 struct inode *inode = mapping->host;
3544 int nr_to_write_done = 0;
3545 struct pagevec pvec;
3548 pgoff_t end; /* Inclusive */
3553 * We have to hold onto the inode so that ordered extents can do their
3554 * work when the IO finishes. The alternative to this is failing to add
3555 * an ordered extent if the igrab() fails there and that is a huge pain
3556 * to deal with, so instead just hold onto the inode throughout the
3557 * writepages operation. If it fails here we are freeing up the inode
3558 * anyway and we'd rather not waste our time writing out stuff that is
3559 * going to be truncated anyway.
3564 pagevec_init(&pvec, 0);
3565 if (wbc->range_cyclic) {
3566 index = mapping->writeback_index; /* Start from prev offset */
3569 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3570 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3573 if (wbc->sync_mode == WB_SYNC_ALL)
3574 tag = PAGECACHE_TAG_TOWRITE;
3576 tag = PAGECACHE_TAG_DIRTY;
3578 if (wbc->sync_mode == WB_SYNC_ALL)
3579 tag_pages_for_writeback(mapping, index, end);
3580 while (!done && !nr_to_write_done && (index <= end) &&
3581 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3582 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3586 for (i = 0; i < nr_pages; i++) {
3587 struct page *page = pvec.pages[i];
3590 * At this point we hold neither mapping->tree_lock nor
3591 * lock on the page itself: the page may be truncated or
3592 * invalidated (changing page->mapping to NULL), or even
3593 * swizzled back from swapper_space to tmpfs file
3596 if (!trylock_page(page)) {
3601 if (unlikely(page->mapping != mapping)) {
3606 if (!wbc->range_cyclic && page->index > end) {
3612 if (wbc->sync_mode != WB_SYNC_NONE) {
3613 if (PageWriteback(page))
3615 wait_on_page_writeback(page);
3618 if (PageWriteback(page) ||
3619 !clear_page_dirty_for_io(page)) {
3624 ret = (*writepage)(page, wbc, data);
3626 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3634 * the filesystem may choose to bump up nr_to_write.
3635 * We have to make sure to honor the new nr_to_write
3638 nr_to_write_done = wbc->nr_to_write <= 0;
3640 pagevec_release(&pvec);
3643 if (!scanned && !done) {
3645 * We hit the last page and there is more work to be done: wrap
3646 * back to the start of the file
3652 btrfs_add_delayed_iput(inode);
3656 static void flush_epd_write_bio(struct extent_page_data *epd)
3665 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
3666 BUG_ON(ret < 0); /* -ENOMEM */
3671 static noinline void flush_write_bio(void *data)
3673 struct extent_page_data *epd = data;
3674 flush_epd_write_bio(epd);
3677 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3678 get_extent_t *get_extent,
3679 struct writeback_control *wbc)
3682 struct extent_page_data epd = {
3685 .get_extent = get_extent,
3687 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3691 ret = __extent_writepage(page, wbc, &epd);
3693 flush_epd_write_bio(&epd);
3697 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3698 u64 start, u64 end, get_extent_t *get_extent,
3702 struct address_space *mapping = inode->i_mapping;
3704 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3707 struct extent_page_data epd = {
3710 .get_extent = get_extent,
3712 .sync_io = mode == WB_SYNC_ALL,
3715 struct writeback_control wbc_writepages = {
3717 .nr_to_write = nr_pages * 2,
3718 .range_start = start,
3719 .range_end = end + 1,
3722 while (start <= end) {
3723 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3724 if (clear_page_dirty_for_io(page))
3725 ret = __extent_writepage(page, &wbc_writepages, &epd);
3727 if (tree->ops && tree->ops->writepage_end_io_hook)
3728 tree->ops->writepage_end_io_hook(page, start,
3729 start + PAGE_CACHE_SIZE - 1,
3733 page_cache_release(page);
3734 start += PAGE_CACHE_SIZE;
3737 flush_epd_write_bio(&epd);
3741 int extent_writepages(struct extent_io_tree *tree,
3742 struct address_space *mapping,
3743 get_extent_t *get_extent,
3744 struct writeback_control *wbc)
3747 struct extent_page_data epd = {
3750 .get_extent = get_extent,
3752 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3756 ret = extent_write_cache_pages(tree, mapping, wbc,
3757 __extent_writepage, &epd,
3759 flush_epd_write_bio(&epd);
3763 int extent_readpages(struct extent_io_tree *tree,
3764 struct address_space *mapping,
3765 struct list_head *pages, unsigned nr_pages,
3766 get_extent_t get_extent)
3768 struct bio *bio = NULL;
3770 unsigned long bio_flags = 0;
3771 struct page *pagepool[16];
3776 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3777 page = list_entry(pages->prev, struct page, lru);
3779 prefetchw(&page->flags);
3780 list_del(&page->lru);
3781 if (add_to_page_cache_lru(page, mapping,
3782 page->index, GFP_NOFS)) {
3783 page_cache_release(page);
3787 pagepool[nr++] = page;
3788 if (nr < ARRAY_SIZE(pagepool))
3790 for (i = 0; i < nr; i++) {
3791 __extent_read_full_page(tree, pagepool[i], get_extent,
3792 &bio, 0, &bio_flags, READ);
3793 page_cache_release(pagepool[i]);
3797 for (i = 0; i < nr; i++) {
3798 __extent_read_full_page(tree, pagepool[i], get_extent,
3799 &bio, 0, &bio_flags, READ);
3800 page_cache_release(pagepool[i]);
3803 BUG_ON(!list_empty(pages));
3805 return submit_one_bio(READ, bio, 0, bio_flags);
3810 * basic invalidatepage code, this waits on any locked or writeback
3811 * ranges corresponding to the page, and then deletes any extent state
3812 * records from the tree
3814 int extent_invalidatepage(struct extent_io_tree *tree,
3815 struct page *page, unsigned long offset)
3817 struct extent_state *cached_state = NULL;
3818 u64 start = page_offset(page);
3819 u64 end = start + PAGE_CACHE_SIZE - 1;
3820 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3822 start += ALIGN(offset, blocksize);
3826 lock_extent_bits(tree, start, end, 0, &cached_state);
3827 wait_on_page_writeback(page);
3828 clear_extent_bit(tree, start, end,
3829 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3830 EXTENT_DO_ACCOUNTING,
3831 1, 1, &cached_state, GFP_NOFS);
3836 * a helper for releasepage, this tests for areas of the page that
3837 * are locked or under IO and drops the related state bits if it is safe
3840 static int try_release_extent_state(struct extent_map_tree *map,
3841 struct extent_io_tree *tree,
3842 struct page *page, gfp_t mask)
3844 u64 start = page_offset(page);
3845 u64 end = start + PAGE_CACHE_SIZE - 1;
3848 if (test_range_bit(tree, start, end,
3849 EXTENT_IOBITS, 0, NULL))
3852 if ((mask & GFP_NOFS) == GFP_NOFS)
3855 * at this point we can safely clear everything except the
3856 * locked bit and the nodatasum bit
3858 ret = clear_extent_bit(tree, start, end,
3859 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3862 /* if clear_extent_bit failed for enomem reasons,
3863 * we can't allow the release to continue.
3874 * a helper for releasepage. As long as there are no locked extents
3875 * in the range corresponding to the page, both state records and extent
3876 * map records are removed
3878 int try_release_extent_mapping(struct extent_map_tree *map,
3879 struct extent_io_tree *tree, struct page *page,
3882 struct extent_map *em;
3883 u64 start = page_offset(page);
3884 u64 end = start + PAGE_CACHE_SIZE - 1;
3886 if ((mask & __GFP_WAIT) &&
3887 page->mapping->host->i_size > 16 * 1024 * 1024) {
3889 while (start <= end) {
3890 len = end - start + 1;
3891 write_lock(&map->lock);
3892 em = lookup_extent_mapping(map, start, len);
3894 write_unlock(&map->lock);
3897 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3898 em->start != start) {
3899 write_unlock(&map->lock);
3900 free_extent_map(em);
3903 if (!test_range_bit(tree, em->start,
3904 extent_map_end(em) - 1,
3905 EXTENT_LOCKED | EXTENT_WRITEBACK,
3907 remove_extent_mapping(map, em);
3908 /* once for the rb tree */
3909 free_extent_map(em);
3911 start = extent_map_end(em);
3912 write_unlock(&map->lock);
3915 free_extent_map(em);
3918 return try_release_extent_state(map, tree, page, mask);
3922 * helper function for fiemap, which doesn't want to see any holes.
3923 * This maps until we find something past 'last'
3925 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3928 get_extent_t *get_extent)
3930 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3931 struct extent_map *em;
3938 len = last - offset;
3941 len = ALIGN(len, sectorsize);
3942 em = get_extent(inode, NULL, 0, offset, len, 0);
3943 if (IS_ERR_OR_NULL(em))
3946 /* if this isn't a hole return it */
3947 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3948 em->block_start != EXTENT_MAP_HOLE) {
3952 /* this is a hole, advance to the next extent */
3953 offset = extent_map_end(em);
3954 free_extent_map(em);
3961 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3962 __u64 start, __u64 len, get_extent_t *get_extent)
3966 u64 max = start + len;
3970 u64 last_for_get_extent = 0;
3972 u64 isize = i_size_read(inode);
3973 struct btrfs_key found_key;
3974 struct extent_map *em = NULL;
3975 struct extent_state *cached_state = NULL;
3976 struct btrfs_path *path;
3977 struct btrfs_file_extent_item *item;
3982 unsigned long emflags;
3987 path = btrfs_alloc_path();
3990 path->leave_spinning = 1;
3992 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3993 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3996 * lookup the last file extent. We're not using i_size here
3997 * because there might be preallocation past i_size
3999 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
4000 path, btrfs_ino(inode), -1, 0);
4002 btrfs_free_path(path);
4007 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4008 struct btrfs_file_extent_item);
4009 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
4010 found_type = btrfs_key_type(&found_key);
4012 /* No extents, but there might be delalloc bits */
4013 if (found_key.objectid != btrfs_ino(inode) ||
4014 found_type != BTRFS_EXTENT_DATA_KEY) {
4015 /* have to trust i_size as the end */
4017 last_for_get_extent = isize;
4020 * remember the start of the last extent. There are a
4021 * bunch of different factors that go into the length of the
4022 * extent, so its much less complex to remember where it started
4024 last = found_key.offset;
4025 last_for_get_extent = last + 1;
4027 btrfs_free_path(path);
4030 * we might have some extents allocated but more delalloc past those
4031 * extents. so, we trust isize unless the start of the last extent is
4036 last_for_get_extent = isize;
4039 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
4042 em = get_extent_skip_holes(inode, start, last_for_get_extent,
4052 u64 offset_in_extent;
4054 /* break if the extent we found is outside the range */
4055 if (em->start >= max || extent_map_end(em) < off)
4059 * get_extent may return an extent that starts before our
4060 * requested range. We have to make sure the ranges
4061 * we return to fiemap always move forward and don't
4062 * overlap, so adjust the offsets here
4064 em_start = max(em->start, off);
4067 * record the offset from the start of the extent
4068 * for adjusting the disk offset below
4070 offset_in_extent = em_start - em->start;
4071 em_end = extent_map_end(em);
4072 em_len = em_end - em_start;
4073 emflags = em->flags;
4078 * bump off for our next call to get_extent
4080 off = extent_map_end(em);
4084 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
4086 flags |= FIEMAP_EXTENT_LAST;
4087 } else if (em->block_start == EXTENT_MAP_INLINE) {
4088 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4089 FIEMAP_EXTENT_NOT_ALIGNED);
4090 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
4091 flags |= (FIEMAP_EXTENT_DELALLOC |
4092 FIEMAP_EXTENT_UNKNOWN);
4094 disko = em->block_start + offset_in_extent;
4096 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4097 flags |= FIEMAP_EXTENT_ENCODED;
4099 free_extent_map(em);
4101 if ((em_start >= last) || em_len == (u64)-1 ||
4102 (last == (u64)-1 && isize <= em_end)) {
4103 flags |= FIEMAP_EXTENT_LAST;
4107 /* now scan forward to see if this is really the last extent. */
4108 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4115 flags |= FIEMAP_EXTENT_LAST;
4118 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4124 free_extent_map(em);
4126 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
4127 &cached_state, GFP_NOFS);
4131 static void __free_extent_buffer(struct extent_buffer *eb)
4133 btrfs_leak_debug_del(&eb->leak_list);
4134 kmem_cache_free(extent_buffer_cache, eb);
4137 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4142 struct extent_buffer *eb = NULL;
4144 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
4151 rwlock_init(&eb->lock);
4152 atomic_set(&eb->write_locks, 0);
4153 atomic_set(&eb->read_locks, 0);
4154 atomic_set(&eb->blocking_readers, 0);
4155 atomic_set(&eb->blocking_writers, 0);
4156 atomic_set(&eb->spinning_readers, 0);
4157 atomic_set(&eb->spinning_writers, 0);
4158 eb->lock_nested = 0;
4159 init_waitqueue_head(&eb->write_lock_wq);
4160 init_waitqueue_head(&eb->read_lock_wq);
4162 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4164 spin_lock_init(&eb->refs_lock);
4165 atomic_set(&eb->refs, 1);
4166 atomic_set(&eb->io_pages, 0);
4169 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4171 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4172 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4173 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4178 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4182 struct extent_buffer *new;
4183 unsigned long num_pages = num_extent_pages(src->start, src->len);
4185 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4189 for (i = 0; i < num_pages; i++) {
4190 p = alloc_page(GFP_ATOMIC);
4192 attach_extent_buffer_page(new, p);
4193 WARN_ON(PageDirty(p));
4198 copy_extent_buffer(new, src, 0, 0, src->len);
4199 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4200 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4205 struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4207 struct extent_buffer *eb;
4208 unsigned long num_pages = num_extent_pages(0, len);
4211 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4215 for (i = 0; i < num_pages; i++) {
4216 eb->pages[i] = alloc_page(GFP_ATOMIC);
4220 set_extent_buffer_uptodate(eb);
4221 btrfs_set_header_nritems(eb, 0);
4222 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4227 __free_page(eb->pages[i - 1]);
4228 __free_extent_buffer(eb);
4232 static int extent_buffer_under_io(struct extent_buffer *eb)
4234 return (atomic_read(&eb->io_pages) ||
4235 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4236 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4240 * Helper for releasing extent buffer page.
4242 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4243 unsigned long start_idx)
4245 unsigned long index;
4246 unsigned long num_pages;
4248 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4250 BUG_ON(extent_buffer_under_io(eb));
4252 num_pages = num_extent_pages(eb->start, eb->len);
4253 index = start_idx + num_pages;
4254 if (start_idx >= index)
4259 page = extent_buffer_page(eb, index);
4260 if (page && mapped) {
4261 spin_lock(&page->mapping->private_lock);
4263 * We do this since we'll remove the pages after we've
4264 * removed the eb from the radix tree, so we could race
4265 * and have this page now attached to the new eb. So
4266 * only clear page_private if it's still connected to
4269 if (PagePrivate(page) &&
4270 page->private == (unsigned long)eb) {
4271 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4272 BUG_ON(PageDirty(page));
4273 BUG_ON(PageWriteback(page));
4275 * We need to make sure we haven't be attached
4278 ClearPagePrivate(page);
4279 set_page_private(page, 0);
4280 /* One for the page private */
4281 page_cache_release(page);
4283 spin_unlock(&page->mapping->private_lock);
4287 /* One for when we alloced the page */
4288 page_cache_release(page);
4290 } while (index != start_idx);
4294 * Helper for releasing the extent buffer.
4296 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4298 btrfs_release_extent_buffer_page(eb, 0);
4299 __free_extent_buffer(eb);
4302 static void check_buffer_tree_ref(struct extent_buffer *eb)
4305 /* the ref bit is tricky. We have to make sure it is set
4306 * if we have the buffer dirty. Otherwise the
4307 * code to free a buffer can end up dropping a dirty
4310 * Once the ref bit is set, it won't go away while the
4311 * buffer is dirty or in writeback, and it also won't
4312 * go away while we have the reference count on the
4315 * We can't just set the ref bit without bumping the
4316 * ref on the eb because free_extent_buffer might
4317 * see the ref bit and try to clear it. If this happens
4318 * free_extent_buffer might end up dropping our original
4319 * ref by mistake and freeing the page before we are able
4320 * to add one more ref.
4322 * So bump the ref count first, then set the bit. If someone
4323 * beat us to it, drop the ref we added.
4325 refs = atomic_read(&eb->refs);
4326 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4329 spin_lock(&eb->refs_lock);
4330 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4331 atomic_inc(&eb->refs);
4332 spin_unlock(&eb->refs_lock);
4335 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4337 unsigned long num_pages, i;
4339 check_buffer_tree_ref(eb);
4341 num_pages = num_extent_pages(eb->start, eb->len);
4342 for (i = 0; i < num_pages; i++) {
4343 struct page *p = extent_buffer_page(eb, i);
4344 mark_page_accessed(p);
4348 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
4349 u64 start, unsigned long len)
4351 unsigned long num_pages = num_extent_pages(start, len);
4353 unsigned long index = start >> PAGE_CACHE_SHIFT;
4354 struct extent_buffer *eb;
4355 struct extent_buffer *exists = NULL;
4357 struct address_space *mapping = tree->mapping;
4362 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4363 if (eb && atomic_inc_not_zero(&eb->refs)) {
4365 mark_extent_buffer_accessed(eb);
4370 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
4374 for (i = 0; i < num_pages; i++, index++) {
4375 p = find_or_create_page(mapping, index, GFP_NOFS);
4379 spin_lock(&mapping->private_lock);
4380 if (PagePrivate(p)) {
4382 * We could have already allocated an eb for this page
4383 * and attached one so lets see if we can get a ref on
4384 * the existing eb, and if we can we know it's good and
4385 * we can just return that one, else we know we can just
4386 * overwrite page->private.
4388 exists = (struct extent_buffer *)p->private;
4389 if (atomic_inc_not_zero(&exists->refs)) {
4390 spin_unlock(&mapping->private_lock);
4392 page_cache_release(p);
4393 mark_extent_buffer_accessed(exists);
4398 * Do this so attach doesn't complain and we need to
4399 * drop the ref the old guy had.
4401 ClearPagePrivate(p);
4402 WARN_ON(PageDirty(p));
4403 page_cache_release(p);
4405 attach_extent_buffer_page(eb, p);
4406 spin_unlock(&mapping->private_lock);
4407 WARN_ON(PageDirty(p));
4408 mark_page_accessed(p);
4410 if (!PageUptodate(p))
4414 * see below about how we avoid a nasty race with release page
4415 * and why we unlock later
4419 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4421 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4425 spin_lock(&tree->buffer_lock);
4426 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4427 if (ret == -EEXIST) {
4428 exists = radix_tree_lookup(&tree->buffer,
4429 start >> PAGE_CACHE_SHIFT);
4430 if (!atomic_inc_not_zero(&exists->refs)) {
4431 spin_unlock(&tree->buffer_lock);
4432 radix_tree_preload_end();
4436 spin_unlock(&tree->buffer_lock);
4437 radix_tree_preload_end();
4438 mark_extent_buffer_accessed(exists);
4441 /* add one reference for the tree */
4442 check_buffer_tree_ref(eb);
4443 spin_unlock(&tree->buffer_lock);
4444 radix_tree_preload_end();
4447 * there is a race where release page may have
4448 * tried to find this extent buffer in the radix
4449 * but failed. It will tell the VM it is safe to
4450 * reclaim the, and it will clear the page private bit.
4451 * We must make sure to set the page private bit properly
4452 * after the extent buffer is in the radix tree so
4453 * it doesn't get lost
4455 SetPageChecked(eb->pages[0]);
4456 for (i = 1; i < num_pages; i++) {
4457 p = extent_buffer_page(eb, i);
4458 ClearPageChecked(p);
4461 unlock_page(eb->pages[0]);
4465 for (i = 0; i < num_pages; i++) {
4467 unlock_page(eb->pages[i]);
4470 WARN_ON(!atomic_dec_and_test(&eb->refs));
4471 btrfs_release_extent_buffer(eb);
4475 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
4476 u64 start, unsigned long len)
4478 struct extent_buffer *eb;
4481 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4482 if (eb && atomic_inc_not_zero(&eb->refs)) {
4484 mark_extent_buffer_accessed(eb);
4492 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4494 struct extent_buffer *eb =
4495 container_of(head, struct extent_buffer, rcu_head);
4497 __free_extent_buffer(eb);
4500 /* Expects to have eb->eb_lock already held */
4501 static int release_extent_buffer(struct extent_buffer *eb)
4503 WARN_ON(atomic_read(&eb->refs) == 0);
4504 if (atomic_dec_and_test(&eb->refs)) {
4505 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4506 spin_unlock(&eb->refs_lock);
4508 struct extent_io_tree *tree = eb->tree;
4510 spin_unlock(&eb->refs_lock);
4512 spin_lock(&tree->buffer_lock);
4513 radix_tree_delete(&tree->buffer,
4514 eb->start >> PAGE_CACHE_SHIFT);
4515 spin_unlock(&tree->buffer_lock);
4518 /* Should be safe to release our pages at this point */
4519 btrfs_release_extent_buffer_page(eb, 0);
4520 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4523 spin_unlock(&eb->refs_lock);
4528 void free_extent_buffer(struct extent_buffer *eb)
4536 refs = atomic_read(&eb->refs);
4539 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4544 spin_lock(&eb->refs_lock);
4545 if (atomic_read(&eb->refs) == 2 &&
4546 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4547 atomic_dec(&eb->refs);
4549 if (atomic_read(&eb->refs) == 2 &&
4550 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
4551 !extent_buffer_under_io(eb) &&
4552 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4553 atomic_dec(&eb->refs);
4556 * I know this is terrible, but it's temporary until we stop tracking
4557 * the uptodate bits and such for the extent buffers.
4559 release_extent_buffer(eb);
4562 void free_extent_buffer_stale(struct extent_buffer *eb)
4567 spin_lock(&eb->refs_lock);
4568 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4570 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
4571 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4572 atomic_dec(&eb->refs);
4573 release_extent_buffer(eb);
4576 void clear_extent_buffer_dirty(struct extent_buffer *eb)
4579 unsigned long num_pages;
4582 num_pages = num_extent_pages(eb->start, eb->len);
4584 for (i = 0; i < num_pages; i++) {
4585 page = extent_buffer_page(eb, i);
4586 if (!PageDirty(page))
4590 WARN_ON(!PagePrivate(page));
4592 clear_page_dirty_for_io(page);
4593 spin_lock_irq(&page->mapping->tree_lock);
4594 if (!PageDirty(page)) {
4595 radix_tree_tag_clear(&page->mapping->page_tree,
4597 PAGECACHE_TAG_DIRTY);
4599 spin_unlock_irq(&page->mapping->tree_lock);
4600 ClearPageError(page);
4603 WARN_ON(atomic_read(&eb->refs) == 0);
4606 int set_extent_buffer_dirty(struct extent_buffer *eb)
4609 unsigned long num_pages;
4612 check_buffer_tree_ref(eb);
4614 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4616 num_pages = num_extent_pages(eb->start, eb->len);
4617 WARN_ON(atomic_read(&eb->refs) == 0);
4618 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4620 for (i = 0; i < num_pages; i++)
4621 set_page_dirty(extent_buffer_page(eb, i));
4625 int clear_extent_buffer_uptodate(struct extent_buffer *eb)
4629 unsigned long num_pages;
4631 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4632 num_pages = num_extent_pages(eb->start, eb->len);
4633 for (i = 0; i < num_pages; i++) {
4634 page = extent_buffer_page(eb, i);
4636 ClearPageUptodate(page);
4641 int set_extent_buffer_uptodate(struct extent_buffer *eb)
4645 unsigned long num_pages;
4647 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4648 num_pages = num_extent_pages(eb->start, eb->len);
4649 for (i = 0; i < num_pages; i++) {
4650 page = extent_buffer_page(eb, i);
4651 SetPageUptodate(page);
4656 int extent_buffer_uptodate(struct extent_buffer *eb)
4658 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4661 int read_extent_buffer_pages(struct extent_io_tree *tree,
4662 struct extent_buffer *eb, u64 start, int wait,
4663 get_extent_t *get_extent, int mirror_num)
4666 unsigned long start_i;
4670 int locked_pages = 0;
4671 int all_uptodate = 1;
4672 unsigned long num_pages;
4673 unsigned long num_reads = 0;
4674 struct bio *bio = NULL;
4675 unsigned long bio_flags = 0;
4677 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4681 WARN_ON(start < eb->start);
4682 start_i = (start >> PAGE_CACHE_SHIFT) -
4683 (eb->start >> PAGE_CACHE_SHIFT);
4688 num_pages = num_extent_pages(eb->start, eb->len);
4689 for (i = start_i; i < num_pages; i++) {
4690 page = extent_buffer_page(eb, i);
4691 if (wait == WAIT_NONE) {
4692 if (!trylock_page(page))
4698 if (!PageUptodate(page)) {
4705 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4709 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
4710 eb->read_mirror = 0;
4711 atomic_set(&eb->io_pages, num_reads);
4712 for (i = start_i; i < num_pages; i++) {
4713 page = extent_buffer_page(eb, i);
4714 if (!PageUptodate(page)) {
4715 ClearPageError(page);
4716 err = __extent_read_full_page(tree, page,
4718 mirror_num, &bio_flags,
4728 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
4734 if (ret || wait != WAIT_COMPLETE)
4737 for (i = start_i; i < num_pages; i++) {
4738 page = extent_buffer_page(eb, i);
4739 wait_on_page_locked(page);
4740 if (!PageUptodate(page))
4748 while (locked_pages > 0) {
4749 page = extent_buffer_page(eb, i);
4757 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4758 unsigned long start,
4765 char *dst = (char *)dstv;
4766 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4767 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4769 WARN_ON(start > eb->len);
4770 WARN_ON(start + len > eb->start + eb->len);
4772 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4775 page = extent_buffer_page(eb, i);
4777 cur = min(len, (PAGE_CACHE_SIZE - offset));
4778 kaddr = page_address(page);
4779 memcpy(dst, kaddr + offset, cur);
4788 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4789 unsigned long min_len, char **map,
4790 unsigned long *map_start,
4791 unsigned long *map_len)
4793 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4796 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4797 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4798 unsigned long end_i = (start_offset + start + min_len - 1) >>
4805 offset = start_offset;
4809 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4812 if (start + min_len > eb->len) {
4813 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4814 "wanted %lu %lu\n", (unsigned long long)eb->start,
4815 eb->len, start, min_len);
4819 p = extent_buffer_page(eb, i);
4820 kaddr = page_address(p);
4821 *map = kaddr + offset;
4822 *map_len = PAGE_CACHE_SIZE - offset;
4826 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4827 unsigned long start,
4834 char *ptr = (char *)ptrv;
4835 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4836 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4839 WARN_ON(start > eb->len);
4840 WARN_ON(start + len > eb->start + eb->len);
4842 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4845 page = extent_buffer_page(eb, i);
4847 cur = min(len, (PAGE_CACHE_SIZE - offset));
4849 kaddr = page_address(page);
4850 ret = memcmp(ptr, kaddr + offset, cur);
4862 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4863 unsigned long start, unsigned long len)
4869 char *src = (char *)srcv;
4870 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4871 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4873 WARN_ON(start > eb->len);
4874 WARN_ON(start + len > eb->start + eb->len);
4876 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4879 page = extent_buffer_page(eb, i);
4880 WARN_ON(!PageUptodate(page));
4882 cur = min(len, PAGE_CACHE_SIZE - offset);
4883 kaddr = page_address(page);
4884 memcpy(kaddr + offset, src, cur);
4893 void memset_extent_buffer(struct extent_buffer *eb, char c,
4894 unsigned long start, unsigned long len)
4900 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4901 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4903 WARN_ON(start > eb->len);
4904 WARN_ON(start + len > eb->start + eb->len);
4906 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4909 page = extent_buffer_page(eb, i);
4910 WARN_ON(!PageUptodate(page));
4912 cur = min(len, PAGE_CACHE_SIZE - offset);
4913 kaddr = page_address(page);
4914 memset(kaddr + offset, c, cur);
4922 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4923 unsigned long dst_offset, unsigned long src_offset,
4926 u64 dst_len = dst->len;
4931 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4932 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4934 WARN_ON(src->len != dst_len);
4936 offset = (start_offset + dst_offset) &
4937 ((unsigned long)PAGE_CACHE_SIZE - 1);
4940 page = extent_buffer_page(dst, i);
4941 WARN_ON(!PageUptodate(page));
4943 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4945 kaddr = page_address(page);
4946 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4955 static void move_pages(struct page *dst_page, struct page *src_page,
4956 unsigned long dst_off, unsigned long src_off,
4959 char *dst_kaddr = page_address(dst_page);
4960 if (dst_page == src_page) {
4961 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4963 char *src_kaddr = page_address(src_page);
4964 char *p = dst_kaddr + dst_off + len;
4965 char *s = src_kaddr + src_off + len;
4972 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4974 unsigned long distance = (src > dst) ? src - dst : dst - src;
4975 return distance < len;
4978 static void copy_pages(struct page *dst_page, struct page *src_page,
4979 unsigned long dst_off, unsigned long src_off,
4982 char *dst_kaddr = page_address(dst_page);
4984 int must_memmove = 0;
4986 if (dst_page != src_page) {
4987 src_kaddr = page_address(src_page);
4989 src_kaddr = dst_kaddr;
4990 if (areas_overlap(src_off, dst_off, len))
4995 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4997 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
5000 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5001 unsigned long src_offset, unsigned long len)
5004 size_t dst_off_in_page;
5005 size_t src_off_in_page;
5006 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5007 unsigned long dst_i;
5008 unsigned long src_i;
5010 if (src_offset + len > dst->len) {
5011 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5012 "len %lu dst len %lu\n", src_offset, len, dst->len);
5015 if (dst_offset + len > dst->len) {
5016 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5017 "len %lu dst len %lu\n", dst_offset, len, dst->len);
5022 dst_off_in_page = (start_offset + dst_offset) &
5023 ((unsigned long)PAGE_CACHE_SIZE - 1);
5024 src_off_in_page = (start_offset + src_offset) &
5025 ((unsigned long)PAGE_CACHE_SIZE - 1);
5027 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5028 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
5030 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
5032 cur = min_t(unsigned long, cur,
5033 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
5035 copy_pages(extent_buffer_page(dst, dst_i),
5036 extent_buffer_page(dst, src_i),
5037 dst_off_in_page, src_off_in_page, cur);
5045 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5046 unsigned long src_offset, unsigned long len)
5049 size_t dst_off_in_page;
5050 size_t src_off_in_page;
5051 unsigned long dst_end = dst_offset + len - 1;
5052 unsigned long src_end = src_offset + len - 1;
5053 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5054 unsigned long dst_i;
5055 unsigned long src_i;
5057 if (src_offset + len > dst->len) {
5058 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5059 "len %lu len %lu\n", src_offset, len, dst->len);
5062 if (dst_offset + len > dst->len) {
5063 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5064 "len %lu len %lu\n", dst_offset, len, dst->len);
5067 if (dst_offset < src_offset) {
5068 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5072 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5073 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5075 dst_off_in_page = (start_offset + dst_end) &
5076 ((unsigned long)PAGE_CACHE_SIZE - 1);
5077 src_off_in_page = (start_offset + src_end) &
5078 ((unsigned long)PAGE_CACHE_SIZE - 1);
5080 cur = min_t(unsigned long, len, src_off_in_page + 1);
5081 cur = min(cur, dst_off_in_page + 1);
5082 move_pages(extent_buffer_page(dst, dst_i),
5083 extent_buffer_page(dst, src_i),
5084 dst_off_in_page - cur + 1,
5085 src_off_in_page - cur + 1, cur);
5093 int try_release_extent_buffer(struct page *page)
5095 struct extent_buffer *eb;
5098 * We need to make sure noboody is attaching this page to an eb right
5101 spin_lock(&page->mapping->private_lock);
5102 if (!PagePrivate(page)) {
5103 spin_unlock(&page->mapping->private_lock);
5107 eb = (struct extent_buffer *)page->private;
5111 * This is a little awful but should be ok, we need to make sure that
5112 * the eb doesn't disappear out from under us while we're looking at
5115 spin_lock(&eb->refs_lock);
5116 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
5117 spin_unlock(&eb->refs_lock);
5118 spin_unlock(&page->mapping->private_lock);
5121 spin_unlock(&page->mapping->private_lock);
5124 * If tree ref isn't set then we know the ref on this eb is a real ref,
5125 * so just return, this page will likely be freed soon anyway.
5127 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5128 spin_unlock(&eb->refs_lock);
5132 return release_extent_buffer(eb);