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