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