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