Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
c1d7c514 | 2 | |
d1310b2e CM |
3 | #include <linux/bitops.h> |
4 | #include <linux/slab.h> | |
5 | #include <linux/bio.h> | |
6 | #include <linux/mm.h> | |
d1310b2e CM |
7 | #include <linux/pagemap.h> |
8 | #include <linux/page-flags.h> | |
395cb57e | 9 | #include <linux/sched/mm.h> |
d1310b2e CM |
10 | #include <linux/spinlock.h> |
11 | #include <linux/blkdev.h> | |
12 | #include <linux/swap.h> | |
d1310b2e CM |
13 | #include <linux/writeback.h> |
14 | #include <linux/pagevec.h> | |
268bb0ce | 15 | #include <linux/prefetch.h> |
14605409 | 16 | #include <linux/fsverity.h> |
cea62800 | 17 | #include "misc.h" |
d1310b2e | 18 | #include "extent_io.h" |
9c7d3a54 | 19 | #include "extent-io-tree.h" |
d1310b2e | 20 | #include "extent_map.h" |
902b22f3 DW |
21 | #include "ctree.h" |
22 | #include "btrfs_inode.h" | |
103c1972 | 23 | #include "bio.h" |
21adbd5c | 24 | #include "check-integrity.h" |
0b32f4bb | 25 | #include "locking.h" |
606686ee | 26 | #include "rcu-string.h" |
fe09e16c | 27 | #include "backref.h" |
6af49dbd | 28 | #include "disk-io.h" |
760f991f | 29 | #include "subpage.h" |
d3575156 | 30 | #include "zoned.h" |
0bc09ca1 | 31 | #include "block-group.h" |
2a5232a8 | 32 | #include "compression.h" |
ec8eb376 | 33 | #include "fs.h" |
07e81dc9 | 34 | #include "accessors.h" |
7c8ede16 | 35 | #include "file-item.h" |
af142b6f | 36 | #include "file.h" |
77407dc0 | 37 | #include "dev-replace.h" |
7f0add25 | 38 | #include "super.h" |
98c8d683 | 39 | #include "transaction.h" |
d1310b2e | 40 | |
d1310b2e CM |
41 | static struct kmem_cache *extent_buffer_cache; |
42 | ||
6d49ba1b | 43 | #ifdef CONFIG_BTRFS_DEBUG |
a40246e8 JB |
44 | static inline void btrfs_leak_debug_add_eb(struct extent_buffer *eb) |
45 | { | |
46 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
47 | unsigned long flags; | |
48 | ||
49 | spin_lock_irqsave(&fs_info->eb_leak_lock, flags); | |
50 | list_add(&eb->leak_list, &fs_info->allocated_ebs); | |
51 | spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); | |
52 | } | |
53 | ||
a40246e8 JB |
54 | static inline void btrfs_leak_debug_del_eb(struct extent_buffer *eb) |
55 | { | |
56 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
57 | unsigned long flags; | |
58 | ||
59 | spin_lock_irqsave(&fs_info->eb_leak_lock, flags); | |
60 | list_del(&eb->leak_list); | |
61 | spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); | |
6d49ba1b ES |
62 | } |
63 | ||
3fd63727 | 64 | void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info) |
6d49ba1b | 65 | { |
6d49ba1b | 66 | struct extent_buffer *eb; |
3fd63727 | 67 | unsigned long flags; |
6d49ba1b | 68 | |
8c38938c JB |
69 | /* |
70 | * If we didn't get into open_ctree our allocated_ebs will not be | |
71 | * initialized, so just skip this. | |
72 | */ | |
73 | if (!fs_info->allocated_ebs.next) | |
74 | return; | |
75 | ||
b95b78e6 | 76 | WARN_ON(!list_empty(&fs_info->allocated_ebs)); |
3fd63727 JB |
77 | spin_lock_irqsave(&fs_info->eb_leak_lock, flags); |
78 | while (!list_empty(&fs_info->allocated_ebs)) { | |
79 | eb = list_first_entry(&fs_info->allocated_ebs, | |
80 | struct extent_buffer, leak_list); | |
8c38938c JB |
81 | pr_err( |
82 | "BTRFS: buffer leak start %llu len %lu refs %d bflags %lu owner %llu\n", | |
83 | eb->start, eb->len, atomic_read(&eb->refs), eb->bflags, | |
84 | btrfs_header_owner(eb)); | |
33ca832f JB |
85 | list_del(&eb->leak_list); |
86 | kmem_cache_free(extent_buffer_cache, eb); | |
87 | } | |
3fd63727 | 88 | spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); |
33ca832f | 89 | } |
6d49ba1b | 90 | #else |
a40246e8 | 91 | #define btrfs_leak_debug_add_eb(eb) do {} while (0) |
a40246e8 | 92 | #define btrfs_leak_debug_del_eb(eb) do {} while (0) |
4bef0848 | 93 | #endif |
d1310b2e | 94 | |
7aab8b32 CH |
95 | /* |
96 | * Structure to record info about the bio being assembled, and other info like | |
97 | * how many bytes are there before stripe/ordered extent boundary. | |
98 | */ | |
99 | struct btrfs_bio_ctrl { | |
9dfde1b4 | 100 | struct btrfs_bio *bbio; |
0f07003b | 101 | enum btrfs_compression_type compress_type; |
7aab8b32 | 102 | u32 len_to_oe_boundary; |
c000bc04 | 103 | blk_opf_t opf; |
5467abba | 104 | btrfs_bio_end_io_t end_io_func; |
72b505dc | 105 | struct writeback_control *wbc; |
d1310b2e CM |
106 | }; |
107 | ||
722c82ac | 108 | static void submit_one_bio(struct btrfs_bio_ctrl *bio_ctrl) |
bb58eb9e | 109 | { |
9dfde1b4 | 110 | struct btrfs_bio *bbio = bio_ctrl->bbio; |
722c82ac | 111 | |
9dfde1b4 | 112 | if (!bbio) |
722c82ac | 113 | return; |
bb58eb9e | 114 | |
e0eefe07 | 115 | /* Caller should ensure the bio has at least some range added */ |
9dfde1b4 | 116 | ASSERT(bbio->bio.bi_iter.bi_size); |
c9583ada | 117 | |
9dfde1b4 | 118 | if (btrfs_op(&bbio->bio) == BTRFS_MAP_READ && |
35a8d7da | 119 | bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) |
e1949310 | 120 | btrfs_submit_compressed_read(bbio); |
35a8d7da | 121 | else |
b78b98e0 | 122 | btrfs_submit_bio(bbio, 0); |
35a8d7da | 123 | |
9dfde1b4 CH |
124 | /* The bbio is owned by the end_io handler now */ |
125 | bio_ctrl->bbio = NULL; | |
3065976b QW |
126 | } |
127 | ||
f4340622 | 128 | /* |
ee5f017d | 129 | * Submit or fail the current bio in the bio_ctrl structure. |
f4340622 | 130 | */ |
ee5f017d | 131 | static void submit_write_bio(struct btrfs_bio_ctrl *bio_ctrl, int ret) |
bb58eb9e | 132 | { |
9dfde1b4 | 133 | struct btrfs_bio *bbio = bio_ctrl->bbio; |
bb58eb9e | 134 | |
9dfde1b4 | 135 | if (!bbio) |
9845e5dd CH |
136 | return; |
137 | ||
138 | if (ret) { | |
139 | ASSERT(ret < 0); | |
9dfde1b4 | 140 | btrfs_bio_end_io(bbio, errno_to_blk_status(ret)); |
917f32a2 | 141 | /* The bio is owned by the end_io handler now */ |
9dfde1b4 | 142 | bio_ctrl->bbio = NULL; |
9845e5dd | 143 | } else { |
ee5f017d | 144 | submit_one_bio(bio_ctrl); |
bb58eb9e QW |
145 | } |
146 | } | |
e2932ee0 | 147 | |
a62a3bd9 JB |
148 | int __init extent_buffer_init_cachep(void) |
149 | { | |
837e1972 | 150 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
9601e3f6 | 151 | sizeof(struct extent_buffer), 0, |
fba4b697 | 152 | SLAB_MEM_SPREAD, NULL); |
a62a3bd9 | 153 | if (!extent_buffer_cache) |
6f0d04f8 | 154 | return -ENOMEM; |
b208c2f7 | 155 | |
d1310b2e | 156 | return 0; |
d1310b2e CM |
157 | } |
158 | ||
a62a3bd9 | 159 | void __cold extent_buffer_free_cachep(void) |
d1310b2e | 160 | { |
8c0a8537 KS |
161 | /* |
162 | * Make sure all delayed rcu free are flushed before we | |
163 | * destroy caches. | |
164 | */ | |
165 | rcu_barrier(); | |
5598e900 | 166 | kmem_cache_destroy(extent_buffer_cache); |
d1310b2e CM |
167 | } |
168 | ||
bd1fa4f0 | 169 | void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) |
4adaa611 | 170 | { |
09cbfeaf KS |
171 | unsigned long index = start >> PAGE_SHIFT; |
172 | unsigned long end_index = end >> PAGE_SHIFT; | |
4adaa611 CM |
173 | struct page *page; |
174 | ||
175 | while (index <= end_index) { | |
176 | page = find_get_page(inode->i_mapping, index); | |
177 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ | |
178 | clear_page_dirty_for_io(page); | |
09cbfeaf | 179 | put_page(page); |
4adaa611 CM |
180 | index++; |
181 | } | |
4adaa611 CM |
182 | } |
183 | ||
f6311572 | 184 | void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end) |
4adaa611 | 185 | { |
ebf55c88 | 186 | struct address_space *mapping = inode->i_mapping; |
09cbfeaf KS |
187 | unsigned long index = start >> PAGE_SHIFT; |
188 | unsigned long end_index = end >> PAGE_SHIFT; | |
ebf55c88 | 189 | struct folio *folio; |
4adaa611 CM |
190 | |
191 | while (index <= end_index) { | |
ebf55c88 MWO |
192 | folio = filemap_get_folio(mapping, index); |
193 | filemap_dirty_folio(mapping, folio); | |
194 | folio_account_redirty(folio); | |
195 | index += folio_nr_pages(folio); | |
196 | folio_put(folio); | |
4adaa611 | 197 | } |
4adaa611 CM |
198 | } |
199 | ||
ef4e88e6 CH |
200 | static void process_one_page(struct btrfs_fs_info *fs_info, |
201 | struct page *page, struct page *locked_page, | |
202 | unsigned long page_ops, u64 start, u64 end) | |
ed8f13bf | 203 | { |
e38992be QW |
204 | u32 len; |
205 | ||
206 | ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX); | |
207 | len = end + 1 - start; | |
208 | ||
ed8f13bf | 209 | if (page_ops & PAGE_SET_ORDERED) |
b945a463 | 210 | btrfs_page_clamp_set_ordered(fs_info, page, start, len); |
ed8f13bf | 211 | if (page_ops & PAGE_START_WRITEBACK) { |
e38992be QW |
212 | btrfs_page_clamp_clear_dirty(fs_info, page, start, len); |
213 | btrfs_page_clamp_set_writeback(fs_info, page, start, len); | |
ed8f13bf QW |
214 | } |
215 | if (page_ops & PAGE_END_WRITEBACK) | |
e38992be | 216 | btrfs_page_clamp_clear_writeback(fs_info, page, start, len); |
a33a8e9a | 217 | |
ef4e88e6 | 218 | if (page != locked_page && (page_ops & PAGE_UNLOCK)) |
1e1de387 | 219 | btrfs_page_end_writer_lock(fs_info, page, start, len); |
ed8f13bf QW |
220 | } |
221 | ||
ef4e88e6 CH |
222 | static void __process_pages_contig(struct address_space *mapping, |
223 | struct page *locked_page, u64 start, u64 end, | |
224 | unsigned long page_ops) | |
ed8f13bf | 225 | { |
e38992be | 226 | struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb); |
ed8f13bf QW |
227 | pgoff_t start_index = start >> PAGE_SHIFT; |
228 | pgoff_t end_index = end >> PAGE_SHIFT; | |
229 | pgoff_t index = start_index; | |
04c6b79a | 230 | struct folio_batch fbatch; |
ed8f13bf QW |
231 | int i; |
232 | ||
04c6b79a VMO |
233 | folio_batch_init(&fbatch); |
234 | while (index <= end_index) { | |
235 | int found_folios; | |
236 | ||
237 | found_folios = filemap_get_folios_contig(mapping, &index, | |
238 | end_index, &fbatch); | |
04c6b79a | 239 | for (i = 0; i < found_folios; i++) { |
04c6b79a | 240 | struct folio *folio = fbatch.folios[i]; |
ef4e88e6 CH |
241 | |
242 | process_one_page(fs_info, &folio->page, locked_page, | |
243 | page_ops, start, end); | |
ed8f13bf | 244 | } |
04c6b79a | 245 | folio_batch_release(&fbatch); |
ed8f13bf QW |
246 | cond_resched(); |
247 | } | |
ed8f13bf | 248 | } |
da2c7009 | 249 | |
143bede5 JM |
250 | static noinline void __unlock_for_delalloc(struct inode *inode, |
251 | struct page *locked_page, | |
252 | u64 start, u64 end) | |
c8b97818 | 253 | { |
09cbfeaf KS |
254 | unsigned long index = start >> PAGE_SHIFT; |
255 | unsigned long end_index = end >> PAGE_SHIFT; | |
c8b97818 | 256 | |
76c0021d | 257 | ASSERT(locked_page); |
c8b97818 | 258 | if (index == locked_page->index && end_index == index) |
143bede5 | 259 | return; |
c8b97818 | 260 | |
98af9ab1 | 261 | __process_pages_contig(inode->i_mapping, locked_page, start, end, |
ef4e88e6 | 262 | PAGE_UNLOCK); |
c8b97818 CM |
263 | } |
264 | ||
265 | static noinline int lock_delalloc_pages(struct inode *inode, | |
266 | struct page *locked_page, | |
ef4e88e6 CH |
267 | u64 start, |
268 | u64 end) | |
c8b97818 | 269 | { |
ef4e88e6 CH |
270 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
271 | struct address_space *mapping = inode->i_mapping; | |
272 | pgoff_t start_index = start >> PAGE_SHIFT; | |
273 | pgoff_t end_index = end >> PAGE_SHIFT; | |
274 | pgoff_t index = start_index; | |
275 | u64 processed_end = start; | |
276 | struct folio_batch fbatch; | |
c8b97818 | 277 | |
c8b97818 CM |
278 | if (index == locked_page->index && index == end_index) |
279 | return 0; | |
280 | ||
ef4e88e6 CH |
281 | folio_batch_init(&fbatch); |
282 | while (index <= end_index) { | |
283 | unsigned int found_folios, i; | |
284 | ||
285 | found_folios = filemap_get_folios_contig(mapping, &index, | |
286 | end_index, &fbatch); | |
287 | if (found_folios == 0) | |
288 | goto out; | |
289 | ||
290 | for (i = 0; i < found_folios; i++) { | |
291 | struct page *page = &fbatch.folios[i]->page; | |
292 | u32 len = end + 1 - start; | |
293 | ||
294 | if (page == locked_page) | |
295 | continue; | |
296 | ||
297 | if (btrfs_page_start_writer_lock(fs_info, page, start, | |
298 | len)) | |
299 | goto out; | |
300 | ||
301 | if (!PageDirty(page) || page->mapping != mapping) { | |
302 | btrfs_page_end_writer_lock(fs_info, page, start, | |
303 | len); | |
304 | goto out; | |
305 | } | |
306 | ||
307 | processed_end = page_offset(page) + PAGE_SIZE - 1; | |
308 | } | |
309 | folio_batch_release(&fbatch); | |
310 | cond_resched(); | |
311 | } | |
312 | ||
313 | return 0; | |
314 | out: | |
315 | folio_batch_release(&fbatch); | |
316 | if (processed_end > start) | |
317 | __unlock_for_delalloc(inode, locked_page, start, processed_end); | |
318 | return -EAGAIN; | |
c8b97818 CM |
319 | } |
320 | ||
321 | /* | |
3522e903 | 322 | * Find and lock a contiguous range of bytes in the file marked as delalloc, no |
2749f7ef | 323 | * more than @max_bytes. |
c8b97818 | 324 | * |
2749f7ef QW |
325 | * @start: The original start bytenr to search. |
326 | * Will store the extent range start bytenr. | |
327 | * @end: The original end bytenr of the search range | |
328 | * Will store the extent range end bytenr. | |
329 | * | |
330 | * Return true if we find a delalloc range which starts inside the original | |
331 | * range, and @start/@end will store the delalloc range start/end. | |
332 | * | |
333 | * Return false if we can't find any delalloc range which starts inside the | |
334 | * original range, and @start/@end will be the non-delalloc range start/end. | |
c8b97818 | 335 | */ |
ce9f967f | 336 | EXPORT_FOR_TESTS |
3522e903 | 337 | noinline_for_stack bool find_lock_delalloc_range(struct inode *inode, |
294e30fe | 338 | struct page *locked_page, u64 *start, |
917aacec | 339 | u64 *end) |
c8b97818 | 340 | { |
f7b12a62 | 341 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9978059b | 342 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
2749f7ef QW |
343 | const u64 orig_start = *start; |
344 | const u64 orig_end = *end; | |
f7b12a62 NA |
345 | /* The sanity tests may not set a valid fs_info. */ |
346 | u64 max_bytes = fs_info ? fs_info->max_extent_size : BTRFS_MAX_EXTENT_SIZE; | |
c8b97818 CM |
347 | u64 delalloc_start; |
348 | u64 delalloc_end; | |
3522e903 | 349 | bool found; |
9655d298 | 350 | struct extent_state *cached_state = NULL; |
c8b97818 CM |
351 | int ret; |
352 | int loops = 0; | |
353 | ||
2749f7ef QW |
354 | /* Caller should pass a valid @end to indicate the search range end */ |
355 | ASSERT(orig_end > orig_start); | |
356 | ||
357 | /* The range should at least cover part of the page */ | |
358 | ASSERT(!(orig_start >= page_offset(locked_page) + PAGE_SIZE || | |
359 | orig_end <= page_offset(locked_page))); | |
c8b97818 CM |
360 | again: |
361 | /* step one, find a bunch of delalloc bytes starting at start */ | |
362 | delalloc_start = *start; | |
363 | delalloc_end = 0; | |
083e75e7 JB |
364 | found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
365 | max_bytes, &cached_state); | |
2749f7ef | 366 | if (!found || delalloc_end <= *start || delalloc_start > orig_end) { |
c8b97818 | 367 | *start = delalloc_start; |
2749f7ef QW |
368 | |
369 | /* @delalloc_end can be -1, never go beyond @orig_end */ | |
370 | *end = min(delalloc_end, orig_end); | |
c2a128d2 | 371 | free_extent_state(cached_state); |
3522e903 | 372 | return false; |
c8b97818 CM |
373 | } |
374 | ||
70b99e69 CM |
375 | /* |
376 | * start comes from the offset of locked_page. We have to lock | |
377 | * pages in order, so we can't process delalloc bytes before | |
378 | * locked_page | |
379 | */ | |
d397712b | 380 | if (delalloc_start < *start) |
70b99e69 | 381 | delalloc_start = *start; |
70b99e69 | 382 | |
c8b97818 CM |
383 | /* |
384 | * make sure to limit the number of pages we try to lock down | |
c8b97818 | 385 | */ |
7bf811a5 JB |
386 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
387 | delalloc_end = delalloc_start + max_bytes - 1; | |
d397712b | 388 | |
c8b97818 CM |
389 | /* step two, lock all the pages after the page that has start */ |
390 | ret = lock_delalloc_pages(inode, locked_page, | |
391 | delalloc_start, delalloc_end); | |
9bfd61d9 | 392 | ASSERT(!ret || ret == -EAGAIN); |
c8b97818 CM |
393 | if (ret == -EAGAIN) { |
394 | /* some of the pages are gone, lets avoid looping by | |
395 | * shortening the size of the delalloc range we're searching | |
396 | */ | |
9655d298 | 397 | free_extent_state(cached_state); |
7d788742 | 398 | cached_state = NULL; |
c8b97818 | 399 | if (!loops) { |
09cbfeaf | 400 | max_bytes = PAGE_SIZE; |
c8b97818 CM |
401 | loops = 1; |
402 | goto again; | |
403 | } else { | |
3522e903 | 404 | found = false; |
c8b97818 CM |
405 | goto out_failed; |
406 | } | |
407 | } | |
c8b97818 CM |
408 | |
409 | /* step three, lock the state bits for the whole range */ | |
570eb97b | 410 | lock_extent(tree, delalloc_start, delalloc_end, &cached_state); |
c8b97818 CM |
411 | |
412 | /* then test to make sure it is all still delalloc */ | |
413 | ret = test_range_bit(tree, delalloc_start, delalloc_end, | |
9655d298 | 414 | EXTENT_DELALLOC, 1, cached_state); |
c8b97818 | 415 | if (!ret) { |
570eb97b JB |
416 | unlock_extent(tree, delalloc_start, delalloc_end, |
417 | &cached_state); | |
c8b97818 CM |
418 | __unlock_for_delalloc(inode, locked_page, |
419 | delalloc_start, delalloc_end); | |
420 | cond_resched(); | |
421 | goto again; | |
422 | } | |
9655d298 | 423 | free_extent_state(cached_state); |
c8b97818 CM |
424 | *start = delalloc_start; |
425 | *end = delalloc_end; | |
426 | out_failed: | |
427 | return found; | |
428 | } | |
429 | ||
ad7ff17b | 430 | void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
74e9194a | 431 | struct page *locked_page, |
f97e27e9 | 432 | u32 clear_bits, unsigned long page_ops) |
873695b3 | 433 | { |
bd015294 | 434 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, NULL); |
873695b3 | 435 | |
ad7ff17b | 436 | __process_pages_contig(inode->vfs_inode.i_mapping, locked_page, |
ef4e88e6 | 437 | start, end, page_ops); |
873695b3 LB |
438 | } |
439 | ||
ed9ee98e CH |
440 | static bool btrfs_verify_page(struct page *page, u64 start) |
441 | { | |
442 | if (!fsverity_active(page->mapping->host) || | |
57201ddd | 443 | PageUptodate(page) || |
ed9ee98e CH |
444 | start >= i_size_read(page->mapping->host)) |
445 | return true; | |
446 | return fsverity_verify_page(page); | |
447 | } | |
448 | ||
150e4b05 QW |
449 | static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len) |
450 | { | |
451 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); | |
452 | ||
453 | ASSERT(page_offset(page) <= start && | |
454 | start + len <= page_offset(page) + PAGE_SIZE); | |
455 | ||
2b2553f1 | 456 | if (uptodate && btrfs_verify_page(page, start)) |
2c14f0ff | 457 | btrfs_page_set_uptodate(fs_info, page, start, len); |
2b2553f1 | 458 | else |
150e4b05 | 459 | btrfs_page_clear_uptodate(fs_info, page, start, len); |
150e4b05 | 460 | |
fbca46eb | 461 | if (!btrfs_is_subpage(fs_info, page)) |
150e4b05 | 462 | unlock_page(page); |
3d078efa | 463 | else |
150e4b05 QW |
464 | btrfs_subpage_end_reader(fs_info, page, start, len); |
465 | } | |
466 | ||
d1310b2e CM |
467 | /* |
468 | * after a writepage IO is done, we need to: | |
469 | * clear the uptodate bits on error | |
470 | * clear the writeback bits in the extent tree for this IO | |
471 | * end_page_writeback if the page has no more pending IO | |
472 | * | |
473 | * Scheduling is not allowed, so the extent state tree is expected | |
474 | * to have one and only one object corresponding to this IO. | |
475 | */ | |
917f32a2 | 476 | static void end_bio_extent_writepage(struct btrfs_bio *bbio) |
d1310b2e | 477 | { |
917f32a2 | 478 | struct bio *bio = &bbio->bio; |
4e4cbee9 | 479 | int error = blk_status_to_errno(bio->bi_status); |
2c30c71b | 480 | struct bio_vec *bvec; |
6dc4f100 | 481 | struct bvec_iter_all iter_all; |
d1310b2e | 482 | |
c09abff8 | 483 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
2b070cfe | 484 | bio_for_each_segment_all(bvec, bio, iter_all) { |
d1310b2e | 485 | struct page *page = bvec->bv_page; |
0b246afa JM |
486 | struct inode *inode = page->mapping->host; |
487 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
321a02db | 488 | const u32 sectorsize = fs_info->sectorsize; |
4ba8223d CH |
489 | u64 start = page_offset(page) + bvec->bv_offset; |
490 | u32 len = bvec->bv_len; | |
321a02db QW |
491 | |
492 | /* Our read/write should always be sector aligned. */ | |
493 | if (!IS_ALIGNED(bvec->bv_offset, sectorsize)) | |
494 | btrfs_err(fs_info, | |
495 | "partial page write in btrfs with offset %u and length %u", | |
496 | bvec->bv_offset, bvec->bv_len); | |
497 | else if (!IS_ALIGNED(bvec->bv_len, sectorsize)) | |
498 | btrfs_info(fs_info, | |
499 | "incomplete page write with offset %u and length %u", | |
500 | bvec->bv_offset, bvec->bv_len); | |
501 | ||
0d394cca | 502 | btrfs_finish_ordered_extent(bbio->ordered, page, start, len, !error); |
4ba8223d CH |
503 | if (error) { |
504 | btrfs_page_clear_uptodate(fs_info, page, start, len); | |
505 | mapping_set_error(page->mapping, error); | |
506 | } | |
507 | btrfs_page_clear_writeback(fs_info, page, start, len); | |
2c30c71b | 508 | } |
2b1f55b0 | 509 | |
d1310b2e | 510 | bio_put(bio); |
d1310b2e CM |
511 | } |
512 | ||
94e8c95c QW |
513 | /* |
514 | * Record previously processed extent range | |
515 | * | |
516 | * For endio_readpage_release_extent() to handle a full extent range, reducing | |
517 | * the extent io operations. | |
518 | */ | |
519 | struct processed_extent { | |
520 | struct btrfs_inode *inode; | |
521 | /* Start of the range in @inode */ | |
522 | u64 start; | |
2e626e56 | 523 | /* End of the range in @inode */ |
94e8c95c QW |
524 | u64 end; |
525 | bool uptodate; | |
526 | }; | |
527 | ||
528 | /* | |
529 | * Try to release processed extent range | |
530 | * | |
531 | * May not release the extent range right now if the current range is | |
532 | * contiguous to processed extent. | |
533 | * | |
534 | * Will release processed extent when any of @inode, @uptodate, the range is | |
535 | * no longer contiguous to the processed range. | |
536 | * | |
537 | * Passing @inode == NULL will force processed extent to be released. | |
538 | */ | |
539 | static void endio_readpage_release_extent(struct processed_extent *processed, | |
540 | struct btrfs_inode *inode, u64 start, u64 end, | |
541 | bool uptodate) | |
883d0de4 MX |
542 | { |
543 | struct extent_state *cached = NULL; | |
94e8c95c QW |
544 | struct extent_io_tree *tree; |
545 | ||
546 | /* The first extent, initialize @processed */ | |
547 | if (!processed->inode) | |
548 | goto update; | |
883d0de4 | 549 | |
94e8c95c QW |
550 | /* |
551 | * Contiguous to processed extent, just uptodate the end. | |
552 | * | |
553 | * Several things to notice: | |
554 | * | |
555 | * - bio can be merged as long as on-disk bytenr is contiguous | |
556 | * This means we can have page belonging to other inodes, thus need to | |
557 | * check if the inode still matches. | |
558 | * - bvec can contain range beyond current page for multi-page bvec | |
559 | * Thus we need to do processed->end + 1 >= start check | |
560 | */ | |
561 | if (processed->inode == inode && processed->uptodate == uptodate && | |
562 | processed->end + 1 >= start && end >= processed->end) { | |
563 | processed->end = end; | |
564 | return; | |
565 | } | |
566 | ||
567 | tree = &processed->inode->io_tree; | |
568 | /* | |
569 | * Now we don't have range contiguous to the processed range, release | |
570 | * the processed range now. | |
571 | */ | |
48acc47d | 572 | unlock_extent(tree, processed->start, processed->end, &cached); |
94e8c95c QW |
573 | |
574 | update: | |
575 | /* Update processed to current range */ | |
576 | processed->inode = inode; | |
577 | processed->start = start; | |
578 | processed->end = end; | |
579 | processed->uptodate = uptodate; | |
883d0de4 MX |
580 | } |
581 | ||
92082d40 QW |
582 | static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page) |
583 | { | |
584 | ASSERT(PageLocked(page)); | |
fbca46eb | 585 | if (!btrfs_is_subpage(fs_info, page)) |
92082d40 QW |
586 | return; |
587 | ||
588 | ASSERT(PagePrivate(page)); | |
589 | btrfs_subpage_start_reader(fs_info, page, page_offset(page), PAGE_SIZE); | |
590 | } | |
591 | ||
d1310b2e CM |
592 | /* |
593 | * after a readpage IO is done, we need to: | |
594 | * clear the uptodate bits on error | |
595 | * set the uptodate bits if things worked | |
596 | * set the page up to date if all extents in the tree are uptodate | |
597 | * clear the lock bit in the extent tree | |
598 | * unlock the page if there are no other extents locked for it | |
599 | * | |
600 | * Scheduling is not allowed, so the extent state tree is expected | |
601 | * to have one and only one object corresponding to this IO. | |
602 | */ | |
917f32a2 | 603 | static void end_bio_extent_readpage(struct btrfs_bio *bbio) |
d1310b2e | 604 | { |
917f32a2 | 605 | struct bio *bio = &bbio->bio; |
2c30c71b | 606 | struct bio_vec *bvec; |
94e8c95c | 607 | struct processed_extent processed = { 0 }; |
7ffd27e3 QW |
608 | /* |
609 | * The offset to the beginning of a bio, since one bio can never be | |
610 | * larger than UINT_MAX, u32 here is enough. | |
611 | */ | |
612 | u32 bio_offset = 0; | |
6dc4f100 | 613 | struct bvec_iter_all iter_all; |
d1310b2e | 614 | |
c09abff8 | 615 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
2b070cfe | 616 | bio_for_each_segment_all(bvec, bio, iter_all) { |
150e4b05 | 617 | bool uptodate = !bio->bi_status; |
d1310b2e | 618 | struct page *page = bvec->bv_page; |
a71754fc | 619 | struct inode *inode = page->mapping->host; |
ab8d0fc4 | 620 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
7ffd27e3 QW |
621 | const u32 sectorsize = fs_info->sectorsize; |
622 | u64 start; | |
623 | u64 end; | |
624 | u32 len; | |
507903b8 | 625 | |
ab8d0fc4 JM |
626 | btrfs_debug(fs_info, |
627 | "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u", | |
1201b58b | 628 | bio->bi_iter.bi_sector, bio->bi_status, |
c3a3b19b | 629 | bbio->mirror_num); |
902b22f3 | 630 | |
8b8bbd46 QW |
631 | /* |
632 | * We always issue full-sector reads, but if some block in a | |
633 | * page fails to read, blk_update_request() will advance | |
634 | * bv_offset and adjust bv_len to compensate. Print a warning | |
635 | * for unaligned offsets, and an error if they don't add up to | |
636 | * a full sector. | |
637 | */ | |
638 | if (!IS_ALIGNED(bvec->bv_offset, sectorsize)) | |
639 | btrfs_err(fs_info, | |
640 | "partial page read in btrfs with offset %u and length %u", | |
641 | bvec->bv_offset, bvec->bv_len); | |
642 | else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len, | |
643 | sectorsize)) | |
644 | btrfs_info(fs_info, | |
645 | "incomplete page read with offset %u and length %u", | |
646 | bvec->bv_offset, bvec->bv_len); | |
647 | ||
648 | start = page_offset(page) + bvec->bv_offset; | |
649 | end = start + bvec->bv_len - 1; | |
facc8a22 | 650 | len = bvec->bv_len; |
d1310b2e | 651 | |
883d0de4 | 652 | if (likely(uptodate)) { |
a71754fc | 653 | loff_t i_size = i_size_read(inode); |
09cbfeaf | 654 | pgoff_t end_index = i_size >> PAGE_SHIFT; |
a71754fc | 655 | |
c28ea613 QW |
656 | /* |
657 | * Zero out the remaining part if this range straddles | |
658 | * i_size. | |
659 | * | |
660 | * Here we should only zero the range inside the bvec, | |
661 | * not touch anything else. | |
662 | * | |
663 | * NOTE: i_size is exclusive while end is inclusive. | |
664 | */ | |
665 | if (page->index == end_index && i_size <= end) { | |
666 | u32 zero_start = max(offset_in_page(i_size), | |
d2dcc8ed | 667 | offset_in_page(start)); |
c28ea613 QW |
668 | |
669 | zero_user_segment(page, zero_start, | |
670 | offset_in_page(end) + 1); | |
671 | } | |
70dec807 | 672 | } |
97861cd1 | 673 | |
7609afac CH |
674 | /* Update page status and unlock. */ |
675 | end_page_read(page, uptodate, start, len); | |
676 | endio_readpage_release_extent(&processed, BTRFS_I(inode), | |
31dd8c81 | 677 | start, end, uptodate); |
97861cd1 | 678 | |
7ffd27e3 QW |
679 | ASSERT(bio_offset + len > bio_offset); |
680 | bio_offset += len; | |
883d0de4 | 681 | |
2c30c71b | 682 | } |
94e8c95c QW |
683 | /* Release the last extent */ |
684 | endio_readpage_release_extent(&processed, NULL, 0, 0, false); | |
d1310b2e | 685 | bio_put(bio); |
d1310b2e CM |
686 | } |
687 | ||
43dd529a | 688 | /* |
dd137dd1 STD |
689 | * Populate every free slot in a provided array with pages. |
690 | * | |
691 | * @nr_pages: number of pages to allocate | |
692 | * @page_array: the array to fill with pages; any existing non-null entries in | |
693 | * the array will be skipped | |
694 | * | |
695 | * Return: 0 if all pages were able to be allocated; | |
696 | * -ENOMEM otherwise, and the caller is responsible for freeing all | |
697 | * non-null page pointers in the array. | |
698 | */ | |
699 | int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array) | |
700 | { | |
91d6ac1d | 701 | unsigned int allocated; |
dd137dd1 | 702 | |
91d6ac1d STD |
703 | for (allocated = 0; allocated < nr_pages;) { |
704 | unsigned int last = allocated; | |
dd137dd1 | 705 | |
91d6ac1d STD |
706 | allocated = alloc_pages_bulk_array(GFP_NOFS, nr_pages, page_array); |
707 | ||
395cb57e STD |
708 | if (allocated == nr_pages) |
709 | return 0; | |
710 | ||
91d6ac1d STD |
711 | /* |
712 | * During this iteration, no page could be allocated, even | |
713 | * though alloc_pages_bulk_array() falls back to alloc_page() | |
714 | * if it could not bulk-allocate. So we must be out of memory. | |
715 | */ | |
716 | if (allocated == last) | |
dd137dd1 | 717 | return -ENOMEM; |
395cb57e STD |
718 | |
719 | memalloc_retry_wait(GFP_NOFS); | |
dd137dd1 STD |
720 | } |
721 | return 0; | |
722 | } | |
723 | ||
78a2ef1b CH |
724 | static bool btrfs_bio_is_contig(struct btrfs_bio_ctrl *bio_ctrl, |
725 | struct page *page, u64 disk_bytenr, | |
726 | unsigned int pg_offset) | |
727 | { | |
9dfde1b4 | 728 | struct bio *bio = &bio_ctrl->bbio->bio; |
78a2ef1b CH |
729 | struct bio_vec *bvec = bio_last_bvec_all(bio); |
730 | const sector_t sector = disk_bytenr >> SECTOR_SHIFT; | |
731 | ||
732 | if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) { | |
733 | /* | |
734 | * For compression, all IO should have its logical bytenr set | |
735 | * to the starting bytenr of the compressed extent. | |
736 | */ | |
737 | return bio->bi_iter.bi_sector == sector; | |
738 | } | |
739 | ||
740 | /* | |
741 | * The contig check requires the following conditions to be met: | |
742 | * | |
743 | * 1) The pages are belonging to the same inode | |
744 | * This is implied by the call chain. | |
745 | * | |
746 | * 2) The range has adjacent logical bytenr | |
747 | * | |
748 | * 3) The range has adjacent file offset | |
749 | * This is required for the usage of btrfs_bio->file_offset. | |
750 | */ | |
751 | return bio_end_sector(bio) == sector && | |
752 | page_offset(bvec->bv_page) + bvec->bv_offset + bvec->bv_len == | |
753 | page_offset(page) + pg_offset; | |
754 | } | |
755 | ||
198bd49e JT |
756 | static void alloc_new_bio(struct btrfs_inode *inode, |
757 | struct btrfs_bio_ctrl *bio_ctrl, | |
758 | u64 disk_bytenr, u64 file_offset) | |
390ed29b | 759 | { |
198bd49e | 760 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
b41bbd29 | 761 | struct btrfs_bio *bbio; |
198bd49e | 762 | |
4317ff00 | 763 | bbio = btrfs_bio_alloc(BIO_MAX_VECS, bio_ctrl->opf, fs_info, |
b41bbd29 CH |
764 | bio_ctrl->end_io_func, NULL); |
765 | bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; | |
4317ff00 | 766 | bbio->inode = inode; |
b41bbd29 CH |
767 | bbio->file_offset = file_offset; |
768 | bio_ctrl->bbio = bbio; | |
198bd49e | 769 | bio_ctrl->len_to_oe_boundary = U32_MAX; |
390ed29b | 770 | |
a39da514 CH |
771 | /* Limit data write bios to the ordered boundary. */ |
772 | if (bio_ctrl->wbc) { | |
198bd49e JT |
773 | struct btrfs_ordered_extent *ordered; |
774 | ||
2380220e QW |
775 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
776 | if (ordered) { | |
777 | bio_ctrl->len_to_oe_boundary = min_t(u32, U32_MAX, | |
72fcf1a4 CH |
778 | ordered->file_offset + |
779 | ordered->disk_num_bytes - file_offset); | |
ec63b84d | 780 | bbio->ordered = ordered; |
2380220e | 781 | } |
390ed29b | 782 | |
50f1cff3 | 783 | /* |
d5e4377d CH |
784 | * Pick the last added device to support cgroup writeback. For |
785 | * multi-device file systems this means blk-cgroup policies have | |
786 | * to always be set on the last added/replaced device. | |
787 | * This is a bit odd but has been like that for a long time. | |
50f1cff3 | 788 | */ |
b41bbd29 CH |
789 | bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev); |
790 | wbc_init_bio(bio_ctrl->wbc, &bbio->bio); | |
e0eefe07 | 791 | } |
e0eefe07 QW |
792 | } |
793 | ||
4b81ba48 | 794 | /* |
0c64c33c | 795 | * @disk_bytenr: logical bytenr where the write will be |
209ecde5 | 796 | * @page: page to add to the bio |
0c64c33c | 797 | * @size: portion of page that we want to write to |
b8b3d625 DS |
798 | * @pg_offset: offset of the new bio or to check whether we are adding |
799 | * a contiguous page to the previous one | |
814b6f91 | 800 | * |
9dfde1b4 CH |
801 | * The will either add the page into the existing @bio_ctrl->bbio, or allocate a |
802 | * new one in @bio_ctrl->bbio. | |
814b6f91 QW |
803 | * The mirror number for this IO should already be initizlied in |
804 | * @bio_ctrl->mirror_num. | |
4b81ba48 | 805 | */ |
55173337 CH |
806 | static void submit_extent_page(struct btrfs_bio_ctrl *bio_ctrl, |
807 | u64 disk_bytenr, struct page *page, | |
808 | size_t size, unsigned long pg_offset) | |
d1310b2e | 809 | { |
e1326f03 | 810 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
5467abba | 811 | |
24e6c808 | 812 | ASSERT(pg_offset + size <= PAGE_SIZE); |
5467abba QW |
813 | ASSERT(bio_ctrl->end_io_func); |
814 | ||
9dfde1b4 | 815 | if (bio_ctrl->bbio && |
78a2ef1b CH |
816 | !btrfs_bio_is_contig(bio_ctrl, page, disk_bytenr, pg_offset)) |
817 | submit_one_bio(bio_ctrl); | |
818 | ||
24e6c808 CH |
819 | do { |
820 | u32 len = size; | |
e0eefe07 QW |
821 | |
822 | /* Allocate new bio if needed */ | |
9dfde1b4 | 823 | if (!bio_ctrl->bbio) { |
72b505dc | 824 | alloc_new_bio(inode, bio_ctrl, disk_bytenr, |
24e6c808 | 825 | page_offset(page) + pg_offset); |
e0eefe07 | 826 | } |
24e6c808 CH |
827 | |
828 | /* Cap to the current ordered extent boundary if there is one. */ | |
829 | if (len > bio_ctrl->len_to_oe_boundary) { | |
830 | ASSERT(bio_ctrl->compress_type == BTRFS_COMPRESS_NONE); | |
831 | ASSERT(is_data_inode(&inode->vfs_inode)); | |
832 | len = bio_ctrl->len_to_oe_boundary; | |
833 | } | |
834 | ||
9dfde1b4 | 835 | if (bio_add_page(&bio_ctrl->bbio->bio, page, len, pg_offset) != len) { |
24e6c808 | 836 | /* bio full: move on to a new one */ |
722c82ac | 837 | submit_one_bio(bio_ctrl); |
24e6c808 | 838 | continue; |
d1310b2e | 839 | } |
24e6c808 CH |
840 | |
841 | if (bio_ctrl->wbc) | |
842 | wbc_account_cgroup_owner(bio_ctrl->wbc, page, len); | |
843 | ||
844 | size -= len; | |
845 | pg_offset += len; | |
846 | disk_bytenr += len; | |
09c3717c CM |
847 | |
848 | /* | |
849 | * len_to_oe_boundary defaults to U32_MAX, which isn't page or | |
850 | * sector aligned. alloc_new_bio() then sets it to the end of | |
851 | * our ordered extent for writes into zoned devices. | |
852 | * | |
853 | * When len_to_oe_boundary is tracking an ordered extent, we | |
854 | * trust the ordered extent code to align things properly, and | |
855 | * the check above to cap our write to the ordered extent | |
856 | * boundary is correct. | |
857 | * | |
858 | * When len_to_oe_boundary is U32_MAX, the cap above would | |
859 | * result in a 4095 byte IO for the last page right before | |
860 | * we hit the bio limit of UINT_MAX. bio_add_page() has all | |
861 | * the checks required to make sure we don't overflow the bio, | |
862 | * and we should just ignore len_to_oe_boundary completely | |
863 | * unless we're using it to track an ordered extent. | |
864 | * | |
865 | * It's pretty hard to make a bio sized U32_MAX, but it can | |
866 | * happen when the page cache is able to feed us contiguous | |
867 | * pages for large extents. | |
868 | */ | |
869 | if (bio_ctrl->len_to_oe_boundary != U32_MAX) | |
870 | bio_ctrl->len_to_oe_boundary -= len; | |
24e6c808 CH |
871 | |
872 | /* Ordered extent boundary: move on to a new bio. */ | |
873 | if (bio_ctrl->len_to_oe_boundary == 0) | |
874 | submit_one_bio(bio_ctrl); | |
875 | } while (size); | |
d1310b2e CM |
876 | } |
877 | ||
760f991f QW |
878 | static int attach_extent_buffer_page(struct extent_buffer *eb, |
879 | struct page *page, | |
880 | struct btrfs_subpage *prealloc) | |
d1310b2e | 881 | { |
760f991f QW |
882 | struct btrfs_fs_info *fs_info = eb->fs_info; |
883 | int ret = 0; | |
884 | ||
0d01e247 QW |
885 | /* |
886 | * If the page is mapped to btree inode, we should hold the private | |
887 | * lock to prevent race. | |
888 | * For cloned or dummy extent buffers, their pages are not mapped and | |
889 | * will not race with any other ebs. | |
890 | */ | |
891 | if (page->mapping) | |
892 | lockdep_assert_held(&page->mapping->private_lock); | |
893 | ||
fbca46eb | 894 | if (fs_info->nodesize >= PAGE_SIZE) { |
760f991f QW |
895 | if (!PagePrivate(page)) |
896 | attach_page_private(page, eb); | |
897 | else | |
898 | WARN_ON(page->private != (unsigned long)eb); | |
899 | return 0; | |
900 | } | |
901 | ||
902 | /* Already mapped, just free prealloc */ | |
903 | if (PagePrivate(page)) { | |
904 | btrfs_free_subpage(prealloc); | |
905 | return 0; | |
906 | } | |
907 | ||
908 | if (prealloc) | |
909 | /* Has preallocated memory for subpage */ | |
910 | attach_page_private(page, prealloc); | |
d1b89bc0 | 911 | else |
760f991f QW |
912 | /* Do new allocation to attach subpage */ |
913 | ret = btrfs_attach_subpage(fs_info, page, | |
914 | BTRFS_SUBPAGE_METADATA); | |
915 | return ret; | |
d1310b2e CM |
916 | } |
917 | ||
32443de3 | 918 | int set_page_extent_mapped(struct page *page) |
d1310b2e | 919 | { |
32443de3 QW |
920 | struct btrfs_fs_info *fs_info; |
921 | ||
922 | ASSERT(page->mapping); | |
923 | ||
924 | if (PagePrivate(page)) | |
925 | return 0; | |
926 | ||
927 | fs_info = btrfs_sb(page->mapping->host->i_sb); | |
928 | ||
fbca46eb | 929 | if (btrfs_is_subpage(fs_info, page)) |
32443de3 QW |
930 | return btrfs_attach_subpage(fs_info, page, BTRFS_SUBPAGE_DATA); |
931 | ||
932 | attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE); | |
933 | return 0; | |
934 | } | |
935 | ||
936 | void clear_page_extent_mapped(struct page *page) | |
937 | { | |
938 | struct btrfs_fs_info *fs_info; | |
939 | ||
940 | ASSERT(page->mapping); | |
941 | ||
d1b89bc0 | 942 | if (!PagePrivate(page)) |
32443de3 QW |
943 | return; |
944 | ||
945 | fs_info = btrfs_sb(page->mapping->host->i_sb); | |
fbca46eb | 946 | if (btrfs_is_subpage(fs_info, page)) |
32443de3 QW |
947 | return btrfs_detach_subpage(fs_info, page); |
948 | ||
949 | detach_page_private(page); | |
d1310b2e CM |
950 | } |
951 | ||
125bac01 MX |
952 | static struct extent_map * |
953 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, | |
1a5ee1e6 | 954 | u64 start, u64 len, struct extent_map **em_cached) |
125bac01 MX |
955 | { |
956 | struct extent_map *em; | |
957 | ||
958 | if (em_cached && *em_cached) { | |
959 | em = *em_cached; | |
cbc0e928 | 960 | if (extent_map_in_tree(em) && start >= em->start && |
125bac01 | 961 | start < extent_map_end(em)) { |
490b54d6 | 962 | refcount_inc(&em->refs); |
125bac01 MX |
963 | return em; |
964 | } | |
965 | ||
966 | free_extent_map(em); | |
967 | *em_cached = NULL; | |
968 | } | |
969 | ||
1a5ee1e6 | 970 | em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, start, len); |
c0347550 | 971 | if (em_cached && !IS_ERR(em)) { |
125bac01 | 972 | BUG_ON(*em_cached); |
490b54d6 | 973 | refcount_inc(&em->refs); |
125bac01 MX |
974 | *em_cached = em; |
975 | } | |
976 | return em; | |
977 | } | |
d1310b2e CM |
978 | /* |
979 | * basic readpage implementation. Locked extent state structs are inserted | |
980 | * into the tree that are removed when the IO is done (by the end_io | |
981 | * handlers) | |
79787eaa | 982 | * XXX JDM: This needs looking at to ensure proper page locking |
baf863b9 | 983 | * return 0 on success, otherwise return error |
d1310b2e | 984 | */ |
7aab8b32 | 985 | static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, |
c000bc04 | 986 | struct btrfs_bio_ctrl *bio_ctrl, u64 *prev_em_start) |
d1310b2e CM |
987 | { |
988 | struct inode *inode = page->mapping->host; | |
92082d40 | 989 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4eee4fa4 | 990 | u64 start = page_offset(page); |
8eec8296 | 991 | const u64 end = start + PAGE_SIZE - 1; |
d1310b2e CM |
992 | u64 cur = start; |
993 | u64 extent_offset; | |
994 | u64 last_byte = i_size_read(inode); | |
995 | u64 block_start; | |
d1310b2e | 996 | struct extent_map *em; |
baf863b9 | 997 | int ret = 0; |
306e16ce | 998 | size_t pg_offset = 0; |
d1310b2e CM |
999 | size_t iosize; |
1000 | size_t blocksize = inode->i_sb->s_blocksize; | |
f657a31c | 1001 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
ae6957eb | 1002 | |
32443de3 QW |
1003 | ret = set_page_extent_mapped(page); |
1004 | if (ret < 0) { | |
570eb97b | 1005 | unlock_extent(tree, start, end, NULL); |
92082d40 | 1006 | unlock_page(page); |
55173337 | 1007 | return ret; |
32443de3 | 1008 | } |
d1310b2e | 1009 | |
09cbfeaf | 1010 | if (page->index == last_byte >> PAGE_SHIFT) { |
7073017a | 1011 | size_t zero_offset = offset_in_page(last_byte); |
c8b97818 CM |
1012 | |
1013 | if (zero_offset) { | |
09cbfeaf | 1014 | iosize = PAGE_SIZE - zero_offset; |
d048b9c2 | 1015 | memzero_page(page, zero_offset, iosize); |
c8b97818 CM |
1016 | } |
1017 | } | |
5467abba | 1018 | bio_ctrl->end_io_func = end_bio_extent_readpage; |
92082d40 | 1019 | begin_page_read(fs_info, page); |
d1310b2e | 1020 | while (cur <= end) { |
a140453b | 1021 | enum btrfs_compression_type compress_type = BTRFS_COMPRESS_NONE; |
005efedf | 1022 | bool force_bio_submit = false; |
0c64c33c | 1023 | u64 disk_bytenr; |
c8f2f24b | 1024 | |
6a404910 | 1025 | ASSERT(IS_ALIGNED(cur, fs_info->sectorsize)); |
d1310b2e | 1026 | if (cur >= last_byte) { |
09cbfeaf | 1027 | iosize = PAGE_SIZE - pg_offset; |
d048b9c2 | 1028 | memzero_page(page, pg_offset, iosize); |
2c8f5e8c | 1029 | unlock_extent(tree, cur, cur + iosize - 1, NULL); |
92082d40 | 1030 | end_page_read(page, true, cur, iosize); |
d1310b2e CM |
1031 | break; |
1032 | } | |
125bac01 | 1033 | em = __get_extent_map(inode, page, pg_offset, cur, |
1a5ee1e6 | 1034 | end - cur + 1, em_cached); |
c0347550 | 1035 | if (IS_ERR(em)) { |
570eb97b | 1036 | unlock_extent(tree, cur, end, NULL); |
92082d40 | 1037 | end_page_read(page, false, cur, end + 1 - cur); |
55173337 | 1038 | return PTR_ERR(em); |
d1310b2e | 1039 | } |
d1310b2e CM |
1040 | extent_offset = cur - em->start; |
1041 | BUG_ON(extent_map_end(em) <= cur); | |
1042 | BUG_ON(end < cur); | |
1043 | ||
7f6ca7f2 | 1044 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
a140453b | 1045 | compress_type = em->compress_type; |
c8b97818 | 1046 | |
d1310b2e | 1047 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
fda2832f | 1048 | iosize = ALIGN(iosize, blocksize); |
a140453b | 1049 | if (compress_type != BTRFS_COMPRESS_NONE) |
0c64c33c | 1050 | disk_bytenr = em->block_start; |
949b3273 | 1051 | else |
0c64c33c | 1052 | disk_bytenr = em->block_start + extent_offset; |
d1310b2e | 1053 | block_start = em->block_start; |
d899e052 YZ |
1054 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
1055 | block_start = EXTENT_MAP_HOLE; | |
005efedf FM |
1056 | |
1057 | /* | |
1058 | * If we have a file range that points to a compressed extent | |
260db43c | 1059 | * and it's followed by a consecutive file range that points |
005efedf FM |
1060 | * to the same compressed extent (possibly with a different |
1061 | * offset and/or length, so it either points to the whole extent | |
1062 | * or only part of it), we must make sure we do not submit a | |
1063 | * single bio to populate the pages for the 2 ranges because | |
1064 | * this makes the compressed extent read zero out the pages | |
1065 | * belonging to the 2nd range. Imagine the following scenario: | |
1066 | * | |
1067 | * File layout | |
1068 | * [0 - 8K] [8K - 24K] | |
1069 | * | | | |
1070 | * | | | |
1071 | * points to extent X, points to extent X, | |
1072 | * offset 4K, length of 8K offset 0, length 16K | |
1073 | * | |
1074 | * [extent X, compressed length = 4K uncompressed length = 16K] | |
1075 | * | |
1076 | * If the bio to read the compressed extent covers both ranges, | |
1077 | * it will decompress extent X into the pages belonging to the | |
1078 | * first range and then it will stop, zeroing out the remaining | |
1079 | * pages that belong to the other range that points to extent X. | |
1080 | * So here we make sure we submit 2 bios, one for the first | |
1081 | * range and another one for the third range. Both will target | |
1082 | * the same physical extent from disk, but we can't currently | |
1083 | * make the compressed bio endio callback populate the pages | |
1084 | * for both ranges because each compressed bio is tightly | |
1085 | * coupled with a single extent map, and each range can have | |
1086 | * an extent map with a different offset value relative to the | |
1087 | * uncompressed data of our extent and different lengths. This | |
1088 | * is a corner case so we prioritize correctness over | |
1089 | * non-optimal behavior (submitting 2 bios for the same extent). | |
1090 | */ | |
1091 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && | |
1092 | prev_em_start && *prev_em_start != (u64)-1 && | |
8e928218 | 1093 | *prev_em_start != em->start) |
005efedf FM |
1094 | force_bio_submit = true; |
1095 | ||
1096 | if (prev_em_start) | |
8e928218 | 1097 | *prev_em_start = em->start; |
005efedf | 1098 | |
d1310b2e CM |
1099 | free_extent_map(em); |
1100 | em = NULL; | |
1101 | ||
1102 | /* we've found a hole, just zero and go on */ | |
1103 | if (block_start == EXTENT_MAP_HOLE) { | |
d048b9c2 | 1104 | memzero_page(page, pg_offset, iosize); |
d1310b2e | 1105 | |
2c8f5e8c | 1106 | unlock_extent(tree, cur, cur + iosize - 1, NULL); |
92082d40 | 1107 | end_page_read(page, true, cur, iosize); |
d1310b2e | 1108 | cur = cur + iosize; |
306e16ce | 1109 | pg_offset += iosize; |
d1310b2e CM |
1110 | continue; |
1111 | } | |
1112 | /* the get_extent function already copied into the page */ | |
70dec807 | 1113 | if (block_start == EXTENT_MAP_INLINE) { |
570eb97b | 1114 | unlock_extent(tree, cur, cur + iosize - 1, NULL); |
52b029f4 | 1115 | end_page_read(page, true, cur, iosize); |
70dec807 | 1116 | cur = cur + iosize; |
306e16ce | 1117 | pg_offset += iosize; |
70dec807 CM |
1118 | continue; |
1119 | } | |
d1310b2e | 1120 | |
f8ed4852 | 1121 | if (bio_ctrl->compress_type != compress_type) { |
c9bc621f | 1122 | submit_one_bio(bio_ctrl); |
f8ed4852 CH |
1123 | bio_ctrl->compress_type = compress_type; |
1124 | } | |
c9bc621f | 1125 | |
eb8d0c6d CH |
1126 | if (force_bio_submit) |
1127 | submit_one_bio(bio_ctrl); | |
55173337 CH |
1128 | submit_extent_page(bio_ctrl, disk_bytenr, page, iosize, |
1129 | pg_offset); | |
d1310b2e | 1130 | cur = cur + iosize; |
306e16ce | 1131 | pg_offset += iosize; |
d1310b2e | 1132 | } |
55173337 CH |
1133 | |
1134 | return 0; | |
d1310b2e CM |
1135 | } |
1136 | ||
fdaf9a58 | 1137 | int btrfs_read_folio(struct file *file, struct folio *folio) |
7aab8b32 | 1138 | { |
fdaf9a58 | 1139 | struct page *page = &folio->page; |
7aab8b32 CH |
1140 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
1141 | u64 start = page_offset(page); | |
1142 | u64 end = start + PAGE_SIZE - 1; | |
c000bc04 | 1143 | struct btrfs_bio_ctrl bio_ctrl = { .opf = REQ_OP_READ }; |
7aab8b32 CH |
1144 | int ret; |
1145 | ||
1146 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); | |
1147 | ||
c000bc04 | 1148 | ret = btrfs_do_readpage(page, NULL, &bio_ctrl, NULL); |
7aab8b32 CH |
1149 | /* |
1150 | * If btrfs_do_readpage() failed we will want to submit the assembled | |
1151 | * bio to do the cleanup. | |
1152 | */ | |
722c82ac | 1153 | submit_one_bio(&bio_ctrl); |
7aab8b32 CH |
1154 | return ret; |
1155 | } | |
1156 | ||
b6660e80 | 1157 | static inline void contiguous_readpages(struct page *pages[], int nr_pages, |
390ed29b QW |
1158 | u64 start, u64 end, |
1159 | struct extent_map **em_cached, | |
1160 | struct btrfs_bio_ctrl *bio_ctrl, | |
1161 | u64 *prev_em_start) | |
9974090b | 1162 | { |
23d31bd4 | 1163 | struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host); |
9974090b MX |
1164 | int index; |
1165 | ||
b272ae22 | 1166 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
9974090b MX |
1167 | |
1168 | for (index = 0; index < nr_pages; index++) { | |
390ed29b | 1169 | btrfs_do_readpage(pages[index], em_cached, bio_ctrl, |
c000bc04 | 1170 | prev_em_start); |
09cbfeaf | 1171 | put_page(pages[index]); |
9974090b MX |
1172 | } |
1173 | } | |
1174 | ||
d1310b2e | 1175 | /* |
40f76580 CM |
1176 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
1177 | * | |
5eaad97a | 1178 | * This returns 1 if btrfs_run_delalloc_range function did all the work required |
40f76580 CM |
1179 | * to write the page (copy into inline extent). In this case the IO has |
1180 | * been started and the page is already unlocked. | |
1181 | * | |
1182 | * This returns 0 if all went well (page still locked) | |
1183 | * This returns < 0 if there were errors (page still locked) | |
d1310b2e | 1184 | */ |
cd4c0bf9 | 1185 | static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode, |
83f1b680 | 1186 | struct page *page, struct writeback_control *wbc) |
40f76580 | 1187 | { |
2749f7ef | 1188 | const u64 page_end = page_offset(page) + PAGE_SIZE - 1; |
cf3075fb | 1189 | u64 delalloc_start = page_offset(page); |
40f76580 | 1190 | u64 delalloc_to_write = 0; |
83f1b680 QW |
1191 | /* How many pages are started by btrfs_run_delalloc_range() */ |
1192 | unsigned long nr_written = 0; | |
40f76580 CM |
1193 | int ret; |
1194 | int page_started = 0; | |
1195 | ||
2749f7ef QW |
1196 | while (delalloc_start < page_end) { |
1197 | u64 delalloc_end = page_end; | |
1198 | bool found; | |
40f76580 | 1199 | |
cd4c0bf9 | 1200 | found = find_lock_delalloc_range(&inode->vfs_inode, page, |
40f76580 | 1201 | &delalloc_start, |
917aacec | 1202 | &delalloc_end); |
3522e903 | 1203 | if (!found) { |
40f76580 CM |
1204 | delalloc_start = delalloc_end + 1; |
1205 | continue; | |
1206 | } | |
cd4c0bf9 | 1207 | ret = btrfs_run_delalloc_range(inode, page, delalloc_start, |
83f1b680 | 1208 | delalloc_end, &page_started, &nr_written, wbc); |
2b2553f1 | 1209 | if (ret) |
7361b4ae | 1210 | return ret; |
2b2553f1 | 1211 | |
40f76580 | 1212 | /* |
ea1754a0 KS |
1213 | * delalloc_end is already one less than the total length, so |
1214 | * we don't subtract one from PAGE_SIZE | |
40f76580 CM |
1215 | */ |
1216 | delalloc_to_write += (delalloc_end - delalloc_start + | |
ea1754a0 | 1217 | PAGE_SIZE) >> PAGE_SHIFT; |
40f76580 CM |
1218 | delalloc_start = delalloc_end + 1; |
1219 | } | |
1220 | if (wbc->nr_to_write < delalloc_to_write) { | |
1221 | int thresh = 8192; | |
1222 | ||
1223 | if (delalloc_to_write < thresh * 2) | |
1224 | thresh = delalloc_to_write; | |
1225 | wbc->nr_to_write = min_t(u64, delalloc_to_write, | |
1226 | thresh); | |
1227 | } | |
1228 | ||
83f1b680 | 1229 | /* Did btrfs_run_dealloc_range() already unlock and start the IO? */ |
40f76580 CM |
1230 | if (page_started) { |
1231 | /* | |
83f1b680 QW |
1232 | * We've unlocked the page, so we can't update the mapping's |
1233 | * writeback index, just update nr_to_write. | |
40f76580 | 1234 | */ |
83f1b680 | 1235 | wbc->nr_to_write -= nr_written; |
40f76580 CM |
1236 | return 1; |
1237 | } | |
1238 | ||
b69d1ee9 | 1239 | return 0; |
40f76580 CM |
1240 | } |
1241 | ||
c5ef5c6c QW |
1242 | /* |
1243 | * Find the first byte we need to write. | |
1244 | * | |
1245 | * For subpage, one page can contain several sectors, and | |
1246 | * __extent_writepage_io() will just grab all extent maps in the page | |
1247 | * range and try to submit all non-inline/non-compressed extents. | |
1248 | * | |
1249 | * This is a big problem for subpage, we shouldn't re-submit already written | |
1250 | * data at all. | |
1251 | * This function will lookup subpage dirty bit to find which range we really | |
1252 | * need to submit. | |
1253 | * | |
1254 | * Return the next dirty range in [@start, @end). | |
1255 | * If no dirty range is found, @start will be page_offset(page) + PAGE_SIZE. | |
1256 | */ | |
1257 | static void find_next_dirty_byte(struct btrfs_fs_info *fs_info, | |
1258 | struct page *page, u64 *start, u64 *end) | |
1259 | { | |
1260 | struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private; | |
72a69cd0 | 1261 | struct btrfs_subpage_info *spi = fs_info->subpage_info; |
c5ef5c6c QW |
1262 | u64 orig_start = *start; |
1263 | /* Declare as unsigned long so we can use bitmap ops */ | |
c5ef5c6c | 1264 | unsigned long flags; |
72a69cd0 | 1265 | int range_start_bit; |
c5ef5c6c QW |
1266 | int range_end_bit; |
1267 | ||
1268 | /* | |
1269 | * For regular sector size == page size case, since one page only | |
1270 | * contains one sector, we return the page offset directly. | |
1271 | */ | |
fbca46eb | 1272 | if (!btrfs_is_subpage(fs_info, page)) { |
c5ef5c6c QW |
1273 | *start = page_offset(page); |
1274 | *end = page_offset(page) + PAGE_SIZE; | |
1275 | return; | |
1276 | } | |
1277 | ||
72a69cd0 QW |
1278 | range_start_bit = spi->dirty_offset + |
1279 | (offset_in_page(orig_start) >> fs_info->sectorsize_bits); | |
1280 | ||
c5ef5c6c QW |
1281 | /* We should have the page locked, but just in case */ |
1282 | spin_lock_irqsave(&subpage->lock, flags); | |
72a69cd0 QW |
1283 | bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit, |
1284 | spi->dirty_offset + spi->bitmap_nr_bits); | |
c5ef5c6c QW |
1285 | spin_unlock_irqrestore(&subpage->lock, flags); |
1286 | ||
72a69cd0 QW |
1287 | range_start_bit -= spi->dirty_offset; |
1288 | range_end_bit -= spi->dirty_offset; | |
1289 | ||
c5ef5c6c QW |
1290 | *start = page_offset(page) + range_start_bit * fs_info->sectorsize; |
1291 | *end = page_offset(page) + range_end_bit * fs_info->sectorsize; | |
1292 | } | |
1293 | ||
40f76580 CM |
1294 | /* |
1295 | * helper for __extent_writepage. This calls the writepage start hooks, | |
1296 | * and does the loop to map the page into extents and bios. | |
1297 | * | |
1298 | * We return 1 if the IO is started and the page is unlocked, | |
1299 | * 0 if all went well (page still locked) | |
1300 | * < 0 if there were errors (page still locked) | |
1301 | */ | |
d4580fe2 | 1302 | static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, |
40f76580 | 1303 | struct page *page, |
ee5f017d | 1304 | struct btrfs_bio_ctrl *bio_ctrl, |
40f76580 | 1305 | loff_t i_size, |
57e5ffeb | 1306 | int *nr_ret) |
d1310b2e | 1307 | { |
6bc5636a | 1308 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
a129ffb8 QW |
1309 | u64 cur = page_offset(page); |
1310 | u64 end = cur + PAGE_SIZE - 1; | |
d1310b2e | 1311 | u64 extent_offset; |
d1310b2e | 1312 | u64 block_start; |
d1310b2e | 1313 | struct extent_map *em; |
40f76580 CM |
1314 | int ret = 0; |
1315 | int nr = 0; | |
c8b97818 | 1316 | |
a129ffb8 | 1317 | ret = btrfs_writepage_cow_fixup(page); |
d75855b4 NB |
1318 | if (ret) { |
1319 | /* Fixup worker will requeue */ | |
72b505dc | 1320 | redirty_page_for_writepage(bio_ctrl->wbc, page); |
d75855b4 NB |
1321 | unlock_page(page); |
1322 | return 1; | |
247e743c CM |
1323 | } |
1324 | ||
ee5f017d | 1325 | bio_ctrl->end_io_func = end_bio_extent_writepage; |
d1310b2e | 1326 | while (cur <= end) { |
6648cedd | 1327 | u32 len = end - cur + 1; |
0c64c33c | 1328 | u64 disk_bytenr; |
40f76580 | 1329 | u64 em_end; |
c5ef5c6c QW |
1330 | u64 dirty_range_start = cur; |
1331 | u64 dirty_range_end; | |
6bc5636a | 1332 | u32 iosize; |
58409edd | 1333 | |
40f76580 | 1334 | if (cur >= i_size) { |
6648cedd CH |
1335 | btrfs_mark_ordered_io_finished(inode, page, cur, len, |
1336 | true); | |
cc1d0d93 QW |
1337 | /* |
1338 | * This range is beyond i_size, thus we don't need to | |
1339 | * bother writing back. | |
1340 | * But we still need to clear the dirty subpage bit, or | |
1341 | * the next time the page gets dirtied, we will try to | |
1342 | * writeback the sectors with subpage dirty bits, | |
1343 | * causing writeback without ordered extent. | |
1344 | */ | |
6648cedd | 1345 | btrfs_page_clear_dirty(fs_info, page, cur, len); |
d1310b2e CM |
1346 | break; |
1347 | } | |
c5ef5c6c QW |
1348 | |
1349 | find_next_dirty_byte(fs_info, page, &dirty_range_start, | |
1350 | &dirty_range_end); | |
1351 | if (cur < dirty_range_start) { | |
1352 | cur = dirty_range_start; | |
1353 | continue; | |
1354 | } | |
1355 | ||
6648cedd | 1356 | em = btrfs_get_extent(inode, NULL, 0, cur, len); |
c0347550 | 1357 | if (IS_ERR(em)) { |
61391d56 | 1358 | ret = PTR_ERR_OR_ZERO(em); |
5380311f | 1359 | goto out_error; |
d1310b2e CM |
1360 | } |
1361 | ||
1362 | extent_offset = cur - em->start; | |
40f76580 | 1363 | em_end = extent_map_end(em); |
6bc5636a QW |
1364 | ASSERT(cur <= em_end); |
1365 | ASSERT(cur < end); | |
1366 | ASSERT(IS_ALIGNED(em->start, fs_info->sectorsize)); | |
1367 | ASSERT(IS_ALIGNED(em->len, fs_info->sectorsize)); | |
f22b5dcb | 1368 | |
d1310b2e | 1369 | block_start = em->block_start; |
6bc5636a QW |
1370 | disk_bytenr = em->block_start + extent_offset; |
1371 | ||
f22b5dcb CH |
1372 | ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)); |
1373 | ASSERT(block_start != EXTENT_MAP_HOLE); | |
1374 | ASSERT(block_start != EXTENT_MAP_INLINE); | |
1375 | ||
c5ef5c6c QW |
1376 | /* |
1377 | * Note that em_end from extent_map_end() and dirty_range_end from | |
1378 | * find_next_dirty_byte() are all exclusive | |
1379 | */ | |
1380 | iosize = min(min(em_end, end + 1), dirty_range_end) - cur; | |
d1310b2e CM |
1381 | free_extent_map(em); |
1382 | em = NULL; | |
1383 | ||
d2a91064 | 1384 | btrfs_set_range_writeback(inode, cur, cur + iosize - 1); |
58409edd | 1385 | if (!PageWriteback(page)) { |
d4580fe2 | 1386 | btrfs_err(inode->root->fs_info, |
58409edd DS |
1387 | "page %lu not writeback, cur %llu end %llu", |
1388 | page->index, cur, end); | |
d1310b2e | 1389 | } |
7f3c74fb | 1390 | |
c5ef5c6c QW |
1391 | /* |
1392 | * Although the PageDirty bit is cleared before entering this | |
1393 | * function, subpage dirty bit is not cleared. | |
1394 | * So clear subpage dirty bit here so next time we won't submit | |
1395 | * page for range already written to disk. | |
1396 | */ | |
1397 | btrfs_page_clear_dirty(fs_info, page, cur, iosize); | |
1398 | ||
55173337 CH |
1399 | submit_extent_page(bio_ctrl, disk_bytenr, page, iosize, |
1400 | cur - page_offset(page)); | |
6bc5636a | 1401 | cur += iosize; |
d1310b2e CM |
1402 | nr++; |
1403 | } | |
5380311f CH |
1404 | |
1405 | btrfs_page_assert_not_dirty(fs_info, page); | |
1406 | *nr_ret = nr; | |
1407 | return 0; | |
1408 | ||
1409 | out_error: | |
cc1d0d93 QW |
1410 | /* |
1411 | * If we finish without problem, we should not only clear page dirty, | |
1412 | * but also empty subpage dirty bits | |
1413 | */ | |
40f76580 | 1414 | *nr_ret = nr; |
40f76580 CM |
1415 | return ret; |
1416 | } | |
1417 | ||
1418 | /* | |
1419 | * the writepage semantics are similar to regular writepage. extent | |
1420 | * records are inserted to lock ranges in the tree, and as dirty areas | |
1421 | * are found, they are marked writeback. Then the lock bits are removed | |
1422 | * and the end_io handler clears the writeback ranges | |
3065976b QW |
1423 | * |
1424 | * Return 0 if everything goes well. | |
1425 | * Return <0 for error. | |
40f76580 | 1426 | */ |
72b505dc | 1427 | static int __extent_writepage(struct page *page, struct btrfs_bio_ctrl *bio_ctrl) |
40f76580 | 1428 | { |
8e1dec8e | 1429 | struct folio *folio = page_folio(page); |
40f76580 | 1430 | struct inode *inode = page->mapping->host; |
cf3075fb | 1431 | const u64 page_start = page_offset(page); |
40f76580 CM |
1432 | int ret; |
1433 | int nr = 0; | |
eb70d222 | 1434 | size_t pg_offset; |
40f76580 | 1435 | loff_t i_size = i_size_read(inode); |
09cbfeaf | 1436 | unsigned long end_index = i_size >> PAGE_SHIFT; |
40f76580 | 1437 | |
72b505dc | 1438 | trace___extent_writepage(page, inode, bio_ctrl->wbc); |
40f76580 CM |
1439 | |
1440 | WARN_ON(!PageLocked(page)); | |
1441 | ||
7073017a | 1442 | pg_offset = offset_in_page(i_size); |
40f76580 CM |
1443 | if (page->index > end_index || |
1444 | (page->index == end_index && !pg_offset)) { | |
8e1dec8e MWO |
1445 | folio_invalidate(folio, 0, folio_size(folio)); |
1446 | folio_unlock(folio); | |
40f76580 CM |
1447 | return 0; |
1448 | } | |
1449 | ||
21a8935e | 1450 | if (page->index == end_index) |
d048b9c2 | 1451 | memzero_page(page, pg_offset, PAGE_SIZE - pg_offset); |
40f76580 | 1452 | |
32443de3 | 1453 | ret = set_page_extent_mapped(page); |
2b2553f1 | 1454 | if (ret < 0) |
32443de3 | 1455 | goto done; |
40f76580 | 1456 | |
eb34dcea CH |
1457 | ret = writepage_delalloc(BTRFS_I(inode), page, bio_ctrl->wbc); |
1458 | if (ret == 1) | |
1459 | return 0; | |
1460 | if (ret) | |
1461 | goto done; | |
40f76580 | 1462 | |
72b505dc | 1463 | ret = __extent_writepage_io(BTRFS_I(inode), page, bio_ctrl, i_size, &nr); |
40f76580 | 1464 | if (ret == 1) |
169d2c87 | 1465 | return 0; |
40f76580 | 1466 | |
9ecdbee8 CH |
1467 | bio_ctrl->wbc->nr_to_write--; |
1468 | ||
d1310b2e CM |
1469 | done: |
1470 | if (nr == 0) { | |
1471 | /* make sure the mapping tag for page dirty gets cleared */ | |
1472 | set_page_writeback(page); | |
1473 | end_page_writeback(page); | |
1474 | } | |
9783e4de CH |
1475 | if (ret) { |
1476 | btrfs_mark_ordered_io_finished(BTRFS_I(inode), page, page_start, | |
1477 | PAGE_SIZE, !ret); | |
1478 | btrfs_page_clear_uptodate(btrfs_sb(inode->i_sb), page, | |
1479 | page_start, PAGE_SIZE); | |
1480 | mapping_set_error(page->mapping, ret); | |
1481 | } | |
eb34dcea | 1482 | unlock_page(page); |
3065976b | 1483 | ASSERT(ret <= 0); |
40f76580 | 1484 | return ret; |
d1310b2e CM |
1485 | } |
1486 | ||
fd8b2b61 | 1487 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
0b32f4bb | 1488 | { |
74316201 N |
1489 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
1490 | TASK_UNINTERRUPTIBLE); | |
0b32f4bb JB |
1491 | } |
1492 | ||
2e3c2513 | 1493 | /* |
a3efb2f0 | 1494 | * Lock extent buffer status and pages for writeback. |
2e3c2513 | 1495 | * |
9fdd1601 CH |
1496 | * Return %false if the extent buffer doesn't need to be submitted (e.g. the |
1497 | * extent buffer is not dirty) | |
1498 | * Return %true is the extent buffer is submitted to bio. | |
2e3c2513 | 1499 | */ |
9fdd1601 | 1500 | static noinline_for_stack bool lock_extent_buffer_for_io(struct extent_buffer *eb, |
50b21d7a | 1501 | struct writeback_control *wbc) |
0b32f4bb | 1502 | { |
9df76fb5 | 1503 | struct btrfs_fs_info *fs_info = eb->fs_info; |
9fdd1601 | 1504 | bool ret = false; |
0b32f4bb | 1505 | |
50b21d7a CH |
1506 | btrfs_tree_lock(eb); |
1507 | while (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { | |
0b32f4bb | 1508 | btrfs_tree_unlock(eb); |
50b21d7a | 1509 | if (wbc->sync_mode != WB_SYNC_ALL) |
9fdd1601 | 1510 | return false; |
50b21d7a CH |
1511 | wait_on_extent_buffer_writeback(eb); |
1512 | btrfs_tree_lock(eb); | |
0b32f4bb JB |
1513 | } |
1514 | ||
51561ffe JB |
1515 | /* |
1516 | * We need to do this to prevent races in people who check if the eb is | |
1517 | * under IO since we can end up having no IO bits set for a short period | |
1518 | * of time. | |
1519 | */ | |
1520 | spin_lock(&eb->refs_lock); | |
0b32f4bb JB |
1521 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
1522 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); | |
51561ffe | 1523 | spin_unlock(&eb->refs_lock); |
0b32f4bb | 1524 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
104b4e51 NB |
1525 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
1526 | -eb->len, | |
1527 | fs_info->dirty_metadata_batch); | |
9fdd1601 | 1528 | ret = true; |
51561ffe JB |
1529 | } else { |
1530 | spin_unlock(&eb->refs_lock); | |
0b32f4bb | 1531 | } |
0b32f4bb | 1532 | btrfs_tree_unlock(eb); |
2e3c2513 | 1533 | return ret; |
0b32f4bb JB |
1534 | } |
1535 | ||
cd88a4fd | 1536 | static void set_btree_ioerr(struct extent_buffer *eb) |
656f30db | 1537 | { |
5a2c6075 | 1538 | struct btrfs_fs_info *fs_info = eb->fs_info; |
656f30db | 1539 | |
cd88a4fd | 1540 | set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); |
656f30db | 1541 | |
c2e39305 JB |
1542 | /* |
1543 | * A read may stumble upon this buffer later, make sure that it gets an | |
1544 | * error and knows there was an error. | |
1545 | */ | |
1546 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); | |
1547 | ||
68b85589 JB |
1548 | /* |
1549 | * We need to set the mapping with the io error as well because a write | |
1550 | * error will flip the file system readonly, and then syncfs() will | |
1551 | * return a 0 because we are readonly if we don't modify the err seq for | |
1552 | * the superblock. | |
1553 | */ | |
cd88a4fd | 1554 | mapping_set_error(eb->fs_info->btree_inode->i_mapping, -EIO); |
68b85589 | 1555 | |
656f30db FM |
1556 | /* |
1557 | * If writeback for a btree extent that doesn't belong to a log tree | |
1558 | * failed, increment the counter transaction->eb_write_errors. | |
1559 | * We do this because while the transaction is running and before it's | |
1560 | * committing (when we call filemap_fdata[write|wait]_range against | |
1561 | * the btree inode), we might have | |
1562 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it | |
1563 | * returns an error or an error happens during writeback, when we're | |
1564 | * committing the transaction we wouldn't know about it, since the pages | |
1565 | * can be no longer dirty nor marked anymore for writeback (if a | |
1566 | * subsequent modification to the extent buffer didn't happen before the | |
1567 | * transaction commit), which makes filemap_fdata[write|wait]_range not | |
1568 | * able to find the pages tagged with SetPageError at transaction | |
1569 | * commit time. So if this happens we must abort the transaction, | |
1570 | * otherwise we commit a super block with btree roots that point to | |
1571 | * btree nodes/leafs whose content on disk is invalid - either garbage | |
1572 | * or the content of some node/leaf from a past generation that got | |
1573 | * cowed or deleted and is no longer valid. | |
1574 | * | |
1575 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would | |
1576 | * not be enough - we need to distinguish between log tree extents vs | |
1577 | * non-log tree extents, and the next filemap_fdatawait_range() call | |
1578 | * will catch and clear such errors in the mapping - and that call might | |
1579 | * be from a log sync and not from a transaction commit. Also, checking | |
1580 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is | |
1581 | * not done and would not be reliable - the eb might have been released | |
1582 | * from memory and reading it back again means that flag would not be | |
1583 | * set (since it's a runtime flag, not persisted on disk). | |
1584 | * | |
1585 | * Using the flags below in the btree inode also makes us achieve the | |
1586 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started | |
1587 | * writeback for all dirty pages and before filemap_fdatawait_range() | |
1588 | * is called, the writeback for all dirty pages had already finished | |
1589 | * with errors - because we were not using AS_EIO/AS_ENOSPC, | |
1590 | * filemap_fdatawait_range() would return success, as it could not know | |
1591 | * that writeback errors happened (the pages were no longer tagged for | |
1592 | * writeback). | |
1593 | */ | |
1594 | switch (eb->log_index) { | |
1595 | case -1: | |
5a2c6075 | 1596 | set_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags); |
656f30db FM |
1597 | break; |
1598 | case 0: | |
5a2c6075 | 1599 | set_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); |
656f30db FM |
1600 | break; |
1601 | case 1: | |
5a2c6075 | 1602 | set_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); |
656f30db FM |
1603 | break; |
1604 | default: | |
1605 | BUG(); /* unexpected, logic error */ | |
1606 | } | |
1607 | } | |
1608 | ||
2f3186d8 QW |
1609 | /* |
1610 | * The endio specific version which won't touch any unsafe spinlock in endio | |
1611 | * context. | |
1612 | */ | |
1613 | static struct extent_buffer *find_extent_buffer_nolock( | |
1614 | struct btrfs_fs_info *fs_info, u64 start) | |
1615 | { | |
1616 | struct extent_buffer *eb; | |
1617 | ||
1618 | rcu_read_lock(); | |
01cd3909 DS |
1619 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
1620 | start >> fs_info->sectorsize_bits); | |
2f3186d8 QW |
1621 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
1622 | rcu_read_unlock(); | |
1623 | return eb; | |
1624 | } | |
1625 | rcu_read_unlock(); | |
1626 | return NULL; | |
1627 | } | |
1628 | ||
cd88a4fd | 1629 | static void extent_buffer_write_end_io(struct btrfs_bio *bbio) |
2f3186d8 | 1630 | { |
cd88a4fd CH |
1631 | struct extent_buffer *eb = bbio->private; |
1632 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
1633 | bool uptodate = !bbio->bio.bi_status; | |
2f3186d8 | 1634 | struct bvec_iter_all iter_all; |
cd88a4fd CH |
1635 | struct bio_vec *bvec; |
1636 | u32 bio_offset = 0; | |
2f3186d8 | 1637 | |
cd88a4fd CH |
1638 | if (!uptodate) |
1639 | set_btree_ioerr(eb); | |
fa04c165 | 1640 | |
cd88a4fd CH |
1641 | bio_for_each_segment_all(bvec, &bbio->bio, iter_all) { |
1642 | u64 start = eb->start + bio_offset; | |
2f3186d8 | 1643 | struct page *page = bvec->bv_page; |
cd88a4fd | 1644 | u32 len = bvec->bv_len; |
2f3186d8 | 1645 | |
011134f4 | 1646 | if (!uptodate) |
cd88a4fd | 1647 | btrfs_page_clear_uptodate(fs_info, page, start, len); |
cd88a4fd CH |
1648 | btrfs_page_clear_writeback(fs_info, page, start, len); |
1649 | bio_offset += len; | |
2f3186d8 | 1650 | } |
0b32f4bb | 1651 | |
cd88a4fd CH |
1652 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
1653 | smp_mb__after_atomic(); | |
1654 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); | |
0b32f4bb | 1655 | |
cd88a4fd | 1656 | bio_put(&bbio->bio); |
0b32f4bb JB |
1657 | } |
1658 | ||
fa04c165 QW |
1659 | static void prepare_eb_write(struct extent_buffer *eb) |
1660 | { | |
1661 | u32 nritems; | |
1662 | unsigned long start; | |
1663 | unsigned long end; | |
1664 | ||
1665 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); | |
fa04c165 QW |
1666 | |
1667 | /* Set btree blocks beyond nritems with 0 to avoid stale content */ | |
1668 | nritems = btrfs_header_nritems(eb); | |
1669 | if (btrfs_header_level(eb) > 0) { | |
e23efd8e | 1670 | end = btrfs_node_key_ptr_offset(eb, nritems); |
fa04c165 QW |
1671 | memzero_extent_buffer(eb, end, eb->len - end); |
1672 | } else { | |
1673 | /* | |
1674 | * Leaf: | |
1675 | * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 | |
1676 | */ | |
42c9419a | 1677 | start = btrfs_item_nr_offset(eb, nritems); |
8009adf3 | 1678 | end = btrfs_item_nr_offset(eb, 0); |
3a3178c7 JB |
1679 | if (nritems == 0) |
1680 | end += BTRFS_LEAF_DATA_SIZE(eb->fs_info); | |
1681 | else | |
1682 | end += btrfs_item_offset(eb, nritems - 1); | |
fa04c165 QW |
1683 | memzero_extent_buffer(eb, start, end - start); |
1684 | } | |
1685 | } | |
1686 | ||
55173337 | 1687 | static noinline_for_stack void write_one_eb(struct extent_buffer *eb, |
50b21d7a | 1688 | struct writeback_control *wbc) |
0b32f4bb | 1689 | { |
46672a44 | 1690 | struct btrfs_fs_info *fs_info = eb->fs_info; |
b51e6b4b | 1691 | struct btrfs_bio *bbio; |
0b32f4bb | 1692 | |
fa04c165 | 1693 | prepare_eb_write(eb); |
35b6ddfa | 1694 | |
b51e6b4b CH |
1695 | bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES, |
1696 | REQ_OP_WRITE | REQ_META | wbc_to_write_flags(wbc), | |
cd88a4fd | 1697 | eb->fs_info, extent_buffer_write_end_io, eb); |
b51e6b4b | 1698 | bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT; |
46672a44 | 1699 | bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev); |
b51e6b4b CH |
1700 | wbc_init_bio(wbc, &bbio->bio); |
1701 | bbio->inode = BTRFS_I(eb->fs_info->btree_inode); | |
1702 | bbio->file_offset = eb->start; | |
46672a44 CH |
1703 | if (fs_info->nodesize < PAGE_SIZE) { |
1704 | struct page *p = eb->pages[0]; | |
0b32f4bb | 1705 | |
81a79b6a | 1706 | lock_page(p); |
46672a44 CH |
1707 | btrfs_subpage_set_writeback(fs_info, p, eb->start, eb->len); |
1708 | if (btrfs_subpage_clear_and_test_dirty(fs_info, p, eb->start, | |
1709 | eb->len)) { | |
1710 | clear_page_dirty_for_io(p); | |
1711 | wbc->nr_to_write--; | |
1712 | } | |
1713 | __bio_add_page(&bbio->bio, p, eb->len, eb->start - page_offset(p)); | |
1714 | wbc_account_cgroup_owner(wbc, p, eb->len); | |
0b32f4bb | 1715 | unlock_page(p); |
46672a44 CH |
1716 | } else { |
1717 | for (int i = 0; i < num_extent_pages(eb); i++) { | |
1718 | struct page *p = eb->pages[i]; | |
1719 | ||
1720 | lock_page(p); | |
1721 | clear_page_dirty_for_io(p); | |
1722 | set_page_writeback(p); | |
1723 | __bio_add_page(&bbio->bio, p, PAGE_SIZE, 0); | |
1724 | wbc_account_cgroup_owner(wbc, p, PAGE_SIZE); | |
1725 | wbc->nr_to_write--; | |
1726 | unlock_page(p); | |
1727 | } | |
0b32f4bb | 1728 | } |
b51e6b4b | 1729 | btrfs_submit_bio(bbio, 0); |
0b32f4bb JB |
1730 | } |
1731 | ||
c4aec299 QW |
1732 | /* |
1733 | * Submit one subpage btree page. | |
1734 | * | |
1735 | * The main difference to submit_eb_page() is: | |
1736 | * - Page locking | |
1737 | * For subpage, we don't rely on page locking at all. | |
1738 | * | |
1739 | * - Flush write bio | |
1740 | * We only flush bio if we may be unable to fit current extent buffers into | |
1741 | * current bio. | |
1742 | * | |
1743 | * Return >=0 for the number of submitted extent buffers. | |
1744 | * Return <0 for fatal error. | |
1745 | */ | |
50b21d7a | 1746 | static int submit_eb_subpage(struct page *page, struct writeback_control *wbc) |
c4aec299 QW |
1747 | { |
1748 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); | |
1749 | int submitted = 0; | |
1750 | u64 page_start = page_offset(page); | |
1751 | int bit_start = 0; | |
c4aec299 | 1752 | int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits; |
c4aec299 QW |
1753 | |
1754 | /* Lock and write each dirty extent buffers in the range */ | |
72a69cd0 | 1755 | while (bit_start < fs_info->subpage_info->bitmap_nr_bits) { |
c4aec299 QW |
1756 | struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private; |
1757 | struct extent_buffer *eb; | |
1758 | unsigned long flags; | |
1759 | u64 start; | |
1760 | ||
1761 | /* | |
1762 | * Take private lock to ensure the subpage won't be detached | |
1763 | * in the meantime. | |
1764 | */ | |
1765 | spin_lock(&page->mapping->private_lock); | |
1766 | if (!PagePrivate(page)) { | |
1767 | spin_unlock(&page->mapping->private_lock); | |
1768 | break; | |
1769 | } | |
1770 | spin_lock_irqsave(&subpage->lock, flags); | |
72a69cd0 QW |
1771 | if (!test_bit(bit_start + fs_info->subpage_info->dirty_offset, |
1772 | subpage->bitmaps)) { | |
c4aec299 QW |
1773 | spin_unlock_irqrestore(&subpage->lock, flags); |
1774 | spin_unlock(&page->mapping->private_lock); | |
1775 | bit_start++; | |
1776 | continue; | |
1777 | } | |
1778 | ||
1779 | start = page_start + bit_start * fs_info->sectorsize; | |
1780 | bit_start += sectors_per_node; | |
1781 | ||
1782 | /* | |
1783 | * Here we just want to grab the eb without touching extra | |
1784 | * spin locks, so call find_extent_buffer_nolock(). | |
1785 | */ | |
1786 | eb = find_extent_buffer_nolock(fs_info, start); | |
1787 | spin_unlock_irqrestore(&subpage->lock, flags); | |
1788 | spin_unlock(&page->mapping->private_lock); | |
1789 | ||
1790 | /* | |
1791 | * The eb has already reached 0 refs thus find_extent_buffer() | |
1792 | * doesn't return it. We don't need to write back such eb | |
1793 | * anyway. | |
1794 | */ | |
1795 | if (!eb) | |
1796 | continue; | |
1797 | ||
50b21d7a | 1798 | if (lock_extent_buffer_for_io(eb, wbc)) { |
46672a44 | 1799 | write_one_eb(eb, wbc); |
9fdd1601 | 1800 | submitted++; |
c4aec299 | 1801 | } |
c4aec299 | 1802 | free_extent_buffer(eb); |
c4aec299 QW |
1803 | } |
1804 | return submitted; | |
c4aec299 QW |
1805 | } |
1806 | ||
f91e0d0c QW |
1807 | /* |
1808 | * Submit all page(s) of one extent buffer. | |
1809 | * | |
1810 | * @page: the page of one extent buffer | |
1811 | * @eb_context: to determine if we need to submit this page, if current page | |
1812 | * belongs to this eb, we don't need to submit | |
1813 | * | |
1814 | * The caller should pass each page in their bytenr order, and here we use | |
1815 | * @eb_context to determine if we have submitted pages of one extent buffer. | |
1816 | * | |
1817 | * If we have, we just skip until we hit a new page that doesn't belong to | |
1818 | * current @eb_context. | |
1819 | * | |
1820 | * If not, we submit all the page(s) of the extent buffer. | |
1821 | * | |
1822 | * Return >0 if we have submitted the extent buffer successfully. | |
1823 | * Return 0 if we don't need to submit the page, as it's already submitted by | |
1824 | * previous call. | |
1825 | * Return <0 for fatal error. | |
1826 | */ | |
50b21d7a | 1827 | static int submit_eb_page(struct page *page, struct writeback_control *wbc, |
f91e0d0c QW |
1828 | struct extent_buffer **eb_context) |
1829 | { | |
1830 | struct address_space *mapping = page->mapping; | |
0bc09ca1 | 1831 | struct btrfs_block_group *cache = NULL; |
f91e0d0c QW |
1832 | struct extent_buffer *eb; |
1833 | int ret; | |
1834 | ||
1835 | if (!PagePrivate(page)) | |
1836 | return 0; | |
1837 | ||
fbca46eb | 1838 | if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) |
50b21d7a | 1839 | return submit_eb_subpage(page, wbc); |
c4aec299 | 1840 | |
f91e0d0c QW |
1841 | spin_lock(&mapping->private_lock); |
1842 | if (!PagePrivate(page)) { | |
1843 | spin_unlock(&mapping->private_lock); | |
1844 | return 0; | |
1845 | } | |
1846 | ||
1847 | eb = (struct extent_buffer *)page->private; | |
1848 | ||
1849 | /* | |
1850 | * Shouldn't happen and normally this would be a BUG_ON but no point | |
1851 | * crashing the machine for something we can survive anyway. | |
1852 | */ | |
1853 | if (WARN_ON(!eb)) { | |
1854 | spin_unlock(&mapping->private_lock); | |
1855 | return 0; | |
1856 | } | |
1857 | ||
1858 | if (eb == *eb_context) { | |
1859 | spin_unlock(&mapping->private_lock); | |
1860 | return 0; | |
1861 | } | |
1862 | ret = atomic_inc_not_zero(&eb->refs); | |
1863 | spin_unlock(&mapping->private_lock); | |
1864 | if (!ret) | |
1865 | return 0; | |
1866 | ||
0bc09ca1 NA |
1867 | if (!btrfs_check_meta_write_pointer(eb->fs_info, eb, &cache)) { |
1868 | /* | |
1869 | * If for_sync, this hole will be filled with | |
1870 | * trasnsaction commit. | |
1871 | */ | |
50b21d7a | 1872 | if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) |
0bc09ca1 NA |
1873 | ret = -EAGAIN; |
1874 | else | |
1875 | ret = 0; | |
1876 | free_extent_buffer(eb); | |
1877 | return ret; | |
1878 | } | |
1879 | ||
f91e0d0c QW |
1880 | *eb_context = eb; |
1881 | ||
50b21d7a | 1882 | if (!lock_extent_buffer_for_io(eb, wbc)) { |
0bc09ca1 NA |
1883 | btrfs_revert_meta_write_pointer(cache, eb); |
1884 | if (cache) | |
1885 | btrfs_put_block_group(cache); | |
f91e0d0c | 1886 | free_extent_buffer(eb); |
50b21d7a | 1887 | return 0; |
f91e0d0c | 1888 | } |
be1a1d7a | 1889 | if (cache) { |
d3e29967 NB |
1890 | /* |
1891 | * Implies write in zoned mode. Mark the last eb in a block group. | |
1892 | */ | |
56fbb0a4 | 1893 | btrfs_schedule_zone_finish_bg(cache, eb); |
d3e29967 | 1894 | btrfs_put_block_group(cache); |
be1a1d7a | 1895 | } |
50b21d7a | 1896 | write_one_eb(eb, wbc); |
f91e0d0c | 1897 | free_extent_buffer(eb); |
f91e0d0c QW |
1898 | return 1; |
1899 | } | |
1900 | ||
0b32f4bb JB |
1901 | int btree_write_cache_pages(struct address_space *mapping, |
1902 | struct writeback_control *wbc) | |
1903 | { | |
f91e0d0c | 1904 | struct extent_buffer *eb_context = NULL; |
b3ff8f1d | 1905 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
0b32f4bb JB |
1906 | int ret = 0; |
1907 | int done = 0; | |
1908 | int nr_to_write_done = 0; | |
51c5cd3b VMO |
1909 | struct folio_batch fbatch; |
1910 | unsigned int nr_folios; | |
0b32f4bb JB |
1911 | pgoff_t index; |
1912 | pgoff_t end; /* Inclusive */ | |
1913 | int scanned = 0; | |
10bbd235 | 1914 | xa_mark_t tag; |
0b32f4bb | 1915 | |
51c5cd3b | 1916 | folio_batch_init(&fbatch); |
0b32f4bb JB |
1917 | if (wbc->range_cyclic) { |
1918 | index = mapping->writeback_index; /* Start from prev offset */ | |
1919 | end = -1; | |
556755a8 JB |
1920 | /* |
1921 | * Start from the beginning does not need to cycle over the | |
1922 | * range, mark it as scanned. | |
1923 | */ | |
1924 | scanned = (index == 0); | |
0b32f4bb | 1925 | } else { |
09cbfeaf KS |
1926 | index = wbc->range_start >> PAGE_SHIFT; |
1927 | end = wbc->range_end >> PAGE_SHIFT; | |
0b32f4bb JB |
1928 | scanned = 1; |
1929 | } | |
1930 | if (wbc->sync_mode == WB_SYNC_ALL) | |
1931 | tag = PAGECACHE_TAG_TOWRITE; | |
1932 | else | |
1933 | tag = PAGECACHE_TAG_DIRTY; | |
0bc09ca1 | 1934 | btrfs_zoned_meta_io_lock(fs_info); |
0b32f4bb JB |
1935 | retry: |
1936 | if (wbc->sync_mode == WB_SYNC_ALL) | |
1937 | tag_pages_for_writeback(mapping, index, end); | |
1938 | while (!done && !nr_to_write_done && (index <= end) && | |
51c5cd3b VMO |
1939 | (nr_folios = filemap_get_folios_tag(mapping, &index, end, |
1940 | tag, &fbatch))) { | |
0b32f4bb JB |
1941 | unsigned i; |
1942 | ||
51c5cd3b VMO |
1943 | for (i = 0; i < nr_folios; i++) { |
1944 | struct folio *folio = fbatch.folios[i]; | |
0b32f4bb | 1945 | |
50b21d7a | 1946 | ret = submit_eb_page(&folio->page, wbc, &eb_context); |
f91e0d0c | 1947 | if (ret == 0) |
0b32f4bb | 1948 | continue; |
f91e0d0c | 1949 | if (ret < 0) { |
0b32f4bb | 1950 | done = 1; |
0b32f4bb JB |
1951 | break; |
1952 | } | |
0b32f4bb JB |
1953 | |
1954 | /* | |
1955 | * the filesystem may choose to bump up nr_to_write. | |
1956 | * We have to make sure to honor the new nr_to_write | |
1957 | * at any time | |
1958 | */ | |
1959 | nr_to_write_done = wbc->nr_to_write <= 0; | |
1960 | } | |
51c5cd3b | 1961 | folio_batch_release(&fbatch); |
0b32f4bb JB |
1962 | cond_resched(); |
1963 | } | |
1964 | if (!scanned && !done) { | |
1965 | /* | |
1966 | * We hit the last page and there is more work to be done: wrap | |
1967 | * back to the start of the file | |
1968 | */ | |
1969 | scanned = 1; | |
1970 | index = 0; | |
1971 | goto retry; | |
1972 | } | |
b3ff8f1d QW |
1973 | /* |
1974 | * If something went wrong, don't allow any metadata write bio to be | |
1975 | * submitted. | |
1976 | * | |
1977 | * This would prevent use-after-free if we had dirty pages not | |
1978 | * cleaned up, which can still happen by fuzzed images. | |
1979 | * | |
1980 | * - Bad extent tree | |
1981 | * Allowing existing tree block to be allocated for other trees. | |
1982 | * | |
1983 | * - Log tree operations | |
1984 | * Exiting tree blocks get allocated to log tree, bumps its | |
1985 | * generation, then get cleaned in tree re-balance. | |
1986 | * Such tree block will not be written back, since it's clean, | |
1987 | * thus no WRITTEN flag set. | |
1988 | * And after log writes back, this tree block is not traced by | |
1989 | * any dirty extent_io_tree. | |
1990 | * | |
1991 | * - Offending tree block gets re-dirtied from its original owner | |
1992 | * Since it has bumped generation, no WRITTEN flag, it can be | |
1993 | * reused without COWing. This tree block will not be traced | |
1994 | * by btrfs_transaction::dirty_pages. | |
1995 | * | |
1996 | * Now such dirty tree block will not be cleaned by any dirty | |
1997 | * extent io tree. Thus we don't want to submit such wild eb | |
1998 | * if the fs already has error. | |
9845e5dd | 1999 | * |
c9583ada QW |
2000 | * We can get ret > 0 from submit_extent_page() indicating how many ebs |
2001 | * were submitted. Reset it to 0 to avoid false alerts for the caller. | |
2002 | */ | |
2003 | if (ret > 0) | |
2004 | ret = 0; | |
9845e5dd CH |
2005 | if (!ret && BTRFS_FS_ERROR(fs_info)) |
2006 | ret = -EROFS; | |
9845e5dd | 2007 | btrfs_zoned_meta_io_unlock(fs_info); |
0b32f4bb JB |
2008 | return ret; |
2009 | } | |
2010 | ||
43dd529a | 2011 | /* |
3bed2da1 NB |
2012 | * Walk the list of dirty pages of the given address space and write all of them. |
2013 | * | |
ee5f017d DS |
2014 | * @mapping: address space structure to write |
2015 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write | |
2016 | * @bio_ctrl: holds context for the write, namely the bio | |
d1310b2e CM |
2017 | * |
2018 | * If a page is already under I/O, write_cache_pages() skips it, even | |
2019 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, | |
2020 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() | |
2021 | * and msync() need to guarantee that all the data which was dirty at the time | |
2022 | * the call was made get new I/O started against them. If wbc->sync_mode is | |
2023 | * WB_SYNC_ALL then we were called for data integrity and we must wait for | |
2024 | * existing IO to complete. | |
2025 | */ | |
4242b64a | 2026 | static int extent_write_cache_pages(struct address_space *mapping, |
ee5f017d | 2027 | struct btrfs_bio_ctrl *bio_ctrl) |
d1310b2e | 2028 | { |
72b505dc | 2029 | struct writeback_control *wbc = bio_ctrl->wbc; |
7fd1a3f7 | 2030 | struct inode *inode = mapping->host; |
d1310b2e CM |
2031 | int ret = 0; |
2032 | int done = 0; | |
f85d7d6c | 2033 | int nr_to_write_done = 0; |
9f50fd2e VMO |
2034 | struct folio_batch fbatch; |
2035 | unsigned int nr_folios; | |
d1310b2e CM |
2036 | pgoff_t index; |
2037 | pgoff_t end; /* Inclusive */ | |
a9132667 LB |
2038 | pgoff_t done_index; |
2039 | int range_whole = 0; | |
d1310b2e | 2040 | int scanned = 0; |
10bbd235 | 2041 | xa_mark_t tag; |
d1310b2e | 2042 | |
7fd1a3f7 JB |
2043 | /* |
2044 | * We have to hold onto the inode so that ordered extents can do their | |
2045 | * work when the IO finishes. The alternative to this is failing to add | |
2046 | * an ordered extent if the igrab() fails there and that is a huge pain | |
2047 | * to deal with, so instead just hold onto the inode throughout the | |
2048 | * writepages operation. If it fails here we are freeing up the inode | |
2049 | * anyway and we'd rather not waste our time writing out stuff that is | |
2050 | * going to be truncated anyway. | |
2051 | */ | |
2052 | if (!igrab(inode)) | |
2053 | return 0; | |
2054 | ||
9f50fd2e | 2055 | folio_batch_init(&fbatch); |
d1310b2e CM |
2056 | if (wbc->range_cyclic) { |
2057 | index = mapping->writeback_index; /* Start from prev offset */ | |
2058 | end = -1; | |
556755a8 JB |
2059 | /* |
2060 | * Start from the beginning does not need to cycle over the | |
2061 | * range, mark it as scanned. | |
2062 | */ | |
2063 | scanned = (index == 0); | |
d1310b2e | 2064 | } else { |
09cbfeaf KS |
2065 | index = wbc->range_start >> PAGE_SHIFT; |
2066 | end = wbc->range_end >> PAGE_SHIFT; | |
a9132667 LB |
2067 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
2068 | range_whole = 1; | |
d1310b2e CM |
2069 | scanned = 1; |
2070 | } | |
3cd24c69 EL |
2071 | |
2072 | /* | |
2073 | * We do the tagged writepage as long as the snapshot flush bit is set | |
2074 | * and we are the first one who do the filemap_flush() on this inode. | |
2075 | * | |
2076 | * The nr_to_write == LONG_MAX is needed to make sure other flushers do | |
2077 | * not race in and drop the bit. | |
2078 | */ | |
2079 | if (range_whole && wbc->nr_to_write == LONG_MAX && | |
2080 | test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
2081 | &BTRFS_I(inode)->runtime_flags)) | |
2082 | wbc->tagged_writepages = 1; | |
2083 | ||
2084 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) | |
f7aaa06b JB |
2085 | tag = PAGECACHE_TAG_TOWRITE; |
2086 | else | |
2087 | tag = PAGECACHE_TAG_DIRTY; | |
d1310b2e | 2088 | retry: |
3cd24c69 | 2089 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
f7aaa06b | 2090 | tag_pages_for_writeback(mapping, index, end); |
a9132667 | 2091 | done_index = index; |
f85d7d6c | 2092 | while (!done && !nr_to_write_done && (index <= end) && |
9f50fd2e VMO |
2093 | (nr_folios = filemap_get_folios_tag(mapping, &index, |
2094 | end, tag, &fbatch))) { | |
d1310b2e CM |
2095 | unsigned i; |
2096 | ||
9f50fd2e VMO |
2097 | for (i = 0; i < nr_folios; i++) { |
2098 | struct folio *folio = fbatch.folios[i]; | |
d1310b2e | 2099 | |
7b365a2a | 2100 | done_index = folio_next_index(folio); |
d1310b2e | 2101 | /* |
b93b0163 MW |
2102 | * At this point we hold neither the i_pages lock nor |
2103 | * the page lock: the page may be truncated or | |
2104 | * invalidated (changing page->mapping to NULL), | |
2105 | * or even swizzled back from swapper_space to | |
2106 | * tmpfs file mapping | |
d1310b2e | 2107 | */ |
9f50fd2e | 2108 | if (!folio_trylock(folio)) { |
ee5f017d | 2109 | submit_write_bio(bio_ctrl, 0); |
9f50fd2e | 2110 | folio_lock(folio); |
01d658f2 | 2111 | } |
d1310b2e | 2112 | |
9f50fd2e VMO |
2113 | if (unlikely(folio->mapping != mapping)) { |
2114 | folio_unlock(folio); | |
d1310b2e CM |
2115 | continue; |
2116 | } | |
2117 | ||
5c256998 CH |
2118 | if (!folio_test_dirty(folio)) { |
2119 | /* Someone wrote it for us. */ | |
2120 | folio_unlock(folio); | |
2121 | continue; | |
2122 | } | |
2123 | ||
d2c3f4f6 | 2124 | if (wbc->sync_mode != WB_SYNC_NONE) { |
9f50fd2e | 2125 | if (folio_test_writeback(folio)) |
ee5f017d | 2126 | submit_write_bio(bio_ctrl, 0); |
9f50fd2e | 2127 | folio_wait_writeback(folio); |
d2c3f4f6 | 2128 | } |
d1310b2e | 2129 | |
9f50fd2e VMO |
2130 | if (folio_test_writeback(folio) || |
2131 | !folio_clear_dirty_for_io(folio)) { | |
2132 | folio_unlock(folio); | |
d1310b2e CM |
2133 | continue; |
2134 | } | |
2135 | ||
72b505dc | 2136 | ret = __extent_writepage(&folio->page, bio_ctrl); |
a9132667 | 2137 | if (ret < 0) { |
a9132667 LB |
2138 | done = 1; |
2139 | break; | |
2140 | } | |
f85d7d6c CM |
2141 | |
2142 | /* | |
effa24f6 | 2143 | * The filesystem may choose to bump up nr_to_write. |
f85d7d6c | 2144 | * We have to make sure to honor the new nr_to_write |
effa24f6 | 2145 | * at any time. |
f85d7d6c | 2146 | */ |
effa24f6 CH |
2147 | nr_to_write_done = (wbc->sync_mode == WB_SYNC_NONE && |
2148 | wbc->nr_to_write <= 0); | |
d1310b2e | 2149 | } |
9f50fd2e | 2150 | folio_batch_release(&fbatch); |
d1310b2e CM |
2151 | cond_resched(); |
2152 | } | |
894b36e3 | 2153 | if (!scanned && !done) { |
d1310b2e CM |
2154 | /* |
2155 | * We hit the last page and there is more work to be done: wrap | |
2156 | * back to the start of the file | |
2157 | */ | |
2158 | scanned = 1; | |
2159 | index = 0; | |
42ffb0bf JB |
2160 | |
2161 | /* | |
2162 | * If we're looping we could run into a page that is locked by a | |
2163 | * writer and that writer could be waiting on writeback for a | |
2164 | * page in our current bio, and thus deadlock, so flush the | |
2165 | * write bio here. | |
2166 | */ | |
ee5f017d | 2167 | submit_write_bio(bio_ctrl, 0); |
c9583ada | 2168 | goto retry; |
d1310b2e | 2169 | } |
a9132667 LB |
2170 | |
2171 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole)) | |
2172 | mapping->writeback_index = done_index; | |
2173 | ||
e55cf7ca | 2174 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
894b36e3 | 2175 | return ret; |
d1310b2e | 2176 | } |
d1310b2e | 2177 | |
2bd0fc93 QW |
2178 | /* |
2179 | * Submit the pages in the range to bio for call sites which delalloc range has | |
2180 | * already been ran (aka, ordered extent inserted) and all pages are still | |
2181 | * locked. | |
2182 | */ | |
7027f871 CH |
2183 | int extent_write_locked_range(struct inode *inode, u64 start, u64 end, |
2184 | struct writeback_control *wbc) | |
771ed689 | 2185 | { |
2bd0fc93 QW |
2186 | bool found_error = false; |
2187 | int first_error = 0; | |
771ed689 CM |
2188 | int ret = 0; |
2189 | struct address_space *mapping = inode->i_mapping; | |
eb34dcea CH |
2190 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2191 | const u32 sectorsize = fs_info->sectorsize; | |
2192 | loff_t i_size = i_size_read(inode); | |
2bd0fc93 | 2193 | u64 cur = start; |
c000bc04 | 2194 | struct btrfs_bio_ctrl bio_ctrl = { |
7027f871 CH |
2195 | .wbc = wbc, |
2196 | .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc), | |
c000bc04 | 2197 | }; |
771ed689 | 2198 | |
7027f871 CH |
2199 | if (wbc->no_cgroup_owner) |
2200 | bio_ctrl.opf |= REQ_BTRFS_CGROUP_PUNT; | |
2201 | ||
66448b9d | 2202 | ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(end + 1, sectorsize)); |
66448b9d | 2203 | |
2bd0fc93 | 2204 | while (cur <= end) { |
66448b9d | 2205 | u64 cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end); |
9783e4de | 2206 | u32 cur_len = cur_end + 1 - cur; |
eb34dcea CH |
2207 | struct page *page; |
2208 | int nr = 0; | |
66448b9d | 2209 | |
2bd0fc93 QW |
2210 | page = find_get_page(mapping, cur >> PAGE_SHIFT); |
2211 | /* | |
2212 | * All pages in the range are locked since | |
2213 | * btrfs_run_delalloc_range(), thus there is no way to clear | |
2214 | * the page dirty flag. | |
2215 | */ | |
66448b9d | 2216 | ASSERT(PageLocked(page)); |
2bd0fc93 QW |
2217 | ASSERT(PageDirty(page)); |
2218 | clear_page_dirty_for_io(page); | |
eb34dcea CH |
2219 | |
2220 | ret = __extent_writepage_io(BTRFS_I(inode), page, &bio_ctrl, | |
2221 | i_size, &nr); | |
2222 | if (ret == 1) | |
2223 | goto next_page; | |
2224 | ||
2225 | /* Make sure the mapping tag for page dirty gets cleared. */ | |
2226 | if (nr == 0) { | |
2227 | set_page_writeback(page); | |
2228 | end_page_writeback(page); | |
2229 | } | |
9783e4de CH |
2230 | if (ret) { |
2231 | btrfs_mark_ordered_io_finished(BTRFS_I(inode), page, | |
2232 | cur, cur_len, !ret); | |
2233 | btrfs_page_clear_uptodate(fs_info, page, cur, cur_len); | |
2234 | mapping_set_error(page->mapping, ret); | |
2235 | } | |
2236 | btrfs_page_unlock_writer(fs_info, page, cur, cur_len); | |
2bd0fc93 QW |
2237 | if (ret < 0) { |
2238 | found_error = true; | |
2239 | first_error = ret; | |
771ed689 | 2240 | } |
eb34dcea | 2241 | next_page: |
09cbfeaf | 2242 | put_page(page); |
66448b9d | 2243 | cur = cur_end + 1; |
771ed689 CM |
2244 | } |
2245 | ||
ee5f017d | 2246 | submit_write_bio(&bio_ctrl, found_error ? ret : 0); |
dbb70bec | 2247 | |
2bd0fc93 QW |
2248 | if (found_error) |
2249 | return first_error; | |
771ed689 CM |
2250 | return ret; |
2251 | } | |
d1310b2e | 2252 | |
8ae225a8 | 2253 | int extent_writepages(struct address_space *mapping, |
d1310b2e CM |
2254 | struct writeback_control *wbc) |
2255 | { | |
35156d85 | 2256 | struct inode *inode = mapping->host; |
d1310b2e | 2257 | int ret = 0; |
ee5f017d | 2258 | struct btrfs_bio_ctrl bio_ctrl = { |
72b505dc | 2259 | .wbc = wbc, |
c000bc04 | 2260 | .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc), |
d1310b2e CM |
2261 | }; |
2262 | ||
35156d85 JT |
2263 | /* |
2264 | * Allow only a single thread to do the reloc work in zoned mode to | |
2265 | * protect the write pointer updates. | |
2266 | */ | |
869f4cdc | 2267 | btrfs_zoned_data_reloc_lock(BTRFS_I(inode)); |
72b505dc | 2268 | ret = extent_write_cache_pages(mapping, &bio_ctrl); |
ee5f017d | 2269 | submit_write_bio(&bio_ctrl, ret); |
19ab78ca | 2270 | btrfs_zoned_data_reloc_unlock(BTRFS_I(inode)); |
d1310b2e CM |
2271 | return ret; |
2272 | } | |
d1310b2e | 2273 | |
ba206a02 | 2274 | void extent_readahead(struct readahead_control *rac) |
d1310b2e | 2275 | { |
c000bc04 | 2276 | struct btrfs_bio_ctrl bio_ctrl = { .opf = REQ_OP_READ | REQ_RAHEAD }; |
67c9684f | 2277 | struct page *pagepool[16]; |
125bac01 | 2278 | struct extent_map *em_cached = NULL; |
808f80b4 | 2279 | u64 prev_em_start = (u64)-1; |
ba206a02 | 2280 | int nr; |
d1310b2e | 2281 | |
ba206a02 | 2282 | while ((nr = readahead_page_batch(rac, pagepool))) { |
32c0a6bc MWO |
2283 | u64 contig_start = readahead_pos(rac); |
2284 | u64 contig_end = contig_start + readahead_batch_length(rac) - 1; | |
e65ef21e | 2285 | |
ba206a02 | 2286 | contiguous_readpages(pagepool, nr, contig_start, contig_end, |
390ed29b | 2287 | &em_cached, &bio_ctrl, &prev_em_start); |
d1310b2e | 2288 | } |
67c9684f | 2289 | |
125bac01 MX |
2290 | if (em_cached) |
2291 | free_extent_map(em_cached); | |
722c82ac | 2292 | submit_one_bio(&bio_ctrl); |
d1310b2e | 2293 | } |
d1310b2e CM |
2294 | |
2295 | /* | |
895586eb MWO |
2296 | * basic invalidate_folio code, this waits on any locked or writeback |
2297 | * ranges corresponding to the folio, and then deletes any extent state | |
d1310b2e CM |
2298 | * records from the tree |
2299 | */ | |
895586eb MWO |
2300 | int extent_invalidate_folio(struct extent_io_tree *tree, |
2301 | struct folio *folio, size_t offset) | |
d1310b2e | 2302 | { |
2ac55d41 | 2303 | struct extent_state *cached_state = NULL; |
895586eb MWO |
2304 | u64 start = folio_pos(folio); |
2305 | u64 end = start + folio_size(folio) - 1; | |
2306 | size_t blocksize = folio->mapping->host->i_sb->s_blocksize; | |
d1310b2e | 2307 | |
829ddec9 QW |
2308 | /* This function is only called for the btree inode */ |
2309 | ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO); | |
2310 | ||
fda2832f | 2311 | start += ALIGN(offset, blocksize); |
d1310b2e CM |
2312 | if (start > end) |
2313 | return 0; | |
2314 | ||
570eb97b | 2315 | lock_extent(tree, start, end, &cached_state); |
895586eb | 2316 | folio_wait_writeback(folio); |
829ddec9 QW |
2317 | |
2318 | /* | |
2319 | * Currently for btree io tree, only EXTENT_LOCKED is utilized, | |
2320 | * so here we only need to unlock the extent range to free any | |
2321 | * existing extent state. | |
2322 | */ | |
570eb97b | 2323 | unlock_extent(tree, start, end, &cached_state); |
d1310b2e CM |
2324 | return 0; |
2325 | } | |
d1310b2e | 2326 | |
7b13b7b1 | 2327 | /* |
f913cff3 | 2328 | * a helper for release_folio, this tests for areas of the page that |
7b13b7b1 CM |
2329 | * are locked or under IO and drops the related state bits if it is safe |
2330 | * to drop the page. | |
2331 | */ | |
29c68b2d | 2332 | static int try_release_extent_state(struct extent_io_tree *tree, |
48a3b636 | 2333 | struct page *page, gfp_t mask) |
7b13b7b1 | 2334 | { |
4eee4fa4 | 2335 | u64 start = page_offset(page); |
09cbfeaf | 2336 | u64 end = start + PAGE_SIZE - 1; |
7b13b7b1 CM |
2337 | int ret = 1; |
2338 | ||
8882679e | 2339 | if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) { |
7b13b7b1 | 2340 | ret = 0; |
8882679e | 2341 | } else { |
b71fb16b JB |
2342 | u32 clear_bits = ~(EXTENT_LOCKED | EXTENT_NODATASUM | |
2343 | EXTENT_DELALLOC_NEW | EXTENT_CTLBITS); | |
2344 | ||
11ef160f | 2345 | /* |
2766ff61 FM |
2346 | * At this point we can safely clear everything except the |
2347 | * locked bit, the nodatasum bit and the delalloc new bit. | |
2348 | * The delalloc new bit will be cleared by ordered extent | |
2349 | * completion. | |
11ef160f | 2350 | */ |
1d126800 | 2351 | ret = __clear_extent_bit(tree, start, end, clear_bits, NULL, NULL); |
e3f24cc5 CM |
2352 | |
2353 | /* if clear_extent_bit failed for enomem reasons, | |
2354 | * we can't allow the release to continue. | |
2355 | */ | |
2356 | if (ret < 0) | |
2357 | ret = 0; | |
2358 | else | |
2359 | ret = 1; | |
7b13b7b1 CM |
2360 | } |
2361 | return ret; | |
2362 | } | |
7b13b7b1 | 2363 | |
d1310b2e | 2364 | /* |
f913cff3 | 2365 | * a helper for release_folio. As long as there are no locked extents |
d1310b2e CM |
2366 | * in the range corresponding to the page, both state records and extent |
2367 | * map records are removed | |
2368 | */ | |
477a30ba | 2369 | int try_release_extent_mapping(struct page *page, gfp_t mask) |
d1310b2e CM |
2370 | { |
2371 | struct extent_map *em; | |
4eee4fa4 | 2372 | u64 start = page_offset(page); |
09cbfeaf | 2373 | u64 end = start + PAGE_SIZE - 1; |
bd3599a0 FM |
2374 | struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host); |
2375 | struct extent_io_tree *tree = &btrfs_inode->io_tree; | |
2376 | struct extent_map_tree *map = &btrfs_inode->extent_tree; | |
7b13b7b1 | 2377 | |
d0164adc | 2378 | if (gfpflags_allow_blocking(mask) && |
ee22184b | 2379 | page->mapping->host->i_size > SZ_16M) { |
39b5637f | 2380 | u64 len; |
70dec807 | 2381 | while (start <= end) { |
fbc2bd7e FM |
2382 | struct btrfs_fs_info *fs_info; |
2383 | u64 cur_gen; | |
2384 | ||
39b5637f | 2385 | len = end - start + 1; |
890871be | 2386 | write_lock(&map->lock); |
39b5637f | 2387 | em = lookup_extent_mapping(map, start, len); |
285190d9 | 2388 | if (!em) { |
890871be | 2389 | write_unlock(&map->lock); |
70dec807 CM |
2390 | break; |
2391 | } | |
7f3c74fb CM |
2392 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
2393 | em->start != start) { | |
890871be | 2394 | write_unlock(&map->lock); |
70dec807 CM |
2395 | free_extent_map(em); |
2396 | break; | |
2397 | } | |
3d6448e6 FM |
2398 | if (test_range_bit(tree, em->start, |
2399 | extent_map_end(em) - 1, | |
2400 | EXTENT_LOCKED, 0, NULL)) | |
2401 | goto next; | |
2402 | /* | |
2403 | * If it's not in the list of modified extents, used | |
2404 | * by a fast fsync, we can remove it. If it's being | |
2405 | * logged we can safely remove it since fsync took an | |
2406 | * extra reference on the em. | |
2407 | */ | |
2408 | if (list_empty(&em->list) || | |
fbc2bd7e FM |
2409 | test_bit(EXTENT_FLAG_LOGGING, &em->flags)) |
2410 | goto remove_em; | |
2411 | /* | |
2412 | * If it's in the list of modified extents, remove it | |
2413 | * only if its generation is older then the current one, | |
2414 | * in which case we don't need it for a fast fsync. | |
2415 | * Otherwise don't remove it, we could be racing with an | |
2416 | * ongoing fast fsync that could miss the new extent. | |
2417 | */ | |
2418 | fs_info = btrfs_inode->root->fs_info; | |
2419 | spin_lock(&fs_info->trans_lock); | |
2420 | cur_gen = fs_info->generation; | |
2421 | spin_unlock(&fs_info->trans_lock); | |
2422 | if (em->generation >= cur_gen) | |
2423 | goto next; | |
2424 | remove_em: | |
5e548b32 FM |
2425 | /* |
2426 | * We only remove extent maps that are not in the list of | |
2427 | * modified extents or that are in the list but with a | |
2428 | * generation lower then the current generation, so there | |
2429 | * is no need to set the full fsync flag on the inode (it | |
2430 | * hurts the fsync performance for workloads with a data | |
2431 | * size that exceeds or is close to the system's memory). | |
2432 | */ | |
fbc2bd7e FM |
2433 | remove_extent_mapping(map, em); |
2434 | /* once for the rb tree */ | |
2435 | free_extent_map(em); | |
3d6448e6 | 2436 | next: |
70dec807 | 2437 | start = extent_map_end(em); |
890871be | 2438 | write_unlock(&map->lock); |
70dec807 CM |
2439 | |
2440 | /* once for us */ | |
d1310b2e | 2441 | free_extent_map(em); |
9f47eb54 PM |
2442 | |
2443 | cond_resched(); /* Allow large-extent preemption. */ | |
d1310b2e | 2444 | } |
d1310b2e | 2445 | } |
29c68b2d | 2446 | return try_release_extent_state(tree, page, mask); |
d1310b2e | 2447 | } |
d1310b2e | 2448 | |
4751832d QW |
2449 | /* |
2450 | * To cache previous fiemap extent | |
2451 | * | |
2452 | * Will be used for merging fiemap extent | |
2453 | */ | |
2454 | struct fiemap_cache { | |
2455 | u64 offset; | |
2456 | u64 phys; | |
2457 | u64 len; | |
2458 | u32 flags; | |
2459 | bool cached; | |
2460 | }; | |
2461 | ||
2462 | /* | |
2463 | * Helper to submit fiemap extent. | |
2464 | * | |
2465 | * Will try to merge current fiemap extent specified by @offset, @phys, | |
2466 | * @len and @flags with cached one. | |
2467 | * And only when we fails to merge, cached one will be submitted as | |
2468 | * fiemap extent. | |
2469 | * | |
2470 | * Return value is the same as fiemap_fill_next_extent(). | |
2471 | */ | |
2472 | static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo, | |
2473 | struct fiemap_cache *cache, | |
2474 | u64 offset, u64 phys, u64 len, u32 flags) | |
2475 | { | |
2476 | int ret = 0; | |
2477 | ||
ac3c0d36 FM |
2478 | /* Set at the end of extent_fiemap(). */ |
2479 | ASSERT((flags & FIEMAP_EXTENT_LAST) == 0); | |
2480 | ||
4751832d QW |
2481 | if (!cache->cached) |
2482 | goto assign; | |
2483 | ||
2484 | /* | |
2485 | * Sanity check, extent_fiemap() should have ensured that new | |
52042d8e | 2486 | * fiemap extent won't overlap with cached one. |
4751832d QW |
2487 | * Not recoverable. |
2488 | * | |
2489 | * NOTE: Physical address can overlap, due to compression | |
2490 | */ | |
2491 | if (cache->offset + cache->len > offset) { | |
2492 | WARN_ON(1); | |
2493 | return -EINVAL; | |
2494 | } | |
2495 | ||
2496 | /* | |
2497 | * Only merges fiemap extents if | |
2498 | * 1) Their logical addresses are continuous | |
2499 | * | |
2500 | * 2) Their physical addresses are continuous | |
2501 | * So truly compressed (physical size smaller than logical size) | |
2502 | * extents won't get merged with each other | |
2503 | * | |
ac3c0d36 | 2504 | * 3) Share same flags |
4751832d QW |
2505 | */ |
2506 | if (cache->offset + cache->len == offset && | |
2507 | cache->phys + cache->len == phys && | |
ac3c0d36 | 2508 | cache->flags == flags) { |
4751832d | 2509 | cache->len += len; |
ac3c0d36 | 2510 | return 0; |
4751832d QW |
2511 | } |
2512 | ||
2513 | /* Not mergeable, need to submit cached one */ | |
2514 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, | |
2515 | cache->len, cache->flags); | |
2516 | cache->cached = false; | |
2517 | if (ret) | |
2518 | return ret; | |
2519 | assign: | |
2520 | cache->cached = true; | |
2521 | cache->offset = offset; | |
2522 | cache->phys = phys; | |
2523 | cache->len = len; | |
2524 | cache->flags = flags; | |
ac3c0d36 FM |
2525 | |
2526 | return 0; | |
4751832d QW |
2527 | } |
2528 | ||
2529 | /* | |
848c23b7 | 2530 | * Emit last fiemap cache |
4751832d | 2531 | * |
848c23b7 QW |
2532 | * The last fiemap cache may still be cached in the following case: |
2533 | * 0 4k 8k | |
2534 | * |<- Fiemap range ->| | |
2535 | * |<------------ First extent ----------->| | |
2536 | * | |
2537 | * In this case, the first extent range will be cached but not emitted. | |
2538 | * So we must emit it before ending extent_fiemap(). | |
4751832d | 2539 | */ |
5c5aff98 | 2540 | static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo, |
848c23b7 | 2541 | struct fiemap_cache *cache) |
4751832d QW |
2542 | { |
2543 | int ret; | |
2544 | ||
2545 | if (!cache->cached) | |
2546 | return 0; | |
2547 | ||
4751832d QW |
2548 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
2549 | cache->len, cache->flags); | |
2550 | cache->cached = false; | |
2551 | if (ret > 0) | |
2552 | ret = 0; | |
2553 | return ret; | |
2554 | } | |
2555 | ||
ac3c0d36 | 2556 | static int fiemap_next_leaf_item(struct btrfs_inode *inode, struct btrfs_path *path) |
1506fcc8 | 2557 | { |
ac3c0d36 FM |
2558 | struct extent_buffer *clone; |
2559 | struct btrfs_key key; | |
2560 | int slot; | |
2561 | int ret; | |
2562 | ||
2563 | path->slots[0]++; | |
2564 | if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) | |
2565 | return 0; | |
2566 | ||
2567 | ret = btrfs_next_leaf(inode->root, path); | |
2568 | if (ret != 0) | |
2569 | return ret; | |
2570 | ||
2571 | /* | |
2572 | * Don't bother with cloning if there are no more file extent items for | |
2573 | * our inode. | |
2574 | */ | |
2575 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
2576 | if (key.objectid != btrfs_ino(inode) || key.type != BTRFS_EXTENT_DATA_KEY) | |
2577 | return 1; | |
2578 | ||
2579 | /* See the comment at fiemap_search_slot() about why we clone. */ | |
2580 | clone = btrfs_clone_extent_buffer(path->nodes[0]); | |
2581 | if (!clone) | |
2582 | return -ENOMEM; | |
2583 | ||
2584 | slot = path->slots[0]; | |
2585 | btrfs_release_path(path); | |
2586 | path->nodes[0] = clone; | |
2587 | path->slots[0] = slot; | |
2588 | ||
2589 | return 0; | |
2590 | } | |
2591 | ||
2592 | /* | |
2593 | * Search for the first file extent item that starts at a given file offset or | |
2594 | * the one that starts immediately before that offset. | |
2595 | * Returns: 0 on success, < 0 on error, 1 if not found. | |
2596 | */ | |
2597 | static int fiemap_search_slot(struct btrfs_inode *inode, struct btrfs_path *path, | |
2598 | u64 file_offset) | |
2599 | { | |
2600 | const u64 ino = btrfs_ino(inode); | |
facee0a0 | 2601 | struct btrfs_root *root = inode->root; |
ac3c0d36 FM |
2602 | struct extent_buffer *clone; |
2603 | struct btrfs_key key; | |
2604 | int slot; | |
2605 | int ret; | |
1506fcc8 | 2606 | |
ac3c0d36 FM |
2607 | key.objectid = ino; |
2608 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2609 | key.offset = file_offset; | |
2610 | ||
2611 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2612 | if (ret < 0) | |
2613 | return ret; | |
2614 | ||
2615 | if (ret > 0 && path->slots[0] > 0) { | |
2616 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1); | |
2617 | if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY) | |
2618 | path->slots[0]--; | |
2619 | } | |
2620 | ||
2621 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | |
2622 | ret = btrfs_next_leaf(root, path); | |
2623 | if (ret != 0) | |
2624 | return ret; | |
2625 | ||
2626 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
2627 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) | |
2628 | return 1; | |
5911c8fe DS |
2629 | } |
2630 | ||
15c7745c | 2631 | /* |
ac3c0d36 FM |
2632 | * We clone the leaf and use it during fiemap. This is because while |
2633 | * using the leaf we do expensive things like checking if an extent is | |
2634 | * shared, which can take a long time. In order to prevent blocking | |
2635 | * other tasks for too long, we use a clone of the leaf. We have locked | |
2636 | * the file range in the inode's io tree, so we know none of our file | |
2637 | * extent items can change. This way we avoid blocking other tasks that | |
2638 | * want to insert items for other inodes in the same leaf or b+tree | |
2639 | * rebalance operations (triggered for example when someone is trying | |
2640 | * to push items into this leaf when trying to insert an item in a | |
2641 | * neighbour leaf). | |
2642 | * We also need the private clone because holding a read lock on an | |
2643 | * extent buffer of the subvolume's b+tree will make lockdep unhappy | |
2644 | * when we call fiemap_fill_next_extent(), because that may cause a page | |
2645 | * fault when filling the user space buffer with fiemap data. | |
15c7745c | 2646 | */ |
ac3c0d36 FM |
2647 | clone = btrfs_clone_extent_buffer(path->nodes[0]); |
2648 | if (!clone) | |
2649 | return -ENOMEM; | |
2650 | ||
2651 | slot = path->slots[0]; | |
2652 | btrfs_release_path(path); | |
2653 | path->nodes[0] = clone; | |
2654 | path->slots[0] = slot; | |
2655 | ||
2656 | return 0; | |
2657 | } | |
2658 | ||
2659 | /* | |
2660 | * Process a range which is a hole or a prealloc extent in the inode's subvolume | |
2661 | * btree. If @disk_bytenr is 0, we are dealing with a hole, otherwise a prealloc | |
2662 | * extent. The end offset (@end) is inclusive. | |
2663 | */ | |
2664 | static int fiemap_process_hole(struct btrfs_inode *inode, | |
2665 | struct fiemap_extent_info *fieinfo, | |
2666 | struct fiemap_cache *cache, | |
b3e744fe | 2667 | struct extent_state **delalloc_cached_state, |
61dbb952 | 2668 | struct btrfs_backref_share_check_ctx *backref_ctx, |
ac3c0d36 FM |
2669 | u64 disk_bytenr, u64 extent_offset, |
2670 | u64 extent_gen, | |
ac3c0d36 FM |
2671 | u64 start, u64 end) |
2672 | { | |
2673 | const u64 i_size = i_size_read(&inode->vfs_inode); | |
ac3c0d36 FM |
2674 | u64 cur_offset = start; |
2675 | u64 last_delalloc_end = 0; | |
2676 | u32 prealloc_flags = FIEMAP_EXTENT_UNWRITTEN; | |
2677 | bool checked_extent_shared = false; | |
2678 | int ret; | |
4d479cf0 | 2679 | |
ec29ed5b | 2680 | /* |
ac3c0d36 FM |
2681 | * There can be no delalloc past i_size, so don't waste time looking for |
2682 | * it beyond i_size. | |
ec29ed5b | 2683 | */ |
ac3c0d36 FM |
2684 | while (cur_offset < end && cur_offset < i_size) { |
2685 | u64 delalloc_start; | |
2686 | u64 delalloc_end; | |
2687 | u64 prealloc_start; | |
2688 | u64 prealloc_len = 0; | |
2689 | bool delalloc; | |
2690 | ||
2691 | delalloc = btrfs_find_delalloc_in_range(inode, cur_offset, end, | |
b3e744fe | 2692 | delalloc_cached_state, |
ac3c0d36 FM |
2693 | &delalloc_start, |
2694 | &delalloc_end); | |
2695 | if (!delalloc) | |
2696 | break; | |
2d324f59 | 2697 | |
ec29ed5b | 2698 | /* |
ac3c0d36 FM |
2699 | * If this is a prealloc extent we have to report every section |
2700 | * of it that has no delalloc. | |
ec29ed5b | 2701 | */ |
ac3c0d36 FM |
2702 | if (disk_bytenr != 0) { |
2703 | if (last_delalloc_end == 0) { | |
2704 | prealloc_start = start; | |
2705 | prealloc_len = delalloc_start - start; | |
2706 | } else { | |
2707 | prealloc_start = last_delalloc_end + 1; | |
2708 | prealloc_len = delalloc_start - prealloc_start; | |
2709 | } | |
2710 | } | |
2711 | ||
2712 | if (prealloc_len > 0) { | |
2713 | if (!checked_extent_shared && fieinfo->fi_extents_max) { | |
ceb707da | 2714 | ret = btrfs_is_data_extent_shared(inode, |
84a7949d FM |
2715 | disk_bytenr, |
2716 | extent_gen, | |
2717 | backref_ctx); | |
ac3c0d36 FM |
2718 | if (ret < 0) |
2719 | return ret; | |
2720 | else if (ret > 0) | |
2721 | prealloc_flags |= FIEMAP_EXTENT_SHARED; | |
2722 | ||
2723 | checked_extent_shared = true; | |
2724 | } | |
2725 | ret = emit_fiemap_extent(fieinfo, cache, prealloc_start, | |
2726 | disk_bytenr + extent_offset, | |
2727 | prealloc_len, prealloc_flags); | |
2728 | if (ret) | |
2729 | return ret; | |
2730 | extent_offset += prealloc_len; | |
2731 | } | |
2732 | ||
2733 | ret = emit_fiemap_extent(fieinfo, cache, delalloc_start, 0, | |
2734 | delalloc_end + 1 - delalloc_start, | |
2735 | FIEMAP_EXTENT_DELALLOC | | |
2736 | FIEMAP_EXTENT_UNKNOWN); | |
2737 | if (ret) | |
2738 | return ret; | |
2739 | ||
2740 | last_delalloc_end = delalloc_end; | |
2741 | cur_offset = delalloc_end + 1; | |
2742 | extent_offset += cur_offset - delalloc_start; | |
2743 | cond_resched(); | |
2744 | } | |
2745 | ||
2746 | /* | |
2747 | * Either we found no delalloc for the whole prealloc extent or we have | |
2748 | * a prealloc extent that spans i_size or starts at or after i_size. | |
2749 | */ | |
2750 | if (disk_bytenr != 0 && last_delalloc_end < end) { | |
2751 | u64 prealloc_start; | |
2752 | u64 prealloc_len; | |
2753 | ||
2754 | if (last_delalloc_end == 0) { | |
2755 | prealloc_start = start; | |
2756 | prealloc_len = end + 1 - start; | |
2757 | } else { | |
2758 | prealloc_start = last_delalloc_end + 1; | |
2759 | prealloc_len = end + 1 - prealloc_start; | |
2760 | } | |
2761 | ||
2762 | if (!checked_extent_shared && fieinfo->fi_extents_max) { | |
ceb707da FM |
2763 | ret = btrfs_is_data_extent_shared(inode, |
2764 | disk_bytenr, | |
84a7949d | 2765 | extent_gen, |
61dbb952 | 2766 | backref_ctx); |
ac3c0d36 FM |
2767 | if (ret < 0) |
2768 | return ret; | |
2769 | else if (ret > 0) | |
2770 | prealloc_flags |= FIEMAP_EXTENT_SHARED; | |
2771 | } | |
2772 | ret = emit_fiemap_extent(fieinfo, cache, prealloc_start, | |
2773 | disk_bytenr + extent_offset, | |
2774 | prealloc_len, prealloc_flags); | |
2775 | if (ret) | |
2776 | return ret; | |
2777 | } | |
2778 | ||
2779 | return 0; | |
2780 | } | |
2781 | ||
2782 | static int fiemap_find_last_extent_offset(struct btrfs_inode *inode, | |
2783 | struct btrfs_path *path, | |
2784 | u64 *last_extent_end_ret) | |
2785 | { | |
2786 | const u64 ino = btrfs_ino(inode); | |
2787 | struct btrfs_root *root = inode->root; | |
2788 | struct extent_buffer *leaf; | |
2789 | struct btrfs_file_extent_item *ei; | |
2790 | struct btrfs_key key; | |
2791 | u64 disk_bytenr; | |
2792 | int ret; | |
2793 | ||
2794 | /* | |
2795 | * Lookup the last file extent. We're not using i_size here because | |
2796 | * there might be preallocation past i_size. | |
2797 | */ | |
2798 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, (u64)-1, 0); | |
2799 | /* There can't be a file extent item at offset (u64)-1 */ | |
2800 | ASSERT(ret != 0); | |
2801 | if (ret < 0) | |
2802 | return ret; | |
2803 | ||
2804 | /* | |
2805 | * For a non-existing key, btrfs_search_slot() always leaves us at a | |
2806 | * slot > 0, except if the btree is empty, which is impossible because | |
2807 | * at least it has the inode item for this inode and all the items for | |
2808 | * the root inode 256. | |
2809 | */ | |
2810 | ASSERT(path->slots[0] > 0); | |
2811 | path->slots[0]--; | |
2812 | leaf = path->nodes[0]; | |
2813 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
2814 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) { | |
2815 | /* No file extent items in the subvolume tree. */ | |
2816 | *last_extent_end_ret = 0; | |
2817 | return 0; | |
975f84fe | 2818 | } |
975f84fe | 2819 | |
ec29ed5b | 2820 | /* |
ac3c0d36 FM |
2821 | * For an inline extent, the disk_bytenr is where inline data starts at, |
2822 | * so first check if we have an inline extent item before checking if we | |
2823 | * have an implicit hole (disk_bytenr == 0). | |
ec29ed5b | 2824 | */ |
ac3c0d36 FM |
2825 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); |
2826 | if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_INLINE) { | |
2827 | *last_extent_end_ret = btrfs_file_extent_end(path); | |
2828 | return 0; | |
ec29ed5b CM |
2829 | } |
2830 | ||
ac3c0d36 FM |
2831 | /* |
2832 | * Find the last file extent item that is not a hole (when NO_HOLES is | |
2833 | * not enabled). This should take at most 2 iterations in the worst | |
2834 | * case: we have one hole file extent item at slot 0 of a leaf and | |
2835 | * another hole file extent item as the last item in the previous leaf. | |
2836 | * This is because we merge file extent items that represent holes. | |
2837 | */ | |
2838 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); | |
2839 | while (disk_bytenr == 0) { | |
2840 | ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY); | |
2841 | if (ret < 0) { | |
2842 | return ret; | |
2843 | } else if (ret > 0) { | |
2844 | /* No file extent items that are not holes. */ | |
2845 | *last_extent_end_ret = 0; | |
2846 | return 0; | |
2847 | } | |
2848 | leaf = path->nodes[0]; | |
2849 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
2850 | struct btrfs_file_extent_item); | |
2851 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); | |
2852 | } | |
ec29ed5b | 2853 | |
ac3c0d36 FM |
2854 | *last_extent_end_ret = btrfs_file_extent_end(path); |
2855 | return 0; | |
2856 | } | |
2857 | ||
2858 | int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo, | |
2859 | u64 start, u64 len) | |
2860 | { | |
2861 | const u64 ino = btrfs_ino(inode); | |
2862 | struct extent_state *cached_state = NULL; | |
b3e744fe | 2863 | struct extent_state *delalloc_cached_state = NULL; |
ac3c0d36 | 2864 | struct btrfs_path *path; |
ac3c0d36 | 2865 | struct fiemap_cache cache = { 0 }; |
61dbb952 | 2866 | struct btrfs_backref_share_check_ctx *backref_ctx; |
ac3c0d36 FM |
2867 | u64 last_extent_end; |
2868 | u64 prev_extent_end; | |
2869 | u64 lockstart; | |
2870 | u64 lockend; | |
2871 | bool stopped = false; | |
2872 | int ret; | |
2873 | ||
84a7949d | 2874 | backref_ctx = btrfs_alloc_backref_share_check_ctx(); |
ac3c0d36 | 2875 | path = btrfs_alloc_path(); |
84a7949d | 2876 | if (!backref_ctx || !path) { |
ac3c0d36 | 2877 | ret = -ENOMEM; |
1506fcc8 YS |
2878 | goto out; |
2879 | } | |
975f84fe | 2880 | |
ceb707da FM |
2881 | lockstart = round_down(start, inode->root->fs_info->sectorsize); |
2882 | lockend = round_up(start + len, inode->root->fs_info->sectorsize); | |
ac3c0d36 | 2883 | prev_extent_end = lockstart; |
ea8efc74 | 2884 | |
519b7e13 | 2885 | btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED); |
570eb97b | 2886 | lock_extent(&inode->io_tree, lockstart, lockend, &cached_state); |
ea8efc74 | 2887 | |
ac3c0d36 FM |
2888 | ret = fiemap_find_last_extent_offset(inode, path, &last_extent_end); |
2889 | if (ret < 0) | |
2890 | goto out_unlock; | |
2891 | btrfs_release_path(path); | |
1506fcc8 | 2892 | |
ac3c0d36 FM |
2893 | path->reada = READA_FORWARD; |
2894 | ret = fiemap_search_slot(inode, path, lockstart); | |
2895 | if (ret < 0) { | |
2896 | goto out_unlock; | |
2897 | } else if (ret > 0) { | |
ea8efc74 | 2898 | /* |
ac3c0d36 FM |
2899 | * No file extent item found, but we may have delalloc between |
2900 | * the current offset and i_size. So check for that. | |
ea8efc74 | 2901 | */ |
ac3c0d36 FM |
2902 | ret = 0; |
2903 | goto check_eof_delalloc; | |
2904 | } | |
2905 | ||
2906 | while (prev_extent_end < lockend) { | |
2907 | struct extent_buffer *leaf = path->nodes[0]; | |
2908 | struct btrfs_file_extent_item *ei; | |
2909 | struct btrfs_key key; | |
2910 | u64 extent_end; | |
2911 | u64 extent_len; | |
2912 | u64 extent_offset = 0; | |
2913 | u64 extent_gen; | |
2914 | u64 disk_bytenr = 0; | |
2915 | u64 flags = 0; | |
2916 | int extent_type; | |
2917 | u8 compression; | |
2918 | ||
2919 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
2920 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) | |
2921 | break; | |
2922 | ||
2923 | extent_end = btrfs_file_extent_end(path); | |
1506fcc8 | 2924 | |
ea8efc74 | 2925 | /* |
ac3c0d36 FM |
2926 | * The first iteration can leave us at an extent item that ends |
2927 | * before our range's start. Move to the next item. | |
ea8efc74 | 2928 | */ |
ac3c0d36 FM |
2929 | if (extent_end <= lockstart) |
2930 | goto next_item; | |
fe09e16c | 2931 | |
877c1476 FM |
2932 | backref_ctx->curr_leaf_bytenr = leaf->start; |
2933 | ||
ac3c0d36 FM |
2934 | /* We have in implicit hole (NO_HOLES feature enabled). */ |
2935 | if (prev_extent_end < key.offset) { | |
2936 | const u64 range_end = min(key.offset, lockend) - 1; | |
b8f164e3 | 2937 | |
ac3c0d36 | 2938 | ret = fiemap_process_hole(inode, fieinfo, &cache, |
b3e744fe | 2939 | &delalloc_cached_state, |
61dbb952 | 2940 | backref_ctx, 0, 0, 0, |
ac3c0d36 FM |
2941 | prev_extent_end, range_end); |
2942 | if (ret < 0) { | |
2943 | goto out_unlock; | |
2944 | } else if (ret > 0) { | |
2945 | /* fiemap_fill_next_extent() told us to stop. */ | |
2946 | stopped = true; | |
2947 | break; | |
2948 | } | |
1506fcc8 | 2949 | |
ac3c0d36 FM |
2950 | /* We've reached the end of the fiemap range, stop. */ |
2951 | if (key.offset >= lockend) { | |
2952 | stopped = true; | |
2953 | break; | |
2954 | } | |
1506fcc8 YS |
2955 | } |
2956 | ||
ac3c0d36 FM |
2957 | extent_len = extent_end - key.offset; |
2958 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
2959 | struct btrfs_file_extent_item); | |
2960 | compression = btrfs_file_extent_compression(leaf, ei); | |
2961 | extent_type = btrfs_file_extent_type(leaf, ei); | |
2962 | extent_gen = btrfs_file_extent_generation(leaf, ei); | |
2963 | ||
2964 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
2965 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); | |
2966 | if (compression == BTRFS_COMPRESS_NONE) | |
2967 | extent_offset = btrfs_file_extent_offset(leaf, ei); | |
ec29ed5b | 2968 | } |
ac3c0d36 FM |
2969 | |
2970 | if (compression != BTRFS_COMPRESS_NONE) | |
2971 | flags |= FIEMAP_EXTENT_ENCODED; | |
2972 | ||
2973 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
2974 | flags |= FIEMAP_EXTENT_DATA_INLINE; | |
2975 | flags |= FIEMAP_EXTENT_NOT_ALIGNED; | |
2976 | ret = emit_fiemap_extent(fieinfo, &cache, key.offset, 0, | |
2977 | extent_len, flags); | |
2978 | } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
2979 | ret = fiemap_process_hole(inode, fieinfo, &cache, | |
b3e744fe | 2980 | &delalloc_cached_state, |
61dbb952 | 2981 | backref_ctx, |
ac3c0d36 | 2982 | disk_bytenr, extent_offset, |
84a7949d FM |
2983 | extent_gen, key.offset, |
2984 | extent_end - 1); | |
ac3c0d36 FM |
2985 | } else if (disk_bytenr == 0) { |
2986 | /* We have an explicit hole. */ | |
2987 | ret = fiemap_process_hole(inode, fieinfo, &cache, | |
b3e744fe | 2988 | &delalloc_cached_state, |
61dbb952 | 2989 | backref_ctx, 0, 0, 0, |
ac3c0d36 FM |
2990 | key.offset, extent_end - 1); |
2991 | } else { | |
2992 | /* We have a regular extent. */ | |
2993 | if (fieinfo->fi_extents_max) { | |
ceb707da | 2994 | ret = btrfs_is_data_extent_shared(inode, |
ac3c0d36 FM |
2995 | disk_bytenr, |
2996 | extent_gen, | |
61dbb952 | 2997 | backref_ctx); |
ac3c0d36 FM |
2998 | if (ret < 0) |
2999 | goto out_unlock; | |
3000 | else if (ret > 0) | |
3001 | flags |= FIEMAP_EXTENT_SHARED; | |
3002 | } | |
3003 | ||
3004 | ret = emit_fiemap_extent(fieinfo, &cache, key.offset, | |
3005 | disk_bytenr + extent_offset, | |
3006 | extent_len, flags); | |
975f84fe | 3007 | } |
ac3c0d36 FM |
3008 | |
3009 | if (ret < 0) { | |
3010 | goto out_unlock; | |
3011 | } else if (ret > 0) { | |
3012 | /* fiemap_fill_next_extent() told us to stop. */ | |
3013 | stopped = true; | |
3014 | break; | |
26e726af | 3015 | } |
09fbc1c8 | 3016 | |
ac3c0d36 FM |
3017 | prev_extent_end = extent_end; |
3018 | next_item: | |
09fbc1c8 FM |
3019 | if (fatal_signal_pending(current)) { |
3020 | ret = -EINTR; | |
ac3c0d36 | 3021 | goto out_unlock; |
09fbc1c8 | 3022 | } |
ac3c0d36 FM |
3023 | |
3024 | ret = fiemap_next_leaf_item(inode, path); | |
3025 | if (ret < 0) { | |
3026 | goto out_unlock; | |
3027 | } else if (ret > 0) { | |
3028 | /* No more file extent items for this inode. */ | |
3029 | break; | |
3030 | } | |
3031 | cond_resched(); | |
1506fcc8 | 3032 | } |
5911c8fe | 3033 | |
ac3c0d36 FM |
3034 | check_eof_delalloc: |
3035 | /* | |
3036 | * Release (and free) the path before emitting any final entries to | |
3037 | * fiemap_fill_next_extent() to keep lockdep happy. This is because | |
3038 | * once we find no more file extent items exist, we may have a | |
3039 | * non-cloned leaf, and fiemap_fill_next_extent() can trigger page | |
3040 | * faults when copying data to the user space buffer. | |
3041 | */ | |
3042 | btrfs_free_path(path); | |
3043 | path = NULL; | |
3044 | ||
3045 | if (!stopped && prev_extent_end < lockend) { | |
b3e744fe FM |
3046 | ret = fiemap_process_hole(inode, fieinfo, &cache, |
3047 | &delalloc_cached_state, backref_ctx, | |
84a7949d | 3048 | 0, 0, 0, prev_extent_end, lockend - 1); |
ac3c0d36 FM |
3049 | if (ret < 0) |
3050 | goto out_unlock; | |
3051 | prev_extent_end = lockend; | |
3052 | } | |
3053 | ||
3054 | if (cache.cached && cache.offset + cache.len >= last_extent_end) { | |
3055 | const u64 i_size = i_size_read(&inode->vfs_inode); | |
3056 | ||
3057 | if (prev_extent_end < i_size) { | |
3058 | u64 delalloc_start; | |
3059 | u64 delalloc_end; | |
3060 | bool delalloc; | |
3061 | ||
3062 | delalloc = btrfs_find_delalloc_in_range(inode, | |
3063 | prev_extent_end, | |
3064 | i_size - 1, | |
b3e744fe | 3065 | &delalloc_cached_state, |
ac3c0d36 FM |
3066 | &delalloc_start, |
3067 | &delalloc_end); | |
3068 | if (!delalloc) | |
3069 | cache.flags |= FIEMAP_EXTENT_LAST; | |
3070 | } else { | |
3071 | cache.flags |= FIEMAP_EXTENT_LAST; | |
3072 | } | |
3073 | } | |
3074 | ||
3075 | ret = emit_last_fiemap_cache(fieinfo, &cache); | |
3076 | ||
3077 | out_unlock: | |
570eb97b | 3078 | unlock_extent(&inode->io_tree, lockstart, lockend, &cached_state); |
519b7e13 | 3079 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
ac3c0d36 | 3080 | out: |
b3e744fe | 3081 | free_extent_state(delalloc_cached_state); |
84a7949d | 3082 | btrfs_free_backref_share_ctx(backref_ctx); |
e02d48ea | 3083 | btrfs_free_path(path); |
1506fcc8 YS |
3084 | return ret; |
3085 | } | |
3086 | ||
727011e0 CM |
3087 | static void __free_extent_buffer(struct extent_buffer *eb) |
3088 | { | |
727011e0 CM |
3089 | kmem_cache_free(extent_buffer_cache, eb); |
3090 | } | |
3091 | ||
7f26fb1c | 3092 | static int extent_buffer_under_io(const struct extent_buffer *eb) |
db7f3436 | 3093 | { |
113fa05c | 3094 | return (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
db7f3436 JB |
3095 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
3096 | } | |
3097 | ||
8ff8466d | 3098 | static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page) |
db7f3436 | 3099 | { |
8ff8466d | 3100 | struct btrfs_subpage *subpage; |
db7f3436 | 3101 | |
8ff8466d | 3102 | lockdep_assert_held(&page->mapping->private_lock); |
db7f3436 | 3103 | |
8ff8466d QW |
3104 | if (PagePrivate(page)) { |
3105 | subpage = (struct btrfs_subpage *)page->private; | |
3106 | if (atomic_read(&subpage->eb_refs)) | |
3107 | return true; | |
3d078efa QW |
3108 | /* |
3109 | * Even there is no eb refs here, we may still have | |
3110 | * end_page_read() call relying on page::private. | |
3111 | */ | |
3112 | if (atomic_read(&subpage->readers)) | |
3113 | return true; | |
8ff8466d QW |
3114 | } |
3115 | return false; | |
3116 | } | |
db7f3436 | 3117 | |
8ff8466d QW |
3118 | static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *page) |
3119 | { | |
3120 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
3121 | const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); | |
3122 | ||
3123 | /* | |
3124 | * For mapped eb, we're going to change the page private, which should | |
3125 | * be done under the private_lock. | |
3126 | */ | |
3127 | if (mapped) | |
3128 | spin_lock(&page->mapping->private_lock); | |
3129 | ||
3130 | if (!PagePrivate(page)) { | |
5d2361db | 3131 | if (mapped) |
8ff8466d QW |
3132 | spin_unlock(&page->mapping->private_lock); |
3133 | return; | |
3134 | } | |
3135 | ||
fbca46eb | 3136 | if (fs_info->nodesize >= PAGE_SIZE) { |
5d2361db FL |
3137 | /* |
3138 | * We do this since we'll remove the pages after we've | |
3139 | * removed the eb from the radix tree, so we could race | |
3140 | * and have this page now attached to the new eb. So | |
3141 | * only clear page_private if it's still connected to | |
3142 | * this eb. | |
3143 | */ | |
3144 | if (PagePrivate(page) && | |
3145 | page->private == (unsigned long)eb) { | |
3146 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); | |
3147 | BUG_ON(PageDirty(page)); | |
3148 | BUG_ON(PageWriteback(page)); | |
db7f3436 | 3149 | /* |
5d2361db FL |
3150 | * We need to make sure we haven't be attached |
3151 | * to a new eb. | |
db7f3436 | 3152 | */ |
d1b89bc0 | 3153 | detach_page_private(page); |
db7f3436 | 3154 | } |
5d2361db FL |
3155 | if (mapped) |
3156 | spin_unlock(&page->mapping->private_lock); | |
8ff8466d QW |
3157 | return; |
3158 | } | |
3159 | ||
3160 | /* | |
3161 | * For subpage, we can have dummy eb with page private. In this case, | |
3162 | * we can directly detach the private as such page is only attached to | |
3163 | * one dummy eb, no sharing. | |
3164 | */ | |
3165 | if (!mapped) { | |
3166 | btrfs_detach_subpage(fs_info, page); | |
3167 | return; | |
3168 | } | |
3169 | ||
3170 | btrfs_page_dec_eb_refs(fs_info, page); | |
3171 | ||
3172 | /* | |
3173 | * We can only detach the page private if there are no other ebs in the | |
3d078efa | 3174 | * page range and no unfinished IO. |
8ff8466d QW |
3175 | */ |
3176 | if (!page_range_has_eb(fs_info, page)) | |
3177 | btrfs_detach_subpage(fs_info, page); | |
3178 | ||
3179 | spin_unlock(&page->mapping->private_lock); | |
3180 | } | |
3181 | ||
3182 | /* Release all pages attached to the extent buffer */ | |
3183 | static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb) | |
3184 | { | |
3185 | int i; | |
3186 | int num_pages; | |
3187 | ||
3188 | ASSERT(!extent_buffer_under_io(eb)); | |
3189 | ||
3190 | num_pages = num_extent_pages(eb); | |
3191 | for (i = 0; i < num_pages; i++) { | |
3192 | struct page *page = eb->pages[i]; | |
3193 | ||
3194 | if (!page) | |
3195 | continue; | |
3196 | ||
3197 | detach_extent_buffer_page(eb, page); | |
5d2361db | 3198 | |
01327610 | 3199 | /* One for when we allocated the page */ |
09cbfeaf | 3200 | put_page(page); |
d64766fd | 3201 | } |
db7f3436 JB |
3202 | } |
3203 | ||
3204 | /* | |
3205 | * Helper for releasing the extent buffer. | |
3206 | */ | |
3207 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) | |
3208 | { | |
55ac0139 | 3209 | btrfs_release_extent_buffer_pages(eb); |
a40246e8 | 3210 | btrfs_leak_debug_del_eb(eb); |
db7f3436 JB |
3211 | __free_extent_buffer(eb); |
3212 | } | |
3213 | ||
f28491e0 JB |
3214 | static struct extent_buffer * |
3215 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, | |
23d79d81 | 3216 | unsigned long len) |
d1310b2e CM |
3217 | { |
3218 | struct extent_buffer *eb = NULL; | |
3219 | ||
d1b5c567 | 3220 | eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); |
d1310b2e CM |
3221 | eb->start = start; |
3222 | eb->len = len; | |
f28491e0 | 3223 | eb->fs_info = fs_info; |
196d59ab | 3224 | init_rwsem(&eb->lock); |
b4ce94de | 3225 | |
a40246e8 | 3226 | btrfs_leak_debug_add_eb(eb); |
6d49ba1b | 3227 | |
3083ee2e | 3228 | spin_lock_init(&eb->refs_lock); |
d1310b2e | 3229 | atomic_set(&eb->refs, 1); |
727011e0 | 3230 | |
deb67895 | 3231 | ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE); |
d1310b2e CM |
3232 | |
3233 | return eb; | |
3234 | } | |
3235 | ||
2b48966a | 3236 | struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src) |
815a51c7 | 3237 | { |
cc5e31a4 | 3238 | int i; |
815a51c7 | 3239 | struct extent_buffer *new; |
cc5e31a4 | 3240 | int num_pages = num_extent_pages(src); |
dd137dd1 | 3241 | int ret; |
815a51c7 | 3242 | |
3f556f78 | 3243 | new = __alloc_extent_buffer(src->fs_info, src->start, src->len); |
815a51c7 JS |
3244 | if (new == NULL) |
3245 | return NULL; | |
3246 | ||
62c053fb QW |
3247 | /* |
3248 | * Set UNMAPPED before calling btrfs_release_extent_buffer(), as | |
3249 | * btrfs_release_extent_buffer() have different behavior for | |
3250 | * UNMAPPED subpage extent buffer. | |
3251 | */ | |
3252 | set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags); | |
3253 | ||
dd137dd1 STD |
3254 | ret = btrfs_alloc_page_array(num_pages, new->pages); |
3255 | if (ret) { | |
3256 | btrfs_release_extent_buffer(new); | |
3257 | return NULL; | |
3258 | } | |
3259 | ||
815a51c7 | 3260 | for (i = 0; i < num_pages; i++) { |
760f991f | 3261 | int ret; |
dd137dd1 | 3262 | struct page *p = new->pages[i]; |
760f991f | 3263 | |
760f991f QW |
3264 | ret = attach_extent_buffer_page(new, p, NULL); |
3265 | if (ret < 0) { | |
760f991f QW |
3266 | btrfs_release_extent_buffer(new); |
3267 | return NULL; | |
3268 | } | |
815a51c7 | 3269 | WARN_ON(PageDirty(p)); |
fba1acf9 | 3270 | copy_page(page_address(p), page_address(src->pages[i])); |
815a51c7 | 3271 | } |
92d83e94 | 3272 | set_extent_buffer_uptodate(new); |
815a51c7 JS |
3273 | |
3274 | return new; | |
3275 | } | |
3276 | ||
0f331229 OS |
3277 | struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
3278 | u64 start, unsigned long len) | |
815a51c7 JS |
3279 | { |
3280 | struct extent_buffer *eb; | |
cc5e31a4 DS |
3281 | int num_pages; |
3282 | int i; | |
dd137dd1 | 3283 | int ret; |
815a51c7 | 3284 | |
3f556f78 | 3285 | eb = __alloc_extent_buffer(fs_info, start, len); |
815a51c7 JS |
3286 | if (!eb) |
3287 | return NULL; | |
3288 | ||
65ad0104 | 3289 | num_pages = num_extent_pages(eb); |
dd137dd1 STD |
3290 | ret = btrfs_alloc_page_array(num_pages, eb->pages); |
3291 | if (ret) | |
3292 | goto err; | |
3293 | ||
815a51c7 | 3294 | for (i = 0; i < num_pages; i++) { |
dd137dd1 | 3295 | struct page *p = eb->pages[i]; |
09bc1f0f | 3296 | |
dd137dd1 | 3297 | ret = attach_extent_buffer_page(eb, p, NULL); |
09bc1f0f QW |
3298 | if (ret < 0) |
3299 | goto err; | |
815a51c7 | 3300 | } |
dd137dd1 | 3301 | |
815a51c7 JS |
3302 | set_extent_buffer_uptodate(eb); |
3303 | btrfs_set_header_nritems(eb, 0); | |
b0132a3b | 3304 | set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); |
815a51c7 JS |
3305 | |
3306 | return eb; | |
3307 | err: | |
dd137dd1 STD |
3308 | for (i = 0; i < num_pages; i++) { |
3309 | if (eb->pages[i]) { | |
3310 | detach_extent_buffer_page(eb, eb->pages[i]); | |
3311 | __free_page(eb->pages[i]); | |
3312 | } | |
09bc1f0f | 3313 | } |
815a51c7 JS |
3314 | __free_extent_buffer(eb); |
3315 | return NULL; | |
3316 | } | |
3317 | ||
0f331229 | 3318 | struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
da17066c | 3319 | u64 start) |
0f331229 | 3320 | { |
da17066c | 3321 | return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize); |
0f331229 OS |
3322 | } |
3323 | ||
0b32f4bb JB |
3324 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
3325 | { | |
242e18c7 | 3326 | int refs; |
6bf9cd2e BB |
3327 | /* |
3328 | * The TREE_REF bit is first set when the extent_buffer is added | |
3329 | * to the radix tree. It is also reset, if unset, when a new reference | |
3330 | * is created by find_extent_buffer. | |
0b32f4bb | 3331 | * |
6bf9cd2e BB |
3332 | * It is only cleared in two cases: freeing the last non-tree |
3333 | * reference to the extent_buffer when its STALE bit is set or | |
f913cff3 | 3334 | * calling release_folio when the tree reference is the only reference. |
0b32f4bb | 3335 | * |
6bf9cd2e | 3336 | * In both cases, care is taken to ensure that the extent_buffer's |
f913cff3 | 3337 | * pages are not under io. However, release_folio can be concurrently |
6bf9cd2e BB |
3338 | * called with creating new references, which is prone to race |
3339 | * conditions between the calls to check_buffer_tree_ref in those | |
3340 | * codepaths and clearing TREE_REF in try_release_extent_buffer. | |
0b32f4bb | 3341 | * |
6bf9cd2e BB |
3342 | * The actual lifetime of the extent_buffer in the radix tree is |
3343 | * adequately protected by the refcount, but the TREE_REF bit and | |
3344 | * its corresponding reference are not. To protect against this | |
3345 | * class of races, we call check_buffer_tree_ref from the codepaths | |
113fa05c CH |
3346 | * which trigger io. Note that once io is initiated, TREE_REF can no |
3347 | * longer be cleared, so that is the moment at which any such race is | |
3348 | * best fixed. | |
0b32f4bb | 3349 | */ |
242e18c7 CM |
3350 | refs = atomic_read(&eb->refs); |
3351 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) | |
3352 | return; | |
3353 | ||
594831c4 JB |
3354 | spin_lock(&eb->refs_lock); |
3355 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) | |
0b32f4bb | 3356 | atomic_inc(&eb->refs); |
594831c4 | 3357 | spin_unlock(&eb->refs_lock); |
0b32f4bb JB |
3358 | } |
3359 | ||
2457aec6 MG |
3360 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
3361 | struct page *accessed) | |
5df4235e | 3362 | { |
cc5e31a4 | 3363 | int num_pages, i; |
5df4235e | 3364 | |
0b32f4bb JB |
3365 | check_buffer_tree_ref(eb); |
3366 | ||
65ad0104 | 3367 | num_pages = num_extent_pages(eb); |
5df4235e | 3368 | for (i = 0; i < num_pages; i++) { |
fb85fc9a DS |
3369 | struct page *p = eb->pages[i]; |
3370 | ||
2457aec6 MG |
3371 | if (p != accessed) |
3372 | mark_page_accessed(p); | |
5df4235e JB |
3373 | } |
3374 | } | |
3375 | ||
f28491e0 JB |
3376 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
3377 | u64 start) | |
452c75c3 CS |
3378 | { |
3379 | struct extent_buffer *eb; | |
3380 | ||
2f3186d8 QW |
3381 | eb = find_extent_buffer_nolock(fs_info, start); |
3382 | if (!eb) | |
3383 | return NULL; | |
3384 | /* | |
3385 | * Lock our eb's refs_lock to avoid races with free_extent_buffer(). | |
3386 | * When we get our eb it might be flagged with EXTENT_BUFFER_STALE and | |
3387 | * another task running free_extent_buffer() might have seen that flag | |
3388 | * set, eb->refs == 2, that the buffer isn't under IO (dirty and | |
3389 | * writeback flags not set) and it's still in the tree (flag | |
3390 | * EXTENT_BUFFER_TREE_REF set), therefore being in the process of | |
3391 | * decrementing the extent buffer's reference count twice. So here we | |
3392 | * could race and increment the eb's reference count, clear its stale | |
3393 | * flag, mark it as dirty and drop our reference before the other task | |
3394 | * finishes executing free_extent_buffer, which would later result in | |
3395 | * an attempt to free an extent buffer that is dirty. | |
3396 | */ | |
3397 | if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { | |
3398 | spin_lock(&eb->refs_lock); | |
3399 | spin_unlock(&eb->refs_lock); | |
452c75c3 | 3400 | } |
2f3186d8 QW |
3401 | mark_extent_buffer_accessed(eb, NULL); |
3402 | return eb; | |
452c75c3 CS |
3403 | } |
3404 | ||
faa2dbf0 JB |
3405 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
3406 | struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, | |
da17066c | 3407 | u64 start) |
faa2dbf0 JB |
3408 | { |
3409 | struct extent_buffer *eb, *exists = NULL; | |
3410 | int ret; | |
3411 | ||
3412 | eb = find_extent_buffer(fs_info, start); | |
3413 | if (eb) | |
3414 | return eb; | |
da17066c | 3415 | eb = alloc_dummy_extent_buffer(fs_info, start); |
faa2dbf0 | 3416 | if (!eb) |
b6293c82 | 3417 | return ERR_PTR(-ENOMEM); |
faa2dbf0 | 3418 | eb->fs_info = fs_info; |
01cd3909 DS |
3419 | again: |
3420 | ret = radix_tree_preload(GFP_NOFS); | |
3421 | if (ret) { | |
3422 | exists = ERR_PTR(ret); | |
3423 | goto free_eb; | |
3424 | } | |
3425 | spin_lock(&fs_info->buffer_lock); | |
3426 | ret = radix_tree_insert(&fs_info->buffer_radix, | |
3427 | start >> fs_info->sectorsize_bits, eb); | |
3428 | spin_unlock(&fs_info->buffer_lock); | |
3429 | radix_tree_preload_end(); | |
3430 | if (ret == -EEXIST) { | |
3431 | exists = find_extent_buffer(fs_info, start); | |
3432 | if (exists) | |
faa2dbf0 | 3433 | goto free_eb; |
01cd3909 DS |
3434 | else |
3435 | goto again; | |
3436 | } | |
faa2dbf0 JB |
3437 | check_buffer_tree_ref(eb); |
3438 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); | |
3439 | ||
faa2dbf0 JB |
3440 | return eb; |
3441 | free_eb: | |
3442 | btrfs_release_extent_buffer(eb); | |
3443 | return exists; | |
3444 | } | |
3445 | #endif | |
3446 | ||
81982210 QW |
3447 | static struct extent_buffer *grab_extent_buffer( |
3448 | struct btrfs_fs_info *fs_info, struct page *page) | |
c0f0a9e7 QW |
3449 | { |
3450 | struct extent_buffer *exists; | |
3451 | ||
81982210 QW |
3452 | /* |
3453 | * For subpage case, we completely rely on radix tree to ensure we | |
3454 | * don't try to insert two ebs for the same bytenr. So here we always | |
3455 | * return NULL and just continue. | |
3456 | */ | |
fbca46eb | 3457 | if (fs_info->nodesize < PAGE_SIZE) |
81982210 QW |
3458 | return NULL; |
3459 | ||
c0f0a9e7 QW |
3460 | /* Page not yet attached to an extent buffer */ |
3461 | if (!PagePrivate(page)) | |
3462 | return NULL; | |
3463 | ||
3464 | /* | |
3465 | * We could have already allocated an eb for this page and attached one | |
3466 | * so lets see if we can get a ref on the existing eb, and if we can we | |
3467 | * know it's good and we can just return that one, else we know we can | |
3468 | * just overwrite page->private. | |
3469 | */ | |
3470 | exists = (struct extent_buffer *)page->private; | |
3471 | if (atomic_inc_not_zero(&exists->refs)) | |
3472 | return exists; | |
3473 | ||
3474 | WARN_ON(PageDirty(page)); | |
3475 | detach_page_private(page); | |
3476 | return NULL; | |
3477 | } | |
3478 | ||
fbca46eb QW |
3479 | static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) |
3480 | { | |
3481 | if (!IS_ALIGNED(start, fs_info->sectorsize)) { | |
3482 | btrfs_err(fs_info, "bad tree block start %llu", start); | |
3483 | return -EINVAL; | |
3484 | } | |
3485 | ||
3486 | if (fs_info->nodesize < PAGE_SIZE && | |
3487 | offset_in_page(start) + fs_info->nodesize > PAGE_SIZE) { | |
3488 | btrfs_err(fs_info, | |
3489 | "tree block crosses page boundary, start %llu nodesize %u", | |
3490 | start, fs_info->nodesize); | |
3491 | return -EINVAL; | |
3492 | } | |
3493 | if (fs_info->nodesize >= PAGE_SIZE && | |
1280d2d1 | 3494 | !PAGE_ALIGNED(start)) { |
fbca46eb QW |
3495 | btrfs_err(fs_info, |
3496 | "tree block is not page aligned, start %llu nodesize %u", | |
3497 | start, fs_info->nodesize); | |
3498 | return -EINVAL; | |
3499 | } | |
3500 | return 0; | |
3501 | } | |
3502 | ||
f28491e0 | 3503 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
3fbaf258 | 3504 | u64 start, u64 owner_root, int level) |
d1310b2e | 3505 | { |
da17066c | 3506 | unsigned long len = fs_info->nodesize; |
cc5e31a4 DS |
3507 | int num_pages; |
3508 | int i; | |
09cbfeaf | 3509 | unsigned long index = start >> PAGE_SHIFT; |
d1310b2e | 3510 | struct extent_buffer *eb; |
6af118ce | 3511 | struct extent_buffer *exists = NULL; |
d1310b2e | 3512 | struct page *p; |
f28491e0 | 3513 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
52ea5bfb | 3514 | struct btrfs_subpage *prealloc = NULL; |
b40130b2 | 3515 | u64 lockdep_owner = owner_root; |
d1310b2e | 3516 | int uptodate = 1; |
19fe0a8b | 3517 | int ret; |
d1310b2e | 3518 | |
fbca46eb | 3519 | if (check_eb_alignment(fs_info, start)) |
c871b0f2 | 3520 | return ERR_PTR(-EINVAL); |
c871b0f2 | 3521 | |
e9306ad4 QW |
3522 | #if BITS_PER_LONG == 32 |
3523 | if (start >= MAX_LFS_FILESIZE) { | |
3524 | btrfs_err_rl(fs_info, | |
3525 | "extent buffer %llu is beyond 32bit page cache limit", start); | |
3526 | btrfs_err_32bit_limit(fs_info); | |
3527 | return ERR_PTR(-EOVERFLOW); | |
3528 | } | |
3529 | if (start >= BTRFS_32BIT_EARLY_WARN_THRESHOLD) | |
3530 | btrfs_warn_32bit_limit(fs_info); | |
3531 | #endif | |
3532 | ||
f28491e0 | 3533 | eb = find_extent_buffer(fs_info, start); |
452c75c3 | 3534 | if (eb) |
6af118ce | 3535 | return eb; |
6af118ce | 3536 | |
23d79d81 | 3537 | eb = __alloc_extent_buffer(fs_info, start, len); |
2b114d1d | 3538 | if (!eb) |
c871b0f2 | 3539 | return ERR_PTR(-ENOMEM); |
b40130b2 JB |
3540 | |
3541 | /* | |
3542 | * The reloc trees are just snapshots, so we need them to appear to be | |
3543 | * just like any other fs tree WRT lockdep. | |
3544 | */ | |
3545 | if (lockdep_owner == BTRFS_TREE_RELOC_OBJECTID) | |
3546 | lockdep_owner = BTRFS_FS_TREE_OBJECTID; | |
3547 | ||
3548 | btrfs_set_buffer_lockdep_class(lockdep_owner, eb, level); | |
d1310b2e | 3549 | |
65ad0104 | 3550 | num_pages = num_extent_pages(eb); |
760f991f | 3551 | |
52ea5bfb QW |
3552 | /* |
3553 | * Preallocate page->private for subpage case, so that we won't | |
3554 | * allocate memory with private_lock nor page lock hold. | |
3555 | * | |
3556 | * The memory will be freed by attach_extent_buffer_page() or freed | |
3557 | * manually if we exit earlier. | |
3558 | */ | |
3559 | if (fs_info->nodesize < PAGE_SIZE) { | |
3560 | prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA); | |
3561 | if (IS_ERR(prealloc)) { | |
3562 | exists = ERR_CAST(prealloc); | |
3563 | goto free_eb; | |
3564 | } | |
3565 | } | |
3566 | ||
3567 | for (i = 0; i < num_pages; i++, index++) { | |
d1b5c567 | 3568 | p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); |
c871b0f2 LB |
3569 | if (!p) { |
3570 | exists = ERR_PTR(-ENOMEM); | |
52ea5bfb | 3571 | btrfs_free_subpage(prealloc); |
6af118ce | 3572 | goto free_eb; |
c871b0f2 | 3573 | } |
4f2de97a JB |
3574 | |
3575 | spin_lock(&mapping->private_lock); | |
81982210 | 3576 | exists = grab_extent_buffer(fs_info, p); |
c0f0a9e7 QW |
3577 | if (exists) { |
3578 | spin_unlock(&mapping->private_lock); | |
3579 | unlock_page(p); | |
3580 | put_page(p); | |
3581 | mark_extent_buffer_accessed(exists, p); | |
760f991f | 3582 | btrfs_free_subpage(prealloc); |
c0f0a9e7 | 3583 | goto free_eb; |
d1310b2e | 3584 | } |
760f991f QW |
3585 | /* Should not fail, as we have preallocated the memory */ |
3586 | ret = attach_extent_buffer_page(eb, p, prealloc); | |
3587 | ASSERT(!ret); | |
8ff8466d QW |
3588 | /* |
3589 | * To inform we have extra eb under allocation, so that | |
3590 | * detach_extent_buffer_page() won't release the page private | |
3591 | * when the eb hasn't yet been inserted into radix tree. | |
3592 | * | |
3593 | * The ref will be decreased when the eb released the page, in | |
3594 | * detach_extent_buffer_page(). | |
3595 | * Thus needs no special handling in error path. | |
3596 | */ | |
3597 | btrfs_page_inc_eb_refs(fs_info, p); | |
4f2de97a | 3598 | spin_unlock(&mapping->private_lock); |
760f991f | 3599 | |
1e5eb3d6 | 3600 | WARN_ON(btrfs_page_test_dirty(fs_info, p, eb->start, eb->len)); |
727011e0 | 3601 | eb->pages[i] = p; |
5a963419 | 3602 | if (!btrfs_page_test_uptodate(fs_info, p, eb->start, eb->len)) |
d1310b2e | 3603 | uptodate = 0; |
eb14ab8e CM |
3604 | |
3605 | /* | |
b16d011e NB |
3606 | * We can't unlock the pages just yet since the extent buffer |
3607 | * hasn't been properly inserted in the radix tree, this | |
f913cff3 | 3608 | * opens a race with btree_release_folio which can free a page |
b16d011e NB |
3609 | * while we are still filling in all pages for the buffer and |
3610 | * we could crash. | |
eb14ab8e | 3611 | */ |
d1310b2e CM |
3612 | } |
3613 | if (uptodate) | |
b4ce94de | 3614 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
01cd3909 DS |
3615 | again: |
3616 | ret = radix_tree_preload(GFP_NOFS); | |
3617 | if (ret) { | |
3618 | exists = ERR_PTR(ret); | |
3619 | goto free_eb; | |
3620 | } | |
3621 | ||
3622 | spin_lock(&fs_info->buffer_lock); | |
3623 | ret = radix_tree_insert(&fs_info->buffer_radix, | |
3624 | start >> fs_info->sectorsize_bits, eb); | |
3625 | spin_unlock(&fs_info->buffer_lock); | |
3626 | radix_tree_preload_end(); | |
3627 | if (ret == -EEXIST) { | |
3628 | exists = find_extent_buffer(fs_info, start); | |
3629 | if (exists) | |
452c75c3 | 3630 | goto free_eb; |
01cd3909 DS |
3631 | else |
3632 | goto again; | |
3633 | } | |
6af118ce | 3634 | /* add one reference for the tree */ |
0b32f4bb | 3635 | check_buffer_tree_ref(eb); |
34b41ace | 3636 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
eb14ab8e CM |
3637 | |
3638 | /* | |
b16d011e | 3639 | * Now it's safe to unlock the pages because any calls to |
f913cff3 | 3640 | * btree_release_folio will correctly detect that a page belongs to a |
b16d011e | 3641 | * live buffer and won't free them prematurely. |
eb14ab8e | 3642 | */ |
28187ae5 NB |
3643 | for (i = 0; i < num_pages; i++) |
3644 | unlock_page(eb->pages[i]); | |
d1310b2e CM |
3645 | return eb; |
3646 | ||
6af118ce | 3647 | free_eb: |
5ca64f45 | 3648 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
727011e0 CM |
3649 | for (i = 0; i < num_pages; i++) { |
3650 | if (eb->pages[i]) | |
3651 | unlock_page(eb->pages[i]); | |
3652 | } | |
eb14ab8e | 3653 | |
897ca6e9 | 3654 | btrfs_release_extent_buffer(eb); |
6af118ce | 3655 | return exists; |
d1310b2e | 3656 | } |
d1310b2e | 3657 | |
3083ee2e JB |
3658 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
3659 | { | |
3660 | struct extent_buffer *eb = | |
3661 | container_of(head, struct extent_buffer, rcu_head); | |
3662 | ||
3663 | __free_extent_buffer(eb); | |
3664 | } | |
3665 | ||
f7a52a40 | 3666 | static int release_extent_buffer(struct extent_buffer *eb) |
5ce48d0f | 3667 | __releases(&eb->refs_lock) |
3083ee2e | 3668 | { |
07e21c4d NB |
3669 | lockdep_assert_held(&eb->refs_lock); |
3670 | ||
3083ee2e JB |
3671 | WARN_ON(atomic_read(&eb->refs) == 0); |
3672 | if (atomic_dec_and_test(&eb->refs)) { | |
34b41ace | 3673 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
f28491e0 | 3674 | struct btrfs_fs_info *fs_info = eb->fs_info; |
3083ee2e | 3675 | |
815a51c7 | 3676 | spin_unlock(&eb->refs_lock); |
3083ee2e | 3677 | |
01cd3909 DS |
3678 | spin_lock(&fs_info->buffer_lock); |
3679 | radix_tree_delete(&fs_info->buffer_radix, | |
3680 | eb->start >> fs_info->sectorsize_bits); | |
3681 | spin_unlock(&fs_info->buffer_lock); | |
34b41ace JB |
3682 | } else { |
3683 | spin_unlock(&eb->refs_lock); | |
815a51c7 | 3684 | } |
3083ee2e | 3685 | |
a40246e8 | 3686 | btrfs_leak_debug_del_eb(eb); |
3083ee2e | 3687 | /* Should be safe to release our pages at this point */ |
55ac0139 | 3688 | btrfs_release_extent_buffer_pages(eb); |
bcb7e449 | 3689 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
b0132a3b | 3690 | if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) { |
bcb7e449 JB |
3691 | __free_extent_buffer(eb); |
3692 | return 1; | |
3693 | } | |
3694 | #endif | |
3083ee2e | 3695 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
e64860aa | 3696 | return 1; |
3083ee2e JB |
3697 | } |
3698 | spin_unlock(&eb->refs_lock); | |
e64860aa JB |
3699 | |
3700 | return 0; | |
3083ee2e JB |
3701 | } |
3702 | ||
d1310b2e CM |
3703 | void free_extent_buffer(struct extent_buffer *eb) |
3704 | { | |
242e18c7 | 3705 | int refs; |
d1310b2e CM |
3706 | if (!eb) |
3707 | return; | |
3708 | ||
e5677f05 | 3709 | refs = atomic_read(&eb->refs); |
242e18c7 | 3710 | while (1) { |
46cc775e NB |
3711 | if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3) |
3712 | || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && | |
3713 | refs == 1)) | |
242e18c7 | 3714 | break; |
e5677f05 | 3715 | if (atomic_try_cmpxchg(&eb->refs, &refs, refs - 1)) |
242e18c7 CM |
3716 | return; |
3717 | } | |
3718 | ||
3083ee2e JB |
3719 | spin_lock(&eb->refs_lock); |
3720 | if (atomic_read(&eb->refs) == 2 && | |
3721 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && | |
0b32f4bb | 3722 | !extent_buffer_under_io(eb) && |
3083ee2e JB |
3723 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
3724 | atomic_dec(&eb->refs); | |
3725 | ||
3726 | /* | |
3727 | * I know this is terrible, but it's temporary until we stop tracking | |
3728 | * the uptodate bits and such for the extent buffers. | |
3729 | */ | |
f7a52a40 | 3730 | release_extent_buffer(eb); |
3083ee2e JB |
3731 | } |
3732 | ||
3733 | void free_extent_buffer_stale(struct extent_buffer *eb) | |
3734 | { | |
3735 | if (!eb) | |
d1310b2e CM |
3736 | return; |
3737 | ||
3083ee2e JB |
3738 | spin_lock(&eb->refs_lock); |
3739 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); | |
3740 | ||
0b32f4bb | 3741 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
3083ee2e JB |
3742 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
3743 | atomic_dec(&eb->refs); | |
f7a52a40 | 3744 | release_extent_buffer(eb); |
d1310b2e | 3745 | } |
d1310b2e | 3746 | |
0d27797e QW |
3747 | static void btree_clear_page_dirty(struct page *page) |
3748 | { | |
3749 | ASSERT(PageDirty(page)); | |
3750 | ASSERT(PageLocked(page)); | |
3751 | clear_page_dirty_for_io(page); | |
3752 | xa_lock_irq(&page->mapping->i_pages); | |
3753 | if (!PageDirty(page)) | |
3754 | __xa_clear_mark(&page->mapping->i_pages, | |
3755 | page_index(page), PAGECACHE_TAG_DIRTY); | |
3756 | xa_unlock_irq(&page->mapping->i_pages); | |
3757 | } | |
3758 | ||
3759 | static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb) | |
3760 | { | |
3761 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
3762 | struct page *page = eb->pages[0]; | |
3763 | bool last; | |
3764 | ||
3765 | /* btree_clear_page_dirty() needs page locked */ | |
3766 | lock_page(page); | |
3767 | last = btrfs_subpage_clear_and_test_dirty(fs_info, page, eb->start, | |
3768 | eb->len); | |
3769 | if (last) | |
3770 | btree_clear_page_dirty(page); | |
3771 | unlock_page(page); | |
3772 | WARN_ON(atomic_read(&eb->refs) == 0); | |
3773 | } | |
3774 | ||
98c8d683 JB |
3775 | void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans, |
3776 | struct extent_buffer *eb) | |
d1310b2e | 3777 | { |
98c8d683 | 3778 | struct btrfs_fs_info *fs_info = eb->fs_info; |
cc5e31a4 DS |
3779 | int i; |
3780 | int num_pages; | |
d1310b2e CM |
3781 | struct page *page; |
3782 | ||
98c8d683 JB |
3783 | btrfs_assert_tree_write_locked(eb); |
3784 | ||
3785 | if (trans && btrfs_header_generation(eb) != trans->transid) | |
3786 | return; | |
3787 | ||
3788 | if (!test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) | |
3789 | return; | |
3790 | ||
3791 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, -eb->len, | |
3792 | fs_info->dirty_metadata_batch); | |
3793 | ||
fbca46eb | 3794 | if (eb->fs_info->nodesize < PAGE_SIZE) |
0d27797e QW |
3795 | return clear_subpage_extent_buffer_dirty(eb); |
3796 | ||
65ad0104 | 3797 | num_pages = num_extent_pages(eb); |
d1310b2e CM |
3798 | |
3799 | for (i = 0; i < num_pages; i++) { | |
fb85fc9a | 3800 | page = eb->pages[i]; |
b9473439 | 3801 | if (!PageDirty(page)) |
d2c3f4f6 | 3802 | continue; |
a61e6f29 | 3803 | lock_page(page); |
0d27797e | 3804 | btree_clear_page_dirty(page); |
a61e6f29 | 3805 | unlock_page(page); |
d1310b2e | 3806 | } |
0b32f4bb | 3807 | WARN_ON(atomic_read(&eb->refs) == 0); |
d1310b2e | 3808 | } |
d1310b2e | 3809 | |
f18cc978 | 3810 | void set_extent_buffer_dirty(struct extent_buffer *eb) |
d1310b2e | 3811 | { |
cc5e31a4 DS |
3812 | int i; |
3813 | int num_pages; | |
abb57ef3 | 3814 | bool was_dirty; |
d1310b2e | 3815 | |
0b32f4bb JB |
3816 | check_buffer_tree_ref(eb); |
3817 | ||
b9473439 | 3818 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
0b32f4bb | 3819 | |
65ad0104 | 3820 | num_pages = num_extent_pages(eb); |
3083ee2e | 3821 | WARN_ON(atomic_read(&eb->refs) == 0); |
0b32f4bb JB |
3822 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
3823 | ||
0d27797e | 3824 | if (!was_dirty) { |
fbca46eb | 3825 | bool subpage = eb->fs_info->nodesize < PAGE_SIZE; |
51995c39 | 3826 | |
0d27797e QW |
3827 | /* |
3828 | * For subpage case, we can have other extent buffers in the | |
3829 | * same page, and in clear_subpage_extent_buffer_dirty() we | |
3830 | * have to clear page dirty without subpage lock held. | |
3831 | * This can cause race where our page gets dirty cleared after | |
3832 | * we just set it. | |
3833 | * | |
3834 | * Thankfully, clear_subpage_extent_buffer_dirty() has locked | |
3835 | * its page for other reasons, we can use page lock to prevent | |
3836 | * the above race. | |
3837 | */ | |
3838 | if (subpage) | |
3839 | lock_page(eb->pages[0]); | |
3840 | for (i = 0; i < num_pages; i++) | |
3841 | btrfs_page_set_dirty(eb->fs_info, eb->pages[i], | |
3842 | eb->start, eb->len); | |
3843 | if (subpage) | |
3844 | unlock_page(eb->pages[0]); | |
f18cc978 CH |
3845 | percpu_counter_add_batch(&eb->fs_info->dirty_metadata_bytes, |
3846 | eb->len, | |
3847 | eb->fs_info->dirty_metadata_batch); | |
0d27797e | 3848 | } |
51995c39 LB |
3849 | #ifdef CONFIG_BTRFS_DEBUG |
3850 | for (i = 0; i < num_pages; i++) | |
3851 | ASSERT(PageDirty(eb->pages[i])); | |
3852 | #endif | |
d1310b2e | 3853 | } |
d1310b2e | 3854 | |
69ba3927 | 3855 | void clear_extent_buffer_uptodate(struct extent_buffer *eb) |
1259ab75 | 3856 | { |
251f2acc | 3857 | struct btrfs_fs_info *fs_info = eb->fs_info; |
1259ab75 | 3858 | struct page *page; |
cc5e31a4 | 3859 | int num_pages; |
251f2acc | 3860 | int i; |
1259ab75 | 3861 | |
b4ce94de | 3862 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
65ad0104 | 3863 | num_pages = num_extent_pages(eb); |
1259ab75 | 3864 | for (i = 0; i < num_pages; i++) { |
fb85fc9a | 3865 | page = eb->pages[i]; |
fbca46eb QW |
3866 | if (!page) |
3867 | continue; | |
3868 | ||
3869 | /* | |
3870 | * This is special handling for metadata subpage, as regular | |
3871 | * btrfs_is_subpage() can not handle cloned/dummy metadata. | |
3872 | */ | |
3873 | if (fs_info->nodesize >= PAGE_SIZE) | |
3874 | ClearPageUptodate(page); | |
3875 | else | |
3876 | btrfs_subpage_clear_uptodate(fs_info, page, eb->start, | |
3877 | eb->len); | |
1259ab75 | 3878 | } |
1259ab75 CM |
3879 | } |
3880 | ||
09c25a8c | 3881 | void set_extent_buffer_uptodate(struct extent_buffer *eb) |
d1310b2e | 3882 | { |
251f2acc | 3883 | struct btrfs_fs_info *fs_info = eb->fs_info; |
d1310b2e | 3884 | struct page *page; |
cc5e31a4 | 3885 | int num_pages; |
251f2acc | 3886 | int i; |
d1310b2e | 3887 | |
0b32f4bb | 3888 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
65ad0104 | 3889 | num_pages = num_extent_pages(eb); |
d1310b2e | 3890 | for (i = 0; i < num_pages; i++) { |
fb85fc9a | 3891 | page = eb->pages[i]; |
fbca46eb QW |
3892 | |
3893 | /* | |
3894 | * This is special handling for metadata subpage, as regular | |
3895 | * btrfs_is_subpage() can not handle cloned/dummy metadata. | |
3896 | */ | |
3897 | if (fs_info->nodesize >= PAGE_SIZE) | |
3898 | SetPageUptodate(page); | |
3899 | else | |
3900 | btrfs_subpage_set_uptodate(fs_info, page, eb->start, | |
3901 | eb->len); | |
d1310b2e | 3902 | } |
d1310b2e | 3903 | } |
d1310b2e | 3904 | |
046b562b CH |
3905 | static void extent_buffer_read_end_io(struct btrfs_bio *bbio) |
3906 | { | |
3907 | struct extent_buffer *eb = bbio->private; | |
d7172f52 | 3908 | struct btrfs_fs_info *fs_info = eb->fs_info; |
046b562b CH |
3909 | bool uptodate = !bbio->bio.bi_status; |
3910 | struct bvec_iter_all iter_all; | |
3911 | struct bio_vec *bvec; | |
3912 | u32 bio_offset = 0; | |
3913 | ||
046b562b CH |
3914 | eb->read_mirror = bbio->mirror_num; |
3915 | ||
3916 | if (uptodate && | |
3917 | btrfs_validate_extent_buffer(eb, &bbio->parent_check) < 0) | |
3918 | uptodate = false; | |
3919 | ||
3920 | if (uptodate) { | |
3921 | set_extent_buffer_uptodate(eb); | |
3922 | } else { | |
3923 | clear_extent_buffer_uptodate(eb); | |
3924 | set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); | |
3925 | } | |
3926 | ||
3927 | bio_for_each_segment_all(bvec, &bbio->bio, iter_all) { | |
d7172f52 CH |
3928 | u64 start = eb->start + bio_offset; |
3929 | struct page *page = bvec->bv_page; | |
3930 | u32 len = bvec->bv_len; | |
046b562b | 3931 | |
d7172f52 CH |
3932 | if (uptodate) |
3933 | btrfs_page_set_uptodate(fs_info, page, start, len); | |
3934 | else | |
3935 | btrfs_page_clear_uptodate(fs_info, page, start, len); | |
3936 | ||
3937 | bio_offset += len; | |
3d66b4b2 | 3938 | } |
d7172f52 CH |
3939 | |
3940 | clear_bit(EXTENT_BUFFER_READING, &eb->bflags); | |
3941 | smp_mb__after_atomic(); | |
3942 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_READING); | |
046b562b CH |
3943 | free_extent_buffer(eb); |
3944 | ||
3945 | bio_put(&bbio->bio); | |
3946 | } | |
3947 | ||
d7172f52 CH |
3948 | int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num, |
3949 | struct btrfs_tree_parent_check *check) | |
b78b98e0 CH |
3950 | { |
3951 | int num_pages = num_extent_pages(eb), i; | |
3952 | struct btrfs_bio *bbio; | |
3953 | ||
d7172f52 CH |
3954 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
3955 | return 0; | |
3956 | ||
3957 | /* | |
3958 | * We could have had EXTENT_BUFFER_UPTODATE cleared by the write | |
3959 | * operation, which could potentially still be in flight. In this case | |
3960 | * we simply want to return an error. | |
3961 | */ | |
3962 | if (unlikely(test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))) | |
3963 | return -EIO; | |
3964 | ||
3965 | /* Someone else is already reading the buffer, just wait for it. */ | |
3966 | if (test_and_set_bit(EXTENT_BUFFER_READING, &eb->bflags)) | |
3967 | goto done; | |
3968 | ||
b78b98e0 CH |
3969 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
3970 | eb->read_mirror = 0; | |
b78b98e0 | 3971 | check_buffer_tree_ref(eb); |
113fa05c | 3972 | atomic_inc(&eb->refs); |
b78b98e0 CH |
3973 | |
3974 | bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES, | |
3975 | REQ_OP_READ | REQ_META, eb->fs_info, | |
046b562b | 3976 | extent_buffer_read_end_io, eb); |
b78b98e0 CH |
3977 | bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT; |
3978 | bbio->inode = BTRFS_I(eb->fs_info->btree_inode); | |
3979 | bbio->file_offset = eb->start; | |
3980 | memcpy(&bbio->parent_check, check, sizeof(*check)); | |
3981 | if (eb->fs_info->nodesize < PAGE_SIZE) { | |
3982 | __bio_add_page(&bbio->bio, eb->pages[0], eb->len, | |
3983 | eb->start - page_offset(eb->pages[0])); | |
3984 | } else { | |
011134f4 | 3985 | for (i = 0; i < num_pages; i++) |
b78b98e0 | 3986 | __bio_add_page(&bbio->bio, eb->pages[i], PAGE_SIZE, 0); |
b78b98e0 CH |
3987 | } |
3988 | btrfs_submit_bio(bbio, mirror_num); | |
b78b98e0 | 3989 | |
d7172f52 CH |
3990 | done: |
3991 | if (wait == WAIT_COMPLETE) { | |
3992 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_READING, TASK_UNINTERRUPTIBLE); | |
3993 | if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) | |
55173337 | 3994 | return -EIO; |
d1310b2e | 3995 | } |
d397712b | 3996 | |
55173337 | 3997 | return 0; |
d1310b2e | 3998 | } |
d1310b2e | 3999 | |
f98b6215 QW |
4000 | static bool report_eb_range(const struct extent_buffer *eb, unsigned long start, |
4001 | unsigned long len) | |
4002 | { | |
4003 | btrfs_warn(eb->fs_info, | |
4004 | "access to eb bytenr %llu len %lu out of range start %lu len %lu", | |
4005 | eb->start, eb->len, start, len); | |
4006 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); | |
4007 | ||
4008 | return true; | |
4009 | } | |
4010 | ||
4011 | /* | |
4012 | * Check if the [start, start + len) range is valid before reading/writing | |
4013 | * the eb. | |
4014 | * NOTE: @start and @len are offset inside the eb, not logical address. | |
4015 | * | |
4016 | * Caller should not touch the dst/src memory if this function returns error. | |
4017 | */ | |
4018 | static inline int check_eb_range(const struct extent_buffer *eb, | |
4019 | unsigned long start, unsigned long len) | |
4020 | { | |
4021 | unsigned long offset; | |
4022 | ||
4023 | /* start, start + len should not go beyond eb->len nor overflow */ | |
4024 | if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len)) | |
4025 | return report_eb_range(eb, start, len); | |
4026 | ||
4027 | return false; | |
4028 | } | |
4029 | ||
1cbb1f45 JM |
4030 | void read_extent_buffer(const struct extent_buffer *eb, void *dstv, |
4031 | unsigned long start, unsigned long len) | |
d1310b2e CM |
4032 | { |
4033 | size_t cur; | |
4034 | size_t offset; | |
4035 | struct page *page; | |
4036 | char *kaddr; | |
4037 | char *dst = (char *)dstv; | |
884b07d0 | 4038 | unsigned long i = get_eb_page_index(start); |
d1310b2e | 4039 | |
f98b6215 | 4040 | if (check_eb_range(eb, start, len)) |
f716abd5 | 4041 | return; |
d1310b2e | 4042 | |
884b07d0 | 4043 | offset = get_eb_offset_in_page(eb, start); |
d1310b2e | 4044 | |
d397712b | 4045 | while (len > 0) { |
fb85fc9a | 4046 | page = eb->pages[i]; |
d1310b2e | 4047 | |
09cbfeaf | 4048 | cur = min(len, (PAGE_SIZE - offset)); |
a6591715 | 4049 | kaddr = page_address(page); |
d1310b2e | 4050 | memcpy(dst, kaddr + offset, cur); |
d1310b2e CM |
4051 | |
4052 | dst += cur; | |
4053 | len -= cur; | |
4054 | offset = 0; | |
4055 | i++; | |
4056 | } | |
4057 | } | |
d1310b2e | 4058 | |
a48b73ec JB |
4059 | int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, |
4060 | void __user *dstv, | |
4061 | unsigned long start, unsigned long len) | |
550ac1d8 GH |
4062 | { |
4063 | size_t cur; | |
4064 | size_t offset; | |
4065 | struct page *page; | |
4066 | char *kaddr; | |
4067 | char __user *dst = (char __user *)dstv; | |
884b07d0 | 4068 | unsigned long i = get_eb_page_index(start); |
550ac1d8 GH |
4069 | int ret = 0; |
4070 | ||
4071 | WARN_ON(start > eb->len); | |
4072 | WARN_ON(start + len > eb->start + eb->len); | |
4073 | ||
884b07d0 | 4074 | offset = get_eb_offset_in_page(eb, start); |
550ac1d8 GH |
4075 | |
4076 | while (len > 0) { | |
fb85fc9a | 4077 | page = eb->pages[i]; |
550ac1d8 | 4078 | |
09cbfeaf | 4079 | cur = min(len, (PAGE_SIZE - offset)); |
550ac1d8 | 4080 | kaddr = page_address(page); |
a48b73ec | 4081 | if (copy_to_user_nofault(dst, kaddr + offset, cur)) { |
550ac1d8 GH |
4082 | ret = -EFAULT; |
4083 | break; | |
4084 | } | |
4085 | ||
4086 | dst += cur; | |
4087 | len -= cur; | |
4088 | offset = 0; | |
4089 | i++; | |
4090 | } | |
4091 | ||
4092 | return ret; | |
4093 | } | |
4094 | ||
1cbb1f45 JM |
4095 | int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, |
4096 | unsigned long start, unsigned long len) | |
d1310b2e CM |
4097 | { |
4098 | size_t cur; | |
4099 | size_t offset; | |
4100 | struct page *page; | |
4101 | char *kaddr; | |
4102 | char *ptr = (char *)ptrv; | |
884b07d0 | 4103 | unsigned long i = get_eb_page_index(start); |
d1310b2e CM |
4104 | int ret = 0; |
4105 | ||
f98b6215 QW |
4106 | if (check_eb_range(eb, start, len)) |
4107 | return -EINVAL; | |
d1310b2e | 4108 | |
884b07d0 | 4109 | offset = get_eb_offset_in_page(eb, start); |
d1310b2e | 4110 | |
d397712b | 4111 | while (len > 0) { |
fb85fc9a | 4112 | page = eb->pages[i]; |
d1310b2e | 4113 | |
09cbfeaf | 4114 | cur = min(len, (PAGE_SIZE - offset)); |
d1310b2e | 4115 | |
a6591715 | 4116 | kaddr = page_address(page); |
d1310b2e | 4117 | ret = memcmp(ptr, kaddr + offset, cur); |
d1310b2e CM |
4118 | if (ret) |
4119 | break; | |
4120 | ||
4121 | ptr += cur; | |
4122 | len -= cur; | |
4123 | offset = 0; | |
4124 | i++; | |
4125 | } | |
4126 | return ret; | |
4127 | } | |
d1310b2e | 4128 | |
b8f95771 QW |
4129 | /* |
4130 | * Check that the extent buffer is uptodate. | |
4131 | * | |
4132 | * For regular sector size == PAGE_SIZE case, check if @page is uptodate. | |
4133 | * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE. | |
4134 | */ | |
4135 | static void assert_eb_page_uptodate(const struct extent_buffer *eb, | |
4136 | struct page *page) | |
4137 | { | |
4138 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
4139 | ||
a50e1fcb JB |
4140 | /* |
4141 | * If we are using the commit root we could potentially clear a page | |
4142 | * Uptodate while we're using the extent buffer that we've previously | |
4143 | * looked up. We don't want to complain in this case, as the page was | |
4144 | * valid before, we just didn't write it out. Instead we want to catch | |
4145 | * the case where we didn't actually read the block properly, which | |
011134f4 | 4146 | * would have !PageUptodate and !EXTENT_BUFFER_WRITE_ERR. |
a50e1fcb | 4147 | */ |
011134f4 CH |
4148 | if (test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
4149 | return; | |
b8f95771 | 4150 | |
011134f4 | 4151 | if (fs_info->nodesize < PAGE_SIZE) { |
75258f20 QW |
4152 | if (WARN_ON(!btrfs_subpage_test_uptodate(fs_info, page, |
4153 | eb->start, eb->len))) | |
4154 | btrfs_subpage_dump_bitmap(fs_info, page, eb->start, eb->len); | |
b8f95771 | 4155 | } else { |
011134f4 | 4156 | WARN_ON(!PageUptodate(page)); |
b8f95771 QW |
4157 | } |
4158 | } | |
4159 | ||
2b48966a | 4160 | void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb, |
f157bf76 DS |
4161 | const void *srcv) |
4162 | { | |
4163 | char *kaddr; | |
4164 | ||
b8f95771 | 4165 | assert_eb_page_uptodate(eb, eb->pages[0]); |
24880be5 DS |
4166 | kaddr = page_address(eb->pages[0]) + |
4167 | get_eb_offset_in_page(eb, offsetof(struct btrfs_header, | |
4168 | chunk_tree_uuid)); | |
4169 | memcpy(kaddr, srcv, BTRFS_FSID_SIZE); | |
f157bf76 DS |
4170 | } |
4171 | ||
2b48966a | 4172 | void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *srcv) |
f157bf76 DS |
4173 | { |
4174 | char *kaddr; | |
4175 | ||
b8f95771 | 4176 | assert_eb_page_uptodate(eb, eb->pages[0]); |
24880be5 DS |
4177 | kaddr = page_address(eb->pages[0]) + |
4178 | get_eb_offset_in_page(eb, offsetof(struct btrfs_header, fsid)); | |
4179 | memcpy(kaddr, srcv, BTRFS_FSID_SIZE); | |
f157bf76 DS |
4180 | } |
4181 | ||
2b48966a | 4182 | void write_extent_buffer(const struct extent_buffer *eb, const void *srcv, |
d1310b2e CM |
4183 | unsigned long start, unsigned long len) |
4184 | { | |
4185 | size_t cur; | |
4186 | size_t offset; | |
4187 | struct page *page; | |
4188 | char *kaddr; | |
4189 | char *src = (char *)srcv; | |
884b07d0 | 4190 | unsigned long i = get_eb_page_index(start); |
d1310b2e | 4191 | |
d3575156 NA |
4192 | WARN_ON(test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)); |
4193 | ||
f98b6215 QW |
4194 | if (check_eb_range(eb, start, len)) |
4195 | return; | |
d1310b2e | 4196 | |
884b07d0 | 4197 | offset = get_eb_offset_in_page(eb, start); |
d1310b2e | 4198 | |
d397712b | 4199 | while (len > 0) { |
fb85fc9a | 4200 | page = eb->pages[i]; |
b8f95771 | 4201 | assert_eb_page_uptodate(eb, page); |
d1310b2e | 4202 | |
09cbfeaf | 4203 | cur = min(len, PAGE_SIZE - offset); |
a6591715 | 4204 | kaddr = page_address(page); |
d1310b2e | 4205 | memcpy(kaddr + offset, src, cur); |
d1310b2e CM |
4206 | |
4207 | src += cur; | |
4208 | len -= cur; | |
4209 | offset = 0; | |
4210 | i++; | |
4211 | } | |
4212 | } | |
d1310b2e | 4213 | |
2b48966a | 4214 | void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start, |
b159fa28 | 4215 | unsigned long len) |
d1310b2e CM |
4216 | { |
4217 | size_t cur; | |
4218 | size_t offset; | |
4219 | struct page *page; | |
4220 | char *kaddr; | |
884b07d0 | 4221 | unsigned long i = get_eb_page_index(start); |
d1310b2e | 4222 | |
f98b6215 QW |
4223 | if (check_eb_range(eb, start, len)) |
4224 | return; | |
d1310b2e | 4225 | |
884b07d0 | 4226 | offset = get_eb_offset_in_page(eb, start); |
d1310b2e | 4227 | |
d397712b | 4228 | while (len > 0) { |
fb85fc9a | 4229 | page = eb->pages[i]; |
b8f95771 | 4230 | assert_eb_page_uptodate(eb, page); |
d1310b2e | 4231 | |
09cbfeaf | 4232 | cur = min(len, PAGE_SIZE - offset); |
a6591715 | 4233 | kaddr = page_address(page); |
b159fa28 | 4234 | memset(kaddr + offset, 0, cur); |
d1310b2e CM |
4235 | |
4236 | len -= cur; | |
4237 | offset = 0; | |
4238 | i++; | |
4239 | } | |
4240 | } | |
d1310b2e | 4241 | |
2b48966a DS |
4242 | void copy_extent_buffer_full(const struct extent_buffer *dst, |
4243 | const struct extent_buffer *src) | |
58e8012c DS |
4244 | { |
4245 | int i; | |
cc5e31a4 | 4246 | int num_pages; |
58e8012c DS |
4247 | |
4248 | ASSERT(dst->len == src->len); | |
4249 | ||
fbca46eb | 4250 | if (dst->fs_info->nodesize >= PAGE_SIZE) { |
884b07d0 QW |
4251 | num_pages = num_extent_pages(dst); |
4252 | for (i = 0; i < num_pages; i++) | |
4253 | copy_page(page_address(dst->pages[i]), | |
4254 | page_address(src->pages[i])); | |
4255 | } else { | |
4256 | size_t src_offset = get_eb_offset_in_page(src, 0); | |
4257 | size_t dst_offset = get_eb_offset_in_page(dst, 0); | |
4258 | ||
fbca46eb | 4259 | ASSERT(src->fs_info->nodesize < PAGE_SIZE); |
884b07d0 QW |
4260 | memcpy(page_address(dst->pages[0]) + dst_offset, |
4261 | page_address(src->pages[0]) + src_offset, | |
4262 | src->len); | |
4263 | } | |
58e8012c DS |
4264 | } |
4265 | ||
2b48966a DS |
4266 | void copy_extent_buffer(const struct extent_buffer *dst, |
4267 | const struct extent_buffer *src, | |
d1310b2e CM |
4268 | unsigned long dst_offset, unsigned long src_offset, |
4269 | unsigned long len) | |
4270 | { | |
4271 | u64 dst_len = dst->len; | |
4272 | size_t cur; | |
4273 | size_t offset; | |
4274 | struct page *page; | |
4275 | char *kaddr; | |
884b07d0 | 4276 | unsigned long i = get_eb_page_index(dst_offset); |
d1310b2e | 4277 | |
f98b6215 QW |
4278 | if (check_eb_range(dst, dst_offset, len) || |
4279 | check_eb_range(src, src_offset, len)) | |
4280 | return; | |
4281 | ||
d1310b2e CM |
4282 | WARN_ON(src->len != dst_len); |
4283 | ||
884b07d0 | 4284 | offset = get_eb_offset_in_page(dst, dst_offset); |
d1310b2e | 4285 | |
d397712b | 4286 | while (len > 0) { |
fb85fc9a | 4287 | page = dst->pages[i]; |
b8f95771 | 4288 | assert_eb_page_uptodate(dst, page); |
d1310b2e | 4289 | |
09cbfeaf | 4290 | cur = min(len, (unsigned long)(PAGE_SIZE - offset)); |
d1310b2e | 4291 | |
a6591715 | 4292 | kaddr = page_address(page); |
d1310b2e | 4293 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
d1310b2e CM |
4294 | |
4295 | src_offset += cur; | |
4296 | len -= cur; | |
4297 | offset = 0; | |
4298 | i++; | |
4299 | } | |
4300 | } | |
d1310b2e | 4301 | |
3e1e8bb7 OS |
4302 | /* |
4303 | * eb_bitmap_offset() - calculate the page and offset of the byte containing the | |
4304 | * given bit number | |
4305 | * @eb: the extent buffer | |
4306 | * @start: offset of the bitmap item in the extent buffer | |
4307 | * @nr: bit number | |
4308 | * @page_index: return index of the page in the extent buffer that contains the | |
4309 | * given bit number | |
4310 | * @page_offset: return offset into the page given by page_index | |
4311 | * | |
4312 | * This helper hides the ugliness of finding the byte in an extent buffer which | |
4313 | * contains a given bit. | |
4314 | */ | |
2b48966a | 4315 | static inline void eb_bitmap_offset(const struct extent_buffer *eb, |
3e1e8bb7 OS |
4316 | unsigned long start, unsigned long nr, |
4317 | unsigned long *page_index, | |
4318 | size_t *page_offset) | |
4319 | { | |
3e1e8bb7 OS |
4320 | size_t byte_offset = BIT_BYTE(nr); |
4321 | size_t offset; | |
4322 | ||
4323 | /* | |
4324 | * The byte we want is the offset of the extent buffer + the offset of | |
4325 | * the bitmap item in the extent buffer + the offset of the byte in the | |
4326 | * bitmap item. | |
4327 | */ | |
884b07d0 | 4328 | offset = start + offset_in_page(eb->start) + byte_offset; |
3e1e8bb7 | 4329 | |
09cbfeaf | 4330 | *page_index = offset >> PAGE_SHIFT; |
7073017a | 4331 | *page_offset = offset_in_page(offset); |
3e1e8bb7 OS |
4332 | } |
4333 | ||
43dd529a DS |
4334 | /* |
4335 | * Determine whether a bit in a bitmap item is set. | |
4336 | * | |
4337 | * @eb: the extent buffer | |
4338 | * @start: offset of the bitmap item in the extent buffer | |
4339 | * @nr: bit number to test | |
3e1e8bb7 | 4340 | */ |
2b48966a | 4341 | int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start, |
3e1e8bb7 OS |
4342 | unsigned long nr) |
4343 | { | |
2fe1d551 | 4344 | u8 *kaddr; |
3e1e8bb7 OS |
4345 | struct page *page; |
4346 | unsigned long i; | |
4347 | size_t offset; | |
4348 | ||
4349 | eb_bitmap_offset(eb, start, nr, &i, &offset); | |
4350 | page = eb->pages[i]; | |
b8f95771 | 4351 | assert_eb_page_uptodate(eb, page); |
3e1e8bb7 OS |
4352 | kaddr = page_address(page); |
4353 | return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); | |
4354 | } | |
4355 | ||
43dd529a DS |
4356 | /* |
4357 | * Set an area of a bitmap to 1. | |
4358 | * | |
4359 | * @eb: the extent buffer | |
4360 | * @start: offset of the bitmap item in the extent buffer | |
4361 | * @pos: bit number of the first bit | |
4362 | * @len: number of bits to set | |
3e1e8bb7 | 4363 | */ |
2b48966a | 4364 | void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start, |
3e1e8bb7 OS |
4365 | unsigned long pos, unsigned long len) |
4366 | { | |
2fe1d551 | 4367 | u8 *kaddr; |
3e1e8bb7 OS |
4368 | struct page *page; |
4369 | unsigned long i; | |
4370 | size_t offset; | |
4371 | const unsigned int size = pos + len; | |
4372 | int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE); | |
2fe1d551 | 4373 | u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos); |
3e1e8bb7 OS |
4374 | |
4375 | eb_bitmap_offset(eb, start, pos, &i, &offset); | |
4376 | page = eb->pages[i]; | |
b8f95771 | 4377 | assert_eb_page_uptodate(eb, page); |
3e1e8bb7 OS |
4378 | kaddr = page_address(page); |
4379 | ||
4380 | while (len >= bits_to_set) { | |
4381 | kaddr[offset] |= mask_to_set; | |
4382 | len -= bits_to_set; | |
4383 | bits_to_set = BITS_PER_BYTE; | |
9c894696 | 4384 | mask_to_set = ~0; |
09cbfeaf | 4385 | if (++offset >= PAGE_SIZE && len > 0) { |
3e1e8bb7 OS |
4386 | offset = 0; |
4387 | page = eb->pages[++i]; | |
b8f95771 | 4388 | assert_eb_page_uptodate(eb, page); |
3e1e8bb7 OS |
4389 | kaddr = page_address(page); |
4390 | } | |
4391 | } | |
4392 | if (len) { | |
4393 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); | |
4394 | kaddr[offset] |= mask_to_set; | |
4395 | } | |
4396 | } | |
4397 | ||
4398 | ||
43dd529a DS |
4399 | /* |
4400 | * Clear an area of a bitmap. | |
4401 | * | |
4402 | * @eb: the extent buffer | |
4403 | * @start: offset of the bitmap item in the extent buffer | |
4404 | * @pos: bit number of the first bit | |
4405 | * @len: number of bits to clear | |
3e1e8bb7 | 4406 | */ |
2b48966a DS |
4407 | void extent_buffer_bitmap_clear(const struct extent_buffer *eb, |
4408 | unsigned long start, unsigned long pos, | |
4409 | unsigned long len) | |
3e1e8bb7 | 4410 | { |
2fe1d551 | 4411 | u8 *kaddr; |
3e1e8bb7 OS |
4412 | struct page *page; |
4413 | unsigned long i; | |
4414 | size_t offset; | |
4415 | const unsigned int size = pos + len; | |
4416 | int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE); | |
2fe1d551 | 4417 | u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos); |
3e1e8bb7 OS |
4418 | |
4419 | eb_bitmap_offset(eb, start, pos, &i, &offset); | |
4420 | page = eb->pages[i]; | |
b8f95771 | 4421 | assert_eb_page_uptodate(eb, page); |
3e1e8bb7 OS |
4422 | kaddr = page_address(page); |
4423 | ||
4424 | while (len >= bits_to_clear) { | |
4425 | kaddr[offset] &= ~mask_to_clear; | |
4426 | len -= bits_to_clear; | |
4427 | bits_to_clear = BITS_PER_BYTE; | |
9c894696 | 4428 | mask_to_clear = ~0; |
09cbfeaf | 4429 | if (++offset >= PAGE_SIZE && len > 0) { |
3e1e8bb7 OS |
4430 | offset = 0; |
4431 | page = eb->pages[++i]; | |
b8f95771 | 4432 | assert_eb_page_uptodate(eb, page); |
3e1e8bb7 OS |
4433 | kaddr = page_address(page); |
4434 | } | |
4435 | } | |
4436 | if (len) { | |
4437 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); | |
4438 | kaddr[offset] &= ~mask_to_clear; | |
4439 | } | |
4440 | } | |
4441 | ||
3387206f ST |
4442 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
4443 | { | |
4444 | unsigned long distance = (src > dst) ? src - dst : dst - src; | |
4445 | return distance < len; | |
4446 | } | |
4447 | ||
d1310b2e CM |
4448 | static void copy_pages(struct page *dst_page, struct page *src_page, |
4449 | unsigned long dst_off, unsigned long src_off, | |
4450 | unsigned long len) | |
4451 | { | |
a6591715 | 4452 | char *dst_kaddr = page_address(dst_page); |
d1310b2e | 4453 | char *src_kaddr; |
727011e0 | 4454 | int must_memmove = 0; |
d1310b2e | 4455 | |
3387206f | 4456 | if (dst_page != src_page) { |
a6591715 | 4457 | src_kaddr = page_address(src_page); |
3387206f | 4458 | } else { |
d1310b2e | 4459 | src_kaddr = dst_kaddr; |
727011e0 CM |
4460 | if (areas_overlap(src_off, dst_off, len)) |
4461 | must_memmove = 1; | |
3387206f | 4462 | } |
d1310b2e | 4463 | |
727011e0 CM |
4464 | if (must_memmove) |
4465 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); | |
4466 | else | |
4467 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); | |
d1310b2e CM |
4468 | } |
4469 | ||
2b48966a DS |
4470 | void memcpy_extent_buffer(const struct extent_buffer *dst, |
4471 | unsigned long dst_offset, unsigned long src_offset, | |
4472 | unsigned long len) | |
d1310b2e CM |
4473 | { |
4474 | size_t cur; | |
4475 | size_t dst_off_in_page; | |
4476 | size_t src_off_in_page; | |
d1310b2e CM |
4477 | unsigned long dst_i; |
4478 | unsigned long src_i; | |
4479 | ||
f98b6215 QW |
4480 | if (check_eb_range(dst, dst_offset, len) || |
4481 | check_eb_range(dst, src_offset, len)) | |
4482 | return; | |
d1310b2e | 4483 | |
d397712b | 4484 | while (len > 0) { |
884b07d0 QW |
4485 | dst_off_in_page = get_eb_offset_in_page(dst, dst_offset); |
4486 | src_off_in_page = get_eb_offset_in_page(dst, src_offset); | |
d1310b2e | 4487 | |
884b07d0 QW |
4488 | dst_i = get_eb_page_index(dst_offset); |
4489 | src_i = get_eb_page_index(src_offset); | |
d1310b2e | 4490 | |
09cbfeaf | 4491 | cur = min(len, (unsigned long)(PAGE_SIZE - |
d1310b2e CM |
4492 | src_off_in_page)); |
4493 | cur = min_t(unsigned long, cur, | |
09cbfeaf | 4494 | (unsigned long)(PAGE_SIZE - dst_off_in_page)); |
d1310b2e | 4495 | |
fb85fc9a | 4496 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
d1310b2e CM |
4497 | dst_off_in_page, src_off_in_page, cur); |
4498 | ||
4499 | src_offset += cur; | |
4500 | dst_offset += cur; | |
4501 | len -= cur; | |
4502 | } | |
4503 | } | |
d1310b2e | 4504 | |
2b48966a DS |
4505 | void memmove_extent_buffer(const struct extent_buffer *dst, |
4506 | unsigned long dst_offset, unsigned long src_offset, | |
4507 | unsigned long len) | |
d1310b2e CM |
4508 | { |
4509 | size_t cur; | |
4510 | size_t dst_off_in_page; | |
4511 | size_t src_off_in_page; | |
4512 | unsigned long dst_end = dst_offset + len - 1; | |
4513 | unsigned long src_end = src_offset + len - 1; | |
d1310b2e CM |
4514 | unsigned long dst_i; |
4515 | unsigned long src_i; | |
4516 | ||
f98b6215 QW |
4517 | if (check_eb_range(dst, dst_offset, len) || |
4518 | check_eb_range(dst, src_offset, len)) | |
4519 | return; | |
727011e0 | 4520 | if (dst_offset < src_offset) { |
d1310b2e CM |
4521 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
4522 | return; | |
4523 | } | |
d397712b | 4524 | while (len > 0) { |
884b07d0 QW |
4525 | dst_i = get_eb_page_index(dst_end); |
4526 | src_i = get_eb_page_index(src_end); | |
d1310b2e | 4527 | |
884b07d0 QW |
4528 | dst_off_in_page = get_eb_offset_in_page(dst, dst_end); |
4529 | src_off_in_page = get_eb_offset_in_page(dst, src_end); | |
d1310b2e CM |
4530 | |
4531 | cur = min_t(unsigned long, len, src_off_in_page + 1); | |
4532 | cur = min(cur, dst_off_in_page + 1); | |
fb85fc9a | 4533 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
d1310b2e CM |
4534 | dst_off_in_page - cur + 1, |
4535 | src_off_in_page - cur + 1, cur); | |
4536 | ||
4537 | dst_end -= cur; | |
4538 | src_end -= cur; | |
4539 | len -= cur; | |
4540 | } | |
4541 | } | |
6af118ce | 4542 | |
01cd3909 | 4543 | #define GANG_LOOKUP_SIZE 16 |
d1e86e3f QW |
4544 | static struct extent_buffer *get_next_extent_buffer( |
4545 | struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr) | |
4546 | { | |
01cd3909 DS |
4547 | struct extent_buffer *gang[GANG_LOOKUP_SIZE]; |
4548 | struct extent_buffer *found = NULL; | |
d1e86e3f | 4549 | u64 page_start = page_offset(page); |
01cd3909 | 4550 | u64 cur = page_start; |
d1e86e3f QW |
4551 | |
4552 | ASSERT(in_range(bytenr, page_start, PAGE_SIZE)); | |
d1e86e3f QW |
4553 | lockdep_assert_held(&fs_info->buffer_lock); |
4554 | ||
01cd3909 DS |
4555 | while (cur < page_start + PAGE_SIZE) { |
4556 | int ret; | |
4557 | int i; | |
4558 | ||
4559 | ret = radix_tree_gang_lookup(&fs_info->buffer_radix, | |
4560 | (void **)gang, cur >> fs_info->sectorsize_bits, | |
4561 | min_t(unsigned int, GANG_LOOKUP_SIZE, | |
4562 | PAGE_SIZE / fs_info->nodesize)); | |
4563 | if (ret == 0) | |
4564 | goto out; | |
4565 | for (i = 0; i < ret; i++) { | |
4566 | /* Already beyond page end */ | |
4567 | if (gang[i]->start >= page_start + PAGE_SIZE) | |
4568 | goto out; | |
4569 | /* Found one */ | |
4570 | if (gang[i]->start >= bytenr) { | |
4571 | found = gang[i]; | |
4572 | goto out; | |
4573 | } | |
4574 | } | |
4575 | cur = gang[ret - 1]->start + gang[ret - 1]->len; | |
d1e86e3f | 4576 | } |
01cd3909 DS |
4577 | out: |
4578 | return found; | |
d1e86e3f QW |
4579 | } |
4580 | ||
4581 | static int try_release_subpage_extent_buffer(struct page *page) | |
4582 | { | |
4583 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); | |
4584 | u64 cur = page_offset(page); | |
4585 | const u64 end = page_offset(page) + PAGE_SIZE; | |
4586 | int ret; | |
4587 | ||
4588 | while (cur < end) { | |
4589 | struct extent_buffer *eb = NULL; | |
4590 | ||
4591 | /* | |
4592 | * Unlike try_release_extent_buffer() which uses page->private | |
4593 | * to grab buffer, for subpage case we rely on radix tree, thus | |
4594 | * we need to ensure radix tree consistency. | |
4595 | * | |
4596 | * We also want an atomic snapshot of the radix tree, thus go | |
4597 | * with spinlock rather than RCU. | |
4598 | */ | |
4599 | spin_lock(&fs_info->buffer_lock); | |
4600 | eb = get_next_extent_buffer(fs_info, page, cur); | |
4601 | if (!eb) { | |
4602 | /* No more eb in the page range after or at cur */ | |
4603 | spin_unlock(&fs_info->buffer_lock); | |
4604 | break; | |
4605 | } | |
4606 | cur = eb->start + eb->len; | |
4607 | ||
4608 | /* | |
4609 | * The same as try_release_extent_buffer(), to ensure the eb | |
4610 | * won't disappear out from under us. | |
4611 | */ | |
4612 | spin_lock(&eb->refs_lock); | |
4613 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { | |
4614 | spin_unlock(&eb->refs_lock); | |
4615 | spin_unlock(&fs_info->buffer_lock); | |
4616 | break; | |
4617 | } | |
4618 | spin_unlock(&fs_info->buffer_lock); | |
4619 | ||
4620 | /* | |
4621 | * If tree ref isn't set then we know the ref on this eb is a | |
4622 | * real ref, so just return, this eb will likely be freed soon | |
4623 | * anyway. | |
4624 | */ | |
4625 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { | |
4626 | spin_unlock(&eb->refs_lock); | |
4627 | break; | |
4628 | } | |
4629 | ||
4630 | /* | |
4631 | * Here we don't care about the return value, we will always | |
4632 | * check the page private at the end. And | |
4633 | * release_extent_buffer() will release the refs_lock. | |
4634 | */ | |
4635 | release_extent_buffer(eb); | |
4636 | } | |
4637 | /* | |
4638 | * Finally to check if we have cleared page private, as if we have | |
4639 | * released all ebs in the page, the page private should be cleared now. | |
4640 | */ | |
4641 | spin_lock(&page->mapping->private_lock); | |
4642 | if (!PagePrivate(page)) | |
4643 | ret = 1; | |
4644 | else | |
4645 | ret = 0; | |
4646 | spin_unlock(&page->mapping->private_lock); | |
4647 | return ret; | |
4648 | ||
4649 | } | |
4650 | ||
f7a52a40 | 4651 | int try_release_extent_buffer(struct page *page) |
19fe0a8b | 4652 | { |
6af118ce | 4653 | struct extent_buffer *eb; |
6af118ce | 4654 | |
fbca46eb | 4655 | if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) |
d1e86e3f QW |
4656 | return try_release_subpage_extent_buffer(page); |
4657 | ||
3083ee2e | 4658 | /* |
d1e86e3f QW |
4659 | * We need to make sure nobody is changing page->private, as we rely on |
4660 | * page->private as the pointer to extent buffer. | |
3083ee2e JB |
4661 | */ |
4662 | spin_lock(&page->mapping->private_lock); | |
4663 | if (!PagePrivate(page)) { | |
4664 | spin_unlock(&page->mapping->private_lock); | |
4f2de97a | 4665 | return 1; |
45f49bce | 4666 | } |
6af118ce | 4667 | |
3083ee2e JB |
4668 | eb = (struct extent_buffer *)page->private; |
4669 | BUG_ON(!eb); | |
19fe0a8b MX |
4670 | |
4671 | /* | |
3083ee2e JB |
4672 | * This is a little awful but should be ok, we need to make sure that |
4673 | * the eb doesn't disappear out from under us while we're looking at | |
4674 | * this page. | |
19fe0a8b | 4675 | */ |
3083ee2e | 4676 | spin_lock(&eb->refs_lock); |
0b32f4bb | 4677 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
3083ee2e JB |
4678 | spin_unlock(&eb->refs_lock); |
4679 | spin_unlock(&page->mapping->private_lock); | |
4680 | return 0; | |
b9473439 | 4681 | } |
3083ee2e | 4682 | spin_unlock(&page->mapping->private_lock); |
897ca6e9 | 4683 | |
19fe0a8b | 4684 | /* |
3083ee2e JB |
4685 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
4686 | * so just return, this page will likely be freed soon anyway. | |
19fe0a8b | 4687 | */ |
3083ee2e JB |
4688 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
4689 | spin_unlock(&eb->refs_lock); | |
4690 | return 0; | |
b9473439 | 4691 | } |
19fe0a8b | 4692 | |
f7a52a40 | 4693 | return release_extent_buffer(eb); |
6af118ce | 4694 | } |
bfb484d9 JB |
4695 | |
4696 | /* | |
4697 | * btrfs_readahead_tree_block - attempt to readahead a child block | |
4698 | * @fs_info: the fs_info | |
4699 | * @bytenr: bytenr to read | |
3fbaf258 | 4700 | * @owner_root: objectid of the root that owns this eb |
bfb484d9 | 4701 | * @gen: generation for the uptodate check, can be 0 |
3fbaf258 | 4702 | * @level: level for the eb |
bfb484d9 JB |
4703 | * |
4704 | * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a | |
4705 | * normal uptodate check of the eb, without checking the generation. If we have | |
4706 | * to read the block we will not block on anything. | |
4707 | */ | |
4708 | void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info, | |
3fbaf258 | 4709 | u64 bytenr, u64 owner_root, u64 gen, int level) |
bfb484d9 | 4710 | { |
947a6299 QW |
4711 | struct btrfs_tree_parent_check check = { |
4712 | .has_first_key = 0, | |
4713 | .level = level, | |
4714 | .transid = gen | |
4715 | }; | |
bfb484d9 JB |
4716 | struct extent_buffer *eb; |
4717 | int ret; | |
4718 | ||
3fbaf258 | 4719 | eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); |
bfb484d9 JB |
4720 | if (IS_ERR(eb)) |
4721 | return; | |
4722 | ||
4723 | if (btrfs_buffer_uptodate(eb, gen, 1)) { | |
4724 | free_extent_buffer(eb); | |
4725 | return; | |
4726 | } | |
4727 | ||
947a6299 | 4728 | ret = read_extent_buffer_pages(eb, WAIT_NONE, 0, &check); |
bfb484d9 JB |
4729 | if (ret < 0) |
4730 | free_extent_buffer_stale(eb); | |
4731 | else | |
4732 | free_extent_buffer(eb); | |
4733 | } | |
4734 | ||
4735 | /* | |
4736 | * btrfs_readahead_node_child - readahead a node's child block | |
4737 | * @node: parent node we're reading from | |
4738 | * @slot: slot in the parent node for the child we want to read | |
4739 | * | |
4740 | * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at | |
4741 | * the slot in the node provided. | |
4742 | */ | |
4743 | void btrfs_readahead_node_child(struct extent_buffer *node, int slot) | |
4744 | { | |
4745 | btrfs_readahead_tree_block(node->fs_info, | |
4746 | btrfs_node_blockptr(node, slot), | |
3fbaf258 JB |
4747 | btrfs_header_owner(node), |
4748 | btrfs_node_ptr_generation(node, slot), | |
4749 | btrfs_header_level(node) - 1); | |
bfb484d9 | 4750 | } |