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