1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/module.h>
8 #include <linux/spinlock.h>
9 #include <linux/blkdev.h>
10 #include <linux/swap.h>
11 #include <linux/writeback.h>
12 #include <linux/pagevec.h>
13 #include "extent_io.h"
14 #include "extent_map.h"
17 #include "btrfs_inode.h"
19 static struct kmem_cache *extent_state_cache;
20 static struct kmem_cache *extent_buffer_cache;
22 static LIST_HEAD(buffers);
23 static LIST_HEAD(states);
27 static DEFINE_SPINLOCK(leak_lock);
30 #define BUFFER_LRU_MAX 64
35 struct rb_node rb_node;
38 struct extent_page_data {
40 struct extent_io_tree *tree;
41 get_extent_t *get_extent;
43 /* tells writepage not to lock the state bits for this range
44 * it still does the unlocking
46 unsigned int extent_locked:1;
48 /* tells the submit_bio code to use a WRITE_SYNC */
49 unsigned int sync_io:1;
52 int __init extent_io_init(void)
54 extent_state_cache = kmem_cache_create("extent_state",
55 sizeof(struct extent_state), 0,
56 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
57 if (!extent_state_cache)
60 extent_buffer_cache = kmem_cache_create("extent_buffers",
61 sizeof(struct extent_buffer), 0,
62 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
63 if (!extent_buffer_cache)
64 goto free_state_cache;
68 kmem_cache_destroy(extent_state_cache);
72 void extent_io_exit(void)
74 struct extent_state *state;
75 struct extent_buffer *eb;
77 while (!list_empty(&states)) {
78 state = list_entry(states.next, struct extent_state, leak_list);
79 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
80 "state %lu in tree %p refs %d\n",
81 (unsigned long long)state->start,
82 (unsigned long long)state->end,
83 state->state, state->tree, atomic_read(&state->refs));
84 list_del(&state->leak_list);
85 kmem_cache_free(extent_state_cache, state);
89 while (!list_empty(&buffers)) {
90 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
91 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
92 "refs %d\n", (unsigned long long)eb->start,
93 eb->len, atomic_read(&eb->refs));
94 list_del(&eb->leak_list);
95 kmem_cache_free(extent_buffer_cache, eb);
97 if (extent_state_cache)
98 kmem_cache_destroy(extent_state_cache);
99 if (extent_buffer_cache)
100 kmem_cache_destroy(extent_buffer_cache);
103 void extent_io_tree_init(struct extent_io_tree *tree,
104 struct address_space *mapping)
106 tree->state = RB_ROOT;
107 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
109 tree->dirty_bytes = 0;
110 spin_lock_init(&tree->lock);
111 spin_lock_init(&tree->buffer_lock);
112 tree->mapping = mapping;
115 static struct extent_state *alloc_extent_state(gfp_t mask)
117 struct extent_state *state;
122 state = kmem_cache_alloc(extent_state_cache, mask);
129 spin_lock_irqsave(&leak_lock, flags);
130 list_add(&state->leak_list, &states);
131 spin_unlock_irqrestore(&leak_lock, flags);
133 atomic_set(&state->refs, 1);
134 init_waitqueue_head(&state->wq);
138 void free_extent_state(struct extent_state *state)
142 if (atomic_dec_and_test(&state->refs)) {
146 WARN_ON(state->tree);
148 spin_lock_irqsave(&leak_lock, flags);
149 list_del(&state->leak_list);
150 spin_unlock_irqrestore(&leak_lock, flags);
152 kmem_cache_free(extent_state_cache, state);
156 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
157 struct rb_node *node)
159 struct rb_node **p = &root->rb_node;
160 struct rb_node *parent = NULL;
161 struct tree_entry *entry;
165 entry = rb_entry(parent, struct tree_entry, rb_node);
167 if (offset < entry->start)
169 else if (offset > entry->end)
175 entry = rb_entry(node, struct tree_entry, rb_node);
176 rb_link_node(node, parent, p);
177 rb_insert_color(node, root);
181 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
182 struct rb_node **prev_ret,
183 struct rb_node **next_ret)
185 struct rb_root *root = &tree->state;
186 struct rb_node *n = root->rb_node;
187 struct rb_node *prev = NULL;
188 struct rb_node *orig_prev = NULL;
189 struct tree_entry *entry;
190 struct tree_entry *prev_entry = NULL;
193 entry = rb_entry(n, struct tree_entry, rb_node);
197 if (offset < entry->start)
199 else if (offset > entry->end)
207 while (prev && offset > prev_entry->end) {
208 prev = rb_next(prev);
209 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
216 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
217 while (prev && offset < prev_entry->start) {
218 prev = rb_prev(prev);
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
226 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
229 struct rb_node *prev = NULL;
232 ret = __etree_search(tree, offset, &prev, NULL);
238 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
239 struct extent_state *other)
241 if (tree->ops && tree->ops->merge_extent_hook)
242 tree->ops->merge_extent_hook(tree->mapping->host, new,
247 * utility function to look for merge candidates inside a given range.
248 * Any extents with matching state are merged together into a single
249 * extent in the tree. Extents with EXTENT_IO in their state field
250 * are not merged because the end_io handlers need to be able to do
251 * operations on them without sleeping (or doing allocations/splits).
253 * This should be called with the tree lock held.
255 static int merge_state(struct extent_io_tree *tree,
256 struct extent_state *state)
258 struct extent_state *other;
259 struct rb_node *other_node;
261 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
264 other_node = rb_prev(&state->rb_node);
266 other = rb_entry(other_node, struct extent_state, rb_node);
267 if (other->end == state->start - 1 &&
268 other->state == state->state) {
269 merge_cb(tree, state, other);
270 state->start = other->start;
272 rb_erase(&other->rb_node, &tree->state);
273 free_extent_state(other);
276 other_node = rb_next(&state->rb_node);
278 other = rb_entry(other_node, struct extent_state, rb_node);
279 if (other->start == state->end + 1 &&
280 other->state == state->state) {
281 merge_cb(tree, state, other);
282 other->start = state->start;
284 rb_erase(&state->rb_node, &tree->state);
285 free_extent_state(state);
293 static int set_state_cb(struct extent_io_tree *tree,
294 struct extent_state *state, int *bits)
296 if (tree->ops && tree->ops->set_bit_hook) {
297 return tree->ops->set_bit_hook(tree->mapping->host,
304 static void clear_state_cb(struct extent_io_tree *tree,
305 struct extent_state *state, int *bits)
307 if (tree->ops && tree->ops->clear_bit_hook)
308 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
312 * insert an extent_state struct into the tree. 'bits' are set on the
313 * struct before it is inserted.
315 * This may return -EEXIST if the extent is already there, in which case the
316 * state struct is freed.
318 * The tree lock is not taken internally. This is a utility function and
319 * probably isn't what you want to call (see set/clear_extent_bit).
321 static int insert_state(struct extent_io_tree *tree,
322 struct extent_state *state, u64 start, u64 end,
325 struct rb_node *node;
326 int bits_to_set = *bits & ~EXTENT_CTLBITS;
330 printk(KERN_ERR "btrfs end < start %llu %llu\n",
331 (unsigned long long)end,
332 (unsigned long long)start);
335 state->start = start;
337 ret = set_state_cb(tree, state, bits);
341 if (bits_to_set & EXTENT_DIRTY)
342 tree->dirty_bytes += end - start + 1;
343 state->state |= bits_to_set;
344 node = tree_insert(&tree->state, end, &state->rb_node);
346 struct extent_state *found;
347 found = rb_entry(node, struct extent_state, rb_node);
348 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
349 "%llu %llu\n", (unsigned long long)found->start,
350 (unsigned long long)found->end,
351 (unsigned long long)start, (unsigned long long)end);
352 free_extent_state(state);
356 merge_state(tree, state);
360 static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
363 if (tree->ops && tree->ops->split_extent_hook)
364 return tree->ops->split_extent_hook(tree->mapping->host,
370 * split a given extent state struct in two, inserting the preallocated
371 * struct 'prealloc' as the newly created second half. 'split' indicates an
372 * offset inside 'orig' where it should be split.
375 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
376 * are two extent state structs in the tree:
377 * prealloc: [orig->start, split - 1]
378 * orig: [ split, orig->end ]
380 * The tree locks are not taken by this function. They need to be held
383 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
384 struct extent_state *prealloc, u64 split)
386 struct rb_node *node;
388 split_cb(tree, orig, split);
390 prealloc->start = orig->start;
391 prealloc->end = split - 1;
392 prealloc->state = orig->state;
395 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
397 free_extent_state(prealloc);
400 prealloc->tree = tree;
405 * utility function to clear some bits in an extent state struct.
406 * it will optionally wake up any one waiting on this state (wake == 1), or
407 * forcibly remove the state from the tree (delete == 1).
409 * If no bits are set on the state struct after clearing things, the
410 * struct is freed and removed from the tree
412 static int clear_state_bit(struct extent_io_tree *tree,
413 struct extent_state *state,
416 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
417 int ret = state->state & bits_to_clear;
419 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
420 u64 range = state->end - state->start + 1;
421 WARN_ON(range > tree->dirty_bytes);
422 tree->dirty_bytes -= range;
424 clear_state_cb(tree, state, bits);
425 state->state &= ~bits_to_clear;
428 if (state->state == 0) {
430 rb_erase(&state->rb_node, &tree->state);
432 free_extent_state(state);
437 merge_state(tree, state);
442 static struct extent_state *
443 alloc_extent_state_atomic(struct extent_state *prealloc)
446 prealloc = alloc_extent_state(GFP_ATOMIC);
452 * clear some bits on a range in the tree. This may require splitting
453 * or inserting elements in the tree, so the gfp mask is used to
454 * indicate which allocations or sleeping are allowed.
456 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
457 * the given range from the tree regardless of state (ie for truncate).
459 * the range [start, end] is inclusive.
461 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
462 * bits were already set, or zero if none of the bits were already set.
464 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
465 int bits, int wake, int delete,
466 struct extent_state **cached_state,
469 struct extent_state *state;
470 struct extent_state *cached;
471 struct extent_state *prealloc = NULL;
472 struct rb_node *next_node;
473 struct rb_node *node;
480 bits |= ~EXTENT_CTLBITS;
481 bits |= EXTENT_FIRST_DELALLOC;
483 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
486 if (!prealloc && (mask & __GFP_WAIT)) {
487 prealloc = alloc_extent_state(mask);
491 spin_lock(&tree->lock);
493 cached = *cached_state;
496 *cached_state = NULL;
500 if (cached && cached->tree && cached->start == start) {
502 atomic_dec(&cached->refs);
507 free_extent_state(cached);
510 * this search will find the extents that end after
513 node = tree_search(tree, start);
516 state = rb_entry(node, struct extent_state, rb_node);
518 if (state->start > end)
520 WARN_ON(state->end < start);
521 last_end = state->end;
524 * | ---- desired range ---- |
526 * | ------------- state -------------- |
528 * We need to split the extent we found, and may flip
529 * bits on second half.
531 * If the extent we found extends past our range, we
532 * just split and search again. It'll get split again
533 * the next time though.
535 * If the extent we found is inside our range, we clear
536 * the desired bit on it.
539 if (state->start < start) {
540 prealloc = alloc_extent_state_atomic(prealloc);
542 err = split_state(tree, state, prealloc, start);
543 BUG_ON(err == -EEXIST);
547 if (state->end <= end) {
548 set |= clear_state_bit(tree, state, &bits, wake);
549 if (last_end == (u64)-1)
551 start = last_end + 1;
556 * | ---- desired range ---- |
558 * We need to split the extent, and clear the bit
561 if (state->start <= end && state->end > end) {
562 prealloc = alloc_extent_state_atomic(prealloc);
564 err = split_state(tree, state, prealloc, end + 1);
565 BUG_ON(err == -EEXIST);
569 set |= clear_state_bit(tree, prealloc, &bits, wake);
575 if (state->end < end && prealloc && !need_resched())
576 next_node = rb_next(&state->rb_node);
580 set |= clear_state_bit(tree, state, &bits, wake);
581 if (last_end == (u64)-1)
583 start = last_end + 1;
584 if (start <= end && next_node) {
585 state = rb_entry(next_node, struct extent_state,
587 if (state->start == start)
593 spin_unlock(&tree->lock);
595 free_extent_state(prealloc);
602 spin_unlock(&tree->lock);
603 if (mask & __GFP_WAIT)
608 static int wait_on_state(struct extent_io_tree *tree,
609 struct extent_state *state)
610 __releases(tree->lock)
611 __acquires(tree->lock)
614 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
615 spin_unlock(&tree->lock);
617 spin_lock(&tree->lock);
618 finish_wait(&state->wq, &wait);
623 * waits for one or more bits to clear on a range in the state tree.
624 * The range [start, end] is inclusive.
625 * The tree lock is taken by this function
627 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
629 struct extent_state *state;
630 struct rb_node *node;
632 spin_lock(&tree->lock);
636 * this search will find all the extents that end after
639 node = tree_search(tree, start);
643 state = rb_entry(node, struct extent_state, rb_node);
645 if (state->start > end)
648 if (state->state & bits) {
649 start = state->start;
650 atomic_inc(&state->refs);
651 wait_on_state(tree, state);
652 free_extent_state(state);
655 start = state->end + 1;
660 if (need_resched()) {
661 spin_unlock(&tree->lock);
663 spin_lock(&tree->lock);
667 spin_unlock(&tree->lock);
671 static int set_state_bits(struct extent_io_tree *tree,
672 struct extent_state *state,
676 int bits_to_set = *bits & ~EXTENT_CTLBITS;
678 ret = set_state_cb(tree, state, bits);
681 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
682 u64 range = state->end - state->start + 1;
683 tree->dirty_bytes += range;
685 state->state |= bits_to_set;
690 static void cache_state(struct extent_state *state,
691 struct extent_state **cached_ptr)
693 if (cached_ptr && !(*cached_ptr)) {
694 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
696 atomic_inc(&state->refs);
701 static void uncache_state(struct extent_state **cached_ptr)
703 if (cached_ptr && (*cached_ptr)) {
704 struct extent_state *state = *cached_ptr;
706 free_extent_state(state);
711 * set some bits on a range in the tree. This may require allocations or
712 * sleeping, so the gfp mask is used to indicate what is allowed.
714 * If any of the exclusive bits are set, this will fail with -EEXIST if some
715 * part of the range already has the desired bits set. The start of the
716 * existing range is returned in failed_start in this case.
718 * [start, end] is inclusive This takes the tree lock.
721 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
722 int bits, int exclusive_bits, u64 *failed_start,
723 struct extent_state **cached_state, gfp_t mask)
725 struct extent_state *state;
726 struct extent_state *prealloc = NULL;
727 struct rb_node *node;
732 bits |= EXTENT_FIRST_DELALLOC;
734 if (!prealloc && (mask & __GFP_WAIT)) {
735 prealloc = alloc_extent_state(mask);
739 spin_lock(&tree->lock);
740 if (cached_state && *cached_state) {
741 state = *cached_state;
742 if (state->start == start && state->tree) {
743 node = &state->rb_node;
748 * this search will find all the extents that end after
751 node = tree_search(tree, start);
753 prealloc = alloc_extent_state_atomic(prealloc);
755 err = insert_state(tree, prealloc, start, end, &bits);
757 BUG_ON(err == -EEXIST);
760 state = rb_entry(node, struct extent_state, rb_node);
762 last_start = state->start;
763 last_end = state->end;
766 * | ---- desired range ---- |
769 * Just lock what we found and keep going
771 if (state->start == start && state->end <= end) {
772 struct rb_node *next_node;
773 if (state->state & exclusive_bits) {
774 *failed_start = state->start;
779 err = set_state_bits(tree, state, &bits);
783 next_node = rb_next(node);
784 cache_state(state, cached_state);
785 merge_state(tree, state);
786 if (last_end == (u64)-1)
789 start = last_end + 1;
790 if (next_node && start < end && prealloc && !need_resched()) {
791 state = rb_entry(next_node, struct extent_state,
793 if (state->start == start)
800 * | ---- desired range ---- |
803 * | ------------- state -------------- |
805 * We need to split the extent we found, and may flip bits on
808 * If the extent we found extends past our
809 * range, we just split and search again. It'll get split
810 * again the next time though.
812 * If the extent we found is inside our range, we set the
815 if (state->start < start) {
816 if (state->state & exclusive_bits) {
817 *failed_start = start;
822 prealloc = alloc_extent_state_atomic(prealloc);
824 err = split_state(tree, state, prealloc, start);
825 BUG_ON(err == -EEXIST);
829 if (state->end <= end) {
830 err = set_state_bits(tree, state, &bits);
833 cache_state(state, cached_state);
834 merge_state(tree, state);
835 if (last_end == (u64)-1)
837 start = last_end + 1;
842 * | ---- desired range ---- |
843 * | state | or | state |
845 * There's a hole, we need to insert something in it and
846 * ignore the extent we found.
848 if (state->start > start) {
850 if (end < last_start)
853 this_end = last_start - 1;
855 prealloc = alloc_extent_state_atomic(prealloc);
859 * Avoid to free 'prealloc' if it can be merged with
862 atomic_inc(&prealloc->refs);
863 err = insert_state(tree, prealloc, start, this_end,
865 BUG_ON(err == -EEXIST);
867 free_extent_state(prealloc);
871 cache_state(prealloc, cached_state);
872 free_extent_state(prealloc);
874 start = this_end + 1;
878 * | ---- desired range ---- |
880 * We need to split the extent, and set the bit
883 if (state->start <= end && state->end > end) {
884 if (state->state & exclusive_bits) {
885 *failed_start = start;
890 prealloc = alloc_extent_state_atomic(prealloc);
892 err = split_state(tree, state, prealloc, end + 1);
893 BUG_ON(err == -EEXIST);
895 err = set_state_bits(tree, prealloc, &bits);
900 cache_state(prealloc, cached_state);
901 merge_state(tree, prealloc);
909 spin_unlock(&tree->lock);
911 free_extent_state(prealloc);
918 spin_unlock(&tree->lock);
919 if (mask & __GFP_WAIT)
924 /* wrappers around set/clear extent bit */
925 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
928 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
932 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
933 int bits, gfp_t mask)
935 return set_extent_bit(tree, start, end, bits, 0, NULL,
939 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
940 int bits, gfp_t mask)
942 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
945 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
946 struct extent_state **cached_state, gfp_t mask)
948 return set_extent_bit(tree, start, end,
949 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
950 0, NULL, cached_state, mask);
953 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
956 return clear_extent_bit(tree, start, end,
957 EXTENT_DIRTY | EXTENT_DELALLOC |
958 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
961 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
964 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
968 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
969 struct extent_state **cached_state, gfp_t mask)
971 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
972 NULL, cached_state, mask);
975 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
976 u64 end, struct extent_state **cached_state,
979 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
984 * either insert or lock state struct between start and end use mask to tell
985 * us if waiting is desired.
987 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
988 int bits, struct extent_state **cached_state, gfp_t mask)
993 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
994 EXTENT_LOCKED, &failed_start,
996 if (err == -EEXIST && (mask & __GFP_WAIT)) {
997 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
998 start = failed_start;
1002 WARN_ON(start > end);
1007 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1009 return lock_extent_bits(tree, start, end, 0, NULL, mask);
1012 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1018 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1019 &failed_start, NULL, mask);
1020 if (err == -EEXIST) {
1021 if (failed_start > start)
1022 clear_extent_bit(tree, start, failed_start - 1,
1023 EXTENT_LOCKED, 1, 0, NULL, mask);
1029 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1030 struct extent_state **cached, gfp_t mask)
1032 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1036 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1038 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1043 * helper function to set both pages and extents in the tree writeback
1045 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1047 unsigned long index = start >> PAGE_CACHE_SHIFT;
1048 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1051 while (index <= end_index) {
1052 page = find_get_page(tree->mapping, index);
1054 set_page_writeback(page);
1055 page_cache_release(page);
1062 * find the first offset in the io tree with 'bits' set. zero is
1063 * returned if we find something, and *start_ret and *end_ret are
1064 * set to reflect the state struct that was found.
1066 * If nothing was found, 1 is returned, < 0 on error
1068 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1069 u64 *start_ret, u64 *end_ret, int bits)
1071 struct rb_node *node;
1072 struct extent_state *state;
1075 spin_lock(&tree->lock);
1077 * this search will find all the extents that end after
1080 node = tree_search(tree, start);
1085 state = rb_entry(node, struct extent_state, rb_node);
1086 if (state->end >= start && (state->state & bits)) {
1087 *start_ret = state->start;
1088 *end_ret = state->end;
1092 node = rb_next(node);
1097 spin_unlock(&tree->lock);
1101 /* find the first state struct with 'bits' set after 'start', and
1102 * return it. tree->lock must be held. NULL will returned if
1103 * nothing was found after 'start'
1105 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1106 u64 start, int bits)
1108 struct rb_node *node;
1109 struct extent_state *state;
1112 * this search will find all the extents that end after
1115 node = tree_search(tree, start);
1120 state = rb_entry(node, struct extent_state, rb_node);
1121 if (state->end >= start && (state->state & bits))
1124 node = rb_next(node);
1133 * find a contiguous range of bytes in the file marked as delalloc, not
1134 * more than 'max_bytes'. start and end are used to return the range,
1136 * 1 is returned if we find something, 0 if nothing was in the tree
1138 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1139 u64 *start, u64 *end, u64 max_bytes,
1140 struct extent_state **cached_state)
1142 struct rb_node *node;
1143 struct extent_state *state;
1144 u64 cur_start = *start;
1146 u64 total_bytes = 0;
1148 spin_lock(&tree->lock);
1151 * this search will find all the extents that end after
1154 node = tree_search(tree, cur_start);
1162 state = rb_entry(node, struct extent_state, rb_node);
1163 if (found && (state->start != cur_start ||
1164 (state->state & EXTENT_BOUNDARY))) {
1167 if (!(state->state & EXTENT_DELALLOC)) {
1173 *start = state->start;
1174 *cached_state = state;
1175 atomic_inc(&state->refs);
1179 cur_start = state->end + 1;
1180 node = rb_next(node);
1183 total_bytes += state->end - state->start + 1;
1184 if (total_bytes >= max_bytes)
1188 spin_unlock(&tree->lock);
1192 static noinline int __unlock_for_delalloc(struct inode *inode,
1193 struct page *locked_page,
1197 struct page *pages[16];
1198 unsigned long index = start >> PAGE_CACHE_SHIFT;
1199 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1200 unsigned long nr_pages = end_index - index + 1;
1203 if (index == locked_page->index && end_index == index)
1206 while (nr_pages > 0) {
1207 ret = find_get_pages_contig(inode->i_mapping, index,
1208 min_t(unsigned long, nr_pages,
1209 ARRAY_SIZE(pages)), pages);
1210 for (i = 0; i < ret; i++) {
1211 if (pages[i] != locked_page)
1212 unlock_page(pages[i]);
1213 page_cache_release(pages[i]);
1222 static noinline int lock_delalloc_pages(struct inode *inode,
1223 struct page *locked_page,
1227 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1228 unsigned long start_index = index;
1229 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1230 unsigned long pages_locked = 0;
1231 struct page *pages[16];
1232 unsigned long nrpages;
1236 /* the caller is responsible for locking the start index */
1237 if (index == locked_page->index && index == end_index)
1240 /* skip the page at the start index */
1241 nrpages = end_index - index + 1;
1242 while (nrpages > 0) {
1243 ret = find_get_pages_contig(inode->i_mapping, index,
1244 min_t(unsigned long,
1245 nrpages, ARRAY_SIZE(pages)), pages);
1250 /* now we have an array of pages, lock them all */
1251 for (i = 0; i < ret; i++) {
1253 * the caller is taking responsibility for
1256 if (pages[i] != locked_page) {
1257 lock_page(pages[i]);
1258 if (!PageDirty(pages[i]) ||
1259 pages[i]->mapping != inode->i_mapping) {
1261 unlock_page(pages[i]);
1262 page_cache_release(pages[i]);
1266 page_cache_release(pages[i]);
1275 if (ret && pages_locked) {
1276 __unlock_for_delalloc(inode, locked_page,
1278 ((u64)(start_index + pages_locked - 1)) <<
1285 * find a contiguous range of bytes in the file marked as delalloc, not
1286 * more than 'max_bytes'. start and end are used to return the range,
1288 * 1 is returned if we find something, 0 if nothing was in the tree
1290 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1291 struct extent_io_tree *tree,
1292 struct page *locked_page,
1293 u64 *start, u64 *end,
1299 struct extent_state *cached_state = NULL;
1304 /* step one, find a bunch of delalloc bytes starting at start */
1305 delalloc_start = *start;
1307 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1308 max_bytes, &cached_state);
1309 if (!found || delalloc_end <= *start) {
1310 *start = delalloc_start;
1311 *end = delalloc_end;
1312 free_extent_state(cached_state);
1317 * start comes from the offset of locked_page. We have to lock
1318 * pages in order, so we can't process delalloc bytes before
1321 if (delalloc_start < *start)
1322 delalloc_start = *start;
1325 * make sure to limit the number of pages we try to lock down
1328 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1329 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1331 /* step two, lock all the pages after the page that has start */
1332 ret = lock_delalloc_pages(inode, locked_page,
1333 delalloc_start, delalloc_end);
1334 if (ret == -EAGAIN) {
1335 /* some of the pages are gone, lets avoid looping by
1336 * shortening the size of the delalloc range we're searching
1338 free_extent_state(cached_state);
1340 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1341 max_bytes = PAGE_CACHE_SIZE - offset;
1351 /* step three, lock the state bits for the whole range */
1352 lock_extent_bits(tree, delalloc_start, delalloc_end,
1353 0, &cached_state, GFP_NOFS);
1355 /* then test to make sure it is all still delalloc */
1356 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1357 EXTENT_DELALLOC, 1, cached_state);
1359 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1360 &cached_state, GFP_NOFS);
1361 __unlock_for_delalloc(inode, locked_page,
1362 delalloc_start, delalloc_end);
1366 free_extent_state(cached_state);
1367 *start = delalloc_start;
1368 *end = delalloc_end;
1373 int extent_clear_unlock_delalloc(struct inode *inode,
1374 struct extent_io_tree *tree,
1375 u64 start, u64 end, struct page *locked_page,
1379 struct page *pages[16];
1380 unsigned long index = start >> PAGE_CACHE_SHIFT;
1381 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1382 unsigned long nr_pages = end_index - index + 1;
1386 if (op & EXTENT_CLEAR_UNLOCK)
1387 clear_bits |= EXTENT_LOCKED;
1388 if (op & EXTENT_CLEAR_DIRTY)
1389 clear_bits |= EXTENT_DIRTY;
1391 if (op & EXTENT_CLEAR_DELALLOC)
1392 clear_bits |= EXTENT_DELALLOC;
1394 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1395 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1396 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1397 EXTENT_SET_PRIVATE2)))
1400 while (nr_pages > 0) {
1401 ret = find_get_pages_contig(inode->i_mapping, index,
1402 min_t(unsigned long,
1403 nr_pages, ARRAY_SIZE(pages)), pages);
1404 for (i = 0; i < ret; i++) {
1406 if (op & EXTENT_SET_PRIVATE2)
1407 SetPagePrivate2(pages[i]);
1409 if (pages[i] == locked_page) {
1410 page_cache_release(pages[i]);
1413 if (op & EXTENT_CLEAR_DIRTY)
1414 clear_page_dirty_for_io(pages[i]);
1415 if (op & EXTENT_SET_WRITEBACK)
1416 set_page_writeback(pages[i]);
1417 if (op & EXTENT_END_WRITEBACK)
1418 end_page_writeback(pages[i]);
1419 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1420 unlock_page(pages[i]);
1421 page_cache_release(pages[i]);
1431 * count the number of bytes in the tree that have a given bit(s)
1432 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1433 * cached. The total number found is returned.
1435 u64 count_range_bits(struct extent_io_tree *tree,
1436 u64 *start, u64 search_end, u64 max_bytes,
1437 unsigned long bits, int contig)
1439 struct rb_node *node;
1440 struct extent_state *state;
1441 u64 cur_start = *start;
1442 u64 total_bytes = 0;
1446 if (search_end <= cur_start) {
1451 spin_lock(&tree->lock);
1452 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1453 total_bytes = tree->dirty_bytes;
1457 * this search will find all the extents that end after
1460 node = tree_search(tree, cur_start);
1465 state = rb_entry(node, struct extent_state, rb_node);
1466 if (state->start > search_end)
1468 if (contig && found && state->start > last + 1)
1470 if (state->end >= cur_start && (state->state & bits) == bits) {
1471 total_bytes += min(search_end, state->end) + 1 -
1472 max(cur_start, state->start);
1473 if (total_bytes >= max_bytes)
1476 *start = state->start;
1480 } else if (contig && found) {
1483 node = rb_next(node);
1488 spin_unlock(&tree->lock);
1493 * set the private field for a given byte offset in the tree. If there isn't
1494 * an extent_state there already, this does nothing.
1496 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1498 struct rb_node *node;
1499 struct extent_state *state;
1502 spin_lock(&tree->lock);
1504 * this search will find all the extents that end after
1507 node = tree_search(tree, start);
1512 state = rb_entry(node, struct extent_state, rb_node);
1513 if (state->start != start) {
1517 state->private = private;
1519 spin_unlock(&tree->lock);
1523 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1525 struct rb_node *node;
1526 struct extent_state *state;
1529 spin_lock(&tree->lock);
1531 * this search will find all the extents that end after
1534 node = tree_search(tree, start);
1539 state = rb_entry(node, struct extent_state, rb_node);
1540 if (state->start != start) {
1544 *private = state->private;
1546 spin_unlock(&tree->lock);
1551 * searches a range in the state tree for a given mask.
1552 * If 'filled' == 1, this returns 1 only if every extent in the tree
1553 * has the bits set. Otherwise, 1 is returned if any bit in the
1554 * range is found set.
1556 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1557 int bits, int filled, struct extent_state *cached)
1559 struct extent_state *state = NULL;
1560 struct rb_node *node;
1563 spin_lock(&tree->lock);
1564 if (cached && cached->tree && cached->start == start)
1565 node = &cached->rb_node;
1567 node = tree_search(tree, start);
1568 while (node && start <= end) {
1569 state = rb_entry(node, struct extent_state, rb_node);
1571 if (filled && state->start > start) {
1576 if (state->start > end)
1579 if (state->state & bits) {
1583 } else if (filled) {
1588 if (state->end == (u64)-1)
1591 start = state->end + 1;
1594 node = rb_next(node);
1601 spin_unlock(&tree->lock);
1606 * helper function to set a given page up to date if all the
1607 * extents in the tree for that page are up to date
1609 static int check_page_uptodate(struct extent_io_tree *tree,
1612 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1613 u64 end = start + PAGE_CACHE_SIZE - 1;
1614 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1615 SetPageUptodate(page);
1620 * helper function to unlock a page if all the extents in the tree
1621 * for that page are unlocked
1623 static int check_page_locked(struct extent_io_tree *tree,
1626 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1627 u64 end = start + PAGE_CACHE_SIZE - 1;
1628 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1634 * helper function to end page writeback if all the extents
1635 * in the tree for that page are done with writeback
1637 static int check_page_writeback(struct extent_io_tree *tree,
1640 end_page_writeback(page);
1644 /* lots and lots of room for performance fixes in the end_bio funcs */
1647 * after a writepage IO is done, we need to:
1648 * clear the uptodate bits on error
1649 * clear the writeback bits in the extent tree for this IO
1650 * end_page_writeback if the page has no more pending IO
1652 * Scheduling is not allowed, so the extent state tree is expected
1653 * to have one and only one object corresponding to this IO.
1655 static void end_bio_extent_writepage(struct bio *bio, int err)
1657 int uptodate = err == 0;
1658 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1659 struct extent_io_tree *tree;
1666 struct page *page = bvec->bv_page;
1667 tree = &BTRFS_I(page->mapping->host)->io_tree;
1669 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1671 end = start + bvec->bv_len - 1;
1673 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1678 if (--bvec >= bio->bi_io_vec)
1679 prefetchw(&bvec->bv_page->flags);
1680 if (tree->ops && tree->ops->writepage_end_io_hook) {
1681 ret = tree->ops->writepage_end_io_hook(page, start,
1682 end, NULL, uptodate);
1687 if (!uptodate && tree->ops &&
1688 tree->ops->writepage_io_failed_hook) {
1689 ret = tree->ops->writepage_io_failed_hook(bio, page,
1692 uptodate = (err == 0);
1698 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
1699 ClearPageUptodate(page);
1704 end_page_writeback(page);
1706 check_page_writeback(tree, page);
1707 } while (bvec >= bio->bi_io_vec);
1713 * after a readpage IO is done, we need to:
1714 * clear the uptodate bits on error
1715 * set the uptodate bits if things worked
1716 * set the page up to date if all extents in the tree are uptodate
1717 * clear the lock bit in the extent tree
1718 * unlock the page if there are no other extents locked for it
1720 * Scheduling is not allowed, so the extent state tree is expected
1721 * to have one and only one object corresponding to this IO.
1723 static void end_bio_extent_readpage(struct bio *bio, int err)
1725 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1726 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1727 struct bio_vec *bvec = bio->bi_io_vec;
1728 struct extent_io_tree *tree;
1738 struct page *page = bvec->bv_page;
1739 struct extent_state *cached = NULL;
1740 struct extent_state *state;
1742 tree = &BTRFS_I(page->mapping->host)->io_tree;
1744 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1746 end = start + bvec->bv_len - 1;
1748 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1753 if (++bvec <= bvec_end)
1754 prefetchw(&bvec->bv_page->flags);
1756 spin_lock(&tree->lock);
1757 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
1758 if (state && state->start == start) {
1760 * take a reference on the state, unlock will drop
1763 cache_state(state, &cached);
1765 spin_unlock(&tree->lock);
1767 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1768 ret = tree->ops->readpage_end_io_hook(page, start, end,
1773 if (!uptodate && tree->ops &&
1774 tree->ops->readpage_io_failed_hook) {
1775 ret = tree->ops->readpage_io_failed_hook(bio, page,
1779 test_bit(BIO_UPTODATE, &bio->bi_flags);
1782 uncache_state(&cached);
1788 set_extent_uptodate(tree, start, end, &cached,
1791 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
1795 SetPageUptodate(page);
1797 ClearPageUptodate(page);
1803 check_page_uptodate(tree, page);
1805 ClearPageUptodate(page);
1808 check_page_locked(tree, page);
1810 } while (bvec <= bvec_end);
1816 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1821 bio = bio_alloc(gfp_flags, nr_vecs);
1823 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1824 while (!bio && (nr_vecs /= 2))
1825 bio = bio_alloc(gfp_flags, nr_vecs);
1830 bio->bi_bdev = bdev;
1831 bio->bi_sector = first_sector;
1836 static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1837 unsigned long bio_flags)
1840 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1841 struct page *page = bvec->bv_page;
1842 struct extent_io_tree *tree = bio->bi_private;
1845 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1847 bio->bi_private = NULL;
1851 if (tree->ops && tree->ops->submit_bio_hook)
1852 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1853 mirror_num, bio_flags, start);
1855 submit_bio(rw, bio);
1856 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1862 static int submit_extent_page(int rw, struct extent_io_tree *tree,
1863 struct page *page, sector_t sector,
1864 size_t size, unsigned long offset,
1865 struct block_device *bdev,
1866 struct bio **bio_ret,
1867 unsigned long max_pages,
1868 bio_end_io_t end_io_func,
1870 unsigned long prev_bio_flags,
1871 unsigned long bio_flags)
1877 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1878 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
1879 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
1881 if (bio_ret && *bio_ret) {
1884 contig = bio->bi_sector == sector;
1886 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1889 if (prev_bio_flags != bio_flags || !contig ||
1890 (tree->ops && tree->ops->merge_bio_hook &&
1891 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1893 bio_add_page(bio, page, page_size, offset) < page_size) {
1894 ret = submit_one_bio(rw, bio, mirror_num,
1901 if (this_compressed)
1904 nr = bio_get_nr_vecs(bdev);
1906 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1910 bio_add_page(bio, page, page_size, offset);
1911 bio->bi_end_io = end_io_func;
1912 bio->bi_private = tree;
1917 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
1922 void set_page_extent_mapped(struct page *page)
1924 if (!PagePrivate(page)) {
1925 SetPagePrivate(page);
1926 page_cache_get(page);
1927 set_page_private(page, EXTENT_PAGE_PRIVATE);
1931 static void set_page_extent_head(struct page *page, unsigned long len)
1933 WARN_ON(!PagePrivate(page));
1934 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1938 * basic readpage implementation. Locked extent state structs are inserted
1939 * into the tree that are removed when the IO is done (by the end_io
1942 static int __extent_read_full_page(struct extent_io_tree *tree,
1944 get_extent_t *get_extent,
1945 struct bio **bio, int mirror_num,
1946 unsigned long *bio_flags)
1948 struct inode *inode = page->mapping->host;
1949 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1950 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1954 u64 last_byte = i_size_read(inode);
1958 struct extent_map *em;
1959 struct block_device *bdev;
1960 struct btrfs_ordered_extent *ordered;
1963 size_t pg_offset = 0;
1965 size_t disk_io_size;
1966 size_t blocksize = inode->i_sb->s_blocksize;
1967 unsigned long this_bio_flag = 0;
1969 set_page_extent_mapped(page);
1973 lock_extent(tree, start, end, GFP_NOFS);
1974 ordered = btrfs_lookup_ordered_extent(inode, start);
1977 unlock_extent(tree, start, end, GFP_NOFS);
1978 btrfs_start_ordered_extent(inode, ordered, 1);
1979 btrfs_put_ordered_extent(ordered);
1982 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1984 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1987 iosize = PAGE_CACHE_SIZE - zero_offset;
1988 userpage = kmap_atomic(page, KM_USER0);
1989 memset(userpage + zero_offset, 0, iosize);
1990 flush_dcache_page(page);
1991 kunmap_atomic(userpage, KM_USER0);
1994 while (cur <= end) {
1995 if (cur >= last_byte) {
1997 struct extent_state *cached = NULL;
1999 iosize = PAGE_CACHE_SIZE - pg_offset;
2000 userpage = kmap_atomic(page, KM_USER0);
2001 memset(userpage + pg_offset, 0, iosize);
2002 flush_dcache_page(page);
2003 kunmap_atomic(userpage, KM_USER0);
2004 set_extent_uptodate(tree, cur, cur + iosize - 1,
2006 unlock_extent_cached(tree, cur, cur + iosize - 1,
2010 em = get_extent(inode, page, pg_offset, cur,
2012 if (IS_ERR_OR_NULL(em)) {
2014 unlock_extent(tree, cur, end, GFP_NOFS);
2017 extent_offset = cur - em->start;
2018 BUG_ON(extent_map_end(em) <= cur);
2021 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2022 this_bio_flag = EXTENT_BIO_COMPRESSED;
2023 extent_set_compress_type(&this_bio_flag,
2027 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2028 cur_end = min(extent_map_end(em) - 1, end);
2029 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2030 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2031 disk_io_size = em->block_len;
2032 sector = em->block_start >> 9;
2034 sector = (em->block_start + extent_offset) >> 9;
2035 disk_io_size = iosize;
2038 block_start = em->block_start;
2039 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2040 block_start = EXTENT_MAP_HOLE;
2041 free_extent_map(em);
2044 /* we've found a hole, just zero and go on */
2045 if (block_start == EXTENT_MAP_HOLE) {
2047 struct extent_state *cached = NULL;
2049 userpage = kmap_atomic(page, KM_USER0);
2050 memset(userpage + pg_offset, 0, iosize);
2051 flush_dcache_page(page);
2052 kunmap_atomic(userpage, KM_USER0);
2054 set_extent_uptodate(tree, cur, cur + iosize - 1,
2056 unlock_extent_cached(tree, cur, cur + iosize - 1,
2059 pg_offset += iosize;
2062 /* the get_extent function already copied into the page */
2063 if (test_range_bit(tree, cur, cur_end,
2064 EXTENT_UPTODATE, 1, NULL)) {
2065 check_page_uptodate(tree, page);
2066 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2068 pg_offset += iosize;
2071 /* we have an inline extent but it didn't get marked up
2072 * to date. Error out
2074 if (block_start == EXTENT_MAP_INLINE) {
2076 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2078 pg_offset += iosize;
2083 if (tree->ops && tree->ops->readpage_io_hook) {
2084 ret = tree->ops->readpage_io_hook(page, cur,
2088 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2090 ret = submit_extent_page(READ, tree, page,
2091 sector, disk_io_size, pg_offset,
2093 end_bio_extent_readpage, mirror_num,
2097 *bio_flags = this_bio_flag;
2102 pg_offset += iosize;
2105 if (!PageError(page))
2106 SetPageUptodate(page);
2112 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2113 get_extent_t *get_extent)
2115 struct bio *bio = NULL;
2116 unsigned long bio_flags = 0;
2119 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2122 ret = submit_one_bio(READ, bio, 0, bio_flags);
2126 static noinline void update_nr_written(struct page *page,
2127 struct writeback_control *wbc,
2128 unsigned long nr_written)
2130 wbc->nr_to_write -= nr_written;
2131 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2132 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2133 page->mapping->writeback_index = page->index + nr_written;
2137 * the writepage semantics are similar to regular writepage. extent
2138 * records are inserted to lock ranges in the tree, and as dirty areas
2139 * are found, they are marked writeback. Then the lock bits are removed
2140 * and the end_io handler clears the writeback ranges
2142 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2145 struct inode *inode = page->mapping->host;
2146 struct extent_page_data *epd = data;
2147 struct extent_io_tree *tree = epd->tree;
2148 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2150 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2154 u64 last_byte = i_size_read(inode);
2158 struct extent_state *cached_state = NULL;
2159 struct extent_map *em;
2160 struct block_device *bdev;
2163 size_t pg_offset = 0;
2165 loff_t i_size = i_size_read(inode);
2166 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2172 unsigned long nr_written = 0;
2174 if (wbc->sync_mode == WB_SYNC_ALL)
2175 write_flags = WRITE_SYNC;
2177 write_flags = WRITE;
2179 trace___extent_writepage(page, inode, wbc);
2181 WARN_ON(!PageLocked(page));
2182 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2183 if (page->index > end_index ||
2184 (page->index == end_index && !pg_offset)) {
2185 page->mapping->a_ops->invalidatepage(page, 0);
2190 if (page->index == end_index) {
2193 userpage = kmap_atomic(page, KM_USER0);
2194 memset(userpage + pg_offset, 0,
2195 PAGE_CACHE_SIZE - pg_offset);
2196 kunmap_atomic(userpage, KM_USER0);
2197 flush_dcache_page(page);
2201 set_page_extent_mapped(page);
2203 delalloc_start = start;
2206 if (!epd->extent_locked) {
2207 u64 delalloc_to_write = 0;
2209 * make sure the wbc mapping index is at least updated
2212 update_nr_written(page, wbc, 0);
2214 while (delalloc_end < page_end) {
2215 nr_delalloc = find_lock_delalloc_range(inode, tree,
2220 if (nr_delalloc == 0) {
2221 delalloc_start = delalloc_end + 1;
2224 tree->ops->fill_delalloc(inode, page, delalloc_start,
2225 delalloc_end, &page_started,
2228 * delalloc_end is already one less than the total
2229 * length, so we don't subtract one from
2232 delalloc_to_write += (delalloc_end - delalloc_start +
2235 delalloc_start = delalloc_end + 1;
2237 if (wbc->nr_to_write < delalloc_to_write) {
2240 if (delalloc_to_write < thresh * 2)
2241 thresh = delalloc_to_write;
2242 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2246 /* did the fill delalloc function already unlock and start
2252 * we've unlocked the page, so we can't update
2253 * the mapping's writeback index, just update
2256 wbc->nr_to_write -= nr_written;
2260 if (tree->ops && tree->ops->writepage_start_hook) {
2261 ret = tree->ops->writepage_start_hook(page, start,
2263 if (ret == -EAGAIN) {
2264 redirty_page_for_writepage(wbc, page);
2265 update_nr_written(page, wbc, nr_written);
2273 * we don't want to touch the inode after unlocking the page,
2274 * so we update the mapping writeback index now
2276 update_nr_written(page, wbc, nr_written + 1);
2279 if (last_byte <= start) {
2280 if (tree->ops && tree->ops->writepage_end_io_hook)
2281 tree->ops->writepage_end_io_hook(page, start,
2286 blocksize = inode->i_sb->s_blocksize;
2288 while (cur <= end) {
2289 if (cur >= last_byte) {
2290 if (tree->ops && tree->ops->writepage_end_io_hook)
2291 tree->ops->writepage_end_io_hook(page, cur,
2295 em = epd->get_extent(inode, page, pg_offset, cur,
2297 if (IS_ERR_OR_NULL(em)) {
2302 extent_offset = cur - em->start;
2303 BUG_ON(extent_map_end(em) <= cur);
2305 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2306 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2307 sector = (em->block_start + extent_offset) >> 9;
2309 block_start = em->block_start;
2310 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2311 free_extent_map(em);
2315 * compressed and inline extents are written through other
2318 if (compressed || block_start == EXTENT_MAP_HOLE ||
2319 block_start == EXTENT_MAP_INLINE) {
2321 * end_io notification does not happen here for
2322 * compressed extents
2324 if (!compressed && tree->ops &&
2325 tree->ops->writepage_end_io_hook)
2326 tree->ops->writepage_end_io_hook(page, cur,
2329 else if (compressed) {
2330 /* we don't want to end_page_writeback on
2331 * a compressed extent. this happens
2338 pg_offset += iosize;
2341 /* leave this out until we have a page_mkwrite call */
2342 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2343 EXTENT_DIRTY, 0, NULL)) {
2345 pg_offset += iosize;
2349 if (tree->ops && tree->ops->writepage_io_hook) {
2350 ret = tree->ops->writepage_io_hook(page, cur,
2358 unsigned long max_nr = end_index + 1;
2360 set_range_writeback(tree, cur, cur + iosize - 1);
2361 if (!PageWriteback(page)) {
2362 printk(KERN_ERR "btrfs warning page %lu not "
2363 "writeback, cur %llu end %llu\n",
2364 page->index, (unsigned long long)cur,
2365 (unsigned long long)end);
2368 ret = submit_extent_page(write_flags, tree, page,
2369 sector, iosize, pg_offset,
2370 bdev, &epd->bio, max_nr,
2371 end_bio_extent_writepage,
2377 pg_offset += iosize;
2382 /* make sure the mapping tag for page dirty gets cleared */
2383 set_page_writeback(page);
2384 end_page_writeback(page);
2390 /* drop our reference on any cached states */
2391 free_extent_state(cached_state);
2396 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2397 * @mapping: address space structure to write
2398 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2399 * @writepage: function called for each page
2400 * @data: data passed to writepage function
2402 * If a page is already under I/O, write_cache_pages() skips it, even
2403 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2404 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2405 * and msync() need to guarantee that all the data which was dirty at the time
2406 * the call was made get new I/O started against them. If wbc->sync_mode is
2407 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2408 * existing IO to complete.
2410 static int extent_write_cache_pages(struct extent_io_tree *tree,
2411 struct address_space *mapping,
2412 struct writeback_control *wbc,
2413 writepage_t writepage, void *data,
2414 void (*flush_fn)(void *))
2418 int nr_to_write_done = 0;
2419 struct pagevec pvec;
2422 pgoff_t end; /* Inclusive */
2425 pagevec_init(&pvec, 0);
2426 if (wbc->range_cyclic) {
2427 index = mapping->writeback_index; /* Start from prev offset */
2430 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2431 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2435 while (!done && !nr_to_write_done && (index <= end) &&
2436 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2437 PAGECACHE_TAG_DIRTY, min(end - index,
2438 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2442 for (i = 0; i < nr_pages; i++) {
2443 struct page *page = pvec.pages[i];
2446 * At this point we hold neither mapping->tree_lock nor
2447 * lock on the page itself: the page may be truncated or
2448 * invalidated (changing page->mapping to NULL), or even
2449 * swizzled back from swapper_space to tmpfs file
2452 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2453 tree->ops->write_cache_pages_lock_hook(page);
2457 if (unlikely(page->mapping != mapping)) {
2462 if (!wbc->range_cyclic && page->index > end) {
2468 if (wbc->sync_mode != WB_SYNC_NONE) {
2469 if (PageWriteback(page))
2471 wait_on_page_writeback(page);
2474 if (PageWriteback(page) ||
2475 !clear_page_dirty_for_io(page)) {
2480 ret = (*writepage)(page, wbc, data);
2482 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2490 * the filesystem may choose to bump up nr_to_write.
2491 * We have to make sure to honor the new nr_to_write
2494 nr_to_write_done = wbc->nr_to_write <= 0;
2496 pagevec_release(&pvec);
2499 if (!scanned && !done) {
2501 * We hit the last page and there is more work to be done: wrap
2502 * back to the start of the file
2511 static void flush_epd_write_bio(struct extent_page_data *epd)
2515 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2517 submit_one_bio(WRITE, epd->bio, 0, 0);
2522 static noinline void flush_write_bio(void *data)
2524 struct extent_page_data *epd = data;
2525 flush_epd_write_bio(epd);
2528 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2529 get_extent_t *get_extent,
2530 struct writeback_control *wbc)
2533 struct address_space *mapping = page->mapping;
2534 struct extent_page_data epd = {
2537 .get_extent = get_extent,
2539 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
2541 struct writeback_control wbc_writepages = {
2542 .sync_mode = wbc->sync_mode,
2543 .older_than_this = NULL,
2545 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2546 .range_end = (loff_t)-1,
2549 ret = __extent_writepage(page, wbc, &epd);
2551 extent_write_cache_pages(tree, mapping, &wbc_writepages,
2552 __extent_writepage, &epd, flush_write_bio);
2553 flush_epd_write_bio(&epd);
2557 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2558 u64 start, u64 end, get_extent_t *get_extent,
2562 struct address_space *mapping = inode->i_mapping;
2564 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2567 struct extent_page_data epd = {
2570 .get_extent = get_extent,
2572 .sync_io = mode == WB_SYNC_ALL,
2574 struct writeback_control wbc_writepages = {
2576 .older_than_this = NULL,
2577 .nr_to_write = nr_pages * 2,
2578 .range_start = start,
2579 .range_end = end + 1,
2582 while (start <= end) {
2583 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2584 if (clear_page_dirty_for_io(page))
2585 ret = __extent_writepage(page, &wbc_writepages, &epd);
2587 if (tree->ops && tree->ops->writepage_end_io_hook)
2588 tree->ops->writepage_end_io_hook(page, start,
2589 start + PAGE_CACHE_SIZE - 1,
2593 page_cache_release(page);
2594 start += PAGE_CACHE_SIZE;
2597 flush_epd_write_bio(&epd);
2601 int extent_writepages(struct extent_io_tree *tree,
2602 struct address_space *mapping,
2603 get_extent_t *get_extent,
2604 struct writeback_control *wbc)
2607 struct extent_page_data epd = {
2610 .get_extent = get_extent,
2612 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
2615 ret = extent_write_cache_pages(tree, mapping, wbc,
2616 __extent_writepage, &epd,
2618 flush_epd_write_bio(&epd);
2622 int extent_readpages(struct extent_io_tree *tree,
2623 struct address_space *mapping,
2624 struct list_head *pages, unsigned nr_pages,
2625 get_extent_t get_extent)
2627 struct bio *bio = NULL;
2629 unsigned long bio_flags = 0;
2631 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2632 struct page *page = list_entry(pages->prev, struct page, lru);
2634 prefetchw(&page->flags);
2635 list_del(&page->lru);
2636 if (!add_to_page_cache_lru(page, mapping,
2637 page->index, GFP_NOFS)) {
2638 __extent_read_full_page(tree, page, get_extent,
2639 &bio, 0, &bio_flags);
2641 page_cache_release(page);
2643 BUG_ON(!list_empty(pages));
2645 submit_one_bio(READ, bio, 0, bio_flags);
2650 * basic invalidatepage code, this waits on any locked or writeback
2651 * ranges corresponding to the page, and then deletes any extent state
2652 * records from the tree
2654 int extent_invalidatepage(struct extent_io_tree *tree,
2655 struct page *page, unsigned long offset)
2657 struct extent_state *cached_state = NULL;
2658 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2659 u64 end = start + PAGE_CACHE_SIZE - 1;
2660 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2662 start += (offset + blocksize - 1) & ~(blocksize - 1);
2666 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
2667 wait_on_page_writeback(page);
2668 clear_extent_bit(tree, start, end,
2669 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2670 EXTENT_DO_ACCOUNTING,
2671 1, 1, &cached_state, GFP_NOFS);
2676 * a helper for releasepage, this tests for areas of the page that
2677 * are locked or under IO and drops the related state bits if it is safe
2680 int try_release_extent_state(struct extent_map_tree *map,
2681 struct extent_io_tree *tree, struct page *page,
2684 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2685 u64 end = start + PAGE_CACHE_SIZE - 1;
2688 if (test_range_bit(tree, start, end,
2689 EXTENT_IOBITS, 0, NULL))
2692 if ((mask & GFP_NOFS) == GFP_NOFS)
2695 * at this point we can safely clear everything except the
2696 * locked bit and the nodatasum bit
2698 ret = clear_extent_bit(tree, start, end,
2699 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2702 /* if clear_extent_bit failed for enomem reasons,
2703 * we can't allow the release to continue.
2714 * a helper for releasepage. As long as there are no locked extents
2715 * in the range corresponding to the page, both state records and extent
2716 * map records are removed
2718 int try_release_extent_mapping(struct extent_map_tree *map,
2719 struct extent_io_tree *tree, struct page *page,
2722 struct extent_map *em;
2723 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2724 u64 end = start + PAGE_CACHE_SIZE - 1;
2726 if ((mask & __GFP_WAIT) &&
2727 page->mapping->host->i_size > 16 * 1024 * 1024) {
2729 while (start <= end) {
2730 len = end - start + 1;
2731 write_lock(&map->lock);
2732 em = lookup_extent_mapping(map, start, len);
2733 if (IS_ERR_OR_NULL(em)) {
2734 write_unlock(&map->lock);
2737 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2738 em->start != start) {
2739 write_unlock(&map->lock);
2740 free_extent_map(em);
2743 if (!test_range_bit(tree, em->start,
2744 extent_map_end(em) - 1,
2745 EXTENT_LOCKED | EXTENT_WRITEBACK,
2747 remove_extent_mapping(map, em);
2748 /* once for the rb tree */
2749 free_extent_map(em);
2751 start = extent_map_end(em);
2752 write_unlock(&map->lock);
2755 free_extent_map(em);
2758 return try_release_extent_state(map, tree, page, mask);
2762 * helper function for fiemap, which doesn't want to see any holes.
2763 * This maps until we find something past 'last'
2765 static struct extent_map *get_extent_skip_holes(struct inode *inode,
2768 get_extent_t *get_extent)
2770 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
2771 struct extent_map *em;
2778 len = last - offset;
2781 len = (len + sectorsize - 1) & ~(sectorsize - 1);
2782 em = get_extent(inode, NULL, 0, offset, len, 0);
2783 if (IS_ERR_OR_NULL(em))
2786 /* if this isn't a hole return it */
2787 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
2788 em->block_start != EXTENT_MAP_HOLE) {
2792 /* this is a hole, advance to the next extent */
2793 offset = extent_map_end(em);
2794 free_extent_map(em);
2801 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2802 __u64 start, __u64 len, get_extent_t *get_extent)
2806 u64 max = start + len;
2810 u64 last_for_get_extent = 0;
2812 u64 isize = i_size_read(inode);
2813 struct btrfs_key found_key;
2814 struct extent_map *em = NULL;
2815 struct extent_state *cached_state = NULL;
2816 struct btrfs_path *path;
2817 struct btrfs_file_extent_item *item;
2822 unsigned long emflags;
2827 path = btrfs_alloc_path();
2830 path->leave_spinning = 1;
2833 * lookup the last file extent. We're not using i_size here
2834 * because there might be preallocation past i_size
2836 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
2837 path, btrfs_ino(inode), -1, 0);
2839 btrfs_free_path(path);
2844 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2845 struct btrfs_file_extent_item);
2846 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
2847 found_type = btrfs_key_type(&found_key);
2849 /* No extents, but there might be delalloc bits */
2850 if (found_key.objectid != btrfs_ino(inode) ||
2851 found_type != BTRFS_EXTENT_DATA_KEY) {
2852 /* have to trust i_size as the end */
2854 last_for_get_extent = isize;
2857 * remember the start of the last extent. There are a
2858 * bunch of different factors that go into the length of the
2859 * extent, so its much less complex to remember where it started
2861 last = found_key.offset;
2862 last_for_get_extent = last + 1;
2864 btrfs_free_path(path);
2867 * we might have some extents allocated but more delalloc past those
2868 * extents. so, we trust isize unless the start of the last extent is
2873 last_for_get_extent = isize;
2876 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
2877 &cached_state, GFP_NOFS);
2879 em = get_extent_skip_holes(inode, off, last_for_get_extent,
2889 u64 offset_in_extent;
2891 /* break if the extent we found is outside the range */
2892 if (em->start >= max || extent_map_end(em) < off)
2896 * get_extent may return an extent that starts before our
2897 * requested range. We have to make sure the ranges
2898 * we return to fiemap always move forward and don't
2899 * overlap, so adjust the offsets here
2901 em_start = max(em->start, off);
2904 * record the offset from the start of the extent
2905 * for adjusting the disk offset below
2907 offset_in_extent = em_start - em->start;
2908 em_end = extent_map_end(em);
2909 em_len = em_end - em_start;
2910 emflags = em->flags;
2915 * bump off for our next call to get_extent
2917 off = extent_map_end(em);
2921 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
2923 flags |= FIEMAP_EXTENT_LAST;
2924 } else if (em->block_start == EXTENT_MAP_INLINE) {
2925 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2926 FIEMAP_EXTENT_NOT_ALIGNED);
2927 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
2928 flags |= (FIEMAP_EXTENT_DELALLOC |
2929 FIEMAP_EXTENT_UNKNOWN);
2931 disko = em->block_start + offset_in_extent;
2933 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2934 flags |= FIEMAP_EXTENT_ENCODED;
2936 free_extent_map(em);
2938 if ((em_start >= last) || em_len == (u64)-1 ||
2939 (last == (u64)-1 && isize <= em_end)) {
2940 flags |= FIEMAP_EXTENT_LAST;
2944 /* now scan forward to see if this is really the last extent. */
2945 em = get_extent_skip_holes(inode, off, last_for_get_extent,
2952 flags |= FIEMAP_EXTENT_LAST;
2955 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2961 free_extent_map(em);
2963 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
2964 &cached_state, GFP_NOFS);
2968 static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2972 struct address_space *mapping;
2975 return eb->first_page;
2976 i += eb->start >> PAGE_CACHE_SHIFT;
2977 mapping = eb->first_page->mapping;
2982 * extent_buffer_page is only called after pinning the page
2983 * by increasing the reference count. So we know the page must
2984 * be in the radix tree.
2987 p = radix_tree_lookup(&mapping->page_tree, i);
2993 static inline unsigned long num_extent_pages(u64 start, u64 len)
2995 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2996 (start >> PAGE_CACHE_SHIFT);
2999 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3004 struct extent_buffer *eb = NULL;
3006 unsigned long flags;
3009 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
3014 spin_lock_init(&eb->lock);
3015 init_waitqueue_head(&eb->lock_wq);
3018 spin_lock_irqsave(&leak_lock, flags);
3019 list_add(&eb->leak_list, &buffers);
3020 spin_unlock_irqrestore(&leak_lock, flags);
3022 atomic_set(&eb->refs, 1);
3027 static void __free_extent_buffer(struct extent_buffer *eb)
3030 unsigned long flags;
3031 spin_lock_irqsave(&leak_lock, flags);
3032 list_del(&eb->leak_list);
3033 spin_unlock_irqrestore(&leak_lock, flags);
3035 kmem_cache_free(extent_buffer_cache, eb);
3039 * Helper for releasing extent buffer page.
3041 static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3042 unsigned long start_idx)
3044 unsigned long index;
3047 if (!eb->first_page)
3050 index = num_extent_pages(eb->start, eb->len);
3051 if (start_idx >= index)
3056 page = extent_buffer_page(eb, index);
3058 page_cache_release(page);
3059 } while (index != start_idx);
3063 * Helper for releasing the extent buffer.
3065 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3067 btrfs_release_extent_buffer_page(eb, 0);
3068 __free_extent_buffer(eb);
3071 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3072 u64 start, unsigned long len,
3075 unsigned long num_pages = num_extent_pages(start, len);
3077 unsigned long index = start >> PAGE_CACHE_SHIFT;
3078 struct extent_buffer *eb;
3079 struct extent_buffer *exists = NULL;
3081 struct address_space *mapping = tree->mapping;
3086 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3087 if (eb && atomic_inc_not_zero(&eb->refs)) {
3089 mark_page_accessed(eb->first_page);
3094 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
3099 eb->first_page = page0;
3102 page_cache_get(page0);
3103 mark_page_accessed(page0);
3104 set_page_extent_mapped(page0);
3105 set_page_extent_head(page0, len);
3106 uptodate = PageUptodate(page0);
3110 for (; i < num_pages; i++, index++) {
3111 p = find_or_create_page(mapping, index, GFP_NOFS | __GFP_HIGHMEM);
3116 set_page_extent_mapped(p);
3117 mark_page_accessed(p);
3120 set_page_extent_head(p, len);
3122 set_page_private(p, EXTENT_PAGE_PRIVATE);
3124 if (!PageUptodate(p))
3128 * see below about how we avoid a nasty race with release page
3129 * and why we unlock later
3135 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3137 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3141 spin_lock(&tree->buffer_lock);
3142 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3143 if (ret == -EEXIST) {
3144 exists = radix_tree_lookup(&tree->buffer,
3145 start >> PAGE_CACHE_SHIFT);
3146 /* add one reference for the caller */
3147 atomic_inc(&exists->refs);
3148 spin_unlock(&tree->buffer_lock);
3149 radix_tree_preload_end();
3152 /* add one reference for the tree */
3153 atomic_inc(&eb->refs);
3154 spin_unlock(&tree->buffer_lock);
3155 radix_tree_preload_end();
3158 * there is a race where release page may have
3159 * tried to find this extent buffer in the radix
3160 * but failed. It will tell the VM it is safe to
3161 * reclaim the, and it will clear the page private bit.
3162 * We must make sure to set the page private bit properly
3163 * after the extent buffer is in the radix tree so
3164 * it doesn't get lost
3166 set_page_extent_mapped(eb->first_page);
3167 set_page_extent_head(eb->first_page, eb->len);
3169 unlock_page(eb->first_page);
3173 if (eb->first_page && !page0)
3174 unlock_page(eb->first_page);
3176 if (!atomic_dec_and_test(&eb->refs))
3178 btrfs_release_extent_buffer(eb);
3182 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3183 u64 start, unsigned long len)
3185 struct extent_buffer *eb;
3188 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3189 if (eb && atomic_inc_not_zero(&eb->refs)) {
3191 mark_page_accessed(eb->first_page);
3199 void free_extent_buffer(struct extent_buffer *eb)
3204 if (!atomic_dec_and_test(&eb->refs))
3210 int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3211 struct extent_buffer *eb)
3214 unsigned long num_pages;
3217 num_pages = num_extent_pages(eb->start, eb->len);
3219 for (i = 0; i < num_pages; i++) {
3220 page = extent_buffer_page(eb, i);
3221 if (!PageDirty(page))
3225 WARN_ON(!PagePrivate(page));
3227 set_page_extent_mapped(page);
3229 set_page_extent_head(page, eb->len);
3231 clear_page_dirty_for_io(page);
3232 spin_lock_irq(&page->mapping->tree_lock);
3233 if (!PageDirty(page)) {
3234 radix_tree_tag_clear(&page->mapping->page_tree,
3236 PAGECACHE_TAG_DIRTY);
3238 spin_unlock_irq(&page->mapping->tree_lock);
3244 int set_extent_buffer_dirty(struct extent_io_tree *tree,
3245 struct extent_buffer *eb)
3248 unsigned long num_pages;
3251 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3252 num_pages = num_extent_pages(eb->start, eb->len);
3253 for (i = 0; i < num_pages; i++)
3254 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
3258 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3259 struct extent_buffer *eb,
3260 struct extent_state **cached_state)
3264 unsigned long num_pages;
3266 num_pages = num_extent_pages(eb->start, eb->len);
3267 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3269 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3270 cached_state, GFP_NOFS);
3271 for (i = 0; i < num_pages; i++) {
3272 page = extent_buffer_page(eb, i);
3274 ClearPageUptodate(page);
3279 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3280 struct extent_buffer *eb)
3284 unsigned long num_pages;
3286 num_pages = num_extent_pages(eb->start, eb->len);
3288 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3290 for (i = 0; i < num_pages; i++) {
3291 page = extent_buffer_page(eb, i);
3292 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3293 ((i == num_pages - 1) &&
3294 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3295 check_page_uptodate(tree, page);
3298 SetPageUptodate(page);
3303 int extent_range_uptodate(struct extent_io_tree *tree,
3308 int pg_uptodate = 1;
3310 unsigned long index;
3312 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
3315 while (start <= end) {
3316 index = start >> PAGE_CACHE_SHIFT;
3317 page = find_get_page(tree->mapping, index);
3318 uptodate = PageUptodate(page);
3319 page_cache_release(page);
3324 start += PAGE_CACHE_SIZE;
3329 int extent_buffer_uptodate(struct extent_io_tree *tree,
3330 struct extent_buffer *eb,
3331 struct extent_state *cached_state)
3334 unsigned long num_pages;
3337 int pg_uptodate = 1;
3339 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3342 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3343 EXTENT_UPTODATE, 1, cached_state);
3347 num_pages = num_extent_pages(eb->start, eb->len);
3348 for (i = 0; i < num_pages; i++) {
3349 page = extent_buffer_page(eb, i);
3350 if (!PageUptodate(page)) {
3358 int read_extent_buffer_pages(struct extent_io_tree *tree,
3359 struct extent_buffer *eb,
3360 u64 start, int wait,
3361 get_extent_t *get_extent, int mirror_num)
3364 unsigned long start_i;
3368 int locked_pages = 0;
3369 int all_uptodate = 1;
3370 int inc_all_pages = 0;
3371 unsigned long num_pages;
3372 struct bio *bio = NULL;
3373 unsigned long bio_flags = 0;
3375 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3378 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3379 EXTENT_UPTODATE, 1, NULL)) {
3384 WARN_ON(start < eb->start);
3385 start_i = (start >> PAGE_CACHE_SHIFT) -
3386 (eb->start >> PAGE_CACHE_SHIFT);
3391 num_pages = num_extent_pages(eb->start, eb->len);
3392 for (i = start_i; i < num_pages; i++) {
3393 page = extent_buffer_page(eb, i);
3395 if (!trylock_page(page))
3401 if (!PageUptodate(page))
3406 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3410 for (i = start_i; i < num_pages; i++) {
3411 page = extent_buffer_page(eb, i);
3413 WARN_ON(!PagePrivate(page));
3415 set_page_extent_mapped(page);
3417 set_page_extent_head(page, eb->len);
3420 page_cache_get(page);
3421 if (!PageUptodate(page)) {
3424 ClearPageError(page);
3425 err = __extent_read_full_page(tree, page,
3427 mirror_num, &bio_flags);
3436 submit_one_bio(READ, bio, mirror_num, bio_flags);
3441 for (i = start_i; i < num_pages; i++) {
3442 page = extent_buffer_page(eb, i);
3443 wait_on_page_locked(page);
3444 if (!PageUptodate(page))
3449 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3454 while (locked_pages > 0) {
3455 page = extent_buffer_page(eb, i);
3463 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3464 unsigned long start,
3471 char *dst = (char *)dstv;
3472 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3473 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3475 WARN_ON(start > eb->len);
3476 WARN_ON(start + len > eb->start + eb->len);
3478 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3481 page = extent_buffer_page(eb, i);
3483 cur = min(len, (PAGE_CACHE_SIZE - offset));
3484 kaddr = kmap_atomic(page, KM_USER1);
3485 memcpy(dst, kaddr + offset, cur);
3486 kunmap_atomic(kaddr, KM_USER1);
3495 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3496 unsigned long min_len, char **token, char **map,
3497 unsigned long *map_start,
3498 unsigned long *map_len, int km)
3500 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3503 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3504 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3505 unsigned long end_i = (start_offset + start + min_len - 1) >>
3512 offset = start_offset;
3516 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3519 if (start + min_len > eb->len) {
3520 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3521 "wanted %lu %lu\n", (unsigned long long)eb->start,
3522 eb->len, start, min_len);
3527 p = extent_buffer_page(eb, i);
3528 kaddr = kmap_atomic(p, km);
3530 *map = kaddr + offset;
3531 *map_len = PAGE_CACHE_SIZE - offset;
3535 int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3536 unsigned long min_len,
3537 char **token, char **map,
3538 unsigned long *map_start,
3539 unsigned long *map_len, int km)
3543 if (eb->map_token) {
3544 unmap_extent_buffer(eb, eb->map_token, km);
3545 eb->map_token = NULL;
3548 err = map_private_extent_buffer(eb, start, min_len, token, map,
3549 map_start, map_len, km);
3551 eb->map_token = *token;
3553 eb->map_start = *map_start;
3554 eb->map_len = *map_len;
3559 void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3561 kunmap_atomic(token, km);
3564 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3565 unsigned long start,
3572 char *ptr = (char *)ptrv;
3573 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3574 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3577 WARN_ON(start > eb->len);
3578 WARN_ON(start + len > eb->start + eb->len);
3580 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3583 page = extent_buffer_page(eb, i);
3585 cur = min(len, (PAGE_CACHE_SIZE - offset));
3587 kaddr = kmap_atomic(page, KM_USER0);
3588 ret = memcmp(ptr, kaddr + offset, cur);
3589 kunmap_atomic(kaddr, KM_USER0);
3601 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3602 unsigned long start, unsigned long len)
3608 char *src = (char *)srcv;
3609 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3610 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3612 WARN_ON(start > eb->len);
3613 WARN_ON(start + len > eb->start + eb->len);
3615 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3618 page = extent_buffer_page(eb, i);
3619 WARN_ON(!PageUptodate(page));
3621 cur = min(len, PAGE_CACHE_SIZE - offset);
3622 kaddr = kmap_atomic(page, KM_USER1);
3623 memcpy(kaddr + offset, src, cur);
3624 kunmap_atomic(kaddr, KM_USER1);
3633 void memset_extent_buffer(struct extent_buffer *eb, char c,
3634 unsigned long start, unsigned long len)
3640 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3641 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3643 WARN_ON(start > eb->len);
3644 WARN_ON(start + len > eb->start + eb->len);
3646 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3649 page = extent_buffer_page(eb, i);
3650 WARN_ON(!PageUptodate(page));
3652 cur = min(len, PAGE_CACHE_SIZE - offset);
3653 kaddr = kmap_atomic(page, KM_USER0);
3654 memset(kaddr + offset, c, cur);
3655 kunmap_atomic(kaddr, KM_USER0);
3663 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3664 unsigned long dst_offset, unsigned long src_offset,
3667 u64 dst_len = dst->len;
3672 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3673 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3675 WARN_ON(src->len != dst_len);
3677 offset = (start_offset + dst_offset) &
3678 ((unsigned long)PAGE_CACHE_SIZE - 1);
3681 page = extent_buffer_page(dst, i);
3682 WARN_ON(!PageUptodate(page));
3684 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3686 kaddr = kmap_atomic(page, KM_USER0);
3687 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3688 kunmap_atomic(kaddr, KM_USER0);
3697 static void move_pages(struct page *dst_page, struct page *src_page,
3698 unsigned long dst_off, unsigned long src_off,
3701 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3702 if (dst_page == src_page) {
3703 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3705 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3706 char *p = dst_kaddr + dst_off + len;
3707 char *s = src_kaddr + src_off + len;
3712 kunmap_atomic(src_kaddr, KM_USER1);
3714 kunmap_atomic(dst_kaddr, KM_USER0);
3717 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
3719 unsigned long distance = (src > dst) ? src - dst : dst - src;
3720 return distance < len;
3723 static void copy_pages(struct page *dst_page, struct page *src_page,
3724 unsigned long dst_off, unsigned long src_off,
3727 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3730 if (dst_page != src_page) {
3731 src_kaddr = kmap_atomic(src_page, KM_USER1);
3733 src_kaddr = dst_kaddr;
3734 BUG_ON(areas_overlap(src_off, dst_off, len));
3737 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3738 kunmap_atomic(dst_kaddr, KM_USER0);
3739 if (dst_page != src_page)
3740 kunmap_atomic(src_kaddr, KM_USER1);
3743 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3744 unsigned long src_offset, unsigned long len)
3747 size_t dst_off_in_page;
3748 size_t src_off_in_page;
3749 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3750 unsigned long dst_i;
3751 unsigned long src_i;
3753 if (src_offset + len > dst->len) {
3754 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3755 "len %lu dst len %lu\n", src_offset, len, dst->len);
3758 if (dst_offset + len > dst->len) {
3759 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3760 "len %lu dst len %lu\n", dst_offset, len, dst->len);
3765 dst_off_in_page = (start_offset + dst_offset) &
3766 ((unsigned long)PAGE_CACHE_SIZE - 1);
3767 src_off_in_page = (start_offset + src_offset) &
3768 ((unsigned long)PAGE_CACHE_SIZE - 1);
3770 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3771 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3773 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3775 cur = min_t(unsigned long, cur,
3776 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3778 copy_pages(extent_buffer_page(dst, dst_i),
3779 extent_buffer_page(dst, src_i),
3780 dst_off_in_page, src_off_in_page, cur);
3788 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3789 unsigned long src_offset, unsigned long len)
3792 size_t dst_off_in_page;
3793 size_t src_off_in_page;
3794 unsigned long dst_end = dst_offset + len - 1;
3795 unsigned long src_end = src_offset + len - 1;
3796 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3797 unsigned long dst_i;
3798 unsigned long src_i;
3800 if (src_offset + len > dst->len) {
3801 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3802 "len %lu len %lu\n", src_offset, len, dst->len);
3805 if (dst_offset + len > dst->len) {
3806 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3807 "len %lu len %lu\n", dst_offset, len, dst->len);
3810 if (!areas_overlap(src_offset, dst_offset, len)) {
3811 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3815 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3816 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3818 dst_off_in_page = (start_offset + dst_end) &
3819 ((unsigned long)PAGE_CACHE_SIZE - 1);
3820 src_off_in_page = (start_offset + src_end) &
3821 ((unsigned long)PAGE_CACHE_SIZE - 1);
3823 cur = min_t(unsigned long, len, src_off_in_page + 1);
3824 cur = min(cur, dst_off_in_page + 1);
3825 move_pages(extent_buffer_page(dst, dst_i),
3826 extent_buffer_page(dst, src_i),
3827 dst_off_in_page - cur + 1,
3828 src_off_in_page - cur + 1, cur);
3836 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
3838 struct extent_buffer *eb =
3839 container_of(head, struct extent_buffer, rcu_head);
3841 btrfs_release_extent_buffer(eb);
3844 int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3846 u64 start = page_offset(page);
3847 struct extent_buffer *eb;
3850 spin_lock(&tree->buffer_lock);
3851 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3853 spin_unlock(&tree->buffer_lock);
3857 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3863 * set @eb->refs to 0 if it is already 1, and then release the @eb.
3866 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
3871 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3873 spin_unlock(&tree->buffer_lock);
3875 /* at this point we can safely release the extent buffer */
3876 if (atomic_read(&eb->refs) == 0)
3877 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);