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