Fix btrfs_get_extent and get_block corner cases, and disable O_DIRECT reads
[linux-2.6] / fs / btrfs / extent_io.c
1 #include <linux/bitops.h>
2 #include <linux/slab.h>
3 #include <linux/bio.h>
4 #include <linux/mm.h>
5 #include <linux/gfp.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"
17
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 *,
22                                                     unsigned long));
23
24 static struct kmem_cache *extent_state_cache;
25 static struct kmem_cache *extent_buffer_cache;
26
27 static LIST_HEAD(buffers);
28 static LIST_HEAD(states);
29 static spinlock_t leak_lock = SPIN_LOCK_UNLOCKED;
30
31 #define BUFFER_LRU_MAX 64
32
33 struct tree_entry {
34         u64 start;
35         u64 end;
36         struct rb_node rb_node;
37 };
38
39 struct extent_page_data {
40         struct bio *bio;
41         struct extent_io_tree *tree;
42         get_extent_t *get_extent;
43 };
44
45 int __init extent_io_init(void)
46 {
47         extent_state_cache = btrfs_cache_create("extent_state",
48                                             sizeof(struct extent_state), 0,
49                                             NULL);
50         if (!extent_state_cache)
51                 return -ENOMEM;
52
53         extent_buffer_cache = btrfs_cache_create("extent_buffers",
54                                             sizeof(struct extent_buffer), 0,
55                                             NULL);
56         if (!extent_buffer_cache)
57                 goto free_state_cache;
58         return 0;
59
60 free_state_cache:
61         kmem_cache_destroy(extent_state_cache);
62         return -ENOMEM;
63 }
64
65 void extent_io_exit(void)
66 {
67         struct extent_state *state;
68         struct extent_buffer *eb;
69
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);
75
76         }
77
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);
83         }
84         if (extent_state_cache)
85                 kmem_cache_destroy(extent_state_cache);
86         if (extent_buffer_cache)
87                 kmem_cache_destroy(extent_buffer_cache);
88 }
89
90 void extent_io_tree_init(struct extent_io_tree *tree,
91                           struct address_space *mapping, gfp_t mask)
92 {
93         tree->state.rb_node = NULL;
94         tree->ops = NULL;
95         tree->dirty_bytes = 0;
96         spin_lock_init(&tree->lock);
97         spin_lock_init(&tree->lru_lock);
98         tree->mapping = mapping;
99         INIT_LIST_HEAD(&tree->buffer_lru);
100         tree->lru_size = 0;
101         tree->last = NULL;
102 }
103 EXPORT_SYMBOL(extent_io_tree_init);
104
105 void extent_io_tree_empty_lru(struct extent_io_tree *tree)
106 {
107         struct extent_buffer *eb;
108         while(!list_empty(&tree->buffer_lru)) {
109                 eb = list_entry(tree->buffer_lru.next, struct extent_buffer,
110                                 lru);
111                 list_del_init(&eb->lru);
112                 free_extent_buffer(eb);
113         }
114 }
115 EXPORT_SYMBOL(extent_io_tree_empty_lru);
116
117 struct extent_state *alloc_extent_state(gfp_t mask)
118 {
119         struct extent_state *state;
120         unsigned long flags;
121
122         state = kmem_cache_alloc(extent_state_cache, mask);
123         if (!state)
124                 return state;
125         state->state = 0;
126         state->private = 0;
127         state->tree = NULL;
128         spin_lock_irqsave(&leak_lock, flags);
129         list_add(&state->leak_list, &states);
130         spin_unlock_irqrestore(&leak_lock, flags);
131
132         atomic_set(&state->refs, 1);
133         init_waitqueue_head(&state->wq);
134         return state;
135 }
136 EXPORT_SYMBOL(alloc_extent_state);
137
138 void free_extent_state(struct extent_state *state)
139 {
140         if (!state)
141                 return;
142         if (atomic_dec_and_test(&state->refs)) {
143                 unsigned long flags;
144                 WARN_ON(state->tree);
145                 spin_lock_irqsave(&leak_lock, flags);
146                 list_del(&state->leak_list);
147                 spin_unlock_irqrestore(&leak_lock, flags);
148                 kmem_cache_free(extent_state_cache, state);
149         }
150 }
151 EXPORT_SYMBOL(free_extent_state);
152
153 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
154                                    struct rb_node *node)
155 {
156         struct rb_node ** p = &root->rb_node;
157         struct rb_node * parent = NULL;
158         struct tree_entry *entry;
159
160         while(*p) {
161                 parent = *p;
162                 entry = rb_entry(parent, struct tree_entry, rb_node);
163
164                 if (offset < entry->start)
165                         p = &(*p)->rb_left;
166                 else if (offset > entry->end)
167                         p = &(*p)->rb_right;
168                 else
169                         return parent;
170         }
171
172         entry = rb_entry(node, struct tree_entry, rb_node);
173         rb_link_node(node, parent, p);
174         rb_insert_color(node, root);
175         return NULL;
176 }
177
178 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
179                                      struct rb_node **prev_ret,
180                                      struct rb_node **next_ret)
181 {
182         struct rb_root *root = &tree->state;
183         struct rb_node * n = root->rb_node;
184         struct rb_node *prev = NULL;
185         struct rb_node *orig_prev = NULL;
186         struct tree_entry *entry;
187         struct tree_entry *prev_entry = NULL;
188
189         if (tree->last) {
190                 struct extent_state *state;
191                 state = tree->last;
192                 if (state->start <= offset && offset <= state->end)
193                         return &tree->last->rb_node;
194         }
195         while(n) {
196                 entry = rb_entry(n, struct tree_entry, rb_node);
197                 prev = n;
198                 prev_entry = entry;
199
200                 if (offset < entry->start)
201                         n = n->rb_left;
202                 else if (offset > entry->end)
203                         n = n->rb_right;
204                 else {
205                         tree->last = rb_entry(n, struct extent_state, rb_node);
206                         return n;
207                 }
208         }
209
210         if (prev_ret) {
211                 orig_prev = prev;
212                 while(prev && offset > prev_entry->end) {
213                         prev = rb_next(prev);
214                         prev_entry = rb_entry(prev, struct tree_entry, rb_node);
215                 }
216                 *prev_ret = prev;
217                 prev = orig_prev;
218         }
219
220         if (next_ret) {
221                 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
222                 while(prev && offset < prev_entry->start) {
223                         prev = rb_prev(prev);
224                         prev_entry = rb_entry(prev, struct tree_entry, rb_node);
225                 }
226                 *next_ret = prev;
227         }
228         return NULL;
229 }
230
231 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
232                                           u64 offset)
233 {
234         struct rb_node *prev = NULL;
235         struct rb_node *ret;
236
237         ret = __etree_search(tree, offset, &prev, NULL);
238         if (!ret) {
239                 if (prev) {
240                         tree->last = rb_entry(prev, struct extent_state,
241                                               rb_node);
242                 }
243                 return prev;
244         }
245         return ret;
246 }
247
248 /*
249  * utility function to look for merge candidates inside a given range.
250  * Any extents with matching state are merged together into a single
251  * extent in the tree.  Extents with EXTENT_IO in their state field
252  * are not merged because the end_io handlers need to be able to do
253  * operations on them without sleeping (or doing allocations/splits).
254  *
255  * This should be called with the tree lock held.
256  */
257 static int merge_state(struct extent_io_tree *tree,
258                        struct extent_state *state)
259 {
260         struct extent_state *other;
261         struct rb_node *other_node;
262
263         if (state->state & EXTENT_IOBITS)
264                 return 0;
265
266         other_node = rb_prev(&state->rb_node);
267         if (other_node) {
268                 other = rb_entry(other_node, struct extent_state, rb_node);
269                 if (other->end == state->start - 1 &&
270                     other->state == state->state) {
271                         state->start = other->start;
272                         other->tree = NULL;
273                         if (tree->last == other)
274                                 tree->last = state;
275                         rb_erase(&other->rb_node, &tree->state);
276                         free_extent_state(other);
277                 }
278         }
279         other_node = rb_next(&state->rb_node);
280         if (other_node) {
281                 other = rb_entry(other_node, struct extent_state, rb_node);
282                 if (other->start == state->end + 1 &&
283                     other->state == state->state) {
284                         other->start = state->start;
285                         state->tree = NULL;
286                         if (tree->last == state)
287                                 tree->last = other;
288                         rb_erase(&state->rb_node, &tree->state);
289                         free_extent_state(state);
290                 }
291         }
292         return 0;
293 }
294
295 static void set_state_cb(struct extent_io_tree *tree,
296                          struct extent_state *state,
297                          unsigned long bits)
298 {
299         if (tree->ops && tree->ops->set_bit_hook) {
300                 tree->ops->set_bit_hook(tree->mapping->host, state->start,
301                                         state->end, state->state, bits);
302         }
303 }
304
305 static void clear_state_cb(struct extent_io_tree *tree,
306                            struct extent_state *state,
307                            unsigned long bits)
308 {
309         if (tree->ops && tree->ops->set_bit_hook) {
310                 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
311                                           state->end, state->state, bits);
312         }
313 }
314
315 /*
316  * insert an extent_state struct into the tree.  'bits' are set on the
317  * struct before it is inserted.
318  *
319  * This may return -EEXIST if the extent is already there, in which case the
320  * state struct is freed.
321  *
322  * The tree lock is not taken internally.  This is a utility function and
323  * probably isn't what you want to call (see set/clear_extent_bit).
324  */
325 static int insert_state(struct extent_io_tree *tree,
326                         struct extent_state *state, u64 start, u64 end,
327                         int bits)
328 {
329         struct rb_node *node;
330
331         if (end < start) {
332                 printk("end < start %Lu %Lu\n", end, start);
333                 WARN_ON(1);
334         }
335         if (bits & EXTENT_DIRTY)
336                 tree->dirty_bytes += end - start + 1;
337         set_state_cb(tree, state, bits);
338         state->state |= bits;
339         state->start = start;
340         state->end = end;
341         node = tree_insert(&tree->state, end, &state->rb_node);
342         if (node) {
343                 struct extent_state *found;
344                 found = rb_entry(node, struct extent_state, rb_node);
345                 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
346                 free_extent_state(state);
347                 return -EEXIST;
348         }
349         state->tree = tree;
350         tree->last = state;
351         merge_state(tree, state);
352         return 0;
353 }
354
355 /*
356  * split a given extent state struct in two, inserting the preallocated
357  * struct 'prealloc' as the newly created second half.  'split' indicates an
358  * offset inside 'orig' where it should be split.
359  *
360  * Before calling,
361  * the tree has 'orig' at [orig->start, orig->end].  After calling, there
362  * are two extent state structs in the tree:
363  * prealloc: [orig->start, split - 1]
364  * orig: [ split, orig->end ]
365  *
366  * The tree locks are not taken by this function. They need to be held
367  * by the caller.
368  */
369 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
370                        struct extent_state *prealloc, u64 split)
371 {
372         struct rb_node *node;
373         prealloc->start = orig->start;
374         prealloc->end = split - 1;
375         prealloc->state = orig->state;
376         orig->start = split;
377
378         node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
379         if (node) {
380                 struct extent_state *found;
381                 found = rb_entry(node, struct extent_state, rb_node);
382                 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end);
383                 free_extent_state(prealloc);
384                 return -EEXIST;
385         }
386         prealloc->tree = tree;
387         return 0;
388 }
389
390 /*
391  * utility function to clear some bits in an extent state struct.
392  * it will optionally wake up any one waiting on this state (wake == 1), or
393  * forcibly remove the state from the tree (delete == 1).
394  *
395  * If no bits are set on the state struct after clearing things, the
396  * struct is freed and removed from the tree
397  */
398 static int clear_state_bit(struct extent_io_tree *tree,
399                             struct extent_state *state, int bits, int wake,
400                             int delete)
401 {
402         int ret = state->state & bits;
403
404         if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
405                 u64 range = state->end - state->start + 1;
406                 WARN_ON(range > tree->dirty_bytes);
407                 tree->dirty_bytes -= range;
408         }
409         clear_state_cb(tree, state, bits);
410         state->state &= ~bits;
411         if (wake)
412                 wake_up(&state->wq);
413         if (delete || state->state == 0) {
414                 if (state->tree) {
415                         clear_state_cb(tree, state, state->state);
416                         if (tree->last == state) {
417                                 tree->last = extent_state_next(state);
418                         }
419                         rb_erase(&state->rb_node, &tree->state);
420                         state->tree = NULL;
421                         free_extent_state(state);
422                 } else {
423                         WARN_ON(1);
424                 }
425         } else {
426                 merge_state(tree, state);
427         }
428         return ret;
429 }
430
431 /*
432  * clear some bits on a range in the tree.  This may require splitting
433  * or inserting elements in the tree, so the gfp mask is used to
434  * indicate which allocations or sleeping are allowed.
435  *
436  * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
437  * the given range from the tree regardless of state (ie for truncate).
438  *
439  * the range [start, end] is inclusive.
440  *
441  * This takes the tree lock, and returns < 0 on error, > 0 if any of the
442  * bits were already set, or zero if none of the bits were already set.
443  */
444 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
445                      int bits, int wake, int delete, gfp_t mask)
446 {
447         struct extent_state *state;
448         struct extent_state *prealloc = NULL;
449         struct rb_node *node;
450         unsigned long flags;
451         int err;
452         int set = 0;
453
454 again:
455         if (!prealloc && (mask & __GFP_WAIT)) {
456                 prealloc = alloc_extent_state(mask);
457                 if (!prealloc)
458                         return -ENOMEM;
459         }
460
461         spin_lock_irqsave(&tree->lock, flags);
462         /*
463          * this search will find the extents that end after
464          * our range starts
465          */
466         node = tree_search(tree, start);
467         if (!node)
468                 goto out;
469         state = rb_entry(node, struct extent_state, rb_node);
470         if (state->start > end)
471                 goto out;
472         WARN_ON(state->end < start);
473
474         /*
475          *     | ---- desired range ---- |
476          *  | state | or
477          *  | ------------- state -------------- |
478          *
479          * We need to split the extent we found, and may flip
480          * bits on second half.
481          *
482          * If the extent we found extends past our range, we
483          * just split and search again.  It'll get split again
484          * the next time though.
485          *
486          * If the extent we found is inside our range, we clear
487          * the desired bit on it.
488          */
489
490         if (state->start < start) {
491                 if (!prealloc)
492                         prealloc = alloc_extent_state(GFP_ATOMIC);
493                 err = split_state(tree, state, prealloc, start);
494                 BUG_ON(err == -EEXIST);
495                 prealloc = NULL;
496                 if (err)
497                         goto out;
498                 if (state->end <= end) {
499                         start = state->end + 1;
500                         set |= clear_state_bit(tree, state, bits,
501                                         wake, delete);
502                 } else {
503                         start = state->start;
504                 }
505                 goto search_again;
506         }
507         /*
508          * | ---- desired range ---- |
509          *                        | state |
510          * We need to split the extent, and clear the bit
511          * on the first half
512          */
513         if (state->start <= end && state->end > end) {
514                 if (!prealloc)
515                         prealloc = alloc_extent_state(GFP_ATOMIC);
516                 err = split_state(tree, state, prealloc, end + 1);
517                 BUG_ON(err == -EEXIST);
518
519                 if (wake)
520                         wake_up(&state->wq);
521                 set |= clear_state_bit(tree, prealloc, bits,
522                                        wake, delete);
523                 prealloc = NULL;
524                 goto out;
525         }
526
527         start = state->end + 1;
528         set |= clear_state_bit(tree, state, bits, wake, delete);
529         goto search_again;
530
531 out:
532         spin_unlock_irqrestore(&tree->lock, flags);
533         if (prealloc)
534                 free_extent_state(prealloc);
535
536         return set;
537
538 search_again:
539         if (start > end)
540                 goto out;
541         spin_unlock_irqrestore(&tree->lock, flags);
542         if (mask & __GFP_WAIT)
543                 cond_resched();
544         goto again;
545 }
546 EXPORT_SYMBOL(clear_extent_bit);
547
548 static int wait_on_state(struct extent_io_tree *tree,
549                          struct extent_state *state)
550 {
551         DEFINE_WAIT(wait);
552         prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
553         spin_unlock_irq(&tree->lock);
554         schedule();
555         spin_lock_irq(&tree->lock);
556         finish_wait(&state->wq, &wait);
557         return 0;
558 }
559
560 /*
561  * waits for one or more bits to clear on a range in the state tree.
562  * The range [start, end] is inclusive.
563  * The tree lock is taken by this function
564  */
565 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
566 {
567         struct extent_state *state;
568         struct rb_node *node;
569
570         spin_lock_irq(&tree->lock);
571 again:
572         while (1) {
573                 /*
574                  * this search will find all the extents that end after
575                  * our range starts
576                  */
577                 node = tree_search(tree, start);
578                 if (!node)
579                         break;
580
581                 state = rb_entry(node, struct extent_state, rb_node);
582
583                 if (state->start > end)
584                         goto out;
585
586                 if (state->state & bits) {
587                         start = state->start;
588                         atomic_inc(&state->refs);
589                         wait_on_state(tree, state);
590                         free_extent_state(state);
591                         goto again;
592                 }
593                 start = state->end + 1;
594
595                 if (start > end)
596                         break;
597
598                 if (need_resched()) {
599                         spin_unlock_irq(&tree->lock);
600                         cond_resched();
601                         spin_lock_irq(&tree->lock);
602                 }
603         }
604 out:
605         spin_unlock_irq(&tree->lock);
606         return 0;
607 }
608 EXPORT_SYMBOL(wait_extent_bit);
609
610 static void set_state_bits(struct extent_io_tree *tree,
611                            struct extent_state *state,
612                            int bits)
613 {
614         if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
615                 u64 range = state->end - state->start + 1;
616                 tree->dirty_bytes += range;
617         }
618         set_state_cb(tree, state, bits);
619         state->state |= bits;
620 }
621
622 /*
623  * set some bits on a range in the tree.  This may require allocations
624  * or sleeping, so the gfp mask is used to indicate what is allowed.
625  *
626  * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
627  * range already has the desired bits set.  The start of the existing
628  * range is returned in failed_start in this case.
629  *
630  * [start, end] is inclusive
631  * This takes the tree lock.
632  */
633 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
634                    int exclusive, u64 *failed_start, gfp_t mask)
635 {
636         struct extent_state *state;
637         struct extent_state *prealloc = NULL;
638         struct rb_node *node;
639         unsigned long flags;
640         int err = 0;
641         int set;
642         u64 last_start;
643         u64 last_end;
644 again:
645         if (!prealloc && (mask & __GFP_WAIT)) {
646                 prealloc = alloc_extent_state(mask);
647                 if (!prealloc)
648                         return -ENOMEM;
649         }
650
651         spin_lock_irqsave(&tree->lock, flags);
652         /*
653          * this search will find all the extents that end after
654          * our range starts.
655          */
656         node = tree_search(tree, start);
657         if (!node) {
658                 err = insert_state(tree, prealloc, start, end, bits);
659                 prealloc = NULL;
660                 BUG_ON(err == -EEXIST);
661                 goto out;
662         }
663
664         state = rb_entry(node, struct extent_state, rb_node);
665         last_start = state->start;
666         last_end = state->end;
667
668         /*
669          * | ---- desired range ---- |
670          * | state |
671          *
672          * Just lock what we found and keep going
673          */
674         if (state->start == start && state->end <= end) {
675                 set = state->state & bits;
676                 if (set && exclusive) {
677                         *failed_start = state->start;
678                         err = -EEXIST;
679                         goto out;
680                 }
681                 set_state_bits(tree, state, bits);
682                 start = state->end + 1;
683                 merge_state(tree, state);
684                 goto search_again;
685         }
686
687         /*
688          *     | ---- desired range ---- |
689          * | state |
690          *   or
691          * | ------------- state -------------- |
692          *
693          * We need to split the extent we found, and may flip bits on
694          * second half.
695          *
696          * If the extent we found extends past our
697          * range, we just split and search again.  It'll get split
698          * again the next time though.
699          *
700          * If the extent we found is inside our range, we set the
701          * desired bit on it.
702          */
703         if (state->start < start) {
704                 set = state->state & bits;
705                 if (exclusive && set) {
706                         *failed_start = start;
707                         err = -EEXIST;
708                         goto out;
709                 }
710                 err = split_state(tree, state, prealloc, start);
711                 BUG_ON(err == -EEXIST);
712                 prealloc = NULL;
713                 if (err)
714                         goto out;
715                 if (state->end <= end) {
716                         set_state_bits(tree, state, bits);
717                         start = state->end + 1;
718                         merge_state(tree, state);
719                 } else {
720                         start = state->start;
721                 }
722                 goto search_again;
723         }
724         /*
725          * | ---- desired range ---- |
726          *     | state | or               | state |
727          *
728          * There's a hole, we need to insert something in it and
729          * ignore the extent we found.
730          */
731         if (state->start > start) {
732                 u64 this_end;
733                 if (end < last_start)
734                         this_end = end;
735                 else
736                         this_end = last_start -1;
737                 err = insert_state(tree, prealloc, start, this_end,
738                                    bits);
739                 prealloc = NULL;
740                 BUG_ON(err == -EEXIST);
741                 if (err)
742                         goto out;
743                 start = this_end + 1;
744                 goto search_again;
745         }
746         /*
747          * | ---- desired range ---- |
748          *                        | state |
749          * We need to split the extent, and set the bit
750          * on the first half
751          */
752         if (state->start <= end && state->end > end) {
753                 set = state->state & bits;
754                 if (exclusive && set) {
755                         *failed_start = start;
756                         err = -EEXIST;
757                         goto out;
758                 }
759                 err = split_state(tree, state, prealloc, end + 1);
760                 BUG_ON(err == -EEXIST);
761
762                 set_state_bits(tree, prealloc, bits);
763                 merge_state(tree, prealloc);
764                 prealloc = NULL;
765                 goto out;
766         }
767
768         goto search_again;
769
770 out:
771         spin_unlock_irqrestore(&tree->lock, flags);
772         if (prealloc)
773                 free_extent_state(prealloc);
774
775         return err;
776
777 search_again:
778         if (start > end)
779                 goto out;
780         spin_unlock_irqrestore(&tree->lock, flags);
781         if (mask & __GFP_WAIT)
782                 cond_resched();
783         goto again;
784 }
785 EXPORT_SYMBOL(set_extent_bit);
786
787 /* wrappers around set/clear extent bit */
788 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
789                      gfp_t mask)
790 {
791         return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
792                               mask);
793 }
794 EXPORT_SYMBOL(set_extent_dirty);
795
796 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
797                     int bits, gfp_t mask)
798 {
799         return set_extent_bit(tree, start, end, bits, 0, NULL,
800                               mask);
801 }
802 EXPORT_SYMBOL(set_extent_bits);
803
804 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
805                       int bits, gfp_t mask)
806 {
807         return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
808 }
809 EXPORT_SYMBOL(clear_extent_bits);
810
811 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
812                      gfp_t mask)
813 {
814         return set_extent_bit(tree, start, end,
815                               EXTENT_DELALLOC | EXTENT_DIRTY, 0, NULL,
816                               mask);
817 }
818 EXPORT_SYMBOL(set_extent_delalloc);
819
820 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
821                        gfp_t mask)
822 {
823         return clear_extent_bit(tree, start, end,
824                                 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
825 }
826 EXPORT_SYMBOL(clear_extent_dirty);
827
828 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
829                      gfp_t mask)
830 {
831         return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
832                               mask);
833 }
834 EXPORT_SYMBOL(set_extent_new);
835
836 int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
837                        gfp_t mask)
838 {
839         return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
840 }
841 EXPORT_SYMBOL(clear_extent_new);
842
843 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
844                         gfp_t mask)
845 {
846         return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
847                               mask);
848 }
849 EXPORT_SYMBOL(set_extent_uptodate);
850
851 int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
852                           gfp_t mask)
853 {
854         return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
855 }
856 EXPORT_SYMBOL(clear_extent_uptodate);
857
858 int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
859                          gfp_t mask)
860 {
861         return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
862                               0, NULL, mask);
863 }
864 EXPORT_SYMBOL(set_extent_writeback);
865
866 int clear_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
867                            gfp_t mask)
868 {
869         return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
870 }
871 EXPORT_SYMBOL(clear_extent_writeback);
872
873 int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
874 {
875         return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
876 }
877 EXPORT_SYMBOL(wait_on_extent_writeback);
878
879 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
880 {
881         int err;
882         u64 failed_start;
883         while (1) {
884                 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
885                                      &failed_start, mask);
886                 if (err == -EEXIST && (mask & __GFP_WAIT)) {
887                         wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
888                         start = failed_start;
889                 } else {
890                         break;
891                 }
892                 WARN_ON(start > end);
893         }
894         return err;
895 }
896 EXPORT_SYMBOL(lock_extent);
897
898 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
899                   gfp_t mask)
900 {
901         return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
902 }
903 EXPORT_SYMBOL(unlock_extent);
904
905 /*
906  * helper function to set pages and extents in the tree dirty
907  */
908 int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
909 {
910         unsigned long index = start >> PAGE_CACHE_SHIFT;
911         unsigned long end_index = end >> PAGE_CACHE_SHIFT;
912         struct page *page;
913
914         while (index <= end_index) {
915                 page = find_get_page(tree->mapping, index);
916                 BUG_ON(!page);
917                 __set_page_dirty_nobuffers(page);
918                 page_cache_release(page);
919                 index++;
920         }
921         set_extent_dirty(tree, start, end, GFP_NOFS);
922         return 0;
923 }
924 EXPORT_SYMBOL(set_range_dirty);
925
926 /*
927  * helper function to set both pages and extents in the tree writeback
928  */
929 int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
930 {
931         unsigned long index = start >> PAGE_CACHE_SHIFT;
932         unsigned long end_index = end >> PAGE_CACHE_SHIFT;
933         struct page *page;
934
935         while (index <= end_index) {
936                 page = find_get_page(tree->mapping, index);
937                 BUG_ON(!page);
938                 set_page_writeback(page);
939                 page_cache_release(page);
940                 index++;
941         }
942         set_extent_writeback(tree, start, end, GFP_NOFS);
943         return 0;
944 }
945 EXPORT_SYMBOL(set_range_writeback);
946
947 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
948                           u64 *start_ret, u64 *end_ret, int bits)
949 {
950         struct rb_node *node;
951         struct extent_state *state;
952         int ret = 1;
953
954         spin_lock_irq(&tree->lock);
955         /*
956          * this search will find all the extents that end after
957          * our range starts.
958          */
959         node = tree_search(tree, start);
960         if (!node) {
961                 goto out;
962         }
963
964         while(1) {
965                 state = rb_entry(node, struct extent_state, rb_node);
966                 if (state->end >= start && (state->state & bits)) {
967                         *start_ret = state->start;
968                         *end_ret = state->end;
969                         ret = 0;
970                         break;
971                 }
972                 node = rb_next(node);
973                 if (!node)
974                         break;
975         }
976 out:
977         spin_unlock_irq(&tree->lock);
978         return ret;
979 }
980 EXPORT_SYMBOL(find_first_extent_bit);
981
982 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
983                                                  u64 start, int bits)
984 {
985         struct rb_node *node;
986         struct extent_state *state;
987
988         /*
989          * this search will find all the extents that end after
990          * our range starts.
991          */
992         node = tree_search(tree, start);
993         if (!node) {
994                 goto out;
995         }
996
997         while(1) {
998                 state = rb_entry(node, struct extent_state, rb_node);
999                 if (state->end >= start && (state->state & bits)) {
1000                         return state;
1001                 }
1002                 node = rb_next(node);
1003                 if (!node)
1004                         break;
1005         }
1006 out:
1007         return NULL;
1008 }
1009 EXPORT_SYMBOL(find_first_extent_bit_state);
1010
1011 u64 find_lock_delalloc_range(struct extent_io_tree *tree,
1012                              u64 *start, u64 *end, u64 max_bytes)
1013 {
1014         struct rb_node *node;
1015         struct extent_state *state;
1016         u64 cur_start = *start;
1017         u64 found = 0;
1018         u64 total_bytes = 0;
1019
1020         spin_lock_irq(&tree->lock);
1021         /*
1022          * this search will find all the extents that end after
1023          * our range starts.
1024          */
1025 search_again:
1026         node = tree_search(tree, cur_start);
1027         if (!node) {
1028                 if (!found)
1029                         *end = (u64)-1;
1030                 goto out;
1031         }
1032
1033         while(1) {
1034                 state = rb_entry(node, struct extent_state, rb_node);
1035                 if (found && state->start != cur_start) {
1036                         goto out;
1037                 }
1038                 if (!(state->state & EXTENT_DELALLOC)) {
1039                         if (!found)
1040                                 *end = state->end;
1041                         goto out;
1042                 }
1043                 if (!found) {
1044                         struct extent_state *prev_state;
1045                         struct rb_node *prev_node = node;
1046                         while(1) {
1047                                 prev_node = rb_prev(prev_node);
1048                                 if (!prev_node)
1049                                         break;
1050                                 prev_state = rb_entry(prev_node,
1051                                                       struct extent_state,
1052                                                       rb_node);
1053                                 if (!(prev_state->state & EXTENT_DELALLOC))
1054                                         break;
1055                                 state = prev_state;
1056                                 node = prev_node;
1057                         }
1058                 }
1059                 if (state->state & EXTENT_LOCKED) {
1060                         DEFINE_WAIT(wait);
1061                         atomic_inc(&state->refs);
1062                         prepare_to_wait(&state->wq, &wait,
1063                                         TASK_UNINTERRUPTIBLE);
1064                         spin_unlock_irq(&tree->lock);
1065                         schedule();
1066                         spin_lock_irq(&tree->lock);
1067                         finish_wait(&state->wq, &wait);
1068                         free_extent_state(state);
1069                         goto search_again;
1070                 }
1071                 set_state_cb(tree, state, EXTENT_LOCKED);
1072                 state->state |= EXTENT_LOCKED;
1073                 if (!found)
1074                         *start = state->start;
1075                 found++;
1076                 *end = state->end;
1077                 cur_start = state->end + 1;
1078                 node = rb_next(node);
1079                 if (!node)
1080                         break;
1081                 total_bytes += state->end - state->start + 1;
1082                 if (total_bytes >= max_bytes)
1083                         break;
1084         }
1085 out:
1086         spin_unlock_irq(&tree->lock);
1087         return found;
1088 }
1089
1090 u64 count_range_bits(struct extent_io_tree *tree,
1091                      u64 *start, u64 search_end, u64 max_bytes,
1092                      unsigned long bits)
1093 {
1094         struct rb_node *node;
1095         struct extent_state *state;
1096         u64 cur_start = *start;
1097         u64 total_bytes = 0;
1098         int found = 0;
1099
1100         if (search_end <= cur_start) {
1101                 printk("search_end %Lu start %Lu\n", search_end, cur_start);
1102                 WARN_ON(1);
1103                 return 0;
1104         }
1105
1106         spin_lock_irq(&tree->lock);
1107         if (cur_start == 0 && bits == EXTENT_DIRTY) {
1108                 total_bytes = tree->dirty_bytes;
1109                 goto out;
1110         }
1111         /*
1112          * this search will find all the extents that end after
1113          * our range starts.
1114          */
1115         node = tree_search(tree, cur_start);
1116         if (!node) {
1117                 goto out;
1118         }
1119
1120         while(1) {
1121                 state = rb_entry(node, struct extent_state, rb_node);
1122                 if (state->start > search_end)
1123                         break;
1124                 if (state->end >= cur_start && (state->state & bits)) {
1125                         total_bytes += min(search_end, state->end) + 1 -
1126                                        max(cur_start, state->start);
1127                         if (total_bytes >= max_bytes)
1128                                 break;
1129                         if (!found) {
1130                                 *start = state->start;
1131                                 found = 1;
1132                         }
1133                 }
1134                 node = rb_next(node);
1135                 if (!node)
1136                         break;
1137         }
1138 out:
1139         spin_unlock_irq(&tree->lock);
1140         return total_bytes;
1141 }
1142 /*
1143  * helper function to lock both pages and extents in the tree.
1144  * pages must be locked first.
1145  */
1146 int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
1147 {
1148         unsigned long index = start >> PAGE_CACHE_SHIFT;
1149         unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1150         struct page *page;
1151         int err;
1152
1153         while (index <= end_index) {
1154                 page = grab_cache_page(tree->mapping, index);
1155                 if (!page) {
1156                         err = -ENOMEM;
1157                         goto failed;
1158                 }
1159                 if (IS_ERR(page)) {
1160                         err = PTR_ERR(page);
1161                         goto failed;
1162                 }
1163                 index++;
1164         }
1165         lock_extent(tree, start, end, GFP_NOFS);
1166         return 0;
1167
1168 failed:
1169         /*
1170          * we failed above in getting the page at 'index', so we undo here
1171          * up to but not including the page at 'index'
1172          */
1173         end_index = index;
1174         index = start >> PAGE_CACHE_SHIFT;
1175         while (index < end_index) {
1176                 page = find_get_page(tree->mapping, index);
1177                 unlock_page(page);
1178                 page_cache_release(page);
1179                 index++;
1180         }
1181         return err;
1182 }
1183 EXPORT_SYMBOL(lock_range);
1184
1185 /*
1186  * helper function to unlock both pages and extents in the tree.
1187  */
1188 int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
1189 {
1190         unsigned long index = start >> PAGE_CACHE_SHIFT;
1191         unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1192         struct page *page;
1193
1194         while (index <= end_index) {
1195                 page = find_get_page(tree->mapping, index);
1196                 unlock_page(page);
1197                 page_cache_release(page);
1198                 index++;
1199         }
1200         unlock_extent(tree, start, end, GFP_NOFS);
1201         return 0;
1202 }
1203 EXPORT_SYMBOL(unlock_range);
1204
1205 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1206 {
1207         struct rb_node *node;
1208         struct extent_state *state;
1209         int ret = 0;
1210
1211         spin_lock_irq(&tree->lock);
1212         /*
1213          * this search will find all the extents that end after
1214          * our range starts.
1215          */
1216         node = tree_search(tree, start);
1217         if (!node) {
1218                 ret = -ENOENT;
1219                 goto out;
1220         }
1221         state = rb_entry(node, struct extent_state, rb_node);
1222         if (state->start != start) {
1223                 ret = -ENOENT;
1224                 goto out;
1225         }
1226         state->private = private;
1227 out:
1228         spin_unlock_irq(&tree->lock);
1229         return ret;
1230 }
1231
1232 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1233 {
1234         struct rb_node *node;
1235         struct extent_state *state;
1236         int ret = 0;
1237
1238         spin_lock_irq(&tree->lock);
1239         /*
1240          * this search will find all the extents that end after
1241          * our range starts.
1242          */
1243         node = tree_search(tree, start);
1244         if (!node) {
1245                 ret = -ENOENT;
1246                 goto out;
1247         }
1248         state = rb_entry(node, struct extent_state, rb_node);
1249         if (state->start != start) {
1250                 ret = -ENOENT;
1251                 goto out;
1252         }
1253         *private = state->private;
1254 out:
1255         spin_unlock_irq(&tree->lock);
1256         return ret;
1257 }
1258
1259 /*
1260  * searches a range in the state tree for a given mask.
1261  * If 'filled' == 1, this returns 1 only if every extent in the tree
1262  * has the bits set.  Otherwise, 1 is returned if any bit in the
1263  * range is found set.
1264  */
1265 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1266                    int bits, int filled)
1267 {
1268         struct extent_state *state = NULL;
1269         struct rb_node *node;
1270         int bitset = 0;
1271         unsigned long flags;
1272
1273         spin_lock_irqsave(&tree->lock, flags);
1274         node = tree_search(tree, start);
1275         while (node && start <= end) {
1276                 state = rb_entry(node, struct extent_state, rb_node);
1277
1278                 if (filled && state->start > start) {
1279                         bitset = 0;
1280                         break;
1281                 }
1282
1283                 if (state->start > end)
1284                         break;
1285
1286                 if (state->state & bits) {
1287                         bitset = 1;
1288                         if (!filled)
1289                                 break;
1290                 } else if (filled) {
1291                         bitset = 0;
1292                         break;
1293                 }
1294                 start = state->end + 1;
1295                 if (start > end)
1296                         break;
1297                 node = rb_next(node);
1298                 if (!node) {
1299                         if (filled)
1300                                 bitset = 0;
1301                         break;
1302                 }
1303         }
1304         spin_unlock_irqrestore(&tree->lock, flags);
1305         return bitset;
1306 }
1307 EXPORT_SYMBOL(test_range_bit);
1308
1309 /*
1310  * helper function to set a given page up to date if all the
1311  * extents in the tree for that page are up to date
1312  */
1313 static int check_page_uptodate(struct extent_io_tree *tree,
1314                                struct page *page)
1315 {
1316         u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1317         u64 end = start + PAGE_CACHE_SIZE - 1;
1318         if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1319                 SetPageUptodate(page);
1320         return 0;
1321 }
1322
1323 /*
1324  * helper function to unlock a page if all the extents in the tree
1325  * for that page are unlocked
1326  */
1327 static int check_page_locked(struct extent_io_tree *tree,
1328                              struct page *page)
1329 {
1330         u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1331         u64 end = start + PAGE_CACHE_SIZE - 1;
1332         if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1333                 unlock_page(page);
1334         return 0;
1335 }
1336
1337 /*
1338  * helper function to end page writeback if all the extents
1339  * in the tree for that page are done with writeback
1340  */
1341 static int check_page_writeback(struct extent_io_tree *tree,
1342                              struct page *page)
1343 {
1344         u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1345         u64 end = start + PAGE_CACHE_SIZE - 1;
1346         if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1347                 end_page_writeback(page);
1348         return 0;
1349 }
1350
1351 /* lots and lots of room for performance fixes in the end_bio funcs */
1352
1353 /*
1354  * after a writepage IO is done, we need to:
1355  * clear the uptodate bits on error
1356  * clear the writeback bits in the extent tree for this IO
1357  * end_page_writeback if the page has no more pending IO
1358  *
1359  * Scheduling is not allowed, so the extent state tree is expected
1360  * to have one and only one object corresponding to this IO.
1361  */
1362 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1363 static void end_bio_extent_writepage(struct bio *bio, int err)
1364 #else
1365 static int end_bio_extent_writepage(struct bio *bio,
1366                                    unsigned int bytes_done, int err)
1367 #endif
1368 {
1369         const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1370         struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1371         struct extent_state *state = bio->bi_private;
1372         struct extent_io_tree *tree = state->tree;
1373         struct rb_node *node;
1374         u64 start;
1375         u64 end;
1376         u64 cur;
1377         int whole_page;
1378         unsigned long flags;
1379
1380 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1381         if (bio->bi_size)
1382                 return 1;
1383 #endif
1384         do {
1385                 struct page *page = bvec->bv_page;
1386                 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1387                          bvec->bv_offset;
1388                 end = start + bvec->bv_len - 1;
1389
1390                 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1391                         whole_page = 1;
1392                 else
1393                         whole_page = 0;
1394
1395                 if (--bvec >= bio->bi_io_vec)
1396                         prefetchw(&bvec->bv_page->flags);
1397
1398                 if (!uptodate) {
1399                         clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1400                         ClearPageUptodate(page);
1401                         SetPageError(page);
1402                 }
1403
1404                 if (tree->ops && tree->ops->writepage_end_io_hook) {
1405                         tree->ops->writepage_end_io_hook(page, start, end,
1406                                                          state);
1407                 }
1408
1409                 /*
1410                  * bios can get merged in funny ways, and so we need to
1411                  * be careful with the state variable.  We know the
1412                  * state won't be merged with others because it has
1413                  * WRITEBACK set, but we can't be sure each biovec is
1414                  * sequential in the file.  So, if our cached state
1415                  * doesn't match the expected end, search the tree
1416                  * for the correct one.
1417                  */
1418
1419                 spin_lock_irqsave(&tree->lock, flags);
1420                 if (!state || state->end != end) {
1421                         state = NULL;
1422                         node = __etree_search(tree, start, NULL, NULL);
1423                         if (node) {
1424                                 state = rb_entry(node, struct extent_state,
1425                                                  rb_node);
1426                                 if (state->end != end ||
1427                                     !(state->state & EXTENT_WRITEBACK))
1428                                         state = NULL;
1429                         }
1430                         if (!state) {
1431                                 spin_unlock_irqrestore(&tree->lock, flags);
1432                                 clear_extent_writeback(tree, start,
1433                                                        end, GFP_ATOMIC);
1434                                 goto next_io;
1435                         }
1436                 }
1437                 cur = end;
1438                 while(1) {
1439                         struct extent_state *clear = state;
1440                         cur = state->start;
1441                         node = rb_prev(&state->rb_node);
1442                         if (node) {
1443                                 state = rb_entry(node,
1444                                                  struct extent_state,
1445                                                  rb_node);
1446                         } else {
1447                                 state = NULL;
1448                         }
1449
1450                         clear_state_bit(tree, clear, EXTENT_WRITEBACK,
1451                                         1, 0);
1452                         if (cur == start)
1453                                 break;
1454                         if (cur < start) {
1455                                 WARN_ON(1);
1456                                 break;
1457                         }
1458                         if (!node)
1459                                 break;
1460                 }
1461                 /* before releasing the lock, make sure the next state
1462                  * variable has the expected bits set and corresponds
1463                  * to the correct offsets in the file
1464                  */
1465                 if (state && (state->end + 1 != start ||
1466                     !(state->state & EXTENT_WRITEBACK))) {
1467                         state = NULL;
1468                 }
1469                 spin_unlock_irqrestore(&tree->lock, flags);
1470 next_io:
1471
1472                 if (whole_page)
1473                         end_page_writeback(page);
1474                 else
1475                         check_page_writeback(tree, page);
1476         } while (bvec >= bio->bi_io_vec);
1477         bio_put(bio);
1478 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1479         return 0;
1480 #endif
1481 }
1482
1483 /*
1484  * after a readpage IO is done, we need to:
1485  * clear the uptodate bits on error
1486  * set the uptodate bits if things worked
1487  * set the page up to date if all extents in the tree are uptodate
1488  * clear the lock bit in the extent tree
1489  * unlock the page if there are no other extents locked for it
1490  *
1491  * Scheduling is not allowed, so the extent state tree is expected
1492  * to have one and only one object corresponding to this IO.
1493  */
1494 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1495 static void end_bio_extent_readpage(struct bio *bio, int err)
1496 #else
1497 static int end_bio_extent_readpage(struct bio *bio,
1498                                    unsigned int bytes_done, int err)
1499 #endif
1500 {
1501         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1502         struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1503         struct extent_state *state = bio->bi_private;
1504         struct extent_io_tree *tree = state->tree;
1505         struct rb_node *node;
1506         u64 start;
1507         u64 end;
1508         u64 cur;
1509         unsigned long flags;
1510         int whole_page;
1511         int ret;
1512
1513 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1514         if (bio->bi_size)
1515                 return 1;
1516 #endif
1517
1518         do {
1519                 struct page *page = bvec->bv_page;
1520                 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1521                         bvec->bv_offset;
1522                 end = start + bvec->bv_len - 1;
1523
1524                 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1525                         whole_page = 1;
1526                 else
1527                         whole_page = 0;
1528
1529                 if (--bvec >= bio->bi_io_vec)
1530                         prefetchw(&bvec->bv_page->flags);
1531
1532                 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1533                         ret = tree->ops->readpage_end_io_hook(page, start, end,
1534                                                               state);
1535                         if (ret)
1536                                 uptodate = 0;
1537                 }
1538                 if (!uptodate && tree->ops &&
1539                     tree->ops->readpage_io_failed_hook) {
1540                         ret = tree->ops->readpage_io_failed_hook(bio, page,
1541                                                          start, end, state);
1542                         if (ret == 0) {
1543                                 state = NULL;
1544                                 uptodate =
1545                                         test_bit(BIO_UPTODATE, &bio->bi_flags);
1546                                 continue;
1547                         }
1548                 }
1549
1550                 spin_lock_irqsave(&tree->lock, flags);
1551                 if (!state || state->end != end) {
1552                         state = NULL;
1553                         node = __etree_search(tree, start, NULL, NULL);
1554                         if (node) {
1555                                 state = rb_entry(node, struct extent_state,
1556                                                  rb_node);
1557                                 if (state->end != end ||
1558                                     !(state->state & EXTENT_LOCKED))
1559                                         state = NULL;
1560                         }
1561                         if (!state) {
1562                                 spin_unlock_irqrestore(&tree->lock, flags);
1563                                 if (uptodate)
1564                                         set_extent_uptodate(tree, start, end,
1565                                                             GFP_ATOMIC);
1566                                 unlock_extent(tree, start, end, GFP_ATOMIC);
1567                                 goto next_io;
1568                         }
1569                 }
1570
1571                 cur = end;
1572                 while(1) {
1573                         struct extent_state *clear = state;
1574                         cur = state->start;
1575                         node = rb_prev(&state->rb_node);
1576                         if (node) {
1577                                 state = rb_entry(node,
1578                                          struct extent_state,
1579                                          rb_node);
1580                         } else {
1581                                 state = NULL;
1582                         }
1583                         if (uptodate) {
1584                                 set_state_cb(tree, clear, EXTENT_UPTODATE);
1585                                 clear->state |= EXTENT_UPTODATE;
1586                         }
1587                         clear_state_bit(tree, clear, EXTENT_LOCKED,
1588                                         1, 0);
1589                         if (cur == start)
1590                                 break;
1591                         if (cur < start) {
1592                                 WARN_ON(1);
1593                                 break;
1594                         }
1595                         if (!node)
1596                                 break;
1597                 }
1598                 /* before releasing the lock, make sure the next state
1599                  * variable has the expected bits set and corresponds
1600                  * to the correct offsets in the file
1601                  */
1602                 if (state && (state->end + 1 != start ||
1603                     !(state->state & EXTENT_LOCKED))) {
1604                         state = NULL;
1605                 }
1606                 spin_unlock_irqrestore(&tree->lock, flags);
1607 next_io:
1608                 if (whole_page) {
1609                         if (uptodate) {
1610                                 SetPageUptodate(page);
1611                         } else {
1612                                 ClearPageUptodate(page);
1613                                 SetPageError(page);
1614                         }
1615                         unlock_page(page);
1616                 } else {
1617                         if (uptodate) {
1618                                 check_page_uptodate(tree, page);
1619                         } else {
1620                                 ClearPageUptodate(page);
1621                                 SetPageError(page);
1622                         }
1623                         check_page_locked(tree, page);
1624                 }
1625         } while (bvec >= bio->bi_io_vec);
1626
1627         bio_put(bio);
1628 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1629         return 0;
1630 #endif
1631 }
1632
1633 /*
1634  * IO done from prepare_write is pretty simple, we just unlock
1635  * the structs in the extent tree when done, and set the uptodate bits
1636  * as appropriate.
1637  */
1638 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1639 static void end_bio_extent_preparewrite(struct bio *bio, int err)
1640 #else
1641 static int end_bio_extent_preparewrite(struct bio *bio,
1642                                        unsigned int bytes_done, int err)
1643 #endif
1644 {
1645         const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1646         struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1647         struct extent_state *state = bio->bi_private;
1648         struct extent_io_tree *tree = state->tree;
1649         u64 start;
1650         u64 end;
1651
1652 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1653         if (bio->bi_size)
1654                 return 1;
1655 #endif
1656
1657         do {
1658                 struct page *page = bvec->bv_page;
1659                 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1660                         bvec->bv_offset;
1661                 end = start + bvec->bv_len - 1;
1662
1663                 if (--bvec >= bio->bi_io_vec)
1664                         prefetchw(&bvec->bv_page->flags);
1665
1666                 if (uptodate) {
1667                         set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1668                 } else {
1669                         ClearPageUptodate(page);
1670                         SetPageError(page);
1671                 }
1672
1673                 unlock_extent(tree, start, end, GFP_ATOMIC);
1674
1675         } while (bvec >= bio->bi_io_vec);
1676
1677         bio_put(bio);
1678 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1679         return 0;
1680 #endif
1681 }
1682
1683 static struct bio *
1684 extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1685                  gfp_t gfp_flags)
1686 {
1687         struct bio *bio;
1688
1689         bio = bio_alloc(gfp_flags, nr_vecs);
1690
1691         if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1692                 while (!bio && (nr_vecs /= 2))
1693                         bio = bio_alloc(gfp_flags, nr_vecs);
1694         }
1695
1696         if (bio) {
1697                 bio->bi_size = 0;
1698                 bio->bi_bdev = bdev;
1699                 bio->bi_sector = first_sector;
1700         }
1701         return bio;
1702 }
1703
1704 static int submit_one_bio(int rw, struct bio *bio, int mirror_num)
1705 {
1706         int ret = 0;
1707         struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1708         struct page *page = bvec->bv_page;
1709         struct extent_io_tree *tree = bio->bi_private;
1710         struct rb_node *node;
1711         struct extent_state *state;
1712         u64 start;
1713         u64 end;
1714
1715         start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1716         end = start + bvec->bv_len - 1;
1717
1718         spin_lock_irq(&tree->lock);
1719         node = __etree_search(tree, start, NULL, NULL);
1720         BUG_ON(!node);
1721         state = rb_entry(node, struct extent_state, rb_node);
1722         while(state->end < end) {
1723                 node = rb_next(node);
1724                 state = rb_entry(node, struct extent_state, rb_node);
1725         }
1726         BUG_ON(state->end != end);
1727         spin_unlock_irq(&tree->lock);
1728
1729         bio->bi_private = state;
1730
1731         bio_get(bio);
1732
1733         if (tree->ops && tree->ops->submit_bio_hook)
1734                 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1735                                            mirror_num);
1736         else
1737                 submit_bio(rw, bio);
1738         if (bio_flagged(bio, BIO_EOPNOTSUPP))
1739                 ret = -EOPNOTSUPP;
1740         bio_put(bio);
1741         return ret;
1742 }
1743
1744 static int submit_extent_page(int rw, struct extent_io_tree *tree,
1745                               struct page *page, sector_t sector,
1746                               size_t size, unsigned long offset,
1747                               struct block_device *bdev,
1748                               struct bio **bio_ret,
1749                               unsigned long max_pages,
1750                               bio_end_io_t end_io_func,
1751                               int mirror_num)
1752 {
1753         int ret = 0;
1754         struct bio *bio;
1755         int nr;
1756
1757         if (bio_ret && *bio_ret) {
1758                 bio = *bio_ret;
1759                 if (bio->bi_sector + (bio->bi_size >> 9) != sector ||
1760                     (tree->ops && tree->ops->merge_bio_hook &&
1761                      tree->ops->merge_bio_hook(page, offset, size, bio)) ||
1762                     bio_add_page(bio, page, size, offset) < size) {
1763                         ret = submit_one_bio(rw, bio, mirror_num);
1764                         bio = NULL;
1765                 } else {
1766                         return 0;
1767                 }
1768         }
1769         nr = bio_get_nr_vecs(bdev);
1770         bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1771         if (!bio) {
1772                 printk("failed to allocate bio nr %d\n", nr);
1773         }
1774
1775
1776         bio_add_page(bio, page, size, offset);
1777         bio->bi_end_io = end_io_func;
1778         bio->bi_private = tree;
1779
1780         if (bio_ret) {
1781                 *bio_ret = bio;
1782         } else {
1783                 ret = submit_one_bio(rw, bio, mirror_num);
1784         }
1785
1786         return ret;
1787 }
1788
1789 void set_page_extent_mapped(struct page *page)
1790 {
1791         if (!PagePrivate(page)) {
1792                 SetPagePrivate(page);
1793                 WARN_ON(!page->mapping->a_ops->invalidatepage);
1794                 set_page_private(page, EXTENT_PAGE_PRIVATE);
1795                 page_cache_get(page);
1796         }
1797 }
1798
1799 void set_page_extent_head(struct page *page, unsigned long len)
1800 {
1801         set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1802 }
1803
1804 /*
1805  * basic readpage implementation.  Locked extent state structs are inserted
1806  * into the tree that are removed when the IO is done (by the end_io
1807  * handlers)
1808  */
1809 static int __extent_read_full_page(struct extent_io_tree *tree,
1810                                    struct page *page,
1811                                    get_extent_t *get_extent,
1812                                    struct bio **bio, int mirror_num)
1813 {
1814         struct inode *inode = page->mapping->host;
1815         u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1816         u64 page_end = start + PAGE_CACHE_SIZE - 1;
1817         u64 end;
1818         u64 cur = start;
1819         u64 extent_offset;
1820         u64 last_byte = i_size_read(inode);
1821         u64 block_start;
1822         u64 cur_end;
1823         sector_t sector;
1824         struct extent_map *em;
1825         struct block_device *bdev;
1826         int ret;
1827         int nr = 0;
1828         size_t page_offset = 0;
1829         size_t iosize;
1830         size_t blocksize = inode->i_sb->s_blocksize;
1831
1832         set_page_extent_mapped(page);
1833
1834         end = page_end;
1835         lock_extent(tree, start, end, GFP_NOFS);
1836
1837         while (cur <= end) {
1838                 if (cur >= last_byte) {
1839                         char *userpage;
1840                         iosize = PAGE_CACHE_SIZE - page_offset;
1841                         userpage = kmap_atomic(page, KM_USER0);
1842                         memset(userpage + page_offset, 0, iosize);
1843                         flush_dcache_page(page);
1844                         kunmap_atomic(userpage, KM_USER0);
1845                         set_extent_uptodate(tree, cur, cur + iosize - 1,
1846                                             GFP_NOFS);
1847                         unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1848                         break;
1849                 }
1850                 em = get_extent(inode, page, page_offset, cur,
1851                                 end - cur + 1, 0);
1852                 if (IS_ERR(em) || !em) {
1853                         SetPageError(page);
1854                         unlock_extent(tree, cur, end, GFP_NOFS);
1855                         break;
1856                 }
1857
1858                 extent_offset = cur - em->start;
1859                 BUG_ON(extent_map_end(em) <= cur);
1860                 BUG_ON(end < cur);
1861
1862                 iosize = min(extent_map_end(em) - cur, end - cur + 1);
1863                 cur_end = min(extent_map_end(em) - 1, end);
1864                 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1865                 sector = (em->block_start + extent_offset) >> 9;
1866                 bdev = em->bdev;
1867                 block_start = em->block_start;
1868                 free_extent_map(em);
1869                 em = NULL;
1870
1871                 /* we've found a hole, just zero and go on */
1872                 if (block_start == EXTENT_MAP_HOLE) {
1873                         char *userpage;
1874                         userpage = kmap_atomic(page, KM_USER0);
1875                         memset(userpage + page_offset, 0, iosize);
1876                         flush_dcache_page(page);
1877                         kunmap_atomic(userpage, KM_USER0);
1878
1879                         set_extent_uptodate(tree, cur, cur + iosize - 1,
1880                                             GFP_NOFS);
1881                         unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1882                         cur = cur + iosize;
1883                         page_offset += iosize;
1884                         continue;
1885                 }
1886                 /* the get_extent function already copied into the page */
1887                 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1888                         unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1889                         cur = cur + iosize;
1890                         page_offset += iosize;
1891                         continue;
1892                 }
1893                 /* we have an inline extent but it didn't get marked up
1894                  * to date.  Error out
1895                  */
1896                 if (block_start == EXTENT_MAP_INLINE) {
1897                         SetPageError(page);
1898                         unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1899                         cur = cur + iosize;
1900                         page_offset += iosize;
1901                         continue;
1902                 }
1903
1904                 ret = 0;
1905                 if (tree->ops && tree->ops->readpage_io_hook) {
1906                         ret = tree->ops->readpage_io_hook(page, cur,
1907                                                           cur + iosize - 1);
1908                 }
1909                 if (!ret) {
1910                         unsigned long nr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
1911                         nr -= page->index;
1912                         ret = submit_extent_page(READ, tree, page,
1913                                          sector, iosize, page_offset,
1914                                          bdev, bio, nr,
1915                                          end_bio_extent_readpage, mirror_num);
1916                 }
1917                 if (ret)
1918                         SetPageError(page);
1919                 cur = cur + iosize;
1920                 page_offset += iosize;
1921                 nr++;
1922         }
1923         if (!nr) {
1924                 if (!PageError(page))
1925                         SetPageUptodate(page);
1926                 unlock_page(page);
1927         }
1928         return 0;
1929 }
1930
1931 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
1932                             get_extent_t *get_extent)
1933 {
1934         struct bio *bio = NULL;
1935         int ret;
1936
1937         ret = __extent_read_full_page(tree, page, get_extent, &bio, 0);
1938         if (bio)
1939                 submit_one_bio(READ, bio, 0);
1940         return ret;
1941 }
1942 EXPORT_SYMBOL(extent_read_full_page);
1943
1944 /*
1945  * the writepage semantics are similar to regular writepage.  extent
1946  * records are inserted to lock ranges in the tree, and as dirty areas
1947  * are found, they are marked writeback.  Then the lock bits are removed
1948  * and the end_io handler clears the writeback ranges
1949  */
1950 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
1951                               void *data)
1952 {
1953         struct inode *inode = page->mapping->host;
1954         struct extent_page_data *epd = data;
1955         struct extent_io_tree *tree = epd->tree;
1956         u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1957         u64 delalloc_start;
1958         u64 page_end = start + PAGE_CACHE_SIZE - 1;
1959         u64 end;
1960         u64 cur = start;
1961         u64 extent_offset;
1962         u64 last_byte = i_size_read(inode);
1963         u64 block_start;
1964         u64 iosize;
1965         sector_t sector;
1966         struct extent_map *em;
1967         struct block_device *bdev;
1968         int ret;
1969         int nr = 0;
1970         size_t page_offset = 0;
1971         size_t blocksize;
1972         loff_t i_size = i_size_read(inode);
1973         unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
1974         u64 nr_delalloc;
1975         u64 delalloc_end;
1976
1977         WARN_ON(!PageLocked(page));
1978         if (page->index > end_index) {
1979                 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1980                 unlock_page(page);
1981                 return 0;
1982         }
1983
1984         if (page->index == end_index) {
1985                 char *userpage;
1986
1987                 size_t offset = i_size & (PAGE_CACHE_SIZE - 1);
1988
1989                 userpage = kmap_atomic(page, KM_USER0);
1990                 memset(userpage + offset, 0, PAGE_CACHE_SIZE - offset);
1991                 flush_dcache_page(page);
1992                 kunmap_atomic(userpage, KM_USER0);
1993         }
1994
1995         set_page_extent_mapped(page);
1996
1997         delalloc_start = start;
1998         delalloc_end = 0;
1999         while(delalloc_end < page_end) {
2000                 nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start,
2001                                                        &delalloc_end,
2002                                                        128 * 1024 * 1024);
2003                 if (nr_delalloc == 0) {
2004                         delalloc_start = delalloc_end + 1;
2005                         continue;
2006                 }
2007                 tree->ops->fill_delalloc(inode, delalloc_start,
2008                                          delalloc_end);
2009                 clear_extent_bit(tree, delalloc_start,
2010                                  delalloc_end,
2011                                  EXTENT_LOCKED | EXTENT_DELALLOC,
2012                                  1, 0, GFP_NOFS);
2013                 delalloc_start = delalloc_end + 1;
2014         }
2015         lock_extent(tree, start, page_end, GFP_NOFS);
2016
2017         end = page_end;
2018         if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
2019                 printk("found delalloc bits after lock_extent\n");
2020         }
2021
2022         if (last_byte <= start) {
2023                 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
2024                 goto done;
2025         }
2026
2027         set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2028         blocksize = inode->i_sb->s_blocksize;
2029
2030         while (cur <= end) {
2031                 if (cur >= last_byte) {
2032                         clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
2033                         break;
2034                 }
2035                 em = epd->get_extent(inode, page, page_offset, cur,
2036                                      end - cur + 1, 1);
2037                 if (IS_ERR(em) || !em) {
2038                         SetPageError(page);
2039                         break;
2040                 }
2041
2042                 extent_offset = cur - em->start;
2043                 BUG_ON(extent_map_end(em) <= cur);
2044                 BUG_ON(end < cur);
2045                 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2046                 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2047                 sector = (em->block_start + extent_offset) >> 9;
2048                 bdev = em->bdev;
2049                 block_start = em->block_start;
2050                 free_extent_map(em);
2051                 em = NULL;
2052
2053                 if (block_start == EXTENT_MAP_HOLE ||
2054                     block_start == EXTENT_MAP_INLINE) {
2055                         clear_extent_dirty(tree, cur,
2056                                            cur + iosize - 1, GFP_NOFS);
2057                         cur = cur + iosize;
2058                         page_offset += iosize;
2059                         continue;
2060                 }
2061
2062                 /* leave this out until we have a page_mkwrite call */
2063                 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2064                                    EXTENT_DIRTY, 0)) {
2065                         cur = cur + iosize;
2066                         page_offset += iosize;
2067                         continue;
2068                 }
2069                 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2070                 if (tree->ops && tree->ops->writepage_io_hook) {
2071                         ret = tree->ops->writepage_io_hook(page, cur,
2072                                                 cur + iosize - 1);
2073                 } else {
2074                         ret = 0;
2075                 }
2076                 if (ret)
2077                         SetPageError(page);
2078                 else {
2079                         unsigned long max_nr = end_index + 1;
2080                         set_range_writeback(tree, cur, cur + iosize - 1);
2081                         if (!PageWriteback(page)) {
2082                                 printk("warning page %lu not writeback, "
2083                                        "cur %llu end %llu\n", page->index,
2084                                        (unsigned long long)cur,
2085                                        (unsigned long long)end);
2086                         }
2087
2088                         ret = submit_extent_page(WRITE, tree, page, sector,
2089                                                  iosize, page_offset, bdev,
2090                                                  &epd->bio, max_nr,
2091                                                  end_bio_extent_writepage, 0);
2092                         if (ret)
2093                                 SetPageError(page);
2094                 }
2095                 cur = cur + iosize;
2096                 page_offset += iosize;
2097                 nr++;
2098         }
2099 done:
2100         if (nr == 0) {
2101                 /* make sure the mapping tag for page dirty gets cleared */
2102                 set_page_writeback(page);
2103                 end_page_writeback(page);
2104         }
2105         unlock_extent(tree, start, page_end, GFP_NOFS);
2106         unlock_page(page);
2107         return 0;
2108 }
2109
2110 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,20)
2111 /* Taken directly from 2.6.23 for 2.6.18 back port */
2112 typedef int (*writepage_t)(struct page *page, struct writeback_control *wbc,
2113                                 void *data);
2114
2115 /**
2116  * write_cache_pages - walk the list of dirty pages of the given address space
2117  * and write all of them.
2118  * @mapping: address space structure to write
2119  * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2120  * @writepage: function called for each page
2121  * @data: data passed to writepage function
2122  *
2123  * If a page is already under I/O, write_cache_pages() skips it, even
2124  * if it's dirty.  This is desirable behaviour for memory-cleaning writeback,
2125  * but it is INCORRECT for data-integrity system calls such as fsync().  fsync()
2126  * and msync() need to guarantee that all the data which was dirty at the time
2127  * the call was made get new I/O started against them.  If wbc->sync_mode is
2128  * WB_SYNC_ALL then we were called for data integrity and we must wait for
2129  * existing IO to complete.
2130  */
2131 static int write_cache_pages(struct address_space *mapping,
2132                       struct writeback_control *wbc, writepage_t writepage,
2133                       void *data)
2134 {
2135         struct backing_dev_info *bdi = mapping->backing_dev_info;
2136         int ret = 0;
2137         int done = 0;
2138         struct pagevec pvec;
2139         int nr_pages;
2140         pgoff_t index;
2141         pgoff_t end;            /* Inclusive */
2142         int scanned = 0;
2143         int range_whole = 0;
2144
2145         if (wbc->nonblocking && bdi_write_congested(bdi)) {
2146                 wbc->encountered_congestion = 1;
2147                 return 0;
2148         }
2149
2150         pagevec_init(&pvec, 0);
2151         if (wbc->range_cyclic) {
2152                 index = mapping->writeback_index; /* Start from prev offset */
2153                 end = -1;
2154         } else {
2155                 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2156                 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2157                 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2158                         range_whole = 1;
2159                 scanned = 1;
2160         }
2161 retry:
2162         while (!done && (index <= end) &&
2163                (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2164                                               PAGECACHE_TAG_DIRTY,
2165                                               min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2166                 unsigned i;
2167
2168                 scanned = 1;
2169                 for (i = 0; i < nr_pages; i++) {
2170                         struct page *page = pvec.pages[i];
2171
2172                         /*
2173                          * At this point we hold neither mapping->tree_lock nor
2174                          * lock on the page itself: the page may be truncated or
2175                          * invalidated (changing page->mapping to NULL), or even
2176                          * swizzled back from swapper_space to tmpfs file
2177                          * mapping
2178                          */
2179                         lock_page(page);
2180
2181                         if (unlikely(page->mapping != mapping)) {
2182                                 unlock_page(page);
2183                                 continue;
2184                         }
2185
2186                         if (!wbc->range_cyclic && page->index > end) {
2187                                 done = 1;
2188                                 unlock_page(page);
2189                                 continue;
2190                         }
2191
2192                         if (wbc->sync_mode != WB_SYNC_NONE)
2193                                 wait_on_page_writeback(page);
2194
2195                         if (PageWriteback(page) ||
2196                             !clear_page_dirty_for_io(page)) {
2197                                 unlock_page(page);
2198                                 continue;
2199                         }
2200
2201                         ret = (*writepage)(page, wbc, data);
2202
2203                         if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2204                                 unlock_page(page);
2205                                 ret = 0;
2206                         }
2207                         if (ret || (--(wbc->nr_to_write) <= 0))
2208                                 done = 1;
2209                         if (wbc->nonblocking && bdi_write_congested(bdi)) {
2210                                 wbc->encountered_congestion = 1;
2211                                 done = 1;
2212                         }
2213                 }
2214                 pagevec_release(&pvec);
2215                 cond_resched();
2216         }
2217         if (!scanned && !done) {
2218                 /*
2219                  * We hit the last page and there is more work to be done: wrap
2220                  * back to the start of the file
2221                  */
2222                 scanned = 1;
2223                 index = 0;
2224                 goto retry;
2225         }
2226         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2227                 mapping->writeback_index = index;
2228         return ret;
2229 }
2230 #endif
2231
2232 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2233                           get_extent_t *get_extent,
2234                           struct writeback_control *wbc)
2235 {
2236         int ret;
2237         struct address_space *mapping = page->mapping;
2238         struct extent_page_data epd = {
2239                 .bio = NULL,
2240                 .tree = tree,
2241                 .get_extent = get_extent,
2242         };
2243         struct writeback_control wbc_writepages = {
2244                 .bdi            = wbc->bdi,
2245                 .sync_mode      = WB_SYNC_NONE,
2246                 .older_than_this = NULL,
2247                 .nr_to_write    = 64,
2248                 .range_start    = page_offset(page) + PAGE_CACHE_SIZE,
2249                 .range_end      = (loff_t)-1,
2250         };
2251
2252
2253         ret = __extent_writepage(page, wbc, &epd);
2254
2255         write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd);
2256         if (epd.bio) {
2257                 submit_one_bio(WRITE, epd.bio, 0);
2258         }
2259         return ret;
2260 }
2261 EXPORT_SYMBOL(extent_write_full_page);
2262
2263
2264 int extent_writepages(struct extent_io_tree *tree,
2265                       struct address_space *mapping,
2266                       get_extent_t *get_extent,
2267                       struct writeback_control *wbc)
2268 {
2269         int ret = 0;
2270         struct extent_page_data epd = {
2271                 .bio = NULL,
2272                 .tree = tree,
2273                 .get_extent = get_extent,
2274         };
2275
2276         ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd);
2277         if (epd.bio) {
2278                 submit_one_bio(WRITE, epd.bio, 0);
2279         }
2280         return ret;
2281 }
2282 EXPORT_SYMBOL(extent_writepages);
2283
2284 int extent_readpages(struct extent_io_tree *tree,
2285                      struct address_space *mapping,
2286                      struct list_head *pages, unsigned nr_pages,
2287                      get_extent_t get_extent)
2288 {
2289         struct bio *bio = NULL;
2290         unsigned page_idx;
2291         struct pagevec pvec;
2292
2293         pagevec_init(&pvec, 0);
2294         for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2295                 struct page *page = list_entry(pages->prev, struct page, lru);
2296
2297                 prefetchw(&page->flags);
2298                 list_del(&page->lru);
2299                 /*
2300                  * what we want to do here is call add_to_page_cache_lru,
2301                  * but that isn't exported, so we reproduce it here
2302                  */
2303                 if (!add_to_page_cache(page, mapping,
2304                                         page->index, GFP_KERNEL)) {
2305
2306                         /* open coding of lru_cache_add, also not exported */
2307                         page_cache_get(page);
2308                         if (!pagevec_add(&pvec, page))
2309                                 __pagevec_lru_add(&pvec);
2310                         __extent_read_full_page(tree, page, get_extent,
2311                                                 &bio, 0);
2312                 }
2313                 page_cache_release(page);
2314         }
2315         if (pagevec_count(&pvec))
2316                 __pagevec_lru_add(&pvec);
2317         BUG_ON(!list_empty(pages));
2318         if (bio)
2319                 submit_one_bio(READ, bio, 0);
2320         return 0;
2321 }
2322 EXPORT_SYMBOL(extent_readpages);
2323
2324 /*
2325  * basic invalidatepage code, this waits on any locked or writeback
2326  * ranges corresponding to the page, and then deletes any extent state
2327  * records from the tree
2328  */
2329 int extent_invalidatepage(struct extent_io_tree *tree,
2330                           struct page *page, unsigned long offset)
2331 {
2332         u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2333         u64 end = start + PAGE_CACHE_SIZE - 1;
2334         size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2335
2336         start += (offset + blocksize -1) & ~(blocksize - 1);
2337         if (start > end)
2338                 return 0;
2339
2340         lock_extent(tree, start, end, GFP_NOFS);
2341         wait_on_extent_writeback(tree, start, end);
2342         clear_extent_bit(tree, start, end,
2343                          EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2344                          1, 1, GFP_NOFS);
2345         return 0;
2346 }
2347 EXPORT_SYMBOL(extent_invalidatepage);
2348
2349 /*
2350  * simple commit_write call, set_range_dirty is used to mark both
2351  * the pages and the extent records as dirty
2352  */
2353 int extent_commit_write(struct extent_io_tree *tree,
2354                         struct inode *inode, struct page *page,
2355                         unsigned from, unsigned to)
2356 {
2357         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2358
2359         set_page_extent_mapped(page);
2360         set_page_dirty(page);
2361
2362         if (pos > inode->i_size) {
2363                 i_size_write(inode, pos);
2364                 mark_inode_dirty(inode);
2365         }
2366         return 0;
2367 }
2368 EXPORT_SYMBOL(extent_commit_write);
2369
2370 int extent_prepare_write(struct extent_io_tree *tree,
2371                          struct inode *inode, struct page *page,
2372                          unsigned from, unsigned to, get_extent_t *get_extent)
2373 {
2374         u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2375         u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2376         u64 block_start;
2377         u64 orig_block_start;
2378         u64 block_end;
2379         u64 cur_end;
2380         struct extent_map *em;
2381         unsigned blocksize = 1 << inode->i_blkbits;
2382         size_t page_offset = 0;
2383         size_t block_off_start;
2384         size_t block_off_end;
2385         int err = 0;
2386         int iocount = 0;
2387         int ret = 0;
2388         int isnew;
2389
2390         set_page_extent_mapped(page);
2391
2392         block_start = (page_start + from) & ~((u64)blocksize - 1);
2393         block_end = (page_start + to - 1) | (blocksize - 1);
2394         orig_block_start = block_start;
2395
2396         lock_extent(tree, page_start, page_end, GFP_NOFS);
2397         while(block_start <= block_end) {
2398                 em = get_extent(inode, page, page_offset, block_start,
2399                                 block_end - block_start + 1, 1);
2400                 if (IS_ERR(em) || !em) {
2401                         goto err;
2402                 }
2403                 cur_end = min(block_end, extent_map_end(em) - 1);
2404                 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2405                 block_off_end = block_off_start + blocksize;
2406                 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2407
2408                 if (!PageUptodate(page) && isnew &&
2409                     (block_off_end > to || block_off_start < from)) {
2410                         void *kaddr;
2411
2412                         kaddr = kmap_atomic(page, KM_USER0);
2413                         if (block_off_end > to)
2414                                 memset(kaddr + to, 0, block_off_end - to);
2415                         if (block_off_start < from)
2416                                 memset(kaddr + block_off_start, 0,
2417                                        from - block_off_start);
2418                         flush_dcache_page(page);
2419                         kunmap_atomic(kaddr, KM_USER0);
2420                 }
2421                 if ((em->block_start != EXTENT_MAP_HOLE &&
2422                      em->block_start != EXTENT_MAP_INLINE) &&
2423                     !isnew && !PageUptodate(page) &&
2424                     (block_off_end > to || block_off_start < from) &&
2425                     !test_range_bit(tree, block_start, cur_end,
2426                                     EXTENT_UPTODATE, 1)) {
2427                         u64 sector;
2428                         u64 extent_offset = block_start - em->start;
2429                         size_t iosize;
2430                         sector = (em->block_start + extent_offset) >> 9;
2431                         iosize = (cur_end - block_start + blocksize) &
2432                                 ~((u64)blocksize - 1);
2433                         /*
2434                          * we've already got the extent locked, but we
2435                          * need to split the state such that our end_bio
2436                          * handler can clear the lock.
2437                          */
2438                         set_extent_bit(tree, block_start,
2439                                        block_start + iosize - 1,
2440                                        EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2441                         ret = submit_extent_page(READ, tree, page,
2442                                          sector, iosize, page_offset, em->bdev,
2443                                          NULL, 1,
2444                                          end_bio_extent_preparewrite, 0);
2445                         iocount++;
2446                         block_start = block_start + iosize;
2447                 } else {
2448                         set_extent_uptodate(tree, block_start, cur_end,
2449                                             GFP_NOFS);
2450                         unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2451                         block_start = cur_end + 1;
2452                 }
2453                 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2454                 free_extent_map(em);
2455         }
2456         if (iocount) {
2457                 wait_extent_bit(tree, orig_block_start,
2458                                 block_end, EXTENT_LOCKED);
2459         }
2460         check_page_uptodate(tree, page);
2461 err:
2462         /* FIXME, zero out newly allocated blocks on error */
2463         return err;
2464 }
2465 EXPORT_SYMBOL(extent_prepare_write);
2466
2467 /*
2468  * a helper for releasepage, this tests for areas of the page that
2469  * are locked or under IO and drops the related state bits if it is safe
2470  * to drop the page.
2471  */
2472 int try_release_extent_state(struct extent_map_tree *map,
2473                              struct extent_io_tree *tree, struct page *page,
2474                              gfp_t mask)
2475 {
2476         u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2477         u64 end = start + PAGE_CACHE_SIZE - 1;
2478         int ret = 1;
2479
2480         if (test_range_bit(tree, start, end, EXTENT_IOBITS, 0))
2481                 ret = 0;
2482         else {
2483                 if ((mask & GFP_NOFS) == GFP_NOFS)
2484                         mask = GFP_NOFS;
2485                 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2486                                  1, 1, mask);
2487         }
2488         return ret;
2489 }
2490 EXPORT_SYMBOL(try_release_extent_state);
2491
2492 /*
2493  * a helper for releasepage.  As long as there are no locked extents
2494  * in the range corresponding to the page, both state records and extent
2495  * map records are removed
2496  */
2497 int try_release_extent_mapping(struct extent_map_tree *map,
2498                                struct extent_io_tree *tree, struct page *page,
2499                                gfp_t mask)
2500 {
2501         struct extent_map *em;
2502         u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2503         u64 end = start + PAGE_CACHE_SIZE - 1;
2504
2505         if ((mask & __GFP_WAIT) &&
2506             page->mapping->host->i_size > 16 * 1024 * 1024) {
2507                 u64 len;
2508                 while (start <= end) {
2509                         len = end - start + 1;
2510                         spin_lock(&map->lock);
2511                         em = lookup_extent_mapping(map, start, len);
2512                         if (!em || IS_ERR(em)) {
2513                                 spin_unlock(&map->lock);
2514                                 break;
2515                         }
2516                         if (em->start != start) {
2517                                 spin_unlock(&map->lock);
2518                                 free_extent_map(em);
2519                                 break;
2520                         }
2521                         if (!test_range_bit(tree, em->start,
2522                                             extent_map_end(em) - 1,
2523                                             EXTENT_LOCKED, 0)) {
2524                                 remove_extent_mapping(map, em);
2525                                 /* once for the rb tree */
2526                                 free_extent_map(em);
2527                         }
2528                         start = extent_map_end(em);
2529                         spin_unlock(&map->lock);
2530
2531                         /* once for us */
2532                         free_extent_map(em);
2533                 }
2534         }
2535         return try_release_extent_state(map, tree, page, mask);
2536 }
2537 EXPORT_SYMBOL(try_release_extent_mapping);
2538
2539 sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2540                 get_extent_t *get_extent)
2541 {
2542         struct inode *inode = mapping->host;
2543         u64 start = iblock << inode->i_blkbits;
2544         sector_t sector = 0;
2545         struct extent_map *em;
2546
2547         em = get_extent(inode, NULL, 0, start, (1 << inode->i_blkbits), 0);
2548         if (!em || IS_ERR(em))
2549                 return 0;
2550
2551         if (em->block_start == EXTENT_MAP_INLINE ||
2552             em->block_start == EXTENT_MAP_HOLE)
2553                 goto out;
2554
2555         sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2556 out:
2557         free_extent_map(em);
2558         return sector;
2559 }
2560
2561 static int add_lru(struct extent_io_tree *tree, struct extent_buffer *eb)
2562 {
2563         if (list_empty(&eb->lru)) {
2564                 extent_buffer_get(eb);
2565                 list_add(&eb->lru, &tree->buffer_lru);
2566                 tree->lru_size++;
2567                 if (tree->lru_size >= BUFFER_LRU_MAX) {
2568                         struct extent_buffer *rm;
2569                         rm = list_entry(tree->buffer_lru.prev,
2570                                         struct extent_buffer, lru);
2571                         tree->lru_size--;
2572                         list_del_init(&rm->lru);
2573                         free_extent_buffer(rm);
2574                 }
2575         } else
2576                 list_move(&eb->lru, &tree->buffer_lru);
2577         return 0;
2578 }
2579 static struct extent_buffer *find_lru(struct extent_io_tree *tree,
2580                                       u64 start, unsigned long len)
2581 {
2582         struct list_head *lru = &tree->buffer_lru;
2583         struct list_head *cur = lru->next;
2584         struct extent_buffer *eb;
2585
2586         if (list_empty(lru))
2587                 return NULL;
2588
2589         do {
2590                 eb = list_entry(cur, struct extent_buffer, lru);
2591                 if (eb->start == start && eb->len == len) {
2592                         extent_buffer_get(eb);
2593                         return eb;
2594                 }
2595                 cur = cur->next;
2596         } while (cur != lru);
2597         return NULL;
2598 }
2599
2600 static inline unsigned long num_extent_pages(u64 start, u64 len)
2601 {
2602         return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2603                 (start >> PAGE_CACHE_SHIFT);
2604 }
2605
2606 static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2607                                               unsigned long i)
2608 {
2609         struct page *p;
2610         struct address_space *mapping;
2611
2612         if (i == 0)
2613                 return eb->first_page;
2614         i += eb->start >> PAGE_CACHE_SHIFT;
2615         mapping = eb->first_page->mapping;
2616         read_lock_irq(&mapping->tree_lock);
2617         p = radix_tree_lookup(&mapping->page_tree, i);
2618         read_unlock_irq(&mapping->tree_lock);
2619         return p;
2620 }
2621
2622 int release_extent_buffer_tail_pages(struct extent_buffer *eb)
2623 {
2624         unsigned long num_pages = num_extent_pages(eb->start, eb->len);
2625         struct page *page;
2626         unsigned long i;
2627
2628         if (num_pages == 1)
2629                 return 0;
2630         for (i = 1; i < num_pages; i++) {
2631                 page = extent_buffer_page(eb, i);
2632                 page_cache_release(page);
2633         }
2634         return 0;
2635 }
2636
2637
2638 int invalidate_extent_lru(struct extent_io_tree *tree, u64 start,
2639                           unsigned long len)
2640 {
2641         struct list_head *lru = &tree->buffer_lru;
2642         struct list_head *cur = lru->next;
2643         struct extent_buffer *eb;
2644         int found = 0;
2645
2646         spin_lock(&tree->lru_lock);
2647         if (list_empty(lru))
2648                 goto out;
2649
2650         do {
2651                 eb = list_entry(cur, struct extent_buffer, lru);
2652                 if (eb->start <= start && eb->start + eb->len > start) {
2653                         eb->flags &= ~EXTENT_UPTODATE;
2654                 }
2655                 cur = cur->next;
2656         } while (cur != lru);
2657 out:
2658         spin_unlock(&tree->lru_lock);
2659         return found;
2660 }
2661
2662 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2663                                                    u64 start,
2664                                                    unsigned long len,
2665                                                    gfp_t mask)
2666 {
2667         struct extent_buffer *eb = NULL;
2668         unsigned long flags;
2669
2670         spin_lock(&tree->lru_lock);
2671         eb = find_lru(tree, start, len);
2672         spin_unlock(&tree->lru_lock);
2673         if (eb) {
2674                 return eb;
2675         }
2676
2677         eb = kmem_cache_zalloc(extent_buffer_cache, mask);
2678         INIT_LIST_HEAD(&eb->lru);
2679         eb->start = start;
2680         eb->len = len;
2681         spin_lock_irqsave(&leak_lock, flags);
2682         list_add(&eb->leak_list, &buffers);
2683         spin_unlock_irqrestore(&leak_lock, flags);
2684         atomic_set(&eb->refs, 1);
2685
2686         return eb;
2687 }
2688
2689 static void __free_extent_buffer(struct extent_buffer *eb)
2690 {
2691         unsigned long flags;
2692         spin_lock_irqsave(&leak_lock, flags);
2693         list_del(&eb->leak_list);
2694         spin_unlock_irqrestore(&leak_lock, flags);
2695         kmem_cache_free(extent_buffer_cache, eb);
2696 }
2697
2698 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
2699                                           u64 start, unsigned long len,
2700                                           struct page *page0,
2701                                           gfp_t mask)
2702 {
2703         unsigned long num_pages = num_extent_pages(start, len);
2704         unsigned long i;
2705         unsigned long index = start >> PAGE_CACHE_SHIFT;
2706         struct extent_buffer *eb;
2707         struct page *p;
2708         struct address_space *mapping = tree->mapping;
2709         int uptodate = 1;
2710
2711         eb = __alloc_extent_buffer(tree, start, len, mask);
2712         if (!eb)
2713                 return NULL;
2714
2715         if (eb->flags & EXTENT_BUFFER_FILLED)
2716                 goto lru_add;
2717
2718         if (page0) {
2719                 eb->first_page = page0;
2720                 i = 1;
2721                 index++;
2722                 page_cache_get(page0);
2723                 mark_page_accessed(page0);
2724                 set_page_extent_mapped(page0);
2725                 set_page_extent_head(page0, len);
2726                 uptodate = PageUptodate(page0);
2727         } else {
2728                 i = 0;
2729         }
2730         for (; i < num_pages; i++, index++) {
2731                 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
2732                 if (!p) {
2733                         WARN_ON(1);
2734                         goto fail;
2735                 }
2736                 set_page_extent_mapped(p);
2737                 mark_page_accessed(p);
2738                 if (i == 0) {
2739                         eb->first_page = p;
2740                         set_page_extent_head(p, len);
2741                 } else {
2742                         set_page_private(p, EXTENT_PAGE_PRIVATE);
2743                 }
2744                 if (!PageUptodate(p))
2745                         uptodate = 0;
2746                 unlock_page(p);
2747         }
2748         if (uptodate)
2749                 eb->flags |= EXTENT_UPTODATE;
2750         eb->flags |= EXTENT_BUFFER_FILLED;
2751
2752 lru_add:
2753         spin_lock(&tree->lru_lock);
2754         add_lru(tree, eb);
2755         spin_unlock(&tree->lru_lock);
2756         return eb;
2757
2758 fail:
2759         spin_lock(&tree->lru_lock);
2760         list_del_init(&eb->lru);
2761         spin_unlock(&tree->lru_lock);
2762         if (!atomic_dec_and_test(&eb->refs))
2763                 return NULL;
2764         for (index = 1; index < i; index++) {
2765                 page_cache_release(extent_buffer_page(eb, index));
2766         }
2767         if (i > 0)
2768                 page_cache_release(extent_buffer_page(eb, 0));
2769         __free_extent_buffer(eb);
2770         return NULL;
2771 }
2772 EXPORT_SYMBOL(alloc_extent_buffer);
2773
2774 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
2775                                          u64 start, unsigned long len,
2776                                           gfp_t mask)
2777 {
2778         unsigned long num_pages = num_extent_pages(start, len);
2779         unsigned long i;
2780         unsigned long index = start >> PAGE_CACHE_SHIFT;
2781         struct extent_buffer *eb;
2782         struct page *p;
2783         struct address_space *mapping = tree->mapping;
2784         int uptodate = 1;
2785
2786         eb = __alloc_extent_buffer(tree, start, len, mask);
2787         if (!eb)
2788                 return NULL;
2789
2790         if (eb->flags & EXTENT_BUFFER_FILLED)
2791                 goto lru_add;
2792
2793         for (i = 0; i < num_pages; i++, index++) {
2794                 p = find_lock_page(mapping, index);
2795                 if (!p) {
2796                         goto fail;
2797                 }
2798                 set_page_extent_mapped(p);
2799                 mark_page_accessed(p);
2800
2801                 if (i == 0) {
2802                         eb->first_page = p;
2803                         set_page_extent_head(p, len);
2804                 } else {
2805                         set_page_private(p, EXTENT_PAGE_PRIVATE);
2806                 }
2807
2808                 if (!PageUptodate(p))
2809                         uptodate = 0;
2810                 unlock_page(p);
2811         }
2812         if (uptodate)
2813                 eb->flags |= EXTENT_UPTODATE;
2814         eb->flags |= EXTENT_BUFFER_FILLED;
2815
2816 lru_add:
2817         spin_lock(&tree->lru_lock);
2818         add_lru(tree, eb);
2819         spin_unlock(&tree->lru_lock);
2820         return eb;
2821 fail:
2822         spin_lock(&tree->lru_lock);
2823         list_del_init(&eb->lru);
2824         spin_unlock(&tree->lru_lock);
2825         if (!atomic_dec_and_test(&eb->refs))
2826                 return NULL;
2827         for (index = 1; index < i; index++) {
2828                 page_cache_release(extent_buffer_page(eb, index));
2829         }
2830         if (i > 0)
2831                 page_cache_release(extent_buffer_page(eb, 0));
2832         __free_extent_buffer(eb);
2833         return NULL;
2834 }
2835 EXPORT_SYMBOL(find_extent_buffer);
2836
2837 void free_extent_buffer(struct extent_buffer *eb)
2838 {
2839         unsigned long i;
2840         unsigned long num_pages;
2841
2842         if (!eb)
2843                 return;
2844
2845         if (!atomic_dec_and_test(&eb->refs))
2846                 return;
2847
2848         WARN_ON(!list_empty(&eb->lru));
2849         num_pages = num_extent_pages(eb->start, eb->len);
2850
2851         for (i = 1; i < num_pages; i++) {
2852                 page_cache_release(extent_buffer_page(eb, i));
2853         }
2854         page_cache_release(extent_buffer_page(eb, 0));
2855         __free_extent_buffer(eb);
2856 }
2857 EXPORT_SYMBOL(free_extent_buffer);
2858
2859 int clear_extent_buffer_dirty(struct extent_io_tree *tree,
2860                               struct extent_buffer *eb)
2861 {
2862         int set;
2863         unsigned long i;
2864         unsigned long num_pages;
2865         struct page *page;
2866
2867         u64 start = eb->start;
2868         u64 end = start + eb->len - 1;
2869
2870         set = clear_extent_dirty(tree, start, end, GFP_NOFS);
2871         num_pages = num_extent_pages(eb->start, eb->len);
2872
2873         for (i = 0; i < num_pages; i++) {
2874                 page = extent_buffer_page(eb, i);
2875                 lock_page(page);
2876                 if (i == 0)
2877                         set_page_extent_head(page, eb->len);
2878                 else
2879                         set_page_private(page, EXTENT_PAGE_PRIVATE);
2880
2881                 /*
2882                  * if we're on the last page or the first page and the
2883                  * block isn't aligned on a page boundary, do extra checks
2884                  * to make sure we don't clean page that is partially dirty
2885                  */
2886                 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2887                     ((i == num_pages - 1) &&
2888                      ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
2889                         start = (u64)page->index << PAGE_CACHE_SHIFT;
2890                         end  = start + PAGE_CACHE_SIZE - 1;
2891                         if (test_range_bit(tree, start, end,
2892                                            EXTENT_DIRTY, 0)) {
2893                                 unlock_page(page);
2894                                 continue;
2895                         }
2896                 }
2897                 clear_page_dirty_for_io(page);
2898                 read_lock_irq(&page->mapping->tree_lock);
2899                 if (!PageDirty(page)) {
2900                         radix_tree_tag_clear(&page->mapping->page_tree,
2901                                                 page_index(page),
2902                                                 PAGECACHE_TAG_DIRTY);
2903                 }
2904                 read_unlock_irq(&page->mapping->tree_lock);
2905                 unlock_page(page);
2906         }
2907         return 0;
2908 }
2909 EXPORT_SYMBOL(clear_extent_buffer_dirty);
2910
2911 int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
2912                                     struct extent_buffer *eb)
2913 {
2914         return wait_on_extent_writeback(tree, eb->start,
2915                                         eb->start + eb->len - 1);
2916 }
2917 EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
2918
2919 int set_extent_buffer_dirty(struct extent_io_tree *tree,
2920                              struct extent_buffer *eb)
2921 {
2922         unsigned long i;
2923         unsigned long num_pages;
2924
2925         num_pages = num_extent_pages(eb->start, eb->len);
2926         for (i = 0; i < num_pages; i++) {
2927                 struct page *page = extent_buffer_page(eb, i);
2928                 /* writepage may need to do something special for the
2929                  * first page, we have to make sure page->private is
2930                  * properly set.  releasepage may drop page->private
2931                  * on us if the page isn't already dirty.
2932                  */
2933                 if (i == 0) {
2934                         lock_page(page);
2935                         set_page_extent_head(page, eb->len);
2936                 } else if (PagePrivate(page) &&
2937                            page->private != EXTENT_PAGE_PRIVATE) {
2938                         lock_page(page);
2939                         set_page_extent_mapped(page);
2940                         unlock_page(page);
2941                 }
2942                 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
2943                 if (i == 0)
2944                         unlock_page(page);
2945         }
2946         return set_extent_dirty(tree, eb->start,
2947                                 eb->start + eb->len - 1, GFP_NOFS);
2948 }
2949 EXPORT_SYMBOL(set_extent_buffer_dirty);
2950
2951 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
2952                                 struct extent_buffer *eb)
2953 {
2954         unsigned long i;
2955         struct page *page;
2956         unsigned long num_pages;
2957
2958         num_pages = num_extent_pages(eb->start, eb->len);
2959
2960         set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2961                             GFP_NOFS);
2962         for (i = 0; i < num_pages; i++) {
2963                 page = extent_buffer_page(eb, i);
2964                 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2965                     ((i == num_pages - 1) &&
2966                      ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
2967                         check_page_uptodate(tree, page);
2968                         continue;
2969                 }
2970                 SetPageUptodate(page);
2971         }
2972         return 0;
2973 }
2974 EXPORT_SYMBOL(set_extent_buffer_uptodate);
2975
2976 int extent_range_uptodate(struct extent_io_tree *tree,
2977                           u64 start, u64 end)
2978 {
2979         struct page *page;
2980         int ret;
2981         int pg_uptodate = 1;
2982         int uptodate;
2983         unsigned long index;
2984
2985         ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
2986         if (ret)
2987                 return 1;
2988         while(start <= end) {
2989                 index = start >> PAGE_CACHE_SHIFT;
2990                 page = find_get_page(tree->mapping, index);
2991                 uptodate = PageUptodate(page);
2992                 page_cache_release(page);
2993                 if (!uptodate) {
2994                         pg_uptodate = 0;
2995                         break;
2996                 }
2997                 start += PAGE_CACHE_SIZE;
2998         }
2999         return pg_uptodate;
3000 }
3001
3002 int extent_buffer_uptodate(struct extent_io_tree *tree,
3003                            struct extent_buffer *eb)
3004 {
3005         int ret = 0;
3006         int ret2;
3007         unsigned long num_pages;
3008         unsigned long i;
3009         struct page *page;
3010         int pg_uptodate = 1;
3011
3012         if (eb->flags & EXTENT_UPTODATE)
3013                 ret = 1;
3014
3015         ret2  = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3016                            EXTENT_UPTODATE, 1);
3017
3018         num_pages = num_extent_pages(eb->start, eb->len);
3019         for (i = 0; i < num_pages; i++) {
3020                 page = extent_buffer_page(eb, i);
3021                 if (!PageUptodate(page)) {
3022                         pg_uptodate = 0;
3023                         break;
3024                 }
3025         }
3026         if ((ret || ret2) && !pg_uptodate) {
3027 printk("uptodate error2 eb %Lu ret %d ret2 %d pg_uptodate %d\n", eb->start, ret, ret2, pg_uptodate);
3028                 WARN_ON(1);
3029         }
3030         return (ret || ret2);
3031 }
3032 EXPORT_SYMBOL(extent_buffer_uptodate);
3033
3034 int read_extent_buffer_pages(struct extent_io_tree *tree,
3035                              struct extent_buffer *eb,
3036                              u64 start, int wait,
3037                              get_extent_t *get_extent, int mirror_num)
3038 {
3039         unsigned long i;
3040         unsigned long start_i;
3041         struct page *page;
3042         int err;
3043         int ret = 0;
3044         int locked_pages = 0;
3045         int all_uptodate = 1;
3046         int inc_all_pages = 0;
3047         unsigned long num_pages;
3048         struct bio *bio = NULL;
3049
3050         if (eb->flags & EXTENT_UPTODATE)
3051                 return 0;
3052
3053         if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3054                            EXTENT_UPTODATE, 1)) {
3055                 return 0;
3056         }
3057
3058         if (start) {
3059                 WARN_ON(start < eb->start);
3060                 start_i = (start >> PAGE_CACHE_SHIFT) -
3061                         (eb->start >> PAGE_CACHE_SHIFT);
3062         } else {
3063                 start_i = 0;
3064         }
3065
3066         num_pages = num_extent_pages(eb->start, eb->len);
3067         for (i = start_i; i < num_pages; i++) {
3068                 page = extent_buffer_page(eb, i);
3069                 if (!wait) {
3070                         if (TestSetPageLocked(page))
3071                                 goto unlock_exit;
3072                 } else {
3073                         lock_page(page);
3074                 }
3075                 locked_pages++;
3076                 if (!PageUptodate(page)) {
3077                         all_uptodate = 0;
3078                 }
3079         }
3080         if (all_uptodate) {
3081                 if (start_i == 0)
3082                         eb->flags |= EXTENT_UPTODATE;
3083                 goto unlock_exit;
3084         }
3085
3086         for (i = start_i; i < num_pages; i++) {
3087                 page = extent_buffer_page(eb, i);
3088                 if (inc_all_pages)
3089                         page_cache_get(page);
3090                 if (!PageUptodate(page)) {
3091                         if (start_i == 0)
3092                                 inc_all_pages = 1;
3093                         ClearPageError(page);
3094                         err = __extent_read_full_page(tree, page,
3095                                                       get_extent, &bio,
3096                                                       mirror_num);
3097                         if (err) {
3098                                 ret = err;
3099                         }
3100                 } else {
3101                         unlock_page(page);
3102                 }
3103         }
3104
3105         if (bio)
3106                 submit_one_bio(READ, bio, mirror_num);
3107
3108         if (ret || !wait) {
3109                 return ret;
3110         }
3111         for (i = start_i; i < num_pages; i++) {
3112                 page = extent_buffer_page(eb, i);
3113                 wait_on_page_locked(page);
3114                 if (!PageUptodate(page)) {
3115                         ret = -EIO;
3116                 }
3117         }
3118         if (!ret)
3119                 eb->flags |= EXTENT_UPTODATE;
3120         return ret;
3121
3122 unlock_exit:
3123         i = start_i;
3124         while(locked_pages > 0) {
3125                 page = extent_buffer_page(eb, i);
3126                 i++;
3127                 unlock_page(page);
3128                 locked_pages--;
3129         }
3130         return ret;
3131 }
3132 EXPORT_SYMBOL(read_extent_buffer_pages);
3133
3134 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3135                         unsigned long start,
3136                         unsigned long len)
3137 {
3138         size_t cur;
3139         size_t offset;
3140         struct page *page;
3141         char *kaddr;
3142         char *dst = (char *)dstv;
3143         size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3144         unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3145
3146         WARN_ON(start > eb->len);
3147         WARN_ON(start + len > eb->start + eb->len);
3148
3149         offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3150
3151         while(len > 0) {
3152                 page = extent_buffer_page(eb, i);
3153
3154                 cur = min(len, (PAGE_CACHE_SIZE - offset));
3155                 kaddr = kmap_atomic(page, KM_USER1);
3156                 memcpy(dst, kaddr + offset, cur);
3157                 kunmap_atomic(kaddr, KM_USER1);
3158
3159                 dst += cur;
3160                 len -= cur;
3161                 offset = 0;
3162                 i++;
3163         }
3164 }
3165 EXPORT_SYMBOL(read_extent_buffer);
3166
3167 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3168                                unsigned long min_len, char **token, char **map,
3169                                unsigned long *map_start,
3170                                unsigned long *map_len, int km)
3171 {
3172         size_t offset = start & (PAGE_CACHE_SIZE - 1);
3173         char *kaddr;
3174         struct page *p;
3175         size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3176         unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3177         unsigned long end_i = (start_offset + start + min_len - 1) >>
3178                 PAGE_CACHE_SHIFT;
3179
3180         if (i != end_i)
3181                 return -EINVAL;
3182
3183         if (i == 0) {
3184                 offset = start_offset;
3185                 *map_start = 0;
3186         } else {
3187                 offset = 0;
3188                 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3189         }
3190         if (start + min_len > eb->len) {
3191 printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
3192                 WARN_ON(1);
3193         }
3194
3195         p = extent_buffer_page(eb, i);
3196         kaddr = kmap_atomic(p, km);
3197         *token = kaddr;
3198         *map = kaddr + offset;
3199         *map_len = PAGE_CACHE_SIZE - offset;
3200         return 0;
3201 }
3202 EXPORT_SYMBOL(map_private_extent_buffer);
3203
3204 int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3205                       unsigned long min_len,
3206                       char **token, char **map,
3207                       unsigned long *map_start,
3208                       unsigned long *map_len, int km)
3209 {
3210         int err;
3211         int save = 0;
3212         if (eb->map_token) {
3213                 unmap_extent_buffer(eb, eb->map_token, km);
3214                 eb->map_token = NULL;
3215                 save = 1;
3216         }
3217         err = map_private_extent_buffer(eb, start, min_len, token, map,
3218                                        map_start, map_len, km);
3219         if (!err && save) {
3220                 eb->map_token = *token;
3221                 eb->kaddr = *map;
3222                 eb->map_start = *map_start;
3223                 eb->map_len = *map_len;
3224         }
3225         return err;
3226 }
3227 EXPORT_SYMBOL(map_extent_buffer);
3228
3229 void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3230 {
3231         kunmap_atomic(token, km);
3232 }
3233 EXPORT_SYMBOL(unmap_extent_buffer);
3234
3235 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3236                           unsigned long start,
3237                           unsigned long len)
3238 {
3239         size_t cur;
3240         size_t offset;
3241         struct page *page;
3242         char *kaddr;
3243         char *ptr = (char *)ptrv;
3244         size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3245         unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3246         int ret = 0;
3247
3248         WARN_ON(start > eb->len);
3249         WARN_ON(start + len > eb->start + eb->len);
3250
3251         offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3252
3253         while(len > 0) {
3254                 page = extent_buffer_page(eb, i);
3255
3256                 cur = min(len, (PAGE_CACHE_SIZE - offset));
3257
3258                 kaddr = kmap_atomic(page, KM_USER0);
3259                 ret = memcmp(ptr, kaddr + offset, cur);
3260                 kunmap_atomic(kaddr, KM_USER0);
3261                 if (ret)
3262                         break;
3263
3264                 ptr += cur;
3265                 len -= cur;
3266                 offset = 0;
3267                 i++;
3268         }
3269         return ret;
3270 }
3271 EXPORT_SYMBOL(memcmp_extent_buffer);
3272
3273 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3274                          unsigned long start, unsigned long len)
3275 {
3276         size_t cur;
3277         size_t offset;
3278         struct page *page;
3279         char *kaddr;
3280         char *src = (char *)srcv;
3281         size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3282         unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3283
3284         WARN_ON(start > eb->len);
3285         WARN_ON(start + len > eb->start + eb->len);
3286
3287         offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3288
3289         while(len > 0) {
3290                 page = extent_buffer_page(eb, i);
3291                 WARN_ON(!PageUptodate(page));
3292
3293                 cur = min(len, PAGE_CACHE_SIZE - offset);
3294                 kaddr = kmap_atomic(page, KM_USER1);
3295                 memcpy(kaddr + offset, src, cur);
3296                 kunmap_atomic(kaddr, KM_USER1);
3297
3298                 src += cur;
3299                 len -= cur;
3300                 offset = 0;
3301                 i++;
3302         }
3303 }
3304 EXPORT_SYMBOL(write_extent_buffer);
3305
3306 void memset_extent_buffer(struct extent_buffer *eb, char c,
3307                           unsigned long start, unsigned long len)
3308 {
3309         size_t cur;
3310         size_t offset;
3311         struct page *page;
3312         char *kaddr;
3313         size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3314         unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3315
3316         WARN_ON(start > eb->len);
3317         WARN_ON(start + len > eb->start + eb->len);
3318
3319         offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3320
3321         while(len > 0) {
3322                 page = extent_buffer_page(eb, i);
3323                 WARN_ON(!PageUptodate(page));
3324
3325                 cur = min(len, PAGE_CACHE_SIZE - offset);
3326                 kaddr = kmap_atomic(page, KM_USER0);
3327                 memset(kaddr + offset, c, cur);
3328                 kunmap_atomic(kaddr, KM_USER0);
3329
3330                 len -= cur;
3331                 offset = 0;
3332                 i++;
3333         }
3334 }
3335 EXPORT_SYMBOL(memset_extent_buffer);
3336
3337 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3338                         unsigned long dst_offset, unsigned long src_offset,
3339                         unsigned long len)
3340 {
3341         u64 dst_len = dst->len;
3342         size_t cur;
3343         size_t offset;
3344         struct page *page;
3345         char *kaddr;
3346         size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3347         unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3348
3349         WARN_ON(src->len != dst_len);
3350
3351         offset = (start_offset + dst_offset) &
3352                 ((unsigned long)PAGE_CACHE_SIZE - 1);
3353
3354         while(len > 0) {
3355                 page = extent_buffer_page(dst, i);
3356                 WARN_ON(!PageUptodate(page));
3357
3358                 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3359
3360                 kaddr = kmap_atomic(page, KM_USER0);
3361                 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3362                 kunmap_atomic(kaddr, KM_USER0);
3363
3364                 src_offset += cur;
3365                 len -= cur;
3366                 offset = 0;
3367                 i++;
3368         }
3369 }
3370 EXPORT_SYMBOL(copy_extent_buffer);
3371
3372 static void move_pages(struct page *dst_page, struct page *src_page,
3373                        unsigned long dst_off, unsigned long src_off,
3374                        unsigned long len)
3375 {
3376         char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3377         if (dst_page == src_page) {
3378                 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3379         } else {
3380                 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3381                 char *p = dst_kaddr + dst_off + len;
3382                 char *s = src_kaddr + src_off + len;
3383
3384                 while (len--)
3385                         *--p = *--s;
3386
3387                 kunmap_atomic(src_kaddr, KM_USER1);
3388         }
3389         kunmap_atomic(dst_kaddr, KM_USER0);
3390 }
3391
3392 static void copy_pages(struct page *dst_page, struct page *src_page,
3393                        unsigned long dst_off, unsigned long src_off,
3394                        unsigned long len)
3395 {
3396         char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3397         char *src_kaddr;
3398
3399         if (dst_page != src_page)
3400                 src_kaddr = kmap_atomic(src_page, KM_USER1);
3401         else
3402                 src_kaddr = dst_kaddr;
3403
3404         memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3405         kunmap_atomic(dst_kaddr, KM_USER0);
3406         if (dst_page != src_page)
3407                 kunmap_atomic(src_kaddr, KM_USER1);
3408 }
3409
3410 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3411                            unsigned long src_offset, unsigned long len)
3412 {
3413         size_t cur;
3414         size_t dst_off_in_page;
3415         size_t src_off_in_page;
3416         size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3417         unsigned long dst_i;
3418         unsigned long src_i;
3419
3420         if (src_offset + len > dst->len) {
3421                 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3422                        src_offset, len, dst->len);
3423                 BUG_ON(1);
3424         }
3425         if (dst_offset + len > dst->len) {
3426                 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3427                        dst_offset, len, dst->len);
3428                 BUG_ON(1);
3429         }
3430
3431         while(len > 0) {
3432                 dst_off_in_page = (start_offset + dst_offset) &
3433                         ((unsigned long)PAGE_CACHE_SIZE - 1);
3434                 src_off_in_page = (start_offset + src_offset) &
3435                         ((unsigned long)PAGE_CACHE_SIZE - 1);
3436
3437                 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3438                 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3439
3440                 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3441                                                src_off_in_page));
3442                 cur = min_t(unsigned long, cur,
3443                         (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3444
3445                 copy_pages(extent_buffer_page(dst, dst_i),
3446                            extent_buffer_page(dst, src_i),
3447                            dst_off_in_page, src_off_in_page, cur);
3448
3449                 src_offset += cur;
3450                 dst_offset += cur;
3451                 len -= cur;
3452         }
3453 }
3454 EXPORT_SYMBOL(memcpy_extent_buffer);
3455
3456 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3457                            unsigned long src_offset, unsigned long len)
3458 {
3459         size_t cur;
3460         size_t dst_off_in_page;
3461         size_t src_off_in_page;
3462         unsigned long dst_end = dst_offset + len - 1;
3463         unsigned long src_end = src_offset + len - 1;
3464         size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3465         unsigned long dst_i;
3466         unsigned long src_i;
3467
3468         if (src_offset + len > dst->len) {
3469                 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3470                        src_offset, len, dst->len);
3471                 BUG_ON(1);
3472         }
3473         if (dst_offset + len > dst->len) {
3474                 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3475                        dst_offset, len, dst->len);
3476                 BUG_ON(1);
3477         }
3478         if (dst_offset < src_offset) {
3479                 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3480                 return;
3481         }
3482         while(len > 0) {
3483                 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3484                 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3485
3486                 dst_off_in_page = (start_offset + dst_end) &
3487                         ((unsigned long)PAGE_CACHE_SIZE - 1);
3488                 src_off_in_page = (start_offset + src_end) &
3489                         ((unsigned long)PAGE_CACHE_SIZE - 1);
3490
3491                 cur = min_t(unsigned long, len, src_off_in_page + 1);
3492                 cur = min(cur, dst_off_in_page + 1);
3493                 move_pages(extent_buffer_page(dst, dst_i),
3494                            extent_buffer_page(dst, src_i),
3495                            dst_off_in_page - cur + 1,
3496                            src_off_in_page - cur + 1, cur);
3497
3498                 dst_end -= cur;
3499                 src_end -= cur;
3500                 len -= cur;
3501         }
3502 }
3503 EXPORT_SYMBOL(memmove_extent_buffer);