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