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