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"
24 static struct kmem_cache *extent_state_cache;
25 static struct kmem_cache *extent_buffer_cache;
27 static LIST_HEAD(buffers);
28 static LIST_HEAD(states);
32 static DEFINE_SPINLOCK(leak_lock);
35 #define BUFFER_LRU_MAX 64
40 struct rb_node rb_node;
43 struct extent_page_data {
45 struct extent_io_tree *tree;
46 get_extent_t *get_extent;
48 /* tells writepage not to lock the state bits for this range
49 * it still does the unlocking
51 unsigned int extent_locked:1;
53 /* tells the submit_bio code to use a WRITE_SYNC */
54 unsigned int sync_io:1;
57 static noinline void flush_write_bio(void *data);
59 int __init extent_io_init(void)
61 extent_state_cache = kmem_cache_create("extent_state",
62 sizeof(struct extent_state), 0,
63 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
64 if (!extent_state_cache)
67 extent_buffer_cache = kmem_cache_create("extent_buffers",
68 sizeof(struct extent_buffer), 0,
69 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
70 if (!extent_buffer_cache)
71 goto free_state_cache;
75 kmem_cache_destroy(extent_state_cache);
79 void extent_io_exit(void)
81 struct extent_state *state;
82 struct extent_buffer *eb;
84 while (!list_empty(&states)) {
85 state = list_entry(states.next, struct extent_state, leak_list);
86 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
87 "state %lu in tree %p refs %d\n",
88 (unsigned long long)state->start,
89 (unsigned long long)state->end,
90 state->state, state->tree, atomic_read(&state->refs));
91 list_del(&state->leak_list);
92 kmem_cache_free(extent_state_cache, state);
96 while (!list_empty(&buffers)) {
97 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
98 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
99 "refs %d\n", (unsigned long long)eb->start,
100 eb->len, atomic_read(&eb->refs));
101 list_del(&eb->leak_list);
102 kmem_cache_free(extent_buffer_cache, eb);
104 if (extent_state_cache)
105 kmem_cache_destroy(extent_state_cache);
106 if (extent_buffer_cache)
107 kmem_cache_destroy(extent_buffer_cache);
110 void extent_io_tree_init(struct extent_io_tree *tree,
111 struct address_space *mapping)
113 tree->state = RB_ROOT;
114 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
116 tree->dirty_bytes = 0;
117 spin_lock_init(&tree->lock);
118 spin_lock_init(&tree->buffer_lock);
119 tree->mapping = mapping;
122 static struct extent_state *alloc_extent_state(gfp_t mask)
124 struct extent_state *state;
129 state = kmem_cache_alloc(extent_state_cache, mask);
136 spin_lock_irqsave(&leak_lock, flags);
137 list_add(&state->leak_list, &states);
138 spin_unlock_irqrestore(&leak_lock, flags);
140 atomic_set(&state->refs, 1);
141 init_waitqueue_head(&state->wq);
145 void free_extent_state(struct extent_state *state)
149 if (atomic_dec_and_test(&state->refs)) {
153 WARN_ON(state->tree);
155 spin_lock_irqsave(&leak_lock, flags);
156 list_del(&state->leak_list);
157 spin_unlock_irqrestore(&leak_lock, flags);
159 kmem_cache_free(extent_state_cache, state);
163 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
164 struct rb_node *node)
166 struct rb_node **p = &root->rb_node;
167 struct rb_node *parent = NULL;
168 struct tree_entry *entry;
172 entry = rb_entry(parent, struct tree_entry, rb_node);
174 if (offset < entry->start)
176 else if (offset > entry->end)
182 entry = rb_entry(node, struct tree_entry, rb_node);
183 rb_link_node(node, parent, p);
184 rb_insert_color(node, root);
188 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
189 struct rb_node **prev_ret,
190 struct rb_node **next_ret)
192 struct rb_root *root = &tree->state;
193 struct rb_node *n = root->rb_node;
194 struct rb_node *prev = NULL;
195 struct rb_node *orig_prev = NULL;
196 struct tree_entry *entry;
197 struct tree_entry *prev_entry = NULL;
200 entry = rb_entry(n, struct tree_entry, rb_node);
204 if (offset < entry->start)
206 else if (offset > entry->end)
214 while (prev && offset > prev_entry->end) {
215 prev = rb_next(prev);
216 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
223 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
224 while (prev && offset < prev_entry->start) {
225 prev = rb_prev(prev);
226 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
233 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
236 struct rb_node *prev = NULL;
239 ret = __etree_search(tree, offset, &prev, NULL);
245 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
246 struct extent_state *other)
248 if (tree->ops && tree->ops->merge_extent_hook)
249 tree->ops->merge_extent_hook(tree->mapping->host, new,
254 * utility function to look for merge candidates inside a given range.
255 * Any extents with matching state are merged together into a single
256 * extent in the tree. Extents with EXTENT_IO in their state field
257 * are not merged because the end_io handlers need to be able to do
258 * operations on them without sleeping (or doing allocations/splits).
260 * This should be called with the tree lock held.
262 static void merge_state(struct extent_io_tree *tree,
263 struct extent_state *state)
265 struct extent_state *other;
266 struct rb_node *other_node;
268 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
271 other_node = rb_prev(&state->rb_node);
273 other = rb_entry(other_node, struct extent_state, rb_node);
274 if (other->end == state->start - 1 &&
275 other->state == state->state) {
276 merge_cb(tree, state, other);
277 state->start = other->start;
279 rb_erase(&other->rb_node, &tree->state);
280 free_extent_state(other);
283 other_node = rb_next(&state->rb_node);
285 other = rb_entry(other_node, struct extent_state, rb_node);
286 if (other->start == state->end + 1 &&
287 other->state == state->state) {
288 merge_cb(tree, state, other);
289 state->end = other->end;
291 rb_erase(&other->rb_node, &tree->state);
292 free_extent_state(other);
297 static void set_state_cb(struct extent_io_tree *tree,
298 struct extent_state *state, int *bits)
300 if (tree->ops && tree->ops->set_bit_hook)
301 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
304 static void clear_state_cb(struct extent_io_tree *tree,
305 struct extent_state *state, int *bits)
307 if (tree->ops && tree->ops->clear_bit_hook)
308 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
311 static void set_state_bits(struct extent_io_tree *tree,
312 struct extent_state *state, int *bits);
315 * insert an extent_state struct into the tree. 'bits' are set on the
316 * struct before it is inserted.
318 * This may return -EEXIST if the extent is already there, in which case the
319 * state struct is freed.
321 * The tree lock is not taken internally. This is a utility function and
322 * probably isn't what you want to call (see set/clear_extent_bit).
324 static int insert_state(struct extent_io_tree *tree,
325 struct extent_state *state, u64 start, u64 end,
328 struct rb_node *node;
331 printk(KERN_ERR "btrfs end < start %llu %llu\n",
332 (unsigned long long)end,
333 (unsigned long long)start);
336 state->start = start;
339 set_state_bits(tree, state, bits);
341 node = tree_insert(&tree->state, end, &state->rb_node);
343 struct extent_state *found;
344 found = rb_entry(node, struct extent_state, rb_node);
345 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
346 "%llu %llu\n", (unsigned long long)found->start,
347 (unsigned long long)found->end,
348 (unsigned long long)start, (unsigned long long)end);
352 merge_state(tree, state);
356 static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
359 if (tree->ops && tree->ops->split_extent_hook)
360 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
364 * split a given extent state struct in two, inserting the preallocated
365 * struct 'prealloc' as the newly created second half. 'split' indicates an
366 * offset inside 'orig' where it should be split.
369 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
370 * are two extent state structs in the tree:
371 * prealloc: [orig->start, split - 1]
372 * orig: [ split, orig->end ]
374 * The tree locks are not taken by this function. They need to be held
377 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
378 struct extent_state *prealloc, u64 split)
380 struct rb_node *node;
382 split_cb(tree, orig, split);
384 prealloc->start = orig->start;
385 prealloc->end = split - 1;
386 prealloc->state = orig->state;
389 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
391 free_extent_state(prealloc);
394 prealloc->tree = tree;
399 * utility function to clear some bits in an extent state struct.
400 * it will optionally wake up any one waiting on this state (wake == 1), or
401 * forcibly remove the state from the tree (delete == 1).
403 * If no bits are set on the state struct after clearing things, the
404 * struct is freed and removed from the tree
406 static int clear_state_bit(struct extent_io_tree *tree,
407 struct extent_state *state,
410 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
411 int ret = state->state & bits_to_clear;
413 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
414 u64 range = state->end - state->start + 1;
415 WARN_ON(range > tree->dirty_bytes);
416 tree->dirty_bytes -= range;
418 clear_state_cb(tree, state, bits);
419 state->state &= ~bits_to_clear;
422 if (state->state == 0) {
424 rb_erase(&state->rb_node, &tree->state);
426 free_extent_state(state);
431 merge_state(tree, state);
436 static struct extent_state *
437 alloc_extent_state_atomic(struct extent_state *prealloc)
440 prealloc = alloc_extent_state(GFP_ATOMIC);
446 * clear some bits on a range in the tree. This may require splitting
447 * or inserting elements in the tree, so the gfp mask is used to
448 * indicate which allocations or sleeping are allowed.
450 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
451 * the given range from the tree regardless of state (ie for truncate).
453 * the range [start, end] is inclusive.
455 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
456 * bits were already set, or zero if none of the bits were already set.
458 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
459 int bits, int wake, int delete,
460 struct extent_state **cached_state,
463 struct extent_state *state;
464 struct extent_state *cached;
465 struct extent_state *prealloc = NULL;
466 struct rb_node *next_node;
467 struct rb_node *node;
474 bits |= ~EXTENT_CTLBITS;
475 bits |= EXTENT_FIRST_DELALLOC;
477 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
480 if (!prealloc && (mask & __GFP_WAIT)) {
481 prealloc = alloc_extent_state(mask);
486 spin_lock(&tree->lock);
488 cached = *cached_state;
491 *cached_state = NULL;
495 if (cached && cached->tree && cached->start <= start &&
496 cached->end > start) {
498 atomic_dec(&cached->refs);
503 free_extent_state(cached);
506 * this search will find the extents that end after
509 node = tree_search(tree, start);
512 state = rb_entry(node, struct extent_state, rb_node);
514 if (state->start > end)
516 WARN_ON(state->end < start);
517 last_end = state->end;
519 if (state->end < end && !need_resched())
520 next_node = rb_next(&state->rb_node);
524 /* the state doesn't have the wanted bits, go ahead */
525 if (!(state->state & bits))
529 * | ---- desired range ---- |
531 * | ------------- state -------------- |
533 * We need to split the extent we found, and may flip
534 * bits on second half.
536 * If the extent we found extends past our range, we
537 * just split and search again. It'll get split again
538 * the next time though.
540 * If the extent we found is inside our range, we clear
541 * the desired bit on it.
544 if (state->start < start) {
545 prealloc = alloc_extent_state_atomic(prealloc);
547 err = split_state(tree, state, prealloc, start);
548 BUG_ON(err == -EEXIST);
552 if (state->end <= end) {
553 set |= clear_state_bit(tree, state, &bits, wake);
554 if (last_end == (u64)-1)
556 start = last_end + 1;
561 * | ---- desired range ---- |
563 * We need to split the extent, and clear the bit
566 if (state->start <= end && state->end > end) {
567 prealloc = alloc_extent_state_atomic(prealloc);
569 err = split_state(tree, state, prealloc, end + 1);
570 BUG_ON(err == -EEXIST);
574 set |= clear_state_bit(tree, prealloc, &bits, wake);
580 set |= clear_state_bit(tree, state, &bits, wake);
582 if (last_end == (u64)-1)
584 start = last_end + 1;
585 if (start <= end && next_node) {
586 state = rb_entry(next_node, struct extent_state,
593 spin_unlock(&tree->lock);
595 free_extent_state(prealloc);
602 spin_unlock(&tree->lock);
603 if (mask & __GFP_WAIT)
608 static int wait_on_state(struct extent_io_tree *tree,
609 struct extent_state *state)
610 __releases(tree->lock)
611 __acquires(tree->lock)
614 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
615 spin_unlock(&tree->lock);
617 spin_lock(&tree->lock);
618 finish_wait(&state->wq, &wait);
623 * waits for one or more bits to clear on a range in the state tree.
624 * The range [start, end] is inclusive.
625 * The tree lock is taken by this function
627 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
629 struct extent_state *state;
630 struct rb_node *node;
632 spin_lock(&tree->lock);
636 * this search will find all the extents that end after
639 node = tree_search(tree, start);
643 state = rb_entry(node, struct extent_state, rb_node);
645 if (state->start > end)
648 if (state->state & bits) {
649 start = state->start;
650 atomic_inc(&state->refs);
651 wait_on_state(tree, state);
652 free_extent_state(state);
655 start = state->end + 1;
660 cond_resched_lock(&tree->lock);
663 spin_unlock(&tree->lock);
667 static void set_state_bits(struct extent_io_tree *tree,
668 struct extent_state *state,
671 int bits_to_set = *bits & ~EXTENT_CTLBITS;
673 set_state_cb(tree, state, bits);
674 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
675 u64 range = state->end - state->start + 1;
676 tree->dirty_bytes += range;
678 state->state |= bits_to_set;
681 static void cache_state(struct extent_state *state,
682 struct extent_state **cached_ptr)
684 if (cached_ptr && !(*cached_ptr)) {
685 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
687 atomic_inc(&state->refs);
692 static void uncache_state(struct extent_state **cached_ptr)
694 if (cached_ptr && (*cached_ptr)) {
695 struct extent_state *state = *cached_ptr;
697 free_extent_state(state);
702 * set some bits on a range in the tree. This may require allocations or
703 * sleeping, so the gfp mask is used to indicate what is allowed.
705 * If any of the exclusive bits are set, this will fail with -EEXIST if some
706 * part of the range already has the desired bits set. The start of the
707 * existing range is returned in failed_start in this case.
709 * [start, end] is inclusive This takes the tree lock.
712 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
713 int bits, int exclusive_bits, u64 *failed_start,
714 struct extent_state **cached_state, gfp_t mask)
716 struct extent_state *state;
717 struct extent_state *prealloc = NULL;
718 struct rb_node *node;
723 bits |= EXTENT_FIRST_DELALLOC;
725 if (!prealloc && (mask & __GFP_WAIT)) {
726 prealloc = alloc_extent_state(mask);
730 spin_lock(&tree->lock);
731 if (cached_state && *cached_state) {
732 state = *cached_state;
733 if (state->start <= start && state->end > start &&
735 node = &state->rb_node;
740 * this search will find all the extents that end after
743 node = tree_search(tree, start);
745 prealloc = alloc_extent_state_atomic(prealloc);
747 err = insert_state(tree, prealloc, start, end, &bits);
749 BUG_ON(err == -EEXIST);
752 state = rb_entry(node, struct extent_state, rb_node);
754 last_start = state->start;
755 last_end = state->end;
758 * | ---- desired range ---- |
761 * Just lock what we found and keep going
763 if (state->start == start && state->end <= end) {
764 struct rb_node *next_node;
765 if (state->state & exclusive_bits) {
766 *failed_start = state->start;
771 set_state_bits(tree, state, &bits);
773 cache_state(state, cached_state);
774 merge_state(tree, state);
775 if (last_end == (u64)-1)
778 start = last_end + 1;
779 next_node = rb_next(&state->rb_node);
780 if (next_node && start < end && prealloc && !need_resched()) {
781 state = rb_entry(next_node, struct extent_state,
783 if (state->start == start)
790 * | ---- desired range ---- |
793 * | ------------- state -------------- |
795 * We need to split the extent we found, and may flip bits on
798 * If the extent we found extends past our
799 * range, we just split and search again. It'll get split
800 * again the next time though.
802 * If the extent we found is inside our range, we set the
805 if (state->start < start) {
806 if (state->state & exclusive_bits) {
807 *failed_start = start;
812 prealloc = alloc_extent_state_atomic(prealloc);
814 err = split_state(tree, state, prealloc, start);
815 BUG_ON(err == -EEXIST);
819 if (state->end <= end) {
820 set_state_bits(tree, state, &bits);
821 cache_state(state, cached_state);
822 merge_state(tree, state);
823 if (last_end == (u64)-1)
825 start = last_end + 1;
830 * | ---- desired range ---- |
831 * | state | or | state |
833 * There's a hole, we need to insert something in it and
834 * ignore the extent we found.
836 if (state->start > start) {
838 if (end < last_start)
841 this_end = last_start - 1;
843 prealloc = alloc_extent_state_atomic(prealloc);
847 * Avoid to free 'prealloc' if it can be merged with
850 err = insert_state(tree, prealloc, start, this_end,
852 BUG_ON(err == -EEXIST);
854 free_extent_state(prealloc);
858 cache_state(prealloc, cached_state);
860 start = this_end + 1;
864 * | ---- desired range ---- |
866 * We need to split the extent, and set the bit
869 if (state->start <= end && state->end > end) {
870 if (state->state & exclusive_bits) {
871 *failed_start = start;
876 prealloc = alloc_extent_state_atomic(prealloc);
878 err = split_state(tree, state, prealloc, end + 1);
879 BUG_ON(err == -EEXIST);
881 set_state_bits(tree, prealloc, &bits);
882 cache_state(prealloc, cached_state);
883 merge_state(tree, prealloc);
891 spin_unlock(&tree->lock);
893 free_extent_state(prealloc);
900 spin_unlock(&tree->lock);
901 if (mask & __GFP_WAIT)
907 * convert_extent - convert all bits in a given range from one bit to another
908 * @tree: the io tree to search
909 * @start: the start offset in bytes
910 * @end: the end offset in bytes (inclusive)
911 * @bits: the bits to set in this range
912 * @clear_bits: the bits to clear in this range
913 * @mask: the allocation mask
915 * This will go through and set bits for the given range. If any states exist
916 * already in this range they are set with the given bit and cleared of the
917 * clear_bits. This is only meant to be used by things that are mergeable, ie
918 * converting from say DELALLOC to DIRTY. This is not meant to be used with
919 * boundary bits like LOCK.
921 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
922 int bits, int clear_bits, gfp_t mask)
924 struct extent_state *state;
925 struct extent_state *prealloc = NULL;
926 struct rb_node *node;
932 if (!prealloc && (mask & __GFP_WAIT)) {
933 prealloc = alloc_extent_state(mask);
938 spin_lock(&tree->lock);
940 * this search will find all the extents that end after
943 node = tree_search(tree, start);
945 prealloc = alloc_extent_state_atomic(prealloc);
950 err = insert_state(tree, prealloc, start, end, &bits);
952 BUG_ON(err == -EEXIST);
955 state = rb_entry(node, struct extent_state, rb_node);
957 last_start = state->start;
958 last_end = state->end;
961 * | ---- desired range ---- |
964 * Just lock what we found and keep going
966 if (state->start == start && state->end <= end) {
967 struct rb_node *next_node;
969 set_state_bits(tree, state, &bits);
970 clear_state_bit(tree, state, &clear_bits, 0);
971 if (last_end == (u64)-1)
974 start = last_end + 1;
975 next_node = rb_next(&state->rb_node);
976 if (next_node && start < end && prealloc && !need_resched()) {
977 state = rb_entry(next_node, struct extent_state,
979 if (state->start == start)
986 * | ---- desired range ---- |
989 * | ------------- state -------------- |
991 * We need to split the extent we found, and may flip bits on
994 * If the extent we found extends past our
995 * range, we just split and search again. It'll get split
996 * again the next time though.
998 * If the extent we found is inside our range, we set the
1001 if (state->start < start) {
1002 prealloc = alloc_extent_state_atomic(prealloc);
1007 err = split_state(tree, state, prealloc, start);
1008 BUG_ON(err == -EEXIST);
1012 if (state->end <= end) {
1013 set_state_bits(tree, state, &bits);
1014 clear_state_bit(tree, state, &clear_bits, 0);
1015 if (last_end == (u64)-1)
1017 start = last_end + 1;
1022 * | ---- desired range ---- |
1023 * | state | or | state |
1025 * There's a hole, we need to insert something in it and
1026 * ignore the extent we found.
1028 if (state->start > start) {
1030 if (end < last_start)
1033 this_end = last_start - 1;
1035 prealloc = alloc_extent_state_atomic(prealloc);
1042 * Avoid to free 'prealloc' if it can be merged with
1045 err = insert_state(tree, prealloc, start, this_end,
1047 BUG_ON(err == -EEXIST);
1049 free_extent_state(prealloc);
1054 start = this_end + 1;
1058 * | ---- desired range ---- |
1060 * We need to split the extent, and set the bit
1063 if (state->start <= end && state->end > end) {
1064 prealloc = alloc_extent_state_atomic(prealloc);
1070 err = split_state(tree, state, prealloc, end + 1);
1071 BUG_ON(err == -EEXIST);
1073 set_state_bits(tree, prealloc, &bits);
1074 clear_state_bit(tree, prealloc, &clear_bits, 0);
1082 spin_unlock(&tree->lock);
1084 free_extent_state(prealloc);
1091 spin_unlock(&tree->lock);
1092 if (mask & __GFP_WAIT)
1097 /* wrappers around set/clear extent bit */
1098 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1101 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
1105 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1106 int bits, gfp_t mask)
1108 return set_extent_bit(tree, start, end, bits, 0, NULL,
1112 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1113 int bits, gfp_t mask)
1115 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
1118 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
1119 struct extent_state **cached_state, gfp_t mask)
1121 return set_extent_bit(tree, start, end,
1122 EXTENT_DELALLOC | EXTENT_UPTODATE,
1123 0, NULL, cached_state, mask);
1126 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1129 return clear_extent_bit(tree, start, end,
1130 EXTENT_DIRTY | EXTENT_DELALLOC |
1131 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
1134 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1137 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
1141 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1142 struct extent_state **cached_state, gfp_t mask)
1144 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1145 NULL, cached_state, mask);
1148 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
1149 u64 end, struct extent_state **cached_state,
1152 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1153 cached_state, mask);
1157 * either insert or lock state struct between start and end use mask to tell
1158 * us if waiting is desired.
1160 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1161 int bits, struct extent_state **cached_state, gfp_t mask)
1166 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1167 EXTENT_LOCKED, &failed_start,
1168 cached_state, mask);
1169 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1170 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1171 start = failed_start;
1175 WARN_ON(start > end);
1180 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1182 return lock_extent_bits(tree, start, end, 0, NULL, mask);
1185 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1191 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1192 &failed_start, NULL, mask);
1193 if (err == -EEXIST) {
1194 if (failed_start > start)
1195 clear_extent_bit(tree, start, failed_start - 1,
1196 EXTENT_LOCKED, 1, 0, NULL, mask);
1202 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1203 struct extent_state **cached, gfp_t mask)
1205 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1209 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1211 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1216 * helper function to set both pages and extents in the tree writeback
1218 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1220 unsigned long index = start >> PAGE_CACHE_SHIFT;
1221 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1224 while (index <= end_index) {
1225 page = find_get_page(tree->mapping, index);
1227 set_page_writeback(page);
1228 page_cache_release(page);
1234 /* find the first state struct with 'bits' set after 'start', and
1235 * return it. tree->lock must be held. NULL will returned if
1236 * nothing was found after 'start'
1238 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1239 u64 start, int bits)
1241 struct rb_node *node;
1242 struct extent_state *state;
1245 * this search will find all the extents that end after
1248 node = tree_search(tree, start);
1253 state = rb_entry(node, struct extent_state, rb_node);
1254 if (state->end >= start && (state->state & bits))
1257 node = rb_next(node);
1266 * find the first offset in the io tree with 'bits' set. zero is
1267 * returned if we find something, and *start_ret and *end_ret are
1268 * set to reflect the state struct that was found.
1270 * If nothing was found, 1 is returned, < 0 on error
1272 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1273 u64 *start_ret, u64 *end_ret, int bits)
1275 struct extent_state *state;
1278 spin_lock(&tree->lock);
1279 state = find_first_extent_bit_state(tree, start, bits);
1281 *start_ret = state->start;
1282 *end_ret = state->end;
1285 spin_unlock(&tree->lock);
1290 * find a contiguous range of bytes in the file marked as delalloc, not
1291 * more than 'max_bytes'. start and end are used to return the range,
1293 * 1 is returned if we find something, 0 if nothing was in the tree
1295 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1296 u64 *start, u64 *end, u64 max_bytes,
1297 struct extent_state **cached_state)
1299 struct rb_node *node;
1300 struct extent_state *state;
1301 u64 cur_start = *start;
1303 u64 total_bytes = 0;
1305 spin_lock(&tree->lock);
1308 * this search will find all the extents that end after
1311 node = tree_search(tree, cur_start);
1319 state = rb_entry(node, struct extent_state, rb_node);
1320 if (found && (state->start != cur_start ||
1321 (state->state & EXTENT_BOUNDARY))) {
1324 if (!(state->state & EXTENT_DELALLOC)) {
1330 *start = state->start;
1331 *cached_state = state;
1332 atomic_inc(&state->refs);
1336 cur_start = state->end + 1;
1337 node = rb_next(node);
1340 total_bytes += state->end - state->start + 1;
1341 if (total_bytes >= max_bytes)
1345 spin_unlock(&tree->lock);
1349 static noinline int __unlock_for_delalloc(struct inode *inode,
1350 struct page *locked_page,
1354 struct page *pages[16];
1355 unsigned long index = start >> PAGE_CACHE_SHIFT;
1356 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1357 unsigned long nr_pages = end_index - index + 1;
1360 if (index == locked_page->index && end_index == index)
1363 while (nr_pages > 0) {
1364 ret = find_get_pages_contig(inode->i_mapping, index,
1365 min_t(unsigned long, nr_pages,
1366 ARRAY_SIZE(pages)), pages);
1367 for (i = 0; i < ret; i++) {
1368 if (pages[i] != locked_page)
1369 unlock_page(pages[i]);
1370 page_cache_release(pages[i]);
1379 static noinline int lock_delalloc_pages(struct inode *inode,
1380 struct page *locked_page,
1384 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1385 unsigned long start_index = index;
1386 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1387 unsigned long pages_locked = 0;
1388 struct page *pages[16];
1389 unsigned long nrpages;
1393 /* the caller is responsible for locking the start index */
1394 if (index == locked_page->index && index == end_index)
1397 /* skip the page at the start index */
1398 nrpages = end_index - index + 1;
1399 while (nrpages > 0) {
1400 ret = find_get_pages_contig(inode->i_mapping, index,
1401 min_t(unsigned long,
1402 nrpages, ARRAY_SIZE(pages)), pages);
1407 /* now we have an array of pages, lock them all */
1408 for (i = 0; i < ret; i++) {
1410 * the caller is taking responsibility for
1413 if (pages[i] != locked_page) {
1414 lock_page(pages[i]);
1415 if (!PageDirty(pages[i]) ||
1416 pages[i]->mapping != inode->i_mapping) {
1418 unlock_page(pages[i]);
1419 page_cache_release(pages[i]);
1423 page_cache_release(pages[i]);
1432 if (ret && pages_locked) {
1433 __unlock_for_delalloc(inode, locked_page,
1435 ((u64)(start_index + pages_locked - 1)) <<
1442 * find a contiguous range of bytes in the file marked as delalloc, not
1443 * more than 'max_bytes'. start and end are used to return the range,
1445 * 1 is returned if we find something, 0 if nothing was in the tree
1447 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1448 struct extent_io_tree *tree,
1449 struct page *locked_page,
1450 u64 *start, u64 *end,
1456 struct extent_state *cached_state = NULL;
1461 /* step one, find a bunch of delalloc bytes starting at start */
1462 delalloc_start = *start;
1464 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1465 max_bytes, &cached_state);
1466 if (!found || delalloc_end <= *start) {
1467 *start = delalloc_start;
1468 *end = delalloc_end;
1469 free_extent_state(cached_state);
1474 * start comes from the offset of locked_page. We have to lock
1475 * pages in order, so we can't process delalloc bytes before
1478 if (delalloc_start < *start)
1479 delalloc_start = *start;
1482 * make sure to limit the number of pages we try to lock down
1485 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1486 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1488 /* step two, lock all the pages after the page that has start */
1489 ret = lock_delalloc_pages(inode, locked_page,
1490 delalloc_start, delalloc_end);
1491 if (ret == -EAGAIN) {
1492 /* some of the pages are gone, lets avoid looping by
1493 * shortening the size of the delalloc range we're searching
1495 free_extent_state(cached_state);
1497 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1498 max_bytes = PAGE_CACHE_SIZE - offset;
1508 /* step three, lock the state bits for the whole range */
1509 lock_extent_bits(tree, delalloc_start, delalloc_end,
1510 0, &cached_state, GFP_NOFS);
1512 /* then test to make sure it is all still delalloc */
1513 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1514 EXTENT_DELALLOC, 1, cached_state);
1516 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1517 &cached_state, GFP_NOFS);
1518 __unlock_for_delalloc(inode, locked_page,
1519 delalloc_start, delalloc_end);
1523 free_extent_state(cached_state);
1524 *start = delalloc_start;
1525 *end = delalloc_end;
1530 int extent_clear_unlock_delalloc(struct inode *inode,
1531 struct extent_io_tree *tree,
1532 u64 start, u64 end, struct page *locked_page,
1536 struct page *pages[16];
1537 unsigned long index = start >> PAGE_CACHE_SHIFT;
1538 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1539 unsigned long nr_pages = end_index - index + 1;
1543 if (op & EXTENT_CLEAR_UNLOCK)
1544 clear_bits |= EXTENT_LOCKED;
1545 if (op & EXTENT_CLEAR_DIRTY)
1546 clear_bits |= EXTENT_DIRTY;
1548 if (op & EXTENT_CLEAR_DELALLOC)
1549 clear_bits |= EXTENT_DELALLOC;
1551 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1552 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1553 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1554 EXTENT_SET_PRIVATE2)))
1557 while (nr_pages > 0) {
1558 ret = find_get_pages_contig(inode->i_mapping, index,
1559 min_t(unsigned long,
1560 nr_pages, ARRAY_SIZE(pages)), pages);
1561 for (i = 0; i < ret; i++) {
1563 if (op & EXTENT_SET_PRIVATE2)
1564 SetPagePrivate2(pages[i]);
1566 if (pages[i] == locked_page) {
1567 page_cache_release(pages[i]);
1570 if (op & EXTENT_CLEAR_DIRTY)
1571 clear_page_dirty_for_io(pages[i]);
1572 if (op & EXTENT_SET_WRITEBACK)
1573 set_page_writeback(pages[i]);
1574 if (op & EXTENT_END_WRITEBACK)
1575 end_page_writeback(pages[i]);
1576 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1577 unlock_page(pages[i]);
1578 page_cache_release(pages[i]);
1588 * count the number of bytes in the tree that have a given bit(s)
1589 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1590 * cached. The total number found is returned.
1592 u64 count_range_bits(struct extent_io_tree *tree,
1593 u64 *start, u64 search_end, u64 max_bytes,
1594 unsigned long bits, int contig)
1596 struct rb_node *node;
1597 struct extent_state *state;
1598 u64 cur_start = *start;
1599 u64 total_bytes = 0;
1603 if (search_end <= cur_start) {
1608 spin_lock(&tree->lock);
1609 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1610 total_bytes = tree->dirty_bytes;
1614 * this search will find all the extents that end after
1617 node = tree_search(tree, cur_start);
1622 state = rb_entry(node, struct extent_state, rb_node);
1623 if (state->start > search_end)
1625 if (contig && found && state->start > last + 1)
1627 if (state->end >= cur_start && (state->state & bits) == bits) {
1628 total_bytes += min(search_end, state->end) + 1 -
1629 max(cur_start, state->start);
1630 if (total_bytes >= max_bytes)
1633 *start = max(cur_start, state->start);
1637 } else if (contig && found) {
1640 node = rb_next(node);
1645 spin_unlock(&tree->lock);
1650 * set the private field for a given byte offset in the tree. If there isn't
1651 * an extent_state there already, this does nothing.
1653 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1655 struct rb_node *node;
1656 struct extent_state *state;
1659 spin_lock(&tree->lock);
1661 * this search will find all the extents that end after
1664 node = tree_search(tree, start);
1669 state = rb_entry(node, struct extent_state, rb_node);
1670 if (state->start != start) {
1674 state->private = private;
1676 spin_unlock(&tree->lock);
1680 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1682 struct rb_node *node;
1683 struct extent_state *state;
1686 spin_lock(&tree->lock);
1688 * this search will find all the extents that end after
1691 node = tree_search(tree, start);
1696 state = rb_entry(node, struct extent_state, rb_node);
1697 if (state->start != start) {
1701 *private = state->private;
1703 spin_unlock(&tree->lock);
1708 * searches a range in the state tree for a given mask.
1709 * If 'filled' == 1, this returns 1 only if every extent in the tree
1710 * has the bits set. Otherwise, 1 is returned if any bit in the
1711 * range is found set.
1713 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1714 int bits, int filled, struct extent_state *cached)
1716 struct extent_state *state = NULL;
1717 struct rb_node *node;
1720 spin_lock(&tree->lock);
1721 if (cached && cached->tree && cached->start <= start &&
1722 cached->end > start)
1723 node = &cached->rb_node;
1725 node = tree_search(tree, start);
1726 while (node && start <= end) {
1727 state = rb_entry(node, struct extent_state, rb_node);
1729 if (filled && state->start > start) {
1734 if (state->start > end)
1737 if (state->state & bits) {
1741 } else if (filled) {
1746 if (state->end == (u64)-1)
1749 start = state->end + 1;
1752 node = rb_next(node);
1759 spin_unlock(&tree->lock);
1764 * helper function to set a given page up to date if all the
1765 * extents in the tree for that page are up to date
1767 static int check_page_uptodate(struct extent_io_tree *tree,
1770 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1771 u64 end = start + PAGE_CACHE_SIZE - 1;
1772 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1773 SetPageUptodate(page);
1778 * helper function to unlock a page if all the extents in the tree
1779 * for that page are unlocked
1781 static int check_page_locked(struct extent_io_tree *tree,
1784 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1785 u64 end = start + PAGE_CACHE_SIZE - 1;
1786 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1792 * helper function to end page writeback if all the extents
1793 * in the tree for that page are done with writeback
1795 static int check_page_writeback(struct extent_io_tree *tree,
1798 end_page_writeback(page);
1803 * When IO fails, either with EIO or csum verification fails, we
1804 * try other mirrors that might have a good copy of the data. This
1805 * io_failure_record is used to record state as we go through all the
1806 * mirrors. If another mirror has good data, the page is set up to date
1807 * and things continue. If a good mirror can't be found, the original
1808 * bio end_io callback is called to indicate things have failed.
1810 struct io_failure_record {
1815 unsigned long bio_flags;
1821 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1826 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1828 set_state_private(failure_tree, rec->start, 0);
1829 ret = clear_extent_bits(failure_tree, rec->start,
1830 rec->start + rec->len - 1,
1831 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1836 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1837 rec->start + rec->len - 1,
1838 EXTENT_DAMAGED, GFP_NOFS);
1847 static void repair_io_failure_callback(struct bio *bio, int err)
1849 complete(bio->bi_private);
1853 * this bypasses the standard btrfs submit functions deliberately, as
1854 * the standard behavior is to write all copies in a raid setup. here we only
1855 * want to write the one bad copy. so we do the mapping for ourselves and issue
1856 * submit_bio directly.
1857 * to avoid any synchonization issues, wait for the data after writing, which
1858 * actually prevents the read that triggered the error from finishing.
1859 * currently, there can be no more than two copies of every data bit. thus,
1860 * exactly one rewrite is required.
1862 int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1863 u64 length, u64 logical, struct page *page,
1867 struct btrfs_device *dev;
1868 DECLARE_COMPLETION_ONSTACK(compl);
1871 struct btrfs_bio *bbio = NULL;
1874 BUG_ON(!mirror_num);
1876 bio = bio_alloc(GFP_NOFS, 1);
1879 bio->bi_private = &compl;
1880 bio->bi_end_io = repair_io_failure_callback;
1882 map_length = length;
1884 ret = btrfs_map_block(map_tree, WRITE, logical,
1885 &map_length, &bbio, mirror_num);
1890 BUG_ON(mirror_num != bbio->mirror_num);
1891 sector = bbio->stripes[mirror_num-1].physical >> 9;
1892 bio->bi_sector = sector;
1893 dev = bbio->stripes[mirror_num-1].dev;
1895 if (!dev || !dev->bdev || !dev->writeable) {
1899 bio->bi_bdev = dev->bdev;
1900 bio_add_page(bio, page, length, start-page_offset(page));
1901 btrfsic_submit_bio(WRITE_SYNC, bio);
1902 wait_for_completion(&compl);
1904 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1905 /* try to remap that extent elsewhere? */
1910 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1911 "sector %llu)\n", page->mapping->host->i_ino, start,
1919 * each time an IO finishes, we do a fast check in the IO failure tree
1920 * to see if we need to process or clean up an io_failure_record
1922 static int clean_io_failure(u64 start, struct page *page)
1925 u64 private_failure;
1926 struct io_failure_record *failrec;
1927 struct btrfs_mapping_tree *map_tree;
1928 struct extent_state *state;
1932 struct inode *inode = page->mapping->host;
1935 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1936 (u64)-1, 1, EXTENT_DIRTY, 0);
1940 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1945 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1946 BUG_ON(!failrec->this_mirror);
1948 if (failrec->in_validation) {
1949 /* there was no real error, just free the record */
1950 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1956 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1957 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1960 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1962 if (state && state->start == failrec->start) {
1963 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1964 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1966 if (num_copies > 1) {
1967 ret = repair_io_failure(map_tree, start, failrec->len,
1968 failrec->logical, page,
1969 failrec->failed_mirror);
1976 ret = free_io_failure(inode, failrec, did_repair);
1982 * this is a generic handler for readpage errors (default
1983 * readpage_io_failed_hook). if other copies exist, read those and write back
1984 * good data to the failed position. does not investigate in remapping the
1985 * failed extent elsewhere, hoping the device will be smart enough to do this as
1989 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
1990 u64 start, u64 end, int failed_mirror,
1991 struct extent_state *state)
1993 struct io_failure_record *failrec = NULL;
1995 struct extent_map *em;
1996 struct inode *inode = page->mapping->host;
1997 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1998 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
1999 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2006 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2008 ret = get_state_private(failure_tree, start, &private);
2010 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2013 failrec->start = start;
2014 failrec->len = end - start + 1;
2015 failrec->this_mirror = 0;
2016 failrec->bio_flags = 0;
2017 failrec->in_validation = 0;
2019 read_lock(&em_tree->lock);
2020 em = lookup_extent_mapping(em_tree, start, failrec->len);
2022 read_unlock(&em_tree->lock);
2027 if (em->start > start || em->start + em->len < start) {
2028 free_extent_map(em);
2031 read_unlock(&em_tree->lock);
2033 if (!em || IS_ERR(em)) {
2037 logical = start - em->start;
2038 logical = em->block_start + logical;
2039 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2040 logical = em->block_start;
2041 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2042 extent_set_compress_type(&failrec->bio_flags,
2045 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2046 "len=%llu\n", logical, start, failrec->len);
2047 failrec->logical = logical;
2048 free_extent_map(em);
2050 /* set the bits in the private failure tree */
2051 ret = set_extent_bits(failure_tree, start, end,
2052 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2054 ret = set_state_private(failure_tree, start,
2055 (u64)(unsigned long)failrec);
2056 /* set the bits in the inode's tree */
2058 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2065 failrec = (struct io_failure_record *)(unsigned long)private;
2066 pr_debug("bio_readpage_error: (found) logical=%llu, "
2067 "start=%llu, len=%llu, validation=%d\n",
2068 failrec->logical, failrec->start, failrec->len,
2069 failrec->in_validation);
2071 * when data can be on disk more than twice, add to failrec here
2072 * (e.g. with a list for failed_mirror) to make
2073 * clean_io_failure() clean all those errors at once.
2076 num_copies = btrfs_num_copies(
2077 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2078 failrec->logical, failrec->len);
2079 if (num_copies == 1) {
2081 * we only have a single copy of the data, so don't bother with
2082 * all the retry and error correction code that follows. no
2083 * matter what the error is, it is very likely to persist.
2085 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2086 "state=%p, num_copies=%d, next_mirror %d, "
2087 "failed_mirror %d\n", state, num_copies,
2088 failrec->this_mirror, failed_mirror);
2089 free_io_failure(inode, failrec, 0);
2094 spin_lock(&tree->lock);
2095 state = find_first_extent_bit_state(tree, failrec->start,
2097 if (state && state->start != failrec->start)
2099 spin_unlock(&tree->lock);
2103 * there are two premises:
2104 * a) deliver good data to the caller
2105 * b) correct the bad sectors on disk
2107 if (failed_bio->bi_vcnt > 1) {
2109 * to fulfill b), we need to know the exact failing sectors, as
2110 * we don't want to rewrite any more than the failed ones. thus,
2111 * we need separate read requests for the failed bio
2113 * if the following BUG_ON triggers, our validation request got
2114 * merged. we need separate requests for our algorithm to work.
2116 BUG_ON(failrec->in_validation);
2117 failrec->in_validation = 1;
2118 failrec->this_mirror = failed_mirror;
2119 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2122 * we're ready to fulfill a) and b) alongside. get a good copy
2123 * of the failed sector and if we succeed, we have setup
2124 * everything for repair_io_failure to do the rest for us.
2126 if (failrec->in_validation) {
2127 BUG_ON(failrec->this_mirror != failed_mirror);
2128 failrec->in_validation = 0;
2129 failrec->this_mirror = 0;
2131 failrec->failed_mirror = failed_mirror;
2132 failrec->this_mirror++;
2133 if (failrec->this_mirror == failed_mirror)
2134 failrec->this_mirror++;
2135 read_mode = READ_SYNC;
2138 if (!state || failrec->this_mirror > num_copies) {
2139 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2140 "next_mirror %d, failed_mirror %d\n", state,
2141 num_copies, failrec->this_mirror, failed_mirror);
2142 free_io_failure(inode, failrec, 0);
2146 bio = bio_alloc(GFP_NOFS, 1);
2147 bio->bi_private = state;
2148 bio->bi_end_io = failed_bio->bi_end_io;
2149 bio->bi_sector = failrec->logical >> 9;
2150 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2153 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2155 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2156 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2157 failrec->this_mirror, num_copies, failrec->in_validation);
2159 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2160 failrec->this_mirror,
2161 failrec->bio_flags, 0);
2165 /* lots and lots of room for performance fixes in the end_bio funcs */
2167 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2169 int uptodate = (err == 0);
2170 struct extent_io_tree *tree;
2173 tree = &BTRFS_I(page->mapping->host)->io_tree;
2175 if (tree->ops && tree->ops->writepage_end_io_hook) {
2176 ret = tree->ops->writepage_end_io_hook(page, start,
2177 end, NULL, uptodate);
2182 if (!uptodate && tree->ops &&
2183 tree->ops->writepage_io_failed_hook) {
2184 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2186 /* Writeback already completed */
2192 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2193 ClearPageUptodate(page);
2200 * after a writepage IO is done, we need to:
2201 * clear the uptodate bits on error
2202 * clear the writeback bits in the extent tree for this IO
2203 * end_page_writeback if the page has no more pending IO
2205 * Scheduling is not allowed, so the extent state tree is expected
2206 * to have one and only one object corresponding to this IO.
2208 static void end_bio_extent_writepage(struct bio *bio, int err)
2210 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2211 struct extent_io_tree *tree;
2217 struct page *page = bvec->bv_page;
2218 tree = &BTRFS_I(page->mapping->host)->io_tree;
2220 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2222 end = start + bvec->bv_len - 1;
2224 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2229 if (--bvec >= bio->bi_io_vec)
2230 prefetchw(&bvec->bv_page->flags);
2232 if (end_extent_writepage(page, err, start, end))
2236 end_page_writeback(page);
2238 check_page_writeback(tree, page);
2239 } while (bvec >= bio->bi_io_vec);
2245 * after a readpage IO is done, we need to:
2246 * clear the uptodate bits on error
2247 * set the uptodate bits if things worked
2248 * set the page up to date if all extents in the tree are uptodate
2249 * clear the lock bit in the extent tree
2250 * unlock the page if there are no other extents locked for it
2252 * Scheduling is not allowed, so the extent state tree is expected
2253 * to have one and only one object corresponding to this IO.
2255 static void end_bio_extent_readpage(struct bio *bio, int err)
2257 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2258 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2259 struct bio_vec *bvec = bio->bi_io_vec;
2260 struct extent_io_tree *tree;
2270 struct page *page = bvec->bv_page;
2271 struct extent_state *cached = NULL;
2272 struct extent_state *state;
2274 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2275 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2276 (long int)bio->bi_bdev);
2277 tree = &BTRFS_I(page->mapping->host)->io_tree;
2279 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2281 end = start + bvec->bv_len - 1;
2283 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2288 if (++bvec <= bvec_end)
2289 prefetchw(&bvec->bv_page->flags);
2291 spin_lock(&tree->lock);
2292 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2293 if (state && state->start == start) {
2295 * take a reference on the state, unlock will drop
2298 cache_state(state, &cached);
2300 spin_unlock(&tree->lock);
2302 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2303 ret = tree->ops->readpage_end_io_hook(page, start, end,
2308 clean_io_failure(start, page);
2312 failed_mirror = (int)(unsigned long)bio->bi_bdev;
2314 * The generic bio_readpage_error handles errors the
2315 * following way: If possible, new read requests are
2316 * created and submitted and will end up in
2317 * end_bio_extent_readpage as well (if we're lucky, not
2318 * in the !uptodate case). In that case it returns 0 and
2319 * we just go on with the next page in our bio. If it
2320 * can't handle the error it will return -EIO and we
2321 * remain responsible for that page.
2323 ret = bio_readpage_error(bio, page, start, end,
2324 failed_mirror, NULL);
2328 test_bit(BIO_UPTODATE, &bio->bi_flags);
2331 uncache_state(&cached);
2334 if (tree->ops && tree->ops->readpage_io_failed_hook) {
2335 ret = tree->ops->readpage_io_failed_hook(
2336 bio, page, start, end,
2337 failed_mirror, state);
2343 if (uptodate && tree->track_uptodate) {
2344 set_extent_uptodate(tree, start, end, &cached,
2347 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2351 SetPageUptodate(page);
2353 ClearPageUptodate(page);
2359 check_page_uptodate(tree, page);
2361 ClearPageUptodate(page);
2364 check_page_locked(tree, page);
2366 } while (bvec <= bvec_end);
2372 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2377 bio = bio_alloc(gfp_flags, nr_vecs);
2379 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2380 while (!bio && (nr_vecs /= 2))
2381 bio = bio_alloc(gfp_flags, nr_vecs);
2386 bio->bi_bdev = bdev;
2387 bio->bi_sector = first_sector;
2392 static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
2393 unsigned long bio_flags)
2396 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2397 struct page *page = bvec->bv_page;
2398 struct extent_io_tree *tree = bio->bi_private;
2401 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
2403 bio->bi_private = NULL;
2407 if (tree->ops && tree->ops->submit_bio_hook)
2408 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2409 mirror_num, bio_flags, start);
2411 btrfsic_submit_bio(rw, bio);
2413 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2419 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2420 struct page *page, sector_t sector,
2421 size_t size, unsigned long offset,
2422 struct block_device *bdev,
2423 struct bio **bio_ret,
2424 unsigned long max_pages,
2425 bio_end_io_t end_io_func,
2427 unsigned long prev_bio_flags,
2428 unsigned long bio_flags)
2434 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2435 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2436 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2438 if (bio_ret && *bio_ret) {
2441 contig = bio->bi_sector == sector;
2443 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2446 if (prev_bio_flags != bio_flags || !contig ||
2447 (tree->ops && tree->ops->merge_bio_hook &&
2448 tree->ops->merge_bio_hook(page, offset, page_size, bio,
2450 bio_add_page(bio, page, page_size, offset) < page_size) {
2451 ret = submit_one_bio(rw, bio, mirror_num,
2458 if (this_compressed)
2461 nr = bio_get_nr_vecs(bdev);
2463 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2467 bio_add_page(bio, page, page_size, offset);
2468 bio->bi_end_io = end_io_func;
2469 bio->bi_private = tree;
2474 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2479 void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2481 if (!PagePrivate(page)) {
2482 SetPagePrivate(page);
2483 page_cache_get(page);
2484 set_page_private(page, (unsigned long)eb);
2486 WARN_ON(page->private != (unsigned long)eb);
2490 void set_page_extent_mapped(struct page *page)
2492 if (!PagePrivate(page)) {
2493 SetPagePrivate(page);
2494 page_cache_get(page);
2495 set_page_private(page, EXTENT_PAGE_PRIVATE);
2500 * basic readpage implementation. Locked extent state structs are inserted
2501 * into the tree that are removed when the IO is done (by the end_io
2504 static int __extent_read_full_page(struct extent_io_tree *tree,
2506 get_extent_t *get_extent,
2507 struct bio **bio, int mirror_num,
2508 unsigned long *bio_flags)
2510 struct inode *inode = page->mapping->host;
2511 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2512 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2516 u64 last_byte = i_size_read(inode);
2520 struct extent_map *em;
2521 struct block_device *bdev;
2522 struct btrfs_ordered_extent *ordered;
2525 size_t pg_offset = 0;
2527 size_t disk_io_size;
2528 size_t blocksize = inode->i_sb->s_blocksize;
2529 unsigned long this_bio_flag = 0;
2531 set_page_extent_mapped(page);
2533 if (!PageUptodate(page)) {
2534 if (cleancache_get_page(page) == 0) {
2535 BUG_ON(blocksize != PAGE_SIZE);
2542 lock_extent(tree, start, end, GFP_NOFS);
2543 ordered = btrfs_lookup_ordered_extent(inode, start);
2546 unlock_extent(tree, start, end, GFP_NOFS);
2547 btrfs_start_ordered_extent(inode, ordered, 1);
2548 btrfs_put_ordered_extent(ordered);
2551 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2553 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2556 iosize = PAGE_CACHE_SIZE - zero_offset;
2557 userpage = kmap_atomic(page, KM_USER0);
2558 memset(userpage + zero_offset, 0, iosize);
2559 flush_dcache_page(page);
2560 kunmap_atomic(userpage, KM_USER0);
2563 while (cur <= end) {
2564 if (cur >= last_byte) {
2566 struct extent_state *cached = NULL;
2568 iosize = PAGE_CACHE_SIZE - pg_offset;
2569 userpage = kmap_atomic(page, KM_USER0);
2570 memset(userpage + pg_offset, 0, iosize);
2571 flush_dcache_page(page);
2572 kunmap_atomic(userpage, KM_USER0);
2573 set_extent_uptodate(tree, cur, cur + iosize - 1,
2575 unlock_extent_cached(tree, cur, cur + iosize - 1,
2579 em = get_extent(inode, page, pg_offset, cur,
2581 if (IS_ERR_OR_NULL(em)) {
2583 unlock_extent(tree, cur, end, GFP_NOFS);
2586 extent_offset = cur - em->start;
2587 BUG_ON(extent_map_end(em) <= cur);
2590 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2591 this_bio_flag = EXTENT_BIO_COMPRESSED;
2592 extent_set_compress_type(&this_bio_flag,
2596 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2597 cur_end = min(extent_map_end(em) - 1, end);
2598 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2599 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2600 disk_io_size = em->block_len;
2601 sector = em->block_start >> 9;
2603 sector = (em->block_start + extent_offset) >> 9;
2604 disk_io_size = iosize;
2607 block_start = em->block_start;
2608 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2609 block_start = EXTENT_MAP_HOLE;
2610 free_extent_map(em);
2613 /* we've found a hole, just zero and go on */
2614 if (block_start == EXTENT_MAP_HOLE) {
2616 struct extent_state *cached = NULL;
2618 userpage = kmap_atomic(page, KM_USER0);
2619 memset(userpage + pg_offset, 0, iosize);
2620 flush_dcache_page(page);
2621 kunmap_atomic(userpage, KM_USER0);
2623 set_extent_uptodate(tree, cur, cur + iosize - 1,
2625 unlock_extent_cached(tree, cur, cur + iosize - 1,
2628 pg_offset += iosize;
2631 /* the get_extent function already copied into the page */
2632 if (test_range_bit(tree, cur, cur_end,
2633 EXTENT_UPTODATE, 1, NULL)) {
2634 check_page_uptodate(tree, page);
2635 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2637 pg_offset += iosize;
2640 /* we have an inline extent but it didn't get marked up
2641 * to date. Error out
2643 if (block_start == EXTENT_MAP_INLINE) {
2645 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2647 pg_offset += iosize;
2652 if (tree->ops && tree->ops->readpage_io_hook) {
2653 ret = tree->ops->readpage_io_hook(page, cur,
2657 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2659 ret = submit_extent_page(READ, tree, page,
2660 sector, disk_io_size, pg_offset,
2662 end_bio_extent_readpage, mirror_num,
2666 *bio_flags = this_bio_flag;
2671 pg_offset += iosize;
2675 if (!PageError(page))
2676 SetPageUptodate(page);
2682 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2683 get_extent_t *get_extent, int mirror_num)
2685 struct bio *bio = NULL;
2686 unsigned long bio_flags = 0;
2689 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2692 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2696 static noinline void update_nr_written(struct page *page,
2697 struct writeback_control *wbc,
2698 unsigned long nr_written)
2700 wbc->nr_to_write -= nr_written;
2701 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2702 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2703 page->mapping->writeback_index = page->index + nr_written;
2707 * the writepage semantics are similar to regular writepage. extent
2708 * records are inserted to lock ranges in the tree, and as dirty areas
2709 * are found, they are marked writeback. Then the lock bits are removed
2710 * and the end_io handler clears the writeback ranges
2712 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2715 struct inode *inode = page->mapping->host;
2716 struct extent_page_data *epd = data;
2717 struct extent_io_tree *tree = epd->tree;
2718 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2720 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2724 u64 last_byte = i_size_read(inode);
2728 struct extent_state *cached_state = NULL;
2729 struct extent_map *em;
2730 struct block_device *bdev;
2733 size_t pg_offset = 0;
2735 loff_t i_size = i_size_read(inode);
2736 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2742 unsigned long nr_written = 0;
2743 bool fill_delalloc = true;
2745 if (wbc->sync_mode == WB_SYNC_ALL)
2746 write_flags = WRITE_SYNC;
2748 write_flags = WRITE;
2750 trace___extent_writepage(page, inode, wbc);
2752 WARN_ON(!PageLocked(page));
2754 ClearPageError(page);
2756 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2757 if (page->index > end_index ||
2758 (page->index == end_index && !pg_offset)) {
2759 page->mapping->a_ops->invalidatepage(page, 0);
2764 if (page->index == end_index) {
2767 userpage = kmap_atomic(page, KM_USER0);
2768 memset(userpage + pg_offset, 0,
2769 PAGE_CACHE_SIZE - pg_offset);
2770 kunmap_atomic(userpage, KM_USER0);
2771 flush_dcache_page(page);
2775 set_page_extent_mapped(page);
2777 if (!tree->ops || !tree->ops->fill_delalloc)
2778 fill_delalloc = false;
2780 delalloc_start = start;
2783 if (!epd->extent_locked && fill_delalloc) {
2784 u64 delalloc_to_write = 0;
2786 * make sure the wbc mapping index is at least updated
2789 update_nr_written(page, wbc, 0);
2791 while (delalloc_end < page_end) {
2792 nr_delalloc = find_lock_delalloc_range(inode, tree,
2797 if (nr_delalloc == 0) {
2798 delalloc_start = delalloc_end + 1;
2801 ret = tree->ops->fill_delalloc(inode, page,
2808 * delalloc_end is already one less than the total
2809 * length, so we don't subtract one from
2812 delalloc_to_write += (delalloc_end - delalloc_start +
2815 delalloc_start = delalloc_end + 1;
2817 if (wbc->nr_to_write < delalloc_to_write) {
2820 if (delalloc_to_write < thresh * 2)
2821 thresh = delalloc_to_write;
2822 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2826 /* did the fill delalloc function already unlock and start
2832 * we've unlocked the page, so we can't update
2833 * the mapping's writeback index, just update
2836 wbc->nr_to_write -= nr_written;
2840 if (tree->ops && tree->ops->writepage_start_hook) {
2841 ret = tree->ops->writepage_start_hook(page, start,
2844 /* Fixup worker will requeue */
2846 wbc->pages_skipped++;
2848 redirty_page_for_writepage(wbc, page);
2849 update_nr_written(page, wbc, nr_written);
2857 * we don't want to touch the inode after unlocking the page,
2858 * so we update the mapping writeback index now
2860 update_nr_written(page, wbc, nr_written + 1);
2863 if (last_byte <= start) {
2864 if (tree->ops && tree->ops->writepage_end_io_hook)
2865 tree->ops->writepage_end_io_hook(page, start,
2870 blocksize = inode->i_sb->s_blocksize;
2872 while (cur <= end) {
2873 if (cur >= last_byte) {
2874 if (tree->ops && tree->ops->writepage_end_io_hook)
2875 tree->ops->writepage_end_io_hook(page, cur,
2879 em = epd->get_extent(inode, page, pg_offset, cur,
2881 if (IS_ERR_OR_NULL(em)) {
2886 extent_offset = cur - em->start;
2887 BUG_ON(extent_map_end(em) <= cur);
2889 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2890 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2891 sector = (em->block_start + extent_offset) >> 9;
2893 block_start = em->block_start;
2894 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2895 free_extent_map(em);
2899 * compressed and inline extents are written through other
2902 if (compressed || block_start == EXTENT_MAP_HOLE ||
2903 block_start == EXTENT_MAP_INLINE) {
2905 * end_io notification does not happen here for
2906 * compressed extents
2908 if (!compressed && tree->ops &&
2909 tree->ops->writepage_end_io_hook)
2910 tree->ops->writepage_end_io_hook(page, cur,
2913 else if (compressed) {
2914 /* we don't want to end_page_writeback on
2915 * a compressed extent. this happens
2922 pg_offset += iosize;
2925 /* leave this out until we have a page_mkwrite call */
2926 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2927 EXTENT_DIRTY, 0, NULL)) {
2929 pg_offset += iosize;
2933 if (tree->ops && tree->ops->writepage_io_hook) {
2934 ret = tree->ops->writepage_io_hook(page, cur,
2942 unsigned long max_nr = end_index + 1;
2944 set_range_writeback(tree, cur, cur + iosize - 1);
2945 if (!PageWriteback(page)) {
2946 printk(KERN_ERR "btrfs warning page %lu not "
2947 "writeback, cur %llu end %llu\n",
2948 page->index, (unsigned long long)cur,
2949 (unsigned long long)end);
2952 ret = submit_extent_page(write_flags, tree, page,
2953 sector, iosize, pg_offset,
2954 bdev, &epd->bio, max_nr,
2955 end_bio_extent_writepage,
2961 pg_offset += iosize;
2966 /* make sure the mapping tag for page dirty gets cleared */
2967 set_page_writeback(page);
2968 end_page_writeback(page);
2974 /* drop our reference on any cached states */
2975 free_extent_state(cached_state);
2979 static int eb_wait(void *word)
2985 static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
2987 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
2988 TASK_UNINTERRUPTIBLE);
2991 static int lock_extent_buffer_for_io(struct extent_buffer *eb,
2992 struct btrfs_fs_info *fs_info,
2993 struct extent_page_data *epd)
2995 unsigned long i, num_pages;
2999 if (!btrfs_try_tree_write_lock(eb)) {
3001 flush_write_bio(epd);
3002 btrfs_tree_lock(eb);
3005 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3006 btrfs_tree_unlock(eb);
3010 flush_write_bio(epd);
3013 wait_on_extent_buffer_writeback(eb);
3014 btrfs_tree_lock(eb);
3015 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3016 printk(KERN_ERR "Um, ok?\n");
3017 btrfs_tree_unlock(eb);
3022 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3023 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3024 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3025 spin_lock(&fs_info->delalloc_lock);
3026 if (fs_info->dirty_metadata_bytes >= eb->len)
3027 fs_info->dirty_metadata_bytes -= eb->len;
3030 spin_unlock(&fs_info->delalloc_lock);
3034 btrfs_tree_unlock(eb);
3039 num_pages = num_extent_pages(eb->start, eb->len);
3040 for (i = 0; i < num_pages; i++) {
3041 struct page *p = extent_buffer_page(eb, i);
3043 if (!trylock_page(p)) {
3045 flush_write_bio(epd);
3055 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3057 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3058 smp_mb__after_clear_bit();
3059 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3062 static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3064 int uptodate = err == 0;
3065 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3066 struct extent_buffer *eb;
3070 struct page *page = bvec->bv_page;
3073 eb = (struct extent_buffer *)page->private;
3075 done = atomic_dec_and_test(&eb->io_pages);
3077 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3078 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3079 ClearPageUptodate(page);
3083 end_page_writeback(page);
3088 end_extent_buffer_writeback(eb);
3089 } while (bvec >= bio->bi_io_vec);
3095 static int write_one_eb(struct extent_buffer *eb,
3096 struct btrfs_fs_info *fs_info,
3097 struct writeback_control *wbc,
3098 struct extent_page_data *epd)
3100 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3101 u64 offset = eb->start;
3102 unsigned long i, num_pages;
3103 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3106 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3107 num_pages = num_extent_pages(eb->start, eb->len);
3108 atomic_set(&eb->io_pages, num_pages);
3109 for (i = 0; i < num_pages; i++) {
3110 struct page *p = extent_buffer_page(eb, i);
3112 clear_page_dirty_for_io(p);
3113 set_page_writeback(p);
3114 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3115 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3116 -1, end_bio_extent_buffer_writepage,
3119 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3121 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3122 end_extent_buffer_writeback(eb);
3126 offset += PAGE_CACHE_SIZE;
3127 update_nr_written(p, wbc, 1);
3131 if (unlikely(ret)) {
3132 for (; i < num_pages; i++) {
3133 struct page *p = extent_buffer_page(eb, i);
3141 int btree_write_cache_pages(struct address_space *mapping,
3142 struct writeback_control *wbc)
3144 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3145 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3146 struct extent_buffer *eb, *prev_eb = NULL;
3147 struct extent_page_data epd = {
3151 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3155 int nr_to_write_done = 0;
3156 struct pagevec pvec;
3159 pgoff_t end; /* Inclusive */
3163 pagevec_init(&pvec, 0);
3164 if (wbc->range_cyclic) {
3165 index = mapping->writeback_index; /* Start from prev offset */
3168 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3169 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3172 if (wbc->sync_mode == WB_SYNC_ALL)
3173 tag = PAGECACHE_TAG_TOWRITE;
3175 tag = PAGECACHE_TAG_DIRTY;
3177 if (wbc->sync_mode == WB_SYNC_ALL)
3178 tag_pages_for_writeback(mapping, index, end);
3179 while (!done && !nr_to_write_done && (index <= end) &&
3180 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3181 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3185 for (i = 0; i < nr_pages; i++) {
3186 struct page *page = pvec.pages[i];
3188 if (!PagePrivate(page))
3191 if (!wbc->range_cyclic && page->index > end) {
3196 eb = (struct extent_buffer *)page->private;
3205 if (!atomic_inc_not_zero(&eb->refs)) {
3211 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3213 free_extent_buffer(eb);
3217 ret = write_one_eb(eb, fs_info, wbc, &epd);
3220 free_extent_buffer(eb);
3223 free_extent_buffer(eb);
3226 * the filesystem may choose to bump up nr_to_write.
3227 * We have to make sure to honor the new nr_to_write
3230 nr_to_write_done = wbc->nr_to_write <= 0;
3232 pagevec_release(&pvec);
3235 if (!scanned && !done) {
3237 * We hit the last page and there is more work to be done: wrap
3238 * back to the start of the file
3244 flush_write_bio(&epd);
3249 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3250 * @mapping: address space structure to write
3251 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3252 * @writepage: function called for each page
3253 * @data: data passed to writepage function
3255 * If a page is already under I/O, write_cache_pages() skips it, even
3256 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3257 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3258 * and msync() need to guarantee that all the data which was dirty at the time
3259 * the call was made get new I/O started against them. If wbc->sync_mode is
3260 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3261 * existing IO to complete.
3263 static int extent_write_cache_pages(struct extent_io_tree *tree,
3264 struct address_space *mapping,
3265 struct writeback_control *wbc,
3266 writepage_t writepage, void *data,
3267 void (*flush_fn)(void *))
3271 int nr_to_write_done = 0;
3272 struct pagevec pvec;
3275 pgoff_t end; /* Inclusive */
3279 pagevec_init(&pvec, 0);
3280 if (wbc->range_cyclic) {
3281 index = mapping->writeback_index; /* Start from prev offset */
3284 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3285 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3288 if (wbc->sync_mode == WB_SYNC_ALL)
3289 tag = PAGECACHE_TAG_TOWRITE;
3291 tag = PAGECACHE_TAG_DIRTY;
3293 if (wbc->sync_mode == WB_SYNC_ALL)
3294 tag_pages_for_writeback(mapping, index, end);
3295 while (!done && !nr_to_write_done && (index <= end) &&
3296 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3297 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3301 for (i = 0; i < nr_pages; i++) {
3302 struct page *page = pvec.pages[i];
3305 * At this point we hold neither mapping->tree_lock nor
3306 * lock on the page itself: the page may be truncated or
3307 * invalidated (changing page->mapping to NULL), or even
3308 * swizzled back from swapper_space to tmpfs file
3312 tree->ops->write_cache_pages_lock_hook) {
3313 tree->ops->write_cache_pages_lock_hook(page,
3316 if (!trylock_page(page)) {
3322 if (unlikely(page->mapping != mapping)) {
3327 if (!wbc->range_cyclic && page->index > end) {
3333 if (wbc->sync_mode != WB_SYNC_NONE) {
3334 if (PageWriteback(page))
3336 wait_on_page_writeback(page);
3339 if (PageWriteback(page) ||
3340 !clear_page_dirty_for_io(page)) {
3345 ret = (*writepage)(page, wbc, data);
3347 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3355 * the filesystem may choose to bump up nr_to_write.
3356 * We have to make sure to honor the new nr_to_write
3359 nr_to_write_done = wbc->nr_to_write <= 0;
3361 pagevec_release(&pvec);
3364 if (!scanned && !done) {
3366 * We hit the last page and there is more work to be done: wrap
3367 * back to the start of the file
3376 static void flush_epd_write_bio(struct extent_page_data *epd)
3380 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
3382 submit_one_bio(WRITE, epd->bio, 0, 0);
3387 static noinline void flush_write_bio(void *data)
3389 struct extent_page_data *epd = data;
3390 flush_epd_write_bio(epd);
3393 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3394 get_extent_t *get_extent,
3395 struct writeback_control *wbc)
3398 struct extent_page_data epd = {
3401 .get_extent = get_extent,
3403 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3406 ret = __extent_writepage(page, wbc, &epd);
3408 flush_epd_write_bio(&epd);
3412 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3413 u64 start, u64 end, get_extent_t *get_extent,
3417 struct address_space *mapping = inode->i_mapping;
3419 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3422 struct extent_page_data epd = {
3425 .get_extent = get_extent,
3427 .sync_io = mode == WB_SYNC_ALL,
3429 struct writeback_control wbc_writepages = {
3431 .nr_to_write = nr_pages * 2,
3432 .range_start = start,
3433 .range_end = end + 1,
3436 while (start <= end) {
3437 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3438 if (clear_page_dirty_for_io(page))
3439 ret = __extent_writepage(page, &wbc_writepages, &epd);
3441 if (tree->ops && tree->ops->writepage_end_io_hook)
3442 tree->ops->writepage_end_io_hook(page, start,
3443 start + PAGE_CACHE_SIZE - 1,
3447 page_cache_release(page);
3448 start += PAGE_CACHE_SIZE;
3451 flush_epd_write_bio(&epd);
3455 int extent_writepages(struct extent_io_tree *tree,
3456 struct address_space *mapping,
3457 get_extent_t *get_extent,
3458 struct writeback_control *wbc)
3461 struct extent_page_data epd = {
3464 .get_extent = get_extent,
3466 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3469 ret = extent_write_cache_pages(tree, mapping, wbc,
3470 __extent_writepage, &epd,
3472 flush_epd_write_bio(&epd);
3476 int extent_readpages(struct extent_io_tree *tree,
3477 struct address_space *mapping,
3478 struct list_head *pages, unsigned nr_pages,
3479 get_extent_t get_extent)
3481 struct bio *bio = NULL;
3483 unsigned long bio_flags = 0;
3485 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3486 struct page *page = list_entry(pages->prev, struct page, lru);
3488 prefetchw(&page->flags);
3489 list_del(&page->lru);
3490 if (!add_to_page_cache_lru(page, mapping,
3491 page->index, GFP_NOFS)) {
3492 __extent_read_full_page(tree, page, get_extent,
3493 &bio, 0, &bio_flags);
3495 page_cache_release(page);
3497 BUG_ON(!list_empty(pages));
3499 submit_one_bio(READ, bio, 0, bio_flags);
3504 * basic invalidatepage code, this waits on any locked or writeback
3505 * ranges corresponding to the page, and then deletes any extent state
3506 * records from the tree
3508 int extent_invalidatepage(struct extent_io_tree *tree,
3509 struct page *page, unsigned long offset)
3511 struct extent_state *cached_state = NULL;
3512 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3513 u64 end = start + PAGE_CACHE_SIZE - 1;
3514 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3516 start += (offset + blocksize - 1) & ~(blocksize - 1);
3520 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
3521 wait_on_page_writeback(page);
3522 clear_extent_bit(tree, start, end,
3523 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3524 EXTENT_DO_ACCOUNTING,
3525 1, 1, &cached_state, GFP_NOFS);
3530 * a helper for releasepage, this tests for areas of the page that
3531 * are locked or under IO and drops the related state bits if it is safe
3534 int try_release_extent_state(struct extent_map_tree *map,
3535 struct extent_io_tree *tree, struct page *page,
3538 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3539 u64 end = start + PAGE_CACHE_SIZE - 1;
3542 if (test_range_bit(tree, start, end,
3543 EXTENT_IOBITS, 0, NULL))
3546 if ((mask & GFP_NOFS) == GFP_NOFS)
3549 * at this point we can safely clear everything except the
3550 * locked bit and the nodatasum bit
3552 ret = clear_extent_bit(tree, start, end,
3553 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3556 /* if clear_extent_bit failed for enomem reasons,
3557 * we can't allow the release to continue.
3568 * a helper for releasepage. As long as there are no locked extents
3569 * in the range corresponding to the page, both state records and extent
3570 * map records are removed
3572 int try_release_extent_mapping(struct extent_map_tree *map,
3573 struct extent_io_tree *tree, struct page *page,
3576 struct extent_map *em;
3577 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3578 u64 end = start + PAGE_CACHE_SIZE - 1;
3580 if ((mask & __GFP_WAIT) &&
3581 page->mapping->host->i_size > 16 * 1024 * 1024) {
3583 while (start <= end) {
3584 len = end - start + 1;
3585 write_lock(&map->lock);
3586 em = lookup_extent_mapping(map, start, len);
3588 write_unlock(&map->lock);
3591 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3592 em->start != start) {
3593 write_unlock(&map->lock);
3594 free_extent_map(em);
3597 if (!test_range_bit(tree, em->start,
3598 extent_map_end(em) - 1,
3599 EXTENT_LOCKED | EXTENT_WRITEBACK,
3601 remove_extent_mapping(map, em);
3602 /* once for the rb tree */
3603 free_extent_map(em);
3605 start = extent_map_end(em);
3606 write_unlock(&map->lock);
3609 free_extent_map(em);
3612 return try_release_extent_state(map, tree, page, mask);
3616 * helper function for fiemap, which doesn't want to see any holes.
3617 * This maps until we find something past 'last'
3619 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3622 get_extent_t *get_extent)
3624 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3625 struct extent_map *em;
3632 len = last - offset;
3635 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3636 em = get_extent(inode, NULL, 0, offset, len, 0);
3637 if (IS_ERR_OR_NULL(em))
3640 /* if this isn't a hole return it */
3641 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3642 em->block_start != EXTENT_MAP_HOLE) {
3646 /* this is a hole, advance to the next extent */
3647 offset = extent_map_end(em);
3648 free_extent_map(em);
3655 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3656 __u64 start, __u64 len, get_extent_t *get_extent)
3660 u64 max = start + len;
3664 u64 last_for_get_extent = 0;
3666 u64 isize = i_size_read(inode);
3667 struct btrfs_key found_key;
3668 struct extent_map *em = NULL;
3669 struct extent_state *cached_state = NULL;
3670 struct btrfs_path *path;
3671 struct btrfs_file_extent_item *item;
3676 unsigned long emflags;
3681 path = btrfs_alloc_path();
3684 path->leave_spinning = 1;
3686 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3687 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3690 * lookup the last file extent. We're not using i_size here
3691 * because there might be preallocation past i_size
3693 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3694 path, btrfs_ino(inode), -1, 0);
3696 btrfs_free_path(path);
3701 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3702 struct btrfs_file_extent_item);
3703 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3704 found_type = btrfs_key_type(&found_key);
3706 /* No extents, but there might be delalloc bits */
3707 if (found_key.objectid != btrfs_ino(inode) ||
3708 found_type != BTRFS_EXTENT_DATA_KEY) {
3709 /* have to trust i_size as the end */
3711 last_for_get_extent = isize;
3714 * remember the start of the last extent. There are a
3715 * bunch of different factors that go into the length of the
3716 * extent, so its much less complex to remember where it started
3718 last = found_key.offset;
3719 last_for_get_extent = last + 1;
3721 btrfs_free_path(path);
3724 * we might have some extents allocated but more delalloc past those
3725 * extents. so, we trust isize unless the start of the last extent is
3730 last_for_get_extent = isize;
3733 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3734 &cached_state, GFP_NOFS);
3736 em = get_extent_skip_holes(inode, start, last_for_get_extent,
3746 u64 offset_in_extent;
3748 /* break if the extent we found is outside the range */
3749 if (em->start >= max || extent_map_end(em) < off)
3753 * get_extent may return an extent that starts before our
3754 * requested range. We have to make sure the ranges
3755 * we return to fiemap always move forward and don't
3756 * overlap, so adjust the offsets here
3758 em_start = max(em->start, off);
3761 * record the offset from the start of the extent
3762 * for adjusting the disk offset below
3764 offset_in_extent = em_start - em->start;
3765 em_end = extent_map_end(em);
3766 em_len = em_end - em_start;
3767 emflags = em->flags;
3772 * bump off for our next call to get_extent
3774 off = extent_map_end(em);
3778 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
3780 flags |= FIEMAP_EXTENT_LAST;
3781 } else if (em->block_start == EXTENT_MAP_INLINE) {
3782 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3783 FIEMAP_EXTENT_NOT_ALIGNED);
3784 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
3785 flags |= (FIEMAP_EXTENT_DELALLOC |
3786 FIEMAP_EXTENT_UNKNOWN);
3788 disko = em->block_start + offset_in_extent;
3790 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3791 flags |= FIEMAP_EXTENT_ENCODED;
3793 free_extent_map(em);
3795 if ((em_start >= last) || em_len == (u64)-1 ||
3796 (last == (u64)-1 && isize <= em_end)) {
3797 flags |= FIEMAP_EXTENT_LAST;
3801 /* now scan forward to see if this is really the last extent. */
3802 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3809 flags |= FIEMAP_EXTENT_LAST;
3812 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3818 free_extent_map(em);
3820 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3821 &cached_state, GFP_NOFS);
3825 inline struct page *extent_buffer_page(struct extent_buffer *eb,
3828 return eb->pages[i];
3831 inline unsigned long num_extent_pages(u64 start, u64 len)
3833 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3834 (start >> PAGE_CACHE_SHIFT);
3837 static void __free_extent_buffer(struct extent_buffer *eb)
3840 unsigned long flags;
3841 spin_lock_irqsave(&leak_lock, flags);
3842 list_del(&eb->leak_list);
3843 spin_unlock_irqrestore(&leak_lock, flags);
3845 if (eb->pages && eb->pages != eb->inline_pages)
3847 kmem_cache_free(extent_buffer_cache, eb);
3850 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3855 struct extent_buffer *eb = NULL;
3857 unsigned long flags;
3860 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
3866 rwlock_init(&eb->lock);
3867 atomic_set(&eb->write_locks, 0);
3868 atomic_set(&eb->read_locks, 0);
3869 atomic_set(&eb->blocking_readers, 0);
3870 atomic_set(&eb->blocking_writers, 0);
3871 atomic_set(&eb->spinning_readers, 0);
3872 atomic_set(&eb->spinning_writers, 0);
3873 eb->lock_nested = 0;
3874 init_waitqueue_head(&eb->write_lock_wq);
3875 init_waitqueue_head(&eb->read_lock_wq);
3878 spin_lock_irqsave(&leak_lock, flags);
3879 list_add(&eb->leak_list, &buffers);
3880 spin_unlock_irqrestore(&leak_lock, flags);
3882 spin_lock_init(&eb->refs_lock);
3883 atomic_set(&eb->refs, 1);
3884 atomic_set(&eb->io_pages, 0);
3886 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3887 struct page **pages;
3888 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3890 pages = kzalloc(num_pages, mask);
3892 __free_extent_buffer(eb);
3897 eb->pages = eb->inline_pages;
3903 static int extent_buffer_under_io(struct extent_buffer *eb)
3905 return (atomic_read(&eb->io_pages) ||
3906 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
3907 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3911 * Helper for releasing extent buffer page.
3913 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3914 unsigned long start_idx)
3916 unsigned long index;
3919 BUG_ON(extent_buffer_under_io(eb));
3921 index = num_extent_pages(eb->start, eb->len);
3922 if (start_idx >= index)
3927 page = extent_buffer_page(eb, index);
3929 spin_lock(&page->mapping->private_lock);
3931 * We do this since we'll remove the pages after we've
3932 * removed the eb from the radix tree, so we could race
3933 * and have this page now attached to the new eb. So
3934 * only clear page_private if it's still connected to
3937 if (PagePrivate(page) &&
3938 page->private == (unsigned long)eb) {
3939 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3940 BUG_ON(PageDirty(page));
3941 BUG_ON(PageWriteback(page));
3943 * We need to make sure we haven't be attached
3946 ClearPagePrivate(page);
3947 set_page_private(page, 0);
3948 /* One for the page private */
3949 page_cache_release(page);
3951 spin_unlock(&page->mapping->private_lock);
3953 /* One for when we alloced the page */
3954 page_cache_release(page);
3956 } while (index != start_idx);
3960 * Helper for releasing the extent buffer.
3962 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3964 btrfs_release_extent_buffer_page(eb, 0);
3965 __free_extent_buffer(eb);
3968 static void check_buffer_tree_ref(struct extent_buffer *eb)
3970 /* the ref bit is tricky. We have to make sure it is set
3971 * if we have the buffer dirty. Otherwise the
3972 * code to free a buffer can end up dropping a dirty
3975 * Once the ref bit is set, it won't go away while the
3976 * buffer is dirty or in writeback, and it also won't
3977 * go away while we have the reference count on the
3980 * We can't just set the ref bit without bumping the
3981 * ref on the eb because free_extent_buffer might
3982 * see the ref bit and try to clear it. If this happens
3983 * free_extent_buffer might end up dropping our original
3984 * ref by mistake and freeing the page before we are able
3985 * to add one more ref.
3987 * So bump the ref count first, then set the bit. If someone
3988 * beat us to it, drop the ref we added.
3990 if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
3991 atomic_inc(&eb->refs);
3992 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
3993 atomic_dec(&eb->refs);
3997 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
3999 unsigned long num_pages, i;
4001 check_buffer_tree_ref(eb);
4003 num_pages = num_extent_pages(eb->start, eb->len);
4004 for (i = 0; i < num_pages; i++) {
4005 struct page *p = extent_buffer_page(eb, i);
4006 mark_page_accessed(p);
4010 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
4011 u64 start, unsigned long len)
4013 unsigned long num_pages = num_extent_pages(start, len);
4015 unsigned long index = start >> PAGE_CACHE_SHIFT;
4016 struct extent_buffer *eb;
4017 struct extent_buffer *exists = NULL;
4019 struct address_space *mapping = tree->mapping;
4024 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4025 if (eb && atomic_inc_not_zero(&eb->refs)) {
4027 mark_extent_buffer_accessed(eb);
4032 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
4036 for (i = 0; i < num_pages; i++, index++) {
4037 p = find_or_create_page(mapping, index, GFP_NOFS);
4043 spin_lock(&mapping->private_lock);
4044 if (PagePrivate(p)) {
4046 * We could have already allocated an eb for this page
4047 * and attached one so lets see if we can get a ref on
4048 * the existing eb, and if we can we know it's good and
4049 * we can just return that one, else we know we can just
4050 * overwrite page->private.
4052 exists = (struct extent_buffer *)p->private;
4053 if (atomic_inc_not_zero(&exists->refs)) {
4054 spin_unlock(&mapping->private_lock);
4056 mark_extent_buffer_accessed(exists);
4061 * Do this so attach doesn't complain and we need to
4062 * drop the ref the old guy had.
4064 ClearPagePrivate(p);
4065 WARN_ON(PageDirty(p));
4066 page_cache_release(p);
4068 attach_extent_buffer_page(eb, p);
4069 spin_unlock(&mapping->private_lock);
4070 WARN_ON(PageDirty(p));
4071 mark_page_accessed(p);
4073 if (!PageUptodate(p))
4077 * see below about how we avoid a nasty race with release page
4078 * and why we unlock later
4082 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4084 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4088 spin_lock(&tree->buffer_lock);
4089 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4090 if (ret == -EEXIST) {
4091 exists = radix_tree_lookup(&tree->buffer,
4092 start >> PAGE_CACHE_SHIFT);
4093 if (!atomic_inc_not_zero(&exists->refs)) {
4094 spin_unlock(&tree->buffer_lock);
4095 radix_tree_preload_end();
4099 spin_unlock(&tree->buffer_lock);
4100 radix_tree_preload_end();
4101 mark_extent_buffer_accessed(exists);
4104 /* add one reference for the tree */
4105 spin_lock(&eb->refs_lock);
4106 check_buffer_tree_ref(eb);
4107 spin_unlock(&eb->refs_lock);
4108 spin_unlock(&tree->buffer_lock);
4109 radix_tree_preload_end();
4112 * there is a race where release page may have
4113 * tried to find this extent buffer in the radix
4114 * but failed. It will tell the VM it is safe to
4115 * reclaim the, and it will clear the page private bit.
4116 * We must make sure to set the page private bit properly
4117 * after the extent buffer is in the radix tree so
4118 * it doesn't get lost
4120 SetPageChecked(eb->pages[0]);
4121 for (i = 1; i < num_pages; i++) {
4122 p = extent_buffer_page(eb, i);
4123 ClearPageChecked(p);
4126 unlock_page(eb->pages[0]);
4130 for (i = 0; i < num_pages; i++) {
4132 unlock_page(eb->pages[i]);
4135 if (!atomic_dec_and_test(&eb->refs))
4137 btrfs_release_extent_buffer(eb);
4141 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
4142 u64 start, unsigned long len)
4144 struct extent_buffer *eb;
4147 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4148 if (eb && atomic_inc_not_zero(&eb->refs)) {
4150 mark_extent_buffer_accessed(eb);
4158 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4160 struct extent_buffer *eb =
4161 container_of(head, struct extent_buffer, rcu_head);
4163 __free_extent_buffer(eb);
4166 /* Expects to have eb->eb_lock already held */
4167 static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4169 WARN_ON(atomic_read(&eb->refs) == 0);
4170 if (atomic_dec_and_test(&eb->refs)) {
4171 struct extent_io_tree *tree = eb->tree;
4173 spin_unlock(&eb->refs_lock);
4175 spin_lock(&tree->buffer_lock);
4176 radix_tree_delete(&tree->buffer,
4177 eb->start >> PAGE_CACHE_SHIFT);
4178 spin_unlock(&tree->buffer_lock);
4180 /* Should be safe to release our pages at this point */
4181 btrfs_release_extent_buffer_page(eb, 0);
4183 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4186 spin_unlock(&eb->refs_lock);
4189 void free_extent_buffer(struct extent_buffer *eb)
4194 spin_lock(&eb->refs_lock);
4195 if (atomic_read(&eb->refs) == 2 &&
4196 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
4197 !extent_buffer_under_io(eb) &&
4198 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4199 atomic_dec(&eb->refs);
4202 * I know this is terrible, but it's temporary until we stop tracking
4203 * the uptodate bits and such for the extent buffers.
4205 release_extent_buffer(eb, GFP_ATOMIC);
4208 void free_extent_buffer_stale(struct extent_buffer *eb)
4213 spin_lock(&eb->refs_lock);
4214 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4216 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
4217 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4218 atomic_dec(&eb->refs);
4219 release_extent_buffer(eb, GFP_NOFS);
4222 int clear_extent_buffer_dirty(struct extent_buffer *eb)
4225 unsigned long num_pages;
4228 num_pages = num_extent_pages(eb->start, eb->len);
4229 WARN_ON(atomic_read(&eb->refs) == 0);
4231 for (i = 0; i < num_pages; i++) {
4232 page = extent_buffer_page(eb, i);
4233 if (!PageDirty(page))
4237 WARN_ON(!PagePrivate(page));
4239 clear_page_dirty_for_io(page);
4240 spin_lock_irq(&page->mapping->tree_lock);
4241 if (!PageDirty(page)) {
4242 radix_tree_tag_clear(&page->mapping->page_tree,
4244 PAGECACHE_TAG_DIRTY);
4246 spin_unlock_irq(&page->mapping->tree_lock);
4247 ClearPageError(page);
4250 WARN_ON(atomic_read(&eb->refs) == 0);
4254 int set_extent_buffer_dirty(struct extent_buffer *eb)
4257 unsigned long num_pages;
4260 check_buffer_tree_ref(eb);
4262 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4264 num_pages = num_extent_pages(eb->start, eb->len);
4265 WARN_ON(atomic_read(&eb->refs) == 0);
4266 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4268 for (i = 0; i < num_pages; i++)
4269 set_page_dirty(extent_buffer_page(eb, i));
4273 static int range_straddles_pages(u64 start, u64 len)
4275 if (len < PAGE_CACHE_SIZE)
4277 if (start & (PAGE_CACHE_SIZE - 1))
4279 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4284 int clear_extent_buffer_uptodate(struct extent_buffer *eb)
4288 unsigned long num_pages;
4290 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4291 num_pages = num_extent_pages(eb->start, eb->len);
4292 for (i = 0; i < num_pages; i++) {
4293 page = extent_buffer_page(eb, i);
4295 ClearPageUptodate(page);
4300 int set_extent_buffer_uptodate(struct extent_buffer *eb)
4304 unsigned long num_pages;
4306 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4307 num_pages = num_extent_pages(eb->start, eb->len);
4308 for (i = 0; i < num_pages; i++) {
4309 page = extent_buffer_page(eb, i);
4310 SetPageUptodate(page);
4315 int extent_range_uptodate(struct extent_io_tree *tree,
4320 int pg_uptodate = 1;
4322 unsigned long index;
4324 if (range_straddles_pages(start, end - start + 1)) {
4325 ret = test_range_bit(tree, start, end,
4326 EXTENT_UPTODATE, 1, NULL);
4330 while (start <= end) {
4331 index = start >> PAGE_CACHE_SHIFT;
4332 page = find_get_page(tree->mapping, index);
4335 uptodate = PageUptodate(page);
4336 page_cache_release(page);
4341 start += PAGE_CACHE_SIZE;
4346 int extent_buffer_uptodate(struct extent_buffer *eb)
4348 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4351 int read_extent_buffer_pages(struct extent_io_tree *tree,
4352 struct extent_buffer *eb, u64 start, int wait,
4353 get_extent_t *get_extent, int mirror_num)
4356 unsigned long start_i;
4360 int locked_pages = 0;
4361 int all_uptodate = 1;
4362 unsigned long num_pages;
4363 unsigned long num_reads = 0;
4364 struct bio *bio = NULL;
4365 unsigned long bio_flags = 0;
4367 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4371 WARN_ON(start < eb->start);
4372 start_i = (start >> PAGE_CACHE_SHIFT) -
4373 (eb->start >> PAGE_CACHE_SHIFT);
4378 num_pages = num_extent_pages(eb->start, eb->len);
4379 for (i = start_i; i < num_pages; i++) {
4380 page = extent_buffer_page(eb, i);
4381 if (wait == WAIT_NONE) {
4382 if (!trylock_page(page))
4388 if (!PageUptodate(page)) {
4395 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4399 atomic_set(&eb->io_pages, num_reads);
4400 for (i = start_i; i < num_pages; i++) {
4401 page = extent_buffer_page(eb, i);
4402 if (!PageUptodate(page)) {
4403 ClearPageError(page);
4404 err = __extent_read_full_page(tree, page,
4406 mirror_num, &bio_flags);
4415 submit_one_bio(READ, bio, mirror_num, bio_flags);
4417 if (ret || wait != WAIT_COMPLETE)
4420 for (i = start_i; i < num_pages; i++) {
4421 page = extent_buffer_page(eb, i);
4422 wait_on_page_locked(page);
4423 if (!PageUptodate(page))
4431 while (locked_pages > 0) {
4432 page = extent_buffer_page(eb, i);
4440 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4441 unsigned long start,
4448 char *dst = (char *)dstv;
4449 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4450 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4452 WARN_ON(start > eb->len);
4453 WARN_ON(start + len > eb->start + eb->len);
4455 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4458 page = extent_buffer_page(eb, i);
4460 cur = min(len, (PAGE_CACHE_SIZE - offset));
4461 kaddr = page_address(page);
4462 memcpy(dst, kaddr + offset, cur);
4471 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4472 unsigned long min_len, char **map,
4473 unsigned long *map_start,
4474 unsigned long *map_len)
4476 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4479 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4480 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4481 unsigned long end_i = (start_offset + start + min_len - 1) >>
4488 offset = start_offset;
4492 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4495 if (start + min_len > eb->len) {
4496 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4497 "wanted %lu %lu\n", (unsigned long long)eb->start,
4498 eb->len, start, min_len);
4503 p = extent_buffer_page(eb, i);
4504 kaddr = page_address(p);
4505 *map = kaddr + offset;
4506 *map_len = PAGE_CACHE_SIZE - offset;
4510 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4511 unsigned long start,
4518 char *ptr = (char *)ptrv;
4519 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4520 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4523 WARN_ON(start > eb->len);
4524 WARN_ON(start + len > eb->start + eb->len);
4526 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4529 page = extent_buffer_page(eb, i);
4531 cur = min(len, (PAGE_CACHE_SIZE - offset));
4533 kaddr = page_address(page);
4534 ret = memcmp(ptr, kaddr + offset, cur);
4546 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4547 unsigned long start, unsigned long len)
4553 char *src = (char *)srcv;
4554 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4555 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4557 WARN_ON(start > eb->len);
4558 WARN_ON(start + len > eb->start + eb->len);
4560 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4563 page = extent_buffer_page(eb, i);
4564 WARN_ON(!PageUptodate(page));
4566 cur = min(len, PAGE_CACHE_SIZE - offset);
4567 kaddr = page_address(page);
4568 memcpy(kaddr + offset, src, cur);
4577 void memset_extent_buffer(struct extent_buffer *eb, char c,
4578 unsigned long start, unsigned long len)
4584 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4585 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4587 WARN_ON(start > eb->len);
4588 WARN_ON(start + len > eb->start + eb->len);
4590 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4593 page = extent_buffer_page(eb, i);
4594 WARN_ON(!PageUptodate(page));
4596 cur = min(len, PAGE_CACHE_SIZE - offset);
4597 kaddr = page_address(page);
4598 memset(kaddr + offset, c, cur);
4606 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4607 unsigned long dst_offset, unsigned long src_offset,
4610 u64 dst_len = dst->len;
4615 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4616 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4618 WARN_ON(src->len != dst_len);
4620 offset = (start_offset + dst_offset) &
4621 ((unsigned long)PAGE_CACHE_SIZE - 1);
4624 page = extent_buffer_page(dst, i);
4625 WARN_ON(!PageUptodate(page));
4627 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4629 kaddr = page_address(page);
4630 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4639 static void move_pages(struct page *dst_page, struct page *src_page,
4640 unsigned long dst_off, unsigned long src_off,
4643 char *dst_kaddr = page_address(dst_page);
4644 if (dst_page == src_page) {
4645 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4647 char *src_kaddr = page_address(src_page);
4648 char *p = dst_kaddr + dst_off + len;
4649 char *s = src_kaddr + src_off + len;
4656 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4658 unsigned long distance = (src > dst) ? src - dst : dst - src;
4659 return distance < len;
4662 static void copy_pages(struct page *dst_page, struct page *src_page,
4663 unsigned long dst_off, unsigned long src_off,
4666 char *dst_kaddr = page_address(dst_page);
4668 int must_memmove = 0;
4670 if (dst_page != src_page) {
4671 src_kaddr = page_address(src_page);
4673 src_kaddr = dst_kaddr;
4674 if (areas_overlap(src_off, dst_off, len))
4679 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4681 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
4684 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4685 unsigned long src_offset, unsigned long len)
4688 size_t dst_off_in_page;
4689 size_t src_off_in_page;
4690 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4691 unsigned long dst_i;
4692 unsigned long src_i;
4694 if (src_offset + len > dst->len) {
4695 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4696 "len %lu dst len %lu\n", src_offset, len, dst->len);
4699 if (dst_offset + len > dst->len) {
4700 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4701 "len %lu dst len %lu\n", dst_offset, len, dst->len);
4706 dst_off_in_page = (start_offset + dst_offset) &
4707 ((unsigned long)PAGE_CACHE_SIZE - 1);
4708 src_off_in_page = (start_offset + src_offset) &
4709 ((unsigned long)PAGE_CACHE_SIZE - 1);
4711 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4712 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4714 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4716 cur = min_t(unsigned long, cur,
4717 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4719 copy_pages(extent_buffer_page(dst, dst_i),
4720 extent_buffer_page(dst, src_i),
4721 dst_off_in_page, src_off_in_page, cur);
4729 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4730 unsigned long src_offset, unsigned long len)
4733 size_t dst_off_in_page;
4734 size_t src_off_in_page;
4735 unsigned long dst_end = dst_offset + len - 1;
4736 unsigned long src_end = src_offset + len - 1;
4737 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4738 unsigned long dst_i;
4739 unsigned long src_i;
4741 if (src_offset + len > dst->len) {
4742 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4743 "len %lu len %lu\n", src_offset, len, dst->len);
4746 if (dst_offset + len > dst->len) {
4747 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4748 "len %lu len %lu\n", dst_offset, len, dst->len);
4751 if (dst_offset < src_offset) {
4752 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4756 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4757 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4759 dst_off_in_page = (start_offset + dst_end) &
4760 ((unsigned long)PAGE_CACHE_SIZE - 1);
4761 src_off_in_page = (start_offset + src_end) &
4762 ((unsigned long)PAGE_CACHE_SIZE - 1);
4764 cur = min_t(unsigned long, len, src_off_in_page + 1);
4765 cur = min(cur, dst_off_in_page + 1);
4766 move_pages(extent_buffer_page(dst, dst_i),
4767 extent_buffer_page(dst, src_i),
4768 dst_off_in_page - cur + 1,
4769 src_off_in_page - cur + 1, cur);
4777 int try_release_extent_buffer(struct page *page, gfp_t mask)
4779 struct extent_buffer *eb;
4782 * We need to make sure noboody is attaching this page to an eb right
4785 spin_lock(&page->mapping->private_lock);
4786 if (!PagePrivate(page)) {
4787 spin_unlock(&page->mapping->private_lock);
4791 eb = (struct extent_buffer *)page->private;
4795 * This is a little awful but should be ok, we need to make sure that
4796 * the eb doesn't disappear out from under us while we're looking at
4799 spin_lock(&eb->refs_lock);
4800 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
4801 spin_unlock(&eb->refs_lock);
4802 spin_unlock(&page->mapping->private_lock);
4805 spin_unlock(&page->mapping->private_lock);
4807 if ((mask & GFP_NOFS) == GFP_NOFS)
4811 * If tree ref isn't set then we know the ref on this eb is a real ref,
4812 * so just return, this page will likely be freed soon anyway.
4814 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4815 spin_unlock(&eb->refs_lock);
4818 release_extent_buffer(eb, mask);