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