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