162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci#include <linux/bitops.h> 462306a36Sopenharmony_ci#include <linux/slab.h> 562306a36Sopenharmony_ci#include <linux/bio.h> 662306a36Sopenharmony_ci#include <linux/mm.h> 762306a36Sopenharmony_ci#include <linux/pagemap.h> 862306a36Sopenharmony_ci#include <linux/page-flags.h> 962306a36Sopenharmony_ci#include <linux/sched/mm.h> 1062306a36Sopenharmony_ci#include <linux/spinlock.h> 1162306a36Sopenharmony_ci#include <linux/blkdev.h> 1262306a36Sopenharmony_ci#include <linux/swap.h> 1362306a36Sopenharmony_ci#include <linux/writeback.h> 1462306a36Sopenharmony_ci#include <linux/pagevec.h> 1562306a36Sopenharmony_ci#include <linux/prefetch.h> 1662306a36Sopenharmony_ci#include <linux/fsverity.h> 1762306a36Sopenharmony_ci#include "misc.h" 1862306a36Sopenharmony_ci#include "extent_io.h" 1962306a36Sopenharmony_ci#include "extent-io-tree.h" 2062306a36Sopenharmony_ci#include "extent_map.h" 2162306a36Sopenharmony_ci#include "ctree.h" 2262306a36Sopenharmony_ci#include "btrfs_inode.h" 2362306a36Sopenharmony_ci#include "bio.h" 2462306a36Sopenharmony_ci#include "check-integrity.h" 2562306a36Sopenharmony_ci#include "locking.h" 2662306a36Sopenharmony_ci#include "rcu-string.h" 2762306a36Sopenharmony_ci#include "backref.h" 2862306a36Sopenharmony_ci#include "disk-io.h" 2962306a36Sopenharmony_ci#include "subpage.h" 3062306a36Sopenharmony_ci#include "zoned.h" 3162306a36Sopenharmony_ci#include "block-group.h" 3262306a36Sopenharmony_ci#include "compression.h" 3362306a36Sopenharmony_ci#include "fs.h" 3462306a36Sopenharmony_ci#include "accessors.h" 3562306a36Sopenharmony_ci#include "file-item.h" 3662306a36Sopenharmony_ci#include "file.h" 3762306a36Sopenharmony_ci#include "dev-replace.h" 3862306a36Sopenharmony_ci#include "super.h" 3962306a36Sopenharmony_ci#include "transaction.h" 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_cistatic struct kmem_cache *extent_buffer_cache; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci#ifdef CONFIG_BTRFS_DEBUG 4462306a36Sopenharmony_cistatic inline void btrfs_leak_debug_add_eb(struct extent_buffer *eb) 4562306a36Sopenharmony_ci{ 4662306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 4762306a36Sopenharmony_ci unsigned long flags; 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci spin_lock_irqsave(&fs_info->eb_leak_lock, flags); 5062306a36Sopenharmony_ci list_add(&eb->leak_list, &fs_info->allocated_ebs); 5162306a36Sopenharmony_ci spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); 5262306a36Sopenharmony_ci} 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cistatic inline void btrfs_leak_debug_del_eb(struct extent_buffer *eb) 5562306a36Sopenharmony_ci{ 5662306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 5762306a36Sopenharmony_ci unsigned long flags; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci spin_lock_irqsave(&fs_info->eb_leak_lock, flags); 6062306a36Sopenharmony_ci list_del(&eb->leak_list); 6162306a36Sopenharmony_ci spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); 6262306a36Sopenharmony_ci} 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_civoid btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info) 6562306a36Sopenharmony_ci{ 6662306a36Sopenharmony_ci struct extent_buffer *eb; 6762306a36Sopenharmony_ci unsigned long flags; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci /* 7062306a36Sopenharmony_ci * If we didn't get into open_ctree our allocated_ebs will not be 7162306a36Sopenharmony_ci * initialized, so just skip this. 7262306a36Sopenharmony_ci */ 7362306a36Sopenharmony_ci if (!fs_info->allocated_ebs.next) 7462306a36Sopenharmony_ci return; 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci WARN_ON(!list_empty(&fs_info->allocated_ebs)); 7762306a36Sopenharmony_ci spin_lock_irqsave(&fs_info->eb_leak_lock, flags); 7862306a36Sopenharmony_ci while (!list_empty(&fs_info->allocated_ebs)) { 7962306a36Sopenharmony_ci eb = list_first_entry(&fs_info->allocated_ebs, 8062306a36Sopenharmony_ci struct extent_buffer, leak_list); 8162306a36Sopenharmony_ci pr_err( 8262306a36Sopenharmony_ci "BTRFS: buffer leak start %llu len %lu refs %d bflags %lu owner %llu\n", 8362306a36Sopenharmony_ci eb->start, eb->len, atomic_read(&eb->refs), eb->bflags, 8462306a36Sopenharmony_ci btrfs_header_owner(eb)); 8562306a36Sopenharmony_ci list_del(&eb->leak_list); 8662306a36Sopenharmony_ci kmem_cache_free(extent_buffer_cache, eb); 8762306a36Sopenharmony_ci } 8862306a36Sopenharmony_ci spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); 8962306a36Sopenharmony_ci} 9062306a36Sopenharmony_ci#else 9162306a36Sopenharmony_ci#define btrfs_leak_debug_add_eb(eb) do {} while (0) 9262306a36Sopenharmony_ci#define btrfs_leak_debug_del_eb(eb) do {} while (0) 9362306a36Sopenharmony_ci#endif 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci/* 9662306a36Sopenharmony_ci * Structure to record info about the bio being assembled, and other info like 9762306a36Sopenharmony_ci * how many bytes are there before stripe/ordered extent boundary. 9862306a36Sopenharmony_ci */ 9962306a36Sopenharmony_cistruct btrfs_bio_ctrl { 10062306a36Sopenharmony_ci struct btrfs_bio *bbio; 10162306a36Sopenharmony_ci enum btrfs_compression_type compress_type; 10262306a36Sopenharmony_ci u32 len_to_oe_boundary; 10362306a36Sopenharmony_ci blk_opf_t opf; 10462306a36Sopenharmony_ci btrfs_bio_end_io_t end_io_func; 10562306a36Sopenharmony_ci struct writeback_control *wbc; 10662306a36Sopenharmony_ci}; 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_cistatic void submit_one_bio(struct btrfs_bio_ctrl *bio_ctrl) 10962306a36Sopenharmony_ci{ 11062306a36Sopenharmony_ci struct btrfs_bio *bbio = bio_ctrl->bbio; 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci if (!bbio) 11362306a36Sopenharmony_ci return; 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci /* Caller should ensure the bio has at least some range added */ 11662306a36Sopenharmony_ci ASSERT(bbio->bio.bi_iter.bi_size); 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci if (btrfs_op(&bbio->bio) == BTRFS_MAP_READ && 11962306a36Sopenharmony_ci bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) 12062306a36Sopenharmony_ci btrfs_submit_compressed_read(bbio); 12162306a36Sopenharmony_ci else 12262306a36Sopenharmony_ci btrfs_submit_bio(bbio, 0); 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci /* The bbio is owned by the end_io handler now */ 12562306a36Sopenharmony_ci bio_ctrl->bbio = NULL; 12662306a36Sopenharmony_ci} 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci/* 12962306a36Sopenharmony_ci * Submit or fail the current bio in the bio_ctrl structure. 13062306a36Sopenharmony_ci */ 13162306a36Sopenharmony_cistatic void submit_write_bio(struct btrfs_bio_ctrl *bio_ctrl, int ret) 13262306a36Sopenharmony_ci{ 13362306a36Sopenharmony_ci struct btrfs_bio *bbio = bio_ctrl->bbio; 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci if (!bbio) 13662306a36Sopenharmony_ci return; 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci if (ret) { 13962306a36Sopenharmony_ci ASSERT(ret < 0); 14062306a36Sopenharmony_ci btrfs_bio_end_io(bbio, errno_to_blk_status(ret)); 14162306a36Sopenharmony_ci /* The bio is owned by the end_io handler now */ 14262306a36Sopenharmony_ci bio_ctrl->bbio = NULL; 14362306a36Sopenharmony_ci } else { 14462306a36Sopenharmony_ci submit_one_bio(bio_ctrl); 14562306a36Sopenharmony_ci } 14662306a36Sopenharmony_ci} 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ciint __init extent_buffer_init_cachep(void) 14962306a36Sopenharmony_ci{ 15062306a36Sopenharmony_ci extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", 15162306a36Sopenharmony_ci sizeof(struct extent_buffer), 0, 15262306a36Sopenharmony_ci SLAB_MEM_SPREAD, NULL); 15362306a36Sopenharmony_ci if (!extent_buffer_cache) 15462306a36Sopenharmony_ci return -ENOMEM; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci return 0; 15762306a36Sopenharmony_ci} 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_civoid __cold extent_buffer_free_cachep(void) 16062306a36Sopenharmony_ci{ 16162306a36Sopenharmony_ci /* 16262306a36Sopenharmony_ci * Make sure all delayed rcu free are flushed before we 16362306a36Sopenharmony_ci * destroy caches. 16462306a36Sopenharmony_ci */ 16562306a36Sopenharmony_ci rcu_barrier(); 16662306a36Sopenharmony_ci kmem_cache_destroy(extent_buffer_cache); 16762306a36Sopenharmony_ci} 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_civoid extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) 17062306a36Sopenharmony_ci{ 17162306a36Sopenharmony_ci unsigned long index = start >> PAGE_SHIFT; 17262306a36Sopenharmony_ci unsigned long end_index = end >> PAGE_SHIFT; 17362306a36Sopenharmony_ci struct page *page; 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci while (index <= end_index) { 17662306a36Sopenharmony_ci page = find_get_page(inode->i_mapping, index); 17762306a36Sopenharmony_ci BUG_ON(!page); /* Pages should be in the extent_io_tree */ 17862306a36Sopenharmony_ci clear_page_dirty_for_io(page); 17962306a36Sopenharmony_ci put_page(page); 18062306a36Sopenharmony_ci index++; 18162306a36Sopenharmony_ci } 18262306a36Sopenharmony_ci} 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_cistatic void process_one_page(struct btrfs_fs_info *fs_info, 18562306a36Sopenharmony_ci struct page *page, struct page *locked_page, 18662306a36Sopenharmony_ci unsigned long page_ops, u64 start, u64 end) 18762306a36Sopenharmony_ci{ 18862306a36Sopenharmony_ci u32 len; 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX); 19162306a36Sopenharmony_ci len = end + 1 - start; 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci if (page_ops & PAGE_SET_ORDERED) 19462306a36Sopenharmony_ci btrfs_page_clamp_set_ordered(fs_info, page, start, len); 19562306a36Sopenharmony_ci if (page_ops & PAGE_START_WRITEBACK) { 19662306a36Sopenharmony_ci btrfs_page_clamp_clear_dirty(fs_info, page, start, len); 19762306a36Sopenharmony_ci btrfs_page_clamp_set_writeback(fs_info, page, start, len); 19862306a36Sopenharmony_ci } 19962306a36Sopenharmony_ci if (page_ops & PAGE_END_WRITEBACK) 20062306a36Sopenharmony_ci btrfs_page_clamp_clear_writeback(fs_info, page, start, len); 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci if (page != locked_page && (page_ops & PAGE_UNLOCK)) 20362306a36Sopenharmony_ci btrfs_page_end_writer_lock(fs_info, page, start, len); 20462306a36Sopenharmony_ci} 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_cistatic void __process_pages_contig(struct address_space *mapping, 20762306a36Sopenharmony_ci struct page *locked_page, u64 start, u64 end, 20862306a36Sopenharmony_ci unsigned long page_ops) 20962306a36Sopenharmony_ci{ 21062306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb); 21162306a36Sopenharmony_ci pgoff_t start_index = start >> PAGE_SHIFT; 21262306a36Sopenharmony_ci pgoff_t end_index = end >> PAGE_SHIFT; 21362306a36Sopenharmony_ci pgoff_t index = start_index; 21462306a36Sopenharmony_ci struct folio_batch fbatch; 21562306a36Sopenharmony_ci int i; 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci folio_batch_init(&fbatch); 21862306a36Sopenharmony_ci while (index <= end_index) { 21962306a36Sopenharmony_ci int found_folios; 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci found_folios = filemap_get_folios_contig(mapping, &index, 22262306a36Sopenharmony_ci end_index, &fbatch); 22362306a36Sopenharmony_ci for (i = 0; i < found_folios; i++) { 22462306a36Sopenharmony_ci struct folio *folio = fbatch.folios[i]; 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci process_one_page(fs_info, &folio->page, locked_page, 22762306a36Sopenharmony_ci page_ops, start, end); 22862306a36Sopenharmony_ci } 22962306a36Sopenharmony_ci folio_batch_release(&fbatch); 23062306a36Sopenharmony_ci cond_resched(); 23162306a36Sopenharmony_ci } 23262306a36Sopenharmony_ci} 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_cistatic noinline void __unlock_for_delalloc(struct inode *inode, 23562306a36Sopenharmony_ci struct page *locked_page, 23662306a36Sopenharmony_ci u64 start, u64 end) 23762306a36Sopenharmony_ci{ 23862306a36Sopenharmony_ci unsigned long index = start >> PAGE_SHIFT; 23962306a36Sopenharmony_ci unsigned long end_index = end >> PAGE_SHIFT; 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci ASSERT(locked_page); 24262306a36Sopenharmony_ci if (index == locked_page->index && end_index == index) 24362306a36Sopenharmony_ci return; 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci __process_pages_contig(inode->i_mapping, locked_page, start, end, 24662306a36Sopenharmony_ci PAGE_UNLOCK); 24762306a36Sopenharmony_ci} 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_cistatic noinline int lock_delalloc_pages(struct inode *inode, 25062306a36Sopenharmony_ci struct page *locked_page, 25162306a36Sopenharmony_ci u64 start, 25262306a36Sopenharmony_ci u64 end) 25362306a36Sopenharmony_ci{ 25462306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 25562306a36Sopenharmony_ci struct address_space *mapping = inode->i_mapping; 25662306a36Sopenharmony_ci pgoff_t start_index = start >> PAGE_SHIFT; 25762306a36Sopenharmony_ci pgoff_t end_index = end >> PAGE_SHIFT; 25862306a36Sopenharmony_ci pgoff_t index = start_index; 25962306a36Sopenharmony_ci u64 processed_end = start; 26062306a36Sopenharmony_ci struct folio_batch fbatch; 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci if (index == locked_page->index && index == end_index) 26362306a36Sopenharmony_ci return 0; 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci folio_batch_init(&fbatch); 26662306a36Sopenharmony_ci while (index <= end_index) { 26762306a36Sopenharmony_ci unsigned int found_folios, i; 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci found_folios = filemap_get_folios_contig(mapping, &index, 27062306a36Sopenharmony_ci end_index, &fbatch); 27162306a36Sopenharmony_ci if (found_folios == 0) 27262306a36Sopenharmony_ci goto out; 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ci for (i = 0; i < found_folios; i++) { 27562306a36Sopenharmony_ci struct page *page = &fbatch.folios[i]->page; 27662306a36Sopenharmony_ci u32 len = end + 1 - start; 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_ci if (page == locked_page) 27962306a36Sopenharmony_ci continue; 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ci if (btrfs_page_start_writer_lock(fs_info, page, start, 28262306a36Sopenharmony_ci len)) 28362306a36Sopenharmony_ci goto out; 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci if (!PageDirty(page) || page->mapping != mapping) { 28662306a36Sopenharmony_ci btrfs_page_end_writer_lock(fs_info, page, start, 28762306a36Sopenharmony_ci len); 28862306a36Sopenharmony_ci goto out; 28962306a36Sopenharmony_ci } 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci processed_end = page_offset(page) + PAGE_SIZE - 1; 29262306a36Sopenharmony_ci } 29362306a36Sopenharmony_ci folio_batch_release(&fbatch); 29462306a36Sopenharmony_ci cond_resched(); 29562306a36Sopenharmony_ci } 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ci return 0; 29862306a36Sopenharmony_ciout: 29962306a36Sopenharmony_ci folio_batch_release(&fbatch); 30062306a36Sopenharmony_ci if (processed_end > start) 30162306a36Sopenharmony_ci __unlock_for_delalloc(inode, locked_page, start, processed_end); 30262306a36Sopenharmony_ci return -EAGAIN; 30362306a36Sopenharmony_ci} 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci/* 30662306a36Sopenharmony_ci * Find and lock a contiguous range of bytes in the file marked as delalloc, no 30762306a36Sopenharmony_ci * more than @max_bytes. 30862306a36Sopenharmony_ci * 30962306a36Sopenharmony_ci * @start: The original start bytenr to search. 31062306a36Sopenharmony_ci * Will store the extent range start bytenr. 31162306a36Sopenharmony_ci * @end: The original end bytenr of the search range 31262306a36Sopenharmony_ci * Will store the extent range end bytenr. 31362306a36Sopenharmony_ci * 31462306a36Sopenharmony_ci * Return true if we find a delalloc range which starts inside the original 31562306a36Sopenharmony_ci * range, and @start/@end will store the delalloc range start/end. 31662306a36Sopenharmony_ci * 31762306a36Sopenharmony_ci * Return false if we can't find any delalloc range which starts inside the 31862306a36Sopenharmony_ci * original range, and @start/@end will be the non-delalloc range start/end. 31962306a36Sopenharmony_ci */ 32062306a36Sopenharmony_ciEXPORT_FOR_TESTS 32162306a36Sopenharmony_cinoinline_for_stack bool find_lock_delalloc_range(struct inode *inode, 32262306a36Sopenharmony_ci struct page *locked_page, u64 *start, 32362306a36Sopenharmony_ci u64 *end) 32462306a36Sopenharmony_ci{ 32562306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 32662306a36Sopenharmony_ci struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; 32762306a36Sopenharmony_ci const u64 orig_start = *start; 32862306a36Sopenharmony_ci const u64 orig_end = *end; 32962306a36Sopenharmony_ci /* The sanity tests may not set a valid fs_info. */ 33062306a36Sopenharmony_ci u64 max_bytes = fs_info ? fs_info->max_extent_size : BTRFS_MAX_EXTENT_SIZE; 33162306a36Sopenharmony_ci u64 delalloc_start; 33262306a36Sopenharmony_ci u64 delalloc_end; 33362306a36Sopenharmony_ci bool found; 33462306a36Sopenharmony_ci struct extent_state *cached_state = NULL; 33562306a36Sopenharmony_ci int ret; 33662306a36Sopenharmony_ci int loops = 0; 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci /* Caller should pass a valid @end to indicate the search range end */ 33962306a36Sopenharmony_ci ASSERT(orig_end > orig_start); 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci /* The range should at least cover part of the page */ 34262306a36Sopenharmony_ci ASSERT(!(orig_start >= page_offset(locked_page) + PAGE_SIZE || 34362306a36Sopenharmony_ci orig_end <= page_offset(locked_page))); 34462306a36Sopenharmony_ciagain: 34562306a36Sopenharmony_ci /* step one, find a bunch of delalloc bytes starting at start */ 34662306a36Sopenharmony_ci delalloc_start = *start; 34762306a36Sopenharmony_ci delalloc_end = 0; 34862306a36Sopenharmony_ci found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end, 34962306a36Sopenharmony_ci max_bytes, &cached_state); 35062306a36Sopenharmony_ci if (!found || delalloc_end <= *start || delalloc_start > orig_end) { 35162306a36Sopenharmony_ci *start = delalloc_start; 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ci /* @delalloc_end can be -1, never go beyond @orig_end */ 35462306a36Sopenharmony_ci *end = min(delalloc_end, orig_end); 35562306a36Sopenharmony_ci free_extent_state(cached_state); 35662306a36Sopenharmony_ci return false; 35762306a36Sopenharmony_ci } 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci /* 36062306a36Sopenharmony_ci * start comes from the offset of locked_page. We have to lock 36162306a36Sopenharmony_ci * pages in order, so we can't process delalloc bytes before 36262306a36Sopenharmony_ci * locked_page 36362306a36Sopenharmony_ci */ 36462306a36Sopenharmony_ci if (delalloc_start < *start) 36562306a36Sopenharmony_ci delalloc_start = *start; 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ci /* 36862306a36Sopenharmony_ci * make sure to limit the number of pages we try to lock down 36962306a36Sopenharmony_ci */ 37062306a36Sopenharmony_ci if (delalloc_end + 1 - delalloc_start > max_bytes) 37162306a36Sopenharmony_ci delalloc_end = delalloc_start + max_bytes - 1; 37262306a36Sopenharmony_ci 37362306a36Sopenharmony_ci /* step two, lock all the pages after the page that has start */ 37462306a36Sopenharmony_ci ret = lock_delalloc_pages(inode, locked_page, 37562306a36Sopenharmony_ci delalloc_start, delalloc_end); 37662306a36Sopenharmony_ci ASSERT(!ret || ret == -EAGAIN); 37762306a36Sopenharmony_ci if (ret == -EAGAIN) { 37862306a36Sopenharmony_ci /* some of the pages are gone, lets avoid looping by 37962306a36Sopenharmony_ci * shortening the size of the delalloc range we're searching 38062306a36Sopenharmony_ci */ 38162306a36Sopenharmony_ci free_extent_state(cached_state); 38262306a36Sopenharmony_ci cached_state = NULL; 38362306a36Sopenharmony_ci if (!loops) { 38462306a36Sopenharmony_ci max_bytes = PAGE_SIZE; 38562306a36Sopenharmony_ci loops = 1; 38662306a36Sopenharmony_ci goto again; 38762306a36Sopenharmony_ci } else { 38862306a36Sopenharmony_ci found = false; 38962306a36Sopenharmony_ci goto out_failed; 39062306a36Sopenharmony_ci } 39162306a36Sopenharmony_ci } 39262306a36Sopenharmony_ci 39362306a36Sopenharmony_ci /* step three, lock the state bits for the whole range */ 39462306a36Sopenharmony_ci lock_extent(tree, delalloc_start, delalloc_end, &cached_state); 39562306a36Sopenharmony_ci 39662306a36Sopenharmony_ci /* then test to make sure it is all still delalloc */ 39762306a36Sopenharmony_ci ret = test_range_bit(tree, delalloc_start, delalloc_end, 39862306a36Sopenharmony_ci EXTENT_DELALLOC, 1, cached_state); 39962306a36Sopenharmony_ci if (!ret) { 40062306a36Sopenharmony_ci unlock_extent(tree, delalloc_start, delalloc_end, 40162306a36Sopenharmony_ci &cached_state); 40262306a36Sopenharmony_ci __unlock_for_delalloc(inode, locked_page, 40362306a36Sopenharmony_ci delalloc_start, delalloc_end); 40462306a36Sopenharmony_ci cond_resched(); 40562306a36Sopenharmony_ci goto again; 40662306a36Sopenharmony_ci } 40762306a36Sopenharmony_ci free_extent_state(cached_state); 40862306a36Sopenharmony_ci *start = delalloc_start; 40962306a36Sopenharmony_ci *end = delalloc_end; 41062306a36Sopenharmony_ciout_failed: 41162306a36Sopenharmony_ci return found; 41262306a36Sopenharmony_ci} 41362306a36Sopenharmony_ci 41462306a36Sopenharmony_civoid extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end, 41562306a36Sopenharmony_ci struct page *locked_page, 41662306a36Sopenharmony_ci u32 clear_bits, unsigned long page_ops) 41762306a36Sopenharmony_ci{ 41862306a36Sopenharmony_ci clear_extent_bit(&inode->io_tree, start, end, clear_bits, NULL); 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ci __process_pages_contig(inode->vfs_inode.i_mapping, locked_page, 42162306a36Sopenharmony_ci start, end, page_ops); 42262306a36Sopenharmony_ci} 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_cistatic bool btrfs_verify_page(struct page *page, u64 start) 42562306a36Sopenharmony_ci{ 42662306a36Sopenharmony_ci if (!fsverity_active(page->mapping->host) || 42762306a36Sopenharmony_ci PageUptodate(page) || 42862306a36Sopenharmony_ci start >= i_size_read(page->mapping->host)) 42962306a36Sopenharmony_ci return true; 43062306a36Sopenharmony_ci return fsverity_verify_page(page); 43162306a36Sopenharmony_ci} 43262306a36Sopenharmony_ci 43362306a36Sopenharmony_cistatic void end_page_read(struct page *page, bool uptodate, u64 start, u32 len) 43462306a36Sopenharmony_ci{ 43562306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci ASSERT(page_offset(page) <= start && 43862306a36Sopenharmony_ci start + len <= page_offset(page) + PAGE_SIZE); 43962306a36Sopenharmony_ci 44062306a36Sopenharmony_ci if (uptodate && btrfs_verify_page(page, start)) 44162306a36Sopenharmony_ci btrfs_page_set_uptodate(fs_info, page, start, len); 44262306a36Sopenharmony_ci else 44362306a36Sopenharmony_ci btrfs_page_clear_uptodate(fs_info, page, start, len); 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_ci if (!btrfs_is_subpage(fs_info, page)) 44662306a36Sopenharmony_ci unlock_page(page); 44762306a36Sopenharmony_ci else 44862306a36Sopenharmony_ci btrfs_subpage_end_reader(fs_info, page, start, len); 44962306a36Sopenharmony_ci} 45062306a36Sopenharmony_ci 45162306a36Sopenharmony_ci/* 45262306a36Sopenharmony_ci * after a writepage IO is done, we need to: 45362306a36Sopenharmony_ci * clear the uptodate bits on error 45462306a36Sopenharmony_ci * clear the writeback bits in the extent tree for this IO 45562306a36Sopenharmony_ci * end_page_writeback if the page has no more pending IO 45662306a36Sopenharmony_ci * 45762306a36Sopenharmony_ci * Scheduling is not allowed, so the extent state tree is expected 45862306a36Sopenharmony_ci * to have one and only one object corresponding to this IO. 45962306a36Sopenharmony_ci */ 46062306a36Sopenharmony_cistatic void end_bio_extent_writepage(struct btrfs_bio *bbio) 46162306a36Sopenharmony_ci{ 46262306a36Sopenharmony_ci struct bio *bio = &bbio->bio; 46362306a36Sopenharmony_ci int error = blk_status_to_errno(bio->bi_status); 46462306a36Sopenharmony_ci struct bio_vec *bvec; 46562306a36Sopenharmony_ci struct bvec_iter_all iter_all; 46662306a36Sopenharmony_ci 46762306a36Sopenharmony_ci ASSERT(!bio_flagged(bio, BIO_CLONED)); 46862306a36Sopenharmony_ci bio_for_each_segment_all(bvec, bio, iter_all) { 46962306a36Sopenharmony_ci struct page *page = bvec->bv_page; 47062306a36Sopenharmony_ci struct inode *inode = page->mapping->host; 47162306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 47262306a36Sopenharmony_ci const u32 sectorsize = fs_info->sectorsize; 47362306a36Sopenharmony_ci u64 start = page_offset(page) + bvec->bv_offset; 47462306a36Sopenharmony_ci u32 len = bvec->bv_len; 47562306a36Sopenharmony_ci 47662306a36Sopenharmony_ci /* Our read/write should always be sector aligned. */ 47762306a36Sopenharmony_ci if (!IS_ALIGNED(bvec->bv_offset, sectorsize)) 47862306a36Sopenharmony_ci btrfs_err(fs_info, 47962306a36Sopenharmony_ci "partial page write in btrfs with offset %u and length %u", 48062306a36Sopenharmony_ci bvec->bv_offset, bvec->bv_len); 48162306a36Sopenharmony_ci else if (!IS_ALIGNED(bvec->bv_len, sectorsize)) 48262306a36Sopenharmony_ci btrfs_info(fs_info, 48362306a36Sopenharmony_ci "incomplete page write with offset %u and length %u", 48462306a36Sopenharmony_ci bvec->bv_offset, bvec->bv_len); 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_ci btrfs_finish_ordered_extent(bbio->ordered, page, start, len, !error); 48762306a36Sopenharmony_ci if (error) 48862306a36Sopenharmony_ci mapping_set_error(page->mapping, error); 48962306a36Sopenharmony_ci btrfs_page_clear_writeback(fs_info, page, start, len); 49062306a36Sopenharmony_ci } 49162306a36Sopenharmony_ci 49262306a36Sopenharmony_ci bio_put(bio); 49362306a36Sopenharmony_ci} 49462306a36Sopenharmony_ci 49562306a36Sopenharmony_ci/* 49662306a36Sopenharmony_ci * Record previously processed extent range 49762306a36Sopenharmony_ci * 49862306a36Sopenharmony_ci * For endio_readpage_release_extent() to handle a full extent range, reducing 49962306a36Sopenharmony_ci * the extent io operations. 50062306a36Sopenharmony_ci */ 50162306a36Sopenharmony_cistruct processed_extent { 50262306a36Sopenharmony_ci struct btrfs_inode *inode; 50362306a36Sopenharmony_ci /* Start of the range in @inode */ 50462306a36Sopenharmony_ci u64 start; 50562306a36Sopenharmony_ci /* End of the range in @inode */ 50662306a36Sopenharmony_ci u64 end; 50762306a36Sopenharmony_ci bool uptodate; 50862306a36Sopenharmony_ci}; 50962306a36Sopenharmony_ci 51062306a36Sopenharmony_ci/* 51162306a36Sopenharmony_ci * Try to release processed extent range 51262306a36Sopenharmony_ci * 51362306a36Sopenharmony_ci * May not release the extent range right now if the current range is 51462306a36Sopenharmony_ci * contiguous to processed extent. 51562306a36Sopenharmony_ci * 51662306a36Sopenharmony_ci * Will release processed extent when any of @inode, @uptodate, the range is 51762306a36Sopenharmony_ci * no longer contiguous to the processed range. 51862306a36Sopenharmony_ci * 51962306a36Sopenharmony_ci * Passing @inode == NULL will force processed extent to be released. 52062306a36Sopenharmony_ci */ 52162306a36Sopenharmony_cistatic void endio_readpage_release_extent(struct processed_extent *processed, 52262306a36Sopenharmony_ci struct btrfs_inode *inode, u64 start, u64 end, 52362306a36Sopenharmony_ci bool uptodate) 52462306a36Sopenharmony_ci{ 52562306a36Sopenharmony_ci struct extent_state *cached = NULL; 52662306a36Sopenharmony_ci struct extent_io_tree *tree; 52762306a36Sopenharmony_ci 52862306a36Sopenharmony_ci /* The first extent, initialize @processed */ 52962306a36Sopenharmony_ci if (!processed->inode) 53062306a36Sopenharmony_ci goto update; 53162306a36Sopenharmony_ci 53262306a36Sopenharmony_ci /* 53362306a36Sopenharmony_ci * Contiguous to processed extent, just uptodate the end. 53462306a36Sopenharmony_ci * 53562306a36Sopenharmony_ci * Several things to notice: 53662306a36Sopenharmony_ci * 53762306a36Sopenharmony_ci * - bio can be merged as long as on-disk bytenr is contiguous 53862306a36Sopenharmony_ci * This means we can have page belonging to other inodes, thus need to 53962306a36Sopenharmony_ci * check if the inode still matches. 54062306a36Sopenharmony_ci * - bvec can contain range beyond current page for multi-page bvec 54162306a36Sopenharmony_ci * Thus we need to do processed->end + 1 >= start check 54262306a36Sopenharmony_ci */ 54362306a36Sopenharmony_ci if (processed->inode == inode && processed->uptodate == uptodate && 54462306a36Sopenharmony_ci processed->end + 1 >= start && end >= processed->end) { 54562306a36Sopenharmony_ci processed->end = end; 54662306a36Sopenharmony_ci return; 54762306a36Sopenharmony_ci } 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci tree = &processed->inode->io_tree; 55062306a36Sopenharmony_ci /* 55162306a36Sopenharmony_ci * Now we don't have range contiguous to the processed range, release 55262306a36Sopenharmony_ci * the processed range now. 55362306a36Sopenharmony_ci */ 55462306a36Sopenharmony_ci unlock_extent(tree, processed->start, processed->end, &cached); 55562306a36Sopenharmony_ci 55662306a36Sopenharmony_ciupdate: 55762306a36Sopenharmony_ci /* Update processed to current range */ 55862306a36Sopenharmony_ci processed->inode = inode; 55962306a36Sopenharmony_ci processed->start = start; 56062306a36Sopenharmony_ci processed->end = end; 56162306a36Sopenharmony_ci processed->uptodate = uptodate; 56262306a36Sopenharmony_ci} 56362306a36Sopenharmony_ci 56462306a36Sopenharmony_cistatic void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page) 56562306a36Sopenharmony_ci{ 56662306a36Sopenharmony_ci ASSERT(PageLocked(page)); 56762306a36Sopenharmony_ci if (!btrfs_is_subpage(fs_info, page)) 56862306a36Sopenharmony_ci return; 56962306a36Sopenharmony_ci 57062306a36Sopenharmony_ci ASSERT(PagePrivate(page)); 57162306a36Sopenharmony_ci btrfs_subpage_start_reader(fs_info, page, page_offset(page), PAGE_SIZE); 57262306a36Sopenharmony_ci} 57362306a36Sopenharmony_ci 57462306a36Sopenharmony_ci/* 57562306a36Sopenharmony_ci * after a readpage IO is done, we need to: 57662306a36Sopenharmony_ci * clear the uptodate bits on error 57762306a36Sopenharmony_ci * set the uptodate bits if things worked 57862306a36Sopenharmony_ci * set the page up to date if all extents in the tree are uptodate 57962306a36Sopenharmony_ci * clear the lock bit in the extent tree 58062306a36Sopenharmony_ci * unlock the page if there are no other extents locked for it 58162306a36Sopenharmony_ci * 58262306a36Sopenharmony_ci * Scheduling is not allowed, so the extent state tree is expected 58362306a36Sopenharmony_ci * to have one and only one object corresponding to this IO. 58462306a36Sopenharmony_ci */ 58562306a36Sopenharmony_cistatic void end_bio_extent_readpage(struct btrfs_bio *bbio) 58662306a36Sopenharmony_ci{ 58762306a36Sopenharmony_ci struct bio *bio = &bbio->bio; 58862306a36Sopenharmony_ci struct bio_vec *bvec; 58962306a36Sopenharmony_ci struct processed_extent processed = { 0 }; 59062306a36Sopenharmony_ci /* 59162306a36Sopenharmony_ci * The offset to the beginning of a bio, since one bio can never be 59262306a36Sopenharmony_ci * larger than UINT_MAX, u32 here is enough. 59362306a36Sopenharmony_ci */ 59462306a36Sopenharmony_ci u32 bio_offset = 0; 59562306a36Sopenharmony_ci struct bvec_iter_all iter_all; 59662306a36Sopenharmony_ci 59762306a36Sopenharmony_ci ASSERT(!bio_flagged(bio, BIO_CLONED)); 59862306a36Sopenharmony_ci bio_for_each_segment_all(bvec, bio, iter_all) { 59962306a36Sopenharmony_ci bool uptodate = !bio->bi_status; 60062306a36Sopenharmony_ci struct page *page = bvec->bv_page; 60162306a36Sopenharmony_ci struct inode *inode = page->mapping->host; 60262306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 60362306a36Sopenharmony_ci const u32 sectorsize = fs_info->sectorsize; 60462306a36Sopenharmony_ci u64 start; 60562306a36Sopenharmony_ci u64 end; 60662306a36Sopenharmony_ci u32 len; 60762306a36Sopenharmony_ci 60862306a36Sopenharmony_ci btrfs_debug(fs_info, 60962306a36Sopenharmony_ci "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u", 61062306a36Sopenharmony_ci bio->bi_iter.bi_sector, bio->bi_status, 61162306a36Sopenharmony_ci bbio->mirror_num); 61262306a36Sopenharmony_ci 61362306a36Sopenharmony_ci /* 61462306a36Sopenharmony_ci * We always issue full-sector reads, but if some block in a 61562306a36Sopenharmony_ci * page fails to read, blk_update_request() will advance 61662306a36Sopenharmony_ci * bv_offset and adjust bv_len to compensate. Print a warning 61762306a36Sopenharmony_ci * for unaligned offsets, and an error if they don't add up to 61862306a36Sopenharmony_ci * a full sector. 61962306a36Sopenharmony_ci */ 62062306a36Sopenharmony_ci if (!IS_ALIGNED(bvec->bv_offset, sectorsize)) 62162306a36Sopenharmony_ci btrfs_err(fs_info, 62262306a36Sopenharmony_ci "partial page read in btrfs with offset %u and length %u", 62362306a36Sopenharmony_ci bvec->bv_offset, bvec->bv_len); 62462306a36Sopenharmony_ci else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len, 62562306a36Sopenharmony_ci sectorsize)) 62662306a36Sopenharmony_ci btrfs_info(fs_info, 62762306a36Sopenharmony_ci "incomplete page read with offset %u and length %u", 62862306a36Sopenharmony_ci bvec->bv_offset, bvec->bv_len); 62962306a36Sopenharmony_ci 63062306a36Sopenharmony_ci start = page_offset(page) + bvec->bv_offset; 63162306a36Sopenharmony_ci end = start + bvec->bv_len - 1; 63262306a36Sopenharmony_ci len = bvec->bv_len; 63362306a36Sopenharmony_ci 63462306a36Sopenharmony_ci if (likely(uptodate)) { 63562306a36Sopenharmony_ci loff_t i_size = i_size_read(inode); 63662306a36Sopenharmony_ci pgoff_t end_index = i_size >> PAGE_SHIFT; 63762306a36Sopenharmony_ci 63862306a36Sopenharmony_ci /* 63962306a36Sopenharmony_ci * Zero out the remaining part if this range straddles 64062306a36Sopenharmony_ci * i_size. 64162306a36Sopenharmony_ci * 64262306a36Sopenharmony_ci * Here we should only zero the range inside the bvec, 64362306a36Sopenharmony_ci * not touch anything else. 64462306a36Sopenharmony_ci * 64562306a36Sopenharmony_ci * NOTE: i_size is exclusive while end is inclusive. 64662306a36Sopenharmony_ci */ 64762306a36Sopenharmony_ci if (page->index == end_index && i_size <= end) { 64862306a36Sopenharmony_ci u32 zero_start = max(offset_in_page(i_size), 64962306a36Sopenharmony_ci offset_in_page(start)); 65062306a36Sopenharmony_ci 65162306a36Sopenharmony_ci zero_user_segment(page, zero_start, 65262306a36Sopenharmony_ci offset_in_page(end) + 1); 65362306a36Sopenharmony_ci } 65462306a36Sopenharmony_ci } 65562306a36Sopenharmony_ci 65662306a36Sopenharmony_ci /* Update page status and unlock. */ 65762306a36Sopenharmony_ci end_page_read(page, uptodate, start, len); 65862306a36Sopenharmony_ci endio_readpage_release_extent(&processed, BTRFS_I(inode), 65962306a36Sopenharmony_ci start, end, uptodate); 66062306a36Sopenharmony_ci 66162306a36Sopenharmony_ci ASSERT(bio_offset + len > bio_offset); 66262306a36Sopenharmony_ci bio_offset += len; 66362306a36Sopenharmony_ci 66462306a36Sopenharmony_ci } 66562306a36Sopenharmony_ci /* Release the last extent */ 66662306a36Sopenharmony_ci endio_readpage_release_extent(&processed, NULL, 0, 0, false); 66762306a36Sopenharmony_ci bio_put(bio); 66862306a36Sopenharmony_ci} 66962306a36Sopenharmony_ci 67062306a36Sopenharmony_ci/* 67162306a36Sopenharmony_ci * Populate every free slot in a provided array with pages. 67262306a36Sopenharmony_ci * 67362306a36Sopenharmony_ci * @nr_pages: number of pages to allocate 67462306a36Sopenharmony_ci * @page_array: the array to fill with pages; any existing non-null entries in 67562306a36Sopenharmony_ci * the array will be skipped 67662306a36Sopenharmony_ci * 67762306a36Sopenharmony_ci * Return: 0 if all pages were able to be allocated; 67862306a36Sopenharmony_ci * -ENOMEM otherwise, the partially allocated pages would be freed and 67962306a36Sopenharmony_ci * the array slots zeroed 68062306a36Sopenharmony_ci */ 68162306a36Sopenharmony_ciint btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array) 68262306a36Sopenharmony_ci{ 68362306a36Sopenharmony_ci unsigned int allocated; 68462306a36Sopenharmony_ci 68562306a36Sopenharmony_ci for (allocated = 0; allocated < nr_pages;) { 68662306a36Sopenharmony_ci unsigned int last = allocated; 68762306a36Sopenharmony_ci 68862306a36Sopenharmony_ci allocated = alloc_pages_bulk_array(GFP_NOFS, nr_pages, page_array); 68962306a36Sopenharmony_ci 69062306a36Sopenharmony_ci if (allocated == nr_pages) 69162306a36Sopenharmony_ci return 0; 69262306a36Sopenharmony_ci 69362306a36Sopenharmony_ci /* 69462306a36Sopenharmony_ci * During this iteration, no page could be allocated, even 69562306a36Sopenharmony_ci * though alloc_pages_bulk_array() falls back to alloc_page() 69662306a36Sopenharmony_ci * if it could not bulk-allocate. So we must be out of memory. 69762306a36Sopenharmony_ci */ 69862306a36Sopenharmony_ci if (allocated == last) { 69962306a36Sopenharmony_ci for (int i = 0; i < allocated; i++) { 70062306a36Sopenharmony_ci __free_page(page_array[i]); 70162306a36Sopenharmony_ci page_array[i] = NULL; 70262306a36Sopenharmony_ci } 70362306a36Sopenharmony_ci return -ENOMEM; 70462306a36Sopenharmony_ci } 70562306a36Sopenharmony_ci 70662306a36Sopenharmony_ci memalloc_retry_wait(GFP_NOFS); 70762306a36Sopenharmony_ci } 70862306a36Sopenharmony_ci return 0; 70962306a36Sopenharmony_ci} 71062306a36Sopenharmony_ci 71162306a36Sopenharmony_cistatic bool btrfs_bio_is_contig(struct btrfs_bio_ctrl *bio_ctrl, 71262306a36Sopenharmony_ci struct page *page, u64 disk_bytenr, 71362306a36Sopenharmony_ci unsigned int pg_offset) 71462306a36Sopenharmony_ci{ 71562306a36Sopenharmony_ci struct bio *bio = &bio_ctrl->bbio->bio; 71662306a36Sopenharmony_ci struct bio_vec *bvec = bio_last_bvec_all(bio); 71762306a36Sopenharmony_ci const sector_t sector = disk_bytenr >> SECTOR_SHIFT; 71862306a36Sopenharmony_ci 71962306a36Sopenharmony_ci if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) { 72062306a36Sopenharmony_ci /* 72162306a36Sopenharmony_ci * For compression, all IO should have its logical bytenr set 72262306a36Sopenharmony_ci * to the starting bytenr of the compressed extent. 72362306a36Sopenharmony_ci */ 72462306a36Sopenharmony_ci return bio->bi_iter.bi_sector == sector; 72562306a36Sopenharmony_ci } 72662306a36Sopenharmony_ci 72762306a36Sopenharmony_ci /* 72862306a36Sopenharmony_ci * The contig check requires the following conditions to be met: 72962306a36Sopenharmony_ci * 73062306a36Sopenharmony_ci * 1) The pages are belonging to the same inode 73162306a36Sopenharmony_ci * This is implied by the call chain. 73262306a36Sopenharmony_ci * 73362306a36Sopenharmony_ci * 2) The range has adjacent logical bytenr 73462306a36Sopenharmony_ci * 73562306a36Sopenharmony_ci * 3) The range has adjacent file offset 73662306a36Sopenharmony_ci * This is required for the usage of btrfs_bio->file_offset. 73762306a36Sopenharmony_ci */ 73862306a36Sopenharmony_ci return bio_end_sector(bio) == sector && 73962306a36Sopenharmony_ci page_offset(bvec->bv_page) + bvec->bv_offset + bvec->bv_len == 74062306a36Sopenharmony_ci page_offset(page) + pg_offset; 74162306a36Sopenharmony_ci} 74262306a36Sopenharmony_ci 74362306a36Sopenharmony_cistatic void alloc_new_bio(struct btrfs_inode *inode, 74462306a36Sopenharmony_ci struct btrfs_bio_ctrl *bio_ctrl, 74562306a36Sopenharmony_ci u64 disk_bytenr, u64 file_offset) 74662306a36Sopenharmony_ci{ 74762306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = inode->root->fs_info; 74862306a36Sopenharmony_ci struct btrfs_bio *bbio; 74962306a36Sopenharmony_ci 75062306a36Sopenharmony_ci bbio = btrfs_bio_alloc(BIO_MAX_VECS, bio_ctrl->opf, fs_info, 75162306a36Sopenharmony_ci bio_ctrl->end_io_func, NULL); 75262306a36Sopenharmony_ci bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; 75362306a36Sopenharmony_ci bbio->inode = inode; 75462306a36Sopenharmony_ci bbio->file_offset = file_offset; 75562306a36Sopenharmony_ci bio_ctrl->bbio = bbio; 75662306a36Sopenharmony_ci bio_ctrl->len_to_oe_boundary = U32_MAX; 75762306a36Sopenharmony_ci 75862306a36Sopenharmony_ci /* Limit data write bios to the ordered boundary. */ 75962306a36Sopenharmony_ci if (bio_ctrl->wbc) { 76062306a36Sopenharmony_ci struct btrfs_ordered_extent *ordered; 76162306a36Sopenharmony_ci 76262306a36Sopenharmony_ci ordered = btrfs_lookup_ordered_extent(inode, file_offset); 76362306a36Sopenharmony_ci if (ordered) { 76462306a36Sopenharmony_ci bio_ctrl->len_to_oe_boundary = min_t(u32, U32_MAX, 76562306a36Sopenharmony_ci ordered->file_offset + 76662306a36Sopenharmony_ci ordered->disk_num_bytes - file_offset); 76762306a36Sopenharmony_ci bbio->ordered = ordered; 76862306a36Sopenharmony_ci } 76962306a36Sopenharmony_ci 77062306a36Sopenharmony_ci /* 77162306a36Sopenharmony_ci * Pick the last added device to support cgroup writeback. For 77262306a36Sopenharmony_ci * multi-device file systems this means blk-cgroup policies have 77362306a36Sopenharmony_ci * to always be set on the last added/replaced device. 77462306a36Sopenharmony_ci * This is a bit odd but has been like that for a long time. 77562306a36Sopenharmony_ci */ 77662306a36Sopenharmony_ci bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev); 77762306a36Sopenharmony_ci wbc_init_bio(bio_ctrl->wbc, &bbio->bio); 77862306a36Sopenharmony_ci } 77962306a36Sopenharmony_ci} 78062306a36Sopenharmony_ci 78162306a36Sopenharmony_ci/* 78262306a36Sopenharmony_ci * @disk_bytenr: logical bytenr where the write will be 78362306a36Sopenharmony_ci * @page: page to add to the bio 78462306a36Sopenharmony_ci * @size: portion of page that we want to write to 78562306a36Sopenharmony_ci * @pg_offset: offset of the new bio or to check whether we are adding 78662306a36Sopenharmony_ci * a contiguous page to the previous one 78762306a36Sopenharmony_ci * 78862306a36Sopenharmony_ci * The will either add the page into the existing @bio_ctrl->bbio, or allocate a 78962306a36Sopenharmony_ci * new one in @bio_ctrl->bbio. 79062306a36Sopenharmony_ci * The mirror number for this IO should already be initizlied in 79162306a36Sopenharmony_ci * @bio_ctrl->mirror_num. 79262306a36Sopenharmony_ci */ 79362306a36Sopenharmony_cistatic void submit_extent_page(struct btrfs_bio_ctrl *bio_ctrl, 79462306a36Sopenharmony_ci u64 disk_bytenr, struct page *page, 79562306a36Sopenharmony_ci size_t size, unsigned long pg_offset) 79662306a36Sopenharmony_ci{ 79762306a36Sopenharmony_ci struct btrfs_inode *inode = BTRFS_I(page->mapping->host); 79862306a36Sopenharmony_ci 79962306a36Sopenharmony_ci ASSERT(pg_offset + size <= PAGE_SIZE); 80062306a36Sopenharmony_ci ASSERT(bio_ctrl->end_io_func); 80162306a36Sopenharmony_ci 80262306a36Sopenharmony_ci if (bio_ctrl->bbio && 80362306a36Sopenharmony_ci !btrfs_bio_is_contig(bio_ctrl, page, disk_bytenr, pg_offset)) 80462306a36Sopenharmony_ci submit_one_bio(bio_ctrl); 80562306a36Sopenharmony_ci 80662306a36Sopenharmony_ci do { 80762306a36Sopenharmony_ci u32 len = size; 80862306a36Sopenharmony_ci 80962306a36Sopenharmony_ci /* Allocate new bio if needed */ 81062306a36Sopenharmony_ci if (!bio_ctrl->bbio) { 81162306a36Sopenharmony_ci alloc_new_bio(inode, bio_ctrl, disk_bytenr, 81262306a36Sopenharmony_ci page_offset(page) + pg_offset); 81362306a36Sopenharmony_ci } 81462306a36Sopenharmony_ci 81562306a36Sopenharmony_ci /* Cap to the current ordered extent boundary if there is one. */ 81662306a36Sopenharmony_ci if (len > bio_ctrl->len_to_oe_boundary) { 81762306a36Sopenharmony_ci ASSERT(bio_ctrl->compress_type == BTRFS_COMPRESS_NONE); 81862306a36Sopenharmony_ci ASSERT(is_data_inode(&inode->vfs_inode)); 81962306a36Sopenharmony_ci len = bio_ctrl->len_to_oe_boundary; 82062306a36Sopenharmony_ci } 82162306a36Sopenharmony_ci 82262306a36Sopenharmony_ci if (bio_add_page(&bio_ctrl->bbio->bio, page, len, pg_offset) != len) { 82362306a36Sopenharmony_ci /* bio full: move on to a new one */ 82462306a36Sopenharmony_ci submit_one_bio(bio_ctrl); 82562306a36Sopenharmony_ci continue; 82662306a36Sopenharmony_ci } 82762306a36Sopenharmony_ci 82862306a36Sopenharmony_ci if (bio_ctrl->wbc) 82962306a36Sopenharmony_ci wbc_account_cgroup_owner(bio_ctrl->wbc, page, len); 83062306a36Sopenharmony_ci 83162306a36Sopenharmony_ci size -= len; 83262306a36Sopenharmony_ci pg_offset += len; 83362306a36Sopenharmony_ci disk_bytenr += len; 83462306a36Sopenharmony_ci 83562306a36Sopenharmony_ci /* 83662306a36Sopenharmony_ci * len_to_oe_boundary defaults to U32_MAX, which isn't page or 83762306a36Sopenharmony_ci * sector aligned. alloc_new_bio() then sets it to the end of 83862306a36Sopenharmony_ci * our ordered extent for writes into zoned devices. 83962306a36Sopenharmony_ci * 84062306a36Sopenharmony_ci * When len_to_oe_boundary is tracking an ordered extent, we 84162306a36Sopenharmony_ci * trust the ordered extent code to align things properly, and 84262306a36Sopenharmony_ci * the check above to cap our write to the ordered extent 84362306a36Sopenharmony_ci * boundary is correct. 84462306a36Sopenharmony_ci * 84562306a36Sopenharmony_ci * When len_to_oe_boundary is U32_MAX, the cap above would 84662306a36Sopenharmony_ci * result in a 4095 byte IO for the last page right before 84762306a36Sopenharmony_ci * we hit the bio limit of UINT_MAX. bio_add_page() has all 84862306a36Sopenharmony_ci * the checks required to make sure we don't overflow the bio, 84962306a36Sopenharmony_ci * and we should just ignore len_to_oe_boundary completely 85062306a36Sopenharmony_ci * unless we're using it to track an ordered extent. 85162306a36Sopenharmony_ci * 85262306a36Sopenharmony_ci * It's pretty hard to make a bio sized U32_MAX, but it can 85362306a36Sopenharmony_ci * happen when the page cache is able to feed us contiguous 85462306a36Sopenharmony_ci * pages for large extents. 85562306a36Sopenharmony_ci */ 85662306a36Sopenharmony_ci if (bio_ctrl->len_to_oe_boundary != U32_MAX) 85762306a36Sopenharmony_ci bio_ctrl->len_to_oe_boundary -= len; 85862306a36Sopenharmony_ci 85962306a36Sopenharmony_ci /* Ordered extent boundary: move on to a new bio. */ 86062306a36Sopenharmony_ci if (bio_ctrl->len_to_oe_boundary == 0) 86162306a36Sopenharmony_ci submit_one_bio(bio_ctrl); 86262306a36Sopenharmony_ci } while (size); 86362306a36Sopenharmony_ci} 86462306a36Sopenharmony_ci 86562306a36Sopenharmony_cistatic int attach_extent_buffer_page(struct extent_buffer *eb, 86662306a36Sopenharmony_ci struct page *page, 86762306a36Sopenharmony_ci struct btrfs_subpage *prealloc) 86862306a36Sopenharmony_ci{ 86962306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 87062306a36Sopenharmony_ci int ret = 0; 87162306a36Sopenharmony_ci 87262306a36Sopenharmony_ci /* 87362306a36Sopenharmony_ci * If the page is mapped to btree inode, we should hold the private 87462306a36Sopenharmony_ci * lock to prevent race. 87562306a36Sopenharmony_ci * For cloned or dummy extent buffers, their pages are not mapped and 87662306a36Sopenharmony_ci * will not race with any other ebs. 87762306a36Sopenharmony_ci */ 87862306a36Sopenharmony_ci if (page->mapping) 87962306a36Sopenharmony_ci lockdep_assert_held(&page->mapping->private_lock); 88062306a36Sopenharmony_ci 88162306a36Sopenharmony_ci if (fs_info->nodesize >= PAGE_SIZE) { 88262306a36Sopenharmony_ci if (!PagePrivate(page)) 88362306a36Sopenharmony_ci attach_page_private(page, eb); 88462306a36Sopenharmony_ci else 88562306a36Sopenharmony_ci WARN_ON(page->private != (unsigned long)eb); 88662306a36Sopenharmony_ci return 0; 88762306a36Sopenharmony_ci } 88862306a36Sopenharmony_ci 88962306a36Sopenharmony_ci /* Already mapped, just free prealloc */ 89062306a36Sopenharmony_ci if (PagePrivate(page)) { 89162306a36Sopenharmony_ci btrfs_free_subpage(prealloc); 89262306a36Sopenharmony_ci return 0; 89362306a36Sopenharmony_ci } 89462306a36Sopenharmony_ci 89562306a36Sopenharmony_ci if (prealloc) 89662306a36Sopenharmony_ci /* Has preallocated memory for subpage */ 89762306a36Sopenharmony_ci attach_page_private(page, prealloc); 89862306a36Sopenharmony_ci else 89962306a36Sopenharmony_ci /* Do new allocation to attach subpage */ 90062306a36Sopenharmony_ci ret = btrfs_attach_subpage(fs_info, page, 90162306a36Sopenharmony_ci BTRFS_SUBPAGE_METADATA); 90262306a36Sopenharmony_ci return ret; 90362306a36Sopenharmony_ci} 90462306a36Sopenharmony_ci 90562306a36Sopenharmony_ciint set_page_extent_mapped(struct page *page) 90662306a36Sopenharmony_ci{ 90762306a36Sopenharmony_ci struct btrfs_fs_info *fs_info; 90862306a36Sopenharmony_ci 90962306a36Sopenharmony_ci ASSERT(page->mapping); 91062306a36Sopenharmony_ci 91162306a36Sopenharmony_ci if (PagePrivate(page)) 91262306a36Sopenharmony_ci return 0; 91362306a36Sopenharmony_ci 91462306a36Sopenharmony_ci fs_info = btrfs_sb(page->mapping->host->i_sb); 91562306a36Sopenharmony_ci 91662306a36Sopenharmony_ci if (btrfs_is_subpage(fs_info, page)) 91762306a36Sopenharmony_ci return btrfs_attach_subpage(fs_info, page, BTRFS_SUBPAGE_DATA); 91862306a36Sopenharmony_ci 91962306a36Sopenharmony_ci attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE); 92062306a36Sopenharmony_ci return 0; 92162306a36Sopenharmony_ci} 92262306a36Sopenharmony_ci 92362306a36Sopenharmony_civoid clear_page_extent_mapped(struct page *page) 92462306a36Sopenharmony_ci{ 92562306a36Sopenharmony_ci struct btrfs_fs_info *fs_info; 92662306a36Sopenharmony_ci 92762306a36Sopenharmony_ci ASSERT(page->mapping); 92862306a36Sopenharmony_ci 92962306a36Sopenharmony_ci if (!PagePrivate(page)) 93062306a36Sopenharmony_ci return; 93162306a36Sopenharmony_ci 93262306a36Sopenharmony_ci fs_info = btrfs_sb(page->mapping->host->i_sb); 93362306a36Sopenharmony_ci if (btrfs_is_subpage(fs_info, page)) 93462306a36Sopenharmony_ci return btrfs_detach_subpage(fs_info, page); 93562306a36Sopenharmony_ci 93662306a36Sopenharmony_ci detach_page_private(page); 93762306a36Sopenharmony_ci} 93862306a36Sopenharmony_ci 93962306a36Sopenharmony_cistatic struct extent_map * 94062306a36Sopenharmony_ci__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, 94162306a36Sopenharmony_ci u64 start, u64 len, struct extent_map **em_cached) 94262306a36Sopenharmony_ci{ 94362306a36Sopenharmony_ci struct extent_map *em; 94462306a36Sopenharmony_ci 94562306a36Sopenharmony_ci if (em_cached && *em_cached) { 94662306a36Sopenharmony_ci em = *em_cached; 94762306a36Sopenharmony_ci if (extent_map_in_tree(em) && start >= em->start && 94862306a36Sopenharmony_ci start < extent_map_end(em)) { 94962306a36Sopenharmony_ci refcount_inc(&em->refs); 95062306a36Sopenharmony_ci return em; 95162306a36Sopenharmony_ci } 95262306a36Sopenharmony_ci 95362306a36Sopenharmony_ci free_extent_map(em); 95462306a36Sopenharmony_ci *em_cached = NULL; 95562306a36Sopenharmony_ci } 95662306a36Sopenharmony_ci 95762306a36Sopenharmony_ci em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, start, len); 95862306a36Sopenharmony_ci if (em_cached && !IS_ERR(em)) { 95962306a36Sopenharmony_ci BUG_ON(*em_cached); 96062306a36Sopenharmony_ci refcount_inc(&em->refs); 96162306a36Sopenharmony_ci *em_cached = em; 96262306a36Sopenharmony_ci } 96362306a36Sopenharmony_ci return em; 96462306a36Sopenharmony_ci} 96562306a36Sopenharmony_ci/* 96662306a36Sopenharmony_ci * basic readpage implementation. Locked extent state structs are inserted 96762306a36Sopenharmony_ci * into the tree that are removed when the IO is done (by the end_io 96862306a36Sopenharmony_ci * handlers) 96962306a36Sopenharmony_ci * XXX JDM: This needs looking at to ensure proper page locking 97062306a36Sopenharmony_ci * return 0 on success, otherwise return error 97162306a36Sopenharmony_ci */ 97262306a36Sopenharmony_cistatic int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, 97362306a36Sopenharmony_ci struct btrfs_bio_ctrl *bio_ctrl, u64 *prev_em_start) 97462306a36Sopenharmony_ci{ 97562306a36Sopenharmony_ci struct inode *inode = page->mapping->host; 97662306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 97762306a36Sopenharmony_ci u64 start = page_offset(page); 97862306a36Sopenharmony_ci const u64 end = start + PAGE_SIZE - 1; 97962306a36Sopenharmony_ci u64 cur = start; 98062306a36Sopenharmony_ci u64 extent_offset; 98162306a36Sopenharmony_ci u64 last_byte = i_size_read(inode); 98262306a36Sopenharmony_ci u64 block_start; 98362306a36Sopenharmony_ci struct extent_map *em; 98462306a36Sopenharmony_ci int ret = 0; 98562306a36Sopenharmony_ci size_t pg_offset = 0; 98662306a36Sopenharmony_ci size_t iosize; 98762306a36Sopenharmony_ci size_t blocksize = inode->i_sb->s_blocksize; 98862306a36Sopenharmony_ci struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; 98962306a36Sopenharmony_ci 99062306a36Sopenharmony_ci ret = set_page_extent_mapped(page); 99162306a36Sopenharmony_ci if (ret < 0) { 99262306a36Sopenharmony_ci unlock_extent(tree, start, end, NULL); 99362306a36Sopenharmony_ci unlock_page(page); 99462306a36Sopenharmony_ci return ret; 99562306a36Sopenharmony_ci } 99662306a36Sopenharmony_ci 99762306a36Sopenharmony_ci if (page->index == last_byte >> PAGE_SHIFT) { 99862306a36Sopenharmony_ci size_t zero_offset = offset_in_page(last_byte); 99962306a36Sopenharmony_ci 100062306a36Sopenharmony_ci if (zero_offset) { 100162306a36Sopenharmony_ci iosize = PAGE_SIZE - zero_offset; 100262306a36Sopenharmony_ci memzero_page(page, zero_offset, iosize); 100362306a36Sopenharmony_ci } 100462306a36Sopenharmony_ci } 100562306a36Sopenharmony_ci bio_ctrl->end_io_func = end_bio_extent_readpage; 100662306a36Sopenharmony_ci begin_page_read(fs_info, page); 100762306a36Sopenharmony_ci while (cur <= end) { 100862306a36Sopenharmony_ci enum btrfs_compression_type compress_type = BTRFS_COMPRESS_NONE; 100962306a36Sopenharmony_ci bool force_bio_submit = false; 101062306a36Sopenharmony_ci u64 disk_bytenr; 101162306a36Sopenharmony_ci 101262306a36Sopenharmony_ci ASSERT(IS_ALIGNED(cur, fs_info->sectorsize)); 101362306a36Sopenharmony_ci if (cur >= last_byte) { 101462306a36Sopenharmony_ci iosize = PAGE_SIZE - pg_offset; 101562306a36Sopenharmony_ci memzero_page(page, pg_offset, iosize); 101662306a36Sopenharmony_ci unlock_extent(tree, cur, cur + iosize - 1, NULL); 101762306a36Sopenharmony_ci end_page_read(page, true, cur, iosize); 101862306a36Sopenharmony_ci break; 101962306a36Sopenharmony_ci } 102062306a36Sopenharmony_ci em = __get_extent_map(inode, page, pg_offset, cur, 102162306a36Sopenharmony_ci end - cur + 1, em_cached); 102262306a36Sopenharmony_ci if (IS_ERR(em)) { 102362306a36Sopenharmony_ci unlock_extent(tree, cur, end, NULL); 102462306a36Sopenharmony_ci end_page_read(page, false, cur, end + 1 - cur); 102562306a36Sopenharmony_ci return PTR_ERR(em); 102662306a36Sopenharmony_ci } 102762306a36Sopenharmony_ci extent_offset = cur - em->start; 102862306a36Sopenharmony_ci BUG_ON(extent_map_end(em) <= cur); 102962306a36Sopenharmony_ci BUG_ON(end < cur); 103062306a36Sopenharmony_ci 103162306a36Sopenharmony_ci if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) 103262306a36Sopenharmony_ci compress_type = em->compress_type; 103362306a36Sopenharmony_ci 103462306a36Sopenharmony_ci iosize = min(extent_map_end(em) - cur, end - cur + 1); 103562306a36Sopenharmony_ci iosize = ALIGN(iosize, blocksize); 103662306a36Sopenharmony_ci if (compress_type != BTRFS_COMPRESS_NONE) 103762306a36Sopenharmony_ci disk_bytenr = em->block_start; 103862306a36Sopenharmony_ci else 103962306a36Sopenharmony_ci disk_bytenr = em->block_start + extent_offset; 104062306a36Sopenharmony_ci block_start = em->block_start; 104162306a36Sopenharmony_ci if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) 104262306a36Sopenharmony_ci block_start = EXTENT_MAP_HOLE; 104362306a36Sopenharmony_ci 104462306a36Sopenharmony_ci /* 104562306a36Sopenharmony_ci * If we have a file range that points to a compressed extent 104662306a36Sopenharmony_ci * and it's followed by a consecutive file range that points 104762306a36Sopenharmony_ci * to the same compressed extent (possibly with a different 104862306a36Sopenharmony_ci * offset and/or length, so it either points to the whole extent 104962306a36Sopenharmony_ci * or only part of it), we must make sure we do not submit a 105062306a36Sopenharmony_ci * single bio to populate the pages for the 2 ranges because 105162306a36Sopenharmony_ci * this makes the compressed extent read zero out the pages 105262306a36Sopenharmony_ci * belonging to the 2nd range. Imagine the following scenario: 105362306a36Sopenharmony_ci * 105462306a36Sopenharmony_ci * File layout 105562306a36Sopenharmony_ci * [0 - 8K] [8K - 24K] 105662306a36Sopenharmony_ci * | | 105762306a36Sopenharmony_ci * | | 105862306a36Sopenharmony_ci * points to extent X, points to extent X, 105962306a36Sopenharmony_ci * offset 4K, length of 8K offset 0, length 16K 106062306a36Sopenharmony_ci * 106162306a36Sopenharmony_ci * [extent X, compressed length = 4K uncompressed length = 16K] 106262306a36Sopenharmony_ci * 106362306a36Sopenharmony_ci * If the bio to read the compressed extent covers both ranges, 106462306a36Sopenharmony_ci * it will decompress extent X into the pages belonging to the 106562306a36Sopenharmony_ci * first range and then it will stop, zeroing out the remaining 106662306a36Sopenharmony_ci * pages that belong to the other range that points to extent X. 106762306a36Sopenharmony_ci * So here we make sure we submit 2 bios, one for the first 106862306a36Sopenharmony_ci * range and another one for the third range. Both will target 106962306a36Sopenharmony_ci * the same physical extent from disk, but we can't currently 107062306a36Sopenharmony_ci * make the compressed bio endio callback populate the pages 107162306a36Sopenharmony_ci * for both ranges because each compressed bio is tightly 107262306a36Sopenharmony_ci * coupled with a single extent map, and each range can have 107362306a36Sopenharmony_ci * an extent map with a different offset value relative to the 107462306a36Sopenharmony_ci * uncompressed data of our extent and different lengths. This 107562306a36Sopenharmony_ci * is a corner case so we prioritize correctness over 107662306a36Sopenharmony_ci * non-optimal behavior (submitting 2 bios for the same extent). 107762306a36Sopenharmony_ci */ 107862306a36Sopenharmony_ci if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && 107962306a36Sopenharmony_ci prev_em_start && *prev_em_start != (u64)-1 && 108062306a36Sopenharmony_ci *prev_em_start != em->start) 108162306a36Sopenharmony_ci force_bio_submit = true; 108262306a36Sopenharmony_ci 108362306a36Sopenharmony_ci if (prev_em_start) 108462306a36Sopenharmony_ci *prev_em_start = em->start; 108562306a36Sopenharmony_ci 108662306a36Sopenharmony_ci free_extent_map(em); 108762306a36Sopenharmony_ci em = NULL; 108862306a36Sopenharmony_ci 108962306a36Sopenharmony_ci /* we've found a hole, just zero and go on */ 109062306a36Sopenharmony_ci if (block_start == EXTENT_MAP_HOLE) { 109162306a36Sopenharmony_ci memzero_page(page, pg_offset, iosize); 109262306a36Sopenharmony_ci 109362306a36Sopenharmony_ci unlock_extent(tree, cur, cur + iosize - 1, NULL); 109462306a36Sopenharmony_ci end_page_read(page, true, cur, iosize); 109562306a36Sopenharmony_ci cur = cur + iosize; 109662306a36Sopenharmony_ci pg_offset += iosize; 109762306a36Sopenharmony_ci continue; 109862306a36Sopenharmony_ci } 109962306a36Sopenharmony_ci /* the get_extent function already copied into the page */ 110062306a36Sopenharmony_ci if (block_start == EXTENT_MAP_INLINE) { 110162306a36Sopenharmony_ci unlock_extent(tree, cur, cur + iosize - 1, NULL); 110262306a36Sopenharmony_ci end_page_read(page, true, cur, iosize); 110362306a36Sopenharmony_ci cur = cur + iosize; 110462306a36Sopenharmony_ci pg_offset += iosize; 110562306a36Sopenharmony_ci continue; 110662306a36Sopenharmony_ci } 110762306a36Sopenharmony_ci 110862306a36Sopenharmony_ci if (bio_ctrl->compress_type != compress_type) { 110962306a36Sopenharmony_ci submit_one_bio(bio_ctrl); 111062306a36Sopenharmony_ci bio_ctrl->compress_type = compress_type; 111162306a36Sopenharmony_ci } 111262306a36Sopenharmony_ci 111362306a36Sopenharmony_ci if (force_bio_submit) 111462306a36Sopenharmony_ci submit_one_bio(bio_ctrl); 111562306a36Sopenharmony_ci submit_extent_page(bio_ctrl, disk_bytenr, page, iosize, 111662306a36Sopenharmony_ci pg_offset); 111762306a36Sopenharmony_ci cur = cur + iosize; 111862306a36Sopenharmony_ci pg_offset += iosize; 111962306a36Sopenharmony_ci } 112062306a36Sopenharmony_ci 112162306a36Sopenharmony_ci return 0; 112262306a36Sopenharmony_ci} 112362306a36Sopenharmony_ci 112462306a36Sopenharmony_ciint btrfs_read_folio(struct file *file, struct folio *folio) 112562306a36Sopenharmony_ci{ 112662306a36Sopenharmony_ci struct page *page = &folio->page; 112762306a36Sopenharmony_ci struct btrfs_inode *inode = BTRFS_I(page->mapping->host); 112862306a36Sopenharmony_ci u64 start = page_offset(page); 112962306a36Sopenharmony_ci u64 end = start + PAGE_SIZE - 1; 113062306a36Sopenharmony_ci struct btrfs_bio_ctrl bio_ctrl = { .opf = REQ_OP_READ }; 113162306a36Sopenharmony_ci int ret; 113262306a36Sopenharmony_ci 113362306a36Sopenharmony_ci btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); 113462306a36Sopenharmony_ci 113562306a36Sopenharmony_ci ret = btrfs_do_readpage(page, NULL, &bio_ctrl, NULL); 113662306a36Sopenharmony_ci /* 113762306a36Sopenharmony_ci * If btrfs_do_readpage() failed we will want to submit the assembled 113862306a36Sopenharmony_ci * bio to do the cleanup. 113962306a36Sopenharmony_ci */ 114062306a36Sopenharmony_ci submit_one_bio(&bio_ctrl); 114162306a36Sopenharmony_ci return ret; 114262306a36Sopenharmony_ci} 114362306a36Sopenharmony_ci 114462306a36Sopenharmony_cistatic inline void contiguous_readpages(struct page *pages[], int nr_pages, 114562306a36Sopenharmony_ci u64 start, u64 end, 114662306a36Sopenharmony_ci struct extent_map **em_cached, 114762306a36Sopenharmony_ci struct btrfs_bio_ctrl *bio_ctrl, 114862306a36Sopenharmony_ci u64 *prev_em_start) 114962306a36Sopenharmony_ci{ 115062306a36Sopenharmony_ci struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host); 115162306a36Sopenharmony_ci int index; 115262306a36Sopenharmony_ci 115362306a36Sopenharmony_ci btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); 115462306a36Sopenharmony_ci 115562306a36Sopenharmony_ci for (index = 0; index < nr_pages; index++) { 115662306a36Sopenharmony_ci btrfs_do_readpage(pages[index], em_cached, bio_ctrl, 115762306a36Sopenharmony_ci prev_em_start); 115862306a36Sopenharmony_ci put_page(pages[index]); 115962306a36Sopenharmony_ci } 116062306a36Sopenharmony_ci} 116162306a36Sopenharmony_ci 116262306a36Sopenharmony_ci/* 116362306a36Sopenharmony_ci * helper for __extent_writepage, doing all of the delayed allocation setup. 116462306a36Sopenharmony_ci * 116562306a36Sopenharmony_ci * This returns 1 if btrfs_run_delalloc_range function did all the work required 116662306a36Sopenharmony_ci * to write the page (copy into inline extent). In this case the IO has 116762306a36Sopenharmony_ci * been started and the page is already unlocked. 116862306a36Sopenharmony_ci * 116962306a36Sopenharmony_ci * This returns 0 if all went well (page still locked) 117062306a36Sopenharmony_ci * This returns < 0 if there were errors (page still locked) 117162306a36Sopenharmony_ci */ 117262306a36Sopenharmony_cistatic noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode, 117362306a36Sopenharmony_ci struct page *page, struct writeback_control *wbc) 117462306a36Sopenharmony_ci{ 117562306a36Sopenharmony_ci const u64 page_start = page_offset(page); 117662306a36Sopenharmony_ci const u64 page_end = page_start + PAGE_SIZE - 1; 117762306a36Sopenharmony_ci u64 delalloc_start = page_start; 117862306a36Sopenharmony_ci u64 delalloc_end = page_end; 117962306a36Sopenharmony_ci u64 delalloc_to_write = 0; 118062306a36Sopenharmony_ci int ret = 0; 118162306a36Sopenharmony_ci 118262306a36Sopenharmony_ci while (delalloc_start < page_end) { 118362306a36Sopenharmony_ci delalloc_end = page_end; 118462306a36Sopenharmony_ci if (!find_lock_delalloc_range(&inode->vfs_inode, page, 118562306a36Sopenharmony_ci &delalloc_start, &delalloc_end)) { 118662306a36Sopenharmony_ci delalloc_start = delalloc_end + 1; 118762306a36Sopenharmony_ci continue; 118862306a36Sopenharmony_ci } 118962306a36Sopenharmony_ci 119062306a36Sopenharmony_ci ret = btrfs_run_delalloc_range(inode, page, delalloc_start, 119162306a36Sopenharmony_ci delalloc_end, wbc); 119262306a36Sopenharmony_ci if (ret < 0) 119362306a36Sopenharmony_ci return ret; 119462306a36Sopenharmony_ci 119562306a36Sopenharmony_ci delalloc_start = delalloc_end + 1; 119662306a36Sopenharmony_ci } 119762306a36Sopenharmony_ci 119862306a36Sopenharmony_ci /* 119962306a36Sopenharmony_ci * delalloc_end is already one less than the total length, so 120062306a36Sopenharmony_ci * we don't subtract one from PAGE_SIZE 120162306a36Sopenharmony_ci */ 120262306a36Sopenharmony_ci delalloc_to_write += 120362306a36Sopenharmony_ci DIV_ROUND_UP(delalloc_end + 1 - page_start, PAGE_SIZE); 120462306a36Sopenharmony_ci 120562306a36Sopenharmony_ci /* 120662306a36Sopenharmony_ci * If btrfs_run_dealloc_range() already started I/O and unlocked 120762306a36Sopenharmony_ci * the pages, we just need to account for them here. 120862306a36Sopenharmony_ci */ 120962306a36Sopenharmony_ci if (ret == 1) { 121062306a36Sopenharmony_ci wbc->nr_to_write -= delalloc_to_write; 121162306a36Sopenharmony_ci return 1; 121262306a36Sopenharmony_ci } 121362306a36Sopenharmony_ci 121462306a36Sopenharmony_ci if (wbc->nr_to_write < delalloc_to_write) { 121562306a36Sopenharmony_ci int thresh = 8192; 121662306a36Sopenharmony_ci 121762306a36Sopenharmony_ci if (delalloc_to_write < thresh * 2) 121862306a36Sopenharmony_ci thresh = delalloc_to_write; 121962306a36Sopenharmony_ci wbc->nr_to_write = min_t(u64, delalloc_to_write, 122062306a36Sopenharmony_ci thresh); 122162306a36Sopenharmony_ci } 122262306a36Sopenharmony_ci 122362306a36Sopenharmony_ci return 0; 122462306a36Sopenharmony_ci} 122562306a36Sopenharmony_ci 122662306a36Sopenharmony_ci/* 122762306a36Sopenharmony_ci * Find the first byte we need to write. 122862306a36Sopenharmony_ci * 122962306a36Sopenharmony_ci * For subpage, one page can contain several sectors, and 123062306a36Sopenharmony_ci * __extent_writepage_io() will just grab all extent maps in the page 123162306a36Sopenharmony_ci * range and try to submit all non-inline/non-compressed extents. 123262306a36Sopenharmony_ci * 123362306a36Sopenharmony_ci * This is a big problem for subpage, we shouldn't re-submit already written 123462306a36Sopenharmony_ci * data at all. 123562306a36Sopenharmony_ci * This function will lookup subpage dirty bit to find which range we really 123662306a36Sopenharmony_ci * need to submit. 123762306a36Sopenharmony_ci * 123862306a36Sopenharmony_ci * Return the next dirty range in [@start, @end). 123962306a36Sopenharmony_ci * If no dirty range is found, @start will be page_offset(page) + PAGE_SIZE. 124062306a36Sopenharmony_ci */ 124162306a36Sopenharmony_cistatic void find_next_dirty_byte(struct btrfs_fs_info *fs_info, 124262306a36Sopenharmony_ci struct page *page, u64 *start, u64 *end) 124362306a36Sopenharmony_ci{ 124462306a36Sopenharmony_ci struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private; 124562306a36Sopenharmony_ci struct btrfs_subpage_info *spi = fs_info->subpage_info; 124662306a36Sopenharmony_ci u64 orig_start = *start; 124762306a36Sopenharmony_ci /* Declare as unsigned long so we can use bitmap ops */ 124862306a36Sopenharmony_ci unsigned long flags; 124962306a36Sopenharmony_ci int range_start_bit; 125062306a36Sopenharmony_ci int range_end_bit; 125162306a36Sopenharmony_ci 125262306a36Sopenharmony_ci /* 125362306a36Sopenharmony_ci * For regular sector size == page size case, since one page only 125462306a36Sopenharmony_ci * contains one sector, we return the page offset directly. 125562306a36Sopenharmony_ci */ 125662306a36Sopenharmony_ci if (!btrfs_is_subpage(fs_info, page)) { 125762306a36Sopenharmony_ci *start = page_offset(page); 125862306a36Sopenharmony_ci *end = page_offset(page) + PAGE_SIZE; 125962306a36Sopenharmony_ci return; 126062306a36Sopenharmony_ci } 126162306a36Sopenharmony_ci 126262306a36Sopenharmony_ci range_start_bit = spi->dirty_offset + 126362306a36Sopenharmony_ci (offset_in_page(orig_start) >> fs_info->sectorsize_bits); 126462306a36Sopenharmony_ci 126562306a36Sopenharmony_ci /* We should have the page locked, but just in case */ 126662306a36Sopenharmony_ci spin_lock_irqsave(&subpage->lock, flags); 126762306a36Sopenharmony_ci bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit, 126862306a36Sopenharmony_ci spi->dirty_offset + spi->bitmap_nr_bits); 126962306a36Sopenharmony_ci spin_unlock_irqrestore(&subpage->lock, flags); 127062306a36Sopenharmony_ci 127162306a36Sopenharmony_ci range_start_bit -= spi->dirty_offset; 127262306a36Sopenharmony_ci range_end_bit -= spi->dirty_offset; 127362306a36Sopenharmony_ci 127462306a36Sopenharmony_ci *start = page_offset(page) + range_start_bit * fs_info->sectorsize; 127562306a36Sopenharmony_ci *end = page_offset(page) + range_end_bit * fs_info->sectorsize; 127662306a36Sopenharmony_ci} 127762306a36Sopenharmony_ci 127862306a36Sopenharmony_ci/* 127962306a36Sopenharmony_ci * helper for __extent_writepage. This calls the writepage start hooks, 128062306a36Sopenharmony_ci * and does the loop to map the page into extents and bios. 128162306a36Sopenharmony_ci * 128262306a36Sopenharmony_ci * We return 1 if the IO is started and the page is unlocked, 128362306a36Sopenharmony_ci * 0 if all went well (page still locked) 128462306a36Sopenharmony_ci * < 0 if there were errors (page still locked) 128562306a36Sopenharmony_ci */ 128662306a36Sopenharmony_cistatic noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, 128762306a36Sopenharmony_ci struct page *page, 128862306a36Sopenharmony_ci struct btrfs_bio_ctrl *bio_ctrl, 128962306a36Sopenharmony_ci loff_t i_size, 129062306a36Sopenharmony_ci int *nr_ret) 129162306a36Sopenharmony_ci{ 129262306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = inode->root->fs_info; 129362306a36Sopenharmony_ci u64 cur = page_offset(page); 129462306a36Sopenharmony_ci u64 end = cur + PAGE_SIZE - 1; 129562306a36Sopenharmony_ci u64 extent_offset; 129662306a36Sopenharmony_ci u64 block_start; 129762306a36Sopenharmony_ci struct extent_map *em; 129862306a36Sopenharmony_ci int ret = 0; 129962306a36Sopenharmony_ci int nr = 0; 130062306a36Sopenharmony_ci 130162306a36Sopenharmony_ci ret = btrfs_writepage_cow_fixup(page); 130262306a36Sopenharmony_ci if (ret) { 130362306a36Sopenharmony_ci /* Fixup worker will requeue */ 130462306a36Sopenharmony_ci redirty_page_for_writepage(bio_ctrl->wbc, page); 130562306a36Sopenharmony_ci unlock_page(page); 130662306a36Sopenharmony_ci return 1; 130762306a36Sopenharmony_ci } 130862306a36Sopenharmony_ci 130962306a36Sopenharmony_ci bio_ctrl->end_io_func = end_bio_extent_writepage; 131062306a36Sopenharmony_ci while (cur <= end) { 131162306a36Sopenharmony_ci u32 len = end - cur + 1; 131262306a36Sopenharmony_ci u64 disk_bytenr; 131362306a36Sopenharmony_ci u64 em_end; 131462306a36Sopenharmony_ci u64 dirty_range_start = cur; 131562306a36Sopenharmony_ci u64 dirty_range_end; 131662306a36Sopenharmony_ci u32 iosize; 131762306a36Sopenharmony_ci 131862306a36Sopenharmony_ci if (cur >= i_size) { 131962306a36Sopenharmony_ci btrfs_mark_ordered_io_finished(inode, page, cur, len, 132062306a36Sopenharmony_ci true); 132162306a36Sopenharmony_ci /* 132262306a36Sopenharmony_ci * This range is beyond i_size, thus we don't need to 132362306a36Sopenharmony_ci * bother writing back. 132462306a36Sopenharmony_ci * But we still need to clear the dirty subpage bit, or 132562306a36Sopenharmony_ci * the next time the page gets dirtied, we will try to 132662306a36Sopenharmony_ci * writeback the sectors with subpage dirty bits, 132762306a36Sopenharmony_ci * causing writeback without ordered extent. 132862306a36Sopenharmony_ci */ 132962306a36Sopenharmony_ci btrfs_page_clear_dirty(fs_info, page, cur, len); 133062306a36Sopenharmony_ci break; 133162306a36Sopenharmony_ci } 133262306a36Sopenharmony_ci 133362306a36Sopenharmony_ci find_next_dirty_byte(fs_info, page, &dirty_range_start, 133462306a36Sopenharmony_ci &dirty_range_end); 133562306a36Sopenharmony_ci if (cur < dirty_range_start) { 133662306a36Sopenharmony_ci cur = dirty_range_start; 133762306a36Sopenharmony_ci continue; 133862306a36Sopenharmony_ci } 133962306a36Sopenharmony_ci 134062306a36Sopenharmony_ci em = btrfs_get_extent(inode, NULL, 0, cur, len); 134162306a36Sopenharmony_ci if (IS_ERR(em)) { 134262306a36Sopenharmony_ci ret = PTR_ERR_OR_ZERO(em); 134362306a36Sopenharmony_ci goto out_error; 134462306a36Sopenharmony_ci } 134562306a36Sopenharmony_ci 134662306a36Sopenharmony_ci extent_offset = cur - em->start; 134762306a36Sopenharmony_ci em_end = extent_map_end(em); 134862306a36Sopenharmony_ci ASSERT(cur <= em_end); 134962306a36Sopenharmony_ci ASSERT(cur < end); 135062306a36Sopenharmony_ci ASSERT(IS_ALIGNED(em->start, fs_info->sectorsize)); 135162306a36Sopenharmony_ci ASSERT(IS_ALIGNED(em->len, fs_info->sectorsize)); 135262306a36Sopenharmony_ci 135362306a36Sopenharmony_ci block_start = em->block_start; 135462306a36Sopenharmony_ci disk_bytenr = em->block_start + extent_offset; 135562306a36Sopenharmony_ci 135662306a36Sopenharmony_ci ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)); 135762306a36Sopenharmony_ci ASSERT(block_start != EXTENT_MAP_HOLE); 135862306a36Sopenharmony_ci ASSERT(block_start != EXTENT_MAP_INLINE); 135962306a36Sopenharmony_ci 136062306a36Sopenharmony_ci /* 136162306a36Sopenharmony_ci * Note that em_end from extent_map_end() and dirty_range_end from 136262306a36Sopenharmony_ci * find_next_dirty_byte() are all exclusive 136362306a36Sopenharmony_ci */ 136462306a36Sopenharmony_ci iosize = min(min(em_end, end + 1), dirty_range_end) - cur; 136562306a36Sopenharmony_ci free_extent_map(em); 136662306a36Sopenharmony_ci em = NULL; 136762306a36Sopenharmony_ci 136862306a36Sopenharmony_ci btrfs_set_range_writeback(inode, cur, cur + iosize - 1); 136962306a36Sopenharmony_ci if (!PageWriteback(page)) { 137062306a36Sopenharmony_ci btrfs_err(inode->root->fs_info, 137162306a36Sopenharmony_ci "page %lu not writeback, cur %llu end %llu", 137262306a36Sopenharmony_ci page->index, cur, end); 137362306a36Sopenharmony_ci } 137462306a36Sopenharmony_ci 137562306a36Sopenharmony_ci /* 137662306a36Sopenharmony_ci * Although the PageDirty bit is cleared before entering this 137762306a36Sopenharmony_ci * function, subpage dirty bit is not cleared. 137862306a36Sopenharmony_ci * So clear subpage dirty bit here so next time we won't submit 137962306a36Sopenharmony_ci * page for range already written to disk. 138062306a36Sopenharmony_ci */ 138162306a36Sopenharmony_ci btrfs_page_clear_dirty(fs_info, page, cur, iosize); 138262306a36Sopenharmony_ci 138362306a36Sopenharmony_ci submit_extent_page(bio_ctrl, disk_bytenr, page, iosize, 138462306a36Sopenharmony_ci cur - page_offset(page)); 138562306a36Sopenharmony_ci cur += iosize; 138662306a36Sopenharmony_ci nr++; 138762306a36Sopenharmony_ci } 138862306a36Sopenharmony_ci 138962306a36Sopenharmony_ci btrfs_page_assert_not_dirty(fs_info, page); 139062306a36Sopenharmony_ci *nr_ret = nr; 139162306a36Sopenharmony_ci return 0; 139262306a36Sopenharmony_ci 139362306a36Sopenharmony_ciout_error: 139462306a36Sopenharmony_ci /* 139562306a36Sopenharmony_ci * If we finish without problem, we should not only clear page dirty, 139662306a36Sopenharmony_ci * but also empty subpage dirty bits 139762306a36Sopenharmony_ci */ 139862306a36Sopenharmony_ci *nr_ret = nr; 139962306a36Sopenharmony_ci return ret; 140062306a36Sopenharmony_ci} 140162306a36Sopenharmony_ci 140262306a36Sopenharmony_ci/* 140362306a36Sopenharmony_ci * the writepage semantics are similar to regular writepage. extent 140462306a36Sopenharmony_ci * records are inserted to lock ranges in the tree, and as dirty areas 140562306a36Sopenharmony_ci * are found, they are marked writeback. Then the lock bits are removed 140662306a36Sopenharmony_ci * and the end_io handler clears the writeback ranges 140762306a36Sopenharmony_ci * 140862306a36Sopenharmony_ci * Return 0 if everything goes well. 140962306a36Sopenharmony_ci * Return <0 for error. 141062306a36Sopenharmony_ci */ 141162306a36Sopenharmony_cistatic int __extent_writepage(struct page *page, struct btrfs_bio_ctrl *bio_ctrl) 141262306a36Sopenharmony_ci{ 141362306a36Sopenharmony_ci struct folio *folio = page_folio(page); 141462306a36Sopenharmony_ci struct inode *inode = page->mapping->host; 141562306a36Sopenharmony_ci const u64 page_start = page_offset(page); 141662306a36Sopenharmony_ci int ret; 141762306a36Sopenharmony_ci int nr = 0; 141862306a36Sopenharmony_ci size_t pg_offset; 141962306a36Sopenharmony_ci loff_t i_size = i_size_read(inode); 142062306a36Sopenharmony_ci unsigned long end_index = i_size >> PAGE_SHIFT; 142162306a36Sopenharmony_ci 142262306a36Sopenharmony_ci trace___extent_writepage(page, inode, bio_ctrl->wbc); 142362306a36Sopenharmony_ci 142462306a36Sopenharmony_ci WARN_ON(!PageLocked(page)); 142562306a36Sopenharmony_ci 142662306a36Sopenharmony_ci pg_offset = offset_in_page(i_size); 142762306a36Sopenharmony_ci if (page->index > end_index || 142862306a36Sopenharmony_ci (page->index == end_index && !pg_offset)) { 142962306a36Sopenharmony_ci folio_invalidate(folio, 0, folio_size(folio)); 143062306a36Sopenharmony_ci folio_unlock(folio); 143162306a36Sopenharmony_ci return 0; 143262306a36Sopenharmony_ci } 143362306a36Sopenharmony_ci 143462306a36Sopenharmony_ci if (page->index == end_index) 143562306a36Sopenharmony_ci memzero_page(page, pg_offset, PAGE_SIZE - pg_offset); 143662306a36Sopenharmony_ci 143762306a36Sopenharmony_ci ret = set_page_extent_mapped(page); 143862306a36Sopenharmony_ci if (ret < 0) 143962306a36Sopenharmony_ci goto done; 144062306a36Sopenharmony_ci 144162306a36Sopenharmony_ci ret = writepage_delalloc(BTRFS_I(inode), page, bio_ctrl->wbc); 144262306a36Sopenharmony_ci if (ret == 1) 144362306a36Sopenharmony_ci return 0; 144462306a36Sopenharmony_ci if (ret) 144562306a36Sopenharmony_ci goto done; 144662306a36Sopenharmony_ci 144762306a36Sopenharmony_ci ret = __extent_writepage_io(BTRFS_I(inode), page, bio_ctrl, i_size, &nr); 144862306a36Sopenharmony_ci if (ret == 1) 144962306a36Sopenharmony_ci return 0; 145062306a36Sopenharmony_ci 145162306a36Sopenharmony_ci bio_ctrl->wbc->nr_to_write--; 145262306a36Sopenharmony_ci 145362306a36Sopenharmony_cidone: 145462306a36Sopenharmony_ci if (nr == 0) { 145562306a36Sopenharmony_ci /* make sure the mapping tag for page dirty gets cleared */ 145662306a36Sopenharmony_ci set_page_writeback(page); 145762306a36Sopenharmony_ci end_page_writeback(page); 145862306a36Sopenharmony_ci } 145962306a36Sopenharmony_ci if (ret) { 146062306a36Sopenharmony_ci btrfs_mark_ordered_io_finished(BTRFS_I(inode), page, page_start, 146162306a36Sopenharmony_ci PAGE_SIZE, !ret); 146262306a36Sopenharmony_ci mapping_set_error(page->mapping, ret); 146362306a36Sopenharmony_ci } 146462306a36Sopenharmony_ci unlock_page(page); 146562306a36Sopenharmony_ci ASSERT(ret <= 0); 146662306a36Sopenharmony_ci return ret; 146762306a36Sopenharmony_ci} 146862306a36Sopenharmony_ci 146962306a36Sopenharmony_civoid wait_on_extent_buffer_writeback(struct extent_buffer *eb) 147062306a36Sopenharmony_ci{ 147162306a36Sopenharmony_ci wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, 147262306a36Sopenharmony_ci TASK_UNINTERRUPTIBLE); 147362306a36Sopenharmony_ci} 147462306a36Sopenharmony_ci 147562306a36Sopenharmony_ci/* 147662306a36Sopenharmony_ci * Lock extent buffer status and pages for writeback. 147762306a36Sopenharmony_ci * 147862306a36Sopenharmony_ci * Return %false if the extent buffer doesn't need to be submitted (e.g. the 147962306a36Sopenharmony_ci * extent buffer is not dirty) 148062306a36Sopenharmony_ci * Return %true is the extent buffer is submitted to bio. 148162306a36Sopenharmony_ci */ 148262306a36Sopenharmony_cistatic noinline_for_stack bool lock_extent_buffer_for_io(struct extent_buffer *eb, 148362306a36Sopenharmony_ci struct writeback_control *wbc) 148462306a36Sopenharmony_ci{ 148562306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 148662306a36Sopenharmony_ci bool ret = false; 148762306a36Sopenharmony_ci 148862306a36Sopenharmony_ci btrfs_tree_lock(eb); 148962306a36Sopenharmony_ci while (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { 149062306a36Sopenharmony_ci btrfs_tree_unlock(eb); 149162306a36Sopenharmony_ci if (wbc->sync_mode != WB_SYNC_ALL) 149262306a36Sopenharmony_ci return false; 149362306a36Sopenharmony_ci wait_on_extent_buffer_writeback(eb); 149462306a36Sopenharmony_ci btrfs_tree_lock(eb); 149562306a36Sopenharmony_ci } 149662306a36Sopenharmony_ci 149762306a36Sopenharmony_ci /* 149862306a36Sopenharmony_ci * We need to do this to prevent races in people who check if the eb is 149962306a36Sopenharmony_ci * under IO since we can end up having no IO bits set for a short period 150062306a36Sopenharmony_ci * of time. 150162306a36Sopenharmony_ci */ 150262306a36Sopenharmony_ci spin_lock(&eb->refs_lock); 150362306a36Sopenharmony_ci if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { 150462306a36Sopenharmony_ci set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); 150562306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 150662306a36Sopenharmony_ci btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); 150762306a36Sopenharmony_ci percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 150862306a36Sopenharmony_ci -eb->len, 150962306a36Sopenharmony_ci fs_info->dirty_metadata_batch); 151062306a36Sopenharmony_ci ret = true; 151162306a36Sopenharmony_ci } else { 151262306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 151362306a36Sopenharmony_ci } 151462306a36Sopenharmony_ci btrfs_tree_unlock(eb); 151562306a36Sopenharmony_ci return ret; 151662306a36Sopenharmony_ci} 151762306a36Sopenharmony_ci 151862306a36Sopenharmony_cistatic void set_btree_ioerr(struct extent_buffer *eb) 151962306a36Sopenharmony_ci{ 152062306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 152162306a36Sopenharmony_ci 152262306a36Sopenharmony_ci set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); 152362306a36Sopenharmony_ci 152462306a36Sopenharmony_ci /* 152562306a36Sopenharmony_ci * A read may stumble upon this buffer later, make sure that it gets an 152662306a36Sopenharmony_ci * error and knows there was an error. 152762306a36Sopenharmony_ci */ 152862306a36Sopenharmony_ci clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 152962306a36Sopenharmony_ci 153062306a36Sopenharmony_ci /* 153162306a36Sopenharmony_ci * We need to set the mapping with the io error as well because a write 153262306a36Sopenharmony_ci * error will flip the file system readonly, and then syncfs() will 153362306a36Sopenharmony_ci * return a 0 because we are readonly if we don't modify the err seq for 153462306a36Sopenharmony_ci * the superblock. 153562306a36Sopenharmony_ci */ 153662306a36Sopenharmony_ci mapping_set_error(eb->fs_info->btree_inode->i_mapping, -EIO); 153762306a36Sopenharmony_ci 153862306a36Sopenharmony_ci /* 153962306a36Sopenharmony_ci * If writeback for a btree extent that doesn't belong to a log tree 154062306a36Sopenharmony_ci * failed, increment the counter transaction->eb_write_errors. 154162306a36Sopenharmony_ci * We do this because while the transaction is running and before it's 154262306a36Sopenharmony_ci * committing (when we call filemap_fdata[write|wait]_range against 154362306a36Sopenharmony_ci * the btree inode), we might have 154462306a36Sopenharmony_ci * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it 154562306a36Sopenharmony_ci * returns an error or an error happens during writeback, when we're 154662306a36Sopenharmony_ci * committing the transaction we wouldn't know about it, since the pages 154762306a36Sopenharmony_ci * can be no longer dirty nor marked anymore for writeback (if a 154862306a36Sopenharmony_ci * subsequent modification to the extent buffer didn't happen before the 154962306a36Sopenharmony_ci * transaction commit), which makes filemap_fdata[write|wait]_range not 155062306a36Sopenharmony_ci * able to find the pages tagged with SetPageError at transaction 155162306a36Sopenharmony_ci * commit time. So if this happens we must abort the transaction, 155262306a36Sopenharmony_ci * otherwise we commit a super block with btree roots that point to 155362306a36Sopenharmony_ci * btree nodes/leafs whose content on disk is invalid - either garbage 155462306a36Sopenharmony_ci * or the content of some node/leaf from a past generation that got 155562306a36Sopenharmony_ci * cowed or deleted and is no longer valid. 155662306a36Sopenharmony_ci * 155762306a36Sopenharmony_ci * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would 155862306a36Sopenharmony_ci * not be enough - we need to distinguish between log tree extents vs 155962306a36Sopenharmony_ci * non-log tree extents, and the next filemap_fdatawait_range() call 156062306a36Sopenharmony_ci * will catch and clear such errors in the mapping - and that call might 156162306a36Sopenharmony_ci * be from a log sync and not from a transaction commit. Also, checking 156262306a36Sopenharmony_ci * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is 156362306a36Sopenharmony_ci * not done and would not be reliable - the eb might have been released 156462306a36Sopenharmony_ci * from memory and reading it back again means that flag would not be 156562306a36Sopenharmony_ci * set (since it's a runtime flag, not persisted on disk). 156662306a36Sopenharmony_ci * 156762306a36Sopenharmony_ci * Using the flags below in the btree inode also makes us achieve the 156862306a36Sopenharmony_ci * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started 156962306a36Sopenharmony_ci * writeback for all dirty pages and before filemap_fdatawait_range() 157062306a36Sopenharmony_ci * is called, the writeback for all dirty pages had already finished 157162306a36Sopenharmony_ci * with errors - because we were not using AS_EIO/AS_ENOSPC, 157262306a36Sopenharmony_ci * filemap_fdatawait_range() would return success, as it could not know 157362306a36Sopenharmony_ci * that writeback errors happened (the pages were no longer tagged for 157462306a36Sopenharmony_ci * writeback). 157562306a36Sopenharmony_ci */ 157662306a36Sopenharmony_ci switch (eb->log_index) { 157762306a36Sopenharmony_ci case -1: 157862306a36Sopenharmony_ci set_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags); 157962306a36Sopenharmony_ci break; 158062306a36Sopenharmony_ci case 0: 158162306a36Sopenharmony_ci set_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); 158262306a36Sopenharmony_ci break; 158362306a36Sopenharmony_ci case 1: 158462306a36Sopenharmony_ci set_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); 158562306a36Sopenharmony_ci break; 158662306a36Sopenharmony_ci default: 158762306a36Sopenharmony_ci BUG(); /* unexpected, logic error */ 158862306a36Sopenharmony_ci } 158962306a36Sopenharmony_ci} 159062306a36Sopenharmony_ci 159162306a36Sopenharmony_ci/* 159262306a36Sopenharmony_ci * The endio specific version which won't touch any unsafe spinlock in endio 159362306a36Sopenharmony_ci * context. 159462306a36Sopenharmony_ci */ 159562306a36Sopenharmony_cistatic struct extent_buffer *find_extent_buffer_nolock( 159662306a36Sopenharmony_ci struct btrfs_fs_info *fs_info, u64 start) 159762306a36Sopenharmony_ci{ 159862306a36Sopenharmony_ci struct extent_buffer *eb; 159962306a36Sopenharmony_ci 160062306a36Sopenharmony_ci rcu_read_lock(); 160162306a36Sopenharmony_ci eb = radix_tree_lookup(&fs_info->buffer_radix, 160262306a36Sopenharmony_ci start >> fs_info->sectorsize_bits); 160362306a36Sopenharmony_ci if (eb && atomic_inc_not_zero(&eb->refs)) { 160462306a36Sopenharmony_ci rcu_read_unlock(); 160562306a36Sopenharmony_ci return eb; 160662306a36Sopenharmony_ci } 160762306a36Sopenharmony_ci rcu_read_unlock(); 160862306a36Sopenharmony_ci return NULL; 160962306a36Sopenharmony_ci} 161062306a36Sopenharmony_ci 161162306a36Sopenharmony_cistatic void extent_buffer_write_end_io(struct btrfs_bio *bbio) 161262306a36Sopenharmony_ci{ 161362306a36Sopenharmony_ci struct extent_buffer *eb = bbio->private; 161462306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 161562306a36Sopenharmony_ci bool uptodate = !bbio->bio.bi_status; 161662306a36Sopenharmony_ci struct bvec_iter_all iter_all; 161762306a36Sopenharmony_ci struct bio_vec *bvec; 161862306a36Sopenharmony_ci u32 bio_offset = 0; 161962306a36Sopenharmony_ci 162062306a36Sopenharmony_ci if (!uptodate) 162162306a36Sopenharmony_ci set_btree_ioerr(eb); 162262306a36Sopenharmony_ci 162362306a36Sopenharmony_ci bio_for_each_segment_all(bvec, &bbio->bio, iter_all) { 162462306a36Sopenharmony_ci u64 start = eb->start + bio_offset; 162562306a36Sopenharmony_ci struct page *page = bvec->bv_page; 162662306a36Sopenharmony_ci u32 len = bvec->bv_len; 162762306a36Sopenharmony_ci 162862306a36Sopenharmony_ci btrfs_page_clear_writeback(fs_info, page, start, len); 162962306a36Sopenharmony_ci bio_offset += len; 163062306a36Sopenharmony_ci } 163162306a36Sopenharmony_ci 163262306a36Sopenharmony_ci clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); 163362306a36Sopenharmony_ci smp_mb__after_atomic(); 163462306a36Sopenharmony_ci wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); 163562306a36Sopenharmony_ci 163662306a36Sopenharmony_ci bio_put(&bbio->bio); 163762306a36Sopenharmony_ci} 163862306a36Sopenharmony_ci 163962306a36Sopenharmony_cistatic void prepare_eb_write(struct extent_buffer *eb) 164062306a36Sopenharmony_ci{ 164162306a36Sopenharmony_ci u32 nritems; 164262306a36Sopenharmony_ci unsigned long start; 164362306a36Sopenharmony_ci unsigned long end; 164462306a36Sopenharmony_ci 164562306a36Sopenharmony_ci clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); 164662306a36Sopenharmony_ci 164762306a36Sopenharmony_ci /* Set btree blocks beyond nritems with 0 to avoid stale content */ 164862306a36Sopenharmony_ci nritems = btrfs_header_nritems(eb); 164962306a36Sopenharmony_ci if (btrfs_header_level(eb) > 0) { 165062306a36Sopenharmony_ci end = btrfs_node_key_ptr_offset(eb, nritems); 165162306a36Sopenharmony_ci memzero_extent_buffer(eb, end, eb->len - end); 165262306a36Sopenharmony_ci } else { 165362306a36Sopenharmony_ci /* 165462306a36Sopenharmony_ci * Leaf: 165562306a36Sopenharmony_ci * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 165662306a36Sopenharmony_ci */ 165762306a36Sopenharmony_ci start = btrfs_item_nr_offset(eb, nritems); 165862306a36Sopenharmony_ci end = btrfs_item_nr_offset(eb, 0); 165962306a36Sopenharmony_ci if (nritems == 0) 166062306a36Sopenharmony_ci end += BTRFS_LEAF_DATA_SIZE(eb->fs_info); 166162306a36Sopenharmony_ci else 166262306a36Sopenharmony_ci end += btrfs_item_offset(eb, nritems - 1); 166362306a36Sopenharmony_ci memzero_extent_buffer(eb, start, end - start); 166462306a36Sopenharmony_ci } 166562306a36Sopenharmony_ci} 166662306a36Sopenharmony_ci 166762306a36Sopenharmony_cistatic noinline_for_stack void write_one_eb(struct extent_buffer *eb, 166862306a36Sopenharmony_ci struct writeback_control *wbc) 166962306a36Sopenharmony_ci{ 167062306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 167162306a36Sopenharmony_ci struct btrfs_bio *bbio; 167262306a36Sopenharmony_ci 167362306a36Sopenharmony_ci prepare_eb_write(eb); 167462306a36Sopenharmony_ci 167562306a36Sopenharmony_ci bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES, 167662306a36Sopenharmony_ci REQ_OP_WRITE | REQ_META | wbc_to_write_flags(wbc), 167762306a36Sopenharmony_ci eb->fs_info, extent_buffer_write_end_io, eb); 167862306a36Sopenharmony_ci bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT; 167962306a36Sopenharmony_ci bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev); 168062306a36Sopenharmony_ci wbc_init_bio(wbc, &bbio->bio); 168162306a36Sopenharmony_ci bbio->inode = BTRFS_I(eb->fs_info->btree_inode); 168262306a36Sopenharmony_ci bbio->file_offset = eb->start; 168362306a36Sopenharmony_ci if (fs_info->nodesize < PAGE_SIZE) { 168462306a36Sopenharmony_ci struct page *p = eb->pages[0]; 168562306a36Sopenharmony_ci 168662306a36Sopenharmony_ci lock_page(p); 168762306a36Sopenharmony_ci btrfs_subpage_set_writeback(fs_info, p, eb->start, eb->len); 168862306a36Sopenharmony_ci if (btrfs_subpage_clear_and_test_dirty(fs_info, p, eb->start, 168962306a36Sopenharmony_ci eb->len)) { 169062306a36Sopenharmony_ci clear_page_dirty_for_io(p); 169162306a36Sopenharmony_ci wbc->nr_to_write--; 169262306a36Sopenharmony_ci } 169362306a36Sopenharmony_ci __bio_add_page(&bbio->bio, p, eb->len, eb->start - page_offset(p)); 169462306a36Sopenharmony_ci wbc_account_cgroup_owner(wbc, p, eb->len); 169562306a36Sopenharmony_ci unlock_page(p); 169662306a36Sopenharmony_ci } else { 169762306a36Sopenharmony_ci for (int i = 0; i < num_extent_pages(eb); i++) { 169862306a36Sopenharmony_ci struct page *p = eb->pages[i]; 169962306a36Sopenharmony_ci 170062306a36Sopenharmony_ci lock_page(p); 170162306a36Sopenharmony_ci clear_page_dirty_for_io(p); 170262306a36Sopenharmony_ci set_page_writeback(p); 170362306a36Sopenharmony_ci __bio_add_page(&bbio->bio, p, PAGE_SIZE, 0); 170462306a36Sopenharmony_ci wbc_account_cgroup_owner(wbc, p, PAGE_SIZE); 170562306a36Sopenharmony_ci wbc->nr_to_write--; 170662306a36Sopenharmony_ci unlock_page(p); 170762306a36Sopenharmony_ci } 170862306a36Sopenharmony_ci } 170962306a36Sopenharmony_ci btrfs_submit_bio(bbio, 0); 171062306a36Sopenharmony_ci} 171162306a36Sopenharmony_ci 171262306a36Sopenharmony_ci/* 171362306a36Sopenharmony_ci * Submit one subpage btree page. 171462306a36Sopenharmony_ci * 171562306a36Sopenharmony_ci * The main difference to submit_eb_page() is: 171662306a36Sopenharmony_ci * - Page locking 171762306a36Sopenharmony_ci * For subpage, we don't rely on page locking at all. 171862306a36Sopenharmony_ci * 171962306a36Sopenharmony_ci * - Flush write bio 172062306a36Sopenharmony_ci * We only flush bio if we may be unable to fit current extent buffers into 172162306a36Sopenharmony_ci * current bio. 172262306a36Sopenharmony_ci * 172362306a36Sopenharmony_ci * Return >=0 for the number of submitted extent buffers. 172462306a36Sopenharmony_ci * Return <0 for fatal error. 172562306a36Sopenharmony_ci */ 172662306a36Sopenharmony_cistatic int submit_eb_subpage(struct page *page, struct writeback_control *wbc) 172762306a36Sopenharmony_ci{ 172862306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 172962306a36Sopenharmony_ci int submitted = 0; 173062306a36Sopenharmony_ci u64 page_start = page_offset(page); 173162306a36Sopenharmony_ci int bit_start = 0; 173262306a36Sopenharmony_ci int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits; 173362306a36Sopenharmony_ci 173462306a36Sopenharmony_ci /* Lock and write each dirty extent buffers in the range */ 173562306a36Sopenharmony_ci while (bit_start < fs_info->subpage_info->bitmap_nr_bits) { 173662306a36Sopenharmony_ci struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private; 173762306a36Sopenharmony_ci struct extent_buffer *eb; 173862306a36Sopenharmony_ci unsigned long flags; 173962306a36Sopenharmony_ci u64 start; 174062306a36Sopenharmony_ci 174162306a36Sopenharmony_ci /* 174262306a36Sopenharmony_ci * Take private lock to ensure the subpage won't be detached 174362306a36Sopenharmony_ci * in the meantime. 174462306a36Sopenharmony_ci */ 174562306a36Sopenharmony_ci spin_lock(&page->mapping->private_lock); 174662306a36Sopenharmony_ci if (!PagePrivate(page)) { 174762306a36Sopenharmony_ci spin_unlock(&page->mapping->private_lock); 174862306a36Sopenharmony_ci break; 174962306a36Sopenharmony_ci } 175062306a36Sopenharmony_ci spin_lock_irqsave(&subpage->lock, flags); 175162306a36Sopenharmony_ci if (!test_bit(bit_start + fs_info->subpage_info->dirty_offset, 175262306a36Sopenharmony_ci subpage->bitmaps)) { 175362306a36Sopenharmony_ci spin_unlock_irqrestore(&subpage->lock, flags); 175462306a36Sopenharmony_ci spin_unlock(&page->mapping->private_lock); 175562306a36Sopenharmony_ci bit_start++; 175662306a36Sopenharmony_ci continue; 175762306a36Sopenharmony_ci } 175862306a36Sopenharmony_ci 175962306a36Sopenharmony_ci start = page_start + bit_start * fs_info->sectorsize; 176062306a36Sopenharmony_ci bit_start += sectors_per_node; 176162306a36Sopenharmony_ci 176262306a36Sopenharmony_ci /* 176362306a36Sopenharmony_ci * Here we just want to grab the eb without touching extra 176462306a36Sopenharmony_ci * spin locks, so call find_extent_buffer_nolock(). 176562306a36Sopenharmony_ci */ 176662306a36Sopenharmony_ci eb = find_extent_buffer_nolock(fs_info, start); 176762306a36Sopenharmony_ci spin_unlock_irqrestore(&subpage->lock, flags); 176862306a36Sopenharmony_ci spin_unlock(&page->mapping->private_lock); 176962306a36Sopenharmony_ci 177062306a36Sopenharmony_ci /* 177162306a36Sopenharmony_ci * The eb has already reached 0 refs thus find_extent_buffer() 177262306a36Sopenharmony_ci * doesn't return it. We don't need to write back such eb 177362306a36Sopenharmony_ci * anyway. 177462306a36Sopenharmony_ci */ 177562306a36Sopenharmony_ci if (!eb) 177662306a36Sopenharmony_ci continue; 177762306a36Sopenharmony_ci 177862306a36Sopenharmony_ci if (lock_extent_buffer_for_io(eb, wbc)) { 177962306a36Sopenharmony_ci write_one_eb(eb, wbc); 178062306a36Sopenharmony_ci submitted++; 178162306a36Sopenharmony_ci } 178262306a36Sopenharmony_ci free_extent_buffer(eb); 178362306a36Sopenharmony_ci } 178462306a36Sopenharmony_ci return submitted; 178562306a36Sopenharmony_ci} 178662306a36Sopenharmony_ci 178762306a36Sopenharmony_ci/* 178862306a36Sopenharmony_ci * Submit all page(s) of one extent buffer. 178962306a36Sopenharmony_ci * 179062306a36Sopenharmony_ci * @page: the page of one extent buffer 179162306a36Sopenharmony_ci * @eb_context: to determine if we need to submit this page, if current page 179262306a36Sopenharmony_ci * belongs to this eb, we don't need to submit 179362306a36Sopenharmony_ci * 179462306a36Sopenharmony_ci * The caller should pass each page in their bytenr order, and here we use 179562306a36Sopenharmony_ci * @eb_context to determine if we have submitted pages of one extent buffer. 179662306a36Sopenharmony_ci * 179762306a36Sopenharmony_ci * If we have, we just skip until we hit a new page that doesn't belong to 179862306a36Sopenharmony_ci * current @eb_context. 179962306a36Sopenharmony_ci * 180062306a36Sopenharmony_ci * If not, we submit all the page(s) of the extent buffer. 180162306a36Sopenharmony_ci * 180262306a36Sopenharmony_ci * Return >0 if we have submitted the extent buffer successfully. 180362306a36Sopenharmony_ci * Return 0 if we don't need to submit the page, as it's already submitted by 180462306a36Sopenharmony_ci * previous call. 180562306a36Sopenharmony_ci * Return <0 for fatal error. 180662306a36Sopenharmony_ci */ 180762306a36Sopenharmony_cistatic int submit_eb_page(struct page *page, struct btrfs_eb_write_context *ctx) 180862306a36Sopenharmony_ci{ 180962306a36Sopenharmony_ci struct writeback_control *wbc = ctx->wbc; 181062306a36Sopenharmony_ci struct address_space *mapping = page->mapping; 181162306a36Sopenharmony_ci struct extent_buffer *eb; 181262306a36Sopenharmony_ci int ret; 181362306a36Sopenharmony_ci 181462306a36Sopenharmony_ci if (!PagePrivate(page)) 181562306a36Sopenharmony_ci return 0; 181662306a36Sopenharmony_ci 181762306a36Sopenharmony_ci if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) 181862306a36Sopenharmony_ci return submit_eb_subpage(page, wbc); 181962306a36Sopenharmony_ci 182062306a36Sopenharmony_ci spin_lock(&mapping->private_lock); 182162306a36Sopenharmony_ci if (!PagePrivate(page)) { 182262306a36Sopenharmony_ci spin_unlock(&mapping->private_lock); 182362306a36Sopenharmony_ci return 0; 182462306a36Sopenharmony_ci } 182562306a36Sopenharmony_ci 182662306a36Sopenharmony_ci eb = (struct extent_buffer *)page->private; 182762306a36Sopenharmony_ci 182862306a36Sopenharmony_ci /* 182962306a36Sopenharmony_ci * Shouldn't happen and normally this would be a BUG_ON but no point 183062306a36Sopenharmony_ci * crashing the machine for something we can survive anyway. 183162306a36Sopenharmony_ci */ 183262306a36Sopenharmony_ci if (WARN_ON(!eb)) { 183362306a36Sopenharmony_ci spin_unlock(&mapping->private_lock); 183462306a36Sopenharmony_ci return 0; 183562306a36Sopenharmony_ci } 183662306a36Sopenharmony_ci 183762306a36Sopenharmony_ci if (eb == ctx->eb) { 183862306a36Sopenharmony_ci spin_unlock(&mapping->private_lock); 183962306a36Sopenharmony_ci return 0; 184062306a36Sopenharmony_ci } 184162306a36Sopenharmony_ci ret = atomic_inc_not_zero(&eb->refs); 184262306a36Sopenharmony_ci spin_unlock(&mapping->private_lock); 184362306a36Sopenharmony_ci if (!ret) 184462306a36Sopenharmony_ci return 0; 184562306a36Sopenharmony_ci 184662306a36Sopenharmony_ci ctx->eb = eb; 184762306a36Sopenharmony_ci 184862306a36Sopenharmony_ci ret = btrfs_check_meta_write_pointer(eb->fs_info, ctx); 184962306a36Sopenharmony_ci if (ret) { 185062306a36Sopenharmony_ci if (ret == -EBUSY) 185162306a36Sopenharmony_ci ret = 0; 185262306a36Sopenharmony_ci free_extent_buffer(eb); 185362306a36Sopenharmony_ci return ret; 185462306a36Sopenharmony_ci } 185562306a36Sopenharmony_ci 185662306a36Sopenharmony_ci if (!lock_extent_buffer_for_io(eb, wbc)) { 185762306a36Sopenharmony_ci free_extent_buffer(eb); 185862306a36Sopenharmony_ci return 0; 185962306a36Sopenharmony_ci } 186062306a36Sopenharmony_ci /* Implies write in zoned mode. */ 186162306a36Sopenharmony_ci if (ctx->zoned_bg) { 186262306a36Sopenharmony_ci /* Mark the last eb in the block group. */ 186362306a36Sopenharmony_ci btrfs_schedule_zone_finish_bg(ctx->zoned_bg, eb); 186462306a36Sopenharmony_ci ctx->zoned_bg->meta_write_pointer += eb->len; 186562306a36Sopenharmony_ci } 186662306a36Sopenharmony_ci write_one_eb(eb, wbc); 186762306a36Sopenharmony_ci free_extent_buffer(eb); 186862306a36Sopenharmony_ci return 1; 186962306a36Sopenharmony_ci} 187062306a36Sopenharmony_ci 187162306a36Sopenharmony_ciint btree_write_cache_pages(struct address_space *mapping, 187262306a36Sopenharmony_ci struct writeback_control *wbc) 187362306a36Sopenharmony_ci{ 187462306a36Sopenharmony_ci struct btrfs_eb_write_context ctx = { .wbc = wbc }; 187562306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; 187662306a36Sopenharmony_ci int ret = 0; 187762306a36Sopenharmony_ci int done = 0; 187862306a36Sopenharmony_ci int nr_to_write_done = 0; 187962306a36Sopenharmony_ci struct folio_batch fbatch; 188062306a36Sopenharmony_ci unsigned int nr_folios; 188162306a36Sopenharmony_ci pgoff_t index; 188262306a36Sopenharmony_ci pgoff_t end; /* Inclusive */ 188362306a36Sopenharmony_ci int scanned = 0; 188462306a36Sopenharmony_ci xa_mark_t tag; 188562306a36Sopenharmony_ci 188662306a36Sopenharmony_ci folio_batch_init(&fbatch); 188762306a36Sopenharmony_ci if (wbc->range_cyclic) { 188862306a36Sopenharmony_ci index = mapping->writeback_index; /* Start from prev offset */ 188962306a36Sopenharmony_ci end = -1; 189062306a36Sopenharmony_ci /* 189162306a36Sopenharmony_ci * Start from the beginning does not need to cycle over the 189262306a36Sopenharmony_ci * range, mark it as scanned. 189362306a36Sopenharmony_ci */ 189462306a36Sopenharmony_ci scanned = (index == 0); 189562306a36Sopenharmony_ci } else { 189662306a36Sopenharmony_ci index = wbc->range_start >> PAGE_SHIFT; 189762306a36Sopenharmony_ci end = wbc->range_end >> PAGE_SHIFT; 189862306a36Sopenharmony_ci scanned = 1; 189962306a36Sopenharmony_ci } 190062306a36Sopenharmony_ci if (wbc->sync_mode == WB_SYNC_ALL) 190162306a36Sopenharmony_ci tag = PAGECACHE_TAG_TOWRITE; 190262306a36Sopenharmony_ci else 190362306a36Sopenharmony_ci tag = PAGECACHE_TAG_DIRTY; 190462306a36Sopenharmony_ci btrfs_zoned_meta_io_lock(fs_info); 190562306a36Sopenharmony_ciretry: 190662306a36Sopenharmony_ci if (wbc->sync_mode == WB_SYNC_ALL) 190762306a36Sopenharmony_ci tag_pages_for_writeback(mapping, index, end); 190862306a36Sopenharmony_ci while (!done && !nr_to_write_done && (index <= end) && 190962306a36Sopenharmony_ci (nr_folios = filemap_get_folios_tag(mapping, &index, end, 191062306a36Sopenharmony_ci tag, &fbatch))) { 191162306a36Sopenharmony_ci unsigned i; 191262306a36Sopenharmony_ci 191362306a36Sopenharmony_ci for (i = 0; i < nr_folios; i++) { 191462306a36Sopenharmony_ci struct folio *folio = fbatch.folios[i]; 191562306a36Sopenharmony_ci 191662306a36Sopenharmony_ci ret = submit_eb_page(&folio->page, &ctx); 191762306a36Sopenharmony_ci if (ret == 0) 191862306a36Sopenharmony_ci continue; 191962306a36Sopenharmony_ci if (ret < 0) { 192062306a36Sopenharmony_ci done = 1; 192162306a36Sopenharmony_ci break; 192262306a36Sopenharmony_ci } 192362306a36Sopenharmony_ci 192462306a36Sopenharmony_ci /* 192562306a36Sopenharmony_ci * the filesystem may choose to bump up nr_to_write. 192662306a36Sopenharmony_ci * We have to make sure to honor the new nr_to_write 192762306a36Sopenharmony_ci * at any time 192862306a36Sopenharmony_ci */ 192962306a36Sopenharmony_ci nr_to_write_done = wbc->nr_to_write <= 0; 193062306a36Sopenharmony_ci } 193162306a36Sopenharmony_ci folio_batch_release(&fbatch); 193262306a36Sopenharmony_ci cond_resched(); 193362306a36Sopenharmony_ci } 193462306a36Sopenharmony_ci if (!scanned && !done) { 193562306a36Sopenharmony_ci /* 193662306a36Sopenharmony_ci * We hit the last page and there is more work to be done: wrap 193762306a36Sopenharmony_ci * back to the start of the file 193862306a36Sopenharmony_ci */ 193962306a36Sopenharmony_ci scanned = 1; 194062306a36Sopenharmony_ci index = 0; 194162306a36Sopenharmony_ci goto retry; 194262306a36Sopenharmony_ci } 194362306a36Sopenharmony_ci /* 194462306a36Sopenharmony_ci * If something went wrong, don't allow any metadata write bio to be 194562306a36Sopenharmony_ci * submitted. 194662306a36Sopenharmony_ci * 194762306a36Sopenharmony_ci * This would prevent use-after-free if we had dirty pages not 194862306a36Sopenharmony_ci * cleaned up, which can still happen by fuzzed images. 194962306a36Sopenharmony_ci * 195062306a36Sopenharmony_ci * - Bad extent tree 195162306a36Sopenharmony_ci * Allowing existing tree block to be allocated for other trees. 195262306a36Sopenharmony_ci * 195362306a36Sopenharmony_ci * - Log tree operations 195462306a36Sopenharmony_ci * Exiting tree blocks get allocated to log tree, bumps its 195562306a36Sopenharmony_ci * generation, then get cleaned in tree re-balance. 195662306a36Sopenharmony_ci * Such tree block will not be written back, since it's clean, 195762306a36Sopenharmony_ci * thus no WRITTEN flag set. 195862306a36Sopenharmony_ci * And after log writes back, this tree block is not traced by 195962306a36Sopenharmony_ci * any dirty extent_io_tree. 196062306a36Sopenharmony_ci * 196162306a36Sopenharmony_ci * - Offending tree block gets re-dirtied from its original owner 196262306a36Sopenharmony_ci * Since it has bumped generation, no WRITTEN flag, it can be 196362306a36Sopenharmony_ci * reused without COWing. This tree block will not be traced 196462306a36Sopenharmony_ci * by btrfs_transaction::dirty_pages. 196562306a36Sopenharmony_ci * 196662306a36Sopenharmony_ci * Now such dirty tree block will not be cleaned by any dirty 196762306a36Sopenharmony_ci * extent io tree. Thus we don't want to submit such wild eb 196862306a36Sopenharmony_ci * if the fs already has error. 196962306a36Sopenharmony_ci * 197062306a36Sopenharmony_ci * We can get ret > 0 from submit_extent_page() indicating how many ebs 197162306a36Sopenharmony_ci * were submitted. Reset it to 0 to avoid false alerts for the caller. 197262306a36Sopenharmony_ci */ 197362306a36Sopenharmony_ci if (ret > 0) 197462306a36Sopenharmony_ci ret = 0; 197562306a36Sopenharmony_ci if (!ret && BTRFS_FS_ERROR(fs_info)) 197662306a36Sopenharmony_ci ret = -EROFS; 197762306a36Sopenharmony_ci 197862306a36Sopenharmony_ci if (ctx.zoned_bg) 197962306a36Sopenharmony_ci btrfs_put_block_group(ctx.zoned_bg); 198062306a36Sopenharmony_ci btrfs_zoned_meta_io_unlock(fs_info); 198162306a36Sopenharmony_ci return ret; 198262306a36Sopenharmony_ci} 198362306a36Sopenharmony_ci 198462306a36Sopenharmony_ci/* 198562306a36Sopenharmony_ci * Walk the list of dirty pages of the given address space and write all of them. 198662306a36Sopenharmony_ci * 198762306a36Sopenharmony_ci * @mapping: address space structure to write 198862306a36Sopenharmony_ci * @wbc: subtract the number of written pages from *@wbc->nr_to_write 198962306a36Sopenharmony_ci * @bio_ctrl: holds context for the write, namely the bio 199062306a36Sopenharmony_ci * 199162306a36Sopenharmony_ci * If a page is already under I/O, write_cache_pages() skips it, even 199262306a36Sopenharmony_ci * if it's dirty. This is desirable behaviour for memory-cleaning writeback, 199362306a36Sopenharmony_ci * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() 199462306a36Sopenharmony_ci * and msync() need to guarantee that all the data which was dirty at the time 199562306a36Sopenharmony_ci * the call was made get new I/O started against them. If wbc->sync_mode is 199662306a36Sopenharmony_ci * WB_SYNC_ALL then we were called for data integrity and we must wait for 199762306a36Sopenharmony_ci * existing IO to complete. 199862306a36Sopenharmony_ci */ 199962306a36Sopenharmony_cistatic int extent_write_cache_pages(struct address_space *mapping, 200062306a36Sopenharmony_ci struct btrfs_bio_ctrl *bio_ctrl) 200162306a36Sopenharmony_ci{ 200262306a36Sopenharmony_ci struct writeback_control *wbc = bio_ctrl->wbc; 200362306a36Sopenharmony_ci struct inode *inode = mapping->host; 200462306a36Sopenharmony_ci int ret = 0; 200562306a36Sopenharmony_ci int done = 0; 200662306a36Sopenharmony_ci int nr_to_write_done = 0; 200762306a36Sopenharmony_ci struct folio_batch fbatch; 200862306a36Sopenharmony_ci unsigned int nr_folios; 200962306a36Sopenharmony_ci pgoff_t index; 201062306a36Sopenharmony_ci pgoff_t end; /* Inclusive */ 201162306a36Sopenharmony_ci pgoff_t done_index; 201262306a36Sopenharmony_ci int range_whole = 0; 201362306a36Sopenharmony_ci int scanned = 0; 201462306a36Sopenharmony_ci xa_mark_t tag; 201562306a36Sopenharmony_ci 201662306a36Sopenharmony_ci /* 201762306a36Sopenharmony_ci * We have to hold onto the inode so that ordered extents can do their 201862306a36Sopenharmony_ci * work when the IO finishes. The alternative to this is failing to add 201962306a36Sopenharmony_ci * an ordered extent if the igrab() fails there and that is a huge pain 202062306a36Sopenharmony_ci * to deal with, so instead just hold onto the inode throughout the 202162306a36Sopenharmony_ci * writepages operation. If it fails here we are freeing up the inode 202262306a36Sopenharmony_ci * anyway and we'd rather not waste our time writing out stuff that is 202362306a36Sopenharmony_ci * going to be truncated anyway. 202462306a36Sopenharmony_ci */ 202562306a36Sopenharmony_ci if (!igrab(inode)) 202662306a36Sopenharmony_ci return 0; 202762306a36Sopenharmony_ci 202862306a36Sopenharmony_ci folio_batch_init(&fbatch); 202962306a36Sopenharmony_ci if (wbc->range_cyclic) { 203062306a36Sopenharmony_ci index = mapping->writeback_index; /* Start from prev offset */ 203162306a36Sopenharmony_ci end = -1; 203262306a36Sopenharmony_ci /* 203362306a36Sopenharmony_ci * Start from the beginning does not need to cycle over the 203462306a36Sopenharmony_ci * range, mark it as scanned. 203562306a36Sopenharmony_ci */ 203662306a36Sopenharmony_ci scanned = (index == 0); 203762306a36Sopenharmony_ci } else { 203862306a36Sopenharmony_ci index = wbc->range_start >> PAGE_SHIFT; 203962306a36Sopenharmony_ci end = wbc->range_end >> PAGE_SHIFT; 204062306a36Sopenharmony_ci if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 204162306a36Sopenharmony_ci range_whole = 1; 204262306a36Sopenharmony_ci scanned = 1; 204362306a36Sopenharmony_ci } 204462306a36Sopenharmony_ci 204562306a36Sopenharmony_ci /* 204662306a36Sopenharmony_ci * We do the tagged writepage as long as the snapshot flush bit is set 204762306a36Sopenharmony_ci * and we are the first one who do the filemap_flush() on this inode. 204862306a36Sopenharmony_ci * 204962306a36Sopenharmony_ci * The nr_to_write == LONG_MAX is needed to make sure other flushers do 205062306a36Sopenharmony_ci * not race in and drop the bit. 205162306a36Sopenharmony_ci */ 205262306a36Sopenharmony_ci if (range_whole && wbc->nr_to_write == LONG_MAX && 205362306a36Sopenharmony_ci test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH, 205462306a36Sopenharmony_ci &BTRFS_I(inode)->runtime_flags)) 205562306a36Sopenharmony_ci wbc->tagged_writepages = 1; 205662306a36Sopenharmony_ci 205762306a36Sopenharmony_ci if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 205862306a36Sopenharmony_ci tag = PAGECACHE_TAG_TOWRITE; 205962306a36Sopenharmony_ci else 206062306a36Sopenharmony_ci tag = PAGECACHE_TAG_DIRTY; 206162306a36Sopenharmony_ciretry: 206262306a36Sopenharmony_ci if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 206362306a36Sopenharmony_ci tag_pages_for_writeback(mapping, index, end); 206462306a36Sopenharmony_ci done_index = index; 206562306a36Sopenharmony_ci while (!done && !nr_to_write_done && (index <= end) && 206662306a36Sopenharmony_ci (nr_folios = filemap_get_folios_tag(mapping, &index, 206762306a36Sopenharmony_ci end, tag, &fbatch))) { 206862306a36Sopenharmony_ci unsigned i; 206962306a36Sopenharmony_ci 207062306a36Sopenharmony_ci for (i = 0; i < nr_folios; i++) { 207162306a36Sopenharmony_ci struct folio *folio = fbatch.folios[i]; 207262306a36Sopenharmony_ci 207362306a36Sopenharmony_ci done_index = folio_next_index(folio); 207462306a36Sopenharmony_ci /* 207562306a36Sopenharmony_ci * At this point we hold neither the i_pages lock nor 207662306a36Sopenharmony_ci * the page lock: the page may be truncated or 207762306a36Sopenharmony_ci * invalidated (changing page->mapping to NULL), 207862306a36Sopenharmony_ci * or even swizzled back from swapper_space to 207962306a36Sopenharmony_ci * tmpfs file mapping 208062306a36Sopenharmony_ci */ 208162306a36Sopenharmony_ci if (!folio_trylock(folio)) { 208262306a36Sopenharmony_ci submit_write_bio(bio_ctrl, 0); 208362306a36Sopenharmony_ci folio_lock(folio); 208462306a36Sopenharmony_ci } 208562306a36Sopenharmony_ci 208662306a36Sopenharmony_ci if (unlikely(folio->mapping != mapping)) { 208762306a36Sopenharmony_ci folio_unlock(folio); 208862306a36Sopenharmony_ci continue; 208962306a36Sopenharmony_ci } 209062306a36Sopenharmony_ci 209162306a36Sopenharmony_ci if (!folio_test_dirty(folio)) { 209262306a36Sopenharmony_ci /* Someone wrote it for us. */ 209362306a36Sopenharmony_ci folio_unlock(folio); 209462306a36Sopenharmony_ci continue; 209562306a36Sopenharmony_ci } 209662306a36Sopenharmony_ci 209762306a36Sopenharmony_ci if (wbc->sync_mode != WB_SYNC_NONE) { 209862306a36Sopenharmony_ci if (folio_test_writeback(folio)) 209962306a36Sopenharmony_ci submit_write_bio(bio_ctrl, 0); 210062306a36Sopenharmony_ci folio_wait_writeback(folio); 210162306a36Sopenharmony_ci } 210262306a36Sopenharmony_ci 210362306a36Sopenharmony_ci if (folio_test_writeback(folio) || 210462306a36Sopenharmony_ci !folio_clear_dirty_for_io(folio)) { 210562306a36Sopenharmony_ci folio_unlock(folio); 210662306a36Sopenharmony_ci continue; 210762306a36Sopenharmony_ci } 210862306a36Sopenharmony_ci 210962306a36Sopenharmony_ci ret = __extent_writepage(&folio->page, bio_ctrl); 211062306a36Sopenharmony_ci if (ret < 0) { 211162306a36Sopenharmony_ci done = 1; 211262306a36Sopenharmony_ci break; 211362306a36Sopenharmony_ci } 211462306a36Sopenharmony_ci 211562306a36Sopenharmony_ci /* 211662306a36Sopenharmony_ci * The filesystem may choose to bump up nr_to_write. 211762306a36Sopenharmony_ci * We have to make sure to honor the new nr_to_write 211862306a36Sopenharmony_ci * at any time. 211962306a36Sopenharmony_ci */ 212062306a36Sopenharmony_ci nr_to_write_done = (wbc->sync_mode == WB_SYNC_NONE && 212162306a36Sopenharmony_ci wbc->nr_to_write <= 0); 212262306a36Sopenharmony_ci } 212362306a36Sopenharmony_ci folio_batch_release(&fbatch); 212462306a36Sopenharmony_ci cond_resched(); 212562306a36Sopenharmony_ci } 212662306a36Sopenharmony_ci if (!scanned && !done) { 212762306a36Sopenharmony_ci /* 212862306a36Sopenharmony_ci * We hit the last page and there is more work to be done: wrap 212962306a36Sopenharmony_ci * back to the start of the file 213062306a36Sopenharmony_ci */ 213162306a36Sopenharmony_ci scanned = 1; 213262306a36Sopenharmony_ci index = 0; 213362306a36Sopenharmony_ci 213462306a36Sopenharmony_ci /* 213562306a36Sopenharmony_ci * If we're looping we could run into a page that is locked by a 213662306a36Sopenharmony_ci * writer and that writer could be waiting on writeback for a 213762306a36Sopenharmony_ci * page in our current bio, and thus deadlock, so flush the 213862306a36Sopenharmony_ci * write bio here. 213962306a36Sopenharmony_ci */ 214062306a36Sopenharmony_ci submit_write_bio(bio_ctrl, 0); 214162306a36Sopenharmony_ci goto retry; 214262306a36Sopenharmony_ci } 214362306a36Sopenharmony_ci 214462306a36Sopenharmony_ci if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole)) 214562306a36Sopenharmony_ci mapping->writeback_index = done_index; 214662306a36Sopenharmony_ci 214762306a36Sopenharmony_ci btrfs_add_delayed_iput(BTRFS_I(inode)); 214862306a36Sopenharmony_ci return ret; 214962306a36Sopenharmony_ci} 215062306a36Sopenharmony_ci 215162306a36Sopenharmony_ci/* 215262306a36Sopenharmony_ci * Submit the pages in the range to bio for call sites which delalloc range has 215362306a36Sopenharmony_ci * already been ran (aka, ordered extent inserted) and all pages are still 215462306a36Sopenharmony_ci * locked. 215562306a36Sopenharmony_ci */ 215662306a36Sopenharmony_civoid extent_write_locked_range(struct inode *inode, struct page *locked_page, 215762306a36Sopenharmony_ci u64 start, u64 end, struct writeback_control *wbc, 215862306a36Sopenharmony_ci bool pages_dirty) 215962306a36Sopenharmony_ci{ 216062306a36Sopenharmony_ci bool found_error = false; 216162306a36Sopenharmony_ci int ret = 0; 216262306a36Sopenharmony_ci struct address_space *mapping = inode->i_mapping; 216362306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 216462306a36Sopenharmony_ci const u32 sectorsize = fs_info->sectorsize; 216562306a36Sopenharmony_ci loff_t i_size = i_size_read(inode); 216662306a36Sopenharmony_ci u64 cur = start; 216762306a36Sopenharmony_ci struct btrfs_bio_ctrl bio_ctrl = { 216862306a36Sopenharmony_ci .wbc = wbc, 216962306a36Sopenharmony_ci .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc), 217062306a36Sopenharmony_ci }; 217162306a36Sopenharmony_ci 217262306a36Sopenharmony_ci if (wbc->no_cgroup_owner) 217362306a36Sopenharmony_ci bio_ctrl.opf |= REQ_BTRFS_CGROUP_PUNT; 217462306a36Sopenharmony_ci 217562306a36Sopenharmony_ci ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(end + 1, sectorsize)); 217662306a36Sopenharmony_ci 217762306a36Sopenharmony_ci while (cur <= end) { 217862306a36Sopenharmony_ci u64 cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end); 217962306a36Sopenharmony_ci u32 cur_len = cur_end + 1 - cur; 218062306a36Sopenharmony_ci struct page *page; 218162306a36Sopenharmony_ci int nr = 0; 218262306a36Sopenharmony_ci 218362306a36Sopenharmony_ci page = find_get_page(mapping, cur >> PAGE_SHIFT); 218462306a36Sopenharmony_ci ASSERT(PageLocked(page)); 218562306a36Sopenharmony_ci if (pages_dirty && page != locked_page) { 218662306a36Sopenharmony_ci ASSERT(PageDirty(page)); 218762306a36Sopenharmony_ci clear_page_dirty_for_io(page); 218862306a36Sopenharmony_ci } 218962306a36Sopenharmony_ci 219062306a36Sopenharmony_ci ret = __extent_writepage_io(BTRFS_I(inode), page, &bio_ctrl, 219162306a36Sopenharmony_ci i_size, &nr); 219262306a36Sopenharmony_ci if (ret == 1) 219362306a36Sopenharmony_ci goto next_page; 219462306a36Sopenharmony_ci 219562306a36Sopenharmony_ci /* Make sure the mapping tag for page dirty gets cleared. */ 219662306a36Sopenharmony_ci if (nr == 0) { 219762306a36Sopenharmony_ci set_page_writeback(page); 219862306a36Sopenharmony_ci end_page_writeback(page); 219962306a36Sopenharmony_ci } 220062306a36Sopenharmony_ci if (ret) { 220162306a36Sopenharmony_ci btrfs_mark_ordered_io_finished(BTRFS_I(inode), page, 220262306a36Sopenharmony_ci cur, cur_len, !ret); 220362306a36Sopenharmony_ci mapping_set_error(page->mapping, ret); 220462306a36Sopenharmony_ci } 220562306a36Sopenharmony_ci btrfs_page_unlock_writer(fs_info, page, cur, cur_len); 220662306a36Sopenharmony_ci if (ret < 0) 220762306a36Sopenharmony_ci found_error = true; 220862306a36Sopenharmony_cinext_page: 220962306a36Sopenharmony_ci put_page(page); 221062306a36Sopenharmony_ci cur = cur_end + 1; 221162306a36Sopenharmony_ci } 221262306a36Sopenharmony_ci 221362306a36Sopenharmony_ci submit_write_bio(&bio_ctrl, found_error ? ret : 0); 221462306a36Sopenharmony_ci} 221562306a36Sopenharmony_ci 221662306a36Sopenharmony_ciint extent_writepages(struct address_space *mapping, 221762306a36Sopenharmony_ci struct writeback_control *wbc) 221862306a36Sopenharmony_ci{ 221962306a36Sopenharmony_ci struct inode *inode = mapping->host; 222062306a36Sopenharmony_ci int ret = 0; 222162306a36Sopenharmony_ci struct btrfs_bio_ctrl bio_ctrl = { 222262306a36Sopenharmony_ci .wbc = wbc, 222362306a36Sopenharmony_ci .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc), 222462306a36Sopenharmony_ci }; 222562306a36Sopenharmony_ci 222662306a36Sopenharmony_ci /* 222762306a36Sopenharmony_ci * Allow only a single thread to do the reloc work in zoned mode to 222862306a36Sopenharmony_ci * protect the write pointer updates. 222962306a36Sopenharmony_ci */ 223062306a36Sopenharmony_ci btrfs_zoned_data_reloc_lock(BTRFS_I(inode)); 223162306a36Sopenharmony_ci ret = extent_write_cache_pages(mapping, &bio_ctrl); 223262306a36Sopenharmony_ci submit_write_bio(&bio_ctrl, ret); 223362306a36Sopenharmony_ci btrfs_zoned_data_reloc_unlock(BTRFS_I(inode)); 223462306a36Sopenharmony_ci return ret; 223562306a36Sopenharmony_ci} 223662306a36Sopenharmony_ci 223762306a36Sopenharmony_civoid extent_readahead(struct readahead_control *rac) 223862306a36Sopenharmony_ci{ 223962306a36Sopenharmony_ci struct btrfs_bio_ctrl bio_ctrl = { .opf = REQ_OP_READ | REQ_RAHEAD }; 224062306a36Sopenharmony_ci struct page *pagepool[16]; 224162306a36Sopenharmony_ci struct extent_map *em_cached = NULL; 224262306a36Sopenharmony_ci u64 prev_em_start = (u64)-1; 224362306a36Sopenharmony_ci int nr; 224462306a36Sopenharmony_ci 224562306a36Sopenharmony_ci while ((nr = readahead_page_batch(rac, pagepool))) { 224662306a36Sopenharmony_ci u64 contig_start = readahead_pos(rac); 224762306a36Sopenharmony_ci u64 contig_end = contig_start + readahead_batch_length(rac) - 1; 224862306a36Sopenharmony_ci 224962306a36Sopenharmony_ci contiguous_readpages(pagepool, nr, contig_start, contig_end, 225062306a36Sopenharmony_ci &em_cached, &bio_ctrl, &prev_em_start); 225162306a36Sopenharmony_ci } 225262306a36Sopenharmony_ci 225362306a36Sopenharmony_ci if (em_cached) 225462306a36Sopenharmony_ci free_extent_map(em_cached); 225562306a36Sopenharmony_ci submit_one_bio(&bio_ctrl); 225662306a36Sopenharmony_ci} 225762306a36Sopenharmony_ci 225862306a36Sopenharmony_ci/* 225962306a36Sopenharmony_ci * basic invalidate_folio code, this waits on any locked or writeback 226062306a36Sopenharmony_ci * ranges corresponding to the folio, and then deletes any extent state 226162306a36Sopenharmony_ci * records from the tree 226262306a36Sopenharmony_ci */ 226362306a36Sopenharmony_ciint extent_invalidate_folio(struct extent_io_tree *tree, 226462306a36Sopenharmony_ci struct folio *folio, size_t offset) 226562306a36Sopenharmony_ci{ 226662306a36Sopenharmony_ci struct extent_state *cached_state = NULL; 226762306a36Sopenharmony_ci u64 start = folio_pos(folio); 226862306a36Sopenharmony_ci u64 end = start + folio_size(folio) - 1; 226962306a36Sopenharmony_ci size_t blocksize = folio->mapping->host->i_sb->s_blocksize; 227062306a36Sopenharmony_ci 227162306a36Sopenharmony_ci /* This function is only called for the btree inode */ 227262306a36Sopenharmony_ci ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO); 227362306a36Sopenharmony_ci 227462306a36Sopenharmony_ci start += ALIGN(offset, blocksize); 227562306a36Sopenharmony_ci if (start > end) 227662306a36Sopenharmony_ci return 0; 227762306a36Sopenharmony_ci 227862306a36Sopenharmony_ci lock_extent(tree, start, end, &cached_state); 227962306a36Sopenharmony_ci folio_wait_writeback(folio); 228062306a36Sopenharmony_ci 228162306a36Sopenharmony_ci /* 228262306a36Sopenharmony_ci * Currently for btree io tree, only EXTENT_LOCKED is utilized, 228362306a36Sopenharmony_ci * so here we only need to unlock the extent range to free any 228462306a36Sopenharmony_ci * existing extent state. 228562306a36Sopenharmony_ci */ 228662306a36Sopenharmony_ci unlock_extent(tree, start, end, &cached_state); 228762306a36Sopenharmony_ci return 0; 228862306a36Sopenharmony_ci} 228962306a36Sopenharmony_ci 229062306a36Sopenharmony_ci/* 229162306a36Sopenharmony_ci * a helper for release_folio, this tests for areas of the page that 229262306a36Sopenharmony_ci * are locked or under IO and drops the related state bits if it is safe 229362306a36Sopenharmony_ci * to drop the page. 229462306a36Sopenharmony_ci */ 229562306a36Sopenharmony_cistatic int try_release_extent_state(struct extent_io_tree *tree, 229662306a36Sopenharmony_ci struct page *page, gfp_t mask) 229762306a36Sopenharmony_ci{ 229862306a36Sopenharmony_ci u64 start = page_offset(page); 229962306a36Sopenharmony_ci u64 end = start + PAGE_SIZE - 1; 230062306a36Sopenharmony_ci int ret = 1; 230162306a36Sopenharmony_ci 230262306a36Sopenharmony_ci if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) { 230362306a36Sopenharmony_ci ret = 0; 230462306a36Sopenharmony_ci } else { 230562306a36Sopenharmony_ci u32 clear_bits = ~(EXTENT_LOCKED | EXTENT_NODATASUM | 230662306a36Sopenharmony_ci EXTENT_DELALLOC_NEW | EXTENT_CTLBITS | 230762306a36Sopenharmony_ci EXTENT_QGROUP_RESERVED); 230862306a36Sopenharmony_ci 230962306a36Sopenharmony_ci /* 231062306a36Sopenharmony_ci * At this point we can safely clear everything except the 231162306a36Sopenharmony_ci * locked bit, the nodatasum bit and the delalloc new bit. 231262306a36Sopenharmony_ci * The delalloc new bit will be cleared by ordered extent 231362306a36Sopenharmony_ci * completion. 231462306a36Sopenharmony_ci */ 231562306a36Sopenharmony_ci ret = __clear_extent_bit(tree, start, end, clear_bits, NULL, NULL); 231662306a36Sopenharmony_ci 231762306a36Sopenharmony_ci /* if clear_extent_bit failed for enomem reasons, 231862306a36Sopenharmony_ci * we can't allow the release to continue. 231962306a36Sopenharmony_ci */ 232062306a36Sopenharmony_ci if (ret < 0) 232162306a36Sopenharmony_ci ret = 0; 232262306a36Sopenharmony_ci else 232362306a36Sopenharmony_ci ret = 1; 232462306a36Sopenharmony_ci } 232562306a36Sopenharmony_ci return ret; 232662306a36Sopenharmony_ci} 232762306a36Sopenharmony_ci 232862306a36Sopenharmony_ci/* 232962306a36Sopenharmony_ci * a helper for release_folio. As long as there are no locked extents 233062306a36Sopenharmony_ci * in the range corresponding to the page, both state records and extent 233162306a36Sopenharmony_ci * map records are removed 233262306a36Sopenharmony_ci */ 233362306a36Sopenharmony_ciint try_release_extent_mapping(struct page *page, gfp_t mask) 233462306a36Sopenharmony_ci{ 233562306a36Sopenharmony_ci struct extent_map *em; 233662306a36Sopenharmony_ci u64 start = page_offset(page); 233762306a36Sopenharmony_ci u64 end = start + PAGE_SIZE - 1; 233862306a36Sopenharmony_ci struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host); 233962306a36Sopenharmony_ci struct extent_io_tree *tree = &btrfs_inode->io_tree; 234062306a36Sopenharmony_ci struct extent_map_tree *map = &btrfs_inode->extent_tree; 234162306a36Sopenharmony_ci 234262306a36Sopenharmony_ci if (gfpflags_allow_blocking(mask) && 234362306a36Sopenharmony_ci page->mapping->host->i_size > SZ_16M) { 234462306a36Sopenharmony_ci u64 len; 234562306a36Sopenharmony_ci while (start <= end) { 234662306a36Sopenharmony_ci struct btrfs_fs_info *fs_info; 234762306a36Sopenharmony_ci u64 cur_gen; 234862306a36Sopenharmony_ci 234962306a36Sopenharmony_ci len = end - start + 1; 235062306a36Sopenharmony_ci write_lock(&map->lock); 235162306a36Sopenharmony_ci em = lookup_extent_mapping(map, start, len); 235262306a36Sopenharmony_ci if (!em) { 235362306a36Sopenharmony_ci write_unlock(&map->lock); 235462306a36Sopenharmony_ci break; 235562306a36Sopenharmony_ci } 235662306a36Sopenharmony_ci if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || 235762306a36Sopenharmony_ci em->start != start) { 235862306a36Sopenharmony_ci write_unlock(&map->lock); 235962306a36Sopenharmony_ci free_extent_map(em); 236062306a36Sopenharmony_ci break; 236162306a36Sopenharmony_ci } 236262306a36Sopenharmony_ci if (test_range_bit(tree, em->start, 236362306a36Sopenharmony_ci extent_map_end(em) - 1, 236462306a36Sopenharmony_ci EXTENT_LOCKED, 0, NULL)) 236562306a36Sopenharmony_ci goto next; 236662306a36Sopenharmony_ci /* 236762306a36Sopenharmony_ci * If it's not in the list of modified extents, used 236862306a36Sopenharmony_ci * by a fast fsync, we can remove it. If it's being 236962306a36Sopenharmony_ci * logged we can safely remove it since fsync took an 237062306a36Sopenharmony_ci * extra reference on the em. 237162306a36Sopenharmony_ci */ 237262306a36Sopenharmony_ci if (list_empty(&em->list) || 237362306a36Sopenharmony_ci test_bit(EXTENT_FLAG_LOGGING, &em->flags)) 237462306a36Sopenharmony_ci goto remove_em; 237562306a36Sopenharmony_ci /* 237662306a36Sopenharmony_ci * If it's in the list of modified extents, remove it 237762306a36Sopenharmony_ci * only if its generation is older then the current one, 237862306a36Sopenharmony_ci * in which case we don't need it for a fast fsync. 237962306a36Sopenharmony_ci * Otherwise don't remove it, we could be racing with an 238062306a36Sopenharmony_ci * ongoing fast fsync that could miss the new extent. 238162306a36Sopenharmony_ci */ 238262306a36Sopenharmony_ci fs_info = btrfs_inode->root->fs_info; 238362306a36Sopenharmony_ci spin_lock(&fs_info->trans_lock); 238462306a36Sopenharmony_ci cur_gen = fs_info->generation; 238562306a36Sopenharmony_ci spin_unlock(&fs_info->trans_lock); 238662306a36Sopenharmony_ci if (em->generation >= cur_gen) 238762306a36Sopenharmony_ci goto next; 238862306a36Sopenharmony_ciremove_em: 238962306a36Sopenharmony_ci /* 239062306a36Sopenharmony_ci * We only remove extent maps that are not in the list of 239162306a36Sopenharmony_ci * modified extents or that are in the list but with a 239262306a36Sopenharmony_ci * generation lower then the current generation, so there 239362306a36Sopenharmony_ci * is no need to set the full fsync flag on the inode (it 239462306a36Sopenharmony_ci * hurts the fsync performance for workloads with a data 239562306a36Sopenharmony_ci * size that exceeds or is close to the system's memory). 239662306a36Sopenharmony_ci */ 239762306a36Sopenharmony_ci remove_extent_mapping(map, em); 239862306a36Sopenharmony_ci /* once for the rb tree */ 239962306a36Sopenharmony_ci free_extent_map(em); 240062306a36Sopenharmony_cinext: 240162306a36Sopenharmony_ci start = extent_map_end(em); 240262306a36Sopenharmony_ci write_unlock(&map->lock); 240362306a36Sopenharmony_ci 240462306a36Sopenharmony_ci /* once for us */ 240562306a36Sopenharmony_ci free_extent_map(em); 240662306a36Sopenharmony_ci 240762306a36Sopenharmony_ci cond_resched(); /* Allow large-extent preemption. */ 240862306a36Sopenharmony_ci } 240962306a36Sopenharmony_ci } 241062306a36Sopenharmony_ci return try_release_extent_state(tree, page, mask); 241162306a36Sopenharmony_ci} 241262306a36Sopenharmony_ci 241362306a36Sopenharmony_ci/* 241462306a36Sopenharmony_ci * To cache previous fiemap extent 241562306a36Sopenharmony_ci * 241662306a36Sopenharmony_ci * Will be used for merging fiemap extent 241762306a36Sopenharmony_ci */ 241862306a36Sopenharmony_cistruct fiemap_cache { 241962306a36Sopenharmony_ci u64 offset; 242062306a36Sopenharmony_ci u64 phys; 242162306a36Sopenharmony_ci u64 len; 242262306a36Sopenharmony_ci u32 flags; 242362306a36Sopenharmony_ci bool cached; 242462306a36Sopenharmony_ci}; 242562306a36Sopenharmony_ci 242662306a36Sopenharmony_ci/* 242762306a36Sopenharmony_ci * Helper to submit fiemap extent. 242862306a36Sopenharmony_ci * 242962306a36Sopenharmony_ci * Will try to merge current fiemap extent specified by @offset, @phys, 243062306a36Sopenharmony_ci * @len and @flags with cached one. 243162306a36Sopenharmony_ci * And only when we fails to merge, cached one will be submitted as 243262306a36Sopenharmony_ci * fiemap extent. 243362306a36Sopenharmony_ci * 243462306a36Sopenharmony_ci * Return value is the same as fiemap_fill_next_extent(). 243562306a36Sopenharmony_ci */ 243662306a36Sopenharmony_cistatic int emit_fiemap_extent(struct fiemap_extent_info *fieinfo, 243762306a36Sopenharmony_ci struct fiemap_cache *cache, 243862306a36Sopenharmony_ci u64 offset, u64 phys, u64 len, u32 flags) 243962306a36Sopenharmony_ci{ 244062306a36Sopenharmony_ci u64 cache_end; 244162306a36Sopenharmony_ci int ret = 0; 244262306a36Sopenharmony_ci 244362306a36Sopenharmony_ci /* Set at the end of extent_fiemap(). */ 244462306a36Sopenharmony_ci ASSERT((flags & FIEMAP_EXTENT_LAST) == 0); 244562306a36Sopenharmony_ci 244662306a36Sopenharmony_ci if (!cache->cached) 244762306a36Sopenharmony_ci goto assign; 244862306a36Sopenharmony_ci 244962306a36Sopenharmony_ci /* 245062306a36Sopenharmony_ci * When iterating the extents of the inode, at extent_fiemap(), we may 245162306a36Sopenharmony_ci * find an extent that starts at an offset behind the end offset of the 245262306a36Sopenharmony_ci * previous extent we processed. This happens if fiemap is called 245362306a36Sopenharmony_ci * without FIEMAP_FLAG_SYNC and there are ordered extents completing 245462306a36Sopenharmony_ci * while we call btrfs_next_leaf() (through fiemap_next_leaf_item()). 245562306a36Sopenharmony_ci * 245662306a36Sopenharmony_ci * For example we are in leaf X processing its last item, which is the 245762306a36Sopenharmony_ci * file extent item for file range [512K, 1M[, and after 245862306a36Sopenharmony_ci * btrfs_next_leaf() releases the path, there's an ordered extent that 245962306a36Sopenharmony_ci * completes for the file range [768K, 2M[, and that results in trimming 246062306a36Sopenharmony_ci * the file extent item so that it now corresponds to the file range 246162306a36Sopenharmony_ci * [512K, 768K[ and a new file extent item is inserted for the file 246262306a36Sopenharmony_ci * range [768K, 2M[, which may end up as the last item of leaf X or as 246362306a36Sopenharmony_ci * the first item of the next leaf - in either case btrfs_next_leaf() 246462306a36Sopenharmony_ci * will leave us with a path pointing to the new extent item, for the 246562306a36Sopenharmony_ci * file range [768K, 2M[, since that's the first key that follows the 246662306a36Sopenharmony_ci * last one we processed. So in order not to report overlapping extents 246762306a36Sopenharmony_ci * to user space, we trim the length of the previously cached extent and 246862306a36Sopenharmony_ci * emit it. 246962306a36Sopenharmony_ci * 247062306a36Sopenharmony_ci * Upon calling btrfs_next_leaf() we may also find an extent with an 247162306a36Sopenharmony_ci * offset smaller than or equals to cache->offset, and this happens 247262306a36Sopenharmony_ci * when we had a hole or prealloc extent with several delalloc ranges in 247362306a36Sopenharmony_ci * it, but after btrfs_next_leaf() released the path, delalloc was 247462306a36Sopenharmony_ci * flushed and the resulting ordered extents were completed, so we can 247562306a36Sopenharmony_ci * now have found a file extent item for an offset that is smaller than 247662306a36Sopenharmony_ci * or equals to what we have in cache->offset. We deal with this as 247762306a36Sopenharmony_ci * described below. 247862306a36Sopenharmony_ci */ 247962306a36Sopenharmony_ci cache_end = cache->offset + cache->len; 248062306a36Sopenharmony_ci if (cache_end > offset) { 248162306a36Sopenharmony_ci if (offset == cache->offset) { 248262306a36Sopenharmony_ci /* 248362306a36Sopenharmony_ci * We cached a dealloc range (found in the io tree) for 248462306a36Sopenharmony_ci * a hole or prealloc extent and we have now found a 248562306a36Sopenharmony_ci * file extent item for the same offset. What we have 248662306a36Sopenharmony_ci * now is more recent and up to date, so discard what 248762306a36Sopenharmony_ci * we had in the cache and use what we have just found. 248862306a36Sopenharmony_ci */ 248962306a36Sopenharmony_ci goto assign; 249062306a36Sopenharmony_ci } else if (offset > cache->offset) { 249162306a36Sopenharmony_ci /* 249262306a36Sopenharmony_ci * The extent range we previously found ends after the 249362306a36Sopenharmony_ci * offset of the file extent item we found and that 249462306a36Sopenharmony_ci * offset falls somewhere in the middle of that previous 249562306a36Sopenharmony_ci * extent range. So adjust the range we previously found 249662306a36Sopenharmony_ci * to end at the offset of the file extent item we have 249762306a36Sopenharmony_ci * just found, since this extent is more up to date. 249862306a36Sopenharmony_ci * Emit that adjusted range and cache the file extent 249962306a36Sopenharmony_ci * item we have just found. This corresponds to the case 250062306a36Sopenharmony_ci * where a previously found file extent item was split 250162306a36Sopenharmony_ci * due to an ordered extent completing. 250262306a36Sopenharmony_ci */ 250362306a36Sopenharmony_ci cache->len = offset - cache->offset; 250462306a36Sopenharmony_ci goto emit; 250562306a36Sopenharmony_ci } else { 250662306a36Sopenharmony_ci const u64 range_end = offset + len; 250762306a36Sopenharmony_ci 250862306a36Sopenharmony_ci /* 250962306a36Sopenharmony_ci * The offset of the file extent item we have just found 251062306a36Sopenharmony_ci * is behind the cached offset. This means we were 251162306a36Sopenharmony_ci * processing a hole or prealloc extent for which we 251262306a36Sopenharmony_ci * have found delalloc ranges (in the io tree), so what 251362306a36Sopenharmony_ci * we have in the cache is the last delalloc range we 251462306a36Sopenharmony_ci * found while the file extent item we found can be 251562306a36Sopenharmony_ci * either for a whole delalloc range we previously 251662306a36Sopenharmony_ci * emmitted or only a part of that range. 251762306a36Sopenharmony_ci * 251862306a36Sopenharmony_ci * We have two cases here: 251962306a36Sopenharmony_ci * 252062306a36Sopenharmony_ci * 1) The file extent item's range ends at or behind the 252162306a36Sopenharmony_ci * cached extent's end. In this case just ignore the 252262306a36Sopenharmony_ci * current file extent item because we don't want to 252362306a36Sopenharmony_ci * overlap with previous ranges that may have been 252462306a36Sopenharmony_ci * emmitted already; 252562306a36Sopenharmony_ci * 252662306a36Sopenharmony_ci * 2) The file extent item starts behind the currently 252762306a36Sopenharmony_ci * cached extent but its end offset goes beyond the 252862306a36Sopenharmony_ci * end offset of the cached extent. We don't want to 252962306a36Sopenharmony_ci * overlap with a previous range that may have been 253062306a36Sopenharmony_ci * emmitted already, so we emit the currently cached 253162306a36Sopenharmony_ci * extent and then partially store the current file 253262306a36Sopenharmony_ci * extent item's range in the cache, for the subrange 253362306a36Sopenharmony_ci * going the cached extent's end to the end of the 253462306a36Sopenharmony_ci * file extent item. 253562306a36Sopenharmony_ci */ 253662306a36Sopenharmony_ci if (range_end <= cache_end) 253762306a36Sopenharmony_ci return 0; 253862306a36Sopenharmony_ci 253962306a36Sopenharmony_ci if (!(flags & (FIEMAP_EXTENT_ENCODED | FIEMAP_EXTENT_DELALLOC))) 254062306a36Sopenharmony_ci phys += cache_end - offset; 254162306a36Sopenharmony_ci 254262306a36Sopenharmony_ci offset = cache_end; 254362306a36Sopenharmony_ci len = range_end - cache_end; 254462306a36Sopenharmony_ci goto emit; 254562306a36Sopenharmony_ci } 254662306a36Sopenharmony_ci } 254762306a36Sopenharmony_ci 254862306a36Sopenharmony_ci /* 254962306a36Sopenharmony_ci * Only merges fiemap extents if 255062306a36Sopenharmony_ci * 1) Their logical addresses are continuous 255162306a36Sopenharmony_ci * 255262306a36Sopenharmony_ci * 2) Their physical addresses are continuous 255362306a36Sopenharmony_ci * So truly compressed (physical size smaller than logical size) 255462306a36Sopenharmony_ci * extents won't get merged with each other 255562306a36Sopenharmony_ci * 255662306a36Sopenharmony_ci * 3) Share same flags 255762306a36Sopenharmony_ci */ 255862306a36Sopenharmony_ci if (cache->offset + cache->len == offset && 255962306a36Sopenharmony_ci cache->phys + cache->len == phys && 256062306a36Sopenharmony_ci cache->flags == flags) { 256162306a36Sopenharmony_ci cache->len += len; 256262306a36Sopenharmony_ci return 0; 256362306a36Sopenharmony_ci } 256462306a36Sopenharmony_ci 256562306a36Sopenharmony_ciemit: 256662306a36Sopenharmony_ci /* Not mergeable, need to submit cached one */ 256762306a36Sopenharmony_ci ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, 256862306a36Sopenharmony_ci cache->len, cache->flags); 256962306a36Sopenharmony_ci cache->cached = false; 257062306a36Sopenharmony_ci if (ret) 257162306a36Sopenharmony_ci return ret; 257262306a36Sopenharmony_ciassign: 257362306a36Sopenharmony_ci cache->cached = true; 257462306a36Sopenharmony_ci cache->offset = offset; 257562306a36Sopenharmony_ci cache->phys = phys; 257662306a36Sopenharmony_ci cache->len = len; 257762306a36Sopenharmony_ci cache->flags = flags; 257862306a36Sopenharmony_ci 257962306a36Sopenharmony_ci return 0; 258062306a36Sopenharmony_ci} 258162306a36Sopenharmony_ci 258262306a36Sopenharmony_ci/* 258362306a36Sopenharmony_ci * Emit last fiemap cache 258462306a36Sopenharmony_ci * 258562306a36Sopenharmony_ci * The last fiemap cache may still be cached in the following case: 258662306a36Sopenharmony_ci * 0 4k 8k 258762306a36Sopenharmony_ci * |<- Fiemap range ->| 258862306a36Sopenharmony_ci * |<------------ First extent ----------->| 258962306a36Sopenharmony_ci * 259062306a36Sopenharmony_ci * In this case, the first extent range will be cached but not emitted. 259162306a36Sopenharmony_ci * So we must emit it before ending extent_fiemap(). 259262306a36Sopenharmony_ci */ 259362306a36Sopenharmony_cistatic int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo, 259462306a36Sopenharmony_ci struct fiemap_cache *cache) 259562306a36Sopenharmony_ci{ 259662306a36Sopenharmony_ci int ret; 259762306a36Sopenharmony_ci 259862306a36Sopenharmony_ci if (!cache->cached) 259962306a36Sopenharmony_ci return 0; 260062306a36Sopenharmony_ci 260162306a36Sopenharmony_ci ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, 260262306a36Sopenharmony_ci cache->len, cache->flags); 260362306a36Sopenharmony_ci cache->cached = false; 260462306a36Sopenharmony_ci if (ret > 0) 260562306a36Sopenharmony_ci ret = 0; 260662306a36Sopenharmony_ci return ret; 260762306a36Sopenharmony_ci} 260862306a36Sopenharmony_ci 260962306a36Sopenharmony_cistatic int fiemap_next_leaf_item(struct btrfs_inode *inode, struct btrfs_path *path) 261062306a36Sopenharmony_ci{ 261162306a36Sopenharmony_ci struct extent_buffer *clone; 261262306a36Sopenharmony_ci struct btrfs_key key; 261362306a36Sopenharmony_ci int slot; 261462306a36Sopenharmony_ci int ret; 261562306a36Sopenharmony_ci 261662306a36Sopenharmony_ci path->slots[0]++; 261762306a36Sopenharmony_ci if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) 261862306a36Sopenharmony_ci return 0; 261962306a36Sopenharmony_ci 262062306a36Sopenharmony_ci ret = btrfs_next_leaf(inode->root, path); 262162306a36Sopenharmony_ci if (ret != 0) 262262306a36Sopenharmony_ci return ret; 262362306a36Sopenharmony_ci 262462306a36Sopenharmony_ci /* 262562306a36Sopenharmony_ci * Don't bother with cloning if there are no more file extent items for 262662306a36Sopenharmony_ci * our inode. 262762306a36Sopenharmony_ci */ 262862306a36Sopenharmony_ci btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 262962306a36Sopenharmony_ci if (key.objectid != btrfs_ino(inode) || key.type != BTRFS_EXTENT_DATA_KEY) 263062306a36Sopenharmony_ci return 1; 263162306a36Sopenharmony_ci 263262306a36Sopenharmony_ci /* See the comment at fiemap_search_slot() about why we clone. */ 263362306a36Sopenharmony_ci clone = btrfs_clone_extent_buffer(path->nodes[0]); 263462306a36Sopenharmony_ci if (!clone) 263562306a36Sopenharmony_ci return -ENOMEM; 263662306a36Sopenharmony_ci 263762306a36Sopenharmony_ci slot = path->slots[0]; 263862306a36Sopenharmony_ci btrfs_release_path(path); 263962306a36Sopenharmony_ci path->nodes[0] = clone; 264062306a36Sopenharmony_ci path->slots[0] = slot; 264162306a36Sopenharmony_ci 264262306a36Sopenharmony_ci return 0; 264362306a36Sopenharmony_ci} 264462306a36Sopenharmony_ci 264562306a36Sopenharmony_ci/* 264662306a36Sopenharmony_ci * Search for the first file extent item that starts at a given file offset or 264762306a36Sopenharmony_ci * the one that starts immediately before that offset. 264862306a36Sopenharmony_ci * Returns: 0 on success, < 0 on error, 1 if not found. 264962306a36Sopenharmony_ci */ 265062306a36Sopenharmony_cistatic int fiemap_search_slot(struct btrfs_inode *inode, struct btrfs_path *path, 265162306a36Sopenharmony_ci u64 file_offset) 265262306a36Sopenharmony_ci{ 265362306a36Sopenharmony_ci const u64 ino = btrfs_ino(inode); 265462306a36Sopenharmony_ci struct btrfs_root *root = inode->root; 265562306a36Sopenharmony_ci struct extent_buffer *clone; 265662306a36Sopenharmony_ci struct btrfs_key key; 265762306a36Sopenharmony_ci int slot; 265862306a36Sopenharmony_ci int ret; 265962306a36Sopenharmony_ci 266062306a36Sopenharmony_ci key.objectid = ino; 266162306a36Sopenharmony_ci key.type = BTRFS_EXTENT_DATA_KEY; 266262306a36Sopenharmony_ci key.offset = file_offset; 266362306a36Sopenharmony_ci 266462306a36Sopenharmony_ci ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 266562306a36Sopenharmony_ci if (ret < 0) 266662306a36Sopenharmony_ci return ret; 266762306a36Sopenharmony_ci 266862306a36Sopenharmony_ci if (ret > 0 && path->slots[0] > 0) { 266962306a36Sopenharmony_ci btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1); 267062306a36Sopenharmony_ci if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY) 267162306a36Sopenharmony_ci path->slots[0]--; 267262306a36Sopenharmony_ci } 267362306a36Sopenharmony_ci 267462306a36Sopenharmony_ci if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 267562306a36Sopenharmony_ci ret = btrfs_next_leaf(root, path); 267662306a36Sopenharmony_ci if (ret != 0) 267762306a36Sopenharmony_ci return ret; 267862306a36Sopenharmony_ci 267962306a36Sopenharmony_ci btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 268062306a36Sopenharmony_ci if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) 268162306a36Sopenharmony_ci return 1; 268262306a36Sopenharmony_ci } 268362306a36Sopenharmony_ci 268462306a36Sopenharmony_ci /* 268562306a36Sopenharmony_ci * We clone the leaf and use it during fiemap. This is because while 268662306a36Sopenharmony_ci * using the leaf we do expensive things like checking if an extent is 268762306a36Sopenharmony_ci * shared, which can take a long time. In order to prevent blocking 268862306a36Sopenharmony_ci * other tasks for too long, we use a clone of the leaf. We have locked 268962306a36Sopenharmony_ci * the file range in the inode's io tree, so we know none of our file 269062306a36Sopenharmony_ci * extent items can change. This way we avoid blocking other tasks that 269162306a36Sopenharmony_ci * want to insert items for other inodes in the same leaf or b+tree 269262306a36Sopenharmony_ci * rebalance operations (triggered for example when someone is trying 269362306a36Sopenharmony_ci * to push items into this leaf when trying to insert an item in a 269462306a36Sopenharmony_ci * neighbour leaf). 269562306a36Sopenharmony_ci * We also need the private clone because holding a read lock on an 269662306a36Sopenharmony_ci * extent buffer of the subvolume's b+tree will make lockdep unhappy 269762306a36Sopenharmony_ci * when we call fiemap_fill_next_extent(), because that may cause a page 269862306a36Sopenharmony_ci * fault when filling the user space buffer with fiemap data. 269962306a36Sopenharmony_ci */ 270062306a36Sopenharmony_ci clone = btrfs_clone_extent_buffer(path->nodes[0]); 270162306a36Sopenharmony_ci if (!clone) 270262306a36Sopenharmony_ci return -ENOMEM; 270362306a36Sopenharmony_ci 270462306a36Sopenharmony_ci slot = path->slots[0]; 270562306a36Sopenharmony_ci btrfs_release_path(path); 270662306a36Sopenharmony_ci path->nodes[0] = clone; 270762306a36Sopenharmony_ci path->slots[0] = slot; 270862306a36Sopenharmony_ci 270962306a36Sopenharmony_ci return 0; 271062306a36Sopenharmony_ci} 271162306a36Sopenharmony_ci 271262306a36Sopenharmony_ci/* 271362306a36Sopenharmony_ci * Process a range which is a hole or a prealloc extent in the inode's subvolume 271462306a36Sopenharmony_ci * btree. If @disk_bytenr is 0, we are dealing with a hole, otherwise a prealloc 271562306a36Sopenharmony_ci * extent. The end offset (@end) is inclusive. 271662306a36Sopenharmony_ci */ 271762306a36Sopenharmony_cistatic int fiemap_process_hole(struct btrfs_inode *inode, 271862306a36Sopenharmony_ci struct fiemap_extent_info *fieinfo, 271962306a36Sopenharmony_ci struct fiemap_cache *cache, 272062306a36Sopenharmony_ci struct extent_state **delalloc_cached_state, 272162306a36Sopenharmony_ci struct btrfs_backref_share_check_ctx *backref_ctx, 272262306a36Sopenharmony_ci u64 disk_bytenr, u64 extent_offset, 272362306a36Sopenharmony_ci u64 extent_gen, 272462306a36Sopenharmony_ci u64 start, u64 end) 272562306a36Sopenharmony_ci{ 272662306a36Sopenharmony_ci const u64 i_size = i_size_read(&inode->vfs_inode); 272762306a36Sopenharmony_ci u64 cur_offset = start; 272862306a36Sopenharmony_ci u64 last_delalloc_end = 0; 272962306a36Sopenharmony_ci u32 prealloc_flags = FIEMAP_EXTENT_UNWRITTEN; 273062306a36Sopenharmony_ci bool checked_extent_shared = false; 273162306a36Sopenharmony_ci int ret; 273262306a36Sopenharmony_ci 273362306a36Sopenharmony_ci /* 273462306a36Sopenharmony_ci * There can be no delalloc past i_size, so don't waste time looking for 273562306a36Sopenharmony_ci * it beyond i_size. 273662306a36Sopenharmony_ci */ 273762306a36Sopenharmony_ci while (cur_offset < end && cur_offset < i_size) { 273862306a36Sopenharmony_ci u64 delalloc_start; 273962306a36Sopenharmony_ci u64 delalloc_end; 274062306a36Sopenharmony_ci u64 prealloc_start; 274162306a36Sopenharmony_ci u64 prealloc_len = 0; 274262306a36Sopenharmony_ci bool delalloc; 274362306a36Sopenharmony_ci 274462306a36Sopenharmony_ci delalloc = btrfs_find_delalloc_in_range(inode, cur_offset, end, 274562306a36Sopenharmony_ci delalloc_cached_state, 274662306a36Sopenharmony_ci &delalloc_start, 274762306a36Sopenharmony_ci &delalloc_end); 274862306a36Sopenharmony_ci if (!delalloc) 274962306a36Sopenharmony_ci break; 275062306a36Sopenharmony_ci 275162306a36Sopenharmony_ci /* 275262306a36Sopenharmony_ci * If this is a prealloc extent we have to report every section 275362306a36Sopenharmony_ci * of it that has no delalloc. 275462306a36Sopenharmony_ci */ 275562306a36Sopenharmony_ci if (disk_bytenr != 0) { 275662306a36Sopenharmony_ci if (last_delalloc_end == 0) { 275762306a36Sopenharmony_ci prealloc_start = start; 275862306a36Sopenharmony_ci prealloc_len = delalloc_start - start; 275962306a36Sopenharmony_ci } else { 276062306a36Sopenharmony_ci prealloc_start = last_delalloc_end + 1; 276162306a36Sopenharmony_ci prealloc_len = delalloc_start - prealloc_start; 276262306a36Sopenharmony_ci } 276362306a36Sopenharmony_ci } 276462306a36Sopenharmony_ci 276562306a36Sopenharmony_ci if (prealloc_len > 0) { 276662306a36Sopenharmony_ci if (!checked_extent_shared && fieinfo->fi_extents_max) { 276762306a36Sopenharmony_ci ret = btrfs_is_data_extent_shared(inode, 276862306a36Sopenharmony_ci disk_bytenr, 276962306a36Sopenharmony_ci extent_gen, 277062306a36Sopenharmony_ci backref_ctx); 277162306a36Sopenharmony_ci if (ret < 0) 277262306a36Sopenharmony_ci return ret; 277362306a36Sopenharmony_ci else if (ret > 0) 277462306a36Sopenharmony_ci prealloc_flags |= FIEMAP_EXTENT_SHARED; 277562306a36Sopenharmony_ci 277662306a36Sopenharmony_ci checked_extent_shared = true; 277762306a36Sopenharmony_ci } 277862306a36Sopenharmony_ci ret = emit_fiemap_extent(fieinfo, cache, prealloc_start, 277962306a36Sopenharmony_ci disk_bytenr + extent_offset, 278062306a36Sopenharmony_ci prealloc_len, prealloc_flags); 278162306a36Sopenharmony_ci if (ret) 278262306a36Sopenharmony_ci return ret; 278362306a36Sopenharmony_ci extent_offset += prealloc_len; 278462306a36Sopenharmony_ci } 278562306a36Sopenharmony_ci 278662306a36Sopenharmony_ci ret = emit_fiemap_extent(fieinfo, cache, delalloc_start, 0, 278762306a36Sopenharmony_ci delalloc_end + 1 - delalloc_start, 278862306a36Sopenharmony_ci FIEMAP_EXTENT_DELALLOC | 278962306a36Sopenharmony_ci FIEMAP_EXTENT_UNKNOWN); 279062306a36Sopenharmony_ci if (ret) 279162306a36Sopenharmony_ci return ret; 279262306a36Sopenharmony_ci 279362306a36Sopenharmony_ci last_delalloc_end = delalloc_end; 279462306a36Sopenharmony_ci cur_offset = delalloc_end + 1; 279562306a36Sopenharmony_ci extent_offset += cur_offset - delalloc_start; 279662306a36Sopenharmony_ci cond_resched(); 279762306a36Sopenharmony_ci } 279862306a36Sopenharmony_ci 279962306a36Sopenharmony_ci /* 280062306a36Sopenharmony_ci * Either we found no delalloc for the whole prealloc extent or we have 280162306a36Sopenharmony_ci * a prealloc extent that spans i_size or starts at or after i_size. 280262306a36Sopenharmony_ci */ 280362306a36Sopenharmony_ci if (disk_bytenr != 0 && last_delalloc_end < end) { 280462306a36Sopenharmony_ci u64 prealloc_start; 280562306a36Sopenharmony_ci u64 prealloc_len; 280662306a36Sopenharmony_ci 280762306a36Sopenharmony_ci if (last_delalloc_end == 0) { 280862306a36Sopenharmony_ci prealloc_start = start; 280962306a36Sopenharmony_ci prealloc_len = end + 1 - start; 281062306a36Sopenharmony_ci } else { 281162306a36Sopenharmony_ci prealloc_start = last_delalloc_end + 1; 281262306a36Sopenharmony_ci prealloc_len = end + 1 - prealloc_start; 281362306a36Sopenharmony_ci } 281462306a36Sopenharmony_ci 281562306a36Sopenharmony_ci if (!checked_extent_shared && fieinfo->fi_extents_max) { 281662306a36Sopenharmony_ci ret = btrfs_is_data_extent_shared(inode, 281762306a36Sopenharmony_ci disk_bytenr, 281862306a36Sopenharmony_ci extent_gen, 281962306a36Sopenharmony_ci backref_ctx); 282062306a36Sopenharmony_ci if (ret < 0) 282162306a36Sopenharmony_ci return ret; 282262306a36Sopenharmony_ci else if (ret > 0) 282362306a36Sopenharmony_ci prealloc_flags |= FIEMAP_EXTENT_SHARED; 282462306a36Sopenharmony_ci } 282562306a36Sopenharmony_ci ret = emit_fiemap_extent(fieinfo, cache, prealloc_start, 282662306a36Sopenharmony_ci disk_bytenr + extent_offset, 282762306a36Sopenharmony_ci prealloc_len, prealloc_flags); 282862306a36Sopenharmony_ci if (ret) 282962306a36Sopenharmony_ci return ret; 283062306a36Sopenharmony_ci } 283162306a36Sopenharmony_ci 283262306a36Sopenharmony_ci return 0; 283362306a36Sopenharmony_ci} 283462306a36Sopenharmony_ci 283562306a36Sopenharmony_cistatic int fiemap_find_last_extent_offset(struct btrfs_inode *inode, 283662306a36Sopenharmony_ci struct btrfs_path *path, 283762306a36Sopenharmony_ci u64 *last_extent_end_ret) 283862306a36Sopenharmony_ci{ 283962306a36Sopenharmony_ci const u64 ino = btrfs_ino(inode); 284062306a36Sopenharmony_ci struct btrfs_root *root = inode->root; 284162306a36Sopenharmony_ci struct extent_buffer *leaf; 284262306a36Sopenharmony_ci struct btrfs_file_extent_item *ei; 284362306a36Sopenharmony_ci struct btrfs_key key; 284462306a36Sopenharmony_ci u64 disk_bytenr; 284562306a36Sopenharmony_ci int ret; 284662306a36Sopenharmony_ci 284762306a36Sopenharmony_ci /* 284862306a36Sopenharmony_ci * Lookup the last file extent. We're not using i_size here because 284962306a36Sopenharmony_ci * there might be preallocation past i_size. 285062306a36Sopenharmony_ci */ 285162306a36Sopenharmony_ci ret = btrfs_lookup_file_extent(NULL, root, path, ino, (u64)-1, 0); 285262306a36Sopenharmony_ci /* There can't be a file extent item at offset (u64)-1 */ 285362306a36Sopenharmony_ci ASSERT(ret != 0); 285462306a36Sopenharmony_ci if (ret < 0) 285562306a36Sopenharmony_ci return ret; 285662306a36Sopenharmony_ci 285762306a36Sopenharmony_ci /* 285862306a36Sopenharmony_ci * For a non-existing key, btrfs_search_slot() always leaves us at a 285962306a36Sopenharmony_ci * slot > 0, except if the btree is empty, which is impossible because 286062306a36Sopenharmony_ci * at least it has the inode item for this inode and all the items for 286162306a36Sopenharmony_ci * the root inode 256. 286262306a36Sopenharmony_ci */ 286362306a36Sopenharmony_ci ASSERT(path->slots[0] > 0); 286462306a36Sopenharmony_ci path->slots[0]--; 286562306a36Sopenharmony_ci leaf = path->nodes[0]; 286662306a36Sopenharmony_ci btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 286762306a36Sopenharmony_ci if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) { 286862306a36Sopenharmony_ci /* No file extent items in the subvolume tree. */ 286962306a36Sopenharmony_ci *last_extent_end_ret = 0; 287062306a36Sopenharmony_ci return 0; 287162306a36Sopenharmony_ci } 287262306a36Sopenharmony_ci 287362306a36Sopenharmony_ci /* 287462306a36Sopenharmony_ci * For an inline extent, the disk_bytenr is where inline data starts at, 287562306a36Sopenharmony_ci * so first check if we have an inline extent item before checking if we 287662306a36Sopenharmony_ci * have an implicit hole (disk_bytenr == 0). 287762306a36Sopenharmony_ci */ 287862306a36Sopenharmony_ci ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); 287962306a36Sopenharmony_ci if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_INLINE) { 288062306a36Sopenharmony_ci *last_extent_end_ret = btrfs_file_extent_end(path); 288162306a36Sopenharmony_ci return 0; 288262306a36Sopenharmony_ci } 288362306a36Sopenharmony_ci 288462306a36Sopenharmony_ci /* 288562306a36Sopenharmony_ci * Find the last file extent item that is not a hole (when NO_HOLES is 288662306a36Sopenharmony_ci * not enabled). This should take at most 2 iterations in the worst 288762306a36Sopenharmony_ci * case: we have one hole file extent item at slot 0 of a leaf and 288862306a36Sopenharmony_ci * another hole file extent item as the last item in the previous leaf. 288962306a36Sopenharmony_ci * This is because we merge file extent items that represent holes. 289062306a36Sopenharmony_ci */ 289162306a36Sopenharmony_ci disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); 289262306a36Sopenharmony_ci while (disk_bytenr == 0) { 289362306a36Sopenharmony_ci ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY); 289462306a36Sopenharmony_ci if (ret < 0) { 289562306a36Sopenharmony_ci return ret; 289662306a36Sopenharmony_ci } else if (ret > 0) { 289762306a36Sopenharmony_ci /* No file extent items that are not holes. */ 289862306a36Sopenharmony_ci *last_extent_end_ret = 0; 289962306a36Sopenharmony_ci return 0; 290062306a36Sopenharmony_ci } 290162306a36Sopenharmony_ci leaf = path->nodes[0]; 290262306a36Sopenharmony_ci ei = btrfs_item_ptr(leaf, path->slots[0], 290362306a36Sopenharmony_ci struct btrfs_file_extent_item); 290462306a36Sopenharmony_ci disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); 290562306a36Sopenharmony_ci } 290662306a36Sopenharmony_ci 290762306a36Sopenharmony_ci *last_extent_end_ret = btrfs_file_extent_end(path); 290862306a36Sopenharmony_ci return 0; 290962306a36Sopenharmony_ci} 291062306a36Sopenharmony_ci 291162306a36Sopenharmony_ciint extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo, 291262306a36Sopenharmony_ci u64 start, u64 len) 291362306a36Sopenharmony_ci{ 291462306a36Sopenharmony_ci const u64 ino = btrfs_ino(inode); 291562306a36Sopenharmony_ci struct extent_state *cached_state = NULL; 291662306a36Sopenharmony_ci struct extent_state *delalloc_cached_state = NULL; 291762306a36Sopenharmony_ci struct btrfs_path *path; 291862306a36Sopenharmony_ci struct fiemap_cache cache = { 0 }; 291962306a36Sopenharmony_ci struct btrfs_backref_share_check_ctx *backref_ctx; 292062306a36Sopenharmony_ci u64 last_extent_end; 292162306a36Sopenharmony_ci u64 prev_extent_end; 292262306a36Sopenharmony_ci u64 lockstart; 292362306a36Sopenharmony_ci u64 lockend; 292462306a36Sopenharmony_ci bool stopped = false; 292562306a36Sopenharmony_ci int ret; 292662306a36Sopenharmony_ci 292762306a36Sopenharmony_ci backref_ctx = btrfs_alloc_backref_share_check_ctx(); 292862306a36Sopenharmony_ci path = btrfs_alloc_path(); 292962306a36Sopenharmony_ci if (!backref_ctx || !path) { 293062306a36Sopenharmony_ci ret = -ENOMEM; 293162306a36Sopenharmony_ci goto out; 293262306a36Sopenharmony_ci } 293362306a36Sopenharmony_ci 293462306a36Sopenharmony_ci lockstart = round_down(start, inode->root->fs_info->sectorsize); 293562306a36Sopenharmony_ci lockend = round_up(start + len, inode->root->fs_info->sectorsize); 293662306a36Sopenharmony_ci prev_extent_end = lockstart; 293762306a36Sopenharmony_ci 293862306a36Sopenharmony_ci btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED); 293962306a36Sopenharmony_ci lock_extent(&inode->io_tree, lockstart, lockend, &cached_state); 294062306a36Sopenharmony_ci 294162306a36Sopenharmony_ci ret = fiemap_find_last_extent_offset(inode, path, &last_extent_end); 294262306a36Sopenharmony_ci if (ret < 0) 294362306a36Sopenharmony_ci goto out_unlock; 294462306a36Sopenharmony_ci btrfs_release_path(path); 294562306a36Sopenharmony_ci 294662306a36Sopenharmony_ci path->reada = READA_FORWARD; 294762306a36Sopenharmony_ci ret = fiemap_search_slot(inode, path, lockstart); 294862306a36Sopenharmony_ci if (ret < 0) { 294962306a36Sopenharmony_ci goto out_unlock; 295062306a36Sopenharmony_ci } else if (ret > 0) { 295162306a36Sopenharmony_ci /* 295262306a36Sopenharmony_ci * No file extent item found, but we may have delalloc between 295362306a36Sopenharmony_ci * the current offset and i_size. So check for that. 295462306a36Sopenharmony_ci */ 295562306a36Sopenharmony_ci ret = 0; 295662306a36Sopenharmony_ci goto check_eof_delalloc; 295762306a36Sopenharmony_ci } 295862306a36Sopenharmony_ci 295962306a36Sopenharmony_ci while (prev_extent_end < lockend) { 296062306a36Sopenharmony_ci struct extent_buffer *leaf = path->nodes[0]; 296162306a36Sopenharmony_ci struct btrfs_file_extent_item *ei; 296262306a36Sopenharmony_ci struct btrfs_key key; 296362306a36Sopenharmony_ci u64 extent_end; 296462306a36Sopenharmony_ci u64 extent_len; 296562306a36Sopenharmony_ci u64 extent_offset = 0; 296662306a36Sopenharmony_ci u64 extent_gen; 296762306a36Sopenharmony_ci u64 disk_bytenr = 0; 296862306a36Sopenharmony_ci u64 flags = 0; 296962306a36Sopenharmony_ci int extent_type; 297062306a36Sopenharmony_ci u8 compression; 297162306a36Sopenharmony_ci 297262306a36Sopenharmony_ci btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 297362306a36Sopenharmony_ci if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) 297462306a36Sopenharmony_ci break; 297562306a36Sopenharmony_ci 297662306a36Sopenharmony_ci extent_end = btrfs_file_extent_end(path); 297762306a36Sopenharmony_ci 297862306a36Sopenharmony_ci /* 297962306a36Sopenharmony_ci * The first iteration can leave us at an extent item that ends 298062306a36Sopenharmony_ci * before our range's start. Move to the next item. 298162306a36Sopenharmony_ci */ 298262306a36Sopenharmony_ci if (extent_end <= lockstart) 298362306a36Sopenharmony_ci goto next_item; 298462306a36Sopenharmony_ci 298562306a36Sopenharmony_ci backref_ctx->curr_leaf_bytenr = leaf->start; 298662306a36Sopenharmony_ci 298762306a36Sopenharmony_ci /* We have in implicit hole (NO_HOLES feature enabled). */ 298862306a36Sopenharmony_ci if (prev_extent_end < key.offset) { 298962306a36Sopenharmony_ci const u64 range_end = min(key.offset, lockend) - 1; 299062306a36Sopenharmony_ci 299162306a36Sopenharmony_ci ret = fiemap_process_hole(inode, fieinfo, &cache, 299262306a36Sopenharmony_ci &delalloc_cached_state, 299362306a36Sopenharmony_ci backref_ctx, 0, 0, 0, 299462306a36Sopenharmony_ci prev_extent_end, range_end); 299562306a36Sopenharmony_ci if (ret < 0) { 299662306a36Sopenharmony_ci goto out_unlock; 299762306a36Sopenharmony_ci } else if (ret > 0) { 299862306a36Sopenharmony_ci /* fiemap_fill_next_extent() told us to stop. */ 299962306a36Sopenharmony_ci stopped = true; 300062306a36Sopenharmony_ci break; 300162306a36Sopenharmony_ci } 300262306a36Sopenharmony_ci 300362306a36Sopenharmony_ci /* We've reached the end of the fiemap range, stop. */ 300462306a36Sopenharmony_ci if (key.offset >= lockend) { 300562306a36Sopenharmony_ci stopped = true; 300662306a36Sopenharmony_ci break; 300762306a36Sopenharmony_ci } 300862306a36Sopenharmony_ci } 300962306a36Sopenharmony_ci 301062306a36Sopenharmony_ci extent_len = extent_end - key.offset; 301162306a36Sopenharmony_ci ei = btrfs_item_ptr(leaf, path->slots[0], 301262306a36Sopenharmony_ci struct btrfs_file_extent_item); 301362306a36Sopenharmony_ci compression = btrfs_file_extent_compression(leaf, ei); 301462306a36Sopenharmony_ci extent_type = btrfs_file_extent_type(leaf, ei); 301562306a36Sopenharmony_ci extent_gen = btrfs_file_extent_generation(leaf, ei); 301662306a36Sopenharmony_ci 301762306a36Sopenharmony_ci if (extent_type != BTRFS_FILE_EXTENT_INLINE) { 301862306a36Sopenharmony_ci disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); 301962306a36Sopenharmony_ci if (compression == BTRFS_COMPRESS_NONE) 302062306a36Sopenharmony_ci extent_offset = btrfs_file_extent_offset(leaf, ei); 302162306a36Sopenharmony_ci } 302262306a36Sopenharmony_ci 302362306a36Sopenharmony_ci if (compression != BTRFS_COMPRESS_NONE) 302462306a36Sopenharmony_ci flags |= FIEMAP_EXTENT_ENCODED; 302562306a36Sopenharmony_ci 302662306a36Sopenharmony_ci if (extent_type == BTRFS_FILE_EXTENT_INLINE) { 302762306a36Sopenharmony_ci flags |= FIEMAP_EXTENT_DATA_INLINE; 302862306a36Sopenharmony_ci flags |= FIEMAP_EXTENT_NOT_ALIGNED; 302962306a36Sopenharmony_ci ret = emit_fiemap_extent(fieinfo, &cache, key.offset, 0, 303062306a36Sopenharmony_ci extent_len, flags); 303162306a36Sopenharmony_ci } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { 303262306a36Sopenharmony_ci ret = fiemap_process_hole(inode, fieinfo, &cache, 303362306a36Sopenharmony_ci &delalloc_cached_state, 303462306a36Sopenharmony_ci backref_ctx, 303562306a36Sopenharmony_ci disk_bytenr, extent_offset, 303662306a36Sopenharmony_ci extent_gen, key.offset, 303762306a36Sopenharmony_ci extent_end - 1); 303862306a36Sopenharmony_ci } else if (disk_bytenr == 0) { 303962306a36Sopenharmony_ci /* We have an explicit hole. */ 304062306a36Sopenharmony_ci ret = fiemap_process_hole(inode, fieinfo, &cache, 304162306a36Sopenharmony_ci &delalloc_cached_state, 304262306a36Sopenharmony_ci backref_ctx, 0, 0, 0, 304362306a36Sopenharmony_ci key.offset, extent_end - 1); 304462306a36Sopenharmony_ci } else { 304562306a36Sopenharmony_ci /* We have a regular extent. */ 304662306a36Sopenharmony_ci if (fieinfo->fi_extents_max) { 304762306a36Sopenharmony_ci ret = btrfs_is_data_extent_shared(inode, 304862306a36Sopenharmony_ci disk_bytenr, 304962306a36Sopenharmony_ci extent_gen, 305062306a36Sopenharmony_ci backref_ctx); 305162306a36Sopenharmony_ci if (ret < 0) 305262306a36Sopenharmony_ci goto out_unlock; 305362306a36Sopenharmony_ci else if (ret > 0) 305462306a36Sopenharmony_ci flags |= FIEMAP_EXTENT_SHARED; 305562306a36Sopenharmony_ci } 305662306a36Sopenharmony_ci 305762306a36Sopenharmony_ci ret = emit_fiemap_extent(fieinfo, &cache, key.offset, 305862306a36Sopenharmony_ci disk_bytenr + extent_offset, 305962306a36Sopenharmony_ci extent_len, flags); 306062306a36Sopenharmony_ci } 306162306a36Sopenharmony_ci 306262306a36Sopenharmony_ci if (ret < 0) { 306362306a36Sopenharmony_ci goto out_unlock; 306462306a36Sopenharmony_ci } else if (ret > 0) { 306562306a36Sopenharmony_ci /* fiemap_fill_next_extent() told us to stop. */ 306662306a36Sopenharmony_ci stopped = true; 306762306a36Sopenharmony_ci break; 306862306a36Sopenharmony_ci } 306962306a36Sopenharmony_ci 307062306a36Sopenharmony_ci prev_extent_end = extent_end; 307162306a36Sopenharmony_cinext_item: 307262306a36Sopenharmony_ci if (fatal_signal_pending(current)) { 307362306a36Sopenharmony_ci ret = -EINTR; 307462306a36Sopenharmony_ci goto out_unlock; 307562306a36Sopenharmony_ci } 307662306a36Sopenharmony_ci 307762306a36Sopenharmony_ci ret = fiemap_next_leaf_item(inode, path); 307862306a36Sopenharmony_ci if (ret < 0) { 307962306a36Sopenharmony_ci goto out_unlock; 308062306a36Sopenharmony_ci } else if (ret > 0) { 308162306a36Sopenharmony_ci /* No more file extent items for this inode. */ 308262306a36Sopenharmony_ci break; 308362306a36Sopenharmony_ci } 308462306a36Sopenharmony_ci cond_resched(); 308562306a36Sopenharmony_ci } 308662306a36Sopenharmony_ci 308762306a36Sopenharmony_cicheck_eof_delalloc: 308862306a36Sopenharmony_ci /* 308962306a36Sopenharmony_ci * Release (and free) the path before emitting any final entries to 309062306a36Sopenharmony_ci * fiemap_fill_next_extent() to keep lockdep happy. This is because 309162306a36Sopenharmony_ci * once we find no more file extent items exist, we may have a 309262306a36Sopenharmony_ci * non-cloned leaf, and fiemap_fill_next_extent() can trigger page 309362306a36Sopenharmony_ci * faults when copying data to the user space buffer. 309462306a36Sopenharmony_ci */ 309562306a36Sopenharmony_ci btrfs_free_path(path); 309662306a36Sopenharmony_ci path = NULL; 309762306a36Sopenharmony_ci 309862306a36Sopenharmony_ci if (!stopped && prev_extent_end < lockend) { 309962306a36Sopenharmony_ci ret = fiemap_process_hole(inode, fieinfo, &cache, 310062306a36Sopenharmony_ci &delalloc_cached_state, backref_ctx, 310162306a36Sopenharmony_ci 0, 0, 0, prev_extent_end, lockend - 1); 310262306a36Sopenharmony_ci if (ret < 0) 310362306a36Sopenharmony_ci goto out_unlock; 310462306a36Sopenharmony_ci prev_extent_end = lockend; 310562306a36Sopenharmony_ci } 310662306a36Sopenharmony_ci 310762306a36Sopenharmony_ci if (cache.cached && cache.offset + cache.len >= last_extent_end) { 310862306a36Sopenharmony_ci const u64 i_size = i_size_read(&inode->vfs_inode); 310962306a36Sopenharmony_ci 311062306a36Sopenharmony_ci if (prev_extent_end < i_size) { 311162306a36Sopenharmony_ci u64 delalloc_start; 311262306a36Sopenharmony_ci u64 delalloc_end; 311362306a36Sopenharmony_ci bool delalloc; 311462306a36Sopenharmony_ci 311562306a36Sopenharmony_ci delalloc = btrfs_find_delalloc_in_range(inode, 311662306a36Sopenharmony_ci prev_extent_end, 311762306a36Sopenharmony_ci i_size - 1, 311862306a36Sopenharmony_ci &delalloc_cached_state, 311962306a36Sopenharmony_ci &delalloc_start, 312062306a36Sopenharmony_ci &delalloc_end); 312162306a36Sopenharmony_ci if (!delalloc) 312262306a36Sopenharmony_ci cache.flags |= FIEMAP_EXTENT_LAST; 312362306a36Sopenharmony_ci } else { 312462306a36Sopenharmony_ci cache.flags |= FIEMAP_EXTENT_LAST; 312562306a36Sopenharmony_ci } 312662306a36Sopenharmony_ci } 312762306a36Sopenharmony_ci 312862306a36Sopenharmony_ci ret = emit_last_fiemap_cache(fieinfo, &cache); 312962306a36Sopenharmony_ci 313062306a36Sopenharmony_ciout_unlock: 313162306a36Sopenharmony_ci unlock_extent(&inode->io_tree, lockstart, lockend, &cached_state); 313262306a36Sopenharmony_ci btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); 313362306a36Sopenharmony_ciout: 313462306a36Sopenharmony_ci free_extent_state(delalloc_cached_state); 313562306a36Sopenharmony_ci btrfs_free_backref_share_ctx(backref_ctx); 313662306a36Sopenharmony_ci btrfs_free_path(path); 313762306a36Sopenharmony_ci return ret; 313862306a36Sopenharmony_ci} 313962306a36Sopenharmony_ci 314062306a36Sopenharmony_cistatic void __free_extent_buffer(struct extent_buffer *eb) 314162306a36Sopenharmony_ci{ 314262306a36Sopenharmony_ci kmem_cache_free(extent_buffer_cache, eb); 314362306a36Sopenharmony_ci} 314462306a36Sopenharmony_ci 314562306a36Sopenharmony_cistatic int extent_buffer_under_io(const struct extent_buffer *eb) 314662306a36Sopenharmony_ci{ 314762306a36Sopenharmony_ci return (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || 314862306a36Sopenharmony_ci test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 314962306a36Sopenharmony_ci} 315062306a36Sopenharmony_ci 315162306a36Sopenharmony_cistatic bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page) 315262306a36Sopenharmony_ci{ 315362306a36Sopenharmony_ci struct btrfs_subpage *subpage; 315462306a36Sopenharmony_ci 315562306a36Sopenharmony_ci lockdep_assert_held(&page->mapping->private_lock); 315662306a36Sopenharmony_ci 315762306a36Sopenharmony_ci if (PagePrivate(page)) { 315862306a36Sopenharmony_ci subpage = (struct btrfs_subpage *)page->private; 315962306a36Sopenharmony_ci if (atomic_read(&subpage->eb_refs)) 316062306a36Sopenharmony_ci return true; 316162306a36Sopenharmony_ci /* 316262306a36Sopenharmony_ci * Even there is no eb refs here, we may still have 316362306a36Sopenharmony_ci * end_page_read() call relying on page::private. 316462306a36Sopenharmony_ci */ 316562306a36Sopenharmony_ci if (atomic_read(&subpage->readers)) 316662306a36Sopenharmony_ci return true; 316762306a36Sopenharmony_ci } 316862306a36Sopenharmony_ci return false; 316962306a36Sopenharmony_ci} 317062306a36Sopenharmony_ci 317162306a36Sopenharmony_cistatic void detach_extent_buffer_page(struct extent_buffer *eb, struct page *page) 317262306a36Sopenharmony_ci{ 317362306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 317462306a36Sopenharmony_ci const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); 317562306a36Sopenharmony_ci 317662306a36Sopenharmony_ci /* 317762306a36Sopenharmony_ci * For mapped eb, we're going to change the page private, which should 317862306a36Sopenharmony_ci * be done under the private_lock. 317962306a36Sopenharmony_ci */ 318062306a36Sopenharmony_ci if (mapped) 318162306a36Sopenharmony_ci spin_lock(&page->mapping->private_lock); 318262306a36Sopenharmony_ci 318362306a36Sopenharmony_ci if (!PagePrivate(page)) { 318462306a36Sopenharmony_ci if (mapped) 318562306a36Sopenharmony_ci spin_unlock(&page->mapping->private_lock); 318662306a36Sopenharmony_ci return; 318762306a36Sopenharmony_ci } 318862306a36Sopenharmony_ci 318962306a36Sopenharmony_ci if (fs_info->nodesize >= PAGE_SIZE) { 319062306a36Sopenharmony_ci /* 319162306a36Sopenharmony_ci * We do this since we'll remove the pages after we've 319262306a36Sopenharmony_ci * removed the eb from the radix tree, so we could race 319362306a36Sopenharmony_ci * and have this page now attached to the new eb. So 319462306a36Sopenharmony_ci * only clear page_private if it's still connected to 319562306a36Sopenharmony_ci * this eb. 319662306a36Sopenharmony_ci */ 319762306a36Sopenharmony_ci if (PagePrivate(page) && 319862306a36Sopenharmony_ci page->private == (unsigned long)eb) { 319962306a36Sopenharmony_ci BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 320062306a36Sopenharmony_ci BUG_ON(PageDirty(page)); 320162306a36Sopenharmony_ci BUG_ON(PageWriteback(page)); 320262306a36Sopenharmony_ci /* 320362306a36Sopenharmony_ci * We need to make sure we haven't be attached 320462306a36Sopenharmony_ci * to a new eb. 320562306a36Sopenharmony_ci */ 320662306a36Sopenharmony_ci detach_page_private(page); 320762306a36Sopenharmony_ci } 320862306a36Sopenharmony_ci if (mapped) 320962306a36Sopenharmony_ci spin_unlock(&page->mapping->private_lock); 321062306a36Sopenharmony_ci return; 321162306a36Sopenharmony_ci } 321262306a36Sopenharmony_ci 321362306a36Sopenharmony_ci /* 321462306a36Sopenharmony_ci * For subpage, we can have dummy eb with page private. In this case, 321562306a36Sopenharmony_ci * we can directly detach the private as such page is only attached to 321662306a36Sopenharmony_ci * one dummy eb, no sharing. 321762306a36Sopenharmony_ci */ 321862306a36Sopenharmony_ci if (!mapped) { 321962306a36Sopenharmony_ci btrfs_detach_subpage(fs_info, page); 322062306a36Sopenharmony_ci return; 322162306a36Sopenharmony_ci } 322262306a36Sopenharmony_ci 322362306a36Sopenharmony_ci btrfs_page_dec_eb_refs(fs_info, page); 322462306a36Sopenharmony_ci 322562306a36Sopenharmony_ci /* 322662306a36Sopenharmony_ci * We can only detach the page private if there are no other ebs in the 322762306a36Sopenharmony_ci * page range and no unfinished IO. 322862306a36Sopenharmony_ci */ 322962306a36Sopenharmony_ci if (!page_range_has_eb(fs_info, page)) 323062306a36Sopenharmony_ci btrfs_detach_subpage(fs_info, page); 323162306a36Sopenharmony_ci 323262306a36Sopenharmony_ci spin_unlock(&page->mapping->private_lock); 323362306a36Sopenharmony_ci} 323462306a36Sopenharmony_ci 323562306a36Sopenharmony_ci/* Release all pages attached to the extent buffer */ 323662306a36Sopenharmony_cistatic void btrfs_release_extent_buffer_pages(struct extent_buffer *eb) 323762306a36Sopenharmony_ci{ 323862306a36Sopenharmony_ci int i; 323962306a36Sopenharmony_ci int num_pages; 324062306a36Sopenharmony_ci 324162306a36Sopenharmony_ci ASSERT(!extent_buffer_under_io(eb)); 324262306a36Sopenharmony_ci 324362306a36Sopenharmony_ci num_pages = num_extent_pages(eb); 324462306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 324562306a36Sopenharmony_ci struct page *page = eb->pages[i]; 324662306a36Sopenharmony_ci 324762306a36Sopenharmony_ci if (!page) 324862306a36Sopenharmony_ci continue; 324962306a36Sopenharmony_ci 325062306a36Sopenharmony_ci detach_extent_buffer_page(eb, page); 325162306a36Sopenharmony_ci 325262306a36Sopenharmony_ci /* One for when we allocated the page */ 325362306a36Sopenharmony_ci put_page(page); 325462306a36Sopenharmony_ci } 325562306a36Sopenharmony_ci} 325662306a36Sopenharmony_ci 325762306a36Sopenharmony_ci/* 325862306a36Sopenharmony_ci * Helper for releasing the extent buffer. 325962306a36Sopenharmony_ci */ 326062306a36Sopenharmony_cistatic inline void btrfs_release_extent_buffer(struct extent_buffer *eb) 326162306a36Sopenharmony_ci{ 326262306a36Sopenharmony_ci btrfs_release_extent_buffer_pages(eb); 326362306a36Sopenharmony_ci btrfs_leak_debug_del_eb(eb); 326462306a36Sopenharmony_ci __free_extent_buffer(eb); 326562306a36Sopenharmony_ci} 326662306a36Sopenharmony_ci 326762306a36Sopenharmony_cistatic struct extent_buffer * 326862306a36Sopenharmony_ci__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, 326962306a36Sopenharmony_ci unsigned long len) 327062306a36Sopenharmony_ci{ 327162306a36Sopenharmony_ci struct extent_buffer *eb = NULL; 327262306a36Sopenharmony_ci 327362306a36Sopenharmony_ci eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); 327462306a36Sopenharmony_ci eb->start = start; 327562306a36Sopenharmony_ci eb->len = len; 327662306a36Sopenharmony_ci eb->fs_info = fs_info; 327762306a36Sopenharmony_ci init_rwsem(&eb->lock); 327862306a36Sopenharmony_ci 327962306a36Sopenharmony_ci btrfs_leak_debug_add_eb(eb); 328062306a36Sopenharmony_ci 328162306a36Sopenharmony_ci spin_lock_init(&eb->refs_lock); 328262306a36Sopenharmony_ci atomic_set(&eb->refs, 1); 328362306a36Sopenharmony_ci 328462306a36Sopenharmony_ci ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE); 328562306a36Sopenharmony_ci 328662306a36Sopenharmony_ci return eb; 328762306a36Sopenharmony_ci} 328862306a36Sopenharmony_ci 328962306a36Sopenharmony_cistruct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src) 329062306a36Sopenharmony_ci{ 329162306a36Sopenharmony_ci int i; 329262306a36Sopenharmony_ci struct extent_buffer *new; 329362306a36Sopenharmony_ci int num_pages = num_extent_pages(src); 329462306a36Sopenharmony_ci int ret; 329562306a36Sopenharmony_ci 329662306a36Sopenharmony_ci new = __alloc_extent_buffer(src->fs_info, src->start, src->len); 329762306a36Sopenharmony_ci if (new == NULL) 329862306a36Sopenharmony_ci return NULL; 329962306a36Sopenharmony_ci 330062306a36Sopenharmony_ci /* 330162306a36Sopenharmony_ci * Set UNMAPPED before calling btrfs_release_extent_buffer(), as 330262306a36Sopenharmony_ci * btrfs_release_extent_buffer() have different behavior for 330362306a36Sopenharmony_ci * UNMAPPED subpage extent buffer. 330462306a36Sopenharmony_ci */ 330562306a36Sopenharmony_ci set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags); 330662306a36Sopenharmony_ci 330762306a36Sopenharmony_ci ret = btrfs_alloc_page_array(num_pages, new->pages); 330862306a36Sopenharmony_ci if (ret) { 330962306a36Sopenharmony_ci btrfs_release_extent_buffer(new); 331062306a36Sopenharmony_ci return NULL; 331162306a36Sopenharmony_ci } 331262306a36Sopenharmony_ci 331362306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 331462306a36Sopenharmony_ci int ret; 331562306a36Sopenharmony_ci struct page *p = new->pages[i]; 331662306a36Sopenharmony_ci 331762306a36Sopenharmony_ci ret = attach_extent_buffer_page(new, p, NULL); 331862306a36Sopenharmony_ci if (ret < 0) { 331962306a36Sopenharmony_ci btrfs_release_extent_buffer(new); 332062306a36Sopenharmony_ci return NULL; 332162306a36Sopenharmony_ci } 332262306a36Sopenharmony_ci WARN_ON(PageDirty(p)); 332362306a36Sopenharmony_ci } 332462306a36Sopenharmony_ci copy_extent_buffer_full(new, src); 332562306a36Sopenharmony_ci set_extent_buffer_uptodate(new); 332662306a36Sopenharmony_ci 332762306a36Sopenharmony_ci return new; 332862306a36Sopenharmony_ci} 332962306a36Sopenharmony_ci 333062306a36Sopenharmony_cistruct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, 333162306a36Sopenharmony_ci u64 start, unsigned long len) 333262306a36Sopenharmony_ci{ 333362306a36Sopenharmony_ci struct extent_buffer *eb; 333462306a36Sopenharmony_ci int num_pages; 333562306a36Sopenharmony_ci int i; 333662306a36Sopenharmony_ci int ret; 333762306a36Sopenharmony_ci 333862306a36Sopenharmony_ci eb = __alloc_extent_buffer(fs_info, start, len); 333962306a36Sopenharmony_ci if (!eb) 334062306a36Sopenharmony_ci return NULL; 334162306a36Sopenharmony_ci 334262306a36Sopenharmony_ci num_pages = num_extent_pages(eb); 334362306a36Sopenharmony_ci ret = btrfs_alloc_page_array(num_pages, eb->pages); 334462306a36Sopenharmony_ci if (ret) 334562306a36Sopenharmony_ci goto err; 334662306a36Sopenharmony_ci 334762306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 334862306a36Sopenharmony_ci struct page *p = eb->pages[i]; 334962306a36Sopenharmony_ci 335062306a36Sopenharmony_ci ret = attach_extent_buffer_page(eb, p, NULL); 335162306a36Sopenharmony_ci if (ret < 0) 335262306a36Sopenharmony_ci goto err; 335362306a36Sopenharmony_ci } 335462306a36Sopenharmony_ci 335562306a36Sopenharmony_ci set_extent_buffer_uptodate(eb); 335662306a36Sopenharmony_ci btrfs_set_header_nritems(eb, 0); 335762306a36Sopenharmony_ci set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); 335862306a36Sopenharmony_ci 335962306a36Sopenharmony_ci return eb; 336062306a36Sopenharmony_cierr: 336162306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 336262306a36Sopenharmony_ci if (eb->pages[i]) { 336362306a36Sopenharmony_ci detach_extent_buffer_page(eb, eb->pages[i]); 336462306a36Sopenharmony_ci __free_page(eb->pages[i]); 336562306a36Sopenharmony_ci } 336662306a36Sopenharmony_ci } 336762306a36Sopenharmony_ci __free_extent_buffer(eb); 336862306a36Sopenharmony_ci return NULL; 336962306a36Sopenharmony_ci} 337062306a36Sopenharmony_ci 337162306a36Sopenharmony_cistruct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, 337262306a36Sopenharmony_ci u64 start) 337362306a36Sopenharmony_ci{ 337462306a36Sopenharmony_ci return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize); 337562306a36Sopenharmony_ci} 337662306a36Sopenharmony_ci 337762306a36Sopenharmony_cistatic void check_buffer_tree_ref(struct extent_buffer *eb) 337862306a36Sopenharmony_ci{ 337962306a36Sopenharmony_ci int refs; 338062306a36Sopenharmony_ci /* 338162306a36Sopenharmony_ci * The TREE_REF bit is first set when the extent_buffer is added 338262306a36Sopenharmony_ci * to the radix tree. It is also reset, if unset, when a new reference 338362306a36Sopenharmony_ci * is created by find_extent_buffer. 338462306a36Sopenharmony_ci * 338562306a36Sopenharmony_ci * It is only cleared in two cases: freeing the last non-tree 338662306a36Sopenharmony_ci * reference to the extent_buffer when its STALE bit is set or 338762306a36Sopenharmony_ci * calling release_folio when the tree reference is the only reference. 338862306a36Sopenharmony_ci * 338962306a36Sopenharmony_ci * In both cases, care is taken to ensure that the extent_buffer's 339062306a36Sopenharmony_ci * pages are not under io. However, release_folio can be concurrently 339162306a36Sopenharmony_ci * called with creating new references, which is prone to race 339262306a36Sopenharmony_ci * conditions between the calls to check_buffer_tree_ref in those 339362306a36Sopenharmony_ci * codepaths and clearing TREE_REF in try_release_extent_buffer. 339462306a36Sopenharmony_ci * 339562306a36Sopenharmony_ci * The actual lifetime of the extent_buffer in the radix tree is 339662306a36Sopenharmony_ci * adequately protected by the refcount, but the TREE_REF bit and 339762306a36Sopenharmony_ci * its corresponding reference are not. To protect against this 339862306a36Sopenharmony_ci * class of races, we call check_buffer_tree_ref from the codepaths 339962306a36Sopenharmony_ci * which trigger io. Note that once io is initiated, TREE_REF can no 340062306a36Sopenharmony_ci * longer be cleared, so that is the moment at which any such race is 340162306a36Sopenharmony_ci * best fixed. 340262306a36Sopenharmony_ci */ 340362306a36Sopenharmony_ci refs = atomic_read(&eb->refs); 340462306a36Sopenharmony_ci if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) 340562306a36Sopenharmony_ci return; 340662306a36Sopenharmony_ci 340762306a36Sopenharmony_ci spin_lock(&eb->refs_lock); 340862306a36Sopenharmony_ci if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) 340962306a36Sopenharmony_ci atomic_inc(&eb->refs); 341062306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 341162306a36Sopenharmony_ci} 341262306a36Sopenharmony_ci 341362306a36Sopenharmony_cistatic void mark_extent_buffer_accessed(struct extent_buffer *eb, 341462306a36Sopenharmony_ci struct page *accessed) 341562306a36Sopenharmony_ci{ 341662306a36Sopenharmony_ci int num_pages, i; 341762306a36Sopenharmony_ci 341862306a36Sopenharmony_ci check_buffer_tree_ref(eb); 341962306a36Sopenharmony_ci 342062306a36Sopenharmony_ci num_pages = num_extent_pages(eb); 342162306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 342262306a36Sopenharmony_ci struct page *p = eb->pages[i]; 342362306a36Sopenharmony_ci 342462306a36Sopenharmony_ci if (p != accessed) 342562306a36Sopenharmony_ci mark_page_accessed(p); 342662306a36Sopenharmony_ci } 342762306a36Sopenharmony_ci} 342862306a36Sopenharmony_ci 342962306a36Sopenharmony_cistruct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, 343062306a36Sopenharmony_ci u64 start) 343162306a36Sopenharmony_ci{ 343262306a36Sopenharmony_ci struct extent_buffer *eb; 343362306a36Sopenharmony_ci 343462306a36Sopenharmony_ci eb = find_extent_buffer_nolock(fs_info, start); 343562306a36Sopenharmony_ci if (!eb) 343662306a36Sopenharmony_ci return NULL; 343762306a36Sopenharmony_ci /* 343862306a36Sopenharmony_ci * Lock our eb's refs_lock to avoid races with free_extent_buffer(). 343962306a36Sopenharmony_ci * When we get our eb it might be flagged with EXTENT_BUFFER_STALE and 344062306a36Sopenharmony_ci * another task running free_extent_buffer() might have seen that flag 344162306a36Sopenharmony_ci * set, eb->refs == 2, that the buffer isn't under IO (dirty and 344262306a36Sopenharmony_ci * writeback flags not set) and it's still in the tree (flag 344362306a36Sopenharmony_ci * EXTENT_BUFFER_TREE_REF set), therefore being in the process of 344462306a36Sopenharmony_ci * decrementing the extent buffer's reference count twice. So here we 344562306a36Sopenharmony_ci * could race and increment the eb's reference count, clear its stale 344662306a36Sopenharmony_ci * flag, mark it as dirty and drop our reference before the other task 344762306a36Sopenharmony_ci * finishes executing free_extent_buffer, which would later result in 344862306a36Sopenharmony_ci * an attempt to free an extent buffer that is dirty. 344962306a36Sopenharmony_ci */ 345062306a36Sopenharmony_ci if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { 345162306a36Sopenharmony_ci spin_lock(&eb->refs_lock); 345262306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 345362306a36Sopenharmony_ci } 345462306a36Sopenharmony_ci mark_extent_buffer_accessed(eb, NULL); 345562306a36Sopenharmony_ci return eb; 345662306a36Sopenharmony_ci} 345762306a36Sopenharmony_ci 345862306a36Sopenharmony_ci#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 345962306a36Sopenharmony_cistruct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, 346062306a36Sopenharmony_ci u64 start) 346162306a36Sopenharmony_ci{ 346262306a36Sopenharmony_ci struct extent_buffer *eb, *exists = NULL; 346362306a36Sopenharmony_ci int ret; 346462306a36Sopenharmony_ci 346562306a36Sopenharmony_ci eb = find_extent_buffer(fs_info, start); 346662306a36Sopenharmony_ci if (eb) 346762306a36Sopenharmony_ci return eb; 346862306a36Sopenharmony_ci eb = alloc_dummy_extent_buffer(fs_info, start); 346962306a36Sopenharmony_ci if (!eb) 347062306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 347162306a36Sopenharmony_ci eb->fs_info = fs_info; 347262306a36Sopenharmony_ciagain: 347362306a36Sopenharmony_ci ret = radix_tree_preload(GFP_NOFS); 347462306a36Sopenharmony_ci if (ret) { 347562306a36Sopenharmony_ci exists = ERR_PTR(ret); 347662306a36Sopenharmony_ci goto free_eb; 347762306a36Sopenharmony_ci } 347862306a36Sopenharmony_ci spin_lock(&fs_info->buffer_lock); 347962306a36Sopenharmony_ci ret = radix_tree_insert(&fs_info->buffer_radix, 348062306a36Sopenharmony_ci start >> fs_info->sectorsize_bits, eb); 348162306a36Sopenharmony_ci spin_unlock(&fs_info->buffer_lock); 348262306a36Sopenharmony_ci radix_tree_preload_end(); 348362306a36Sopenharmony_ci if (ret == -EEXIST) { 348462306a36Sopenharmony_ci exists = find_extent_buffer(fs_info, start); 348562306a36Sopenharmony_ci if (exists) 348662306a36Sopenharmony_ci goto free_eb; 348762306a36Sopenharmony_ci else 348862306a36Sopenharmony_ci goto again; 348962306a36Sopenharmony_ci } 349062306a36Sopenharmony_ci check_buffer_tree_ref(eb); 349162306a36Sopenharmony_ci set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); 349262306a36Sopenharmony_ci 349362306a36Sopenharmony_ci return eb; 349462306a36Sopenharmony_cifree_eb: 349562306a36Sopenharmony_ci btrfs_release_extent_buffer(eb); 349662306a36Sopenharmony_ci return exists; 349762306a36Sopenharmony_ci} 349862306a36Sopenharmony_ci#endif 349962306a36Sopenharmony_ci 350062306a36Sopenharmony_cistatic struct extent_buffer *grab_extent_buffer( 350162306a36Sopenharmony_ci struct btrfs_fs_info *fs_info, struct page *page) 350262306a36Sopenharmony_ci{ 350362306a36Sopenharmony_ci struct extent_buffer *exists; 350462306a36Sopenharmony_ci 350562306a36Sopenharmony_ci /* 350662306a36Sopenharmony_ci * For subpage case, we completely rely on radix tree to ensure we 350762306a36Sopenharmony_ci * don't try to insert two ebs for the same bytenr. So here we always 350862306a36Sopenharmony_ci * return NULL and just continue. 350962306a36Sopenharmony_ci */ 351062306a36Sopenharmony_ci if (fs_info->nodesize < PAGE_SIZE) 351162306a36Sopenharmony_ci return NULL; 351262306a36Sopenharmony_ci 351362306a36Sopenharmony_ci /* Page not yet attached to an extent buffer */ 351462306a36Sopenharmony_ci if (!PagePrivate(page)) 351562306a36Sopenharmony_ci return NULL; 351662306a36Sopenharmony_ci 351762306a36Sopenharmony_ci /* 351862306a36Sopenharmony_ci * We could have already allocated an eb for this page and attached one 351962306a36Sopenharmony_ci * so lets see if we can get a ref on the existing eb, and if we can we 352062306a36Sopenharmony_ci * know it's good and we can just return that one, else we know we can 352162306a36Sopenharmony_ci * just overwrite page->private. 352262306a36Sopenharmony_ci */ 352362306a36Sopenharmony_ci exists = (struct extent_buffer *)page->private; 352462306a36Sopenharmony_ci if (atomic_inc_not_zero(&exists->refs)) 352562306a36Sopenharmony_ci return exists; 352662306a36Sopenharmony_ci 352762306a36Sopenharmony_ci WARN_ON(PageDirty(page)); 352862306a36Sopenharmony_ci detach_page_private(page); 352962306a36Sopenharmony_ci return NULL; 353062306a36Sopenharmony_ci} 353162306a36Sopenharmony_ci 353262306a36Sopenharmony_cistatic int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) 353362306a36Sopenharmony_ci{ 353462306a36Sopenharmony_ci if (!IS_ALIGNED(start, fs_info->sectorsize)) { 353562306a36Sopenharmony_ci btrfs_err(fs_info, "bad tree block start %llu", start); 353662306a36Sopenharmony_ci return -EINVAL; 353762306a36Sopenharmony_ci } 353862306a36Sopenharmony_ci 353962306a36Sopenharmony_ci if (fs_info->nodesize < PAGE_SIZE && 354062306a36Sopenharmony_ci offset_in_page(start) + fs_info->nodesize > PAGE_SIZE) { 354162306a36Sopenharmony_ci btrfs_err(fs_info, 354262306a36Sopenharmony_ci "tree block crosses page boundary, start %llu nodesize %u", 354362306a36Sopenharmony_ci start, fs_info->nodesize); 354462306a36Sopenharmony_ci return -EINVAL; 354562306a36Sopenharmony_ci } 354662306a36Sopenharmony_ci if (fs_info->nodesize >= PAGE_SIZE && 354762306a36Sopenharmony_ci !PAGE_ALIGNED(start)) { 354862306a36Sopenharmony_ci btrfs_err(fs_info, 354962306a36Sopenharmony_ci "tree block is not page aligned, start %llu nodesize %u", 355062306a36Sopenharmony_ci start, fs_info->nodesize); 355162306a36Sopenharmony_ci return -EINVAL; 355262306a36Sopenharmony_ci } 355362306a36Sopenharmony_ci return 0; 355462306a36Sopenharmony_ci} 355562306a36Sopenharmony_ci 355662306a36Sopenharmony_cistruct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, 355762306a36Sopenharmony_ci u64 start, u64 owner_root, int level) 355862306a36Sopenharmony_ci{ 355962306a36Sopenharmony_ci unsigned long len = fs_info->nodesize; 356062306a36Sopenharmony_ci int num_pages; 356162306a36Sopenharmony_ci int i; 356262306a36Sopenharmony_ci unsigned long index = start >> PAGE_SHIFT; 356362306a36Sopenharmony_ci struct extent_buffer *eb; 356462306a36Sopenharmony_ci struct extent_buffer *exists = NULL; 356562306a36Sopenharmony_ci struct page *p; 356662306a36Sopenharmony_ci struct address_space *mapping = fs_info->btree_inode->i_mapping; 356762306a36Sopenharmony_ci struct btrfs_subpage *prealloc = NULL; 356862306a36Sopenharmony_ci u64 lockdep_owner = owner_root; 356962306a36Sopenharmony_ci int uptodate = 1; 357062306a36Sopenharmony_ci int ret; 357162306a36Sopenharmony_ci 357262306a36Sopenharmony_ci if (check_eb_alignment(fs_info, start)) 357362306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 357462306a36Sopenharmony_ci 357562306a36Sopenharmony_ci#if BITS_PER_LONG == 32 357662306a36Sopenharmony_ci if (start >= MAX_LFS_FILESIZE) { 357762306a36Sopenharmony_ci btrfs_err_rl(fs_info, 357862306a36Sopenharmony_ci "extent buffer %llu is beyond 32bit page cache limit", start); 357962306a36Sopenharmony_ci btrfs_err_32bit_limit(fs_info); 358062306a36Sopenharmony_ci return ERR_PTR(-EOVERFLOW); 358162306a36Sopenharmony_ci } 358262306a36Sopenharmony_ci if (start >= BTRFS_32BIT_EARLY_WARN_THRESHOLD) 358362306a36Sopenharmony_ci btrfs_warn_32bit_limit(fs_info); 358462306a36Sopenharmony_ci#endif 358562306a36Sopenharmony_ci 358662306a36Sopenharmony_ci eb = find_extent_buffer(fs_info, start); 358762306a36Sopenharmony_ci if (eb) 358862306a36Sopenharmony_ci return eb; 358962306a36Sopenharmony_ci 359062306a36Sopenharmony_ci eb = __alloc_extent_buffer(fs_info, start, len); 359162306a36Sopenharmony_ci if (!eb) 359262306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 359362306a36Sopenharmony_ci 359462306a36Sopenharmony_ci /* 359562306a36Sopenharmony_ci * The reloc trees are just snapshots, so we need them to appear to be 359662306a36Sopenharmony_ci * just like any other fs tree WRT lockdep. 359762306a36Sopenharmony_ci */ 359862306a36Sopenharmony_ci if (lockdep_owner == BTRFS_TREE_RELOC_OBJECTID) 359962306a36Sopenharmony_ci lockdep_owner = BTRFS_FS_TREE_OBJECTID; 360062306a36Sopenharmony_ci 360162306a36Sopenharmony_ci btrfs_set_buffer_lockdep_class(lockdep_owner, eb, level); 360262306a36Sopenharmony_ci 360362306a36Sopenharmony_ci num_pages = num_extent_pages(eb); 360462306a36Sopenharmony_ci 360562306a36Sopenharmony_ci /* 360662306a36Sopenharmony_ci * Preallocate page->private for subpage case, so that we won't 360762306a36Sopenharmony_ci * allocate memory with private_lock nor page lock hold. 360862306a36Sopenharmony_ci * 360962306a36Sopenharmony_ci * The memory will be freed by attach_extent_buffer_page() or freed 361062306a36Sopenharmony_ci * manually if we exit earlier. 361162306a36Sopenharmony_ci */ 361262306a36Sopenharmony_ci if (fs_info->nodesize < PAGE_SIZE) { 361362306a36Sopenharmony_ci prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA); 361462306a36Sopenharmony_ci if (IS_ERR(prealloc)) { 361562306a36Sopenharmony_ci exists = ERR_CAST(prealloc); 361662306a36Sopenharmony_ci goto free_eb; 361762306a36Sopenharmony_ci } 361862306a36Sopenharmony_ci } 361962306a36Sopenharmony_ci 362062306a36Sopenharmony_ci for (i = 0; i < num_pages; i++, index++) { 362162306a36Sopenharmony_ci p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); 362262306a36Sopenharmony_ci if (!p) { 362362306a36Sopenharmony_ci exists = ERR_PTR(-ENOMEM); 362462306a36Sopenharmony_ci btrfs_free_subpage(prealloc); 362562306a36Sopenharmony_ci goto free_eb; 362662306a36Sopenharmony_ci } 362762306a36Sopenharmony_ci 362862306a36Sopenharmony_ci spin_lock(&mapping->private_lock); 362962306a36Sopenharmony_ci exists = grab_extent_buffer(fs_info, p); 363062306a36Sopenharmony_ci if (exists) { 363162306a36Sopenharmony_ci spin_unlock(&mapping->private_lock); 363262306a36Sopenharmony_ci unlock_page(p); 363362306a36Sopenharmony_ci put_page(p); 363462306a36Sopenharmony_ci mark_extent_buffer_accessed(exists, p); 363562306a36Sopenharmony_ci btrfs_free_subpage(prealloc); 363662306a36Sopenharmony_ci goto free_eb; 363762306a36Sopenharmony_ci } 363862306a36Sopenharmony_ci /* Should not fail, as we have preallocated the memory */ 363962306a36Sopenharmony_ci ret = attach_extent_buffer_page(eb, p, prealloc); 364062306a36Sopenharmony_ci ASSERT(!ret); 364162306a36Sopenharmony_ci /* 364262306a36Sopenharmony_ci * To inform we have extra eb under allocation, so that 364362306a36Sopenharmony_ci * detach_extent_buffer_page() won't release the page private 364462306a36Sopenharmony_ci * when the eb hasn't yet been inserted into radix tree. 364562306a36Sopenharmony_ci * 364662306a36Sopenharmony_ci * The ref will be decreased when the eb released the page, in 364762306a36Sopenharmony_ci * detach_extent_buffer_page(). 364862306a36Sopenharmony_ci * Thus needs no special handling in error path. 364962306a36Sopenharmony_ci */ 365062306a36Sopenharmony_ci btrfs_page_inc_eb_refs(fs_info, p); 365162306a36Sopenharmony_ci spin_unlock(&mapping->private_lock); 365262306a36Sopenharmony_ci 365362306a36Sopenharmony_ci WARN_ON(btrfs_page_test_dirty(fs_info, p, eb->start, eb->len)); 365462306a36Sopenharmony_ci eb->pages[i] = p; 365562306a36Sopenharmony_ci if (!btrfs_page_test_uptodate(fs_info, p, eb->start, eb->len)) 365662306a36Sopenharmony_ci uptodate = 0; 365762306a36Sopenharmony_ci 365862306a36Sopenharmony_ci /* 365962306a36Sopenharmony_ci * We can't unlock the pages just yet since the extent buffer 366062306a36Sopenharmony_ci * hasn't been properly inserted in the radix tree, this 366162306a36Sopenharmony_ci * opens a race with btree_release_folio which can free a page 366262306a36Sopenharmony_ci * while we are still filling in all pages for the buffer and 366362306a36Sopenharmony_ci * we could crash. 366462306a36Sopenharmony_ci */ 366562306a36Sopenharmony_ci } 366662306a36Sopenharmony_ci if (uptodate) 366762306a36Sopenharmony_ci set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 366862306a36Sopenharmony_ciagain: 366962306a36Sopenharmony_ci ret = radix_tree_preload(GFP_NOFS); 367062306a36Sopenharmony_ci if (ret) { 367162306a36Sopenharmony_ci exists = ERR_PTR(ret); 367262306a36Sopenharmony_ci goto free_eb; 367362306a36Sopenharmony_ci } 367462306a36Sopenharmony_ci 367562306a36Sopenharmony_ci spin_lock(&fs_info->buffer_lock); 367662306a36Sopenharmony_ci ret = radix_tree_insert(&fs_info->buffer_radix, 367762306a36Sopenharmony_ci start >> fs_info->sectorsize_bits, eb); 367862306a36Sopenharmony_ci spin_unlock(&fs_info->buffer_lock); 367962306a36Sopenharmony_ci radix_tree_preload_end(); 368062306a36Sopenharmony_ci if (ret == -EEXIST) { 368162306a36Sopenharmony_ci exists = find_extent_buffer(fs_info, start); 368262306a36Sopenharmony_ci if (exists) 368362306a36Sopenharmony_ci goto free_eb; 368462306a36Sopenharmony_ci else 368562306a36Sopenharmony_ci goto again; 368662306a36Sopenharmony_ci } 368762306a36Sopenharmony_ci /* add one reference for the tree */ 368862306a36Sopenharmony_ci check_buffer_tree_ref(eb); 368962306a36Sopenharmony_ci set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); 369062306a36Sopenharmony_ci 369162306a36Sopenharmony_ci /* 369262306a36Sopenharmony_ci * Now it's safe to unlock the pages because any calls to 369362306a36Sopenharmony_ci * btree_release_folio will correctly detect that a page belongs to a 369462306a36Sopenharmony_ci * live buffer and won't free them prematurely. 369562306a36Sopenharmony_ci */ 369662306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) 369762306a36Sopenharmony_ci unlock_page(eb->pages[i]); 369862306a36Sopenharmony_ci return eb; 369962306a36Sopenharmony_ci 370062306a36Sopenharmony_cifree_eb: 370162306a36Sopenharmony_ci WARN_ON(!atomic_dec_and_test(&eb->refs)); 370262306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 370362306a36Sopenharmony_ci if (eb->pages[i]) 370462306a36Sopenharmony_ci unlock_page(eb->pages[i]); 370562306a36Sopenharmony_ci } 370662306a36Sopenharmony_ci 370762306a36Sopenharmony_ci btrfs_release_extent_buffer(eb); 370862306a36Sopenharmony_ci return exists; 370962306a36Sopenharmony_ci} 371062306a36Sopenharmony_ci 371162306a36Sopenharmony_cistatic inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) 371262306a36Sopenharmony_ci{ 371362306a36Sopenharmony_ci struct extent_buffer *eb = 371462306a36Sopenharmony_ci container_of(head, struct extent_buffer, rcu_head); 371562306a36Sopenharmony_ci 371662306a36Sopenharmony_ci __free_extent_buffer(eb); 371762306a36Sopenharmony_ci} 371862306a36Sopenharmony_ci 371962306a36Sopenharmony_cistatic int release_extent_buffer(struct extent_buffer *eb) 372062306a36Sopenharmony_ci __releases(&eb->refs_lock) 372162306a36Sopenharmony_ci{ 372262306a36Sopenharmony_ci lockdep_assert_held(&eb->refs_lock); 372362306a36Sopenharmony_ci 372462306a36Sopenharmony_ci WARN_ON(atomic_read(&eb->refs) == 0); 372562306a36Sopenharmony_ci if (atomic_dec_and_test(&eb->refs)) { 372662306a36Sopenharmony_ci if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { 372762306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 372862306a36Sopenharmony_ci 372962306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 373062306a36Sopenharmony_ci 373162306a36Sopenharmony_ci spin_lock(&fs_info->buffer_lock); 373262306a36Sopenharmony_ci radix_tree_delete(&fs_info->buffer_radix, 373362306a36Sopenharmony_ci eb->start >> fs_info->sectorsize_bits); 373462306a36Sopenharmony_ci spin_unlock(&fs_info->buffer_lock); 373562306a36Sopenharmony_ci } else { 373662306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 373762306a36Sopenharmony_ci } 373862306a36Sopenharmony_ci 373962306a36Sopenharmony_ci btrfs_leak_debug_del_eb(eb); 374062306a36Sopenharmony_ci /* Should be safe to release our pages at this point */ 374162306a36Sopenharmony_ci btrfs_release_extent_buffer_pages(eb); 374262306a36Sopenharmony_ci#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 374362306a36Sopenharmony_ci if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) { 374462306a36Sopenharmony_ci __free_extent_buffer(eb); 374562306a36Sopenharmony_ci return 1; 374662306a36Sopenharmony_ci } 374762306a36Sopenharmony_ci#endif 374862306a36Sopenharmony_ci call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); 374962306a36Sopenharmony_ci return 1; 375062306a36Sopenharmony_ci } 375162306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 375262306a36Sopenharmony_ci 375362306a36Sopenharmony_ci return 0; 375462306a36Sopenharmony_ci} 375562306a36Sopenharmony_ci 375662306a36Sopenharmony_civoid free_extent_buffer(struct extent_buffer *eb) 375762306a36Sopenharmony_ci{ 375862306a36Sopenharmony_ci int refs; 375962306a36Sopenharmony_ci if (!eb) 376062306a36Sopenharmony_ci return; 376162306a36Sopenharmony_ci 376262306a36Sopenharmony_ci refs = atomic_read(&eb->refs); 376362306a36Sopenharmony_ci while (1) { 376462306a36Sopenharmony_ci if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3) 376562306a36Sopenharmony_ci || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && 376662306a36Sopenharmony_ci refs == 1)) 376762306a36Sopenharmony_ci break; 376862306a36Sopenharmony_ci if (atomic_try_cmpxchg(&eb->refs, &refs, refs - 1)) 376962306a36Sopenharmony_ci return; 377062306a36Sopenharmony_ci } 377162306a36Sopenharmony_ci 377262306a36Sopenharmony_ci spin_lock(&eb->refs_lock); 377362306a36Sopenharmony_ci if (atomic_read(&eb->refs) == 2 && 377462306a36Sopenharmony_ci test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && 377562306a36Sopenharmony_ci !extent_buffer_under_io(eb) && 377662306a36Sopenharmony_ci test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) 377762306a36Sopenharmony_ci atomic_dec(&eb->refs); 377862306a36Sopenharmony_ci 377962306a36Sopenharmony_ci /* 378062306a36Sopenharmony_ci * I know this is terrible, but it's temporary until we stop tracking 378162306a36Sopenharmony_ci * the uptodate bits and such for the extent buffers. 378262306a36Sopenharmony_ci */ 378362306a36Sopenharmony_ci release_extent_buffer(eb); 378462306a36Sopenharmony_ci} 378562306a36Sopenharmony_ci 378662306a36Sopenharmony_civoid free_extent_buffer_stale(struct extent_buffer *eb) 378762306a36Sopenharmony_ci{ 378862306a36Sopenharmony_ci if (!eb) 378962306a36Sopenharmony_ci return; 379062306a36Sopenharmony_ci 379162306a36Sopenharmony_ci spin_lock(&eb->refs_lock); 379262306a36Sopenharmony_ci set_bit(EXTENT_BUFFER_STALE, &eb->bflags); 379362306a36Sopenharmony_ci 379462306a36Sopenharmony_ci if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && 379562306a36Sopenharmony_ci test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) 379662306a36Sopenharmony_ci atomic_dec(&eb->refs); 379762306a36Sopenharmony_ci release_extent_buffer(eb); 379862306a36Sopenharmony_ci} 379962306a36Sopenharmony_ci 380062306a36Sopenharmony_cistatic void btree_clear_page_dirty(struct page *page) 380162306a36Sopenharmony_ci{ 380262306a36Sopenharmony_ci ASSERT(PageDirty(page)); 380362306a36Sopenharmony_ci ASSERT(PageLocked(page)); 380462306a36Sopenharmony_ci clear_page_dirty_for_io(page); 380562306a36Sopenharmony_ci xa_lock_irq(&page->mapping->i_pages); 380662306a36Sopenharmony_ci if (!PageDirty(page)) 380762306a36Sopenharmony_ci __xa_clear_mark(&page->mapping->i_pages, 380862306a36Sopenharmony_ci page_index(page), PAGECACHE_TAG_DIRTY); 380962306a36Sopenharmony_ci xa_unlock_irq(&page->mapping->i_pages); 381062306a36Sopenharmony_ci} 381162306a36Sopenharmony_ci 381262306a36Sopenharmony_cistatic void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb) 381362306a36Sopenharmony_ci{ 381462306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 381562306a36Sopenharmony_ci struct page *page = eb->pages[0]; 381662306a36Sopenharmony_ci bool last; 381762306a36Sopenharmony_ci 381862306a36Sopenharmony_ci /* btree_clear_page_dirty() needs page locked */ 381962306a36Sopenharmony_ci lock_page(page); 382062306a36Sopenharmony_ci last = btrfs_subpage_clear_and_test_dirty(fs_info, page, eb->start, 382162306a36Sopenharmony_ci eb->len); 382262306a36Sopenharmony_ci if (last) 382362306a36Sopenharmony_ci btree_clear_page_dirty(page); 382462306a36Sopenharmony_ci unlock_page(page); 382562306a36Sopenharmony_ci WARN_ON(atomic_read(&eb->refs) == 0); 382662306a36Sopenharmony_ci} 382762306a36Sopenharmony_ci 382862306a36Sopenharmony_civoid btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans, 382962306a36Sopenharmony_ci struct extent_buffer *eb) 383062306a36Sopenharmony_ci{ 383162306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 383262306a36Sopenharmony_ci int i; 383362306a36Sopenharmony_ci int num_pages; 383462306a36Sopenharmony_ci struct page *page; 383562306a36Sopenharmony_ci 383662306a36Sopenharmony_ci btrfs_assert_tree_write_locked(eb); 383762306a36Sopenharmony_ci 383862306a36Sopenharmony_ci if (trans && btrfs_header_generation(eb) != trans->transid) 383962306a36Sopenharmony_ci return; 384062306a36Sopenharmony_ci 384162306a36Sopenharmony_ci if (!test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) 384262306a36Sopenharmony_ci return; 384362306a36Sopenharmony_ci 384462306a36Sopenharmony_ci percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, -eb->len, 384562306a36Sopenharmony_ci fs_info->dirty_metadata_batch); 384662306a36Sopenharmony_ci 384762306a36Sopenharmony_ci if (eb->fs_info->nodesize < PAGE_SIZE) 384862306a36Sopenharmony_ci return clear_subpage_extent_buffer_dirty(eb); 384962306a36Sopenharmony_ci 385062306a36Sopenharmony_ci num_pages = num_extent_pages(eb); 385162306a36Sopenharmony_ci 385262306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 385362306a36Sopenharmony_ci page = eb->pages[i]; 385462306a36Sopenharmony_ci if (!PageDirty(page)) 385562306a36Sopenharmony_ci continue; 385662306a36Sopenharmony_ci lock_page(page); 385762306a36Sopenharmony_ci btree_clear_page_dirty(page); 385862306a36Sopenharmony_ci unlock_page(page); 385962306a36Sopenharmony_ci } 386062306a36Sopenharmony_ci WARN_ON(atomic_read(&eb->refs) == 0); 386162306a36Sopenharmony_ci} 386262306a36Sopenharmony_ci 386362306a36Sopenharmony_civoid set_extent_buffer_dirty(struct extent_buffer *eb) 386462306a36Sopenharmony_ci{ 386562306a36Sopenharmony_ci int i; 386662306a36Sopenharmony_ci int num_pages; 386762306a36Sopenharmony_ci bool was_dirty; 386862306a36Sopenharmony_ci 386962306a36Sopenharmony_ci check_buffer_tree_ref(eb); 387062306a36Sopenharmony_ci 387162306a36Sopenharmony_ci was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); 387262306a36Sopenharmony_ci 387362306a36Sopenharmony_ci num_pages = num_extent_pages(eb); 387462306a36Sopenharmony_ci WARN_ON(atomic_read(&eb->refs) == 0); 387562306a36Sopenharmony_ci WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); 387662306a36Sopenharmony_ci 387762306a36Sopenharmony_ci if (!was_dirty) { 387862306a36Sopenharmony_ci bool subpage = eb->fs_info->nodesize < PAGE_SIZE; 387962306a36Sopenharmony_ci 388062306a36Sopenharmony_ci /* 388162306a36Sopenharmony_ci * For subpage case, we can have other extent buffers in the 388262306a36Sopenharmony_ci * same page, and in clear_subpage_extent_buffer_dirty() we 388362306a36Sopenharmony_ci * have to clear page dirty without subpage lock held. 388462306a36Sopenharmony_ci * This can cause race where our page gets dirty cleared after 388562306a36Sopenharmony_ci * we just set it. 388662306a36Sopenharmony_ci * 388762306a36Sopenharmony_ci * Thankfully, clear_subpage_extent_buffer_dirty() has locked 388862306a36Sopenharmony_ci * its page for other reasons, we can use page lock to prevent 388962306a36Sopenharmony_ci * the above race. 389062306a36Sopenharmony_ci */ 389162306a36Sopenharmony_ci if (subpage) 389262306a36Sopenharmony_ci lock_page(eb->pages[0]); 389362306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) 389462306a36Sopenharmony_ci btrfs_page_set_dirty(eb->fs_info, eb->pages[i], 389562306a36Sopenharmony_ci eb->start, eb->len); 389662306a36Sopenharmony_ci if (subpage) 389762306a36Sopenharmony_ci unlock_page(eb->pages[0]); 389862306a36Sopenharmony_ci percpu_counter_add_batch(&eb->fs_info->dirty_metadata_bytes, 389962306a36Sopenharmony_ci eb->len, 390062306a36Sopenharmony_ci eb->fs_info->dirty_metadata_batch); 390162306a36Sopenharmony_ci } 390262306a36Sopenharmony_ci#ifdef CONFIG_BTRFS_DEBUG 390362306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) 390462306a36Sopenharmony_ci ASSERT(PageDirty(eb->pages[i])); 390562306a36Sopenharmony_ci#endif 390662306a36Sopenharmony_ci} 390762306a36Sopenharmony_ci 390862306a36Sopenharmony_civoid clear_extent_buffer_uptodate(struct extent_buffer *eb) 390962306a36Sopenharmony_ci{ 391062306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 391162306a36Sopenharmony_ci struct page *page; 391262306a36Sopenharmony_ci int num_pages; 391362306a36Sopenharmony_ci int i; 391462306a36Sopenharmony_ci 391562306a36Sopenharmony_ci clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 391662306a36Sopenharmony_ci num_pages = num_extent_pages(eb); 391762306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 391862306a36Sopenharmony_ci page = eb->pages[i]; 391962306a36Sopenharmony_ci if (!page) 392062306a36Sopenharmony_ci continue; 392162306a36Sopenharmony_ci 392262306a36Sopenharmony_ci /* 392362306a36Sopenharmony_ci * This is special handling for metadata subpage, as regular 392462306a36Sopenharmony_ci * btrfs_is_subpage() can not handle cloned/dummy metadata. 392562306a36Sopenharmony_ci */ 392662306a36Sopenharmony_ci if (fs_info->nodesize >= PAGE_SIZE) 392762306a36Sopenharmony_ci ClearPageUptodate(page); 392862306a36Sopenharmony_ci else 392962306a36Sopenharmony_ci btrfs_subpage_clear_uptodate(fs_info, page, eb->start, 393062306a36Sopenharmony_ci eb->len); 393162306a36Sopenharmony_ci } 393262306a36Sopenharmony_ci} 393362306a36Sopenharmony_ci 393462306a36Sopenharmony_civoid set_extent_buffer_uptodate(struct extent_buffer *eb) 393562306a36Sopenharmony_ci{ 393662306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 393762306a36Sopenharmony_ci struct page *page; 393862306a36Sopenharmony_ci int num_pages; 393962306a36Sopenharmony_ci int i; 394062306a36Sopenharmony_ci 394162306a36Sopenharmony_ci set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 394262306a36Sopenharmony_ci num_pages = num_extent_pages(eb); 394362306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 394462306a36Sopenharmony_ci page = eb->pages[i]; 394562306a36Sopenharmony_ci 394662306a36Sopenharmony_ci /* 394762306a36Sopenharmony_ci * This is special handling for metadata subpage, as regular 394862306a36Sopenharmony_ci * btrfs_is_subpage() can not handle cloned/dummy metadata. 394962306a36Sopenharmony_ci */ 395062306a36Sopenharmony_ci if (fs_info->nodesize >= PAGE_SIZE) 395162306a36Sopenharmony_ci SetPageUptodate(page); 395262306a36Sopenharmony_ci else 395362306a36Sopenharmony_ci btrfs_subpage_set_uptodate(fs_info, page, eb->start, 395462306a36Sopenharmony_ci eb->len); 395562306a36Sopenharmony_ci } 395662306a36Sopenharmony_ci} 395762306a36Sopenharmony_ci 395862306a36Sopenharmony_cistatic void extent_buffer_read_end_io(struct btrfs_bio *bbio) 395962306a36Sopenharmony_ci{ 396062306a36Sopenharmony_ci struct extent_buffer *eb = bbio->private; 396162306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 396262306a36Sopenharmony_ci bool uptodate = !bbio->bio.bi_status; 396362306a36Sopenharmony_ci struct bvec_iter_all iter_all; 396462306a36Sopenharmony_ci struct bio_vec *bvec; 396562306a36Sopenharmony_ci u32 bio_offset = 0; 396662306a36Sopenharmony_ci 396762306a36Sopenharmony_ci eb->read_mirror = bbio->mirror_num; 396862306a36Sopenharmony_ci 396962306a36Sopenharmony_ci if (uptodate && 397062306a36Sopenharmony_ci btrfs_validate_extent_buffer(eb, &bbio->parent_check) < 0) 397162306a36Sopenharmony_ci uptodate = false; 397262306a36Sopenharmony_ci 397362306a36Sopenharmony_ci if (uptodate) { 397462306a36Sopenharmony_ci set_extent_buffer_uptodate(eb); 397562306a36Sopenharmony_ci } else { 397662306a36Sopenharmony_ci clear_extent_buffer_uptodate(eb); 397762306a36Sopenharmony_ci set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 397862306a36Sopenharmony_ci } 397962306a36Sopenharmony_ci 398062306a36Sopenharmony_ci bio_for_each_segment_all(bvec, &bbio->bio, iter_all) { 398162306a36Sopenharmony_ci u64 start = eb->start + bio_offset; 398262306a36Sopenharmony_ci struct page *page = bvec->bv_page; 398362306a36Sopenharmony_ci u32 len = bvec->bv_len; 398462306a36Sopenharmony_ci 398562306a36Sopenharmony_ci if (uptodate) 398662306a36Sopenharmony_ci btrfs_page_set_uptodate(fs_info, page, start, len); 398762306a36Sopenharmony_ci else 398862306a36Sopenharmony_ci btrfs_page_clear_uptodate(fs_info, page, start, len); 398962306a36Sopenharmony_ci 399062306a36Sopenharmony_ci bio_offset += len; 399162306a36Sopenharmony_ci } 399262306a36Sopenharmony_ci 399362306a36Sopenharmony_ci clear_bit(EXTENT_BUFFER_READING, &eb->bflags); 399462306a36Sopenharmony_ci smp_mb__after_atomic(); 399562306a36Sopenharmony_ci wake_up_bit(&eb->bflags, EXTENT_BUFFER_READING); 399662306a36Sopenharmony_ci free_extent_buffer(eb); 399762306a36Sopenharmony_ci 399862306a36Sopenharmony_ci bio_put(&bbio->bio); 399962306a36Sopenharmony_ci} 400062306a36Sopenharmony_ci 400162306a36Sopenharmony_ciint read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num, 400262306a36Sopenharmony_ci struct btrfs_tree_parent_check *check) 400362306a36Sopenharmony_ci{ 400462306a36Sopenharmony_ci int num_pages = num_extent_pages(eb), i; 400562306a36Sopenharmony_ci struct btrfs_bio *bbio; 400662306a36Sopenharmony_ci 400762306a36Sopenharmony_ci if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) 400862306a36Sopenharmony_ci return 0; 400962306a36Sopenharmony_ci 401062306a36Sopenharmony_ci /* 401162306a36Sopenharmony_ci * We could have had EXTENT_BUFFER_UPTODATE cleared by the write 401262306a36Sopenharmony_ci * operation, which could potentially still be in flight. In this case 401362306a36Sopenharmony_ci * we simply want to return an error. 401462306a36Sopenharmony_ci */ 401562306a36Sopenharmony_ci if (unlikely(test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))) 401662306a36Sopenharmony_ci return -EIO; 401762306a36Sopenharmony_ci 401862306a36Sopenharmony_ci /* Someone else is already reading the buffer, just wait for it. */ 401962306a36Sopenharmony_ci if (test_and_set_bit(EXTENT_BUFFER_READING, &eb->bflags)) 402062306a36Sopenharmony_ci goto done; 402162306a36Sopenharmony_ci 402262306a36Sopenharmony_ci clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 402362306a36Sopenharmony_ci eb->read_mirror = 0; 402462306a36Sopenharmony_ci check_buffer_tree_ref(eb); 402562306a36Sopenharmony_ci atomic_inc(&eb->refs); 402662306a36Sopenharmony_ci 402762306a36Sopenharmony_ci bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES, 402862306a36Sopenharmony_ci REQ_OP_READ | REQ_META, eb->fs_info, 402962306a36Sopenharmony_ci extent_buffer_read_end_io, eb); 403062306a36Sopenharmony_ci bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT; 403162306a36Sopenharmony_ci bbio->inode = BTRFS_I(eb->fs_info->btree_inode); 403262306a36Sopenharmony_ci bbio->file_offset = eb->start; 403362306a36Sopenharmony_ci memcpy(&bbio->parent_check, check, sizeof(*check)); 403462306a36Sopenharmony_ci if (eb->fs_info->nodesize < PAGE_SIZE) { 403562306a36Sopenharmony_ci __bio_add_page(&bbio->bio, eb->pages[0], eb->len, 403662306a36Sopenharmony_ci eb->start - page_offset(eb->pages[0])); 403762306a36Sopenharmony_ci } else { 403862306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) 403962306a36Sopenharmony_ci __bio_add_page(&bbio->bio, eb->pages[i], PAGE_SIZE, 0); 404062306a36Sopenharmony_ci } 404162306a36Sopenharmony_ci btrfs_submit_bio(bbio, mirror_num); 404262306a36Sopenharmony_ci 404362306a36Sopenharmony_cidone: 404462306a36Sopenharmony_ci if (wait == WAIT_COMPLETE) { 404562306a36Sopenharmony_ci wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_READING, TASK_UNINTERRUPTIBLE); 404662306a36Sopenharmony_ci if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) 404762306a36Sopenharmony_ci return -EIO; 404862306a36Sopenharmony_ci } 404962306a36Sopenharmony_ci 405062306a36Sopenharmony_ci return 0; 405162306a36Sopenharmony_ci} 405262306a36Sopenharmony_ci 405362306a36Sopenharmony_cistatic bool report_eb_range(const struct extent_buffer *eb, unsigned long start, 405462306a36Sopenharmony_ci unsigned long len) 405562306a36Sopenharmony_ci{ 405662306a36Sopenharmony_ci btrfs_warn(eb->fs_info, 405762306a36Sopenharmony_ci "access to eb bytenr %llu len %lu out of range start %lu len %lu", 405862306a36Sopenharmony_ci eb->start, eb->len, start, len); 405962306a36Sopenharmony_ci WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 406062306a36Sopenharmony_ci 406162306a36Sopenharmony_ci return true; 406262306a36Sopenharmony_ci} 406362306a36Sopenharmony_ci 406462306a36Sopenharmony_ci/* 406562306a36Sopenharmony_ci * Check if the [start, start + len) range is valid before reading/writing 406662306a36Sopenharmony_ci * the eb. 406762306a36Sopenharmony_ci * NOTE: @start and @len are offset inside the eb, not logical address. 406862306a36Sopenharmony_ci * 406962306a36Sopenharmony_ci * Caller should not touch the dst/src memory if this function returns error. 407062306a36Sopenharmony_ci */ 407162306a36Sopenharmony_cistatic inline int check_eb_range(const struct extent_buffer *eb, 407262306a36Sopenharmony_ci unsigned long start, unsigned long len) 407362306a36Sopenharmony_ci{ 407462306a36Sopenharmony_ci unsigned long offset; 407562306a36Sopenharmony_ci 407662306a36Sopenharmony_ci /* start, start + len should not go beyond eb->len nor overflow */ 407762306a36Sopenharmony_ci if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len)) 407862306a36Sopenharmony_ci return report_eb_range(eb, start, len); 407962306a36Sopenharmony_ci 408062306a36Sopenharmony_ci return false; 408162306a36Sopenharmony_ci} 408262306a36Sopenharmony_ci 408362306a36Sopenharmony_civoid read_extent_buffer(const struct extent_buffer *eb, void *dstv, 408462306a36Sopenharmony_ci unsigned long start, unsigned long len) 408562306a36Sopenharmony_ci{ 408662306a36Sopenharmony_ci size_t cur; 408762306a36Sopenharmony_ci size_t offset; 408862306a36Sopenharmony_ci struct page *page; 408962306a36Sopenharmony_ci char *kaddr; 409062306a36Sopenharmony_ci char *dst = (char *)dstv; 409162306a36Sopenharmony_ci unsigned long i = get_eb_page_index(start); 409262306a36Sopenharmony_ci 409362306a36Sopenharmony_ci if (check_eb_range(eb, start, len)) { 409462306a36Sopenharmony_ci /* 409562306a36Sopenharmony_ci * Invalid range hit, reset the memory, so callers won't get 409662306a36Sopenharmony_ci * some random garbage for their uninitialzed memory. 409762306a36Sopenharmony_ci */ 409862306a36Sopenharmony_ci memset(dstv, 0, len); 409962306a36Sopenharmony_ci return; 410062306a36Sopenharmony_ci } 410162306a36Sopenharmony_ci 410262306a36Sopenharmony_ci offset = get_eb_offset_in_page(eb, start); 410362306a36Sopenharmony_ci 410462306a36Sopenharmony_ci while (len > 0) { 410562306a36Sopenharmony_ci page = eb->pages[i]; 410662306a36Sopenharmony_ci 410762306a36Sopenharmony_ci cur = min(len, (PAGE_SIZE - offset)); 410862306a36Sopenharmony_ci kaddr = page_address(page); 410962306a36Sopenharmony_ci memcpy(dst, kaddr + offset, cur); 411062306a36Sopenharmony_ci 411162306a36Sopenharmony_ci dst += cur; 411262306a36Sopenharmony_ci len -= cur; 411362306a36Sopenharmony_ci offset = 0; 411462306a36Sopenharmony_ci i++; 411562306a36Sopenharmony_ci } 411662306a36Sopenharmony_ci} 411762306a36Sopenharmony_ci 411862306a36Sopenharmony_ciint read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, 411962306a36Sopenharmony_ci void __user *dstv, 412062306a36Sopenharmony_ci unsigned long start, unsigned long len) 412162306a36Sopenharmony_ci{ 412262306a36Sopenharmony_ci size_t cur; 412362306a36Sopenharmony_ci size_t offset; 412462306a36Sopenharmony_ci struct page *page; 412562306a36Sopenharmony_ci char *kaddr; 412662306a36Sopenharmony_ci char __user *dst = (char __user *)dstv; 412762306a36Sopenharmony_ci unsigned long i = get_eb_page_index(start); 412862306a36Sopenharmony_ci int ret = 0; 412962306a36Sopenharmony_ci 413062306a36Sopenharmony_ci WARN_ON(start > eb->len); 413162306a36Sopenharmony_ci WARN_ON(start + len > eb->start + eb->len); 413262306a36Sopenharmony_ci 413362306a36Sopenharmony_ci offset = get_eb_offset_in_page(eb, start); 413462306a36Sopenharmony_ci 413562306a36Sopenharmony_ci while (len > 0) { 413662306a36Sopenharmony_ci page = eb->pages[i]; 413762306a36Sopenharmony_ci 413862306a36Sopenharmony_ci cur = min(len, (PAGE_SIZE - offset)); 413962306a36Sopenharmony_ci kaddr = page_address(page); 414062306a36Sopenharmony_ci if (copy_to_user_nofault(dst, kaddr + offset, cur)) { 414162306a36Sopenharmony_ci ret = -EFAULT; 414262306a36Sopenharmony_ci break; 414362306a36Sopenharmony_ci } 414462306a36Sopenharmony_ci 414562306a36Sopenharmony_ci dst += cur; 414662306a36Sopenharmony_ci len -= cur; 414762306a36Sopenharmony_ci offset = 0; 414862306a36Sopenharmony_ci i++; 414962306a36Sopenharmony_ci } 415062306a36Sopenharmony_ci 415162306a36Sopenharmony_ci return ret; 415262306a36Sopenharmony_ci} 415362306a36Sopenharmony_ci 415462306a36Sopenharmony_ciint memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, 415562306a36Sopenharmony_ci unsigned long start, unsigned long len) 415662306a36Sopenharmony_ci{ 415762306a36Sopenharmony_ci size_t cur; 415862306a36Sopenharmony_ci size_t offset; 415962306a36Sopenharmony_ci struct page *page; 416062306a36Sopenharmony_ci char *kaddr; 416162306a36Sopenharmony_ci char *ptr = (char *)ptrv; 416262306a36Sopenharmony_ci unsigned long i = get_eb_page_index(start); 416362306a36Sopenharmony_ci int ret = 0; 416462306a36Sopenharmony_ci 416562306a36Sopenharmony_ci if (check_eb_range(eb, start, len)) 416662306a36Sopenharmony_ci return -EINVAL; 416762306a36Sopenharmony_ci 416862306a36Sopenharmony_ci offset = get_eb_offset_in_page(eb, start); 416962306a36Sopenharmony_ci 417062306a36Sopenharmony_ci while (len > 0) { 417162306a36Sopenharmony_ci page = eb->pages[i]; 417262306a36Sopenharmony_ci 417362306a36Sopenharmony_ci cur = min(len, (PAGE_SIZE - offset)); 417462306a36Sopenharmony_ci 417562306a36Sopenharmony_ci kaddr = page_address(page); 417662306a36Sopenharmony_ci ret = memcmp(ptr, kaddr + offset, cur); 417762306a36Sopenharmony_ci if (ret) 417862306a36Sopenharmony_ci break; 417962306a36Sopenharmony_ci 418062306a36Sopenharmony_ci ptr += cur; 418162306a36Sopenharmony_ci len -= cur; 418262306a36Sopenharmony_ci offset = 0; 418362306a36Sopenharmony_ci i++; 418462306a36Sopenharmony_ci } 418562306a36Sopenharmony_ci return ret; 418662306a36Sopenharmony_ci} 418762306a36Sopenharmony_ci 418862306a36Sopenharmony_ci/* 418962306a36Sopenharmony_ci * Check that the extent buffer is uptodate. 419062306a36Sopenharmony_ci * 419162306a36Sopenharmony_ci * For regular sector size == PAGE_SIZE case, check if @page is uptodate. 419262306a36Sopenharmony_ci * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE. 419362306a36Sopenharmony_ci */ 419462306a36Sopenharmony_cistatic void assert_eb_page_uptodate(const struct extent_buffer *eb, 419562306a36Sopenharmony_ci struct page *page) 419662306a36Sopenharmony_ci{ 419762306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = eb->fs_info; 419862306a36Sopenharmony_ci 419962306a36Sopenharmony_ci /* 420062306a36Sopenharmony_ci * If we are using the commit root we could potentially clear a page 420162306a36Sopenharmony_ci * Uptodate while we're using the extent buffer that we've previously 420262306a36Sopenharmony_ci * looked up. We don't want to complain in this case, as the page was 420362306a36Sopenharmony_ci * valid before, we just didn't write it out. Instead we want to catch 420462306a36Sopenharmony_ci * the case where we didn't actually read the block properly, which 420562306a36Sopenharmony_ci * would have !PageUptodate and !EXTENT_BUFFER_WRITE_ERR. 420662306a36Sopenharmony_ci */ 420762306a36Sopenharmony_ci if (test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) 420862306a36Sopenharmony_ci return; 420962306a36Sopenharmony_ci 421062306a36Sopenharmony_ci if (fs_info->nodesize < PAGE_SIZE) { 421162306a36Sopenharmony_ci if (WARN_ON(!btrfs_subpage_test_uptodate(fs_info, page, 421262306a36Sopenharmony_ci eb->start, eb->len))) 421362306a36Sopenharmony_ci btrfs_subpage_dump_bitmap(fs_info, page, eb->start, eb->len); 421462306a36Sopenharmony_ci } else { 421562306a36Sopenharmony_ci WARN_ON(!PageUptodate(page)); 421662306a36Sopenharmony_ci } 421762306a36Sopenharmony_ci} 421862306a36Sopenharmony_ci 421962306a36Sopenharmony_cistatic void __write_extent_buffer(const struct extent_buffer *eb, 422062306a36Sopenharmony_ci const void *srcv, unsigned long start, 422162306a36Sopenharmony_ci unsigned long len, bool use_memmove) 422262306a36Sopenharmony_ci{ 422362306a36Sopenharmony_ci size_t cur; 422462306a36Sopenharmony_ci size_t offset; 422562306a36Sopenharmony_ci struct page *page; 422662306a36Sopenharmony_ci char *kaddr; 422762306a36Sopenharmony_ci char *src = (char *)srcv; 422862306a36Sopenharmony_ci unsigned long i = get_eb_page_index(start); 422962306a36Sopenharmony_ci /* For unmapped (dummy) ebs, no need to check their uptodate status. */ 423062306a36Sopenharmony_ci const bool check_uptodate = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); 423162306a36Sopenharmony_ci 423262306a36Sopenharmony_ci WARN_ON(test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)); 423362306a36Sopenharmony_ci 423462306a36Sopenharmony_ci if (check_eb_range(eb, start, len)) 423562306a36Sopenharmony_ci return; 423662306a36Sopenharmony_ci 423762306a36Sopenharmony_ci offset = get_eb_offset_in_page(eb, start); 423862306a36Sopenharmony_ci 423962306a36Sopenharmony_ci while (len > 0) { 424062306a36Sopenharmony_ci page = eb->pages[i]; 424162306a36Sopenharmony_ci if (check_uptodate) 424262306a36Sopenharmony_ci assert_eb_page_uptodate(eb, page); 424362306a36Sopenharmony_ci 424462306a36Sopenharmony_ci cur = min(len, PAGE_SIZE - offset); 424562306a36Sopenharmony_ci kaddr = page_address(page); 424662306a36Sopenharmony_ci if (use_memmove) 424762306a36Sopenharmony_ci memmove(kaddr + offset, src, cur); 424862306a36Sopenharmony_ci else 424962306a36Sopenharmony_ci memcpy(kaddr + offset, src, cur); 425062306a36Sopenharmony_ci 425162306a36Sopenharmony_ci src += cur; 425262306a36Sopenharmony_ci len -= cur; 425362306a36Sopenharmony_ci offset = 0; 425462306a36Sopenharmony_ci i++; 425562306a36Sopenharmony_ci } 425662306a36Sopenharmony_ci} 425762306a36Sopenharmony_ci 425862306a36Sopenharmony_civoid write_extent_buffer(const struct extent_buffer *eb, const void *srcv, 425962306a36Sopenharmony_ci unsigned long start, unsigned long len) 426062306a36Sopenharmony_ci{ 426162306a36Sopenharmony_ci return __write_extent_buffer(eb, srcv, start, len, false); 426262306a36Sopenharmony_ci} 426362306a36Sopenharmony_ci 426462306a36Sopenharmony_cistatic void memset_extent_buffer(const struct extent_buffer *eb, int c, 426562306a36Sopenharmony_ci unsigned long start, unsigned long len) 426662306a36Sopenharmony_ci{ 426762306a36Sopenharmony_ci unsigned long cur = start; 426862306a36Sopenharmony_ci 426962306a36Sopenharmony_ci while (cur < start + len) { 427062306a36Sopenharmony_ci unsigned long index = get_eb_page_index(cur); 427162306a36Sopenharmony_ci unsigned int offset = get_eb_offset_in_page(eb, cur); 427262306a36Sopenharmony_ci unsigned int cur_len = min(start + len - cur, PAGE_SIZE - offset); 427362306a36Sopenharmony_ci struct page *page = eb->pages[index]; 427462306a36Sopenharmony_ci 427562306a36Sopenharmony_ci assert_eb_page_uptodate(eb, page); 427662306a36Sopenharmony_ci memset(page_address(page) + offset, c, cur_len); 427762306a36Sopenharmony_ci 427862306a36Sopenharmony_ci cur += cur_len; 427962306a36Sopenharmony_ci } 428062306a36Sopenharmony_ci} 428162306a36Sopenharmony_ci 428262306a36Sopenharmony_civoid memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start, 428362306a36Sopenharmony_ci unsigned long len) 428462306a36Sopenharmony_ci{ 428562306a36Sopenharmony_ci if (check_eb_range(eb, start, len)) 428662306a36Sopenharmony_ci return; 428762306a36Sopenharmony_ci return memset_extent_buffer(eb, 0, start, len); 428862306a36Sopenharmony_ci} 428962306a36Sopenharmony_ci 429062306a36Sopenharmony_civoid copy_extent_buffer_full(const struct extent_buffer *dst, 429162306a36Sopenharmony_ci const struct extent_buffer *src) 429262306a36Sopenharmony_ci{ 429362306a36Sopenharmony_ci unsigned long cur = 0; 429462306a36Sopenharmony_ci 429562306a36Sopenharmony_ci ASSERT(dst->len == src->len); 429662306a36Sopenharmony_ci 429762306a36Sopenharmony_ci while (cur < src->len) { 429862306a36Sopenharmony_ci unsigned long index = get_eb_page_index(cur); 429962306a36Sopenharmony_ci unsigned long offset = get_eb_offset_in_page(src, cur); 430062306a36Sopenharmony_ci unsigned long cur_len = min(src->len, PAGE_SIZE - offset); 430162306a36Sopenharmony_ci void *addr = page_address(src->pages[index]) + offset; 430262306a36Sopenharmony_ci 430362306a36Sopenharmony_ci write_extent_buffer(dst, addr, cur, cur_len); 430462306a36Sopenharmony_ci 430562306a36Sopenharmony_ci cur += cur_len; 430662306a36Sopenharmony_ci } 430762306a36Sopenharmony_ci} 430862306a36Sopenharmony_ci 430962306a36Sopenharmony_civoid copy_extent_buffer(const struct extent_buffer *dst, 431062306a36Sopenharmony_ci const struct extent_buffer *src, 431162306a36Sopenharmony_ci unsigned long dst_offset, unsigned long src_offset, 431262306a36Sopenharmony_ci unsigned long len) 431362306a36Sopenharmony_ci{ 431462306a36Sopenharmony_ci u64 dst_len = dst->len; 431562306a36Sopenharmony_ci size_t cur; 431662306a36Sopenharmony_ci size_t offset; 431762306a36Sopenharmony_ci struct page *page; 431862306a36Sopenharmony_ci char *kaddr; 431962306a36Sopenharmony_ci unsigned long i = get_eb_page_index(dst_offset); 432062306a36Sopenharmony_ci 432162306a36Sopenharmony_ci if (check_eb_range(dst, dst_offset, len) || 432262306a36Sopenharmony_ci check_eb_range(src, src_offset, len)) 432362306a36Sopenharmony_ci return; 432462306a36Sopenharmony_ci 432562306a36Sopenharmony_ci WARN_ON(src->len != dst_len); 432662306a36Sopenharmony_ci 432762306a36Sopenharmony_ci offset = get_eb_offset_in_page(dst, dst_offset); 432862306a36Sopenharmony_ci 432962306a36Sopenharmony_ci while (len > 0) { 433062306a36Sopenharmony_ci page = dst->pages[i]; 433162306a36Sopenharmony_ci assert_eb_page_uptodate(dst, page); 433262306a36Sopenharmony_ci 433362306a36Sopenharmony_ci cur = min(len, (unsigned long)(PAGE_SIZE - offset)); 433462306a36Sopenharmony_ci 433562306a36Sopenharmony_ci kaddr = page_address(page); 433662306a36Sopenharmony_ci read_extent_buffer(src, kaddr + offset, src_offset, cur); 433762306a36Sopenharmony_ci 433862306a36Sopenharmony_ci src_offset += cur; 433962306a36Sopenharmony_ci len -= cur; 434062306a36Sopenharmony_ci offset = 0; 434162306a36Sopenharmony_ci i++; 434262306a36Sopenharmony_ci } 434362306a36Sopenharmony_ci} 434462306a36Sopenharmony_ci 434562306a36Sopenharmony_ci/* 434662306a36Sopenharmony_ci * eb_bitmap_offset() - calculate the page and offset of the byte containing the 434762306a36Sopenharmony_ci * given bit number 434862306a36Sopenharmony_ci * @eb: the extent buffer 434962306a36Sopenharmony_ci * @start: offset of the bitmap item in the extent buffer 435062306a36Sopenharmony_ci * @nr: bit number 435162306a36Sopenharmony_ci * @page_index: return index of the page in the extent buffer that contains the 435262306a36Sopenharmony_ci * given bit number 435362306a36Sopenharmony_ci * @page_offset: return offset into the page given by page_index 435462306a36Sopenharmony_ci * 435562306a36Sopenharmony_ci * This helper hides the ugliness of finding the byte in an extent buffer which 435662306a36Sopenharmony_ci * contains a given bit. 435762306a36Sopenharmony_ci */ 435862306a36Sopenharmony_cistatic inline void eb_bitmap_offset(const struct extent_buffer *eb, 435962306a36Sopenharmony_ci unsigned long start, unsigned long nr, 436062306a36Sopenharmony_ci unsigned long *page_index, 436162306a36Sopenharmony_ci size_t *page_offset) 436262306a36Sopenharmony_ci{ 436362306a36Sopenharmony_ci size_t byte_offset = BIT_BYTE(nr); 436462306a36Sopenharmony_ci size_t offset; 436562306a36Sopenharmony_ci 436662306a36Sopenharmony_ci /* 436762306a36Sopenharmony_ci * The byte we want is the offset of the extent buffer + the offset of 436862306a36Sopenharmony_ci * the bitmap item in the extent buffer + the offset of the byte in the 436962306a36Sopenharmony_ci * bitmap item. 437062306a36Sopenharmony_ci */ 437162306a36Sopenharmony_ci offset = start + offset_in_page(eb->start) + byte_offset; 437262306a36Sopenharmony_ci 437362306a36Sopenharmony_ci *page_index = offset >> PAGE_SHIFT; 437462306a36Sopenharmony_ci *page_offset = offset_in_page(offset); 437562306a36Sopenharmony_ci} 437662306a36Sopenharmony_ci 437762306a36Sopenharmony_ci/* 437862306a36Sopenharmony_ci * Determine whether a bit in a bitmap item is set. 437962306a36Sopenharmony_ci * 438062306a36Sopenharmony_ci * @eb: the extent buffer 438162306a36Sopenharmony_ci * @start: offset of the bitmap item in the extent buffer 438262306a36Sopenharmony_ci * @nr: bit number to test 438362306a36Sopenharmony_ci */ 438462306a36Sopenharmony_ciint extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start, 438562306a36Sopenharmony_ci unsigned long nr) 438662306a36Sopenharmony_ci{ 438762306a36Sopenharmony_ci u8 *kaddr; 438862306a36Sopenharmony_ci struct page *page; 438962306a36Sopenharmony_ci unsigned long i; 439062306a36Sopenharmony_ci size_t offset; 439162306a36Sopenharmony_ci 439262306a36Sopenharmony_ci eb_bitmap_offset(eb, start, nr, &i, &offset); 439362306a36Sopenharmony_ci page = eb->pages[i]; 439462306a36Sopenharmony_ci assert_eb_page_uptodate(eb, page); 439562306a36Sopenharmony_ci kaddr = page_address(page); 439662306a36Sopenharmony_ci return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); 439762306a36Sopenharmony_ci} 439862306a36Sopenharmony_ci 439962306a36Sopenharmony_cistatic u8 *extent_buffer_get_byte(const struct extent_buffer *eb, unsigned long bytenr) 440062306a36Sopenharmony_ci{ 440162306a36Sopenharmony_ci unsigned long index = get_eb_page_index(bytenr); 440262306a36Sopenharmony_ci 440362306a36Sopenharmony_ci if (check_eb_range(eb, bytenr, 1)) 440462306a36Sopenharmony_ci return NULL; 440562306a36Sopenharmony_ci return page_address(eb->pages[index]) + get_eb_offset_in_page(eb, bytenr); 440662306a36Sopenharmony_ci} 440762306a36Sopenharmony_ci 440862306a36Sopenharmony_ci/* 440962306a36Sopenharmony_ci * Set an area of a bitmap to 1. 441062306a36Sopenharmony_ci * 441162306a36Sopenharmony_ci * @eb: the extent buffer 441262306a36Sopenharmony_ci * @start: offset of the bitmap item in the extent buffer 441362306a36Sopenharmony_ci * @pos: bit number of the first bit 441462306a36Sopenharmony_ci * @len: number of bits to set 441562306a36Sopenharmony_ci */ 441662306a36Sopenharmony_civoid extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start, 441762306a36Sopenharmony_ci unsigned long pos, unsigned long len) 441862306a36Sopenharmony_ci{ 441962306a36Sopenharmony_ci unsigned int first_byte = start + BIT_BYTE(pos); 442062306a36Sopenharmony_ci unsigned int last_byte = start + BIT_BYTE(pos + len - 1); 442162306a36Sopenharmony_ci const bool same_byte = (first_byte == last_byte); 442262306a36Sopenharmony_ci u8 mask = BITMAP_FIRST_BYTE_MASK(pos); 442362306a36Sopenharmony_ci u8 *kaddr; 442462306a36Sopenharmony_ci 442562306a36Sopenharmony_ci if (same_byte) 442662306a36Sopenharmony_ci mask &= BITMAP_LAST_BYTE_MASK(pos + len); 442762306a36Sopenharmony_ci 442862306a36Sopenharmony_ci /* Handle the first byte. */ 442962306a36Sopenharmony_ci kaddr = extent_buffer_get_byte(eb, first_byte); 443062306a36Sopenharmony_ci *kaddr |= mask; 443162306a36Sopenharmony_ci if (same_byte) 443262306a36Sopenharmony_ci return; 443362306a36Sopenharmony_ci 443462306a36Sopenharmony_ci /* Handle the byte aligned part. */ 443562306a36Sopenharmony_ci ASSERT(first_byte + 1 <= last_byte); 443662306a36Sopenharmony_ci memset_extent_buffer(eb, 0xff, first_byte + 1, last_byte - first_byte - 1); 443762306a36Sopenharmony_ci 443862306a36Sopenharmony_ci /* Handle the last byte. */ 443962306a36Sopenharmony_ci kaddr = extent_buffer_get_byte(eb, last_byte); 444062306a36Sopenharmony_ci *kaddr |= BITMAP_LAST_BYTE_MASK(pos + len); 444162306a36Sopenharmony_ci} 444262306a36Sopenharmony_ci 444362306a36Sopenharmony_ci 444462306a36Sopenharmony_ci/* 444562306a36Sopenharmony_ci * Clear an area of a bitmap. 444662306a36Sopenharmony_ci * 444762306a36Sopenharmony_ci * @eb: the extent buffer 444862306a36Sopenharmony_ci * @start: offset of the bitmap item in the extent buffer 444962306a36Sopenharmony_ci * @pos: bit number of the first bit 445062306a36Sopenharmony_ci * @len: number of bits to clear 445162306a36Sopenharmony_ci */ 445262306a36Sopenharmony_civoid extent_buffer_bitmap_clear(const struct extent_buffer *eb, 445362306a36Sopenharmony_ci unsigned long start, unsigned long pos, 445462306a36Sopenharmony_ci unsigned long len) 445562306a36Sopenharmony_ci{ 445662306a36Sopenharmony_ci unsigned int first_byte = start + BIT_BYTE(pos); 445762306a36Sopenharmony_ci unsigned int last_byte = start + BIT_BYTE(pos + len - 1); 445862306a36Sopenharmony_ci const bool same_byte = (first_byte == last_byte); 445962306a36Sopenharmony_ci u8 mask = BITMAP_FIRST_BYTE_MASK(pos); 446062306a36Sopenharmony_ci u8 *kaddr; 446162306a36Sopenharmony_ci 446262306a36Sopenharmony_ci if (same_byte) 446362306a36Sopenharmony_ci mask &= BITMAP_LAST_BYTE_MASK(pos + len); 446462306a36Sopenharmony_ci 446562306a36Sopenharmony_ci /* Handle the first byte. */ 446662306a36Sopenharmony_ci kaddr = extent_buffer_get_byte(eb, first_byte); 446762306a36Sopenharmony_ci *kaddr &= ~mask; 446862306a36Sopenharmony_ci if (same_byte) 446962306a36Sopenharmony_ci return; 447062306a36Sopenharmony_ci 447162306a36Sopenharmony_ci /* Handle the byte aligned part. */ 447262306a36Sopenharmony_ci ASSERT(first_byte + 1 <= last_byte); 447362306a36Sopenharmony_ci memset_extent_buffer(eb, 0, first_byte + 1, last_byte - first_byte - 1); 447462306a36Sopenharmony_ci 447562306a36Sopenharmony_ci /* Handle the last byte. */ 447662306a36Sopenharmony_ci kaddr = extent_buffer_get_byte(eb, last_byte); 447762306a36Sopenharmony_ci *kaddr &= ~BITMAP_LAST_BYTE_MASK(pos + len); 447862306a36Sopenharmony_ci} 447962306a36Sopenharmony_ci 448062306a36Sopenharmony_cistatic inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) 448162306a36Sopenharmony_ci{ 448262306a36Sopenharmony_ci unsigned long distance = (src > dst) ? src - dst : dst - src; 448362306a36Sopenharmony_ci return distance < len; 448462306a36Sopenharmony_ci} 448562306a36Sopenharmony_ci 448662306a36Sopenharmony_civoid memcpy_extent_buffer(const struct extent_buffer *dst, 448762306a36Sopenharmony_ci unsigned long dst_offset, unsigned long src_offset, 448862306a36Sopenharmony_ci unsigned long len) 448962306a36Sopenharmony_ci{ 449062306a36Sopenharmony_ci unsigned long cur_off = 0; 449162306a36Sopenharmony_ci 449262306a36Sopenharmony_ci if (check_eb_range(dst, dst_offset, len) || 449362306a36Sopenharmony_ci check_eb_range(dst, src_offset, len)) 449462306a36Sopenharmony_ci return; 449562306a36Sopenharmony_ci 449662306a36Sopenharmony_ci while (cur_off < len) { 449762306a36Sopenharmony_ci unsigned long cur_src = cur_off + src_offset; 449862306a36Sopenharmony_ci unsigned long pg_index = get_eb_page_index(cur_src); 449962306a36Sopenharmony_ci unsigned long pg_off = get_eb_offset_in_page(dst, cur_src); 450062306a36Sopenharmony_ci unsigned long cur_len = min(src_offset + len - cur_src, 450162306a36Sopenharmony_ci PAGE_SIZE - pg_off); 450262306a36Sopenharmony_ci void *src_addr = page_address(dst->pages[pg_index]) + pg_off; 450362306a36Sopenharmony_ci const bool use_memmove = areas_overlap(src_offset + cur_off, 450462306a36Sopenharmony_ci dst_offset + cur_off, cur_len); 450562306a36Sopenharmony_ci 450662306a36Sopenharmony_ci __write_extent_buffer(dst, src_addr, dst_offset + cur_off, cur_len, 450762306a36Sopenharmony_ci use_memmove); 450862306a36Sopenharmony_ci cur_off += cur_len; 450962306a36Sopenharmony_ci } 451062306a36Sopenharmony_ci} 451162306a36Sopenharmony_ci 451262306a36Sopenharmony_civoid memmove_extent_buffer(const struct extent_buffer *dst, 451362306a36Sopenharmony_ci unsigned long dst_offset, unsigned long src_offset, 451462306a36Sopenharmony_ci unsigned long len) 451562306a36Sopenharmony_ci{ 451662306a36Sopenharmony_ci unsigned long dst_end = dst_offset + len - 1; 451762306a36Sopenharmony_ci unsigned long src_end = src_offset + len - 1; 451862306a36Sopenharmony_ci 451962306a36Sopenharmony_ci if (check_eb_range(dst, dst_offset, len) || 452062306a36Sopenharmony_ci check_eb_range(dst, src_offset, len)) 452162306a36Sopenharmony_ci return; 452262306a36Sopenharmony_ci 452362306a36Sopenharmony_ci if (dst_offset < src_offset) { 452462306a36Sopenharmony_ci memcpy_extent_buffer(dst, dst_offset, src_offset, len); 452562306a36Sopenharmony_ci return; 452662306a36Sopenharmony_ci } 452762306a36Sopenharmony_ci 452862306a36Sopenharmony_ci while (len > 0) { 452962306a36Sopenharmony_ci unsigned long src_i; 453062306a36Sopenharmony_ci size_t cur; 453162306a36Sopenharmony_ci size_t dst_off_in_page; 453262306a36Sopenharmony_ci size_t src_off_in_page; 453362306a36Sopenharmony_ci void *src_addr; 453462306a36Sopenharmony_ci bool use_memmove; 453562306a36Sopenharmony_ci 453662306a36Sopenharmony_ci src_i = get_eb_page_index(src_end); 453762306a36Sopenharmony_ci 453862306a36Sopenharmony_ci dst_off_in_page = get_eb_offset_in_page(dst, dst_end); 453962306a36Sopenharmony_ci src_off_in_page = get_eb_offset_in_page(dst, src_end); 454062306a36Sopenharmony_ci 454162306a36Sopenharmony_ci cur = min_t(unsigned long, len, src_off_in_page + 1); 454262306a36Sopenharmony_ci cur = min(cur, dst_off_in_page + 1); 454362306a36Sopenharmony_ci 454462306a36Sopenharmony_ci src_addr = page_address(dst->pages[src_i]) + src_off_in_page - 454562306a36Sopenharmony_ci cur + 1; 454662306a36Sopenharmony_ci use_memmove = areas_overlap(src_end - cur + 1, dst_end - cur + 1, 454762306a36Sopenharmony_ci cur); 454862306a36Sopenharmony_ci 454962306a36Sopenharmony_ci __write_extent_buffer(dst, src_addr, dst_end - cur + 1, cur, 455062306a36Sopenharmony_ci use_memmove); 455162306a36Sopenharmony_ci 455262306a36Sopenharmony_ci dst_end -= cur; 455362306a36Sopenharmony_ci src_end -= cur; 455462306a36Sopenharmony_ci len -= cur; 455562306a36Sopenharmony_ci } 455662306a36Sopenharmony_ci} 455762306a36Sopenharmony_ci 455862306a36Sopenharmony_ci#define GANG_LOOKUP_SIZE 16 455962306a36Sopenharmony_cistatic struct extent_buffer *get_next_extent_buffer( 456062306a36Sopenharmony_ci struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr) 456162306a36Sopenharmony_ci{ 456262306a36Sopenharmony_ci struct extent_buffer *gang[GANG_LOOKUP_SIZE]; 456362306a36Sopenharmony_ci struct extent_buffer *found = NULL; 456462306a36Sopenharmony_ci u64 page_start = page_offset(page); 456562306a36Sopenharmony_ci u64 cur = page_start; 456662306a36Sopenharmony_ci 456762306a36Sopenharmony_ci ASSERT(in_range(bytenr, page_start, PAGE_SIZE)); 456862306a36Sopenharmony_ci lockdep_assert_held(&fs_info->buffer_lock); 456962306a36Sopenharmony_ci 457062306a36Sopenharmony_ci while (cur < page_start + PAGE_SIZE) { 457162306a36Sopenharmony_ci int ret; 457262306a36Sopenharmony_ci int i; 457362306a36Sopenharmony_ci 457462306a36Sopenharmony_ci ret = radix_tree_gang_lookup(&fs_info->buffer_radix, 457562306a36Sopenharmony_ci (void **)gang, cur >> fs_info->sectorsize_bits, 457662306a36Sopenharmony_ci min_t(unsigned int, GANG_LOOKUP_SIZE, 457762306a36Sopenharmony_ci PAGE_SIZE / fs_info->nodesize)); 457862306a36Sopenharmony_ci if (ret == 0) 457962306a36Sopenharmony_ci goto out; 458062306a36Sopenharmony_ci for (i = 0; i < ret; i++) { 458162306a36Sopenharmony_ci /* Already beyond page end */ 458262306a36Sopenharmony_ci if (gang[i]->start >= page_start + PAGE_SIZE) 458362306a36Sopenharmony_ci goto out; 458462306a36Sopenharmony_ci /* Found one */ 458562306a36Sopenharmony_ci if (gang[i]->start >= bytenr) { 458662306a36Sopenharmony_ci found = gang[i]; 458762306a36Sopenharmony_ci goto out; 458862306a36Sopenharmony_ci } 458962306a36Sopenharmony_ci } 459062306a36Sopenharmony_ci cur = gang[ret - 1]->start + gang[ret - 1]->len; 459162306a36Sopenharmony_ci } 459262306a36Sopenharmony_ciout: 459362306a36Sopenharmony_ci return found; 459462306a36Sopenharmony_ci} 459562306a36Sopenharmony_ci 459662306a36Sopenharmony_cistatic int try_release_subpage_extent_buffer(struct page *page) 459762306a36Sopenharmony_ci{ 459862306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 459962306a36Sopenharmony_ci u64 cur = page_offset(page); 460062306a36Sopenharmony_ci const u64 end = page_offset(page) + PAGE_SIZE; 460162306a36Sopenharmony_ci int ret; 460262306a36Sopenharmony_ci 460362306a36Sopenharmony_ci while (cur < end) { 460462306a36Sopenharmony_ci struct extent_buffer *eb = NULL; 460562306a36Sopenharmony_ci 460662306a36Sopenharmony_ci /* 460762306a36Sopenharmony_ci * Unlike try_release_extent_buffer() which uses page->private 460862306a36Sopenharmony_ci * to grab buffer, for subpage case we rely on radix tree, thus 460962306a36Sopenharmony_ci * we need to ensure radix tree consistency. 461062306a36Sopenharmony_ci * 461162306a36Sopenharmony_ci * We also want an atomic snapshot of the radix tree, thus go 461262306a36Sopenharmony_ci * with spinlock rather than RCU. 461362306a36Sopenharmony_ci */ 461462306a36Sopenharmony_ci spin_lock(&fs_info->buffer_lock); 461562306a36Sopenharmony_ci eb = get_next_extent_buffer(fs_info, page, cur); 461662306a36Sopenharmony_ci if (!eb) { 461762306a36Sopenharmony_ci /* No more eb in the page range after or at cur */ 461862306a36Sopenharmony_ci spin_unlock(&fs_info->buffer_lock); 461962306a36Sopenharmony_ci break; 462062306a36Sopenharmony_ci } 462162306a36Sopenharmony_ci cur = eb->start + eb->len; 462262306a36Sopenharmony_ci 462362306a36Sopenharmony_ci /* 462462306a36Sopenharmony_ci * The same as try_release_extent_buffer(), to ensure the eb 462562306a36Sopenharmony_ci * won't disappear out from under us. 462662306a36Sopenharmony_ci */ 462762306a36Sopenharmony_ci spin_lock(&eb->refs_lock); 462862306a36Sopenharmony_ci if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { 462962306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 463062306a36Sopenharmony_ci spin_unlock(&fs_info->buffer_lock); 463162306a36Sopenharmony_ci break; 463262306a36Sopenharmony_ci } 463362306a36Sopenharmony_ci spin_unlock(&fs_info->buffer_lock); 463462306a36Sopenharmony_ci 463562306a36Sopenharmony_ci /* 463662306a36Sopenharmony_ci * If tree ref isn't set then we know the ref on this eb is a 463762306a36Sopenharmony_ci * real ref, so just return, this eb will likely be freed soon 463862306a36Sopenharmony_ci * anyway. 463962306a36Sopenharmony_ci */ 464062306a36Sopenharmony_ci if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { 464162306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 464262306a36Sopenharmony_ci break; 464362306a36Sopenharmony_ci } 464462306a36Sopenharmony_ci 464562306a36Sopenharmony_ci /* 464662306a36Sopenharmony_ci * Here we don't care about the return value, we will always 464762306a36Sopenharmony_ci * check the page private at the end. And 464862306a36Sopenharmony_ci * release_extent_buffer() will release the refs_lock. 464962306a36Sopenharmony_ci */ 465062306a36Sopenharmony_ci release_extent_buffer(eb); 465162306a36Sopenharmony_ci } 465262306a36Sopenharmony_ci /* 465362306a36Sopenharmony_ci * Finally to check if we have cleared page private, as if we have 465462306a36Sopenharmony_ci * released all ebs in the page, the page private should be cleared now. 465562306a36Sopenharmony_ci */ 465662306a36Sopenharmony_ci spin_lock(&page->mapping->private_lock); 465762306a36Sopenharmony_ci if (!PagePrivate(page)) 465862306a36Sopenharmony_ci ret = 1; 465962306a36Sopenharmony_ci else 466062306a36Sopenharmony_ci ret = 0; 466162306a36Sopenharmony_ci spin_unlock(&page->mapping->private_lock); 466262306a36Sopenharmony_ci return ret; 466362306a36Sopenharmony_ci 466462306a36Sopenharmony_ci} 466562306a36Sopenharmony_ci 466662306a36Sopenharmony_ciint try_release_extent_buffer(struct page *page) 466762306a36Sopenharmony_ci{ 466862306a36Sopenharmony_ci struct extent_buffer *eb; 466962306a36Sopenharmony_ci 467062306a36Sopenharmony_ci if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) 467162306a36Sopenharmony_ci return try_release_subpage_extent_buffer(page); 467262306a36Sopenharmony_ci 467362306a36Sopenharmony_ci /* 467462306a36Sopenharmony_ci * We need to make sure nobody is changing page->private, as we rely on 467562306a36Sopenharmony_ci * page->private as the pointer to extent buffer. 467662306a36Sopenharmony_ci */ 467762306a36Sopenharmony_ci spin_lock(&page->mapping->private_lock); 467862306a36Sopenharmony_ci if (!PagePrivate(page)) { 467962306a36Sopenharmony_ci spin_unlock(&page->mapping->private_lock); 468062306a36Sopenharmony_ci return 1; 468162306a36Sopenharmony_ci } 468262306a36Sopenharmony_ci 468362306a36Sopenharmony_ci eb = (struct extent_buffer *)page->private; 468462306a36Sopenharmony_ci BUG_ON(!eb); 468562306a36Sopenharmony_ci 468662306a36Sopenharmony_ci /* 468762306a36Sopenharmony_ci * This is a little awful but should be ok, we need to make sure that 468862306a36Sopenharmony_ci * the eb doesn't disappear out from under us while we're looking at 468962306a36Sopenharmony_ci * this page. 469062306a36Sopenharmony_ci */ 469162306a36Sopenharmony_ci spin_lock(&eb->refs_lock); 469262306a36Sopenharmony_ci if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { 469362306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 469462306a36Sopenharmony_ci spin_unlock(&page->mapping->private_lock); 469562306a36Sopenharmony_ci return 0; 469662306a36Sopenharmony_ci } 469762306a36Sopenharmony_ci spin_unlock(&page->mapping->private_lock); 469862306a36Sopenharmony_ci 469962306a36Sopenharmony_ci /* 470062306a36Sopenharmony_ci * If tree ref isn't set then we know the ref on this eb is a real ref, 470162306a36Sopenharmony_ci * so just return, this page will likely be freed soon anyway. 470262306a36Sopenharmony_ci */ 470362306a36Sopenharmony_ci if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { 470462306a36Sopenharmony_ci spin_unlock(&eb->refs_lock); 470562306a36Sopenharmony_ci return 0; 470662306a36Sopenharmony_ci } 470762306a36Sopenharmony_ci 470862306a36Sopenharmony_ci return release_extent_buffer(eb); 470962306a36Sopenharmony_ci} 471062306a36Sopenharmony_ci 471162306a36Sopenharmony_ci/* 471262306a36Sopenharmony_ci * btrfs_readahead_tree_block - attempt to readahead a child block 471362306a36Sopenharmony_ci * @fs_info: the fs_info 471462306a36Sopenharmony_ci * @bytenr: bytenr to read 471562306a36Sopenharmony_ci * @owner_root: objectid of the root that owns this eb 471662306a36Sopenharmony_ci * @gen: generation for the uptodate check, can be 0 471762306a36Sopenharmony_ci * @level: level for the eb 471862306a36Sopenharmony_ci * 471962306a36Sopenharmony_ci * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a 472062306a36Sopenharmony_ci * normal uptodate check of the eb, without checking the generation. If we have 472162306a36Sopenharmony_ci * to read the block we will not block on anything. 472262306a36Sopenharmony_ci */ 472362306a36Sopenharmony_civoid btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info, 472462306a36Sopenharmony_ci u64 bytenr, u64 owner_root, u64 gen, int level) 472562306a36Sopenharmony_ci{ 472662306a36Sopenharmony_ci struct btrfs_tree_parent_check check = { 472762306a36Sopenharmony_ci .has_first_key = 0, 472862306a36Sopenharmony_ci .level = level, 472962306a36Sopenharmony_ci .transid = gen 473062306a36Sopenharmony_ci }; 473162306a36Sopenharmony_ci struct extent_buffer *eb; 473262306a36Sopenharmony_ci int ret; 473362306a36Sopenharmony_ci 473462306a36Sopenharmony_ci eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 473562306a36Sopenharmony_ci if (IS_ERR(eb)) 473662306a36Sopenharmony_ci return; 473762306a36Sopenharmony_ci 473862306a36Sopenharmony_ci if (btrfs_buffer_uptodate(eb, gen, 1)) { 473962306a36Sopenharmony_ci free_extent_buffer(eb); 474062306a36Sopenharmony_ci return; 474162306a36Sopenharmony_ci } 474262306a36Sopenharmony_ci 474362306a36Sopenharmony_ci ret = read_extent_buffer_pages(eb, WAIT_NONE, 0, &check); 474462306a36Sopenharmony_ci if (ret < 0) 474562306a36Sopenharmony_ci free_extent_buffer_stale(eb); 474662306a36Sopenharmony_ci else 474762306a36Sopenharmony_ci free_extent_buffer(eb); 474862306a36Sopenharmony_ci} 474962306a36Sopenharmony_ci 475062306a36Sopenharmony_ci/* 475162306a36Sopenharmony_ci * btrfs_readahead_node_child - readahead a node's child block 475262306a36Sopenharmony_ci * @node: parent node we're reading from 475362306a36Sopenharmony_ci * @slot: slot in the parent node for the child we want to read 475462306a36Sopenharmony_ci * 475562306a36Sopenharmony_ci * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at 475662306a36Sopenharmony_ci * the slot in the node provided. 475762306a36Sopenharmony_ci */ 475862306a36Sopenharmony_civoid btrfs_readahead_node_child(struct extent_buffer *node, int slot) 475962306a36Sopenharmony_ci{ 476062306a36Sopenharmony_ci btrfs_readahead_tree_block(node->fs_info, 476162306a36Sopenharmony_ci btrfs_node_blockptr(node, slot), 476262306a36Sopenharmony_ci btrfs_header_owner(node), 476362306a36Sopenharmony_ci btrfs_node_ptr_generation(node, slot), 476462306a36Sopenharmony_ci btrfs_header_level(node) - 1); 476562306a36Sopenharmony_ci} 4766