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 free_extent_state(prealloc);
421 prealloc->tree = tree;
426 * utility function to clear some bits in an extent state struct.
427 * it will optionally wake up any one waiting on this state (wake == 1), or
428 * forcibly remove the state from the tree (delete == 1).
430 * If no bits are set on the state struct after clearing things, the
431 * struct is freed and removed from the tree
433 static int clear_state_bit(struct extent_io_tree *tree,
434 struct extent_state *state, int bits, int wake,
437 int ret = state->state & bits;
439 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
440 u64 range = state->end - state->start + 1;
441 WARN_ON(range > tree->dirty_bytes);
442 tree->dirty_bytes -= range;
444 clear_state_cb(tree, state, bits);
445 state->state &= ~bits;
448 if (delete || state->state == 0) {
450 clear_state_cb(tree, state, state->state);
451 rb_erase(&state->rb_node, &tree->state);
453 free_extent_state(state);
458 merge_state(tree, state);
464 * clear some bits on a range in the tree. This may require splitting
465 * or inserting elements in the tree, so the gfp mask is used to
466 * indicate which allocations or sleeping are allowed.
468 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
469 * the given range from the tree regardless of state (ie for truncate).
471 * the range [start, end] is inclusive.
473 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
474 * bits were already set, or zero if none of the bits were already set.
476 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
477 int bits, int wake, int delete, gfp_t mask)
479 struct extent_state *state;
480 struct extent_state *prealloc = NULL;
481 struct rb_node *node;
486 if (!prealloc && (mask & __GFP_WAIT)) {
487 prealloc = alloc_extent_state(mask);
492 spin_lock(&tree->lock);
494 * this search will find the extents that end after
497 node = tree_search(tree, start);
500 state = rb_entry(node, struct extent_state, rb_node);
501 if (state->start > end)
503 WARN_ON(state->end < start);
506 * | ---- desired range ---- |
508 * | ------------- state -------------- |
510 * We need to split the extent we found, and may flip
511 * bits on second half.
513 * If the extent we found extends past our range, we
514 * just split and search again. It'll get split again
515 * the next time though.
517 * If the extent we found is inside our range, we clear
518 * the desired bit on it.
521 if (state->start < start) {
523 prealloc = alloc_extent_state(GFP_ATOMIC);
524 err = split_state(tree, state, prealloc, start);
525 BUG_ON(err == -EEXIST);
529 if (state->end <= end) {
530 start = state->end + 1;
531 set |= clear_state_bit(tree, state, bits,
534 start = state->start;
539 * | ---- desired range ---- |
541 * We need to split the extent, and clear the bit
544 if (state->start <= end && state->end > end) {
546 prealloc = alloc_extent_state(GFP_ATOMIC);
547 err = split_state(tree, state, prealloc, end + 1);
548 BUG_ON(err == -EEXIST);
552 set |= clear_state_bit(tree, prealloc, bits,
558 start = state->end + 1;
559 set |= clear_state_bit(tree, state, bits, wake, delete);
563 spin_unlock(&tree->lock);
565 free_extent_state(prealloc);
572 spin_unlock(&tree->lock);
573 if (mask & __GFP_WAIT)
578 static int wait_on_state(struct extent_io_tree *tree,
579 struct extent_state *state)
580 __releases(tree->lock)
581 __acquires(tree->lock)
584 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
585 spin_unlock(&tree->lock);
587 spin_lock(&tree->lock);
588 finish_wait(&state->wq, &wait);
593 * waits for one or more bits to clear on a range in the state tree.
594 * The range [start, end] is inclusive.
595 * The tree lock is taken by this function
597 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
599 struct extent_state *state;
600 struct rb_node *node;
602 spin_lock(&tree->lock);
606 * this search will find all the extents that end after
609 node = tree_search(tree, start);
613 state = rb_entry(node, struct extent_state, rb_node);
615 if (state->start > end)
618 if (state->state & bits) {
619 start = state->start;
620 atomic_inc(&state->refs);
621 wait_on_state(tree, state);
622 free_extent_state(state);
625 start = state->end + 1;
630 if (need_resched()) {
631 spin_unlock(&tree->lock);
633 spin_lock(&tree->lock);
637 spin_unlock(&tree->lock);
641 static void set_state_bits(struct extent_io_tree *tree,
642 struct extent_state *state,
645 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
646 u64 range = state->end - state->start + 1;
647 tree->dirty_bytes += range;
649 set_state_cb(tree, state, bits);
650 state->state |= bits;
654 * set some bits on a range in the tree. This may require allocations
655 * or sleeping, so the gfp mask is used to indicate what is allowed.
657 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
658 * range already has the desired bits set. The start of the existing
659 * range is returned in failed_start in this case.
661 * [start, end] is inclusive
662 * This takes the tree lock.
664 static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
665 int bits, int exclusive, u64 *failed_start,
668 struct extent_state *state;
669 struct extent_state *prealloc = NULL;
670 struct rb_node *node;
676 if (!prealloc && (mask & __GFP_WAIT)) {
677 prealloc = alloc_extent_state(mask);
682 spin_lock(&tree->lock);
684 * this search will find all the extents that end after
687 node = tree_search(tree, start);
689 err = insert_state(tree, prealloc, start, end, bits);
691 BUG_ON(err == -EEXIST);
695 state = rb_entry(node, struct extent_state, rb_node);
696 last_start = state->start;
697 last_end = state->end;
700 * | ---- desired range ---- |
703 * Just lock what we found and keep going
705 if (state->start == start && state->end <= end) {
706 set = state->state & bits;
707 if (set && exclusive) {
708 *failed_start = state->start;
712 set_state_bits(tree, state, bits);
713 start = state->end + 1;
714 merge_state(tree, state);
719 * | ---- desired range ---- |
722 * | ------------- state -------------- |
724 * We need to split the extent we found, and may flip bits on
727 * If the extent we found extends past our
728 * range, we just split and search again. It'll get split
729 * again the next time though.
731 * If the extent we found is inside our range, we set the
734 if (state->start < start) {
735 set = state->state & bits;
736 if (exclusive && set) {
737 *failed_start = start;
741 err = split_state(tree, state, prealloc, start);
742 BUG_ON(err == -EEXIST);
746 if (state->end <= end) {
747 set_state_bits(tree, state, bits);
748 start = state->end + 1;
749 merge_state(tree, state);
751 start = state->start;
756 * | ---- desired range ---- |
757 * | state | or | state |
759 * There's a hole, we need to insert something in it and
760 * ignore the extent we found.
762 if (state->start > start) {
764 if (end < last_start)
767 this_end = last_start - 1;
768 err = insert_state(tree, prealloc, start, this_end,
771 BUG_ON(err == -EEXIST);
774 start = this_end + 1;
778 * | ---- desired range ---- |
780 * We need to split the extent, and set the bit
783 if (state->start <= end && state->end > end) {
784 set = state->state & bits;
785 if (exclusive && set) {
786 *failed_start = start;
790 err = split_state(tree, state, prealloc, end + 1);
791 BUG_ON(err == -EEXIST);
793 set_state_bits(tree, prealloc, bits);
794 merge_state(tree, prealloc);
802 spin_unlock(&tree->lock);
804 free_extent_state(prealloc);
811 spin_unlock(&tree->lock);
812 if (mask & __GFP_WAIT)
817 /* wrappers around set/clear extent bit */
818 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
821 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
825 int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
828 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask);
831 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
832 int bits, gfp_t mask)
834 return set_extent_bit(tree, start, end, bits, 0, NULL,
838 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
839 int bits, gfp_t mask)
841 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
844 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
847 return set_extent_bit(tree, start, end,
848 EXTENT_DELALLOC | EXTENT_DIRTY,
852 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
855 return clear_extent_bit(tree, start, end,
856 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
859 int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
862 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask);
865 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
868 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
872 static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
875 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
878 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
881 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
885 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
888 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
891 static int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
894 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
898 static int clear_extent_writeback(struct extent_io_tree *tree, u64 start,
901 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
904 int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
906 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
910 * either insert or lock state struct between start and end use mask to tell
911 * us if waiting is desired.
913 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
918 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
919 &failed_start, mask);
920 if (err == -EEXIST && (mask & __GFP_WAIT)) {
921 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
922 start = failed_start;
926 WARN_ON(start > end);
931 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
937 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
938 &failed_start, mask);
939 if (err == -EEXIST) {
940 if (failed_start > start)
941 clear_extent_bit(tree, start, failed_start - 1,
942 EXTENT_LOCKED, 1, 0, mask);
948 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
951 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
955 * helper function to set pages and extents in the tree dirty
957 int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
959 unsigned long index = start >> PAGE_CACHE_SHIFT;
960 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
963 while (index <= end_index) {
964 page = find_get_page(tree->mapping, index);
966 __set_page_dirty_nobuffers(page);
967 page_cache_release(page);
970 set_extent_dirty(tree, start, end, GFP_NOFS);
975 * helper function to set both pages and extents in the tree writeback
977 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
979 unsigned long index = start >> PAGE_CACHE_SHIFT;
980 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
983 while (index <= end_index) {
984 page = find_get_page(tree->mapping, index);
986 set_page_writeback(page);
987 page_cache_release(page);
990 set_extent_writeback(tree, start, end, GFP_NOFS);
995 * find the first offset in the io tree with 'bits' set. zero is
996 * returned if we find something, and *start_ret and *end_ret are
997 * set to reflect the state struct that was found.
999 * If nothing was found, 1 is returned, < 0 on error
1001 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1002 u64 *start_ret, u64 *end_ret, int bits)
1004 struct rb_node *node;
1005 struct extent_state *state;
1008 spin_lock(&tree->lock);
1010 * this search will find all the extents that end after
1013 node = tree_search(tree, start);
1018 state = rb_entry(node, struct extent_state, rb_node);
1019 if (state->end >= start && (state->state & bits)) {
1020 *start_ret = state->start;
1021 *end_ret = state->end;
1025 node = rb_next(node);
1030 spin_unlock(&tree->lock);
1034 /* find the first state struct with 'bits' set after 'start', and
1035 * return it. tree->lock must be held. NULL will returned if
1036 * nothing was found after 'start'
1038 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1039 u64 start, int bits)
1041 struct rb_node *node;
1042 struct extent_state *state;
1045 * this search will find all the extents that end after
1048 node = tree_search(tree, start);
1053 state = rb_entry(node, struct extent_state, rb_node);
1054 if (state->end >= start && (state->state & bits))
1057 node = rb_next(node);
1066 * find a contiguous range of bytes in the file marked as delalloc, not
1067 * more than 'max_bytes'. start and end are used to return the range,
1069 * 1 is returned if we find something, 0 if nothing was in the tree
1071 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1072 u64 *start, u64 *end, u64 max_bytes)
1074 struct rb_node *node;
1075 struct extent_state *state;
1076 u64 cur_start = *start;
1078 u64 total_bytes = 0;
1080 spin_lock(&tree->lock);
1083 * this search will find all the extents that end after
1086 node = tree_search(tree, cur_start);
1094 state = rb_entry(node, struct extent_state, rb_node);
1095 if (found && (state->start != cur_start ||
1096 (state->state & EXTENT_BOUNDARY))) {
1099 if (!(state->state & EXTENT_DELALLOC)) {
1105 *start = state->start;
1108 cur_start = state->end + 1;
1109 node = rb_next(node);
1112 total_bytes += state->end - state->start + 1;
1113 if (total_bytes >= max_bytes)
1117 spin_unlock(&tree->lock);
1121 static noinline int __unlock_for_delalloc(struct inode *inode,
1122 struct page *locked_page,
1126 struct page *pages[16];
1127 unsigned long index = start >> PAGE_CACHE_SHIFT;
1128 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1129 unsigned long nr_pages = end_index - index + 1;
1132 if (index == locked_page->index && end_index == index)
1135 while (nr_pages > 0) {
1136 ret = find_get_pages_contig(inode->i_mapping, index,
1137 min_t(unsigned long, nr_pages,
1138 ARRAY_SIZE(pages)), pages);
1139 for (i = 0; i < ret; i++) {
1140 if (pages[i] != locked_page)
1141 unlock_page(pages[i]);
1142 page_cache_release(pages[i]);
1151 static noinline int lock_delalloc_pages(struct inode *inode,
1152 struct page *locked_page,
1156 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1157 unsigned long start_index = index;
1158 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1159 unsigned long pages_locked = 0;
1160 struct page *pages[16];
1161 unsigned long nrpages;
1165 /* the caller is responsible for locking the start index */
1166 if (index == locked_page->index && index == end_index)
1169 /* skip the page at the start index */
1170 nrpages = end_index - index + 1;
1171 while (nrpages > 0) {
1172 ret = find_get_pages_contig(inode->i_mapping, index,
1173 min_t(unsigned long,
1174 nrpages, ARRAY_SIZE(pages)), pages);
1179 /* now we have an array of pages, lock them all */
1180 for (i = 0; i < ret; i++) {
1182 * the caller is taking responsibility for
1185 if (pages[i] != locked_page) {
1186 lock_page(pages[i]);
1187 if (!PageDirty(pages[i]) ||
1188 pages[i]->mapping != inode->i_mapping) {
1190 unlock_page(pages[i]);
1191 page_cache_release(pages[i]);
1195 page_cache_release(pages[i]);
1204 if (ret && pages_locked) {
1205 __unlock_for_delalloc(inode, locked_page,
1207 ((u64)(start_index + pages_locked - 1)) <<
1214 * find a contiguous range of bytes in the file marked as delalloc, not
1215 * more than 'max_bytes'. start and end are used to return the range,
1217 * 1 is returned if we find something, 0 if nothing was in the tree
1219 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1220 struct extent_io_tree *tree,
1221 struct page *locked_page,
1222 u64 *start, u64 *end,
1232 /* step one, find a bunch of delalloc bytes starting at start */
1233 delalloc_start = *start;
1235 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1237 if (!found || delalloc_end <= *start) {
1238 *start = delalloc_start;
1239 *end = delalloc_end;
1244 * start comes from the offset of locked_page. We have to lock
1245 * pages in order, so we can't process delalloc bytes before
1248 if (delalloc_start < *start)
1249 delalloc_start = *start;
1252 * make sure to limit the number of pages we try to lock down
1255 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1256 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1258 /* step two, lock all the pages after the page that has start */
1259 ret = lock_delalloc_pages(inode, locked_page,
1260 delalloc_start, delalloc_end);
1261 if (ret == -EAGAIN) {
1262 /* some of the pages are gone, lets avoid looping by
1263 * shortening the size of the delalloc range we're searching
1266 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1267 max_bytes = PAGE_CACHE_SIZE - offset;
1277 /* step three, lock the state bits for the whole range */
1278 lock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1280 /* then test to make sure it is all still delalloc */
1281 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1282 EXTENT_DELALLOC, 1);
1284 unlock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1285 __unlock_for_delalloc(inode, locked_page,
1286 delalloc_start, delalloc_end);
1290 *start = delalloc_start;
1291 *end = delalloc_end;
1296 int extent_clear_unlock_delalloc(struct inode *inode,
1297 struct extent_io_tree *tree,
1298 u64 start, u64 end, struct page *locked_page,
1301 int clear_delalloc, int clear_dirty,
1306 struct page *pages[16];
1307 unsigned long index = start >> PAGE_CACHE_SHIFT;
1308 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1309 unsigned long nr_pages = end_index - index + 1;
1314 clear_bits |= EXTENT_LOCKED;
1316 clear_bits |= EXTENT_DIRTY;
1319 clear_bits |= EXTENT_DELALLOC;
1321 clear_extent_bit(tree, start, end, clear_bits, 1, 0, GFP_NOFS);
1322 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback))
1325 while (nr_pages > 0) {
1326 ret = find_get_pages_contig(inode->i_mapping, index,
1327 min_t(unsigned long,
1328 nr_pages, ARRAY_SIZE(pages)), pages);
1329 for (i = 0; i < ret; i++) {
1330 if (pages[i] == locked_page) {
1331 page_cache_release(pages[i]);
1335 clear_page_dirty_for_io(pages[i]);
1337 set_page_writeback(pages[i]);
1339 end_page_writeback(pages[i]);
1341 unlock_page(pages[i]);
1342 page_cache_release(pages[i]);
1352 * count the number of bytes in the tree that have a given bit(s)
1353 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1354 * cached. The total number found is returned.
1356 u64 count_range_bits(struct extent_io_tree *tree,
1357 u64 *start, u64 search_end, u64 max_bytes,
1360 struct rb_node *node;
1361 struct extent_state *state;
1362 u64 cur_start = *start;
1363 u64 total_bytes = 0;
1366 if (search_end <= cur_start) {
1371 spin_lock(&tree->lock);
1372 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1373 total_bytes = tree->dirty_bytes;
1377 * this search will find all the extents that end after
1380 node = tree_search(tree, cur_start);
1385 state = rb_entry(node, struct extent_state, rb_node);
1386 if (state->start > search_end)
1388 if (state->end >= cur_start && (state->state & bits)) {
1389 total_bytes += min(search_end, state->end) + 1 -
1390 max(cur_start, state->start);
1391 if (total_bytes >= max_bytes)
1394 *start = state->start;
1398 node = rb_next(node);
1403 spin_unlock(&tree->lock);
1409 * helper function to lock both pages and extents in the tree.
1410 * pages must be locked first.
1412 static int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
1414 unsigned long index = start >> PAGE_CACHE_SHIFT;
1415 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1419 while (index <= end_index) {
1420 page = grab_cache_page(tree->mapping, index);
1426 err = PTR_ERR(page);
1431 lock_extent(tree, start, end, GFP_NOFS);
1436 * we failed above in getting the page at 'index', so we undo here
1437 * up to but not including the page at 'index'
1440 index = start >> PAGE_CACHE_SHIFT;
1441 while (index < end_index) {
1442 page = find_get_page(tree->mapping, index);
1444 page_cache_release(page);
1451 * helper function to unlock both pages and extents in the tree.
1453 static int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
1455 unsigned long index = start >> PAGE_CACHE_SHIFT;
1456 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1459 while (index <= end_index) {
1460 page = find_get_page(tree->mapping, index);
1462 page_cache_release(page);
1465 unlock_extent(tree, start, end, GFP_NOFS);
1471 * set the private field for a given byte offset in the tree. If there isn't
1472 * an extent_state there already, this does nothing.
1474 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1476 struct rb_node *node;
1477 struct extent_state *state;
1480 spin_lock(&tree->lock);
1482 * this search will find all the extents that end after
1485 node = tree_search(tree, start);
1490 state = rb_entry(node, struct extent_state, rb_node);
1491 if (state->start != start) {
1495 state->private = private;
1497 spin_unlock(&tree->lock);
1501 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1503 struct rb_node *node;
1504 struct extent_state *state;
1507 spin_lock(&tree->lock);
1509 * this search will find all the extents that end after
1512 node = tree_search(tree, start);
1517 state = rb_entry(node, struct extent_state, rb_node);
1518 if (state->start != start) {
1522 *private = state->private;
1524 spin_unlock(&tree->lock);
1529 * searches a range in the state tree for a given mask.
1530 * If 'filled' == 1, this returns 1 only if every extent in the tree
1531 * has the bits set. Otherwise, 1 is returned if any bit in the
1532 * range is found set.
1534 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1535 int bits, int filled)
1537 struct extent_state *state = NULL;
1538 struct rb_node *node;
1541 spin_lock(&tree->lock);
1542 node = tree_search(tree, start);
1543 while (node && start <= end) {
1544 state = rb_entry(node, struct extent_state, rb_node);
1546 if (filled && state->start > start) {
1551 if (state->start > end)
1554 if (state->state & bits) {
1558 } else if (filled) {
1562 start = state->end + 1;
1565 node = rb_next(node);
1572 spin_unlock(&tree->lock);
1577 * helper function to set a given page up to date if all the
1578 * extents in the tree for that page are up to date
1580 static int check_page_uptodate(struct extent_io_tree *tree,
1583 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1584 u64 end = start + PAGE_CACHE_SIZE - 1;
1585 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1586 SetPageUptodate(page);
1591 * helper function to unlock a page if all the extents in the tree
1592 * for that page are unlocked
1594 static int check_page_locked(struct extent_io_tree *tree,
1597 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1598 u64 end = start + PAGE_CACHE_SIZE - 1;
1599 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1605 * helper function to end page writeback if all the extents
1606 * in the tree for that page are done with writeback
1608 static int check_page_writeback(struct extent_io_tree *tree,
1611 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1612 u64 end = start + PAGE_CACHE_SIZE - 1;
1613 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1614 end_page_writeback(page);
1618 /* lots and lots of room for performance fixes in the end_bio funcs */
1621 * after a writepage IO is done, we need to:
1622 * clear the uptodate bits on error
1623 * clear the writeback bits in the extent tree for this IO
1624 * end_page_writeback if the page has no more pending IO
1626 * Scheduling is not allowed, so the extent state tree is expected
1627 * to have one and only one object corresponding to this IO.
1629 static void end_bio_extent_writepage(struct bio *bio, int err)
1631 int uptodate = err == 0;
1632 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1633 struct extent_io_tree *tree;
1640 struct page *page = bvec->bv_page;
1641 tree = &BTRFS_I(page->mapping->host)->io_tree;
1643 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1645 end = start + bvec->bv_len - 1;
1647 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1652 if (--bvec >= bio->bi_io_vec)
1653 prefetchw(&bvec->bv_page->flags);
1654 if (tree->ops && tree->ops->writepage_end_io_hook) {
1655 ret = tree->ops->writepage_end_io_hook(page, start,
1656 end, NULL, uptodate);
1661 if (!uptodate && tree->ops &&
1662 tree->ops->writepage_io_failed_hook) {
1663 ret = tree->ops->writepage_io_failed_hook(bio, page,
1666 uptodate = (err == 0);
1672 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1673 ClearPageUptodate(page);
1677 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
1680 end_page_writeback(page);
1682 check_page_writeback(tree, page);
1683 } while (bvec >= bio->bi_io_vec);
1689 * after a readpage IO is done, we need to:
1690 * clear the uptodate bits on error
1691 * set the uptodate bits if things worked
1692 * set the page up to date if all extents in the tree are uptodate
1693 * clear the lock bit in the extent tree
1694 * unlock the page if there are no other extents locked for it
1696 * Scheduling is not allowed, so the extent state tree is expected
1697 * to have one and only one object corresponding to this IO.
1699 static void end_bio_extent_readpage(struct bio *bio, int err)
1701 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1702 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1703 struct extent_io_tree *tree;
1713 struct page *page = bvec->bv_page;
1714 tree = &BTRFS_I(page->mapping->host)->io_tree;
1716 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1718 end = start + bvec->bv_len - 1;
1720 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1725 if (--bvec >= bio->bi_io_vec)
1726 prefetchw(&bvec->bv_page->flags);
1728 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1729 ret = tree->ops->readpage_end_io_hook(page, start, end,
1734 if (!uptodate && tree->ops &&
1735 tree->ops->readpage_io_failed_hook) {
1736 ret = tree->ops->readpage_io_failed_hook(bio, page,
1740 test_bit(BIO_UPTODATE, &bio->bi_flags);
1748 set_extent_uptodate(tree, start, end,
1751 unlock_extent(tree, start, end, GFP_ATOMIC);
1755 SetPageUptodate(page);
1757 ClearPageUptodate(page);
1763 check_page_uptodate(tree, page);
1765 ClearPageUptodate(page);
1768 check_page_locked(tree, page);
1770 } while (bvec >= bio->bi_io_vec);
1776 * IO done from prepare_write is pretty simple, we just unlock
1777 * the structs in the extent tree when done, and set the uptodate bits
1780 static void end_bio_extent_preparewrite(struct bio *bio, int err)
1782 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1783 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1784 struct extent_io_tree *tree;
1789 struct page *page = bvec->bv_page;
1790 tree = &BTRFS_I(page->mapping->host)->io_tree;
1792 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1794 end = start + bvec->bv_len - 1;
1796 if (--bvec >= bio->bi_io_vec)
1797 prefetchw(&bvec->bv_page->flags);
1800 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1802 ClearPageUptodate(page);
1806 unlock_extent(tree, start, end, GFP_ATOMIC);
1808 } while (bvec >= bio->bi_io_vec);
1814 extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1819 bio = bio_alloc(gfp_flags, nr_vecs);
1821 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1822 while (!bio && (nr_vecs /= 2))
1823 bio = bio_alloc(gfp_flags, nr_vecs);
1828 bio->bi_bdev = bdev;
1829 bio->bi_sector = first_sector;
1834 static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1835 unsigned long bio_flags)
1838 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1839 struct page *page = bvec->bv_page;
1840 struct extent_io_tree *tree = bio->bi_private;
1844 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1845 end = start + bvec->bv_len - 1;
1847 bio->bi_private = NULL;
1851 if (tree->ops && tree->ops->submit_bio_hook)
1852 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1853 mirror_num, bio_flags);
1855 submit_bio(rw, bio);
1856 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1862 static int submit_extent_page(int rw, struct extent_io_tree *tree,
1863 struct page *page, sector_t sector,
1864 size_t size, unsigned long offset,
1865 struct block_device *bdev,
1866 struct bio **bio_ret,
1867 unsigned long max_pages,
1868 bio_end_io_t end_io_func,
1870 unsigned long prev_bio_flags,
1871 unsigned long bio_flags)
1877 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1878 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
1879 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
1881 if (bio_ret && *bio_ret) {
1884 contig = bio->bi_sector == sector;
1886 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1889 if (prev_bio_flags != bio_flags || !contig ||
1890 (tree->ops && tree->ops->merge_bio_hook &&
1891 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1893 bio_add_page(bio, page, page_size, offset) < page_size) {
1894 ret = submit_one_bio(rw, bio, mirror_num,
1901 if (this_compressed)
1904 nr = bio_get_nr_vecs(bdev);
1906 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1908 bio_add_page(bio, page, page_size, offset);
1909 bio->bi_end_io = end_io_func;
1910 bio->bi_private = tree;
1915 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
1920 void set_page_extent_mapped(struct page *page)
1922 if (!PagePrivate(page)) {
1923 SetPagePrivate(page);
1924 page_cache_get(page);
1925 set_page_private(page, EXTENT_PAGE_PRIVATE);
1929 static void set_page_extent_head(struct page *page, unsigned long len)
1931 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1935 * basic readpage implementation. Locked extent state structs are inserted
1936 * into the tree that are removed when the IO is done (by the end_io
1939 static int __extent_read_full_page(struct extent_io_tree *tree,
1941 get_extent_t *get_extent,
1942 struct bio **bio, int mirror_num,
1943 unsigned long *bio_flags)
1945 struct inode *inode = page->mapping->host;
1946 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1947 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1951 u64 last_byte = i_size_read(inode);
1955 struct extent_map *em;
1956 struct block_device *bdev;
1959 size_t page_offset = 0;
1961 size_t disk_io_size;
1962 size_t blocksize = inode->i_sb->s_blocksize;
1963 unsigned long this_bio_flag = 0;
1965 set_page_extent_mapped(page);
1968 lock_extent(tree, start, end, GFP_NOFS);
1970 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1972 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1975 iosize = PAGE_CACHE_SIZE - zero_offset;
1976 userpage = kmap_atomic(page, KM_USER0);
1977 memset(userpage + zero_offset, 0, iosize);
1978 flush_dcache_page(page);
1979 kunmap_atomic(userpage, KM_USER0);
1982 while (cur <= end) {
1983 if (cur >= last_byte) {
1985 iosize = PAGE_CACHE_SIZE - page_offset;
1986 userpage = kmap_atomic(page, KM_USER0);
1987 memset(userpage + page_offset, 0, iosize);
1988 flush_dcache_page(page);
1989 kunmap_atomic(userpage, KM_USER0);
1990 set_extent_uptodate(tree, cur, cur + iosize - 1,
1992 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1995 em = get_extent(inode, page, page_offset, cur,
1997 if (IS_ERR(em) || !em) {
1999 unlock_extent(tree, cur, end, GFP_NOFS);
2002 extent_offset = cur - em->start;
2003 BUG_ON(extent_map_end(em) <= cur);
2006 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2007 this_bio_flag = EXTENT_BIO_COMPRESSED;
2009 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2010 cur_end = min(extent_map_end(em) - 1, end);
2011 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2012 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2013 disk_io_size = em->block_len;
2014 sector = em->block_start >> 9;
2016 sector = (em->block_start + extent_offset) >> 9;
2017 disk_io_size = iosize;
2020 block_start = em->block_start;
2021 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2022 block_start = EXTENT_MAP_HOLE;
2023 free_extent_map(em);
2026 /* we've found a hole, just zero and go on */
2027 if (block_start == EXTENT_MAP_HOLE) {
2029 userpage = kmap_atomic(page, KM_USER0);
2030 memset(userpage + page_offset, 0, iosize);
2031 flush_dcache_page(page);
2032 kunmap_atomic(userpage, KM_USER0);
2034 set_extent_uptodate(tree, cur, cur + iosize - 1,
2036 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2038 page_offset += iosize;
2041 /* the get_extent function already copied into the page */
2042 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
2043 check_page_uptodate(tree, page);
2044 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2046 page_offset += iosize;
2049 /* we have an inline extent but it didn't get marked up
2050 * to date. Error out
2052 if (block_start == EXTENT_MAP_INLINE) {
2054 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2056 page_offset += iosize;
2061 if (tree->ops && tree->ops->readpage_io_hook) {
2062 ret = tree->ops->readpage_io_hook(page, cur,
2066 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2068 ret = submit_extent_page(READ, tree, page,
2069 sector, disk_io_size, page_offset,
2071 end_bio_extent_readpage, mirror_num,
2075 *bio_flags = this_bio_flag;
2080 page_offset += iosize;
2083 if (!PageError(page))
2084 SetPageUptodate(page);
2090 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2091 get_extent_t *get_extent)
2093 struct bio *bio = NULL;
2094 unsigned long bio_flags = 0;
2097 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2100 submit_one_bio(READ, bio, 0, bio_flags);
2105 * the writepage semantics are similar to regular writepage. extent
2106 * records are inserted to lock ranges in the tree, and as dirty areas
2107 * are found, they are marked writeback. Then the lock bits are removed
2108 * and the end_io handler clears the writeback ranges
2110 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2113 struct inode *inode = page->mapping->host;
2114 struct extent_page_data *epd = data;
2115 struct extent_io_tree *tree = epd->tree;
2116 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2118 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2122 u64 last_byte = i_size_read(inode);
2127 struct extent_map *em;
2128 struct block_device *bdev;
2131 size_t pg_offset = 0;
2133 loff_t i_size = i_size_read(inode);
2134 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2139 unsigned long nr_written = 0;
2141 WARN_ON(!PageLocked(page));
2142 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2143 if (page->index > end_index ||
2144 (page->index == end_index && !pg_offset)) {
2145 page->mapping->a_ops->invalidatepage(page, 0);
2150 if (page->index == end_index) {
2153 userpage = kmap_atomic(page, KM_USER0);
2154 memset(userpage + pg_offset, 0,
2155 PAGE_CACHE_SIZE - pg_offset);
2156 kunmap_atomic(userpage, KM_USER0);
2157 flush_dcache_page(page);
2161 set_page_extent_mapped(page);
2163 delalloc_start = start;
2166 if (!epd->extent_locked) {
2167 while (delalloc_end < page_end) {
2168 nr_delalloc = find_lock_delalloc_range(inode, tree,
2173 if (nr_delalloc == 0) {
2174 delalloc_start = delalloc_end + 1;
2177 tree->ops->fill_delalloc(inode, page, delalloc_start,
2178 delalloc_end, &page_started,
2180 delalloc_start = delalloc_end + 1;
2183 /* did the fill delalloc function already unlock and start
2188 goto update_nr_written;
2191 lock_extent(tree, start, page_end, GFP_NOFS);
2193 unlock_start = start;
2195 if (tree->ops && tree->ops->writepage_start_hook) {
2196 ret = tree->ops->writepage_start_hook(page, start,
2198 if (ret == -EAGAIN) {
2199 unlock_extent(tree, start, page_end, GFP_NOFS);
2200 redirty_page_for_writepage(wbc, page);
2203 goto update_nr_written;
2210 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0))
2211 printk(KERN_ERR "btrfs delalloc bits after lock_extent\n");
2213 if (last_byte <= start) {
2214 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
2215 unlock_extent(tree, start, page_end, GFP_NOFS);
2216 if (tree->ops && tree->ops->writepage_end_io_hook)
2217 tree->ops->writepage_end_io_hook(page, start,
2219 unlock_start = page_end + 1;
2223 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2224 blocksize = inode->i_sb->s_blocksize;
2226 while (cur <= end) {
2227 if (cur >= last_byte) {
2228 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
2229 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2230 if (tree->ops && tree->ops->writepage_end_io_hook)
2231 tree->ops->writepage_end_io_hook(page, cur,
2233 unlock_start = page_end + 1;
2236 em = epd->get_extent(inode, page, pg_offset, cur,
2238 if (IS_ERR(em) || !em) {
2243 extent_offset = cur - em->start;
2244 BUG_ON(extent_map_end(em) <= cur);
2246 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2247 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2248 sector = (em->block_start + extent_offset) >> 9;
2250 block_start = em->block_start;
2251 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2252 free_extent_map(em);
2256 * compressed and inline extents are written through other
2259 if (compressed || block_start == EXTENT_MAP_HOLE ||
2260 block_start == EXTENT_MAP_INLINE) {
2261 clear_extent_dirty(tree, cur,
2262 cur + iosize - 1, GFP_NOFS);
2264 unlock_extent(tree, unlock_start, cur + iosize - 1,
2268 * end_io notification does not happen here for
2269 * compressed extents
2271 if (!compressed && tree->ops &&
2272 tree->ops->writepage_end_io_hook)
2273 tree->ops->writepage_end_io_hook(page, cur,
2276 else if (compressed) {
2277 /* we don't want to end_page_writeback on
2278 * a compressed extent. this happens
2285 pg_offset += iosize;
2289 /* leave this out until we have a page_mkwrite call */
2290 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2293 pg_offset += iosize;
2297 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2298 if (tree->ops && tree->ops->writepage_io_hook) {
2299 ret = tree->ops->writepage_io_hook(page, cur,
2307 unsigned long max_nr = end_index + 1;
2309 set_range_writeback(tree, cur, cur + iosize - 1);
2310 if (!PageWriteback(page)) {
2311 printk(KERN_ERR "btrfs warning page %lu not "
2312 "writeback, cur %llu end %llu\n",
2313 page->index, (unsigned long long)cur,
2314 (unsigned long long)end);
2317 ret = submit_extent_page(WRITE, tree, page, sector,
2318 iosize, pg_offset, bdev,
2320 end_bio_extent_writepage,
2326 pg_offset += iosize;
2331 /* make sure the mapping tag for page dirty gets cleared */
2332 set_page_writeback(page);
2333 end_page_writeback(page);
2335 if (unlock_start <= page_end)
2336 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2340 wbc->nr_to_write -= nr_written;
2341 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2342 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2343 page->mapping->writeback_index = page->index + nr_written;
2348 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2349 * @mapping: address space structure to write
2350 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2351 * @writepage: function called for each page
2352 * @data: data passed to writepage function
2354 * If a page is already under I/O, write_cache_pages() skips it, even
2355 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2356 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2357 * and msync() need to guarantee that all the data which was dirty at the time
2358 * the call was made get new I/O started against them. If wbc->sync_mode is
2359 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2360 * existing IO to complete.
2362 static int extent_write_cache_pages(struct extent_io_tree *tree,
2363 struct address_space *mapping,
2364 struct writeback_control *wbc,
2365 writepage_t writepage, void *data,
2366 void (*flush_fn)(void *))
2368 struct backing_dev_info *bdi = mapping->backing_dev_info;
2371 struct pagevec pvec;
2374 pgoff_t end; /* Inclusive */
2376 int range_whole = 0;
2378 pagevec_init(&pvec, 0);
2379 if (wbc->range_cyclic) {
2380 index = mapping->writeback_index; /* Start from prev offset */
2383 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2384 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2385 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2390 while (!done && (index <= end) &&
2391 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2392 PAGECACHE_TAG_DIRTY, min(end - index,
2393 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2397 for (i = 0; i < nr_pages; i++) {
2398 struct page *page = pvec.pages[i];
2401 * At this point we hold neither mapping->tree_lock nor
2402 * lock on the page itself: the page may be truncated or
2403 * invalidated (changing page->mapping to NULL), or even
2404 * swizzled back from swapper_space to tmpfs file
2407 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2408 tree->ops->write_cache_pages_lock_hook(page);
2412 if (unlikely(page->mapping != mapping)) {
2417 if (!wbc->range_cyclic && page->index > end) {
2423 if (wbc->sync_mode != WB_SYNC_NONE) {
2424 if (PageWriteback(page))
2426 wait_on_page_writeback(page);
2429 if (PageWriteback(page) ||
2430 !clear_page_dirty_for_io(page)) {
2435 ret = (*writepage)(page, wbc, data);
2437 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2441 if (ret || wbc->nr_to_write <= 0)
2443 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2444 wbc->encountered_congestion = 1;
2448 pagevec_release(&pvec);
2451 if (!scanned && !done) {
2453 * We hit the last page and there is more work to be done: wrap
2454 * back to the start of the file
2463 static noinline void flush_write_bio(void *data)
2465 struct extent_page_data *epd = data;
2467 submit_one_bio(WRITE, epd->bio, 0, 0);
2472 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2473 get_extent_t *get_extent,
2474 struct writeback_control *wbc)
2477 struct address_space *mapping = page->mapping;
2478 struct extent_page_data epd = {
2481 .get_extent = get_extent,
2484 struct writeback_control wbc_writepages = {
2486 .sync_mode = WB_SYNC_NONE,
2487 .older_than_this = NULL,
2489 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2490 .range_end = (loff_t)-1,
2494 ret = __extent_writepage(page, wbc, &epd);
2496 extent_write_cache_pages(tree, mapping, &wbc_writepages,
2497 __extent_writepage, &epd, flush_write_bio);
2499 submit_one_bio(WRITE, epd.bio, 0, 0);
2503 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2504 u64 start, u64 end, get_extent_t *get_extent,
2508 struct address_space *mapping = inode->i_mapping;
2510 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2513 struct extent_page_data epd = {
2516 .get_extent = get_extent,
2519 struct writeback_control wbc_writepages = {
2520 .bdi = inode->i_mapping->backing_dev_info,
2522 .older_than_this = NULL,
2523 .nr_to_write = nr_pages * 2,
2524 .range_start = start,
2525 .range_end = end + 1,
2528 while (start <= end) {
2529 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2530 if (clear_page_dirty_for_io(page))
2531 ret = __extent_writepage(page, &wbc_writepages, &epd);
2533 if (tree->ops && tree->ops->writepage_end_io_hook)
2534 tree->ops->writepage_end_io_hook(page, start,
2535 start + PAGE_CACHE_SIZE - 1,
2539 page_cache_release(page);
2540 start += PAGE_CACHE_SIZE;
2544 submit_one_bio(WRITE, epd.bio, 0, 0);
2548 int extent_writepages(struct extent_io_tree *tree,
2549 struct address_space *mapping,
2550 get_extent_t *get_extent,
2551 struct writeback_control *wbc)
2554 struct extent_page_data epd = {
2557 .get_extent = get_extent,
2561 ret = extent_write_cache_pages(tree, mapping, wbc,
2562 __extent_writepage, &epd,
2565 submit_one_bio(WRITE, epd.bio, 0, 0);
2569 int extent_readpages(struct extent_io_tree *tree,
2570 struct address_space *mapping,
2571 struct list_head *pages, unsigned nr_pages,
2572 get_extent_t get_extent)
2574 struct bio *bio = NULL;
2576 struct pagevec pvec;
2577 unsigned long bio_flags = 0;
2579 pagevec_init(&pvec, 0);
2580 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2581 struct page *page = list_entry(pages->prev, struct page, lru);
2583 prefetchw(&page->flags);
2584 list_del(&page->lru);
2586 * what we want to do here is call add_to_page_cache_lru,
2587 * but that isn't exported, so we reproduce it here
2589 if (!add_to_page_cache(page, mapping,
2590 page->index, GFP_KERNEL)) {
2592 /* open coding of lru_cache_add, also not exported */
2593 page_cache_get(page);
2594 if (!pagevec_add(&pvec, page))
2595 __pagevec_lru_add_file(&pvec);
2596 __extent_read_full_page(tree, page, get_extent,
2597 &bio, 0, &bio_flags);
2599 page_cache_release(page);
2601 if (pagevec_count(&pvec))
2602 __pagevec_lru_add_file(&pvec);
2603 BUG_ON(!list_empty(pages));
2605 submit_one_bio(READ, bio, 0, bio_flags);
2610 * basic invalidatepage code, this waits on any locked or writeback
2611 * ranges corresponding to the page, and then deletes any extent state
2612 * records from the tree
2614 int extent_invalidatepage(struct extent_io_tree *tree,
2615 struct page *page, unsigned long offset)
2617 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2618 u64 end = start + PAGE_CACHE_SIZE - 1;
2619 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2621 start += (offset + blocksize - 1) & ~(blocksize - 1);
2625 lock_extent(tree, start, end, GFP_NOFS);
2626 wait_on_extent_writeback(tree, start, end);
2627 clear_extent_bit(tree, start, end,
2628 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2634 * simple commit_write call, set_range_dirty is used to mark both
2635 * the pages and the extent records as dirty
2637 int extent_commit_write(struct extent_io_tree *tree,
2638 struct inode *inode, struct page *page,
2639 unsigned from, unsigned to)
2641 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2643 set_page_extent_mapped(page);
2644 set_page_dirty(page);
2646 if (pos > inode->i_size) {
2647 i_size_write(inode, pos);
2648 mark_inode_dirty(inode);
2653 int extent_prepare_write(struct extent_io_tree *tree,
2654 struct inode *inode, struct page *page,
2655 unsigned from, unsigned to, get_extent_t *get_extent)
2657 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2658 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2660 u64 orig_block_start;
2663 struct extent_map *em;
2664 unsigned blocksize = 1 << inode->i_blkbits;
2665 size_t page_offset = 0;
2666 size_t block_off_start;
2667 size_t block_off_end;
2673 set_page_extent_mapped(page);
2675 block_start = (page_start + from) & ~((u64)blocksize - 1);
2676 block_end = (page_start + to - 1) | (blocksize - 1);
2677 orig_block_start = block_start;
2679 lock_extent(tree, page_start, page_end, GFP_NOFS);
2680 while (block_start <= block_end) {
2681 em = get_extent(inode, page, page_offset, block_start,
2682 block_end - block_start + 1, 1);
2683 if (IS_ERR(em) || !em)
2686 cur_end = min(block_end, extent_map_end(em) - 1);
2687 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2688 block_off_end = block_off_start + blocksize;
2689 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2691 if (!PageUptodate(page) && isnew &&
2692 (block_off_end > to || block_off_start < from)) {
2695 kaddr = kmap_atomic(page, KM_USER0);
2696 if (block_off_end > to)
2697 memset(kaddr + to, 0, block_off_end - to);
2698 if (block_off_start < from)
2699 memset(kaddr + block_off_start, 0,
2700 from - block_off_start);
2701 flush_dcache_page(page);
2702 kunmap_atomic(kaddr, KM_USER0);
2704 if ((em->block_start != EXTENT_MAP_HOLE &&
2705 em->block_start != EXTENT_MAP_INLINE) &&
2706 !isnew && !PageUptodate(page) &&
2707 (block_off_end > to || block_off_start < from) &&
2708 !test_range_bit(tree, block_start, cur_end,
2709 EXTENT_UPTODATE, 1)) {
2711 u64 extent_offset = block_start - em->start;
2713 sector = (em->block_start + extent_offset) >> 9;
2714 iosize = (cur_end - block_start + blocksize) &
2715 ~((u64)blocksize - 1);
2717 * we've already got the extent locked, but we
2718 * need to split the state such that our end_bio
2719 * handler can clear the lock.
2721 set_extent_bit(tree, block_start,
2722 block_start + iosize - 1,
2723 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2724 ret = submit_extent_page(READ, tree, page,
2725 sector, iosize, page_offset, em->bdev,
2727 end_bio_extent_preparewrite, 0,
2730 block_start = block_start + iosize;
2732 set_extent_uptodate(tree, block_start, cur_end,
2734 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2735 block_start = cur_end + 1;
2737 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2738 free_extent_map(em);
2741 wait_extent_bit(tree, orig_block_start,
2742 block_end, EXTENT_LOCKED);
2744 check_page_uptodate(tree, page);
2746 /* FIXME, zero out newly allocated blocks on error */
2751 * a helper for releasepage, this tests for areas of the page that
2752 * are locked or under IO and drops the related state bits if it is safe
2755 int try_release_extent_state(struct extent_map_tree *map,
2756 struct extent_io_tree *tree, struct page *page,
2759 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2760 u64 end = start + PAGE_CACHE_SIZE - 1;
2763 if (test_range_bit(tree, start, end,
2764 EXTENT_IOBITS | EXTENT_ORDERED, 0))
2767 if ((mask & GFP_NOFS) == GFP_NOFS)
2769 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2776 * a helper for releasepage. As long as there are no locked extents
2777 * in the range corresponding to the page, both state records and extent
2778 * map records are removed
2780 int try_release_extent_mapping(struct extent_map_tree *map,
2781 struct extent_io_tree *tree, struct page *page,
2784 struct extent_map *em;
2785 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2786 u64 end = start + PAGE_CACHE_SIZE - 1;
2788 if ((mask & __GFP_WAIT) &&
2789 page->mapping->host->i_size > 16 * 1024 * 1024) {
2791 while (start <= end) {
2792 len = end - start + 1;
2793 spin_lock(&map->lock);
2794 em = lookup_extent_mapping(map, start, len);
2795 if (!em || IS_ERR(em)) {
2796 spin_unlock(&map->lock);
2799 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2800 em->start != start) {
2801 spin_unlock(&map->lock);
2802 free_extent_map(em);
2805 if (!test_range_bit(tree, em->start,
2806 extent_map_end(em) - 1,
2807 EXTENT_LOCKED | EXTENT_WRITEBACK |
2810 remove_extent_mapping(map, em);
2811 /* once for the rb tree */
2812 free_extent_map(em);
2814 start = extent_map_end(em);
2815 spin_unlock(&map->lock);
2818 free_extent_map(em);
2821 return try_release_extent_state(map, tree, page, mask);
2824 sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2825 get_extent_t *get_extent)
2827 struct inode *inode = mapping->host;
2828 u64 start = iblock << inode->i_blkbits;
2829 sector_t sector = 0;
2830 size_t blksize = (1 << inode->i_blkbits);
2831 struct extent_map *em;
2833 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2835 em = get_extent(inode, NULL, 0, start, blksize, 0);
2836 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2838 if (!em || IS_ERR(em))
2841 if (em->block_start > EXTENT_MAP_LAST_BYTE)
2844 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2846 free_extent_map(em);
2850 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2851 __u64 start, __u64 len, get_extent_t *get_extent)
2855 u64 max = start + len;
2858 struct extent_map *em = NULL;
2860 u64 em_start = 0, em_len = 0;
2861 unsigned long emflags;
2867 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2869 em = get_extent(inode, NULL, 0, off, max - off, 0);
2877 off = em->start + em->len;
2881 em_start = em->start;
2887 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
2889 flags |= FIEMAP_EXTENT_LAST;
2890 } else if (em->block_start == EXTENT_MAP_HOLE) {
2891 flags |= FIEMAP_EXTENT_UNWRITTEN;
2892 } else if (em->block_start == EXTENT_MAP_INLINE) {
2893 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2894 FIEMAP_EXTENT_NOT_ALIGNED);
2895 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
2896 flags |= (FIEMAP_EXTENT_DELALLOC |
2897 FIEMAP_EXTENT_UNKNOWN);
2899 disko = em->block_start;
2901 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2902 flags |= FIEMAP_EXTENT_ENCODED;
2904 emflags = em->flags;
2905 free_extent_map(em);
2909 em = get_extent(inode, NULL, 0, off, max - off, 0);
2916 emflags = em->flags;
2918 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
2919 flags |= FIEMAP_EXTENT_LAST;
2923 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2929 free_extent_map(em);
2931 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2936 static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2940 struct address_space *mapping;
2943 return eb->first_page;
2944 i += eb->start >> PAGE_CACHE_SHIFT;
2945 mapping = eb->first_page->mapping;
2950 * extent_buffer_page is only called after pinning the page
2951 * by increasing the reference count. So we know the page must
2952 * be in the radix tree.
2955 p = radix_tree_lookup(&mapping->page_tree, i);
2961 static inline unsigned long num_extent_pages(u64 start, u64 len)
2963 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2964 (start >> PAGE_CACHE_SHIFT);
2967 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2972 struct extent_buffer *eb = NULL;
2974 unsigned long flags;
2977 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
2980 spin_lock_init(&eb->lock);
2981 init_waitqueue_head(&eb->lock_wq);
2984 spin_lock_irqsave(&leak_lock, flags);
2985 list_add(&eb->leak_list, &buffers);
2986 spin_unlock_irqrestore(&leak_lock, flags);
2988 atomic_set(&eb->refs, 1);
2993 static void __free_extent_buffer(struct extent_buffer *eb)
2996 unsigned long flags;
2997 spin_lock_irqsave(&leak_lock, flags);
2998 list_del(&eb->leak_list);
2999 spin_unlock_irqrestore(&leak_lock, flags);
3001 kmem_cache_free(extent_buffer_cache, eb);
3004 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3005 u64 start, unsigned long len,
3009 unsigned long num_pages = num_extent_pages(start, len);
3011 unsigned long index = start >> PAGE_CACHE_SHIFT;
3012 struct extent_buffer *eb;
3013 struct extent_buffer *exists = NULL;
3015 struct address_space *mapping = tree->mapping;
3018 spin_lock(&tree->buffer_lock);
3019 eb = buffer_search(tree, start);
3021 atomic_inc(&eb->refs);
3022 spin_unlock(&tree->buffer_lock);
3023 mark_page_accessed(eb->first_page);
3026 spin_unlock(&tree->buffer_lock);
3028 eb = __alloc_extent_buffer(tree, start, len, mask);
3033 eb->first_page = page0;
3036 page_cache_get(page0);
3037 mark_page_accessed(page0);
3038 set_page_extent_mapped(page0);
3039 set_page_extent_head(page0, len);
3040 uptodate = PageUptodate(page0);
3044 for (; i < num_pages; i++, index++) {
3045 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3050 set_page_extent_mapped(p);
3051 mark_page_accessed(p);
3054 set_page_extent_head(p, len);
3056 set_page_private(p, EXTENT_PAGE_PRIVATE);
3058 if (!PageUptodate(p))
3063 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3065 spin_lock(&tree->buffer_lock);
3066 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3068 /* add one reference for the caller */
3069 atomic_inc(&exists->refs);
3070 spin_unlock(&tree->buffer_lock);
3073 spin_unlock(&tree->buffer_lock);
3075 /* add one reference for the tree */
3076 atomic_inc(&eb->refs);
3080 if (!atomic_dec_and_test(&eb->refs))
3082 for (index = 1; index < i; index++)
3083 page_cache_release(extent_buffer_page(eb, index));
3084 page_cache_release(extent_buffer_page(eb, 0));
3085 __free_extent_buffer(eb);
3089 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3090 u64 start, unsigned long len,
3093 struct extent_buffer *eb;
3095 spin_lock(&tree->buffer_lock);
3096 eb = buffer_search(tree, start);
3098 atomic_inc(&eb->refs);
3099 spin_unlock(&tree->buffer_lock);
3102 mark_page_accessed(eb->first_page);
3107 void free_extent_buffer(struct extent_buffer *eb)
3112 if (!atomic_dec_and_test(&eb->refs))
3118 int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3119 struct extent_buffer *eb)
3122 unsigned long num_pages;
3125 num_pages = num_extent_pages(eb->start, eb->len);
3127 for (i = 0; i < num_pages; i++) {
3128 page = extent_buffer_page(eb, i);
3129 if (!PageDirty(page))
3134 set_page_extent_head(page, eb->len);
3136 set_page_private(page, EXTENT_PAGE_PRIVATE);
3138 clear_page_dirty_for_io(page);
3139 spin_lock_irq(&page->mapping->tree_lock);
3140 if (!PageDirty(page)) {
3141 radix_tree_tag_clear(&page->mapping->page_tree,
3143 PAGECACHE_TAG_DIRTY);
3145 spin_unlock_irq(&page->mapping->tree_lock);
3151 int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3152 struct extent_buffer *eb)
3154 return wait_on_extent_writeback(tree, eb->start,
3155 eb->start + eb->len - 1);
3158 int set_extent_buffer_dirty(struct extent_io_tree *tree,
3159 struct extent_buffer *eb)
3162 unsigned long num_pages;
3165 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3166 num_pages = num_extent_pages(eb->start, eb->len);
3167 for (i = 0; i < num_pages; i++)
3168 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
3172 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3173 struct extent_buffer *eb)
3177 unsigned long num_pages;
3179 num_pages = num_extent_pages(eb->start, eb->len);
3180 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3182 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3184 for (i = 0; i < num_pages; i++) {
3185 page = extent_buffer_page(eb, i);
3187 ClearPageUptodate(page);
3192 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3193 struct extent_buffer *eb)
3197 unsigned long num_pages;
3199 num_pages = num_extent_pages(eb->start, eb->len);
3201 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3203 for (i = 0; i < num_pages; i++) {
3204 page = extent_buffer_page(eb, i);
3205 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3206 ((i == num_pages - 1) &&
3207 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3208 check_page_uptodate(tree, page);
3211 SetPageUptodate(page);
3216 int extent_range_uptodate(struct extent_io_tree *tree,
3221 int pg_uptodate = 1;
3223 unsigned long index;
3225 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3228 while (start <= end) {
3229 index = start >> PAGE_CACHE_SHIFT;
3230 page = find_get_page(tree->mapping, index);
3231 uptodate = PageUptodate(page);
3232 page_cache_release(page);
3237 start += PAGE_CACHE_SIZE;
3242 int extent_buffer_uptodate(struct extent_io_tree *tree,
3243 struct extent_buffer *eb)
3246 unsigned long num_pages;
3249 int pg_uptodate = 1;
3251 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3254 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3255 EXTENT_UPTODATE, 1);
3259 num_pages = num_extent_pages(eb->start, eb->len);
3260 for (i = 0; i < num_pages; i++) {
3261 page = extent_buffer_page(eb, i);
3262 if (!PageUptodate(page)) {
3270 int read_extent_buffer_pages(struct extent_io_tree *tree,
3271 struct extent_buffer *eb,
3272 u64 start, int wait,
3273 get_extent_t *get_extent, int mirror_num)
3276 unsigned long start_i;
3280 int locked_pages = 0;
3281 int all_uptodate = 1;
3282 int inc_all_pages = 0;
3283 unsigned long num_pages;
3284 struct bio *bio = NULL;
3285 unsigned long bio_flags = 0;
3287 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3290 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3291 EXTENT_UPTODATE, 1)) {
3296 WARN_ON(start < eb->start);
3297 start_i = (start >> PAGE_CACHE_SHIFT) -
3298 (eb->start >> PAGE_CACHE_SHIFT);
3303 num_pages = num_extent_pages(eb->start, eb->len);
3304 for (i = start_i; i < num_pages; i++) {
3305 page = extent_buffer_page(eb, i);
3307 if (!trylock_page(page))
3313 if (!PageUptodate(page))
3318 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3322 for (i = start_i; i < num_pages; i++) {
3323 page = extent_buffer_page(eb, i);
3325 page_cache_get(page);
3326 if (!PageUptodate(page)) {
3329 ClearPageError(page);
3330 err = __extent_read_full_page(tree, page,
3332 mirror_num, &bio_flags);
3341 submit_one_bio(READ, bio, mirror_num, bio_flags);
3346 for (i = start_i; i < num_pages; i++) {
3347 page = extent_buffer_page(eb, i);
3348 wait_on_page_locked(page);
3349 if (!PageUptodate(page))
3354 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3359 while (locked_pages > 0) {
3360 page = extent_buffer_page(eb, i);
3368 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3369 unsigned long start,
3376 char *dst = (char *)dstv;
3377 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3378 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3380 WARN_ON(start > eb->len);
3381 WARN_ON(start + len > eb->start + eb->len);
3383 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3386 page = extent_buffer_page(eb, i);
3388 cur = min(len, (PAGE_CACHE_SIZE - offset));
3389 kaddr = kmap_atomic(page, KM_USER1);
3390 memcpy(dst, kaddr + offset, cur);
3391 kunmap_atomic(kaddr, KM_USER1);
3400 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3401 unsigned long min_len, char **token, char **map,
3402 unsigned long *map_start,
3403 unsigned long *map_len, int km)
3405 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3408 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3409 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3410 unsigned long end_i = (start_offset + start + min_len - 1) >>
3417 offset = start_offset;
3421 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3424 if (start + min_len > eb->len) {
3425 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3426 "wanted %lu %lu\n", (unsigned long long)eb->start,
3427 eb->len, start, min_len);
3431 p = extent_buffer_page(eb, i);
3432 kaddr = kmap_atomic(p, km);
3434 *map = kaddr + offset;
3435 *map_len = PAGE_CACHE_SIZE - offset;
3439 int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3440 unsigned long min_len,
3441 char **token, char **map,
3442 unsigned long *map_start,
3443 unsigned long *map_len, int km)
3447 if (eb->map_token) {
3448 unmap_extent_buffer(eb, eb->map_token, km);
3449 eb->map_token = NULL;
3452 err = map_private_extent_buffer(eb, start, min_len, token, map,
3453 map_start, map_len, km);
3455 eb->map_token = *token;
3457 eb->map_start = *map_start;
3458 eb->map_len = *map_len;
3463 void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3465 kunmap_atomic(token, km);
3468 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3469 unsigned long start,
3476 char *ptr = (char *)ptrv;
3477 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3478 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3481 WARN_ON(start > eb->len);
3482 WARN_ON(start + len > eb->start + eb->len);
3484 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3487 page = extent_buffer_page(eb, i);
3489 cur = min(len, (PAGE_CACHE_SIZE - offset));
3491 kaddr = kmap_atomic(page, KM_USER0);
3492 ret = memcmp(ptr, kaddr + offset, cur);
3493 kunmap_atomic(kaddr, KM_USER0);
3505 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3506 unsigned long start, unsigned long len)
3512 char *src = (char *)srcv;
3513 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3514 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3516 WARN_ON(start > eb->len);
3517 WARN_ON(start + len > eb->start + eb->len);
3519 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3522 page = extent_buffer_page(eb, i);
3523 WARN_ON(!PageUptodate(page));
3525 cur = min(len, PAGE_CACHE_SIZE - offset);
3526 kaddr = kmap_atomic(page, KM_USER1);
3527 memcpy(kaddr + offset, src, cur);
3528 kunmap_atomic(kaddr, KM_USER1);
3537 void memset_extent_buffer(struct extent_buffer *eb, char c,
3538 unsigned long start, unsigned long len)
3544 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3545 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3547 WARN_ON(start > eb->len);
3548 WARN_ON(start + len > eb->start + eb->len);
3550 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3553 page = extent_buffer_page(eb, i);
3554 WARN_ON(!PageUptodate(page));
3556 cur = min(len, PAGE_CACHE_SIZE - offset);
3557 kaddr = kmap_atomic(page, KM_USER0);
3558 memset(kaddr + offset, c, cur);
3559 kunmap_atomic(kaddr, KM_USER0);
3567 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3568 unsigned long dst_offset, unsigned long src_offset,
3571 u64 dst_len = dst->len;
3576 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3577 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3579 WARN_ON(src->len != dst_len);
3581 offset = (start_offset + dst_offset) &
3582 ((unsigned long)PAGE_CACHE_SIZE - 1);
3585 page = extent_buffer_page(dst, i);
3586 WARN_ON(!PageUptodate(page));
3588 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3590 kaddr = kmap_atomic(page, KM_USER0);
3591 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3592 kunmap_atomic(kaddr, KM_USER0);
3601 static void move_pages(struct page *dst_page, struct page *src_page,
3602 unsigned long dst_off, unsigned long src_off,
3605 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3606 if (dst_page == src_page) {
3607 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3609 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3610 char *p = dst_kaddr + dst_off + len;
3611 char *s = src_kaddr + src_off + len;
3616 kunmap_atomic(src_kaddr, KM_USER1);
3618 kunmap_atomic(dst_kaddr, KM_USER0);
3621 static void copy_pages(struct page *dst_page, struct page *src_page,
3622 unsigned long dst_off, unsigned long src_off,
3625 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3628 if (dst_page != src_page)
3629 src_kaddr = kmap_atomic(src_page, KM_USER1);
3631 src_kaddr = dst_kaddr;
3633 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3634 kunmap_atomic(dst_kaddr, KM_USER0);
3635 if (dst_page != src_page)
3636 kunmap_atomic(src_kaddr, KM_USER1);
3639 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3640 unsigned long src_offset, unsigned long len)
3643 size_t dst_off_in_page;
3644 size_t src_off_in_page;
3645 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3646 unsigned long dst_i;
3647 unsigned long src_i;
3649 if (src_offset + len > dst->len) {
3650 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3651 "len %lu dst len %lu\n", src_offset, len, dst->len);
3654 if (dst_offset + len > dst->len) {
3655 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3656 "len %lu dst len %lu\n", dst_offset, len, dst->len);
3661 dst_off_in_page = (start_offset + dst_offset) &
3662 ((unsigned long)PAGE_CACHE_SIZE - 1);
3663 src_off_in_page = (start_offset + src_offset) &
3664 ((unsigned long)PAGE_CACHE_SIZE - 1);
3666 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3667 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3669 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3671 cur = min_t(unsigned long, cur,
3672 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3674 copy_pages(extent_buffer_page(dst, dst_i),
3675 extent_buffer_page(dst, src_i),
3676 dst_off_in_page, src_off_in_page, cur);
3684 void memmove_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 unsigned long dst_end = dst_offset + len - 1;
3691 unsigned long src_end = src_offset + len - 1;
3692 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3693 unsigned long dst_i;
3694 unsigned long src_i;
3696 if (src_offset + len > dst->len) {
3697 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3698 "len %lu len %lu\n", src_offset, len, dst->len);
3701 if (dst_offset + len > dst->len) {
3702 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3703 "len %lu len %lu\n", dst_offset, len, dst->len);
3706 if (dst_offset < src_offset) {
3707 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3711 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3712 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3714 dst_off_in_page = (start_offset + dst_end) &
3715 ((unsigned long)PAGE_CACHE_SIZE - 1);
3716 src_off_in_page = (start_offset + src_end) &
3717 ((unsigned long)PAGE_CACHE_SIZE - 1);
3719 cur = min_t(unsigned long, len, src_off_in_page + 1);
3720 cur = min(cur, dst_off_in_page + 1);
3721 move_pages(extent_buffer_page(dst, dst_i),
3722 extent_buffer_page(dst, src_i),
3723 dst_off_in_page - cur + 1,
3724 src_off_in_page - cur + 1, cur);
3732 int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3734 u64 start = page_offset(page);
3735 struct extent_buffer *eb;
3738 unsigned long num_pages;
3740 spin_lock(&tree->buffer_lock);
3741 eb = buffer_search(tree, start);
3745 if (atomic_read(&eb->refs) > 1) {
3749 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3753 /* at this point we can safely release the extent buffer */
3754 num_pages = num_extent_pages(eb->start, eb->len);
3755 for (i = 0; i < num_pages; i++)
3756 page_cache_release(extent_buffer_page(eb, i));
3757 rb_erase(&eb->rb_node, &tree->buffer);
3758 __free_extent_buffer(eb);
3760 spin_unlock(&tree->buffer_lock);