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