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