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