1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/module.h>
8 #include <linux/spinlock.h>
9 #include <linux/blkdev.h>
10 #include <linux/swap.h>
11 #include <linux/writeback.h>
12 #include <linux/pagevec.h>
13 #include <linux/prefetch.h>
14 #include <linux/cleancache.h>
15 #include "extent_io.h"
16 #include "extent_map.h"
19 #include "btrfs_inode.h"
21 #include "check-integrity.h"
24 static struct kmem_cache *extent_state_cache;
25 static struct kmem_cache *extent_buffer_cache;
27 static LIST_HEAD(buffers);
28 static LIST_HEAD(states);
32 static DEFINE_SPINLOCK(leak_lock);
35 #define BUFFER_LRU_MAX 64
40 struct rb_node rb_node;
43 struct extent_page_data {
45 struct extent_io_tree *tree;
46 get_extent_t *get_extent;
48 /* tells writepage not to lock the state bits for this range
49 * it still does the unlocking
51 unsigned int extent_locked:1;
53 /* tells the submit_bio code to use a WRITE_SYNC */
54 unsigned int sync_io:1;
57 static noinline void flush_write_bio(void *data);
58 static inline struct btrfs_fs_info *
59 tree_fs_info(struct extent_io_tree *tree)
61 return btrfs_sb(tree->mapping->host->i_sb);
64 int __init extent_io_init(void)
66 extent_state_cache = kmem_cache_create("extent_state",
67 sizeof(struct extent_state), 0,
68 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
69 if (!extent_state_cache)
72 extent_buffer_cache = kmem_cache_create("extent_buffers",
73 sizeof(struct extent_buffer), 0,
74 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
75 if (!extent_buffer_cache)
76 goto free_state_cache;
80 kmem_cache_destroy(extent_state_cache);
84 void extent_io_exit(void)
86 struct extent_state *state;
87 struct extent_buffer *eb;
89 while (!list_empty(&states)) {
90 state = list_entry(states.next, struct extent_state, leak_list);
91 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
92 "state %lu in tree %p refs %d\n",
93 (unsigned long long)state->start,
94 (unsigned long long)state->end,
95 state->state, state->tree, atomic_read(&state->refs));
96 list_del(&state->leak_list);
97 kmem_cache_free(extent_state_cache, state);
101 while (!list_empty(&buffers)) {
102 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
103 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
104 "refs %d\n", (unsigned long long)eb->start,
105 eb->len, atomic_read(&eb->refs));
106 list_del(&eb->leak_list);
107 kmem_cache_free(extent_buffer_cache, eb);
109 if (extent_state_cache)
110 kmem_cache_destroy(extent_state_cache);
111 if (extent_buffer_cache)
112 kmem_cache_destroy(extent_buffer_cache);
115 void extent_io_tree_init(struct extent_io_tree *tree,
116 struct address_space *mapping)
118 tree->state = RB_ROOT;
119 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
121 tree->dirty_bytes = 0;
122 spin_lock_init(&tree->lock);
123 spin_lock_init(&tree->buffer_lock);
124 tree->mapping = mapping;
127 static struct extent_state *alloc_extent_state(gfp_t mask)
129 struct extent_state *state;
134 state = kmem_cache_alloc(extent_state_cache, mask);
141 spin_lock_irqsave(&leak_lock, flags);
142 list_add(&state->leak_list, &states);
143 spin_unlock_irqrestore(&leak_lock, flags);
145 atomic_set(&state->refs, 1);
146 init_waitqueue_head(&state->wq);
147 trace_alloc_extent_state(state, mask, _RET_IP_);
151 void free_extent_state(struct extent_state *state)
155 if (atomic_dec_and_test(&state->refs)) {
159 WARN_ON(state->tree);
161 spin_lock_irqsave(&leak_lock, flags);
162 list_del(&state->leak_list);
163 spin_unlock_irqrestore(&leak_lock, flags);
165 trace_free_extent_state(state, _RET_IP_);
166 kmem_cache_free(extent_state_cache, state);
170 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
171 struct rb_node *node)
173 struct rb_node **p = &root->rb_node;
174 struct rb_node *parent = NULL;
175 struct tree_entry *entry;
179 entry = rb_entry(parent, struct tree_entry, rb_node);
181 if (offset < entry->start)
183 else if (offset > entry->end)
189 rb_link_node(node, parent, p);
190 rb_insert_color(node, root);
194 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
195 struct rb_node **prev_ret,
196 struct rb_node **next_ret)
198 struct rb_root *root = &tree->state;
199 struct rb_node *n = root->rb_node;
200 struct rb_node *prev = NULL;
201 struct rb_node *orig_prev = NULL;
202 struct tree_entry *entry;
203 struct tree_entry *prev_entry = NULL;
206 entry = rb_entry(n, struct tree_entry, rb_node);
210 if (offset < entry->start)
212 else if (offset > entry->end)
220 while (prev && offset > prev_entry->end) {
221 prev = rb_next(prev);
222 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
229 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
230 while (prev && offset < prev_entry->start) {
231 prev = rb_prev(prev);
232 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
239 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
242 struct rb_node *prev = NULL;
245 ret = __etree_search(tree, offset, &prev, NULL);
251 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
252 struct extent_state *other)
254 if (tree->ops && tree->ops->merge_extent_hook)
255 tree->ops->merge_extent_hook(tree->mapping->host, new,
260 * utility function to look for merge candidates inside a given range.
261 * Any extents with matching state are merged together into a single
262 * extent in the tree. Extents with EXTENT_IO in their state field
263 * are not merged because the end_io handlers need to be able to do
264 * operations on them without sleeping (or doing allocations/splits).
266 * This should be called with the tree lock held.
268 static void merge_state(struct extent_io_tree *tree,
269 struct extent_state *state)
271 struct extent_state *other;
272 struct rb_node *other_node;
274 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
277 other_node = rb_prev(&state->rb_node);
279 other = rb_entry(other_node, struct extent_state, rb_node);
280 if (other->end == state->start - 1 &&
281 other->state == state->state) {
282 merge_cb(tree, state, other);
283 state->start = other->start;
285 rb_erase(&other->rb_node, &tree->state);
286 free_extent_state(other);
289 other_node = rb_next(&state->rb_node);
291 other = rb_entry(other_node, struct extent_state, rb_node);
292 if (other->start == state->end + 1 &&
293 other->state == state->state) {
294 merge_cb(tree, state, other);
295 state->end = other->end;
297 rb_erase(&other->rb_node, &tree->state);
298 free_extent_state(other);
303 static void set_state_cb(struct extent_io_tree *tree,
304 struct extent_state *state, int *bits)
306 if (tree->ops && tree->ops->set_bit_hook)
307 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
310 static void clear_state_cb(struct extent_io_tree *tree,
311 struct extent_state *state, int *bits)
313 if (tree->ops && tree->ops->clear_bit_hook)
314 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
317 static void set_state_bits(struct extent_io_tree *tree,
318 struct extent_state *state, int *bits);
321 * insert an extent_state struct into the tree. 'bits' are set on the
322 * struct before it is inserted.
324 * This may return -EEXIST if the extent is already there, in which case the
325 * state struct is freed.
327 * The tree lock is not taken internally. This is a utility function and
328 * probably isn't what you want to call (see set/clear_extent_bit).
330 static int insert_state(struct extent_io_tree *tree,
331 struct extent_state *state, u64 start, u64 end,
334 struct rb_node *node;
337 printk(KERN_ERR "btrfs end < start %llu %llu\n",
338 (unsigned long long)end,
339 (unsigned long long)start);
342 state->start = start;
345 set_state_bits(tree, state, bits);
347 node = tree_insert(&tree->state, end, &state->rb_node);
349 struct extent_state *found;
350 found = rb_entry(node, struct extent_state, rb_node);
351 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
352 "%llu %llu\n", (unsigned long long)found->start,
353 (unsigned long long)found->end,
354 (unsigned long long)start, (unsigned long long)end);
358 merge_state(tree, state);
362 static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
365 if (tree->ops && tree->ops->split_extent_hook)
366 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
370 * split a given extent state struct in two, inserting the preallocated
371 * struct 'prealloc' as the newly created second half. 'split' indicates an
372 * offset inside 'orig' where it should be split.
375 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
376 * are two extent state structs in the tree:
377 * prealloc: [orig->start, split - 1]
378 * orig: [ split, orig->end ]
380 * The tree locks are not taken by this function. They need to be held
383 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
384 struct extent_state *prealloc, u64 split)
386 struct rb_node *node;
388 split_cb(tree, orig, split);
390 prealloc->start = orig->start;
391 prealloc->end = split - 1;
392 prealloc->state = orig->state;
395 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
397 free_extent_state(prealloc);
400 prealloc->tree = tree;
404 static struct extent_state *next_state(struct extent_state *state)
406 struct rb_node *next = rb_next(&state->rb_node);
408 return rb_entry(next, struct extent_state, rb_node);
414 * utility function to clear some bits in an extent state struct.
415 * it will optionally wake up any one waiting on this state (wake == 1).
417 * If no bits are set on the state struct after clearing things, the
418 * struct is freed and removed from the tree
420 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
421 struct extent_state *state,
424 struct extent_state *next;
425 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
427 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
428 u64 range = state->end - state->start + 1;
429 WARN_ON(range > tree->dirty_bytes);
430 tree->dirty_bytes -= range;
432 clear_state_cb(tree, state, bits);
433 state->state &= ~bits_to_clear;
436 if (state->state == 0) {
437 next = next_state(state);
439 rb_erase(&state->rb_node, &tree->state);
441 free_extent_state(state);
446 merge_state(tree, state);
447 next = next_state(state);
452 static struct extent_state *
453 alloc_extent_state_atomic(struct extent_state *prealloc)
456 prealloc = alloc_extent_state(GFP_ATOMIC);
461 void extent_io_tree_panic(struct extent_io_tree *tree, int err)
463 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
464 "Extent tree was modified by another "
465 "thread while locked.");
469 * clear some bits on a range in the tree. This may require splitting
470 * or inserting elements in the tree, so the gfp mask is used to
471 * indicate which allocations or sleeping are allowed.
473 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
474 * the given range from the tree regardless of state (ie for truncate).
476 * the range [start, end] is inclusive.
478 * This takes the tree lock, and returns 0 on success and < 0 on error.
480 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
481 int bits, int wake, int delete,
482 struct extent_state **cached_state,
485 struct extent_state *state;
486 struct extent_state *cached;
487 struct extent_state *prealloc = NULL;
488 struct rb_node *node;
494 bits |= ~EXTENT_CTLBITS;
495 bits |= EXTENT_FIRST_DELALLOC;
497 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
500 if (!prealloc && (mask & __GFP_WAIT)) {
501 prealloc = alloc_extent_state(mask);
506 spin_lock(&tree->lock);
508 cached = *cached_state;
511 *cached_state = NULL;
515 if (cached && cached->tree && cached->start <= start &&
516 cached->end > start) {
518 atomic_dec(&cached->refs);
523 free_extent_state(cached);
526 * this search will find the extents that end after
529 node = tree_search(tree, start);
532 state = rb_entry(node, struct extent_state, rb_node);
534 if (state->start > end)
536 WARN_ON(state->end < start);
537 last_end = state->end;
539 /* the state doesn't have the wanted bits, go ahead */
540 if (!(state->state & bits)) {
541 state = next_state(state);
546 * | ---- desired range ---- |
548 * | ------------- state -------------- |
550 * We need to split the extent we found, and may flip
551 * bits on second half.
553 * If the extent we found extends past our range, we
554 * just split and search again. It'll get split again
555 * the next time though.
557 * If the extent we found is inside our range, we clear
558 * the desired bit on it.
561 if (state->start < start) {
562 prealloc = alloc_extent_state_atomic(prealloc);
564 err = split_state(tree, state, prealloc, start);
566 extent_io_tree_panic(tree, err);
571 if (state->end <= end) {
572 state = clear_state_bit(tree, state, &bits, wake);
578 * | ---- desired range ---- |
580 * We need to split the extent, and clear the bit
583 if (state->start <= end && state->end > end) {
584 prealloc = alloc_extent_state_atomic(prealloc);
586 err = split_state(tree, state, prealloc, end + 1);
588 extent_io_tree_panic(tree, err);
593 clear_state_bit(tree, prealloc, &bits, wake);
599 state = clear_state_bit(tree, state, &bits, wake);
601 if (last_end == (u64)-1)
603 start = last_end + 1;
604 if (start <= end && state && !need_resched())
609 spin_unlock(&tree->lock);
611 free_extent_state(prealloc);
618 spin_unlock(&tree->lock);
619 if (mask & __GFP_WAIT)
624 static void wait_on_state(struct extent_io_tree *tree,
625 struct extent_state *state)
626 __releases(tree->lock)
627 __acquires(tree->lock)
630 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
631 spin_unlock(&tree->lock);
633 spin_lock(&tree->lock);
634 finish_wait(&state->wq, &wait);
638 * waits for one or more bits to clear on a range in the state tree.
639 * The range [start, end] is inclusive.
640 * The tree lock is taken by this function
642 void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
644 struct extent_state *state;
645 struct rb_node *node;
647 spin_lock(&tree->lock);
651 * this search will find all the extents that end after
654 node = tree_search(tree, start);
658 state = rb_entry(node, struct extent_state, rb_node);
660 if (state->start > end)
663 if (state->state & bits) {
664 start = state->start;
665 atomic_inc(&state->refs);
666 wait_on_state(tree, state);
667 free_extent_state(state);
670 start = state->end + 1;
675 cond_resched_lock(&tree->lock);
678 spin_unlock(&tree->lock);
681 static void set_state_bits(struct extent_io_tree *tree,
682 struct extent_state *state,
685 int bits_to_set = *bits & ~EXTENT_CTLBITS;
687 set_state_cb(tree, state, bits);
688 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
689 u64 range = state->end - state->start + 1;
690 tree->dirty_bytes += range;
692 state->state |= bits_to_set;
695 static void cache_state(struct extent_state *state,
696 struct extent_state **cached_ptr)
698 if (cached_ptr && !(*cached_ptr)) {
699 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
701 atomic_inc(&state->refs);
706 static void uncache_state(struct extent_state **cached_ptr)
708 if (cached_ptr && (*cached_ptr)) {
709 struct extent_state *state = *cached_ptr;
711 free_extent_state(state);
716 * set some bits on a range in the tree. This may require allocations or
717 * sleeping, so the gfp mask is used to indicate what is allowed.
719 * If any of the exclusive bits are set, this will fail with -EEXIST if some
720 * part of the range already has the desired bits set. The start of the
721 * existing range is returned in failed_start in this case.
723 * [start, end] is inclusive This takes the tree lock.
726 static int __must_check
727 __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
728 int bits, int exclusive_bits, u64 *failed_start,
729 struct extent_state **cached_state, gfp_t mask)
731 struct extent_state *state;
732 struct extent_state *prealloc = NULL;
733 struct rb_node *node;
738 bits |= EXTENT_FIRST_DELALLOC;
740 if (!prealloc && (mask & __GFP_WAIT)) {
741 prealloc = alloc_extent_state(mask);
745 spin_lock(&tree->lock);
746 if (cached_state && *cached_state) {
747 state = *cached_state;
748 if (state->start <= start && state->end > start &&
750 node = &state->rb_node;
755 * this search will find all the extents that end after
758 node = tree_search(tree, start);
760 prealloc = alloc_extent_state_atomic(prealloc);
762 err = insert_state(tree, prealloc, start, end, &bits);
764 extent_io_tree_panic(tree, err);
769 state = rb_entry(node, struct extent_state, rb_node);
771 last_start = state->start;
772 last_end = state->end;
775 * | ---- desired range ---- |
778 * Just lock what we found and keep going
780 if (state->start == start && state->end <= end) {
781 if (state->state & exclusive_bits) {
782 *failed_start = state->start;
787 set_state_bits(tree, state, &bits);
788 cache_state(state, cached_state);
789 merge_state(tree, state);
790 if (last_end == (u64)-1)
792 start = last_end + 1;
793 state = next_state(state);
794 if (start < end && state && state->start == start &&
801 * | ---- desired range ---- |
804 * | ------------- state -------------- |
806 * We need to split the extent we found, and may flip bits on
809 * If the extent we found extends past our
810 * range, we just split and search again. It'll get split
811 * again the next time though.
813 * If the extent we found is inside our range, we set the
816 if (state->start < start) {
817 if (state->state & exclusive_bits) {
818 *failed_start = start;
823 prealloc = alloc_extent_state_atomic(prealloc);
825 err = split_state(tree, state, prealloc, start);
827 extent_io_tree_panic(tree, err);
832 if (state->end <= end) {
833 set_state_bits(tree, state, &bits);
834 cache_state(state, cached_state);
835 merge_state(tree, state);
836 if (last_end == (u64)-1)
838 start = last_end + 1;
839 state = next_state(state);
840 if (start < end && state && state->start == start &&
847 * | ---- desired range ---- |
848 * | state | or | state |
850 * There's a hole, we need to insert something in it and
851 * ignore the extent we found.
853 if (state->start > start) {
855 if (end < last_start)
858 this_end = last_start - 1;
860 prealloc = alloc_extent_state_atomic(prealloc);
864 * Avoid to free 'prealloc' if it can be merged with
867 err = insert_state(tree, prealloc, start, this_end,
870 extent_io_tree_panic(tree, err);
872 cache_state(prealloc, cached_state);
874 start = this_end + 1;
878 * | ---- desired range ---- |
880 * We need to split the extent, and set the bit
883 if (state->start <= end && state->end > end) {
884 if (state->state & exclusive_bits) {
885 *failed_start = start;
890 prealloc = alloc_extent_state_atomic(prealloc);
892 err = split_state(tree, state, prealloc, end + 1);
894 extent_io_tree_panic(tree, err);
896 set_state_bits(tree, prealloc, &bits);
897 cache_state(prealloc, cached_state);
898 merge_state(tree, prealloc);
906 spin_unlock(&tree->lock);
908 free_extent_state(prealloc);
915 spin_unlock(&tree->lock);
916 if (mask & __GFP_WAIT)
921 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
922 u64 *failed_start, struct extent_state **cached_state,
925 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
931 * convert_extent - convert all bits in a given range from one bit to another
932 * @tree: the io tree to search
933 * @start: the start offset in bytes
934 * @end: the end offset in bytes (inclusive)
935 * @bits: the bits to set in this range
936 * @clear_bits: the bits to clear in this range
937 * @mask: the allocation mask
939 * This will go through and set bits for the given range. If any states exist
940 * already in this range they are set with the given bit and cleared of the
941 * clear_bits. This is only meant to be used by things that are mergeable, ie
942 * converting from say DELALLOC to DIRTY. This is not meant to be used with
943 * boundary bits like LOCK.
945 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
946 int bits, int clear_bits, gfp_t mask)
948 struct extent_state *state;
949 struct extent_state *prealloc = NULL;
950 struct rb_node *node;
956 if (!prealloc && (mask & __GFP_WAIT)) {
957 prealloc = alloc_extent_state(mask);
962 spin_lock(&tree->lock);
964 * this search will find all the extents that end after
967 node = tree_search(tree, start);
969 prealloc = alloc_extent_state_atomic(prealloc);
974 err = insert_state(tree, prealloc, start, end, &bits);
977 extent_io_tree_panic(tree, err);
980 state = rb_entry(node, struct extent_state, rb_node);
982 last_start = state->start;
983 last_end = state->end;
986 * | ---- desired range ---- |
989 * Just lock what we found and keep going
991 if (state->start == start && state->end <= end) {
992 set_state_bits(tree, state, &bits);
993 state = clear_state_bit(tree, state, &clear_bits, 0);
994 if (last_end == (u64)-1)
996 start = last_end + 1;
997 if (start < end && state && state->start == start &&
1004 * | ---- desired range ---- |
1007 * | ------------- state -------------- |
1009 * We need to split the extent we found, and may flip bits on
1012 * If the extent we found extends past our
1013 * range, we just split and search again. It'll get split
1014 * again the next time though.
1016 * If the extent we found is inside our range, we set the
1017 * desired bit on it.
1019 if (state->start < start) {
1020 prealloc = alloc_extent_state_atomic(prealloc);
1025 err = split_state(tree, state, prealloc, start);
1027 extent_io_tree_panic(tree, err);
1031 if (state->end <= end) {
1032 set_state_bits(tree, state, &bits);
1033 state = clear_state_bit(tree, state, &clear_bits, 0);
1034 if (last_end == (u64)-1)
1036 start = last_end + 1;
1037 if (start < end && state && state->start == start &&
1044 * | ---- desired range ---- |
1045 * | state | or | state |
1047 * There's a hole, we need to insert something in it and
1048 * ignore the extent we found.
1050 if (state->start > start) {
1052 if (end < last_start)
1055 this_end = last_start - 1;
1057 prealloc = alloc_extent_state_atomic(prealloc);
1064 * Avoid to free 'prealloc' if it can be merged with
1067 err = insert_state(tree, prealloc, start, this_end,
1070 extent_io_tree_panic(tree, err);
1072 start = this_end + 1;
1076 * | ---- desired range ---- |
1078 * We need to split the extent, and set the bit
1081 if (state->start <= end && state->end > end) {
1082 prealloc = alloc_extent_state_atomic(prealloc);
1088 err = split_state(tree, state, prealloc, end + 1);
1090 extent_io_tree_panic(tree, err);
1092 set_state_bits(tree, prealloc, &bits);
1093 clear_state_bit(tree, prealloc, &clear_bits, 0);
1101 spin_unlock(&tree->lock);
1103 free_extent_state(prealloc);
1110 spin_unlock(&tree->lock);
1111 if (mask & __GFP_WAIT)
1116 /* wrappers around set/clear extent bit */
1117 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1120 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
1124 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1125 int bits, gfp_t mask)
1127 return set_extent_bit(tree, start, end, bits, NULL,
1131 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1132 int bits, gfp_t mask)
1134 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
1137 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
1138 struct extent_state **cached_state, gfp_t mask)
1140 return set_extent_bit(tree, start, end,
1141 EXTENT_DELALLOC | EXTENT_UPTODATE,
1142 NULL, cached_state, mask);
1145 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1148 return clear_extent_bit(tree, start, end,
1149 EXTENT_DIRTY | EXTENT_DELALLOC |
1150 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
1153 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1156 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
1160 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1161 struct extent_state **cached_state, gfp_t mask)
1163 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1164 cached_state, mask);
1167 int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1168 struct extent_state **cached_state, gfp_t mask)
1170 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1171 cached_state, mask);
1175 * either insert or lock state struct between start and end use mask to tell
1176 * us if waiting is desired.
1178 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1179 int bits, struct extent_state **cached_state)
1184 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1185 EXTENT_LOCKED, &failed_start,
1186 cached_state, GFP_NOFS);
1187 if (err == -EEXIST) {
1188 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1189 start = failed_start;
1192 WARN_ON(start > end);
1197 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1199 return lock_extent_bits(tree, start, end, 0, NULL);
1202 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1207 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1208 &failed_start, NULL, GFP_NOFS);
1209 if (err == -EEXIST) {
1210 if (failed_start > start)
1211 clear_extent_bit(tree, start, failed_start - 1,
1212 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
1218 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1219 struct extent_state **cached, gfp_t mask)
1221 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1225 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1227 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1232 * helper function to set both pages and extents in the tree writeback
1234 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1236 unsigned long index = start >> PAGE_CACHE_SHIFT;
1237 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1240 while (index <= end_index) {
1241 page = find_get_page(tree->mapping, index);
1242 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1243 set_page_writeback(page);
1244 page_cache_release(page);
1250 /* find the first state struct with 'bits' set after 'start', and
1251 * return it. tree->lock must be held. NULL will returned if
1252 * nothing was found after 'start'
1254 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1255 u64 start, int bits)
1257 struct rb_node *node;
1258 struct extent_state *state;
1261 * this search will find all the extents that end after
1264 node = tree_search(tree, start);
1269 state = rb_entry(node, struct extent_state, rb_node);
1270 if (state->end >= start && (state->state & bits))
1273 node = rb_next(node);
1282 * find the first offset in the io tree with 'bits' set. zero is
1283 * returned if we find something, and *start_ret and *end_ret are
1284 * set to reflect the state struct that was found.
1286 * If nothing was found, 1 is returned. If found something, return 0.
1288 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1289 u64 *start_ret, u64 *end_ret, int bits)
1291 struct extent_state *state;
1294 spin_lock(&tree->lock);
1295 state = find_first_extent_bit_state(tree, start, bits);
1297 *start_ret = state->start;
1298 *end_ret = state->end;
1301 spin_unlock(&tree->lock);
1306 * find a contiguous range of bytes in the file marked as delalloc, not
1307 * more than 'max_bytes'. start and end are used to return the range,
1309 * 1 is returned if we find something, 0 if nothing was in the tree
1311 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1312 u64 *start, u64 *end, u64 max_bytes,
1313 struct extent_state **cached_state)
1315 struct rb_node *node;
1316 struct extent_state *state;
1317 u64 cur_start = *start;
1319 u64 total_bytes = 0;
1321 spin_lock(&tree->lock);
1324 * this search will find all the extents that end after
1327 node = tree_search(tree, cur_start);
1335 state = rb_entry(node, struct extent_state, rb_node);
1336 if (found && (state->start != cur_start ||
1337 (state->state & EXTENT_BOUNDARY))) {
1340 if (!(state->state & EXTENT_DELALLOC)) {
1346 *start = state->start;
1347 *cached_state = state;
1348 atomic_inc(&state->refs);
1352 cur_start = state->end + 1;
1353 node = rb_next(node);
1356 total_bytes += state->end - state->start + 1;
1357 if (total_bytes >= max_bytes)
1361 spin_unlock(&tree->lock);
1365 static noinline void __unlock_for_delalloc(struct inode *inode,
1366 struct page *locked_page,
1370 struct page *pages[16];
1371 unsigned long index = start >> PAGE_CACHE_SHIFT;
1372 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1373 unsigned long nr_pages = end_index - index + 1;
1376 if (index == locked_page->index && end_index == index)
1379 while (nr_pages > 0) {
1380 ret = find_get_pages_contig(inode->i_mapping, index,
1381 min_t(unsigned long, nr_pages,
1382 ARRAY_SIZE(pages)), pages);
1383 for (i = 0; i < ret; i++) {
1384 if (pages[i] != locked_page)
1385 unlock_page(pages[i]);
1386 page_cache_release(pages[i]);
1394 static noinline int lock_delalloc_pages(struct inode *inode,
1395 struct page *locked_page,
1399 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1400 unsigned long start_index = index;
1401 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1402 unsigned long pages_locked = 0;
1403 struct page *pages[16];
1404 unsigned long nrpages;
1408 /* the caller is responsible for locking the start index */
1409 if (index == locked_page->index && index == end_index)
1412 /* skip the page at the start index */
1413 nrpages = end_index - index + 1;
1414 while (nrpages > 0) {
1415 ret = find_get_pages_contig(inode->i_mapping, index,
1416 min_t(unsigned long,
1417 nrpages, ARRAY_SIZE(pages)), pages);
1422 /* now we have an array of pages, lock them all */
1423 for (i = 0; i < ret; i++) {
1425 * the caller is taking responsibility for
1428 if (pages[i] != locked_page) {
1429 lock_page(pages[i]);
1430 if (!PageDirty(pages[i]) ||
1431 pages[i]->mapping != inode->i_mapping) {
1433 unlock_page(pages[i]);
1434 page_cache_release(pages[i]);
1438 page_cache_release(pages[i]);
1447 if (ret && pages_locked) {
1448 __unlock_for_delalloc(inode, locked_page,
1450 ((u64)(start_index + pages_locked - 1)) <<
1457 * find a contiguous range of bytes in the file marked as delalloc, not
1458 * more than 'max_bytes'. start and end are used to return the range,
1460 * 1 is returned if we find something, 0 if nothing was in the tree
1462 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1463 struct extent_io_tree *tree,
1464 struct page *locked_page,
1465 u64 *start, u64 *end,
1471 struct extent_state *cached_state = NULL;
1476 /* step one, find a bunch of delalloc bytes starting at start */
1477 delalloc_start = *start;
1479 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1480 max_bytes, &cached_state);
1481 if (!found || delalloc_end <= *start) {
1482 *start = delalloc_start;
1483 *end = delalloc_end;
1484 free_extent_state(cached_state);
1489 * start comes from the offset of locked_page. We have to lock
1490 * pages in order, so we can't process delalloc bytes before
1493 if (delalloc_start < *start)
1494 delalloc_start = *start;
1497 * make sure to limit the number of pages we try to lock down
1500 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1501 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1503 /* step two, lock all the pages after the page that has start */
1504 ret = lock_delalloc_pages(inode, locked_page,
1505 delalloc_start, delalloc_end);
1506 if (ret == -EAGAIN) {
1507 /* some of the pages are gone, lets avoid looping by
1508 * shortening the size of the delalloc range we're searching
1510 free_extent_state(cached_state);
1512 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1513 max_bytes = PAGE_CACHE_SIZE - offset;
1521 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
1523 /* step three, lock the state bits for the whole range */
1524 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
1526 /* then test to make sure it is all still delalloc */
1527 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1528 EXTENT_DELALLOC, 1, cached_state);
1530 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1531 &cached_state, GFP_NOFS);
1532 __unlock_for_delalloc(inode, locked_page,
1533 delalloc_start, delalloc_end);
1537 free_extent_state(cached_state);
1538 *start = delalloc_start;
1539 *end = delalloc_end;
1544 int extent_clear_unlock_delalloc(struct inode *inode,
1545 struct extent_io_tree *tree,
1546 u64 start, u64 end, struct page *locked_page,
1550 struct page *pages[16];
1551 unsigned long index = start >> PAGE_CACHE_SHIFT;
1552 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1553 unsigned long nr_pages = end_index - index + 1;
1557 if (op & EXTENT_CLEAR_UNLOCK)
1558 clear_bits |= EXTENT_LOCKED;
1559 if (op & EXTENT_CLEAR_DIRTY)
1560 clear_bits |= EXTENT_DIRTY;
1562 if (op & EXTENT_CLEAR_DELALLOC)
1563 clear_bits |= EXTENT_DELALLOC;
1565 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1566 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1567 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1568 EXTENT_SET_PRIVATE2)))
1571 while (nr_pages > 0) {
1572 ret = find_get_pages_contig(inode->i_mapping, index,
1573 min_t(unsigned long,
1574 nr_pages, ARRAY_SIZE(pages)), pages);
1575 for (i = 0; i < ret; i++) {
1577 if (op & EXTENT_SET_PRIVATE2)
1578 SetPagePrivate2(pages[i]);
1580 if (pages[i] == locked_page) {
1581 page_cache_release(pages[i]);
1584 if (op & EXTENT_CLEAR_DIRTY)
1585 clear_page_dirty_for_io(pages[i]);
1586 if (op & EXTENT_SET_WRITEBACK)
1587 set_page_writeback(pages[i]);
1588 if (op & EXTENT_END_WRITEBACK)
1589 end_page_writeback(pages[i]);
1590 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1591 unlock_page(pages[i]);
1592 page_cache_release(pages[i]);
1602 * count the number of bytes in the tree that have a given bit(s)
1603 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1604 * cached. The total number found is returned.
1606 u64 count_range_bits(struct extent_io_tree *tree,
1607 u64 *start, u64 search_end, u64 max_bytes,
1608 unsigned long bits, int contig)
1610 struct rb_node *node;
1611 struct extent_state *state;
1612 u64 cur_start = *start;
1613 u64 total_bytes = 0;
1617 if (search_end <= cur_start) {
1622 spin_lock(&tree->lock);
1623 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1624 total_bytes = tree->dirty_bytes;
1628 * this search will find all the extents that end after
1631 node = tree_search(tree, cur_start);
1636 state = rb_entry(node, struct extent_state, rb_node);
1637 if (state->start > search_end)
1639 if (contig && found && state->start > last + 1)
1641 if (state->end >= cur_start && (state->state & bits) == bits) {
1642 total_bytes += min(search_end, state->end) + 1 -
1643 max(cur_start, state->start);
1644 if (total_bytes >= max_bytes)
1647 *start = max(cur_start, state->start);
1651 } else if (contig && found) {
1654 node = rb_next(node);
1659 spin_unlock(&tree->lock);
1664 * set the private field for a given byte offset in the tree. If there isn't
1665 * an extent_state there already, this does nothing.
1667 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1669 struct rb_node *node;
1670 struct extent_state *state;
1673 spin_lock(&tree->lock);
1675 * this search will find all the extents that end after
1678 node = tree_search(tree, start);
1683 state = rb_entry(node, struct extent_state, rb_node);
1684 if (state->start != start) {
1688 state->private = private;
1690 spin_unlock(&tree->lock);
1694 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1696 struct rb_node *node;
1697 struct extent_state *state;
1700 spin_lock(&tree->lock);
1702 * this search will find all the extents that end after
1705 node = tree_search(tree, start);
1710 state = rb_entry(node, struct extent_state, rb_node);
1711 if (state->start != start) {
1715 *private = state->private;
1717 spin_unlock(&tree->lock);
1722 * searches a range in the state tree for a given mask.
1723 * If 'filled' == 1, this returns 1 only if every extent in the tree
1724 * has the bits set. Otherwise, 1 is returned if any bit in the
1725 * range is found set.
1727 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1728 int bits, int filled, struct extent_state *cached)
1730 struct extent_state *state = NULL;
1731 struct rb_node *node;
1734 spin_lock(&tree->lock);
1735 if (cached && cached->tree && cached->start <= start &&
1736 cached->end > start)
1737 node = &cached->rb_node;
1739 node = tree_search(tree, start);
1740 while (node && start <= end) {
1741 state = rb_entry(node, struct extent_state, rb_node);
1743 if (filled && state->start > start) {
1748 if (state->start > end)
1751 if (state->state & bits) {
1755 } else if (filled) {
1760 if (state->end == (u64)-1)
1763 start = state->end + 1;
1766 node = rb_next(node);
1773 spin_unlock(&tree->lock);
1778 * helper function to set a given page up to date if all the
1779 * extents in the tree for that page are up to date
1781 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
1783 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1784 u64 end = start + PAGE_CACHE_SIZE - 1;
1785 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1786 SetPageUptodate(page);
1790 * helper function to unlock a page if all the extents in the tree
1791 * for that page are unlocked
1793 static void check_page_locked(struct extent_io_tree *tree, struct page *page)
1795 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1796 u64 end = start + PAGE_CACHE_SIZE - 1;
1797 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1802 * helper function to end page writeback if all the extents
1803 * in the tree for that page are done with writeback
1805 static void check_page_writeback(struct extent_io_tree *tree,
1808 end_page_writeback(page);
1812 * When IO fails, either with EIO or csum verification fails, we
1813 * try other mirrors that might have a good copy of the data. This
1814 * io_failure_record is used to record state as we go through all the
1815 * mirrors. If another mirror has good data, the page is set up to date
1816 * and things continue. If a good mirror can't be found, the original
1817 * bio end_io callback is called to indicate things have failed.
1819 struct io_failure_record {
1824 unsigned long bio_flags;
1830 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1835 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1837 set_state_private(failure_tree, rec->start, 0);
1838 ret = clear_extent_bits(failure_tree, rec->start,
1839 rec->start + rec->len - 1,
1840 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1845 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1846 rec->start + rec->len - 1,
1847 EXTENT_DAMAGED, GFP_NOFS);
1856 static void repair_io_failure_callback(struct bio *bio, int err)
1858 complete(bio->bi_private);
1862 * this bypasses the standard btrfs submit functions deliberately, as
1863 * the standard behavior is to write all copies in a raid setup. here we only
1864 * want to write the one bad copy. so we do the mapping for ourselves and issue
1865 * submit_bio directly.
1866 * to avoid any synchonization issues, wait for the data after writing, which
1867 * actually prevents the read that triggered the error from finishing.
1868 * currently, there can be no more than two copies of every data bit. thus,
1869 * exactly one rewrite is required.
1871 int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1872 u64 length, u64 logical, struct page *page,
1876 struct btrfs_device *dev;
1877 DECLARE_COMPLETION_ONSTACK(compl);
1880 struct btrfs_bio *bbio = NULL;
1883 BUG_ON(!mirror_num);
1885 bio = bio_alloc(GFP_NOFS, 1);
1888 bio->bi_private = &compl;
1889 bio->bi_end_io = repair_io_failure_callback;
1891 map_length = length;
1893 ret = btrfs_map_block(map_tree, WRITE, logical,
1894 &map_length, &bbio, mirror_num);
1899 BUG_ON(mirror_num != bbio->mirror_num);
1900 sector = bbio->stripes[mirror_num-1].physical >> 9;
1901 bio->bi_sector = sector;
1902 dev = bbio->stripes[mirror_num-1].dev;
1904 if (!dev || !dev->bdev || !dev->writeable) {
1908 bio->bi_bdev = dev->bdev;
1909 bio_add_page(bio, page, length, start-page_offset(page));
1910 btrfsic_submit_bio(WRITE_SYNC, bio);
1911 wait_for_completion(&compl);
1913 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1914 /* try to remap that extent elsewhere? */
1916 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
1920 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1921 "sector %llu)\n", page->mapping->host->i_ino, start,
1928 int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1931 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1932 u64 start = eb->start;
1933 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
1936 for (i = 0; i < num_pages; i++) {
1937 struct page *p = extent_buffer_page(eb, i);
1938 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1939 start, p, mirror_num);
1942 start += PAGE_CACHE_SIZE;
1949 * each time an IO finishes, we do a fast check in the IO failure tree
1950 * to see if we need to process or clean up an io_failure_record
1952 static int clean_io_failure(u64 start, struct page *page)
1955 u64 private_failure;
1956 struct io_failure_record *failrec;
1957 struct btrfs_mapping_tree *map_tree;
1958 struct extent_state *state;
1962 struct inode *inode = page->mapping->host;
1965 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1966 (u64)-1, 1, EXTENT_DIRTY, 0);
1970 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1975 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1976 BUG_ON(!failrec->this_mirror);
1978 if (failrec->in_validation) {
1979 /* there was no real error, just free the record */
1980 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1986 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1987 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1990 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1992 if (state && state->start == failrec->start) {
1993 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1994 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1996 if (num_copies > 1) {
1997 ret = repair_io_failure(map_tree, start, failrec->len,
1998 failrec->logical, page,
1999 failrec->failed_mirror);
2006 ret = free_io_failure(inode, failrec, did_repair);
2012 * this is a generic handler for readpage errors (default
2013 * readpage_io_failed_hook). if other copies exist, read those and write back
2014 * good data to the failed position. does not investigate in remapping the
2015 * failed extent elsewhere, hoping the device will be smart enough to do this as
2019 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2020 u64 start, u64 end, int failed_mirror,
2021 struct extent_state *state)
2023 struct io_failure_record *failrec = NULL;
2025 struct extent_map *em;
2026 struct inode *inode = page->mapping->host;
2027 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2028 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2029 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2036 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2038 ret = get_state_private(failure_tree, start, &private);
2040 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2043 failrec->start = start;
2044 failrec->len = end - start + 1;
2045 failrec->this_mirror = 0;
2046 failrec->bio_flags = 0;
2047 failrec->in_validation = 0;
2049 read_lock(&em_tree->lock);
2050 em = lookup_extent_mapping(em_tree, start, failrec->len);
2052 read_unlock(&em_tree->lock);
2057 if (em->start > start || em->start + em->len < start) {
2058 free_extent_map(em);
2061 read_unlock(&em_tree->lock);
2063 if (!em || IS_ERR(em)) {
2067 logical = start - em->start;
2068 logical = em->block_start + logical;
2069 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2070 logical = em->block_start;
2071 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2072 extent_set_compress_type(&failrec->bio_flags,
2075 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2076 "len=%llu\n", logical, start, failrec->len);
2077 failrec->logical = logical;
2078 free_extent_map(em);
2080 /* set the bits in the private failure tree */
2081 ret = set_extent_bits(failure_tree, start, end,
2082 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2084 ret = set_state_private(failure_tree, start,
2085 (u64)(unsigned long)failrec);
2086 /* set the bits in the inode's tree */
2088 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2095 failrec = (struct io_failure_record *)(unsigned long)private;
2096 pr_debug("bio_readpage_error: (found) logical=%llu, "
2097 "start=%llu, len=%llu, validation=%d\n",
2098 failrec->logical, failrec->start, failrec->len,
2099 failrec->in_validation);
2101 * when data can be on disk more than twice, add to failrec here
2102 * (e.g. with a list for failed_mirror) to make
2103 * clean_io_failure() clean all those errors at once.
2106 num_copies = btrfs_num_copies(
2107 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2108 failrec->logical, failrec->len);
2109 if (num_copies == 1) {
2111 * we only have a single copy of the data, so don't bother with
2112 * all the retry and error correction code that follows. no
2113 * matter what the error is, it is very likely to persist.
2115 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2116 "state=%p, num_copies=%d, next_mirror %d, "
2117 "failed_mirror %d\n", state, num_copies,
2118 failrec->this_mirror, failed_mirror);
2119 free_io_failure(inode, failrec, 0);
2124 spin_lock(&tree->lock);
2125 state = find_first_extent_bit_state(tree, failrec->start,
2127 if (state && state->start != failrec->start)
2129 spin_unlock(&tree->lock);
2133 * there are two premises:
2134 * a) deliver good data to the caller
2135 * b) correct the bad sectors on disk
2137 if (failed_bio->bi_vcnt > 1) {
2139 * to fulfill b), we need to know the exact failing sectors, as
2140 * we don't want to rewrite any more than the failed ones. thus,
2141 * we need separate read requests for the failed bio
2143 * if the following BUG_ON triggers, our validation request got
2144 * merged. we need separate requests for our algorithm to work.
2146 BUG_ON(failrec->in_validation);
2147 failrec->in_validation = 1;
2148 failrec->this_mirror = failed_mirror;
2149 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2152 * we're ready to fulfill a) and b) alongside. get a good copy
2153 * of the failed sector and if we succeed, we have setup
2154 * everything for repair_io_failure to do the rest for us.
2156 if (failrec->in_validation) {
2157 BUG_ON(failrec->this_mirror != failed_mirror);
2158 failrec->in_validation = 0;
2159 failrec->this_mirror = 0;
2161 failrec->failed_mirror = failed_mirror;
2162 failrec->this_mirror++;
2163 if (failrec->this_mirror == failed_mirror)
2164 failrec->this_mirror++;
2165 read_mode = READ_SYNC;
2168 if (!state || failrec->this_mirror > num_copies) {
2169 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2170 "next_mirror %d, failed_mirror %d\n", state,
2171 num_copies, failrec->this_mirror, failed_mirror);
2172 free_io_failure(inode, failrec, 0);
2176 bio = bio_alloc(GFP_NOFS, 1);
2178 free_io_failure(inode, failrec, 0);
2181 bio->bi_private = state;
2182 bio->bi_end_io = failed_bio->bi_end_io;
2183 bio->bi_sector = failrec->logical >> 9;
2184 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2187 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2189 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2190 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2191 failrec->this_mirror, num_copies, failrec->in_validation);
2193 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2194 failrec->this_mirror,
2195 failrec->bio_flags, 0);
2199 /* lots and lots of room for performance fixes in the end_bio funcs */
2201 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2203 int uptodate = (err == 0);
2204 struct extent_io_tree *tree;
2207 tree = &BTRFS_I(page->mapping->host)->io_tree;
2209 if (tree->ops && tree->ops->writepage_end_io_hook) {
2210 ret = tree->ops->writepage_end_io_hook(page, start,
2211 end, NULL, uptodate);
2217 ClearPageUptodate(page);
2224 * after a writepage IO is done, we need to:
2225 * clear the uptodate bits on error
2226 * clear the writeback bits in the extent tree for this IO
2227 * end_page_writeback if the page has no more pending IO
2229 * Scheduling is not allowed, so the extent state tree is expected
2230 * to have one and only one object corresponding to this IO.
2232 static void end_bio_extent_writepage(struct bio *bio, int err)
2234 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2235 struct extent_io_tree *tree;
2241 struct page *page = bvec->bv_page;
2242 tree = &BTRFS_I(page->mapping->host)->io_tree;
2244 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2246 end = start + bvec->bv_len - 1;
2248 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2253 if (--bvec >= bio->bi_io_vec)
2254 prefetchw(&bvec->bv_page->flags);
2256 if (end_extent_writepage(page, err, start, end))
2260 end_page_writeback(page);
2262 check_page_writeback(tree, page);
2263 } while (bvec >= bio->bi_io_vec);
2269 * after a readpage IO is done, we need to:
2270 * clear the uptodate bits on error
2271 * set the uptodate bits if things worked
2272 * set the page up to date if all extents in the tree are uptodate
2273 * clear the lock bit in the extent tree
2274 * unlock the page if there are no other extents locked for it
2276 * Scheduling is not allowed, so the extent state tree is expected
2277 * to have one and only one object corresponding to this IO.
2279 static void end_bio_extent_readpage(struct bio *bio, int err)
2281 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2282 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2283 struct bio_vec *bvec = bio->bi_io_vec;
2284 struct extent_io_tree *tree;
2295 struct page *page = bvec->bv_page;
2296 struct extent_state *cached = NULL;
2297 struct extent_state *state;
2299 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2300 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2301 (long int)bio->bi_bdev);
2302 tree = &BTRFS_I(page->mapping->host)->io_tree;
2304 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2306 end = start + bvec->bv_len - 1;
2308 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2313 if (++bvec <= bvec_end)
2314 prefetchw(&bvec->bv_page->flags);
2316 spin_lock(&tree->lock);
2317 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2318 if (state && state->start == start) {
2320 * take a reference on the state, unlock will drop
2323 cache_state(state, &cached);
2325 spin_unlock(&tree->lock);
2327 mirror = (int)(unsigned long)bio->bi_bdev;
2328 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2329 ret = tree->ops->readpage_end_io_hook(page, start, end,
2332 /* no IO indicated but software detected errors
2333 * in the block, either checksum errors or
2334 * issues with the contents */
2335 struct btrfs_root *root =
2336 BTRFS_I(page->mapping->host)->root;
2337 struct btrfs_device *device;
2340 device = btrfs_find_device_for_logical(
2341 root, start, mirror);
2343 btrfs_dev_stat_inc_and_print(device,
2344 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2346 clean_io_failure(start, page);
2350 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
2351 ret = tree->ops->readpage_io_failed_hook(page, mirror);
2353 test_bit(BIO_UPTODATE, &bio->bi_flags))
2355 } else if (!uptodate) {
2357 * The generic bio_readpage_error handles errors the
2358 * following way: If possible, new read requests are
2359 * created and submitted and will end up in
2360 * end_bio_extent_readpage as well (if we're lucky, not
2361 * in the !uptodate case). In that case it returns 0 and
2362 * we just go on with the next page in our bio. If it
2363 * can't handle the error it will return -EIO and we
2364 * remain responsible for that page.
2366 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
2369 test_bit(BIO_UPTODATE, &bio->bi_flags);
2372 uncache_state(&cached);
2377 if (uptodate && tree->track_uptodate) {
2378 set_extent_uptodate(tree, start, end, &cached,
2381 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2385 SetPageUptodate(page);
2387 ClearPageUptodate(page);
2393 check_page_uptodate(tree, page);
2395 ClearPageUptodate(page);
2398 check_page_locked(tree, page);
2400 } while (bvec <= bvec_end);
2406 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2411 bio = bio_alloc(gfp_flags, nr_vecs);
2413 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2414 while (!bio && (nr_vecs /= 2))
2415 bio = bio_alloc(gfp_flags, nr_vecs);
2420 bio->bi_bdev = bdev;
2421 bio->bi_sector = first_sector;
2427 * Since writes are async, they will only return -ENOMEM.
2428 * Reads can return the full range of I/O error conditions.
2430 static int __must_check submit_one_bio(int rw, struct bio *bio,
2431 int mirror_num, unsigned long bio_flags)
2434 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2435 struct page *page = bvec->bv_page;
2436 struct extent_io_tree *tree = bio->bi_private;
2439 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
2441 bio->bi_private = NULL;
2445 if (tree->ops && tree->ops->submit_bio_hook)
2446 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2447 mirror_num, bio_flags, start);
2449 btrfsic_submit_bio(rw, bio);
2451 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2457 static int merge_bio(struct extent_io_tree *tree, struct page *page,
2458 unsigned long offset, size_t size, struct bio *bio,
2459 unsigned long bio_flags)
2462 if (tree->ops && tree->ops->merge_bio_hook)
2463 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2470 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2471 struct page *page, sector_t sector,
2472 size_t size, unsigned long offset,
2473 struct block_device *bdev,
2474 struct bio **bio_ret,
2475 unsigned long max_pages,
2476 bio_end_io_t end_io_func,
2478 unsigned long prev_bio_flags,
2479 unsigned long bio_flags)
2485 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2486 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2487 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2489 if (bio_ret && *bio_ret) {
2492 contig = bio->bi_sector == sector;
2494 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2497 if (prev_bio_flags != bio_flags || !contig ||
2498 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
2499 bio_add_page(bio, page, page_size, offset) < page_size) {
2500 ret = submit_one_bio(rw, bio, mirror_num,
2509 if (this_compressed)
2512 nr = bio_get_nr_vecs(bdev);
2514 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2518 bio_add_page(bio, page, page_size, offset);
2519 bio->bi_end_io = end_io_func;
2520 bio->bi_private = tree;
2525 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2530 void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2532 if (!PagePrivate(page)) {
2533 SetPagePrivate(page);
2534 page_cache_get(page);
2535 set_page_private(page, (unsigned long)eb);
2537 WARN_ON(page->private != (unsigned long)eb);
2541 void set_page_extent_mapped(struct page *page)
2543 if (!PagePrivate(page)) {
2544 SetPagePrivate(page);
2545 page_cache_get(page);
2546 set_page_private(page, EXTENT_PAGE_PRIVATE);
2551 * basic readpage implementation. Locked extent state structs are inserted
2552 * into the tree that are removed when the IO is done (by the end_io
2554 * XXX JDM: This needs looking at to ensure proper page locking
2556 static int __extent_read_full_page(struct extent_io_tree *tree,
2558 get_extent_t *get_extent,
2559 struct bio **bio, int mirror_num,
2560 unsigned long *bio_flags)
2562 struct inode *inode = page->mapping->host;
2563 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2564 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2568 u64 last_byte = i_size_read(inode);
2572 struct extent_map *em;
2573 struct block_device *bdev;
2574 struct btrfs_ordered_extent *ordered;
2577 size_t pg_offset = 0;
2579 size_t disk_io_size;
2580 size_t blocksize = inode->i_sb->s_blocksize;
2581 unsigned long this_bio_flag = 0;
2583 set_page_extent_mapped(page);
2585 if (!PageUptodate(page)) {
2586 if (cleancache_get_page(page) == 0) {
2587 BUG_ON(blocksize != PAGE_SIZE);
2594 lock_extent(tree, start, end);
2595 ordered = btrfs_lookup_ordered_extent(inode, start);
2598 unlock_extent(tree, start, end);
2599 btrfs_start_ordered_extent(inode, ordered, 1);
2600 btrfs_put_ordered_extent(ordered);
2603 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2605 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2608 iosize = PAGE_CACHE_SIZE - zero_offset;
2609 userpage = kmap_atomic(page);
2610 memset(userpage + zero_offset, 0, iosize);
2611 flush_dcache_page(page);
2612 kunmap_atomic(userpage);
2615 while (cur <= end) {
2616 if (cur >= last_byte) {
2618 struct extent_state *cached = NULL;
2620 iosize = PAGE_CACHE_SIZE - pg_offset;
2621 userpage = kmap_atomic(page);
2622 memset(userpage + pg_offset, 0, iosize);
2623 flush_dcache_page(page);
2624 kunmap_atomic(userpage);
2625 set_extent_uptodate(tree, cur, cur + iosize - 1,
2627 unlock_extent_cached(tree, cur, cur + iosize - 1,
2631 em = get_extent(inode, page, pg_offset, cur,
2633 if (IS_ERR_OR_NULL(em)) {
2635 unlock_extent(tree, cur, end);
2638 extent_offset = cur - em->start;
2639 BUG_ON(extent_map_end(em) <= cur);
2642 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2643 this_bio_flag = EXTENT_BIO_COMPRESSED;
2644 extent_set_compress_type(&this_bio_flag,
2648 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2649 cur_end = min(extent_map_end(em) - 1, end);
2650 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2651 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2652 disk_io_size = em->block_len;
2653 sector = em->block_start >> 9;
2655 sector = (em->block_start + extent_offset) >> 9;
2656 disk_io_size = iosize;
2659 block_start = em->block_start;
2660 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2661 block_start = EXTENT_MAP_HOLE;
2662 free_extent_map(em);
2665 /* we've found a hole, just zero and go on */
2666 if (block_start == EXTENT_MAP_HOLE) {
2668 struct extent_state *cached = NULL;
2670 userpage = kmap_atomic(page);
2671 memset(userpage + pg_offset, 0, iosize);
2672 flush_dcache_page(page);
2673 kunmap_atomic(userpage);
2675 set_extent_uptodate(tree, cur, cur + iosize - 1,
2677 unlock_extent_cached(tree, cur, cur + iosize - 1,
2680 pg_offset += iosize;
2683 /* the get_extent function already copied into the page */
2684 if (test_range_bit(tree, cur, cur_end,
2685 EXTENT_UPTODATE, 1, NULL)) {
2686 check_page_uptodate(tree, page);
2687 unlock_extent(tree, cur, cur + iosize - 1);
2689 pg_offset += iosize;
2692 /* we have an inline extent but it didn't get marked up
2693 * to date. Error out
2695 if (block_start == EXTENT_MAP_INLINE) {
2697 unlock_extent(tree, cur, cur + iosize - 1);
2699 pg_offset += iosize;
2704 if (tree->ops && tree->ops->readpage_io_hook) {
2705 ret = tree->ops->readpage_io_hook(page, cur,
2709 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2711 ret = submit_extent_page(READ, tree, page,
2712 sector, disk_io_size, pg_offset,
2714 end_bio_extent_readpage, mirror_num,
2717 BUG_ON(ret == -ENOMEM);
2719 *bio_flags = this_bio_flag;
2724 pg_offset += iosize;
2728 if (!PageError(page))
2729 SetPageUptodate(page);
2735 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2736 get_extent_t *get_extent, int mirror_num)
2738 struct bio *bio = NULL;
2739 unsigned long bio_flags = 0;
2742 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2745 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2749 static noinline void update_nr_written(struct page *page,
2750 struct writeback_control *wbc,
2751 unsigned long nr_written)
2753 wbc->nr_to_write -= nr_written;
2754 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2755 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2756 page->mapping->writeback_index = page->index + nr_written;
2760 * the writepage semantics are similar to regular writepage. extent
2761 * records are inserted to lock ranges in the tree, and as dirty areas
2762 * are found, they are marked writeback. Then the lock bits are removed
2763 * and the end_io handler clears the writeback ranges
2765 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2768 struct inode *inode = page->mapping->host;
2769 struct extent_page_data *epd = data;
2770 struct extent_io_tree *tree = epd->tree;
2771 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2773 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2777 u64 last_byte = i_size_read(inode);
2781 struct extent_state *cached_state = NULL;
2782 struct extent_map *em;
2783 struct block_device *bdev;
2786 size_t pg_offset = 0;
2788 loff_t i_size = i_size_read(inode);
2789 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2795 unsigned long nr_written = 0;
2796 bool fill_delalloc = true;
2798 if (wbc->sync_mode == WB_SYNC_ALL)
2799 write_flags = WRITE_SYNC;
2801 write_flags = WRITE;
2803 trace___extent_writepage(page, inode, wbc);
2805 WARN_ON(!PageLocked(page));
2807 ClearPageError(page);
2809 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2810 if (page->index > end_index ||
2811 (page->index == end_index && !pg_offset)) {
2812 page->mapping->a_ops->invalidatepage(page, 0);
2817 if (page->index == end_index) {
2820 userpage = kmap_atomic(page);
2821 memset(userpage + pg_offset, 0,
2822 PAGE_CACHE_SIZE - pg_offset);
2823 kunmap_atomic(userpage);
2824 flush_dcache_page(page);
2828 set_page_extent_mapped(page);
2830 if (!tree->ops || !tree->ops->fill_delalloc)
2831 fill_delalloc = false;
2833 delalloc_start = start;
2836 if (!epd->extent_locked && fill_delalloc) {
2837 u64 delalloc_to_write = 0;
2839 * make sure the wbc mapping index is at least updated
2842 update_nr_written(page, wbc, 0);
2844 while (delalloc_end < page_end) {
2845 nr_delalloc = find_lock_delalloc_range(inode, tree,
2850 if (nr_delalloc == 0) {
2851 delalloc_start = delalloc_end + 1;
2854 ret = tree->ops->fill_delalloc(inode, page,
2859 /* File system has been set read-only */
2865 * delalloc_end is already one less than the total
2866 * length, so we don't subtract one from
2869 delalloc_to_write += (delalloc_end - delalloc_start +
2872 delalloc_start = delalloc_end + 1;
2874 if (wbc->nr_to_write < delalloc_to_write) {
2877 if (delalloc_to_write < thresh * 2)
2878 thresh = delalloc_to_write;
2879 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2883 /* did the fill delalloc function already unlock and start
2889 * we've unlocked the page, so we can't update
2890 * the mapping's writeback index, just update
2893 wbc->nr_to_write -= nr_written;
2897 if (tree->ops && tree->ops->writepage_start_hook) {
2898 ret = tree->ops->writepage_start_hook(page, start,
2901 /* Fixup worker will requeue */
2903 wbc->pages_skipped++;
2905 redirty_page_for_writepage(wbc, page);
2906 update_nr_written(page, wbc, nr_written);
2914 * we don't want to touch the inode after unlocking the page,
2915 * so we update the mapping writeback index now
2917 update_nr_written(page, wbc, nr_written + 1);
2920 if (last_byte <= start) {
2921 if (tree->ops && tree->ops->writepage_end_io_hook)
2922 tree->ops->writepage_end_io_hook(page, start,
2927 blocksize = inode->i_sb->s_blocksize;
2929 while (cur <= end) {
2930 if (cur >= last_byte) {
2931 if (tree->ops && tree->ops->writepage_end_io_hook)
2932 tree->ops->writepage_end_io_hook(page, cur,
2936 em = epd->get_extent(inode, page, pg_offset, cur,
2938 if (IS_ERR_OR_NULL(em)) {
2943 extent_offset = cur - em->start;
2944 BUG_ON(extent_map_end(em) <= cur);
2946 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2947 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2948 sector = (em->block_start + extent_offset) >> 9;
2950 block_start = em->block_start;
2951 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2952 free_extent_map(em);
2956 * compressed and inline extents are written through other
2959 if (compressed || block_start == EXTENT_MAP_HOLE ||
2960 block_start == EXTENT_MAP_INLINE) {
2962 * end_io notification does not happen here for
2963 * compressed extents
2965 if (!compressed && tree->ops &&
2966 tree->ops->writepage_end_io_hook)
2967 tree->ops->writepage_end_io_hook(page, cur,
2970 else if (compressed) {
2971 /* we don't want to end_page_writeback on
2972 * a compressed extent. this happens
2979 pg_offset += iosize;
2982 /* leave this out until we have a page_mkwrite call */
2983 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2984 EXTENT_DIRTY, 0, NULL)) {
2986 pg_offset += iosize;
2990 if (tree->ops && tree->ops->writepage_io_hook) {
2991 ret = tree->ops->writepage_io_hook(page, cur,
2999 unsigned long max_nr = end_index + 1;
3001 set_range_writeback(tree, cur, cur + iosize - 1);
3002 if (!PageWriteback(page)) {
3003 printk(KERN_ERR "btrfs warning page %lu not "
3004 "writeback, cur %llu end %llu\n",
3005 page->index, (unsigned long long)cur,
3006 (unsigned long long)end);
3009 ret = submit_extent_page(write_flags, tree, page,
3010 sector, iosize, pg_offset,
3011 bdev, &epd->bio, max_nr,
3012 end_bio_extent_writepage,
3018 pg_offset += iosize;
3023 /* make sure the mapping tag for page dirty gets cleared */
3024 set_page_writeback(page);
3025 end_page_writeback(page);
3031 /* drop our reference on any cached states */
3032 free_extent_state(cached_state);
3036 static int eb_wait(void *word)
3042 static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3044 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3045 TASK_UNINTERRUPTIBLE);
3048 static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3049 struct btrfs_fs_info *fs_info,
3050 struct extent_page_data *epd)
3052 unsigned long i, num_pages;
3056 if (!btrfs_try_tree_write_lock(eb)) {
3058 flush_write_bio(epd);
3059 btrfs_tree_lock(eb);
3062 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3063 btrfs_tree_unlock(eb);
3067 flush_write_bio(epd);
3071 wait_on_extent_buffer_writeback(eb);
3072 btrfs_tree_lock(eb);
3073 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3075 btrfs_tree_unlock(eb);
3079 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3080 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3081 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3082 spin_lock(&fs_info->delalloc_lock);
3083 if (fs_info->dirty_metadata_bytes >= eb->len)
3084 fs_info->dirty_metadata_bytes -= eb->len;
3087 spin_unlock(&fs_info->delalloc_lock);
3091 btrfs_tree_unlock(eb);
3096 num_pages = num_extent_pages(eb->start, eb->len);
3097 for (i = 0; i < num_pages; i++) {
3098 struct page *p = extent_buffer_page(eb, i);
3100 if (!trylock_page(p)) {
3102 flush_write_bio(epd);
3112 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3114 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3115 smp_mb__after_clear_bit();
3116 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3119 static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3121 int uptodate = err == 0;
3122 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3123 struct extent_buffer *eb;
3127 struct page *page = bvec->bv_page;
3130 eb = (struct extent_buffer *)page->private;
3132 done = atomic_dec_and_test(&eb->io_pages);
3134 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3135 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3136 ClearPageUptodate(page);
3140 end_page_writeback(page);
3145 end_extent_buffer_writeback(eb);
3146 } while (bvec >= bio->bi_io_vec);
3152 static int write_one_eb(struct extent_buffer *eb,
3153 struct btrfs_fs_info *fs_info,
3154 struct writeback_control *wbc,
3155 struct extent_page_data *epd)
3157 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3158 u64 offset = eb->start;
3159 unsigned long i, num_pages;
3160 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3163 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3164 num_pages = num_extent_pages(eb->start, eb->len);
3165 atomic_set(&eb->io_pages, num_pages);
3166 for (i = 0; i < num_pages; i++) {
3167 struct page *p = extent_buffer_page(eb, i);
3169 clear_page_dirty_for_io(p);
3170 set_page_writeback(p);
3171 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3172 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3173 -1, end_bio_extent_buffer_writepage,
3176 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3178 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3179 end_extent_buffer_writeback(eb);
3183 offset += PAGE_CACHE_SIZE;
3184 update_nr_written(p, wbc, 1);
3188 if (unlikely(ret)) {
3189 for (; i < num_pages; i++) {
3190 struct page *p = extent_buffer_page(eb, i);
3198 int btree_write_cache_pages(struct address_space *mapping,
3199 struct writeback_control *wbc)
3201 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3202 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3203 struct extent_buffer *eb, *prev_eb = NULL;
3204 struct extent_page_data epd = {
3208 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3212 int nr_to_write_done = 0;
3213 struct pagevec pvec;
3216 pgoff_t end; /* Inclusive */
3220 pagevec_init(&pvec, 0);
3221 if (wbc->range_cyclic) {
3222 index = mapping->writeback_index; /* Start from prev offset */
3225 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3226 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3229 if (wbc->sync_mode == WB_SYNC_ALL)
3230 tag = PAGECACHE_TAG_TOWRITE;
3232 tag = PAGECACHE_TAG_DIRTY;
3234 if (wbc->sync_mode == WB_SYNC_ALL)
3235 tag_pages_for_writeback(mapping, index, end);
3236 while (!done && !nr_to_write_done && (index <= end) &&
3237 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3238 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3242 for (i = 0; i < nr_pages; i++) {
3243 struct page *page = pvec.pages[i];
3245 if (!PagePrivate(page))
3248 if (!wbc->range_cyclic && page->index > end) {
3253 eb = (struct extent_buffer *)page->private;
3262 if (!atomic_inc_not_zero(&eb->refs)) {
3268 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3270 free_extent_buffer(eb);
3274 ret = write_one_eb(eb, fs_info, wbc, &epd);
3277 free_extent_buffer(eb);
3280 free_extent_buffer(eb);
3283 * the filesystem may choose to bump up nr_to_write.
3284 * We have to make sure to honor the new nr_to_write
3287 nr_to_write_done = wbc->nr_to_write <= 0;
3289 pagevec_release(&pvec);
3292 if (!scanned && !done) {
3294 * We hit the last page and there is more work to be done: wrap
3295 * back to the start of the file
3301 flush_write_bio(&epd);
3306 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3307 * @mapping: address space structure to write
3308 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3309 * @writepage: function called for each page
3310 * @data: data passed to writepage function
3312 * If a page is already under I/O, write_cache_pages() skips it, even
3313 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3314 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3315 * and msync() need to guarantee that all the data which was dirty at the time
3316 * the call was made get new I/O started against them. If wbc->sync_mode is
3317 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3318 * existing IO to complete.
3320 static int extent_write_cache_pages(struct extent_io_tree *tree,
3321 struct address_space *mapping,
3322 struct writeback_control *wbc,
3323 writepage_t writepage, void *data,
3324 void (*flush_fn)(void *))
3328 int nr_to_write_done = 0;
3329 struct pagevec pvec;
3332 pgoff_t end; /* Inclusive */
3336 pagevec_init(&pvec, 0);
3337 if (wbc->range_cyclic) {
3338 index = mapping->writeback_index; /* Start from prev offset */
3341 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3342 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3345 if (wbc->sync_mode == WB_SYNC_ALL)
3346 tag = PAGECACHE_TAG_TOWRITE;
3348 tag = PAGECACHE_TAG_DIRTY;
3350 if (wbc->sync_mode == WB_SYNC_ALL)
3351 tag_pages_for_writeback(mapping, index, end);
3352 while (!done && !nr_to_write_done && (index <= end) &&
3353 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3354 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3358 for (i = 0; i < nr_pages; i++) {
3359 struct page *page = pvec.pages[i];
3362 * At this point we hold neither mapping->tree_lock nor
3363 * lock on the page itself: the page may be truncated or
3364 * invalidated (changing page->mapping to NULL), or even
3365 * swizzled back from swapper_space to tmpfs file
3369 tree->ops->write_cache_pages_lock_hook) {
3370 tree->ops->write_cache_pages_lock_hook(page,
3373 if (!trylock_page(page)) {
3379 if (unlikely(page->mapping != mapping)) {
3384 if (!wbc->range_cyclic && page->index > end) {
3390 if (wbc->sync_mode != WB_SYNC_NONE) {
3391 if (PageWriteback(page))
3393 wait_on_page_writeback(page);
3396 if (PageWriteback(page) ||
3397 !clear_page_dirty_for_io(page)) {
3402 ret = (*writepage)(page, wbc, data);
3404 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3412 * the filesystem may choose to bump up nr_to_write.
3413 * We have to make sure to honor the new nr_to_write
3416 nr_to_write_done = wbc->nr_to_write <= 0;
3418 pagevec_release(&pvec);
3421 if (!scanned && !done) {
3423 * We hit the last page and there is more work to be done: wrap
3424 * back to the start of the file
3433 static void flush_epd_write_bio(struct extent_page_data *epd)
3442 ret = submit_one_bio(rw, epd->bio, 0, 0);
3443 BUG_ON(ret < 0); /* -ENOMEM */
3448 static noinline void flush_write_bio(void *data)
3450 struct extent_page_data *epd = data;
3451 flush_epd_write_bio(epd);
3454 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3455 get_extent_t *get_extent,
3456 struct writeback_control *wbc)
3459 struct extent_page_data epd = {
3462 .get_extent = get_extent,
3464 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3467 ret = __extent_writepage(page, wbc, &epd);
3469 flush_epd_write_bio(&epd);
3473 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3474 u64 start, u64 end, get_extent_t *get_extent,
3478 struct address_space *mapping = inode->i_mapping;
3480 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3483 struct extent_page_data epd = {
3486 .get_extent = get_extent,
3488 .sync_io = mode == WB_SYNC_ALL,
3490 struct writeback_control wbc_writepages = {
3492 .nr_to_write = nr_pages * 2,
3493 .range_start = start,
3494 .range_end = end + 1,
3497 while (start <= end) {
3498 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3499 if (clear_page_dirty_for_io(page))
3500 ret = __extent_writepage(page, &wbc_writepages, &epd);
3502 if (tree->ops && tree->ops->writepage_end_io_hook)
3503 tree->ops->writepage_end_io_hook(page, start,
3504 start + PAGE_CACHE_SIZE - 1,
3508 page_cache_release(page);
3509 start += PAGE_CACHE_SIZE;
3512 flush_epd_write_bio(&epd);
3516 int extent_writepages(struct extent_io_tree *tree,
3517 struct address_space *mapping,
3518 get_extent_t *get_extent,
3519 struct writeback_control *wbc)
3522 struct extent_page_data epd = {
3525 .get_extent = get_extent,
3527 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3530 ret = extent_write_cache_pages(tree, mapping, wbc,
3531 __extent_writepage, &epd,
3533 flush_epd_write_bio(&epd);
3537 int extent_readpages(struct extent_io_tree *tree,
3538 struct address_space *mapping,
3539 struct list_head *pages, unsigned nr_pages,
3540 get_extent_t get_extent)
3542 struct bio *bio = NULL;
3544 unsigned long bio_flags = 0;
3546 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3547 struct page *page = list_entry(pages->prev, struct page, lru);
3549 prefetchw(&page->flags);
3550 list_del(&page->lru);
3551 if (!add_to_page_cache_lru(page, mapping,
3552 page->index, GFP_NOFS)) {
3553 __extent_read_full_page(tree, page, get_extent,
3554 &bio, 0, &bio_flags);
3556 page_cache_release(page);
3558 BUG_ON(!list_empty(pages));
3560 return submit_one_bio(READ, bio, 0, bio_flags);
3565 * basic invalidatepage code, this waits on any locked or writeback
3566 * ranges corresponding to the page, and then deletes any extent state
3567 * records from the tree
3569 int extent_invalidatepage(struct extent_io_tree *tree,
3570 struct page *page, unsigned long offset)
3572 struct extent_state *cached_state = NULL;
3573 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3574 u64 end = start + PAGE_CACHE_SIZE - 1;
3575 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3577 start += (offset + blocksize - 1) & ~(blocksize - 1);
3581 lock_extent_bits(tree, start, end, 0, &cached_state);
3582 wait_on_page_writeback(page);
3583 clear_extent_bit(tree, start, end,
3584 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3585 EXTENT_DO_ACCOUNTING,
3586 1, 1, &cached_state, GFP_NOFS);
3591 * a helper for releasepage, this tests for areas of the page that
3592 * are locked or under IO and drops the related state bits if it is safe
3595 int try_release_extent_state(struct extent_map_tree *map,
3596 struct extent_io_tree *tree, struct page *page,
3599 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3600 u64 end = start + PAGE_CACHE_SIZE - 1;
3603 if (test_range_bit(tree, start, end,
3604 EXTENT_IOBITS, 0, NULL))
3607 if ((mask & GFP_NOFS) == GFP_NOFS)
3610 * at this point we can safely clear everything except the
3611 * locked bit and the nodatasum bit
3613 ret = clear_extent_bit(tree, start, end,
3614 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3617 /* if clear_extent_bit failed for enomem reasons,
3618 * we can't allow the release to continue.
3629 * a helper for releasepage. As long as there are no locked extents
3630 * in the range corresponding to the page, both state records and extent
3631 * map records are removed
3633 int try_release_extent_mapping(struct extent_map_tree *map,
3634 struct extent_io_tree *tree, struct page *page,
3637 struct extent_map *em;
3638 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3639 u64 end = start + PAGE_CACHE_SIZE - 1;
3641 if ((mask & __GFP_WAIT) &&
3642 page->mapping->host->i_size > 16 * 1024 * 1024) {
3644 while (start <= end) {
3645 len = end - start + 1;
3646 write_lock(&map->lock);
3647 em = lookup_extent_mapping(map, start, len);
3649 write_unlock(&map->lock);
3652 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3653 em->start != start) {
3654 write_unlock(&map->lock);
3655 free_extent_map(em);
3658 if (!test_range_bit(tree, em->start,
3659 extent_map_end(em) - 1,
3660 EXTENT_LOCKED | EXTENT_WRITEBACK,
3662 remove_extent_mapping(map, em);
3663 /* once for the rb tree */
3664 free_extent_map(em);
3666 start = extent_map_end(em);
3667 write_unlock(&map->lock);
3670 free_extent_map(em);
3673 return try_release_extent_state(map, tree, page, mask);
3677 * helper function for fiemap, which doesn't want to see any holes.
3678 * This maps until we find something past 'last'
3680 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3683 get_extent_t *get_extent)
3685 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3686 struct extent_map *em;
3693 len = last - offset;
3696 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3697 em = get_extent(inode, NULL, 0, offset, len, 0);
3698 if (IS_ERR_OR_NULL(em))
3701 /* if this isn't a hole return it */
3702 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3703 em->block_start != EXTENT_MAP_HOLE) {
3707 /* this is a hole, advance to the next extent */
3708 offset = extent_map_end(em);
3709 free_extent_map(em);
3716 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3717 __u64 start, __u64 len, get_extent_t *get_extent)
3721 u64 max = start + len;
3725 u64 last_for_get_extent = 0;
3727 u64 isize = i_size_read(inode);
3728 struct btrfs_key found_key;
3729 struct extent_map *em = NULL;
3730 struct extent_state *cached_state = NULL;
3731 struct btrfs_path *path;
3732 struct btrfs_file_extent_item *item;
3737 unsigned long emflags;
3742 path = btrfs_alloc_path();
3745 path->leave_spinning = 1;
3747 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3748 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3751 * lookup the last file extent. We're not using i_size here
3752 * because there might be preallocation past i_size
3754 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3755 path, btrfs_ino(inode), -1, 0);
3757 btrfs_free_path(path);
3762 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3763 struct btrfs_file_extent_item);
3764 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3765 found_type = btrfs_key_type(&found_key);
3767 /* No extents, but there might be delalloc bits */
3768 if (found_key.objectid != btrfs_ino(inode) ||
3769 found_type != BTRFS_EXTENT_DATA_KEY) {
3770 /* have to trust i_size as the end */
3772 last_for_get_extent = isize;
3775 * remember the start of the last extent. There are a
3776 * bunch of different factors that go into the length of the
3777 * extent, so its much less complex to remember where it started
3779 last = found_key.offset;
3780 last_for_get_extent = last + 1;
3782 btrfs_free_path(path);
3785 * we might have some extents allocated but more delalloc past those
3786 * extents. so, we trust isize unless the start of the last extent is
3791 last_for_get_extent = isize;
3794 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3797 em = get_extent_skip_holes(inode, start, last_for_get_extent,
3807 u64 offset_in_extent;
3809 /* break if the extent we found is outside the range */
3810 if (em->start >= max || extent_map_end(em) < off)
3814 * get_extent may return an extent that starts before our
3815 * requested range. We have to make sure the ranges
3816 * we return to fiemap always move forward and don't
3817 * overlap, so adjust the offsets here
3819 em_start = max(em->start, off);
3822 * record the offset from the start of the extent
3823 * for adjusting the disk offset below
3825 offset_in_extent = em_start - em->start;
3826 em_end = extent_map_end(em);
3827 em_len = em_end - em_start;
3828 emflags = em->flags;
3833 * bump off for our next call to get_extent
3835 off = extent_map_end(em);
3839 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
3841 flags |= FIEMAP_EXTENT_LAST;
3842 } else if (em->block_start == EXTENT_MAP_INLINE) {
3843 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3844 FIEMAP_EXTENT_NOT_ALIGNED);
3845 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
3846 flags |= (FIEMAP_EXTENT_DELALLOC |
3847 FIEMAP_EXTENT_UNKNOWN);
3849 disko = em->block_start + offset_in_extent;
3851 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3852 flags |= FIEMAP_EXTENT_ENCODED;
3854 free_extent_map(em);
3856 if ((em_start >= last) || em_len == (u64)-1 ||
3857 (last == (u64)-1 && isize <= em_end)) {
3858 flags |= FIEMAP_EXTENT_LAST;
3862 /* now scan forward to see if this is really the last extent. */
3863 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3870 flags |= FIEMAP_EXTENT_LAST;
3873 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3879 free_extent_map(em);
3881 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3882 &cached_state, GFP_NOFS);
3886 inline struct page *extent_buffer_page(struct extent_buffer *eb,
3889 return eb->pages[i];
3892 inline unsigned long num_extent_pages(u64 start, u64 len)
3894 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3895 (start >> PAGE_CACHE_SHIFT);
3898 static void __free_extent_buffer(struct extent_buffer *eb)
3901 unsigned long flags;
3902 spin_lock_irqsave(&leak_lock, flags);
3903 list_del(&eb->leak_list);
3904 spin_unlock_irqrestore(&leak_lock, flags);
3906 if (eb->pages && eb->pages != eb->inline_pages)
3908 kmem_cache_free(extent_buffer_cache, eb);
3911 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3916 struct extent_buffer *eb = NULL;
3918 unsigned long flags;
3921 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
3927 rwlock_init(&eb->lock);
3928 atomic_set(&eb->write_locks, 0);
3929 atomic_set(&eb->read_locks, 0);
3930 atomic_set(&eb->blocking_readers, 0);
3931 atomic_set(&eb->blocking_writers, 0);
3932 atomic_set(&eb->spinning_readers, 0);
3933 atomic_set(&eb->spinning_writers, 0);
3934 eb->lock_nested = 0;
3935 init_waitqueue_head(&eb->write_lock_wq);
3936 init_waitqueue_head(&eb->read_lock_wq);
3939 spin_lock_irqsave(&leak_lock, flags);
3940 list_add(&eb->leak_list, &buffers);
3941 spin_unlock_irqrestore(&leak_lock, flags);
3943 spin_lock_init(&eb->refs_lock);
3944 atomic_set(&eb->refs, 1);
3945 atomic_set(&eb->io_pages, 0);
3947 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3948 struct page **pages;
3949 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3951 pages = kzalloc(num_pages, mask);
3953 __free_extent_buffer(eb);
3958 eb->pages = eb->inline_pages;
3964 static int extent_buffer_under_io(struct extent_buffer *eb)
3966 return (atomic_read(&eb->io_pages) ||
3967 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
3968 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3972 * Helper for releasing extent buffer page.
3974 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3975 unsigned long start_idx)
3977 unsigned long index;
3978 unsigned long num_pages;
3981 BUG_ON(extent_buffer_under_io(eb));
3983 num_pages = num_extent_pages(eb->start, eb->len);
3984 index = start_idx + num_pages;
3985 if (start_idx >= index)
3990 page = extent_buffer_page(eb, index);
3992 spin_lock(&page->mapping->private_lock);
3994 * We do this since we'll remove the pages after we've
3995 * removed the eb from the radix tree, so we could race
3996 * and have this page now attached to the new eb. So
3997 * only clear page_private if it's still connected to
4000 if (PagePrivate(page) &&
4001 page->private == (unsigned long)eb) {
4002 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4003 BUG_ON(PageDirty(page));
4004 BUG_ON(PageWriteback(page));
4006 * We need to make sure we haven't be attached
4009 ClearPagePrivate(page);
4010 set_page_private(page, 0);
4011 /* One for the page private */
4012 page_cache_release(page);
4014 spin_unlock(&page->mapping->private_lock);
4016 /* One for when we alloced the page */
4017 page_cache_release(page);
4019 } while (index != start_idx);
4023 * Helper for releasing the extent buffer.
4025 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4027 btrfs_release_extent_buffer_page(eb, 0);
4028 __free_extent_buffer(eb);
4031 static void check_buffer_tree_ref(struct extent_buffer *eb)
4033 /* the ref bit is tricky. We have to make sure it is set
4034 * if we have the buffer dirty. Otherwise the
4035 * code to free a buffer can end up dropping a dirty
4038 * Once the ref bit is set, it won't go away while the
4039 * buffer is dirty or in writeback, and it also won't
4040 * go away while we have the reference count on the
4043 * We can't just set the ref bit without bumping the
4044 * ref on the eb because free_extent_buffer might
4045 * see the ref bit and try to clear it. If this happens
4046 * free_extent_buffer might end up dropping our original
4047 * ref by mistake and freeing the page before we are able
4048 * to add one more ref.
4050 * So bump the ref count first, then set the bit. If someone
4051 * beat us to it, drop the ref we added.
4053 if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4054 atomic_inc(&eb->refs);
4055 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4056 atomic_dec(&eb->refs);
4060 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4062 unsigned long num_pages, i;
4064 check_buffer_tree_ref(eb);
4066 num_pages = num_extent_pages(eb->start, eb->len);
4067 for (i = 0; i < num_pages; i++) {
4068 struct page *p = extent_buffer_page(eb, i);
4069 mark_page_accessed(p);
4073 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
4074 u64 start, unsigned long len)
4076 unsigned long num_pages = num_extent_pages(start, len);
4078 unsigned long index = start >> PAGE_CACHE_SHIFT;
4079 struct extent_buffer *eb;
4080 struct extent_buffer *exists = NULL;
4082 struct address_space *mapping = tree->mapping;
4087 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4088 if (eb && atomic_inc_not_zero(&eb->refs)) {
4090 mark_extent_buffer_accessed(eb);
4095 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
4099 for (i = 0; i < num_pages; i++, index++) {
4100 p = find_or_create_page(mapping, index, GFP_NOFS);
4106 spin_lock(&mapping->private_lock);
4107 if (PagePrivate(p)) {
4109 * We could have already allocated an eb for this page
4110 * and attached one so lets see if we can get a ref on
4111 * the existing eb, and if we can we know it's good and
4112 * we can just return that one, else we know we can just
4113 * overwrite page->private.
4115 exists = (struct extent_buffer *)p->private;
4116 if (atomic_inc_not_zero(&exists->refs)) {
4117 spin_unlock(&mapping->private_lock);
4119 page_cache_release(p);
4120 mark_extent_buffer_accessed(exists);
4125 * Do this so attach doesn't complain and we need to
4126 * drop the ref the old guy had.
4128 ClearPagePrivate(p);
4129 WARN_ON(PageDirty(p));
4130 page_cache_release(p);
4132 attach_extent_buffer_page(eb, p);
4133 spin_unlock(&mapping->private_lock);
4134 WARN_ON(PageDirty(p));
4135 mark_page_accessed(p);
4137 if (!PageUptodate(p))
4141 * see below about how we avoid a nasty race with release page
4142 * and why we unlock later
4146 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4148 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4152 spin_lock(&tree->buffer_lock);
4153 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4154 if (ret == -EEXIST) {
4155 exists = radix_tree_lookup(&tree->buffer,
4156 start >> PAGE_CACHE_SHIFT);
4157 if (!atomic_inc_not_zero(&exists->refs)) {
4158 spin_unlock(&tree->buffer_lock);
4159 radix_tree_preload_end();
4163 spin_unlock(&tree->buffer_lock);
4164 radix_tree_preload_end();
4165 mark_extent_buffer_accessed(exists);
4168 /* add one reference for the tree */
4169 spin_lock(&eb->refs_lock);
4170 check_buffer_tree_ref(eb);
4171 spin_unlock(&eb->refs_lock);
4172 spin_unlock(&tree->buffer_lock);
4173 radix_tree_preload_end();
4176 * there is a race where release page may have
4177 * tried to find this extent buffer in the radix
4178 * but failed. It will tell the VM it is safe to
4179 * reclaim the, and it will clear the page private bit.
4180 * We must make sure to set the page private bit properly
4181 * after the extent buffer is in the radix tree so
4182 * it doesn't get lost
4184 SetPageChecked(eb->pages[0]);
4185 for (i = 1; i < num_pages; i++) {
4186 p = extent_buffer_page(eb, i);
4187 ClearPageChecked(p);
4190 unlock_page(eb->pages[0]);
4194 for (i = 0; i < num_pages; i++) {
4196 unlock_page(eb->pages[i]);
4199 WARN_ON(!atomic_dec_and_test(&eb->refs));
4200 btrfs_release_extent_buffer(eb);
4204 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
4205 u64 start, unsigned long len)
4207 struct extent_buffer *eb;
4210 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4211 if (eb && atomic_inc_not_zero(&eb->refs)) {
4213 mark_extent_buffer_accessed(eb);
4221 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4223 struct extent_buffer *eb =
4224 container_of(head, struct extent_buffer, rcu_head);
4226 __free_extent_buffer(eb);
4229 /* Expects to have eb->eb_lock already held */
4230 static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4232 WARN_ON(atomic_read(&eb->refs) == 0);
4233 if (atomic_dec_and_test(&eb->refs)) {
4234 struct extent_io_tree *tree = eb->tree;
4236 spin_unlock(&eb->refs_lock);
4238 spin_lock(&tree->buffer_lock);
4239 radix_tree_delete(&tree->buffer,
4240 eb->start >> PAGE_CACHE_SHIFT);
4241 spin_unlock(&tree->buffer_lock);
4243 /* Should be safe to release our pages at this point */
4244 btrfs_release_extent_buffer_page(eb, 0);
4246 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4249 spin_unlock(&eb->refs_lock);
4252 void free_extent_buffer(struct extent_buffer *eb)
4257 spin_lock(&eb->refs_lock);
4258 if (atomic_read(&eb->refs) == 2 &&
4259 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
4260 !extent_buffer_under_io(eb) &&
4261 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4262 atomic_dec(&eb->refs);
4265 * I know this is terrible, but it's temporary until we stop tracking
4266 * the uptodate bits and such for the extent buffers.
4268 release_extent_buffer(eb, GFP_ATOMIC);
4271 void free_extent_buffer_stale(struct extent_buffer *eb)
4276 spin_lock(&eb->refs_lock);
4277 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4279 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
4280 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4281 atomic_dec(&eb->refs);
4282 release_extent_buffer(eb, GFP_NOFS);
4285 void clear_extent_buffer_dirty(struct extent_buffer *eb)
4288 unsigned long num_pages;
4291 num_pages = num_extent_pages(eb->start, eb->len);
4293 for (i = 0; i < num_pages; i++) {
4294 page = extent_buffer_page(eb, i);
4295 if (!PageDirty(page))
4299 WARN_ON(!PagePrivate(page));
4301 clear_page_dirty_for_io(page);
4302 spin_lock_irq(&page->mapping->tree_lock);
4303 if (!PageDirty(page)) {
4304 radix_tree_tag_clear(&page->mapping->page_tree,
4306 PAGECACHE_TAG_DIRTY);
4308 spin_unlock_irq(&page->mapping->tree_lock);
4309 ClearPageError(page);
4312 WARN_ON(atomic_read(&eb->refs) == 0);
4315 int set_extent_buffer_dirty(struct extent_buffer *eb)
4318 unsigned long num_pages;
4321 check_buffer_tree_ref(eb);
4323 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4325 num_pages = num_extent_pages(eb->start, eb->len);
4326 WARN_ON(atomic_read(&eb->refs) == 0);
4327 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4329 for (i = 0; i < num_pages; i++)
4330 set_page_dirty(extent_buffer_page(eb, i));
4334 static int range_straddles_pages(u64 start, u64 len)
4336 if (len < PAGE_CACHE_SIZE)
4338 if (start & (PAGE_CACHE_SIZE - 1))
4340 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4345 int clear_extent_buffer_uptodate(struct extent_buffer *eb)
4349 unsigned long num_pages;
4351 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4352 num_pages = num_extent_pages(eb->start, eb->len);
4353 for (i = 0; i < num_pages; i++) {
4354 page = extent_buffer_page(eb, i);
4356 ClearPageUptodate(page);
4361 int set_extent_buffer_uptodate(struct extent_buffer *eb)
4365 unsigned long num_pages;
4367 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4368 num_pages = num_extent_pages(eb->start, eb->len);
4369 for (i = 0; i < num_pages; i++) {
4370 page = extent_buffer_page(eb, i);
4371 SetPageUptodate(page);
4376 int extent_range_uptodate(struct extent_io_tree *tree,
4381 int pg_uptodate = 1;
4383 unsigned long index;
4385 if (range_straddles_pages(start, end - start + 1)) {
4386 ret = test_range_bit(tree, start, end,
4387 EXTENT_UPTODATE, 1, NULL);
4391 while (start <= end) {
4392 index = start >> PAGE_CACHE_SHIFT;
4393 page = find_get_page(tree->mapping, index);
4396 uptodate = PageUptodate(page);
4397 page_cache_release(page);
4402 start += PAGE_CACHE_SIZE;
4407 int extent_buffer_uptodate(struct extent_buffer *eb)
4409 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4412 int read_extent_buffer_pages(struct extent_io_tree *tree,
4413 struct extent_buffer *eb, u64 start, int wait,
4414 get_extent_t *get_extent, int mirror_num)
4417 unsigned long start_i;
4421 int locked_pages = 0;
4422 int all_uptodate = 1;
4423 unsigned long num_pages;
4424 unsigned long num_reads = 0;
4425 struct bio *bio = NULL;
4426 unsigned long bio_flags = 0;
4428 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4432 WARN_ON(start < eb->start);
4433 start_i = (start >> PAGE_CACHE_SHIFT) -
4434 (eb->start >> PAGE_CACHE_SHIFT);
4439 num_pages = num_extent_pages(eb->start, eb->len);
4440 for (i = start_i; i < num_pages; i++) {
4441 page = extent_buffer_page(eb, i);
4442 if (wait == WAIT_NONE) {
4443 if (!trylock_page(page))
4449 if (!PageUptodate(page)) {
4456 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4460 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
4461 eb->read_mirror = 0;
4462 atomic_set(&eb->io_pages, num_reads);
4463 for (i = start_i; i < num_pages; i++) {
4464 page = extent_buffer_page(eb, i);
4465 if (!PageUptodate(page)) {
4466 ClearPageError(page);
4467 err = __extent_read_full_page(tree, page,
4469 mirror_num, &bio_flags);
4478 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
4483 if (ret || wait != WAIT_COMPLETE)
4486 for (i = start_i; i < num_pages; i++) {
4487 page = extent_buffer_page(eb, i);
4488 wait_on_page_locked(page);
4489 if (!PageUptodate(page))
4497 while (locked_pages > 0) {
4498 page = extent_buffer_page(eb, i);
4506 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4507 unsigned long start,
4514 char *dst = (char *)dstv;
4515 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4516 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4518 WARN_ON(start > eb->len);
4519 WARN_ON(start + len > eb->start + eb->len);
4521 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4524 page = extent_buffer_page(eb, i);
4526 cur = min(len, (PAGE_CACHE_SIZE - offset));
4527 kaddr = page_address(page);
4528 memcpy(dst, kaddr + offset, cur);
4537 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4538 unsigned long min_len, char **map,
4539 unsigned long *map_start,
4540 unsigned long *map_len)
4542 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4545 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4546 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4547 unsigned long end_i = (start_offset + start + min_len - 1) >>
4554 offset = start_offset;
4558 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4561 if (start + min_len > eb->len) {
4562 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4563 "wanted %lu %lu\n", (unsigned long long)eb->start,
4564 eb->len, start, min_len);
4569 p = extent_buffer_page(eb, i);
4570 kaddr = page_address(p);
4571 *map = kaddr + offset;
4572 *map_len = PAGE_CACHE_SIZE - offset;
4576 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4577 unsigned long start,
4584 char *ptr = (char *)ptrv;
4585 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4586 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4589 WARN_ON(start > eb->len);
4590 WARN_ON(start + len > eb->start + eb->len);
4592 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4595 page = extent_buffer_page(eb, i);
4597 cur = min(len, (PAGE_CACHE_SIZE - offset));
4599 kaddr = page_address(page);
4600 ret = memcmp(ptr, kaddr + offset, cur);
4612 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4613 unsigned long start, unsigned long len)
4619 char *src = (char *)srcv;
4620 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4621 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4623 WARN_ON(start > eb->len);
4624 WARN_ON(start + len > eb->start + eb->len);
4626 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4629 page = extent_buffer_page(eb, i);
4630 WARN_ON(!PageUptodate(page));
4632 cur = min(len, PAGE_CACHE_SIZE - offset);
4633 kaddr = page_address(page);
4634 memcpy(kaddr + offset, src, cur);
4643 void memset_extent_buffer(struct extent_buffer *eb, char c,
4644 unsigned long start, unsigned long len)
4650 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4651 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4653 WARN_ON(start > eb->len);
4654 WARN_ON(start + len > eb->start + eb->len);
4656 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4659 page = extent_buffer_page(eb, i);
4660 WARN_ON(!PageUptodate(page));
4662 cur = min(len, PAGE_CACHE_SIZE - offset);
4663 kaddr = page_address(page);
4664 memset(kaddr + offset, c, cur);
4672 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4673 unsigned long dst_offset, unsigned long src_offset,
4676 u64 dst_len = dst->len;
4681 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4682 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4684 WARN_ON(src->len != dst_len);
4686 offset = (start_offset + dst_offset) &
4687 ((unsigned long)PAGE_CACHE_SIZE - 1);
4690 page = extent_buffer_page(dst, i);
4691 WARN_ON(!PageUptodate(page));
4693 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4695 kaddr = page_address(page);
4696 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4705 static void move_pages(struct page *dst_page, struct page *src_page,
4706 unsigned long dst_off, unsigned long src_off,
4709 char *dst_kaddr = page_address(dst_page);
4710 if (dst_page == src_page) {
4711 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4713 char *src_kaddr = page_address(src_page);
4714 char *p = dst_kaddr + dst_off + len;
4715 char *s = src_kaddr + src_off + len;
4722 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4724 unsigned long distance = (src > dst) ? src - dst : dst - src;
4725 return distance < len;
4728 static void copy_pages(struct page *dst_page, struct page *src_page,
4729 unsigned long dst_off, unsigned long src_off,
4732 char *dst_kaddr = page_address(dst_page);
4734 int must_memmove = 0;
4736 if (dst_page != src_page) {
4737 src_kaddr = page_address(src_page);
4739 src_kaddr = dst_kaddr;
4740 if (areas_overlap(src_off, dst_off, len))
4745 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4747 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
4750 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4751 unsigned long src_offset, unsigned long len)
4754 size_t dst_off_in_page;
4755 size_t src_off_in_page;
4756 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4757 unsigned long dst_i;
4758 unsigned long src_i;
4760 if (src_offset + len > dst->len) {
4761 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4762 "len %lu dst len %lu\n", src_offset, len, dst->len);
4765 if (dst_offset + len > dst->len) {
4766 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4767 "len %lu dst len %lu\n", dst_offset, len, dst->len);
4772 dst_off_in_page = (start_offset + dst_offset) &
4773 ((unsigned long)PAGE_CACHE_SIZE - 1);
4774 src_off_in_page = (start_offset + src_offset) &
4775 ((unsigned long)PAGE_CACHE_SIZE - 1);
4777 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4778 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4780 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4782 cur = min_t(unsigned long, cur,
4783 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4785 copy_pages(extent_buffer_page(dst, dst_i),
4786 extent_buffer_page(dst, src_i),
4787 dst_off_in_page, src_off_in_page, cur);
4795 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4796 unsigned long src_offset, unsigned long len)
4799 size_t dst_off_in_page;
4800 size_t src_off_in_page;
4801 unsigned long dst_end = dst_offset + len - 1;
4802 unsigned long src_end = src_offset + len - 1;
4803 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4804 unsigned long dst_i;
4805 unsigned long src_i;
4807 if (src_offset + len > dst->len) {
4808 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4809 "len %lu len %lu\n", src_offset, len, dst->len);
4812 if (dst_offset + len > dst->len) {
4813 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4814 "len %lu len %lu\n", dst_offset, len, dst->len);
4817 if (dst_offset < src_offset) {
4818 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4822 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4823 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4825 dst_off_in_page = (start_offset + dst_end) &
4826 ((unsigned long)PAGE_CACHE_SIZE - 1);
4827 src_off_in_page = (start_offset + src_end) &
4828 ((unsigned long)PAGE_CACHE_SIZE - 1);
4830 cur = min_t(unsigned long, len, src_off_in_page + 1);
4831 cur = min(cur, dst_off_in_page + 1);
4832 move_pages(extent_buffer_page(dst, dst_i),
4833 extent_buffer_page(dst, src_i),
4834 dst_off_in_page - cur + 1,
4835 src_off_in_page - cur + 1, cur);
4843 int try_release_extent_buffer(struct page *page, gfp_t mask)
4845 struct extent_buffer *eb;
4848 * We need to make sure noboody is attaching this page to an eb right
4851 spin_lock(&page->mapping->private_lock);
4852 if (!PagePrivate(page)) {
4853 spin_unlock(&page->mapping->private_lock);
4857 eb = (struct extent_buffer *)page->private;
4861 * This is a little awful but should be ok, we need to make sure that
4862 * the eb doesn't disappear out from under us while we're looking at
4865 spin_lock(&eb->refs_lock);
4866 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
4867 spin_unlock(&eb->refs_lock);
4868 spin_unlock(&page->mapping->private_lock);
4871 spin_unlock(&page->mapping->private_lock);
4873 if ((mask & GFP_NOFS) == GFP_NOFS)
4877 * If tree ref isn't set then we know the ref on this eb is a real ref,
4878 * so just return, this page will likely be freed soon anyway.
4880 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4881 spin_unlock(&eb->refs_lock);
4884 release_extent_buffer(eb, mask);