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