Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
c1d7c514 | 2 | |
d1310b2e CM |
3 | #include <linux/bitops.h> |
4 | #include <linux/slab.h> | |
5 | #include <linux/bio.h> | |
6 | #include <linux/mm.h> | |
d1310b2e CM |
7 | #include <linux/pagemap.h> |
8 | #include <linux/page-flags.h> | |
395cb57e | 9 | #include <linux/sched/mm.h> |
d1310b2e CM |
10 | #include <linux/spinlock.h> |
11 | #include <linux/blkdev.h> | |
12 | #include <linux/swap.h> | |
d1310b2e CM |
13 | #include <linux/writeback.h> |
14 | #include <linux/pagevec.h> | |
268bb0ce | 15 | #include <linux/prefetch.h> |
14605409 | 16 | #include <linux/fsverity.h> |
cea62800 | 17 | #include "misc.h" |
d1310b2e | 18 | #include "extent_io.h" |
9c7d3a54 | 19 | #include "extent-io-tree.h" |
d1310b2e | 20 | #include "extent_map.h" |
902b22f3 DW |
21 | #include "ctree.h" |
22 | #include "btrfs_inode.h" | |
4a54c8c1 | 23 | #include "volumes.h" |
21adbd5c | 24 | #include "check-integrity.h" |
0b32f4bb | 25 | #include "locking.h" |
606686ee | 26 | #include "rcu-string.h" |
fe09e16c | 27 | #include "backref.h" |
6af49dbd | 28 | #include "disk-io.h" |
760f991f | 29 | #include "subpage.h" |
d3575156 | 30 | #include "zoned.h" |
0bc09ca1 | 31 | #include "block-group.h" |
2a5232a8 | 32 | #include "compression.h" |
d1310b2e | 33 | |
d1310b2e CM |
34 | static struct kmem_cache *extent_buffer_cache; |
35 | ||
6d49ba1b | 36 | #ifdef CONFIG_BTRFS_DEBUG |
a40246e8 JB |
37 | static inline void btrfs_leak_debug_add_eb(struct extent_buffer *eb) |
38 | { | |
39 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
40 | unsigned long flags; | |
41 | ||
42 | spin_lock_irqsave(&fs_info->eb_leak_lock, flags); | |
43 | list_add(&eb->leak_list, &fs_info->allocated_ebs); | |
44 | spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); | |
45 | } | |
46 | ||
a40246e8 JB |
47 | static inline void btrfs_leak_debug_del_eb(struct extent_buffer *eb) |
48 | { | |
49 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
50 | unsigned long flags; | |
51 | ||
52 | spin_lock_irqsave(&fs_info->eb_leak_lock, flags); | |
53 | list_del(&eb->leak_list); | |
54 | spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); | |
6d49ba1b ES |
55 | } |
56 | ||
3fd63727 | 57 | void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info) |
6d49ba1b | 58 | { |
6d49ba1b | 59 | struct extent_buffer *eb; |
3fd63727 | 60 | unsigned long flags; |
6d49ba1b | 61 | |
8c38938c JB |
62 | /* |
63 | * If we didn't get into open_ctree our allocated_ebs will not be | |
64 | * initialized, so just skip this. | |
65 | */ | |
66 | if (!fs_info->allocated_ebs.next) | |
67 | return; | |
68 | ||
b95b78e6 | 69 | WARN_ON(!list_empty(&fs_info->allocated_ebs)); |
3fd63727 JB |
70 | spin_lock_irqsave(&fs_info->eb_leak_lock, flags); |
71 | while (!list_empty(&fs_info->allocated_ebs)) { | |
72 | eb = list_first_entry(&fs_info->allocated_ebs, | |
73 | struct extent_buffer, leak_list); | |
8c38938c JB |
74 | pr_err( |
75 | "BTRFS: buffer leak start %llu len %lu refs %d bflags %lu owner %llu\n", | |
76 | eb->start, eb->len, atomic_read(&eb->refs), eb->bflags, | |
77 | btrfs_header_owner(eb)); | |
33ca832f JB |
78 | list_del(&eb->leak_list); |
79 | kmem_cache_free(extent_buffer_cache, eb); | |
80 | } | |
3fd63727 | 81 | spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); |
33ca832f | 82 | } |
6d49ba1b | 83 | #else |
a40246e8 | 84 | #define btrfs_leak_debug_add_eb(eb) do {} while (0) |
a40246e8 | 85 | #define btrfs_leak_debug_del_eb(eb) do {} while (0) |
4bef0848 | 86 | #endif |
d1310b2e | 87 | |
7aab8b32 CH |
88 | /* |
89 | * Structure to record info about the bio being assembled, and other info like | |
90 | * how many bytes are there before stripe/ordered extent boundary. | |
91 | */ | |
92 | struct btrfs_bio_ctrl { | |
93 | struct bio *bio; | |
722c82ac | 94 | int mirror_num; |
0f07003b | 95 | enum btrfs_compression_type compress_type; |
7aab8b32 CH |
96 | u32 len_to_stripe_boundary; |
97 | u32 len_to_oe_boundary; | |
5467abba | 98 | btrfs_bio_end_io_t end_io_func; |
7aab8b32 CH |
99 | }; |
100 | ||
d1310b2e | 101 | struct extent_page_data { |
390ed29b | 102 | struct btrfs_bio_ctrl bio_ctrl; |
771ed689 CM |
103 | /* tells writepage not to lock the state bits for this range |
104 | * it still does the unlocking | |
105 | */ | |
ffbd517d CM |
106 | unsigned int extent_locked:1; |
107 | ||
70fd7614 | 108 | /* tells the submit_bio code to use REQ_SYNC */ |
ffbd517d | 109 | unsigned int sync_io:1; |
d1310b2e CM |
110 | }; |
111 | ||
722c82ac | 112 | static void submit_one_bio(struct btrfs_bio_ctrl *bio_ctrl) |
bb58eb9e | 113 | { |
722c82ac | 114 | struct bio *bio; |
7aa51232 | 115 | struct bio_vec *bv; |
722c82ac CH |
116 | struct inode *inode; |
117 | int mirror_num; | |
118 | ||
119 | if (!bio_ctrl->bio) | |
120 | return; | |
bb58eb9e | 121 | |
722c82ac | 122 | bio = bio_ctrl->bio; |
7aa51232 CH |
123 | bv = bio_first_bvec_all(bio); |
124 | inode = bv->bv_page->mapping->host; | |
722c82ac | 125 | mirror_num = bio_ctrl->mirror_num; |
bb58eb9e | 126 | |
e0eefe07 QW |
127 | /* Caller should ensure the bio has at least some range added */ |
128 | ASSERT(bio->bi_iter.bi_size); | |
c9583ada | 129 | |
7aa51232 | 130 | btrfs_bio(bio)->file_offset = page_offset(bv->bv_page) + bv->bv_offset; |
bb58eb9e | 131 | |
c93104e7 CH |
132 | if (!is_data_inode(inode)) |
133 | btrfs_submit_metadata_bio(inode, bio, mirror_num); | |
134 | else if (btrfs_op(bio) == BTRFS_MAP_WRITE) | |
135 | btrfs_submit_data_write_bio(inode, bio, mirror_num); | |
908930f3 | 136 | else |
722c82ac CH |
137 | btrfs_submit_data_read_bio(inode, bio, mirror_num, |
138 | bio_ctrl->compress_type); | |
390ed29b | 139 | |
917f32a2 | 140 | /* The bio is owned by the end_io handler now */ |
722c82ac | 141 | bio_ctrl->bio = NULL; |
3065976b QW |
142 | } |
143 | ||
f4340622 | 144 | /* |
9845e5dd | 145 | * Submit or fail the current bio in an extent_page_data structure. |
f4340622 | 146 | */ |
9845e5dd | 147 | static void submit_write_bio(struct extent_page_data *epd, int ret) |
bb58eb9e | 148 | { |
390ed29b | 149 | struct bio *bio = epd->bio_ctrl.bio; |
bb58eb9e | 150 | |
9845e5dd CH |
151 | if (!bio) |
152 | return; | |
153 | ||
154 | if (ret) { | |
155 | ASSERT(ret < 0); | |
917f32a2 CH |
156 | btrfs_bio_end_io(btrfs_bio(bio), errno_to_blk_status(ret)); |
157 | /* The bio is owned by the end_io handler now */ | |
390ed29b | 158 | epd->bio_ctrl.bio = NULL; |
9845e5dd | 159 | } else { |
722c82ac | 160 | submit_one_bio(&epd->bio_ctrl); |
bb58eb9e QW |
161 | } |
162 | } | |
e2932ee0 | 163 | |
a62a3bd9 JB |
164 | int __init extent_buffer_init_cachep(void) |
165 | { | |
837e1972 | 166 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
9601e3f6 | 167 | sizeof(struct extent_buffer), 0, |
fba4b697 | 168 | SLAB_MEM_SPREAD, NULL); |
a62a3bd9 | 169 | if (!extent_buffer_cache) |
6f0d04f8 | 170 | return -ENOMEM; |
b208c2f7 | 171 | |
d1310b2e | 172 | return 0; |
d1310b2e CM |
173 | } |
174 | ||
a62a3bd9 | 175 | void __cold extent_buffer_free_cachep(void) |
d1310b2e | 176 | { |
8c0a8537 KS |
177 | /* |
178 | * Make sure all delayed rcu free are flushed before we | |
179 | * destroy caches. | |
180 | */ | |
181 | rcu_barrier(); | |
5598e900 | 182 | kmem_cache_destroy(extent_buffer_cache); |
d1310b2e CM |
183 | } |
184 | ||
bd1fa4f0 | 185 | void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) |
4adaa611 | 186 | { |
09cbfeaf KS |
187 | unsigned long index = start >> PAGE_SHIFT; |
188 | unsigned long end_index = end >> PAGE_SHIFT; | |
4adaa611 CM |
189 | struct page *page; |
190 | ||
191 | while (index <= end_index) { | |
192 | page = find_get_page(inode->i_mapping, index); | |
193 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ | |
194 | clear_page_dirty_for_io(page); | |
09cbfeaf | 195 | put_page(page); |
4adaa611 CM |
196 | index++; |
197 | } | |
4adaa611 CM |
198 | } |
199 | ||
f6311572 | 200 | void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end) |
4adaa611 | 201 | { |
ebf55c88 | 202 | struct address_space *mapping = inode->i_mapping; |
09cbfeaf KS |
203 | unsigned long index = start >> PAGE_SHIFT; |
204 | unsigned long end_index = end >> PAGE_SHIFT; | |
ebf55c88 | 205 | struct folio *folio; |
4adaa611 CM |
206 | |
207 | while (index <= end_index) { | |
ebf55c88 MWO |
208 | folio = filemap_get_folio(mapping, index); |
209 | filemap_dirty_folio(mapping, folio); | |
210 | folio_account_redirty(folio); | |
211 | index += folio_nr_pages(folio); | |
212 | folio_put(folio); | |
4adaa611 | 213 | } |
4adaa611 CM |
214 | } |
215 | ||
ed8f13bf QW |
216 | /* |
217 | * Process one page for __process_pages_contig(). | |
218 | * | |
219 | * Return >0 if we hit @page == @locked_page. | |
220 | * Return 0 if we updated the page status. | |
221 | * Return -EGAIN if the we need to try again. | |
222 | * (For PAGE_LOCK case but got dirty page or page not belong to mapping) | |
223 | */ | |
e38992be QW |
224 | static int process_one_page(struct btrfs_fs_info *fs_info, |
225 | struct address_space *mapping, | |
ed8f13bf | 226 | struct page *page, struct page *locked_page, |
e38992be | 227 | unsigned long page_ops, u64 start, u64 end) |
ed8f13bf | 228 | { |
e38992be QW |
229 | u32 len; |
230 | ||
231 | ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX); | |
232 | len = end + 1 - start; | |
233 | ||
ed8f13bf | 234 | if (page_ops & PAGE_SET_ORDERED) |
b945a463 | 235 | btrfs_page_clamp_set_ordered(fs_info, page, start, len); |
ed8f13bf | 236 | if (page_ops & PAGE_SET_ERROR) |
e38992be | 237 | btrfs_page_clamp_set_error(fs_info, page, start, len); |
ed8f13bf | 238 | if (page_ops & PAGE_START_WRITEBACK) { |
e38992be QW |
239 | btrfs_page_clamp_clear_dirty(fs_info, page, start, len); |
240 | btrfs_page_clamp_set_writeback(fs_info, page, start, len); | |
ed8f13bf QW |
241 | } |
242 | if (page_ops & PAGE_END_WRITEBACK) | |
e38992be | 243 | btrfs_page_clamp_clear_writeback(fs_info, page, start, len); |
a33a8e9a QW |
244 | |
245 | if (page == locked_page) | |
246 | return 1; | |
247 | ||
ed8f13bf | 248 | if (page_ops & PAGE_LOCK) { |
1e1de387 QW |
249 | int ret; |
250 | ||
251 | ret = btrfs_page_start_writer_lock(fs_info, page, start, len); | |
252 | if (ret) | |
253 | return ret; | |
ed8f13bf | 254 | if (!PageDirty(page) || page->mapping != mapping) { |
1e1de387 | 255 | btrfs_page_end_writer_lock(fs_info, page, start, len); |
ed8f13bf QW |
256 | return -EAGAIN; |
257 | } | |
258 | } | |
259 | if (page_ops & PAGE_UNLOCK) | |
1e1de387 | 260 | btrfs_page_end_writer_lock(fs_info, page, start, len); |
ed8f13bf QW |
261 | return 0; |
262 | } | |
263 | ||
da2c7009 LB |
264 | static int __process_pages_contig(struct address_space *mapping, |
265 | struct page *locked_page, | |
98af9ab1 | 266 | u64 start, u64 end, unsigned long page_ops, |
ed8f13bf QW |
267 | u64 *processed_end) |
268 | { | |
e38992be | 269 | struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb); |
ed8f13bf QW |
270 | pgoff_t start_index = start >> PAGE_SHIFT; |
271 | pgoff_t end_index = end >> PAGE_SHIFT; | |
272 | pgoff_t index = start_index; | |
ed8f13bf | 273 | unsigned long pages_processed = 0; |
04c6b79a | 274 | struct folio_batch fbatch; |
ed8f13bf QW |
275 | int err = 0; |
276 | int i; | |
277 | ||
278 | if (page_ops & PAGE_LOCK) { | |
279 | ASSERT(page_ops == PAGE_LOCK); | |
280 | ASSERT(processed_end && *processed_end == start); | |
281 | } | |
282 | ||
04c6b79a | 283 | if ((page_ops & PAGE_SET_ERROR) && start_index <= end_index) |
ed8f13bf QW |
284 | mapping_set_error(mapping, -EIO); |
285 | ||
04c6b79a VMO |
286 | folio_batch_init(&fbatch); |
287 | while (index <= end_index) { | |
288 | int found_folios; | |
289 | ||
290 | found_folios = filemap_get_folios_contig(mapping, &index, | |
291 | end_index, &fbatch); | |
ed8f13bf | 292 | |
04c6b79a | 293 | if (found_folios == 0) { |
ed8f13bf QW |
294 | /* |
295 | * Only if we're going to lock these pages, we can find | |
296 | * nothing at @index. | |
297 | */ | |
298 | ASSERT(page_ops & PAGE_LOCK); | |
299 | err = -EAGAIN; | |
300 | goto out; | |
301 | } | |
302 | ||
04c6b79a | 303 | for (i = 0; i < found_folios; i++) { |
ed8f13bf | 304 | int process_ret; |
04c6b79a | 305 | struct folio *folio = fbatch.folios[i]; |
e38992be | 306 | process_ret = process_one_page(fs_info, mapping, |
04c6b79a | 307 | &folio->page, locked_page, page_ops, |
e38992be | 308 | start, end); |
ed8f13bf | 309 | if (process_ret < 0) { |
ed8f13bf | 310 | err = -EAGAIN; |
04c6b79a | 311 | folio_batch_release(&fbatch); |
ed8f13bf QW |
312 | goto out; |
313 | } | |
04c6b79a | 314 | pages_processed += folio_nr_pages(folio); |
ed8f13bf | 315 | } |
04c6b79a | 316 | folio_batch_release(&fbatch); |
ed8f13bf QW |
317 | cond_resched(); |
318 | } | |
319 | out: | |
320 | if (err && processed_end) { | |
321 | /* | |
322 | * Update @processed_end. I know this is awful since it has | |
323 | * two different return value patterns (inclusive vs exclusive). | |
324 | * | |
325 | * But the exclusive pattern is necessary if @start is 0, or we | |
326 | * underflow and check against processed_end won't work as | |
327 | * expected. | |
328 | */ | |
329 | if (pages_processed) | |
330 | *processed_end = min(end, | |
331 | ((u64)(start_index + pages_processed) << PAGE_SHIFT) - 1); | |
332 | else | |
333 | *processed_end = start; | |
334 | } | |
335 | return err; | |
336 | } | |
da2c7009 | 337 | |
143bede5 JM |
338 | static noinline void __unlock_for_delalloc(struct inode *inode, |
339 | struct page *locked_page, | |
340 | u64 start, u64 end) | |
c8b97818 | 341 | { |
09cbfeaf KS |
342 | unsigned long index = start >> PAGE_SHIFT; |
343 | unsigned long end_index = end >> PAGE_SHIFT; | |
c8b97818 | 344 | |
76c0021d | 345 | ASSERT(locked_page); |
c8b97818 | 346 | if (index == locked_page->index && end_index == index) |
143bede5 | 347 | return; |
c8b97818 | 348 | |
98af9ab1 | 349 | __process_pages_contig(inode->i_mapping, locked_page, start, end, |
76c0021d | 350 | PAGE_UNLOCK, NULL); |
c8b97818 CM |
351 | } |
352 | ||
353 | static noinline int lock_delalloc_pages(struct inode *inode, | |
354 | struct page *locked_page, | |
355 | u64 delalloc_start, | |
356 | u64 delalloc_end) | |
357 | { | |
09cbfeaf | 358 | unsigned long index = delalloc_start >> PAGE_SHIFT; |
09cbfeaf | 359 | unsigned long end_index = delalloc_end >> PAGE_SHIFT; |
98af9ab1 | 360 | u64 processed_end = delalloc_start; |
c8b97818 | 361 | int ret; |
c8b97818 | 362 | |
76c0021d | 363 | ASSERT(locked_page); |
c8b97818 CM |
364 | if (index == locked_page->index && index == end_index) |
365 | return 0; | |
366 | ||
98af9ab1 QW |
367 | ret = __process_pages_contig(inode->i_mapping, locked_page, delalloc_start, |
368 | delalloc_end, PAGE_LOCK, &processed_end); | |
369 | if (ret == -EAGAIN && processed_end > delalloc_start) | |
76c0021d | 370 | __unlock_for_delalloc(inode, locked_page, delalloc_start, |
98af9ab1 | 371 | processed_end); |
c8b97818 CM |
372 | return ret; |
373 | } | |
374 | ||
375 | /* | |
3522e903 | 376 | * Find and lock a contiguous range of bytes in the file marked as delalloc, no |
2749f7ef | 377 | * more than @max_bytes. |
c8b97818 | 378 | * |
2749f7ef QW |
379 | * @start: The original start bytenr to search. |
380 | * Will store the extent range start bytenr. | |
381 | * @end: The original end bytenr of the search range | |
382 | * Will store the extent range end bytenr. | |
383 | * | |
384 | * Return true if we find a delalloc range which starts inside the original | |
385 | * range, and @start/@end will store the delalloc range start/end. | |
386 | * | |
387 | * Return false if we can't find any delalloc range which starts inside the | |
388 | * original range, and @start/@end will be the non-delalloc range start/end. | |
c8b97818 | 389 | */ |
ce9f967f | 390 | EXPORT_FOR_TESTS |
3522e903 | 391 | noinline_for_stack bool find_lock_delalloc_range(struct inode *inode, |
294e30fe | 392 | struct page *locked_page, u64 *start, |
917aacec | 393 | u64 *end) |
c8b97818 | 394 | { |
f7b12a62 | 395 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9978059b | 396 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
2749f7ef QW |
397 | const u64 orig_start = *start; |
398 | const u64 orig_end = *end; | |
f7b12a62 NA |
399 | /* The sanity tests may not set a valid fs_info. */ |
400 | u64 max_bytes = fs_info ? fs_info->max_extent_size : BTRFS_MAX_EXTENT_SIZE; | |
c8b97818 CM |
401 | u64 delalloc_start; |
402 | u64 delalloc_end; | |
3522e903 | 403 | bool found; |
9655d298 | 404 | struct extent_state *cached_state = NULL; |
c8b97818 CM |
405 | int ret; |
406 | int loops = 0; | |
407 | ||
2749f7ef QW |
408 | /* Caller should pass a valid @end to indicate the search range end */ |
409 | ASSERT(orig_end > orig_start); | |
410 | ||
411 | /* The range should at least cover part of the page */ | |
412 | ASSERT(!(orig_start >= page_offset(locked_page) + PAGE_SIZE || | |
413 | orig_end <= page_offset(locked_page))); | |
c8b97818 CM |
414 | again: |
415 | /* step one, find a bunch of delalloc bytes starting at start */ | |
416 | delalloc_start = *start; | |
417 | delalloc_end = 0; | |
083e75e7 JB |
418 | found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
419 | max_bytes, &cached_state); | |
2749f7ef | 420 | if (!found || delalloc_end <= *start || delalloc_start > orig_end) { |
c8b97818 | 421 | *start = delalloc_start; |
2749f7ef QW |
422 | |
423 | /* @delalloc_end can be -1, never go beyond @orig_end */ | |
424 | *end = min(delalloc_end, orig_end); | |
c2a128d2 | 425 | free_extent_state(cached_state); |
3522e903 | 426 | return false; |
c8b97818 CM |
427 | } |
428 | ||
70b99e69 CM |
429 | /* |
430 | * start comes from the offset of locked_page. We have to lock | |
431 | * pages in order, so we can't process delalloc bytes before | |
432 | * locked_page | |
433 | */ | |
d397712b | 434 | if (delalloc_start < *start) |
70b99e69 | 435 | delalloc_start = *start; |
70b99e69 | 436 | |
c8b97818 CM |
437 | /* |
438 | * make sure to limit the number of pages we try to lock down | |
c8b97818 | 439 | */ |
7bf811a5 JB |
440 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
441 | delalloc_end = delalloc_start + max_bytes - 1; | |
d397712b | 442 | |
c8b97818 CM |
443 | /* step two, lock all the pages after the page that has start */ |
444 | ret = lock_delalloc_pages(inode, locked_page, | |
445 | delalloc_start, delalloc_end); | |
9bfd61d9 | 446 | ASSERT(!ret || ret == -EAGAIN); |
c8b97818 CM |
447 | if (ret == -EAGAIN) { |
448 | /* some of the pages are gone, lets avoid looping by | |
449 | * shortening the size of the delalloc range we're searching | |
450 | */ | |
9655d298 | 451 | free_extent_state(cached_state); |
7d788742 | 452 | cached_state = NULL; |
c8b97818 | 453 | if (!loops) { |
09cbfeaf | 454 | max_bytes = PAGE_SIZE; |
c8b97818 CM |
455 | loops = 1; |
456 | goto again; | |
457 | } else { | |
3522e903 | 458 | found = false; |
c8b97818 CM |
459 | goto out_failed; |
460 | } | |
461 | } | |
c8b97818 CM |
462 | |
463 | /* step three, lock the state bits for the whole range */ | |
570eb97b | 464 | lock_extent(tree, delalloc_start, delalloc_end, &cached_state); |
c8b97818 CM |
465 | |
466 | /* then test to make sure it is all still delalloc */ | |
467 | ret = test_range_bit(tree, delalloc_start, delalloc_end, | |
9655d298 | 468 | EXTENT_DELALLOC, 1, cached_state); |
c8b97818 | 469 | if (!ret) { |
570eb97b JB |
470 | unlock_extent(tree, delalloc_start, delalloc_end, |
471 | &cached_state); | |
c8b97818 CM |
472 | __unlock_for_delalloc(inode, locked_page, |
473 | delalloc_start, delalloc_end); | |
474 | cond_resched(); | |
475 | goto again; | |
476 | } | |
9655d298 | 477 | free_extent_state(cached_state); |
c8b97818 CM |
478 | *start = delalloc_start; |
479 | *end = delalloc_end; | |
480 | out_failed: | |
481 | return found; | |
482 | } | |
483 | ||
ad7ff17b | 484 | void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
74e9194a | 485 | struct page *locked_page, |
f97e27e9 | 486 | u32 clear_bits, unsigned long page_ops) |
873695b3 | 487 | { |
bd015294 | 488 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, NULL); |
873695b3 | 489 | |
ad7ff17b | 490 | __process_pages_contig(inode->vfs_inode.i_mapping, locked_page, |
98af9ab1 | 491 | start, end, page_ops, NULL); |
873695b3 LB |
492 | } |
493 | ||
87c11705 JB |
494 | static int insert_failrec(struct btrfs_inode *inode, |
495 | struct io_failure_record *failrec) | |
d1310b2e | 496 | { |
87c11705 | 497 | struct rb_node *exist; |
d1310b2e | 498 | |
87c11705 JB |
499 | spin_lock(&inode->io_failure_lock); |
500 | exist = rb_simple_insert(&inode->io_failure_tree, failrec->bytenr, | |
501 | &failrec->rb_node); | |
502 | spin_unlock(&inode->io_failure_lock); | |
503 | ||
504 | return (exist == NULL) ? 0 : -EEXIST; | |
d1310b2e CM |
505 | } |
506 | ||
87c11705 | 507 | static struct io_failure_record *get_failrec(struct btrfs_inode *inode, u64 start) |
d1310b2e CM |
508 | { |
509 | struct rb_node *node; | |
87c11705 | 510 | struct io_failure_record *failrec = ERR_PTR(-ENOENT); |
d1310b2e | 511 | |
87c11705 JB |
512 | spin_lock(&inode->io_failure_lock); |
513 | node = rb_simple_search(&inode->io_failure_tree, start); | |
514 | if (node) | |
515 | failrec = rb_entry(node, struct io_failure_record, rb_node); | |
516 | spin_unlock(&inode->io_failure_lock); | |
2279a270 | 517 | return failrec; |
d1310b2e CM |
518 | } |
519 | ||
bd86a532 CH |
520 | static void free_io_failure(struct btrfs_inode *inode, |
521 | struct io_failure_record *rec) | |
4a54c8c1 | 522 | { |
87c11705 JB |
523 | spin_lock(&inode->io_failure_lock); |
524 | rb_erase(&rec->rb_node, &inode->io_failure_tree); | |
525 | spin_unlock(&inode->io_failure_lock); | |
4a54c8c1 | 526 | |
4a54c8c1 | 527 | kfree(rec); |
4a54c8c1 JS |
528 | } |
529 | ||
4a54c8c1 JS |
530 | /* |
531 | * this bypasses the standard btrfs submit functions deliberately, as | |
532 | * the standard behavior is to write all copies in a raid setup. here we only | |
533 | * want to write the one bad copy. so we do the mapping for ourselves and issue | |
534 | * submit_bio directly. | |
3ec706c8 | 535 | * to avoid any synchronization issues, wait for the data after writing, which |
4a54c8c1 JS |
536 | * actually prevents the read that triggered the error from finishing. |
537 | * currently, there can be no more than two copies of every data bit. thus, | |
538 | * exactly one rewrite is required. | |
539 | */ | |
38d5e541 QW |
540 | static int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, |
541 | u64 length, u64 logical, struct page *page, | |
542 | unsigned int pg_offset, int mirror_num) | |
4a54c8c1 | 543 | { |
4a54c8c1 | 544 | struct btrfs_device *dev; |
e9458bfe CH |
545 | struct bio_vec bvec; |
546 | struct bio bio; | |
4a54c8c1 JS |
547 | u64 map_length = 0; |
548 | u64 sector; | |
4c664611 | 549 | struct btrfs_io_context *bioc = NULL; |
e9458bfe | 550 | int ret = 0; |
4a54c8c1 | 551 | |
1751e8a6 | 552 | ASSERT(!(fs_info->sb->s_flags & SB_RDONLY)); |
4a54c8c1 JS |
553 | BUG_ON(!mirror_num); |
554 | ||
554aed7d JT |
555 | if (btrfs_repair_one_zone(fs_info, logical)) |
556 | return 0; | |
f7ef5287 | 557 | |
4a54c8c1 JS |
558 | map_length = length; |
559 | ||
b5de8d0d | 560 | /* |
4c664611 | 561 | * Avoid races with device replace and make sure our bioc has devices |
b5de8d0d FM |
562 | * associated to its stripes that don't go away while we are doing the |
563 | * read repair operation. | |
564 | */ | |
565 | btrfs_bio_counter_inc_blocked(fs_info); | |
e4ff5fb5 | 566 | if (btrfs_is_parity_mirror(fs_info, logical, length)) { |
c725328c LB |
567 | /* |
568 | * Note that we don't use BTRFS_MAP_WRITE because it's supposed | |
569 | * to update all raid stripes, but here we just want to correct | |
570 | * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad | |
571 | * stripe's dev and sector. | |
572 | */ | |
573 | ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical, | |
4c664611 | 574 | &map_length, &bioc, 0); |
e9458bfe CH |
575 | if (ret) |
576 | goto out_counter_dec; | |
4c664611 | 577 | ASSERT(bioc->mirror_num == 1); |
c725328c LB |
578 | } else { |
579 | ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, | |
4c664611 | 580 | &map_length, &bioc, mirror_num); |
e9458bfe CH |
581 | if (ret) |
582 | goto out_counter_dec; | |
4c664611 | 583 | BUG_ON(mirror_num != bioc->mirror_num); |
4a54c8c1 | 584 | } |
c725328c | 585 | |
4c664611 | 586 | sector = bioc->stripes[bioc->mirror_num - 1].physical >> 9; |
4c664611 QW |
587 | dev = bioc->stripes[bioc->mirror_num - 1].dev; |
588 | btrfs_put_bioc(bioc); | |
e9458bfe | 589 | |
ebbede42 AJ |
590 | if (!dev || !dev->bdev || |
591 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) { | |
e9458bfe CH |
592 | ret = -EIO; |
593 | goto out_counter_dec; | |
4a54c8c1 | 594 | } |
4a54c8c1 | 595 | |
e9458bfe CH |
596 | bio_init(&bio, dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC); |
597 | bio.bi_iter.bi_sector = sector; | |
598 | __bio_add_page(&bio, page, length, pg_offset); | |
599 | ||
600 | btrfsic_check_bio(&bio); | |
601 | ret = submit_bio_wait(&bio); | |
602 | if (ret) { | |
4a54c8c1 | 603 | /* try to remap that extent elsewhere? */ |
442a4f63 | 604 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
e9458bfe | 605 | goto out_bio_uninit; |
4a54c8c1 JS |
606 | } |
607 | ||
b14af3b4 DS |
608 | btrfs_info_rl_in_rcu(fs_info, |
609 | "read error corrected: ino %llu off %llu (dev %s sector %llu)", | |
6ec656bc | 610 | ino, start, |
1203b681 | 611 | rcu_str_deref(dev->name), sector); |
e9458bfe CH |
612 | ret = 0; |
613 | ||
614 | out_bio_uninit: | |
615 | bio_uninit(&bio); | |
616 | out_counter_dec: | |
b5de8d0d | 617 | btrfs_bio_counter_dec(fs_info); |
e9458bfe | 618 | return ret; |
4a54c8c1 JS |
619 | } |
620 | ||
2b48966a | 621 | int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num) |
ea466794 | 622 | { |
20a1fbf9 | 623 | struct btrfs_fs_info *fs_info = eb->fs_info; |
ea466794 | 624 | u64 start = eb->start; |
cc5e31a4 | 625 | int i, num_pages = num_extent_pages(eb); |
d95603b2 | 626 | int ret = 0; |
ea466794 | 627 | |
bc98a42c | 628 | if (sb_rdonly(fs_info->sb)) |
908960c6 ID |
629 | return -EROFS; |
630 | ||
ea466794 | 631 | for (i = 0; i < num_pages; i++) { |
fb85fc9a | 632 | struct page *p = eb->pages[i]; |
1203b681 | 633 | |
6ec656bc | 634 | ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p, |
1203b681 | 635 | start - page_offset(p), mirror_num); |
ea466794 JB |
636 | if (ret) |
637 | break; | |
09cbfeaf | 638 | start += PAGE_SIZE; |
ea466794 JB |
639 | } |
640 | ||
641 | return ret; | |
642 | } | |
643 | ||
c144c63f CH |
644 | static int next_mirror(const struct io_failure_record *failrec, int cur_mirror) |
645 | { | |
646 | if (cur_mirror == failrec->num_copies) | |
647 | return cur_mirror + 1 - failrec->num_copies; | |
648 | return cur_mirror + 1; | |
649 | } | |
650 | ||
651 | static int prev_mirror(const struct io_failure_record *failrec, int cur_mirror) | |
652 | { | |
653 | if (cur_mirror == 1) | |
654 | return failrec->num_copies; | |
655 | return cur_mirror - 1; | |
656 | } | |
657 | ||
4a54c8c1 JS |
658 | /* |
659 | * each time an IO finishes, we do a fast check in the IO failure tree | |
660 | * to see if we need to process or clean up an io_failure_record | |
661 | */ | |
0d0a762c JB |
662 | int btrfs_clean_io_failure(struct btrfs_inode *inode, u64 start, |
663 | struct page *page, unsigned int pg_offset) | |
4a54c8c1 | 664 | { |
0d0a762c | 665 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
0d0a762c JB |
666 | struct extent_io_tree *io_tree = &inode->io_tree; |
667 | u64 ino = btrfs_ino(inode); | |
cdca85b0 | 668 | u64 locked_start, locked_end; |
4a54c8c1 | 669 | struct io_failure_record *failrec; |
c144c63f | 670 | int mirror; |
cdca85b0 | 671 | int ret; |
4a54c8c1 | 672 | |
87c11705 | 673 | failrec = get_failrec(inode, start); |
2279a270 | 674 | if (IS_ERR(failrec)) |
4a54c8c1 JS |
675 | return 0; |
676 | ||
4a54c8c1 JS |
677 | BUG_ON(!failrec->this_mirror); |
678 | ||
bc98a42c | 679 | if (sb_rdonly(fs_info->sb)) |
908960c6 | 680 | goto out; |
4a54c8c1 | 681 | |
cdca85b0 JB |
682 | ret = find_first_extent_bit(io_tree, failrec->bytenr, &locked_start, |
683 | &locked_end, EXTENT_LOCKED, NULL); | |
684 | if (ret || locked_start > failrec->bytenr || | |
685 | locked_end < failrec->bytenr + failrec->len - 1) | |
c144c63f CH |
686 | goto out; |
687 | ||
688 | mirror = failrec->this_mirror; | |
689 | do { | |
690 | mirror = prev_mirror(failrec, mirror); | |
691 | repair_io_failure(fs_info, ino, start, failrec->len, | |
692 | failrec->logical, page, pg_offset, mirror); | |
693 | } while (mirror != failrec->failed_mirror); | |
4a54c8c1 JS |
694 | |
695 | out: | |
87c11705 | 696 | free_io_failure(inode, failrec); |
454ff3de | 697 | return 0; |
4a54c8c1 JS |
698 | } |
699 | ||
f612496b MX |
700 | /* |
701 | * Can be called when | |
702 | * - hold extent lock | |
703 | * - under ordered extent | |
704 | * - the inode is freeing | |
705 | */ | |
7ab7956e | 706 | void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end) |
f612496b | 707 | { |
f612496b | 708 | struct io_failure_record *failrec; |
87c11705 | 709 | struct rb_node *node, *next; |
f612496b | 710 | |
87c11705 | 711 | if (RB_EMPTY_ROOT(&inode->io_failure_tree)) |
f612496b MX |
712 | return; |
713 | ||
87c11705 JB |
714 | spin_lock(&inode->io_failure_lock); |
715 | node = rb_simple_search_first(&inode->io_failure_tree, start); | |
716 | while (node) { | |
717 | failrec = rb_entry(node, struct io_failure_record, rb_node); | |
718 | if (failrec->bytenr > end) | |
f612496b MX |
719 | break; |
720 | ||
87c11705 JB |
721 | next = rb_next(node); |
722 | rb_erase(&failrec->rb_node, &inode->io_failure_tree); | |
f612496b MX |
723 | kfree(failrec); |
724 | ||
87c11705 | 725 | node = next; |
f612496b | 726 | } |
87c11705 | 727 | spin_unlock(&inode->io_failure_lock); |
f612496b MX |
728 | } |
729 | ||
3526302f | 730 | static struct io_failure_record *btrfs_get_io_failure_record(struct inode *inode, |
7aa51232 CH |
731 | struct btrfs_bio *bbio, |
732 | unsigned int bio_offset) | |
4a54c8c1 | 733 | { |
ab8d0fc4 | 734 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
7aa51232 | 735 | u64 start = bbio->file_offset + bio_offset; |
2fe6303e | 736 | struct io_failure_record *failrec; |
150e4b05 | 737 | const u32 sectorsize = fs_info->sectorsize; |
4a54c8c1 | 738 | int ret; |
4a54c8c1 | 739 | |
87c11705 | 740 | failrec = get_failrec(BTRFS_I(inode), start); |
3526302f | 741 | if (!IS_ERR(failrec)) { |
ab8d0fc4 | 742 | btrfs_debug(fs_info, |
1245835d | 743 | "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu", |
87c11705 | 744 | failrec->logical, failrec->bytenr, failrec->len); |
4a54c8c1 JS |
745 | /* |
746 | * when data can be on disk more than twice, add to failrec here | |
747 | * (e.g. with a list for failed_mirror) to make | |
748 | * clean_io_failure() clean all those errors at once. | |
749 | */ | |
7aa51232 | 750 | ASSERT(failrec->this_mirror == bbio->mirror_num); |
c144c63f | 751 | ASSERT(failrec->len == fs_info->sectorsize); |
3526302f | 752 | return failrec; |
4a54c8c1 | 753 | } |
2fe6303e | 754 | |
3526302f NB |
755 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
756 | if (!failrec) | |
757 | return ERR_PTR(-ENOMEM); | |
2fe6303e | 758 | |
87c11705 JB |
759 | RB_CLEAR_NODE(&failrec->rb_node); |
760 | failrec->bytenr = start; | |
150e4b05 | 761 | failrec->len = sectorsize; |
7aa51232 CH |
762 | failrec->failed_mirror = bbio->mirror_num; |
763 | failrec->this_mirror = bbio->mirror_num; | |
81bd9328 | 764 | failrec->logical = (bbio->iter.bi_sector << SECTOR_SHIFT) + bio_offset; |
3526302f | 765 | |
3526302f | 766 | btrfs_debug(fs_info, |
81bd9328 CH |
767 | "new io failure record logical %llu start %llu", |
768 | failrec->logical, start); | |
3526302f | 769 | |
81bd9328 | 770 | failrec->num_copies = btrfs_num_copies(fs_info, failrec->logical, sectorsize); |
c144c63f CH |
771 | if (failrec->num_copies == 1) { |
772 | /* | |
773 | * We only have a single copy of the data, so don't bother with | |
774 | * all the retry and error correction code that follows. No | |
775 | * matter what the error is, it is very likely to persist. | |
776 | */ | |
777 | btrfs_debug(fs_info, | |
778 | "cannot repair logical %llu num_copies %d", | |
779 | failrec->logical, failrec->num_copies); | |
3526302f NB |
780 | kfree(failrec); |
781 | return ERR_PTR(-EIO); | |
782 | } | |
783 | ||
3526302f | 784 | /* Set the bits in the private failure tree */ |
87c11705 JB |
785 | ret = insert_failrec(BTRFS_I(inode), failrec); |
786 | if (ret) { | |
3526302f NB |
787 | kfree(failrec); |
788 | return ERR_PTR(ret); | |
789 | } | |
790 | ||
791 | return failrec; | |
2fe6303e MX |
792 | } |
793 | ||
7aa51232 CH |
794 | int btrfs_repair_one_sector(struct inode *inode, struct btrfs_bio *failed_bbio, |
795 | u32 bio_offset, struct page *page, unsigned int pgoff, | |
150e4b05 | 796 | submit_bio_hook_t *submit_bio_hook) |
2fe6303e | 797 | { |
7aa51232 | 798 | u64 start = failed_bbio->file_offset + bio_offset; |
2fe6303e | 799 | struct io_failure_record *failrec; |
77d5d689 | 800 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
7aa51232 | 801 | struct bio *failed_bio = &failed_bbio->bio; |
7ffd27e3 | 802 | const int icsum = bio_offset >> fs_info->sectorsize_bits; |
77d5d689 | 803 | struct bio *repair_bio; |
c3a3b19b | 804 | struct btrfs_bio *repair_bbio; |
2fe6303e | 805 | |
77d5d689 OS |
806 | btrfs_debug(fs_info, |
807 | "repair read error: read error at %llu", start); | |
2fe6303e | 808 | |
1f7ad75b | 809 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
2fe6303e | 810 | |
7aa51232 | 811 | failrec = btrfs_get_io_failure_record(inode, failed_bbio, bio_offset); |
3526302f | 812 | if (IS_ERR(failrec)) |
150e4b05 | 813 | return PTR_ERR(failrec); |
2fe6303e | 814 | |
c144c63f CH |
815 | /* |
816 | * There are two premises: | |
817 | * a) deliver good data to the caller | |
818 | * b) correct the bad sectors on disk | |
819 | * | |
820 | * Since we're only doing repair for one sector, we only need to get | |
821 | * a good copy of the failed sector and if we succeed, we have setup | |
822 | * everything for repair_io_failure to do the rest for us. | |
823 | */ | |
824 | failrec->this_mirror = next_mirror(failrec, failrec->this_mirror); | |
825 | if (failrec->this_mirror == failrec->failed_mirror) { | |
826 | btrfs_debug(fs_info, | |
827 | "failed to repair num_copies %d this_mirror %d failed_mirror %d", | |
828 | failrec->num_copies, failrec->this_mirror, failrec->failed_mirror); | |
87c11705 | 829 | free_io_failure(BTRFS_I(inode), failrec); |
150e4b05 | 830 | return -EIO; |
2fe6303e MX |
831 | } |
832 | ||
917f32a2 CH |
833 | repair_bio = btrfs_bio_alloc(1, REQ_OP_READ, failed_bbio->end_io, |
834 | failed_bbio->private); | |
c3a3b19b | 835 | repair_bbio = btrfs_bio(repair_bio); |
00d82525 | 836 | repair_bbio->file_offset = start; |
77d5d689 | 837 | repair_bio->bi_iter.bi_sector = failrec->logical >> 9; |
2fe6303e | 838 | |
c3a3b19b | 839 | if (failed_bbio->csum) { |
223486c2 | 840 | const u32 csum_size = fs_info->csum_size; |
77d5d689 | 841 | |
c3a3b19b QW |
842 | repair_bbio->csum = repair_bbio->csum_inline; |
843 | memcpy(repair_bbio->csum, | |
844 | failed_bbio->csum + csum_size * icsum, csum_size); | |
77d5d689 | 845 | } |
2fe6303e | 846 | |
77d5d689 | 847 | bio_add_page(repair_bio, page, failrec->len, pgoff); |
c3a3b19b | 848 | repair_bbio->iter = repair_bio->bi_iter; |
4a54c8c1 | 849 | |
ab8d0fc4 | 850 | btrfs_debug(btrfs_sb(inode->i_sb), |
1245835d QW |
851 | "repair read error: submitting new read to mirror %d", |
852 | failrec->this_mirror); | |
4a54c8c1 | 853 | |
8cbc3001 JB |
854 | /* |
855 | * At this point we have a bio, so any errors from submit_bio_hook() | |
856 | * will be handled by the endio on the repair_bio, so we can't return an | |
857 | * error here. | |
858 | */ | |
81bd9328 | 859 | submit_bio_hook(inode, repair_bio, failrec->this_mirror, 0); |
8cbc3001 | 860 | return BLK_STS_OK; |
150e4b05 QW |
861 | } |
862 | ||
863 | static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len) | |
864 | { | |
865 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); | |
866 | ||
867 | ASSERT(page_offset(page) <= start && | |
868 | start + len <= page_offset(page) + PAGE_SIZE); | |
869 | ||
150e4b05 | 870 | if (uptodate) { |
14605409 BB |
871 | if (fsverity_active(page->mapping->host) && |
872 | !PageError(page) && | |
873 | !PageUptodate(page) && | |
874 | start < i_size_read(page->mapping->host) && | |
875 | !fsverity_verify_page(page)) { | |
876 | btrfs_page_set_error(fs_info, page, start, len); | |
877 | } else { | |
878 | btrfs_page_set_uptodate(fs_info, page, start, len); | |
879 | } | |
150e4b05 QW |
880 | } else { |
881 | btrfs_page_clear_uptodate(fs_info, page, start, len); | |
882 | btrfs_page_set_error(fs_info, page, start, len); | |
883 | } | |
884 | ||
fbca46eb | 885 | if (!btrfs_is_subpage(fs_info, page)) |
150e4b05 | 886 | unlock_page(page); |
3d078efa | 887 | else |
150e4b05 QW |
888 | btrfs_subpage_end_reader(fs_info, page, start, len); |
889 | } | |
890 | ||
a5aa7ab6 CH |
891 | static void end_sector_io(struct page *page, u64 offset, bool uptodate) |
892 | { | |
893 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); | |
894 | const u32 sectorsize = inode->root->fs_info->sectorsize; | |
895 | struct extent_state *cached = NULL; | |
896 | ||
897 | end_page_read(page, uptodate, offset, sectorsize); | |
898 | if (uptodate) | |
899 | set_extent_uptodate(&inode->io_tree, offset, | |
900 | offset + sectorsize - 1, &cached, GFP_ATOMIC); | |
570eb97b JB |
901 | unlock_extent_atomic(&inode->io_tree, offset, offset + sectorsize - 1, |
902 | &cached); | |
a5aa7ab6 CH |
903 | } |
904 | ||
7aa51232 CH |
905 | static void submit_data_read_repair(struct inode *inode, |
906 | struct btrfs_bio *failed_bbio, | |
fd5a6f63 | 907 | u32 bio_offset, const struct bio_vec *bvec, |
7aa51232 | 908 | unsigned int error_bitmap) |
150e4b05 | 909 | { |
fd5a6f63 | 910 | const unsigned int pgoff = bvec->bv_offset; |
150e4b05 | 911 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
fd5a6f63 QW |
912 | struct page *page = bvec->bv_page; |
913 | const u64 start = page_offset(bvec->bv_page) + bvec->bv_offset; | |
914 | const u64 end = start + bvec->bv_len - 1; | |
150e4b05 QW |
915 | const u32 sectorsize = fs_info->sectorsize; |
916 | const int nr_bits = (end + 1 - start) >> fs_info->sectorsize_bits; | |
150e4b05 QW |
917 | int i; |
918 | ||
7aa51232 | 919 | BUG_ON(bio_op(&failed_bbio->bio) == REQ_OP_WRITE); |
150e4b05 | 920 | |
c0111c44 QW |
921 | /* This repair is only for data */ |
922 | ASSERT(is_data_inode(inode)); | |
923 | ||
150e4b05 QW |
924 | /* We're here because we had some read errors or csum mismatch */ |
925 | ASSERT(error_bitmap); | |
926 | ||
927 | /* | |
928 | * We only get called on buffered IO, thus page must be mapped and bio | |
929 | * must not be cloned. | |
930 | */ | |
7aa51232 | 931 | ASSERT(page->mapping && !bio_flagged(&failed_bbio->bio, BIO_CLONED)); |
150e4b05 QW |
932 | |
933 | /* Iterate through all the sectors in the range */ | |
934 | for (i = 0; i < nr_bits; i++) { | |
935 | const unsigned int offset = i * sectorsize; | |
150e4b05 QW |
936 | bool uptodate = false; |
937 | int ret; | |
938 | ||
939 | if (!(error_bitmap & (1U << i))) { | |
940 | /* | |
941 | * This sector has no error, just end the page read | |
942 | * and unlock the range. | |
943 | */ | |
944 | uptodate = true; | |
945 | goto next; | |
946 | } | |
947 | ||
7aa51232 CH |
948 | ret = btrfs_repair_one_sector(inode, failed_bbio, |
949 | bio_offset + offset, page, pgoff + offset, | |
950 | btrfs_submit_data_read_bio); | |
150e4b05 QW |
951 | if (!ret) { |
952 | /* | |
953 | * We have submitted the read repair, the page release | |
954 | * will be handled by the endio function of the | |
955 | * submitted repair bio. | |
956 | * Thus we don't need to do any thing here. | |
957 | */ | |
958 | continue; | |
959 | } | |
960 | /* | |
fd5a6f63 QW |
961 | * Continue on failed repair, otherwise the remaining sectors |
962 | * will not be properly unlocked. | |
150e4b05 | 963 | */ |
150e4b05 | 964 | next: |
a5aa7ab6 | 965 | end_sector_io(page, start + offset, uptodate); |
150e4b05 | 966 | } |
4a54c8c1 JS |
967 | } |
968 | ||
d1310b2e CM |
969 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
970 | ||
b5227c07 | 971 | void end_extent_writepage(struct page *page, int err, u64 start, u64 end) |
87826df0 | 972 | { |
38a39ac7 | 973 | struct btrfs_inode *inode; |
25c1252a | 974 | const bool uptodate = (err == 0); |
3e2426bd | 975 | int ret = 0; |
87826df0 | 976 | |
38a39ac7 QW |
977 | ASSERT(page && page->mapping); |
978 | inode = BTRFS_I(page->mapping->host); | |
979 | btrfs_writepage_endio_finish_ordered(inode, page, start, end, uptodate); | |
87826df0 | 980 | |
87826df0 | 981 | if (!uptodate) { |
963e4db8 QW |
982 | const struct btrfs_fs_info *fs_info = inode->root->fs_info; |
983 | u32 len; | |
984 | ||
985 | ASSERT(end + 1 - start <= U32_MAX); | |
986 | len = end + 1 - start; | |
987 | ||
988 | btrfs_page_clear_uptodate(fs_info, page, start, len); | |
989 | btrfs_page_set_error(fs_info, page, start, len); | |
bff5baf8 | 990 | ret = err < 0 ? err : -EIO; |
5dca6eea | 991 | mapping_set_error(page->mapping, ret); |
87826df0 | 992 | } |
87826df0 JM |
993 | } |
994 | ||
d1310b2e CM |
995 | /* |
996 | * after a writepage IO is done, we need to: | |
997 | * clear the uptodate bits on error | |
998 | * clear the writeback bits in the extent tree for this IO | |
999 | * end_page_writeback if the page has no more pending IO | |
1000 | * | |
1001 | * Scheduling is not allowed, so the extent state tree is expected | |
1002 | * to have one and only one object corresponding to this IO. | |
1003 | */ | |
917f32a2 | 1004 | static void end_bio_extent_writepage(struct btrfs_bio *bbio) |
d1310b2e | 1005 | { |
917f32a2 | 1006 | struct bio *bio = &bbio->bio; |
4e4cbee9 | 1007 | int error = blk_status_to_errno(bio->bi_status); |
2c30c71b | 1008 | struct bio_vec *bvec; |
d1310b2e CM |
1009 | u64 start; |
1010 | u64 end; | |
6dc4f100 | 1011 | struct bvec_iter_all iter_all; |
d8e3fb10 | 1012 | bool first_bvec = true; |
d1310b2e | 1013 | |
c09abff8 | 1014 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
2b070cfe | 1015 | bio_for_each_segment_all(bvec, bio, iter_all) { |
d1310b2e | 1016 | struct page *page = bvec->bv_page; |
0b246afa JM |
1017 | struct inode *inode = page->mapping->host; |
1018 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
321a02db QW |
1019 | const u32 sectorsize = fs_info->sectorsize; |
1020 | ||
1021 | /* Our read/write should always be sector aligned. */ | |
1022 | if (!IS_ALIGNED(bvec->bv_offset, sectorsize)) | |
1023 | btrfs_err(fs_info, | |
1024 | "partial page write in btrfs with offset %u and length %u", | |
1025 | bvec->bv_offset, bvec->bv_len); | |
1026 | else if (!IS_ALIGNED(bvec->bv_len, sectorsize)) | |
1027 | btrfs_info(fs_info, | |
1028 | "incomplete page write with offset %u and length %u", | |
1029 | bvec->bv_offset, bvec->bv_len); | |
1030 | ||
1031 | start = page_offset(page) + bvec->bv_offset; | |
1032 | end = start + bvec->bv_len - 1; | |
d1310b2e | 1033 | |
d8e3fb10 NA |
1034 | if (first_bvec) { |
1035 | btrfs_record_physical_zoned(inode, start, bio); | |
1036 | first_bvec = false; | |
1037 | } | |
1038 | ||
4e4cbee9 | 1039 | end_extent_writepage(page, error, start, end); |
9047e317 QW |
1040 | |
1041 | btrfs_page_clear_writeback(fs_info, page, start, bvec->bv_len); | |
2c30c71b | 1042 | } |
2b1f55b0 | 1043 | |
d1310b2e | 1044 | bio_put(bio); |
d1310b2e CM |
1045 | } |
1046 | ||
94e8c95c QW |
1047 | /* |
1048 | * Record previously processed extent range | |
1049 | * | |
1050 | * For endio_readpage_release_extent() to handle a full extent range, reducing | |
1051 | * the extent io operations. | |
1052 | */ | |
1053 | struct processed_extent { | |
1054 | struct btrfs_inode *inode; | |
1055 | /* Start of the range in @inode */ | |
1056 | u64 start; | |
2e626e56 | 1057 | /* End of the range in @inode */ |
94e8c95c QW |
1058 | u64 end; |
1059 | bool uptodate; | |
1060 | }; | |
1061 | ||
1062 | /* | |
1063 | * Try to release processed extent range | |
1064 | * | |
1065 | * May not release the extent range right now if the current range is | |
1066 | * contiguous to processed extent. | |
1067 | * | |
1068 | * Will release processed extent when any of @inode, @uptodate, the range is | |
1069 | * no longer contiguous to the processed range. | |
1070 | * | |
1071 | * Passing @inode == NULL will force processed extent to be released. | |
1072 | */ | |
1073 | static void endio_readpage_release_extent(struct processed_extent *processed, | |
1074 | struct btrfs_inode *inode, u64 start, u64 end, | |
1075 | bool uptodate) | |
883d0de4 MX |
1076 | { |
1077 | struct extent_state *cached = NULL; | |
94e8c95c QW |
1078 | struct extent_io_tree *tree; |
1079 | ||
1080 | /* The first extent, initialize @processed */ | |
1081 | if (!processed->inode) | |
1082 | goto update; | |
883d0de4 | 1083 | |
94e8c95c QW |
1084 | /* |
1085 | * Contiguous to processed extent, just uptodate the end. | |
1086 | * | |
1087 | * Several things to notice: | |
1088 | * | |
1089 | * - bio can be merged as long as on-disk bytenr is contiguous | |
1090 | * This means we can have page belonging to other inodes, thus need to | |
1091 | * check if the inode still matches. | |
1092 | * - bvec can contain range beyond current page for multi-page bvec | |
1093 | * Thus we need to do processed->end + 1 >= start check | |
1094 | */ | |
1095 | if (processed->inode == inode && processed->uptodate == uptodate && | |
1096 | processed->end + 1 >= start && end >= processed->end) { | |
1097 | processed->end = end; | |
1098 | return; | |
1099 | } | |
1100 | ||
1101 | tree = &processed->inode->io_tree; | |
1102 | /* | |
1103 | * Now we don't have range contiguous to the processed range, release | |
1104 | * the processed range now. | |
1105 | */ | |
570eb97b | 1106 | unlock_extent_atomic(tree, processed->start, processed->end, &cached); |
94e8c95c QW |
1107 | |
1108 | update: | |
1109 | /* Update processed to current range */ | |
1110 | processed->inode = inode; | |
1111 | processed->start = start; | |
1112 | processed->end = end; | |
1113 | processed->uptodate = uptodate; | |
883d0de4 MX |
1114 | } |
1115 | ||
92082d40 QW |
1116 | static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page) |
1117 | { | |
1118 | ASSERT(PageLocked(page)); | |
fbca46eb | 1119 | if (!btrfs_is_subpage(fs_info, page)) |
92082d40 QW |
1120 | return; |
1121 | ||
1122 | ASSERT(PagePrivate(page)); | |
1123 | btrfs_subpage_start_reader(fs_info, page, page_offset(page), PAGE_SIZE); | |
1124 | } | |
1125 | ||
d9bb77d5 | 1126 | /* |
01cd3909 | 1127 | * Find extent buffer for a givne bytenr. |
d9bb77d5 QW |
1128 | * |
1129 | * This is for end_bio_extent_readpage(), thus we can't do any unsafe locking | |
1130 | * in endio context. | |
1131 | */ | |
1132 | static struct extent_buffer *find_extent_buffer_readpage( | |
1133 | struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr) | |
1134 | { | |
1135 | struct extent_buffer *eb; | |
1136 | ||
1137 | /* | |
1138 | * For regular sectorsize, we can use page->private to grab extent | |
1139 | * buffer | |
1140 | */ | |
fbca46eb | 1141 | if (fs_info->nodesize >= PAGE_SIZE) { |
d9bb77d5 QW |
1142 | ASSERT(PagePrivate(page) && page->private); |
1143 | return (struct extent_buffer *)page->private; | |
1144 | } | |
1145 | ||
01cd3909 DS |
1146 | /* For subpage case, we need to lookup buffer radix tree */ |
1147 | rcu_read_lock(); | |
1148 | eb = radix_tree_lookup(&fs_info->buffer_radix, | |
1149 | bytenr >> fs_info->sectorsize_bits); | |
1150 | rcu_read_unlock(); | |
d9bb77d5 QW |
1151 | ASSERT(eb); |
1152 | return eb; | |
1153 | } | |
1154 | ||
d1310b2e CM |
1155 | /* |
1156 | * after a readpage IO is done, we need to: | |
1157 | * clear the uptodate bits on error | |
1158 | * set the uptodate bits if things worked | |
1159 | * set the page up to date if all extents in the tree are uptodate | |
1160 | * clear the lock bit in the extent tree | |
1161 | * unlock the page if there are no other extents locked for it | |
1162 | * | |
1163 | * Scheduling is not allowed, so the extent state tree is expected | |
1164 | * to have one and only one object corresponding to this IO. | |
1165 | */ | |
917f32a2 | 1166 | static void end_bio_extent_readpage(struct btrfs_bio *bbio) |
d1310b2e | 1167 | { |
917f32a2 | 1168 | struct bio *bio = &bbio->bio; |
2c30c71b | 1169 | struct bio_vec *bvec; |
94e8c95c | 1170 | struct processed_extent processed = { 0 }; |
7ffd27e3 QW |
1171 | /* |
1172 | * The offset to the beginning of a bio, since one bio can never be | |
1173 | * larger than UINT_MAX, u32 here is enough. | |
1174 | */ | |
1175 | u32 bio_offset = 0; | |
5cf1ab56 | 1176 | int mirror; |
6dc4f100 | 1177 | struct bvec_iter_all iter_all; |
d1310b2e | 1178 | |
c09abff8 | 1179 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
2b070cfe | 1180 | bio_for_each_segment_all(bvec, bio, iter_all) { |
150e4b05 | 1181 | bool uptodate = !bio->bi_status; |
d1310b2e | 1182 | struct page *page = bvec->bv_page; |
a71754fc | 1183 | struct inode *inode = page->mapping->host; |
ab8d0fc4 | 1184 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
7ffd27e3 | 1185 | const u32 sectorsize = fs_info->sectorsize; |
150e4b05 | 1186 | unsigned int error_bitmap = (unsigned int)-1; |
97861cd1 | 1187 | bool repair = false; |
7ffd27e3 QW |
1188 | u64 start; |
1189 | u64 end; | |
1190 | u32 len; | |
507903b8 | 1191 | |
ab8d0fc4 JM |
1192 | btrfs_debug(fs_info, |
1193 | "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u", | |
1201b58b | 1194 | bio->bi_iter.bi_sector, bio->bi_status, |
c3a3b19b | 1195 | bbio->mirror_num); |
902b22f3 | 1196 | |
8b8bbd46 QW |
1197 | /* |
1198 | * We always issue full-sector reads, but if some block in a | |
1199 | * page fails to read, blk_update_request() will advance | |
1200 | * bv_offset and adjust bv_len to compensate. Print a warning | |
1201 | * for unaligned offsets, and an error if they don't add up to | |
1202 | * a full sector. | |
1203 | */ | |
1204 | if (!IS_ALIGNED(bvec->bv_offset, sectorsize)) | |
1205 | btrfs_err(fs_info, | |
1206 | "partial page read in btrfs with offset %u and length %u", | |
1207 | bvec->bv_offset, bvec->bv_len); | |
1208 | else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len, | |
1209 | sectorsize)) | |
1210 | btrfs_info(fs_info, | |
1211 | "incomplete page read with offset %u and length %u", | |
1212 | bvec->bv_offset, bvec->bv_len); | |
1213 | ||
1214 | start = page_offset(page) + bvec->bv_offset; | |
1215 | end = start + bvec->bv_len - 1; | |
facc8a22 | 1216 | len = bvec->bv_len; |
d1310b2e | 1217 | |
c3a3b19b | 1218 | mirror = bbio->mirror_num; |
78e62c02 | 1219 | if (likely(uptodate)) { |
150e4b05 | 1220 | if (is_data_inode(inode)) { |
c3a3b19b | 1221 | error_bitmap = btrfs_verify_data_csum(bbio, |
5e295768 | 1222 | bio_offset, page, start, end); |
97861cd1 CH |
1223 | if (error_bitmap) |
1224 | uptodate = false; | |
150e4b05 | 1225 | } else { |
97861cd1 CH |
1226 | if (btrfs_validate_metadata_buffer(bbio, |
1227 | page, start, end, mirror)) | |
1228 | uptodate = false; | |
150e4b05 | 1229 | } |
d1310b2e | 1230 | } |
ea466794 | 1231 | |
883d0de4 | 1232 | if (likely(uptodate)) { |
a71754fc | 1233 | loff_t i_size = i_size_read(inode); |
09cbfeaf | 1234 | pgoff_t end_index = i_size >> PAGE_SHIFT; |
a71754fc | 1235 | |
0d0a762c | 1236 | btrfs_clean_io_failure(BTRFS_I(inode), start, page, 0); |
97861cd1 | 1237 | |
c28ea613 QW |
1238 | /* |
1239 | * Zero out the remaining part if this range straddles | |
1240 | * i_size. | |
1241 | * | |
1242 | * Here we should only zero the range inside the bvec, | |
1243 | * not touch anything else. | |
1244 | * | |
1245 | * NOTE: i_size is exclusive while end is inclusive. | |
1246 | */ | |
1247 | if (page->index == end_index && i_size <= end) { | |
1248 | u32 zero_start = max(offset_in_page(i_size), | |
d2dcc8ed | 1249 | offset_in_page(start)); |
c28ea613 QW |
1250 | |
1251 | zero_user_segment(page, zero_start, | |
1252 | offset_in_page(end) + 1); | |
1253 | } | |
97861cd1 CH |
1254 | } else if (is_data_inode(inode)) { |
1255 | /* | |
1256 | * Only try to repair bios that actually made it to a | |
1257 | * device. If the bio failed to be submitted mirror | |
1258 | * is 0 and we need to fail it without retrying. | |
81bd9328 CH |
1259 | * |
1260 | * This also includes the high level bios for compressed | |
1261 | * extents - these never make it to a device and repair | |
1262 | * is already handled on the lower compressed bio. | |
97861cd1 CH |
1263 | */ |
1264 | if (mirror > 0) | |
1265 | repair = true; | |
1266 | } else { | |
1267 | struct extent_buffer *eb; | |
1268 | ||
1269 | eb = find_extent_buffer_readpage(fs_info, page, start); | |
1270 | set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); | |
1271 | eb->read_mirror = mirror; | |
1272 | atomic_dec(&eb->io_pages); | |
70dec807 | 1273 | } |
97861cd1 CH |
1274 | |
1275 | if (repair) { | |
1276 | /* | |
1277 | * submit_data_read_repair() will handle all the good | |
1278 | * and bad sectors, we just continue to the next bvec. | |
1279 | */ | |
7aa51232 CH |
1280 | submit_data_read_repair(inode, bbio, bio_offset, bvec, |
1281 | error_bitmap); | |
97861cd1 CH |
1282 | } else { |
1283 | /* Update page status and unlock */ | |
1284 | end_page_read(page, uptodate, start, len); | |
1285 | endio_readpage_release_extent(&processed, BTRFS_I(inode), | |
1286 | start, end, PageUptodate(page)); | |
70dec807 | 1287 | } |
97861cd1 | 1288 | |
7ffd27e3 QW |
1289 | ASSERT(bio_offset + len > bio_offset); |
1290 | bio_offset += len; | |
883d0de4 | 1291 | |
2c30c71b | 1292 | } |
94e8c95c QW |
1293 | /* Release the last extent */ |
1294 | endio_readpage_release_extent(&processed, NULL, 0, 0, false); | |
c3a3b19b | 1295 | btrfs_bio_free_csum(bbio); |
d1310b2e | 1296 | bio_put(bio); |
d1310b2e CM |
1297 | } |
1298 | ||
dd137dd1 STD |
1299 | /** |
1300 | * Populate every free slot in a provided array with pages. | |
1301 | * | |
1302 | * @nr_pages: number of pages to allocate | |
1303 | * @page_array: the array to fill with pages; any existing non-null entries in | |
1304 | * the array will be skipped | |
1305 | * | |
1306 | * Return: 0 if all pages were able to be allocated; | |
1307 | * -ENOMEM otherwise, and the caller is responsible for freeing all | |
1308 | * non-null page pointers in the array. | |
1309 | */ | |
1310 | int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array) | |
1311 | { | |
91d6ac1d | 1312 | unsigned int allocated; |
dd137dd1 | 1313 | |
91d6ac1d STD |
1314 | for (allocated = 0; allocated < nr_pages;) { |
1315 | unsigned int last = allocated; | |
dd137dd1 | 1316 | |
91d6ac1d STD |
1317 | allocated = alloc_pages_bulk_array(GFP_NOFS, nr_pages, page_array); |
1318 | ||
395cb57e STD |
1319 | if (allocated == nr_pages) |
1320 | return 0; | |
1321 | ||
91d6ac1d STD |
1322 | /* |
1323 | * During this iteration, no page could be allocated, even | |
1324 | * though alloc_pages_bulk_array() falls back to alloc_page() | |
1325 | * if it could not bulk-allocate. So we must be out of memory. | |
1326 | */ | |
1327 | if (allocated == last) | |
dd137dd1 | 1328 | return -ENOMEM; |
395cb57e STD |
1329 | |
1330 | memalloc_retry_wait(GFP_NOFS); | |
dd137dd1 STD |
1331 | } |
1332 | return 0; | |
1333 | } | |
1334 | ||
953651eb NA |
1335 | /** |
1336 | * Attempt to add a page to bio | |
1337 | * | |
be8d1a2a | 1338 | * @bio_ctrl: record both the bio, and its bio_flags |
953651eb NA |
1339 | * @page: page to add to the bio |
1340 | * @disk_bytenr: offset of the new bio or to check whether we are adding | |
1341 | * a contiguous page to the previous one | |
953651eb | 1342 | * @size: portion of page that we want to write |
be8d1a2a | 1343 | * @pg_offset: starting offset in the page |
cb3a12d9 | 1344 | * @compress_type: compression type of the current bio to see if we can merge them |
953651eb NA |
1345 | * |
1346 | * Attempt to add a page to bio considering stripe alignment etc. | |
1347 | * | |
e0eefe07 QW |
1348 | * Return >= 0 for the number of bytes added to the bio. |
1349 | * Can return 0 if the current bio is already at stripe/zone boundary. | |
1350 | * Return <0 for error. | |
953651eb | 1351 | */ |
e0eefe07 QW |
1352 | static int btrfs_bio_add_page(struct btrfs_bio_ctrl *bio_ctrl, |
1353 | struct page *page, | |
1354 | u64 disk_bytenr, unsigned int size, | |
1355 | unsigned int pg_offset, | |
cb3a12d9 | 1356 | enum btrfs_compression_type compress_type) |
953651eb | 1357 | { |
390ed29b QW |
1358 | struct bio *bio = bio_ctrl->bio; |
1359 | u32 bio_size = bio->bi_iter.bi_size; | |
e0eefe07 | 1360 | u32 real_size; |
953651eb | 1361 | const sector_t sector = disk_bytenr >> SECTOR_SHIFT; |
4a445b7b | 1362 | bool contig = false; |
e1326f03 | 1363 | int ret; |
953651eb | 1364 | |
390ed29b QW |
1365 | ASSERT(bio); |
1366 | /* The limit should be calculated when bio_ctrl->bio is allocated */ | |
1367 | ASSERT(bio_ctrl->len_to_oe_boundary && bio_ctrl->len_to_stripe_boundary); | |
0f07003b | 1368 | if (bio_ctrl->compress_type != compress_type) |
e0eefe07 | 1369 | return 0; |
953651eb | 1370 | |
4a445b7b QW |
1371 | |
1372 | if (bio->bi_iter.bi_size == 0) { | |
1373 | /* We can always add a page into an empty bio. */ | |
1374 | contig = true; | |
1375 | } else if (bio_ctrl->compress_type == BTRFS_COMPRESS_NONE) { | |
1376 | struct bio_vec *bvec = bio_last_bvec_all(bio); | |
1377 | ||
1378 | /* | |
1379 | * The contig check requires the following conditions to be met: | |
1380 | * 1) The pages are belonging to the same inode | |
1381 | * This is implied by the call chain. | |
1382 | * | |
1383 | * 2) The range has adjacent logical bytenr | |
1384 | * | |
1385 | * 3) The range has adjacent file offset | |
1386 | * This is required for the usage of btrfs_bio->file_offset. | |
1387 | */ | |
1388 | if (bio_end_sector(bio) == sector && | |
1389 | page_offset(bvec->bv_page) + bvec->bv_offset + | |
1390 | bvec->bv_len == page_offset(page) + pg_offset) | |
1391 | contig = true; | |
1392 | } else { | |
1393 | /* | |
1394 | * For compression, all IO should have its logical bytenr | |
1395 | * set to the starting bytenr of the compressed extent. | |
1396 | */ | |
953651eb | 1397 | contig = bio->bi_iter.bi_sector == sector; |
4a445b7b QW |
1398 | } |
1399 | ||
953651eb | 1400 | if (!contig) |
e0eefe07 | 1401 | return 0; |
953651eb | 1402 | |
e0eefe07 QW |
1403 | real_size = min(bio_ctrl->len_to_oe_boundary, |
1404 | bio_ctrl->len_to_stripe_boundary) - bio_size; | |
1405 | real_size = min(real_size, size); | |
1406 | ||
1407 | /* | |
1408 | * If real_size is 0, never call bio_add_*_page(), as even size is 0, | |
1409 | * bio will still execute its endio function on the page! | |
1410 | */ | |
1411 | if (real_size == 0) | |
1412 | return 0; | |
953651eb | 1413 | |
390ed29b | 1414 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) |
e0eefe07 | 1415 | ret = bio_add_zone_append_page(bio, page, real_size, pg_offset); |
390ed29b | 1416 | else |
e0eefe07 | 1417 | ret = bio_add_page(bio, page, real_size, pg_offset); |
e1326f03 | 1418 | |
e0eefe07 | 1419 | return ret; |
953651eb NA |
1420 | } |
1421 | ||
390ed29b | 1422 | static int calc_bio_boundaries(struct btrfs_bio_ctrl *bio_ctrl, |
939c7feb | 1423 | struct btrfs_inode *inode, u64 file_offset) |
390ed29b QW |
1424 | { |
1425 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
1426 | struct btrfs_io_geometry geom; | |
1427 | struct btrfs_ordered_extent *ordered; | |
1428 | struct extent_map *em; | |
1429 | u64 logical = (bio_ctrl->bio->bi_iter.bi_sector << SECTOR_SHIFT); | |
1430 | int ret; | |
1431 | ||
1432 | /* | |
1433 | * Pages for compressed extent are never submitted to disk directly, | |
1434 | * thus it has no real boundary, just set them to U32_MAX. | |
1435 | * | |
1436 | * The split happens for real compressed bio, which happens in | |
1437 | * btrfs_submit_compressed_read/write(). | |
1438 | */ | |
0f07003b | 1439 | if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) { |
390ed29b QW |
1440 | bio_ctrl->len_to_oe_boundary = U32_MAX; |
1441 | bio_ctrl->len_to_stripe_boundary = U32_MAX; | |
1442 | return 0; | |
1443 | } | |
1444 | em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize); | |
1445 | if (IS_ERR(em)) | |
1446 | return PTR_ERR(em); | |
1447 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio_ctrl->bio), | |
1448 | logical, &geom); | |
1449 | free_extent_map(em); | |
1450 | if (ret < 0) { | |
1451 | return ret; | |
1452 | } | |
1453 | if (geom.len > U32_MAX) | |
1454 | bio_ctrl->len_to_stripe_boundary = U32_MAX; | |
1455 | else | |
1456 | bio_ctrl->len_to_stripe_boundary = (u32)geom.len; | |
1457 | ||
73672710 | 1458 | if (bio_op(bio_ctrl->bio) != REQ_OP_ZONE_APPEND) { |
390ed29b QW |
1459 | bio_ctrl->len_to_oe_boundary = U32_MAX; |
1460 | return 0; | |
1461 | } | |
1462 | ||
390ed29b | 1463 | /* Ordered extent not yet created, so we're good */ |
939c7feb | 1464 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
390ed29b QW |
1465 | if (!ordered) { |
1466 | bio_ctrl->len_to_oe_boundary = U32_MAX; | |
1467 | return 0; | |
1468 | } | |
1469 | ||
1470 | bio_ctrl->len_to_oe_boundary = min_t(u32, U32_MAX, | |
1471 | ordered->disk_bytenr + ordered->disk_num_bytes - logical); | |
1472 | btrfs_put_ordered_extent(ordered); | |
1473 | return 0; | |
1474 | } | |
1475 | ||
e0eefe07 QW |
1476 | static int alloc_new_bio(struct btrfs_inode *inode, |
1477 | struct btrfs_bio_ctrl *bio_ctrl, | |
1478 | struct writeback_control *wbc, | |
bf9486d6 | 1479 | blk_opf_t opf, |
939c7feb | 1480 | u64 disk_bytenr, u32 offset, u64 file_offset, |
cb3a12d9 | 1481 | enum btrfs_compression_type compress_type) |
e0eefe07 QW |
1482 | { |
1483 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
1484 | struct bio *bio; | |
1485 | int ret; | |
1486 | ||
5467abba QW |
1487 | ASSERT(bio_ctrl->end_io_func); |
1488 | ||
1489 | bio = btrfs_bio_alloc(BIO_MAX_VECS, opf, bio_ctrl->end_io_func, NULL); | |
e0eefe07 QW |
1490 | /* |
1491 | * For compressed page range, its disk_bytenr is always @disk_bytenr | |
1492 | * passed in, no matter if we have added any range into previous bio. | |
1493 | */ | |
cb3a12d9 | 1494 | if (compress_type != BTRFS_COMPRESS_NONE) |
cd8e0cca | 1495 | bio->bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; |
e0eefe07 | 1496 | else |
cd8e0cca | 1497 | bio->bi_iter.bi_sector = (disk_bytenr + offset) >> SECTOR_SHIFT; |
e0eefe07 | 1498 | bio_ctrl->bio = bio; |
0f07003b | 1499 | bio_ctrl->compress_type = compress_type; |
939c7feb NA |
1500 | ret = calc_bio_boundaries(bio_ctrl, inode, file_offset); |
1501 | if (ret < 0) | |
1502 | goto error; | |
e0eefe07 | 1503 | |
50f1cff3 CH |
1504 | if (wbc) { |
1505 | /* | |
1506 | * For Zone append we need the correct block_device that we are | |
1507 | * going to write to set in the bio to be able to respect the | |
1508 | * hardware limitation. Look it up here: | |
1509 | */ | |
1510 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { | |
1511 | struct btrfs_device *dev; | |
1512 | ||
1513 | dev = btrfs_zoned_get_device(fs_info, disk_bytenr, | |
1514 | fs_info->sectorsize); | |
1515 | if (IS_ERR(dev)) { | |
1516 | ret = PTR_ERR(dev); | |
1517 | goto error; | |
1518 | } | |
e0eefe07 | 1519 | |
50f1cff3 CH |
1520 | bio_set_dev(bio, dev->bdev); |
1521 | } else { | |
1522 | /* | |
1523 | * Otherwise pick the last added device to support | |
1524 | * cgroup writeback. For multi-device file systems this | |
1525 | * means blk-cgroup policies have to always be set on the | |
1526 | * last added/replaced device. This is a bit odd but has | |
1527 | * been like that for a long time. | |
1528 | */ | |
1529 | bio_set_dev(bio, fs_info->fs_devices->latest_dev->bdev); | |
e0eefe07 | 1530 | } |
50f1cff3 CH |
1531 | wbc_init_bio(wbc, bio); |
1532 | } else { | |
1533 | ASSERT(bio_op(bio) != REQ_OP_ZONE_APPEND); | |
e0eefe07 QW |
1534 | } |
1535 | return 0; | |
1536 | error: | |
1537 | bio_ctrl->bio = NULL; | |
917f32a2 | 1538 | btrfs_bio_end_io(btrfs_bio(bio), errno_to_blk_status(ret)); |
e0eefe07 QW |
1539 | return ret; |
1540 | } | |
1541 | ||
4b81ba48 DS |
1542 | /* |
1543 | * @opf: bio REQ_OP_* and REQ_* flags as one value | |
b8b3d625 | 1544 | * @wbc: optional writeback control for io accounting |
0c64c33c | 1545 | * @disk_bytenr: logical bytenr where the write will be |
209ecde5 | 1546 | * @page: page to add to the bio |
0c64c33c | 1547 | * @size: portion of page that we want to write to |
b8b3d625 DS |
1548 | * @pg_offset: offset of the new bio or to check whether we are adding |
1549 | * a contiguous page to the previous one | |
cb3a12d9 | 1550 | * @compress_type: compress type for current bio |
814b6f91 QW |
1551 | * |
1552 | * The will either add the page into the existing @bio_ctrl->bio, or allocate a | |
1553 | * new one in @bio_ctrl->bio. | |
1554 | * The mirror number for this IO should already be initizlied in | |
1555 | * @bio_ctrl->mirror_num. | |
4b81ba48 | 1556 | */ |
bf9486d6 | 1557 | static int submit_extent_page(blk_opf_t opf, |
da2f0f74 | 1558 | struct writeback_control *wbc, |
390ed29b | 1559 | struct btrfs_bio_ctrl *bio_ctrl, |
209ecde5 | 1560 | u64 disk_bytenr, struct page *page, |
6c5a4e2c | 1561 | size_t size, unsigned long pg_offset, |
cb3a12d9 | 1562 | enum btrfs_compression_type compress_type, |
005efedf | 1563 | bool force_bio_submit) |
d1310b2e CM |
1564 | { |
1565 | int ret = 0; | |
e1326f03 | 1566 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
e0eefe07 | 1567 | unsigned int cur = pg_offset; |
d1310b2e | 1568 | |
390ed29b | 1569 | ASSERT(bio_ctrl); |
5c2b1fd7 | 1570 | |
390ed29b QW |
1571 | ASSERT(pg_offset < PAGE_SIZE && size <= PAGE_SIZE && |
1572 | pg_offset + size <= PAGE_SIZE); | |
5467abba QW |
1573 | |
1574 | ASSERT(bio_ctrl->end_io_func); | |
1575 | ||
722c82ac CH |
1576 | if (force_bio_submit) |
1577 | submit_one_bio(bio_ctrl); | |
e0eefe07 QW |
1578 | |
1579 | while (cur < pg_offset + size) { | |
1580 | u32 offset = cur - pg_offset; | |
1581 | int added; | |
1582 | ||
1583 | /* Allocate new bio if needed */ | |
1584 | if (!bio_ctrl->bio) { | |
1585 | ret = alloc_new_bio(inode, bio_ctrl, wbc, opf, | |
5467abba | 1586 | disk_bytenr, offset, |
939c7feb | 1587 | page_offset(page) + cur, |
cb3a12d9 | 1588 | compress_type); |
e0eefe07 QW |
1589 | if (ret < 0) |
1590 | return ret; | |
1591 | } | |
1592 | /* | |
1593 | * We must go through btrfs_bio_add_page() to ensure each | |
1594 | * page range won't cross various boundaries. | |
1595 | */ | |
cb3a12d9 | 1596 | if (compress_type != BTRFS_COMPRESS_NONE) |
e0eefe07 QW |
1597 | added = btrfs_bio_add_page(bio_ctrl, page, disk_bytenr, |
1598 | size - offset, pg_offset + offset, | |
cb3a12d9 | 1599 | compress_type); |
e0eefe07 QW |
1600 | else |
1601 | added = btrfs_bio_add_page(bio_ctrl, page, | |
1602 | disk_bytenr + offset, size - offset, | |
cb3a12d9 | 1603 | pg_offset + offset, compress_type); |
e0eefe07 QW |
1604 | |
1605 | /* Metadata page range should never be split */ | |
1606 | if (!is_data_inode(&inode->vfs_inode)) | |
1607 | ASSERT(added == 0 || added == size - offset); | |
1608 | ||
1609 | /* At least we added some page, update the account */ | |
1610 | if (wbc && added) | |
1611 | wbc_account_cgroup_owner(wbc, page, added); | |
1612 | ||
1613 | /* We have reached boundary, submit right now */ | |
1614 | if (added < size - offset) { | |
1615 | /* The bio should contain some page(s) */ | |
1616 | ASSERT(bio_ctrl->bio->bi_iter.bi_size); | |
722c82ac | 1617 | submit_one_bio(bio_ctrl); |
d1310b2e | 1618 | } |
e0eefe07 | 1619 | cur += added; |
d1310b2e | 1620 | } |
e0eefe07 | 1621 | return 0; |
d1310b2e CM |
1622 | } |
1623 | ||
760f991f QW |
1624 | static int attach_extent_buffer_page(struct extent_buffer *eb, |
1625 | struct page *page, | |
1626 | struct btrfs_subpage *prealloc) | |
d1310b2e | 1627 | { |
760f991f QW |
1628 | struct btrfs_fs_info *fs_info = eb->fs_info; |
1629 | int ret = 0; | |
1630 | ||
0d01e247 QW |
1631 | /* |
1632 | * If the page is mapped to btree inode, we should hold the private | |
1633 | * lock to prevent race. | |
1634 | * For cloned or dummy extent buffers, their pages are not mapped and | |
1635 | * will not race with any other ebs. | |
1636 | */ | |
1637 | if (page->mapping) | |
1638 | lockdep_assert_held(&page->mapping->private_lock); | |
1639 | ||
fbca46eb | 1640 | if (fs_info->nodesize >= PAGE_SIZE) { |
760f991f QW |
1641 | if (!PagePrivate(page)) |
1642 | attach_page_private(page, eb); | |
1643 | else | |
1644 | WARN_ON(page->private != (unsigned long)eb); | |
1645 | return 0; | |
1646 | } | |
1647 | ||
1648 | /* Already mapped, just free prealloc */ | |
1649 | if (PagePrivate(page)) { | |
1650 | btrfs_free_subpage(prealloc); | |
1651 | return 0; | |
1652 | } | |
1653 | ||
1654 | if (prealloc) | |
1655 | /* Has preallocated memory for subpage */ | |
1656 | attach_page_private(page, prealloc); | |
d1b89bc0 | 1657 | else |
760f991f QW |
1658 | /* Do new allocation to attach subpage */ |
1659 | ret = btrfs_attach_subpage(fs_info, page, | |
1660 | BTRFS_SUBPAGE_METADATA); | |
1661 | return ret; | |
d1310b2e CM |
1662 | } |
1663 | ||
32443de3 | 1664 | int set_page_extent_mapped(struct page *page) |
d1310b2e | 1665 | { |
32443de3 QW |
1666 | struct btrfs_fs_info *fs_info; |
1667 | ||
1668 | ASSERT(page->mapping); | |
1669 | ||
1670 | if (PagePrivate(page)) | |
1671 | return 0; | |
1672 | ||
1673 | fs_info = btrfs_sb(page->mapping->host->i_sb); | |
1674 | ||
fbca46eb | 1675 | if (btrfs_is_subpage(fs_info, page)) |
32443de3 QW |
1676 | return btrfs_attach_subpage(fs_info, page, BTRFS_SUBPAGE_DATA); |
1677 | ||
1678 | attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE); | |
1679 | return 0; | |
1680 | } | |
1681 | ||
1682 | void clear_page_extent_mapped(struct page *page) | |
1683 | { | |
1684 | struct btrfs_fs_info *fs_info; | |
1685 | ||
1686 | ASSERT(page->mapping); | |
1687 | ||
d1b89bc0 | 1688 | if (!PagePrivate(page)) |
32443de3 QW |
1689 | return; |
1690 | ||
1691 | fs_info = btrfs_sb(page->mapping->host->i_sb); | |
fbca46eb | 1692 | if (btrfs_is_subpage(fs_info, page)) |
32443de3 QW |
1693 | return btrfs_detach_subpage(fs_info, page); |
1694 | ||
1695 | detach_page_private(page); | |
d1310b2e CM |
1696 | } |
1697 | ||
125bac01 MX |
1698 | static struct extent_map * |
1699 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, | |
1a5ee1e6 | 1700 | u64 start, u64 len, struct extent_map **em_cached) |
125bac01 MX |
1701 | { |
1702 | struct extent_map *em; | |
1703 | ||
1704 | if (em_cached && *em_cached) { | |
1705 | em = *em_cached; | |
cbc0e928 | 1706 | if (extent_map_in_tree(em) && start >= em->start && |
125bac01 | 1707 | start < extent_map_end(em)) { |
490b54d6 | 1708 | refcount_inc(&em->refs); |
125bac01 MX |
1709 | return em; |
1710 | } | |
1711 | ||
1712 | free_extent_map(em); | |
1713 | *em_cached = NULL; | |
1714 | } | |
1715 | ||
1a5ee1e6 | 1716 | em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, start, len); |
c0347550 | 1717 | if (em_cached && !IS_ERR(em)) { |
125bac01 | 1718 | BUG_ON(*em_cached); |
490b54d6 | 1719 | refcount_inc(&em->refs); |
125bac01 MX |
1720 | *em_cached = em; |
1721 | } | |
1722 | return em; | |
1723 | } | |
d1310b2e CM |
1724 | /* |
1725 | * basic readpage implementation. Locked extent state structs are inserted | |
1726 | * into the tree that are removed when the IO is done (by the end_io | |
1727 | * handlers) | |
79787eaa | 1728 | * XXX JDM: This needs looking at to ensure proper page locking |
baf863b9 | 1729 | * return 0 on success, otherwise return error |
d1310b2e | 1730 | */ |
7aab8b32 | 1731 | static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, |
390ed29b | 1732 | struct btrfs_bio_ctrl *bio_ctrl, |
bf9486d6 | 1733 | blk_opf_t read_flags, u64 *prev_em_start) |
d1310b2e CM |
1734 | { |
1735 | struct inode *inode = page->mapping->host; | |
92082d40 | 1736 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4eee4fa4 | 1737 | u64 start = page_offset(page); |
8eec8296 | 1738 | const u64 end = start + PAGE_SIZE - 1; |
d1310b2e CM |
1739 | u64 cur = start; |
1740 | u64 extent_offset; | |
1741 | u64 last_byte = i_size_read(inode); | |
1742 | u64 block_start; | |
d1310b2e | 1743 | struct extent_map *em; |
baf863b9 | 1744 | int ret = 0; |
306e16ce | 1745 | size_t pg_offset = 0; |
d1310b2e CM |
1746 | size_t iosize; |
1747 | size_t blocksize = inode->i_sb->s_blocksize; | |
f657a31c | 1748 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
ae6957eb | 1749 | |
32443de3 QW |
1750 | ret = set_page_extent_mapped(page); |
1751 | if (ret < 0) { | |
570eb97b | 1752 | unlock_extent(tree, start, end, NULL); |
92082d40 QW |
1753 | btrfs_page_set_error(fs_info, page, start, PAGE_SIZE); |
1754 | unlock_page(page); | |
32443de3 QW |
1755 | goto out; |
1756 | } | |
d1310b2e | 1757 | |
09cbfeaf | 1758 | if (page->index == last_byte >> PAGE_SHIFT) { |
7073017a | 1759 | size_t zero_offset = offset_in_page(last_byte); |
c8b97818 CM |
1760 | |
1761 | if (zero_offset) { | |
09cbfeaf | 1762 | iosize = PAGE_SIZE - zero_offset; |
d048b9c2 | 1763 | memzero_page(page, zero_offset, iosize); |
c8b97818 CM |
1764 | } |
1765 | } | |
5467abba | 1766 | bio_ctrl->end_io_func = end_bio_extent_readpage; |
92082d40 | 1767 | begin_page_read(fs_info, page); |
d1310b2e | 1768 | while (cur <= end) { |
4c37a793 | 1769 | unsigned long this_bio_flag = 0; |
005efedf | 1770 | bool force_bio_submit = false; |
0c64c33c | 1771 | u64 disk_bytenr; |
c8f2f24b | 1772 | |
6a404910 | 1773 | ASSERT(IS_ALIGNED(cur, fs_info->sectorsize)); |
d1310b2e | 1774 | if (cur >= last_byte) { |
507903b8 AJ |
1775 | struct extent_state *cached = NULL; |
1776 | ||
09cbfeaf | 1777 | iosize = PAGE_SIZE - pg_offset; |
d048b9c2 | 1778 | memzero_page(page, pg_offset, iosize); |
d1310b2e | 1779 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
507903b8 | 1780 | &cached, GFP_NOFS); |
570eb97b | 1781 | unlock_extent(tree, cur, cur + iosize - 1, &cached); |
92082d40 | 1782 | end_page_read(page, true, cur, iosize); |
d1310b2e CM |
1783 | break; |
1784 | } | |
125bac01 | 1785 | em = __get_extent_map(inode, page, pg_offset, cur, |
1a5ee1e6 | 1786 | end - cur + 1, em_cached); |
c0347550 | 1787 | if (IS_ERR(em)) { |
570eb97b | 1788 | unlock_extent(tree, cur, end, NULL); |
92082d40 | 1789 | end_page_read(page, false, cur, end + 1 - cur); |
bbf0ea7e | 1790 | ret = PTR_ERR(em); |
d1310b2e CM |
1791 | break; |
1792 | } | |
d1310b2e CM |
1793 | extent_offset = cur - em->start; |
1794 | BUG_ON(extent_map_end(em) <= cur); | |
1795 | BUG_ON(end < cur); | |
1796 | ||
7f6ca7f2 DS |
1797 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
1798 | this_bio_flag = em->compress_type; | |
c8b97818 | 1799 | |
d1310b2e | 1800 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
fda2832f | 1801 | iosize = ALIGN(iosize, blocksize); |
2a5232a8 | 1802 | if (this_bio_flag != BTRFS_COMPRESS_NONE) |
0c64c33c | 1803 | disk_bytenr = em->block_start; |
949b3273 | 1804 | else |
0c64c33c | 1805 | disk_bytenr = em->block_start + extent_offset; |
d1310b2e | 1806 | block_start = em->block_start; |
d899e052 YZ |
1807 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
1808 | block_start = EXTENT_MAP_HOLE; | |
005efedf FM |
1809 | |
1810 | /* | |
1811 | * If we have a file range that points to a compressed extent | |
260db43c | 1812 | * and it's followed by a consecutive file range that points |
005efedf FM |
1813 | * to the same compressed extent (possibly with a different |
1814 | * offset and/or length, so it either points to the whole extent | |
1815 | * or only part of it), we must make sure we do not submit a | |
1816 | * single bio to populate the pages for the 2 ranges because | |
1817 | * this makes the compressed extent read zero out the pages | |
1818 | * belonging to the 2nd range. Imagine the following scenario: | |
1819 | * | |
1820 | * File layout | |
1821 | * [0 - 8K] [8K - 24K] | |
1822 | * | | | |
1823 | * | | | |
1824 | * points to extent X, points to extent X, | |
1825 | * offset 4K, length of 8K offset 0, length 16K | |
1826 | * | |
1827 | * [extent X, compressed length = 4K uncompressed length = 16K] | |
1828 | * | |
1829 | * If the bio to read the compressed extent covers both ranges, | |
1830 | * it will decompress extent X into the pages belonging to the | |
1831 | * first range and then it will stop, zeroing out the remaining | |
1832 | * pages that belong to the other range that points to extent X. | |
1833 | * So here we make sure we submit 2 bios, one for the first | |
1834 | * range and another one for the third range. Both will target | |
1835 | * the same physical extent from disk, but we can't currently | |
1836 | * make the compressed bio endio callback populate the pages | |
1837 | * for both ranges because each compressed bio is tightly | |
1838 | * coupled with a single extent map, and each range can have | |
1839 | * an extent map with a different offset value relative to the | |
1840 | * uncompressed data of our extent and different lengths. This | |
1841 | * is a corner case so we prioritize correctness over | |
1842 | * non-optimal behavior (submitting 2 bios for the same extent). | |
1843 | */ | |
1844 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && | |
1845 | prev_em_start && *prev_em_start != (u64)-1 && | |
8e928218 | 1846 | *prev_em_start != em->start) |
005efedf FM |
1847 | force_bio_submit = true; |
1848 | ||
1849 | if (prev_em_start) | |
8e928218 | 1850 | *prev_em_start = em->start; |
005efedf | 1851 | |
d1310b2e CM |
1852 | free_extent_map(em); |
1853 | em = NULL; | |
1854 | ||
1855 | /* we've found a hole, just zero and go on */ | |
1856 | if (block_start == EXTENT_MAP_HOLE) { | |
507903b8 AJ |
1857 | struct extent_state *cached = NULL; |
1858 | ||
d048b9c2 | 1859 | memzero_page(page, pg_offset, iosize); |
d1310b2e CM |
1860 | |
1861 | set_extent_uptodate(tree, cur, cur + iosize - 1, | |
507903b8 | 1862 | &cached, GFP_NOFS); |
570eb97b | 1863 | unlock_extent(tree, cur, cur + iosize - 1, &cached); |
92082d40 | 1864 | end_page_read(page, true, cur, iosize); |
d1310b2e | 1865 | cur = cur + iosize; |
306e16ce | 1866 | pg_offset += iosize; |
d1310b2e CM |
1867 | continue; |
1868 | } | |
1869 | /* the get_extent function already copied into the page */ | |
70dec807 | 1870 | if (block_start == EXTENT_MAP_INLINE) { |
570eb97b | 1871 | unlock_extent(tree, cur, cur + iosize - 1, NULL); |
52b029f4 | 1872 | end_page_read(page, true, cur, iosize); |
70dec807 | 1873 | cur = cur + iosize; |
306e16ce | 1874 | pg_offset += iosize; |
70dec807 CM |
1875 | continue; |
1876 | } | |
d1310b2e | 1877 | |
0ceb34bf | 1878 | ret = submit_extent_page(REQ_OP_READ | read_flags, NULL, |
209ecde5 | 1879 | bio_ctrl, disk_bytenr, page, iosize, |
5467abba QW |
1880 | pg_offset, this_bio_flag, |
1881 | force_bio_submit); | |
ad3fc794 | 1882 | if (ret) { |
10f7f6f8 QW |
1883 | /* |
1884 | * We have to unlock the remaining range, or the page | |
1885 | * will never be unlocked. | |
1886 | */ | |
570eb97b | 1887 | unlock_extent(tree, cur, end, NULL); |
10f7f6f8 | 1888 | end_page_read(page, false, cur, end + 1 - cur); |
baf863b9 | 1889 | goto out; |
edd33c99 | 1890 | } |
d1310b2e | 1891 | cur = cur + iosize; |
306e16ce | 1892 | pg_offset += iosize; |
d1310b2e | 1893 | } |
90a887c9 | 1894 | out: |
baf863b9 | 1895 | return ret; |
d1310b2e CM |
1896 | } |
1897 | ||
fdaf9a58 | 1898 | int btrfs_read_folio(struct file *file, struct folio *folio) |
7aab8b32 | 1899 | { |
fdaf9a58 | 1900 | struct page *page = &folio->page; |
7aab8b32 CH |
1901 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
1902 | u64 start = page_offset(page); | |
1903 | u64 end = start + PAGE_SIZE - 1; | |
1904 | struct btrfs_bio_ctrl bio_ctrl = { 0 }; | |
1905 | int ret; | |
1906 | ||
1907 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); | |
1908 | ||
1909 | ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL); | |
1910 | /* | |
1911 | * If btrfs_do_readpage() failed we will want to submit the assembled | |
1912 | * bio to do the cleanup. | |
1913 | */ | |
722c82ac | 1914 | submit_one_bio(&bio_ctrl); |
7aab8b32 CH |
1915 | return ret; |
1916 | } | |
1917 | ||
b6660e80 | 1918 | static inline void contiguous_readpages(struct page *pages[], int nr_pages, |
390ed29b QW |
1919 | u64 start, u64 end, |
1920 | struct extent_map **em_cached, | |
1921 | struct btrfs_bio_ctrl *bio_ctrl, | |
1922 | u64 *prev_em_start) | |
9974090b | 1923 | { |
23d31bd4 | 1924 | struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host); |
9974090b MX |
1925 | int index; |
1926 | ||
b272ae22 | 1927 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
9974090b MX |
1928 | |
1929 | for (index = 0; index < nr_pages; index++) { | |
390ed29b | 1930 | btrfs_do_readpage(pages[index], em_cached, bio_ctrl, |
0f208812 | 1931 | REQ_RAHEAD, prev_em_start); |
09cbfeaf | 1932 | put_page(pages[index]); |
9974090b MX |
1933 | } |
1934 | } | |
1935 | ||
d1310b2e | 1936 | /* |
40f76580 CM |
1937 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
1938 | * | |
5eaad97a | 1939 | * This returns 1 if btrfs_run_delalloc_range function did all the work required |
40f76580 CM |
1940 | * to write the page (copy into inline extent). In this case the IO has |
1941 | * been started and the page is already unlocked. | |
1942 | * | |
1943 | * This returns 0 if all went well (page still locked) | |
1944 | * This returns < 0 if there were errors (page still locked) | |
d1310b2e | 1945 | */ |
cd4c0bf9 | 1946 | static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode, |
83f1b680 | 1947 | struct page *page, struct writeback_control *wbc) |
40f76580 | 1948 | { |
2749f7ef | 1949 | const u64 page_end = page_offset(page) + PAGE_SIZE - 1; |
cf3075fb | 1950 | u64 delalloc_start = page_offset(page); |
40f76580 | 1951 | u64 delalloc_to_write = 0; |
83f1b680 QW |
1952 | /* How many pages are started by btrfs_run_delalloc_range() */ |
1953 | unsigned long nr_written = 0; | |
40f76580 CM |
1954 | int ret; |
1955 | int page_started = 0; | |
1956 | ||
2749f7ef QW |
1957 | while (delalloc_start < page_end) { |
1958 | u64 delalloc_end = page_end; | |
1959 | bool found; | |
40f76580 | 1960 | |
cd4c0bf9 | 1961 | found = find_lock_delalloc_range(&inode->vfs_inode, page, |
40f76580 | 1962 | &delalloc_start, |
917aacec | 1963 | &delalloc_end); |
3522e903 | 1964 | if (!found) { |
40f76580 CM |
1965 | delalloc_start = delalloc_end + 1; |
1966 | continue; | |
1967 | } | |
cd4c0bf9 | 1968 | ret = btrfs_run_delalloc_range(inode, page, delalloc_start, |
83f1b680 | 1969 | delalloc_end, &page_started, &nr_written, wbc); |
40f76580 | 1970 | if (ret) { |
963e4db8 QW |
1971 | btrfs_page_set_error(inode->root->fs_info, page, |
1972 | page_offset(page), PAGE_SIZE); | |
7361b4ae | 1973 | return ret; |
40f76580 CM |
1974 | } |
1975 | /* | |
ea1754a0 KS |
1976 | * delalloc_end is already one less than the total length, so |
1977 | * we don't subtract one from PAGE_SIZE | |
40f76580 CM |
1978 | */ |
1979 | delalloc_to_write += (delalloc_end - delalloc_start + | |
ea1754a0 | 1980 | PAGE_SIZE) >> PAGE_SHIFT; |
40f76580 CM |
1981 | delalloc_start = delalloc_end + 1; |
1982 | } | |
1983 | if (wbc->nr_to_write < delalloc_to_write) { | |
1984 | int thresh = 8192; | |
1985 | ||
1986 | if (delalloc_to_write < thresh * 2) | |
1987 | thresh = delalloc_to_write; | |
1988 | wbc->nr_to_write = min_t(u64, delalloc_to_write, | |
1989 | thresh); | |
1990 | } | |
1991 | ||
83f1b680 | 1992 | /* Did btrfs_run_dealloc_range() already unlock and start the IO? */ |
40f76580 CM |
1993 | if (page_started) { |
1994 | /* | |
83f1b680 QW |
1995 | * We've unlocked the page, so we can't update the mapping's |
1996 | * writeback index, just update nr_to_write. | |
40f76580 | 1997 | */ |
83f1b680 | 1998 | wbc->nr_to_write -= nr_written; |
40f76580 CM |
1999 | return 1; |
2000 | } | |
2001 | ||
b69d1ee9 | 2002 | return 0; |
40f76580 CM |
2003 | } |
2004 | ||
c5ef5c6c QW |
2005 | /* |
2006 | * Find the first byte we need to write. | |
2007 | * | |
2008 | * For subpage, one page can contain several sectors, and | |
2009 | * __extent_writepage_io() will just grab all extent maps in the page | |
2010 | * range and try to submit all non-inline/non-compressed extents. | |
2011 | * | |
2012 | * This is a big problem for subpage, we shouldn't re-submit already written | |
2013 | * data at all. | |
2014 | * This function will lookup subpage dirty bit to find which range we really | |
2015 | * need to submit. | |
2016 | * | |
2017 | * Return the next dirty range in [@start, @end). | |
2018 | * If no dirty range is found, @start will be page_offset(page) + PAGE_SIZE. | |
2019 | */ | |
2020 | static void find_next_dirty_byte(struct btrfs_fs_info *fs_info, | |
2021 | struct page *page, u64 *start, u64 *end) | |
2022 | { | |
2023 | struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private; | |
72a69cd0 | 2024 | struct btrfs_subpage_info *spi = fs_info->subpage_info; |
c5ef5c6c QW |
2025 | u64 orig_start = *start; |
2026 | /* Declare as unsigned long so we can use bitmap ops */ | |
c5ef5c6c | 2027 | unsigned long flags; |
72a69cd0 | 2028 | int range_start_bit; |
c5ef5c6c QW |
2029 | int range_end_bit; |
2030 | ||
2031 | /* | |
2032 | * For regular sector size == page size case, since one page only | |
2033 | * contains one sector, we return the page offset directly. | |
2034 | */ | |
fbca46eb | 2035 | if (!btrfs_is_subpage(fs_info, page)) { |
c5ef5c6c QW |
2036 | *start = page_offset(page); |
2037 | *end = page_offset(page) + PAGE_SIZE; | |
2038 | return; | |
2039 | } | |
2040 | ||
72a69cd0 QW |
2041 | range_start_bit = spi->dirty_offset + |
2042 | (offset_in_page(orig_start) >> fs_info->sectorsize_bits); | |
2043 | ||
c5ef5c6c QW |
2044 | /* We should have the page locked, but just in case */ |
2045 | spin_lock_irqsave(&subpage->lock, flags); | |
72a69cd0 QW |
2046 | bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit, |
2047 | spi->dirty_offset + spi->bitmap_nr_bits); | |
c5ef5c6c QW |
2048 | spin_unlock_irqrestore(&subpage->lock, flags); |
2049 | ||
72a69cd0 QW |
2050 | range_start_bit -= spi->dirty_offset; |
2051 | range_end_bit -= spi->dirty_offset; | |
2052 | ||
c5ef5c6c QW |
2053 | *start = page_offset(page) + range_start_bit * fs_info->sectorsize; |
2054 | *end = page_offset(page) + range_end_bit * fs_info->sectorsize; | |
2055 | } | |
2056 | ||
40f76580 CM |
2057 | /* |
2058 | * helper for __extent_writepage. This calls the writepage start hooks, | |
2059 | * and does the loop to map the page into extents and bios. | |
2060 | * | |
2061 | * We return 1 if the IO is started and the page is unlocked, | |
2062 | * 0 if all went well (page still locked) | |
2063 | * < 0 if there were errors (page still locked) | |
2064 | */ | |
d4580fe2 | 2065 | static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, |
40f76580 CM |
2066 | struct page *page, |
2067 | struct writeback_control *wbc, | |
2068 | struct extent_page_data *epd, | |
2069 | loff_t i_size, | |
57e5ffeb | 2070 | int *nr_ret) |
d1310b2e | 2071 | { |
6bc5636a | 2072 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
a129ffb8 QW |
2073 | u64 cur = page_offset(page); |
2074 | u64 end = cur + PAGE_SIZE - 1; | |
d1310b2e | 2075 | u64 extent_offset; |
d1310b2e | 2076 | u64 block_start; |
d1310b2e | 2077 | struct extent_map *em; |
44e5801f | 2078 | int saved_ret = 0; |
40f76580 CM |
2079 | int ret = 0; |
2080 | int nr = 0; | |
bf9486d6 BVA |
2081 | enum req_op op = REQ_OP_WRITE; |
2082 | const blk_opf_t write_flags = wbc_to_write_flags(wbc); | |
44e5801f | 2083 | bool has_error = false; |
40f76580 | 2084 | bool compressed; |
c8b97818 | 2085 | |
a129ffb8 | 2086 | ret = btrfs_writepage_cow_fixup(page); |
d75855b4 NB |
2087 | if (ret) { |
2088 | /* Fixup worker will requeue */ | |
5ab58055 | 2089 | redirty_page_for_writepage(wbc, page); |
d75855b4 NB |
2090 | unlock_page(page); |
2091 | return 1; | |
247e743c CM |
2092 | } |
2093 | ||
11c8349b CM |
2094 | /* |
2095 | * we don't want to touch the inode after unlocking the page, | |
2096 | * so we update the mapping writeback index now | |
2097 | */ | |
572f3dad | 2098 | wbc->nr_to_write--; |
771ed689 | 2099 | |
5467abba | 2100 | epd->bio_ctrl.end_io_func = end_bio_extent_writepage; |
d1310b2e | 2101 | while (cur <= end) { |
0c64c33c | 2102 | u64 disk_bytenr; |
40f76580 | 2103 | u64 em_end; |
c5ef5c6c QW |
2104 | u64 dirty_range_start = cur; |
2105 | u64 dirty_range_end; | |
6bc5636a | 2106 | u32 iosize; |
58409edd | 2107 | |
40f76580 | 2108 | if (cur >= i_size) { |
38a39ac7 | 2109 | btrfs_writepage_endio_finish_ordered(inode, page, cur, |
25c1252a | 2110 | end, true); |
cc1d0d93 QW |
2111 | /* |
2112 | * This range is beyond i_size, thus we don't need to | |
2113 | * bother writing back. | |
2114 | * But we still need to clear the dirty subpage bit, or | |
2115 | * the next time the page gets dirtied, we will try to | |
2116 | * writeback the sectors with subpage dirty bits, | |
2117 | * causing writeback without ordered extent. | |
2118 | */ | |
2119 | btrfs_page_clear_dirty(fs_info, page, cur, end + 1 - cur); | |
d1310b2e CM |
2120 | break; |
2121 | } | |
c5ef5c6c QW |
2122 | |
2123 | find_next_dirty_byte(fs_info, page, &dirty_range_start, | |
2124 | &dirty_range_end); | |
2125 | if (cur < dirty_range_start) { | |
2126 | cur = dirty_range_start; | |
2127 | continue; | |
2128 | } | |
2129 | ||
d4580fe2 | 2130 | em = btrfs_get_extent(inode, NULL, 0, cur, end - cur + 1); |
c0347550 | 2131 | if (IS_ERR(em)) { |
c5ef5c6c | 2132 | btrfs_page_set_error(fs_info, page, cur, end - cur + 1); |
61391d56 | 2133 | ret = PTR_ERR_OR_ZERO(em); |
44e5801f QW |
2134 | has_error = true; |
2135 | if (!saved_ret) | |
2136 | saved_ret = ret; | |
d1310b2e CM |
2137 | break; |
2138 | } | |
2139 | ||
2140 | extent_offset = cur - em->start; | |
40f76580 | 2141 | em_end = extent_map_end(em); |
6bc5636a QW |
2142 | ASSERT(cur <= em_end); |
2143 | ASSERT(cur < end); | |
2144 | ASSERT(IS_ALIGNED(em->start, fs_info->sectorsize)); | |
2145 | ASSERT(IS_ALIGNED(em->len, fs_info->sectorsize)); | |
d1310b2e | 2146 | block_start = em->block_start; |
c8b97818 | 2147 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
6bc5636a QW |
2148 | disk_bytenr = em->block_start + extent_offset; |
2149 | ||
c5ef5c6c QW |
2150 | /* |
2151 | * Note that em_end from extent_map_end() and dirty_range_end from | |
2152 | * find_next_dirty_byte() are all exclusive | |
2153 | */ | |
2154 | iosize = min(min(em_end, end + 1), dirty_range_end) - cur; | |
d8e3fb10 | 2155 | |
e380adfc | 2156 | if (btrfs_use_zone_append(inode, em->block_start)) |
bf9486d6 | 2157 | op = REQ_OP_ZONE_APPEND; |
d8e3fb10 | 2158 | |
d1310b2e CM |
2159 | free_extent_map(em); |
2160 | em = NULL; | |
2161 | ||
c8b97818 CM |
2162 | /* |
2163 | * compressed and inline extents are written through other | |
2164 | * paths in the FS | |
2165 | */ | |
2166 | if (compressed || block_start == EXTENT_MAP_HOLE || | |
d1310b2e | 2167 | block_start == EXTENT_MAP_INLINE) { |
c8b04030 | 2168 | if (compressed) |
c8b97818 | 2169 | nr++; |
c8b04030 | 2170 | else |
38a39ac7 | 2171 | btrfs_writepage_endio_finish_ordered(inode, |
25c1252a | 2172 | page, cur, cur + iosize - 1, true); |
cc1d0d93 | 2173 | btrfs_page_clear_dirty(fs_info, page, cur, iosize); |
c8b97818 | 2174 | cur += iosize; |
d1310b2e CM |
2175 | continue; |
2176 | } | |
c8b97818 | 2177 | |
d2a91064 | 2178 | btrfs_set_range_writeback(inode, cur, cur + iosize - 1); |
58409edd | 2179 | if (!PageWriteback(page)) { |
d4580fe2 | 2180 | btrfs_err(inode->root->fs_info, |
58409edd DS |
2181 | "page %lu not writeback, cur %llu end %llu", |
2182 | page->index, cur, end); | |
d1310b2e | 2183 | } |
7f3c74fb | 2184 | |
c5ef5c6c QW |
2185 | /* |
2186 | * Although the PageDirty bit is cleared before entering this | |
2187 | * function, subpage dirty bit is not cleared. | |
2188 | * So clear subpage dirty bit here so next time we won't submit | |
2189 | * page for range already written to disk. | |
2190 | */ | |
2191 | btrfs_page_clear_dirty(fs_info, page, cur, iosize); | |
2192 | ||
bf9486d6 | 2193 | ret = submit_extent_page(op | write_flags, wbc, |
209ecde5 QW |
2194 | &epd->bio_ctrl, disk_bytenr, |
2195 | page, iosize, | |
390ed29b | 2196 | cur - page_offset(page), |
722c82ac | 2197 | 0, false); |
fe01aa65 | 2198 | if (ret) { |
44e5801f QW |
2199 | has_error = true; |
2200 | if (!saved_ret) | |
2201 | saved_ret = ret; | |
2202 | ||
c5ef5c6c | 2203 | btrfs_page_set_error(fs_info, page, cur, iosize); |
fe01aa65 | 2204 | if (PageWriteback(page)) |
c5ef5c6c QW |
2205 | btrfs_page_clear_writeback(fs_info, page, cur, |
2206 | iosize); | |
fe01aa65 | 2207 | } |
d1310b2e | 2208 | |
6bc5636a | 2209 | cur += iosize; |
d1310b2e CM |
2210 | nr++; |
2211 | } | |
cc1d0d93 QW |
2212 | /* |
2213 | * If we finish without problem, we should not only clear page dirty, | |
2214 | * but also empty subpage dirty bits | |
2215 | */ | |
44e5801f | 2216 | if (!has_error) |
cc1d0d93 | 2217 | btrfs_page_assert_not_dirty(fs_info, page); |
44e5801f QW |
2218 | else |
2219 | ret = saved_ret; | |
40f76580 | 2220 | *nr_ret = nr; |
40f76580 CM |
2221 | return ret; |
2222 | } | |
2223 | ||
2224 | /* | |
2225 | * the writepage semantics are similar to regular writepage. extent | |
2226 | * records are inserted to lock ranges in the tree, and as dirty areas | |
2227 | * are found, they are marked writeback. Then the lock bits are removed | |
2228 | * and the end_io handler clears the writeback ranges | |
3065976b QW |
2229 | * |
2230 | * Return 0 if everything goes well. | |
2231 | * Return <0 for error. | |
40f76580 CM |
2232 | */ |
2233 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, | |
aab6e9ed | 2234 | struct extent_page_data *epd) |
40f76580 | 2235 | { |
8e1dec8e | 2236 | struct folio *folio = page_folio(page); |
40f76580 | 2237 | struct inode *inode = page->mapping->host; |
e55a0de1 | 2238 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
cf3075fb QW |
2239 | const u64 page_start = page_offset(page); |
2240 | const u64 page_end = page_start + PAGE_SIZE - 1; | |
40f76580 CM |
2241 | int ret; |
2242 | int nr = 0; | |
eb70d222 | 2243 | size_t pg_offset; |
40f76580 | 2244 | loff_t i_size = i_size_read(inode); |
09cbfeaf | 2245 | unsigned long end_index = i_size >> PAGE_SHIFT; |
40f76580 | 2246 | |
40f76580 CM |
2247 | trace___extent_writepage(page, inode, wbc); |
2248 | ||
2249 | WARN_ON(!PageLocked(page)); | |
2250 | ||
963e4db8 QW |
2251 | btrfs_page_clear_error(btrfs_sb(inode->i_sb), page, |
2252 | page_offset(page), PAGE_SIZE); | |
40f76580 | 2253 | |
7073017a | 2254 | pg_offset = offset_in_page(i_size); |
40f76580 CM |
2255 | if (page->index > end_index || |
2256 | (page->index == end_index && !pg_offset)) { | |
8e1dec8e MWO |
2257 | folio_invalidate(folio, 0, folio_size(folio)); |
2258 | folio_unlock(folio); | |
40f76580 CM |
2259 | return 0; |
2260 | } | |
2261 | ||
21a8935e | 2262 | if (page->index == end_index) |
d048b9c2 | 2263 | memzero_page(page, pg_offset, PAGE_SIZE - pg_offset); |
40f76580 | 2264 | |
32443de3 QW |
2265 | ret = set_page_extent_mapped(page); |
2266 | if (ret < 0) { | |
2267 | SetPageError(page); | |
2268 | goto done; | |
2269 | } | |
40f76580 | 2270 | |
7789a55a | 2271 | if (!epd->extent_locked) { |
83f1b680 | 2272 | ret = writepage_delalloc(BTRFS_I(inode), page, wbc); |
7789a55a | 2273 | if (ret == 1) |
169d2c87 | 2274 | return 0; |
7789a55a NB |
2275 | if (ret) |
2276 | goto done; | |
2277 | } | |
40f76580 | 2278 | |
d4580fe2 | 2279 | ret = __extent_writepage_io(BTRFS_I(inode), page, wbc, epd, i_size, |
83f1b680 | 2280 | &nr); |
40f76580 | 2281 | if (ret == 1) |
169d2c87 | 2282 | return 0; |
40f76580 | 2283 | |
d1310b2e CM |
2284 | done: |
2285 | if (nr == 0) { | |
2286 | /* make sure the mapping tag for page dirty gets cleared */ | |
2287 | set_page_writeback(page); | |
2288 | end_page_writeback(page); | |
2289 | } | |
963e4db8 QW |
2290 | /* |
2291 | * Here we used to have a check for PageError() and then set @ret and | |
2292 | * call end_extent_writepage(). | |
2293 | * | |
2294 | * But in fact setting @ret here will cause different error paths | |
2295 | * between subpage and regular sectorsize. | |
2296 | * | |
2297 | * For regular page size, we never submit current page, but only add | |
2298 | * current page to current bio. | |
2299 | * The bio submission can only happen in next page. | |
2300 | * Thus if we hit the PageError() branch, @ret is already set to | |
2301 | * non-zero value and will not get updated for regular sectorsize. | |
2302 | * | |
2303 | * But for subpage case, it's possible we submit part of current page, | |
2304 | * thus can get PageError() set by submitted bio of the same page, | |
2305 | * while our @ret is still 0. | |
2306 | * | |
2307 | * So here we unify the behavior and don't set @ret. | |
2308 | * Error can still be properly passed to higher layer as page will | |
2309 | * be set error, here we just don't handle the IO failure. | |
2310 | * | |
2311 | * NOTE: This is just a hotfix for subpage. | |
2312 | * The root fix will be properly ending ordered extent when we hit | |
2313 | * an error during writeback. | |
2314 | * | |
2315 | * But that needs a bigger refactoring, as we not only need to grab the | |
2316 | * submitted OE, but also need to know exactly at which bytenr we hit | |
2317 | * the error. | |
2318 | * Currently the full page based __extent_writepage_io() is not | |
2319 | * capable of that. | |
2320 | */ | |
2321 | if (PageError(page)) | |
cf3075fb | 2322 | end_extent_writepage(page, ret, page_start, page_end); |
e55a0de1 QW |
2323 | if (epd->extent_locked) { |
2324 | /* | |
2325 | * If epd->extent_locked, it's from extent_write_locked_range(), | |
2326 | * the page can either be locked by lock_page() or | |
2327 | * process_one_page(). | |
2328 | * Let btrfs_page_unlock_writer() handle both cases. | |
2329 | */ | |
2330 | ASSERT(wbc); | |
2331 | btrfs_page_unlock_writer(fs_info, page, wbc->range_start, | |
2332 | wbc->range_end + 1 - wbc->range_start); | |
2333 | } else { | |
2334 | unlock_page(page); | |
2335 | } | |
3065976b | 2336 | ASSERT(ret <= 0); |
40f76580 | 2337 | return ret; |
d1310b2e CM |
2338 | } |
2339 | ||
fd8b2b61 | 2340 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
0b32f4bb | 2341 | { |
74316201 N |
2342 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
2343 | TASK_UNINTERRUPTIBLE); | |
0b32f4bb JB |
2344 | } |
2345 | ||
18dfa711 FM |
2346 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
2347 | { | |
2348 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); | |
2349 | smp_mb__after_atomic(); | |
2350 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); | |
2351 | } | |
2352 | ||
2e3c2513 | 2353 | /* |
a3efb2f0 | 2354 | * Lock extent buffer status and pages for writeback. |
2e3c2513 | 2355 | * |
a3efb2f0 QW |
2356 | * May try to flush write bio if we can't get the lock. |
2357 | * | |
2358 | * Return 0 if the extent buffer doesn't need to be submitted. | |
2359 | * (E.g. the extent buffer is not dirty) | |
2360 | * Return >0 is the extent buffer is submitted to bio. | |
2361 | * Return <0 if something went wrong, no page is locked. | |
2e3c2513 | 2362 | */ |
9df76fb5 | 2363 | static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb, |
0e378df1 | 2364 | struct extent_page_data *epd) |
0b32f4bb | 2365 | { |
9df76fb5 | 2366 | struct btrfs_fs_info *fs_info = eb->fs_info; |
c9583ada | 2367 | int i, num_pages; |
0b32f4bb JB |
2368 | int flush = 0; |
2369 | int ret = 0; | |
2370 | ||
2371 | if (!btrfs_try_tree_write_lock(eb)) { | |
9845e5dd | 2372 | submit_write_bio(epd, 0); |
2e3c2513 | 2373 | flush = 1; |
0b32f4bb JB |
2374 | btrfs_tree_lock(eb); |
2375 | } | |
2376 | ||
2377 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { | |
2378 | btrfs_tree_unlock(eb); | |
2379 | if (!epd->sync_io) | |
2380 | return 0; | |
2381 | if (!flush) { | |
9845e5dd | 2382 | submit_write_bio(epd, 0); |
0b32f4bb JB |
2383 | flush = 1; |
2384 | } | |
a098d8e8 CM |
2385 | while (1) { |
2386 | wait_on_extent_buffer_writeback(eb); | |
2387 | btrfs_tree_lock(eb); | |
2388 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) | |
2389 | break; | |
0b32f4bb | 2390 | btrfs_tree_unlock(eb); |
0b32f4bb JB |
2391 | } |
2392 | } | |
2393 | ||
51561ffe JB |
2394 | /* |
2395 | * We need to do this to prevent races in people who check if the eb is | |
2396 | * under IO since we can end up having no IO bits set for a short period | |
2397 | * of time. | |
2398 | */ | |
2399 | spin_lock(&eb->refs_lock); | |
0b32f4bb JB |
2400 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
2401 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); | |
51561ffe | 2402 | spin_unlock(&eb->refs_lock); |
0b32f4bb | 2403 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
104b4e51 NB |
2404 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
2405 | -eb->len, | |
2406 | fs_info->dirty_metadata_batch); | |
0b32f4bb | 2407 | ret = 1; |
51561ffe JB |
2408 | } else { |
2409 | spin_unlock(&eb->refs_lock); | |
0b32f4bb JB |
2410 | } |
2411 | ||
2412 | btrfs_tree_unlock(eb); | |
2413 | ||
f3156df9 QW |
2414 | /* |
2415 | * Either we don't need to submit any tree block, or we're submitting | |
2416 | * subpage eb. | |
2417 | * Subpage metadata doesn't use page locking at all, so we can skip | |
2418 | * the page locking. | |
2419 | */ | |
fbca46eb | 2420 | if (!ret || fs_info->nodesize < PAGE_SIZE) |
0b32f4bb JB |
2421 | return ret; |
2422 | ||
65ad0104 | 2423 | num_pages = num_extent_pages(eb); |
0b32f4bb | 2424 | for (i = 0; i < num_pages; i++) { |
fb85fc9a | 2425 | struct page *p = eb->pages[i]; |
0b32f4bb JB |
2426 | |
2427 | if (!trylock_page(p)) { | |
2428 | if (!flush) { | |
9845e5dd | 2429 | submit_write_bio(epd, 0); |
0b32f4bb JB |
2430 | flush = 1; |
2431 | } | |
2432 | lock_page(p); | |
2433 | } | |
2434 | } | |
2435 | ||
2e3c2513 | 2436 | return ret; |
0b32f4bb JB |
2437 | } |
2438 | ||
5a2c6075 | 2439 | static void set_btree_ioerr(struct page *page, struct extent_buffer *eb) |
656f30db | 2440 | { |
5a2c6075 | 2441 | struct btrfs_fs_info *fs_info = eb->fs_info; |
656f30db | 2442 | |
5a2c6075 | 2443 | btrfs_page_set_error(fs_info, page, eb->start, eb->len); |
656f30db FM |
2444 | if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
2445 | return; | |
2446 | ||
c2e39305 JB |
2447 | /* |
2448 | * A read may stumble upon this buffer later, make sure that it gets an | |
2449 | * error and knows there was an error. | |
2450 | */ | |
2451 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); | |
2452 | ||
68b85589 JB |
2453 | /* |
2454 | * We need to set the mapping with the io error as well because a write | |
2455 | * error will flip the file system readonly, and then syncfs() will | |
2456 | * return a 0 because we are readonly if we don't modify the err seq for | |
2457 | * the superblock. | |
2458 | */ | |
2459 | mapping_set_error(page->mapping, -EIO); | |
2460 | ||
eb5b64f1 DZ |
2461 | /* |
2462 | * If we error out, we should add back the dirty_metadata_bytes | |
2463 | * to make it consistent. | |
2464 | */ | |
eb5b64f1 DZ |
2465 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
2466 | eb->len, fs_info->dirty_metadata_batch); | |
2467 | ||
656f30db FM |
2468 | /* |
2469 | * If writeback for a btree extent that doesn't belong to a log tree | |
2470 | * failed, increment the counter transaction->eb_write_errors. | |
2471 | * We do this because while the transaction is running and before it's | |
2472 | * committing (when we call filemap_fdata[write|wait]_range against | |
2473 | * the btree inode), we might have | |
2474 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it | |
2475 | * returns an error or an error happens during writeback, when we're | |
2476 | * committing the transaction we wouldn't know about it, since the pages | |
2477 | * can be no longer dirty nor marked anymore for writeback (if a | |
2478 | * subsequent modification to the extent buffer didn't happen before the | |
2479 | * transaction commit), which makes filemap_fdata[write|wait]_range not | |
2480 | * able to find the pages tagged with SetPageError at transaction | |
2481 | * commit time. So if this happens we must abort the transaction, | |
2482 | * otherwise we commit a super block with btree roots that point to | |
2483 | * btree nodes/leafs whose content on disk is invalid - either garbage | |
2484 | * or the content of some node/leaf from a past generation that got | |
2485 | * cowed or deleted and is no longer valid. | |
2486 | * | |
2487 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would | |
2488 | * not be enough - we need to distinguish between log tree extents vs | |
2489 | * non-log tree extents, and the next filemap_fdatawait_range() call | |
2490 | * will catch and clear such errors in the mapping - and that call might | |
2491 | * be from a log sync and not from a transaction commit. Also, checking | |
2492 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is | |
2493 | * not done and would not be reliable - the eb might have been released | |
2494 | * from memory and reading it back again means that flag would not be | |
2495 | * set (since it's a runtime flag, not persisted on disk). | |
2496 | * | |
2497 | * Using the flags below in the btree inode also makes us achieve the | |
2498 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started | |
2499 | * writeback for all dirty pages and before filemap_fdatawait_range() | |
2500 | * is called, the writeback for all dirty pages had already finished | |
2501 | * with errors - because we were not using AS_EIO/AS_ENOSPC, | |
2502 | * filemap_fdatawait_range() would return success, as it could not know | |
2503 | * that writeback errors happened (the pages were no longer tagged for | |
2504 | * writeback). | |
2505 | */ | |
2506 | switch (eb->log_index) { | |
2507 | case -1: | |
5a2c6075 | 2508 | set_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags); |
656f30db FM |
2509 | break; |
2510 | case 0: | |
5a2c6075 | 2511 | set_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); |
656f30db FM |
2512 | break; |
2513 | case 1: | |
5a2c6075 | 2514 | set_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); |
656f30db FM |
2515 | break; |
2516 | default: | |
2517 | BUG(); /* unexpected, logic error */ | |
2518 | } | |
2519 | } | |
2520 | ||
2f3186d8 QW |
2521 | /* |
2522 | * The endio specific version which won't touch any unsafe spinlock in endio | |
2523 | * context. | |
2524 | */ | |
2525 | static struct extent_buffer *find_extent_buffer_nolock( | |
2526 | struct btrfs_fs_info *fs_info, u64 start) | |
2527 | { | |
2528 | struct extent_buffer *eb; | |
2529 | ||
2530 | rcu_read_lock(); | |
01cd3909 DS |
2531 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
2532 | start >> fs_info->sectorsize_bits); | |
2f3186d8 QW |
2533 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
2534 | rcu_read_unlock(); | |
2535 | return eb; | |
2536 | } | |
2537 | rcu_read_unlock(); | |
2538 | return NULL; | |
2539 | } | |
2540 | ||
2541 | /* | |
2542 | * The endio function for subpage extent buffer write. | |
2543 | * | |
2544 | * Unlike end_bio_extent_buffer_writepage(), we only call end_page_writeback() | |
2545 | * after all extent buffers in the page has finished their writeback. | |
2546 | */ | |
917f32a2 | 2547 | static void end_bio_subpage_eb_writepage(struct btrfs_bio *bbio) |
2f3186d8 | 2548 | { |
917f32a2 | 2549 | struct bio *bio = &bbio->bio; |
fa04c165 | 2550 | struct btrfs_fs_info *fs_info; |
2f3186d8 QW |
2551 | struct bio_vec *bvec; |
2552 | struct bvec_iter_all iter_all; | |
2553 | ||
fa04c165 | 2554 | fs_info = btrfs_sb(bio_first_page_all(bio)->mapping->host->i_sb); |
fbca46eb | 2555 | ASSERT(fs_info->nodesize < PAGE_SIZE); |
fa04c165 | 2556 | |
2f3186d8 QW |
2557 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
2558 | bio_for_each_segment_all(bvec, bio, iter_all) { | |
2559 | struct page *page = bvec->bv_page; | |
2560 | u64 bvec_start = page_offset(page) + bvec->bv_offset; | |
2561 | u64 bvec_end = bvec_start + bvec->bv_len - 1; | |
2562 | u64 cur_bytenr = bvec_start; | |
2563 | ||
2564 | ASSERT(IS_ALIGNED(bvec->bv_len, fs_info->nodesize)); | |
2565 | ||
2566 | /* Iterate through all extent buffers in the range */ | |
2567 | while (cur_bytenr <= bvec_end) { | |
2568 | struct extent_buffer *eb; | |
2569 | int done; | |
2570 | ||
2571 | /* | |
2572 | * Here we can't use find_extent_buffer(), as it may | |
2573 | * try to lock eb->refs_lock, which is not safe in endio | |
2574 | * context. | |
2575 | */ | |
2576 | eb = find_extent_buffer_nolock(fs_info, cur_bytenr); | |
2577 | ASSERT(eb); | |
2578 | ||
2579 | cur_bytenr = eb->start + eb->len; | |
2580 | ||
2581 | ASSERT(test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)); | |
2582 | done = atomic_dec_and_test(&eb->io_pages); | |
2583 | ASSERT(done); | |
2584 | ||
2585 | if (bio->bi_status || | |
2586 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { | |
2587 | ClearPageUptodate(page); | |
2588 | set_btree_ioerr(page, eb); | |
2589 | } | |
2590 | ||
2591 | btrfs_subpage_clear_writeback(fs_info, page, eb->start, | |
2592 | eb->len); | |
2593 | end_extent_buffer_writeback(eb); | |
2594 | /* | |
2595 | * free_extent_buffer() will grab spinlock which is not | |
2596 | * safe in endio context. Thus here we manually dec | |
2597 | * the ref. | |
2598 | */ | |
2599 | atomic_dec(&eb->refs); | |
2600 | } | |
2601 | } | |
2602 | bio_put(bio); | |
2603 | } | |
2604 | ||
917f32a2 | 2605 | static void end_bio_extent_buffer_writepage(struct btrfs_bio *bbio) |
0b32f4bb | 2606 | { |
917f32a2 | 2607 | struct bio *bio = &bbio->bio; |
2c30c71b | 2608 | struct bio_vec *bvec; |
0b32f4bb | 2609 | struct extent_buffer *eb; |
2b070cfe | 2610 | int done; |
6dc4f100 | 2611 | struct bvec_iter_all iter_all; |
0b32f4bb | 2612 | |
c09abff8 | 2613 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
2b070cfe | 2614 | bio_for_each_segment_all(bvec, bio, iter_all) { |
0b32f4bb JB |
2615 | struct page *page = bvec->bv_page; |
2616 | ||
0b32f4bb JB |
2617 | eb = (struct extent_buffer *)page->private; |
2618 | BUG_ON(!eb); | |
2619 | done = atomic_dec_and_test(&eb->io_pages); | |
2620 | ||
4e4cbee9 | 2621 | if (bio->bi_status || |
4246a0b6 | 2622 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
0b32f4bb | 2623 | ClearPageUptodate(page); |
5a2c6075 | 2624 | set_btree_ioerr(page, eb); |
0b32f4bb JB |
2625 | } |
2626 | ||
2627 | end_page_writeback(page); | |
2628 | ||
2629 | if (!done) | |
2630 | continue; | |
2631 | ||
2632 | end_extent_buffer_writeback(eb); | |
2c30c71b | 2633 | } |
0b32f4bb JB |
2634 | |
2635 | bio_put(bio); | |
0b32f4bb JB |
2636 | } |
2637 | ||
fa04c165 QW |
2638 | static void prepare_eb_write(struct extent_buffer *eb) |
2639 | { | |
2640 | u32 nritems; | |
2641 | unsigned long start; | |
2642 | unsigned long end; | |
2643 | ||
2644 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); | |
2645 | atomic_set(&eb->io_pages, num_extent_pages(eb)); | |
2646 | ||
2647 | /* Set btree blocks beyond nritems with 0 to avoid stale content */ | |
2648 | nritems = btrfs_header_nritems(eb); | |
2649 | if (btrfs_header_level(eb) > 0) { | |
2650 | end = btrfs_node_key_ptr_offset(nritems); | |
2651 | memzero_extent_buffer(eb, end, eb->len - end); | |
2652 | } else { | |
2653 | /* | |
2654 | * Leaf: | |
2655 | * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 | |
2656 | */ | |
2657 | start = btrfs_item_nr_offset(nritems); | |
2658 | end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb); | |
2659 | memzero_extent_buffer(eb, start, end - start); | |
2660 | } | |
2661 | } | |
2662 | ||
35b6ddfa QW |
2663 | /* |
2664 | * Unlike the work in write_one_eb(), we rely completely on extent locking. | |
2665 | * Page locking is only utilized at minimum to keep the VMM code happy. | |
35b6ddfa QW |
2666 | */ |
2667 | static int write_one_subpage_eb(struct extent_buffer *eb, | |
2668 | struct writeback_control *wbc, | |
2669 | struct extent_page_data *epd) | |
2670 | { | |
2671 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
2672 | struct page *page = eb->pages[0]; | |
353767e4 | 2673 | blk_opf_t write_flags = wbc_to_write_flags(wbc); |
35b6ddfa QW |
2674 | bool no_dirty_ebs = false; |
2675 | int ret; | |
2676 | ||
fa04c165 QW |
2677 | prepare_eb_write(eb); |
2678 | ||
35b6ddfa QW |
2679 | /* clear_page_dirty_for_io() in subpage helper needs page locked */ |
2680 | lock_page(page); | |
2681 | btrfs_subpage_set_writeback(fs_info, page, eb->start, eb->len); | |
2682 | ||
2683 | /* Check if this is the last dirty bit to update nr_written */ | |
2684 | no_dirty_ebs = btrfs_subpage_clear_and_test_dirty(fs_info, page, | |
2685 | eb->start, eb->len); | |
2686 | if (no_dirty_ebs) | |
2687 | clear_page_dirty_for_io(page); | |
2688 | ||
5467abba QW |
2689 | epd->bio_ctrl.end_io_func = end_bio_subpage_eb_writepage; |
2690 | ||
390ed29b | 2691 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc, |
209ecde5 | 2692 | &epd->bio_ctrl, eb->start, page, eb->len, |
5467abba | 2693 | eb->start - page_offset(page), 0, false); |
35b6ddfa QW |
2694 | if (ret) { |
2695 | btrfs_subpage_clear_writeback(fs_info, page, eb->start, eb->len); | |
2696 | set_btree_ioerr(page, eb); | |
2697 | unlock_page(page); | |
2698 | ||
2699 | if (atomic_dec_and_test(&eb->io_pages)) | |
2700 | end_extent_buffer_writeback(eb); | |
2701 | return -EIO; | |
2702 | } | |
2703 | unlock_page(page); | |
2704 | /* | |
2705 | * Submission finished without problem, if no range of the page is | |
2706 | * dirty anymore, we have submitted a page. Update nr_written in wbc. | |
2707 | */ | |
2708 | if (no_dirty_ebs) | |
572f3dad | 2709 | wbc->nr_to_write--; |
35b6ddfa QW |
2710 | return ret; |
2711 | } | |
2712 | ||
0e378df1 | 2713 | static noinline_for_stack int write_one_eb(struct extent_buffer *eb, |
0b32f4bb JB |
2714 | struct writeback_control *wbc, |
2715 | struct extent_page_data *epd) | |
2716 | { | |
0c64c33c | 2717 | u64 disk_bytenr = eb->start; |
cc5e31a4 | 2718 | int i, num_pages; |
353767e4 | 2719 | blk_opf_t write_flags = wbc_to_write_flags(wbc); |
d7dbe9e7 | 2720 | int ret = 0; |
0b32f4bb | 2721 | |
fa04c165 | 2722 | prepare_eb_write(eb); |
35b6ddfa | 2723 | |
5467abba QW |
2724 | epd->bio_ctrl.end_io_func = end_bio_extent_buffer_writepage; |
2725 | ||
fa04c165 | 2726 | num_pages = num_extent_pages(eb); |
0b32f4bb | 2727 | for (i = 0; i < num_pages; i++) { |
fb85fc9a | 2728 | struct page *p = eb->pages[i]; |
0b32f4bb JB |
2729 | |
2730 | clear_page_dirty_for_io(p); | |
2731 | set_page_writeback(p); | |
0ceb34bf | 2732 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc, |
209ecde5 | 2733 | &epd->bio_ctrl, disk_bytenr, p, |
5467abba | 2734 | PAGE_SIZE, 0, 0, false); |
0b32f4bb | 2735 | if (ret) { |
5a2c6075 | 2736 | set_btree_ioerr(p, eb); |
fe01aa65 TK |
2737 | if (PageWriteback(p)) |
2738 | end_page_writeback(p); | |
0b32f4bb JB |
2739 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
2740 | end_extent_buffer_writeback(eb); | |
2741 | ret = -EIO; | |
2742 | break; | |
2743 | } | |
0c64c33c | 2744 | disk_bytenr += PAGE_SIZE; |
572f3dad | 2745 | wbc->nr_to_write--; |
0b32f4bb JB |
2746 | unlock_page(p); |
2747 | } | |
2748 | ||
2749 | if (unlikely(ret)) { | |
2750 | for (; i < num_pages; i++) { | |
bbf65cf0 | 2751 | struct page *p = eb->pages[i]; |
81465028 | 2752 | clear_page_dirty_for_io(p); |
0b32f4bb JB |
2753 | unlock_page(p); |
2754 | } | |
2755 | } | |
2756 | ||
2757 | return ret; | |
2758 | } | |
2759 | ||
c4aec299 QW |
2760 | /* |
2761 | * Submit one subpage btree page. | |
2762 | * | |
2763 | * The main difference to submit_eb_page() is: | |
2764 | * - Page locking | |
2765 | * For subpage, we don't rely on page locking at all. | |
2766 | * | |
2767 | * - Flush write bio | |
2768 | * We only flush bio if we may be unable to fit current extent buffers into | |
2769 | * current bio. | |
2770 | * | |
2771 | * Return >=0 for the number of submitted extent buffers. | |
2772 | * Return <0 for fatal error. | |
2773 | */ | |
2774 | static int submit_eb_subpage(struct page *page, | |
2775 | struct writeback_control *wbc, | |
2776 | struct extent_page_data *epd) | |
2777 | { | |
2778 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); | |
2779 | int submitted = 0; | |
2780 | u64 page_start = page_offset(page); | |
2781 | int bit_start = 0; | |
c4aec299 QW |
2782 | int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits; |
2783 | int ret; | |
2784 | ||
2785 | /* Lock and write each dirty extent buffers in the range */ | |
72a69cd0 | 2786 | while (bit_start < fs_info->subpage_info->bitmap_nr_bits) { |
c4aec299 QW |
2787 | struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private; |
2788 | struct extent_buffer *eb; | |
2789 | unsigned long flags; | |
2790 | u64 start; | |
2791 | ||
2792 | /* | |
2793 | * Take private lock to ensure the subpage won't be detached | |
2794 | * in the meantime. | |
2795 | */ | |
2796 | spin_lock(&page->mapping->private_lock); | |
2797 | if (!PagePrivate(page)) { | |
2798 | spin_unlock(&page->mapping->private_lock); | |
2799 | break; | |
2800 | } | |
2801 | spin_lock_irqsave(&subpage->lock, flags); | |
72a69cd0 QW |
2802 | if (!test_bit(bit_start + fs_info->subpage_info->dirty_offset, |
2803 | subpage->bitmaps)) { | |
c4aec299 QW |
2804 | spin_unlock_irqrestore(&subpage->lock, flags); |
2805 | spin_unlock(&page->mapping->private_lock); | |
2806 | bit_start++; | |
2807 | continue; | |
2808 | } | |
2809 | ||
2810 | start = page_start + bit_start * fs_info->sectorsize; | |
2811 | bit_start += sectors_per_node; | |
2812 | ||
2813 | /* | |
2814 | * Here we just want to grab the eb without touching extra | |
2815 | * spin locks, so call find_extent_buffer_nolock(). | |
2816 | */ | |
2817 | eb = find_extent_buffer_nolock(fs_info, start); | |
2818 | spin_unlock_irqrestore(&subpage->lock, flags); | |
2819 | spin_unlock(&page->mapping->private_lock); | |
2820 | ||
2821 | /* | |
2822 | * The eb has already reached 0 refs thus find_extent_buffer() | |
2823 | * doesn't return it. We don't need to write back such eb | |
2824 | * anyway. | |
2825 | */ | |
2826 | if (!eb) | |
2827 | continue; | |
2828 | ||
2829 | ret = lock_extent_buffer_for_io(eb, epd); | |
2830 | if (ret == 0) { | |
2831 | free_extent_buffer(eb); | |
2832 | continue; | |
2833 | } | |
2834 | if (ret < 0) { | |
2835 | free_extent_buffer(eb); | |
2836 | goto cleanup; | |
2837 | } | |
fa04c165 | 2838 | ret = write_one_subpage_eb(eb, wbc, epd); |
c4aec299 QW |
2839 | free_extent_buffer(eb); |
2840 | if (ret < 0) | |
2841 | goto cleanup; | |
2842 | submitted++; | |
2843 | } | |
2844 | return submitted; | |
2845 | ||
2846 | cleanup: | |
2847 | /* We hit error, end bio for the submitted extent buffers */ | |
9845e5dd | 2848 | submit_write_bio(epd, ret); |
c4aec299 QW |
2849 | return ret; |
2850 | } | |
2851 | ||
f91e0d0c QW |
2852 | /* |
2853 | * Submit all page(s) of one extent buffer. | |
2854 | * | |
2855 | * @page: the page of one extent buffer | |
2856 | * @eb_context: to determine if we need to submit this page, if current page | |
2857 | * belongs to this eb, we don't need to submit | |
2858 | * | |
2859 | * The caller should pass each page in their bytenr order, and here we use | |
2860 | * @eb_context to determine if we have submitted pages of one extent buffer. | |
2861 | * | |
2862 | * If we have, we just skip until we hit a new page that doesn't belong to | |
2863 | * current @eb_context. | |
2864 | * | |
2865 | * If not, we submit all the page(s) of the extent buffer. | |
2866 | * | |
2867 | * Return >0 if we have submitted the extent buffer successfully. | |
2868 | * Return 0 if we don't need to submit the page, as it's already submitted by | |
2869 | * previous call. | |
2870 | * Return <0 for fatal error. | |
2871 | */ | |
2872 | static int submit_eb_page(struct page *page, struct writeback_control *wbc, | |
2873 | struct extent_page_data *epd, | |
2874 | struct extent_buffer **eb_context) | |
2875 | { | |
2876 | struct address_space *mapping = page->mapping; | |
0bc09ca1 | 2877 | struct btrfs_block_group *cache = NULL; |
f91e0d0c QW |
2878 | struct extent_buffer *eb; |
2879 | int ret; | |
2880 | ||
2881 | if (!PagePrivate(page)) | |
2882 | return 0; | |
2883 | ||
fbca46eb | 2884 | if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) |
c4aec299 QW |
2885 | return submit_eb_subpage(page, wbc, epd); |
2886 | ||
f91e0d0c QW |
2887 | spin_lock(&mapping->private_lock); |
2888 | if (!PagePrivate(page)) { | |
2889 | spin_unlock(&mapping->private_lock); | |
2890 | return 0; | |
2891 | } | |
2892 | ||
2893 | eb = (struct extent_buffer *)page->private; | |
2894 | ||
2895 | /* | |
2896 | * Shouldn't happen and normally this would be a BUG_ON but no point | |
2897 | * crashing the machine for something we can survive anyway. | |
2898 | */ | |
2899 | if (WARN_ON(!eb)) { | |
2900 | spin_unlock(&mapping->private_lock); | |
2901 | return 0; | |
2902 | } | |
2903 | ||
2904 | if (eb == *eb_context) { | |
2905 | spin_unlock(&mapping->private_lock); | |
2906 | return 0; | |
2907 | } | |
2908 | ret = atomic_inc_not_zero(&eb->refs); | |
2909 | spin_unlock(&mapping->private_lock); | |
2910 | if (!ret) | |
2911 | return 0; | |
2912 | ||
0bc09ca1 NA |
2913 | if (!btrfs_check_meta_write_pointer(eb->fs_info, eb, &cache)) { |
2914 | /* | |
2915 | * If for_sync, this hole will be filled with | |
2916 | * trasnsaction commit. | |
2917 | */ | |
2918 | if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) | |
2919 | ret = -EAGAIN; | |
2920 | else | |
2921 | ret = 0; | |
2922 | free_extent_buffer(eb); | |
2923 | return ret; | |
2924 | } | |
2925 | ||
f91e0d0c QW |
2926 | *eb_context = eb; |
2927 | ||
2928 | ret = lock_extent_buffer_for_io(eb, epd); | |
2929 | if (ret <= 0) { | |
0bc09ca1 NA |
2930 | btrfs_revert_meta_write_pointer(cache, eb); |
2931 | if (cache) | |
2932 | btrfs_put_block_group(cache); | |
f91e0d0c QW |
2933 | free_extent_buffer(eb); |
2934 | return ret; | |
2935 | } | |
be1a1d7a | 2936 | if (cache) { |
d3e29967 NB |
2937 | /* |
2938 | * Implies write in zoned mode. Mark the last eb in a block group. | |
2939 | */ | |
56fbb0a4 | 2940 | btrfs_schedule_zone_finish_bg(cache, eb); |
d3e29967 | 2941 | btrfs_put_block_group(cache); |
be1a1d7a | 2942 | } |
f91e0d0c QW |
2943 | ret = write_one_eb(eb, wbc, epd); |
2944 | free_extent_buffer(eb); | |
2945 | if (ret < 0) | |
2946 | return ret; | |
2947 | return 1; | |
2948 | } | |
2949 | ||
0b32f4bb JB |
2950 | int btree_write_cache_pages(struct address_space *mapping, |
2951 | struct writeback_control *wbc) | |
2952 | { | |
f91e0d0c | 2953 | struct extent_buffer *eb_context = NULL; |
0b32f4bb | 2954 | struct extent_page_data epd = { |
390ed29b | 2955 | .bio_ctrl = { 0 }, |
0b32f4bb JB |
2956 | .extent_locked = 0, |
2957 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, | |
2958 | }; | |
b3ff8f1d | 2959 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
0b32f4bb JB |
2960 | int ret = 0; |
2961 | int done = 0; | |
2962 | int nr_to_write_done = 0; | |
2963 | struct pagevec pvec; | |
2964 | int nr_pages; | |
2965 | pgoff_t index; | |
2966 | pgoff_t end; /* Inclusive */ | |
2967 | int scanned = 0; | |
10bbd235 | 2968 | xa_mark_t tag; |
0b32f4bb | 2969 | |
86679820 | 2970 | pagevec_init(&pvec); |
0b32f4bb JB |
2971 | if (wbc->range_cyclic) { |
2972 | index = mapping->writeback_index; /* Start from prev offset */ | |
2973 | end = -1; | |
556755a8 JB |
2974 | /* |
2975 | * Start from the beginning does not need to cycle over the | |
2976 | * range, mark it as scanned. | |
2977 | */ | |
2978 | scanned = (index == 0); | |
0b32f4bb | 2979 | } else { |
09cbfeaf KS |
2980 | index = wbc->range_start >> PAGE_SHIFT; |
2981 | end = wbc->range_end >> PAGE_SHIFT; | |
0b32f4bb JB |
2982 | scanned = 1; |
2983 | } | |
2984 | if (wbc->sync_mode == WB_SYNC_ALL) | |
2985 | tag = PAGECACHE_TAG_TOWRITE; | |
2986 | else | |
2987 | tag = PAGECACHE_TAG_DIRTY; | |
0bc09ca1 | 2988 | btrfs_zoned_meta_io_lock(fs_info); |
0b32f4bb JB |
2989 | retry: |
2990 | if (wbc->sync_mode == WB_SYNC_ALL) | |
2991 | tag_pages_for_writeback(mapping, index, end); | |
2992 | while (!done && !nr_to_write_done && (index <= end) && | |
4006f437 | 2993 | (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, |
67fd707f | 2994 | tag))) { |
0b32f4bb JB |
2995 | unsigned i; |
2996 | ||
0b32f4bb JB |
2997 | for (i = 0; i < nr_pages; i++) { |
2998 | struct page *page = pvec.pages[i]; | |
2999 | ||
f91e0d0c QW |
3000 | ret = submit_eb_page(page, wbc, &epd, &eb_context); |
3001 | if (ret == 0) | |
0b32f4bb | 3002 | continue; |
f91e0d0c | 3003 | if (ret < 0) { |
0b32f4bb | 3004 | done = 1; |
0b32f4bb JB |
3005 | break; |
3006 | } | |
0b32f4bb JB |
3007 | |
3008 | /* | |
3009 | * the filesystem may choose to bump up nr_to_write. | |
3010 | * We have to make sure to honor the new nr_to_write | |
3011 | * at any time | |
3012 | */ | |
3013 | nr_to_write_done = wbc->nr_to_write <= 0; | |
3014 | } | |
3015 | pagevec_release(&pvec); | |
3016 | cond_resched(); | |
3017 | } | |
3018 | if (!scanned && !done) { | |
3019 | /* | |
3020 | * We hit the last page and there is more work to be done: wrap | |
3021 | * back to the start of the file | |
3022 | */ | |
3023 | scanned = 1; | |
3024 | index = 0; | |
3025 | goto retry; | |
3026 | } | |
b3ff8f1d QW |
3027 | /* |
3028 | * If something went wrong, don't allow any metadata write bio to be | |
3029 | * submitted. | |
3030 | * | |
3031 | * This would prevent use-after-free if we had dirty pages not | |
3032 | * cleaned up, which can still happen by fuzzed images. | |
3033 | * | |
3034 | * - Bad extent tree | |
3035 | * Allowing existing tree block to be allocated for other trees. | |
3036 | * | |
3037 | * - Log tree operations | |
3038 | * Exiting tree blocks get allocated to log tree, bumps its | |
3039 | * generation, then get cleaned in tree re-balance. | |
3040 | * Such tree block will not be written back, since it's clean, | |
3041 | * thus no WRITTEN flag set. | |
3042 | * And after log writes back, this tree block is not traced by | |
3043 | * any dirty extent_io_tree. | |
3044 | * | |
3045 | * - Offending tree block gets re-dirtied from its original owner | |
3046 | * Since it has bumped generation, no WRITTEN flag, it can be | |
3047 | * reused without COWing. This tree block will not be traced | |
3048 | * by btrfs_transaction::dirty_pages. | |
3049 | * | |
3050 | * Now such dirty tree block will not be cleaned by any dirty | |
3051 | * extent io tree. Thus we don't want to submit such wild eb | |
3052 | * if the fs already has error. | |
9845e5dd | 3053 | * |
c9583ada QW |
3054 | * We can get ret > 0 from submit_extent_page() indicating how many ebs |
3055 | * were submitted. Reset it to 0 to avoid false alerts for the caller. | |
3056 | */ | |
3057 | if (ret > 0) | |
3058 | ret = 0; | |
9845e5dd CH |
3059 | if (!ret && BTRFS_FS_ERROR(fs_info)) |
3060 | ret = -EROFS; | |
3061 | submit_write_bio(&epd, ret); | |
3062 | ||
3063 | btrfs_zoned_meta_io_unlock(fs_info); | |
0b32f4bb JB |
3064 | return ret; |
3065 | } | |
3066 | ||
d1310b2e | 3067 | /** |
3bed2da1 NB |
3068 | * Walk the list of dirty pages of the given address space and write all of them. |
3069 | * | |
d1310b2e | 3070 | * @mapping: address space structure to write |
3bed2da1 NB |
3071 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
3072 | * @epd: holds context for the write, namely the bio | |
d1310b2e CM |
3073 | * |
3074 | * If a page is already under I/O, write_cache_pages() skips it, even | |
3075 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, | |
3076 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() | |
3077 | * and msync() need to guarantee that all the data which was dirty at the time | |
3078 | * the call was made get new I/O started against them. If wbc->sync_mode is | |
3079 | * WB_SYNC_ALL then we were called for data integrity and we must wait for | |
3080 | * existing IO to complete. | |
3081 | */ | |
4242b64a | 3082 | static int extent_write_cache_pages(struct address_space *mapping, |
4bef0848 | 3083 | struct writeback_control *wbc, |
aab6e9ed | 3084 | struct extent_page_data *epd) |
d1310b2e | 3085 | { |
7fd1a3f7 | 3086 | struct inode *inode = mapping->host; |
d1310b2e CM |
3087 | int ret = 0; |
3088 | int done = 0; | |
f85d7d6c | 3089 | int nr_to_write_done = 0; |
d1310b2e CM |
3090 | struct pagevec pvec; |
3091 | int nr_pages; | |
3092 | pgoff_t index; | |
3093 | pgoff_t end; /* Inclusive */ | |
a9132667 LB |
3094 | pgoff_t done_index; |
3095 | int range_whole = 0; | |
d1310b2e | 3096 | int scanned = 0; |
10bbd235 | 3097 | xa_mark_t tag; |
d1310b2e | 3098 | |
7fd1a3f7 JB |
3099 | /* |
3100 | * We have to hold onto the inode so that ordered extents can do their | |
3101 | * work when the IO finishes. The alternative to this is failing to add | |
3102 | * an ordered extent if the igrab() fails there and that is a huge pain | |
3103 | * to deal with, so instead just hold onto the inode throughout the | |
3104 | * writepages operation. If it fails here we are freeing up the inode | |
3105 | * anyway and we'd rather not waste our time writing out stuff that is | |
3106 | * going to be truncated anyway. | |
3107 | */ | |
3108 | if (!igrab(inode)) | |
3109 | return 0; | |
3110 | ||
86679820 | 3111 | pagevec_init(&pvec); |
d1310b2e CM |
3112 | if (wbc->range_cyclic) { |
3113 | index = mapping->writeback_index; /* Start from prev offset */ | |
3114 | end = -1; | |
556755a8 JB |
3115 | /* |
3116 | * Start from the beginning does not need to cycle over the | |
3117 | * range, mark it as scanned. | |
3118 | */ | |
3119 | scanned = (index == 0); | |
d1310b2e | 3120 | } else { |
09cbfeaf KS |
3121 | index = wbc->range_start >> PAGE_SHIFT; |
3122 | end = wbc->range_end >> PAGE_SHIFT; | |
a9132667 LB |
3123 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
3124 | range_whole = 1; | |
d1310b2e CM |
3125 | scanned = 1; |
3126 | } | |
3cd24c69 EL |
3127 | |
3128 | /* | |
3129 | * We do the tagged writepage as long as the snapshot flush bit is set | |
3130 | * and we are the first one who do the filemap_flush() on this inode. | |
3131 | * | |
3132 | * The nr_to_write == LONG_MAX is needed to make sure other flushers do | |
3133 | * not race in and drop the bit. | |
3134 | */ | |
3135 | if (range_whole && wbc->nr_to_write == LONG_MAX && | |
3136 | test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
3137 | &BTRFS_I(inode)->runtime_flags)) | |
3138 | wbc->tagged_writepages = 1; | |
3139 | ||
3140 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) | |
f7aaa06b JB |
3141 | tag = PAGECACHE_TAG_TOWRITE; |
3142 | else | |
3143 | tag = PAGECACHE_TAG_DIRTY; | |
d1310b2e | 3144 | retry: |
3cd24c69 | 3145 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
f7aaa06b | 3146 | tag_pages_for_writeback(mapping, index, end); |
a9132667 | 3147 | done_index = index; |
f85d7d6c | 3148 | while (!done && !nr_to_write_done && (index <= end) && |
67fd707f JK |
3149 | (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, |
3150 | &index, end, tag))) { | |
d1310b2e CM |
3151 | unsigned i; |
3152 | ||
d1310b2e CM |
3153 | for (i = 0; i < nr_pages; i++) { |
3154 | struct page *page = pvec.pages[i]; | |
3155 | ||
f7bddf1e | 3156 | done_index = page->index + 1; |
d1310b2e | 3157 | /* |
b93b0163 MW |
3158 | * At this point we hold neither the i_pages lock nor |
3159 | * the page lock: the page may be truncated or | |
3160 | * invalidated (changing page->mapping to NULL), | |
3161 | * or even swizzled back from swapper_space to | |
3162 | * tmpfs file mapping | |
d1310b2e | 3163 | */ |
c8f2f24b | 3164 | if (!trylock_page(page)) { |
9845e5dd | 3165 | submit_write_bio(epd, 0); |
c8f2f24b | 3166 | lock_page(page); |
01d658f2 | 3167 | } |
d1310b2e CM |
3168 | |
3169 | if (unlikely(page->mapping != mapping)) { | |
3170 | unlock_page(page); | |
3171 | continue; | |
3172 | } | |
3173 | ||
d2c3f4f6 | 3174 | if (wbc->sync_mode != WB_SYNC_NONE) { |
c9583ada | 3175 | if (PageWriteback(page)) |
9845e5dd | 3176 | submit_write_bio(epd, 0); |
d1310b2e | 3177 | wait_on_page_writeback(page); |
d2c3f4f6 | 3178 | } |
d1310b2e CM |
3179 | |
3180 | if (PageWriteback(page) || | |
3181 | !clear_page_dirty_for_io(page)) { | |
3182 | unlock_page(page); | |
3183 | continue; | |
3184 | } | |
3185 | ||
aab6e9ed | 3186 | ret = __extent_writepage(page, wbc, epd); |
a9132667 | 3187 | if (ret < 0) { |
a9132667 LB |
3188 | done = 1; |
3189 | break; | |
3190 | } | |
f85d7d6c CM |
3191 | |
3192 | /* | |
3193 | * the filesystem may choose to bump up nr_to_write. | |
3194 | * We have to make sure to honor the new nr_to_write | |
3195 | * at any time | |
3196 | */ | |
3197 | nr_to_write_done = wbc->nr_to_write <= 0; | |
d1310b2e CM |
3198 | } |
3199 | pagevec_release(&pvec); | |
3200 | cond_resched(); | |
3201 | } | |
894b36e3 | 3202 | if (!scanned && !done) { |
d1310b2e CM |
3203 | /* |
3204 | * We hit the last page and there is more work to be done: wrap | |
3205 | * back to the start of the file | |
3206 | */ | |
3207 | scanned = 1; | |
3208 | index = 0; | |
42ffb0bf JB |
3209 | |
3210 | /* | |
3211 | * If we're looping we could run into a page that is locked by a | |
3212 | * writer and that writer could be waiting on writeback for a | |
3213 | * page in our current bio, and thus deadlock, so flush the | |
3214 | * write bio here. | |
3215 | */ | |
9845e5dd | 3216 | submit_write_bio(epd, 0); |
c9583ada | 3217 | goto retry; |
d1310b2e | 3218 | } |
a9132667 LB |
3219 | |
3220 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole)) | |
3221 | mapping->writeback_index = done_index; | |
3222 | ||
7fd1a3f7 | 3223 | btrfs_add_delayed_iput(inode); |
894b36e3 | 3224 | return ret; |
d1310b2e | 3225 | } |
d1310b2e | 3226 | |
2bd0fc93 QW |
3227 | /* |
3228 | * Submit the pages in the range to bio for call sites which delalloc range has | |
3229 | * already been ran (aka, ordered extent inserted) and all pages are still | |
3230 | * locked. | |
3231 | */ | |
3232 | int extent_write_locked_range(struct inode *inode, u64 start, u64 end) | |
771ed689 | 3233 | { |
2bd0fc93 QW |
3234 | bool found_error = false; |
3235 | int first_error = 0; | |
771ed689 CM |
3236 | int ret = 0; |
3237 | struct address_space *mapping = inode->i_mapping; | |
3238 | struct page *page; | |
2bd0fc93 | 3239 | u64 cur = start; |
66448b9d QW |
3240 | unsigned long nr_pages; |
3241 | const u32 sectorsize = btrfs_sb(inode->i_sb)->sectorsize; | |
771ed689 | 3242 | struct extent_page_data epd = { |
390ed29b | 3243 | .bio_ctrl = { 0 }, |
771ed689 | 3244 | .extent_locked = 1, |
2bd0fc93 | 3245 | .sync_io = 1, |
771ed689 CM |
3246 | }; |
3247 | struct writeback_control wbc_writepages = { | |
2bd0fc93 | 3248 | .sync_mode = WB_SYNC_ALL, |
771ed689 CM |
3249 | .range_start = start, |
3250 | .range_end = end + 1, | |
ec39f769 CM |
3251 | /* We're called from an async helper function */ |
3252 | .punt_to_cgroup = 1, | |
3253 | .no_cgroup_owner = 1, | |
771ed689 CM |
3254 | }; |
3255 | ||
66448b9d QW |
3256 | ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(end + 1, sectorsize)); |
3257 | nr_pages = (round_up(end, PAGE_SIZE) - round_down(start, PAGE_SIZE)) >> | |
3258 | PAGE_SHIFT; | |
3259 | wbc_writepages.nr_to_write = nr_pages * 2; | |
3260 | ||
dbb70bec | 3261 | wbc_attach_fdatawrite_inode(&wbc_writepages, inode); |
2bd0fc93 | 3262 | while (cur <= end) { |
66448b9d QW |
3263 | u64 cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end); |
3264 | ||
2bd0fc93 QW |
3265 | page = find_get_page(mapping, cur >> PAGE_SHIFT); |
3266 | /* | |
3267 | * All pages in the range are locked since | |
3268 | * btrfs_run_delalloc_range(), thus there is no way to clear | |
3269 | * the page dirty flag. | |
3270 | */ | |
66448b9d | 3271 | ASSERT(PageLocked(page)); |
2bd0fc93 QW |
3272 | ASSERT(PageDirty(page)); |
3273 | clear_page_dirty_for_io(page); | |
3274 | ret = __extent_writepage(page, &wbc_writepages, &epd); | |
3275 | ASSERT(ret <= 0); | |
3276 | if (ret < 0) { | |
3277 | found_error = true; | |
3278 | first_error = ret; | |
771ed689 | 3279 | } |
09cbfeaf | 3280 | put_page(page); |
66448b9d | 3281 | cur = cur_end + 1; |
771ed689 CM |
3282 | } |
3283 | ||
9845e5dd | 3284 | submit_write_bio(&epd, found_error ? ret : 0); |
dbb70bec CM |
3285 | |
3286 | wbc_detach_inode(&wbc_writepages); | |
2bd0fc93 QW |
3287 | if (found_error) |
3288 | return first_error; | |
771ed689 CM |
3289 | return ret; |
3290 | } | |
d1310b2e | 3291 | |
8ae225a8 | 3292 | int extent_writepages(struct address_space *mapping, |
d1310b2e CM |
3293 | struct writeback_control *wbc) |
3294 | { | |
35156d85 | 3295 | struct inode *inode = mapping->host; |
d1310b2e CM |
3296 | int ret = 0; |
3297 | struct extent_page_data epd = { | |
390ed29b | 3298 | .bio_ctrl = { 0 }, |
771ed689 | 3299 | .extent_locked = 0, |
ffbd517d | 3300 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
d1310b2e CM |
3301 | }; |
3302 | ||
35156d85 JT |
3303 | /* |
3304 | * Allow only a single thread to do the reloc work in zoned mode to | |
3305 | * protect the write pointer updates. | |
3306 | */ | |
869f4cdc | 3307 | btrfs_zoned_data_reloc_lock(BTRFS_I(inode)); |
935db853 | 3308 | ret = extent_write_cache_pages(mapping, wbc, &epd); |
9845e5dd | 3309 | submit_write_bio(&epd, ret); |
19ab78ca | 3310 | btrfs_zoned_data_reloc_unlock(BTRFS_I(inode)); |
d1310b2e CM |
3311 | return ret; |
3312 | } | |
d1310b2e | 3313 | |
ba206a02 | 3314 | void extent_readahead(struct readahead_control *rac) |
d1310b2e | 3315 | { |
390ed29b | 3316 | struct btrfs_bio_ctrl bio_ctrl = { 0 }; |
67c9684f | 3317 | struct page *pagepool[16]; |
125bac01 | 3318 | struct extent_map *em_cached = NULL; |
808f80b4 | 3319 | u64 prev_em_start = (u64)-1; |
ba206a02 | 3320 | int nr; |
d1310b2e | 3321 | |
ba206a02 | 3322 | while ((nr = readahead_page_batch(rac, pagepool))) { |
32c0a6bc MWO |
3323 | u64 contig_start = readahead_pos(rac); |
3324 | u64 contig_end = contig_start + readahead_batch_length(rac) - 1; | |
e65ef21e | 3325 | |
ba206a02 | 3326 | contiguous_readpages(pagepool, nr, contig_start, contig_end, |
390ed29b | 3327 | &em_cached, &bio_ctrl, &prev_em_start); |
d1310b2e | 3328 | } |
67c9684f | 3329 | |
125bac01 MX |
3330 | if (em_cached) |
3331 | free_extent_map(em_cached); | |
722c82ac | 3332 | submit_one_bio(&bio_ctrl); |
d1310b2e | 3333 | } |
d1310b2e CM |
3334 | |
3335 | /* | |
895586eb MWO |
3336 | * basic invalidate_folio code, this waits on any locked or writeback |
3337 | * ranges corresponding to the folio, and then deletes any extent state | |
d1310b2e CM |
3338 | * records from the tree |
3339 | */ | |
895586eb MWO |
3340 | int extent_invalidate_folio(struct extent_io_tree *tree, |
3341 | struct folio *folio, size_t offset) | |
d1310b2e | 3342 | { |
2ac55d41 | 3343 | struct extent_state *cached_state = NULL; |
895586eb MWO |
3344 | u64 start = folio_pos(folio); |
3345 | u64 end = start + folio_size(folio) - 1; | |
3346 | size_t blocksize = folio->mapping->host->i_sb->s_blocksize; | |
d1310b2e | 3347 | |
829ddec9 QW |
3348 | /* This function is only called for the btree inode */ |
3349 | ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO); | |
3350 | ||
fda2832f | 3351 | start += ALIGN(offset, blocksize); |
d1310b2e CM |
3352 | if (start > end) |
3353 | return 0; | |
3354 | ||
570eb97b | 3355 | lock_extent(tree, start, end, &cached_state); |
895586eb | 3356 | folio_wait_writeback(folio); |
829ddec9 QW |
3357 | |
3358 | /* | |
3359 | * Currently for btree io tree, only EXTENT_LOCKED is utilized, | |
3360 | * so here we only need to unlock the extent range to free any | |
3361 | * existing extent state. | |
3362 | */ | |
570eb97b | 3363 | unlock_extent(tree, start, end, &cached_state); |
d1310b2e CM |
3364 | return 0; |
3365 | } | |
d1310b2e | 3366 | |
7b13b7b1 | 3367 | /* |
f913cff3 | 3368 | * a helper for release_folio, this tests for areas of the page that |
7b13b7b1 CM |
3369 | * are locked or under IO and drops the related state bits if it is safe |
3370 | * to drop the page. | |
3371 | */ | |
29c68b2d | 3372 | static int try_release_extent_state(struct extent_io_tree *tree, |
48a3b636 | 3373 | struct page *page, gfp_t mask) |
7b13b7b1 | 3374 | { |
4eee4fa4 | 3375 | u64 start = page_offset(page); |
09cbfeaf | 3376 | u64 end = start + PAGE_SIZE - 1; |
7b13b7b1 CM |
3377 | int ret = 1; |
3378 | ||
8882679e | 3379 | if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) { |
7b13b7b1 | 3380 | ret = 0; |
8882679e | 3381 | } else { |
b71fb16b JB |
3382 | u32 clear_bits = ~(EXTENT_LOCKED | EXTENT_NODATASUM | |
3383 | EXTENT_DELALLOC_NEW | EXTENT_CTLBITS); | |
3384 | ||
11ef160f | 3385 | /* |
2766ff61 FM |
3386 | * At this point we can safely clear everything except the |
3387 | * locked bit, the nodatasum bit and the delalloc new bit. | |
3388 | * The delalloc new bit will be cleared by ordered extent | |
3389 | * completion. | |
11ef160f | 3390 | */ |
bd015294 | 3391 | ret = __clear_extent_bit(tree, start, end, clear_bits, NULL, |
b71fb16b | 3392 | mask, NULL); |
e3f24cc5 CM |
3393 | |
3394 | /* if clear_extent_bit failed for enomem reasons, | |
3395 | * we can't allow the release to continue. | |
3396 | */ | |
3397 | if (ret < 0) | |
3398 | ret = 0; | |
3399 | else | |
3400 | ret = 1; | |
7b13b7b1 CM |
3401 | } |
3402 | return ret; | |
3403 | } | |
7b13b7b1 | 3404 | |
d1310b2e | 3405 | /* |
f913cff3 | 3406 | * a helper for release_folio. As long as there are no locked extents |
d1310b2e CM |
3407 | * in the range corresponding to the page, both state records and extent |
3408 | * map records are removed | |
3409 | */ | |
477a30ba | 3410 | int try_release_extent_mapping(struct page *page, gfp_t mask) |
d1310b2e CM |
3411 | { |
3412 | struct extent_map *em; | |
4eee4fa4 | 3413 | u64 start = page_offset(page); |
09cbfeaf | 3414 | u64 end = start + PAGE_SIZE - 1; |
bd3599a0 FM |
3415 | struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host); |
3416 | struct extent_io_tree *tree = &btrfs_inode->io_tree; | |
3417 | struct extent_map_tree *map = &btrfs_inode->extent_tree; | |
7b13b7b1 | 3418 | |
d0164adc | 3419 | if (gfpflags_allow_blocking(mask) && |
ee22184b | 3420 | page->mapping->host->i_size > SZ_16M) { |
39b5637f | 3421 | u64 len; |
70dec807 | 3422 | while (start <= end) { |
fbc2bd7e FM |
3423 | struct btrfs_fs_info *fs_info; |
3424 | u64 cur_gen; | |
3425 | ||
39b5637f | 3426 | len = end - start + 1; |
890871be | 3427 | write_lock(&map->lock); |
39b5637f | 3428 | em = lookup_extent_mapping(map, start, len); |
285190d9 | 3429 | if (!em) { |
890871be | 3430 | write_unlock(&map->lock); |
70dec807 CM |
3431 | break; |
3432 | } | |
7f3c74fb CM |
3433 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
3434 | em->start != start) { | |
890871be | 3435 | write_unlock(&map->lock); |
70dec807 CM |
3436 | free_extent_map(em); |
3437 | break; | |
3438 | } | |
3d6448e6 FM |
3439 | if (test_range_bit(tree, em->start, |
3440 | extent_map_end(em) - 1, | |
3441 | EXTENT_LOCKED, 0, NULL)) | |
3442 | goto next; | |
3443 | /* | |
3444 | * If it's not in the list of modified extents, used | |
3445 | * by a fast fsync, we can remove it. If it's being | |
3446 | * logged we can safely remove it since fsync took an | |
3447 | * extra reference on the em. | |
3448 | */ | |
3449 | if (list_empty(&em->list) || | |
fbc2bd7e FM |
3450 | test_bit(EXTENT_FLAG_LOGGING, &em->flags)) |
3451 | goto remove_em; | |
3452 | /* | |
3453 | * If it's in the list of modified extents, remove it | |
3454 | * only if its generation is older then the current one, | |
3455 | * in which case we don't need it for a fast fsync. | |
3456 | * Otherwise don't remove it, we could be racing with an | |
3457 | * ongoing fast fsync that could miss the new extent. | |
3458 | */ | |
3459 | fs_info = btrfs_inode->root->fs_info; | |
3460 | spin_lock(&fs_info->trans_lock); | |
3461 | cur_gen = fs_info->generation; | |
3462 | spin_unlock(&fs_info->trans_lock); | |
3463 | if (em->generation >= cur_gen) | |
3464 | goto next; | |
3465 | remove_em: | |
5e548b32 FM |
3466 | /* |
3467 | * We only remove extent maps that are not in the list of | |
3468 | * modified extents or that are in the list but with a | |
3469 | * generation lower then the current generation, so there | |
3470 | * is no need to set the full fsync flag on the inode (it | |
3471 | * hurts the fsync performance for workloads with a data | |
3472 | * size that exceeds or is close to the system's memory). | |
3473 | */ | |
fbc2bd7e FM |
3474 | remove_extent_mapping(map, em); |
3475 | /* once for the rb tree */ | |
3476 | free_extent_map(em); | |
3d6448e6 | 3477 | next: |
70dec807 | 3478 | start = extent_map_end(em); |
890871be | 3479 | write_unlock(&map->lock); |
70dec807 CM |
3480 | |
3481 | /* once for us */ | |
d1310b2e | 3482 | free_extent_map(em); |
9f47eb54 PM |
3483 | |
3484 | cond_resched(); /* Allow large-extent preemption. */ | |
d1310b2e | 3485 | } |
d1310b2e | 3486 | } |
29c68b2d | 3487 | return try_release_extent_state(tree, page, mask); |
d1310b2e | 3488 | } |
d1310b2e | 3489 | |
4751832d QW |
3490 | /* |
3491 | * To cache previous fiemap extent | |
3492 | * | |
3493 | * Will be used for merging fiemap extent | |
3494 | */ | |
3495 | struct fiemap_cache { | |
3496 | u64 offset; | |
3497 | u64 phys; | |
3498 | u64 len; | |
3499 | u32 flags; | |
3500 | bool cached; | |
3501 | }; | |
3502 | ||
3503 | /* | |
3504 | * Helper to submit fiemap extent. | |
3505 | * | |
3506 | * Will try to merge current fiemap extent specified by @offset, @phys, | |
3507 | * @len and @flags with cached one. | |
3508 | * And only when we fails to merge, cached one will be submitted as | |
3509 | * fiemap extent. | |
3510 | * | |
3511 | * Return value is the same as fiemap_fill_next_extent(). | |
3512 | */ | |
3513 | static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo, | |
3514 | struct fiemap_cache *cache, | |
3515 | u64 offset, u64 phys, u64 len, u32 flags) | |
3516 | { | |
3517 | int ret = 0; | |
3518 | ||
ac3c0d36 FM |
3519 | /* Set at the end of extent_fiemap(). */ |
3520 | ASSERT((flags & FIEMAP_EXTENT_LAST) == 0); | |
3521 | ||
4751832d QW |
3522 | if (!cache->cached) |
3523 | goto assign; | |
3524 | ||
3525 | /* | |
3526 | * Sanity check, extent_fiemap() should have ensured that new | |
52042d8e | 3527 | * fiemap extent won't overlap with cached one. |
4751832d QW |
3528 | * Not recoverable. |
3529 | * | |
3530 | * NOTE: Physical address can overlap, due to compression | |
3531 | */ | |
3532 | if (cache->offset + cache->len > offset) { | |
3533 | WARN_ON(1); | |
3534 | return -EINVAL; | |
3535 | } | |
3536 | ||
3537 | /* | |
3538 | * Only merges fiemap extents if | |
3539 | * 1) Their logical addresses are continuous | |
3540 | * | |
3541 | * 2) Their physical addresses are continuous | |
3542 | * So truly compressed (physical size smaller than logical size) | |
3543 | * extents won't get merged with each other | |
3544 | * | |
ac3c0d36 | 3545 | * 3) Share same flags |
4751832d QW |
3546 | */ |
3547 | if (cache->offset + cache->len == offset && | |
3548 | cache->phys + cache->len == phys && | |
ac3c0d36 | 3549 | cache->flags == flags) { |
4751832d | 3550 | cache->len += len; |
ac3c0d36 | 3551 | return 0; |
4751832d QW |
3552 | } |
3553 | ||
3554 | /* Not mergeable, need to submit cached one */ | |
3555 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, | |
3556 | cache->len, cache->flags); | |
3557 | cache->cached = false; | |
3558 | if (ret) | |
3559 | return ret; | |
3560 | assign: | |
3561 | cache->cached = true; | |
3562 | cache->offset = offset; | |
3563 | cache->phys = phys; | |
3564 | cache->len = len; | |
3565 | cache->flags = flags; | |
ac3c0d36 FM |
3566 | |
3567 | return 0; | |
4751832d QW |
3568 | } |
3569 | ||
3570 | /* | |
848c23b7 | 3571 | * Emit last fiemap cache |
4751832d | 3572 | * |
848c23b7 QW |
3573 | * The last fiemap cache may still be cached in the following case: |
3574 | * 0 4k 8k | |
3575 | * |<- Fiemap range ->| | |
3576 | * |<------------ First extent ----------->| | |
3577 | * | |
3578 | * In this case, the first extent range will be cached but not emitted. | |
3579 | * So we must emit it before ending extent_fiemap(). | |
4751832d | 3580 | */ |
5c5aff98 | 3581 | static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo, |
848c23b7 | 3582 | struct fiemap_cache *cache) |
4751832d QW |
3583 | { |
3584 | int ret; | |
3585 | ||
3586 | if (!cache->cached) | |
3587 | return 0; | |
3588 | ||
4751832d QW |
3589 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
3590 | cache->len, cache->flags); | |
3591 | cache->cached = false; | |
3592 | if (ret > 0) | |
3593 | ret = 0; | |
3594 | return ret; | |
3595 | } | |
3596 | ||
ac3c0d36 | 3597 | static int fiemap_next_leaf_item(struct btrfs_inode *inode, struct btrfs_path *path) |
1506fcc8 | 3598 | { |
ac3c0d36 FM |
3599 | struct extent_buffer *clone; |
3600 | struct btrfs_key key; | |
3601 | int slot; | |
3602 | int ret; | |
3603 | ||
3604 | path->slots[0]++; | |
3605 | if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) | |
3606 | return 0; | |
3607 | ||
3608 | ret = btrfs_next_leaf(inode->root, path); | |
3609 | if (ret != 0) | |
3610 | return ret; | |
3611 | ||
3612 | /* | |
3613 | * Don't bother with cloning if there are no more file extent items for | |
3614 | * our inode. | |
3615 | */ | |
3616 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
3617 | if (key.objectid != btrfs_ino(inode) || key.type != BTRFS_EXTENT_DATA_KEY) | |
3618 | return 1; | |
3619 | ||
3620 | /* See the comment at fiemap_search_slot() about why we clone. */ | |
3621 | clone = btrfs_clone_extent_buffer(path->nodes[0]); | |
3622 | if (!clone) | |
3623 | return -ENOMEM; | |
3624 | ||
3625 | slot = path->slots[0]; | |
3626 | btrfs_release_path(path); | |
3627 | path->nodes[0] = clone; | |
3628 | path->slots[0] = slot; | |
3629 | ||
3630 | return 0; | |
3631 | } | |
3632 | ||
3633 | /* | |
3634 | * Search for the first file extent item that starts at a given file offset or | |
3635 | * the one that starts immediately before that offset. | |
3636 | * Returns: 0 on success, < 0 on error, 1 if not found. | |
3637 | */ | |
3638 | static int fiemap_search_slot(struct btrfs_inode *inode, struct btrfs_path *path, | |
3639 | u64 file_offset) | |
3640 | { | |
3641 | const u64 ino = btrfs_ino(inode); | |
facee0a0 | 3642 | struct btrfs_root *root = inode->root; |
ac3c0d36 FM |
3643 | struct extent_buffer *clone; |
3644 | struct btrfs_key key; | |
3645 | int slot; | |
3646 | int ret; | |
1506fcc8 | 3647 | |
ac3c0d36 FM |
3648 | key.objectid = ino; |
3649 | key.type = BTRFS_EXTENT_DATA_KEY; | |
3650 | key.offset = file_offset; | |
3651 | ||
3652 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
3653 | if (ret < 0) | |
3654 | return ret; | |
3655 | ||
3656 | if (ret > 0 && path->slots[0] > 0) { | |
3657 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1); | |
3658 | if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY) | |
3659 | path->slots[0]--; | |
3660 | } | |
3661 | ||
3662 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | |
3663 | ret = btrfs_next_leaf(root, path); | |
3664 | if (ret != 0) | |
3665 | return ret; | |
3666 | ||
3667 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
3668 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) | |
3669 | return 1; | |
5911c8fe DS |
3670 | } |
3671 | ||
15c7745c | 3672 | /* |
ac3c0d36 FM |
3673 | * We clone the leaf and use it during fiemap. This is because while |
3674 | * using the leaf we do expensive things like checking if an extent is | |
3675 | * shared, which can take a long time. In order to prevent blocking | |
3676 | * other tasks for too long, we use a clone of the leaf. We have locked | |
3677 | * the file range in the inode's io tree, so we know none of our file | |
3678 | * extent items can change. This way we avoid blocking other tasks that | |
3679 | * want to insert items for other inodes in the same leaf or b+tree | |
3680 | * rebalance operations (triggered for example when someone is trying | |
3681 | * to push items into this leaf when trying to insert an item in a | |
3682 | * neighbour leaf). | |
3683 | * We also need the private clone because holding a read lock on an | |
3684 | * extent buffer of the subvolume's b+tree will make lockdep unhappy | |
3685 | * when we call fiemap_fill_next_extent(), because that may cause a page | |
3686 | * fault when filling the user space buffer with fiemap data. | |
15c7745c | 3687 | */ |
ac3c0d36 FM |
3688 | clone = btrfs_clone_extent_buffer(path->nodes[0]); |
3689 | if (!clone) | |
3690 | return -ENOMEM; | |
3691 | ||
3692 | slot = path->slots[0]; | |
3693 | btrfs_release_path(path); | |
3694 | path->nodes[0] = clone; | |
3695 | path->slots[0] = slot; | |
3696 | ||
3697 | return 0; | |
3698 | } | |
3699 | ||
3700 | /* | |
3701 | * Process a range which is a hole or a prealloc extent in the inode's subvolume | |
3702 | * btree. If @disk_bytenr is 0, we are dealing with a hole, otherwise a prealloc | |
3703 | * extent. The end offset (@end) is inclusive. | |
3704 | */ | |
3705 | static int fiemap_process_hole(struct btrfs_inode *inode, | |
3706 | struct fiemap_extent_info *fieinfo, | |
3707 | struct fiemap_cache *cache, | |
3708 | struct btrfs_backref_shared_cache *backref_cache, | |
3709 | u64 disk_bytenr, u64 extent_offset, | |
3710 | u64 extent_gen, | |
3711 | struct ulist *roots, struct ulist *tmp_ulist, | |
3712 | u64 start, u64 end) | |
3713 | { | |
3714 | const u64 i_size = i_size_read(&inode->vfs_inode); | |
3715 | const u64 ino = btrfs_ino(inode); | |
3716 | u64 cur_offset = start; | |
3717 | u64 last_delalloc_end = 0; | |
3718 | u32 prealloc_flags = FIEMAP_EXTENT_UNWRITTEN; | |
3719 | bool checked_extent_shared = false; | |
3720 | int ret; | |
4d479cf0 | 3721 | |
ec29ed5b | 3722 | /* |
ac3c0d36 FM |
3723 | * There can be no delalloc past i_size, so don't waste time looking for |
3724 | * it beyond i_size. | |
ec29ed5b | 3725 | */ |
ac3c0d36 FM |
3726 | while (cur_offset < end && cur_offset < i_size) { |
3727 | u64 delalloc_start; | |
3728 | u64 delalloc_end; | |
3729 | u64 prealloc_start; | |
3730 | u64 prealloc_len = 0; | |
3731 | bool delalloc; | |
3732 | ||
3733 | delalloc = btrfs_find_delalloc_in_range(inode, cur_offset, end, | |
3734 | &delalloc_start, | |
3735 | &delalloc_end); | |
3736 | if (!delalloc) | |
3737 | break; | |
2d324f59 | 3738 | |
ec29ed5b | 3739 | /* |
ac3c0d36 FM |
3740 | * If this is a prealloc extent we have to report every section |
3741 | * of it that has no delalloc. | |
ec29ed5b | 3742 | */ |
ac3c0d36 FM |
3743 | if (disk_bytenr != 0) { |
3744 | if (last_delalloc_end == 0) { | |
3745 | prealloc_start = start; | |
3746 | prealloc_len = delalloc_start - start; | |
3747 | } else { | |
3748 | prealloc_start = last_delalloc_end + 1; | |
3749 | prealloc_len = delalloc_start - prealloc_start; | |
3750 | } | |
3751 | } | |
3752 | ||
3753 | if (prealloc_len > 0) { | |
3754 | if (!checked_extent_shared && fieinfo->fi_extents_max) { | |
3755 | ret = btrfs_is_data_extent_shared(inode->root, | |
3756 | ino, disk_bytenr, | |
3757 | extent_gen, roots, | |
3758 | tmp_ulist, | |
3759 | backref_cache); | |
3760 | if (ret < 0) | |
3761 | return ret; | |
3762 | else if (ret > 0) | |
3763 | prealloc_flags |= FIEMAP_EXTENT_SHARED; | |
3764 | ||
3765 | checked_extent_shared = true; | |
3766 | } | |
3767 | ret = emit_fiemap_extent(fieinfo, cache, prealloc_start, | |
3768 | disk_bytenr + extent_offset, | |
3769 | prealloc_len, prealloc_flags); | |
3770 | if (ret) | |
3771 | return ret; | |
3772 | extent_offset += prealloc_len; | |
3773 | } | |
3774 | ||
3775 | ret = emit_fiemap_extent(fieinfo, cache, delalloc_start, 0, | |
3776 | delalloc_end + 1 - delalloc_start, | |
3777 | FIEMAP_EXTENT_DELALLOC | | |
3778 | FIEMAP_EXTENT_UNKNOWN); | |
3779 | if (ret) | |
3780 | return ret; | |
3781 | ||
3782 | last_delalloc_end = delalloc_end; | |
3783 | cur_offset = delalloc_end + 1; | |
3784 | extent_offset += cur_offset - delalloc_start; | |
3785 | cond_resched(); | |
3786 | } | |
3787 | ||
3788 | /* | |
3789 | * Either we found no delalloc for the whole prealloc extent or we have | |
3790 | * a prealloc extent that spans i_size or starts at or after i_size. | |
3791 | */ | |
3792 | if (disk_bytenr != 0 && last_delalloc_end < end) { | |
3793 | u64 prealloc_start; | |
3794 | u64 prealloc_len; | |
3795 | ||
3796 | if (last_delalloc_end == 0) { | |
3797 | prealloc_start = start; | |
3798 | prealloc_len = end + 1 - start; | |
3799 | } else { | |
3800 | prealloc_start = last_delalloc_end + 1; | |
3801 | prealloc_len = end + 1 - prealloc_start; | |
3802 | } | |
3803 | ||
3804 | if (!checked_extent_shared && fieinfo->fi_extents_max) { | |
3805 | ret = btrfs_is_data_extent_shared(inode->root, | |
3806 | ino, disk_bytenr, | |
3807 | extent_gen, roots, | |
3808 | tmp_ulist, | |
3809 | backref_cache); | |
3810 | if (ret < 0) | |
3811 | return ret; | |
3812 | else if (ret > 0) | |
3813 | prealloc_flags |= FIEMAP_EXTENT_SHARED; | |
3814 | } | |
3815 | ret = emit_fiemap_extent(fieinfo, cache, prealloc_start, | |
3816 | disk_bytenr + extent_offset, | |
3817 | prealloc_len, prealloc_flags); | |
3818 | if (ret) | |
3819 | return ret; | |
3820 | } | |
3821 | ||
3822 | return 0; | |
3823 | } | |
3824 | ||
3825 | static int fiemap_find_last_extent_offset(struct btrfs_inode *inode, | |
3826 | struct btrfs_path *path, | |
3827 | u64 *last_extent_end_ret) | |
3828 | { | |
3829 | const u64 ino = btrfs_ino(inode); | |
3830 | struct btrfs_root *root = inode->root; | |
3831 | struct extent_buffer *leaf; | |
3832 | struct btrfs_file_extent_item *ei; | |
3833 | struct btrfs_key key; | |
3834 | u64 disk_bytenr; | |
3835 | int ret; | |
3836 | ||
3837 | /* | |
3838 | * Lookup the last file extent. We're not using i_size here because | |
3839 | * there might be preallocation past i_size. | |
3840 | */ | |
3841 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, (u64)-1, 0); | |
3842 | /* There can't be a file extent item at offset (u64)-1 */ | |
3843 | ASSERT(ret != 0); | |
3844 | if (ret < 0) | |
3845 | return ret; | |
3846 | ||
3847 | /* | |
3848 | * For a non-existing key, btrfs_search_slot() always leaves us at a | |
3849 | * slot > 0, except if the btree is empty, which is impossible because | |
3850 | * at least it has the inode item for this inode and all the items for | |
3851 | * the root inode 256. | |
3852 | */ | |
3853 | ASSERT(path->slots[0] > 0); | |
3854 | path->slots[0]--; | |
3855 | leaf = path->nodes[0]; | |
3856 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
3857 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) { | |
3858 | /* No file extent items in the subvolume tree. */ | |
3859 | *last_extent_end_ret = 0; | |
3860 | return 0; | |
975f84fe | 3861 | } |
975f84fe | 3862 | |
ec29ed5b | 3863 | /* |
ac3c0d36 FM |
3864 | * For an inline extent, the disk_bytenr is where inline data starts at, |
3865 | * so first check if we have an inline extent item before checking if we | |
3866 | * have an implicit hole (disk_bytenr == 0). | |
ec29ed5b | 3867 | */ |
ac3c0d36 FM |
3868 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); |
3869 | if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_INLINE) { | |
3870 | *last_extent_end_ret = btrfs_file_extent_end(path); | |
3871 | return 0; | |
ec29ed5b CM |
3872 | } |
3873 | ||
ac3c0d36 FM |
3874 | /* |
3875 | * Find the last file extent item that is not a hole (when NO_HOLES is | |
3876 | * not enabled). This should take at most 2 iterations in the worst | |
3877 | * case: we have one hole file extent item at slot 0 of a leaf and | |
3878 | * another hole file extent item as the last item in the previous leaf. | |
3879 | * This is because we merge file extent items that represent holes. | |
3880 | */ | |
3881 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); | |
3882 | while (disk_bytenr == 0) { | |
3883 | ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY); | |
3884 | if (ret < 0) { | |
3885 | return ret; | |
3886 | } else if (ret > 0) { | |
3887 | /* No file extent items that are not holes. */ | |
3888 | *last_extent_end_ret = 0; | |
3889 | return 0; | |
3890 | } | |
3891 | leaf = path->nodes[0]; | |
3892 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
3893 | struct btrfs_file_extent_item); | |
3894 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); | |
3895 | } | |
ec29ed5b | 3896 | |
ac3c0d36 FM |
3897 | *last_extent_end_ret = btrfs_file_extent_end(path); |
3898 | return 0; | |
3899 | } | |
3900 | ||
3901 | int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo, | |
3902 | u64 start, u64 len) | |
3903 | { | |
3904 | const u64 ino = btrfs_ino(inode); | |
3905 | struct extent_state *cached_state = NULL; | |
3906 | struct btrfs_path *path; | |
3907 | struct btrfs_root *root = inode->root; | |
3908 | struct fiemap_cache cache = { 0 }; | |
3909 | struct btrfs_backref_shared_cache *backref_cache; | |
3910 | struct ulist *roots; | |
3911 | struct ulist *tmp_ulist; | |
3912 | u64 last_extent_end; | |
3913 | u64 prev_extent_end; | |
3914 | u64 lockstart; | |
3915 | u64 lockend; | |
3916 | bool stopped = false; | |
3917 | int ret; | |
3918 | ||
3919 | backref_cache = kzalloc(sizeof(*backref_cache), GFP_KERNEL); | |
3920 | path = btrfs_alloc_path(); | |
3921 | roots = ulist_alloc(GFP_KERNEL); | |
3922 | tmp_ulist = ulist_alloc(GFP_KERNEL); | |
3923 | if (!backref_cache || !path || !roots || !tmp_ulist) { | |
3924 | ret = -ENOMEM; | |
1506fcc8 YS |
3925 | goto out; |
3926 | } | |
975f84fe | 3927 | |
ee8ba05c JB |
3928 | lockstart = round_down(start, root->fs_info->sectorsize); |
3929 | lockend = round_up(start + len, root->fs_info->sectorsize); | |
ac3c0d36 | 3930 | prev_extent_end = lockstart; |
ea8efc74 | 3931 | |
570eb97b | 3932 | lock_extent(&inode->io_tree, lockstart, lockend, &cached_state); |
ea8efc74 | 3933 | |
ac3c0d36 FM |
3934 | ret = fiemap_find_last_extent_offset(inode, path, &last_extent_end); |
3935 | if (ret < 0) | |
3936 | goto out_unlock; | |
3937 | btrfs_release_path(path); | |
1506fcc8 | 3938 | |
ac3c0d36 FM |
3939 | path->reada = READA_FORWARD; |
3940 | ret = fiemap_search_slot(inode, path, lockstart); | |
3941 | if (ret < 0) { | |
3942 | goto out_unlock; | |
3943 | } else if (ret > 0) { | |
ea8efc74 | 3944 | /* |
ac3c0d36 FM |
3945 | * No file extent item found, but we may have delalloc between |
3946 | * the current offset and i_size. So check for that. | |
ea8efc74 | 3947 | */ |
ac3c0d36 FM |
3948 | ret = 0; |
3949 | goto check_eof_delalloc; | |
3950 | } | |
3951 | ||
3952 | while (prev_extent_end < lockend) { | |
3953 | struct extent_buffer *leaf = path->nodes[0]; | |
3954 | struct btrfs_file_extent_item *ei; | |
3955 | struct btrfs_key key; | |
3956 | u64 extent_end; | |
3957 | u64 extent_len; | |
3958 | u64 extent_offset = 0; | |
3959 | u64 extent_gen; | |
3960 | u64 disk_bytenr = 0; | |
3961 | u64 flags = 0; | |
3962 | int extent_type; | |
3963 | u8 compression; | |
3964 | ||
3965 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
3966 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) | |
3967 | break; | |
3968 | ||
3969 | extent_end = btrfs_file_extent_end(path); | |
1506fcc8 | 3970 | |
ea8efc74 | 3971 | /* |
ac3c0d36 FM |
3972 | * The first iteration can leave us at an extent item that ends |
3973 | * before our range's start. Move to the next item. | |
ea8efc74 | 3974 | */ |
ac3c0d36 FM |
3975 | if (extent_end <= lockstart) |
3976 | goto next_item; | |
fe09e16c | 3977 | |
ac3c0d36 FM |
3978 | /* We have in implicit hole (NO_HOLES feature enabled). */ |
3979 | if (prev_extent_end < key.offset) { | |
3980 | const u64 range_end = min(key.offset, lockend) - 1; | |
b8f164e3 | 3981 | |
ac3c0d36 FM |
3982 | ret = fiemap_process_hole(inode, fieinfo, &cache, |
3983 | backref_cache, 0, 0, 0, | |
3984 | roots, tmp_ulist, | |
3985 | prev_extent_end, range_end); | |
3986 | if (ret < 0) { | |
3987 | goto out_unlock; | |
3988 | } else if (ret > 0) { | |
3989 | /* fiemap_fill_next_extent() told us to stop. */ | |
3990 | stopped = true; | |
3991 | break; | |
3992 | } | |
1506fcc8 | 3993 | |
ac3c0d36 FM |
3994 | /* We've reached the end of the fiemap range, stop. */ |
3995 | if (key.offset >= lockend) { | |
3996 | stopped = true; | |
3997 | break; | |
3998 | } | |
1506fcc8 YS |
3999 | } |
4000 | ||
ac3c0d36 FM |
4001 | extent_len = extent_end - key.offset; |
4002 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
4003 | struct btrfs_file_extent_item); | |
4004 | compression = btrfs_file_extent_compression(leaf, ei); | |
4005 | extent_type = btrfs_file_extent_type(leaf, ei); | |
4006 | extent_gen = btrfs_file_extent_generation(leaf, ei); | |
4007 | ||
4008 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
4009 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); | |
4010 | if (compression == BTRFS_COMPRESS_NONE) | |
4011 | extent_offset = btrfs_file_extent_offset(leaf, ei); | |
ec29ed5b | 4012 | } |
ac3c0d36 FM |
4013 | |
4014 | if (compression != BTRFS_COMPRESS_NONE) | |
4015 | flags |= FIEMAP_EXTENT_ENCODED; | |
4016 | ||
4017 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
4018 | flags |= FIEMAP_EXTENT_DATA_INLINE; | |
4019 | flags |= FIEMAP_EXTENT_NOT_ALIGNED; | |
4020 | ret = emit_fiemap_extent(fieinfo, &cache, key.offset, 0, | |
4021 | extent_len, flags); | |
4022 | } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
4023 | ret = fiemap_process_hole(inode, fieinfo, &cache, | |
4024 | backref_cache, | |
4025 | disk_bytenr, extent_offset, | |
4026 | extent_gen, roots, tmp_ulist, | |
4027 | key.offset, extent_end - 1); | |
4028 | } else if (disk_bytenr == 0) { | |
4029 | /* We have an explicit hole. */ | |
4030 | ret = fiemap_process_hole(inode, fieinfo, &cache, | |
4031 | backref_cache, 0, 0, 0, | |
4032 | roots, tmp_ulist, | |
4033 | key.offset, extent_end - 1); | |
4034 | } else { | |
4035 | /* We have a regular extent. */ | |
4036 | if (fieinfo->fi_extents_max) { | |
4037 | ret = btrfs_is_data_extent_shared(root, ino, | |
4038 | disk_bytenr, | |
4039 | extent_gen, | |
4040 | roots, | |
4041 | tmp_ulist, | |
4042 | backref_cache); | |
4043 | if (ret < 0) | |
4044 | goto out_unlock; | |
4045 | else if (ret > 0) | |
4046 | flags |= FIEMAP_EXTENT_SHARED; | |
4047 | } | |
4048 | ||
4049 | ret = emit_fiemap_extent(fieinfo, &cache, key.offset, | |
4050 | disk_bytenr + extent_offset, | |
4051 | extent_len, flags); | |
975f84fe | 4052 | } |
ac3c0d36 FM |
4053 | |
4054 | if (ret < 0) { | |
4055 | goto out_unlock; | |
4056 | } else if (ret > 0) { | |
4057 | /* fiemap_fill_next_extent() told us to stop. */ | |
4058 | stopped = true; | |
4059 | break; | |
26e726af | 4060 | } |
09fbc1c8 | 4061 | |
ac3c0d36 FM |
4062 | prev_extent_end = extent_end; |
4063 | next_item: | |
09fbc1c8 FM |
4064 | if (fatal_signal_pending(current)) { |
4065 | ret = -EINTR; | |
ac3c0d36 | 4066 | goto out_unlock; |
09fbc1c8 | 4067 | } |
ac3c0d36 FM |
4068 | |
4069 | ret = fiemap_next_leaf_item(inode, path); | |
4070 | if (ret < 0) { | |
4071 | goto out_unlock; | |
4072 | } else if (ret > 0) { | |
4073 | /* No more file extent items for this inode. */ | |
4074 | break; | |
4075 | } | |
4076 | cond_resched(); | |
1506fcc8 | 4077 | } |
5911c8fe | 4078 | |
ac3c0d36 FM |
4079 | check_eof_delalloc: |
4080 | /* | |
4081 | * Release (and free) the path before emitting any final entries to | |
4082 | * fiemap_fill_next_extent() to keep lockdep happy. This is because | |
4083 | * once we find no more file extent items exist, we may have a | |
4084 | * non-cloned leaf, and fiemap_fill_next_extent() can trigger page | |
4085 | * faults when copying data to the user space buffer. | |
4086 | */ | |
4087 | btrfs_free_path(path); | |
4088 | path = NULL; | |
4089 | ||
4090 | if (!stopped && prev_extent_end < lockend) { | |
4091 | ret = fiemap_process_hole(inode, fieinfo, &cache, backref_cache, | |
4092 | 0, 0, 0, roots, tmp_ulist, | |
4093 | prev_extent_end, lockend - 1); | |
4094 | if (ret < 0) | |
4095 | goto out_unlock; | |
4096 | prev_extent_end = lockend; | |
4097 | } | |
4098 | ||
4099 | if (cache.cached && cache.offset + cache.len >= last_extent_end) { | |
4100 | const u64 i_size = i_size_read(&inode->vfs_inode); | |
4101 | ||
4102 | if (prev_extent_end < i_size) { | |
4103 | u64 delalloc_start; | |
4104 | u64 delalloc_end; | |
4105 | bool delalloc; | |
4106 | ||
4107 | delalloc = btrfs_find_delalloc_in_range(inode, | |
4108 | prev_extent_end, | |
4109 | i_size - 1, | |
4110 | &delalloc_start, | |
4111 | &delalloc_end); | |
4112 | if (!delalloc) | |
4113 | cache.flags |= FIEMAP_EXTENT_LAST; | |
4114 | } else { | |
4115 | cache.flags |= FIEMAP_EXTENT_LAST; | |
4116 | } | |
4117 | } | |
4118 | ||
4119 | ret = emit_last_fiemap_cache(fieinfo, &cache); | |
4120 | ||
4121 | out_unlock: | |
570eb97b | 4122 | unlock_extent(&inode->io_tree, lockstart, lockend, &cached_state); |
ac3c0d36 | 4123 | out: |
12a824dc | 4124 | kfree(backref_cache); |
e02d48ea | 4125 | btrfs_free_path(path); |
5911c8fe DS |
4126 | ulist_free(roots); |
4127 | ulist_free(tmp_ulist); | |
1506fcc8 YS |
4128 | return ret; |
4129 | } | |
4130 | ||
727011e0 CM |
4131 | static void __free_extent_buffer(struct extent_buffer *eb) |
4132 | { | |
727011e0 CM |
4133 | kmem_cache_free(extent_buffer_cache, eb); |
4134 | } | |
4135 | ||
2b48966a | 4136 | int extent_buffer_under_io(const struct extent_buffer *eb) |
db7f3436 JB |
4137 | { |
4138 | return (atomic_read(&eb->io_pages) || | |
4139 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || | |
4140 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); | |
4141 | } | |
4142 | ||
8ff8466d | 4143 | static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page) |
db7f3436 | 4144 | { |
8ff8466d | 4145 | struct btrfs_subpage *subpage; |
db7f3436 | 4146 | |
8ff8466d | 4147 | lockdep_assert_held(&page->mapping->private_lock); |
db7f3436 | 4148 | |
8ff8466d QW |
4149 | if (PagePrivate(page)) { |
4150 | subpage = (struct btrfs_subpage *)page->private; | |
4151 | if (atomic_read(&subpage->eb_refs)) | |
4152 | return true; | |
3d078efa QW |
4153 | /* |
4154 | * Even there is no eb refs here, we may still have | |
4155 | * end_page_read() call relying on page::private. | |
4156 | */ | |
4157 | if (atomic_read(&subpage->readers)) | |
4158 | return true; | |
8ff8466d QW |
4159 | } |
4160 | return false; | |
4161 | } | |
db7f3436 | 4162 | |
8ff8466d QW |
4163 | static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *page) |
4164 | { | |
4165 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
4166 | const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); | |
4167 | ||
4168 | /* | |
4169 | * For mapped eb, we're going to change the page private, which should | |
4170 | * be done under the private_lock. | |
4171 | */ | |
4172 | if (mapped) | |
4173 | spin_lock(&page->mapping->private_lock); | |
4174 | ||
4175 | if (!PagePrivate(page)) { | |
5d2361db | 4176 | if (mapped) |
8ff8466d QW |
4177 | spin_unlock(&page->mapping->private_lock); |
4178 | return; | |
4179 | } | |
4180 | ||
fbca46eb | 4181 | if (fs_info->nodesize >= PAGE_SIZE) { |
5d2361db FL |
4182 | /* |
4183 | * We do this since we'll remove the pages after we've | |
4184 | * removed the eb from the radix tree, so we could race | |
4185 | * and have this page now attached to the new eb. So | |
4186 | * only clear page_private if it's still connected to | |
4187 | * this eb. | |
4188 | */ | |
4189 | if (PagePrivate(page) && | |
4190 | page->private == (unsigned long)eb) { | |
4191 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); | |
4192 | BUG_ON(PageDirty(page)); | |
4193 | BUG_ON(PageWriteback(page)); | |
db7f3436 | 4194 | /* |
5d2361db FL |
4195 | * We need to make sure we haven't be attached |
4196 | * to a new eb. | |
db7f3436 | 4197 | */ |
d1b89bc0 | 4198 | detach_page_private(page); |
db7f3436 | 4199 | } |
5d2361db FL |
4200 | if (mapped) |
4201 | spin_unlock(&page->mapping->private_lock); | |
8ff8466d QW |
4202 | return; |
4203 | } | |
4204 | ||
4205 | /* | |
4206 | * For subpage, we can have dummy eb with page private. In this case, | |
4207 | * we can directly detach the private as such page is only attached to | |
4208 | * one dummy eb, no sharing. | |
4209 | */ | |
4210 | if (!mapped) { | |
4211 | btrfs_detach_subpage(fs_info, page); | |
4212 | return; | |
4213 | } | |
4214 | ||
4215 | btrfs_page_dec_eb_refs(fs_info, page); | |
4216 | ||
4217 | /* | |
4218 | * We can only detach the page private if there are no other ebs in the | |
3d078efa | 4219 | * page range and no unfinished IO. |
8ff8466d QW |
4220 | */ |
4221 | if (!page_range_has_eb(fs_info, page)) | |
4222 | btrfs_detach_subpage(fs_info, page); | |
4223 | ||
4224 | spin_unlock(&page->mapping->private_lock); | |
4225 | } | |
4226 | ||
4227 | /* Release all pages attached to the extent buffer */ | |
4228 | static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb) | |
4229 | { | |
4230 | int i; | |
4231 | int num_pages; | |
4232 | ||
4233 | ASSERT(!extent_buffer_under_io(eb)); | |
4234 | ||
4235 | num_pages = num_extent_pages(eb); | |
4236 | for (i = 0; i < num_pages; i++) { | |
4237 | struct page *page = eb->pages[i]; | |
4238 | ||
4239 | if (!page) | |
4240 | continue; | |
4241 | ||
4242 | detach_extent_buffer_page(eb, page); | |
5d2361db | 4243 | |
01327610 | 4244 | /* One for when we allocated the page */ |
09cbfeaf | 4245 | put_page(page); |
d64766fd | 4246 | } |
db7f3436 JB |
4247 | } |
4248 | ||
4249 | /* | |
4250 | * Helper for releasing the extent buffer. | |
4251 | */ | |
4252 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) | |
4253 | { | |
55ac0139 | 4254 | btrfs_release_extent_buffer_pages(eb); |
a40246e8 | 4255 | btrfs_leak_debug_del_eb(eb); |
db7f3436 JB |
4256 | __free_extent_buffer(eb); |
4257 | } | |
4258 | ||
f28491e0 JB |
4259 | static struct extent_buffer * |
4260 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, | |
23d79d81 | 4261 | unsigned long len) |
d1310b2e CM |
4262 | { |
4263 | struct extent_buffer *eb = NULL; | |
4264 | ||
d1b5c567 | 4265 | eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); |
d1310b2e CM |
4266 | eb->start = start; |
4267 | eb->len = len; | |
f28491e0 | 4268 | eb->fs_info = fs_info; |
196d59ab | 4269 | init_rwsem(&eb->lock); |
b4ce94de | 4270 | |
a40246e8 | 4271 | btrfs_leak_debug_add_eb(eb); |
d3575156 | 4272 | INIT_LIST_HEAD(&eb->release_list); |
6d49ba1b | 4273 | |
3083ee2e | 4274 | spin_lock_init(&eb->refs_lock); |
d1310b2e | 4275 | atomic_set(&eb->refs, 1); |
0b32f4bb | 4276 | atomic_set(&eb->io_pages, 0); |
727011e0 | 4277 | |
deb67895 | 4278 | ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE); |
d1310b2e CM |
4279 | |
4280 | return eb; | |
4281 | } | |
4282 | ||
2b48966a | 4283 | struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src) |
815a51c7 | 4284 | { |
cc5e31a4 | 4285 | int i; |
815a51c7 | 4286 | struct extent_buffer *new; |
cc5e31a4 | 4287 | int num_pages = num_extent_pages(src); |
dd137dd1 | 4288 | int ret; |
815a51c7 | 4289 | |
3f556f78 | 4290 | new = __alloc_extent_buffer(src->fs_info, src->start, src->len); |
815a51c7 JS |
4291 | if (new == NULL) |
4292 | return NULL; | |
4293 | ||
62c053fb QW |
4294 | /* |
4295 | * Set UNMAPPED before calling btrfs_release_extent_buffer(), as | |
4296 | * btrfs_release_extent_buffer() have different behavior for | |
4297 | * UNMAPPED subpage extent buffer. | |
4298 | */ | |
4299 | set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags); | |
4300 | ||
dd137dd1 STD |
4301 | ret = btrfs_alloc_page_array(num_pages, new->pages); |
4302 | if (ret) { | |
4303 | btrfs_release_extent_buffer(new); | |
4304 | return NULL; | |
4305 | } | |
4306 | ||
815a51c7 | 4307 | for (i = 0; i < num_pages; i++) { |
760f991f | 4308 | int ret; |
dd137dd1 | 4309 | struct page *p = new->pages[i]; |
760f991f | 4310 | |
760f991f QW |
4311 | ret = attach_extent_buffer_page(new, p, NULL); |
4312 | if (ret < 0) { | |
760f991f QW |
4313 | btrfs_release_extent_buffer(new); |
4314 | return NULL; | |
4315 | } | |
815a51c7 | 4316 | WARN_ON(PageDirty(p)); |
fba1acf9 | 4317 | copy_page(page_address(p), page_address(src->pages[i])); |
815a51c7 | 4318 | } |
92d83e94 | 4319 | set_extent_buffer_uptodate(new); |
815a51c7 JS |
4320 | |
4321 | return new; | |
4322 | } | |
4323 | ||
0f331229 OS |
4324 | struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
4325 | u64 start, unsigned long len) | |
815a51c7 JS |
4326 | { |
4327 | struct extent_buffer *eb; | |
cc5e31a4 DS |
4328 | int num_pages; |
4329 | int i; | |
dd137dd1 | 4330 | int ret; |
815a51c7 | 4331 | |
3f556f78 | 4332 | eb = __alloc_extent_buffer(fs_info, start, len); |
815a51c7 JS |
4333 | if (!eb) |
4334 | return NULL; | |
4335 | ||
65ad0104 | 4336 | num_pages = num_extent_pages(eb); |
dd137dd1 STD |
4337 | ret = btrfs_alloc_page_array(num_pages, eb->pages); |
4338 | if (ret) | |
4339 | goto err; | |
4340 | ||
815a51c7 | 4341 | for (i = 0; i < num_pages; i++) { |
dd137dd1 | 4342 | struct page *p = eb->pages[i]; |
09bc1f0f | 4343 | |
dd137dd1 | 4344 | ret = attach_extent_buffer_page(eb, p, NULL); |
09bc1f0f QW |
4345 | if (ret < 0) |
4346 | goto err; | |
815a51c7 | 4347 | } |
dd137dd1 | 4348 | |
815a51c7 JS |
4349 | set_extent_buffer_uptodate(eb); |
4350 | btrfs_set_header_nritems(eb, 0); | |
b0132a3b | 4351 | set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); |
815a51c7 JS |
4352 | |
4353 | return eb; | |
4354 | err: | |
dd137dd1 STD |
4355 | for (i = 0; i < num_pages; i++) { |
4356 | if (eb->pages[i]) { | |
4357 | detach_extent_buffer_page(eb, eb->pages[i]); | |
4358 | __free_page(eb->pages[i]); | |
4359 | } | |
09bc1f0f | 4360 | } |
815a51c7 JS |
4361 | __free_extent_buffer(eb); |
4362 | return NULL; | |
4363 | } | |
4364 | ||
0f331229 | 4365 | struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
da17066c | 4366 | u64 start) |
0f331229 | 4367 | { |
da17066c | 4368 | return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize); |
0f331229 OS |
4369 | } |
4370 | ||
0b32f4bb JB |
4371 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
4372 | { | |
242e18c7 | 4373 | int refs; |
6bf9cd2e BB |
4374 | /* |
4375 | * The TREE_REF bit is first set when the extent_buffer is added | |
4376 | * to the radix tree. It is also reset, if unset, when a new reference | |
4377 | * is created by find_extent_buffer. | |
0b32f4bb | 4378 | * |
6bf9cd2e BB |
4379 | * It is only cleared in two cases: freeing the last non-tree |
4380 | * reference to the extent_buffer when its STALE bit is set or | |
f913cff3 | 4381 | * calling release_folio when the tree reference is the only reference. |
0b32f4bb | 4382 | * |
6bf9cd2e | 4383 | * In both cases, care is taken to ensure that the extent_buffer's |
f913cff3 | 4384 | * pages are not under io. However, release_folio can be concurrently |
6bf9cd2e BB |
4385 | * called with creating new references, which is prone to race |
4386 | * conditions between the calls to check_buffer_tree_ref in those | |
4387 | * codepaths and clearing TREE_REF in try_release_extent_buffer. | |
0b32f4bb | 4388 | * |
6bf9cd2e BB |
4389 | * The actual lifetime of the extent_buffer in the radix tree is |
4390 | * adequately protected by the refcount, but the TREE_REF bit and | |
4391 | * its corresponding reference are not. To protect against this | |
4392 | * class of races, we call check_buffer_tree_ref from the codepaths | |
4393 | * which trigger io after they set eb->io_pages. Note that once io is | |
4394 | * initiated, TREE_REF can no longer be cleared, so that is the | |
4395 | * moment at which any such race is best fixed. | |
0b32f4bb | 4396 | */ |
242e18c7 CM |
4397 | refs = atomic_read(&eb->refs); |
4398 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) | |
4399 | return; | |
4400 | ||
594831c4 JB |
4401 | spin_lock(&eb->refs_lock); |
4402 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) | |
0b32f4bb | 4403 | atomic_inc(&eb->refs); |
594831c4 | 4404 | spin_unlock(&eb->refs_lock); |
0b32f4bb JB |
4405 | } |
4406 | ||
2457aec6 MG |
4407 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
4408 | struct page *accessed) | |
5df4235e | 4409 | { |
cc5e31a4 | 4410 | int num_pages, i; |
5df4235e | 4411 | |
0b32f4bb JB |
4412 | check_buffer_tree_ref(eb); |
4413 | ||
65ad0104 | 4414 | num_pages = num_extent_pages(eb); |
5df4235e | 4415 | for (i = 0; i < num_pages; i++) { |
fb85fc9a DS |
4416 | struct page *p = eb->pages[i]; |
4417 | ||
2457aec6 MG |
4418 | if (p != accessed) |
4419 | mark_page_accessed(p); | |
5df4235e JB |
4420 | } |
4421 | } | |
4422 | ||
f28491e0 JB |
4423 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
4424 | u64 start) | |
452c75c3 CS |
4425 | { |
4426 | struct extent_buffer *eb; | |
4427 | ||
2f3186d8 QW |
4428 | eb = find_extent_buffer_nolock(fs_info, start); |
4429 | if (!eb) | |
4430 | return NULL; | |
4431 | /* | |
4432 | * Lock our eb's refs_lock to avoid races with free_extent_buffer(). | |
4433 | * When we get our eb it might be flagged with EXTENT_BUFFER_STALE and | |
4434 | * another task running free_extent_buffer() might have seen that flag | |
4435 | * set, eb->refs == 2, that the buffer isn't under IO (dirty and | |
4436 | * writeback flags not set) and it's still in the tree (flag | |
4437 | * EXTENT_BUFFER_TREE_REF set), therefore being in the process of | |
4438 | * decrementing the extent buffer's reference count twice. So here we | |
4439 | * could race and increment the eb's reference count, clear its stale | |
4440 | * flag, mark it as dirty and drop our reference before the other task | |
4441 | * finishes executing free_extent_buffer, which would later result in | |
4442 | * an attempt to free an extent buffer that is dirty. | |
4443 | */ | |
4444 | if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { | |
4445 | spin_lock(&eb->refs_lock); | |
4446 | spin_unlock(&eb->refs_lock); | |
452c75c3 | 4447 | } |
2f3186d8 QW |
4448 | mark_extent_buffer_accessed(eb, NULL); |
4449 | return eb; | |
452c75c3 CS |
4450 | } |
4451 | ||
faa2dbf0 JB |
4452 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
4453 | struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, | |
da17066c | 4454 | u64 start) |
faa2dbf0 JB |
4455 | { |
4456 | struct extent_buffer *eb, *exists = NULL; | |
4457 | int ret; | |
4458 | ||
4459 | eb = find_extent_buffer(fs_info, start); | |
4460 | if (eb) | |
4461 | return eb; | |
da17066c | 4462 | eb = alloc_dummy_extent_buffer(fs_info, start); |
faa2dbf0 | 4463 | if (!eb) |
b6293c82 | 4464 | return ERR_PTR(-ENOMEM); |
faa2dbf0 | 4465 | eb->fs_info = fs_info; |
01cd3909 DS |
4466 | again: |
4467 | ret = radix_tree_preload(GFP_NOFS); | |
4468 | if (ret) { | |
4469 | exists = ERR_PTR(ret); | |
4470 | goto free_eb; | |
4471 | } | |
4472 | spin_lock(&fs_info->buffer_lock); | |
4473 | ret = radix_tree_insert(&fs_info->buffer_radix, | |
4474 | start >> fs_info->sectorsize_bits, eb); | |
4475 | spin_unlock(&fs_info->buffer_lock); | |
4476 | radix_tree_preload_end(); | |
4477 | if (ret == -EEXIST) { | |
4478 | exists = find_extent_buffer(fs_info, start); | |
4479 | if (exists) | |
faa2dbf0 | 4480 | goto free_eb; |
01cd3909 DS |
4481 | else |
4482 | goto again; | |
4483 | } | |
faa2dbf0 JB |
4484 | check_buffer_tree_ref(eb); |
4485 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); | |
4486 | ||
faa2dbf0 JB |
4487 | return eb; |
4488 | free_eb: | |
4489 | btrfs_release_extent_buffer(eb); | |
4490 | return exists; | |
4491 | } | |
4492 | #endif | |
4493 | ||
81982210 QW |
4494 | static struct extent_buffer *grab_extent_buffer( |
4495 | struct btrfs_fs_info *fs_info, struct page *page) | |
c0f0a9e7 QW |
4496 | { |
4497 | struct extent_buffer *exists; | |
4498 | ||
81982210 QW |
4499 | /* |
4500 | * For subpage case, we completely rely on radix tree to ensure we | |
4501 | * don't try to insert two ebs for the same bytenr. So here we always | |
4502 | * return NULL and just continue. | |
4503 | */ | |
fbca46eb | 4504 | if (fs_info->nodesize < PAGE_SIZE) |
81982210 QW |
4505 | return NULL; |
4506 | ||
c0f0a9e7 QW |
4507 | /* Page not yet attached to an extent buffer */ |
4508 | if (!PagePrivate(page)) | |
4509 | return NULL; | |
4510 | ||
4511 | /* | |
4512 | * We could have already allocated an eb for this page and attached one | |
4513 | * so lets see if we can get a ref on the existing eb, and if we can we | |
4514 | * know it's good and we can just return that one, else we know we can | |
4515 | * just overwrite page->private. | |
4516 | */ | |
4517 | exists = (struct extent_buffer *)page->private; | |
4518 | if (atomic_inc_not_zero(&exists->refs)) | |
4519 | return exists; | |
4520 | ||
4521 | WARN_ON(PageDirty(page)); | |
4522 | detach_page_private(page); | |
4523 | return NULL; | |
4524 | } | |
4525 | ||
fbca46eb QW |
4526 | static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) |
4527 | { | |
4528 | if (!IS_ALIGNED(start, fs_info->sectorsize)) { | |
4529 | btrfs_err(fs_info, "bad tree block start %llu", start); | |
4530 | return -EINVAL; | |
4531 | } | |
4532 | ||
4533 | if (fs_info->nodesize < PAGE_SIZE && | |
4534 | offset_in_page(start) + fs_info->nodesize > PAGE_SIZE) { | |
4535 | btrfs_err(fs_info, | |
4536 | "tree block crosses page boundary, start %llu nodesize %u", | |
4537 | start, fs_info->nodesize); | |
4538 | return -EINVAL; | |
4539 | } | |
4540 | if (fs_info->nodesize >= PAGE_SIZE && | |
1280d2d1 | 4541 | !PAGE_ALIGNED(start)) { |
fbca46eb QW |
4542 | btrfs_err(fs_info, |
4543 | "tree block is not page aligned, start %llu nodesize %u", | |
4544 | start, fs_info->nodesize); | |
4545 | return -EINVAL; | |
4546 | } | |
4547 | return 0; | |
4548 | } | |
4549 | ||
f28491e0 | 4550 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
3fbaf258 | 4551 | u64 start, u64 owner_root, int level) |
d1310b2e | 4552 | { |
da17066c | 4553 | unsigned long len = fs_info->nodesize; |
cc5e31a4 DS |
4554 | int num_pages; |
4555 | int i; | |
09cbfeaf | 4556 | unsigned long index = start >> PAGE_SHIFT; |
d1310b2e | 4557 | struct extent_buffer *eb; |
6af118ce | 4558 | struct extent_buffer *exists = NULL; |
d1310b2e | 4559 | struct page *p; |
f28491e0 | 4560 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
b40130b2 | 4561 | u64 lockdep_owner = owner_root; |
d1310b2e | 4562 | int uptodate = 1; |
19fe0a8b | 4563 | int ret; |
d1310b2e | 4564 | |
fbca46eb | 4565 | if (check_eb_alignment(fs_info, start)) |
c871b0f2 | 4566 | return ERR_PTR(-EINVAL); |
c871b0f2 | 4567 | |
e9306ad4 QW |
4568 | #if BITS_PER_LONG == 32 |
4569 | if (start >= MAX_LFS_FILESIZE) { | |
4570 | btrfs_err_rl(fs_info, | |
4571 | "extent buffer %llu is beyond 32bit page cache limit", start); | |
4572 | btrfs_err_32bit_limit(fs_info); | |
4573 | return ERR_PTR(-EOVERFLOW); | |
4574 | } | |
4575 | if (start >= BTRFS_32BIT_EARLY_WARN_THRESHOLD) | |
4576 | btrfs_warn_32bit_limit(fs_info); | |
4577 | #endif | |
4578 | ||
f28491e0 | 4579 | eb = find_extent_buffer(fs_info, start); |
452c75c3 | 4580 | if (eb) |
6af118ce | 4581 | return eb; |
6af118ce | 4582 | |
23d79d81 | 4583 | eb = __alloc_extent_buffer(fs_info, start, len); |
2b114d1d | 4584 | if (!eb) |
c871b0f2 | 4585 | return ERR_PTR(-ENOMEM); |
b40130b2 JB |
4586 | |
4587 | /* | |
4588 | * The reloc trees are just snapshots, so we need them to appear to be | |
4589 | * just like any other fs tree WRT lockdep. | |
4590 | */ | |
4591 | if (lockdep_owner == BTRFS_TREE_RELOC_OBJECTID) | |
4592 | lockdep_owner = BTRFS_FS_TREE_OBJECTID; | |
4593 | ||
4594 | btrfs_set_buffer_lockdep_class(lockdep_owner, eb, level); | |
d1310b2e | 4595 | |
65ad0104 | 4596 | num_pages = num_extent_pages(eb); |
727011e0 | 4597 | for (i = 0; i < num_pages; i++, index++) { |
760f991f QW |
4598 | struct btrfs_subpage *prealloc = NULL; |
4599 | ||
d1b5c567 | 4600 | p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); |
c871b0f2 LB |
4601 | if (!p) { |
4602 | exists = ERR_PTR(-ENOMEM); | |
6af118ce | 4603 | goto free_eb; |
c871b0f2 | 4604 | } |
4f2de97a | 4605 | |
760f991f QW |
4606 | /* |
4607 | * Preallocate page->private for subpage case, so that we won't | |
4608 | * allocate memory with private_lock hold. The memory will be | |
4609 | * freed by attach_extent_buffer_page() or freed manually if | |
4610 | * we exit earlier. | |
4611 | * | |
4612 | * Although we have ensured one subpage eb can only have one | |
4613 | * page, but it may change in the future for 16K page size | |
4614 | * support, so we still preallocate the memory in the loop. | |
4615 | */ | |
fbca46eb | 4616 | if (fs_info->nodesize < PAGE_SIZE) { |
651fb419 QW |
4617 | prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA); |
4618 | if (IS_ERR(prealloc)) { | |
4619 | ret = PTR_ERR(prealloc); | |
fdf250db QW |
4620 | unlock_page(p); |
4621 | put_page(p); | |
4622 | exists = ERR_PTR(ret); | |
4623 | goto free_eb; | |
4624 | } | |
760f991f QW |
4625 | } |
4626 | ||
4f2de97a | 4627 | spin_lock(&mapping->private_lock); |
81982210 | 4628 | exists = grab_extent_buffer(fs_info, p); |
c0f0a9e7 QW |
4629 | if (exists) { |
4630 | spin_unlock(&mapping->private_lock); | |
4631 | unlock_page(p); | |
4632 | put_page(p); | |
4633 | mark_extent_buffer_accessed(exists, p); | |
760f991f | 4634 | btrfs_free_subpage(prealloc); |
c0f0a9e7 | 4635 | goto free_eb; |
d1310b2e | 4636 | } |
760f991f QW |
4637 | /* Should not fail, as we have preallocated the memory */ |
4638 | ret = attach_extent_buffer_page(eb, p, prealloc); | |
4639 | ASSERT(!ret); | |
8ff8466d QW |
4640 | /* |
4641 | * To inform we have extra eb under allocation, so that | |
4642 | * detach_extent_buffer_page() won't release the page private | |
4643 | * when the eb hasn't yet been inserted into radix tree. | |
4644 | * | |
4645 | * The ref will be decreased when the eb released the page, in | |
4646 | * detach_extent_buffer_page(). | |
4647 | * Thus needs no special handling in error path. | |
4648 | */ | |
4649 | btrfs_page_inc_eb_refs(fs_info, p); | |
4f2de97a | 4650 | spin_unlock(&mapping->private_lock); |
760f991f | 4651 | |
1e5eb3d6 | 4652 | WARN_ON(btrfs_page_test_dirty(fs_info, p, eb->start, eb->len)); |
727011e0 | 4653 | eb->pages[i] = p; |
d1310b2e CM |
4654 | if (!PageUptodate(p)) |
4655 | uptodate = 0; | |
eb14ab8e CM |
4656 | |
4657 | /* | |
b16d011e NB |
4658 | * We can't unlock the pages just yet since the extent buffer |
4659 | * hasn't been properly inserted in the radix tree, this | |
f913cff3 | 4660 | * opens a race with btree_release_folio which can free a page |
b16d011e NB |
4661 | * while we are still filling in all pages for the buffer and |
4662 | * we could crash. | |
eb14ab8e | 4663 | */ |
d1310b2e CM |
4664 | } |
4665 | if (uptodate) | |
b4ce94de | 4666 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
01cd3909 DS |
4667 | again: |
4668 | ret = radix_tree_preload(GFP_NOFS); | |
4669 | if (ret) { | |
4670 | exists = ERR_PTR(ret); | |
4671 | goto free_eb; | |
4672 | } | |
4673 | ||
4674 | spin_lock(&fs_info->buffer_lock); | |
4675 | ret = radix_tree_insert(&fs_info->buffer_radix, | |
4676 | start >> fs_info->sectorsize_bits, eb); | |
4677 | spin_unlock(&fs_info->buffer_lock); | |
4678 | radix_tree_preload_end(); | |
4679 | if (ret == -EEXIST) { | |
4680 | exists = find_extent_buffer(fs_info, start); | |
4681 | if (exists) | |
452c75c3 | 4682 | goto free_eb; |
01cd3909 DS |
4683 | else |
4684 | goto again; | |
4685 | } | |
6af118ce | 4686 | /* add one reference for the tree */ |
0b32f4bb | 4687 | check_buffer_tree_ref(eb); |
34b41ace | 4688 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
eb14ab8e CM |
4689 | |
4690 | /* | |
b16d011e | 4691 | * Now it's safe to unlock the pages because any calls to |
f913cff3 | 4692 | * btree_release_folio will correctly detect that a page belongs to a |
b16d011e | 4693 | * live buffer and won't free them prematurely. |
eb14ab8e | 4694 | */ |
28187ae5 NB |
4695 | for (i = 0; i < num_pages; i++) |
4696 | unlock_page(eb->pages[i]); | |
d1310b2e CM |
4697 | return eb; |
4698 | ||
6af118ce | 4699 | free_eb: |
5ca64f45 | 4700 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
727011e0 CM |
4701 | for (i = 0; i < num_pages; i++) { |
4702 | if (eb->pages[i]) | |
4703 | unlock_page(eb->pages[i]); | |
4704 | } | |
eb14ab8e | 4705 | |
897ca6e9 | 4706 | btrfs_release_extent_buffer(eb); |
6af118ce | 4707 | return exists; |
d1310b2e | 4708 | } |
d1310b2e | 4709 | |
3083ee2e JB |
4710 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
4711 | { | |
4712 | struct extent_buffer *eb = | |
4713 | container_of(head, struct extent_buffer, rcu_head); | |
4714 | ||
4715 | __free_extent_buffer(eb); | |
4716 | } | |
4717 | ||
f7a52a40 | 4718 | static int release_extent_buffer(struct extent_buffer *eb) |
5ce48d0f | 4719 | __releases(&eb->refs_lock) |
3083ee2e | 4720 | { |
07e21c4d NB |
4721 | lockdep_assert_held(&eb->refs_lock); |
4722 | ||
3083ee2e JB |
4723 | WARN_ON(atomic_read(&eb->refs) == 0); |
4724 | if (atomic_dec_and_test(&eb->refs)) { | |
34b41ace | 4725 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
f28491e0 | 4726 | struct btrfs_fs_info *fs_info = eb->fs_info; |
3083ee2e | 4727 | |
815a51c7 | 4728 | spin_unlock(&eb->refs_lock); |
3083ee2e | 4729 | |
01cd3909 DS |
4730 | spin_lock(&fs_info->buffer_lock); |
4731 | radix_tree_delete(&fs_info->buffer_radix, | |
4732 | eb->start >> fs_info->sectorsize_bits); | |
4733 | spin_unlock(&fs_info->buffer_lock); | |
34b41ace JB |
4734 | } else { |
4735 | spin_unlock(&eb->refs_lock); | |
815a51c7 | 4736 | } |
3083ee2e | 4737 | |
a40246e8 | 4738 | btrfs_leak_debug_del_eb(eb); |
3083ee2e | 4739 | /* Should be safe to release our pages at this point */ |
55ac0139 | 4740 | btrfs_release_extent_buffer_pages(eb); |
bcb7e449 | 4741 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
b0132a3b | 4742 | if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) { |
bcb7e449 JB |
4743 | __free_extent_buffer(eb); |
4744 | return 1; | |
4745 | } | |
4746 | #endif | |
3083ee2e | 4747 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
e64860aa | 4748 | return 1; |
3083ee2e JB |
4749 | } |
4750 | spin_unlock(&eb->refs_lock); | |
e64860aa JB |
4751 | |
4752 | return 0; | |
3083ee2e JB |
4753 | } |
4754 | ||
d1310b2e CM |
4755 | void free_extent_buffer(struct extent_buffer *eb) |
4756 | { | |
242e18c7 | 4757 | int refs; |
d1310b2e CM |
4758 | if (!eb) |
4759 | return; | |
4760 | ||
e5677f05 | 4761 | refs = atomic_read(&eb->refs); |
242e18c7 | 4762 | while (1) { |
46cc775e NB |
4763 | if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3) |
4764 | || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && | |
4765 | refs == 1)) | |
242e18c7 | 4766 | break; |
e5677f05 | 4767 | if (atomic_try_cmpxchg(&eb->refs, &refs, refs - 1)) |
242e18c7 CM |
4768 | return; |
4769 | } | |
4770 | ||
3083ee2e JB |
4771 | spin_lock(&eb->refs_lock); |
4772 | if (atomic_read(&eb->refs) == 2 && | |
4773 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && | |
0b32f4bb | 4774 | !extent_buffer_under_io(eb) && |
3083ee2e JB |
4775 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
4776 | atomic_dec(&eb->refs); | |
4777 | ||
4778 | /* | |
4779 | * I know this is terrible, but it's temporary until we stop tracking | |
4780 | * the uptodate bits and such for the extent buffers. | |
4781 | */ | |
f7a52a40 | 4782 | release_extent_buffer(eb); |
3083ee2e JB |
4783 | } |
4784 | ||
4785 | void free_extent_buffer_stale(struct extent_buffer *eb) | |
4786 | { | |
4787 | if (!eb) | |
d1310b2e CM |
4788 | return; |
4789 | ||
3083ee2e JB |
4790 | spin_lock(&eb->refs_lock); |
4791 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); | |
4792 | ||
0b32f4bb | 4793 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
3083ee2e JB |
4794 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
4795 | atomic_dec(&eb->refs); | |
f7a52a40 | 4796 | release_extent_buffer(eb); |
d1310b2e | 4797 | } |
d1310b2e | 4798 | |
0d27797e QW |
4799 | static void btree_clear_page_dirty(struct page *page) |
4800 | { | |
4801 | ASSERT(PageDirty(page)); | |
4802 | ASSERT(PageLocked(page)); | |
4803 | clear_page_dirty_for_io(page); | |
4804 | xa_lock_irq(&page->mapping->i_pages); | |
4805 | if (!PageDirty(page)) | |
4806 | __xa_clear_mark(&page->mapping->i_pages, | |
4807 | page_index(page), PAGECACHE_TAG_DIRTY); | |
4808 | xa_unlock_irq(&page->mapping->i_pages); | |
4809 | } | |
4810 | ||
4811 | static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb) | |
4812 | { | |
4813 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
4814 | struct page *page = eb->pages[0]; | |
4815 | bool last; | |
4816 | ||
4817 | /* btree_clear_page_dirty() needs page locked */ | |
4818 | lock_page(page); | |
4819 | last = btrfs_subpage_clear_and_test_dirty(fs_info, page, eb->start, | |
4820 | eb->len); | |
4821 | if (last) | |
4822 | btree_clear_page_dirty(page); | |
4823 | unlock_page(page); | |
4824 | WARN_ON(atomic_read(&eb->refs) == 0); | |
4825 | } | |
4826 | ||
2b48966a | 4827 | void clear_extent_buffer_dirty(const struct extent_buffer *eb) |
d1310b2e | 4828 | { |
cc5e31a4 DS |
4829 | int i; |
4830 | int num_pages; | |
d1310b2e CM |
4831 | struct page *page; |
4832 | ||
fbca46eb | 4833 | if (eb->fs_info->nodesize < PAGE_SIZE) |
0d27797e QW |
4834 | return clear_subpage_extent_buffer_dirty(eb); |
4835 | ||
65ad0104 | 4836 | num_pages = num_extent_pages(eb); |
d1310b2e CM |
4837 | |
4838 | for (i = 0; i < num_pages; i++) { | |
fb85fc9a | 4839 | page = eb->pages[i]; |
b9473439 | 4840 | if (!PageDirty(page)) |
d2c3f4f6 | 4841 | continue; |
a61e6f29 | 4842 | lock_page(page); |
0d27797e | 4843 | btree_clear_page_dirty(page); |
bf0da8c1 | 4844 | ClearPageError(page); |
a61e6f29 | 4845 | unlock_page(page); |
d1310b2e | 4846 | } |
0b32f4bb | 4847 | WARN_ON(atomic_read(&eb->refs) == 0); |
d1310b2e | 4848 | } |
d1310b2e | 4849 | |
abb57ef3 | 4850 | bool set_extent_buffer_dirty(struct extent_buffer *eb) |
d1310b2e | 4851 | { |
cc5e31a4 DS |
4852 | int i; |
4853 | int num_pages; | |
abb57ef3 | 4854 | bool was_dirty; |
d1310b2e | 4855 | |
0b32f4bb JB |
4856 | check_buffer_tree_ref(eb); |
4857 | ||
b9473439 | 4858 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
0b32f4bb | 4859 | |
65ad0104 | 4860 | num_pages = num_extent_pages(eb); |
3083ee2e | 4861 | WARN_ON(atomic_read(&eb->refs) == 0); |
0b32f4bb JB |
4862 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
4863 | ||
0d27797e | 4864 | if (!was_dirty) { |
fbca46eb | 4865 | bool subpage = eb->fs_info->nodesize < PAGE_SIZE; |
51995c39 | 4866 | |
0d27797e QW |
4867 | /* |
4868 | * For subpage case, we can have other extent buffers in the | |
4869 | * same page, and in clear_subpage_extent_buffer_dirty() we | |
4870 | * have to clear page dirty without subpage lock held. | |
4871 | * This can cause race where our page gets dirty cleared after | |
4872 | * we just set it. | |
4873 | * | |
4874 | * Thankfully, clear_subpage_extent_buffer_dirty() has locked | |
4875 | * its page for other reasons, we can use page lock to prevent | |
4876 | * the above race. | |
4877 | */ | |
4878 | if (subpage) | |
4879 | lock_page(eb->pages[0]); | |
4880 | for (i = 0; i < num_pages; i++) | |
4881 | btrfs_page_set_dirty(eb->fs_info, eb->pages[i], | |
4882 | eb->start, eb->len); | |
4883 | if (subpage) | |
4884 | unlock_page(eb->pages[0]); | |
4885 | } | |
51995c39 LB |
4886 | #ifdef CONFIG_BTRFS_DEBUG |
4887 | for (i = 0; i < num_pages; i++) | |
4888 | ASSERT(PageDirty(eb->pages[i])); | |
4889 | #endif | |
4890 | ||
b9473439 | 4891 | return was_dirty; |
d1310b2e | 4892 | } |
d1310b2e | 4893 | |
69ba3927 | 4894 | void clear_extent_buffer_uptodate(struct extent_buffer *eb) |
1259ab75 | 4895 | { |
251f2acc | 4896 | struct btrfs_fs_info *fs_info = eb->fs_info; |
1259ab75 | 4897 | struct page *page; |
cc5e31a4 | 4898 | int num_pages; |
251f2acc | 4899 | int i; |
1259ab75 | 4900 | |
b4ce94de | 4901 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
65ad0104 | 4902 | num_pages = num_extent_pages(eb); |
1259ab75 | 4903 | for (i = 0; i < num_pages; i++) { |
fb85fc9a | 4904 | page = eb->pages[i]; |
fbca46eb QW |
4905 | if (!page) |
4906 | continue; | |
4907 | ||
4908 | /* | |
4909 | * This is special handling for metadata subpage, as regular | |
4910 | * btrfs_is_subpage() can not handle cloned/dummy metadata. | |
4911 | */ | |
4912 | if (fs_info->nodesize >= PAGE_SIZE) | |
4913 | ClearPageUptodate(page); | |
4914 | else | |
4915 | btrfs_subpage_clear_uptodate(fs_info, page, eb->start, | |
4916 | eb->len); | |
1259ab75 | 4917 | } |
1259ab75 CM |
4918 | } |
4919 | ||
09c25a8c | 4920 | void set_extent_buffer_uptodate(struct extent_buffer *eb) |
d1310b2e | 4921 | { |
251f2acc | 4922 | struct btrfs_fs_info *fs_info = eb->fs_info; |
d1310b2e | 4923 | struct page *page; |
cc5e31a4 | 4924 | int num_pages; |
251f2acc | 4925 | int i; |
d1310b2e | 4926 | |
0b32f4bb | 4927 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
65ad0104 | 4928 | num_pages = num_extent_pages(eb); |
d1310b2e | 4929 | for (i = 0; i < num_pages; i++) { |
fb85fc9a | 4930 | page = eb->pages[i]; |
fbca46eb QW |
4931 | |
4932 | /* | |
4933 | * This is special handling for metadata subpage, as regular | |
4934 | * btrfs_is_subpage() can not handle cloned/dummy metadata. | |
4935 | */ | |
4936 | if (fs_info->nodesize >= PAGE_SIZE) | |
4937 | SetPageUptodate(page); | |
4938 | else | |
4939 | btrfs_subpage_set_uptodate(fs_info, page, eb->start, | |
4940 | eb->len); | |
d1310b2e | 4941 | } |
d1310b2e | 4942 | } |
d1310b2e | 4943 | |
4012daf7 QW |
4944 | static int read_extent_buffer_subpage(struct extent_buffer *eb, int wait, |
4945 | int mirror_num) | |
4946 | { | |
4947 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
4948 | struct extent_io_tree *io_tree; | |
4949 | struct page *page = eb->pages[0]; | |
e5e886ba | 4950 | struct extent_state *cached_state = NULL; |
722c82ac CH |
4951 | struct btrfs_bio_ctrl bio_ctrl = { |
4952 | .mirror_num = mirror_num, | |
4953 | }; | |
4012daf7 QW |
4954 | int ret = 0; |
4955 | ||
4956 | ASSERT(!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags)); | |
4957 | ASSERT(PagePrivate(page)); | |
4958 | io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; | |
4959 | ||
4960 | if (wait == WAIT_NONE) { | |
83ae4133 | 4961 | if (!try_lock_extent(io_tree, eb->start, eb->start + eb->len - 1, |
e5e886ba | 4962 | &cached_state)) |
dc56219f | 4963 | return -EAGAIN; |
4012daf7 | 4964 | } else { |
e5e886ba JB |
4965 | ret = lock_extent(io_tree, eb->start, eb->start + eb->len - 1, |
4966 | &cached_state); | |
4012daf7 QW |
4967 | if (ret < 0) |
4968 | return ret; | |
4969 | } | |
4970 | ||
4971 | ret = 0; | |
4972 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags) || | |
4973 | PageUptodate(page) || | |
4974 | btrfs_subpage_test_uptodate(fs_info, page, eb->start, eb->len)) { | |
4975 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); | |
e5e886ba JB |
4976 | unlock_extent(io_tree, eb->start, eb->start + eb->len - 1, |
4977 | &cached_state); | |
4012daf7 QW |
4978 | return ret; |
4979 | } | |
4980 | ||
4981 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); | |
4982 | eb->read_mirror = 0; | |
4983 | atomic_set(&eb->io_pages, 1); | |
4984 | check_buffer_tree_ref(eb); | |
5467abba QW |
4985 | bio_ctrl.end_io_func = end_bio_extent_readpage; |
4986 | ||
4012daf7 QW |
4987 | btrfs_subpage_clear_error(fs_info, page, eb->start, eb->len); |
4988 | ||
3d078efa | 4989 | btrfs_subpage_start_reader(fs_info, page, eb->start, eb->len); |
08a6f464 | 4990 | ret = submit_extent_page(REQ_OP_READ, NULL, &bio_ctrl, |
209ecde5 | 4991 | eb->start, page, eb->len, |
5467abba | 4992 | eb->start - page_offset(page), 0, true); |
4012daf7 QW |
4993 | if (ret) { |
4994 | /* | |
4995 | * In the endio function, if we hit something wrong we will | |
4996 | * increase the io_pages, so here we need to decrease it for | |
4997 | * error path. | |
4998 | */ | |
4999 | atomic_dec(&eb->io_pages); | |
5000 | } | |
722c82ac | 5001 | submit_one_bio(&bio_ctrl); |
e5e886ba JB |
5002 | if (ret || wait != WAIT_COMPLETE) { |
5003 | free_extent_state(cached_state); | |
4012daf7 | 5004 | return ret; |
e5e886ba | 5005 | } |
4012daf7 | 5006 | |
123a7f00 | 5007 | wait_extent_bit(io_tree, eb->start, eb->start + eb->len - 1, |
e5e886ba | 5008 | EXTENT_LOCKED, &cached_state); |
4012daf7 QW |
5009 | if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
5010 | ret = -EIO; | |
5011 | return ret; | |
5012 | } | |
5013 | ||
c2ccfbc6 | 5014 | int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num) |
d1310b2e | 5015 | { |
cc5e31a4 | 5016 | int i; |
d1310b2e CM |
5017 | struct page *page; |
5018 | int err; | |
5019 | int ret = 0; | |
ce9adaa5 CM |
5020 | int locked_pages = 0; |
5021 | int all_uptodate = 1; | |
cc5e31a4 | 5022 | int num_pages; |
727011e0 | 5023 | unsigned long num_reads = 0; |
722c82ac CH |
5024 | struct btrfs_bio_ctrl bio_ctrl = { |
5025 | .mirror_num = mirror_num, | |
5026 | }; | |
a86c12c7 | 5027 | |
b4ce94de | 5028 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
d1310b2e CM |
5029 | return 0; |
5030 | ||
651740a5 JB |
5031 | /* |
5032 | * We could have had EXTENT_BUFFER_UPTODATE cleared by the write | |
5033 | * operation, which could potentially still be in flight. In this case | |
5034 | * we simply want to return an error. | |
5035 | */ | |
5036 | if (unlikely(test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))) | |
5037 | return -EIO; | |
5038 | ||
fbca46eb | 5039 | if (eb->fs_info->nodesize < PAGE_SIZE) |
4012daf7 QW |
5040 | return read_extent_buffer_subpage(eb, wait, mirror_num); |
5041 | ||
65ad0104 | 5042 | num_pages = num_extent_pages(eb); |
8436ea91 | 5043 | for (i = 0; i < num_pages; i++) { |
fb85fc9a | 5044 | page = eb->pages[i]; |
bb82ab88 | 5045 | if (wait == WAIT_NONE) { |
2c4d8cb7 QW |
5046 | /* |
5047 | * WAIT_NONE is only utilized by readahead. If we can't | |
5048 | * acquire the lock atomically it means either the eb | |
5049 | * is being read out or under modification. | |
5050 | * Either way the eb will be or has been cached, | |
5051 | * readahead can exit safely. | |
5052 | */ | |
2db04966 | 5053 | if (!trylock_page(page)) |
ce9adaa5 | 5054 | goto unlock_exit; |
d1310b2e CM |
5055 | } else { |
5056 | lock_page(page); | |
5057 | } | |
ce9adaa5 | 5058 | locked_pages++; |
2571e739 LB |
5059 | } |
5060 | /* | |
5061 | * We need to firstly lock all pages to make sure that | |
5062 | * the uptodate bit of our pages won't be affected by | |
5063 | * clear_extent_buffer_uptodate(). | |
5064 | */ | |
8436ea91 | 5065 | for (i = 0; i < num_pages; i++) { |
2571e739 | 5066 | page = eb->pages[i]; |
727011e0 CM |
5067 | if (!PageUptodate(page)) { |
5068 | num_reads++; | |
ce9adaa5 | 5069 | all_uptodate = 0; |
727011e0 | 5070 | } |
ce9adaa5 | 5071 | } |
2571e739 | 5072 | |
ce9adaa5 | 5073 | if (all_uptodate) { |
8436ea91 | 5074 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
ce9adaa5 CM |
5075 | goto unlock_exit; |
5076 | } | |
5077 | ||
656f30db | 5078 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
5cf1ab56 | 5079 | eb->read_mirror = 0; |
0b32f4bb | 5080 | atomic_set(&eb->io_pages, num_reads); |
6bf9cd2e | 5081 | /* |
f913cff3 | 5082 | * It is possible for release_folio to clear the TREE_REF bit before we |
6bf9cd2e BB |
5083 | * set io_pages. See check_buffer_tree_ref for a more detailed comment. |
5084 | */ | |
5085 | check_buffer_tree_ref(eb); | |
5467abba | 5086 | bio_ctrl.end_io_func = end_bio_extent_readpage; |
8436ea91 | 5087 | for (i = 0; i < num_pages; i++) { |
fb85fc9a | 5088 | page = eb->pages[i]; |
baf863b9 | 5089 | |
ce9adaa5 | 5090 | if (!PageUptodate(page)) { |
baf863b9 LB |
5091 | if (ret) { |
5092 | atomic_dec(&eb->io_pages); | |
5093 | unlock_page(page); | |
5094 | continue; | |
5095 | } | |
5096 | ||
f188591e | 5097 | ClearPageError(page); |
08a6f464 | 5098 | err = submit_extent_page(REQ_OP_READ, NULL, |
209ecde5 | 5099 | &bio_ctrl, page_offset(page), page, |
5467abba | 5100 | PAGE_SIZE, 0, 0, false); |
baf863b9 | 5101 | if (err) { |
baf863b9 | 5102 | /* |
0420177c NB |
5103 | * We failed to submit the bio so it's the |
5104 | * caller's responsibility to perform cleanup | |
5105 | * i.e unlock page/set error bit. | |
baf863b9 | 5106 | */ |
0420177c NB |
5107 | ret = err; |
5108 | SetPageError(page); | |
5109 | unlock_page(page); | |
baf863b9 LB |
5110 | atomic_dec(&eb->io_pages); |
5111 | } | |
d1310b2e CM |
5112 | } else { |
5113 | unlock_page(page); | |
5114 | } | |
5115 | } | |
5116 | ||
722c82ac | 5117 | submit_one_bio(&bio_ctrl); |
a86c12c7 | 5118 | |
bb82ab88 | 5119 | if (ret || wait != WAIT_COMPLETE) |
d1310b2e | 5120 | return ret; |
d397712b | 5121 | |
8436ea91 | 5122 | for (i = 0; i < num_pages; i++) { |
fb85fc9a | 5123 | page = eb->pages[i]; |
d1310b2e | 5124 | wait_on_page_locked(page); |
d397712b | 5125 | if (!PageUptodate(page)) |
d1310b2e | 5126 | ret = -EIO; |
d1310b2e | 5127 | } |
d397712b | 5128 | |
d1310b2e | 5129 | return ret; |
ce9adaa5 CM |
5130 | |
5131 | unlock_exit: | |
d397712b | 5132 | while (locked_pages > 0) { |
ce9adaa5 | 5133 | locked_pages--; |
8436ea91 JB |
5134 | page = eb->pages[locked_pages]; |
5135 | unlock_page(page); | |
ce9adaa5 CM |
5136 | } |
5137 | return ret; | |
d1310b2e | 5138 | } |
d1310b2e | 5139 | |
f98b6215 QW |
5140 | static bool report_eb_range(const struct extent_buffer *eb, unsigned long start, |
5141 | unsigned long len) | |
5142 | { | |
5143 | btrfs_warn(eb->fs_info, | |
5144 | "access to eb bytenr %llu len %lu out of range start %lu len %lu", | |
5145 | eb->start, eb->len, start, len); | |
5146 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); | |
5147 | ||
5148 | return true; | |
5149 | } | |
5150 | ||
5151 | /* | |
5152 | * Check if the [start, start + len) range is valid before reading/writing | |
5153 | * the eb. | |
5154 | * NOTE: @start and @len are offset inside the eb, not logical address. | |
5155 | * | |
5156 | * Caller should not touch the dst/src memory if this function returns error. | |
5157 | */ | |
5158 | static inline int check_eb_range(const struct extent_buffer *eb, | |
5159 | unsigned long start, unsigned long len) | |
5160 | { | |
5161 | unsigned long offset; | |
5162 | ||
5163 | /* start, start + len should not go beyond eb->len nor overflow */ | |
5164 | if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len)) | |
5165 | return report_eb_range(eb, start, len); | |
5166 | ||
5167 | return false; | |
5168 | } | |
5169 | ||
1cbb1f45 JM |
5170 | void read_extent_buffer(const struct extent_buffer *eb, void *dstv, |
5171 | unsigned long start, unsigned long len) | |
d1310b2e CM |
5172 | { |
5173 | size_t cur; | |
5174 | size_t offset; | |
5175 | struct page *page; | |
5176 | char *kaddr; | |
5177 | char *dst = (char *)dstv; | |
884b07d0 | 5178 | unsigned long i = get_eb_page_index(start); |
d1310b2e | 5179 | |
f98b6215 | 5180 | if (check_eb_range(eb, start, len)) |
f716abd5 | 5181 | return; |
d1310b2e | 5182 | |
884b07d0 | 5183 | offset = get_eb_offset_in_page(eb, start); |
d1310b2e | 5184 | |
d397712b | 5185 | while (len > 0) { |
fb85fc9a | 5186 | page = eb->pages[i]; |
d1310b2e | 5187 | |
09cbfeaf | 5188 | cur = min(len, (PAGE_SIZE - offset)); |
a6591715 | 5189 | kaddr = page_address(page); |
d1310b2e | 5190 | memcpy(dst, kaddr + offset, cur); |
d1310b2e CM |
5191 | |
5192 | dst += cur; | |
5193 | len -= cur; | |
5194 | offset = 0; | |
5195 | i++; | |
5196 | } | |
5197 | } | |
d1310b2e | 5198 | |
a48b73ec JB |
5199 | int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, |
5200 | void __user *dstv, | |
5201 | unsigned long start, unsigned long len) | |
550ac1d8 GH |
5202 | { |
5203 | size_t cur; | |
5204 | size_t offset; | |
5205 | struct page *page; | |
5206 | char *kaddr; | |
5207 | char __user *dst = (char __user *)dstv; | |
884b07d0 | 5208 | unsigned long i = get_eb_page_index(start); |
550ac1d8 GH |
5209 | int ret = 0; |
5210 | ||
5211 | WARN_ON(start > eb->len); | |
5212 | WARN_ON(start + len > eb->start + eb->len); | |
5213 | ||
884b07d0 | 5214 | offset = get_eb_offset_in_page(eb, start); |
550ac1d8 GH |
5215 | |
5216 | while (len > 0) { | |
fb85fc9a | 5217 | page = eb->pages[i]; |
550ac1d8 | 5218 | |
09cbfeaf | 5219 | cur = min(len, (PAGE_SIZE - offset)); |
550ac1d8 | 5220 | kaddr = page_address(page); |
a48b73ec | 5221 | if (copy_to_user_nofault(dst, kaddr + offset, cur)) { |
550ac1d8 GH |
5222 | ret = -EFAULT; |
5223 | break; | |
5224 | } | |
5225 | ||
5226 | dst += cur; | |
5227 | len -= cur; | |
5228 | offset = 0; | |
5229 | i++; | |
5230 | } | |
5231 | ||
5232 | return ret; | |
5233 | } | |
5234 | ||
1cbb1f45 JM |
5235 | int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, |
5236 | unsigned long start, unsigned long len) | |
d1310b2e CM |
5237 | { |
5238 | size_t cur; | |
5239 | size_t offset; | |
5240 | struct page *page; | |
5241 | char *kaddr; | |
5242 | char *ptr = (char *)ptrv; | |
884b07d0 | 5243 | unsigned long i = get_eb_page_index(start); |
d1310b2e CM |
5244 | int ret = 0; |
5245 | ||
f98b6215 QW |
5246 | if (check_eb_range(eb, start, len)) |
5247 | return -EINVAL; | |
d1310b2e | 5248 | |
884b07d0 | 5249 | offset = get_eb_offset_in_page(eb, start); |
d1310b2e | 5250 | |
d397712b | 5251 | while (len > 0) { |
fb85fc9a | 5252 | page = eb->pages[i]; |
d1310b2e | 5253 | |
09cbfeaf | 5254 | cur = min(len, (PAGE_SIZE - offset)); |
d1310b2e | 5255 | |
a6591715 | 5256 | kaddr = page_address(page); |
d1310b2e | 5257 | ret = memcmp(ptr, kaddr + offset, cur); |
d1310b2e CM |
5258 | if (ret) |
5259 | break; | |
5260 | ||
5261 | ptr += cur; | |
5262 | len -= cur; | |
5263 | offset = 0; | |
5264 | i++; | |
5265 | } | |
5266 | return ret; | |
5267 | } | |
d1310b2e | 5268 | |
b8f95771 QW |
5269 | /* |
5270 | * Check that the extent buffer is uptodate. | |
5271 | * | |
5272 | * For regular sector size == PAGE_SIZE case, check if @page is uptodate. | |
5273 | * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE. | |
5274 | */ | |
5275 | static void assert_eb_page_uptodate(const struct extent_buffer *eb, | |
5276 | struct page *page) | |
5277 | { | |
5278 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
5279 | ||
a50e1fcb JB |
5280 | /* |
5281 | * If we are using the commit root we could potentially clear a page | |
5282 | * Uptodate while we're using the extent buffer that we've previously | |
5283 | * looked up. We don't want to complain in this case, as the page was | |
5284 | * valid before, we just didn't write it out. Instead we want to catch | |
5285 | * the case where we didn't actually read the block properly, which | |
5286 | * would have !PageUptodate && !PageError, as we clear PageError before | |
5287 | * reading. | |
5288 | */ | |
fbca46eb | 5289 | if (fs_info->nodesize < PAGE_SIZE) { |
a50e1fcb | 5290 | bool uptodate, error; |
b8f95771 QW |
5291 | |
5292 | uptodate = btrfs_subpage_test_uptodate(fs_info, page, | |
5293 | eb->start, eb->len); | |
a50e1fcb JB |
5294 | error = btrfs_subpage_test_error(fs_info, page, eb->start, eb->len); |
5295 | WARN_ON(!uptodate && !error); | |
b8f95771 | 5296 | } else { |
a50e1fcb | 5297 | WARN_ON(!PageUptodate(page) && !PageError(page)); |
b8f95771 QW |
5298 | } |
5299 | } | |
5300 | ||
2b48966a | 5301 | void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb, |
f157bf76 DS |
5302 | const void *srcv) |
5303 | { | |
5304 | char *kaddr; | |
5305 | ||
b8f95771 | 5306 | assert_eb_page_uptodate(eb, eb->pages[0]); |
24880be5 DS |
5307 | kaddr = page_address(eb->pages[0]) + |
5308 | get_eb_offset_in_page(eb, offsetof(struct btrfs_header, | |
5309 | chunk_tree_uuid)); | |
5310 | memcpy(kaddr, srcv, BTRFS_FSID_SIZE); | |
f157bf76 DS |
5311 | } |
5312 | ||
2b48966a | 5313 | void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *srcv) |
f157bf76 DS |
5314 | { |
5315 | char *kaddr; | |
5316 | ||
b8f95771 | 5317 | assert_eb_page_uptodate(eb, eb->pages[0]); |
24880be5 DS |
5318 | kaddr = page_address(eb->pages[0]) + |
5319 | get_eb_offset_in_page(eb, offsetof(struct btrfs_header, fsid)); | |
5320 | memcpy(kaddr, srcv, BTRFS_FSID_SIZE); | |
f157bf76 DS |
5321 | } |
5322 | ||
2b48966a | 5323 | void write_extent_buffer(const struct extent_buffer *eb, const void *srcv, |
d1310b2e CM |
5324 | unsigned long start, unsigned long len) |
5325 | { | |
5326 | size_t cur; | |
5327 | size_t offset; | |
5328 | struct page *page; | |
5329 | char *kaddr; | |
5330 | char *src = (char *)srcv; | |
884b07d0 | 5331 | unsigned long i = get_eb_page_index(start); |
d1310b2e | 5332 | |
d3575156 NA |
5333 | WARN_ON(test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)); |
5334 | ||
f98b6215 QW |
5335 | if (check_eb_range(eb, start, len)) |
5336 | return; | |
d1310b2e | 5337 | |
884b07d0 | 5338 | offset = get_eb_offset_in_page(eb, start); |
d1310b2e | 5339 | |
d397712b | 5340 | while (len > 0) { |
fb85fc9a | 5341 | page = eb->pages[i]; |
b8f95771 | 5342 | assert_eb_page_uptodate(eb, page); |
d1310b2e | 5343 | |
09cbfeaf | 5344 | cur = min(len, PAGE_SIZE - offset); |
a6591715 | 5345 | kaddr = page_address(page); |
d1310b2e | 5346 | memcpy(kaddr + offset, src, cur); |
d1310b2e CM |
5347 | |
5348 | src += cur; | |
5349 | len -= cur; | |
5350 | offset = 0; | |
5351 | i++; | |
5352 | } | |
5353 | } | |
d1310b2e | 5354 | |
2b48966a | 5355 | void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start, |
b159fa28 | 5356 | unsigned long len) |
d1310b2e CM |
5357 | { |
5358 | size_t cur; | |
5359 | size_t offset; | |
5360 | struct page *page; | |
5361 | char *kaddr; | |
884b07d0 | 5362 | unsigned long i = get_eb_page_index(start); |
d1310b2e | 5363 | |
f98b6215 QW |
5364 | if (check_eb_range(eb, start, len)) |
5365 | return; | |
d1310b2e | 5366 | |
884b07d0 | 5367 | offset = get_eb_offset_in_page(eb, start); |
d1310b2e | 5368 | |
d397712b | 5369 | while (len > 0) { |
fb85fc9a | 5370 | page = eb->pages[i]; |
b8f95771 | 5371 | assert_eb_page_uptodate(eb, page); |
d1310b2e | 5372 | |
09cbfeaf | 5373 | cur = min(len, PAGE_SIZE - offset); |
a6591715 | 5374 | kaddr = page_address(page); |
b159fa28 | 5375 | memset(kaddr + offset, 0, cur); |
d1310b2e CM |
5376 | |
5377 | len -= cur; | |
5378 | offset = 0; | |
5379 | i++; | |
5380 | } | |
5381 | } | |
d1310b2e | 5382 | |
2b48966a DS |
5383 | void copy_extent_buffer_full(const struct extent_buffer *dst, |
5384 | const struct extent_buffer *src) | |
58e8012c DS |
5385 | { |
5386 | int i; | |
cc5e31a4 | 5387 | int num_pages; |
58e8012c DS |
5388 | |
5389 | ASSERT(dst->len == src->len); | |
5390 | ||
fbca46eb | 5391 | if (dst->fs_info->nodesize >= PAGE_SIZE) { |
884b07d0 QW |
5392 | num_pages = num_extent_pages(dst); |
5393 | for (i = 0; i < num_pages; i++) | |
5394 | copy_page(page_address(dst->pages[i]), | |
5395 | page_address(src->pages[i])); | |
5396 | } else { | |
5397 | size_t src_offset = get_eb_offset_in_page(src, 0); | |
5398 | size_t dst_offset = get_eb_offset_in_page(dst, 0); | |
5399 | ||
fbca46eb | 5400 | ASSERT(src->fs_info->nodesize < PAGE_SIZE); |
884b07d0 QW |
5401 | memcpy(page_address(dst->pages[0]) + dst_offset, |
5402 | page_address(src->pages[0]) + src_offset, | |
5403 | src->len); | |
5404 | } | |
58e8012c DS |
5405 | } |
5406 | ||
2b48966a DS |
5407 | void copy_extent_buffer(const struct extent_buffer *dst, |
5408 | const struct extent_buffer *src, | |
d1310b2e CM |
5409 | unsigned long dst_offset, unsigned long src_offset, |
5410 | unsigned long len) | |
5411 | { | |
5412 | u64 dst_len = dst->len; | |
5413 | size_t cur; | |
5414 | size_t offset; | |
5415 | struct page *page; | |
5416 | char *kaddr; | |
884b07d0 | 5417 | unsigned long i = get_eb_page_index(dst_offset); |
d1310b2e | 5418 | |
f98b6215 QW |
5419 | if (check_eb_range(dst, dst_offset, len) || |
5420 | check_eb_range(src, src_offset, len)) | |
5421 | return; | |
5422 | ||
d1310b2e CM |
5423 | WARN_ON(src->len != dst_len); |
5424 | ||
884b07d0 | 5425 | offset = get_eb_offset_in_page(dst, dst_offset); |
d1310b2e | 5426 | |
d397712b | 5427 | while (len > 0) { |
fb85fc9a | 5428 | page = dst->pages[i]; |
b8f95771 | 5429 | assert_eb_page_uptodate(dst, page); |
d1310b2e | 5430 | |
09cbfeaf | 5431 | cur = min(len, (unsigned long)(PAGE_SIZE - offset)); |
d1310b2e | 5432 | |
a6591715 | 5433 | kaddr = page_address(page); |
d1310b2e | 5434 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
d1310b2e CM |
5435 | |
5436 | src_offset += cur; | |
5437 | len -= cur; | |
5438 | offset = 0; | |
5439 | i++; | |
5440 | } | |
5441 | } | |
d1310b2e | 5442 | |
3e1e8bb7 OS |
5443 | /* |
5444 | * eb_bitmap_offset() - calculate the page and offset of the byte containing the | |
5445 | * given bit number | |
5446 | * @eb: the extent buffer | |
5447 | * @start: offset of the bitmap item in the extent buffer | |
5448 | * @nr: bit number | |
5449 | * @page_index: return index of the page in the extent buffer that contains the | |
5450 | * given bit number | |
5451 | * @page_offset: return offset into the page given by page_index | |
5452 | * | |
5453 | * This helper hides the ugliness of finding the byte in an extent buffer which | |
5454 | * contains a given bit. | |
5455 | */ | |
2b48966a | 5456 | static inline void eb_bitmap_offset(const struct extent_buffer *eb, |
3e1e8bb7 OS |
5457 | unsigned long start, unsigned long nr, |
5458 | unsigned long *page_index, | |
5459 | size_t *page_offset) | |
5460 | { | |
3e1e8bb7 OS |
5461 | size_t byte_offset = BIT_BYTE(nr); |
5462 | size_t offset; | |
5463 | ||
5464 | /* | |
5465 | * The byte we want is the offset of the extent buffer + the offset of | |
5466 | * the bitmap item in the extent buffer + the offset of the byte in the | |
5467 | * bitmap item. | |
5468 | */ | |
884b07d0 | 5469 | offset = start + offset_in_page(eb->start) + byte_offset; |
3e1e8bb7 | 5470 | |
09cbfeaf | 5471 | *page_index = offset >> PAGE_SHIFT; |
7073017a | 5472 | *page_offset = offset_in_page(offset); |
3e1e8bb7 OS |
5473 | } |
5474 | ||
5475 | /** | |
5476 | * extent_buffer_test_bit - determine whether a bit in a bitmap item is set | |
5477 | * @eb: the extent buffer | |
5478 | * @start: offset of the bitmap item in the extent buffer | |
5479 | * @nr: bit number to test | |
5480 | */ | |
2b48966a | 5481 | int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start, |
3e1e8bb7 OS |
5482 | unsigned long nr) |
5483 | { | |
2fe1d551 | 5484 | u8 *kaddr; |
3e1e8bb7 OS |
5485 | struct page *page; |
5486 | unsigned long i; | |
5487 | size_t offset; | |
5488 | ||
5489 | eb_bitmap_offset(eb, start, nr, &i, &offset); | |
5490 | page = eb->pages[i]; | |
b8f95771 | 5491 | assert_eb_page_uptodate(eb, page); |
3e1e8bb7 OS |
5492 | kaddr = page_address(page); |
5493 | return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); | |
5494 | } | |
5495 | ||
5496 | /** | |
5497 | * extent_buffer_bitmap_set - set an area of a bitmap | |
5498 | * @eb: the extent buffer | |
5499 | * @start: offset of the bitmap item in the extent buffer | |
5500 | * @pos: bit number of the first bit | |
5501 | * @len: number of bits to set | |
5502 | */ | |
2b48966a | 5503 | void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start, |
3e1e8bb7 OS |
5504 | unsigned long pos, unsigned long len) |
5505 | { | |
2fe1d551 | 5506 | u8 *kaddr; |
3e1e8bb7 OS |
5507 | struct page *page; |
5508 | unsigned long i; | |
5509 | size_t offset; | |
5510 | const unsigned int size = pos + len; | |
5511 | int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE); | |
2fe1d551 | 5512 | u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos); |
3e1e8bb7 OS |
5513 | |
5514 | eb_bitmap_offset(eb, start, pos, &i, &offset); | |
5515 | page = eb->pages[i]; | |
b8f95771 | 5516 | assert_eb_page_uptodate(eb, page); |
3e1e8bb7 OS |
5517 | kaddr = page_address(page); |
5518 | ||
5519 | while (len >= bits_to_set) { | |
5520 | kaddr[offset] |= mask_to_set; | |
5521 | len -= bits_to_set; | |
5522 | bits_to_set = BITS_PER_BYTE; | |
9c894696 | 5523 | mask_to_set = ~0; |
09cbfeaf | 5524 | if (++offset >= PAGE_SIZE && len > 0) { |
3e1e8bb7 OS |
5525 | offset = 0; |
5526 | page = eb->pages[++i]; | |
b8f95771 | 5527 | assert_eb_page_uptodate(eb, page); |
3e1e8bb7 OS |
5528 | kaddr = page_address(page); |
5529 | } | |
5530 | } | |
5531 | if (len) { | |
5532 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); | |
5533 | kaddr[offset] |= mask_to_set; | |
5534 | } | |
5535 | } | |
5536 | ||
5537 | ||
5538 | /** | |
5539 | * extent_buffer_bitmap_clear - clear an area of a bitmap | |
5540 | * @eb: the extent buffer | |
5541 | * @start: offset of the bitmap item in the extent buffer | |
5542 | * @pos: bit number of the first bit | |
5543 | * @len: number of bits to clear | |
5544 | */ | |
2b48966a DS |
5545 | void extent_buffer_bitmap_clear(const struct extent_buffer *eb, |
5546 | unsigned long start, unsigned long pos, | |
5547 | unsigned long len) | |
3e1e8bb7 | 5548 | { |
2fe1d551 | 5549 | u8 *kaddr; |
3e1e8bb7 OS |
5550 | struct page *page; |
5551 | unsigned long i; | |
5552 | size_t offset; | |
5553 | const unsigned int size = pos + len; | |
5554 | int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE); | |
2fe1d551 | 5555 | u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos); |
3e1e8bb7 OS |
5556 | |
5557 | eb_bitmap_offset(eb, start, pos, &i, &offset); | |
5558 | page = eb->pages[i]; | |
b8f95771 | 5559 | assert_eb_page_uptodate(eb, page); |
3e1e8bb7 OS |
5560 | kaddr = page_address(page); |
5561 | ||
5562 | while (len >= bits_to_clear) { | |
5563 | kaddr[offset] &= ~mask_to_clear; | |
5564 | len -= bits_to_clear; | |
5565 | bits_to_clear = BITS_PER_BYTE; | |
9c894696 | 5566 | mask_to_clear = ~0; |
09cbfeaf | 5567 | if (++offset >= PAGE_SIZE && len > 0) { |
3e1e8bb7 OS |
5568 | offset = 0; |
5569 | page = eb->pages[++i]; | |
b8f95771 | 5570 | assert_eb_page_uptodate(eb, page); |
3e1e8bb7 OS |
5571 | kaddr = page_address(page); |
5572 | } | |
5573 | } | |
5574 | if (len) { | |
5575 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); | |
5576 | kaddr[offset] &= ~mask_to_clear; | |
5577 | } | |
5578 | } | |
5579 | ||
3387206f ST |
5580 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
5581 | { | |
5582 | unsigned long distance = (src > dst) ? src - dst : dst - src; | |
5583 | return distance < len; | |
5584 | } | |
5585 | ||
d1310b2e CM |
5586 | static void copy_pages(struct page *dst_page, struct page *src_page, |
5587 | unsigned long dst_off, unsigned long src_off, | |
5588 | unsigned long len) | |
5589 | { | |
a6591715 | 5590 | char *dst_kaddr = page_address(dst_page); |
d1310b2e | 5591 | char *src_kaddr; |
727011e0 | 5592 | int must_memmove = 0; |
d1310b2e | 5593 | |
3387206f | 5594 | if (dst_page != src_page) { |
a6591715 | 5595 | src_kaddr = page_address(src_page); |
3387206f | 5596 | } else { |
d1310b2e | 5597 | src_kaddr = dst_kaddr; |
727011e0 CM |
5598 | if (areas_overlap(src_off, dst_off, len)) |
5599 | must_memmove = 1; | |
3387206f | 5600 | } |
d1310b2e | 5601 | |
727011e0 CM |
5602 | if (must_memmove) |
5603 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); | |
5604 | else | |
5605 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); | |
d1310b2e CM |
5606 | } |
5607 | ||
2b48966a DS |
5608 | void memcpy_extent_buffer(const struct extent_buffer *dst, |
5609 | unsigned long dst_offset, unsigned long src_offset, | |
5610 | unsigned long len) | |
d1310b2e CM |
5611 | { |
5612 | size_t cur; | |
5613 | size_t dst_off_in_page; | |
5614 | size_t src_off_in_page; | |
d1310b2e CM |
5615 | unsigned long dst_i; |
5616 | unsigned long src_i; | |
5617 | ||
f98b6215 QW |
5618 | if (check_eb_range(dst, dst_offset, len) || |
5619 | check_eb_range(dst, src_offset, len)) | |
5620 | return; | |
d1310b2e | 5621 | |
d397712b | 5622 | while (len > 0) { |
884b07d0 QW |
5623 | dst_off_in_page = get_eb_offset_in_page(dst, dst_offset); |
5624 | src_off_in_page = get_eb_offset_in_page(dst, src_offset); | |
d1310b2e | 5625 | |
884b07d0 QW |
5626 | dst_i = get_eb_page_index(dst_offset); |
5627 | src_i = get_eb_page_index(src_offset); | |
d1310b2e | 5628 | |
09cbfeaf | 5629 | cur = min(len, (unsigned long)(PAGE_SIZE - |
d1310b2e CM |
5630 | src_off_in_page)); |
5631 | cur = min_t(unsigned long, cur, | |
09cbfeaf | 5632 | (unsigned long)(PAGE_SIZE - dst_off_in_page)); |
d1310b2e | 5633 | |
fb85fc9a | 5634 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
d1310b2e CM |
5635 | dst_off_in_page, src_off_in_page, cur); |
5636 | ||
5637 | src_offset += cur; | |
5638 | dst_offset += cur; | |
5639 | len -= cur; | |
5640 | } | |
5641 | } | |
d1310b2e | 5642 | |
2b48966a DS |
5643 | void memmove_extent_buffer(const struct extent_buffer *dst, |
5644 | unsigned long dst_offset, unsigned long src_offset, | |
5645 | unsigned long len) | |
d1310b2e CM |
5646 | { |
5647 | size_t cur; | |
5648 | size_t dst_off_in_page; | |
5649 | size_t src_off_in_page; | |
5650 | unsigned long dst_end = dst_offset + len - 1; | |
5651 | unsigned long src_end = src_offset + len - 1; | |
d1310b2e CM |
5652 | unsigned long dst_i; |
5653 | unsigned long src_i; | |
5654 | ||
f98b6215 QW |
5655 | if (check_eb_range(dst, dst_offset, len) || |
5656 | check_eb_range(dst, src_offset, len)) | |
5657 | return; | |
727011e0 | 5658 | if (dst_offset < src_offset) { |
d1310b2e CM |
5659 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
5660 | return; | |
5661 | } | |
d397712b | 5662 | while (len > 0) { |
884b07d0 QW |
5663 | dst_i = get_eb_page_index(dst_end); |
5664 | src_i = get_eb_page_index(src_end); | |
d1310b2e | 5665 | |
884b07d0 QW |
5666 | dst_off_in_page = get_eb_offset_in_page(dst, dst_end); |
5667 | src_off_in_page = get_eb_offset_in_page(dst, src_end); | |
d1310b2e CM |
5668 | |
5669 | cur = min_t(unsigned long, len, src_off_in_page + 1); | |
5670 | cur = min(cur, dst_off_in_page + 1); | |
fb85fc9a | 5671 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
d1310b2e CM |
5672 | dst_off_in_page - cur + 1, |
5673 | src_off_in_page - cur + 1, cur); | |
5674 | ||
5675 | dst_end -= cur; | |
5676 | src_end -= cur; | |
5677 | len -= cur; | |
5678 | } | |
5679 | } | |
6af118ce | 5680 | |
01cd3909 | 5681 | #define GANG_LOOKUP_SIZE 16 |
d1e86e3f QW |
5682 | static struct extent_buffer *get_next_extent_buffer( |
5683 | struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr) | |
5684 | { | |
01cd3909 DS |
5685 | struct extent_buffer *gang[GANG_LOOKUP_SIZE]; |
5686 | struct extent_buffer *found = NULL; | |
d1e86e3f | 5687 | u64 page_start = page_offset(page); |
01cd3909 | 5688 | u64 cur = page_start; |
d1e86e3f QW |
5689 | |
5690 | ASSERT(in_range(bytenr, page_start, PAGE_SIZE)); | |
d1e86e3f QW |
5691 | lockdep_assert_held(&fs_info->buffer_lock); |
5692 | ||
01cd3909 DS |
5693 | while (cur < page_start + PAGE_SIZE) { |
5694 | int ret; | |
5695 | int i; | |
5696 | ||
5697 | ret = radix_tree_gang_lookup(&fs_info->buffer_radix, | |
5698 | (void **)gang, cur >> fs_info->sectorsize_bits, | |
5699 | min_t(unsigned int, GANG_LOOKUP_SIZE, | |
5700 | PAGE_SIZE / fs_info->nodesize)); | |
5701 | if (ret == 0) | |
5702 | goto out; | |
5703 | for (i = 0; i < ret; i++) { | |
5704 | /* Already beyond page end */ | |
5705 | if (gang[i]->start >= page_start + PAGE_SIZE) | |
5706 | goto out; | |
5707 | /* Found one */ | |
5708 | if (gang[i]->start >= bytenr) { | |
5709 | found = gang[i]; | |
5710 | goto out; | |
5711 | } | |
5712 | } | |
5713 | cur = gang[ret - 1]->start + gang[ret - 1]->len; | |
d1e86e3f | 5714 | } |
01cd3909 DS |
5715 | out: |
5716 | return found; | |
d1e86e3f QW |
5717 | } |
5718 | ||
5719 | static int try_release_subpage_extent_buffer(struct page *page) | |
5720 | { | |
5721 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); | |
5722 | u64 cur = page_offset(page); | |
5723 | const u64 end = page_offset(page) + PAGE_SIZE; | |
5724 | int ret; | |
5725 | ||
5726 | while (cur < end) { | |
5727 | struct extent_buffer *eb = NULL; | |
5728 | ||
5729 | /* | |
5730 | * Unlike try_release_extent_buffer() which uses page->private | |
5731 | * to grab buffer, for subpage case we rely on radix tree, thus | |
5732 | * we need to ensure radix tree consistency. | |
5733 | * | |
5734 | * We also want an atomic snapshot of the radix tree, thus go | |
5735 | * with spinlock rather than RCU. | |
5736 | */ | |
5737 | spin_lock(&fs_info->buffer_lock); | |
5738 | eb = get_next_extent_buffer(fs_info, page, cur); | |
5739 | if (!eb) { | |
5740 | /* No more eb in the page range after or at cur */ | |
5741 | spin_unlock(&fs_info->buffer_lock); | |
5742 | break; | |
5743 | } | |
5744 | cur = eb->start + eb->len; | |
5745 | ||
5746 | /* | |
5747 | * The same as try_release_extent_buffer(), to ensure the eb | |
5748 | * won't disappear out from under us. | |
5749 | */ | |
5750 | spin_lock(&eb->refs_lock); | |
5751 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { | |
5752 | spin_unlock(&eb->refs_lock); | |
5753 | spin_unlock(&fs_info->buffer_lock); | |
5754 | break; | |
5755 | } | |
5756 | spin_unlock(&fs_info->buffer_lock); | |
5757 | ||
5758 | /* | |
5759 | * If tree ref isn't set then we know the ref on this eb is a | |
5760 | * real ref, so just return, this eb will likely be freed soon | |
5761 | * anyway. | |
5762 | */ | |
5763 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { | |
5764 | spin_unlock(&eb->refs_lock); | |
5765 | break; | |
5766 | } | |
5767 | ||
5768 | /* | |
5769 | * Here we don't care about the return value, we will always | |
5770 | * check the page private at the end. And | |
5771 | * release_extent_buffer() will release the refs_lock. | |
5772 | */ | |
5773 | release_extent_buffer(eb); | |
5774 | } | |
5775 | /* | |
5776 | * Finally to check if we have cleared page private, as if we have | |
5777 | * released all ebs in the page, the page private should be cleared now. | |
5778 | */ | |
5779 | spin_lock(&page->mapping->private_lock); | |
5780 | if (!PagePrivate(page)) | |
5781 | ret = 1; | |
5782 | else | |
5783 | ret = 0; | |
5784 | spin_unlock(&page->mapping->private_lock); | |
5785 | return ret; | |
5786 | ||
5787 | } | |
5788 | ||
f7a52a40 | 5789 | int try_release_extent_buffer(struct page *page) |
19fe0a8b | 5790 | { |
6af118ce | 5791 | struct extent_buffer *eb; |
6af118ce | 5792 | |
fbca46eb | 5793 | if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) |
d1e86e3f QW |
5794 | return try_release_subpage_extent_buffer(page); |
5795 | ||
3083ee2e | 5796 | /* |
d1e86e3f QW |
5797 | * We need to make sure nobody is changing page->private, as we rely on |
5798 | * page->private as the pointer to extent buffer. | |
3083ee2e JB |
5799 | */ |
5800 | spin_lock(&page->mapping->private_lock); | |
5801 | if (!PagePrivate(page)) { | |
5802 | spin_unlock(&page->mapping->private_lock); | |
4f2de97a | 5803 | return 1; |
45f49bce | 5804 | } |
6af118ce | 5805 | |
3083ee2e JB |
5806 | eb = (struct extent_buffer *)page->private; |
5807 | BUG_ON(!eb); | |
19fe0a8b MX |
5808 | |
5809 | /* | |
3083ee2e JB |
5810 | * This is a little awful but should be ok, we need to make sure that |
5811 | * the eb doesn't disappear out from under us while we're looking at | |
5812 | * this page. | |
19fe0a8b | 5813 | */ |
3083ee2e | 5814 | spin_lock(&eb->refs_lock); |
0b32f4bb | 5815 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
3083ee2e JB |
5816 | spin_unlock(&eb->refs_lock); |
5817 | spin_unlock(&page->mapping->private_lock); | |
5818 | return 0; | |
b9473439 | 5819 | } |
3083ee2e | 5820 | spin_unlock(&page->mapping->private_lock); |
897ca6e9 | 5821 | |
19fe0a8b | 5822 | /* |
3083ee2e JB |
5823 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
5824 | * so just return, this page will likely be freed soon anyway. | |
19fe0a8b | 5825 | */ |
3083ee2e JB |
5826 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
5827 | spin_unlock(&eb->refs_lock); | |
5828 | return 0; | |
b9473439 | 5829 | } |
19fe0a8b | 5830 | |
f7a52a40 | 5831 | return release_extent_buffer(eb); |
6af118ce | 5832 | } |
bfb484d9 JB |
5833 | |
5834 | /* | |
5835 | * btrfs_readahead_tree_block - attempt to readahead a child block | |
5836 | * @fs_info: the fs_info | |
5837 | * @bytenr: bytenr to read | |
3fbaf258 | 5838 | * @owner_root: objectid of the root that owns this eb |
bfb484d9 | 5839 | * @gen: generation for the uptodate check, can be 0 |
3fbaf258 | 5840 | * @level: level for the eb |
bfb484d9 JB |
5841 | * |
5842 | * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a | |
5843 | * normal uptodate check of the eb, without checking the generation. If we have | |
5844 | * to read the block we will not block on anything. | |
5845 | */ | |
5846 | void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info, | |
3fbaf258 | 5847 | u64 bytenr, u64 owner_root, u64 gen, int level) |
bfb484d9 JB |
5848 | { |
5849 | struct extent_buffer *eb; | |
5850 | int ret; | |
5851 | ||
3fbaf258 | 5852 | eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); |
bfb484d9 JB |
5853 | if (IS_ERR(eb)) |
5854 | return; | |
5855 | ||
5856 | if (btrfs_buffer_uptodate(eb, gen, 1)) { | |
5857 | free_extent_buffer(eb); | |
5858 | return; | |
5859 | } | |
5860 | ||
5861 | ret = read_extent_buffer_pages(eb, WAIT_NONE, 0); | |
5862 | if (ret < 0) | |
5863 | free_extent_buffer_stale(eb); | |
5864 | else | |
5865 | free_extent_buffer(eb); | |
5866 | } | |
5867 | ||
5868 | /* | |
5869 | * btrfs_readahead_node_child - readahead a node's child block | |
5870 | * @node: parent node we're reading from | |
5871 | * @slot: slot in the parent node for the child we want to read | |
5872 | * | |
5873 | * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at | |
5874 | * the slot in the node provided. | |
5875 | */ | |
5876 | void btrfs_readahead_node_child(struct extent_buffer *node, int slot) | |
5877 | { | |
5878 | btrfs_readahead_tree_block(node->fs_info, | |
5879 | btrfs_node_blockptr(node, slot), | |
3fbaf258 JB |
5880 | btrfs_header_owner(node), |
5881 | btrfs_node_ptr_generation(node, slot), | |
5882 | btrfs_header_level(node) - 1); | |
bfb484d9 | 5883 | } |