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