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