1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/bitops.h> 4 #include <linux/slab.h> 5 #include <linux/bio.h> 6 #include <linux/mm.h> 7 #include <linux/pagemap.h> 8 #include <linux/page-flags.h> 9 #include <linux/sched/mm.h> 10 #include <linux/spinlock.h> 11 #include <linux/blkdev.h> 12 #include <linux/swap.h> 13 #include <linux/writeback.h> 14 #include <linux/pagevec.h> 15 #include <linux/prefetch.h> 16 #include <linux/fsverity.h> 17 #include "extent_io.h" 18 #include "extent-io-tree.h" 19 #include "extent_map.h" 20 #include "ctree.h" 21 #include "btrfs_inode.h" 22 #include "bio.h" 23 #include "locking.h" 24 #include "backref.h" 25 #include "disk-io.h" 26 #include "subpage.h" 27 #include "zoned.h" 28 #include "block-group.h" 29 #include "compression.h" 30 #include "fs.h" 31 #include "accessors.h" 32 #include "file-item.h" 33 #include "file.h" 34 #include "dev-replace.h" 35 #include "super.h" 36 #include "transaction.h" 37 38 static struct kmem_cache *extent_buffer_cache; 39 40 #ifdef CONFIG_BTRFS_DEBUG 41 static inline void btrfs_leak_debug_add_eb(struct extent_buffer *eb) 42 { 43 struct btrfs_fs_info *fs_info = eb->fs_info; 44 unsigned long flags; 45 46 spin_lock_irqsave(&fs_info->eb_leak_lock, flags); 47 list_add(&eb->leak_list, &fs_info->allocated_ebs); 48 spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); 49 } 50 51 static inline void btrfs_leak_debug_del_eb(struct extent_buffer *eb) 52 { 53 struct btrfs_fs_info *fs_info = eb->fs_info; 54 unsigned long flags; 55 56 spin_lock_irqsave(&fs_info->eb_leak_lock, flags); 57 list_del(&eb->leak_list); 58 spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); 59 } 60 61 void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info) 62 { 63 struct extent_buffer *eb; 64 unsigned long flags; 65 66 /* 67 * If we didn't get into open_ctree our allocated_ebs will not be 68 * initialized, so just skip this. 69 */ 70 if (!fs_info->allocated_ebs.next) 71 return; 72 73 WARN_ON(!list_empty(&fs_info->allocated_ebs)); 74 spin_lock_irqsave(&fs_info->eb_leak_lock, flags); 75 while (!list_empty(&fs_info->allocated_ebs)) { 76 eb = list_first_entry(&fs_info->allocated_ebs, 77 struct extent_buffer, leak_list); 78 pr_err( 79 "BTRFS: buffer leak start %llu len %u refs %d bflags %lu owner %llu\n", 80 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags, 81 btrfs_header_owner(eb)); 82 list_del(&eb->leak_list); 83 WARN_ON_ONCE(1); 84 kmem_cache_free(extent_buffer_cache, eb); 85 } 86 spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); 87 } 88 #else 89 #define btrfs_leak_debug_add_eb(eb) do {} while (0) 90 #define btrfs_leak_debug_del_eb(eb) do {} while (0) 91 #endif 92 93 /* 94 * Structure to record info about the bio being assembled, and other info like 95 * how many bytes are there before stripe/ordered extent boundary. 96 */ 97 struct btrfs_bio_ctrl { 98 struct btrfs_bio *bbio; 99 enum btrfs_compression_type compress_type; 100 u32 len_to_oe_boundary; 101 blk_opf_t opf; 102 btrfs_bio_end_io_t end_io_func; 103 struct writeback_control *wbc; 104 105 /* 106 * The sectors of the page which are going to be submitted by 107 * extent_writepage_io(). 108 * This is to avoid touching ranges covered by compression/inline. 109 */ 110 unsigned long submit_bitmap; 111 }; 112 113 static void submit_one_bio(struct btrfs_bio_ctrl *bio_ctrl) 114 { 115 struct btrfs_bio *bbio = bio_ctrl->bbio; 116 117 if (!bbio) 118 return; 119 120 /* Caller should ensure the bio has at least some range added */ 121 ASSERT(bbio->bio.bi_iter.bi_size); 122 123 if (btrfs_op(&bbio->bio) == BTRFS_MAP_READ && 124 bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) 125 btrfs_submit_compressed_read(bbio); 126 else 127 btrfs_submit_bbio(bbio, 0); 128 129 /* The bbio is owned by the end_io handler now */ 130 bio_ctrl->bbio = NULL; 131 } 132 133 /* 134 * Submit or fail the current bio in the bio_ctrl structure. 135 */ 136 static void submit_write_bio(struct btrfs_bio_ctrl *bio_ctrl, int ret) 137 { 138 struct btrfs_bio *bbio = bio_ctrl->bbio; 139 140 if (!bbio) 141 return; 142 143 if (ret) { 144 ASSERT(ret < 0); 145 btrfs_bio_end_io(bbio, errno_to_blk_status(ret)); 146 /* The bio is owned by the end_io handler now */ 147 bio_ctrl->bbio = NULL; 148 } else { 149 submit_one_bio(bio_ctrl); 150 } 151 } 152 153 int __init extent_buffer_init_cachep(void) 154 { 155 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", 156 sizeof(struct extent_buffer), 0, 0, 157 NULL); 158 if (!extent_buffer_cache) 159 return -ENOMEM; 160 161 return 0; 162 } 163 164 void __cold extent_buffer_free_cachep(void) 165 { 166 /* 167 * Make sure all delayed rcu free are flushed before we 168 * destroy caches. 169 */ 170 rcu_barrier(); 171 kmem_cache_destroy(extent_buffer_cache); 172 } 173 174 static void process_one_folio(struct btrfs_fs_info *fs_info, 175 struct folio *folio, const struct folio *locked_folio, 176 unsigned long page_ops, u64 start, u64 end) 177 { 178 u32 len; 179 180 ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX); 181 len = end + 1 - start; 182 183 if (page_ops & PAGE_SET_ORDERED) 184 btrfs_folio_clamp_set_ordered(fs_info, folio, start, len); 185 if (page_ops & PAGE_START_WRITEBACK) { 186 btrfs_folio_clamp_clear_dirty(fs_info, folio, start, len); 187 btrfs_folio_clamp_set_writeback(fs_info, folio, start, len); 188 } 189 if (page_ops & PAGE_END_WRITEBACK) 190 btrfs_folio_clamp_clear_writeback(fs_info, folio, start, len); 191 192 if (folio != locked_folio && (page_ops & PAGE_UNLOCK)) 193 btrfs_folio_end_writer_lock(fs_info, folio, start, len); 194 } 195 196 static void __process_folios_contig(struct address_space *mapping, 197 const struct folio *locked_folio, u64 start, 198 u64 end, unsigned long page_ops) 199 { 200 struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host); 201 pgoff_t start_index = start >> PAGE_SHIFT; 202 pgoff_t end_index = end >> PAGE_SHIFT; 203 pgoff_t index = start_index; 204 struct folio_batch fbatch; 205 int i; 206 207 folio_batch_init(&fbatch); 208 while (index <= end_index) { 209 int found_folios; 210 211 found_folios = filemap_get_folios_contig(mapping, &index, 212 end_index, &fbatch); 213 for (i = 0; i < found_folios; i++) { 214 struct folio *folio = fbatch.folios[i]; 215 216 process_one_folio(fs_info, folio, locked_folio, 217 page_ops, start, end); 218 } 219 folio_batch_release(&fbatch); 220 cond_resched(); 221 } 222 } 223 224 static noinline void __unlock_for_delalloc(const struct inode *inode, 225 const struct folio *locked_folio, 226 u64 start, u64 end) 227 { 228 unsigned long index = start >> PAGE_SHIFT; 229 unsigned long end_index = end >> PAGE_SHIFT; 230 231 ASSERT(locked_folio); 232 if (index == locked_folio->index && end_index == index) 233 return; 234 235 __process_folios_contig(inode->i_mapping, locked_folio, start, end, 236 PAGE_UNLOCK); 237 } 238 239 static noinline int lock_delalloc_folios(struct inode *inode, 240 const struct folio *locked_folio, 241 u64 start, u64 end) 242 { 243 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); 244 struct address_space *mapping = inode->i_mapping; 245 pgoff_t start_index = start >> PAGE_SHIFT; 246 pgoff_t end_index = end >> PAGE_SHIFT; 247 pgoff_t index = start_index; 248 u64 processed_end = start; 249 struct folio_batch fbatch; 250 251 if (index == locked_folio->index && index == end_index) 252 return 0; 253 254 folio_batch_init(&fbatch); 255 while (index <= end_index) { 256 unsigned int found_folios, i; 257 258 found_folios = filemap_get_folios_contig(mapping, &index, 259 end_index, &fbatch); 260 if (found_folios == 0) 261 goto out; 262 263 for (i = 0; i < found_folios; i++) { 264 struct folio *folio = fbatch.folios[i]; 265 u64 range_start; 266 u32 range_len; 267 268 if (folio == locked_folio) 269 continue; 270 271 folio_lock(folio); 272 if (!folio_test_dirty(folio) || folio->mapping != mapping) { 273 folio_unlock(folio); 274 goto out; 275 } 276 range_start = max_t(u64, folio_pos(folio), start); 277 range_len = min_t(u64, folio_pos(folio) + folio_size(folio), 278 end + 1) - range_start; 279 btrfs_folio_set_writer_lock(fs_info, folio, range_start, range_len); 280 281 processed_end = range_start + range_len - 1; 282 } 283 folio_batch_release(&fbatch); 284 cond_resched(); 285 } 286 287 return 0; 288 out: 289 folio_batch_release(&fbatch); 290 if (processed_end > start) 291 __unlock_for_delalloc(inode, locked_folio, start, 292 processed_end); 293 return -EAGAIN; 294 } 295 296 /* 297 * Find and lock a contiguous range of bytes in the file marked as delalloc, no 298 * more than @max_bytes. 299 * 300 * @start: The original start bytenr to search. 301 * Will store the extent range start bytenr. 302 * @end: The original end bytenr of the search range 303 * Will store the extent range end bytenr. 304 * 305 * Return true if we find a delalloc range which starts inside the original 306 * range, and @start/@end will store the delalloc range start/end. 307 * 308 * Return false if we can't find any delalloc range which starts inside the 309 * original range, and @start/@end will be the non-delalloc range start/end. 310 */ 311 EXPORT_FOR_TESTS 312 noinline_for_stack bool find_lock_delalloc_range(struct inode *inode, 313 struct folio *locked_folio, 314 u64 *start, u64 *end) 315 { 316 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); 317 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; 318 const u64 orig_start = *start; 319 const u64 orig_end = *end; 320 /* The sanity tests may not set a valid fs_info. */ 321 u64 max_bytes = fs_info ? fs_info->max_extent_size : BTRFS_MAX_EXTENT_SIZE; 322 u64 delalloc_start; 323 u64 delalloc_end; 324 bool found; 325 struct extent_state *cached_state = NULL; 326 int ret; 327 int loops = 0; 328 329 /* Caller should pass a valid @end to indicate the search range end */ 330 ASSERT(orig_end > orig_start); 331 332 /* The range should at least cover part of the folio */ 333 ASSERT(!(orig_start >= folio_pos(locked_folio) + folio_size(locked_folio) || 334 orig_end <= folio_pos(locked_folio))); 335 again: 336 /* step one, find a bunch of delalloc bytes starting at start */ 337 delalloc_start = *start; 338 delalloc_end = 0; 339 found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end, 340 max_bytes, &cached_state); 341 if (!found || delalloc_end <= *start || delalloc_start > orig_end) { 342 *start = delalloc_start; 343 344 /* @delalloc_end can be -1, never go beyond @orig_end */ 345 *end = min(delalloc_end, orig_end); 346 free_extent_state(cached_state); 347 return false; 348 } 349 350 /* 351 * start comes from the offset of locked_folio. We have to lock 352 * folios in order, so we can't process delalloc bytes before 353 * locked_folio 354 */ 355 if (delalloc_start < *start) 356 delalloc_start = *start; 357 358 /* 359 * make sure to limit the number of folios we try to lock down 360 */ 361 if (delalloc_end + 1 - delalloc_start > max_bytes) 362 delalloc_end = delalloc_start + max_bytes - 1; 363 364 /* step two, lock all the folioss after the folios that has start */ 365 ret = lock_delalloc_folios(inode, locked_folio, delalloc_start, 366 delalloc_end); 367 ASSERT(!ret || ret == -EAGAIN); 368 if (ret == -EAGAIN) { 369 /* some of the folios are gone, lets avoid looping by 370 * shortening the size of the delalloc range we're searching 371 */ 372 free_extent_state(cached_state); 373 cached_state = NULL; 374 if (!loops) { 375 max_bytes = PAGE_SIZE; 376 loops = 1; 377 goto again; 378 } else { 379 found = false; 380 goto out_failed; 381 } 382 } 383 384 /* step three, lock the state bits for the whole range */ 385 lock_extent(tree, delalloc_start, delalloc_end, &cached_state); 386 387 /* then test to make sure it is all still delalloc */ 388 ret = test_range_bit(tree, delalloc_start, delalloc_end, 389 EXTENT_DELALLOC, cached_state); 390 391 unlock_extent(tree, delalloc_start, delalloc_end, &cached_state); 392 if (!ret) { 393 __unlock_for_delalloc(inode, locked_folio, delalloc_start, 394 delalloc_end); 395 cond_resched(); 396 goto again; 397 } 398 *start = delalloc_start; 399 *end = delalloc_end; 400 out_failed: 401 return found; 402 } 403 404 void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end, 405 const struct folio *locked_folio, 406 struct extent_state **cached, 407 u32 clear_bits, unsigned long page_ops) 408 { 409 clear_extent_bit(&inode->io_tree, start, end, clear_bits, cached); 410 411 __process_folios_contig(inode->vfs_inode.i_mapping, locked_folio, start, 412 end, page_ops); 413 } 414 415 static bool btrfs_verify_folio(struct folio *folio, u64 start, u32 len) 416 { 417 struct btrfs_fs_info *fs_info = folio_to_fs_info(folio); 418 419 if (!fsverity_active(folio->mapping->host) || 420 btrfs_folio_test_uptodate(fs_info, folio, start, len) || 421 start >= i_size_read(folio->mapping->host)) 422 return true; 423 return fsverity_verify_folio(folio); 424 } 425 426 static void end_folio_read(struct folio *folio, bool uptodate, u64 start, u32 len) 427 { 428 struct btrfs_fs_info *fs_info = folio_to_fs_info(folio); 429 430 ASSERT(folio_pos(folio) <= start && 431 start + len <= folio_pos(folio) + PAGE_SIZE); 432 433 if (uptodate && btrfs_verify_folio(folio, start, len)) 434 btrfs_folio_set_uptodate(fs_info, folio, start, len); 435 else 436 btrfs_folio_clear_uptodate(fs_info, folio, start, len); 437 438 if (!btrfs_is_subpage(fs_info, folio->mapping)) 439 folio_unlock(folio); 440 else 441 btrfs_subpage_end_reader(fs_info, folio, start, len); 442 } 443 444 /* 445 * After a write IO is done, we need to: 446 * 447 * - clear the uptodate bits on error 448 * - clear the writeback bits in the extent tree for the range 449 * - filio_end_writeback() if there is no more pending io for the folio 450 * 451 * Scheduling is not allowed, so the extent state tree is expected 452 * to have one and only one object corresponding to this IO. 453 */ 454 static void end_bbio_data_write(struct btrfs_bio *bbio) 455 { 456 struct btrfs_fs_info *fs_info = bbio->fs_info; 457 struct bio *bio = &bbio->bio; 458 int error = blk_status_to_errno(bio->bi_status); 459 struct folio_iter fi; 460 const u32 sectorsize = fs_info->sectorsize; 461 462 ASSERT(!bio_flagged(bio, BIO_CLONED)); 463 bio_for_each_folio_all(fi, bio) { 464 struct folio *folio = fi.folio; 465 u64 start = folio_pos(folio) + fi.offset; 466 u32 len = fi.length; 467 468 /* Only order 0 (single page) folios are allowed for data. */ 469 ASSERT(folio_order(folio) == 0); 470 471 /* Our read/write should always be sector aligned. */ 472 if (!IS_ALIGNED(fi.offset, sectorsize)) 473 btrfs_err(fs_info, 474 "partial page write in btrfs with offset %zu and length %zu", 475 fi.offset, fi.length); 476 else if (!IS_ALIGNED(fi.length, sectorsize)) 477 btrfs_info(fs_info, 478 "incomplete page write with offset %zu and length %zu", 479 fi.offset, fi.length); 480 481 btrfs_finish_ordered_extent(bbio->ordered, folio, start, len, 482 !error); 483 if (error) 484 mapping_set_error(folio->mapping, error); 485 btrfs_folio_clear_writeback(fs_info, folio, start, len); 486 } 487 488 bio_put(bio); 489 } 490 491 static void begin_folio_read(struct btrfs_fs_info *fs_info, struct folio *folio) 492 { 493 ASSERT(folio_test_locked(folio)); 494 if (!btrfs_is_subpage(fs_info, folio->mapping)) 495 return; 496 497 ASSERT(folio_test_private(folio)); 498 btrfs_subpage_start_reader(fs_info, folio, folio_pos(folio), PAGE_SIZE); 499 } 500 501 /* 502 * After a data read IO is done, we need to: 503 * 504 * - clear the uptodate bits on error 505 * - set the uptodate bits if things worked 506 * - set the folio up to date if all extents in the tree are uptodate 507 * - clear the lock bit in the extent tree 508 * - unlock the folio if there are no other extents locked for it 509 * 510 * Scheduling is not allowed, so the extent state tree is expected 511 * to have one and only one object corresponding to this IO. 512 */ 513 static void end_bbio_data_read(struct btrfs_bio *bbio) 514 { 515 struct btrfs_fs_info *fs_info = bbio->fs_info; 516 struct bio *bio = &bbio->bio; 517 struct folio_iter fi; 518 const u32 sectorsize = fs_info->sectorsize; 519 520 ASSERT(!bio_flagged(bio, BIO_CLONED)); 521 bio_for_each_folio_all(fi, &bbio->bio) { 522 bool uptodate = !bio->bi_status; 523 struct folio *folio = fi.folio; 524 struct inode *inode = folio->mapping->host; 525 u64 start; 526 u64 end; 527 u32 len; 528 529 /* For now only order 0 folios are supported for data. */ 530 ASSERT(folio_order(folio) == 0); 531 btrfs_debug(fs_info, 532 "%s: bi_sector=%llu, err=%d, mirror=%u", 533 __func__, bio->bi_iter.bi_sector, bio->bi_status, 534 bbio->mirror_num); 535 536 /* 537 * We always issue full-sector reads, but if some block in a 538 * folio fails to read, blk_update_request() will advance 539 * bv_offset and adjust bv_len to compensate. Print a warning 540 * for unaligned offsets, and an error if they don't add up to 541 * a full sector. 542 */ 543 if (!IS_ALIGNED(fi.offset, sectorsize)) 544 btrfs_err(fs_info, 545 "partial page read in btrfs with offset %zu and length %zu", 546 fi.offset, fi.length); 547 else if (!IS_ALIGNED(fi.offset + fi.length, sectorsize)) 548 btrfs_info(fs_info, 549 "incomplete page read with offset %zu and length %zu", 550 fi.offset, fi.length); 551 552 start = folio_pos(folio) + fi.offset; 553 end = start + fi.length - 1; 554 len = fi.length; 555 556 if (likely(uptodate)) { 557 loff_t i_size = i_size_read(inode); 558 pgoff_t end_index = i_size >> folio_shift(folio); 559 560 /* 561 * Zero out the remaining part if this range straddles 562 * i_size. 563 * 564 * Here we should only zero the range inside the folio, 565 * not touch anything else. 566 * 567 * NOTE: i_size is exclusive while end is inclusive. 568 */ 569 if (folio_index(folio) == end_index && i_size <= end) { 570 u32 zero_start = max(offset_in_folio(folio, i_size), 571 offset_in_folio(folio, start)); 572 u32 zero_len = offset_in_folio(folio, end) + 1 - 573 zero_start; 574 575 folio_zero_range(folio, zero_start, zero_len); 576 } 577 } 578 579 /* Update page status and unlock. */ 580 end_folio_read(folio, uptodate, start, len); 581 } 582 bio_put(bio); 583 } 584 585 /* 586 * Populate every free slot in a provided array with folios using GFP_NOFS. 587 * 588 * @nr_folios: number of folios to allocate 589 * @folio_array: the array to fill with folios; any existing non-NULL entries in 590 * the array will be skipped 591 * 592 * Return: 0 if all folios were able to be allocated; 593 * -ENOMEM otherwise, the partially allocated folios would be freed and 594 * the array slots zeroed 595 */ 596 int btrfs_alloc_folio_array(unsigned int nr_folios, struct folio **folio_array) 597 { 598 for (int i = 0; i < nr_folios; i++) { 599 if (folio_array[i]) 600 continue; 601 folio_array[i] = folio_alloc(GFP_NOFS, 0); 602 if (!folio_array[i]) 603 goto error; 604 } 605 return 0; 606 error: 607 for (int i = 0; i < nr_folios; i++) { 608 if (folio_array[i]) 609 folio_put(folio_array[i]); 610 } 611 return -ENOMEM; 612 } 613 614 /* 615 * Populate every free slot in a provided array with pages, using GFP_NOFS. 616 * 617 * @nr_pages: number of pages to allocate 618 * @page_array: the array to fill with pages; any existing non-null entries in 619 * the array will be skipped 620 * @nofail: whether using __GFP_NOFAIL flag 621 * 622 * Return: 0 if all pages were able to be allocated; 623 * -ENOMEM otherwise, the partially allocated pages would be freed and 624 * the array slots zeroed 625 */ 626 int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array, 627 bool nofail) 628 { 629 const gfp_t gfp = nofail ? (GFP_NOFS | __GFP_NOFAIL) : GFP_NOFS; 630 unsigned int allocated; 631 632 for (allocated = 0; allocated < nr_pages;) { 633 unsigned int last = allocated; 634 635 allocated = alloc_pages_bulk_array(gfp, nr_pages, page_array); 636 if (unlikely(allocated == last)) { 637 /* No progress, fail and do cleanup. */ 638 for (int i = 0; i < allocated; i++) { 639 __free_page(page_array[i]); 640 page_array[i] = NULL; 641 } 642 return -ENOMEM; 643 } 644 } 645 return 0; 646 } 647 648 /* 649 * Populate needed folios for the extent buffer. 650 * 651 * For now, the folios populated are always in order 0 (aka, single page). 652 */ 653 static int alloc_eb_folio_array(struct extent_buffer *eb, bool nofail) 654 { 655 struct page *page_array[INLINE_EXTENT_BUFFER_PAGES] = { 0 }; 656 int num_pages = num_extent_pages(eb); 657 int ret; 658 659 ret = btrfs_alloc_page_array(num_pages, page_array, nofail); 660 if (ret < 0) 661 return ret; 662 663 for (int i = 0; i < num_pages; i++) 664 eb->folios[i] = page_folio(page_array[i]); 665 eb->folio_size = PAGE_SIZE; 666 eb->folio_shift = PAGE_SHIFT; 667 return 0; 668 } 669 670 static bool btrfs_bio_is_contig(struct btrfs_bio_ctrl *bio_ctrl, 671 struct folio *folio, u64 disk_bytenr, 672 unsigned int pg_offset) 673 { 674 struct bio *bio = &bio_ctrl->bbio->bio; 675 struct bio_vec *bvec = bio_last_bvec_all(bio); 676 const sector_t sector = disk_bytenr >> SECTOR_SHIFT; 677 struct folio *bv_folio = page_folio(bvec->bv_page); 678 679 if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) { 680 /* 681 * For compression, all IO should have its logical bytenr set 682 * to the starting bytenr of the compressed extent. 683 */ 684 return bio->bi_iter.bi_sector == sector; 685 } 686 687 /* 688 * The contig check requires the following conditions to be met: 689 * 690 * 1) The folios are belonging to the same inode 691 * This is implied by the call chain. 692 * 693 * 2) The range has adjacent logical bytenr 694 * 695 * 3) The range has adjacent file offset 696 * This is required for the usage of btrfs_bio->file_offset. 697 */ 698 return bio_end_sector(bio) == sector && 699 folio_pos(bv_folio) + bvec->bv_offset + bvec->bv_len == 700 folio_pos(folio) + pg_offset; 701 } 702 703 static void alloc_new_bio(struct btrfs_inode *inode, 704 struct btrfs_bio_ctrl *bio_ctrl, 705 u64 disk_bytenr, u64 file_offset) 706 { 707 struct btrfs_fs_info *fs_info = inode->root->fs_info; 708 struct btrfs_bio *bbio; 709 710 bbio = btrfs_bio_alloc(BIO_MAX_VECS, bio_ctrl->opf, fs_info, 711 bio_ctrl->end_io_func, NULL); 712 bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; 713 bbio->inode = inode; 714 bbio->file_offset = file_offset; 715 bio_ctrl->bbio = bbio; 716 bio_ctrl->len_to_oe_boundary = U32_MAX; 717 718 /* Limit data write bios to the ordered boundary. */ 719 if (bio_ctrl->wbc) { 720 struct btrfs_ordered_extent *ordered; 721 722 ordered = btrfs_lookup_ordered_extent(inode, file_offset); 723 if (ordered) { 724 bio_ctrl->len_to_oe_boundary = min_t(u32, U32_MAX, 725 ordered->file_offset + 726 ordered->disk_num_bytes - file_offset); 727 bbio->ordered = ordered; 728 } 729 730 /* 731 * Pick the last added device to support cgroup writeback. For 732 * multi-device file systems this means blk-cgroup policies have 733 * to always be set on the last added/replaced device. 734 * This is a bit odd but has been like that for a long time. 735 */ 736 bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev); 737 wbc_init_bio(bio_ctrl->wbc, &bbio->bio); 738 } 739 } 740 741 /* 742 * @disk_bytenr: logical bytenr where the write will be 743 * @page: page to add to the bio 744 * @size: portion of page that we want to write to 745 * @pg_offset: offset of the new bio or to check whether we are adding 746 * a contiguous page to the previous one 747 * 748 * The will either add the page into the existing @bio_ctrl->bbio, or allocate a 749 * new one in @bio_ctrl->bbio. 750 * The mirror number for this IO should already be initizlied in 751 * @bio_ctrl->mirror_num. 752 */ 753 static void submit_extent_folio(struct btrfs_bio_ctrl *bio_ctrl, 754 u64 disk_bytenr, struct folio *folio, 755 size_t size, unsigned long pg_offset) 756 { 757 struct btrfs_inode *inode = folio_to_inode(folio); 758 759 ASSERT(pg_offset + size <= PAGE_SIZE); 760 ASSERT(bio_ctrl->end_io_func); 761 762 if (bio_ctrl->bbio && 763 !btrfs_bio_is_contig(bio_ctrl, folio, disk_bytenr, pg_offset)) 764 submit_one_bio(bio_ctrl); 765 766 do { 767 u32 len = size; 768 769 /* Allocate new bio if needed */ 770 if (!bio_ctrl->bbio) { 771 alloc_new_bio(inode, bio_ctrl, disk_bytenr, 772 folio_pos(folio) + pg_offset); 773 } 774 775 /* Cap to the current ordered extent boundary if there is one. */ 776 if (len > bio_ctrl->len_to_oe_boundary) { 777 ASSERT(bio_ctrl->compress_type == BTRFS_COMPRESS_NONE); 778 ASSERT(is_data_inode(inode)); 779 len = bio_ctrl->len_to_oe_boundary; 780 } 781 782 if (!bio_add_folio(&bio_ctrl->bbio->bio, folio, len, pg_offset)) { 783 /* bio full: move on to a new one */ 784 submit_one_bio(bio_ctrl); 785 continue; 786 } 787 788 if (bio_ctrl->wbc) 789 wbc_account_cgroup_owner(bio_ctrl->wbc, folio, 790 len); 791 792 size -= len; 793 pg_offset += len; 794 disk_bytenr += len; 795 796 /* 797 * len_to_oe_boundary defaults to U32_MAX, which isn't folio or 798 * sector aligned. alloc_new_bio() then sets it to the end of 799 * our ordered extent for writes into zoned devices. 800 * 801 * When len_to_oe_boundary is tracking an ordered extent, we 802 * trust the ordered extent code to align things properly, and 803 * the check above to cap our write to the ordered extent 804 * boundary is correct. 805 * 806 * When len_to_oe_boundary is U32_MAX, the cap above would 807 * result in a 4095 byte IO for the last folio right before 808 * we hit the bio limit of UINT_MAX. bio_add_folio() has all 809 * the checks required to make sure we don't overflow the bio, 810 * and we should just ignore len_to_oe_boundary completely 811 * unless we're using it to track an ordered extent. 812 * 813 * It's pretty hard to make a bio sized U32_MAX, but it can 814 * happen when the page cache is able to feed us contiguous 815 * folios for large extents. 816 */ 817 if (bio_ctrl->len_to_oe_boundary != U32_MAX) 818 bio_ctrl->len_to_oe_boundary -= len; 819 820 /* Ordered extent boundary: move on to a new bio. */ 821 if (bio_ctrl->len_to_oe_boundary == 0) 822 submit_one_bio(bio_ctrl); 823 } while (size); 824 } 825 826 static int attach_extent_buffer_folio(struct extent_buffer *eb, 827 struct folio *folio, 828 struct btrfs_subpage *prealloc) 829 { 830 struct btrfs_fs_info *fs_info = eb->fs_info; 831 int ret = 0; 832 833 /* 834 * If the page is mapped to btree inode, we should hold the private 835 * lock to prevent race. 836 * For cloned or dummy extent buffers, their pages are not mapped and 837 * will not race with any other ebs. 838 */ 839 if (folio->mapping) 840 lockdep_assert_held(&folio->mapping->i_private_lock); 841 842 if (fs_info->nodesize >= PAGE_SIZE) { 843 if (!folio_test_private(folio)) 844 folio_attach_private(folio, eb); 845 else 846 WARN_ON(folio_get_private(folio) != eb); 847 return 0; 848 } 849 850 /* Already mapped, just free prealloc */ 851 if (folio_test_private(folio)) { 852 btrfs_free_subpage(prealloc); 853 return 0; 854 } 855 856 if (prealloc) 857 /* Has preallocated memory for subpage */ 858 folio_attach_private(folio, prealloc); 859 else 860 /* Do new allocation to attach subpage */ 861 ret = btrfs_attach_subpage(fs_info, folio, BTRFS_SUBPAGE_METADATA); 862 return ret; 863 } 864 865 int set_page_extent_mapped(struct page *page) 866 { 867 return set_folio_extent_mapped(page_folio(page)); 868 } 869 870 int set_folio_extent_mapped(struct folio *folio) 871 { 872 struct btrfs_fs_info *fs_info; 873 874 ASSERT(folio->mapping); 875 876 if (folio_test_private(folio)) 877 return 0; 878 879 fs_info = folio_to_fs_info(folio); 880 881 if (btrfs_is_subpage(fs_info, folio->mapping)) 882 return btrfs_attach_subpage(fs_info, folio, BTRFS_SUBPAGE_DATA); 883 884 folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE); 885 return 0; 886 } 887 888 void clear_folio_extent_mapped(struct folio *folio) 889 { 890 struct btrfs_fs_info *fs_info; 891 892 ASSERT(folio->mapping); 893 894 if (!folio_test_private(folio)) 895 return; 896 897 fs_info = folio_to_fs_info(folio); 898 if (btrfs_is_subpage(fs_info, folio->mapping)) 899 return btrfs_detach_subpage(fs_info, folio); 900 901 folio_detach_private(folio); 902 } 903 904 static struct extent_map *__get_extent_map(struct inode *inode, 905 struct folio *folio, u64 start, 906 u64 len, struct extent_map **em_cached) 907 { 908 struct extent_map *em; 909 struct extent_state *cached_state = NULL; 910 911 ASSERT(em_cached); 912 913 if (*em_cached) { 914 em = *em_cached; 915 if (extent_map_in_tree(em) && start >= em->start && 916 start < extent_map_end(em)) { 917 refcount_inc(&em->refs); 918 return em; 919 } 920 921 free_extent_map(em); 922 *em_cached = NULL; 923 } 924 925 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), start, start + len - 1, &cached_state); 926 em = btrfs_get_extent(BTRFS_I(inode), folio, start, len); 927 if (!IS_ERR(em)) { 928 BUG_ON(*em_cached); 929 refcount_inc(&em->refs); 930 *em_cached = em; 931 } 932 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len - 1, &cached_state); 933 934 return em; 935 } 936 /* 937 * basic readpage implementation. Locked extent state structs are inserted 938 * into the tree that are removed when the IO is done (by the end_io 939 * handlers) 940 * XXX JDM: This needs looking at to ensure proper page locking 941 * return 0 on success, otherwise return error 942 */ 943 static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached, 944 struct btrfs_bio_ctrl *bio_ctrl, u64 *prev_em_start) 945 { 946 struct inode *inode = folio->mapping->host; 947 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); 948 u64 start = folio_pos(folio); 949 const u64 end = start + PAGE_SIZE - 1; 950 u64 cur = start; 951 u64 extent_offset; 952 u64 last_byte = i_size_read(inode); 953 u64 block_start; 954 struct extent_map *em; 955 int ret = 0; 956 size_t pg_offset = 0; 957 size_t iosize; 958 size_t blocksize = fs_info->sectorsize; 959 960 ret = set_folio_extent_mapped(folio); 961 if (ret < 0) { 962 folio_unlock(folio); 963 return ret; 964 } 965 966 if (folio->index == last_byte >> folio_shift(folio)) { 967 size_t zero_offset = offset_in_folio(folio, last_byte); 968 969 if (zero_offset) { 970 iosize = folio_size(folio) - zero_offset; 971 folio_zero_range(folio, zero_offset, iosize); 972 } 973 } 974 bio_ctrl->end_io_func = end_bbio_data_read; 975 begin_folio_read(fs_info, folio); 976 while (cur <= end) { 977 enum btrfs_compression_type compress_type = BTRFS_COMPRESS_NONE; 978 bool force_bio_submit = false; 979 u64 disk_bytenr; 980 981 ASSERT(IS_ALIGNED(cur, fs_info->sectorsize)); 982 if (cur >= last_byte) { 983 iosize = folio_size(folio) - pg_offset; 984 folio_zero_range(folio, pg_offset, iosize); 985 end_folio_read(folio, true, cur, iosize); 986 break; 987 } 988 em = __get_extent_map(inode, folio, cur, end - cur + 1, 989 em_cached); 990 if (IS_ERR(em)) { 991 end_folio_read(folio, false, cur, end + 1 - cur); 992 return PTR_ERR(em); 993 } 994 extent_offset = cur - em->start; 995 BUG_ON(extent_map_end(em) <= cur); 996 BUG_ON(end < cur); 997 998 compress_type = extent_map_compression(em); 999 1000 iosize = min(extent_map_end(em) - cur, end - cur + 1); 1001 iosize = ALIGN(iosize, blocksize); 1002 if (compress_type != BTRFS_COMPRESS_NONE) 1003 disk_bytenr = em->disk_bytenr; 1004 else 1005 disk_bytenr = extent_map_block_start(em) + extent_offset; 1006 block_start = extent_map_block_start(em); 1007 if (em->flags & EXTENT_FLAG_PREALLOC) 1008 block_start = EXTENT_MAP_HOLE; 1009 1010 /* 1011 * If we have a file range that points to a compressed extent 1012 * and it's followed by a consecutive file range that points 1013 * to the same compressed extent (possibly with a different 1014 * offset and/or length, so it either points to the whole extent 1015 * or only part of it), we must make sure we do not submit a 1016 * single bio to populate the folios for the 2 ranges because 1017 * this makes the compressed extent read zero out the folios 1018 * belonging to the 2nd range. Imagine the following scenario: 1019 * 1020 * File layout 1021 * [0 - 8K] [8K - 24K] 1022 * | | 1023 * | | 1024 * points to extent X, points to extent X, 1025 * offset 4K, length of 8K offset 0, length 16K 1026 * 1027 * [extent X, compressed length = 4K uncompressed length = 16K] 1028 * 1029 * If the bio to read the compressed extent covers both ranges, 1030 * it will decompress extent X into the folios belonging to the 1031 * first range and then it will stop, zeroing out the remaining 1032 * folios that belong to the other range that points to extent X. 1033 * So here we make sure we submit 2 bios, one for the first 1034 * range and another one for the third range. Both will target 1035 * the same physical extent from disk, but we can't currently 1036 * make the compressed bio endio callback populate the folios 1037 * for both ranges because each compressed bio is tightly 1038 * coupled with a single extent map, and each range can have 1039 * an extent map with a different offset value relative to the 1040 * uncompressed data of our extent and different lengths. This 1041 * is a corner case so we prioritize correctness over 1042 * non-optimal behavior (submitting 2 bios for the same extent). 1043 */ 1044 if (compress_type != BTRFS_COMPRESS_NONE && 1045 prev_em_start && *prev_em_start != (u64)-1 && 1046 *prev_em_start != em->start) 1047 force_bio_submit = true; 1048 1049 if (prev_em_start) 1050 *prev_em_start = em->start; 1051 1052 free_extent_map(em); 1053 em = NULL; 1054 1055 /* we've found a hole, just zero and go on */ 1056 if (block_start == EXTENT_MAP_HOLE) { 1057 folio_zero_range(folio, pg_offset, iosize); 1058 1059 end_folio_read(folio, true, cur, iosize); 1060 cur = cur + iosize; 1061 pg_offset += iosize; 1062 continue; 1063 } 1064 /* the get_extent function already copied into the folio */ 1065 if (block_start == EXTENT_MAP_INLINE) { 1066 end_folio_read(folio, true, cur, iosize); 1067 cur = cur + iosize; 1068 pg_offset += iosize; 1069 continue; 1070 } 1071 1072 if (bio_ctrl->compress_type != compress_type) { 1073 submit_one_bio(bio_ctrl); 1074 bio_ctrl->compress_type = compress_type; 1075 } 1076 1077 if (force_bio_submit) 1078 submit_one_bio(bio_ctrl); 1079 submit_extent_folio(bio_ctrl, disk_bytenr, folio, iosize, 1080 pg_offset); 1081 cur = cur + iosize; 1082 pg_offset += iosize; 1083 } 1084 1085 return 0; 1086 } 1087 1088 int btrfs_read_folio(struct file *file, struct folio *folio) 1089 { 1090 struct btrfs_bio_ctrl bio_ctrl = { .opf = REQ_OP_READ }; 1091 struct extent_map *em_cached = NULL; 1092 int ret; 1093 1094 ret = btrfs_do_readpage(folio, &em_cached, &bio_ctrl, NULL); 1095 free_extent_map(em_cached); 1096 1097 /* 1098 * If btrfs_do_readpage() failed we will want to submit the assembled 1099 * bio to do the cleanup. 1100 */ 1101 submit_one_bio(&bio_ctrl); 1102 return ret; 1103 } 1104 1105 /* 1106 * helper for extent_writepage(), doing all of the delayed allocation setup. 1107 * 1108 * This returns 1 if btrfs_run_delalloc_range function did all the work required 1109 * to write the page (copy into inline extent). In this case the IO has 1110 * been started and the page is already unlocked. 1111 * 1112 * This returns 0 if all went well (page still locked) 1113 * This returns < 0 if there were errors (page still locked) 1114 */ 1115 static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode, 1116 struct folio *folio, 1117 struct btrfs_bio_ctrl *bio_ctrl) 1118 { 1119 struct btrfs_fs_info *fs_info = inode_to_fs_info(&inode->vfs_inode); 1120 struct writeback_control *wbc = bio_ctrl->wbc; 1121 const bool is_subpage = btrfs_is_subpage(fs_info, folio->mapping); 1122 const u64 page_start = folio_pos(folio); 1123 const u64 page_end = page_start + folio_size(folio) - 1; 1124 /* 1125 * Save the last found delalloc end. As the delalloc end can go beyond 1126 * page boundary, thus we cannot rely on subpage bitmap to locate the 1127 * last delalloc end. 1128 */ 1129 u64 last_delalloc_end = 0; 1130 u64 delalloc_start = page_start; 1131 u64 delalloc_end = page_end; 1132 u64 delalloc_to_write = 0; 1133 int ret = 0; 1134 1135 /* Save the dirty bitmap as our submission bitmap will be a subset of it. */ 1136 if (btrfs_is_subpage(fs_info, inode->vfs_inode.i_mapping)) { 1137 ASSERT(fs_info->sectors_per_page > 1); 1138 btrfs_get_subpage_dirty_bitmap(fs_info, folio, &bio_ctrl->submit_bitmap); 1139 } else { 1140 bio_ctrl->submit_bitmap = 1; 1141 } 1142 1143 /* Lock all (subpage) delalloc ranges inside the folio first. */ 1144 while (delalloc_start < page_end) { 1145 delalloc_end = page_end; 1146 if (!find_lock_delalloc_range(&inode->vfs_inode, folio, 1147 &delalloc_start, &delalloc_end)) { 1148 delalloc_start = delalloc_end + 1; 1149 continue; 1150 } 1151 btrfs_folio_set_writer_lock(fs_info, folio, delalloc_start, 1152 min(delalloc_end, page_end) + 1 - 1153 delalloc_start); 1154 last_delalloc_end = delalloc_end; 1155 delalloc_start = delalloc_end + 1; 1156 } 1157 delalloc_start = page_start; 1158 1159 if (!last_delalloc_end) 1160 goto out; 1161 1162 /* Run the delalloc ranges for the above locked ranges. */ 1163 while (delalloc_start < page_end) { 1164 u64 found_start; 1165 u32 found_len; 1166 bool found; 1167 1168 if (!is_subpage) { 1169 /* 1170 * For non-subpage case, the found delalloc range must 1171 * cover this folio and there must be only one locked 1172 * delalloc range. 1173 */ 1174 found_start = page_start; 1175 found_len = last_delalloc_end + 1 - found_start; 1176 found = true; 1177 } else { 1178 found = btrfs_subpage_find_writer_locked(fs_info, folio, 1179 delalloc_start, &found_start, &found_len); 1180 } 1181 if (!found) 1182 break; 1183 /* 1184 * The subpage range covers the last sector, the delalloc range may 1185 * end beyond the folio boundary, use the saved delalloc_end 1186 * instead. 1187 */ 1188 if (found_start + found_len >= page_end) 1189 found_len = last_delalloc_end + 1 - found_start; 1190 1191 if (ret >= 0) { 1192 /* No errors hit so far, run the current delalloc range. */ 1193 ret = btrfs_run_delalloc_range(inode, folio, 1194 found_start, 1195 found_start + found_len - 1, 1196 wbc); 1197 } else { 1198 /* 1199 * We've hit an error during previous delalloc range, 1200 * have to cleanup the remaining locked ranges. 1201 */ 1202 unlock_extent(&inode->io_tree, found_start, 1203 found_start + found_len - 1, NULL); 1204 __unlock_for_delalloc(&inode->vfs_inode, folio, 1205 found_start, 1206 found_start + found_len - 1); 1207 } 1208 1209 /* 1210 * We have some ranges that's going to be submitted asynchronously 1211 * (compression or inline). These range have their own control 1212 * on when to unlock the pages. We should not touch them 1213 * anymore, so clear the range from the submission bitmap. 1214 */ 1215 if (ret > 0) { 1216 unsigned int start_bit = (found_start - page_start) >> 1217 fs_info->sectorsize_bits; 1218 unsigned int end_bit = (min(page_end + 1, found_start + found_len) - 1219 page_start) >> fs_info->sectorsize_bits; 1220 bitmap_clear(&bio_ctrl->submit_bitmap, start_bit, end_bit - start_bit); 1221 } 1222 /* 1223 * Above btrfs_run_delalloc_range() may have unlocked the folio, 1224 * thus for the last range, we cannot touch the folio anymore. 1225 */ 1226 if (found_start + found_len >= last_delalloc_end + 1) 1227 break; 1228 1229 delalloc_start = found_start + found_len; 1230 } 1231 if (ret < 0) 1232 return ret; 1233 out: 1234 if (last_delalloc_end) 1235 delalloc_end = last_delalloc_end; 1236 else 1237 delalloc_end = page_end; 1238 /* 1239 * delalloc_end is already one less than the total length, so 1240 * we don't subtract one from PAGE_SIZE 1241 */ 1242 delalloc_to_write += 1243 DIV_ROUND_UP(delalloc_end + 1 - page_start, PAGE_SIZE); 1244 1245 /* 1246 * If all ranges are submitted asynchronously, we just need to account 1247 * for them here. 1248 */ 1249 if (bitmap_empty(&bio_ctrl->submit_bitmap, fs_info->sectors_per_page)) { 1250 wbc->nr_to_write -= delalloc_to_write; 1251 return 1; 1252 } 1253 1254 if (wbc->nr_to_write < delalloc_to_write) { 1255 int thresh = 8192; 1256 1257 if (delalloc_to_write < thresh * 2) 1258 thresh = delalloc_to_write; 1259 wbc->nr_to_write = min_t(u64, delalloc_to_write, 1260 thresh); 1261 } 1262 1263 return 0; 1264 } 1265 1266 /* 1267 * Return 0 if we have submitted or queued the sector for submission. 1268 * Return <0 for critical errors. 1269 * 1270 * Caller should make sure filepos < i_size and handle filepos >= i_size case. 1271 */ 1272 static int submit_one_sector(struct btrfs_inode *inode, 1273 struct folio *folio, 1274 u64 filepos, struct btrfs_bio_ctrl *bio_ctrl, 1275 loff_t i_size) 1276 { 1277 struct btrfs_fs_info *fs_info = inode->root->fs_info; 1278 struct extent_map *em; 1279 u64 block_start; 1280 u64 disk_bytenr; 1281 u64 extent_offset; 1282 u64 em_end; 1283 const u32 sectorsize = fs_info->sectorsize; 1284 1285 ASSERT(IS_ALIGNED(filepos, sectorsize)); 1286 1287 /* @filepos >= i_size case should be handled by the caller. */ 1288 ASSERT(filepos < i_size); 1289 1290 em = btrfs_get_extent(inode, NULL, filepos, sectorsize); 1291 if (IS_ERR(em)) 1292 return PTR_ERR_OR_ZERO(em); 1293 1294 extent_offset = filepos - em->start; 1295 em_end = extent_map_end(em); 1296 ASSERT(filepos <= em_end); 1297 ASSERT(IS_ALIGNED(em->start, sectorsize)); 1298 ASSERT(IS_ALIGNED(em->len, sectorsize)); 1299 1300 block_start = extent_map_block_start(em); 1301 disk_bytenr = extent_map_block_start(em) + extent_offset; 1302 1303 ASSERT(!extent_map_is_compressed(em)); 1304 ASSERT(block_start != EXTENT_MAP_HOLE); 1305 ASSERT(block_start != EXTENT_MAP_INLINE); 1306 1307 free_extent_map(em); 1308 em = NULL; 1309 1310 /* 1311 * Although the PageDirty bit is cleared before entering this 1312 * function, subpage dirty bit is not cleared. 1313 * So clear subpage dirty bit here so next time we won't submit 1314 * a folio for a range already written to disk. 1315 */ 1316 btrfs_folio_clear_dirty(fs_info, folio, filepos, sectorsize); 1317 btrfs_set_range_writeback(inode, filepos, filepos + sectorsize - 1); 1318 /* 1319 * Above call should set the whole folio with writeback flag, even 1320 * just for a single subpage sector. 1321 * As long as the folio is properly locked and the range is correct, 1322 * we should always get the folio with writeback flag. 1323 */ 1324 ASSERT(folio_test_writeback(folio)); 1325 1326 submit_extent_folio(bio_ctrl, disk_bytenr, folio, 1327 sectorsize, filepos - folio_pos(folio)); 1328 return 0; 1329 } 1330 1331 /* 1332 * Helper for extent_writepage(). This calls the writepage start hooks, 1333 * and does the loop to map the page into extents and bios. 1334 * 1335 * We return 1 if the IO is started and the page is unlocked, 1336 * 0 if all went well (page still locked) 1337 * < 0 if there were errors (page still locked) 1338 */ 1339 static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode, 1340 struct folio *folio, 1341 u64 start, u32 len, 1342 struct btrfs_bio_ctrl *bio_ctrl, 1343 loff_t i_size) 1344 { 1345 struct btrfs_fs_info *fs_info = inode->root->fs_info; 1346 unsigned long range_bitmap = 0; 1347 bool submitted_io = false; 1348 const u64 folio_start = folio_pos(folio); 1349 u64 cur; 1350 int bit; 1351 int ret = 0; 1352 1353 ASSERT(start >= folio_start && 1354 start + len <= folio_start + folio_size(folio)); 1355 1356 ret = btrfs_writepage_cow_fixup(folio); 1357 if (ret) { 1358 /* Fixup worker will requeue */ 1359 folio_redirty_for_writepage(bio_ctrl->wbc, folio); 1360 folio_unlock(folio); 1361 return 1; 1362 } 1363 1364 for (cur = start; cur < start + len; cur += fs_info->sectorsize) 1365 set_bit((cur - folio_start) >> fs_info->sectorsize_bits, &range_bitmap); 1366 bitmap_and(&bio_ctrl->submit_bitmap, &bio_ctrl->submit_bitmap, &range_bitmap, 1367 fs_info->sectors_per_page); 1368 1369 bio_ctrl->end_io_func = end_bbio_data_write; 1370 1371 for_each_set_bit(bit, &bio_ctrl->submit_bitmap, fs_info->sectors_per_page) { 1372 cur = folio_pos(folio) + (bit << fs_info->sectorsize_bits); 1373 1374 if (cur >= i_size) { 1375 btrfs_mark_ordered_io_finished(inode, folio, cur, 1376 start + len - cur, true); 1377 /* 1378 * This range is beyond i_size, thus we don't need to 1379 * bother writing back. 1380 * But we still need to clear the dirty subpage bit, or 1381 * the next time the folio gets dirtied, we will try to 1382 * writeback the sectors with subpage dirty bits, 1383 * causing writeback without ordered extent. 1384 */ 1385 btrfs_folio_clear_dirty(fs_info, folio, cur, 1386 start + len - cur); 1387 break; 1388 } 1389 ret = submit_one_sector(inode, folio, cur, bio_ctrl, i_size); 1390 if (ret < 0) 1391 goto out; 1392 submitted_io = true; 1393 } 1394 1395 btrfs_folio_assert_not_dirty(fs_info, folio, start, len); 1396 out: 1397 /* 1398 * If we didn't submitted any sector (>= i_size), folio dirty get 1399 * cleared but PAGECACHE_TAG_DIRTY is not cleared (only cleared 1400 * by folio_start_writeback() if the folio is not dirty). 1401 * 1402 * Here we set writeback and clear for the range. If the full folio 1403 * is no longer dirty then we clear the PAGECACHE_TAG_DIRTY tag. 1404 */ 1405 if (!submitted_io) { 1406 btrfs_folio_set_writeback(fs_info, folio, start, len); 1407 btrfs_folio_clear_writeback(fs_info, folio, start, len); 1408 } 1409 return ret; 1410 } 1411 1412 /* 1413 * the writepage semantics are similar to regular writepage. extent 1414 * records are inserted to lock ranges in the tree, and as dirty areas 1415 * are found, they are marked writeback. Then the lock bits are removed 1416 * and the end_io handler clears the writeback ranges 1417 * 1418 * Return 0 if everything goes well. 1419 * Return <0 for error. 1420 */ 1421 static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl) 1422 { 1423 struct inode *inode = folio->mapping->host; 1424 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); 1425 const u64 page_start = folio_pos(folio); 1426 int ret; 1427 size_t pg_offset; 1428 loff_t i_size = i_size_read(inode); 1429 unsigned long end_index = i_size >> PAGE_SHIFT; 1430 1431 trace_extent_writepage(folio, inode, bio_ctrl->wbc); 1432 1433 WARN_ON(!folio_test_locked(folio)); 1434 1435 pg_offset = offset_in_folio(folio, i_size); 1436 if (folio->index > end_index || 1437 (folio->index == end_index && !pg_offset)) { 1438 folio_invalidate(folio, 0, folio_size(folio)); 1439 folio_unlock(folio); 1440 return 0; 1441 } 1442 1443 if (folio->index == end_index) 1444 folio_zero_range(folio, pg_offset, folio_size(folio) - pg_offset); 1445 1446 /* 1447 * Default to unlock the whole folio. 1448 * The proper bitmap can only be initialized until writepage_delalloc(). 1449 */ 1450 bio_ctrl->submit_bitmap = (unsigned long)-1; 1451 ret = set_folio_extent_mapped(folio); 1452 if (ret < 0) 1453 goto done; 1454 1455 ret = writepage_delalloc(BTRFS_I(inode), folio, bio_ctrl); 1456 if (ret == 1) 1457 return 0; 1458 if (ret) 1459 goto done; 1460 1461 ret = extent_writepage_io(BTRFS_I(inode), folio, folio_pos(folio), 1462 PAGE_SIZE, bio_ctrl, i_size); 1463 if (ret == 1) 1464 return 0; 1465 1466 bio_ctrl->wbc->nr_to_write--; 1467 1468 done: 1469 if (ret) { 1470 btrfs_mark_ordered_io_finished(BTRFS_I(inode), folio, 1471 page_start, PAGE_SIZE, !ret); 1472 mapping_set_error(folio->mapping, ret); 1473 } 1474 1475 /* 1476 * Only unlock ranges that are submitted. As there can be some async 1477 * submitted ranges inside the folio. 1478 */ 1479 btrfs_folio_end_writer_lock_bitmap(fs_info, folio, bio_ctrl->submit_bitmap); 1480 ASSERT(ret <= 0); 1481 return ret; 1482 } 1483 1484 void wait_on_extent_buffer_writeback(struct extent_buffer *eb) 1485 { 1486 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, 1487 TASK_UNINTERRUPTIBLE); 1488 } 1489 1490 /* 1491 * Lock extent buffer status and pages for writeback. 1492 * 1493 * Return %false if the extent buffer doesn't need to be submitted (e.g. the 1494 * extent buffer is not dirty) 1495 * Return %true is the extent buffer is submitted to bio. 1496 */ 1497 static noinline_for_stack bool lock_extent_buffer_for_io(struct extent_buffer *eb, 1498 struct writeback_control *wbc) 1499 { 1500 struct btrfs_fs_info *fs_info = eb->fs_info; 1501 bool ret = false; 1502 1503 btrfs_tree_lock(eb); 1504 while (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { 1505 btrfs_tree_unlock(eb); 1506 if (wbc->sync_mode != WB_SYNC_ALL) 1507 return false; 1508 wait_on_extent_buffer_writeback(eb); 1509 btrfs_tree_lock(eb); 1510 } 1511 1512 /* 1513 * We need to do this to prevent races in people who check if the eb is 1514 * under IO since we can end up having no IO bits set for a short period 1515 * of time. 1516 */ 1517 spin_lock(&eb->refs_lock); 1518 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { 1519 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); 1520 spin_unlock(&eb->refs_lock); 1521 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); 1522 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1523 -eb->len, 1524 fs_info->dirty_metadata_batch); 1525 ret = true; 1526 } else { 1527 spin_unlock(&eb->refs_lock); 1528 } 1529 btrfs_tree_unlock(eb); 1530 return ret; 1531 } 1532 1533 static void set_btree_ioerr(struct extent_buffer *eb) 1534 { 1535 struct btrfs_fs_info *fs_info = eb->fs_info; 1536 1537 set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); 1538 1539 /* 1540 * A read may stumble upon this buffer later, make sure that it gets an 1541 * error and knows there was an error. 1542 */ 1543 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 1544 1545 /* 1546 * We need to set the mapping with the io error as well because a write 1547 * error will flip the file system readonly, and then syncfs() will 1548 * return a 0 because we are readonly if we don't modify the err seq for 1549 * the superblock. 1550 */ 1551 mapping_set_error(eb->fs_info->btree_inode->i_mapping, -EIO); 1552 1553 /* 1554 * If writeback for a btree extent that doesn't belong to a log tree 1555 * failed, increment the counter transaction->eb_write_errors. 1556 * We do this because while the transaction is running and before it's 1557 * committing (when we call filemap_fdata[write|wait]_range against 1558 * the btree inode), we might have 1559 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it 1560 * returns an error or an error happens during writeback, when we're 1561 * committing the transaction we wouldn't know about it, since the pages 1562 * can be no longer dirty nor marked anymore for writeback (if a 1563 * subsequent modification to the extent buffer didn't happen before the 1564 * transaction commit), which makes filemap_fdata[write|wait]_range not 1565 * able to find the pages which contain errors at transaction 1566 * commit time. So if this happens we must abort the transaction, 1567 * otherwise we commit a super block with btree roots that point to 1568 * btree nodes/leafs whose content on disk is invalid - either garbage 1569 * or the content of some node/leaf from a past generation that got 1570 * cowed or deleted and is no longer valid. 1571 * 1572 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would 1573 * not be enough - we need to distinguish between log tree extents vs 1574 * non-log tree extents, and the next filemap_fdatawait_range() call 1575 * will catch and clear such errors in the mapping - and that call might 1576 * be from a log sync and not from a transaction commit. Also, checking 1577 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is 1578 * not done and would not be reliable - the eb might have been released 1579 * from memory and reading it back again means that flag would not be 1580 * set (since it's a runtime flag, not persisted on disk). 1581 * 1582 * Using the flags below in the btree inode also makes us achieve the 1583 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started 1584 * writeback for all dirty pages and before filemap_fdatawait_range() 1585 * is called, the writeback for all dirty pages had already finished 1586 * with errors - because we were not using AS_EIO/AS_ENOSPC, 1587 * filemap_fdatawait_range() would return success, as it could not know 1588 * that writeback errors happened (the pages were no longer tagged for 1589 * writeback). 1590 */ 1591 switch (eb->log_index) { 1592 case -1: 1593 set_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags); 1594 break; 1595 case 0: 1596 set_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); 1597 break; 1598 case 1: 1599 set_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); 1600 break; 1601 default: 1602 BUG(); /* unexpected, logic error */ 1603 } 1604 } 1605 1606 /* 1607 * The endio specific version which won't touch any unsafe spinlock in endio 1608 * context. 1609 */ 1610 static struct extent_buffer *find_extent_buffer_nolock( 1611 const struct btrfs_fs_info *fs_info, u64 start) 1612 { 1613 struct extent_buffer *eb; 1614 1615 rcu_read_lock(); 1616 eb = radix_tree_lookup(&fs_info->buffer_radix, 1617 start >> fs_info->sectorsize_bits); 1618 if (eb && atomic_inc_not_zero(&eb->refs)) { 1619 rcu_read_unlock(); 1620 return eb; 1621 } 1622 rcu_read_unlock(); 1623 return NULL; 1624 } 1625 1626 static void end_bbio_meta_write(struct btrfs_bio *bbio) 1627 { 1628 struct extent_buffer *eb = bbio->private; 1629 struct btrfs_fs_info *fs_info = eb->fs_info; 1630 bool uptodate = !bbio->bio.bi_status; 1631 struct folio_iter fi; 1632 u32 bio_offset = 0; 1633 1634 if (!uptodate) 1635 set_btree_ioerr(eb); 1636 1637 bio_for_each_folio_all(fi, &bbio->bio) { 1638 u64 start = eb->start + bio_offset; 1639 struct folio *folio = fi.folio; 1640 u32 len = fi.length; 1641 1642 btrfs_folio_clear_writeback(fs_info, folio, start, len); 1643 bio_offset += len; 1644 } 1645 1646 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); 1647 smp_mb__after_atomic(); 1648 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); 1649 1650 bio_put(&bbio->bio); 1651 } 1652 1653 static void prepare_eb_write(struct extent_buffer *eb) 1654 { 1655 u32 nritems; 1656 unsigned long start; 1657 unsigned long end; 1658 1659 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); 1660 1661 /* Set btree blocks beyond nritems with 0 to avoid stale content */ 1662 nritems = btrfs_header_nritems(eb); 1663 if (btrfs_header_level(eb) > 0) { 1664 end = btrfs_node_key_ptr_offset(eb, nritems); 1665 memzero_extent_buffer(eb, end, eb->len - end); 1666 } else { 1667 /* 1668 * Leaf: 1669 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 1670 */ 1671 start = btrfs_item_nr_offset(eb, nritems); 1672 end = btrfs_item_nr_offset(eb, 0); 1673 if (nritems == 0) 1674 end += BTRFS_LEAF_DATA_SIZE(eb->fs_info); 1675 else 1676 end += btrfs_item_offset(eb, nritems - 1); 1677 memzero_extent_buffer(eb, start, end - start); 1678 } 1679 } 1680 1681 static noinline_for_stack void write_one_eb(struct extent_buffer *eb, 1682 struct writeback_control *wbc) 1683 { 1684 struct btrfs_fs_info *fs_info = eb->fs_info; 1685 struct btrfs_bio *bbio; 1686 1687 prepare_eb_write(eb); 1688 1689 bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES, 1690 REQ_OP_WRITE | REQ_META | wbc_to_write_flags(wbc), 1691 eb->fs_info, end_bbio_meta_write, eb); 1692 bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT; 1693 bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev); 1694 wbc_init_bio(wbc, &bbio->bio); 1695 bbio->inode = BTRFS_I(eb->fs_info->btree_inode); 1696 bbio->file_offset = eb->start; 1697 if (fs_info->nodesize < PAGE_SIZE) { 1698 struct folio *folio = eb->folios[0]; 1699 bool ret; 1700 1701 folio_lock(folio); 1702 btrfs_subpage_set_writeback(fs_info, folio, eb->start, eb->len); 1703 if (btrfs_subpage_clear_and_test_dirty(fs_info, folio, eb->start, 1704 eb->len)) { 1705 folio_clear_dirty_for_io(folio); 1706 wbc->nr_to_write--; 1707 } 1708 ret = bio_add_folio(&bbio->bio, folio, eb->len, 1709 eb->start - folio_pos(folio)); 1710 ASSERT(ret); 1711 wbc_account_cgroup_owner(wbc, folio, eb->len); 1712 folio_unlock(folio); 1713 } else { 1714 int num_folios = num_extent_folios(eb); 1715 1716 for (int i = 0; i < num_folios; i++) { 1717 struct folio *folio = eb->folios[i]; 1718 bool ret; 1719 1720 folio_lock(folio); 1721 folio_clear_dirty_for_io(folio); 1722 folio_start_writeback(folio); 1723 ret = bio_add_folio(&bbio->bio, folio, eb->folio_size, 0); 1724 ASSERT(ret); 1725 wbc_account_cgroup_owner(wbc, folio, eb->folio_size); 1726 wbc->nr_to_write -= folio_nr_pages(folio); 1727 folio_unlock(folio); 1728 } 1729 } 1730 btrfs_submit_bbio(bbio, 0); 1731 } 1732 1733 /* 1734 * Submit one subpage btree page. 1735 * 1736 * The main difference to submit_eb_page() is: 1737 * - Page locking 1738 * For subpage, we don't rely on page locking at all. 1739 * 1740 * - Flush write bio 1741 * We only flush bio if we may be unable to fit current extent buffers into 1742 * current bio. 1743 * 1744 * Return >=0 for the number of submitted extent buffers. 1745 * Return <0 for fatal error. 1746 */ 1747 static int submit_eb_subpage(struct folio *folio, struct writeback_control *wbc) 1748 { 1749 struct btrfs_fs_info *fs_info = folio_to_fs_info(folio); 1750 int submitted = 0; 1751 u64 folio_start = folio_pos(folio); 1752 int bit_start = 0; 1753 int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits; 1754 1755 /* Lock and write each dirty extent buffers in the range */ 1756 while (bit_start < fs_info->sectors_per_page) { 1757 struct btrfs_subpage *subpage = folio_get_private(folio); 1758 struct extent_buffer *eb; 1759 unsigned long flags; 1760 u64 start; 1761 1762 /* 1763 * Take private lock to ensure the subpage won't be detached 1764 * in the meantime. 1765 */ 1766 spin_lock(&folio->mapping->i_private_lock); 1767 if (!folio_test_private(folio)) { 1768 spin_unlock(&folio->mapping->i_private_lock); 1769 break; 1770 } 1771 spin_lock_irqsave(&subpage->lock, flags); 1772 if (!test_bit(bit_start + btrfs_bitmap_nr_dirty * fs_info->sectors_per_page, 1773 subpage->bitmaps)) { 1774 spin_unlock_irqrestore(&subpage->lock, flags); 1775 spin_unlock(&folio->mapping->i_private_lock); 1776 bit_start++; 1777 continue; 1778 } 1779 1780 start = folio_start + bit_start * fs_info->sectorsize; 1781 bit_start += sectors_per_node; 1782 1783 /* 1784 * Here we just want to grab the eb without touching extra 1785 * spin locks, so call find_extent_buffer_nolock(). 1786 */ 1787 eb = find_extent_buffer_nolock(fs_info, start); 1788 spin_unlock_irqrestore(&subpage->lock, flags); 1789 spin_unlock(&folio->mapping->i_private_lock); 1790 1791 /* 1792 * The eb has already reached 0 refs thus find_extent_buffer() 1793 * doesn't return it. We don't need to write back such eb 1794 * anyway. 1795 */ 1796 if (!eb) 1797 continue; 1798 1799 if (lock_extent_buffer_for_io(eb, wbc)) { 1800 write_one_eb(eb, wbc); 1801 submitted++; 1802 } 1803 free_extent_buffer(eb); 1804 } 1805 return submitted; 1806 } 1807 1808 /* 1809 * Submit all page(s) of one extent buffer. 1810 * 1811 * @page: the page of one extent buffer 1812 * @eb_context: to determine if we need to submit this page, if current page 1813 * belongs to this eb, we don't need to submit 1814 * 1815 * The caller should pass each page in their bytenr order, and here we use 1816 * @eb_context to determine if we have submitted pages of one extent buffer. 1817 * 1818 * If we have, we just skip until we hit a new page that doesn't belong to 1819 * current @eb_context. 1820 * 1821 * If not, we submit all the page(s) of the extent buffer. 1822 * 1823 * Return >0 if we have submitted the extent buffer successfully. 1824 * Return 0 if we don't need to submit the page, as it's already submitted by 1825 * previous call. 1826 * Return <0 for fatal error. 1827 */ 1828 static int submit_eb_page(struct folio *folio, struct btrfs_eb_write_context *ctx) 1829 { 1830 struct writeback_control *wbc = ctx->wbc; 1831 struct address_space *mapping = folio->mapping; 1832 struct extent_buffer *eb; 1833 int ret; 1834 1835 if (!folio_test_private(folio)) 1836 return 0; 1837 1838 if (folio_to_fs_info(folio)->nodesize < PAGE_SIZE) 1839 return submit_eb_subpage(folio, wbc); 1840 1841 spin_lock(&mapping->i_private_lock); 1842 if (!folio_test_private(folio)) { 1843 spin_unlock(&mapping->i_private_lock); 1844 return 0; 1845 } 1846 1847 eb = folio_get_private(folio); 1848 1849 /* 1850 * Shouldn't happen and normally this would be a BUG_ON but no point 1851 * crashing the machine for something we can survive anyway. 1852 */ 1853 if (WARN_ON(!eb)) { 1854 spin_unlock(&mapping->i_private_lock); 1855 return 0; 1856 } 1857 1858 if (eb == ctx->eb) { 1859 spin_unlock(&mapping->i_private_lock); 1860 return 0; 1861 } 1862 ret = atomic_inc_not_zero(&eb->refs); 1863 spin_unlock(&mapping->i_private_lock); 1864 if (!ret) 1865 return 0; 1866 1867 ctx->eb = eb; 1868 1869 ret = btrfs_check_meta_write_pointer(eb->fs_info, ctx); 1870 if (ret) { 1871 if (ret == -EBUSY) 1872 ret = 0; 1873 free_extent_buffer(eb); 1874 return ret; 1875 } 1876 1877 if (!lock_extent_buffer_for_io(eb, wbc)) { 1878 free_extent_buffer(eb); 1879 return 0; 1880 } 1881 /* Implies write in zoned mode. */ 1882 if (ctx->zoned_bg) { 1883 /* Mark the last eb in the block group. */ 1884 btrfs_schedule_zone_finish_bg(ctx->zoned_bg, eb); 1885 ctx->zoned_bg->meta_write_pointer += eb->len; 1886 } 1887 write_one_eb(eb, wbc); 1888 free_extent_buffer(eb); 1889 return 1; 1890 } 1891 1892 int btree_write_cache_pages(struct address_space *mapping, 1893 struct writeback_control *wbc) 1894 { 1895 struct btrfs_eb_write_context ctx = { .wbc = wbc }; 1896 struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host); 1897 int ret = 0; 1898 int done = 0; 1899 int nr_to_write_done = 0; 1900 struct folio_batch fbatch; 1901 unsigned int nr_folios; 1902 pgoff_t index; 1903 pgoff_t end; /* Inclusive */ 1904 int scanned = 0; 1905 xa_mark_t tag; 1906 1907 folio_batch_init(&fbatch); 1908 if (wbc->range_cyclic) { 1909 index = mapping->writeback_index; /* Start from prev offset */ 1910 end = -1; 1911 /* 1912 * Start from the beginning does not need to cycle over the 1913 * range, mark it as scanned. 1914 */ 1915 scanned = (index == 0); 1916 } else { 1917 index = wbc->range_start >> PAGE_SHIFT; 1918 end = wbc->range_end >> PAGE_SHIFT; 1919 scanned = 1; 1920 } 1921 if (wbc->sync_mode == WB_SYNC_ALL) 1922 tag = PAGECACHE_TAG_TOWRITE; 1923 else 1924 tag = PAGECACHE_TAG_DIRTY; 1925 btrfs_zoned_meta_io_lock(fs_info); 1926 retry: 1927 if (wbc->sync_mode == WB_SYNC_ALL) 1928 tag_pages_for_writeback(mapping, index, end); 1929 while (!done && !nr_to_write_done && (index <= end) && 1930 (nr_folios = filemap_get_folios_tag(mapping, &index, end, 1931 tag, &fbatch))) { 1932 unsigned i; 1933 1934 for (i = 0; i < nr_folios; i++) { 1935 struct folio *folio = fbatch.folios[i]; 1936 1937 ret = submit_eb_page(folio, &ctx); 1938 if (ret == 0) 1939 continue; 1940 if (ret < 0) { 1941 done = 1; 1942 break; 1943 } 1944 1945 /* 1946 * the filesystem may choose to bump up nr_to_write. 1947 * We have to make sure to honor the new nr_to_write 1948 * at any time 1949 */ 1950 nr_to_write_done = wbc->nr_to_write <= 0; 1951 } 1952 folio_batch_release(&fbatch); 1953 cond_resched(); 1954 } 1955 if (!scanned && !done) { 1956 /* 1957 * We hit the last page and there is more work to be done: wrap 1958 * back to the start of the file 1959 */ 1960 scanned = 1; 1961 index = 0; 1962 goto retry; 1963 } 1964 /* 1965 * If something went wrong, don't allow any metadata write bio to be 1966 * submitted. 1967 * 1968 * This would prevent use-after-free if we had dirty pages not 1969 * cleaned up, which can still happen by fuzzed images. 1970 * 1971 * - Bad extent tree 1972 * Allowing existing tree block to be allocated for other trees. 1973 * 1974 * - Log tree operations 1975 * Exiting tree blocks get allocated to log tree, bumps its 1976 * generation, then get cleaned in tree re-balance. 1977 * Such tree block will not be written back, since it's clean, 1978 * thus no WRITTEN flag set. 1979 * And after log writes back, this tree block is not traced by 1980 * any dirty extent_io_tree. 1981 * 1982 * - Offending tree block gets re-dirtied from its original owner 1983 * Since it has bumped generation, no WRITTEN flag, it can be 1984 * reused without COWing. This tree block will not be traced 1985 * by btrfs_transaction::dirty_pages. 1986 * 1987 * Now such dirty tree block will not be cleaned by any dirty 1988 * extent io tree. Thus we don't want to submit such wild eb 1989 * if the fs already has error. 1990 * 1991 * We can get ret > 0 from submit_extent_folio() indicating how many ebs 1992 * were submitted. Reset it to 0 to avoid false alerts for the caller. 1993 */ 1994 if (ret > 0) 1995 ret = 0; 1996 if (!ret && BTRFS_FS_ERROR(fs_info)) 1997 ret = -EROFS; 1998 1999 if (ctx.zoned_bg) 2000 btrfs_put_block_group(ctx.zoned_bg); 2001 btrfs_zoned_meta_io_unlock(fs_info); 2002 return ret; 2003 } 2004 2005 /* 2006 * Walk the list of dirty pages of the given address space and write all of them. 2007 * 2008 * @mapping: address space structure to write 2009 * @wbc: subtract the number of written pages from *@wbc->nr_to_write 2010 * @bio_ctrl: holds context for the write, namely the bio 2011 * 2012 * If a page is already under I/O, write_cache_pages() skips it, even 2013 * if it's dirty. This is desirable behaviour for memory-cleaning writeback, 2014 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() 2015 * and msync() need to guarantee that all the data which was dirty at the time 2016 * the call was made get new I/O started against them. If wbc->sync_mode is 2017 * WB_SYNC_ALL then we were called for data integrity and we must wait for 2018 * existing IO to complete. 2019 */ 2020 static int extent_write_cache_pages(struct address_space *mapping, 2021 struct btrfs_bio_ctrl *bio_ctrl) 2022 { 2023 struct writeback_control *wbc = bio_ctrl->wbc; 2024 struct inode *inode = mapping->host; 2025 int ret = 0; 2026 int done = 0; 2027 int nr_to_write_done = 0; 2028 struct folio_batch fbatch; 2029 unsigned int nr_folios; 2030 pgoff_t index; 2031 pgoff_t end; /* Inclusive */ 2032 pgoff_t done_index; 2033 int range_whole = 0; 2034 int scanned = 0; 2035 xa_mark_t tag; 2036 2037 /* 2038 * We have to hold onto the inode so that ordered extents can do their 2039 * work when the IO finishes. The alternative to this is failing to add 2040 * an ordered extent if the igrab() fails there and that is a huge pain 2041 * to deal with, so instead just hold onto the inode throughout the 2042 * writepages operation. If it fails here we are freeing up the inode 2043 * anyway and we'd rather not waste our time writing out stuff that is 2044 * going to be truncated anyway. 2045 */ 2046 if (!igrab(inode)) 2047 return 0; 2048 2049 folio_batch_init(&fbatch); 2050 if (wbc->range_cyclic) { 2051 index = mapping->writeback_index; /* Start from prev offset */ 2052 end = -1; 2053 /* 2054 * Start from the beginning does not need to cycle over the 2055 * range, mark it as scanned. 2056 */ 2057 scanned = (index == 0); 2058 } else { 2059 index = wbc->range_start >> PAGE_SHIFT; 2060 end = wbc->range_end >> PAGE_SHIFT; 2061 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 2062 range_whole = 1; 2063 scanned = 1; 2064 } 2065 2066 /* 2067 * We do the tagged writepage as long as the snapshot flush bit is set 2068 * and we are the first one who do the filemap_flush() on this inode. 2069 * 2070 * The nr_to_write == LONG_MAX is needed to make sure other flushers do 2071 * not race in and drop the bit. 2072 */ 2073 if (range_whole && wbc->nr_to_write == LONG_MAX && 2074 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH, 2075 &BTRFS_I(inode)->runtime_flags)) 2076 wbc->tagged_writepages = 1; 2077 2078 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 2079 tag = PAGECACHE_TAG_TOWRITE; 2080 else 2081 tag = PAGECACHE_TAG_DIRTY; 2082 retry: 2083 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 2084 tag_pages_for_writeback(mapping, index, end); 2085 done_index = index; 2086 while (!done && !nr_to_write_done && (index <= end) && 2087 (nr_folios = filemap_get_folios_tag(mapping, &index, 2088 end, tag, &fbatch))) { 2089 unsigned i; 2090 2091 for (i = 0; i < nr_folios; i++) { 2092 struct folio *folio = fbatch.folios[i]; 2093 2094 done_index = folio_next_index(folio); 2095 /* 2096 * At this point we hold neither the i_pages lock nor 2097 * the page lock: the page may be truncated or 2098 * invalidated (changing page->mapping to NULL), 2099 * or even swizzled back from swapper_space to 2100 * tmpfs file mapping 2101 */ 2102 if (!folio_trylock(folio)) { 2103 submit_write_bio(bio_ctrl, 0); 2104 folio_lock(folio); 2105 } 2106 2107 if (unlikely(folio->mapping != mapping)) { 2108 folio_unlock(folio); 2109 continue; 2110 } 2111 2112 if (!folio_test_dirty(folio)) { 2113 /* Someone wrote it for us. */ 2114 folio_unlock(folio); 2115 continue; 2116 } 2117 2118 if (wbc->sync_mode != WB_SYNC_NONE) { 2119 if (folio_test_writeback(folio)) 2120 submit_write_bio(bio_ctrl, 0); 2121 folio_wait_writeback(folio); 2122 } 2123 2124 if (folio_test_writeback(folio) || 2125 !folio_clear_dirty_for_io(folio)) { 2126 folio_unlock(folio); 2127 continue; 2128 } 2129 2130 ret = extent_writepage(folio, bio_ctrl); 2131 if (ret < 0) { 2132 done = 1; 2133 break; 2134 } 2135 2136 /* 2137 * The filesystem may choose to bump up nr_to_write. 2138 * We have to make sure to honor the new nr_to_write 2139 * at any time. 2140 */ 2141 nr_to_write_done = (wbc->sync_mode == WB_SYNC_NONE && 2142 wbc->nr_to_write <= 0); 2143 } 2144 folio_batch_release(&fbatch); 2145 cond_resched(); 2146 } 2147 if (!scanned && !done) { 2148 /* 2149 * We hit the last page and there is more work to be done: wrap 2150 * back to the start of the file 2151 */ 2152 scanned = 1; 2153 index = 0; 2154 2155 /* 2156 * If we're looping we could run into a page that is locked by a 2157 * writer and that writer could be waiting on writeback for a 2158 * page in our current bio, and thus deadlock, so flush the 2159 * write bio here. 2160 */ 2161 submit_write_bio(bio_ctrl, 0); 2162 goto retry; 2163 } 2164 2165 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole)) 2166 mapping->writeback_index = done_index; 2167 2168 btrfs_add_delayed_iput(BTRFS_I(inode)); 2169 return ret; 2170 } 2171 2172 /* 2173 * Submit the pages in the range to bio for call sites which delalloc range has 2174 * already been ran (aka, ordered extent inserted) and all pages are still 2175 * locked. 2176 */ 2177 void extent_write_locked_range(struct inode *inode, const struct folio *locked_folio, 2178 u64 start, u64 end, struct writeback_control *wbc, 2179 bool pages_dirty) 2180 { 2181 bool found_error = false; 2182 int ret = 0; 2183 struct address_space *mapping = inode->i_mapping; 2184 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); 2185 const u32 sectorsize = fs_info->sectorsize; 2186 loff_t i_size = i_size_read(inode); 2187 u64 cur = start; 2188 struct btrfs_bio_ctrl bio_ctrl = { 2189 .wbc = wbc, 2190 .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc), 2191 }; 2192 2193 if (wbc->no_cgroup_owner) 2194 bio_ctrl.opf |= REQ_BTRFS_CGROUP_PUNT; 2195 2196 ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(end + 1, sectorsize)); 2197 2198 while (cur <= end) { 2199 u64 cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end); 2200 u32 cur_len = cur_end + 1 - cur; 2201 struct folio *folio; 2202 2203 folio = __filemap_get_folio(mapping, cur >> PAGE_SHIFT, 0, 0); 2204 2205 /* 2206 * This shouldn't happen, the pages are pinned and locked, this 2207 * code is just in case, but shouldn't actually be run. 2208 */ 2209 if (IS_ERR(folio)) { 2210 btrfs_mark_ordered_io_finished(BTRFS_I(inode), NULL, 2211 cur, cur_len, false); 2212 mapping_set_error(mapping, PTR_ERR(folio)); 2213 cur = cur_end + 1; 2214 continue; 2215 } 2216 2217 ASSERT(folio_test_locked(folio)); 2218 if (pages_dirty && folio != locked_folio) 2219 ASSERT(folio_test_dirty(folio)); 2220 2221 /* 2222 * Set the submission bitmap to submit all sectors. 2223 * extent_writepage_io() will do the truncation correctly. 2224 */ 2225 bio_ctrl.submit_bitmap = (unsigned long)-1; 2226 ret = extent_writepage_io(BTRFS_I(inode), folio, cur, cur_len, 2227 &bio_ctrl, i_size); 2228 if (ret == 1) 2229 goto next_page; 2230 2231 if (ret) { 2232 btrfs_mark_ordered_io_finished(BTRFS_I(inode), folio, 2233 cur, cur_len, !ret); 2234 mapping_set_error(mapping, ret); 2235 } 2236 btrfs_folio_end_writer_lock(fs_info, folio, cur, cur_len); 2237 if (ret < 0) 2238 found_error = true; 2239 next_page: 2240 folio_put(folio); 2241 cur = cur_end + 1; 2242 } 2243 2244 submit_write_bio(&bio_ctrl, found_error ? ret : 0); 2245 } 2246 2247 int btrfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 2248 { 2249 struct inode *inode = mapping->host; 2250 int ret = 0; 2251 struct btrfs_bio_ctrl bio_ctrl = { 2252 .wbc = wbc, 2253 .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc), 2254 }; 2255 2256 /* 2257 * Allow only a single thread to do the reloc work in zoned mode to 2258 * protect the write pointer updates. 2259 */ 2260 btrfs_zoned_data_reloc_lock(BTRFS_I(inode)); 2261 ret = extent_write_cache_pages(mapping, &bio_ctrl); 2262 submit_write_bio(&bio_ctrl, ret); 2263 btrfs_zoned_data_reloc_unlock(BTRFS_I(inode)); 2264 return ret; 2265 } 2266 2267 void btrfs_readahead(struct readahead_control *rac) 2268 { 2269 struct btrfs_bio_ctrl bio_ctrl = { .opf = REQ_OP_READ | REQ_RAHEAD }; 2270 struct folio *folio; 2271 struct extent_map *em_cached = NULL; 2272 u64 prev_em_start = (u64)-1; 2273 2274 while ((folio = readahead_folio(rac)) != NULL) 2275 btrfs_do_readpage(folio, &em_cached, &bio_ctrl, &prev_em_start); 2276 2277 if (em_cached) 2278 free_extent_map(em_cached); 2279 submit_one_bio(&bio_ctrl); 2280 } 2281 2282 /* 2283 * basic invalidate_folio code, this waits on any locked or writeback 2284 * ranges corresponding to the folio, and then deletes any extent state 2285 * records from the tree 2286 */ 2287 int extent_invalidate_folio(struct extent_io_tree *tree, 2288 struct folio *folio, size_t offset) 2289 { 2290 struct extent_state *cached_state = NULL; 2291 u64 start = folio_pos(folio); 2292 u64 end = start + folio_size(folio) - 1; 2293 size_t blocksize = folio_to_fs_info(folio)->sectorsize; 2294 2295 /* This function is only called for the btree inode */ 2296 ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO); 2297 2298 start += ALIGN(offset, blocksize); 2299 if (start > end) 2300 return 0; 2301 2302 lock_extent(tree, start, end, &cached_state); 2303 folio_wait_writeback(folio); 2304 2305 /* 2306 * Currently for btree io tree, only EXTENT_LOCKED is utilized, 2307 * so here we only need to unlock the extent range to free any 2308 * existing extent state. 2309 */ 2310 unlock_extent(tree, start, end, &cached_state); 2311 return 0; 2312 } 2313 2314 /* 2315 * a helper for release_folio, this tests for areas of the page that 2316 * are locked or under IO and drops the related state bits if it is safe 2317 * to drop the page. 2318 */ 2319 static bool try_release_extent_state(struct extent_io_tree *tree, 2320 struct folio *folio, gfp_t mask) 2321 { 2322 u64 start = folio_pos(folio); 2323 u64 end = start + PAGE_SIZE - 1; 2324 bool ret; 2325 2326 if (test_range_bit_exists(tree, start, end, EXTENT_LOCKED)) { 2327 ret = false; 2328 } else { 2329 u32 clear_bits = ~(EXTENT_LOCKED | EXTENT_NODATASUM | 2330 EXTENT_DELALLOC_NEW | EXTENT_CTLBITS | 2331 EXTENT_QGROUP_RESERVED); 2332 int ret2; 2333 2334 /* 2335 * At this point we can safely clear everything except the 2336 * locked bit, the nodatasum bit and the delalloc new bit. 2337 * The delalloc new bit will be cleared by ordered extent 2338 * completion. 2339 */ 2340 ret2 = __clear_extent_bit(tree, start, end, clear_bits, NULL, NULL); 2341 2342 /* if clear_extent_bit failed for enomem reasons, 2343 * we can't allow the release to continue. 2344 */ 2345 if (ret2 < 0) 2346 ret = false; 2347 else 2348 ret = true; 2349 } 2350 return ret; 2351 } 2352 2353 /* 2354 * a helper for release_folio. As long as there are no locked extents 2355 * in the range corresponding to the page, both state records and extent 2356 * map records are removed 2357 */ 2358 bool try_release_extent_mapping(struct folio *folio, gfp_t mask) 2359 { 2360 u64 start = folio_pos(folio); 2361 u64 end = start + PAGE_SIZE - 1; 2362 struct btrfs_inode *inode = folio_to_inode(folio); 2363 struct extent_io_tree *io_tree = &inode->io_tree; 2364 2365 while (start <= end) { 2366 const u64 cur_gen = btrfs_get_fs_generation(inode->root->fs_info); 2367 const u64 len = end - start + 1; 2368 struct extent_map_tree *extent_tree = &inode->extent_tree; 2369 struct extent_map *em; 2370 2371 write_lock(&extent_tree->lock); 2372 em = lookup_extent_mapping(extent_tree, start, len); 2373 if (!em) { 2374 write_unlock(&extent_tree->lock); 2375 break; 2376 } 2377 if ((em->flags & EXTENT_FLAG_PINNED) || em->start != start) { 2378 write_unlock(&extent_tree->lock); 2379 free_extent_map(em); 2380 break; 2381 } 2382 if (test_range_bit_exists(io_tree, em->start, 2383 extent_map_end(em) - 1, EXTENT_LOCKED)) 2384 goto next; 2385 /* 2386 * If it's not in the list of modified extents, used by a fast 2387 * fsync, we can remove it. If it's being logged we can safely 2388 * remove it since fsync took an extra reference on the em. 2389 */ 2390 if (list_empty(&em->list) || (em->flags & EXTENT_FLAG_LOGGING)) 2391 goto remove_em; 2392 /* 2393 * If it's in the list of modified extents, remove it only if 2394 * its generation is older then the current one, in which case 2395 * we don't need it for a fast fsync. Otherwise don't remove it, 2396 * we could be racing with an ongoing fast fsync that could miss 2397 * the new extent. 2398 */ 2399 if (em->generation >= cur_gen) 2400 goto next; 2401 remove_em: 2402 /* 2403 * We only remove extent maps that are not in the list of 2404 * modified extents or that are in the list but with a 2405 * generation lower then the current generation, so there is no 2406 * need to set the full fsync flag on the inode (it hurts the 2407 * fsync performance for workloads with a data size that exceeds 2408 * or is close to the system's memory). 2409 */ 2410 remove_extent_mapping(inode, em); 2411 /* Once for the inode's extent map tree. */ 2412 free_extent_map(em); 2413 next: 2414 start = extent_map_end(em); 2415 write_unlock(&extent_tree->lock); 2416 2417 /* Once for us, for the lookup_extent_mapping() reference. */ 2418 free_extent_map(em); 2419 2420 if (need_resched()) { 2421 /* 2422 * If we need to resched but we can't block just exit 2423 * and leave any remaining extent maps. 2424 */ 2425 if (!gfpflags_allow_blocking(mask)) 2426 break; 2427 2428 cond_resched(); 2429 } 2430 } 2431 return try_release_extent_state(io_tree, folio, mask); 2432 } 2433 2434 static void __free_extent_buffer(struct extent_buffer *eb) 2435 { 2436 kmem_cache_free(extent_buffer_cache, eb); 2437 } 2438 2439 static int extent_buffer_under_io(const struct extent_buffer *eb) 2440 { 2441 return (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || 2442 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 2443 } 2444 2445 static bool folio_range_has_eb(struct btrfs_fs_info *fs_info, struct folio *folio) 2446 { 2447 struct btrfs_subpage *subpage; 2448 2449 lockdep_assert_held(&folio->mapping->i_private_lock); 2450 2451 if (folio_test_private(folio)) { 2452 subpage = folio_get_private(folio); 2453 if (atomic_read(&subpage->eb_refs)) 2454 return true; 2455 /* 2456 * Even there is no eb refs here, we may still have 2457 * end_folio_read() call relying on page::private. 2458 */ 2459 if (atomic_read(&subpage->readers)) 2460 return true; 2461 } 2462 return false; 2463 } 2464 2465 static void detach_extent_buffer_folio(const struct extent_buffer *eb, struct folio *folio) 2466 { 2467 struct btrfs_fs_info *fs_info = eb->fs_info; 2468 const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); 2469 2470 /* 2471 * For mapped eb, we're going to change the folio private, which should 2472 * be done under the i_private_lock. 2473 */ 2474 if (mapped) 2475 spin_lock(&folio->mapping->i_private_lock); 2476 2477 if (!folio_test_private(folio)) { 2478 if (mapped) 2479 spin_unlock(&folio->mapping->i_private_lock); 2480 return; 2481 } 2482 2483 if (fs_info->nodesize >= PAGE_SIZE) { 2484 /* 2485 * We do this since we'll remove the pages after we've 2486 * removed the eb from the radix tree, so we could race 2487 * and have this page now attached to the new eb. So 2488 * only clear folio if it's still connected to 2489 * this eb. 2490 */ 2491 if (folio_test_private(folio) && folio_get_private(folio) == eb) { 2492 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 2493 BUG_ON(folio_test_dirty(folio)); 2494 BUG_ON(folio_test_writeback(folio)); 2495 /* We need to make sure we haven't be attached to a new eb. */ 2496 folio_detach_private(folio); 2497 } 2498 if (mapped) 2499 spin_unlock(&folio->mapping->i_private_lock); 2500 return; 2501 } 2502 2503 /* 2504 * For subpage, we can have dummy eb with folio private attached. In 2505 * this case, we can directly detach the private as such folio is only 2506 * attached to one dummy eb, no sharing. 2507 */ 2508 if (!mapped) { 2509 btrfs_detach_subpage(fs_info, folio); 2510 return; 2511 } 2512 2513 btrfs_folio_dec_eb_refs(fs_info, folio); 2514 2515 /* 2516 * We can only detach the folio private if there are no other ebs in the 2517 * page range and no unfinished IO. 2518 */ 2519 if (!folio_range_has_eb(fs_info, folio)) 2520 btrfs_detach_subpage(fs_info, folio); 2521 2522 spin_unlock(&folio->mapping->i_private_lock); 2523 } 2524 2525 /* Release all pages attached to the extent buffer */ 2526 static void btrfs_release_extent_buffer_pages(const struct extent_buffer *eb) 2527 { 2528 ASSERT(!extent_buffer_under_io(eb)); 2529 2530 for (int i = 0; i < INLINE_EXTENT_BUFFER_PAGES; i++) { 2531 struct folio *folio = eb->folios[i]; 2532 2533 if (!folio) 2534 continue; 2535 2536 detach_extent_buffer_folio(eb, folio); 2537 2538 /* One for when we allocated the folio. */ 2539 folio_put(folio); 2540 } 2541 } 2542 2543 /* 2544 * Helper for releasing the extent buffer. 2545 */ 2546 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) 2547 { 2548 btrfs_release_extent_buffer_pages(eb); 2549 btrfs_leak_debug_del_eb(eb); 2550 __free_extent_buffer(eb); 2551 } 2552 2553 static struct extent_buffer * 2554 __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, 2555 unsigned long len) 2556 { 2557 struct extent_buffer *eb = NULL; 2558 2559 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); 2560 eb->start = start; 2561 eb->len = len; 2562 eb->fs_info = fs_info; 2563 init_rwsem(&eb->lock); 2564 2565 btrfs_leak_debug_add_eb(eb); 2566 2567 spin_lock_init(&eb->refs_lock); 2568 atomic_set(&eb->refs, 1); 2569 2570 ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE); 2571 2572 return eb; 2573 } 2574 2575 struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src) 2576 { 2577 struct extent_buffer *new; 2578 int num_folios = num_extent_folios(src); 2579 int ret; 2580 2581 new = __alloc_extent_buffer(src->fs_info, src->start, src->len); 2582 if (new == NULL) 2583 return NULL; 2584 2585 /* 2586 * Set UNMAPPED before calling btrfs_release_extent_buffer(), as 2587 * btrfs_release_extent_buffer() have different behavior for 2588 * UNMAPPED subpage extent buffer. 2589 */ 2590 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags); 2591 2592 ret = alloc_eb_folio_array(new, false); 2593 if (ret) { 2594 btrfs_release_extent_buffer(new); 2595 return NULL; 2596 } 2597 2598 for (int i = 0; i < num_folios; i++) { 2599 struct folio *folio = new->folios[i]; 2600 2601 ret = attach_extent_buffer_folio(new, folio, NULL); 2602 if (ret < 0) { 2603 btrfs_release_extent_buffer(new); 2604 return NULL; 2605 } 2606 WARN_ON(folio_test_dirty(folio)); 2607 } 2608 copy_extent_buffer_full(new, src); 2609 set_extent_buffer_uptodate(new); 2610 2611 return new; 2612 } 2613 2614 struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, 2615 u64 start, unsigned long len) 2616 { 2617 struct extent_buffer *eb; 2618 int num_folios = 0; 2619 int ret; 2620 2621 eb = __alloc_extent_buffer(fs_info, start, len); 2622 if (!eb) 2623 return NULL; 2624 2625 ret = alloc_eb_folio_array(eb, false); 2626 if (ret) 2627 goto err; 2628 2629 num_folios = num_extent_folios(eb); 2630 for (int i = 0; i < num_folios; i++) { 2631 ret = attach_extent_buffer_folio(eb, eb->folios[i], NULL); 2632 if (ret < 0) 2633 goto err; 2634 } 2635 2636 set_extent_buffer_uptodate(eb); 2637 btrfs_set_header_nritems(eb, 0); 2638 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); 2639 2640 return eb; 2641 err: 2642 for (int i = 0; i < num_folios; i++) { 2643 if (eb->folios[i]) { 2644 detach_extent_buffer_folio(eb, eb->folios[i]); 2645 folio_put(eb->folios[i]); 2646 } 2647 } 2648 __free_extent_buffer(eb); 2649 return NULL; 2650 } 2651 2652 struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, 2653 u64 start) 2654 { 2655 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize); 2656 } 2657 2658 static void check_buffer_tree_ref(struct extent_buffer *eb) 2659 { 2660 int refs; 2661 /* 2662 * The TREE_REF bit is first set when the extent_buffer is added 2663 * to the radix tree. It is also reset, if unset, when a new reference 2664 * is created by find_extent_buffer. 2665 * 2666 * It is only cleared in two cases: freeing the last non-tree 2667 * reference to the extent_buffer when its STALE bit is set or 2668 * calling release_folio when the tree reference is the only reference. 2669 * 2670 * In both cases, care is taken to ensure that the extent_buffer's 2671 * pages are not under io. However, release_folio can be concurrently 2672 * called with creating new references, which is prone to race 2673 * conditions between the calls to check_buffer_tree_ref in those 2674 * codepaths and clearing TREE_REF in try_release_extent_buffer. 2675 * 2676 * The actual lifetime of the extent_buffer in the radix tree is 2677 * adequately protected by the refcount, but the TREE_REF bit and 2678 * its corresponding reference are not. To protect against this 2679 * class of races, we call check_buffer_tree_ref from the codepaths 2680 * which trigger io. Note that once io is initiated, TREE_REF can no 2681 * longer be cleared, so that is the moment at which any such race is 2682 * best fixed. 2683 */ 2684 refs = atomic_read(&eb->refs); 2685 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) 2686 return; 2687 2688 spin_lock(&eb->refs_lock); 2689 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) 2690 atomic_inc(&eb->refs); 2691 spin_unlock(&eb->refs_lock); 2692 } 2693 2694 static void mark_extent_buffer_accessed(struct extent_buffer *eb) 2695 { 2696 int num_folios= num_extent_folios(eb); 2697 2698 check_buffer_tree_ref(eb); 2699 2700 for (int i = 0; i < num_folios; i++) 2701 folio_mark_accessed(eb->folios[i]); 2702 } 2703 2704 struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, 2705 u64 start) 2706 { 2707 struct extent_buffer *eb; 2708 2709 eb = find_extent_buffer_nolock(fs_info, start); 2710 if (!eb) 2711 return NULL; 2712 /* 2713 * Lock our eb's refs_lock to avoid races with free_extent_buffer(). 2714 * When we get our eb it might be flagged with EXTENT_BUFFER_STALE and 2715 * another task running free_extent_buffer() might have seen that flag 2716 * set, eb->refs == 2, that the buffer isn't under IO (dirty and 2717 * writeback flags not set) and it's still in the tree (flag 2718 * EXTENT_BUFFER_TREE_REF set), therefore being in the process of 2719 * decrementing the extent buffer's reference count twice. So here we 2720 * could race and increment the eb's reference count, clear its stale 2721 * flag, mark it as dirty and drop our reference before the other task 2722 * finishes executing free_extent_buffer, which would later result in 2723 * an attempt to free an extent buffer that is dirty. 2724 */ 2725 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { 2726 spin_lock(&eb->refs_lock); 2727 spin_unlock(&eb->refs_lock); 2728 } 2729 mark_extent_buffer_accessed(eb); 2730 return eb; 2731 } 2732 2733 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 2734 struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, 2735 u64 start) 2736 { 2737 struct extent_buffer *eb, *exists = NULL; 2738 int ret; 2739 2740 eb = find_extent_buffer(fs_info, start); 2741 if (eb) 2742 return eb; 2743 eb = alloc_dummy_extent_buffer(fs_info, start); 2744 if (!eb) 2745 return ERR_PTR(-ENOMEM); 2746 eb->fs_info = fs_info; 2747 again: 2748 ret = radix_tree_preload(GFP_NOFS); 2749 if (ret) { 2750 exists = ERR_PTR(ret); 2751 goto free_eb; 2752 } 2753 spin_lock(&fs_info->buffer_lock); 2754 ret = radix_tree_insert(&fs_info->buffer_radix, 2755 start >> fs_info->sectorsize_bits, eb); 2756 spin_unlock(&fs_info->buffer_lock); 2757 radix_tree_preload_end(); 2758 if (ret == -EEXIST) { 2759 exists = find_extent_buffer(fs_info, start); 2760 if (exists) 2761 goto free_eb; 2762 else 2763 goto again; 2764 } 2765 check_buffer_tree_ref(eb); 2766 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); 2767 2768 return eb; 2769 free_eb: 2770 btrfs_release_extent_buffer(eb); 2771 return exists; 2772 } 2773 #endif 2774 2775 static struct extent_buffer *grab_extent_buffer( 2776 struct btrfs_fs_info *fs_info, struct page *page) 2777 { 2778 struct folio *folio = page_folio(page); 2779 struct extent_buffer *exists; 2780 2781 lockdep_assert_held(&page->mapping->i_private_lock); 2782 2783 /* 2784 * For subpage case, we completely rely on radix tree to ensure we 2785 * don't try to insert two ebs for the same bytenr. So here we always 2786 * return NULL and just continue. 2787 */ 2788 if (fs_info->nodesize < PAGE_SIZE) 2789 return NULL; 2790 2791 /* Page not yet attached to an extent buffer */ 2792 if (!folio_test_private(folio)) 2793 return NULL; 2794 2795 /* 2796 * We could have already allocated an eb for this page and attached one 2797 * so lets see if we can get a ref on the existing eb, and if we can we 2798 * know it's good and we can just return that one, else we know we can 2799 * just overwrite folio private. 2800 */ 2801 exists = folio_get_private(folio); 2802 if (atomic_inc_not_zero(&exists->refs)) 2803 return exists; 2804 2805 WARN_ON(PageDirty(page)); 2806 folio_detach_private(folio); 2807 return NULL; 2808 } 2809 2810 static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) 2811 { 2812 if (!IS_ALIGNED(start, fs_info->sectorsize)) { 2813 btrfs_err(fs_info, "bad tree block start %llu", start); 2814 return -EINVAL; 2815 } 2816 2817 if (fs_info->nodesize < PAGE_SIZE && 2818 offset_in_page(start) + fs_info->nodesize > PAGE_SIZE) { 2819 btrfs_err(fs_info, 2820 "tree block crosses page boundary, start %llu nodesize %u", 2821 start, fs_info->nodesize); 2822 return -EINVAL; 2823 } 2824 if (fs_info->nodesize >= PAGE_SIZE && 2825 !PAGE_ALIGNED(start)) { 2826 btrfs_err(fs_info, 2827 "tree block is not page aligned, start %llu nodesize %u", 2828 start, fs_info->nodesize); 2829 return -EINVAL; 2830 } 2831 if (!IS_ALIGNED(start, fs_info->nodesize) && 2832 !test_and_set_bit(BTRFS_FS_UNALIGNED_TREE_BLOCK, &fs_info->flags)) { 2833 btrfs_warn(fs_info, 2834 "tree block not nodesize aligned, start %llu nodesize %u, can be resolved by a full metadata balance", 2835 start, fs_info->nodesize); 2836 } 2837 return 0; 2838 } 2839 2840 2841 /* 2842 * Return 0 if eb->folios[i] is attached to btree inode successfully. 2843 * Return >0 if there is already another extent buffer for the range, 2844 * and @found_eb_ret would be updated. 2845 * Return -EAGAIN if the filemap has an existing folio but with different size 2846 * than @eb. 2847 * The caller needs to free the existing folios and retry using the same order. 2848 */ 2849 static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i, 2850 struct btrfs_subpage *prealloc, 2851 struct extent_buffer **found_eb_ret) 2852 { 2853 2854 struct btrfs_fs_info *fs_info = eb->fs_info; 2855 struct address_space *mapping = fs_info->btree_inode->i_mapping; 2856 const unsigned long index = eb->start >> PAGE_SHIFT; 2857 struct folio *existing_folio = NULL; 2858 int ret; 2859 2860 ASSERT(found_eb_ret); 2861 2862 /* Caller should ensure the folio exists. */ 2863 ASSERT(eb->folios[i]); 2864 2865 retry: 2866 ret = filemap_add_folio(mapping, eb->folios[i], index + i, 2867 GFP_NOFS | __GFP_NOFAIL); 2868 if (!ret) 2869 goto finish; 2870 2871 existing_folio = filemap_lock_folio(mapping, index + i); 2872 /* The page cache only exists for a very short time, just retry. */ 2873 if (IS_ERR(existing_folio)) { 2874 existing_folio = NULL; 2875 goto retry; 2876 } 2877 2878 /* For now, we should only have single-page folios for btree inode. */ 2879 ASSERT(folio_nr_pages(existing_folio) == 1); 2880 2881 if (folio_size(existing_folio) != eb->folio_size) { 2882 folio_unlock(existing_folio); 2883 folio_put(existing_folio); 2884 return -EAGAIN; 2885 } 2886 2887 finish: 2888 spin_lock(&mapping->i_private_lock); 2889 if (existing_folio && fs_info->nodesize < PAGE_SIZE) { 2890 /* We're going to reuse the existing page, can drop our folio now. */ 2891 __free_page(folio_page(eb->folios[i], 0)); 2892 eb->folios[i] = existing_folio; 2893 } else if (existing_folio) { 2894 struct extent_buffer *existing_eb; 2895 2896 existing_eb = grab_extent_buffer(fs_info, 2897 folio_page(existing_folio, 0)); 2898 if (existing_eb) { 2899 /* The extent buffer still exists, we can use it directly. */ 2900 *found_eb_ret = existing_eb; 2901 spin_unlock(&mapping->i_private_lock); 2902 folio_unlock(existing_folio); 2903 folio_put(existing_folio); 2904 return 1; 2905 } 2906 /* The extent buffer no longer exists, we can reuse the folio. */ 2907 __free_page(folio_page(eb->folios[i], 0)); 2908 eb->folios[i] = existing_folio; 2909 } 2910 eb->folio_size = folio_size(eb->folios[i]); 2911 eb->folio_shift = folio_shift(eb->folios[i]); 2912 /* Should not fail, as we have preallocated the memory. */ 2913 ret = attach_extent_buffer_folio(eb, eb->folios[i], prealloc); 2914 ASSERT(!ret); 2915 /* 2916 * To inform we have an extra eb under allocation, so that 2917 * detach_extent_buffer_page() won't release the folio private when the 2918 * eb hasn't been inserted into radix tree yet. 2919 * 2920 * The ref will be decreased when the eb releases the page, in 2921 * detach_extent_buffer_page(). Thus needs no special handling in the 2922 * error path. 2923 */ 2924 btrfs_folio_inc_eb_refs(fs_info, eb->folios[i]); 2925 spin_unlock(&mapping->i_private_lock); 2926 return 0; 2927 } 2928 2929 struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, 2930 u64 start, u64 owner_root, int level) 2931 { 2932 unsigned long len = fs_info->nodesize; 2933 int num_folios; 2934 int attached = 0; 2935 struct extent_buffer *eb; 2936 struct extent_buffer *existing_eb = NULL; 2937 struct btrfs_subpage *prealloc = NULL; 2938 u64 lockdep_owner = owner_root; 2939 bool page_contig = true; 2940 int uptodate = 1; 2941 int ret; 2942 2943 if (check_eb_alignment(fs_info, start)) 2944 return ERR_PTR(-EINVAL); 2945 2946 #if BITS_PER_LONG == 32 2947 if (start >= MAX_LFS_FILESIZE) { 2948 btrfs_err_rl(fs_info, 2949 "extent buffer %llu is beyond 32bit page cache limit", start); 2950 btrfs_err_32bit_limit(fs_info); 2951 return ERR_PTR(-EOVERFLOW); 2952 } 2953 if (start >= BTRFS_32BIT_EARLY_WARN_THRESHOLD) 2954 btrfs_warn_32bit_limit(fs_info); 2955 #endif 2956 2957 eb = find_extent_buffer(fs_info, start); 2958 if (eb) 2959 return eb; 2960 2961 eb = __alloc_extent_buffer(fs_info, start, len); 2962 if (!eb) 2963 return ERR_PTR(-ENOMEM); 2964 2965 /* 2966 * The reloc trees are just snapshots, so we need them to appear to be 2967 * just like any other fs tree WRT lockdep. 2968 */ 2969 if (lockdep_owner == BTRFS_TREE_RELOC_OBJECTID) 2970 lockdep_owner = BTRFS_FS_TREE_OBJECTID; 2971 2972 btrfs_set_buffer_lockdep_class(lockdep_owner, eb, level); 2973 2974 /* 2975 * Preallocate folio private for subpage case, so that we won't 2976 * allocate memory with i_private_lock nor page lock hold. 2977 * 2978 * The memory will be freed by attach_extent_buffer_page() or freed 2979 * manually if we exit earlier. 2980 */ 2981 if (fs_info->nodesize < PAGE_SIZE) { 2982 prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA); 2983 if (IS_ERR(prealloc)) { 2984 ret = PTR_ERR(prealloc); 2985 goto out; 2986 } 2987 } 2988 2989 reallocate: 2990 /* Allocate all pages first. */ 2991 ret = alloc_eb_folio_array(eb, true); 2992 if (ret < 0) { 2993 btrfs_free_subpage(prealloc); 2994 goto out; 2995 } 2996 2997 num_folios = num_extent_folios(eb); 2998 /* Attach all pages to the filemap. */ 2999 for (int i = 0; i < num_folios; i++) { 3000 struct folio *folio; 3001 3002 ret = attach_eb_folio_to_filemap(eb, i, prealloc, &existing_eb); 3003 if (ret > 0) { 3004 ASSERT(existing_eb); 3005 goto out; 3006 } 3007 3008 /* 3009 * TODO: Special handling for a corner case where the order of 3010 * folios mismatch between the new eb and filemap. 3011 * 3012 * This happens when: 3013 * 3014 * - the new eb is using higher order folio 3015 * 3016 * - the filemap is still using 0-order folios for the range 3017 * This can happen at the previous eb allocation, and we don't 3018 * have higher order folio for the call. 3019 * 3020 * - the existing eb has already been freed 3021 * 3022 * In this case, we have to free the existing folios first, and 3023 * re-allocate using the same order. 3024 * Thankfully this is not going to happen yet, as we're still 3025 * using 0-order folios. 3026 */ 3027 if (unlikely(ret == -EAGAIN)) { 3028 ASSERT(0); 3029 goto reallocate; 3030 } 3031 attached++; 3032 3033 /* 3034 * Only after attach_eb_folio_to_filemap(), eb->folios[] is 3035 * reliable, as we may choose to reuse the existing page cache 3036 * and free the allocated page. 3037 */ 3038 folio = eb->folios[i]; 3039 WARN_ON(btrfs_folio_test_dirty(fs_info, folio, eb->start, eb->len)); 3040 3041 /* 3042 * Check if the current page is physically contiguous with previous eb 3043 * page. 3044 * At this stage, either we allocated a large folio, thus @i 3045 * would only be 0, or we fall back to per-page allocation. 3046 */ 3047 if (i && folio_page(eb->folios[i - 1], 0) + 1 != folio_page(folio, 0)) 3048 page_contig = false; 3049 3050 if (!btrfs_folio_test_uptodate(fs_info, folio, eb->start, eb->len)) 3051 uptodate = 0; 3052 3053 /* 3054 * We can't unlock the pages just yet since the extent buffer 3055 * hasn't been properly inserted in the radix tree, this 3056 * opens a race with btree_release_folio which can free a page 3057 * while we are still filling in all pages for the buffer and 3058 * we could crash. 3059 */ 3060 } 3061 if (uptodate) 3062 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 3063 /* All pages are physically contiguous, can skip cross page handling. */ 3064 if (page_contig) 3065 eb->addr = folio_address(eb->folios[0]) + offset_in_page(eb->start); 3066 again: 3067 ret = radix_tree_preload(GFP_NOFS); 3068 if (ret) 3069 goto out; 3070 3071 spin_lock(&fs_info->buffer_lock); 3072 ret = radix_tree_insert(&fs_info->buffer_radix, 3073 start >> fs_info->sectorsize_bits, eb); 3074 spin_unlock(&fs_info->buffer_lock); 3075 radix_tree_preload_end(); 3076 if (ret == -EEXIST) { 3077 ret = 0; 3078 existing_eb = find_extent_buffer(fs_info, start); 3079 if (existing_eb) 3080 goto out; 3081 else 3082 goto again; 3083 } 3084 /* add one reference for the tree */ 3085 check_buffer_tree_ref(eb); 3086 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); 3087 3088 /* 3089 * Now it's safe to unlock the pages because any calls to 3090 * btree_release_folio will correctly detect that a page belongs to a 3091 * live buffer and won't free them prematurely. 3092 */ 3093 for (int i = 0; i < num_folios; i++) 3094 unlock_page(folio_page(eb->folios[i], 0)); 3095 return eb; 3096 3097 out: 3098 WARN_ON(!atomic_dec_and_test(&eb->refs)); 3099 3100 /* 3101 * Any attached folios need to be detached before we unlock them. This 3102 * is because when we're inserting our new folios into the mapping, and 3103 * then attaching our eb to that folio. If we fail to insert our folio 3104 * we'll lookup the folio for that index, and grab that EB. We do not 3105 * want that to grab this eb, as we're getting ready to free it. So we 3106 * have to detach it first and then unlock it. 3107 * 3108 * We have to drop our reference and NULL it out here because in the 3109 * subpage case detaching does a btrfs_folio_dec_eb_refs() for our eb. 3110 * Below when we call btrfs_release_extent_buffer() we will call 3111 * detach_extent_buffer_folio() on our remaining pages in the !subpage 3112 * case. If we left eb->folios[i] populated in the subpage case we'd 3113 * double put our reference and be super sad. 3114 */ 3115 for (int i = 0; i < attached; i++) { 3116 ASSERT(eb->folios[i]); 3117 detach_extent_buffer_folio(eb, eb->folios[i]); 3118 unlock_page(folio_page(eb->folios[i], 0)); 3119 folio_put(eb->folios[i]); 3120 eb->folios[i] = NULL; 3121 } 3122 /* 3123 * Now all pages of that extent buffer is unmapped, set UNMAPPED flag, 3124 * so it can be cleaned up without utlizing page->mapping. 3125 */ 3126 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); 3127 3128 btrfs_release_extent_buffer(eb); 3129 if (ret < 0) 3130 return ERR_PTR(ret); 3131 ASSERT(existing_eb); 3132 return existing_eb; 3133 } 3134 3135 static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) 3136 { 3137 struct extent_buffer *eb = 3138 container_of(head, struct extent_buffer, rcu_head); 3139 3140 __free_extent_buffer(eb); 3141 } 3142 3143 static int release_extent_buffer(struct extent_buffer *eb) 3144 __releases(&eb->refs_lock) 3145 { 3146 lockdep_assert_held(&eb->refs_lock); 3147 3148 WARN_ON(atomic_read(&eb->refs) == 0); 3149 if (atomic_dec_and_test(&eb->refs)) { 3150 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { 3151 struct btrfs_fs_info *fs_info = eb->fs_info; 3152 3153 spin_unlock(&eb->refs_lock); 3154 3155 spin_lock(&fs_info->buffer_lock); 3156 radix_tree_delete(&fs_info->buffer_radix, 3157 eb->start >> fs_info->sectorsize_bits); 3158 spin_unlock(&fs_info->buffer_lock); 3159 } else { 3160 spin_unlock(&eb->refs_lock); 3161 } 3162 3163 btrfs_leak_debug_del_eb(eb); 3164 /* Should be safe to release our pages at this point */ 3165 btrfs_release_extent_buffer_pages(eb); 3166 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 3167 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) { 3168 __free_extent_buffer(eb); 3169 return 1; 3170 } 3171 #endif 3172 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); 3173 return 1; 3174 } 3175 spin_unlock(&eb->refs_lock); 3176 3177 return 0; 3178 } 3179 3180 void free_extent_buffer(struct extent_buffer *eb) 3181 { 3182 int refs; 3183 if (!eb) 3184 return; 3185 3186 refs = atomic_read(&eb->refs); 3187 while (1) { 3188 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3) 3189 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && 3190 refs == 1)) 3191 break; 3192 if (atomic_try_cmpxchg(&eb->refs, &refs, refs - 1)) 3193 return; 3194 } 3195 3196 spin_lock(&eb->refs_lock); 3197 if (atomic_read(&eb->refs) == 2 && 3198 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && 3199 !extent_buffer_under_io(eb) && 3200 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) 3201 atomic_dec(&eb->refs); 3202 3203 /* 3204 * I know this is terrible, but it's temporary until we stop tracking 3205 * the uptodate bits and such for the extent buffers. 3206 */ 3207 release_extent_buffer(eb); 3208 } 3209 3210 void free_extent_buffer_stale(struct extent_buffer *eb) 3211 { 3212 if (!eb) 3213 return; 3214 3215 spin_lock(&eb->refs_lock); 3216 set_bit(EXTENT_BUFFER_STALE, &eb->bflags); 3217 3218 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && 3219 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) 3220 atomic_dec(&eb->refs); 3221 release_extent_buffer(eb); 3222 } 3223 3224 static void btree_clear_folio_dirty(struct folio *folio) 3225 { 3226 ASSERT(folio_test_dirty(folio)); 3227 ASSERT(folio_test_locked(folio)); 3228 folio_clear_dirty_for_io(folio); 3229 xa_lock_irq(&folio->mapping->i_pages); 3230 if (!folio_test_dirty(folio)) 3231 __xa_clear_mark(&folio->mapping->i_pages, 3232 folio_index(folio), PAGECACHE_TAG_DIRTY); 3233 xa_unlock_irq(&folio->mapping->i_pages); 3234 } 3235 3236 static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb) 3237 { 3238 struct btrfs_fs_info *fs_info = eb->fs_info; 3239 struct folio *folio = eb->folios[0]; 3240 bool last; 3241 3242 /* btree_clear_folio_dirty() needs page locked. */ 3243 folio_lock(folio); 3244 last = btrfs_subpage_clear_and_test_dirty(fs_info, folio, eb->start, eb->len); 3245 if (last) 3246 btree_clear_folio_dirty(folio); 3247 folio_unlock(folio); 3248 WARN_ON(atomic_read(&eb->refs) == 0); 3249 } 3250 3251 void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans, 3252 struct extent_buffer *eb) 3253 { 3254 struct btrfs_fs_info *fs_info = eb->fs_info; 3255 int num_folios; 3256 3257 btrfs_assert_tree_write_locked(eb); 3258 3259 if (trans && btrfs_header_generation(eb) != trans->transid) 3260 return; 3261 3262 /* 3263 * Instead of clearing the dirty flag off of the buffer, mark it as 3264 * EXTENT_BUFFER_ZONED_ZEROOUT. This allows us to preserve 3265 * write-ordering in zoned mode, without the need to later re-dirty 3266 * the extent_buffer. 3267 * 3268 * The actual zeroout of the buffer will happen later in 3269 * btree_csum_one_bio. 3270 */ 3271 if (btrfs_is_zoned(fs_info) && test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { 3272 set_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags); 3273 return; 3274 } 3275 3276 if (!test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) 3277 return; 3278 3279 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, -eb->len, 3280 fs_info->dirty_metadata_batch); 3281 3282 if (eb->fs_info->nodesize < PAGE_SIZE) 3283 return clear_subpage_extent_buffer_dirty(eb); 3284 3285 num_folios = num_extent_folios(eb); 3286 for (int i = 0; i < num_folios; i++) { 3287 struct folio *folio = eb->folios[i]; 3288 3289 if (!folio_test_dirty(folio)) 3290 continue; 3291 folio_lock(folio); 3292 btree_clear_folio_dirty(folio); 3293 folio_unlock(folio); 3294 } 3295 WARN_ON(atomic_read(&eb->refs) == 0); 3296 } 3297 3298 void set_extent_buffer_dirty(struct extent_buffer *eb) 3299 { 3300 int num_folios; 3301 bool was_dirty; 3302 3303 check_buffer_tree_ref(eb); 3304 3305 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); 3306 3307 num_folios = num_extent_folios(eb); 3308 WARN_ON(atomic_read(&eb->refs) == 0); 3309 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); 3310 WARN_ON(test_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags)); 3311 3312 if (!was_dirty) { 3313 bool subpage = eb->fs_info->nodesize < PAGE_SIZE; 3314 3315 /* 3316 * For subpage case, we can have other extent buffers in the 3317 * same page, and in clear_subpage_extent_buffer_dirty() we 3318 * have to clear page dirty without subpage lock held. 3319 * This can cause race where our page gets dirty cleared after 3320 * we just set it. 3321 * 3322 * Thankfully, clear_subpage_extent_buffer_dirty() has locked 3323 * its page for other reasons, we can use page lock to prevent 3324 * the above race. 3325 */ 3326 if (subpage) 3327 lock_page(folio_page(eb->folios[0], 0)); 3328 for (int i = 0; i < num_folios; i++) 3329 btrfs_folio_set_dirty(eb->fs_info, eb->folios[i], 3330 eb->start, eb->len); 3331 if (subpage) 3332 unlock_page(folio_page(eb->folios[0], 0)); 3333 percpu_counter_add_batch(&eb->fs_info->dirty_metadata_bytes, 3334 eb->len, 3335 eb->fs_info->dirty_metadata_batch); 3336 } 3337 #ifdef CONFIG_BTRFS_DEBUG 3338 for (int i = 0; i < num_folios; i++) 3339 ASSERT(folio_test_dirty(eb->folios[i])); 3340 #endif 3341 } 3342 3343 void clear_extent_buffer_uptodate(struct extent_buffer *eb) 3344 { 3345 struct btrfs_fs_info *fs_info = eb->fs_info; 3346 int num_folios = num_extent_folios(eb); 3347 3348 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 3349 for (int i = 0; i < num_folios; i++) { 3350 struct folio *folio = eb->folios[i]; 3351 3352 if (!folio) 3353 continue; 3354 3355 /* 3356 * This is special handling for metadata subpage, as regular 3357 * btrfs_is_subpage() can not handle cloned/dummy metadata. 3358 */ 3359 if (fs_info->nodesize >= PAGE_SIZE) 3360 folio_clear_uptodate(folio); 3361 else 3362 btrfs_subpage_clear_uptodate(fs_info, folio, 3363 eb->start, eb->len); 3364 } 3365 } 3366 3367 void set_extent_buffer_uptodate(struct extent_buffer *eb) 3368 { 3369 struct btrfs_fs_info *fs_info = eb->fs_info; 3370 int num_folios = num_extent_folios(eb); 3371 3372 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 3373 for (int i = 0; i < num_folios; i++) { 3374 struct folio *folio = eb->folios[i]; 3375 3376 /* 3377 * This is special handling for metadata subpage, as regular 3378 * btrfs_is_subpage() can not handle cloned/dummy metadata. 3379 */ 3380 if (fs_info->nodesize >= PAGE_SIZE) 3381 folio_mark_uptodate(folio); 3382 else 3383 btrfs_subpage_set_uptodate(fs_info, folio, 3384 eb->start, eb->len); 3385 } 3386 } 3387 3388 static void clear_extent_buffer_reading(struct extent_buffer *eb) 3389 { 3390 clear_bit(EXTENT_BUFFER_READING, &eb->bflags); 3391 smp_mb__after_atomic(); 3392 wake_up_bit(&eb->bflags, EXTENT_BUFFER_READING); 3393 } 3394 3395 static void end_bbio_meta_read(struct btrfs_bio *bbio) 3396 { 3397 struct extent_buffer *eb = bbio->private; 3398 struct btrfs_fs_info *fs_info = eb->fs_info; 3399 bool uptodate = !bbio->bio.bi_status; 3400 struct folio_iter fi; 3401 u32 bio_offset = 0; 3402 3403 /* 3404 * If the extent buffer is marked UPTODATE before the read operation 3405 * completes, other calls to read_extent_buffer_pages() will return 3406 * early without waiting for the read to finish, causing data races. 3407 */ 3408 WARN_ON(test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)); 3409 3410 eb->read_mirror = bbio->mirror_num; 3411 3412 if (uptodate && 3413 btrfs_validate_extent_buffer(eb, &bbio->parent_check) < 0) 3414 uptodate = false; 3415 3416 if (uptodate) { 3417 set_extent_buffer_uptodate(eb); 3418 } else { 3419 clear_extent_buffer_uptodate(eb); 3420 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 3421 } 3422 3423 bio_for_each_folio_all(fi, &bbio->bio) { 3424 struct folio *folio = fi.folio; 3425 u64 start = eb->start + bio_offset; 3426 u32 len = fi.length; 3427 3428 if (uptodate) 3429 btrfs_folio_set_uptodate(fs_info, folio, start, len); 3430 else 3431 btrfs_folio_clear_uptodate(fs_info, folio, start, len); 3432 3433 bio_offset += len; 3434 } 3435 3436 clear_extent_buffer_reading(eb); 3437 free_extent_buffer(eb); 3438 3439 bio_put(&bbio->bio); 3440 } 3441 3442 int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num, 3443 const struct btrfs_tree_parent_check *check) 3444 { 3445 struct btrfs_bio *bbio; 3446 bool ret; 3447 3448 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) 3449 return 0; 3450 3451 /* 3452 * We could have had EXTENT_BUFFER_UPTODATE cleared by the write 3453 * operation, which could potentially still be in flight. In this case 3454 * we simply want to return an error. 3455 */ 3456 if (unlikely(test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))) 3457 return -EIO; 3458 3459 /* Someone else is already reading the buffer, just wait for it. */ 3460 if (test_and_set_bit(EXTENT_BUFFER_READING, &eb->bflags)) 3461 goto done; 3462 3463 /* 3464 * Between the initial test_bit(EXTENT_BUFFER_UPTODATE) and the above 3465 * test_and_set_bit(EXTENT_BUFFER_READING), someone else could have 3466 * started and finished reading the same eb. In this case, UPTODATE 3467 * will now be set, and we shouldn't read it in again. 3468 */ 3469 if (unlikely(test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))) { 3470 clear_extent_buffer_reading(eb); 3471 return 0; 3472 } 3473 3474 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 3475 eb->read_mirror = 0; 3476 check_buffer_tree_ref(eb); 3477 atomic_inc(&eb->refs); 3478 3479 bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES, 3480 REQ_OP_READ | REQ_META, eb->fs_info, 3481 end_bbio_meta_read, eb); 3482 bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT; 3483 bbio->inode = BTRFS_I(eb->fs_info->btree_inode); 3484 bbio->file_offset = eb->start; 3485 memcpy(&bbio->parent_check, check, sizeof(*check)); 3486 if (eb->fs_info->nodesize < PAGE_SIZE) { 3487 ret = bio_add_folio(&bbio->bio, eb->folios[0], eb->len, 3488 eb->start - folio_pos(eb->folios[0])); 3489 ASSERT(ret); 3490 } else { 3491 int num_folios = num_extent_folios(eb); 3492 3493 for (int i = 0; i < num_folios; i++) { 3494 struct folio *folio = eb->folios[i]; 3495 3496 ret = bio_add_folio(&bbio->bio, folio, eb->folio_size, 0); 3497 ASSERT(ret); 3498 } 3499 } 3500 btrfs_submit_bbio(bbio, mirror_num); 3501 3502 done: 3503 if (wait == WAIT_COMPLETE) { 3504 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_READING, TASK_UNINTERRUPTIBLE); 3505 if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) 3506 return -EIO; 3507 } 3508 3509 return 0; 3510 } 3511 3512 static bool report_eb_range(const struct extent_buffer *eb, unsigned long start, 3513 unsigned long len) 3514 { 3515 btrfs_warn(eb->fs_info, 3516 "access to eb bytenr %llu len %u out of range start %lu len %lu", 3517 eb->start, eb->len, start, len); 3518 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 3519 3520 return true; 3521 } 3522 3523 /* 3524 * Check if the [start, start + len) range is valid before reading/writing 3525 * the eb. 3526 * NOTE: @start and @len are offset inside the eb, not logical address. 3527 * 3528 * Caller should not touch the dst/src memory if this function returns error. 3529 */ 3530 static inline int check_eb_range(const struct extent_buffer *eb, 3531 unsigned long start, unsigned long len) 3532 { 3533 unsigned long offset; 3534 3535 /* start, start + len should not go beyond eb->len nor overflow */ 3536 if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len)) 3537 return report_eb_range(eb, start, len); 3538 3539 return false; 3540 } 3541 3542 void read_extent_buffer(const struct extent_buffer *eb, void *dstv, 3543 unsigned long start, unsigned long len) 3544 { 3545 const int unit_size = eb->folio_size; 3546 size_t cur; 3547 size_t offset; 3548 char *dst = (char *)dstv; 3549 unsigned long i = get_eb_folio_index(eb, start); 3550 3551 if (check_eb_range(eb, start, len)) { 3552 /* 3553 * Invalid range hit, reset the memory, so callers won't get 3554 * some random garbage for their uninitialized memory. 3555 */ 3556 memset(dstv, 0, len); 3557 return; 3558 } 3559 3560 if (eb->addr) { 3561 memcpy(dstv, eb->addr + start, len); 3562 return; 3563 } 3564 3565 offset = get_eb_offset_in_folio(eb, start); 3566 3567 while (len > 0) { 3568 char *kaddr; 3569 3570 cur = min(len, unit_size - offset); 3571 kaddr = folio_address(eb->folios[i]); 3572 memcpy(dst, kaddr + offset, cur); 3573 3574 dst += cur; 3575 len -= cur; 3576 offset = 0; 3577 i++; 3578 } 3579 } 3580 3581 int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, 3582 void __user *dstv, 3583 unsigned long start, unsigned long len) 3584 { 3585 const int unit_size = eb->folio_size; 3586 size_t cur; 3587 size_t offset; 3588 char __user *dst = (char __user *)dstv; 3589 unsigned long i = get_eb_folio_index(eb, start); 3590 int ret = 0; 3591 3592 WARN_ON(start > eb->len); 3593 WARN_ON(start + len > eb->start + eb->len); 3594 3595 if (eb->addr) { 3596 if (copy_to_user_nofault(dstv, eb->addr + start, len)) 3597 ret = -EFAULT; 3598 return ret; 3599 } 3600 3601 offset = get_eb_offset_in_folio(eb, start); 3602 3603 while (len > 0) { 3604 char *kaddr; 3605 3606 cur = min(len, unit_size - offset); 3607 kaddr = folio_address(eb->folios[i]); 3608 if (copy_to_user_nofault(dst, kaddr + offset, cur)) { 3609 ret = -EFAULT; 3610 break; 3611 } 3612 3613 dst += cur; 3614 len -= cur; 3615 offset = 0; 3616 i++; 3617 } 3618 3619 return ret; 3620 } 3621 3622 int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, 3623 unsigned long start, unsigned long len) 3624 { 3625 const int unit_size = eb->folio_size; 3626 size_t cur; 3627 size_t offset; 3628 char *kaddr; 3629 char *ptr = (char *)ptrv; 3630 unsigned long i = get_eb_folio_index(eb, start); 3631 int ret = 0; 3632 3633 if (check_eb_range(eb, start, len)) 3634 return -EINVAL; 3635 3636 if (eb->addr) 3637 return memcmp(ptrv, eb->addr + start, len); 3638 3639 offset = get_eb_offset_in_folio(eb, start); 3640 3641 while (len > 0) { 3642 cur = min(len, unit_size - offset); 3643 kaddr = folio_address(eb->folios[i]); 3644 ret = memcmp(ptr, kaddr + offset, cur); 3645 if (ret) 3646 break; 3647 3648 ptr += cur; 3649 len -= cur; 3650 offset = 0; 3651 i++; 3652 } 3653 return ret; 3654 } 3655 3656 /* 3657 * Check that the extent buffer is uptodate. 3658 * 3659 * For regular sector size == PAGE_SIZE case, check if @page is uptodate. 3660 * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE. 3661 */ 3662 static void assert_eb_folio_uptodate(const struct extent_buffer *eb, int i) 3663 { 3664 struct btrfs_fs_info *fs_info = eb->fs_info; 3665 struct folio *folio = eb->folios[i]; 3666 3667 ASSERT(folio); 3668 3669 /* 3670 * If we are using the commit root we could potentially clear a page 3671 * Uptodate while we're using the extent buffer that we've previously 3672 * looked up. We don't want to complain in this case, as the page was 3673 * valid before, we just didn't write it out. Instead we want to catch 3674 * the case where we didn't actually read the block properly, which 3675 * would have !PageUptodate and !EXTENT_BUFFER_WRITE_ERR. 3676 */ 3677 if (test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) 3678 return; 3679 3680 if (fs_info->nodesize < PAGE_SIZE) { 3681 folio = eb->folios[0]; 3682 ASSERT(i == 0); 3683 if (WARN_ON(!btrfs_subpage_test_uptodate(fs_info, folio, 3684 eb->start, eb->len))) 3685 btrfs_subpage_dump_bitmap(fs_info, folio, eb->start, eb->len); 3686 } else { 3687 WARN_ON(!folio_test_uptodate(folio)); 3688 } 3689 } 3690 3691 static void __write_extent_buffer(const struct extent_buffer *eb, 3692 const void *srcv, unsigned long start, 3693 unsigned long len, bool use_memmove) 3694 { 3695 const int unit_size = eb->folio_size; 3696 size_t cur; 3697 size_t offset; 3698 char *kaddr; 3699 const char *src = (const char *)srcv; 3700 unsigned long i = get_eb_folio_index(eb, start); 3701 /* For unmapped (dummy) ebs, no need to check their uptodate status. */ 3702 const bool check_uptodate = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); 3703 3704 if (check_eb_range(eb, start, len)) 3705 return; 3706 3707 if (eb->addr) { 3708 if (use_memmove) 3709 memmove(eb->addr + start, srcv, len); 3710 else 3711 memcpy(eb->addr + start, srcv, len); 3712 return; 3713 } 3714 3715 offset = get_eb_offset_in_folio(eb, start); 3716 3717 while (len > 0) { 3718 if (check_uptodate) 3719 assert_eb_folio_uptodate(eb, i); 3720 3721 cur = min(len, unit_size - offset); 3722 kaddr = folio_address(eb->folios[i]); 3723 if (use_memmove) 3724 memmove(kaddr + offset, src, cur); 3725 else 3726 memcpy(kaddr + offset, src, cur); 3727 3728 src += cur; 3729 len -= cur; 3730 offset = 0; 3731 i++; 3732 } 3733 } 3734 3735 void write_extent_buffer(const struct extent_buffer *eb, const void *srcv, 3736 unsigned long start, unsigned long len) 3737 { 3738 return __write_extent_buffer(eb, srcv, start, len, false); 3739 } 3740 3741 static void memset_extent_buffer(const struct extent_buffer *eb, int c, 3742 unsigned long start, unsigned long len) 3743 { 3744 const int unit_size = eb->folio_size; 3745 unsigned long cur = start; 3746 3747 if (eb->addr) { 3748 memset(eb->addr + start, c, len); 3749 return; 3750 } 3751 3752 while (cur < start + len) { 3753 unsigned long index = get_eb_folio_index(eb, cur); 3754 unsigned int offset = get_eb_offset_in_folio(eb, cur); 3755 unsigned int cur_len = min(start + len - cur, unit_size - offset); 3756 3757 assert_eb_folio_uptodate(eb, index); 3758 memset(folio_address(eb->folios[index]) + offset, c, cur_len); 3759 3760 cur += cur_len; 3761 } 3762 } 3763 3764 void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start, 3765 unsigned long len) 3766 { 3767 if (check_eb_range(eb, start, len)) 3768 return; 3769 return memset_extent_buffer(eb, 0, start, len); 3770 } 3771 3772 void copy_extent_buffer_full(const struct extent_buffer *dst, 3773 const struct extent_buffer *src) 3774 { 3775 const int unit_size = src->folio_size; 3776 unsigned long cur = 0; 3777 3778 ASSERT(dst->len == src->len); 3779 3780 while (cur < src->len) { 3781 unsigned long index = get_eb_folio_index(src, cur); 3782 unsigned long offset = get_eb_offset_in_folio(src, cur); 3783 unsigned long cur_len = min(src->len, unit_size - offset); 3784 void *addr = folio_address(src->folios[index]) + offset; 3785 3786 write_extent_buffer(dst, addr, cur, cur_len); 3787 3788 cur += cur_len; 3789 } 3790 } 3791 3792 void copy_extent_buffer(const struct extent_buffer *dst, 3793 const struct extent_buffer *src, 3794 unsigned long dst_offset, unsigned long src_offset, 3795 unsigned long len) 3796 { 3797 const int unit_size = dst->folio_size; 3798 u64 dst_len = dst->len; 3799 size_t cur; 3800 size_t offset; 3801 char *kaddr; 3802 unsigned long i = get_eb_folio_index(dst, dst_offset); 3803 3804 if (check_eb_range(dst, dst_offset, len) || 3805 check_eb_range(src, src_offset, len)) 3806 return; 3807 3808 WARN_ON(src->len != dst_len); 3809 3810 offset = get_eb_offset_in_folio(dst, dst_offset); 3811 3812 while (len > 0) { 3813 assert_eb_folio_uptodate(dst, i); 3814 3815 cur = min(len, (unsigned long)(unit_size - offset)); 3816 3817 kaddr = folio_address(dst->folios[i]); 3818 read_extent_buffer(src, kaddr + offset, src_offset, cur); 3819 3820 src_offset += cur; 3821 len -= cur; 3822 offset = 0; 3823 i++; 3824 } 3825 } 3826 3827 /* 3828 * Calculate the folio and offset of the byte containing the given bit number. 3829 * 3830 * @eb: the extent buffer 3831 * @start: offset of the bitmap item in the extent buffer 3832 * @nr: bit number 3833 * @folio_index: return index of the folio in the extent buffer that contains 3834 * the given bit number 3835 * @folio_offset: return offset into the folio given by folio_index 3836 * 3837 * This helper hides the ugliness of finding the byte in an extent buffer which 3838 * contains a given bit. 3839 */ 3840 static inline void eb_bitmap_offset(const struct extent_buffer *eb, 3841 unsigned long start, unsigned long nr, 3842 unsigned long *folio_index, 3843 size_t *folio_offset) 3844 { 3845 size_t byte_offset = BIT_BYTE(nr); 3846 size_t offset; 3847 3848 /* 3849 * The byte we want is the offset of the extent buffer + the offset of 3850 * the bitmap item in the extent buffer + the offset of the byte in the 3851 * bitmap item. 3852 */ 3853 offset = start + offset_in_eb_folio(eb, eb->start) + byte_offset; 3854 3855 *folio_index = offset >> eb->folio_shift; 3856 *folio_offset = offset_in_eb_folio(eb, offset); 3857 } 3858 3859 /* 3860 * Determine whether a bit in a bitmap item is set. 3861 * 3862 * @eb: the extent buffer 3863 * @start: offset of the bitmap item in the extent buffer 3864 * @nr: bit number to test 3865 */ 3866 int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start, 3867 unsigned long nr) 3868 { 3869 unsigned long i; 3870 size_t offset; 3871 u8 *kaddr; 3872 3873 eb_bitmap_offset(eb, start, nr, &i, &offset); 3874 assert_eb_folio_uptodate(eb, i); 3875 kaddr = folio_address(eb->folios[i]); 3876 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); 3877 } 3878 3879 static u8 *extent_buffer_get_byte(const struct extent_buffer *eb, unsigned long bytenr) 3880 { 3881 unsigned long index = get_eb_folio_index(eb, bytenr); 3882 3883 if (check_eb_range(eb, bytenr, 1)) 3884 return NULL; 3885 return folio_address(eb->folios[index]) + get_eb_offset_in_folio(eb, bytenr); 3886 } 3887 3888 /* 3889 * Set an area of a bitmap to 1. 3890 * 3891 * @eb: the extent buffer 3892 * @start: offset of the bitmap item in the extent buffer 3893 * @pos: bit number of the first bit 3894 * @len: number of bits to set 3895 */ 3896 void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start, 3897 unsigned long pos, unsigned long len) 3898 { 3899 unsigned int first_byte = start + BIT_BYTE(pos); 3900 unsigned int last_byte = start + BIT_BYTE(pos + len - 1); 3901 const bool same_byte = (first_byte == last_byte); 3902 u8 mask = BITMAP_FIRST_BYTE_MASK(pos); 3903 u8 *kaddr; 3904 3905 if (same_byte) 3906 mask &= BITMAP_LAST_BYTE_MASK(pos + len); 3907 3908 /* Handle the first byte. */ 3909 kaddr = extent_buffer_get_byte(eb, first_byte); 3910 *kaddr |= mask; 3911 if (same_byte) 3912 return; 3913 3914 /* Handle the byte aligned part. */ 3915 ASSERT(first_byte + 1 <= last_byte); 3916 memset_extent_buffer(eb, 0xff, first_byte + 1, last_byte - first_byte - 1); 3917 3918 /* Handle the last byte. */ 3919 kaddr = extent_buffer_get_byte(eb, last_byte); 3920 *kaddr |= BITMAP_LAST_BYTE_MASK(pos + len); 3921 } 3922 3923 3924 /* 3925 * Clear an area of a bitmap. 3926 * 3927 * @eb: the extent buffer 3928 * @start: offset of the bitmap item in the extent buffer 3929 * @pos: bit number of the first bit 3930 * @len: number of bits to clear 3931 */ 3932 void extent_buffer_bitmap_clear(const struct extent_buffer *eb, 3933 unsigned long start, unsigned long pos, 3934 unsigned long len) 3935 { 3936 unsigned int first_byte = start + BIT_BYTE(pos); 3937 unsigned int last_byte = start + BIT_BYTE(pos + len - 1); 3938 const bool same_byte = (first_byte == last_byte); 3939 u8 mask = BITMAP_FIRST_BYTE_MASK(pos); 3940 u8 *kaddr; 3941 3942 if (same_byte) 3943 mask &= BITMAP_LAST_BYTE_MASK(pos + len); 3944 3945 /* Handle the first byte. */ 3946 kaddr = extent_buffer_get_byte(eb, first_byte); 3947 *kaddr &= ~mask; 3948 if (same_byte) 3949 return; 3950 3951 /* Handle the byte aligned part. */ 3952 ASSERT(first_byte + 1 <= last_byte); 3953 memset_extent_buffer(eb, 0, first_byte + 1, last_byte - first_byte - 1); 3954 3955 /* Handle the last byte. */ 3956 kaddr = extent_buffer_get_byte(eb, last_byte); 3957 *kaddr &= ~BITMAP_LAST_BYTE_MASK(pos + len); 3958 } 3959 3960 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) 3961 { 3962 unsigned long distance = (src > dst) ? src - dst : dst - src; 3963 return distance < len; 3964 } 3965 3966 void memcpy_extent_buffer(const struct extent_buffer *dst, 3967 unsigned long dst_offset, unsigned long src_offset, 3968 unsigned long len) 3969 { 3970 const int unit_size = dst->folio_size; 3971 unsigned long cur_off = 0; 3972 3973 if (check_eb_range(dst, dst_offset, len) || 3974 check_eb_range(dst, src_offset, len)) 3975 return; 3976 3977 if (dst->addr) { 3978 const bool use_memmove = areas_overlap(src_offset, dst_offset, len); 3979 3980 if (use_memmove) 3981 memmove(dst->addr + dst_offset, dst->addr + src_offset, len); 3982 else 3983 memcpy(dst->addr + dst_offset, dst->addr + src_offset, len); 3984 return; 3985 } 3986 3987 while (cur_off < len) { 3988 unsigned long cur_src = cur_off + src_offset; 3989 unsigned long folio_index = get_eb_folio_index(dst, cur_src); 3990 unsigned long folio_off = get_eb_offset_in_folio(dst, cur_src); 3991 unsigned long cur_len = min(src_offset + len - cur_src, 3992 unit_size - folio_off); 3993 void *src_addr = folio_address(dst->folios[folio_index]) + folio_off; 3994 const bool use_memmove = areas_overlap(src_offset + cur_off, 3995 dst_offset + cur_off, cur_len); 3996 3997 __write_extent_buffer(dst, src_addr, dst_offset + cur_off, cur_len, 3998 use_memmove); 3999 cur_off += cur_len; 4000 } 4001 } 4002 4003 void memmove_extent_buffer(const struct extent_buffer *dst, 4004 unsigned long dst_offset, unsigned long src_offset, 4005 unsigned long len) 4006 { 4007 unsigned long dst_end = dst_offset + len - 1; 4008 unsigned long src_end = src_offset + len - 1; 4009 4010 if (check_eb_range(dst, dst_offset, len) || 4011 check_eb_range(dst, src_offset, len)) 4012 return; 4013 4014 if (dst_offset < src_offset) { 4015 memcpy_extent_buffer(dst, dst_offset, src_offset, len); 4016 return; 4017 } 4018 4019 if (dst->addr) { 4020 memmove(dst->addr + dst_offset, dst->addr + src_offset, len); 4021 return; 4022 } 4023 4024 while (len > 0) { 4025 unsigned long src_i; 4026 size_t cur; 4027 size_t dst_off_in_folio; 4028 size_t src_off_in_folio; 4029 void *src_addr; 4030 bool use_memmove; 4031 4032 src_i = get_eb_folio_index(dst, src_end); 4033 4034 dst_off_in_folio = get_eb_offset_in_folio(dst, dst_end); 4035 src_off_in_folio = get_eb_offset_in_folio(dst, src_end); 4036 4037 cur = min_t(unsigned long, len, src_off_in_folio + 1); 4038 cur = min(cur, dst_off_in_folio + 1); 4039 4040 src_addr = folio_address(dst->folios[src_i]) + src_off_in_folio - 4041 cur + 1; 4042 use_memmove = areas_overlap(src_end - cur + 1, dst_end - cur + 1, 4043 cur); 4044 4045 __write_extent_buffer(dst, src_addr, dst_end - cur + 1, cur, 4046 use_memmove); 4047 4048 dst_end -= cur; 4049 src_end -= cur; 4050 len -= cur; 4051 } 4052 } 4053 4054 #define GANG_LOOKUP_SIZE 16 4055 static struct extent_buffer *get_next_extent_buffer( 4056 const struct btrfs_fs_info *fs_info, struct folio *folio, u64 bytenr) 4057 { 4058 struct extent_buffer *gang[GANG_LOOKUP_SIZE]; 4059 struct extent_buffer *found = NULL; 4060 u64 folio_start = folio_pos(folio); 4061 u64 cur = folio_start; 4062 4063 ASSERT(in_range(bytenr, folio_start, PAGE_SIZE)); 4064 lockdep_assert_held(&fs_info->buffer_lock); 4065 4066 while (cur < folio_start + PAGE_SIZE) { 4067 int ret; 4068 int i; 4069 4070 ret = radix_tree_gang_lookup(&fs_info->buffer_radix, 4071 (void **)gang, cur >> fs_info->sectorsize_bits, 4072 min_t(unsigned int, GANG_LOOKUP_SIZE, 4073 PAGE_SIZE / fs_info->nodesize)); 4074 if (ret == 0) 4075 goto out; 4076 for (i = 0; i < ret; i++) { 4077 /* Already beyond page end */ 4078 if (gang[i]->start >= folio_start + PAGE_SIZE) 4079 goto out; 4080 /* Found one */ 4081 if (gang[i]->start >= bytenr) { 4082 found = gang[i]; 4083 goto out; 4084 } 4085 } 4086 cur = gang[ret - 1]->start + gang[ret - 1]->len; 4087 } 4088 out: 4089 return found; 4090 } 4091 4092 static int try_release_subpage_extent_buffer(struct folio *folio) 4093 { 4094 struct btrfs_fs_info *fs_info = folio_to_fs_info(folio); 4095 u64 cur = folio_pos(folio); 4096 const u64 end = cur + PAGE_SIZE; 4097 int ret; 4098 4099 while (cur < end) { 4100 struct extent_buffer *eb = NULL; 4101 4102 /* 4103 * Unlike try_release_extent_buffer() which uses folio private 4104 * to grab buffer, for subpage case we rely on radix tree, thus 4105 * we need to ensure radix tree consistency. 4106 * 4107 * We also want an atomic snapshot of the radix tree, thus go 4108 * with spinlock rather than RCU. 4109 */ 4110 spin_lock(&fs_info->buffer_lock); 4111 eb = get_next_extent_buffer(fs_info, folio, cur); 4112 if (!eb) { 4113 /* No more eb in the page range after or at cur */ 4114 spin_unlock(&fs_info->buffer_lock); 4115 break; 4116 } 4117 cur = eb->start + eb->len; 4118 4119 /* 4120 * The same as try_release_extent_buffer(), to ensure the eb 4121 * won't disappear out from under us. 4122 */ 4123 spin_lock(&eb->refs_lock); 4124 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { 4125 spin_unlock(&eb->refs_lock); 4126 spin_unlock(&fs_info->buffer_lock); 4127 break; 4128 } 4129 spin_unlock(&fs_info->buffer_lock); 4130 4131 /* 4132 * If tree ref isn't set then we know the ref on this eb is a 4133 * real ref, so just return, this eb will likely be freed soon 4134 * anyway. 4135 */ 4136 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { 4137 spin_unlock(&eb->refs_lock); 4138 break; 4139 } 4140 4141 /* 4142 * Here we don't care about the return value, we will always 4143 * check the folio private at the end. And 4144 * release_extent_buffer() will release the refs_lock. 4145 */ 4146 release_extent_buffer(eb); 4147 } 4148 /* 4149 * Finally to check if we have cleared folio private, as if we have 4150 * released all ebs in the page, the folio private should be cleared now. 4151 */ 4152 spin_lock(&folio->mapping->i_private_lock); 4153 if (!folio_test_private(folio)) 4154 ret = 1; 4155 else 4156 ret = 0; 4157 spin_unlock(&folio->mapping->i_private_lock); 4158 return ret; 4159 4160 } 4161 4162 int try_release_extent_buffer(struct folio *folio) 4163 { 4164 struct extent_buffer *eb; 4165 4166 if (folio_to_fs_info(folio)->nodesize < PAGE_SIZE) 4167 return try_release_subpage_extent_buffer(folio); 4168 4169 /* 4170 * We need to make sure nobody is changing folio private, as we rely on 4171 * folio private as the pointer to extent buffer. 4172 */ 4173 spin_lock(&folio->mapping->i_private_lock); 4174 if (!folio_test_private(folio)) { 4175 spin_unlock(&folio->mapping->i_private_lock); 4176 return 1; 4177 } 4178 4179 eb = folio_get_private(folio); 4180 BUG_ON(!eb); 4181 4182 /* 4183 * This is a little awful but should be ok, we need to make sure that 4184 * the eb doesn't disappear out from under us while we're looking at 4185 * this page. 4186 */ 4187 spin_lock(&eb->refs_lock); 4188 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { 4189 spin_unlock(&eb->refs_lock); 4190 spin_unlock(&folio->mapping->i_private_lock); 4191 return 0; 4192 } 4193 spin_unlock(&folio->mapping->i_private_lock); 4194 4195 /* 4196 * If tree ref isn't set then we know the ref on this eb is a real ref, 4197 * so just return, this page will likely be freed soon anyway. 4198 */ 4199 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { 4200 spin_unlock(&eb->refs_lock); 4201 return 0; 4202 } 4203 4204 return release_extent_buffer(eb); 4205 } 4206 4207 /* 4208 * Attempt to readahead a child block. 4209 * 4210 * @fs_info: the fs_info 4211 * @bytenr: bytenr to read 4212 * @owner_root: objectid of the root that owns this eb 4213 * @gen: generation for the uptodate check, can be 0 4214 * @level: level for the eb 4215 * 4216 * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a 4217 * normal uptodate check of the eb, without checking the generation. If we have 4218 * to read the block we will not block on anything. 4219 */ 4220 void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info, 4221 u64 bytenr, u64 owner_root, u64 gen, int level) 4222 { 4223 struct btrfs_tree_parent_check check = { 4224 .has_first_key = 0, 4225 .level = level, 4226 .transid = gen 4227 }; 4228 struct extent_buffer *eb; 4229 int ret; 4230 4231 eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 4232 if (IS_ERR(eb)) 4233 return; 4234 4235 if (btrfs_buffer_uptodate(eb, gen, 1)) { 4236 free_extent_buffer(eb); 4237 return; 4238 } 4239 4240 ret = read_extent_buffer_pages(eb, WAIT_NONE, 0, &check); 4241 if (ret < 0) 4242 free_extent_buffer_stale(eb); 4243 else 4244 free_extent_buffer(eb); 4245 } 4246 4247 /* 4248 * Readahead a node's child block. 4249 * 4250 * @node: parent node we're reading from 4251 * @slot: slot in the parent node for the child we want to read 4252 * 4253 * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at 4254 * the slot in the node provided. 4255 */ 4256 void btrfs_readahead_node_child(struct extent_buffer *node, int slot) 4257 { 4258 btrfs_readahead_tree_block(node->fs_info, 4259 btrfs_node_blockptr(node, slot), 4260 btrfs_header_owner(node), 4261 btrfs_node_ptr_generation(node, slot), 4262 btrfs_header_level(node) - 1); 4263 } 4264