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