1 #include <linux/bitops.h>
2 #include <linux/slab.h>
6 #include <linux/pagemap.h>
7 #include <linux/page-flags.h>
8 #include <linux/module.h>
9 #include <linux/spinlock.h>
10 #include <linux/blkdev.h>
11 #include <linux/swap.h>
12 #include <linux/writeback.h>
13 #include <linux/pagevec.h>
14 #include "extent_io.h"
15 #include "extent_map.h"
18 #include "btrfs_inode.h"
20 static struct kmem_cache *extent_state_cache;
21 static struct kmem_cache *extent_buffer_cache;
23 static LIST_HEAD(buffers);
24 static LIST_HEAD(states);
28 static DEFINE_SPINLOCK(leak_lock);
31 #define BUFFER_LRU_MAX 64
36 struct rb_node rb_node;
39 struct extent_page_data {
41 struct extent_io_tree *tree;
42 get_extent_t *get_extent;
44 /* tells writepage not to lock the state bits for this range
45 * it still does the unlocking
47 unsigned int extent_locked:1;
49 /* tells the submit_bio code to use a WRITE_SYNC */
50 unsigned int sync_io:1;
53 int __init extent_io_init(void)
55 extent_state_cache = kmem_cache_create("extent_state",
56 sizeof(struct extent_state), 0,
57 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
58 if (!extent_state_cache)
61 extent_buffer_cache = kmem_cache_create("extent_buffers",
62 sizeof(struct extent_buffer), 0,
63 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
64 if (!extent_buffer_cache)
65 goto free_state_cache;
69 kmem_cache_destroy(extent_state_cache);
73 void extent_io_exit(void)
75 struct extent_state *state;
76 struct extent_buffer *eb;
78 while (!list_empty(&states)) {
79 state = list_entry(states.next, struct extent_state, leak_list);
80 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
81 "state %lu in tree %p refs %d\n",
82 (unsigned long long)state->start,
83 (unsigned long long)state->end,
84 state->state, state->tree, atomic_read(&state->refs));
85 list_del(&state->leak_list);
86 kmem_cache_free(extent_state_cache, state);
90 while (!list_empty(&buffers)) {
91 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
92 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
93 "refs %d\n", (unsigned long long)eb->start,
94 eb->len, atomic_read(&eb->refs));
95 list_del(&eb->leak_list);
96 kmem_cache_free(extent_buffer_cache, eb);
98 if (extent_state_cache)
99 kmem_cache_destroy(extent_state_cache);
100 if (extent_buffer_cache)
101 kmem_cache_destroy(extent_buffer_cache);
104 void extent_io_tree_init(struct extent_io_tree *tree,
105 struct address_space *mapping, gfp_t mask)
107 tree->state.rb_node = NULL;
108 tree->buffer.rb_node = NULL;
110 tree->dirty_bytes = 0;
111 spin_lock_init(&tree->lock);
112 spin_lock_init(&tree->buffer_lock);
113 tree->mapping = mapping;
116 static struct extent_state *alloc_extent_state(gfp_t mask)
118 struct extent_state *state;
123 state = kmem_cache_alloc(extent_state_cache, mask);
130 spin_lock_irqsave(&leak_lock, flags);
131 list_add(&state->leak_list, &states);
132 spin_unlock_irqrestore(&leak_lock, flags);
134 atomic_set(&state->refs, 1);
135 init_waitqueue_head(&state->wq);
139 static void free_extent_state(struct extent_state *state)
143 if (atomic_dec_and_test(&state->refs)) {
147 WARN_ON(state->tree);
149 spin_lock_irqsave(&leak_lock, flags);
150 list_del(&state->leak_list);
151 spin_unlock_irqrestore(&leak_lock, flags);
153 kmem_cache_free(extent_state_cache, state);
157 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
158 struct rb_node *node)
160 struct rb_node **p = &root->rb_node;
161 struct rb_node *parent = NULL;
162 struct tree_entry *entry;
166 entry = rb_entry(parent, struct tree_entry, rb_node);
168 if (offset < entry->start)
170 else if (offset > entry->end)
176 entry = rb_entry(node, struct tree_entry, rb_node);
177 rb_link_node(node, parent, p);
178 rb_insert_color(node, root);
182 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
183 struct rb_node **prev_ret,
184 struct rb_node **next_ret)
186 struct rb_root *root = &tree->state;
187 struct rb_node *n = root->rb_node;
188 struct rb_node *prev = NULL;
189 struct rb_node *orig_prev = NULL;
190 struct tree_entry *entry;
191 struct tree_entry *prev_entry = NULL;
194 entry = rb_entry(n, struct tree_entry, rb_node);
198 if (offset < entry->start)
200 else if (offset > entry->end)
208 while (prev && offset > prev_entry->end) {
209 prev = rb_next(prev);
210 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
217 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
218 while (prev && offset < prev_entry->start) {
219 prev = rb_prev(prev);
220 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
227 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
230 struct rb_node *prev = NULL;
233 ret = __etree_search(tree, offset, &prev, NULL);
239 static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
240 u64 offset, struct rb_node *node)
242 struct rb_root *root = &tree->buffer;
243 struct rb_node **p = &root->rb_node;
244 struct rb_node *parent = NULL;
245 struct extent_buffer *eb;
249 eb = rb_entry(parent, struct extent_buffer, rb_node);
251 if (offset < eb->start)
253 else if (offset > eb->start)
259 rb_link_node(node, parent, p);
260 rb_insert_color(node, root);
264 static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
267 struct rb_root *root = &tree->buffer;
268 struct rb_node *n = root->rb_node;
269 struct extent_buffer *eb;
272 eb = rb_entry(n, struct extent_buffer, rb_node);
273 if (offset < eb->start)
275 else if (offset > eb->start)
284 * utility function to look for merge candidates inside a given range.
285 * Any extents with matching state are merged together into a single
286 * extent in the tree. Extents with EXTENT_IO in their state field
287 * are not merged because the end_io handlers need to be able to do
288 * operations on them without sleeping (or doing allocations/splits).
290 * This should be called with the tree lock held.
292 static int merge_state(struct extent_io_tree *tree,
293 struct extent_state *state)
295 struct extent_state *other;
296 struct rb_node *other_node;
298 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
301 other_node = rb_prev(&state->rb_node);
303 other = rb_entry(other_node, struct extent_state, rb_node);
304 if (other->end == state->start - 1 &&
305 other->state == state->state) {
306 state->start = other->start;
308 rb_erase(&other->rb_node, &tree->state);
309 free_extent_state(other);
312 other_node = rb_next(&state->rb_node);
314 other = rb_entry(other_node, struct extent_state, rb_node);
315 if (other->start == state->end + 1 &&
316 other->state == state->state) {
317 other->start = state->start;
319 rb_erase(&state->rb_node, &tree->state);
320 free_extent_state(state);
326 static void set_state_cb(struct extent_io_tree *tree,
327 struct extent_state *state,
330 if (tree->ops && tree->ops->set_bit_hook) {
331 tree->ops->set_bit_hook(tree->mapping->host, state->start,
332 state->end, state->state, bits);
336 static void clear_state_cb(struct extent_io_tree *tree,
337 struct extent_state *state,
340 if (tree->ops && tree->ops->clear_bit_hook) {
341 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
342 state->end, state->state, bits);
347 * insert an extent_state struct into the tree. 'bits' are set on the
348 * struct before it is inserted.
350 * This may return -EEXIST if the extent is already there, in which case the
351 * state struct is freed.
353 * The tree lock is not taken internally. This is a utility function and
354 * probably isn't what you want to call (see set/clear_extent_bit).
356 static int insert_state(struct extent_io_tree *tree,
357 struct extent_state *state, u64 start, u64 end,
360 struct rb_node *node;
363 printk(KERN_ERR "btrfs end < start %llu %llu\n",
364 (unsigned long long)end,
365 (unsigned long long)start);
368 if (bits & EXTENT_DIRTY)
369 tree->dirty_bytes += end - start + 1;
370 set_state_cb(tree, state, bits);
371 state->state |= bits;
372 state->start = start;
374 node = tree_insert(&tree->state, end, &state->rb_node);
376 struct extent_state *found;
377 found = rb_entry(node, struct extent_state, rb_node);
378 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
379 "%llu %llu\n", (unsigned long long)found->start,
380 (unsigned long long)found->end,
381 (unsigned long long)start, (unsigned long long)end);
382 free_extent_state(state);
386 merge_state(tree, state);
391 * split a given extent state struct in two, inserting the preallocated
392 * struct 'prealloc' as the newly created second half. 'split' indicates an
393 * offset inside 'orig' where it should be split.
396 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
397 * are two extent state structs in the tree:
398 * prealloc: [orig->start, split - 1]
399 * orig: [ split, orig->end ]
401 * The tree locks are not taken by this function. They need to be held
404 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
405 struct extent_state *prealloc, u64 split)
407 struct rb_node *node;
408 prealloc->start = orig->start;
409 prealloc->end = split - 1;
410 prealloc->state = orig->state;
413 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
415 free_extent_state(prealloc);
418 prealloc->tree = tree;
423 * utility function to clear some bits in an extent state struct.
424 * it will optionally wake up any one waiting on this state (wake == 1), or
425 * forcibly remove the state from the tree (delete == 1).
427 * If no bits are set on the state struct after clearing things, the
428 * struct is freed and removed from the tree
430 static int clear_state_bit(struct extent_io_tree *tree,
431 struct extent_state *state, int bits, int wake,
434 int ret = state->state & bits;
436 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
437 u64 range = state->end - state->start + 1;
438 WARN_ON(range > tree->dirty_bytes);
439 tree->dirty_bytes -= range;
441 clear_state_cb(tree, state, bits);
442 state->state &= ~bits;
445 if (delete || state->state == 0) {
447 clear_state_cb(tree, state, state->state);
448 rb_erase(&state->rb_node, &tree->state);
450 free_extent_state(state);
455 merge_state(tree, state);
461 * clear some bits on a range in the tree. This may require splitting
462 * or inserting elements in the tree, so the gfp mask is used to
463 * indicate which allocations or sleeping are allowed.
465 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
466 * the given range from the tree regardless of state (ie for truncate).
468 * the range [start, end] is inclusive.
470 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
471 * bits were already set, or zero if none of the bits were already set.
473 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
474 int bits, int wake, int delete, gfp_t mask)
476 struct extent_state *state;
477 struct extent_state *prealloc = NULL;
478 struct rb_node *node;
484 if (!prealloc && (mask & __GFP_WAIT)) {
485 prealloc = alloc_extent_state(mask);
490 spin_lock(&tree->lock);
492 * this search will find the extents that end after
495 node = tree_search(tree, start);
498 state = rb_entry(node, struct extent_state, rb_node);
499 if (state->start > end)
501 WARN_ON(state->end < start);
502 last_end = state->end;
505 * | ---- desired range ---- |
507 * | ------------- state -------------- |
509 * We need to split the extent we found, and may flip
510 * bits on second half.
512 * If the extent we found extends past our range, we
513 * just split and search again. It'll get split again
514 * the next time though.
516 * If the extent we found is inside our range, we clear
517 * the desired bit on it.
520 if (state->start < start) {
522 prealloc = alloc_extent_state(GFP_ATOMIC);
523 err = split_state(tree, state, prealloc, start);
524 BUG_ON(err == -EEXIST);
528 if (state->end <= end) {
529 set |= clear_state_bit(tree, state, bits,
531 if (last_end == (u64)-1)
533 start = last_end + 1;
535 start = state->start;
540 * | ---- desired range ---- |
542 * We need to split the extent, and clear the bit
545 if (state->start <= end && state->end > end) {
547 prealloc = alloc_extent_state(GFP_ATOMIC);
548 err = split_state(tree, state, prealloc, end + 1);
549 BUG_ON(err == -EEXIST);
553 set |= clear_state_bit(tree, prealloc, bits,
559 set |= clear_state_bit(tree, state, bits, wake, delete);
560 if (last_end == (u64)-1)
562 start = last_end + 1;
566 spin_unlock(&tree->lock);
568 free_extent_state(prealloc);
575 spin_unlock(&tree->lock);
576 if (mask & __GFP_WAIT)
581 static int wait_on_state(struct extent_io_tree *tree,
582 struct extent_state *state)
583 __releases(tree->lock)
584 __acquires(tree->lock)
587 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
588 spin_unlock(&tree->lock);
590 spin_lock(&tree->lock);
591 finish_wait(&state->wq, &wait);
596 * waits for one or more bits to clear on a range in the state tree.
597 * The range [start, end] is inclusive.
598 * The tree lock is taken by this function
600 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
602 struct extent_state *state;
603 struct rb_node *node;
605 spin_lock(&tree->lock);
609 * this search will find all the extents that end after
612 node = tree_search(tree, start);
616 state = rb_entry(node, struct extent_state, rb_node);
618 if (state->start > end)
621 if (state->state & bits) {
622 start = state->start;
623 atomic_inc(&state->refs);
624 wait_on_state(tree, state);
625 free_extent_state(state);
628 start = state->end + 1;
633 if (need_resched()) {
634 spin_unlock(&tree->lock);
636 spin_lock(&tree->lock);
640 spin_unlock(&tree->lock);
644 static void set_state_bits(struct extent_io_tree *tree,
645 struct extent_state *state,
648 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
649 u64 range = state->end - state->start + 1;
650 tree->dirty_bytes += range;
652 set_state_cb(tree, state, bits);
653 state->state |= bits;
657 * set some bits on a range in the tree. This may require allocations
658 * or sleeping, so the gfp mask is used to indicate what is allowed.
660 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
661 * range already has the desired bits set. The start of the existing
662 * range is returned in failed_start in this case.
664 * [start, end] is inclusive
665 * This takes the tree lock.
667 static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
668 int bits, int exclusive, u64 *failed_start,
671 struct extent_state *state;
672 struct extent_state *prealloc = NULL;
673 struct rb_node *node;
679 if (!prealloc && (mask & __GFP_WAIT)) {
680 prealloc = alloc_extent_state(mask);
685 spin_lock(&tree->lock);
687 * this search will find all the extents that end after
690 node = tree_search(tree, start);
692 err = insert_state(tree, prealloc, start, end, bits);
694 BUG_ON(err == -EEXIST);
697 state = rb_entry(node, struct extent_state, rb_node);
699 last_start = state->start;
700 last_end = state->end;
703 * | ---- desired range ---- |
706 * Just lock what we found and keep going
708 if (state->start == start && state->end <= end) {
709 struct rb_node *next_node;
710 set = state->state & bits;
711 if (set && exclusive) {
712 *failed_start = state->start;
716 set_state_bits(tree, state, bits);
717 merge_state(tree, state);
718 if (last_end == (u64)-1)
721 start = last_end + 1;
722 if (start < end && prealloc && !need_resched()) {
723 next_node = rb_next(node);
725 state = rb_entry(next_node, struct extent_state,
727 if (state->start == start)
735 * | ---- desired range ---- |
738 * | ------------- state -------------- |
740 * We need to split the extent we found, and may flip bits on
743 * If the extent we found extends past our
744 * range, we just split and search again. It'll get split
745 * again the next time though.
747 * If the extent we found is inside our range, we set the
750 if (state->start < start) {
751 set = state->state & bits;
752 if (exclusive && set) {
753 *failed_start = start;
757 err = split_state(tree, state, prealloc, start);
758 BUG_ON(err == -EEXIST);
762 if (state->end <= end) {
763 set_state_bits(tree, state, bits);
764 merge_state(tree, state);
765 if (last_end == (u64)-1)
767 start = last_end + 1;
769 start = state->start;
774 * | ---- desired range ---- |
775 * | state | or | state |
777 * There's a hole, we need to insert something in it and
778 * ignore the extent we found.
780 if (state->start > start) {
782 if (end < last_start)
785 this_end = last_start - 1;
786 err = insert_state(tree, prealloc, start, this_end,
789 BUG_ON(err == -EEXIST);
792 start = this_end + 1;
796 * | ---- desired range ---- |
798 * We need to split the extent, and set the bit
801 if (state->start <= end && state->end > end) {
802 set = state->state & bits;
803 if (exclusive && set) {
804 *failed_start = start;
808 err = split_state(tree, state, prealloc, end + 1);
809 BUG_ON(err == -EEXIST);
811 set_state_bits(tree, prealloc, bits);
812 merge_state(tree, prealloc);
820 spin_unlock(&tree->lock);
822 free_extent_state(prealloc);
829 spin_unlock(&tree->lock);
830 if (mask & __GFP_WAIT)
835 /* wrappers around set/clear extent bit */
836 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
839 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
843 int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
846 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask);
849 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
850 int bits, gfp_t mask)
852 return set_extent_bit(tree, start, end, bits, 0, NULL,
856 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
857 int bits, gfp_t mask)
859 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
862 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
865 return set_extent_bit(tree, start, end,
866 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
870 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
873 return clear_extent_bit(tree, start, end,
874 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
877 int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
880 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask);
883 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
886 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
890 static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
893 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
896 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
899 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
903 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
906 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
909 static int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
912 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
916 static int clear_extent_writeback(struct extent_io_tree *tree, u64 start,
919 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
922 int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
924 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
928 * either insert or lock state struct between start and end use mask to tell
929 * us if waiting is desired.
931 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
936 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
937 &failed_start, mask);
938 if (err == -EEXIST && (mask & __GFP_WAIT)) {
939 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
940 start = failed_start;
944 WARN_ON(start > end);
949 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
955 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
956 &failed_start, mask);
957 if (err == -EEXIST) {
958 if (failed_start > start)
959 clear_extent_bit(tree, start, failed_start - 1,
960 EXTENT_LOCKED, 1, 0, mask);
966 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
969 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
973 * helper function to set pages and extents in the tree dirty
975 int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
977 unsigned long index = start >> PAGE_CACHE_SHIFT;
978 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
981 while (index <= end_index) {
982 page = find_get_page(tree->mapping, index);
984 __set_page_dirty_nobuffers(page);
985 page_cache_release(page);
988 set_extent_dirty(tree, start, end, GFP_NOFS);
993 * helper function to set both pages and extents in the tree writeback
995 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
997 unsigned long index = start >> PAGE_CACHE_SHIFT;
998 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1001 while (index <= end_index) {
1002 page = find_get_page(tree->mapping, index);
1004 set_page_writeback(page);
1005 page_cache_release(page);
1008 set_extent_writeback(tree, start, end, GFP_NOFS);
1013 * find the first offset in the io tree with 'bits' set. zero is
1014 * returned if we find something, and *start_ret and *end_ret are
1015 * set to reflect the state struct that was found.
1017 * If nothing was found, 1 is returned, < 0 on error
1019 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1020 u64 *start_ret, u64 *end_ret, int bits)
1022 struct rb_node *node;
1023 struct extent_state *state;
1026 spin_lock(&tree->lock);
1028 * this search will find all the extents that end after
1031 node = tree_search(tree, start);
1036 state = rb_entry(node, struct extent_state, rb_node);
1037 if (state->end >= start && (state->state & bits)) {
1038 *start_ret = state->start;
1039 *end_ret = state->end;
1043 node = rb_next(node);
1048 spin_unlock(&tree->lock);
1052 /* find the first state struct with 'bits' set after 'start', and
1053 * return it. tree->lock must be held. NULL will returned if
1054 * nothing was found after 'start'
1056 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1057 u64 start, int bits)
1059 struct rb_node *node;
1060 struct extent_state *state;
1063 * this search will find all the extents that end after
1066 node = tree_search(tree, start);
1071 state = rb_entry(node, struct extent_state, rb_node);
1072 if (state->end >= start && (state->state & bits))
1075 node = rb_next(node);
1084 * find a contiguous range of bytes in the file marked as delalloc, not
1085 * more than 'max_bytes'. start and end are used to return the range,
1087 * 1 is returned if we find something, 0 if nothing was in the tree
1089 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1090 u64 *start, u64 *end, u64 max_bytes)
1092 struct rb_node *node;
1093 struct extent_state *state;
1094 u64 cur_start = *start;
1096 u64 total_bytes = 0;
1098 spin_lock(&tree->lock);
1101 * this search will find all the extents that end after
1104 node = tree_search(tree, cur_start);
1112 state = rb_entry(node, struct extent_state, rb_node);
1113 if (found && (state->start != cur_start ||
1114 (state->state & EXTENT_BOUNDARY))) {
1117 if (!(state->state & EXTENT_DELALLOC)) {
1123 *start = state->start;
1126 cur_start = state->end + 1;
1127 node = rb_next(node);
1130 total_bytes += state->end - state->start + 1;
1131 if (total_bytes >= max_bytes)
1135 spin_unlock(&tree->lock);
1139 static noinline int __unlock_for_delalloc(struct inode *inode,
1140 struct page *locked_page,
1144 struct page *pages[16];
1145 unsigned long index = start >> PAGE_CACHE_SHIFT;
1146 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1147 unsigned long nr_pages = end_index - index + 1;
1150 if (index == locked_page->index && end_index == index)
1153 while (nr_pages > 0) {
1154 ret = find_get_pages_contig(inode->i_mapping, index,
1155 min_t(unsigned long, nr_pages,
1156 ARRAY_SIZE(pages)), pages);
1157 for (i = 0; i < ret; i++) {
1158 if (pages[i] != locked_page)
1159 unlock_page(pages[i]);
1160 page_cache_release(pages[i]);
1169 static noinline int lock_delalloc_pages(struct inode *inode,
1170 struct page *locked_page,
1174 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1175 unsigned long start_index = index;
1176 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1177 unsigned long pages_locked = 0;
1178 struct page *pages[16];
1179 unsigned long nrpages;
1183 /* the caller is responsible for locking the start index */
1184 if (index == locked_page->index && index == end_index)
1187 /* skip the page at the start index */
1188 nrpages = end_index - index + 1;
1189 while (nrpages > 0) {
1190 ret = find_get_pages_contig(inode->i_mapping, index,
1191 min_t(unsigned long,
1192 nrpages, ARRAY_SIZE(pages)), pages);
1197 /* now we have an array of pages, lock them all */
1198 for (i = 0; i < ret; i++) {
1200 * the caller is taking responsibility for
1203 if (pages[i] != locked_page) {
1204 lock_page(pages[i]);
1205 if (!PageDirty(pages[i]) ||
1206 pages[i]->mapping != inode->i_mapping) {
1208 unlock_page(pages[i]);
1209 page_cache_release(pages[i]);
1213 page_cache_release(pages[i]);
1222 if (ret && pages_locked) {
1223 __unlock_for_delalloc(inode, locked_page,
1225 ((u64)(start_index + pages_locked - 1)) <<
1232 * find a contiguous range of bytes in the file marked as delalloc, not
1233 * more than 'max_bytes'. start and end are used to return the range,
1235 * 1 is returned if we find something, 0 if nothing was in the tree
1237 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1238 struct extent_io_tree *tree,
1239 struct page *locked_page,
1240 u64 *start, u64 *end,
1250 /* step one, find a bunch of delalloc bytes starting at start */
1251 delalloc_start = *start;
1253 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1255 if (!found || delalloc_end <= *start) {
1256 *start = delalloc_start;
1257 *end = delalloc_end;
1262 * start comes from the offset of locked_page. We have to lock
1263 * pages in order, so we can't process delalloc bytes before
1266 if (delalloc_start < *start)
1267 delalloc_start = *start;
1270 * make sure to limit the number of pages we try to lock down
1273 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1274 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1276 /* step two, lock all the pages after the page that has start */
1277 ret = lock_delalloc_pages(inode, locked_page,
1278 delalloc_start, delalloc_end);
1279 if (ret == -EAGAIN) {
1280 /* some of the pages are gone, lets avoid looping by
1281 * shortening the size of the delalloc range we're searching
1284 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1285 max_bytes = PAGE_CACHE_SIZE - offset;
1295 /* step three, lock the state bits for the whole range */
1296 lock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1298 /* then test to make sure it is all still delalloc */
1299 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1300 EXTENT_DELALLOC, 1);
1302 unlock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1303 __unlock_for_delalloc(inode, locked_page,
1304 delalloc_start, delalloc_end);
1308 *start = delalloc_start;
1309 *end = delalloc_end;
1314 int extent_clear_unlock_delalloc(struct inode *inode,
1315 struct extent_io_tree *tree,
1316 u64 start, u64 end, struct page *locked_page,
1319 int clear_delalloc, int clear_dirty,
1324 struct page *pages[16];
1325 unsigned long index = start >> PAGE_CACHE_SHIFT;
1326 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1327 unsigned long nr_pages = end_index - index + 1;
1332 clear_bits |= EXTENT_LOCKED;
1334 clear_bits |= EXTENT_DIRTY;
1337 clear_bits |= EXTENT_DELALLOC;
1339 clear_extent_bit(tree, start, end, clear_bits, 1, 0, GFP_NOFS);
1340 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback))
1343 while (nr_pages > 0) {
1344 ret = find_get_pages_contig(inode->i_mapping, index,
1345 min_t(unsigned long,
1346 nr_pages, ARRAY_SIZE(pages)), pages);
1347 for (i = 0; i < ret; i++) {
1348 if (pages[i] == locked_page) {
1349 page_cache_release(pages[i]);
1353 clear_page_dirty_for_io(pages[i]);
1355 set_page_writeback(pages[i]);
1357 end_page_writeback(pages[i]);
1359 unlock_page(pages[i]);
1360 page_cache_release(pages[i]);
1370 * count the number of bytes in the tree that have a given bit(s)
1371 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1372 * cached. The total number found is returned.
1374 u64 count_range_bits(struct extent_io_tree *tree,
1375 u64 *start, u64 search_end, u64 max_bytes,
1378 struct rb_node *node;
1379 struct extent_state *state;
1380 u64 cur_start = *start;
1381 u64 total_bytes = 0;
1384 if (search_end <= cur_start) {
1389 spin_lock(&tree->lock);
1390 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1391 total_bytes = tree->dirty_bytes;
1395 * this search will find all the extents that end after
1398 node = tree_search(tree, cur_start);
1403 state = rb_entry(node, struct extent_state, rb_node);
1404 if (state->start > search_end)
1406 if (state->end >= cur_start && (state->state & bits)) {
1407 total_bytes += min(search_end, state->end) + 1 -
1408 max(cur_start, state->start);
1409 if (total_bytes >= max_bytes)
1412 *start = state->start;
1416 node = rb_next(node);
1421 spin_unlock(&tree->lock);
1426 * set the private field for a given byte offset in the tree. If there isn't
1427 * an extent_state there already, this does nothing.
1429 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1431 struct rb_node *node;
1432 struct extent_state *state;
1435 spin_lock(&tree->lock);
1437 * this search will find all the extents that end after
1440 node = tree_search(tree, start);
1445 state = rb_entry(node, struct extent_state, rb_node);
1446 if (state->start != start) {
1450 state->private = private;
1452 spin_unlock(&tree->lock);
1456 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1458 struct rb_node *node;
1459 struct extent_state *state;
1462 spin_lock(&tree->lock);
1464 * this search will find all the extents that end after
1467 node = tree_search(tree, start);
1472 state = rb_entry(node, struct extent_state, rb_node);
1473 if (state->start != start) {
1477 *private = state->private;
1479 spin_unlock(&tree->lock);
1484 * searches a range in the state tree for a given mask.
1485 * If 'filled' == 1, this returns 1 only if every extent in the tree
1486 * has the bits set. Otherwise, 1 is returned if any bit in the
1487 * range is found set.
1489 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1490 int bits, int filled)
1492 struct extent_state *state = NULL;
1493 struct rb_node *node;
1496 spin_lock(&tree->lock);
1497 node = tree_search(tree, start);
1498 while (node && start <= end) {
1499 state = rb_entry(node, struct extent_state, rb_node);
1501 if (filled && state->start > start) {
1506 if (state->start > end)
1509 if (state->state & bits) {
1513 } else if (filled) {
1517 start = state->end + 1;
1520 node = rb_next(node);
1527 spin_unlock(&tree->lock);
1532 * helper function to set a given page up to date if all the
1533 * extents in the tree for that page are up to date
1535 static int check_page_uptodate(struct extent_io_tree *tree,
1538 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1539 u64 end = start + PAGE_CACHE_SIZE - 1;
1540 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1541 SetPageUptodate(page);
1546 * helper function to unlock a page if all the extents in the tree
1547 * for that page are unlocked
1549 static int check_page_locked(struct extent_io_tree *tree,
1552 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1553 u64 end = start + PAGE_CACHE_SIZE - 1;
1554 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1560 * helper function to end page writeback if all the extents
1561 * in the tree for that page are done with writeback
1563 static int check_page_writeback(struct extent_io_tree *tree,
1566 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1567 u64 end = start + PAGE_CACHE_SIZE - 1;
1568 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1569 end_page_writeback(page);
1573 /* lots and lots of room for performance fixes in the end_bio funcs */
1576 * after a writepage IO is done, we need to:
1577 * clear the uptodate bits on error
1578 * clear the writeback bits in the extent tree for this IO
1579 * end_page_writeback if the page has no more pending IO
1581 * Scheduling is not allowed, so the extent state tree is expected
1582 * to have one and only one object corresponding to this IO.
1584 static void end_bio_extent_writepage(struct bio *bio, int err)
1586 int uptodate = err == 0;
1587 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1588 struct extent_io_tree *tree;
1595 struct page *page = bvec->bv_page;
1596 tree = &BTRFS_I(page->mapping->host)->io_tree;
1598 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1600 end = start + bvec->bv_len - 1;
1602 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1607 if (--bvec >= bio->bi_io_vec)
1608 prefetchw(&bvec->bv_page->flags);
1609 if (tree->ops && tree->ops->writepage_end_io_hook) {
1610 ret = tree->ops->writepage_end_io_hook(page, start,
1611 end, NULL, uptodate);
1616 if (!uptodate && tree->ops &&
1617 tree->ops->writepage_io_failed_hook) {
1618 ret = tree->ops->writepage_io_failed_hook(bio, page,
1621 uptodate = (err == 0);
1627 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1628 ClearPageUptodate(page);
1632 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
1635 end_page_writeback(page);
1637 check_page_writeback(tree, page);
1638 } while (bvec >= bio->bi_io_vec);
1644 * after a readpage IO is done, we need to:
1645 * clear the uptodate bits on error
1646 * set the uptodate bits if things worked
1647 * set the page up to date if all extents in the tree are uptodate
1648 * clear the lock bit in the extent tree
1649 * unlock the page if there are no other extents locked for it
1651 * Scheduling is not allowed, so the extent state tree is expected
1652 * to have one and only one object corresponding to this IO.
1654 static void end_bio_extent_readpage(struct bio *bio, int err)
1656 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1657 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1658 struct extent_io_tree *tree;
1668 struct page *page = bvec->bv_page;
1669 tree = &BTRFS_I(page->mapping->host)->io_tree;
1671 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1673 end = start + bvec->bv_len - 1;
1675 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1680 if (--bvec >= bio->bi_io_vec)
1681 prefetchw(&bvec->bv_page->flags);
1683 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1684 ret = tree->ops->readpage_end_io_hook(page, start, end,
1689 if (!uptodate && tree->ops &&
1690 tree->ops->readpage_io_failed_hook) {
1691 ret = tree->ops->readpage_io_failed_hook(bio, page,
1695 test_bit(BIO_UPTODATE, &bio->bi_flags);
1703 set_extent_uptodate(tree, start, end,
1706 unlock_extent(tree, start, end, GFP_ATOMIC);
1710 SetPageUptodate(page);
1712 ClearPageUptodate(page);
1718 check_page_uptodate(tree, page);
1720 ClearPageUptodate(page);
1723 check_page_locked(tree, page);
1725 } while (bvec >= bio->bi_io_vec);
1731 * IO done from prepare_write is pretty simple, we just unlock
1732 * the structs in the extent tree when done, and set the uptodate bits
1735 static void end_bio_extent_preparewrite(struct bio *bio, int err)
1737 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1738 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1739 struct extent_io_tree *tree;
1744 struct page *page = bvec->bv_page;
1745 tree = &BTRFS_I(page->mapping->host)->io_tree;
1747 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1749 end = start + bvec->bv_len - 1;
1751 if (--bvec >= bio->bi_io_vec)
1752 prefetchw(&bvec->bv_page->flags);
1755 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1757 ClearPageUptodate(page);
1761 unlock_extent(tree, start, end, GFP_ATOMIC);
1763 } while (bvec >= bio->bi_io_vec);
1769 extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1774 bio = bio_alloc(gfp_flags, nr_vecs);
1776 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1777 while (!bio && (nr_vecs /= 2))
1778 bio = bio_alloc(gfp_flags, nr_vecs);
1783 bio->bi_bdev = bdev;
1784 bio->bi_sector = first_sector;
1789 static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1790 unsigned long bio_flags)
1793 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1794 struct page *page = bvec->bv_page;
1795 struct extent_io_tree *tree = bio->bi_private;
1799 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1800 end = start + bvec->bv_len - 1;
1802 bio->bi_private = NULL;
1806 if (tree->ops && tree->ops->submit_bio_hook)
1807 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1808 mirror_num, bio_flags);
1810 submit_bio(rw, bio);
1811 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1817 static int submit_extent_page(int rw, struct extent_io_tree *tree,
1818 struct page *page, sector_t sector,
1819 size_t size, unsigned long offset,
1820 struct block_device *bdev,
1821 struct bio **bio_ret,
1822 unsigned long max_pages,
1823 bio_end_io_t end_io_func,
1825 unsigned long prev_bio_flags,
1826 unsigned long bio_flags)
1832 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1833 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
1834 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
1836 if (bio_ret && *bio_ret) {
1839 contig = bio->bi_sector == sector;
1841 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1844 if (prev_bio_flags != bio_flags || !contig ||
1845 (tree->ops && tree->ops->merge_bio_hook &&
1846 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1848 bio_add_page(bio, page, page_size, offset) < page_size) {
1849 ret = submit_one_bio(rw, bio, mirror_num,
1856 if (this_compressed)
1859 nr = bio_get_nr_vecs(bdev);
1861 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1863 bio_add_page(bio, page, page_size, offset);
1864 bio->bi_end_io = end_io_func;
1865 bio->bi_private = tree;
1870 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
1875 void set_page_extent_mapped(struct page *page)
1877 if (!PagePrivate(page)) {
1878 SetPagePrivate(page);
1879 page_cache_get(page);
1880 set_page_private(page, EXTENT_PAGE_PRIVATE);
1884 static void set_page_extent_head(struct page *page, unsigned long len)
1886 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1890 * basic readpage implementation. Locked extent state structs are inserted
1891 * into the tree that are removed when the IO is done (by the end_io
1894 static int __extent_read_full_page(struct extent_io_tree *tree,
1896 get_extent_t *get_extent,
1897 struct bio **bio, int mirror_num,
1898 unsigned long *bio_flags)
1900 struct inode *inode = page->mapping->host;
1901 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1902 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1906 u64 last_byte = i_size_read(inode);
1910 struct extent_map *em;
1911 struct block_device *bdev;
1914 size_t page_offset = 0;
1916 size_t disk_io_size;
1917 size_t blocksize = inode->i_sb->s_blocksize;
1918 unsigned long this_bio_flag = 0;
1920 set_page_extent_mapped(page);
1923 lock_extent(tree, start, end, GFP_NOFS);
1925 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1927 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1930 iosize = PAGE_CACHE_SIZE - zero_offset;
1931 userpage = kmap_atomic(page, KM_USER0);
1932 memset(userpage + zero_offset, 0, iosize);
1933 flush_dcache_page(page);
1934 kunmap_atomic(userpage, KM_USER0);
1937 while (cur <= end) {
1938 if (cur >= last_byte) {
1940 iosize = PAGE_CACHE_SIZE - page_offset;
1941 userpage = kmap_atomic(page, KM_USER0);
1942 memset(userpage + page_offset, 0, iosize);
1943 flush_dcache_page(page);
1944 kunmap_atomic(userpage, KM_USER0);
1945 set_extent_uptodate(tree, cur, cur + iosize - 1,
1947 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1950 em = get_extent(inode, page, page_offset, cur,
1952 if (IS_ERR(em) || !em) {
1954 unlock_extent(tree, cur, end, GFP_NOFS);
1957 extent_offset = cur - em->start;
1958 BUG_ON(extent_map_end(em) <= cur);
1961 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
1962 this_bio_flag = EXTENT_BIO_COMPRESSED;
1964 iosize = min(extent_map_end(em) - cur, end - cur + 1);
1965 cur_end = min(extent_map_end(em) - 1, end);
1966 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1967 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
1968 disk_io_size = em->block_len;
1969 sector = em->block_start >> 9;
1971 sector = (em->block_start + extent_offset) >> 9;
1972 disk_io_size = iosize;
1975 block_start = em->block_start;
1976 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
1977 block_start = EXTENT_MAP_HOLE;
1978 free_extent_map(em);
1981 /* we've found a hole, just zero and go on */
1982 if (block_start == EXTENT_MAP_HOLE) {
1984 userpage = kmap_atomic(page, KM_USER0);
1985 memset(userpage + page_offset, 0, iosize);
1986 flush_dcache_page(page);
1987 kunmap_atomic(userpage, KM_USER0);
1989 set_extent_uptodate(tree, cur, cur + iosize - 1,
1991 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1993 page_offset += iosize;
1996 /* the get_extent function already copied into the page */
1997 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1998 check_page_uptodate(tree, page);
1999 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2001 page_offset += iosize;
2004 /* we have an inline extent but it didn't get marked up
2005 * to date. Error out
2007 if (block_start == EXTENT_MAP_INLINE) {
2009 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2011 page_offset += iosize;
2016 if (tree->ops && tree->ops->readpage_io_hook) {
2017 ret = tree->ops->readpage_io_hook(page, cur,
2021 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2023 ret = submit_extent_page(READ, tree, page,
2024 sector, disk_io_size, page_offset,
2026 end_bio_extent_readpage, mirror_num,
2030 *bio_flags = this_bio_flag;
2035 page_offset += iosize;
2038 if (!PageError(page))
2039 SetPageUptodate(page);
2045 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2046 get_extent_t *get_extent)
2048 struct bio *bio = NULL;
2049 unsigned long bio_flags = 0;
2052 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2055 submit_one_bio(READ, bio, 0, bio_flags);
2059 static noinline void update_nr_written(struct page *page,
2060 struct writeback_control *wbc,
2061 unsigned long nr_written)
2063 wbc->nr_to_write -= nr_written;
2064 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2065 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2066 page->mapping->writeback_index = page->index + nr_written;
2070 * the writepage semantics are similar to regular writepage. extent
2071 * records are inserted to lock ranges in the tree, and as dirty areas
2072 * are found, they are marked writeback. Then the lock bits are removed
2073 * and the end_io handler clears the writeback ranges
2075 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2078 struct inode *inode = page->mapping->host;
2079 struct extent_page_data *epd = data;
2080 struct extent_io_tree *tree = epd->tree;
2081 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2083 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2087 u64 last_byte = i_size_read(inode);
2092 struct extent_map *em;
2093 struct block_device *bdev;
2096 size_t pg_offset = 0;
2098 loff_t i_size = i_size_read(inode);
2099 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2105 unsigned long nr_written = 0;
2107 if (wbc->sync_mode == WB_SYNC_ALL)
2108 write_flags = WRITE_SYNC_PLUG;
2110 write_flags = WRITE;
2112 WARN_ON(!PageLocked(page));
2113 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2114 if (page->index > end_index ||
2115 (page->index == end_index && !pg_offset)) {
2116 page->mapping->a_ops->invalidatepage(page, 0);
2121 if (page->index == end_index) {
2124 userpage = kmap_atomic(page, KM_USER0);
2125 memset(userpage + pg_offset, 0,
2126 PAGE_CACHE_SIZE - pg_offset);
2127 kunmap_atomic(userpage, KM_USER0);
2128 flush_dcache_page(page);
2132 set_page_extent_mapped(page);
2134 delalloc_start = start;
2137 if (!epd->extent_locked) {
2139 * make sure the wbc mapping index is at least updated
2142 update_nr_written(page, wbc, 0);
2144 while (delalloc_end < page_end) {
2145 nr_delalloc = find_lock_delalloc_range(inode, tree,
2150 if (nr_delalloc == 0) {
2151 delalloc_start = delalloc_end + 1;
2154 tree->ops->fill_delalloc(inode, page, delalloc_start,
2155 delalloc_end, &page_started,
2157 delalloc_start = delalloc_end + 1;
2160 /* did the fill delalloc function already unlock and start
2166 * we've unlocked the page, so we can't update
2167 * the mapping's writeback index, just update
2170 wbc->nr_to_write -= nr_written;
2174 lock_extent(tree, start, page_end, GFP_NOFS);
2176 unlock_start = start;
2178 if (tree->ops && tree->ops->writepage_start_hook) {
2179 ret = tree->ops->writepage_start_hook(page, start,
2181 if (ret == -EAGAIN) {
2182 unlock_extent(tree, start, page_end, GFP_NOFS);
2183 redirty_page_for_writepage(wbc, page);
2184 update_nr_written(page, wbc, nr_written);
2192 * we don't want to touch the inode after unlocking the page,
2193 * so we update the mapping writeback index now
2195 update_nr_written(page, wbc, nr_written + 1);
2198 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0))
2199 printk(KERN_ERR "btrfs delalloc bits after lock_extent\n");
2201 if (last_byte <= start) {
2202 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
2203 unlock_extent(tree, start, page_end, GFP_NOFS);
2204 if (tree->ops && tree->ops->writepage_end_io_hook)
2205 tree->ops->writepage_end_io_hook(page, start,
2207 unlock_start = page_end + 1;
2211 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2212 blocksize = inode->i_sb->s_blocksize;
2214 while (cur <= end) {
2215 if (cur >= last_byte) {
2216 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
2217 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2218 if (tree->ops && tree->ops->writepage_end_io_hook)
2219 tree->ops->writepage_end_io_hook(page, cur,
2221 unlock_start = page_end + 1;
2224 em = epd->get_extent(inode, page, pg_offset, cur,
2226 if (IS_ERR(em) || !em) {
2231 extent_offset = cur - em->start;
2232 BUG_ON(extent_map_end(em) <= cur);
2234 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2235 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2236 sector = (em->block_start + extent_offset) >> 9;
2238 block_start = em->block_start;
2239 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2240 free_extent_map(em);
2244 * compressed and inline extents are written through other
2247 if (compressed || block_start == EXTENT_MAP_HOLE ||
2248 block_start == EXTENT_MAP_INLINE) {
2249 clear_extent_dirty(tree, cur,
2250 cur + iosize - 1, GFP_NOFS);
2252 unlock_extent(tree, unlock_start, cur + iosize - 1,
2256 * end_io notification does not happen here for
2257 * compressed extents
2259 if (!compressed && tree->ops &&
2260 tree->ops->writepage_end_io_hook)
2261 tree->ops->writepage_end_io_hook(page, cur,
2264 else if (compressed) {
2265 /* we don't want to end_page_writeback on
2266 * a compressed extent. this happens
2273 pg_offset += iosize;
2277 /* leave this out until we have a page_mkwrite call */
2278 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2281 pg_offset += iosize;
2285 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2286 if (tree->ops && tree->ops->writepage_io_hook) {
2287 ret = tree->ops->writepage_io_hook(page, cur,
2295 unsigned long max_nr = end_index + 1;
2297 set_range_writeback(tree, cur, cur + iosize - 1);
2298 if (!PageWriteback(page)) {
2299 printk(KERN_ERR "btrfs warning page %lu not "
2300 "writeback, cur %llu end %llu\n",
2301 page->index, (unsigned long long)cur,
2302 (unsigned long long)end);
2305 ret = submit_extent_page(write_flags, tree, page,
2306 sector, iosize, pg_offset,
2307 bdev, &epd->bio, max_nr,
2308 end_bio_extent_writepage,
2314 pg_offset += iosize;
2319 /* make sure the mapping tag for page dirty gets cleared */
2320 set_page_writeback(page);
2321 end_page_writeback(page);
2323 if (unlock_start <= page_end)
2324 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2333 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2334 * @mapping: address space structure to write
2335 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2336 * @writepage: function called for each page
2337 * @data: data passed to writepage function
2339 * If a page is already under I/O, write_cache_pages() skips it, even
2340 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2341 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2342 * and msync() need to guarantee that all the data which was dirty at the time
2343 * the call was made get new I/O started against them. If wbc->sync_mode is
2344 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2345 * existing IO to complete.
2347 static int extent_write_cache_pages(struct extent_io_tree *tree,
2348 struct address_space *mapping,
2349 struct writeback_control *wbc,
2350 writepage_t writepage, void *data,
2351 void (*flush_fn)(void *))
2353 struct backing_dev_info *bdi = mapping->backing_dev_info;
2356 struct pagevec pvec;
2359 pgoff_t end; /* Inclusive */
2361 int range_whole = 0;
2363 pagevec_init(&pvec, 0);
2364 if (wbc->range_cyclic) {
2365 index = mapping->writeback_index; /* Start from prev offset */
2368 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2369 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2370 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2375 while (!done && (index <= end) &&
2376 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2377 PAGECACHE_TAG_DIRTY, min(end - index,
2378 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2382 for (i = 0; i < nr_pages; i++) {
2383 struct page *page = pvec.pages[i];
2386 * At this point we hold neither mapping->tree_lock nor
2387 * lock on the page itself: the page may be truncated or
2388 * invalidated (changing page->mapping to NULL), or even
2389 * swizzled back from swapper_space to tmpfs file
2392 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2393 tree->ops->write_cache_pages_lock_hook(page);
2397 if (unlikely(page->mapping != mapping)) {
2402 if (!wbc->range_cyclic && page->index > end) {
2408 if (wbc->sync_mode != WB_SYNC_NONE) {
2409 if (PageWriteback(page))
2411 wait_on_page_writeback(page);
2414 if (PageWriteback(page) ||
2415 !clear_page_dirty_for_io(page)) {
2420 ret = (*writepage)(page, wbc, data);
2422 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2426 if (ret || wbc->nr_to_write <= 0)
2428 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2429 wbc->encountered_congestion = 1;
2433 pagevec_release(&pvec);
2436 if (!scanned && !done) {
2438 * We hit the last page and there is more work to be done: wrap
2439 * back to the start of the file
2448 static void flush_epd_write_bio(struct extent_page_data *epd)
2452 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2454 submit_one_bio(WRITE, epd->bio, 0, 0);
2459 static noinline void flush_write_bio(void *data)
2461 struct extent_page_data *epd = data;
2462 flush_epd_write_bio(epd);
2465 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2466 get_extent_t *get_extent,
2467 struct writeback_control *wbc)
2470 struct address_space *mapping = page->mapping;
2471 struct extent_page_data epd = {
2474 .get_extent = get_extent,
2476 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
2478 struct writeback_control wbc_writepages = {
2480 .sync_mode = wbc->sync_mode,
2481 .older_than_this = NULL,
2483 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2484 .range_end = (loff_t)-1,
2487 ret = __extent_writepage(page, wbc, &epd);
2489 extent_write_cache_pages(tree, mapping, &wbc_writepages,
2490 __extent_writepage, &epd, flush_write_bio);
2491 flush_epd_write_bio(&epd);
2495 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2496 u64 start, u64 end, get_extent_t *get_extent,
2500 struct address_space *mapping = inode->i_mapping;
2502 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2505 struct extent_page_data epd = {
2508 .get_extent = get_extent,
2510 .sync_io = mode == WB_SYNC_ALL,
2512 struct writeback_control wbc_writepages = {
2513 .bdi = inode->i_mapping->backing_dev_info,
2515 .older_than_this = NULL,
2516 .nr_to_write = nr_pages * 2,
2517 .range_start = start,
2518 .range_end = end + 1,
2521 while (start <= end) {
2522 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2523 if (clear_page_dirty_for_io(page))
2524 ret = __extent_writepage(page, &wbc_writepages, &epd);
2526 if (tree->ops && tree->ops->writepage_end_io_hook)
2527 tree->ops->writepage_end_io_hook(page, start,
2528 start + PAGE_CACHE_SIZE - 1,
2532 page_cache_release(page);
2533 start += PAGE_CACHE_SIZE;
2536 flush_epd_write_bio(&epd);
2540 int extent_writepages(struct extent_io_tree *tree,
2541 struct address_space *mapping,
2542 get_extent_t *get_extent,
2543 struct writeback_control *wbc)
2546 struct extent_page_data epd = {
2549 .get_extent = get_extent,
2551 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
2554 ret = extent_write_cache_pages(tree, mapping, wbc,
2555 __extent_writepage, &epd,
2557 flush_epd_write_bio(&epd);
2561 int extent_readpages(struct extent_io_tree *tree,
2562 struct address_space *mapping,
2563 struct list_head *pages, unsigned nr_pages,
2564 get_extent_t get_extent)
2566 struct bio *bio = NULL;
2568 struct pagevec pvec;
2569 unsigned long bio_flags = 0;
2571 pagevec_init(&pvec, 0);
2572 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2573 struct page *page = list_entry(pages->prev, struct page, lru);
2575 prefetchw(&page->flags);
2576 list_del(&page->lru);
2578 * what we want to do here is call add_to_page_cache_lru,
2579 * but that isn't exported, so we reproduce it here
2581 if (!add_to_page_cache(page, mapping,
2582 page->index, GFP_KERNEL)) {
2584 /* open coding of lru_cache_add, also not exported */
2585 page_cache_get(page);
2586 if (!pagevec_add(&pvec, page))
2587 __pagevec_lru_add_file(&pvec);
2588 __extent_read_full_page(tree, page, get_extent,
2589 &bio, 0, &bio_flags);
2591 page_cache_release(page);
2593 if (pagevec_count(&pvec))
2594 __pagevec_lru_add_file(&pvec);
2595 BUG_ON(!list_empty(pages));
2597 submit_one_bio(READ, bio, 0, bio_flags);
2602 * basic invalidatepage code, this waits on any locked or writeback
2603 * ranges corresponding to the page, and then deletes any extent state
2604 * records from the tree
2606 int extent_invalidatepage(struct extent_io_tree *tree,
2607 struct page *page, unsigned long offset)
2609 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2610 u64 end = start + PAGE_CACHE_SIZE - 1;
2611 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2613 start += (offset + blocksize - 1) & ~(blocksize - 1);
2617 lock_extent(tree, start, end, GFP_NOFS);
2618 wait_on_extent_writeback(tree, start, end);
2619 clear_extent_bit(tree, start, end,
2620 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2626 * simple commit_write call, set_range_dirty is used to mark both
2627 * the pages and the extent records as dirty
2629 int extent_commit_write(struct extent_io_tree *tree,
2630 struct inode *inode, struct page *page,
2631 unsigned from, unsigned to)
2633 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2635 set_page_extent_mapped(page);
2636 set_page_dirty(page);
2638 if (pos > inode->i_size) {
2639 i_size_write(inode, pos);
2640 mark_inode_dirty(inode);
2645 int extent_prepare_write(struct extent_io_tree *tree,
2646 struct inode *inode, struct page *page,
2647 unsigned from, unsigned to, get_extent_t *get_extent)
2649 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2650 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2652 u64 orig_block_start;
2655 struct extent_map *em;
2656 unsigned blocksize = 1 << inode->i_blkbits;
2657 size_t page_offset = 0;
2658 size_t block_off_start;
2659 size_t block_off_end;
2665 set_page_extent_mapped(page);
2667 block_start = (page_start + from) & ~((u64)blocksize - 1);
2668 block_end = (page_start + to - 1) | (blocksize - 1);
2669 orig_block_start = block_start;
2671 lock_extent(tree, page_start, page_end, GFP_NOFS);
2672 while (block_start <= block_end) {
2673 em = get_extent(inode, page, page_offset, block_start,
2674 block_end - block_start + 1, 1);
2675 if (IS_ERR(em) || !em)
2678 cur_end = min(block_end, extent_map_end(em) - 1);
2679 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2680 block_off_end = block_off_start + blocksize;
2681 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2683 if (!PageUptodate(page) && isnew &&
2684 (block_off_end > to || block_off_start < from)) {
2687 kaddr = kmap_atomic(page, KM_USER0);
2688 if (block_off_end > to)
2689 memset(kaddr + to, 0, block_off_end - to);
2690 if (block_off_start < from)
2691 memset(kaddr + block_off_start, 0,
2692 from - block_off_start);
2693 flush_dcache_page(page);
2694 kunmap_atomic(kaddr, KM_USER0);
2696 if ((em->block_start != EXTENT_MAP_HOLE &&
2697 em->block_start != EXTENT_MAP_INLINE) &&
2698 !isnew && !PageUptodate(page) &&
2699 (block_off_end > to || block_off_start < from) &&
2700 !test_range_bit(tree, block_start, cur_end,
2701 EXTENT_UPTODATE, 1)) {
2703 u64 extent_offset = block_start - em->start;
2705 sector = (em->block_start + extent_offset) >> 9;
2706 iosize = (cur_end - block_start + blocksize) &
2707 ~((u64)blocksize - 1);
2709 * we've already got the extent locked, but we
2710 * need to split the state such that our end_bio
2711 * handler can clear the lock.
2713 set_extent_bit(tree, block_start,
2714 block_start + iosize - 1,
2715 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2716 ret = submit_extent_page(READ, tree, page,
2717 sector, iosize, page_offset, em->bdev,
2719 end_bio_extent_preparewrite, 0,
2722 block_start = block_start + iosize;
2724 set_extent_uptodate(tree, block_start, cur_end,
2726 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2727 block_start = cur_end + 1;
2729 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2730 free_extent_map(em);
2733 wait_extent_bit(tree, orig_block_start,
2734 block_end, EXTENT_LOCKED);
2736 check_page_uptodate(tree, page);
2738 /* FIXME, zero out newly allocated blocks on error */
2743 * a helper for releasepage, this tests for areas of the page that
2744 * are locked or under IO and drops the related state bits if it is safe
2747 int try_release_extent_state(struct extent_map_tree *map,
2748 struct extent_io_tree *tree, struct page *page,
2751 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2752 u64 end = start + PAGE_CACHE_SIZE - 1;
2755 if (test_range_bit(tree, start, end,
2756 EXTENT_IOBITS | EXTENT_ORDERED, 0))
2759 if ((mask & GFP_NOFS) == GFP_NOFS)
2761 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2768 * a helper for releasepage. As long as there are no locked extents
2769 * in the range corresponding to the page, both state records and extent
2770 * map records are removed
2772 int try_release_extent_mapping(struct extent_map_tree *map,
2773 struct extent_io_tree *tree, struct page *page,
2776 struct extent_map *em;
2777 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2778 u64 end = start + PAGE_CACHE_SIZE - 1;
2780 if ((mask & __GFP_WAIT) &&
2781 page->mapping->host->i_size > 16 * 1024 * 1024) {
2783 while (start <= end) {
2784 len = end - start + 1;
2785 spin_lock(&map->lock);
2786 em = lookup_extent_mapping(map, start, len);
2787 if (!em || IS_ERR(em)) {
2788 spin_unlock(&map->lock);
2791 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2792 em->start != start) {
2793 spin_unlock(&map->lock);
2794 free_extent_map(em);
2797 if (!test_range_bit(tree, em->start,
2798 extent_map_end(em) - 1,
2799 EXTENT_LOCKED | EXTENT_WRITEBACK |
2802 remove_extent_mapping(map, em);
2803 /* once for the rb tree */
2804 free_extent_map(em);
2806 start = extent_map_end(em);
2807 spin_unlock(&map->lock);
2810 free_extent_map(em);
2813 return try_release_extent_state(map, tree, page, mask);
2816 sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2817 get_extent_t *get_extent)
2819 struct inode *inode = mapping->host;
2820 u64 start = iblock << inode->i_blkbits;
2821 sector_t sector = 0;
2822 size_t blksize = (1 << inode->i_blkbits);
2823 struct extent_map *em;
2825 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2827 em = get_extent(inode, NULL, 0, start, blksize, 0);
2828 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2830 if (!em || IS_ERR(em))
2833 if (em->block_start > EXTENT_MAP_LAST_BYTE)
2836 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2838 free_extent_map(em);
2842 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2843 __u64 start, __u64 len, get_extent_t *get_extent)
2847 u64 max = start + len;
2850 struct extent_map *em = NULL;
2852 u64 em_start = 0, em_len = 0;
2853 unsigned long emflags;
2859 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2861 em = get_extent(inode, NULL, 0, off, max - off, 0);
2869 off = em->start + em->len;
2873 em_start = em->start;
2879 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
2881 flags |= FIEMAP_EXTENT_LAST;
2882 } else if (em->block_start == EXTENT_MAP_HOLE) {
2883 flags |= FIEMAP_EXTENT_UNWRITTEN;
2884 } else if (em->block_start == EXTENT_MAP_INLINE) {
2885 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2886 FIEMAP_EXTENT_NOT_ALIGNED);
2887 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
2888 flags |= (FIEMAP_EXTENT_DELALLOC |
2889 FIEMAP_EXTENT_UNKNOWN);
2891 disko = em->block_start;
2893 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2894 flags |= FIEMAP_EXTENT_ENCODED;
2896 emflags = em->flags;
2897 free_extent_map(em);
2901 em = get_extent(inode, NULL, 0, off, max - off, 0);
2908 emflags = em->flags;
2910 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
2911 flags |= FIEMAP_EXTENT_LAST;
2915 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2921 free_extent_map(em);
2923 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2928 static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2932 struct address_space *mapping;
2935 return eb->first_page;
2936 i += eb->start >> PAGE_CACHE_SHIFT;
2937 mapping = eb->first_page->mapping;
2942 * extent_buffer_page is only called after pinning the page
2943 * by increasing the reference count. So we know the page must
2944 * be in the radix tree.
2947 p = radix_tree_lookup(&mapping->page_tree, i);
2953 static inline unsigned long num_extent_pages(u64 start, u64 len)
2955 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2956 (start >> PAGE_CACHE_SHIFT);
2959 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2964 struct extent_buffer *eb = NULL;
2966 unsigned long flags;
2969 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
2972 spin_lock_init(&eb->lock);
2973 init_waitqueue_head(&eb->lock_wq);
2976 spin_lock_irqsave(&leak_lock, flags);
2977 list_add(&eb->leak_list, &buffers);
2978 spin_unlock_irqrestore(&leak_lock, flags);
2980 atomic_set(&eb->refs, 1);
2985 static void __free_extent_buffer(struct extent_buffer *eb)
2988 unsigned long flags;
2989 spin_lock_irqsave(&leak_lock, flags);
2990 list_del(&eb->leak_list);
2991 spin_unlock_irqrestore(&leak_lock, flags);
2993 kmem_cache_free(extent_buffer_cache, eb);
2996 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
2997 u64 start, unsigned long len,
3001 unsigned long num_pages = num_extent_pages(start, len);
3003 unsigned long index = start >> PAGE_CACHE_SHIFT;
3004 struct extent_buffer *eb;
3005 struct extent_buffer *exists = NULL;
3007 struct address_space *mapping = tree->mapping;
3010 spin_lock(&tree->buffer_lock);
3011 eb = buffer_search(tree, start);
3013 atomic_inc(&eb->refs);
3014 spin_unlock(&tree->buffer_lock);
3015 mark_page_accessed(eb->first_page);
3018 spin_unlock(&tree->buffer_lock);
3020 eb = __alloc_extent_buffer(tree, start, len, mask);
3025 eb->first_page = page0;
3028 page_cache_get(page0);
3029 mark_page_accessed(page0);
3030 set_page_extent_mapped(page0);
3031 set_page_extent_head(page0, len);
3032 uptodate = PageUptodate(page0);
3036 for (; i < num_pages; i++, index++) {
3037 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3042 set_page_extent_mapped(p);
3043 mark_page_accessed(p);
3046 set_page_extent_head(p, len);
3048 set_page_private(p, EXTENT_PAGE_PRIVATE);
3050 if (!PageUptodate(p))
3055 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3057 spin_lock(&tree->buffer_lock);
3058 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3060 /* add one reference for the caller */
3061 atomic_inc(&exists->refs);
3062 spin_unlock(&tree->buffer_lock);
3065 spin_unlock(&tree->buffer_lock);
3067 /* add one reference for the tree */
3068 atomic_inc(&eb->refs);
3072 if (!atomic_dec_and_test(&eb->refs))
3074 for (index = 1; index < i; index++)
3075 page_cache_release(extent_buffer_page(eb, index));
3076 page_cache_release(extent_buffer_page(eb, 0));
3077 __free_extent_buffer(eb);
3081 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3082 u64 start, unsigned long len,
3085 struct extent_buffer *eb;
3087 spin_lock(&tree->buffer_lock);
3088 eb = buffer_search(tree, start);
3090 atomic_inc(&eb->refs);
3091 spin_unlock(&tree->buffer_lock);
3094 mark_page_accessed(eb->first_page);
3099 void free_extent_buffer(struct extent_buffer *eb)
3104 if (!atomic_dec_and_test(&eb->refs))
3110 int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3111 struct extent_buffer *eb)
3114 unsigned long num_pages;
3117 num_pages = num_extent_pages(eb->start, eb->len);
3119 for (i = 0; i < num_pages; i++) {
3120 page = extent_buffer_page(eb, i);
3121 if (!PageDirty(page))
3126 set_page_extent_head(page, eb->len);
3128 set_page_private(page, EXTENT_PAGE_PRIVATE);
3130 clear_page_dirty_for_io(page);
3131 spin_lock_irq(&page->mapping->tree_lock);
3132 if (!PageDirty(page)) {
3133 radix_tree_tag_clear(&page->mapping->page_tree,
3135 PAGECACHE_TAG_DIRTY);
3137 spin_unlock_irq(&page->mapping->tree_lock);
3143 int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3144 struct extent_buffer *eb)
3146 return wait_on_extent_writeback(tree, eb->start,
3147 eb->start + eb->len - 1);
3150 int set_extent_buffer_dirty(struct extent_io_tree *tree,
3151 struct extent_buffer *eb)
3154 unsigned long num_pages;
3157 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3158 num_pages = num_extent_pages(eb->start, eb->len);
3159 for (i = 0; i < num_pages; i++)
3160 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
3164 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3165 struct extent_buffer *eb)
3169 unsigned long num_pages;
3171 num_pages = num_extent_pages(eb->start, eb->len);
3172 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3174 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3176 for (i = 0; i < num_pages; i++) {
3177 page = extent_buffer_page(eb, i);
3179 ClearPageUptodate(page);
3184 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3185 struct extent_buffer *eb)
3189 unsigned long num_pages;
3191 num_pages = num_extent_pages(eb->start, eb->len);
3193 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3195 for (i = 0; i < num_pages; i++) {
3196 page = extent_buffer_page(eb, i);
3197 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3198 ((i == num_pages - 1) &&
3199 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3200 check_page_uptodate(tree, page);
3203 SetPageUptodate(page);
3208 int extent_range_uptodate(struct extent_io_tree *tree,
3213 int pg_uptodate = 1;
3215 unsigned long index;
3217 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3220 while (start <= end) {
3221 index = start >> PAGE_CACHE_SHIFT;
3222 page = find_get_page(tree->mapping, index);
3223 uptodate = PageUptodate(page);
3224 page_cache_release(page);
3229 start += PAGE_CACHE_SIZE;
3234 int extent_buffer_uptodate(struct extent_io_tree *tree,
3235 struct extent_buffer *eb)
3238 unsigned long num_pages;
3241 int pg_uptodate = 1;
3243 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3246 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3247 EXTENT_UPTODATE, 1);
3251 num_pages = num_extent_pages(eb->start, eb->len);
3252 for (i = 0; i < num_pages; i++) {
3253 page = extent_buffer_page(eb, i);
3254 if (!PageUptodate(page)) {
3262 int read_extent_buffer_pages(struct extent_io_tree *tree,
3263 struct extent_buffer *eb,
3264 u64 start, int wait,
3265 get_extent_t *get_extent, int mirror_num)
3268 unsigned long start_i;
3272 int locked_pages = 0;
3273 int all_uptodate = 1;
3274 int inc_all_pages = 0;
3275 unsigned long num_pages;
3276 struct bio *bio = NULL;
3277 unsigned long bio_flags = 0;
3279 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3282 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3283 EXTENT_UPTODATE, 1)) {
3288 WARN_ON(start < eb->start);
3289 start_i = (start >> PAGE_CACHE_SHIFT) -
3290 (eb->start >> PAGE_CACHE_SHIFT);
3295 num_pages = num_extent_pages(eb->start, eb->len);
3296 for (i = start_i; i < num_pages; i++) {
3297 page = extent_buffer_page(eb, i);
3299 if (!trylock_page(page))
3305 if (!PageUptodate(page))
3310 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3314 for (i = start_i; i < num_pages; i++) {
3315 page = extent_buffer_page(eb, i);
3317 page_cache_get(page);
3318 if (!PageUptodate(page)) {
3321 ClearPageError(page);
3322 err = __extent_read_full_page(tree, page,
3324 mirror_num, &bio_flags);
3333 submit_one_bio(READ, bio, mirror_num, bio_flags);
3338 for (i = start_i; i < num_pages; i++) {
3339 page = extent_buffer_page(eb, i);
3340 wait_on_page_locked(page);
3341 if (!PageUptodate(page))
3346 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3351 while (locked_pages > 0) {
3352 page = extent_buffer_page(eb, i);
3360 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3361 unsigned long start,
3368 char *dst = (char *)dstv;
3369 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3370 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3372 WARN_ON(start > eb->len);
3373 WARN_ON(start + len > eb->start + eb->len);
3375 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3378 page = extent_buffer_page(eb, i);
3380 cur = min(len, (PAGE_CACHE_SIZE - offset));
3381 kaddr = kmap_atomic(page, KM_USER1);
3382 memcpy(dst, kaddr + offset, cur);
3383 kunmap_atomic(kaddr, KM_USER1);
3392 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3393 unsigned long min_len, char **token, char **map,
3394 unsigned long *map_start,
3395 unsigned long *map_len, int km)
3397 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3400 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3401 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3402 unsigned long end_i = (start_offset + start + min_len - 1) >>
3409 offset = start_offset;
3413 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3416 if (start + min_len > eb->len) {
3417 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3418 "wanted %lu %lu\n", (unsigned long long)eb->start,
3419 eb->len, start, min_len);
3423 p = extent_buffer_page(eb, i);
3424 kaddr = kmap_atomic(p, km);
3426 *map = kaddr + offset;
3427 *map_len = PAGE_CACHE_SIZE - offset;
3431 int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3432 unsigned long min_len,
3433 char **token, char **map,
3434 unsigned long *map_start,
3435 unsigned long *map_len, int km)
3439 if (eb->map_token) {
3440 unmap_extent_buffer(eb, eb->map_token, km);
3441 eb->map_token = NULL;
3444 err = map_private_extent_buffer(eb, start, min_len, token, map,
3445 map_start, map_len, km);
3447 eb->map_token = *token;
3449 eb->map_start = *map_start;
3450 eb->map_len = *map_len;
3455 void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3457 kunmap_atomic(token, km);
3460 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3461 unsigned long start,
3468 char *ptr = (char *)ptrv;
3469 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3470 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3473 WARN_ON(start > eb->len);
3474 WARN_ON(start + len > eb->start + eb->len);
3476 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3479 page = extent_buffer_page(eb, i);
3481 cur = min(len, (PAGE_CACHE_SIZE - offset));
3483 kaddr = kmap_atomic(page, KM_USER0);
3484 ret = memcmp(ptr, kaddr + offset, cur);
3485 kunmap_atomic(kaddr, KM_USER0);
3497 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3498 unsigned long start, unsigned long len)
3504 char *src = (char *)srcv;
3505 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3506 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3508 WARN_ON(start > eb->len);
3509 WARN_ON(start + len > eb->start + eb->len);
3511 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3514 page = extent_buffer_page(eb, i);
3515 WARN_ON(!PageUptodate(page));
3517 cur = min(len, PAGE_CACHE_SIZE - offset);
3518 kaddr = kmap_atomic(page, KM_USER1);
3519 memcpy(kaddr + offset, src, cur);
3520 kunmap_atomic(kaddr, KM_USER1);
3529 void memset_extent_buffer(struct extent_buffer *eb, char c,
3530 unsigned long start, unsigned long len)
3536 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3537 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3539 WARN_ON(start > eb->len);
3540 WARN_ON(start + len > eb->start + eb->len);
3542 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3545 page = extent_buffer_page(eb, i);
3546 WARN_ON(!PageUptodate(page));
3548 cur = min(len, PAGE_CACHE_SIZE - offset);
3549 kaddr = kmap_atomic(page, KM_USER0);
3550 memset(kaddr + offset, c, cur);
3551 kunmap_atomic(kaddr, KM_USER0);
3559 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3560 unsigned long dst_offset, unsigned long src_offset,
3563 u64 dst_len = dst->len;
3568 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3569 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3571 WARN_ON(src->len != dst_len);
3573 offset = (start_offset + dst_offset) &
3574 ((unsigned long)PAGE_CACHE_SIZE - 1);
3577 page = extent_buffer_page(dst, i);
3578 WARN_ON(!PageUptodate(page));
3580 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3582 kaddr = kmap_atomic(page, KM_USER0);
3583 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3584 kunmap_atomic(kaddr, KM_USER0);
3593 static void move_pages(struct page *dst_page, struct page *src_page,
3594 unsigned long dst_off, unsigned long src_off,
3597 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3598 if (dst_page == src_page) {
3599 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3601 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3602 char *p = dst_kaddr + dst_off + len;
3603 char *s = src_kaddr + src_off + len;
3608 kunmap_atomic(src_kaddr, KM_USER1);
3610 kunmap_atomic(dst_kaddr, KM_USER0);
3613 static void copy_pages(struct page *dst_page, struct page *src_page,
3614 unsigned long dst_off, unsigned long src_off,
3617 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3620 if (dst_page != src_page)
3621 src_kaddr = kmap_atomic(src_page, KM_USER1);
3623 src_kaddr = dst_kaddr;
3625 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3626 kunmap_atomic(dst_kaddr, KM_USER0);
3627 if (dst_page != src_page)
3628 kunmap_atomic(src_kaddr, KM_USER1);
3631 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3632 unsigned long src_offset, unsigned long len)
3635 size_t dst_off_in_page;
3636 size_t src_off_in_page;
3637 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3638 unsigned long dst_i;
3639 unsigned long src_i;
3641 if (src_offset + len > dst->len) {
3642 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3643 "len %lu dst len %lu\n", src_offset, len, dst->len);
3646 if (dst_offset + len > dst->len) {
3647 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3648 "len %lu dst len %lu\n", dst_offset, len, dst->len);
3653 dst_off_in_page = (start_offset + dst_offset) &
3654 ((unsigned long)PAGE_CACHE_SIZE - 1);
3655 src_off_in_page = (start_offset + src_offset) &
3656 ((unsigned long)PAGE_CACHE_SIZE - 1);
3658 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3659 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3661 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3663 cur = min_t(unsigned long, cur,
3664 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3666 copy_pages(extent_buffer_page(dst, dst_i),
3667 extent_buffer_page(dst, src_i),
3668 dst_off_in_page, src_off_in_page, cur);
3676 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3677 unsigned long src_offset, unsigned long len)
3680 size_t dst_off_in_page;
3681 size_t src_off_in_page;
3682 unsigned long dst_end = dst_offset + len - 1;
3683 unsigned long src_end = src_offset + len - 1;
3684 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3685 unsigned long dst_i;
3686 unsigned long src_i;
3688 if (src_offset + len > dst->len) {
3689 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3690 "len %lu len %lu\n", src_offset, len, dst->len);
3693 if (dst_offset + len > dst->len) {
3694 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3695 "len %lu len %lu\n", dst_offset, len, dst->len);
3698 if (dst_offset < src_offset) {
3699 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3703 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3704 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3706 dst_off_in_page = (start_offset + dst_end) &
3707 ((unsigned long)PAGE_CACHE_SIZE - 1);
3708 src_off_in_page = (start_offset + src_end) &
3709 ((unsigned long)PAGE_CACHE_SIZE - 1);
3711 cur = min_t(unsigned long, len, src_off_in_page + 1);
3712 cur = min(cur, dst_off_in_page + 1);
3713 move_pages(extent_buffer_page(dst, dst_i),
3714 extent_buffer_page(dst, src_i),
3715 dst_off_in_page - cur + 1,
3716 src_off_in_page - cur + 1, cur);
3724 int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3726 u64 start = page_offset(page);
3727 struct extent_buffer *eb;
3730 unsigned long num_pages;
3732 spin_lock(&tree->buffer_lock);
3733 eb = buffer_search(tree, start);
3737 if (atomic_read(&eb->refs) > 1) {
3741 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3745 /* at this point we can safely release the extent buffer */
3746 num_pages = num_extent_pages(eb->start, eb->len);
3747 for (i = 0; i < num_pages; i++)
3748 page_cache_release(extent_buffer_page(eb, i));
3749 rb_erase(&eb->rb_node, &tree->buffer);
3750 __free_extent_buffer(eb);
3752 spin_unlock(&tree->buffer_lock);