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