1 #include <linux/bitops.h>
2 #include <linux/slab.h>
6 #include <linux/pagemap.h>
7 #include <linux/page-flags.h>
8 #include <linux/module.h>
9 #include <linux/spinlock.h>
10 #include <linux/blkdev.h>
11 #include <linux/swap.h>
12 #include <linux/writeback.h>
13 #include <linux/pagevec.h>
14 #include "extent_io.h"
15 #include "extent_map.h"
18 #include "btrfs_inode.h"
20 static struct kmem_cache *extent_state_cache;
21 static struct kmem_cache *extent_buffer_cache;
23 static LIST_HEAD(buffers);
24 static LIST_HEAD(states);
28 static DEFINE_SPINLOCK(leak_lock);
31 #define BUFFER_LRU_MAX 64
36 struct rb_node rb_node;
39 struct extent_page_data {
41 struct extent_io_tree *tree;
42 get_extent_t *get_extent;
44 /* tells writepage not to lock the state bits for this range
45 * it still does the unlocking
47 unsigned int extent_locked:1;
49 /* tells the submit_bio code to use a WRITE_SYNC */
50 unsigned int sync_io:1;
53 int __init extent_io_init(void)
55 extent_state_cache = kmem_cache_create("extent_state",
56 sizeof(struct extent_state), 0,
57 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
58 if (!extent_state_cache)
61 extent_buffer_cache = kmem_cache_create("extent_buffers",
62 sizeof(struct extent_buffer), 0,
63 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
64 if (!extent_buffer_cache)
65 goto free_state_cache;
69 kmem_cache_destroy(extent_state_cache);
73 void extent_io_exit(void)
75 struct extent_state *state;
76 struct extent_buffer *eb;
78 while (!list_empty(&states)) {
79 state = list_entry(states.next, struct extent_state, leak_list);
80 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
81 "state %lu in tree %p refs %d\n",
82 (unsigned long long)state->start,
83 (unsigned long long)state->end,
84 state->state, state->tree, atomic_read(&state->refs));
85 list_del(&state->leak_list);
86 kmem_cache_free(extent_state_cache, state);
90 while (!list_empty(&buffers)) {
91 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
92 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
93 "refs %d\n", (unsigned long long)eb->start,
94 eb->len, atomic_read(&eb->refs));
95 list_del(&eb->leak_list);
96 kmem_cache_free(extent_buffer_cache, eb);
98 if (extent_state_cache)
99 kmem_cache_destroy(extent_state_cache);
100 if (extent_buffer_cache)
101 kmem_cache_destroy(extent_buffer_cache);
104 void extent_io_tree_init(struct extent_io_tree *tree,
105 struct address_space *mapping, gfp_t mask)
107 tree->state.rb_node = NULL;
108 tree->buffer.rb_node = NULL;
110 tree->dirty_bytes = 0;
111 spin_lock_init(&tree->lock);
112 spin_lock_init(&tree->buffer_lock);
113 tree->mapping = mapping;
116 static struct extent_state *alloc_extent_state(gfp_t mask)
118 struct extent_state *state;
123 state = kmem_cache_alloc(extent_state_cache, mask);
130 spin_lock_irqsave(&leak_lock, flags);
131 list_add(&state->leak_list, &states);
132 spin_unlock_irqrestore(&leak_lock, flags);
134 atomic_set(&state->refs, 1);
135 init_waitqueue_head(&state->wq);
139 static void free_extent_state(struct extent_state *state)
143 if (atomic_dec_and_test(&state->refs)) {
147 WARN_ON(state->tree);
149 spin_lock_irqsave(&leak_lock, flags);
150 list_del(&state->leak_list);
151 spin_unlock_irqrestore(&leak_lock, flags);
153 kmem_cache_free(extent_state_cache, state);
157 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
158 struct rb_node *node)
160 struct rb_node **p = &root->rb_node;
161 struct rb_node *parent = NULL;
162 struct tree_entry *entry;
166 entry = rb_entry(parent, struct tree_entry, rb_node);
168 if (offset < entry->start)
170 else if (offset > entry->end)
176 entry = rb_entry(node, struct tree_entry, rb_node);
177 rb_link_node(node, parent, p);
178 rb_insert_color(node, root);
182 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
183 struct rb_node **prev_ret,
184 struct rb_node **next_ret)
186 struct rb_root *root = &tree->state;
187 struct rb_node *n = root->rb_node;
188 struct rb_node *prev = NULL;
189 struct rb_node *orig_prev = NULL;
190 struct tree_entry *entry;
191 struct tree_entry *prev_entry = NULL;
194 entry = rb_entry(n, struct tree_entry, rb_node);
198 if (offset < entry->start)
200 else if (offset > entry->end)
208 while (prev && offset > prev_entry->end) {
209 prev = rb_next(prev);
210 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
217 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
218 while (prev && offset < prev_entry->start) {
219 prev = rb_prev(prev);
220 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
227 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
230 struct rb_node *prev = NULL;
233 ret = __etree_search(tree, offset, &prev, NULL);
239 static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
240 u64 offset, struct rb_node *node)
242 struct rb_root *root = &tree->buffer;
243 struct rb_node **p = &root->rb_node;
244 struct rb_node *parent = NULL;
245 struct extent_buffer *eb;
249 eb = rb_entry(parent, struct extent_buffer, rb_node);
251 if (offset < eb->start)
253 else if (offset > eb->start)
259 rb_link_node(node, parent, p);
260 rb_insert_color(node, root);
264 static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
267 struct rb_root *root = &tree->buffer;
268 struct rb_node *n = root->rb_node;
269 struct extent_buffer *eb;
272 eb = rb_entry(n, struct extent_buffer, rb_node);
273 if (offset < eb->start)
275 else if (offset > eb->start)
284 * utility function to look for merge candidates inside a given range.
285 * Any extents with matching state are merged together into a single
286 * extent in the tree. Extents with EXTENT_IO in their state field
287 * are not merged because the end_io handlers need to be able to do
288 * operations on them without sleeping (or doing allocations/splits).
290 * This should be called with the tree lock held.
292 static int merge_state(struct extent_io_tree *tree,
293 struct extent_state *state)
295 struct extent_state *other;
296 struct rb_node *other_node;
298 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
301 other_node = rb_prev(&state->rb_node);
303 other = rb_entry(other_node, struct extent_state, rb_node);
304 if (other->end == state->start - 1 &&
305 other->state == state->state) {
306 state->start = other->start;
308 rb_erase(&other->rb_node, &tree->state);
309 free_extent_state(other);
312 other_node = rb_next(&state->rb_node);
314 other = rb_entry(other_node, struct extent_state, rb_node);
315 if (other->start == state->end + 1 &&
316 other->state == state->state) {
317 other->start = state->start;
319 rb_erase(&state->rb_node, &tree->state);
320 free_extent_state(state);
326 static void set_state_cb(struct extent_io_tree *tree,
327 struct extent_state *state,
330 if (tree->ops && tree->ops->set_bit_hook) {
331 tree->ops->set_bit_hook(tree->mapping->host, state->start,
332 state->end, state->state, bits);
336 static void clear_state_cb(struct extent_io_tree *tree,
337 struct extent_state *state,
340 if (tree->ops && tree->ops->clear_bit_hook) {
341 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
342 state->end, state->state, bits);
347 * insert an extent_state struct into the tree. 'bits' are set on the
348 * struct before it is inserted.
350 * This may return -EEXIST if the extent is already there, in which case the
351 * state struct is freed.
353 * The tree lock is not taken internally. This is a utility function and
354 * probably isn't what you want to call (see set/clear_extent_bit).
356 static int insert_state(struct extent_io_tree *tree,
357 struct extent_state *state, u64 start, u64 end,
360 struct rb_node *node;
363 printk(KERN_ERR "btrfs end < start %llu %llu\n",
364 (unsigned long long)end,
365 (unsigned long long)start);
368 if (bits & EXTENT_DIRTY)
369 tree->dirty_bytes += end - start + 1;
370 state->start = start;
372 set_state_cb(tree, state, bits);
373 state->state |= bits;
374 node = tree_insert(&tree->state, end, &state->rb_node);
376 struct extent_state *found;
377 found = rb_entry(node, struct extent_state, rb_node);
378 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
379 "%llu %llu\n", (unsigned long long)found->start,
380 (unsigned long long)found->end,
381 (unsigned long long)start, (unsigned long long)end);
382 free_extent_state(state);
386 merge_state(tree, state);
391 * split a given extent state struct in two, inserting the preallocated
392 * struct 'prealloc' as the newly created second half. 'split' indicates an
393 * offset inside 'orig' where it should be split.
396 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
397 * are two extent state structs in the tree:
398 * prealloc: [orig->start, split - 1]
399 * orig: [ split, orig->end ]
401 * The tree locks are not taken by this function. They need to be held
404 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
405 struct extent_state *prealloc, u64 split)
407 struct rb_node *node;
408 prealloc->start = orig->start;
409 prealloc->end = split - 1;
410 prealloc->state = orig->state;
413 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
415 free_extent_state(prealloc);
418 prealloc->tree = tree;
423 * utility function to clear some bits in an extent state struct.
424 * it will optionally wake up any one waiting on this state (wake == 1), or
425 * forcibly remove the state from the tree (delete == 1).
427 * If no bits are set on the state struct after clearing things, the
428 * struct is freed and removed from the tree
430 static int clear_state_bit(struct extent_io_tree *tree,
431 struct extent_state *state, int bits, int wake,
434 int ret = state->state & bits;
436 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
437 u64 range = state->end - state->start + 1;
438 WARN_ON(range > tree->dirty_bytes);
439 tree->dirty_bytes -= range;
441 clear_state_cb(tree, state, bits);
442 state->state &= ~bits;
445 if (delete || state->state == 0) {
447 clear_state_cb(tree, state, state->state);
448 rb_erase(&state->rb_node, &tree->state);
450 free_extent_state(state);
455 merge_state(tree, state);
461 * clear some bits on a range in the tree. This may require splitting
462 * or inserting elements in the tree, so the gfp mask is used to
463 * indicate which allocations or sleeping are allowed.
465 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
466 * the given range from the tree regardless of state (ie for truncate).
468 * the range [start, end] is inclusive.
470 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
471 * bits were already set, or zero if none of the bits were already set.
473 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
474 int bits, int wake, int delete,
475 struct extent_state **cached_state,
478 struct extent_state *state;
479 struct extent_state *cached;
480 struct extent_state *prealloc = NULL;
481 struct rb_node *next_node;
482 struct rb_node *node;
488 if (!prealloc && (mask & __GFP_WAIT)) {
489 prealloc = alloc_extent_state(mask);
494 spin_lock(&tree->lock);
496 cached = *cached_state;
497 *cached_state = NULL;
499 if (cached && cached->tree && cached->start == start) {
500 atomic_dec(&cached->refs);
504 free_extent_state(cached);
507 * this search will find the extents that end after
510 node = tree_search(tree, start);
513 state = rb_entry(node, struct extent_state, rb_node);
515 if (state->start > end)
517 WARN_ON(state->end < start);
518 last_end = state->end;
521 * | ---- desired range ---- |
523 * | ------------- state -------------- |
525 * We need to split the extent we found, and may flip
526 * bits on second half.
528 * If the extent we found extends past our range, we
529 * just split and search again. It'll get split again
530 * the next time though.
532 * If the extent we found is inside our range, we clear
533 * the desired bit on it.
536 if (state->start < start) {
538 prealloc = alloc_extent_state(GFP_ATOMIC);
539 err = split_state(tree, state, prealloc, start);
540 BUG_ON(err == -EEXIST);
544 if (state->end <= end) {
545 set |= clear_state_bit(tree, state, bits,
547 if (last_end == (u64)-1)
549 start = last_end + 1;
554 * | ---- desired range ---- |
556 * We need to split the extent, and clear the bit
559 if (state->start <= end && state->end > end) {
561 prealloc = alloc_extent_state(GFP_ATOMIC);
562 err = split_state(tree, state, prealloc, end + 1);
563 BUG_ON(err == -EEXIST);
568 set |= clear_state_bit(tree, prealloc, bits,
574 if (state->end < end && prealloc && !need_resched())
575 next_node = rb_next(&state->rb_node);
579 set |= clear_state_bit(tree, state, bits, wake, delete);
580 if (last_end == (u64)-1)
582 start = last_end + 1;
583 if (start <= end && next_node) {
584 state = rb_entry(next_node, struct extent_state,
586 if (state->start == start)
592 spin_unlock(&tree->lock);
594 free_extent_state(prealloc);
601 spin_unlock(&tree->lock);
602 if (mask & __GFP_WAIT)
607 static int wait_on_state(struct extent_io_tree *tree,
608 struct extent_state *state)
609 __releases(tree->lock)
610 __acquires(tree->lock)
613 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
614 spin_unlock(&tree->lock);
616 spin_lock(&tree->lock);
617 finish_wait(&state->wq, &wait);
622 * waits for one or more bits to clear on a range in the state tree.
623 * The range [start, end] is inclusive.
624 * The tree lock is taken by this function
626 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
628 struct extent_state *state;
629 struct rb_node *node;
631 spin_lock(&tree->lock);
635 * this search will find all the extents that end after
638 node = tree_search(tree, start);
642 state = rb_entry(node, struct extent_state, rb_node);
644 if (state->start > end)
647 if (state->state & bits) {
648 start = state->start;
649 atomic_inc(&state->refs);
650 wait_on_state(tree, state);
651 free_extent_state(state);
654 start = state->end + 1;
659 if (need_resched()) {
660 spin_unlock(&tree->lock);
662 spin_lock(&tree->lock);
666 spin_unlock(&tree->lock);
670 static void set_state_bits(struct extent_io_tree *tree,
671 struct extent_state *state,
674 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
675 u64 range = state->end - state->start + 1;
676 tree->dirty_bytes += range;
678 set_state_cb(tree, state, bits);
679 state->state |= bits;
682 static void cache_state(struct extent_state *state,
683 struct extent_state **cached_ptr)
685 if (cached_ptr && !(*cached_ptr)) {
686 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
688 atomic_inc(&state->refs);
694 * set some bits on a range in the tree. This may require allocations or
695 * sleeping, so the gfp mask is used to indicate what is allowed.
697 * If any of the exclusive bits are set, this will fail with -EEXIST if some
698 * part of the range already has the desired bits set. The start of the
699 * existing range is returned in failed_start in this case.
701 * [start, end] is inclusive This takes the tree lock.
704 static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
705 int bits, int exclusive_bits, u64 *failed_start,
706 struct extent_state **cached_state,
709 struct extent_state *state;
710 struct extent_state *prealloc = NULL;
711 struct rb_node *node;
717 if (!prealloc && (mask & __GFP_WAIT)) {
718 prealloc = alloc_extent_state(mask);
723 spin_lock(&tree->lock);
724 if (cached_state && *cached_state) {
725 state = *cached_state;
726 if (state->start == start && state->tree) {
727 node = &state->rb_node;
732 * this search will find all the extents that end after
735 node = tree_search(tree, start);
737 err = insert_state(tree, prealloc, start, end, bits);
739 BUG_ON(err == -EEXIST);
742 state = rb_entry(node, struct extent_state, rb_node);
744 last_start = state->start;
745 last_end = state->end;
748 * | ---- desired range ---- |
751 * Just lock what we found and keep going
753 if (state->start == start && state->end <= end) {
754 struct rb_node *next_node;
755 if (state->state & exclusive_bits) {
756 *failed_start = state->start;
761 set_state_bits(tree, state, bits);
762 cache_state(state, cached_state);
763 merge_state(tree, state);
764 if (last_end == (u64)-1)
767 start = last_end + 1;
768 if (start < end && prealloc && !need_resched()) {
769 next_node = rb_next(node);
771 state = rb_entry(next_node, struct extent_state,
773 if (state->start == start)
781 * | ---- desired range ---- |
784 * | ------------- state -------------- |
786 * We need to split the extent we found, and may flip bits on
789 * If the extent we found extends past our
790 * range, we just split and search again. It'll get split
791 * again the next time though.
793 * If the extent we found is inside our range, we set the
796 if (state->start < start) {
797 if (state->state & exclusive_bits) {
798 *failed_start = start;
802 err = split_state(tree, state, prealloc, start);
803 BUG_ON(err == -EEXIST);
807 if (state->end <= end) {
808 set_state_bits(tree, state, bits);
809 cache_state(state, cached_state);
810 merge_state(tree, state);
811 if (last_end == (u64)-1)
813 start = last_end + 1;
818 * | ---- desired range ---- |
819 * | state | or | state |
821 * There's a hole, we need to insert something in it and
822 * ignore the extent we found.
824 if (state->start > start) {
826 if (end < last_start)
829 this_end = last_start - 1;
830 err = insert_state(tree, prealloc, start, this_end,
832 cache_state(prealloc, cached_state);
834 BUG_ON(err == -EEXIST);
837 start = this_end + 1;
841 * | ---- desired range ---- |
843 * We need to split the extent, and set the bit
846 if (state->start <= end && state->end > end) {
847 if (state->state & exclusive_bits) {
848 *failed_start = start;
852 err = split_state(tree, state, prealloc, end + 1);
853 BUG_ON(err == -EEXIST);
855 set_state_bits(tree, prealloc, bits);
856 cache_state(prealloc, cached_state);
857 merge_state(tree, prealloc);
865 spin_unlock(&tree->lock);
867 free_extent_state(prealloc);
874 spin_unlock(&tree->lock);
875 if (mask & __GFP_WAIT)
880 /* wrappers around set/clear extent bit */
881 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
884 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
888 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
889 int bits, gfp_t mask)
891 return set_extent_bit(tree, start, end, bits, 0, NULL,
895 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
896 int bits, gfp_t mask)
898 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
901 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
904 return set_extent_bit(tree, start, end,
905 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
906 0, NULL, NULL, mask);
909 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
912 return clear_extent_bit(tree, start, end,
913 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
917 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
920 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
924 static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
927 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
931 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
934 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
938 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
941 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
945 int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
947 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
951 * either insert or lock state struct between start and end use mask to tell
952 * us if waiting is desired.
954 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
955 int bits, struct extent_state **cached_state, gfp_t mask)
960 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
961 EXTENT_LOCKED, &failed_start,
963 if (err == -EEXIST && (mask & __GFP_WAIT)) {
964 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
965 start = failed_start;
969 WARN_ON(start > end);
974 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
976 return lock_extent_bits(tree, start, end, 0, NULL, mask);
979 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
985 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
986 &failed_start, NULL, mask);
987 if (err == -EEXIST) {
988 if (failed_start > start)
989 clear_extent_bit(tree, start, failed_start - 1,
990 EXTENT_LOCKED, 1, 0, NULL, mask);
996 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
997 struct extent_state **cached, gfp_t mask)
999 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1003 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1006 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1011 * helper function to set pages and extents in the tree dirty
1013 int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1015 unsigned long index = start >> PAGE_CACHE_SHIFT;
1016 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1019 while (index <= end_index) {
1020 page = find_get_page(tree->mapping, index);
1022 __set_page_dirty_nobuffers(page);
1023 page_cache_release(page);
1030 * helper function to set both pages and extents in the tree writeback
1032 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1034 unsigned long index = start >> PAGE_CACHE_SHIFT;
1035 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1038 while (index <= end_index) {
1039 page = find_get_page(tree->mapping, index);
1041 set_page_writeback(page);
1042 page_cache_release(page);
1049 * find the first offset in the io tree with 'bits' set. zero is
1050 * returned if we find something, and *start_ret and *end_ret are
1051 * set to reflect the state struct that was found.
1053 * If nothing was found, 1 is returned, < 0 on error
1055 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1056 u64 *start_ret, u64 *end_ret, int bits)
1058 struct rb_node *node;
1059 struct extent_state *state;
1062 spin_lock(&tree->lock);
1064 * this search will find all the extents that end after
1067 node = tree_search(tree, start);
1072 state = rb_entry(node, struct extent_state, rb_node);
1073 if (state->end >= start && (state->state & bits)) {
1074 *start_ret = state->start;
1075 *end_ret = state->end;
1079 node = rb_next(node);
1084 spin_unlock(&tree->lock);
1088 /* find the first state struct with 'bits' set after 'start', and
1089 * return it. tree->lock must be held. NULL will returned if
1090 * nothing was found after 'start'
1092 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1093 u64 start, int bits)
1095 struct rb_node *node;
1096 struct extent_state *state;
1099 * this search will find all the extents that end after
1102 node = tree_search(tree, start);
1107 state = rb_entry(node, struct extent_state, rb_node);
1108 if (state->end >= start && (state->state & bits))
1111 node = rb_next(node);
1120 * find a contiguous range of bytes in the file marked as delalloc, not
1121 * more than 'max_bytes'. start and end are used to return the range,
1123 * 1 is returned if we find something, 0 if nothing was in the tree
1125 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1126 u64 *start, u64 *end, u64 max_bytes)
1128 struct rb_node *node;
1129 struct extent_state *state;
1130 u64 cur_start = *start;
1132 u64 total_bytes = 0;
1134 spin_lock(&tree->lock);
1137 * this search will find all the extents that end after
1140 node = tree_search(tree, cur_start);
1148 state = rb_entry(node, struct extent_state, rb_node);
1149 if (found && (state->start != cur_start ||
1150 (state->state & EXTENT_BOUNDARY))) {
1153 if (!(state->state & EXTENT_DELALLOC)) {
1159 *start = state->start;
1162 cur_start = state->end + 1;
1163 node = rb_next(node);
1166 total_bytes += state->end - state->start + 1;
1167 if (total_bytes >= max_bytes)
1171 spin_unlock(&tree->lock);
1175 static noinline int __unlock_for_delalloc(struct inode *inode,
1176 struct page *locked_page,
1180 struct page *pages[16];
1181 unsigned long index = start >> PAGE_CACHE_SHIFT;
1182 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1183 unsigned long nr_pages = end_index - index + 1;
1186 if (index == locked_page->index && end_index == index)
1189 while (nr_pages > 0) {
1190 ret = find_get_pages_contig(inode->i_mapping, index,
1191 min_t(unsigned long, nr_pages,
1192 ARRAY_SIZE(pages)), pages);
1193 for (i = 0; i < ret; i++) {
1194 if (pages[i] != locked_page)
1195 unlock_page(pages[i]);
1196 page_cache_release(pages[i]);
1205 static noinline int lock_delalloc_pages(struct inode *inode,
1206 struct page *locked_page,
1210 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1211 unsigned long start_index = index;
1212 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1213 unsigned long pages_locked = 0;
1214 struct page *pages[16];
1215 unsigned long nrpages;
1219 /* the caller is responsible for locking the start index */
1220 if (index == locked_page->index && index == end_index)
1223 /* skip the page at the start index */
1224 nrpages = end_index - index + 1;
1225 while (nrpages > 0) {
1226 ret = find_get_pages_contig(inode->i_mapping, index,
1227 min_t(unsigned long,
1228 nrpages, ARRAY_SIZE(pages)), pages);
1233 /* now we have an array of pages, lock them all */
1234 for (i = 0; i < ret; i++) {
1236 * the caller is taking responsibility for
1239 if (pages[i] != locked_page) {
1240 lock_page(pages[i]);
1241 if (!PageDirty(pages[i]) ||
1242 pages[i]->mapping != inode->i_mapping) {
1244 unlock_page(pages[i]);
1245 page_cache_release(pages[i]);
1249 page_cache_release(pages[i]);
1258 if (ret && pages_locked) {
1259 __unlock_for_delalloc(inode, locked_page,
1261 ((u64)(start_index + pages_locked - 1)) <<
1268 * find a contiguous range of bytes in the file marked as delalloc, not
1269 * more than 'max_bytes'. start and end are used to return the range,
1271 * 1 is returned if we find something, 0 if nothing was in the tree
1273 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1274 struct extent_io_tree *tree,
1275 struct page *locked_page,
1276 u64 *start, u64 *end,
1282 struct extent_state *cached_state = NULL;
1287 /* step one, find a bunch of delalloc bytes starting at start */
1288 delalloc_start = *start;
1290 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1292 if (!found || delalloc_end <= *start) {
1293 *start = delalloc_start;
1294 *end = delalloc_end;
1299 * start comes from the offset of locked_page. We have to lock
1300 * pages in order, so we can't process delalloc bytes before
1303 if (delalloc_start < *start)
1304 delalloc_start = *start;
1307 * make sure to limit the number of pages we try to lock down
1310 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1311 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1313 /* step two, lock all the pages after the page that has start */
1314 ret = lock_delalloc_pages(inode, locked_page,
1315 delalloc_start, delalloc_end);
1316 if (ret == -EAGAIN) {
1317 /* some of the pages are gone, lets avoid looping by
1318 * shortening the size of the delalloc range we're searching
1320 free_extent_state(cached_state);
1322 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1323 max_bytes = PAGE_CACHE_SIZE - offset;
1333 /* step three, lock the state bits for the whole range */
1334 lock_extent_bits(tree, delalloc_start, delalloc_end,
1335 0, &cached_state, GFP_NOFS);
1337 /* then test to make sure it is all still delalloc */
1338 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1339 EXTENT_DELALLOC, 1, cached_state);
1341 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1342 &cached_state, GFP_NOFS);
1343 __unlock_for_delalloc(inode, locked_page,
1344 delalloc_start, delalloc_end);
1348 free_extent_state(cached_state);
1349 *start = delalloc_start;
1350 *end = delalloc_end;
1355 int extent_clear_unlock_delalloc(struct inode *inode,
1356 struct extent_io_tree *tree,
1357 u64 start, u64 end, struct page *locked_page,
1360 int clear_delalloc, int clear_dirty,
1366 struct page *pages[16];
1367 unsigned long index = start >> PAGE_CACHE_SHIFT;
1368 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1369 unsigned long nr_pages = end_index - index + 1;
1374 clear_bits |= EXTENT_LOCKED;
1376 clear_bits |= EXTENT_DIRTY;
1379 clear_bits |= EXTENT_DELALLOC;
1381 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1382 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback ||
1386 while (nr_pages > 0) {
1387 ret = find_get_pages_contig(inode->i_mapping, index,
1388 min_t(unsigned long,
1389 nr_pages, ARRAY_SIZE(pages)), pages);
1390 for (i = 0; i < ret; i++) {
1393 SetPagePrivate2(pages[i]);
1395 if (pages[i] == locked_page) {
1396 page_cache_release(pages[i]);
1400 clear_page_dirty_for_io(pages[i]);
1402 set_page_writeback(pages[i]);
1404 end_page_writeback(pages[i]);
1406 unlock_page(pages[i]);
1407 page_cache_release(pages[i]);
1417 * count the number of bytes in the tree that have a given bit(s)
1418 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1419 * cached. The total number found is returned.
1421 u64 count_range_bits(struct extent_io_tree *tree,
1422 u64 *start, u64 search_end, u64 max_bytes,
1425 struct rb_node *node;
1426 struct extent_state *state;
1427 u64 cur_start = *start;
1428 u64 total_bytes = 0;
1431 if (search_end <= cur_start) {
1436 spin_lock(&tree->lock);
1437 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1438 total_bytes = tree->dirty_bytes;
1442 * this search will find all the extents that end after
1445 node = tree_search(tree, cur_start);
1450 state = rb_entry(node, struct extent_state, rb_node);
1451 if (state->start > search_end)
1453 if (state->end >= cur_start && (state->state & bits)) {
1454 total_bytes += min(search_end, state->end) + 1 -
1455 max(cur_start, state->start);
1456 if (total_bytes >= max_bytes)
1459 *start = state->start;
1463 node = rb_next(node);
1468 spin_unlock(&tree->lock);
1473 * set the private field for a given byte offset in the tree. If there isn't
1474 * an extent_state there already, this does nothing.
1476 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1478 struct rb_node *node;
1479 struct extent_state *state;
1482 spin_lock(&tree->lock);
1484 * this search will find all the extents that end after
1487 node = tree_search(tree, start);
1492 state = rb_entry(node, struct extent_state, rb_node);
1493 if (state->start != start) {
1497 state->private = private;
1499 spin_unlock(&tree->lock);
1503 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1505 struct rb_node *node;
1506 struct extent_state *state;
1509 spin_lock(&tree->lock);
1511 * this search will find all the extents that end after
1514 node = tree_search(tree, start);
1519 state = rb_entry(node, struct extent_state, rb_node);
1520 if (state->start != start) {
1524 *private = state->private;
1526 spin_unlock(&tree->lock);
1531 * searches a range in the state tree for a given mask.
1532 * If 'filled' == 1, this returns 1 only if every extent in the tree
1533 * has the bits set. Otherwise, 1 is returned if any bit in the
1534 * range is found set.
1536 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1537 int bits, int filled, struct extent_state *cached)
1539 struct extent_state *state = NULL;
1540 struct rb_node *node;
1543 spin_lock(&tree->lock);
1544 if (cached && cached->tree && cached->start == start)
1545 node = &cached->rb_node;
1547 node = tree_search(tree, start);
1548 while (node && start <= end) {
1549 state = rb_entry(node, struct extent_state, rb_node);
1551 if (filled && state->start > start) {
1556 if (state->start > end)
1559 if (state->state & bits) {
1563 } else if (filled) {
1568 if (state->end == (u64)-1)
1571 start = state->end + 1;
1574 node = rb_next(node);
1581 spin_unlock(&tree->lock);
1586 * helper function to set a given page up to date if all the
1587 * extents in the tree for that page are up to date
1589 static int check_page_uptodate(struct extent_io_tree *tree,
1592 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1593 u64 end = start + PAGE_CACHE_SIZE - 1;
1594 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1595 SetPageUptodate(page);
1600 * helper function to unlock a page if all the extents in the tree
1601 * for that page are unlocked
1603 static int check_page_locked(struct extent_io_tree *tree,
1606 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1607 u64 end = start + PAGE_CACHE_SIZE - 1;
1608 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1614 * helper function to end page writeback if all the extents
1615 * in the tree for that page are done with writeback
1617 static int check_page_writeback(struct extent_io_tree *tree,
1620 end_page_writeback(page);
1624 /* lots and lots of room for performance fixes in the end_bio funcs */
1627 * after a writepage IO is done, we need to:
1628 * clear the uptodate bits on error
1629 * clear the writeback bits in the extent tree for this IO
1630 * end_page_writeback if the page has no more pending IO
1632 * Scheduling is not allowed, so the extent state tree is expected
1633 * to have one and only one object corresponding to this IO.
1635 static void end_bio_extent_writepage(struct bio *bio, int err)
1637 int uptodate = err == 0;
1638 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1639 struct extent_io_tree *tree;
1646 struct page *page = bvec->bv_page;
1647 tree = &BTRFS_I(page->mapping->host)->io_tree;
1649 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1651 end = start + bvec->bv_len - 1;
1653 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1658 if (--bvec >= bio->bi_io_vec)
1659 prefetchw(&bvec->bv_page->flags);
1660 if (tree->ops && tree->ops->writepage_end_io_hook) {
1661 ret = tree->ops->writepage_end_io_hook(page, start,
1662 end, NULL, uptodate);
1667 if (!uptodate && tree->ops &&
1668 tree->ops->writepage_io_failed_hook) {
1669 ret = tree->ops->writepage_io_failed_hook(bio, page,
1672 uptodate = (err == 0);
1678 clear_extent_uptodate(tree, start, end, GFP_NOFS);
1679 ClearPageUptodate(page);
1684 end_page_writeback(page);
1686 check_page_writeback(tree, page);
1687 } while (bvec >= bio->bi_io_vec);
1693 * after a readpage IO is done, we need to:
1694 * clear the uptodate bits on error
1695 * set the uptodate bits if things worked
1696 * set the page up to date if all extents in the tree are uptodate
1697 * clear the lock bit in the extent tree
1698 * unlock the page if there are no other extents locked for it
1700 * Scheduling is not allowed, so the extent state tree is expected
1701 * to have one and only one object corresponding to this IO.
1703 static void end_bio_extent_readpage(struct bio *bio, int err)
1705 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1706 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1707 struct extent_io_tree *tree;
1717 struct page *page = bvec->bv_page;
1718 tree = &BTRFS_I(page->mapping->host)->io_tree;
1720 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1722 end = start + bvec->bv_len - 1;
1724 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1729 if (--bvec >= bio->bi_io_vec)
1730 prefetchw(&bvec->bv_page->flags);
1732 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1733 ret = tree->ops->readpage_end_io_hook(page, start, end,
1738 if (!uptodate && tree->ops &&
1739 tree->ops->readpage_io_failed_hook) {
1740 ret = tree->ops->readpage_io_failed_hook(bio, page,
1744 test_bit(BIO_UPTODATE, &bio->bi_flags);
1752 set_extent_uptodate(tree, start, end,
1755 unlock_extent(tree, start, end, GFP_ATOMIC);
1759 SetPageUptodate(page);
1761 ClearPageUptodate(page);
1767 check_page_uptodate(tree, page);
1769 ClearPageUptodate(page);
1772 check_page_locked(tree, page);
1774 } while (bvec >= bio->bi_io_vec);
1780 * IO done from prepare_write is pretty simple, we just unlock
1781 * the structs in the extent tree when done, and set the uptodate bits
1784 static void end_bio_extent_preparewrite(struct bio *bio, int err)
1786 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1787 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1788 struct extent_io_tree *tree;
1793 struct page *page = bvec->bv_page;
1794 tree = &BTRFS_I(page->mapping->host)->io_tree;
1796 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1798 end = start + bvec->bv_len - 1;
1800 if (--bvec >= bio->bi_io_vec)
1801 prefetchw(&bvec->bv_page->flags);
1804 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1806 ClearPageUptodate(page);
1810 unlock_extent(tree, start, end, GFP_ATOMIC);
1812 } while (bvec >= bio->bi_io_vec);
1818 extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1823 bio = bio_alloc(gfp_flags, nr_vecs);
1825 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1826 while (!bio && (nr_vecs /= 2))
1827 bio = bio_alloc(gfp_flags, nr_vecs);
1832 bio->bi_bdev = bdev;
1833 bio->bi_sector = first_sector;
1838 static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1839 unsigned long bio_flags)
1842 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1843 struct page *page = bvec->bv_page;
1844 struct extent_io_tree *tree = bio->bi_private;
1848 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1849 end = start + bvec->bv_len - 1;
1851 bio->bi_private = NULL;
1855 if (tree->ops && tree->ops->submit_bio_hook)
1856 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1857 mirror_num, bio_flags);
1859 submit_bio(rw, bio);
1860 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1866 static int submit_extent_page(int rw, struct extent_io_tree *tree,
1867 struct page *page, sector_t sector,
1868 size_t size, unsigned long offset,
1869 struct block_device *bdev,
1870 struct bio **bio_ret,
1871 unsigned long max_pages,
1872 bio_end_io_t end_io_func,
1874 unsigned long prev_bio_flags,
1875 unsigned long bio_flags)
1881 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1882 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
1883 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
1885 if (bio_ret && *bio_ret) {
1888 contig = bio->bi_sector == sector;
1890 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1893 if (prev_bio_flags != bio_flags || !contig ||
1894 (tree->ops && tree->ops->merge_bio_hook &&
1895 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1897 bio_add_page(bio, page, page_size, offset) < page_size) {
1898 ret = submit_one_bio(rw, bio, mirror_num,
1905 if (this_compressed)
1908 nr = bio_get_nr_vecs(bdev);
1910 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1912 bio_add_page(bio, page, page_size, offset);
1913 bio->bi_end_io = end_io_func;
1914 bio->bi_private = tree;
1919 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
1924 void set_page_extent_mapped(struct page *page)
1926 if (!PagePrivate(page)) {
1927 SetPagePrivate(page);
1928 page_cache_get(page);
1929 set_page_private(page, EXTENT_PAGE_PRIVATE);
1933 static void set_page_extent_head(struct page *page, unsigned long len)
1935 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1939 * basic readpage implementation. Locked extent state structs are inserted
1940 * into the tree that are removed when the IO is done (by the end_io
1943 static int __extent_read_full_page(struct extent_io_tree *tree,
1945 get_extent_t *get_extent,
1946 struct bio **bio, int mirror_num,
1947 unsigned long *bio_flags)
1949 struct inode *inode = page->mapping->host;
1950 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1951 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1955 u64 last_byte = i_size_read(inode);
1959 struct extent_map *em;
1960 struct block_device *bdev;
1963 size_t page_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);
1972 lock_extent(tree, start, end, GFP_NOFS);
1974 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1976 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1979 iosize = PAGE_CACHE_SIZE - zero_offset;
1980 userpage = kmap_atomic(page, KM_USER0);
1981 memset(userpage + zero_offset, 0, iosize);
1982 flush_dcache_page(page);
1983 kunmap_atomic(userpage, KM_USER0);
1986 while (cur <= end) {
1987 if (cur >= last_byte) {
1989 iosize = PAGE_CACHE_SIZE - page_offset;
1990 userpage = kmap_atomic(page, KM_USER0);
1991 memset(userpage + page_offset, 0, iosize);
1992 flush_dcache_page(page);
1993 kunmap_atomic(userpage, KM_USER0);
1994 set_extent_uptodate(tree, cur, cur + iosize - 1,
1996 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1999 em = get_extent(inode, page, page_offset, cur,
2001 if (IS_ERR(em) || !em) {
2003 unlock_extent(tree, cur, end, GFP_NOFS);
2006 extent_offset = cur - em->start;
2007 BUG_ON(extent_map_end(em) <= cur);
2010 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2011 this_bio_flag = EXTENT_BIO_COMPRESSED;
2013 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2014 cur_end = min(extent_map_end(em) - 1, end);
2015 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2016 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2017 disk_io_size = em->block_len;
2018 sector = em->block_start >> 9;
2020 sector = (em->block_start + extent_offset) >> 9;
2021 disk_io_size = iosize;
2024 block_start = em->block_start;
2025 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2026 block_start = EXTENT_MAP_HOLE;
2027 free_extent_map(em);
2030 /* we've found a hole, just zero and go on */
2031 if (block_start == EXTENT_MAP_HOLE) {
2033 userpage = kmap_atomic(page, KM_USER0);
2034 memset(userpage + page_offset, 0, iosize);
2035 flush_dcache_page(page);
2036 kunmap_atomic(userpage, KM_USER0);
2038 set_extent_uptodate(tree, cur, cur + iosize - 1,
2040 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2042 page_offset += iosize;
2045 /* the get_extent function already copied into the page */
2046 if (test_range_bit(tree, cur, cur_end,
2047 EXTENT_UPTODATE, 1, NULL)) {
2048 check_page_uptodate(tree, page);
2049 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2051 page_offset += iosize;
2054 /* we have an inline extent but it didn't get marked up
2055 * to date. Error out
2057 if (block_start == EXTENT_MAP_INLINE) {
2059 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2061 page_offset += iosize;
2066 if (tree->ops && tree->ops->readpage_io_hook) {
2067 ret = tree->ops->readpage_io_hook(page, cur,
2071 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2073 ret = submit_extent_page(READ, tree, page,
2074 sector, disk_io_size, page_offset,
2076 end_bio_extent_readpage, mirror_num,
2080 *bio_flags = this_bio_flag;
2085 page_offset += iosize;
2088 if (!PageError(page))
2089 SetPageUptodate(page);
2095 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2096 get_extent_t *get_extent)
2098 struct bio *bio = NULL;
2099 unsigned long bio_flags = 0;
2102 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2105 submit_one_bio(READ, bio, 0, bio_flags);
2109 static noinline void update_nr_written(struct page *page,
2110 struct writeback_control *wbc,
2111 unsigned long nr_written)
2113 wbc->nr_to_write -= nr_written;
2114 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2115 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2116 page->mapping->writeback_index = page->index + nr_written;
2120 * the writepage semantics are similar to regular writepage. extent
2121 * records are inserted to lock ranges in the tree, and as dirty areas
2122 * are found, they are marked writeback. Then the lock bits are removed
2123 * and the end_io handler clears the writeback ranges
2125 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2128 struct inode *inode = page->mapping->host;
2129 struct extent_page_data *epd = data;
2130 struct extent_io_tree *tree = epd->tree;
2131 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2133 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2137 u64 last_byte = i_size_read(inode);
2142 struct extent_state *cached_state = NULL;
2143 struct extent_map *em;
2144 struct block_device *bdev;
2147 size_t pg_offset = 0;
2149 loff_t i_size = i_size_read(inode);
2150 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2156 unsigned long nr_written = 0;
2158 if (wbc->sync_mode == WB_SYNC_ALL)
2159 write_flags = WRITE_SYNC_PLUG;
2161 write_flags = WRITE;
2163 WARN_ON(!PageLocked(page));
2164 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2165 if (page->index > end_index ||
2166 (page->index == end_index && !pg_offset)) {
2167 page->mapping->a_ops->invalidatepage(page, 0);
2172 if (page->index == end_index) {
2175 userpage = kmap_atomic(page, KM_USER0);
2176 memset(userpage + pg_offset, 0,
2177 PAGE_CACHE_SIZE - pg_offset);
2178 kunmap_atomic(userpage, KM_USER0);
2179 flush_dcache_page(page);
2183 set_page_extent_mapped(page);
2185 delalloc_start = start;
2188 if (!epd->extent_locked) {
2189 u64 delalloc_to_write = 0;
2191 * make sure the wbc mapping index is at least updated
2194 update_nr_written(page, wbc, 0);
2196 while (delalloc_end < page_end) {
2197 nr_delalloc = find_lock_delalloc_range(inode, tree,
2202 if (nr_delalloc == 0) {
2203 delalloc_start = delalloc_end + 1;
2206 tree->ops->fill_delalloc(inode, page, delalloc_start,
2207 delalloc_end, &page_started,
2210 * delalloc_end is already one less than the total
2211 * length, so we don't subtract one from
2214 delalloc_to_write += (delalloc_end - delalloc_start +
2217 delalloc_start = delalloc_end + 1;
2219 if (wbc->nr_to_write < delalloc_to_write) {
2222 if (delalloc_to_write < thresh * 2)
2223 thresh = delalloc_to_write;
2224 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2228 /* did the fill delalloc function already unlock and start
2234 * we've unlocked the page, so we can't update
2235 * the mapping's writeback index, just update
2238 wbc->nr_to_write -= nr_written;
2242 if (tree->ops && tree->ops->writepage_start_hook) {
2243 ret = tree->ops->writepage_start_hook(page, start,
2245 if (ret == -EAGAIN) {
2246 redirty_page_for_writepage(wbc, page);
2247 update_nr_written(page, wbc, nr_written);
2255 * we don't want to touch the inode after unlocking the page,
2256 * so we update the mapping writeback index now
2258 update_nr_written(page, wbc, nr_written + 1);
2261 if (last_byte <= start) {
2262 if (tree->ops && tree->ops->writepage_end_io_hook)
2263 tree->ops->writepage_end_io_hook(page, start,
2265 unlock_start = page_end + 1;
2269 blocksize = inode->i_sb->s_blocksize;
2271 while (cur <= end) {
2272 if (cur >= last_byte) {
2273 if (tree->ops && tree->ops->writepage_end_io_hook)
2274 tree->ops->writepage_end_io_hook(page, cur,
2276 unlock_start = page_end + 1;
2279 em = epd->get_extent(inode, page, pg_offset, cur,
2281 if (IS_ERR(em) || !em) {
2286 extent_offset = cur - em->start;
2287 BUG_ON(extent_map_end(em) <= cur);
2289 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2290 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2291 sector = (em->block_start + extent_offset) >> 9;
2293 block_start = em->block_start;
2294 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2295 free_extent_map(em);
2299 * compressed and inline extents are written through other
2302 if (compressed || block_start == EXTENT_MAP_HOLE ||
2303 block_start == EXTENT_MAP_INLINE) {
2305 * end_io notification does not happen here for
2306 * compressed extents
2308 if (!compressed && tree->ops &&
2309 tree->ops->writepage_end_io_hook)
2310 tree->ops->writepage_end_io_hook(page, cur,
2313 else if (compressed) {
2314 /* we don't want to end_page_writeback on
2315 * a compressed extent. this happens
2322 pg_offset += iosize;
2326 /* leave this out until we have a page_mkwrite call */
2327 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2328 EXTENT_DIRTY, 0, NULL)) {
2330 pg_offset += iosize;
2334 if (tree->ops && tree->ops->writepage_io_hook) {
2335 ret = tree->ops->writepage_io_hook(page, cur,
2343 unsigned long max_nr = end_index + 1;
2345 set_range_writeback(tree, cur, cur + iosize - 1);
2346 if (!PageWriteback(page)) {
2347 printk(KERN_ERR "btrfs warning page %lu not "
2348 "writeback, cur %llu end %llu\n",
2349 page->index, (unsigned long long)cur,
2350 (unsigned long long)end);
2353 ret = submit_extent_page(write_flags, tree, page,
2354 sector, iosize, pg_offset,
2355 bdev, &epd->bio, max_nr,
2356 end_bio_extent_writepage,
2362 pg_offset += iosize;
2367 /* make sure the mapping tag for page dirty gets cleared */
2368 set_page_writeback(page);
2369 end_page_writeback(page);
2375 /* drop our reference on any cached states */
2376 free_extent_state(cached_state);
2381 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2382 * @mapping: address space structure to write
2383 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2384 * @writepage: function called for each page
2385 * @data: data passed to writepage function
2387 * If a page is already under I/O, write_cache_pages() skips it, even
2388 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2389 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2390 * and msync() need to guarantee that all the data which was dirty at the time
2391 * the call was made get new I/O started against them. If wbc->sync_mode is
2392 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2393 * existing IO to complete.
2395 static int extent_write_cache_pages(struct extent_io_tree *tree,
2396 struct address_space *mapping,
2397 struct writeback_control *wbc,
2398 writepage_t writepage, void *data,
2399 void (*flush_fn)(void *))
2403 int nr_to_write_done = 0;
2404 struct pagevec pvec;
2407 pgoff_t end; /* Inclusive */
2409 int range_whole = 0;
2411 pagevec_init(&pvec, 0);
2412 if (wbc->range_cyclic) {
2413 index = mapping->writeback_index; /* Start from prev offset */
2416 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2417 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2418 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2423 while (!done && !nr_to_write_done && (index <= end) &&
2424 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2425 PAGECACHE_TAG_DIRTY, min(end - index,
2426 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2430 for (i = 0; i < nr_pages; i++) {
2431 struct page *page = pvec.pages[i];
2434 * At this point we hold neither mapping->tree_lock nor
2435 * lock on the page itself: the page may be truncated or
2436 * invalidated (changing page->mapping to NULL), or even
2437 * swizzled back from swapper_space to tmpfs file
2440 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2441 tree->ops->write_cache_pages_lock_hook(page);
2445 if (unlikely(page->mapping != mapping)) {
2450 if (!wbc->range_cyclic && page->index > end) {
2456 if (wbc->sync_mode != WB_SYNC_NONE) {
2457 if (PageWriteback(page))
2459 wait_on_page_writeback(page);
2462 if (PageWriteback(page) ||
2463 !clear_page_dirty_for_io(page)) {
2468 ret = (*writepage)(page, wbc, data);
2470 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2478 * the filesystem may choose to bump up nr_to_write.
2479 * We have to make sure to honor the new nr_to_write
2482 nr_to_write_done = wbc->nr_to_write <= 0;
2484 pagevec_release(&pvec);
2487 if (!scanned && !done) {
2489 * We hit the last page and there is more work to be done: wrap
2490 * back to the start of the file
2499 static void flush_epd_write_bio(struct extent_page_data *epd)
2503 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2505 submit_one_bio(WRITE, epd->bio, 0, 0);
2510 static noinline void flush_write_bio(void *data)
2512 struct extent_page_data *epd = data;
2513 flush_epd_write_bio(epd);
2516 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2517 get_extent_t *get_extent,
2518 struct writeback_control *wbc)
2521 struct address_space *mapping = page->mapping;
2522 struct extent_page_data epd = {
2525 .get_extent = get_extent,
2527 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
2529 struct writeback_control wbc_writepages = {
2531 .sync_mode = wbc->sync_mode,
2532 .older_than_this = NULL,
2534 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2535 .range_end = (loff_t)-1,
2538 ret = __extent_writepage(page, wbc, &epd);
2540 extent_write_cache_pages(tree, mapping, &wbc_writepages,
2541 __extent_writepage, &epd, flush_write_bio);
2542 flush_epd_write_bio(&epd);
2546 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2547 u64 start, u64 end, get_extent_t *get_extent,
2551 struct address_space *mapping = inode->i_mapping;
2553 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2556 struct extent_page_data epd = {
2559 .get_extent = get_extent,
2561 .sync_io = mode == WB_SYNC_ALL,
2563 struct writeback_control wbc_writepages = {
2564 .bdi = inode->i_mapping->backing_dev_info,
2566 .older_than_this = NULL,
2567 .nr_to_write = nr_pages * 2,
2568 .range_start = start,
2569 .range_end = end + 1,
2572 while (start <= end) {
2573 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2574 if (clear_page_dirty_for_io(page))
2575 ret = __extent_writepage(page, &wbc_writepages, &epd);
2577 if (tree->ops && tree->ops->writepage_end_io_hook)
2578 tree->ops->writepage_end_io_hook(page, start,
2579 start + PAGE_CACHE_SIZE - 1,
2583 page_cache_release(page);
2584 start += PAGE_CACHE_SIZE;
2587 flush_epd_write_bio(&epd);
2591 int extent_writepages(struct extent_io_tree *tree,
2592 struct address_space *mapping,
2593 get_extent_t *get_extent,
2594 struct writeback_control *wbc)
2597 struct extent_page_data epd = {
2600 .get_extent = get_extent,
2602 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
2605 ret = extent_write_cache_pages(tree, mapping, wbc,
2606 __extent_writepage, &epd,
2608 flush_epd_write_bio(&epd);
2612 int extent_readpages(struct extent_io_tree *tree,
2613 struct address_space *mapping,
2614 struct list_head *pages, unsigned nr_pages,
2615 get_extent_t get_extent)
2617 struct bio *bio = NULL;
2619 struct pagevec pvec;
2620 unsigned long bio_flags = 0;
2622 pagevec_init(&pvec, 0);
2623 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2624 struct page *page = list_entry(pages->prev, struct page, lru);
2626 prefetchw(&page->flags);
2627 list_del(&page->lru);
2629 * what we want to do here is call add_to_page_cache_lru,
2630 * but that isn't exported, so we reproduce it here
2632 if (!add_to_page_cache(page, mapping,
2633 page->index, GFP_KERNEL)) {
2635 /* open coding of lru_cache_add, also not exported */
2636 page_cache_get(page);
2637 if (!pagevec_add(&pvec, page))
2638 __pagevec_lru_add_file(&pvec);
2639 __extent_read_full_page(tree, page, get_extent,
2640 &bio, 0, &bio_flags);
2642 page_cache_release(page);
2644 if (pagevec_count(&pvec))
2645 __pagevec_lru_add_file(&pvec);
2646 BUG_ON(!list_empty(pages));
2648 submit_one_bio(READ, bio, 0, bio_flags);
2653 * basic invalidatepage code, this waits on any locked or writeback
2654 * ranges corresponding to the page, and then deletes any extent state
2655 * records from the tree
2657 int extent_invalidatepage(struct extent_io_tree *tree,
2658 struct page *page, unsigned long offset)
2660 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2661 u64 end = start + PAGE_CACHE_SIZE - 1;
2662 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2664 start += (offset + blocksize - 1) & ~(blocksize - 1);
2668 lock_extent(tree, start, end, GFP_NOFS);
2669 wait_on_page_writeback(page);
2670 clear_extent_bit(tree, start, end,
2671 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2672 1, 1, NULL, GFP_NOFS);
2677 * simple commit_write call, set_range_dirty is used to mark both
2678 * the pages and the extent records as dirty
2680 int extent_commit_write(struct extent_io_tree *tree,
2681 struct inode *inode, struct page *page,
2682 unsigned from, unsigned to)
2684 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2686 set_page_extent_mapped(page);
2687 set_page_dirty(page);
2689 if (pos > inode->i_size) {
2690 i_size_write(inode, pos);
2691 mark_inode_dirty(inode);
2696 int extent_prepare_write(struct extent_io_tree *tree,
2697 struct inode *inode, struct page *page,
2698 unsigned from, unsigned to, get_extent_t *get_extent)
2700 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2701 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2703 u64 orig_block_start;
2706 struct extent_map *em;
2707 unsigned blocksize = 1 << inode->i_blkbits;
2708 size_t page_offset = 0;
2709 size_t block_off_start;
2710 size_t block_off_end;
2716 set_page_extent_mapped(page);
2718 block_start = (page_start + from) & ~((u64)blocksize - 1);
2719 block_end = (page_start + to - 1) | (blocksize - 1);
2720 orig_block_start = block_start;
2722 lock_extent(tree, page_start, page_end, GFP_NOFS);
2723 while (block_start <= block_end) {
2724 em = get_extent(inode, page, page_offset, block_start,
2725 block_end - block_start + 1, 1);
2726 if (IS_ERR(em) || !em)
2729 cur_end = min(block_end, extent_map_end(em) - 1);
2730 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2731 block_off_end = block_off_start + blocksize;
2732 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2734 if (!PageUptodate(page) && isnew &&
2735 (block_off_end > to || block_off_start < from)) {
2738 kaddr = kmap_atomic(page, KM_USER0);
2739 if (block_off_end > to)
2740 memset(kaddr + to, 0, block_off_end - to);
2741 if (block_off_start < from)
2742 memset(kaddr + block_off_start, 0,
2743 from - block_off_start);
2744 flush_dcache_page(page);
2745 kunmap_atomic(kaddr, KM_USER0);
2747 if ((em->block_start != EXTENT_MAP_HOLE &&
2748 em->block_start != EXTENT_MAP_INLINE) &&
2749 !isnew && !PageUptodate(page) &&
2750 (block_off_end > to || block_off_start < from) &&
2751 !test_range_bit(tree, block_start, cur_end,
2752 EXTENT_UPTODATE, 1, NULL)) {
2754 u64 extent_offset = block_start - em->start;
2756 sector = (em->block_start + extent_offset) >> 9;
2757 iosize = (cur_end - block_start + blocksize) &
2758 ~((u64)blocksize - 1);
2760 * we've already got the extent locked, but we
2761 * need to split the state such that our end_bio
2762 * handler can clear the lock.
2764 set_extent_bit(tree, block_start,
2765 block_start + iosize - 1,
2766 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
2767 ret = submit_extent_page(READ, tree, page,
2768 sector, iosize, page_offset, em->bdev,
2770 end_bio_extent_preparewrite, 0,
2773 block_start = block_start + iosize;
2775 set_extent_uptodate(tree, block_start, cur_end,
2777 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2778 block_start = cur_end + 1;
2780 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2781 free_extent_map(em);
2784 wait_extent_bit(tree, orig_block_start,
2785 block_end, EXTENT_LOCKED);
2787 check_page_uptodate(tree, page);
2789 /* FIXME, zero out newly allocated blocks on error */
2794 * a helper for releasepage, this tests for areas of the page that
2795 * are locked or under IO and drops the related state bits if it is safe
2798 int try_release_extent_state(struct extent_map_tree *map,
2799 struct extent_io_tree *tree, struct page *page,
2802 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2803 u64 end = start + PAGE_CACHE_SIZE - 1;
2806 if (test_range_bit(tree, start, end,
2807 EXTENT_IOBITS, 0, NULL))
2810 if ((mask & GFP_NOFS) == GFP_NOFS)
2812 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2819 * a helper for releasepage. As long as there are no locked extents
2820 * in the range corresponding to the page, both state records and extent
2821 * map records are removed
2823 int try_release_extent_mapping(struct extent_map_tree *map,
2824 struct extent_io_tree *tree, struct page *page,
2827 struct extent_map *em;
2828 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2829 u64 end = start + PAGE_CACHE_SIZE - 1;
2831 if ((mask & __GFP_WAIT) &&
2832 page->mapping->host->i_size > 16 * 1024 * 1024) {
2834 while (start <= end) {
2835 len = end - start + 1;
2836 write_lock(&map->lock);
2837 em = lookup_extent_mapping(map, start, len);
2838 if (!em || IS_ERR(em)) {
2839 write_unlock(&map->lock);
2842 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2843 em->start != start) {
2844 write_unlock(&map->lock);
2845 free_extent_map(em);
2848 if (!test_range_bit(tree, em->start,
2849 extent_map_end(em) - 1,
2850 EXTENT_LOCKED | EXTENT_WRITEBACK,
2852 remove_extent_mapping(map, em);
2853 /* once for the rb tree */
2854 free_extent_map(em);
2856 start = extent_map_end(em);
2857 write_unlock(&map->lock);
2860 free_extent_map(em);
2863 return try_release_extent_state(map, tree, page, mask);
2866 sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2867 get_extent_t *get_extent)
2869 struct inode *inode = mapping->host;
2870 u64 start = iblock << inode->i_blkbits;
2871 sector_t sector = 0;
2872 size_t blksize = (1 << inode->i_blkbits);
2873 struct extent_map *em;
2875 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2877 em = get_extent(inode, NULL, 0, start, blksize, 0);
2878 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2880 if (!em || IS_ERR(em))
2883 if (em->block_start > EXTENT_MAP_LAST_BYTE)
2886 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2888 free_extent_map(em);
2892 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2893 __u64 start, __u64 len, get_extent_t *get_extent)
2897 u64 max = start + len;
2900 struct extent_map *em = NULL;
2902 u64 em_start = 0, em_len = 0;
2903 unsigned long emflags;
2909 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2911 em = get_extent(inode, NULL, 0, off, max - off, 0);
2919 off = em->start + em->len;
2923 em_start = em->start;
2929 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
2931 flags |= FIEMAP_EXTENT_LAST;
2932 } else if (em->block_start == EXTENT_MAP_HOLE) {
2933 flags |= FIEMAP_EXTENT_UNWRITTEN;
2934 } else if (em->block_start == EXTENT_MAP_INLINE) {
2935 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2936 FIEMAP_EXTENT_NOT_ALIGNED);
2937 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
2938 flags |= (FIEMAP_EXTENT_DELALLOC |
2939 FIEMAP_EXTENT_UNKNOWN);
2941 disko = em->block_start;
2943 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2944 flags |= FIEMAP_EXTENT_ENCODED;
2946 emflags = em->flags;
2947 free_extent_map(em);
2951 em = get_extent(inode, NULL, 0, off, max - off, 0);
2958 emflags = em->flags;
2960 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
2961 flags |= FIEMAP_EXTENT_LAST;
2965 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2971 free_extent_map(em);
2973 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2978 static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2982 struct address_space *mapping;
2985 return eb->first_page;
2986 i += eb->start >> PAGE_CACHE_SHIFT;
2987 mapping = eb->first_page->mapping;
2992 * extent_buffer_page is only called after pinning the page
2993 * by increasing the reference count. So we know the page must
2994 * be in the radix tree.
2997 p = radix_tree_lookup(&mapping->page_tree, i);
3003 static inline unsigned long num_extent_pages(u64 start, u64 len)
3005 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3006 (start >> PAGE_CACHE_SHIFT);
3009 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3014 struct extent_buffer *eb = NULL;
3016 unsigned long flags;
3019 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
3022 spin_lock_init(&eb->lock);
3023 init_waitqueue_head(&eb->lock_wq);
3026 spin_lock_irqsave(&leak_lock, flags);
3027 list_add(&eb->leak_list, &buffers);
3028 spin_unlock_irqrestore(&leak_lock, flags);
3030 atomic_set(&eb->refs, 1);
3035 static void __free_extent_buffer(struct extent_buffer *eb)
3038 unsigned long flags;
3039 spin_lock_irqsave(&leak_lock, flags);
3040 list_del(&eb->leak_list);
3041 spin_unlock_irqrestore(&leak_lock, flags);
3043 kmem_cache_free(extent_buffer_cache, eb);
3046 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3047 u64 start, unsigned long len,
3051 unsigned long num_pages = num_extent_pages(start, len);
3053 unsigned long index = start >> PAGE_CACHE_SHIFT;
3054 struct extent_buffer *eb;
3055 struct extent_buffer *exists = NULL;
3057 struct address_space *mapping = tree->mapping;
3060 spin_lock(&tree->buffer_lock);
3061 eb = buffer_search(tree, start);
3063 atomic_inc(&eb->refs);
3064 spin_unlock(&tree->buffer_lock);
3065 mark_page_accessed(eb->first_page);
3068 spin_unlock(&tree->buffer_lock);
3070 eb = __alloc_extent_buffer(tree, start, len, mask);
3075 eb->first_page = page0;
3078 page_cache_get(page0);
3079 mark_page_accessed(page0);
3080 set_page_extent_mapped(page0);
3081 set_page_extent_head(page0, len);
3082 uptodate = PageUptodate(page0);
3086 for (; i < num_pages; i++, index++) {
3087 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3092 set_page_extent_mapped(p);
3093 mark_page_accessed(p);
3096 set_page_extent_head(p, len);
3098 set_page_private(p, EXTENT_PAGE_PRIVATE);
3100 if (!PageUptodate(p))
3105 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3107 spin_lock(&tree->buffer_lock);
3108 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3110 /* add one reference for the caller */
3111 atomic_inc(&exists->refs);
3112 spin_unlock(&tree->buffer_lock);
3115 spin_unlock(&tree->buffer_lock);
3117 /* add one reference for the tree */
3118 atomic_inc(&eb->refs);
3122 if (!atomic_dec_and_test(&eb->refs))
3124 for (index = 1; index < i; index++)
3125 page_cache_release(extent_buffer_page(eb, index));
3126 page_cache_release(extent_buffer_page(eb, 0));
3127 __free_extent_buffer(eb);
3131 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3132 u64 start, unsigned long len,
3135 struct extent_buffer *eb;
3137 spin_lock(&tree->buffer_lock);
3138 eb = buffer_search(tree, start);
3140 atomic_inc(&eb->refs);
3141 spin_unlock(&tree->buffer_lock);
3144 mark_page_accessed(eb->first_page);
3149 void free_extent_buffer(struct extent_buffer *eb)
3154 if (!atomic_dec_and_test(&eb->refs))
3160 int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3161 struct extent_buffer *eb)
3164 unsigned long num_pages;
3167 num_pages = num_extent_pages(eb->start, eb->len);
3169 for (i = 0; i < num_pages; i++) {
3170 page = extent_buffer_page(eb, i);
3171 if (!PageDirty(page))
3176 set_page_extent_head(page, eb->len);
3178 set_page_private(page, EXTENT_PAGE_PRIVATE);
3180 clear_page_dirty_for_io(page);
3181 spin_lock_irq(&page->mapping->tree_lock);
3182 if (!PageDirty(page)) {
3183 radix_tree_tag_clear(&page->mapping->page_tree,
3185 PAGECACHE_TAG_DIRTY);
3187 spin_unlock_irq(&page->mapping->tree_lock);
3193 int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3194 struct extent_buffer *eb)
3196 return wait_on_extent_writeback(tree, eb->start,
3197 eb->start + eb->len - 1);
3200 int set_extent_buffer_dirty(struct extent_io_tree *tree,
3201 struct extent_buffer *eb)
3204 unsigned long num_pages;
3207 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3208 num_pages = num_extent_pages(eb->start, eb->len);
3209 for (i = 0; i < num_pages; i++)
3210 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
3214 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3215 struct extent_buffer *eb)
3219 unsigned long num_pages;
3221 num_pages = num_extent_pages(eb->start, eb->len);
3222 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3224 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3226 for (i = 0; i < num_pages; i++) {
3227 page = extent_buffer_page(eb, i);
3229 ClearPageUptodate(page);
3234 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3235 struct extent_buffer *eb)
3239 unsigned long num_pages;
3241 num_pages = num_extent_pages(eb->start, eb->len);
3243 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3245 for (i = 0; i < num_pages; i++) {
3246 page = extent_buffer_page(eb, i);
3247 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3248 ((i == num_pages - 1) &&
3249 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3250 check_page_uptodate(tree, page);
3253 SetPageUptodate(page);
3258 int extent_range_uptodate(struct extent_io_tree *tree,
3263 int pg_uptodate = 1;
3265 unsigned long index;
3267 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
3270 while (start <= end) {
3271 index = start >> PAGE_CACHE_SHIFT;
3272 page = find_get_page(tree->mapping, index);
3273 uptodate = PageUptodate(page);
3274 page_cache_release(page);
3279 start += PAGE_CACHE_SIZE;
3284 int extent_buffer_uptodate(struct extent_io_tree *tree,
3285 struct extent_buffer *eb)
3288 unsigned long num_pages;
3291 int pg_uptodate = 1;
3293 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3296 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3297 EXTENT_UPTODATE, 1, NULL);
3301 num_pages = num_extent_pages(eb->start, eb->len);
3302 for (i = 0; i < num_pages; i++) {
3303 page = extent_buffer_page(eb, i);
3304 if (!PageUptodate(page)) {
3312 int read_extent_buffer_pages(struct extent_io_tree *tree,
3313 struct extent_buffer *eb,
3314 u64 start, int wait,
3315 get_extent_t *get_extent, int mirror_num)
3318 unsigned long start_i;
3322 int locked_pages = 0;
3323 int all_uptodate = 1;
3324 int inc_all_pages = 0;
3325 unsigned long num_pages;
3326 struct bio *bio = NULL;
3327 unsigned long bio_flags = 0;
3329 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3332 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3333 EXTENT_UPTODATE, 1, NULL)) {
3338 WARN_ON(start < eb->start);
3339 start_i = (start >> PAGE_CACHE_SHIFT) -
3340 (eb->start >> PAGE_CACHE_SHIFT);
3345 num_pages = num_extent_pages(eb->start, eb->len);
3346 for (i = start_i; i < num_pages; i++) {
3347 page = extent_buffer_page(eb, i);
3349 if (!trylock_page(page))
3355 if (!PageUptodate(page))
3360 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3364 for (i = start_i; i < num_pages; i++) {
3365 page = extent_buffer_page(eb, i);
3367 page_cache_get(page);
3368 if (!PageUptodate(page)) {
3371 ClearPageError(page);
3372 err = __extent_read_full_page(tree, page,
3374 mirror_num, &bio_flags);
3383 submit_one_bio(READ, bio, mirror_num, bio_flags);
3388 for (i = start_i; i < num_pages; i++) {
3389 page = extent_buffer_page(eb, i);
3390 wait_on_page_locked(page);
3391 if (!PageUptodate(page))
3396 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3401 while (locked_pages > 0) {
3402 page = extent_buffer_page(eb, i);
3410 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3411 unsigned long start,
3418 char *dst = (char *)dstv;
3419 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3420 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3422 WARN_ON(start > eb->len);
3423 WARN_ON(start + len > eb->start + eb->len);
3425 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3428 page = extent_buffer_page(eb, i);
3430 cur = min(len, (PAGE_CACHE_SIZE - offset));
3431 kaddr = kmap_atomic(page, KM_USER1);
3432 memcpy(dst, kaddr + offset, cur);
3433 kunmap_atomic(kaddr, KM_USER1);
3442 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3443 unsigned long min_len, char **token, char **map,
3444 unsigned long *map_start,
3445 unsigned long *map_len, int km)
3447 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3450 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3451 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3452 unsigned long end_i = (start_offset + start + min_len - 1) >>
3459 offset = start_offset;
3463 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3466 if (start + min_len > eb->len) {
3467 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3468 "wanted %lu %lu\n", (unsigned long long)eb->start,
3469 eb->len, start, min_len);
3473 p = extent_buffer_page(eb, i);
3474 kaddr = kmap_atomic(p, km);
3476 *map = kaddr + offset;
3477 *map_len = PAGE_CACHE_SIZE - offset;
3481 int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3482 unsigned long min_len,
3483 char **token, char **map,
3484 unsigned long *map_start,
3485 unsigned long *map_len, int km)
3489 if (eb->map_token) {
3490 unmap_extent_buffer(eb, eb->map_token, km);
3491 eb->map_token = NULL;
3494 err = map_private_extent_buffer(eb, start, min_len, token, map,
3495 map_start, map_len, km);
3497 eb->map_token = *token;
3499 eb->map_start = *map_start;
3500 eb->map_len = *map_len;
3505 void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3507 kunmap_atomic(token, km);
3510 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3511 unsigned long start,
3518 char *ptr = (char *)ptrv;
3519 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3520 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3523 WARN_ON(start > eb->len);
3524 WARN_ON(start + len > eb->start + eb->len);
3526 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3529 page = extent_buffer_page(eb, i);
3531 cur = min(len, (PAGE_CACHE_SIZE - offset));
3533 kaddr = kmap_atomic(page, KM_USER0);
3534 ret = memcmp(ptr, kaddr + offset, cur);
3535 kunmap_atomic(kaddr, KM_USER0);
3547 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3548 unsigned long start, unsigned long len)
3554 char *src = (char *)srcv;
3555 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3556 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3558 WARN_ON(start > eb->len);
3559 WARN_ON(start + len > eb->start + eb->len);
3561 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3564 page = extent_buffer_page(eb, i);
3565 WARN_ON(!PageUptodate(page));
3567 cur = min(len, PAGE_CACHE_SIZE - offset);
3568 kaddr = kmap_atomic(page, KM_USER1);
3569 memcpy(kaddr + offset, src, cur);
3570 kunmap_atomic(kaddr, KM_USER1);
3579 void memset_extent_buffer(struct extent_buffer *eb, char c,
3580 unsigned long start, unsigned long len)
3586 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3587 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3589 WARN_ON(start > eb->len);
3590 WARN_ON(start + len > eb->start + eb->len);
3592 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3595 page = extent_buffer_page(eb, i);
3596 WARN_ON(!PageUptodate(page));
3598 cur = min(len, PAGE_CACHE_SIZE - offset);
3599 kaddr = kmap_atomic(page, KM_USER0);
3600 memset(kaddr + offset, c, cur);
3601 kunmap_atomic(kaddr, KM_USER0);
3609 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3610 unsigned long dst_offset, unsigned long src_offset,
3613 u64 dst_len = dst->len;
3618 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3619 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3621 WARN_ON(src->len != dst_len);
3623 offset = (start_offset + dst_offset) &
3624 ((unsigned long)PAGE_CACHE_SIZE - 1);
3627 page = extent_buffer_page(dst, i);
3628 WARN_ON(!PageUptodate(page));
3630 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3632 kaddr = kmap_atomic(page, KM_USER0);
3633 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3634 kunmap_atomic(kaddr, KM_USER0);
3643 static void move_pages(struct page *dst_page, struct page *src_page,
3644 unsigned long dst_off, unsigned long src_off,
3647 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3648 if (dst_page == src_page) {
3649 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3651 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3652 char *p = dst_kaddr + dst_off + len;
3653 char *s = src_kaddr + src_off + len;
3658 kunmap_atomic(src_kaddr, KM_USER1);
3660 kunmap_atomic(dst_kaddr, KM_USER0);
3663 static void copy_pages(struct page *dst_page, struct page *src_page,
3664 unsigned long dst_off, unsigned long src_off,
3667 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3670 if (dst_page != src_page)
3671 src_kaddr = kmap_atomic(src_page, KM_USER1);
3673 src_kaddr = dst_kaddr;
3675 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3676 kunmap_atomic(dst_kaddr, KM_USER0);
3677 if (dst_page != src_page)
3678 kunmap_atomic(src_kaddr, KM_USER1);
3681 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3682 unsigned long src_offset, unsigned long len)
3685 size_t dst_off_in_page;
3686 size_t src_off_in_page;
3687 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3688 unsigned long dst_i;
3689 unsigned long src_i;
3691 if (src_offset + len > dst->len) {
3692 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3693 "len %lu dst len %lu\n", src_offset, len, dst->len);
3696 if (dst_offset + len > dst->len) {
3697 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3698 "len %lu dst len %lu\n", dst_offset, len, dst->len);
3703 dst_off_in_page = (start_offset + dst_offset) &
3704 ((unsigned long)PAGE_CACHE_SIZE - 1);
3705 src_off_in_page = (start_offset + src_offset) &
3706 ((unsigned long)PAGE_CACHE_SIZE - 1);
3708 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3709 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3711 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3713 cur = min_t(unsigned long, cur,
3714 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3716 copy_pages(extent_buffer_page(dst, dst_i),
3717 extent_buffer_page(dst, src_i),
3718 dst_off_in_page, src_off_in_page, cur);
3726 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3727 unsigned long src_offset, unsigned long len)
3730 size_t dst_off_in_page;
3731 size_t src_off_in_page;
3732 unsigned long dst_end = dst_offset + len - 1;
3733 unsigned long src_end = src_offset + len - 1;
3734 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3735 unsigned long dst_i;
3736 unsigned long src_i;
3738 if (src_offset + len > dst->len) {
3739 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3740 "len %lu len %lu\n", src_offset, len, dst->len);
3743 if (dst_offset + len > dst->len) {
3744 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3745 "len %lu len %lu\n", dst_offset, len, dst->len);
3748 if (dst_offset < src_offset) {
3749 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3753 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3754 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3756 dst_off_in_page = (start_offset + dst_end) &
3757 ((unsigned long)PAGE_CACHE_SIZE - 1);
3758 src_off_in_page = (start_offset + src_end) &
3759 ((unsigned long)PAGE_CACHE_SIZE - 1);
3761 cur = min_t(unsigned long, len, src_off_in_page + 1);
3762 cur = min(cur, dst_off_in_page + 1);
3763 move_pages(extent_buffer_page(dst, dst_i),
3764 extent_buffer_page(dst, src_i),
3765 dst_off_in_page - cur + 1,
3766 src_off_in_page - cur + 1, cur);
3774 int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3776 u64 start = page_offset(page);
3777 struct extent_buffer *eb;
3780 unsigned long num_pages;
3782 spin_lock(&tree->buffer_lock);
3783 eb = buffer_search(tree, start);
3787 if (atomic_read(&eb->refs) > 1) {
3791 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3795 /* at this point we can safely release the extent buffer */
3796 num_pages = num_extent_pages(eb->start, eb->len);
3797 for (i = 0; i < num_pages; i++)
3798 page_cache_release(extent_buffer_page(eb, i));
3799 rb_erase(&eb->rb_node, &tree->buffer);
3800 __free_extent_buffer(eb);
3802 spin_unlock(&tree->buffer_lock);