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