btrfs: fix and document the zoned device choice in alloc_new_bio
[linux-block.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>
14605409 15#include <linux/fsverity.h>
cea62800 16#include "misc.h"
d1310b2e 17#include "extent_io.h"
9c7d3a54 18#include "extent-io-tree.h"
d1310b2e 19#include "extent_map.h"
902b22f3
DW
20#include "ctree.h"
21#include "btrfs_inode.h"
4a54c8c1 22#include "volumes.h"
21adbd5c 23#include "check-integrity.h"
0b32f4bb 24#include "locking.h"
606686ee 25#include "rcu-string.h"
fe09e16c 26#include "backref.h"
6af49dbd 27#include "disk-io.h"
760f991f 28#include "subpage.h"
d3575156 29#include "zoned.h"
0bc09ca1 30#include "block-group.h"
d1310b2e 31
d1310b2e
CM
32static struct kmem_cache *extent_state_cache;
33static struct kmem_cache *extent_buffer_cache;
8ac9f7c1 34static struct bio_set btrfs_bioset;
d1310b2e 35
27a3507d
FM
36static inline bool extent_state_in_tree(const struct extent_state *state)
37{
38 return !RB_EMPTY_NODE(&state->rb_node);
39}
40
6d49ba1b 41#ifdef CONFIG_BTRFS_DEBUG
d1310b2e 42static LIST_HEAD(states);
d397712b 43static DEFINE_SPINLOCK(leak_lock);
6d49ba1b 44
3fd63727
JB
45static inline void btrfs_leak_debug_add(spinlock_t *lock,
46 struct list_head *new,
47 struct list_head *head)
6d49ba1b
ES
48{
49 unsigned long flags;
50
3fd63727 51 spin_lock_irqsave(lock, flags);
6d49ba1b 52 list_add(new, head);
3fd63727 53 spin_unlock_irqrestore(lock, flags);
6d49ba1b
ES
54}
55
3fd63727
JB
56static inline void btrfs_leak_debug_del(spinlock_t *lock,
57 struct list_head *entry)
6d49ba1b
ES
58{
59 unsigned long flags;
60
3fd63727 61 spin_lock_irqsave(lock, flags);
6d49ba1b 62 list_del(entry);
3fd63727 63 spin_unlock_irqrestore(lock, flags);
6d49ba1b
ES
64}
65
3fd63727 66void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info)
6d49ba1b 67{
6d49ba1b 68 struct extent_buffer *eb;
3fd63727 69 unsigned long flags;
6d49ba1b 70
8c38938c
JB
71 /*
72 * If we didn't get into open_ctree our allocated_ebs will not be
73 * initialized, so just skip this.
74 */
75 if (!fs_info->allocated_ebs.next)
76 return;
77
3fd63727
JB
78 spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
79 while (!list_empty(&fs_info->allocated_ebs)) {
80 eb = list_first_entry(&fs_info->allocated_ebs,
81 struct extent_buffer, leak_list);
8c38938c
JB
82 pr_err(
83 "BTRFS: buffer leak start %llu len %lu refs %d bflags %lu owner %llu\n",
84 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags,
85 btrfs_header_owner(eb));
33ca832f
JB
86 list_del(&eb->leak_list);
87 kmem_cache_free(extent_buffer_cache, eb);
88 }
3fd63727 89 spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
33ca832f
JB
90}
91
92static inline void btrfs_extent_state_leak_debug_check(void)
93{
94 struct extent_state *state;
95
6d49ba1b
ES
96 while (!list_empty(&states)) {
97 state = list_entry(states.next, struct extent_state, leak_list);
9ee49a04 98 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
27a3507d
FM
99 state->start, state->end, state->state,
100 extent_state_in_tree(state),
b7ac31b7 101 refcount_read(&state->refs));
6d49ba1b
ES
102 list_del(&state->leak_list);
103 kmem_cache_free(extent_state_cache, state);
104 }
6d49ba1b 105}
8d599ae1 106
a5dee37d
JB
107#define btrfs_debug_check_extent_io_range(tree, start, end) \
108 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
8d599ae1 109static inline void __btrfs_debug_check_extent_io_range(const char *caller,
a5dee37d 110 struct extent_io_tree *tree, u64 start, u64 end)
8d599ae1 111{
65a680f6
NB
112 struct inode *inode = tree->private_data;
113 u64 isize;
114
115 if (!inode || !is_data_inode(inode))
116 return;
117
118 isize = i_size_read(inode);
119 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
120 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
121 "%s: ino %llu isize %llu odd range [%llu,%llu]",
122 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
123 }
8d599ae1 124}
6d49ba1b 125#else
3fd63727
JB
126#define btrfs_leak_debug_add(lock, new, head) do {} while (0)
127#define btrfs_leak_debug_del(lock, entry) do {} while (0)
33ca832f 128#define btrfs_extent_state_leak_debug_check() do {} while (0)
8d599ae1 129#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
4bef0848 130#endif
d1310b2e 131
d1310b2e
CM
132struct tree_entry {
133 u64 start;
134 u64 end;
d1310b2e
CM
135 struct rb_node rb_node;
136};
137
138struct extent_page_data {
390ed29b 139 struct btrfs_bio_ctrl bio_ctrl;
771ed689
CM
140 /* tells writepage not to lock the state bits for this range
141 * it still does the unlocking
142 */
ffbd517d
CM
143 unsigned int extent_locked:1;
144
70fd7614 145 /* tells the submit_bio code to use REQ_SYNC */
ffbd517d 146 unsigned int sync_io:1;
d1310b2e
CM
147};
148
f97e27e9 149static int add_extent_changeset(struct extent_state *state, u32 bits,
d38ed27f
QW
150 struct extent_changeset *changeset,
151 int set)
152{
153 int ret;
154
155 if (!changeset)
57599c7e 156 return 0;
d38ed27f 157 if (set && (state->state & bits) == bits)
57599c7e 158 return 0;
fefdc557 159 if (!set && (state->state & bits) == 0)
57599c7e 160 return 0;
d38ed27f 161 changeset->bytes_changed += state->end - state->start + 1;
53d32359 162 ret = ulist_add(&changeset->range_changed, state->start, state->end,
d38ed27f 163 GFP_ATOMIC);
57599c7e 164 return ret;
d38ed27f
QW
165}
166
c1be9c1a
NB
167int __must_check submit_one_bio(struct bio *bio, int mirror_num,
168 unsigned long bio_flags)
bb58eb9e
QW
169{
170 blk_status_t ret = 0;
bb58eb9e 171 struct extent_io_tree *tree = bio->bi_private;
bb58eb9e
QW
172
173 bio->bi_private = NULL;
174
e0eefe07
QW
175 /* Caller should ensure the bio has at least some range added */
176 ASSERT(bio->bi_iter.bi_size);
908930f3
NB
177 if (is_data_inode(tree->private_data))
178 ret = btrfs_submit_data_bio(tree->private_data, bio, mirror_num,
179 bio_flags);
180 else
1b36294a
NB
181 ret = btrfs_submit_metadata_bio(tree->private_data, bio,
182 mirror_num, bio_flags);
bb58eb9e
QW
183
184 return blk_status_to_errno(ret);
185}
186
3065976b
QW
187/* Cleanup unsubmitted bios */
188static void end_write_bio(struct extent_page_data *epd, int ret)
189{
390ed29b
QW
190 struct bio *bio = epd->bio_ctrl.bio;
191
192 if (bio) {
193 bio->bi_status = errno_to_blk_status(ret);
194 bio_endio(bio);
195 epd->bio_ctrl.bio = NULL;
3065976b
QW
196 }
197}
198
f4340622
QW
199/*
200 * Submit bio from extent page data via submit_one_bio
201 *
202 * Return 0 if everything is OK.
203 * Return <0 for error.
204 */
205static int __must_check flush_write_bio(struct extent_page_data *epd)
bb58eb9e 206{
f4340622 207 int ret = 0;
390ed29b 208 struct bio *bio = epd->bio_ctrl.bio;
bb58eb9e 209
390ed29b
QW
210 if (bio) {
211 ret = submit_one_bio(bio, 0, 0);
f4340622
QW
212 /*
213 * Clean up of epd->bio is handled by its endio function.
214 * And endio is either triggered by successful bio execution
215 * or the error handler of submit bio hook.
216 * So at this point, no matter what happened, we don't need
217 * to clean up epd->bio.
218 */
390ed29b 219 epd->bio_ctrl.bio = NULL;
bb58eb9e 220 }
f4340622 221 return ret;
bb58eb9e 222}
e2932ee0 223
6f0d04f8 224int __init extent_state_cache_init(void)
d1310b2e 225{
837e1972 226 extent_state_cache = kmem_cache_create("btrfs_extent_state",
9601e3f6 227 sizeof(struct extent_state), 0,
fba4b697 228 SLAB_MEM_SPREAD, NULL);
d1310b2e
CM
229 if (!extent_state_cache)
230 return -ENOMEM;
6f0d04f8
JB
231 return 0;
232}
d1310b2e 233
6f0d04f8
JB
234int __init extent_io_init(void)
235{
837e1972 236 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
9601e3f6 237 sizeof(struct extent_buffer), 0,
fba4b697 238 SLAB_MEM_SPREAD, NULL);
d1310b2e 239 if (!extent_buffer_cache)
6f0d04f8 240 return -ENOMEM;
9be3395b 241
8ac9f7c1 242 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
c3a3b19b 243 offsetof(struct btrfs_bio, bio),
8ac9f7c1 244 BIOSET_NEED_BVECS))
9be3395b 245 goto free_buffer_cache;
b208c2f7 246
8ac9f7c1 247 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
b208c2f7
DW
248 goto free_bioset;
249
d1310b2e
CM
250 return 0;
251
b208c2f7 252free_bioset:
8ac9f7c1 253 bioset_exit(&btrfs_bioset);
b208c2f7 254
9be3395b
CM
255free_buffer_cache:
256 kmem_cache_destroy(extent_buffer_cache);
257 extent_buffer_cache = NULL;
6f0d04f8
JB
258 return -ENOMEM;
259}
9be3395b 260
6f0d04f8
JB
261void __cold extent_state_cache_exit(void)
262{
263 btrfs_extent_state_leak_debug_check();
d1310b2e 264 kmem_cache_destroy(extent_state_cache);
d1310b2e
CM
265}
266
e67c718b 267void __cold extent_io_exit(void)
d1310b2e 268{
8c0a8537
KS
269 /*
270 * Make sure all delayed rcu free are flushed before we
271 * destroy caches.
272 */
273 rcu_barrier();
5598e900 274 kmem_cache_destroy(extent_buffer_cache);
8ac9f7c1 275 bioset_exit(&btrfs_bioset);
d1310b2e
CM
276}
277
41a2ee75
JB
278/*
279 * For the file_extent_tree, we want to hold the inode lock when we lookup and
280 * update the disk_i_size, but lockdep will complain because our io_tree we hold
281 * the tree lock and get the inode lock when setting delalloc. These two things
282 * are unrelated, so make a class for the file_extent_tree so we don't get the
283 * two locking patterns mixed up.
284 */
285static struct lock_class_key file_extent_tree_class;
286
c258d6e3 287void extent_io_tree_init(struct btrfs_fs_info *fs_info,
43eb5f29
QW
288 struct extent_io_tree *tree, unsigned int owner,
289 void *private_data)
d1310b2e 290{
c258d6e3 291 tree->fs_info = fs_info;
6bef4d31 292 tree->state = RB_ROOT;
d1310b2e 293 tree->dirty_bytes = 0;
70dec807 294 spin_lock_init(&tree->lock);
c6100a4b 295 tree->private_data = private_data;
43eb5f29 296 tree->owner = owner;
41a2ee75
JB
297 if (owner == IO_TREE_INODE_FILE_EXTENT)
298 lockdep_set_class(&tree->lock, &file_extent_tree_class);
d1310b2e 299}
d1310b2e 300
41e7acd3
NB
301void extent_io_tree_release(struct extent_io_tree *tree)
302{
303 spin_lock(&tree->lock);
304 /*
305 * Do a single barrier for the waitqueue_active check here, the state
306 * of the waitqueue should not change once extent_io_tree_release is
307 * called.
308 */
309 smp_mb();
310 while (!RB_EMPTY_ROOT(&tree->state)) {
311 struct rb_node *node;
312 struct extent_state *state;
313
314 node = rb_first(&tree->state);
315 state = rb_entry(node, struct extent_state, rb_node);
316 rb_erase(&state->rb_node, &tree->state);
317 RB_CLEAR_NODE(&state->rb_node);
318 /*
319 * btree io trees aren't supposed to have tasks waiting for
320 * changes in the flags of extent states ever.
321 */
322 ASSERT(!waitqueue_active(&state->wq));
323 free_extent_state(state);
324
325 cond_resched_lock(&tree->lock);
326 }
327 spin_unlock(&tree->lock);
328}
329
b2950863 330static struct extent_state *alloc_extent_state(gfp_t mask)
d1310b2e
CM
331{
332 struct extent_state *state;
d1310b2e 333
3ba7ab22
MH
334 /*
335 * The given mask might be not appropriate for the slab allocator,
336 * drop the unsupported bits
337 */
338 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
d1310b2e 339 state = kmem_cache_alloc(extent_state_cache, mask);
2b114d1d 340 if (!state)
d1310b2e
CM
341 return state;
342 state->state = 0;
47dc196a 343 state->failrec = NULL;
27a3507d 344 RB_CLEAR_NODE(&state->rb_node);
3fd63727 345 btrfs_leak_debug_add(&leak_lock, &state->leak_list, &states);
b7ac31b7 346 refcount_set(&state->refs, 1);
d1310b2e 347 init_waitqueue_head(&state->wq);
143bede5 348 trace_alloc_extent_state(state, mask, _RET_IP_);
d1310b2e
CM
349 return state;
350}
d1310b2e 351
4845e44f 352void free_extent_state(struct extent_state *state)
d1310b2e 353{
d1310b2e
CM
354 if (!state)
355 return;
b7ac31b7 356 if (refcount_dec_and_test(&state->refs)) {
27a3507d 357 WARN_ON(extent_state_in_tree(state));
3fd63727 358 btrfs_leak_debug_del(&leak_lock, &state->leak_list);
143bede5 359 trace_free_extent_state(state, _RET_IP_);
d1310b2e
CM
360 kmem_cache_free(extent_state_cache, state);
361 }
362}
d1310b2e 363
f2071b21
FM
364static struct rb_node *tree_insert(struct rb_root *root,
365 struct rb_node *search_start,
366 u64 offset,
12cfbad9
FDBM
367 struct rb_node *node,
368 struct rb_node ***p_in,
369 struct rb_node **parent_in)
d1310b2e 370{
f2071b21 371 struct rb_node **p;
d397712b 372 struct rb_node *parent = NULL;
d1310b2e
CM
373 struct tree_entry *entry;
374
12cfbad9
FDBM
375 if (p_in && parent_in) {
376 p = *p_in;
377 parent = *parent_in;
378 goto do_insert;
379 }
380
f2071b21 381 p = search_start ? &search_start : &root->rb_node;
d397712b 382 while (*p) {
d1310b2e
CM
383 parent = *p;
384 entry = rb_entry(parent, struct tree_entry, rb_node);
385
386 if (offset < entry->start)
387 p = &(*p)->rb_left;
388 else if (offset > entry->end)
389 p = &(*p)->rb_right;
390 else
391 return parent;
392 }
393
12cfbad9 394do_insert:
d1310b2e
CM
395 rb_link_node(node, parent, p);
396 rb_insert_color(node, root);
397 return NULL;
398}
399
8666e638 400/**
3bed2da1
NB
401 * Search @tree for an entry that contains @offset. Such entry would have
402 * entry->start <= offset && entry->end >= offset.
8666e638 403 *
3bed2da1
NB
404 * @tree: the tree to search
405 * @offset: offset that should fall within an entry in @tree
406 * @next_ret: pointer to the first entry whose range ends after @offset
407 * @prev_ret: pointer to the first entry whose range begins before @offset
408 * @p_ret: pointer where new node should be anchored (used when inserting an
409 * entry in the tree)
410 * @parent_ret: points to entry which would have been the parent of the entry,
8666e638
NB
411 * containing @offset
412 *
413 * This function returns a pointer to the entry that contains @offset byte
414 * address. If no such entry exists, then NULL is returned and the other
415 * pointer arguments to the function are filled, otherwise the found entry is
416 * returned and other pointers are left untouched.
417 */
80ea96b1 418static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
12cfbad9 419 struct rb_node **next_ret,
352646c7 420 struct rb_node **prev_ret,
12cfbad9
FDBM
421 struct rb_node ***p_ret,
422 struct rb_node **parent_ret)
d1310b2e 423{
80ea96b1 424 struct rb_root *root = &tree->state;
12cfbad9 425 struct rb_node **n = &root->rb_node;
d1310b2e
CM
426 struct rb_node *prev = NULL;
427 struct rb_node *orig_prev = NULL;
428 struct tree_entry *entry;
429 struct tree_entry *prev_entry = NULL;
430
12cfbad9
FDBM
431 while (*n) {
432 prev = *n;
433 entry = rb_entry(prev, struct tree_entry, rb_node);
d1310b2e
CM
434 prev_entry = entry;
435
436 if (offset < entry->start)
12cfbad9 437 n = &(*n)->rb_left;
d1310b2e 438 else if (offset > entry->end)
12cfbad9 439 n = &(*n)->rb_right;
d397712b 440 else
12cfbad9 441 return *n;
d1310b2e
CM
442 }
443
12cfbad9
FDBM
444 if (p_ret)
445 *p_ret = n;
446 if (parent_ret)
447 *parent_ret = prev;
448
352646c7 449 if (next_ret) {
d1310b2e 450 orig_prev = prev;
d397712b 451 while (prev && offset > prev_entry->end) {
d1310b2e
CM
452 prev = rb_next(prev);
453 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
454 }
352646c7 455 *next_ret = prev;
d1310b2e
CM
456 prev = orig_prev;
457 }
458
352646c7 459 if (prev_ret) {
d1310b2e 460 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
d397712b 461 while (prev && offset < prev_entry->start) {
d1310b2e
CM
462 prev = rb_prev(prev);
463 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
464 }
352646c7 465 *prev_ret = prev;
d1310b2e
CM
466 }
467 return NULL;
468}
469
12cfbad9
FDBM
470static inline struct rb_node *
471tree_search_for_insert(struct extent_io_tree *tree,
472 u64 offset,
473 struct rb_node ***p_ret,
474 struct rb_node **parent_ret)
d1310b2e 475{
352646c7 476 struct rb_node *next= NULL;
d1310b2e 477 struct rb_node *ret;
70dec807 478
352646c7 479 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
d397712b 480 if (!ret)
352646c7 481 return next;
d1310b2e
CM
482 return ret;
483}
484
12cfbad9
FDBM
485static inline struct rb_node *tree_search(struct extent_io_tree *tree,
486 u64 offset)
487{
488 return tree_search_for_insert(tree, offset, NULL, NULL);
489}
490
d1310b2e
CM
491/*
492 * utility function to look for merge candidates inside a given range.
493 * Any extents with matching state are merged together into a single
494 * extent in the tree. Extents with EXTENT_IO in their state field
495 * are not merged because the end_io handlers need to be able to do
496 * operations on them without sleeping (or doing allocations/splits).
497 *
498 * This should be called with the tree lock held.
499 */
1bf85046
JM
500static void merge_state(struct extent_io_tree *tree,
501 struct extent_state *state)
d1310b2e
CM
502{
503 struct extent_state *other;
504 struct rb_node *other_node;
505
8882679e 506 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
1bf85046 507 return;
d1310b2e
CM
508
509 other_node = rb_prev(&state->rb_node);
510 if (other_node) {
511 other = rb_entry(other_node, struct extent_state, rb_node);
512 if (other->end == state->start - 1 &&
513 other->state == state->state) {
5c848198
NB
514 if (tree->private_data &&
515 is_data_inode(tree->private_data))
516 btrfs_merge_delalloc_extent(tree->private_data,
517 state, other);
d1310b2e 518 state->start = other->start;
d1310b2e 519 rb_erase(&other->rb_node, &tree->state);
27a3507d 520 RB_CLEAR_NODE(&other->rb_node);
d1310b2e
CM
521 free_extent_state(other);
522 }
523 }
524 other_node = rb_next(&state->rb_node);
525 if (other_node) {
526 other = rb_entry(other_node, struct extent_state, rb_node);
527 if (other->start == state->end + 1 &&
528 other->state == state->state) {
5c848198
NB
529 if (tree->private_data &&
530 is_data_inode(tree->private_data))
531 btrfs_merge_delalloc_extent(tree->private_data,
532 state, other);
df98b6e2 533 state->end = other->end;
df98b6e2 534 rb_erase(&other->rb_node, &tree->state);
27a3507d 535 RB_CLEAR_NODE(&other->rb_node);
df98b6e2 536 free_extent_state(other);
d1310b2e
CM
537 }
538 }
d1310b2e
CM
539}
540
3150b699 541static void set_state_bits(struct extent_io_tree *tree,
f97e27e9 542 struct extent_state *state, u32 *bits,
d38ed27f 543 struct extent_changeset *changeset);
3150b699 544
d1310b2e
CM
545/*
546 * insert an extent_state struct into the tree. 'bits' are set on the
547 * struct before it is inserted.
548 *
549 * This may return -EEXIST if the extent is already there, in which case the
550 * state struct is freed.
551 *
552 * The tree lock is not taken internally. This is a utility function and
553 * probably isn't what you want to call (see set/clear_extent_bit).
554 */
555static int insert_state(struct extent_io_tree *tree,
556 struct extent_state *state, u64 start, u64 end,
12cfbad9
FDBM
557 struct rb_node ***p,
558 struct rb_node **parent,
f97e27e9 559 u32 *bits, struct extent_changeset *changeset)
d1310b2e
CM
560{
561 struct rb_node *node;
562
2792237d
DS
563 if (end < start) {
564 btrfs_err(tree->fs_info,
565 "insert state: end < start %llu %llu", end, start);
566 WARN_ON(1);
567 }
d1310b2e
CM
568 state->start = start;
569 state->end = end;
9ed74f2d 570
d38ed27f 571 set_state_bits(tree, state, bits, changeset);
3150b699 572
f2071b21 573 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
d1310b2e
CM
574 if (node) {
575 struct extent_state *found;
576 found = rb_entry(node, struct extent_state, rb_node);
2792237d
DS
577 btrfs_err(tree->fs_info,
578 "found node %llu %llu on insert of %llu %llu",
c1c9ff7c 579 found->start, found->end, start, end);
d1310b2e
CM
580 return -EEXIST;
581 }
582 merge_state(tree, state);
583 return 0;
584}
585
586/*
587 * split a given extent state struct in two, inserting the preallocated
588 * struct 'prealloc' as the newly created second half. 'split' indicates an
589 * offset inside 'orig' where it should be split.
590 *
591 * Before calling,
592 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
593 * are two extent state structs in the tree:
594 * prealloc: [orig->start, split - 1]
595 * orig: [ split, orig->end ]
596 *
597 * The tree locks are not taken by this function. They need to be held
598 * by the caller.
599 */
600static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
601 struct extent_state *prealloc, u64 split)
602{
603 struct rb_node *node;
9ed74f2d 604
abbb55f4
NB
605 if (tree->private_data && is_data_inode(tree->private_data))
606 btrfs_split_delalloc_extent(tree->private_data, orig, split);
9ed74f2d 607
d1310b2e
CM
608 prealloc->start = orig->start;
609 prealloc->end = split - 1;
610 prealloc->state = orig->state;
611 orig->start = split;
612
f2071b21
FM
613 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
614 &prealloc->rb_node, NULL, NULL);
d1310b2e 615 if (node) {
d1310b2e
CM
616 free_extent_state(prealloc);
617 return -EEXIST;
618 }
619 return 0;
620}
621
cdc6a395
LZ
622static struct extent_state *next_state(struct extent_state *state)
623{
624 struct rb_node *next = rb_next(&state->rb_node);
625 if (next)
626 return rb_entry(next, struct extent_state, rb_node);
627 else
628 return NULL;
629}
630
d1310b2e
CM
631/*
632 * utility function to clear some bits in an extent state struct.
52042d8e 633 * it will optionally wake up anyone waiting on this state (wake == 1).
d1310b2e
CM
634 *
635 * If no bits are set on the state struct after clearing things, the
636 * struct is freed and removed from the tree
637 */
cdc6a395
LZ
638static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
639 struct extent_state *state,
f97e27e9 640 u32 *bits, int wake,
fefdc557 641 struct extent_changeset *changeset)
d1310b2e 642{
cdc6a395 643 struct extent_state *next;
f97e27e9 644 u32 bits_to_clear = *bits & ~EXTENT_CTLBITS;
57599c7e 645 int ret;
d1310b2e 646
0ca1f7ce 647 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
d1310b2e
CM
648 u64 range = state->end - state->start + 1;
649 WARN_ON(range > tree->dirty_bytes);
650 tree->dirty_bytes -= range;
651 }
a36bb5f9
NB
652
653 if (tree->private_data && is_data_inode(tree->private_data))
654 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
655
57599c7e
DS
656 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
657 BUG_ON(ret < 0);
32c00aff 658 state->state &= ~bits_to_clear;
d1310b2e
CM
659 if (wake)
660 wake_up(&state->wq);
0ca1f7ce 661 if (state->state == 0) {
cdc6a395 662 next = next_state(state);
27a3507d 663 if (extent_state_in_tree(state)) {
d1310b2e 664 rb_erase(&state->rb_node, &tree->state);
27a3507d 665 RB_CLEAR_NODE(&state->rb_node);
d1310b2e
CM
666 free_extent_state(state);
667 } else {
668 WARN_ON(1);
669 }
670 } else {
671 merge_state(tree, state);
cdc6a395 672 next = next_state(state);
d1310b2e 673 }
cdc6a395 674 return next;
d1310b2e
CM
675}
676
8233767a
XG
677static struct extent_state *
678alloc_extent_state_atomic(struct extent_state *prealloc)
679{
680 if (!prealloc)
681 prealloc = alloc_extent_state(GFP_ATOMIC);
682
683 return prealloc;
684}
685
48a3b636 686static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
c2d904e0 687{
29b665cc 688 btrfs_panic(tree->fs_info, err,
05912a3c 689 "locking error: extent tree was modified by another thread while locked");
c2d904e0
JM
690}
691
d1310b2e
CM
692/*
693 * clear some bits on a range in the tree. This may require splitting
694 * or inserting elements in the tree, so the gfp mask is used to
695 * indicate which allocations or sleeping are allowed.
696 *
697 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
698 * the given range from the tree regardless of state (ie for truncate).
699 *
700 * the range [start, end] is inclusive.
701 *
6763af84 702 * This takes the tree lock, and returns 0 on success and < 0 on error.
d1310b2e 703 */
66b0c887 704int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
f97e27e9
QW
705 u32 bits, int wake, int delete,
706 struct extent_state **cached_state,
707 gfp_t mask, struct extent_changeset *changeset)
d1310b2e
CM
708{
709 struct extent_state *state;
2c64c53d 710 struct extent_state *cached;
d1310b2e
CM
711 struct extent_state *prealloc = NULL;
712 struct rb_node *node;
5c939df5 713 u64 last_end;
d1310b2e 714 int err;
2ac55d41 715 int clear = 0;
d1310b2e 716
a5dee37d 717 btrfs_debug_check_extent_io_range(tree, start, end);
a1d19847 718 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
8d599ae1 719
7ee9e440
JB
720 if (bits & EXTENT_DELALLOC)
721 bits |= EXTENT_NORESERVE;
722
0ca1f7ce
YZ
723 if (delete)
724 bits |= ~EXTENT_CTLBITS;
0ca1f7ce 725
8882679e 726 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
2ac55d41 727 clear = 1;
d1310b2e 728again:
d0164adc 729 if (!prealloc && gfpflags_allow_blocking(mask)) {
c7bc6319
FM
730 /*
731 * Don't care for allocation failure here because we might end
732 * up not needing the pre-allocated extent state at all, which
733 * is the case if we only have in the tree extent states that
734 * cover our input range and don't cover too any other range.
735 * If we end up needing a new extent state we allocate it later.
736 */
d1310b2e 737 prealloc = alloc_extent_state(mask);
d1310b2e
CM
738 }
739
cad321ad 740 spin_lock(&tree->lock);
2c64c53d
CM
741 if (cached_state) {
742 cached = *cached_state;
2ac55d41
JB
743
744 if (clear) {
745 *cached_state = NULL;
746 cached_state = NULL;
747 }
748
27a3507d
FM
749 if (cached && extent_state_in_tree(cached) &&
750 cached->start <= start && cached->end > start) {
2ac55d41 751 if (clear)
b7ac31b7 752 refcount_dec(&cached->refs);
2c64c53d 753 state = cached;
42daec29 754 goto hit_next;
2c64c53d 755 }
2ac55d41
JB
756 if (clear)
757 free_extent_state(cached);
2c64c53d 758 }
d1310b2e
CM
759 /*
760 * this search will find the extents that end after
761 * our range starts
762 */
80ea96b1 763 node = tree_search(tree, start);
d1310b2e
CM
764 if (!node)
765 goto out;
766 state = rb_entry(node, struct extent_state, rb_node);
2c64c53d 767hit_next:
d1310b2e
CM
768 if (state->start > end)
769 goto out;
770 WARN_ON(state->end < start);
5c939df5 771 last_end = state->end;
d1310b2e 772
0449314a 773 /* the state doesn't have the wanted bits, go ahead */
cdc6a395
LZ
774 if (!(state->state & bits)) {
775 state = next_state(state);
0449314a 776 goto next;
cdc6a395 777 }
0449314a 778
d1310b2e
CM
779 /*
780 * | ---- desired range ---- |
781 * | state | or
782 * | ------------- state -------------- |
783 *
784 * We need to split the extent we found, and may flip
785 * bits on second half.
786 *
787 * If the extent we found extends past our range, we
788 * just split and search again. It'll get split again
789 * the next time though.
790 *
791 * If the extent we found is inside our range, we clear
792 * the desired bit on it.
793 */
794
795 if (state->start < start) {
8233767a
XG
796 prealloc = alloc_extent_state_atomic(prealloc);
797 BUG_ON(!prealloc);
d1310b2e 798 err = split_state(tree, state, prealloc, start);
c2d904e0
JM
799 if (err)
800 extent_io_tree_panic(tree, err);
801
d1310b2e
CM
802 prealloc = NULL;
803 if (err)
804 goto out;
805 if (state->end <= end) {
fefdc557
QW
806 state = clear_state_bit(tree, state, &bits, wake,
807 changeset);
d1ac6e41 808 goto next;
d1310b2e
CM
809 }
810 goto search_again;
811 }
812 /*
813 * | ---- desired range ---- |
814 * | state |
815 * We need to split the extent, and clear the bit
816 * on the first half
817 */
818 if (state->start <= end && state->end > end) {
8233767a
XG
819 prealloc = alloc_extent_state_atomic(prealloc);
820 BUG_ON(!prealloc);
d1310b2e 821 err = split_state(tree, state, prealloc, end + 1);
c2d904e0
JM
822 if (err)
823 extent_io_tree_panic(tree, err);
824
d1310b2e
CM
825 if (wake)
826 wake_up(&state->wq);
42daec29 827
fefdc557 828 clear_state_bit(tree, prealloc, &bits, wake, changeset);
9ed74f2d 829
d1310b2e
CM
830 prealloc = NULL;
831 goto out;
832 }
42daec29 833
fefdc557 834 state = clear_state_bit(tree, state, &bits, wake, changeset);
0449314a 835next:
5c939df5
YZ
836 if (last_end == (u64)-1)
837 goto out;
838 start = last_end + 1;
cdc6a395 839 if (start <= end && state && !need_resched())
692e5759 840 goto hit_next;
d1310b2e
CM
841
842search_again:
843 if (start > end)
844 goto out;
cad321ad 845 spin_unlock(&tree->lock);
d0164adc 846 if (gfpflags_allow_blocking(mask))
d1310b2e
CM
847 cond_resched();
848 goto again;
7ab5cb2a
DS
849
850out:
851 spin_unlock(&tree->lock);
852 if (prealloc)
853 free_extent_state(prealloc);
854
855 return 0;
856
d1310b2e 857}
d1310b2e 858
143bede5
JM
859static void wait_on_state(struct extent_io_tree *tree,
860 struct extent_state *state)
641f5219
CH
861 __releases(tree->lock)
862 __acquires(tree->lock)
d1310b2e
CM
863{
864 DEFINE_WAIT(wait);
865 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
cad321ad 866 spin_unlock(&tree->lock);
d1310b2e 867 schedule();
cad321ad 868 spin_lock(&tree->lock);
d1310b2e 869 finish_wait(&state->wq, &wait);
d1310b2e
CM
870}
871
872/*
873 * waits for one or more bits to clear on a range in the state tree.
874 * The range [start, end] is inclusive.
875 * The tree lock is taken by this function
876 */
41074888 877static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
f97e27e9 878 u32 bits)
d1310b2e
CM
879{
880 struct extent_state *state;
881 struct rb_node *node;
882
a5dee37d 883 btrfs_debug_check_extent_io_range(tree, start, end);
8d599ae1 884
cad321ad 885 spin_lock(&tree->lock);
d1310b2e
CM
886again:
887 while (1) {
888 /*
889 * this search will find all the extents that end after
890 * our range starts
891 */
80ea96b1 892 node = tree_search(tree, start);
c50d3e71 893process_node:
d1310b2e
CM
894 if (!node)
895 break;
896
897 state = rb_entry(node, struct extent_state, rb_node);
898
899 if (state->start > end)
900 goto out;
901
902 if (state->state & bits) {
903 start = state->start;
b7ac31b7 904 refcount_inc(&state->refs);
d1310b2e
CM
905 wait_on_state(tree, state);
906 free_extent_state(state);
907 goto again;
908 }
909 start = state->end + 1;
910
911 if (start > end)
912 break;
913
c50d3e71
FM
914 if (!cond_resched_lock(&tree->lock)) {
915 node = rb_next(node);
916 goto process_node;
917 }
d1310b2e
CM
918 }
919out:
cad321ad 920 spin_unlock(&tree->lock);
d1310b2e 921}
d1310b2e 922
1bf85046 923static void set_state_bits(struct extent_io_tree *tree,
d1310b2e 924 struct extent_state *state,
f97e27e9 925 u32 *bits, struct extent_changeset *changeset)
d1310b2e 926{
f97e27e9 927 u32 bits_to_set = *bits & ~EXTENT_CTLBITS;
57599c7e 928 int ret;
9ed74f2d 929
e06a1fc9
NB
930 if (tree->private_data && is_data_inode(tree->private_data))
931 btrfs_set_delalloc_extent(tree->private_data, state, bits);
932
0ca1f7ce 933 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
d1310b2e
CM
934 u64 range = state->end - state->start + 1;
935 tree->dirty_bytes += range;
936 }
57599c7e
DS
937 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
938 BUG_ON(ret < 0);
0ca1f7ce 939 state->state |= bits_to_set;
d1310b2e
CM
940}
941
e38e2ed7
FM
942static void cache_state_if_flags(struct extent_state *state,
943 struct extent_state **cached_ptr,
9ee49a04 944 unsigned flags)
2c64c53d
CM
945{
946 if (cached_ptr && !(*cached_ptr)) {
e38e2ed7 947 if (!flags || (state->state & flags)) {
2c64c53d 948 *cached_ptr = state;
b7ac31b7 949 refcount_inc(&state->refs);
2c64c53d
CM
950 }
951 }
952}
953
e38e2ed7
FM
954static void cache_state(struct extent_state *state,
955 struct extent_state **cached_ptr)
956{
957 return cache_state_if_flags(state, cached_ptr,
8882679e 958 EXTENT_LOCKED | EXTENT_BOUNDARY);
e38e2ed7
FM
959}
960
d1310b2e 961/*
1edbb734
CM
962 * set some bits on a range in the tree. This may require allocations or
963 * sleeping, so the gfp mask is used to indicate what is allowed.
d1310b2e 964 *
1edbb734
CM
965 * If any of the exclusive bits are set, this will fail with -EEXIST if some
966 * part of the range already has the desired bits set. The start of the
967 * existing range is returned in failed_start in this case.
d1310b2e 968 *
1edbb734 969 * [start, end] is inclusive This takes the tree lock.
d1310b2e 970 */
f97e27e9
QW
971int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
972 u32 exclusive_bits, u64 *failed_start,
1cab5e72
NB
973 struct extent_state **cached_state, gfp_t mask,
974 struct extent_changeset *changeset)
d1310b2e
CM
975{
976 struct extent_state *state;
977 struct extent_state *prealloc = NULL;
978 struct rb_node *node;
12cfbad9
FDBM
979 struct rb_node **p;
980 struct rb_node *parent;
d1310b2e 981 int err = 0;
d1310b2e
CM
982 u64 last_start;
983 u64 last_end;
42daec29 984
a5dee37d 985 btrfs_debug_check_extent_io_range(tree, start, end);
a1d19847 986 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
8d599ae1 987
3f6bb4ae
QW
988 if (exclusive_bits)
989 ASSERT(failed_start);
990 else
991 ASSERT(failed_start == NULL);
d1310b2e 992again:
d0164adc 993 if (!prealloc && gfpflags_allow_blocking(mask)) {
059f791c
DS
994 /*
995 * Don't care for allocation failure here because we might end
996 * up not needing the pre-allocated extent state at all, which
997 * is the case if we only have in the tree extent states that
998 * cover our input range and don't cover too any other range.
999 * If we end up needing a new extent state we allocate it later.
1000 */
d1310b2e 1001 prealloc = alloc_extent_state(mask);
d1310b2e
CM
1002 }
1003
cad321ad 1004 spin_lock(&tree->lock);
9655d298
CM
1005 if (cached_state && *cached_state) {
1006 state = *cached_state;
df98b6e2 1007 if (state->start <= start && state->end > start &&
27a3507d 1008 extent_state_in_tree(state)) {
9655d298
CM
1009 node = &state->rb_node;
1010 goto hit_next;
1011 }
1012 }
d1310b2e
CM
1013 /*
1014 * this search will find all the extents that end after
1015 * our range starts.
1016 */
12cfbad9 1017 node = tree_search_for_insert(tree, start, &p, &parent);
d1310b2e 1018 if (!node) {
8233767a
XG
1019 prealloc = alloc_extent_state_atomic(prealloc);
1020 BUG_ON(!prealloc);
12cfbad9 1021 err = insert_state(tree, prealloc, start, end,
d38ed27f 1022 &p, &parent, &bits, changeset);
c2d904e0
JM
1023 if (err)
1024 extent_io_tree_panic(tree, err);
1025
c42ac0bc 1026 cache_state(prealloc, cached_state);
d1310b2e 1027 prealloc = NULL;
d1310b2e
CM
1028 goto out;
1029 }
d1310b2e 1030 state = rb_entry(node, struct extent_state, rb_node);
40431d6c 1031hit_next:
d1310b2e
CM
1032 last_start = state->start;
1033 last_end = state->end;
1034
1035 /*
1036 * | ---- desired range ---- |
1037 * | state |
1038 *
1039 * Just lock what we found and keep going
1040 */
1041 if (state->start == start && state->end <= end) {
1edbb734 1042 if (state->state & exclusive_bits) {
d1310b2e
CM
1043 *failed_start = state->start;
1044 err = -EEXIST;
1045 goto out;
1046 }
42daec29 1047
d38ed27f 1048 set_state_bits(tree, state, &bits, changeset);
2c64c53d 1049 cache_state(state, cached_state);
d1310b2e 1050 merge_state(tree, state);
5c939df5
YZ
1051 if (last_end == (u64)-1)
1052 goto out;
1053 start = last_end + 1;
d1ac6e41
LB
1054 state = next_state(state);
1055 if (start < end && state && state->start == start &&
1056 !need_resched())
1057 goto hit_next;
d1310b2e
CM
1058 goto search_again;
1059 }
1060
1061 /*
1062 * | ---- desired range ---- |
1063 * | state |
1064 * or
1065 * | ------------- state -------------- |
1066 *
1067 * We need to split the extent we found, and may flip bits on
1068 * second half.
1069 *
1070 * If the extent we found extends past our
1071 * range, we just split and search again. It'll get split
1072 * again the next time though.
1073 *
1074 * If the extent we found is inside our range, we set the
1075 * desired bit on it.
1076 */
1077 if (state->start < start) {
1edbb734 1078 if (state->state & exclusive_bits) {
d1310b2e
CM
1079 *failed_start = start;
1080 err = -EEXIST;
1081 goto out;
1082 }
8233767a 1083
55ffaabe
FM
1084 /*
1085 * If this extent already has all the bits we want set, then
1086 * skip it, not necessary to split it or do anything with it.
1087 */
1088 if ((state->state & bits) == bits) {
1089 start = state->end + 1;
1090 cache_state(state, cached_state);
1091 goto search_again;
1092 }
1093
8233767a
XG
1094 prealloc = alloc_extent_state_atomic(prealloc);
1095 BUG_ON(!prealloc);
d1310b2e 1096 err = split_state(tree, state, prealloc, start);
c2d904e0
JM
1097 if (err)
1098 extent_io_tree_panic(tree, err);
1099
d1310b2e
CM
1100 prealloc = NULL;
1101 if (err)
1102 goto out;
1103 if (state->end <= end) {
d38ed27f 1104 set_state_bits(tree, state, &bits, changeset);
2c64c53d 1105 cache_state(state, cached_state);
d1310b2e 1106 merge_state(tree, state);
5c939df5
YZ
1107 if (last_end == (u64)-1)
1108 goto out;
1109 start = last_end + 1;
d1ac6e41
LB
1110 state = next_state(state);
1111 if (start < end && state && state->start == start &&
1112 !need_resched())
1113 goto hit_next;
d1310b2e
CM
1114 }
1115 goto search_again;
1116 }
1117 /*
1118 * | ---- desired range ---- |
1119 * | state | or | state |
1120 *
1121 * There's a hole, we need to insert something in it and
1122 * ignore the extent we found.
1123 */
1124 if (state->start > start) {
1125 u64 this_end;
1126 if (end < last_start)
1127 this_end = end;
1128 else
d397712b 1129 this_end = last_start - 1;
8233767a
XG
1130
1131 prealloc = alloc_extent_state_atomic(prealloc);
1132 BUG_ON(!prealloc);
c7f895a2
XG
1133
1134 /*
1135 * Avoid to free 'prealloc' if it can be merged with
1136 * the later extent.
1137 */
d1310b2e 1138 err = insert_state(tree, prealloc, start, this_end,
d38ed27f 1139 NULL, NULL, &bits, changeset);
c2d904e0
JM
1140 if (err)
1141 extent_io_tree_panic(tree, err);
1142
9ed74f2d
JB
1143 cache_state(prealloc, cached_state);
1144 prealloc = NULL;
d1310b2e
CM
1145 start = this_end + 1;
1146 goto search_again;
1147 }
1148 /*
1149 * | ---- desired range ---- |
1150 * | state |
1151 * We need to split the extent, and set the bit
1152 * on the first half
1153 */
1154 if (state->start <= end && state->end > end) {
1edbb734 1155 if (state->state & exclusive_bits) {
d1310b2e
CM
1156 *failed_start = start;
1157 err = -EEXIST;
1158 goto out;
1159 }
8233767a
XG
1160
1161 prealloc = alloc_extent_state_atomic(prealloc);
1162 BUG_ON(!prealloc);
d1310b2e 1163 err = split_state(tree, state, prealloc, end + 1);
c2d904e0
JM
1164 if (err)
1165 extent_io_tree_panic(tree, err);
d1310b2e 1166
d38ed27f 1167 set_state_bits(tree, prealloc, &bits, changeset);
2c64c53d 1168 cache_state(prealloc, cached_state);
d1310b2e
CM
1169 merge_state(tree, prealloc);
1170 prealloc = NULL;
1171 goto out;
1172 }
1173
b5a4ba14
DS
1174search_again:
1175 if (start > end)
1176 goto out;
1177 spin_unlock(&tree->lock);
1178 if (gfpflags_allow_blocking(mask))
1179 cond_resched();
1180 goto again;
d1310b2e
CM
1181
1182out:
cad321ad 1183 spin_unlock(&tree->lock);
d1310b2e
CM
1184 if (prealloc)
1185 free_extent_state(prealloc);
1186
1187 return err;
1188
d1310b2e 1189}
d1310b2e 1190
462d6fac 1191/**
10983f2e
LB
1192 * convert_extent_bit - convert all bits in a given range from one bit to
1193 * another
462d6fac
JB
1194 * @tree: the io tree to search
1195 * @start: the start offset in bytes
1196 * @end: the end offset in bytes (inclusive)
1197 * @bits: the bits to set in this range
1198 * @clear_bits: the bits to clear in this range
e6138876 1199 * @cached_state: state that we're going to cache
462d6fac
JB
1200 *
1201 * This will go through and set bits for the given range. If any states exist
1202 * already in this range they are set with the given bit and cleared of the
1203 * clear_bits. This is only meant to be used by things that are mergeable, ie
1204 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1205 * boundary bits like LOCK.
210aa277
DS
1206 *
1207 * All allocations are done with GFP_NOFS.
462d6fac
JB
1208 */
1209int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
f97e27e9 1210 u32 bits, u32 clear_bits,
210aa277 1211 struct extent_state **cached_state)
462d6fac
JB
1212{
1213 struct extent_state *state;
1214 struct extent_state *prealloc = NULL;
1215 struct rb_node *node;
12cfbad9
FDBM
1216 struct rb_node **p;
1217 struct rb_node *parent;
462d6fac
JB
1218 int err = 0;
1219 u64 last_start;
1220 u64 last_end;
c8fd3de7 1221 bool first_iteration = true;
462d6fac 1222
a5dee37d 1223 btrfs_debug_check_extent_io_range(tree, start, end);
a1d19847
QW
1224 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1225 clear_bits);
8d599ae1 1226
462d6fac 1227again:
210aa277 1228 if (!prealloc) {
c8fd3de7
FM
1229 /*
1230 * Best effort, don't worry if extent state allocation fails
1231 * here for the first iteration. We might have a cached state
1232 * that matches exactly the target range, in which case no
1233 * extent state allocations are needed. We'll only know this
1234 * after locking the tree.
1235 */
210aa277 1236 prealloc = alloc_extent_state(GFP_NOFS);
c8fd3de7 1237 if (!prealloc && !first_iteration)
462d6fac
JB
1238 return -ENOMEM;
1239 }
1240
1241 spin_lock(&tree->lock);
e6138876
JB
1242 if (cached_state && *cached_state) {
1243 state = *cached_state;
1244 if (state->start <= start && state->end > start &&
27a3507d 1245 extent_state_in_tree(state)) {
e6138876
JB
1246 node = &state->rb_node;
1247 goto hit_next;
1248 }
1249 }
1250
462d6fac
JB
1251 /*
1252 * this search will find all the extents that end after
1253 * our range starts.
1254 */
12cfbad9 1255 node = tree_search_for_insert(tree, start, &p, &parent);
462d6fac
JB
1256 if (!node) {
1257 prealloc = alloc_extent_state_atomic(prealloc);
1cf4ffdb
LB
1258 if (!prealloc) {
1259 err = -ENOMEM;
1260 goto out;
1261 }
12cfbad9 1262 err = insert_state(tree, prealloc, start, end,
d38ed27f 1263 &p, &parent, &bits, NULL);
c2d904e0
JM
1264 if (err)
1265 extent_io_tree_panic(tree, err);
c42ac0bc
FDBM
1266 cache_state(prealloc, cached_state);
1267 prealloc = NULL;
462d6fac
JB
1268 goto out;
1269 }
1270 state = rb_entry(node, struct extent_state, rb_node);
1271hit_next:
1272 last_start = state->start;
1273 last_end = state->end;
1274
1275 /*
1276 * | ---- desired range ---- |
1277 * | state |
1278 *
1279 * Just lock what we found and keep going
1280 */
1281 if (state->start == start && state->end <= end) {
d38ed27f 1282 set_state_bits(tree, state, &bits, NULL);
e6138876 1283 cache_state(state, cached_state);
fefdc557 1284 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
462d6fac
JB
1285 if (last_end == (u64)-1)
1286 goto out;
462d6fac 1287 start = last_end + 1;
d1ac6e41
LB
1288 if (start < end && state && state->start == start &&
1289 !need_resched())
1290 goto hit_next;
462d6fac
JB
1291 goto search_again;
1292 }
1293
1294 /*
1295 * | ---- desired range ---- |
1296 * | state |
1297 * or
1298 * | ------------- state -------------- |
1299 *
1300 * We need to split the extent we found, and may flip bits on
1301 * second half.
1302 *
1303 * If the extent we found extends past our
1304 * range, we just split and search again. It'll get split
1305 * again the next time though.
1306 *
1307 * If the extent we found is inside our range, we set the
1308 * desired bit on it.
1309 */
1310 if (state->start < start) {
1311 prealloc = alloc_extent_state_atomic(prealloc);
1cf4ffdb
LB
1312 if (!prealloc) {
1313 err = -ENOMEM;
1314 goto out;
1315 }
462d6fac 1316 err = split_state(tree, state, prealloc, start);
c2d904e0
JM
1317 if (err)
1318 extent_io_tree_panic(tree, err);
462d6fac
JB
1319 prealloc = NULL;
1320 if (err)
1321 goto out;
1322 if (state->end <= end) {
d38ed27f 1323 set_state_bits(tree, state, &bits, NULL);
e6138876 1324 cache_state(state, cached_state);
fefdc557
QW
1325 state = clear_state_bit(tree, state, &clear_bits, 0,
1326 NULL);
462d6fac
JB
1327 if (last_end == (u64)-1)
1328 goto out;
1329 start = last_end + 1;
d1ac6e41
LB
1330 if (start < end && state && state->start == start &&
1331 !need_resched())
1332 goto hit_next;
462d6fac
JB
1333 }
1334 goto search_again;
1335 }
1336 /*
1337 * | ---- desired range ---- |
1338 * | state | or | state |
1339 *
1340 * There's a hole, we need to insert something in it and
1341 * ignore the extent we found.
1342 */
1343 if (state->start > start) {
1344 u64 this_end;
1345 if (end < last_start)
1346 this_end = end;
1347 else
1348 this_end = last_start - 1;
1349
1350 prealloc = alloc_extent_state_atomic(prealloc);
1cf4ffdb
LB
1351 if (!prealloc) {
1352 err = -ENOMEM;
1353 goto out;
1354 }
462d6fac
JB
1355
1356 /*
1357 * Avoid to free 'prealloc' if it can be merged with
1358 * the later extent.
1359 */
1360 err = insert_state(tree, prealloc, start, this_end,
d38ed27f 1361 NULL, NULL, &bits, NULL);
c2d904e0
JM
1362 if (err)
1363 extent_io_tree_panic(tree, err);
e6138876 1364 cache_state(prealloc, cached_state);
462d6fac
JB
1365 prealloc = NULL;
1366 start = this_end + 1;
1367 goto search_again;
1368 }
1369 /*
1370 * | ---- desired range ---- |
1371 * | state |
1372 * We need to split the extent, and set the bit
1373 * on the first half
1374 */
1375 if (state->start <= end && state->end > end) {
1376 prealloc = alloc_extent_state_atomic(prealloc);
1cf4ffdb
LB
1377 if (!prealloc) {
1378 err = -ENOMEM;
1379 goto out;
1380 }
462d6fac
JB
1381
1382 err = split_state(tree, state, prealloc, end + 1);
c2d904e0
JM
1383 if (err)
1384 extent_io_tree_panic(tree, err);
462d6fac 1385
d38ed27f 1386 set_state_bits(tree, prealloc, &bits, NULL);
e6138876 1387 cache_state(prealloc, cached_state);
fefdc557 1388 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
462d6fac
JB
1389 prealloc = NULL;
1390 goto out;
1391 }
1392
462d6fac
JB
1393search_again:
1394 if (start > end)
1395 goto out;
1396 spin_unlock(&tree->lock);
210aa277 1397 cond_resched();
c8fd3de7 1398 first_iteration = false;
462d6fac 1399 goto again;
462d6fac
JB
1400
1401out:
1402 spin_unlock(&tree->lock);
1403 if (prealloc)
1404 free_extent_state(prealloc);
1405
1406 return err;
462d6fac
JB
1407}
1408
d1310b2e 1409/* wrappers around set/clear extent bit */
d38ed27f 1410int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
f97e27e9 1411 u32 bits, struct extent_changeset *changeset)
d38ed27f
QW
1412{
1413 /*
1414 * We don't support EXTENT_LOCKED yet, as current changeset will
1415 * record any bits changed, so for EXTENT_LOCKED case, it will
1416 * either fail with -EEXIST or changeset will record the whole
1417 * range.
1418 */
1419 BUG_ON(bits & EXTENT_LOCKED);
1420
1cab5e72
NB
1421 return set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
1422 changeset);
d38ed27f
QW
1423}
1424
4ca73656 1425int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
f97e27e9 1426 u32 bits)
4ca73656 1427{
1cab5e72
NB
1428 return set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
1429 GFP_NOWAIT, NULL);
4ca73656
NB
1430}
1431
fefdc557 1432int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
f97e27e9 1433 u32 bits, int wake, int delete,
ae0f1625 1434 struct extent_state **cached)
fefdc557
QW
1435{
1436 return __clear_extent_bit(tree, start, end, bits, wake, delete,
ae0f1625 1437 cached, GFP_NOFS, NULL);
fefdc557
QW
1438}
1439
fefdc557 1440int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
f97e27e9 1441 u32 bits, struct extent_changeset *changeset)
fefdc557
QW
1442{
1443 /*
1444 * Don't support EXTENT_LOCKED case, same reason as
1445 * set_record_extent_bits().
1446 */
1447 BUG_ON(bits & EXTENT_LOCKED);
1448
f734c44a 1449 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
fefdc557
QW
1450 changeset);
1451}
1452
d352ac68
CM
1453/*
1454 * either insert or lock state struct between start and end use mask to tell
1455 * us if waiting is desired.
1456 */
1edbb734 1457int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
ff13db41 1458 struct extent_state **cached_state)
d1310b2e
CM
1459{
1460 int err;
1461 u64 failed_start;
9ee49a04 1462
d1310b2e 1463 while (1) {
1cab5e72
NB
1464 err = set_extent_bit(tree, start, end, EXTENT_LOCKED,
1465 EXTENT_LOCKED, &failed_start,
1466 cached_state, GFP_NOFS, NULL);
d0082371 1467 if (err == -EEXIST) {
d1310b2e
CM
1468 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1469 start = failed_start;
d0082371 1470 } else
d1310b2e 1471 break;
d1310b2e
CM
1472 WARN_ON(start > end);
1473 }
1474 return err;
1475}
d1310b2e 1476
d0082371 1477int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
25179201
JB
1478{
1479 int err;
1480 u64 failed_start;
1481
1cab5e72
NB
1482 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1483 &failed_start, NULL, GFP_NOFS, NULL);
6643558d
YZ
1484 if (err == -EEXIST) {
1485 if (failed_start > start)
1486 clear_extent_bit(tree, start, failed_start - 1,
ae0f1625 1487 EXTENT_LOCKED, 1, 0, NULL);
25179201 1488 return 0;
6643558d 1489 }
25179201
JB
1490 return 1;
1491}
25179201 1492
bd1fa4f0 1493void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
4adaa611 1494{
09cbfeaf
KS
1495 unsigned long index = start >> PAGE_SHIFT;
1496 unsigned long end_index = end >> PAGE_SHIFT;
4adaa611
CM
1497 struct page *page;
1498
1499 while (index <= end_index) {
1500 page = find_get_page(inode->i_mapping, index);
1501 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1502 clear_page_dirty_for_io(page);
09cbfeaf 1503 put_page(page);
4adaa611
CM
1504 index++;
1505 }
4adaa611
CM
1506}
1507
f6311572 1508void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
4adaa611 1509{
09cbfeaf
KS
1510 unsigned long index = start >> PAGE_SHIFT;
1511 unsigned long end_index = end >> PAGE_SHIFT;
4adaa611
CM
1512 struct page *page;
1513
1514 while (index <= end_index) {
1515 page = find_get_page(inode->i_mapping, index);
1516 BUG_ON(!page); /* Pages should be in the extent_io_tree */
4adaa611 1517 __set_page_dirty_nobuffers(page);
8d38633c 1518 account_page_redirty(page);
09cbfeaf 1519 put_page(page);
4adaa611
CM
1520 index++;
1521 }
4adaa611
CM
1522}
1523
d352ac68
CM
1524/* find the first state struct with 'bits' set after 'start', and
1525 * return it. tree->lock must be held. NULL will returned if
1526 * nothing was found after 'start'
1527 */
48a3b636 1528static struct extent_state *
f97e27e9 1529find_first_extent_bit_state(struct extent_io_tree *tree, u64 start, u32 bits)
d7fc640e
CM
1530{
1531 struct rb_node *node;
1532 struct extent_state *state;
1533
1534 /*
1535 * this search will find all the extents that end after
1536 * our range starts.
1537 */
1538 node = tree_search(tree, start);
d397712b 1539 if (!node)
d7fc640e 1540 goto out;
d7fc640e 1541
d397712b 1542 while (1) {
d7fc640e 1543 state = rb_entry(node, struct extent_state, rb_node);
d397712b 1544 if (state->end >= start && (state->state & bits))
d7fc640e 1545 return state;
d397712b 1546
d7fc640e
CM
1547 node = rb_next(node);
1548 if (!node)
1549 break;
1550 }
1551out:
1552 return NULL;
1553}
d7fc640e 1554
69261c4b 1555/*
03509b78 1556 * Find the first offset in the io tree with one or more @bits set.
69261c4b 1557 *
03509b78
QW
1558 * Note: If there are multiple bits set in @bits, any of them will match.
1559 *
1560 * Return 0 if we find something, and update @start_ret and @end_ret.
1561 * Return 1 if we found nothing.
69261c4b
XG
1562 */
1563int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
f97e27e9 1564 u64 *start_ret, u64 *end_ret, u32 bits,
e6138876 1565 struct extent_state **cached_state)
69261c4b
XG
1566{
1567 struct extent_state *state;
1568 int ret = 1;
1569
1570 spin_lock(&tree->lock);
e6138876
JB
1571 if (cached_state && *cached_state) {
1572 state = *cached_state;
27a3507d 1573 if (state->end == start - 1 && extent_state_in_tree(state)) {
9688e9a9 1574 while ((state = next_state(state)) != NULL) {
e6138876
JB
1575 if (state->state & bits)
1576 goto got_it;
e6138876
JB
1577 }
1578 free_extent_state(*cached_state);
1579 *cached_state = NULL;
1580 goto out;
1581 }
1582 free_extent_state(*cached_state);
1583 *cached_state = NULL;
1584 }
1585
69261c4b 1586 state = find_first_extent_bit_state(tree, start, bits);
e6138876 1587got_it:
69261c4b 1588 if (state) {
e38e2ed7 1589 cache_state_if_flags(state, cached_state, 0);
69261c4b
XG
1590 *start_ret = state->start;
1591 *end_ret = state->end;
1592 ret = 0;
1593 }
e6138876 1594out:
69261c4b
XG
1595 spin_unlock(&tree->lock);
1596 return ret;
1597}
1598
41a2ee75 1599/**
3bed2da1
NB
1600 * Find a contiguous area of bits
1601 *
1602 * @tree: io tree to check
1603 * @start: offset to start the search from
1604 * @start_ret: the first offset we found with the bits set
1605 * @end_ret: the final contiguous range of the bits that were set
1606 * @bits: bits to look for
41a2ee75
JB
1607 *
1608 * set_extent_bit and clear_extent_bit can temporarily split contiguous ranges
1609 * to set bits appropriately, and then merge them again. During this time it
1610 * will drop the tree->lock, so use this helper if you want to find the actual
1611 * contiguous area for given bits. We will search to the first bit we find, and
1612 * then walk down the tree until we find a non-contiguous area. The area
1613 * returned will be the full contiguous area with the bits set.
1614 */
1615int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
f97e27e9 1616 u64 *start_ret, u64 *end_ret, u32 bits)
41a2ee75
JB
1617{
1618 struct extent_state *state;
1619 int ret = 1;
1620
1621 spin_lock(&tree->lock);
1622 state = find_first_extent_bit_state(tree, start, bits);
1623 if (state) {
1624 *start_ret = state->start;
1625 *end_ret = state->end;
1626 while ((state = next_state(state)) != NULL) {
1627 if (state->start > (*end_ret + 1))
1628 break;
1629 *end_ret = state->end;
1630 }
1631 ret = 0;
1632 }
1633 spin_unlock(&tree->lock);
1634 return ret;
1635}
1636
45bfcfc1 1637/**
3bed2da1
NB
1638 * Find the first range that has @bits not set. This range could start before
1639 * @start.
45bfcfc1 1640 *
3bed2da1
NB
1641 * @tree: the tree to search
1642 * @start: offset at/after which the found extent should start
1643 * @start_ret: records the beginning of the range
1644 * @end_ret: records the end of the range (inclusive)
1645 * @bits: the set of bits which must be unset
45bfcfc1
NB
1646 *
1647 * Since unallocated range is also considered one which doesn't have the bits
1648 * set it's possible that @end_ret contains -1, this happens in case the range
1649 * spans (last_range_end, end of device]. In this case it's up to the caller to
1650 * trim @end_ret to the appropriate size.
1651 */
1652void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
f97e27e9 1653 u64 *start_ret, u64 *end_ret, u32 bits)
45bfcfc1
NB
1654{
1655 struct extent_state *state;
1656 struct rb_node *node, *prev = NULL, *next;
1657
1658 spin_lock(&tree->lock);
1659
1660 /* Find first extent with bits cleared */
1661 while (1) {
1662 node = __etree_search(tree, start, &next, &prev, NULL, NULL);
5750c375
NB
1663 if (!node && !next && !prev) {
1664 /*
1665 * Tree is completely empty, send full range and let
1666 * caller deal with it
1667 */
1668 *start_ret = 0;
1669 *end_ret = -1;
1670 goto out;
1671 } else if (!node && !next) {
1672 /*
1673 * We are past the last allocated chunk, set start at
1674 * the end of the last extent.
1675 */
1676 state = rb_entry(prev, struct extent_state, rb_node);
1677 *start_ret = state->end + 1;
1678 *end_ret = -1;
1679 goto out;
1680 } else if (!node) {
45bfcfc1 1681 node = next;
45bfcfc1 1682 }
1eaebb34
NB
1683 /*
1684 * At this point 'node' either contains 'start' or start is
1685 * before 'node'
1686 */
45bfcfc1 1687 state = rb_entry(node, struct extent_state, rb_node);
1eaebb34
NB
1688
1689 if (in_range(start, state->start, state->end - state->start + 1)) {
1690 if (state->state & bits) {
1691 /*
1692 * |--range with bits sets--|
1693 * |
1694 * start
1695 */
1696 start = state->end + 1;
1697 } else {
1698 /*
1699 * 'start' falls within a range that doesn't
1700 * have the bits set, so take its start as
1701 * the beginning of the desired range
1702 *
1703 * |--range with bits cleared----|
1704 * |
1705 * start
1706 */
1707 *start_ret = state->start;
1708 break;
1709 }
45bfcfc1 1710 } else {
1eaebb34
NB
1711 /*
1712 * |---prev range---|---hole/unset---|---node range---|
1713 * |
1714 * start
1715 *
1716 * or
1717 *
1718 * |---hole/unset--||--first node--|
1719 * 0 |
1720 * start
1721 */
1722 if (prev) {
1723 state = rb_entry(prev, struct extent_state,
1724 rb_node);
1725 *start_ret = state->end + 1;
1726 } else {
1727 *start_ret = 0;
1728 }
45bfcfc1
NB
1729 break;
1730 }
1731 }
1732
1733 /*
1734 * Find the longest stretch from start until an entry which has the
1735 * bits set
1736 */
1737 while (1) {
1738 state = rb_entry(node, struct extent_state, rb_node);
1739 if (state->end >= start && !(state->state & bits)) {
1740 *end_ret = state->end;
1741 } else {
1742 *end_ret = state->start - 1;
1743 break;
1744 }
1745
1746 node = rb_next(node);
1747 if (!node)
1748 break;
1749 }
1750out:
1751 spin_unlock(&tree->lock);
1752}
1753
d352ac68
CM
1754/*
1755 * find a contiguous range of bytes in the file marked as delalloc, not
1756 * more than 'max_bytes'. start and end are used to return the range,
1757 *
3522e903 1758 * true is returned if we find something, false if nothing was in the tree
d352ac68 1759 */
083e75e7
JB
1760bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
1761 u64 *end, u64 max_bytes,
1762 struct extent_state **cached_state)
d1310b2e
CM
1763{
1764 struct rb_node *node;
1765 struct extent_state *state;
1766 u64 cur_start = *start;
3522e903 1767 bool found = false;
d1310b2e
CM
1768 u64 total_bytes = 0;
1769
cad321ad 1770 spin_lock(&tree->lock);
c8b97818 1771
d1310b2e
CM
1772 /*
1773 * this search will find all the extents that end after
1774 * our range starts.
1775 */
80ea96b1 1776 node = tree_search(tree, cur_start);
2b114d1d 1777 if (!node) {
3522e903 1778 *end = (u64)-1;
d1310b2e
CM
1779 goto out;
1780 }
1781
d397712b 1782 while (1) {
d1310b2e 1783 state = rb_entry(node, struct extent_state, rb_node);
5b21f2ed
ZY
1784 if (found && (state->start != cur_start ||
1785 (state->state & EXTENT_BOUNDARY))) {
d1310b2e
CM
1786 goto out;
1787 }
1788 if (!(state->state & EXTENT_DELALLOC)) {
1789 if (!found)
1790 *end = state->end;
1791 goto out;
1792 }
c2a128d2 1793 if (!found) {
d1310b2e 1794 *start = state->start;
c2a128d2 1795 *cached_state = state;
b7ac31b7 1796 refcount_inc(&state->refs);
c2a128d2 1797 }
3522e903 1798 found = true;
d1310b2e
CM
1799 *end = state->end;
1800 cur_start = state->end + 1;
1801 node = rb_next(node);
d1310b2e 1802 total_bytes += state->end - state->start + 1;
7bf811a5 1803 if (total_bytes >= max_bytes)
573aecaf 1804 break;
573aecaf 1805 if (!node)
d1310b2e
CM
1806 break;
1807 }
1808out:
cad321ad 1809 spin_unlock(&tree->lock);
d1310b2e
CM
1810 return found;
1811}
1812
ed8f13bf
QW
1813/*
1814 * Process one page for __process_pages_contig().
1815 *
1816 * Return >0 if we hit @page == @locked_page.
1817 * Return 0 if we updated the page status.
1818 * Return -EGAIN if the we need to try again.
1819 * (For PAGE_LOCK case but got dirty page or page not belong to mapping)
1820 */
e38992be
QW
1821static int process_one_page(struct btrfs_fs_info *fs_info,
1822 struct address_space *mapping,
ed8f13bf 1823 struct page *page, struct page *locked_page,
e38992be 1824 unsigned long page_ops, u64 start, u64 end)
ed8f13bf 1825{
e38992be
QW
1826 u32 len;
1827
1828 ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX);
1829 len = end + 1 - start;
1830
ed8f13bf 1831 if (page_ops & PAGE_SET_ORDERED)
b945a463 1832 btrfs_page_clamp_set_ordered(fs_info, page, start, len);
ed8f13bf 1833 if (page_ops & PAGE_SET_ERROR)
e38992be 1834 btrfs_page_clamp_set_error(fs_info, page, start, len);
ed8f13bf 1835 if (page_ops & PAGE_START_WRITEBACK) {
e38992be
QW
1836 btrfs_page_clamp_clear_dirty(fs_info, page, start, len);
1837 btrfs_page_clamp_set_writeback(fs_info, page, start, len);
ed8f13bf
QW
1838 }
1839 if (page_ops & PAGE_END_WRITEBACK)
e38992be 1840 btrfs_page_clamp_clear_writeback(fs_info, page, start, len);
a33a8e9a
QW
1841
1842 if (page == locked_page)
1843 return 1;
1844
ed8f13bf 1845 if (page_ops & PAGE_LOCK) {
1e1de387
QW
1846 int ret;
1847
1848 ret = btrfs_page_start_writer_lock(fs_info, page, start, len);
1849 if (ret)
1850 return ret;
ed8f13bf 1851 if (!PageDirty(page) || page->mapping != mapping) {
1e1de387 1852 btrfs_page_end_writer_lock(fs_info, page, start, len);
ed8f13bf
QW
1853 return -EAGAIN;
1854 }
1855 }
1856 if (page_ops & PAGE_UNLOCK)
1e1de387 1857 btrfs_page_end_writer_lock(fs_info, page, start, len);
ed8f13bf
QW
1858 return 0;
1859}
1860
da2c7009
LB
1861static int __process_pages_contig(struct address_space *mapping,
1862 struct page *locked_page,
98af9ab1 1863 u64 start, u64 end, unsigned long page_ops,
ed8f13bf
QW
1864 u64 *processed_end)
1865{
e38992be 1866 struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb);
ed8f13bf
QW
1867 pgoff_t start_index = start >> PAGE_SHIFT;
1868 pgoff_t end_index = end >> PAGE_SHIFT;
1869 pgoff_t index = start_index;
1870 unsigned long nr_pages = end_index - start_index + 1;
1871 unsigned long pages_processed = 0;
1872 struct page *pages[16];
1873 int err = 0;
1874 int i;
1875
1876 if (page_ops & PAGE_LOCK) {
1877 ASSERT(page_ops == PAGE_LOCK);
1878 ASSERT(processed_end && *processed_end == start);
1879 }
1880
1881 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
1882 mapping_set_error(mapping, -EIO);
1883
1884 while (nr_pages > 0) {
1885 int found_pages;
1886
1887 found_pages = find_get_pages_contig(mapping, index,
1888 min_t(unsigned long,
1889 nr_pages, ARRAY_SIZE(pages)), pages);
1890 if (found_pages == 0) {
1891 /*
1892 * Only if we're going to lock these pages, we can find
1893 * nothing at @index.
1894 */
1895 ASSERT(page_ops & PAGE_LOCK);
1896 err = -EAGAIN;
1897 goto out;
1898 }
1899
1900 for (i = 0; i < found_pages; i++) {
1901 int process_ret;
1902
e38992be
QW
1903 process_ret = process_one_page(fs_info, mapping,
1904 pages[i], locked_page, page_ops,
1905 start, end);
ed8f13bf
QW
1906 if (process_ret < 0) {
1907 for (; i < found_pages; i++)
1908 put_page(pages[i]);
1909 err = -EAGAIN;
1910 goto out;
1911 }
1912 put_page(pages[i]);
1913 pages_processed++;
1914 }
1915 nr_pages -= found_pages;
1916 index += found_pages;
1917 cond_resched();
1918 }
1919out:
1920 if (err && processed_end) {
1921 /*
1922 * Update @processed_end. I know this is awful since it has
1923 * two different return value patterns (inclusive vs exclusive).
1924 *
1925 * But the exclusive pattern is necessary if @start is 0, or we
1926 * underflow and check against processed_end won't work as
1927 * expected.
1928 */
1929 if (pages_processed)
1930 *processed_end = min(end,
1931 ((u64)(start_index + pages_processed) << PAGE_SHIFT) - 1);
1932 else
1933 *processed_end = start;
1934 }
1935 return err;
1936}
da2c7009 1937
143bede5
JM
1938static noinline void __unlock_for_delalloc(struct inode *inode,
1939 struct page *locked_page,
1940 u64 start, u64 end)
c8b97818 1941{
09cbfeaf
KS
1942 unsigned long index = start >> PAGE_SHIFT;
1943 unsigned long end_index = end >> PAGE_SHIFT;
c8b97818 1944
76c0021d 1945 ASSERT(locked_page);
c8b97818 1946 if (index == locked_page->index && end_index == index)
143bede5 1947 return;
c8b97818 1948
98af9ab1 1949 __process_pages_contig(inode->i_mapping, locked_page, start, end,
76c0021d 1950 PAGE_UNLOCK, NULL);
c8b97818
CM
1951}
1952
1953static noinline int lock_delalloc_pages(struct inode *inode,
1954 struct page *locked_page,
1955 u64 delalloc_start,
1956 u64 delalloc_end)
1957{
09cbfeaf 1958 unsigned long index = delalloc_start >> PAGE_SHIFT;
09cbfeaf 1959 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
98af9ab1 1960 u64 processed_end = delalloc_start;
c8b97818 1961 int ret;
c8b97818 1962
76c0021d 1963 ASSERT(locked_page);
c8b97818
CM
1964 if (index == locked_page->index && index == end_index)
1965 return 0;
1966
98af9ab1
QW
1967 ret = __process_pages_contig(inode->i_mapping, locked_page, delalloc_start,
1968 delalloc_end, PAGE_LOCK, &processed_end);
1969 if (ret == -EAGAIN && processed_end > delalloc_start)
76c0021d 1970 __unlock_for_delalloc(inode, locked_page, delalloc_start,
98af9ab1 1971 processed_end);
c8b97818
CM
1972 return ret;
1973}
1974
1975/*
3522e903 1976 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
2749f7ef 1977 * more than @max_bytes.
c8b97818 1978 *
2749f7ef
QW
1979 * @start: The original start bytenr to search.
1980 * Will store the extent range start bytenr.
1981 * @end: The original end bytenr of the search range
1982 * Will store the extent range end bytenr.
1983 *
1984 * Return true if we find a delalloc range which starts inside the original
1985 * range, and @start/@end will store the delalloc range start/end.
1986 *
1987 * Return false if we can't find any delalloc range which starts inside the
1988 * original range, and @start/@end will be the non-delalloc range start/end.
c8b97818 1989 */
ce9f967f 1990EXPORT_FOR_TESTS
3522e903 1991noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
294e30fe 1992 struct page *locked_page, u64 *start,
917aacec 1993 u64 *end)
c8b97818 1994{
9978059b 1995 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2749f7ef
QW
1996 const u64 orig_start = *start;
1997 const u64 orig_end = *end;
917aacec 1998 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
c8b97818
CM
1999 u64 delalloc_start;
2000 u64 delalloc_end;
3522e903 2001 bool found;
9655d298 2002 struct extent_state *cached_state = NULL;
c8b97818
CM
2003 int ret;
2004 int loops = 0;
2005
2749f7ef
QW
2006 /* Caller should pass a valid @end to indicate the search range end */
2007 ASSERT(orig_end > orig_start);
2008
2009 /* The range should at least cover part of the page */
2010 ASSERT(!(orig_start >= page_offset(locked_page) + PAGE_SIZE ||
2011 orig_end <= page_offset(locked_page)));
c8b97818
CM
2012again:
2013 /* step one, find a bunch of delalloc bytes starting at start */
2014 delalloc_start = *start;
2015 delalloc_end = 0;
083e75e7
JB
2016 found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end,
2017 max_bytes, &cached_state);
2749f7ef 2018 if (!found || delalloc_end <= *start || delalloc_start > orig_end) {
c8b97818 2019 *start = delalloc_start;
2749f7ef
QW
2020
2021 /* @delalloc_end can be -1, never go beyond @orig_end */
2022 *end = min(delalloc_end, orig_end);
c2a128d2 2023 free_extent_state(cached_state);
3522e903 2024 return false;
c8b97818
CM
2025 }
2026
70b99e69
CM
2027 /*
2028 * start comes from the offset of locked_page. We have to lock
2029 * pages in order, so we can't process delalloc bytes before
2030 * locked_page
2031 */
d397712b 2032 if (delalloc_start < *start)
70b99e69 2033 delalloc_start = *start;
70b99e69 2034
c8b97818
CM
2035 /*
2036 * make sure to limit the number of pages we try to lock down
c8b97818 2037 */
7bf811a5
JB
2038 if (delalloc_end + 1 - delalloc_start > max_bytes)
2039 delalloc_end = delalloc_start + max_bytes - 1;
d397712b 2040
c8b97818
CM
2041 /* step two, lock all the pages after the page that has start */
2042 ret = lock_delalloc_pages(inode, locked_page,
2043 delalloc_start, delalloc_end);
9bfd61d9 2044 ASSERT(!ret || ret == -EAGAIN);
c8b97818
CM
2045 if (ret == -EAGAIN) {
2046 /* some of the pages are gone, lets avoid looping by
2047 * shortening the size of the delalloc range we're searching
2048 */
9655d298 2049 free_extent_state(cached_state);
7d788742 2050 cached_state = NULL;
c8b97818 2051 if (!loops) {
09cbfeaf 2052 max_bytes = PAGE_SIZE;
c8b97818
CM
2053 loops = 1;
2054 goto again;
2055 } else {
3522e903 2056 found = false;
c8b97818
CM
2057 goto out_failed;
2058 }
2059 }
c8b97818
CM
2060
2061 /* step three, lock the state bits for the whole range */
ff13db41 2062 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
c8b97818
CM
2063
2064 /* then test to make sure it is all still delalloc */
2065 ret = test_range_bit(tree, delalloc_start, delalloc_end,
9655d298 2066 EXTENT_DELALLOC, 1, cached_state);
c8b97818 2067 if (!ret) {
9655d298 2068 unlock_extent_cached(tree, delalloc_start, delalloc_end,
e43bbe5e 2069 &cached_state);
c8b97818
CM
2070 __unlock_for_delalloc(inode, locked_page,
2071 delalloc_start, delalloc_end);
2072 cond_resched();
2073 goto again;
2074 }
9655d298 2075 free_extent_state(cached_state);
c8b97818
CM
2076 *start = delalloc_start;
2077 *end = delalloc_end;
2078out_failed:
2079 return found;
2080}
2081
ad7ff17b 2082void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
74e9194a 2083 struct page *locked_page,
f97e27e9 2084 u32 clear_bits, unsigned long page_ops)
873695b3 2085{
ad7ff17b 2086 clear_extent_bit(&inode->io_tree, start, end, clear_bits, 1, 0, NULL);
873695b3 2087
ad7ff17b 2088 __process_pages_contig(inode->vfs_inode.i_mapping, locked_page,
98af9ab1 2089 start, end, page_ops, NULL);
873695b3
LB
2090}
2091
d352ac68
CM
2092/*
2093 * count the number of bytes in the tree that have a given bit(s)
2094 * set. This can be fairly slow, except for EXTENT_DIRTY which is
2095 * cached. The total number found is returned.
2096 */
d1310b2e
CM
2097u64 count_range_bits(struct extent_io_tree *tree,
2098 u64 *start, u64 search_end, u64 max_bytes,
f97e27e9 2099 u32 bits, int contig)
d1310b2e
CM
2100{
2101 struct rb_node *node;
2102 struct extent_state *state;
2103 u64 cur_start = *start;
2104 u64 total_bytes = 0;
ec29ed5b 2105 u64 last = 0;
d1310b2e
CM
2106 int found = 0;
2107
fae7f21c 2108 if (WARN_ON(search_end <= cur_start))
d1310b2e 2109 return 0;
d1310b2e 2110
cad321ad 2111 spin_lock(&tree->lock);
d1310b2e
CM
2112 if (cur_start == 0 && bits == EXTENT_DIRTY) {
2113 total_bytes = tree->dirty_bytes;
2114 goto out;
2115 }
2116 /*
2117 * this search will find all the extents that end after
2118 * our range starts.
2119 */
80ea96b1 2120 node = tree_search(tree, cur_start);
d397712b 2121 if (!node)
d1310b2e 2122 goto out;
d1310b2e 2123
d397712b 2124 while (1) {
d1310b2e
CM
2125 state = rb_entry(node, struct extent_state, rb_node);
2126 if (state->start > search_end)
2127 break;
ec29ed5b
CM
2128 if (contig && found && state->start > last + 1)
2129 break;
2130 if (state->end >= cur_start && (state->state & bits) == bits) {
d1310b2e
CM
2131 total_bytes += min(search_end, state->end) + 1 -
2132 max(cur_start, state->start);
2133 if (total_bytes >= max_bytes)
2134 break;
2135 if (!found) {
af60bed2 2136 *start = max(cur_start, state->start);
d1310b2e
CM
2137 found = 1;
2138 }
ec29ed5b
CM
2139 last = state->end;
2140 } else if (contig && found) {
2141 break;
d1310b2e
CM
2142 }
2143 node = rb_next(node);
2144 if (!node)
2145 break;
2146 }
2147out:
cad321ad 2148 spin_unlock(&tree->lock);
d1310b2e
CM
2149 return total_bytes;
2150}
b2950863 2151
d352ac68
CM
2152/*
2153 * set the private field for a given byte offset in the tree. If there isn't
2154 * an extent_state there already, this does nothing.
2155 */
b3f167aa
JB
2156int set_state_failrec(struct extent_io_tree *tree, u64 start,
2157 struct io_failure_record *failrec)
d1310b2e
CM
2158{
2159 struct rb_node *node;
2160 struct extent_state *state;
2161 int ret = 0;
2162
cad321ad 2163 spin_lock(&tree->lock);
d1310b2e
CM
2164 /*
2165 * this search will find all the extents that end after
2166 * our range starts.
2167 */
80ea96b1 2168 node = tree_search(tree, start);
2b114d1d 2169 if (!node) {
d1310b2e
CM
2170 ret = -ENOENT;
2171 goto out;
2172 }
2173 state = rb_entry(node, struct extent_state, rb_node);
2174 if (state->start != start) {
2175 ret = -ENOENT;
2176 goto out;
2177 }
47dc196a 2178 state->failrec = failrec;
d1310b2e 2179out:
cad321ad 2180 spin_unlock(&tree->lock);
d1310b2e
CM
2181 return ret;
2182}
2183
2279a270 2184struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start)
d1310b2e
CM
2185{
2186 struct rb_node *node;
2187 struct extent_state *state;
2279a270 2188 struct io_failure_record *failrec;
d1310b2e 2189
cad321ad 2190 spin_lock(&tree->lock);
d1310b2e
CM
2191 /*
2192 * this search will find all the extents that end after
2193 * our range starts.
2194 */
80ea96b1 2195 node = tree_search(tree, start);
2b114d1d 2196 if (!node) {
2279a270 2197 failrec = ERR_PTR(-ENOENT);
d1310b2e
CM
2198 goto out;
2199 }
2200 state = rb_entry(node, struct extent_state, rb_node);
2201 if (state->start != start) {
2279a270 2202 failrec = ERR_PTR(-ENOENT);
d1310b2e
CM
2203 goto out;
2204 }
2279a270
NB
2205
2206 failrec = state->failrec;
d1310b2e 2207out:
cad321ad 2208 spin_unlock(&tree->lock);
2279a270 2209 return failrec;
d1310b2e
CM
2210}
2211
2212/*
2213 * searches a range in the state tree for a given mask.
70dec807 2214 * If 'filled' == 1, this returns 1 only if every extent in the tree
d1310b2e
CM
2215 * has the bits set. Otherwise, 1 is returned if any bit in the
2216 * range is found set.
2217 */
2218int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
f97e27e9 2219 u32 bits, int filled, struct extent_state *cached)
d1310b2e
CM
2220{
2221 struct extent_state *state = NULL;
2222 struct rb_node *node;
2223 int bitset = 0;
d1310b2e 2224
cad321ad 2225 spin_lock(&tree->lock);
27a3507d 2226 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
df98b6e2 2227 cached->end > start)
9655d298
CM
2228 node = &cached->rb_node;
2229 else
2230 node = tree_search(tree, start);
d1310b2e
CM
2231 while (node && start <= end) {
2232 state = rb_entry(node, struct extent_state, rb_node);
2233
2234 if (filled && state->start > start) {
2235 bitset = 0;
2236 break;
2237 }
2238
2239 if (state->start > end)
2240 break;
2241
2242 if (state->state & bits) {
2243 bitset = 1;
2244 if (!filled)
2245 break;
2246 } else if (filled) {
2247 bitset = 0;
2248 break;
2249 }
46562cec
CM
2250
2251 if (state->end == (u64)-1)
2252 break;
2253
d1310b2e
CM
2254 start = state->end + 1;
2255 if (start > end)
2256 break;
2257 node = rb_next(node);
2258 if (!node) {
2259 if (filled)
2260 bitset = 0;
2261 break;
2262 }
2263 }
cad321ad 2264 spin_unlock(&tree->lock);
d1310b2e
CM
2265 return bitset;
2266}
d1310b2e 2267
7870d082
JB
2268int free_io_failure(struct extent_io_tree *failure_tree,
2269 struct extent_io_tree *io_tree,
2270 struct io_failure_record *rec)
4a54c8c1
JS
2271{
2272 int ret;
2273 int err = 0;
4a54c8c1 2274
47dc196a 2275 set_state_failrec(failure_tree, rec->start, NULL);
4a54c8c1
JS
2276 ret = clear_extent_bits(failure_tree, rec->start,
2277 rec->start + rec->len - 1,
91166212 2278 EXTENT_LOCKED | EXTENT_DIRTY);
4a54c8c1
JS
2279 if (ret)
2280 err = ret;
2281
7870d082 2282 ret = clear_extent_bits(io_tree, rec->start,
53b381b3 2283 rec->start + rec->len - 1,
91166212 2284 EXTENT_DAMAGED);
53b381b3
DW
2285 if (ret && !err)
2286 err = ret;
4a54c8c1
JS
2287
2288 kfree(rec);
2289 return err;
2290}
2291
4a54c8c1
JS
2292/*
2293 * this bypasses the standard btrfs submit functions deliberately, as
2294 * the standard behavior is to write all copies in a raid setup. here we only
2295 * want to write the one bad copy. so we do the mapping for ourselves and issue
2296 * submit_bio directly.
3ec706c8 2297 * to avoid any synchronization issues, wait for the data after writing, which
4a54c8c1
JS
2298 * actually prevents the read that triggered the error from finishing.
2299 * currently, there can be no more than two copies of every data bit. thus,
2300 * exactly one rewrite is required.
2301 */
38d5e541
QW
2302static int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2303 u64 length, u64 logical, struct page *page,
2304 unsigned int pg_offset, int mirror_num)
4a54c8c1
JS
2305{
2306 struct bio *bio;
2307 struct btrfs_device *dev;
4a54c8c1
JS
2308 u64 map_length = 0;
2309 u64 sector;
4c664611 2310 struct btrfs_io_context *bioc = NULL;
4a54c8c1
JS
2311 int ret;
2312
1751e8a6 2313 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
4a54c8c1
JS
2314 BUG_ON(!mirror_num);
2315
554aed7d
JT
2316 if (btrfs_repair_one_zone(fs_info, logical))
2317 return 0;
f7ef5287 2318
c3a3b19b 2319 bio = btrfs_bio_alloc(1);
4f024f37 2320 bio->bi_iter.bi_size = 0;
4a54c8c1
JS
2321 map_length = length;
2322
b5de8d0d 2323 /*
4c664611 2324 * Avoid races with device replace and make sure our bioc has devices
b5de8d0d
FM
2325 * associated to its stripes that don't go away while we are doing the
2326 * read repair operation.
2327 */
2328 btrfs_bio_counter_inc_blocked(fs_info);
e4ff5fb5 2329 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
c725328c
LB
2330 /*
2331 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2332 * to update all raid stripes, but here we just want to correct
2333 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2334 * stripe's dev and sector.
2335 */
2336 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
4c664611 2337 &map_length, &bioc, 0);
c725328c
LB
2338 if (ret) {
2339 btrfs_bio_counter_dec(fs_info);
2340 bio_put(bio);
2341 return -EIO;
2342 }
4c664611 2343 ASSERT(bioc->mirror_num == 1);
c725328c
LB
2344 } else {
2345 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
4c664611 2346 &map_length, &bioc, mirror_num);
c725328c
LB
2347 if (ret) {
2348 btrfs_bio_counter_dec(fs_info);
2349 bio_put(bio);
2350 return -EIO;
2351 }
4c664611 2352 BUG_ON(mirror_num != bioc->mirror_num);
4a54c8c1 2353 }
c725328c 2354
4c664611 2355 sector = bioc->stripes[bioc->mirror_num - 1].physical >> 9;
4f024f37 2356 bio->bi_iter.bi_sector = sector;
4c664611
QW
2357 dev = bioc->stripes[bioc->mirror_num - 1].dev;
2358 btrfs_put_bioc(bioc);
ebbede42
AJ
2359 if (!dev || !dev->bdev ||
2360 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
b5de8d0d 2361 btrfs_bio_counter_dec(fs_info);
4a54c8c1
JS
2362 bio_put(bio);
2363 return -EIO;
2364 }
74d46992 2365 bio_set_dev(bio, dev->bdev);
70fd7614 2366 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
ffdd2018 2367 bio_add_page(bio, page, length, pg_offset);
4a54c8c1 2368
4e49ea4a 2369 if (btrfsic_submit_bio_wait(bio)) {
4a54c8c1 2370 /* try to remap that extent elsewhere? */
b5de8d0d 2371 btrfs_bio_counter_dec(fs_info);
4a54c8c1 2372 bio_put(bio);
442a4f63 2373 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4a54c8c1
JS
2374 return -EIO;
2375 }
2376
b14af3b4
DS
2377 btrfs_info_rl_in_rcu(fs_info,
2378 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
6ec656bc 2379 ino, start,
1203b681 2380 rcu_str_deref(dev->name), sector);
b5de8d0d 2381 btrfs_bio_counter_dec(fs_info);
4a54c8c1
JS
2382 bio_put(bio);
2383 return 0;
2384}
2385
2b48966a 2386int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num)
ea466794 2387{
20a1fbf9 2388 struct btrfs_fs_info *fs_info = eb->fs_info;
ea466794 2389 u64 start = eb->start;
cc5e31a4 2390 int i, num_pages = num_extent_pages(eb);
d95603b2 2391 int ret = 0;
ea466794 2392
bc98a42c 2393 if (sb_rdonly(fs_info->sb))
908960c6
ID
2394 return -EROFS;
2395
ea466794 2396 for (i = 0; i < num_pages; i++) {
fb85fc9a 2397 struct page *p = eb->pages[i];
1203b681 2398
6ec656bc 2399 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
1203b681 2400 start - page_offset(p), mirror_num);
ea466794
JB
2401 if (ret)
2402 break;
09cbfeaf 2403 start += PAGE_SIZE;
ea466794
JB
2404 }
2405
2406 return ret;
2407}
2408
4a54c8c1
JS
2409/*
2410 * each time an IO finishes, we do a fast check in the IO failure tree
2411 * to see if we need to process or clean up an io_failure_record
2412 */
7870d082
JB
2413int clean_io_failure(struct btrfs_fs_info *fs_info,
2414 struct extent_io_tree *failure_tree,
2415 struct extent_io_tree *io_tree, u64 start,
2416 struct page *page, u64 ino, unsigned int pg_offset)
4a54c8c1
JS
2417{
2418 u64 private;
4a54c8c1 2419 struct io_failure_record *failrec;
4a54c8c1
JS
2420 struct extent_state *state;
2421 int num_copies;
4a54c8c1 2422 int ret;
4a54c8c1
JS
2423
2424 private = 0;
7870d082
JB
2425 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2426 EXTENT_DIRTY, 0);
4a54c8c1
JS
2427 if (!ret)
2428 return 0;
2429
2279a270
NB
2430 failrec = get_state_failrec(failure_tree, start);
2431 if (IS_ERR(failrec))
4a54c8c1
JS
2432 return 0;
2433
4a54c8c1
JS
2434 BUG_ON(!failrec->this_mirror);
2435
bc98a42c 2436 if (sb_rdonly(fs_info->sb))
908960c6 2437 goto out;
4a54c8c1 2438
7870d082
JB
2439 spin_lock(&io_tree->lock);
2440 state = find_first_extent_bit_state(io_tree,
4a54c8c1
JS
2441 failrec->start,
2442 EXTENT_LOCKED);
7870d082 2443 spin_unlock(&io_tree->lock);
4a54c8c1 2444
883d0de4
MX
2445 if (state && state->start <= failrec->start &&
2446 state->end >= failrec->start + failrec->len - 1) {
3ec706c8
SB
2447 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2448 failrec->len);
4a54c8c1 2449 if (num_copies > 1) {
7870d082
JB
2450 repair_io_failure(fs_info, ino, start, failrec->len,
2451 failrec->logical, page, pg_offset,
2452 failrec->failed_mirror);
4a54c8c1
JS
2453 }
2454 }
2455
2456out:
7870d082 2457 free_io_failure(failure_tree, io_tree, failrec);
4a54c8c1 2458
454ff3de 2459 return 0;
4a54c8c1
JS
2460}
2461
f612496b
MX
2462/*
2463 * Can be called when
2464 * - hold extent lock
2465 * - under ordered extent
2466 * - the inode is freeing
2467 */
7ab7956e 2468void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
f612496b 2469{
7ab7956e 2470 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
f612496b
MX
2471 struct io_failure_record *failrec;
2472 struct extent_state *state, *next;
2473
2474 if (RB_EMPTY_ROOT(&failure_tree->state))
2475 return;
2476
2477 spin_lock(&failure_tree->lock);
2478 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2479 while (state) {
2480 if (state->start > end)
2481 break;
2482
2483 ASSERT(state->end <= end);
2484
2485 next = next_state(state);
2486
47dc196a 2487 failrec = state->failrec;
f612496b
MX
2488 free_extent_state(state);
2489 kfree(failrec);
2490
2491 state = next;
2492 }
2493 spin_unlock(&failure_tree->lock);
2494}
2495
3526302f 2496static struct io_failure_record *btrfs_get_io_failure_record(struct inode *inode,
150e4b05 2497 u64 start)
4a54c8c1 2498{
ab8d0fc4 2499 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2fe6303e 2500 struct io_failure_record *failrec;
4a54c8c1 2501 struct extent_map *em;
4a54c8c1
JS
2502 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2503 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2504 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
150e4b05 2505 const u32 sectorsize = fs_info->sectorsize;
4a54c8c1 2506 int ret;
4a54c8c1
JS
2507 u64 logical;
2508
2279a270 2509 failrec = get_state_failrec(failure_tree, start);
3526302f 2510 if (!IS_ERR(failrec)) {
ab8d0fc4 2511 btrfs_debug(fs_info,
1245835d
QW
2512 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu",
2513 failrec->logical, failrec->start, failrec->len);
4a54c8c1
JS
2514 /*
2515 * when data can be on disk more than twice, add to failrec here
2516 * (e.g. with a list for failed_mirror) to make
2517 * clean_io_failure() clean all those errors at once.
2518 */
3526302f
NB
2519
2520 return failrec;
4a54c8c1 2521 }
2fe6303e 2522
3526302f
NB
2523 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2524 if (!failrec)
2525 return ERR_PTR(-ENOMEM);
2fe6303e 2526
3526302f 2527 failrec->start = start;
150e4b05 2528 failrec->len = sectorsize;
3526302f
NB
2529 failrec->this_mirror = 0;
2530 failrec->bio_flags = 0;
3526302f
NB
2531
2532 read_lock(&em_tree->lock);
2533 em = lookup_extent_mapping(em_tree, start, failrec->len);
2534 if (!em) {
2535 read_unlock(&em_tree->lock);
2536 kfree(failrec);
2537 return ERR_PTR(-EIO);
2538 }
2539
2540 if (em->start > start || em->start + em->len <= start) {
2541 free_extent_map(em);
2542 em = NULL;
2543 }
2544 read_unlock(&em_tree->lock);
2545 if (!em) {
2546 kfree(failrec);
2547 return ERR_PTR(-EIO);
2548 }
2549
2550 logical = start - em->start;
2551 logical = em->block_start + logical;
2552 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2553 logical = em->block_start;
2554 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2555 extent_set_compress_type(&failrec->bio_flags, em->compress_type);
2556 }
2557
2558 btrfs_debug(fs_info,
2559 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2560 logical, start, failrec->len);
2561
2562 failrec->logical = logical;
2563 free_extent_map(em);
2564
2565 /* Set the bits in the private failure tree */
150e4b05 2566 ret = set_extent_bits(failure_tree, start, start + sectorsize - 1,
3526302f
NB
2567 EXTENT_LOCKED | EXTENT_DIRTY);
2568 if (ret >= 0) {
2569 ret = set_state_failrec(failure_tree, start, failrec);
2570 /* Set the bits in the inode's tree */
150e4b05
QW
2571 ret = set_extent_bits(tree, start, start + sectorsize - 1,
2572 EXTENT_DAMAGED);
3526302f
NB
2573 } else if (ret < 0) {
2574 kfree(failrec);
2575 return ERR_PTR(ret);
2576 }
2577
2578 return failrec;
2fe6303e
MX
2579}
2580
1245835d 2581static bool btrfs_check_repairable(struct inode *inode,
ce06d3ec
OS
2582 struct io_failure_record *failrec,
2583 int failed_mirror)
2fe6303e 2584{
ab8d0fc4 2585 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2fe6303e
MX
2586 int num_copies;
2587
ab8d0fc4 2588 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
4a54c8c1
JS
2589 if (num_copies == 1) {
2590 /*
2591 * we only have a single copy of the data, so don't bother with
2592 * all the retry and error correction code that follows. no
2593 * matter what the error is, it is very likely to persist.
2594 */
ab8d0fc4
JM
2595 btrfs_debug(fs_info,
2596 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2597 num_copies, failrec->this_mirror, failed_mirror);
c3cfb656 2598 return false;
4a54c8c1
JS
2599 }
2600
1245835d
QW
2601 /* The failure record should only contain one sector */
2602 ASSERT(failrec->len == fs_info->sectorsize);
2603
4a54c8c1 2604 /*
1245835d
QW
2605 * There are two premises:
2606 * a) deliver good data to the caller
2607 * b) correct the bad sectors on disk
2608 *
2609 * Since we're only doing repair for one sector, we only need to get
2610 * a good copy of the failed sector and if we succeed, we have setup
2611 * everything for repair_io_failure to do the rest for us.
4a54c8c1 2612 */
510671d2 2613 ASSERT(failed_mirror);
1245835d
QW
2614 failrec->failed_mirror = failed_mirror;
2615 failrec->this_mirror++;
2616 if (failrec->this_mirror == failed_mirror)
4a54c8c1 2617 failrec->this_mirror++;
4a54c8c1 2618
facc8a22 2619 if (failrec->this_mirror > num_copies) {
ab8d0fc4
JM
2620 btrfs_debug(fs_info,
2621 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2622 num_copies, failrec->this_mirror, failed_mirror);
c3cfb656 2623 return false;
4a54c8c1
JS
2624 }
2625
c3cfb656 2626 return true;
2fe6303e
MX
2627}
2628
150e4b05
QW
2629int btrfs_repair_one_sector(struct inode *inode,
2630 struct bio *failed_bio, u32 bio_offset,
2631 struct page *page, unsigned int pgoff,
2632 u64 start, int failed_mirror,
2633 submit_bio_hook_t *submit_bio_hook)
2fe6303e
MX
2634{
2635 struct io_failure_record *failrec;
77d5d689 2636 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2fe6303e 2637 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
7870d082 2638 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
c3a3b19b 2639 struct btrfs_bio *failed_bbio = btrfs_bio(failed_bio);
7ffd27e3 2640 const int icsum = bio_offset >> fs_info->sectorsize_bits;
77d5d689 2641 struct bio *repair_bio;
c3a3b19b 2642 struct btrfs_bio *repair_bbio;
2fe6303e 2643
77d5d689
OS
2644 btrfs_debug(fs_info,
2645 "repair read error: read error at %llu", start);
2fe6303e 2646
1f7ad75b 2647 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
2fe6303e 2648
150e4b05 2649 failrec = btrfs_get_io_failure_record(inode, start);
3526302f 2650 if (IS_ERR(failrec))
150e4b05 2651 return PTR_ERR(failrec);
2fe6303e 2652
1245835d
QW
2653
2654 if (!btrfs_check_repairable(inode, failrec, failed_mirror)) {
7870d082 2655 free_io_failure(failure_tree, tree, failrec);
150e4b05 2656 return -EIO;
2fe6303e
MX
2657 }
2658
c3a3b19b
QW
2659 repair_bio = btrfs_bio_alloc(1);
2660 repair_bbio = btrfs_bio(repair_bio);
77d5d689 2661 repair_bio->bi_opf = REQ_OP_READ;
77d5d689
OS
2662 repair_bio->bi_end_io = failed_bio->bi_end_io;
2663 repair_bio->bi_iter.bi_sector = failrec->logical >> 9;
2664 repair_bio->bi_private = failed_bio->bi_private;
2fe6303e 2665
c3a3b19b 2666 if (failed_bbio->csum) {
223486c2 2667 const u32 csum_size = fs_info->csum_size;
77d5d689 2668
c3a3b19b
QW
2669 repair_bbio->csum = repair_bbio->csum_inline;
2670 memcpy(repair_bbio->csum,
2671 failed_bbio->csum + csum_size * icsum, csum_size);
77d5d689 2672 }
2fe6303e 2673
77d5d689 2674 bio_add_page(repair_bio, page, failrec->len, pgoff);
c3a3b19b 2675 repair_bbio->iter = repair_bio->bi_iter;
4a54c8c1 2676
ab8d0fc4 2677 btrfs_debug(btrfs_sb(inode->i_sb),
1245835d
QW
2678 "repair read error: submitting new read to mirror %d",
2679 failrec->this_mirror);
4a54c8c1 2680
8cbc3001
JB
2681 /*
2682 * At this point we have a bio, so any errors from submit_bio_hook()
2683 * will be handled by the endio on the repair_bio, so we can't return an
2684 * error here.
2685 */
2686 submit_bio_hook(inode, repair_bio, failrec->this_mirror, failrec->bio_flags);
2687 return BLK_STS_OK;
150e4b05
QW
2688}
2689
2690static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len)
2691{
2692 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
2693
2694 ASSERT(page_offset(page) <= start &&
2695 start + len <= page_offset(page) + PAGE_SIZE);
2696
150e4b05 2697 if (uptodate) {
14605409
BB
2698 if (fsverity_active(page->mapping->host) &&
2699 !PageError(page) &&
2700 !PageUptodate(page) &&
2701 start < i_size_read(page->mapping->host) &&
2702 !fsverity_verify_page(page)) {
2703 btrfs_page_set_error(fs_info, page, start, len);
2704 } else {
2705 btrfs_page_set_uptodate(fs_info, page, start, len);
2706 }
150e4b05
QW
2707 } else {
2708 btrfs_page_clear_uptodate(fs_info, page, start, len);
2709 btrfs_page_set_error(fs_info, page, start, len);
2710 }
2711
2712 if (fs_info->sectorsize == PAGE_SIZE)
2713 unlock_page(page);
3d078efa 2714 else
150e4b05
QW
2715 btrfs_subpage_end_reader(fs_info, page, start, len);
2716}
2717
2718static blk_status_t submit_read_repair(struct inode *inode,
2719 struct bio *failed_bio, u32 bio_offset,
2720 struct page *page, unsigned int pgoff,
2721 u64 start, u64 end, int failed_mirror,
2722 unsigned int error_bitmap,
2723 submit_bio_hook_t *submit_bio_hook)
2724{
2725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2726 const u32 sectorsize = fs_info->sectorsize;
2727 const int nr_bits = (end + 1 - start) >> fs_info->sectorsize_bits;
2728 int error = 0;
2729 int i;
2730
2731 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
2732
2733 /* We're here because we had some read errors or csum mismatch */
2734 ASSERT(error_bitmap);
2735
2736 /*
2737 * We only get called on buffered IO, thus page must be mapped and bio
2738 * must not be cloned.
2739 */
2740 ASSERT(page->mapping && !bio_flagged(failed_bio, BIO_CLONED));
2741
2742 /* Iterate through all the sectors in the range */
2743 for (i = 0; i < nr_bits; i++) {
2744 const unsigned int offset = i * sectorsize;
2745 struct extent_state *cached = NULL;
2746 bool uptodate = false;
2747 int ret;
2748
2749 if (!(error_bitmap & (1U << i))) {
2750 /*
2751 * This sector has no error, just end the page read
2752 * and unlock the range.
2753 */
2754 uptodate = true;
2755 goto next;
2756 }
2757
2758 ret = btrfs_repair_one_sector(inode, failed_bio,
2759 bio_offset + offset,
2760 page, pgoff + offset, start + offset,
2761 failed_mirror, submit_bio_hook);
2762 if (!ret) {
2763 /*
2764 * We have submitted the read repair, the page release
2765 * will be handled by the endio function of the
2766 * submitted repair bio.
2767 * Thus we don't need to do any thing here.
2768 */
2769 continue;
2770 }
2771 /*
2772 * Repair failed, just record the error but still continue.
2773 * Or the remaining sectors will not be properly unlocked.
2774 */
2775 if (!error)
2776 error = ret;
2777next:
2778 end_page_read(page, uptodate, start + offset, sectorsize);
2779 if (uptodate)
2780 set_extent_uptodate(&BTRFS_I(inode)->io_tree,
2781 start + offset,
2782 start + offset + sectorsize - 1,
2783 &cached, GFP_ATOMIC);
2784 unlock_extent_cached_atomic(&BTRFS_I(inode)->io_tree,
2785 start + offset,
2786 start + offset + sectorsize - 1,
2787 &cached);
2788 }
2789 return errno_to_blk_status(error);
4a54c8c1
JS
2790}
2791
d1310b2e
CM
2792/* lots and lots of room for performance fixes in the end_bio funcs */
2793
b5227c07 2794void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
87826df0 2795{
38a39ac7 2796 struct btrfs_inode *inode;
25c1252a 2797 const bool uptodate = (err == 0);
3e2426bd 2798 int ret = 0;
87826df0 2799
38a39ac7
QW
2800 ASSERT(page && page->mapping);
2801 inode = BTRFS_I(page->mapping->host);
2802 btrfs_writepage_endio_finish_ordered(inode, page, start, end, uptodate);
87826df0 2803
87826df0 2804 if (!uptodate) {
963e4db8
QW
2805 const struct btrfs_fs_info *fs_info = inode->root->fs_info;
2806 u32 len;
2807
2808 ASSERT(end + 1 - start <= U32_MAX);
2809 len = end + 1 - start;
2810
2811 btrfs_page_clear_uptodate(fs_info, page, start, len);
2812 btrfs_page_set_error(fs_info, page, start, len);
bff5baf8 2813 ret = err < 0 ? err : -EIO;
5dca6eea 2814 mapping_set_error(page->mapping, ret);
87826df0 2815 }
87826df0
JM
2816}
2817
d1310b2e
CM
2818/*
2819 * after a writepage IO is done, we need to:
2820 * clear the uptodate bits on error
2821 * clear the writeback bits in the extent tree for this IO
2822 * end_page_writeback if the page has no more pending IO
2823 *
2824 * Scheduling is not allowed, so the extent state tree is expected
2825 * to have one and only one object corresponding to this IO.
2826 */
4246a0b6 2827static void end_bio_extent_writepage(struct bio *bio)
d1310b2e 2828{
4e4cbee9 2829 int error = blk_status_to_errno(bio->bi_status);
2c30c71b 2830 struct bio_vec *bvec;
d1310b2e
CM
2831 u64 start;
2832 u64 end;
6dc4f100 2833 struct bvec_iter_all iter_all;
d8e3fb10 2834 bool first_bvec = true;
d1310b2e 2835
c09abff8 2836 ASSERT(!bio_flagged(bio, BIO_CLONED));
2b070cfe 2837 bio_for_each_segment_all(bvec, bio, iter_all) {
d1310b2e 2838 struct page *page = bvec->bv_page;
0b246afa
JM
2839 struct inode *inode = page->mapping->host;
2840 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
321a02db
QW
2841 const u32 sectorsize = fs_info->sectorsize;
2842
2843 /* Our read/write should always be sector aligned. */
2844 if (!IS_ALIGNED(bvec->bv_offset, sectorsize))
2845 btrfs_err(fs_info,
2846 "partial page write in btrfs with offset %u and length %u",
2847 bvec->bv_offset, bvec->bv_len);
2848 else if (!IS_ALIGNED(bvec->bv_len, sectorsize))
2849 btrfs_info(fs_info,
2850 "incomplete page write with offset %u and length %u",
2851 bvec->bv_offset, bvec->bv_len);
2852
2853 start = page_offset(page) + bvec->bv_offset;
2854 end = start + bvec->bv_len - 1;
d1310b2e 2855
d8e3fb10
NA
2856 if (first_bvec) {
2857 btrfs_record_physical_zoned(inode, start, bio);
2858 first_bvec = false;
2859 }
2860
4e4cbee9 2861 end_extent_writepage(page, error, start, end);
9047e317
QW
2862
2863 btrfs_page_clear_writeback(fs_info, page, start, bvec->bv_len);
2c30c71b 2864 }
2b1f55b0 2865
d1310b2e 2866 bio_put(bio);
d1310b2e
CM
2867}
2868
94e8c95c
QW
2869/*
2870 * Record previously processed extent range
2871 *
2872 * For endio_readpage_release_extent() to handle a full extent range, reducing
2873 * the extent io operations.
2874 */
2875struct processed_extent {
2876 struct btrfs_inode *inode;
2877 /* Start of the range in @inode */
2878 u64 start;
2e626e56 2879 /* End of the range in @inode */
94e8c95c
QW
2880 u64 end;
2881 bool uptodate;
2882};
2883
2884/*
2885 * Try to release processed extent range
2886 *
2887 * May not release the extent range right now if the current range is
2888 * contiguous to processed extent.
2889 *
2890 * Will release processed extent when any of @inode, @uptodate, the range is
2891 * no longer contiguous to the processed range.
2892 *
2893 * Passing @inode == NULL will force processed extent to be released.
2894 */
2895static void endio_readpage_release_extent(struct processed_extent *processed,
2896 struct btrfs_inode *inode, u64 start, u64 end,
2897 bool uptodate)
883d0de4
MX
2898{
2899 struct extent_state *cached = NULL;
94e8c95c
QW
2900 struct extent_io_tree *tree;
2901
2902 /* The first extent, initialize @processed */
2903 if (!processed->inode)
2904 goto update;
883d0de4 2905
94e8c95c
QW
2906 /*
2907 * Contiguous to processed extent, just uptodate the end.
2908 *
2909 * Several things to notice:
2910 *
2911 * - bio can be merged as long as on-disk bytenr is contiguous
2912 * This means we can have page belonging to other inodes, thus need to
2913 * check if the inode still matches.
2914 * - bvec can contain range beyond current page for multi-page bvec
2915 * Thus we need to do processed->end + 1 >= start check
2916 */
2917 if (processed->inode == inode && processed->uptodate == uptodate &&
2918 processed->end + 1 >= start && end >= processed->end) {
2919 processed->end = end;
2920 return;
2921 }
2922
2923 tree = &processed->inode->io_tree;
2924 /*
2925 * Now we don't have range contiguous to the processed range, release
2926 * the processed range now.
2927 */
2928 if (processed->uptodate && tree->track_uptodate)
2929 set_extent_uptodate(tree, processed->start, processed->end,
2930 &cached, GFP_ATOMIC);
2931 unlock_extent_cached_atomic(tree, processed->start, processed->end,
2932 &cached);
2933
2934update:
2935 /* Update processed to current range */
2936 processed->inode = inode;
2937 processed->start = start;
2938 processed->end = end;
2939 processed->uptodate = uptodate;
883d0de4
MX
2940}
2941
92082d40
QW
2942static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page)
2943{
2944 ASSERT(PageLocked(page));
2945 if (fs_info->sectorsize == PAGE_SIZE)
2946 return;
2947
2948 ASSERT(PagePrivate(page));
2949 btrfs_subpage_start_reader(fs_info, page, page_offset(page), PAGE_SIZE);
2950}
2951
d9bb77d5
QW
2952/*
2953 * Find extent buffer for a givne bytenr.
2954 *
2955 * This is for end_bio_extent_readpage(), thus we can't do any unsafe locking
2956 * in endio context.
2957 */
2958static struct extent_buffer *find_extent_buffer_readpage(
2959 struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
2960{
2961 struct extent_buffer *eb;
2962
2963 /*
2964 * For regular sectorsize, we can use page->private to grab extent
2965 * buffer
2966 */
2967 if (fs_info->sectorsize == PAGE_SIZE) {
2968 ASSERT(PagePrivate(page) && page->private);
2969 return (struct extent_buffer *)page->private;
2970 }
2971
2972 /* For subpage case, we need to lookup buffer radix tree */
2973 rcu_read_lock();
2974 eb = radix_tree_lookup(&fs_info->buffer_radix,
2975 bytenr >> fs_info->sectorsize_bits);
2976 rcu_read_unlock();
2977 ASSERT(eb);
2978 return eb;
2979}
2980
d1310b2e
CM
2981/*
2982 * after a readpage IO is done, we need to:
2983 * clear the uptodate bits on error
2984 * set the uptodate bits if things worked
2985 * set the page up to date if all extents in the tree are uptodate
2986 * clear the lock bit in the extent tree
2987 * unlock the page if there are no other extents locked for it
2988 *
2989 * Scheduling is not allowed, so the extent state tree is expected
2990 * to have one and only one object corresponding to this IO.
2991 */
4246a0b6 2992static void end_bio_extent_readpage(struct bio *bio)
d1310b2e 2993{
2c30c71b 2994 struct bio_vec *bvec;
c3a3b19b 2995 struct btrfs_bio *bbio = btrfs_bio(bio);
7870d082 2996 struct extent_io_tree *tree, *failure_tree;
94e8c95c 2997 struct processed_extent processed = { 0 };
7ffd27e3
QW
2998 /*
2999 * The offset to the beginning of a bio, since one bio can never be
3000 * larger than UINT_MAX, u32 here is enough.
3001 */
3002 u32 bio_offset = 0;
5cf1ab56 3003 int mirror;
d1310b2e 3004 int ret;
6dc4f100 3005 struct bvec_iter_all iter_all;
d1310b2e 3006
c09abff8 3007 ASSERT(!bio_flagged(bio, BIO_CLONED));
2b070cfe 3008 bio_for_each_segment_all(bvec, bio, iter_all) {
150e4b05 3009 bool uptodate = !bio->bi_status;
d1310b2e 3010 struct page *page = bvec->bv_page;
a71754fc 3011 struct inode *inode = page->mapping->host;
ab8d0fc4 3012 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7ffd27e3 3013 const u32 sectorsize = fs_info->sectorsize;
150e4b05 3014 unsigned int error_bitmap = (unsigned int)-1;
7ffd27e3
QW
3015 u64 start;
3016 u64 end;
3017 u32 len;
507903b8 3018
ab8d0fc4
JM
3019 btrfs_debug(fs_info,
3020 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
1201b58b 3021 bio->bi_iter.bi_sector, bio->bi_status,
c3a3b19b 3022 bbio->mirror_num);
a71754fc 3023 tree = &BTRFS_I(inode)->io_tree;
7870d082 3024 failure_tree = &BTRFS_I(inode)->io_failure_tree;
902b22f3 3025
8b8bbd46
QW
3026 /*
3027 * We always issue full-sector reads, but if some block in a
3028 * page fails to read, blk_update_request() will advance
3029 * bv_offset and adjust bv_len to compensate. Print a warning
3030 * for unaligned offsets, and an error if they don't add up to
3031 * a full sector.
3032 */
3033 if (!IS_ALIGNED(bvec->bv_offset, sectorsize))
3034 btrfs_err(fs_info,
3035 "partial page read in btrfs with offset %u and length %u",
3036 bvec->bv_offset, bvec->bv_len);
3037 else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len,
3038 sectorsize))
3039 btrfs_info(fs_info,
3040 "incomplete page read with offset %u and length %u",
3041 bvec->bv_offset, bvec->bv_len);
3042
3043 start = page_offset(page) + bvec->bv_offset;
3044 end = start + bvec->bv_len - 1;
facc8a22 3045 len = bvec->bv_len;
d1310b2e 3046
c3a3b19b 3047 mirror = bbio->mirror_num;
78e62c02 3048 if (likely(uptodate)) {
150e4b05 3049 if (is_data_inode(inode)) {
c3a3b19b 3050 error_bitmap = btrfs_verify_data_csum(bbio,
5e295768 3051 bio_offset, page, start, end);
150e4b05
QW
3052 ret = error_bitmap;
3053 } else {
c3a3b19b 3054 ret = btrfs_validate_metadata_buffer(bbio,
8e1dc982 3055 page, start, end, mirror);
150e4b05 3056 }
5ee0844d 3057 if (ret)
150e4b05 3058 uptodate = false;
5ee0844d 3059 else
7870d082
JB
3060 clean_io_failure(BTRFS_I(inode)->root->fs_info,
3061 failure_tree, tree, start,
3062 page,
3063 btrfs_ino(BTRFS_I(inode)), 0);
d1310b2e 3064 }
ea466794 3065
f2a09da9
MX
3066 if (likely(uptodate))
3067 goto readpage_ok;
3068
be17b3af 3069 if (is_data_inode(inode)) {
510671d2
JB
3070 /*
3071 * If we failed to submit the IO at all we'll have a
3072 * mirror_num == 0, in which case we need to just mark
3073 * the page with an error and unlock it and carry on.
3074 */
3075 if (mirror == 0)
3076 goto readpage_ok;
3077
f4a8e656 3078 /*
150e4b05
QW
3079 * btrfs_submit_read_repair() will handle all the good
3080 * and bad sectors, we just continue to the next bvec.
f4a8e656 3081 */
150e4b05
QW
3082 submit_read_repair(inode, bio, bio_offset, page,
3083 start - page_offset(page), start,
3084 end, mirror, error_bitmap,
3085 btrfs_submit_data_bio);
3086
3087 ASSERT(bio_offset + len > bio_offset);
3088 bio_offset += len;
3089 continue;
78e62c02
NB
3090 } else {
3091 struct extent_buffer *eb;
3092
d9bb77d5 3093 eb = find_extent_buffer_readpage(fs_info, page, start);
78e62c02
NB
3094 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
3095 eb->read_mirror = mirror;
3096 atomic_dec(&eb->io_pages);
7e38326f 3097 }
f2a09da9 3098readpage_ok:
883d0de4 3099 if (likely(uptodate)) {
a71754fc 3100 loff_t i_size = i_size_read(inode);
09cbfeaf 3101 pgoff_t end_index = i_size >> PAGE_SHIFT;
a71754fc 3102
c28ea613
QW
3103 /*
3104 * Zero out the remaining part if this range straddles
3105 * i_size.
3106 *
3107 * Here we should only zero the range inside the bvec,
3108 * not touch anything else.
3109 *
3110 * NOTE: i_size is exclusive while end is inclusive.
3111 */
3112 if (page->index == end_index && i_size <= end) {
3113 u32 zero_start = max(offset_in_page(i_size),
d2dcc8ed 3114 offset_in_page(start));
c28ea613
QW
3115
3116 zero_user_segment(page, zero_start,
3117 offset_in_page(end) + 1);
3118 }
70dec807 3119 }
7ffd27e3
QW
3120 ASSERT(bio_offset + len > bio_offset);
3121 bio_offset += len;
883d0de4 3122
e09caaf9 3123 /* Update page status and unlock */
92082d40 3124 end_page_read(page, uptodate, start, len);
94e8c95c 3125 endio_readpage_release_extent(&processed, BTRFS_I(inode),
14605409 3126 start, end, PageUptodate(page));
2c30c71b 3127 }
94e8c95c
QW
3128 /* Release the last extent */
3129 endio_readpage_release_extent(&processed, NULL, 0, 0, false);
c3a3b19b 3130 btrfs_bio_free_csum(bbio);
d1310b2e 3131 bio_put(bio);
d1310b2e
CM
3132}
3133
9be3395b 3134/*
184f999e
DS
3135 * Initialize the members up to but not including 'bio'. Use after allocating a
3136 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
3137 * 'bio' because use of __GFP_ZERO is not supported.
9be3395b 3138 */
c3a3b19b 3139static inline void btrfs_bio_init(struct btrfs_bio *bbio)
d1310b2e 3140{
c3a3b19b 3141 memset(bbio, 0, offsetof(struct btrfs_bio, bio));
184f999e 3142}
d1310b2e 3143
9be3395b 3144/*
cd8e0cca
QW
3145 * Allocate a btrfs_io_bio, with @nr_iovecs as maximum number of iovecs.
3146 *
3147 * The bio allocation is backed by bioset and does not fail.
9be3395b 3148 */
c3a3b19b 3149struct bio *btrfs_bio_alloc(unsigned int nr_iovecs)
d1310b2e
CM
3150{
3151 struct bio *bio;
d1310b2e 3152
cd8e0cca
QW
3153 ASSERT(0 < nr_iovecs && nr_iovecs <= BIO_MAX_VECS);
3154 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
c3a3b19b 3155 btrfs_bio_init(btrfs_bio(bio));
d1310b2e
CM
3156 return bio;
3157}
3158
8b6c1d56 3159struct bio *btrfs_bio_clone(struct bio *bio)
9be3395b 3160{
c3a3b19b 3161 struct btrfs_bio *bbio;
23ea8e5a 3162 struct bio *new;
9be3395b 3163
6e707bcd 3164 /* Bio allocation backed by a bioset does not fail */
8ac9f7c1 3165 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
c3a3b19b
QW
3166 bbio = btrfs_bio(new);
3167 btrfs_bio_init(bbio);
3168 bbio->iter = bio->bi_iter;
23ea8e5a
MX
3169 return new;
3170}
9be3395b 3171
21dda654 3172struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size)
2f8e9140
LB
3173{
3174 struct bio *bio;
c3a3b19b 3175 struct btrfs_bio *bbio;
2f8e9140 3176
21dda654
CK
3177 ASSERT(offset <= UINT_MAX && size <= UINT_MAX);
3178
2f8e9140 3179 /* this will never fail when it's backed by a bioset */
8ac9f7c1 3180 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
2f8e9140
LB
3181 ASSERT(bio);
3182
c3a3b19b
QW
3183 bbio = btrfs_bio(bio);
3184 btrfs_bio_init(bbio);
2f8e9140
LB
3185
3186 bio_trim(bio, offset >> 9, size >> 9);
c3a3b19b 3187 bbio->iter = bio->bi_iter;
2f8e9140
LB
3188 return bio;
3189}
9be3395b 3190
953651eb
NA
3191/**
3192 * Attempt to add a page to bio
3193 *
be8d1a2a 3194 * @bio_ctrl: record both the bio, and its bio_flags
953651eb
NA
3195 * @page: page to add to the bio
3196 * @disk_bytenr: offset of the new bio or to check whether we are adding
3197 * a contiguous page to the previous one
953651eb 3198 * @size: portion of page that we want to write
be8d1a2a 3199 * @pg_offset: starting offset in the page
953651eb 3200 * @bio_flags: flags of the current bio to see if we can merge them
953651eb
NA
3201 *
3202 * Attempt to add a page to bio considering stripe alignment etc.
3203 *
e0eefe07
QW
3204 * Return >= 0 for the number of bytes added to the bio.
3205 * Can return 0 if the current bio is already at stripe/zone boundary.
3206 * Return <0 for error.
953651eb 3207 */
e0eefe07
QW
3208static int btrfs_bio_add_page(struct btrfs_bio_ctrl *bio_ctrl,
3209 struct page *page,
3210 u64 disk_bytenr, unsigned int size,
3211 unsigned int pg_offset,
3212 unsigned long bio_flags)
953651eb 3213{
390ed29b
QW
3214 struct bio *bio = bio_ctrl->bio;
3215 u32 bio_size = bio->bi_iter.bi_size;
e0eefe07 3216 u32 real_size;
953651eb
NA
3217 const sector_t sector = disk_bytenr >> SECTOR_SHIFT;
3218 bool contig;
e1326f03 3219 int ret;
953651eb 3220
390ed29b
QW
3221 ASSERT(bio);
3222 /* The limit should be calculated when bio_ctrl->bio is allocated */
3223 ASSERT(bio_ctrl->len_to_oe_boundary && bio_ctrl->len_to_stripe_boundary);
3224 if (bio_ctrl->bio_flags != bio_flags)
e0eefe07 3225 return 0;
953651eb 3226
390ed29b 3227 if (bio_ctrl->bio_flags & EXTENT_BIO_COMPRESSED)
953651eb
NA
3228 contig = bio->bi_iter.bi_sector == sector;
3229 else
3230 contig = bio_end_sector(bio) == sector;
3231 if (!contig)
e0eefe07 3232 return 0;
953651eb 3233
e0eefe07
QW
3234 real_size = min(bio_ctrl->len_to_oe_boundary,
3235 bio_ctrl->len_to_stripe_boundary) - bio_size;
3236 real_size = min(real_size, size);
3237
3238 /*
3239 * If real_size is 0, never call bio_add_*_page(), as even size is 0,
3240 * bio will still execute its endio function on the page!
3241 */
3242 if (real_size == 0)
3243 return 0;
953651eb 3244
390ed29b 3245 if (bio_op(bio) == REQ_OP_ZONE_APPEND)
e0eefe07 3246 ret = bio_add_zone_append_page(bio, page, real_size, pg_offset);
390ed29b 3247 else
e0eefe07 3248 ret = bio_add_page(bio, page, real_size, pg_offset);
e1326f03 3249
e0eefe07 3250 return ret;
953651eb
NA
3251}
3252
390ed29b 3253static int calc_bio_boundaries(struct btrfs_bio_ctrl *bio_ctrl,
939c7feb 3254 struct btrfs_inode *inode, u64 file_offset)
390ed29b
QW
3255{
3256 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3257 struct btrfs_io_geometry geom;
3258 struct btrfs_ordered_extent *ordered;
3259 struct extent_map *em;
3260 u64 logical = (bio_ctrl->bio->bi_iter.bi_sector << SECTOR_SHIFT);
3261 int ret;
3262
3263 /*
3264 * Pages for compressed extent are never submitted to disk directly,
3265 * thus it has no real boundary, just set them to U32_MAX.
3266 *
3267 * The split happens for real compressed bio, which happens in
3268 * btrfs_submit_compressed_read/write().
3269 */
3270 if (bio_ctrl->bio_flags & EXTENT_BIO_COMPRESSED) {
3271 bio_ctrl->len_to_oe_boundary = U32_MAX;
3272 bio_ctrl->len_to_stripe_boundary = U32_MAX;
3273 return 0;
3274 }
3275 em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
3276 if (IS_ERR(em))
3277 return PTR_ERR(em);
3278 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio_ctrl->bio),
3279 logical, &geom);
3280 free_extent_map(em);
3281 if (ret < 0) {
3282 return ret;
3283 }
3284 if (geom.len > U32_MAX)
3285 bio_ctrl->len_to_stripe_boundary = U32_MAX;
3286 else
3287 bio_ctrl->len_to_stripe_boundary = (u32)geom.len;
3288
73672710 3289 if (bio_op(bio_ctrl->bio) != REQ_OP_ZONE_APPEND) {
390ed29b
QW
3290 bio_ctrl->len_to_oe_boundary = U32_MAX;
3291 return 0;
3292 }
3293
390ed29b 3294 /* Ordered extent not yet created, so we're good */
939c7feb 3295 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
390ed29b
QW
3296 if (!ordered) {
3297 bio_ctrl->len_to_oe_boundary = U32_MAX;
3298 return 0;
3299 }
3300
3301 bio_ctrl->len_to_oe_boundary = min_t(u32, U32_MAX,
3302 ordered->disk_bytenr + ordered->disk_num_bytes - logical);
3303 btrfs_put_ordered_extent(ordered);
3304 return 0;
3305}
3306
e0eefe07
QW
3307static int alloc_new_bio(struct btrfs_inode *inode,
3308 struct btrfs_bio_ctrl *bio_ctrl,
3309 struct writeback_control *wbc,
3310 unsigned int opf,
3311 bio_end_io_t end_io_func,
939c7feb 3312 u64 disk_bytenr, u32 offset, u64 file_offset,
e0eefe07
QW
3313 unsigned long bio_flags)
3314{
3315 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3316 struct bio *bio;
3317 int ret;
3318
c3a3b19b 3319 bio = btrfs_bio_alloc(BIO_MAX_VECS);
e0eefe07
QW
3320 /*
3321 * For compressed page range, its disk_bytenr is always @disk_bytenr
3322 * passed in, no matter if we have added any range into previous bio.
3323 */
3324 if (bio_flags & EXTENT_BIO_COMPRESSED)
cd8e0cca 3325 bio->bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
e0eefe07 3326 else
cd8e0cca 3327 bio->bi_iter.bi_sector = (disk_bytenr + offset) >> SECTOR_SHIFT;
e0eefe07
QW
3328 bio_ctrl->bio = bio;
3329 bio_ctrl->bio_flags = bio_flags;
e0eefe07
QW
3330 bio->bi_end_io = end_io_func;
3331 bio->bi_private = &inode->io_tree;
3332 bio->bi_write_hint = inode->vfs_inode.i_write_hint;
3333 bio->bi_opf = opf;
939c7feb
NA
3334 ret = calc_bio_boundaries(bio_ctrl, inode, file_offset);
3335 if (ret < 0)
3336 goto error;
e0eefe07 3337
50f1cff3
CH
3338 if (wbc) {
3339 /*
3340 * For Zone append we need the correct block_device that we are
3341 * going to write to set in the bio to be able to respect the
3342 * hardware limitation. Look it up here:
3343 */
3344 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
3345 struct btrfs_device *dev;
3346
3347 dev = btrfs_zoned_get_device(fs_info, disk_bytenr,
3348 fs_info->sectorsize);
3349 if (IS_ERR(dev)) {
3350 ret = PTR_ERR(dev);
3351 goto error;
3352 }
e0eefe07 3353
50f1cff3
CH
3354 bio_set_dev(bio, dev->bdev);
3355 } else {
3356 /*
3357 * Otherwise pick the last added device to support
3358 * cgroup writeback. For multi-device file systems this
3359 * means blk-cgroup policies have to always be set on the
3360 * last added/replaced device. This is a bit odd but has
3361 * been like that for a long time.
3362 */
3363 bio_set_dev(bio, fs_info->fs_devices->latest_dev->bdev);
e0eefe07 3364 }
50f1cff3
CH
3365 wbc_init_bio(wbc, bio);
3366 } else {
3367 ASSERT(bio_op(bio) != REQ_OP_ZONE_APPEND);
e0eefe07
QW
3368 }
3369 return 0;
3370error:
3371 bio_ctrl->bio = NULL;
3372 bio->bi_status = errno_to_blk_status(ret);
3373 bio_endio(bio);
3374 return ret;
3375}
3376
4b81ba48
DS
3377/*
3378 * @opf: bio REQ_OP_* and REQ_* flags as one value
b8b3d625
DS
3379 * @wbc: optional writeback control for io accounting
3380 * @page: page to add to the bio
0c64c33c
QW
3381 * @disk_bytenr: logical bytenr where the write will be
3382 * @size: portion of page that we want to write to
b8b3d625
DS
3383 * @pg_offset: offset of the new bio or to check whether we are adding
3384 * a contiguous page to the previous one
5c2b1fd7 3385 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
b8b3d625
DS
3386 * @end_io_func: end_io callback for new bio
3387 * @mirror_num: desired mirror to read/write
3388 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
3389 * @bio_flags: flags of the current bio to see if we can merge them
4b81ba48 3390 */
0ceb34bf 3391static int submit_extent_page(unsigned int opf,
da2f0f74 3392 struct writeback_control *wbc,
390ed29b 3393 struct btrfs_bio_ctrl *bio_ctrl,
0c64c33c 3394 struct page *page, u64 disk_bytenr,
6c5a4e2c 3395 size_t size, unsigned long pg_offset,
f188591e 3396 bio_end_io_t end_io_func,
c8b97818 3397 int mirror_num,
005efedf
FM
3398 unsigned long bio_flags,
3399 bool force_bio_submit)
d1310b2e
CM
3400{
3401 int ret = 0;
e1326f03 3402 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
e0eefe07 3403 unsigned int cur = pg_offset;
d1310b2e 3404
390ed29b 3405 ASSERT(bio_ctrl);
5c2b1fd7 3406
390ed29b
QW
3407 ASSERT(pg_offset < PAGE_SIZE && size <= PAGE_SIZE &&
3408 pg_offset + size <= PAGE_SIZE);
e0eefe07
QW
3409 if (force_bio_submit && bio_ctrl->bio) {
3410 ret = submit_one_bio(bio_ctrl->bio, mirror_num, bio_ctrl->bio_flags);
3411 bio_ctrl->bio = NULL;
3412 if (ret < 0)
3413 return ret;
3414 }
3415
3416 while (cur < pg_offset + size) {
3417 u32 offset = cur - pg_offset;
3418 int added;
3419
3420 /* Allocate new bio if needed */
3421 if (!bio_ctrl->bio) {
3422 ret = alloc_new_bio(inode, bio_ctrl, wbc, opf,
3423 end_io_func, disk_bytenr, offset,
939c7feb 3424 page_offset(page) + cur,
e0eefe07
QW
3425 bio_flags);
3426 if (ret < 0)
3427 return ret;
3428 }
3429 /*
3430 * We must go through btrfs_bio_add_page() to ensure each
3431 * page range won't cross various boundaries.
3432 */
3433 if (bio_flags & EXTENT_BIO_COMPRESSED)
3434 added = btrfs_bio_add_page(bio_ctrl, page, disk_bytenr,
3435 size - offset, pg_offset + offset,
3436 bio_flags);
3437 else
3438 added = btrfs_bio_add_page(bio_ctrl, page,
3439 disk_bytenr + offset, size - offset,
3440 pg_offset + offset, bio_flags);
3441
3442 /* Metadata page range should never be split */
3443 if (!is_data_inode(&inode->vfs_inode))
3444 ASSERT(added == 0 || added == size - offset);
3445
3446 /* At least we added some page, update the account */
3447 if (wbc && added)
3448 wbc_account_cgroup_owner(wbc, page, added);
3449
3450 /* We have reached boundary, submit right now */
3451 if (added < size - offset) {
3452 /* The bio should contain some page(s) */
3453 ASSERT(bio_ctrl->bio->bi_iter.bi_size);
3454 ret = submit_one_bio(bio_ctrl->bio, mirror_num,
3455 bio_ctrl->bio_flags);
390ed29b
QW
3456 bio_ctrl->bio = NULL;
3457 if (ret < 0)
79787eaa 3458 return ret;
d1310b2e 3459 }
e0eefe07 3460 cur += added;
d1310b2e 3461 }
e0eefe07 3462 return 0;
d1310b2e
CM
3463}
3464
760f991f
QW
3465static int attach_extent_buffer_page(struct extent_buffer *eb,
3466 struct page *page,
3467 struct btrfs_subpage *prealloc)
d1310b2e 3468{
760f991f
QW
3469 struct btrfs_fs_info *fs_info = eb->fs_info;
3470 int ret = 0;
3471
0d01e247
QW
3472 /*
3473 * If the page is mapped to btree inode, we should hold the private
3474 * lock to prevent race.
3475 * For cloned or dummy extent buffers, their pages are not mapped and
3476 * will not race with any other ebs.
3477 */
3478 if (page->mapping)
3479 lockdep_assert_held(&page->mapping->private_lock);
3480
760f991f
QW
3481 if (fs_info->sectorsize == PAGE_SIZE) {
3482 if (!PagePrivate(page))
3483 attach_page_private(page, eb);
3484 else
3485 WARN_ON(page->private != (unsigned long)eb);
3486 return 0;
3487 }
3488
3489 /* Already mapped, just free prealloc */
3490 if (PagePrivate(page)) {
3491 btrfs_free_subpage(prealloc);
3492 return 0;
3493 }
3494
3495 if (prealloc)
3496 /* Has preallocated memory for subpage */
3497 attach_page_private(page, prealloc);
d1b89bc0 3498 else
760f991f
QW
3499 /* Do new allocation to attach subpage */
3500 ret = btrfs_attach_subpage(fs_info, page,
3501 BTRFS_SUBPAGE_METADATA);
3502 return ret;
d1310b2e
CM
3503}
3504
32443de3 3505int set_page_extent_mapped(struct page *page)
d1310b2e 3506{
32443de3
QW
3507 struct btrfs_fs_info *fs_info;
3508
3509 ASSERT(page->mapping);
3510
3511 if (PagePrivate(page))
3512 return 0;
3513
3514 fs_info = btrfs_sb(page->mapping->host->i_sb);
3515
3516 if (fs_info->sectorsize < PAGE_SIZE)
3517 return btrfs_attach_subpage(fs_info, page, BTRFS_SUBPAGE_DATA);
3518
3519 attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE);
3520 return 0;
3521}
3522
3523void clear_page_extent_mapped(struct page *page)
3524{
3525 struct btrfs_fs_info *fs_info;
3526
3527 ASSERT(page->mapping);
3528
d1b89bc0 3529 if (!PagePrivate(page))
32443de3
QW
3530 return;
3531
3532 fs_info = btrfs_sb(page->mapping->host->i_sb);
3533 if (fs_info->sectorsize < PAGE_SIZE)
3534 return btrfs_detach_subpage(fs_info, page);
3535
3536 detach_page_private(page);
d1310b2e
CM
3537}
3538
125bac01
MX
3539static struct extent_map *
3540__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
1a5ee1e6 3541 u64 start, u64 len, struct extent_map **em_cached)
125bac01
MX
3542{
3543 struct extent_map *em;
3544
3545 if (em_cached && *em_cached) {
3546 em = *em_cached;
cbc0e928 3547 if (extent_map_in_tree(em) && start >= em->start &&
125bac01 3548 start < extent_map_end(em)) {
490b54d6 3549 refcount_inc(&em->refs);
125bac01
MX
3550 return em;
3551 }
3552
3553 free_extent_map(em);
3554 *em_cached = NULL;
3555 }
3556
1a5ee1e6 3557 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, start, len);
c0347550 3558 if (em_cached && !IS_ERR(em)) {
125bac01 3559 BUG_ON(*em_cached);
490b54d6 3560 refcount_inc(&em->refs);
125bac01
MX
3561 *em_cached = em;
3562 }
3563 return em;
3564}
d1310b2e
CM
3565/*
3566 * basic readpage implementation. Locked extent state structs are inserted
3567 * into the tree that are removed when the IO is done (by the end_io
3568 * handlers)
79787eaa 3569 * XXX JDM: This needs looking at to ensure proper page locking
baf863b9 3570 * return 0 on success, otherwise return error
d1310b2e 3571 */
0f208812 3572int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
390ed29b 3573 struct btrfs_bio_ctrl *bio_ctrl,
0f208812 3574 unsigned int read_flags, u64 *prev_em_start)
d1310b2e
CM
3575{
3576 struct inode *inode = page->mapping->host;
92082d40 3577 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4eee4fa4 3578 u64 start = page_offset(page);
8eec8296 3579 const u64 end = start + PAGE_SIZE - 1;
d1310b2e
CM
3580 u64 cur = start;
3581 u64 extent_offset;
3582 u64 last_byte = i_size_read(inode);
3583 u64 block_start;
3584 u64 cur_end;
d1310b2e 3585 struct extent_map *em;
baf863b9 3586 int ret = 0;
306e16ce 3587 size_t pg_offset = 0;
d1310b2e
CM
3588 size_t iosize;
3589 size_t blocksize = inode->i_sb->s_blocksize;
f657a31c 3590 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
ae6957eb 3591
32443de3
QW
3592 ret = set_page_extent_mapped(page);
3593 if (ret < 0) {
3594 unlock_extent(tree, start, end);
92082d40
QW
3595 btrfs_page_set_error(fs_info, page, start, PAGE_SIZE);
3596 unlock_page(page);
32443de3
QW
3597 goto out;
3598 }
d1310b2e 3599
09cbfeaf 3600 if (page->index == last_byte >> PAGE_SHIFT) {
7073017a 3601 size_t zero_offset = offset_in_page(last_byte);
c8b97818
CM
3602
3603 if (zero_offset) {
09cbfeaf 3604 iosize = PAGE_SIZE - zero_offset;
d048b9c2 3605 memzero_page(page, zero_offset, iosize);
c8b97818 3606 flush_dcache_page(page);
c8b97818
CM
3607 }
3608 }
92082d40 3609 begin_page_read(fs_info, page);
d1310b2e 3610 while (cur <= end) {
4c37a793 3611 unsigned long this_bio_flag = 0;
005efedf 3612 bool force_bio_submit = false;
0c64c33c 3613 u64 disk_bytenr;
c8f2f24b 3614
6a404910 3615 ASSERT(IS_ALIGNED(cur, fs_info->sectorsize));
d1310b2e 3616 if (cur >= last_byte) {
507903b8
AJ
3617 struct extent_state *cached = NULL;
3618
09cbfeaf 3619 iosize = PAGE_SIZE - pg_offset;
d048b9c2 3620 memzero_page(page, pg_offset, iosize);
d1310b2e 3621 flush_dcache_page(page);
d1310b2e 3622 set_extent_uptodate(tree, cur, cur + iosize - 1,
507903b8 3623 &cached, GFP_NOFS);
7f042a83 3624 unlock_extent_cached(tree, cur,
e43bbe5e 3625 cur + iosize - 1, &cached);
92082d40 3626 end_page_read(page, true, cur, iosize);
d1310b2e
CM
3627 break;
3628 }
125bac01 3629 em = __get_extent_map(inode, page, pg_offset, cur,
1a5ee1e6 3630 end - cur + 1, em_cached);
c0347550 3631 if (IS_ERR(em)) {
7f042a83 3632 unlock_extent(tree, cur, end);
92082d40 3633 end_page_read(page, false, cur, end + 1 - cur);
bbf0ea7e 3634 ret = PTR_ERR(em);
d1310b2e
CM
3635 break;
3636 }
d1310b2e
CM
3637 extent_offset = cur - em->start;
3638 BUG_ON(extent_map_end(em) <= cur);
3639 BUG_ON(end < cur);
3640
261507a0 3641 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
4b384318 3642 this_bio_flag |= EXTENT_BIO_COMPRESSED;
261507a0
LZ
3643 extent_set_compress_type(&this_bio_flag,
3644 em->compress_type);
3645 }
c8b97818 3646
d1310b2e
CM
3647 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3648 cur_end = min(extent_map_end(em) - 1, end);
fda2832f 3649 iosize = ALIGN(iosize, blocksize);
949b3273 3650 if (this_bio_flag & EXTENT_BIO_COMPRESSED)
0c64c33c 3651 disk_bytenr = em->block_start;
949b3273 3652 else
0c64c33c 3653 disk_bytenr = em->block_start + extent_offset;
d1310b2e 3654 block_start = em->block_start;
d899e052
YZ
3655 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3656 block_start = EXTENT_MAP_HOLE;
005efedf
FM
3657
3658 /*
3659 * If we have a file range that points to a compressed extent
260db43c 3660 * and it's followed by a consecutive file range that points
005efedf
FM
3661 * to the same compressed extent (possibly with a different
3662 * offset and/or length, so it either points to the whole extent
3663 * or only part of it), we must make sure we do not submit a
3664 * single bio to populate the pages for the 2 ranges because
3665 * this makes the compressed extent read zero out the pages
3666 * belonging to the 2nd range. Imagine the following scenario:
3667 *
3668 * File layout
3669 * [0 - 8K] [8K - 24K]
3670 * | |
3671 * | |
3672 * points to extent X, points to extent X,
3673 * offset 4K, length of 8K offset 0, length 16K
3674 *
3675 * [extent X, compressed length = 4K uncompressed length = 16K]
3676 *
3677 * If the bio to read the compressed extent covers both ranges,
3678 * it will decompress extent X into the pages belonging to the
3679 * first range and then it will stop, zeroing out the remaining
3680 * pages that belong to the other range that points to extent X.
3681 * So here we make sure we submit 2 bios, one for the first
3682 * range and another one for the third range. Both will target
3683 * the same physical extent from disk, but we can't currently
3684 * make the compressed bio endio callback populate the pages
3685 * for both ranges because each compressed bio is tightly
3686 * coupled with a single extent map, and each range can have
3687 * an extent map with a different offset value relative to the
3688 * uncompressed data of our extent and different lengths. This
3689 * is a corner case so we prioritize correctness over
3690 * non-optimal behavior (submitting 2 bios for the same extent).
3691 */
3692 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3693 prev_em_start && *prev_em_start != (u64)-1 &&
8e928218 3694 *prev_em_start != em->start)
005efedf
FM
3695 force_bio_submit = true;
3696
3697 if (prev_em_start)
8e928218 3698 *prev_em_start = em->start;
005efedf 3699
d1310b2e
CM
3700 free_extent_map(em);
3701 em = NULL;
3702
3703 /* we've found a hole, just zero and go on */
3704 if (block_start == EXTENT_MAP_HOLE) {
507903b8
AJ
3705 struct extent_state *cached = NULL;
3706
d048b9c2 3707 memzero_page(page, pg_offset, iosize);
d1310b2e 3708 flush_dcache_page(page);
d1310b2e
CM
3709
3710 set_extent_uptodate(tree, cur, cur + iosize - 1,
507903b8 3711 &cached, GFP_NOFS);
7f042a83 3712 unlock_extent_cached(tree, cur,
e43bbe5e 3713 cur + iosize - 1, &cached);
92082d40 3714 end_page_read(page, true, cur, iosize);
d1310b2e 3715 cur = cur + iosize;
306e16ce 3716 pg_offset += iosize;
d1310b2e
CM
3717 continue;
3718 }
3719 /* the get_extent function already copied into the page */
9655d298
CM
3720 if (test_range_bit(tree, cur, cur_end,
3721 EXTENT_UPTODATE, 1, NULL)) {
7f042a83 3722 unlock_extent(tree, cur, cur + iosize - 1);
92082d40 3723 end_page_read(page, true, cur, iosize);
d1310b2e 3724 cur = cur + iosize;
306e16ce 3725 pg_offset += iosize;
d1310b2e
CM
3726 continue;
3727 }
70dec807
CM
3728 /* we have an inline extent but it didn't get marked up
3729 * to date. Error out
3730 */
3731 if (block_start == EXTENT_MAP_INLINE) {
7f042a83 3732 unlock_extent(tree, cur, cur + iosize - 1);
92082d40 3733 end_page_read(page, false, cur, iosize);
70dec807 3734 cur = cur + iosize;
306e16ce 3735 pg_offset += iosize;
70dec807
CM
3736 continue;
3737 }
d1310b2e 3738
0ceb34bf 3739 ret = submit_extent_page(REQ_OP_READ | read_flags, NULL,
390ed29b
QW
3740 bio_ctrl, page, disk_bytenr, iosize,
3741 pg_offset,
fd513000 3742 end_bio_extent_readpage, 0,
005efedf
FM
3743 this_bio_flag,
3744 force_bio_submit);
ad3fc794 3745 if (ret) {
7f042a83 3746 unlock_extent(tree, cur, cur + iosize - 1);
92082d40 3747 end_page_read(page, false, cur, iosize);
baf863b9 3748 goto out;
edd33c99 3749 }
d1310b2e 3750 cur = cur + iosize;
306e16ce 3751 pg_offset += iosize;
d1310b2e 3752 }
90a887c9 3753out:
baf863b9 3754 return ret;
d1310b2e
CM
3755}
3756
b6660e80 3757static inline void contiguous_readpages(struct page *pages[], int nr_pages,
390ed29b
QW
3758 u64 start, u64 end,
3759 struct extent_map **em_cached,
3760 struct btrfs_bio_ctrl *bio_ctrl,
3761 u64 *prev_em_start)
9974090b 3762{
23d31bd4 3763 struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);
9974090b
MX
3764 int index;
3765
b272ae22 3766 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
9974090b
MX
3767
3768 for (index = 0; index < nr_pages; index++) {
390ed29b 3769 btrfs_do_readpage(pages[index], em_cached, bio_ctrl,
0f208812 3770 REQ_RAHEAD, prev_em_start);
09cbfeaf 3771 put_page(pages[index]);
9974090b
MX
3772 }
3773}
3774
3d4b9496 3775static void update_nr_written(struct writeback_control *wbc,
a9132667 3776 unsigned long nr_written)
11c8349b
CM
3777{
3778 wbc->nr_to_write -= nr_written;
11c8349b
CM
3779}
3780
d1310b2e 3781/*
40f76580
CM
3782 * helper for __extent_writepage, doing all of the delayed allocation setup.
3783 *
5eaad97a 3784 * This returns 1 if btrfs_run_delalloc_range function did all the work required
40f76580
CM
3785 * to write the page (copy into inline extent). In this case the IO has
3786 * been started and the page is already unlocked.
3787 *
3788 * This returns 0 if all went well (page still locked)
3789 * This returns < 0 if there were errors (page still locked)
d1310b2e 3790 */
cd4c0bf9 3791static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
83f1b680 3792 struct page *page, struct writeback_control *wbc)
40f76580 3793{
2749f7ef 3794 const u64 page_end = page_offset(page) + PAGE_SIZE - 1;
cf3075fb 3795 u64 delalloc_start = page_offset(page);
40f76580 3796 u64 delalloc_to_write = 0;
83f1b680
QW
3797 /* How many pages are started by btrfs_run_delalloc_range() */
3798 unsigned long nr_written = 0;
40f76580
CM
3799 int ret;
3800 int page_started = 0;
3801
2749f7ef
QW
3802 while (delalloc_start < page_end) {
3803 u64 delalloc_end = page_end;
3804 bool found;
40f76580 3805
cd4c0bf9 3806 found = find_lock_delalloc_range(&inode->vfs_inode, page,
40f76580 3807 &delalloc_start,
917aacec 3808 &delalloc_end);
3522e903 3809 if (!found) {
40f76580
CM
3810 delalloc_start = delalloc_end + 1;
3811 continue;
3812 }
cd4c0bf9 3813 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
83f1b680 3814 delalloc_end, &page_started, &nr_written, wbc);
40f76580 3815 if (ret) {
963e4db8
QW
3816 btrfs_page_set_error(inode->root->fs_info, page,
3817 page_offset(page), PAGE_SIZE);
7361b4ae 3818 return ret;
40f76580
CM
3819 }
3820 /*
ea1754a0
KS
3821 * delalloc_end is already one less than the total length, so
3822 * we don't subtract one from PAGE_SIZE
40f76580
CM
3823 */
3824 delalloc_to_write += (delalloc_end - delalloc_start +
ea1754a0 3825 PAGE_SIZE) >> PAGE_SHIFT;
40f76580
CM
3826 delalloc_start = delalloc_end + 1;
3827 }
3828 if (wbc->nr_to_write < delalloc_to_write) {
3829 int thresh = 8192;
3830
3831 if (delalloc_to_write < thresh * 2)
3832 thresh = delalloc_to_write;
3833 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3834 thresh);
3835 }
3836
83f1b680 3837 /* Did btrfs_run_dealloc_range() already unlock and start the IO? */
40f76580
CM
3838 if (page_started) {
3839 /*
83f1b680
QW
3840 * We've unlocked the page, so we can't update the mapping's
3841 * writeback index, just update nr_to_write.
40f76580 3842 */
83f1b680 3843 wbc->nr_to_write -= nr_written;
40f76580
CM
3844 return 1;
3845 }
3846
b69d1ee9 3847 return 0;
40f76580
CM
3848}
3849
c5ef5c6c
QW
3850/*
3851 * Find the first byte we need to write.
3852 *
3853 * For subpage, one page can contain several sectors, and
3854 * __extent_writepage_io() will just grab all extent maps in the page
3855 * range and try to submit all non-inline/non-compressed extents.
3856 *
3857 * This is a big problem for subpage, we shouldn't re-submit already written
3858 * data at all.
3859 * This function will lookup subpage dirty bit to find which range we really
3860 * need to submit.
3861 *
3862 * Return the next dirty range in [@start, @end).
3863 * If no dirty range is found, @start will be page_offset(page) + PAGE_SIZE.
3864 */
3865static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
3866 struct page *page, u64 *start, u64 *end)
3867{
3868 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
72a69cd0 3869 struct btrfs_subpage_info *spi = fs_info->subpage_info;
c5ef5c6c
QW
3870 u64 orig_start = *start;
3871 /* Declare as unsigned long so we can use bitmap ops */
c5ef5c6c 3872 unsigned long flags;
72a69cd0 3873 int range_start_bit;
c5ef5c6c
QW
3874 int range_end_bit;
3875
3876 /*
3877 * For regular sector size == page size case, since one page only
3878 * contains one sector, we return the page offset directly.
3879 */
3880 if (fs_info->sectorsize == PAGE_SIZE) {
3881 *start = page_offset(page);
3882 *end = page_offset(page) + PAGE_SIZE;
3883 return;
3884 }
3885
72a69cd0
QW
3886 range_start_bit = spi->dirty_offset +
3887 (offset_in_page(orig_start) >> fs_info->sectorsize_bits);
3888
c5ef5c6c
QW
3889 /* We should have the page locked, but just in case */
3890 spin_lock_irqsave(&subpage->lock, flags);
72a69cd0
QW
3891 bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit,
3892 spi->dirty_offset + spi->bitmap_nr_bits);
c5ef5c6c
QW
3893 spin_unlock_irqrestore(&subpage->lock, flags);
3894
72a69cd0
QW
3895 range_start_bit -= spi->dirty_offset;
3896 range_end_bit -= spi->dirty_offset;
3897
c5ef5c6c
QW
3898 *start = page_offset(page) + range_start_bit * fs_info->sectorsize;
3899 *end = page_offset(page) + range_end_bit * fs_info->sectorsize;
3900}
3901
40f76580
CM
3902/*
3903 * helper for __extent_writepage. This calls the writepage start hooks,
3904 * and does the loop to map the page into extents and bios.
3905 *
3906 * We return 1 if the IO is started and the page is unlocked,
3907 * 0 if all went well (page still locked)
3908 * < 0 if there were errors (page still locked)
3909 */
d4580fe2 3910static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
40f76580
CM
3911 struct page *page,
3912 struct writeback_control *wbc,
3913 struct extent_page_data *epd,
3914 loff_t i_size,
57e5ffeb 3915 int *nr_ret)
d1310b2e 3916{
6bc5636a 3917 struct btrfs_fs_info *fs_info = inode->root->fs_info;
a129ffb8
QW
3918 u64 cur = page_offset(page);
3919 u64 end = cur + PAGE_SIZE - 1;
d1310b2e 3920 u64 extent_offset;
d1310b2e 3921 u64 block_start;
d1310b2e 3922 struct extent_map *em;
40f76580
CM
3923 int ret = 0;
3924 int nr = 0;
d8e3fb10 3925 u32 opf = REQ_OP_WRITE;
57e5ffeb 3926 const unsigned int write_flags = wbc_to_write_flags(wbc);
40f76580 3927 bool compressed;
c8b97818 3928
a129ffb8 3929 ret = btrfs_writepage_cow_fixup(page);
d75855b4
NB
3930 if (ret) {
3931 /* Fixup worker will requeue */
5ab58055 3932 redirty_page_for_writepage(wbc, page);
d75855b4
NB
3933 unlock_page(page);
3934 return 1;
247e743c
CM
3935 }
3936
11c8349b
CM
3937 /*
3938 * we don't want to touch the inode after unlocking the page,
3939 * so we update the mapping writeback index now
3940 */
83f1b680 3941 update_nr_written(wbc, 1);
771ed689 3942
d1310b2e 3943 while (cur <= end) {
0c64c33c 3944 u64 disk_bytenr;
40f76580 3945 u64 em_end;
c5ef5c6c
QW
3946 u64 dirty_range_start = cur;
3947 u64 dirty_range_end;
6bc5636a 3948 u32 iosize;
58409edd 3949
40f76580 3950 if (cur >= i_size) {
38a39ac7 3951 btrfs_writepage_endio_finish_ordered(inode, page, cur,
25c1252a 3952 end, true);
cc1d0d93
QW
3953 /*
3954 * This range is beyond i_size, thus we don't need to
3955 * bother writing back.
3956 * But we still need to clear the dirty subpage bit, or
3957 * the next time the page gets dirtied, we will try to
3958 * writeback the sectors with subpage dirty bits,
3959 * causing writeback without ordered extent.
3960 */
3961 btrfs_page_clear_dirty(fs_info, page, cur, end + 1 - cur);
d1310b2e
CM
3962 break;
3963 }
c5ef5c6c
QW
3964
3965 find_next_dirty_byte(fs_info, page, &dirty_range_start,
3966 &dirty_range_end);
3967 if (cur < dirty_range_start) {
3968 cur = dirty_range_start;
3969 continue;
3970 }
3971
d4580fe2 3972 em = btrfs_get_extent(inode, NULL, 0, cur, end - cur + 1);
c0347550 3973 if (IS_ERR(em)) {
c5ef5c6c 3974 btrfs_page_set_error(fs_info, page, cur, end - cur + 1);
61391d56 3975 ret = PTR_ERR_OR_ZERO(em);
d1310b2e
CM
3976 break;
3977 }
3978
3979 extent_offset = cur - em->start;
40f76580 3980 em_end = extent_map_end(em);
6bc5636a
QW
3981 ASSERT(cur <= em_end);
3982 ASSERT(cur < end);
3983 ASSERT(IS_ALIGNED(em->start, fs_info->sectorsize));
3984 ASSERT(IS_ALIGNED(em->len, fs_info->sectorsize));
d1310b2e 3985 block_start = em->block_start;
c8b97818 3986 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
6bc5636a
QW
3987 disk_bytenr = em->block_start + extent_offset;
3988
c5ef5c6c
QW
3989 /*
3990 * Note that em_end from extent_map_end() and dirty_range_end from
3991 * find_next_dirty_byte() are all exclusive
3992 */
3993 iosize = min(min(em_end, end + 1), dirty_range_end) - cur;
d8e3fb10 3994
e380adfc 3995 if (btrfs_use_zone_append(inode, em->block_start))
d8e3fb10
NA
3996 opf = REQ_OP_ZONE_APPEND;
3997
d1310b2e
CM
3998 free_extent_map(em);
3999 em = NULL;
4000
c8b97818
CM
4001 /*
4002 * compressed and inline extents are written through other
4003 * paths in the FS
4004 */
4005 if (compressed || block_start == EXTENT_MAP_HOLE ||
d1310b2e 4006 block_start == EXTENT_MAP_INLINE) {
c8b04030 4007 if (compressed)
c8b97818 4008 nr++;
c8b04030 4009 else
38a39ac7 4010 btrfs_writepage_endio_finish_ordered(inode,
25c1252a 4011 page, cur, cur + iosize - 1, true);
cc1d0d93 4012 btrfs_page_clear_dirty(fs_info, page, cur, iosize);
c8b97818 4013 cur += iosize;
d1310b2e
CM
4014 continue;
4015 }
c8b97818 4016
d2a91064 4017 btrfs_set_range_writeback(inode, cur, cur + iosize - 1);
58409edd 4018 if (!PageWriteback(page)) {
d4580fe2 4019 btrfs_err(inode->root->fs_info,
58409edd
DS
4020 "page %lu not writeback, cur %llu end %llu",
4021 page->index, cur, end);
d1310b2e 4022 }
7f3c74fb 4023
c5ef5c6c
QW
4024 /*
4025 * Although the PageDirty bit is cleared before entering this
4026 * function, subpage dirty bit is not cleared.
4027 * So clear subpage dirty bit here so next time we won't submit
4028 * page for range already written to disk.
4029 */
4030 btrfs_page_clear_dirty(fs_info, page, cur, iosize);
4031
390ed29b
QW
4032 ret = submit_extent_page(opf | write_flags, wbc,
4033 &epd->bio_ctrl, page,
d8e3fb10 4034 disk_bytenr, iosize,
390ed29b 4035 cur - page_offset(page),
58409edd 4036 end_bio_extent_writepage,
390ed29b 4037 0, 0, false);
fe01aa65 4038 if (ret) {
c5ef5c6c 4039 btrfs_page_set_error(fs_info, page, cur, iosize);
fe01aa65 4040 if (PageWriteback(page))
c5ef5c6c
QW
4041 btrfs_page_clear_writeback(fs_info, page, cur,
4042 iosize);
fe01aa65 4043 }
d1310b2e 4044
6bc5636a 4045 cur += iosize;
d1310b2e
CM
4046 nr++;
4047 }
cc1d0d93
QW
4048 /*
4049 * If we finish without problem, we should not only clear page dirty,
4050 * but also empty subpage dirty bits
4051 */
4052 if (!ret)
4053 btrfs_page_assert_not_dirty(fs_info, page);
40f76580 4054 *nr_ret = nr;
40f76580
CM
4055 return ret;
4056}
4057
4058/*
4059 * the writepage semantics are similar to regular writepage. extent
4060 * records are inserted to lock ranges in the tree, and as dirty areas
4061 * are found, they are marked writeback. Then the lock bits are removed
4062 * and the end_io handler clears the writeback ranges
3065976b
QW
4063 *
4064 * Return 0 if everything goes well.
4065 * Return <0 for error.
40f76580
CM
4066 */
4067static int __extent_writepage(struct page *page, struct writeback_control *wbc,
aab6e9ed 4068 struct extent_page_data *epd)
40f76580
CM
4069{
4070 struct inode *inode = page->mapping->host;
e55a0de1 4071 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
cf3075fb
QW
4072 const u64 page_start = page_offset(page);
4073 const u64 page_end = page_start + PAGE_SIZE - 1;
40f76580
CM
4074 int ret;
4075 int nr = 0;
eb70d222 4076 size_t pg_offset;
40f76580 4077 loff_t i_size = i_size_read(inode);
09cbfeaf 4078 unsigned long end_index = i_size >> PAGE_SHIFT;
40f76580 4079
40f76580
CM
4080 trace___extent_writepage(page, inode, wbc);
4081
4082 WARN_ON(!PageLocked(page));
4083
963e4db8
QW
4084 btrfs_page_clear_error(btrfs_sb(inode->i_sb), page,
4085 page_offset(page), PAGE_SIZE);
40f76580 4086
7073017a 4087 pg_offset = offset_in_page(i_size);
40f76580
CM
4088 if (page->index > end_index ||
4089 (page->index == end_index && !pg_offset)) {
09cbfeaf 4090 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
40f76580
CM
4091 unlock_page(page);
4092 return 0;
4093 }
4094
4095 if (page->index == end_index) {
d048b9c2 4096 memzero_page(page, pg_offset, PAGE_SIZE - pg_offset);
40f76580
CM
4097 flush_dcache_page(page);
4098 }
4099
32443de3
QW
4100 ret = set_page_extent_mapped(page);
4101 if (ret < 0) {
4102 SetPageError(page);
4103 goto done;
4104 }
40f76580 4105
7789a55a 4106 if (!epd->extent_locked) {
83f1b680 4107 ret = writepage_delalloc(BTRFS_I(inode), page, wbc);
7789a55a 4108 if (ret == 1)
169d2c87 4109 return 0;
7789a55a
NB
4110 if (ret)
4111 goto done;
4112 }
40f76580 4113
d4580fe2 4114 ret = __extent_writepage_io(BTRFS_I(inode), page, wbc, epd, i_size,
83f1b680 4115 &nr);
40f76580 4116 if (ret == 1)
169d2c87 4117 return 0;
40f76580 4118
d1310b2e
CM
4119done:
4120 if (nr == 0) {
4121 /* make sure the mapping tag for page dirty gets cleared */
4122 set_page_writeback(page);
4123 end_page_writeback(page);
4124 }
963e4db8
QW
4125 /*
4126 * Here we used to have a check for PageError() and then set @ret and
4127 * call end_extent_writepage().
4128 *
4129 * But in fact setting @ret here will cause different error paths
4130 * between subpage and regular sectorsize.
4131 *
4132 * For regular page size, we never submit current page, but only add
4133 * current page to current bio.
4134 * The bio submission can only happen in next page.
4135 * Thus if we hit the PageError() branch, @ret is already set to
4136 * non-zero value and will not get updated for regular sectorsize.
4137 *
4138 * But for subpage case, it's possible we submit part of current page,
4139 * thus can get PageError() set by submitted bio of the same page,
4140 * while our @ret is still 0.
4141 *
4142 * So here we unify the behavior and don't set @ret.
4143 * Error can still be properly passed to higher layer as page will
4144 * be set error, here we just don't handle the IO failure.
4145 *
4146 * NOTE: This is just a hotfix for subpage.
4147 * The root fix will be properly ending ordered extent when we hit
4148 * an error during writeback.
4149 *
4150 * But that needs a bigger refactoring, as we not only need to grab the
4151 * submitted OE, but also need to know exactly at which bytenr we hit
4152 * the error.
4153 * Currently the full page based __extent_writepage_io() is not
4154 * capable of that.
4155 */
4156 if (PageError(page))
cf3075fb 4157 end_extent_writepage(page, ret, page_start, page_end);
e55a0de1
QW
4158 if (epd->extent_locked) {
4159 /*
4160 * If epd->extent_locked, it's from extent_write_locked_range(),
4161 * the page can either be locked by lock_page() or
4162 * process_one_page().
4163 * Let btrfs_page_unlock_writer() handle both cases.
4164 */
4165 ASSERT(wbc);
4166 btrfs_page_unlock_writer(fs_info, page, wbc->range_start,
4167 wbc->range_end + 1 - wbc->range_start);
4168 } else {
4169 unlock_page(page);
4170 }
3065976b 4171 ASSERT(ret <= 0);
40f76580 4172 return ret;
d1310b2e
CM
4173}
4174
fd8b2b61 4175void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
0b32f4bb 4176{
74316201
N
4177 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
4178 TASK_UNINTERRUPTIBLE);
0b32f4bb
JB
4179}
4180
18dfa711
FM
4181static void end_extent_buffer_writeback(struct extent_buffer *eb)
4182{
be1a1d7a
NA
4183 if (test_bit(EXTENT_BUFFER_ZONE_FINISH, &eb->bflags))
4184 btrfs_zone_finish_endio(eb->fs_info, eb->start, eb->len);
4185
18dfa711
FM
4186 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
4187 smp_mb__after_atomic();
4188 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
4189}
4190
2e3c2513 4191/*
a3efb2f0 4192 * Lock extent buffer status and pages for writeback.
2e3c2513 4193 *
a3efb2f0
QW
4194 * May try to flush write bio if we can't get the lock.
4195 *
4196 * Return 0 if the extent buffer doesn't need to be submitted.
4197 * (E.g. the extent buffer is not dirty)
4198 * Return >0 is the extent buffer is submitted to bio.
4199 * Return <0 if something went wrong, no page is locked.
2e3c2513 4200 */
9df76fb5 4201static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
0e378df1 4202 struct extent_page_data *epd)
0b32f4bb 4203{
9df76fb5 4204 struct btrfs_fs_info *fs_info = eb->fs_info;
2e3c2513 4205 int i, num_pages, failed_page_nr;
0b32f4bb
JB
4206 int flush = 0;
4207 int ret = 0;
4208
4209 if (!btrfs_try_tree_write_lock(eb)) {
f4340622 4210 ret = flush_write_bio(epd);
2e3c2513
QW
4211 if (ret < 0)
4212 return ret;
4213 flush = 1;
0b32f4bb
JB
4214 btrfs_tree_lock(eb);
4215 }
4216
4217 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
4218 btrfs_tree_unlock(eb);
4219 if (!epd->sync_io)
4220 return 0;
4221 if (!flush) {
f4340622 4222 ret = flush_write_bio(epd);
2e3c2513
QW
4223 if (ret < 0)
4224 return ret;
0b32f4bb
JB
4225 flush = 1;
4226 }
a098d8e8
CM
4227 while (1) {
4228 wait_on_extent_buffer_writeback(eb);
4229 btrfs_tree_lock(eb);
4230 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
4231 break;
0b32f4bb 4232 btrfs_tree_unlock(eb);
0b32f4bb
JB
4233 }
4234 }
4235
51561ffe
JB
4236 /*
4237 * We need to do this to prevent races in people who check if the eb is
4238 * under IO since we can end up having no IO bits set for a short period
4239 * of time.
4240 */
4241 spin_lock(&eb->refs_lock);
0b32f4bb
JB
4242 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4243 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
51561ffe 4244 spin_unlock(&eb->refs_lock);
0b32f4bb 4245 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
104b4e51
NB
4246 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
4247 -eb->len,
4248 fs_info->dirty_metadata_batch);
0b32f4bb 4249 ret = 1;
51561ffe
JB
4250 } else {
4251 spin_unlock(&eb->refs_lock);
0b32f4bb
JB
4252 }
4253
4254 btrfs_tree_unlock(eb);
4255
f3156df9
QW
4256 /*
4257 * Either we don't need to submit any tree block, or we're submitting
4258 * subpage eb.
4259 * Subpage metadata doesn't use page locking at all, so we can skip
4260 * the page locking.
4261 */
4262 if (!ret || fs_info->sectorsize < PAGE_SIZE)
0b32f4bb
JB
4263 return ret;
4264
65ad0104 4265 num_pages = num_extent_pages(eb);
0b32f4bb 4266 for (i = 0; i < num_pages; i++) {
fb85fc9a 4267 struct page *p = eb->pages[i];
0b32f4bb
JB
4268
4269 if (!trylock_page(p)) {
4270 if (!flush) {
18dfa711
FM
4271 int err;
4272
4273 err = flush_write_bio(epd);
4274 if (err < 0) {
4275 ret = err;
2e3c2513
QW
4276 failed_page_nr = i;
4277 goto err_unlock;
4278 }
0b32f4bb
JB
4279 flush = 1;
4280 }
4281 lock_page(p);
4282 }
4283 }
4284
4285 return ret;
2e3c2513
QW
4286err_unlock:
4287 /* Unlock already locked pages */
4288 for (i = 0; i < failed_page_nr; i++)
4289 unlock_page(eb->pages[i]);
18dfa711
FM
4290 /*
4291 * Clear EXTENT_BUFFER_WRITEBACK and wake up anyone waiting on it.
4292 * Also set back EXTENT_BUFFER_DIRTY so future attempts to this eb can
4293 * be made and undo everything done before.
4294 */
4295 btrfs_tree_lock(eb);
4296 spin_lock(&eb->refs_lock);
4297 set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4298 end_extent_buffer_writeback(eb);
4299 spin_unlock(&eb->refs_lock);
4300 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, eb->len,
4301 fs_info->dirty_metadata_batch);
4302 btrfs_clear_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
4303 btrfs_tree_unlock(eb);
2e3c2513 4304 return ret;
0b32f4bb
JB
4305}
4306
5a2c6075 4307static void set_btree_ioerr(struct page *page, struct extent_buffer *eb)
656f30db 4308{
5a2c6075 4309 struct btrfs_fs_info *fs_info = eb->fs_info;
656f30db 4310
5a2c6075 4311 btrfs_page_set_error(fs_info, page, eb->start, eb->len);
656f30db
FM
4312 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
4313 return;
4314
c2e39305
JB
4315 /*
4316 * A read may stumble upon this buffer later, make sure that it gets an
4317 * error and knows there was an error.
4318 */
4319 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4320
68b85589
JB
4321 /*
4322 * We need to set the mapping with the io error as well because a write
4323 * error will flip the file system readonly, and then syncfs() will
4324 * return a 0 because we are readonly if we don't modify the err seq for
4325 * the superblock.
4326 */
4327 mapping_set_error(page->mapping, -EIO);
4328
eb5b64f1
DZ
4329 /*
4330 * If we error out, we should add back the dirty_metadata_bytes
4331 * to make it consistent.
4332 */
eb5b64f1
DZ
4333 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
4334 eb->len, fs_info->dirty_metadata_batch);
4335
656f30db
FM
4336 /*
4337 * If writeback for a btree extent that doesn't belong to a log tree
4338 * failed, increment the counter transaction->eb_write_errors.
4339 * We do this because while the transaction is running and before it's
4340 * committing (when we call filemap_fdata[write|wait]_range against
4341 * the btree inode), we might have
4342 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
4343 * returns an error or an error happens during writeback, when we're
4344 * committing the transaction we wouldn't know about it, since the pages
4345 * can be no longer dirty nor marked anymore for writeback (if a
4346 * subsequent modification to the extent buffer didn't happen before the
4347 * transaction commit), which makes filemap_fdata[write|wait]_range not
4348 * able to find the pages tagged with SetPageError at transaction
4349 * commit time. So if this happens we must abort the transaction,
4350 * otherwise we commit a super block with btree roots that point to
4351 * btree nodes/leafs whose content on disk is invalid - either garbage
4352 * or the content of some node/leaf from a past generation that got
4353 * cowed or deleted and is no longer valid.
4354 *
4355 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
4356 * not be enough - we need to distinguish between log tree extents vs
4357 * non-log tree extents, and the next filemap_fdatawait_range() call
4358 * will catch and clear such errors in the mapping - and that call might
4359 * be from a log sync and not from a transaction commit. Also, checking
4360 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
4361 * not done and would not be reliable - the eb might have been released
4362 * from memory and reading it back again means that flag would not be
4363 * set (since it's a runtime flag, not persisted on disk).
4364 *
4365 * Using the flags below in the btree inode also makes us achieve the
4366 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
4367 * writeback for all dirty pages and before filemap_fdatawait_range()
4368 * is called, the writeback for all dirty pages had already finished
4369 * with errors - because we were not using AS_EIO/AS_ENOSPC,
4370 * filemap_fdatawait_range() would return success, as it could not know
4371 * that writeback errors happened (the pages were no longer tagged for
4372 * writeback).
4373 */
4374 switch (eb->log_index) {
4375 case -1:
5a2c6075 4376 set_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags);
656f30db
FM
4377 break;
4378 case 0:
5a2c6075 4379 set_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
656f30db
FM
4380 break;
4381 case 1:
5a2c6075 4382 set_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
656f30db
FM
4383 break;
4384 default:
4385 BUG(); /* unexpected, logic error */
4386 }
4387}
4388
2f3186d8
QW
4389/*
4390 * The endio specific version which won't touch any unsafe spinlock in endio
4391 * context.
4392 */
4393static struct extent_buffer *find_extent_buffer_nolock(
4394 struct btrfs_fs_info *fs_info, u64 start)
4395{
4396 struct extent_buffer *eb;
4397
4398 rcu_read_lock();
4399 eb = radix_tree_lookup(&fs_info->buffer_radix,
4400 start >> fs_info->sectorsize_bits);
4401 if (eb && atomic_inc_not_zero(&eb->refs)) {
4402 rcu_read_unlock();
4403 return eb;
4404 }
4405 rcu_read_unlock();
4406 return NULL;
4407}
4408
4409/*
4410 * The endio function for subpage extent buffer write.
4411 *
4412 * Unlike end_bio_extent_buffer_writepage(), we only call end_page_writeback()
4413 * after all extent buffers in the page has finished their writeback.
4414 */
fa04c165 4415static void end_bio_subpage_eb_writepage(struct bio *bio)
2f3186d8 4416{
fa04c165 4417 struct btrfs_fs_info *fs_info;
2f3186d8
QW
4418 struct bio_vec *bvec;
4419 struct bvec_iter_all iter_all;
4420
fa04c165
QW
4421 fs_info = btrfs_sb(bio_first_page_all(bio)->mapping->host->i_sb);
4422 ASSERT(fs_info->sectorsize < PAGE_SIZE);
4423
2f3186d8
QW
4424 ASSERT(!bio_flagged(bio, BIO_CLONED));
4425 bio_for_each_segment_all(bvec, bio, iter_all) {
4426 struct page *page = bvec->bv_page;
4427 u64 bvec_start = page_offset(page) + bvec->bv_offset;
4428 u64 bvec_end = bvec_start + bvec->bv_len - 1;
4429 u64 cur_bytenr = bvec_start;
4430
4431 ASSERT(IS_ALIGNED(bvec->bv_len, fs_info->nodesize));
4432
4433 /* Iterate through all extent buffers in the range */
4434 while (cur_bytenr <= bvec_end) {
4435 struct extent_buffer *eb;
4436 int done;
4437
4438 /*
4439 * Here we can't use find_extent_buffer(), as it may
4440 * try to lock eb->refs_lock, which is not safe in endio
4441 * context.
4442 */
4443 eb = find_extent_buffer_nolock(fs_info, cur_bytenr);
4444 ASSERT(eb);
4445
4446 cur_bytenr = eb->start + eb->len;
4447
4448 ASSERT(test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags));
4449 done = atomic_dec_and_test(&eb->io_pages);
4450 ASSERT(done);
4451
4452 if (bio->bi_status ||
4453 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
4454 ClearPageUptodate(page);
4455 set_btree_ioerr(page, eb);
4456 }
4457
4458 btrfs_subpage_clear_writeback(fs_info, page, eb->start,
4459 eb->len);
4460 end_extent_buffer_writeback(eb);
4461 /*
4462 * free_extent_buffer() will grab spinlock which is not
4463 * safe in endio context. Thus here we manually dec
4464 * the ref.
4465 */
4466 atomic_dec(&eb->refs);
4467 }
4468 }
4469 bio_put(bio);
4470}
4471
4246a0b6 4472static void end_bio_extent_buffer_writepage(struct bio *bio)
0b32f4bb 4473{
2c30c71b 4474 struct bio_vec *bvec;
0b32f4bb 4475 struct extent_buffer *eb;
2b070cfe 4476 int done;
6dc4f100 4477 struct bvec_iter_all iter_all;
0b32f4bb 4478
c09abff8 4479 ASSERT(!bio_flagged(bio, BIO_CLONED));
2b070cfe 4480 bio_for_each_segment_all(bvec, bio, iter_all) {
0b32f4bb
JB
4481 struct page *page = bvec->bv_page;
4482
0b32f4bb
JB
4483 eb = (struct extent_buffer *)page->private;
4484 BUG_ON(!eb);
4485 done = atomic_dec_and_test(&eb->io_pages);
4486
4e4cbee9 4487 if (bio->bi_status ||
4246a0b6 4488 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
0b32f4bb 4489 ClearPageUptodate(page);
5a2c6075 4490 set_btree_ioerr(page, eb);
0b32f4bb
JB
4491 }
4492
4493 end_page_writeback(page);
4494
4495 if (!done)
4496 continue;
4497
4498 end_extent_buffer_writeback(eb);
2c30c71b 4499 }
0b32f4bb
JB
4500
4501 bio_put(bio);
0b32f4bb
JB
4502}
4503
fa04c165
QW
4504static void prepare_eb_write(struct extent_buffer *eb)
4505{
4506 u32 nritems;
4507 unsigned long start;
4508 unsigned long end;
4509
4510 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
4511 atomic_set(&eb->io_pages, num_extent_pages(eb));
4512
4513 /* Set btree blocks beyond nritems with 0 to avoid stale content */
4514 nritems = btrfs_header_nritems(eb);
4515 if (btrfs_header_level(eb) > 0) {
4516 end = btrfs_node_key_ptr_offset(nritems);
4517 memzero_extent_buffer(eb, end, eb->len - end);
4518 } else {
4519 /*
4520 * Leaf:
4521 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
4522 */
4523 start = btrfs_item_nr_offset(nritems);
4524 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
4525 memzero_extent_buffer(eb, start, end - start);
4526 }
4527}
4528
35b6ddfa
QW
4529/*
4530 * Unlike the work in write_one_eb(), we rely completely on extent locking.
4531 * Page locking is only utilized at minimum to keep the VMM code happy.
35b6ddfa
QW
4532 */
4533static int write_one_subpage_eb(struct extent_buffer *eb,
4534 struct writeback_control *wbc,
4535 struct extent_page_data *epd)
4536{
4537 struct btrfs_fs_info *fs_info = eb->fs_info;
4538 struct page *page = eb->pages[0];
4539 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
4540 bool no_dirty_ebs = false;
4541 int ret;
4542
fa04c165
QW
4543 prepare_eb_write(eb);
4544
35b6ddfa
QW
4545 /* clear_page_dirty_for_io() in subpage helper needs page locked */
4546 lock_page(page);
4547 btrfs_subpage_set_writeback(fs_info, page, eb->start, eb->len);
4548
4549 /* Check if this is the last dirty bit to update nr_written */
4550 no_dirty_ebs = btrfs_subpage_clear_and_test_dirty(fs_info, page,
4551 eb->start, eb->len);
4552 if (no_dirty_ebs)
4553 clear_page_dirty_for_io(page);
4554
390ed29b
QW
4555 ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
4556 &epd->bio_ctrl, page, eb->start, eb->len,
4557 eb->start - page_offset(page),
fa04c165 4558 end_bio_subpage_eb_writepage, 0, 0, false);
35b6ddfa
QW
4559 if (ret) {
4560 btrfs_subpage_clear_writeback(fs_info, page, eb->start, eb->len);
4561 set_btree_ioerr(page, eb);
4562 unlock_page(page);
4563
4564 if (atomic_dec_and_test(&eb->io_pages))
4565 end_extent_buffer_writeback(eb);
4566 return -EIO;
4567 }
4568 unlock_page(page);
4569 /*
4570 * Submission finished without problem, if no range of the page is
4571 * dirty anymore, we have submitted a page. Update nr_written in wbc.
4572 */
4573 if (no_dirty_ebs)
4574 update_nr_written(wbc, 1);
4575 return ret;
4576}
4577
0e378df1 4578static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
0b32f4bb
JB
4579 struct writeback_control *wbc,
4580 struct extent_page_data *epd)
4581{
0c64c33c 4582 u64 disk_bytenr = eb->start;
cc5e31a4 4583 int i, num_pages;
ff40adf7 4584 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
d7dbe9e7 4585 int ret = 0;
0b32f4bb 4586
fa04c165 4587 prepare_eb_write(eb);
35b6ddfa 4588
fa04c165 4589 num_pages = num_extent_pages(eb);
0b32f4bb 4590 for (i = 0; i < num_pages; i++) {
fb85fc9a 4591 struct page *p = eb->pages[i];
0b32f4bb
JB
4592
4593 clear_page_dirty_for_io(p);
4594 set_page_writeback(p);
0ceb34bf 4595 ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
390ed29b
QW
4596 &epd->bio_ctrl, p, disk_bytenr,
4597 PAGE_SIZE, 0,
1f7ad75b 4598 end_bio_extent_buffer_writepage,
390ed29b 4599 0, 0, false);
0b32f4bb 4600 if (ret) {
5a2c6075 4601 set_btree_ioerr(p, eb);
fe01aa65
TK
4602 if (PageWriteback(p))
4603 end_page_writeback(p);
0b32f4bb
JB
4604 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
4605 end_extent_buffer_writeback(eb);
4606 ret = -EIO;
4607 break;
4608 }
0c64c33c 4609 disk_bytenr += PAGE_SIZE;
3d4b9496 4610 update_nr_written(wbc, 1);
0b32f4bb
JB
4611 unlock_page(p);
4612 }
4613
4614 if (unlikely(ret)) {
4615 for (; i < num_pages; i++) {
bbf65cf0 4616 struct page *p = eb->pages[i];
81465028 4617 clear_page_dirty_for_io(p);
0b32f4bb
JB
4618 unlock_page(p);
4619 }
4620 }
4621
4622 return ret;
4623}
4624
c4aec299
QW
4625/*
4626 * Submit one subpage btree page.
4627 *
4628 * The main difference to submit_eb_page() is:
4629 * - Page locking
4630 * For subpage, we don't rely on page locking at all.
4631 *
4632 * - Flush write bio
4633 * We only flush bio if we may be unable to fit current extent buffers into
4634 * current bio.
4635 *
4636 * Return >=0 for the number of submitted extent buffers.
4637 * Return <0 for fatal error.
4638 */
4639static int submit_eb_subpage(struct page *page,
4640 struct writeback_control *wbc,
4641 struct extent_page_data *epd)
4642{
4643 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
4644 int submitted = 0;
4645 u64 page_start = page_offset(page);
4646 int bit_start = 0;
c4aec299
QW
4647 int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits;
4648 int ret;
4649
4650 /* Lock and write each dirty extent buffers in the range */
72a69cd0 4651 while (bit_start < fs_info->subpage_info->bitmap_nr_bits) {
c4aec299
QW
4652 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
4653 struct extent_buffer *eb;
4654 unsigned long flags;
4655 u64 start;
4656
4657 /*
4658 * Take private lock to ensure the subpage won't be detached
4659 * in the meantime.
4660 */
4661 spin_lock(&page->mapping->private_lock);
4662 if (!PagePrivate(page)) {
4663 spin_unlock(&page->mapping->private_lock);
4664 break;
4665 }
4666 spin_lock_irqsave(&subpage->lock, flags);
72a69cd0
QW
4667 if (!test_bit(bit_start + fs_info->subpage_info->dirty_offset,
4668 subpage->bitmaps)) {
c4aec299
QW
4669 spin_unlock_irqrestore(&subpage->lock, flags);
4670 spin_unlock(&page->mapping->private_lock);
4671 bit_start++;
4672 continue;
4673 }
4674
4675 start = page_start + bit_start * fs_info->sectorsize;
4676 bit_start += sectors_per_node;
4677
4678 /*
4679 * Here we just want to grab the eb without touching extra
4680 * spin locks, so call find_extent_buffer_nolock().
4681 */
4682 eb = find_extent_buffer_nolock(fs_info, start);
4683 spin_unlock_irqrestore(&subpage->lock, flags);
4684 spin_unlock(&page->mapping->private_lock);
4685
4686 /*
4687 * The eb has already reached 0 refs thus find_extent_buffer()
4688 * doesn't return it. We don't need to write back such eb
4689 * anyway.
4690 */
4691 if (!eb)
4692 continue;
4693
4694 ret = lock_extent_buffer_for_io(eb, epd);
4695 if (ret == 0) {
4696 free_extent_buffer(eb);
4697 continue;
4698 }
4699 if (ret < 0) {
4700 free_extent_buffer(eb);
4701 goto cleanup;
4702 }
fa04c165 4703 ret = write_one_subpage_eb(eb, wbc, epd);
c4aec299
QW
4704 free_extent_buffer(eb);
4705 if (ret < 0)
4706 goto cleanup;
4707 submitted++;
4708 }
4709 return submitted;
4710
4711cleanup:
4712 /* We hit error, end bio for the submitted extent buffers */
4713 end_write_bio(epd, ret);
4714 return ret;
4715}
4716
f91e0d0c
QW
4717/*
4718 * Submit all page(s) of one extent buffer.
4719 *
4720 * @page: the page of one extent buffer
4721 * @eb_context: to determine if we need to submit this page, if current page
4722 * belongs to this eb, we don't need to submit
4723 *
4724 * The caller should pass each page in their bytenr order, and here we use
4725 * @eb_context to determine if we have submitted pages of one extent buffer.
4726 *
4727 * If we have, we just skip until we hit a new page that doesn't belong to
4728 * current @eb_context.
4729 *
4730 * If not, we submit all the page(s) of the extent buffer.
4731 *
4732 * Return >0 if we have submitted the extent buffer successfully.
4733 * Return 0 if we don't need to submit the page, as it's already submitted by
4734 * previous call.
4735 * Return <0 for fatal error.
4736 */
4737static int submit_eb_page(struct page *page, struct writeback_control *wbc,
4738 struct extent_page_data *epd,
4739 struct extent_buffer **eb_context)
4740{
4741 struct address_space *mapping = page->mapping;
0bc09ca1 4742 struct btrfs_block_group *cache = NULL;
f91e0d0c
QW
4743 struct extent_buffer *eb;
4744 int ret;
4745
4746 if (!PagePrivate(page))
4747 return 0;
4748
c4aec299
QW
4749 if (btrfs_sb(page->mapping->host->i_sb)->sectorsize < PAGE_SIZE)
4750 return submit_eb_subpage(page, wbc, epd);
4751
f91e0d0c
QW
4752 spin_lock(&mapping->private_lock);
4753 if (!PagePrivate(page)) {
4754 spin_unlock(&mapping->private_lock);
4755 return 0;
4756 }
4757
4758 eb = (struct extent_buffer *)page->private;
4759
4760 /*
4761 * Shouldn't happen and normally this would be a BUG_ON but no point
4762 * crashing the machine for something we can survive anyway.
4763 */
4764 if (WARN_ON(!eb)) {
4765 spin_unlock(&mapping->private_lock);
4766 return 0;
4767 }
4768
4769 if (eb == *eb_context) {
4770 spin_unlock(&mapping->private_lock);
4771 return 0;
4772 }
4773 ret = atomic_inc_not_zero(&eb->refs);
4774 spin_unlock(&mapping->private_lock);
4775 if (!ret)
4776 return 0;
4777
0bc09ca1
NA
4778 if (!btrfs_check_meta_write_pointer(eb->fs_info, eb, &cache)) {
4779 /*
4780 * If for_sync, this hole will be filled with
4781 * trasnsaction commit.
4782 */
4783 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
4784 ret = -EAGAIN;
4785 else
4786 ret = 0;
4787 free_extent_buffer(eb);
4788 return ret;
4789 }
4790
f91e0d0c
QW
4791 *eb_context = eb;
4792
4793 ret = lock_extent_buffer_for_io(eb, epd);
4794 if (ret <= 0) {
0bc09ca1
NA
4795 btrfs_revert_meta_write_pointer(cache, eb);
4796 if (cache)
4797 btrfs_put_block_group(cache);
f91e0d0c
QW
4798 free_extent_buffer(eb);
4799 return ret;
4800 }
be1a1d7a 4801 if (cache) {
d3e29967
NB
4802 /*
4803 * Implies write in zoned mode. Mark the last eb in a block group.
4804 */
be1a1d7a
NA
4805 if (cache->seq_zone && eb->start + eb->len == cache->zone_capacity)
4806 set_bit(EXTENT_BUFFER_ZONE_FINISH, &eb->bflags);
d3e29967 4807 btrfs_put_block_group(cache);
be1a1d7a 4808 }
f91e0d0c
QW
4809 ret = write_one_eb(eb, wbc, epd);
4810 free_extent_buffer(eb);
4811 if (ret < 0)
4812 return ret;
4813 return 1;
4814}
4815
0b32f4bb
JB
4816int btree_write_cache_pages(struct address_space *mapping,
4817 struct writeback_control *wbc)
4818{
f91e0d0c 4819 struct extent_buffer *eb_context = NULL;
0b32f4bb 4820 struct extent_page_data epd = {
390ed29b 4821 .bio_ctrl = { 0 },
0b32f4bb
JB
4822 .extent_locked = 0,
4823 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
4824 };
b3ff8f1d 4825 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
0b32f4bb
JB
4826 int ret = 0;
4827 int done = 0;
4828 int nr_to_write_done = 0;
4829 struct pagevec pvec;
4830 int nr_pages;
4831 pgoff_t index;
4832 pgoff_t end; /* Inclusive */
4833 int scanned = 0;
10bbd235 4834 xa_mark_t tag;
0b32f4bb 4835
86679820 4836 pagevec_init(&pvec);
0b32f4bb
JB
4837 if (wbc->range_cyclic) {
4838 index = mapping->writeback_index; /* Start from prev offset */
4839 end = -1;
556755a8
JB
4840 /*
4841 * Start from the beginning does not need to cycle over the
4842 * range, mark it as scanned.
4843 */
4844 scanned = (index == 0);
0b32f4bb 4845 } else {
09cbfeaf
KS
4846 index = wbc->range_start >> PAGE_SHIFT;
4847 end = wbc->range_end >> PAGE_SHIFT;
0b32f4bb
JB
4848 scanned = 1;
4849 }
4850 if (wbc->sync_mode == WB_SYNC_ALL)
4851 tag = PAGECACHE_TAG_TOWRITE;
4852 else
4853 tag = PAGECACHE_TAG_DIRTY;
0bc09ca1 4854 btrfs_zoned_meta_io_lock(fs_info);
0b32f4bb
JB
4855retry:
4856 if (wbc->sync_mode == WB_SYNC_ALL)
4857 tag_pages_for_writeback(mapping, index, end);
4858 while (!done && !nr_to_write_done && (index <= end) &&
4006f437 4859 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
67fd707f 4860 tag))) {
0b32f4bb
JB
4861 unsigned i;
4862
0b32f4bb
JB
4863 for (i = 0; i < nr_pages; i++) {
4864 struct page *page = pvec.pages[i];
4865
f91e0d0c
QW
4866 ret = submit_eb_page(page, wbc, &epd, &eb_context);
4867 if (ret == 0)
0b32f4bb 4868 continue;
f91e0d0c 4869 if (ret < 0) {
0b32f4bb 4870 done = 1;
0b32f4bb
JB
4871 break;
4872 }
0b32f4bb
JB
4873
4874 /*
4875 * the filesystem may choose to bump up nr_to_write.
4876 * We have to make sure to honor the new nr_to_write
4877 * at any time
4878 */
4879 nr_to_write_done = wbc->nr_to_write <= 0;
4880 }
4881 pagevec_release(&pvec);
4882 cond_resched();
4883 }
4884 if (!scanned && !done) {
4885 /*
4886 * We hit the last page and there is more work to be done: wrap
4887 * back to the start of the file
4888 */
4889 scanned = 1;
4890 index = 0;
4891 goto retry;
4892 }
2b952eea
QW
4893 if (ret < 0) {
4894 end_write_bio(&epd, ret);
0bc09ca1 4895 goto out;
2b952eea 4896 }
b3ff8f1d
QW
4897 /*
4898 * If something went wrong, don't allow any metadata write bio to be
4899 * submitted.
4900 *
4901 * This would prevent use-after-free if we had dirty pages not
4902 * cleaned up, which can still happen by fuzzed images.
4903 *
4904 * - Bad extent tree
4905 * Allowing existing tree block to be allocated for other trees.
4906 *
4907 * - Log tree operations
4908 * Exiting tree blocks get allocated to log tree, bumps its
4909 * generation, then get cleaned in tree re-balance.
4910 * Such tree block will not be written back, since it's clean,
4911 * thus no WRITTEN flag set.
4912 * And after log writes back, this tree block is not traced by
4913 * any dirty extent_io_tree.
4914 *
4915 * - Offending tree block gets re-dirtied from its original owner
4916 * Since it has bumped generation, no WRITTEN flag, it can be
4917 * reused without COWing. This tree block will not be traced
4918 * by btrfs_transaction::dirty_pages.
4919 *
4920 * Now such dirty tree block will not be cleaned by any dirty
4921 * extent io tree. Thus we don't want to submit such wild eb
4922 * if the fs already has error.
4923 */
84961539 4924 if (!BTRFS_FS_ERROR(fs_info)) {
b3ff8f1d
QW
4925 ret = flush_write_bio(&epd);
4926 } else {
fbabd4a3 4927 ret = -EROFS;
b3ff8f1d
QW
4928 end_write_bio(&epd, ret);
4929 }
0bc09ca1
NA
4930out:
4931 btrfs_zoned_meta_io_unlock(fs_info);
0b32f4bb
JB
4932 return ret;
4933}
4934
d1310b2e 4935/**
3bed2da1
NB
4936 * Walk the list of dirty pages of the given address space and write all of them.
4937 *
d1310b2e 4938 * @mapping: address space structure to write
3bed2da1
NB
4939 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
4940 * @epd: holds context for the write, namely the bio
d1310b2e
CM
4941 *
4942 * If a page is already under I/O, write_cache_pages() skips it, even
4943 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
4944 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
4945 * and msync() need to guarantee that all the data which was dirty at the time
4946 * the call was made get new I/O started against them. If wbc->sync_mode is
4947 * WB_SYNC_ALL then we were called for data integrity and we must wait for
4948 * existing IO to complete.
4949 */
4242b64a 4950static int extent_write_cache_pages(struct address_space *mapping,
4bef0848 4951 struct writeback_control *wbc,
aab6e9ed 4952 struct extent_page_data *epd)
d1310b2e 4953{
7fd1a3f7 4954 struct inode *inode = mapping->host;
d1310b2e
CM
4955 int ret = 0;
4956 int done = 0;
f85d7d6c 4957 int nr_to_write_done = 0;
d1310b2e
CM
4958 struct pagevec pvec;
4959 int nr_pages;
4960 pgoff_t index;
4961 pgoff_t end; /* Inclusive */
a9132667
LB
4962 pgoff_t done_index;
4963 int range_whole = 0;
d1310b2e 4964 int scanned = 0;
10bbd235 4965 xa_mark_t tag;
d1310b2e 4966
7fd1a3f7
JB
4967 /*
4968 * We have to hold onto the inode so that ordered extents can do their
4969 * work when the IO finishes. The alternative to this is failing to add
4970 * an ordered extent if the igrab() fails there and that is a huge pain
4971 * to deal with, so instead just hold onto the inode throughout the
4972 * writepages operation. If it fails here we are freeing up the inode
4973 * anyway and we'd rather not waste our time writing out stuff that is
4974 * going to be truncated anyway.
4975 */
4976 if (!igrab(inode))
4977 return 0;
4978
86679820 4979 pagevec_init(&pvec);
d1310b2e
CM
4980 if (wbc->range_cyclic) {
4981 index = mapping->writeback_index; /* Start from prev offset */
4982 end = -1;
556755a8
JB
4983 /*
4984 * Start from the beginning does not need to cycle over the
4985 * range, mark it as scanned.
4986 */
4987 scanned = (index == 0);
d1310b2e 4988 } else {
09cbfeaf
KS
4989 index = wbc->range_start >> PAGE_SHIFT;
4990 end = wbc->range_end >> PAGE_SHIFT;
a9132667
LB
4991 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
4992 range_whole = 1;
d1310b2e
CM
4993 scanned = 1;
4994 }
3cd24c69
EL
4995
4996 /*
4997 * We do the tagged writepage as long as the snapshot flush bit is set
4998 * and we are the first one who do the filemap_flush() on this inode.
4999 *
5000 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
5001 * not race in and drop the bit.
5002 */
5003 if (range_whole && wbc->nr_to_write == LONG_MAX &&
5004 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
5005 &BTRFS_I(inode)->runtime_flags))
5006 wbc->tagged_writepages = 1;
5007
5008 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
f7aaa06b
JB
5009 tag = PAGECACHE_TAG_TOWRITE;
5010 else
5011 tag = PAGECACHE_TAG_DIRTY;
d1310b2e 5012retry:
3cd24c69 5013 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
f7aaa06b 5014 tag_pages_for_writeback(mapping, index, end);
a9132667 5015 done_index = index;
f85d7d6c 5016 while (!done && !nr_to_write_done && (index <= end) &&
67fd707f
JK
5017 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
5018 &index, end, tag))) {
d1310b2e
CM
5019 unsigned i;
5020
d1310b2e
CM
5021 for (i = 0; i < nr_pages; i++) {
5022 struct page *page = pvec.pages[i];
5023
f7bddf1e 5024 done_index = page->index + 1;
d1310b2e 5025 /*
b93b0163
MW
5026 * At this point we hold neither the i_pages lock nor
5027 * the page lock: the page may be truncated or
5028 * invalidated (changing page->mapping to NULL),
5029 * or even swizzled back from swapper_space to
5030 * tmpfs file mapping
d1310b2e 5031 */
c8f2f24b 5032 if (!trylock_page(page)) {
f4340622
QW
5033 ret = flush_write_bio(epd);
5034 BUG_ON(ret < 0);
c8f2f24b 5035 lock_page(page);
01d658f2 5036 }
d1310b2e
CM
5037
5038 if (unlikely(page->mapping != mapping)) {
5039 unlock_page(page);
5040 continue;
5041 }
5042
d2c3f4f6 5043 if (wbc->sync_mode != WB_SYNC_NONE) {
f4340622
QW
5044 if (PageWriteback(page)) {
5045 ret = flush_write_bio(epd);
5046 BUG_ON(ret < 0);
5047 }
d1310b2e 5048 wait_on_page_writeback(page);
d2c3f4f6 5049 }
d1310b2e
CM
5050
5051 if (PageWriteback(page) ||
5052 !clear_page_dirty_for_io(page)) {
5053 unlock_page(page);
5054 continue;
5055 }
5056
aab6e9ed 5057 ret = __extent_writepage(page, wbc, epd);
a9132667 5058 if (ret < 0) {
a9132667
LB
5059 done = 1;
5060 break;
5061 }
f85d7d6c
CM
5062
5063 /*
5064 * the filesystem may choose to bump up nr_to_write.
5065 * We have to make sure to honor the new nr_to_write
5066 * at any time
5067 */
5068 nr_to_write_done = wbc->nr_to_write <= 0;
d1310b2e
CM
5069 }
5070 pagevec_release(&pvec);
5071 cond_resched();
5072 }
894b36e3 5073 if (!scanned && !done) {
d1310b2e
CM
5074 /*
5075 * We hit the last page and there is more work to be done: wrap
5076 * back to the start of the file
5077 */
5078 scanned = 1;
5079 index = 0;
42ffb0bf
JB
5080
5081 /*
5082 * If we're looping we could run into a page that is locked by a
5083 * writer and that writer could be waiting on writeback for a
5084 * page in our current bio, and thus deadlock, so flush the
5085 * write bio here.
5086 */
5087 ret = flush_write_bio(epd);
5088 if (!ret)
5089 goto retry;
d1310b2e 5090 }
a9132667
LB
5091
5092 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
5093 mapping->writeback_index = done_index;
5094
7fd1a3f7 5095 btrfs_add_delayed_iput(inode);
894b36e3 5096 return ret;
d1310b2e 5097}
d1310b2e 5098
0a9b0e53 5099int extent_write_full_page(struct page *page, struct writeback_control *wbc)
d1310b2e
CM
5100{
5101 int ret;
d1310b2e 5102 struct extent_page_data epd = {
390ed29b 5103 .bio_ctrl = { 0 },
771ed689 5104 .extent_locked = 0,
ffbd517d 5105 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
d1310b2e 5106 };
d1310b2e 5107
d1310b2e 5108 ret = __extent_writepage(page, wbc, &epd);
3065976b
QW
5109 ASSERT(ret <= 0);
5110 if (ret < 0) {
5111 end_write_bio(&epd, ret);
5112 return ret;
5113 }
d1310b2e 5114
3065976b
QW
5115 ret = flush_write_bio(&epd);
5116 ASSERT(ret <= 0);
d1310b2e
CM
5117 return ret;
5118}
d1310b2e 5119
2bd0fc93
QW
5120/*
5121 * Submit the pages in the range to bio for call sites which delalloc range has
5122 * already been ran (aka, ordered extent inserted) and all pages are still
5123 * locked.
5124 */
5125int extent_write_locked_range(struct inode *inode, u64 start, u64 end)
771ed689 5126{
2bd0fc93
QW
5127 bool found_error = false;
5128 int first_error = 0;
771ed689
CM
5129 int ret = 0;
5130 struct address_space *mapping = inode->i_mapping;
5131 struct page *page;
2bd0fc93 5132 u64 cur = start;
66448b9d
QW
5133 unsigned long nr_pages;
5134 const u32 sectorsize = btrfs_sb(inode->i_sb)->sectorsize;
771ed689 5135 struct extent_page_data epd = {
390ed29b 5136 .bio_ctrl = { 0 },
771ed689 5137 .extent_locked = 1,
2bd0fc93 5138 .sync_io = 1,
771ed689
CM
5139 };
5140 struct writeback_control wbc_writepages = {
2bd0fc93 5141 .sync_mode = WB_SYNC_ALL,
771ed689
CM
5142 .range_start = start,
5143 .range_end = end + 1,
ec39f769
CM
5144 /* We're called from an async helper function */
5145 .punt_to_cgroup = 1,
5146 .no_cgroup_owner = 1,
771ed689
CM
5147 };
5148
66448b9d
QW
5149 ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(end + 1, sectorsize));
5150 nr_pages = (round_up(end, PAGE_SIZE) - round_down(start, PAGE_SIZE)) >>
5151 PAGE_SHIFT;
5152 wbc_writepages.nr_to_write = nr_pages * 2;
5153
dbb70bec 5154 wbc_attach_fdatawrite_inode(&wbc_writepages, inode);
2bd0fc93 5155 while (cur <= end) {
66448b9d
QW
5156 u64 cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);
5157
2bd0fc93
QW
5158 page = find_get_page(mapping, cur >> PAGE_SHIFT);
5159 /*
5160 * All pages in the range are locked since
5161 * btrfs_run_delalloc_range(), thus there is no way to clear
5162 * the page dirty flag.
5163 */
66448b9d 5164 ASSERT(PageLocked(page));
2bd0fc93
QW
5165 ASSERT(PageDirty(page));
5166 clear_page_dirty_for_io(page);
5167 ret = __extent_writepage(page, &wbc_writepages, &epd);
5168 ASSERT(ret <= 0);
5169 if (ret < 0) {
5170 found_error = true;
5171 first_error = ret;
771ed689 5172 }
09cbfeaf 5173 put_page(page);
66448b9d 5174 cur = cur_end + 1;
771ed689
CM
5175 }
5176
2bd0fc93 5177 if (!found_error)
dbb70bec
CM
5178 ret = flush_write_bio(&epd);
5179 else
02c6db4f 5180 end_write_bio(&epd, ret);
dbb70bec
CM
5181
5182 wbc_detach_inode(&wbc_writepages);
2bd0fc93
QW
5183 if (found_error)
5184 return first_error;
771ed689
CM
5185 return ret;
5186}
d1310b2e 5187
8ae225a8 5188int extent_writepages(struct address_space *mapping,
d1310b2e
CM
5189 struct writeback_control *wbc)
5190{
35156d85 5191 struct inode *inode = mapping->host;
d1310b2e
CM
5192 int ret = 0;
5193 struct extent_page_data epd = {
390ed29b 5194 .bio_ctrl = { 0 },
771ed689 5195 .extent_locked = 0,
ffbd517d 5196 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
d1310b2e
CM
5197 };
5198
35156d85
JT
5199 /*
5200 * Allow only a single thread to do the reloc work in zoned mode to
5201 * protect the write pointer updates.
5202 */
869f4cdc 5203 btrfs_zoned_data_reloc_lock(BTRFS_I(inode));
935db853 5204 ret = extent_write_cache_pages(mapping, wbc, &epd);
869f4cdc 5205 btrfs_zoned_data_reloc_unlock(BTRFS_I(inode));
a2a72fbd
QW
5206 ASSERT(ret <= 0);
5207 if (ret < 0) {
5208 end_write_bio(&epd, ret);
5209 return ret;
5210 }
5211 ret = flush_write_bio(&epd);
d1310b2e
CM
5212 return ret;
5213}
d1310b2e 5214
ba206a02 5215void extent_readahead(struct readahead_control *rac)
d1310b2e 5216{
390ed29b 5217 struct btrfs_bio_ctrl bio_ctrl = { 0 };
67c9684f 5218 struct page *pagepool[16];
125bac01 5219 struct extent_map *em_cached = NULL;
808f80b4 5220 u64 prev_em_start = (u64)-1;
ba206a02 5221 int nr;
d1310b2e 5222
ba206a02 5223 while ((nr = readahead_page_batch(rac, pagepool))) {
32c0a6bc
MWO
5224 u64 contig_start = readahead_pos(rac);
5225 u64 contig_end = contig_start + readahead_batch_length(rac) - 1;
e65ef21e 5226
ba206a02 5227 contiguous_readpages(pagepool, nr, contig_start, contig_end,
390ed29b 5228 &em_cached, &bio_ctrl, &prev_em_start);
d1310b2e 5229 }
67c9684f 5230
125bac01
MX
5231 if (em_cached)
5232 free_extent_map(em_cached);
5233
390ed29b
QW
5234 if (bio_ctrl.bio) {
5235 if (submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags))
ba206a02
MWO
5236 return;
5237 }
d1310b2e 5238}
d1310b2e
CM
5239
5240/*
5241 * basic invalidatepage code, this waits on any locked or writeback
5242 * ranges corresponding to the page, and then deletes any extent state
5243 * records from the tree
5244 */
5245int extent_invalidatepage(struct extent_io_tree *tree,
5246 struct page *page, unsigned long offset)
5247{
2ac55d41 5248 struct extent_state *cached_state = NULL;
4eee4fa4 5249 u64 start = page_offset(page);
09cbfeaf 5250 u64 end = start + PAGE_SIZE - 1;
d1310b2e
CM
5251 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
5252
829ddec9
QW
5253 /* This function is only called for the btree inode */
5254 ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO);
5255
fda2832f 5256 start += ALIGN(offset, blocksize);
d1310b2e
CM
5257 if (start > end)
5258 return 0;
5259
ff13db41 5260 lock_extent_bits(tree, start, end, &cached_state);
1edbb734 5261 wait_on_page_writeback(page);
829ddec9
QW
5262
5263 /*
5264 * Currently for btree io tree, only EXTENT_LOCKED is utilized,
5265 * so here we only need to unlock the extent range to free any
5266 * existing extent state.
5267 */
5268 unlock_extent_cached(tree, start, end, &cached_state);
d1310b2e
CM
5269 return 0;
5270}
d1310b2e 5271
7b13b7b1
CM
5272/*
5273 * a helper for releasepage, this tests for areas of the page that
5274 * are locked or under IO and drops the related state bits if it is safe
5275 * to drop the page.
5276 */
29c68b2d 5277static int try_release_extent_state(struct extent_io_tree *tree,
48a3b636 5278 struct page *page, gfp_t mask)
7b13b7b1 5279{
4eee4fa4 5280 u64 start = page_offset(page);
09cbfeaf 5281 u64 end = start + PAGE_SIZE - 1;
7b13b7b1
CM
5282 int ret = 1;
5283
8882679e 5284 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
7b13b7b1 5285 ret = 0;
8882679e 5286 } else {
11ef160f 5287 /*
2766ff61
FM
5288 * At this point we can safely clear everything except the
5289 * locked bit, the nodatasum bit and the delalloc new bit.
5290 * The delalloc new bit will be cleared by ordered extent
5291 * completion.
11ef160f 5292 */
66b0c887 5293 ret = __clear_extent_bit(tree, start, end,
2766ff61
FM
5294 ~(EXTENT_LOCKED | EXTENT_NODATASUM | EXTENT_DELALLOC_NEW),
5295 0, 0, NULL, mask, NULL);
e3f24cc5
CM
5296
5297 /* if clear_extent_bit failed for enomem reasons,
5298 * we can't allow the release to continue.
5299 */
5300 if (ret < 0)
5301 ret = 0;
5302 else
5303 ret = 1;
7b13b7b1
CM
5304 }
5305 return ret;
5306}
7b13b7b1 5307
d1310b2e
CM
5308/*
5309 * a helper for releasepage. As long as there are no locked extents
5310 * in the range corresponding to the page, both state records and extent
5311 * map records are removed
5312 */
477a30ba 5313int try_release_extent_mapping(struct page *page, gfp_t mask)
d1310b2e
CM
5314{
5315 struct extent_map *em;
4eee4fa4 5316 u64 start = page_offset(page);
09cbfeaf 5317 u64 end = start + PAGE_SIZE - 1;
bd3599a0
FM
5318 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
5319 struct extent_io_tree *tree = &btrfs_inode->io_tree;
5320 struct extent_map_tree *map = &btrfs_inode->extent_tree;
7b13b7b1 5321
d0164adc 5322 if (gfpflags_allow_blocking(mask) &&
ee22184b 5323 page->mapping->host->i_size > SZ_16M) {
39b5637f 5324 u64 len;
70dec807 5325 while (start <= end) {
fbc2bd7e
FM
5326 struct btrfs_fs_info *fs_info;
5327 u64 cur_gen;
5328
39b5637f 5329 len = end - start + 1;
890871be 5330 write_lock(&map->lock);
39b5637f 5331 em = lookup_extent_mapping(map, start, len);
285190d9 5332 if (!em) {
890871be 5333 write_unlock(&map->lock);
70dec807
CM
5334 break;
5335 }
7f3c74fb
CM
5336 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
5337 em->start != start) {
890871be 5338 write_unlock(&map->lock);
70dec807
CM
5339 free_extent_map(em);
5340 break;
5341 }
3d6448e6
FM
5342 if (test_range_bit(tree, em->start,
5343 extent_map_end(em) - 1,
5344 EXTENT_LOCKED, 0, NULL))
5345 goto next;
5346 /*
5347 * If it's not in the list of modified extents, used
5348 * by a fast fsync, we can remove it. If it's being
5349 * logged we can safely remove it since fsync took an
5350 * extra reference on the em.
5351 */
5352 if (list_empty(&em->list) ||
fbc2bd7e
FM
5353 test_bit(EXTENT_FLAG_LOGGING, &em->flags))
5354 goto remove_em;
5355 /*
5356 * If it's in the list of modified extents, remove it
5357 * only if its generation is older then the current one,
5358 * in which case we don't need it for a fast fsync.
5359 * Otherwise don't remove it, we could be racing with an
5360 * ongoing fast fsync that could miss the new extent.
5361 */
5362 fs_info = btrfs_inode->root->fs_info;
5363 spin_lock(&fs_info->trans_lock);
5364 cur_gen = fs_info->generation;
5365 spin_unlock(&fs_info->trans_lock);
5366 if (em->generation >= cur_gen)
5367 goto next;
5368remove_em:
5e548b32
FM
5369 /*
5370 * We only remove extent maps that are not in the list of
5371 * modified extents or that are in the list but with a
5372 * generation lower then the current generation, so there
5373 * is no need to set the full fsync flag on the inode (it
5374 * hurts the fsync performance for workloads with a data
5375 * size that exceeds or is close to the system's memory).
5376 */
fbc2bd7e
FM
5377 remove_extent_mapping(map, em);
5378 /* once for the rb tree */
5379 free_extent_map(em);
3d6448e6 5380next:
70dec807 5381 start = extent_map_end(em);
890871be 5382 write_unlock(&map->lock);
70dec807
CM
5383
5384 /* once for us */
d1310b2e 5385 free_extent_map(em);
9f47eb54
PM
5386
5387 cond_resched(); /* Allow large-extent preemption. */
d1310b2e 5388 }
d1310b2e 5389 }
29c68b2d 5390 return try_release_extent_state(tree, page, mask);
d1310b2e 5391}
d1310b2e 5392
ec29ed5b
CM
5393/*
5394 * helper function for fiemap, which doesn't want to see any holes.
5395 * This maps until we find something past 'last'
5396 */
f1bbde8d 5397static struct extent_map *get_extent_skip_holes(struct btrfs_inode *inode,
e3350e16 5398 u64 offset, u64 last)
ec29ed5b 5399{
f1bbde8d 5400 u64 sectorsize = btrfs_inode_sectorsize(inode);
ec29ed5b
CM
5401 struct extent_map *em;
5402 u64 len;
5403
5404 if (offset >= last)
5405 return NULL;
5406
67871254 5407 while (1) {
ec29ed5b
CM
5408 len = last - offset;
5409 if (len == 0)
5410 break;
fda2832f 5411 len = ALIGN(len, sectorsize);
f1bbde8d 5412 em = btrfs_get_extent_fiemap(inode, offset, len);
6b5b7a41 5413 if (IS_ERR(em))
ec29ed5b
CM
5414 return em;
5415
5416 /* if this isn't a hole return it */
4a2d25cd 5417 if (em->block_start != EXTENT_MAP_HOLE)
ec29ed5b 5418 return em;
ec29ed5b
CM
5419
5420 /* this is a hole, advance to the next extent */
5421 offset = extent_map_end(em);
5422 free_extent_map(em);
5423 if (offset >= last)
5424 break;
5425 }
5426 return NULL;
5427}
5428
4751832d
QW
5429/*
5430 * To cache previous fiemap extent
5431 *
5432 * Will be used for merging fiemap extent
5433 */
5434struct fiemap_cache {
5435 u64 offset;
5436 u64 phys;
5437 u64 len;
5438 u32 flags;
5439 bool cached;
5440};
5441
5442/*
5443 * Helper to submit fiemap extent.
5444 *
5445 * Will try to merge current fiemap extent specified by @offset, @phys,
5446 * @len and @flags with cached one.
5447 * And only when we fails to merge, cached one will be submitted as
5448 * fiemap extent.
5449 *
5450 * Return value is the same as fiemap_fill_next_extent().
5451 */
5452static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
5453 struct fiemap_cache *cache,
5454 u64 offset, u64 phys, u64 len, u32 flags)
5455{
5456 int ret = 0;
5457
5458 if (!cache->cached)
5459 goto assign;
5460
5461 /*
5462 * Sanity check, extent_fiemap() should have ensured that new
52042d8e 5463 * fiemap extent won't overlap with cached one.
4751832d
QW
5464 * Not recoverable.
5465 *
5466 * NOTE: Physical address can overlap, due to compression
5467 */
5468 if (cache->offset + cache->len > offset) {
5469 WARN_ON(1);
5470 return -EINVAL;
5471 }
5472
5473 /*
5474 * Only merges fiemap extents if
5475 * 1) Their logical addresses are continuous
5476 *
5477 * 2) Their physical addresses are continuous
5478 * So truly compressed (physical size smaller than logical size)
5479 * extents won't get merged with each other
5480 *
5481 * 3) Share same flags except FIEMAP_EXTENT_LAST
5482 * So regular extent won't get merged with prealloc extent
5483 */
5484 if (cache->offset + cache->len == offset &&
5485 cache->phys + cache->len == phys &&
5486 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
5487 (flags & ~FIEMAP_EXTENT_LAST)) {
5488 cache->len += len;
5489 cache->flags |= flags;
5490 goto try_submit_last;
5491 }
5492
5493 /* Not mergeable, need to submit cached one */
5494 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
5495 cache->len, cache->flags);
5496 cache->cached = false;
5497 if (ret)
5498 return ret;
5499assign:
5500 cache->cached = true;
5501 cache->offset = offset;
5502 cache->phys = phys;
5503 cache->len = len;
5504 cache->flags = flags;
5505try_submit_last:
5506 if (cache->flags & FIEMAP_EXTENT_LAST) {
5507 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
5508 cache->phys, cache->len, cache->flags);
5509 cache->cached = false;
5510 }
5511 return ret;
5512}
5513
5514/*
848c23b7 5515 * Emit last fiemap cache
4751832d 5516 *
848c23b7
QW
5517 * The last fiemap cache may still be cached in the following case:
5518 * 0 4k 8k
5519 * |<- Fiemap range ->|
5520 * |<------------ First extent ----------->|
5521 *
5522 * In this case, the first extent range will be cached but not emitted.
5523 * So we must emit it before ending extent_fiemap().
4751832d 5524 */
5c5aff98 5525static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
848c23b7 5526 struct fiemap_cache *cache)
4751832d
QW
5527{
5528 int ret;
5529
5530 if (!cache->cached)
5531 return 0;
5532
4751832d
QW
5533 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
5534 cache->len, cache->flags);
5535 cache->cached = false;
5536 if (ret > 0)
5537 ret = 0;
5538 return ret;
5539}
5540
facee0a0 5541int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo,
bab16e21 5542 u64 start, u64 len)
1506fcc8 5543{
975f84fe 5544 int ret = 0;
15c7745c 5545 u64 off;
1506fcc8
YS
5546 u64 max = start + len;
5547 u32 flags = 0;
975f84fe
JB
5548 u32 found_type;
5549 u64 last;
ec29ed5b 5550 u64 last_for_get_extent = 0;
1506fcc8 5551 u64 disko = 0;
facee0a0 5552 u64 isize = i_size_read(&inode->vfs_inode);
975f84fe 5553 struct btrfs_key found_key;
1506fcc8 5554 struct extent_map *em = NULL;
2ac55d41 5555 struct extent_state *cached_state = NULL;
975f84fe 5556 struct btrfs_path *path;
facee0a0 5557 struct btrfs_root *root = inode->root;
4751832d 5558 struct fiemap_cache cache = { 0 };
5911c8fe
DS
5559 struct ulist *roots;
5560 struct ulist *tmp_ulist;
1506fcc8 5561 int end = 0;
ec29ed5b
CM
5562 u64 em_start = 0;
5563 u64 em_len = 0;
5564 u64 em_end = 0;
1506fcc8
YS
5565
5566 if (len == 0)
5567 return -EINVAL;
5568
975f84fe
JB
5569 path = btrfs_alloc_path();
5570 if (!path)
5571 return -ENOMEM;
975f84fe 5572
5911c8fe
DS
5573 roots = ulist_alloc(GFP_KERNEL);
5574 tmp_ulist = ulist_alloc(GFP_KERNEL);
5575 if (!roots || !tmp_ulist) {
5576 ret = -ENOMEM;
5577 goto out_free_ulist;
5578 }
5579
15c7745c
BB
5580 /*
5581 * We can't initialize that to 'start' as this could miss extents due
5582 * to extent item merging
5583 */
5584 off = 0;
facee0a0
NB
5585 start = round_down(start, btrfs_inode_sectorsize(inode));
5586 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
4d479cf0 5587
ec29ed5b
CM
5588 /*
5589 * lookup the last file extent. We're not using i_size here
5590 * because there might be preallocation past i_size
5591 */
facee0a0
NB
5592 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
5593 0);
975f84fe 5594 if (ret < 0) {
5911c8fe 5595 goto out_free_ulist;
2d324f59
LB
5596 } else {
5597 WARN_ON(!ret);
5598 if (ret == 1)
5599 ret = 0;
975f84fe 5600 }
2d324f59 5601
975f84fe 5602 path->slots[0]--;
975f84fe 5603 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
962a298f 5604 found_type = found_key.type;
975f84fe 5605
ec29ed5b 5606 /* No extents, but there might be delalloc bits */
facee0a0 5607 if (found_key.objectid != btrfs_ino(inode) ||
975f84fe 5608 found_type != BTRFS_EXTENT_DATA_KEY) {
ec29ed5b
CM
5609 /* have to trust i_size as the end */
5610 last = (u64)-1;
5611 last_for_get_extent = isize;
5612 } else {
5613 /*
5614 * remember the start of the last extent. There are a
5615 * bunch of different factors that go into the length of the
5616 * extent, so its much less complex to remember where it started
5617 */
5618 last = found_key.offset;
5619 last_for_get_extent = last + 1;
975f84fe 5620 }
fe09e16c 5621 btrfs_release_path(path);
975f84fe 5622
ec29ed5b
CM
5623 /*
5624 * we might have some extents allocated but more delalloc past those
5625 * extents. so, we trust isize unless the start of the last extent is
5626 * beyond isize
5627 */
5628 if (last < isize) {
5629 last = (u64)-1;
5630 last_for_get_extent = isize;
5631 }
5632
facee0a0 5633 lock_extent_bits(&inode->io_tree, start, start + len - 1,
d0082371 5634 &cached_state);
ec29ed5b 5635
facee0a0 5636 em = get_extent_skip_holes(inode, start, last_for_get_extent);
1506fcc8
YS
5637 if (!em)
5638 goto out;
5639 if (IS_ERR(em)) {
5640 ret = PTR_ERR(em);
5641 goto out;
5642 }
975f84fe 5643
1506fcc8 5644 while (!end) {
b76bb701 5645 u64 offset_in_extent = 0;
ea8efc74
CM
5646
5647 /* break if the extent we found is outside the range */
5648 if (em->start >= max || extent_map_end(em) < off)
5649 break;
5650
5651 /*
5652 * get_extent may return an extent that starts before our
5653 * requested range. We have to make sure the ranges
5654 * we return to fiemap always move forward and don't
5655 * overlap, so adjust the offsets here
5656 */
5657 em_start = max(em->start, off);
1506fcc8 5658
ea8efc74
CM
5659 /*
5660 * record the offset from the start of the extent
b76bb701
JB
5661 * for adjusting the disk offset below. Only do this if the
5662 * extent isn't compressed since our in ram offset may be past
5663 * what we have actually allocated on disk.
ea8efc74 5664 */
b76bb701
JB
5665 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
5666 offset_in_extent = em_start - em->start;
ec29ed5b 5667 em_end = extent_map_end(em);
ea8efc74 5668 em_len = em_end - em_start;
1506fcc8 5669 flags = 0;
f0986318
FM
5670 if (em->block_start < EXTENT_MAP_LAST_BYTE)
5671 disko = em->block_start + offset_in_extent;
5672 else
5673 disko = 0;
1506fcc8 5674
ea8efc74
CM
5675 /*
5676 * bump off for our next call to get_extent
5677 */
5678 off = extent_map_end(em);
5679 if (off >= max)
5680 end = 1;
5681
93dbfad7 5682 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
1506fcc8
YS
5683 end = 1;
5684 flags |= FIEMAP_EXTENT_LAST;
93dbfad7 5685 } else if (em->block_start == EXTENT_MAP_INLINE) {
1506fcc8
YS
5686 flags |= (FIEMAP_EXTENT_DATA_INLINE |
5687 FIEMAP_EXTENT_NOT_ALIGNED);
93dbfad7 5688 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
1506fcc8
YS
5689 flags |= (FIEMAP_EXTENT_DELALLOC |
5690 FIEMAP_EXTENT_UNKNOWN);
dc046b10
JB
5691 } else if (fieinfo->fi_extents_max) {
5692 u64 bytenr = em->block_start -
5693 (em->start - em->orig_start);
fe09e16c 5694
fe09e16c
LB
5695 /*
5696 * As btrfs supports shared space, this information
5697 * can be exported to userspace tools via
dc046b10
JB
5698 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
5699 * then we're just getting a count and we can skip the
5700 * lookup stuff.
fe09e16c 5701 */
facee0a0 5702 ret = btrfs_check_shared(root, btrfs_ino(inode),
5911c8fe 5703 bytenr, roots, tmp_ulist);
dc046b10 5704 if (ret < 0)
fe09e16c 5705 goto out_free;
dc046b10 5706 if (ret)
fe09e16c 5707 flags |= FIEMAP_EXTENT_SHARED;
dc046b10 5708 ret = 0;
1506fcc8
YS
5709 }
5710 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
5711 flags |= FIEMAP_EXTENT_ENCODED;
0d2b2372
JB
5712 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5713 flags |= FIEMAP_EXTENT_UNWRITTEN;
1506fcc8 5714
1506fcc8
YS
5715 free_extent_map(em);
5716 em = NULL;
ec29ed5b
CM
5717 if ((em_start >= last) || em_len == (u64)-1 ||
5718 (last == (u64)-1 && isize <= em_end)) {
1506fcc8
YS
5719 flags |= FIEMAP_EXTENT_LAST;
5720 end = 1;
5721 }
5722
ec29ed5b 5723 /* now scan forward to see if this is really the last extent. */
facee0a0 5724 em = get_extent_skip_holes(inode, off, last_for_get_extent);
ec29ed5b
CM
5725 if (IS_ERR(em)) {
5726 ret = PTR_ERR(em);
5727 goto out;
5728 }
5729 if (!em) {
975f84fe
JB
5730 flags |= FIEMAP_EXTENT_LAST;
5731 end = 1;
5732 }
4751832d
QW
5733 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
5734 em_len, flags);
26e726af
CS
5735 if (ret) {
5736 if (ret == 1)
5737 ret = 0;
ec29ed5b 5738 goto out_free;
26e726af 5739 }
1506fcc8
YS
5740 }
5741out_free:
4751832d 5742 if (!ret)
5c5aff98 5743 ret = emit_last_fiemap_cache(fieinfo, &cache);
1506fcc8
YS
5744 free_extent_map(em);
5745out:
facee0a0 5746 unlock_extent_cached(&inode->io_tree, start, start + len - 1,
e43bbe5e 5747 &cached_state);
5911c8fe
DS
5748
5749out_free_ulist:
e02d48ea 5750 btrfs_free_path(path);
5911c8fe
DS
5751 ulist_free(roots);
5752 ulist_free(tmp_ulist);
1506fcc8
YS
5753 return ret;
5754}
5755
727011e0
CM
5756static void __free_extent_buffer(struct extent_buffer *eb)
5757{
727011e0
CM
5758 kmem_cache_free(extent_buffer_cache, eb);
5759}
5760
2b48966a 5761int extent_buffer_under_io(const struct extent_buffer *eb)
db7f3436
JB
5762{
5763 return (atomic_read(&eb->io_pages) ||
5764 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
5765 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5766}
5767
8ff8466d 5768static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page)
db7f3436 5769{
8ff8466d 5770 struct btrfs_subpage *subpage;
db7f3436 5771
8ff8466d 5772 lockdep_assert_held(&page->mapping->private_lock);
db7f3436 5773
8ff8466d
QW
5774 if (PagePrivate(page)) {
5775 subpage = (struct btrfs_subpage *)page->private;
5776 if (atomic_read(&subpage->eb_refs))
5777 return true;
3d078efa
QW
5778 /*
5779 * Even there is no eb refs here, we may still have
5780 * end_page_read() call relying on page::private.
5781 */
5782 if (atomic_read(&subpage->readers))
5783 return true;
8ff8466d
QW
5784 }
5785 return false;
5786}
db7f3436 5787
8ff8466d
QW
5788static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
5789{
5790 struct btrfs_fs_info *fs_info = eb->fs_info;
5791 const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
5792
5793 /*
5794 * For mapped eb, we're going to change the page private, which should
5795 * be done under the private_lock.
5796 */
5797 if (mapped)
5798 spin_lock(&page->mapping->private_lock);
5799
5800 if (!PagePrivate(page)) {
5d2361db 5801 if (mapped)
8ff8466d
QW
5802 spin_unlock(&page->mapping->private_lock);
5803 return;
5804 }
5805
5806 if (fs_info->sectorsize == PAGE_SIZE) {
5d2361db
FL
5807 /*
5808 * We do this since we'll remove the pages after we've
5809 * removed the eb from the radix tree, so we could race
5810 * and have this page now attached to the new eb. So
5811 * only clear page_private if it's still connected to
5812 * this eb.
5813 */
5814 if (PagePrivate(page) &&
5815 page->private == (unsigned long)eb) {
5816 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5817 BUG_ON(PageDirty(page));
5818 BUG_ON(PageWriteback(page));
db7f3436 5819 /*
5d2361db
FL
5820 * We need to make sure we haven't be attached
5821 * to a new eb.
db7f3436 5822 */
d1b89bc0 5823 detach_page_private(page);
db7f3436 5824 }
5d2361db
FL
5825 if (mapped)
5826 spin_unlock(&page->mapping->private_lock);
8ff8466d
QW
5827 return;
5828 }
5829
5830 /*
5831 * For subpage, we can have dummy eb with page private. In this case,
5832 * we can directly detach the private as such page is only attached to
5833 * one dummy eb, no sharing.
5834 */
5835 if (!mapped) {
5836 btrfs_detach_subpage(fs_info, page);
5837 return;
5838 }
5839
5840 btrfs_page_dec_eb_refs(fs_info, page);
5841
5842 /*
5843 * We can only detach the page private if there are no other ebs in the
3d078efa 5844 * page range and no unfinished IO.
8ff8466d
QW
5845 */
5846 if (!page_range_has_eb(fs_info, page))
5847 btrfs_detach_subpage(fs_info, page);
5848
5849 spin_unlock(&page->mapping->private_lock);
5850}
5851
5852/* Release all pages attached to the extent buffer */
5853static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
5854{
5855 int i;
5856 int num_pages;
5857
5858 ASSERT(!extent_buffer_under_io(eb));
5859
5860 num_pages = num_extent_pages(eb);
5861 for (i = 0; i < num_pages; i++) {
5862 struct page *page = eb->pages[i];
5863
5864 if (!page)
5865 continue;
5866
5867 detach_extent_buffer_page(eb, page);
5d2361db 5868
01327610 5869 /* One for when we allocated the page */
09cbfeaf 5870 put_page(page);
d64766fd 5871 }
db7f3436
JB
5872}
5873
5874/*
5875 * Helper for releasing the extent buffer.
5876 */
5877static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
5878{
55ac0139 5879 btrfs_release_extent_buffer_pages(eb);
8c38938c 5880 btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
db7f3436
JB
5881 __free_extent_buffer(eb);
5882}
5883
f28491e0
JB
5884static struct extent_buffer *
5885__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
23d79d81 5886 unsigned long len)
d1310b2e
CM
5887{
5888 struct extent_buffer *eb = NULL;
5889
d1b5c567 5890 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
d1310b2e
CM
5891 eb->start = start;
5892 eb->len = len;
f28491e0 5893 eb->fs_info = fs_info;
815a51c7 5894 eb->bflags = 0;
196d59ab 5895 init_rwsem(&eb->lock);
b4ce94de 5896
3fd63727
JB
5897 btrfs_leak_debug_add(&fs_info->eb_leak_lock, &eb->leak_list,
5898 &fs_info->allocated_ebs);
d3575156 5899 INIT_LIST_HEAD(&eb->release_list);
6d49ba1b 5900
3083ee2e 5901 spin_lock_init(&eb->refs_lock);
d1310b2e 5902 atomic_set(&eb->refs, 1);
0b32f4bb 5903 atomic_set(&eb->io_pages, 0);
727011e0 5904
deb67895 5905 ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE);
d1310b2e
CM
5906
5907 return eb;
5908}
5909
2b48966a 5910struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
815a51c7 5911{
cc5e31a4 5912 int i;
815a51c7
JS
5913 struct page *p;
5914 struct extent_buffer *new;
cc5e31a4 5915 int num_pages = num_extent_pages(src);
815a51c7 5916
3f556f78 5917 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
815a51c7
JS
5918 if (new == NULL)
5919 return NULL;
5920
62c053fb
QW
5921 /*
5922 * Set UNMAPPED before calling btrfs_release_extent_buffer(), as
5923 * btrfs_release_extent_buffer() have different behavior for
5924 * UNMAPPED subpage extent buffer.
5925 */
5926 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
5927
815a51c7 5928 for (i = 0; i < num_pages; i++) {
760f991f
QW
5929 int ret;
5930
9ec72677 5931 p = alloc_page(GFP_NOFS);
db7f3436
JB
5932 if (!p) {
5933 btrfs_release_extent_buffer(new);
5934 return NULL;
5935 }
760f991f
QW
5936 ret = attach_extent_buffer_page(new, p, NULL);
5937 if (ret < 0) {
5938 put_page(p);
5939 btrfs_release_extent_buffer(new);
5940 return NULL;
5941 }
815a51c7 5942 WARN_ON(PageDirty(p));
815a51c7 5943 new->pages[i] = p;
fba1acf9 5944 copy_page(page_address(p), page_address(src->pages[i]));
815a51c7 5945 }
92d83e94 5946 set_extent_buffer_uptodate(new);
815a51c7
JS
5947
5948 return new;
5949}
5950
0f331229
OS
5951struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
5952 u64 start, unsigned long len)
815a51c7
JS
5953{
5954 struct extent_buffer *eb;
cc5e31a4
DS
5955 int num_pages;
5956 int i;
815a51c7 5957
3f556f78 5958 eb = __alloc_extent_buffer(fs_info, start, len);
815a51c7
JS
5959 if (!eb)
5960 return NULL;
5961
65ad0104 5962 num_pages = num_extent_pages(eb);
815a51c7 5963 for (i = 0; i < num_pages; i++) {
09bc1f0f
QW
5964 int ret;
5965
9ec72677 5966 eb->pages[i] = alloc_page(GFP_NOFS);
815a51c7
JS
5967 if (!eb->pages[i])
5968 goto err;
09bc1f0f
QW
5969 ret = attach_extent_buffer_page(eb, eb->pages[i], NULL);
5970 if (ret < 0)
5971 goto err;
815a51c7
JS
5972 }
5973 set_extent_buffer_uptodate(eb);
5974 btrfs_set_header_nritems(eb, 0);
b0132a3b 5975 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
815a51c7
JS
5976
5977 return eb;
5978err:
09bc1f0f
QW
5979 for (; i > 0; i--) {
5980 detach_extent_buffer_page(eb, eb->pages[i - 1]);
84167d19 5981 __free_page(eb->pages[i - 1]);
09bc1f0f 5982 }
815a51c7
JS
5983 __free_extent_buffer(eb);
5984 return NULL;
5985}
5986
0f331229 5987struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
da17066c 5988 u64 start)
0f331229 5989{
da17066c 5990 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
0f331229
OS
5991}
5992
0b32f4bb
JB
5993static void check_buffer_tree_ref(struct extent_buffer *eb)
5994{
242e18c7 5995 int refs;
6bf9cd2e
BB
5996 /*
5997 * The TREE_REF bit is first set when the extent_buffer is added
5998 * to the radix tree. It is also reset, if unset, when a new reference
5999 * is created by find_extent_buffer.
0b32f4bb 6000 *
6bf9cd2e
BB
6001 * It is only cleared in two cases: freeing the last non-tree
6002 * reference to the extent_buffer when its STALE bit is set or
6003 * calling releasepage when the tree reference is the only reference.
0b32f4bb 6004 *
6bf9cd2e
BB
6005 * In both cases, care is taken to ensure that the extent_buffer's
6006 * pages are not under io. However, releasepage can be concurrently
6007 * called with creating new references, which is prone to race
6008 * conditions between the calls to check_buffer_tree_ref in those
6009 * codepaths and clearing TREE_REF in try_release_extent_buffer.
0b32f4bb 6010 *
6bf9cd2e
BB
6011 * The actual lifetime of the extent_buffer in the radix tree is
6012 * adequately protected by the refcount, but the TREE_REF bit and
6013 * its corresponding reference are not. To protect against this
6014 * class of races, we call check_buffer_tree_ref from the codepaths
6015 * which trigger io after they set eb->io_pages. Note that once io is
6016 * initiated, TREE_REF can no longer be cleared, so that is the
6017 * moment at which any such race is best fixed.
0b32f4bb 6018 */
242e18c7
CM
6019 refs = atomic_read(&eb->refs);
6020 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6021 return;
6022
594831c4
JB
6023 spin_lock(&eb->refs_lock);
6024 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
0b32f4bb 6025 atomic_inc(&eb->refs);
594831c4 6026 spin_unlock(&eb->refs_lock);
0b32f4bb
JB
6027}
6028
2457aec6
MG
6029static void mark_extent_buffer_accessed(struct extent_buffer *eb,
6030 struct page *accessed)
5df4235e 6031{
cc5e31a4 6032 int num_pages, i;
5df4235e 6033
0b32f4bb
JB
6034 check_buffer_tree_ref(eb);
6035
65ad0104 6036 num_pages = num_extent_pages(eb);
5df4235e 6037 for (i = 0; i < num_pages; i++) {
fb85fc9a
DS
6038 struct page *p = eb->pages[i];
6039
2457aec6
MG
6040 if (p != accessed)
6041 mark_page_accessed(p);
5df4235e
JB
6042 }
6043}
6044
f28491e0
JB
6045struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
6046 u64 start)
452c75c3
CS
6047{
6048 struct extent_buffer *eb;
6049
2f3186d8
QW
6050 eb = find_extent_buffer_nolock(fs_info, start);
6051 if (!eb)
6052 return NULL;
6053 /*
6054 * Lock our eb's refs_lock to avoid races with free_extent_buffer().
6055 * When we get our eb it might be flagged with EXTENT_BUFFER_STALE and
6056 * another task running free_extent_buffer() might have seen that flag
6057 * set, eb->refs == 2, that the buffer isn't under IO (dirty and
6058 * writeback flags not set) and it's still in the tree (flag
6059 * EXTENT_BUFFER_TREE_REF set), therefore being in the process of
6060 * decrementing the extent buffer's reference count twice. So here we
6061 * could race and increment the eb's reference count, clear its stale
6062 * flag, mark it as dirty and drop our reference before the other task
6063 * finishes executing free_extent_buffer, which would later result in
6064 * an attempt to free an extent buffer that is dirty.
6065 */
6066 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
6067 spin_lock(&eb->refs_lock);
6068 spin_unlock(&eb->refs_lock);
452c75c3 6069 }
2f3186d8
QW
6070 mark_extent_buffer_accessed(eb, NULL);
6071 return eb;
452c75c3
CS
6072}
6073
faa2dbf0
JB
6074#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
6075struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
da17066c 6076 u64 start)
faa2dbf0
JB
6077{
6078 struct extent_buffer *eb, *exists = NULL;
6079 int ret;
6080
6081 eb = find_extent_buffer(fs_info, start);
6082 if (eb)
6083 return eb;
da17066c 6084 eb = alloc_dummy_extent_buffer(fs_info, start);
faa2dbf0 6085 if (!eb)
b6293c82 6086 return ERR_PTR(-ENOMEM);
faa2dbf0
JB
6087 eb->fs_info = fs_info;
6088again:
e1860a77 6089 ret = radix_tree_preload(GFP_NOFS);
b6293c82
DC
6090 if (ret) {
6091 exists = ERR_PTR(ret);
faa2dbf0 6092 goto free_eb;
b6293c82 6093 }
faa2dbf0
JB
6094 spin_lock(&fs_info->buffer_lock);
6095 ret = radix_tree_insert(&fs_info->buffer_radix,
478ef886 6096 start >> fs_info->sectorsize_bits, eb);
faa2dbf0
JB
6097 spin_unlock(&fs_info->buffer_lock);
6098 radix_tree_preload_end();
6099 if (ret == -EEXIST) {
6100 exists = find_extent_buffer(fs_info, start);
6101 if (exists)
6102 goto free_eb;
6103 else
6104 goto again;
6105 }
6106 check_buffer_tree_ref(eb);
6107 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
6108
faa2dbf0
JB
6109 return eb;
6110free_eb:
6111 btrfs_release_extent_buffer(eb);
6112 return exists;
6113}
6114#endif
6115
81982210
QW
6116static struct extent_buffer *grab_extent_buffer(
6117 struct btrfs_fs_info *fs_info, struct page *page)
c0f0a9e7
QW
6118{
6119 struct extent_buffer *exists;
6120
81982210
QW
6121 /*
6122 * For subpage case, we completely rely on radix tree to ensure we
6123 * don't try to insert two ebs for the same bytenr. So here we always
6124 * return NULL and just continue.
6125 */
6126 if (fs_info->sectorsize < PAGE_SIZE)
6127 return NULL;
6128
c0f0a9e7
QW
6129 /* Page not yet attached to an extent buffer */
6130 if (!PagePrivate(page))
6131 return NULL;
6132
6133 /*
6134 * We could have already allocated an eb for this page and attached one
6135 * so lets see if we can get a ref on the existing eb, and if we can we
6136 * know it's good and we can just return that one, else we know we can
6137 * just overwrite page->private.
6138 */
6139 exists = (struct extent_buffer *)page->private;
6140 if (atomic_inc_not_zero(&exists->refs))
6141 return exists;
6142
6143 WARN_ON(PageDirty(page));
6144 detach_page_private(page);
6145 return NULL;
6146}
6147
f28491e0 6148struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
3fbaf258 6149 u64 start, u64 owner_root, int level)
d1310b2e 6150{
da17066c 6151 unsigned long len = fs_info->nodesize;
cc5e31a4
DS
6152 int num_pages;
6153 int i;
09cbfeaf 6154 unsigned long index = start >> PAGE_SHIFT;
d1310b2e 6155 struct extent_buffer *eb;
6af118ce 6156 struct extent_buffer *exists = NULL;
d1310b2e 6157 struct page *p;
f28491e0 6158 struct address_space *mapping = fs_info->btree_inode->i_mapping;
d1310b2e 6159 int uptodate = 1;
19fe0a8b 6160 int ret;
d1310b2e 6161
da17066c 6162 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
c871b0f2
LB
6163 btrfs_err(fs_info, "bad tree block start %llu", start);
6164 return ERR_PTR(-EINVAL);
6165 }
6166
e9306ad4
QW
6167#if BITS_PER_LONG == 32
6168 if (start >= MAX_LFS_FILESIZE) {
6169 btrfs_err_rl(fs_info,
6170 "extent buffer %llu is beyond 32bit page cache limit", start);
6171 btrfs_err_32bit_limit(fs_info);
6172 return ERR_PTR(-EOVERFLOW);
6173 }
6174 if (start >= BTRFS_32BIT_EARLY_WARN_THRESHOLD)
6175 btrfs_warn_32bit_limit(fs_info);
6176#endif
6177
1aaac38c
QW
6178 if (fs_info->sectorsize < PAGE_SIZE &&
6179 offset_in_page(start) + len > PAGE_SIZE) {
6180 btrfs_err(fs_info,
6181 "tree block crosses page boundary, start %llu nodesize %lu",
6182 start, len);
6183 return ERR_PTR(-EINVAL);
6184 }
6185
f28491e0 6186 eb = find_extent_buffer(fs_info, start);
452c75c3 6187 if (eb)
6af118ce 6188 return eb;
6af118ce 6189
23d79d81 6190 eb = __alloc_extent_buffer(fs_info, start, len);
2b114d1d 6191 if (!eb)
c871b0f2 6192 return ERR_PTR(-ENOMEM);
e114c545 6193 btrfs_set_buffer_lockdep_class(owner_root, eb, level);
d1310b2e 6194
65ad0104 6195 num_pages = num_extent_pages(eb);
727011e0 6196 for (i = 0; i < num_pages; i++, index++) {
760f991f
QW
6197 struct btrfs_subpage *prealloc = NULL;
6198
d1b5c567 6199 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
c871b0f2
LB
6200 if (!p) {
6201 exists = ERR_PTR(-ENOMEM);
6af118ce 6202 goto free_eb;
c871b0f2 6203 }
4f2de97a 6204
760f991f
QW
6205 /*
6206 * Preallocate page->private for subpage case, so that we won't
6207 * allocate memory with private_lock hold. The memory will be
6208 * freed by attach_extent_buffer_page() or freed manually if
6209 * we exit earlier.
6210 *
6211 * Although we have ensured one subpage eb can only have one
6212 * page, but it may change in the future for 16K page size
6213 * support, so we still preallocate the memory in the loop.
6214 */
fdf250db 6215 if (fs_info->sectorsize < PAGE_SIZE) {
651fb419
QW
6216 prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA);
6217 if (IS_ERR(prealloc)) {
6218 ret = PTR_ERR(prealloc);
fdf250db
QW
6219 unlock_page(p);
6220 put_page(p);
6221 exists = ERR_PTR(ret);
6222 goto free_eb;
6223 }
760f991f
QW
6224 }
6225
4f2de97a 6226 spin_lock(&mapping->private_lock);
81982210 6227 exists = grab_extent_buffer(fs_info, p);
c0f0a9e7
QW
6228 if (exists) {
6229 spin_unlock(&mapping->private_lock);
6230 unlock_page(p);
6231 put_page(p);
6232 mark_extent_buffer_accessed(exists, p);
760f991f 6233 btrfs_free_subpage(prealloc);
c0f0a9e7 6234 goto free_eb;
d1310b2e 6235 }
760f991f
QW
6236 /* Should not fail, as we have preallocated the memory */
6237 ret = attach_extent_buffer_page(eb, p, prealloc);
6238 ASSERT(!ret);
8ff8466d
QW
6239 /*
6240 * To inform we have extra eb under allocation, so that
6241 * detach_extent_buffer_page() won't release the page private
6242 * when the eb hasn't yet been inserted into radix tree.
6243 *
6244 * The ref will be decreased when the eb released the page, in
6245 * detach_extent_buffer_page().
6246 * Thus needs no special handling in error path.
6247 */
6248 btrfs_page_inc_eb_refs(fs_info, p);
4f2de97a 6249 spin_unlock(&mapping->private_lock);
760f991f 6250
1e5eb3d6 6251 WARN_ON(btrfs_page_test_dirty(fs_info, p, eb->start, eb->len));
727011e0 6252 eb->pages[i] = p;
d1310b2e
CM
6253 if (!PageUptodate(p))
6254 uptodate = 0;
eb14ab8e
CM
6255
6256 /*
b16d011e
NB
6257 * We can't unlock the pages just yet since the extent buffer
6258 * hasn't been properly inserted in the radix tree, this
6259 * opens a race with btree_releasepage which can free a page
6260 * while we are still filling in all pages for the buffer and
6261 * we could crash.
eb14ab8e 6262 */
d1310b2e
CM
6263 }
6264 if (uptodate)
b4ce94de 6265 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
115391d2 6266again:
e1860a77 6267 ret = radix_tree_preload(GFP_NOFS);
c871b0f2
LB
6268 if (ret) {
6269 exists = ERR_PTR(ret);
19fe0a8b 6270 goto free_eb;
c871b0f2 6271 }
19fe0a8b 6272
f28491e0
JB
6273 spin_lock(&fs_info->buffer_lock);
6274 ret = radix_tree_insert(&fs_info->buffer_radix,
478ef886 6275 start >> fs_info->sectorsize_bits, eb);
f28491e0 6276 spin_unlock(&fs_info->buffer_lock);
452c75c3 6277 radix_tree_preload_end();
19fe0a8b 6278 if (ret == -EEXIST) {
f28491e0 6279 exists = find_extent_buffer(fs_info, start);
452c75c3
CS
6280 if (exists)
6281 goto free_eb;
6282 else
115391d2 6283 goto again;
6af118ce 6284 }
6af118ce 6285 /* add one reference for the tree */
0b32f4bb 6286 check_buffer_tree_ref(eb);
34b41ace 6287 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
eb14ab8e
CM
6288
6289 /*
b16d011e
NB
6290 * Now it's safe to unlock the pages because any calls to
6291 * btree_releasepage will correctly detect that a page belongs to a
6292 * live buffer and won't free them prematurely.
eb14ab8e 6293 */
28187ae5
NB
6294 for (i = 0; i < num_pages; i++)
6295 unlock_page(eb->pages[i]);
d1310b2e
CM
6296 return eb;
6297
6af118ce 6298free_eb:
5ca64f45 6299 WARN_ON(!atomic_dec_and_test(&eb->refs));
727011e0
CM
6300 for (i = 0; i < num_pages; i++) {
6301 if (eb->pages[i])
6302 unlock_page(eb->pages[i]);
6303 }
eb14ab8e 6304
897ca6e9 6305 btrfs_release_extent_buffer(eb);
6af118ce 6306 return exists;
d1310b2e 6307}
d1310b2e 6308
3083ee2e
JB
6309static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
6310{
6311 struct extent_buffer *eb =
6312 container_of(head, struct extent_buffer, rcu_head);
6313
6314 __free_extent_buffer(eb);
6315}
6316
f7a52a40 6317static int release_extent_buffer(struct extent_buffer *eb)
5ce48d0f 6318 __releases(&eb->refs_lock)
3083ee2e 6319{
07e21c4d
NB
6320 lockdep_assert_held(&eb->refs_lock);
6321
3083ee2e
JB
6322 WARN_ON(atomic_read(&eb->refs) == 0);
6323 if (atomic_dec_and_test(&eb->refs)) {
34b41ace 6324 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
f28491e0 6325 struct btrfs_fs_info *fs_info = eb->fs_info;
3083ee2e 6326
815a51c7 6327 spin_unlock(&eb->refs_lock);
3083ee2e 6328
f28491e0
JB
6329 spin_lock(&fs_info->buffer_lock);
6330 radix_tree_delete(&fs_info->buffer_radix,
478ef886 6331 eb->start >> fs_info->sectorsize_bits);
f28491e0 6332 spin_unlock(&fs_info->buffer_lock);
34b41ace
JB
6333 } else {
6334 spin_unlock(&eb->refs_lock);
815a51c7 6335 }
3083ee2e 6336
8c38938c 6337 btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
3083ee2e 6338 /* Should be safe to release our pages at this point */
55ac0139 6339 btrfs_release_extent_buffer_pages(eb);
bcb7e449 6340#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
b0132a3b 6341 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
bcb7e449
JB
6342 __free_extent_buffer(eb);
6343 return 1;
6344 }
6345#endif
3083ee2e 6346 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
e64860aa 6347 return 1;
3083ee2e
JB
6348 }
6349 spin_unlock(&eb->refs_lock);
e64860aa
JB
6350
6351 return 0;
3083ee2e
JB
6352}
6353
d1310b2e
CM
6354void free_extent_buffer(struct extent_buffer *eb)
6355{
242e18c7
CM
6356 int refs;
6357 int old;
d1310b2e
CM
6358 if (!eb)
6359 return;
6360
242e18c7
CM
6361 while (1) {
6362 refs = atomic_read(&eb->refs);
46cc775e
NB
6363 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
6364 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
6365 refs == 1))
242e18c7
CM
6366 break;
6367 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
6368 if (old == refs)
6369 return;
6370 }
6371
3083ee2e
JB
6372 spin_lock(&eb->refs_lock);
6373 if (atomic_read(&eb->refs) == 2 &&
6374 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
0b32f4bb 6375 !extent_buffer_under_io(eb) &&
3083ee2e
JB
6376 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6377 atomic_dec(&eb->refs);
6378
6379 /*
6380 * I know this is terrible, but it's temporary until we stop tracking
6381 * the uptodate bits and such for the extent buffers.
6382 */
f7a52a40 6383 release_extent_buffer(eb);
3083ee2e
JB
6384}
6385
6386void free_extent_buffer_stale(struct extent_buffer *eb)
6387{
6388 if (!eb)
d1310b2e
CM
6389 return;
6390
3083ee2e
JB
6391 spin_lock(&eb->refs_lock);
6392 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
6393
0b32f4bb 6394 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
3083ee2e
JB
6395 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
6396 atomic_dec(&eb->refs);
f7a52a40 6397 release_extent_buffer(eb);
d1310b2e 6398}
d1310b2e 6399
0d27797e
QW
6400static void btree_clear_page_dirty(struct page *page)
6401{
6402 ASSERT(PageDirty(page));
6403 ASSERT(PageLocked(page));
6404 clear_page_dirty_for_io(page);
6405 xa_lock_irq(&page->mapping->i_pages);
6406 if (!PageDirty(page))
6407 __xa_clear_mark(&page->mapping->i_pages,
6408 page_index(page), PAGECACHE_TAG_DIRTY);
6409 xa_unlock_irq(&page->mapping->i_pages);
6410}
6411
6412static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb)
6413{
6414 struct btrfs_fs_info *fs_info = eb->fs_info;
6415 struct page *page = eb->pages[0];
6416 bool last;
6417
6418 /* btree_clear_page_dirty() needs page locked */
6419 lock_page(page);
6420 last = btrfs_subpage_clear_and_test_dirty(fs_info, page, eb->start,
6421 eb->len);
6422 if (last)
6423 btree_clear_page_dirty(page);
6424 unlock_page(page);
6425 WARN_ON(atomic_read(&eb->refs) == 0);
6426}
6427
2b48966a 6428void clear_extent_buffer_dirty(const struct extent_buffer *eb)
d1310b2e 6429{
cc5e31a4
DS
6430 int i;
6431 int num_pages;
d1310b2e
CM
6432 struct page *page;
6433
0d27797e
QW
6434 if (eb->fs_info->sectorsize < PAGE_SIZE)
6435 return clear_subpage_extent_buffer_dirty(eb);
6436
65ad0104 6437 num_pages = num_extent_pages(eb);
d1310b2e
CM
6438
6439 for (i = 0; i < num_pages; i++) {
fb85fc9a 6440 page = eb->pages[i];
b9473439 6441 if (!PageDirty(page))
d2c3f4f6 6442 continue;
a61e6f29 6443 lock_page(page);
0d27797e 6444 btree_clear_page_dirty(page);
bf0da8c1 6445 ClearPageError(page);
a61e6f29 6446 unlock_page(page);
d1310b2e 6447 }
0b32f4bb 6448 WARN_ON(atomic_read(&eb->refs) == 0);
d1310b2e 6449}
d1310b2e 6450
abb57ef3 6451bool set_extent_buffer_dirty(struct extent_buffer *eb)
d1310b2e 6452{
cc5e31a4
DS
6453 int i;
6454 int num_pages;
abb57ef3 6455 bool was_dirty;
d1310b2e 6456
0b32f4bb
JB
6457 check_buffer_tree_ref(eb);
6458
b9473439 6459 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
0b32f4bb 6460
65ad0104 6461 num_pages = num_extent_pages(eb);
3083ee2e 6462 WARN_ON(atomic_read(&eb->refs) == 0);
0b32f4bb
JB
6463 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
6464
0d27797e
QW
6465 if (!was_dirty) {
6466 bool subpage = eb->fs_info->sectorsize < PAGE_SIZE;
51995c39 6467
0d27797e
QW
6468 /*
6469 * For subpage case, we can have other extent buffers in the
6470 * same page, and in clear_subpage_extent_buffer_dirty() we
6471 * have to clear page dirty without subpage lock held.
6472 * This can cause race where our page gets dirty cleared after
6473 * we just set it.
6474 *
6475 * Thankfully, clear_subpage_extent_buffer_dirty() has locked
6476 * its page for other reasons, we can use page lock to prevent
6477 * the above race.
6478 */
6479 if (subpage)
6480 lock_page(eb->pages[0]);
6481 for (i = 0; i < num_pages; i++)
6482 btrfs_page_set_dirty(eb->fs_info, eb->pages[i],
6483 eb->start, eb->len);
6484 if (subpage)
6485 unlock_page(eb->pages[0]);
6486 }
51995c39
LB
6487#ifdef CONFIG_BTRFS_DEBUG
6488 for (i = 0; i < num_pages; i++)
6489 ASSERT(PageDirty(eb->pages[i]));
6490#endif
6491
b9473439 6492 return was_dirty;
d1310b2e 6493}
d1310b2e 6494
69ba3927 6495void clear_extent_buffer_uptodate(struct extent_buffer *eb)
1259ab75 6496{
251f2acc 6497 struct btrfs_fs_info *fs_info = eb->fs_info;
1259ab75 6498 struct page *page;
cc5e31a4 6499 int num_pages;
251f2acc 6500 int i;
1259ab75 6501
b4ce94de 6502 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
65ad0104 6503 num_pages = num_extent_pages(eb);
1259ab75 6504 for (i = 0; i < num_pages; i++) {
fb85fc9a 6505 page = eb->pages[i];
33958dc6 6506 if (page)
251f2acc
QW
6507 btrfs_page_clear_uptodate(fs_info, page,
6508 eb->start, eb->len);
1259ab75 6509 }
1259ab75
CM
6510}
6511
09c25a8c 6512void set_extent_buffer_uptodate(struct extent_buffer *eb)
d1310b2e 6513{
251f2acc 6514 struct btrfs_fs_info *fs_info = eb->fs_info;
d1310b2e 6515 struct page *page;
cc5e31a4 6516 int num_pages;
251f2acc 6517 int i;
d1310b2e 6518
0b32f4bb 6519 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
65ad0104 6520 num_pages = num_extent_pages(eb);
d1310b2e 6521 for (i = 0; i < num_pages; i++) {
fb85fc9a 6522 page = eb->pages[i];
251f2acc 6523 btrfs_page_set_uptodate(fs_info, page, eb->start, eb->len);
d1310b2e 6524 }
d1310b2e 6525}
d1310b2e 6526
4012daf7
QW
6527static int read_extent_buffer_subpage(struct extent_buffer *eb, int wait,
6528 int mirror_num)
6529{
6530 struct btrfs_fs_info *fs_info = eb->fs_info;
6531 struct extent_io_tree *io_tree;
6532 struct page *page = eb->pages[0];
390ed29b 6533 struct btrfs_bio_ctrl bio_ctrl = { 0 };
4012daf7
QW
6534 int ret = 0;
6535
6536 ASSERT(!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags));
6537 ASSERT(PagePrivate(page));
6538 io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
6539
6540 if (wait == WAIT_NONE) {
dc56219f
GR
6541 if (!try_lock_extent(io_tree, eb->start, eb->start + eb->len - 1))
6542 return -EAGAIN;
4012daf7
QW
6543 } else {
6544 ret = lock_extent(io_tree, eb->start, eb->start + eb->len - 1);
6545 if (ret < 0)
6546 return ret;
6547 }
6548
6549 ret = 0;
6550 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags) ||
6551 PageUptodate(page) ||
6552 btrfs_subpage_test_uptodate(fs_info, page, eb->start, eb->len)) {
6553 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
6554 unlock_extent(io_tree, eb->start, eb->start + eb->len - 1);
6555 return ret;
6556 }
6557
6558 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
6559 eb->read_mirror = 0;
6560 atomic_set(&eb->io_pages, 1);
6561 check_buffer_tree_ref(eb);
6562 btrfs_subpage_clear_error(fs_info, page, eb->start, eb->len);
6563
3d078efa 6564 btrfs_subpage_start_reader(fs_info, page, eb->start, eb->len);
390ed29b
QW
6565 ret = submit_extent_page(REQ_OP_READ | REQ_META, NULL, &bio_ctrl,
6566 page, eb->start, eb->len,
6567 eb->start - page_offset(page),
6568 end_bio_extent_readpage, mirror_num, 0,
4012daf7
QW
6569 true);
6570 if (ret) {
6571 /*
6572 * In the endio function, if we hit something wrong we will
6573 * increase the io_pages, so here we need to decrease it for
6574 * error path.
6575 */
6576 atomic_dec(&eb->io_pages);
6577 }
390ed29b 6578 if (bio_ctrl.bio) {
4012daf7
QW
6579 int tmp;
6580
390ed29b
QW
6581 tmp = submit_one_bio(bio_ctrl.bio, mirror_num, 0);
6582 bio_ctrl.bio = NULL;
4012daf7
QW
6583 if (tmp < 0)
6584 return tmp;
6585 }
6586 if (ret || wait != WAIT_COMPLETE)
6587 return ret;
6588
6589 wait_extent_bit(io_tree, eb->start, eb->start + eb->len - 1, EXTENT_LOCKED);
6590 if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
6591 ret = -EIO;
6592 return ret;
6593}
6594
c2ccfbc6 6595int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
d1310b2e 6596{
cc5e31a4 6597 int i;
d1310b2e
CM
6598 struct page *page;
6599 int err;
6600 int ret = 0;
ce9adaa5
CM
6601 int locked_pages = 0;
6602 int all_uptodate = 1;
cc5e31a4 6603 int num_pages;
727011e0 6604 unsigned long num_reads = 0;
390ed29b 6605 struct btrfs_bio_ctrl bio_ctrl = { 0 };
a86c12c7 6606
b4ce94de 6607 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
d1310b2e
CM
6608 return 0;
6609
651740a5
JB
6610 /*
6611 * We could have had EXTENT_BUFFER_UPTODATE cleared by the write
6612 * operation, which could potentially still be in flight. In this case
6613 * we simply want to return an error.
6614 */
6615 if (unlikely(test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)))
6616 return -EIO;
6617
4012daf7
QW
6618 if (eb->fs_info->sectorsize < PAGE_SIZE)
6619 return read_extent_buffer_subpage(eb, wait, mirror_num);
6620
65ad0104 6621 num_pages = num_extent_pages(eb);
8436ea91 6622 for (i = 0; i < num_pages; i++) {
fb85fc9a 6623 page = eb->pages[i];
bb82ab88 6624 if (wait == WAIT_NONE) {
2c4d8cb7
QW
6625 /*
6626 * WAIT_NONE is only utilized by readahead. If we can't
6627 * acquire the lock atomically it means either the eb
6628 * is being read out or under modification.
6629 * Either way the eb will be or has been cached,
6630 * readahead can exit safely.
6631 */
2db04966 6632 if (!trylock_page(page))
ce9adaa5 6633 goto unlock_exit;
d1310b2e
CM
6634 } else {
6635 lock_page(page);
6636 }
ce9adaa5 6637 locked_pages++;
2571e739
LB
6638 }
6639 /*
6640 * We need to firstly lock all pages to make sure that
6641 * the uptodate bit of our pages won't be affected by
6642 * clear_extent_buffer_uptodate().
6643 */
8436ea91 6644 for (i = 0; i < num_pages; i++) {
2571e739 6645 page = eb->pages[i];
727011e0
CM
6646 if (!PageUptodate(page)) {
6647 num_reads++;
ce9adaa5 6648 all_uptodate = 0;
727011e0 6649 }
ce9adaa5 6650 }
2571e739 6651
ce9adaa5 6652 if (all_uptodate) {
8436ea91 6653 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
ce9adaa5
CM
6654 goto unlock_exit;
6655 }
6656
656f30db 6657 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
5cf1ab56 6658 eb->read_mirror = 0;
0b32f4bb 6659 atomic_set(&eb->io_pages, num_reads);
6bf9cd2e
BB
6660 /*
6661 * It is possible for releasepage to clear the TREE_REF bit before we
6662 * set io_pages. See check_buffer_tree_ref for a more detailed comment.
6663 */
6664 check_buffer_tree_ref(eb);
8436ea91 6665 for (i = 0; i < num_pages; i++) {
fb85fc9a 6666 page = eb->pages[i];
baf863b9 6667
ce9adaa5 6668 if (!PageUptodate(page)) {
baf863b9
LB
6669 if (ret) {
6670 atomic_dec(&eb->io_pages);
6671 unlock_page(page);
6672 continue;
6673 }
6674
f188591e 6675 ClearPageError(page);
0420177c 6676 err = submit_extent_page(REQ_OP_READ | REQ_META, NULL,
390ed29b
QW
6677 &bio_ctrl, page, page_offset(page),
6678 PAGE_SIZE, 0, end_bio_extent_readpage,
6679 mirror_num, 0, false);
baf863b9 6680 if (err) {
baf863b9 6681 /*
0420177c
NB
6682 * We failed to submit the bio so it's the
6683 * caller's responsibility to perform cleanup
6684 * i.e unlock page/set error bit.
baf863b9 6685 */
0420177c
NB
6686 ret = err;
6687 SetPageError(page);
6688 unlock_page(page);
baf863b9
LB
6689 atomic_dec(&eb->io_pages);
6690 }
d1310b2e
CM
6691 } else {
6692 unlock_page(page);
6693 }
6694 }
6695
390ed29b
QW
6696 if (bio_ctrl.bio) {
6697 err = submit_one_bio(bio_ctrl.bio, mirror_num, bio_ctrl.bio_flags);
6698 bio_ctrl.bio = NULL;
79787eaa
JM
6699 if (err)
6700 return err;
355808c2 6701 }
a86c12c7 6702
bb82ab88 6703 if (ret || wait != WAIT_COMPLETE)
d1310b2e 6704 return ret;
d397712b 6705
8436ea91 6706 for (i = 0; i < num_pages; i++) {
fb85fc9a 6707 page = eb->pages[i];
d1310b2e 6708 wait_on_page_locked(page);
d397712b 6709 if (!PageUptodate(page))
d1310b2e 6710 ret = -EIO;
d1310b2e 6711 }
d397712b 6712
d1310b2e 6713 return ret;
ce9adaa5
CM
6714
6715unlock_exit:
d397712b 6716 while (locked_pages > 0) {
ce9adaa5 6717 locked_pages--;
8436ea91
JB
6718 page = eb->pages[locked_pages];
6719 unlock_page(page);
ce9adaa5
CM
6720 }
6721 return ret;
d1310b2e 6722}
d1310b2e 6723
f98b6215
QW
6724static bool report_eb_range(const struct extent_buffer *eb, unsigned long start,
6725 unsigned long len)
6726{
6727 btrfs_warn(eb->fs_info,
6728 "access to eb bytenr %llu len %lu out of range start %lu len %lu",
6729 eb->start, eb->len, start, len);
6730 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
6731
6732 return true;
6733}
6734
6735/*
6736 * Check if the [start, start + len) range is valid before reading/writing
6737 * the eb.
6738 * NOTE: @start and @len are offset inside the eb, not logical address.
6739 *
6740 * Caller should not touch the dst/src memory if this function returns error.
6741 */
6742static inline int check_eb_range(const struct extent_buffer *eb,
6743 unsigned long start, unsigned long len)
6744{
6745 unsigned long offset;
6746
6747 /* start, start + len should not go beyond eb->len nor overflow */
6748 if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len))
6749 return report_eb_range(eb, start, len);
6750
6751 return false;
6752}
6753
1cbb1f45
JM
6754void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
6755 unsigned long start, unsigned long len)
d1310b2e
CM
6756{
6757 size_t cur;
6758 size_t offset;
6759 struct page *page;
6760 char *kaddr;
6761 char *dst = (char *)dstv;
884b07d0 6762 unsigned long i = get_eb_page_index(start);
d1310b2e 6763
f98b6215 6764 if (check_eb_range(eb, start, len))
f716abd5 6765 return;
d1310b2e 6766
884b07d0 6767 offset = get_eb_offset_in_page(eb, start);
d1310b2e 6768
d397712b 6769 while (len > 0) {
fb85fc9a 6770 page = eb->pages[i];
d1310b2e 6771
09cbfeaf 6772 cur = min(len, (PAGE_SIZE - offset));
a6591715 6773 kaddr = page_address(page);
d1310b2e 6774 memcpy(dst, kaddr + offset, cur);
d1310b2e
CM
6775
6776 dst += cur;
6777 len -= cur;
6778 offset = 0;
6779 i++;
6780 }
6781}
d1310b2e 6782
a48b73ec
JB
6783int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
6784 void __user *dstv,
6785 unsigned long start, unsigned long len)
550ac1d8
GH
6786{
6787 size_t cur;
6788 size_t offset;
6789 struct page *page;
6790 char *kaddr;
6791 char __user *dst = (char __user *)dstv;
884b07d0 6792 unsigned long i = get_eb_page_index(start);
550ac1d8
GH
6793 int ret = 0;
6794
6795 WARN_ON(start > eb->len);
6796 WARN_ON(start + len > eb->start + eb->len);
6797
884b07d0 6798 offset = get_eb_offset_in_page(eb, start);
550ac1d8
GH
6799
6800 while (len > 0) {
fb85fc9a 6801 page = eb->pages[i];
550ac1d8 6802
09cbfeaf 6803 cur = min(len, (PAGE_SIZE - offset));
550ac1d8 6804 kaddr = page_address(page);
a48b73ec 6805 if (copy_to_user_nofault(dst, kaddr + offset, cur)) {
550ac1d8
GH
6806 ret = -EFAULT;
6807 break;
6808 }
6809
6810 dst += cur;
6811 len -= cur;
6812 offset = 0;
6813 i++;
6814 }
6815
6816 return ret;
6817}
6818
1cbb1f45
JM
6819int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
6820 unsigned long start, unsigned long len)
d1310b2e
CM
6821{
6822 size_t cur;
6823 size_t offset;
6824 struct page *page;
6825 char *kaddr;
6826 char *ptr = (char *)ptrv;
884b07d0 6827 unsigned long i = get_eb_page_index(start);
d1310b2e
CM
6828 int ret = 0;
6829
f98b6215
QW
6830 if (check_eb_range(eb, start, len))
6831 return -EINVAL;
d1310b2e 6832
884b07d0 6833 offset = get_eb_offset_in_page(eb, start);
d1310b2e 6834
d397712b 6835 while (len > 0) {
fb85fc9a 6836 page = eb->pages[i];
d1310b2e 6837
09cbfeaf 6838 cur = min(len, (PAGE_SIZE - offset));
d1310b2e 6839
a6591715 6840 kaddr = page_address(page);
d1310b2e 6841 ret = memcmp(ptr, kaddr + offset, cur);
d1310b2e
CM
6842 if (ret)
6843 break;
6844
6845 ptr += cur;
6846 len -= cur;
6847 offset = 0;
6848 i++;
6849 }
6850 return ret;
6851}
d1310b2e 6852
b8f95771
QW
6853/*
6854 * Check that the extent buffer is uptodate.
6855 *
6856 * For regular sector size == PAGE_SIZE case, check if @page is uptodate.
6857 * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE.
6858 */
6859static void assert_eb_page_uptodate(const struct extent_buffer *eb,
6860 struct page *page)
6861{
6862 struct btrfs_fs_info *fs_info = eb->fs_info;
6863
a50e1fcb
JB
6864 /*
6865 * If we are using the commit root we could potentially clear a page
6866 * Uptodate while we're using the extent buffer that we've previously
6867 * looked up. We don't want to complain in this case, as the page was
6868 * valid before, we just didn't write it out. Instead we want to catch
6869 * the case where we didn't actually read the block properly, which
6870 * would have !PageUptodate && !PageError, as we clear PageError before
6871 * reading.
6872 */
b8f95771 6873 if (fs_info->sectorsize < PAGE_SIZE) {
a50e1fcb 6874 bool uptodate, error;
b8f95771
QW
6875
6876 uptodate = btrfs_subpage_test_uptodate(fs_info, page,
6877 eb->start, eb->len);
a50e1fcb
JB
6878 error = btrfs_subpage_test_error(fs_info, page, eb->start, eb->len);
6879 WARN_ON(!uptodate && !error);
b8f95771 6880 } else {
a50e1fcb 6881 WARN_ON(!PageUptodate(page) && !PageError(page));
b8f95771
QW
6882 }
6883}
6884
2b48966a 6885void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb,
f157bf76
DS
6886 const void *srcv)
6887{
6888 char *kaddr;
6889
b8f95771 6890 assert_eb_page_uptodate(eb, eb->pages[0]);
24880be5
DS
6891 kaddr = page_address(eb->pages[0]) +
6892 get_eb_offset_in_page(eb, offsetof(struct btrfs_header,
6893 chunk_tree_uuid));
6894 memcpy(kaddr, srcv, BTRFS_FSID_SIZE);
f157bf76
DS
6895}
6896
2b48966a 6897void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *srcv)
f157bf76
DS
6898{
6899 char *kaddr;
6900
b8f95771 6901 assert_eb_page_uptodate(eb, eb->pages[0]);
24880be5
DS
6902 kaddr = page_address(eb->pages[0]) +
6903 get_eb_offset_in_page(eb, offsetof(struct btrfs_header, fsid));
6904 memcpy(kaddr, srcv, BTRFS_FSID_SIZE);
f157bf76
DS
6905}
6906
2b48966a 6907void write_extent_buffer(const struct extent_buffer *eb, const void *srcv,
d1310b2e
CM
6908 unsigned long start, unsigned long len)
6909{
6910 size_t cur;
6911 size_t offset;
6912 struct page *page;
6913 char *kaddr;
6914 char *src = (char *)srcv;
884b07d0 6915 unsigned long i = get_eb_page_index(start);
d1310b2e 6916
d3575156
NA
6917 WARN_ON(test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags));
6918
f98b6215
QW
6919 if (check_eb_range(eb, start, len))
6920 return;
d1310b2e 6921
884b07d0 6922 offset = get_eb_offset_in_page(eb, start);
d1310b2e 6923
d397712b 6924 while (len > 0) {
fb85fc9a 6925 page = eb->pages[i];
b8f95771 6926 assert_eb_page_uptodate(eb, page);
d1310b2e 6927
09cbfeaf 6928 cur = min(len, PAGE_SIZE - offset);
a6591715 6929 kaddr = page_address(page);
d1310b2e 6930 memcpy(kaddr + offset, src, cur);
d1310b2e
CM
6931
6932 src += cur;
6933 len -= cur;
6934 offset = 0;
6935 i++;
6936 }
6937}
d1310b2e 6938
2b48966a 6939void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
b159fa28 6940 unsigned long len)
d1310b2e
CM
6941{
6942 size_t cur;
6943 size_t offset;
6944 struct page *page;
6945 char *kaddr;
884b07d0 6946 unsigned long i = get_eb_page_index(start);
d1310b2e 6947
f98b6215
QW
6948 if (check_eb_range(eb, start, len))
6949 return;
d1310b2e 6950
884b07d0 6951 offset = get_eb_offset_in_page(eb, start);
d1310b2e 6952
d397712b 6953 while (len > 0) {
fb85fc9a 6954 page = eb->pages[i];
b8f95771 6955 assert_eb_page_uptodate(eb, page);
d1310b2e 6956
09cbfeaf 6957 cur = min(len, PAGE_SIZE - offset);
a6591715 6958 kaddr = page_address(page);
b159fa28 6959 memset(kaddr + offset, 0, cur);
d1310b2e
CM
6960
6961 len -= cur;
6962 offset = 0;
6963 i++;
6964 }
6965}
d1310b2e 6966
2b48966a
DS
6967void copy_extent_buffer_full(const struct extent_buffer *dst,
6968 const struct extent_buffer *src)
58e8012c
DS
6969{
6970 int i;
cc5e31a4 6971 int num_pages;
58e8012c
DS
6972
6973 ASSERT(dst->len == src->len);
6974
884b07d0
QW
6975 if (dst->fs_info->sectorsize == PAGE_SIZE) {
6976 num_pages = num_extent_pages(dst);
6977 for (i = 0; i < num_pages; i++)
6978 copy_page(page_address(dst->pages[i]),
6979 page_address(src->pages[i]));
6980 } else {
6981 size_t src_offset = get_eb_offset_in_page(src, 0);
6982 size_t dst_offset = get_eb_offset_in_page(dst, 0);
6983
6984 ASSERT(src->fs_info->sectorsize < PAGE_SIZE);
6985 memcpy(page_address(dst->pages[0]) + dst_offset,
6986 page_address(src->pages[0]) + src_offset,
6987 src->len);
6988 }
58e8012c
DS
6989}
6990
2b48966a
DS
6991void copy_extent_buffer(const struct extent_buffer *dst,
6992 const struct extent_buffer *src,
d1310b2e
CM
6993 unsigned long dst_offset, unsigned long src_offset,
6994 unsigned long len)
6995{
6996 u64 dst_len = dst->len;
6997 size_t cur;
6998 size_t offset;
6999 struct page *page;
7000 char *kaddr;
884b07d0 7001 unsigned long i = get_eb_page_index(dst_offset);
d1310b2e 7002
f98b6215
QW
7003 if (check_eb_range(dst, dst_offset, len) ||
7004 check_eb_range(src, src_offset, len))
7005 return;
7006
d1310b2e
CM
7007 WARN_ON(src->len != dst_len);
7008
884b07d0 7009 offset = get_eb_offset_in_page(dst, dst_offset);
d1310b2e 7010
d397712b 7011 while (len > 0) {
fb85fc9a 7012 page = dst->pages[i];
b8f95771 7013 assert_eb_page_uptodate(dst, page);
d1310b2e 7014
09cbfeaf 7015 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
d1310b2e 7016
a6591715 7017 kaddr = page_address(page);
d1310b2e 7018 read_extent_buffer(src, kaddr + offset, src_offset, cur);
d1310b2e
CM
7019
7020 src_offset += cur;
7021 len -= cur;
7022 offset = 0;
7023 i++;
7024 }
7025}
d1310b2e 7026
3e1e8bb7
OS
7027/*
7028 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
7029 * given bit number
7030 * @eb: the extent buffer
7031 * @start: offset of the bitmap item in the extent buffer
7032 * @nr: bit number
7033 * @page_index: return index of the page in the extent buffer that contains the
7034 * given bit number
7035 * @page_offset: return offset into the page given by page_index
7036 *
7037 * This helper hides the ugliness of finding the byte in an extent buffer which
7038 * contains a given bit.
7039 */
2b48966a 7040static inline void eb_bitmap_offset(const struct extent_buffer *eb,
3e1e8bb7
OS
7041 unsigned long start, unsigned long nr,
7042 unsigned long *page_index,
7043 size_t *page_offset)
7044{
3e1e8bb7
OS
7045 size_t byte_offset = BIT_BYTE(nr);
7046 size_t offset;
7047
7048 /*
7049 * The byte we want is the offset of the extent buffer + the offset of
7050 * the bitmap item in the extent buffer + the offset of the byte in the
7051 * bitmap item.
7052 */
884b07d0 7053 offset = start + offset_in_page(eb->start) + byte_offset;
3e1e8bb7 7054
09cbfeaf 7055 *page_index = offset >> PAGE_SHIFT;
7073017a 7056 *page_offset = offset_in_page(offset);
3e1e8bb7
OS
7057}
7058
7059/**
7060 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
7061 * @eb: the extent buffer
7062 * @start: offset of the bitmap item in the extent buffer
7063 * @nr: bit number to test
7064 */
2b48966a 7065int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
3e1e8bb7
OS
7066 unsigned long nr)
7067{
2fe1d551 7068 u8 *kaddr;
3e1e8bb7
OS
7069 struct page *page;
7070 unsigned long i;
7071 size_t offset;
7072
7073 eb_bitmap_offset(eb, start, nr, &i, &offset);
7074 page = eb->pages[i];
b8f95771 7075 assert_eb_page_uptodate(eb, page);
3e1e8bb7
OS
7076 kaddr = page_address(page);
7077 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
7078}
7079
7080/**
7081 * extent_buffer_bitmap_set - set an area of a bitmap
7082 * @eb: the extent buffer
7083 * @start: offset of the bitmap item in the extent buffer
7084 * @pos: bit number of the first bit
7085 * @len: number of bits to set
7086 */
2b48966a 7087void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
3e1e8bb7
OS
7088 unsigned long pos, unsigned long len)
7089{
2fe1d551 7090 u8 *kaddr;
3e1e8bb7
OS
7091 struct page *page;
7092 unsigned long i;
7093 size_t offset;
7094 const unsigned int size = pos + len;
7095 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
2fe1d551 7096 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
3e1e8bb7
OS
7097
7098 eb_bitmap_offset(eb, start, pos, &i, &offset);
7099 page = eb->pages[i];
b8f95771 7100 assert_eb_page_uptodate(eb, page);
3e1e8bb7
OS
7101 kaddr = page_address(page);
7102
7103 while (len >= bits_to_set) {
7104 kaddr[offset] |= mask_to_set;
7105 len -= bits_to_set;
7106 bits_to_set = BITS_PER_BYTE;
9c894696 7107 mask_to_set = ~0;
09cbfeaf 7108 if (++offset >= PAGE_SIZE && len > 0) {
3e1e8bb7
OS
7109 offset = 0;
7110 page = eb->pages[++i];
b8f95771 7111 assert_eb_page_uptodate(eb, page);
3e1e8bb7
OS
7112 kaddr = page_address(page);
7113 }
7114 }
7115 if (len) {
7116 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
7117 kaddr[offset] |= mask_to_set;
7118 }
7119}
7120
7121
7122/**
7123 * extent_buffer_bitmap_clear - clear an area of a bitmap
7124 * @eb: the extent buffer
7125 * @start: offset of the bitmap item in the extent buffer
7126 * @pos: bit number of the first bit
7127 * @len: number of bits to clear
7128 */
2b48966a
DS
7129void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
7130 unsigned long start, unsigned long pos,
7131 unsigned long len)
3e1e8bb7 7132{
2fe1d551 7133 u8 *kaddr;
3e1e8bb7
OS
7134 struct page *page;
7135 unsigned long i;
7136 size_t offset;
7137 const unsigned int size = pos + len;
7138 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
2fe1d551 7139 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
3e1e8bb7
OS
7140
7141 eb_bitmap_offset(eb, start, pos, &i, &offset);
7142 page = eb->pages[i];
b8f95771 7143 assert_eb_page_uptodate(eb, page);
3e1e8bb7
OS
7144 kaddr = page_address(page);
7145
7146 while (len >= bits_to_clear) {
7147 kaddr[offset] &= ~mask_to_clear;
7148 len -= bits_to_clear;
7149 bits_to_clear = BITS_PER_BYTE;
9c894696 7150 mask_to_clear = ~0;
09cbfeaf 7151 if (++offset >= PAGE_SIZE && len > 0) {
3e1e8bb7
OS
7152 offset = 0;
7153 page = eb->pages[++i];
b8f95771 7154 assert_eb_page_uptodate(eb, page);
3e1e8bb7
OS
7155 kaddr = page_address(page);
7156 }
7157 }
7158 if (len) {
7159 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
7160 kaddr[offset] &= ~mask_to_clear;
7161 }
7162}
7163
3387206f
ST
7164static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
7165{
7166 unsigned long distance = (src > dst) ? src - dst : dst - src;
7167 return distance < len;
7168}
7169
d1310b2e
CM
7170static void copy_pages(struct page *dst_page, struct page *src_page,
7171 unsigned long dst_off, unsigned long src_off,
7172 unsigned long len)
7173{
a6591715 7174 char *dst_kaddr = page_address(dst_page);
d1310b2e 7175 char *src_kaddr;
727011e0 7176 int must_memmove = 0;
d1310b2e 7177
3387206f 7178 if (dst_page != src_page) {
a6591715 7179 src_kaddr = page_address(src_page);
3387206f 7180 } else {
d1310b2e 7181 src_kaddr = dst_kaddr;
727011e0
CM
7182 if (areas_overlap(src_off, dst_off, len))
7183 must_memmove = 1;
3387206f 7184 }
d1310b2e 7185
727011e0
CM
7186 if (must_memmove)
7187 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
7188 else
7189 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
d1310b2e
CM
7190}
7191
2b48966a
DS
7192void memcpy_extent_buffer(const struct extent_buffer *dst,
7193 unsigned long dst_offset, unsigned long src_offset,
7194 unsigned long len)
d1310b2e
CM
7195{
7196 size_t cur;
7197 size_t dst_off_in_page;
7198 size_t src_off_in_page;
d1310b2e
CM
7199 unsigned long dst_i;
7200 unsigned long src_i;
7201
f98b6215
QW
7202 if (check_eb_range(dst, dst_offset, len) ||
7203 check_eb_range(dst, src_offset, len))
7204 return;
d1310b2e 7205
d397712b 7206 while (len > 0) {
884b07d0
QW
7207 dst_off_in_page = get_eb_offset_in_page(dst, dst_offset);
7208 src_off_in_page = get_eb_offset_in_page(dst, src_offset);
d1310b2e 7209
884b07d0
QW
7210 dst_i = get_eb_page_index(dst_offset);
7211 src_i = get_eb_page_index(src_offset);
d1310b2e 7212
09cbfeaf 7213 cur = min(len, (unsigned long)(PAGE_SIZE -
d1310b2e
CM
7214 src_off_in_page));
7215 cur = min_t(unsigned long, cur,
09cbfeaf 7216 (unsigned long)(PAGE_SIZE - dst_off_in_page));
d1310b2e 7217
fb85fc9a 7218 copy_pages(dst->pages[dst_i], dst->pages[src_i],
d1310b2e
CM
7219 dst_off_in_page, src_off_in_page, cur);
7220
7221 src_offset += cur;
7222 dst_offset += cur;
7223 len -= cur;
7224 }
7225}
d1310b2e 7226
2b48966a
DS
7227void memmove_extent_buffer(const struct extent_buffer *dst,
7228 unsigned long dst_offset, unsigned long src_offset,
7229 unsigned long len)
d1310b2e
CM
7230{
7231 size_t cur;
7232 size_t dst_off_in_page;
7233 size_t src_off_in_page;
7234 unsigned long dst_end = dst_offset + len - 1;
7235 unsigned long src_end = src_offset + len - 1;
d1310b2e
CM
7236 unsigned long dst_i;
7237 unsigned long src_i;
7238
f98b6215
QW
7239 if (check_eb_range(dst, dst_offset, len) ||
7240 check_eb_range(dst, src_offset, len))
7241 return;
727011e0 7242 if (dst_offset < src_offset) {
d1310b2e
CM
7243 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
7244 return;
7245 }
d397712b 7246 while (len > 0) {
884b07d0
QW
7247 dst_i = get_eb_page_index(dst_end);
7248 src_i = get_eb_page_index(src_end);
d1310b2e 7249
884b07d0
QW
7250 dst_off_in_page = get_eb_offset_in_page(dst, dst_end);
7251 src_off_in_page = get_eb_offset_in_page(dst, src_end);
d1310b2e
CM
7252
7253 cur = min_t(unsigned long, len, src_off_in_page + 1);
7254 cur = min(cur, dst_off_in_page + 1);
fb85fc9a 7255 copy_pages(dst->pages[dst_i], dst->pages[src_i],
d1310b2e
CM
7256 dst_off_in_page - cur + 1,
7257 src_off_in_page - cur + 1, cur);
7258
7259 dst_end -= cur;
7260 src_end -= cur;
7261 len -= cur;
7262 }
7263}
6af118ce 7264
72a69cd0 7265#define GANG_LOOKUP_SIZE 16
d1e86e3f
QW
7266static struct extent_buffer *get_next_extent_buffer(
7267 struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
7268{
72a69cd0 7269 struct extent_buffer *gang[GANG_LOOKUP_SIZE];
d1e86e3f
QW
7270 struct extent_buffer *found = NULL;
7271 u64 page_start = page_offset(page);
72a69cd0 7272 u64 cur = page_start;
d1e86e3f
QW
7273
7274 ASSERT(in_range(bytenr, page_start, PAGE_SIZE));
d1e86e3f
QW
7275 lockdep_assert_held(&fs_info->buffer_lock);
7276
72a69cd0
QW
7277 while (cur < page_start + PAGE_SIZE) {
7278 int ret;
7279 int i;
7280
7281 ret = radix_tree_gang_lookup(&fs_info->buffer_radix,
7282 (void **)gang, cur >> fs_info->sectorsize_bits,
7283 min_t(unsigned int, GANG_LOOKUP_SIZE,
7284 PAGE_SIZE / fs_info->nodesize));
7285 if (ret == 0)
7286 goto out;
7287 for (i = 0; i < ret; i++) {
7288 /* Already beyond page end */
7289 if (gang[i]->start >= page_start + PAGE_SIZE)
7290 goto out;
7291 /* Found one */
7292 if (gang[i]->start >= bytenr) {
7293 found = gang[i];
7294 goto out;
7295 }
d1e86e3f 7296 }
72a69cd0 7297 cur = gang[ret - 1]->start + gang[ret - 1]->len;
d1e86e3f 7298 }
72a69cd0 7299out:
d1e86e3f
QW
7300 return found;
7301}
7302
7303static int try_release_subpage_extent_buffer(struct page *page)
7304{
7305 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
7306 u64 cur = page_offset(page);
7307 const u64 end = page_offset(page) + PAGE_SIZE;
7308 int ret;
7309
7310 while (cur < end) {
7311 struct extent_buffer *eb = NULL;
7312
7313 /*
7314 * Unlike try_release_extent_buffer() which uses page->private
7315 * to grab buffer, for subpage case we rely on radix tree, thus
7316 * we need to ensure radix tree consistency.
7317 *
7318 * We also want an atomic snapshot of the radix tree, thus go
7319 * with spinlock rather than RCU.
7320 */
7321 spin_lock(&fs_info->buffer_lock);
7322 eb = get_next_extent_buffer(fs_info, page, cur);
7323 if (!eb) {
7324 /* No more eb in the page range after or at cur */
7325 spin_unlock(&fs_info->buffer_lock);
7326 break;
7327 }
7328 cur = eb->start + eb->len;
7329
7330 /*
7331 * The same as try_release_extent_buffer(), to ensure the eb
7332 * won't disappear out from under us.
7333 */
7334 spin_lock(&eb->refs_lock);
7335 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
7336 spin_unlock(&eb->refs_lock);
7337 spin_unlock(&fs_info->buffer_lock);
7338 break;
7339 }
7340 spin_unlock(&fs_info->buffer_lock);
7341
7342 /*
7343 * If tree ref isn't set then we know the ref on this eb is a
7344 * real ref, so just return, this eb will likely be freed soon
7345 * anyway.
7346 */
7347 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
7348 spin_unlock(&eb->refs_lock);
7349 break;
7350 }
7351
7352 /*
7353 * Here we don't care about the return value, we will always
7354 * check the page private at the end. And
7355 * release_extent_buffer() will release the refs_lock.
7356 */
7357 release_extent_buffer(eb);
7358 }
7359 /*
7360 * Finally to check if we have cleared page private, as if we have
7361 * released all ebs in the page, the page private should be cleared now.
7362 */
7363 spin_lock(&page->mapping->private_lock);
7364 if (!PagePrivate(page))
7365 ret = 1;
7366 else
7367 ret = 0;
7368 spin_unlock(&page->mapping->private_lock);
7369 return ret;
7370
7371}
7372
f7a52a40 7373int try_release_extent_buffer(struct page *page)
19fe0a8b 7374{
6af118ce 7375 struct extent_buffer *eb;
6af118ce 7376
d1e86e3f
QW
7377 if (btrfs_sb(page->mapping->host->i_sb)->sectorsize < PAGE_SIZE)
7378 return try_release_subpage_extent_buffer(page);
7379
3083ee2e 7380 /*
d1e86e3f
QW
7381 * We need to make sure nobody is changing page->private, as we rely on
7382 * page->private as the pointer to extent buffer.
3083ee2e
JB
7383 */
7384 spin_lock(&page->mapping->private_lock);
7385 if (!PagePrivate(page)) {
7386 spin_unlock(&page->mapping->private_lock);
4f2de97a 7387 return 1;
45f49bce 7388 }
6af118ce 7389
3083ee2e
JB
7390 eb = (struct extent_buffer *)page->private;
7391 BUG_ON(!eb);
19fe0a8b
MX
7392
7393 /*
3083ee2e
JB
7394 * This is a little awful but should be ok, we need to make sure that
7395 * the eb doesn't disappear out from under us while we're looking at
7396 * this page.
19fe0a8b 7397 */
3083ee2e 7398 spin_lock(&eb->refs_lock);
0b32f4bb 7399 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
3083ee2e
JB
7400 spin_unlock(&eb->refs_lock);
7401 spin_unlock(&page->mapping->private_lock);
7402 return 0;
b9473439 7403 }
3083ee2e 7404 spin_unlock(&page->mapping->private_lock);
897ca6e9 7405
19fe0a8b 7406 /*
3083ee2e
JB
7407 * If tree ref isn't set then we know the ref on this eb is a real ref,
7408 * so just return, this page will likely be freed soon anyway.
19fe0a8b 7409 */
3083ee2e
JB
7410 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
7411 spin_unlock(&eb->refs_lock);
7412 return 0;
b9473439 7413 }
19fe0a8b 7414
f7a52a40 7415 return release_extent_buffer(eb);
6af118ce 7416}
bfb484d9
JB
7417
7418/*
7419 * btrfs_readahead_tree_block - attempt to readahead a child block
7420 * @fs_info: the fs_info
7421 * @bytenr: bytenr to read
3fbaf258 7422 * @owner_root: objectid of the root that owns this eb
bfb484d9 7423 * @gen: generation for the uptodate check, can be 0
3fbaf258 7424 * @level: level for the eb
bfb484d9
JB
7425 *
7426 * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a
7427 * normal uptodate check of the eb, without checking the generation. If we have
7428 * to read the block we will not block on anything.
7429 */
7430void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
3fbaf258 7431 u64 bytenr, u64 owner_root, u64 gen, int level)
bfb484d9
JB
7432{
7433 struct extent_buffer *eb;
7434 int ret;
7435
3fbaf258 7436 eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level);
bfb484d9
JB
7437 if (IS_ERR(eb))
7438 return;
7439
7440 if (btrfs_buffer_uptodate(eb, gen, 1)) {
7441 free_extent_buffer(eb);
7442 return;
7443 }
7444
7445 ret = read_extent_buffer_pages(eb, WAIT_NONE, 0);
7446 if (ret < 0)
7447 free_extent_buffer_stale(eb);
7448 else
7449 free_extent_buffer(eb);
7450}
7451
7452/*
7453 * btrfs_readahead_node_child - readahead a node's child block
7454 * @node: parent node we're reading from
7455 * @slot: slot in the parent node for the child we want to read
7456 *
7457 * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at
7458 * the slot in the node provided.
7459 */
7460void btrfs_readahead_node_child(struct extent_buffer *node, int slot)
7461{
7462 btrfs_readahead_tree_block(node->fs_info,
7463 btrfs_node_blockptr(node, slot),
3fbaf258
JB
7464 btrfs_header_owner(node),
7465 btrfs_node_ptr_generation(node, slot),
7466 btrfs_header_level(node) - 1);
bfb484d9 7467}