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