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