1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/module.h>
8 #include <linux/spinlock.h>
9 #include <linux/blkdev.h>
10 #include <linux/swap.h>
11 #include <linux/writeback.h>
12 #include <linux/pagevec.h>
13 #include <linux/prefetch.h>
14 #include <linux/cleancache.h>
15 #include "extent_io.h"
16 #include "extent_map.h"
19 #include "btrfs_inode.h"
21 #include "check-integrity.h"
23 #include "rcu-string.h"
25 static struct kmem_cache *extent_state_cache;
26 static struct kmem_cache *extent_buffer_cache;
28 static LIST_HEAD(buffers);
29 static LIST_HEAD(states);
33 static DEFINE_SPINLOCK(leak_lock);
36 #define BUFFER_LRU_MAX 64
41 struct rb_node rb_node;
44 struct extent_page_data {
46 struct extent_io_tree *tree;
47 get_extent_t *get_extent;
49 /* tells writepage not to lock the state bits for this range
50 * it still does the unlocking
52 unsigned int extent_locked:1;
54 /* tells the submit_bio code to use a WRITE_SYNC */
55 unsigned int sync_io:1;
58 static noinline void flush_write_bio(void *data);
59 static inline struct btrfs_fs_info *
60 tree_fs_info(struct extent_io_tree *tree)
62 return btrfs_sb(tree->mapping->host->i_sb);
65 int __init extent_io_init(void)
67 extent_state_cache = kmem_cache_create("extent_state",
68 sizeof(struct extent_state), 0,
69 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
70 if (!extent_state_cache)
73 extent_buffer_cache = kmem_cache_create("extent_buffers",
74 sizeof(struct extent_buffer), 0,
75 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
76 if (!extent_buffer_cache)
77 goto free_state_cache;
81 kmem_cache_destroy(extent_state_cache);
85 void extent_io_exit(void)
87 struct extent_state *state;
88 struct extent_buffer *eb;
90 while (!list_empty(&states)) {
91 state = list_entry(states.next, struct extent_state, leak_list);
92 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
93 "state %lu in tree %p refs %d\n",
94 (unsigned long long)state->start,
95 (unsigned long long)state->end,
96 state->state, state->tree, atomic_read(&state->refs));
97 list_del(&state->leak_list);
98 kmem_cache_free(extent_state_cache, state);
102 while (!list_empty(&buffers)) {
103 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
104 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
105 "refs %d\n", (unsigned long long)eb->start,
106 eb->len, atomic_read(&eb->refs));
107 list_del(&eb->leak_list);
108 kmem_cache_free(extent_buffer_cache, eb);
112 * Make sure all delayed rcu free are flushed before we
116 if (extent_state_cache)
117 kmem_cache_destroy(extent_state_cache);
118 if (extent_buffer_cache)
119 kmem_cache_destroy(extent_buffer_cache);
122 void extent_io_tree_init(struct extent_io_tree *tree,
123 struct address_space *mapping)
125 tree->state = RB_ROOT;
126 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
128 tree->dirty_bytes = 0;
129 spin_lock_init(&tree->lock);
130 spin_lock_init(&tree->buffer_lock);
131 tree->mapping = mapping;
134 static struct extent_state *alloc_extent_state(gfp_t mask)
136 struct extent_state *state;
141 state = kmem_cache_alloc(extent_state_cache, mask);
148 spin_lock_irqsave(&leak_lock, flags);
149 list_add(&state->leak_list, &states);
150 spin_unlock_irqrestore(&leak_lock, flags);
152 atomic_set(&state->refs, 1);
153 init_waitqueue_head(&state->wq);
154 trace_alloc_extent_state(state, mask, _RET_IP_);
158 void free_extent_state(struct extent_state *state)
162 if (atomic_dec_and_test(&state->refs)) {
166 WARN_ON(state->tree);
168 spin_lock_irqsave(&leak_lock, flags);
169 list_del(&state->leak_list);
170 spin_unlock_irqrestore(&leak_lock, flags);
172 trace_free_extent_state(state, _RET_IP_);
173 kmem_cache_free(extent_state_cache, state);
177 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
178 struct rb_node *node)
180 struct rb_node **p = &root->rb_node;
181 struct rb_node *parent = NULL;
182 struct tree_entry *entry;
186 entry = rb_entry(parent, struct tree_entry, rb_node);
188 if (offset < entry->start)
190 else if (offset > entry->end)
196 rb_link_node(node, parent, p);
197 rb_insert_color(node, root);
201 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
202 struct rb_node **prev_ret,
203 struct rb_node **next_ret)
205 struct rb_root *root = &tree->state;
206 struct rb_node *n = root->rb_node;
207 struct rb_node *prev = NULL;
208 struct rb_node *orig_prev = NULL;
209 struct tree_entry *entry;
210 struct tree_entry *prev_entry = NULL;
213 entry = rb_entry(n, struct tree_entry, rb_node);
217 if (offset < entry->start)
219 else if (offset > entry->end)
227 while (prev && offset > prev_entry->end) {
228 prev = rb_next(prev);
229 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
236 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
237 while (prev && offset < prev_entry->start) {
238 prev = rb_prev(prev);
239 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
246 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
249 struct rb_node *prev = NULL;
252 ret = __etree_search(tree, offset, &prev, NULL);
258 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
259 struct extent_state *other)
261 if (tree->ops && tree->ops->merge_extent_hook)
262 tree->ops->merge_extent_hook(tree->mapping->host, new,
267 * utility function to look for merge candidates inside a given range.
268 * Any extents with matching state are merged together into a single
269 * extent in the tree. Extents with EXTENT_IO in their state field
270 * are not merged because the end_io handlers need to be able to do
271 * operations on them without sleeping (or doing allocations/splits).
273 * This should be called with the tree lock held.
275 static void merge_state(struct extent_io_tree *tree,
276 struct extent_state *state)
278 struct extent_state *other;
279 struct rb_node *other_node;
281 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
284 other_node = rb_prev(&state->rb_node);
286 other = rb_entry(other_node, struct extent_state, rb_node);
287 if (other->end == state->start - 1 &&
288 other->state == state->state) {
289 merge_cb(tree, state, other);
290 state->start = other->start;
292 rb_erase(&other->rb_node, &tree->state);
293 free_extent_state(other);
296 other_node = rb_next(&state->rb_node);
298 other = rb_entry(other_node, struct extent_state, rb_node);
299 if (other->start == state->end + 1 &&
300 other->state == state->state) {
301 merge_cb(tree, state, other);
302 state->end = other->end;
304 rb_erase(&other->rb_node, &tree->state);
305 free_extent_state(other);
310 static void set_state_cb(struct extent_io_tree *tree,
311 struct extent_state *state, int *bits)
313 if (tree->ops && tree->ops->set_bit_hook)
314 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
317 static void clear_state_cb(struct extent_io_tree *tree,
318 struct extent_state *state, int *bits)
320 if (tree->ops && tree->ops->clear_bit_hook)
321 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
324 static void set_state_bits(struct extent_io_tree *tree,
325 struct extent_state *state, int *bits);
328 * insert an extent_state struct into the tree. 'bits' are set on the
329 * struct before it is inserted.
331 * This may return -EEXIST if the extent is already there, in which case the
332 * state struct is freed.
334 * The tree lock is not taken internally. This is a utility function and
335 * probably isn't what you want to call (see set/clear_extent_bit).
337 static int insert_state(struct extent_io_tree *tree,
338 struct extent_state *state, u64 start, u64 end,
341 struct rb_node *node;
344 printk(KERN_ERR "btrfs end < start %llu %llu\n",
345 (unsigned long long)end,
346 (unsigned long long)start);
349 state->start = start;
352 set_state_bits(tree, state, bits);
354 node = tree_insert(&tree->state, end, &state->rb_node);
356 struct extent_state *found;
357 found = rb_entry(node, struct extent_state, rb_node);
358 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
359 "%llu %llu\n", (unsigned long long)found->start,
360 (unsigned long long)found->end,
361 (unsigned long long)start, (unsigned long long)end);
365 merge_state(tree, state);
369 static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
372 if (tree->ops && tree->ops->split_extent_hook)
373 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
377 * split a given extent state struct in two, inserting the preallocated
378 * struct 'prealloc' as the newly created second half. 'split' indicates an
379 * offset inside 'orig' where it should be split.
382 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
383 * are two extent state structs in the tree:
384 * prealloc: [orig->start, split - 1]
385 * orig: [ split, orig->end ]
387 * The tree locks are not taken by this function. They need to be held
390 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
391 struct extent_state *prealloc, u64 split)
393 struct rb_node *node;
395 split_cb(tree, orig, split);
397 prealloc->start = orig->start;
398 prealloc->end = split - 1;
399 prealloc->state = orig->state;
402 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
404 free_extent_state(prealloc);
407 prealloc->tree = tree;
411 static struct extent_state *next_state(struct extent_state *state)
413 struct rb_node *next = rb_next(&state->rb_node);
415 return rb_entry(next, struct extent_state, rb_node);
421 * utility function to clear some bits in an extent state struct.
422 * it will optionally wake up any one waiting on this state (wake == 1).
424 * If no bits are set on the state struct after clearing things, the
425 * struct is freed and removed from the tree
427 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
428 struct extent_state *state,
431 struct extent_state *next;
432 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
434 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
435 u64 range = state->end - state->start + 1;
436 WARN_ON(range > tree->dirty_bytes);
437 tree->dirty_bytes -= range;
439 clear_state_cb(tree, state, bits);
440 state->state &= ~bits_to_clear;
443 if (state->state == 0) {
444 next = next_state(state);
446 rb_erase(&state->rb_node, &tree->state);
448 free_extent_state(state);
453 merge_state(tree, state);
454 next = next_state(state);
459 static struct extent_state *
460 alloc_extent_state_atomic(struct extent_state *prealloc)
463 prealloc = alloc_extent_state(GFP_ATOMIC);
468 void extent_io_tree_panic(struct extent_io_tree *tree, int err)
470 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
471 "Extent tree was modified by another "
472 "thread while locked.");
476 * clear some bits on a range in the tree. This may require splitting
477 * or inserting elements in the tree, so the gfp mask is used to
478 * indicate which allocations or sleeping are allowed.
480 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
481 * the given range from the tree regardless of state (ie for truncate).
483 * the range [start, end] is inclusive.
485 * This takes the tree lock, and returns 0 on success and < 0 on error.
487 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
488 int bits, int wake, int delete,
489 struct extent_state **cached_state,
492 struct extent_state *state;
493 struct extent_state *cached;
494 struct extent_state *prealloc = NULL;
495 struct rb_node *node;
501 bits |= ~EXTENT_CTLBITS;
502 bits |= EXTENT_FIRST_DELALLOC;
504 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
507 if (!prealloc && (mask & __GFP_WAIT)) {
508 prealloc = alloc_extent_state(mask);
513 spin_lock(&tree->lock);
515 cached = *cached_state;
518 *cached_state = NULL;
522 if (cached && cached->tree && cached->start <= start &&
523 cached->end > start) {
525 atomic_dec(&cached->refs);
530 free_extent_state(cached);
533 * this search will find the extents that end after
536 node = tree_search(tree, start);
539 state = rb_entry(node, struct extent_state, rb_node);
541 if (state->start > end)
543 WARN_ON(state->end < start);
544 last_end = state->end;
546 /* the state doesn't have the wanted bits, go ahead */
547 if (!(state->state & bits)) {
548 state = next_state(state);
553 * | ---- desired range ---- |
555 * | ------------- state -------------- |
557 * We need to split the extent we found, and may flip
558 * bits on second half.
560 * If the extent we found extends past our range, we
561 * just split and search again. It'll get split again
562 * the next time though.
564 * If the extent we found is inside our range, we clear
565 * the desired bit on it.
568 if (state->start < start) {
569 prealloc = alloc_extent_state_atomic(prealloc);
571 err = split_state(tree, state, prealloc, start);
573 extent_io_tree_panic(tree, err);
578 if (state->end <= end) {
579 state = clear_state_bit(tree, state, &bits, wake);
585 * | ---- desired range ---- |
587 * We need to split the extent, and clear the bit
590 if (state->start <= end && state->end > end) {
591 prealloc = alloc_extent_state_atomic(prealloc);
593 err = split_state(tree, state, prealloc, end + 1);
595 extent_io_tree_panic(tree, err);
600 clear_state_bit(tree, prealloc, &bits, wake);
606 state = clear_state_bit(tree, state, &bits, wake);
608 if (last_end == (u64)-1)
610 start = last_end + 1;
611 if (start <= end && state && !need_resched())
616 spin_unlock(&tree->lock);
618 free_extent_state(prealloc);
625 spin_unlock(&tree->lock);
626 if (mask & __GFP_WAIT)
631 static void wait_on_state(struct extent_io_tree *tree,
632 struct extent_state *state)
633 __releases(tree->lock)
634 __acquires(tree->lock)
637 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
638 spin_unlock(&tree->lock);
640 spin_lock(&tree->lock);
641 finish_wait(&state->wq, &wait);
645 * waits for one or more bits to clear on a range in the state tree.
646 * The range [start, end] is inclusive.
647 * The tree lock is taken by this function
649 void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
651 struct extent_state *state;
652 struct rb_node *node;
654 spin_lock(&tree->lock);
658 * this search will find all the extents that end after
661 node = tree_search(tree, start);
665 state = rb_entry(node, struct extent_state, rb_node);
667 if (state->start > end)
670 if (state->state & bits) {
671 start = state->start;
672 atomic_inc(&state->refs);
673 wait_on_state(tree, state);
674 free_extent_state(state);
677 start = state->end + 1;
682 cond_resched_lock(&tree->lock);
685 spin_unlock(&tree->lock);
688 static void set_state_bits(struct extent_io_tree *tree,
689 struct extent_state *state,
692 int bits_to_set = *bits & ~EXTENT_CTLBITS;
694 set_state_cb(tree, state, bits);
695 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
696 u64 range = state->end - state->start + 1;
697 tree->dirty_bytes += range;
699 state->state |= bits_to_set;
702 static void cache_state(struct extent_state *state,
703 struct extent_state **cached_ptr)
705 if (cached_ptr && !(*cached_ptr)) {
706 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
708 atomic_inc(&state->refs);
713 static void uncache_state(struct extent_state **cached_ptr)
715 if (cached_ptr && (*cached_ptr)) {
716 struct extent_state *state = *cached_ptr;
718 free_extent_state(state);
723 * set some bits on a range in the tree. This may require allocations or
724 * sleeping, so the gfp mask is used to indicate what is allowed.
726 * If any of the exclusive bits are set, this will fail with -EEXIST if some
727 * part of the range already has the desired bits set. The start of the
728 * existing range is returned in failed_start in this case.
730 * [start, end] is inclusive This takes the tree lock.
733 static int __must_check
734 __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
735 int bits, int exclusive_bits, u64 *failed_start,
736 struct extent_state **cached_state, gfp_t mask)
738 struct extent_state *state;
739 struct extent_state *prealloc = NULL;
740 struct rb_node *node;
745 bits |= EXTENT_FIRST_DELALLOC;
747 if (!prealloc && (mask & __GFP_WAIT)) {
748 prealloc = alloc_extent_state(mask);
752 spin_lock(&tree->lock);
753 if (cached_state && *cached_state) {
754 state = *cached_state;
755 if (state->start <= start && state->end > start &&
757 node = &state->rb_node;
762 * this search will find all the extents that end after
765 node = tree_search(tree, start);
767 prealloc = alloc_extent_state_atomic(prealloc);
769 err = insert_state(tree, prealloc, start, end, &bits);
771 extent_io_tree_panic(tree, err);
776 state = rb_entry(node, struct extent_state, rb_node);
778 last_start = state->start;
779 last_end = state->end;
782 * | ---- desired range ---- |
785 * Just lock what we found and keep going
787 if (state->start == start && state->end <= end) {
788 if (state->state & exclusive_bits) {
789 *failed_start = state->start;
794 set_state_bits(tree, state, &bits);
795 cache_state(state, cached_state);
796 merge_state(tree, state);
797 if (last_end == (u64)-1)
799 start = last_end + 1;
800 state = next_state(state);
801 if (start < end && state && state->start == start &&
808 * | ---- desired range ---- |
811 * | ------------- state -------------- |
813 * We need to split the extent we found, and may flip bits on
816 * If the extent we found extends past our
817 * range, we just split and search again. It'll get split
818 * again the next time though.
820 * If the extent we found is inside our range, we set the
823 if (state->start < start) {
824 if (state->state & exclusive_bits) {
825 *failed_start = start;
830 prealloc = alloc_extent_state_atomic(prealloc);
832 err = split_state(tree, state, prealloc, start);
834 extent_io_tree_panic(tree, err);
839 if (state->end <= end) {
840 set_state_bits(tree, state, &bits);
841 cache_state(state, cached_state);
842 merge_state(tree, state);
843 if (last_end == (u64)-1)
845 start = last_end + 1;
846 state = next_state(state);
847 if (start < end && state && state->start == start &&
854 * | ---- desired range ---- |
855 * | state | or | state |
857 * There's a hole, we need to insert something in it and
858 * ignore the extent we found.
860 if (state->start > start) {
862 if (end < last_start)
865 this_end = last_start - 1;
867 prealloc = alloc_extent_state_atomic(prealloc);
871 * Avoid to free 'prealloc' if it can be merged with
874 err = insert_state(tree, prealloc, start, this_end,
877 extent_io_tree_panic(tree, err);
879 cache_state(prealloc, cached_state);
881 start = this_end + 1;
885 * | ---- desired range ---- |
887 * We need to split the extent, and set the bit
890 if (state->start <= end && state->end > end) {
891 if (state->state & exclusive_bits) {
892 *failed_start = start;
897 prealloc = alloc_extent_state_atomic(prealloc);
899 err = split_state(tree, state, prealloc, end + 1);
901 extent_io_tree_panic(tree, err);
903 set_state_bits(tree, prealloc, &bits);
904 cache_state(prealloc, cached_state);
905 merge_state(tree, prealloc);
913 spin_unlock(&tree->lock);
915 free_extent_state(prealloc);
922 spin_unlock(&tree->lock);
923 if (mask & __GFP_WAIT)
928 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
929 u64 *failed_start, struct extent_state **cached_state,
932 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
938 * convert_extent_bit - convert all bits in a given range from one bit to
940 * @tree: the io tree to search
941 * @start: the start offset in bytes
942 * @end: the end offset in bytes (inclusive)
943 * @bits: the bits to set in this range
944 * @clear_bits: the bits to clear in this range
945 * @mask: the allocation mask
947 * This will go through and set bits for the given range. If any states exist
948 * already in this range they are set with the given bit and cleared of the
949 * clear_bits. This is only meant to be used by things that are mergeable, ie
950 * converting from say DELALLOC to DIRTY. This is not meant to be used with
951 * boundary bits like LOCK.
953 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
954 int bits, int clear_bits, gfp_t mask)
956 struct extent_state *state;
957 struct extent_state *prealloc = NULL;
958 struct rb_node *node;
964 if (!prealloc && (mask & __GFP_WAIT)) {
965 prealloc = alloc_extent_state(mask);
970 spin_lock(&tree->lock);
972 * this search will find all the extents that end after
975 node = tree_search(tree, start);
977 prealloc = alloc_extent_state_atomic(prealloc);
982 err = insert_state(tree, prealloc, start, end, &bits);
985 extent_io_tree_panic(tree, err);
988 state = rb_entry(node, struct extent_state, rb_node);
990 last_start = state->start;
991 last_end = state->end;
994 * | ---- desired range ---- |
997 * Just lock what we found and keep going
999 if (state->start == start && state->end <= end) {
1000 set_state_bits(tree, state, &bits);
1001 state = clear_state_bit(tree, state, &clear_bits, 0);
1002 if (last_end == (u64)-1)
1004 start = last_end + 1;
1005 if (start < end && state && state->start == start &&
1012 * | ---- desired range ---- |
1015 * | ------------- state -------------- |
1017 * We need to split the extent we found, and may flip bits on
1020 * If the extent we found extends past our
1021 * range, we just split and search again. It'll get split
1022 * again the next time though.
1024 * If the extent we found is inside our range, we set the
1025 * desired bit on it.
1027 if (state->start < start) {
1028 prealloc = alloc_extent_state_atomic(prealloc);
1033 err = split_state(tree, state, prealloc, start);
1035 extent_io_tree_panic(tree, err);
1039 if (state->end <= end) {
1040 set_state_bits(tree, state, &bits);
1041 state = clear_state_bit(tree, state, &clear_bits, 0);
1042 if (last_end == (u64)-1)
1044 start = last_end + 1;
1045 if (start < end && state && state->start == start &&
1052 * | ---- desired range ---- |
1053 * | state | or | state |
1055 * There's a hole, we need to insert something in it and
1056 * ignore the extent we found.
1058 if (state->start > start) {
1060 if (end < last_start)
1063 this_end = last_start - 1;
1065 prealloc = alloc_extent_state_atomic(prealloc);
1072 * Avoid to free 'prealloc' if it can be merged with
1075 err = insert_state(tree, prealloc, start, this_end,
1078 extent_io_tree_panic(tree, err);
1080 start = this_end + 1;
1084 * | ---- desired range ---- |
1086 * We need to split the extent, and set the bit
1089 if (state->start <= end && state->end > end) {
1090 prealloc = alloc_extent_state_atomic(prealloc);
1096 err = split_state(tree, state, prealloc, end + 1);
1098 extent_io_tree_panic(tree, err);
1100 set_state_bits(tree, prealloc, &bits);
1101 clear_state_bit(tree, prealloc, &clear_bits, 0);
1109 spin_unlock(&tree->lock);
1111 free_extent_state(prealloc);
1118 spin_unlock(&tree->lock);
1119 if (mask & __GFP_WAIT)
1124 /* wrappers around set/clear extent bit */
1125 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1128 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
1132 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1133 int bits, gfp_t mask)
1135 return set_extent_bit(tree, start, end, bits, NULL,
1139 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1140 int bits, gfp_t mask)
1142 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
1145 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
1146 struct extent_state **cached_state, gfp_t mask)
1148 return set_extent_bit(tree, start, end,
1149 EXTENT_DELALLOC | EXTENT_UPTODATE,
1150 NULL, cached_state, mask);
1153 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1156 return clear_extent_bit(tree, start, end,
1157 EXTENT_DIRTY | EXTENT_DELALLOC |
1158 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
1161 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1164 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
1168 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1169 struct extent_state **cached_state, gfp_t mask)
1171 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1172 cached_state, mask);
1175 int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1176 struct extent_state **cached_state, gfp_t mask)
1178 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1179 cached_state, mask);
1183 * either insert or lock state struct between start and end use mask to tell
1184 * us if waiting is desired.
1186 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1187 int bits, struct extent_state **cached_state)
1192 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1193 EXTENT_LOCKED, &failed_start,
1194 cached_state, GFP_NOFS);
1195 if (err == -EEXIST) {
1196 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1197 start = failed_start;
1200 WARN_ON(start > end);
1205 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1207 return lock_extent_bits(tree, start, end, 0, NULL);
1210 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1215 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1216 &failed_start, NULL, GFP_NOFS);
1217 if (err == -EEXIST) {
1218 if (failed_start > start)
1219 clear_extent_bit(tree, start, failed_start - 1,
1220 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
1226 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1227 struct extent_state **cached, gfp_t mask)
1229 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1233 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1235 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1240 * helper function to set both pages and extents in the tree writeback
1242 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1244 unsigned long index = start >> PAGE_CACHE_SHIFT;
1245 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1248 while (index <= end_index) {
1249 page = find_get_page(tree->mapping, index);
1250 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1251 set_page_writeback(page);
1252 page_cache_release(page);
1258 /* find the first state struct with 'bits' set after 'start', and
1259 * return it. tree->lock must be held. NULL will returned if
1260 * nothing was found after 'start'
1262 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1263 u64 start, int bits)
1265 struct rb_node *node;
1266 struct extent_state *state;
1269 * this search will find all the extents that end after
1272 node = tree_search(tree, start);
1277 state = rb_entry(node, struct extent_state, rb_node);
1278 if (state->end >= start && (state->state & bits))
1281 node = rb_next(node);
1290 * find the first offset in the io tree with 'bits' set. zero is
1291 * returned if we find something, and *start_ret and *end_ret are
1292 * set to reflect the state struct that was found.
1294 * If nothing was found, 1 is returned. If found something, return 0.
1296 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1297 u64 *start_ret, u64 *end_ret, int bits)
1299 struct extent_state *state;
1302 spin_lock(&tree->lock);
1303 state = find_first_extent_bit_state(tree, start, bits);
1305 *start_ret = state->start;
1306 *end_ret = state->end;
1309 spin_unlock(&tree->lock);
1314 * find a contiguous range of bytes in the file marked as delalloc, not
1315 * more than 'max_bytes'. start and end are used to return the range,
1317 * 1 is returned if we find something, 0 if nothing was in the tree
1319 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1320 u64 *start, u64 *end, u64 max_bytes,
1321 struct extent_state **cached_state)
1323 struct rb_node *node;
1324 struct extent_state *state;
1325 u64 cur_start = *start;
1327 u64 total_bytes = 0;
1329 spin_lock(&tree->lock);
1332 * this search will find all the extents that end after
1335 node = tree_search(tree, cur_start);
1343 state = rb_entry(node, struct extent_state, rb_node);
1344 if (found && (state->start != cur_start ||
1345 (state->state & EXTENT_BOUNDARY))) {
1348 if (!(state->state & EXTENT_DELALLOC)) {
1354 *start = state->start;
1355 *cached_state = state;
1356 atomic_inc(&state->refs);
1360 cur_start = state->end + 1;
1361 node = rb_next(node);
1364 total_bytes += state->end - state->start + 1;
1365 if (total_bytes >= max_bytes)
1369 spin_unlock(&tree->lock);
1373 static noinline void __unlock_for_delalloc(struct inode *inode,
1374 struct page *locked_page,
1378 struct page *pages[16];
1379 unsigned long index = start >> PAGE_CACHE_SHIFT;
1380 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1381 unsigned long nr_pages = end_index - index + 1;
1384 if (index == locked_page->index && end_index == index)
1387 while (nr_pages > 0) {
1388 ret = find_get_pages_contig(inode->i_mapping, index,
1389 min_t(unsigned long, nr_pages,
1390 ARRAY_SIZE(pages)), pages);
1391 for (i = 0; i < ret; i++) {
1392 if (pages[i] != locked_page)
1393 unlock_page(pages[i]);
1394 page_cache_release(pages[i]);
1402 static noinline int lock_delalloc_pages(struct inode *inode,
1403 struct page *locked_page,
1407 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1408 unsigned long start_index = index;
1409 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1410 unsigned long pages_locked = 0;
1411 struct page *pages[16];
1412 unsigned long nrpages;
1416 /* the caller is responsible for locking the start index */
1417 if (index == locked_page->index && index == end_index)
1420 /* skip the page at the start index */
1421 nrpages = end_index - index + 1;
1422 while (nrpages > 0) {
1423 ret = find_get_pages_contig(inode->i_mapping, index,
1424 min_t(unsigned long,
1425 nrpages, ARRAY_SIZE(pages)), pages);
1430 /* now we have an array of pages, lock them all */
1431 for (i = 0; i < ret; i++) {
1433 * the caller is taking responsibility for
1436 if (pages[i] != locked_page) {
1437 lock_page(pages[i]);
1438 if (!PageDirty(pages[i]) ||
1439 pages[i]->mapping != inode->i_mapping) {
1441 unlock_page(pages[i]);
1442 page_cache_release(pages[i]);
1446 page_cache_release(pages[i]);
1455 if (ret && pages_locked) {
1456 __unlock_for_delalloc(inode, locked_page,
1458 ((u64)(start_index + pages_locked - 1)) <<
1465 * find a contiguous range of bytes in the file marked as delalloc, not
1466 * more than 'max_bytes'. start and end are used to return the range,
1468 * 1 is returned if we find something, 0 if nothing was in the tree
1470 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1471 struct extent_io_tree *tree,
1472 struct page *locked_page,
1473 u64 *start, u64 *end,
1479 struct extent_state *cached_state = NULL;
1484 /* step one, find a bunch of delalloc bytes starting at start */
1485 delalloc_start = *start;
1487 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1488 max_bytes, &cached_state);
1489 if (!found || delalloc_end <= *start) {
1490 *start = delalloc_start;
1491 *end = delalloc_end;
1492 free_extent_state(cached_state);
1497 * start comes from the offset of locked_page. We have to lock
1498 * pages in order, so we can't process delalloc bytes before
1501 if (delalloc_start < *start)
1502 delalloc_start = *start;
1505 * make sure to limit the number of pages we try to lock down
1508 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1509 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1511 /* step two, lock all the pages after the page that has start */
1512 ret = lock_delalloc_pages(inode, locked_page,
1513 delalloc_start, delalloc_end);
1514 if (ret == -EAGAIN) {
1515 /* some of the pages are gone, lets avoid looping by
1516 * shortening the size of the delalloc range we're searching
1518 free_extent_state(cached_state);
1520 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1521 max_bytes = PAGE_CACHE_SIZE - offset;
1529 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
1531 /* step three, lock the state bits for the whole range */
1532 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
1534 /* then test to make sure it is all still delalloc */
1535 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1536 EXTENT_DELALLOC, 1, cached_state);
1538 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1539 &cached_state, GFP_NOFS);
1540 __unlock_for_delalloc(inode, locked_page,
1541 delalloc_start, delalloc_end);
1545 free_extent_state(cached_state);
1546 *start = delalloc_start;
1547 *end = delalloc_end;
1552 int extent_clear_unlock_delalloc(struct inode *inode,
1553 struct extent_io_tree *tree,
1554 u64 start, u64 end, struct page *locked_page,
1558 struct page *pages[16];
1559 unsigned long index = start >> PAGE_CACHE_SHIFT;
1560 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1561 unsigned long nr_pages = end_index - index + 1;
1565 if (op & EXTENT_CLEAR_UNLOCK)
1566 clear_bits |= EXTENT_LOCKED;
1567 if (op & EXTENT_CLEAR_DIRTY)
1568 clear_bits |= EXTENT_DIRTY;
1570 if (op & EXTENT_CLEAR_DELALLOC)
1571 clear_bits |= EXTENT_DELALLOC;
1573 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1574 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1575 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1576 EXTENT_SET_PRIVATE2)))
1579 while (nr_pages > 0) {
1580 ret = find_get_pages_contig(inode->i_mapping, index,
1581 min_t(unsigned long,
1582 nr_pages, ARRAY_SIZE(pages)), pages);
1583 for (i = 0; i < ret; i++) {
1585 if (op & EXTENT_SET_PRIVATE2)
1586 SetPagePrivate2(pages[i]);
1588 if (pages[i] == locked_page) {
1589 page_cache_release(pages[i]);
1592 if (op & EXTENT_CLEAR_DIRTY)
1593 clear_page_dirty_for_io(pages[i]);
1594 if (op & EXTENT_SET_WRITEBACK)
1595 set_page_writeback(pages[i]);
1596 if (op & EXTENT_END_WRITEBACK)
1597 end_page_writeback(pages[i]);
1598 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1599 unlock_page(pages[i]);
1600 page_cache_release(pages[i]);
1610 * count the number of bytes in the tree that have a given bit(s)
1611 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1612 * cached. The total number found is returned.
1614 u64 count_range_bits(struct extent_io_tree *tree,
1615 u64 *start, u64 search_end, u64 max_bytes,
1616 unsigned long bits, int contig)
1618 struct rb_node *node;
1619 struct extent_state *state;
1620 u64 cur_start = *start;
1621 u64 total_bytes = 0;
1625 if (search_end <= cur_start) {
1630 spin_lock(&tree->lock);
1631 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1632 total_bytes = tree->dirty_bytes;
1636 * this search will find all the extents that end after
1639 node = tree_search(tree, cur_start);
1644 state = rb_entry(node, struct extent_state, rb_node);
1645 if (state->start > search_end)
1647 if (contig && found && state->start > last + 1)
1649 if (state->end >= cur_start && (state->state & bits) == bits) {
1650 total_bytes += min(search_end, state->end) + 1 -
1651 max(cur_start, state->start);
1652 if (total_bytes >= max_bytes)
1655 *start = max(cur_start, state->start);
1659 } else if (contig && found) {
1662 node = rb_next(node);
1667 spin_unlock(&tree->lock);
1672 * set the private field for a given byte offset in the tree. If there isn't
1673 * an extent_state there already, this does nothing.
1675 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1677 struct rb_node *node;
1678 struct extent_state *state;
1681 spin_lock(&tree->lock);
1683 * this search will find all the extents that end after
1686 node = tree_search(tree, start);
1691 state = rb_entry(node, struct extent_state, rb_node);
1692 if (state->start != start) {
1696 state->private = private;
1698 spin_unlock(&tree->lock);
1702 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1704 struct rb_node *node;
1705 struct extent_state *state;
1708 spin_lock(&tree->lock);
1710 * this search will find all the extents that end after
1713 node = tree_search(tree, start);
1718 state = rb_entry(node, struct extent_state, rb_node);
1719 if (state->start != start) {
1723 *private = state->private;
1725 spin_unlock(&tree->lock);
1730 * searches a range in the state tree for a given mask.
1731 * If 'filled' == 1, this returns 1 only if every extent in the tree
1732 * has the bits set. Otherwise, 1 is returned if any bit in the
1733 * range is found set.
1735 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1736 int bits, int filled, struct extent_state *cached)
1738 struct extent_state *state = NULL;
1739 struct rb_node *node;
1742 spin_lock(&tree->lock);
1743 if (cached && cached->tree && cached->start <= start &&
1744 cached->end > start)
1745 node = &cached->rb_node;
1747 node = tree_search(tree, start);
1748 while (node && start <= end) {
1749 state = rb_entry(node, struct extent_state, rb_node);
1751 if (filled && state->start > start) {
1756 if (state->start > end)
1759 if (state->state & bits) {
1763 } else if (filled) {
1768 if (state->end == (u64)-1)
1771 start = state->end + 1;
1774 node = rb_next(node);
1781 spin_unlock(&tree->lock);
1786 * helper function to set a given page up to date if all the
1787 * extents in the tree for that page are up to date
1789 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
1791 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1792 u64 end = start + PAGE_CACHE_SIZE - 1;
1793 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1794 SetPageUptodate(page);
1798 * helper function to unlock a page if all the extents in the tree
1799 * for that page are unlocked
1801 static void check_page_locked(struct extent_io_tree *tree, struct page *page)
1803 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1804 u64 end = start + PAGE_CACHE_SIZE - 1;
1805 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1810 * helper function to end page writeback if all the extents
1811 * in the tree for that page are done with writeback
1813 static void check_page_writeback(struct extent_io_tree *tree,
1816 end_page_writeback(page);
1820 * When IO fails, either with EIO or csum verification fails, we
1821 * try other mirrors that might have a good copy of the data. This
1822 * io_failure_record is used to record state as we go through all the
1823 * mirrors. If another mirror has good data, the page is set up to date
1824 * and things continue. If a good mirror can't be found, the original
1825 * bio end_io callback is called to indicate things have failed.
1827 struct io_failure_record {
1832 unsigned long bio_flags;
1838 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1843 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1845 set_state_private(failure_tree, rec->start, 0);
1846 ret = clear_extent_bits(failure_tree, rec->start,
1847 rec->start + rec->len - 1,
1848 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1853 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1854 rec->start + rec->len - 1,
1855 EXTENT_DAMAGED, GFP_NOFS);
1864 static void repair_io_failure_callback(struct bio *bio, int err)
1866 complete(bio->bi_private);
1870 * this bypasses the standard btrfs submit functions deliberately, as
1871 * the standard behavior is to write all copies in a raid setup. here we only
1872 * want to write the one bad copy. so we do the mapping for ourselves and issue
1873 * submit_bio directly.
1874 * to avoid any synchonization issues, wait for the data after writing, which
1875 * actually prevents the read that triggered the error from finishing.
1876 * currently, there can be no more than two copies of every data bit. thus,
1877 * exactly one rewrite is required.
1879 int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1880 u64 length, u64 logical, struct page *page,
1884 struct btrfs_device *dev;
1885 DECLARE_COMPLETION_ONSTACK(compl);
1888 struct btrfs_bio *bbio = NULL;
1891 BUG_ON(!mirror_num);
1893 bio = bio_alloc(GFP_NOFS, 1);
1896 bio->bi_private = &compl;
1897 bio->bi_end_io = repair_io_failure_callback;
1899 map_length = length;
1901 ret = btrfs_map_block(map_tree, WRITE, logical,
1902 &map_length, &bbio, mirror_num);
1907 BUG_ON(mirror_num != bbio->mirror_num);
1908 sector = bbio->stripes[mirror_num-1].physical >> 9;
1909 bio->bi_sector = sector;
1910 dev = bbio->stripes[mirror_num-1].dev;
1912 if (!dev || !dev->bdev || !dev->writeable) {
1916 bio->bi_bdev = dev->bdev;
1917 bio_add_page(bio, page, length, start-page_offset(page));
1918 btrfsic_submit_bio(WRITE_SYNC, bio);
1919 wait_for_completion(&compl);
1921 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1922 /* try to remap that extent elsewhere? */
1924 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
1928 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
1929 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
1930 start, rcu_str_deref(dev->name), sector);
1936 int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1939 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1940 u64 start = eb->start;
1941 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
1944 for (i = 0; i < num_pages; i++) {
1945 struct page *p = extent_buffer_page(eb, i);
1946 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1947 start, p, mirror_num);
1950 start += PAGE_CACHE_SIZE;
1957 * each time an IO finishes, we do a fast check in the IO failure tree
1958 * to see if we need to process or clean up an io_failure_record
1960 static int clean_io_failure(u64 start, struct page *page)
1963 u64 private_failure;
1964 struct io_failure_record *failrec;
1965 struct btrfs_mapping_tree *map_tree;
1966 struct extent_state *state;
1970 struct inode *inode = page->mapping->host;
1973 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1974 (u64)-1, 1, EXTENT_DIRTY, 0);
1978 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1983 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1984 BUG_ON(!failrec->this_mirror);
1986 if (failrec->in_validation) {
1987 /* there was no real error, just free the record */
1988 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1994 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1995 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1998 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2000 if (state && state->start == failrec->start) {
2001 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
2002 num_copies = btrfs_num_copies(map_tree, failrec->logical,
2004 if (num_copies > 1) {
2005 ret = repair_io_failure(map_tree, start, failrec->len,
2006 failrec->logical, page,
2007 failrec->failed_mirror);
2014 ret = free_io_failure(inode, failrec, did_repair);
2020 * this is a generic handler for readpage errors (default
2021 * readpage_io_failed_hook). if other copies exist, read those and write back
2022 * good data to the failed position. does not investigate in remapping the
2023 * failed extent elsewhere, hoping the device will be smart enough to do this as
2027 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2028 u64 start, u64 end, int failed_mirror,
2029 struct extent_state *state)
2031 struct io_failure_record *failrec = NULL;
2033 struct extent_map *em;
2034 struct inode *inode = page->mapping->host;
2035 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2036 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2037 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2044 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2046 ret = get_state_private(failure_tree, start, &private);
2048 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2051 failrec->start = start;
2052 failrec->len = end - start + 1;
2053 failrec->this_mirror = 0;
2054 failrec->bio_flags = 0;
2055 failrec->in_validation = 0;
2057 read_lock(&em_tree->lock);
2058 em = lookup_extent_mapping(em_tree, start, failrec->len);
2060 read_unlock(&em_tree->lock);
2065 if (em->start > start || em->start + em->len < start) {
2066 free_extent_map(em);
2069 read_unlock(&em_tree->lock);
2071 if (!em || IS_ERR(em)) {
2075 logical = start - em->start;
2076 logical = em->block_start + logical;
2077 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2078 logical = em->block_start;
2079 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2080 extent_set_compress_type(&failrec->bio_flags,
2083 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2084 "len=%llu\n", logical, start, failrec->len);
2085 failrec->logical = logical;
2086 free_extent_map(em);
2088 /* set the bits in the private failure tree */
2089 ret = set_extent_bits(failure_tree, start, end,
2090 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2092 ret = set_state_private(failure_tree, start,
2093 (u64)(unsigned long)failrec);
2094 /* set the bits in the inode's tree */
2096 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2103 failrec = (struct io_failure_record *)(unsigned long)private;
2104 pr_debug("bio_readpage_error: (found) logical=%llu, "
2105 "start=%llu, len=%llu, validation=%d\n",
2106 failrec->logical, failrec->start, failrec->len,
2107 failrec->in_validation);
2109 * when data can be on disk more than twice, add to failrec here
2110 * (e.g. with a list for failed_mirror) to make
2111 * clean_io_failure() clean all those errors at once.
2114 num_copies = btrfs_num_copies(
2115 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2116 failrec->logical, failrec->len);
2117 if (num_copies == 1) {
2119 * we only have a single copy of the data, so don't bother with
2120 * all the retry and error correction code that follows. no
2121 * matter what the error is, it is very likely to persist.
2123 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2124 "state=%p, num_copies=%d, next_mirror %d, "
2125 "failed_mirror %d\n", state, num_copies,
2126 failrec->this_mirror, failed_mirror);
2127 free_io_failure(inode, failrec, 0);
2132 spin_lock(&tree->lock);
2133 state = find_first_extent_bit_state(tree, failrec->start,
2135 if (state && state->start != failrec->start)
2137 spin_unlock(&tree->lock);
2141 * there are two premises:
2142 * a) deliver good data to the caller
2143 * b) correct the bad sectors on disk
2145 if (failed_bio->bi_vcnt > 1) {
2147 * to fulfill b), we need to know the exact failing sectors, as
2148 * we don't want to rewrite any more than the failed ones. thus,
2149 * we need separate read requests for the failed bio
2151 * if the following BUG_ON triggers, our validation request got
2152 * merged. we need separate requests for our algorithm to work.
2154 BUG_ON(failrec->in_validation);
2155 failrec->in_validation = 1;
2156 failrec->this_mirror = failed_mirror;
2157 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2160 * we're ready to fulfill a) and b) alongside. get a good copy
2161 * of the failed sector and if we succeed, we have setup
2162 * everything for repair_io_failure to do the rest for us.
2164 if (failrec->in_validation) {
2165 BUG_ON(failrec->this_mirror != failed_mirror);
2166 failrec->in_validation = 0;
2167 failrec->this_mirror = 0;
2169 failrec->failed_mirror = failed_mirror;
2170 failrec->this_mirror++;
2171 if (failrec->this_mirror == failed_mirror)
2172 failrec->this_mirror++;
2173 read_mode = READ_SYNC;
2176 if (!state || failrec->this_mirror > num_copies) {
2177 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2178 "next_mirror %d, failed_mirror %d\n", state,
2179 num_copies, failrec->this_mirror, failed_mirror);
2180 free_io_failure(inode, failrec, 0);
2184 bio = bio_alloc(GFP_NOFS, 1);
2186 free_io_failure(inode, failrec, 0);
2189 bio->bi_private = state;
2190 bio->bi_end_io = failed_bio->bi_end_io;
2191 bio->bi_sector = failrec->logical >> 9;
2192 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2195 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2197 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2198 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2199 failrec->this_mirror, num_copies, failrec->in_validation);
2201 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2202 failrec->this_mirror,
2203 failrec->bio_flags, 0);
2207 /* lots and lots of room for performance fixes in the end_bio funcs */
2209 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2211 int uptodate = (err == 0);
2212 struct extent_io_tree *tree;
2215 tree = &BTRFS_I(page->mapping->host)->io_tree;
2217 if (tree->ops && tree->ops->writepage_end_io_hook) {
2218 ret = tree->ops->writepage_end_io_hook(page, start,
2219 end, NULL, uptodate);
2225 ClearPageUptodate(page);
2232 * after a writepage IO is done, we need to:
2233 * clear the uptodate bits on error
2234 * clear the writeback bits in the extent tree for this IO
2235 * end_page_writeback if the page has no more pending IO
2237 * Scheduling is not allowed, so the extent state tree is expected
2238 * to have one and only one object corresponding to this IO.
2240 static void end_bio_extent_writepage(struct bio *bio, int err)
2242 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2243 struct extent_io_tree *tree;
2249 struct page *page = bvec->bv_page;
2250 tree = &BTRFS_I(page->mapping->host)->io_tree;
2252 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2254 end = start + bvec->bv_len - 1;
2256 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2261 if (--bvec >= bio->bi_io_vec)
2262 prefetchw(&bvec->bv_page->flags);
2264 if (end_extent_writepage(page, err, start, end))
2268 end_page_writeback(page);
2270 check_page_writeback(tree, page);
2271 } while (bvec >= bio->bi_io_vec);
2277 * after a readpage IO is done, we need to:
2278 * clear the uptodate bits on error
2279 * set the uptodate bits if things worked
2280 * set the page up to date if all extents in the tree are uptodate
2281 * clear the lock bit in the extent tree
2282 * unlock the page if there are no other extents locked for it
2284 * Scheduling is not allowed, so the extent state tree is expected
2285 * to have one and only one object corresponding to this IO.
2287 static void end_bio_extent_readpage(struct bio *bio, int err)
2289 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2290 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2291 struct bio_vec *bvec = bio->bi_io_vec;
2292 struct extent_io_tree *tree;
2303 struct page *page = bvec->bv_page;
2304 struct extent_state *cached = NULL;
2305 struct extent_state *state;
2307 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2308 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2309 (long int)bio->bi_bdev);
2310 tree = &BTRFS_I(page->mapping->host)->io_tree;
2312 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2314 end = start + bvec->bv_len - 1;
2316 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2321 if (++bvec <= bvec_end)
2322 prefetchw(&bvec->bv_page->flags);
2324 spin_lock(&tree->lock);
2325 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2326 if (state && state->start == start) {
2328 * take a reference on the state, unlock will drop
2331 cache_state(state, &cached);
2333 spin_unlock(&tree->lock);
2335 mirror = (int)(unsigned long)bio->bi_bdev;
2336 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2337 ret = tree->ops->readpage_end_io_hook(page, start, end,
2342 clean_io_failure(start, page);
2345 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
2346 ret = tree->ops->readpage_io_failed_hook(page, mirror);
2348 test_bit(BIO_UPTODATE, &bio->bi_flags))
2350 } else if (!uptodate) {
2352 * The generic bio_readpage_error handles errors the
2353 * following way: If possible, new read requests are
2354 * created and submitted and will end up in
2355 * end_bio_extent_readpage as well (if we're lucky, not
2356 * in the !uptodate case). In that case it returns 0 and
2357 * we just go on with the next page in our bio. If it
2358 * can't handle the error it will return -EIO and we
2359 * remain responsible for that page.
2361 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
2364 test_bit(BIO_UPTODATE, &bio->bi_flags);
2367 uncache_state(&cached);
2372 if (uptodate && tree->track_uptodate) {
2373 set_extent_uptodate(tree, start, end, &cached,
2376 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2380 SetPageUptodate(page);
2382 ClearPageUptodate(page);
2388 check_page_uptodate(tree, page);
2390 ClearPageUptodate(page);
2393 check_page_locked(tree, page);
2395 } while (bvec <= bvec_end);
2401 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2406 bio = bio_alloc(gfp_flags, nr_vecs);
2408 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2409 while (!bio && (nr_vecs /= 2))
2410 bio = bio_alloc(gfp_flags, nr_vecs);
2415 bio->bi_bdev = bdev;
2416 bio->bi_sector = first_sector;
2422 * Since writes are async, they will only return -ENOMEM.
2423 * Reads can return the full range of I/O error conditions.
2425 static int __must_check submit_one_bio(int rw, struct bio *bio,
2426 int mirror_num, unsigned long bio_flags)
2429 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2430 struct page *page = bvec->bv_page;
2431 struct extent_io_tree *tree = bio->bi_private;
2434 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
2436 bio->bi_private = NULL;
2440 if (tree->ops && tree->ops->submit_bio_hook)
2441 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2442 mirror_num, bio_flags, start);
2444 btrfsic_submit_bio(rw, bio);
2446 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2452 static int merge_bio(struct extent_io_tree *tree, struct page *page,
2453 unsigned long offset, size_t size, struct bio *bio,
2454 unsigned long bio_flags)
2457 if (tree->ops && tree->ops->merge_bio_hook)
2458 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2465 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2466 struct page *page, sector_t sector,
2467 size_t size, unsigned long offset,
2468 struct block_device *bdev,
2469 struct bio **bio_ret,
2470 unsigned long max_pages,
2471 bio_end_io_t end_io_func,
2473 unsigned long prev_bio_flags,
2474 unsigned long bio_flags)
2480 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2481 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2482 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2484 if (bio_ret && *bio_ret) {
2487 contig = bio->bi_sector == sector;
2489 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2492 if (prev_bio_flags != bio_flags || !contig ||
2493 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
2494 bio_add_page(bio, page, page_size, offset) < page_size) {
2495 ret = submit_one_bio(rw, bio, mirror_num,
2504 if (this_compressed)
2507 nr = bio_get_nr_vecs(bdev);
2509 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2513 bio_add_page(bio, page, page_size, offset);
2514 bio->bi_end_io = end_io_func;
2515 bio->bi_private = tree;
2520 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2525 void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2527 if (!PagePrivate(page)) {
2528 SetPagePrivate(page);
2529 page_cache_get(page);
2530 set_page_private(page, (unsigned long)eb);
2532 WARN_ON(page->private != (unsigned long)eb);
2536 void set_page_extent_mapped(struct page *page)
2538 if (!PagePrivate(page)) {
2539 SetPagePrivate(page);
2540 page_cache_get(page);
2541 set_page_private(page, EXTENT_PAGE_PRIVATE);
2546 * basic readpage implementation. Locked extent state structs are inserted
2547 * into the tree that are removed when the IO is done (by the end_io
2549 * XXX JDM: This needs looking at to ensure proper page locking
2551 static int __extent_read_full_page(struct extent_io_tree *tree,
2553 get_extent_t *get_extent,
2554 struct bio **bio, int mirror_num,
2555 unsigned long *bio_flags)
2557 struct inode *inode = page->mapping->host;
2558 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2559 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2563 u64 last_byte = i_size_read(inode);
2567 struct extent_map *em;
2568 struct block_device *bdev;
2569 struct btrfs_ordered_extent *ordered;
2572 size_t pg_offset = 0;
2574 size_t disk_io_size;
2575 size_t blocksize = inode->i_sb->s_blocksize;
2576 unsigned long this_bio_flag = 0;
2578 set_page_extent_mapped(page);
2580 if (!PageUptodate(page)) {
2581 if (cleancache_get_page(page) == 0) {
2582 BUG_ON(blocksize != PAGE_SIZE);
2589 lock_extent(tree, start, end);
2590 ordered = btrfs_lookup_ordered_extent(inode, start);
2593 unlock_extent(tree, start, end);
2594 btrfs_start_ordered_extent(inode, ordered, 1);
2595 btrfs_put_ordered_extent(ordered);
2598 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2600 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2603 iosize = PAGE_CACHE_SIZE - zero_offset;
2604 userpage = kmap_atomic(page);
2605 memset(userpage + zero_offset, 0, iosize);
2606 flush_dcache_page(page);
2607 kunmap_atomic(userpage);
2610 while (cur <= end) {
2611 if (cur >= last_byte) {
2613 struct extent_state *cached = NULL;
2615 iosize = PAGE_CACHE_SIZE - pg_offset;
2616 userpage = kmap_atomic(page);
2617 memset(userpage + pg_offset, 0, iosize);
2618 flush_dcache_page(page);
2619 kunmap_atomic(userpage);
2620 set_extent_uptodate(tree, cur, cur + iosize - 1,
2622 unlock_extent_cached(tree, cur, cur + iosize - 1,
2626 em = get_extent(inode, page, pg_offset, cur,
2628 if (IS_ERR_OR_NULL(em)) {
2630 unlock_extent(tree, cur, end);
2633 extent_offset = cur - em->start;
2634 BUG_ON(extent_map_end(em) <= cur);
2637 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2638 this_bio_flag = EXTENT_BIO_COMPRESSED;
2639 extent_set_compress_type(&this_bio_flag,
2643 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2644 cur_end = min(extent_map_end(em) - 1, end);
2645 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2646 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2647 disk_io_size = em->block_len;
2648 sector = em->block_start >> 9;
2650 sector = (em->block_start + extent_offset) >> 9;
2651 disk_io_size = iosize;
2654 block_start = em->block_start;
2655 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2656 block_start = EXTENT_MAP_HOLE;
2657 free_extent_map(em);
2660 /* we've found a hole, just zero and go on */
2661 if (block_start == EXTENT_MAP_HOLE) {
2663 struct extent_state *cached = NULL;
2665 userpage = kmap_atomic(page);
2666 memset(userpage + pg_offset, 0, iosize);
2667 flush_dcache_page(page);
2668 kunmap_atomic(userpage);
2670 set_extent_uptodate(tree, cur, cur + iosize - 1,
2672 unlock_extent_cached(tree, cur, cur + iosize - 1,
2675 pg_offset += iosize;
2678 /* the get_extent function already copied into the page */
2679 if (test_range_bit(tree, cur, cur_end,
2680 EXTENT_UPTODATE, 1, NULL)) {
2681 check_page_uptodate(tree, page);
2682 unlock_extent(tree, cur, cur + iosize - 1);
2684 pg_offset += iosize;
2687 /* we have an inline extent but it didn't get marked up
2688 * to date. Error out
2690 if (block_start == EXTENT_MAP_INLINE) {
2692 unlock_extent(tree, cur, cur + iosize - 1);
2694 pg_offset += iosize;
2699 if (tree->ops && tree->ops->readpage_io_hook) {
2700 ret = tree->ops->readpage_io_hook(page, cur,
2704 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2706 ret = submit_extent_page(READ, tree, page,
2707 sector, disk_io_size, pg_offset,
2709 end_bio_extent_readpage, mirror_num,
2712 BUG_ON(ret == -ENOMEM);
2714 *bio_flags = this_bio_flag;
2719 pg_offset += iosize;
2723 if (!PageError(page))
2724 SetPageUptodate(page);
2730 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2731 get_extent_t *get_extent, int mirror_num)
2733 struct bio *bio = NULL;
2734 unsigned long bio_flags = 0;
2737 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2740 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2744 static noinline void update_nr_written(struct page *page,
2745 struct writeback_control *wbc,
2746 unsigned long nr_written)
2748 wbc->nr_to_write -= nr_written;
2749 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2750 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2751 page->mapping->writeback_index = page->index + nr_written;
2755 * the writepage semantics are similar to regular writepage. extent
2756 * records are inserted to lock ranges in the tree, and as dirty areas
2757 * are found, they are marked writeback. Then the lock bits are removed
2758 * and the end_io handler clears the writeback ranges
2760 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2763 struct inode *inode = page->mapping->host;
2764 struct extent_page_data *epd = data;
2765 struct extent_io_tree *tree = epd->tree;
2766 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2768 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2772 u64 last_byte = i_size_read(inode);
2776 struct extent_state *cached_state = NULL;
2777 struct extent_map *em;
2778 struct block_device *bdev;
2781 size_t pg_offset = 0;
2783 loff_t i_size = i_size_read(inode);
2784 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2790 unsigned long nr_written = 0;
2791 bool fill_delalloc = true;
2793 if (wbc->sync_mode == WB_SYNC_ALL)
2794 write_flags = WRITE_SYNC;
2796 write_flags = WRITE;
2798 trace___extent_writepage(page, inode, wbc);
2800 WARN_ON(!PageLocked(page));
2802 ClearPageError(page);
2804 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2805 if (page->index > end_index ||
2806 (page->index == end_index && !pg_offset)) {
2807 page->mapping->a_ops->invalidatepage(page, 0);
2812 if (page->index == end_index) {
2815 userpage = kmap_atomic(page);
2816 memset(userpage + pg_offset, 0,
2817 PAGE_CACHE_SIZE - pg_offset);
2818 kunmap_atomic(userpage);
2819 flush_dcache_page(page);
2823 set_page_extent_mapped(page);
2825 if (!tree->ops || !tree->ops->fill_delalloc)
2826 fill_delalloc = false;
2828 delalloc_start = start;
2831 if (!epd->extent_locked && fill_delalloc) {
2832 u64 delalloc_to_write = 0;
2834 * make sure the wbc mapping index is at least updated
2837 update_nr_written(page, wbc, 0);
2839 while (delalloc_end < page_end) {
2840 nr_delalloc = find_lock_delalloc_range(inode, tree,
2845 if (nr_delalloc == 0) {
2846 delalloc_start = delalloc_end + 1;
2849 ret = tree->ops->fill_delalloc(inode, page,
2854 /* File system has been set read-only */
2860 * delalloc_end is already one less than the total
2861 * length, so we don't subtract one from
2864 delalloc_to_write += (delalloc_end - delalloc_start +
2867 delalloc_start = delalloc_end + 1;
2869 if (wbc->nr_to_write < delalloc_to_write) {
2872 if (delalloc_to_write < thresh * 2)
2873 thresh = delalloc_to_write;
2874 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2878 /* did the fill delalloc function already unlock and start
2884 * we've unlocked the page, so we can't update
2885 * the mapping's writeback index, just update
2888 wbc->nr_to_write -= nr_written;
2892 if (tree->ops && tree->ops->writepage_start_hook) {
2893 ret = tree->ops->writepage_start_hook(page, start,
2896 /* Fixup worker will requeue */
2898 wbc->pages_skipped++;
2900 redirty_page_for_writepage(wbc, page);
2901 update_nr_written(page, wbc, nr_written);
2909 * we don't want to touch the inode after unlocking the page,
2910 * so we update the mapping writeback index now
2912 update_nr_written(page, wbc, nr_written + 1);
2915 if (last_byte <= start) {
2916 if (tree->ops && tree->ops->writepage_end_io_hook)
2917 tree->ops->writepage_end_io_hook(page, start,
2922 blocksize = inode->i_sb->s_blocksize;
2924 while (cur <= end) {
2925 if (cur >= last_byte) {
2926 if (tree->ops && tree->ops->writepage_end_io_hook)
2927 tree->ops->writepage_end_io_hook(page, cur,
2931 em = epd->get_extent(inode, page, pg_offset, cur,
2933 if (IS_ERR_OR_NULL(em)) {
2938 extent_offset = cur - em->start;
2939 BUG_ON(extent_map_end(em) <= cur);
2941 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2942 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2943 sector = (em->block_start + extent_offset) >> 9;
2945 block_start = em->block_start;
2946 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2947 free_extent_map(em);
2951 * compressed and inline extents are written through other
2954 if (compressed || block_start == EXTENT_MAP_HOLE ||
2955 block_start == EXTENT_MAP_INLINE) {
2957 * end_io notification does not happen here for
2958 * compressed extents
2960 if (!compressed && tree->ops &&
2961 tree->ops->writepage_end_io_hook)
2962 tree->ops->writepage_end_io_hook(page, cur,
2965 else if (compressed) {
2966 /* we don't want to end_page_writeback on
2967 * a compressed extent. this happens
2974 pg_offset += iosize;
2977 /* leave this out until we have a page_mkwrite call */
2978 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2979 EXTENT_DIRTY, 0, NULL)) {
2981 pg_offset += iosize;
2985 if (tree->ops && tree->ops->writepage_io_hook) {
2986 ret = tree->ops->writepage_io_hook(page, cur,
2994 unsigned long max_nr = end_index + 1;
2996 set_range_writeback(tree, cur, cur + iosize - 1);
2997 if (!PageWriteback(page)) {
2998 printk(KERN_ERR "btrfs warning page %lu not "
2999 "writeback, cur %llu end %llu\n",
3000 page->index, (unsigned long long)cur,
3001 (unsigned long long)end);
3004 ret = submit_extent_page(write_flags, tree, page,
3005 sector, iosize, pg_offset,
3006 bdev, &epd->bio, max_nr,
3007 end_bio_extent_writepage,
3013 pg_offset += iosize;
3018 /* make sure the mapping tag for page dirty gets cleared */
3019 set_page_writeback(page);
3020 end_page_writeback(page);
3026 /* drop our reference on any cached states */
3027 free_extent_state(cached_state);
3031 static int eb_wait(void *word)
3037 static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3039 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3040 TASK_UNINTERRUPTIBLE);
3043 static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3044 struct btrfs_fs_info *fs_info,
3045 struct extent_page_data *epd)
3047 unsigned long i, num_pages;
3051 if (!btrfs_try_tree_write_lock(eb)) {
3053 flush_write_bio(epd);
3054 btrfs_tree_lock(eb);
3057 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3058 btrfs_tree_unlock(eb);
3062 flush_write_bio(epd);
3066 wait_on_extent_buffer_writeback(eb);
3067 btrfs_tree_lock(eb);
3068 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3070 btrfs_tree_unlock(eb);
3075 * We need to do this to prevent races in people who check if the eb is
3076 * under IO since we can end up having no IO bits set for a short period
3079 spin_lock(&eb->refs_lock);
3080 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3081 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3082 spin_unlock(&eb->refs_lock);
3083 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3084 spin_lock(&fs_info->delalloc_lock);
3085 if (fs_info->dirty_metadata_bytes >= eb->len)
3086 fs_info->dirty_metadata_bytes -= eb->len;
3089 spin_unlock(&fs_info->delalloc_lock);
3092 spin_unlock(&eb->refs_lock);
3095 btrfs_tree_unlock(eb);
3100 num_pages = num_extent_pages(eb->start, eb->len);
3101 for (i = 0; i < num_pages; i++) {
3102 struct page *p = extent_buffer_page(eb, i);
3104 if (!trylock_page(p)) {
3106 flush_write_bio(epd);
3116 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3118 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3119 smp_mb__after_clear_bit();
3120 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3123 static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3125 int uptodate = err == 0;
3126 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3127 struct extent_buffer *eb;
3131 struct page *page = bvec->bv_page;
3134 eb = (struct extent_buffer *)page->private;
3136 done = atomic_dec_and_test(&eb->io_pages);
3138 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3139 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3140 ClearPageUptodate(page);
3144 end_page_writeback(page);
3149 end_extent_buffer_writeback(eb);
3150 } while (bvec >= bio->bi_io_vec);
3156 static int write_one_eb(struct extent_buffer *eb,
3157 struct btrfs_fs_info *fs_info,
3158 struct writeback_control *wbc,
3159 struct extent_page_data *epd)
3161 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3162 u64 offset = eb->start;
3163 unsigned long i, num_pages;
3164 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3167 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3168 num_pages = num_extent_pages(eb->start, eb->len);
3169 atomic_set(&eb->io_pages, num_pages);
3170 for (i = 0; i < num_pages; i++) {
3171 struct page *p = extent_buffer_page(eb, i);
3173 clear_page_dirty_for_io(p);
3174 set_page_writeback(p);
3175 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3176 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3177 -1, end_bio_extent_buffer_writepage,
3180 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3182 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3183 end_extent_buffer_writeback(eb);
3187 offset += PAGE_CACHE_SIZE;
3188 update_nr_written(p, wbc, 1);
3192 if (unlikely(ret)) {
3193 for (; i < num_pages; i++) {
3194 struct page *p = extent_buffer_page(eb, i);
3202 int btree_write_cache_pages(struct address_space *mapping,
3203 struct writeback_control *wbc)
3205 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3206 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3207 struct extent_buffer *eb, *prev_eb = NULL;
3208 struct extent_page_data epd = {
3212 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3216 int nr_to_write_done = 0;
3217 struct pagevec pvec;
3220 pgoff_t end; /* Inclusive */
3224 pagevec_init(&pvec, 0);
3225 if (wbc->range_cyclic) {
3226 index = mapping->writeback_index; /* Start from prev offset */
3229 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3230 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3233 if (wbc->sync_mode == WB_SYNC_ALL)
3234 tag = PAGECACHE_TAG_TOWRITE;
3236 tag = PAGECACHE_TAG_DIRTY;
3238 if (wbc->sync_mode == WB_SYNC_ALL)
3239 tag_pages_for_writeback(mapping, index, end);
3240 while (!done && !nr_to_write_done && (index <= end) &&
3241 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3242 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3246 for (i = 0; i < nr_pages; i++) {
3247 struct page *page = pvec.pages[i];
3249 if (!PagePrivate(page))
3252 if (!wbc->range_cyclic && page->index > end) {
3257 eb = (struct extent_buffer *)page->private;
3266 if (!atomic_inc_not_zero(&eb->refs)) {
3272 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3274 free_extent_buffer(eb);
3278 ret = write_one_eb(eb, fs_info, wbc, &epd);
3281 free_extent_buffer(eb);
3284 free_extent_buffer(eb);
3287 * the filesystem may choose to bump up nr_to_write.
3288 * We have to make sure to honor the new nr_to_write
3291 nr_to_write_done = wbc->nr_to_write <= 0;
3293 pagevec_release(&pvec);
3296 if (!scanned && !done) {
3298 * We hit the last page and there is more work to be done: wrap
3299 * back to the start of the file
3305 flush_write_bio(&epd);
3310 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3311 * @mapping: address space structure to write
3312 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3313 * @writepage: function called for each page
3314 * @data: data passed to writepage function
3316 * If a page is already under I/O, write_cache_pages() skips it, even
3317 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3318 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3319 * and msync() need to guarantee that all the data which was dirty at the time
3320 * the call was made get new I/O started against them. If wbc->sync_mode is
3321 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3322 * existing IO to complete.
3324 static int extent_write_cache_pages(struct extent_io_tree *tree,
3325 struct address_space *mapping,
3326 struct writeback_control *wbc,
3327 writepage_t writepage, void *data,
3328 void (*flush_fn)(void *))
3330 struct inode *inode = mapping->host;
3333 int nr_to_write_done = 0;
3334 struct pagevec pvec;
3337 pgoff_t end; /* Inclusive */
3342 * We have to hold onto the inode so that ordered extents can do their
3343 * work when the IO finishes. The alternative to this is failing to add
3344 * an ordered extent if the igrab() fails there and that is a huge pain
3345 * to deal with, so instead just hold onto the inode throughout the
3346 * writepages operation. If it fails here we are freeing up the inode
3347 * anyway and we'd rather not waste our time writing out stuff that is
3348 * going to be truncated anyway.
3353 pagevec_init(&pvec, 0);
3354 if (wbc->range_cyclic) {
3355 index = mapping->writeback_index; /* Start from prev offset */
3358 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3359 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3362 if (wbc->sync_mode == WB_SYNC_ALL)
3363 tag = PAGECACHE_TAG_TOWRITE;
3365 tag = PAGECACHE_TAG_DIRTY;
3367 if (wbc->sync_mode == WB_SYNC_ALL)
3368 tag_pages_for_writeback(mapping, index, end);
3369 while (!done && !nr_to_write_done && (index <= end) &&
3370 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3371 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3375 for (i = 0; i < nr_pages; i++) {
3376 struct page *page = pvec.pages[i];
3379 * At this point we hold neither mapping->tree_lock nor
3380 * lock on the page itself: the page may be truncated or
3381 * invalidated (changing page->mapping to NULL), or even
3382 * swizzled back from swapper_space to tmpfs file
3386 tree->ops->write_cache_pages_lock_hook) {
3387 tree->ops->write_cache_pages_lock_hook(page,
3390 if (!trylock_page(page)) {
3396 if (unlikely(page->mapping != mapping)) {
3401 if (!wbc->range_cyclic && page->index > end) {
3407 if (wbc->sync_mode != WB_SYNC_NONE) {
3408 if (PageWriteback(page))
3410 wait_on_page_writeback(page);
3413 if (PageWriteback(page) ||
3414 !clear_page_dirty_for_io(page)) {
3419 ret = (*writepage)(page, wbc, data);
3421 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3429 * the filesystem may choose to bump up nr_to_write.
3430 * We have to make sure to honor the new nr_to_write
3433 nr_to_write_done = wbc->nr_to_write <= 0;
3435 pagevec_release(&pvec);
3438 if (!scanned && !done) {
3440 * We hit the last page and there is more work to be done: wrap
3441 * back to the start of the file
3447 btrfs_add_delayed_iput(inode);
3451 static void flush_epd_write_bio(struct extent_page_data *epd)
3460 ret = submit_one_bio(rw, epd->bio, 0, 0);
3461 BUG_ON(ret < 0); /* -ENOMEM */
3466 static noinline void flush_write_bio(void *data)
3468 struct extent_page_data *epd = data;
3469 flush_epd_write_bio(epd);
3472 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3473 get_extent_t *get_extent,
3474 struct writeback_control *wbc)
3477 struct extent_page_data epd = {
3480 .get_extent = get_extent,
3482 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3485 ret = __extent_writepage(page, wbc, &epd);
3487 flush_epd_write_bio(&epd);
3491 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3492 u64 start, u64 end, get_extent_t *get_extent,
3496 struct address_space *mapping = inode->i_mapping;
3498 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3501 struct extent_page_data epd = {
3504 .get_extent = get_extent,
3506 .sync_io = mode == WB_SYNC_ALL,
3508 struct writeback_control wbc_writepages = {
3510 .nr_to_write = nr_pages * 2,
3511 .range_start = start,
3512 .range_end = end + 1,
3515 while (start <= end) {
3516 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3517 if (clear_page_dirty_for_io(page))
3518 ret = __extent_writepage(page, &wbc_writepages, &epd);
3520 if (tree->ops && tree->ops->writepage_end_io_hook)
3521 tree->ops->writepage_end_io_hook(page, start,
3522 start + PAGE_CACHE_SIZE - 1,
3526 page_cache_release(page);
3527 start += PAGE_CACHE_SIZE;
3530 flush_epd_write_bio(&epd);
3534 int extent_writepages(struct extent_io_tree *tree,
3535 struct address_space *mapping,
3536 get_extent_t *get_extent,
3537 struct writeback_control *wbc)
3540 struct extent_page_data epd = {
3543 .get_extent = get_extent,
3545 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3548 ret = extent_write_cache_pages(tree, mapping, wbc,
3549 __extent_writepage, &epd,
3551 flush_epd_write_bio(&epd);
3555 int extent_readpages(struct extent_io_tree *tree,
3556 struct address_space *mapping,
3557 struct list_head *pages, unsigned nr_pages,
3558 get_extent_t get_extent)
3560 struct bio *bio = NULL;
3562 unsigned long bio_flags = 0;
3563 struct page *pagepool[16];
3568 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3569 page = list_entry(pages->prev, struct page, lru);
3571 prefetchw(&page->flags);
3572 list_del(&page->lru);
3573 if (add_to_page_cache_lru(page, mapping,
3574 page->index, GFP_NOFS)) {
3575 page_cache_release(page);
3579 pagepool[nr++] = page;
3580 if (nr < ARRAY_SIZE(pagepool))
3582 for (i = 0; i < nr; i++) {
3583 __extent_read_full_page(tree, pagepool[i], get_extent,
3584 &bio, 0, &bio_flags);
3585 page_cache_release(pagepool[i]);
3589 for (i = 0; i < nr; i++) {
3590 __extent_read_full_page(tree, pagepool[i], get_extent,
3591 &bio, 0, &bio_flags);
3592 page_cache_release(pagepool[i]);
3595 BUG_ON(!list_empty(pages));
3597 return submit_one_bio(READ, bio, 0, bio_flags);
3602 * basic invalidatepage code, this waits on any locked or writeback
3603 * ranges corresponding to the page, and then deletes any extent state
3604 * records from the tree
3606 int extent_invalidatepage(struct extent_io_tree *tree,
3607 struct page *page, unsigned long offset)
3609 struct extent_state *cached_state = NULL;
3610 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3611 u64 end = start + PAGE_CACHE_SIZE - 1;
3612 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3614 start += (offset + blocksize - 1) & ~(blocksize - 1);
3618 lock_extent_bits(tree, start, end, 0, &cached_state);
3619 wait_on_page_writeback(page);
3620 clear_extent_bit(tree, start, end,
3621 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3622 EXTENT_DO_ACCOUNTING,
3623 1, 1, &cached_state, GFP_NOFS);
3628 * a helper for releasepage, this tests for areas of the page that
3629 * are locked or under IO and drops the related state bits if it is safe
3632 int try_release_extent_state(struct extent_map_tree *map,
3633 struct extent_io_tree *tree, struct page *page,
3636 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3637 u64 end = start + PAGE_CACHE_SIZE - 1;
3640 if (test_range_bit(tree, start, end,
3641 EXTENT_IOBITS, 0, NULL))
3644 if ((mask & GFP_NOFS) == GFP_NOFS)
3647 * at this point we can safely clear everything except the
3648 * locked bit and the nodatasum bit
3650 ret = clear_extent_bit(tree, start, end,
3651 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3654 /* if clear_extent_bit failed for enomem reasons,
3655 * we can't allow the release to continue.
3666 * a helper for releasepage. As long as there are no locked extents
3667 * in the range corresponding to the page, both state records and extent
3668 * map records are removed
3670 int try_release_extent_mapping(struct extent_map_tree *map,
3671 struct extent_io_tree *tree, struct page *page,
3674 struct extent_map *em;
3675 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3676 u64 end = start + PAGE_CACHE_SIZE - 1;
3678 if ((mask & __GFP_WAIT) &&
3679 page->mapping->host->i_size > 16 * 1024 * 1024) {
3681 while (start <= end) {
3682 len = end - start + 1;
3683 write_lock(&map->lock);
3684 em = lookup_extent_mapping(map, start, len);
3686 write_unlock(&map->lock);
3689 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3690 em->start != start) {
3691 write_unlock(&map->lock);
3692 free_extent_map(em);
3695 if (!test_range_bit(tree, em->start,
3696 extent_map_end(em) - 1,
3697 EXTENT_LOCKED | EXTENT_WRITEBACK,
3699 remove_extent_mapping(map, em);
3700 /* once for the rb tree */
3701 free_extent_map(em);
3703 start = extent_map_end(em);
3704 write_unlock(&map->lock);
3707 free_extent_map(em);
3710 return try_release_extent_state(map, tree, page, mask);
3714 * helper function for fiemap, which doesn't want to see any holes.
3715 * This maps until we find something past 'last'
3717 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3720 get_extent_t *get_extent)
3722 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3723 struct extent_map *em;
3730 len = last - offset;
3733 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3734 em = get_extent(inode, NULL, 0, offset, len, 0);
3735 if (IS_ERR_OR_NULL(em))
3738 /* if this isn't a hole return it */
3739 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3740 em->block_start != EXTENT_MAP_HOLE) {
3744 /* this is a hole, advance to the next extent */
3745 offset = extent_map_end(em);
3746 free_extent_map(em);
3753 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3754 __u64 start, __u64 len, get_extent_t *get_extent)
3758 u64 max = start + len;
3762 u64 last_for_get_extent = 0;
3764 u64 isize = i_size_read(inode);
3765 struct btrfs_key found_key;
3766 struct extent_map *em = NULL;
3767 struct extent_state *cached_state = NULL;
3768 struct btrfs_path *path;
3769 struct btrfs_file_extent_item *item;
3774 unsigned long emflags;
3779 path = btrfs_alloc_path();
3782 path->leave_spinning = 1;
3784 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3785 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3788 * lookup the last file extent. We're not using i_size here
3789 * because there might be preallocation past i_size
3791 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3792 path, btrfs_ino(inode), -1, 0);
3794 btrfs_free_path(path);
3799 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3800 struct btrfs_file_extent_item);
3801 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3802 found_type = btrfs_key_type(&found_key);
3804 /* No extents, but there might be delalloc bits */
3805 if (found_key.objectid != btrfs_ino(inode) ||
3806 found_type != BTRFS_EXTENT_DATA_KEY) {
3807 /* have to trust i_size as the end */
3809 last_for_get_extent = isize;
3812 * remember the start of the last extent. There are a
3813 * bunch of different factors that go into the length of the
3814 * extent, so its much less complex to remember where it started
3816 last = found_key.offset;
3817 last_for_get_extent = last + 1;
3819 btrfs_free_path(path);
3822 * we might have some extents allocated but more delalloc past those
3823 * extents. so, we trust isize unless the start of the last extent is
3828 last_for_get_extent = isize;
3831 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3834 em = get_extent_skip_holes(inode, start, last_for_get_extent,
3844 u64 offset_in_extent;
3846 /* break if the extent we found is outside the range */
3847 if (em->start >= max || extent_map_end(em) < off)
3851 * get_extent may return an extent that starts before our
3852 * requested range. We have to make sure the ranges
3853 * we return to fiemap always move forward and don't
3854 * overlap, so adjust the offsets here
3856 em_start = max(em->start, off);
3859 * record the offset from the start of the extent
3860 * for adjusting the disk offset below
3862 offset_in_extent = em_start - em->start;
3863 em_end = extent_map_end(em);
3864 em_len = em_end - em_start;
3865 emflags = em->flags;
3870 * bump off for our next call to get_extent
3872 off = extent_map_end(em);
3876 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
3878 flags |= FIEMAP_EXTENT_LAST;
3879 } else if (em->block_start == EXTENT_MAP_INLINE) {
3880 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3881 FIEMAP_EXTENT_NOT_ALIGNED);
3882 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
3883 flags |= (FIEMAP_EXTENT_DELALLOC |
3884 FIEMAP_EXTENT_UNKNOWN);
3886 disko = em->block_start + offset_in_extent;
3888 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3889 flags |= FIEMAP_EXTENT_ENCODED;
3891 free_extent_map(em);
3893 if ((em_start >= last) || em_len == (u64)-1 ||
3894 (last == (u64)-1 && isize <= em_end)) {
3895 flags |= FIEMAP_EXTENT_LAST;
3899 /* now scan forward to see if this is really the last extent. */
3900 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3907 flags |= FIEMAP_EXTENT_LAST;
3910 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3916 free_extent_map(em);
3918 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3919 &cached_state, GFP_NOFS);
3923 inline struct page *extent_buffer_page(struct extent_buffer *eb,
3926 return eb->pages[i];
3929 inline unsigned long num_extent_pages(u64 start, u64 len)
3931 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3932 (start >> PAGE_CACHE_SHIFT);
3935 static void __free_extent_buffer(struct extent_buffer *eb)
3938 unsigned long flags;
3939 spin_lock_irqsave(&leak_lock, flags);
3940 list_del(&eb->leak_list);
3941 spin_unlock_irqrestore(&leak_lock, flags);
3943 if (eb->pages && eb->pages != eb->inline_pages)
3945 kmem_cache_free(extent_buffer_cache, eb);
3948 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3953 struct extent_buffer *eb = NULL;
3955 unsigned long flags;
3958 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
3965 rwlock_init(&eb->lock);
3966 atomic_set(&eb->write_locks, 0);
3967 atomic_set(&eb->read_locks, 0);
3968 atomic_set(&eb->blocking_readers, 0);
3969 atomic_set(&eb->blocking_writers, 0);
3970 atomic_set(&eb->spinning_readers, 0);
3971 atomic_set(&eb->spinning_writers, 0);
3972 eb->lock_nested = 0;
3973 init_waitqueue_head(&eb->write_lock_wq);
3974 init_waitqueue_head(&eb->read_lock_wq);
3977 spin_lock_irqsave(&leak_lock, flags);
3978 list_add(&eb->leak_list, &buffers);
3979 spin_unlock_irqrestore(&leak_lock, flags);
3981 spin_lock_init(&eb->refs_lock);
3982 atomic_set(&eb->refs, 1);
3983 atomic_set(&eb->io_pages, 0);
3985 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3986 struct page **pages;
3987 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3989 pages = kzalloc(num_pages, mask);
3991 __free_extent_buffer(eb);
3996 eb->pages = eb->inline_pages;
4002 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4006 struct extent_buffer *new;
4007 unsigned long num_pages = num_extent_pages(src->start, src->len);
4009 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4013 for (i = 0; i < num_pages; i++) {
4014 p = alloc_page(GFP_ATOMIC);
4016 attach_extent_buffer_page(new, p);
4017 WARN_ON(PageDirty(p));
4022 copy_extent_buffer(new, src, 0, 0, src->len);
4023 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4024 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4029 struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4031 struct extent_buffer *eb;
4032 unsigned long num_pages = num_extent_pages(0, len);
4035 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4039 for (i = 0; i < num_pages; i++) {
4040 eb->pages[i] = alloc_page(GFP_ATOMIC);
4044 set_extent_buffer_uptodate(eb);
4045 btrfs_set_header_nritems(eb, 0);
4046 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4050 for (i--; i > 0; i--)
4051 __free_page(eb->pages[i]);
4052 __free_extent_buffer(eb);
4056 static int extent_buffer_under_io(struct extent_buffer *eb)
4058 return (atomic_read(&eb->io_pages) ||
4059 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4060 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4064 * Helper for releasing extent buffer page.
4066 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4067 unsigned long start_idx)
4069 unsigned long index;
4070 unsigned long num_pages;
4072 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4074 BUG_ON(extent_buffer_under_io(eb));
4076 num_pages = num_extent_pages(eb->start, eb->len);
4077 index = start_idx + num_pages;
4078 if (start_idx >= index)
4083 page = extent_buffer_page(eb, index);
4084 if (page && mapped) {
4085 spin_lock(&page->mapping->private_lock);
4087 * We do this since we'll remove the pages after we've
4088 * removed the eb from the radix tree, so we could race
4089 * and have this page now attached to the new eb. So
4090 * only clear page_private if it's still connected to
4093 if (PagePrivate(page) &&
4094 page->private == (unsigned long)eb) {
4095 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4096 BUG_ON(PageDirty(page));
4097 BUG_ON(PageWriteback(page));
4099 * We need to make sure we haven't be attached
4102 ClearPagePrivate(page);
4103 set_page_private(page, 0);
4104 /* One for the page private */
4105 page_cache_release(page);
4107 spin_unlock(&page->mapping->private_lock);
4111 /* One for when we alloced the page */
4112 page_cache_release(page);
4114 } while (index != start_idx);
4118 * Helper for releasing the extent buffer.
4120 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4122 btrfs_release_extent_buffer_page(eb, 0);
4123 __free_extent_buffer(eb);
4126 static void check_buffer_tree_ref(struct extent_buffer *eb)
4128 /* the ref bit is tricky. We have to make sure it is set
4129 * if we have the buffer dirty. Otherwise the
4130 * code to free a buffer can end up dropping a dirty
4133 * Once the ref bit is set, it won't go away while the
4134 * buffer is dirty or in writeback, and it also won't
4135 * go away while we have the reference count on the
4138 * We can't just set the ref bit without bumping the
4139 * ref on the eb because free_extent_buffer might
4140 * see the ref bit and try to clear it. If this happens
4141 * free_extent_buffer might end up dropping our original
4142 * ref by mistake and freeing the page before we are able
4143 * to add one more ref.
4145 * So bump the ref count first, then set the bit. If someone
4146 * beat us to it, drop the ref we added.
4148 spin_lock(&eb->refs_lock);
4149 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4150 atomic_inc(&eb->refs);
4151 spin_unlock(&eb->refs_lock);
4154 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4156 unsigned long num_pages, i;
4158 check_buffer_tree_ref(eb);
4160 num_pages = num_extent_pages(eb->start, eb->len);
4161 for (i = 0; i < num_pages; i++) {
4162 struct page *p = extent_buffer_page(eb, i);
4163 mark_page_accessed(p);
4167 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
4168 u64 start, unsigned long len)
4170 unsigned long num_pages = num_extent_pages(start, len);
4172 unsigned long index = start >> PAGE_CACHE_SHIFT;
4173 struct extent_buffer *eb;
4174 struct extent_buffer *exists = NULL;
4176 struct address_space *mapping = tree->mapping;
4181 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4182 if (eb && atomic_inc_not_zero(&eb->refs)) {
4184 mark_extent_buffer_accessed(eb);
4189 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
4193 for (i = 0; i < num_pages; i++, index++) {
4194 p = find_or_create_page(mapping, index, GFP_NOFS);
4200 spin_lock(&mapping->private_lock);
4201 if (PagePrivate(p)) {
4203 * We could have already allocated an eb for this page
4204 * and attached one so lets see if we can get a ref on
4205 * the existing eb, and if we can we know it's good and
4206 * we can just return that one, else we know we can just
4207 * overwrite page->private.
4209 exists = (struct extent_buffer *)p->private;
4210 if (atomic_inc_not_zero(&exists->refs)) {
4211 spin_unlock(&mapping->private_lock);
4213 page_cache_release(p);
4214 mark_extent_buffer_accessed(exists);
4219 * Do this so attach doesn't complain and we need to
4220 * drop the ref the old guy had.
4222 ClearPagePrivate(p);
4223 WARN_ON(PageDirty(p));
4224 page_cache_release(p);
4226 attach_extent_buffer_page(eb, p);
4227 spin_unlock(&mapping->private_lock);
4228 WARN_ON(PageDirty(p));
4229 mark_page_accessed(p);
4231 if (!PageUptodate(p))
4235 * see below about how we avoid a nasty race with release page
4236 * and why we unlock later
4240 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4242 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4246 spin_lock(&tree->buffer_lock);
4247 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4248 if (ret == -EEXIST) {
4249 exists = radix_tree_lookup(&tree->buffer,
4250 start >> PAGE_CACHE_SHIFT);
4251 if (!atomic_inc_not_zero(&exists->refs)) {
4252 spin_unlock(&tree->buffer_lock);
4253 radix_tree_preload_end();
4257 spin_unlock(&tree->buffer_lock);
4258 radix_tree_preload_end();
4259 mark_extent_buffer_accessed(exists);
4262 /* add one reference for the tree */
4263 check_buffer_tree_ref(eb);
4264 spin_unlock(&tree->buffer_lock);
4265 radix_tree_preload_end();
4268 * there is a race where release page may have
4269 * tried to find this extent buffer in the radix
4270 * but failed. It will tell the VM it is safe to
4271 * reclaim the, and it will clear the page private bit.
4272 * We must make sure to set the page private bit properly
4273 * after the extent buffer is in the radix tree so
4274 * it doesn't get lost
4276 SetPageChecked(eb->pages[0]);
4277 for (i = 1; i < num_pages; i++) {
4278 p = extent_buffer_page(eb, i);
4279 ClearPageChecked(p);
4282 unlock_page(eb->pages[0]);
4286 for (i = 0; i < num_pages; i++) {
4288 unlock_page(eb->pages[i]);
4291 WARN_ON(!atomic_dec_and_test(&eb->refs));
4292 btrfs_release_extent_buffer(eb);
4296 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
4297 u64 start, unsigned long len)
4299 struct extent_buffer *eb;
4302 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4303 if (eb && atomic_inc_not_zero(&eb->refs)) {
4305 mark_extent_buffer_accessed(eb);
4313 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4315 struct extent_buffer *eb =
4316 container_of(head, struct extent_buffer, rcu_head);
4318 __free_extent_buffer(eb);
4321 /* Expects to have eb->eb_lock already held */
4322 static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4324 WARN_ON(atomic_read(&eb->refs) == 0);
4325 if (atomic_dec_and_test(&eb->refs)) {
4326 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4327 spin_unlock(&eb->refs_lock);
4329 struct extent_io_tree *tree = eb->tree;
4331 spin_unlock(&eb->refs_lock);
4333 spin_lock(&tree->buffer_lock);
4334 radix_tree_delete(&tree->buffer,
4335 eb->start >> PAGE_CACHE_SHIFT);
4336 spin_unlock(&tree->buffer_lock);
4339 /* Should be safe to release our pages at this point */
4340 btrfs_release_extent_buffer_page(eb, 0);
4342 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4345 spin_unlock(&eb->refs_lock);
4350 void free_extent_buffer(struct extent_buffer *eb)
4355 spin_lock(&eb->refs_lock);
4356 if (atomic_read(&eb->refs) == 2 &&
4357 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4358 atomic_dec(&eb->refs);
4360 if (atomic_read(&eb->refs) == 2 &&
4361 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
4362 !extent_buffer_under_io(eb) &&
4363 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4364 atomic_dec(&eb->refs);
4367 * I know this is terrible, but it's temporary until we stop tracking
4368 * the uptodate bits and such for the extent buffers.
4370 release_extent_buffer(eb, GFP_ATOMIC);
4373 void free_extent_buffer_stale(struct extent_buffer *eb)
4378 spin_lock(&eb->refs_lock);
4379 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4381 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
4382 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4383 atomic_dec(&eb->refs);
4384 release_extent_buffer(eb, GFP_NOFS);
4387 void clear_extent_buffer_dirty(struct extent_buffer *eb)
4390 unsigned long num_pages;
4393 num_pages = num_extent_pages(eb->start, eb->len);
4395 for (i = 0; i < num_pages; i++) {
4396 page = extent_buffer_page(eb, i);
4397 if (!PageDirty(page))
4401 WARN_ON(!PagePrivate(page));
4403 clear_page_dirty_for_io(page);
4404 spin_lock_irq(&page->mapping->tree_lock);
4405 if (!PageDirty(page)) {
4406 radix_tree_tag_clear(&page->mapping->page_tree,
4408 PAGECACHE_TAG_DIRTY);
4410 spin_unlock_irq(&page->mapping->tree_lock);
4411 ClearPageError(page);
4414 WARN_ON(atomic_read(&eb->refs) == 0);
4417 int set_extent_buffer_dirty(struct extent_buffer *eb)
4420 unsigned long num_pages;
4423 check_buffer_tree_ref(eb);
4425 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4427 num_pages = num_extent_pages(eb->start, eb->len);
4428 WARN_ON(atomic_read(&eb->refs) == 0);
4429 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4431 for (i = 0; i < num_pages; i++)
4432 set_page_dirty(extent_buffer_page(eb, i));
4436 static int range_straddles_pages(u64 start, u64 len)
4438 if (len < PAGE_CACHE_SIZE)
4440 if (start & (PAGE_CACHE_SIZE - 1))
4442 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4447 int clear_extent_buffer_uptodate(struct extent_buffer *eb)
4451 unsigned long num_pages;
4453 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4454 num_pages = num_extent_pages(eb->start, eb->len);
4455 for (i = 0; i < num_pages; i++) {
4456 page = extent_buffer_page(eb, i);
4458 ClearPageUptodate(page);
4463 int set_extent_buffer_uptodate(struct extent_buffer *eb)
4467 unsigned long num_pages;
4469 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4470 num_pages = num_extent_pages(eb->start, eb->len);
4471 for (i = 0; i < num_pages; i++) {
4472 page = extent_buffer_page(eb, i);
4473 SetPageUptodate(page);
4478 int extent_range_uptodate(struct extent_io_tree *tree,
4483 int pg_uptodate = 1;
4485 unsigned long index;
4487 if (range_straddles_pages(start, end - start + 1)) {
4488 ret = test_range_bit(tree, start, end,
4489 EXTENT_UPTODATE, 1, NULL);
4493 while (start <= end) {
4494 index = start >> PAGE_CACHE_SHIFT;
4495 page = find_get_page(tree->mapping, index);
4498 uptodate = PageUptodate(page);
4499 page_cache_release(page);
4504 start += PAGE_CACHE_SIZE;
4509 int extent_buffer_uptodate(struct extent_buffer *eb)
4511 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4514 int read_extent_buffer_pages(struct extent_io_tree *tree,
4515 struct extent_buffer *eb, u64 start, int wait,
4516 get_extent_t *get_extent, int mirror_num)
4519 unsigned long start_i;
4523 int locked_pages = 0;
4524 int all_uptodate = 1;
4525 unsigned long num_pages;
4526 unsigned long num_reads = 0;
4527 struct bio *bio = NULL;
4528 unsigned long bio_flags = 0;
4530 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4534 WARN_ON(start < eb->start);
4535 start_i = (start >> PAGE_CACHE_SHIFT) -
4536 (eb->start >> PAGE_CACHE_SHIFT);
4541 num_pages = num_extent_pages(eb->start, eb->len);
4542 for (i = start_i; i < num_pages; i++) {
4543 page = extent_buffer_page(eb, i);
4544 if (wait == WAIT_NONE) {
4545 if (!trylock_page(page))
4551 if (!PageUptodate(page)) {
4558 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4562 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
4563 eb->read_mirror = 0;
4564 atomic_set(&eb->io_pages, num_reads);
4565 for (i = start_i; i < num_pages; i++) {
4566 page = extent_buffer_page(eb, i);
4567 if (!PageUptodate(page)) {
4568 ClearPageError(page);
4569 err = __extent_read_full_page(tree, page,
4571 mirror_num, &bio_flags);
4580 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
4585 if (ret || wait != WAIT_COMPLETE)
4588 for (i = start_i; i < num_pages; i++) {
4589 page = extent_buffer_page(eb, i);
4590 wait_on_page_locked(page);
4591 if (!PageUptodate(page))
4599 while (locked_pages > 0) {
4600 page = extent_buffer_page(eb, i);
4608 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4609 unsigned long start,
4616 char *dst = (char *)dstv;
4617 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4618 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4620 WARN_ON(start > eb->len);
4621 WARN_ON(start + len > eb->start + eb->len);
4623 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4626 page = extent_buffer_page(eb, i);
4628 cur = min(len, (PAGE_CACHE_SIZE - offset));
4629 kaddr = page_address(page);
4630 memcpy(dst, kaddr + offset, cur);
4639 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4640 unsigned long min_len, char **map,
4641 unsigned long *map_start,
4642 unsigned long *map_len)
4644 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4647 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4648 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4649 unsigned long end_i = (start_offset + start + min_len - 1) >>
4656 offset = start_offset;
4660 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4663 if (start + min_len > eb->len) {
4664 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4665 "wanted %lu %lu\n", (unsigned long long)eb->start,
4666 eb->len, start, min_len);
4671 p = extent_buffer_page(eb, i);
4672 kaddr = page_address(p);
4673 *map = kaddr + offset;
4674 *map_len = PAGE_CACHE_SIZE - offset;
4678 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4679 unsigned long start,
4686 char *ptr = (char *)ptrv;
4687 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4688 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4691 WARN_ON(start > eb->len);
4692 WARN_ON(start + len > eb->start + eb->len);
4694 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4697 page = extent_buffer_page(eb, i);
4699 cur = min(len, (PAGE_CACHE_SIZE - offset));
4701 kaddr = page_address(page);
4702 ret = memcmp(ptr, kaddr + offset, cur);
4714 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4715 unsigned long start, unsigned long len)
4721 char *src = (char *)srcv;
4722 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4723 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4725 WARN_ON(start > eb->len);
4726 WARN_ON(start + len > eb->start + eb->len);
4728 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4731 page = extent_buffer_page(eb, i);
4732 WARN_ON(!PageUptodate(page));
4734 cur = min(len, PAGE_CACHE_SIZE - offset);
4735 kaddr = page_address(page);
4736 memcpy(kaddr + offset, src, cur);
4745 void memset_extent_buffer(struct extent_buffer *eb, char c,
4746 unsigned long start, unsigned long len)
4752 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4753 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4755 WARN_ON(start > eb->len);
4756 WARN_ON(start + len > eb->start + eb->len);
4758 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4761 page = extent_buffer_page(eb, i);
4762 WARN_ON(!PageUptodate(page));
4764 cur = min(len, PAGE_CACHE_SIZE - offset);
4765 kaddr = page_address(page);
4766 memset(kaddr + offset, c, cur);
4774 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4775 unsigned long dst_offset, unsigned long src_offset,
4778 u64 dst_len = dst->len;
4783 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4784 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4786 WARN_ON(src->len != dst_len);
4788 offset = (start_offset + dst_offset) &
4789 ((unsigned long)PAGE_CACHE_SIZE - 1);
4792 page = extent_buffer_page(dst, i);
4793 WARN_ON(!PageUptodate(page));
4795 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4797 kaddr = page_address(page);
4798 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4807 static void move_pages(struct page *dst_page, struct page *src_page,
4808 unsigned long dst_off, unsigned long src_off,
4811 char *dst_kaddr = page_address(dst_page);
4812 if (dst_page == src_page) {
4813 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4815 char *src_kaddr = page_address(src_page);
4816 char *p = dst_kaddr + dst_off + len;
4817 char *s = src_kaddr + src_off + len;
4824 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4826 unsigned long distance = (src > dst) ? src - dst : dst - src;
4827 return distance < len;
4830 static void copy_pages(struct page *dst_page, struct page *src_page,
4831 unsigned long dst_off, unsigned long src_off,
4834 char *dst_kaddr = page_address(dst_page);
4836 int must_memmove = 0;
4838 if (dst_page != src_page) {
4839 src_kaddr = page_address(src_page);
4841 src_kaddr = dst_kaddr;
4842 if (areas_overlap(src_off, dst_off, len))
4847 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4849 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
4852 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4853 unsigned long src_offset, unsigned long len)
4856 size_t dst_off_in_page;
4857 size_t src_off_in_page;
4858 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4859 unsigned long dst_i;
4860 unsigned long src_i;
4862 if (src_offset + len > dst->len) {
4863 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4864 "len %lu dst len %lu\n", src_offset, len, dst->len);
4867 if (dst_offset + len > dst->len) {
4868 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4869 "len %lu dst len %lu\n", dst_offset, len, dst->len);
4874 dst_off_in_page = (start_offset + dst_offset) &
4875 ((unsigned long)PAGE_CACHE_SIZE - 1);
4876 src_off_in_page = (start_offset + src_offset) &
4877 ((unsigned long)PAGE_CACHE_SIZE - 1);
4879 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4880 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4882 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4884 cur = min_t(unsigned long, cur,
4885 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4887 copy_pages(extent_buffer_page(dst, dst_i),
4888 extent_buffer_page(dst, src_i),
4889 dst_off_in_page, src_off_in_page, cur);
4897 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4898 unsigned long src_offset, unsigned long len)
4901 size_t dst_off_in_page;
4902 size_t src_off_in_page;
4903 unsigned long dst_end = dst_offset + len - 1;
4904 unsigned long src_end = src_offset + len - 1;
4905 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4906 unsigned long dst_i;
4907 unsigned long src_i;
4909 if (src_offset + len > dst->len) {
4910 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4911 "len %lu len %lu\n", src_offset, len, dst->len);
4914 if (dst_offset + len > dst->len) {
4915 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4916 "len %lu len %lu\n", dst_offset, len, dst->len);
4919 if (dst_offset < src_offset) {
4920 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4924 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4925 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4927 dst_off_in_page = (start_offset + dst_end) &
4928 ((unsigned long)PAGE_CACHE_SIZE - 1);
4929 src_off_in_page = (start_offset + src_end) &
4930 ((unsigned long)PAGE_CACHE_SIZE - 1);
4932 cur = min_t(unsigned long, len, src_off_in_page + 1);
4933 cur = min(cur, dst_off_in_page + 1);
4934 move_pages(extent_buffer_page(dst, dst_i),
4935 extent_buffer_page(dst, src_i),
4936 dst_off_in_page - cur + 1,
4937 src_off_in_page - cur + 1, cur);
4945 int try_release_extent_buffer(struct page *page, gfp_t mask)
4947 struct extent_buffer *eb;
4950 * We need to make sure noboody is attaching this page to an eb right
4953 spin_lock(&page->mapping->private_lock);
4954 if (!PagePrivate(page)) {
4955 spin_unlock(&page->mapping->private_lock);
4959 eb = (struct extent_buffer *)page->private;
4963 * This is a little awful but should be ok, we need to make sure that
4964 * the eb doesn't disappear out from under us while we're looking at
4967 spin_lock(&eb->refs_lock);
4968 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
4969 spin_unlock(&eb->refs_lock);
4970 spin_unlock(&page->mapping->private_lock);
4973 spin_unlock(&page->mapping->private_lock);
4975 if ((mask & GFP_NOFS) == GFP_NOFS)
4979 * If tree ref isn't set then we know the ref on this eb is a real ref,
4980 * so just return, this page will likely be freed soon anyway.
4982 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4983 spin_unlock(&eb->refs_lock);
4987 return release_extent_buffer(eb, mask);