1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * fs/f2fs/data.c 4 * 5 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 6 * http://www.samsung.com/ 7 */ 8 #include <linux/fs.h> 9 #include <linux/f2fs_fs.h> 10 #include <linux/sched/mm.h> 11 #include <linux/mpage.h> 12 #include <linux/writeback.h> 13 #include <linux/folio_batch.h> 14 #include <linux/blkdev.h> 15 #include <linux/bio.h> 16 #include <linux/blk-crypto.h> 17 #include <linux/swap.h> 18 #include <linux/prefetch.h> 19 #include <linux/uio.h> 20 #include <linux/sched/signal.h> 21 #include <linux/fiemap.h> 22 #include <linux/iomap.h> 23 #include <linux/fserror.h> 24 25 #include "f2fs.h" 26 #include "node.h" 27 #include "segment.h" 28 #include "iostat.h" 29 #include <trace/events/f2fs.h> 30 31 #define NUM_PREALLOC_POST_READ_CTXS 128 32 33 static struct kmem_cache *bio_post_read_ctx_cache; 34 static struct kmem_cache *bio_entry_slab; 35 static struct kmem_cache *ffs_entry_slab; 36 static mempool_t *bio_post_read_ctx_pool; 37 static struct bio_set f2fs_bioset; 38 39 struct f2fs_folio_state { 40 spinlock_t state_lock; 41 unsigned int read_pages_pending; 42 }; 43 44 #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE 45 46 int __init f2fs_init_bioset(void) 47 { 48 return bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE, 49 offsetof(struct f2fs_bio, bio), BIOSET_NEED_BVECS); 50 } 51 52 void f2fs_destroy_bioset(void) 53 { 54 bioset_exit(&f2fs_bioset); 55 } 56 57 bool f2fs_is_cp_guaranteed(const struct folio *folio) 58 { 59 struct address_space *mapping = folio->mapping; 60 struct inode *inode; 61 62 inode = mapping->host; 63 64 if (S_ISDIR(inode->i_mode)) 65 return true; 66 67 if ((S_ISREG(inode->i_mode) && IS_NOQUOTA(inode)) || 68 folio_test_f2fs_gcing(folio)) 69 return true; 70 return false; 71 } 72 73 /* postprocessing steps for read bios */ 74 enum bio_post_read_step { 75 #ifdef CONFIG_F2FS_FS_COMPRESSION 76 STEP_DECOMPRESS = BIT(1), 77 #else 78 STEP_DECOMPRESS = 0, /* compile out the decompression-related code */ 79 #endif 80 #ifdef CONFIG_FS_VERITY 81 STEP_VERITY = BIT(2), 82 #else 83 STEP_VERITY = 0, /* compile out the verity-related code */ 84 #endif 85 }; 86 87 struct bio_post_read_ctx { 88 struct bio *bio; 89 struct f2fs_sb_info *sbi; 90 struct fsverity_info *vi; 91 struct work_struct work; 92 unsigned int enabled_steps; 93 /* 94 * decompression_attempted keeps track of whether 95 * f2fs_end_read_compressed_page() has been called on the pages in the 96 * bio that belong to a compressed cluster yet. 97 */ 98 bool decompression_attempted; 99 block_t fs_blkaddr; 100 }; 101 102 /* 103 * Update and unlock a bio's pages, and free the bio. 104 * 105 * This marks pages up-to-date only if there was no error in the bio (I/O error, 106 * decryption error, or verity error), as indicated by bio->bi_status. 107 * 108 * "Compressed pages" (pagecache pages backed by a compressed cluster on-disk) 109 * aren't marked up-to-date here, as decompression is done on a per-compression- 110 * cluster basis rather than a per-bio basis. Instead, we only must do two 111 * things for each compressed page here: call f2fs_end_read_compressed_page() 112 * with failed=true if an error occurred before it would have normally gotten 113 * called (i.e., I/O error or decryption error, but *not* verity error), and 114 * release the bio's reference to the decompress_io_ctx of the page's cluster. 115 */ 116 static void f2fs_finish_read_bio(struct bio *bio, bool in_task) 117 { 118 struct folio_iter fi; 119 struct bio_post_read_ctx *ctx = bio->bi_private; 120 unsigned long flags; 121 122 bio_for_each_folio_all(fi, bio) { 123 struct folio *folio = fi.folio; 124 unsigned nr_pages = fi.length >> PAGE_SHIFT; 125 bool finished = true; 126 127 if (!folio_test_large(folio) && 128 f2fs_is_compressed_page(folio)) { 129 if (ctx && !ctx->decompression_attempted) 130 f2fs_end_read_compressed_page(folio, true, 0, 131 in_task); 132 f2fs_put_folio_dic(folio, in_task); 133 continue; 134 } 135 136 if (folio_test_large(folio)) { 137 struct f2fs_folio_state *ffs = folio->private; 138 139 spin_lock_irqsave(&ffs->state_lock, flags); 140 ffs->read_pages_pending -= nr_pages; 141 finished = !ffs->read_pages_pending; 142 spin_unlock_irqrestore(&ffs->state_lock, flags); 143 } 144 145 while (nr_pages--) 146 dec_cache_count(F2FS_F_SB(folio), F2FS_RD_DATA); 147 148 if (finished) 149 folio_end_read(folio, bio->bi_status == BLK_STS_OK); 150 } 151 152 if (ctx) 153 mempool_free(ctx, bio_post_read_ctx_pool); 154 bio_put(bio); 155 } 156 157 static void f2fs_verify_bio(struct work_struct *work) 158 { 159 struct bio_post_read_ctx *ctx = 160 container_of(work, struct bio_post_read_ctx, work); 161 struct bio *bio = ctx->bio; 162 bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS); 163 struct fsverity_info *vi = ctx->vi; 164 165 /* 166 * fsverity_verify_bio() may call readahead() again, and while verity 167 * will be disabled for this, decryption and/or decompression may still 168 * be needed, resulting in another bio_post_read_ctx being allocated. 169 * So to prevent deadlocks we need to release the current ctx to the 170 * mempool first. This assumes that verity is the last post-read step. 171 */ 172 mempool_free(ctx, bio_post_read_ctx_pool); 173 bio->bi_private = NULL; 174 175 /* 176 * Verify the bio's pages with fs-verity. Exclude compressed pages, 177 * as those were handled separately by f2fs_end_read_compressed_page(). 178 */ 179 if (may_have_compressed_pages) { 180 struct folio_iter fi; 181 182 bio_for_each_folio_all(fi, bio) { 183 struct folio *folio = fi.folio; 184 185 if (!f2fs_is_compressed_page(folio) && 186 !fsverity_verify_folio(vi, folio)) { 187 bio->bi_status = BLK_STS_IOERR; 188 break; 189 } 190 } 191 } else { 192 fsverity_verify_bio(vi, bio); 193 } 194 195 f2fs_finish_read_bio(bio, true); 196 } 197 198 /* 199 * If the bio's data needs to be verified with fs-verity, then enqueue the 200 * verity work for the bio. Otherwise finish the bio now. 201 * 202 * Note that to avoid deadlocks, the verity work can't be done on the 203 * decryption/decompression workqueue. This is because verifying the data pages 204 * can involve reading verity metadata pages from the file, and these verity 205 * metadata pages may be encrypted and/or compressed. 206 */ 207 static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task) 208 { 209 struct bio_post_read_ctx *ctx = bio->bi_private; 210 211 if (ctx && (ctx->enabled_steps & STEP_VERITY)) { 212 INIT_WORK(&ctx->work, f2fs_verify_bio); 213 fsverity_enqueue_verify_work(&ctx->work); 214 } else { 215 f2fs_finish_read_bio(bio, in_task); 216 } 217 } 218 219 /* 220 * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last 221 * remaining page was read by @ctx->bio. 222 * 223 * Note that a bio may span clusters (even a mix of compressed and uncompressed 224 * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates 225 * that the bio includes at least one compressed page. The actual decompression 226 * is done on a per-cluster basis, not a per-bio basis. 227 */ 228 static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx, 229 bool in_task) 230 { 231 struct folio_iter fi; 232 bool all_compressed = true; 233 block_t blkaddr = ctx->fs_blkaddr; 234 235 bio_for_each_folio_all(fi, ctx->bio) { 236 struct folio *folio = fi.folio; 237 238 if (f2fs_is_compressed_page(folio)) 239 f2fs_end_read_compressed_page(folio, false, blkaddr, 240 in_task); 241 else 242 all_compressed = false; 243 244 blkaddr++; 245 } 246 247 ctx->decompression_attempted = true; 248 249 /* 250 * Optimization: if all the bio's pages are compressed, then scheduling 251 * the per-bio verity work is unnecessary, as verity will be fully 252 * handled at the compression cluster level. 253 */ 254 if (all_compressed) 255 ctx->enabled_steps &= ~STEP_VERITY; 256 } 257 258 static void f2fs_post_read_work(struct work_struct *work) 259 { 260 struct bio_post_read_ctx *ctx = 261 container_of(work, struct bio_post_read_ctx, work); 262 struct bio *bio = ctx->bio; 263 264 if (ctx->enabled_steps & STEP_DECOMPRESS) 265 f2fs_handle_step_decompress(ctx, true); 266 267 f2fs_verify_and_finish_bio(bio, true); 268 } 269 270 static void f2fs_read_end_io(struct bio *bio) 271 { 272 struct f2fs_sb_info *sbi = F2FS_F_SB(bio_first_folio_all(bio)); 273 struct bio_post_read_ctx *ctx; 274 bool intask = !bio_in_atomic(); 275 276 iostat_update_and_unbind_ctx(bio); 277 ctx = bio->bi_private; 278 279 if (time_to_inject(sbi, FAULT_READ_IO)) 280 bio->bi_status = BLK_STS_IOERR; 281 282 if (bio->bi_status != BLK_STS_OK) { 283 f2fs_finish_read_bio(bio, intask); 284 return; 285 } 286 287 if (ctx && (ctx->enabled_steps & STEP_DECOMPRESS)) { 288 if (!f2fs_low_mem_mode(sbi)) { 289 /* Decompress inline. */ 290 f2fs_handle_step_decompress(ctx, intask); 291 } else { 292 INIT_WORK(&ctx->work, f2fs_post_read_work); 293 queue_work(ctx->sbi->wq, &ctx->work); 294 return; 295 } 296 } 297 298 f2fs_verify_and_finish_bio(bio, intask); 299 } 300 301 static void f2fs_cache_write_end_bio(struct bio *bio) 302 { 303 struct f2fs_cached_block *entry = F2FS_BIO(bio)->entry; 304 struct f2fs_sb_info *sbi = entry->cache->sbi; 305 struct f2fs_cached_block *next; 306 307 if (bio->bi_status != BLK_STS_OK) 308 f2fs_stop_checkpoint(sbi, true, 309 STOP_CP_REASON_WRITE_FAIL); 310 311 while (entry) { 312 next = entry->next_entry; 313 entry->next_entry = NULL; 314 315 if (f2fs_is_node_cache(entry)) { 316 f2fs_sanity_check_node_footer(sbi, entry, 317 entry->index, NODE_TYPE_REGULAR, true); 318 f2fs_bug_on(sbi, entry->index != nid_of_node(sbi, entry)); 319 } 320 if (f2fs_in_warm_node_list(sbi, entry)) 321 f2fs_del_fsync_node_entry(sbi, entry); 322 323 dec_cache_count(sbi, F2FS_WB_CP_DATA); 324 325 if (!get_nr_caches(sbi, F2FS_WB_CP_DATA) && 326 wq_has_sleeper(&sbi->cp_wait)) 327 wake_up(&sbi->cp_wait); 328 329 f2fs_end_cache_writeback(entry); 330 entry = next; 331 } 332 bio_put(bio); 333 } 334 335 static void f2fs_write_end_bio(struct bio *bio) 336 { 337 struct f2fs_sb_info *sbi = bio->bi_private; 338 struct folio_iter fi; 339 340 if (time_to_inject(sbi, FAULT_WRITE_IO)) 341 bio->bi_status = BLK_STS_IOERR; 342 343 if (f2fs_is_cache_bio(bio)) { 344 f2fs_cache_write_end_bio(bio); 345 return; 346 } 347 348 bio_for_each_folio_all(fi, bio) { 349 struct folio *folio = fi.folio; 350 enum count_type type; 351 352 #ifdef CONFIG_F2FS_FS_COMPRESSION 353 if (f2fs_is_compressed_page(folio)) { 354 f2fs_compress_write_end_io(bio, folio); 355 continue; 356 } 357 #endif 358 359 type = WB_DATA_TYPE(folio, false); 360 361 if (unlikely(bio->bi_status != BLK_STS_OK)) { 362 mapping_set_error(folio->mapping, -EIO); 363 if (type == F2FS_WB_CP_DATA) { 364 f2fs_stop_checkpoint(sbi, true, 365 STOP_CP_REASON_WRITE_FAIL); 366 } 367 } 368 369 dec_cache_count(sbi, type); 370 371 /* 372 * we should access sbi before folio_end_writeback() to 373 * avoid racing w/ kill_f2fs_super() 374 */ 375 if (type == F2FS_WB_CP_DATA && !get_nr_caches(sbi, type) && 376 wq_has_sleeper(&sbi->cp_wait)) 377 wake_up(&sbi->cp_wait); 378 379 folio_clear_f2fs_gcing(folio); 380 folio_end_writeback(folio); 381 } 382 383 bio_put(bio); 384 } 385 386 static void f2fs_write_end_io_work(struct work_struct *work) 387 { 388 struct bio *bio = &container_of(work, struct f2fs_bio, work)->bio; 389 390 f2fs_write_end_bio(bio); 391 } 392 393 static void f2fs_write_end_io(struct bio *bio) 394 { 395 struct f2fs_sb_info *sbi; 396 397 iostat_update_and_unbind_ctx(bio); 398 399 sbi = bio->bi_private; 400 401 if (bio_in_atomic() && 402 (bio->bi_iter.bi_size > sbi->max_atc_write_bio_size || 403 F2FS_BIO(bio)->entry_cnt > sbi->max_atc_write_bio_entry_cnt)) { 404 struct work_struct *w; 405 406 w = &container_of(bio, struct f2fs_bio, bio)->work; 407 INIT_WORK(w, f2fs_write_end_io_work); 408 queue_work(sbi->wq, w); 409 } else { 410 f2fs_write_end_bio(bio); 411 } 412 } 413 414 static void f2fs_cache_read_end_io(struct bio *bio) 415 { 416 struct f2fs_cached_block *entry = F2FS_BIO(bio)->entry; 417 struct f2fs_sb_info *sbi = entry->cache->sbi; 418 enum count_type io_type = IS_META_CACHE(entry->cache) ? 419 F2FS_RD_META : F2FS_RD_NODE; 420 struct f2fs_cached_block *next; 421 422 iostat_update_and_unbind_ctx(bio); 423 424 if (time_to_inject(sbi, FAULT_READ_IO)) 425 bio->bi_status = BLK_STS_IOERR; 426 427 while (entry) { 428 next = entry->next_entry; 429 entry->next_entry = NULL; 430 431 if (bio->bi_status == BLK_STS_OK && 432 f2fs_is_node_cache(entry) && 433 f2fs_sanity_check_node_footer(sbi, entry, 434 entry->index, NODE_TYPE_REGULAR, true)) 435 bio->bi_status = BLK_STS_IOERR; 436 437 if (bio->bi_status == BLK_STS_OK) 438 f2fs_cache_set_uptodate(entry); 439 440 dec_cache_count(sbi, io_type); 441 442 f2fs_unlock_cache(entry); 443 entry = next; 444 } 445 bio_put(bio); 446 } 447 448 #ifdef CONFIG_BLK_DEV_ZONED 449 static void f2fs_zone_write_end_io(struct bio *bio) 450 { 451 struct f2fs_bio_info *io = (struct f2fs_bio_info *)bio->bi_private; 452 453 bio->bi_private = io->bi_private; 454 complete(&io->zone_wait); 455 f2fs_write_end_io(bio); 456 } 457 #endif 458 459 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi, 460 block_t blk_addr, sector_t *sector) 461 { 462 struct block_device *bdev = sbi->sb->s_bdev; 463 int i; 464 465 if (f2fs_is_multi_device(sbi)) { 466 for (i = 0; i < sbi->s_ndevs; i++) { 467 if (FDEV(i).start_blk <= blk_addr && 468 FDEV(i).end_blk >= blk_addr) { 469 blk_addr -= FDEV(i).start_blk; 470 bdev = FDEV(i).bdev; 471 break; 472 } 473 } 474 } 475 476 if (sector) 477 *sector = SECTOR_FROM_BLOCK(sbi, blk_addr); 478 return bdev; 479 } 480 481 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr) 482 { 483 int i; 484 485 if (!f2fs_is_multi_device(sbi)) 486 return 0; 487 488 for (i = 0; i < sbi->s_ndevs; i++) 489 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr) 490 return i; 491 return 0; 492 } 493 494 static blk_opf_t f2fs_io_flags(struct f2fs_io_info *fio) 495 { 496 unsigned int temp_mask = GENMASK(NR_TEMP_TYPE - 1, 0); 497 unsigned int fua_flag, meta_flag, io_flag; 498 blk_opf_t op_flags = 0; 499 500 if (fio->op != REQ_OP_WRITE) 501 return 0; 502 if (fio->type == DATA) 503 io_flag = fio->sbi->data_io_flag; 504 else if (fio->type == NODE) 505 io_flag = fio->sbi->node_io_flag; 506 else 507 return 0; 508 509 fua_flag = io_flag & temp_mask; 510 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask; 511 512 /* 513 * data/node io flag bits per temp: 514 * REQ_META | REQ_FUA | 515 * 5 | 4 | 3 | 2 | 1 | 0 | 516 * Cold | Warm | Hot | Cold | Warm | Hot | 517 */ 518 if (BIT(fio->temp) & meta_flag) 519 op_flags |= REQ_META; 520 if (BIT(fio->temp) & fua_flag) 521 op_flags |= REQ_FUA; 522 523 if (fio->type == DATA && 524 F2FS_I(fio->folio->mapping->host)->ioprio_hint == F2FS_IOPRIO_WRITE) 525 op_flags |= REQ_PRIO; 526 527 return op_flags; 528 } 529 530 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages) 531 { 532 struct f2fs_sb_info *sbi = fio->sbi; 533 struct block_device *bdev; 534 sector_t sector; 535 struct bio *bio; 536 537 bdev = f2fs_target_device(sbi, fio->new_blkaddr, §or); 538 /* avoid reversed IO if bio is split due to exceeding max_segments */ 539 if (npages > 1 && !is_read_io(fio->op) && 540 f2fs_is_sequential_zone_area(sbi, fio->new_blkaddr)) 541 npages = min_t(int, npages, bdev_max_segments(bdev)); 542 bio = bio_alloc_bioset(bdev, npages, 543 fio->op | fio->op_flags | f2fs_io_flags(fio), 544 GFP_NOIO, &f2fs_bioset); 545 bio->bi_iter.bi_sector = sector; 546 F2FS_BIO(bio)->entry = NULL; 547 bio->bi_private = NULL; 548 if (is_read_io(fio->op)) { 549 if (fio->is_cache) 550 bio->bi_end_io = f2fs_cache_read_end_io; 551 else 552 bio->bi_end_io = f2fs_read_end_io; 553 } else { 554 bio->bi_end_io = f2fs_write_end_io; 555 bio->bi_private = sbi; 556 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, 557 fio->type, fio->temp); 558 bio->bi_write_stream = f2fs_io_type_to_write_stream(bdev, fio->type, 559 fio->temp); 560 F2FS_BIO(bio)->entry_cnt = 0; 561 } 562 iostat_alloc_and_bind_ctx(sbi, bio, NULL); 563 564 if (fio->io_wbc) 565 wbc_init_bio(fio->io_wbc, bio); 566 567 return bio; 568 } 569 570 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode, 571 pgoff_t first_idx, 572 const struct f2fs_io_info *fio, 573 gfp_t gfp_mask) 574 { 575 /* 576 * The f2fs garbage collector sets ->encrypted_page when it wants to 577 * read/write raw data without encryption. 578 */ 579 if (!fio || (!fio->encrypted_page && !fio->is_cache)) 580 fscrypt_set_bio_crypt_ctx(bio, inode, 581 (loff_t)first_idx << inode->i_blkbits, 582 gfp_mask); 583 } 584 585 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode, 586 pgoff_t next_idx, 587 const struct f2fs_io_info *fio) 588 { 589 /* 590 * The f2fs garbage collector sets ->encrypted_page when it wants to 591 * read/write raw data without encryption. 592 */ 593 if (fio && fio->encrypted_page) 594 return !bio_has_crypt_ctx(bio); 595 596 return fscrypt_mergeable_bio(bio, inode, 597 (loff_t)next_idx << inode->i_blkbits); 598 } 599 600 void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio, 601 enum page_type type) 602 { 603 if (!bio) 604 return; 605 606 WARN_ON_ONCE(!is_read_io(bio_op(bio))); 607 trace_f2fs_submit_read_bio(sbi->sb, type, bio); 608 609 iostat_update_submit_ctx(bio, type); 610 blk_crypto_submit_bio(bio); 611 } 612 613 static void f2fs_submit_write_bio(struct f2fs_sb_info *sbi, struct bio *bio, 614 enum page_type type) 615 { 616 WARN_ON_ONCE(is_read_io(bio_op(bio))); 617 trace_f2fs_submit_write_bio(sbi->sb, type, bio); 618 iostat_update_submit_ctx(bio, type); 619 blk_crypto_submit_bio(bio); 620 } 621 622 static void __submit_merged_bio(struct f2fs_bio_info *io) 623 { 624 struct f2fs_io_info *fio = &io->fio; 625 626 if (!io->bio) 627 return; 628 629 if (is_read_io(fio->op)) { 630 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio); 631 f2fs_submit_read_bio(io->sbi, io->bio, fio->type); 632 } else { 633 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio); 634 f2fs_submit_write_bio(io->sbi, io->bio, fio->type); 635 } 636 io->bio = NULL; 637 } 638 639 static bool __has_merged_page(struct bio *bio, struct inode *inode, 640 struct folio *folio) 641 { 642 struct folio_iter fi; 643 644 if (!bio) 645 return false; 646 647 if (!inode && !folio) 648 return true; 649 650 if (f2fs_is_cache_bio(bio)) 651 return false; 652 653 bio_for_each_folio_all(fi, bio) { 654 struct folio *target = fi.folio; 655 656 if (f2fs_is_compressed_page(target)) { 657 target = f2fs_compress_control_folio(target); 658 if (IS_ERR(target)) 659 continue; 660 } 661 662 if (inode && inode == target->mapping->host) 663 return true; 664 if (folio && folio == target) 665 return true; 666 } 667 668 return false; 669 } 670 671 int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi) 672 { 673 int i; 674 675 for (i = 0; i < NR_PAGE_TYPE; i++) { 676 int n = (i == META) ? 1 : NR_TEMP_TYPE; 677 int j; 678 679 sbi->write_io[i] = f2fs_kmalloc(sbi, 680 array_size(n, sizeof(struct f2fs_bio_info)), 681 GFP_KERNEL); 682 if (!sbi->write_io[i]) 683 return -ENOMEM; 684 685 for (j = HOT; j < n; j++) { 686 struct f2fs_bio_info *io = &sbi->write_io[i][j]; 687 688 init_f2fs_rwsem_trace(&io->io_rwsem, sbi, 689 LOCK_NAME_IO_RWSEM); 690 io->sbi = sbi; 691 io->bio = NULL; 692 io->last_block_in_bio = 0; 693 spin_lock_init(&io->io_lock); 694 INIT_LIST_HEAD(&io->io_list); 695 INIT_LIST_HEAD(&io->bio_list); 696 init_f2fs_rwsem(&io->bio_list_lock); 697 #ifdef CONFIG_BLK_DEV_ZONED 698 init_completion(&io->zone_wait); 699 io->zone_pending_bio = NULL; 700 io->bi_private = NULL; 701 #endif 702 } 703 } 704 705 return 0; 706 } 707 708 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi, 709 enum page_type type, enum temp_type temp) 710 { 711 enum page_type btype = PAGE_TYPE_OF_BIO(type); 712 struct f2fs_bio_info *io = sbi->write_io[btype] + temp; 713 struct f2fs_lock_context lc; 714 715 f2fs_down_write_trace(&io->io_rwsem, &lc); 716 717 if (!io->bio) 718 goto unlock_out; 719 720 /* change META to META_FLUSH in the checkpoint procedure */ 721 if (type >= META_FLUSH) { 722 io->fio.type = META_FLUSH; 723 io->bio->bi_opf |= REQ_META | REQ_PRIO | REQ_SYNC; 724 if (!test_opt(sbi, NOBARRIER)) 725 io->bio->bi_opf |= REQ_PREFLUSH | REQ_FUA; 726 } 727 __submit_merged_bio(io); 728 unlock_out: 729 f2fs_up_write_trace(&io->io_rwsem, &lc); 730 } 731 732 static void __submit_merged_data_write_cond(struct f2fs_sb_info *sbi, 733 struct inode *inode, struct folio *folio, 734 bool writeback) 735 { 736 enum temp_type temp; 737 bool ret = true; 738 bool force = !inode && !folio; 739 740 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) { 741 if (!force) { 742 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; 743 struct f2fs_lock_context lc; 744 745 f2fs_down_read_trace(&io->io_rwsem, &lc); 746 ret = __has_merged_page(io->bio, inode, folio); 747 f2fs_up_read_trace(&io->io_rwsem, &lc); 748 } 749 if (ret) { 750 __f2fs_submit_merged_write(sbi, DATA, temp); 751 /* 752 * For waitting writebck case, if the bio owned by the 753 * folio is already submitted, we do not need to submit 754 * other types of bios. 755 */ 756 if (writeback) 757 break; 758 } 759 } 760 } 761 762 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi, 763 struct inode *inode, struct folio *folio) 764 { 765 __submit_merged_data_write_cond(sbi, inode, folio, false); 766 } 767 768 void f2fs_submit_merged_write_folio(struct f2fs_sb_info *sbi, 769 struct folio *folio) 770 { 771 __submit_merged_data_write_cond(sbi, NULL, folio, true); 772 } 773 774 static bool __has_merged_cache(struct f2fs_sb_info *sbi, struct bio *bio, 775 struct f2fs_cached_block *target, nid_t ino) 776 { 777 struct f2fs_cached_block *entry; 778 779 if (!bio) 780 return false; 781 782 entry = F2FS_BIO(bio)->entry; 783 784 while (entry) { 785 if (target && entry == target) 786 return true; 787 788 if (ino) { 789 if (likely(f2fs_is_node_cache(entry))) { 790 if (ino_of_node(sbi, entry) == ino) 791 return true; 792 } else { 793 WARN_ON_ONCE(1); 794 } 795 } 796 entry = entry->next_entry; 797 } 798 return false; 799 } 800 801 bool f2fs_submit_merged_write_cache(struct f2fs_sb_info *sbi, 802 struct f2fs_cached_block *entry, 803 nid_t ino, enum page_type type) 804 { 805 enum temp_type temp; 806 bool ret = false; 807 bool force = !entry && !ino; 808 809 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) { 810 enum page_type btype = PAGE_TYPE_OF_BIO(type); 811 struct f2fs_bio_info *io = sbi->write_io[btype] + temp; 812 struct f2fs_lock_context lc; 813 bool merged = true; 814 815 if (!force) { 816 f2fs_down_read_trace(&io->io_rwsem, &lc); 817 merged = __has_merged_cache(sbi, io->bio, entry, ino); 818 f2fs_up_read_trace(&io->io_rwsem, &lc); 819 } 820 821 if (merged) { 822 __f2fs_submit_merged_write(sbi, type, temp); 823 ret = true; 824 } 825 826 /* TODO: use HOT temp only for meta pages now. */ 827 if (type >= META) 828 break; 829 } 830 return ret; 831 } 832 833 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type) 834 { 835 if (type == DATA) 836 __submit_merged_data_write_cond(sbi, NULL, NULL, false); 837 else 838 f2fs_submit_merged_write_cache(sbi, NULL, 0, type); 839 } 840 841 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi) 842 { 843 f2fs_submit_merged_write(sbi, DATA); 844 f2fs_submit_merged_write(sbi, NODE); 845 f2fs_submit_merged_write(sbi, META); 846 } 847 848 /* 849 * Fill the locked page with data located in the block address. 850 * A caller needs to unlock the page on failure. 851 */ 852 int f2fs_submit_page_bio(struct f2fs_io_info *fio) 853 { 854 struct bio *bio; 855 struct folio *fio_folio = fio->folio; 856 struct folio *data_folio = fio->encrypted_page ? 857 page_folio(fio->encrypted_page) : fio_folio; 858 859 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, 860 fio->is_por ? META_POR : (__is_meta_io(fio) ? 861 META_GENERIC : DATA_GENERIC_ENHANCE))) 862 return -EFSCORRUPTED; 863 864 trace_f2fs_submit_folio_bio(data_folio, fio); 865 866 /* Allocate a new bio */ 867 bio = __bio_alloc(fio, 1); 868 869 f2fs_set_bio_crypt_ctx(bio, fio_folio->mapping->host, 870 fio_folio->index, fio, GFP_NOIO); 871 bio_add_folio_nofail(bio, data_folio, folio_size(data_folio), 0); 872 873 if (fio->io_wbc && !is_read_io(fio->op)) 874 wbc_account_cgroup_owner(fio->io_wbc, fio_folio, PAGE_SIZE); 875 876 inc_cache_count(fio->sbi, is_read_io(fio->op) ? 877 F2FS_RD_DATA : WB_DATA_TYPE(fio->folio, false)); 878 879 if (is_read_io(bio_op(bio))) 880 f2fs_submit_read_bio(fio->sbi, bio, fio->type); 881 else 882 f2fs_submit_write_bio(fio->sbi, bio, fio->type); 883 return 0; 884 } 885 886 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, 887 block_t last_blkaddr, block_t cur_blkaddr) 888 { 889 if (unlikely(sbi->max_io_bytes && 890 bio->bi_iter.bi_size >= sbi->max_io_bytes)) 891 return false; 892 if (last_blkaddr + 1 != cur_blkaddr) 893 return false; 894 if (bio->bi_bdev != f2fs_target_device(sbi, cur_blkaddr, NULL)) 895 return false; 896 /* avoid reversed IO if bio is split due to exceeding max_segments/sectors */ 897 if (f2fs_is_sequential_zone_area(sbi, cur_blkaddr)) { 898 struct request_queue *q = bdev_get_queue(bio->bi_bdev); 899 900 if (bio->bi_vcnt >= queue_max_segments(q)) 901 return false; 902 if (bio_sectors(bio) + (F2FS_BLKSIZE(sbi) >> SECTOR_SHIFT) > 903 queue_max_sectors(q)) 904 return false; 905 } 906 return true; 907 } 908 909 static bool io_type_is_mergeable(struct f2fs_bio_info *io, 910 struct f2fs_io_info *fio) 911 { 912 blk_opf_t mask = ~(REQ_PREFLUSH | REQ_FUA); 913 914 if (io->fio.op != fio->op) 915 return false; 916 if (io->fio.is_cache != fio->is_cache) 917 return false; 918 return (io->fio.op_flags & mask) == (fio->op_flags & mask); 919 } 920 921 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, 922 struct f2fs_bio_info *io, 923 struct f2fs_io_info *fio, 924 block_t last_blkaddr, 925 block_t cur_blkaddr) 926 { 927 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr)) 928 return false; 929 return io_type_is_mergeable(io, fio); 930 } 931 932 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio, 933 struct folio *folio, enum temp_type temp) 934 { 935 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; 936 struct bio_entry *be; 937 938 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL); 939 be->bio = bio; 940 bio_get(bio); 941 942 bio_add_folio_nofail(bio, folio, folio_size(folio), 0); 943 944 f2fs_down_write(&io->bio_list_lock); 945 list_add_tail(&be->list, &io->bio_list); 946 f2fs_up_write(&io->bio_list_lock); 947 } 948 949 static void del_bio_entry(struct bio_entry *be) 950 { 951 list_del(&be->list); 952 kmem_cache_free(bio_entry_slab, be); 953 } 954 955 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio, 956 struct folio *folio) 957 { 958 struct folio *fio_folio = fio->folio; 959 struct f2fs_sb_info *sbi = fio->sbi; 960 enum temp_type temp; 961 bool found = false; 962 int ret = -EAGAIN; 963 964 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { 965 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; 966 struct list_head *head = &io->bio_list; 967 struct bio_entry *be; 968 969 f2fs_down_write(&io->bio_list_lock); 970 list_for_each_entry(be, head, list) { 971 if (be->bio != *bio) 972 continue; 973 974 found = true; 975 976 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio, 977 *fio->last_block, 978 fio->new_blkaddr)); 979 if (f2fs_crypt_mergeable_bio(*bio, 980 fio_folio->mapping->host, 981 fio_folio->index, fio) && 982 bio_add_folio(*bio, folio, folio_size(folio), 0)) { 983 ret = 0; 984 break; 985 } 986 987 /* page can't be merged into bio; submit the bio */ 988 del_bio_entry(be); 989 f2fs_submit_write_bio(sbi, *bio, DATA); 990 break; 991 } 992 f2fs_up_write(&io->bio_list_lock); 993 } 994 995 if (ret) { 996 bio_put(*bio); 997 *bio = NULL; 998 } 999 1000 return ret; 1001 } 1002 1003 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi, 1004 struct bio **bio, struct folio *folio) 1005 { 1006 enum temp_type temp; 1007 bool found = false; 1008 struct bio *target = bio ? *bio : NULL; 1009 1010 f2fs_bug_on(sbi, !target && !folio); 1011 1012 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { 1013 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; 1014 struct list_head *head = &io->bio_list; 1015 struct bio_entry *be; 1016 1017 if (list_empty(head)) 1018 continue; 1019 1020 f2fs_down_read(&io->bio_list_lock); 1021 list_for_each_entry(be, head, list) { 1022 if (target) 1023 found = (target == be->bio); 1024 else 1025 found = __has_merged_page(be->bio, 1026 NULL, folio); 1027 if (found) 1028 break; 1029 } 1030 f2fs_up_read(&io->bio_list_lock); 1031 1032 if (!found) 1033 continue; 1034 1035 found = false; 1036 1037 f2fs_down_write(&io->bio_list_lock); 1038 list_for_each_entry(be, head, list) { 1039 if (target) 1040 found = (target == be->bio); 1041 else 1042 found = __has_merged_page(be->bio, 1043 NULL, folio); 1044 if (found) { 1045 target = be->bio; 1046 del_bio_entry(be); 1047 break; 1048 } 1049 } 1050 f2fs_up_write(&io->bio_list_lock); 1051 } 1052 1053 if (found) 1054 f2fs_submit_write_bio(sbi, target, DATA); 1055 if (bio && *bio) { 1056 bio_put(*bio); 1057 *bio = NULL; 1058 } 1059 } 1060 1061 void f2fs_submit_all_merged_ipu_writes(struct f2fs_sb_info *sbi) 1062 { 1063 struct bio_entry *be, *tmp; 1064 struct f2fs_bio_info *io; 1065 enum temp_type temp; 1066 1067 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) { 1068 LIST_HEAD(list); 1069 1070 io = sbi->write_io[DATA] + temp; 1071 1072 /* A lockless list_empty() check is safe here: any bios from 1073 * other kworkers that we miss will be submitted by those 1074 * kworkers accordingly. 1075 */ 1076 if (list_empty(&io->bio_list)) 1077 continue; 1078 1079 f2fs_down_write(&io->bio_list_lock); 1080 list_splice_init(&io->bio_list, &list); 1081 f2fs_up_write(&io->bio_list_lock); 1082 1083 list_for_each_entry_safe(be, tmp, &list, list) { 1084 f2fs_submit_write_bio(sbi, be->bio, DATA); 1085 del_bio_entry(be); 1086 } 1087 } 1088 } 1089 1090 int f2fs_merge_page_bio(struct f2fs_io_info *fio) 1091 { 1092 struct bio *bio = *fio->bio; 1093 struct folio *data_folio = fio->encrypted_page ? 1094 page_folio(fio->encrypted_page) : fio->folio; 1095 struct folio *folio = fio->folio; 1096 1097 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, 1098 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC)) 1099 return -EFSCORRUPTED; 1100 1101 trace_f2fs_submit_folio_bio(data_folio, fio); 1102 1103 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block, 1104 fio->new_blkaddr)) 1105 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL); 1106 alloc_new: 1107 if (!bio) { 1108 bio = __bio_alloc(fio, BIO_MAX_VECS); 1109 f2fs_set_bio_crypt_ctx(bio, folio->mapping->host, 1110 folio->index, fio, GFP_NOIO); 1111 1112 add_bio_entry(fio->sbi, bio, data_folio, fio->temp); 1113 } else { 1114 if (add_ipu_page(fio, &bio, data_folio)) 1115 goto alloc_new; 1116 } 1117 1118 if (fio->io_wbc) 1119 wbc_account_cgroup_owner(fio->io_wbc, folio, folio_size(folio)); 1120 1121 inc_cache_count(fio->sbi, WB_DATA_TYPE(folio, false)); 1122 1123 *fio->last_block = fio->new_blkaddr; 1124 *fio->bio = bio; 1125 1126 return 0; 1127 } 1128 1129 #ifdef CONFIG_BLK_DEV_ZONED 1130 static bool is_end_zone_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr) 1131 { 1132 struct block_device *bdev = sbi->sb->s_bdev; 1133 int devi = 0; 1134 1135 if (f2fs_is_multi_device(sbi)) { 1136 devi = f2fs_target_device_index(sbi, blkaddr); 1137 if (blkaddr < FDEV(devi).start_blk || 1138 blkaddr > FDEV(devi).end_blk) { 1139 f2fs_err(sbi, "Invalid block %x", blkaddr); 1140 return false; 1141 } 1142 blkaddr -= FDEV(devi).start_blk; 1143 bdev = FDEV(devi).bdev; 1144 } 1145 return bdev_is_zoned(bdev) && 1146 f2fs_blkz_is_seq(sbi, devi, blkaddr) && 1147 (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1); 1148 } 1149 1150 static void f2fs_wait_zone_io_completion(struct f2fs_sb_info *sbi, 1151 struct f2fs_bio_info *io, enum page_type btype) 1152 { 1153 if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) { 1154 wait_for_completion_io(&io->zone_wait); 1155 bio_put(io->zone_pending_bio); 1156 io->zone_pending_bio = NULL; 1157 io->bi_private = NULL; 1158 } 1159 } 1160 1161 static void f2fs_submit_zone_io(struct f2fs_sb_info *sbi, 1162 struct f2fs_io_info *fio, struct f2fs_bio_info *io, 1163 enum page_type btype) 1164 { 1165 if (f2fs_sb_has_blkzoned(sbi) && btype < META && 1166 is_end_zone_blkaddr(sbi, fio->new_blkaddr)) { 1167 bio_get(io->bio); 1168 reinit_completion(&io->zone_wait); 1169 io->bi_private = io->bio->bi_private; 1170 io->bio->bi_private = io; 1171 io->bio->bi_end_io = f2fs_zone_write_end_io; 1172 io->zone_pending_bio = io->bio; 1173 __submit_merged_bio(io); 1174 } 1175 } 1176 #endif 1177 1178 void f2fs_submit_page_write(struct f2fs_io_info *fio) 1179 { 1180 struct f2fs_sb_info *sbi = fio->sbi; 1181 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); 1182 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp; 1183 struct folio *bio_folio; 1184 struct f2fs_lock_context lc; 1185 enum count_type type; 1186 1187 f2fs_bug_on(sbi, is_read_io(fio->op)); 1188 1189 f2fs_down_write_trace(&io->io_rwsem, &lc); 1190 next: 1191 #ifdef CONFIG_BLK_DEV_ZONED 1192 f2fs_wait_zone_io_completion(sbi, io, btype); 1193 #endif 1194 if (fio->in_list) { 1195 spin_lock(&io->io_lock); 1196 if (list_empty(&io->io_list)) { 1197 spin_unlock(&io->io_lock); 1198 goto out; 1199 } 1200 fio = list_first_entry(&io->io_list, 1201 struct f2fs_io_info, list); 1202 list_del(&fio->list); 1203 spin_unlock(&io->io_lock); 1204 } 1205 1206 verify_fio_blkaddr(fio); 1207 1208 if (fio->encrypted_page) 1209 bio_folio = page_folio(fio->encrypted_page); 1210 else if (fio->compressed_page) 1211 bio_folio = page_folio(fio->compressed_page); 1212 else 1213 bio_folio = fio->folio; 1214 1215 /* set submitted = true as a return value */ 1216 fio->submitted = 1; 1217 1218 type = WB_DATA_TYPE(bio_folio, fio->compressed_page); 1219 inc_cache_count(sbi, type); 1220 1221 if (io->bio && 1222 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio, 1223 fio->new_blkaddr) || 1224 !f2fs_crypt_mergeable_bio(io->bio, fio_inode(fio), 1225 bio_folio->index, fio))) 1226 __submit_merged_bio(io); 1227 alloc_new: 1228 if (io->bio == NULL) { 1229 io->bio = __bio_alloc(fio, BIO_MAX_VECS); 1230 f2fs_set_bio_crypt_ctx(io->bio, fio_inode(fio), 1231 bio_folio->index, fio, GFP_NOIO); 1232 io->fio = *fio; 1233 } 1234 1235 if (!bio_add_folio(io->bio, bio_folio, folio_size(bio_folio), 0)) { 1236 __submit_merged_bio(io); 1237 goto alloc_new; 1238 } 1239 1240 F2FS_BIO(io->bio)->entry_cnt++; 1241 1242 if (fio->io_wbc) 1243 wbc_account_cgroup_owner(fio->io_wbc, fio->folio, 1244 folio_size(fio->folio)); 1245 1246 io->last_block_in_bio = fio->new_blkaddr; 1247 1248 trace_f2fs_submit_folio_write(fio->folio, fio); 1249 #ifdef CONFIG_BLK_DEV_ZONED 1250 f2fs_submit_zone_io(sbi, fio, io, btype); 1251 #endif 1252 1253 if (fio->in_list) 1254 goto next; 1255 out: 1256 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || 1257 !f2fs_is_checkpoint_ready(sbi)) 1258 __submit_merged_bio(io); 1259 f2fs_up_write_trace(&io->io_rwsem, &lc); 1260 } 1261 1262 static void f2fs_bio_add_cache(struct f2fs_io_info *fio, struct bio *bio) 1263 { 1264 struct f2fs_bio *fbio = F2FS_BIO(bio); 1265 struct f2fs_cached_block *head = fbio->entry; 1266 struct f2fs_cached_block *new = fio->cache_entry; 1267 1268 new->next_entry = head; 1269 fbio->entry = new; 1270 } 1271 1272 int f2fs_submit_cache_read(struct f2fs_io_info *fio) 1273 { 1274 struct f2fs_sb_info *sbi = fio->sbi; 1275 struct f2fs_cached_block *entry = fio->cache_entry; 1276 struct bio *bio; 1277 enum count_type io_type = IS_META_CACHE(entry->cache) ? 1278 F2FS_RD_META : F2FS_RD_NODE; 1279 1280 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, 1281 fio->is_por ? META_POR : (__is_meta_io(fio) ? 1282 META_GENERIC : DATA_GENERIC_ENHANCE))) 1283 return -EFSCORRUPTED; 1284 1285 bio = __bio_alloc(fio, 1); 1286 1287 bio_add_virt_nofail(bio, cache_address(entry), sbi->blocksize); 1288 f2fs_bio_add_cache(fio, bio); 1289 inc_cache_count(sbi, io_type); 1290 1291 f2fs_submit_read_bio(sbi, bio, fio->type); 1292 return 0; 1293 } 1294 1295 void f2fs_submit_cache_write(struct f2fs_io_info *fio) 1296 { 1297 struct f2fs_sb_info *sbi = fio->sbi; 1298 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); 1299 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp; 1300 struct f2fs_lock_context lc; 1301 struct folio *folio; 1302 1303 f2fs_bug_on(sbi, is_read_io(fio->op)); 1304 1305 f2fs_down_write_trace(&io->io_rwsem, &lc); 1306 next: 1307 #ifdef CONFIG_BLK_DEV_ZONED 1308 f2fs_wait_zone_io_completion(sbi, io, btype); 1309 #endif 1310 if (fio->in_list) { 1311 spin_lock(&io->io_lock); 1312 if (list_empty(&io->io_list)) { 1313 spin_unlock(&io->io_lock); 1314 goto out; 1315 } 1316 fio = list_first_entry(&io->io_list, 1317 struct f2fs_io_info, list); 1318 list_del(&fio->list); 1319 spin_unlock(&io->io_lock); 1320 } 1321 1322 verify_fio_blkaddr(fio); 1323 1324 fio->submitted = 1; 1325 inc_cache_count(sbi, F2FS_WB_CP_DATA); 1326 1327 if (io->bio && 1328 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio, 1329 fio->new_blkaddr))) 1330 __submit_merged_bio(io); 1331 alloc_new: 1332 if (io->bio == NULL) { 1333 io->bio = __bio_alloc(fio, BIO_MAX_VECS); 1334 io->fio = *fio; 1335 } 1336 1337 folio = cache_folio(fio->cache_entry); 1338 1339 if (!bio_add_folio(io->bio, folio, sbi->blocksize, 1340 offset_in_folio(folio, cache_address(fio->cache_entry)))) { 1341 f2fs_bug_on(sbi, !F2FS_BIO(io->bio)->entry); 1342 1343 __submit_merged_bio(io); 1344 goto alloc_new; 1345 } 1346 1347 f2fs_bio_add_cache(fio, io->bio); 1348 F2FS_BIO(io->bio)->entry_cnt++; 1349 1350 io->last_block_in_bio = fio->new_blkaddr; 1351 1352 #ifdef CONFIG_BLK_DEV_ZONED 1353 f2fs_submit_zone_io(sbi, fio, io, btype); 1354 #endif 1355 if (fio->in_list) 1356 goto next; 1357 out: 1358 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || 1359 !f2fs_is_checkpoint_ready(sbi)) 1360 __submit_merged_bio(io); 1361 f2fs_up_write_trace(&io->io_rwsem, &lc); 1362 } 1363 1364 static struct bio *f2fs_grab_read_bio(struct inode *inode, 1365 struct fsverity_info *vi, block_t blkaddr, 1366 unsigned nr_pages, blk_opf_t op_flag, 1367 pgoff_t first_idx, bool for_write) 1368 { 1369 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1370 struct bio *bio; 1371 struct bio_post_read_ctx *ctx = NULL; 1372 unsigned int post_read_steps = 0; 1373 sector_t sector; 1374 struct block_device *bdev = f2fs_target_device(sbi, blkaddr, §or); 1375 1376 bio = bio_alloc_bioset(bdev, bio_max_segs(nr_pages), 1377 REQ_OP_READ | op_flag, 1378 for_write ? GFP_NOIO : GFP_KERNEL, &f2fs_bioset); 1379 bio->bi_iter.bi_sector = sector; 1380 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS); 1381 bio->bi_end_io = f2fs_read_end_io; 1382 1383 if (vi) 1384 post_read_steps |= STEP_VERITY; 1385 1386 /* 1387 * STEP_DECOMPRESS is handled specially, since a compressed file might 1388 * contain both compressed and uncompressed clusters. We'll allocate a 1389 * bio_post_read_ctx if the file is compressed, but the caller is 1390 * responsible for enabling STEP_DECOMPRESS if it's actually needed. 1391 */ 1392 1393 if (post_read_steps || f2fs_compressed_file(inode)) { 1394 /* Due to the mempool, this never fails. */ 1395 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS); 1396 ctx->bio = bio; 1397 ctx->sbi = sbi; 1398 ctx->vi = vi; 1399 ctx->enabled_steps = post_read_steps; 1400 ctx->fs_blkaddr = blkaddr; 1401 ctx->decompression_attempted = false; 1402 bio->bi_private = ctx; 1403 } 1404 iostat_alloc_and_bind_ctx(sbi, bio, ctx); 1405 1406 return bio; 1407 } 1408 1409 /* This can handle encryption stuffs */ 1410 static void f2fs_submit_page_read(struct inode *inode, struct fsverity_info *vi, 1411 struct folio *folio, block_t blkaddr, 1412 blk_opf_t op_flags, bool for_write) 1413 { 1414 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1415 struct bio *bio; 1416 1417 bio = f2fs_grab_read_bio(inode, vi, blkaddr, 1, op_flags, folio->index, 1418 for_write); 1419 1420 /* wait for GCed page writeback via generic cache */ 1421 f2fs_wait_on_block_writeback(inode, blkaddr); 1422 1423 if (!bio_add_folio(bio, folio, PAGE_SIZE, 0)) 1424 f2fs_bug_on(sbi, 1); 1425 1426 inc_cache_count(sbi, F2FS_RD_DATA); 1427 f2fs_update_iostat(sbi, NULL, FS_DATA_READ_IO, F2FS_BLKSIZE(sbi)); 1428 f2fs_submit_read_bio(sbi, bio, DATA); 1429 } 1430 1431 static void __set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) 1432 { 1433 __le32 *addr = get_dnode_addr(dn->inode, dn->node_entry); 1434 1435 dn->data_blkaddr = blkaddr; 1436 addr[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr); 1437 } 1438 1439 /* 1440 * Lock ordering for the change of data block address: 1441 * ->data_page 1442 * ->node_folio 1443 * update block addresses in the node page 1444 */ 1445 void f2fs_set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) 1446 { 1447 f2fs_cache_wait_writeback(dn->node_entry); 1448 __set_data_blkaddr(dn, blkaddr); 1449 if (f2fs_mark_cache_dirty(dn->node_entry)) 1450 dn->node_changed = true; 1451 } 1452 1453 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) 1454 { 1455 f2fs_set_data_blkaddr(dn, blkaddr); 1456 f2fs_update_read_extent_cache(dn); 1457 } 1458 1459 /* dn->ofs_in_node will be returned with up-to-date last block pointer */ 1460 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) 1461 { 1462 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 1463 int err; 1464 1465 if (!count) 1466 return 0; 1467 1468 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) 1469 return -EPERM; 1470 err = inc_valid_block_count(sbi, dn->inode, &count, true, false); 1471 if (unlikely(err)) 1472 return err; 1473 1474 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, 1475 dn->ofs_in_node, count); 1476 1477 f2fs_cache_wait_writeback(dn->node_entry); 1478 1479 for (; count > 0; dn->ofs_in_node++) { 1480 block_t blkaddr = f2fs_data_blkaddr(dn); 1481 1482 if (blkaddr == NULL_ADDR) { 1483 __set_data_blkaddr(dn, NEW_ADDR); 1484 count--; 1485 } 1486 } 1487 1488 if (f2fs_mark_cache_dirty(dn->node_entry)) 1489 dn->node_changed = true; 1490 return 0; 1491 } 1492 1493 /* Should keep dn->ofs_in_node unchanged */ 1494 int f2fs_reserve_new_block(struct dnode_of_data *dn) 1495 { 1496 unsigned int ofs_in_node = dn->ofs_in_node; 1497 int ret; 1498 1499 ret = f2fs_reserve_new_blocks(dn, 1); 1500 dn->ofs_in_node = ofs_in_node; 1501 return ret; 1502 } 1503 1504 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) 1505 { 1506 bool need_put = dn->inode_entry ? false : true; 1507 int err; 1508 1509 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE); 1510 if (err) 1511 return err; 1512 1513 if (dn->data_blkaddr == NULL_ADDR) 1514 err = f2fs_reserve_new_block(dn); 1515 if (err || need_put) 1516 f2fs_put_dnode(dn); 1517 return err; 1518 } 1519 1520 static inline struct fsverity_info *f2fs_need_verity(const struct inode *inode, 1521 pgoff_t idx) 1522 { 1523 if (idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE)) 1524 return fsverity_get_info(inode); 1525 return NULL; 1526 } 1527 1528 struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index, 1529 blk_opf_t op_flags, bool for_write, pgoff_t *next_pgofs) 1530 { 1531 struct address_space *mapping = inode->i_mapping; 1532 struct dnode_of_data dn; 1533 struct folio *folio; 1534 int err; 1535 retry: 1536 folio = f2fs_grab_cache_folio(mapping, index, for_write); 1537 if (IS_ERR(folio)) 1538 return folio; 1539 1540 if (folio_test_large(folio)) { 1541 pgoff_t folio_index = mapping_align_index(mapping, index); 1542 unsigned long nr_pages = folio_nr_pages(folio); 1543 1544 f2fs_folio_put(folio, true); 1545 invalidate_inode_pages2_range(mapping, folio_index, 1546 folio_index + nr_pages - 1); 1547 f2fs_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT); 1548 goto retry; 1549 } 1550 1551 if (f2fs_lookup_read_extent_cache_block(inode, index, 1552 &dn.data_blkaddr)) { 1553 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr, 1554 DATA_GENERIC_ENHANCE_READ)) { 1555 err = -EFSCORRUPTED; 1556 goto put_err; 1557 } 1558 goto got_it; 1559 } 1560 1561 set_new_dnode(&dn, inode, NULL, NULL, 0); 1562 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 1563 if (err) { 1564 if (err == -ENOENT && next_pgofs) 1565 *next_pgofs = f2fs_get_next_page_offset(&dn, index); 1566 goto put_err; 1567 } 1568 f2fs_put_dnode(&dn); 1569 1570 if (unlikely(dn.data_blkaddr == NULL_ADDR)) { 1571 err = -ENOENT; 1572 if (next_pgofs) 1573 *next_pgofs = index + 1; 1574 goto put_err; 1575 } 1576 if (dn.data_blkaddr != NEW_ADDR && 1577 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode), 1578 dn.data_blkaddr, 1579 DATA_GENERIC_ENHANCE)) { 1580 err = -EFSCORRUPTED; 1581 goto put_err; 1582 } 1583 got_it: 1584 if (folio_test_uptodate(folio)) { 1585 folio_unlock(folio); 1586 return folio; 1587 } 1588 1589 /* 1590 * A new dentry page is allocated but not able to be written, since its 1591 * new inode page couldn't be allocated due to -ENOSPC. 1592 * In such the case, its blkaddr can be remained as NEW_ADDR. 1593 * see, f2fs_add_link -> f2fs_get_new_data_folio -> 1594 * f2fs_init_inode_metadata. 1595 */ 1596 if (dn.data_blkaddr == NEW_ADDR) { 1597 folio_zero_segment(folio, 0, folio_size(folio)); 1598 if (!folio_test_uptodate(folio)) 1599 folio_mark_uptodate(folio); 1600 folio_unlock(folio); 1601 return folio; 1602 } 1603 1604 f2fs_submit_page_read(inode, f2fs_need_verity(inode, folio->index), 1605 folio, dn.data_blkaddr, op_flags, for_write); 1606 return folio; 1607 1608 put_err: 1609 f2fs_folio_put(folio, true); 1610 return ERR_PTR(err); 1611 } 1612 1613 struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index, 1614 pgoff_t *next_pgofs) 1615 { 1616 struct address_space *mapping = inode->i_mapping; 1617 struct folio *folio; 1618 1619 folio = f2fs_filemap_get_folio(mapping, index, FGP_ACCESSED, 0); 1620 if (IS_ERR(folio)) 1621 goto read; 1622 if (folio_test_uptodate(folio)) 1623 return folio; 1624 f2fs_folio_put(folio, false); 1625 1626 read: 1627 folio = f2fs_get_read_data_folio(inode, index, 0, false, next_pgofs); 1628 if (IS_ERR(folio)) 1629 return folio; 1630 1631 if (folio_test_uptodate(folio)) 1632 return folio; 1633 1634 folio_wait_locked(folio); 1635 if (unlikely(!folio_test_uptodate(folio))) { 1636 f2fs_folio_put(folio, false); 1637 return ERR_PTR(-EIO); 1638 } 1639 return folio; 1640 } 1641 1642 /* 1643 * If it tries to access a hole, return an error. 1644 * Because, the callers, functions in dir.c and GC, should be able to know 1645 * whether this page exists or not. 1646 */ 1647 struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index, 1648 bool for_write) 1649 { 1650 struct address_space *mapping = inode->i_mapping; 1651 struct folio *folio; 1652 1653 folio = f2fs_get_read_data_folio(inode, index, 0, for_write, NULL); 1654 if (IS_ERR(folio)) 1655 return folio; 1656 1657 /* wait for read completion */ 1658 folio_lock(folio); 1659 if (unlikely(folio->mapping != mapping || !folio_test_uptodate(folio))) { 1660 f2fs_folio_put(folio, true); 1661 return ERR_PTR(-EIO); 1662 } 1663 return folio; 1664 } 1665 1666 /* 1667 * Caller ensures that this data page is never allocated. 1668 * A new zero-filled data page is allocated in the page cache. 1669 * 1670 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and 1671 * f2fs_unlock_op(). 1672 * Note that, ientry is set only by make_empty_dir, and if any error occur, 1673 * ientry should be released by this function. 1674 */ 1675 struct folio *f2fs_get_new_data_folio(struct inode *inode, 1676 struct f2fs_cached_block *ientry, pgoff_t index, bool new_i_size) 1677 { 1678 struct address_space *mapping = inode->i_mapping; 1679 struct folio *folio; 1680 struct dnode_of_data dn; 1681 int err; 1682 1683 folio = f2fs_grab_cache_folio(mapping, index, true); 1684 if (IS_ERR(folio)) { 1685 /* 1686 * before exiting, we should make sure ientry will be released 1687 * if any error occur. 1688 */ 1689 f2fs_put_cache(ientry, true); 1690 return ERR_PTR(-ENOMEM); 1691 } 1692 1693 set_new_dnode(&dn, inode, ientry, NULL, 0); 1694 err = f2fs_reserve_block(&dn, index); 1695 if (err) { 1696 f2fs_folio_put(folio, true); 1697 return ERR_PTR(err); 1698 } 1699 if (!ientry) 1700 f2fs_put_dnode(&dn); 1701 1702 if (folio_test_uptodate(folio)) 1703 goto got_it; 1704 1705 if (dn.data_blkaddr == NEW_ADDR) { 1706 folio_zero_segment(folio, 0, folio_size(folio)); 1707 if (!folio_test_uptodate(folio)) 1708 folio_mark_uptodate(folio); 1709 } else { 1710 f2fs_folio_put(folio, true); 1711 1712 /* if ientry exists, blkaddr should be NEW_ADDR */ 1713 f2fs_bug_on(F2FS_I_SB(inode), ientry); 1714 folio = f2fs_get_lock_data_folio(inode, index, true); 1715 if (IS_ERR(folio)) 1716 return folio; 1717 } 1718 got_it: 1719 if (new_i_size && i_size_read(inode) < 1720 ((loff_t)(index + 1) << PAGE_SHIFT)) 1721 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT)); 1722 return folio; 1723 } 1724 1725 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type) 1726 { 1727 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 1728 struct f2fs_summary sum; 1729 struct node_info ni; 1730 block_t old_blkaddr; 1731 blkcnt_t count = 1; 1732 int err; 1733 1734 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) 1735 return -EPERM; 1736 1737 err = f2fs_get_node_info(sbi, dn->nid, &ni, false); 1738 if (err) 1739 return err; 1740 1741 dn->data_blkaddr = f2fs_data_blkaddr(dn); 1742 if (dn->data_blkaddr == NULL_ADDR) { 1743 err = inc_valid_block_count(sbi, dn->inode, &count, true, false); 1744 if (unlikely(err)) 1745 return err; 1746 } 1747 1748 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); 1749 old_blkaddr = dn->data_blkaddr; 1750 err = f2fs_allocate_data_block(sbi, old_blkaddr, 1751 &dn->data_blkaddr, &sum, seg_type, NULL); 1752 if (err) { 1753 if (old_blkaddr == NULL_ADDR) 1754 dec_valid_block_count(sbi, dn->inode, count); 1755 return err; 1756 } 1757 1758 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) 1759 f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1); 1760 1761 f2fs_update_data_blkaddr(dn, dn->data_blkaddr); 1762 return 0; 1763 } 1764 1765 static void f2fs_map_lock(struct f2fs_sb_info *sbi, 1766 struct f2fs_lock_context *lc, 1767 int flag) 1768 { 1769 if (flag == F2FS_GET_BLOCK_PRE_AIO) 1770 f2fs_down_read_trace(&sbi->node_change, lc); 1771 else 1772 f2fs_lock_op(sbi, lc); 1773 } 1774 1775 static void f2fs_map_unlock(struct f2fs_sb_info *sbi, 1776 struct f2fs_lock_context *lc, 1777 int flag) 1778 { 1779 if (flag == F2FS_GET_BLOCK_PRE_AIO) 1780 f2fs_up_read_trace(&sbi->node_change, lc); 1781 else 1782 f2fs_unlock_op(sbi, lc); 1783 } 1784 1785 int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index) 1786 { 1787 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 1788 struct f2fs_lock_context lc; 1789 int err = 0; 1790 1791 f2fs_map_lock(sbi, &lc, F2FS_GET_BLOCK_PRE_AIO); 1792 if (!f2fs_lookup_read_extent_cache_block(dn->inode, index, 1793 &dn->data_blkaddr)) 1794 err = f2fs_reserve_block(dn, index); 1795 f2fs_map_unlock(sbi, &lc, F2FS_GET_BLOCK_PRE_AIO); 1796 1797 return err; 1798 } 1799 1800 static int f2fs_map_no_dnode(struct inode *inode, 1801 struct f2fs_map_blocks *map, struct dnode_of_data *dn, 1802 pgoff_t pgoff) 1803 { 1804 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1805 1806 /* 1807 * There is one exceptional case that read_node_page() may return 1808 * -ENOENT due to filesystem has been shutdown or cp_error, return 1809 * -EIO in that case. 1810 */ 1811 if (map->m_may_create && 1812 (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || f2fs_cp_error(sbi))) 1813 return -EIO; 1814 1815 if (map->m_next_pgofs) 1816 *map->m_next_pgofs = f2fs_get_next_page_offset(dn, pgoff); 1817 if (map->m_next_extent) 1818 *map->m_next_extent = f2fs_get_next_page_offset(dn, pgoff); 1819 return 0; 1820 } 1821 1822 static bool f2fs_map_blocks_cached(struct inode *inode, 1823 struct f2fs_map_blocks *map, int flag) 1824 { 1825 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1826 unsigned int maxblocks = map->m_len; 1827 pgoff_t pgoff = (pgoff_t)map->m_lblk; 1828 struct extent_info ei = {}; 1829 1830 if (!f2fs_lookup_read_extent_cache(inode, pgoff, &ei)) 1831 return false; 1832 1833 map->m_pblk = ei.blk + pgoff - ei.fofs; 1834 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgoff); 1835 map->m_flags = F2FS_MAP_MAPPED; 1836 if (map->m_next_extent) 1837 *map->m_next_extent = pgoff + map->m_len; 1838 1839 /* for hardware encryption, but to avoid potential issue in future */ 1840 if (flag == F2FS_GET_BLOCK_DIO) 1841 f2fs_wait_on_block_writeback_range(inode, 1842 map->m_pblk, map->m_len); 1843 1844 map->m_multidev_dio = f2fs_allow_multi_device_dio(sbi, flag); 1845 if (map->m_multidev_dio) { 1846 int bidx = f2fs_target_device_index(sbi, map->m_pblk); 1847 struct f2fs_dev_info *dev = &sbi->devs[bidx]; 1848 1849 map->m_bdev = dev->bdev; 1850 map->m_len = min(map->m_len, dev->end_blk + 1 - map->m_pblk); 1851 map->m_pblk -= dev->start_blk; 1852 } else { 1853 map->m_bdev = inode->i_sb->s_bdev; 1854 } 1855 return true; 1856 } 1857 1858 static bool map_is_mergeable(struct f2fs_sb_info *sbi, 1859 struct f2fs_map_blocks *map, 1860 block_t blkaddr, int flag, int bidx, 1861 int ofs) 1862 { 1863 if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev) 1864 return false; 1865 if (map->m_pblk != NEW_ADDR && blkaddr == (map->m_pblk + ofs)) 1866 return true; 1867 if (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) 1868 return true; 1869 if (flag == F2FS_GET_BLOCK_PRE_DIO) 1870 return true; 1871 if (flag == F2FS_GET_BLOCK_DIO && 1872 map->m_pblk == NULL_ADDR && blkaddr == NULL_ADDR) 1873 return true; 1874 return false; 1875 } 1876 1877 /* 1878 * f2fs_map_blocks() tries to find or build mapping relationship which 1879 * maps continuous logical blocks to physical blocks, and return such 1880 * info via f2fs_map_blocks structure. 1881 */ 1882 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag) 1883 { 1884 unsigned int maxblocks = map->m_len; 1885 struct dnode_of_data dn; 1886 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1887 struct f2fs_lock_context lc; 1888 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE; 1889 pgoff_t pgofs, end_offset, end; 1890 int err = 0, ofs = 1; 1891 unsigned int ofs_in_node, last_ofs_in_node; 1892 blkcnt_t prealloc; 1893 block_t blkaddr; 1894 unsigned int start_pgofs; 1895 int bidx = 0; 1896 bool is_hole; 1897 bool lfs_dio_write; 1898 1899 if (!maxblocks) 1900 return 0; 1901 1902 lfs_dio_write = (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) && 1903 map->m_may_create); 1904 1905 if (!map->m_may_create && f2fs_map_blocks_cached(inode, map, flag)) { 1906 struct extent_info ei; 1907 1908 /* 1909 * 1. If map->m_multidev_dio is true, map->m_pblk cannot be 1910 * waitted by f2fs_wait_on_block_writeback_range() and are not 1911 * mergeable. 1912 * 2. If pgofs hits the read extent cache, it means the mapping 1913 * is already cached in the extent cache, but it is not 1914 * mergeable, and there is no need to query the mapping again 1915 * via f2fs_get_dnode_of_data(). 1916 */ 1917 pgofs = (pgoff_t)map->m_lblk + map->m_len; 1918 if (map->m_len == maxblocks || 1919 map->m_multidev_dio || 1920 f2fs_lookup_read_extent_cache(inode, pgofs, &ei)) 1921 goto out; 1922 ofs = map->m_len; 1923 goto map_more; 1924 } 1925 1926 map->m_bdev = inode->i_sb->s_bdev; 1927 map->m_multidev_dio = 1928 f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag); 1929 1930 map->m_len = 0; 1931 map->m_flags = 0; 1932 1933 /* it only supports block size == page size */ 1934 pgofs = (pgoff_t)map->m_lblk; 1935 map_more: 1936 end = (pgoff_t)map->m_lblk + maxblocks; 1937 1938 if (flag == F2FS_GET_BLOCK_PRECACHE) 1939 mode = LOOKUP_NODE_RA; 1940 1941 next_dnode: 1942 if (map->m_may_create) { 1943 if (f2fs_lfs_mode(sbi)) 1944 f2fs_balance_fs(sbi, true); 1945 f2fs_map_lock(sbi, &lc, flag); 1946 } 1947 1948 /* When reading holes, we need its node page */ 1949 set_new_dnode(&dn, inode, NULL, NULL, 0); 1950 err = f2fs_get_dnode_of_data(&dn, pgofs, mode); 1951 if (err) { 1952 if (flag == F2FS_GET_BLOCK_BMAP) 1953 map->m_pblk = 0; 1954 if (err == -ENOENT) 1955 err = f2fs_map_no_dnode(inode, map, &dn, pgofs); 1956 goto unlock_out; 1957 } 1958 1959 start_pgofs = pgofs; 1960 prealloc = 0; 1961 last_ofs_in_node = ofs_in_node = dn.ofs_in_node; 1962 end_offset = ADDRS_PER_PAGE(dn.node_entry, inode); 1963 1964 next_block: 1965 blkaddr = f2fs_data_blkaddr(&dn); 1966 is_hole = !__is_valid_data_blkaddr(blkaddr); 1967 if (!is_hole && 1968 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) { 1969 err = -EFSCORRUPTED; 1970 goto sync_out; 1971 } 1972 1973 /* use out-place-update for direct IO under LFS mode */ 1974 if (map->m_may_create && (is_hole || 1975 (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) && 1976 !f2fs_is_pinned_file(inode) && map->m_last_pblk != blkaddr))) { 1977 if (unlikely(f2fs_cp_error(sbi))) { 1978 err = -EIO; 1979 goto sync_out; 1980 } 1981 1982 switch (flag) { 1983 case F2FS_GET_BLOCK_PRE_AIO: 1984 if (blkaddr == NULL_ADDR) { 1985 prealloc++; 1986 last_ofs_in_node = dn.ofs_in_node; 1987 } 1988 break; 1989 case F2FS_GET_BLOCK_PRE_DIO: 1990 case F2FS_GET_BLOCK_DIO: 1991 err = __allocate_data_block(&dn, map->m_seg_type); 1992 if (err) 1993 goto sync_out; 1994 if (flag == F2FS_GET_BLOCK_PRE_DIO) 1995 file_need_truncate(inode); 1996 set_inode_flag(inode, FI_APPEND_WRITE); 1997 break; 1998 default: 1999 WARN_ON_ONCE(1); 2000 err = -EIO; 2001 goto sync_out; 2002 } 2003 2004 blkaddr = dn.data_blkaddr; 2005 if (is_hole) 2006 map->m_flags |= F2FS_MAP_NEW; 2007 } else if (is_hole) { 2008 if (f2fs_compressed_file(inode) && 2009 f2fs_sanity_check_cluster(&dn)) { 2010 err = -EFSCORRUPTED; 2011 f2fs_handle_error(sbi, 2012 ERROR_CORRUPTED_CLUSTER); 2013 fserror_report_file_metadata(inode, err, GFP_NOFS); 2014 goto sync_out; 2015 } 2016 2017 switch (flag) { 2018 case F2FS_GET_BLOCK_PRECACHE: 2019 goto sync_out; 2020 case F2FS_GET_BLOCK_BMAP: 2021 map->m_pblk = 0; 2022 goto sync_out; 2023 case F2FS_GET_BLOCK_FIEMAP: 2024 if (blkaddr == NULL_ADDR) { 2025 if (map->m_next_pgofs) 2026 *map->m_next_pgofs = pgofs + 1; 2027 goto sync_out; 2028 } 2029 break; 2030 case F2FS_GET_BLOCK_DIO: 2031 if (map->m_next_pgofs) 2032 *map->m_next_pgofs = pgofs + 1; 2033 break; 2034 default: 2035 /* for defragment case */ 2036 if (map->m_next_pgofs) 2037 *map->m_next_pgofs = pgofs + 1; 2038 goto sync_out; 2039 } 2040 } 2041 2042 if (flag == F2FS_GET_BLOCK_PRE_AIO) 2043 goto skip; 2044 2045 if (map->m_multidev_dio) 2046 bidx = f2fs_target_device_index(sbi, blkaddr); 2047 2048 if (map->m_len == 0) { 2049 /* reserved delalloc block should be mapped for fiemap. */ 2050 if (blkaddr == NEW_ADDR) 2051 map->m_flags |= F2FS_MAP_DELALLOC; 2052 /* DIO READ and hole case, should not map the blocks. */ 2053 if (!(flag == F2FS_GET_BLOCK_DIO && is_hole && !map->m_may_create)) 2054 map->m_flags |= F2FS_MAP_MAPPED; 2055 2056 map->m_pblk = blkaddr; 2057 map->m_len = 1; 2058 2059 if (map->m_multidev_dio) 2060 map->m_bdev = FDEV(bidx).bdev; 2061 2062 if (lfs_dio_write) 2063 map->m_last_pblk = NULL_ADDR; 2064 } else if (map_is_mergeable(sbi, map, blkaddr, flag, bidx, ofs)) { 2065 ofs++; 2066 map->m_len++; 2067 } else { 2068 if (lfs_dio_write && !f2fs_is_pinned_file(inode)) 2069 map->m_last_pblk = blkaddr; 2070 goto sync_out; 2071 } 2072 2073 skip: 2074 dn.ofs_in_node++; 2075 pgofs++; 2076 2077 /* preallocate blocks in batch for one dnode page */ 2078 if (flag == F2FS_GET_BLOCK_PRE_AIO && 2079 (pgofs == end || dn.ofs_in_node == end_offset)) { 2080 2081 dn.ofs_in_node = ofs_in_node; 2082 err = f2fs_reserve_new_blocks(&dn, prealloc); 2083 if (err) 2084 goto sync_out; 2085 2086 map->m_len += dn.ofs_in_node - ofs_in_node; 2087 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) { 2088 err = -ENOSPC; 2089 goto sync_out; 2090 } 2091 dn.ofs_in_node = end_offset; 2092 } 2093 2094 if (pgofs >= end) 2095 goto sync_out; 2096 else if (dn.ofs_in_node < end_offset) 2097 goto next_block; 2098 2099 if (flag == F2FS_GET_BLOCK_PRECACHE) { 2100 if (map->m_flags & F2FS_MAP_MAPPED) { 2101 unsigned int ofs = start_pgofs - map->m_lblk; 2102 2103 f2fs_update_read_extent_cache_range(&dn, 2104 start_pgofs, map->m_pblk + ofs, 2105 map->m_len - ofs); 2106 } 2107 } 2108 2109 f2fs_put_dnode(&dn); 2110 2111 if (map->m_may_create) { 2112 f2fs_map_unlock(sbi, &lc, flag); 2113 f2fs_balance_fs(sbi, dn.node_changed); 2114 } 2115 goto next_dnode; 2116 2117 sync_out: 2118 2119 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) { 2120 /* 2121 * for hardware encryption, but to avoid potential issue 2122 * in future 2123 */ 2124 f2fs_wait_on_block_writeback_range(inode, 2125 map->m_pblk, map->m_len); 2126 2127 if (map->m_multidev_dio) { 2128 block_t blk_addr = map->m_pblk; 2129 2130 bidx = f2fs_target_device_index(sbi, map->m_pblk); 2131 2132 map->m_bdev = FDEV(bidx).bdev; 2133 map->m_pblk -= FDEV(bidx).start_blk; 2134 2135 if (map->m_may_create) 2136 f2fs_update_device_state(sbi, inode->i_ino, 2137 blk_addr, map->m_len); 2138 2139 f2fs_bug_on(sbi, blk_addr + map->m_len > 2140 FDEV(bidx).end_blk + 1); 2141 } 2142 } 2143 2144 if (flag == F2FS_GET_BLOCK_PRECACHE) { 2145 if (map->m_flags & F2FS_MAP_MAPPED) { 2146 unsigned int ofs = start_pgofs - map->m_lblk; 2147 2148 if (map->m_len > ofs) 2149 f2fs_update_read_extent_cache_range(&dn, 2150 start_pgofs, map->m_pblk + ofs, 2151 map->m_len - ofs); 2152 } 2153 if (map->m_next_extent) 2154 *map->m_next_extent = is_hole ? pgofs + 1 : pgofs; 2155 } 2156 f2fs_put_dnode(&dn); 2157 unlock_out: 2158 if (map->m_may_create) { 2159 f2fs_map_unlock(sbi, &lc, flag); 2160 f2fs_balance_fs(sbi, dn.node_changed); 2161 } 2162 out: 2163 trace_f2fs_map_blocks(inode, map, flag, err); 2164 return err; 2165 } 2166 2167 static bool __f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len, 2168 bool check_first) 2169 { 2170 struct f2fs_map_blocks map; 2171 block_t last_lblk; 2172 int err; 2173 2174 if (pos + len > i_size_read(inode)) 2175 return false; 2176 2177 map.m_lblk = F2FS_BYTES_TO_BLK(F2FS_I_SB(inode), pos); 2178 map.m_next_pgofs = NULL; 2179 map.m_next_extent = NULL; 2180 map.m_seg_type = NO_CHECK_TYPE; 2181 map.m_may_create = false; 2182 last_lblk = F2FS_BLK_ALIGN(F2FS_I_SB(inode), pos + len); 2183 2184 while (map.m_lblk < last_lblk) { 2185 map.m_len = last_lblk - map.m_lblk; 2186 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT); 2187 if (err || map.m_len == 0) 2188 return false; 2189 map.m_lblk += map.m_len; 2190 if (check_first) 2191 break; 2192 } 2193 return true; 2194 } 2195 2196 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len) 2197 { 2198 return __f2fs_overwrite_io(inode, pos, len, false); 2199 } 2200 2201 static int f2fs_xattr_fiemap(struct inode *inode, 2202 struct fiemap_extent_info *fieinfo) 2203 { 2204 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2205 struct node_info ni; 2206 __u64 phys = 0, len; 2207 __u32 flags; 2208 nid_t xnid = F2FS_I(inode)->i_xattr_nid; 2209 int err = 0; 2210 2211 if (f2fs_has_inline_xattr(inode)) { 2212 int offset; 2213 struct f2fs_cached_block *entry = 2214 f2fs_grab_node_cache(sbi, inode->i_ino); 2215 2216 if (IS_ERR(entry)) 2217 return PTR_ERR(entry); 2218 2219 err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false); 2220 if (err) { 2221 f2fs_put_cache(entry, true); 2222 return err; 2223 } 2224 2225 phys = F2FS_BLK_TO_BYTES(sbi, ni.blk_addr); 2226 offset = offsetof(struct f2fs_inode, i_addr) + 2227 sizeof(__le32) * (DEF_ADDRS_PER_INODE(sbi) - 2228 get_inline_xattr_addrs(inode)); 2229 2230 phys += offset; 2231 len = inline_xattr_size(inode); 2232 2233 f2fs_put_cache(entry, true); 2234 2235 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED; 2236 2237 if (!xnid) 2238 flags |= FIEMAP_EXTENT_LAST; 2239 2240 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); 2241 trace_f2fs_fiemap(inode, 0, phys, len, flags, err); 2242 if (err) 2243 return err; 2244 } 2245 2246 if (xnid) { 2247 struct f2fs_cached_block *entry = 2248 f2fs_grab_node_cache(sbi, xnid); 2249 2250 if (IS_ERR(entry)) 2251 return PTR_ERR(entry); 2252 2253 err = f2fs_get_node_info(sbi, xnid, &ni, false); 2254 if (err) { 2255 f2fs_put_cache(entry, true); 2256 return err; 2257 } 2258 2259 phys = F2FS_BLK_TO_BYTES(sbi, ni.blk_addr); 2260 len = inode->i_sb->s_blocksize; 2261 2262 f2fs_put_cache(entry, true); 2263 2264 flags = FIEMAP_EXTENT_LAST; 2265 } 2266 2267 if (phys) { 2268 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); 2269 trace_f2fs_fiemap(inode, 0, phys, len, flags, err); 2270 } 2271 2272 return (err < 0 ? err : 0); 2273 } 2274 2275 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 2276 u64 start, u64 len) 2277 { 2278 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2279 struct f2fs_map_blocks map; 2280 sector_t start_blk, last_blk, blk_len, max_len; 2281 pgoff_t next_pgofs; 2282 u64 logical = 0, phys = 0, size = 0; 2283 u32 flags = 0; 2284 int ret = 0; 2285 bool compr_cluster = false, compr_appended; 2286 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size; 2287 unsigned int count_in_cluster = 0; 2288 loff_t maxbytes; 2289 2290 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { 2291 ret = f2fs_precache_extents(inode); 2292 if (ret) 2293 return ret; 2294 } 2295 2296 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR); 2297 if (ret) 2298 return ret; 2299 2300 inode_lock_shared(inode); 2301 2302 maxbytes = F2FS_BLK_TO_BYTES(sbi, max_file_blocks(sbi, inode)); 2303 if (start > maxbytes) { 2304 ret = -EFBIG; 2305 goto out; 2306 } 2307 2308 if (len > maxbytes || (maxbytes - len) < start) 2309 len = maxbytes - start; 2310 2311 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 2312 ret = f2fs_xattr_fiemap(inode, fieinfo); 2313 goto out; 2314 } 2315 2316 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { 2317 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len); 2318 if (ret != -EAGAIN) 2319 goto out; 2320 } 2321 2322 start_blk = F2FS_BYTES_TO_BLK(sbi, start); 2323 last_blk = F2FS_BYTES_TO_BLK(sbi, start + len - 1); 2324 blk_len = last_blk - start_blk + 1; 2325 max_len = F2FS_BYTES_TO_BLK(sbi, maxbytes) - start_blk; 2326 2327 next: 2328 memset(&map, 0, sizeof(map)); 2329 map.m_lblk = start_blk; 2330 map.m_len = blk_len; 2331 map.m_next_pgofs = &next_pgofs; 2332 map.m_seg_type = NO_CHECK_TYPE; 2333 2334 if (compr_cluster) { 2335 map.m_lblk += 1; 2336 map.m_len = cluster_size - count_in_cluster; 2337 } 2338 2339 ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP); 2340 if (ret) 2341 goto out; 2342 2343 /* HOLE */ 2344 if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) { 2345 start_blk = next_pgofs; 2346 2347 if (F2FS_BLK_TO_BYTES(sbi, start_blk) < maxbytes) 2348 goto prep_next; 2349 2350 flags |= FIEMAP_EXTENT_LAST; 2351 } 2352 2353 /* 2354 * current extent may cross boundary of inquiry, increase len to 2355 * requery. 2356 */ 2357 if (!compr_cluster && (map.m_flags & F2FS_MAP_MAPPED) && 2358 map.m_lblk + map.m_len - 1 == last_blk && 2359 blk_len != max_len) { 2360 blk_len = max_len; 2361 goto next; 2362 } 2363 2364 compr_appended = false; 2365 /* In a case of compressed cluster, append this to the last extent */ 2366 if (compr_cluster && ((map.m_flags & F2FS_MAP_DELALLOC) || 2367 !(map.m_flags & F2FS_MAP_FLAGS))) { 2368 compr_appended = true; 2369 goto skip_fill; 2370 } 2371 2372 if (size) { 2373 flags |= FIEMAP_EXTENT_MERGED; 2374 if (IS_ENCRYPTED(inode)) 2375 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; 2376 2377 ret = fiemap_fill_next_extent(fieinfo, logical, 2378 phys, size, flags); 2379 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret); 2380 if (ret) 2381 goto out; 2382 size = 0; 2383 } 2384 2385 if (start_blk > last_blk) 2386 goto out; 2387 2388 skip_fill: 2389 if (map.m_pblk == COMPRESS_ADDR) { 2390 compr_cluster = true; 2391 count_in_cluster = 1; 2392 } else if (compr_appended) { 2393 unsigned int appended_blks = cluster_size - 2394 count_in_cluster + 1; 2395 size += F2FS_BLK_TO_BYTES(sbi, appended_blks); 2396 start_blk += appended_blks; 2397 compr_cluster = false; 2398 } else { 2399 logical = F2FS_BLK_TO_BYTES(sbi, start_blk); 2400 phys = __is_valid_data_blkaddr(map.m_pblk) ? 2401 F2FS_BLK_TO_BYTES(sbi, map.m_pblk) : 0; 2402 size = F2FS_BLK_TO_BYTES(sbi, map.m_len); 2403 flags = 0; 2404 2405 if (compr_cluster) { 2406 flags = FIEMAP_EXTENT_ENCODED; 2407 count_in_cluster += map.m_len; 2408 if (count_in_cluster == cluster_size) { 2409 compr_cluster = false; 2410 size += F2FS_BLKSIZE(sbi); 2411 } 2412 } else if (map.m_flags & F2FS_MAP_DELALLOC) { 2413 flags = FIEMAP_EXTENT_UNWRITTEN; 2414 } 2415 2416 start_blk += F2FS_BYTES_TO_BLK(sbi, size); 2417 } 2418 2419 prep_next: 2420 cond_resched(); 2421 if (fatal_signal_pending(current)) 2422 ret = -EINTR; 2423 else 2424 goto next; 2425 out: 2426 if (ret == 1) 2427 ret = 0; 2428 2429 inode_unlock_shared(inode); 2430 return ret; 2431 } 2432 2433 static inline loff_t f2fs_readpage_limit(struct inode *inode) 2434 { 2435 if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode)) 2436 return F2FS_BLK_TO_BYTES(F2FS_I_SB(inode), 2437 max_file_blocks(F2FS_I_SB(inode), inode)); 2438 2439 return i_size_read(inode); 2440 } 2441 2442 static inline blk_opf_t f2fs_ra_op_flags(struct readahead_control *rac) 2443 { 2444 return rac ? REQ_RAHEAD : 0; 2445 } 2446 2447 static int f2fs_read_single_page(struct inode *inode, struct fsverity_info *vi, 2448 struct folio *folio, unsigned int nr_pages, 2449 struct f2fs_map_blocks *map, 2450 struct bio **bio_ret, 2451 sector_t *last_block_in_bio, 2452 struct readahead_control *rac) 2453 { 2454 struct bio *bio = *bio_ret; 2455 const unsigned int blocksize = F2FS_BLKSIZE(F2FS_I_SB(inode)); 2456 sector_t block_in_file; 2457 sector_t last_block; 2458 sector_t last_block_in_file; 2459 sector_t block_nr; 2460 pgoff_t index = folio->index; 2461 int ret = 0; 2462 2463 block_in_file = (sector_t)index; 2464 last_block = block_in_file + nr_pages; 2465 last_block_in_file = F2FS_BYTES_TO_BLK(F2FS_I_SB(inode), 2466 f2fs_readpage_limit(inode) + 2467 blocksize - 1); 2468 if (last_block > last_block_in_file) 2469 last_block = last_block_in_file; 2470 2471 /* just zeroing out page which is beyond EOF */ 2472 if (block_in_file >= last_block) 2473 goto zero_out; 2474 /* 2475 * Map blocks using the previous result first. 2476 */ 2477 if (map->m_flags & F2FS_MAP_MAPPED) { 2478 if (block_in_file > map->m_lblk && 2479 block_in_file < (map->m_lblk + map->m_len)) 2480 goto got_it; 2481 } else if (block_in_file < *map->m_next_pgofs) { 2482 goto got_it; 2483 } 2484 2485 /* 2486 * Then do more f2fs_map_blocks() calls until we are 2487 * done with this page. 2488 */ 2489 map->m_lblk = block_in_file; 2490 map->m_len = last_block - block_in_file; 2491 2492 ret = f2fs_map_blocks(inode, map, F2FS_GET_BLOCK_DEFAULT); 2493 if (ret) 2494 goto out; 2495 got_it: 2496 if ((map->m_flags & F2FS_MAP_MAPPED)) { 2497 block_nr = map->m_pblk + block_in_file - map->m_lblk; 2498 2499 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr, 2500 DATA_GENERIC_ENHANCE_READ)) { 2501 ret = -EFSCORRUPTED; 2502 goto out; 2503 } 2504 } else { 2505 zero_out: 2506 folio_zero_segment(folio, 0, folio_size(folio)); 2507 if (vi && !fsverity_verify_folio(vi, folio)) { 2508 ret = -EIO; 2509 goto out; 2510 } 2511 if (!folio_test_uptodate(folio)) 2512 folio_mark_uptodate(folio); 2513 folio_unlock(folio); 2514 goto out; 2515 } 2516 2517 /* 2518 * This page will go to BIO. Do we need to send this 2519 * BIO off first? 2520 */ 2521 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio, 2522 *last_block_in_bio, block_nr) || 2523 !f2fs_crypt_mergeable_bio(bio, inode, index, NULL))) { 2524 submit_and_realloc: 2525 f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); 2526 bio = NULL; 2527 } 2528 if (bio == NULL) 2529 bio = f2fs_grab_read_bio(inode, vi, block_nr, nr_pages, 2530 f2fs_ra_op_flags(rac), index, false); 2531 2532 /* 2533 * If the page is under writeback, we need to wait for 2534 * its completion to see the correct decrypted data. 2535 */ 2536 f2fs_wait_on_block_writeback(inode, block_nr); 2537 2538 if (!bio_add_folio(bio, folio, blocksize, 0)) 2539 goto submit_and_realloc; 2540 2541 inc_cache_count(F2FS_I_SB(inode), F2FS_RD_DATA); 2542 f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO, 2543 F2FS_BLKSIZE(F2FS_I_SB(inode))); 2544 *last_block_in_bio = block_nr; 2545 out: 2546 *bio_ret = bio; 2547 return ret; 2548 } 2549 2550 #ifdef CONFIG_F2FS_FS_COMPRESSION 2551 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret, 2552 unsigned nr_pages, sector_t *last_block_in_bio, 2553 struct readahead_control *rac, bool for_write) 2554 { 2555 struct dnode_of_data dn; 2556 struct inode *inode = cc->inode; 2557 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2558 struct bio *bio = *bio_ret; 2559 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size; 2560 sector_t last_block_in_file; 2561 const unsigned int blocksize = F2FS_BLKSIZE(sbi); 2562 struct decompress_io_ctx *dic = NULL; 2563 struct extent_info ei = {}; 2564 bool from_dnode = true; 2565 int i; 2566 int ret = 0; 2567 2568 if (unlikely(f2fs_cp_error(sbi))) { 2569 ret = -EIO; 2570 from_dnode = false; 2571 goto out_put_dnode; 2572 } 2573 2574 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc)); 2575 2576 last_block_in_file = F2FS_BYTES_TO_BLK(sbi, 2577 f2fs_readpage_limit(inode) + 2578 blocksize - 1); 2579 2580 /* get rid of pages beyond EOF */ 2581 for (i = 0; i < cc->cluster_size; i++) { 2582 struct page *page = cc->rpages[i]; 2583 struct folio *folio; 2584 2585 if (!page) 2586 continue; 2587 2588 folio = page_folio(page); 2589 if ((sector_t)folio->index >= last_block_in_file) { 2590 folio_zero_segment(folio, 0, folio_size(folio)); 2591 if (!folio_test_uptodate(folio)) 2592 folio_mark_uptodate(folio); 2593 } else if (!folio_test_uptodate(folio)) { 2594 continue; 2595 } 2596 folio_unlock(folio); 2597 if (for_write) 2598 folio_put(folio); 2599 cc->rpages[i] = NULL; 2600 cc->nr_rpages--; 2601 } 2602 2603 /* we are done since all pages are beyond EOF */ 2604 if (f2fs_cluster_is_empty(cc)) 2605 goto out; 2606 2607 if (f2fs_lookup_read_extent_cache(inode, start_idx, &ei)) 2608 from_dnode = false; 2609 2610 if (!from_dnode) 2611 goto skip_reading_dnode; 2612 2613 set_new_dnode(&dn, inode, NULL, NULL, 0); 2614 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); 2615 if (ret) 2616 goto out; 2617 2618 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR); 2619 2620 skip_reading_dnode: 2621 for (i = 1; i < cc->cluster_size; i++) { 2622 block_t blkaddr; 2623 2624 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_entry, 2625 dn.ofs_in_node + i) : 2626 ei.blk + i - 1; 2627 2628 if (!__is_valid_data_blkaddr(blkaddr)) 2629 break; 2630 2631 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) { 2632 ret = -EFAULT; 2633 goto out_put_dnode; 2634 } 2635 cc->nr_cpages++; 2636 2637 if (!from_dnode && i >= ei.c_len) 2638 break; 2639 } 2640 2641 /* nothing to decompress */ 2642 if (cc->nr_cpages == 0) { 2643 ret = 0; 2644 goto out_put_dnode; 2645 } 2646 2647 dic = f2fs_alloc_dic(cc); 2648 if (IS_ERR(dic)) { 2649 ret = PTR_ERR(dic); 2650 goto out_put_dnode; 2651 } 2652 2653 for (i = 0; i < cc->nr_cpages; i++) { 2654 struct folio *folio = page_folio(dic->cpages[i]); 2655 block_t blkaddr; 2656 struct bio_post_read_ctx *ctx; 2657 2658 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_entry, 2659 dn.ofs_in_node + i + 1) : 2660 ei.blk + i; 2661 2662 f2fs_wait_on_block_writeback(inode, blkaddr); 2663 2664 if (f2fs_load_compressed_folio(sbi, folio, blkaddr)) { 2665 if (atomic_dec_and_test(&dic->remaining_pages)) { 2666 f2fs_decompress_cluster(dic, true); 2667 break; 2668 } 2669 continue; 2670 } 2671 2672 if (bio && (!page_is_mergeable(sbi, bio, 2673 *last_block_in_bio, blkaddr) || 2674 !f2fs_crypt_mergeable_bio(bio, inode, folio->index, NULL))) { 2675 submit_and_realloc: 2676 f2fs_submit_read_bio(sbi, bio, DATA); 2677 bio = NULL; 2678 } 2679 2680 if (!bio) 2681 bio = f2fs_grab_read_bio(inode, cc->vi, blkaddr, 2682 nr_pages - i, 2683 f2fs_ra_op_flags(rac), 2684 folio->index, for_write); 2685 2686 if (!bio_add_folio(bio, folio, blocksize, 0)) 2687 goto submit_and_realloc; 2688 2689 ctx = get_post_read_ctx(bio); 2690 ctx->enabled_steps |= STEP_DECOMPRESS; 2691 refcount_inc(&dic->refcnt); 2692 2693 inc_cache_count(sbi, F2FS_RD_DATA); 2694 f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, 2695 F2FS_BLKSIZE(sbi)); 2696 *last_block_in_bio = blkaddr; 2697 } 2698 2699 if (from_dnode) 2700 f2fs_put_dnode(&dn); 2701 2702 *bio_ret = bio; 2703 return 0; 2704 2705 out_put_dnode: 2706 if (from_dnode) 2707 f2fs_put_dnode(&dn); 2708 out: 2709 for (i = 0; i < cc->cluster_size; i++) { 2710 if (cc->rpages[i]) { 2711 ClearPageUptodate(cc->rpages[i]); 2712 unlock_page(cc->rpages[i]); 2713 } 2714 } 2715 *bio_ret = bio; 2716 return ret; 2717 } 2718 #endif 2719 2720 static struct f2fs_folio_state *ffs_find_or_alloc(struct folio *folio) 2721 { 2722 struct f2fs_folio_state *ffs = folio->private; 2723 2724 if (ffs) 2725 return ffs; 2726 2727 ffs = f2fs_kmem_cache_alloc(ffs_entry_slab, 2728 GFP_NOIO | __GFP_ZERO, true, NULL); 2729 2730 spin_lock_init(&ffs->state_lock); 2731 folio_attach_private(folio, ffs); 2732 return ffs; 2733 } 2734 2735 static void ffs_detach_free(struct folio *folio) 2736 { 2737 struct f2fs_folio_state *ffs; 2738 2739 if (!folio_test_large(folio)) { 2740 folio_detach_private(folio); 2741 return; 2742 } 2743 2744 ffs = folio_detach_private(folio); 2745 if (!ffs) 2746 return; 2747 2748 WARN_ON_ONCE(ffs->read_pages_pending != 0); 2749 kmem_cache_free(ffs_entry_slab, ffs); 2750 } 2751 2752 static int f2fs_read_data_large_folio(struct inode *inode, 2753 struct fsverity_info *vi, 2754 struct readahead_control *rac, struct folio *folio) 2755 { 2756 struct bio *bio = NULL; 2757 sector_t last_block_in_bio = 0; 2758 struct f2fs_map_blocks map = {0, }; 2759 pgoff_t index, offset, next_pgofs = 0; 2760 unsigned max_nr_pages = rac ? readahead_count(rac) : 2761 folio_nr_pages(folio); 2762 unsigned nrpages; 2763 struct f2fs_folio_state *ffs; 2764 int ret = 0; 2765 bool folio_in_bio = false; 2766 2767 if (!IS_IMMUTABLE(inode) || f2fs_compressed_file(inode)) { 2768 if (folio) 2769 folio_unlock(folio); 2770 return -EOPNOTSUPP; 2771 } 2772 2773 map.m_seg_type = NO_CHECK_TYPE; 2774 2775 if (rac) 2776 folio = readahead_folio(rac); 2777 next_folio: 2778 if (!folio) 2779 goto out; 2780 2781 f2fs_update_read_folio_count(F2FS_I_SB(inode), folio); 2782 2783 folio_in_bio = false; 2784 index = folio->index; 2785 offset = 0; 2786 ffs = NULL; 2787 nrpages = folio_nr_pages(folio); 2788 2789 for (; nrpages; nrpages--, max_nr_pages--, index++, offset++) { 2790 sector_t block_nr; 2791 /* 2792 * Map blocks using the previous result first. 2793 */ 2794 if (map.m_flags & F2FS_MAP_MAPPED) { 2795 if (index > map.m_lblk && 2796 index < (map.m_lblk + map.m_len)) 2797 goto got_it; 2798 } else if (index < next_pgofs) { 2799 /* hole case */ 2800 goto got_it; 2801 } 2802 2803 /* 2804 * Then do more f2fs_map_blocks() calls until we are 2805 * done with this page. 2806 */ 2807 memset(&map, 0, sizeof(map)); 2808 map.m_next_pgofs = &next_pgofs; 2809 map.m_seg_type = NO_CHECK_TYPE; 2810 map.m_lblk = index; 2811 map.m_len = max_nr_pages; 2812 2813 ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT); 2814 if (ret) 2815 goto err_out; 2816 got_it: 2817 if ((map.m_flags & F2FS_MAP_MAPPED)) { 2818 block_nr = map.m_pblk + index - map.m_lblk; 2819 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr, 2820 DATA_GENERIC_ENHANCE_READ)) { 2821 ret = -EFSCORRUPTED; 2822 goto err_out; 2823 } 2824 } else { 2825 size_t page_offset = offset << PAGE_SHIFT; 2826 folio_zero_range(folio, page_offset, PAGE_SIZE); 2827 if (vi && !fsverity_verify_blocks(vi, folio, PAGE_SIZE, page_offset)) { 2828 ret = -EIO; 2829 goto err_out; 2830 } 2831 continue; 2832 } 2833 2834 /* We must increment read_pages_pending before possible BIOs submitting 2835 * to prevent from premature folio_end_read() call on folio 2836 */ 2837 if (folio_test_large(folio)) { 2838 ffs = ffs_find_or_alloc(folio); 2839 2840 /* set the bitmap to wait */ 2841 spin_lock_irq(&ffs->state_lock); 2842 ffs->read_pages_pending++; 2843 spin_unlock_irq(&ffs->state_lock); 2844 } 2845 2846 /* 2847 * This page will go to BIO. Do we need to send this 2848 * BIO off first? 2849 */ 2850 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio, 2851 last_block_in_bio, block_nr) || 2852 !f2fs_crypt_mergeable_bio(bio, inode, index, NULL))) { 2853 submit_and_realloc: 2854 f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); 2855 bio = NULL; 2856 } 2857 if (bio == NULL) 2858 bio = f2fs_grab_read_bio(inode, vi, 2859 block_nr, max_nr_pages, 2860 f2fs_ra_op_flags(rac), 2861 index, false); 2862 2863 /* 2864 * If the page is under writeback, we need to wait for 2865 * its completion to see the correct decrypted data. 2866 */ 2867 f2fs_wait_on_block_writeback(inode, block_nr); 2868 2869 if (!bio_add_folio(bio, folio, F2FS_BLKSIZE(F2FS_I_SB(inode)), 2870 offset << PAGE_SHIFT)) 2871 goto submit_and_realloc; 2872 2873 folio_in_bio = true; 2874 inc_cache_count(F2FS_I_SB(inode), F2FS_RD_DATA); 2875 f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO, 2876 F2FS_BLKSIZE(F2FS_I_SB(inode))); 2877 last_block_in_bio = block_nr; 2878 } 2879 trace_f2fs_read_folio(folio, DATA); 2880 err_out: 2881 if (!folio_in_bio) 2882 folio_end_read(folio, !ret); 2883 if (ret) 2884 goto out; 2885 if (rac) { 2886 folio = readahead_folio(rac); 2887 goto next_folio; 2888 } 2889 out: 2890 f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); 2891 if (ret && folio_in_bio) { 2892 /* Wait bios and clear uptodate. */ 2893 folio_lock(folio); 2894 folio_clear_uptodate(folio); 2895 folio_unlock(folio); 2896 } 2897 return ret; 2898 } 2899 2900 /* 2901 * This function was originally taken from fs/mpage.c, and customized for f2fs. 2902 * Major change was from block_size == page_size in f2fs by default. 2903 */ 2904 static int f2fs_mpage_readpages(struct inode *inode, struct fsverity_info *vi, 2905 struct readahead_control *rac, struct folio *folio) 2906 { 2907 struct bio *bio = NULL; 2908 sector_t last_block_in_bio = 0; 2909 struct f2fs_map_blocks map; 2910 #ifdef CONFIG_F2FS_FS_COMPRESSION 2911 struct compress_ctx cc = { 2912 .inode = inode, 2913 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 2914 .cluster_size = F2FS_I(inode)->i_cluster_size, 2915 .cluster_idx = NULL_CLUSTER, 2916 .rpages = NULL, 2917 .cpages = NULL, 2918 .nr_rpages = 0, 2919 .nr_cpages = 0, 2920 }; 2921 pgoff_t nc_cluster_idx = NULL_CLUSTER; 2922 pgoff_t index; 2923 #endif 2924 pgoff_t next_pgofs = 0; 2925 unsigned nr_pages = rac ? readahead_count(rac) : 1; 2926 struct address_space *mapping = rac ? rac->mapping : folio->mapping; 2927 unsigned max_nr_pages = nr_pages; 2928 int ret = 0; 2929 2930 if (mapping_large_folio_support(mapping)) 2931 return f2fs_read_data_large_folio(inode, vi, rac, folio); 2932 2933 #ifdef CONFIG_F2FS_FS_COMPRESSION 2934 if (f2fs_compressed_file(inode)) { 2935 index = rac ? readahead_index(rac) : folio->index; 2936 max_nr_pages = round_up(index + nr_pages, cc.cluster_size) - 2937 round_down(index, cc.cluster_size); 2938 } 2939 #endif 2940 2941 map.m_pblk = 0; 2942 map.m_lblk = 0; 2943 map.m_len = 0; 2944 map.m_flags = 0; 2945 map.m_next_pgofs = &next_pgofs; 2946 map.m_next_extent = NULL; 2947 map.m_seg_type = NO_CHECK_TYPE; 2948 map.m_may_create = false; 2949 2950 for (; nr_pages; nr_pages--) { 2951 if (rac) { 2952 folio = readahead_folio(rac); 2953 prefetchw(&folio->flags); 2954 } 2955 2956 f2fs_update_read_folio_count(F2FS_I_SB(inode), folio); 2957 2958 #ifdef CONFIG_F2FS_FS_COMPRESSION 2959 index = folio->index; 2960 2961 if (!f2fs_compressed_file(inode)) 2962 goto read_single_page; 2963 2964 /* there are remained compressed pages, submit them */ 2965 if (!f2fs_cluster_can_merge_page(&cc, index)) { 2966 cc.vi = vi; 2967 ret = f2fs_read_multi_pages(&cc, &bio, 2968 max_nr_pages, 2969 &last_block_in_bio, 2970 rac, false); 2971 f2fs_destroy_compress_ctx(&cc, false); 2972 if (ret) 2973 goto set_error_page; 2974 } 2975 if (cc.cluster_idx == NULL_CLUSTER) { 2976 if (nc_cluster_idx == index >> cc.log_cluster_size) 2977 goto read_single_page; 2978 2979 ret = f2fs_is_compressed_cluster(inode, index); 2980 if (ret < 0) 2981 goto set_error_page; 2982 else if (!ret) { 2983 nc_cluster_idx = 2984 index >> cc.log_cluster_size; 2985 goto read_single_page; 2986 } 2987 2988 nc_cluster_idx = NULL_CLUSTER; 2989 } 2990 ret = f2fs_init_compress_ctx(&cc); 2991 if (ret) 2992 goto set_error_page; 2993 2994 f2fs_compress_ctx_add_page(&cc, folio); 2995 2996 goto next_page; 2997 read_single_page: 2998 #endif 2999 3000 ret = f2fs_read_single_page(inode, vi, folio, max_nr_pages, 3001 &map, &bio, &last_block_in_bio, 3002 rac); 3003 if (ret) { 3004 #ifdef CONFIG_F2FS_FS_COMPRESSION 3005 set_error_page: 3006 #endif 3007 folio_zero_segment(folio, 0, folio_size(folio)); 3008 folio_unlock(folio); 3009 } 3010 #ifdef CONFIG_F2FS_FS_COMPRESSION 3011 next_page: 3012 #endif 3013 3014 #ifdef CONFIG_F2FS_FS_COMPRESSION 3015 if (f2fs_compressed_file(inode)) { 3016 /* last page */ 3017 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) { 3018 cc.vi = vi; 3019 ret = f2fs_read_multi_pages(&cc, &bio, 3020 max_nr_pages, 3021 &last_block_in_bio, 3022 rac, false); 3023 f2fs_destroy_compress_ctx(&cc, false); 3024 } 3025 } 3026 #endif 3027 } 3028 f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); 3029 return ret; 3030 } 3031 3032 static int f2fs_read_data_folio(struct file *file, struct folio *folio) 3033 { 3034 struct inode *inode = folio->mapping->host; 3035 struct fsverity_info *vi = NULL; 3036 int ret; 3037 3038 trace_f2fs_readpage(folio, DATA); 3039 3040 if (!f2fs_is_compress_backend_ready(inode)) { 3041 folio_unlock(folio); 3042 return -EOPNOTSUPP; 3043 } 3044 3045 /* If the file has inline data, try to read it directly */ 3046 if (f2fs_has_inline_data(inode)) { 3047 ret = f2fs_read_inline_data(inode, folio); 3048 if (ret != -EAGAIN) 3049 return ret; 3050 } 3051 3052 vi = f2fs_need_verity(inode, folio->index); 3053 if (vi) 3054 fsverity_readahead(vi, folio->index, folio_nr_pages(folio)); 3055 return f2fs_mpage_readpages(inode, vi, NULL, folio); 3056 } 3057 3058 static void f2fs_readahead(struct readahead_control *rac) 3059 { 3060 struct inode *inode = rac->mapping->host; 3061 struct fsverity_info *vi = NULL; 3062 3063 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac)); 3064 3065 if (!f2fs_is_compress_backend_ready(inode)) 3066 return; 3067 3068 /* If the file has inline data, skip readahead */ 3069 if (f2fs_has_inline_data(inode)) 3070 return; 3071 3072 vi = f2fs_need_verity(inode, readahead_index(rac)); 3073 if (vi) 3074 fsverity_readahead(vi, readahead_index(rac), 3075 readahead_count(rac)); 3076 f2fs_mpage_readpages(inode, vi, rac, NULL); 3077 } 3078 3079 static inline bool check_inplace_update_policy(struct inode *inode, 3080 struct f2fs_io_info *fio) 3081 { 3082 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3083 3084 if (IS_F2FS_IPU_HONOR_OPU_WRITE(sbi) && 3085 is_inode_flag_set(inode, FI_OPU_WRITE)) 3086 return false; 3087 if (IS_F2FS_IPU_FORCE(sbi)) 3088 return true; 3089 if (IS_F2FS_IPU_SSR(sbi) && f2fs_need_SSR(sbi)) 3090 return true; 3091 if (IS_F2FS_IPU_UTIL(sbi) && utilization(sbi) > SM_I(sbi)->min_ipu_util) 3092 return true; 3093 if (IS_F2FS_IPU_SSR_UTIL(sbi) && f2fs_need_SSR(sbi) && 3094 utilization(sbi) > SM_I(sbi)->min_ipu_util) 3095 return true; 3096 3097 /* 3098 * IPU for rewrite async pages 3099 */ 3100 if (IS_F2FS_IPU_ASYNC(sbi) && fio && fio->op == REQ_OP_WRITE && 3101 !(fio->op_flags & REQ_SYNC) && !IS_ENCRYPTED(inode)) 3102 return true; 3103 3104 /* this is only set during fdatasync */ 3105 if (IS_F2FS_IPU_FSYNC(sbi) && is_inode_flag_set(inode, FI_NEED_IPU)) 3106 return true; 3107 3108 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) && 3109 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) 3110 return true; 3111 3112 return false; 3113 } 3114 3115 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio) 3116 { 3117 /* swap file is migrating in aligned write mode */ 3118 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE)) 3119 return false; 3120 3121 if (f2fs_is_pinned_file(inode)) 3122 return true; 3123 3124 /* if this is cold file, we should overwrite to avoid fragmentation */ 3125 if (file_is_cold(inode) && !is_inode_flag_set(inode, FI_OPU_WRITE)) 3126 return true; 3127 3128 return check_inplace_update_policy(inode, fio); 3129 } 3130 3131 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio) 3132 { 3133 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3134 3135 /* The below cases were checked when setting it. */ 3136 if (f2fs_is_pinned_file(inode)) 3137 return false; 3138 if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 3139 return true; 3140 if (f2fs_lfs_mode(sbi)) 3141 return true; 3142 if (S_ISDIR(inode->i_mode)) 3143 return true; 3144 if (IS_NOQUOTA(inode)) 3145 return true; 3146 if (f2fs_used_in_atomic_write(inode)) 3147 return true; 3148 /* 3149 * rewrite low ratio compress data w/ OPU mode to avoid fragmentation. 3150 * If IO comes from compressed write path and fallback to raw write, 3151 * force out‑place to prevent metadata‑data inconsistency. 3152 */ 3153 if (fio && f2fs_compressed_file(inode)) 3154 return true; 3155 3156 /* swap file is migrating in aligned write mode */ 3157 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE)) 3158 return true; 3159 3160 if (is_inode_flag_set(inode, FI_OPU_WRITE)) 3161 return true; 3162 3163 if (fio) { 3164 if (folio_test_f2fs_gcing(fio->folio)) 3165 return true; 3166 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) && 3167 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) 3168 return true; 3169 } 3170 return false; 3171 } 3172 3173 static inline bool need_inplace_update(struct f2fs_io_info *fio) 3174 { 3175 struct inode *inode = fio_inode(fio); 3176 3177 if (f2fs_should_update_outplace(inode, fio)) 3178 return false; 3179 3180 return f2fs_should_update_inplace(inode, fio); 3181 } 3182 3183 int f2fs_do_write_data_page(struct f2fs_io_info *fio) 3184 { 3185 struct folio *folio = fio->folio; 3186 struct inode *inode = folio->mapping->host; 3187 struct dnode_of_data dn; 3188 struct node_info ni; 3189 struct f2fs_lock_context lc; 3190 bool ipu_force = false; 3191 bool atomic_commit; 3192 int err = 0; 3193 3194 /* Use COW inode to make dnode_of_data for atomic write */ 3195 atomic_commit = f2fs_is_atomic_file(inode) && 3196 folio_test_f2fs_atomic(folio); 3197 if (atomic_commit) 3198 set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0); 3199 else 3200 set_new_dnode(&dn, inode, NULL, NULL, 0); 3201 3202 if (need_inplace_update(fio) && 3203 f2fs_lookup_read_extent_cache_block(inode, folio->index, 3204 &fio->old_blkaddr)) { 3205 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, 3206 DATA_GENERIC_ENHANCE)) 3207 return -EFSCORRUPTED; 3208 3209 ipu_force = true; 3210 fio->need_lock = LOCK_DONE; 3211 goto got_it; 3212 } 3213 3214 if (is_sbi_flag_set(fio->sbi, SBI_ENABLE_CHECKPOINT) && 3215 time_to_inject(fio->sbi, FAULT_SKIP_WRITE)) 3216 return -EINVAL; 3217 3218 /* Deadlock due to between page->lock and f2fs_lock_op */ 3219 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi, &lc)) 3220 return -EAGAIN; 3221 3222 err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE); 3223 if (err) 3224 goto out; 3225 3226 fio->old_blkaddr = dn.data_blkaddr; 3227 3228 /* This page is already truncated */ 3229 if (fio->old_blkaddr == NULL_ADDR) { 3230 folio_clear_uptodate(folio); 3231 folio_clear_f2fs_gcing(folio); 3232 goto out_writepage; 3233 } 3234 got_it: 3235 if (__is_valid_data_blkaddr(fio->old_blkaddr) && 3236 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, 3237 DATA_GENERIC_ENHANCE)) { 3238 err = -EFSCORRUPTED; 3239 goto out_writepage; 3240 } 3241 3242 /* wait for GCed page writeback via generic cache */ 3243 if (fio->meta_gc) 3244 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr); 3245 3246 /* 3247 * If current allocation needs SSR, 3248 * it had better in-place writes for updated data. 3249 */ 3250 if (ipu_force || 3251 (__is_valid_data_blkaddr(fio->old_blkaddr) && 3252 need_inplace_update(fio))) { 3253 folio_start_writeback(folio); 3254 f2fs_put_dnode(&dn); 3255 if (fio->need_lock == LOCK_REQ) 3256 f2fs_unlock_op(fio->sbi, &lc); 3257 err = f2fs_inplace_write_data(fio); 3258 if (err) 3259 folio_end_writeback(folio); 3260 else 3261 set_inode_flag(inode, FI_UPDATE_WRITE); 3262 trace_f2fs_do_write_data_page(folio, IPU); 3263 return err; 3264 } 3265 3266 if (fio->need_lock == LOCK_RETRY) { 3267 if (!f2fs_trylock_op(fio->sbi, &lc)) { 3268 err = -EAGAIN; 3269 goto out_writepage; 3270 } 3271 fio->need_lock = LOCK_REQ; 3272 } 3273 3274 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false); 3275 if (err) 3276 goto out_writepage; 3277 3278 fio->version = ni.version; 3279 3280 folio_start_writeback(folio); 3281 3282 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR) 3283 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false); 3284 3285 /* LFS mode write path */ 3286 f2fs_outplace_write_data(&dn, fio); 3287 trace_f2fs_do_write_data_page(folio, OPU); 3288 set_inode_flag(inode, FI_APPEND_WRITE); 3289 if (atomic_commit) 3290 folio_clear_f2fs_atomic(folio); 3291 out_writepage: 3292 f2fs_put_dnode(&dn); 3293 out: 3294 if (fio->need_lock == LOCK_REQ) 3295 f2fs_unlock_op(fio->sbi, &lc); 3296 return err; 3297 } 3298 3299 int f2fs_write_single_data_page(struct folio *folio, int *submitted, 3300 struct bio **bio, 3301 sector_t *last_block, 3302 struct writeback_control *wbc, 3303 enum iostat_type io_type, 3304 int compr_blocks, 3305 bool allow_balance) 3306 { 3307 struct inode *inode = folio->mapping->host; 3308 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3309 loff_t i_size = i_size_read(inode); 3310 const pgoff_t end_index = ((unsigned long long)i_size) 3311 >> PAGE_SHIFT; 3312 loff_t psize = (loff_t)(folio->index + 1) << PAGE_SHIFT; 3313 unsigned offset = 0; 3314 bool need_balance_fs = false; 3315 bool quota_inode = IS_NOQUOTA(inode); 3316 int err = 0; 3317 struct f2fs_io_info fio = { 3318 .sbi = sbi, 3319 .ino = inode->i_ino, 3320 .type = DATA, 3321 .op = REQ_OP_WRITE, 3322 .op_flags = wbc_to_write_flags(wbc), 3323 .old_blkaddr = NULL_ADDR, 3324 .folio = folio, 3325 .encrypted_page = NULL, 3326 .submitted = 0, 3327 .compr_blocks = compr_blocks, 3328 .need_lock = compr_blocks ? LOCK_DONE : LOCK_RETRY, 3329 .meta_gc = f2fs_meta_inode_gc_required(inode) ? 1 : 0, 3330 .io_type = io_type, 3331 .io_wbc = wbc, 3332 .bio = bio, 3333 .last_block = last_block, 3334 }; 3335 3336 trace_f2fs_writepage(folio, DATA); 3337 3338 /* we should bypass data pages to proceed the kworker jobs */ 3339 if (unlikely(f2fs_cp_error(sbi))) { 3340 mapping_set_error(folio->mapping, -EIO); 3341 /* 3342 * don't drop any dirty dentry pages for keeping lastest 3343 * directory structure. 3344 */ 3345 if (S_ISDIR(inode->i_mode) && 3346 !is_sbi_flag_set(sbi, SBI_IS_CLOSE)) 3347 goto redirty_out; 3348 3349 /* keep data pages in remount-ro mode */ 3350 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY) 3351 goto redirty_out; 3352 goto out; 3353 } 3354 3355 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 3356 goto redirty_out; 3357 3358 if (folio->index < end_index || 3359 f2fs_verity_in_progress(inode) || 3360 compr_blocks) 3361 goto write; 3362 3363 /* 3364 * If the offset is out-of-range of file size, 3365 * this page does not have to be written to disk. 3366 */ 3367 offset = i_size & (PAGE_SIZE - 1); 3368 if ((folio->index >= end_index + 1) || !offset) 3369 goto out; 3370 3371 folio_zero_segment(folio, offset, folio_size(folio)); 3372 write: 3373 /* Dentry/quota blocks are controlled by checkpoint */ 3374 if (S_ISDIR(inode->i_mode) || quota_inode) { 3375 struct f2fs_lock_context lc; 3376 3377 /* 3378 * We need to wait for node_write to avoid block allocation during 3379 * checkpoint. This can only happen to quota writes which can cause 3380 * the below discard race condition. 3381 */ 3382 if (quota_inode) 3383 f2fs_down_read_trace(&sbi->node_write, &lc); 3384 3385 fio.need_lock = LOCK_DONE; 3386 err = f2fs_do_write_data_page(&fio); 3387 3388 if (quota_inode) 3389 f2fs_up_read_trace(&sbi->node_write, &lc); 3390 3391 goto done; 3392 } 3393 3394 need_balance_fs = true; 3395 err = -EAGAIN; 3396 if (f2fs_has_inline_data(inode)) { 3397 err = f2fs_write_inline_data(inode, folio); 3398 if (!err) 3399 goto out; 3400 } 3401 3402 if (err == -EAGAIN) { 3403 err = f2fs_do_write_data_page(&fio); 3404 if (err == -EAGAIN) { 3405 f2fs_bug_on(sbi, compr_blocks); 3406 fio.need_lock = LOCK_REQ; 3407 err = f2fs_do_write_data_page(&fio); 3408 } 3409 } 3410 3411 if (err) { 3412 file_set_keep_isize(inode); 3413 } else { 3414 spin_lock(&F2FS_I(inode)->i_size_lock); 3415 if (F2FS_I(inode)->last_disk_size < psize) 3416 F2FS_I(inode)->last_disk_size = psize; 3417 spin_unlock(&F2FS_I(inode)->i_size_lock); 3418 } 3419 3420 done: 3421 if (err && err != -ENOENT) 3422 goto redirty_out; 3423 3424 out: 3425 inode_dec_dirty_pages(inode); 3426 if (err) { 3427 folio_clear_uptodate(folio); 3428 folio_clear_f2fs_gcing(folio); 3429 } 3430 folio_unlock(folio); 3431 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) && 3432 !F2FS_I(inode)->wb_task && allow_balance) 3433 f2fs_balance_fs(sbi, need_balance_fs); 3434 3435 if (unlikely(f2fs_cp_error(sbi))) { 3436 f2fs_submit_merged_write(sbi, DATA); 3437 if (bio && *bio) 3438 f2fs_submit_merged_ipu_write(sbi, bio, NULL); 3439 submitted = NULL; 3440 } 3441 3442 if (submitted) 3443 *submitted = fio.submitted; 3444 3445 return 0; 3446 3447 redirty_out: 3448 folio_redirty_for_writepage(wbc, folio); 3449 /* 3450 * pageout() in MM translates EAGAIN, so calls handle_write_error() 3451 * -> mapping_set_error() -> set_bit(AS_EIO, ...). 3452 * file_write_and_wait_range() will see EIO error, which is critical 3453 * to return value of fsync() followed by atomic_write failure to user. 3454 */ 3455 folio_unlock(folio); 3456 if (!err) 3457 return 1; 3458 return err; 3459 } 3460 3461 /* 3462 * This function was copied from write_cache_pages from mm/page-writeback.c. 3463 * The major change is making write step of cold data page separately from 3464 * warm/hot data page. 3465 */ 3466 static int f2fs_write_cache_pages(struct address_space *mapping, 3467 struct writeback_control *wbc, 3468 enum iostat_type io_type) 3469 { 3470 int ret = 0; 3471 int done = 0, retry = 0; 3472 struct page *pages_local[F2FS_ONSTACK_PAGES]; 3473 struct page **pages = pages_local; 3474 struct folio_batch fbatch; 3475 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 3476 struct bio *bio = NULL; 3477 sector_t last_block; 3478 #ifdef CONFIG_F2FS_FS_COMPRESSION 3479 struct inode *inode = mapping->host; 3480 struct compress_ctx cc = { 3481 .inode = inode, 3482 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 3483 .cluster_size = F2FS_I(inode)->i_cluster_size, 3484 .cluster_idx = NULL_CLUSTER, 3485 .rpages = NULL, 3486 .nr_rpages = 0, 3487 .cpages = NULL, 3488 .valid_nr_cpages = 0, 3489 .rbuf = NULL, 3490 .cbuf = NULL, 3491 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size, 3492 .private = NULL, 3493 }; 3494 #endif 3495 int nr_folios, p, idx; 3496 int nr_pages; 3497 unsigned int max_pages = F2FS_ONSTACK_PAGES; 3498 pgoff_t index; 3499 pgoff_t end; /* Inclusive */ 3500 pgoff_t done_index; 3501 int range_whole = 0; 3502 xa_mark_t tag; 3503 int nwritten = 0; 3504 int submitted = 0; 3505 int i; 3506 3507 #ifdef CONFIG_F2FS_FS_COMPRESSION 3508 if (f2fs_compressed_file(inode) && 3509 1 << cc.log_cluster_size > F2FS_ONSTACK_PAGES) { 3510 pages = f2fs_kzalloc(sbi, sizeof(struct page *) << 3511 cc.log_cluster_size, GFP_NOFS | __GFP_NOFAIL); 3512 max_pages = 1 << cc.log_cluster_size; 3513 } 3514 #endif 3515 3516 folio_batch_init(&fbatch); 3517 3518 if (get_dirty_pages(mapping->host) <= 3519 SM_I(F2FS_M_SB(mapping))->min_hot_blocks) 3520 set_inode_flag(mapping->host, FI_HOT_DATA); 3521 else 3522 clear_inode_flag(mapping->host, FI_HOT_DATA); 3523 3524 if (wbc->range_cyclic) { 3525 index = mapping->writeback_index; /* prev offset */ 3526 end = -1; 3527 } else { 3528 index = wbc->range_start >> PAGE_SHIFT; 3529 end = wbc->range_end >> PAGE_SHIFT; 3530 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 3531 range_whole = 1; 3532 } 3533 tag = wbc_to_tag(wbc); 3534 retry: 3535 retry = 0; 3536 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 3537 tag_pages_for_writeback(mapping, index, end); 3538 done_index = index; 3539 while (!done && !retry && (index <= end)) { 3540 nr_pages = 0; 3541 again: 3542 nr_folios = filemap_get_folios_tag(mapping, &index, end, 3543 tag, &fbatch); 3544 if (nr_folios == 0) { 3545 if (nr_pages) 3546 goto write; 3547 break; 3548 } 3549 3550 for (i = 0; i < nr_folios; i++) { 3551 struct folio *folio = fbatch.folios[i]; 3552 3553 idx = 0; 3554 p = folio_nr_pages(folio); 3555 add_more: 3556 pages[nr_pages] = folio_page(folio, idx); 3557 folio_get(folio); 3558 if (++nr_pages == max_pages) { 3559 index = folio->index + idx + 1; 3560 folio_batch_release(&fbatch); 3561 goto write; 3562 } 3563 if (++idx < p) 3564 goto add_more; 3565 } 3566 folio_batch_release(&fbatch); 3567 goto again; 3568 write: 3569 for (i = 0; i < nr_pages; i++) { 3570 struct page *page = pages[i]; 3571 struct folio *folio = page_folio(page); 3572 bool need_readd; 3573 readd: 3574 need_readd = false; 3575 #ifdef CONFIG_F2FS_FS_COMPRESSION 3576 if (f2fs_compressed_file(inode)) { 3577 void *fsdata = NULL; 3578 struct page *pagep; 3579 int ret2; 3580 3581 ret = f2fs_init_compress_ctx(&cc); 3582 if (ret) { 3583 done = 1; 3584 break; 3585 } 3586 3587 if (!f2fs_cluster_can_merge_page(&cc, 3588 folio->index)) { 3589 ret = f2fs_write_multi_pages(&cc, 3590 &submitted, wbc, io_type); 3591 if (!ret) 3592 need_readd = true; 3593 goto result; 3594 } 3595 3596 if (unlikely(f2fs_cp_error(sbi))) 3597 goto lock_folio; 3598 3599 if (!f2fs_cluster_is_empty(&cc)) 3600 goto lock_folio; 3601 3602 if (f2fs_all_cluster_page_ready(&cc, 3603 pages, i, nr_pages, true)) 3604 goto lock_folio; 3605 3606 ret2 = f2fs_prepare_compress_overwrite( 3607 inode, &pagep, 3608 folio->index, &fsdata); 3609 if (ret2 < 0) { 3610 ret = ret2; 3611 done = 1; 3612 break; 3613 } else if (ret2 && 3614 (!f2fs_compress_write_end(inode, 3615 fsdata, folio->index, 1) || 3616 !f2fs_all_cluster_page_ready(&cc, 3617 pages, i, nr_pages, 3618 false))) { 3619 retry = 1; 3620 break; 3621 } 3622 } 3623 #endif 3624 /* give a priority to WB_SYNC threads */ 3625 if (atomic_read(&sbi->wb_sync_req[DATA]) && 3626 wbc->sync_mode == WB_SYNC_NONE) { 3627 done = 1; 3628 break; 3629 } 3630 #ifdef CONFIG_F2FS_FS_COMPRESSION 3631 lock_folio: 3632 #endif 3633 done_index = folio->index; 3634 retry_write: 3635 folio_lock(folio); 3636 3637 if (unlikely(folio->mapping != mapping)) { 3638 continue_unlock: 3639 folio_unlock(folio); 3640 continue; 3641 } 3642 3643 if (!folio_test_dirty(folio)) { 3644 /* someone wrote it for us */ 3645 goto continue_unlock; 3646 } 3647 3648 if (folio_test_writeback(folio)) { 3649 if (wbc->sync_mode == WB_SYNC_NONE) 3650 goto continue_unlock; 3651 f2fs_folio_wait_writeback(folio, true, true); 3652 } 3653 3654 if (!folio_clear_dirty_for_io(folio)) 3655 goto continue_unlock; 3656 3657 #ifdef CONFIG_F2FS_FS_COMPRESSION 3658 if (f2fs_compressed_file(inode)) { 3659 folio_get(folio); 3660 f2fs_compress_ctx_add_page(&cc, folio); 3661 continue; 3662 } 3663 #endif 3664 submitted = 0; 3665 ret = f2fs_write_single_data_page(folio, 3666 &submitted, &bio, &last_block, 3667 wbc, io_type, 0, true); 3668 #ifdef CONFIG_F2FS_FS_COMPRESSION 3669 result: 3670 #endif 3671 nwritten += submitted; 3672 wbc->nr_to_write -= submitted; 3673 3674 if (unlikely(ret)) { 3675 /* 3676 * keep nr_to_write, since vfs uses this to 3677 * get # of written pages. 3678 */ 3679 if (ret == 1) { 3680 ret = 0; 3681 goto next; 3682 } else if (ret == -EAGAIN) { 3683 ret = 0; 3684 if (wbc->sync_mode == WB_SYNC_ALL) { 3685 f2fs_schedule_timeout( 3686 DEFAULT_SCHEDULE_TIMEOUT); 3687 goto retry_write; 3688 } 3689 goto next; 3690 } 3691 done_index = folio_next_index(folio); 3692 done = 1; 3693 break; 3694 } 3695 3696 if (wbc->nr_to_write <= 0 && 3697 wbc->sync_mode == WB_SYNC_NONE) { 3698 done = 1; 3699 break; 3700 } 3701 next: 3702 if (need_readd) 3703 goto readd; 3704 } 3705 release_pages(pages, nr_pages); 3706 cond_resched(); 3707 } 3708 #ifdef CONFIG_F2FS_FS_COMPRESSION 3709 /* flush remained pages in compress cluster */ 3710 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) { 3711 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type); 3712 nwritten += submitted; 3713 wbc->nr_to_write -= submitted; 3714 if (ret) { 3715 done = 1; 3716 retry = 0; 3717 } 3718 } 3719 if (f2fs_compressed_file(inode)) 3720 f2fs_destroy_compress_ctx(&cc, false); 3721 #endif 3722 if (retry) { 3723 index = 0; 3724 end = -1; 3725 goto retry; 3726 } 3727 if (wbc->range_cyclic && !done) 3728 done_index = 0; 3729 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 3730 mapping->writeback_index = done_index; 3731 3732 if (nwritten) 3733 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), 3734 mapping->host, NULL); 3735 /* submit cached bio of IPU write */ 3736 if (bio) 3737 f2fs_submit_merged_ipu_write(sbi, &bio, NULL); 3738 3739 #ifdef CONFIG_F2FS_FS_COMPRESSION 3740 if (pages != pages_local) 3741 kfree(pages); 3742 #endif 3743 3744 return ret; 3745 } 3746 3747 static inline bool __should_serialize_io(struct inode *inode, 3748 struct writeback_control *wbc) 3749 { 3750 /* to avoid deadlock in path of data flush */ 3751 if (F2FS_I(inode)->wb_task) 3752 return false; 3753 3754 if (!S_ISREG(inode->i_mode)) 3755 return false; 3756 if (IS_NOQUOTA(inode)) 3757 return false; 3758 3759 if (f2fs_is_pinned_file(inode)) 3760 return false; 3761 if (f2fs_need_compress_data(inode)) 3762 return true; 3763 if (wbc->sync_mode != WB_SYNC_ALL) 3764 return true; 3765 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks) 3766 return true; 3767 return false; 3768 } 3769 3770 static inline void account_writeback(struct inode *inode, bool inc) 3771 { 3772 if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) 3773 return; 3774 3775 f2fs_down_read(&F2FS_I(inode)->i_sem); 3776 if (inc) 3777 atomic_inc(&F2FS_I(inode)->writeback); 3778 else 3779 atomic_dec(&F2FS_I(inode)->writeback); 3780 f2fs_up_read(&F2FS_I(inode)->i_sem); 3781 } 3782 3783 static inline void update_skipped_write(struct f2fs_sb_info *sbi, 3784 struct writeback_control *wbc) 3785 { 3786 long skipped = wbc->pages_skipped; 3787 3788 if (is_sbi_flag_set(sbi, SBI_ENABLE_CHECKPOINT) && skipped && 3789 wbc->sync_mode == WB_SYNC_ALL) 3790 atomic_add(skipped, &sbi->nr_caches[F2FS_SKIPPED_WRITE]); 3791 } 3792 3793 static int __f2fs_write_data_pages(struct address_space *mapping, 3794 struct writeback_control *wbc, 3795 enum iostat_type io_type) 3796 { 3797 struct inode *inode = mapping->host; 3798 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3799 struct blk_plug plug; 3800 int ret; 3801 bool locked = false; 3802 3803 /* skip writing if there is no dirty page in this inode */ 3804 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) 3805 return 0; 3806 3807 /* during POR, we don't need to trigger writepage at all. */ 3808 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 3809 goto skip_write; 3810 3811 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) && 3812 wbc->sync_mode == WB_SYNC_NONE && 3813 get_dirty_pages(inode) < nr_caches_to_skip(sbi, DATA) && 3814 f2fs_available_free_memory(sbi, DIRTY_DENTS)) 3815 goto skip_write; 3816 3817 /* skip writing in file defragment preparing stage */ 3818 if (is_inode_flag_set(inode, FI_SKIP_WRITES)) 3819 goto skip_write; 3820 3821 trace_f2fs_writepages(mapping->host, wbc, DATA); 3822 3823 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */ 3824 if (wbc->sync_mode == WB_SYNC_ALL) 3825 atomic_inc(&sbi->wb_sync_req[DATA]); 3826 else if (atomic_read(&sbi->wb_sync_req[DATA])) { 3827 /* to avoid potential deadlock */ 3828 if (current->plug) 3829 blk_finish_plug(current->plug); 3830 goto skip_write; 3831 } 3832 3833 if (__should_serialize_io(inode, wbc)) { 3834 mutex_lock(&sbi->writepages); 3835 locked = true; 3836 } 3837 3838 account_writeback(inode, true); 3839 3840 blk_start_plug(&plug); 3841 ret = f2fs_write_cache_pages(mapping, wbc, io_type); 3842 blk_finish_plug(&plug); 3843 3844 account_writeback(inode, false); 3845 3846 if (locked) 3847 mutex_unlock(&sbi->writepages); 3848 3849 if (wbc->sync_mode == WB_SYNC_ALL) 3850 atomic_dec(&sbi->wb_sync_req[DATA]); 3851 /* 3852 * if some pages were truncated, we cannot guarantee its mapping->host 3853 * to detect pending bios. 3854 */ 3855 3856 f2fs_remove_dirty_inode(inode); 3857 3858 /* 3859 * f2fs_write_cache_pages() has retry logic for EAGAIN case which is 3860 * common when racing w/ checkpoint, so only update skipped write 3861 * when ret is non-zero. 3862 */ 3863 if (ret) 3864 update_skipped_write(sbi, wbc); 3865 return ret; 3866 3867 skip_write: 3868 wbc->pages_skipped += get_dirty_pages(inode); 3869 update_skipped_write(sbi, wbc); 3870 trace_f2fs_writepages(mapping->host, wbc, DATA); 3871 return 0; 3872 } 3873 3874 static int f2fs_write_data_pages(struct address_space *mapping, 3875 struct writeback_control *wbc) 3876 { 3877 struct inode *inode = mapping->host; 3878 3879 return __f2fs_write_data_pages(mapping, wbc, 3880 F2FS_I(inode)->cp_task == current ? 3881 FS_CP_DATA_IO : FS_DATA_IO); 3882 } 3883 3884 void f2fs_write_failed(struct inode *inode, loff_t to) 3885 { 3886 loff_t i_size = i_size_read(inode); 3887 3888 if (IS_NOQUOTA(inode)) 3889 return; 3890 3891 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */ 3892 if (to > i_size && !f2fs_verity_in_progress(inode)) { 3893 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 3894 filemap_invalidate_lock(inode->i_mapping); 3895 3896 truncate_pagecache(inode, i_size); 3897 f2fs_truncate_blocks(inode, i_size, true); 3898 3899 filemap_invalidate_unlock(inode->i_mapping); 3900 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 3901 } 3902 } 3903 3904 static int prepare_write_begin(struct f2fs_sb_info *sbi, 3905 struct folio *folio, loff_t pos, unsigned int len, 3906 block_t *blk_addr, bool *node_changed) 3907 { 3908 struct inode *inode = folio->mapping->host; 3909 pgoff_t index = folio->index; 3910 struct dnode_of_data dn; 3911 struct f2fs_lock_context lc; 3912 struct f2fs_cached_block *ientry; 3913 bool locked = false; 3914 int flag = F2FS_GET_BLOCK_PRE_AIO; 3915 int err = 0; 3916 3917 if (!f2fs_has_inline_data(inode) && !f2fs_compressed_file(inode) && 3918 (pos & PAGE_MASK) < i_size_read(inode) && 3919 f2fs_lookup_read_extent_cache_block(inode, index, blk_addr)) 3920 return 0; 3921 3922 /* 3923 * If a whole page is being written and we already preallocated all the 3924 * blocks, then there is no need to get a block address now. 3925 */ 3926 if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL)) 3927 return 0; 3928 3929 /* f2fs_lock_op avoids race between write CP and convert_inline_page */ 3930 if (f2fs_has_inline_data(inode)) { 3931 if (pos + len > MAX_INLINE_DATA(inode)) 3932 flag = F2FS_GET_BLOCK_DEFAULT; 3933 /* 3934 * it needs to cover FI_DATA_EXIST and inline data update w/ 3935 * node_change, otherwise concurrent checkpoint may persist 3936 * inline data only w/o F2FS_DATA_EXIST. 3937 */ 3938 f2fs_map_lock(sbi, &lc, flag); 3939 locked = true; 3940 } else if ((pos & PAGE_MASK) >= i_size_read(inode)) { 3941 f2fs_map_lock(sbi, &lc, flag); 3942 locked = true; 3943 } 3944 3945 restart: 3946 /* check inline_data */ 3947 ientry = f2fs_get_inode_cache(sbi, inode->i_ino); 3948 if (IS_ERR(ientry)) { 3949 err = PTR_ERR(ientry); 3950 goto unlock_out; 3951 } 3952 3953 set_new_dnode(&dn, inode, ientry, ientry, 0); 3954 3955 if (f2fs_has_inline_data(inode)) { 3956 if (pos + len <= MAX_INLINE_DATA(inode)) { 3957 f2fs_do_read_inline_data(folio, ientry); 3958 set_inode_flag(inode, FI_DATA_EXIST); 3959 if (inode->i_nlink) 3960 f2fs_cache_set_inline(ientry); 3961 goto out; 3962 } 3963 err = f2fs_convert_inline_folio(&dn, folio); 3964 if (err || dn.data_blkaddr != NULL_ADDR) 3965 goto out; 3966 } 3967 3968 if (!f2fs_lookup_read_extent_cache_block(inode, index, 3969 &dn.data_blkaddr)) { 3970 if (IS_DEVICE_ALIASING(inode)) { 3971 err = -ENODATA; 3972 goto out; 3973 } 3974 3975 if (locked) { 3976 err = f2fs_reserve_block(&dn, index); 3977 goto out; 3978 } 3979 3980 /* hole case */ 3981 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 3982 if (!err && dn.data_blkaddr != NULL_ADDR) 3983 goto out; 3984 f2fs_put_dnode(&dn); 3985 f2fs_map_lock(sbi, &lc, F2FS_GET_BLOCK_PRE_AIO); 3986 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO); 3987 locked = true; 3988 goto restart; 3989 } 3990 out: 3991 if (!err) { 3992 /* convert_inline_page can make node_changed */ 3993 *blk_addr = dn.data_blkaddr; 3994 *node_changed = dn.node_changed; 3995 } 3996 f2fs_put_dnode(&dn); 3997 unlock_out: 3998 if (locked) 3999 f2fs_map_unlock(sbi, &lc, flag); 4000 return err; 4001 } 4002 4003 static int __find_data_block(struct inode *inode, pgoff_t index, 4004 block_t *blk_addr) 4005 { 4006 struct dnode_of_data dn; 4007 struct f2fs_cached_block *ientry; 4008 int err = 0; 4009 4010 ientry = f2fs_get_inode_cache(F2FS_I_SB(inode), inode->i_ino); 4011 if (IS_ERR(ientry)) 4012 return PTR_ERR(ientry); 4013 4014 set_new_dnode(&dn, inode, ientry, ientry, 0); 4015 4016 if (!f2fs_lookup_read_extent_cache_block(inode, index, 4017 &dn.data_blkaddr)) { 4018 /* hole case */ 4019 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 4020 if (err) { 4021 dn.data_blkaddr = NULL_ADDR; 4022 err = 0; 4023 } 4024 } 4025 *blk_addr = dn.data_blkaddr; 4026 f2fs_put_dnode(&dn); 4027 return err; 4028 } 4029 4030 static int __reserve_data_block(struct inode *inode, pgoff_t index, 4031 block_t *blk_addr, bool *node_changed) 4032 { 4033 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4034 struct dnode_of_data dn; 4035 struct f2fs_lock_context lc; 4036 struct f2fs_cached_block *ientry; 4037 int err = 0; 4038 4039 f2fs_map_lock(sbi, &lc, F2FS_GET_BLOCK_PRE_AIO); 4040 4041 ientry = f2fs_get_inode_cache(sbi, inode->i_ino); 4042 if (IS_ERR(ientry)) { 4043 err = PTR_ERR(ientry); 4044 goto unlock_out; 4045 } 4046 set_new_dnode(&dn, inode, ientry, ientry, 0); 4047 4048 if (!f2fs_lookup_read_extent_cache_block(dn.inode, index, 4049 &dn.data_blkaddr)) 4050 err = f2fs_reserve_block(&dn, index); 4051 4052 *blk_addr = dn.data_blkaddr; 4053 *node_changed = dn.node_changed; 4054 f2fs_put_dnode(&dn); 4055 4056 unlock_out: 4057 f2fs_map_unlock(sbi, &lc, F2FS_GET_BLOCK_PRE_AIO); 4058 return err; 4059 } 4060 4061 static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi, 4062 struct folio *folio, loff_t pos, unsigned int len, 4063 block_t *blk_addr, bool *node_changed) 4064 { 4065 struct inode *inode = folio->mapping->host; 4066 struct inode *cow_inode = F2FS_I(inode)->cow_inode; 4067 pgoff_t index = folio->index; 4068 int err = 0; 4069 block_t ori_blk_addr = NULL_ADDR; 4070 bool cow_has_reserved_block = false; 4071 4072 /* If pos is beyond the end of file, reserve a new block in COW inode */ 4073 if ((pos & PAGE_MASK) >= i_size_read(inode)) 4074 goto reserve_block; 4075 4076 /* Look for the block in COW inode first */ 4077 err = __find_data_block(cow_inode, index, blk_addr); 4078 if (err) 4079 return err; 4080 4081 if (__is_valid_data_blkaddr(*blk_addr)) 4082 return 0; 4083 4084 if (*blk_addr == NEW_ADDR) 4085 cow_has_reserved_block = true; 4086 4087 if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE)) 4088 goto reserve_block; 4089 4090 /* Look for the block in the original inode */ 4091 err = __find_data_block(inode, index, &ori_blk_addr); 4092 if (err) 4093 return err; 4094 4095 reserve_block: 4096 /* Finally, we should reserve a new block in COW inode for the update */ 4097 if (!cow_has_reserved_block) { 4098 err = __reserve_data_block(cow_inode, index, blk_addr, 4099 node_changed); 4100 if (err) 4101 return err; 4102 inc_atomic_write_cnt(inode); 4103 } 4104 4105 if (ori_blk_addr != NULL_ADDR) 4106 *blk_addr = ori_blk_addr; 4107 return 0; 4108 } 4109 4110 static int f2fs_write_begin(const struct kiocb *iocb, 4111 struct address_space *mapping, 4112 loff_t pos, unsigned len, struct folio **foliop, 4113 void **fsdata) 4114 { 4115 struct inode *inode = mapping->host; 4116 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4117 struct folio *folio; 4118 pgoff_t index = pos >> PAGE_SHIFT; 4119 bool need_balance = false; 4120 block_t blkaddr = NULL_ADDR; 4121 int err = 0; 4122 4123 trace_f2fs_write_begin(inode, pos, len); 4124 4125 if (!f2fs_is_checkpoint_ready(sbi)) { 4126 err = -ENOSPC; 4127 goto fail; 4128 } 4129 4130 /* 4131 * We should check this at this moment to avoid deadlock on inode page 4132 * and #0 page. The locking rule for inline_data conversion should be: 4133 * folio_lock(folio #0) -> folio_lock(inode_page) 4134 */ 4135 if (index != 0) { 4136 err = f2fs_convert_inline_inode(inode); 4137 if (err) 4138 goto fail; 4139 } 4140 4141 #ifdef CONFIG_F2FS_FS_COMPRESSION 4142 if (f2fs_compressed_file(inode)) { 4143 int ret; 4144 struct page *page; 4145 4146 *fsdata = NULL; 4147 4148 if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode))) 4149 goto repeat; 4150 4151 ret = f2fs_prepare_compress_overwrite(inode, &page, 4152 index, fsdata); 4153 if (ret < 0) { 4154 err = ret; 4155 goto fail; 4156 } else if (ret) { 4157 *foliop = page_folio(page); 4158 return 0; 4159 } 4160 } 4161 #endif 4162 4163 repeat: 4164 /* 4165 * Do not use FGP_STABLE to avoid deadlock. 4166 * Will wait that below with our IO control. 4167 */ 4168 folio = f2fs_filemap_get_folio(mapping, index, 4169 FGP_LOCK | FGP_WRITE | FGP_CREAT, 4170 mapping_gfp_mask(mapping)); 4171 if (IS_ERR(folio)) { 4172 err = PTR_ERR(folio); 4173 goto fail; 4174 } 4175 4176 /* TODO: cluster can be compressed due to race with .writepage */ 4177 4178 *foliop = folio; 4179 4180 if (f2fs_is_atomic_file(inode)) 4181 err = prepare_atomic_write_begin(sbi, folio, pos, len, 4182 &blkaddr, &need_balance); 4183 else 4184 err = prepare_write_begin(sbi, folio, pos, len, 4185 &blkaddr, &need_balance); 4186 if (err) 4187 goto put_folio; 4188 4189 if (need_balance && !IS_NOQUOTA(inode) && 4190 has_not_enough_free_secs(sbi, 0, 0)) { 4191 folio_unlock(folio); 4192 f2fs_balance_fs(sbi, true); 4193 folio_lock(folio); 4194 if (folio->mapping != mapping) { 4195 /* The folio got truncated from under us */ 4196 folio_unlock(folio); 4197 folio_put(folio); 4198 goto repeat; 4199 } 4200 } 4201 4202 f2fs_folio_wait_writeback(folio, false, true); 4203 4204 if (len == folio_size(folio) || folio_test_uptodate(folio)) 4205 return 0; 4206 4207 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) && 4208 !f2fs_verity_in_progress(inode)) { 4209 folio_zero_segment(folio, len, folio_size(folio)); 4210 return 0; 4211 } 4212 4213 if (blkaddr == NEW_ADDR) { 4214 folio_zero_segment(folio, 0, folio_size(folio)); 4215 folio_mark_uptodate(folio); 4216 } else { 4217 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, 4218 DATA_GENERIC_ENHANCE_READ)) { 4219 err = -EFSCORRUPTED; 4220 goto put_folio; 4221 } 4222 /* 4223 * Although the block may be stored in the COW inode, the folio 4224 * belongs to @inode and its data was encrypted (or not) using 4225 * @inode's context (see f2fs_set_bio_crypt_ctx()). Read with 4226 * @inode so the post-read decryption decision matches the 4227 * folio's owner; otherwise an unencrypted @inode whose COW inode 4228 * is encrypted hits a NULL ->i_crypt_info on decryption. 4229 */ 4230 f2fs_submit_page_read(inode, 4231 NULL, /* can't write to fsverity files */ 4232 folio, blkaddr, 0, true); 4233 4234 folio_lock(folio); 4235 if (unlikely(folio->mapping != mapping)) { 4236 folio_unlock(folio); 4237 folio_put(folio); 4238 goto repeat; 4239 } 4240 if (unlikely(!folio_test_uptodate(folio))) { 4241 err = -EIO; 4242 goto put_folio; 4243 } 4244 } 4245 return 0; 4246 4247 put_folio: 4248 f2fs_folio_put(folio, true); 4249 fail: 4250 f2fs_write_failed(inode, pos + len); 4251 return err; 4252 } 4253 4254 static int f2fs_write_end(const struct kiocb *iocb, 4255 struct address_space *mapping, 4256 loff_t pos, unsigned len, unsigned copied, 4257 struct folio *folio, void *fsdata) 4258 { 4259 struct inode *inode = folio->mapping->host; 4260 4261 trace_f2fs_write_end(inode, pos, len, copied); 4262 4263 /* 4264 * This should be come from len == PAGE_SIZE, and we expect copied 4265 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and 4266 * let generic_perform_write() try to copy data again through copied=0. 4267 */ 4268 if (!folio_test_uptodate(folio)) { 4269 if (unlikely(copied != len)) 4270 copied = 0; 4271 else 4272 folio_mark_uptodate(folio); 4273 } 4274 4275 #ifdef CONFIG_F2FS_FS_COMPRESSION 4276 /* overwrite compressed file */ 4277 if (f2fs_compressed_file(inode) && fsdata) { 4278 f2fs_compress_write_end(inode, fsdata, folio->index, copied); 4279 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 4280 4281 if (pos + copied > i_size_read(inode) && 4282 !f2fs_verity_in_progress(inode)) 4283 f2fs_i_size_write(inode, pos + copied); 4284 return copied; 4285 } 4286 #endif 4287 4288 if (!copied) 4289 goto unlock_out; 4290 4291 folio_mark_dirty(folio); 4292 4293 if (f2fs_is_atomic_file(inode)) 4294 folio_set_f2fs_atomic(folio); 4295 4296 if (pos + copied > i_size_read(inode) && 4297 !f2fs_verity_in_progress(inode)) { 4298 f2fs_i_size_write(inode, pos + copied); 4299 if (f2fs_is_atomic_file(inode)) 4300 f2fs_i_size_write(F2FS_I(inode)->cow_inode, 4301 pos + copied); 4302 } 4303 unlock_out: 4304 f2fs_folio_put(folio, true); 4305 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 4306 return copied; 4307 } 4308 4309 void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length) 4310 { 4311 struct inode *inode = folio->mapping->host; 4312 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4313 4314 if (inode->i_ino >= F2FS_ROOT_INO(sbi) && 4315 (offset || length != folio_size(folio))) 4316 return; 4317 4318 if (folio_test_dirty(folio)) { 4319 inode_dec_dirty_pages(inode); 4320 f2fs_remove_dirty_inode(inode); 4321 } 4322 4323 if (offset || length != folio_size(folio)) 4324 return; 4325 4326 folio_cancel_dirty(folio); 4327 ffs_detach_free(folio); 4328 } 4329 4330 bool f2fs_release_folio(struct folio *folio, gfp_t wait) 4331 { 4332 /* If this is dirty folio, keep private data */ 4333 if (folio_test_dirty(folio)) 4334 return false; 4335 4336 ffs_detach_free(folio); 4337 return true; 4338 } 4339 4340 static bool f2fs_dirty_data_folio(struct address_space *mapping, 4341 struct folio *folio) 4342 { 4343 struct inode *inode = mapping->host; 4344 4345 trace_f2fs_set_page_dirty(folio, DATA); 4346 4347 if (!folio_test_uptodate(folio)) 4348 folio_mark_uptodate(folio); 4349 BUG_ON(folio_test_swapcache(folio)); 4350 4351 if (filemap_dirty_folio(mapping, folio)) { 4352 f2fs_update_dirty_folio(inode, folio); 4353 return true; 4354 } 4355 return false; 4356 } 4357 4358 4359 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block) 4360 { 4361 #ifdef CONFIG_F2FS_FS_COMPRESSION 4362 struct dnode_of_data dn; 4363 sector_t start_idx, blknr = 0; 4364 int ret; 4365 4366 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size); 4367 4368 set_new_dnode(&dn, inode, NULL, NULL, 0); 4369 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); 4370 if (ret) 4371 return 0; 4372 4373 if (dn.data_blkaddr != COMPRESS_ADDR) { 4374 dn.ofs_in_node += block - start_idx; 4375 blknr = f2fs_data_blkaddr(&dn); 4376 if (!__is_valid_data_blkaddr(blknr)) 4377 blknr = 0; 4378 } 4379 4380 f2fs_put_dnode(&dn); 4381 return blknr; 4382 #else 4383 return 0; 4384 #endif 4385 } 4386 4387 4388 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) 4389 { 4390 struct inode *inode = mapping->host; 4391 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4392 sector_t blknr = 0; 4393 4394 if (f2fs_has_inline_data(inode)) 4395 goto out; 4396 4397 /* make sure allocating whole blocks */ 4398 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 4399 filemap_write_and_wait(mapping); 4400 4401 /* Block number less than F2FS MAX BLOCKS */ 4402 if (unlikely(block >= max_file_blocks(sbi, inode))) 4403 goto out; 4404 4405 if (f2fs_compressed_file(inode)) { 4406 blknr = f2fs_bmap_compress(inode, block); 4407 } else { 4408 struct f2fs_map_blocks map; 4409 4410 memset(&map, 0, sizeof(map)); 4411 map.m_lblk = block; 4412 map.m_len = 1; 4413 map.m_next_pgofs = NULL; 4414 map.m_seg_type = NO_CHECK_TYPE; 4415 4416 if (!f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_BMAP)) 4417 blknr = map.m_pblk; 4418 } 4419 out: 4420 trace_f2fs_bmap(inode, block, blknr); 4421 return blknr; 4422 } 4423 4424 #ifdef CONFIG_SWAP 4425 static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk, 4426 unsigned int blkcnt) 4427 { 4428 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4429 unsigned int blkofs; 4430 unsigned int blk_per_sec = BLKS_PER_SEC(sbi); 4431 unsigned int end_blk = start_blk + blkcnt - 1; 4432 unsigned int secidx = start_blk / blk_per_sec; 4433 unsigned int end_sec; 4434 int ret = 0; 4435 4436 if (!blkcnt) 4437 return 0; 4438 end_sec = end_blk / blk_per_sec; 4439 4440 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 4441 filemap_invalidate_lock(inode->i_mapping); 4442 4443 set_inode_flag(inode, FI_ALIGNED_WRITE); 4444 set_inode_flag(inode, FI_OPU_WRITE); 4445 4446 for (; secidx <= end_sec; secidx++) { 4447 unsigned int blkofs_end = secidx == end_sec ? 4448 end_blk % blk_per_sec : blk_per_sec - 1; 4449 4450 f2fs_down_write(&sbi->pin_sem); 4451 4452 ret = f2fs_allocate_pinning_section(sbi); 4453 if (ret) { 4454 f2fs_up_write(&sbi->pin_sem); 4455 break; 4456 } 4457 4458 set_inode_flag(inode, FI_SKIP_WRITES); 4459 4460 for (blkofs = 0; blkofs <= blkofs_end; blkofs++) { 4461 struct folio *folio; 4462 unsigned int blkidx = secidx * blk_per_sec + blkofs; 4463 4464 folio = f2fs_get_lock_data_folio(inode, blkidx, true); 4465 if (IS_ERR(folio)) { 4466 f2fs_up_write(&sbi->pin_sem); 4467 ret = PTR_ERR(folio); 4468 goto done; 4469 } 4470 4471 folio_mark_dirty(folio); 4472 f2fs_folio_put(folio, true); 4473 } 4474 4475 clear_inode_flag(inode, FI_SKIP_WRITES); 4476 4477 ret = filemap_fdatawrite(inode->i_mapping); 4478 4479 f2fs_up_write(&sbi->pin_sem); 4480 4481 if (ret) 4482 break; 4483 } 4484 4485 done: 4486 clear_inode_flag(inode, FI_SKIP_WRITES); 4487 clear_inode_flag(inode, FI_OPU_WRITE); 4488 clear_inode_flag(inode, FI_ALIGNED_WRITE); 4489 4490 filemap_invalidate_unlock(inode->i_mapping); 4491 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 4492 4493 return ret; 4494 } 4495 4496 static int check_swap_activate(struct swap_info_struct *sis, 4497 struct file *swap_file, sector_t *span) 4498 { 4499 struct address_space *mapping = swap_file->f_mapping; 4500 struct inode *inode = mapping->host; 4501 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4502 block_t cur_lblock; 4503 block_t last_lblock; 4504 block_t pblock; 4505 block_t lowest_pblock = -1; 4506 block_t highest_pblock = 0; 4507 int nr_extents = 0; 4508 unsigned int nr_pblocks; 4509 unsigned int blks_per_sec = BLKS_PER_SEC(sbi); 4510 unsigned int not_aligned = 0; 4511 int ret = 0; 4512 4513 /* 4514 * Map all the blocks into the extent list. This code doesn't try 4515 * to be very smart. 4516 */ 4517 cur_lblock = 0; 4518 last_lblock = F2FS_BYTES_TO_BLK(sbi, i_size_read(inode)); 4519 4520 while (cur_lblock < last_lblock && cur_lblock < sis->max) { 4521 struct f2fs_map_blocks map; 4522 bool last_extent = false; 4523 retry: 4524 cond_resched(); 4525 4526 memset(&map, 0, sizeof(map)); 4527 map.m_lblk = cur_lblock; 4528 map.m_len = last_lblock - cur_lblock; 4529 map.m_next_pgofs = NULL; 4530 map.m_next_extent = NULL; 4531 map.m_seg_type = NO_CHECK_TYPE; 4532 map.m_may_create = false; 4533 4534 ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP); 4535 if (ret) 4536 goto out; 4537 4538 /* hole */ 4539 if (!(map.m_flags & F2FS_MAP_FLAGS)) { 4540 f2fs_err(sbi, "Swapfile has holes"); 4541 ret = -EINVAL; 4542 goto out; 4543 } 4544 4545 pblock = map.m_pblk; 4546 nr_pblocks = map.m_len; 4547 4548 if (!last_extent && 4549 ((pblock - SM_I(sbi)->main_blkaddr) % blks_per_sec || 4550 nr_pblocks % blks_per_sec || 4551 f2fs_is_sequential_zone_area(sbi, pblock))) { 4552 not_aligned++; 4553 4554 nr_pblocks = roundup(nr_pblocks, blks_per_sec); 4555 if (cur_lblock + nr_pblocks > sis->max) 4556 nr_pblocks -= blks_per_sec; 4557 4558 /* this extent is last one */ 4559 if (!nr_pblocks) { 4560 nr_pblocks = last_lblock - cur_lblock; 4561 last_extent = true; 4562 } 4563 4564 ret = f2fs_migrate_blocks(inode, cur_lblock, 4565 nr_pblocks); 4566 if (ret) { 4567 if (ret == -ENOENT) 4568 ret = -EINVAL; 4569 goto out; 4570 } 4571 4572 /* lookup block mapping info after block migration */ 4573 goto retry; 4574 } 4575 4576 if (cur_lblock + nr_pblocks >= sis->max) 4577 nr_pblocks = sis->max - cur_lblock; 4578 4579 if (cur_lblock) { /* exclude the header page */ 4580 if (pblock < lowest_pblock) 4581 lowest_pblock = pblock; 4582 if (pblock + nr_pblocks - 1 > highest_pblock) 4583 highest_pblock = pblock + nr_pblocks - 1; 4584 } 4585 4586 /* 4587 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks 4588 */ 4589 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock); 4590 if (ret < 0) 4591 goto out; 4592 nr_extents += ret; 4593 cur_lblock += nr_pblocks; 4594 } 4595 ret = nr_extents; 4596 *span = 1 + highest_pblock - lowest_pblock; 4597 if (cur_lblock == 0) 4598 cur_lblock = 1; /* force Empty message */ 4599 sis->max = cur_lblock; 4600 sis->pages = cur_lblock - 1; 4601 out: 4602 if (not_aligned) 4603 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%lu * N)", 4604 not_aligned, 4605 (unsigned long)blks_per_sec * F2FS_BLKSIZE(sbi)); 4606 return ret; 4607 } 4608 4609 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, 4610 sector_t *span) 4611 { 4612 struct inode *inode = file_inode(file); 4613 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4614 int ret; 4615 4616 if (!S_ISREG(inode->i_mode)) 4617 return -EINVAL; 4618 4619 if (f2fs_readonly(sbi->sb)) 4620 return -EROFS; 4621 4622 if (f2fs_lfs_mode(sbi) && !f2fs_sb_has_blkzoned(sbi)) { 4623 f2fs_err(sbi, "Swapfile not supported in LFS mode"); 4624 return -EINVAL; 4625 } 4626 4627 ret = f2fs_convert_inline_inode(inode); 4628 if (ret) 4629 return ret; 4630 4631 if (!f2fs_disable_compressed_file(inode)) 4632 return -EINVAL; 4633 4634 ret = filemap_fdatawrite(inode->i_mapping); 4635 if (ret < 0) 4636 return ret; 4637 4638 f2fs_precache_extents(inode); 4639 4640 ret = check_swap_activate(sis, file, span); 4641 if (ret < 0) 4642 return ret; 4643 4644 stat_inc_swapfile_inode(inode); 4645 set_inode_flag(inode, FI_PIN_FILE); 4646 f2fs_update_time(sbi, REQ_TIME); 4647 return ret; 4648 } 4649 4650 static void f2fs_swap_deactivate(struct file *file) 4651 { 4652 struct inode *inode = file_inode(file); 4653 4654 stat_dec_swapfile_inode(inode); 4655 clear_inode_flag(inode, FI_PIN_FILE); 4656 } 4657 #else 4658 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, 4659 sector_t *span) 4660 { 4661 return -EOPNOTSUPP; 4662 } 4663 4664 static void f2fs_swap_deactivate(struct file *file) 4665 { 4666 } 4667 #endif 4668 4669 const struct address_space_operations f2fs_dblock_aops = { 4670 .read_folio = f2fs_read_data_folio, 4671 .readahead = f2fs_readahead, 4672 .writepages = f2fs_write_data_pages, 4673 .write_begin = f2fs_write_begin, 4674 .write_end = f2fs_write_end, 4675 .dirty_folio = f2fs_dirty_data_folio, 4676 .migrate_folio = filemap_migrate_folio, 4677 .invalidate_folio = f2fs_invalidate_folio, 4678 .release_folio = f2fs_release_folio, 4679 .bmap = f2fs_bmap, 4680 .swap_activate = f2fs_swap_activate, 4681 .swap_deactivate = f2fs_swap_deactivate, 4682 }; 4683 4684 void f2fs_clear_page_cache_dirty_tag(struct folio *folio) 4685 { 4686 struct address_space *mapping = folio->mapping; 4687 unsigned long flags; 4688 4689 xa_lock_irqsave(&mapping->i_pages, flags); 4690 __xa_clear_mark(&mapping->i_pages, folio->index, 4691 PAGECACHE_TAG_DIRTY); 4692 xa_unlock_irqrestore(&mapping->i_pages, flags); 4693 } 4694 4695 int __init f2fs_init_post_read_processing(void) 4696 { 4697 bio_post_read_ctx_cache = 4698 kmem_cache_create("f2fs_bio_post_read_ctx", 4699 sizeof(struct bio_post_read_ctx), 0, 0, NULL); 4700 if (!bio_post_read_ctx_cache) 4701 goto fail; 4702 bio_post_read_ctx_pool = 4703 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS, 4704 bio_post_read_ctx_cache); 4705 if (!bio_post_read_ctx_pool) 4706 goto fail_free_cache; 4707 return 0; 4708 4709 fail_free_cache: 4710 kmem_cache_destroy(bio_post_read_ctx_cache); 4711 fail: 4712 return -ENOMEM; 4713 } 4714 4715 void f2fs_destroy_post_read_processing(void) 4716 { 4717 mempool_destroy(bio_post_read_ctx_pool); 4718 kmem_cache_destroy(bio_post_read_ctx_cache); 4719 } 4720 4721 int f2fs_init_wq(struct f2fs_sb_info *sbi) 4722 { 4723 sbi->wq = alloc_workqueue("f2fs_wq", WQ_UNBOUND | WQ_HIGHPRI, 4724 num_online_cpus()); 4725 if (!sbi->wq) 4726 return -ENOMEM; 4727 4728 sbi->evict_wq = alloc_workqueue("f2fs_evict_wq", 4729 WQ_UNBOUND | WQ_HIGHPRI, num_online_cpus()); 4730 if (!sbi->evict_wq) { 4731 destroy_workqueue(sbi->wq); 4732 return -ENOMEM; 4733 } 4734 return 0; 4735 } 4736 4737 void f2fs_destroy_wq(struct f2fs_sb_info *sbi) 4738 { 4739 if (sbi->wq) 4740 destroy_workqueue(sbi->wq); 4741 if (sbi->evict_wq) 4742 destroy_workqueue(sbi->evict_wq); 4743 } 4744 4745 int __init f2fs_init_bio_entry_cache(void) 4746 { 4747 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab", 4748 sizeof(struct bio_entry)); 4749 4750 if (!bio_entry_slab) 4751 return -ENOMEM; 4752 4753 ffs_entry_slab = f2fs_kmem_cache_create("f2fs_ffs_slab", 4754 sizeof(struct f2fs_folio_state)); 4755 4756 if (!ffs_entry_slab) { 4757 kmem_cache_destroy(bio_entry_slab); 4758 return -ENOMEM; 4759 } 4760 4761 return 0; 4762 } 4763 4764 void f2fs_destroy_bio_entry_cache(void) 4765 { 4766 kmem_cache_destroy(bio_entry_slab); 4767 kmem_cache_destroy(ffs_entry_slab); 4768 } 4769 4770 static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 4771 unsigned int flags, struct iomap *iomap, 4772 struct iomap *srcmap) 4773 { 4774 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4775 struct f2fs_map_blocks map = { NULL, }; 4776 pgoff_t next_pgofs = 0; 4777 int err; 4778 4779 map.m_lblk = F2FS_BYTES_TO_BLK(sbi, offset); 4780 map.m_len = F2FS_BYTES_TO_BLK(sbi, offset + length - 1) - 4781 map.m_lblk + 1; 4782 map.m_next_pgofs = &next_pgofs; 4783 map.m_seg_type = f2fs_rw_hint_to_seg_type(F2FS_I_SB(inode), 4784 inode->i_write_hint); 4785 if (flags & IOMAP_WRITE && iomap->private) { 4786 map.m_last_pblk = (unsigned long)iomap->private; 4787 iomap->private = NULL; 4788 } 4789 4790 /* 4791 * If the blocks being overwritten are already allocated, 4792 * f2fs_map_lock and f2fs_balance_fs are not necessary. 4793 */ 4794 if ((flags & IOMAP_WRITE) && 4795 !__f2fs_overwrite_io(inode, offset, length, true)) 4796 map.m_may_create = true; 4797 4798 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DIO); 4799 if (err) 4800 return err; 4801 4802 iomap->offset = F2FS_BLK_TO_BYTES(sbi, map.m_lblk); 4803 4804 /* 4805 * Sometimes I/O to an encrypted file has to be broken up to guarantee 4806 * DUN contiguity. Handle this by limiting the length of the mapping 4807 * returned. 4808 */ 4809 map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len); 4810 4811 /* 4812 * We should never see delalloc or compressed extents here based on 4813 * prior flushing and checks. 4814 */ 4815 if (WARN_ON_ONCE(map.m_pblk == COMPRESS_ADDR)) 4816 return -EINVAL; 4817 4818 if (map.m_flags & F2FS_MAP_MAPPED) { 4819 if (WARN_ON_ONCE(map.m_pblk == NEW_ADDR)) 4820 return -EINVAL; 4821 4822 iomap->length = F2FS_BLK_TO_BYTES(sbi, map.m_len); 4823 iomap->type = IOMAP_MAPPED; 4824 iomap->flags |= IOMAP_F_MERGED; 4825 iomap->bdev = map.m_bdev; 4826 iomap->addr = F2FS_BLK_TO_BYTES(sbi, map.m_pblk); 4827 4828 if (flags & IOMAP_WRITE && map.m_last_pblk) 4829 iomap->private = (void *)map.m_last_pblk; 4830 } else { 4831 if (flags & IOMAP_WRITE) 4832 return -ENOTBLK; 4833 4834 if (map.m_pblk == NULL_ADDR) { 4835 iomap->length = F2FS_BLK_TO_BYTES(sbi, next_pgofs) - 4836 iomap->offset; 4837 iomap->type = IOMAP_HOLE; 4838 } else if (map.m_pblk == NEW_ADDR) { 4839 iomap->length = F2FS_BLK_TO_BYTES(sbi, map.m_len); 4840 iomap->type = IOMAP_UNWRITTEN; 4841 } else { 4842 f2fs_bug_on(F2FS_I_SB(inode), 1); 4843 } 4844 iomap->addr = IOMAP_NULL_ADDR; 4845 } 4846 4847 if (map.m_flags & F2FS_MAP_NEW) 4848 iomap->flags |= IOMAP_F_NEW; 4849 if ((inode_state_read_once(inode) & I_DIRTY_DATASYNC) || 4850 offset + length > i_size_read(inode)) 4851 iomap->flags |= IOMAP_F_DIRTY; 4852 4853 return 0; 4854 } 4855 4856 static DEFINE_IOMAP_ITER_NEXT(f2fs_iomap_next, f2fs_iomap_begin); 4857 4858 const struct iomap_ops f2fs_iomap_ops = { 4859 .iomap_next = f2fs_iomap_next, 4860 }; 4861