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