]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/btrfs/extent_io.c
btrfs: simplify compressed/inline check in __extent_writepage_io()
[linux.git] / fs / btrfs / extent_io.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/bitops.h>
4 #include <linux/slab.h>
5 #include <linux/bio.h>
6 #include <linux/mm.h>
7 #include <linux/pagemap.h>
8 #include <linux/page-flags.h>
9 #include <linux/spinlock.h>
10 #include <linux/blkdev.h>
11 #include <linux/swap.h>
12 #include <linux/writeback.h>
13 #include <linux/pagevec.h>
14 #include <linux/prefetch.h>
15 #include <linux/cleancache.h>
16 #include "extent_io.h"
17 #include "extent-io-tree.h"
18 #include "extent_map.h"
19 #include "ctree.h"
20 #include "btrfs_inode.h"
21 #include "volumes.h"
22 #include "check-integrity.h"
23 #include "locking.h"
24 #include "rcu-string.h"
25 #include "backref.h"
26 #include "disk-io.h"
27
28 static struct kmem_cache *extent_state_cache;
29 static struct kmem_cache *extent_buffer_cache;
30 static struct bio_set btrfs_bioset;
31
32 static inline bool extent_state_in_tree(const struct extent_state *state)
33 {
34         return !RB_EMPTY_NODE(&state->rb_node);
35 }
36
37 #ifdef CONFIG_BTRFS_DEBUG
38 static LIST_HEAD(buffers);
39 static LIST_HEAD(states);
40
41 static DEFINE_SPINLOCK(leak_lock);
42
43 static inline
44 void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
45 {
46         unsigned long flags;
47
48         spin_lock_irqsave(&leak_lock, flags);
49         list_add(new, head);
50         spin_unlock_irqrestore(&leak_lock, flags);
51 }
52
53 static inline
54 void btrfs_leak_debug_del(struct list_head *entry)
55 {
56         unsigned long flags;
57
58         spin_lock_irqsave(&leak_lock, flags);
59         list_del(entry);
60         spin_unlock_irqrestore(&leak_lock, flags);
61 }
62
63 static inline void btrfs_extent_buffer_leak_debug_check(void)
64 {
65         struct extent_buffer *eb;
66
67         while (!list_empty(&buffers)) {
68                 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
69                 pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n",
70                        eb->start, eb->len, atomic_read(&eb->refs), eb->bflags);
71                 list_del(&eb->leak_list);
72                 kmem_cache_free(extent_buffer_cache, eb);
73         }
74 }
75
76 static inline void btrfs_extent_state_leak_debug_check(void)
77 {
78         struct extent_state *state;
79
80         while (!list_empty(&states)) {
81                 state = list_entry(states.next, struct extent_state, leak_list);
82                 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
83                        state->start, state->end, state->state,
84                        extent_state_in_tree(state),
85                        refcount_read(&state->refs));
86                 list_del(&state->leak_list);
87                 kmem_cache_free(extent_state_cache, state);
88         }
89 }
90
91 #define btrfs_debug_check_extent_io_range(tree, start, end)             \
92         __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
93 static inline void __btrfs_debug_check_extent_io_range(const char *caller,
94                 struct extent_io_tree *tree, u64 start, u64 end)
95 {
96         struct inode *inode = tree->private_data;
97         u64 isize;
98
99         if (!inode || !is_data_inode(inode))
100                 return;
101
102         isize = i_size_read(inode);
103         if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
104                 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
105                     "%s: ino %llu isize %llu odd range [%llu,%llu]",
106                         caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
107         }
108 }
109 #else
110 #define btrfs_leak_debug_add(new, head) do {} while (0)
111 #define btrfs_leak_debug_del(entry)     do {} while (0)
112 #define btrfs_extent_buffer_leak_debug_check()  do {} while (0)
113 #define btrfs_extent_state_leak_debug_check()   do {} while (0)
114 #define btrfs_debug_check_extent_io_range(c, s, e)      do {} while (0)
115 #endif
116
117 struct tree_entry {
118         u64 start;
119         u64 end;
120         struct rb_node rb_node;
121 };
122
123 struct extent_page_data {
124         struct bio *bio;
125         struct extent_io_tree *tree;
126         /* tells writepage not to lock the state bits for this range
127          * it still does the unlocking
128          */
129         unsigned int extent_locked:1;
130
131         /* tells the submit_bio code to use REQ_SYNC */
132         unsigned int sync_io:1;
133 };
134
135 static int add_extent_changeset(struct extent_state *state, unsigned bits,
136                                  struct extent_changeset *changeset,
137                                  int set)
138 {
139         int ret;
140
141         if (!changeset)
142                 return 0;
143         if (set && (state->state & bits) == bits)
144                 return 0;
145         if (!set && (state->state & bits) == 0)
146                 return 0;
147         changeset->bytes_changed += state->end - state->start + 1;
148         ret = ulist_add(&changeset->range_changed, state->start, state->end,
149                         GFP_ATOMIC);
150         return ret;
151 }
152
153 static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
154                                        unsigned long bio_flags)
155 {
156         blk_status_t ret = 0;
157         struct extent_io_tree *tree = bio->bi_private;
158
159         bio->bi_private = NULL;
160
161         if (tree->ops)
162                 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
163                                                  mirror_num, bio_flags);
164         else
165                 btrfsic_submit_bio(bio);
166
167         return blk_status_to_errno(ret);
168 }
169
170 /* Cleanup unsubmitted bios */
171 static void end_write_bio(struct extent_page_data *epd, int ret)
172 {
173         if (epd->bio) {
174                 epd->bio->bi_status = errno_to_blk_status(ret);
175                 bio_endio(epd->bio);
176                 epd->bio = NULL;
177         }
178 }
179
180 /*
181  * Submit bio from extent page data via submit_one_bio
182  *
183  * Return 0 if everything is OK.
184  * Return <0 for error.
185  */
186 static int __must_check flush_write_bio(struct extent_page_data *epd)
187 {
188         int ret = 0;
189
190         if (epd->bio) {
191                 ret = submit_one_bio(epd->bio, 0, 0);
192                 /*
193                  * Clean up of epd->bio is handled by its endio function.
194                  * And endio is either triggered by successful bio execution
195                  * or the error handler of submit bio hook.
196                  * So at this point, no matter what happened, we don't need
197                  * to clean up epd->bio.
198                  */
199                 epd->bio = NULL;
200         }
201         return ret;
202 }
203
204 int __init extent_state_cache_init(void)
205 {
206         extent_state_cache = kmem_cache_create("btrfs_extent_state",
207                         sizeof(struct extent_state), 0,
208                         SLAB_MEM_SPREAD, NULL);
209         if (!extent_state_cache)
210                 return -ENOMEM;
211         return 0;
212 }
213
214 int __init extent_io_init(void)
215 {
216         extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
217                         sizeof(struct extent_buffer), 0,
218                         SLAB_MEM_SPREAD, NULL);
219         if (!extent_buffer_cache)
220                 return -ENOMEM;
221
222         if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
223                         offsetof(struct btrfs_io_bio, bio),
224                         BIOSET_NEED_BVECS))
225                 goto free_buffer_cache;
226
227         if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
228                 goto free_bioset;
229
230         return 0;
231
232 free_bioset:
233         bioset_exit(&btrfs_bioset);
234
235 free_buffer_cache:
236         kmem_cache_destroy(extent_buffer_cache);
237         extent_buffer_cache = NULL;
238         return -ENOMEM;
239 }
240
241 void __cold extent_state_cache_exit(void)
242 {
243         btrfs_extent_state_leak_debug_check();
244         kmem_cache_destroy(extent_state_cache);
245 }
246
247 void __cold extent_io_exit(void)
248 {
249         btrfs_extent_buffer_leak_debug_check();
250
251         /*
252          * Make sure all delayed rcu free are flushed before we
253          * destroy caches.
254          */
255         rcu_barrier();
256         kmem_cache_destroy(extent_buffer_cache);
257         bioset_exit(&btrfs_bioset);
258 }
259
260 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
261                          struct extent_io_tree *tree, unsigned int owner,
262                          void *private_data)
263 {
264         tree->fs_info = fs_info;
265         tree->state = RB_ROOT;
266         tree->ops = NULL;
267         tree->dirty_bytes = 0;
268         spin_lock_init(&tree->lock);
269         tree->private_data = private_data;
270         tree->owner = owner;
271 }
272
273 void extent_io_tree_release(struct extent_io_tree *tree)
274 {
275         spin_lock(&tree->lock);
276         /*
277          * Do a single barrier for the waitqueue_active check here, the state
278          * of the waitqueue should not change once extent_io_tree_release is
279          * called.
280          */
281         smp_mb();
282         while (!RB_EMPTY_ROOT(&tree->state)) {
283                 struct rb_node *node;
284                 struct extent_state *state;
285
286                 node = rb_first(&tree->state);
287                 state = rb_entry(node, struct extent_state, rb_node);
288                 rb_erase(&state->rb_node, &tree->state);
289                 RB_CLEAR_NODE(&state->rb_node);
290                 /*
291                  * btree io trees aren't supposed to have tasks waiting for
292                  * changes in the flags of extent states ever.
293                  */
294                 ASSERT(!waitqueue_active(&state->wq));
295                 free_extent_state(state);
296
297                 cond_resched_lock(&tree->lock);
298         }
299         spin_unlock(&tree->lock);
300 }
301
302 static struct extent_state *alloc_extent_state(gfp_t mask)
303 {
304         struct extent_state *state;
305
306         /*
307          * The given mask might be not appropriate for the slab allocator,
308          * drop the unsupported bits
309          */
310         mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
311         state = kmem_cache_alloc(extent_state_cache, mask);
312         if (!state)
313                 return state;
314         state->state = 0;
315         state->failrec = NULL;
316         RB_CLEAR_NODE(&state->rb_node);
317         btrfs_leak_debug_add(&state->leak_list, &states);
318         refcount_set(&state->refs, 1);
319         init_waitqueue_head(&state->wq);
320         trace_alloc_extent_state(state, mask, _RET_IP_);
321         return state;
322 }
323
324 void free_extent_state(struct extent_state *state)
325 {
326         if (!state)
327                 return;
328         if (refcount_dec_and_test(&state->refs)) {
329                 WARN_ON(extent_state_in_tree(state));
330                 btrfs_leak_debug_del(&state->leak_list);
331                 trace_free_extent_state(state, _RET_IP_);
332                 kmem_cache_free(extent_state_cache, state);
333         }
334 }
335
336 static struct rb_node *tree_insert(struct rb_root *root,
337                                    struct rb_node *search_start,
338                                    u64 offset,
339                                    struct rb_node *node,
340                                    struct rb_node ***p_in,
341                                    struct rb_node **parent_in)
342 {
343         struct rb_node **p;
344         struct rb_node *parent = NULL;
345         struct tree_entry *entry;
346
347         if (p_in && parent_in) {
348                 p = *p_in;
349                 parent = *parent_in;
350                 goto do_insert;
351         }
352
353         p = search_start ? &search_start : &root->rb_node;
354         while (*p) {
355                 parent = *p;
356                 entry = rb_entry(parent, struct tree_entry, rb_node);
357
358                 if (offset < entry->start)
359                         p = &(*p)->rb_left;
360                 else if (offset > entry->end)
361                         p = &(*p)->rb_right;
362                 else
363                         return parent;
364         }
365
366 do_insert:
367         rb_link_node(node, parent, p);
368         rb_insert_color(node, root);
369         return NULL;
370 }
371
372 /**
373  * __etree_search - searche @tree for an entry that contains @offset. Such
374  * entry would have entry->start <= offset && entry->end >= offset.
375  *
376  * @tree - the tree to search
377  * @offset - offset that should fall within an entry in @tree
378  * @next_ret - pointer to the first entry whose range ends after @offset
379  * @prev - pointer to the first entry whose range begins before @offset
380  * @p_ret - pointer where new node should be anchored (used when inserting an
381  *          entry in the tree)
382  * @parent_ret - points to entry which would have been the parent of the entry,
383  *               containing @offset
384  *
385  * This function returns a pointer to the entry that contains @offset byte
386  * address. If no such entry exists, then NULL is returned and the other
387  * pointer arguments to the function are filled, otherwise the found entry is
388  * returned and other pointers are left untouched.
389  */
390 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
391                                       struct rb_node **next_ret,
392                                       struct rb_node **prev_ret,
393                                       struct rb_node ***p_ret,
394                                       struct rb_node **parent_ret)
395 {
396         struct rb_root *root = &tree->state;
397         struct rb_node **n = &root->rb_node;
398         struct rb_node *prev = NULL;
399         struct rb_node *orig_prev = NULL;
400         struct tree_entry *entry;
401         struct tree_entry *prev_entry = NULL;
402
403         while (*n) {
404                 prev = *n;
405                 entry = rb_entry(prev, struct tree_entry, rb_node);
406                 prev_entry = entry;
407
408                 if (offset < entry->start)
409                         n = &(*n)->rb_left;
410                 else if (offset > entry->end)
411                         n = &(*n)->rb_right;
412                 else
413                         return *n;
414         }
415
416         if (p_ret)
417                 *p_ret = n;
418         if (parent_ret)
419                 *parent_ret = prev;
420
421         if (next_ret) {
422                 orig_prev = prev;
423                 while (prev && offset > prev_entry->end) {
424                         prev = rb_next(prev);
425                         prev_entry = rb_entry(prev, struct tree_entry, rb_node);
426                 }
427                 *next_ret = prev;
428                 prev = orig_prev;
429         }
430
431         if (prev_ret) {
432                 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
433                 while (prev && offset < prev_entry->start) {
434                         prev = rb_prev(prev);
435                         prev_entry = rb_entry(prev, struct tree_entry, rb_node);
436                 }
437                 *prev_ret = prev;
438         }
439         return NULL;
440 }
441
442 static inline struct rb_node *
443 tree_search_for_insert(struct extent_io_tree *tree,
444                        u64 offset,
445                        struct rb_node ***p_ret,
446                        struct rb_node **parent_ret)
447 {
448         struct rb_node *next= NULL;
449         struct rb_node *ret;
450
451         ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
452         if (!ret)
453                 return next;
454         return ret;
455 }
456
457 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
458                                           u64 offset)
459 {
460         return tree_search_for_insert(tree, offset, NULL, NULL);
461 }
462
463 /*
464  * utility function to look for merge candidates inside a given range.
465  * Any extents with matching state are merged together into a single
466  * extent in the tree.  Extents with EXTENT_IO in their state field
467  * are not merged because the end_io handlers need to be able to do
468  * operations on them without sleeping (or doing allocations/splits).
469  *
470  * This should be called with the tree lock held.
471  */
472 static void merge_state(struct extent_io_tree *tree,
473                         struct extent_state *state)
474 {
475         struct extent_state *other;
476         struct rb_node *other_node;
477
478         if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
479                 return;
480
481         other_node = rb_prev(&state->rb_node);
482         if (other_node) {
483                 other = rb_entry(other_node, struct extent_state, rb_node);
484                 if (other->end == state->start - 1 &&
485                     other->state == state->state) {
486                         if (tree->private_data &&
487                             is_data_inode(tree->private_data))
488                                 btrfs_merge_delalloc_extent(tree->private_data,
489                                                             state, other);
490                         state->start = other->start;
491                         rb_erase(&other->rb_node, &tree->state);
492                         RB_CLEAR_NODE(&other->rb_node);
493                         free_extent_state(other);
494                 }
495         }
496         other_node = rb_next(&state->rb_node);
497         if (other_node) {
498                 other = rb_entry(other_node, struct extent_state, rb_node);
499                 if (other->start == state->end + 1 &&
500                     other->state == state->state) {
501                         if (tree->private_data &&
502                             is_data_inode(tree->private_data))
503                                 btrfs_merge_delalloc_extent(tree->private_data,
504                                                             state, other);
505                         state->end = other->end;
506                         rb_erase(&other->rb_node, &tree->state);
507                         RB_CLEAR_NODE(&other->rb_node);
508                         free_extent_state(other);
509                 }
510         }
511 }
512
513 static void set_state_bits(struct extent_io_tree *tree,
514                            struct extent_state *state, unsigned *bits,
515                            struct extent_changeset *changeset);
516
517 /*
518  * insert an extent_state struct into the tree.  'bits' are set on the
519  * struct before it is inserted.
520  *
521  * This may return -EEXIST if the extent is already there, in which case the
522  * state struct is freed.
523  *
524  * The tree lock is not taken internally.  This is a utility function and
525  * probably isn't what you want to call (see set/clear_extent_bit).
526  */
527 static int insert_state(struct extent_io_tree *tree,
528                         struct extent_state *state, u64 start, u64 end,
529                         struct rb_node ***p,
530                         struct rb_node **parent,
531                         unsigned *bits, struct extent_changeset *changeset)
532 {
533         struct rb_node *node;
534
535         if (end < start) {
536                 btrfs_err(tree->fs_info,
537                         "insert state: end < start %llu %llu", end, start);
538                 WARN_ON(1);
539         }
540         state->start = start;
541         state->end = end;
542
543         set_state_bits(tree, state, bits, changeset);
544
545         node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
546         if (node) {
547                 struct extent_state *found;
548                 found = rb_entry(node, struct extent_state, rb_node);
549                 btrfs_err(tree->fs_info,
550                        "found node %llu %llu on insert of %llu %llu",
551                        found->start, found->end, start, end);
552                 return -EEXIST;
553         }
554         merge_state(tree, state);
555         return 0;
556 }
557
558 /*
559  * split a given extent state struct in two, inserting the preallocated
560  * struct 'prealloc' as the newly created second half.  'split' indicates an
561  * offset inside 'orig' where it should be split.
562  *
563  * Before calling,
564  * the tree has 'orig' at [orig->start, orig->end].  After calling, there
565  * are two extent state structs in the tree:
566  * prealloc: [orig->start, split - 1]
567  * orig: [ split, orig->end ]
568  *
569  * The tree locks are not taken by this function. They need to be held
570  * by the caller.
571  */
572 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
573                        struct extent_state *prealloc, u64 split)
574 {
575         struct rb_node *node;
576
577         if (tree->private_data && is_data_inode(tree->private_data))
578                 btrfs_split_delalloc_extent(tree->private_data, orig, split);
579
580         prealloc->start = orig->start;
581         prealloc->end = split - 1;
582         prealloc->state = orig->state;
583         orig->start = split;
584
585         node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
586                            &prealloc->rb_node, NULL, NULL);
587         if (node) {
588                 free_extent_state(prealloc);
589                 return -EEXIST;
590         }
591         return 0;
592 }
593
594 static struct extent_state *next_state(struct extent_state *state)
595 {
596         struct rb_node *next = rb_next(&state->rb_node);
597         if (next)
598                 return rb_entry(next, struct extent_state, rb_node);
599         else
600                 return NULL;
601 }
602
603 /*
604  * utility function to clear some bits in an extent state struct.
605  * it will optionally wake up anyone waiting on this state (wake == 1).
606  *
607  * If no bits are set on the state struct after clearing things, the
608  * struct is freed and removed from the tree
609  */
610 static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
611                                             struct extent_state *state,
612                                             unsigned *bits, int wake,
613                                             struct extent_changeset *changeset)
614 {
615         struct extent_state *next;
616         unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
617         int ret;
618
619         if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
620                 u64 range = state->end - state->start + 1;
621                 WARN_ON(range > tree->dirty_bytes);
622                 tree->dirty_bytes -= range;
623         }
624
625         if (tree->private_data && is_data_inode(tree->private_data))
626                 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
627
628         ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
629         BUG_ON(ret < 0);
630         state->state &= ~bits_to_clear;
631         if (wake)
632                 wake_up(&state->wq);
633         if (state->state == 0) {
634                 next = next_state(state);
635                 if (extent_state_in_tree(state)) {
636                         rb_erase(&state->rb_node, &tree->state);
637                         RB_CLEAR_NODE(&state->rb_node);
638                         free_extent_state(state);
639                 } else {
640                         WARN_ON(1);
641                 }
642         } else {
643                 merge_state(tree, state);
644                 next = next_state(state);
645         }
646         return next;
647 }
648
649 static struct extent_state *
650 alloc_extent_state_atomic(struct extent_state *prealloc)
651 {
652         if (!prealloc)
653                 prealloc = alloc_extent_state(GFP_ATOMIC);
654
655         return prealloc;
656 }
657
658 static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
659 {
660         struct inode *inode = tree->private_data;
661
662         btrfs_panic(btrfs_sb(inode->i_sb), err,
663         "locking error: extent tree was modified by another thread while locked");
664 }
665
666 /*
667  * clear some bits on a range in the tree.  This may require splitting
668  * or inserting elements in the tree, so the gfp mask is used to
669  * indicate which allocations or sleeping are allowed.
670  *
671  * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
672  * the given range from the tree regardless of state (ie for truncate).
673  *
674  * the range [start, end] is inclusive.
675  *
676  * This takes the tree lock, and returns 0 on success and < 0 on error.
677  */
678 int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
679                               unsigned bits, int wake, int delete,
680                               struct extent_state **cached_state,
681                               gfp_t mask, struct extent_changeset *changeset)
682 {
683         struct extent_state *state;
684         struct extent_state *cached;
685         struct extent_state *prealloc = NULL;
686         struct rb_node *node;
687         u64 last_end;
688         int err;
689         int clear = 0;
690
691         btrfs_debug_check_extent_io_range(tree, start, end);
692         trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
693
694         if (bits & EXTENT_DELALLOC)
695                 bits |= EXTENT_NORESERVE;
696
697         if (delete)
698                 bits |= ~EXTENT_CTLBITS;
699
700         if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
701                 clear = 1;
702 again:
703         if (!prealloc && gfpflags_allow_blocking(mask)) {
704                 /*
705                  * Don't care for allocation failure here because we might end
706                  * up not needing the pre-allocated extent state at all, which
707                  * is the case if we only have in the tree extent states that
708                  * cover our input range and don't cover too any other range.
709                  * If we end up needing a new extent state we allocate it later.
710                  */
711                 prealloc = alloc_extent_state(mask);
712         }
713
714         spin_lock(&tree->lock);
715         if (cached_state) {
716                 cached = *cached_state;
717
718                 if (clear) {
719                         *cached_state = NULL;
720                         cached_state = NULL;
721                 }
722
723                 if (cached && extent_state_in_tree(cached) &&
724                     cached->start <= start && cached->end > start) {
725                         if (clear)
726                                 refcount_dec(&cached->refs);
727                         state = cached;
728                         goto hit_next;
729                 }
730                 if (clear)
731                         free_extent_state(cached);
732         }
733         /*
734          * this search will find the extents that end after
735          * our range starts
736          */
737         node = tree_search(tree, start);
738         if (!node)
739                 goto out;
740         state = rb_entry(node, struct extent_state, rb_node);
741 hit_next:
742         if (state->start > end)
743                 goto out;
744         WARN_ON(state->end < start);
745         last_end = state->end;
746
747         /* the state doesn't have the wanted bits, go ahead */
748         if (!(state->state & bits)) {
749                 state = next_state(state);
750                 goto next;
751         }
752
753         /*
754          *     | ---- desired range ---- |
755          *  | state | or
756          *  | ------------- state -------------- |
757          *
758          * We need to split the extent we found, and may flip
759          * bits on second half.
760          *
761          * If the extent we found extends past our range, we
762          * just split and search again.  It'll get split again
763          * the next time though.
764          *
765          * If the extent we found is inside our range, we clear
766          * the desired bit on it.
767          */
768
769         if (state->start < start) {
770                 prealloc = alloc_extent_state_atomic(prealloc);
771                 BUG_ON(!prealloc);
772                 err = split_state(tree, state, prealloc, start);
773                 if (err)
774                         extent_io_tree_panic(tree, err);
775
776                 prealloc = NULL;
777                 if (err)
778                         goto out;
779                 if (state->end <= end) {
780                         state = clear_state_bit(tree, state, &bits, wake,
781                                                 changeset);
782                         goto next;
783                 }
784                 goto search_again;
785         }
786         /*
787          * | ---- desired range ---- |
788          *                        | state |
789          * We need to split the extent, and clear the bit
790          * on the first half
791          */
792         if (state->start <= end && state->end > end) {
793                 prealloc = alloc_extent_state_atomic(prealloc);
794                 BUG_ON(!prealloc);
795                 err = split_state(tree, state, prealloc, end + 1);
796                 if (err)
797                         extent_io_tree_panic(tree, err);
798
799                 if (wake)
800                         wake_up(&state->wq);
801
802                 clear_state_bit(tree, prealloc, &bits, wake, changeset);
803
804                 prealloc = NULL;
805                 goto out;
806         }
807
808         state = clear_state_bit(tree, state, &bits, wake, changeset);
809 next:
810         if (last_end == (u64)-1)
811                 goto out;
812         start = last_end + 1;
813         if (start <= end && state && !need_resched())
814                 goto hit_next;
815
816 search_again:
817         if (start > end)
818                 goto out;
819         spin_unlock(&tree->lock);
820         if (gfpflags_allow_blocking(mask))
821                 cond_resched();
822         goto again;
823
824 out:
825         spin_unlock(&tree->lock);
826         if (prealloc)
827                 free_extent_state(prealloc);
828
829         return 0;
830
831 }
832
833 static void wait_on_state(struct extent_io_tree *tree,
834                           struct extent_state *state)
835                 __releases(tree->lock)
836                 __acquires(tree->lock)
837 {
838         DEFINE_WAIT(wait);
839         prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
840         spin_unlock(&tree->lock);
841         schedule();
842         spin_lock(&tree->lock);
843         finish_wait(&state->wq, &wait);
844 }
845
846 /*
847  * waits for one or more bits to clear on a range in the state tree.
848  * The range [start, end] is inclusive.
849  * The tree lock is taken by this function
850  */
851 static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
852                             unsigned long bits)
853 {
854         struct extent_state *state;
855         struct rb_node *node;
856
857         btrfs_debug_check_extent_io_range(tree, start, end);
858
859         spin_lock(&tree->lock);
860 again:
861         while (1) {
862                 /*
863                  * this search will find all the extents that end after
864                  * our range starts
865                  */
866                 node = tree_search(tree, start);
867 process_node:
868                 if (!node)
869                         break;
870
871                 state = rb_entry(node, struct extent_state, rb_node);
872
873                 if (state->start > end)
874                         goto out;
875
876                 if (state->state & bits) {
877                         start = state->start;
878                         refcount_inc(&state->refs);
879                         wait_on_state(tree, state);
880                         free_extent_state(state);
881                         goto again;
882                 }
883                 start = state->end + 1;
884
885                 if (start > end)
886                         break;
887
888                 if (!cond_resched_lock(&tree->lock)) {
889                         node = rb_next(node);
890                         goto process_node;
891                 }
892         }
893 out:
894         spin_unlock(&tree->lock);
895 }
896
897 static void set_state_bits(struct extent_io_tree *tree,
898                            struct extent_state *state,
899                            unsigned *bits, struct extent_changeset *changeset)
900 {
901         unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
902         int ret;
903
904         if (tree->private_data && is_data_inode(tree->private_data))
905                 btrfs_set_delalloc_extent(tree->private_data, state, bits);
906
907         if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
908                 u64 range = state->end - state->start + 1;
909                 tree->dirty_bytes += range;
910         }
911         ret = add_extent_changeset(state, bits_to_set, changeset, 1);
912         BUG_ON(ret < 0);
913         state->state |= bits_to_set;
914 }
915
916 static void cache_state_if_flags(struct extent_state *state,
917                                  struct extent_state **cached_ptr,
918                                  unsigned flags)
919 {
920         if (cached_ptr && !(*cached_ptr)) {
921                 if (!flags || (state->state & flags)) {
922                         *cached_ptr = state;
923                         refcount_inc(&state->refs);
924                 }
925         }
926 }
927
928 static void cache_state(struct extent_state *state,
929                         struct extent_state **cached_ptr)
930 {
931         return cache_state_if_flags(state, cached_ptr,
932                                     EXTENT_LOCKED | EXTENT_BOUNDARY);
933 }
934
935 /*
936  * set some bits on a range in the tree.  This may require allocations or
937  * sleeping, so the gfp mask is used to indicate what is allowed.
938  *
939  * If any of the exclusive bits are set, this will fail with -EEXIST if some
940  * part of the range already has the desired bits set.  The start of the
941  * existing range is returned in failed_start in this case.
942  *
943  * [start, end] is inclusive This takes the tree lock.
944  */
945
946 static int __must_check
947 __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
948                  unsigned bits, unsigned exclusive_bits,
949                  u64 *failed_start, struct extent_state **cached_state,
950                  gfp_t mask, struct extent_changeset *changeset)
951 {
952         struct extent_state *state;
953         struct extent_state *prealloc = NULL;
954         struct rb_node *node;
955         struct rb_node **p;
956         struct rb_node *parent;
957         int err = 0;
958         u64 last_start;
959         u64 last_end;
960
961         btrfs_debug_check_extent_io_range(tree, start, end);
962         trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
963
964 again:
965         if (!prealloc && gfpflags_allow_blocking(mask)) {
966                 /*
967                  * Don't care for allocation failure here because we might end
968                  * up not needing the pre-allocated extent state at all, which
969                  * is the case if we only have in the tree extent states that
970                  * cover our input range and don't cover too any other range.
971                  * If we end up needing a new extent state we allocate it later.
972                  */
973                 prealloc = alloc_extent_state(mask);
974         }
975
976         spin_lock(&tree->lock);
977         if (cached_state && *cached_state) {
978                 state = *cached_state;
979                 if (state->start <= start && state->end > start &&
980                     extent_state_in_tree(state)) {
981                         node = &state->rb_node;
982                         goto hit_next;
983                 }
984         }
985         /*
986          * this search will find all the extents that end after
987          * our range starts.
988          */
989         node = tree_search_for_insert(tree, start, &p, &parent);
990         if (!node) {
991                 prealloc = alloc_extent_state_atomic(prealloc);
992                 BUG_ON(!prealloc);
993                 err = insert_state(tree, prealloc, start, end,
994                                    &p, &parent, &bits, changeset);
995                 if (err)
996                         extent_io_tree_panic(tree, err);
997
998                 cache_state(prealloc, cached_state);
999                 prealloc = NULL;
1000                 goto out;
1001         }
1002         state = rb_entry(node, struct extent_state, rb_node);
1003 hit_next:
1004         last_start = state->start;
1005         last_end = state->end;
1006
1007         /*
1008          * | ---- desired range ---- |
1009          * | state |
1010          *
1011          * Just lock what we found and keep going
1012          */
1013         if (state->start == start && state->end <= end) {
1014                 if (state->state & exclusive_bits) {
1015                         *failed_start = state->start;
1016                         err = -EEXIST;
1017                         goto out;
1018                 }
1019
1020                 set_state_bits(tree, state, &bits, changeset);
1021                 cache_state(state, cached_state);
1022                 merge_state(tree, state);
1023                 if (last_end == (u64)-1)
1024                         goto out;
1025                 start = last_end + 1;
1026                 state = next_state(state);
1027                 if (start < end && state && state->start == start &&
1028                     !need_resched())
1029                         goto hit_next;
1030                 goto search_again;
1031         }
1032
1033         /*
1034          *     | ---- desired range ---- |
1035          * | state |
1036          *   or
1037          * | ------------- state -------------- |
1038          *
1039          * We need to split the extent we found, and may flip bits on
1040          * second half.
1041          *
1042          * If the extent we found extends past our
1043          * range, we just split and search again.  It'll get split
1044          * again the next time though.
1045          *
1046          * If the extent we found is inside our range, we set the
1047          * desired bit on it.
1048          */
1049         if (state->start < start) {
1050                 if (state->state & exclusive_bits) {
1051                         *failed_start = start;
1052                         err = -EEXIST;
1053                         goto out;
1054                 }
1055
1056                 prealloc = alloc_extent_state_atomic(prealloc);
1057                 BUG_ON(!prealloc);
1058                 err = split_state(tree, state, prealloc, start);
1059                 if (err)
1060                         extent_io_tree_panic(tree, err);
1061
1062                 prealloc = NULL;
1063                 if (err)
1064                         goto out;
1065                 if (state->end <= end) {
1066                         set_state_bits(tree, state, &bits, changeset);
1067                         cache_state(state, cached_state);
1068                         merge_state(tree, state);
1069                         if (last_end == (u64)-1)
1070                                 goto out;
1071                         start = last_end + 1;
1072                         state = next_state(state);
1073                         if (start < end && state && state->start == start &&
1074                             !need_resched())
1075                                 goto hit_next;
1076                 }
1077                 goto search_again;
1078         }
1079         /*
1080          * | ---- desired range ---- |
1081          *     | state | or               | state |
1082          *
1083          * There's a hole, we need to insert something in it and
1084          * ignore the extent we found.
1085          */
1086         if (state->start > start) {
1087                 u64 this_end;
1088                 if (end < last_start)
1089                         this_end = end;
1090                 else
1091                         this_end = last_start - 1;
1092
1093                 prealloc = alloc_extent_state_atomic(prealloc);
1094                 BUG_ON(!prealloc);
1095
1096                 /*
1097                  * Avoid to free 'prealloc' if it can be merged with
1098                  * the later extent.
1099                  */
1100                 err = insert_state(tree, prealloc, start, this_end,
1101                                    NULL, NULL, &bits, changeset);
1102                 if (err)
1103                         extent_io_tree_panic(tree, err);
1104
1105                 cache_state(prealloc, cached_state);
1106                 prealloc = NULL;
1107                 start = this_end + 1;
1108                 goto search_again;
1109         }
1110         /*
1111          * | ---- desired range ---- |
1112          *                        | state |
1113          * We need to split the extent, and set the bit
1114          * on the first half
1115          */
1116         if (state->start <= end && state->end > end) {
1117                 if (state->state & exclusive_bits) {
1118                         *failed_start = start;
1119                         err = -EEXIST;
1120                         goto out;
1121                 }
1122
1123                 prealloc = alloc_extent_state_atomic(prealloc);
1124                 BUG_ON(!prealloc);
1125                 err = split_state(tree, state, prealloc, end + 1);
1126                 if (err)
1127                         extent_io_tree_panic(tree, err);
1128
1129                 set_state_bits(tree, prealloc, &bits, changeset);
1130                 cache_state(prealloc, cached_state);
1131                 merge_state(tree, prealloc);
1132                 prealloc = NULL;
1133                 goto out;
1134         }
1135
1136 search_again:
1137         if (start > end)
1138                 goto out;
1139         spin_unlock(&tree->lock);
1140         if (gfpflags_allow_blocking(mask))
1141                 cond_resched();
1142         goto again;
1143
1144 out:
1145         spin_unlock(&tree->lock);
1146         if (prealloc)
1147                 free_extent_state(prealloc);
1148
1149         return err;
1150
1151 }
1152
1153 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1154                    unsigned bits, u64 * failed_start,
1155                    struct extent_state **cached_state, gfp_t mask)
1156 {
1157         return __set_extent_bit(tree, start, end, bits, 0, failed_start,
1158                                 cached_state, mask, NULL);
1159 }
1160
1161
1162 /**
1163  * convert_extent_bit - convert all bits in a given range from one bit to
1164  *                      another
1165  * @tree:       the io tree to search
1166  * @start:      the start offset in bytes
1167  * @end:        the end offset in bytes (inclusive)
1168  * @bits:       the bits to set in this range
1169  * @clear_bits: the bits to clear in this range
1170  * @cached_state:       state that we're going to cache
1171  *
1172  * This will go through and set bits for the given range.  If any states exist
1173  * already in this range they are set with the given bit and cleared of the
1174  * clear_bits.  This is only meant to be used by things that are mergeable, ie
1175  * converting from say DELALLOC to DIRTY.  This is not meant to be used with
1176  * boundary bits like LOCK.
1177  *
1178  * All allocations are done with GFP_NOFS.
1179  */
1180 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1181                        unsigned bits, unsigned clear_bits,
1182                        struct extent_state **cached_state)
1183 {
1184         struct extent_state *state;
1185         struct extent_state *prealloc = NULL;
1186         struct rb_node *node;
1187         struct rb_node **p;
1188         struct rb_node *parent;
1189         int err = 0;
1190         u64 last_start;
1191         u64 last_end;
1192         bool first_iteration = true;
1193
1194         btrfs_debug_check_extent_io_range(tree, start, end);
1195         trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1196                                        clear_bits);
1197
1198 again:
1199         if (!prealloc) {
1200                 /*
1201                  * Best effort, don't worry if extent state allocation fails
1202                  * here for the first iteration. We might have a cached state
1203                  * that matches exactly the target range, in which case no
1204                  * extent state allocations are needed. We'll only know this
1205                  * after locking the tree.
1206                  */
1207                 prealloc = alloc_extent_state(GFP_NOFS);
1208                 if (!prealloc && !first_iteration)
1209                         return -ENOMEM;
1210         }
1211
1212         spin_lock(&tree->lock);
1213         if (cached_state && *cached_state) {
1214                 state = *cached_state;
1215                 if (state->start <= start && state->end > start &&
1216                     extent_state_in_tree(state)) {
1217                         node = &state->rb_node;
1218                         goto hit_next;
1219                 }
1220         }
1221
1222         /*
1223          * this search will find all the extents that end after
1224          * our range starts.
1225          */
1226         node = tree_search_for_insert(tree, start, &p, &parent);
1227         if (!node) {
1228                 prealloc = alloc_extent_state_atomic(prealloc);
1229                 if (!prealloc) {
1230                         err = -ENOMEM;
1231                         goto out;
1232                 }
1233                 err = insert_state(tree, prealloc, start, end,
1234                                    &p, &parent, &bits, NULL);
1235                 if (err)
1236                         extent_io_tree_panic(tree, err);
1237                 cache_state(prealloc, cached_state);
1238                 prealloc = NULL;
1239                 goto out;
1240         }
1241         state = rb_entry(node, struct extent_state, rb_node);
1242 hit_next:
1243         last_start = state->start;
1244         last_end = state->end;
1245
1246         /*
1247          * | ---- desired range ---- |
1248          * | state |
1249          *
1250          * Just lock what we found and keep going
1251          */
1252         if (state->start == start && state->end <= end) {
1253                 set_state_bits(tree, state, &bits, NULL);
1254                 cache_state(state, cached_state);
1255                 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
1256                 if (last_end == (u64)-1)
1257                         goto out;
1258                 start = last_end + 1;
1259                 if (start < end && state && state->start == start &&
1260                     !need_resched())
1261                         goto hit_next;
1262                 goto search_again;
1263         }
1264
1265         /*
1266          *     | ---- desired range ---- |
1267          * | state |
1268          *   or
1269          * | ------------- state -------------- |
1270          *
1271          * We need to split the extent we found, and may flip bits on
1272          * second half.
1273          *
1274          * If the extent we found extends past our
1275          * range, we just split and search again.  It'll get split
1276          * again the next time though.
1277          *
1278          * If the extent we found is inside our range, we set the
1279          * desired bit on it.
1280          */
1281         if (state->start < start) {
1282                 prealloc = alloc_extent_state_atomic(prealloc);
1283                 if (!prealloc) {
1284                         err = -ENOMEM;
1285                         goto out;
1286                 }
1287                 err = split_state(tree, state, prealloc, start);
1288                 if (err)
1289                         extent_io_tree_panic(tree, err);
1290                 prealloc = NULL;
1291                 if (err)
1292                         goto out;
1293                 if (state->end <= end) {
1294                         set_state_bits(tree, state, &bits, NULL);
1295                         cache_state(state, cached_state);
1296                         state = clear_state_bit(tree, state, &clear_bits, 0,
1297                                                 NULL);
1298                         if (last_end == (u64)-1)
1299                                 goto out;
1300                         start = last_end + 1;
1301                         if (start < end && state && state->start == start &&
1302                             !need_resched())
1303                                 goto hit_next;
1304                 }
1305                 goto search_again;
1306         }
1307         /*
1308          * | ---- desired range ---- |
1309          *     | state | or               | state |
1310          *
1311          * There's a hole, we need to insert something in it and
1312          * ignore the extent we found.
1313          */
1314         if (state->start > start) {
1315                 u64 this_end;
1316                 if (end < last_start)
1317                         this_end = end;
1318                 else
1319                         this_end = last_start - 1;
1320
1321                 prealloc = alloc_extent_state_atomic(prealloc);
1322                 if (!prealloc) {
1323                         err = -ENOMEM;
1324                         goto out;
1325                 }
1326
1327                 /*
1328                  * Avoid to free 'prealloc' if it can be merged with
1329                  * the later extent.
1330                  */
1331                 err = insert_state(tree, prealloc, start, this_end,
1332                                    NULL, NULL, &bits, NULL);
1333                 if (err)
1334                         extent_io_tree_panic(tree, err);
1335                 cache_state(prealloc, cached_state);
1336                 prealloc = NULL;
1337                 start = this_end + 1;
1338                 goto search_again;
1339         }
1340         /*
1341          * | ---- desired range ---- |
1342          *                        | state |
1343          * We need to split the extent, and set the bit
1344          * on the first half
1345          */
1346         if (state->start <= end && state->end > end) {
1347                 prealloc = alloc_extent_state_atomic(prealloc);
1348                 if (!prealloc) {
1349                         err = -ENOMEM;
1350                         goto out;
1351                 }
1352
1353                 err = split_state(tree, state, prealloc, end + 1);
1354                 if (err)
1355                         extent_io_tree_panic(tree, err);
1356
1357                 set_state_bits(tree, prealloc, &bits, NULL);
1358                 cache_state(prealloc, cached_state);
1359                 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
1360                 prealloc = NULL;
1361                 goto out;
1362         }
1363
1364 search_again:
1365         if (start > end)
1366                 goto out;
1367         spin_unlock(&tree->lock);
1368         cond_resched();
1369         first_iteration = false;
1370         goto again;
1371
1372 out:
1373         spin_unlock(&tree->lock);
1374         if (prealloc)
1375                 free_extent_state(prealloc);
1376
1377         return err;
1378 }
1379
1380 /* wrappers around set/clear extent bit */
1381 int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1382                            unsigned bits, struct extent_changeset *changeset)
1383 {
1384         /*
1385          * We don't support EXTENT_LOCKED yet, as current changeset will
1386          * record any bits changed, so for EXTENT_LOCKED case, it will
1387          * either fail with -EEXIST or changeset will record the whole
1388          * range.
1389          */
1390         BUG_ON(bits & EXTENT_LOCKED);
1391
1392         return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
1393                                 changeset);
1394 }
1395
1396 int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
1397                            unsigned bits)
1398 {
1399         return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
1400                                 GFP_NOWAIT, NULL);
1401 }
1402
1403 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1404                      unsigned bits, int wake, int delete,
1405                      struct extent_state **cached)
1406 {
1407         return __clear_extent_bit(tree, start, end, bits, wake, delete,
1408                                   cached, GFP_NOFS, NULL);
1409 }
1410
1411 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1412                 unsigned bits, struct extent_changeset *changeset)
1413 {
1414         /*
1415          * Don't support EXTENT_LOCKED case, same reason as
1416          * set_record_extent_bits().
1417          */
1418         BUG_ON(bits & EXTENT_LOCKED);
1419
1420         return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
1421                                   changeset);
1422 }
1423
1424 /*
1425  * either insert or lock state struct between start and end use mask to tell
1426  * us if waiting is desired.
1427  */
1428 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1429                      struct extent_state **cached_state)
1430 {
1431         int err;
1432         u64 failed_start;
1433
1434         while (1) {
1435                 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
1436                                        EXTENT_LOCKED, &failed_start,
1437                                        cached_state, GFP_NOFS, NULL);
1438                 if (err == -EEXIST) {
1439                         wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1440                         start = failed_start;
1441                 } else
1442                         break;
1443                 WARN_ON(start > end);
1444         }
1445         return err;
1446 }
1447
1448 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1449 {
1450         int err;
1451         u64 failed_start;
1452
1453         err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1454                                &failed_start, NULL, GFP_NOFS, NULL);
1455         if (err == -EEXIST) {
1456                 if (failed_start > start)
1457                         clear_extent_bit(tree, start, failed_start - 1,
1458                                          EXTENT_LOCKED, 1, 0, NULL);
1459                 return 0;
1460         }
1461         return 1;
1462 }
1463
1464 void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1465 {
1466         unsigned long index = start >> PAGE_SHIFT;
1467         unsigned long end_index = end >> PAGE_SHIFT;
1468         struct page *page;
1469
1470         while (index <= end_index) {
1471                 page = find_get_page(inode->i_mapping, index);
1472                 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1473                 clear_page_dirty_for_io(page);
1474                 put_page(page);
1475                 index++;
1476         }
1477 }
1478
1479 void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1480 {
1481         unsigned long index = start >> PAGE_SHIFT;
1482         unsigned long end_index = end >> PAGE_SHIFT;
1483         struct page *page;
1484
1485         while (index <= end_index) {
1486                 page = find_get_page(inode->i_mapping, index);
1487                 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1488                 __set_page_dirty_nobuffers(page);
1489                 account_page_redirty(page);
1490                 put_page(page);
1491                 index++;
1492         }
1493 }
1494
1495 /* find the first state struct with 'bits' set after 'start', and
1496  * return it.  tree->lock must be held.  NULL will returned if
1497  * nothing was found after 'start'
1498  */
1499 static struct extent_state *
1500 find_first_extent_bit_state(struct extent_io_tree *tree,
1501                             u64 start, unsigned bits)
1502 {
1503         struct rb_node *node;
1504         struct extent_state *state;
1505
1506         /*
1507          * this search will find all the extents that end after
1508          * our range starts.
1509          */
1510         node = tree_search(tree, start);
1511         if (!node)
1512                 goto out;
1513
1514         while (1) {
1515                 state = rb_entry(node, struct extent_state, rb_node);
1516                 if (state->end >= start && (state->state & bits))
1517                         return state;
1518
1519                 node = rb_next(node);
1520                 if (!node)
1521                         break;
1522         }
1523 out:
1524         return NULL;
1525 }
1526
1527 /*
1528  * find the first offset in the io tree with 'bits' set. zero is
1529  * returned if we find something, and *start_ret and *end_ret are
1530  * set to reflect the state struct that was found.
1531  *
1532  * If nothing was found, 1 is returned. If found something, return 0.
1533  */
1534 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1535                           u64 *start_ret, u64 *end_ret, unsigned bits,
1536                           struct extent_state **cached_state)
1537 {
1538         struct extent_state *state;
1539         int ret = 1;
1540
1541         spin_lock(&tree->lock);
1542         if (cached_state && *cached_state) {
1543                 state = *cached_state;
1544                 if (state->end == start - 1 && extent_state_in_tree(state)) {
1545                         while ((state = next_state(state)) != NULL) {
1546                                 if (state->state & bits)
1547                                         goto got_it;
1548                         }
1549                         free_extent_state(*cached_state);
1550                         *cached_state = NULL;
1551                         goto out;
1552                 }
1553                 free_extent_state(*cached_state);
1554                 *cached_state = NULL;
1555         }
1556
1557         state = find_first_extent_bit_state(tree, start, bits);
1558 got_it:
1559         if (state) {
1560                 cache_state_if_flags(state, cached_state, 0);
1561                 *start_ret = state->start;
1562                 *end_ret = state->end;
1563                 ret = 0;
1564         }
1565 out:
1566         spin_unlock(&tree->lock);
1567         return ret;
1568 }
1569
1570 /**
1571  * find_first_clear_extent_bit - find the first range that has @bits not set.
1572  * This range could start before @start.
1573  *
1574  * @tree - the tree to search
1575  * @start - the offset at/after which the found extent should start
1576  * @start_ret - records the beginning of the range
1577  * @end_ret - records the end of the range (inclusive)
1578  * @bits - the set of bits which must be unset
1579  *
1580  * Since unallocated range is also considered one which doesn't have the bits
1581  * set it's possible that @end_ret contains -1, this happens in case the range
1582  * spans (last_range_end, end of device]. In this case it's up to the caller to
1583  * trim @end_ret to the appropriate size.
1584  */
1585 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
1586                                  u64 *start_ret, u64 *end_ret, unsigned bits)
1587 {
1588         struct extent_state *state;
1589         struct rb_node *node, *prev = NULL, *next;
1590
1591         spin_lock(&tree->lock);
1592
1593         /* Find first extent with bits cleared */
1594         while (1) {
1595                 node = __etree_search(tree, start, &next, &prev, NULL, NULL);
1596                 if (!node) {
1597                         node = next;
1598                         if (!node) {
1599                                 /*
1600                                  * We are past the last allocated chunk,
1601                                  * set start at the end of the last extent. The
1602                                  * device alloc tree should never be empty so
1603                                  * prev is always set.
1604                                  */
1605                                 ASSERT(prev);
1606                                 state = rb_entry(prev, struct extent_state, rb_node);
1607                                 *start_ret = state->end + 1;
1608                                 *end_ret = -1;
1609                                 goto out;
1610                         }
1611                 }
1612                 /*
1613                  * At this point 'node' either contains 'start' or start is
1614                  * before 'node'
1615                  */
1616                 state = rb_entry(node, struct extent_state, rb_node);
1617
1618                 if (in_range(start, state->start, state->end - state->start + 1)) {
1619                         if (state->state & bits) {
1620                                 /*
1621                                  * |--range with bits sets--|
1622                                  *    |
1623                                  *    start
1624                                  */
1625                                 start = state->end + 1;
1626                         } else {
1627                                 /*
1628                                  * 'start' falls within a range that doesn't
1629                                  * have the bits set, so take its start as
1630                                  * the beginning of the desired range
1631                                  *
1632                                  * |--range with bits cleared----|
1633                                  *      |
1634                                  *      start
1635                                  */
1636                                 *start_ret = state->start;
1637                                 break;
1638                         }
1639                 } else {
1640                         /*
1641                          * |---prev range---|---hole/unset---|---node range---|
1642                          *                          |
1643                          *                        start
1644                          *
1645                          *                        or
1646                          *
1647                          * |---hole/unset--||--first node--|
1648                          * 0   |
1649                          *    start
1650                          */
1651                         if (prev) {
1652                                 state = rb_entry(prev, struct extent_state,
1653                                                  rb_node);
1654                                 *start_ret = state->end + 1;
1655                         } else {
1656                                 *start_ret = 0;
1657                         }
1658                         break;
1659                 }
1660         }
1661
1662         /*
1663          * Find the longest stretch from start until an entry which has the
1664          * bits set
1665          */
1666         while (1) {
1667                 state = rb_entry(node, struct extent_state, rb_node);
1668                 if (state->end >= start && !(state->state & bits)) {
1669                         *end_ret = state->end;
1670                 } else {
1671                         *end_ret = state->start - 1;
1672                         break;
1673                 }
1674
1675                 node = rb_next(node);
1676                 if (!node)
1677                         break;
1678         }
1679 out:
1680         spin_unlock(&tree->lock);
1681 }
1682
1683 /*
1684  * find a contiguous range of bytes in the file marked as delalloc, not
1685  * more than 'max_bytes'.  start and end are used to return the range,
1686  *
1687  * true is returned if we find something, false if nothing was in the tree
1688  */
1689 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
1690                                u64 *end, u64 max_bytes,
1691                                struct extent_state **cached_state)
1692 {
1693         struct rb_node *node;
1694         struct extent_state *state;
1695         u64 cur_start = *start;
1696         bool found = false;
1697         u64 total_bytes = 0;
1698
1699         spin_lock(&tree->lock);
1700
1701         /*
1702          * this search will find all the extents that end after
1703          * our range starts.
1704          */
1705         node = tree_search(tree, cur_start);
1706         if (!node) {
1707                 *end = (u64)-1;
1708                 goto out;
1709         }
1710
1711         while (1) {
1712                 state = rb_entry(node, struct extent_state, rb_node);
1713                 if (found && (state->start != cur_start ||
1714                               (state->state & EXTENT_BOUNDARY))) {
1715                         goto out;
1716                 }
1717                 if (!(state->state & EXTENT_DELALLOC)) {
1718                         if (!found)
1719                                 *end = state->end;
1720                         goto out;
1721                 }
1722                 if (!found) {
1723                         *start = state->start;
1724                         *cached_state = state;
1725                         refcount_inc(&state->refs);
1726                 }
1727                 found = true;
1728                 *end = state->end;
1729                 cur_start = state->end + 1;
1730                 node = rb_next(node);
1731                 total_bytes += state->end - state->start + 1;
1732                 if (total_bytes >= max_bytes)
1733                         break;
1734                 if (!node)
1735                         break;
1736         }
1737 out:
1738         spin_unlock(&tree->lock);
1739         return found;
1740 }
1741
1742 static int __process_pages_contig(struct address_space *mapping,
1743                                   struct page *locked_page,
1744                                   pgoff_t start_index, pgoff_t end_index,
1745                                   unsigned long page_ops, pgoff_t *index_ret);
1746
1747 static noinline void __unlock_for_delalloc(struct inode *inode,
1748                                            struct page *locked_page,
1749                                            u64 start, u64 end)
1750 {
1751         unsigned long index = start >> PAGE_SHIFT;
1752         unsigned long end_index = end >> PAGE_SHIFT;
1753
1754         ASSERT(locked_page);
1755         if (index == locked_page->index && end_index == index)
1756                 return;
1757
1758         __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1759                                PAGE_UNLOCK, NULL);
1760 }
1761
1762 static noinline int lock_delalloc_pages(struct inode *inode,
1763                                         struct page *locked_page,
1764                                         u64 delalloc_start,
1765                                         u64 delalloc_end)
1766 {
1767         unsigned long index = delalloc_start >> PAGE_SHIFT;
1768         unsigned long index_ret = index;
1769         unsigned long end_index = delalloc_end >> PAGE_SHIFT;
1770         int ret;
1771
1772         ASSERT(locked_page);
1773         if (index == locked_page->index && index == end_index)
1774                 return 0;
1775
1776         ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1777                                      end_index, PAGE_LOCK, &index_ret);
1778         if (ret == -EAGAIN)
1779                 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1780                                       (u64)index_ret << PAGE_SHIFT);
1781         return ret;
1782 }
1783
1784 /*
1785  * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1786  * more than @max_bytes.  @Start and @end are used to return the range,
1787  *
1788  * Return: true if we find something
1789  *         false if nothing was in the tree
1790  */
1791 EXPORT_FOR_TESTS
1792 noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
1793                                     struct page *locked_page, u64 *start,
1794                                     u64 *end)
1795 {
1796         struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
1797         u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
1798         u64 delalloc_start;
1799         u64 delalloc_end;
1800         bool found;
1801         struct extent_state *cached_state = NULL;
1802         int ret;
1803         int loops = 0;
1804
1805 again:
1806         /* step one, find a bunch of delalloc bytes starting at start */
1807         delalloc_start = *start;
1808         delalloc_end = 0;
1809         found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1810                                           max_bytes, &cached_state);
1811         if (!found || delalloc_end <= *start) {
1812                 *start = delalloc_start;
1813                 *end = delalloc_end;
1814                 free_extent_state(cached_state);
1815                 return false;
1816         }
1817
1818         /*
1819          * start comes from the offset of locked_page.  We have to lock
1820          * pages in order, so we can't process delalloc bytes before
1821          * locked_page
1822          */
1823         if (delalloc_start < *start)
1824                 delalloc_start = *start;
1825
1826         /*
1827          * make sure to limit the number of pages we try to lock down
1828          */
1829         if (delalloc_end + 1 - delalloc_start > max_bytes)
1830                 delalloc_end = delalloc_start + max_bytes - 1;
1831
1832         /* step two, lock all the pages after the page that has start */
1833         ret = lock_delalloc_pages(inode, locked_page,
1834                                   delalloc_start, delalloc_end);
1835         ASSERT(!ret || ret == -EAGAIN);
1836         if (ret == -EAGAIN) {
1837                 /* some of the pages are gone, lets avoid looping by
1838                  * shortening the size of the delalloc range we're searching
1839                  */
1840                 free_extent_state(cached_state);
1841                 cached_state = NULL;
1842                 if (!loops) {
1843                         max_bytes = PAGE_SIZE;
1844                         loops = 1;
1845                         goto again;
1846                 } else {
1847                         found = false;
1848                         goto out_failed;
1849                 }
1850         }
1851
1852         /* step three, lock the state bits for the whole range */
1853         lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
1854
1855         /* then test to make sure it is all still delalloc */
1856         ret = test_range_bit(tree, delalloc_start, delalloc_end,
1857                              EXTENT_DELALLOC, 1, cached_state);
1858         if (!ret) {
1859                 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1860                                      &cached_state);
1861                 __unlock_for_delalloc(inode, locked_page,
1862                               delalloc_start, delalloc_end);
1863                 cond_resched();
1864                 goto again;
1865         }
1866         free_extent_state(cached_state);
1867         *start = delalloc_start;
1868         *end = delalloc_end;
1869 out_failed:
1870         return found;
1871 }
1872
1873 static int __process_pages_contig(struct address_space *mapping,
1874                                   struct page *locked_page,
1875                                   pgoff_t start_index, pgoff_t end_index,
1876                                   unsigned long page_ops, pgoff_t *index_ret)
1877 {
1878         unsigned long nr_pages = end_index - start_index + 1;
1879         unsigned long pages_locked = 0;
1880         pgoff_t index = start_index;
1881         struct page *pages[16];
1882         unsigned ret;
1883         int err = 0;
1884         int i;
1885
1886         if (page_ops & PAGE_LOCK) {
1887                 ASSERT(page_ops == PAGE_LOCK);
1888                 ASSERT(index_ret && *index_ret == start_index);
1889         }
1890
1891         if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
1892                 mapping_set_error(mapping, -EIO);
1893
1894         while (nr_pages > 0) {
1895                 ret = find_get_pages_contig(mapping, index,
1896                                      min_t(unsigned long,
1897                                      nr_pages, ARRAY_SIZE(pages)), pages);
1898                 if (ret == 0) {
1899                         /*
1900                          * Only if we're going to lock these pages,
1901                          * can we find nothing at @index.
1902                          */
1903                         ASSERT(page_ops & PAGE_LOCK);
1904                         err = -EAGAIN;
1905                         goto out;
1906                 }
1907
1908                 for (i = 0; i < ret; i++) {
1909                         if (page_ops & PAGE_SET_PRIVATE2)
1910                                 SetPagePrivate2(pages[i]);
1911
1912                         if (locked_page && pages[i] == locked_page) {
1913                                 put_page(pages[i]);
1914                                 pages_locked++;
1915                                 continue;
1916                         }
1917                         if (page_ops & PAGE_CLEAR_DIRTY)
1918                                 clear_page_dirty_for_io(pages[i]);
1919                         if (page_ops & PAGE_SET_WRITEBACK)
1920                                 set_page_writeback(pages[i]);
1921                         if (page_ops & PAGE_SET_ERROR)
1922                                 SetPageError(pages[i]);
1923                         if (page_ops & PAGE_END_WRITEBACK)
1924                                 end_page_writeback(pages[i]);
1925                         if (page_ops & PAGE_UNLOCK)
1926                                 unlock_page(pages[i]);
1927                         if (page_ops & PAGE_LOCK) {
1928                                 lock_page(pages[i]);
1929                                 if (!PageDirty(pages[i]) ||
1930                                     pages[i]->mapping != mapping) {
1931                                         unlock_page(pages[i]);
1932                                         put_page(pages[i]);
1933                                         err = -EAGAIN;
1934                                         goto out;
1935                                 }
1936                         }
1937                         put_page(pages[i]);
1938                         pages_locked++;
1939                 }
1940                 nr_pages -= ret;
1941                 index += ret;
1942                 cond_resched();
1943         }
1944 out:
1945         if (err && index_ret)
1946                 *index_ret = start_index + pages_locked - 1;
1947         return err;
1948 }
1949
1950 void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1951                                   struct page *locked_page,
1952                                   unsigned clear_bits,
1953                                   unsigned long page_ops)
1954 {
1955         clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
1956                          NULL);
1957
1958         __process_pages_contig(inode->i_mapping, locked_page,
1959                                start >> PAGE_SHIFT, end >> PAGE_SHIFT,
1960                                page_ops, NULL);
1961 }
1962
1963 /*
1964  * count the number of bytes in the tree that have a given bit(s)
1965  * set.  This can be fairly slow, except for EXTENT_DIRTY which is
1966  * cached.  The total number found is returned.
1967  */
1968 u64 count_range_bits(struct extent_io_tree *tree,
1969                      u64 *start, u64 search_end, u64 max_bytes,
1970                      unsigned bits, int contig)
1971 {
1972         struct rb_node *node;
1973         struct extent_state *state;
1974         u64 cur_start = *start;
1975         u64 total_bytes = 0;
1976         u64 last = 0;
1977         int found = 0;
1978
1979         if (WARN_ON(search_end <= cur_start))
1980                 return 0;
1981
1982         spin_lock(&tree->lock);
1983         if (cur_start == 0 && bits == EXTENT_DIRTY) {
1984                 total_bytes = tree->dirty_bytes;
1985                 goto out;
1986         }
1987         /*
1988          * this search will find all the extents that end after
1989          * our range starts.
1990          */
1991         node = tree_search(tree, cur_start);
1992         if (!node)
1993                 goto out;
1994
1995         while (1) {
1996                 state = rb_entry(node, struct extent_state, rb_node);
1997                 if (state->start > search_end)
1998                         break;
1999                 if (contig && found && state->start > last + 1)
2000                         break;
2001                 if (state->end >= cur_start && (state->state & bits) == bits) {
2002                         total_bytes += min(search_end, state->end) + 1 -
2003                                        max(cur_start, state->start);
2004                         if (total_bytes >= max_bytes)
2005                                 break;
2006                         if (!found) {
2007                                 *start = max(cur_start, state->start);
2008                                 found = 1;
2009                         }
2010                         last = state->end;
2011                 } else if (contig && found) {
2012                         break;
2013                 }
2014                 node = rb_next(node);
2015                 if (!node)
2016                         break;
2017         }
2018 out:
2019         spin_unlock(&tree->lock);
2020         return total_bytes;
2021 }
2022
2023 /*
2024  * set the private field for a given byte offset in the tree.  If there isn't
2025  * an extent_state there already, this does nothing.
2026  */
2027 int set_state_failrec(struct extent_io_tree *tree, u64 start,
2028                       struct io_failure_record *failrec)
2029 {
2030         struct rb_node *node;
2031         struct extent_state *state;
2032         int ret = 0;
2033
2034         spin_lock(&tree->lock);
2035         /*
2036          * this search will find all the extents that end after
2037          * our range starts.
2038          */
2039         node = tree_search(tree, start);
2040         if (!node) {
2041                 ret = -ENOENT;
2042                 goto out;
2043         }
2044         state = rb_entry(node, struct extent_state, rb_node);
2045         if (state->start != start) {
2046                 ret = -ENOENT;
2047                 goto out;
2048         }
2049         state->failrec = failrec;
2050 out:
2051         spin_unlock(&tree->lock);
2052         return ret;
2053 }
2054
2055 int get_state_failrec(struct extent_io_tree *tree, u64 start,
2056                       struct io_failure_record **failrec)
2057 {
2058         struct rb_node *node;
2059         struct extent_state *state;
2060         int ret = 0;
2061
2062         spin_lock(&tree->lock);
2063         /*
2064          * this search will find all the extents that end after
2065          * our range starts.
2066          */
2067         node = tree_search(tree, start);
2068         if (!node) {
2069                 ret = -ENOENT;
2070                 goto out;
2071         }
2072         state = rb_entry(node, struct extent_state, rb_node);
2073         if (state->start != start) {
2074                 ret = -ENOENT;
2075                 goto out;
2076         }
2077         *failrec = state->failrec;
2078 out:
2079         spin_unlock(&tree->lock);
2080         return ret;
2081 }
2082
2083 /*
2084  * searches a range in the state tree for a given mask.
2085  * If 'filled' == 1, this returns 1 only if every extent in the tree
2086  * has the bits set.  Otherwise, 1 is returned if any bit in the
2087  * range is found set.
2088  */
2089 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
2090                    unsigned bits, int filled, struct extent_state *cached)
2091 {
2092         struct extent_state *state = NULL;
2093         struct rb_node *node;
2094         int bitset = 0;
2095
2096         spin_lock(&tree->lock);
2097         if (cached && extent_state_in_tree(cached) && cached->start <= start &&
2098             cached->end > start)
2099                 node = &cached->rb_node;
2100         else
2101                 node = tree_search(tree, start);
2102         while (node && start <= end) {
2103                 state = rb_entry(node, struct extent_state, rb_node);
2104
2105                 if (filled && state->start > start) {
2106                         bitset = 0;
2107                         break;
2108                 }
2109
2110                 if (state->start > end)
2111                         break;
2112
2113                 if (state->state & bits) {
2114                         bitset = 1;
2115                         if (!filled)
2116                                 break;
2117                 } else if (filled) {
2118                         bitset = 0;
2119                         break;
2120                 }
2121
2122                 if (state->end == (u64)-1)
2123                         break;
2124
2125                 start = state->end + 1;
2126                 if (start > end)
2127                         break;
2128                 node = rb_next(node);
2129                 if (!node) {
2130                         if (filled)
2131                                 bitset = 0;
2132                         break;
2133                 }
2134         }
2135         spin_unlock(&tree->lock);
2136         return bitset;
2137 }
2138
2139 /*
2140  * helper function to set a given page up to date if all the
2141  * extents in the tree for that page are up to date
2142  */
2143 static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
2144 {
2145         u64 start = page_offset(page);
2146         u64 end = start + PAGE_SIZE - 1;
2147         if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
2148                 SetPageUptodate(page);
2149 }
2150
2151 int free_io_failure(struct extent_io_tree *failure_tree,
2152                     struct extent_io_tree *io_tree,
2153                     struct io_failure_record *rec)
2154 {
2155         int ret;
2156         int err = 0;
2157
2158         set_state_failrec(failure_tree, rec->start, NULL);
2159         ret = clear_extent_bits(failure_tree, rec->start,
2160                                 rec->start + rec->len - 1,
2161                                 EXTENT_LOCKED | EXTENT_DIRTY);
2162         if (ret)
2163                 err = ret;
2164
2165         ret = clear_extent_bits(io_tree, rec->start,
2166                                 rec->start + rec->len - 1,
2167                                 EXTENT_DAMAGED);
2168         if (ret && !err)
2169                 err = ret;
2170
2171         kfree(rec);
2172         return err;
2173 }
2174
2175 /*
2176  * this bypasses the standard btrfs submit functions deliberately, as
2177  * the standard behavior is to write all copies in a raid setup. here we only
2178  * want to write the one bad copy. so we do the mapping for ourselves and issue
2179  * submit_bio directly.
2180  * to avoid any synchronization issues, wait for the data after writing, which
2181  * actually prevents the read that triggered the error from finishing.
2182  * currently, there can be no more than two copies of every data bit. thus,
2183  * exactly one rewrite is required.
2184  */
2185 int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2186                       u64 length, u64 logical, struct page *page,
2187                       unsigned int pg_offset, int mirror_num)
2188 {
2189         struct bio *bio;
2190         struct btrfs_device *dev;
2191         u64 map_length = 0;
2192         u64 sector;
2193         struct btrfs_bio *bbio = NULL;
2194         int ret;
2195
2196         ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
2197         BUG_ON(!mirror_num);
2198
2199         bio = btrfs_io_bio_alloc(1);
2200         bio->bi_iter.bi_size = 0;
2201         map_length = length;
2202
2203         /*
2204          * Avoid races with device replace and make sure our bbio has devices
2205          * associated to its stripes that don't go away while we are doing the
2206          * read repair operation.
2207          */
2208         btrfs_bio_counter_inc_blocked(fs_info);
2209         if (btrfs_is_parity_mirror(fs_info, logical, length)) {
2210                 /*
2211                  * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2212                  * to update all raid stripes, but here we just want to correct
2213                  * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2214                  * stripe's dev and sector.
2215                  */
2216                 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2217                                       &map_length, &bbio, 0);
2218                 if (ret) {
2219                         btrfs_bio_counter_dec(fs_info);
2220                         bio_put(bio);
2221                         return -EIO;
2222                 }
2223                 ASSERT(bbio->mirror_num == 1);
2224         } else {
2225                 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2226                                       &map_length, &bbio, mirror_num);
2227                 if (ret) {
2228                         btrfs_bio_counter_dec(fs_info);
2229                         bio_put(bio);
2230                         return -EIO;
2231                 }
2232                 BUG_ON(mirror_num != bbio->mirror_num);
2233         }
2234
2235         sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
2236         bio->bi_iter.bi_sector = sector;
2237         dev = bbio->stripes[bbio->mirror_num - 1].dev;
2238         btrfs_put_bbio(bbio);
2239         if (!dev || !dev->bdev ||
2240             !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
2241                 btrfs_bio_counter_dec(fs_info);
2242                 bio_put(bio);
2243                 return -EIO;
2244         }
2245         bio_set_dev(bio, dev->bdev);
2246         bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
2247         bio_add_page(bio, page, length, pg_offset);
2248
2249         if (btrfsic_submit_bio_wait(bio)) {
2250                 /* try to remap that extent elsewhere? */
2251                 btrfs_bio_counter_dec(fs_info);
2252                 bio_put(bio);
2253                 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
2254                 return -EIO;
2255         }
2256
2257         btrfs_info_rl_in_rcu(fs_info,
2258                 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
2259                                   ino, start,
2260                                   rcu_str_deref(dev->name), sector);
2261         btrfs_bio_counter_dec(fs_info);
2262         bio_put(bio);
2263         return 0;
2264 }
2265
2266 int btrfs_repair_eb_io_failure(struct extent_buffer *eb, int mirror_num)
2267 {
2268         struct btrfs_fs_info *fs_info = eb->fs_info;
2269         u64 start = eb->start;
2270         int i, num_pages = num_extent_pages(eb);
2271         int ret = 0;
2272
2273         if (sb_rdonly(fs_info->sb))
2274                 return -EROFS;
2275
2276         for (i = 0; i < num_pages; i++) {
2277                 struct page *p = eb->pages[i];
2278
2279                 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
2280                                         start - page_offset(p), mirror_num);
2281                 if (ret)
2282                         break;
2283                 start += PAGE_SIZE;
2284         }
2285
2286         return ret;
2287 }
2288
2289 /*
2290  * each time an IO finishes, we do a fast check in the IO failure tree
2291  * to see if we need to process or clean up an io_failure_record
2292  */
2293 int clean_io_failure(struct btrfs_fs_info *fs_info,
2294                      struct extent_io_tree *failure_tree,
2295                      struct extent_io_tree *io_tree, u64 start,
2296                      struct page *page, u64 ino, unsigned int pg_offset)
2297 {
2298         u64 private;
2299         struct io_failure_record *failrec;
2300         struct extent_state *state;
2301         int num_copies;
2302         int ret;
2303
2304         private = 0;
2305         ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2306                                EXTENT_DIRTY, 0);
2307         if (!ret)
2308                 return 0;
2309
2310         ret = get_state_failrec(failure_tree, start, &failrec);
2311         if (ret)
2312                 return 0;
2313
2314         BUG_ON(!failrec->this_mirror);
2315
2316         if (failrec->in_validation) {
2317                 /* there was no real error, just free the record */
2318                 btrfs_debug(fs_info,
2319                         "clean_io_failure: freeing dummy error at %llu",
2320                         failrec->start);
2321                 goto out;
2322         }
2323         if (sb_rdonly(fs_info->sb))
2324                 goto out;
2325
2326         spin_lock(&io_tree->lock);
2327         state = find_first_extent_bit_state(io_tree,
2328                                             failrec->start,
2329                                             EXTENT_LOCKED);
2330         spin_unlock(&io_tree->lock);
2331
2332         if (state && state->start <= failrec->start &&
2333             state->end >= failrec->start + failrec->len - 1) {
2334                 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2335                                               failrec->len);
2336                 if (num_copies > 1)  {
2337                         repair_io_failure(fs_info, ino, start, failrec->len,
2338                                           failrec->logical, page, pg_offset,
2339                                           failrec->failed_mirror);
2340                 }
2341         }
2342
2343 out:
2344         free_io_failure(failure_tree, io_tree, failrec);
2345
2346         return 0;
2347 }
2348
2349 /*
2350  * Can be called when
2351  * - hold extent lock
2352  * - under ordered extent
2353  * - the inode is freeing
2354  */
2355 void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
2356 {
2357         struct extent_io_tree *failure_tree = &inode->io_failure_tree;
2358         struct io_failure_record *failrec;
2359         struct extent_state *state, *next;
2360
2361         if (RB_EMPTY_ROOT(&failure_tree->state))
2362                 return;
2363
2364         spin_lock(&failure_tree->lock);
2365         state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2366         while (state) {
2367                 if (state->start > end)
2368                         break;
2369
2370                 ASSERT(state->end <= end);
2371
2372                 next = next_state(state);
2373
2374                 failrec = state->failrec;
2375                 free_extent_state(state);
2376                 kfree(failrec);
2377
2378                 state = next;
2379         }
2380         spin_unlock(&failure_tree->lock);
2381 }
2382
2383 int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
2384                 struct io_failure_record **failrec_ret)
2385 {
2386         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2387         struct io_failure_record *failrec;
2388         struct extent_map *em;
2389         struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2390         struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2391         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2392         int ret;
2393         u64 logical;
2394
2395         ret = get_state_failrec(failure_tree, start, &failrec);
2396         if (ret) {
2397                 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2398                 if (!failrec)
2399                         return -ENOMEM;
2400
2401                 failrec->start = start;
2402                 failrec->len = end - start + 1;
2403                 failrec->this_mirror = 0;
2404                 failrec->bio_flags = 0;
2405                 failrec->in_validation = 0;
2406
2407                 read_lock(&em_tree->lock);
2408                 em = lookup_extent_mapping(em_tree, start, failrec->len);
2409                 if (!em) {
2410                         read_unlock(&em_tree->lock);
2411                         kfree(failrec);
2412                         return -EIO;
2413                 }
2414
2415                 if (em->start > start || em->start + em->len <= start) {
2416                         free_extent_map(em);
2417                         em = NULL;
2418                 }
2419                 read_unlock(&em_tree->lock);
2420                 if (!em) {
2421                         kfree(failrec);
2422                         return -EIO;
2423                 }
2424
2425                 logical = start - em->start;
2426                 logical = em->block_start + logical;
2427                 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2428                         logical = em->block_start;
2429                         failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2430                         extent_set_compress_type(&failrec->bio_flags,
2431                                                  em->compress_type);
2432                 }
2433
2434                 btrfs_debug(fs_info,
2435                         "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2436                         logical, start, failrec->len);
2437
2438                 failrec->logical = logical;
2439                 free_extent_map(em);
2440
2441                 /* set the bits in the private failure tree */
2442                 ret = set_extent_bits(failure_tree, start, end,
2443                                         EXTENT_LOCKED | EXTENT_DIRTY);
2444                 if (ret >= 0)
2445                         ret = set_state_failrec(failure_tree, start, failrec);
2446                 /* set the bits in the inode's tree */
2447                 if (ret >= 0)
2448                         ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
2449                 if (ret < 0) {
2450                         kfree(failrec);
2451                         return ret;
2452                 }
2453         } else {
2454                 btrfs_debug(fs_info,
2455                         "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2456                         failrec->logical, failrec->start, failrec->len,
2457                         failrec->in_validation);
2458                 /*
2459                  * when data can be on disk more than twice, add to failrec here
2460                  * (e.g. with a list for failed_mirror) to make
2461                  * clean_io_failure() clean all those errors at once.
2462                  */
2463         }
2464
2465         *failrec_ret = failrec;
2466
2467         return 0;
2468 }
2469
2470 bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
2471                            struct io_failure_record *failrec, int failed_mirror)
2472 {
2473         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2474         int num_copies;
2475
2476         num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
2477         if (num_copies == 1) {
2478                 /*
2479                  * we only have a single copy of the data, so don't bother with
2480                  * all the retry and error correction code that follows. no
2481                  * matter what the error is, it is very likely to persist.
2482                  */
2483                 btrfs_debug(fs_info,
2484                         "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2485                         num_copies, failrec->this_mirror, failed_mirror);
2486                 return false;
2487         }
2488
2489         /*
2490          * there are two premises:
2491          *      a) deliver good data to the caller
2492          *      b) correct the bad sectors on disk
2493          */
2494         if (failed_bio_pages > 1) {
2495                 /*
2496                  * to fulfill b), we need to know the exact failing sectors, as
2497                  * we don't want to rewrite any more than the failed ones. thus,
2498                  * we need separate read requests for the failed bio
2499                  *
2500                  * if the following BUG_ON triggers, our validation request got
2501                  * merged. we need separate requests for our algorithm to work.
2502                  */
2503                 BUG_ON(failrec->in_validation);
2504                 failrec->in_validation = 1;
2505                 failrec->this_mirror = failed_mirror;
2506         } else {
2507                 /*
2508                  * we're ready to fulfill a) and b) alongside. get a good copy
2509                  * of the failed sector and if we succeed, we have setup
2510                  * everything for repair_io_failure to do the rest for us.
2511                  */
2512                 if (failrec->in_validation) {
2513                         BUG_ON(failrec->this_mirror != failed_mirror);
2514                         failrec->in_validation = 0;
2515                         failrec->this_mirror = 0;
2516                 }
2517                 failrec->failed_mirror = failed_mirror;
2518                 failrec->this_mirror++;
2519                 if (failrec->this_mirror == failed_mirror)
2520                         failrec->this_mirror++;
2521         }
2522
2523         if (failrec->this_mirror > num_copies) {
2524                 btrfs_debug(fs_info,
2525                         "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2526                         num_copies, failrec->this_mirror, failed_mirror);
2527                 return false;
2528         }
2529
2530         return true;
2531 }
2532
2533
2534 struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2535                                     struct io_failure_record *failrec,
2536                                     struct page *page, int pg_offset, int icsum,
2537                                     bio_end_io_t *endio_func, void *data)
2538 {
2539         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2540         struct bio *bio;
2541         struct btrfs_io_bio *btrfs_failed_bio;
2542         struct btrfs_io_bio *btrfs_bio;
2543
2544         bio = btrfs_io_bio_alloc(1);
2545         bio->bi_end_io = endio_func;
2546         bio->bi_iter.bi_sector = failrec->logical >> 9;
2547         bio->bi_iter.bi_size = 0;
2548         bio->bi_private = data;
2549
2550         btrfs_failed_bio = btrfs_io_bio(failed_bio);
2551         if (btrfs_failed_bio->csum) {
2552                 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2553
2554                 btrfs_bio = btrfs_io_bio(bio);
2555                 btrfs_bio->csum = btrfs_bio->csum_inline;
2556                 icsum *= csum_size;
2557                 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
2558                        csum_size);
2559         }
2560
2561         bio_add_page(bio, page, failrec->len, pg_offset);
2562
2563         return bio;
2564 }
2565
2566 /*
2567  * This is a generic handler for readpage errors. If other copies exist, read
2568  * those and write back good data to the failed position. Does not investigate
2569  * in remapping the failed extent elsewhere, hoping the device will be smart
2570  * enough to do this as needed
2571  */
2572 static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2573                               struct page *page, u64 start, u64 end,
2574                               int failed_mirror)
2575 {
2576         struct io_failure_record *failrec;
2577         struct inode *inode = page->mapping->host;
2578         struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2579         struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2580         struct bio *bio;
2581         int read_mode = 0;
2582         blk_status_t status;
2583         int ret;
2584         unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
2585
2586         BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
2587
2588         ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2589         if (ret)
2590                 return ret;
2591
2592         if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
2593                                     failed_mirror)) {
2594                 free_io_failure(failure_tree, tree, failrec);
2595                 return -EIO;
2596         }
2597
2598         if (failed_bio_pages > 1)
2599                 read_mode |= REQ_FAILFAST_DEV;
2600
2601         phy_offset >>= inode->i_sb->s_blocksize_bits;
2602         bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2603                                       start - page_offset(page),
2604                                       (int)phy_offset, failed_bio->bi_end_io,
2605                                       NULL);
2606         bio->bi_opf = REQ_OP_READ | read_mode;
2607
2608         btrfs_debug(btrfs_sb(inode->i_sb),
2609                 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2610                 read_mode, failrec->this_mirror, failrec->in_validation);
2611
2612         status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
2613                                          failrec->bio_flags);
2614         if (status) {
2615                 free_io_failure(failure_tree, tree, failrec);
2616                 bio_put(bio);
2617                 ret = blk_status_to_errno(status);
2618         }
2619
2620         return ret;
2621 }
2622
2623 /* lots and lots of room for performance fixes in the end_bio funcs */
2624
2625 void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2626 {
2627         int uptodate = (err == 0);
2628         int ret = 0;
2629
2630         btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
2631
2632         if (!uptodate) {
2633                 ClearPageUptodate(page);
2634                 SetPageError(page);
2635                 ret = err < 0 ? err : -EIO;
2636                 mapping_set_error(page->mapping, ret);
2637         }
2638 }
2639
2640 /*
2641  * after a writepage IO is done, we need to:
2642  * clear the uptodate bits on error
2643  * clear the writeback bits in the extent tree for this IO
2644  * end_page_writeback if the page has no more pending IO
2645  *
2646  * Scheduling is not allowed, so the extent state tree is expected
2647  * to have one and only one object corresponding to this IO.
2648  */
2649 static void end_bio_extent_writepage(struct bio *bio)
2650 {
2651         int error = blk_status_to_errno(bio->bi_status);
2652         struct bio_vec *bvec;
2653         u64 start;
2654         u64 end;
2655         struct bvec_iter_all iter_all;
2656
2657         ASSERT(!bio_flagged(bio, BIO_CLONED));
2658         bio_for_each_segment_all(bvec, bio, iter_all) {
2659                 struct page *page = bvec->bv_page;
2660                 struct inode *inode = page->mapping->host;
2661                 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2662
2663                 /* We always issue full-page reads, but if some block
2664                  * in a page fails to read, blk_update_request() will
2665                  * advance bv_offset and adjust bv_len to compensate.
2666                  * Print a warning for nonzero offsets, and an error
2667                  * if they don't add up to a full page.  */
2668                 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2669                         if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
2670                                 btrfs_err(fs_info,
2671                                    "partial page write in btrfs with offset %u and length %u",
2672                                         bvec->bv_offset, bvec->bv_len);
2673                         else
2674                                 btrfs_info(fs_info,
2675                                    "incomplete page write in btrfs with offset %u and length %u",
2676                                         bvec->bv_offset, bvec->bv_len);
2677                 }
2678
2679                 start = page_offset(page);
2680                 end = start + bvec->bv_offset + bvec->bv_len - 1;
2681
2682                 end_extent_writepage(page, error, start, end);
2683                 end_page_writeback(page);
2684         }
2685
2686         bio_put(bio);
2687 }
2688
2689 static void
2690 endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2691                               int uptodate)
2692 {
2693         struct extent_state *cached = NULL;
2694         u64 end = start + len - 1;
2695
2696         if (uptodate && tree->track_uptodate)
2697                 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2698         unlock_extent_cached_atomic(tree, start, end, &cached);
2699 }
2700
2701 /*
2702  * after a readpage IO is done, we need to:
2703  * clear the uptodate bits on error
2704  * set the uptodate bits if things worked
2705  * set the page up to date if all extents in the tree are uptodate
2706  * clear the lock bit in the extent tree
2707  * unlock the page if there are no other extents locked for it
2708  *
2709  * Scheduling is not allowed, so the extent state tree is expected
2710  * to have one and only one object corresponding to this IO.
2711  */
2712 static void end_bio_extent_readpage(struct bio *bio)
2713 {
2714         struct bio_vec *bvec;
2715         int uptodate = !bio->bi_status;
2716         struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
2717         struct extent_io_tree *tree, *failure_tree;
2718         u64 offset = 0;
2719         u64 start;
2720         u64 end;
2721         u64 len;
2722         u64 extent_start = 0;
2723         u64 extent_len = 0;
2724         int mirror;
2725         int ret;
2726         struct bvec_iter_all iter_all;
2727
2728         ASSERT(!bio_flagged(bio, BIO_CLONED));
2729         bio_for_each_segment_all(bvec, bio, iter_all) {
2730                 struct page *page = bvec->bv_page;
2731                 struct inode *inode = page->mapping->host;
2732                 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2733                 bool data_inode = btrfs_ino(BTRFS_I(inode))
2734                         != BTRFS_BTREE_INODE_OBJECTID;
2735
2736                 btrfs_debug(fs_info,
2737                         "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
2738                         (u64)bio->bi_iter.bi_sector, bio->bi_status,
2739                         io_bio->mirror_num);
2740                 tree = &BTRFS_I(inode)->io_tree;
2741                 failure_tree = &BTRFS_I(inode)->io_failure_tree;
2742
2743                 /* We always issue full-page reads, but if some block
2744                  * in a page fails to read, blk_update_request() will
2745                  * advance bv_offset and adjust bv_len to compensate.
2746                  * Print a warning for nonzero offsets, and an error
2747                  * if they don't add up to a full page.  */
2748                 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2749                         if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
2750                                 btrfs_err(fs_info,
2751                                         "partial page read in btrfs with offset %u and length %u",
2752                                         bvec->bv_offset, bvec->bv_len);
2753                         else
2754                                 btrfs_info(fs_info,
2755                                         "incomplete page read in btrfs with offset %u and length %u",
2756                                         bvec->bv_offset, bvec->bv_len);
2757                 }
2758
2759                 start = page_offset(page);
2760                 end = start + bvec->bv_offset + bvec->bv_len - 1;
2761                 len = bvec->bv_len;
2762
2763                 mirror = io_bio->mirror_num;
2764                 if (likely(uptodate)) {
2765                         ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2766                                                               page, start, end,
2767                                                               mirror);
2768                         if (ret)
2769                                 uptodate = 0;
2770                         else
2771                                 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2772                                                  failure_tree, tree, start,
2773                                                  page,
2774                                                  btrfs_ino(BTRFS_I(inode)), 0);
2775                 }
2776
2777                 if (likely(uptodate))
2778                         goto readpage_ok;
2779
2780                 if (data_inode) {
2781
2782                         /*
2783                          * The generic bio_readpage_error handles errors the
2784                          * following way: If possible, new read requests are
2785                          * created and submitted and will end up in
2786                          * end_bio_extent_readpage as well (if we're lucky,
2787                          * not in the !uptodate case). In that case it returns
2788                          * 0 and we just go on with the next page in our bio.
2789                          * If it can't handle the error it will return -EIO and
2790                          * we remain responsible for that page.
2791                          */
2792                         ret = bio_readpage_error(bio, offset, page, start, end,
2793                                                  mirror);
2794                         if (ret == 0) {
2795                                 uptodate = !bio->bi_status;
2796                                 offset += len;
2797                                 continue;
2798                         }
2799                 } else {
2800                         struct extent_buffer *eb;
2801
2802                         eb = (struct extent_buffer *)page->private;
2803                         set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2804                         eb->read_mirror = mirror;
2805                         atomic_dec(&eb->io_pages);
2806                         if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2807                                                &eb->bflags))
2808                                 btree_readahead_hook(eb, -EIO);
2809                 }
2810 readpage_ok:
2811                 if (likely(uptodate)) {
2812                         loff_t i_size = i_size_read(inode);
2813                         pgoff_t end_index = i_size >> PAGE_SHIFT;
2814                         unsigned off;
2815
2816                         /* Zero out the end if this page straddles i_size */
2817                         off = offset_in_page(i_size);
2818                         if (page->index == end_index && off)
2819                                 zero_user_segment(page, off, PAGE_SIZE);
2820                         SetPageUptodate(page);
2821                 } else {
2822                         ClearPageUptodate(page);
2823                         SetPageError(page);
2824                 }
2825                 unlock_page(page);
2826                 offset += len;
2827
2828                 if (unlikely(!uptodate)) {
2829                         if (extent_len) {
2830                                 endio_readpage_release_extent(tree,
2831                                                               extent_start,
2832                                                               extent_len, 1);
2833                                 extent_start = 0;
2834                                 extent_len = 0;
2835                         }
2836                         endio_readpage_release_extent(tree, start,
2837                                                       end - start + 1, 0);
2838                 } else if (!extent_len) {
2839                         extent_start = start;
2840                         extent_len = end + 1 - start;
2841                 } else if (extent_start + extent_len == start) {
2842                         extent_len += end + 1 - start;
2843                 } else {
2844                         endio_readpage_release_extent(tree, extent_start,
2845                                                       extent_len, uptodate);
2846                         extent_start = start;
2847                         extent_len = end + 1 - start;
2848                 }
2849         }
2850
2851         if (extent_len)
2852                 endio_readpage_release_extent(tree, extent_start, extent_len,
2853                                               uptodate);
2854         btrfs_io_bio_free_csum(io_bio);
2855         bio_put(bio);
2856 }
2857
2858 /*
2859  * Initialize the members up to but not including 'bio'. Use after allocating a
2860  * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2861  * 'bio' because use of __GFP_ZERO is not supported.
2862  */
2863 static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
2864 {
2865         memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2866 }
2867
2868 /*
2869  * The following helpers allocate a bio. As it's backed by a bioset, it'll
2870  * never fail.  We're returning a bio right now but you can call btrfs_io_bio
2871  * for the appropriate container_of magic
2872  */
2873 struct bio *btrfs_bio_alloc(u64 first_byte)
2874 {
2875         struct bio *bio;
2876
2877         bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
2878         bio->bi_iter.bi_sector = first_byte >> 9;
2879         btrfs_io_bio_init(btrfs_io_bio(bio));
2880         return bio;
2881 }
2882
2883 struct bio *btrfs_bio_clone(struct bio *bio)
2884 {
2885         struct btrfs_io_bio *btrfs_bio;
2886         struct bio *new;
2887
2888         /* Bio allocation backed by a bioset does not fail */
2889         new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
2890         btrfs_bio = btrfs_io_bio(new);
2891         btrfs_io_bio_init(btrfs_bio);
2892         btrfs_bio->iter = bio->bi_iter;
2893         return new;
2894 }
2895
2896 struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
2897 {
2898         struct bio *bio;
2899
2900         /* Bio allocation backed by a bioset does not fail */
2901         bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
2902         btrfs_io_bio_init(btrfs_io_bio(bio));
2903         return bio;
2904 }
2905
2906 struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
2907 {
2908         struct bio *bio;
2909         struct btrfs_io_bio *btrfs_bio;
2910
2911         /* this will never fail when it's backed by a bioset */
2912         bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
2913         ASSERT(bio);
2914
2915         btrfs_bio = btrfs_io_bio(bio);
2916         btrfs_io_bio_init(btrfs_bio);
2917
2918         bio_trim(bio, offset >> 9, size >> 9);
2919         btrfs_bio->iter = bio->bi_iter;
2920         return bio;
2921 }
2922
2923 /*
2924  * @opf:        bio REQ_OP_* and REQ_* flags as one value
2925  * @tree:       tree so we can call our merge_bio hook
2926  * @wbc:        optional writeback control for io accounting
2927  * @page:       page to add to the bio
2928  * @pg_offset:  offset of the new bio or to check whether we are adding
2929  *              a contiguous page to the previous one
2930  * @size:       portion of page that we want to write
2931  * @offset:     starting offset in the page
2932  * @bio_ret:    must be valid pointer, newly allocated bio will be stored there
2933  * @end_io_func:     end_io callback for new bio
2934  * @mirror_num:      desired mirror to read/write
2935  * @prev_bio_flags:  flags of previous bio to see if we can merge the current one
2936  * @bio_flags:  flags of the current bio to see if we can merge them
2937  */
2938 static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
2939                               struct writeback_control *wbc,
2940                               struct page *page, u64 offset,
2941                               size_t size, unsigned long pg_offset,
2942                               struct bio **bio_ret,
2943                               bio_end_io_t end_io_func,
2944                               int mirror_num,
2945                               unsigned long prev_bio_flags,
2946                               unsigned long bio_flags,
2947                               bool force_bio_submit)
2948 {
2949         int ret = 0;
2950         struct bio *bio;
2951         size_t page_size = min_t(size_t, size, PAGE_SIZE);
2952         sector_t sector = offset >> 9;
2953
2954         ASSERT(bio_ret);
2955
2956         if (*bio_ret) {
2957                 bool contig;
2958                 bool can_merge = true;
2959
2960                 bio = *bio_ret;
2961                 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
2962                         contig = bio->bi_iter.bi_sector == sector;
2963                 else
2964                         contig = bio_end_sector(bio) == sector;
2965
2966                 ASSERT(tree->ops);
2967                 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
2968                         can_merge = false;
2969
2970                 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
2971                     force_bio_submit ||
2972                     bio_add_page(bio, page, page_size, pg_offset) < page_size) {
2973                         ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
2974                         if (ret < 0) {
2975                                 *bio_ret = NULL;
2976                                 return ret;
2977                         }
2978                         bio = NULL;
2979                 } else {
2980                         if (wbc)
2981                                 wbc_account_cgroup_owner(wbc, page, page_size);
2982                         return 0;
2983                 }
2984         }
2985
2986         bio = btrfs_bio_alloc(offset);
2987         bio_add_page(bio, page, page_size, pg_offset);
2988         bio->bi_end_io = end_io_func;
2989         bio->bi_private = tree;
2990         bio->bi_write_hint = page->mapping->host->i_write_hint;
2991         bio->bi_opf = opf;
2992         if (wbc) {
2993                 struct block_device *bdev;
2994
2995                 bdev = BTRFS_I(page->mapping->host)->root->fs_info->fs_devices->latest_bdev;
2996                 bio_set_dev(bio, bdev);
2997                 wbc_init_bio(wbc, bio);
2998                 wbc_account_cgroup_owner(wbc, page, page_size);
2999         }
3000
3001         *bio_ret = bio;
3002
3003         return ret;
3004 }
3005
3006 static void attach_extent_buffer_page(struct extent_buffer *eb,
3007                                       struct page *page)
3008 {
3009         if (!PagePrivate(page)) {
3010                 SetPagePrivate(page);
3011                 get_page(page);
3012                 set_page_private(page, (unsigned long)eb);
3013         } else {
3014                 WARN_ON(page->private != (unsigned long)eb);
3015         }
3016 }
3017
3018 void set_page_extent_mapped(struct page *page)
3019 {
3020         if (!PagePrivate(page)) {
3021                 SetPagePrivate(page);
3022                 get_page(page);
3023                 set_page_private(page, EXTENT_PAGE_PRIVATE);
3024         }
3025 }
3026
3027 static struct extent_map *
3028 __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
3029                  u64 start, u64 len, get_extent_t *get_extent,
3030                  struct extent_map **em_cached)
3031 {
3032         struct extent_map *em;
3033
3034         if (em_cached && *em_cached) {
3035                 em = *em_cached;
3036                 if (extent_map_in_tree(em) && start >= em->start &&
3037                     start < extent_map_end(em)) {
3038                         refcount_inc(&em->refs);
3039                         return em;
3040                 }
3041
3042                 free_extent_map(em);
3043                 *em_cached = NULL;
3044         }
3045
3046         em = get_extent(BTRFS_I(inode), page, pg_offset, start, len);
3047         if (em_cached && !IS_ERR_OR_NULL(em)) {
3048                 BUG_ON(*em_cached);
3049                 refcount_inc(&em->refs);
3050                 *em_cached = em;
3051         }
3052         return em;
3053 }
3054 /*
3055  * basic readpage implementation.  Locked extent state structs are inserted
3056  * into the tree that are removed when the IO is done (by the end_io
3057  * handlers)
3058  * XXX JDM: This needs looking at to ensure proper page locking
3059  * return 0 on success, otherwise return error
3060  */
3061 static int __do_readpage(struct extent_io_tree *tree,
3062                          struct page *page,
3063                          get_extent_t *get_extent,
3064                          struct extent_map **em_cached,
3065                          struct bio **bio, int mirror_num,
3066                          unsigned long *bio_flags, unsigned int read_flags,
3067                          u64 *prev_em_start)
3068 {
3069         struct inode *inode = page->mapping->host;
3070         u64 start = page_offset(page);
3071         const u64 end = start + PAGE_SIZE - 1;
3072         u64 cur = start;
3073         u64 extent_offset;
3074         u64 last_byte = i_size_read(inode);
3075         u64 block_start;
3076         u64 cur_end;
3077         struct extent_map *em;
3078         int ret = 0;
3079         int nr = 0;
3080         size_t pg_offset = 0;
3081         size_t iosize;
3082         size_t disk_io_size;
3083         size_t blocksize = inode->i_sb->s_blocksize;
3084         unsigned long this_bio_flag = 0;
3085
3086         set_page_extent_mapped(page);
3087
3088         if (!PageUptodate(page)) {
3089                 if (cleancache_get_page(page) == 0) {
3090                         BUG_ON(blocksize != PAGE_SIZE);
3091                         unlock_extent(tree, start, end);
3092                         goto out;
3093                 }
3094         }
3095
3096         if (page->index == last_byte >> PAGE_SHIFT) {
3097                 char *userpage;
3098                 size_t zero_offset = offset_in_page(last_byte);
3099
3100                 if (zero_offset) {
3101                         iosize = PAGE_SIZE - zero_offset;
3102                         userpage = kmap_atomic(page);
3103                         memset(userpage + zero_offset, 0, iosize);
3104                         flush_dcache_page(page);
3105                         kunmap_atomic(userpage);
3106                 }
3107         }
3108         while (cur <= end) {
3109                 bool force_bio_submit = false;
3110                 u64 offset;
3111
3112                 if (cur >= last_byte) {
3113                         char *userpage;
3114                         struct extent_state *cached = NULL;
3115
3116                         iosize = PAGE_SIZE - pg_offset;
3117                         userpage = kmap_atomic(page);
3118                         memset(userpage + pg_offset, 0, iosize);
3119                         flush_dcache_page(page);
3120                         kunmap_atomic(userpage);
3121                         set_extent_uptodate(tree, cur, cur + iosize - 1,
3122                                             &cached, GFP_NOFS);
3123                         unlock_extent_cached(tree, cur,
3124                                              cur + iosize - 1, &cached);
3125                         break;
3126                 }
3127                 em = __get_extent_map(inode, page, pg_offset, cur,
3128                                       end - cur + 1, get_extent, em_cached);
3129                 if (IS_ERR_OR_NULL(em)) {
3130                         SetPageError(page);
3131                         unlock_extent(tree, cur, end);
3132                         break;
3133                 }
3134                 extent_offset = cur - em->start;
3135                 BUG_ON(extent_map_end(em) <= cur);
3136                 BUG_ON(end < cur);
3137
3138                 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
3139                         this_bio_flag |= EXTENT_BIO_COMPRESSED;
3140                         extent_set_compress_type(&this_bio_flag,
3141                                                  em->compress_type);
3142                 }
3143
3144                 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3145                 cur_end = min(extent_map_end(em) - 1, end);
3146                 iosize = ALIGN(iosize, blocksize);
3147                 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
3148                         disk_io_size = em->block_len;
3149                         offset = em->block_start;
3150                 } else {
3151                         offset = em->block_start + extent_offset;
3152                         disk_io_size = iosize;
3153                 }
3154                 block_start = em->block_start;
3155                 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3156                         block_start = EXTENT_MAP_HOLE;
3157
3158                 /*
3159                  * If we have a file range that points to a compressed extent
3160                  * and it's followed by a consecutive file range that points to
3161                  * to the same compressed extent (possibly with a different
3162                  * offset and/or length, so it either points to the whole extent
3163                  * or only part of it), we must make sure we do not submit a
3164                  * single bio to populate the pages for the 2 ranges because
3165                  * this makes the compressed extent read zero out the pages
3166                  * belonging to the 2nd range. Imagine the following scenario:
3167                  *
3168                  *  File layout
3169                  *  [0 - 8K]                     [8K - 24K]
3170                  *    |                               |
3171                  *    |                               |
3172                  * points to extent X,         points to extent X,
3173                  * offset 4K, length of 8K     offset 0, length 16K
3174                  *
3175                  * [extent X, compressed length = 4K uncompressed length = 16K]
3176                  *
3177                  * If the bio to read the compressed extent covers both ranges,
3178                  * it will decompress extent X into the pages belonging to the
3179                  * first range and then it will stop, zeroing out the remaining
3180                  * pages that belong to the other range that points to extent X.
3181                  * So here we make sure we submit 2 bios, one for the first
3182                  * range and another one for the third range. Both will target
3183                  * the same physical extent from disk, but we can't currently
3184                  * make the compressed bio endio callback populate the pages
3185                  * for both ranges because each compressed bio is tightly
3186                  * coupled with a single extent map, and each range can have
3187                  * an extent map with a different offset value relative to the
3188                  * uncompressed data of our extent and different lengths. This
3189                  * is a corner case so we prioritize correctness over
3190                  * non-optimal behavior (submitting 2 bios for the same extent).
3191                  */
3192                 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3193                     prev_em_start && *prev_em_start != (u64)-1 &&
3194                     *prev_em_start != em->start)
3195                         force_bio_submit = true;
3196
3197                 if (prev_em_start)
3198                         *prev_em_start = em->start;
3199
3200                 free_extent_map(em);
3201                 em = NULL;
3202
3203                 /* we've found a hole, just zero and go on */
3204                 if (block_start == EXTENT_MAP_HOLE) {
3205                         char *userpage;
3206                         struct extent_state *cached = NULL;
3207
3208                         userpage = kmap_atomic(page);
3209                         memset(userpage + pg_offset, 0, iosize);
3210                         flush_dcache_page(page);
3211                         kunmap_atomic(userpage);
3212
3213                         set_extent_uptodate(tree, cur, cur + iosize - 1,
3214                                             &cached, GFP_NOFS);
3215                         unlock_extent_cached(tree, cur,
3216                                              cur + iosize - 1, &cached);
3217                         cur = cur + iosize;
3218                         pg_offset += iosize;
3219                         continue;
3220                 }
3221                 /* the get_extent function already copied into the page */
3222                 if (test_range_bit(tree, cur, cur_end,
3223                                    EXTENT_UPTODATE, 1, NULL)) {
3224                         check_page_uptodate(tree, page);
3225                         unlock_extent(tree, cur, cur + iosize - 1);
3226                         cur = cur + iosize;
3227                         pg_offset += iosize;
3228                         continue;
3229                 }
3230                 /* we have an inline extent but it didn't get marked up
3231                  * to date.  Error out
3232                  */
3233                 if (block_start == EXTENT_MAP_INLINE) {
3234                         SetPageError(page);
3235                         unlock_extent(tree, cur, cur + iosize - 1);
3236                         cur = cur + iosize;
3237                         pg_offset += iosize;
3238                         continue;
3239                 }
3240
3241                 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
3242                                          page, offset, disk_io_size,
3243                                          pg_offset, bio,
3244                                          end_bio_extent_readpage, mirror_num,
3245                                          *bio_flags,
3246                                          this_bio_flag,
3247                                          force_bio_submit);
3248                 if (!ret) {
3249                         nr++;
3250                         *bio_flags = this_bio_flag;
3251                 } else {
3252                         SetPageError(page);
3253                         unlock_extent(tree, cur, cur + iosize - 1);
3254                         goto out;
3255                 }
3256                 cur = cur + iosize;
3257                 pg_offset += iosize;
3258         }
3259 out:
3260         if (!nr) {
3261                 if (!PageError(page))
3262                         SetPageUptodate(page);
3263                 unlock_page(page);
3264         }
3265         return ret;
3266 }
3267
3268 static inline void contiguous_readpages(struct extent_io_tree *tree,
3269                                              struct page *pages[], int nr_pages,
3270                                              u64 start, u64 end,
3271                                              struct extent_map **em_cached,
3272                                              struct bio **bio,
3273                                              unsigned long *bio_flags,
3274                                              u64 *prev_em_start)
3275 {
3276         struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);
3277         int index;
3278
3279         btrfs_lock_and_flush_ordered_range(tree, inode, start, end, NULL);
3280
3281         for (index = 0; index < nr_pages; index++) {
3282                 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
3283                                 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
3284                 put_page(pages[index]);
3285         }
3286 }
3287
3288 static int __extent_read_full_page(struct extent_io_tree *tree,
3289                                    struct page *page,
3290                                    get_extent_t *get_extent,
3291                                    struct bio **bio, int mirror_num,
3292                                    unsigned long *bio_flags,
3293                                    unsigned int read_flags)
3294 {
3295         struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
3296         u64 start = page_offset(page);
3297         u64 end = start + PAGE_SIZE - 1;
3298         int ret;
3299
3300         btrfs_lock_and_flush_ordered_range(tree, inode, start, end, NULL);
3301
3302         ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
3303                             bio_flags, read_flags, NULL);
3304         return ret;
3305 }
3306
3307 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
3308                             get_extent_t *get_extent, int mirror_num)
3309 {
3310         struct bio *bio = NULL;
3311         unsigned long bio_flags = 0;
3312         int ret;
3313
3314         ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
3315                                       &bio_flags, 0);
3316         if (bio)
3317                 ret = submit_one_bio(bio, mirror_num, bio_flags);
3318         return ret;
3319 }
3320
3321 static void update_nr_written(struct writeback_control *wbc,
3322                               unsigned long nr_written)
3323 {
3324         wbc->nr_to_write -= nr_written;
3325 }
3326
3327 /*
3328  * helper for __extent_writepage, doing all of the delayed allocation setup.
3329  *
3330  * This returns 1 if btrfs_run_delalloc_range function did all the work required
3331  * to write the page (copy into inline extent).  In this case the IO has
3332  * been started and the page is already unlocked.
3333  *
3334  * This returns 0 if all went well (page still locked)
3335  * This returns < 0 if there were errors (page still locked)
3336  */
3337 static noinline_for_stack int writepage_delalloc(struct inode *inode,
3338                 struct page *page, struct writeback_control *wbc,
3339                 u64 delalloc_start, unsigned long *nr_written)
3340 {
3341         u64 page_end = delalloc_start + PAGE_SIZE - 1;
3342         bool found;
3343         u64 delalloc_to_write = 0;
3344         u64 delalloc_end = 0;
3345         int ret;
3346         int page_started = 0;
3347
3348
3349         while (delalloc_end < page_end) {
3350                 found = find_lock_delalloc_range(inode, page,
3351                                                &delalloc_start,
3352                                                &delalloc_end);
3353                 if (!found) {
3354                         delalloc_start = delalloc_end + 1;
3355                         continue;
3356                 }
3357                 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3358                                 delalloc_end, &page_started, nr_written, wbc);
3359                 if (ret) {
3360                         SetPageError(page);
3361                         /*
3362                          * btrfs_run_delalloc_range should return < 0 for error
3363                          * but just in case, we use > 0 here meaning the IO is
3364                          * started, so we don't want to return > 0 unless
3365                          * things are going well.
3366                          */
3367                         ret = ret < 0 ? ret : -EIO;
3368                         goto done;
3369                 }
3370                 /*
3371                  * delalloc_end is already one less than the total length, so
3372                  * we don't subtract one from PAGE_SIZE
3373                  */
3374                 delalloc_to_write += (delalloc_end - delalloc_start +
3375                                       PAGE_SIZE) >> PAGE_SHIFT;
3376                 delalloc_start = delalloc_end + 1;
3377         }
3378         if (wbc->nr_to_write < delalloc_to_write) {
3379                 int thresh = 8192;
3380
3381                 if (delalloc_to_write < thresh * 2)
3382                         thresh = delalloc_to_write;
3383                 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3384                                          thresh);
3385         }
3386
3387         /* did the fill delalloc function already unlock and start
3388          * the IO?
3389          */
3390         if (page_started) {
3391                 /*
3392                  * we've unlocked the page, so we can't update
3393                  * the mapping's writeback index, just update
3394                  * nr_to_write.
3395                  */
3396                 wbc->nr_to_write -= *nr_written;
3397                 return 1;
3398         }
3399
3400         ret = 0;
3401
3402 done:
3403         return ret;
3404 }
3405
3406 /*
3407  * helper for __extent_writepage.  This calls the writepage start hooks,
3408  * and does the loop to map the page into extents and bios.
3409  *
3410  * We return 1 if the IO is started and the page is unlocked,
3411  * 0 if all went well (page still locked)
3412  * < 0 if there were errors (page still locked)
3413  */
3414 static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3415                                  struct page *page,
3416                                  struct writeback_control *wbc,
3417                                  struct extent_page_data *epd,
3418                                  loff_t i_size,
3419                                  unsigned long nr_written,
3420                                  int *nr_ret)
3421 {
3422         struct extent_io_tree *tree = epd->tree;
3423         u64 start = page_offset(page);
3424         u64 page_end = start + PAGE_SIZE - 1;
3425         u64 end;
3426         u64 cur = start;
3427         u64 extent_offset;
3428         u64 block_start;
3429         u64 iosize;
3430         struct extent_map *em;
3431         size_t pg_offset = 0;
3432         size_t blocksize;
3433         int ret = 0;
3434         int nr = 0;
3435         const unsigned int write_flags = wbc_to_write_flags(wbc);
3436         bool compressed;
3437
3438         ret = btrfs_writepage_cow_fixup(page, start, page_end);
3439         if (ret) {
3440                 /* Fixup worker will requeue */
3441                 if (ret == -EBUSY)
3442                         wbc->pages_skipped++;
3443                 else
3444                         redirty_page_for_writepage(wbc, page);
3445
3446                 update_nr_written(wbc, nr_written);
3447                 unlock_page(page);
3448                 return 1;
3449         }
3450
3451         /*
3452          * we don't want to touch the inode after unlocking the page,
3453          * so we update the mapping writeback index now
3454          */
3455         update_nr_written(wbc, nr_written + 1);
3456
3457         end = page_end;
3458         blocksize = inode->i_sb->s_blocksize;
3459
3460         while (cur <= end) {
3461                 u64 em_end;
3462                 u64 offset;
3463
3464                 if (cur >= i_size) {
3465                         btrfs_writepage_endio_finish_ordered(page, cur,
3466                                                              page_end, 1);
3467                         break;
3468                 }
3469                 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur,
3470                                       end - cur + 1);
3471                 if (IS_ERR_OR_NULL(em)) {
3472                         SetPageError(page);
3473                         ret = PTR_ERR_OR_ZERO(em);
3474                         break;
3475                 }
3476
3477                 extent_offset = cur - em->start;
3478                 em_end = extent_map_end(em);
3479                 BUG_ON(em_end <= cur);
3480                 BUG_ON(end < cur);
3481                 iosize = min(em_end - cur, end - cur + 1);
3482                 iosize = ALIGN(iosize, blocksize);
3483                 offset = em->block_start + extent_offset;
3484                 block_start = em->block_start;
3485                 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3486                 free_extent_map(em);
3487                 em = NULL;
3488
3489                 /*
3490                  * compressed and inline extents are written through other
3491                  * paths in the FS
3492                  */
3493                 if (compressed || block_start == EXTENT_MAP_HOLE ||
3494                     block_start == EXTENT_MAP_INLINE) {
3495                         if (compressed)
3496                                 nr++;
3497                         else
3498                                 btrfs_writepage_endio_finish_ordered(page, cur,
3499                                                         cur + iosize - 1, 1);
3500                         cur += iosize;
3501                         pg_offset += iosize;
3502                         continue;
3503                 }
3504
3505                 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
3506                 if (!PageWriteback(page)) {
3507                         btrfs_err(BTRFS_I(inode)->root->fs_info,
3508                                    "page %lu not writeback, cur %llu end %llu",
3509                                page->index, cur, end);
3510                 }
3511
3512                 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
3513                                          page, offset, iosize, pg_offset,
3514                                          &epd->bio,
3515                                          end_bio_extent_writepage,
3516                                          0, 0, 0, false);
3517                 if (ret) {
3518                         SetPageError(page);
3519                         if (PageWriteback(page))
3520                                 end_page_writeback(page);
3521                 }
3522
3523                 cur = cur + iosize;
3524                 pg_offset += iosize;
3525                 nr++;
3526         }
3527         *nr_ret = nr;
3528         return ret;
3529 }
3530
3531 /*
3532  * the writepage semantics are similar to regular writepage.  extent
3533  * records are inserted to lock ranges in the tree, and as dirty areas
3534  * are found, they are marked writeback.  Then the lock bits are removed
3535  * and the end_io handler clears the writeback ranges
3536  *
3537  * Return 0 if everything goes well.
3538  * Return <0 for error.
3539  */
3540 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3541                               struct extent_page_data *epd)
3542 {
3543         struct inode *inode = page->mapping->host;
3544         u64 start = page_offset(page);
3545         u64 page_end = start + PAGE_SIZE - 1;
3546         int ret;
3547         int nr = 0;
3548         size_t pg_offset;
3549         loff_t i_size = i_size_read(inode);
3550         unsigned long end_index = i_size >> PAGE_SHIFT;
3551         unsigned long nr_written = 0;
3552
3553         trace___extent_writepage(page, inode, wbc);
3554
3555         WARN_ON(!PageLocked(page));
3556
3557         ClearPageError(page);
3558
3559         pg_offset = offset_in_page(i_size);
3560         if (page->index > end_index ||
3561            (page->index == end_index && !pg_offset)) {
3562                 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
3563                 unlock_page(page);
3564                 return 0;
3565         }
3566
3567         if (page->index == end_index) {
3568                 char *userpage;
3569
3570                 userpage = kmap_atomic(page);
3571                 memset(userpage + pg_offset, 0,
3572                        PAGE_SIZE - pg_offset);
3573                 kunmap_atomic(userpage);
3574                 flush_dcache_page(page);
3575         }
3576
3577         set_page_extent_mapped(page);
3578
3579         if (!epd->extent_locked) {
3580                 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
3581                 if (ret == 1)
3582                         return 0;
3583                 if (ret)
3584                         goto done;
3585         }
3586
3587         ret = __extent_writepage_io(inode, page, wbc, epd,
3588                                     i_size, nr_written, &nr);
3589         if (ret == 1)
3590                 return 0;
3591
3592 done:
3593         if (nr == 0) {
3594                 /* make sure the mapping tag for page dirty gets cleared */
3595                 set_page_writeback(page);
3596                 end_page_writeback(page);
3597         }
3598         if (PageError(page)) {
3599                 ret = ret < 0 ? ret : -EIO;
3600                 end_extent_writepage(page, ret, start, page_end);
3601         }
3602         unlock_page(page);
3603         ASSERT(ret <= 0);
3604         return ret;
3605 }
3606
3607 void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3608 {
3609         wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3610                        TASK_UNINTERRUPTIBLE);
3611 }
3612
3613 static void end_extent_buffer_writeback(struct extent_buffer *eb)
3614 {
3615         clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3616         smp_mb__after_atomic();
3617         wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3618 }
3619
3620 /*
3621  * Lock eb pages and flush the bio if we can't the locks
3622  *
3623  * Return  0 if nothing went wrong
3624  * Return >0 is same as 0, except bio is not submitted
3625  * Return <0 if something went wrong, no page is locked
3626  */
3627 static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
3628                           struct extent_page_data *epd)
3629 {
3630         struct btrfs_fs_info *fs_info = eb->fs_info;
3631         int i, num_pages, failed_page_nr;
3632         int flush = 0;
3633         int ret = 0;
3634
3635         if (!btrfs_try_tree_write_lock(eb)) {
3636                 ret = flush_write_bio(epd);
3637                 if (ret < 0)
3638                         return ret;
3639                 flush = 1;
3640                 btrfs_tree_lock(eb);
3641         }
3642
3643         if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3644                 btrfs_tree_unlock(eb);
3645                 if (!epd->sync_io)
3646                         return 0;
3647                 if (!flush) {
3648                         ret = flush_write_bio(epd);
3649                         if (ret < 0)
3650                                 return ret;
3651                         flush = 1;
3652                 }
3653                 while (1) {
3654                         wait_on_extent_buffer_writeback(eb);
3655                         btrfs_tree_lock(eb);
3656                         if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3657                                 break;
3658                         btrfs_tree_unlock(eb);
3659                 }
3660         }
3661
3662         /*
3663          * We need to do this to prevent races in people who check if the eb is
3664          * under IO since we can end up having no IO bits set for a short period
3665          * of time.
3666          */
3667         spin_lock(&eb->refs_lock);
3668         if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3669                 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3670                 spin_unlock(&eb->refs_lock);
3671                 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3672                 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3673                                          -eb->len,
3674                                          fs_info->dirty_metadata_batch);
3675                 ret = 1;
3676         } else {
3677                 spin_unlock(&eb->refs_lock);
3678         }
3679
3680         btrfs_tree_unlock(eb);
3681
3682         if (!ret)
3683                 return ret;
3684
3685         num_pages = num_extent_pages(eb);
3686         for (i = 0; i < num_pages; i++) {
3687                 struct page *p = eb->pages[i];
3688
3689                 if (!trylock_page(p)) {
3690                         if (!flush) {
3691                                 int err;
3692
3693                                 err = flush_write_bio(epd);
3694                                 if (err < 0) {
3695                                         ret = err;
3696                                         failed_page_nr = i;
3697                                         goto err_unlock;
3698                                 }
3699                                 flush = 1;
3700                         }
3701                         lock_page(p);
3702                 }
3703         }
3704
3705         return ret;
3706 err_unlock:
3707         /* Unlock already locked pages */
3708         for (i = 0; i < failed_page_nr; i++)
3709                 unlock_page(eb->pages[i]);
3710         /*
3711          * Clear EXTENT_BUFFER_WRITEBACK and wake up anyone waiting on it.
3712          * Also set back EXTENT_BUFFER_DIRTY so future attempts to this eb can
3713          * be made and undo everything done before.
3714          */
3715         btrfs_tree_lock(eb);
3716         spin_lock(&eb->refs_lock);
3717         set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3718         end_extent_buffer_writeback(eb);
3719         spin_unlock(&eb->refs_lock);
3720         percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, eb->len,
3721                                  fs_info->dirty_metadata_batch);
3722         btrfs_clear_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3723         btrfs_tree_unlock(eb);
3724         return ret;
3725 }
3726
3727 static void set_btree_ioerr(struct page *page)
3728 {
3729         struct extent_buffer *eb = (struct extent_buffer *)page->private;
3730         struct btrfs_fs_info *fs_info;
3731
3732         SetPageError(page);
3733         if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3734                 return;
3735
3736         /*
3737          * If we error out, we should add back the dirty_metadata_bytes
3738          * to make it consistent.
3739          */
3740         fs_info = eb->fs_info;
3741         percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3742                                  eb->len, fs_info->dirty_metadata_batch);
3743
3744         /*
3745          * If writeback for a btree extent that doesn't belong to a log tree
3746          * failed, increment the counter transaction->eb_write_errors.
3747          * We do this because while the transaction is running and before it's
3748          * committing (when we call filemap_fdata[write|wait]_range against
3749          * the btree inode), we might have
3750          * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3751          * returns an error or an error happens during writeback, when we're
3752          * committing the transaction we wouldn't know about it, since the pages
3753          * can be no longer dirty nor marked anymore for writeback (if a
3754          * subsequent modification to the extent buffer didn't happen before the
3755          * transaction commit), which makes filemap_fdata[write|wait]_range not
3756          * able to find the pages tagged with SetPageError at transaction
3757          * commit time. So if this happens we must abort the transaction,
3758          * otherwise we commit a super block with btree roots that point to
3759          * btree nodes/leafs whose content on disk is invalid - either garbage
3760          * or the content of some node/leaf from a past generation that got
3761          * cowed or deleted and is no longer valid.
3762          *
3763          * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3764          * not be enough - we need to distinguish between log tree extents vs
3765          * non-log tree extents, and the next filemap_fdatawait_range() call
3766          * will catch and clear such errors in the mapping - and that call might
3767          * be from a log sync and not from a transaction commit. Also, checking
3768          * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3769          * not done and would not be reliable - the eb might have been released
3770          * from memory and reading it back again means that flag would not be
3771          * set (since it's a runtime flag, not persisted on disk).
3772          *
3773          * Using the flags below in the btree inode also makes us achieve the
3774          * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3775          * writeback for all dirty pages and before filemap_fdatawait_range()
3776          * is called, the writeback for all dirty pages had already finished
3777          * with errors - because we were not using AS_EIO/AS_ENOSPC,
3778          * filemap_fdatawait_range() would return success, as it could not know
3779          * that writeback errors happened (the pages were no longer tagged for
3780          * writeback).
3781          */
3782         switch (eb->log_index) {
3783         case -1:
3784                 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
3785                 break;
3786         case 0:
3787                 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
3788                 break;
3789         case 1:
3790                 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
3791                 break;
3792         default:
3793                 BUG(); /* unexpected, logic error */
3794         }
3795 }
3796
3797 static void end_bio_extent_buffer_writepage(struct bio *bio)
3798 {
3799         struct bio_vec *bvec;
3800         struct extent_buffer *eb;
3801         int done;
3802         struct bvec_iter_all iter_all;
3803
3804         ASSERT(!bio_flagged(bio, BIO_CLONED));
3805         bio_for_each_segment_all(bvec, bio, iter_all) {
3806                 struct page *page = bvec->bv_page;
3807
3808                 eb = (struct extent_buffer *)page->private;
3809                 BUG_ON(!eb);
3810                 done = atomic_dec_and_test(&eb->io_pages);
3811
3812                 if (bio->bi_status ||
3813                     test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
3814                         ClearPageUptodate(page);
3815                         set_btree_ioerr(page);
3816                 }
3817
3818                 end_page_writeback(page);
3819
3820                 if (!done)
3821                         continue;
3822
3823                 end_extent_buffer_writeback(eb);
3824         }
3825
3826         bio_put(bio);
3827 }
3828
3829 static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
3830                         struct writeback_control *wbc,
3831                         struct extent_page_data *epd)
3832 {
3833         struct btrfs_fs_info *fs_info = eb->fs_info;
3834         struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
3835         u64 offset = eb->start;
3836         u32 nritems;
3837         int i, num_pages;
3838         unsigned long start, end;
3839         unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
3840         int ret = 0;
3841
3842         clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
3843         num_pages = num_extent_pages(eb);
3844         atomic_set(&eb->io_pages, num_pages);
3845
3846         /* set btree blocks beyond nritems with 0 to avoid stale content. */
3847         nritems = btrfs_header_nritems(eb);
3848         if (btrfs_header_level(eb) > 0) {
3849                 end = btrfs_node_key_ptr_offset(nritems);
3850
3851                 memzero_extent_buffer(eb, end, eb->len - end);
3852         } else {
3853                 /*
3854                  * leaf:
3855                  * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3856                  */
3857                 start = btrfs_item_nr_offset(nritems);
3858                 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
3859                 memzero_extent_buffer(eb, start, end - start);
3860         }
3861
3862         for (i = 0; i < num_pages; i++) {
3863                 struct page *p = eb->pages[i];
3864
3865                 clear_page_dirty_for_io(p);
3866                 set_page_writeback(p);
3867                 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
3868                                          p, offset, PAGE_SIZE, 0,
3869                                          &epd->bio,
3870                                          end_bio_extent_buffer_writepage,
3871                                          0, 0, 0, false);
3872                 if (ret) {
3873                         set_btree_ioerr(p);
3874                         if (PageWriteback(p))
3875                                 end_page_writeback(p);
3876                         if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3877                                 end_extent_buffer_writeback(eb);
3878                         ret = -EIO;
3879                         break;
3880                 }
3881                 offset += PAGE_SIZE;
3882                 update_nr_written(wbc, 1);
3883                 unlock_page(p);
3884         }
3885
3886         if (unlikely(ret)) {
3887                 for (; i < num_pages; i++) {
3888                         struct page *p = eb->pages[i];
3889                         clear_page_dirty_for_io(p);
3890                         unlock_page(p);
3891                 }
3892         }
3893
3894         return ret;
3895 }
3896
3897 int btree_write_cache_pages(struct address_space *mapping,
3898                                    struct writeback_control *wbc)
3899 {
3900         struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3901         struct extent_buffer *eb, *prev_eb = NULL;
3902         struct extent_page_data epd = {
3903                 .bio = NULL,
3904                 .tree = tree,
3905                 .extent_locked = 0,
3906                 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3907         };
3908         int ret = 0;
3909         int done = 0;
3910         int nr_to_write_done = 0;
3911         struct pagevec pvec;
3912         int nr_pages;
3913         pgoff_t index;
3914         pgoff_t end;            /* Inclusive */
3915         int scanned = 0;
3916         xa_mark_t tag;
3917
3918         pagevec_init(&pvec);
3919         if (wbc->range_cyclic) {
3920                 index = mapping->writeback_index; /* Start from prev offset */
3921                 end = -1;
3922         } else {
3923                 index = wbc->range_start >> PAGE_SHIFT;
3924                 end = wbc->range_end >> PAGE_SHIFT;
3925                 scanned = 1;
3926         }
3927         if (wbc->sync_mode == WB_SYNC_ALL)
3928                 tag = PAGECACHE_TAG_TOWRITE;
3929         else
3930                 tag = PAGECACHE_TAG_DIRTY;
3931 retry:
3932         if (wbc->sync_mode == WB_SYNC_ALL)
3933                 tag_pages_for_writeback(mapping, index, end);
3934         while (!done && !nr_to_write_done && (index <= end) &&
3935                (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
3936                         tag))) {
3937                 unsigned i;
3938
3939                 scanned = 1;
3940                 for (i = 0; i < nr_pages; i++) {
3941                         struct page *page = pvec.pages[i];
3942
3943                         if (!PagePrivate(page))
3944                                 continue;
3945
3946                         spin_lock(&mapping->private_lock);
3947                         if (!PagePrivate(page)) {
3948                                 spin_unlock(&mapping->private_lock);
3949                                 continue;
3950                         }
3951
3952                         eb = (struct extent_buffer *)page->private;
3953
3954                         /*
3955                          * Shouldn't happen and normally this would be a BUG_ON
3956                          * but no sense in crashing the users box for something
3957                          * we can survive anyway.
3958                          */
3959                         if (WARN_ON(!eb)) {
3960                                 spin_unlock(&mapping->private_lock);
3961                                 continue;
3962                         }
3963
3964                         if (eb == prev_eb) {
3965                                 spin_unlock(&mapping->private_lock);
3966                                 continue;
3967                         }
3968
3969                         ret = atomic_inc_not_zero(&eb->refs);
3970                         spin_unlock(&mapping->private_lock);
3971                         if (!ret)
3972                                 continue;
3973
3974                         prev_eb = eb;
3975                         ret = lock_extent_buffer_for_io(eb, &epd);
3976                         if (!ret) {
3977                                 free_extent_buffer(eb);
3978                                 continue;
3979                         } else if (ret < 0) {
3980                                 done = 1;
3981                                 free_extent_buffer(eb);
3982                                 break;
3983                         }
3984
3985                         ret = write_one_eb(eb, wbc, &epd);
3986                         if (ret) {
3987                                 done = 1;
3988                                 free_extent_buffer(eb);
3989                                 break;
3990                         }
3991                         free_extent_buffer(eb);
3992
3993                         /*
3994                          * the filesystem may choose to bump up nr_to_write.
3995                          * We have to make sure to honor the new nr_to_write
3996                          * at any time
3997                          */
3998                         nr_to_write_done = wbc->nr_to_write <= 0;
3999                 }
4000                 pagevec_release(&pvec);
4001                 cond_resched();
4002         }
4003         if (!scanned && !done) {
4004                 /*
4005                  * We hit the last page and there is more work to be done: wrap
4006                  * back to the start of the file
4007                  */
4008                 scanned = 1;
4009                 index = 0;
4010                 goto retry;
4011         }
4012         ASSERT(ret <= 0);
4013         if (ret < 0) {
4014                 end_write_bio(&epd, ret);
4015                 return ret;
4016         }
4017         ret = flush_write_bio(&epd);
4018         return ret;
4019 }
4020
4021 /**
4022  * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
4023  * @mapping: address space structure to write
4024  * @wbc: subtract the number of written pages from *@wbc->nr_to_write
4025  * @data: data passed to __extent_writepage function
4026  *
4027  * If a page is already under I/O, write_cache_pages() skips it, even
4028  * if it's dirty.  This is desirable behaviour for memory-cleaning writeback,
4029  * but it is INCORRECT for data-integrity system calls such as fsync().  fsync()
4030  * and msync() need to guarantee that all the data which was dirty at the time
4031  * the call was made get new I/O started against them.  If wbc->sync_mode is
4032  * WB_SYNC_ALL then we were called for data integrity and we must wait for
4033  * existing IO to complete.
4034  */
4035 static int extent_write_cache_pages(struct address_space *mapping,
4036                              struct writeback_control *wbc,
4037                              struct extent_page_data *epd)
4038 {
4039         struct inode *inode = mapping->host;
4040         int ret = 0;
4041         int done = 0;
4042         int nr_to_write_done = 0;
4043         struct pagevec pvec;
4044         int nr_pages;
4045         pgoff_t index;
4046         pgoff_t end;            /* Inclusive */
4047         pgoff_t done_index;
4048         int range_whole = 0;
4049         int scanned = 0;
4050         xa_mark_t tag;
4051
4052         /*
4053          * We have to hold onto the inode so that ordered extents can do their
4054          * work when the IO finishes.  The alternative to this is failing to add
4055          * an ordered extent if the igrab() fails there and that is a huge pain
4056          * to deal with, so instead just hold onto the inode throughout the
4057          * writepages operation.  If it fails here we are freeing up the inode
4058          * anyway and we'd rather not waste our time writing out stuff that is
4059          * going to be truncated anyway.
4060          */
4061         if (!igrab(inode))
4062                 return 0;
4063
4064         pagevec_init(&pvec);
4065         if (wbc->range_cyclic) {
4066                 index = mapping->writeback_index; /* Start from prev offset */
4067                 end = -1;
4068         } else {
4069                 index = wbc->range_start >> PAGE_SHIFT;
4070                 end = wbc->range_end >> PAGE_SHIFT;
4071                 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
4072                         range_whole = 1;
4073                 scanned = 1;
4074         }
4075
4076         /*
4077          * We do the tagged writepage as long as the snapshot flush bit is set
4078          * and we are the first one who do the filemap_flush() on this inode.
4079          *
4080          * The nr_to_write == LONG_MAX is needed to make sure other flushers do
4081          * not race in and drop the bit.
4082          */
4083         if (range_whole && wbc->nr_to_write == LONG_MAX &&
4084             test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
4085                                &BTRFS_I(inode)->runtime_flags))
4086                 wbc->tagged_writepages = 1;
4087
4088         if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
4089                 tag = PAGECACHE_TAG_TOWRITE;
4090         else
4091                 tag = PAGECACHE_TAG_DIRTY;
4092 retry:
4093         if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
4094                 tag_pages_for_writeback(mapping, index, end);
4095         done_index = index;
4096         while (!done && !nr_to_write_done && (index <= end) &&
4097                         (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
4098                                                 &index, end, tag))) {
4099                 unsigned i;
4100
4101                 scanned = 1;
4102                 for (i = 0; i < nr_pages; i++) {
4103                         struct page *page = pvec.pages[i];
4104
4105                         done_index = page->index + 1;
4106                         /*
4107                          * At this point we hold neither the i_pages lock nor
4108                          * the page lock: the page may be truncated or
4109                          * invalidated (changing page->mapping to NULL),
4110                          * or even swizzled back from swapper_space to
4111                          * tmpfs file mapping
4112                          */
4113                         if (!trylock_page(page)) {
4114                                 ret = flush_write_bio(epd);
4115                                 BUG_ON(ret < 0);
4116                                 lock_page(page);
4117                         }
4118
4119                         if (unlikely(page->mapping != mapping)) {
4120                                 unlock_page(page);
4121                                 continue;
4122                         }
4123
4124                         if (wbc->sync_mode != WB_SYNC_NONE) {
4125                                 if (PageWriteback(page)) {
4126                                         ret = flush_write_bio(epd);
4127                                         BUG_ON(ret < 0);
4128                                 }
4129                                 wait_on_page_writeback(page);
4130                         }
4131
4132                         if (PageWriteback(page) ||
4133                             !clear_page_dirty_for_io(page)) {
4134                                 unlock_page(page);
4135                                 continue;
4136                         }
4137
4138                         ret = __extent_writepage(page, wbc, epd);
4139                         if (ret < 0) {
4140                                 done = 1;
4141                                 break;
4142                         }
4143
4144                         /*
4145                          * the filesystem may choose to bump up nr_to_write.
4146                          * We have to make sure to honor the new nr_to_write
4147                          * at any time
4148                          */
4149                         nr_to_write_done = wbc->nr_to_write <= 0;
4150                 }
4151                 pagevec_release(&pvec);
4152                 cond_resched();
4153         }
4154         if (!scanned && !done) {
4155                 /*
4156                  * We hit the last page and there is more work to be done: wrap
4157                  * back to the start of the file
4158                  */
4159                 scanned = 1;
4160                 index = 0;
4161                 goto retry;
4162         }
4163
4164         if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4165                 mapping->writeback_index = done_index;
4166
4167         btrfs_add_delayed_iput(inode);
4168         return ret;
4169 }
4170
4171 int extent_write_full_page(struct page *page, struct writeback_control *wbc)
4172 {
4173         int ret;
4174         struct extent_page_data epd = {
4175                 .bio = NULL,
4176                 .tree = &BTRFS_I(page->mapping->host)->io_tree,
4177                 .extent_locked = 0,
4178                 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
4179         };
4180
4181         ret = __extent_writepage(page, wbc, &epd);
4182         ASSERT(ret <= 0);
4183         if (ret < 0) {
4184                 end_write_bio(&epd, ret);
4185                 return ret;
4186         }
4187
4188         ret = flush_write_bio(&epd);
4189         ASSERT(ret <= 0);
4190         return ret;
4191 }
4192
4193 int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
4194                               int mode)
4195 {
4196         int ret = 0;
4197         struct address_space *mapping = inode->i_mapping;
4198         struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
4199         struct page *page;
4200         unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4201                 PAGE_SHIFT;
4202
4203         struct extent_page_data epd = {
4204                 .bio = NULL,
4205                 .tree = tree,
4206                 .extent_locked = 1,
4207                 .sync_io = mode == WB_SYNC_ALL,
4208         };
4209         struct writeback_control wbc_writepages = {
4210                 .sync_mode      = mode,
4211                 .nr_to_write    = nr_pages * 2,
4212                 .range_start    = start,
4213                 .range_end      = end + 1,
4214                 /* We're called from an async helper function */
4215                 .punt_to_cgroup = 1,
4216                 .no_cgroup_owner = 1,
4217         };
4218
4219         wbc_attach_fdatawrite_inode(&wbc_writepages, inode);
4220         while (start <= end) {
4221                 page = find_get_page(mapping, start >> PAGE_SHIFT);
4222                 if (clear_page_dirty_for_io(page))
4223                         ret = __extent_writepage(page, &wbc_writepages, &epd);
4224                 else {
4225                         btrfs_writepage_endio_finish_ordered(page, start,
4226                                                     start + PAGE_SIZE - 1, 1);
4227                         unlock_page(page);
4228                 }
4229                 put_page(page);
4230                 start += PAGE_SIZE;
4231         }
4232
4233         ASSERT(ret <= 0);
4234         if (ret == 0)
4235                 ret = flush_write_bio(&epd);
4236         else
4237                 end_write_bio(&epd, ret);
4238
4239         wbc_detach_inode(&wbc_writepages);
4240         return ret;
4241 }
4242
4243 int extent_writepages(struct address_space *mapping,
4244                       struct writeback_control *wbc)
4245 {
4246         int ret = 0;
4247         struct extent_page_data epd = {
4248                 .bio = NULL,
4249                 .tree = &BTRFS_I(mapping->host)->io_tree,
4250                 .extent_locked = 0,
4251                 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
4252         };
4253
4254         ret = extent_write_cache_pages(mapping, wbc, &epd);
4255         ASSERT(ret <= 0);
4256         if (ret < 0) {
4257                 end_write_bio(&epd, ret);
4258                 return ret;
4259         }
4260         ret = flush_write_bio(&epd);
4261         return ret;
4262 }
4263
4264 int extent_readpages(struct address_space *mapping, struct list_head *pages,
4265                      unsigned nr_pages)
4266 {
4267         struct bio *bio = NULL;
4268         unsigned long bio_flags = 0;
4269         struct page *pagepool[16];
4270         struct extent_map *em_cached = NULL;
4271         struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
4272         int nr = 0;
4273         u64 prev_em_start = (u64)-1;
4274
4275         while (!list_empty(pages)) {
4276                 u64 contig_end = 0;
4277
4278                 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
4279                         struct page *page = lru_to_page(pages);
4280
4281                         prefetchw(&page->flags);
4282                         list_del(&page->lru);
4283                         if (add_to_page_cache_lru(page, mapping, page->index,
4284                                                 readahead_gfp_mask(mapping))) {
4285                                 put_page(page);
4286                                 break;
4287                         }
4288
4289                         pagepool[nr++] = page;
4290                         contig_end = page_offset(page) + PAGE_SIZE - 1;
4291                 }
4292
4293                 if (nr) {
4294                         u64 contig_start = page_offset(pagepool[0]);
4295
4296                         ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4297
4298                         contiguous_readpages(tree, pagepool, nr, contig_start,
4299                                      contig_end, &em_cached, &bio, &bio_flags,
4300                                      &prev_em_start);
4301                 }
4302         }
4303
4304         if (em_cached)
4305                 free_extent_map(em_cached);
4306
4307         if (bio)
4308                 return submit_one_bio(bio, 0, bio_flags);
4309         return 0;
4310 }
4311
4312 /*
4313  * basic invalidatepage code, this waits on any locked or writeback
4314  * ranges corresponding to the page, and then deletes any extent state
4315  * records from the tree
4316  */
4317 int extent_invalidatepage(struct extent_io_tree *tree,
4318                           struct page *page, unsigned long offset)
4319 {
4320         struct extent_state *cached_state = NULL;
4321         u64 start = page_offset(page);
4322         u64 end = start + PAGE_SIZE - 1;
4323         size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4324
4325         start += ALIGN(offset, blocksize);
4326         if (start > end)
4327                 return 0;
4328
4329         lock_extent_bits(tree, start, end, &cached_state);
4330         wait_on_page_writeback(page);
4331         clear_extent_bit(tree, start, end, EXTENT_LOCKED | EXTENT_DELALLOC |
4332                          EXTENT_DO_ACCOUNTING, 1, 1, &cached_state);
4333         return 0;
4334 }
4335
4336 /*
4337  * a helper for releasepage, this tests for areas of the page that
4338  * are locked or under IO and drops the related state bits if it is safe
4339  * to drop the page.
4340  */
4341 static int try_release_extent_state(struct extent_io_tree *tree,
4342                                     struct page *page, gfp_t mask)
4343 {
4344         u64 start = page_offset(page);
4345         u64 end = start + PAGE_SIZE - 1;
4346         int ret = 1;
4347
4348         if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
4349                 ret = 0;
4350         } else {
4351                 /*
4352                  * at this point we can safely clear everything except the
4353                  * locked bit and the nodatasum bit
4354                  */
4355                 ret = __clear_extent_bit(tree, start, end,
4356                                  ~(EXTENT_LOCKED | EXTENT_NODATASUM),
4357                                  0, 0, NULL, mask, NULL);
4358
4359                 /* if clear_extent_bit failed for enomem reasons,
4360                  * we can't allow the release to continue.
4361                  */
4362                 if (ret < 0)
4363                         ret = 0;
4364                 else
4365                         ret = 1;
4366         }
4367         return ret;
4368 }
4369
4370 /*
4371  * a helper for releasepage.  As long as there are no locked extents
4372  * in the range corresponding to the page, both state records and extent
4373  * map records are removed
4374  */
4375 int try_release_extent_mapping(struct page *page, gfp_t mask)
4376 {
4377         struct extent_map *em;
4378         u64 start = page_offset(page);
4379         u64 end = start + PAGE_SIZE - 1;
4380         struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4381         struct extent_io_tree *tree = &btrfs_inode->io_tree;
4382         struct extent_map_tree *map = &btrfs_inode->extent_tree;
4383
4384         if (gfpflags_allow_blocking(mask) &&
4385             page->mapping->host->i_size > SZ_16M) {
4386                 u64 len;
4387                 while (start <= end) {
4388                         len = end - start + 1;
4389                         write_lock(&map->lock);
4390                         em = lookup_extent_mapping(map, start, len);
4391                         if (!em) {
4392                                 write_unlock(&map->lock);
4393                                 break;
4394                         }
4395                         if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4396                             em->start != start) {
4397                                 write_unlock(&map->lock);
4398                                 free_extent_map(em);
4399                                 break;
4400                         }
4401                         if (!test_range_bit(tree, em->start,
4402                                             extent_map_end(em) - 1,
4403                                             EXTENT_LOCKED, 0, NULL)) {
4404                                 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4405                                         &btrfs_inode->runtime_flags);
4406                                 remove_extent_mapping(map, em);
4407                                 /* once for the rb tree */
4408                                 free_extent_map(em);
4409                         }
4410                         start = extent_map_end(em);
4411                         write_unlock(&map->lock);
4412
4413                         /* once for us */
4414                         free_extent_map(em);
4415                 }
4416         }
4417         return try_release_extent_state(tree, page, mask);
4418 }
4419
4420 /*
4421  * helper function for fiemap, which doesn't want to see any holes.
4422  * This maps until we find something past 'last'
4423  */
4424 static struct extent_map *get_extent_skip_holes(struct inode *inode,
4425                                                 u64 offset, u64 last)
4426 {
4427         u64 sectorsize = btrfs_inode_sectorsize(inode);
4428         struct extent_map *em;
4429         u64 len;
4430
4431         if (offset >= last)
4432                 return NULL;
4433
4434         while (1) {
4435                 len = last - offset;
4436                 if (len == 0)
4437                         break;
4438                 len = ALIGN(len, sectorsize);
4439                 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
4440                 if (IS_ERR_OR_NULL(em))
4441                         return em;
4442
4443                 /* if this isn't a hole return it */
4444                 if (em->block_start != EXTENT_MAP_HOLE)
4445                         return em;
4446
4447                 /* this is a hole, advance to the next extent */
4448                 offset = extent_map_end(em);
4449                 free_extent_map(em);
4450                 if (offset >= last)
4451                         break;
4452         }
4453         return NULL;
4454 }
4455
4456 /*
4457  * To cache previous fiemap extent
4458  *
4459  * Will be used for merging fiemap extent
4460  */
4461 struct fiemap_cache {
4462         u64 offset;
4463         u64 phys;
4464         u64 len;
4465         u32 flags;
4466         bool cached;
4467 };
4468
4469 /*
4470  * Helper to submit fiemap extent.
4471  *
4472  * Will try to merge current fiemap extent specified by @offset, @phys,
4473  * @len and @flags with cached one.
4474  * And only when we fails to merge, cached one will be submitted as
4475  * fiemap extent.
4476  *
4477  * Return value is the same as fiemap_fill_next_extent().
4478  */
4479 static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4480                                 struct fiemap_cache *cache,
4481                                 u64 offset, u64 phys, u64 len, u32 flags)
4482 {
4483         int ret = 0;
4484
4485         if (!cache->cached)
4486                 goto assign;
4487
4488         /*
4489          * Sanity check, extent_fiemap() should have ensured that new
4490          * fiemap extent won't overlap with cached one.
4491          * Not recoverable.
4492          *
4493          * NOTE: Physical address can overlap, due to compression
4494          */
4495         if (cache->offset + cache->len > offset) {
4496                 WARN_ON(1);
4497                 return -EINVAL;
4498         }
4499
4500         /*
4501          * Only merges fiemap extents if
4502          * 1) Their logical addresses are continuous
4503          *
4504          * 2) Their physical addresses are continuous
4505          *    So truly compressed (physical size smaller than logical size)
4506          *    extents won't get merged with each other
4507          *
4508          * 3) Share same flags except FIEMAP_EXTENT_LAST
4509          *    So regular extent won't get merged with prealloc extent
4510          */
4511         if (cache->offset + cache->len  == offset &&
4512             cache->phys + cache->len == phys  &&
4513             (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4514                         (flags & ~FIEMAP_EXTENT_LAST)) {
4515                 cache->len += len;
4516                 cache->flags |= flags;
4517                 goto try_submit_last;
4518         }
4519
4520         /* Not mergeable, need to submit cached one */
4521         ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4522                                       cache->len, cache->flags);
4523         cache->cached = false;
4524         if (ret)
4525                 return ret;
4526 assign:
4527         cache->cached = true;
4528         cache->offset = offset;
4529         cache->phys = phys;
4530         cache->len = len;
4531         cache->flags = flags;
4532 try_submit_last:
4533         if (cache->flags & FIEMAP_EXTENT_LAST) {
4534                 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4535                                 cache->phys, cache->len, cache->flags);
4536                 cache->cached = false;
4537         }
4538         return ret;
4539 }
4540
4541 /*
4542  * Emit last fiemap cache
4543  *
4544  * The last fiemap cache may still be cached in the following case:
4545  * 0                  4k                    8k
4546  * |<- Fiemap range ->|
4547  * |<------------  First extent ----------->|
4548  *
4549  * In this case, the first extent range will be cached but not emitted.
4550  * So we must emit it before ending extent_fiemap().
4551  */
4552 static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
4553                                   struct fiemap_cache *cache)
4554 {
4555         int ret;
4556
4557         if (!cache->cached)
4558                 return 0;
4559
4560         ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4561                                       cache->len, cache->flags);
4562         cache->cached = false;
4563         if (ret > 0)
4564                 ret = 0;
4565         return ret;
4566 }
4567
4568 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4569                 __u64 start, __u64 len)
4570 {
4571         int ret = 0;
4572         u64 off = start;
4573         u64 max = start + len;
4574         u32 flags = 0;
4575         u32 found_type;
4576         u64 last;
4577         u64 last_for_get_extent = 0;
4578         u64 disko = 0;
4579         u64 isize = i_size_read(inode);
4580         struct btrfs_key found_key;
4581         struct extent_map *em = NULL;
4582         struct extent_state *cached_state = NULL;
4583         struct btrfs_path *path;
4584         struct btrfs_root *root = BTRFS_I(inode)->root;
4585         struct fiemap_cache cache = { 0 };
4586         struct ulist *roots;
4587         struct ulist *tmp_ulist;
4588         int end = 0;
4589         u64 em_start = 0;
4590         u64 em_len = 0;
4591         u64 em_end = 0;
4592
4593         if (len == 0)
4594                 return -EINVAL;
4595
4596         path = btrfs_alloc_path();
4597         if (!path)
4598                 return -ENOMEM;
4599         path->leave_spinning = 1;
4600
4601         roots = ulist_alloc(GFP_KERNEL);
4602         tmp_ulist = ulist_alloc(GFP_KERNEL);
4603         if (!roots || !tmp_ulist) {
4604                 ret = -ENOMEM;
4605                 goto out_free_ulist;
4606         }
4607
4608         start = round_down(start, btrfs_inode_sectorsize(inode));
4609         len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
4610
4611         /*
4612          * lookup the last file extent.  We're not using i_size here
4613          * because there might be preallocation past i_size
4614          */
4615         ret = btrfs_lookup_file_extent(NULL, root, path,
4616                         btrfs_ino(BTRFS_I(inode)), -1, 0);
4617         if (ret < 0) {
4618                 goto out_free_ulist;
4619         } else {
4620                 WARN_ON(!ret);
4621                 if (ret == 1)
4622                         ret = 0;
4623         }
4624
4625         path->slots[0]--;
4626         btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
4627         found_type = found_key.type;
4628
4629         /* No extents, but there might be delalloc bits */
4630         if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
4631             found_type != BTRFS_EXTENT_DATA_KEY) {
4632                 /* have to trust i_size as the end */
4633                 last = (u64)-1;
4634                 last_for_get_extent = isize;
4635         } else {
4636                 /*
4637                  * remember the start of the last extent.  There are a
4638                  * bunch of different factors that go into the length of the
4639                  * extent, so its much less complex to remember where it started
4640                  */
4641                 last = found_key.offset;
4642                 last_for_get_extent = last + 1;
4643         }
4644         btrfs_release_path(path);
4645
4646         /*
4647          * we might have some extents allocated but more delalloc past those
4648          * extents.  so, we trust isize unless the start of the last extent is
4649          * beyond isize
4650          */
4651         if (last < isize) {
4652                 last = (u64)-1;
4653                 last_for_get_extent = isize;
4654         }
4655
4656         lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
4657                          &cached_state);
4658
4659         em = get_extent_skip_holes(inode, start, last_for_get_extent);
4660         if (!em)
4661                 goto out;
4662         if (IS_ERR(em)) {
4663                 ret = PTR_ERR(em);
4664                 goto out;
4665         }
4666
4667         while (!end) {
4668                 u64 offset_in_extent = 0;
4669
4670                 /* break if the extent we found is outside the range */
4671                 if (em->start >= max || extent_map_end(em) < off)
4672                         break;
4673
4674                 /*
4675                  * get_extent may return an extent that starts before our
4676                  * requested range.  We have to make sure the ranges
4677                  * we return to fiemap always move forward and don't
4678                  * overlap, so adjust the offsets here
4679                  */
4680                 em_start = max(em->start, off);
4681
4682                 /*
4683                  * record the offset from the start of the extent
4684                  * for adjusting the disk offset below.  Only do this if the
4685                  * extent isn't compressed since our in ram offset may be past
4686                  * what we have actually allocated on disk.
4687                  */
4688                 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4689                         offset_in_extent = em_start - em->start;
4690                 em_end = extent_map_end(em);
4691                 em_len = em_end - em_start;
4692                 flags = 0;
4693                 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4694                         disko = em->block_start + offset_in_extent;
4695                 else
4696                         disko = 0;
4697
4698                 /*
4699                  * bump off for our next call to get_extent
4700                  */
4701                 off = extent_map_end(em);
4702                 if (off >= max)
4703                         end = 1;
4704
4705                 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
4706                         end = 1;
4707                         flags |= FIEMAP_EXTENT_LAST;
4708                 } else if (em->block_start == EXTENT_MAP_INLINE) {
4709                         flags |= (FIEMAP_EXTENT_DATA_INLINE |
4710                                   FIEMAP_EXTENT_NOT_ALIGNED);
4711                 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
4712                         flags |= (FIEMAP_EXTENT_DELALLOC |
4713                                   FIEMAP_EXTENT_UNKNOWN);
4714                 } else if (fieinfo->fi_extents_max) {
4715                         u64 bytenr = em->block_start -
4716                                 (em->start - em->orig_start);
4717
4718                         /*
4719                          * As btrfs supports shared space, this information
4720                          * can be exported to userspace tools via
4721                          * flag FIEMAP_EXTENT_SHARED.  If fi_extents_max == 0
4722                          * then we're just getting a count and we can skip the
4723                          * lookup stuff.
4724                          */
4725                         ret = btrfs_check_shared(root,
4726                                                  btrfs_ino(BTRFS_I(inode)),
4727                                                  bytenr, roots, tmp_ulist);
4728                         if (ret < 0)
4729                                 goto out_free;
4730                         if (ret)
4731                                 flags |= FIEMAP_EXTENT_SHARED;
4732                         ret = 0;
4733                 }
4734                 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4735                         flags |= FIEMAP_EXTENT_ENCODED;
4736                 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4737                         flags |= FIEMAP_EXTENT_UNWRITTEN;
4738
4739                 free_extent_map(em);
4740                 em = NULL;
4741                 if ((em_start >= last) || em_len == (u64)-1 ||
4742                    (last == (u64)-1 && isize <= em_end)) {
4743                         flags |= FIEMAP_EXTENT_LAST;
4744                         end = 1;
4745                 }
4746
4747                 /* now scan forward to see if this is really the last extent. */
4748                 em = get_extent_skip_holes(inode, off, last_for_get_extent);
4749                 if (IS_ERR(em)) {
4750                         ret = PTR_ERR(em);
4751                         goto out;
4752                 }
4753                 if (!em) {
4754                         flags |= FIEMAP_EXTENT_LAST;
4755                         end = 1;
4756                 }
4757                 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4758                                            em_len, flags);
4759                 if (ret) {
4760                         if (ret == 1)
4761                                 ret = 0;
4762                         goto out_free;
4763                 }
4764         }
4765 out_free:
4766         if (!ret)
4767                 ret = emit_last_fiemap_cache(fieinfo, &cache);
4768         free_extent_map(em);
4769 out:
4770         unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
4771                              &cached_state);
4772
4773 out_free_ulist:
4774         btrfs_free_path(path);
4775         ulist_free(roots);
4776         ulist_free(tmp_ulist);
4777         return ret;
4778 }
4779
4780 static void __free_extent_buffer(struct extent_buffer *eb)
4781 {
4782         btrfs_leak_debug_del(&eb->leak_list);
4783         kmem_cache_free(extent_buffer_cache, eb);
4784 }
4785
4786 int extent_buffer_under_io(struct extent_buffer *eb)
4787 {
4788         return (atomic_read(&eb->io_pages) ||
4789                 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4790                 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4791 }
4792
4793 /*
4794  * Release all pages attached to the extent buffer.
4795  */
4796 static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
4797 {
4798         int i;
4799         int num_pages;
4800         int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
4801
4802         BUG_ON(extent_buffer_under_io(eb));
4803
4804         num_pages = num_extent_pages(eb);
4805         for (i = 0; i < num_pages; i++) {
4806                 struct page *page = eb->pages[i];
4807
4808                 if (!page)
4809                         continue;
4810                 if (mapped)
4811                         spin_lock(&page->mapping->private_lock);
4812                 /*
4813                  * We do this since we'll remove the pages after we've
4814                  * removed the eb from the radix tree, so we could race
4815                  * and have this page now attached to the new eb.  So
4816                  * only clear page_private if it's still connected to
4817                  * this eb.
4818                  */
4819                 if (PagePrivate(page) &&
4820                     page->private == (unsigned long)eb) {
4821                         BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4822                         BUG_ON(PageDirty(page));
4823                         BUG_ON(PageWriteback(page));
4824                         /*
4825                          * We need to make sure we haven't be attached
4826                          * to a new eb.
4827                          */
4828                         ClearPagePrivate(page);
4829                         set_page_private(page, 0);
4830                         /* One for the page private */
4831                         put_page(page);
4832                 }
4833
4834                 if (mapped)
4835                         spin_unlock(&page->mapping->private_lock);
4836
4837                 /* One for when we allocated the page */
4838                 put_page(page);
4839         }
4840 }
4841
4842 /*
4843  * Helper for releasing the extent buffer.
4844  */
4845 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4846 {
4847         btrfs_release_extent_buffer_pages(eb);
4848         __free_extent_buffer(eb);
4849 }
4850
4851 static struct extent_buffer *
4852 __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
4853                       unsigned long len)
4854 {
4855         struct extent_buffer *eb = NULL;
4856
4857         eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
4858         eb->start = start;
4859         eb->len = len;
4860         eb->fs_info = fs_info;
4861         eb->bflags = 0;
4862         rwlock_init(&eb->lock);
4863         atomic_set(&eb->blocking_readers, 0);
4864         eb->blocking_writers = 0;
4865         eb->lock_nested = false;
4866         init_waitqueue_head(&eb->write_lock_wq);
4867         init_waitqueue_head(&eb->read_lock_wq);
4868
4869         btrfs_leak_debug_add(&eb->leak_list, &buffers);
4870
4871         spin_lock_init(&eb->refs_lock);
4872         atomic_set(&eb->refs, 1);
4873         atomic_set(&eb->io_pages, 0);
4874
4875         /*
4876          * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4877          */
4878         BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4879                 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4880         BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4881
4882 #ifdef CONFIG_BTRFS_DEBUG
4883         eb->spinning_writers = 0;
4884         atomic_set(&eb->spinning_readers, 0);
4885         atomic_set(&eb->read_locks, 0);
4886         eb->write_locks = 0;
4887 #endif
4888
4889         return eb;
4890 }
4891
4892 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4893 {
4894         int i;
4895         struct page *p;
4896         struct extent_buffer *new;
4897         int num_pages = num_extent_pages(src);
4898
4899         new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
4900         if (new == NULL)
4901                 return NULL;
4902
4903         for (i = 0; i < num_pages; i++) {
4904                 p = alloc_page(GFP_NOFS);
4905                 if (!p) {
4906                         btrfs_release_extent_buffer(new);
4907                         return NULL;
4908                 }
4909                 attach_extent_buffer_page(new, p);
4910                 WARN_ON(PageDirty(p));
4911                 SetPageUptodate(p);
4912                 new->pages[i] = p;
4913                 copy_page(page_address(p), page_address(src->pages[i]));
4914         }
4915
4916         set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4917         set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
4918
4919         return new;
4920 }
4921
4922 struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4923                                                   u64 start, unsigned long len)
4924 {
4925         struct extent_buffer *eb;
4926         int num_pages;
4927         int i;
4928
4929         eb = __alloc_extent_buffer(fs_info, start, len);
4930         if (!eb)
4931                 return NULL;
4932
4933         num_pages = num_extent_pages(eb);
4934         for (i = 0; i < num_pages; i++) {
4935                 eb->pages[i] = alloc_page(GFP_NOFS);
4936                 if (!eb->pages[i])
4937                         goto err;
4938         }
4939         set_extent_buffer_uptodate(eb);
4940         btrfs_set_header_nritems(eb, 0);
4941         set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
4942
4943         return eb;
4944 err:
4945         for (; i > 0; i--)
4946                 __free_page(eb->pages[i - 1]);
4947         __free_extent_buffer(eb);
4948         return NULL;
4949 }
4950
4951 struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4952                                                 u64 start)
4953 {
4954         return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
4955 }
4956
4957 static void check_buffer_tree_ref(struct extent_buffer *eb)
4958 {
4959         int refs;
4960         /* the ref bit is tricky.  We have to make sure it is set
4961          * if we have the buffer dirty.   Otherwise the
4962          * code to free a buffer can end up dropping a dirty
4963          * page
4964          *
4965          * Once the ref bit is set, it won't go away while the
4966          * buffer is dirty or in writeback, and it also won't
4967          * go away while we have the reference count on the
4968          * eb bumped.
4969          *
4970          * We can't just set the ref bit without bumping the
4971          * ref on the eb because free_extent_buffer might
4972          * see the ref bit and try to clear it.  If this happens
4973          * free_extent_buffer might end up dropping our original
4974          * ref by mistake and freeing the page before we are able
4975          * to add one more ref.
4976          *
4977          * So bump the ref count first, then set the bit.  If someone
4978          * beat us to it, drop the ref we added.
4979          */
4980         refs = atomic_read(&eb->refs);
4981         if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4982                 return;
4983
4984         spin_lock(&eb->refs_lock);
4985         if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4986                 atomic_inc(&eb->refs);
4987         spin_unlock(&eb->refs_lock);
4988 }
4989
4990 static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4991                 struct page *accessed)
4992 {
4993         int num_pages, i;
4994
4995         check_buffer_tree_ref(eb);
4996
4997         num_pages = num_extent_pages(eb);
4998         for (i = 0; i < num_pages; i++) {
4999                 struct page *p = eb->pages[i];
5000
5001                 if (p != accessed)
5002                         mark_page_accessed(p);
5003         }
5004 }
5005
5006 struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
5007                                          u64 start)
5008 {
5009         struct extent_buffer *eb;
5010
5011         rcu_read_lock();
5012         eb = radix_tree_lookup(&fs_info->buffer_radix,
5013                                start >> PAGE_SHIFT);
5014         if (eb && atomic_inc_not_zero(&eb->refs)) {
5015                 rcu_read_unlock();
5016                 /*
5017                  * Lock our eb's refs_lock to avoid races with
5018                  * free_extent_buffer. When we get our eb it might be flagged
5019                  * with EXTENT_BUFFER_STALE and another task running
5020                  * free_extent_buffer might have seen that flag set,
5021                  * eb->refs == 2, that the buffer isn't under IO (dirty and
5022                  * writeback flags not set) and it's still in the tree (flag
5023                  * EXTENT_BUFFER_TREE_REF set), therefore being in the process
5024                  * of decrementing the extent buffer's reference count twice.
5025                  * So here we could race and increment the eb's reference count,
5026                  * clear its stale flag, mark it as dirty and drop our reference
5027                  * before the other task finishes executing free_extent_buffer,
5028                  * which would later result in an attempt to free an extent
5029                  * buffer that is dirty.
5030                  */
5031                 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
5032                         spin_lock(&eb->refs_lock);
5033                         spin_unlock(&eb->refs_lock);
5034                 }
5035                 mark_extent_buffer_accessed(eb, NULL);
5036                 return eb;
5037         }
5038         rcu_read_unlock();
5039
5040         return NULL;
5041 }
5042
5043 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5044 struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
5045                                         u64 start)
5046 {
5047         struct extent_buffer *eb, *exists = NULL;
5048         int ret;
5049
5050         eb = find_extent_buffer(fs_info, start);
5051         if (eb)
5052                 return eb;
5053         eb = alloc_dummy_extent_buffer(fs_info, start);
5054         if (!eb)
5055                 return ERR_PTR(-ENOMEM);
5056         eb->fs_info = fs_info;
5057 again:
5058         ret = radix_tree_preload(GFP_NOFS);
5059         if (ret) {
5060                 exists = ERR_PTR(ret);
5061                 goto free_eb;
5062         }
5063         spin_lock(&fs_info->buffer_lock);
5064         ret = radix_tree_insert(&fs_info->buffer_radix,
5065                                 start >> PAGE_SHIFT, eb);
5066         spin_unlock(&fs_info->buffer_lock);
5067         radix_tree_preload_end();
5068         if (ret == -EEXIST) {
5069                 exists = find_extent_buffer(fs_info, start);
5070                 if (exists)
5071                         goto free_eb;
5072                 else
5073                         goto again;
5074         }
5075         check_buffer_tree_ref(eb);
5076         set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
5077
5078         return eb;
5079 free_eb:
5080         btrfs_release_extent_buffer(eb);
5081         return exists;
5082 }
5083 #endif
5084
5085 struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
5086                                           u64 start)
5087 {
5088         unsigned long len = fs_info->nodesize;
5089         int num_pages;
5090         int i;
5091         unsigned long index = start >> PAGE_SHIFT;
5092         struct extent_buffer *eb;
5093         struct extent_buffer *exists = NULL;
5094         struct page *p;
5095         struct address_space *mapping = fs_info->btree_inode->i_mapping;
5096         int uptodate = 1;
5097         int ret;
5098
5099         if (!IS_ALIGNED(start, fs_info->sectorsize)) {
5100                 btrfs_err(fs_info, "bad tree block start %llu", start);
5101                 return ERR_PTR(-EINVAL);
5102         }
5103
5104         eb = find_extent_buffer(fs_info, start);
5105         if (eb)
5106                 return eb;
5107
5108         eb = __alloc_extent_buffer(fs_info, start, len);
5109         if (!eb)
5110                 return ERR_PTR(-ENOMEM);
5111
5112         num_pages = num_extent_pages(eb);
5113         for (i = 0; i < num_pages; i++, index++) {
5114                 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
5115                 if (!p) {
5116                         exists = ERR_PTR(-ENOMEM);
5117                         goto free_eb;
5118                 }
5119
5120                 spin_lock(&mapping->private_lock);
5121                 if (PagePrivate(p)) {
5122                         /*
5123                          * We could have already allocated an eb for this page
5124                          * and attached one so lets see if we can get a ref on
5125                          * the existing eb, and if we can we know it's good and
5126                          * we can just return that one, else we know we can just
5127                          * overwrite page->private.
5128                          */
5129                         exists = (struct extent_buffer *)p->private;
5130                         if (atomic_inc_not_zero(&exists->refs)) {
5131                                 spin_unlock(&mapping->private_lock);
5132                                 unlock_page(p);
5133                                 put_page(p);
5134                                 mark_extent_buffer_accessed(exists, p);
5135                                 goto free_eb;
5136                         }
5137                         exists = NULL;
5138
5139                         /*
5140                          * Do this so attach doesn't complain and we need to
5141                          * drop the ref the old guy had.
5142                          */
5143                         ClearPagePrivate(p);
5144                         WARN_ON(PageDirty(p));
5145                         put_page(p);
5146                 }
5147                 attach_extent_buffer_page(eb, p);
5148                 spin_unlock(&mapping->private_lock);
5149                 WARN_ON(PageDirty(p));
5150                 eb->pages[i] = p;
5151                 if (!PageUptodate(p))
5152                         uptodate = 0;
5153
5154                 /*
5155                  * We can't unlock the pages just yet since the extent buffer
5156                  * hasn't been properly inserted in the radix tree, this
5157                  * opens a race with btree_releasepage which can free a page
5158                  * while we are still filling in all pages for the buffer and
5159                  * we could crash.
5160                  */
5161         }
5162         if (uptodate)
5163                 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
5164 again:
5165         ret = radix_tree_preload(GFP_NOFS);
5166         if (ret) {
5167                 exists = ERR_PTR(ret);
5168                 goto free_eb;
5169         }
5170
5171         spin_lock(&fs_info->buffer_lock);
5172         ret = radix_tree_insert(&fs_info->buffer_radix,
5173                                 start >> PAGE_SHIFT, eb);
5174         spin_unlock(&fs_info->buffer_lock);
5175         radix_tree_preload_end();
5176         if (ret == -EEXIST) {
5177                 exists = find_extent_buffer(fs_info, start);
5178                 if (exists)
5179                         goto free_eb;
5180                 else
5181                         goto again;
5182         }
5183         /* add one reference for the tree */
5184         check_buffer_tree_ref(eb);
5185         set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
5186
5187         /*
5188          * Now it's safe to unlock the pages because any calls to
5189          * btree_releasepage will correctly detect that a page belongs to a
5190          * live buffer and won't free them prematurely.
5191          */
5192         for (i = 0; i < num_pages; i++)
5193                 unlock_page(eb->pages[i]);
5194         return eb;
5195
5196 free_eb:
5197         WARN_ON(!atomic_dec_and_test(&eb->refs));
5198         for (i = 0; i < num_pages; i++) {
5199                 if (eb->pages[i])
5200                         unlock_page(eb->pages[i]);
5201         }
5202
5203         btrfs_release_extent_buffer(eb);
5204         return exists;
5205 }
5206
5207 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5208 {
5209         struct extent_buffer *eb =
5210                         container_of(head, struct extent_buffer, rcu_head);
5211
5212         __free_extent_buffer(eb);
5213 }
5214
5215 static int release_extent_buffer(struct extent_buffer *eb)
5216 {
5217         lockdep_assert_held(&eb->refs_lock);
5218
5219         WARN_ON(atomic_read(&eb->refs) == 0);
5220         if (atomic_dec_and_test(&eb->refs)) {
5221                 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
5222                         struct btrfs_fs_info *fs_info = eb->fs_info;
5223
5224                         spin_unlock(&eb->refs_lock);
5225
5226                         spin_lock(&fs_info->buffer_lock);
5227                         radix_tree_delete(&fs_info->buffer_radix,
5228                                           eb->start >> PAGE_SHIFT);
5229                         spin_unlock(&fs_info->buffer_lock);
5230                 } else {
5231                         spin_unlock(&eb->refs_lock);
5232                 }
5233
5234                 /* Should be safe to release our pages at this point */
5235                 btrfs_release_extent_buffer_pages(eb);
5236 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5237                 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
5238                         __free_extent_buffer(eb);
5239                         return 1;
5240                 }
5241 #endif
5242                 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
5243                 return 1;
5244         }
5245         spin_unlock(&eb->refs_lock);
5246
5247         return 0;
5248 }
5249
5250 void free_extent_buffer(struct extent_buffer *eb)
5251 {
5252         int refs;
5253         int old;
5254         if (!eb)
5255                 return;
5256
5257         while (1) {
5258                 refs = atomic_read(&eb->refs);
5259                 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5260                     || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5261                         refs == 1))
5262                         break;
5263                 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5264                 if (old == refs)
5265                         return;
5266         }
5267
5268         spin_lock(&eb->refs_lock);
5269         if (atomic_read(&eb->refs) == 2 &&
5270             test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
5271             !extent_buffer_under_io(eb) &&
5272             test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5273                 atomic_dec(&eb->refs);
5274
5275         /*
5276          * I know this is terrible, but it's temporary until we stop tracking
5277          * the uptodate bits and such for the extent buffers.
5278          */
5279         release_extent_buffer(eb);
5280 }
5281
5282 void free_extent_buffer_stale(struct extent_buffer *eb)
5283 {
5284         if (!eb)
5285                 return;
5286
5287         spin_lock(&eb->refs_lock);
5288         set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5289
5290         if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
5291             test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5292                 atomic_dec(&eb->refs);
5293         release_extent_buffer(eb);
5294 }
5295
5296 void clear_extent_buffer_dirty(struct extent_buffer *eb)
5297 {
5298         int i;
5299         int num_pages;
5300         struct page *page;
5301
5302         num_pages = num_extent_pages(eb);
5303
5304         for (i = 0; i < num_pages; i++) {
5305                 page = eb->pages[i];
5306                 if (!PageDirty(page))
5307                         continue;
5308
5309                 lock_page(page);
5310                 WARN_ON(!PagePrivate(page));
5311
5312                 clear_page_dirty_for_io(page);
5313                 xa_lock_irq(&page->mapping->i_pages);
5314                 if (!PageDirty(page))
5315                         __xa_clear_mark(&page->mapping->i_pages,
5316                                         page_index(page), PAGECACHE_TAG_DIRTY);
5317                 xa_unlock_irq(&page->mapping->i_pages);
5318                 ClearPageError(page);
5319                 unlock_page(page);
5320         }
5321         WARN_ON(atomic_read(&eb->refs) == 0);
5322 }
5323
5324 bool set_extent_buffer_dirty(struct extent_buffer *eb)
5325 {
5326         int i;
5327         int num_pages;
5328         bool was_dirty;
5329
5330         check_buffer_tree_ref(eb);
5331
5332         was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
5333
5334         num_pages = num_extent_pages(eb);
5335         WARN_ON(atomic_read(&eb->refs) == 0);
5336         WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5337
5338         if (!was_dirty)
5339                 for (i = 0; i < num_pages; i++)
5340                         set_page_dirty(eb->pages[i]);
5341
5342 #ifdef CONFIG_BTRFS_DEBUG
5343         for (i = 0; i < num_pages; i++)
5344                 ASSERT(PageDirty(eb->pages[i]));
5345 #endif
5346
5347         return was_dirty;
5348 }
5349
5350 void clear_extent_buffer_uptodate(struct extent_buffer *eb)
5351 {
5352         int i;
5353         struct page *page;
5354         int num_pages;
5355
5356         clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
5357         num_pages = num_extent_pages(eb);
5358         for (i = 0; i < num_pages; i++) {
5359                 page = eb->pages[i];
5360                 if (page)
5361                         ClearPageUptodate(page);
5362         }
5363 }
5364
5365 void set_extent_buffer_uptodate(struct extent_buffer *eb)
5366 {
5367         int i;
5368         struct page *page;
5369         int num_pages;
5370
5371         set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
5372         num_pages = num_extent_pages(eb);
5373         for (i = 0; i < num_pages; i++) {
5374                 page = eb->pages[i];
5375                 SetPageUptodate(page);
5376         }
5377 }
5378
5379 int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
5380 {
5381         int i;
5382         struct page *page;
5383         int err;
5384         int ret = 0;
5385         int locked_pages = 0;
5386         int all_uptodate = 1;
5387         int num_pages;
5388         unsigned long num_reads = 0;
5389         struct bio *bio = NULL;
5390         unsigned long bio_flags = 0;
5391         struct extent_io_tree *tree = &BTRFS_I(eb->fs_info->btree_inode)->io_tree;
5392
5393         if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
5394                 return 0;
5395
5396         num_pages = num_extent_pages(eb);
5397         for (i = 0; i < num_pages; i++) {
5398                 page = eb->pages[i];
5399                 if (wait == WAIT_NONE) {
5400                         if (!trylock_page(page))
5401                                 goto unlock_exit;
5402                 } else {
5403                         lock_page(page);
5404                 }
5405                 locked_pages++;
5406         }
5407         /*
5408          * We need to firstly lock all pages to make sure that
5409          * the uptodate bit of our pages won't be affected by
5410          * clear_extent_buffer_uptodate().
5411          */
5412         for (i = 0; i < num_pages; i++) {
5413                 page = eb->pages[i];
5414                 if (!PageUptodate(page)) {
5415                         num_reads++;
5416                         all_uptodate = 0;
5417                 }
5418         }
5419
5420         if (all_uptodate) {
5421                 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
5422                 goto unlock_exit;
5423         }
5424
5425         clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
5426         eb->read_mirror = 0;
5427         atomic_set(&eb->io_pages, num_reads);
5428         for (i = 0; i < num_pages; i++) {
5429                 page = eb->pages[i];
5430
5431                 if (!PageUptodate(page)) {
5432                         if (ret) {
5433                                 atomic_dec(&eb->io_pages);
5434                                 unlock_page(page);
5435                                 continue;
5436                         }
5437
5438                         ClearPageError(page);
5439                         err = __extent_read_full_page(tree, page,
5440                                                       btree_get_extent, &bio,
5441                                                       mirror_num, &bio_flags,
5442                                                       REQ_META);
5443                         if (err) {
5444                                 ret = err;
5445                                 /*
5446                                  * We use &bio in above __extent_read_full_page,
5447                                  * so we ensure that if it returns error, the
5448                                  * current page fails to add itself to bio and
5449                                  * it's been unlocked.
5450                                  *
5451                                  * We must dec io_pages by ourselves.
5452                                  */
5453                                 atomic_dec(&eb->io_pages);
5454                         }
5455                 } else {
5456                         unlock_page(page);
5457                 }
5458         }
5459
5460         if (bio) {
5461                 err = submit_one_bio(bio, mirror_num, bio_flags);
5462                 if (err)
5463                         return err;
5464         }
5465
5466         if (ret || wait != WAIT_COMPLETE)
5467                 return ret;
5468
5469         for (i = 0; i < num_pages; i++) {
5470                 page = eb->pages[i];
5471                 wait_on_page_locked(page);
5472                 if (!PageUptodate(page))
5473                         ret = -EIO;
5474         }
5475
5476         return ret;
5477
5478 unlock_exit:
5479         while (locked_pages > 0) {
5480                 locked_pages--;
5481                 page = eb->pages[locked_pages];
5482                 unlock_page(page);
5483         }
5484         return ret;
5485 }
5486
5487 void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5488                         unsigned long start, unsigned long len)
5489 {
5490         size_t cur;
5491         size_t offset;
5492         struct page *page;
5493         char *kaddr;
5494         char *dst = (char *)dstv;
5495         size_t start_offset = offset_in_page(eb->start);
5496         unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5497
5498         if (start + len > eb->len) {
5499                 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5500                      eb->start, eb->len, start, len);
5501                 memset(dst, 0, len);
5502                 return;
5503         }
5504
5505         offset = offset_in_page(start_offset + start);
5506
5507         while (len > 0) {
5508                 page = eb->pages[i];
5509
5510                 cur = min(len, (PAGE_SIZE - offset));
5511                 kaddr = page_address(page);
5512                 memcpy(dst, kaddr + offset, cur);
5513
5514                 dst += cur;
5515                 len -= cur;
5516                 offset = 0;
5517                 i++;
5518         }
5519 }
5520
5521 int read_extent_buffer_to_user(const struct extent_buffer *eb,
5522                                void __user *dstv,
5523                                unsigned long start, unsigned long len)
5524 {
5525         size_t cur;
5526         size_t offset;
5527         struct page *page;
5528         char *kaddr;
5529         char __user *dst = (char __user *)dstv;
5530         size_t start_offset = offset_in_page(eb->start);
5531         unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5532         int ret = 0;
5533
5534         WARN_ON(start > eb->len);
5535         WARN_ON(start + len > eb->start + eb->len);
5536
5537         offset = offset_in_page(start_offset + start);
5538
5539         while (len > 0) {
5540                 page = eb->pages[i];
5541
5542                 cur = min(len, (PAGE_SIZE - offset));
5543                 kaddr = page_address(page);
5544                 if (copy_to_user(dst, kaddr + offset, cur)) {
5545                         ret = -EFAULT;
5546                         break;
5547                 }
5548
5549                 dst += cur;
5550                 len -= cur;
5551                 offset = 0;
5552                 i++;
5553         }
5554
5555         return ret;
5556 }
5557
5558 /*
5559  * return 0 if the item is found within a page.
5560  * return 1 if the item spans two pages.
5561  * return -EINVAL otherwise.
5562  */
5563 int map_private_extent_buffer(const struct extent_buffer *eb,
5564                               unsigned long start, unsigned long min_len,
5565                               char **map, unsigned long *map_start,
5566                               unsigned long *map_len)
5567 {
5568         size_t offset;
5569         char *kaddr;
5570         struct page *p;
5571         size_t start_offset = offset_in_page(eb->start);
5572         unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5573         unsigned long end_i = (start_offset + start + min_len - 1) >>
5574                 PAGE_SHIFT;
5575
5576         if (start + min_len > eb->len) {
5577                 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5578                        eb->start, eb->len, start, min_len);
5579                 return -EINVAL;
5580         }
5581
5582         if (i != end_i)
5583                 return 1;
5584
5585         if (i == 0) {
5586                 offset = start_offset;
5587                 *map_start = 0;
5588         } else {
5589                 offset = 0;
5590                 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
5591         }
5592
5593         p = eb->pages[i];
5594         kaddr = page_address(p);
5595         *map = kaddr + offset;
5596         *map_len = PAGE_SIZE - offset;
5597         return 0;
5598 }
5599
5600 int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5601                          unsigned long start, unsigned long len)
5602 {
5603         size_t cur;
5604         size_t offset;
5605         struct page *page;
5606         char *kaddr;
5607         char *ptr = (char *)ptrv;
5608         size_t start_offset = offset_in_page(eb->start);
5609         unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5610         int ret = 0;
5611
5612         WARN_ON(start > eb->len);
5613         WARN_ON(start + len > eb->start + eb->len);
5614
5615         offset = offset_in_page(start_offset + start);
5616
5617         while (len > 0) {
5618                 page = eb->pages[i];
5619
5620                 cur = min(len, (PAGE_SIZE - offset));
5621
5622                 kaddr = page_address(page);
5623                 ret = memcmp(ptr, kaddr + offset, cur);
5624                 if (ret)
5625                         break;
5626
5627                 ptr += cur;
5628                 len -= cur;
5629                 offset = 0;
5630                 i++;
5631         }
5632         return ret;
5633 }
5634
5635 void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5636                 const void *srcv)
5637 {
5638         char *kaddr;
5639
5640         WARN_ON(!PageUptodate(eb->pages[0]));
5641         kaddr = page_address(eb->pages[0]);
5642         memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5643                         BTRFS_FSID_SIZE);
5644 }
5645
5646 void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5647 {
5648         char *kaddr;
5649
5650         WARN_ON(!PageUptodate(eb->pages[0]));
5651         kaddr = page_address(eb->pages[0]);
5652         memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5653                         BTRFS_FSID_SIZE);
5654 }
5655
5656 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5657                          unsigned long start, unsigned long len)
5658 {
5659         size_t cur;
5660         size_t offset;
5661         struct page *page;
5662         char *kaddr;
5663         char *src = (char *)srcv;
5664         size_t start_offset = offset_in_page(eb->start);
5665         unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5666
5667         WARN_ON(start > eb->len);
5668         WARN_ON(start + len > eb->start + eb->len);
5669
5670         offset = offset_in_page(start_offset + start);
5671
5672         while (len > 0) {
5673                 page = eb->pages[i];
5674                 WARN_ON(!PageUptodate(page));
5675
5676                 cur = min(len, PAGE_SIZE - offset);
5677                 kaddr = page_address(page);
5678                 memcpy(kaddr + offset, src, cur);
5679
5680                 src += cur;
5681                 len -= cur;
5682                 offset = 0;
5683                 i++;
5684         }
5685 }
5686
5687 void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5688                 unsigned long len)
5689 {
5690         size_t cur;
5691         size_t offset;
5692         struct page *page;
5693         char *kaddr;
5694         size_t start_offset = offset_in_page(eb->start);
5695         unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5696
5697         WARN_ON(start > eb->len);
5698         WARN_ON(start + len > eb->start + eb->len);
5699
5700         offset = offset_in_page(start_offset + start);
5701
5702         while (len > 0) {
5703                 page = eb->pages[i];
5704                 WARN_ON(!PageUptodate(page));
5705
5706                 cur = min(len, PAGE_SIZE - offset);
5707                 kaddr = page_address(page);
5708                 memset(kaddr + offset, 0, cur);
5709
5710                 len -= cur;
5711                 offset = 0;
5712                 i++;
5713         }
5714 }
5715
5716 void copy_extent_buffer_full(struct extent_buffer *dst,
5717                              struct extent_buffer *src)
5718 {
5719         int i;
5720         int num_pages;
5721
5722         ASSERT(dst->len == src->len);
5723
5724         num_pages = num_extent_pages(dst);
5725         for (i = 0; i < num_pages; i++)
5726                 copy_page(page_address(dst->pages[i]),
5727                                 page_address(src->pages[i]));
5728 }
5729
5730 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5731                         unsigned long dst_offset, unsigned long src_offset,
5732                         unsigned long len)
5733 {
5734         u64 dst_len = dst->len;
5735         size_t cur;
5736         size_t offset;
5737         struct page *page;
5738         char *kaddr;
5739         size_t start_offset = offset_in_page(dst->start);
5740         unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
5741
5742         WARN_ON(src->len != dst_len);
5743
5744         offset = offset_in_page(start_offset + dst_offset);
5745
5746         while (len > 0) {
5747                 page = dst->pages[i];
5748                 WARN_ON(!PageUptodate(page));
5749
5750                 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
5751
5752                 kaddr = page_address(page);
5753                 read_extent_buffer(src, kaddr + offset, src_offset, cur);
5754
5755                 src_offset += cur;
5756                 len -= cur;
5757                 offset = 0;
5758                 i++;
5759         }
5760 }
5761
5762 /*
5763  * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5764  * given bit number
5765  * @eb: the extent buffer
5766  * @start: offset of the bitmap item in the extent buffer
5767  * @nr: bit number
5768  * @page_index: return index of the page in the extent buffer that contains the
5769  * given bit number
5770  * @page_offset: return offset into the page given by page_index
5771  *
5772  * This helper hides the ugliness of finding the byte in an extent buffer which
5773  * contains a given bit.
5774  */
5775 static inline void eb_bitmap_offset(struct extent_buffer *eb,
5776                                     unsigned long start, unsigned long nr,
5777                                     unsigned long *page_index,
5778                                     size_t *page_offset)
5779 {
5780         size_t start_offset = offset_in_page(eb->start);
5781         size_t byte_offset = BIT_BYTE(nr);
5782         size_t offset;
5783
5784         /*
5785          * The byte we want is the offset of the extent buffer + the offset of
5786          * the bitmap item in the extent buffer + the offset of the byte in the
5787          * bitmap item.
5788          */
5789         offset = start_offset + start + byte_offset;
5790
5791         *page_index = offset >> PAGE_SHIFT;
5792         *page_offset = offset_in_page(offset);
5793 }
5794
5795 /**
5796  * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5797  * @eb: the extent buffer
5798  * @start: offset of the bitmap item in the extent buffer
5799  * @nr: bit number to test
5800  */
5801 int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5802                            unsigned long nr)
5803 {
5804         u8 *kaddr;
5805         struct page *page;
5806         unsigned long i;
5807         size_t offset;
5808
5809         eb_bitmap_offset(eb, start, nr, &i, &offset);
5810         page = eb->pages[i];
5811         WARN_ON(!PageUptodate(page));
5812         kaddr = page_address(page);
5813         return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5814 }
5815
5816 /**
5817  * extent_buffer_bitmap_set - set an area of a bitmap
5818  * @eb: the extent buffer
5819  * @start: offset of the bitmap item in the extent buffer
5820  * @pos: bit number of the first bit
5821  * @len: number of bits to set
5822  */
5823 void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5824                               unsigned long pos, unsigned long len)
5825 {
5826         u8 *kaddr;
5827         struct page *page;
5828         unsigned long i;
5829         size_t offset;
5830         const unsigned int size = pos + len;
5831         int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
5832         u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
5833
5834         eb_bitmap_offset(eb, start, pos, &i, &offset);
5835         page = eb->pages[i];
5836         WARN_ON(!PageUptodate(page));
5837         kaddr = page_address(page);
5838
5839         while (len >= bits_to_set) {
5840                 kaddr[offset] |= mask_to_set;
5841                 len -= bits_to_set;
5842                 bits_to_set = BITS_PER_BYTE;
5843                 mask_to_set = ~0;
5844                 if (++offset >= PAGE_SIZE && len > 0) {
5845                         offset = 0;
5846                         page = eb->pages[++i];
5847                         WARN_ON(!PageUptodate(page));
5848                         kaddr = page_address(page);
5849                 }
5850         }
5851         if (len) {
5852                 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5853                 kaddr[offset] |= mask_to_set;
5854         }
5855 }
5856
5857
5858 /**
5859  * extent_buffer_bitmap_clear - clear an area of a bitmap
5860  * @eb: the extent buffer
5861  * @start: offset of the bitmap item in the extent buffer
5862  * @pos: bit number of the first bit
5863  * @len: number of bits to clear
5864  */
5865 void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5866                                 unsigned long pos, unsigned long len)
5867 {
5868         u8 *kaddr;
5869         struct page *page;
5870         unsigned long i;
5871         size_t offset;
5872         const unsigned int size = pos + len;
5873         int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
5874         u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
5875
5876         eb_bitmap_offset(eb, start, pos, &i, &offset);
5877         page = eb->pages[i];
5878         WARN_ON(!PageUptodate(page));
5879         kaddr = page_address(page);
5880
5881         while (len >= bits_to_clear) {
5882                 kaddr[offset] &= ~mask_to_clear;
5883                 len -= bits_to_clear;
5884                 bits_to_clear = BITS_PER_BYTE;
5885                 mask_to_clear = ~0;
5886                 if (++offset >= PAGE_SIZE && len > 0) {
5887                         offset = 0;
5888                         page = eb->pages[++i];
5889                         WARN_ON(!PageUptodate(page));
5890                         kaddr = page_address(page);
5891                 }
5892         }
5893         if (len) {
5894                 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5895                 kaddr[offset] &= ~mask_to_clear;
5896         }
5897 }
5898
5899 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5900 {
5901         unsigned long distance = (src > dst) ? src - dst : dst - src;
5902         return distance < len;
5903 }
5904
5905 static void copy_pages(struct page *dst_page, struct page *src_page,
5906                        unsigned long dst_off, unsigned long src_off,
5907                        unsigned long len)
5908 {
5909         char *dst_kaddr = page_address(dst_page);
5910         char *src_kaddr;
5911         int must_memmove = 0;
5912
5913         if (dst_page != src_page) {
5914                 src_kaddr = page_address(src_page);
5915         } else {
5916                 src_kaddr = dst_kaddr;
5917                 if (areas_overlap(src_off, dst_off, len))
5918                         must_memmove = 1;
5919         }
5920
5921         if (must_memmove)
5922                 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5923         else
5924                 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
5925 }
5926
5927 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5928                            unsigned long src_offset, unsigned long len)
5929 {
5930         struct btrfs_fs_info *fs_info = dst->fs_info;
5931         size_t cur;
5932         size_t dst_off_in_page;
5933         size_t src_off_in_page;
5934         size_t start_offset = offset_in_page(dst->start);
5935         unsigned long dst_i;
5936         unsigned long src_i;
5937
5938         if (src_offset + len > dst->len) {
5939                 btrfs_err(fs_info,
5940                         "memmove bogus src_offset %lu move len %lu dst len %lu",
5941                          src_offset, len, dst->len);
5942                 BUG();
5943         }
5944         if (dst_offset + len > dst->len) {
5945                 btrfs_err(fs_info,
5946                         "memmove bogus dst_offset %lu move len %lu dst len %lu",
5947                          dst_offset, len, dst->len);
5948                 BUG();
5949         }
5950
5951         while (len > 0) {
5952                 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5953                 src_off_in_page = offset_in_page(start_offset + src_offset);
5954
5955                 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5956                 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
5957
5958                 cur = min(len, (unsigned long)(PAGE_SIZE -
5959                                                src_off_in_page));
5960                 cur = min_t(unsigned long, cur,
5961                         (unsigned long)(PAGE_SIZE - dst_off_in_page));
5962
5963                 copy_pages(dst->pages[dst_i], dst->pages[src_i],
5964                            dst_off_in_page, src_off_in_page, cur);
5965
5966                 src_offset += cur;
5967                 dst_offset += cur;
5968                 len -= cur;
5969         }
5970 }
5971
5972 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5973                            unsigned long src_offset, unsigned long len)
5974 {
5975         struct btrfs_fs_info *fs_info = dst->fs_info;
5976         size_t cur;
5977         size_t dst_off_in_page;
5978         size_t src_off_in_page;
5979         unsigned long dst_end = dst_offset + len - 1;
5980         unsigned long src_end = src_offset + len - 1;
5981         size_t start_offset = offset_in_page(dst->start);
5982         unsigned long dst_i;
5983         unsigned long src_i;
5984
5985         if (src_offset + len > dst->len) {
5986                 btrfs_err(fs_info,
5987                           "memmove bogus src_offset %lu move len %lu len %lu",
5988                           src_offset, len, dst->len);
5989                 BUG();
5990         }
5991         if (dst_offset + len > dst->len) {
5992                 btrfs_err(fs_info,
5993                           "memmove bogus dst_offset %lu move len %lu len %lu",
5994                           dst_offset, len, dst->len);
5995                 BUG();
5996         }
5997         if (dst_offset < src_offset) {
5998                 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5999                 return;
6000         }
6001         while (len > 0) {
6002                 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
6003                 src_i = (start_offset + src_end) >> PAGE_SHIFT;
6004
6005                 dst_off_in_page = offset_in_page(start_offset + dst_end);
6006                 src_off_in_page = offset_in_page(start_offset + src_end);
6007
6008                 cur = min_t(unsigned long, len, src_off_in_page + 1);
6009                 cur = min(cur, dst_off_in_page + 1);
6010                 copy_pages(dst->pages[dst_i], dst->pages[src_i],
6011                            dst_off_in_page - cur + 1,
6012                            src_off_in_page - cur + 1, cur);
6013
6014                 dst_end -= cur;
6015                 src_end -= cur;
6016                 len -= cur;
6017         }
6018 }
6019
6020 int try_release_extent_buffer(struct page *page)
6021 {
6022         struct extent_buffer *eb;
6023
6024         /*
6025          * We need to make sure nobody is attaching this page to an eb right
6026          * now.
6027          */
6028         spin_lock(&page->mapping->private_lock);
6029         if (!PagePrivate(page)) {
6030                 spin_unlock(&page->mapping->private_lock);
6031                 return 1;
6032         }
6033
6034         eb = (struct extent_buffer *)page->private;
6035         BUG_ON(!eb);
6036
6037         /*
6038          * This is a little awful but should be ok, we need to make sure that
6039          * the eb doesn't disappear out from under us while we're looking at
6040          * this page.
6041          */
6042         spin_lock(&eb->refs_lock);
6043         if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
6044                 spin_unlock(&eb->refs_lock);
6045                 spin_unlock(&page->mapping->private_lock);
6046                 return 0;
6047         }
6048         spin_unlock(&page->mapping->private_lock);
6049
6050         /*
6051          * If tree ref isn't set then we know the ref on this eb is a real ref,
6052          * so just return, this page will likely be freed soon anyway.
6053          */
6054         if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
6055                 spin_unlock(&eb->refs_lock);
6056                 return 0;
6057         }
6058
6059         return release_extent_buffer(eb);
6060 }