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 int __init extent_io_init(void)
58 extent_state_cache = kmem_cache_create("extent_state",
59 sizeof(struct extent_state), 0,
60 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
61 if (!extent_state_cache)
64 extent_buffer_cache = kmem_cache_create("extent_buffers",
65 sizeof(struct extent_buffer), 0,
66 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
67 if (!extent_buffer_cache)
68 goto free_state_cache;
72 kmem_cache_destroy(extent_state_cache);
76 void extent_io_exit(void)
78 struct extent_state *state;
79 struct extent_buffer *eb;
81 while (!list_empty(&states)) {
82 state = list_entry(states.next, struct extent_state, leak_list);
83 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
84 "state %lu in tree %p refs %d\n",
85 (unsigned long long)state->start,
86 (unsigned long long)state->end,
87 state->state, state->tree, atomic_read(&state->refs));
88 list_del(&state->leak_list);
89 kmem_cache_free(extent_state_cache, state);
93 while (!list_empty(&buffers)) {
94 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
95 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
96 "refs %d\n", (unsigned long long)eb->start,
97 eb->len, atomic_read(&eb->refs));
98 list_del(&eb->leak_list);
99 kmem_cache_free(extent_buffer_cache, eb);
101 if (extent_state_cache)
102 kmem_cache_destroy(extent_state_cache);
103 if (extent_buffer_cache)
104 kmem_cache_destroy(extent_buffer_cache);
107 void extent_io_tree_init(struct extent_io_tree *tree,
108 struct address_space *mapping)
110 tree->state = RB_ROOT;
111 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
113 tree->dirty_bytes = 0;
114 spin_lock_init(&tree->lock);
115 spin_lock_init(&tree->buffer_lock);
116 tree->mapping = mapping;
119 static struct extent_state *alloc_extent_state(gfp_t mask)
121 struct extent_state *state;
126 state = kmem_cache_alloc(extent_state_cache, mask);
133 spin_lock_irqsave(&leak_lock, flags);
134 list_add(&state->leak_list, &states);
135 spin_unlock_irqrestore(&leak_lock, flags);
137 atomic_set(&state->refs, 1);
138 init_waitqueue_head(&state->wq);
142 void free_extent_state(struct extent_state *state)
146 if (atomic_dec_and_test(&state->refs)) {
150 WARN_ON(state->tree);
152 spin_lock_irqsave(&leak_lock, flags);
153 list_del(&state->leak_list);
154 spin_unlock_irqrestore(&leak_lock, flags);
156 kmem_cache_free(extent_state_cache, state);
160 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
161 struct rb_node *node)
163 struct rb_node **p = &root->rb_node;
164 struct rb_node *parent = NULL;
165 struct tree_entry *entry;
169 entry = rb_entry(parent, struct tree_entry, rb_node);
171 if (offset < entry->start)
173 else if (offset > entry->end)
179 entry = rb_entry(node, struct tree_entry, rb_node);
180 rb_link_node(node, parent, p);
181 rb_insert_color(node, root);
185 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
186 struct rb_node **prev_ret,
187 struct rb_node **next_ret)
189 struct rb_root *root = &tree->state;
190 struct rb_node *n = root->rb_node;
191 struct rb_node *prev = NULL;
192 struct rb_node *orig_prev = NULL;
193 struct tree_entry *entry;
194 struct tree_entry *prev_entry = NULL;
197 entry = rb_entry(n, struct tree_entry, rb_node);
201 if (offset < entry->start)
203 else if (offset > entry->end)
211 while (prev && offset > prev_entry->end) {
212 prev = rb_next(prev);
213 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
221 while (prev && offset < prev_entry->start) {
222 prev = rb_prev(prev);
223 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
230 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
233 struct rb_node *prev = NULL;
236 ret = __etree_search(tree, offset, &prev, NULL);
242 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
243 struct extent_state *other)
245 if (tree->ops && tree->ops->merge_extent_hook)
246 tree->ops->merge_extent_hook(tree->mapping->host, new,
251 * utility function to look for merge candidates inside a given range.
252 * Any extents with matching state are merged together into a single
253 * extent in the tree. Extents with EXTENT_IO in their state field
254 * are not merged because the end_io handlers need to be able to do
255 * operations on them without sleeping (or doing allocations/splits).
257 * This should be called with the tree lock held.
259 static void merge_state(struct extent_io_tree *tree,
260 struct extent_state *state)
262 struct extent_state *other;
263 struct rb_node *other_node;
265 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
268 other_node = rb_prev(&state->rb_node);
270 other = rb_entry(other_node, struct extent_state, rb_node);
271 if (other->end == state->start - 1 &&
272 other->state == state->state) {
273 merge_cb(tree, state, other);
274 state->start = other->start;
276 rb_erase(&other->rb_node, &tree->state);
277 free_extent_state(other);
280 other_node = rb_next(&state->rb_node);
282 other = rb_entry(other_node, struct extent_state, rb_node);
283 if (other->start == state->end + 1 &&
284 other->state == state->state) {
285 merge_cb(tree, state, other);
286 state->end = other->end;
288 rb_erase(&other->rb_node, &tree->state);
289 free_extent_state(other);
294 static void set_state_cb(struct extent_io_tree *tree,
295 struct extent_state *state, int *bits)
297 if (tree->ops && tree->ops->set_bit_hook)
298 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
301 static void clear_state_cb(struct extent_io_tree *tree,
302 struct extent_state *state, int *bits)
304 if (tree->ops && tree->ops->clear_bit_hook)
305 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
308 static void set_state_bits(struct extent_io_tree *tree,
309 struct extent_state *state, int *bits);
312 * insert an extent_state struct into the tree. 'bits' are set on the
313 * struct before it is inserted.
315 * This may return -EEXIST if the extent is already there, in which case the
316 * state struct is freed.
318 * The tree lock is not taken internally. This is a utility function and
319 * probably isn't what you want to call (see set/clear_extent_bit).
321 static int insert_state(struct extent_io_tree *tree,
322 struct extent_state *state, u64 start, u64 end,
325 struct rb_node *node;
328 printk(KERN_ERR "btrfs end < start %llu %llu\n",
329 (unsigned long long)end,
330 (unsigned long long)start);
333 state->start = start;
336 set_state_bits(tree, state, bits);
338 node = tree_insert(&tree->state, end, &state->rb_node);
340 struct extent_state *found;
341 found = rb_entry(node, struct extent_state, rb_node);
342 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
343 "%llu %llu\n", (unsigned long long)found->start,
344 (unsigned long long)found->end,
345 (unsigned long long)start, (unsigned long long)end);
349 merge_state(tree, state);
353 static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
356 if (tree->ops && tree->ops->split_extent_hook)
357 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
361 * split a given extent state struct in two, inserting the preallocated
362 * struct 'prealloc' as the newly created second half. 'split' indicates an
363 * offset inside 'orig' where it should be split.
366 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
367 * are two extent state structs in the tree:
368 * prealloc: [orig->start, split - 1]
369 * orig: [ split, orig->end ]
371 * The tree locks are not taken by this function. They need to be held
374 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
375 struct extent_state *prealloc, u64 split)
377 struct rb_node *node;
379 split_cb(tree, orig, split);
381 prealloc->start = orig->start;
382 prealloc->end = split - 1;
383 prealloc->state = orig->state;
386 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
388 free_extent_state(prealloc);
391 prealloc->tree = tree;
396 * utility function to clear some bits in an extent state struct.
397 * it will optionally wake up any one waiting on this state (wake == 1), or
398 * forcibly remove the state from the tree (delete == 1).
400 * If no bits are set on the state struct after clearing things, the
401 * struct is freed and removed from the tree
403 static int clear_state_bit(struct extent_io_tree *tree,
404 struct extent_state *state,
407 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
408 int ret = state->state & bits_to_clear;
410 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
411 u64 range = state->end - state->start + 1;
412 WARN_ON(range > tree->dirty_bytes);
413 tree->dirty_bytes -= range;
415 clear_state_cb(tree, state, bits);
416 state->state &= ~bits_to_clear;
419 if (state->state == 0) {
421 rb_erase(&state->rb_node, &tree->state);
423 free_extent_state(state);
428 merge_state(tree, state);
433 static struct extent_state *
434 alloc_extent_state_atomic(struct extent_state *prealloc)
437 prealloc = alloc_extent_state(GFP_ATOMIC);
443 * clear some bits on a range in the tree. This may require splitting
444 * or inserting elements in the tree, so the gfp mask is used to
445 * indicate which allocations or sleeping are allowed.
447 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
448 * the given range from the tree regardless of state (ie for truncate).
450 * the range [start, end] is inclusive.
452 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
453 * bits were already set, or zero if none of the bits were already set.
455 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
456 int bits, int wake, int delete,
457 struct extent_state **cached_state,
460 struct extent_state *state;
461 struct extent_state *cached;
462 struct extent_state *prealloc = NULL;
463 struct rb_node *next_node;
464 struct rb_node *node;
471 bits |= ~EXTENT_CTLBITS;
472 bits |= EXTENT_FIRST_DELALLOC;
474 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
477 if (!prealloc && (mask & __GFP_WAIT)) {
478 prealloc = alloc_extent_state(mask);
483 spin_lock(&tree->lock);
485 cached = *cached_state;
488 *cached_state = NULL;
492 if (cached && cached->tree && cached->start <= start &&
493 cached->end > start) {
495 atomic_dec(&cached->refs);
500 free_extent_state(cached);
503 * this search will find the extents that end after
506 node = tree_search(tree, start);
509 state = rb_entry(node, struct extent_state, rb_node);
511 if (state->start > end)
513 WARN_ON(state->end < start);
514 last_end = state->end;
516 if (state->end < end && !need_resched())
517 next_node = rb_next(&state->rb_node);
521 /* the state doesn't have the wanted bits, go ahead */
522 if (!(state->state & bits))
526 * | ---- desired range ---- |
528 * | ------------- state -------------- |
530 * We need to split the extent we found, and may flip
531 * bits on second half.
533 * If the extent we found extends past our range, we
534 * just split and search again. It'll get split again
535 * the next time though.
537 * If the extent we found is inside our range, we clear
538 * the desired bit on it.
541 if (state->start < start) {
542 prealloc = alloc_extent_state_atomic(prealloc);
544 err = split_state(tree, state, prealloc, start);
545 BUG_ON(err == -EEXIST);
549 if (state->end <= end) {
550 set |= clear_state_bit(tree, state, &bits, wake);
551 if (last_end == (u64)-1)
553 start = last_end + 1;
558 * | ---- desired range ---- |
560 * We need to split the extent, and clear the bit
563 if (state->start <= end && state->end > end) {
564 prealloc = alloc_extent_state_atomic(prealloc);
566 err = split_state(tree, state, prealloc, end + 1);
567 BUG_ON(err == -EEXIST);
571 set |= clear_state_bit(tree, prealloc, &bits, wake);
577 set |= clear_state_bit(tree, state, &bits, wake);
579 if (last_end == (u64)-1)
581 start = last_end + 1;
582 if (start <= end && next_node) {
583 state = rb_entry(next_node, struct extent_state,
590 spin_unlock(&tree->lock);
592 free_extent_state(prealloc);
599 spin_unlock(&tree->lock);
600 if (mask & __GFP_WAIT)
605 static int wait_on_state(struct extent_io_tree *tree,
606 struct extent_state *state)
607 __releases(tree->lock)
608 __acquires(tree->lock)
611 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
612 spin_unlock(&tree->lock);
614 spin_lock(&tree->lock);
615 finish_wait(&state->wq, &wait);
620 * waits for one or more bits to clear on a range in the state tree.
621 * The range [start, end] is inclusive.
622 * The tree lock is taken by this function
624 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
626 struct extent_state *state;
627 struct rb_node *node;
629 spin_lock(&tree->lock);
633 * this search will find all the extents that end after
636 node = tree_search(tree, start);
640 state = rb_entry(node, struct extent_state, rb_node);
642 if (state->start > end)
645 if (state->state & bits) {
646 start = state->start;
647 atomic_inc(&state->refs);
648 wait_on_state(tree, state);
649 free_extent_state(state);
652 start = state->end + 1;
657 cond_resched_lock(&tree->lock);
660 spin_unlock(&tree->lock);
664 static void set_state_bits(struct extent_io_tree *tree,
665 struct extent_state *state,
668 int bits_to_set = *bits & ~EXTENT_CTLBITS;
670 set_state_cb(tree, state, bits);
671 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
672 u64 range = state->end - state->start + 1;
673 tree->dirty_bytes += range;
675 state->state |= bits_to_set;
678 static void cache_state(struct extent_state *state,
679 struct extent_state **cached_ptr)
681 if (cached_ptr && !(*cached_ptr)) {
682 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
684 atomic_inc(&state->refs);
689 static void uncache_state(struct extent_state **cached_ptr)
691 if (cached_ptr && (*cached_ptr)) {
692 struct extent_state *state = *cached_ptr;
694 free_extent_state(state);
699 * set some bits on a range in the tree. This may require allocations or
700 * sleeping, so the gfp mask is used to indicate what is allowed.
702 * If any of the exclusive bits are set, this will fail with -EEXIST if some
703 * part of the range already has the desired bits set. The start of the
704 * existing range is returned in failed_start in this case.
706 * [start, end] is inclusive This takes the tree lock.
709 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
710 int bits, int exclusive_bits, u64 *failed_start,
711 struct extent_state **cached_state, gfp_t mask)
713 struct extent_state *state;
714 struct extent_state *prealloc = NULL;
715 struct rb_node *node;
720 bits |= EXTENT_FIRST_DELALLOC;
722 if (!prealloc && (mask & __GFP_WAIT)) {
723 prealloc = alloc_extent_state(mask);
727 spin_lock(&tree->lock);
728 if (cached_state && *cached_state) {
729 state = *cached_state;
730 if (state->start <= start && state->end > start &&
732 node = &state->rb_node;
737 * this search will find all the extents that end after
740 node = tree_search(tree, start);
742 prealloc = alloc_extent_state_atomic(prealloc);
744 err = insert_state(tree, prealloc, start, end, &bits);
746 BUG_ON(err == -EEXIST);
749 state = rb_entry(node, struct extent_state, rb_node);
751 last_start = state->start;
752 last_end = state->end;
755 * | ---- desired range ---- |
758 * Just lock what we found and keep going
760 if (state->start == start && state->end <= end) {
761 struct rb_node *next_node;
762 if (state->state & exclusive_bits) {
763 *failed_start = state->start;
768 set_state_bits(tree, state, &bits);
770 cache_state(state, cached_state);
771 merge_state(tree, state);
772 if (last_end == (u64)-1)
775 start = last_end + 1;
776 next_node = rb_next(&state->rb_node);
777 if (next_node && start < end && prealloc && !need_resched()) {
778 state = rb_entry(next_node, struct extent_state,
780 if (state->start == start)
787 * | ---- desired range ---- |
790 * | ------------- state -------------- |
792 * We need to split the extent we found, and may flip bits on
795 * If the extent we found extends past our
796 * range, we just split and search again. It'll get split
797 * again the next time though.
799 * If the extent we found is inside our range, we set the
802 if (state->start < start) {
803 if (state->state & exclusive_bits) {
804 *failed_start = start;
809 prealloc = alloc_extent_state_atomic(prealloc);
811 err = split_state(tree, state, prealloc, start);
812 BUG_ON(err == -EEXIST);
816 if (state->end <= end) {
817 set_state_bits(tree, state, &bits);
818 cache_state(state, cached_state);
819 merge_state(tree, state);
820 if (last_end == (u64)-1)
822 start = last_end + 1;
827 * | ---- desired range ---- |
828 * | state | or | state |
830 * There's a hole, we need to insert something in it and
831 * ignore the extent we found.
833 if (state->start > start) {
835 if (end < last_start)
838 this_end = last_start - 1;
840 prealloc = alloc_extent_state_atomic(prealloc);
844 * Avoid to free 'prealloc' if it can be merged with
847 err = insert_state(tree, prealloc, start, this_end,
849 BUG_ON(err == -EEXIST);
851 free_extent_state(prealloc);
855 cache_state(prealloc, cached_state);
857 start = this_end + 1;
861 * | ---- desired range ---- |
863 * We need to split the extent, and set the bit
866 if (state->start <= end && state->end > end) {
867 if (state->state & exclusive_bits) {
868 *failed_start = start;
873 prealloc = alloc_extent_state_atomic(prealloc);
875 err = split_state(tree, state, prealloc, end + 1);
876 BUG_ON(err == -EEXIST);
878 set_state_bits(tree, prealloc, &bits);
879 cache_state(prealloc, cached_state);
880 merge_state(tree, prealloc);
888 spin_unlock(&tree->lock);
890 free_extent_state(prealloc);
897 spin_unlock(&tree->lock);
898 if (mask & __GFP_WAIT)
904 * convert_extent - convert all bits in a given range from one bit to another
905 * @tree: the io tree to search
906 * @start: the start offset in bytes
907 * @end: the end offset in bytes (inclusive)
908 * @bits: the bits to set in this range
909 * @clear_bits: the bits to clear in this range
910 * @mask: the allocation mask
912 * This will go through and set bits for the given range. If any states exist
913 * already in this range they are set with the given bit and cleared of the
914 * clear_bits. This is only meant to be used by things that are mergeable, ie
915 * converting from say DELALLOC to DIRTY. This is not meant to be used with
916 * boundary bits like LOCK.
918 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
919 int bits, int clear_bits, gfp_t mask)
921 struct extent_state *state;
922 struct extent_state *prealloc = NULL;
923 struct rb_node *node;
929 if (!prealloc && (mask & __GFP_WAIT)) {
930 prealloc = alloc_extent_state(mask);
935 spin_lock(&tree->lock);
937 * this search will find all the extents that end after
940 node = tree_search(tree, start);
942 prealloc = alloc_extent_state_atomic(prealloc);
947 err = insert_state(tree, prealloc, start, end, &bits);
949 BUG_ON(err == -EEXIST);
952 state = rb_entry(node, struct extent_state, rb_node);
954 last_start = state->start;
955 last_end = state->end;
958 * | ---- desired range ---- |
961 * Just lock what we found and keep going
963 if (state->start == start && state->end <= end) {
964 struct rb_node *next_node;
966 set_state_bits(tree, state, &bits);
967 clear_state_bit(tree, state, &clear_bits, 0);
968 if (last_end == (u64)-1)
971 start = last_end + 1;
972 next_node = rb_next(&state->rb_node);
973 if (next_node && start < end && prealloc && !need_resched()) {
974 state = rb_entry(next_node, struct extent_state,
976 if (state->start == start)
983 * | ---- desired range ---- |
986 * | ------------- state -------------- |
988 * We need to split the extent we found, and may flip bits on
991 * If the extent we found extends past our
992 * range, we just split and search again. It'll get split
993 * again the next time though.
995 * If the extent we found is inside our range, we set the
998 if (state->start < start) {
999 prealloc = alloc_extent_state_atomic(prealloc);
1004 err = split_state(tree, state, prealloc, start);
1005 BUG_ON(err == -EEXIST);
1009 if (state->end <= end) {
1010 set_state_bits(tree, state, &bits);
1011 clear_state_bit(tree, state, &clear_bits, 0);
1012 if (last_end == (u64)-1)
1014 start = last_end + 1;
1019 * | ---- desired range ---- |
1020 * | state | or | state |
1022 * There's a hole, we need to insert something in it and
1023 * ignore the extent we found.
1025 if (state->start > start) {
1027 if (end < last_start)
1030 this_end = last_start - 1;
1032 prealloc = alloc_extent_state_atomic(prealloc);
1039 * Avoid to free 'prealloc' if it can be merged with
1042 err = insert_state(tree, prealloc, start, this_end,
1044 BUG_ON(err == -EEXIST);
1046 free_extent_state(prealloc);
1051 start = this_end + 1;
1055 * | ---- desired range ---- |
1057 * We need to split the extent, and set the bit
1060 if (state->start <= end && state->end > end) {
1061 prealloc = alloc_extent_state_atomic(prealloc);
1067 err = split_state(tree, state, prealloc, end + 1);
1068 BUG_ON(err == -EEXIST);
1070 set_state_bits(tree, prealloc, &bits);
1071 clear_state_bit(tree, prealloc, &clear_bits, 0);
1079 spin_unlock(&tree->lock);
1081 free_extent_state(prealloc);
1088 spin_unlock(&tree->lock);
1089 if (mask & __GFP_WAIT)
1094 /* wrappers around set/clear extent bit */
1095 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1098 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
1102 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1103 int bits, gfp_t mask)
1105 return set_extent_bit(tree, start, end, bits, 0, NULL,
1109 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1110 int bits, gfp_t mask)
1112 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
1115 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
1116 struct extent_state **cached_state, gfp_t mask)
1118 return set_extent_bit(tree, start, end,
1119 EXTENT_DELALLOC | EXTENT_UPTODATE,
1120 0, NULL, cached_state, mask);
1123 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1126 return clear_extent_bit(tree, start, end,
1127 EXTENT_DIRTY | EXTENT_DELALLOC |
1128 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
1131 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1134 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
1138 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1139 struct extent_state **cached_state, gfp_t mask)
1141 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1142 NULL, cached_state, mask);
1145 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
1146 u64 end, struct extent_state **cached_state,
1149 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1150 cached_state, mask);
1154 * either insert or lock state struct between start and end use mask to tell
1155 * us if waiting is desired.
1157 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1158 int bits, struct extent_state **cached_state, gfp_t mask)
1163 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1164 EXTENT_LOCKED, &failed_start,
1165 cached_state, mask);
1166 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1167 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1168 start = failed_start;
1172 WARN_ON(start > end);
1177 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1179 return lock_extent_bits(tree, start, end, 0, NULL, mask);
1182 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1188 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1189 &failed_start, NULL, mask);
1190 if (err == -EEXIST) {
1191 if (failed_start > start)
1192 clear_extent_bit(tree, start, failed_start - 1,
1193 EXTENT_LOCKED, 1, 0, NULL, mask);
1199 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1200 struct extent_state **cached, gfp_t mask)
1202 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1206 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1208 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1213 * helper function to set both pages and extents in the tree writeback
1215 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1217 unsigned long index = start >> PAGE_CACHE_SHIFT;
1218 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1221 while (index <= end_index) {
1222 page = find_get_page(tree->mapping, index);
1224 set_page_writeback(page);
1225 page_cache_release(page);
1231 /* find the first state struct with 'bits' set after 'start', and
1232 * return it. tree->lock must be held. NULL will returned if
1233 * nothing was found after 'start'
1235 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1236 u64 start, int bits)
1238 struct rb_node *node;
1239 struct extent_state *state;
1242 * this search will find all the extents that end after
1245 node = tree_search(tree, start);
1250 state = rb_entry(node, struct extent_state, rb_node);
1251 if (state->end >= start && (state->state & bits))
1254 node = rb_next(node);
1263 * find the first offset in the io tree with 'bits' set. zero is
1264 * returned if we find something, and *start_ret and *end_ret are
1265 * set to reflect the state struct that was found.
1267 * If nothing was found, 1 is returned, < 0 on error
1269 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1270 u64 *start_ret, u64 *end_ret, int bits)
1272 struct extent_state *state;
1275 spin_lock(&tree->lock);
1276 state = find_first_extent_bit_state(tree, start, bits);
1278 *start_ret = state->start;
1279 *end_ret = state->end;
1282 spin_unlock(&tree->lock);
1287 * find a contiguous range of bytes in the file marked as delalloc, not
1288 * more than 'max_bytes'. start and end are used to return the range,
1290 * 1 is returned if we find something, 0 if nothing was in the tree
1292 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1293 u64 *start, u64 *end, u64 max_bytes,
1294 struct extent_state **cached_state)
1296 struct rb_node *node;
1297 struct extent_state *state;
1298 u64 cur_start = *start;
1300 u64 total_bytes = 0;
1302 spin_lock(&tree->lock);
1305 * this search will find all the extents that end after
1308 node = tree_search(tree, cur_start);
1316 state = rb_entry(node, struct extent_state, rb_node);
1317 if (found && (state->start != cur_start ||
1318 (state->state & EXTENT_BOUNDARY))) {
1321 if (!(state->state & EXTENT_DELALLOC)) {
1327 *start = state->start;
1328 *cached_state = state;
1329 atomic_inc(&state->refs);
1333 cur_start = state->end + 1;
1334 node = rb_next(node);
1337 total_bytes += state->end - state->start + 1;
1338 if (total_bytes >= max_bytes)
1342 spin_unlock(&tree->lock);
1346 static noinline int __unlock_for_delalloc(struct inode *inode,
1347 struct page *locked_page,
1351 struct page *pages[16];
1352 unsigned long index = start >> PAGE_CACHE_SHIFT;
1353 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1354 unsigned long nr_pages = end_index - index + 1;
1357 if (index == locked_page->index && end_index == index)
1360 while (nr_pages > 0) {
1361 ret = find_get_pages_contig(inode->i_mapping, index,
1362 min_t(unsigned long, nr_pages,
1363 ARRAY_SIZE(pages)), pages);
1364 for (i = 0; i < ret; i++) {
1365 if (pages[i] != locked_page)
1366 unlock_page(pages[i]);
1367 page_cache_release(pages[i]);
1376 static noinline int lock_delalloc_pages(struct inode *inode,
1377 struct page *locked_page,
1381 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1382 unsigned long start_index = index;
1383 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1384 unsigned long pages_locked = 0;
1385 struct page *pages[16];
1386 unsigned long nrpages;
1390 /* the caller is responsible for locking the start index */
1391 if (index == locked_page->index && index == end_index)
1394 /* skip the page at the start index */
1395 nrpages = end_index - index + 1;
1396 while (nrpages > 0) {
1397 ret = find_get_pages_contig(inode->i_mapping, index,
1398 min_t(unsigned long,
1399 nrpages, ARRAY_SIZE(pages)), pages);
1404 /* now we have an array of pages, lock them all */
1405 for (i = 0; i < ret; i++) {
1407 * the caller is taking responsibility for
1410 if (pages[i] != locked_page) {
1411 lock_page(pages[i]);
1412 if (!PageDirty(pages[i]) ||
1413 pages[i]->mapping != inode->i_mapping) {
1415 unlock_page(pages[i]);
1416 page_cache_release(pages[i]);
1420 page_cache_release(pages[i]);
1429 if (ret && pages_locked) {
1430 __unlock_for_delalloc(inode, locked_page,
1432 ((u64)(start_index + pages_locked - 1)) <<
1439 * find a contiguous range of bytes in the file marked as delalloc, not
1440 * more than 'max_bytes'. start and end are used to return the range,
1442 * 1 is returned if we find something, 0 if nothing was in the tree
1444 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1445 struct extent_io_tree *tree,
1446 struct page *locked_page,
1447 u64 *start, u64 *end,
1453 struct extent_state *cached_state = NULL;
1458 /* step one, find a bunch of delalloc bytes starting at start */
1459 delalloc_start = *start;
1461 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1462 max_bytes, &cached_state);
1463 if (!found || delalloc_end <= *start) {
1464 *start = delalloc_start;
1465 *end = delalloc_end;
1466 free_extent_state(cached_state);
1471 * start comes from the offset of locked_page. We have to lock
1472 * pages in order, so we can't process delalloc bytes before
1475 if (delalloc_start < *start)
1476 delalloc_start = *start;
1479 * make sure to limit the number of pages we try to lock down
1482 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1483 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1485 /* step two, lock all the pages after the page that has start */
1486 ret = lock_delalloc_pages(inode, locked_page,
1487 delalloc_start, delalloc_end);
1488 if (ret == -EAGAIN) {
1489 /* some of the pages are gone, lets avoid looping by
1490 * shortening the size of the delalloc range we're searching
1492 free_extent_state(cached_state);
1494 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1495 max_bytes = PAGE_CACHE_SIZE - offset;
1505 /* step three, lock the state bits for the whole range */
1506 lock_extent_bits(tree, delalloc_start, delalloc_end,
1507 0, &cached_state, GFP_NOFS);
1509 /* then test to make sure it is all still delalloc */
1510 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1511 EXTENT_DELALLOC, 1, cached_state);
1513 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1514 &cached_state, GFP_NOFS);
1515 __unlock_for_delalloc(inode, locked_page,
1516 delalloc_start, delalloc_end);
1520 free_extent_state(cached_state);
1521 *start = delalloc_start;
1522 *end = delalloc_end;
1527 int extent_clear_unlock_delalloc(struct inode *inode,
1528 struct extent_io_tree *tree,
1529 u64 start, u64 end, struct page *locked_page,
1533 struct page *pages[16];
1534 unsigned long index = start >> PAGE_CACHE_SHIFT;
1535 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1536 unsigned long nr_pages = end_index - index + 1;
1540 if (op & EXTENT_CLEAR_UNLOCK)
1541 clear_bits |= EXTENT_LOCKED;
1542 if (op & EXTENT_CLEAR_DIRTY)
1543 clear_bits |= EXTENT_DIRTY;
1545 if (op & EXTENT_CLEAR_DELALLOC)
1546 clear_bits |= EXTENT_DELALLOC;
1548 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1549 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1550 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1551 EXTENT_SET_PRIVATE2)))
1554 while (nr_pages > 0) {
1555 ret = find_get_pages_contig(inode->i_mapping, index,
1556 min_t(unsigned long,
1557 nr_pages, ARRAY_SIZE(pages)), pages);
1558 for (i = 0; i < ret; i++) {
1560 if (op & EXTENT_SET_PRIVATE2)
1561 SetPagePrivate2(pages[i]);
1563 if (pages[i] == locked_page) {
1564 page_cache_release(pages[i]);
1567 if (op & EXTENT_CLEAR_DIRTY)
1568 clear_page_dirty_for_io(pages[i]);
1569 if (op & EXTENT_SET_WRITEBACK)
1570 set_page_writeback(pages[i]);
1571 if (op & EXTENT_END_WRITEBACK)
1572 end_page_writeback(pages[i]);
1573 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1574 unlock_page(pages[i]);
1575 page_cache_release(pages[i]);
1585 * count the number of bytes in the tree that have a given bit(s)
1586 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1587 * cached. The total number found is returned.
1589 u64 count_range_bits(struct extent_io_tree *tree,
1590 u64 *start, u64 search_end, u64 max_bytes,
1591 unsigned long bits, int contig)
1593 struct rb_node *node;
1594 struct extent_state *state;
1595 u64 cur_start = *start;
1596 u64 total_bytes = 0;
1600 if (search_end <= cur_start) {
1605 spin_lock(&tree->lock);
1606 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1607 total_bytes = tree->dirty_bytes;
1611 * this search will find all the extents that end after
1614 node = tree_search(tree, cur_start);
1619 state = rb_entry(node, struct extent_state, rb_node);
1620 if (state->start > search_end)
1622 if (contig && found && state->start > last + 1)
1624 if (state->end >= cur_start && (state->state & bits) == bits) {
1625 total_bytes += min(search_end, state->end) + 1 -
1626 max(cur_start, state->start);
1627 if (total_bytes >= max_bytes)
1630 *start = max(cur_start, state->start);
1634 } else if (contig && found) {
1637 node = rb_next(node);
1642 spin_unlock(&tree->lock);
1647 * set the private field for a given byte offset in the tree. If there isn't
1648 * an extent_state there already, this does nothing.
1650 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1652 struct rb_node *node;
1653 struct extent_state *state;
1656 spin_lock(&tree->lock);
1658 * this search will find all the extents that end after
1661 node = tree_search(tree, start);
1666 state = rb_entry(node, struct extent_state, rb_node);
1667 if (state->start != start) {
1671 state->private = private;
1673 spin_unlock(&tree->lock);
1677 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1679 struct rb_node *node;
1680 struct extent_state *state;
1683 spin_lock(&tree->lock);
1685 * this search will find all the extents that end after
1688 node = tree_search(tree, start);
1693 state = rb_entry(node, struct extent_state, rb_node);
1694 if (state->start != start) {
1698 *private = state->private;
1700 spin_unlock(&tree->lock);
1705 * searches a range in the state tree for a given mask.
1706 * If 'filled' == 1, this returns 1 only if every extent in the tree
1707 * has the bits set. Otherwise, 1 is returned if any bit in the
1708 * range is found set.
1710 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1711 int bits, int filled, struct extent_state *cached)
1713 struct extent_state *state = NULL;
1714 struct rb_node *node;
1717 spin_lock(&tree->lock);
1718 if (cached && cached->tree && cached->start <= start &&
1719 cached->end > start)
1720 node = &cached->rb_node;
1722 node = tree_search(tree, start);
1723 while (node && start <= end) {
1724 state = rb_entry(node, struct extent_state, rb_node);
1726 if (filled && state->start > start) {
1731 if (state->start > end)
1734 if (state->state & bits) {
1738 } else if (filled) {
1743 if (state->end == (u64)-1)
1746 start = state->end + 1;
1749 node = rb_next(node);
1756 spin_unlock(&tree->lock);
1761 * helper function to set a given page up to date if all the
1762 * extents in the tree for that page are up to date
1764 static int check_page_uptodate(struct extent_io_tree *tree,
1767 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1768 u64 end = start + PAGE_CACHE_SIZE - 1;
1769 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1770 SetPageUptodate(page);
1775 * helper function to unlock a page if all the extents in the tree
1776 * for that page are unlocked
1778 static int check_page_locked(struct extent_io_tree *tree,
1781 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1782 u64 end = start + PAGE_CACHE_SIZE - 1;
1783 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1789 * helper function to end page writeback if all the extents
1790 * in the tree for that page are done with writeback
1792 static int check_page_writeback(struct extent_io_tree *tree,
1795 end_page_writeback(page);
1800 * When IO fails, either with EIO or csum verification fails, we
1801 * try other mirrors that might have a good copy of the data. This
1802 * io_failure_record is used to record state as we go through all the
1803 * mirrors. If another mirror has good data, the page is set up to date
1804 * and things continue. If a good mirror can't be found, the original
1805 * bio end_io callback is called to indicate things have failed.
1807 struct io_failure_record {
1812 unsigned long bio_flags;
1818 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1823 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1825 set_state_private(failure_tree, rec->start, 0);
1826 ret = clear_extent_bits(failure_tree, rec->start,
1827 rec->start + rec->len - 1,
1828 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1833 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1834 rec->start + rec->len - 1,
1835 EXTENT_DAMAGED, GFP_NOFS);
1844 static void repair_io_failure_callback(struct bio *bio, int err)
1846 complete(bio->bi_private);
1850 * this bypasses the standard btrfs submit functions deliberately, as
1851 * the standard behavior is to write all copies in a raid setup. here we only
1852 * want to write the one bad copy. so we do the mapping for ourselves and issue
1853 * submit_bio directly.
1854 * to avoid any synchonization issues, wait for the data after writing, which
1855 * actually prevents the read that triggered the error from finishing.
1856 * currently, there can be no more than two copies of every data bit. thus,
1857 * exactly one rewrite is required.
1859 int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1860 u64 length, u64 logical, struct page *page,
1864 struct btrfs_device *dev;
1865 DECLARE_COMPLETION_ONSTACK(compl);
1868 struct btrfs_bio *bbio = NULL;
1871 BUG_ON(!mirror_num);
1873 bio = bio_alloc(GFP_NOFS, 1);
1876 bio->bi_private = &compl;
1877 bio->bi_end_io = repair_io_failure_callback;
1879 map_length = length;
1881 ret = btrfs_map_block(map_tree, WRITE, logical,
1882 &map_length, &bbio, mirror_num);
1887 BUG_ON(mirror_num != bbio->mirror_num);
1888 sector = bbio->stripes[mirror_num-1].physical >> 9;
1889 bio->bi_sector = sector;
1890 dev = bbio->stripes[mirror_num-1].dev;
1892 if (!dev || !dev->bdev || !dev->writeable) {
1896 bio->bi_bdev = dev->bdev;
1897 bio_add_page(bio, page, length, start-page_offset(page));
1898 btrfsic_submit_bio(WRITE_SYNC, bio);
1899 wait_for_completion(&compl);
1901 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1902 /* try to remap that extent elsewhere? */
1907 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1908 "sector %llu)\n", page->mapping->host->i_ino, start,
1916 * each time an IO finishes, we do a fast check in the IO failure tree
1917 * to see if we need to process or clean up an io_failure_record
1919 static int clean_io_failure(u64 start, struct page *page)
1922 u64 private_failure;
1923 struct io_failure_record *failrec;
1924 struct btrfs_mapping_tree *map_tree;
1925 struct extent_state *state;
1929 struct inode *inode = page->mapping->host;
1932 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1933 (u64)-1, 1, EXTENT_DIRTY, 0);
1937 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1942 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1943 BUG_ON(!failrec->this_mirror);
1945 if (failrec->in_validation) {
1946 /* there was no real error, just free the record */
1947 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1953 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1954 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1957 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1959 if (state && state->start == failrec->start) {
1960 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1961 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1963 if (num_copies > 1) {
1964 ret = repair_io_failure(map_tree, start, failrec->len,
1965 failrec->logical, page,
1966 failrec->failed_mirror);
1973 ret = free_io_failure(inode, failrec, did_repair);
1979 * this is a generic handler for readpage errors (default
1980 * readpage_io_failed_hook). if other copies exist, read those and write back
1981 * good data to the failed position. does not investigate in remapping the
1982 * failed extent elsewhere, hoping the device will be smart enough to do this as
1986 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
1987 u64 start, u64 end, int failed_mirror,
1988 struct extent_state *state)
1990 struct io_failure_record *failrec = NULL;
1992 struct extent_map *em;
1993 struct inode *inode = page->mapping->host;
1994 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1995 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
1996 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2003 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2005 ret = get_state_private(failure_tree, start, &private);
2007 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2010 failrec->start = start;
2011 failrec->len = end - start + 1;
2012 failrec->this_mirror = 0;
2013 failrec->bio_flags = 0;
2014 failrec->in_validation = 0;
2016 read_lock(&em_tree->lock);
2017 em = lookup_extent_mapping(em_tree, start, failrec->len);
2019 read_unlock(&em_tree->lock);
2024 if (em->start > start || em->start + em->len < start) {
2025 free_extent_map(em);
2028 read_unlock(&em_tree->lock);
2030 if (!em || IS_ERR(em)) {
2034 logical = start - em->start;
2035 logical = em->block_start + logical;
2036 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2037 logical = em->block_start;
2038 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2039 extent_set_compress_type(&failrec->bio_flags,
2042 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2043 "len=%llu\n", logical, start, failrec->len);
2044 failrec->logical = logical;
2045 free_extent_map(em);
2047 /* set the bits in the private failure tree */
2048 ret = set_extent_bits(failure_tree, start, end,
2049 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2051 ret = set_state_private(failure_tree, start,
2052 (u64)(unsigned long)failrec);
2053 /* set the bits in the inode's tree */
2055 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2062 failrec = (struct io_failure_record *)(unsigned long)private;
2063 pr_debug("bio_readpage_error: (found) logical=%llu, "
2064 "start=%llu, len=%llu, validation=%d\n",
2065 failrec->logical, failrec->start, failrec->len,
2066 failrec->in_validation);
2068 * when data can be on disk more than twice, add to failrec here
2069 * (e.g. with a list for failed_mirror) to make
2070 * clean_io_failure() clean all those errors at once.
2073 num_copies = btrfs_num_copies(
2074 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2075 failrec->logical, failrec->len);
2076 if (num_copies == 1) {
2078 * we only have a single copy of the data, so don't bother with
2079 * all the retry and error correction code that follows. no
2080 * matter what the error is, it is very likely to persist.
2082 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2083 "state=%p, num_copies=%d, next_mirror %d, "
2084 "failed_mirror %d\n", state, num_copies,
2085 failrec->this_mirror, failed_mirror);
2086 free_io_failure(inode, failrec, 0);
2091 spin_lock(&tree->lock);
2092 state = find_first_extent_bit_state(tree, failrec->start,
2094 if (state && state->start != failrec->start)
2096 spin_unlock(&tree->lock);
2100 * there are two premises:
2101 * a) deliver good data to the caller
2102 * b) correct the bad sectors on disk
2104 if (failed_bio->bi_vcnt > 1) {
2106 * to fulfill b), we need to know the exact failing sectors, as
2107 * we don't want to rewrite any more than the failed ones. thus,
2108 * we need separate read requests for the failed bio
2110 * if the following BUG_ON triggers, our validation request got
2111 * merged. we need separate requests for our algorithm to work.
2113 BUG_ON(failrec->in_validation);
2114 failrec->in_validation = 1;
2115 failrec->this_mirror = failed_mirror;
2116 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2119 * we're ready to fulfill a) and b) alongside. get a good copy
2120 * of the failed sector and if we succeed, we have setup
2121 * everything for repair_io_failure to do the rest for us.
2123 if (failrec->in_validation) {
2124 BUG_ON(failrec->this_mirror != failed_mirror);
2125 failrec->in_validation = 0;
2126 failrec->this_mirror = 0;
2128 failrec->failed_mirror = failed_mirror;
2129 failrec->this_mirror++;
2130 if (failrec->this_mirror == failed_mirror)
2131 failrec->this_mirror++;
2132 read_mode = READ_SYNC;
2135 if (!state || failrec->this_mirror > num_copies) {
2136 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2137 "next_mirror %d, failed_mirror %d\n", state,
2138 num_copies, failrec->this_mirror, failed_mirror);
2139 free_io_failure(inode, failrec, 0);
2143 bio = bio_alloc(GFP_NOFS, 1);
2144 bio->bi_private = state;
2145 bio->bi_end_io = failed_bio->bi_end_io;
2146 bio->bi_sector = failrec->logical >> 9;
2147 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2150 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2152 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2153 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2154 failrec->this_mirror, num_copies, failrec->in_validation);
2156 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2157 failrec->this_mirror,
2158 failrec->bio_flags, 0);
2162 /* lots and lots of room for performance fixes in the end_bio funcs */
2164 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2166 int uptodate = (err == 0);
2167 struct extent_io_tree *tree;
2170 tree = &BTRFS_I(page->mapping->host)->io_tree;
2172 if (tree->ops && tree->ops->writepage_end_io_hook) {
2173 ret = tree->ops->writepage_end_io_hook(page, start,
2174 end, NULL, uptodate);
2179 if (!uptodate && tree->ops &&
2180 tree->ops->writepage_io_failed_hook) {
2181 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2183 /* Writeback already completed */
2189 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2190 ClearPageUptodate(page);
2197 * after a writepage IO is done, we need to:
2198 * clear the uptodate bits on error
2199 * clear the writeback bits in the extent tree for this IO
2200 * end_page_writeback if the page has no more pending IO
2202 * Scheduling is not allowed, so the extent state tree is expected
2203 * to have one and only one object corresponding to this IO.
2205 static void end_bio_extent_writepage(struct bio *bio, int err)
2207 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2208 struct extent_io_tree *tree;
2214 struct page *page = bvec->bv_page;
2215 tree = &BTRFS_I(page->mapping->host)->io_tree;
2217 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2219 end = start + bvec->bv_len - 1;
2221 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2226 if (--bvec >= bio->bi_io_vec)
2227 prefetchw(&bvec->bv_page->flags);
2229 if (end_extent_writepage(page, err, start, end))
2233 end_page_writeback(page);
2235 check_page_writeback(tree, page);
2236 } while (bvec >= bio->bi_io_vec);
2242 * after a readpage IO is done, we need to:
2243 * clear the uptodate bits on error
2244 * set the uptodate bits if things worked
2245 * set the page up to date if all extents in the tree are uptodate
2246 * clear the lock bit in the extent tree
2247 * unlock the page if there are no other extents locked for it
2249 * Scheduling is not allowed, so the extent state tree is expected
2250 * to have one and only one object corresponding to this IO.
2252 static void end_bio_extent_readpage(struct bio *bio, int err)
2254 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2255 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2256 struct bio_vec *bvec = bio->bi_io_vec;
2257 struct extent_io_tree *tree;
2267 struct page *page = bvec->bv_page;
2268 struct extent_state *cached = NULL;
2269 struct extent_state *state;
2271 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2272 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2273 (long int)bio->bi_bdev);
2274 tree = &BTRFS_I(page->mapping->host)->io_tree;
2276 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2278 end = start + bvec->bv_len - 1;
2280 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2285 if (++bvec <= bvec_end)
2286 prefetchw(&bvec->bv_page->flags);
2288 spin_lock(&tree->lock);
2289 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2290 if (state && state->start == start) {
2292 * take a reference on the state, unlock will drop
2295 cache_state(state, &cached);
2297 spin_unlock(&tree->lock);
2299 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2300 ret = tree->ops->readpage_end_io_hook(page, start, end,
2305 clean_io_failure(start, page);
2309 failed_mirror = (int)(unsigned long)bio->bi_bdev;
2311 * The generic bio_readpage_error handles errors the
2312 * following way: If possible, new read requests are
2313 * created and submitted and will end up in
2314 * end_bio_extent_readpage as well (if we're lucky, not
2315 * in the !uptodate case). In that case it returns 0 and
2316 * we just go on with the next page in our bio. If it
2317 * can't handle the error it will return -EIO and we
2318 * remain responsible for that page.
2320 ret = bio_readpage_error(bio, page, start, end,
2321 failed_mirror, NULL);
2325 test_bit(BIO_UPTODATE, &bio->bi_flags);
2328 uncache_state(&cached);
2331 if (tree->ops && tree->ops->readpage_io_failed_hook) {
2332 ret = tree->ops->readpage_io_failed_hook(
2333 bio, page, start, end,
2334 failed_mirror, state);
2341 set_extent_uptodate(tree, start, end, &cached,
2344 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2348 SetPageUptodate(page);
2350 ClearPageUptodate(page);
2356 check_page_uptodate(tree, page);
2358 ClearPageUptodate(page);
2361 check_page_locked(tree, page);
2363 } while (bvec <= bvec_end);
2369 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2374 bio = bio_alloc(gfp_flags, nr_vecs);
2376 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2377 while (!bio && (nr_vecs /= 2))
2378 bio = bio_alloc(gfp_flags, nr_vecs);
2383 bio->bi_bdev = bdev;
2384 bio->bi_sector = first_sector;
2389 static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
2390 unsigned long bio_flags)
2393 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2394 struct page *page = bvec->bv_page;
2395 struct extent_io_tree *tree = bio->bi_private;
2398 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
2400 bio->bi_private = NULL;
2404 if (tree->ops && tree->ops->submit_bio_hook)
2405 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2406 mirror_num, bio_flags, start);
2408 btrfsic_submit_bio(rw, bio);
2410 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2416 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2417 struct page *page, sector_t sector,
2418 size_t size, unsigned long offset,
2419 struct block_device *bdev,
2420 struct bio **bio_ret,
2421 unsigned long max_pages,
2422 bio_end_io_t end_io_func,
2424 unsigned long prev_bio_flags,
2425 unsigned long bio_flags)
2431 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2432 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2433 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2435 if (bio_ret && *bio_ret) {
2438 contig = bio->bi_sector == sector;
2440 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2443 if (prev_bio_flags != bio_flags || !contig ||
2444 (tree->ops && tree->ops->merge_bio_hook &&
2445 tree->ops->merge_bio_hook(page, offset, page_size, bio,
2447 bio_add_page(bio, page, page_size, offset) < page_size) {
2448 ret = submit_one_bio(rw, bio, mirror_num,
2455 if (this_compressed)
2458 nr = bio_get_nr_vecs(bdev);
2460 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2464 bio_add_page(bio, page, page_size, offset);
2465 bio->bi_end_io = end_io_func;
2466 bio->bi_private = tree;
2471 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2476 void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2478 if (!PagePrivate(page)) {
2479 SetPagePrivate(page);
2480 page_cache_get(page);
2481 set_page_private(page, (unsigned long)eb);
2483 WARN_ON(page->private != (unsigned long)eb);
2487 void set_page_extent_mapped(struct page *page)
2489 if (!PagePrivate(page)) {
2490 SetPagePrivate(page);
2491 page_cache_get(page);
2492 set_page_private(page, EXTENT_PAGE_PRIVATE);
2497 * basic readpage implementation. Locked extent state structs are inserted
2498 * into the tree that are removed when the IO is done (by the end_io
2501 static int __extent_read_full_page(struct extent_io_tree *tree,
2503 get_extent_t *get_extent,
2504 struct bio **bio, int mirror_num,
2505 unsigned long *bio_flags)
2507 struct inode *inode = page->mapping->host;
2508 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2509 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2513 u64 last_byte = i_size_read(inode);
2517 struct extent_map *em;
2518 struct block_device *bdev;
2519 struct btrfs_ordered_extent *ordered;
2522 size_t pg_offset = 0;
2524 size_t disk_io_size;
2525 size_t blocksize = inode->i_sb->s_blocksize;
2526 unsigned long this_bio_flag = 0;
2528 set_page_extent_mapped(page);
2530 if (!PageUptodate(page)) {
2531 if (cleancache_get_page(page) == 0) {
2532 BUG_ON(blocksize != PAGE_SIZE);
2539 lock_extent(tree, start, end, GFP_NOFS);
2540 ordered = btrfs_lookup_ordered_extent(inode, start);
2543 unlock_extent(tree, start, end, GFP_NOFS);
2544 btrfs_start_ordered_extent(inode, ordered, 1);
2545 btrfs_put_ordered_extent(ordered);
2548 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2550 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2553 iosize = PAGE_CACHE_SIZE - zero_offset;
2554 userpage = kmap_atomic(page, KM_USER0);
2555 memset(userpage + zero_offset, 0, iosize);
2556 flush_dcache_page(page);
2557 kunmap_atomic(userpage, KM_USER0);
2560 while (cur <= end) {
2561 if (cur >= last_byte) {
2563 struct extent_state *cached = NULL;
2565 iosize = PAGE_CACHE_SIZE - pg_offset;
2566 userpage = kmap_atomic(page, KM_USER0);
2567 memset(userpage + pg_offset, 0, iosize);
2568 flush_dcache_page(page);
2569 kunmap_atomic(userpage, KM_USER0);
2570 set_extent_uptodate(tree, cur, cur + iosize - 1,
2572 unlock_extent_cached(tree, cur, cur + iosize - 1,
2576 em = get_extent(inode, page, pg_offset, cur,
2578 if (IS_ERR_OR_NULL(em)) {
2580 unlock_extent(tree, cur, end, GFP_NOFS);
2583 extent_offset = cur - em->start;
2584 BUG_ON(extent_map_end(em) <= cur);
2587 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2588 this_bio_flag = EXTENT_BIO_COMPRESSED;
2589 extent_set_compress_type(&this_bio_flag,
2593 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2594 cur_end = min(extent_map_end(em) - 1, end);
2595 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2596 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2597 disk_io_size = em->block_len;
2598 sector = em->block_start >> 9;
2600 sector = (em->block_start + extent_offset) >> 9;
2601 disk_io_size = iosize;
2604 block_start = em->block_start;
2605 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2606 block_start = EXTENT_MAP_HOLE;
2607 free_extent_map(em);
2610 /* we've found a hole, just zero and go on */
2611 if (block_start == EXTENT_MAP_HOLE) {
2613 struct extent_state *cached = NULL;
2615 userpage = kmap_atomic(page, KM_USER0);
2616 memset(userpage + pg_offset, 0, iosize);
2617 flush_dcache_page(page);
2618 kunmap_atomic(userpage, KM_USER0);
2620 set_extent_uptodate(tree, cur, cur + iosize - 1,
2622 unlock_extent_cached(tree, cur, cur + iosize - 1,
2625 pg_offset += iosize;
2628 /* the get_extent function already copied into the page */
2629 if (test_range_bit(tree, cur, cur_end,
2630 EXTENT_UPTODATE, 1, NULL)) {
2631 check_page_uptodate(tree, page);
2632 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2634 pg_offset += iosize;
2637 /* we have an inline extent but it didn't get marked up
2638 * to date. Error out
2640 if (block_start == EXTENT_MAP_INLINE) {
2642 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2644 pg_offset += iosize;
2649 if (tree->ops && tree->ops->readpage_io_hook) {
2650 ret = tree->ops->readpage_io_hook(page, cur,
2654 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2656 ret = submit_extent_page(READ, tree, page,
2657 sector, disk_io_size, pg_offset,
2659 end_bio_extent_readpage, mirror_num,
2663 *bio_flags = this_bio_flag;
2668 pg_offset += iosize;
2672 if (!PageError(page))
2673 SetPageUptodate(page);
2679 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2680 get_extent_t *get_extent, int mirror_num)
2682 struct bio *bio = NULL;
2683 unsigned long bio_flags = 0;
2686 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2689 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2693 static noinline void update_nr_written(struct page *page,
2694 struct writeback_control *wbc,
2695 unsigned long nr_written)
2697 wbc->nr_to_write -= nr_written;
2698 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2699 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2700 page->mapping->writeback_index = page->index + nr_written;
2704 * the writepage semantics are similar to regular writepage. extent
2705 * records are inserted to lock ranges in the tree, and as dirty areas
2706 * are found, they are marked writeback. Then the lock bits are removed
2707 * and the end_io handler clears the writeback ranges
2709 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2712 struct inode *inode = page->mapping->host;
2713 struct extent_page_data *epd = data;
2714 struct extent_io_tree *tree = epd->tree;
2715 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2717 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2721 u64 last_byte = i_size_read(inode);
2725 struct extent_state *cached_state = NULL;
2726 struct extent_map *em;
2727 struct block_device *bdev;
2730 size_t pg_offset = 0;
2732 loff_t i_size = i_size_read(inode);
2733 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2739 unsigned long nr_written = 0;
2740 bool fill_delalloc = true;
2742 if (wbc->sync_mode == WB_SYNC_ALL)
2743 write_flags = WRITE_SYNC;
2745 write_flags = WRITE;
2747 trace___extent_writepage(page, inode, wbc);
2749 WARN_ON(!PageLocked(page));
2751 ClearPageError(page);
2753 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2754 if (page->index > end_index ||
2755 (page->index == end_index && !pg_offset)) {
2756 page->mapping->a_ops->invalidatepage(page, 0);
2761 if (page->index == end_index) {
2764 userpage = kmap_atomic(page, KM_USER0);
2765 memset(userpage + pg_offset, 0,
2766 PAGE_CACHE_SIZE - pg_offset);
2767 kunmap_atomic(userpage, KM_USER0);
2768 flush_dcache_page(page);
2772 set_page_extent_mapped(page);
2774 if (!tree->ops || !tree->ops->fill_delalloc)
2775 fill_delalloc = false;
2777 delalloc_start = start;
2780 if (!epd->extent_locked && fill_delalloc) {
2781 u64 delalloc_to_write = 0;
2783 * make sure the wbc mapping index is at least updated
2786 update_nr_written(page, wbc, 0);
2788 while (delalloc_end < page_end) {
2789 nr_delalloc = find_lock_delalloc_range(inode, tree,
2794 if (nr_delalloc == 0) {
2795 delalloc_start = delalloc_end + 1;
2798 ret = tree->ops->fill_delalloc(inode, page,
2805 * delalloc_end is already one less than the total
2806 * length, so we don't subtract one from
2809 delalloc_to_write += (delalloc_end - delalloc_start +
2812 delalloc_start = delalloc_end + 1;
2814 if (wbc->nr_to_write < delalloc_to_write) {
2817 if (delalloc_to_write < thresh * 2)
2818 thresh = delalloc_to_write;
2819 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2823 /* did the fill delalloc function already unlock and start
2829 * we've unlocked the page, so we can't update
2830 * the mapping's writeback index, just update
2833 wbc->nr_to_write -= nr_written;
2837 if (tree->ops && tree->ops->writepage_start_hook) {
2838 ret = tree->ops->writepage_start_hook(page, start,
2841 /* Fixup worker will requeue */
2843 wbc->pages_skipped++;
2845 redirty_page_for_writepage(wbc, page);
2846 update_nr_written(page, wbc, nr_written);
2854 * we don't want to touch the inode after unlocking the page,
2855 * so we update the mapping writeback index now
2857 update_nr_written(page, wbc, nr_written + 1);
2860 if (last_byte <= start) {
2861 if (tree->ops && tree->ops->writepage_end_io_hook)
2862 tree->ops->writepage_end_io_hook(page, start,
2867 blocksize = inode->i_sb->s_blocksize;
2869 while (cur <= end) {
2870 if (cur >= last_byte) {
2871 if (tree->ops && tree->ops->writepage_end_io_hook)
2872 tree->ops->writepage_end_io_hook(page, cur,
2876 em = epd->get_extent(inode, page, pg_offset, cur,
2878 if (IS_ERR_OR_NULL(em)) {
2883 extent_offset = cur - em->start;
2884 BUG_ON(extent_map_end(em) <= cur);
2886 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2887 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2888 sector = (em->block_start + extent_offset) >> 9;
2890 block_start = em->block_start;
2891 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2892 free_extent_map(em);
2896 * compressed and inline extents are written through other
2899 if (compressed || block_start == EXTENT_MAP_HOLE ||
2900 block_start == EXTENT_MAP_INLINE) {
2902 * end_io notification does not happen here for
2903 * compressed extents
2905 if (!compressed && tree->ops &&
2906 tree->ops->writepage_end_io_hook)
2907 tree->ops->writepage_end_io_hook(page, cur,
2910 else if (compressed) {
2911 /* we don't want to end_page_writeback on
2912 * a compressed extent. this happens
2919 pg_offset += iosize;
2922 /* leave this out until we have a page_mkwrite call */
2923 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2924 EXTENT_DIRTY, 0, NULL)) {
2926 pg_offset += iosize;
2930 if (tree->ops && tree->ops->writepage_io_hook) {
2931 ret = tree->ops->writepage_io_hook(page, cur,
2939 unsigned long max_nr = end_index + 1;
2941 set_range_writeback(tree, cur, cur + iosize - 1);
2942 if (!PageWriteback(page)) {
2943 printk(KERN_ERR "btrfs warning page %lu not "
2944 "writeback, cur %llu end %llu\n",
2945 page->index, (unsigned long long)cur,
2946 (unsigned long long)end);
2949 ret = submit_extent_page(write_flags, tree, page,
2950 sector, iosize, pg_offset,
2951 bdev, &epd->bio, max_nr,
2952 end_bio_extent_writepage,
2958 pg_offset += iosize;
2963 /* make sure the mapping tag for page dirty gets cleared */
2964 set_page_writeback(page);
2965 end_page_writeback(page);
2971 /* drop our reference on any cached states */
2972 free_extent_state(cached_state);
2977 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2978 * @mapping: address space structure to write
2979 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2980 * @writepage: function called for each page
2981 * @data: data passed to writepage function
2983 * If a page is already under I/O, write_cache_pages() skips it, even
2984 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2985 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2986 * and msync() need to guarantee that all the data which was dirty at the time
2987 * the call was made get new I/O started against them. If wbc->sync_mode is
2988 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2989 * existing IO to complete.
2991 static int extent_write_cache_pages(struct extent_io_tree *tree,
2992 struct address_space *mapping,
2993 struct writeback_control *wbc,
2994 writepage_t writepage, void *data,
2995 void (*flush_fn)(void *))
2999 int nr_to_write_done = 0;
3000 struct pagevec pvec;
3003 pgoff_t end; /* Inclusive */
3007 pagevec_init(&pvec, 0);
3008 if (wbc->range_cyclic) {
3009 index = mapping->writeback_index; /* Start from prev offset */
3012 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3013 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3016 if (wbc->sync_mode == WB_SYNC_ALL)
3017 tag = PAGECACHE_TAG_TOWRITE;
3019 tag = PAGECACHE_TAG_DIRTY;
3021 if (wbc->sync_mode == WB_SYNC_ALL)
3022 tag_pages_for_writeback(mapping, index, end);
3023 while (!done && !nr_to_write_done && (index <= end) &&
3024 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3025 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3029 for (i = 0; i < nr_pages; i++) {
3030 struct page *page = pvec.pages[i];
3033 * At this point we hold neither mapping->tree_lock nor
3034 * lock on the page itself: the page may be truncated or
3035 * invalidated (changing page->mapping to NULL), or even
3036 * swizzled back from swapper_space to tmpfs file
3040 tree->ops->write_cache_pages_lock_hook) {
3041 tree->ops->write_cache_pages_lock_hook(page,
3044 if (!trylock_page(page)) {
3050 if (unlikely(page->mapping != mapping)) {
3055 if (!wbc->range_cyclic && page->index > end) {
3061 if (wbc->sync_mode != WB_SYNC_NONE) {
3062 if (PageWriteback(page))
3064 wait_on_page_writeback(page);
3067 if (PageWriteback(page) ||
3068 !clear_page_dirty_for_io(page)) {
3073 ret = (*writepage)(page, wbc, data);
3075 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3083 * the filesystem may choose to bump up nr_to_write.
3084 * We have to make sure to honor the new nr_to_write
3087 nr_to_write_done = wbc->nr_to_write <= 0;
3089 pagevec_release(&pvec);
3092 if (!scanned && !done) {
3094 * We hit the last page and there is more work to be done: wrap
3095 * back to the start of the file
3104 static void flush_epd_write_bio(struct extent_page_data *epd)
3108 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
3110 submit_one_bio(WRITE, epd->bio, 0, 0);
3115 static noinline void flush_write_bio(void *data)
3117 struct extent_page_data *epd = data;
3118 flush_epd_write_bio(epd);
3121 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3122 get_extent_t *get_extent,
3123 struct writeback_control *wbc)
3126 struct extent_page_data epd = {
3129 .get_extent = get_extent,
3131 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3134 ret = __extent_writepage(page, wbc, &epd);
3136 flush_epd_write_bio(&epd);
3140 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3141 u64 start, u64 end, get_extent_t *get_extent,
3145 struct address_space *mapping = inode->i_mapping;
3147 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3150 struct extent_page_data epd = {
3153 .get_extent = get_extent,
3155 .sync_io = mode == WB_SYNC_ALL,
3157 struct writeback_control wbc_writepages = {
3159 .nr_to_write = nr_pages * 2,
3160 .range_start = start,
3161 .range_end = end + 1,
3164 while (start <= end) {
3165 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3166 if (clear_page_dirty_for_io(page))
3167 ret = __extent_writepage(page, &wbc_writepages, &epd);
3169 if (tree->ops && tree->ops->writepage_end_io_hook)
3170 tree->ops->writepage_end_io_hook(page, start,
3171 start + PAGE_CACHE_SIZE - 1,
3175 page_cache_release(page);
3176 start += PAGE_CACHE_SIZE;
3179 flush_epd_write_bio(&epd);
3183 int extent_writepages(struct extent_io_tree *tree,
3184 struct address_space *mapping,
3185 get_extent_t *get_extent,
3186 struct writeback_control *wbc)
3189 struct extent_page_data epd = {
3192 .get_extent = get_extent,
3194 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3197 ret = extent_write_cache_pages(tree, mapping, wbc,
3198 __extent_writepage, &epd,
3200 flush_epd_write_bio(&epd);
3204 int extent_readpages(struct extent_io_tree *tree,
3205 struct address_space *mapping,
3206 struct list_head *pages, unsigned nr_pages,
3207 get_extent_t get_extent)
3209 struct bio *bio = NULL;
3211 unsigned long bio_flags = 0;
3213 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3214 struct page *page = list_entry(pages->prev, struct page, lru);
3216 prefetchw(&page->flags);
3217 list_del(&page->lru);
3218 if (!add_to_page_cache_lru(page, mapping,
3219 page->index, GFP_NOFS)) {
3220 __extent_read_full_page(tree, page, get_extent,
3221 &bio, 0, &bio_flags);
3223 page_cache_release(page);
3225 BUG_ON(!list_empty(pages));
3227 submit_one_bio(READ, bio, 0, bio_flags);
3232 * basic invalidatepage code, this waits on any locked or writeback
3233 * ranges corresponding to the page, and then deletes any extent state
3234 * records from the tree
3236 int extent_invalidatepage(struct extent_io_tree *tree,
3237 struct page *page, unsigned long offset)
3239 struct extent_state *cached_state = NULL;
3240 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3241 u64 end = start + PAGE_CACHE_SIZE - 1;
3242 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3244 start += (offset + blocksize - 1) & ~(blocksize - 1);
3248 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
3249 wait_on_page_writeback(page);
3250 clear_extent_bit(tree, start, end,
3251 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3252 EXTENT_DO_ACCOUNTING,
3253 1, 1, &cached_state, GFP_NOFS);
3258 * a helper for releasepage, this tests for areas of the page that
3259 * are locked or under IO and drops the related state bits if it is safe
3262 int try_release_extent_state(struct extent_map_tree *map,
3263 struct extent_io_tree *tree, struct page *page,
3266 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3267 u64 end = start + PAGE_CACHE_SIZE - 1;
3270 if (test_range_bit(tree, start, end,
3271 EXTENT_IOBITS, 0, NULL))
3274 if ((mask & GFP_NOFS) == GFP_NOFS)
3277 * at this point we can safely clear everything except the
3278 * locked bit and the nodatasum bit
3280 ret = clear_extent_bit(tree, start, end,
3281 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3284 /* if clear_extent_bit failed for enomem reasons,
3285 * we can't allow the release to continue.
3296 * a helper for releasepage. As long as there are no locked extents
3297 * in the range corresponding to the page, both state records and extent
3298 * map records are removed
3300 int try_release_extent_mapping(struct extent_map_tree *map,
3301 struct extent_io_tree *tree, struct page *page,
3304 struct extent_map *em;
3305 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3306 u64 end = start + PAGE_CACHE_SIZE - 1;
3308 if ((mask & __GFP_WAIT) &&
3309 page->mapping->host->i_size > 16 * 1024 * 1024) {
3311 while (start <= end) {
3312 len = end - start + 1;
3313 write_lock(&map->lock);
3314 em = lookup_extent_mapping(map, start, len);
3316 write_unlock(&map->lock);
3319 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3320 em->start != start) {
3321 write_unlock(&map->lock);
3322 free_extent_map(em);
3325 if (!test_range_bit(tree, em->start,
3326 extent_map_end(em) - 1,
3327 EXTENT_LOCKED | EXTENT_WRITEBACK,
3329 remove_extent_mapping(map, em);
3330 /* once for the rb tree */
3331 free_extent_map(em);
3333 start = extent_map_end(em);
3334 write_unlock(&map->lock);
3337 free_extent_map(em);
3340 return try_release_extent_state(map, tree, page, mask);
3344 * helper function for fiemap, which doesn't want to see any holes.
3345 * This maps until we find something past 'last'
3347 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3350 get_extent_t *get_extent)
3352 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3353 struct extent_map *em;
3360 len = last - offset;
3363 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3364 em = get_extent(inode, NULL, 0, offset, len, 0);
3365 if (IS_ERR_OR_NULL(em))
3368 /* if this isn't a hole return it */
3369 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3370 em->block_start != EXTENT_MAP_HOLE) {
3374 /* this is a hole, advance to the next extent */
3375 offset = extent_map_end(em);
3376 free_extent_map(em);
3383 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3384 __u64 start, __u64 len, get_extent_t *get_extent)
3388 u64 max = start + len;
3392 u64 last_for_get_extent = 0;
3394 u64 isize = i_size_read(inode);
3395 struct btrfs_key found_key;
3396 struct extent_map *em = NULL;
3397 struct extent_state *cached_state = NULL;
3398 struct btrfs_path *path;
3399 struct btrfs_file_extent_item *item;
3404 unsigned long emflags;
3409 path = btrfs_alloc_path();
3412 path->leave_spinning = 1;
3414 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3415 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3418 * lookup the last file extent. We're not using i_size here
3419 * because there might be preallocation past i_size
3421 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3422 path, btrfs_ino(inode), -1, 0);
3424 btrfs_free_path(path);
3429 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3430 struct btrfs_file_extent_item);
3431 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3432 found_type = btrfs_key_type(&found_key);
3434 /* No extents, but there might be delalloc bits */
3435 if (found_key.objectid != btrfs_ino(inode) ||
3436 found_type != BTRFS_EXTENT_DATA_KEY) {
3437 /* have to trust i_size as the end */
3439 last_for_get_extent = isize;
3442 * remember the start of the last extent. There are a
3443 * bunch of different factors that go into the length of the
3444 * extent, so its much less complex to remember where it started
3446 last = found_key.offset;
3447 last_for_get_extent = last + 1;
3449 btrfs_free_path(path);
3452 * we might have some extents allocated but more delalloc past those
3453 * extents. so, we trust isize unless the start of the last extent is
3458 last_for_get_extent = isize;
3461 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3462 &cached_state, GFP_NOFS);
3464 em = get_extent_skip_holes(inode, start, last_for_get_extent,
3474 u64 offset_in_extent;
3476 /* break if the extent we found is outside the range */
3477 if (em->start >= max || extent_map_end(em) < off)
3481 * get_extent may return an extent that starts before our
3482 * requested range. We have to make sure the ranges
3483 * we return to fiemap always move forward and don't
3484 * overlap, so adjust the offsets here
3486 em_start = max(em->start, off);
3489 * record the offset from the start of the extent
3490 * for adjusting the disk offset below
3492 offset_in_extent = em_start - em->start;
3493 em_end = extent_map_end(em);
3494 em_len = em_end - em_start;
3495 emflags = em->flags;
3500 * bump off for our next call to get_extent
3502 off = extent_map_end(em);
3506 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
3508 flags |= FIEMAP_EXTENT_LAST;
3509 } else if (em->block_start == EXTENT_MAP_INLINE) {
3510 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3511 FIEMAP_EXTENT_NOT_ALIGNED);
3512 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
3513 flags |= (FIEMAP_EXTENT_DELALLOC |
3514 FIEMAP_EXTENT_UNKNOWN);
3516 disko = em->block_start + offset_in_extent;
3518 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3519 flags |= FIEMAP_EXTENT_ENCODED;
3521 free_extent_map(em);
3523 if ((em_start >= last) || em_len == (u64)-1 ||
3524 (last == (u64)-1 && isize <= em_end)) {
3525 flags |= FIEMAP_EXTENT_LAST;
3529 /* now scan forward to see if this is really the last extent. */
3530 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3537 flags |= FIEMAP_EXTENT_LAST;
3540 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3546 free_extent_map(em);
3548 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3549 &cached_state, GFP_NOFS);
3553 inline struct page *extent_buffer_page(struct extent_buffer *eb,
3556 return eb->pages[i];
3559 inline unsigned long num_extent_pages(u64 start, u64 len)
3561 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3562 (start >> PAGE_CACHE_SHIFT);
3565 static void __free_extent_buffer(struct extent_buffer *eb)
3568 unsigned long flags;
3569 spin_lock_irqsave(&leak_lock, flags);
3570 list_del(&eb->leak_list);
3571 spin_unlock_irqrestore(&leak_lock, flags);
3573 if (eb->pages && eb->pages != eb->inline_pages)
3575 kmem_cache_free(extent_buffer_cache, eb);
3578 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3583 struct extent_buffer *eb = NULL;
3585 unsigned long flags;
3588 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
3594 rwlock_init(&eb->lock);
3595 atomic_set(&eb->write_locks, 0);
3596 atomic_set(&eb->read_locks, 0);
3597 atomic_set(&eb->blocking_readers, 0);
3598 atomic_set(&eb->blocking_writers, 0);
3599 atomic_set(&eb->spinning_readers, 0);
3600 atomic_set(&eb->spinning_writers, 0);
3601 eb->lock_nested = 0;
3602 init_waitqueue_head(&eb->write_lock_wq);
3603 init_waitqueue_head(&eb->read_lock_wq);
3606 spin_lock_irqsave(&leak_lock, flags);
3607 list_add(&eb->leak_list, &buffers);
3608 spin_unlock_irqrestore(&leak_lock, flags);
3610 spin_lock_init(&eb->refs_lock);
3611 atomic_set(&eb->refs, 1);
3612 atomic_set(&eb->pages_reading, 0);
3614 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3615 struct page **pages;
3616 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3618 pages = kzalloc(num_pages, mask);
3620 __free_extent_buffer(eb);
3625 eb->pages = eb->inline_pages;
3632 * Helper for releasing extent buffer page.
3634 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3635 unsigned long start_idx)
3637 unsigned long index;
3640 index = num_extent_pages(eb->start, eb->len);
3641 if (start_idx >= index)
3646 page = extent_buffer_page(eb, index);
3648 spin_lock(&page->mapping->private_lock);
3650 * We do this since we'll remove the pages after we've
3651 * removed the eb from the radix tree, so we could race
3652 * and have this page now attached to the new eb. So
3653 * only clear page_private if it's still connected to
3656 if (PagePrivate(page) &&
3657 page->private == (unsigned long)eb) {
3658 BUG_ON(PageDirty(page));
3659 BUG_ON(PageWriteback(page));
3661 * We need to make sure we haven't be attached
3664 ClearPagePrivate(page);
3665 set_page_private(page, 0);
3666 /* One for the page private */
3667 page_cache_release(page);
3669 spin_unlock(&page->mapping->private_lock);
3671 /* One for when we alloced the page */
3672 page_cache_release(page);
3674 } while (index != start_idx);
3678 * Helper for releasing the extent buffer.
3680 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3682 btrfs_release_extent_buffer_page(eb, 0);
3683 __free_extent_buffer(eb);
3686 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
3688 unsigned long num_pages, i;
3690 num_pages = num_extent_pages(eb->start, eb->len);
3691 for (i = 0; i < num_pages; i++) {
3692 struct page *p = extent_buffer_page(eb, i);
3693 mark_page_accessed(p);
3697 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3698 u64 start, unsigned long len)
3700 unsigned long num_pages = num_extent_pages(start, len);
3702 unsigned long index = start >> PAGE_CACHE_SHIFT;
3703 struct extent_buffer *eb;
3704 struct extent_buffer *exists = NULL;
3706 struct address_space *mapping = tree->mapping;
3711 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3712 if (eb && atomic_inc_not_zero(&eb->refs)) {
3714 mark_extent_buffer_accessed(eb);
3719 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
3723 for (i = 0; i < num_pages; i++, index++) {
3724 p = find_or_create_page(mapping, index, GFP_NOFS);
3730 spin_lock(&mapping->private_lock);
3731 if (PagePrivate(p)) {
3733 * We could have already allocated an eb for this page
3734 * and attached one so lets see if we can get a ref on
3735 * the existing eb, and if we can we know it's good and
3736 * we can just return that one, else we know we can just
3737 * overwrite page->private.
3739 exists = (struct extent_buffer *)p->private;
3740 if (atomic_inc_not_zero(&exists->refs)) {
3741 spin_unlock(&mapping->private_lock);
3743 mark_extent_buffer_accessed(exists);
3748 * Do this so attach doesn't complain and we need to
3749 * drop the ref the old guy had.
3751 ClearPagePrivate(p);
3752 page_cache_release(p);
3754 attach_extent_buffer_page(eb, p);
3755 spin_unlock(&mapping->private_lock);
3756 mark_page_accessed(p);
3758 if (!PageUptodate(p))
3762 * see below about how we avoid a nasty race with release page
3763 * and why we unlock later
3767 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3769 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3773 spin_lock(&tree->buffer_lock);
3774 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3775 if (ret == -EEXIST) {
3776 exists = radix_tree_lookup(&tree->buffer,
3777 start >> PAGE_CACHE_SHIFT);
3778 if (!atomic_inc_not_zero(&exists->refs)) {
3779 spin_unlock(&tree->buffer_lock);
3780 radix_tree_preload_end();
3784 spin_unlock(&tree->buffer_lock);
3785 radix_tree_preload_end();
3786 mark_extent_buffer_accessed(exists);
3789 /* add one reference for the tree */
3790 spin_lock(&eb->refs_lock);
3791 atomic_inc(&eb->refs);
3792 set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags);
3793 spin_unlock(&eb->refs_lock);
3794 spin_unlock(&tree->buffer_lock);
3795 radix_tree_preload_end();
3798 * there is a race where release page may have
3799 * tried to find this extent buffer in the radix
3800 * but failed. It will tell the VM it is safe to
3801 * reclaim the, and it will clear the page private bit.
3802 * We must make sure to set the page private bit properly
3803 * after the extent buffer is in the radix tree so
3804 * it doesn't get lost
3806 SetPageChecked(eb->pages[0]);
3807 for (i = 1; i < num_pages; i++) {
3808 p = extent_buffer_page(eb, i);
3809 ClearPageChecked(p);
3812 unlock_page(eb->pages[0]);
3816 for (i = 0; i < num_pages; i++) {
3818 unlock_page(eb->pages[i]);
3821 if (!atomic_dec_and_test(&eb->refs))
3823 btrfs_release_extent_buffer(eb);
3827 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3828 u64 start, unsigned long len)
3830 struct extent_buffer *eb;
3833 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3834 if (eb && atomic_inc_not_zero(&eb->refs)) {
3836 mark_extent_buffer_accessed(eb);
3844 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
3846 struct extent_buffer *eb =
3847 container_of(head, struct extent_buffer, rcu_head);
3849 __free_extent_buffer(eb);
3852 static int extent_buffer_under_io(struct extent_buffer *eb,
3853 struct page *locked_page)
3855 unsigned long num_pages, i;
3857 num_pages = num_extent_pages(eb->start, eb->len);
3858 for (i = 0; i < num_pages; i++) {
3859 struct page *page = eb->pages[i];
3860 int need_unlock = 0;
3865 if (page != locked_page) {
3866 if (!trylock_page(page))
3871 if (PageDirty(page) || PageWriteback(page)) {
3883 /* Expects to have eb->eb_lock already held */
3884 static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
3886 WARN_ON(atomic_read(&eb->refs) == 0);
3887 if (atomic_dec_and_test(&eb->refs)) {
3888 struct extent_io_tree *tree = eb->tree;
3891 spin_unlock(&eb->refs_lock);
3893 might_sleep_if(mask & __GFP_WAIT);
3894 ret = clear_extent_bit(tree, eb->start,
3895 eb->start + eb->len - 1, -1, 0, 0,
3898 unsigned long num_pages, i;
3900 num_pages = num_extent_pages(eb->start, eb->len);
3902 * We failed to clear the state bits which likely means
3903 * ENOMEM, so just re-up the eb ref and continue, we
3904 * will get freed later on via releasepage or something
3905 * else and will be ok.
3907 spin_lock(&eb->tree->mapping->private_lock);
3908 spin_lock(&eb->refs_lock);
3909 set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags);
3910 atomic_inc(&eb->refs);
3913 * We may have started to reclaim the pages for a newly
3914 * allocated eb, make sure we own all of them again.
3916 for (i = 0; i < num_pages; i++) {
3917 struct page *page = eb->pages[i];
3924 BUG_ON(!PagePrivate(page));
3925 if (page->private != (unsigned long)eb) {
3926 ClearPagePrivate(page);
3927 page_cache_release(page);
3928 attach_extent_buffer_page(eb, page);
3931 spin_unlock(&eb->refs_lock);
3932 spin_unlock(&eb->tree->mapping->private_lock);
3936 spin_lock(&tree->buffer_lock);
3937 radix_tree_delete(&tree->buffer,
3938 eb->start >> PAGE_CACHE_SHIFT);
3939 spin_unlock(&tree->buffer_lock);
3941 /* Should be safe to release our pages at this point */
3942 btrfs_release_extent_buffer_page(eb, 0);
3944 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
3947 spin_unlock(&eb->refs_lock);
3950 void free_extent_buffer(struct extent_buffer *eb)
3955 spin_lock(&eb->refs_lock);
3956 if (atomic_read(&eb->refs) == 2 &&
3957 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
3958 !extent_buffer_under_io(eb, NULL) &&
3959 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
3960 atomic_dec(&eb->refs);
3963 * I know this is terrible, but it's temporary until we stop tracking
3964 * the uptodate bits and such for the extent buffers.
3966 release_extent_buffer(eb, GFP_ATOMIC);
3969 void free_extent_buffer_stale(struct extent_buffer *eb)
3974 spin_lock(&eb->refs_lock);
3975 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
3977 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb, NULL) &&
3978 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
3979 atomic_dec(&eb->refs);
3980 release_extent_buffer(eb, GFP_NOFS);
3983 int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3984 struct extent_buffer *eb)
3987 unsigned long num_pages;
3990 num_pages = num_extent_pages(eb->start, eb->len);
3992 for (i = 0; i < num_pages; i++) {
3993 page = extent_buffer_page(eb, i);
3994 if (!PageDirty(page))
3998 WARN_ON(!PagePrivate(page));
4000 clear_page_dirty_for_io(page);
4001 spin_lock_irq(&page->mapping->tree_lock);
4002 if (!PageDirty(page)) {
4003 radix_tree_tag_clear(&page->mapping->page_tree,
4005 PAGECACHE_TAG_DIRTY);
4007 spin_unlock_irq(&page->mapping->tree_lock);
4008 ClearPageError(page);
4014 int set_extent_buffer_dirty(struct extent_io_tree *tree,
4015 struct extent_buffer *eb)
4018 unsigned long num_pages;
4021 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4022 num_pages = num_extent_pages(eb->start, eb->len);
4023 WARN_ON(atomic_read(&eb->refs) == 0);
4024 for (i = 0; i < num_pages; i++)
4025 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
4029 static int __eb_straddles_pages(u64 start, u64 len)
4031 if (len < PAGE_CACHE_SIZE)
4033 if (start & (PAGE_CACHE_SIZE - 1))
4035 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4040 static int eb_straddles_pages(struct extent_buffer *eb)
4042 return __eb_straddles_pages(eb->start, eb->len);
4045 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
4046 struct extent_buffer *eb,
4047 struct extent_state **cached_state)
4051 unsigned long num_pages;
4053 num_pages = num_extent_pages(eb->start, eb->len);
4054 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4056 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
4057 cached_state, GFP_NOFS);
4059 for (i = 0; i < num_pages; i++) {
4060 page = extent_buffer_page(eb, i);
4062 ClearPageUptodate(page);
4067 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
4068 struct extent_buffer *eb)
4072 unsigned long num_pages;
4074 num_pages = num_extent_pages(eb->start, eb->len);
4076 if (eb_straddles_pages(eb)) {
4077 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
4080 for (i = 0; i < num_pages; i++) {
4081 page = extent_buffer_page(eb, i);
4082 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
4083 ((i == num_pages - 1) &&
4084 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
4085 check_page_uptodate(tree, page);
4088 SetPageUptodate(page);
4093 int extent_range_uptodate(struct extent_io_tree *tree,
4098 int pg_uptodate = 1;
4100 unsigned long index;
4102 if (__eb_straddles_pages(start, end - start + 1)) {
4103 ret = test_range_bit(tree, start, end,
4104 EXTENT_UPTODATE, 1, NULL);
4108 while (start <= end) {
4109 index = start >> PAGE_CACHE_SHIFT;
4110 page = find_get_page(tree->mapping, index);
4113 uptodate = PageUptodate(page);
4114 page_cache_release(page);
4119 start += PAGE_CACHE_SIZE;
4124 int extent_buffer_uptodate(struct extent_io_tree *tree,
4125 struct extent_buffer *eb,
4126 struct extent_state *cached_state)
4129 unsigned long num_pages;
4132 int pg_uptodate = 1;
4134 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4137 if (eb_straddles_pages(eb)) {
4138 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
4139 EXTENT_UPTODATE, 1, cached_state);
4144 num_pages = num_extent_pages(eb->start, eb->len);
4145 for (i = 0; i < num_pages; i++) {
4146 page = extent_buffer_page(eb, i);
4147 if (!PageUptodate(page)) {
4155 int read_extent_buffer_pages(struct extent_io_tree *tree,
4156 struct extent_buffer *eb, u64 start, int wait,
4157 get_extent_t *get_extent, int mirror_num)
4160 unsigned long start_i;
4164 int locked_pages = 0;
4165 int all_uptodate = 1;
4166 unsigned long num_pages;
4167 unsigned long num_reads = 0;
4168 struct bio *bio = NULL;
4169 unsigned long bio_flags = 0;
4171 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4174 if (eb_straddles_pages(eb)) {
4175 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
4176 EXTENT_UPTODATE, 1, NULL)) {
4182 WARN_ON(start < eb->start);
4183 start_i = (start >> PAGE_CACHE_SHIFT) -
4184 (eb->start >> PAGE_CACHE_SHIFT);
4189 num_pages = num_extent_pages(eb->start, eb->len);
4190 for (i = start_i; i < num_pages; i++) {
4191 page = extent_buffer_page(eb, i);
4192 if (wait == WAIT_NONE) {
4193 if (!trylock_page(page))
4199 if (!PageUptodate(page)) {
4206 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4210 atomic_set(&eb->pages_reading, num_reads);
4211 for (i = start_i; i < num_pages; i++) {
4212 page = extent_buffer_page(eb, i);
4213 if (!PageUptodate(page)) {
4214 ClearPageError(page);
4215 err = __extent_read_full_page(tree, page,
4217 mirror_num, &bio_flags);
4226 submit_one_bio(READ, bio, mirror_num, bio_flags);
4228 if (ret || wait != WAIT_COMPLETE)
4231 for (i = start_i; i < num_pages; i++) {
4232 page = extent_buffer_page(eb, i);
4233 wait_on_page_locked(page);
4234 if (!PageUptodate(page))
4239 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4244 while (locked_pages > 0) {
4245 page = extent_buffer_page(eb, i);
4253 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4254 unsigned long start,
4261 char *dst = (char *)dstv;
4262 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4263 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4265 WARN_ON(start > eb->len);
4266 WARN_ON(start + len > eb->start + eb->len);
4268 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4271 page = extent_buffer_page(eb, i);
4273 cur = min(len, (PAGE_CACHE_SIZE - offset));
4274 kaddr = page_address(page);
4275 memcpy(dst, kaddr + offset, cur);
4284 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4285 unsigned long min_len, char **map,
4286 unsigned long *map_start,
4287 unsigned long *map_len)
4289 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4292 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4293 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4294 unsigned long end_i = (start_offset + start + min_len - 1) >>
4301 offset = start_offset;
4305 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4308 if (start + min_len > eb->len) {
4309 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4310 "wanted %lu %lu\n", (unsigned long long)eb->start,
4311 eb->len, start, min_len);
4316 p = extent_buffer_page(eb, i);
4317 kaddr = page_address(p);
4318 *map = kaddr + offset;
4319 *map_len = PAGE_CACHE_SIZE - offset;
4323 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4324 unsigned long start,
4331 char *ptr = (char *)ptrv;
4332 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4333 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4336 WARN_ON(start > eb->len);
4337 WARN_ON(start + len > eb->start + eb->len);
4339 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4342 page = extent_buffer_page(eb, i);
4344 cur = min(len, (PAGE_CACHE_SIZE - offset));
4346 kaddr = page_address(page);
4347 ret = memcmp(ptr, kaddr + offset, cur);
4359 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4360 unsigned long start, unsigned long len)
4366 char *src = (char *)srcv;
4367 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4368 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4370 WARN_ON(start > eb->len);
4371 WARN_ON(start + len > eb->start + eb->len);
4373 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4376 page = extent_buffer_page(eb, i);
4377 WARN_ON(!PageUptodate(page));
4379 cur = min(len, PAGE_CACHE_SIZE - offset);
4380 kaddr = page_address(page);
4381 memcpy(kaddr + offset, src, cur);
4390 void memset_extent_buffer(struct extent_buffer *eb, char c,
4391 unsigned long start, unsigned long len)
4397 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4398 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4400 WARN_ON(start > eb->len);
4401 WARN_ON(start + len > eb->start + eb->len);
4403 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4406 page = extent_buffer_page(eb, i);
4407 WARN_ON(!PageUptodate(page));
4409 cur = min(len, PAGE_CACHE_SIZE - offset);
4410 kaddr = page_address(page);
4411 memset(kaddr + offset, c, cur);
4419 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4420 unsigned long dst_offset, unsigned long src_offset,
4423 u64 dst_len = dst->len;
4428 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4429 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4431 WARN_ON(src->len != dst_len);
4433 offset = (start_offset + dst_offset) &
4434 ((unsigned long)PAGE_CACHE_SIZE - 1);
4437 page = extent_buffer_page(dst, i);
4438 WARN_ON(!PageUptodate(page));
4440 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4442 kaddr = page_address(page);
4443 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4452 static void move_pages(struct page *dst_page, struct page *src_page,
4453 unsigned long dst_off, unsigned long src_off,
4456 char *dst_kaddr = page_address(dst_page);
4457 if (dst_page == src_page) {
4458 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4460 char *src_kaddr = page_address(src_page);
4461 char *p = dst_kaddr + dst_off + len;
4462 char *s = src_kaddr + src_off + len;
4469 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4471 unsigned long distance = (src > dst) ? src - dst : dst - src;
4472 return distance < len;
4475 static void copy_pages(struct page *dst_page, struct page *src_page,
4476 unsigned long dst_off, unsigned long src_off,
4479 char *dst_kaddr = page_address(dst_page);
4481 int must_memmove = 0;
4483 if (dst_page != src_page) {
4484 src_kaddr = page_address(src_page);
4486 src_kaddr = dst_kaddr;
4487 if (areas_overlap(src_off, dst_off, len))
4492 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4494 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
4497 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4498 unsigned long src_offset, unsigned long len)
4501 size_t dst_off_in_page;
4502 size_t src_off_in_page;
4503 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4504 unsigned long dst_i;
4505 unsigned long src_i;
4507 if (src_offset + len > dst->len) {
4508 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4509 "len %lu dst len %lu\n", src_offset, len, dst->len);
4512 if (dst_offset + len > dst->len) {
4513 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4514 "len %lu dst len %lu\n", dst_offset, len, dst->len);
4519 dst_off_in_page = (start_offset + dst_offset) &
4520 ((unsigned long)PAGE_CACHE_SIZE - 1);
4521 src_off_in_page = (start_offset + src_offset) &
4522 ((unsigned long)PAGE_CACHE_SIZE - 1);
4524 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4525 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4527 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4529 cur = min_t(unsigned long, cur,
4530 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4532 copy_pages(extent_buffer_page(dst, dst_i),
4533 extent_buffer_page(dst, src_i),
4534 dst_off_in_page, src_off_in_page, cur);
4542 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4543 unsigned long src_offset, unsigned long len)
4546 size_t dst_off_in_page;
4547 size_t src_off_in_page;
4548 unsigned long dst_end = dst_offset + len - 1;
4549 unsigned long src_end = src_offset + len - 1;
4550 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4551 unsigned long dst_i;
4552 unsigned long src_i;
4554 if (src_offset + len > dst->len) {
4555 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4556 "len %lu len %lu\n", src_offset, len, dst->len);
4559 if (dst_offset + len > dst->len) {
4560 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4561 "len %lu len %lu\n", dst_offset, len, dst->len);
4564 if (dst_offset < src_offset) {
4565 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4569 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4570 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4572 dst_off_in_page = (start_offset + dst_end) &
4573 ((unsigned long)PAGE_CACHE_SIZE - 1);
4574 src_off_in_page = (start_offset + src_end) &
4575 ((unsigned long)PAGE_CACHE_SIZE - 1);
4577 cur = min_t(unsigned long, len, src_off_in_page + 1);
4578 cur = min(cur, dst_off_in_page + 1);
4579 move_pages(extent_buffer_page(dst, dst_i),
4580 extent_buffer_page(dst, src_i),
4581 dst_off_in_page - cur + 1,
4582 src_off_in_page - cur + 1, cur);
4590 int try_release_extent_buffer(struct page *page, gfp_t mask)
4592 struct extent_buffer *eb;
4595 * We need to make sure noboody is attaching this page to an eb right
4598 spin_lock(&page->mapping->private_lock);
4599 if (!PagePrivate(page)) {
4600 spin_unlock(&page->mapping->private_lock);
4604 eb = (struct extent_buffer *)page->private;
4608 * This is a little awful but should be ok, we need to make sure that
4609 * the eb doesn't disappear out from under us while we're looking at
4612 spin_lock(&eb->refs_lock);
4613 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb, page)) {
4614 spin_unlock(&eb->refs_lock);
4615 spin_unlock(&page->mapping->private_lock);
4618 spin_unlock(&page->mapping->private_lock);
4620 if ((mask & GFP_NOFS) == GFP_NOFS)
4624 * If tree ref isn't set then we know the ref on this eb is a real ref,
4625 * so just return, this page will likely be freed soon anyway.
4627 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4628 spin_unlock(&eb->refs_lock);
4631 release_extent_buffer(eb, mask);