1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/module.h>
8 #include <linux/spinlock.h>
9 #include <linux/blkdev.h>
10 #include <linux/swap.h>
11 #include <linux/writeback.h>
12 #include <linux/pagevec.h>
13 #include <linux/prefetch.h>
14 #include <linux/cleancache.h>
15 #include "extent_io.h"
16 #include "extent_map.h"
19 #include "btrfs_inode.h"
21 #include "check-integrity.h"
23 static struct kmem_cache *extent_state_cache;
24 static struct kmem_cache *extent_buffer_cache;
26 static LIST_HEAD(buffers);
27 static LIST_HEAD(states);
31 static DEFINE_SPINLOCK(leak_lock);
34 #define BUFFER_LRU_MAX 64
39 struct rb_node rb_node;
42 struct extent_page_data {
44 struct extent_io_tree *tree;
45 get_extent_t *get_extent;
47 /* tells writepage not to lock the state bits for this range
48 * it still does the unlocking
50 unsigned int extent_locked:1;
52 /* tells the submit_bio code to use a WRITE_SYNC */
53 unsigned int sync_io:1;
56 static inline struct btrfs_fs_info *
57 tree_fs_info(struct extent_io_tree *tree)
59 return btrfs_sb(tree->mapping->host->i_sb);
62 int __init extent_io_init(void)
64 extent_state_cache = kmem_cache_create("extent_state",
65 sizeof(struct extent_state), 0,
66 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
67 if (!extent_state_cache)
70 extent_buffer_cache = kmem_cache_create("extent_buffers",
71 sizeof(struct extent_buffer), 0,
72 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
73 if (!extent_buffer_cache)
74 goto free_state_cache;
78 kmem_cache_destroy(extent_state_cache);
82 void extent_io_exit(void)
84 struct extent_state *state;
85 struct extent_buffer *eb;
87 while (!list_empty(&states)) {
88 state = list_entry(states.next, struct extent_state, leak_list);
89 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
90 "state %lu in tree %p refs %d\n",
91 (unsigned long long)state->start,
92 (unsigned long long)state->end,
93 state->state, state->tree, atomic_read(&state->refs));
94 list_del(&state->leak_list);
95 kmem_cache_free(extent_state_cache, state);
99 while (!list_empty(&buffers)) {
100 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
101 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
102 "refs %d\n", (unsigned long long)eb->start,
103 eb->len, atomic_read(&eb->refs));
104 list_del(&eb->leak_list);
105 kmem_cache_free(extent_buffer_cache, eb);
107 if (extent_state_cache)
108 kmem_cache_destroy(extent_state_cache);
109 if (extent_buffer_cache)
110 kmem_cache_destroy(extent_buffer_cache);
113 void extent_io_tree_init(struct extent_io_tree *tree,
114 struct address_space *mapping)
116 tree->state = RB_ROOT;
117 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
119 tree->dirty_bytes = 0;
120 spin_lock_init(&tree->lock);
121 spin_lock_init(&tree->buffer_lock);
122 tree->mapping = mapping;
125 static struct extent_state *alloc_extent_state(gfp_t mask)
127 struct extent_state *state;
132 state = kmem_cache_alloc(extent_state_cache, mask);
139 spin_lock_irqsave(&leak_lock, flags);
140 list_add(&state->leak_list, &states);
141 spin_unlock_irqrestore(&leak_lock, flags);
143 atomic_set(&state->refs, 1);
144 init_waitqueue_head(&state->wq);
145 trace_alloc_extent_state(state, mask, _RET_IP_);
149 void free_extent_state(struct extent_state *state)
153 if (atomic_dec_and_test(&state->refs)) {
157 WARN_ON(state->tree);
159 spin_lock_irqsave(&leak_lock, flags);
160 list_del(&state->leak_list);
161 spin_unlock_irqrestore(&leak_lock, flags);
163 trace_free_extent_state(state, _RET_IP_);
164 kmem_cache_free(extent_state_cache, state);
168 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
169 struct rb_node *node)
171 struct rb_node **p = &root->rb_node;
172 struct rb_node *parent = NULL;
173 struct tree_entry *entry;
177 entry = rb_entry(parent, struct tree_entry, rb_node);
179 if (offset < entry->start)
181 else if (offset > entry->end)
187 entry = rb_entry(node, struct tree_entry, rb_node);
188 rb_link_node(node, parent, p);
189 rb_insert_color(node, root);
193 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
194 struct rb_node **prev_ret,
195 struct rb_node **next_ret)
197 struct rb_root *root = &tree->state;
198 struct rb_node *n = root->rb_node;
199 struct rb_node *prev = NULL;
200 struct rb_node *orig_prev = NULL;
201 struct tree_entry *entry;
202 struct tree_entry *prev_entry = NULL;
205 entry = rb_entry(n, struct tree_entry, rb_node);
209 if (offset < entry->start)
211 else if (offset > entry->end)
219 while (prev && offset > prev_entry->end) {
220 prev = rb_next(prev);
221 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
228 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
229 while (prev && offset < prev_entry->start) {
230 prev = rb_prev(prev);
231 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
238 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
241 struct rb_node *prev = NULL;
244 ret = __etree_search(tree, offset, &prev, NULL);
250 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
251 struct extent_state *other)
253 if (tree->ops && tree->ops->merge_extent_hook)
254 tree->ops->merge_extent_hook(tree->mapping->host, new,
259 * utility function to look for merge candidates inside a given range.
260 * Any extents with matching state are merged together into a single
261 * extent in the tree. Extents with EXTENT_IO in their state field
262 * are not merged because the end_io handlers need to be able to do
263 * operations on them without sleeping (or doing allocations/splits).
265 * This should be called with the tree lock held.
267 static void merge_state(struct extent_io_tree *tree,
268 struct extent_state *state)
270 struct extent_state *other;
271 struct rb_node *other_node;
273 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
276 other_node = rb_prev(&state->rb_node);
278 other = rb_entry(other_node, struct extent_state, rb_node);
279 if (other->end == state->start - 1 &&
280 other->state == state->state) {
281 merge_cb(tree, state, other);
282 state->start = other->start;
284 rb_erase(&other->rb_node, &tree->state);
285 free_extent_state(other);
288 other_node = rb_next(&state->rb_node);
290 other = rb_entry(other_node, struct extent_state, rb_node);
291 if (other->start == state->end + 1 &&
292 other->state == state->state) {
293 merge_cb(tree, state, other);
294 state->end = other->end;
296 rb_erase(&other->rb_node, &tree->state);
297 free_extent_state(other);
302 static void set_state_cb(struct extent_io_tree *tree,
303 struct extent_state *state, int *bits)
305 if (tree->ops && tree->ops->set_bit_hook)
306 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
309 static void clear_state_cb(struct extent_io_tree *tree,
310 struct extent_state *state, int *bits)
312 if (tree->ops && tree->ops->clear_bit_hook)
313 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
316 static void set_state_bits(struct extent_io_tree *tree,
317 struct extent_state *state, int *bits);
320 * insert an extent_state struct into the tree. 'bits' are set on the
321 * struct before it is inserted.
323 * This may return -EEXIST if the extent is already there, in which case the
324 * state struct is freed.
326 * The tree lock is not taken internally. This is a utility function and
327 * probably isn't what you want to call (see set/clear_extent_bit).
329 static int insert_state(struct extent_io_tree *tree,
330 struct extent_state *state, u64 start, u64 end,
333 struct rb_node *node;
336 printk(KERN_ERR "btrfs end < start %llu %llu\n",
337 (unsigned long long)end,
338 (unsigned long long)start);
341 state->start = start;
344 set_state_bits(tree, state, bits);
346 node = tree_insert(&tree->state, end, &state->rb_node);
348 struct extent_state *found;
349 found = rb_entry(node, struct extent_state, rb_node);
350 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
351 "%llu %llu\n", (unsigned long long)found->start,
352 (unsigned long long)found->end,
353 (unsigned long long)start, (unsigned long long)end);
357 merge_state(tree, state);
361 static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
364 if (tree->ops && tree->ops->split_extent_hook)
365 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
369 * split a given extent state struct in two, inserting the preallocated
370 * struct 'prealloc' as the newly created second half. 'split' indicates an
371 * offset inside 'orig' where it should be split.
374 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
375 * are two extent state structs in the tree:
376 * prealloc: [orig->start, split - 1]
377 * orig: [ split, orig->end ]
379 * The tree locks are not taken by this function. They need to be held
382 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
383 struct extent_state *prealloc, u64 split)
385 struct rb_node *node;
387 split_cb(tree, orig, split);
389 prealloc->start = orig->start;
390 prealloc->end = split - 1;
391 prealloc->state = orig->state;
394 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
396 free_extent_state(prealloc);
399 prealloc->tree = tree;
404 * utility function to clear some bits in an extent state struct.
405 * it will optionally wake up any one waiting on this state (wake == 1), or
406 * forcibly remove the state from the tree (delete == 1).
408 * If no bits are set on the state struct after clearing things, the
409 * struct is freed and removed from the tree
411 static int clear_state_bit(struct extent_io_tree *tree,
412 struct extent_state *state,
415 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
416 int ret = state->state & bits_to_clear;
418 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
419 u64 range = state->end - state->start + 1;
420 WARN_ON(range > tree->dirty_bytes);
421 tree->dirty_bytes -= range;
423 clear_state_cb(tree, state, bits);
424 state->state &= ~bits_to_clear;
427 if (state->state == 0) {
429 rb_erase(&state->rb_node, &tree->state);
431 free_extent_state(state);
436 merge_state(tree, state);
441 static struct extent_state *
442 alloc_extent_state_atomic(struct extent_state *prealloc)
445 prealloc = alloc_extent_state(GFP_ATOMIC);
450 void extent_io_tree_panic(struct extent_io_tree *tree, int err)
452 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
453 "Extent tree was modified by another "
454 "thread while locked.");
458 * clear some bits on a range in the tree. This may require splitting
459 * or inserting elements in the tree, so the gfp mask is used to
460 * indicate which allocations or sleeping are allowed.
462 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
463 * the given range from the tree regardless of state (ie for truncate).
465 * the range [start, end] is inclusive.
467 * This takes the tree lock, and returns 0 on success and < 0 on error.
469 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
470 int bits, int wake, int delete,
471 struct extent_state **cached_state,
474 struct extent_state *state;
475 struct extent_state *cached;
476 struct extent_state *prealloc = NULL;
477 struct rb_node *next_node;
478 struct rb_node *node;
484 bits |= ~EXTENT_CTLBITS;
485 bits |= EXTENT_FIRST_DELALLOC;
487 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
490 if (!prealloc && (mask & __GFP_WAIT)) {
491 prealloc = alloc_extent_state(mask);
496 spin_lock(&tree->lock);
498 cached = *cached_state;
501 *cached_state = NULL;
505 if (cached && cached->tree && cached->start <= start &&
506 cached->end > start) {
508 atomic_dec(&cached->refs);
513 free_extent_state(cached);
516 * this search will find the extents that end after
519 node = tree_search(tree, start);
522 state = rb_entry(node, struct extent_state, rb_node);
524 if (state->start > end)
526 WARN_ON(state->end < start);
527 last_end = state->end;
529 if (state->end < end && !need_resched())
530 next_node = rb_next(&state->rb_node);
534 /* the state doesn't have the wanted bits, go ahead */
535 if (!(state->state & bits))
539 * | ---- desired range ---- |
541 * | ------------- state -------------- |
543 * We need to split the extent we found, and may flip
544 * bits on second half.
546 * If the extent we found extends past our range, we
547 * just split and search again. It'll get split again
548 * the next time though.
550 * If the extent we found is inside our range, we clear
551 * the desired bit on it.
554 if (state->start < start) {
555 prealloc = alloc_extent_state_atomic(prealloc);
557 err = split_state(tree, state, prealloc, start);
559 extent_io_tree_panic(tree, err);
564 if (state->end <= end) {
565 clear_state_bit(tree, state, &bits, wake);
566 if (last_end == (u64)-1)
568 start = last_end + 1;
573 * | ---- desired range ---- |
575 * We need to split the extent, and clear the bit
578 if (state->start <= end && state->end > end) {
579 prealloc = alloc_extent_state_atomic(prealloc);
581 err = split_state(tree, state, prealloc, end + 1);
583 extent_io_tree_panic(tree, err);
588 clear_state_bit(tree, prealloc, &bits, wake);
594 clear_state_bit(tree, state, &bits, wake);
596 if (last_end == (u64)-1)
598 start = last_end + 1;
599 if (start <= end && next_node) {
600 state = rb_entry(next_node, struct extent_state,
607 spin_unlock(&tree->lock);
609 free_extent_state(prealloc);
616 spin_unlock(&tree->lock);
617 if (mask & __GFP_WAIT)
622 static void wait_on_state(struct extent_io_tree *tree,
623 struct extent_state *state)
624 __releases(tree->lock)
625 __acquires(tree->lock)
628 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
629 spin_unlock(&tree->lock);
631 spin_lock(&tree->lock);
632 finish_wait(&state->wq, &wait);
636 * waits for one or more bits to clear on a range in the state tree.
637 * The range [start, end] is inclusive.
638 * The tree lock is taken by this function
640 void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
642 struct extent_state *state;
643 struct rb_node *node;
645 spin_lock(&tree->lock);
649 * this search will find all the extents that end after
652 node = tree_search(tree, start);
656 state = rb_entry(node, struct extent_state, rb_node);
658 if (state->start > end)
661 if (state->state & bits) {
662 start = state->start;
663 atomic_inc(&state->refs);
664 wait_on_state(tree, state);
665 free_extent_state(state);
668 start = state->end + 1;
673 cond_resched_lock(&tree->lock);
676 spin_unlock(&tree->lock);
679 static void set_state_bits(struct extent_io_tree *tree,
680 struct extent_state *state,
683 int bits_to_set = *bits & ~EXTENT_CTLBITS;
685 set_state_cb(tree, state, bits);
686 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
687 u64 range = state->end - state->start + 1;
688 tree->dirty_bytes += range;
690 state->state |= bits_to_set;
693 static void cache_state(struct extent_state *state,
694 struct extent_state **cached_ptr)
696 if (cached_ptr && !(*cached_ptr)) {
697 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
699 atomic_inc(&state->refs);
704 static void uncache_state(struct extent_state **cached_ptr)
706 if (cached_ptr && (*cached_ptr)) {
707 struct extent_state *state = *cached_ptr;
709 free_extent_state(state);
714 * set some bits on a range in the tree. This may require allocations or
715 * sleeping, so the gfp mask is used to indicate what is allowed.
717 * If any of the exclusive bits are set, this will fail with -EEXIST if some
718 * part of the range already has the desired bits set. The start of the
719 * existing range is returned in failed_start in this case.
721 * [start, end] is inclusive This takes the tree lock.
724 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
725 int bits, int exclusive_bits, u64 *failed_start,
726 struct extent_state **cached_state, gfp_t mask)
728 struct extent_state *state;
729 struct extent_state *prealloc = NULL;
730 struct rb_node *node;
735 bits |= EXTENT_FIRST_DELALLOC;
737 if (!prealloc && (mask & __GFP_WAIT)) {
738 prealloc = alloc_extent_state(mask);
742 spin_lock(&tree->lock);
743 if (cached_state && *cached_state) {
744 state = *cached_state;
745 if (state->start <= start && state->end > start &&
747 node = &state->rb_node;
752 * this search will find all the extents that end after
755 node = tree_search(tree, start);
757 prealloc = alloc_extent_state_atomic(prealloc);
759 err = insert_state(tree, prealloc, start, end, &bits);
761 extent_io_tree_panic(tree, err);
766 state = rb_entry(node, struct extent_state, rb_node);
768 last_start = state->start;
769 last_end = state->end;
772 * | ---- desired range ---- |
775 * Just lock what we found and keep going
777 if (state->start == start && state->end <= end) {
778 struct rb_node *next_node;
779 if (state->state & exclusive_bits) {
780 *failed_start = state->start;
785 set_state_bits(tree, state, &bits);
787 cache_state(state, cached_state);
788 merge_state(tree, state);
789 if (last_end == (u64)-1)
792 start = last_end + 1;
793 next_node = rb_next(&state->rb_node);
794 if (next_node && start < end && prealloc && !need_resched()) {
795 state = rb_entry(next_node, struct extent_state,
797 if (state->start == start)
804 * | ---- desired range ---- |
807 * | ------------- state -------------- |
809 * We need to split the extent we found, and may flip bits on
812 * If the extent we found extends past our
813 * range, we just split and search again. It'll get split
814 * again the next time though.
816 * If the extent we found is inside our range, we set the
819 if (state->start < start) {
820 if (state->state & exclusive_bits) {
821 *failed_start = start;
826 prealloc = alloc_extent_state_atomic(prealloc);
828 err = split_state(tree, state, prealloc, start);
830 extent_io_tree_panic(tree, err);
835 if (state->end <= end) {
836 set_state_bits(tree, state, &bits);
837 cache_state(state, cached_state);
838 merge_state(tree, state);
839 if (last_end == (u64)-1)
841 start = last_end + 1;
846 * | ---- desired range ---- |
847 * | state | or | state |
849 * There's a hole, we need to insert something in it and
850 * ignore the extent we found.
852 if (state->start > start) {
854 if (end < last_start)
857 this_end = last_start - 1;
859 prealloc = alloc_extent_state_atomic(prealloc);
863 * Avoid to free 'prealloc' if it can be merged with
866 err = insert_state(tree, prealloc, start, this_end,
869 extent_io_tree_panic(tree, err);
871 cache_state(prealloc, cached_state);
873 start = this_end + 1;
877 * | ---- desired range ---- |
879 * We need to split the extent, and set the bit
882 if (state->start <= end && state->end > end) {
883 if (state->state & exclusive_bits) {
884 *failed_start = start;
889 prealloc = alloc_extent_state_atomic(prealloc);
891 err = split_state(tree, state, prealloc, end + 1);
893 extent_io_tree_panic(tree, err);
895 set_state_bits(tree, prealloc, &bits);
896 cache_state(prealloc, cached_state);
897 merge_state(tree, prealloc);
905 spin_unlock(&tree->lock);
907 free_extent_state(prealloc);
914 spin_unlock(&tree->lock);
915 if (mask & __GFP_WAIT)
921 * convert_extent - convert all bits in a given range from one bit to another
922 * @tree: the io tree to search
923 * @start: the start offset in bytes
924 * @end: the end offset in bytes (inclusive)
925 * @bits: the bits to set in this range
926 * @clear_bits: the bits to clear in this range
927 * @mask: the allocation mask
929 * This will go through and set bits for the given range. If any states exist
930 * already in this range they are set with the given bit and cleared of the
931 * clear_bits. This is only meant to be used by things that are mergeable, ie
932 * converting from say DELALLOC to DIRTY. This is not meant to be used with
933 * boundary bits like LOCK.
935 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
936 int bits, int clear_bits, gfp_t mask)
938 struct extent_state *state;
939 struct extent_state *prealloc = NULL;
940 struct rb_node *node;
946 if (!prealloc && (mask & __GFP_WAIT)) {
947 prealloc = alloc_extent_state(mask);
952 spin_lock(&tree->lock);
954 * this search will find all the extents that end after
957 node = tree_search(tree, start);
959 prealloc = alloc_extent_state_atomic(prealloc);
964 err = insert_state(tree, prealloc, start, end, &bits);
967 extent_io_tree_panic(tree, err);
970 state = rb_entry(node, struct extent_state, rb_node);
972 last_start = state->start;
973 last_end = state->end;
976 * | ---- desired range ---- |
979 * Just lock what we found and keep going
981 if (state->start == start && state->end <= end) {
982 struct rb_node *next_node;
984 set_state_bits(tree, state, &bits);
985 clear_state_bit(tree, state, &clear_bits, 0);
986 if (last_end == (u64)-1)
989 start = last_end + 1;
990 next_node = rb_next(&state->rb_node);
991 if (next_node && start < end && prealloc && !need_resched()) {
992 state = rb_entry(next_node, struct extent_state,
994 if (state->start == start)
1001 * | ---- desired range ---- |
1004 * | ------------- state -------------- |
1006 * We need to split the extent we found, and may flip bits on
1009 * If the extent we found extends past our
1010 * range, we just split and search again. It'll get split
1011 * again the next time though.
1013 * If the extent we found is inside our range, we set the
1014 * desired bit on it.
1016 if (state->start < start) {
1017 prealloc = alloc_extent_state_atomic(prealloc);
1022 err = split_state(tree, state, prealloc, start);
1024 extent_io_tree_panic(tree, err);
1028 if (state->end <= end) {
1029 set_state_bits(tree, state, &bits);
1030 clear_state_bit(tree, state, &clear_bits, 0);
1031 if (last_end == (u64)-1)
1033 start = last_end + 1;
1038 * | ---- desired range ---- |
1039 * | state | or | state |
1041 * There's a hole, we need to insert something in it and
1042 * ignore the extent we found.
1044 if (state->start > start) {
1046 if (end < last_start)
1049 this_end = last_start - 1;
1051 prealloc = alloc_extent_state_atomic(prealloc);
1058 * Avoid to free 'prealloc' if it can be merged with
1061 err = insert_state(tree, prealloc, start, this_end,
1064 extent_io_tree_panic(tree, err);
1066 start = this_end + 1;
1070 * | ---- desired range ---- |
1072 * We need to split the extent, and set the bit
1075 if (state->start <= end && state->end > end) {
1076 prealloc = alloc_extent_state_atomic(prealloc);
1082 err = split_state(tree, state, prealloc, end + 1);
1084 extent_io_tree_panic(tree, err);
1086 set_state_bits(tree, prealloc, &bits);
1087 clear_state_bit(tree, prealloc, &clear_bits, 0);
1095 spin_unlock(&tree->lock);
1097 free_extent_state(prealloc);
1104 spin_unlock(&tree->lock);
1105 if (mask & __GFP_WAIT)
1110 /* wrappers around set/clear extent bit */
1111 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1114 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
1118 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1119 int bits, gfp_t mask)
1121 return set_extent_bit(tree, start, end, bits, 0, NULL,
1125 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1126 int bits, gfp_t mask)
1128 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
1131 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
1132 struct extent_state **cached_state, gfp_t mask)
1134 return set_extent_bit(tree, start, end,
1135 EXTENT_DELALLOC | EXTENT_UPTODATE,
1136 0, NULL, cached_state, mask);
1139 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1142 return clear_extent_bit(tree, start, end,
1143 EXTENT_DIRTY | EXTENT_DELALLOC |
1144 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
1147 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1150 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
1154 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1155 struct extent_state **cached_state, gfp_t mask)
1157 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1158 NULL, cached_state, mask);
1161 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
1162 u64 end, struct extent_state **cached_state,
1165 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1166 cached_state, mask);
1170 * either insert or lock state struct between start and end use mask to tell
1171 * us if waiting is desired.
1173 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1174 int bits, struct extent_state **cached_state)
1179 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1180 EXTENT_LOCKED, &failed_start,
1181 cached_state, GFP_NOFS);
1182 if (err == -EEXIST) {
1183 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1184 start = failed_start;
1187 WARN_ON(start > end);
1192 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1194 return lock_extent_bits(tree, start, end, 0, NULL);
1197 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1202 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1203 &failed_start, NULL, GFP_NOFS);
1204 if (err == -EEXIST) {
1205 if (failed_start > start)
1206 clear_extent_bit(tree, start, failed_start - 1,
1207 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
1213 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1214 struct extent_state **cached, gfp_t mask)
1216 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1220 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1222 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1227 * helper function to set both pages and extents in the tree writeback
1229 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1231 unsigned long index = start >> PAGE_CACHE_SHIFT;
1232 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1235 while (index <= end_index) {
1236 page = find_get_page(tree->mapping, index);
1238 set_page_writeback(page);
1239 page_cache_release(page);
1245 /* find the first state struct with 'bits' set after 'start', and
1246 * return it. tree->lock must be held. NULL will returned if
1247 * nothing was found after 'start'
1249 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1250 u64 start, int bits)
1252 struct rb_node *node;
1253 struct extent_state *state;
1256 * this search will find all the extents that end after
1259 node = tree_search(tree, start);
1264 state = rb_entry(node, struct extent_state, rb_node);
1265 if (state->end >= start && (state->state & bits))
1268 node = rb_next(node);
1277 * find the first offset in the io tree with 'bits' set. zero is
1278 * returned if we find something, and *start_ret and *end_ret are
1279 * set to reflect the state struct that was found.
1281 * If nothing was found, 1 is returned, < 0 on error
1283 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1284 u64 *start_ret, u64 *end_ret, int bits)
1286 struct extent_state *state;
1289 spin_lock(&tree->lock);
1290 state = find_first_extent_bit_state(tree, start, bits);
1292 *start_ret = state->start;
1293 *end_ret = state->end;
1296 spin_unlock(&tree->lock);
1301 * find a contiguous range of bytes in the file marked as delalloc, not
1302 * more than 'max_bytes'. start and end are used to return the range,
1304 * 1 is returned if we find something, 0 if nothing was in the tree
1306 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1307 u64 *start, u64 *end, u64 max_bytes,
1308 struct extent_state **cached_state)
1310 struct rb_node *node;
1311 struct extent_state *state;
1312 u64 cur_start = *start;
1314 u64 total_bytes = 0;
1316 spin_lock(&tree->lock);
1319 * this search will find all the extents that end after
1322 node = tree_search(tree, cur_start);
1330 state = rb_entry(node, struct extent_state, rb_node);
1331 if (found && (state->start != cur_start ||
1332 (state->state & EXTENT_BOUNDARY))) {
1335 if (!(state->state & EXTENT_DELALLOC)) {
1341 *start = state->start;
1342 *cached_state = state;
1343 atomic_inc(&state->refs);
1347 cur_start = state->end + 1;
1348 node = rb_next(node);
1351 total_bytes += state->end - state->start + 1;
1352 if (total_bytes >= max_bytes)
1356 spin_unlock(&tree->lock);
1360 static noinline void __unlock_for_delalloc(struct inode *inode,
1361 struct page *locked_page,
1365 struct page *pages[16];
1366 unsigned long index = start >> PAGE_CACHE_SHIFT;
1367 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1368 unsigned long nr_pages = end_index - index + 1;
1371 if (index == locked_page->index && end_index == index)
1374 while (nr_pages > 0) {
1375 ret = find_get_pages_contig(inode->i_mapping, index,
1376 min_t(unsigned long, nr_pages,
1377 ARRAY_SIZE(pages)), pages);
1378 for (i = 0; i < ret; i++) {
1379 if (pages[i] != locked_page)
1380 unlock_page(pages[i]);
1381 page_cache_release(pages[i]);
1389 static noinline int lock_delalloc_pages(struct inode *inode,
1390 struct page *locked_page,
1394 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1395 unsigned long start_index = index;
1396 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1397 unsigned long pages_locked = 0;
1398 struct page *pages[16];
1399 unsigned long nrpages;
1403 /* the caller is responsible for locking the start index */
1404 if (index == locked_page->index && index == end_index)
1407 /* skip the page at the start index */
1408 nrpages = end_index - index + 1;
1409 while (nrpages > 0) {
1410 ret = find_get_pages_contig(inode->i_mapping, index,
1411 min_t(unsigned long,
1412 nrpages, ARRAY_SIZE(pages)), pages);
1417 /* now we have an array of pages, lock them all */
1418 for (i = 0; i < ret; i++) {
1420 * the caller is taking responsibility for
1423 if (pages[i] != locked_page) {
1424 lock_page(pages[i]);
1425 if (!PageDirty(pages[i]) ||
1426 pages[i]->mapping != inode->i_mapping) {
1428 unlock_page(pages[i]);
1429 page_cache_release(pages[i]);
1433 page_cache_release(pages[i]);
1442 if (ret && pages_locked) {
1443 __unlock_for_delalloc(inode, locked_page,
1445 ((u64)(start_index + pages_locked - 1)) <<
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_lock_delalloc_range(struct inode *inode,
1458 struct extent_io_tree *tree,
1459 struct page *locked_page,
1460 u64 *start, u64 *end,
1466 struct extent_state *cached_state = NULL;
1471 /* step one, find a bunch of delalloc bytes starting at start */
1472 delalloc_start = *start;
1474 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1475 max_bytes, &cached_state);
1476 if (!found || delalloc_end <= *start) {
1477 *start = delalloc_start;
1478 *end = delalloc_end;
1479 free_extent_state(cached_state);
1484 * start comes from the offset of locked_page. We have to lock
1485 * pages in order, so we can't process delalloc bytes before
1488 if (delalloc_start < *start)
1489 delalloc_start = *start;
1492 * make sure to limit the number of pages we try to lock down
1495 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1496 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1498 /* step two, lock all the pages after the page that has start */
1499 ret = lock_delalloc_pages(inode, locked_page,
1500 delalloc_start, delalloc_end);
1501 if (ret == -EAGAIN) {
1502 /* some of the pages are gone, lets avoid looping by
1503 * shortening the size of the delalloc range we're searching
1505 free_extent_state(cached_state);
1507 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1508 max_bytes = PAGE_CACHE_SIZE - offset;
1518 /* step three, lock the state bits for the whole range */
1519 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
1521 /* then test to make sure it is all still delalloc */
1522 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1523 EXTENT_DELALLOC, 1, cached_state);
1525 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1526 &cached_state, GFP_NOFS);
1527 __unlock_for_delalloc(inode, locked_page,
1528 delalloc_start, delalloc_end);
1532 free_extent_state(cached_state);
1533 *start = delalloc_start;
1534 *end = delalloc_end;
1539 int extent_clear_unlock_delalloc(struct inode *inode,
1540 struct extent_io_tree *tree,
1541 u64 start, u64 end, struct page *locked_page,
1545 struct page *pages[16];
1546 unsigned long index = start >> PAGE_CACHE_SHIFT;
1547 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1548 unsigned long nr_pages = end_index - index + 1;
1552 if (op & EXTENT_CLEAR_UNLOCK)
1553 clear_bits |= EXTENT_LOCKED;
1554 if (op & EXTENT_CLEAR_DIRTY)
1555 clear_bits |= EXTENT_DIRTY;
1557 if (op & EXTENT_CLEAR_DELALLOC)
1558 clear_bits |= EXTENT_DELALLOC;
1560 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1561 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1562 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1563 EXTENT_SET_PRIVATE2)))
1566 while (nr_pages > 0) {
1567 ret = find_get_pages_contig(inode->i_mapping, index,
1568 min_t(unsigned long,
1569 nr_pages, ARRAY_SIZE(pages)), pages);
1570 for (i = 0; i < ret; i++) {
1572 if (op & EXTENT_SET_PRIVATE2)
1573 SetPagePrivate2(pages[i]);
1575 if (pages[i] == locked_page) {
1576 page_cache_release(pages[i]);
1579 if (op & EXTENT_CLEAR_DIRTY)
1580 clear_page_dirty_for_io(pages[i]);
1581 if (op & EXTENT_SET_WRITEBACK)
1582 set_page_writeback(pages[i]);
1583 if (op & EXTENT_END_WRITEBACK)
1584 end_page_writeback(pages[i]);
1585 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1586 unlock_page(pages[i]);
1587 page_cache_release(pages[i]);
1597 * count the number of bytes in the tree that have a given bit(s)
1598 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1599 * cached. The total number found is returned.
1601 u64 count_range_bits(struct extent_io_tree *tree,
1602 u64 *start, u64 search_end, u64 max_bytes,
1603 unsigned long bits, int contig)
1605 struct rb_node *node;
1606 struct extent_state *state;
1607 u64 cur_start = *start;
1608 u64 total_bytes = 0;
1612 if (search_end <= cur_start) {
1617 spin_lock(&tree->lock);
1618 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1619 total_bytes = tree->dirty_bytes;
1623 * this search will find all the extents that end after
1626 node = tree_search(tree, cur_start);
1631 state = rb_entry(node, struct extent_state, rb_node);
1632 if (state->start > search_end)
1634 if (contig && found && state->start > last + 1)
1636 if (state->end >= cur_start && (state->state & bits) == bits) {
1637 total_bytes += min(search_end, state->end) + 1 -
1638 max(cur_start, state->start);
1639 if (total_bytes >= max_bytes)
1642 *start = max(cur_start, state->start);
1646 } else if (contig && found) {
1649 node = rb_next(node);
1654 spin_unlock(&tree->lock);
1659 * set the private field for a given byte offset in the tree. If there isn't
1660 * an extent_state there already, this does nothing.
1662 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1664 struct rb_node *node;
1665 struct extent_state *state;
1668 spin_lock(&tree->lock);
1670 * this search will find all the extents that end after
1673 node = tree_search(tree, start);
1678 state = rb_entry(node, struct extent_state, rb_node);
1679 if (state->start != start) {
1683 state->private = private;
1685 spin_unlock(&tree->lock);
1689 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1691 struct rb_node *node;
1692 struct extent_state *state;
1695 spin_lock(&tree->lock);
1697 * this search will find all the extents that end after
1700 node = tree_search(tree, start);
1705 state = rb_entry(node, struct extent_state, rb_node);
1706 if (state->start != start) {
1710 *private = state->private;
1712 spin_unlock(&tree->lock);
1717 * searches a range in the state tree for a given mask.
1718 * If 'filled' == 1, this returns 1 only if every extent in the tree
1719 * has the bits set. Otherwise, 1 is returned if any bit in the
1720 * range is found set.
1722 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1723 int bits, int filled, struct extent_state *cached)
1725 struct extent_state *state = NULL;
1726 struct rb_node *node;
1729 spin_lock(&tree->lock);
1730 if (cached && cached->tree && cached->start <= start &&
1731 cached->end > start)
1732 node = &cached->rb_node;
1734 node = tree_search(tree, start);
1735 while (node && start <= end) {
1736 state = rb_entry(node, struct extent_state, rb_node);
1738 if (filled && state->start > start) {
1743 if (state->start > end)
1746 if (state->state & bits) {
1750 } else if (filled) {
1755 if (state->end == (u64)-1)
1758 start = state->end + 1;
1761 node = rb_next(node);
1768 spin_unlock(&tree->lock);
1773 * helper function to set a given page up to date if all the
1774 * extents in the tree for that page are up to date
1776 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
1778 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1779 u64 end = start + PAGE_CACHE_SIZE - 1;
1780 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1781 SetPageUptodate(page);
1785 * helper function to unlock a page if all the extents in the tree
1786 * for that page are unlocked
1788 static void check_page_locked(struct extent_io_tree *tree, struct page *page)
1790 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1791 u64 end = start + PAGE_CACHE_SIZE - 1;
1792 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1797 * helper function to end page writeback if all the extents
1798 * in the tree for that page are done with writeback
1800 static void check_page_writeback(struct extent_io_tree *tree,
1803 end_page_writeback(page);
1807 * When IO fails, either with EIO or csum verification fails, we
1808 * try other mirrors that might have a good copy of the data. This
1809 * io_failure_record is used to record state as we go through all the
1810 * mirrors. If another mirror has good data, the page is set up to date
1811 * and things continue. If a good mirror can't be found, the original
1812 * bio end_io callback is called to indicate things have failed.
1814 struct io_failure_record {
1819 unsigned long bio_flags;
1825 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1830 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1832 set_state_private(failure_tree, rec->start, 0);
1833 ret = clear_extent_bits(failure_tree, rec->start,
1834 rec->start + rec->len - 1,
1835 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1840 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1841 rec->start + rec->len - 1,
1842 EXTENT_DAMAGED, GFP_NOFS);
1851 static void repair_io_failure_callback(struct bio *bio, int err)
1853 complete(bio->bi_private);
1857 * this bypasses the standard btrfs submit functions deliberately, as
1858 * the standard behavior is to write all copies in a raid setup. here we only
1859 * want to write the one bad copy. so we do the mapping for ourselves and issue
1860 * submit_bio directly.
1861 * to avoid any synchonization issues, wait for the data after writing, which
1862 * actually prevents the read that triggered the error from finishing.
1863 * currently, there can be no more than two copies of every data bit. thus,
1864 * exactly one rewrite is required.
1866 int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1867 u64 length, u64 logical, struct page *page,
1871 struct btrfs_device *dev;
1872 DECLARE_COMPLETION_ONSTACK(compl);
1875 struct btrfs_bio *bbio = NULL;
1878 BUG_ON(!mirror_num);
1880 bio = bio_alloc(GFP_NOFS, 1);
1883 bio->bi_private = &compl;
1884 bio->bi_end_io = repair_io_failure_callback;
1886 map_length = length;
1888 ret = btrfs_map_block(map_tree, WRITE, logical,
1889 &map_length, &bbio, mirror_num);
1894 BUG_ON(mirror_num != bbio->mirror_num);
1895 sector = bbio->stripes[mirror_num-1].physical >> 9;
1896 bio->bi_sector = sector;
1897 dev = bbio->stripes[mirror_num-1].dev;
1899 if (!dev || !dev->bdev || !dev->writeable) {
1903 bio->bi_bdev = dev->bdev;
1904 bio_add_page(bio, page, length, start-page_offset(page));
1905 btrfsic_submit_bio(WRITE_SYNC, bio);
1906 wait_for_completion(&compl);
1908 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1909 /* try to remap that extent elsewhere? */
1914 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1915 "sector %llu)\n", page->mapping->host->i_ino, start,
1923 * each time an IO finishes, we do a fast check in the IO failure tree
1924 * to see if we need to process or clean up an io_failure_record
1926 static int clean_io_failure(u64 start, struct page *page)
1929 u64 private_failure;
1930 struct io_failure_record *failrec;
1931 struct btrfs_mapping_tree *map_tree;
1932 struct extent_state *state;
1936 struct inode *inode = page->mapping->host;
1939 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1940 (u64)-1, 1, EXTENT_DIRTY, 0);
1944 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1949 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1950 BUG_ON(!failrec->this_mirror);
1952 if (failrec->in_validation) {
1953 /* there was no real error, just free the record */
1954 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1960 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1961 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1964 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1966 if (state && state->start == failrec->start) {
1967 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1968 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1970 if (num_copies > 1) {
1971 ret = repair_io_failure(map_tree, start, failrec->len,
1972 failrec->logical, page,
1973 failrec->failed_mirror);
1980 ret = free_io_failure(inode, failrec, did_repair);
1986 * this is a generic handler for readpage errors (default
1987 * readpage_io_failed_hook). if other copies exist, read those and write back
1988 * good data to the failed position. does not investigate in remapping the
1989 * failed extent elsewhere, hoping the device will be smart enough to do this as
1993 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
1994 u64 start, u64 end, int failed_mirror,
1995 struct extent_state *state)
1997 struct io_failure_record *failrec = NULL;
1999 struct extent_map *em;
2000 struct inode *inode = page->mapping->host;
2001 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2002 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2003 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2010 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2012 ret = get_state_private(failure_tree, start, &private);
2014 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2017 failrec->start = start;
2018 failrec->len = end - start + 1;
2019 failrec->this_mirror = 0;
2020 failrec->bio_flags = 0;
2021 failrec->in_validation = 0;
2023 read_lock(&em_tree->lock);
2024 em = lookup_extent_mapping(em_tree, start, failrec->len);
2026 read_unlock(&em_tree->lock);
2031 if (em->start > start || em->start + em->len < start) {
2032 free_extent_map(em);
2035 read_unlock(&em_tree->lock);
2037 if (!em || IS_ERR(em)) {
2041 logical = start - em->start;
2042 logical = em->block_start + logical;
2043 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2044 logical = em->block_start;
2045 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2046 extent_set_compress_type(&failrec->bio_flags,
2049 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2050 "len=%llu\n", logical, start, failrec->len);
2051 failrec->logical = logical;
2052 free_extent_map(em);
2054 /* set the bits in the private failure tree */
2055 ret = set_extent_bits(failure_tree, start, end,
2056 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2058 ret = set_state_private(failure_tree, start,
2059 (u64)(unsigned long)failrec);
2060 /* set the bits in the inode's tree */
2062 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2069 failrec = (struct io_failure_record *)(unsigned long)private;
2070 pr_debug("bio_readpage_error: (found) logical=%llu, "
2071 "start=%llu, len=%llu, validation=%d\n",
2072 failrec->logical, failrec->start, failrec->len,
2073 failrec->in_validation);
2075 * when data can be on disk more than twice, add to failrec here
2076 * (e.g. with a list for failed_mirror) to make
2077 * clean_io_failure() clean all those errors at once.
2080 num_copies = btrfs_num_copies(
2081 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2082 failrec->logical, failrec->len);
2083 if (num_copies == 1) {
2085 * we only have a single copy of the data, so don't bother with
2086 * all the retry and error correction code that follows. no
2087 * matter what the error is, it is very likely to persist.
2089 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2090 "state=%p, num_copies=%d, next_mirror %d, "
2091 "failed_mirror %d\n", state, num_copies,
2092 failrec->this_mirror, failed_mirror);
2093 free_io_failure(inode, failrec, 0);
2098 spin_lock(&tree->lock);
2099 state = find_first_extent_bit_state(tree, failrec->start,
2101 if (state && state->start != failrec->start)
2103 spin_unlock(&tree->lock);
2107 * there are two premises:
2108 * a) deliver good data to the caller
2109 * b) correct the bad sectors on disk
2111 if (failed_bio->bi_vcnt > 1) {
2113 * to fulfill b), we need to know the exact failing sectors, as
2114 * we don't want to rewrite any more than the failed ones. thus,
2115 * we need separate read requests for the failed bio
2117 * if the following BUG_ON triggers, our validation request got
2118 * merged. we need separate requests for our algorithm to work.
2120 BUG_ON(failrec->in_validation);
2121 failrec->in_validation = 1;
2122 failrec->this_mirror = failed_mirror;
2123 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2126 * we're ready to fulfill a) and b) alongside. get a good copy
2127 * of the failed sector and if we succeed, we have setup
2128 * everything for repair_io_failure to do the rest for us.
2130 if (failrec->in_validation) {
2131 BUG_ON(failrec->this_mirror != failed_mirror);
2132 failrec->in_validation = 0;
2133 failrec->this_mirror = 0;
2135 failrec->failed_mirror = failed_mirror;
2136 failrec->this_mirror++;
2137 if (failrec->this_mirror == failed_mirror)
2138 failrec->this_mirror++;
2139 read_mode = READ_SYNC;
2142 if (!state || failrec->this_mirror > num_copies) {
2143 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2144 "next_mirror %d, failed_mirror %d\n", state,
2145 num_copies, failrec->this_mirror, failed_mirror);
2146 free_io_failure(inode, failrec, 0);
2150 bio = bio_alloc(GFP_NOFS, 1);
2151 bio->bi_private = state;
2152 bio->bi_end_io = failed_bio->bi_end_io;
2153 bio->bi_sector = failrec->logical >> 9;
2154 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2157 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2159 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2160 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2161 failrec->this_mirror, num_copies, failrec->in_validation);
2163 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2164 failrec->this_mirror,
2165 failrec->bio_flags, 0);
2169 /* lots and lots of room for performance fixes in the end_bio funcs */
2171 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2173 int uptodate = (err == 0);
2174 struct extent_io_tree *tree;
2177 tree = &BTRFS_I(page->mapping->host)->io_tree;
2179 if (tree->ops && tree->ops->writepage_end_io_hook) {
2180 ret = tree->ops->writepage_end_io_hook(page, start,
2181 end, NULL, uptodate);
2186 if (!uptodate && tree->ops &&
2187 tree->ops->writepage_io_failed_hook) {
2188 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2190 /* Writeback already completed */
2197 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2198 ClearPageUptodate(page);
2205 * after a writepage IO is done, we need to:
2206 * clear the uptodate bits on error
2207 * clear the writeback bits in the extent tree for this IO
2208 * end_page_writeback if the page has no more pending IO
2210 * Scheduling is not allowed, so the extent state tree is expected
2211 * to have one and only one object corresponding to this IO.
2213 static void end_bio_extent_writepage(struct bio *bio, int err)
2215 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2216 struct extent_io_tree *tree;
2222 struct page *page = bvec->bv_page;
2223 tree = &BTRFS_I(page->mapping->host)->io_tree;
2225 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2227 end = start + bvec->bv_len - 1;
2229 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2234 if (--bvec >= bio->bi_io_vec)
2235 prefetchw(&bvec->bv_page->flags);
2237 if (end_extent_writepage(page, err, start, end))
2241 end_page_writeback(page);
2243 check_page_writeback(tree, page);
2244 } while (bvec >= bio->bi_io_vec);
2250 * after a readpage IO is done, we need to:
2251 * clear the uptodate bits on error
2252 * set the uptodate bits if things worked
2253 * set the page up to date if all extents in the tree are uptodate
2254 * clear the lock bit in the extent tree
2255 * unlock the page if there are no other extents locked for it
2257 * Scheduling is not allowed, so the extent state tree is expected
2258 * to have one and only one object corresponding to this IO.
2260 static void end_bio_extent_readpage(struct bio *bio, int err)
2262 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2263 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2264 struct bio_vec *bvec = bio->bi_io_vec;
2265 struct extent_io_tree *tree;
2275 struct page *page = bvec->bv_page;
2276 struct extent_state *cached = NULL;
2277 struct extent_state *state;
2279 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2280 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2281 (long int)bio->bi_bdev);
2282 tree = &BTRFS_I(page->mapping->host)->io_tree;
2284 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2286 end = start + bvec->bv_len - 1;
2288 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2293 if (++bvec <= bvec_end)
2294 prefetchw(&bvec->bv_page->flags);
2296 spin_lock(&tree->lock);
2297 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2298 if (state && state->start == start) {
2300 * take a reference on the state, unlock will drop
2303 cache_state(state, &cached);
2305 spin_unlock(&tree->lock);
2307 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2308 ret = tree->ops->readpage_end_io_hook(page, start, end,
2313 clean_io_failure(start, page);
2317 failed_mirror = (int)(unsigned long)bio->bi_bdev;
2319 * The generic bio_readpage_error handles errors the
2320 * following way: If possible, new read requests are
2321 * created and submitted and will end up in
2322 * end_bio_extent_readpage as well (if we're lucky, not
2323 * in the !uptodate case). In that case it returns 0 and
2324 * we just go on with the next page in our bio. If it
2325 * can't handle the error it will return -EIO and we
2326 * remain responsible for that page.
2328 ret = bio_readpage_error(bio, page, start, end,
2329 failed_mirror, NULL);
2333 test_bit(BIO_UPTODATE, &bio->bi_flags);
2336 uncache_state(&cached);
2339 if (tree->ops && tree->ops->readpage_io_failed_hook) {
2340 ret = tree->ops->readpage_io_failed_hook(
2341 bio, page, start, end,
2342 failed_mirror, state);
2350 set_extent_uptodate(tree, start, end, &cached,
2353 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2357 SetPageUptodate(page);
2359 ClearPageUptodate(page);
2365 check_page_uptodate(tree, page);
2367 ClearPageUptodate(page);
2370 check_page_locked(tree, page);
2372 } while (bvec <= bvec_end);
2378 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2383 bio = bio_alloc(gfp_flags, nr_vecs);
2385 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2386 while (!bio && (nr_vecs /= 2))
2387 bio = bio_alloc(gfp_flags, nr_vecs);
2392 bio->bi_bdev = bdev;
2393 bio->bi_sector = first_sector;
2398 static int __must_check submit_one_bio(int rw, struct bio *bio,
2399 int mirror_num, unsigned long bio_flags)
2402 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2403 struct page *page = bvec->bv_page;
2404 struct extent_io_tree *tree = bio->bi_private;
2407 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
2409 bio->bi_private = NULL;
2413 if (tree->ops && tree->ops->submit_bio_hook)
2414 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2415 mirror_num, bio_flags, start);
2417 btrfsic_submit_bio(rw, bio);
2419 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2425 static int merge_bio(struct extent_io_tree *tree, struct page *page,
2426 unsigned long offset, size_t size, struct bio *bio,
2427 unsigned long bio_flags)
2430 if (tree->ops && tree->ops->merge_bio_hook)
2431 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2438 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2439 struct page *page, sector_t sector,
2440 size_t size, unsigned long offset,
2441 struct block_device *bdev,
2442 struct bio **bio_ret,
2443 unsigned long max_pages,
2444 bio_end_io_t end_io_func,
2446 unsigned long prev_bio_flags,
2447 unsigned long bio_flags)
2453 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2454 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2455 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2457 if (bio_ret && *bio_ret) {
2460 contig = bio->bi_sector == sector;
2462 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2465 if (prev_bio_flags != bio_flags || !contig ||
2466 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
2467 bio_add_page(bio, page, page_size, offset) < page_size) {
2468 ret = submit_one_bio(rw, bio, mirror_num,
2476 if (this_compressed)
2479 nr = bio_get_nr_vecs(bdev);
2481 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2485 bio_add_page(bio, page, page_size, offset);
2486 bio->bi_end_io = end_io_func;
2487 bio->bi_private = tree;
2492 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2499 void set_page_extent_mapped(struct page *page)
2501 if (!PagePrivate(page)) {
2502 SetPagePrivate(page);
2503 page_cache_get(page);
2504 set_page_private(page, EXTENT_PAGE_PRIVATE);
2508 static void set_page_extent_head(struct page *page, unsigned long len)
2510 WARN_ON(!PagePrivate(page));
2511 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
2515 * basic readpage implementation. Locked extent state structs are inserted
2516 * into the tree that are removed when the IO is done (by the end_io
2519 static int __extent_read_full_page(struct extent_io_tree *tree,
2521 get_extent_t *get_extent,
2522 struct bio **bio, int mirror_num,
2523 unsigned long *bio_flags)
2525 struct inode *inode = page->mapping->host;
2526 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2527 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2531 u64 last_byte = i_size_read(inode);
2535 struct extent_map *em;
2536 struct block_device *bdev;
2537 struct btrfs_ordered_extent *ordered;
2540 size_t pg_offset = 0;
2542 size_t disk_io_size;
2543 size_t blocksize = inode->i_sb->s_blocksize;
2544 unsigned long this_bio_flag = 0;
2546 set_page_extent_mapped(page);
2548 if (!PageUptodate(page)) {
2549 if (cleancache_get_page(page) == 0) {
2550 BUG_ON(blocksize != PAGE_SIZE);
2557 lock_extent(tree, start, end);
2558 ordered = btrfs_lookup_ordered_extent(inode, start);
2561 unlock_extent(tree, start, end);
2562 btrfs_start_ordered_extent(inode, ordered, 1);
2563 btrfs_put_ordered_extent(ordered);
2566 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2568 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2571 iosize = PAGE_CACHE_SIZE - zero_offset;
2572 userpage = kmap_atomic(page, KM_USER0);
2573 memset(userpage + zero_offset, 0, iosize);
2574 flush_dcache_page(page);
2575 kunmap_atomic(userpage, KM_USER0);
2578 while (cur <= end) {
2579 if (cur >= last_byte) {
2581 struct extent_state *cached = NULL;
2583 iosize = PAGE_CACHE_SIZE - pg_offset;
2584 userpage = kmap_atomic(page, KM_USER0);
2585 memset(userpage + pg_offset, 0, iosize);
2586 flush_dcache_page(page);
2587 kunmap_atomic(userpage, KM_USER0);
2588 set_extent_uptodate(tree, cur, cur + iosize - 1,
2590 unlock_extent_cached(tree, cur, cur + iosize - 1,
2594 em = get_extent(inode, page, pg_offset, cur,
2596 if (IS_ERR_OR_NULL(em)) {
2598 unlock_extent(tree, cur, end);
2601 extent_offset = cur - em->start;
2602 BUG_ON(extent_map_end(em) <= cur);
2605 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2606 this_bio_flag = EXTENT_BIO_COMPRESSED;
2607 extent_set_compress_type(&this_bio_flag,
2611 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2612 cur_end = min(extent_map_end(em) - 1, end);
2613 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2614 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2615 disk_io_size = em->block_len;
2616 sector = em->block_start >> 9;
2618 sector = (em->block_start + extent_offset) >> 9;
2619 disk_io_size = iosize;
2622 block_start = em->block_start;
2623 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2624 block_start = EXTENT_MAP_HOLE;
2625 free_extent_map(em);
2628 /* we've found a hole, just zero and go on */
2629 if (block_start == EXTENT_MAP_HOLE) {
2631 struct extent_state *cached = NULL;
2633 userpage = kmap_atomic(page, KM_USER0);
2634 memset(userpage + pg_offset, 0, iosize);
2635 flush_dcache_page(page);
2636 kunmap_atomic(userpage, KM_USER0);
2638 set_extent_uptodate(tree, cur, cur + iosize - 1,
2640 unlock_extent_cached(tree, cur, cur + iosize - 1,
2643 pg_offset += iosize;
2646 /* the get_extent function already copied into the page */
2647 if (test_range_bit(tree, cur, cur_end,
2648 EXTENT_UPTODATE, 1, NULL)) {
2649 check_page_uptodate(tree, page);
2650 unlock_extent(tree, cur, cur + iosize - 1);
2652 pg_offset += iosize;
2655 /* we have an inline extent but it didn't get marked up
2656 * to date. Error out
2658 if (block_start == EXTENT_MAP_INLINE) {
2660 unlock_extent(tree, cur, cur + iosize - 1);
2662 pg_offset += iosize;
2667 if (tree->ops && tree->ops->readpage_io_hook) {
2668 ret = tree->ops->readpage_io_hook(page, cur,
2672 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2674 ret = submit_extent_page(READ, tree, page,
2675 sector, disk_io_size, pg_offset,
2677 end_bio_extent_readpage, mirror_num,
2681 *bio_flags = this_bio_flag;
2686 pg_offset += iosize;
2690 if (!PageError(page))
2691 SetPageUptodate(page);
2697 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2698 get_extent_t *get_extent, int mirror_num)
2700 struct bio *bio = NULL;
2701 unsigned long bio_flags = 0;
2704 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2707 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2713 static noinline void update_nr_written(struct page *page,
2714 struct writeback_control *wbc,
2715 unsigned long nr_written)
2717 wbc->nr_to_write -= nr_written;
2718 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2719 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2720 page->mapping->writeback_index = page->index + nr_written;
2724 * the writepage semantics are similar to regular writepage. extent
2725 * records are inserted to lock ranges in the tree, and as dirty areas
2726 * are found, they are marked writeback. Then the lock bits are removed
2727 * and the end_io handler clears the writeback ranges
2729 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2732 struct inode *inode = page->mapping->host;
2733 struct extent_page_data *epd = data;
2734 struct extent_io_tree *tree = epd->tree;
2735 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2737 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2741 u64 last_byte = i_size_read(inode);
2745 struct extent_state *cached_state = NULL;
2746 struct extent_map *em;
2747 struct block_device *bdev;
2750 size_t pg_offset = 0;
2752 loff_t i_size = i_size_read(inode);
2753 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2759 unsigned long nr_written = 0;
2760 bool fill_delalloc = true;
2762 if (wbc->sync_mode == WB_SYNC_ALL)
2763 write_flags = WRITE_SYNC;
2765 write_flags = WRITE;
2767 trace___extent_writepage(page, inode, wbc);
2769 WARN_ON(!PageLocked(page));
2771 ClearPageError(page);
2773 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2774 if (page->index > end_index ||
2775 (page->index == end_index && !pg_offset)) {
2776 page->mapping->a_ops->invalidatepage(page, 0);
2781 if (page->index == end_index) {
2784 userpage = kmap_atomic(page, KM_USER0);
2785 memset(userpage + pg_offset, 0,
2786 PAGE_CACHE_SIZE - pg_offset);
2787 kunmap_atomic(userpage, KM_USER0);
2788 flush_dcache_page(page);
2792 set_page_extent_mapped(page);
2794 if (!tree->ops || !tree->ops->fill_delalloc)
2795 fill_delalloc = false;
2797 delalloc_start = start;
2800 if (!epd->extent_locked && fill_delalloc) {
2801 u64 delalloc_to_write = 0;
2803 * make sure the wbc mapping index is at least updated
2806 update_nr_written(page, wbc, 0);
2808 while (delalloc_end < page_end) {
2809 nr_delalloc = find_lock_delalloc_range(inode, tree,
2814 if (nr_delalloc == 0) {
2815 delalloc_start = delalloc_end + 1;
2818 ret = tree->ops->fill_delalloc(inode, page,
2825 * delalloc_end is already one less than the total
2826 * length, so we don't subtract one from
2829 delalloc_to_write += (delalloc_end - delalloc_start +
2832 delalloc_start = delalloc_end + 1;
2834 if (wbc->nr_to_write < delalloc_to_write) {
2837 if (delalloc_to_write < thresh * 2)
2838 thresh = delalloc_to_write;
2839 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2843 /* did the fill delalloc function already unlock and start
2849 * we've unlocked the page, so we can't update
2850 * the mapping's writeback index, just update
2853 wbc->nr_to_write -= nr_written;
2857 if (tree->ops && tree->ops->writepage_start_hook) {
2858 ret = tree->ops->writepage_start_hook(page, start,
2861 /* Fixup worker will requeue */
2863 wbc->pages_skipped++;
2865 redirty_page_for_writepage(wbc, page);
2866 update_nr_written(page, wbc, nr_written);
2874 * we don't want to touch the inode after unlocking the page,
2875 * so we update the mapping writeback index now
2877 update_nr_written(page, wbc, nr_written + 1);
2880 if (last_byte <= start) {
2881 if (tree->ops && tree->ops->writepage_end_io_hook)
2882 tree->ops->writepage_end_io_hook(page, start,
2887 blocksize = inode->i_sb->s_blocksize;
2889 while (cur <= end) {
2890 if (cur >= last_byte) {
2891 if (tree->ops && tree->ops->writepage_end_io_hook)
2892 tree->ops->writepage_end_io_hook(page, cur,
2896 em = epd->get_extent(inode, page, pg_offset, cur,
2898 if (IS_ERR_OR_NULL(em)) {
2903 extent_offset = cur - em->start;
2904 BUG_ON(extent_map_end(em) <= cur);
2906 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2907 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2908 sector = (em->block_start + extent_offset) >> 9;
2910 block_start = em->block_start;
2911 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2912 free_extent_map(em);
2916 * compressed and inline extents are written through other
2919 if (compressed || block_start == EXTENT_MAP_HOLE ||
2920 block_start == EXTENT_MAP_INLINE) {
2922 * end_io notification does not happen here for
2923 * compressed extents
2925 if (!compressed && tree->ops &&
2926 tree->ops->writepage_end_io_hook)
2927 tree->ops->writepage_end_io_hook(page, cur,
2930 else if (compressed) {
2931 /* we don't want to end_page_writeback on
2932 * a compressed extent. this happens
2939 pg_offset += iosize;
2942 /* leave this out until we have a page_mkwrite call */
2943 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2944 EXTENT_DIRTY, 0, NULL)) {
2946 pg_offset += iosize;
2950 if (tree->ops && tree->ops->writepage_io_hook) {
2951 ret = tree->ops->writepage_io_hook(page, cur,
2959 unsigned long max_nr = end_index + 1;
2961 set_range_writeback(tree, cur, cur + iosize - 1);
2962 if (!PageWriteback(page)) {
2963 printk(KERN_ERR "btrfs warning page %lu not "
2964 "writeback, cur %llu end %llu\n",
2965 page->index, (unsigned long long)cur,
2966 (unsigned long long)end);
2969 ret = submit_extent_page(write_flags, tree, page,
2970 sector, iosize, pg_offset,
2971 bdev, &epd->bio, max_nr,
2972 end_bio_extent_writepage,
2978 pg_offset += iosize;
2983 /* make sure the mapping tag for page dirty gets cleared */
2984 set_page_writeback(page);
2985 end_page_writeback(page);
2991 /* drop our reference on any cached states */
2992 free_extent_state(cached_state);
2997 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2998 * @mapping: address space structure to write
2999 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3000 * @writepage: function called for each page
3001 * @data: data passed to writepage function
3003 * If a page is already under I/O, write_cache_pages() skips it, even
3004 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3005 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3006 * and msync() need to guarantee that all the data which was dirty at the time
3007 * the call was made get new I/O started against them. If wbc->sync_mode is
3008 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3009 * existing IO to complete.
3011 static int extent_write_cache_pages(struct extent_io_tree *tree,
3012 struct address_space *mapping,
3013 struct writeback_control *wbc,
3014 writepage_t writepage, void *data,
3015 void (*flush_fn)(void *))
3019 int nr_to_write_done = 0;
3020 struct pagevec pvec;
3023 pgoff_t end; /* Inclusive */
3027 pagevec_init(&pvec, 0);
3028 if (wbc->range_cyclic) {
3029 index = mapping->writeback_index; /* Start from prev offset */
3032 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3033 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3036 if (wbc->sync_mode == WB_SYNC_ALL)
3037 tag = PAGECACHE_TAG_TOWRITE;
3039 tag = PAGECACHE_TAG_DIRTY;
3041 if (wbc->sync_mode == WB_SYNC_ALL)
3042 tag_pages_for_writeback(mapping, index, end);
3043 while (!done && !nr_to_write_done && (index <= end) &&
3044 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3045 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3049 for (i = 0; i < nr_pages; i++) {
3050 struct page *page = pvec.pages[i];
3053 * At this point we hold neither mapping->tree_lock nor
3054 * lock on the page itself: the page may be truncated or
3055 * invalidated (changing page->mapping to NULL), or even
3056 * swizzled back from swapper_space to tmpfs file
3060 tree->ops->write_cache_pages_lock_hook) {
3061 tree->ops->write_cache_pages_lock_hook(page,
3064 if (!trylock_page(page)) {
3070 if (unlikely(page->mapping != mapping)) {
3075 if (!wbc->range_cyclic && page->index > end) {
3081 if (wbc->sync_mode != WB_SYNC_NONE) {
3082 if (PageWriteback(page))
3084 wait_on_page_writeback(page);
3087 if (PageWriteback(page) ||
3088 !clear_page_dirty_for_io(page)) {
3093 ret = (*writepage)(page, wbc, data);
3095 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3103 * the filesystem may choose to bump up nr_to_write.
3104 * We have to make sure to honor the new nr_to_write
3107 nr_to_write_done = wbc->nr_to_write <= 0;
3109 pagevec_release(&pvec);
3112 if (!scanned && !done) {
3114 * We hit the last page and there is more work to be done: wrap
3115 * back to the start of the file
3124 static void flush_epd_write_bio(struct extent_page_data *epd)
3133 ret = submit_one_bio(rw, epd->bio, 0, 0);
3139 static noinline void flush_write_bio(void *data)
3141 struct extent_page_data *epd = data;
3142 flush_epd_write_bio(epd);
3145 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3146 get_extent_t *get_extent,
3147 struct writeback_control *wbc)
3150 struct extent_page_data epd = {
3153 .get_extent = get_extent,
3155 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3158 ret = __extent_writepage(page, wbc, &epd);
3160 flush_epd_write_bio(&epd);
3164 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3165 u64 start, u64 end, get_extent_t *get_extent,
3169 struct address_space *mapping = inode->i_mapping;
3171 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3174 struct extent_page_data epd = {
3177 .get_extent = get_extent,
3179 .sync_io = mode == WB_SYNC_ALL,
3181 struct writeback_control wbc_writepages = {
3183 .nr_to_write = nr_pages * 2,
3184 .range_start = start,
3185 .range_end = end + 1,
3188 while (start <= end) {
3189 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3190 if (clear_page_dirty_for_io(page))
3191 ret = __extent_writepage(page, &wbc_writepages, &epd);
3193 if (tree->ops && tree->ops->writepage_end_io_hook)
3194 tree->ops->writepage_end_io_hook(page, start,
3195 start + PAGE_CACHE_SIZE - 1,
3199 page_cache_release(page);
3200 start += PAGE_CACHE_SIZE;
3203 flush_epd_write_bio(&epd);
3207 int extent_writepages(struct extent_io_tree *tree,
3208 struct address_space *mapping,
3209 get_extent_t *get_extent,
3210 struct writeback_control *wbc)
3213 struct extent_page_data epd = {
3216 .get_extent = get_extent,
3218 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3221 ret = extent_write_cache_pages(tree, mapping, wbc,
3222 __extent_writepage, &epd,
3224 flush_epd_write_bio(&epd);
3228 int extent_readpages(struct extent_io_tree *tree,
3229 struct address_space *mapping,
3230 struct list_head *pages, unsigned nr_pages,
3231 get_extent_t get_extent)
3233 struct bio *bio = NULL;
3235 unsigned long bio_flags = 0;
3237 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3238 struct page *page = list_entry(pages->prev, struct page, lru);
3240 prefetchw(&page->flags);
3241 list_del(&page->lru);
3242 if (!add_to_page_cache_lru(page, mapping,
3243 page->index, GFP_NOFS)) {
3244 __extent_read_full_page(tree, page, get_extent,
3245 &bio, 0, &bio_flags);
3247 page_cache_release(page);
3249 BUG_ON(!list_empty(pages));
3251 int ret = submit_one_bio(READ, bio, 0, bio_flags);
3258 * basic invalidatepage code, this waits on any locked or writeback
3259 * ranges corresponding to the page, and then deletes any extent state
3260 * records from the tree
3262 int extent_invalidatepage(struct extent_io_tree *tree,
3263 struct page *page, unsigned long offset)
3265 struct extent_state *cached_state = NULL;
3266 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3267 u64 end = start + PAGE_CACHE_SIZE - 1;
3268 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3270 start += (offset + blocksize - 1) & ~(blocksize - 1);
3274 lock_extent_bits(tree, start, end, 0, &cached_state);
3275 wait_on_page_writeback(page);
3276 clear_extent_bit(tree, start, end,
3277 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3278 EXTENT_DO_ACCOUNTING,
3279 1, 1, &cached_state, GFP_NOFS);
3284 * a helper for releasepage, this tests for areas of the page that
3285 * are locked or under IO and drops the related state bits if it is safe
3288 int try_release_extent_state(struct extent_map_tree *map,
3289 struct extent_io_tree *tree, struct page *page,
3292 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3293 u64 end = start + PAGE_CACHE_SIZE - 1;
3296 if (test_range_bit(tree, start, end,
3297 EXTENT_IOBITS, 0, NULL))
3300 if ((mask & GFP_NOFS) == GFP_NOFS)
3303 * at this point we can safely clear everything except the
3304 * locked bit and the nodatasum bit
3306 ret = clear_extent_bit(tree, start, end,
3307 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3310 /* if clear_extent_bit failed for enomem reasons,
3311 * we can't allow the release to continue.
3322 * a helper for releasepage. As long as there are no locked extents
3323 * in the range corresponding to the page, both state records and extent
3324 * map records are removed
3326 int try_release_extent_mapping(struct extent_map_tree *map,
3327 struct extent_io_tree *tree, struct page *page,
3330 struct extent_map *em;
3331 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3332 u64 end = start + PAGE_CACHE_SIZE - 1;
3334 if ((mask & __GFP_WAIT) &&
3335 page->mapping->host->i_size > 16 * 1024 * 1024) {
3337 while (start <= end) {
3338 len = end - start + 1;
3339 write_lock(&map->lock);
3340 em = lookup_extent_mapping(map, start, len);
3342 write_unlock(&map->lock);
3345 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3346 em->start != start) {
3347 write_unlock(&map->lock);
3348 free_extent_map(em);
3351 if (!test_range_bit(tree, em->start,
3352 extent_map_end(em) - 1,
3353 EXTENT_LOCKED | EXTENT_WRITEBACK,
3355 remove_extent_mapping(map, em);
3356 /* once for the rb tree */
3357 free_extent_map(em);
3359 start = extent_map_end(em);
3360 write_unlock(&map->lock);
3363 free_extent_map(em);
3366 return try_release_extent_state(map, tree, page, mask);
3370 * helper function for fiemap, which doesn't want to see any holes.
3371 * This maps until we find something past 'last'
3373 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3376 get_extent_t *get_extent)
3378 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3379 struct extent_map *em;
3386 len = last - offset;
3389 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3390 em = get_extent(inode, NULL, 0, offset, len, 0);
3391 if (IS_ERR_OR_NULL(em))
3394 /* if this isn't a hole return it */
3395 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3396 em->block_start != EXTENT_MAP_HOLE) {
3400 /* this is a hole, advance to the next extent */
3401 offset = extent_map_end(em);
3402 free_extent_map(em);
3409 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3410 __u64 start, __u64 len, get_extent_t *get_extent)
3414 u64 max = start + len;
3418 u64 last_for_get_extent = 0;
3420 u64 isize = i_size_read(inode);
3421 struct btrfs_key found_key;
3422 struct extent_map *em = NULL;
3423 struct extent_state *cached_state = NULL;
3424 struct btrfs_path *path;
3425 struct btrfs_file_extent_item *item;
3430 unsigned long emflags;
3435 path = btrfs_alloc_path();
3438 path->leave_spinning = 1;
3440 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3441 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3444 * lookup the last file extent. We're not using i_size here
3445 * because there might be preallocation past i_size
3447 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3448 path, btrfs_ino(inode), -1, 0);
3450 btrfs_free_path(path);
3455 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3456 struct btrfs_file_extent_item);
3457 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3458 found_type = btrfs_key_type(&found_key);
3460 /* No extents, but there might be delalloc bits */
3461 if (found_key.objectid != btrfs_ino(inode) ||
3462 found_type != BTRFS_EXTENT_DATA_KEY) {
3463 /* have to trust i_size as the end */
3465 last_for_get_extent = isize;
3468 * remember the start of the last extent. There are a
3469 * bunch of different factors that go into the length of the
3470 * extent, so its much less complex to remember where it started
3472 last = found_key.offset;
3473 last_for_get_extent = last + 1;
3475 btrfs_free_path(path);
3478 * we might have some extents allocated but more delalloc past those
3479 * extents. so, we trust isize unless the start of the last extent is
3484 last_for_get_extent = isize;
3487 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3490 em = get_extent_skip_holes(inode, start, last_for_get_extent,
3500 u64 offset_in_extent;
3502 /* break if the extent we found is outside the range */
3503 if (em->start >= max || extent_map_end(em) < off)
3507 * get_extent may return an extent that starts before our
3508 * requested range. We have to make sure the ranges
3509 * we return to fiemap always move forward and don't
3510 * overlap, so adjust the offsets here
3512 em_start = max(em->start, off);
3515 * record the offset from the start of the extent
3516 * for adjusting the disk offset below
3518 offset_in_extent = em_start - em->start;
3519 em_end = extent_map_end(em);
3520 em_len = em_end - em_start;
3521 emflags = em->flags;
3526 * bump off for our next call to get_extent
3528 off = extent_map_end(em);
3532 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
3534 flags |= FIEMAP_EXTENT_LAST;
3535 } else if (em->block_start == EXTENT_MAP_INLINE) {
3536 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3537 FIEMAP_EXTENT_NOT_ALIGNED);
3538 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
3539 flags |= (FIEMAP_EXTENT_DELALLOC |
3540 FIEMAP_EXTENT_UNKNOWN);
3542 disko = em->block_start + offset_in_extent;
3544 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3545 flags |= FIEMAP_EXTENT_ENCODED;
3547 free_extent_map(em);
3549 if ((em_start >= last) || em_len == (u64)-1 ||
3550 (last == (u64)-1 && isize <= em_end)) {
3551 flags |= FIEMAP_EXTENT_LAST;
3555 /* now scan forward to see if this is really the last extent. */
3556 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3563 flags |= FIEMAP_EXTENT_LAST;
3566 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3572 free_extent_map(em);
3574 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3575 &cached_state, GFP_NOFS);
3579 inline struct page *extent_buffer_page(struct extent_buffer *eb,
3583 struct address_space *mapping;
3586 return eb->first_page;
3587 i += eb->start >> PAGE_CACHE_SHIFT;
3588 mapping = eb->first_page->mapping;
3593 * extent_buffer_page is only called after pinning the page
3594 * by increasing the reference count. So we know the page must
3595 * be in the radix tree.
3598 p = radix_tree_lookup(&mapping->page_tree, i);
3604 inline unsigned long num_extent_pages(u64 start, u64 len)
3606 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3607 (start >> PAGE_CACHE_SHIFT);
3610 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3615 struct extent_buffer *eb = NULL;
3617 unsigned long flags;
3620 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
3625 rwlock_init(&eb->lock);
3626 atomic_set(&eb->write_locks, 0);
3627 atomic_set(&eb->read_locks, 0);
3628 atomic_set(&eb->blocking_readers, 0);
3629 atomic_set(&eb->blocking_writers, 0);
3630 atomic_set(&eb->spinning_readers, 0);
3631 atomic_set(&eb->spinning_writers, 0);
3632 eb->lock_nested = 0;
3633 init_waitqueue_head(&eb->write_lock_wq);
3634 init_waitqueue_head(&eb->read_lock_wq);
3637 spin_lock_irqsave(&leak_lock, flags);
3638 list_add(&eb->leak_list, &buffers);
3639 spin_unlock_irqrestore(&leak_lock, flags);
3641 atomic_set(&eb->refs, 1);
3646 static void __free_extent_buffer(struct extent_buffer *eb)
3649 unsigned long flags;
3650 spin_lock_irqsave(&leak_lock, flags);
3651 list_del(&eb->leak_list);
3652 spin_unlock_irqrestore(&leak_lock, flags);
3654 kmem_cache_free(extent_buffer_cache, eb);
3658 * Helper for releasing extent buffer page.
3660 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3661 unsigned long start_idx)
3663 unsigned long index;
3666 if (!eb->first_page)
3669 index = num_extent_pages(eb->start, eb->len);
3670 if (start_idx >= index)
3675 page = extent_buffer_page(eb, index);
3677 page_cache_release(page);
3678 } while (index != start_idx);
3682 * Helper for releasing the extent buffer.
3684 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3686 btrfs_release_extent_buffer_page(eb, 0);
3687 __free_extent_buffer(eb);
3690 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3691 u64 start, unsigned long len,
3694 unsigned long num_pages = num_extent_pages(start, len);
3696 unsigned long index = start >> PAGE_CACHE_SHIFT;
3697 struct extent_buffer *eb;
3698 struct extent_buffer *exists = NULL;
3700 struct address_space *mapping = tree->mapping;
3705 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3706 if (eb && atomic_inc_not_zero(&eb->refs)) {
3708 mark_page_accessed(eb->first_page);
3713 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
3718 eb->first_page = page0;
3721 page_cache_get(page0);
3722 mark_page_accessed(page0);
3723 set_page_extent_mapped(page0);
3724 set_page_extent_head(page0, len);
3725 uptodate = PageUptodate(page0);
3729 for (; i < num_pages; i++, index++) {
3730 p = find_or_create_page(mapping, index, GFP_NOFS);
3735 set_page_extent_mapped(p);
3736 mark_page_accessed(p);
3739 set_page_extent_head(p, len);
3741 set_page_private(p, EXTENT_PAGE_PRIVATE);
3743 if (!PageUptodate(p))
3747 * see below about how we avoid a nasty race with release page
3748 * and why we unlock later
3754 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3756 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3760 spin_lock(&tree->buffer_lock);
3761 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3762 if (ret == -EEXIST) {
3763 exists = radix_tree_lookup(&tree->buffer,
3764 start >> PAGE_CACHE_SHIFT);
3765 /* add one reference for the caller */
3766 atomic_inc(&exists->refs);
3767 spin_unlock(&tree->buffer_lock);
3768 radix_tree_preload_end();
3771 /* add one reference for the tree */
3772 atomic_inc(&eb->refs);
3773 spin_unlock(&tree->buffer_lock);
3774 radix_tree_preload_end();
3777 * there is a race where release page may have
3778 * tried to find this extent buffer in the radix
3779 * but failed. It will tell the VM it is safe to
3780 * reclaim the, and it will clear the page private bit.
3781 * We must make sure to set the page private bit properly
3782 * after the extent buffer is in the radix tree so
3783 * it doesn't get lost
3785 set_page_extent_mapped(eb->first_page);
3786 set_page_extent_head(eb->first_page, eb->len);
3788 unlock_page(eb->first_page);
3792 if (eb->first_page && !page0)
3793 unlock_page(eb->first_page);
3795 if (!atomic_dec_and_test(&eb->refs))
3797 btrfs_release_extent_buffer(eb);
3801 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3802 u64 start, unsigned long len)
3804 struct extent_buffer *eb;
3807 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3808 if (eb && atomic_inc_not_zero(&eb->refs)) {
3810 mark_page_accessed(eb->first_page);
3818 void free_extent_buffer(struct extent_buffer *eb)
3823 if (!atomic_dec_and_test(&eb->refs))
3829 void clear_extent_buffer_dirty(struct extent_io_tree *tree,
3830 struct extent_buffer *eb)
3833 unsigned long num_pages;
3836 num_pages = num_extent_pages(eb->start, eb->len);
3838 for (i = 0; i < num_pages; i++) {
3839 page = extent_buffer_page(eb, i);
3840 if (!PageDirty(page))
3844 WARN_ON(!PagePrivate(page));
3846 set_page_extent_mapped(page);
3848 set_page_extent_head(page, eb->len);
3850 clear_page_dirty_for_io(page);
3851 spin_lock_irq(&page->mapping->tree_lock);
3852 if (!PageDirty(page)) {
3853 radix_tree_tag_clear(&page->mapping->page_tree,
3855 PAGECACHE_TAG_DIRTY);
3857 spin_unlock_irq(&page->mapping->tree_lock);
3858 ClearPageError(page);
3863 int set_extent_buffer_dirty(struct extent_io_tree *tree,
3864 struct extent_buffer *eb)
3867 unsigned long num_pages;
3870 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3871 num_pages = num_extent_pages(eb->start, eb->len);
3872 for (i = 0; i < num_pages; i++)
3873 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
3877 static int __eb_straddles_pages(u64 start, u64 len)
3879 if (len < PAGE_CACHE_SIZE)
3881 if (start & (PAGE_CACHE_SIZE - 1))
3883 if ((start + len) & (PAGE_CACHE_SIZE - 1))
3888 static int eb_straddles_pages(struct extent_buffer *eb)
3890 return __eb_straddles_pages(eb->start, eb->len);
3893 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3894 struct extent_buffer *eb,
3895 struct extent_state **cached_state)
3899 unsigned long num_pages;
3901 num_pages = num_extent_pages(eb->start, eb->len);
3902 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3904 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3905 cached_state, GFP_NOFS);
3907 for (i = 0; i < num_pages; i++) {
3908 page = extent_buffer_page(eb, i);
3910 ClearPageUptodate(page);
3915 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3916 struct extent_buffer *eb)
3920 unsigned long num_pages;
3922 num_pages = num_extent_pages(eb->start, eb->len);
3924 if (eb_straddles_pages(eb)) {
3925 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3928 for (i = 0; i < num_pages; i++) {
3929 page = extent_buffer_page(eb, i);
3930 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3931 ((i == num_pages - 1) &&
3932 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3933 check_page_uptodate(tree, page);
3936 SetPageUptodate(page);
3941 int extent_range_uptodate(struct extent_io_tree *tree,
3946 int pg_uptodate = 1;
3948 unsigned long index;
3950 if (__eb_straddles_pages(start, end - start + 1)) {
3951 ret = test_range_bit(tree, start, end,
3952 EXTENT_UPTODATE, 1, NULL);
3956 while (start <= end) {
3957 index = start >> PAGE_CACHE_SHIFT;
3958 page = find_get_page(tree->mapping, index);
3961 uptodate = PageUptodate(page);
3962 page_cache_release(page);
3967 start += PAGE_CACHE_SIZE;
3972 int extent_buffer_uptodate(struct extent_io_tree *tree,
3973 struct extent_buffer *eb,
3974 struct extent_state *cached_state)
3977 unsigned long num_pages;
3980 int pg_uptodate = 1;
3982 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3985 if (eb_straddles_pages(eb)) {
3986 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3987 EXTENT_UPTODATE, 1, cached_state);
3992 num_pages = num_extent_pages(eb->start, eb->len);
3993 for (i = 0; i < num_pages; i++) {
3994 page = extent_buffer_page(eb, i);
3995 if (!PageUptodate(page)) {
4003 int read_extent_buffer_pages(struct extent_io_tree *tree,
4004 struct extent_buffer *eb, u64 start, int wait,
4005 get_extent_t *get_extent, int mirror_num)
4008 unsigned long start_i;
4012 int locked_pages = 0;
4013 int all_uptodate = 1;
4014 int inc_all_pages = 0;
4015 unsigned long num_pages;
4016 struct bio *bio = NULL;
4017 unsigned long bio_flags = 0;
4019 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4022 if (eb_straddles_pages(eb)) {
4023 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
4024 EXTENT_UPTODATE, 1, NULL)) {
4030 WARN_ON(start < eb->start);
4031 start_i = (start >> PAGE_CACHE_SHIFT) -
4032 (eb->start >> PAGE_CACHE_SHIFT);
4037 num_pages = num_extent_pages(eb->start, eb->len);
4038 for (i = start_i; i < num_pages; i++) {
4039 page = extent_buffer_page(eb, i);
4040 if (wait == WAIT_NONE) {
4041 if (!trylock_page(page))
4047 if (!PageUptodate(page))
4052 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4056 for (i = start_i; i < num_pages; i++) {
4057 page = extent_buffer_page(eb, i);
4059 WARN_ON(!PagePrivate(page));
4061 set_page_extent_mapped(page);
4063 set_page_extent_head(page, eb->len);
4066 page_cache_get(page);
4067 if (!PageUptodate(page)) {
4070 ClearPageError(page);
4071 err = __extent_read_full_page(tree, page,
4073 mirror_num, &bio_flags);
4082 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
4086 if (ret || wait != WAIT_COMPLETE)
4089 for (i = start_i; i < num_pages; i++) {
4090 page = extent_buffer_page(eb, i);
4091 wait_on_page_locked(page);
4092 if (!PageUptodate(page))
4097 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4102 while (locked_pages > 0) {
4103 page = extent_buffer_page(eb, i);
4111 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4112 unsigned long start,
4119 char *dst = (char *)dstv;
4120 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4121 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4123 WARN_ON(start > eb->len);
4124 WARN_ON(start + len > eb->start + eb->len);
4126 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4129 page = extent_buffer_page(eb, i);
4131 cur = min(len, (PAGE_CACHE_SIZE - offset));
4132 kaddr = page_address(page);
4133 memcpy(dst, kaddr + offset, cur);
4142 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4143 unsigned long min_len, char **map,
4144 unsigned long *map_start,
4145 unsigned long *map_len)
4147 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4150 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4151 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4152 unsigned long end_i = (start_offset + start + min_len - 1) >>
4159 offset = start_offset;
4163 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4166 if (start + min_len > eb->len) {
4167 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4168 "wanted %lu %lu\n", (unsigned long long)eb->start,
4169 eb->len, start, min_len);
4174 p = extent_buffer_page(eb, i);
4175 kaddr = page_address(p);
4176 *map = kaddr + offset;
4177 *map_len = PAGE_CACHE_SIZE - offset;
4181 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4182 unsigned long start,
4189 char *ptr = (char *)ptrv;
4190 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4191 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4194 WARN_ON(start > eb->len);
4195 WARN_ON(start + len > eb->start + eb->len);
4197 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4200 page = extent_buffer_page(eb, i);
4202 cur = min(len, (PAGE_CACHE_SIZE - offset));
4204 kaddr = page_address(page);
4205 ret = memcmp(ptr, kaddr + offset, cur);
4217 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4218 unsigned long start, unsigned long len)
4224 char *src = (char *)srcv;
4225 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4226 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4228 WARN_ON(start > eb->len);
4229 WARN_ON(start + len > eb->start + eb->len);
4231 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4234 page = extent_buffer_page(eb, i);
4235 WARN_ON(!PageUptodate(page));
4237 cur = min(len, PAGE_CACHE_SIZE - offset);
4238 kaddr = page_address(page);
4239 memcpy(kaddr + offset, src, cur);
4248 void memset_extent_buffer(struct extent_buffer *eb, char c,
4249 unsigned long start, unsigned long len)
4255 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4256 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4258 WARN_ON(start > eb->len);
4259 WARN_ON(start + len > eb->start + eb->len);
4261 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4264 page = extent_buffer_page(eb, i);
4265 WARN_ON(!PageUptodate(page));
4267 cur = min(len, PAGE_CACHE_SIZE - offset);
4268 kaddr = page_address(page);
4269 memset(kaddr + offset, c, cur);
4277 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4278 unsigned long dst_offset, unsigned long src_offset,
4281 u64 dst_len = dst->len;
4286 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4287 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4289 WARN_ON(src->len != dst_len);
4291 offset = (start_offset + dst_offset) &
4292 ((unsigned long)PAGE_CACHE_SIZE - 1);
4295 page = extent_buffer_page(dst, i);
4296 WARN_ON(!PageUptodate(page));
4298 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4300 kaddr = page_address(page);
4301 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4310 static void move_pages(struct page *dst_page, struct page *src_page,
4311 unsigned long dst_off, unsigned long src_off,
4314 char *dst_kaddr = page_address(dst_page);
4315 if (dst_page == src_page) {
4316 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4318 char *src_kaddr = page_address(src_page);
4319 char *p = dst_kaddr + dst_off + len;
4320 char *s = src_kaddr + src_off + len;
4327 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4329 unsigned long distance = (src > dst) ? src - dst : dst - src;
4330 return distance < len;
4333 static void copy_pages(struct page *dst_page, struct page *src_page,
4334 unsigned long dst_off, unsigned long src_off,
4337 char *dst_kaddr = page_address(dst_page);
4340 if (dst_page != src_page) {
4341 src_kaddr = page_address(src_page);
4343 src_kaddr = dst_kaddr;
4344 BUG_ON(areas_overlap(src_off, dst_off, len));
4347 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
4350 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4351 unsigned long src_offset, unsigned long len)
4354 size_t dst_off_in_page;
4355 size_t src_off_in_page;
4356 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4357 unsigned long dst_i;
4358 unsigned long src_i;
4360 if (src_offset + len > dst->len) {
4361 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4362 "len %lu dst len %lu\n", src_offset, len, dst->len);
4365 if (dst_offset + len > dst->len) {
4366 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4367 "len %lu dst len %lu\n", dst_offset, len, dst->len);
4372 dst_off_in_page = (start_offset + dst_offset) &
4373 ((unsigned long)PAGE_CACHE_SIZE - 1);
4374 src_off_in_page = (start_offset + src_offset) &
4375 ((unsigned long)PAGE_CACHE_SIZE - 1);
4377 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4378 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4380 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4382 cur = min_t(unsigned long, cur,
4383 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4385 copy_pages(extent_buffer_page(dst, dst_i),
4386 extent_buffer_page(dst, src_i),
4387 dst_off_in_page, src_off_in_page, cur);
4395 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4396 unsigned long src_offset, unsigned long len)
4399 size_t dst_off_in_page;
4400 size_t src_off_in_page;
4401 unsigned long dst_end = dst_offset + len - 1;
4402 unsigned long src_end = src_offset + len - 1;
4403 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4404 unsigned long dst_i;
4405 unsigned long src_i;
4407 if (src_offset + len > dst->len) {
4408 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4409 "len %lu len %lu\n", src_offset, len, dst->len);
4412 if (dst_offset + len > dst->len) {
4413 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4414 "len %lu len %lu\n", dst_offset, len, dst->len);
4417 if (!areas_overlap(src_offset, dst_offset, len)) {
4418 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4422 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4423 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4425 dst_off_in_page = (start_offset + dst_end) &
4426 ((unsigned long)PAGE_CACHE_SIZE - 1);
4427 src_off_in_page = (start_offset + src_end) &
4428 ((unsigned long)PAGE_CACHE_SIZE - 1);
4430 cur = min_t(unsigned long, len, src_off_in_page + 1);
4431 cur = min(cur, dst_off_in_page + 1);
4432 move_pages(extent_buffer_page(dst, dst_i),
4433 extent_buffer_page(dst, src_i),
4434 dst_off_in_page - cur + 1,
4435 src_off_in_page - cur + 1, cur);
4443 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4445 struct extent_buffer *eb =
4446 container_of(head, struct extent_buffer, rcu_head);
4448 btrfs_release_extent_buffer(eb);
4451 int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4453 u64 start = page_offset(page);
4454 struct extent_buffer *eb;
4457 spin_lock(&tree->buffer_lock);
4458 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4460 spin_unlock(&tree->buffer_lock);
4464 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4470 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4473 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4478 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4480 spin_unlock(&tree->buffer_lock);
4482 /* at this point we can safely release the extent buffer */
4483 if (atomic_read(&eb->refs) == 0)
4484 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);