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