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