1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/spinlock.h>
8 #include <linux/blkdev.h>
9 #include <linux/swap.h>
10 #include <linux/writeback.h>
11 #include <linux/pagevec.h>
12 #include <linux/prefetch.h>
13 #include <linux/cleancache.h>
14 #include "extent_io.h"
15 #include "extent_map.h"
18 #include "btrfs_inode.h"
20 #include "check-integrity.h"
22 #include "rcu-string.h"
24 static struct kmem_cache *extent_state_cache;
25 static struct kmem_cache *extent_buffer_cache;
26 static struct bio_set *btrfs_bioset;
28 #ifdef CONFIG_BTRFS_DEBUG
29 static LIST_HEAD(buffers);
30 static LIST_HEAD(states);
32 static DEFINE_SPINLOCK(leak_lock);
35 void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
39 spin_lock_irqsave(&leak_lock, flags);
41 spin_unlock_irqrestore(&leak_lock, flags);
45 void btrfs_leak_debug_del(struct list_head *entry)
49 spin_lock_irqsave(&leak_lock, flags);
51 spin_unlock_irqrestore(&leak_lock, flags);
55 void btrfs_leak_debug_check(void)
57 struct extent_state *state;
58 struct extent_buffer *eb;
60 while (!list_empty(&states)) {
61 state = list_entry(states.next, struct extent_state, leak_list);
62 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
63 "state %lu in tree %p refs %d\n",
64 (unsigned long long)state->start,
65 (unsigned long long)state->end,
66 state->state, state->tree, atomic_read(&state->refs));
67 list_del(&state->leak_list);
68 kmem_cache_free(extent_state_cache, state);
71 while (!list_empty(&buffers)) {
72 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
73 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
74 "refs %d\n", (unsigned long long)eb->start,
75 eb->len, atomic_read(&eb->refs));
76 list_del(&eb->leak_list);
77 kmem_cache_free(extent_buffer_cache, eb);
81 #define btrfs_leak_debug_add(new, head) do {} while (0)
82 #define btrfs_leak_debug_del(entry) do {} while (0)
83 #define btrfs_leak_debug_check() do {} while (0)
86 #define BUFFER_LRU_MAX 64
91 struct rb_node rb_node;
94 struct extent_page_data {
96 struct extent_io_tree *tree;
97 get_extent_t *get_extent;
98 unsigned long bio_flags;
100 /* tells writepage not to lock the state bits for this range
101 * it still does the unlocking
103 unsigned int extent_locked:1;
105 /* tells the submit_bio code to use a WRITE_SYNC */
106 unsigned int sync_io:1;
109 static noinline void flush_write_bio(void *data);
110 static inline struct btrfs_fs_info *
111 tree_fs_info(struct extent_io_tree *tree)
113 return btrfs_sb(tree->mapping->host->i_sb);
116 int __init extent_io_init(void)
118 extent_state_cache = kmem_cache_create("btrfs_extent_state",
119 sizeof(struct extent_state), 0,
120 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
121 if (!extent_state_cache)
124 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
125 sizeof(struct extent_buffer), 0,
126 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
127 if (!extent_buffer_cache)
128 goto free_state_cache;
130 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
131 offsetof(struct btrfs_io_bio, bio));
133 goto free_buffer_cache;
137 kmem_cache_destroy(extent_buffer_cache);
138 extent_buffer_cache = NULL;
141 kmem_cache_destroy(extent_state_cache);
142 extent_state_cache = NULL;
146 void extent_io_exit(void)
148 btrfs_leak_debug_check();
151 * Make sure all delayed rcu free are flushed before we
155 if (extent_state_cache)
156 kmem_cache_destroy(extent_state_cache);
157 if (extent_buffer_cache)
158 kmem_cache_destroy(extent_buffer_cache);
160 bioset_free(btrfs_bioset);
163 void extent_io_tree_init(struct extent_io_tree *tree,
164 struct address_space *mapping)
166 tree->state = RB_ROOT;
167 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
169 tree->dirty_bytes = 0;
170 spin_lock_init(&tree->lock);
171 spin_lock_init(&tree->buffer_lock);
172 tree->mapping = mapping;
175 static struct extent_state *alloc_extent_state(gfp_t mask)
177 struct extent_state *state;
179 state = kmem_cache_alloc(extent_state_cache, mask);
185 btrfs_leak_debug_add(&state->leak_list, &states);
186 atomic_set(&state->refs, 1);
187 init_waitqueue_head(&state->wq);
188 trace_alloc_extent_state(state, mask, _RET_IP_);
192 void free_extent_state(struct extent_state *state)
196 if (atomic_dec_and_test(&state->refs)) {
197 WARN_ON(state->tree);
198 btrfs_leak_debug_del(&state->leak_list);
199 trace_free_extent_state(state, _RET_IP_);
200 kmem_cache_free(extent_state_cache, state);
204 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
205 struct rb_node *node)
207 struct rb_node **p = &root->rb_node;
208 struct rb_node *parent = NULL;
209 struct tree_entry *entry;
213 entry = rb_entry(parent, struct tree_entry, rb_node);
215 if (offset < entry->start)
217 else if (offset > entry->end)
223 rb_link_node(node, parent, p);
224 rb_insert_color(node, root);
228 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
229 struct rb_node **prev_ret,
230 struct rb_node **next_ret)
232 struct rb_root *root = &tree->state;
233 struct rb_node *n = root->rb_node;
234 struct rb_node *prev = NULL;
235 struct rb_node *orig_prev = NULL;
236 struct tree_entry *entry;
237 struct tree_entry *prev_entry = NULL;
240 entry = rb_entry(n, struct tree_entry, rb_node);
244 if (offset < entry->start)
246 else if (offset > entry->end)
254 while (prev && offset > prev_entry->end) {
255 prev = rb_next(prev);
256 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
263 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
264 while (prev && offset < prev_entry->start) {
265 prev = rb_prev(prev);
266 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
273 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
276 struct rb_node *prev = NULL;
279 ret = __etree_search(tree, offset, &prev, NULL);
285 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
286 struct extent_state *other)
288 if (tree->ops && tree->ops->merge_extent_hook)
289 tree->ops->merge_extent_hook(tree->mapping->host, new,
294 * utility function to look for merge candidates inside a given range.
295 * Any extents with matching state are merged together into a single
296 * extent in the tree. Extents with EXTENT_IO in their state field
297 * are not merged because the end_io handlers need to be able to do
298 * operations on them without sleeping (or doing allocations/splits).
300 * This should be called with the tree lock held.
302 static void merge_state(struct extent_io_tree *tree,
303 struct extent_state *state)
305 struct extent_state *other;
306 struct rb_node *other_node;
308 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
311 other_node = rb_prev(&state->rb_node);
313 other = rb_entry(other_node, struct extent_state, rb_node);
314 if (other->end == state->start - 1 &&
315 other->state == state->state) {
316 merge_cb(tree, state, other);
317 state->start = other->start;
319 rb_erase(&other->rb_node, &tree->state);
320 free_extent_state(other);
323 other_node = rb_next(&state->rb_node);
325 other = rb_entry(other_node, struct extent_state, rb_node);
326 if (other->start == state->end + 1 &&
327 other->state == state->state) {
328 merge_cb(tree, state, other);
329 state->end = other->end;
331 rb_erase(&other->rb_node, &tree->state);
332 free_extent_state(other);
337 static void set_state_cb(struct extent_io_tree *tree,
338 struct extent_state *state, unsigned long *bits)
340 if (tree->ops && tree->ops->set_bit_hook)
341 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
344 static void clear_state_cb(struct extent_io_tree *tree,
345 struct extent_state *state, unsigned long *bits)
347 if (tree->ops && tree->ops->clear_bit_hook)
348 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
351 static void set_state_bits(struct extent_io_tree *tree,
352 struct extent_state *state, unsigned long *bits);
355 * insert an extent_state struct into the tree. 'bits' are set on the
356 * struct before it is inserted.
358 * This may return -EEXIST if the extent is already there, in which case the
359 * state struct is freed.
361 * The tree lock is not taken internally. This is a utility function and
362 * probably isn't what you want to call (see set/clear_extent_bit).
364 static int insert_state(struct extent_io_tree *tree,
365 struct extent_state *state, u64 start, u64 end,
368 struct rb_node *node;
371 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
372 (unsigned long long)end,
373 (unsigned long long)start);
374 state->start = start;
377 set_state_bits(tree, state, bits);
379 node = tree_insert(&tree->state, end, &state->rb_node);
381 struct extent_state *found;
382 found = rb_entry(node, struct extent_state, rb_node);
383 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
384 "%llu %llu\n", (unsigned long long)found->start,
385 (unsigned long long)found->end,
386 (unsigned long long)start, (unsigned long long)end);
390 merge_state(tree, state);
394 static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
397 if (tree->ops && tree->ops->split_extent_hook)
398 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
402 * split a given extent state struct in two, inserting the preallocated
403 * struct 'prealloc' as the newly created second half. 'split' indicates an
404 * offset inside 'orig' where it should be split.
407 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
408 * are two extent state structs in the tree:
409 * prealloc: [orig->start, split - 1]
410 * orig: [ split, orig->end ]
412 * The tree locks are not taken by this function. They need to be held
415 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
416 struct extent_state *prealloc, u64 split)
418 struct rb_node *node;
420 split_cb(tree, orig, split);
422 prealloc->start = orig->start;
423 prealloc->end = split - 1;
424 prealloc->state = orig->state;
427 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
429 free_extent_state(prealloc);
432 prealloc->tree = tree;
436 static struct extent_state *next_state(struct extent_state *state)
438 struct rb_node *next = rb_next(&state->rb_node);
440 return rb_entry(next, struct extent_state, rb_node);
446 * utility function to clear some bits in an extent state struct.
447 * it will optionally wake up any one waiting on this state (wake == 1).
449 * If no bits are set on the state struct after clearing things, the
450 * struct is freed and removed from the tree
452 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
453 struct extent_state *state,
454 unsigned long *bits, int wake)
456 struct extent_state *next;
457 unsigned long bits_to_clear = *bits & ~EXTENT_CTLBITS;
459 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
460 u64 range = state->end - state->start + 1;
461 WARN_ON(range > tree->dirty_bytes);
462 tree->dirty_bytes -= range;
464 clear_state_cb(tree, state, bits);
465 state->state &= ~bits_to_clear;
468 if (state->state == 0) {
469 next = next_state(state);
471 rb_erase(&state->rb_node, &tree->state);
473 free_extent_state(state);
478 merge_state(tree, state);
479 next = next_state(state);
484 static struct extent_state *
485 alloc_extent_state_atomic(struct extent_state *prealloc)
488 prealloc = alloc_extent_state(GFP_ATOMIC);
493 static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
495 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
496 "Extent tree was modified by another "
497 "thread while locked.");
501 * clear some bits on a range in the tree. This may require splitting
502 * or inserting elements in the tree, so the gfp mask is used to
503 * indicate which allocations or sleeping are allowed.
505 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
506 * the given range from the tree regardless of state (ie for truncate).
508 * the range [start, end] is inclusive.
510 * This takes the tree lock, and returns 0 on success and < 0 on error.
512 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
513 unsigned long bits, int wake, int delete,
514 struct extent_state **cached_state,
517 struct extent_state *state;
518 struct extent_state *cached;
519 struct extent_state *prealloc = NULL;
520 struct rb_node *node;
526 bits |= ~EXTENT_CTLBITS;
527 bits |= EXTENT_FIRST_DELALLOC;
529 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
532 if (!prealloc && (mask & __GFP_WAIT)) {
533 prealloc = alloc_extent_state(mask);
538 spin_lock(&tree->lock);
540 cached = *cached_state;
543 *cached_state = NULL;
547 if (cached && cached->tree && cached->start <= start &&
548 cached->end > start) {
550 atomic_dec(&cached->refs);
555 free_extent_state(cached);
558 * this search will find the extents that end after
561 node = tree_search(tree, start);
564 state = rb_entry(node, struct extent_state, rb_node);
566 if (state->start > end)
568 WARN_ON(state->end < start);
569 last_end = state->end;
571 /* the state doesn't have the wanted bits, go ahead */
572 if (!(state->state & bits)) {
573 state = next_state(state);
578 * | ---- desired range ---- |
580 * | ------------- state -------------- |
582 * We need to split the extent we found, and may flip
583 * bits on second half.
585 * If the extent we found extends past our range, we
586 * just split and search again. It'll get split again
587 * the next time though.
589 * If the extent we found is inside our range, we clear
590 * the desired bit on it.
593 if (state->start < start) {
594 prealloc = alloc_extent_state_atomic(prealloc);
596 err = split_state(tree, state, prealloc, start);
598 extent_io_tree_panic(tree, err);
603 if (state->end <= end) {
604 state = clear_state_bit(tree, state, &bits, wake);
610 * | ---- desired range ---- |
612 * We need to split the extent, and clear the bit
615 if (state->start <= end && state->end > end) {
616 prealloc = alloc_extent_state_atomic(prealloc);
618 err = split_state(tree, state, prealloc, end + 1);
620 extent_io_tree_panic(tree, err);
625 clear_state_bit(tree, prealloc, &bits, wake);
631 state = clear_state_bit(tree, state, &bits, wake);
633 if (last_end == (u64)-1)
635 start = last_end + 1;
636 if (start <= end && state && !need_resched())
641 spin_unlock(&tree->lock);
643 free_extent_state(prealloc);
650 spin_unlock(&tree->lock);
651 if (mask & __GFP_WAIT)
656 static void wait_on_state(struct extent_io_tree *tree,
657 struct extent_state *state)
658 __releases(tree->lock)
659 __acquires(tree->lock)
662 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
663 spin_unlock(&tree->lock);
665 spin_lock(&tree->lock);
666 finish_wait(&state->wq, &wait);
670 * waits for one or more bits to clear on a range in the state tree.
671 * The range [start, end] is inclusive.
672 * The tree lock is taken by this function
674 static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
677 struct extent_state *state;
678 struct rb_node *node;
680 spin_lock(&tree->lock);
684 * this search will find all the extents that end after
687 node = tree_search(tree, start);
691 state = rb_entry(node, struct extent_state, rb_node);
693 if (state->start > end)
696 if (state->state & bits) {
697 start = state->start;
698 atomic_inc(&state->refs);
699 wait_on_state(tree, state);
700 free_extent_state(state);
703 start = state->end + 1;
708 cond_resched_lock(&tree->lock);
711 spin_unlock(&tree->lock);
714 static void set_state_bits(struct extent_io_tree *tree,
715 struct extent_state *state,
718 unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS;
720 set_state_cb(tree, state, bits);
721 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
722 u64 range = state->end - state->start + 1;
723 tree->dirty_bytes += range;
725 state->state |= bits_to_set;
728 static void cache_state(struct extent_state *state,
729 struct extent_state **cached_ptr)
731 if (cached_ptr && !(*cached_ptr)) {
732 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
734 atomic_inc(&state->refs);
739 static void uncache_state(struct extent_state **cached_ptr)
741 if (cached_ptr && (*cached_ptr)) {
742 struct extent_state *state = *cached_ptr;
744 free_extent_state(state);
749 * set some bits on a range in the tree. This may require allocations or
750 * sleeping, so the gfp mask is used to indicate what is allowed.
752 * If any of the exclusive bits are set, this will fail with -EEXIST if some
753 * part of the range already has the desired bits set. The start of the
754 * existing range is returned in failed_start in this case.
756 * [start, end] is inclusive This takes the tree lock.
759 static int __must_check
760 __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
761 unsigned long bits, unsigned long exclusive_bits,
762 u64 *failed_start, struct extent_state **cached_state,
765 struct extent_state *state;
766 struct extent_state *prealloc = NULL;
767 struct rb_node *node;
772 bits |= EXTENT_FIRST_DELALLOC;
774 if (!prealloc && (mask & __GFP_WAIT)) {
775 prealloc = alloc_extent_state(mask);
779 spin_lock(&tree->lock);
780 if (cached_state && *cached_state) {
781 state = *cached_state;
782 if (state->start <= start && state->end > start &&
784 node = &state->rb_node;
789 * this search will find all the extents that end after
792 node = tree_search(tree, start);
794 prealloc = alloc_extent_state_atomic(prealloc);
796 err = insert_state(tree, prealloc, start, end, &bits);
798 extent_io_tree_panic(tree, err);
803 state = rb_entry(node, struct extent_state, rb_node);
805 last_start = state->start;
806 last_end = state->end;
809 * | ---- desired range ---- |
812 * Just lock what we found and keep going
814 if (state->start == start && state->end <= end) {
815 if (state->state & exclusive_bits) {
816 *failed_start = state->start;
821 set_state_bits(tree, state, &bits);
822 cache_state(state, cached_state);
823 merge_state(tree, state);
824 if (last_end == (u64)-1)
826 start = last_end + 1;
827 state = next_state(state);
828 if (start < end && state && state->start == start &&
835 * | ---- desired range ---- |
838 * | ------------- state -------------- |
840 * We need to split the extent we found, and may flip bits on
843 * If the extent we found extends past our
844 * range, we just split and search again. It'll get split
845 * again the next time though.
847 * If the extent we found is inside our range, we set the
850 if (state->start < start) {
851 if (state->state & exclusive_bits) {
852 *failed_start = start;
857 prealloc = alloc_extent_state_atomic(prealloc);
859 err = split_state(tree, state, prealloc, start);
861 extent_io_tree_panic(tree, err);
866 if (state->end <= end) {
867 set_state_bits(tree, state, &bits);
868 cache_state(state, cached_state);
869 merge_state(tree, state);
870 if (last_end == (u64)-1)
872 start = last_end + 1;
873 state = next_state(state);
874 if (start < end && state && state->start == start &&
881 * | ---- desired range ---- |
882 * | state | or | state |
884 * There's a hole, we need to insert something in it and
885 * ignore the extent we found.
887 if (state->start > start) {
889 if (end < last_start)
892 this_end = last_start - 1;
894 prealloc = alloc_extent_state_atomic(prealloc);
898 * Avoid to free 'prealloc' if it can be merged with
901 err = insert_state(tree, prealloc, start, this_end,
904 extent_io_tree_panic(tree, err);
906 cache_state(prealloc, cached_state);
908 start = this_end + 1;
912 * | ---- desired range ---- |
914 * We need to split the extent, and set the bit
917 if (state->start <= end && state->end > end) {
918 if (state->state & exclusive_bits) {
919 *failed_start = start;
924 prealloc = alloc_extent_state_atomic(prealloc);
926 err = split_state(tree, state, prealloc, end + 1);
928 extent_io_tree_panic(tree, err);
930 set_state_bits(tree, prealloc, &bits);
931 cache_state(prealloc, cached_state);
932 merge_state(tree, prealloc);
940 spin_unlock(&tree->lock);
942 free_extent_state(prealloc);
949 spin_unlock(&tree->lock);
950 if (mask & __GFP_WAIT)
955 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
956 unsigned long bits, u64 * failed_start,
957 struct extent_state **cached_state, gfp_t mask)
959 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
965 * convert_extent_bit - convert all bits in a given range from one bit to
967 * @tree: the io tree to search
968 * @start: the start offset in bytes
969 * @end: the end offset in bytes (inclusive)
970 * @bits: the bits to set in this range
971 * @clear_bits: the bits to clear in this range
972 * @cached_state: state that we're going to cache
973 * @mask: the allocation mask
975 * This will go through and set bits for the given range. If any states exist
976 * already in this range they are set with the given bit and cleared of the
977 * clear_bits. This is only meant to be used by things that are mergeable, ie
978 * converting from say DELALLOC to DIRTY. This is not meant to be used with
979 * boundary bits like LOCK.
981 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
982 unsigned long bits, unsigned long clear_bits,
983 struct extent_state **cached_state, gfp_t mask)
985 struct extent_state *state;
986 struct extent_state *prealloc = NULL;
987 struct rb_node *node;
993 if (!prealloc && (mask & __GFP_WAIT)) {
994 prealloc = alloc_extent_state(mask);
999 spin_lock(&tree->lock);
1000 if (cached_state && *cached_state) {
1001 state = *cached_state;
1002 if (state->start <= start && state->end > start &&
1004 node = &state->rb_node;
1010 * this search will find all the extents that end after
1013 node = tree_search(tree, start);
1015 prealloc = alloc_extent_state_atomic(prealloc);
1020 err = insert_state(tree, prealloc, start, end, &bits);
1023 extent_io_tree_panic(tree, err);
1026 state = rb_entry(node, struct extent_state, rb_node);
1028 last_start = state->start;
1029 last_end = state->end;
1032 * | ---- desired range ---- |
1035 * Just lock what we found and keep going
1037 if (state->start == start && state->end <= end) {
1038 set_state_bits(tree, state, &bits);
1039 cache_state(state, cached_state);
1040 state = clear_state_bit(tree, state, &clear_bits, 0);
1041 if (last_end == (u64)-1)
1043 start = last_end + 1;
1044 if (start < end && state && state->start == start &&
1051 * | ---- desired range ---- |
1054 * | ------------- state -------------- |
1056 * We need to split the extent we found, and may flip bits on
1059 * If the extent we found extends past our
1060 * range, we just split and search again. It'll get split
1061 * again the next time though.
1063 * If the extent we found is inside our range, we set the
1064 * desired bit on it.
1066 if (state->start < start) {
1067 prealloc = alloc_extent_state_atomic(prealloc);
1072 err = split_state(tree, state, prealloc, start);
1074 extent_io_tree_panic(tree, err);
1078 if (state->end <= end) {
1079 set_state_bits(tree, state, &bits);
1080 cache_state(state, cached_state);
1081 state = clear_state_bit(tree, state, &clear_bits, 0);
1082 if (last_end == (u64)-1)
1084 start = last_end + 1;
1085 if (start < end && state && state->start == start &&
1092 * | ---- desired range ---- |
1093 * | state | or | state |
1095 * There's a hole, we need to insert something in it and
1096 * ignore the extent we found.
1098 if (state->start > start) {
1100 if (end < last_start)
1103 this_end = last_start - 1;
1105 prealloc = alloc_extent_state_atomic(prealloc);
1112 * Avoid to free 'prealloc' if it can be merged with
1115 err = insert_state(tree, prealloc, start, this_end,
1118 extent_io_tree_panic(tree, err);
1119 cache_state(prealloc, cached_state);
1121 start = this_end + 1;
1125 * | ---- desired range ---- |
1127 * We need to split the extent, and set the bit
1130 if (state->start <= end && state->end > end) {
1131 prealloc = alloc_extent_state_atomic(prealloc);
1137 err = split_state(tree, state, prealloc, end + 1);
1139 extent_io_tree_panic(tree, err);
1141 set_state_bits(tree, prealloc, &bits);
1142 cache_state(prealloc, cached_state);
1143 clear_state_bit(tree, prealloc, &clear_bits, 0);
1151 spin_unlock(&tree->lock);
1153 free_extent_state(prealloc);
1160 spin_unlock(&tree->lock);
1161 if (mask & __GFP_WAIT)
1166 /* wrappers around set/clear extent bit */
1167 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1170 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
1174 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1175 unsigned long bits, gfp_t mask)
1177 return set_extent_bit(tree, start, end, bits, NULL,
1181 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1182 unsigned long bits, gfp_t mask)
1184 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
1187 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
1188 struct extent_state **cached_state, gfp_t mask)
1190 return set_extent_bit(tree, start, end,
1191 EXTENT_DELALLOC | EXTENT_UPTODATE,
1192 NULL, cached_state, mask);
1195 int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1196 struct extent_state **cached_state, gfp_t mask)
1198 return set_extent_bit(tree, start, end,
1199 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1200 NULL, cached_state, mask);
1203 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1206 return clear_extent_bit(tree, start, end,
1207 EXTENT_DIRTY | EXTENT_DELALLOC |
1208 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
1211 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1214 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
1218 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1219 struct extent_state **cached_state, gfp_t mask)
1221 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
1222 cached_state, mask);
1225 int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1226 struct extent_state **cached_state, gfp_t mask)
1228 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1229 cached_state, mask);
1233 * either insert or lock state struct between start and end use mask to tell
1234 * us if waiting is desired.
1236 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1237 unsigned long bits, struct extent_state **cached_state)
1242 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1243 EXTENT_LOCKED, &failed_start,
1244 cached_state, GFP_NOFS);
1245 if (err == -EEXIST) {
1246 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1247 start = failed_start;
1250 WARN_ON(start > end);
1255 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1257 return lock_extent_bits(tree, start, end, 0, NULL);
1260 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1265 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1266 &failed_start, NULL, GFP_NOFS);
1267 if (err == -EEXIST) {
1268 if (failed_start > start)
1269 clear_extent_bit(tree, start, failed_start - 1,
1270 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
1276 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1277 struct extent_state **cached, gfp_t mask)
1279 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1283 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1285 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1289 int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1291 unsigned long index = start >> PAGE_CACHE_SHIFT;
1292 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1295 while (index <= end_index) {
1296 page = find_get_page(inode->i_mapping, index);
1297 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1298 clear_page_dirty_for_io(page);
1299 page_cache_release(page);
1305 int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1307 unsigned long index = start >> PAGE_CACHE_SHIFT;
1308 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1311 while (index <= end_index) {
1312 page = find_get_page(inode->i_mapping, index);
1313 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1314 account_page_redirty(page);
1315 __set_page_dirty_nobuffers(page);
1316 page_cache_release(page);
1323 * helper function to set both pages and extents in the tree writeback
1325 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1327 unsigned long index = start >> PAGE_CACHE_SHIFT;
1328 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1331 while (index <= end_index) {
1332 page = find_get_page(tree->mapping, index);
1333 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1334 set_page_writeback(page);
1335 page_cache_release(page);
1341 /* find the first state struct with 'bits' set after 'start', and
1342 * return it. tree->lock must be held. NULL will returned if
1343 * nothing was found after 'start'
1345 static struct extent_state *
1346 find_first_extent_bit_state(struct extent_io_tree *tree,
1347 u64 start, unsigned long bits)
1349 struct rb_node *node;
1350 struct extent_state *state;
1353 * this search will find all the extents that end after
1356 node = tree_search(tree, start);
1361 state = rb_entry(node, struct extent_state, rb_node);
1362 if (state->end >= start && (state->state & bits))
1365 node = rb_next(node);
1374 * find the first offset in the io tree with 'bits' set. zero is
1375 * returned if we find something, and *start_ret and *end_ret are
1376 * set to reflect the state struct that was found.
1378 * If nothing was found, 1 is returned. If found something, return 0.
1380 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1381 u64 *start_ret, u64 *end_ret, unsigned long bits,
1382 struct extent_state **cached_state)
1384 struct extent_state *state;
1388 spin_lock(&tree->lock);
1389 if (cached_state && *cached_state) {
1390 state = *cached_state;
1391 if (state->end == start - 1 && state->tree) {
1392 n = rb_next(&state->rb_node);
1394 state = rb_entry(n, struct extent_state,
1396 if (state->state & bits)
1400 free_extent_state(*cached_state);
1401 *cached_state = NULL;
1404 free_extent_state(*cached_state);
1405 *cached_state = NULL;
1408 state = find_first_extent_bit_state(tree, start, bits);
1411 cache_state(state, cached_state);
1412 *start_ret = state->start;
1413 *end_ret = state->end;
1417 spin_unlock(&tree->lock);
1422 * find a contiguous range of bytes in the file marked as delalloc, not
1423 * more than 'max_bytes'. start and end are used to return the range,
1425 * 1 is returned if we find something, 0 if nothing was in the tree
1427 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1428 u64 *start, u64 *end, u64 max_bytes,
1429 struct extent_state **cached_state)
1431 struct rb_node *node;
1432 struct extent_state *state;
1433 u64 cur_start = *start;
1435 u64 total_bytes = 0;
1437 spin_lock(&tree->lock);
1440 * this search will find all the extents that end after
1443 node = tree_search(tree, cur_start);
1451 state = rb_entry(node, struct extent_state, rb_node);
1452 if (found && (state->start != cur_start ||
1453 (state->state & EXTENT_BOUNDARY))) {
1456 if (!(state->state & EXTENT_DELALLOC)) {
1462 *start = state->start;
1463 *cached_state = state;
1464 atomic_inc(&state->refs);
1468 cur_start = state->end + 1;
1469 node = rb_next(node);
1472 total_bytes += state->end - state->start + 1;
1473 if (total_bytes >= max_bytes)
1477 spin_unlock(&tree->lock);
1481 static noinline void __unlock_for_delalloc(struct inode *inode,
1482 struct page *locked_page,
1486 struct page *pages[16];
1487 unsigned long index = start >> PAGE_CACHE_SHIFT;
1488 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1489 unsigned long nr_pages = end_index - index + 1;
1492 if (index == locked_page->index && end_index == index)
1495 while (nr_pages > 0) {
1496 ret = find_get_pages_contig(inode->i_mapping, index,
1497 min_t(unsigned long, nr_pages,
1498 ARRAY_SIZE(pages)), pages);
1499 for (i = 0; i < ret; i++) {
1500 if (pages[i] != locked_page)
1501 unlock_page(pages[i]);
1502 page_cache_release(pages[i]);
1510 static noinline int lock_delalloc_pages(struct inode *inode,
1511 struct page *locked_page,
1515 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1516 unsigned long start_index = index;
1517 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1518 unsigned long pages_locked = 0;
1519 struct page *pages[16];
1520 unsigned long nrpages;
1524 /* the caller is responsible for locking the start index */
1525 if (index == locked_page->index && index == end_index)
1528 /* skip the page at the start index */
1529 nrpages = end_index - index + 1;
1530 while (nrpages > 0) {
1531 ret = find_get_pages_contig(inode->i_mapping, index,
1532 min_t(unsigned long,
1533 nrpages, ARRAY_SIZE(pages)), pages);
1538 /* now we have an array of pages, lock them all */
1539 for (i = 0; i < ret; i++) {
1541 * the caller is taking responsibility for
1544 if (pages[i] != locked_page) {
1545 lock_page(pages[i]);
1546 if (!PageDirty(pages[i]) ||
1547 pages[i]->mapping != inode->i_mapping) {
1549 unlock_page(pages[i]);
1550 page_cache_release(pages[i]);
1554 page_cache_release(pages[i]);
1563 if (ret && pages_locked) {
1564 __unlock_for_delalloc(inode, locked_page,
1566 ((u64)(start_index + pages_locked - 1)) <<
1573 * find a contiguous range of bytes in the file marked as delalloc, not
1574 * more than 'max_bytes'. start and end are used to return the range,
1576 * 1 is returned if we find something, 0 if nothing was in the tree
1578 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1579 struct extent_io_tree *tree,
1580 struct page *locked_page,
1581 u64 *start, u64 *end,
1587 struct extent_state *cached_state = NULL;
1592 /* step one, find a bunch of delalloc bytes starting at start */
1593 delalloc_start = *start;
1595 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1596 max_bytes, &cached_state);
1597 if (!found || delalloc_end <= *start) {
1598 *start = delalloc_start;
1599 *end = delalloc_end;
1600 free_extent_state(cached_state);
1605 * start comes from the offset of locked_page. We have to lock
1606 * pages in order, so we can't process delalloc bytes before
1609 if (delalloc_start < *start)
1610 delalloc_start = *start;
1613 * make sure to limit the number of pages we try to lock down
1616 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1617 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1619 /* step two, lock all the pages after the page that has start */
1620 ret = lock_delalloc_pages(inode, locked_page,
1621 delalloc_start, delalloc_end);
1622 if (ret == -EAGAIN) {
1623 /* some of the pages are gone, lets avoid looping by
1624 * shortening the size of the delalloc range we're searching
1626 free_extent_state(cached_state);
1627 cached_state = NULL;
1629 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1630 max_bytes = PAGE_CACHE_SIZE - offset;
1638 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
1640 /* step three, lock the state bits for the whole range */
1641 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
1643 /* then test to make sure it is all still delalloc */
1644 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1645 EXTENT_DELALLOC, 1, cached_state);
1647 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1648 &cached_state, GFP_NOFS);
1649 __unlock_for_delalloc(inode, locked_page,
1650 delalloc_start, delalloc_end);
1654 free_extent_state(cached_state);
1655 *start = delalloc_start;
1656 *end = delalloc_end;
1661 int extent_clear_unlock_delalloc(struct inode *inode,
1662 struct extent_io_tree *tree,
1663 u64 start, u64 end, struct page *locked_page,
1667 struct page *pages[16];
1668 unsigned long index = start >> PAGE_CACHE_SHIFT;
1669 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1670 unsigned long nr_pages = end_index - index + 1;
1672 unsigned long clear_bits = 0;
1674 if (op & EXTENT_CLEAR_UNLOCK)
1675 clear_bits |= EXTENT_LOCKED;
1676 if (op & EXTENT_CLEAR_DIRTY)
1677 clear_bits |= EXTENT_DIRTY;
1679 if (op & EXTENT_CLEAR_DELALLOC)
1680 clear_bits |= EXTENT_DELALLOC;
1682 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1683 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1684 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1685 EXTENT_SET_PRIVATE2)))
1688 while (nr_pages > 0) {
1689 ret = find_get_pages_contig(inode->i_mapping, index,
1690 min_t(unsigned long,
1691 nr_pages, ARRAY_SIZE(pages)), pages);
1692 for (i = 0; i < ret; i++) {
1694 if (op & EXTENT_SET_PRIVATE2)
1695 SetPagePrivate2(pages[i]);
1697 if (pages[i] == locked_page) {
1698 page_cache_release(pages[i]);
1701 if (op & EXTENT_CLEAR_DIRTY)
1702 clear_page_dirty_for_io(pages[i]);
1703 if (op & EXTENT_SET_WRITEBACK)
1704 set_page_writeback(pages[i]);
1705 if (op & EXTENT_END_WRITEBACK)
1706 end_page_writeback(pages[i]);
1707 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1708 unlock_page(pages[i]);
1709 page_cache_release(pages[i]);
1719 * count the number of bytes in the tree that have a given bit(s)
1720 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1721 * cached. The total number found is returned.
1723 u64 count_range_bits(struct extent_io_tree *tree,
1724 u64 *start, u64 search_end, u64 max_bytes,
1725 unsigned long bits, int contig)
1727 struct rb_node *node;
1728 struct extent_state *state;
1729 u64 cur_start = *start;
1730 u64 total_bytes = 0;
1734 if (search_end <= cur_start) {
1739 spin_lock(&tree->lock);
1740 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1741 total_bytes = tree->dirty_bytes;
1745 * this search will find all the extents that end after
1748 node = tree_search(tree, cur_start);
1753 state = rb_entry(node, struct extent_state, rb_node);
1754 if (state->start > search_end)
1756 if (contig && found && state->start > last + 1)
1758 if (state->end >= cur_start && (state->state & bits) == bits) {
1759 total_bytes += min(search_end, state->end) + 1 -
1760 max(cur_start, state->start);
1761 if (total_bytes >= max_bytes)
1764 *start = max(cur_start, state->start);
1768 } else if (contig && found) {
1771 node = rb_next(node);
1776 spin_unlock(&tree->lock);
1781 * set the private field for a given byte offset in the tree. If there isn't
1782 * an extent_state there already, this does nothing.
1784 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1786 struct rb_node *node;
1787 struct extent_state *state;
1790 spin_lock(&tree->lock);
1792 * this search will find all the extents that end after
1795 node = tree_search(tree, start);
1800 state = rb_entry(node, struct extent_state, rb_node);
1801 if (state->start != start) {
1805 state->private = private;
1807 spin_unlock(&tree->lock);
1811 void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 csums[],
1814 struct rb_node *node;
1815 struct extent_state *state;
1817 spin_lock(&tree->lock);
1819 * this search will find all the extents that end after
1822 node = tree_search(tree, start);
1825 state = rb_entry(node, struct extent_state, rb_node);
1826 BUG_ON(state->start != start);
1829 state->private = *csums++;
1831 state = next_state(state);
1833 spin_unlock(&tree->lock);
1836 static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index)
1838 struct bio_vec *bvec = bio->bi_io_vec + bio_index;
1840 return page_offset(bvec->bv_page) + bvec->bv_offset;
1843 void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int bio_index,
1844 u32 csums[], int count)
1846 struct rb_node *node;
1847 struct extent_state *state = NULL;
1850 spin_lock(&tree->lock);
1852 start = __btrfs_get_bio_offset(bio, bio_index);
1853 if (state == NULL || state->start != start) {
1854 node = tree_search(tree, start);
1857 state = rb_entry(node, struct extent_state, rb_node);
1858 BUG_ON(state->start != start);
1860 state->private = *csums++;
1864 state = next_state(state);
1866 spin_unlock(&tree->lock);
1869 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1871 struct rb_node *node;
1872 struct extent_state *state;
1875 spin_lock(&tree->lock);
1877 * this search will find all the extents that end after
1880 node = tree_search(tree, start);
1885 state = rb_entry(node, struct extent_state, rb_node);
1886 if (state->start != start) {
1890 *private = state->private;
1892 spin_unlock(&tree->lock);
1897 * searches a range in the state tree for a given mask.
1898 * If 'filled' == 1, this returns 1 only if every extent in the tree
1899 * has the bits set. Otherwise, 1 is returned if any bit in the
1900 * range is found set.
1902 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1903 unsigned long bits, int filled, struct extent_state *cached)
1905 struct extent_state *state = NULL;
1906 struct rb_node *node;
1909 spin_lock(&tree->lock);
1910 if (cached && cached->tree && cached->start <= start &&
1911 cached->end > start)
1912 node = &cached->rb_node;
1914 node = tree_search(tree, start);
1915 while (node && start <= end) {
1916 state = rb_entry(node, struct extent_state, rb_node);
1918 if (filled && state->start > start) {
1923 if (state->start > end)
1926 if (state->state & bits) {
1930 } else if (filled) {
1935 if (state->end == (u64)-1)
1938 start = state->end + 1;
1941 node = rb_next(node);
1948 spin_unlock(&tree->lock);
1953 * helper function to set a given page up to date if all the
1954 * extents in the tree for that page are up to date
1956 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
1958 u64 start = page_offset(page);
1959 u64 end = start + PAGE_CACHE_SIZE - 1;
1960 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1961 SetPageUptodate(page);
1965 * When IO fails, either with EIO or csum verification fails, we
1966 * try other mirrors that might have a good copy of the data. This
1967 * io_failure_record is used to record state as we go through all the
1968 * mirrors. If another mirror has good data, the page is set up to date
1969 * and things continue. If a good mirror can't be found, the original
1970 * bio end_io callback is called to indicate things have failed.
1972 struct io_failure_record {
1977 unsigned long bio_flags;
1983 static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1988 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1990 set_state_private(failure_tree, rec->start, 0);
1991 ret = clear_extent_bits(failure_tree, rec->start,
1992 rec->start + rec->len - 1,
1993 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1997 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1998 rec->start + rec->len - 1,
1999 EXTENT_DAMAGED, GFP_NOFS);
2007 static void repair_io_failure_callback(struct bio *bio, int err)
2009 complete(bio->bi_private);
2013 * this bypasses the standard btrfs submit functions deliberately, as
2014 * the standard behavior is to write all copies in a raid setup. here we only
2015 * want to write the one bad copy. so we do the mapping for ourselves and issue
2016 * submit_bio directly.
2017 * to avoid any synchronization issues, wait for the data after writing, which
2018 * actually prevents the read that triggered the error from finishing.
2019 * currently, there can be no more than two copies of every data bit. thus,
2020 * exactly one rewrite is required.
2022 int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
2023 u64 length, u64 logical, struct page *page,
2027 struct btrfs_device *dev;
2028 DECLARE_COMPLETION_ONSTACK(compl);
2031 struct btrfs_bio *bbio = NULL;
2032 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
2035 BUG_ON(!mirror_num);
2037 /* we can't repair anything in raid56 yet */
2038 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2041 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
2044 bio->bi_private = &compl;
2045 bio->bi_end_io = repair_io_failure_callback;
2047 map_length = length;
2049 ret = btrfs_map_block(fs_info, WRITE, logical,
2050 &map_length, &bbio, mirror_num);
2055 BUG_ON(mirror_num != bbio->mirror_num);
2056 sector = bbio->stripes[mirror_num-1].physical >> 9;
2057 bio->bi_sector = sector;
2058 dev = bbio->stripes[mirror_num-1].dev;
2060 if (!dev || !dev->bdev || !dev->writeable) {
2064 bio->bi_bdev = dev->bdev;
2065 bio_add_page(bio, page, length, start - page_offset(page));
2066 btrfsic_submit_bio(WRITE_SYNC, bio);
2067 wait_for_completion(&compl);
2069 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2070 /* try to remap that extent elsewhere? */
2072 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
2076 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
2077 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2078 start, rcu_str_deref(dev->name), sector);
2084 int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2087 u64 start = eb->start;
2088 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
2091 for (i = 0; i < num_pages; i++) {
2092 struct page *p = extent_buffer_page(eb, i);
2093 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
2094 start, p, mirror_num);
2097 start += PAGE_CACHE_SIZE;
2104 * each time an IO finishes, we do a fast check in the IO failure tree
2105 * to see if we need to process or clean up an io_failure_record
2107 static int clean_io_failure(u64 start, struct page *page)
2110 u64 private_failure;
2111 struct io_failure_record *failrec;
2112 struct btrfs_fs_info *fs_info;
2113 struct extent_state *state;
2117 struct inode *inode = page->mapping->host;
2120 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2121 (u64)-1, 1, EXTENT_DIRTY, 0);
2125 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2130 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2131 BUG_ON(!failrec->this_mirror);
2133 if (failrec->in_validation) {
2134 /* there was no real error, just free the record */
2135 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2141 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2142 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2145 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2147 if (state && state->start == failrec->start) {
2148 fs_info = BTRFS_I(inode)->root->fs_info;
2149 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2151 if (num_copies > 1) {
2152 ret = repair_io_failure(fs_info, start, failrec->len,
2153 failrec->logical, page,
2154 failrec->failed_mirror);
2162 ret = free_io_failure(inode, failrec, did_repair);
2168 * this is a generic handler for readpage errors (default
2169 * readpage_io_failed_hook). if other copies exist, read those and write back
2170 * good data to the failed position. does not investigate in remapping the
2171 * failed extent elsewhere, hoping the device will be smart enough to do this as
2175 static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2176 u64 start, u64 end, int failed_mirror,
2177 struct extent_state *state)
2179 struct io_failure_record *failrec = NULL;
2181 struct extent_map *em;
2182 struct inode *inode = page->mapping->host;
2183 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2184 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2185 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2192 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2194 ret = get_state_private(failure_tree, start, &private);
2196 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2199 failrec->start = start;
2200 failrec->len = end - start + 1;
2201 failrec->this_mirror = 0;
2202 failrec->bio_flags = 0;
2203 failrec->in_validation = 0;
2205 read_lock(&em_tree->lock);
2206 em = lookup_extent_mapping(em_tree, start, failrec->len);
2208 read_unlock(&em_tree->lock);
2213 if (em->start > start || em->start + em->len < start) {
2214 free_extent_map(em);
2217 read_unlock(&em_tree->lock);
2223 logical = start - em->start;
2224 logical = em->block_start + logical;
2225 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2226 logical = em->block_start;
2227 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2228 extent_set_compress_type(&failrec->bio_flags,
2231 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2232 "len=%llu\n", logical, start, failrec->len);
2233 failrec->logical = logical;
2234 free_extent_map(em);
2236 /* set the bits in the private failure tree */
2237 ret = set_extent_bits(failure_tree, start, end,
2238 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2240 ret = set_state_private(failure_tree, start,
2241 (u64)(unsigned long)failrec);
2242 /* set the bits in the inode's tree */
2244 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2251 failrec = (struct io_failure_record *)(unsigned long)private;
2252 pr_debug("bio_readpage_error: (found) logical=%llu, "
2253 "start=%llu, len=%llu, validation=%d\n",
2254 failrec->logical, failrec->start, failrec->len,
2255 failrec->in_validation);
2257 * when data can be on disk more than twice, add to failrec here
2258 * (e.g. with a list for failed_mirror) to make
2259 * clean_io_failure() clean all those errors at once.
2262 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2263 failrec->logical, failrec->len);
2264 if (num_copies == 1) {
2266 * we only have a single copy of the data, so don't bother with
2267 * all the retry and error correction code that follows. no
2268 * matter what the error is, it is very likely to persist.
2270 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2271 "state=%p, num_copies=%d, next_mirror %d, "
2272 "failed_mirror %d\n", state, num_copies,
2273 failrec->this_mirror, failed_mirror);
2274 free_io_failure(inode, failrec, 0);
2279 spin_lock(&tree->lock);
2280 state = find_first_extent_bit_state(tree, failrec->start,
2282 if (state && state->start != failrec->start)
2284 spin_unlock(&tree->lock);
2288 * there are two premises:
2289 * a) deliver good data to the caller
2290 * b) correct the bad sectors on disk
2292 if (failed_bio->bi_vcnt > 1) {
2294 * to fulfill b), we need to know the exact failing sectors, as
2295 * we don't want to rewrite any more than the failed ones. thus,
2296 * we need separate read requests for the failed bio
2298 * if the following BUG_ON triggers, our validation request got
2299 * merged. we need separate requests for our algorithm to work.
2301 BUG_ON(failrec->in_validation);
2302 failrec->in_validation = 1;
2303 failrec->this_mirror = failed_mirror;
2304 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2307 * we're ready to fulfill a) and b) alongside. get a good copy
2308 * of the failed sector and if we succeed, we have setup
2309 * everything for repair_io_failure to do the rest for us.
2311 if (failrec->in_validation) {
2312 BUG_ON(failrec->this_mirror != failed_mirror);
2313 failrec->in_validation = 0;
2314 failrec->this_mirror = 0;
2316 failrec->failed_mirror = failed_mirror;
2317 failrec->this_mirror++;
2318 if (failrec->this_mirror == failed_mirror)
2319 failrec->this_mirror++;
2320 read_mode = READ_SYNC;
2323 if (!state || failrec->this_mirror > num_copies) {
2324 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2325 "next_mirror %d, failed_mirror %d\n", state,
2326 num_copies, failrec->this_mirror, failed_mirror);
2327 free_io_failure(inode, failrec, 0);
2331 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
2333 free_io_failure(inode, failrec, 0);
2336 bio->bi_private = state;
2337 bio->bi_end_io = failed_bio->bi_end_io;
2338 bio->bi_sector = failrec->logical >> 9;
2339 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2342 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2344 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2345 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2346 failrec->this_mirror, num_copies, failrec->in_validation);
2348 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2349 failrec->this_mirror,
2350 failrec->bio_flags, 0);
2354 /* lots and lots of room for performance fixes in the end_bio funcs */
2356 int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2358 int uptodate = (err == 0);
2359 struct extent_io_tree *tree;
2362 tree = &BTRFS_I(page->mapping->host)->io_tree;
2364 if (tree->ops && tree->ops->writepage_end_io_hook) {
2365 ret = tree->ops->writepage_end_io_hook(page, start,
2366 end, NULL, uptodate);
2372 ClearPageUptodate(page);
2379 * after a writepage IO is done, we need to:
2380 * clear the uptodate bits on error
2381 * clear the writeback bits in the extent tree for this IO
2382 * end_page_writeback if the page has no more pending IO
2384 * Scheduling is not allowed, so the extent state tree is expected
2385 * to have one and only one object corresponding to this IO.
2387 static void end_bio_extent_writepage(struct bio *bio, int err)
2389 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2390 struct extent_io_tree *tree;
2395 struct page *page = bvec->bv_page;
2396 tree = &BTRFS_I(page->mapping->host)->io_tree;
2398 /* We always issue full-page reads, but if some block
2399 * in a page fails to read, blk_update_request() will
2400 * advance bv_offset and adjust bv_len to compensate.
2401 * Print a warning for nonzero offsets, and an error
2402 * if they don't add up to a full page. */
2403 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2404 printk("%s page write in btrfs with offset %u and length %u\n",
2405 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2406 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2407 bvec->bv_offset, bvec->bv_len);
2409 start = page_offset(page);
2410 end = start + bvec->bv_offset + bvec->bv_len - 1;
2412 if (--bvec >= bio->bi_io_vec)
2413 prefetchw(&bvec->bv_page->flags);
2415 if (end_extent_writepage(page, err, start, end))
2418 end_page_writeback(page);
2419 } while (bvec >= bio->bi_io_vec);
2425 * after a readpage IO is done, we need to:
2426 * clear the uptodate bits on error
2427 * set the uptodate bits if things worked
2428 * set the page up to date if all extents in the tree are uptodate
2429 * clear the lock bit in the extent tree
2430 * unlock the page if there are no other extents locked for it
2432 * Scheduling is not allowed, so the extent state tree is expected
2433 * to have one and only one object corresponding to this IO.
2435 static void end_bio_extent_readpage(struct bio *bio, int err)
2437 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
2438 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2439 struct bio_vec *bvec = bio->bi_io_vec;
2440 struct extent_io_tree *tree;
2450 struct page *page = bvec->bv_page;
2451 struct extent_state *cached = NULL;
2452 struct extent_state *state;
2453 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
2455 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2456 "mirror=%lu\n", (u64)bio->bi_sector, err,
2457 io_bio->mirror_num);
2458 tree = &BTRFS_I(page->mapping->host)->io_tree;
2460 /* We always issue full-page reads, but if some block
2461 * in a page fails to read, blk_update_request() will
2462 * advance bv_offset and adjust bv_len to compensate.
2463 * Print a warning for nonzero offsets, and an error
2464 * if they don't add up to a full page. */
2465 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2466 printk("%s page read in btrfs with offset %u and length %u\n",
2467 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2468 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2469 bvec->bv_offset, bvec->bv_len);
2471 start = page_offset(page);
2472 end = start + bvec->bv_offset + bvec->bv_len - 1;
2474 if (++bvec <= bvec_end)
2475 prefetchw(&bvec->bv_page->flags);
2477 spin_lock(&tree->lock);
2478 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
2479 if (state && state->start == start) {
2481 * take a reference on the state, unlock will drop
2484 cache_state(state, &cached);
2486 spin_unlock(&tree->lock);
2488 mirror = io_bio->mirror_num;
2489 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2490 ret = tree->ops->readpage_end_io_hook(page, start, end,
2495 clean_io_failure(start, page);
2498 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
2499 ret = tree->ops->readpage_io_failed_hook(page, mirror);
2501 test_bit(BIO_UPTODATE, &bio->bi_flags))
2503 } else if (!uptodate) {
2505 * The generic bio_readpage_error handles errors the
2506 * following way: If possible, new read requests are
2507 * created and submitted and will end up in
2508 * end_bio_extent_readpage as well (if we're lucky, not
2509 * in the !uptodate case). In that case it returns 0 and
2510 * we just go on with the next page in our bio. If it
2511 * can't handle the error it will return -EIO and we
2512 * remain responsible for that page.
2514 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
2517 test_bit(BIO_UPTODATE, &bio->bi_flags);
2520 uncache_state(&cached);
2525 if (uptodate && tree->track_uptodate) {
2526 set_extent_uptodate(tree, start, end, &cached,
2529 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2532 SetPageUptodate(page);
2534 ClearPageUptodate(page);
2538 } while (bvec <= bvec_end);
2544 * this allocates from the btrfs_bioset. We're returning a bio right now
2545 * but you can call btrfs_io_bio for the appropriate container_of magic
2548 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2553 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
2555 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2556 while (!bio && (nr_vecs /= 2)) {
2557 bio = bio_alloc_bioset(gfp_flags,
2558 nr_vecs, btrfs_bioset);
2564 bio->bi_bdev = bdev;
2565 bio->bi_sector = first_sector;
2570 struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2572 return bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2576 /* this also allocates from the btrfs_bioset */
2577 struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2579 return bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2583 static int __must_check submit_one_bio(int rw, struct bio *bio,
2584 int mirror_num, unsigned long bio_flags)
2587 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2588 struct page *page = bvec->bv_page;
2589 struct extent_io_tree *tree = bio->bi_private;
2592 start = page_offset(page) + bvec->bv_offset;
2594 bio->bi_private = NULL;
2598 if (tree->ops && tree->ops->submit_bio_hook)
2599 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
2600 mirror_num, bio_flags, start);
2602 btrfsic_submit_bio(rw, bio);
2604 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2610 static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
2611 unsigned long offset, size_t size, struct bio *bio,
2612 unsigned long bio_flags)
2615 if (tree->ops && tree->ops->merge_bio_hook)
2616 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
2623 static int submit_extent_page(int rw, struct extent_io_tree *tree,
2624 struct page *page, sector_t sector,
2625 size_t size, unsigned long offset,
2626 struct block_device *bdev,
2627 struct bio **bio_ret,
2628 unsigned long max_pages,
2629 bio_end_io_t end_io_func,
2631 unsigned long prev_bio_flags,
2632 unsigned long bio_flags)
2638 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2639 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
2640 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
2642 if (bio_ret && *bio_ret) {
2645 contig = bio->bi_sector == sector;
2647 contig = bio_end_sector(bio) == sector;
2649 if (prev_bio_flags != bio_flags || !contig ||
2650 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
2651 bio_add_page(bio, page, page_size, offset) < page_size) {
2652 ret = submit_one_bio(rw, bio, mirror_num,
2661 if (this_compressed)
2664 nr = bio_get_nr_vecs(bdev);
2666 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
2670 bio_add_page(bio, page, page_size, offset);
2671 bio->bi_end_io = end_io_func;
2672 bio->bi_private = tree;
2677 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2682 static void attach_extent_buffer_page(struct extent_buffer *eb,
2685 if (!PagePrivate(page)) {
2686 SetPagePrivate(page);
2687 page_cache_get(page);
2688 set_page_private(page, (unsigned long)eb);
2690 WARN_ON(page->private != (unsigned long)eb);
2694 void set_page_extent_mapped(struct page *page)
2696 if (!PagePrivate(page)) {
2697 SetPagePrivate(page);
2698 page_cache_get(page);
2699 set_page_private(page, EXTENT_PAGE_PRIVATE);
2704 * basic readpage implementation. Locked extent state structs are inserted
2705 * into the tree that are removed when the IO is done (by the end_io
2707 * XXX JDM: This needs looking at to ensure proper page locking
2709 static int __extent_read_full_page(struct extent_io_tree *tree,
2711 get_extent_t *get_extent,
2712 struct bio **bio, int mirror_num,
2713 unsigned long *bio_flags, int rw)
2715 struct inode *inode = page->mapping->host;
2716 u64 start = page_offset(page);
2717 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2721 u64 last_byte = i_size_read(inode);
2725 struct extent_map *em;
2726 struct block_device *bdev;
2727 struct btrfs_ordered_extent *ordered;
2730 size_t pg_offset = 0;
2732 size_t disk_io_size;
2733 size_t blocksize = inode->i_sb->s_blocksize;
2734 unsigned long this_bio_flag = 0;
2736 set_page_extent_mapped(page);
2738 if (!PageUptodate(page)) {
2739 if (cleancache_get_page(page) == 0) {
2740 BUG_ON(blocksize != PAGE_SIZE);
2747 lock_extent(tree, start, end);
2748 ordered = btrfs_lookup_ordered_extent(inode, start);
2751 unlock_extent(tree, start, end);
2752 btrfs_start_ordered_extent(inode, ordered, 1);
2753 btrfs_put_ordered_extent(ordered);
2756 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2758 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2761 iosize = PAGE_CACHE_SIZE - zero_offset;
2762 userpage = kmap_atomic(page);
2763 memset(userpage + zero_offset, 0, iosize);
2764 flush_dcache_page(page);
2765 kunmap_atomic(userpage);
2768 while (cur <= end) {
2769 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2771 if (cur >= last_byte) {
2773 struct extent_state *cached = NULL;
2775 iosize = PAGE_CACHE_SIZE - pg_offset;
2776 userpage = kmap_atomic(page);
2777 memset(userpage + pg_offset, 0, iosize);
2778 flush_dcache_page(page);
2779 kunmap_atomic(userpage);
2780 set_extent_uptodate(tree, cur, cur + iosize - 1,
2782 unlock_extent_cached(tree, cur, cur + iosize - 1,
2786 em = get_extent(inode, page, pg_offset, cur,
2788 if (IS_ERR_OR_NULL(em)) {
2790 unlock_extent(tree, cur, end);
2793 extent_offset = cur - em->start;
2794 BUG_ON(extent_map_end(em) <= cur);
2797 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2798 this_bio_flag = EXTENT_BIO_COMPRESSED;
2799 extent_set_compress_type(&this_bio_flag,
2803 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2804 cur_end = min(extent_map_end(em) - 1, end);
2805 iosize = ALIGN(iosize, blocksize);
2806 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2807 disk_io_size = em->block_len;
2808 sector = em->block_start >> 9;
2810 sector = (em->block_start + extent_offset) >> 9;
2811 disk_io_size = iosize;
2814 block_start = em->block_start;
2815 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2816 block_start = EXTENT_MAP_HOLE;
2817 free_extent_map(em);
2820 /* we've found a hole, just zero and go on */
2821 if (block_start == EXTENT_MAP_HOLE) {
2823 struct extent_state *cached = NULL;
2825 userpage = kmap_atomic(page);
2826 memset(userpage + pg_offset, 0, iosize);
2827 flush_dcache_page(page);
2828 kunmap_atomic(userpage);
2830 set_extent_uptodate(tree, cur, cur + iosize - 1,
2832 unlock_extent_cached(tree, cur, cur + iosize - 1,
2835 pg_offset += iosize;
2838 /* the get_extent function already copied into the page */
2839 if (test_range_bit(tree, cur, cur_end,
2840 EXTENT_UPTODATE, 1, NULL)) {
2841 check_page_uptodate(tree, page);
2842 unlock_extent(tree, cur, cur + iosize - 1);
2844 pg_offset += iosize;
2847 /* we have an inline extent but it didn't get marked up
2848 * to date. Error out
2850 if (block_start == EXTENT_MAP_INLINE) {
2852 unlock_extent(tree, cur, cur + iosize - 1);
2854 pg_offset += iosize;
2859 ret = submit_extent_page(rw, tree, page,
2860 sector, disk_io_size, pg_offset,
2862 end_bio_extent_readpage, mirror_num,
2867 *bio_flags = this_bio_flag;
2870 unlock_extent(tree, cur, cur + iosize - 1);
2873 pg_offset += iosize;
2877 if (!PageError(page))
2878 SetPageUptodate(page);
2884 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2885 get_extent_t *get_extent, int mirror_num)
2887 struct bio *bio = NULL;
2888 unsigned long bio_flags = 0;
2891 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2894 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2898 static noinline void update_nr_written(struct page *page,
2899 struct writeback_control *wbc,
2900 unsigned long nr_written)
2902 wbc->nr_to_write -= nr_written;
2903 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2904 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2905 page->mapping->writeback_index = page->index + nr_written;
2909 * the writepage semantics are similar to regular writepage. extent
2910 * records are inserted to lock ranges in the tree, and as dirty areas
2911 * are found, they are marked writeback. Then the lock bits are removed
2912 * and the end_io handler clears the writeback ranges
2914 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2917 struct inode *inode = page->mapping->host;
2918 struct extent_page_data *epd = data;
2919 struct extent_io_tree *tree = epd->tree;
2920 u64 start = page_offset(page);
2922 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2926 u64 last_byte = i_size_read(inode);
2930 struct extent_state *cached_state = NULL;
2931 struct extent_map *em;
2932 struct block_device *bdev;
2935 size_t pg_offset = 0;
2937 loff_t i_size = i_size_read(inode);
2938 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2944 unsigned long nr_written = 0;
2945 bool fill_delalloc = true;
2947 if (wbc->sync_mode == WB_SYNC_ALL)
2948 write_flags = WRITE_SYNC;
2950 write_flags = WRITE;
2952 trace___extent_writepage(page, inode, wbc);
2954 WARN_ON(!PageLocked(page));
2956 ClearPageError(page);
2958 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2959 if (page->index > end_index ||
2960 (page->index == end_index && !pg_offset)) {
2961 page->mapping->a_ops->invalidatepage(page, 0);
2966 if (page->index == end_index) {
2969 userpage = kmap_atomic(page);
2970 memset(userpage + pg_offset, 0,
2971 PAGE_CACHE_SIZE - pg_offset);
2972 kunmap_atomic(userpage);
2973 flush_dcache_page(page);
2977 set_page_extent_mapped(page);
2979 if (!tree->ops || !tree->ops->fill_delalloc)
2980 fill_delalloc = false;
2982 delalloc_start = start;
2985 if (!epd->extent_locked && fill_delalloc) {
2986 u64 delalloc_to_write = 0;
2988 * make sure the wbc mapping index is at least updated
2991 update_nr_written(page, wbc, 0);
2993 while (delalloc_end < page_end) {
2994 nr_delalloc = find_lock_delalloc_range(inode, tree,
2999 if (nr_delalloc == 0) {
3000 delalloc_start = delalloc_end + 1;
3003 ret = tree->ops->fill_delalloc(inode, page,
3008 /* File system has been set read-only */
3014 * delalloc_end is already one less than the total
3015 * length, so we don't subtract one from
3018 delalloc_to_write += (delalloc_end - delalloc_start +
3021 delalloc_start = delalloc_end + 1;
3023 if (wbc->nr_to_write < delalloc_to_write) {
3026 if (delalloc_to_write < thresh * 2)
3027 thresh = delalloc_to_write;
3028 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3032 /* did the fill delalloc function already unlock and start
3038 * we've unlocked the page, so we can't update
3039 * the mapping's writeback index, just update
3042 wbc->nr_to_write -= nr_written;
3046 if (tree->ops && tree->ops->writepage_start_hook) {
3047 ret = tree->ops->writepage_start_hook(page, start,
3050 /* Fixup worker will requeue */
3052 wbc->pages_skipped++;
3054 redirty_page_for_writepage(wbc, page);
3055 update_nr_written(page, wbc, nr_written);
3063 * we don't want to touch the inode after unlocking the page,
3064 * so we update the mapping writeback index now
3066 update_nr_written(page, wbc, nr_written + 1);
3069 if (last_byte <= start) {
3070 if (tree->ops && tree->ops->writepage_end_io_hook)
3071 tree->ops->writepage_end_io_hook(page, start,
3076 blocksize = inode->i_sb->s_blocksize;
3078 while (cur <= end) {
3079 if (cur >= last_byte) {
3080 if (tree->ops && tree->ops->writepage_end_io_hook)
3081 tree->ops->writepage_end_io_hook(page, cur,
3085 em = epd->get_extent(inode, page, pg_offset, cur,
3087 if (IS_ERR_OR_NULL(em)) {
3092 extent_offset = cur - em->start;
3093 BUG_ON(extent_map_end(em) <= cur);
3095 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3096 iosize = ALIGN(iosize, blocksize);
3097 sector = (em->block_start + extent_offset) >> 9;
3099 block_start = em->block_start;
3100 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3101 free_extent_map(em);
3105 * compressed and inline extents are written through other
3108 if (compressed || block_start == EXTENT_MAP_HOLE ||
3109 block_start == EXTENT_MAP_INLINE) {
3111 * end_io notification does not happen here for
3112 * compressed extents
3114 if (!compressed && tree->ops &&
3115 tree->ops->writepage_end_io_hook)
3116 tree->ops->writepage_end_io_hook(page, cur,
3119 else if (compressed) {
3120 /* we don't want to end_page_writeback on
3121 * a compressed extent. this happens
3128 pg_offset += iosize;
3131 /* leave this out until we have a page_mkwrite call */
3132 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
3133 EXTENT_DIRTY, 0, NULL)) {
3135 pg_offset += iosize;
3139 if (tree->ops && tree->ops->writepage_io_hook) {
3140 ret = tree->ops->writepage_io_hook(page, cur,
3148 unsigned long max_nr = end_index + 1;
3150 set_range_writeback(tree, cur, cur + iosize - 1);
3151 if (!PageWriteback(page)) {
3152 printk(KERN_ERR "btrfs warning page %lu not "
3153 "writeback, cur %llu end %llu\n",
3154 page->index, (unsigned long long)cur,
3155 (unsigned long long)end);
3158 ret = submit_extent_page(write_flags, tree, page,
3159 sector, iosize, pg_offset,
3160 bdev, &epd->bio, max_nr,
3161 end_bio_extent_writepage,
3167 pg_offset += iosize;
3172 /* make sure the mapping tag for page dirty gets cleared */
3173 set_page_writeback(page);
3174 end_page_writeback(page);
3180 /* drop our reference on any cached states */
3181 free_extent_state(cached_state);
3185 static int eb_wait(void *word)
3191 void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3193 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3194 TASK_UNINTERRUPTIBLE);
3197 static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3198 struct btrfs_fs_info *fs_info,
3199 struct extent_page_data *epd)
3201 unsigned long i, num_pages;
3205 if (!btrfs_try_tree_write_lock(eb)) {
3207 flush_write_bio(epd);
3208 btrfs_tree_lock(eb);
3211 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3212 btrfs_tree_unlock(eb);
3216 flush_write_bio(epd);
3220 wait_on_extent_buffer_writeback(eb);
3221 btrfs_tree_lock(eb);
3222 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3224 btrfs_tree_unlock(eb);
3229 * We need to do this to prevent races in people who check if the eb is
3230 * under IO since we can end up having no IO bits set for a short period
3233 spin_lock(&eb->refs_lock);
3234 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3235 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3236 spin_unlock(&eb->refs_lock);
3237 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3238 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3240 fs_info->dirty_metadata_batch);
3243 spin_unlock(&eb->refs_lock);
3246 btrfs_tree_unlock(eb);
3251 num_pages = num_extent_pages(eb->start, eb->len);
3252 for (i = 0; i < num_pages; i++) {
3253 struct page *p = extent_buffer_page(eb, i);
3255 if (!trylock_page(p)) {
3257 flush_write_bio(epd);
3267 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3269 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3270 smp_mb__after_clear_bit();
3271 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3274 static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3276 int uptodate = err == 0;
3277 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3278 struct extent_buffer *eb;
3282 struct page *page = bvec->bv_page;
3285 eb = (struct extent_buffer *)page->private;
3287 done = atomic_dec_and_test(&eb->io_pages);
3289 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3290 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3291 ClearPageUptodate(page);
3295 end_page_writeback(page);
3300 end_extent_buffer_writeback(eb);
3301 } while (bvec >= bio->bi_io_vec);
3307 static int write_one_eb(struct extent_buffer *eb,
3308 struct btrfs_fs_info *fs_info,
3309 struct writeback_control *wbc,
3310 struct extent_page_data *epd)
3312 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3313 u64 offset = eb->start;
3314 unsigned long i, num_pages;
3315 unsigned long bio_flags = 0;
3316 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
3319 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3320 num_pages = num_extent_pages(eb->start, eb->len);
3321 atomic_set(&eb->io_pages, num_pages);
3322 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3323 bio_flags = EXTENT_BIO_TREE_LOG;
3325 for (i = 0; i < num_pages; i++) {
3326 struct page *p = extent_buffer_page(eb, i);
3328 clear_page_dirty_for_io(p);
3329 set_page_writeback(p);
3330 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3331 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3332 -1, end_bio_extent_buffer_writepage,
3333 0, epd->bio_flags, bio_flags);
3334 epd->bio_flags = bio_flags;
3336 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3338 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3339 end_extent_buffer_writeback(eb);
3343 offset += PAGE_CACHE_SIZE;
3344 update_nr_written(p, wbc, 1);
3348 if (unlikely(ret)) {
3349 for (; i < num_pages; i++) {
3350 struct page *p = extent_buffer_page(eb, i);
3358 int btree_write_cache_pages(struct address_space *mapping,
3359 struct writeback_control *wbc)
3361 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3362 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3363 struct extent_buffer *eb, *prev_eb = NULL;
3364 struct extent_page_data epd = {
3368 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3373 int nr_to_write_done = 0;
3374 struct pagevec pvec;
3377 pgoff_t end; /* Inclusive */
3381 pagevec_init(&pvec, 0);
3382 if (wbc->range_cyclic) {
3383 index = mapping->writeback_index; /* Start from prev offset */
3386 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3387 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3390 if (wbc->sync_mode == WB_SYNC_ALL)
3391 tag = PAGECACHE_TAG_TOWRITE;
3393 tag = PAGECACHE_TAG_DIRTY;
3395 if (wbc->sync_mode == WB_SYNC_ALL)
3396 tag_pages_for_writeback(mapping, index, end);
3397 while (!done && !nr_to_write_done && (index <= end) &&
3398 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3399 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3403 for (i = 0; i < nr_pages; i++) {
3404 struct page *page = pvec.pages[i];
3406 if (!PagePrivate(page))
3409 if (!wbc->range_cyclic && page->index > end) {
3414 spin_lock(&mapping->private_lock);
3415 if (!PagePrivate(page)) {
3416 spin_unlock(&mapping->private_lock);
3420 eb = (struct extent_buffer *)page->private;
3423 * Shouldn't happen and normally this would be a BUG_ON
3424 * but no sense in crashing the users box for something
3425 * we can survive anyway.
3428 spin_unlock(&mapping->private_lock);
3433 if (eb == prev_eb) {
3434 spin_unlock(&mapping->private_lock);
3438 ret = atomic_inc_not_zero(&eb->refs);
3439 spin_unlock(&mapping->private_lock);
3444 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3446 free_extent_buffer(eb);
3450 ret = write_one_eb(eb, fs_info, wbc, &epd);
3453 free_extent_buffer(eb);
3456 free_extent_buffer(eb);
3459 * the filesystem may choose to bump up nr_to_write.
3460 * We have to make sure to honor the new nr_to_write
3463 nr_to_write_done = wbc->nr_to_write <= 0;
3465 pagevec_release(&pvec);
3468 if (!scanned && !done) {
3470 * We hit the last page and there is more work to be done: wrap
3471 * back to the start of the file
3477 flush_write_bio(&epd);
3482 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3483 * @mapping: address space structure to write
3484 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3485 * @writepage: function called for each page
3486 * @data: data passed to writepage function
3488 * If a page is already under I/O, write_cache_pages() skips it, even
3489 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3490 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3491 * and msync() need to guarantee that all the data which was dirty at the time
3492 * the call was made get new I/O started against them. If wbc->sync_mode is
3493 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3494 * existing IO to complete.
3496 static int extent_write_cache_pages(struct extent_io_tree *tree,
3497 struct address_space *mapping,
3498 struct writeback_control *wbc,
3499 writepage_t writepage, void *data,
3500 void (*flush_fn)(void *))
3502 struct inode *inode = mapping->host;
3505 int nr_to_write_done = 0;
3506 struct pagevec pvec;
3509 pgoff_t end; /* Inclusive */
3514 * We have to hold onto the inode so that ordered extents can do their
3515 * work when the IO finishes. The alternative to this is failing to add
3516 * an ordered extent if the igrab() fails there and that is a huge pain
3517 * to deal with, so instead just hold onto the inode throughout the
3518 * writepages operation. If it fails here we are freeing up the inode
3519 * anyway and we'd rather not waste our time writing out stuff that is
3520 * going to be truncated anyway.
3525 pagevec_init(&pvec, 0);
3526 if (wbc->range_cyclic) {
3527 index = mapping->writeback_index; /* Start from prev offset */
3530 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3531 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3534 if (wbc->sync_mode == WB_SYNC_ALL)
3535 tag = PAGECACHE_TAG_TOWRITE;
3537 tag = PAGECACHE_TAG_DIRTY;
3539 if (wbc->sync_mode == WB_SYNC_ALL)
3540 tag_pages_for_writeback(mapping, index, end);
3541 while (!done && !nr_to_write_done && (index <= end) &&
3542 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3543 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3547 for (i = 0; i < nr_pages; i++) {
3548 struct page *page = pvec.pages[i];
3551 * At this point we hold neither mapping->tree_lock nor
3552 * lock on the page itself: the page may be truncated or
3553 * invalidated (changing page->mapping to NULL), or even
3554 * swizzled back from swapper_space to tmpfs file
3557 if (!trylock_page(page)) {
3562 if (unlikely(page->mapping != mapping)) {
3567 if (!wbc->range_cyclic && page->index > end) {
3573 if (wbc->sync_mode != WB_SYNC_NONE) {
3574 if (PageWriteback(page))
3576 wait_on_page_writeback(page);
3579 if (PageWriteback(page) ||
3580 !clear_page_dirty_for_io(page)) {
3585 ret = (*writepage)(page, wbc, data);
3587 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3595 * the filesystem may choose to bump up nr_to_write.
3596 * We have to make sure to honor the new nr_to_write
3599 nr_to_write_done = wbc->nr_to_write <= 0;
3601 pagevec_release(&pvec);
3604 if (!scanned && !done) {
3606 * We hit the last page and there is more work to be done: wrap
3607 * back to the start of the file
3613 btrfs_add_delayed_iput(inode);
3617 static void flush_epd_write_bio(struct extent_page_data *epd)
3626 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
3627 BUG_ON(ret < 0); /* -ENOMEM */
3632 static noinline void flush_write_bio(void *data)
3634 struct extent_page_data *epd = data;
3635 flush_epd_write_bio(epd);
3638 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3639 get_extent_t *get_extent,
3640 struct writeback_control *wbc)
3643 struct extent_page_data epd = {
3646 .get_extent = get_extent,
3648 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3652 ret = __extent_writepage(page, wbc, &epd);
3654 flush_epd_write_bio(&epd);
3658 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3659 u64 start, u64 end, get_extent_t *get_extent,
3663 struct address_space *mapping = inode->i_mapping;
3665 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3668 struct extent_page_data epd = {
3671 .get_extent = get_extent,
3673 .sync_io = mode == WB_SYNC_ALL,
3676 struct writeback_control wbc_writepages = {
3678 .nr_to_write = nr_pages * 2,
3679 .range_start = start,
3680 .range_end = end + 1,
3683 while (start <= end) {
3684 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3685 if (clear_page_dirty_for_io(page))
3686 ret = __extent_writepage(page, &wbc_writepages, &epd);
3688 if (tree->ops && tree->ops->writepage_end_io_hook)
3689 tree->ops->writepage_end_io_hook(page, start,
3690 start + PAGE_CACHE_SIZE - 1,
3694 page_cache_release(page);
3695 start += PAGE_CACHE_SIZE;
3698 flush_epd_write_bio(&epd);
3702 int extent_writepages(struct extent_io_tree *tree,
3703 struct address_space *mapping,
3704 get_extent_t *get_extent,
3705 struct writeback_control *wbc)
3708 struct extent_page_data epd = {
3711 .get_extent = get_extent,
3713 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3717 ret = extent_write_cache_pages(tree, mapping, wbc,
3718 __extent_writepage, &epd,
3720 flush_epd_write_bio(&epd);
3724 int extent_readpages(struct extent_io_tree *tree,
3725 struct address_space *mapping,
3726 struct list_head *pages, unsigned nr_pages,
3727 get_extent_t get_extent)
3729 struct bio *bio = NULL;
3731 unsigned long bio_flags = 0;
3732 struct page *pagepool[16];
3737 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3738 page = list_entry(pages->prev, struct page, lru);
3740 prefetchw(&page->flags);
3741 list_del(&page->lru);
3742 if (add_to_page_cache_lru(page, mapping,
3743 page->index, GFP_NOFS)) {
3744 page_cache_release(page);
3748 pagepool[nr++] = page;
3749 if (nr < ARRAY_SIZE(pagepool))
3751 for (i = 0; i < nr; i++) {
3752 __extent_read_full_page(tree, pagepool[i], get_extent,
3753 &bio, 0, &bio_flags, READ);
3754 page_cache_release(pagepool[i]);
3758 for (i = 0; i < nr; i++) {
3759 __extent_read_full_page(tree, pagepool[i], get_extent,
3760 &bio, 0, &bio_flags, READ);
3761 page_cache_release(pagepool[i]);
3764 BUG_ON(!list_empty(pages));
3766 return submit_one_bio(READ, bio, 0, bio_flags);
3771 * basic invalidatepage code, this waits on any locked or writeback
3772 * ranges corresponding to the page, and then deletes any extent state
3773 * records from the tree
3775 int extent_invalidatepage(struct extent_io_tree *tree,
3776 struct page *page, unsigned long offset)
3778 struct extent_state *cached_state = NULL;
3779 u64 start = page_offset(page);
3780 u64 end = start + PAGE_CACHE_SIZE - 1;
3781 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3783 start += ALIGN(offset, blocksize);
3787 lock_extent_bits(tree, start, end, 0, &cached_state);
3788 wait_on_page_writeback(page);
3789 clear_extent_bit(tree, start, end,
3790 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3791 EXTENT_DO_ACCOUNTING,
3792 1, 1, &cached_state, GFP_NOFS);
3797 * a helper for releasepage, this tests for areas of the page that
3798 * are locked or under IO and drops the related state bits if it is safe
3801 static int try_release_extent_state(struct extent_map_tree *map,
3802 struct extent_io_tree *tree,
3803 struct page *page, gfp_t mask)
3805 u64 start = page_offset(page);
3806 u64 end = start + PAGE_CACHE_SIZE - 1;
3809 if (test_range_bit(tree, start, end,
3810 EXTENT_IOBITS, 0, NULL))
3813 if ((mask & GFP_NOFS) == GFP_NOFS)
3816 * at this point we can safely clear everything except the
3817 * locked bit and the nodatasum bit
3819 ret = clear_extent_bit(tree, start, end,
3820 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3823 /* if clear_extent_bit failed for enomem reasons,
3824 * we can't allow the release to continue.
3835 * a helper for releasepage. As long as there are no locked extents
3836 * in the range corresponding to the page, both state records and extent
3837 * map records are removed
3839 int try_release_extent_mapping(struct extent_map_tree *map,
3840 struct extent_io_tree *tree, struct page *page,
3843 struct extent_map *em;
3844 u64 start = page_offset(page);
3845 u64 end = start + PAGE_CACHE_SIZE - 1;
3847 if ((mask & __GFP_WAIT) &&
3848 page->mapping->host->i_size > 16 * 1024 * 1024) {
3850 while (start <= end) {
3851 len = end - start + 1;
3852 write_lock(&map->lock);
3853 em = lookup_extent_mapping(map, start, len);
3855 write_unlock(&map->lock);
3858 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3859 em->start != start) {
3860 write_unlock(&map->lock);
3861 free_extent_map(em);
3864 if (!test_range_bit(tree, em->start,
3865 extent_map_end(em) - 1,
3866 EXTENT_LOCKED | EXTENT_WRITEBACK,
3868 remove_extent_mapping(map, em);
3869 /* once for the rb tree */
3870 free_extent_map(em);
3872 start = extent_map_end(em);
3873 write_unlock(&map->lock);
3876 free_extent_map(em);
3879 return try_release_extent_state(map, tree, page, mask);
3883 * helper function for fiemap, which doesn't want to see any holes.
3884 * This maps until we find something past 'last'
3886 static struct extent_map *get_extent_skip_holes(struct inode *inode,
3889 get_extent_t *get_extent)
3891 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3892 struct extent_map *em;
3899 len = last - offset;
3902 len = ALIGN(len, sectorsize);
3903 em = get_extent(inode, NULL, 0, offset, len, 0);
3904 if (IS_ERR_OR_NULL(em))
3907 /* if this isn't a hole return it */
3908 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3909 em->block_start != EXTENT_MAP_HOLE) {
3913 /* this is a hole, advance to the next extent */
3914 offset = extent_map_end(em);
3915 free_extent_map(em);
3922 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3923 __u64 start, __u64 len, get_extent_t *get_extent)
3927 u64 max = start + len;
3931 u64 last_for_get_extent = 0;
3933 u64 isize = i_size_read(inode);
3934 struct btrfs_key found_key;
3935 struct extent_map *em = NULL;
3936 struct extent_state *cached_state = NULL;
3937 struct btrfs_path *path;
3938 struct btrfs_file_extent_item *item;
3943 unsigned long emflags;
3948 path = btrfs_alloc_path();
3951 path->leave_spinning = 1;
3953 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3954 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3957 * lookup the last file extent. We're not using i_size here
3958 * because there might be preallocation past i_size
3960 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3961 path, btrfs_ino(inode), -1, 0);
3963 btrfs_free_path(path);
3968 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3969 struct btrfs_file_extent_item);
3970 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3971 found_type = btrfs_key_type(&found_key);
3973 /* No extents, but there might be delalloc bits */
3974 if (found_key.objectid != btrfs_ino(inode) ||
3975 found_type != BTRFS_EXTENT_DATA_KEY) {
3976 /* have to trust i_size as the end */
3978 last_for_get_extent = isize;
3981 * remember the start of the last extent. There are a
3982 * bunch of different factors that go into the length of the
3983 * extent, so its much less complex to remember where it started
3985 last = found_key.offset;
3986 last_for_get_extent = last + 1;
3988 btrfs_free_path(path);
3991 * we might have some extents allocated but more delalloc past those
3992 * extents. so, we trust isize unless the start of the last extent is
3997 last_for_get_extent = isize;
4000 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
4003 em = get_extent_skip_holes(inode, start, last_for_get_extent,
4013 u64 offset_in_extent;
4015 /* break if the extent we found is outside the range */
4016 if (em->start >= max || extent_map_end(em) < off)
4020 * get_extent may return an extent that starts before our
4021 * requested range. We have to make sure the ranges
4022 * we return to fiemap always move forward and don't
4023 * overlap, so adjust the offsets here
4025 em_start = max(em->start, off);
4028 * record the offset from the start of the extent
4029 * for adjusting the disk offset below
4031 offset_in_extent = em_start - em->start;
4032 em_end = extent_map_end(em);
4033 em_len = em_end - em_start;
4034 emflags = em->flags;
4039 * bump off for our next call to get_extent
4041 off = extent_map_end(em);
4045 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
4047 flags |= FIEMAP_EXTENT_LAST;
4048 } else if (em->block_start == EXTENT_MAP_INLINE) {
4049 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4050 FIEMAP_EXTENT_NOT_ALIGNED);
4051 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
4052 flags |= (FIEMAP_EXTENT_DELALLOC |
4053 FIEMAP_EXTENT_UNKNOWN);
4055 disko = em->block_start + offset_in_extent;
4057 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4058 flags |= FIEMAP_EXTENT_ENCODED;
4060 free_extent_map(em);
4062 if ((em_start >= last) || em_len == (u64)-1 ||
4063 (last == (u64)-1 && isize <= em_end)) {
4064 flags |= FIEMAP_EXTENT_LAST;
4068 /* now scan forward to see if this is really the last extent. */
4069 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4076 flags |= FIEMAP_EXTENT_LAST;
4079 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4085 free_extent_map(em);
4087 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
4088 &cached_state, GFP_NOFS);
4092 static void __free_extent_buffer(struct extent_buffer *eb)
4094 btrfs_leak_debug_del(&eb->leak_list);
4095 kmem_cache_free(extent_buffer_cache, eb);
4098 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4103 struct extent_buffer *eb = NULL;
4105 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
4112 rwlock_init(&eb->lock);
4113 atomic_set(&eb->write_locks, 0);
4114 atomic_set(&eb->read_locks, 0);
4115 atomic_set(&eb->blocking_readers, 0);
4116 atomic_set(&eb->blocking_writers, 0);
4117 atomic_set(&eb->spinning_readers, 0);
4118 atomic_set(&eb->spinning_writers, 0);
4119 eb->lock_nested = 0;
4120 init_waitqueue_head(&eb->write_lock_wq);
4121 init_waitqueue_head(&eb->read_lock_wq);
4123 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4125 spin_lock_init(&eb->refs_lock);
4126 atomic_set(&eb->refs, 1);
4127 atomic_set(&eb->io_pages, 0);
4130 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4132 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4133 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4134 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4139 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4143 struct extent_buffer *new;
4144 unsigned long num_pages = num_extent_pages(src->start, src->len);
4146 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4150 for (i = 0; i < num_pages; i++) {
4151 p = alloc_page(GFP_ATOMIC);
4153 attach_extent_buffer_page(new, p);
4154 WARN_ON(PageDirty(p));
4159 copy_extent_buffer(new, src, 0, 0, src->len);
4160 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4161 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4166 struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4168 struct extent_buffer *eb;
4169 unsigned long num_pages = num_extent_pages(0, len);
4172 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4176 for (i = 0; i < num_pages; i++) {
4177 eb->pages[i] = alloc_page(GFP_ATOMIC);
4181 set_extent_buffer_uptodate(eb);
4182 btrfs_set_header_nritems(eb, 0);
4183 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4188 __free_page(eb->pages[i - 1]);
4189 __free_extent_buffer(eb);
4193 static int extent_buffer_under_io(struct extent_buffer *eb)
4195 return (atomic_read(&eb->io_pages) ||
4196 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4197 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4201 * Helper for releasing extent buffer page.
4203 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4204 unsigned long start_idx)
4206 unsigned long index;
4207 unsigned long num_pages;
4209 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4211 BUG_ON(extent_buffer_under_io(eb));
4213 num_pages = num_extent_pages(eb->start, eb->len);
4214 index = start_idx + num_pages;
4215 if (start_idx >= index)
4220 page = extent_buffer_page(eb, index);
4221 if (page && mapped) {
4222 spin_lock(&page->mapping->private_lock);
4224 * We do this since we'll remove the pages after we've
4225 * removed the eb from the radix tree, so we could race
4226 * and have this page now attached to the new eb. So
4227 * only clear page_private if it's still connected to
4230 if (PagePrivate(page) &&
4231 page->private == (unsigned long)eb) {
4232 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4233 BUG_ON(PageDirty(page));
4234 BUG_ON(PageWriteback(page));
4236 * We need to make sure we haven't be attached
4239 ClearPagePrivate(page);
4240 set_page_private(page, 0);
4241 /* One for the page private */
4242 page_cache_release(page);
4244 spin_unlock(&page->mapping->private_lock);
4248 /* One for when we alloced the page */
4249 page_cache_release(page);
4251 } while (index != start_idx);
4255 * Helper for releasing the extent buffer.
4257 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4259 btrfs_release_extent_buffer_page(eb, 0);
4260 __free_extent_buffer(eb);
4263 static void check_buffer_tree_ref(struct extent_buffer *eb)
4266 /* the ref bit is tricky. We have to make sure it is set
4267 * if we have the buffer dirty. Otherwise the
4268 * code to free a buffer can end up dropping a dirty
4271 * Once the ref bit is set, it won't go away while the
4272 * buffer is dirty or in writeback, and it also won't
4273 * go away while we have the reference count on the
4276 * We can't just set the ref bit without bumping the
4277 * ref on the eb because free_extent_buffer might
4278 * see the ref bit and try to clear it. If this happens
4279 * free_extent_buffer might end up dropping our original
4280 * ref by mistake and freeing the page before we are able
4281 * to add one more ref.
4283 * So bump the ref count first, then set the bit. If someone
4284 * beat us to it, drop the ref we added.
4286 refs = atomic_read(&eb->refs);
4287 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4290 spin_lock(&eb->refs_lock);
4291 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4292 atomic_inc(&eb->refs);
4293 spin_unlock(&eb->refs_lock);
4296 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4298 unsigned long num_pages, i;
4300 check_buffer_tree_ref(eb);
4302 num_pages = num_extent_pages(eb->start, eb->len);
4303 for (i = 0; i < num_pages; i++) {
4304 struct page *p = extent_buffer_page(eb, i);
4305 mark_page_accessed(p);
4309 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
4310 u64 start, unsigned long len)
4312 unsigned long num_pages = num_extent_pages(start, len);
4314 unsigned long index = start >> PAGE_CACHE_SHIFT;
4315 struct extent_buffer *eb;
4316 struct extent_buffer *exists = NULL;
4318 struct address_space *mapping = tree->mapping;
4323 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4324 if (eb && atomic_inc_not_zero(&eb->refs)) {
4326 mark_extent_buffer_accessed(eb);
4331 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
4335 for (i = 0; i < num_pages; i++, index++) {
4336 p = find_or_create_page(mapping, index, GFP_NOFS);
4340 spin_lock(&mapping->private_lock);
4341 if (PagePrivate(p)) {
4343 * We could have already allocated an eb for this page
4344 * and attached one so lets see if we can get a ref on
4345 * the existing eb, and if we can we know it's good and
4346 * we can just return that one, else we know we can just
4347 * overwrite page->private.
4349 exists = (struct extent_buffer *)p->private;
4350 if (atomic_inc_not_zero(&exists->refs)) {
4351 spin_unlock(&mapping->private_lock);
4353 page_cache_release(p);
4354 mark_extent_buffer_accessed(exists);
4359 * Do this so attach doesn't complain and we need to
4360 * drop the ref the old guy had.
4362 ClearPagePrivate(p);
4363 WARN_ON(PageDirty(p));
4364 page_cache_release(p);
4366 attach_extent_buffer_page(eb, p);
4367 spin_unlock(&mapping->private_lock);
4368 WARN_ON(PageDirty(p));
4369 mark_page_accessed(p);
4371 if (!PageUptodate(p))
4375 * see below about how we avoid a nasty race with release page
4376 * and why we unlock later
4380 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4382 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4386 spin_lock(&tree->buffer_lock);
4387 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4388 if (ret == -EEXIST) {
4389 exists = radix_tree_lookup(&tree->buffer,
4390 start >> PAGE_CACHE_SHIFT);
4391 if (!atomic_inc_not_zero(&exists->refs)) {
4392 spin_unlock(&tree->buffer_lock);
4393 radix_tree_preload_end();
4397 spin_unlock(&tree->buffer_lock);
4398 radix_tree_preload_end();
4399 mark_extent_buffer_accessed(exists);
4402 /* add one reference for the tree */
4403 check_buffer_tree_ref(eb);
4404 spin_unlock(&tree->buffer_lock);
4405 radix_tree_preload_end();
4408 * there is a race where release page may have
4409 * tried to find this extent buffer in the radix
4410 * but failed. It will tell the VM it is safe to
4411 * reclaim the, and it will clear the page private bit.
4412 * We must make sure to set the page private bit properly
4413 * after the extent buffer is in the radix tree so
4414 * it doesn't get lost
4416 SetPageChecked(eb->pages[0]);
4417 for (i = 1; i < num_pages; i++) {
4418 p = extent_buffer_page(eb, i);
4419 ClearPageChecked(p);
4422 unlock_page(eb->pages[0]);
4426 for (i = 0; i < num_pages; i++) {
4428 unlock_page(eb->pages[i]);
4431 WARN_ON(!atomic_dec_and_test(&eb->refs));
4432 btrfs_release_extent_buffer(eb);
4436 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
4437 u64 start, unsigned long len)
4439 struct extent_buffer *eb;
4442 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4443 if (eb && atomic_inc_not_zero(&eb->refs)) {
4445 mark_extent_buffer_accessed(eb);
4453 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4455 struct extent_buffer *eb =
4456 container_of(head, struct extent_buffer, rcu_head);
4458 __free_extent_buffer(eb);
4461 /* Expects to have eb->eb_lock already held */
4462 static int release_extent_buffer(struct extent_buffer *eb)
4464 WARN_ON(atomic_read(&eb->refs) == 0);
4465 if (atomic_dec_and_test(&eb->refs)) {
4466 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4467 spin_unlock(&eb->refs_lock);
4469 struct extent_io_tree *tree = eb->tree;
4471 spin_unlock(&eb->refs_lock);
4473 spin_lock(&tree->buffer_lock);
4474 radix_tree_delete(&tree->buffer,
4475 eb->start >> PAGE_CACHE_SHIFT);
4476 spin_unlock(&tree->buffer_lock);
4479 /* Should be safe to release our pages at this point */
4480 btrfs_release_extent_buffer_page(eb, 0);
4481 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4484 spin_unlock(&eb->refs_lock);
4489 void free_extent_buffer(struct extent_buffer *eb)
4497 refs = atomic_read(&eb->refs);
4500 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4505 spin_lock(&eb->refs_lock);
4506 if (atomic_read(&eb->refs) == 2 &&
4507 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4508 atomic_dec(&eb->refs);
4510 if (atomic_read(&eb->refs) == 2 &&
4511 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
4512 !extent_buffer_under_io(eb) &&
4513 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4514 atomic_dec(&eb->refs);
4517 * I know this is terrible, but it's temporary until we stop tracking
4518 * the uptodate bits and such for the extent buffers.
4520 release_extent_buffer(eb);
4523 void free_extent_buffer_stale(struct extent_buffer *eb)
4528 spin_lock(&eb->refs_lock);
4529 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4531 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
4532 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4533 atomic_dec(&eb->refs);
4534 release_extent_buffer(eb);
4537 void clear_extent_buffer_dirty(struct extent_buffer *eb)
4540 unsigned long num_pages;
4543 num_pages = num_extent_pages(eb->start, eb->len);
4545 for (i = 0; i < num_pages; i++) {
4546 page = extent_buffer_page(eb, i);
4547 if (!PageDirty(page))
4551 WARN_ON(!PagePrivate(page));
4553 clear_page_dirty_for_io(page);
4554 spin_lock_irq(&page->mapping->tree_lock);
4555 if (!PageDirty(page)) {
4556 radix_tree_tag_clear(&page->mapping->page_tree,
4558 PAGECACHE_TAG_DIRTY);
4560 spin_unlock_irq(&page->mapping->tree_lock);
4561 ClearPageError(page);
4564 WARN_ON(atomic_read(&eb->refs) == 0);
4567 int set_extent_buffer_dirty(struct extent_buffer *eb)
4570 unsigned long num_pages;
4573 check_buffer_tree_ref(eb);
4575 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4577 num_pages = num_extent_pages(eb->start, eb->len);
4578 WARN_ON(atomic_read(&eb->refs) == 0);
4579 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4581 for (i = 0; i < num_pages; i++)
4582 set_page_dirty(extent_buffer_page(eb, i));
4586 int clear_extent_buffer_uptodate(struct extent_buffer *eb)
4590 unsigned long num_pages;
4592 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4593 num_pages = num_extent_pages(eb->start, eb->len);
4594 for (i = 0; i < num_pages; i++) {
4595 page = extent_buffer_page(eb, i);
4597 ClearPageUptodate(page);
4602 int set_extent_buffer_uptodate(struct extent_buffer *eb)
4606 unsigned long num_pages;
4608 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4609 num_pages = num_extent_pages(eb->start, eb->len);
4610 for (i = 0; i < num_pages; i++) {
4611 page = extent_buffer_page(eb, i);
4612 SetPageUptodate(page);
4617 int extent_buffer_uptodate(struct extent_buffer *eb)
4619 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4622 int read_extent_buffer_pages(struct extent_io_tree *tree,
4623 struct extent_buffer *eb, u64 start, int wait,
4624 get_extent_t *get_extent, int mirror_num)
4627 unsigned long start_i;
4631 int locked_pages = 0;
4632 int all_uptodate = 1;
4633 unsigned long num_pages;
4634 unsigned long num_reads = 0;
4635 struct bio *bio = NULL;
4636 unsigned long bio_flags = 0;
4638 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4642 WARN_ON(start < eb->start);
4643 start_i = (start >> PAGE_CACHE_SHIFT) -
4644 (eb->start >> PAGE_CACHE_SHIFT);
4649 num_pages = num_extent_pages(eb->start, eb->len);
4650 for (i = start_i; i < num_pages; i++) {
4651 page = extent_buffer_page(eb, i);
4652 if (wait == WAIT_NONE) {
4653 if (!trylock_page(page))
4659 if (!PageUptodate(page)) {
4666 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4670 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
4671 eb->read_mirror = 0;
4672 atomic_set(&eb->io_pages, num_reads);
4673 for (i = start_i; i < num_pages; i++) {
4674 page = extent_buffer_page(eb, i);
4675 if (!PageUptodate(page)) {
4676 ClearPageError(page);
4677 err = __extent_read_full_page(tree, page,
4679 mirror_num, &bio_flags,
4689 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
4695 if (ret || wait != WAIT_COMPLETE)
4698 for (i = start_i; i < num_pages; i++) {
4699 page = extent_buffer_page(eb, i);
4700 wait_on_page_locked(page);
4701 if (!PageUptodate(page))
4709 while (locked_pages > 0) {
4710 page = extent_buffer_page(eb, i);
4718 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4719 unsigned long start,
4726 char *dst = (char *)dstv;
4727 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4728 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4730 WARN_ON(start > eb->len);
4731 WARN_ON(start + len > eb->start + eb->len);
4733 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4736 page = extent_buffer_page(eb, i);
4738 cur = min(len, (PAGE_CACHE_SIZE - offset));
4739 kaddr = page_address(page);
4740 memcpy(dst, kaddr + offset, cur);
4749 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
4750 unsigned long min_len, char **map,
4751 unsigned long *map_start,
4752 unsigned long *map_len)
4754 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4757 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4758 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4759 unsigned long end_i = (start_offset + start + min_len - 1) >>
4766 offset = start_offset;
4770 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4773 if (start + min_len > eb->len) {
4774 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4775 "wanted %lu %lu\n", (unsigned long long)eb->start,
4776 eb->len, start, min_len);
4780 p = extent_buffer_page(eb, i);
4781 kaddr = page_address(p);
4782 *map = kaddr + offset;
4783 *map_len = PAGE_CACHE_SIZE - offset;
4787 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4788 unsigned long start,
4795 char *ptr = (char *)ptrv;
4796 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4797 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4800 WARN_ON(start > eb->len);
4801 WARN_ON(start + len > eb->start + eb->len);
4803 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4806 page = extent_buffer_page(eb, i);
4808 cur = min(len, (PAGE_CACHE_SIZE - offset));
4810 kaddr = page_address(page);
4811 ret = memcmp(ptr, kaddr + offset, cur);
4823 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4824 unsigned long start, unsigned long len)
4830 char *src = (char *)srcv;
4831 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4832 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4834 WARN_ON(start > eb->len);
4835 WARN_ON(start + len > eb->start + eb->len);
4837 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4840 page = extent_buffer_page(eb, i);
4841 WARN_ON(!PageUptodate(page));
4843 cur = min(len, PAGE_CACHE_SIZE - offset);
4844 kaddr = page_address(page);
4845 memcpy(kaddr + offset, src, cur);
4854 void memset_extent_buffer(struct extent_buffer *eb, char c,
4855 unsigned long start, unsigned long len)
4861 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4862 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4864 WARN_ON(start > eb->len);
4865 WARN_ON(start + len > eb->start + eb->len);
4867 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4870 page = extent_buffer_page(eb, i);
4871 WARN_ON(!PageUptodate(page));
4873 cur = min(len, PAGE_CACHE_SIZE - offset);
4874 kaddr = page_address(page);
4875 memset(kaddr + offset, c, cur);
4883 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4884 unsigned long dst_offset, unsigned long src_offset,
4887 u64 dst_len = dst->len;
4892 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4893 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4895 WARN_ON(src->len != dst_len);
4897 offset = (start_offset + dst_offset) &
4898 ((unsigned long)PAGE_CACHE_SIZE - 1);
4901 page = extent_buffer_page(dst, i);
4902 WARN_ON(!PageUptodate(page));
4904 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4906 kaddr = page_address(page);
4907 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4916 static void move_pages(struct page *dst_page, struct page *src_page,
4917 unsigned long dst_off, unsigned long src_off,
4920 char *dst_kaddr = page_address(dst_page);
4921 if (dst_page == src_page) {
4922 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4924 char *src_kaddr = page_address(src_page);
4925 char *p = dst_kaddr + dst_off + len;
4926 char *s = src_kaddr + src_off + len;
4933 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4935 unsigned long distance = (src > dst) ? src - dst : dst - src;
4936 return distance < len;
4939 static void copy_pages(struct page *dst_page, struct page *src_page,
4940 unsigned long dst_off, unsigned long src_off,
4943 char *dst_kaddr = page_address(dst_page);
4945 int must_memmove = 0;
4947 if (dst_page != src_page) {
4948 src_kaddr = page_address(src_page);
4950 src_kaddr = dst_kaddr;
4951 if (areas_overlap(src_off, dst_off, len))
4956 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4958 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
4961 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4962 unsigned long src_offset, unsigned long len)
4965 size_t dst_off_in_page;
4966 size_t src_off_in_page;
4967 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4968 unsigned long dst_i;
4969 unsigned long src_i;
4971 if (src_offset + len > dst->len) {
4972 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4973 "len %lu dst len %lu\n", src_offset, len, dst->len);
4976 if (dst_offset + len > dst->len) {
4977 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4978 "len %lu dst len %lu\n", dst_offset, len, dst->len);
4983 dst_off_in_page = (start_offset + dst_offset) &
4984 ((unsigned long)PAGE_CACHE_SIZE - 1);
4985 src_off_in_page = (start_offset + src_offset) &
4986 ((unsigned long)PAGE_CACHE_SIZE - 1);
4988 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4989 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4991 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4993 cur = min_t(unsigned long, cur,
4994 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4996 copy_pages(extent_buffer_page(dst, dst_i),
4997 extent_buffer_page(dst, src_i),
4998 dst_off_in_page, src_off_in_page, cur);
5006 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5007 unsigned long src_offset, unsigned long len)
5010 size_t dst_off_in_page;
5011 size_t src_off_in_page;
5012 unsigned long dst_end = dst_offset + len - 1;
5013 unsigned long src_end = src_offset + len - 1;
5014 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5015 unsigned long dst_i;
5016 unsigned long src_i;
5018 if (src_offset + len > dst->len) {
5019 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5020 "len %lu len %lu\n", src_offset, len, dst->len);
5023 if (dst_offset + len > dst->len) {
5024 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5025 "len %lu len %lu\n", dst_offset, len, dst->len);
5028 if (dst_offset < src_offset) {
5029 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5033 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5034 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5036 dst_off_in_page = (start_offset + dst_end) &
5037 ((unsigned long)PAGE_CACHE_SIZE - 1);
5038 src_off_in_page = (start_offset + src_end) &
5039 ((unsigned long)PAGE_CACHE_SIZE - 1);
5041 cur = min_t(unsigned long, len, src_off_in_page + 1);
5042 cur = min(cur, dst_off_in_page + 1);
5043 move_pages(extent_buffer_page(dst, dst_i),
5044 extent_buffer_page(dst, src_i),
5045 dst_off_in_page - cur + 1,
5046 src_off_in_page - cur + 1, cur);
5054 int try_release_extent_buffer(struct page *page)
5056 struct extent_buffer *eb;
5059 * We need to make sure noboody is attaching this page to an eb right
5062 spin_lock(&page->mapping->private_lock);
5063 if (!PagePrivate(page)) {
5064 spin_unlock(&page->mapping->private_lock);
5068 eb = (struct extent_buffer *)page->private;
5072 * This is a little awful but should be ok, we need to make sure that
5073 * the eb doesn't disappear out from under us while we're looking at
5076 spin_lock(&eb->refs_lock);
5077 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
5078 spin_unlock(&eb->refs_lock);
5079 spin_unlock(&page->mapping->private_lock);
5082 spin_unlock(&page->mapping->private_lock);
5085 * If tree ref isn't set then we know the ref on this eb is a real ref,
5086 * so just return, this page will likely be freed soon anyway.
5088 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5089 spin_unlock(&eb->refs_lock);
5093 return release_extent_buffer(eb);