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