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