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/version.h>
13 #include <linux/writeback.h>
14 #include <linux/pagevec.h>
15 #include "extent_io.h"
16 #include "extent_map.h"
18 /* temporary define until extent_map moves out of btrfs */
19 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
20 unsigned long extra_flags,
21 void (*ctor)(void *, struct kmem_cache *,
24 static struct kmem_cache *extent_state_cache;
25 static struct kmem_cache *extent_buffer_cache;
27 static LIST_HEAD(buffers);
28 static LIST_HEAD(states);
29 static spinlock_t leak_lock = SPIN_LOCK_UNLOCKED;
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;
45 int __init extent_io_init(void)
47 extent_state_cache = btrfs_cache_create("extent_state",
48 sizeof(struct extent_state), 0,
50 if (!extent_state_cache)
53 extent_buffer_cache = btrfs_cache_create("extent_buffers",
54 sizeof(struct extent_buffer), 0,
56 if (!extent_buffer_cache)
57 goto free_state_cache;
61 kmem_cache_destroy(extent_state_cache);
65 void extent_io_exit(void)
67 struct extent_state *state;
68 struct extent_buffer *eb;
70 while (!list_empty(&states)) {
71 state = list_entry(states.next, struct extent_state, leak_list);
72 printk("state leak: start %Lu end %Lu state %lu in tree %p refs %d\n", state->start, state->end, state->state, state->tree, atomic_read(&state->refs));
73 list_del(&state->leak_list);
74 kmem_cache_free(extent_state_cache, state);
78 while (!list_empty(&buffers)) {
79 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
80 printk("buffer leak start %Lu len %lu refs %d\n", eb->start, eb->len, atomic_read(&eb->refs));
81 list_del(&eb->leak_list);
82 kmem_cache_free(extent_buffer_cache, eb);
84 if (extent_state_cache)
85 kmem_cache_destroy(extent_state_cache);
86 if (extent_buffer_cache)
87 kmem_cache_destroy(extent_buffer_cache);
90 void extent_io_tree_init(struct extent_io_tree *tree,
91 struct address_space *mapping, gfp_t mask)
93 tree->state.rb_node = NULL;
94 tree->buffer.rb_node = NULL;
96 tree->dirty_bytes = 0;
97 spin_lock_init(&tree->lock);
98 spin_lock_init(&tree->buffer_lock);
99 tree->mapping = mapping;
102 EXPORT_SYMBOL(extent_io_tree_init);
104 struct extent_state *alloc_extent_state(gfp_t mask)
106 struct extent_state *state;
109 state = kmem_cache_alloc(extent_state_cache, mask);
115 spin_lock_irqsave(&leak_lock, flags);
116 list_add(&state->leak_list, &states);
117 spin_unlock_irqrestore(&leak_lock, flags);
119 atomic_set(&state->refs, 1);
120 init_waitqueue_head(&state->wq);
123 EXPORT_SYMBOL(alloc_extent_state);
125 void free_extent_state(struct extent_state *state)
129 if (atomic_dec_and_test(&state->refs)) {
131 WARN_ON(state->tree);
132 spin_lock_irqsave(&leak_lock, flags);
133 list_del(&state->leak_list);
134 spin_unlock_irqrestore(&leak_lock, flags);
135 kmem_cache_free(extent_state_cache, state);
138 EXPORT_SYMBOL(free_extent_state);
140 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
141 struct rb_node *node)
143 struct rb_node ** p = &root->rb_node;
144 struct rb_node * parent = NULL;
145 struct tree_entry *entry;
149 entry = rb_entry(parent, struct tree_entry, rb_node);
151 if (offset < entry->start)
153 else if (offset > entry->end)
159 entry = rb_entry(node, struct tree_entry, rb_node);
160 rb_link_node(node, parent, p);
161 rb_insert_color(node, root);
165 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
166 struct rb_node **prev_ret,
167 struct rb_node **next_ret)
169 struct rb_root *root = &tree->state;
170 struct rb_node * n = root->rb_node;
171 struct rb_node *prev = NULL;
172 struct rb_node *orig_prev = NULL;
173 struct tree_entry *entry;
174 struct tree_entry *prev_entry = NULL;
177 struct extent_state *state;
179 if (state->start <= offset && offset <= state->end)
180 return &tree->last->rb_node;
183 entry = rb_entry(n, struct tree_entry, rb_node);
187 if (offset < entry->start)
189 else if (offset > entry->end)
192 tree->last = rb_entry(n, struct extent_state, rb_node);
199 while(prev && offset > prev_entry->end) {
200 prev = rb_next(prev);
201 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
208 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
209 while(prev && offset < prev_entry->start) {
210 prev = rb_prev(prev);
211 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
218 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
221 struct rb_node *prev = NULL;
224 ret = __etree_search(tree, offset, &prev, NULL);
227 tree->last = rb_entry(prev, struct extent_state,
235 static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
236 u64 offset, struct rb_node *node)
238 struct rb_root *root = &tree->buffer;
239 struct rb_node ** p = &root->rb_node;
240 struct rb_node * parent = NULL;
241 struct extent_buffer *eb;
245 eb = rb_entry(parent, struct extent_buffer, rb_node);
247 if (offset < eb->start)
249 else if (offset > eb->start)
255 rb_link_node(node, parent, p);
256 rb_insert_color(node, root);
260 static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
263 struct rb_root *root = &tree->buffer;
264 struct rb_node * n = root->rb_node;
265 struct extent_buffer *eb;
268 eb = rb_entry(n, struct extent_buffer, rb_node);
269 if (offset < eb->start)
271 else if (offset > eb->start)
280 * utility function to look for merge candidates inside a given range.
281 * Any extents with matching state are merged together into a single
282 * extent in the tree. Extents with EXTENT_IO in their state field
283 * are not merged because the end_io handlers need to be able to do
284 * operations on them without sleeping (or doing allocations/splits).
286 * This should be called with the tree lock held.
288 static int merge_state(struct extent_io_tree *tree,
289 struct extent_state *state)
291 struct extent_state *other;
292 struct rb_node *other_node;
294 if (state->state & EXTENT_IOBITS)
297 other_node = rb_prev(&state->rb_node);
299 other = rb_entry(other_node, struct extent_state, rb_node);
300 if (other->end == state->start - 1 &&
301 other->state == state->state) {
302 state->start = other->start;
304 if (tree->last == other)
306 rb_erase(&other->rb_node, &tree->state);
307 free_extent_state(other);
310 other_node = rb_next(&state->rb_node);
312 other = rb_entry(other_node, struct extent_state, rb_node);
313 if (other->start == state->end + 1 &&
314 other->state == state->state) {
315 other->start = state->start;
317 if (tree->last == state)
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->set_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("end < start %Lu %Lu\n", end, start);
366 if (bits & EXTENT_DIRTY)
367 tree->dirty_bytes += end - start + 1;
368 set_state_cb(tree, state, bits);
369 state->state |= bits;
370 state->start = start;
372 node = tree_insert(&tree->state, end, &state->rb_node);
374 struct extent_state *found;
375 found = rb_entry(node, struct extent_state, rb_node);
376 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
377 free_extent_state(state);
382 merge_state(tree, state);
387 * split a given extent state struct in two, inserting the preallocated
388 * struct 'prealloc' as the newly created second half. 'split' indicates an
389 * offset inside 'orig' where it should be split.
392 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
393 * are two extent state structs in the tree:
394 * prealloc: [orig->start, split - 1]
395 * orig: [ split, orig->end ]
397 * The tree locks are not taken by this function. They need to be held
400 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
401 struct extent_state *prealloc, u64 split)
403 struct rb_node *node;
404 prealloc->start = orig->start;
405 prealloc->end = split - 1;
406 prealloc->state = orig->state;
409 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
411 struct extent_state *found;
412 found = rb_entry(node, struct extent_state, rb_node);
413 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end);
414 free_extent_state(prealloc);
417 prealloc->tree = tree;
422 * utility function to clear some bits in an extent state struct.
423 * it will optionally wake up any one waiting on this state (wake == 1), or
424 * forcibly remove the state from the tree (delete == 1).
426 * If no bits are set on the state struct after clearing things, the
427 * struct is freed and removed from the tree
429 static int clear_state_bit(struct extent_io_tree *tree,
430 struct extent_state *state, int bits, int wake,
433 int ret = state->state & bits;
435 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
436 u64 range = state->end - state->start + 1;
437 WARN_ON(range > tree->dirty_bytes);
438 tree->dirty_bytes -= range;
440 clear_state_cb(tree, state, bits);
441 state->state &= ~bits;
444 if (delete || state->state == 0) {
446 clear_state_cb(tree, state, state->state);
447 if (tree->last == state) {
448 tree->last = extent_state_next(state);
450 rb_erase(&state->rb_node, &tree->state);
452 free_extent_state(state);
457 merge_state(tree, state);
463 * clear some bits on a range in the tree. This may require splitting
464 * or inserting elements in the tree, so the gfp mask is used to
465 * indicate which allocations or sleeping are allowed.
467 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
468 * the given range from the tree regardless of state (ie for truncate).
470 * the range [start, end] is inclusive.
472 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
473 * bits were already set, or zero if none of the bits were already set.
475 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
476 int bits, int wake, int delete, gfp_t mask)
478 struct extent_state *state;
479 struct extent_state *prealloc = NULL;
480 struct rb_node *node;
486 if (!prealloc && (mask & __GFP_WAIT)) {
487 prealloc = alloc_extent_state(mask);
492 spin_lock_irqsave(&tree->lock, flags);
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_irqrestore(&tree->lock, flags);
565 free_extent_state(prealloc);
572 spin_unlock_irqrestore(&tree->lock, flags);
573 if (mask & __GFP_WAIT)
577 EXPORT_SYMBOL(clear_extent_bit);
579 static int wait_on_state(struct extent_io_tree *tree,
580 struct extent_state *state)
583 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
584 spin_unlock_irq(&tree->lock);
586 spin_lock_irq(&tree->lock);
587 finish_wait(&state->wq, &wait);
592 * waits for one or more bits to clear on a range in the state tree.
593 * The range [start, end] is inclusive.
594 * The tree lock is taken by this function
596 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
598 struct extent_state *state;
599 struct rb_node *node;
601 spin_lock_irq(&tree->lock);
605 * this search will find all the extents that end after
608 node = tree_search(tree, start);
612 state = rb_entry(node, struct extent_state, rb_node);
614 if (state->start > end)
617 if (state->state & bits) {
618 start = state->start;
619 atomic_inc(&state->refs);
620 wait_on_state(tree, state);
621 free_extent_state(state);
624 start = state->end + 1;
629 if (need_resched()) {
630 spin_unlock_irq(&tree->lock);
632 spin_lock_irq(&tree->lock);
636 spin_unlock_irq(&tree->lock);
639 EXPORT_SYMBOL(wait_extent_bit);
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 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
665 int exclusive, u64 *failed_start, gfp_t mask)
667 struct extent_state *state;
668 struct extent_state *prealloc = NULL;
669 struct rb_node *node;
676 if (!prealloc && (mask & __GFP_WAIT)) {
677 prealloc = alloc_extent_state(mask);
682 spin_lock_irqsave(&tree->lock, flags);
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_irqrestore(&tree->lock, flags);
804 free_extent_state(prealloc);
811 spin_unlock_irqrestore(&tree->lock, flags);
812 if (mask & __GFP_WAIT)
816 EXPORT_SYMBOL(set_extent_bit);
818 /* wrappers around set/clear extent bit */
819 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
822 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
825 EXPORT_SYMBOL(set_extent_dirty);
827 int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
830 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask);
832 EXPORT_SYMBOL(set_extent_ordered);
834 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
835 int bits, gfp_t mask)
837 return set_extent_bit(tree, start, end, bits, 0, NULL,
840 EXPORT_SYMBOL(set_extent_bits);
842 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
843 int bits, gfp_t mask)
845 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
847 EXPORT_SYMBOL(clear_extent_bits);
849 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
852 return set_extent_bit(tree, start, end,
853 EXTENT_DELALLOC | EXTENT_DIRTY,
856 EXPORT_SYMBOL(set_extent_delalloc);
858 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
861 return clear_extent_bit(tree, start, end,
862 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
864 EXPORT_SYMBOL(clear_extent_dirty);
866 int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
869 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask);
871 EXPORT_SYMBOL(clear_extent_ordered);
873 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
876 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
879 EXPORT_SYMBOL(set_extent_new);
881 int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
884 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
886 EXPORT_SYMBOL(clear_extent_new);
888 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
891 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
894 EXPORT_SYMBOL(set_extent_uptodate);
896 int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
899 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
901 EXPORT_SYMBOL(clear_extent_uptodate);
903 int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
906 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
909 EXPORT_SYMBOL(set_extent_writeback);
911 int clear_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
914 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
916 EXPORT_SYMBOL(clear_extent_writeback);
918 int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
920 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
922 EXPORT_SYMBOL(wait_on_extent_writeback);
924 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
929 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
930 &failed_start, mask);
931 if (err == -EEXIST && (mask & __GFP_WAIT)) {
932 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
933 start = failed_start;
937 WARN_ON(start > end);
941 EXPORT_SYMBOL(lock_extent);
943 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
946 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
948 EXPORT_SYMBOL(unlock_extent);
951 * helper function to set pages and extents in the tree dirty
953 int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
955 unsigned long index = start >> PAGE_CACHE_SHIFT;
956 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
959 while (index <= end_index) {
960 page = find_get_page(tree->mapping, index);
962 __set_page_dirty_nobuffers(page);
963 page_cache_release(page);
966 set_extent_dirty(tree, start, end, GFP_NOFS);
969 EXPORT_SYMBOL(set_range_dirty);
972 * helper function to set both pages and extents in the tree writeback
974 int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
976 unsigned long index = start >> PAGE_CACHE_SHIFT;
977 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
980 while (index <= end_index) {
981 page = find_get_page(tree->mapping, index);
983 set_page_writeback(page);
984 page_cache_release(page);
987 set_extent_writeback(tree, start, end, GFP_NOFS);
990 EXPORT_SYMBOL(set_range_writeback);
992 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
993 u64 *start_ret, u64 *end_ret, int bits)
995 struct rb_node *node;
996 struct extent_state *state;
999 spin_lock_irq(&tree->lock);
1001 * this search will find all the extents that end after
1004 node = tree_search(tree, start);
1010 state = rb_entry(node, struct extent_state, rb_node);
1011 if (state->end >= start && (state->state & bits)) {
1012 *start_ret = state->start;
1013 *end_ret = state->end;
1017 node = rb_next(node);
1022 spin_unlock_irq(&tree->lock);
1025 EXPORT_SYMBOL(find_first_extent_bit);
1027 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1028 u64 start, int bits)
1030 struct rb_node *node;
1031 struct extent_state *state;
1034 * this search will find all the extents that end after
1037 node = tree_search(tree, start);
1043 state = rb_entry(node, struct extent_state, rb_node);
1044 if (state->end >= start && (state->state & bits)) {
1047 node = rb_next(node);
1054 EXPORT_SYMBOL(find_first_extent_bit_state);
1056 u64 find_lock_delalloc_range(struct extent_io_tree *tree,
1057 u64 *start, u64 *end, u64 max_bytes)
1059 struct rb_node *node;
1060 struct extent_state *state;
1061 u64 cur_start = *start;
1063 u64 total_bytes = 0;
1065 spin_lock_irq(&tree->lock);
1067 * this search will find all the extents that end after
1071 node = tree_search(tree, cur_start);
1079 state = rb_entry(node, struct extent_state, rb_node);
1080 if (found && state->start != cur_start) {
1083 if (!(state->state & EXTENT_DELALLOC)) {
1089 struct extent_state *prev_state;
1090 struct rb_node *prev_node = node;
1092 prev_node = rb_prev(prev_node);
1095 prev_state = rb_entry(prev_node,
1096 struct extent_state,
1098 if (!(prev_state->state & EXTENT_DELALLOC))
1104 if (state->state & EXTENT_LOCKED) {
1106 atomic_inc(&state->refs);
1107 prepare_to_wait(&state->wq, &wait,
1108 TASK_UNINTERRUPTIBLE);
1109 spin_unlock_irq(&tree->lock);
1111 spin_lock_irq(&tree->lock);
1112 finish_wait(&state->wq, &wait);
1113 free_extent_state(state);
1116 set_state_cb(tree, state, EXTENT_LOCKED);
1117 state->state |= EXTENT_LOCKED;
1119 *start = state->start;
1122 cur_start = state->end + 1;
1123 node = rb_next(node);
1126 total_bytes += state->end - state->start + 1;
1127 if (total_bytes >= max_bytes)
1131 spin_unlock_irq(&tree->lock);
1135 u64 count_range_bits(struct extent_io_tree *tree,
1136 u64 *start, u64 search_end, u64 max_bytes,
1139 struct rb_node *node;
1140 struct extent_state *state;
1141 u64 cur_start = *start;
1142 u64 total_bytes = 0;
1145 if (search_end <= cur_start) {
1146 printk("search_end %Lu start %Lu\n", search_end, cur_start);
1151 spin_lock_irq(&tree->lock);
1152 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1153 total_bytes = tree->dirty_bytes;
1157 * this search will find all the extents that end after
1160 node = tree_search(tree, cur_start);
1166 state = rb_entry(node, struct extent_state, rb_node);
1167 if (state->start > search_end)
1169 if (state->end >= cur_start && (state->state & bits)) {
1170 total_bytes += min(search_end, state->end) + 1 -
1171 max(cur_start, state->start);
1172 if (total_bytes >= max_bytes)
1175 *start = state->start;
1179 node = rb_next(node);
1184 spin_unlock_irq(&tree->lock);
1188 * helper function to lock both pages and extents in the tree.
1189 * pages must be locked first.
1191 int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
1193 unsigned long index = start >> PAGE_CACHE_SHIFT;
1194 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1198 while (index <= end_index) {
1199 page = grab_cache_page(tree->mapping, index);
1205 err = PTR_ERR(page);
1210 lock_extent(tree, start, end, GFP_NOFS);
1215 * we failed above in getting the page at 'index', so we undo here
1216 * up to but not including the page at 'index'
1219 index = start >> PAGE_CACHE_SHIFT;
1220 while (index < end_index) {
1221 page = find_get_page(tree->mapping, index);
1223 page_cache_release(page);
1228 EXPORT_SYMBOL(lock_range);
1231 * helper function to unlock both pages and extents in the tree.
1233 int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
1235 unsigned long index = start >> PAGE_CACHE_SHIFT;
1236 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1239 while (index <= end_index) {
1240 page = find_get_page(tree->mapping, index);
1242 page_cache_release(page);
1245 unlock_extent(tree, start, end, GFP_NOFS);
1248 EXPORT_SYMBOL(unlock_range);
1250 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1252 struct rb_node *node;
1253 struct extent_state *state;
1256 spin_lock_irq(&tree->lock);
1258 * this search will find all the extents that end after
1261 node = tree_search(tree, start);
1266 state = rb_entry(node, struct extent_state, rb_node);
1267 if (state->start != start) {
1271 state->private = private;
1273 spin_unlock_irq(&tree->lock);
1277 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1279 struct rb_node *node;
1280 struct extent_state *state;
1283 spin_lock_irq(&tree->lock);
1285 * this search will find all the extents that end after
1288 node = tree_search(tree, start);
1293 state = rb_entry(node, struct extent_state, rb_node);
1294 if (state->start != start) {
1298 *private = state->private;
1300 spin_unlock_irq(&tree->lock);
1305 * searches a range in the state tree for a given mask.
1306 * If 'filled' == 1, this returns 1 only if every extent in the tree
1307 * has the bits set. Otherwise, 1 is returned if any bit in the
1308 * range is found set.
1310 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1311 int bits, int filled)
1313 struct extent_state *state = NULL;
1314 struct rb_node *node;
1316 unsigned long flags;
1318 spin_lock_irqsave(&tree->lock, flags);
1319 node = tree_search(tree, start);
1320 while (node && start <= end) {
1321 state = rb_entry(node, struct extent_state, rb_node);
1323 if (filled && state->start > start) {
1328 if (state->start > end)
1331 if (state->state & bits) {
1335 } else if (filled) {
1339 start = state->end + 1;
1342 node = rb_next(node);
1349 spin_unlock_irqrestore(&tree->lock, flags);
1352 EXPORT_SYMBOL(test_range_bit);
1355 * helper function to set a given page up to date if all the
1356 * extents in the tree for that page are up to date
1358 static int check_page_uptodate(struct extent_io_tree *tree,
1361 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1362 u64 end = start + PAGE_CACHE_SIZE - 1;
1363 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1364 SetPageUptodate(page);
1369 * helper function to unlock a page if all the extents in the tree
1370 * for that page are unlocked
1372 static int check_page_locked(struct extent_io_tree *tree,
1375 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1376 u64 end = start + PAGE_CACHE_SIZE - 1;
1377 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1383 * helper function to end page writeback if all the extents
1384 * in the tree for that page are done with writeback
1386 static int check_page_writeback(struct extent_io_tree *tree,
1389 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1390 u64 end = start + PAGE_CACHE_SIZE - 1;
1391 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1392 end_page_writeback(page);
1396 /* lots and lots of room for performance fixes in the end_bio funcs */
1399 * after a writepage IO is done, we need to:
1400 * clear the uptodate bits on error
1401 * clear the writeback bits in the extent tree for this IO
1402 * end_page_writeback if the page has no more pending IO
1404 * Scheduling is not allowed, so the extent state tree is expected
1405 * to have one and only one object corresponding to this IO.
1407 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1408 static void end_bio_extent_writepage(struct bio *bio, int err)
1410 static int end_bio_extent_writepage(struct bio *bio,
1411 unsigned int bytes_done, int err)
1414 int uptodate = err == 0;
1415 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1416 struct extent_state *state = bio->bi_private;
1417 struct extent_io_tree *tree = state->tree;
1418 struct rb_node *node;
1424 unsigned long flags;
1426 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1431 struct page *page = bvec->bv_page;
1432 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1434 end = start + bvec->bv_len - 1;
1436 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1441 if (--bvec >= bio->bi_io_vec)
1442 prefetchw(&bvec->bv_page->flags);
1443 if (tree->ops && tree->ops->writepage_end_io_hook) {
1444 ret = tree->ops->writepage_end_io_hook(page, start,
1445 end, state, uptodate);
1450 if (!uptodate && tree->ops &&
1451 tree->ops->writepage_io_failed_hook) {
1452 ret = tree->ops->writepage_io_failed_hook(bio, page,
1456 uptodate = (err == 0);
1462 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1463 ClearPageUptodate(page);
1468 * bios can get merged in funny ways, and so we need to
1469 * be careful with the state variable. We know the
1470 * state won't be merged with others because it has
1471 * WRITEBACK set, but we can't be sure each biovec is
1472 * sequential in the file. So, if our cached state
1473 * doesn't match the expected end, search the tree
1474 * for the correct one.
1477 spin_lock_irqsave(&tree->lock, flags);
1478 if (!state || state->end != end) {
1480 node = __etree_search(tree, start, NULL, NULL);
1482 state = rb_entry(node, struct extent_state,
1484 if (state->end != end ||
1485 !(state->state & EXTENT_WRITEBACK))
1489 spin_unlock_irqrestore(&tree->lock, flags);
1490 clear_extent_writeback(tree, start,
1497 struct extent_state *clear = state;
1499 node = rb_prev(&state->rb_node);
1501 state = rb_entry(node,
1502 struct extent_state,
1508 clear_state_bit(tree, clear, EXTENT_WRITEBACK,
1519 /* before releasing the lock, make sure the next state
1520 * variable has the expected bits set and corresponds
1521 * to the correct offsets in the file
1523 if (state && (state->end + 1 != start ||
1524 !(state->state & EXTENT_WRITEBACK))) {
1527 spin_unlock_irqrestore(&tree->lock, flags);
1531 end_page_writeback(page);
1533 check_page_writeback(tree, page);
1534 } while (bvec >= bio->bi_io_vec);
1536 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1542 * after a readpage IO is done, we need to:
1543 * clear the uptodate bits on error
1544 * set the uptodate bits if things worked
1545 * set the page up to date if all extents in the tree are uptodate
1546 * clear the lock bit in the extent tree
1547 * unlock the page if there are no other extents locked for it
1549 * Scheduling is not allowed, so the extent state tree is expected
1550 * to have one and only one object corresponding to this IO.
1552 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1553 static void end_bio_extent_readpage(struct bio *bio, int err)
1555 static int end_bio_extent_readpage(struct bio *bio,
1556 unsigned int bytes_done, int err)
1559 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1560 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1561 struct extent_state *state = bio->bi_private;
1562 struct extent_io_tree *tree = state->tree;
1563 struct rb_node *node;
1567 unsigned long flags;
1571 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1577 struct page *page = bvec->bv_page;
1578 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1580 end = start + bvec->bv_len - 1;
1582 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1587 if (--bvec >= bio->bi_io_vec)
1588 prefetchw(&bvec->bv_page->flags);
1590 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1591 ret = tree->ops->readpage_end_io_hook(page, start, end,
1596 if (!uptodate && tree->ops &&
1597 tree->ops->readpage_io_failed_hook) {
1598 ret = tree->ops->readpage_io_failed_hook(bio, page,
1603 test_bit(BIO_UPTODATE, &bio->bi_flags);
1608 spin_lock_irqsave(&tree->lock, flags);
1609 if (!state || state->end != end) {
1611 node = __etree_search(tree, start, NULL, NULL);
1613 state = rb_entry(node, struct extent_state,
1615 if (state->end != end ||
1616 !(state->state & EXTENT_LOCKED))
1620 spin_unlock_irqrestore(&tree->lock, flags);
1622 set_extent_uptodate(tree, start, end,
1624 unlock_extent(tree, start, end, GFP_ATOMIC);
1631 struct extent_state *clear = state;
1633 node = rb_prev(&state->rb_node);
1635 state = rb_entry(node,
1636 struct extent_state,
1642 set_state_cb(tree, clear, EXTENT_UPTODATE);
1643 clear->state |= EXTENT_UPTODATE;
1645 clear_state_bit(tree, clear, EXTENT_LOCKED,
1656 /* before releasing the lock, make sure the next state
1657 * variable has the expected bits set and corresponds
1658 * to the correct offsets in the file
1660 if (state && (state->end + 1 != start ||
1661 !(state->state & EXTENT_LOCKED))) {
1664 spin_unlock_irqrestore(&tree->lock, flags);
1668 SetPageUptodate(page);
1670 ClearPageUptodate(page);
1676 check_page_uptodate(tree, page);
1678 ClearPageUptodate(page);
1681 check_page_locked(tree, page);
1683 } while (bvec >= bio->bi_io_vec);
1686 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1692 * IO done from prepare_write is pretty simple, we just unlock
1693 * the structs in the extent tree when done, and set the uptodate bits
1696 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1697 static void end_bio_extent_preparewrite(struct bio *bio, int err)
1699 static int end_bio_extent_preparewrite(struct bio *bio,
1700 unsigned int bytes_done, int err)
1703 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1704 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1705 struct extent_state *state = bio->bi_private;
1706 struct extent_io_tree *tree = state->tree;
1710 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1716 struct page *page = bvec->bv_page;
1717 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1719 end = start + bvec->bv_len - 1;
1721 if (--bvec >= bio->bi_io_vec)
1722 prefetchw(&bvec->bv_page->flags);
1725 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1727 ClearPageUptodate(page);
1731 unlock_extent(tree, start, end, GFP_ATOMIC);
1733 } while (bvec >= bio->bi_io_vec);
1736 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1742 extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1747 bio = bio_alloc(gfp_flags, nr_vecs);
1749 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1750 while (!bio && (nr_vecs /= 2))
1751 bio = bio_alloc(gfp_flags, nr_vecs);
1756 bio->bi_bdev = bdev;
1757 bio->bi_sector = first_sector;
1762 static int submit_one_bio(int rw, struct bio *bio, int mirror_num)
1765 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1766 struct page *page = bvec->bv_page;
1767 struct extent_io_tree *tree = bio->bi_private;
1768 struct rb_node *node;
1769 struct extent_state *state;
1773 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1774 end = start + bvec->bv_len - 1;
1776 spin_lock_irq(&tree->lock);
1777 node = __etree_search(tree, start, NULL, NULL);
1779 state = rb_entry(node, struct extent_state, rb_node);
1780 while(state->end < end) {
1781 node = rb_next(node);
1782 state = rb_entry(node, struct extent_state, rb_node);
1784 BUG_ON(state->end != end);
1785 spin_unlock_irq(&tree->lock);
1787 bio->bi_private = state;
1791 if (tree->ops && tree->ops->submit_bio_hook)
1792 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1795 submit_bio(rw, bio);
1796 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1802 static int submit_extent_page(int rw, struct extent_io_tree *tree,
1803 struct page *page, sector_t sector,
1804 size_t size, unsigned long offset,
1805 struct block_device *bdev,
1806 struct bio **bio_ret,
1807 unsigned long max_pages,
1808 bio_end_io_t end_io_func,
1815 if (bio_ret && *bio_ret) {
1817 if (bio->bi_sector + (bio->bi_size >> 9) != sector ||
1818 (tree->ops && tree->ops->merge_bio_hook &&
1819 tree->ops->merge_bio_hook(page, offset, size, bio)) ||
1820 bio_add_page(bio, page, size, offset) < size) {
1821 ret = submit_one_bio(rw, bio, mirror_num);
1827 nr = bio_get_nr_vecs(bdev);
1828 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1830 printk("failed to allocate bio nr %d\n", nr);
1834 bio_add_page(bio, page, size, offset);
1835 bio->bi_end_io = end_io_func;
1836 bio->bi_private = tree;
1841 ret = submit_one_bio(rw, bio, mirror_num);
1847 void set_page_extent_mapped(struct page *page)
1849 if (!PagePrivate(page)) {
1850 SetPagePrivate(page);
1851 page_cache_get(page);
1852 set_page_private(page, EXTENT_PAGE_PRIVATE);
1856 void set_page_extent_head(struct page *page, unsigned long len)
1858 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1862 * basic readpage implementation. Locked extent state structs are inserted
1863 * into the tree that are removed when the IO is done (by the end_io
1866 static int __extent_read_full_page(struct extent_io_tree *tree,
1868 get_extent_t *get_extent,
1869 struct bio **bio, int mirror_num)
1871 struct inode *inode = page->mapping->host;
1872 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1873 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1877 u64 last_byte = i_size_read(inode);
1881 struct extent_map *em;
1882 struct block_device *bdev;
1885 size_t page_offset = 0;
1887 size_t blocksize = inode->i_sb->s_blocksize;
1889 set_page_extent_mapped(page);
1892 lock_extent(tree, start, end, GFP_NOFS);
1894 while (cur <= end) {
1895 if (cur >= last_byte) {
1897 iosize = PAGE_CACHE_SIZE - page_offset;
1898 userpage = kmap_atomic(page, KM_USER0);
1899 memset(userpage + page_offset, 0, iosize);
1900 flush_dcache_page(page);
1901 kunmap_atomic(userpage, KM_USER0);
1902 set_extent_uptodate(tree, cur, cur + iosize - 1,
1904 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1907 em = get_extent(inode, page, page_offset, cur,
1909 if (IS_ERR(em) || !em) {
1911 unlock_extent(tree, cur, end, GFP_NOFS);
1914 extent_offset = cur - em->start;
1915 if (extent_map_end(em) <= cur) {
1916 printk("bad mapping em [%Lu %Lu] cur %Lu\n", em->start, extent_map_end(em), cur);
1918 BUG_ON(extent_map_end(em) <= cur);
1920 printk("2bad mapping end %Lu cur %Lu\n", end, cur);
1924 iosize = min(extent_map_end(em) - cur, end - cur + 1);
1925 cur_end = min(extent_map_end(em) - 1, end);
1926 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1927 sector = (em->block_start + extent_offset) >> 9;
1929 block_start = em->block_start;
1930 free_extent_map(em);
1933 /* we've found a hole, just zero and go on */
1934 if (block_start == EXTENT_MAP_HOLE) {
1936 userpage = kmap_atomic(page, KM_USER0);
1937 memset(userpage + page_offset, 0, iosize);
1938 flush_dcache_page(page);
1939 kunmap_atomic(userpage, KM_USER0);
1941 set_extent_uptodate(tree, cur, cur + iosize - 1,
1943 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1945 page_offset += iosize;
1948 /* the get_extent function already copied into the page */
1949 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1950 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1952 page_offset += iosize;
1955 /* we have an inline extent but it didn't get marked up
1956 * to date. Error out
1958 if (block_start == EXTENT_MAP_INLINE) {
1960 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1962 page_offset += iosize;
1967 if (tree->ops && tree->ops->readpage_io_hook) {
1968 ret = tree->ops->readpage_io_hook(page, cur,
1972 unsigned long nr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
1974 ret = submit_extent_page(READ, tree, page,
1975 sector, iosize, page_offset,
1977 end_bio_extent_readpage, mirror_num);
1982 page_offset += iosize;
1986 if (!PageError(page))
1987 SetPageUptodate(page);
1993 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
1994 get_extent_t *get_extent)
1996 struct bio *bio = NULL;
1999 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0);
2001 submit_one_bio(READ, bio, 0);
2004 EXPORT_SYMBOL(extent_read_full_page);
2007 * the writepage semantics are similar to regular writepage. extent
2008 * records are inserted to lock ranges in the tree, and as dirty areas
2009 * are found, they are marked writeback. Then the lock bits are removed
2010 * and the end_io handler clears the writeback ranges
2012 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2015 struct inode *inode = page->mapping->host;
2016 struct extent_page_data *epd = data;
2017 struct extent_io_tree *tree = epd->tree;
2018 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2020 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2024 u64 last_byte = i_size_read(inode);
2029 struct extent_map *em;
2030 struct block_device *bdev;
2033 size_t pg_offset = 0;
2035 loff_t i_size = i_size_read(inode);
2036 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2040 WARN_ON(!PageLocked(page));
2041 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2042 if (page->index > end_index ||
2043 (page->index == end_index && !pg_offset)) {
2044 page->mapping->a_ops->invalidatepage(page, 0);
2049 if (page->index == end_index) {
2052 userpage = kmap_atomic(page, KM_USER0);
2053 memset(userpage + pg_offset, 0,
2054 PAGE_CACHE_SIZE - pg_offset);
2055 kunmap_atomic(userpage, KM_USER0);
2056 flush_dcache_page(page);
2060 set_page_extent_mapped(page);
2062 delalloc_start = start;
2064 while(delalloc_end < page_end) {
2065 nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start,
2068 if (nr_delalloc == 0) {
2069 delalloc_start = delalloc_end + 1;
2072 tree->ops->fill_delalloc(inode, delalloc_start,
2074 clear_extent_bit(tree, delalloc_start,
2076 EXTENT_LOCKED | EXTENT_DELALLOC,
2078 delalloc_start = delalloc_end + 1;
2080 lock_extent(tree, start, page_end, GFP_NOFS);
2081 unlock_start = start;
2083 if (tree->ops && tree->ops->writepage_start_hook) {
2084 ret = tree->ops->writepage_start_hook(page, start, page_end);
2085 if (ret == -EAGAIN) {
2086 unlock_extent(tree, start, page_end, GFP_NOFS);
2087 redirty_page_for_writepage(wbc, page);
2094 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
2095 printk("found delalloc bits after lock_extent\n");
2098 if (last_byte <= start) {
2099 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
2100 unlock_extent(tree, start, page_end, GFP_NOFS);
2101 if (tree->ops && tree->ops->writepage_end_io_hook)
2102 tree->ops->writepage_end_io_hook(page, start,
2104 unlock_start = page_end + 1;
2108 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2109 blocksize = inode->i_sb->s_blocksize;
2111 while (cur <= end) {
2112 if (cur >= last_byte) {
2113 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
2114 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2115 if (tree->ops && tree->ops->writepage_end_io_hook)
2116 tree->ops->writepage_end_io_hook(page, cur,
2118 unlock_start = page_end + 1;
2121 em = epd->get_extent(inode, page, pg_offset, cur,
2123 if (IS_ERR(em) || !em) {
2128 extent_offset = cur - em->start;
2129 BUG_ON(extent_map_end(em) <= cur);
2131 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2132 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2133 sector = (em->block_start + extent_offset) >> 9;
2135 block_start = em->block_start;
2136 free_extent_map(em);
2139 if (block_start == EXTENT_MAP_HOLE ||
2140 block_start == EXTENT_MAP_INLINE) {
2141 clear_extent_dirty(tree, cur,
2142 cur + iosize - 1, GFP_NOFS);
2144 unlock_extent(tree, unlock_start, cur + iosize -1,
2147 if (tree->ops && tree->ops->writepage_end_io_hook)
2148 tree->ops->writepage_end_io_hook(page, cur,
2152 pg_offset += iosize;
2157 /* leave this out until we have a page_mkwrite call */
2158 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2161 pg_offset += iosize;
2164 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2165 if (tree->ops && tree->ops->writepage_io_hook) {
2166 ret = tree->ops->writepage_io_hook(page, cur,
2174 unsigned long max_nr = end_index + 1;
2176 set_range_writeback(tree, cur, cur + iosize - 1);
2177 if (!PageWriteback(page)) {
2178 printk("warning page %lu not writeback, "
2179 "cur %llu end %llu\n", page->index,
2180 (unsigned long long)cur,
2181 (unsigned long long)end);
2184 ret = submit_extent_page(WRITE, tree, page, sector,
2185 iosize, pg_offset, bdev,
2187 end_bio_extent_writepage, 0);
2192 pg_offset += iosize;
2197 /* make sure the mapping tag for page dirty gets cleared */
2198 set_page_writeback(page);
2199 end_page_writeback(page);
2201 if (unlock_start <= page_end)
2202 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2207 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
2208 /* Taken directly from 2.6.23 for 2.6.18 back port */
2209 typedef int (*writepage_t)(struct page *page, struct writeback_control *wbc,
2213 * write_cache_pages - walk the list of dirty pages of the given address space
2214 * and write all of them.
2215 * @mapping: address space structure to write
2216 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2217 * @writepage: function called for each page
2218 * @data: data passed to writepage function
2220 * If a page is already under I/O, write_cache_pages() skips it, even
2221 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2222 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2223 * and msync() need to guarantee that all the data which was dirty at the time
2224 * the call was made get new I/O started against them. If wbc->sync_mode is
2225 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2226 * existing IO to complete.
2228 static int write_cache_pages(struct address_space *mapping,
2229 struct writeback_control *wbc, writepage_t writepage,
2232 struct backing_dev_info *bdi = mapping->backing_dev_info;
2235 struct pagevec pvec;
2238 pgoff_t end; /* Inclusive */
2240 int range_whole = 0;
2242 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2243 wbc->encountered_congestion = 1;
2247 pagevec_init(&pvec, 0);
2248 if (wbc->range_cyclic) {
2249 index = mapping->writeback_index; /* Start from prev offset */
2252 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2253 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2254 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2259 while (!done && (index <= end) &&
2260 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2261 PAGECACHE_TAG_DIRTY,
2262 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2266 for (i = 0; i < nr_pages; i++) {
2267 struct page *page = pvec.pages[i];
2270 * At this point we hold neither mapping->tree_lock nor
2271 * lock on the page itself: the page may be truncated or
2272 * invalidated (changing page->mapping to NULL), or even
2273 * swizzled back from swapper_space to tmpfs file
2278 if (unlikely(page->mapping != mapping)) {
2283 if (!wbc->range_cyclic && page->index > end) {
2289 if (wbc->sync_mode != WB_SYNC_NONE)
2290 wait_on_page_writeback(page);
2292 if (PageWriteback(page) ||
2293 !clear_page_dirty_for_io(page)) {
2298 ret = (*writepage)(page, wbc, data);
2300 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2304 if (ret || (--(wbc->nr_to_write) <= 0))
2306 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2307 wbc->encountered_congestion = 1;
2311 pagevec_release(&pvec);
2314 if (!scanned && !done) {
2316 * We hit the last page and there is more work to be done: wrap
2317 * back to the start of the file
2323 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2324 mapping->writeback_index = index;
2329 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2330 get_extent_t *get_extent,
2331 struct writeback_control *wbc)
2334 struct address_space *mapping = page->mapping;
2335 struct extent_page_data epd = {
2338 .get_extent = get_extent,
2340 struct writeback_control wbc_writepages = {
2342 .sync_mode = WB_SYNC_NONE,
2343 .older_than_this = NULL,
2345 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2346 .range_end = (loff_t)-1,
2350 ret = __extent_writepage(page, wbc, &epd);
2352 write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd);
2354 submit_one_bio(WRITE, epd.bio, 0);
2358 EXPORT_SYMBOL(extent_write_full_page);
2361 int extent_writepages(struct extent_io_tree *tree,
2362 struct address_space *mapping,
2363 get_extent_t *get_extent,
2364 struct writeback_control *wbc)
2367 struct extent_page_data epd = {
2370 .get_extent = get_extent,
2373 ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd);
2375 submit_one_bio(WRITE, epd.bio, 0);
2379 EXPORT_SYMBOL(extent_writepages);
2381 int extent_readpages(struct extent_io_tree *tree,
2382 struct address_space *mapping,
2383 struct list_head *pages, unsigned nr_pages,
2384 get_extent_t get_extent)
2386 struct bio *bio = NULL;
2388 struct pagevec pvec;
2390 pagevec_init(&pvec, 0);
2391 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2392 struct page *page = list_entry(pages->prev, struct page, lru);
2394 prefetchw(&page->flags);
2395 list_del(&page->lru);
2397 * what we want to do here is call add_to_page_cache_lru,
2398 * but that isn't exported, so we reproduce it here
2400 if (!add_to_page_cache(page, mapping,
2401 page->index, GFP_KERNEL)) {
2403 /* open coding of lru_cache_add, also not exported */
2404 page_cache_get(page);
2405 if (!pagevec_add(&pvec, page))
2406 __pagevec_lru_add(&pvec);
2407 __extent_read_full_page(tree, page, get_extent,
2410 page_cache_release(page);
2412 if (pagevec_count(&pvec))
2413 __pagevec_lru_add(&pvec);
2414 BUG_ON(!list_empty(pages));
2416 submit_one_bio(READ, bio, 0);
2419 EXPORT_SYMBOL(extent_readpages);
2422 * basic invalidatepage code, this waits on any locked or writeback
2423 * ranges corresponding to the page, and then deletes any extent state
2424 * records from the tree
2426 int extent_invalidatepage(struct extent_io_tree *tree,
2427 struct page *page, unsigned long offset)
2429 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2430 u64 end = start + PAGE_CACHE_SIZE - 1;
2431 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2433 start += (offset + blocksize -1) & ~(blocksize - 1);
2437 lock_extent(tree, start, end, GFP_NOFS);
2438 wait_on_extent_writeback(tree, start, end);
2439 clear_extent_bit(tree, start, end,
2440 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2444 EXPORT_SYMBOL(extent_invalidatepage);
2447 * simple commit_write call, set_range_dirty is used to mark both
2448 * the pages and the extent records as dirty
2450 int extent_commit_write(struct extent_io_tree *tree,
2451 struct inode *inode, struct page *page,
2452 unsigned from, unsigned to)
2454 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2456 set_page_extent_mapped(page);
2457 set_page_dirty(page);
2459 if (pos > inode->i_size) {
2460 i_size_write(inode, pos);
2461 mark_inode_dirty(inode);
2465 EXPORT_SYMBOL(extent_commit_write);
2467 int extent_prepare_write(struct extent_io_tree *tree,
2468 struct inode *inode, struct page *page,
2469 unsigned from, unsigned to, get_extent_t *get_extent)
2471 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2472 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2474 u64 orig_block_start;
2477 struct extent_map *em;
2478 unsigned blocksize = 1 << inode->i_blkbits;
2479 size_t page_offset = 0;
2480 size_t block_off_start;
2481 size_t block_off_end;
2487 set_page_extent_mapped(page);
2489 block_start = (page_start + from) & ~((u64)blocksize - 1);
2490 block_end = (page_start + to - 1) | (blocksize - 1);
2491 orig_block_start = block_start;
2493 lock_extent(tree, page_start, page_end, GFP_NOFS);
2494 while(block_start <= block_end) {
2495 em = get_extent(inode, page, page_offset, block_start,
2496 block_end - block_start + 1, 1);
2497 if (IS_ERR(em) || !em) {
2500 cur_end = min(block_end, extent_map_end(em) - 1);
2501 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2502 block_off_end = block_off_start + blocksize;
2503 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2505 if (!PageUptodate(page) && isnew &&
2506 (block_off_end > to || block_off_start < from)) {
2509 kaddr = kmap_atomic(page, KM_USER0);
2510 if (block_off_end > to)
2511 memset(kaddr + to, 0, block_off_end - to);
2512 if (block_off_start < from)
2513 memset(kaddr + block_off_start, 0,
2514 from - block_off_start);
2515 flush_dcache_page(page);
2516 kunmap_atomic(kaddr, KM_USER0);
2518 if ((em->block_start != EXTENT_MAP_HOLE &&
2519 em->block_start != EXTENT_MAP_INLINE) &&
2520 !isnew && !PageUptodate(page) &&
2521 (block_off_end > to || block_off_start < from) &&
2522 !test_range_bit(tree, block_start, cur_end,
2523 EXTENT_UPTODATE, 1)) {
2525 u64 extent_offset = block_start - em->start;
2527 sector = (em->block_start + extent_offset) >> 9;
2528 iosize = (cur_end - block_start + blocksize) &
2529 ~((u64)blocksize - 1);
2531 * we've already got the extent locked, but we
2532 * need to split the state such that our end_bio
2533 * handler can clear the lock.
2535 set_extent_bit(tree, block_start,
2536 block_start + iosize - 1,
2537 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2538 ret = submit_extent_page(READ, tree, page,
2539 sector, iosize, page_offset, em->bdev,
2541 end_bio_extent_preparewrite, 0);
2543 block_start = block_start + iosize;
2545 set_extent_uptodate(tree, block_start, cur_end,
2547 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2548 block_start = cur_end + 1;
2550 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2551 free_extent_map(em);
2554 wait_extent_bit(tree, orig_block_start,
2555 block_end, EXTENT_LOCKED);
2557 check_page_uptodate(tree, page);
2559 /* FIXME, zero out newly allocated blocks on error */
2562 EXPORT_SYMBOL(extent_prepare_write);
2565 * a helper for releasepage, this tests for areas of the page that
2566 * are locked or under IO and drops the related state bits if it is safe
2569 int try_release_extent_state(struct extent_map_tree *map,
2570 struct extent_io_tree *tree, struct page *page,
2573 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2574 u64 end = start + PAGE_CACHE_SIZE - 1;
2577 if (test_range_bit(tree, start, end,
2578 EXTENT_IOBITS | EXTENT_ORDERED, 0))
2581 if ((mask & GFP_NOFS) == GFP_NOFS)
2583 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2588 EXPORT_SYMBOL(try_release_extent_state);
2591 * a helper for releasepage. As long as there are no locked extents
2592 * in the range corresponding to the page, both state records and extent
2593 * map records are removed
2595 int try_release_extent_mapping(struct extent_map_tree *map,
2596 struct extent_io_tree *tree, struct page *page,
2599 struct extent_map *em;
2600 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2601 u64 end = start + PAGE_CACHE_SIZE - 1;
2603 if ((mask & __GFP_WAIT) &&
2604 page->mapping->host->i_size > 16 * 1024 * 1024) {
2606 while (start <= end) {
2607 len = end - start + 1;
2608 spin_lock(&map->lock);
2609 em = lookup_extent_mapping(map, start, len);
2610 if (!em || IS_ERR(em)) {
2611 spin_unlock(&map->lock);
2614 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2615 em->start != start) {
2616 spin_unlock(&map->lock);
2617 free_extent_map(em);
2620 if (!test_range_bit(tree, em->start,
2621 extent_map_end(em) - 1,
2622 EXTENT_LOCKED, 0)) {
2623 remove_extent_mapping(map, em);
2624 /* once for the rb tree */
2625 free_extent_map(em);
2627 start = extent_map_end(em);
2628 spin_unlock(&map->lock);
2631 free_extent_map(em);
2634 return try_release_extent_state(map, tree, page, mask);
2636 EXPORT_SYMBOL(try_release_extent_mapping);
2638 sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2639 get_extent_t *get_extent)
2641 struct inode *inode = mapping->host;
2642 u64 start = iblock << inode->i_blkbits;
2643 sector_t sector = 0;
2644 struct extent_map *em;
2646 em = get_extent(inode, NULL, 0, start, (1 << inode->i_blkbits), 0);
2647 if (!em || IS_ERR(em))
2650 if (em->block_start == EXTENT_MAP_INLINE ||
2651 em->block_start == EXTENT_MAP_HOLE)
2654 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2656 free_extent_map(em);
2660 static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2664 struct address_space *mapping;
2667 return eb->first_page;
2668 i += eb->start >> PAGE_CACHE_SHIFT;
2669 mapping = eb->first_page->mapping;
2670 read_lock_irq(&mapping->tree_lock);
2671 p = radix_tree_lookup(&mapping->page_tree, i);
2672 read_unlock_irq(&mapping->tree_lock);
2676 static inline unsigned long num_extent_pages(u64 start, u64 len)
2678 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2679 (start >> PAGE_CACHE_SHIFT);
2682 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2687 struct extent_buffer *eb = NULL;
2688 unsigned long flags;
2690 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
2693 spin_lock_irqsave(&leak_lock, flags);
2694 list_add(&eb->leak_list, &buffers);
2695 spin_unlock_irqrestore(&leak_lock, flags);
2696 atomic_set(&eb->refs, 1);
2701 static void __free_extent_buffer(struct extent_buffer *eb)
2703 unsigned long flags;
2704 spin_lock_irqsave(&leak_lock, flags);
2705 list_del(&eb->leak_list);
2706 spin_unlock_irqrestore(&leak_lock, flags);
2707 kmem_cache_free(extent_buffer_cache, eb);
2710 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
2711 u64 start, unsigned long len,
2715 unsigned long num_pages = num_extent_pages(start, len);
2717 unsigned long index = start >> PAGE_CACHE_SHIFT;
2718 struct extent_buffer *eb;
2719 struct extent_buffer *exists = NULL;
2721 struct address_space *mapping = tree->mapping;
2724 spin_lock(&tree->buffer_lock);
2725 eb = buffer_search(tree, start);
2727 atomic_inc(&eb->refs);
2728 spin_unlock(&tree->buffer_lock);
2731 spin_unlock(&tree->buffer_lock);
2733 eb = __alloc_extent_buffer(tree, start, len, mask);
2738 eb->first_page = page0;
2741 page_cache_get(page0);
2742 mark_page_accessed(page0);
2743 set_page_extent_mapped(page0);
2744 set_page_extent_head(page0, len);
2745 uptodate = PageUptodate(page0);
2749 for (; i < num_pages; i++, index++) {
2750 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
2755 set_page_extent_mapped(p);
2756 mark_page_accessed(p);
2759 set_page_extent_head(p, len);
2761 set_page_private(p, EXTENT_PAGE_PRIVATE);
2763 if (!PageUptodate(p))
2768 eb->flags |= EXTENT_UPTODATE;
2769 eb->flags |= EXTENT_BUFFER_FILLED;
2771 spin_lock(&tree->buffer_lock);
2772 exists = buffer_tree_insert(tree, start, &eb->rb_node);
2774 /* add one reference for the caller */
2775 atomic_inc(&exists->refs);
2776 spin_unlock(&tree->buffer_lock);
2779 spin_unlock(&tree->buffer_lock);
2781 /* add one reference for the tree */
2782 atomic_inc(&eb->refs);
2786 if (!atomic_dec_and_test(&eb->refs))
2788 for (index = 1; index < i; index++)
2789 page_cache_release(extent_buffer_page(eb, index));
2790 page_cache_release(extent_buffer_page(eb, 0));
2791 __free_extent_buffer(eb);
2794 EXPORT_SYMBOL(alloc_extent_buffer);
2796 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
2797 u64 start, unsigned long len,
2800 struct extent_buffer *eb;
2802 spin_lock(&tree->buffer_lock);
2803 eb = buffer_search(tree, start);
2805 atomic_inc(&eb->refs);
2806 spin_unlock(&tree->buffer_lock);
2810 EXPORT_SYMBOL(find_extent_buffer);
2812 void free_extent_buffer(struct extent_buffer *eb)
2817 if (!atomic_dec_and_test(&eb->refs))
2822 EXPORT_SYMBOL(free_extent_buffer);
2824 int clear_extent_buffer_dirty(struct extent_io_tree *tree,
2825 struct extent_buffer *eb)
2829 unsigned long num_pages;
2832 u64 start = eb->start;
2833 u64 end = start + eb->len - 1;
2835 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
2836 num_pages = num_extent_pages(eb->start, eb->len);
2838 for (i = 0; i < num_pages; i++) {
2839 page = extent_buffer_page(eb, i);
2841 set_page_extent_head(page, eb->len);
2843 set_page_private(page, EXTENT_PAGE_PRIVATE);
2846 * if we're on the last page or the first page and the
2847 * block isn't aligned on a page boundary, do extra checks
2848 * to make sure we don't clean page that is partially dirty
2850 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2851 ((i == num_pages - 1) &&
2852 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
2853 start = (u64)page->index << PAGE_CACHE_SHIFT;
2854 end = start + PAGE_CACHE_SIZE - 1;
2855 if (test_range_bit(tree, start, end,
2860 clear_page_dirty_for_io(page);
2861 read_lock_irq(&page->mapping->tree_lock);
2862 if (!PageDirty(page)) {
2863 radix_tree_tag_clear(&page->mapping->page_tree,
2865 PAGECACHE_TAG_DIRTY);
2867 read_unlock_irq(&page->mapping->tree_lock);
2871 EXPORT_SYMBOL(clear_extent_buffer_dirty);
2873 int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
2874 struct extent_buffer *eb)
2876 return wait_on_extent_writeback(tree, eb->start,
2877 eb->start + eb->len - 1);
2879 EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
2881 int set_extent_buffer_dirty(struct extent_io_tree *tree,
2882 struct extent_buffer *eb)
2885 unsigned long num_pages;
2887 num_pages = num_extent_pages(eb->start, eb->len);
2888 for (i = 0; i < num_pages; i++) {
2889 struct page *page = extent_buffer_page(eb, i);
2890 /* writepage may need to do something special for the
2891 * first page, we have to make sure page->private is
2892 * properly set. releasepage may drop page->private
2893 * on us if the page isn't already dirty.
2896 set_page_extent_head(page, eb->len);
2897 } else if (PagePrivate(page) &&
2898 page->private != EXTENT_PAGE_PRIVATE) {
2899 set_page_extent_mapped(page);
2901 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
2903 return set_extent_dirty(tree, eb->start,
2904 eb->start + eb->len - 1, GFP_NOFS);
2906 EXPORT_SYMBOL(set_extent_buffer_dirty);
2908 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
2909 struct extent_buffer *eb)
2913 unsigned long num_pages;
2915 num_pages = num_extent_pages(eb->start, eb->len);
2916 eb->flags &= ~EXTENT_UPTODATE;
2918 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2920 for (i = 0; i < num_pages; i++) {
2921 page = extent_buffer_page(eb, i);
2922 ClearPageUptodate(page);
2927 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
2928 struct extent_buffer *eb)
2932 unsigned long num_pages;
2934 num_pages = num_extent_pages(eb->start, eb->len);
2936 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2938 for (i = 0; i < num_pages; i++) {
2939 page = extent_buffer_page(eb, i);
2940 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2941 ((i == num_pages - 1) &&
2942 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
2943 check_page_uptodate(tree, page);
2946 SetPageUptodate(page);
2950 EXPORT_SYMBOL(set_extent_buffer_uptodate);
2952 int extent_range_uptodate(struct extent_io_tree *tree,
2957 int pg_uptodate = 1;
2959 unsigned long index;
2961 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
2964 while(start <= end) {
2965 index = start >> PAGE_CACHE_SHIFT;
2966 page = find_get_page(tree->mapping, index);
2967 uptodate = PageUptodate(page);
2968 page_cache_release(page);
2973 start += PAGE_CACHE_SIZE;
2978 int extent_buffer_uptodate(struct extent_io_tree *tree,
2979 struct extent_buffer *eb)
2982 unsigned long num_pages;
2985 int pg_uptodate = 1;
2987 if (eb->flags & EXTENT_UPTODATE)
2990 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
2991 EXTENT_UPTODATE, 1);
2995 num_pages = num_extent_pages(eb->start, eb->len);
2996 for (i = 0; i < num_pages; i++) {
2997 page = extent_buffer_page(eb, i);
2998 if (!PageUptodate(page)) {
3005 EXPORT_SYMBOL(extent_buffer_uptodate);
3007 int read_extent_buffer_pages(struct extent_io_tree *tree,
3008 struct extent_buffer *eb,
3009 u64 start, int wait,
3010 get_extent_t *get_extent, int mirror_num)
3013 unsigned long start_i;
3017 int locked_pages = 0;
3018 int all_uptodate = 1;
3019 int inc_all_pages = 0;
3020 unsigned long num_pages;
3021 struct bio *bio = NULL;
3023 if (eb->flags & EXTENT_UPTODATE)
3026 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3027 EXTENT_UPTODATE, 1)) {
3032 WARN_ON(start < eb->start);
3033 start_i = (start >> PAGE_CACHE_SHIFT) -
3034 (eb->start >> PAGE_CACHE_SHIFT);
3039 num_pages = num_extent_pages(eb->start, eb->len);
3040 for (i = start_i; i < num_pages; i++) {
3041 page = extent_buffer_page(eb, i);
3043 if (TestSetPageLocked(page))
3049 if (!PageUptodate(page)) {
3055 eb->flags |= EXTENT_UPTODATE;
3059 for (i = start_i; i < num_pages; i++) {
3060 page = extent_buffer_page(eb, i);
3062 page_cache_get(page);
3063 if (!PageUptodate(page)) {
3066 ClearPageError(page);
3067 err = __extent_read_full_page(tree, page,
3079 submit_one_bio(READ, bio, mirror_num);
3084 for (i = start_i; i < num_pages; i++) {
3085 page = extent_buffer_page(eb, i);
3086 wait_on_page_locked(page);
3087 if (!PageUptodate(page)) {
3092 eb->flags |= EXTENT_UPTODATE;
3097 while(locked_pages > 0) {
3098 page = extent_buffer_page(eb, i);
3105 EXPORT_SYMBOL(read_extent_buffer_pages);
3107 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3108 unsigned long start,
3115 char *dst = (char *)dstv;
3116 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3117 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3119 WARN_ON(start > eb->len);
3120 WARN_ON(start + len > eb->start + eb->len);
3122 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3125 page = extent_buffer_page(eb, i);
3127 cur = min(len, (PAGE_CACHE_SIZE - offset));
3128 kaddr = kmap_atomic(page, KM_USER1);
3129 memcpy(dst, kaddr + offset, cur);
3130 kunmap_atomic(kaddr, KM_USER1);
3138 EXPORT_SYMBOL(read_extent_buffer);
3140 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3141 unsigned long min_len, char **token, char **map,
3142 unsigned long *map_start,
3143 unsigned long *map_len, int km)
3145 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3148 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3149 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3150 unsigned long end_i = (start_offset + start + min_len - 1) >>
3157 offset = start_offset;
3161 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3163 if (start + min_len > eb->len) {
3164 printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
3168 p = extent_buffer_page(eb, i);
3169 kaddr = kmap_atomic(p, km);
3171 *map = kaddr + offset;
3172 *map_len = PAGE_CACHE_SIZE - offset;
3175 EXPORT_SYMBOL(map_private_extent_buffer);
3177 int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3178 unsigned long min_len,
3179 char **token, char **map,
3180 unsigned long *map_start,
3181 unsigned long *map_len, int km)
3185 if (eb->map_token) {
3186 unmap_extent_buffer(eb, eb->map_token, km);
3187 eb->map_token = NULL;
3190 err = map_private_extent_buffer(eb, start, min_len, token, map,
3191 map_start, map_len, km);
3193 eb->map_token = *token;
3195 eb->map_start = *map_start;
3196 eb->map_len = *map_len;
3200 EXPORT_SYMBOL(map_extent_buffer);
3202 void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3204 kunmap_atomic(token, km);
3206 EXPORT_SYMBOL(unmap_extent_buffer);
3208 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3209 unsigned long start,
3216 char *ptr = (char *)ptrv;
3217 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3218 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3221 WARN_ON(start > eb->len);
3222 WARN_ON(start + len > eb->start + eb->len);
3224 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3227 page = extent_buffer_page(eb, i);
3229 cur = min(len, (PAGE_CACHE_SIZE - offset));
3231 kaddr = kmap_atomic(page, KM_USER0);
3232 ret = memcmp(ptr, kaddr + offset, cur);
3233 kunmap_atomic(kaddr, KM_USER0);
3244 EXPORT_SYMBOL(memcmp_extent_buffer);
3246 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3247 unsigned long start, unsigned long len)
3253 char *src = (char *)srcv;
3254 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3255 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3257 WARN_ON(start > eb->len);
3258 WARN_ON(start + len > eb->start + eb->len);
3260 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3263 page = extent_buffer_page(eb, i);
3264 WARN_ON(!PageUptodate(page));
3266 cur = min(len, PAGE_CACHE_SIZE - offset);
3267 kaddr = kmap_atomic(page, KM_USER1);
3268 memcpy(kaddr + offset, src, cur);
3269 kunmap_atomic(kaddr, KM_USER1);
3277 EXPORT_SYMBOL(write_extent_buffer);
3279 void memset_extent_buffer(struct extent_buffer *eb, char c,
3280 unsigned long start, unsigned long len)
3286 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3287 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3289 WARN_ON(start > eb->len);
3290 WARN_ON(start + len > eb->start + eb->len);
3292 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3295 page = extent_buffer_page(eb, i);
3296 WARN_ON(!PageUptodate(page));
3298 cur = min(len, PAGE_CACHE_SIZE - offset);
3299 kaddr = kmap_atomic(page, KM_USER0);
3300 memset(kaddr + offset, c, cur);
3301 kunmap_atomic(kaddr, KM_USER0);
3308 EXPORT_SYMBOL(memset_extent_buffer);
3310 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3311 unsigned long dst_offset, unsigned long src_offset,
3314 u64 dst_len = dst->len;
3319 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3320 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3322 WARN_ON(src->len != dst_len);
3324 offset = (start_offset + dst_offset) &
3325 ((unsigned long)PAGE_CACHE_SIZE - 1);
3328 page = extent_buffer_page(dst, i);
3329 WARN_ON(!PageUptodate(page));
3331 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3333 kaddr = kmap_atomic(page, KM_USER0);
3334 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3335 kunmap_atomic(kaddr, KM_USER0);
3343 EXPORT_SYMBOL(copy_extent_buffer);
3345 static void move_pages(struct page *dst_page, struct page *src_page,
3346 unsigned long dst_off, unsigned long src_off,
3349 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3350 if (dst_page == src_page) {
3351 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3353 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3354 char *p = dst_kaddr + dst_off + len;
3355 char *s = src_kaddr + src_off + len;
3360 kunmap_atomic(src_kaddr, KM_USER1);
3362 kunmap_atomic(dst_kaddr, KM_USER0);
3365 static void copy_pages(struct page *dst_page, struct page *src_page,
3366 unsigned long dst_off, unsigned long src_off,
3369 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3372 if (dst_page != src_page)
3373 src_kaddr = kmap_atomic(src_page, KM_USER1);
3375 src_kaddr = dst_kaddr;
3377 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3378 kunmap_atomic(dst_kaddr, KM_USER0);
3379 if (dst_page != src_page)
3380 kunmap_atomic(src_kaddr, KM_USER1);
3383 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3384 unsigned long src_offset, unsigned long len)
3387 size_t dst_off_in_page;
3388 size_t src_off_in_page;
3389 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3390 unsigned long dst_i;
3391 unsigned long src_i;
3393 if (src_offset + len > dst->len) {
3394 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3395 src_offset, len, dst->len);
3398 if (dst_offset + len > dst->len) {
3399 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3400 dst_offset, len, dst->len);
3405 dst_off_in_page = (start_offset + dst_offset) &
3406 ((unsigned long)PAGE_CACHE_SIZE - 1);
3407 src_off_in_page = (start_offset + src_offset) &
3408 ((unsigned long)PAGE_CACHE_SIZE - 1);
3410 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3411 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3413 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3415 cur = min_t(unsigned long, cur,
3416 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3418 copy_pages(extent_buffer_page(dst, dst_i),
3419 extent_buffer_page(dst, src_i),
3420 dst_off_in_page, src_off_in_page, cur);
3427 EXPORT_SYMBOL(memcpy_extent_buffer);
3429 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3430 unsigned long src_offset, unsigned long len)
3433 size_t dst_off_in_page;
3434 size_t src_off_in_page;
3435 unsigned long dst_end = dst_offset + len - 1;
3436 unsigned long src_end = src_offset + len - 1;
3437 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3438 unsigned long dst_i;
3439 unsigned long src_i;
3441 if (src_offset + len > dst->len) {
3442 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3443 src_offset, len, dst->len);
3446 if (dst_offset + len > dst->len) {
3447 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3448 dst_offset, len, dst->len);
3451 if (dst_offset < src_offset) {
3452 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3456 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3457 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3459 dst_off_in_page = (start_offset + dst_end) &
3460 ((unsigned long)PAGE_CACHE_SIZE - 1);
3461 src_off_in_page = (start_offset + src_end) &
3462 ((unsigned long)PAGE_CACHE_SIZE - 1);
3464 cur = min_t(unsigned long, len, src_off_in_page + 1);
3465 cur = min(cur, dst_off_in_page + 1);
3466 move_pages(extent_buffer_page(dst, dst_i),
3467 extent_buffer_page(dst, src_i),
3468 dst_off_in_page - cur + 1,
3469 src_off_in_page - cur + 1, cur);
3476 EXPORT_SYMBOL(memmove_extent_buffer);
3478 int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3480 u64 start = page_offset(page);
3481 struct extent_buffer *eb;
3484 unsigned long num_pages;
3486 spin_lock(&tree->buffer_lock);
3487 eb = buffer_search(tree, start);
3491 if (atomic_read(&eb->refs) > 1) {
3495 /* at this point we can safely release the extent buffer */
3496 num_pages = num_extent_pages(eb->start, eb->len);
3497 for (i = 0; i < num_pages; i++) {
3498 struct page *page = extent_buffer_page(eb, i);
3499 page_cache_release(page);
3501 rb_erase(&eb->rb_node, &tree->buffer);
3502 __free_extent_buffer(eb);
3504 spin_unlock(&tree->buffer_lock);
3507 EXPORT_SYMBOL(try_release_extent_buffer);