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