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