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 folio *folio, 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 bio_add_folio_nofail(bio, folio, folio_size(folio), 0); 765 766 f2fs_down_write(&io->bio_list_lock); 767 list_add_tail(&be->list, &io->bio_list); 768 f2fs_up_write(&io->bio_list_lock); 769 } 770 771 static void del_bio_entry(struct bio_entry *be) 772 { 773 list_del(&be->list); 774 kmem_cache_free(bio_entry_slab, be); 775 } 776 777 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio, 778 struct folio *folio) 779 { 780 struct folio *fio_folio = fio->folio; 781 struct f2fs_sb_info *sbi = fio->sbi; 782 enum temp_type temp; 783 bool found = false; 784 int ret = -EAGAIN; 785 786 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { 787 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; 788 struct list_head *head = &io->bio_list; 789 struct bio_entry *be; 790 791 f2fs_down_write(&io->bio_list_lock); 792 list_for_each_entry(be, head, list) { 793 if (be->bio != *bio) 794 continue; 795 796 found = true; 797 798 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio, 799 *fio->last_block, 800 fio->new_blkaddr)); 801 if (f2fs_crypt_mergeable_bio(*bio, 802 fio_folio->mapping->host, 803 fio_folio->index, fio) && 804 bio_add_folio(*bio, folio, folio_size(folio), 0)) { 805 ret = 0; 806 break; 807 } 808 809 /* page can't be merged into bio; submit the bio */ 810 del_bio_entry(be); 811 f2fs_submit_write_bio(sbi, *bio, DATA); 812 break; 813 } 814 f2fs_up_write(&io->bio_list_lock); 815 } 816 817 if (ret) { 818 bio_put(*bio); 819 *bio = NULL; 820 } 821 822 return ret; 823 } 824 825 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi, 826 struct bio **bio, struct folio *folio) 827 { 828 enum temp_type temp; 829 bool found = false; 830 struct bio *target = bio ? *bio : NULL; 831 832 f2fs_bug_on(sbi, !target && !folio); 833 834 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { 835 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; 836 struct list_head *head = &io->bio_list; 837 struct bio_entry *be; 838 839 if (list_empty(head)) 840 continue; 841 842 f2fs_down_read(&io->bio_list_lock); 843 list_for_each_entry(be, head, list) { 844 if (target) 845 found = (target == be->bio); 846 else 847 found = __has_merged_page(be->bio, NULL, 848 folio, 0); 849 if (found) 850 break; 851 } 852 f2fs_up_read(&io->bio_list_lock); 853 854 if (!found) 855 continue; 856 857 found = false; 858 859 f2fs_down_write(&io->bio_list_lock); 860 list_for_each_entry(be, head, list) { 861 if (target) 862 found = (target == be->bio); 863 else 864 found = __has_merged_page(be->bio, NULL, 865 folio, 0); 866 if (found) { 867 target = be->bio; 868 del_bio_entry(be); 869 break; 870 } 871 } 872 f2fs_up_write(&io->bio_list_lock); 873 } 874 875 if (found) 876 f2fs_submit_write_bio(sbi, target, DATA); 877 if (bio && *bio) { 878 bio_put(*bio); 879 *bio = NULL; 880 } 881 } 882 883 int f2fs_merge_page_bio(struct f2fs_io_info *fio) 884 { 885 struct bio *bio = *fio->bio; 886 struct folio *data_folio = fio->encrypted_page ? 887 page_folio(fio->encrypted_page) : fio->folio; 888 struct folio *folio = fio->folio; 889 890 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, 891 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC)) 892 return -EFSCORRUPTED; 893 894 trace_f2fs_submit_folio_bio(data_folio, fio); 895 896 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block, 897 fio->new_blkaddr)) 898 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL); 899 alloc_new: 900 if (!bio) { 901 bio = __bio_alloc(fio, BIO_MAX_VECS); 902 f2fs_set_bio_crypt_ctx(bio, folio->mapping->host, 903 folio->index, fio, GFP_NOIO); 904 905 add_bio_entry(fio->sbi, bio, data_folio, fio->temp); 906 } else { 907 if (add_ipu_page(fio, &bio, data_folio)) 908 goto alloc_new; 909 } 910 911 if (fio->io_wbc) 912 wbc_account_cgroup_owner(fio->io_wbc, folio, folio_size(folio)); 913 914 inc_page_count(fio->sbi, WB_DATA_TYPE(folio, false)); 915 916 *fio->last_block = fio->new_blkaddr; 917 *fio->bio = bio; 918 919 return 0; 920 } 921 922 #ifdef CONFIG_BLK_DEV_ZONED 923 static bool is_end_zone_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr) 924 { 925 struct block_device *bdev = sbi->sb->s_bdev; 926 int devi = 0; 927 928 if (f2fs_is_multi_device(sbi)) { 929 devi = f2fs_target_device_index(sbi, blkaddr); 930 if (blkaddr < FDEV(devi).start_blk || 931 blkaddr > FDEV(devi).end_blk) { 932 f2fs_err(sbi, "Invalid block %x", blkaddr); 933 return false; 934 } 935 blkaddr -= FDEV(devi).start_blk; 936 bdev = FDEV(devi).bdev; 937 } 938 return bdev_is_zoned(bdev) && 939 f2fs_blkz_is_seq(sbi, devi, blkaddr) && 940 (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1); 941 } 942 #endif 943 944 void f2fs_submit_page_write(struct f2fs_io_info *fio) 945 { 946 struct f2fs_sb_info *sbi = fio->sbi; 947 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); 948 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp; 949 struct folio *bio_folio; 950 enum count_type type; 951 952 f2fs_bug_on(sbi, is_read_io(fio->op)); 953 954 f2fs_down_write(&io->io_rwsem); 955 next: 956 #ifdef CONFIG_BLK_DEV_ZONED 957 if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) { 958 wait_for_completion_io(&io->zone_wait); 959 bio_put(io->zone_pending_bio); 960 io->zone_pending_bio = NULL; 961 io->bi_private = NULL; 962 } 963 #endif 964 965 if (fio->in_list) { 966 spin_lock(&io->io_lock); 967 if (list_empty(&io->io_list)) { 968 spin_unlock(&io->io_lock); 969 goto out; 970 } 971 fio = list_first_entry(&io->io_list, 972 struct f2fs_io_info, list); 973 list_del(&fio->list); 974 spin_unlock(&io->io_lock); 975 } 976 977 verify_fio_blkaddr(fio); 978 979 if (fio->encrypted_page) 980 bio_folio = page_folio(fio->encrypted_page); 981 else if (fio->compressed_page) 982 bio_folio = page_folio(fio->compressed_page); 983 else 984 bio_folio = fio->folio; 985 986 /* set submitted = true as a return value */ 987 fio->submitted = 1; 988 989 type = WB_DATA_TYPE(bio_folio, fio->compressed_page); 990 inc_page_count(sbi, type); 991 992 if (io->bio && 993 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio, 994 fio->new_blkaddr) || 995 !f2fs_crypt_mergeable_bio(io->bio, fio_inode(fio), 996 bio_folio->index, fio))) 997 __submit_merged_bio(io); 998 alloc_new: 999 if (io->bio == NULL) { 1000 io->bio = __bio_alloc(fio, BIO_MAX_VECS); 1001 f2fs_set_bio_crypt_ctx(io->bio, fio_inode(fio), 1002 bio_folio->index, fio, GFP_NOIO); 1003 io->fio = *fio; 1004 } 1005 1006 if (!bio_add_folio(io->bio, bio_folio, folio_size(bio_folio), 0)) { 1007 __submit_merged_bio(io); 1008 goto alloc_new; 1009 } 1010 1011 if (fio->io_wbc) 1012 wbc_account_cgroup_owner(fio->io_wbc, fio->folio, 1013 folio_size(fio->folio)); 1014 1015 io->last_block_in_bio = fio->new_blkaddr; 1016 1017 trace_f2fs_submit_folio_write(fio->folio, fio); 1018 #ifdef CONFIG_BLK_DEV_ZONED 1019 if (f2fs_sb_has_blkzoned(sbi) && btype < META && 1020 is_end_zone_blkaddr(sbi, fio->new_blkaddr)) { 1021 bio_get(io->bio); 1022 reinit_completion(&io->zone_wait); 1023 io->bi_private = io->bio->bi_private; 1024 io->bio->bi_private = io; 1025 io->bio->bi_end_io = f2fs_zone_write_end_io; 1026 io->zone_pending_bio = io->bio; 1027 __submit_merged_bio(io); 1028 } 1029 #endif 1030 if (fio->in_list) 1031 goto next; 1032 out: 1033 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || 1034 !f2fs_is_checkpoint_ready(sbi)) 1035 __submit_merged_bio(io); 1036 f2fs_up_write(&io->io_rwsem); 1037 } 1038 1039 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr, 1040 unsigned nr_pages, blk_opf_t op_flag, 1041 pgoff_t first_idx, bool for_write) 1042 { 1043 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1044 struct bio *bio; 1045 struct bio_post_read_ctx *ctx = NULL; 1046 unsigned int post_read_steps = 0; 1047 sector_t sector; 1048 struct block_device *bdev = f2fs_target_device(sbi, blkaddr, §or); 1049 1050 bio = bio_alloc_bioset(bdev, bio_max_segs(nr_pages), 1051 REQ_OP_READ | op_flag, 1052 for_write ? GFP_NOIO : GFP_KERNEL, &f2fs_bioset); 1053 bio->bi_iter.bi_sector = sector; 1054 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS); 1055 bio->bi_end_io = f2fs_read_end_io; 1056 1057 if (fscrypt_inode_uses_fs_layer_crypto(inode)) 1058 post_read_steps |= STEP_DECRYPT; 1059 1060 if (f2fs_need_verity(inode, first_idx)) 1061 post_read_steps |= STEP_VERITY; 1062 1063 /* 1064 * STEP_DECOMPRESS is handled specially, since a compressed file might 1065 * contain both compressed and uncompressed clusters. We'll allocate a 1066 * bio_post_read_ctx if the file is compressed, but the caller is 1067 * responsible for enabling STEP_DECOMPRESS if it's actually needed. 1068 */ 1069 1070 if (post_read_steps || f2fs_compressed_file(inode)) { 1071 /* Due to the mempool, this never fails. */ 1072 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS); 1073 ctx->bio = bio; 1074 ctx->sbi = sbi; 1075 ctx->enabled_steps = post_read_steps; 1076 ctx->fs_blkaddr = blkaddr; 1077 ctx->decompression_attempted = false; 1078 bio->bi_private = ctx; 1079 } 1080 iostat_alloc_and_bind_ctx(sbi, bio, ctx); 1081 1082 return bio; 1083 } 1084 1085 /* This can handle encryption stuffs */ 1086 static void f2fs_submit_page_read(struct inode *inode, struct folio *folio, 1087 block_t blkaddr, blk_opf_t op_flags, 1088 bool for_write) 1089 { 1090 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1091 struct bio *bio; 1092 1093 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags, 1094 folio->index, for_write); 1095 1096 /* wait for GCed page writeback via META_MAPPING */ 1097 f2fs_wait_on_block_writeback(inode, blkaddr); 1098 1099 if (!bio_add_folio(bio, folio, PAGE_SIZE, 0)) 1100 f2fs_bug_on(sbi, 1); 1101 1102 inc_page_count(sbi, F2FS_RD_DATA); 1103 f2fs_update_iostat(sbi, NULL, FS_DATA_READ_IO, F2FS_BLKSIZE); 1104 f2fs_submit_read_bio(sbi, bio, DATA); 1105 } 1106 1107 static void __set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) 1108 { 1109 __le32 *addr = get_dnode_addr(dn->inode, dn->node_folio); 1110 1111 dn->data_blkaddr = blkaddr; 1112 addr[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr); 1113 } 1114 1115 /* 1116 * Lock ordering for the change of data block address: 1117 * ->data_page 1118 * ->node_folio 1119 * update block addresses in the node page 1120 */ 1121 void f2fs_set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) 1122 { 1123 f2fs_folio_wait_writeback(dn->node_folio, NODE, true, true); 1124 __set_data_blkaddr(dn, blkaddr); 1125 if (folio_mark_dirty(dn->node_folio)) 1126 dn->node_changed = true; 1127 } 1128 1129 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) 1130 { 1131 f2fs_set_data_blkaddr(dn, blkaddr); 1132 f2fs_update_read_extent_cache(dn); 1133 } 1134 1135 /* dn->ofs_in_node will be returned with up-to-date last block pointer */ 1136 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) 1137 { 1138 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 1139 int err; 1140 1141 if (!count) 1142 return 0; 1143 1144 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) 1145 return -EPERM; 1146 err = inc_valid_block_count(sbi, dn->inode, &count, true); 1147 if (unlikely(err)) 1148 return err; 1149 1150 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, 1151 dn->ofs_in_node, count); 1152 1153 f2fs_folio_wait_writeback(dn->node_folio, NODE, true, true); 1154 1155 for (; count > 0; dn->ofs_in_node++) { 1156 block_t blkaddr = f2fs_data_blkaddr(dn); 1157 1158 if (blkaddr == NULL_ADDR) { 1159 __set_data_blkaddr(dn, NEW_ADDR); 1160 count--; 1161 } 1162 } 1163 1164 if (folio_mark_dirty(dn->node_folio)) 1165 dn->node_changed = true; 1166 return 0; 1167 } 1168 1169 /* Should keep dn->ofs_in_node unchanged */ 1170 int f2fs_reserve_new_block(struct dnode_of_data *dn) 1171 { 1172 unsigned int ofs_in_node = dn->ofs_in_node; 1173 int ret; 1174 1175 ret = f2fs_reserve_new_blocks(dn, 1); 1176 dn->ofs_in_node = ofs_in_node; 1177 return ret; 1178 } 1179 1180 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) 1181 { 1182 bool need_put = dn->inode_folio ? false : true; 1183 int err; 1184 1185 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE); 1186 if (err) 1187 return err; 1188 1189 if (dn->data_blkaddr == NULL_ADDR) 1190 err = f2fs_reserve_new_block(dn); 1191 if (err || need_put) 1192 f2fs_put_dnode(dn); 1193 return err; 1194 } 1195 1196 struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index, 1197 blk_opf_t op_flags, bool for_write, pgoff_t *next_pgofs) 1198 { 1199 struct address_space *mapping = inode->i_mapping; 1200 struct dnode_of_data dn; 1201 struct folio *folio; 1202 int err; 1203 1204 folio = f2fs_grab_cache_folio(mapping, index, for_write); 1205 if (IS_ERR(folio)) 1206 return folio; 1207 1208 if (f2fs_lookup_read_extent_cache_block(inode, index, 1209 &dn.data_blkaddr)) { 1210 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr, 1211 DATA_GENERIC_ENHANCE_READ)) { 1212 err = -EFSCORRUPTED; 1213 goto put_err; 1214 } 1215 goto got_it; 1216 } 1217 1218 set_new_dnode(&dn, inode, NULL, NULL, 0); 1219 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 1220 if (err) { 1221 if (err == -ENOENT && next_pgofs) 1222 *next_pgofs = f2fs_get_next_page_offset(&dn, index); 1223 goto put_err; 1224 } 1225 f2fs_put_dnode(&dn); 1226 1227 if (unlikely(dn.data_blkaddr == NULL_ADDR)) { 1228 err = -ENOENT; 1229 if (next_pgofs) 1230 *next_pgofs = index + 1; 1231 goto put_err; 1232 } 1233 if (dn.data_blkaddr != NEW_ADDR && 1234 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode), 1235 dn.data_blkaddr, 1236 DATA_GENERIC_ENHANCE)) { 1237 err = -EFSCORRUPTED; 1238 goto put_err; 1239 } 1240 got_it: 1241 if (folio_test_uptodate(folio)) { 1242 folio_unlock(folio); 1243 return folio; 1244 } 1245 1246 /* 1247 * A new dentry page is allocated but not able to be written, since its 1248 * new inode page couldn't be allocated due to -ENOSPC. 1249 * In such the case, its blkaddr can be remained as NEW_ADDR. 1250 * see, f2fs_add_link -> f2fs_get_new_data_folio -> 1251 * f2fs_init_inode_metadata. 1252 */ 1253 if (dn.data_blkaddr == NEW_ADDR) { 1254 folio_zero_segment(folio, 0, folio_size(folio)); 1255 if (!folio_test_uptodate(folio)) 1256 folio_mark_uptodate(folio); 1257 folio_unlock(folio); 1258 return folio; 1259 } 1260 1261 f2fs_submit_page_read(inode, folio, dn.data_blkaddr, 1262 op_flags, for_write); 1263 return folio; 1264 1265 put_err: 1266 f2fs_folio_put(folio, true); 1267 return ERR_PTR(err); 1268 } 1269 1270 struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index, 1271 pgoff_t *next_pgofs) 1272 { 1273 struct address_space *mapping = inode->i_mapping; 1274 struct folio *folio; 1275 1276 folio = f2fs_filemap_get_folio(mapping, index, FGP_ACCESSED, 0); 1277 if (IS_ERR(folio)) 1278 goto read; 1279 if (folio_test_uptodate(folio)) 1280 return folio; 1281 f2fs_folio_put(folio, false); 1282 1283 read: 1284 folio = f2fs_get_read_data_folio(inode, index, 0, false, next_pgofs); 1285 if (IS_ERR(folio)) 1286 return folio; 1287 1288 if (folio_test_uptodate(folio)) 1289 return folio; 1290 1291 folio_wait_locked(folio); 1292 if (unlikely(!folio_test_uptodate(folio))) { 1293 f2fs_folio_put(folio, false); 1294 return ERR_PTR(-EIO); 1295 } 1296 return folio; 1297 } 1298 1299 /* 1300 * If it tries to access a hole, return an error. 1301 * Because, the callers, functions in dir.c and GC, should be able to know 1302 * whether this page exists or not. 1303 */ 1304 struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index, 1305 bool for_write) 1306 { 1307 struct address_space *mapping = inode->i_mapping; 1308 struct folio *folio; 1309 1310 folio = f2fs_get_read_data_folio(inode, index, 0, for_write, NULL); 1311 if (IS_ERR(folio)) 1312 return folio; 1313 1314 /* wait for read completion */ 1315 folio_lock(folio); 1316 if (unlikely(folio->mapping != mapping || !folio_test_uptodate(folio))) { 1317 f2fs_folio_put(folio, true); 1318 return ERR_PTR(-EIO); 1319 } 1320 return folio; 1321 } 1322 1323 /* 1324 * Caller ensures that this data page is never allocated. 1325 * A new zero-filled data page is allocated in the page cache. 1326 * 1327 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and 1328 * f2fs_unlock_op(). 1329 * Note that, ifolio is set only by make_empty_dir, and if any error occur, 1330 * ifolio should be released by this function. 1331 */ 1332 struct folio *f2fs_get_new_data_folio(struct inode *inode, 1333 struct folio *ifolio, pgoff_t index, bool new_i_size) 1334 { 1335 struct address_space *mapping = inode->i_mapping; 1336 struct folio *folio; 1337 struct dnode_of_data dn; 1338 int err; 1339 1340 folio = f2fs_grab_cache_folio(mapping, index, true); 1341 if (IS_ERR(folio)) { 1342 /* 1343 * before exiting, we should make sure ifolio will be released 1344 * if any error occur. 1345 */ 1346 f2fs_folio_put(ifolio, true); 1347 return ERR_PTR(-ENOMEM); 1348 } 1349 1350 set_new_dnode(&dn, inode, ifolio, NULL, 0); 1351 err = f2fs_reserve_block(&dn, index); 1352 if (err) { 1353 f2fs_folio_put(folio, true); 1354 return ERR_PTR(err); 1355 } 1356 if (!ifolio) 1357 f2fs_put_dnode(&dn); 1358 1359 if (folio_test_uptodate(folio)) 1360 goto got_it; 1361 1362 if (dn.data_blkaddr == NEW_ADDR) { 1363 folio_zero_segment(folio, 0, folio_size(folio)); 1364 if (!folio_test_uptodate(folio)) 1365 folio_mark_uptodate(folio); 1366 } else { 1367 f2fs_folio_put(folio, true); 1368 1369 /* if ifolio exists, blkaddr should be NEW_ADDR */ 1370 f2fs_bug_on(F2FS_I_SB(inode), ifolio); 1371 folio = f2fs_get_lock_data_folio(inode, index, true); 1372 if (IS_ERR(folio)) 1373 return folio; 1374 } 1375 got_it: 1376 if (new_i_size && i_size_read(inode) < 1377 ((loff_t)(index + 1) << PAGE_SHIFT)) 1378 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT)); 1379 return folio; 1380 } 1381 1382 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type) 1383 { 1384 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 1385 struct f2fs_summary sum; 1386 struct node_info ni; 1387 block_t old_blkaddr; 1388 blkcnt_t count = 1; 1389 int err; 1390 1391 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) 1392 return -EPERM; 1393 1394 err = f2fs_get_node_info(sbi, dn->nid, &ni, false); 1395 if (err) 1396 return err; 1397 1398 dn->data_blkaddr = f2fs_data_blkaddr(dn); 1399 if (dn->data_blkaddr == NULL_ADDR) { 1400 err = inc_valid_block_count(sbi, dn->inode, &count, true); 1401 if (unlikely(err)) 1402 return err; 1403 } 1404 1405 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); 1406 old_blkaddr = dn->data_blkaddr; 1407 err = f2fs_allocate_data_block(sbi, NULL, old_blkaddr, 1408 &dn->data_blkaddr, &sum, seg_type, NULL); 1409 if (err) 1410 return err; 1411 1412 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) 1413 f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1); 1414 1415 f2fs_update_data_blkaddr(dn, dn->data_blkaddr); 1416 return 0; 1417 } 1418 1419 static void f2fs_map_lock(struct f2fs_sb_info *sbi, int flag) 1420 { 1421 f2fs_down_read(&sbi->cp_enable_rwsem); 1422 if (flag == F2FS_GET_BLOCK_PRE_AIO) 1423 f2fs_down_read(&sbi->node_change); 1424 else 1425 f2fs_lock_op(sbi); 1426 } 1427 1428 static void f2fs_map_unlock(struct f2fs_sb_info *sbi, int flag) 1429 { 1430 if (flag == F2FS_GET_BLOCK_PRE_AIO) 1431 f2fs_up_read(&sbi->node_change); 1432 else 1433 f2fs_unlock_op(sbi); 1434 f2fs_up_read(&sbi->cp_enable_rwsem); 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_len = min(map->m_len, dev->end_blk + 1 - map->m_pblk); 1501 map->m_pblk -= dev->start_blk; 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 tag = wbc_to_tag(wbc); 2990 retry: 2991 retry = 0; 2992 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 2993 tag_pages_for_writeback(mapping, index, end); 2994 done_index = index; 2995 while (!done && !retry && (index <= end)) { 2996 nr_pages = 0; 2997 again: 2998 nr_folios = filemap_get_folios_tag(mapping, &index, end, 2999 tag, &fbatch); 3000 if (nr_folios == 0) { 3001 if (nr_pages) 3002 goto write; 3003 break; 3004 } 3005 3006 for (i = 0; i < nr_folios; i++) { 3007 struct folio *folio = fbatch.folios[i]; 3008 3009 idx = 0; 3010 p = folio_nr_pages(folio); 3011 add_more: 3012 pages[nr_pages] = folio_page(folio, idx); 3013 folio_get(folio); 3014 if (++nr_pages == max_pages) { 3015 index = folio->index + idx + 1; 3016 folio_batch_release(&fbatch); 3017 goto write; 3018 } 3019 if (++idx < p) 3020 goto add_more; 3021 } 3022 folio_batch_release(&fbatch); 3023 goto again; 3024 write: 3025 for (i = 0; i < nr_pages; i++) { 3026 struct page *page = pages[i]; 3027 struct folio *folio = page_folio(page); 3028 bool need_readd; 3029 readd: 3030 need_readd = false; 3031 #ifdef CONFIG_F2FS_FS_COMPRESSION 3032 if (f2fs_compressed_file(inode)) { 3033 void *fsdata = NULL; 3034 struct page *pagep; 3035 int ret2; 3036 3037 ret = f2fs_init_compress_ctx(&cc); 3038 if (ret) { 3039 done = 1; 3040 break; 3041 } 3042 3043 if (!f2fs_cluster_can_merge_page(&cc, 3044 folio->index)) { 3045 ret = f2fs_write_multi_pages(&cc, 3046 &submitted, wbc, io_type); 3047 if (!ret) 3048 need_readd = true; 3049 goto result; 3050 } 3051 3052 if (unlikely(f2fs_cp_error(sbi))) 3053 goto lock_folio; 3054 3055 if (!f2fs_cluster_is_empty(&cc)) 3056 goto lock_folio; 3057 3058 if (f2fs_all_cluster_page_ready(&cc, 3059 pages, i, nr_pages, true)) 3060 goto lock_folio; 3061 3062 ret2 = f2fs_prepare_compress_overwrite( 3063 inode, &pagep, 3064 folio->index, &fsdata); 3065 if (ret2 < 0) { 3066 ret = ret2; 3067 done = 1; 3068 break; 3069 } else if (ret2 && 3070 (!f2fs_compress_write_end(inode, 3071 fsdata, folio->index, 1) || 3072 !f2fs_all_cluster_page_ready(&cc, 3073 pages, i, nr_pages, 3074 false))) { 3075 retry = 1; 3076 break; 3077 } 3078 } 3079 #endif 3080 /* give a priority to WB_SYNC threads */ 3081 if (atomic_read(&sbi->wb_sync_req[DATA]) && 3082 wbc->sync_mode == WB_SYNC_NONE) { 3083 done = 1; 3084 break; 3085 } 3086 #ifdef CONFIG_F2FS_FS_COMPRESSION 3087 lock_folio: 3088 #endif 3089 done_index = folio->index; 3090 retry_write: 3091 folio_lock(folio); 3092 3093 if (unlikely(folio->mapping != mapping)) { 3094 continue_unlock: 3095 folio_unlock(folio); 3096 continue; 3097 } 3098 3099 if (!folio_test_dirty(folio)) { 3100 /* someone wrote it for us */ 3101 goto continue_unlock; 3102 } 3103 3104 if (folio_test_writeback(folio)) { 3105 if (wbc->sync_mode == WB_SYNC_NONE) 3106 goto continue_unlock; 3107 f2fs_folio_wait_writeback(folio, DATA, true, true); 3108 } 3109 3110 if (!folio_clear_dirty_for_io(folio)) 3111 goto continue_unlock; 3112 3113 #ifdef CONFIG_F2FS_FS_COMPRESSION 3114 if (f2fs_compressed_file(inode)) { 3115 folio_get(folio); 3116 f2fs_compress_ctx_add_page(&cc, folio); 3117 continue; 3118 } 3119 #endif 3120 submitted = 0; 3121 ret = f2fs_write_single_data_page(folio, 3122 &submitted, &bio, &last_block, 3123 wbc, io_type, 0, true); 3124 #ifdef CONFIG_F2FS_FS_COMPRESSION 3125 result: 3126 #endif 3127 nwritten += submitted; 3128 wbc->nr_to_write -= submitted; 3129 3130 if (unlikely(ret)) { 3131 /* 3132 * keep nr_to_write, since vfs uses this to 3133 * get # of written pages. 3134 */ 3135 if (ret == 1) { 3136 ret = 0; 3137 goto next; 3138 } else if (ret == -EAGAIN) { 3139 ret = 0; 3140 if (wbc->sync_mode == WB_SYNC_ALL) { 3141 f2fs_schedule_timeout( 3142 DEFAULT_SCHEDULE_TIMEOUT); 3143 goto retry_write; 3144 } 3145 goto next; 3146 } 3147 done_index = folio_next_index(folio); 3148 done = 1; 3149 break; 3150 } 3151 3152 if (wbc->nr_to_write <= 0 && 3153 wbc->sync_mode == WB_SYNC_NONE) { 3154 done = 1; 3155 break; 3156 } 3157 next: 3158 if (need_readd) 3159 goto readd; 3160 } 3161 release_pages(pages, nr_pages); 3162 cond_resched(); 3163 } 3164 #ifdef CONFIG_F2FS_FS_COMPRESSION 3165 /* flush remained pages in compress cluster */ 3166 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) { 3167 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type); 3168 nwritten += submitted; 3169 wbc->nr_to_write -= submitted; 3170 if (ret) { 3171 done = 1; 3172 retry = 0; 3173 } 3174 } 3175 if (f2fs_compressed_file(inode)) 3176 f2fs_destroy_compress_ctx(&cc, false); 3177 #endif 3178 if (retry) { 3179 index = 0; 3180 end = -1; 3181 goto retry; 3182 } 3183 if (wbc->range_cyclic && !done) 3184 done_index = 0; 3185 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 3186 mapping->writeback_index = done_index; 3187 3188 if (nwritten) 3189 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host, 3190 NULL, 0, DATA); 3191 /* submit cached bio of IPU write */ 3192 if (bio) 3193 f2fs_submit_merged_ipu_write(sbi, &bio, NULL); 3194 3195 #ifdef CONFIG_F2FS_FS_COMPRESSION 3196 if (pages != pages_local) 3197 kfree(pages); 3198 #endif 3199 3200 return ret; 3201 } 3202 3203 static inline bool __should_serialize_io(struct inode *inode, 3204 struct writeback_control *wbc) 3205 { 3206 /* to avoid deadlock in path of data flush */ 3207 if (F2FS_I(inode)->wb_task) 3208 return false; 3209 3210 if (!S_ISREG(inode->i_mode)) 3211 return false; 3212 if (IS_NOQUOTA(inode)) 3213 return false; 3214 3215 if (f2fs_need_compress_data(inode)) 3216 return true; 3217 if (wbc->sync_mode != WB_SYNC_ALL) 3218 return true; 3219 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks) 3220 return true; 3221 return false; 3222 } 3223 3224 static inline void account_writeback(struct inode *inode, bool inc) 3225 { 3226 if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) 3227 return; 3228 3229 f2fs_down_read(&F2FS_I(inode)->i_sem); 3230 if (inc) 3231 atomic_inc(&F2FS_I(inode)->writeback); 3232 else 3233 atomic_dec(&F2FS_I(inode)->writeback); 3234 f2fs_up_read(&F2FS_I(inode)->i_sem); 3235 } 3236 3237 static int __f2fs_write_data_pages(struct address_space *mapping, 3238 struct writeback_control *wbc, 3239 enum iostat_type io_type) 3240 { 3241 struct inode *inode = mapping->host; 3242 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3243 struct blk_plug plug; 3244 int ret; 3245 bool locked = false; 3246 3247 /* skip writing if there is no dirty page in this inode */ 3248 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) 3249 return 0; 3250 3251 /* during POR, we don't need to trigger writepage at all. */ 3252 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 3253 goto skip_write; 3254 3255 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) && 3256 wbc->sync_mode == WB_SYNC_NONE && 3257 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && 3258 f2fs_available_free_memory(sbi, DIRTY_DENTS)) 3259 goto skip_write; 3260 3261 /* skip writing in file defragment preparing stage */ 3262 if (is_inode_flag_set(inode, FI_SKIP_WRITES)) 3263 goto skip_write; 3264 3265 trace_f2fs_writepages(mapping->host, wbc, DATA); 3266 3267 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */ 3268 if (wbc->sync_mode == WB_SYNC_ALL) 3269 atomic_inc(&sbi->wb_sync_req[DATA]); 3270 else if (atomic_read(&sbi->wb_sync_req[DATA])) { 3271 /* to avoid potential deadlock */ 3272 if (current->plug) 3273 blk_finish_plug(current->plug); 3274 goto skip_write; 3275 } 3276 3277 if (__should_serialize_io(inode, wbc)) { 3278 mutex_lock(&sbi->writepages); 3279 locked = true; 3280 } 3281 3282 account_writeback(inode, true); 3283 3284 blk_start_plug(&plug); 3285 ret = f2fs_write_cache_pages(mapping, wbc, io_type); 3286 blk_finish_plug(&plug); 3287 3288 account_writeback(inode, false); 3289 3290 if (locked) 3291 mutex_unlock(&sbi->writepages); 3292 3293 if (wbc->sync_mode == WB_SYNC_ALL) 3294 atomic_dec(&sbi->wb_sync_req[DATA]); 3295 /* 3296 * if some pages were truncated, we cannot guarantee its mapping->host 3297 * to detect pending bios. 3298 */ 3299 3300 f2fs_remove_dirty_inode(inode); 3301 return ret; 3302 3303 skip_write: 3304 wbc->pages_skipped += get_dirty_pages(inode); 3305 trace_f2fs_writepages(mapping->host, wbc, DATA); 3306 return 0; 3307 } 3308 3309 static int f2fs_write_data_pages(struct address_space *mapping, 3310 struct writeback_control *wbc) 3311 { 3312 struct inode *inode = mapping->host; 3313 3314 return __f2fs_write_data_pages(mapping, wbc, 3315 F2FS_I(inode)->cp_task == current ? 3316 FS_CP_DATA_IO : FS_DATA_IO); 3317 } 3318 3319 void f2fs_write_failed(struct inode *inode, loff_t to) 3320 { 3321 loff_t i_size = i_size_read(inode); 3322 3323 if (IS_NOQUOTA(inode)) 3324 return; 3325 3326 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */ 3327 if (to > i_size && !f2fs_verity_in_progress(inode)) { 3328 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 3329 filemap_invalidate_lock(inode->i_mapping); 3330 3331 truncate_pagecache(inode, i_size); 3332 f2fs_truncate_blocks(inode, i_size, true); 3333 3334 filemap_invalidate_unlock(inode->i_mapping); 3335 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 3336 } 3337 } 3338 3339 static int prepare_write_begin(struct f2fs_sb_info *sbi, 3340 struct folio *folio, loff_t pos, unsigned int len, 3341 block_t *blk_addr, bool *node_changed) 3342 { 3343 struct inode *inode = folio->mapping->host; 3344 pgoff_t index = folio->index; 3345 struct dnode_of_data dn; 3346 struct folio *ifolio; 3347 bool locked = false; 3348 int flag = F2FS_GET_BLOCK_PRE_AIO; 3349 int err = 0; 3350 3351 /* 3352 * If a whole page is being written and we already preallocated all the 3353 * blocks, then there is no need to get a block address now. 3354 */ 3355 if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL)) 3356 return 0; 3357 3358 /* f2fs_lock_op avoids race between write CP and convert_inline_page */ 3359 if (f2fs_has_inline_data(inode)) { 3360 if (pos + len > MAX_INLINE_DATA(inode)) 3361 flag = F2FS_GET_BLOCK_DEFAULT; 3362 f2fs_map_lock(sbi, flag); 3363 locked = true; 3364 } else if ((pos & PAGE_MASK) >= i_size_read(inode)) { 3365 f2fs_map_lock(sbi, flag); 3366 locked = true; 3367 } 3368 3369 restart: 3370 /* check inline_data */ 3371 ifolio = f2fs_get_inode_folio(sbi, inode->i_ino); 3372 if (IS_ERR(ifolio)) { 3373 err = PTR_ERR(ifolio); 3374 goto unlock_out; 3375 } 3376 3377 set_new_dnode(&dn, inode, ifolio, ifolio, 0); 3378 3379 if (f2fs_has_inline_data(inode)) { 3380 if (pos + len <= MAX_INLINE_DATA(inode)) { 3381 f2fs_do_read_inline_data(folio, ifolio); 3382 set_inode_flag(inode, FI_DATA_EXIST); 3383 if (inode->i_nlink) 3384 folio_set_f2fs_inline(ifolio); 3385 goto out; 3386 } 3387 err = f2fs_convert_inline_folio(&dn, folio); 3388 if (err || dn.data_blkaddr != NULL_ADDR) 3389 goto out; 3390 } 3391 3392 if (!f2fs_lookup_read_extent_cache_block(inode, index, 3393 &dn.data_blkaddr)) { 3394 if (IS_DEVICE_ALIASING(inode)) { 3395 err = -ENODATA; 3396 goto out; 3397 } 3398 3399 if (locked) { 3400 err = f2fs_reserve_block(&dn, index); 3401 goto out; 3402 } 3403 3404 /* hole case */ 3405 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 3406 if (!err && dn.data_blkaddr != NULL_ADDR) 3407 goto out; 3408 f2fs_put_dnode(&dn); 3409 f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO); 3410 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO); 3411 locked = true; 3412 goto restart; 3413 } 3414 out: 3415 if (!err) { 3416 /* convert_inline_page can make node_changed */ 3417 *blk_addr = dn.data_blkaddr; 3418 *node_changed = dn.node_changed; 3419 } 3420 f2fs_put_dnode(&dn); 3421 unlock_out: 3422 if (locked) 3423 f2fs_map_unlock(sbi, flag); 3424 return err; 3425 } 3426 3427 static int __find_data_block(struct inode *inode, pgoff_t index, 3428 block_t *blk_addr) 3429 { 3430 struct dnode_of_data dn; 3431 struct folio *ifolio; 3432 int err = 0; 3433 3434 ifolio = f2fs_get_inode_folio(F2FS_I_SB(inode), inode->i_ino); 3435 if (IS_ERR(ifolio)) 3436 return PTR_ERR(ifolio); 3437 3438 set_new_dnode(&dn, inode, ifolio, ifolio, 0); 3439 3440 if (!f2fs_lookup_read_extent_cache_block(inode, index, 3441 &dn.data_blkaddr)) { 3442 /* hole case */ 3443 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 3444 if (err) { 3445 dn.data_blkaddr = NULL_ADDR; 3446 err = 0; 3447 } 3448 } 3449 *blk_addr = dn.data_blkaddr; 3450 f2fs_put_dnode(&dn); 3451 return err; 3452 } 3453 3454 static int __reserve_data_block(struct inode *inode, pgoff_t index, 3455 block_t *blk_addr, bool *node_changed) 3456 { 3457 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3458 struct dnode_of_data dn; 3459 struct folio *ifolio; 3460 int err = 0; 3461 3462 f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO); 3463 3464 ifolio = f2fs_get_inode_folio(sbi, inode->i_ino); 3465 if (IS_ERR(ifolio)) { 3466 err = PTR_ERR(ifolio); 3467 goto unlock_out; 3468 } 3469 set_new_dnode(&dn, inode, ifolio, ifolio, 0); 3470 3471 if (!f2fs_lookup_read_extent_cache_block(dn.inode, index, 3472 &dn.data_blkaddr)) 3473 err = f2fs_reserve_block(&dn, index); 3474 3475 *blk_addr = dn.data_blkaddr; 3476 *node_changed = dn.node_changed; 3477 f2fs_put_dnode(&dn); 3478 3479 unlock_out: 3480 f2fs_map_unlock(sbi, F2FS_GET_BLOCK_PRE_AIO); 3481 return err; 3482 } 3483 3484 static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi, 3485 struct folio *folio, loff_t pos, unsigned int len, 3486 block_t *blk_addr, bool *node_changed, bool *use_cow) 3487 { 3488 struct inode *inode = folio->mapping->host; 3489 struct inode *cow_inode = F2FS_I(inode)->cow_inode; 3490 pgoff_t index = folio->index; 3491 int err = 0; 3492 block_t ori_blk_addr = NULL_ADDR; 3493 3494 /* If pos is beyond the end of file, reserve a new block in COW inode */ 3495 if ((pos & PAGE_MASK) >= i_size_read(inode)) 3496 goto reserve_block; 3497 3498 /* Look for the block in COW inode first */ 3499 err = __find_data_block(cow_inode, index, blk_addr); 3500 if (err) { 3501 return err; 3502 } else if (*blk_addr != NULL_ADDR) { 3503 *use_cow = true; 3504 return 0; 3505 } 3506 3507 if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE)) 3508 goto reserve_block; 3509 3510 /* Look for the block in the original inode */ 3511 err = __find_data_block(inode, index, &ori_blk_addr); 3512 if (err) 3513 return err; 3514 3515 reserve_block: 3516 /* Finally, we should reserve a new block in COW inode for the update */ 3517 err = __reserve_data_block(cow_inode, index, blk_addr, node_changed); 3518 if (err) 3519 return err; 3520 inc_atomic_write_cnt(inode); 3521 3522 if (ori_blk_addr != NULL_ADDR) 3523 *blk_addr = ori_blk_addr; 3524 return 0; 3525 } 3526 3527 static int f2fs_write_begin(const struct kiocb *iocb, 3528 struct address_space *mapping, 3529 loff_t pos, unsigned len, struct folio **foliop, 3530 void **fsdata) 3531 { 3532 struct inode *inode = mapping->host; 3533 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3534 struct folio *folio; 3535 pgoff_t index = pos >> PAGE_SHIFT; 3536 bool need_balance = false; 3537 bool use_cow = false; 3538 block_t blkaddr = NULL_ADDR; 3539 int err = 0; 3540 3541 trace_f2fs_write_begin(inode, pos, len); 3542 3543 if (!f2fs_is_checkpoint_ready(sbi)) { 3544 err = -ENOSPC; 3545 goto fail; 3546 } 3547 3548 /* 3549 * We should check this at this moment to avoid deadlock on inode page 3550 * and #0 page. The locking rule for inline_data conversion should be: 3551 * folio_lock(folio #0) -> folio_lock(inode_page) 3552 */ 3553 if (index != 0) { 3554 err = f2fs_convert_inline_inode(inode); 3555 if (err) 3556 goto fail; 3557 } 3558 3559 #ifdef CONFIG_F2FS_FS_COMPRESSION 3560 if (f2fs_compressed_file(inode)) { 3561 int ret; 3562 struct page *page; 3563 3564 *fsdata = NULL; 3565 3566 if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode))) 3567 goto repeat; 3568 3569 ret = f2fs_prepare_compress_overwrite(inode, &page, 3570 index, fsdata); 3571 if (ret < 0) { 3572 err = ret; 3573 goto fail; 3574 } else if (ret) { 3575 *foliop = page_folio(page); 3576 return 0; 3577 } 3578 } 3579 #endif 3580 3581 repeat: 3582 /* 3583 * Do not use FGP_STABLE to avoid deadlock. 3584 * Will wait that below with our IO control. 3585 */ 3586 folio = f2fs_filemap_get_folio(mapping, index, 3587 FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_NOFS, 3588 mapping_gfp_mask(mapping)); 3589 if (IS_ERR(folio)) { 3590 err = PTR_ERR(folio); 3591 goto fail; 3592 } 3593 3594 /* TODO: cluster can be compressed due to race with .writepage */ 3595 3596 *foliop = folio; 3597 3598 if (f2fs_is_atomic_file(inode)) 3599 err = prepare_atomic_write_begin(sbi, folio, pos, len, 3600 &blkaddr, &need_balance, &use_cow); 3601 else 3602 err = prepare_write_begin(sbi, folio, pos, len, 3603 &blkaddr, &need_balance); 3604 if (err) 3605 goto put_folio; 3606 3607 if (need_balance && !IS_NOQUOTA(inode) && 3608 has_not_enough_free_secs(sbi, 0, 0)) { 3609 folio_unlock(folio); 3610 f2fs_balance_fs(sbi, true); 3611 folio_lock(folio); 3612 if (folio->mapping != mapping) { 3613 /* The folio got truncated from under us */ 3614 folio_unlock(folio); 3615 folio_put(folio); 3616 goto repeat; 3617 } 3618 } 3619 3620 f2fs_folio_wait_writeback(folio, DATA, false, true); 3621 3622 if (len == folio_size(folio) || folio_test_uptodate(folio)) 3623 return 0; 3624 3625 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) && 3626 !f2fs_verity_in_progress(inode)) { 3627 folio_zero_segment(folio, len, folio_size(folio)); 3628 return 0; 3629 } 3630 3631 if (blkaddr == NEW_ADDR) { 3632 folio_zero_segment(folio, 0, folio_size(folio)); 3633 folio_mark_uptodate(folio); 3634 } else { 3635 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, 3636 DATA_GENERIC_ENHANCE_READ)) { 3637 err = -EFSCORRUPTED; 3638 goto put_folio; 3639 } 3640 f2fs_submit_page_read(use_cow ? 3641 F2FS_I(inode)->cow_inode : inode, 3642 folio, blkaddr, 0, true); 3643 3644 folio_lock(folio); 3645 if (unlikely(folio->mapping != mapping)) { 3646 folio_unlock(folio); 3647 folio_put(folio); 3648 goto repeat; 3649 } 3650 if (unlikely(!folio_test_uptodate(folio))) { 3651 err = -EIO; 3652 goto put_folio; 3653 } 3654 } 3655 return 0; 3656 3657 put_folio: 3658 f2fs_folio_put(folio, true); 3659 fail: 3660 f2fs_write_failed(inode, pos + len); 3661 return err; 3662 } 3663 3664 static int f2fs_write_end(const struct kiocb *iocb, 3665 struct address_space *mapping, 3666 loff_t pos, unsigned len, unsigned copied, 3667 struct folio *folio, void *fsdata) 3668 { 3669 struct inode *inode = folio->mapping->host; 3670 3671 trace_f2fs_write_end(inode, pos, len, copied); 3672 3673 /* 3674 * This should be come from len == PAGE_SIZE, and we expect copied 3675 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and 3676 * let generic_perform_write() try to copy data again through copied=0. 3677 */ 3678 if (!folio_test_uptodate(folio)) { 3679 if (unlikely(copied != len)) 3680 copied = 0; 3681 else 3682 folio_mark_uptodate(folio); 3683 } 3684 3685 #ifdef CONFIG_F2FS_FS_COMPRESSION 3686 /* overwrite compressed file */ 3687 if (f2fs_compressed_file(inode) && fsdata) { 3688 f2fs_compress_write_end(inode, fsdata, folio->index, copied); 3689 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 3690 3691 if (pos + copied > i_size_read(inode) && 3692 !f2fs_verity_in_progress(inode)) 3693 f2fs_i_size_write(inode, pos + copied); 3694 return copied; 3695 } 3696 #endif 3697 3698 if (!copied) 3699 goto unlock_out; 3700 3701 folio_mark_dirty(folio); 3702 3703 if (f2fs_is_atomic_file(inode)) 3704 folio_set_f2fs_atomic(folio); 3705 3706 if (pos + copied > i_size_read(inode) && 3707 !f2fs_verity_in_progress(inode)) { 3708 f2fs_i_size_write(inode, pos + copied); 3709 if (f2fs_is_atomic_file(inode)) 3710 f2fs_i_size_write(F2FS_I(inode)->cow_inode, 3711 pos + copied); 3712 } 3713 unlock_out: 3714 f2fs_folio_put(folio, true); 3715 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 3716 return copied; 3717 } 3718 3719 void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length) 3720 { 3721 struct inode *inode = folio->mapping->host; 3722 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3723 3724 if (inode->i_ino >= F2FS_ROOT_INO(sbi) && 3725 (offset || length != folio_size(folio))) 3726 return; 3727 3728 if (folio_test_dirty(folio)) { 3729 if (inode->i_ino == F2FS_META_INO(sbi)) { 3730 dec_page_count(sbi, F2FS_DIRTY_META); 3731 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) { 3732 dec_page_count(sbi, F2FS_DIRTY_NODES); 3733 } else { 3734 inode_dec_dirty_pages(inode); 3735 f2fs_remove_dirty_inode(inode); 3736 } 3737 } 3738 folio_detach_private(folio); 3739 } 3740 3741 bool f2fs_release_folio(struct folio *folio, gfp_t wait) 3742 { 3743 /* If this is dirty folio, keep private data */ 3744 if (folio_test_dirty(folio)) 3745 return false; 3746 3747 folio_detach_private(folio); 3748 return true; 3749 } 3750 3751 static bool f2fs_dirty_data_folio(struct address_space *mapping, 3752 struct folio *folio) 3753 { 3754 struct inode *inode = mapping->host; 3755 3756 trace_f2fs_set_page_dirty(folio, DATA); 3757 3758 if (!folio_test_uptodate(folio)) 3759 folio_mark_uptodate(folio); 3760 BUG_ON(folio_test_swapcache(folio)); 3761 3762 if (filemap_dirty_folio(mapping, folio)) { 3763 f2fs_update_dirty_folio(inode, folio); 3764 return true; 3765 } 3766 return false; 3767 } 3768 3769 3770 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block) 3771 { 3772 #ifdef CONFIG_F2FS_FS_COMPRESSION 3773 struct dnode_of_data dn; 3774 sector_t start_idx, blknr = 0; 3775 int ret; 3776 3777 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size); 3778 3779 set_new_dnode(&dn, inode, NULL, NULL, 0); 3780 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); 3781 if (ret) 3782 return 0; 3783 3784 if (dn.data_blkaddr != COMPRESS_ADDR) { 3785 dn.ofs_in_node += block - start_idx; 3786 blknr = f2fs_data_blkaddr(&dn); 3787 if (!__is_valid_data_blkaddr(blknr)) 3788 blknr = 0; 3789 } 3790 3791 f2fs_put_dnode(&dn); 3792 return blknr; 3793 #else 3794 return 0; 3795 #endif 3796 } 3797 3798 3799 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) 3800 { 3801 struct inode *inode = mapping->host; 3802 sector_t blknr = 0; 3803 3804 if (f2fs_has_inline_data(inode)) 3805 goto out; 3806 3807 /* make sure allocating whole blocks */ 3808 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 3809 filemap_write_and_wait(mapping); 3810 3811 /* Block number less than F2FS MAX BLOCKS */ 3812 if (unlikely(block >= max_file_blocks(inode))) 3813 goto out; 3814 3815 if (f2fs_compressed_file(inode)) { 3816 blknr = f2fs_bmap_compress(inode, block); 3817 } else { 3818 struct f2fs_map_blocks map; 3819 3820 memset(&map, 0, sizeof(map)); 3821 map.m_lblk = block; 3822 map.m_len = 1; 3823 map.m_next_pgofs = NULL; 3824 map.m_seg_type = NO_CHECK_TYPE; 3825 3826 if (!f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_BMAP)) 3827 blknr = map.m_pblk; 3828 } 3829 out: 3830 trace_f2fs_bmap(inode, block, blknr); 3831 return blknr; 3832 } 3833 3834 #ifdef CONFIG_SWAP 3835 static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk, 3836 unsigned int blkcnt) 3837 { 3838 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3839 unsigned int blkofs; 3840 unsigned int blk_per_sec = BLKS_PER_SEC(sbi); 3841 unsigned int end_blk = start_blk + blkcnt - 1; 3842 unsigned int secidx = start_blk / blk_per_sec; 3843 unsigned int end_sec; 3844 int ret = 0; 3845 3846 if (!blkcnt) 3847 return 0; 3848 end_sec = end_blk / blk_per_sec; 3849 3850 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 3851 filemap_invalidate_lock(inode->i_mapping); 3852 3853 set_inode_flag(inode, FI_ALIGNED_WRITE); 3854 set_inode_flag(inode, FI_OPU_WRITE); 3855 3856 for (; secidx <= end_sec; secidx++) { 3857 unsigned int blkofs_end = secidx == end_sec ? 3858 end_blk % blk_per_sec : blk_per_sec - 1; 3859 3860 f2fs_down_write(&sbi->pin_sem); 3861 3862 ret = f2fs_allocate_pinning_section(sbi); 3863 if (ret) { 3864 f2fs_up_write(&sbi->pin_sem); 3865 break; 3866 } 3867 3868 set_inode_flag(inode, FI_SKIP_WRITES); 3869 3870 for (blkofs = 0; blkofs <= blkofs_end; blkofs++) { 3871 struct folio *folio; 3872 unsigned int blkidx = secidx * blk_per_sec + blkofs; 3873 3874 folio = f2fs_get_lock_data_folio(inode, blkidx, true); 3875 if (IS_ERR(folio)) { 3876 f2fs_up_write(&sbi->pin_sem); 3877 ret = PTR_ERR(folio); 3878 goto done; 3879 } 3880 3881 folio_mark_dirty(folio); 3882 f2fs_folio_put(folio, true); 3883 } 3884 3885 clear_inode_flag(inode, FI_SKIP_WRITES); 3886 3887 ret = filemap_fdatawrite(inode->i_mapping); 3888 3889 f2fs_up_write(&sbi->pin_sem); 3890 3891 if (ret) 3892 break; 3893 } 3894 3895 done: 3896 clear_inode_flag(inode, FI_SKIP_WRITES); 3897 clear_inode_flag(inode, FI_OPU_WRITE); 3898 clear_inode_flag(inode, FI_ALIGNED_WRITE); 3899 3900 filemap_invalidate_unlock(inode->i_mapping); 3901 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 3902 3903 return ret; 3904 } 3905 3906 static int check_swap_activate(struct swap_info_struct *sis, 3907 struct file *swap_file, sector_t *span) 3908 { 3909 struct address_space *mapping = swap_file->f_mapping; 3910 struct inode *inode = mapping->host; 3911 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3912 block_t cur_lblock; 3913 block_t last_lblock; 3914 block_t pblock; 3915 block_t lowest_pblock = -1; 3916 block_t highest_pblock = 0; 3917 int nr_extents = 0; 3918 unsigned int nr_pblocks; 3919 unsigned int blks_per_sec = BLKS_PER_SEC(sbi); 3920 unsigned int not_aligned = 0; 3921 int ret = 0; 3922 3923 /* 3924 * Map all the blocks into the extent list. This code doesn't try 3925 * to be very smart. 3926 */ 3927 cur_lblock = 0; 3928 last_lblock = F2FS_BYTES_TO_BLK(i_size_read(inode)); 3929 3930 while (cur_lblock < last_lblock && cur_lblock < sis->max) { 3931 struct f2fs_map_blocks map; 3932 retry: 3933 cond_resched(); 3934 3935 memset(&map, 0, sizeof(map)); 3936 map.m_lblk = cur_lblock; 3937 map.m_len = last_lblock - cur_lblock; 3938 map.m_next_pgofs = NULL; 3939 map.m_next_extent = NULL; 3940 map.m_seg_type = NO_CHECK_TYPE; 3941 map.m_may_create = false; 3942 3943 ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP); 3944 if (ret) 3945 goto out; 3946 3947 /* hole */ 3948 if (!(map.m_flags & F2FS_MAP_FLAGS)) { 3949 f2fs_err(sbi, "Swapfile has holes"); 3950 ret = -EINVAL; 3951 goto out; 3952 } 3953 3954 pblock = map.m_pblk; 3955 nr_pblocks = map.m_len; 3956 3957 if ((pblock - SM_I(sbi)->main_blkaddr) % blks_per_sec || 3958 nr_pblocks % blks_per_sec || 3959 f2fs_is_sequential_zone_area(sbi, pblock)) { 3960 bool last_extent = false; 3961 3962 not_aligned++; 3963 3964 nr_pblocks = roundup(nr_pblocks, blks_per_sec); 3965 if (cur_lblock + nr_pblocks > sis->max) 3966 nr_pblocks -= blks_per_sec; 3967 3968 /* this extent is last one */ 3969 if (!nr_pblocks) { 3970 nr_pblocks = last_lblock - cur_lblock; 3971 last_extent = true; 3972 } 3973 3974 ret = f2fs_migrate_blocks(inode, cur_lblock, 3975 nr_pblocks); 3976 if (ret) { 3977 if (ret == -ENOENT) 3978 ret = -EINVAL; 3979 goto out; 3980 } 3981 3982 if (!last_extent) 3983 goto retry; 3984 } 3985 3986 if (cur_lblock + nr_pblocks >= sis->max) 3987 nr_pblocks = sis->max - cur_lblock; 3988 3989 if (cur_lblock) { /* exclude the header page */ 3990 if (pblock < lowest_pblock) 3991 lowest_pblock = pblock; 3992 if (pblock + nr_pblocks - 1 > highest_pblock) 3993 highest_pblock = pblock + nr_pblocks - 1; 3994 } 3995 3996 /* 3997 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks 3998 */ 3999 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock); 4000 if (ret < 0) 4001 goto out; 4002 nr_extents += ret; 4003 cur_lblock += nr_pblocks; 4004 } 4005 ret = nr_extents; 4006 *span = 1 + highest_pblock - lowest_pblock; 4007 if (cur_lblock == 0) 4008 cur_lblock = 1; /* force Empty message */ 4009 sis->max = cur_lblock; 4010 sis->pages = cur_lblock - 1; 4011 out: 4012 if (not_aligned) 4013 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%lu * N)", 4014 not_aligned, blks_per_sec * F2FS_BLKSIZE); 4015 return ret; 4016 } 4017 4018 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, 4019 sector_t *span) 4020 { 4021 struct inode *inode = file_inode(file); 4022 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4023 int ret; 4024 4025 if (!S_ISREG(inode->i_mode)) 4026 return -EINVAL; 4027 4028 if (f2fs_readonly(sbi->sb)) 4029 return -EROFS; 4030 4031 if (f2fs_lfs_mode(sbi) && !f2fs_sb_has_blkzoned(sbi)) { 4032 f2fs_err(sbi, "Swapfile not supported in LFS mode"); 4033 return -EINVAL; 4034 } 4035 4036 ret = f2fs_convert_inline_inode(inode); 4037 if (ret) 4038 return ret; 4039 4040 if (!f2fs_disable_compressed_file(inode)) 4041 return -EINVAL; 4042 4043 ret = filemap_fdatawrite(inode->i_mapping); 4044 if (ret < 0) 4045 return ret; 4046 4047 f2fs_precache_extents(inode); 4048 4049 ret = check_swap_activate(sis, file, span); 4050 if (ret < 0) 4051 return ret; 4052 4053 stat_inc_swapfile_inode(inode); 4054 set_inode_flag(inode, FI_PIN_FILE); 4055 f2fs_update_time(sbi, REQ_TIME); 4056 return ret; 4057 } 4058 4059 static void f2fs_swap_deactivate(struct file *file) 4060 { 4061 struct inode *inode = file_inode(file); 4062 4063 stat_dec_swapfile_inode(inode); 4064 clear_inode_flag(inode, FI_PIN_FILE); 4065 } 4066 #else 4067 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, 4068 sector_t *span) 4069 { 4070 return -EOPNOTSUPP; 4071 } 4072 4073 static void f2fs_swap_deactivate(struct file *file) 4074 { 4075 } 4076 #endif 4077 4078 const struct address_space_operations f2fs_dblock_aops = { 4079 .read_folio = f2fs_read_data_folio, 4080 .readahead = f2fs_readahead, 4081 .writepages = f2fs_write_data_pages, 4082 .write_begin = f2fs_write_begin, 4083 .write_end = f2fs_write_end, 4084 .dirty_folio = f2fs_dirty_data_folio, 4085 .migrate_folio = filemap_migrate_folio, 4086 .invalidate_folio = f2fs_invalidate_folio, 4087 .release_folio = f2fs_release_folio, 4088 .bmap = f2fs_bmap, 4089 .swap_activate = f2fs_swap_activate, 4090 .swap_deactivate = f2fs_swap_deactivate, 4091 }; 4092 4093 void f2fs_clear_page_cache_dirty_tag(struct folio *folio) 4094 { 4095 struct address_space *mapping = folio->mapping; 4096 unsigned long flags; 4097 4098 xa_lock_irqsave(&mapping->i_pages, flags); 4099 __xa_clear_mark(&mapping->i_pages, folio->index, 4100 PAGECACHE_TAG_DIRTY); 4101 xa_unlock_irqrestore(&mapping->i_pages, flags); 4102 } 4103 4104 int __init f2fs_init_post_read_processing(void) 4105 { 4106 bio_post_read_ctx_cache = 4107 kmem_cache_create("f2fs_bio_post_read_ctx", 4108 sizeof(struct bio_post_read_ctx), 0, 0, NULL); 4109 if (!bio_post_read_ctx_cache) 4110 goto fail; 4111 bio_post_read_ctx_pool = 4112 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS, 4113 bio_post_read_ctx_cache); 4114 if (!bio_post_read_ctx_pool) 4115 goto fail_free_cache; 4116 return 0; 4117 4118 fail_free_cache: 4119 kmem_cache_destroy(bio_post_read_ctx_cache); 4120 fail: 4121 return -ENOMEM; 4122 } 4123 4124 void f2fs_destroy_post_read_processing(void) 4125 { 4126 mempool_destroy(bio_post_read_ctx_pool); 4127 kmem_cache_destroy(bio_post_read_ctx_cache); 4128 } 4129 4130 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi) 4131 { 4132 if (!f2fs_sb_has_encrypt(sbi) && 4133 !f2fs_sb_has_verity(sbi) && 4134 !f2fs_sb_has_compression(sbi)) 4135 return 0; 4136 4137 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq", 4138 WQ_UNBOUND | WQ_HIGHPRI, 4139 num_online_cpus()); 4140 return sbi->post_read_wq ? 0 : -ENOMEM; 4141 } 4142 4143 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi) 4144 { 4145 if (sbi->post_read_wq) 4146 destroy_workqueue(sbi->post_read_wq); 4147 } 4148 4149 int __init f2fs_init_bio_entry_cache(void) 4150 { 4151 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab", 4152 sizeof(struct bio_entry)); 4153 return bio_entry_slab ? 0 : -ENOMEM; 4154 } 4155 4156 void f2fs_destroy_bio_entry_cache(void) 4157 { 4158 kmem_cache_destroy(bio_entry_slab); 4159 } 4160 4161 static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 4162 unsigned int flags, struct iomap *iomap, 4163 struct iomap *srcmap) 4164 { 4165 struct f2fs_map_blocks map = { NULL, }; 4166 pgoff_t next_pgofs = 0; 4167 int err; 4168 4169 map.m_lblk = F2FS_BYTES_TO_BLK(offset); 4170 map.m_len = F2FS_BYTES_TO_BLK(offset + length - 1) - map.m_lblk + 1; 4171 map.m_next_pgofs = &next_pgofs; 4172 map.m_seg_type = f2fs_rw_hint_to_seg_type(F2FS_I_SB(inode), 4173 inode->i_write_hint); 4174 if (flags & IOMAP_WRITE && iomap->private) { 4175 map.m_last_pblk = (unsigned long)iomap->private; 4176 iomap->private = NULL; 4177 } 4178 4179 /* 4180 * If the blocks being overwritten are already allocated, 4181 * f2fs_map_lock and f2fs_balance_fs are not necessary. 4182 */ 4183 if ((flags & IOMAP_WRITE) && 4184 !f2fs_overwrite_io(inode, offset, length)) 4185 map.m_may_create = true; 4186 4187 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DIO); 4188 if (err) 4189 return err; 4190 4191 iomap->offset = F2FS_BLK_TO_BYTES(map.m_lblk); 4192 4193 /* 4194 * When inline encryption is enabled, sometimes I/O to an encrypted file 4195 * has to be broken up to guarantee DUN contiguity. Handle this by 4196 * limiting the length of the mapping returned. 4197 */ 4198 map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len); 4199 4200 /* 4201 * We should never see delalloc or compressed extents here based on 4202 * prior flushing and checks. 4203 */ 4204 if (WARN_ON_ONCE(map.m_pblk == COMPRESS_ADDR)) 4205 return -EINVAL; 4206 4207 if (map.m_flags & F2FS_MAP_MAPPED) { 4208 if (WARN_ON_ONCE(map.m_pblk == NEW_ADDR)) 4209 return -EINVAL; 4210 4211 iomap->length = F2FS_BLK_TO_BYTES(map.m_len); 4212 iomap->type = IOMAP_MAPPED; 4213 iomap->flags |= IOMAP_F_MERGED; 4214 iomap->bdev = map.m_bdev; 4215 iomap->addr = F2FS_BLK_TO_BYTES(map.m_pblk); 4216 4217 if (flags & IOMAP_WRITE && map.m_last_pblk) 4218 iomap->private = (void *)map.m_last_pblk; 4219 } else { 4220 if (flags & IOMAP_WRITE) 4221 return -ENOTBLK; 4222 4223 if (map.m_pblk == NULL_ADDR) { 4224 iomap->length = F2FS_BLK_TO_BYTES(next_pgofs) - 4225 iomap->offset; 4226 iomap->type = IOMAP_HOLE; 4227 } else if (map.m_pblk == NEW_ADDR) { 4228 iomap->length = F2FS_BLK_TO_BYTES(map.m_len); 4229 iomap->type = IOMAP_UNWRITTEN; 4230 } else { 4231 f2fs_bug_on(F2FS_I_SB(inode), 1); 4232 } 4233 iomap->addr = IOMAP_NULL_ADDR; 4234 } 4235 4236 if (map.m_flags & F2FS_MAP_NEW) 4237 iomap->flags |= IOMAP_F_NEW; 4238 if ((inode_state_read_once(inode) & I_DIRTY_DATASYNC) || 4239 offset + length > i_size_read(inode)) 4240 iomap->flags |= IOMAP_F_DIRTY; 4241 4242 return 0; 4243 } 4244 4245 const struct iomap_ops f2fs_iomap_ops = { 4246 .iomap_begin = f2fs_iomap_begin, 4247 }; 4248