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