1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitops.h>
4 #include <linux/slab.h>
7 #include <linux/pagemap.h>
8 #include <linux/page-flags.h>
9 #include <linux/sched/mm.h>
10 #include <linux/spinlock.h>
11 #include <linux/blkdev.h>
12 #include <linux/swap.h>
13 #include <linux/writeback.h>
14 #include <linux/pagevec.h>
15 #include <linux/prefetch.h>
16 #include <linux/fsverity.h>
17 #include "extent_io.h"
18 #include "extent-io-tree.h"
19 #include "extent_map.h"
21 #include "btrfs_inode.h"
28 #include "block-group.h"
29 #include "compression.h"
31 #include "accessors.h"
32 #include "file-item.h"
34 #include "dev-replace.h"
36 #include "transaction.h"
38 static struct kmem_cache *extent_buffer_cache;
40 #ifdef CONFIG_BTRFS_DEBUG
41 static inline void btrfs_leak_debug_add_eb(struct extent_buffer *eb)
43 struct btrfs_fs_info *fs_info = eb->fs_info;
46 spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
47 list_add(&eb->leak_list, &fs_info->allocated_ebs);
48 spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
51 static inline void btrfs_leak_debug_del_eb(struct extent_buffer *eb)
53 struct btrfs_fs_info *fs_info = eb->fs_info;
56 spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
57 list_del(&eb->leak_list);
58 spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
61 void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info)
63 struct extent_buffer *eb;
67 * If we didn't get into open_ctree our allocated_ebs will not be
68 * initialized, so just skip this.
70 if (!fs_info->allocated_ebs.next)
73 WARN_ON(!list_empty(&fs_info->allocated_ebs));
74 spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
75 while (!list_empty(&fs_info->allocated_ebs)) {
76 eb = list_first_entry(&fs_info->allocated_ebs,
77 struct extent_buffer, leak_list);
79 "BTRFS: buffer leak start %llu len %u refs %d bflags %lu owner %llu\n",
80 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags,
81 btrfs_header_owner(eb));
82 list_del(&eb->leak_list);
84 kmem_cache_free(extent_buffer_cache, eb);
86 spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
89 #define btrfs_leak_debug_add_eb(eb) do {} while (0)
90 #define btrfs_leak_debug_del_eb(eb) do {} while (0)
94 * Structure to record info about the bio being assembled, and other info like
95 * how many bytes are there before stripe/ordered extent boundary.
97 struct btrfs_bio_ctrl {
98 struct btrfs_bio *bbio;
99 enum btrfs_compression_type compress_type;
100 u32 len_to_oe_boundary;
102 btrfs_bio_end_io_t end_io_func;
103 struct writeback_control *wbc;
106 static void submit_one_bio(struct btrfs_bio_ctrl *bio_ctrl)
108 struct btrfs_bio *bbio = bio_ctrl->bbio;
113 /* Caller should ensure the bio has at least some range added */
114 ASSERT(bbio->bio.bi_iter.bi_size);
116 if (btrfs_op(&bbio->bio) == BTRFS_MAP_READ &&
117 bio_ctrl->compress_type != BTRFS_COMPRESS_NONE)
118 btrfs_submit_compressed_read(bbio);
120 btrfs_submit_bio(bbio, 0);
122 /* The bbio is owned by the end_io handler now */
123 bio_ctrl->bbio = NULL;
127 * Submit or fail the current bio in the bio_ctrl structure.
129 static void submit_write_bio(struct btrfs_bio_ctrl *bio_ctrl, int ret)
131 struct btrfs_bio *bbio = bio_ctrl->bbio;
138 btrfs_bio_end_io(bbio, errno_to_blk_status(ret));
139 /* The bio is owned by the end_io handler now */
140 bio_ctrl->bbio = NULL;
142 submit_one_bio(bio_ctrl);
146 int __init extent_buffer_init_cachep(void)
148 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
149 sizeof(struct extent_buffer), 0, 0,
151 if (!extent_buffer_cache)
157 void __cold extent_buffer_free_cachep(void)
160 * Make sure all delayed rcu free are flushed before we
164 kmem_cache_destroy(extent_buffer_cache);
167 void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
169 unsigned long index = start >> PAGE_SHIFT;
170 unsigned long end_index = end >> PAGE_SHIFT;
173 while (index <= end_index) {
174 page = find_get_page(inode->i_mapping, index);
175 BUG_ON(!page); /* Pages should be in the extent_io_tree */
176 clear_page_dirty_for_io(page);
182 static void process_one_page(struct btrfs_fs_info *fs_info,
183 struct page *page, struct page *locked_page,
184 unsigned long page_ops, u64 start, u64 end)
186 struct folio *folio = page_folio(page);
189 ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX);
190 len = end + 1 - start;
192 if (page_ops & PAGE_SET_ORDERED)
193 btrfs_folio_clamp_set_ordered(fs_info, folio, start, len);
194 if (page_ops & PAGE_START_WRITEBACK) {
195 btrfs_folio_clamp_clear_dirty(fs_info, folio, start, len);
196 btrfs_folio_clamp_set_writeback(fs_info, folio, start, len);
198 if (page_ops & PAGE_END_WRITEBACK)
199 btrfs_folio_clamp_clear_writeback(fs_info, folio, start, len);
201 if (page != locked_page && (page_ops & PAGE_UNLOCK))
202 btrfs_folio_end_writer_lock(fs_info, folio, start, len);
205 static void __process_pages_contig(struct address_space *mapping,
206 struct page *locked_page, u64 start, u64 end,
207 unsigned long page_ops)
209 struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host);
210 pgoff_t start_index = start >> PAGE_SHIFT;
211 pgoff_t end_index = end >> PAGE_SHIFT;
212 pgoff_t index = start_index;
213 struct folio_batch fbatch;
216 folio_batch_init(&fbatch);
217 while (index <= end_index) {
220 found_folios = filemap_get_folios_contig(mapping, &index,
222 for (i = 0; i < found_folios; i++) {
223 struct folio *folio = fbatch.folios[i];
225 process_one_page(fs_info, &folio->page, locked_page,
226 page_ops, start, end);
228 folio_batch_release(&fbatch);
233 static noinline void __unlock_for_delalloc(struct inode *inode,
234 struct page *locked_page,
237 unsigned long index = start >> PAGE_SHIFT;
238 unsigned long end_index = end >> PAGE_SHIFT;
241 if (index == locked_page->index && end_index == index)
244 __process_pages_contig(inode->i_mapping, locked_page, start, end,
248 static noinline int lock_delalloc_pages(struct inode *inode,
249 struct page *locked_page,
253 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
254 struct address_space *mapping = inode->i_mapping;
255 pgoff_t start_index = start >> PAGE_SHIFT;
256 pgoff_t end_index = end >> PAGE_SHIFT;
257 pgoff_t index = start_index;
258 u64 processed_end = start;
259 struct folio_batch fbatch;
261 if (index == locked_page->index && index == end_index)
264 folio_batch_init(&fbatch);
265 while (index <= end_index) {
266 unsigned int found_folios, i;
268 found_folios = filemap_get_folios_contig(mapping, &index,
270 if (found_folios == 0)
273 for (i = 0; i < found_folios; i++) {
274 struct folio *folio = fbatch.folios[i];
275 struct page *page = folio_page(folio, 0);
276 u32 len = end + 1 - start;
278 if (page == locked_page)
281 if (btrfs_folio_start_writer_lock(fs_info, folio, start,
285 if (!PageDirty(page) || page->mapping != mapping) {
286 btrfs_folio_end_writer_lock(fs_info, folio, start,
291 processed_end = page_offset(page) + PAGE_SIZE - 1;
293 folio_batch_release(&fbatch);
299 folio_batch_release(&fbatch);
300 if (processed_end > start)
301 __unlock_for_delalloc(inode, locked_page, start, processed_end);
306 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
307 * more than @max_bytes.
309 * @start: The original start bytenr to search.
310 * Will store the extent range start bytenr.
311 * @end: The original end bytenr of the search range
312 * Will store the extent range end bytenr.
314 * Return true if we find a delalloc range which starts inside the original
315 * range, and @start/@end will store the delalloc range start/end.
317 * Return false if we can't find any delalloc range which starts inside the
318 * original range, and @start/@end will be the non-delalloc range start/end.
321 noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
322 struct page *locked_page, u64 *start,
325 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
326 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
327 const u64 orig_start = *start;
328 const u64 orig_end = *end;
329 /* The sanity tests may not set a valid fs_info. */
330 u64 max_bytes = fs_info ? fs_info->max_extent_size : BTRFS_MAX_EXTENT_SIZE;
334 struct extent_state *cached_state = NULL;
338 /* Caller should pass a valid @end to indicate the search range end */
339 ASSERT(orig_end > orig_start);
341 /* The range should at least cover part of the page */
342 ASSERT(!(orig_start >= page_offset(locked_page) + PAGE_SIZE ||
343 orig_end <= page_offset(locked_page)));
345 /* step one, find a bunch of delalloc bytes starting at start */
346 delalloc_start = *start;
348 found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end,
349 max_bytes, &cached_state);
350 if (!found || delalloc_end <= *start || delalloc_start > orig_end) {
351 *start = delalloc_start;
353 /* @delalloc_end can be -1, never go beyond @orig_end */
354 *end = min(delalloc_end, orig_end);
355 free_extent_state(cached_state);
360 * start comes from the offset of locked_page. We have to lock
361 * pages in order, so we can't process delalloc bytes before
364 if (delalloc_start < *start)
365 delalloc_start = *start;
368 * make sure to limit the number of pages we try to lock down
370 if (delalloc_end + 1 - delalloc_start > max_bytes)
371 delalloc_end = delalloc_start + max_bytes - 1;
373 /* step two, lock all the pages after the page that has start */
374 ret = lock_delalloc_pages(inode, locked_page,
375 delalloc_start, delalloc_end);
376 ASSERT(!ret || ret == -EAGAIN);
377 if (ret == -EAGAIN) {
378 /* some of the pages are gone, lets avoid looping by
379 * shortening the size of the delalloc range we're searching
381 free_extent_state(cached_state);
384 max_bytes = PAGE_SIZE;
393 /* step three, lock the state bits for the whole range */
394 lock_extent(tree, delalloc_start, delalloc_end, &cached_state);
396 /* then test to make sure it is all still delalloc */
397 ret = test_range_bit(tree, delalloc_start, delalloc_end,
398 EXTENT_DELALLOC, cached_state);
400 unlock_extent(tree, delalloc_start, delalloc_end, &cached_state);
402 __unlock_for_delalloc(inode, locked_page,
403 delalloc_start, delalloc_end);
407 *start = delalloc_start;
413 void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
414 struct page *locked_page,
415 struct extent_state **cached,
416 u32 clear_bits, unsigned long page_ops)
418 clear_extent_bit(&inode->io_tree, start, end, clear_bits, cached);
420 __process_pages_contig(inode->vfs_inode.i_mapping, locked_page,
421 start, end, page_ops);
424 static bool btrfs_verify_page(struct page *page, u64 start)
426 if (!fsverity_active(page->mapping->host) ||
427 PageUptodate(page) ||
428 start >= i_size_read(page->mapping->host))
430 return fsverity_verify_page(page);
433 static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len)
435 struct btrfs_fs_info *fs_info = page_to_fs_info(page);
436 struct folio *folio = page_folio(page);
438 ASSERT(page_offset(page) <= start &&
439 start + len <= page_offset(page) + PAGE_SIZE);
441 if (uptodate && btrfs_verify_page(page, start))
442 btrfs_folio_set_uptodate(fs_info, folio, start, len);
444 btrfs_folio_clear_uptodate(fs_info, folio, start, len);
446 if (!btrfs_is_subpage(fs_info, page->mapping))
449 btrfs_subpage_end_reader(fs_info, folio, start, len);
453 * After a write IO is done, we need to:
455 * - clear the uptodate bits on error
456 * - clear the writeback bits in the extent tree for the range
457 * - filio_end_writeback() if there is no more pending io for the folio
459 * Scheduling is not allowed, so the extent state tree is expected
460 * to have one and only one object corresponding to this IO.
462 static void end_bbio_data_write(struct btrfs_bio *bbio)
464 struct btrfs_fs_info *fs_info = bbio->fs_info;
465 struct bio *bio = &bbio->bio;
466 int error = blk_status_to_errno(bio->bi_status);
467 struct folio_iter fi;
468 const u32 sectorsize = fs_info->sectorsize;
470 ASSERT(!bio_flagged(bio, BIO_CLONED));
471 bio_for_each_folio_all(fi, bio) {
472 struct folio *folio = fi.folio;
473 u64 start = folio_pos(folio) + fi.offset;
476 /* Only order 0 (single page) folios are allowed for data. */
477 ASSERT(folio_order(folio) == 0);
479 /* Our read/write should always be sector aligned. */
480 if (!IS_ALIGNED(fi.offset, sectorsize))
482 "partial page write in btrfs with offset %zu and length %zu",
483 fi.offset, fi.length);
484 else if (!IS_ALIGNED(fi.length, sectorsize))
486 "incomplete page write with offset %zu and length %zu",
487 fi.offset, fi.length);
489 btrfs_finish_ordered_extent(bbio->ordered,
490 folio_page(folio, 0), start, len, !error);
492 mapping_set_error(folio->mapping, error);
493 btrfs_folio_clear_writeback(fs_info, folio, start, len);
500 * Record previously processed extent range
502 * For endio_readpage_release_extent() to handle a full extent range, reducing
503 * the extent io operations.
505 struct processed_extent {
506 struct btrfs_inode *inode;
507 /* Start of the range in @inode */
509 /* End of the range in @inode */
515 * Try to release processed extent range
517 * May not release the extent range right now if the current range is
518 * contiguous to processed extent.
520 * Will release processed extent when any of @inode, @uptodate, the range is
521 * no longer contiguous to the processed range.
523 * Passing @inode == NULL will force processed extent to be released.
525 static void endio_readpage_release_extent(struct processed_extent *processed,
526 struct btrfs_inode *inode, u64 start, u64 end,
529 struct extent_state *cached = NULL;
530 struct extent_io_tree *tree;
532 /* The first extent, initialize @processed */
533 if (!processed->inode)
537 * Contiguous to processed extent, just uptodate the end.
539 * Several things to notice:
541 * - bio can be merged as long as on-disk bytenr is contiguous
542 * This means we can have page belonging to other inodes, thus need to
543 * check if the inode still matches.
544 * - bvec can contain range beyond current page for multi-page bvec
545 * Thus we need to do processed->end + 1 >= start check
547 if (processed->inode == inode && processed->uptodate == uptodate &&
548 processed->end + 1 >= start && end >= processed->end) {
549 processed->end = end;
553 tree = &processed->inode->io_tree;
555 * Now we don't have range contiguous to the processed range, release
556 * the processed range now.
558 unlock_extent(tree, processed->start, processed->end, &cached);
561 /* Update processed to current range */
562 processed->inode = inode;
563 processed->start = start;
564 processed->end = end;
565 processed->uptodate = uptodate;
568 static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page)
570 struct folio *folio = page_folio(page);
572 ASSERT(folio_test_locked(folio));
573 if (!btrfs_is_subpage(fs_info, folio->mapping))
576 ASSERT(folio_test_private(folio));
577 btrfs_subpage_start_reader(fs_info, folio, page_offset(page), PAGE_SIZE);
581 * After a data read IO is done, we need to:
583 * - clear the uptodate bits on error
584 * - set the uptodate bits if things worked
585 * - set the folio up to date if all extents in the tree are uptodate
586 * - clear the lock bit in the extent tree
587 * - unlock the folio if there are no other extents locked for it
589 * Scheduling is not allowed, so the extent state tree is expected
590 * to have one and only one object corresponding to this IO.
592 static void end_bbio_data_read(struct btrfs_bio *bbio)
594 struct btrfs_fs_info *fs_info = bbio->fs_info;
595 struct bio *bio = &bbio->bio;
596 struct processed_extent processed = { 0 };
597 struct folio_iter fi;
598 const u32 sectorsize = fs_info->sectorsize;
600 ASSERT(!bio_flagged(bio, BIO_CLONED));
601 bio_for_each_folio_all(fi, &bbio->bio) {
602 bool uptodate = !bio->bi_status;
603 struct folio *folio = fi.folio;
604 struct inode *inode = folio->mapping->host;
609 /* For now only order 0 folios are supported for data. */
610 ASSERT(folio_order(folio) == 0);
612 "%s: bi_sector=%llu, err=%d, mirror=%u",
613 __func__, bio->bi_iter.bi_sector, bio->bi_status,
617 * We always issue full-sector reads, but if some block in a
618 * folio fails to read, blk_update_request() will advance
619 * bv_offset and adjust bv_len to compensate. Print a warning
620 * for unaligned offsets, and an error if they don't add up to
623 if (!IS_ALIGNED(fi.offset, sectorsize))
625 "partial page read in btrfs with offset %zu and length %zu",
626 fi.offset, fi.length);
627 else if (!IS_ALIGNED(fi.offset + fi.length, sectorsize))
629 "incomplete page read with offset %zu and length %zu",
630 fi.offset, fi.length);
632 start = folio_pos(folio) + fi.offset;
633 end = start + fi.length - 1;
636 if (likely(uptodate)) {
637 loff_t i_size = i_size_read(inode);
638 pgoff_t end_index = i_size >> folio_shift(folio);
641 * Zero out the remaining part if this range straddles
644 * Here we should only zero the range inside the folio,
645 * not touch anything else.
647 * NOTE: i_size is exclusive while end is inclusive.
649 if (folio_index(folio) == end_index && i_size <= end) {
650 u32 zero_start = max(offset_in_folio(folio, i_size),
651 offset_in_folio(folio, start));
652 u32 zero_len = offset_in_folio(folio, end) + 1 -
655 folio_zero_range(folio, zero_start, zero_len);
659 /* Update page status and unlock. */
660 end_page_read(folio_page(folio, 0), uptodate, start, len);
661 endio_readpage_release_extent(&processed, BTRFS_I(inode),
662 start, end, uptodate);
664 /* Release the last extent */
665 endio_readpage_release_extent(&processed, NULL, 0, 0, false);
670 * Populate every free slot in a provided array with folios.
672 * @nr_folios: number of folios to allocate
673 * @folio_array: the array to fill with folios; any existing non-NULL entries in
674 * the array will be skipped
675 * @extra_gfp: the extra GFP flags for the allocation
677 * Return: 0 if all folios were able to be allocated;
678 * -ENOMEM otherwise, the partially allocated folios would be freed and
679 * the array slots zeroed
681 int btrfs_alloc_folio_array(unsigned int nr_folios, struct folio **folio_array,
684 for (int i = 0; i < nr_folios; i++) {
687 folio_array[i] = folio_alloc(GFP_NOFS | extra_gfp, 0);
693 for (int i = 0; i < nr_folios; i++) {
695 folio_put(folio_array[i]);
701 * Populate every free slot in a provided array with pages.
703 * @nr_pages: number of pages to allocate
704 * @page_array: the array to fill with pages; any existing non-null entries in
705 * the array will be skipped
706 * @extra_gfp: the extra GFP flags for the allocation.
708 * Return: 0 if all pages were able to be allocated;
709 * -ENOMEM otherwise, the partially allocated pages would be freed and
710 * the array slots zeroed
712 int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array,
715 const gfp_t gfp = GFP_NOFS | extra_gfp;
716 unsigned int allocated;
718 for (allocated = 0; allocated < nr_pages;) {
719 unsigned int last = allocated;
721 allocated = alloc_pages_bulk_array(gfp, nr_pages, page_array);
722 if (unlikely(allocated == last)) {
723 /* No progress, fail and do cleanup. */
724 for (int i = 0; i < allocated; i++) {
725 __free_page(page_array[i]);
726 page_array[i] = NULL;
735 * Populate needed folios for the extent buffer.
737 * For now, the folios populated are always in order 0 (aka, single page).
739 static int alloc_eb_folio_array(struct extent_buffer *eb, gfp_t extra_gfp)
741 struct page *page_array[INLINE_EXTENT_BUFFER_PAGES] = { 0 };
742 int num_pages = num_extent_pages(eb);
745 ret = btrfs_alloc_page_array(num_pages, page_array, extra_gfp);
749 for (int i = 0; i < num_pages; i++)
750 eb->folios[i] = page_folio(page_array[i]);
751 eb->folio_size = PAGE_SIZE;
752 eb->folio_shift = PAGE_SHIFT;
756 static bool btrfs_bio_is_contig(struct btrfs_bio_ctrl *bio_ctrl,
757 struct page *page, u64 disk_bytenr,
758 unsigned int pg_offset)
760 struct bio *bio = &bio_ctrl->bbio->bio;
761 struct bio_vec *bvec = bio_last_bvec_all(bio);
762 const sector_t sector = disk_bytenr >> SECTOR_SHIFT;
764 if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) {
766 * For compression, all IO should have its logical bytenr set
767 * to the starting bytenr of the compressed extent.
769 return bio->bi_iter.bi_sector == sector;
773 * The contig check requires the following conditions to be met:
775 * 1) The pages are belonging to the same inode
776 * This is implied by the call chain.
778 * 2) The range has adjacent logical bytenr
780 * 3) The range has adjacent file offset
781 * This is required for the usage of btrfs_bio->file_offset.
783 return bio_end_sector(bio) == sector &&
784 page_offset(bvec->bv_page) + bvec->bv_offset + bvec->bv_len ==
785 page_offset(page) + pg_offset;
788 static void alloc_new_bio(struct btrfs_inode *inode,
789 struct btrfs_bio_ctrl *bio_ctrl,
790 u64 disk_bytenr, u64 file_offset)
792 struct btrfs_fs_info *fs_info = inode->root->fs_info;
793 struct btrfs_bio *bbio;
795 bbio = btrfs_bio_alloc(BIO_MAX_VECS, bio_ctrl->opf, fs_info,
796 bio_ctrl->end_io_func, NULL);
797 bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
799 bbio->file_offset = file_offset;
800 bio_ctrl->bbio = bbio;
801 bio_ctrl->len_to_oe_boundary = U32_MAX;
803 /* Limit data write bios to the ordered boundary. */
805 struct btrfs_ordered_extent *ordered;
807 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
809 bio_ctrl->len_to_oe_boundary = min_t(u32, U32_MAX,
810 ordered->file_offset +
811 ordered->disk_num_bytes - file_offset);
812 bbio->ordered = ordered;
816 * Pick the last added device to support cgroup writeback. For
817 * multi-device file systems this means blk-cgroup policies have
818 * to always be set on the last added/replaced device.
819 * This is a bit odd but has been like that for a long time.
821 bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev);
822 wbc_init_bio(bio_ctrl->wbc, &bbio->bio);
827 * @disk_bytenr: logical bytenr where the write will be
828 * @page: page to add to the bio
829 * @size: portion of page that we want to write to
830 * @pg_offset: offset of the new bio or to check whether we are adding
831 * a contiguous page to the previous one
833 * The will either add the page into the existing @bio_ctrl->bbio, or allocate a
834 * new one in @bio_ctrl->bbio.
835 * The mirror number for this IO should already be initizlied in
836 * @bio_ctrl->mirror_num.
838 static void submit_extent_page(struct btrfs_bio_ctrl *bio_ctrl,
839 u64 disk_bytenr, struct page *page,
840 size_t size, unsigned long pg_offset)
842 struct btrfs_inode *inode = page_to_inode(page);
844 ASSERT(pg_offset + size <= PAGE_SIZE);
845 ASSERT(bio_ctrl->end_io_func);
847 if (bio_ctrl->bbio &&
848 !btrfs_bio_is_contig(bio_ctrl, page, disk_bytenr, pg_offset))
849 submit_one_bio(bio_ctrl);
854 /* Allocate new bio if needed */
855 if (!bio_ctrl->bbio) {
856 alloc_new_bio(inode, bio_ctrl, disk_bytenr,
857 page_offset(page) + pg_offset);
860 /* Cap to the current ordered extent boundary if there is one. */
861 if (len > bio_ctrl->len_to_oe_boundary) {
862 ASSERT(bio_ctrl->compress_type == BTRFS_COMPRESS_NONE);
863 ASSERT(is_data_inode(&inode->vfs_inode));
864 len = bio_ctrl->len_to_oe_boundary;
867 if (bio_add_page(&bio_ctrl->bbio->bio, page, len, pg_offset) != len) {
868 /* bio full: move on to a new one */
869 submit_one_bio(bio_ctrl);
874 wbc_account_cgroup_owner(bio_ctrl->wbc, page, len);
881 * len_to_oe_boundary defaults to U32_MAX, which isn't page or
882 * sector aligned. alloc_new_bio() then sets it to the end of
883 * our ordered extent for writes into zoned devices.
885 * When len_to_oe_boundary is tracking an ordered extent, we
886 * trust the ordered extent code to align things properly, and
887 * the check above to cap our write to the ordered extent
888 * boundary is correct.
890 * When len_to_oe_boundary is U32_MAX, the cap above would
891 * result in a 4095 byte IO for the last page right before
892 * we hit the bio limit of UINT_MAX. bio_add_page() has all
893 * the checks required to make sure we don't overflow the bio,
894 * and we should just ignore len_to_oe_boundary completely
895 * unless we're using it to track an ordered extent.
897 * It's pretty hard to make a bio sized U32_MAX, but it can
898 * happen when the page cache is able to feed us contiguous
899 * pages for large extents.
901 if (bio_ctrl->len_to_oe_boundary != U32_MAX)
902 bio_ctrl->len_to_oe_boundary -= len;
904 /* Ordered extent boundary: move on to a new bio. */
905 if (bio_ctrl->len_to_oe_boundary == 0)
906 submit_one_bio(bio_ctrl);
910 static int attach_extent_buffer_folio(struct extent_buffer *eb,
912 struct btrfs_subpage *prealloc)
914 struct btrfs_fs_info *fs_info = eb->fs_info;
918 * If the page is mapped to btree inode, we should hold the private
919 * lock to prevent race.
920 * For cloned or dummy extent buffers, their pages are not mapped and
921 * will not race with any other ebs.
924 lockdep_assert_held(&folio->mapping->i_private_lock);
926 if (fs_info->nodesize >= PAGE_SIZE) {
927 if (!folio_test_private(folio))
928 folio_attach_private(folio, eb);
930 WARN_ON(folio_get_private(folio) != eb);
934 /* Already mapped, just free prealloc */
935 if (folio_test_private(folio)) {
936 btrfs_free_subpage(prealloc);
941 /* Has preallocated memory for subpage */
942 folio_attach_private(folio, prealloc);
944 /* Do new allocation to attach subpage */
945 ret = btrfs_attach_subpage(fs_info, folio, BTRFS_SUBPAGE_METADATA);
949 int set_page_extent_mapped(struct page *page)
951 return set_folio_extent_mapped(page_folio(page));
954 int set_folio_extent_mapped(struct folio *folio)
956 struct btrfs_fs_info *fs_info;
958 ASSERT(folio->mapping);
960 if (folio_test_private(folio))
963 fs_info = folio_to_fs_info(folio);
965 if (btrfs_is_subpage(fs_info, folio->mapping))
966 return btrfs_attach_subpage(fs_info, folio, BTRFS_SUBPAGE_DATA);
968 folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
972 void clear_page_extent_mapped(struct page *page)
974 struct folio *folio = page_folio(page);
975 struct btrfs_fs_info *fs_info;
977 ASSERT(page->mapping);
979 if (!folio_test_private(folio))
982 fs_info = page_to_fs_info(page);
983 if (btrfs_is_subpage(fs_info, page->mapping))
984 return btrfs_detach_subpage(fs_info, folio);
986 folio_detach_private(folio);
989 static struct extent_map *__get_extent_map(struct inode *inode, struct page *page,
990 u64 start, u64 len, struct extent_map **em_cached)
992 struct extent_map *em;
998 if (extent_map_in_tree(em) && start >= em->start &&
999 start < extent_map_end(em)) {
1000 refcount_inc(&em->refs);
1004 free_extent_map(em);
1008 em = btrfs_get_extent(BTRFS_I(inode), page, start, len);
1011 refcount_inc(&em->refs);
1017 * basic readpage implementation. Locked extent state structs are inserted
1018 * into the tree that are removed when the IO is done (by the end_io
1020 * XXX JDM: This needs looking at to ensure proper page locking
1021 * return 0 on success, otherwise return error
1023 static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
1024 struct btrfs_bio_ctrl *bio_ctrl, u64 *prev_em_start)
1026 struct inode *inode = page->mapping->host;
1027 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
1028 u64 start = page_offset(page);
1029 const u64 end = start + PAGE_SIZE - 1;
1032 u64 last_byte = i_size_read(inode);
1034 struct extent_map *em;
1036 size_t pg_offset = 0;
1038 size_t blocksize = fs_info->sectorsize;
1039 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
1041 ret = set_page_extent_mapped(page);
1043 unlock_extent(tree, start, end, NULL);
1048 if (page->index == last_byte >> PAGE_SHIFT) {
1049 size_t zero_offset = offset_in_page(last_byte);
1052 iosize = PAGE_SIZE - zero_offset;
1053 memzero_page(page, zero_offset, iosize);
1056 bio_ctrl->end_io_func = end_bbio_data_read;
1057 begin_page_read(fs_info, page);
1058 while (cur <= end) {
1059 enum btrfs_compression_type compress_type = BTRFS_COMPRESS_NONE;
1060 bool force_bio_submit = false;
1063 ASSERT(IS_ALIGNED(cur, fs_info->sectorsize));
1064 if (cur >= last_byte) {
1065 iosize = PAGE_SIZE - pg_offset;
1066 memzero_page(page, pg_offset, iosize);
1067 unlock_extent(tree, cur, cur + iosize - 1, NULL);
1068 end_page_read(page, true, cur, iosize);
1071 em = __get_extent_map(inode, page, cur, end - cur + 1, em_cached);
1073 unlock_extent(tree, cur, end, NULL);
1074 end_page_read(page, false, cur, end + 1 - cur);
1077 extent_offset = cur - em->start;
1078 BUG_ON(extent_map_end(em) <= cur);
1081 compress_type = extent_map_compression(em);
1083 iosize = min(extent_map_end(em) - cur, end - cur + 1);
1084 iosize = ALIGN(iosize, blocksize);
1085 if (compress_type != BTRFS_COMPRESS_NONE)
1086 disk_bytenr = em->block_start;
1088 disk_bytenr = em->block_start + extent_offset;
1089 block_start = em->block_start;
1090 if (em->flags & EXTENT_FLAG_PREALLOC)
1091 block_start = EXTENT_MAP_HOLE;
1094 * If we have a file range that points to a compressed extent
1095 * and it's followed by a consecutive file range that points
1096 * to the same compressed extent (possibly with a different
1097 * offset and/or length, so it either points to the whole extent
1098 * or only part of it), we must make sure we do not submit a
1099 * single bio to populate the pages for the 2 ranges because
1100 * this makes the compressed extent read zero out the pages
1101 * belonging to the 2nd range. Imagine the following scenario:
1104 * [0 - 8K] [8K - 24K]
1107 * points to extent X, points to extent X,
1108 * offset 4K, length of 8K offset 0, length 16K
1110 * [extent X, compressed length = 4K uncompressed length = 16K]
1112 * If the bio to read the compressed extent covers both ranges,
1113 * it will decompress extent X into the pages belonging to the
1114 * first range and then it will stop, zeroing out the remaining
1115 * pages that belong to the other range that points to extent X.
1116 * So here we make sure we submit 2 bios, one for the first
1117 * range and another one for the third range. Both will target
1118 * the same physical extent from disk, but we can't currently
1119 * make the compressed bio endio callback populate the pages
1120 * for both ranges because each compressed bio is tightly
1121 * coupled with a single extent map, and each range can have
1122 * an extent map with a different offset value relative to the
1123 * uncompressed data of our extent and different lengths. This
1124 * is a corner case so we prioritize correctness over
1125 * non-optimal behavior (submitting 2 bios for the same extent).
1127 if (compress_type != BTRFS_COMPRESS_NONE &&
1128 prev_em_start && *prev_em_start != (u64)-1 &&
1129 *prev_em_start != em->start)
1130 force_bio_submit = true;
1133 *prev_em_start = em->start;
1135 free_extent_map(em);
1138 /* we've found a hole, just zero and go on */
1139 if (block_start == EXTENT_MAP_HOLE) {
1140 memzero_page(page, pg_offset, iosize);
1142 unlock_extent(tree, cur, cur + iosize - 1, NULL);
1143 end_page_read(page, true, cur, iosize);
1145 pg_offset += iosize;
1148 /* the get_extent function already copied into the page */
1149 if (block_start == EXTENT_MAP_INLINE) {
1150 unlock_extent(tree, cur, cur + iosize - 1, NULL);
1151 end_page_read(page, true, cur, iosize);
1153 pg_offset += iosize;
1157 if (bio_ctrl->compress_type != compress_type) {
1158 submit_one_bio(bio_ctrl);
1159 bio_ctrl->compress_type = compress_type;
1162 if (force_bio_submit)
1163 submit_one_bio(bio_ctrl);
1164 submit_extent_page(bio_ctrl, disk_bytenr, page, iosize,
1167 pg_offset += iosize;
1173 int btrfs_read_folio(struct file *file, struct folio *folio)
1175 struct page *page = &folio->page;
1176 struct btrfs_inode *inode = page_to_inode(page);
1177 u64 start = page_offset(page);
1178 u64 end = start + PAGE_SIZE - 1;
1179 struct btrfs_bio_ctrl bio_ctrl = { .opf = REQ_OP_READ };
1180 struct extent_map *em_cached = NULL;
1183 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
1185 ret = btrfs_do_readpage(page, &em_cached, &bio_ctrl, NULL);
1186 free_extent_map(em_cached);
1189 * If btrfs_do_readpage() failed we will want to submit the assembled
1190 * bio to do the cleanup.
1192 submit_one_bio(&bio_ctrl);
1196 static inline void contiguous_readpages(struct page *pages[], int nr_pages,
1198 struct extent_map **em_cached,
1199 struct btrfs_bio_ctrl *bio_ctrl,
1202 struct btrfs_inode *inode = page_to_inode(pages[0]);
1207 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
1209 for (index = 0; index < nr_pages; index++) {
1210 btrfs_do_readpage(pages[index], em_cached, bio_ctrl,
1212 put_page(pages[index]);
1217 * helper for __extent_writepage, doing all of the delayed allocation setup.
1219 * This returns 1 if btrfs_run_delalloc_range function did all the work required
1220 * to write the page (copy into inline extent). In this case the IO has
1221 * been started and the page is already unlocked.
1223 * This returns 0 if all went well (page still locked)
1224 * This returns < 0 if there were errors (page still locked)
1226 static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
1227 struct page *page, struct writeback_control *wbc)
1229 const u64 page_start = page_offset(page);
1230 const u64 page_end = page_start + PAGE_SIZE - 1;
1231 u64 delalloc_start = page_start;
1232 u64 delalloc_end = page_end;
1233 u64 delalloc_to_write = 0;
1236 while (delalloc_start < page_end) {
1237 delalloc_end = page_end;
1238 if (!find_lock_delalloc_range(&inode->vfs_inode, page,
1239 &delalloc_start, &delalloc_end)) {
1240 delalloc_start = delalloc_end + 1;
1244 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
1249 delalloc_start = delalloc_end + 1;
1253 * delalloc_end is already one less than the total length, so
1254 * we don't subtract one from PAGE_SIZE
1256 delalloc_to_write +=
1257 DIV_ROUND_UP(delalloc_end + 1 - page_start, PAGE_SIZE);
1260 * If btrfs_run_dealloc_range() already started I/O and unlocked
1261 * the pages, we just need to account for them here.
1264 wbc->nr_to_write -= delalloc_to_write;
1268 if (wbc->nr_to_write < delalloc_to_write) {
1271 if (delalloc_to_write < thresh * 2)
1272 thresh = delalloc_to_write;
1273 wbc->nr_to_write = min_t(u64, delalloc_to_write,
1281 * Find the first byte we need to write.
1283 * For subpage, one page can contain several sectors, and
1284 * __extent_writepage_io() will just grab all extent maps in the page
1285 * range and try to submit all non-inline/non-compressed extents.
1287 * This is a big problem for subpage, we shouldn't re-submit already written
1289 * This function will lookup subpage dirty bit to find which range we really
1292 * Return the next dirty range in [@start, @end).
1293 * If no dirty range is found, @start will be page_offset(page) + PAGE_SIZE.
1295 static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
1296 struct page *page, u64 *start, u64 *end)
1298 struct folio *folio = page_folio(page);
1299 struct btrfs_subpage *subpage = folio_get_private(folio);
1300 struct btrfs_subpage_info *spi = fs_info->subpage_info;
1301 u64 orig_start = *start;
1302 /* Declare as unsigned long so we can use bitmap ops */
1303 unsigned long flags;
1304 int range_start_bit;
1308 * For regular sector size == page size case, since one page only
1309 * contains one sector, we return the page offset directly.
1311 if (!btrfs_is_subpage(fs_info, page->mapping)) {
1312 *start = page_offset(page);
1313 *end = page_offset(page) + PAGE_SIZE;
1317 range_start_bit = spi->dirty_offset +
1318 (offset_in_page(orig_start) >> fs_info->sectorsize_bits);
1320 /* We should have the page locked, but just in case */
1321 spin_lock_irqsave(&subpage->lock, flags);
1322 bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit,
1323 spi->dirty_offset + spi->bitmap_nr_bits);
1324 spin_unlock_irqrestore(&subpage->lock, flags);
1326 range_start_bit -= spi->dirty_offset;
1327 range_end_bit -= spi->dirty_offset;
1329 *start = page_offset(page) + range_start_bit * fs_info->sectorsize;
1330 *end = page_offset(page) + range_end_bit * fs_info->sectorsize;
1334 * helper for __extent_writepage. This calls the writepage start hooks,
1335 * and does the loop to map the page into extents and bios.
1337 * We return 1 if the IO is started and the page is unlocked,
1338 * 0 if all went well (page still locked)
1339 * < 0 if there were errors (page still locked)
1341 static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
1343 struct btrfs_bio_ctrl *bio_ctrl,
1347 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1348 u64 cur = page_offset(page);
1349 u64 end = cur + PAGE_SIZE - 1;
1352 struct extent_map *em;
1356 ret = btrfs_writepage_cow_fixup(page);
1358 /* Fixup worker will requeue */
1359 redirty_page_for_writepage(bio_ctrl->wbc, page);
1364 bio_ctrl->end_io_func = end_bbio_data_write;
1365 while (cur <= end) {
1366 u32 len = end - cur + 1;
1369 u64 dirty_range_start = cur;
1370 u64 dirty_range_end;
1373 if (cur >= i_size) {
1374 btrfs_mark_ordered_io_finished(inode, page, cur, len,
1377 * This range is beyond i_size, thus we don't need to
1378 * bother writing back.
1379 * But we still need to clear the dirty subpage bit, or
1380 * the next time the page gets dirtied, we will try to
1381 * writeback the sectors with subpage dirty bits,
1382 * causing writeback without ordered extent.
1384 btrfs_folio_clear_dirty(fs_info, page_folio(page), cur, len);
1388 find_next_dirty_byte(fs_info, page, &dirty_range_start,
1390 if (cur < dirty_range_start) {
1391 cur = dirty_range_start;
1395 em = btrfs_get_extent(inode, NULL, cur, len);
1397 ret = PTR_ERR_OR_ZERO(em);
1401 extent_offset = cur - em->start;
1402 em_end = extent_map_end(em);
1403 ASSERT(cur <= em_end);
1405 ASSERT(IS_ALIGNED(em->start, fs_info->sectorsize));
1406 ASSERT(IS_ALIGNED(em->len, fs_info->sectorsize));
1408 block_start = em->block_start;
1409 disk_bytenr = em->block_start + extent_offset;
1411 ASSERT(!extent_map_is_compressed(em));
1412 ASSERT(block_start != EXTENT_MAP_HOLE);
1413 ASSERT(block_start != EXTENT_MAP_INLINE);
1416 * Note that em_end from extent_map_end() and dirty_range_end from
1417 * find_next_dirty_byte() are all exclusive
1419 iosize = min(min(em_end, end + 1), dirty_range_end) - cur;
1420 free_extent_map(em);
1423 btrfs_set_range_writeback(inode, cur, cur + iosize - 1);
1424 if (!PageWriteback(page)) {
1425 btrfs_err(inode->root->fs_info,
1426 "page %lu not writeback, cur %llu end %llu",
1427 page->index, cur, end);
1431 * Although the PageDirty bit is cleared before entering this
1432 * function, subpage dirty bit is not cleared.
1433 * So clear subpage dirty bit here so next time we won't submit
1434 * page for range already written to disk.
1436 btrfs_folio_clear_dirty(fs_info, page_folio(page), cur, iosize);
1438 submit_extent_page(bio_ctrl, disk_bytenr, page, iosize,
1439 cur - page_offset(page));
1444 btrfs_folio_assert_not_dirty(fs_info, page_folio(page));
1450 * If we finish without problem, we should not only clear page dirty,
1451 * but also empty subpage dirty bits
1458 * the writepage semantics are similar to regular writepage. extent
1459 * records are inserted to lock ranges in the tree, and as dirty areas
1460 * are found, they are marked writeback. Then the lock bits are removed
1461 * and the end_io handler clears the writeback ranges
1463 * Return 0 if everything goes well.
1464 * Return <0 for error.
1466 static int __extent_writepage(struct page *page, struct btrfs_bio_ctrl *bio_ctrl)
1468 struct folio *folio = page_folio(page);
1469 struct inode *inode = page->mapping->host;
1470 const u64 page_start = page_offset(page);
1474 loff_t i_size = i_size_read(inode);
1475 unsigned long end_index = i_size >> PAGE_SHIFT;
1477 trace___extent_writepage(page, inode, bio_ctrl->wbc);
1479 WARN_ON(!PageLocked(page));
1481 pg_offset = offset_in_page(i_size);
1482 if (page->index > end_index ||
1483 (page->index == end_index && !pg_offset)) {
1484 folio_invalidate(folio, 0, folio_size(folio));
1485 folio_unlock(folio);
1489 if (page->index == end_index)
1490 memzero_page(page, pg_offset, PAGE_SIZE - pg_offset);
1492 ret = set_page_extent_mapped(page);
1496 ret = writepage_delalloc(BTRFS_I(inode), page, bio_ctrl->wbc);
1502 ret = __extent_writepage_io(BTRFS_I(inode), page, bio_ctrl, i_size, &nr);
1506 bio_ctrl->wbc->nr_to_write--;
1510 /* make sure the mapping tag for page dirty gets cleared */
1511 set_page_writeback(page);
1512 end_page_writeback(page);
1515 btrfs_mark_ordered_io_finished(BTRFS_I(inode), page, page_start,
1517 mapping_set_error(page->mapping, ret);
1524 void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
1526 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
1527 TASK_UNINTERRUPTIBLE);
1531 * Lock extent buffer status and pages for writeback.
1533 * Return %false if the extent buffer doesn't need to be submitted (e.g. the
1534 * extent buffer is not dirty)
1535 * Return %true is the extent buffer is submitted to bio.
1537 static noinline_for_stack bool lock_extent_buffer_for_io(struct extent_buffer *eb,
1538 struct writeback_control *wbc)
1540 struct btrfs_fs_info *fs_info = eb->fs_info;
1543 btrfs_tree_lock(eb);
1544 while (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
1545 btrfs_tree_unlock(eb);
1546 if (wbc->sync_mode != WB_SYNC_ALL)
1548 wait_on_extent_buffer_writeback(eb);
1549 btrfs_tree_lock(eb);
1553 * We need to do this to prevent races in people who check if the eb is
1554 * under IO since we can end up having no IO bits set for a short period
1557 spin_lock(&eb->refs_lock);
1558 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
1559 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
1560 spin_unlock(&eb->refs_lock);
1561 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
1562 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
1564 fs_info->dirty_metadata_batch);
1567 spin_unlock(&eb->refs_lock);
1569 btrfs_tree_unlock(eb);
1573 static void set_btree_ioerr(struct extent_buffer *eb)
1575 struct btrfs_fs_info *fs_info = eb->fs_info;
1577 set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
1580 * A read may stumble upon this buffer later, make sure that it gets an
1581 * error and knows there was an error.
1583 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
1586 * We need to set the mapping with the io error as well because a write
1587 * error will flip the file system readonly, and then syncfs() will
1588 * return a 0 because we are readonly if we don't modify the err seq for
1591 mapping_set_error(eb->fs_info->btree_inode->i_mapping, -EIO);
1594 * If writeback for a btree extent that doesn't belong to a log tree
1595 * failed, increment the counter transaction->eb_write_errors.
1596 * We do this because while the transaction is running and before it's
1597 * committing (when we call filemap_fdata[write|wait]_range against
1598 * the btree inode), we might have
1599 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
1600 * returns an error or an error happens during writeback, when we're
1601 * committing the transaction we wouldn't know about it, since the pages
1602 * can be no longer dirty nor marked anymore for writeback (if a
1603 * subsequent modification to the extent buffer didn't happen before the
1604 * transaction commit), which makes filemap_fdata[write|wait]_range not
1605 * able to find the pages which contain errors at transaction
1606 * commit time. So if this happens we must abort the transaction,
1607 * otherwise we commit a super block with btree roots that point to
1608 * btree nodes/leafs whose content on disk is invalid - either garbage
1609 * or the content of some node/leaf from a past generation that got
1610 * cowed or deleted and is no longer valid.
1612 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
1613 * not be enough - we need to distinguish between log tree extents vs
1614 * non-log tree extents, and the next filemap_fdatawait_range() call
1615 * will catch and clear such errors in the mapping - and that call might
1616 * be from a log sync and not from a transaction commit. Also, checking
1617 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
1618 * not done and would not be reliable - the eb might have been released
1619 * from memory and reading it back again means that flag would not be
1620 * set (since it's a runtime flag, not persisted on disk).
1622 * Using the flags below in the btree inode also makes us achieve the
1623 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
1624 * writeback for all dirty pages and before filemap_fdatawait_range()
1625 * is called, the writeback for all dirty pages had already finished
1626 * with errors - because we were not using AS_EIO/AS_ENOSPC,
1627 * filemap_fdatawait_range() would return success, as it could not know
1628 * that writeback errors happened (the pages were no longer tagged for
1631 switch (eb->log_index) {
1633 set_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags);
1636 set_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
1639 set_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
1642 BUG(); /* unexpected, logic error */
1647 * The endio specific version which won't touch any unsafe spinlock in endio
1650 static struct extent_buffer *find_extent_buffer_nolock(
1651 struct btrfs_fs_info *fs_info, u64 start)
1653 struct extent_buffer *eb;
1656 eb = radix_tree_lookup(&fs_info->buffer_radix,
1657 start >> fs_info->sectorsize_bits);
1658 if (eb && atomic_inc_not_zero(&eb->refs)) {
1666 static void end_bbio_meta_write(struct btrfs_bio *bbio)
1668 struct extent_buffer *eb = bbio->private;
1669 struct btrfs_fs_info *fs_info = eb->fs_info;
1670 bool uptodate = !bbio->bio.bi_status;
1671 struct folio_iter fi;
1675 set_btree_ioerr(eb);
1677 bio_for_each_folio_all(fi, &bbio->bio) {
1678 u64 start = eb->start + bio_offset;
1679 struct folio *folio = fi.folio;
1680 u32 len = fi.length;
1682 btrfs_folio_clear_writeback(fs_info, folio, start, len);
1686 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
1687 smp_mb__after_atomic();
1688 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
1690 bio_put(&bbio->bio);
1693 static void prepare_eb_write(struct extent_buffer *eb)
1696 unsigned long start;
1699 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
1701 /* Set btree blocks beyond nritems with 0 to avoid stale content */
1702 nritems = btrfs_header_nritems(eb);
1703 if (btrfs_header_level(eb) > 0) {
1704 end = btrfs_node_key_ptr_offset(eb, nritems);
1705 memzero_extent_buffer(eb, end, eb->len - end);
1709 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
1711 start = btrfs_item_nr_offset(eb, nritems);
1712 end = btrfs_item_nr_offset(eb, 0);
1714 end += BTRFS_LEAF_DATA_SIZE(eb->fs_info);
1716 end += btrfs_item_offset(eb, nritems - 1);
1717 memzero_extent_buffer(eb, start, end - start);
1721 static noinline_for_stack void write_one_eb(struct extent_buffer *eb,
1722 struct writeback_control *wbc)
1724 struct btrfs_fs_info *fs_info = eb->fs_info;
1725 struct btrfs_bio *bbio;
1727 prepare_eb_write(eb);
1729 bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES,
1730 REQ_OP_WRITE | REQ_META | wbc_to_write_flags(wbc),
1731 eb->fs_info, end_bbio_meta_write, eb);
1732 bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;
1733 bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev);
1734 wbc_init_bio(wbc, &bbio->bio);
1735 bbio->inode = BTRFS_I(eb->fs_info->btree_inode);
1736 bbio->file_offset = eb->start;
1737 if (fs_info->nodesize < PAGE_SIZE) {
1738 struct folio *folio = eb->folios[0];
1742 btrfs_subpage_set_writeback(fs_info, folio, eb->start, eb->len);
1743 if (btrfs_subpage_clear_and_test_dirty(fs_info, folio, eb->start,
1745 folio_clear_dirty_for_io(folio);
1748 ret = bio_add_folio(&bbio->bio, folio, eb->len,
1749 eb->start - folio_pos(folio));
1751 wbc_account_cgroup_owner(wbc, folio_page(folio, 0), eb->len);
1752 folio_unlock(folio);
1754 int num_folios = num_extent_folios(eb);
1756 for (int i = 0; i < num_folios; i++) {
1757 struct folio *folio = eb->folios[i];
1761 folio_clear_dirty_for_io(folio);
1762 folio_start_writeback(folio);
1763 ret = bio_add_folio(&bbio->bio, folio, eb->folio_size, 0);
1765 wbc_account_cgroup_owner(wbc, folio_page(folio, 0),
1767 wbc->nr_to_write -= folio_nr_pages(folio);
1768 folio_unlock(folio);
1771 btrfs_submit_bio(bbio, 0);
1775 * Submit one subpage btree page.
1777 * The main difference to submit_eb_page() is:
1779 * For subpage, we don't rely on page locking at all.
1782 * We only flush bio if we may be unable to fit current extent buffers into
1785 * Return >=0 for the number of submitted extent buffers.
1786 * Return <0 for fatal error.
1788 static int submit_eb_subpage(struct page *page, struct writeback_control *wbc)
1790 struct btrfs_fs_info *fs_info = page_to_fs_info(page);
1791 struct folio *folio = page_folio(page);
1793 u64 page_start = page_offset(page);
1795 int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits;
1797 /* Lock and write each dirty extent buffers in the range */
1798 while (bit_start < fs_info->subpage_info->bitmap_nr_bits) {
1799 struct btrfs_subpage *subpage = folio_get_private(folio);
1800 struct extent_buffer *eb;
1801 unsigned long flags;
1805 * Take private lock to ensure the subpage won't be detached
1808 spin_lock(&page->mapping->i_private_lock);
1809 if (!folio_test_private(folio)) {
1810 spin_unlock(&page->mapping->i_private_lock);
1813 spin_lock_irqsave(&subpage->lock, flags);
1814 if (!test_bit(bit_start + fs_info->subpage_info->dirty_offset,
1815 subpage->bitmaps)) {
1816 spin_unlock_irqrestore(&subpage->lock, flags);
1817 spin_unlock(&page->mapping->i_private_lock);
1822 start = page_start + bit_start * fs_info->sectorsize;
1823 bit_start += sectors_per_node;
1826 * Here we just want to grab the eb without touching extra
1827 * spin locks, so call find_extent_buffer_nolock().
1829 eb = find_extent_buffer_nolock(fs_info, start);
1830 spin_unlock_irqrestore(&subpage->lock, flags);
1831 spin_unlock(&page->mapping->i_private_lock);
1834 * The eb has already reached 0 refs thus find_extent_buffer()
1835 * doesn't return it. We don't need to write back such eb
1841 if (lock_extent_buffer_for_io(eb, wbc)) {
1842 write_one_eb(eb, wbc);
1845 free_extent_buffer(eb);
1851 * Submit all page(s) of one extent buffer.
1853 * @page: the page of one extent buffer
1854 * @eb_context: to determine if we need to submit this page, if current page
1855 * belongs to this eb, we don't need to submit
1857 * The caller should pass each page in their bytenr order, and here we use
1858 * @eb_context to determine if we have submitted pages of one extent buffer.
1860 * If we have, we just skip until we hit a new page that doesn't belong to
1861 * current @eb_context.
1863 * If not, we submit all the page(s) of the extent buffer.
1865 * Return >0 if we have submitted the extent buffer successfully.
1866 * Return 0 if we don't need to submit the page, as it's already submitted by
1868 * Return <0 for fatal error.
1870 static int submit_eb_page(struct page *page, struct btrfs_eb_write_context *ctx)
1872 struct writeback_control *wbc = ctx->wbc;
1873 struct address_space *mapping = page->mapping;
1874 struct folio *folio = page_folio(page);
1875 struct extent_buffer *eb;
1878 if (!folio_test_private(folio))
1881 if (page_to_fs_info(page)->nodesize < PAGE_SIZE)
1882 return submit_eb_subpage(page, wbc);
1884 spin_lock(&mapping->i_private_lock);
1885 if (!folio_test_private(folio)) {
1886 spin_unlock(&mapping->i_private_lock);
1890 eb = folio_get_private(folio);
1893 * Shouldn't happen and normally this would be a BUG_ON but no point
1894 * crashing the machine for something we can survive anyway.
1897 spin_unlock(&mapping->i_private_lock);
1901 if (eb == ctx->eb) {
1902 spin_unlock(&mapping->i_private_lock);
1905 ret = atomic_inc_not_zero(&eb->refs);
1906 spin_unlock(&mapping->i_private_lock);
1912 ret = btrfs_check_meta_write_pointer(eb->fs_info, ctx);
1916 free_extent_buffer(eb);
1920 if (!lock_extent_buffer_for_io(eb, wbc)) {
1921 free_extent_buffer(eb);
1924 /* Implies write in zoned mode. */
1925 if (ctx->zoned_bg) {
1926 /* Mark the last eb in the block group. */
1927 btrfs_schedule_zone_finish_bg(ctx->zoned_bg, eb);
1928 ctx->zoned_bg->meta_write_pointer += eb->len;
1930 write_one_eb(eb, wbc);
1931 free_extent_buffer(eb);
1935 int btree_write_cache_pages(struct address_space *mapping,
1936 struct writeback_control *wbc)
1938 struct btrfs_eb_write_context ctx = { .wbc = wbc };
1939 struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host);
1942 int nr_to_write_done = 0;
1943 struct folio_batch fbatch;
1944 unsigned int nr_folios;
1946 pgoff_t end; /* Inclusive */
1950 folio_batch_init(&fbatch);
1951 if (wbc->range_cyclic) {
1952 index = mapping->writeback_index; /* Start from prev offset */
1955 * Start from the beginning does not need to cycle over the
1956 * range, mark it as scanned.
1958 scanned = (index == 0);
1960 index = wbc->range_start >> PAGE_SHIFT;
1961 end = wbc->range_end >> PAGE_SHIFT;
1964 if (wbc->sync_mode == WB_SYNC_ALL)
1965 tag = PAGECACHE_TAG_TOWRITE;
1967 tag = PAGECACHE_TAG_DIRTY;
1968 btrfs_zoned_meta_io_lock(fs_info);
1970 if (wbc->sync_mode == WB_SYNC_ALL)
1971 tag_pages_for_writeback(mapping, index, end);
1972 while (!done && !nr_to_write_done && (index <= end) &&
1973 (nr_folios = filemap_get_folios_tag(mapping, &index, end,
1977 for (i = 0; i < nr_folios; i++) {
1978 struct folio *folio = fbatch.folios[i];
1980 ret = submit_eb_page(&folio->page, &ctx);
1989 * the filesystem may choose to bump up nr_to_write.
1990 * We have to make sure to honor the new nr_to_write
1993 nr_to_write_done = wbc->nr_to_write <= 0;
1995 folio_batch_release(&fbatch);
1998 if (!scanned && !done) {
2000 * We hit the last page and there is more work to be done: wrap
2001 * back to the start of the file
2008 * If something went wrong, don't allow any metadata write bio to be
2011 * This would prevent use-after-free if we had dirty pages not
2012 * cleaned up, which can still happen by fuzzed images.
2015 * Allowing existing tree block to be allocated for other trees.
2017 * - Log tree operations
2018 * Exiting tree blocks get allocated to log tree, bumps its
2019 * generation, then get cleaned in tree re-balance.
2020 * Such tree block will not be written back, since it's clean,
2021 * thus no WRITTEN flag set.
2022 * And after log writes back, this tree block is not traced by
2023 * any dirty extent_io_tree.
2025 * - Offending tree block gets re-dirtied from its original owner
2026 * Since it has bumped generation, no WRITTEN flag, it can be
2027 * reused without COWing. This tree block will not be traced
2028 * by btrfs_transaction::dirty_pages.
2030 * Now such dirty tree block will not be cleaned by any dirty
2031 * extent io tree. Thus we don't want to submit such wild eb
2032 * if the fs already has error.
2034 * We can get ret > 0 from submit_extent_page() indicating how many ebs
2035 * were submitted. Reset it to 0 to avoid false alerts for the caller.
2039 if (!ret && BTRFS_FS_ERROR(fs_info))
2043 btrfs_put_block_group(ctx.zoned_bg);
2044 btrfs_zoned_meta_io_unlock(fs_info);
2049 * Walk the list of dirty pages of the given address space and write all of them.
2051 * @mapping: address space structure to write
2052 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2053 * @bio_ctrl: holds context for the write, namely the bio
2055 * If a page is already under I/O, write_cache_pages() skips it, even
2056 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2057 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2058 * and msync() need to guarantee that all the data which was dirty at the time
2059 * the call was made get new I/O started against them. If wbc->sync_mode is
2060 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2061 * existing IO to complete.
2063 static int extent_write_cache_pages(struct address_space *mapping,
2064 struct btrfs_bio_ctrl *bio_ctrl)
2066 struct writeback_control *wbc = bio_ctrl->wbc;
2067 struct inode *inode = mapping->host;
2070 int nr_to_write_done = 0;
2071 struct folio_batch fbatch;
2072 unsigned int nr_folios;
2074 pgoff_t end; /* Inclusive */
2076 int range_whole = 0;
2081 * We have to hold onto the inode so that ordered extents can do their
2082 * work when the IO finishes. The alternative to this is failing to add
2083 * an ordered extent if the igrab() fails there and that is a huge pain
2084 * to deal with, so instead just hold onto the inode throughout the
2085 * writepages operation. If it fails here we are freeing up the inode
2086 * anyway and we'd rather not waste our time writing out stuff that is
2087 * going to be truncated anyway.
2092 folio_batch_init(&fbatch);
2093 if (wbc->range_cyclic) {
2094 index = mapping->writeback_index; /* Start from prev offset */
2097 * Start from the beginning does not need to cycle over the
2098 * range, mark it as scanned.
2100 scanned = (index == 0);
2102 index = wbc->range_start >> PAGE_SHIFT;
2103 end = wbc->range_end >> PAGE_SHIFT;
2104 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2110 * We do the tagged writepage as long as the snapshot flush bit is set
2111 * and we are the first one who do the filemap_flush() on this inode.
2113 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
2114 * not race in and drop the bit.
2116 if (range_whole && wbc->nr_to_write == LONG_MAX &&
2117 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
2118 &BTRFS_I(inode)->runtime_flags))
2119 wbc->tagged_writepages = 1;
2121 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2122 tag = PAGECACHE_TAG_TOWRITE;
2124 tag = PAGECACHE_TAG_DIRTY;
2126 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2127 tag_pages_for_writeback(mapping, index, end);
2129 while (!done && !nr_to_write_done && (index <= end) &&
2130 (nr_folios = filemap_get_folios_tag(mapping, &index,
2131 end, tag, &fbatch))) {
2134 for (i = 0; i < nr_folios; i++) {
2135 struct folio *folio = fbatch.folios[i];
2137 done_index = folio_next_index(folio);
2139 * At this point we hold neither the i_pages lock nor
2140 * the page lock: the page may be truncated or
2141 * invalidated (changing page->mapping to NULL),
2142 * or even swizzled back from swapper_space to
2143 * tmpfs file mapping
2145 if (!folio_trylock(folio)) {
2146 submit_write_bio(bio_ctrl, 0);
2150 if (unlikely(folio->mapping != mapping)) {
2151 folio_unlock(folio);
2155 if (!folio_test_dirty(folio)) {
2156 /* Someone wrote it for us. */
2157 folio_unlock(folio);
2161 if (wbc->sync_mode != WB_SYNC_NONE) {
2162 if (folio_test_writeback(folio))
2163 submit_write_bio(bio_ctrl, 0);
2164 folio_wait_writeback(folio);
2167 if (folio_test_writeback(folio) ||
2168 !folio_clear_dirty_for_io(folio)) {
2169 folio_unlock(folio);
2173 ret = __extent_writepage(&folio->page, bio_ctrl);
2180 * The filesystem may choose to bump up nr_to_write.
2181 * We have to make sure to honor the new nr_to_write
2184 nr_to_write_done = (wbc->sync_mode == WB_SYNC_NONE &&
2185 wbc->nr_to_write <= 0);
2187 folio_batch_release(&fbatch);
2190 if (!scanned && !done) {
2192 * We hit the last page and there is more work to be done: wrap
2193 * back to the start of the file
2199 * If we're looping we could run into a page that is locked by a
2200 * writer and that writer could be waiting on writeback for a
2201 * page in our current bio, and thus deadlock, so flush the
2204 submit_write_bio(bio_ctrl, 0);
2208 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
2209 mapping->writeback_index = done_index;
2211 btrfs_add_delayed_iput(BTRFS_I(inode));
2216 * Submit the pages in the range to bio for call sites which delalloc range has
2217 * already been ran (aka, ordered extent inserted) and all pages are still
2220 void extent_write_locked_range(struct inode *inode, struct page *locked_page,
2221 u64 start, u64 end, struct writeback_control *wbc,
2224 bool found_error = false;
2226 struct address_space *mapping = inode->i_mapping;
2227 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
2228 const u32 sectorsize = fs_info->sectorsize;
2229 loff_t i_size = i_size_read(inode);
2231 struct btrfs_bio_ctrl bio_ctrl = {
2233 .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc),
2236 if (wbc->no_cgroup_owner)
2237 bio_ctrl.opf |= REQ_BTRFS_CGROUP_PUNT;
2239 ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(end + 1, sectorsize));
2241 while (cur <= end) {
2242 u64 cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);
2243 u32 cur_len = cur_end + 1 - cur;
2247 page = find_get_page(mapping, cur >> PAGE_SHIFT);
2248 ASSERT(PageLocked(page));
2249 if (pages_dirty && page != locked_page) {
2250 ASSERT(PageDirty(page));
2251 clear_page_dirty_for_io(page);
2254 ret = __extent_writepage_io(BTRFS_I(inode), page, &bio_ctrl,
2259 /* Make sure the mapping tag for page dirty gets cleared. */
2261 set_page_writeback(page);
2262 end_page_writeback(page);
2265 btrfs_mark_ordered_io_finished(BTRFS_I(inode), page,
2266 cur, cur_len, !ret);
2267 mapping_set_error(page->mapping, ret);
2269 btrfs_folio_unlock_writer(fs_info, page_folio(page), cur, cur_len);
2277 submit_write_bio(&bio_ctrl, found_error ? ret : 0);
2280 int btrfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
2282 struct inode *inode = mapping->host;
2284 struct btrfs_bio_ctrl bio_ctrl = {
2286 .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc),
2290 * Allow only a single thread to do the reloc work in zoned mode to
2291 * protect the write pointer updates.
2293 btrfs_zoned_data_reloc_lock(BTRFS_I(inode));
2294 ret = extent_write_cache_pages(mapping, &bio_ctrl);
2295 submit_write_bio(&bio_ctrl, ret);
2296 btrfs_zoned_data_reloc_unlock(BTRFS_I(inode));
2300 void btrfs_readahead(struct readahead_control *rac)
2302 struct btrfs_bio_ctrl bio_ctrl = { .opf = REQ_OP_READ | REQ_RAHEAD };
2303 struct page *pagepool[16];
2304 struct extent_map *em_cached = NULL;
2305 u64 prev_em_start = (u64)-1;
2308 while ((nr = readahead_page_batch(rac, pagepool))) {
2309 u64 contig_start = readahead_pos(rac);
2310 u64 contig_end = contig_start + readahead_batch_length(rac) - 1;
2312 contiguous_readpages(pagepool, nr, contig_start, contig_end,
2313 &em_cached, &bio_ctrl, &prev_em_start);
2317 free_extent_map(em_cached);
2318 submit_one_bio(&bio_ctrl);
2322 * basic invalidate_folio code, this waits on any locked or writeback
2323 * ranges corresponding to the folio, and then deletes any extent state
2324 * records from the tree
2326 int extent_invalidate_folio(struct extent_io_tree *tree,
2327 struct folio *folio, size_t offset)
2329 struct extent_state *cached_state = NULL;
2330 u64 start = folio_pos(folio);
2331 u64 end = start + folio_size(folio) - 1;
2332 size_t blocksize = folio_to_fs_info(folio)->sectorsize;
2334 /* This function is only called for the btree inode */
2335 ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO);
2337 start += ALIGN(offset, blocksize);
2341 lock_extent(tree, start, end, &cached_state);
2342 folio_wait_writeback(folio);
2345 * Currently for btree io tree, only EXTENT_LOCKED is utilized,
2346 * so here we only need to unlock the extent range to free any
2347 * existing extent state.
2349 unlock_extent(tree, start, end, &cached_state);
2354 * a helper for release_folio, this tests for areas of the page that
2355 * are locked or under IO and drops the related state bits if it is safe
2358 static bool try_release_extent_state(struct extent_io_tree *tree,
2359 struct page *page, gfp_t mask)
2361 u64 start = page_offset(page);
2362 u64 end = start + PAGE_SIZE - 1;
2365 if (test_range_bit_exists(tree, start, end, EXTENT_LOCKED)) {
2368 u32 clear_bits = ~(EXTENT_LOCKED | EXTENT_NODATASUM |
2369 EXTENT_DELALLOC_NEW | EXTENT_CTLBITS |
2370 EXTENT_QGROUP_RESERVED);
2374 * At this point we can safely clear everything except the
2375 * locked bit, the nodatasum bit and the delalloc new bit.
2376 * The delalloc new bit will be cleared by ordered extent
2379 ret2 = __clear_extent_bit(tree, start, end, clear_bits, NULL, NULL);
2381 /* if clear_extent_bit failed for enomem reasons,
2382 * we can't allow the release to continue.
2393 * a helper for release_folio. As long as there are no locked extents
2394 * in the range corresponding to the page, both state records and extent
2395 * map records are removed
2397 bool try_release_extent_mapping(struct page *page, gfp_t mask)
2399 u64 start = page_offset(page);
2400 u64 end = start + PAGE_SIZE - 1;
2401 struct btrfs_inode *inode = page_to_inode(page);
2402 struct extent_io_tree *io_tree = &inode->io_tree;
2404 while (start <= end) {
2405 const u64 cur_gen = btrfs_get_fs_generation(inode->root->fs_info);
2406 const u64 len = end - start + 1;
2407 struct extent_map_tree *extent_tree = &inode->extent_tree;
2408 struct extent_map *em;
2410 write_lock(&extent_tree->lock);
2411 em = lookup_extent_mapping(extent_tree, start, len);
2413 write_unlock(&extent_tree->lock);
2416 if ((em->flags & EXTENT_FLAG_PINNED) || em->start != start) {
2417 write_unlock(&extent_tree->lock);
2418 free_extent_map(em);
2421 if (test_range_bit_exists(io_tree, em->start,
2422 extent_map_end(em) - 1, EXTENT_LOCKED))
2425 * If it's not in the list of modified extents, used by a fast
2426 * fsync, we can remove it. If it's being logged we can safely
2427 * remove it since fsync took an extra reference on the em.
2429 if (list_empty(&em->list) || (em->flags & EXTENT_FLAG_LOGGING))
2432 * If it's in the list of modified extents, remove it only if
2433 * its generation is older then the current one, in which case
2434 * we don't need it for a fast fsync. Otherwise don't remove it,
2435 * we could be racing with an ongoing fast fsync that could miss
2438 if (em->generation >= cur_gen)
2442 * We only remove extent maps that are not in the list of
2443 * modified extents or that are in the list but with a
2444 * generation lower then the current generation, so there is no
2445 * need to set the full fsync flag on the inode (it hurts the
2446 * fsync performance for workloads with a data size that exceeds
2447 * or is close to the system's memory).
2449 remove_extent_mapping(inode, em);
2450 /* Once for the inode's extent map tree. */
2451 free_extent_map(em);
2453 start = extent_map_end(em);
2454 write_unlock(&extent_tree->lock);
2456 /* Once for us, for the lookup_extent_mapping() reference. */
2457 free_extent_map(em);
2459 if (need_resched()) {
2461 * If we need to resched but we can't block just exit
2462 * and leave any remaining extent maps.
2464 if (!gfpflags_allow_blocking(mask))
2470 return try_release_extent_state(io_tree, page, mask);
2473 struct btrfs_fiemap_entry {
2481 * Indicate the caller of emit_fiemap_extent() that it needs to unlock the file
2482 * range from the inode's io tree, unlock the subvolume tree search path, flush
2483 * the fiemap cache and relock the file range and research the subvolume tree.
2484 * The value here is something negative that can't be confused with a valid
2485 * errno value and different from 1 because that's also a return value from
2486 * fiemap_fill_next_extent() and also it's often used to mean some btree search
2487 * did not find a key, so make it some distinct negative value.
2489 #define BTRFS_FIEMAP_FLUSH_CACHE (-(MAX_ERRNO + 1))
2494 * - Cache the next entry to be emitted to the fiemap buffer, so that we can
2495 * merge extents that are contiguous and can be grouped as a single one;
2497 * - Store extents ready to be written to the fiemap buffer in an intermediary
2498 * buffer. This intermediary buffer is to ensure that in case the fiemap
2499 * buffer is memory mapped to the fiemap target file, we don't deadlock
2500 * during btrfs_page_mkwrite(). This is because during fiemap we are locking
2501 * an extent range in order to prevent races with delalloc flushing and
2502 * ordered extent completion, which is needed in order to reliably detect
2503 * delalloc in holes and prealloc extents. And this can lead to a deadlock
2504 * if the fiemap buffer is memory mapped to the file we are running fiemap
2505 * against (a silly, useless in practice scenario, but possible) because
2506 * btrfs_page_mkwrite() will try to lock the same extent range.
2508 struct fiemap_cache {
2509 /* An array of ready fiemap entries. */
2510 struct btrfs_fiemap_entry *entries;
2511 /* Number of entries in the entries array. */
2513 /* Index of the next entry in the entries array to write to. */
2516 * Once the entries array is full, this indicates what's the offset for
2517 * the next file extent item we must search for in the inode's subvolume
2518 * tree after unlocking the extent range in the inode's io tree and
2519 * releasing the search path.
2521 u64 next_search_offset;
2523 * This matches struct fiemap_extent_info::fi_mapped_extents, we use it
2524 * to count ourselves emitted extents and stop instead of relying on
2525 * fiemap_fill_next_extent() because we buffer ready fiemap entries at
2526 * the @entries array, and we want to stop as soon as we hit the max
2527 * amount of extents to map, not just to save time but also to make the
2528 * logic at extent_fiemap() simpler.
2530 unsigned int extents_mapped;
2531 /* Fields for the cached extent (unsubmitted, not ready, extent). */
2539 static int flush_fiemap_cache(struct fiemap_extent_info *fieinfo,
2540 struct fiemap_cache *cache)
2542 for (int i = 0; i < cache->entries_pos; i++) {
2543 struct btrfs_fiemap_entry *entry = &cache->entries[i];
2546 ret = fiemap_fill_next_extent(fieinfo, entry->offset,
2547 entry->phys, entry->len,
2550 * Ignore 1 (reached max entries) because we keep track of that
2551 * ourselves in emit_fiemap_extent().
2556 cache->entries_pos = 0;
2562 * Helper to submit fiemap extent.
2564 * Will try to merge current fiemap extent specified by @offset, @phys,
2565 * @len and @flags with cached one.
2566 * And only when we fails to merge, cached one will be submitted as
2569 * Return value is the same as fiemap_fill_next_extent().
2571 static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
2572 struct fiemap_cache *cache,
2573 u64 offset, u64 phys, u64 len, u32 flags)
2575 struct btrfs_fiemap_entry *entry;
2578 /* Set at the end of extent_fiemap(). */
2579 ASSERT((flags & FIEMAP_EXTENT_LAST) == 0);
2585 * When iterating the extents of the inode, at extent_fiemap(), we may
2586 * find an extent that starts at an offset behind the end offset of the
2587 * previous extent we processed. This happens if fiemap is called
2588 * without FIEMAP_FLAG_SYNC and there are ordered extents completing
2589 * after we had to unlock the file range, release the search path, emit
2590 * the fiemap extents stored in the buffer (cache->entries array) and
2591 * the lock the remainder of the range and re-search the btree.
2593 * For example we are in leaf X processing its last item, which is the
2594 * file extent item for file range [512K, 1M[, and after
2595 * btrfs_next_leaf() releases the path, there's an ordered extent that
2596 * completes for the file range [768K, 2M[, and that results in trimming
2597 * the file extent item so that it now corresponds to the file range
2598 * [512K, 768K[ and a new file extent item is inserted for the file
2599 * range [768K, 2M[, which may end up as the last item of leaf X or as
2600 * the first item of the next leaf - in either case btrfs_next_leaf()
2601 * will leave us with a path pointing to the new extent item, for the
2602 * file range [768K, 2M[, since that's the first key that follows the
2603 * last one we processed. So in order not to report overlapping extents
2604 * to user space, we trim the length of the previously cached extent and
2607 * Upon calling btrfs_next_leaf() we may also find an extent with an
2608 * offset smaller than or equals to cache->offset, and this happens
2609 * when we had a hole or prealloc extent with several delalloc ranges in
2610 * it, but after btrfs_next_leaf() released the path, delalloc was
2611 * flushed and the resulting ordered extents were completed, so we can
2612 * now have found a file extent item for an offset that is smaller than
2613 * or equals to what we have in cache->offset. We deal with this as
2616 cache_end = cache->offset + cache->len;
2617 if (cache_end > offset) {
2618 if (offset == cache->offset) {
2620 * We cached a dealloc range (found in the io tree) for
2621 * a hole or prealloc extent and we have now found a
2622 * file extent item for the same offset. What we have
2623 * now is more recent and up to date, so discard what
2624 * we had in the cache and use what we have just found.
2627 } else if (offset > cache->offset) {
2629 * The extent range we previously found ends after the
2630 * offset of the file extent item we found and that
2631 * offset falls somewhere in the middle of that previous
2632 * extent range. So adjust the range we previously found
2633 * to end at the offset of the file extent item we have
2634 * just found, since this extent is more up to date.
2635 * Emit that adjusted range and cache the file extent
2636 * item we have just found. This corresponds to the case
2637 * where a previously found file extent item was split
2638 * due to an ordered extent completing.
2640 cache->len = offset - cache->offset;
2643 const u64 range_end = offset + len;
2646 * The offset of the file extent item we have just found
2647 * is behind the cached offset. This means we were
2648 * processing a hole or prealloc extent for which we
2649 * have found delalloc ranges (in the io tree), so what
2650 * we have in the cache is the last delalloc range we
2651 * found while the file extent item we found can be
2652 * either for a whole delalloc range we previously
2653 * emmitted or only a part of that range.
2655 * We have two cases here:
2657 * 1) The file extent item's range ends at or behind the
2658 * cached extent's end. In this case just ignore the
2659 * current file extent item because we don't want to
2660 * overlap with previous ranges that may have been
2663 * 2) The file extent item starts behind the currently
2664 * cached extent but its end offset goes beyond the
2665 * end offset of the cached extent. We don't want to
2666 * overlap with a previous range that may have been
2667 * emmitted already, so we emit the currently cached
2668 * extent and then partially store the current file
2669 * extent item's range in the cache, for the subrange
2670 * going the cached extent's end to the end of the
2673 if (range_end <= cache_end)
2676 if (!(flags & (FIEMAP_EXTENT_ENCODED | FIEMAP_EXTENT_DELALLOC)))
2677 phys += cache_end - offset;
2680 len = range_end - cache_end;
2686 * Only merges fiemap extents if
2687 * 1) Their logical addresses are continuous
2689 * 2) Their physical addresses are continuous
2690 * So truly compressed (physical size smaller than logical size)
2691 * extents won't get merged with each other
2693 * 3) Share same flags
2695 if (cache->offset + cache->len == offset &&
2696 cache->phys + cache->len == phys &&
2697 cache->flags == flags) {
2703 /* Not mergeable, need to submit cached one */
2705 if (cache->entries_pos == cache->entries_size) {
2707 * We will need to research for the end offset of the last
2708 * stored extent and not from the current offset, because after
2709 * unlocking the range and releasing the path, if there's a hole
2710 * between that end offset and this current offset, a new extent
2711 * may have been inserted due to a new write, so we don't want
2714 entry = &cache->entries[cache->entries_size - 1];
2715 cache->next_search_offset = entry->offset + entry->len;
2716 cache->cached = false;
2718 return BTRFS_FIEMAP_FLUSH_CACHE;
2721 entry = &cache->entries[cache->entries_pos];
2722 entry->offset = cache->offset;
2723 entry->phys = cache->phys;
2724 entry->len = cache->len;
2725 entry->flags = cache->flags;
2726 cache->entries_pos++;
2727 cache->extents_mapped++;
2729 if (cache->extents_mapped == fieinfo->fi_extents_max) {
2730 cache->cached = false;
2734 cache->cached = true;
2735 cache->offset = offset;
2738 cache->flags = flags;
2744 * Emit last fiemap cache
2746 * The last fiemap cache may still be cached in the following case:
2748 * |<- Fiemap range ->|
2749 * |<------------ First extent ----------->|
2751 * In this case, the first extent range will be cached but not emitted.
2752 * So we must emit it before ending extent_fiemap().
2754 static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
2755 struct fiemap_cache *cache)
2762 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
2763 cache->len, cache->flags);
2764 cache->cached = false;
2770 static int fiemap_next_leaf_item(struct btrfs_inode *inode, struct btrfs_path *path)
2772 struct extent_buffer *clone = path->nodes[0];
2773 struct btrfs_key key;
2778 if (path->slots[0] < btrfs_header_nritems(path->nodes[0]))
2782 * Add a temporary extra ref to an already cloned extent buffer to
2783 * prevent btrfs_next_leaf() freeing it, we want to reuse it to avoid
2784 * the cost of allocating a new one.
2786 ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED, &clone->bflags));
2787 atomic_inc(&clone->refs);
2789 ret = btrfs_next_leaf(inode->root, path);
2794 * Don't bother with cloning if there are no more file extent items for
2797 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2798 if (key.objectid != btrfs_ino(inode) || key.type != BTRFS_EXTENT_DATA_KEY) {
2804 * Important to preserve the start field, for the optimizations when
2805 * checking if extents are shared (see extent_fiemap()).
2807 * We must set ->start before calling copy_extent_buffer_full(). If we
2808 * are on sub-pagesize blocksize, we use ->start to determine the offset
2809 * into the folio where our eb exists, and if we update ->start after
2810 * the fact then any subsequent reads of the eb may read from a
2811 * different offset in the folio than where we originally copied into.
2813 clone->start = path->nodes[0]->start;
2814 /* See the comment at fiemap_search_slot() about why we clone. */
2815 copy_extent_buffer_full(clone, path->nodes[0]);
2817 slot = path->slots[0];
2818 btrfs_release_path(path);
2819 path->nodes[0] = clone;
2820 path->slots[0] = slot;
2823 free_extent_buffer(clone);
2829 * Search for the first file extent item that starts at a given file offset or
2830 * the one that starts immediately before that offset.
2831 * Returns: 0 on success, < 0 on error, 1 if not found.
2833 static int fiemap_search_slot(struct btrfs_inode *inode, struct btrfs_path *path,
2836 const u64 ino = btrfs_ino(inode);
2837 struct btrfs_root *root = inode->root;
2838 struct extent_buffer *clone;
2839 struct btrfs_key key;
2844 key.type = BTRFS_EXTENT_DATA_KEY;
2845 key.offset = file_offset;
2847 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2851 if (ret > 0 && path->slots[0] > 0) {
2852 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
2853 if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY)
2857 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2858 ret = btrfs_next_leaf(root, path);
2862 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2863 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
2868 * We clone the leaf and use it during fiemap. This is because while
2869 * using the leaf we do expensive things like checking if an extent is
2870 * shared, which can take a long time. In order to prevent blocking
2871 * other tasks for too long, we use a clone of the leaf. We have locked
2872 * the file range in the inode's io tree, so we know none of our file
2873 * extent items can change. This way we avoid blocking other tasks that
2874 * want to insert items for other inodes in the same leaf or b+tree
2875 * rebalance operations (triggered for example when someone is trying
2876 * to push items into this leaf when trying to insert an item in a
2878 * We also need the private clone because holding a read lock on an
2879 * extent buffer of the subvolume's b+tree will make lockdep unhappy
2880 * when we check if extents are shared, as backref walking may need to
2881 * lock the same leaf we are processing.
2883 clone = btrfs_clone_extent_buffer(path->nodes[0]);
2887 slot = path->slots[0];
2888 btrfs_release_path(path);
2889 path->nodes[0] = clone;
2890 path->slots[0] = slot;
2896 * Process a range which is a hole or a prealloc extent in the inode's subvolume
2897 * btree. If @disk_bytenr is 0, we are dealing with a hole, otherwise a prealloc
2898 * extent. The end offset (@end) is inclusive.
2900 static int fiemap_process_hole(struct btrfs_inode *inode,
2901 struct fiemap_extent_info *fieinfo,
2902 struct fiemap_cache *cache,
2903 struct extent_state **delalloc_cached_state,
2904 struct btrfs_backref_share_check_ctx *backref_ctx,
2905 u64 disk_bytenr, u64 extent_offset,
2909 const u64 i_size = i_size_read(&inode->vfs_inode);
2910 u64 cur_offset = start;
2911 u64 last_delalloc_end = 0;
2912 u32 prealloc_flags = FIEMAP_EXTENT_UNWRITTEN;
2913 bool checked_extent_shared = false;
2917 * There can be no delalloc past i_size, so don't waste time looking for
2920 while (cur_offset < end && cur_offset < i_size) {
2924 u64 prealloc_len = 0;
2927 delalloc = btrfs_find_delalloc_in_range(inode, cur_offset, end,
2928 delalloc_cached_state,
2935 * If this is a prealloc extent we have to report every section
2936 * of it that has no delalloc.
2938 if (disk_bytenr != 0) {
2939 if (last_delalloc_end == 0) {
2940 prealloc_start = start;
2941 prealloc_len = delalloc_start - start;
2943 prealloc_start = last_delalloc_end + 1;
2944 prealloc_len = delalloc_start - prealloc_start;
2948 if (prealloc_len > 0) {
2949 if (!checked_extent_shared && fieinfo->fi_extents_max) {
2950 ret = btrfs_is_data_extent_shared(inode,
2957 prealloc_flags |= FIEMAP_EXTENT_SHARED;
2959 checked_extent_shared = true;
2961 ret = emit_fiemap_extent(fieinfo, cache, prealloc_start,
2962 disk_bytenr + extent_offset,
2963 prealloc_len, prealloc_flags);
2966 extent_offset += prealloc_len;
2969 ret = emit_fiemap_extent(fieinfo, cache, delalloc_start, 0,
2970 delalloc_end + 1 - delalloc_start,
2971 FIEMAP_EXTENT_DELALLOC |
2972 FIEMAP_EXTENT_UNKNOWN);
2976 last_delalloc_end = delalloc_end;
2977 cur_offset = delalloc_end + 1;
2978 extent_offset += cur_offset - delalloc_start;
2983 * Either we found no delalloc for the whole prealloc extent or we have
2984 * a prealloc extent that spans i_size or starts at or after i_size.
2986 if (disk_bytenr != 0 && last_delalloc_end < end) {
2990 if (last_delalloc_end == 0) {
2991 prealloc_start = start;
2992 prealloc_len = end + 1 - start;
2994 prealloc_start = last_delalloc_end + 1;
2995 prealloc_len = end + 1 - prealloc_start;
2998 if (!checked_extent_shared && fieinfo->fi_extents_max) {
2999 ret = btrfs_is_data_extent_shared(inode,
3006 prealloc_flags |= FIEMAP_EXTENT_SHARED;
3008 ret = emit_fiemap_extent(fieinfo, cache, prealloc_start,
3009 disk_bytenr + extent_offset,
3010 prealloc_len, prealloc_flags);
3018 static int fiemap_find_last_extent_offset(struct btrfs_inode *inode,
3019 struct btrfs_path *path,
3020 u64 *last_extent_end_ret)
3022 const u64 ino = btrfs_ino(inode);
3023 struct btrfs_root *root = inode->root;
3024 struct extent_buffer *leaf;
3025 struct btrfs_file_extent_item *ei;
3026 struct btrfs_key key;
3031 * Lookup the last file extent. We're not using i_size here because
3032 * there might be preallocation past i_size.
3034 ret = btrfs_lookup_file_extent(NULL, root, path, ino, (u64)-1, 0);
3035 /* There can't be a file extent item at offset (u64)-1 */
3041 * For a non-existing key, btrfs_search_slot() always leaves us at a
3042 * slot > 0, except if the btree is empty, which is impossible because
3043 * at least it has the inode item for this inode and all the items for
3044 * the root inode 256.
3046 ASSERT(path->slots[0] > 0);
3048 leaf = path->nodes[0];
3049 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3050 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
3051 /* No file extent items in the subvolume tree. */
3052 *last_extent_end_ret = 0;
3057 * For an inline extent, the disk_bytenr is where inline data starts at,
3058 * so first check if we have an inline extent item before checking if we
3059 * have an implicit hole (disk_bytenr == 0).
3061 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
3062 if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_INLINE) {
3063 *last_extent_end_ret = btrfs_file_extent_end(path);
3068 * Find the last file extent item that is not a hole (when NO_HOLES is
3069 * not enabled). This should take at most 2 iterations in the worst
3070 * case: we have one hole file extent item at slot 0 of a leaf and
3071 * another hole file extent item as the last item in the previous leaf.
3072 * This is because we merge file extent items that represent holes.
3074 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
3075 while (disk_bytenr == 0) {
3076 ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
3079 } else if (ret > 0) {
3080 /* No file extent items that are not holes. */
3081 *last_extent_end_ret = 0;
3084 leaf = path->nodes[0];
3085 ei = btrfs_item_ptr(leaf, path->slots[0],
3086 struct btrfs_file_extent_item);
3087 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
3090 *last_extent_end_ret = btrfs_file_extent_end(path);
3094 int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo,
3097 const u64 ino = btrfs_ino(inode);
3098 struct extent_state *cached_state = NULL;
3099 struct extent_state *delalloc_cached_state = NULL;
3100 struct btrfs_path *path;
3101 struct fiemap_cache cache = { 0 };
3102 struct btrfs_backref_share_check_ctx *backref_ctx;
3103 u64 last_extent_end;
3104 u64 prev_extent_end;
3107 const u64 sectorsize = inode->root->fs_info->sectorsize;
3108 bool stopped = false;
3111 cache.entries_size = PAGE_SIZE / sizeof(struct btrfs_fiemap_entry);
3112 cache.entries = kmalloc_array(cache.entries_size,
3113 sizeof(struct btrfs_fiemap_entry),
3115 backref_ctx = btrfs_alloc_backref_share_check_ctx();
3116 path = btrfs_alloc_path();
3117 if (!cache.entries || !backref_ctx || !path) {
3123 range_start = round_down(start, sectorsize);
3124 range_end = round_up(start + len, sectorsize);
3125 prev_extent_end = range_start;
3127 lock_extent(&inode->io_tree, range_start, range_end, &cached_state);
3129 ret = fiemap_find_last_extent_offset(inode, path, &last_extent_end);
3132 btrfs_release_path(path);
3134 path->reada = READA_FORWARD;
3135 ret = fiemap_search_slot(inode, path, range_start);
3138 } else if (ret > 0) {
3140 * No file extent item found, but we may have delalloc between
3141 * the current offset and i_size. So check for that.
3144 goto check_eof_delalloc;
3147 while (prev_extent_end < range_end) {
3148 struct extent_buffer *leaf = path->nodes[0];
3149 struct btrfs_file_extent_item *ei;
3150 struct btrfs_key key;
3153 u64 extent_offset = 0;
3155 u64 disk_bytenr = 0;
3160 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3161 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
3164 extent_end = btrfs_file_extent_end(path);
3167 * The first iteration can leave us at an extent item that ends
3168 * before our range's start. Move to the next item.
3170 if (extent_end <= range_start)
3173 backref_ctx->curr_leaf_bytenr = leaf->start;
3175 /* We have in implicit hole (NO_HOLES feature enabled). */
3176 if (prev_extent_end < key.offset) {
3177 const u64 hole_end = min(key.offset, range_end) - 1;
3179 ret = fiemap_process_hole(inode, fieinfo, &cache,
3180 &delalloc_cached_state,
3181 backref_ctx, 0, 0, 0,
3182 prev_extent_end, hole_end);
3185 } else if (ret > 0) {
3186 /* fiemap_fill_next_extent() told us to stop. */
3191 /* We've reached the end of the fiemap range, stop. */
3192 if (key.offset >= range_end) {
3198 extent_len = extent_end - key.offset;
3199 ei = btrfs_item_ptr(leaf, path->slots[0],
3200 struct btrfs_file_extent_item);
3201 compression = btrfs_file_extent_compression(leaf, ei);
3202 extent_type = btrfs_file_extent_type(leaf, ei);
3203 extent_gen = btrfs_file_extent_generation(leaf, ei);
3205 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3206 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
3207 if (compression == BTRFS_COMPRESS_NONE)
3208 extent_offset = btrfs_file_extent_offset(leaf, ei);
3211 if (compression != BTRFS_COMPRESS_NONE)
3212 flags |= FIEMAP_EXTENT_ENCODED;
3214 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3215 flags |= FIEMAP_EXTENT_DATA_INLINE;
3216 flags |= FIEMAP_EXTENT_NOT_ALIGNED;
3217 ret = emit_fiemap_extent(fieinfo, &cache, key.offset, 0,
3219 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
3220 ret = fiemap_process_hole(inode, fieinfo, &cache,
3221 &delalloc_cached_state,
3223 disk_bytenr, extent_offset,
3224 extent_gen, key.offset,
3226 } else if (disk_bytenr == 0) {
3227 /* We have an explicit hole. */
3228 ret = fiemap_process_hole(inode, fieinfo, &cache,
3229 &delalloc_cached_state,
3230 backref_ctx, 0, 0, 0,
3231 key.offset, extent_end - 1);
3233 /* We have a regular extent. */
3234 if (fieinfo->fi_extents_max) {
3235 ret = btrfs_is_data_extent_shared(inode,
3242 flags |= FIEMAP_EXTENT_SHARED;
3245 ret = emit_fiemap_extent(fieinfo, &cache, key.offset,
3246 disk_bytenr + extent_offset,
3252 } else if (ret > 0) {
3253 /* emit_fiemap_extent() told us to stop. */
3258 prev_extent_end = extent_end;
3260 if (fatal_signal_pending(current)) {
3265 ret = fiemap_next_leaf_item(inode, path);
3268 } else if (ret > 0) {
3269 /* No more file extent items for this inode. */
3276 if (!stopped && prev_extent_end < range_end) {
3277 ret = fiemap_process_hole(inode, fieinfo, &cache,
3278 &delalloc_cached_state, backref_ctx,
3279 0, 0, 0, prev_extent_end, range_end - 1);
3282 prev_extent_end = range_end;
3285 if (cache.cached && cache.offset + cache.len >= last_extent_end) {
3286 const u64 i_size = i_size_read(&inode->vfs_inode);
3288 if (prev_extent_end < i_size) {
3293 delalloc = btrfs_find_delalloc_in_range(inode,
3296 &delalloc_cached_state,
3300 cache.flags |= FIEMAP_EXTENT_LAST;
3302 cache.flags |= FIEMAP_EXTENT_LAST;
3307 unlock_extent(&inode->io_tree, range_start, range_end, &cached_state);
3309 if (ret == BTRFS_FIEMAP_FLUSH_CACHE) {
3310 btrfs_release_path(path);
3311 ret = flush_fiemap_cache(fieinfo, &cache);
3314 len -= cache.next_search_offset - start;
3315 start = cache.next_search_offset;
3317 } else if (ret < 0) {
3322 * Must free the path before emitting to the fiemap buffer because we
3323 * may have a non-cloned leaf and if the fiemap buffer is memory mapped
3324 * to a file, a write into it (through btrfs_page_mkwrite()) may trigger
3325 * waiting for an ordered extent that in order to complete needs to
3326 * modify that leaf, therefore leading to a deadlock.
3328 btrfs_free_path(path);
3331 ret = flush_fiemap_cache(fieinfo, &cache);
3335 ret = emit_last_fiemap_cache(fieinfo, &cache);
3337 free_extent_state(delalloc_cached_state);
3338 kfree(cache.entries);
3339 btrfs_free_backref_share_ctx(backref_ctx);
3340 btrfs_free_path(path);
3344 static void __free_extent_buffer(struct extent_buffer *eb)
3346 kmem_cache_free(extent_buffer_cache, eb);
3349 static int extent_buffer_under_io(const struct extent_buffer *eb)
3351 return (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
3352 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3355 static bool folio_range_has_eb(struct btrfs_fs_info *fs_info, struct folio *folio)
3357 struct btrfs_subpage *subpage;
3359 lockdep_assert_held(&folio->mapping->i_private_lock);
3361 if (folio_test_private(folio)) {
3362 subpage = folio_get_private(folio);
3363 if (atomic_read(&subpage->eb_refs))
3366 * Even there is no eb refs here, we may still have
3367 * end_page_read() call relying on page::private.
3369 if (atomic_read(&subpage->readers))
3375 static void detach_extent_buffer_folio(struct extent_buffer *eb, struct folio *folio)
3377 struct btrfs_fs_info *fs_info = eb->fs_info;
3378 const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
3381 * For mapped eb, we're going to change the folio private, which should
3382 * be done under the i_private_lock.
3385 spin_lock(&folio->mapping->i_private_lock);
3387 if (!folio_test_private(folio)) {
3389 spin_unlock(&folio->mapping->i_private_lock);
3393 if (fs_info->nodesize >= PAGE_SIZE) {
3395 * We do this since we'll remove the pages after we've
3396 * removed the eb from the radix tree, so we could race
3397 * and have this page now attached to the new eb. So
3398 * only clear folio if it's still connected to
3401 if (folio_test_private(folio) && folio_get_private(folio) == eb) {
3402 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3403 BUG_ON(folio_test_dirty(folio));
3404 BUG_ON(folio_test_writeback(folio));
3405 /* We need to make sure we haven't be attached to a new eb. */
3406 folio_detach_private(folio);
3409 spin_unlock(&folio->mapping->i_private_lock);
3414 * For subpage, we can have dummy eb with folio private attached. In
3415 * this case, we can directly detach the private as such folio is only
3416 * attached to one dummy eb, no sharing.
3419 btrfs_detach_subpage(fs_info, folio);
3423 btrfs_folio_dec_eb_refs(fs_info, folio);
3426 * We can only detach the folio private if there are no other ebs in the
3427 * page range and no unfinished IO.
3429 if (!folio_range_has_eb(fs_info, folio))
3430 btrfs_detach_subpage(fs_info, folio);
3432 spin_unlock(&folio->mapping->i_private_lock);
3435 /* Release all pages attached to the extent buffer */
3436 static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
3438 ASSERT(!extent_buffer_under_io(eb));
3440 for (int i = 0; i < INLINE_EXTENT_BUFFER_PAGES; i++) {
3441 struct folio *folio = eb->folios[i];
3446 detach_extent_buffer_folio(eb, folio);
3448 /* One for when we allocated the folio. */
3454 * Helper for releasing the extent buffer.
3456 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3458 btrfs_release_extent_buffer_pages(eb);
3459 btrfs_leak_debug_del_eb(eb);
3460 __free_extent_buffer(eb);
3463 static struct extent_buffer *
3464 __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
3467 struct extent_buffer *eb = NULL;
3469 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
3472 eb->fs_info = fs_info;
3473 init_rwsem(&eb->lock);
3475 btrfs_leak_debug_add_eb(eb);
3477 spin_lock_init(&eb->refs_lock);
3478 atomic_set(&eb->refs, 1);
3480 ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE);
3485 struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
3487 struct extent_buffer *new;
3488 int num_folios = num_extent_folios(src);
3491 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
3496 * Set UNMAPPED before calling btrfs_release_extent_buffer(), as
3497 * btrfs_release_extent_buffer() have different behavior for
3498 * UNMAPPED subpage extent buffer.
3500 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
3502 ret = alloc_eb_folio_array(new, 0);
3504 btrfs_release_extent_buffer(new);
3508 for (int i = 0; i < num_folios; i++) {
3509 struct folio *folio = new->folios[i];
3512 ret = attach_extent_buffer_folio(new, folio, NULL);
3514 btrfs_release_extent_buffer(new);
3517 WARN_ON(folio_test_dirty(folio));
3519 copy_extent_buffer_full(new, src);
3520 set_extent_buffer_uptodate(new);
3525 struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
3526 u64 start, unsigned long len)
3528 struct extent_buffer *eb;
3532 eb = __alloc_extent_buffer(fs_info, start, len);
3536 ret = alloc_eb_folio_array(eb, 0);
3540 num_folios = num_extent_folios(eb);
3541 for (int i = 0; i < num_folios; i++) {
3542 ret = attach_extent_buffer_folio(eb, eb->folios[i], NULL);
3547 set_extent_buffer_uptodate(eb);
3548 btrfs_set_header_nritems(eb, 0);
3549 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
3553 for (int i = 0; i < num_folios; i++) {
3554 if (eb->folios[i]) {
3555 detach_extent_buffer_folio(eb, eb->folios[i]);
3556 __folio_put(eb->folios[i]);
3559 __free_extent_buffer(eb);
3563 struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
3566 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
3569 static void check_buffer_tree_ref(struct extent_buffer *eb)
3573 * The TREE_REF bit is first set when the extent_buffer is added
3574 * to the radix tree. It is also reset, if unset, when a new reference
3575 * is created by find_extent_buffer.
3577 * It is only cleared in two cases: freeing the last non-tree
3578 * reference to the extent_buffer when its STALE bit is set or
3579 * calling release_folio when the tree reference is the only reference.
3581 * In both cases, care is taken to ensure that the extent_buffer's
3582 * pages are not under io. However, release_folio can be concurrently
3583 * called with creating new references, which is prone to race
3584 * conditions between the calls to check_buffer_tree_ref in those
3585 * codepaths and clearing TREE_REF in try_release_extent_buffer.
3587 * The actual lifetime of the extent_buffer in the radix tree is
3588 * adequately protected by the refcount, but the TREE_REF bit and
3589 * its corresponding reference are not. To protect against this
3590 * class of races, we call check_buffer_tree_ref from the codepaths
3591 * which trigger io. Note that once io is initiated, TREE_REF can no
3592 * longer be cleared, so that is the moment at which any such race is
3595 refs = atomic_read(&eb->refs);
3596 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
3599 spin_lock(&eb->refs_lock);
3600 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
3601 atomic_inc(&eb->refs);
3602 spin_unlock(&eb->refs_lock);
3605 static void mark_extent_buffer_accessed(struct extent_buffer *eb)
3607 int num_folios= num_extent_folios(eb);
3609 check_buffer_tree_ref(eb);
3611 for (int i = 0; i < num_folios; i++)
3612 folio_mark_accessed(eb->folios[i]);
3615 struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
3618 struct extent_buffer *eb;
3620 eb = find_extent_buffer_nolock(fs_info, start);
3624 * Lock our eb's refs_lock to avoid races with free_extent_buffer().
3625 * When we get our eb it might be flagged with EXTENT_BUFFER_STALE and
3626 * another task running free_extent_buffer() might have seen that flag
3627 * set, eb->refs == 2, that the buffer isn't under IO (dirty and
3628 * writeback flags not set) and it's still in the tree (flag
3629 * EXTENT_BUFFER_TREE_REF set), therefore being in the process of
3630 * decrementing the extent buffer's reference count twice. So here we
3631 * could race and increment the eb's reference count, clear its stale
3632 * flag, mark it as dirty and drop our reference before the other task
3633 * finishes executing free_extent_buffer, which would later result in
3634 * an attempt to free an extent buffer that is dirty.
3636 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
3637 spin_lock(&eb->refs_lock);
3638 spin_unlock(&eb->refs_lock);
3640 mark_extent_buffer_accessed(eb);
3644 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3645 struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
3648 struct extent_buffer *eb, *exists = NULL;
3651 eb = find_extent_buffer(fs_info, start);
3654 eb = alloc_dummy_extent_buffer(fs_info, start);
3656 return ERR_PTR(-ENOMEM);
3657 eb->fs_info = fs_info;
3659 ret = radix_tree_preload(GFP_NOFS);
3661 exists = ERR_PTR(ret);
3664 spin_lock(&fs_info->buffer_lock);
3665 ret = radix_tree_insert(&fs_info->buffer_radix,
3666 start >> fs_info->sectorsize_bits, eb);
3667 spin_unlock(&fs_info->buffer_lock);
3668 radix_tree_preload_end();
3669 if (ret == -EEXIST) {
3670 exists = find_extent_buffer(fs_info, start);
3676 check_buffer_tree_ref(eb);
3677 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
3681 btrfs_release_extent_buffer(eb);
3686 static struct extent_buffer *grab_extent_buffer(
3687 struct btrfs_fs_info *fs_info, struct page *page)
3689 struct folio *folio = page_folio(page);
3690 struct extent_buffer *exists;
3693 * For subpage case, we completely rely on radix tree to ensure we
3694 * don't try to insert two ebs for the same bytenr. So here we always
3695 * return NULL and just continue.
3697 if (fs_info->nodesize < PAGE_SIZE)
3700 /* Page not yet attached to an extent buffer */
3701 if (!folio_test_private(folio))
3705 * We could have already allocated an eb for this page and attached one
3706 * so lets see if we can get a ref on the existing eb, and if we can we
3707 * know it's good and we can just return that one, else we know we can
3708 * just overwrite folio private.
3710 exists = folio_get_private(folio);
3711 if (atomic_inc_not_zero(&exists->refs))
3714 WARN_ON(PageDirty(page));
3715 folio_detach_private(folio);
3719 static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start)
3721 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
3722 btrfs_err(fs_info, "bad tree block start %llu", start);
3726 if (fs_info->nodesize < PAGE_SIZE &&
3727 offset_in_page(start) + fs_info->nodesize > PAGE_SIZE) {
3729 "tree block crosses page boundary, start %llu nodesize %u",
3730 start, fs_info->nodesize);
3733 if (fs_info->nodesize >= PAGE_SIZE &&
3734 !PAGE_ALIGNED(start)) {
3736 "tree block is not page aligned, start %llu nodesize %u",
3737 start, fs_info->nodesize);
3740 if (!IS_ALIGNED(start, fs_info->nodesize) &&
3741 !test_and_set_bit(BTRFS_FS_UNALIGNED_TREE_BLOCK, &fs_info->flags)) {
3743 "tree block not nodesize aligned, start %llu nodesize %u, can be resolved by a full metadata balance",
3744 start, fs_info->nodesize);
3751 * Return 0 if eb->folios[i] is attached to btree inode successfully.
3752 * Return >0 if there is already another extent buffer for the range,
3753 * and @found_eb_ret would be updated.
3754 * Return -EAGAIN if the filemap has an existing folio but with different size
3756 * The caller needs to free the existing folios and retry using the same order.
3758 static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
3759 struct extent_buffer **found_eb_ret)
3762 struct btrfs_fs_info *fs_info = eb->fs_info;
3763 struct address_space *mapping = fs_info->btree_inode->i_mapping;
3764 const unsigned long index = eb->start >> PAGE_SHIFT;
3765 struct folio *existing_folio;
3768 ASSERT(found_eb_ret);
3770 /* Caller should ensure the folio exists. */
3771 ASSERT(eb->folios[i]);
3774 ret = filemap_add_folio(mapping, eb->folios[i], index + i,
3775 GFP_NOFS | __GFP_NOFAIL);
3779 existing_folio = filemap_lock_folio(mapping, index + i);
3780 /* The page cache only exists for a very short time, just retry. */
3781 if (IS_ERR(existing_folio))
3784 /* For now, we should only have single-page folios for btree inode. */
3785 ASSERT(folio_nr_pages(existing_folio) == 1);
3787 if (folio_size(existing_folio) != eb->folio_size) {
3788 folio_unlock(existing_folio);
3789 folio_put(existing_folio);
3793 if (fs_info->nodesize < PAGE_SIZE) {
3795 * We're going to reuse the existing page, can drop our page
3796 * and subpage structure now.
3798 __free_page(folio_page(eb->folios[i], 0));
3799 eb->folios[i] = existing_folio;
3801 struct extent_buffer *existing_eb;
3803 existing_eb = grab_extent_buffer(fs_info,
3804 folio_page(existing_folio, 0));
3806 /* The extent buffer still exists, we can use it directly. */
3807 *found_eb_ret = existing_eb;
3808 folio_unlock(existing_folio);
3809 folio_put(existing_folio);
3812 /* The extent buffer no longer exists, we can reuse the folio. */
3813 __free_page(folio_page(eb->folios[i], 0));
3814 eb->folios[i] = existing_folio;
3819 struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
3820 u64 start, u64 owner_root, int level)
3822 unsigned long len = fs_info->nodesize;
3825 struct extent_buffer *eb;
3826 struct extent_buffer *existing_eb = NULL;
3827 struct address_space *mapping = fs_info->btree_inode->i_mapping;
3828 struct btrfs_subpage *prealloc = NULL;
3829 u64 lockdep_owner = owner_root;
3830 bool page_contig = true;
3834 if (check_eb_alignment(fs_info, start))
3835 return ERR_PTR(-EINVAL);
3837 #if BITS_PER_LONG == 32
3838 if (start >= MAX_LFS_FILESIZE) {
3839 btrfs_err_rl(fs_info,
3840 "extent buffer %llu is beyond 32bit page cache limit", start);
3841 btrfs_err_32bit_limit(fs_info);
3842 return ERR_PTR(-EOVERFLOW);
3844 if (start >= BTRFS_32BIT_EARLY_WARN_THRESHOLD)
3845 btrfs_warn_32bit_limit(fs_info);
3848 eb = find_extent_buffer(fs_info, start);
3852 eb = __alloc_extent_buffer(fs_info, start, len);
3854 return ERR_PTR(-ENOMEM);
3857 * The reloc trees are just snapshots, so we need them to appear to be
3858 * just like any other fs tree WRT lockdep.
3860 if (lockdep_owner == BTRFS_TREE_RELOC_OBJECTID)
3861 lockdep_owner = BTRFS_FS_TREE_OBJECTID;
3863 btrfs_set_buffer_lockdep_class(lockdep_owner, eb, level);
3866 * Preallocate folio private for subpage case, so that we won't
3867 * allocate memory with i_private_lock nor page lock hold.
3869 * The memory will be freed by attach_extent_buffer_page() or freed
3870 * manually if we exit earlier.
3872 if (fs_info->nodesize < PAGE_SIZE) {
3873 prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA);
3874 if (IS_ERR(prealloc)) {
3875 ret = PTR_ERR(prealloc);
3881 /* Allocate all pages first. */
3882 ret = alloc_eb_folio_array(eb, __GFP_NOFAIL);
3884 btrfs_free_subpage(prealloc);
3888 num_folios = num_extent_folios(eb);
3889 /* Attach all pages to the filemap. */
3890 for (int i = 0; i < num_folios; i++) {
3891 struct folio *folio;
3893 ret = attach_eb_folio_to_filemap(eb, i, &existing_eb);
3895 ASSERT(existing_eb);
3900 * TODO: Special handling for a corner case where the order of
3901 * folios mismatch between the new eb and filemap.
3903 * This happens when:
3905 * - the new eb is using higher order folio
3907 * - the filemap is still using 0-order folios for the range
3908 * This can happen at the previous eb allocation, and we don't
3909 * have higher order folio for the call.
3911 * - the existing eb has already been freed
3913 * In this case, we have to free the existing folios first, and
3914 * re-allocate using the same order.
3915 * Thankfully this is not going to happen yet, as we're still
3916 * using 0-order folios.
3918 if (unlikely(ret == -EAGAIN)) {
3925 * Only after attach_eb_folio_to_filemap(), eb->folios[] is
3926 * reliable, as we may choose to reuse the existing page cache
3927 * and free the allocated page.
3929 folio = eb->folios[i];
3930 eb->folio_size = folio_size(folio);
3931 eb->folio_shift = folio_shift(folio);
3932 spin_lock(&mapping->i_private_lock);
3933 /* Should not fail, as we have preallocated the memory */
3934 ret = attach_extent_buffer_folio(eb, folio, prealloc);
3937 * To inform we have extra eb under allocation, so that
3938 * detach_extent_buffer_page() won't release the folio private
3939 * when the eb hasn't yet been inserted into radix tree.
3941 * The ref will be decreased when the eb released the page, in
3942 * detach_extent_buffer_page().
3943 * Thus needs no special handling in error path.
3945 btrfs_folio_inc_eb_refs(fs_info, folio);
3946 spin_unlock(&mapping->i_private_lock);
3948 WARN_ON(btrfs_folio_test_dirty(fs_info, folio, eb->start, eb->len));
3951 * Check if the current page is physically contiguous with previous eb
3953 * At this stage, either we allocated a large folio, thus @i
3954 * would only be 0, or we fall back to per-page allocation.
3956 if (i && folio_page(eb->folios[i - 1], 0) + 1 != folio_page(folio, 0))
3957 page_contig = false;
3959 if (!btrfs_folio_test_uptodate(fs_info, folio, eb->start, eb->len))
3963 * We can't unlock the pages just yet since the extent buffer
3964 * hasn't been properly inserted in the radix tree, this
3965 * opens a race with btree_release_folio which can free a page
3966 * while we are still filling in all pages for the buffer and
3971 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3972 /* All pages are physically contiguous, can skip cross page handling. */
3974 eb->addr = folio_address(eb->folios[0]) + offset_in_page(eb->start);
3976 ret = radix_tree_preload(GFP_NOFS);
3980 spin_lock(&fs_info->buffer_lock);
3981 ret = radix_tree_insert(&fs_info->buffer_radix,
3982 start >> fs_info->sectorsize_bits, eb);
3983 spin_unlock(&fs_info->buffer_lock);
3984 radix_tree_preload_end();
3985 if (ret == -EEXIST) {
3987 existing_eb = find_extent_buffer(fs_info, start);
3993 /* add one reference for the tree */
3994 check_buffer_tree_ref(eb);
3995 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
3998 * Now it's safe to unlock the pages because any calls to
3999 * btree_release_folio will correctly detect that a page belongs to a
4000 * live buffer and won't free them prematurely.
4002 for (int i = 0; i < num_folios; i++)
4003 unlock_page(folio_page(eb->folios[i], 0));
4007 WARN_ON(!atomic_dec_and_test(&eb->refs));
4010 * Any attached folios need to be detached before we unlock them. This
4011 * is because when we're inserting our new folios into the mapping, and
4012 * then attaching our eb to that folio. If we fail to insert our folio
4013 * we'll lookup the folio for that index, and grab that EB. We do not
4014 * want that to grab this eb, as we're getting ready to free it. So we
4015 * have to detach it first and then unlock it.
4017 * We have to drop our reference and NULL it out here because in the
4018 * subpage case detaching does a btrfs_folio_dec_eb_refs() for our eb.
4019 * Below when we call btrfs_release_extent_buffer() we will call
4020 * detach_extent_buffer_folio() on our remaining pages in the !subpage
4021 * case. If we left eb->folios[i] populated in the subpage case we'd
4022 * double put our reference and be super sad.
4024 for (int i = 0; i < attached; i++) {
4025 ASSERT(eb->folios[i]);
4026 detach_extent_buffer_folio(eb, eb->folios[i]);
4027 unlock_page(folio_page(eb->folios[i], 0));
4028 folio_put(eb->folios[i]);
4029 eb->folios[i] = NULL;
4032 * Now all pages of that extent buffer is unmapped, set UNMAPPED flag,
4033 * so it can be cleaned up without utlizing page->mapping.
4035 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
4037 btrfs_release_extent_buffer(eb);
4039 return ERR_PTR(ret);
4040 ASSERT(existing_eb);
4044 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4046 struct extent_buffer *eb =
4047 container_of(head, struct extent_buffer, rcu_head);
4049 __free_extent_buffer(eb);
4052 static int release_extent_buffer(struct extent_buffer *eb)
4053 __releases(&eb->refs_lock)
4055 lockdep_assert_held(&eb->refs_lock);
4057 WARN_ON(atomic_read(&eb->refs) == 0);
4058 if (atomic_dec_and_test(&eb->refs)) {
4059 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
4060 struct btrfs_fs_info *fs_info = eb->fs_info;
4062 spin_unlock(&eb->refs_lock);
4064 spin_lock(&fs_info->buffer_lock);
4065 radix_tree_delete(&fs_info->buffer_radix,
4066 eb->start >> fs_info->sectorsize_bits);
4067 spin_unlock(&fs_info->buffer_lock);
4069 spin_unlock(&eb->refs_lock);
4072 btrfs_leak_debug_del_eb(eb);
4073 /* Should be safe to release our pages at this point */
4074 btrfs_release_extent_buffer_pages(eb);
4075 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4076 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
4077 __free_extent_buffer(eb);
4081 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4084 spin_unlock(&eb->refs_lock);
4089 void free_extent_buffer(struct extent_buffer *eb)
4095 refs = atomic_read(&eb->refs);
4097 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
4098 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
4101 if (atomic_try_cmpxchg(&eb->refs, &refs, refs - 1))
4105 spin_lock(&eb->refs_lock);
4106 if (atomic_read(&eb->refs) == 2 &&
4107 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
4108 !extent_buffer_under_io(eb) &&
4109 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4110 atomic_dec(&eb->refs);
4113 * I know this is terrible, but it's temporary until we stop tracking
4114 * the uptodate bits and such for the extent buffers.
4116 release_extent_buffer(eb);
4119 void free_extent_buffer_stale(struct extent_buffer *eb)
4124 spin_lock(&eb->refs_lock);
4125 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4127 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
4128 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4129 atomic_dec(&eb->refs);
4130 release_extent_buffer(eb);
4133 static void btree_clear_folio_dirty(struct folio *folio)
4135 ASSERT(folio_test_dirty(folio));
4136 ASSERT(folio_test_locked(folio));
4137 folio_clear_dirty_for_io(folio);
4138 xa_lock_irq(&folio->mapping->i_pages);
4139 if (!folio_test_dirty(folio))
4140 __xa_clear_mark(&folio->mapping->i_pages,
4141 folio_index(folio), PAGECACHE_TAG_DIRTY);
4142 xa_unlock_irq(&folio->mapping->i_pages);
4145 static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb)
4147 struct btrfs_fs_info *fs_info = eb->fs_info;
4148 struct folio *folio = eb->folios[0];
4151 /* btree_clear_folio_dirty() needs page locked. */
4153 last = btrfs_subpage_clear_and_test_dirty(fs_info, folio, eb->start, eb->len);
4155 btree_clear_folio_dirty(folio);
4156 folio_unlock(folio);
4157 WARN_ON(atomic_read(&eb->refs) == 0);
4160 void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,
4161 struct extent_buffer *eb)
4163 struct btrfs_fs_info *fs_info = eb->fs_info;
4166 btrfs_assert_tree_write_locked(eb);
4168 if (trans && btrfs_header_generation(eb) != trans->transid)
4172 * Instead of clearing the dirty flag off of the buffer, mark it as
4173 * EXTENT_BUFFER_ZONED_ZEROOUT. This allows us to preserve
4174 * write-ordering in zoned mode, without the need to later re-dirty
4175 * the extent_buffer.
4177 * The actual zeroout of the buffer will happen later in
4178 * btree_csum_one_bio.
4180 if (btrfs_is_zoned(fs_info) && test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4181 set_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags);
4185 if (!test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags))
4188 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, -eb->len,
4189 fs_info->dirty_metadata_batch);
4191 if (eb->fs_info->nodesize < PAGE_SIZE)
4192 return clear_subpage_extent_buffer_dirty(eb);
4194 num_folios = num_extent_folios(eb);
4195 for (int i = 0; i < num_folios; i++) {
4196 struct folio *folio = eb->folios[i];
4198 if (!folio_test_dirty(folio))
4201 btree_clear_folio_dirty(folio);
4202 folio_unlock(folio);
4204 WARN_ON(atomic_read(&eb->refs) == 0);
4207 void set_extent_buffer_dirty(struct extent_buffer *eb)
4212 check_buffer_tree_ref(eb);
4214 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4216 num_folios = num_extent_folios(eb);
4217 WARN_ON(atomic_read(&eb->refs) == 0);
4218 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4219 WARN_ON(test_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags));
4222 bool subpage = eb->fs_info->nodesize < PAGE_SIZE;
4225 * For subpage case, we can have other extent buffers in the
4226 * same page, and in clear_subpage_extent_buffer_dirty() we
4227 * have to clear page dirty without subpage lock held.
4228 * This can cause race where our page gets dirty cleared after
4231 * Thankfully, clear_subpage_extent_buffer_dirty() has locked
4232 * its page for other reasons, we can use page lock to prevent
4236 lock_page(folio_page(eb->folios[0], 0));
4237 for (int i = 0; i < num_folios; i++)
4238 btrfs_folio_set_dirty(eb->fs_info, eb->folios[i],
4239 eb->start, eb->len);
4241 unlock_page(folio_page(eb->folios[0], 0));
4242 percpu_counter_add_batch(&eb->fs_info->dirty_metadata_bytes,
4244 eb->fs_info->dirty_metadata_batch);
4246 #ifdef CONFIG_BTRFS_DEBUG
4247 for (int i = 0; i < num_folios; i++)
4248 ASSERT(folio_test_dirty(eb->folios[i]));
4252 void clear_extent_buffer_uptodate(struct extent_buffer *eb)
4254 struct btrfs_fs_info *fs_info = eb->fs_info;
4255 int num_folios = num_extent_folios(eb);
4257 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4258 for (int i = 0; i < num_folios; i++) {
4259 struct folio *folio = eb->folios[i];
4265 * This is special handling for metadata subpage, as regular
4266 * btrfs_is_subpage() can not handle cloned/dummy metadata.
4268 if (fs_info->nodesize >= PAGE_SIZE)
4269 folio_clear_uptodate(folio);
4271 btrfs_subpage_clear_uptodate(fs_info, folio,
4272 eb->start, eb->len);
4276 void set_extent_buffer_uptodate(struct extent_buffer *eb)
4278 struct btrfs_fs_info *fs_info = eb->fs_info;
4279 int num_folios = num_extent_folios(eb);
4281 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4282 for (int i = 0; i < num_folios; i++) {
4283 struct folio *folio = eb->folios[i];
4286 * This is special handling for metadata subpage, as regular
4287 * btrfs_is_subpage() can not handle cloned/dummy metadata.
4289 if (fs_info->nodesize >= PAGE_SIZE)
4290 folio_mark_uptodate(folio);
4292 btrfs_subpage_set_uptodate(fs_info, folio,
4293 eb->start, eb->len);
4297 static void clear_extent_buffer_reading(struct extent_buffer *eb)
4299 clear_bit(EXTENT_BUFFER_READING, &eb->bflags);
4300 smp_mb__after_atomic();
4301 wake_up_bit(&eb->bflags, EXTENT_BUFFER_READING);
4304 static void end_bbio_meta_read(struct btrfs_bio *bbio)
4306 struct extent_buffer *eb = bbio->private;
4307 struct btrfs_fs_info *fs_info = eb->fs_info;
4308 bool uptodate = !bbio->bio.bi_status;
4309 struct folio_iter fi;
4313 * If the extent buffer is marked UPTODATE before the read operation
4314 * completes, other calls to read_extent_buffer_pages() will return
4315 * early without waiting for the read to finish, causing data races.
4317 WARN_ON(test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags));
4319 eb->read_mirror = bbio->mirror_num;
4322 btrfs_validate_extent_buffer(eb, &bbio->parent_check) < 0)
4326 set_extent_buffer_uptodate(eb);
4328 clear_extent_buffer_uptodate(eb);
4329 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
4332 bio_for_each_folio_all(fi, &bbio->bio) {
4333 struct folio *folio = fi.folio;
4334 u64 start = eb->start + bio_offset;
4335 u32 len = fi.length;
4338 btrfs_folio_set_uptodate(fs_info, folio, start, len);
4340 btrfs_folio_clear_uptodate(fs_info, folio, start, len);
4345 clear_extent_buffer_reading(eb);
4346 free_extent_buffer(eb);
4348 bio_put(&bbio->bio);
4351 int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num,
4352 struct btrfs_tree_parent_check *check)
4354 struct btrfs_bio *bbio;
4357 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4361 * We could have had EXTENT_BUFFER_UPTODATE cleared by the write
4362 * operation, which could potentially still be in flight. In this case
4363 * we simply want to return an error.
4365 if (unlikely(test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)))
4368 /* Someone else is already reading the buffer, just wait for it. */
4369 if (test_and_set_bit(EXTENT_BUFFER_READING, &eb->bflags))
4373 * Between the initial test_bit(EXTENT_BUFFER_UPTODATE) and the above
4374 * test_and_set_bit(EXTENT_BUFFER_READING), someone else could have
4375 * started and finished reading the same eb. In this case, UPTODATE
4376 * will now be set, and we shouldn't read it in again.
4378 if (unlikely(test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))) {
4379 clear_extent_buffer_reading(eb);
4383 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
4384 eb->read_mirror = 0;
4385 check_buffer_tree_ref(eb);
4386 atomic_inc(&eb->refs);
4388 bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES,
4389 REQ_OP_READ | REQ_META, eb->fs_info,
4390 end_bbio_meta_read, eb);
4391 bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;
4392 bbio->inode = BTRFS_I(eb->fs_info->btree_inode);
4393 bbio->file_offset = eb->start;
4394 memcpy(&bbio->parent_check, check, sizeof(*check));
4395 if (eb->fs_info->nodesize < PAGE_SIZE) {
4396 ret = bio_add_folio(&bbio->bio, eb->folios[0], eb->len,
4397 eb->start - folio_pos(eb->folios[0]));
4400 int num_folios = num_extent_folios(eb);
4402 for (int i = 0; i < num_folios; i++) {
4403 struct folio *folio = eb->folios[i];
4405 ret = bio_add_folio(&bbio->bio, folio, eb->folio_size, 0);
4409 btrfs_submit_bio(bbio, mirror_num);
4412 if (wait == WAIT_COMPLETE) {
4413 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_READING, TASK_UNINTERRUPTIBLE);
4414 if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4421 static bool report_eb_range(const struct extent_buffer *eb, unsigned long start,
4424 btrfs_warn(eb->fs_info,
4425 "access to eb bytenr %llu len %u out of range start %lu len %lu",
4426 eb->start, eb->len, start, len);
4427 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
4433 * Check if the [start, start + len) range is valid before reading/writing
4435 * NOTE: @start and @len are offset inside the eb, not logical address.
4437 * Caller should not touch the dst/src memory if this function returns error.
4439 static inline int check_eb_range(const struct extent_buffer *eb,
4440 unsigned long start, unsigned long len)
4442 unsigned long offset;
4444 /* start, start + len should not go beyond eb->len nor overflow */
4445 if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len))
4446 return report_eb_range(eb, start, len);
4451 void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
4452 unsigned long start, unsigned long len)
4454 const int unit_size = eb->folio_size;
4457 char *dst = (char *)dstv;
4458 unsigned long i = get_eb_folio_index(eb, start);
4460 if (check_eb_range(eb, start, len)) {
4462 * Invalid range hit, reset the memory, so callers won't get
4463 * some random garbage for their uninitialized memory.
4465 memset(dstv, 0, len);
4470 memcpy(dstv, eb->addr + start, len);
4474 offset = get_eb_offset_in_folio(eb, start);
4479 cur = min(len, unit_size - offset);
4480 kaddr = folio_address(eb->folios[i]);
4481 memcpy(dst, kaddr + offset, cur);
4490 int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
4492 unsigned long start, unsigned long len)
4494 const int unit_size = eb->folio_size;
4497 char __user *dst = (char __user *)dstv;
4498 unsigned long i = get_eb_folio_index(eb, start);
4501 WARN_ON(start > eb->len);
4502 WARN_ON(start + len > eb->start + eb->len);
4505 if (copy_to_user_nofault(dstv, eb->addr + start, len))
4510 offset = get_eb_offset_in_folio(eb, start);
4515 cur = min(len, unit_size - offset);
4516 kaddr = folio_address(eb->folios[i]);
4517 if (copy_to_user_nofault(dst, kaddr + offset, cur)) {
4531 int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
4532 unsigned long start, unsigned long len)
4534 const int unit_size = eb->folio_size;
4538 char *ptr = (char *)ptrv;
4539 unsigned long i = get_eb_folio_index(eb, start);
4542 if (check_eb_range(eb, start, len))
4546 return memcmp(ptrv, eb->addr + start, len);
4548 offset = get_eb_offset_in_folio(eb, start);
4551 cur = min(len, unit_size - offset);
4552 kaddr = folio_address(eb->folios[i]);
4553 ret = memcmp(ptr, kaddr + offset, cur);
4566 * Check that the extent buffer is uptodate.
4568 * For regular sector size == PAGE_SIZE case, check if @page is uptodate.
4569 * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE.
4571 static void assert_eb_folio_uptodate(const struct extent_buffer *eb, int i)
4573 struct btrfs_fs_info *fs_info = eb->fs_info;
4574 struct folio *folio = eb->folios[i];
4579 * If we are using the commit root we could potentially clear a page
4580 * Uptodate while we're using the extent buffer that we've previously
4581 * looked up. We don't want to complain in this case, as the page was
4582 * valid before, we just didn't write it out. Instead we want to catch
4583 * the case where we didn't actually read the block properly, which
4584 * would have !PageUptodate and !EXTENT_BUFFER_WRITE_ERR.
4586 if (test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
4589 if (fs_info->nodesize < PAGE_SIZE) {
4590 struct folio *folio = eb->folios[0];
4593 if (WARN_ON(!btrfs_subpage_test_uptodate(fs_info, folio,
4594 eb->start, eb->len)))
4595 btrfs_subpage_dump_bitmap(fs_info, folio, eb->start, eb->len);
4597 WARN_ON(!folio_test_uptodate(folio));
4601 static void __write_extent_buffer(const struct extent_buffer *eb,
4602 const void *srcv, unsigned long start,
4603 unsigned long len, bool use_memmove)
4605 const int unit_size = eb->folio_size;
4609 char *src = (char *)srcv;
4610 unsigned long i = get_eb_folio_index(eb, start);
4611 /* For unmapped (dummy) ebs, no need to check their uptodate status. */
4612 const bool check_uptodate = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
4614 if (check_eb_range(eb, start, len))
4619 memmove(eb->addr + start, srcv, len);
4621 memcpy(eb->addr + start, srcv, len);
4625 offset = get_eb_offset_in_folio(eb, start);
4629 assert_eb_folio_uptodate(eb, i);
4631 cur = min(len, unit_size - offset);
4632 kaddr = folio_address(eb->folios[i]);
4634 memmove(kaddr + offset, src, cur);
4636 memcpy(kaddr + offset, src, cur);
4645 void write_extent_buffer(const struct extent_buffer *eb, const void *srcv,
4646 unsigned long start, unsigned long len)
4648 return __write_extent_buffer(eb, srcv, start, len, false);
4651 static void memset_extent_buffer(const struct extent_buffer *eb, int c,
4652 unsigned long start, unsigned long len)
4654 const int unit_size = eb->folio_size;
4655 unsigned long cur = start;
4658 memset(eb->addr + start, c, len);
4662 while (cur < start + len) {
4663 unsigned long index = get_eb_folio_index(eb, cur);
4664 unsigned int offset = get_eb_offset_in_folio(eb, cur);
4665 unsigned int cur_len = min(start + len - cur, unit_size - offset);
4667 assert_eb_folio_uptodate(eb, index);
4668 memset(folio_address(eb->folios[index]) + offset, c, cur_len);
4674 void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
4677 if (check_eb_range(eb, start, len))
4679 return memset_extent_buffer(eb, 0, start, len);
4682 void copy_extent_buffer_full(const struct extent_buffer *dst,
4683 const struct extent_buffer *src)
4685 const int unit_size = src->folio_size;
4686 unsigned long cur = 0;
4688 ASSERT(dst->len == src->len);
4690 while (cur < src->len) {
4691 unsigned long index = get_eb_folio_index(src, cur);
4692 unsigned long offset = get_eb_offset_in_folio(src, cur);
4693 unsigned long cur_len = min(src->len, unit_size - offset);
4694 void *addr = folio_address(src->folios[index]) + offset;
4696 write_extent_buffer(dst, addr, cur, cur_len);
4702 void copy_extent_buffer(const struct extent_buffer *dst,
4703 const struct extent_buffer *src,
4704 unsigned long dst_offset, unsigned long src_offset,
4707 const int unit_size = dst->folio_size;
4708 u64 dst_len = dst->len;
4712 unsigned long i = get_eb_folio_index(dst, dst_offset);
4714 if (check_eb_range(dst, dst_offset, len) ||
4715 check_eb_range(src, src_offset, len))
4718 WARN_ON(src->len != dst_len);
4720 offset = get_eb_offset_in_folio(dst, dst_offset);
4723 assert_eb_folio_uptodate(dst, i);
4725 cur = min(len, (unsigned long)(unit_size - offset));
4727 kaddr = folio_address(dst->folios[i]);
4728 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4738 * Calculate the folio and offset of the byte containing the given bit number.
4740 * @eb: the extent buffer
4741 * @start: offset of the bitmap item in the extent buffer
4743 * @folio_index: return index of the folio in the extent buffer that contains
4744 * the given bit number
4745 * @folio_offset: return offset into the folio given by folio_index
4747 * This helper hides the ugliness of finding the byte in an extent buffer which
4748 * contains a given bit.
4750 static inline void eb_bitmap_offset(const struct extent_buffer *eb,
4751 unsigned long start, unsigned long nr,
4752 unsigned long *folio_index,
4753 size_t *folio_offset)
4755 size_t byte_offset = BIT_BYTE(nr);
4759 * The byte we want is the offset of the extent buffer + the offset of
4760 * the bitmap item in the extent buffer + the offset of the byte in the
4763 offset = start + offset_in_eb_folio(eb, eb->start) + byte_offset;
4765 *folio_index = offset >> eb->folio_shift;
4766 *folio_offset = offset_in_eb_folio(eb, offset);
4770 * Determine whether a bit in a bitmap item is set.
4772 * @eb: the extent buffer
4773 * @start: offset of the bitmap item in the extent buffer
4774 * @nr: bit number to test
4776 int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
4783 eb_bitmap_offset(eb, start, nr, &i, &offset);
4784 assert_eb_folio_uptodate(eb, i);
4785 kaddr = folio_address(eb->folios[i]);
4786 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
4789 static u8 *extent_buffer_get_byte(const struct extent_buffer *eb, unsigned long bytenr)
4791 unsigned long index = get_eb_folio_index(eb, bytenr);
4793 if (check_eb_range(eb, bytenr, 1))
4795 return folio_address(eb->folios[index]) + get_eb_offset_in_folio(eb, bytenr);
4799 * Set an area of a bitmap to 1.
4801 * @eb: the extent buffer
4802 * @start: offset of the bitmap item in the extent buffer
4803 * @pos: bit number of the first bit
4804 * @len: number of bits to set
4806 void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
4807 unsigned long pos, unsigned long len)
4809 unsigned int first_byte = start + BIT_BYTE(pos);
4810 unsigned int last_byte = start + BIT_BYTE(pos + len - 1);
4811 const bool same_byte = (first_byte == last_byte);
4812 u8 mask = BITMAP_FIRST_BYTE_MASK(pos);
4816 mask &= BITMAP_LAST_BYTE_MASK(pos + len);
4818 /* Handle the first byte. */
4819 kaddr = extent_buffer_get_byte(eb, first_byte);
4824 /* Handle the byte aligned part. */
4825 ASSERT(first_byte + 1 <= last_byte);
4826 memset_extent_buffer(eb, 0xff, first_byte + 1, last_byte - first_byte - 1);
4828 /* Handle the last byte. */
4829 kaddr = extent_buffer_get_byte(eb, last_byte);
4830 *kaddr |= BITMAP_LAST_BYTE_MASK(pos + len);
4835 * Clear an area of a bitmap.
4837 * @eb: the extent buffer
4838 * @start: offset of the bitmap item in the extent buffer
4839 * @pos: bit number of the first bit
4840 * @len: number of bits to clear
4842 void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
4843 unsigned long start, unsigned long pos,
4846 unsigned int first_byte = start + BIT_BYTE(pos);
4847 unsigned int last_byte = start + BIT_BYTE(pos + len - 1);
4848 const bool same_byte = (first_byte == last_byte);
4849 u8 mask = BITMAP_FIRST_BYTE_MASK(pos);
4853 mask &= BITMAP_LAST_BYTE_MASK(pos + len);
4855 /* Handle the first byte. */
4856 kaddr = extent_buffer_get_byte(eb, first_byte);
4861 /* Handle the byte aligned part. */
4862 ASSERT(first_byte + 1 <= last_byte);
4863 memset_extent_buffer(eb, 0, first_byte + 1, last_byte - first_byte - 1);
4865 /* Handle the last byte. */
4866 kaddr = extent_buffer_get_byte(eb, last_byte);
4867 *kaddr &= ~BITMAP_LAST_BYTE_MASK(pos + len);
4870 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4872 unsigned long distance = (src > dst) ? src - dst : dst - src;
4873 return distance < len;
4876 void memcpy_extent_buffer(const struct extent_buffer *dst,
4877 unsigned long dst_offset, unsigned long src_offset,
4880 const int unit_size = dst->folio_size;
4881 unsigned long cur_off = 0;
4883 if (check_eb_range(dst, dst_offset, len) ||
4884 check_eb_range(dst, src_offset, len))
4888 const bool use_memmove = areas_overlap(src_offset, dst_offset, len);
4891 memmove(dst->addr + dst_offset, dst->addr + src_offset, len);
4893 memcpy(dst->addr + dst_offset, dst->addr + src_offset, len);
4897 while (cur_off < len) {
4898 unsigned long cur_src = cur_off + src_offset;
4899 unsigned long folio_index = get_eb_folio_index(dst, cur_src);
4900 unsigned long folio_off = get_eb_offset_in_folio(dst, cur_src);
4901 unsigned long cur_len = min(src_offset + len - cur_src,
4902 unit_size - folio_off);
4903 void *src_addr = folio_address(dst->folios[folio_index]) + folio_off;
4904 const bool use_memmove = areas_overlap(src_offset + cur_off,
4905 dst_offset + cur_off, cur_len);
4907 __write_extent_buffer(dst, src_addr, dst_offset + cur_off, cur_len,
4913 void memmove_extent_buffer(const struct extent_buffer *dst,
4914 unsigned long dst_offset, unsigned long src_offset,
4917 unsigned long dst_end = dst_offset + len - 1;
4918 unsigned long src_end = src_offset + len - 1;
4920 if (check_eb_range(dst, dst_offset, len) ||
4921 check_eb_range(dst, src_offset, len))
4924 if (dst_offset < src_offset) {
4925 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4930 memmove(dst->addr + dst_offset, dst->addr + src_offset, len);
4935 unsigned long src_i;
4937 size_t dst_off_in_folio;
4938 size_t src_off_in_folio;
4942 src_i = get_eb_folio_index(dst, src_end);
4944 dst_off_in_folio = get_eb_offset_in_folio(dst, dst_end);
4945 src_off_in_folio = get_eb_offset_in_folio(dst, src_end);
4947 cur = min_t(unsigned long, len, src_off_in_folio + 1);
4948 cur = min(cur, dst_off_in_folio + 1);
4950 src_addr = folio_address(dst->folios[src_i]) + src_off_in_folio -
4952 use_memmove = areas_overlap(src_end - cur + 1, dst_end - cur + 1,
4955 __write_extent_buffer(dst, src_addr, dst_end - cur + 1, cur,
4964 #define GANG_LOOKUP_SIZE 16
4965 static struct extent_buffer *get_next_extent_buffer(
4966 struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
4968 struct extent_buffer *gang[GANG_LOOKUP_SIZE];
4969 struct extent_buffer *found = NULL;
4970 u64 page_start = page_offset(page);
4971 u64 cur = page_start;
4973 ASSERT(in_range(bytenr, page_start, PAGE_SIZE));
4974 lockdep_assert_held(&fs_info->buffer_lock);
4976 while (cur < page_start + PAGE_SIZE) {
4980 ret = radix_tree_gang_lookup(&fs_info->buffer_radix,
4981 (void **)gang, cur >> fs_info->sectorsize_bits,
4982 min_t(unsigned int, GANG_LOOKUP_SIZE,
4983 PAGE_SIZE / fs_info->nodesize));
4986 for (i = 0; i < ret; i++) {
4987 /* Already beyond page end */
4988 if (gang[i]->start >= page_start + PAGE_SIZE)
4991 if (gang[i]->start >= bytenr) {
4996 cur = gang[ret - 1]->start + gang[ret - 1]->len;
5002 static int try_release_subpage_extent_buffer(struct page *page)
5004 struct btrfs_fs_info *fs_info = page_to_fs_info(page);
5005 u64 cur = page_offset(page);
5006 const u64 end = page_offset(page) + PAGE_SIZE;
5010 struct extent_buffer *eb = NULL;
5013 * Unlike try_release_extent_buffer() which uses folio private
5014 * to grab buffer, for subpage case we rely on radix tree, thus
5015 * we need to ensure radix tree consistency.
5017 * We also want an atomic snapshot of the radix tree, thus go
5018 * with spinlock rather than RCU.
5020 spin_lock(&fs_info->buffer_lock);
5021 eb = get_next_extent_buffer(fs_info, page, cur);
5023 /* No more eb in the page range after or at cur */
5024 spin_unlock(&fs_info->buffer_lock);
5027 cur = eb->start + eb->len;
5030 * The same as try_release_extent_buffer(), to ensure the eb
5031 * won't disappear out from under us.
5033 spin_lock(&eb->refs_lock);
5034 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
5035 spin_unlock(&eb->refs_lock);
5036 spin_unlock(&fs_info->buffer_lock);
5039 spin_unlock(&fs_info->buffer_lock);
5042 * If tree ref isn't set then we know the ref on this eb is a
5043 * real ref, so just return, this eb will likely be freed soon
5046 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5047 spin_unlock(&eb->refs_lock);
5052 * Here we don't care about the return value, we will always
5053 * check the folio private at the end. And
5054 * release_extent_buffer() will release the refs_lock.
5056 release_extent_buffer(eb);
5059 * Finally to check if we have cleared folio private, as if we have
5060 * released all ebs in the page, the folio private should be cleared now.
5062 spin_lock(&page->mapping->i_private_lock);
5063 if (!folio_test_private(page_folio(page)))
5067 spin_unlock(&page->mapping->i_private_lock);
5072 int try_release_extent_buffer(struct page *page)
5074 struct folio *folio = page_folio(page);
5075 struct extent_buffer *eb;
5077 if (page_to_fs_info(page)->nodesize < PAGE_SIZE)
5078 return try_release_subpage_extent_buffer(page);
5081 * We need to make sure nobody is changing folio private, as we rely on
5082 * folio private as the pointer to extent buffer.
5084 spin_lock(&page->mapping->i_private_lock);
5085 if (!folio_test_private(folio)) {
5086 spin_unlock(&page->mapping->i_private_lock);
5090 eb = folio_get_private(folio);
5094 * This is a little awful but should be ok, we need to make sure that
5095 * the eb doesn't disappear out from under us while we're looking at
5098 spin_lock(&eb->refs_lock);
5099 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
5100 spin_unlock(&eb->refs_lock);
5101 spin_unlock(&page->mapping->i_private_lock);
5104 spin_unlock(&page->mapping->i_private_lock);
5107 * If tree ref isn't set then we know the ref on this eb is a real ref,
5108 * so just return, this page will likely be freed soon anyway.
5110 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5111 spin_unlock(&eb->refs_lock);
5115 return release_extent_buffer(eb);
5119 * Attempt to readahead a child block.
5121 * @fs_info: the fs_info
5122 * @bytenr: bytenr to read
5123 * @owner_root: objectid of the root that owns this eb
5124 * @gen: generation for the uptodate check, can be 0
5125 * @level: level for the eb
5127 * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a
5128 * normal uptodate check of the eb, without checking the generation. If we have
5129 * to read the block we will not block on anything.
5131 void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
5132 u64 bytenr, u64 owner_root, u64 gen, int level)
5134 struct btrfs_tree_parent_check check = {
5139 struct extent_buffer *eb;
5142 eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level);
5146 if (btrfs_buffer_uptodate(eb, gen, 1)) {
5147 free_extent_buffer(eb);
5151 ret = read_extent_buffer_pages(eb, WAIT_NONE, 0, &check);
5153 free_extent_buffer_stale(eb);
5155 free_extent_buffer(eb);
5159 * Readahead a node's child block.
5161 * @node: parent node we're reading from
5162 * @slot: slot in the parent node for the child we want to read
5164 * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at
5165 * the slot in the node provided.
5167 void btrfs_readahead_node_child(struct extent_buffer *node, int slot)
5169 btrfs_readahead_tree_block(node->fs_info,
5170 btrfs_node_blockptr(node, slot),
5171 btrfs_header_owner(node),
5172 btrfs_node_ptr_generation(node, slot),
5173 btrfs_header_level(node) - 1);