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