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