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