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