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) && 2057 (IS_VERITY(inode) || f2fs_verity_in_progress(inode))) 2058 return inode->i_sb->s_maxbytes; 2059 2060 return i_size_read(inode); 2061 } 2062 2063 static int f2fs_read_single_page(struct inode *inode, struct page *page, 2064 unsigned nr_pages, 2065 struct f2fs_map_blocks *map, 2066 struct bio **bio_ret, 2067 sector_t *last_block_in_bio, 2068 bool is_readahead) 2069 { 2070 struct bio *bio = *bio_ret; 2071 const unsigned blocksize = blks_to_bytes(inode, 1); 2072 sector_t block_in_file; 2073 sector_t last_block; 2074 sector_t last_block_in_file; 2075 sector_t block_nr; 2076 int ret = 0; 2077 2078 block_in_file = (sector_t)page_index(page); 2079 last_block = block_in_file + nr_pages; 2080 last_block_in_file = bytes_to_blks(inode, 2081 f2fs_readpage_limit(inode) + blocksize - 1); 2082 if (last_block > last_block_in_file) 2083 last_block = last_block_in_file; 2084 2085 /* just zeroing out page which is beyond EOF */ 2086 if (block_in_file >= last_block) 2087 goto zero_out; 2088 /* 2089 * Map blocks using the previous result first. 2090 */ 2091 if ((map->m_flags & F2FS_MAP_MAPPED) && 2092 block_in_file > map->m_lblk && 2093 block_in_file < (map->m_lblk + map->m_len)) 2094 goto got_it; 2095 2096 /* 2097 * Then do more f2fs_map_blocks() calls until we are 2098 * done with this page. 2099 */ 2100 map->m_lblk = block_in_file; 2101 map->m_len = last_block - block_in_file; 2102 2103 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT); 2104 if (ret) 2105 goto out; 2106 got_it: 2107 if ((map->m_flags & F2FS_MAP_MAPPED)) { 2108 block_nr = map->m_pblk + block_in_file - map->m_lblk; 2109 SetPageMappedToDisk(page); 2110 2111 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr, 2112 DATA_GENERIC_ENHANCE_READ)) { 2113 ret = -EFSCORRUPTED; 2114 f2fs_handle_error(F2FS_I_SB(inode), 2115 ERROR_INVALID_BLKADDR); 2116 goto out; 2117 } 2118 } else { 2119 zero_out: 2120 zero_user_segment(page, 0, PAGE_SIZE); 2121 if (f2fs_need_verity(inode, page->index) && 2122 !fsverity_verify_page(page)) { 2123 ret = -EIO; 2124 goto out; 2125 } 2126 if (!PageUptodate(page)) 2127 SetPageUptodate(page); 2128 unlock_page(page); 2129 goto out; 2130 } 2131 2132 /* 2133 * This page will go to BIO. Do we need to send this 2134 * BIO off first? 2135 */ 2136 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio, 2137 *last_block_in_bio, block_nr) || 2138 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) { 2139 submit_and_realloc: 2140 __submit_bio(F2FS_I_SB(inode), bio, DATA); 2141 bio = NULL; 2142 } 2143 if (bio == NULL) { 2144 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages, 2145 is_readahead ? REQ_RAHEAD : 0, page->index, 2146 false); 2147 if (IS_ERR(bio)) { 2148 ret = PTR_ERR(bio); 2149 bio = NULL; 2150 goto out; 2151 } 2152 } 2153 2154 /* 2155 * If the page is under writeback, we need to wait for 2156 * its completion to see the correct decrypted data. 2157 */ 2158 f2fs_wait_on_block_writeback(inode, block_nr); 2159 2160 if (bio_add_page(bio, page, blocksize, 0) < blocksize) 2161 goto submit_and_realloc; 2162 2163 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA); 2164 f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO, 2165 F2FS_BLKSIZE); 2166 *last_block_in_bio = block_nr; 2167 goto out; 2168 out: 2169 *bio_ret = bio; 2170 return ret; 2171 } 2172 2173 #ifdef CONFIG_F2FS_FS_COMPRESSION 2174 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret, 2175 unsigned nr_pages, sector_t *last_block_in_bio, 2176 bool is_readahead, bool for_write) 2177 { 2178 struct dnode_of_data dn; 2179 struct inode *inode = cc->inode; 2180 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2181 struct bio *bio = *bio_ret; 2182 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size; 2183 sector_t last_block_in_file; 2184 const unsigned blocksize = blks_to_bytes(inode, 1); 2185 struct decompress_io_ctx *dic = NULL; 2186 struct extent_info ei = {0, }; 2187 bool from_dnode = true; 2188 int i; 2189 int ret = 0; 2190 2191 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc)); 2192 2193 last_block_in_file = bytes_to_blks(inode, 2194 f2fs_readpage_limit(inode) + blocksize - 1); 2195 2196 /* get rid of pages beyond EOF */ 2197 for (i = 0; i < cc->cluster_size; i++) { 2198 struct page *page = cc->rpages[i]; 2199 2200 if (!page) 2201 continue; 2202 if ((sector_t)page->index >= last_block_in_file) { 2203 zero_user_segment(page, 0, PAGE_SIZE); 2204 if (!PageUptodate(page)) 2205 SetPageUptodate(page); 2206 } else if (!PageUptodate(page)) { 2207 continue; 2208 } 2209 unlock_page(page); 2210 if (for_write) 2211 put_page(page); 2212 cc->rpages[i] = NULL; 2213 cc->nr_rpages--; 2214 } 2215 2216 /* we are done since all pages are beyond EOF */ 2217 if (f2fs_cluster_is_empty(cc)) 2218 goto out; 2219 2220 if (f2fs_lookup_read_extent_cache(inode, start_idx, &ei)) 2221 from_dnode = false; 2222 2223 if (!from_dnode) 2224 goto skip_reading_dnode; 2225 2226 set_new_dnode(&dn, inode, NULL, NULL, 0); 2227 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); 2228 if (ret) 2229 goto out; 2230 2231 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR); 2232 2233 skip_reading_dnode: 2234 for (i = 1; i < cc->cluster_size; i++) { 2235 block_t blkaddr; 2236 2237 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page, 2238 dn.ofs_in_node + i) : 2239 ei.blk + i - 1; 2240 2241 if (!__is_valid_data_blkaddr(blkaddr)) 2242 break; 2243 2244 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) { 2245 ret = -EFAULT; 2246 goto out_put_dnode; 2247 } 2248 cc->nr_cpages++; 2249 2250 if (!from_dnode && i >= ei.c_len) 2251 break; 2252 } 2253 2254 /* nothing to decompress */ 2255 if (cc->nr_cpages == 0) { 2256 ret = 0; 2257 goto out_put_dnode; 2258 } 2259 2260 dic = f2fs_alloc_dic(cc); 2261 if (IS_ERR(dic)) { 2262 ret = PTR_ERR(dic); 2263 goto out_put_dnode; 2264 } 2265 2266 for (i = 0; i < cc->nr_cpages; i++) { 2267 struct page *page = dic->cpages[i]; 2268 block_t blkaddr; 2269 struct bio_post_read_ctx *ctx; 2270 2271 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page, 2272 dn.ofs_in_node + i + 1) : 2273 ei.blk + i; 2274 2275 f2fs_wait_on_block_writeback(inode, blkaddr); 2276 2277 if (f2fs_load_compressed_page(sbi, page, blkaddr)) { 2278 if (atomic_dec_and_test(&dic->remaining_pages)) 2279 f2fs_decompress_cluster(dic, true); 2280 continue; 2281 } 2282 2283 if (bio && (!page_is_mergeable(sbi, bio, 2284 *last_block_in_bio, blkaddr) || 2285 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) { 2286 submit_and_realloc: 2287 __submit_bio(sbi, bio, DATA); 2288 bio = NULL; 2289 } 2290 2291 if (!bio) { 2292 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages, 2293 is_readahead ? REQ_RAHEAD : 0, 2294 page->index, for_write); 2295 if (IS_ERR(bio)) { 2296 ret = PTR_ERR(bio); 2297 f2fs_decompress_end_io(dic, ret, true); 2298 f2fs_put_dnode(&dn); 2299 *bio_ret = NULL; 2300 return ret; 2301 } 2302 } 2303 2304 if (bio_add_page(bio, page, blocksize, 0) < blocksize) 2305 goto submit_and_realloc; 2306 2307 ctx = get_post_read_ctx(bio); 2308 ctx->enabled_steps |= STEP_DECOMPRESS; 2309 refcount_inc(&dic->refcnt); 2310 2311 inc_page_count(sbi, F2FS_RD_DATA); 2312 f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE); 2313 *last_block_in_bio = blkaddr; 2314 } 2315 2316 if (from_dnode) 2317 f2fs_put_dnode(&dn); 2318 2319 *bio_ret = bio; 2320 return 0; 2321 2322 out_put_dnode: 2323 if (from_dnode) 2324 f2fs_put_dnode(&dn); 2325 out: 2326 for (i = 0; i < cc->cluster_size; i++) { 2327 if (cc->rpages[i]) { 2328 ClearPageUptodate(cc->rpages[i]); 2329 unlock_page(cc->rpages[i]); 2330 } 2331 } 2332 *bio_ret = bio; 2333 return ret; 2334 } 2335 #endif 2336 2337 /* 2338 * This function was originally taken from fs/mpage.c, and customized for f2fs. 2339 * Major change was from block_size == page_size in f2fs by default. 2340 */ 2341 static int f2fs_mpage_readpages(struct inode *inode, 2342 struct readahead_control *rac, struct page *page) 2343 { 2344 struct bio *bio = NULL; 2345 sector_t last_block_in_bio = 0; 2346 struct f2fs_map_blocks map; 2347 #ifdef CONFIG_F2FS_FS_COMPRESSION 2348 struct compress_ctx cc = { 2349 .inode = inode, 2350 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 2351 .cluster_size = F2FS_I(inode)->i_cluster_size, 2352 .cluster_idx = NULL_CLUSTER, 2353 .rpages = NULL, 2354 .cpages = NULL, 2355 .nr_rpages = 0, 2356 .nr_cpages = 0, 2357 }; 2358 pgoff_t nc_cluster_idx = NULL_CLUSTER; 2359 #endif 2360 unsigned nr_pages = rac ? readahead_count(rac) : 1; 2361 unsigned max_nr_pages = nr_pages; 2362 int ret = 0; 2363 2364 map.m_pblk = 0; 2365 map.m_lblk = 0; 2366 map.m_len = 0; 2367 map.m_flags = 0; 2368 map.m_next_pgofs = NULL; 2369 map.m_next_extent = NULL; 2370 map.m_seg_type = NO_CHECK_TYPE; 2371 map.m_may_create = false; 2372 2373 for (; nr_pages; nr_pages--) { 2374 if (rac) { 2375 page = readahead_page(rac); 2376 prefetchw(&page->flags); 2377 } 2378 2379 #ifdef CONFIG_F2FS_FS_COMPRESSION 2380 if (f2fs_compressed_file(inode)) { 2381 /* there are remained comressed pages, submit them */ 2382 if (!f2fs_cluster_can_merge_page(&cc, page->index)) { 2383 ret = f2fs_read_multi_pages(&cc, &bio, 2384 max_nr_pages, 2385 &last_block_in_bio, 2386 rac != NULL, false); 2387 f2fs_destroy_compress_ctx(&cc, false); 2388 if (ret) 2389 goto set_error_page; 2390 } 2391 if (cc.cluster_idx == NULL_CLUSTER) { 2392 if (nc_cluster_idx == 2393 page->index >> cc.log_cluster_size) { 2394 goto read_single_page; 2395 } 2396 2397 ret = f2fs_is_compressed_cluster(inode, page->index); 2398 if (ret < 0) 2399 goto set_error_page; 2400 else if (!ret) { 2401 nc_cluster_idx = 2402 page->index >> cc.log_cluster_size; 2403 goto read_single_page; 2404 } 2405 2406 nc_cluster_idx = NULL_CLUSTER; 2407 } 2408 ret = f2fs_init_compress_ctx(&cc); 2409 if (ret) 2410 goto set_error_page; 2411 2412 f2fs_compress_ctx_add_page(&cc, page); 2413 2414 goto next_page; 2415 } 2416 read_single_page: 2417 #endif 2418 2419 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map, 2420 &bio, &last_block_in_bio, rac); 2421 if (ret) { 2422 #ifdef CONFIG_F2FS_FS_COMPRESSION 2423 set_error_page: 2424 #endif 2425 zero_user_segment(page, 0, PAGE_SIZE); 2426 unlock_page(page); 2427 } 2428 #ifdef CONFIG_F2FS_FS_COMPRESSION 2429 next_page: 2430 #endif 2431 if (rac) 2432 put_page(page); 2433 2434 #ifdef CONFIG_F2FS_FS_COMPRESSION 2435 if (f2fs_compressed_file(inode)) { 2436 /* last page */ 2437 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) { 2438 ret = f2fs_read_multi_pages(&cc, &bio, 2439 max_nr_pages, 2440 &last_block_in_bio, 2441 rac != NULL, false); 2442 f2fs_destroy_compress_ctx(&cc, false); 2443 } 2444 } 2445 #endif 2446 } 2447 if (bio) 2448 __submit_bio(F2FS_I_SB(inode), bio, DATA); 2449 return ret; 2450 } 2451 2452 static int f2fs_read_data_folio(struct file *file, struct folio *folio) 2453 { 2454 struct page *page = &folio->page; 2455 struct inode *inode = page_file_mapping(page)->host; 2456 int ret = -EAGAIN; 2457 2458 trace_f2fs_readpage(page, DATA); 2459 2460 if (!f2fs_is_compress_backend_ready(inode)) { 2461 unlock_page(page); 2462 return -EOPNOTSUPP; 2463 } 2464 2465 /* If the file has inline data, try to read it directly */ 2466 if (f2fs_has_inline_data(inode)) 2467 ret = f2fs_read_inline_data(inode, page); 2468 if (ret == -EAGAIN) 2469 ret = f2fs_mpage_readpages(inode, NULL, page); 2470 return ret; 2471 } 2472 2473 static void f2fs_readahead(struct readahead_control *rac) 2474 { 2475 struct inode *inode = rac->mapping->host; 2476 2477 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac)); 2478 2479 if (!f2fs_is_compress_backend_ready(inode)) 2480 return; 2481 2482 /* If the file has inline data, skip readahead */ 2483 if (f2fs_has_inline_data(inode)) 2484 return; 2485 2486 f2fs_mpage_readpages(inode, rac, NULL); 2487 } 2488 2489 int f2fs_encrypt_one_page(struct f2fs_io_info *fio) 2490 { 2491 struct inode *inode = fio->page->mapping->host; 2492 struct page *mpage, *page; 2493 gfp_t gfp_flags = GFP_NOFS; 2494 2495 if (!f2fs_encrypted_file(inode)) 2496 return 0; 2497 2498 page = fio->compressed_page ? fio->compressed_page : fio->page; 2499 2500 /* wait for GCed page writeback via META_MAPPING */ 2501 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr); 2502 2503 if (fscrypt_inode_uses_inline_crypto(inode)) 2504 return 0; 2505 2506 retry_encrypt: 2507 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page, 2508 PAGE_SIZE, 0, gfp_flags); 2509 if (IS_ERR(fio->encrypted_page)) { 2510 /* flush pending IOs and wait for a while in the ENOMEM case */ 2511 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) { 2512 f2fs_flush_merged_writes(fio->sbi); 2513 memalloc_retry_wait(GFP_NOFS); 2514 gfp_flags |= __GFP_NOFAIL; 2515 goto retry_encrypt; 2516 } 2517 return PTR_ERR(fio->encrypted_page); 2518 } 2519 2520 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr); 2521 if (mpage) { 2522 if (PageUptodate(mpage)) 2523 memcpy(page_address(mpage), 2524 page_address(fio->encrypted_page), PAGE_SIZE); 2525 f2fs_put_page(mpage, 1); 2526 } 2527 return 0; 2528 } 2529 2530 static inline bool check_inplace_update_policy(struct inode *inode, 2531 struct f2fs_io_info *fio) 2532 { 2533 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2534 unsigned int policy = SM_I(sbi)->ipu_policy; 2535 2536 if (policy & (0x1 << F2FS_IPU_HONOR_OPU_WRITE) && 2537 is_inode_flag_set(inode, FI_OPU_WRITE)) 2538 return false; 2539 if (policy & (0x1 << F2FS_IPU_FORCE)) 2540 return true; 2541 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi)) 2542 return true; 2543 if (policy & (0x1 << F2FS_IPU_UTIL) && 2544 utilization(sbi) > SM_I(sbi)->min_ipu_util) 2545 return true; 2546 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) && 2547 utilization(sbi) > SM_I(sbi)->min_ipu_util) 2548 return true; 2549 2550 /* 2551 * IPU for rewrite async pages 2552 */ 2553 if (policy & (0x1 << F2FS_IPU_ASYNC) && 2554 fio && fio->op == REQ_OP_WRITE && 2555 !(fio->op_flags & REQ_SYNC) && 2556 !IS_ENCRYPTED(inode)) 2557 return true; 2558 2559 /* this is only set during fdatasync */ 2560 if (policy & (0x1 << F2FS_IPU_FSYNC) && 2561 is_inode_flag_set(inode, FI_NEED_IPU)) 2562 return true; 2563 2564 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) && 2565 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) 2566 return true; 2567 2568 return false; 2569 } 2570 2571 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio) 2572 { 2573 /* swap file is migrating in aligned write mode */ 2574 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE)) 2575 return false; 2576 2577 if (f2fs_is_pinned_file(inode)) 2578 return true; 2579 2580 /* if this is cold file, we should overwrite to avoid fragmentation */ 2581 if (file_is_cold(inode) && !is_inode_flag_set(inode, FI_OPU_WRITE)) 2582 return true; 2583 2584 return check_inplace_update_policy(inode, fio); 2585 } 2586 2587 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio) 2588 { 2589 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2590 2591 /* The below cases were checked when setting it. */ 2592 if (f2fs_is_pinned_file(inode)) 2593 return false; 2594 if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 2595 return true; 2596 if (f2fs_lfs_mode(sbi)) 2597 return true; 2598 if (S_ISDIR(inode->i_mode)) 2599 return true; 2600 if (IS_NOQUOTA(inode)) 2601 return true; 2602 if (f2fs_is_atomic_file(inode)) 2603 return true; 2604 2605 /* swap file is migrating in aligned write mode */ 2606 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE)) 2607 return true; 2608 2609 if (is_inode_flag_set(inode, FI_OPU_WRITE)) 2610 return true; 2611 2612 if (fio) { 2613 if (page_private_gcing(fio->page)) 2614 return true; 2615 if (page_private_dummy(fio->page)) 2616 return true; 2617 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) && 2618 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) 2619 return true; 2620 } 2621 return false; 2622 } 2623 2624 static inline bool need_inplace_update(struct f2fs_io_info *fio) 2625 { 2626 struct inode *inode = fio->page->mapping->host; 2627 2628 if (f2fs_should_update_outplace(inode, fio)) 2629 return false; 2630 2631 return f2fs_should_update_inplace(inode, fio); 2632 } 2633 2634 int f2fs_do_write_data_page(struct f2fs_io_info *fio) 2635 { 2636 struct page *page = fio->page; 2637 struct inode *inode = page->mapping->host; 2638 struct dnode_of_data dn; 2639 struct extent_info ei = {0, }; 2640 struct node_info ni; 2641 bool ipu_force = false; 2642 int err = 0; 2643 2644 /* Use COW inode to make dnode_of_data for atomic write */ 2645 if (f2fs_is_atomic_file(inode)) 2646 set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0); 2647 else 2648 set_new_dnode(&dn, inode, NULL, NULL, 0); 2649 2650 if (need_inplace_update(fio) && 2651 f2fs_lookup_read_extent_cache(inode, page->index, &ei)) { 2652 fio->old_blkaddr = ei.blk + page->index - ei.fofs; 2653 2654 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, 2655 DATA_GENERIC_ENHANCE)) { 2656 f2fs_handle_error(fio->sbi, 2657 ERROR_INVALID_BLKADDR); 2658 return -EFSCORRUPTED; 2659 } 2660 2661 ipu_force = true; 2662 fio->need_lock = LOCK_DONE; 2663 goto got_it; 2664 } 2665 2666 /* Deadlock due to between page->lock and f2fs_lock_op */ 2667 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi)) 2668 return -EAGAIN; 2669 2670 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE); 2671 if (err) 2672 goto out; 2673 2674 fio->old_blkaddr = dn.data_blkaddr; 2675 2676 /* This page is already truncated */ 2677 if (fio->old_blkaddr == NULL_ADDR) { 2678 ClearPageUptodate(page); 2679 clear_page_private_gcing(page); 2680 goto out_writepage; 2681 } 2682 got_it: 2683 if (__is_valid_data_blkaddr(fio->old_blkaddr) && 2684 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, 2685 DATA_GENERIC_ENHANCE)) { 2686 err = -EFSCORRUPTED; 2687 f2fs_handle_error(fio->sbi, ERROR_INVALID_BLKADDR); 2688 goto out_writepage; 2689 } 2690 2691 /* 2692 * If current allocation needs SSR, 2693 * it had better in-place writes for updated data. 2694 */ 2695 if (ipu_force || 2696 (__is_valid_data_blkaddr(fio->old_blkaddr) && 2697 need_inplace_update(fio))) { 2698 err = f2fs_encrypt_one_page(fio); 2699 if (err) 2700 goto out_writepage; 2701 2702 set_page_writeback(page); 2703 ClearPageError(page); 2704 f2fs_put_dnode(&dn); 2705 if (fio->need_lock == LOCK_REQ) 2706 f2fs_unlock_op(fio->sbi); 2707 err = f2fs_inplace_write_data(fio); 2708 if (err) { 2709 if (fscrypt_inode_uses_fs_layer_crypto(inode)) 2710 fscrypt_finalize_bounce_page(&fio->encrypted_page); 2711 if (PageWriteback(page)) 2712 end_page_writeback(page); 2713 } else { 2714 set_inode_flag(inode, FI_UPDATE_WRITE); 2715 } 2716 trace_f2fs_do_write_data_page(fio->page, IPU); 2717 return err; 2718 } 2719 2720 if (fio->need_lock == LOCK_RETRY) { 2721 if (!f2fs_trylock_op(fio->sbi)) { 2722 err = -EAGAIN; 2723 goto out_writepage; 2724 } 2725 fio->need_lock = LOCK_REQ; 2726 } 2727 2728 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false); 2729 if (err) 2730 goto out_writepage; 2731 2732 fio->version = ni.version; 2733 2734 err = f2fs_encrypt_one_page(fio); 2735 if (err) 2736 goto out_writepage; 2737 2738 set_page_writeback(page); 2739 ClearPageError(page); 2740 2741 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR) 2742 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false); 2743 2744 /* LFS mode write path */ 2745 f2fs_outplace_write_data(&dn, fio); 2746 trace_f2fs_do_write_data_page(page, OPU); 2747 set_inode_flag(inode, FI_APPEND_WRITE); 2748 if (page->index == 0) 2749 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); 2750 out_writepage: 2751 f2fs_put_dnode(&dn); 2752 out: 2753 if (fio->need_lock == LOCK_REQ) 2754 f2fs_unlock_op(fio->sbi); 2755 return err; 2756 } 2757 2758 int f2fs_write_single_data_page(struct page *page, int *submitted, 2759 struct bio **bio, 2760 sector_t *last_block, 2761 struct writeback_control *wbc, 2762 enum iostat_type io_type, 2763 int compr_blocks, 2764 bool allow_balance) 2765 { 2766 struct inode *inode = page->mapping->host; 2767 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2768 loff_t i_size = i_size_read(inode); 2769 const pgoff_t end_index = ((unsigned long long)i_size) 2770 >> PAGE_SHIFT; 2771 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT; 2772 unsigned offset = 0; 2773 bool need_balance_fs = false; 2774 int err = 0; 2775 struct f2fs_io_info fio = { 2776 .sbi = sbi, 2777 .ino = inode->i_ino, 2778 .type = DATA, 2779 .op = REQ_OP_WRITE, 2780 .op_flags = wbc_to_write_flags(wbc), 2781 .old_blkaddr = NULL_ADDR, 2782 .page = page, 2783 .encrypted_page = NULL, 2784 .submitted = false, 2785 .compr_blocks = compr_blocks, 2786 .need_lock = LOCK_RETRY, 2787 .post_read = f2fs_post_read_required(inode), 2788 .io_type = io_type, 2789 .io_wbc = wbc, 2790 .bio = bio, 2791 .last_block = last_block, 2792 }; 2793 2794 trace_f2fs_writepage(page, DATA); 2795 2796 /* we should bypass data pages to proceed the kworkder jobs */ 2797 if (unlikely(f2fs_cp_error(sbi))) { 2798 mapping_set_error(page->mapping, -EIO); 2799 /* 2800 * don't drop any dirty dentry pages for keeping lastest 2801 * directory structure. 2802 */ 2803 if (S_ISDIR(inode->i_mode)) 2804 goto redirty_out; 2805 goto out; 2806 } 2807 2808 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 2809 goto redirty_out; 2810 2811 if (page->index < end_index || 2812 f2fs_verity_in_progress(inode) || 2813 compr_blocks) 2814 goto write; 2815 2816 /* 2817 * If the offset is out-of-range of file size, 2818 * this page does not have to be written to disk. 2819 */ 2820 offset = i_size & (PAGE_SIZE - 1); 2821 if ((page->index >= end_index + 1) || !offset) 2822 goto out; 2823 2824 zero_user_segment(page, offset, PAGE_SIZE); 2825 write: 2826 if (f2fs_is_drop_cache(inode)) 2827 goto out; 2828 2829 /* Dentry/quota blocks are controlled by checkpoint */ 2830 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) { 2831 /* 2832 * We need to wait for node_write to avoid block allocation during 2833 * checkpoint. This can only happen to quota writes which can cause 2834 * the below discard race condition. 2835 */ 2836 if (IS_NOQUOTA(inode)) 2837 f2fs_down_read(&sbi->node_write); 2838 2839 fio.need_lock = LOCK_DONE; 2840 err = f2fs_do_write_data_page(&fio); 2841 2842 if (IS_NOQUOTA(inode)) 2843 f2fs_up_read(&sbi->node_write); 2844 2845 goto done; 2846 } 2847 2848 if (!wbc->for_reclaim) 2849 need_balance_fs = true; 2850 else if (has_not_enough_free_secs(sbi, 0, 0)) 2851 goto redirty_out; 2852 else 2853 set_inode_flag(inode, FI_HOT_DATA); 2854 2855 err = -EAGAIN; 2856 if (f2fs_has_inline_data(inode)) { 2857 err = f2fs_write_inline_data(inode, page); 2858 if (!err) 2859 goto out; 2860 } 2861 2862 if (err == -EAGAIN) { 2863 err = f2fs_do_write_data_page(&fio); 2864 if (err == -EAGAIN) { 2865 fio.need_lock = LOCK_REQ; 2866 err = f2fs_do_write_data_page(&fio); 2867 } 2868 } 2869 2870 if (err) { 2871 file_set_keep_isize(inode); 2872 } else { 2873 spin_lock(&F2FS_I(inode)->i_size_lock); 2874 if (F2FS_I(inode)->last_disk_size < psize) 2875 F2FS_I(inode)->last_disk_size = psize; 2876 spin_unlock(&F2FS_I(inode)->i_size_lock); 2877 } 2878 2879 done: 2880 if (err && err != -ENOENT) 2881 goto redirty_out; 2882 2883 out: 2884 inode_dec_dirty_pages(inode); 2885 if (err) { 2886 ClearPageUptodate(page); 2887 clear_page_private_gcing(page); 2888 } 2889 2890 if (wbc->for_reclaim) { 2891 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA); 2892 clear_inode_flag(inode, FI_HOT_DATA); 2893 f2fs_remove_dirty_inode(inode); 2894 submitted = NULL; 2895 } 2896 unlock_page(page); 2897 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) && 2898 !F2FS_I(inode)->wb_task && allow_balance) 2899 f2fs_balance_fs(sbi, need_balance_fs); 2900 2901 if (unlikely(f2fs_cp_error(sbi))) { 2902 f2fs_submit_merged_write(sbi, DATA); 2903 f2fs_submit_merged_ipu_write(sbi, bio, NULL); 2904 submitted = NULL; 2905 } 2906 2907 if (submitted) 2908 *submitted = fio.submitted ? 1 : 0; 2909 2910 return 0; 2911 2912 redirty_out: 2913 redirty_page_for_writepage(wbc, page); 2914 /* 2915 * pageout() in MM traslates EAGAIN, so calls handle_write_error() 2916 * -> mapping_set_error() -> set_bit(AS_EIO, ...). 2917 * file_write_and_wait_range() will see EIO error, which is critical 2918 * to return value of fsync() followed by atomic_write failure to user. 2919 */ 2920 if (!err || wbc->for_reclaim) 2921 return AOP_WRITEPAGE_ACTIVATE; 2922 unlock_page(page); 2923 return err; 2924 } 2925 2926 static int f2fs_write_data_page(struct page *page, 2927 struct writeback_control *wbc) 2928 { 2929 #ifdef CONFIG_F2FS_FS_COMPRESSION 2930 struct inode *inode = page->mapping->host; 2931 2932 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) 2933 goto out; 2934 2935 if (f2fs_compressed_file(inode)) { 2936 if (f2fs_is_compressed_cluster(inode, page->index)) { 2937 redirty_page_for_writepage(wbc, page); 2938 return AOP_WRITEPAGE_ACTIVATE; 2939 } 2940 } 2941 out: 2942 #endif 2943 2944 return f2fs_write_single_data_page(page, NULL, NULL, NULL, 2945 wbc, FS_DATA_IO, 0, true); 2946 } 2947 2948 /* 2949 * This function was copied from write_cche_pages from mm/page-writeback.c. 2950 * The major change is making write step of cold data page separately from 2951 * warm/hot data page. 2952 */ 2953 static int f2fs_write_cache_pages(struct address_space *mapping, 2954 struct writeback_control *wbc, 2955 enum iostat_type io_type) 2956 { 2957 int ret = 0; 2958 int done = 0, retry = 0; 2959 struct page *pages[F2FS_ONSTACK_PAGES]; 2960 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 2961 struct bio *bio = NULL; 2962 sector_t last_block; 2963 #ifdef CONFIG_F2FS_FS_COMPRESSION 2964 struct inode *inode = mapping->host; 2965 struct compress_ctx cc = { 2966 .inode = inode, 2967 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 2968 .cluster_size = F2FS_I(inode)->i_cluster_size, 2969 .cluster_idx = NULL_CLUSTER, 2970 .rpages = NULL, 2971 .nr_rpages = 0, 2972 .cpages = NULL, 2973 .valid_nr_cpages = 0, 2974 .rbuf = NULL, 2975 .cbuf = NULL, 2976 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size, 2977 .private = NULL, 2978 }; 2979 #endif 2980 int nr_pages; 2981 pgoff_t index; 2982 pgoff_t end; /* Inclusive */ 2983 pgoff_t done_index; 2984 int range_whole = 0; 2985 xa_mark_t tag; 2986 int nwritten = 0; 2987 int submitted = 0; 2988 int i; 2989 2990 if (get_dirty_pages(mapping->host) <= 2991 SM_I(F2FS_M_SB(mapping))->min_hot_blocks) 2992 set_inode_flag(mapping->host, FI_HOT_DATA); 2993 else 2994 clear_inode_flag(mapping->host, FI_HOT_DATA); 2995 2996 if (wbc->range_cyclic) { 2997 index = mapping->writeback_index; /* prev offset */ 2998 end = -1; 2999 } else { 3000 index = wbc->range_start >> PAGE_SHIFT; 3001 end = wbc->range_end >> PAGE_SHIFT; 3002 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 3003 range_whole = 1; 3004 } 3005 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 3006 tag = PAGECACHE_TAG_TOWRITE; 3007 else 3008 tag = PAGECACHE_TAG_DIRTY; 3009 retry: 3010 retry = 0; 3011 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 3012 tag_pages_for_writeback(mapping, index, end); 3013 done_index = index; 3014 while (!done && !retry && (index <= end)) { 3015 nr_pages = find_get_pages_range_tag(mapping, &index, end, 3016 tag, F2FS_ONSTACK_PAGES, pages); 3017 if (nr_pages == 0) 3018 break; 3019 3020 for (i = 0; i < nr_pages; i++) { 3021 struct page *page = pages[i]; 3022 bool need_readd; 3023 readd: 3024 need_readd = false; 3025 #ifdef CONFIG_F2FS_FS_COMPRESSION 3026 if (f2fs_compressed_file(inode)) { 3027 void *fsdata = NULL; 3028 struct page *pagep; 3029 int ret2; 3030 3031 ret = f2fs_init_compress_ctx(&cc); 3032 if (ret) { 3033 done = 1; 3034 break; 3035 } 3036 3037 if (!f2fs_cluster_can_merge_page(&cc, 3038 page->index)) { 3039 ret = f2fs_write_multi_pages(&cc, 3040 &submitted, wbc, io_type); 3041 if (!ret) 3042 need_readd = true; 3043 goto result; 3044 } 3045 3046 if (unlikely(f2fs_cp_error(sbi))) 3047 goto lock_page; 3048 3049 if (!f2fs_cluster_is_empty(&cc)) 3050 goto lock_page; 3051 3052 if (f2fs_all_cluster_page_ready(&cc, 3053 pages, i, nr_pages, true)) 3054 goto lock_page; 3055 3056 ret2 = f2fs_prepare_compress_overwrite( 3057 inode, &pagep, 3058 page->index, &fsdata); 3059 if (ret2 < 0) { 3060 ret = ret2; 3061 done = 1; 3062 break; 3063 } else if (ret2 && 3064 (!f2fs_compress_write_end(inode, 3065 fsdata, page->index, 1) || 3066 !f2fs_all_cluster_page_ready(&cc, 3067 pages, i, nr_pages, false))) { 3068 retry = 1; 3069 break; 3070 } 3071 } 3072 #endif 3073 /* give a priority to WB_SYNC threads */ 3074 if (atomic_read(&sbi->wb_sync_req[DATA]) && 3075 wbc->sync_mode == WB_SYNC_NONE) { 3076 done = 1; 3077 break; 3078 } 3079 #ifdef CONFIG_F2FS_FS_COMPRESSION 3080 lock_page: 3081 #endif 3082 done_index = page->index; 3083 retry_write: 3084 lock_page(page); 3085 3086 if (unlikely(page->mapping != mapping)) { 3087 continue_unlock: 3088 unlock_page(page); 3089 continue; 3090 } 3091 3092 if (!PageDirty(page)) { 3093 /* someone wrote it for us */ 3094 goto continue_unlock; 3095 } 3096 3097 if (PageWriteback(page)) { 3098 if (wbc->sync_mode != WB_SYNC_NONE) 3099 f2fs_wait_on_page_writeback(page, 3100 DATA, true, true); 3101 else 3102 goto continue_unlock; 3103 } 3104 3105 if (!clear_page_dirty_for_io(page)) 3106 goto continue_unlock; 3107 3108 #ifdef CONFIG_F2FS_FS_COMPRESSION 3109 if (f2fs_compressed_file(inode)) { 3110 get_page(page); 3111 f2fs_compress_ctx_add_page(&cc, page); 3112 continue; 3113 } 3114 #endif 3115 ret = f2fs_write_single_data_page(page, &submitted, 3116 &bio, &last_block, wbc, io_type, 3117 0, true); 3118 if (ret == AOP_WRITEPAGE_ACTIVATE) 3119 unlock_page(page); 3120 #ifdef CONFIG_F2FS_FS_COMPRESSION 3121 result: 3122 #endif 3123 nwritten += submitted; 3124 wbc->nr_to_write -= submitted; 3125 3126 if (unlikely(ret)) { 3127 /* 3128 * keep nr_to_write, since vfs uses this to 3129 * get # of written pages. 3130 */ 3131 if (ret == AOP_WRITEPAGE_ACTIVATE) { 3132 ret = 0; 3133 goto next; 3134 } else if (ret == -EAGAIN) { 3135 ret = 0; 3136 if (wbc->sync_mode == WB_SYNC_ALL) { 3137 f2fs_io_schedule_timeout( 3138 DEFAULT_IO_TIMEOUT); 3139 goto retry_write; 3140 } 3141 goto next; 3142 } 3143 done_index = page->index + 1; 3144 done = 1; 3145 break; 3146 } 3147 3148 if (wbc->nr_to_write <= 0 && 3149 wbc->sync_mode == WB_SYNC_NONE) { 3150 done = 1; 3151 break; 3152 } 3153 next: 3154 if (need_readd) 3155 goto readd; 3156 } 3157 release_pages(pages, nr_pages); 3158 cond_resched(); 3159 } 3160 #ifdef CONFIG_F2FS_FS_COMPRESSION 3161 /* flush remained pages in compress cluster */ 3162 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) { 3163 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type); 3164 nwritten += submitted; 3165 wbc->nr_to_write -= submitted; 3166 if (ret) { 3167 done = 1; 3168 retry = 0; 3169 } 3170 } 3171 if (f2fs_compressed_file(inode)) 3172 f2fs_destroy_compress_ctx(&cc, false); 3173 #endif 3174 if (retry) { 3175 index = 0; 3176 end = -1; 3177 goto retry; 3178 } 3179 if (wbc->range_cyclic && !done) 3180 done_index = 0; 3181 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 3182 mapping->writeback_index = done_index; 3183 3184 if (nwritten) 3185 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host, 3186 NULL, 0, DATA); 3187 /* submit cached bio of IPU write */ 3188 if (bio) 3189 f2fs_submit_merged_ipu_write(sbi, &bio, NULL); 3190 3191 return ret; 3192 } 3193 3194 static inline bool __should_serialize_io(struct inode *inode, 3195 struct writeback_control *wbc) 3196 { 3197 /* to avoid deadlock in path of data flush */ 3198 if (F2FS_I(inode)->wb_task) 3199 return false; 3200 3201 if (!S_ISREG(inode->i_mode)) 3202 return false; 3203 if (IS_NOQUOTA(inode)) 3204 return false; 3205 3206 if (f2fs_need_compress_data(inode)) 3207 return true; 3208 if (wbc->sync_mode != WB_SYNC_ALL) 3209 return true; 3210 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks) 3211 return true; 3212 return false; 3213 } 3214 3215 static int __f2fs_write_data_pages(struct address_space *mapping, 3216 struct writeback_control *wbc, 3217 enum iostat_type io_type) 3218 { 3219 struct inode *inode = mapping->host; 3220 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3221 struct blk_plug plug; 3222 int ret; 3223 bool locked = false; 3224 3225 /* deal with chardevs and other special file */ 3226 if (!mapping->a_ops->writepage) 3227 return 0; 3228 3229 /* skip writing if there is no dirty page in this inode */ 3230 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) 3231 return 0; 3232 3233 /* during POR, we don't need to trigger writepage at all. */ 3234 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 3235 goto skip_write; 3236 3237 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) && 3238 wbc->sync_mode == WB_SYNC_NONE && 3239 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && 3240 f2fs_available_free_memory(sbi, DIRTY_DENTS)) 3241 goto skip_write; 3242 3243 /* skip writing in file defragment preparing stage */ 3244 if (is_inode_flag_set(inode, FI_SKIP_WRITES)) 3245 goto skip_write; 3246 3247 trace_f2fs_writepages(mapping->host, wbc, DATA); 3248 3249 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */ 3250 if (wbc->sync_mode == WB_SYNC_ALL) 3251 atomic_inc(&sbi->wb_sync_req[DATA]); 3252 else if (atomic_read(&sbi->wb_sync_req[DATA])) { 3253 /* to avoid potential deadlock */ 3254 if (current->plug) 3255 blk_finish_plug(current->plug); 3256 goto skip_write; 3257 } 3258 3259 if (__should_serialize_io(inode, wbc)) { 3260 mutex_lock(&sbi->writepages); 3261 locked = true; 3262 } 3263 3264 blk_start_plug(&plug); 3265 ret = f2fs_write_cache_pages(mapping, wbc, io_type); 3266 blk_finish_plug(&plug); 3267 3268 if (locked) 3269 mutex_unlock(&sbi->writepages); 3270 3271 if (wbc->sync_mode == WB_SYNC_ALL) 3272 atomic_dec(&sbi->wb_sync_req[DATA]); 3273 /* 3274 * if some pages were truncated, we cannot guarantee its mapping->host 3275 * to detect pending bios. 3276 */ 3277 3278 f2fs_remove_dirty_inode(inode); 3279 return ret; 3280 3281 skip_write: 3282 wbc->pages_skipped += get_dirty_pages(inode); 3283 trace_f2fs_writepages(mapping->host, wbc, DATA); 3284 return 0; 3285 } 3286 3287 static int f2fs_write_data_pages(struct address_space *mapping, 3288 struct writeback_control *wbc) 3289 { 3290 struct inode *inode = mapping->host; 3291 3292 return __f2fs_write_data_pages(mapping, wbc, 3293 F2FS_I(inode)->cp_task == current ? 3294 FS_CP_DATA_IO : FS_DATA_IO); 3295 } 3296 3297 void f2fs_write_failed(struct inode *inode, loff_t to) 3298 { 3299 loff_t i_size = i_size_read(inode); 3300 3301 if (IS_NOQUOTA(inode)) 3302 return; 3303 3304 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */ 3305 if (to > i_size && !f2fs_verity_in_progress(inode)) { 3306 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 3307 filemap_invalidate_lock(inode->i_mapping); 3308 3309 truncate_pagecache(inode, i_size); 3310 f2fs_truncate_blocks(inode, i_size, true); 3311 3312 filemap_invalidate_unlock(inode->i_mapping); 3313 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 3314 } 3315 } 3316 3317 static int prepare_write_begin(struct f2fs_sb_info *sbi, 3318 struct page *page, loff_t pos, unsigned len, 3319 block_t *blk_addr, bool *node_changed) 3320 { 3321 struct inode *inode = page->mapping->host; 3322 pgoff_t index = page->index; 3323 struct dnode_of_data dn; 3324 struct page *ipage; 3325 bool locked = false; 3326 struct extent_info ei = {0, }; 3327 int err = 0; 3328 int flag; 3329 3330 /* 3331 * If a whole page is being written and we already preallocated all the 3332 * blocks, then there is no need to get a block address now. 3333 */ 3334 if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL)) 3335 return 0; 3336 3337 /* f2fs_lock_op avoids race between write CP and convert_inline_page */ 3338 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode)) 3339 flag = F2FS_GET_BLOCK_DEFAULT; 3340 else 3341 flag = F2FS_GET_BLOCK_PRE_AIO; 3342 3343 if (f2fs_has_inline_data(inode) || 3344 (pos & PAGE_MASK) >= i_size_read(inode)) { 3345 f2fs_do_map_lock(sbi, flag, true); 3346 locked = true; 3347 } 3348 3349 restart: 3350 /* check inline_data */ 3351 ipage = f2fs_get_node_page(sbi, inode->i_ino); 3352 if (IS_ERR(ipage)) { 3353 err = PTR_ERR(ipage); 3354 goto unlock_out; 3355 } 3356 3357 set_new_dnode(&dn, inode, ipage, ipage, 0); 3358 3359 if (f2fs_has_inline_data(inode)) { 3360 if (pos + len <= MAX_INLINE_DATA(inode)) { 3361 f2fs_do_read_inline_data(page, ipage); 3362 set_inode_flag(inode, FI_DATA_EXIST); 3363 if (inode->i_nlink) 3364 set_page_private_inline(ipage); 3365 } else { 3366 err = f2fs_convert_inline_page(&dn, page); 3367 if (err) 3368 goto out; 3369 if (dn.data_blkaddr == NULL_ADDR) 3370 err = f2fs_get_block(&dn, index); 3371 } 3372 } else if (locked) { 3373 err = f2fs_get_block(&dn, index); 3374 } else { 3375 if (f2fs_lookup_read_extent_cache(inode, index, &ei)) { 3376 dn.data_blkaddr = ei.blk + index - ei.fofs; 3377 } else { 3378 /* hole case */ 3379 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 3380 if (err || dn.data_blkaddr == NULL_ADDR) { 3381 f2fs_put_dnode(&dn); 3382 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, 3383 true); 3384 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO); 3385 locked = true; 3386 goto restart; 3387 } 3388 } 3389 } 3390 3391 /* convert_inline_page can make node_changed */ 3392 *blk_addr = dn.data_blkaddr; 3393 *node_changed = dn.node_changed; 3394 out: 3395 f2fs_put_dnode(&dn); 3396 unlock_out: 3397 if (locked) 3398 f2fs_do_map_lock(sbi, flag, false); 3399 return err; 3400 } 3401 3402 static int __find_data_block(struct inode *inode, pgoff_t index, 3403 block_t *blk_addr) 3404 { 3405 struct dnode_of_data dn; 3406 struct page *ipage; 3407 struct extent_info ei = {0, }; 3408 int err = 0; 3409 3410 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); 3411 if (IS_ERR(ipage)) 3412 return PTR_ERR(ipage); 3413 3414 set_new_dnode(&dn, inode, ipage, ipage, 0); 3415 3416 if (f2fs_lookup_read_extent_cache(inode, index, &ei)) { 3417 dn.data_blkaddr = ei.blk + index - ei.fofs; 3418 } else { 3419 /* hole case */ 3420 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 3421 if (err) { 3422 dn.data_blkaddr = NULL_ADDR; 3423 err = 0; 3424 } 3425 } 3426 *blk_addr = dn.data_blkaddr; 3427 f2fs_put_dnode(&dn); 3428 return err; 3429 } 3430 3431 static int __reserve_data_block(struct inode *inode, pgoff_t index, 3432 block_t *blk_addr, bool *node_changed) 3433 { 3434 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3435 struct dnode_of_data dn; 3436 struct page *ipage; 3437 int err = 0; 3438 3439 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true); 3440 3441 ipage = f2fs_get_node_page(sbi, inode->i_ino); 3442 if (IS_ERR(ipage)) { 3443 err = PTR_ERR(ipage); 3444 goto unlock_out; 3445 } 3446 set_new_dnode(&dn, inode, ipage, ipage, 0); 3447 3448 err = f2fs_get_block(&dn, index); 3449 3450 *blk_addr = dn.data_blkaddr; 3451 *node_changed = dn.node_changed; 3452 f2fs_put_dnode(&dn); 3453 3454 unlock_out: 3455 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false); 3456 return err; 3457 } 3458 3459 static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi, 3460 struct page *page, loff_t pos, unsigned int len, 3461 block_t *blk_addr, bool *node_changed) 3462 { 3463 struct inode *inode = page->mapping->host; 3464 struct inode *cow_inode = F2FS_I(inode)->cow_inode; 3465 pgoff_t index = page->index; 3466 int err = 0; 3467 block_t ori_blk_addr = NULL_ADDR; 3468 3469 /* If pos is beyond the end of file, reserve a new block in COW inode */ 3470 if ((pos & PAGE_MASK) >= i_size_read(inode)) 3471 goto reserve_block; 3472 3473 /* Look for the block in COW inode first */ 3474 err = __find_data_block(cow_inode, index, blk_addr); 3475 if (err) 3476 return err; 3477 else if (*blk_addr != NULL_ADDR) 3478 return 0; 3479 3480 if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE)) 3481 goto reserve_block; 3482 3483 /* Look for the block in the original inode */ 3484 err = __find_data_block(inode, index, &ori_blk_addr); 3485 if (err) 3486 return err; 3487 3488 reserve_block: 3489 /* Finally, we should reserve a new block in COW inode for the update */ 3490 err = __reserve_data_block(cow_inode, index, blk_addr, node_changed); 3491 if (err) 3492 return err; 3493 inc_atomic_write_cnt(inode); 3494 3495 if (ori_blk_addr != NULL_ADDR) 3496 *blk_addr = ori_blk_addr; 3497 return 0; 3498 } 3499 3500 static int f2fs_write_begin(struct file *file, struct address_space *mapping, 3501 loff_t pos, unsigned len, struct page **pagep, void **fsdata) 3502 { 3503 struct inode *inode = mapping->host; 3504 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3505 struct page *page = NULL; 3506 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT; 3507 bool need_balance = false; 3508 block_t blkaddr = NULL_ADDR; 3509 int err = 0; 3510 3511 trace_f2fs_write_begin(inode, pos, len); 3512 3513 if (!f2fs_is_checkpoint_ready(sbi)) { 3514 err = -ENOSPC; 3515 goto fail; 3516 } 3517 3518 /* 3519 * We should check this at this moment to avoid deadlock on inode page 3520 * and #0 page. The locking rule for inline_data conversion should be: 3521 * lock_page(page #0) -> lock_page(inode_page) 3522 */ 3523 if (index != 0) { 3524 err = f2fs_convert_inline_inode(inode); 3525 if (err) 3526 goto fail; 3527 } 3528 3529 #ifdef CONFIG_F2FS_FS_COMPRESSION 3530 if (f2fs_compressed_file(inode)) { 3531 int ret; 3532 3533 *fsdata = NULL; 3534 3535 if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode))) 3536 goto repeat; 3537 3538 ret = f2fs_prepare_compress_overwrite(inode, pagep, 3539 index, fsdata); 3540 if (ret < 0) { 3541 err = ret; 3542 goto fail; 3543 } else if (ret) { 3544 return 0; 3545 } 3546 } 3547 #endif 3548 3549 repeat: 3550 /* 3551 * Do not use grab_cache_page_write_begin() to avoid deadlock due to 3552 * wait_for_stable_page. Will wait that below with our IO control. 3553 */ 3554 page = f2fs_pagecache_get_page(mapping, index, 3555 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS); 3556 if (!page) { 3557 err = -ENOMEM; 3558 goto fail; 3559 } 3560 3561 /* TODO: cluster can be compressed due to race with .writepage */ 3562 3563 *pagep = page; 3564 3565 if (f2fs_is_atomic_file(inode)) 3566 err = prepare_atomic_write_begin(sbi, page, pos, len, 3567 &blkaddr, &need_balance); 3568 else 3569 err = prepare_write_begin(sbi, page, pos, len, 3570 &blkaddr, &need_balance); 3571 if (err) 3572 goto fail; 3573 3574 if (need_balance && !IS_NOQUOTA(inode) && 3575 has_not_enough_free_secs(sbi, 0, 0)) { 3576 unlock_page(page); 3577 f2fs_balance_fs(sbi, true); 3578 lock_page(page); 3579 if (page->mapping != mapping) { 3580 /* The page got truncated from under us */ 3581 f2fs_put_page(page, 1); 3582 goto repeat; 3583 } 3584 } 3585 3586 f2fs_wait_on_page_writeback(page, DATA, false, true); 3587 3588 if (len == PAGE_SIZE || PageUptodate(page)) 3589 return 0; 3590 3591 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) && 3592 !f2fs_verity_in_progress(inode)) { 3593 zero_user_segment(page, len, PAGE_SIZE); 3594 return 0; 3595 } 3596 3597 if (blkaddr == NEW_ADDR) { 3598 zero_user_segment(page, 0, PAGE_SIZE); 3599 SetPageUptodate(page); 3600 } else { 3601 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, 3602 DATA_GENERIC_ENHANCE_READ)) { 3603 err = -EFSCORRUPTED; 3604 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR); 3605 goto fail; 3606 } 3607 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true); 3608 if (err) 3609 goto fail; 3610 3611 lock_page(page); 3612 if (unlikely(page->mapping != mapping)) { 3613 f2fs_put_page(page, 1); 3614 goto repeat; 3615 } 3616 if (unlikely(!PageUptodate(page))) { 3617 err = -EIO; 3618 goto fail; 3619 } 3620 } 3621 return 0; 3622 3623 fail: 3624 f2fs_put_page(page, 1); 3625 f2fs_write_failed(inode, pos + len); 3626 return err; 3627 } 3628 3629 static int f2fs_write_end(struct file *file, 3630 struct address_space *mapping, 3631 loff_t pos, unsigned len, unsigned copied, 3632 struct page *page, void *fsdata) 3633 { 3634 struct inode *inode = page->mapping->host; 3635 3636 trace_f2fs_write_end(inode, pos, len, copied); 3637 3638 /* 3639 * This should be come from len == PAGE_SIZE, and we expect copied 3640 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and 3641 * let generic_perform_write() try to copy data again through copied=0. 3642 */ 3643 if (!PageUptodate(page)) { 3644 if (unlikely(copied != len)) 3645 copied = 0; 3646 else 3647 SetPageUptodate(page); 3648 } 3649 3650 #ifdef CONFIG_F2FS_FS_COMPRESSION 3651 /* overwrite compressed file */ 3652 if (f2fs_compressed_file(inode) && fsdata) { 3653 f2fs_compress_write_end(inode, fsdata, page->index, copied); 3654 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 3655 3656 if (pos + copied > i_size_read(inode) && 3657 !f2fs_verity_in_progress(inode)) 3658 f2fs_i_size_write(inode, pos + copied); 3659 return copied; 3660 } 3661 #endif 3662 3663 if (!copied) 3664 goto unlock_out; 3665 3666 set_page_dirty(page); 3667 3668 if (pos + copied > i_size_read(inode) && 3669 !f2fs_verity_in_progress(inode)) { 3670 f2fs_i_size_write(inode, pos + copied); 3671 if (f2fs_is_atomic_file(inode)) 3672 f2fs_i_size_write(F2FS_I(inode)->cow_inode, 3673 pos + copied); 3674 } 3675 unlock_out: 3676 f2fs_put_page(page, 1); 3677 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 3678 return copied; 3679 } 3680 3681 void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length) 3682 { 3683 struct inode *inode = folio->mapping->host; 3684 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3685 3686 if (inode->i_ino >= F2FS_ROOT_INO(sbi) && 3687 (offset || length != folio_size(folio))) 3688 return; 3689 3690 if (folio_test_dirty(folio)) { 3691 if (inode->i_ino == F2FS_META_INO(sbi)) { 3692 dec_page_count(sbi, F2FS_DIRTY_META); 3693 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) { 3694 dec_page_count(sbi, F2FS_DIRTY_NODES); 3695 } else { 3696 inode_dec_dirty_pages(inode); 3697 f2fs_remove_dirty_inode(inode); 3698 } 3699 } 3700 3701 clear_page_private_gcing(&folio->page); 3702 3703 if (test_opt(sbi, COMPRESS_CACHE) && 3704 inode->i_ino == F2FS_COMPRESS_INO(sbi)) 3705 clear_page_private_data(&folio->page); 3706 3707 folio_detach_private(folio); 3708 } 3709 3710 bool f2fs_release_folio(struct folio *folio, gfp_t wait) 3711 { 3712 struct f2fs_sb_info *sbi; 3713 3714 /* If this is dirty folio, keep private data */ 3715 if (folio_test_dirty(folio)) 3716 return false; 3717 3718 sbi = F2FS_M_SB(folio->mapping); 3719 if (test_opt(sbi, COMPRESS_CACHE)) { 3720 struct inode *inode = folio->mapping->host; 3721 3722 if (inode->i_ino == F2FS_COMPRESS_INO(sbi)) 3723 clear_page_private_data(&folio->page); 3724 } 3725 3726 clear_page_private_gcing(&folio->page); 3727 3728 folio_detach_private(folio); 3729 return true; 3730 } 3731 3732 static bool f2fs_dirty_data_folio(struct address_space *mapping, 3733 struct folio *folio) 3734 { 3735 struct inode *inode = mapping->host; 3736 3737 trace_f2fs_set_page_dirty(&folio->page, DATA); 3738 3739 if (!folio_test_uptodate(folio)) 3740 folio_mark_uptodate(folio); 3741 BUG_ON(folio_test_swapcache(folio)); 3742 3743 if (filemap_dirty_folio(mapping, folio)) { 3744 f2fs_update_dirty_folio(inode, folio); 3745 return true; 3746 } 3747 return false; 3748 } 3749 3750 3751 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block) 3752 { 3753 #ifdef CONFIG_F2FS_FS_COMPRESSION 3754 struct dnode_of_data dn; 3755 sector_t start_idx, blknr = 0; 3756 int ret; 3757 3758 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size); 3759 3760 set_new_dnode(&dn, inode, NULL, NULL, 0); 3761 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); 3762 if (ret) 3763 return 0; 3764 3765 if (dn.data_blkaddr != COMPRESS_ADDR) { 3766 dn.ofs_in_node += block - start_idx; 3767 blknr = f2fs_data_blkaddr(&dn); 3768 if (!__is_valid_data_blkaddr(blknr)) 3769 blknr = 0; 3770 } 3771 3772 f2fs_put_dnode(&dn); 3773 return blknr; 3774 #else 3775 return 0; 3776 #endif 3777 } 3778 3779 3780 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) 3781 { 3782 struct inode *inode = mapping->host; 3783 sector_t blknr = 0; 3784 3785 if (f2fs_has_inline_data(inode)) 3786 goto out; 3787 3788 /* make sure allocating whole blocks */ 3789 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 3790 filemap_write_and_wait(mapping); 3791 3792 /* Block number less than F2FS MAX BLOCKS */ 3793 if (unlikely(block >= max_file_blocks(inode))) 3794 goto out; 3795 3796 if (f2fs_compressed_file(inode)) { 3797 blknr = f2fs_bmap_compress(inode, block); 3798 } else { 3799 struct f2fs_map_blocks map; 3800 3801 memset(&map, 0, sizeof(map)); 3802 map.m_lblk = block; 3803 map.m_len = 1; 3804 map.m_next_pgofs = NULL; 3805 map.m_seg_type = NO_CHECK_TYPE; 3806 3807 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP)) 3808 blknr = map.m_pblk; 3809 } 3810 out: 3811 trace_f2fs_bmap(inode, block, blknr); 3812 return blknr; 3813 } 3814 3815 #ifdef CONFIG_SWAP 3816 static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk, 3817 unsigned int blkcnt) 3818 { 3819 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3820 unsigned int blkofs; 3821 unsigned int blk_per_sec = BLKS_PER_SEC(sbi); 3822 unsigned int secidx = start_blk / blk_per_sec; 3823 unsigned int end_sec = secidx + blkcnt / blk_per_sec; 3824 int ret = 0; 3825 3826 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 3827 filemap_invalidate_lock(inode->i_mapping); 3828 3829 set_inode_flag(inode, FI_ALIGNED_WRITE); 3830 set_inode_flag(inode, FI_OPU_WRITE); 3831 3832 for (; secidx < end_sec; secidx++) { 3833 f2fs_down_write(&sbi->pin_sem); 3834 3835 f2fs_lock_op(sbi); 3836 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false); 3837 f2fs_unlock_op(sbi); 3838 3839 set_inode_flag(inode, FI_SKIP_WRITES); 3840 3841 for (blkofs = 0; blkofs < blk_per_sec; blkofs++) { 3842 struct page *page; 3843 unsigned int blkidx = secidx * blk_per_sec + blkofs; 3844 3845 page = f2fs_get_lock_data_page(inode, blkidx, true); 3846 if (IS_ERR(page)) { 3847 f2fs_up_write(&sbi->pin_sem); 3848 ret = PTR_ERR(page); 3849 goto done; 3850 } 3851 3852 set_page_dirty(page); 3853 f2fs_put_page(page, 1); 3854 } 3855 3856 clear_inode_flag(inode, FI_SKIP_WRITES); 3857 3858 ret = filemap_fdatawrite(inode->i_mapping); 3859 3860 f2fs_up_write(&sbi->pin_sem); 3861 3862 if (ret) 3863 break; 3864 } 3865 3866 done: 3867 clear_inode_flag(inode, FI_SKIP_WRITES); 3868 clear_inode_flag(inode, FI_OPU_WRITE); 3869 clear_inode_flag(inode, FI_ALIGNED_WRITE); 3870 3871 filemap_invalidate_unlock(inode->i_mapping); 3872 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 3873 3874 return ret; 3875 } 3876 3877 static int check_swap_activate(struct swap_info_struct *sis, 3878 struct file *swap_file, sector_t *span) 3879 { 3880 struct address_space *mapping = swap_file->f_mapping; 3881 struct inode *inode = mapping->host; 3882 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3883 sector_t cur_lblock; 3884 sector_t last_lblock; 3885 sector_t pblock; 3886 sector_t lowest_pblock = -1; 3887 sector_t highest_pblock = 0; 3888 int nr_extents = 0; 3889 unsigned long nr_pblocks; 3890 unsigned int blks_per_sec = BLKS_PER_SEC(sbi); 3891 unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1; 3892 unsigned int not_aligned = 0; 3893 int ret = 0; 3894 3895 /* 3896 * Map all the blocks into the extent list. This code doesn't try 3897 * to be very smart. 3898 */ 3899 cur_lblock = 0; 3900 last_lblock = bytes_to_blks(inode, i_size_read(inode)); 3901 3902 while (cur_lblock < last_lblock && cur_lblock < sis->max) { 3903 struct f2fs_map_blocks map; 3904 retry: 3905 cond_resched(); 3906 3907 memset(&map, 0, sizeof(map)); 3908 map.m_lblk = cur_lblock; 3909 map.m_len = last_lblock - cur_lblock; 3910 map.m_next_pgofs = NULL; 3911 map.m_next_extent = NULL; 3912 map.m_seg_type = NO_CHECK_TYPE; 3913 map.m_may_create = false; 3914 3915 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP); 3916 if (ret) 3917 goto out; 3918 3919 /* hole */ 3920 if (!(map.m_flags & F2FS_MAP_FLAGS)) { 3921 f2fs_err(sbi, "Swapfile has holes"); 3922 ret = -EINVAL; 3923 goto out; 3924 } 3925 3926 pblock = map.m_pblk; 3927 nr_pblocks = map.m_len; 3928 3929 if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask || 3930 nr_pblocks & sec_blks_mask) { 3931 not_aligned++; 3932 3933 nr_pblocks = roundup(nr_pblocks, blks_per_sec); 3934 if (cur_lblock + nr_pblocks > sis->max) 3935 nr_pblocks -= blks_per_sec; 3936 3937 if (!nr_pblocks) { 3938 /* this extent is last one */ 3939 nr_pblocks = map.m_len; 3940 f2fs_warn(sbi, "Swapfile: last extent is not aligned to section"); 3941 goto next; 3942 } 3943 3944 ret = f2fs_migrate_blocks(inode, cur_lblock, 3945 nr_pblocks); 3946 if (ret) 3947 goto out; 3948 goto retry; 3949 } 3950 next: 3951 if (cur_lblock + nr_pblocks >= sis->max) 3952 nr_pblocks = sis->max - cur_lblock; 3953 3954 if (cur_lblock) { /* exclude the header page */ 3955 if (pblock < lowest_pblock) 3956 lowest_pblock = pblock; 3957 if (pblock + nr_pblocks - 1 > highest_pblock) 3958 highest_pblock = pblock + nr_pblocks - 1; 3959 } 3960 3961 /* 3962 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks 3963 */ 3964 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock); 3965 if (ret < 0) 3966 goto out; 3967 nr_extents += ret; 3968 cur_lblock += nr_pblocks; 3969 } 3970 ret = nr_extents; 3971 *span = 1 + highest_pblock - lowest_pblock; 3972 if (cur_lblock == 0) 3973 cur_lblock = 1; /* force Empty message */ 3974 sis->max = cur_lblock; 3975 sis->pages = cur_lblock - 1; 3976 sis->highest_bit = cur_lblock - 1; 3977 out: 3978 if (not_aligned) 3979 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%u * N)", 3980 not_aligned, blks_per_sec * F2FS_BLKSIZE); 3981 return ret; 3982 } 3983 3984 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, 3985 sector_t *span) 3986 { 3987 struct inode *inode = file_inode(file); 3988 int ret; 3989 3990 if (!S_ISREG(inode->i_mode)) 3991 return -EINVAL; 3992 3993 if (f2fs_readonly(F2FS_I_SB(inode)->sb)) 3994 return -EROFS; 3995 3996 if (f2fs_lfs_mode(F2FS_I_SB(inode))) { 3997 f2fs_err(F2FS_I_SB(inode), 3998 "Swapfile not supported in LFS mode"); 3999 return -EINVAL; 4000 } 4001 4002 ret = f2fs_convert_inline_inode(inode); 4003 if (ret) 4004 return ret; 4005 4006 if (!f2fs_disable_compressed_file(inode)) 4007 return -EINVAL; 4008 4009 f2fs_precache_extents(inode); 4010 4011 ret = check_swap_activate(sis, file, span); 4012 if (ret < 0) 4013 return ret; 4014 4015 stat_inc_swapfile_inode(inode); 4016 set_inode_flag(inode, FI_PIN_FILE); 4017 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 4018 return ret; 4019 } 4020 4021 static void f2fs_swap_deactivate(struct file *file) 4022 { 4023 struct inode *inode = file_inode(file); 4024 4025 stat_dec_swapfile_inode(inode); 4026 clear_inode_flag(inode, FI_PIN_FILE); 4027 } 4028 #else 4029 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, 4030 sector_t *span) 4031 { 4032 return -EOPNOTSUPP; 4033 } 4034 4035 static void f2fs_swap_deactivate(struct file *file) 4036 { 4037 } 4038 #endif 4039 4040 const struct address_space_operations f2fs_dblock_aops = { 4041 .read_folio = f2fs_read_data_folio, 4042 .readahead = f2fs_readahead, 4043 .writepage = f2fs_write_data_page, 4044 .writepages = f2fs_write_data_pages, 4045 .write_begin = f2fs_write_begin, 4046 .write_end = f2fs_write_end, 4047 .dirty_folio = f2fs_dirty_data_folio, 4048 .migrate_folio = filemap_migrate_folio, 4049 .invalidate_folio = f2fs_invalidate_folio, 4050 .release_folio = f2fs_release_folio, 4051 .direct_IO = noop_direct_IO, 4052 .bmap = f2fs_bmap, 4053 .swap_activate = f2fs_swap_activate, 4054 .swap_deactivate = f2fs_swap_deactivate, 4055 }; 4056 4057 void f2fs_clear_page_cache_dirty_tag(struct page *page) 4058 { 4059 struct address_space *mapping = page_mapping(page); 4060 unsigned long flags; 4061 4062 xa_lock_irqsave(&mapping->i_pages, flags); 4063 __xa_clear_mark(&mapping->i_pages, page_index(page), 4064 PAGECACHE_TAG_DIRTY); 4065 xa_unlock_irqrestore(&mapping->i_pages, flags); 4066 } 4067 4068 int __init f2fs_init_post_read_processing(void) 4069 { 4070 bio_post_read_ctx_cache = 4071 kmem_cache_create("f2fs_bio_post_read_ctx", 4072 sizeof(struct bio_post_read_ctx), 0, 0, NULL); 4073 if (!bio_post_read_ctx_cache) 4074 goto fail; 4075 bio_post_read_ctx_pool = 4076 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS, 4077 bio_post_read_ctx_cache); 4078 if (!bio_post_read_ctx_pool) 4079 goto fail_free_cache; 4080 return 0; 4081 4082 fail_free_cache: 4083 kmem_cache_destroy(bio_post_read_ctx_cache); 4084 fail: 4085 return -ENOMEM; 4086 } 4087 4088 void f2fs_destroy_post_read_processing(void) 4089 { 4090 mempool_destroy(bio_post_read_ctx_pool); 4091 kmem_cache_destroy(bio_post_read_ctx_cache); 4092 } 4093 4094 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi) 4095 { 4096 if (!f2fs_sb_has_encrypt(sbi) && 4097 !f2fs_sb_has_verity(sbi) && 4098 !f2fs_sb_has_compression(sbi)) 4099 return 0; 4100 4101 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq", 4102 WQ_UNBOUND | WQ_HIGHPRI, 4103 num_online_cpus()); 4104 return sbi->post_read_wq ? 0 : -ENOMEM; 4105 } 4106 4107 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi) 4108 { 4109 if (sbi->post_read_wq) 4110 destroy_workqueue(sbi->post_read_wq); 4111 } 4112 4113 int __init f2fs_init_bio_entry_cache(void) 4114 { 4115 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab", 4116 sizeof(struct bio_entry)); 4117 return bio_entry_slab ? 0 : -ENOMEM; 4118 } 4119 4120 void f2fs_destroy_bio_entry_cache(void) 4121 { 4122 kmem_cache_destroy(bio_entry_slab); 4123 } 4124 4125 static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 4126 unsigned int flags, struct iomap *iomap, 4127 struct iomap *srcmap) 4128 { 4129 struct f2fs_map_blocks map = {}; 4130 pgoff_t next_pgofs = 0; 4131 int err; 4132 4133 map.m_lblk = bytes_to_blks(inode, offset); 4134 map.m_len = bytes_to_blks(inode, offset + length - 1) - map.m_lblk + 1; 4135 map.m_next_pgofs = &next_pgofs; 4136 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint); 4137 if (flags & IOMAP_WRITE) 4138 map.m_may_create = true; 4139 4140 err = f2fs_map_blocks(inode, &map, flags & IOMAP_WRITE, 4141 F2FS_GET_BLOCK_DIO); 4142 if (err) 4143 return err; 4144 4145 iomap->offset = blks_to_bytes(inode, map.m_lblk); 4146 4147 /* 4148 * When inline encryption is enabled, sometimes I/O to an encrypted file 4149 * has to be broken up to guarantee DUN contiguity. Handle this by 4150 * limiting the length of the mapping returned. 4151 */ 4152 map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len); 4153 4154 if (map.m_flags & (F2FS_MAP_MAPPED | F2FS_MAP_UNWRITTEN)) { 4155 iomap->length = blks_to_bytes(inode, map.m_len); 4156 if (map.m_flags & F2FS_MAP_MAPPED) { 4157 iomap->type = IOMAP_MAPPED; 4158 iomap->flags |= IOMAP_F_MERGED; 4159 } else { 4160 iomap->type = IOMAP_UNWRITTEN; 4161 } 4162 if (WARN_ON_ONCE(!__is_valid_data_blkaddr(map.m_pblk))) 4163 return -EINVAL; 4164 4165 iomap->bdev = map.m_bdev; 4166 iomap->addr = blks_to_bytes(inode, map.m_pblk); 4167 } else { 4168 iomap->length = blks_to_bytes(inode, next_pgofs) - 4169 iomap->offset; 4170 iomap->type = IOMAP_HOLE; 4171 iomap->addr = IOMAP_NULL_ADDR; 4172 } 4173 4174 if (map.m_flags & F2FS_MAP_NEW) 4175 iomap->flags |= IOMAP_F_NEW; 4176 if ((inode->i_state & I_DIRTY_DATASYNC) || 4177 offset + length > i_size_read(inode)) 4178 iomap->flags |= IOMAP_F_DIRTY; 4179 4180 return 0; 4181 } 4182 4183 const struct iomap_ops f2fs_iomap_ops = { 4184 .iomap_begin = f2fs_iomap_begin, 4185 }; 4186