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 /* temporary define until extent_map moves out of btrfs */
21 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
22 unsigned long extra_flags,
23 void (*ctor)(void *, struct kmem_cache *,
26 static struct kmem_cache *extent_state_cache;
27 static struct kmem_cache *extent_buffer_cache;
29 static LIST_HEAD(buffers);
30 static LIST_HEAD(states);
34 static DEFINE_SPINLOCK(leak_lock);
37 #define BUFFER_LRU_MAX 64
42 struct rb_node rb_node;
45 struct extent_page_data {
47 struct extent_io_tree *tree;
48 get_extent_t *get_extent;
50 /* tells writepage not to lock the state bits for this range
51 * it still does the unlocking
56 int __init extent_io_init(void)
58 extent_state_cache = btrfs_cache_create("extent_state",
59 sizeof(struct extent_state), 0,
61 if (!extent_state_cache)
64 extent_buffer_cache = btrfs_cache_create("extent_buffers",
65 sizeof(struct extent_buffer), 0,
67 if (!extent_buffer_cache)
68 goto free_state_cache;
72 kmem_cache_destroy(extent_state_cache);
76 void extent_io_exit(void)
78 struct extent_state *state;
79 struct extent_buffer *eb;
81 while (!list_empty(&states)) {
82 state = list_entry(states.next, struct extent_state, leak_list);
83 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
84 "state %lu in tree %p refs %d\n",
85 (unsigned long long)state->start,
86 (unsigned long long)state->end,
87 state->state, state->tree, atomic_read(&state->refs));
88 list_del(&state->leak_list);
89 kmem_cache_free(extent_state_cache, state);
93 while (!list_empty(&buffers)) {
94 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
95 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
96 "refs %d\n", (unsigned long long)eb->start,
97 eb->len, atomic_read(&eb->refs));
98 list_del(&eb->leak_list);
99 kmem_cache_free(extent_buffer_cache, eb);
101 if (extent_state_cache)
102 kmem_cache_destroy(extent_state_cache);
103 if (extent_buffer_cache)
104 kmem_cache_destroy(extent_buffer_cache);
107 void extent_io_tree_init(struct extent_io_tree *tree,
108 struct address_space *mapping, gfp_t mask)
110 tree->state.rb_node = NULL;
111 tree->buffer.rb_node = NULL;
113 tree->dirty_bytes = 0;
114 spin_lock_init(&tree->lock);
115 spin_lock_init(&tree->buffer_lock);
116 tree->mapping = mapping;
119 static struct extent_state *alloc_extent_state(gfp_t mask)
121 struct extent_state *state;
126 state = kmem_cache_alloc(extent_state_cache, mask);
133 spin_lock_irqsave(&leak_lock, flags);
134 list_add(&state->leak_list, &states);
135 spin_unlock_irqrestore(&leak_lock, flags);
137 atomic_set(&state->refs, 1);
138 init_waitqueue_head(&state->wq);
142 static void free_extent_state(struct extent_state *state)
146 if (atomic_dec_and_test(&state->refs)) {
150 WARN_ON(state->tree);
152 spin_lock_irqsave(&leak_lock, flags);
153 list_del(&state->leak_list);
154 spin_unlock_irqrestore(&leak_lock, flags);
156 kmem_cache_free(extent_state_cache, state);
160 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
161 struct rb_node *node)
163 struct rb_node **p = &root->rb_node;
164 struct rb_node *parent = NULL;
165 struct tree_entry *entry;
169 entry = rb_entry(parent, struct tree_entry, rb_node);
171 if (offset < entry->start)
173 else if (offset > entry->end)
179 entry = rb_entry(node, struct tree_entry, rb_node);
180 rb_link_node(node, parent, p);
181 rb_insert_color(node, root);
185 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
186 struct rb_node **prev_ret,
187 struct rb_node **next_ret)
189 struct rb_root *root = &tree->state;
190 struct rb_node *n = root->rb_node;
191 struct rb_node *prev = NULL;
192 struct rb_node *orig_prev = NULL;
193 struct tree_entry *entry;
194 struct tree_entry *prev_entry = NULL;
197 entry = rb_entry(n, struct tree_entry, rb_node);
201 if (offset < entry->start)
203 else if (offset > entry->end)
211 while (prev && offset > prev_entry->end) {
212 prev = rb_next(prev);
213 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
221 while (prev && offset < prev_entry->start) {
222 prev = rb_prev(prev);
223 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
230 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
233 struct rb_node *prev = NULL;
236 ret = __etree_search(tree, offset, &prev, NULL);
242 static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
243 u64 offset, struct rb_node *node)
245 struct rb_root *root = &tree->buffer;
246 struct rb_node **p = &root->rb_node;
247 struct rb_node *parent = NULL;
248 struct extent_buffer *eb;
252 eb = rb_entry(parent, struct extent_buffer, rb_node);
254 if (offset < eb->start)
256 else if (offset > eb->start)
262 rb_link_node(node, parent, p);
263 rb_insert_color(node, root);
267 static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
270 struct rb_root *root = &tree->buffer;
271 struct rb_node *n = root->rb_node;
272 struct extent_buffer *eb;
275 eb = rb_entry(n, struct extent_buffer, rb_node);
276 if (offset < eb->start)
278 else if (offset > eb->start)
287 * utility function to look for merge candidates inside a given range.
288 * Any extents with matching state are merged together into a single
289 * extent in the tree. Extents with EXTENT_IO in their state field
290 * are not merged because the end_io handlers need to be able to do
291 * operations on them without sleeping (or doing allocations/splits).
293 * This should be called with the tree lock held.
295 static int merge_state(struct extent_io_tree *tree,
296 struct extent_state *state)
298 struct extent_state *other;
299 struct rb_node *other_node;
301 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
304 other_node = rb_prev(&state->rb_node);
306 other = rb_entry(other_node, struct extent_state, rb_node);
307 if (other->end == state->start - 1 &&
308 other->state == state->state) {
309 state->start = other->start;
311 rb_erase(&other->rb_node, &tree->state);
312 free_extent_state(other);
315 other_node = rb_next(&state->rb_node);
317 other = rb_entry(other_node, struct extent_state, rb_node);
318 if (other->start == state->end + 1 &&
319 other->state == state->state) {
320 other->start = state->start;
322 rb_erase(&state->rb_node, &tree->state);
323 free_extent_state(state);
329 static void set_state_cb(struct extent_io_tree *tree,
330 struct extent_state *state,
333 if (tree->ops && tree->ops->set_bit_hook) {
334 tree->ops->set_bit_hook(tree->mapping->host, state->start,
335 state->end, state->state, bits);
339 static void clear_state_cb(struct extent_io_tree *tree,
340 struct extent_state *state,
343 if (tree->ops && tree->ops->clear_bit_hook) {
344 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
345 state->end, state->state, bits);
350 * insert an extent_state struct into the tree. 'bits' are set on the
351 * struct before it is inserted.
353 * This may return -EEXIST if the extent is already there, in which case the
354 * state struct is freed.
356 * The tree lock is not taken internally. This is a utility function and
357 * probably isn't what you want to call (see set/clear_extent_bit).
359 static int insert_state(struct extent_io_tree *tree,
360 struct extent_state *state, u64 start, u64 end,
363 struct rb_node *node;
366 printk(KERN_ERR "btrfs end < start %llu %llu\n",
367 (unsigned long long)end,
368 (unsigned long long)start);
371 if (bits & EXTENT_DIRTY)
372 tree->dirty_bytes += end - start + 1;
373 set_state_cb(tree, state, bits);
374 state->state |= bits;
375 state->start = start;
377 node = tree_insert(&tree->state, end, &state->rb_node);
379 struct extent_state *found;
380 found = rb_entry(node, struct extent_state, rb_node);
381 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
382 "%llu %llu\n", (unsigned long long)found->start,
383 (unsigned long long)found->end,
384 (unsigned long long)start, (unsigned long long)end);
385 free_extent_state(state);
389 merge_state(tree, state);
394 * split a given extent state struct in two, inserting the preallocated
395 * struct 'prealloc' as the newly created second half. 'split' indicates an
396 * offset inside 'orig' where it should be split.
399 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
400 * are two extent state structs in the tree:
401 * prealloc: [orig->start, split - 1]
402 * orig: [ split, orig->end ]
404 * The tree locks are not taken by this function. They need to be held
407 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
408 struct extent_state *prealloc, u64 split)
410 struct rb_node *node;
411 prealloc->start = orig->start;
412 prealloc->end = split - 1;
413 prealloc->state = orig->state;
416 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
418 struct extent_state *found;
419 found = rb_entry(node, struct extent_state, rb_node);
420 free_extent_state(prealloc);
423 prealloc->tree = tree;
428 * utility function to clear some bits in an extent state struct.
429 * it will optionally wake up any one waiting on this state (wake == 1), or
430 * forcibly remove the state from the tree (delete == 1).
432 * If no bits are set on the state struct after clearing things, the
433 * struct is freed and removed from the tree
435 static int clear_state_bit(struct extent_io_tree *tree,
436 struct extent_state *state, int bits, int wake,
439 int ret = state->state & bits;
441 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
442 u64 range = state->end - state->start + 1;
443 WARN_ON(range > tree->dirty_bytes);
444 tree->dirty_bytes -= range;
446 clear_state_cb(tree, state, bits);
447 state->state &= ~bits;
450 if (delete || state->state == 0) {
452 clear_state_cb(tree, state, state->state);
453 rb_erase(&state->rb_node, &tree->state);
455 free_extent_state(state);
460 merge_state(tree, state);
466 * clear some bits on a range in the tree. This may require splitting
467 * or inserting elements in the tree, so the gfp mask is used to
468 * indicate which allocations or sleeping are allowed.
470 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
471 * the given range from the tree regardless of state (ie for truncate).
473 * the range [start, end] is inclusive.
475 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
476 * bits were already set, or zero if none of the bits were already set.
478 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
479 int bits, int wake, int delete, gfp_t mask)
481 struct extent_state *state;
482 struct extent_state *prealloc = NULL;
483 struct rb_node *node;
488 if (!prealloc && (mask & __GFP_WAIT)) {
489 prealloc = alloc_extent_state(mask);
494 spin_lock(&tree->lock);
496 * this search will find the extents that end after
499 node = tree_search(tree, start);
502 state = rb_entry(node, struct extent_state, rb_node);
503 if (state->start > end)
505 WARN_ON(state->end < start);
508 * | ---- desired range ---- |
510 * | ------------- state -------------- |
512 * We need to split the extent we found, and may flip
513 * bits on second half.
515 * If the extent we found extends past our range, we
516 * just split and search again. It'll get split again
517 * the next time though.
519 * If the extent we found is inside our range, we clear
520 * the desired bit on it.
523 if (state->start < start) {
525 prealloc = alloc_extent_state(GFP_ATOMIC);
526 err = split_state(tree, state, prealloc, start);
527 BUG_ON(err == -EEXIST);
531 if (state->end <= end) {
532 start = state->end + 1;
533 set |= clear_state_bit(tree, state, bits,
536 start = state->start;
541 * | ---- desired range ---- |
543 * We need to split the extent, and clear the bit
546 if (state->start <= end && state->end > end) {
548 prealloc = alloc_extent_state(GFP_ATOMIC);
549 err = split_state(tree, state, prealloc, end + 1);
550 BUG_ON(err == -EEXIST);
554 set |= clear_state_bit(tree, prealloc, bits,
560 start = state->end + 1;
561 set |= clear_state_bit(tree, state, bits, wake, delete);
565 spin_unlock(&tree->lock);
567 free_extent_state(prealloc);
574 spin_unlock(&tree->lock);
575 if (mask & __GFP_WAIT)
580 static int wait_on_state(struct extent_io_tree *tree,
581 struct extent_state *state)
582 __releases(tree->lock)
583 __acquires(tree->lock)
586 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
587 spin_unlock(&tree->lock);
589 spin_lock(&tree->lock);
590 finish_wait(&state->wq, &wait);
595 * waits for one or more bits to clear on a range in the state tree.
596 * The range [start, end] is inclusive.
597 * The tree lock is taken by this function
599 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
601 struct extent_state *state;
602 struct rb_node *node;
604 spin_lock(&tree->lock);
608 * this search will find all the extents that end after
611 node = tree_search(tree, start);
615 state = rb_entry(node, struct extent_state, rb_node);
617 if (state->start > end)
620 if (state->state & bits) {
621 start = state->start;
622 atomic_inc(&state->refs);
623 wait_on_state(tree, state);
624 free_extent_state(state);
627 start = state->end + 1;
632 if (need_resched()) {
633 spin_unlock(&tree->lock);
635 spin_lock(&tree->lock);
639 spin_unlock(&tree->lock);
643 static void set_state_bits(struct extent_io_tree *tree,
644 struct extent_state *state,
647 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
648 u64 range = state->end - state->start + 1;
649 tree->dirty_bytes += range;
651 set_state_cb(tree, state, bits);
652 state->state |= bits;
656 * set some bits on a range in the tree. This may require allocations
657 * or sleeping, so the gfp mask is used to indicate what is allowed.
659 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
660 * range already has the desired bits set. The start of the existing
661 * range is returned in failed_start in this case.
663 * [start, end] is inclusive
664 * This takes the tree lock.
666 static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
667 int bits, int exclusive, u64 *failed_start,
670 struct extent_state *state;
671 struct extent_state *prealloc = NULL;
672 struct rb_node *node;
678 if (!prealloc && (mask & __GFP_WAIT)) {
679 prealloc = alloc_extent_state(mask);
684 spin_lock(&tree->lock);
686 * this search will find all the extents that end after
689 node = tree_search(tree, start);
691 err = insert_state(tree, prealloc, start, end, bits);
693 BUG_ON(err == -EEXIST);
697 state = rb_entry(node, struct extent_state, rb_node);
698 last_start = state->start;
699 last_end = state->end;
702 * | ---- desired range ---- |
705 * Just lock what we found and keep going
707 if (state->start == start && state->end <= end) {
708 set = state->state & bits;
709 if (set && exclusive) {
710 *failed_start = state->start;
714 set_state_bits(tree, state, bits);
715 start = state->end + 1;
716 merge_state(tree, state);
721 * | ---- desired range ---- |
724 * | ------------- state -------------- |
726 * We need to split the extent we found, and may flip bits on
729 * If the extent we found extends past our
730 * range, we just split and search again. It'll get split
731 * again the next time though.
733 * If the extent we found is inside our range, we set the
736 if (state->start < start) {
737 set = state->state & bits;
738 if (exclusive && set) {
739 *failed_start = start;
743 err = split_state(tree, state, prealloc, start);
744 BUG_ON(err == -EEXIST);
748 if (state->end <= end) {
749 set_state_bits(tree, state, bits);
750 start = state->end + 1;
751 merge_state(tree, state);
753 start = state->start;
758 * | ---- desired range ---- |
759 * | state | or | state |
761 * There's a hole, we need to insert something in it and
762 * ignore the extent we found.
764 if (state->start > start) {
766 if (end < last_start)
769 this_end = last_start - 1;
770 err = insert_state(tree, prealloc, start, this_end,
773 BUG_ON(err == -EEXIST);
776 start = this_end + 1;
780 * | ---- desired range ---- |
782 * We need to split the extent, and set the bit
785 if (state->start <= end && state->end > end) {
786 set = state->state & bits;
787 if (exclusive && set) {
788 *failed_start = start;
792 err = split_state(tree, state, prealloc, end + 1);
793 BUG_ON(err == -EEXIST);
795 set_state_bits(tree, prealloc, bits);
796 merge_state(tree, prealloc);
804 spin_unlock(&tree->lock);
806 free_extent_state(prealloc);
813 spin_unlock(&tree->lock);
814 if (mask & __GFP_WAIT)
819 /* wrappers around set/clear extent bit */
820 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
823 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
827 int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
830 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask);
833 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
834 int bits, gfp_t mask)
836 return set_extent_bit(tree, start, end, bits, 0, NULL,
840 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
841 int bits, gfp_t mask)
843 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
846 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
849 return set_extent_bit(tree, start, end,
850 EXTENT_DELALLOC | EXTENT_DIRTY,
854 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
857 return clear_extent_bit(tree, start, end,
858 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
861 int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
864 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask);
867 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
870 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
874 static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
877 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
880 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
883 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
887 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
890 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
893 static int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
896 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
900 static int clear_extent_writeback(struct extent_io_tree *tree, u64 start,
903 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
906 int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
908 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
912 * either insert or lock state struct between start and end use mask to tell
913 * us if waiting is desired.
915 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
920 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
921 &failed_start, mask);
922 if (err == -EEXIST && (mask & __GFP_WAIT)) {
923 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
924 start = failed_start;
928 WARN_ON(start > end);
933 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
939 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
940 &failed_start, mask);
941 if (err == -EEXIST) {
942 if (failed_start > start)
943 clear_extent_bit(tree, start, failed_start - 1,
944 EXTENT_LOCKED, 1, 0, mask);
950 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
953 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
957 * helper function to set pages and extents in the tree dirty
959 int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
961 unsigned long index = start >> PAGE_CACHE_SHIFT;
962 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
965 while (index <= end_index) {
966 page = find_get_page(tree->mapping, index);
968 __set_page_dirty_nobuffers(page);
969 page_cache_release(page);
972 set_extent_dirty(tree, start, end, GFP_NOFS);
977 * helper function to set both pages and extents in the tree writeback
979 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
981 unsigned long index = start >> PAGE_CACHE_SHIFT;
982 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
985 while (index <= end_index) {
986 page = find_get_page(tree->mapping, index);
988 set_page_writeback(page);
989 page_cache_release(page);
992 set_extent_writeback(tree, start, end, GFP_NOFS);
997 * find the first offset in the io tree with 'bits' set. zero is
998 * returned if we find something, and *start_ret and *end_ret are
999 * set to reflect the state struct that was found.
1001 * If nothing was found, 1 is returned, < 0 on error
1003 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1004 u64 *start_ret, u64 *end_ret, int bits)
1006 struct rb_node *node;
1007 struct extent_state *state;
1010 spin_lock(&tree->lock);
1012 * this search will find all the extents that end after
1015 node = tree_search(tree, start);
1020 state = rb_entry(node, struct extent_state, rb_node);
1021 if (state->end >= start && (state->state & bits)) {
1022 *start_ret = state->start;
1023 *end_ret = state->end;
1027 node = rb_next(node);
1032 spin_unlock(&tree->lock);
1036 /* find the first state struct with 'bits' set after 'start', and
1037 * return it. tree->lock must be held. NULL will returned if
1038 * nothing was found after 'start'
1040 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1041 u64 start, int bits)
1043 struct rb_node *node;
1044 struct extent_state *state;
1047 * this search will find all the extents that end after
1050 node = tree_search(tree, start);
1055 state = rb_entry(node, struct extent_state, rb_node);
1056 if (state->end >= start && (state->state & bits))
1059 node = rb_next(node);
1068 * find a contiguous range of bytes in the file marked as delalloc, not
1069 * more than 'max_bytes'. start and end are used to return the range,
1071 * 1 is returned if we find something, 0 if nothing was in the tree
1073 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1074 u64 *start, u64 *end, u64 max_bytes)
1076 struct rb_node *node;
1077 struct extent_state *state;
1078 u64 cur_start = *start;
1080 u64 total_bytes = 0;
1082 spin_lock(&tree->lock);
1085 * this search will find all the extents that end after
1088 node = tree_search(tree, cur_start);
1096 state = rb_entry(node, struct extent_state, rb_node);
1097 if (found && (state->start != cur_start ||
1098 (state->state & EXTENT_BOUNDARY))) {
1101 if (!(state->state & EXTENT_DELALLOC)) {
1107 *start = state->start;
1110 cur_start = state->end + 1;
1111 node = rb_next(node);
1114 total_bytes += state->end - state->start + 1;
1115 if (total_bytes >= max_bytes)
1119 spin_unlock(&tree->lock);
1123 static noinline int __unlock_for_delalloc(struct inode *inode,
1124 struct page *locked_page,
1128 struct page *pages[16];
1129 unsigned long index = start >> PAGE_CACHE_SHIFT;
1130 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1131 unsigned long nr_pages = end_index - index + 1;
1134 if (index == locked_page->index && end_index == index)
1137 while (nr_pages > 0) {
1138 ret = find_get_pages_contig(inode->i_mapping, index,
1139 min_t(unsigned long, nr_pages,
1140 ARRAY_SIZE(pages)), pages);
1141 for (i = 0; i < ret; i++) {
1142 if (pages[i] != locked_page)
1143 unlock_page(pages[i]);
1144 page_cache_release(pages[i]);
1153 static noinline int lock_delalloc_pages(struct inode *inode,
1154 struct page *locked_page,
1158 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1159 unsigned long start_index = index;
1160 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1161 unsigned long pages_locked = 0;
1162 struct page *pages[16];
1163 unsigned long nrpages;
1167 /* the caller is responsible for locking the start index */
1168 if (index == locked_page->index && index == end_index)
1171 /* skip the page at the start index */
1172 nrpages = end_index - index + 1;
1173 while (nrpages > 0) {
1174 ret = find_get_pages_contig(inode->i_mapping, index,
1175 min_t(unsigned long,
1176 nrpages, ARRAY_SIZE(pages)), pages);
1181 /* now we have an array of pages, lock them all */
1182 for (i = 0; i < ret; i++) {
1184 * the caller is taking responsibility for
1187 if (pages[i] != locked_page) {
1188 lock_page(pages[i]);
1189 if (!PageDirty(pages[i]) ||
1190 pages[i]->mapping != inode->i_mapping) {
1192 unlock_page(pages[i]);
1193 page_cache_release(pages[i]);
1197 page_cache_release(pages[i]);
1206 if (ret && pages_locked) {
1207 __unlock_for_delalloc(inode, locked_page,
1209 ((u64)(start_index + pages_locked - 1)) <<
1216 * find a contiguous range of bytes in the file marked as delalloc, not
1217 * more than 'max_bytes'. start and end are used to return the range,
1219 * 1 is returned if we find something, 0 if nothing was in the tree
1221 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1222 struct extent_io_tree *tree,
1223 struct page *locked_page,
1224 u64 *start, u64 *end,
1234 /* step one, find a bunch of delalloc bytes starting at start */
1235 delalloc_start = *start;
1237 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1239 if (!found || delalloc_end <= *start) {
1240 *start = delalloc_start;
1241 *end = delalloc_end;
1246 * start comes from the offset of locked_page. We have to lock
1247 * pages in order, so we can't process delalloc bytes before
1250 if (delalloc_start < *start)
1251 delalloc_start = *start;
1254 * make sure to limit the number of pages we try to lock down
1257 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1258 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1260 /* step two, lock all the pages after the page that has start */
1261 ret = lock_delalloc_pages(inode, locked_page,
1262 delalloc_start, delalloc_end);
1263 if (ret == -EAGAIN) {
1264 /* some of the pages are gone, lets avoid looping by
1265 * shortening the size of the delalloc range we're searching
1268 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1269 max_bytes = PAGE_CACHE_SIZE - offset;
1279 /* step three, lock the state bits for the whole range */
1280 lock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1282 /* then test to make sure it is all still delalloc */
1283 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1284 EXTENT_DELALLOC, 1);
1286 unlock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1287 __unlock_for_delalloc(inode, locked_page,
1288 delalloc_start, delalloc_end);
1292 *start = delalloc_start;
1293 *end = delalloc_end;
1298 int extent_clear_unlock_delalloc(struct inode *inode,
1299 struct extent_io_tree *tree,
1300 u64 start, u64 end, struct page *locked_page,
1303 int clear_delalloc, int clear_dirty,
1308 struct page *pages[16];
1309 unsigned long index = start >> PAGE_CACHE_SHIFT;
1310 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1311 unsigned long nr_pages = end_index - index + 1;
1316 clear_bits |= EXTENT_LOCKED;
1318 clear_bits |= EXTENT_DIRTY;
1321 clear_bits |= EXTENT_DELALLOC;
1323 clear_extent_bit(tree, start, end, clear_bits, 1, 0, GFP_NOFS);
1324 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback))
1327 while (nr_pages > 0) {
1328 ret = find_get_pages_contig(inode->i_mapping, index,
1329 min_t(unsigned long,
1330 nr_pages, ARRAY_SIZE(pages)), pages);
1331 for (i = 0; i < ret; i++) {
1332 if (pages[i] == locked_page) {
1333 page_cache_release(pages[i]);
1337 clear_page_dirty_for_io(pages[i]);
1339 set_page_writeback(pages[i]);
1341 end_page_writeback(pages[i]);
1343 unlock_page(pages[i]);
1344 page_cache_release(pages[i]);
1354 * count the number of bytes in the tree that have a given bit(s)
1355 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1356 * cached. The total number found is returned.
1358 u64 count_range_bits(struct extent_io_tree *tree,
1359 u64 *start, u64 search_end, u64 max_bytes,
1362 struct rb_node *node;
1363 struct extent_state *state;
1364 u64 cur_start = *start;
1365 u64 total_bytes = 0;
1368 if (search_end <= cur_start) {
1373 spin_lock(&tree->lock);
1374 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1375 total_bytes = tree->dirty_bytes;
1379 * this search will find all the extents that end after
1382 node = tree_search(tree, cur_start);
1387 state = rb_entry(node, struct extent_state, rb_node);
1388 if (state->start > search_end)
1390 if (state->end >= cur_start && (state->state & bits)) {
1391 total_bytes += min(search_end, state->end) + 1 -
1392 max(cur_start, state->start);
1393 if (total_bytes >= max_bytes)
1396 *start = state->start;
1400 node = rb_next(node);
1405 spin_unlock(&tree->lock);
1411 * helper function to lock both pages and extents in the tree.
1412 * pages must be locked first.
1414 static int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
1416 unsigned long index = start >> PAGE_CACHE_SHIFT;
1417 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1421 while (index <= end_index) {
1422 page = grab_cache_page(tree->mapping, index);
1428 err = PTR_ERR(page);
1433 lock_extent(tree, start, end, GFP_NOFS);
1438 * we failed above in getting the page at 'index', so we undo here
1439 * up to but not including the page at 'index'
1442 index = start >> PAGE_CACHE_SHIFT;
1443 while (index < end_index) {
1444 page = find_get_page(tree->mapping, index);
1446 page_cache_release(page);
1453 * helper function to unlock both pages and extents in the tree.
1455 static int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
1457 unsigned long index = start >> PAGE_CACHE_SHIFT;
1458 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1461 while (index <= end_index) {
1462 page = find_get_page(tree->mapping, index);
1464 page_cache_release(page);
1467 unlock_extent(tree, start, end, GFP_NOFS);
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)
1539 struct extent_state *state = NULL;
1540 struct rb_node *node;
1543 spin_lock(&tree->lock);
1544 node = tree_search(tree, start);
1545 while (node && start <= end) {
1546 state = rb_entry(node, struct extent_state, rb_node);
1548 if (filled && state->start > start) {
1553 if (state->start > end)
1556 if (state->state & bits) {
1560 } else if (filled) {
1564 start = state->end + 1;
1567 node = rb_next(node);
1574 spin_unlock(&tree->lock);
1579 * helper function to set a given page up to date if all the
1580 * extents in the tree for that page are up to date
1582 static int check_page_uptodate(struct extent_io_tree *tree,
1585 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1586 u64 end = start + PAGE_CACHE_SIZE - 1;
1587 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1588 SetPageUptodate(page);
1593 * helper function to unlock a page if all the extents in the tree
1594 * for that page are unlocked
1596 static int check_page_locked(struct extent_io_tree *tree,
1599 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1600 u64 end = start + PAGE_CACHE_SIZE - 1;
1601 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1607 * helper function to end page writeback if all the extents
1608 * in the tree for that page are done with writeback
1610 static int check_page_writeback(struct extent_io_tree *tree,
1613 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1614 u64 end = start + PAGE_CACHE_SIZE - 1;
1615 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1616 end_page_writeback(page);
1620 /* lots and lots of room for performance fixes in the end_bio funcs */
1623 * after a writepage IO is done, we need to:
1624 * clear the uptodate bits on error
1625 * clear the writeback bits in the extent tree for this IO
1626 * end_page_writeback if the page has no more pending IO
1628 * Scheduling is not allowed, so the extent state tree is expected
1629 * to have one and only one object corresponding to this IO.
1631 static void end_bio_extent_writepage(struct bio *bio, int err)
1633 int uptodate = err == 0;
1634 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1635 struct extent_io_tree *tree;
1642 struct page *page = bvec->bv_page;
1643 tree = &BTRFS_I(page->mapping->host)->io_tree;
1645 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1647 end = start + bvec->bv_len - 1;
1649 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1654 if (--bvec >= bio->bi_io_vec)
1655 prefetchw(&bvec->bv_page->flags);
1656 if (tree->ops && tree->ops->writepage_end_io_hook) {
1657 ret = tree->ops->writepage_end_io_hook(page, start,
1658 end, NULL, uptodate);
1663 if (!uptodate && tree->ops &&
1664 tree->ops->writepage_io_failed_hook) {
1665 ret = tree->ops->writepage_io_failed_hook(bio, page,
1668 uptodate = (err == 0);
1674 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1675 ClearPageUptodate(page);
1679 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
1682 end_page_writeback(page);
1684 check_page_writeback(tree, page);
1685 } while (bvec >= bio->bi_io_vec);
1691 * after a readpage IO is done, we need to:
1692 * clear the uptodate bits on error
1693 * set the uptodate bits if things worked
1694 * set the page up to date if all extents in the tree are uptodate
1695 * clear the lock bit in the extent tree
1696 * unlock the page if there are no other extents locked for it
1698 * Scheduling is not allowed, so the extent state tree is expected
1699 * to have one and only one object corresponding to this IO.
1701 static void end_bio_extent_readpage(struct bio *bio, int err)
1703 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1704 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1705 struct extent_io_tree *tree;
1715 struct page *page = bvec->bv_page;
1716 tree = &BTRFS_I(page->mapping->host)->io_tree;
1718 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1720 end = start + bvec->bv_len - 1;
1722 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1727 if (--bvec >= bio->bi_io_vec)
1728 prefetchw(&bvec->bv_page->flags);
1730 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1731 ret = tree->ops->readpage_end_io_hook(page, start, end,
1736 if (!uptodate && tree->ops &&
1737 tree->ops->readpage_io_failed_hook) {
1738 ret = tree->ops->readpage_io_failed_hook(bio, page,
1742 test_bit(BIO_UPTODATE, &bio->bi_flags);
1750 set_extent_uptodate(tree, start, end,
1753 unlock_extent(tree, start, end, GFP_ATOMIC);
1757 SetPageUptodate(page);
1759 ClearPageUptodate(page);
1765 check_page_uptodate(tree, page);
1767 ClearPageUptodate(page);
1770 check_page_locked(tree, page);
1772 } while (bvec >= bio->bi_io_vec);
1778 * IO done from prepare_write is pretty simple, we just unlock
1779 * the structs in the extent tree when done, and set the uptodate bits
1782 static void end_bio_extent_preparewrite(struct bio *bio, int err)
1784 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1785 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1786 struct extent_io_tree *tree;
1791 struct page *page = bvec->bv_page;
1792 tree = &BTRFS_I(page->mapping->host)->io_tree;
1794 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1796 end = start + bvec->bv_len - 1;
1798 if (--bvec >= bio->bi_io_vec)
1799 prefetchw(&bvec->bv_page->flags);
1802 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1804 ClearPageUptodate(page);
1808 unlock_extent(tree, start, end, GFP_ATOMIC);
1810 } while (bvec >= bio->bi_io_vec);
1816 extent_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;
1846 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1847 end = start + bvec->bv_len - 1;
1849 bio->bi_private = NULL;
1853 if (tree->ops && tree->ops->submit_bio_hook)
1854 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1855 mirror_num, bio_flags);
1857 submit_bio(rw, bio);
1858 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1864 static int submit_extent_page(int rw, struct extent_io_tree *tree,
1865 struct page *page, sector_t sector,
1866 size_t size, unsigned long offset,
1867 struct block_device *bdev,
1868 struct bio **bio_ret,
1869 unsigned long max_pages,
1870 bio_end_io_t end_io_func,
1872 unsigned long prev_bio_flags,
1873 unsigned long bio_flags)
1879 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1880 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
1881 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
1883 if (bio_ret && *bio_ret) {
1886 contig = bio->bi_sector == sector;
1888 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1891 if (prev_bio_flags != bio_flags || !contig ||
1892 (tree->ops && tree->ops->merge_bio_hook &&
1893 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1895 bio_add_page(bio, page, page_size, offset) < page_size) {
1896 ret = submit_one_bio(rw, bio, mirror_num,
1903 if (this_compressed)
1906 nr = bio_get_nr_vecs(bdev);
1908 bio = extent_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 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1937 * basic readpage implementation. Locked extent state structs are inserted
1938 * into the tree that are removed when the IO is done (by the end_io
1941 static int __extent_read_full_page(struct extent_io_tree *tree,
1943 get_extent_t *get_extent,
1944 struct bio **bio, int mirror_num,
1945 unsigned long *bio_flags)
1947 struct inode *inode = page->mapping->host;
1948 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1949 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1953 u64 last_byte = i_size_read(inode);
1957 struct extent_map *em;
1958 struct block_device *bdev;
1961 size_t page_offset = 0;
1963 size_t disk_io_size;
1964 size_t blocksize = inode->i_sb->s_blocksize;
1965 unsigned long this_bio_flag = 0;
1967 set_page_extent_mapped(page);
1970 lock_extent(tree, start, end, GFP_NOFS);
1972 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1974 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1977 iosize = PAGE_CACHE_SIZE - zero_offset;
1978 userpage = kmap_atomic(page, KM_USER0);
1979 memset(userpage + zero_offset, 0, iosize);
1980 flush_dcache_page(page);
1981 kunmap_atomic(userpage, KM_USER0);
1984 while (cur <= end) {
1985 if (cur >= last_byte) {
1987 iosize = PAGE_CACHE_SIZE - page_offset;
1988 userpage = kmap_atomic(page, KM_USER0);
1989 memset(userpage + page_offset, 0, iosize);
1990 flush_dcache_page(page);
1991 kunmap_atomic(userpage, KM_USER0);
1992 set_extent_uptodate(tree, cur, cur + iosize - 1,
1994 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1997 em = get_extent(inode, page, page_offset, cur,
1999 if (IS_ERR(em) || !em) {
2001 unlock_extent(tree, cur, end, GFP_NOFS);
2004 extent_offset = cur - em->start;
2005 BUG_ON(extent_map_end(em) <= cur);
2008 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2009 this_bio_flag = EXTENT_BIO_COMPRESSED;
2011 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2012 cur_end = min(extent_map_end(em) - 1, end);
2013 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2014 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2015 disk_io_size = em->block_len;
2016 sector = em->block_start >> 9;
2018 sector = (em->block_start + extent_offset) >> 9;
2019 disk_io_size = iosize;
2022 block_start = em->block_start;
2023 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2024 block_start = EXTENT_MAP_HOLE;
2025 free_extent_map(em);
2028 /* we've found a hole, just zero and go on */
2029 if (block_start == EXTENT_MAP_HOLE) {
2031 userpage = kmap_atomic(page, KM_USER0);
2032 memset(userpage + page_offset, 0, iosize);
2033 flush_dcache_page(page);
2034 kunmap_atomic(userpage, KM_USER0);
2036 set_extent_uptodate(tree, cur, cur + iosize - 1,
2038 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2040 page_offset += iosize;
2043 /* the get_extent function already copied into the page */
2044 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
2045 check_page_uptodate(tree, page);
2046 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2048 page_offset += iosize;
2051 /* we have an inline extent but it didn't get marked up
2052 * to date. Error out
2054 if (block_start == EXTENT_MAP_INLINE) {
2056 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2058 page_offset += iosize;
2063 if (tree->ops && tree->ops->readpage_io_hook) {
2064 ret = tree->ops->readpage_io_hook(page, cur,
2068 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2070 ret = submit_extent_page(READ, tree, page,
2071 sector, disk_io_size, page_offset,
2073 end_bio_extent_readpage, mirror_num,
2077 *bio_flags = this_bio_flag;
2082 page_offset += iosize;
2085 if (!PageError(page))
2086 SetPageUptodate(page);
2092 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2093 get_extent_t *get_extent)
2095 struct bio *bio = NULL;
2096 unsigned long bio_flags = 0;
2099 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2102 submit_one_bio(READ, bio, 0, bio_flags);
2107 * the writepage semantics are similar to regular writepage. extent
2108 * records are inserted to lock ranges in the tree, and as dirty areas
2109 * are found, they are marked writeback. Then the lock bits are removed
2110 * and the end_io handler clears the writeback ranges
2112 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2115 struct inode *inode = page->mapping->host;
2116 struct extent_page_data *epd = data;
2117 struct extent_io_tree *tree = epd->tree;
2118 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2120 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2124 u64 last_byte = i_size_read(inode);
2129 struct extent_map *em;
2130 struct block_device *bdev;
2133 size_t pg_offset = 0;
2135 loff_t i_size = i_size_read(inode);
2136 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2141 unsigned long nr_written = 0;
2143 WARN_ON(!PageLocked(page));
2144 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2145 if (page->index > end_index ||
2146 (page->index == end_index && !pg_offset)) {
2147 page->mapping->a_ops->invalidatepage(page, 0);
2152 if (page->index == end_index) {
2155 userpage = kmap_atomic(page, KM_USER0);
2156 memset(userpage + pg_offset, 0,
2157 PAGE_CACHE_SIZE - pg_offset);
2158 kunmap_atomic(userpage, KM_USER0);
2159 flush_dcache_page(page);
2163 set_page_extent_mapped(page);
2165 delalloc_start = start;
2168 if (!epd->extent_locked) {
2169 while (delalloc_end < page_end) {
2170 nr_delalloc = find_lock_delalloc_range(inode, tree,
2175 if (nr_delalloc == 0) {
2176 delalloc_start = delalloc_end + 1;
2179 tree->ops->fill_delalloc(inode, page, delalloc_start,
2180 delalloc_end, &page_started,
2182 delalloc_start = delalloc_end + 1;
2185 /* did the fill delalloc function already unlock and start
2190 goto update_nr_written;
2193 lock_extent(tree, start, page_end, GFP_NOFS);
2195 unlock_start = start;
2197 if (tree->ops && tree->ops->writepage_start_hook) {
2198 ret = tree->ops->writepage_start_hook(page, start,
2200 if (ret == -EAGAIN) {
2201 unlock_extent(tree, start, page_end, GFP_NOFS);
2202 redirty_page_for_writepage(wbc, page);
2205 goto update_nr_written;
2212 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0))
2213 printk(KERN_ERR "btrfs delalloc bits after lock_extent\n");
2215 if (last_byte <= start) {
2216 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
2217 unlock_extent(tree, start, page_end, GFP_NOFS);
2218 if (tree->ops && tree->ops->writepage_end_io_hook)
2219 tree->ops->writepage_end_io_hook(page, start,
2221 unlock_start = page_end + 1;
2225 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2226 blocksize = inode->i_sb->s_blocksize;
2228 while (cur <= end) {
2229 if (cur >= last_byte) {
2230 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
2231 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2232 if (tree->ops && tree->ops->writepage_end_io_hook)
2233 tree->ops->writepage_end_io_hook(page, cur,
2235 unlock_start = page_end + 1;
2238 em = epd->get_extent(inode, page, pg_offset, cur,
2240 if (IS_ERR(em) || !em) {
2245 extent_offset = cur - em->start;
2246 BUG_ON(extent_map_end(em) <= cur);
2248 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2249 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2250 sector = (em->block_start + extent_offset) >> 9;
2252 block_start = em->block_start;
2253 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2254 free_extent_map(em);
2258 * compressed and inline extents are written through other
2261 if (compressed || block_start == EXTENT_MAP_HOLE ||
2262 block_start == EXTENT_MAP_INLINE) {
2263 clear_extent_dirty(tree, cur,
2264 cur + iosize - 1, GFP_NOFS);
2266 unlock_extent(tree, unlock_start, cur + iosize - 1,
2270 * end_io notification does not happen here for
2271 * compressed extents
2273 if (!compressed && tree->ops &&
2274 tree->ops->writepage_end_io_hook)
2275 tree->ops->writepage_end_io_hook(page, cur,
2278 else if (compressed) {
2279 /* we don't want to end_page_writeback on
2280 * a compressed extent. this happens
2287 pg_offset += iosize;
2291 /* leave this out until we have a page_mkwrite call */
2292 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2295 pg_offset += iosize;
2299 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2300 if (tree->ops && tree->ops->writepage_io_hook) {
2301 ret = tree->ops->writepage_io_hook(page, cur,
2309 unsigned long max_nr = end_index + 1;
2311 set_range_writeback(tree, cur, cur + iosize - 1);
2312 if (!PageWriteback(page)) {
2313 printk(KERN_ERR "btrfs warning page %lu not "
2314 "writeback, cur %llu end %llu\n",
2315 page->index, (unsigned long long)cur,
2316 (unsigned long long)end);
2319 ret = submit_extent_page(WRITE, tree, page, sector,
2320 iosize, pg_offset, bdev,
2322 end_bio_extent_writepage,
2328 pg_offset += iosize;
2333 /* make sure the mapping tag for page dirty gets cleared */
2334 set_page_writeback(page);
2335 end_page_writeback(page);
2337 if (unlock_start <= page_end)
2338 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2342 wbc->nr_to_write -= nr_written;
2343 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2344 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2345 page->mapping->writeback_index = page->index + nr_written;
2350 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2351 * @mapping: address space structure to write
2352 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2353 * @writepage: function called for each page
2354 * @data: data passed to writepage function
2356 * If a page is already under I/O, write_cache_pages() skips it, even
2357 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2358 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2359 * and msync() need to guarantee that all the data which was dirty at the time
2360 * the call was made get new I/O started against them. If wbc->sync_mode is
2361 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2362 * existing IO to complete.
2364 static int extent_write_cache_pages(struct extent_io_tree *tree,
2365 struct address_space *mapping,
2366 struct writeback_control *wbc,
2367 writepage_t writepage, void *data,
2368 void (*flush_fn)(void *))
2370 struct backing_dev_info *bdi = mapping->backing_dev_info;
2373 struct pagevec pvec;
2376 pgoff_t end; /* Inclusive */
2378 int range_whole = 0;
2380 pagevec_init(&pvec, 0);
2381 if (wbc->range_cyclic) {
2382 index = mapping->writeback_index; /* Start from prev offset */
2385 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2386 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2387 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2392 while (!done && (index <= end) &&
2393 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2394 PAGECACHE_TAG_DIRTY, min(end - index,
2395 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2399 for (i = 0; i < nr_pages; i++) {
2400 struct page *page = pvec.pages[i];
2403 * At this point we hold neither mapping->tree_lock nor
2404 * lock on the page itself: the page may be truncated or
2405 * invalidated (changing page->mapping to NULL), or even
2406 * swizzled back from swapper_space to tmpfs file
2409 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2410 tree->ops->write_cache_pages_lock_hook(page);
2414 if (unlikely(page->mapping != mapping)) {
2419 if (!wbc->range_cyclic && page->index > end) {
2425 if (wbc->sync_mode != WB_SYNC_NONE) {
2426 if (PageWriteback(page))
2428 wait_on_page_writeback(page);
2431 if (PageWriteback(page) ||
2432 !clear_page_dirty_for_io(page)) {
2437 ret = (*writepage)(page, wbc, data);
2439 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2443 if (ret || wbc->nr_to_write <= 0)
2445 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2446 wbc->encountered_congestion = 1;
2450 pagevec_release(&pvec);
2453 if (!scanned && !done) {
2455 * We hit the last page and there is more work to be done: wrap
2456 * back to the start of the file
2465 static noinline void flush_write_bio(void *data)
2467 struct extent_page_data *epd = data;
2469 submit_one_bio(WRITE, epd->bio, 0, 0);
2474 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2475 get_extent_t *get_extent,
2476 struct writeback_control *wbc)
2479 struct address_space *mapping = page->mapping;
2480 struct extent_page_data epd = {
2483 .get_extent = get_extent,
2486 struct writeback_control wbc_writepages = {
2488 .sync_mode = WB_SYNC_NONE,
2489 .older_than_this = NULL,
2491 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2492 .range_end = (loff_t)-1,
2496 ret = __extent_writepage(page, wbc, &epd);
2498 extent_write_cache_pages(tree, mapping, &wbc_writepages,
2499 __extent_writepage, &epd, flush_write_bio);
2501 submit_one_bio(WRITE, epd.bio, 0, 0);
2505 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2506 u64 start, u64 end, get_extent_t *get_extent,
2510 struct address_space *mapping = inode->i_mapping;
2512 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2515 struct extent_page_data epd = {
2518 .get_extent = get_extent,
2521 struct writeback_control wbc_writepages = {
2522 .bdi = inode->i_mapping->backing_dev_info,
2524 .older_than_this = NULL,
2525 .nr_to_write = nr_pages * 2,
2526 .range_start = start,
2527 .range_end = end + 1,
2530 while (start <= end) {
2531 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2532 if (clear_page_dirty_for_io(page))
2533 ret = __extent_writepage(page, &wbc_writepages, &epd);
2535 if (tree->ops && tree->ops->writepage_end_io_hook)
2536 tree->ops->writepage_end_io_hook(page, start,
2537 start + PAGE_CACHE_SIZE - 1,
2541 page_cache_release(page);
2542 start += PAGE_CACHE_SIZE;
2546 submit_one_bio(WRITE, epd.bio, 0, 0);
2550 int extent_writepages(struct extent_io_tree *tree,
2551 struct address_space *mapping,
2552 get_extent_t *get_extent,
2553 struct writeback_control *wbc)
2556 struct extent_page_data epd = {
2559 .get_extent = get_extent,
2563 ret = extent_write_cache_pages(tree, mapping, wbc,
2564 __extent_writepage, &epd,
2567 submit_one_bio(WRITE, epd.bio, 0, 0);
2571 int extent_readpages(struct extent_io_tree *tree,
2572 struct address_space *mapping,
2573 struct list_head *pages, unsigned nr_pages,
2574 get_extent_t get_extent)
2576 struct bio *bio = NULL;
2578 struct pagevec pvec;
2579 unsigned long bio_flags = 0;
2581 pagevec_init(&pvec, 0);
2582 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2583 struct page *page = list_entry(pages->prev, struct page, lru);
2585 prefetchw(&page->flags);
2586 list_del(&page->lru);
2588 * what we want to do here is call add_to_page_cache_lru,
2589 * but that isn't exported, so we reproduce it here
2591 if (!add_to_page_cache(page, mapping,
2592 page->index, GFP_KERNEL)) {
2594 /* open coding of lru_cache_add, also not exported */
2595 page_cache_get(page);
2596 if (!pagevec_add(&pvec, page))
2597 __pagevec_lru_add_file(&pvec);
2598 __extent_read_full_page(tree, page, get_extent,
2599 &bio, 0, &bio_flags);
2601 page_cache_release(page);
2603 if (pagevec_count(&pvec))
2604 __pagevec_lru_add_file(&pvec);
2605 BUG_ON(!list_empty(pages));
2607 submit_one_bio(READ, bio, 0, bio_flags);
2612 * basic invalidatepage code, this waits on any locked or writeback
2613 * ranges corresponding to the page, and then deletes any extent state
2614 * records from the tree
2616 int extent_invalidatepage(struct extent_io_tree *tree,
2617 struct page *page, unsigned long offset)
2619 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2620 u64 end = start + PAGE_CACHE_SIZE - 1;
2621 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2623 start += (offset + blocksize - 1) & ~(blocksize - 1);
2627 lock_extent(tree, start, end, GFP_NOFS);
2628 wait_on_extent_writeback(tree, start, end);
2629 clear_extent_bit(tree, start, end,
2630 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2636 * simple commit_write call, set_range_dirty is used to mark both
2637 * the pages and the extent records as dirty
2639 int extent_commit_write(struct extent_io_tree *tree,
2640 struct inode *inode, struct page *page,
2641 unsigned from, unsigned to)
2643 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2645 set_page_extent_mapped(page);
2646 set_page_dirty(page);
2648 if (pos > inode->i_size) {
2649 i_size_write(inode, pos);
2650 mark_inode_dirty(inode);
2655 int extent_prepare_write(struct extent_io_tree *tree,
2656 struct inode *inode, struct page *page,
2657 unsigned from, unsigned to, get_extent_t *get_extent)
2659 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2660 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2662 u64 orig_block_start;
2665 struct extent_map *em;
2666 unsigned blocksize = 1 << inode->i_blkbits;
2667 size_t page_offset = 0;
2668 size_t block_off_start;
2669 size_t block_off_end;
2675 set_page_extent_mapped(page);
2677 block_start = (page_start + from) & ~((u64)blocksize - 1);
2678 block_end = (page_start + to - 1) | (blocksize - 1);
2679 orig_block_start = block_start;
2681 lock_extent(tree, page_start, page_end, GFP_NOFS);
2682 while (block_start <= block_end) {
2683 em = get_extent(inode, page, page_offset, block_start,
2684 block_end - block_start + 1, 1);
2685 if (IS_ERR(em) || !em)
2688 cur_end = min(block_end, extent_map_end(em) - 1);
2689 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2690 block_off_end = block_off_start + blocksize;
2691 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2693 if (!PageUptodate(page) && isnew &&
2694 (block_off_end > to || block_off_start < from)) {
2697 kaddr = kmap_atomic(page, KM_USER0);
2698 if (block_off_end > to)
2699 memset(kaddr + to, 0, block_off_end - to);
2700 if (block_off_start < from)
2701 memset(kaddr + block_off_start, 0,
2702 from - block_off_start);
2703 flush_dcache_page(page);
2704 kunmap_atomic(kaddr, KM_USER0);
2706 if ((em->block_start != EXTENT_MAP_HOLE &&
2707 em->block_start != EXTENT_MAP_INLINE) &&
2708 !isnew && !PageUptodate(page) &&
2709 (block_off_end > to || block_off_start < from) &&
2710 !test_range_bit(tree, block_start, cur_end,
2711 EXTENT_UPTODATE, 1)) {
2713 u64 extent_offset = block_start - em->start;
2715 sector = (em->block_start + extent_offset) >> 9;
2716 iosize = (cur_end - block_start + blocksize) &
2717 ~((u64)blocksize - 1);
2719 * we've already got the extent locked, but we
2720 * need to split the state such that our end_bio
2721 * handler can clear the lock.
2723 set_extent_bit(tree, block_start,
2724 block_start + iosize - 1,
2725 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2726 ret = submit_extent_page(READ, tree, page,
2727 sector, iosize, page_offset, em->bdev,
2729 end_bio_extent_preparewrite, 0,
2732 block_start = block_start + iosize;
2734 set_extent_uptodate(tree, block_start, cur_end,
2736 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2737 block_start = cur_end + 1;
2739 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2740 free_extent_map(em);
2743 wait_extent_bit(tree, orig_block_start,
2744 block_end, EXTENT_LOCKED);
2746 check_page_uptodate(tree, page);
2748 /* FIXME, zero out newly allocated blocks on error */
2753 * a helper for releasepage, this tests for areas of the page that
2754 * are locked or under IO and drops the related state bits if it is safe
2757 int try_release_extent_state(struct extent_map_tree *map,
2758 struct extent_io_tree *tree, struct page *page,
2761 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2762 u64 end = start + PAGE_CACHE_SIZE - 1;
2765 if (test_range_bit(tree, start, end,
2766 EXTENT_IOBITS | EXTENT_ORDERED, 0))
2769 if ((mask & GFP_NOFS) == GFP_NOFS)
2771 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2778 * a helper for releasepage. As long as there are no locked extents
2779 * in the range corresponding to the page, both state records and extent
2780 * map records are removed
2782 int try_release_extent_mapping(struct extent_map_tree *map,
2783 struct extent_io_tree *tree, struct page *page,
2786 struct extent_map *em;
2787 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2788 u64 end = start + PAGE_CACHE_SIZE - 1;
2790 if ((mask & __GFP_WAIT) &&
2791 page->mapping->host->i_size > 16 * 1024 * 1024) {
2793 while (start <= end) {
2794 len = end - start + 1;
2795 spin_lock(&map->lock);
2796 em = lookup_extent_mapping(map, start, len);
2797 if (!em || IS_ERR(em)) {
2798 spin_unlock(&map->lock);
2801 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2802 em->start != start) {
2803 spin_unlock(&map->lock);
2804 free_extent_map(em);
2807 if (!test_range_bit(tree, em->start,
2808 extent_map_end(em) - 1,
2809 EXTENT_LOCKED | EXTENT_WRITEBACK |
2812 remove_extent_mapping(map, em);
2813 /* once for the rb tree */
2814 free_extent_map(em);
2816 start = extent_map_end(em);
2817 spin_unlock(&map->lock);
2820 free_extent_map(em);
2823 return try_release_extent_state(map, tree, page, mask);
2826 sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2827 get_extent_t *get_extent)
2829 struct inode *inode = mapping->host;
2830 u64 start = iblock << inode->i_blkbits;
2831 sector_t sector = 0;
2832 size_t blksize = (1 << inode->i_blkbits);
2833 struct extent_map *em;
2835 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2837 em = get_extent(inode, NULL, 0, start, blksize, 0);
2838 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2840 if (!em || IS_ERR(em))
2843 if (em->block_start > EXTENT_MAP_LAST_BYTE)
2846 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2848 free_extent_map(em);
2852 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2853 __u64 start, __u64 len, get_extent_t *get_extent)
2857 u64 max = start + len;
2860 struct extent_map *em = NULL;
2862 u64 em_start = 0, em_len = 0;
2863 unsigned long emflags;
2869 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2871 em = get_extent(inode, NULL, 0, off, max - off, 0);
2879 off = em->start + em->len;
2883 em_start = em->start;
2889 switch (em->block_start) {
2890 case EXTENT_MAP_LAST_BYTE:
2892 flags |= FIEMAP_EXTENT_LAST;
2894 case EXTENT_MAP_HOLE:
2895 flags |= FIEMAP_EXTENT_UNWRITTEN;
2897 case EXTENT_MAP_INLINE:
2898 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2899 FIEMAP_EXTENT_NOT_ALIGNED);
2901 case EXTENT_MAP_DELALLOC:
2902 flags |= (FIEMAP_EXTENT_DELALLOC |
2903 FIEMAP_EXTENT_UNKNOWN);
2906 disko = em->block_start;
2909 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2910 flags |= FIEMAP_EXTENT_ENCODED;
2912 emflags = em->flags;
2913 free_extent_map(em);
2917 em = get_extent(inode, NULL, 0, off, max - off, 0);
2924 emflags = em->flags;
2926 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
2927 flags |= FIEMAP_EXTENT_LAST;
2931 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2937 free_extent_map(em);
2939 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2944 static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2948 struct address_space *mapping;
2951 return eb->first_page;
2952 i += eb->start >> PAGE_CACHE_SHIFT;
2953 mapping = eb->first_page->mapping;
2958 * extent_buffer_page is only called after pinning the page
2959 * by increasing the reference count. So we know the page must
2960 * be in the radix tree.
2963 p = radix_tree_lookup(&mapping->page_tree, i);
2969 static inline unsigned long num_extent_pages(u64 start, u64 len)
2971 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2972 (start >> PAGE_CACHE_SHIFT);
2975 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2980 struct extent_buffer *eb = NULL;
2982 unsigned long flags;
2985 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
2988 spin_lock_init(&eb->lock);
2989 init_waitqueue_head(&eb->lock_wq);
2992 spin_lock_irqsave(&leak_lock, flags);
2993 list_add(&eb->leak_list, &buffers);
2994 spin_unlock_irqrestore(&leak_lock, flags);
2996 atomic_set(&eb->refs, 1);
3001 static void __free_extent_buffer(struct extent_buffer *eb)
3004 unsigned long flags;
3005 spin_lock_irqsave(&leak_lock, flags);
3006 list_del(&eb->leak_list);
3007 spin_unlock_irqrestore(&leak_lock, flags);
3009 kmem_cache_free(extent_buffer_cache, eb);
3012 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3013 u64 start, unsigned long len,
3017 unsigned long num_pages = num_extent_pages(start, len);
3019 unsigned long index = start >> PAGE_CACHE_SHIFT;
3020 struct extent_buffer *eb;
3021 struct extent_buffer *exists = NULL;
3023 struct address_space *mapping = tree->mapping;
3026 spin_lock(&tree->buffer_lock);
3027 eb = buffer_search(tree, start);
3029 atomic_inc(&eb->refs);
3030 spin_unlock(&tree->buffer_lock);
3031 mark_page_accessed(eb->first_page);
3034 spin_unlock(&tree->buffer_lock);
3036 eb = __alloc_extent_buffer(tree, start, len, mask);
3041 eb->first_page = page0;
3044 page_cache_get(page0);
3045 mark_page_accessed(page0);
3046 set_page_extent_mapped(page0);
3047 set_page_extent_head(page0, len);
3048 uptodate = PageUptodate(page0);
3052 for (; i < num_pages; i++, index++) {
3053 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3058 set_page_extent_mapped(p);
3059 mark_page_accessed(p);
3062 set_page_extent_head(p, len);
3064 set_page_private(p, EXTENT_PAGE_PRIVATE);
3066 if (!PageUptodate(p))
3071 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3073 spin_lock(&tree->buffer_lock);
3074 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3076 /* add one reference for the caller */
3077 atomic_inc(&exists->refs);
3078 spin_unlock(&tree->buffer_lock);
3081 spin_unlock(&tree->buffer_lock);
3083 /* add one reference for the tree */
3084 atomic_inc(&eb->refs);
3088 if (!atomic_dec_and_test(&eb->refs))
3090 for (index = 1; index < i; index++)
3091 page_cache_release(extent_buffer_page(eb, index));
3092 page_cache_release(extent_buffer_page(eb, 0));
3093 __free_extent_buffer(eb);
3097 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3098 u64 start, unsigned long len,
3101 struct extent_buffer *eb;
3103 spin_lock(&tree->buffer_lock);
3104 eb = buffer_search(tree, start);
3106 atomic_inc(&eb->refs);
3107 spin_unlock(&tree->buffer_lock);
3110 mark_page_accessed(eb->first_page);
3115 void free_extent_buffer(struct extent_buffer *eb)
3120 if (!atomic_dec_and_test(&eb->refs))
3126 int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3127 struct extent_buffer *eb)
3131 unsigned long num_pages;
3134 u64 start = eb->start;
3135 u64 end = start + eb->len - 1;
3137 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
3138 num_pages = num_extent_pages(eb->start, eb->len);
3140 for (i = 0; i < num_pages; i++) {
3141 page = extent_buffer_page(eb, i);
3142 if (!set && !PageDirty(page))
3147 set_page_extent_head(page, eb->len);
3149 set_page_private(page, EXTENT_PAGE_PRIVATE);
3152 * if we're on the last page or the first page and the
3153 * block isn't aligned on a page boundary, do extra checks
3154 * to make sure we don't clean page that is partially dirty
3156 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3157 ((i == num_pages - 1) &&
3158 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3159 start = (u64)page->index << PAGE_CACHE_SHIFT;
3160 end = start + PAGE_CACHE_SIZE - 1;
3161 if (test_range_bit(tree, start, end,
3167 clear_page_dirty_for_io(page);
3168 spin_lock_irq(&page->mapping->tree_lock);
3169 if (!PageDirty(page)) {
3170 radix_tree_tag_clear(&page->mapping->page_tree,
3172 PAGECACHE_TAG_DIRTY);
3174 spin_unlock_irq(&page->mapping->tree_lock);
3180 int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3181 struct extent_buffer *eb)
3183 return wait_on_extent_writeback(tree, eb->start,
3184 eb->start + eb->len - 1);
3187 int set_extent_buffer_dirty(struct extent_io_tree *tree,
3188 struct extent_buffer *eb)
3191 unsigned long num_pages;
3193 num_pages = num_extent_pages(eb->start, eb->len);
3194 for (i = 0; i < num_pages; i++) {
3195 struct page *page = extent_buffer_page(eb, i);
3196 /* writepage may need to do something special for the
3197 * first page, we have to make sure page->private is
3198 * properly set. releasepage may drop page->private
3199 * on us if the page isn't already dirty.
3203 set_page_extent_head(page, eb->len);
3204 } else if (PagePrivate(page) &&
3205 page->private != EXTENT_PAGE_PRIVATE) {
3206 set_page_extent_mapped(page);
3208 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
3209 set_extent_dirty(tree, page_offset(page),
3210 page_offset(page) + PAGE_CACHE_SIZE - 1,
3217 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3218 struct extent_buffer *eb)
3222 unsigned long num_pages;
3224 num_pages = num_extent_pages(eb->start, eb->len);
3225 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3227 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3229 for (i = 0; i < num_pages; i++) {
3230 page = extent_buffer_page(eb, i);
3232 ClearPageUptodate(page);
3237 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3238 struct extent_buffer *eb)
3242 unsigned long num_pages;
3244 num_pages = num_extent_pages(eb->start, eb->len);
3246 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3248 for (i = 0; i < num_pages; i++) {
3249 page = extent_buffer_page(eb, i);
3250 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3251 ((i == num_pages - 1) &&
3252 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3253 check_page_uptodate(tree, page);
3256 SetPageUptodate(page);
3261 int extent_range_uptodate(struct extent_io_tree *tree,
3266 int pg_uptodate = 1;
3268 unsigned long index;
3270 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3273 while (start <= end) {
3274 index = start >> PAGE_CACHE_SHIFT;
3275 page = find_get_page(tree->mapping, index);
3276 uptodate = PageUptodate(page);
3277 page_cache_release(page);
3282 start += PAGE_CACHE_SIZE;
3287 int extent_buffer_uptodate(struct extent_io_tree *tree,
3288 struct extent_buffer *eb)
3291 unsigned long num_pages;
3294 int pg_uptodate = 1;
3296 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3299 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3300 EXTENT_UPTODATE, 1);
3304 num_pages = num_extent_pages(eb->start, eb->len);
3305 for (i = 0; i < num_pages; i++) {
3306 page = extent_buffer_page(eb, i);
3307 if (!PageUptodate(page)) {
3315 int read_extent_buffer_pages(struct extent_io_tree *tree,
3316 struct extent_buffer *eb,
3317 u64 start, int wait,
3318 get_extent_t *get_extent, int mirror_num)
3321 unsigned long start_i;
3325 int locked_pages = 0;
3326 int all_uptodate = 1;
3327 int inc_all_pages = 0;
3328 unsigned long num_pages;
3329 struct bio *bio = NULL;
3330 unsigned long bio_flags = 0;
3332 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3335 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3336 EXTENT_UPTODATE, 1)) {
3341 WARN_ON(start < eb->start);
3342 start_i = (start >> PAGE_CACHE_SHIFT) -
3343 (eb->start >> PAGE_CACHE_SHIFT);
3348 num_pages = num_extent_pages(eb->start, eb->len);
3349 for (i = start_i; i < num_pages; i++) {
3350 page = extent_buffer_page(eb, i);
3352 if (!trylock_page(page))
3358 if (!PageUptodate(page))
3363 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3367 for (i = start_i; i < num_pages; i++) {
3368 page = extent_buffer_page(eb, i);
3370 page_cache_get(page);
3371 if (!PageUptodate(page)) {
3374 ClearPageError(page);
3375 err = __extent_read_full_page(tree, page,
3377 mirror_num, &bio_flags);
3386 submit_one_bio(READ, bio, mirror_num, bio_flags);
3391 for (i = start_i; i < num_pages; i++) {
3392 page = extent_buffer_page(eb, i);
3393 wait_on_page_locked(page);
3394 if (!PageUptodate(page))
3399 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3404 while (locked_pages > 0) {
3405 page = extent_buffer_page(eb, i);
3413 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3414 unsigned long start,
3421 char *dst = (char *)dstv;
3422 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3423 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3425 WARN_ON(start > eb->len);
3426 WARN_ON(start + len > eb->start + eb->len);
3428 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3431 page = extent_buffer_page(eb, i);
3433 cur = min(len, (PAGE_CACHE_SIZE - offset));
3434 kaddr = kmap_atomic(page, KM_USER1);
3435 memcpy(dst, kaddr + offset, cur);
3436 kunmap_atomic(kaddr, KM_USER1);
3445 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3446 unsigned long min_len, char **token, char **map,
3447 unsigned long *map_start,
3448 unsigned long *map_len, int km)
3450 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3453 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3454 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3455 unsigned long end_i = (start_offset + start + min_len - 1) >>
3462 offset = start_offset;
3466 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3469 if (start + min_len > eb->len) {
3470 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3471 "wanted %lu %lu\n", (unsigned long long)eb->start,
3472 eb->len, start, min_len);
3476 p = extent_buffer_page(eb, i);
3477 kaddr = kmap_atomic(p, km);
3479 *map = kaddr + offset;
3480 *map_len = PAGE_CACHE_SIZE - offset;
3484 int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3485 unsigned long min_len,
3486 char **token, char **map,
3487 unsigned long *map_start,
3488 unsigned long *map_len, int km)
3492 if (eb->map_token) {
3493 unmap_extent_buffer(eb, eb->map_token, km);
3494 eb->map_token = NULL;
3497 err = map_private_extent_buffer(eb, start, min_len, token, map,
3498 map_start, map_len, km);
3500 eb->map_token = *token;
3502 eb->map_start = *map_start;
3503 eb->map_len = *map_len;
3508 void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3510 kunmap_atomic(token, km);
3513 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3514 unsigned long start,
3521 char *ptr = (char *)ptrv;
3522 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3523 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3526 WARN_ON(start > eb->len);
3527 WARN_ON(start + len > eb->start + eb->len);
3529 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3532 page = extent_buffer_page(eb, i);
3534 cur = min(len, (PAGE_CACHE_SIZE - offset));
3536 kaddr = kmap_atomic(page, KM_USER0);
3537 ret = memcmp(ptr, kaddr + offset, cur);
3538 kunmap_atomic(kaddr, KM_USER0);
3550 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3551 unsigned long start, unsigned long len)
3557 char *src = (char *)srcv;
3558 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3559 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3561 WARN_ON(start > eb->len);
3562 WARN_ON(start + len > eb->start + eb->len);
3564 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3567 page = extent_buffer_page(eb, i);
3568 WARN_ON(!PageUptodate(page));
3570 cur = min(len, PAGE_CACHE_SIZE - offset);
3571 kaddr = kmap_atomic(page, KM_USER1);
3572 memcpy(kaddr + offset, src, cur);
3573 kunmap_atomic(kaddr, KM_USER1);
3582 void memset_extent_buffer(struct extent_buffer *eb, char c,
3583 unsigned long start, unsigned long len)
3589 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3590 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3592 WARN_ON(start > eb->len);
3593 WARN_ON(start + len > eb->start + eb->len);
3595 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3598 page = extent_buffer_page(eb, i);
3599 WARN_ON(!PageUptodate(page));
3601 cur = min(len, PAGE_CACHE_SIZE - offset);
3602 kaddr = kmap_atomic(page, KM_USER0);
3603 memset(kaddr + offset, c, cur);
3604 kunmap_atomic(kaddr, KM_USER0);
3612 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3613 unsigned long dst_offset, unsigned long src_offset,
3616 u64 dst_len = dst->len;
3621 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3622 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3624 WARN_ON(src->len != dst_len);
3626 offset = (start_offset + dst_offset) &
3627 ((unsigned long)PAGE_CACHE_SIZE - 1);
3630 page = extent_buffer_page(dst, i);
3631 WARN_ON(!PageUptodate(page));
3633 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3635 kaddr = kmap_atomic(page, KM_USER0);
3636 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3637 kunmap_atomic(kaddr, KM_USER0);
3646 static void move_pages(struct page *dst_page, struct page *src_page,
3647 unsigned long dst_off, unsigned long src_off,
3650 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3651 if (dst_page == src_page) {
3652 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3654 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3655 char *p = dst_kaddr + dst_off + len;
3656 char *s = src_kaddr + src_off + len;
3661 kunmap_atomic(src_kaddr, KM_USER1);
3663 kunmap_atomic(dst_kaddr, KM_USER0);
3666 static void copy_pages(struct page *dst_page, struct page *src_page,
3667 unsigned long dst_off, unsigned long src_off,
3670 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3673 if (dst_page != src_page)
3674 src_kaddr = kmap_atomic(src_page, KM_USER1);
3676 src_kaddr = dst_kaddr;
3678 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3679 kunmap_atomic(dst_kaddr, KM_USER0);
3680 if (dst_page != src_page)
3681 kunmap_atomic(src_kaddr, KM_USER1);
3684 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3685 unsigned long src_offset, unsigned long len)
3688 size_t dst_off_in_page;
3689 size_t src_off_in_page;
3690 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3691 unsigned long dst_i;
3692 unsigned long src_i;
3694 if (src_offset + len > dst->len) {
3695 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3696 "len %lu dst len %lu\n", src_offset, len, dst->len);
3699 if (dst_offset + len > dst->len) {
3700 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3701 "len %lu dst len %lu\n", dst_offset, len, dst->len);
3706 dst_off_in_page = (start_offset + dst_offset) &
3707 ((unsigned long)PAGE_CACHE_SIZE - 1);
3708 src_off_in_page = (start_offset + src_offset) &
3709 ((unsigned long)PAGE_CACHE_SIZE - 1);
3711 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3712 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3714 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3716 cur = min_t(unsigned long, cur,
3717 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3719 copy_pages(extent_buffer_page(dst, dst_i),
3720 extent_buffer_page(dst, src_i),
3721 dst_off_in_page, src_off_in_page, cur);
3729 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3730 unsigned long src_offset, unsigned long len)
3733 size_t dst_off_in_page;
3734 size_t src_off_in_page;
3735 unsigned long dst_end = dst_offset + len - 1;
3736 unsigned long src_end = src_offset + len - 1;
3737 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3738 unsigned long dst_i;
3739 unsigned long src_i;
3741 if (src_offset + len > dst->len) {
3742 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3743 "len %lu len %lu\n", src_offset, len, dst->len);
3746 if (dst_offset + len > dst->len) {
3747 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3748 "len %lu len %lu\n", dst_offset, len, dst->len);
3751 if (dst_offset < src_offset) {
3752 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3756 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3757 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3759 dst_off_in_page = (start_offset + dst_end) &
3760 ((unsigned long)PAGE_CACHE_SIZE - 1);
3761 src_off_in_page = (start_offset + src_end) &
3762 ((unsigned long)PAGE_CACHE_SIZE - 1);
3764 cur = min_t(unsigned long, len, src_off_in_page + 1);
3765 cur = min(cur, dst_off_in_page + 1);
3766 move_pages(extent_buffer_page(dst, dst_i),
3767 extent_buffer_page(dst, src_i),
3768 dst_off_in_page - cur + 1,
3769 src_off_in_page - cur + 1, cur);
3777 int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3779 u64 start = page_offset(page);
3780 struct extent_buffer *eb;
3783 unsigned long num_pages;
3785 spin_lock(&tree->buffer_lock);
3786 eb = buffer_search(tree, start);
3790 if (atomic_read(&eb->refs) > 1) {
3794 /* at this point we can safely release the extent buffer */
3795 num_pages = num_extent_pages(eb->start, eb->len);
3796 for (i = 0; i < num_pages; i++)
3797 page_cache_release(extent_buffer_page(eb, i));
3798 rb_erase(&eb->rb_node, &tree->buffer);
3799 __free_extent_buffer(eb);
3801 spin_unlock(&tree->buffer_lock);