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/swap.h> 18 #include <linux/prefetch.h> 19 #include <linux/uio.h> 20 #include <linux/cleancache.h> 21 #include <linux/sched/signal.h> 22 23 #include "f2fs.h" 24 #include "node.h" 25 #include "segment.h" 26 #include "trace.h" 27 #include <trace/events/f2fs.h> 28 29 #define NUM_PREALLOC_POST_READ_CTXS 128 30 31 static struct kmem_cache *bio_post_read_ctx_cache; 32 static struct kmem_cache *bio_entry_slab; 33 static mempool_t *bio_post_read_ctx_pool; 34 35 static bool __is_cp_guaranteed(struct page *page) 36 { 37 struct address_space *mapping = page->mapping; 38 struct inode *inode; 39 struct f2fs_sb_info *sbi; 40 41 if (!mapping) 42 return false; 43 44 inode = mapping->host; 45 sbi = F2FS_I_SB(inode); 46 47 if (inode->i_ino == F2FS_META_INO(sbi) || 48 inode->i_ino == F2FS_NODE_INO(sbi) || 49 S_ISDIR(inode->i_mode) || 50 (S_ISREG(inode->i_mode) && 51 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) || 52 is_cold_data(page)) 53 return true; 54 return false; 55 } 56 57 static enum count_type __read_io_type(struct page *page) 58 { 59 struct address_space *mapping = page_file_mapping(page); 60 61 if (mapping) { 62 struct inode *inode = mapping->host; 63 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 64 65 if (inode->i_ino == F2FS_META_INO(sbi)) 66 return F2FS_RD_META; 67 68 if (inode->i_ino == F2FS_NODE_INO(sbi)) 69 return F2FS_RD_NODE; 70 } 71 return F2FS_RD_DATA; 72 } 73 74 /* postprocessing steps for read bios */ 75 enum bio_post_read_step { 76 STEP_INITIAL = 0, 77 STEP_DECRYPT, 78 STEP_VERITY, 79 }; 80 81 struct bio_post_read_ctx { 82 struct bio *bio; 83 struct work_struct work; 84 unsigned int cur_step; 85 unsigned int enabled_steps; 86 }; 87 88 static void __read_end_io(struct bio *bio) 89 { 90 struct page *page; 91 struct bio_vec *bv; 92 struct bvec_iter_all iter_all; 93 94 bio_for_each_segment_all(bv, bio, iter_all) { 95 page = bv->bv_page; 96 97 /* PG_error was set if any post_read step failed */ 98 if (bio->bi_status || PageError(page)) { 99 ClearPageUptodate(page); 100 /* will re-read again later */ 101 ClearPageError(page); 102 } else { 103 SetPageUptodate(page); 104 } 105 dec_page_count(F2FS_P_SB(page), __read_io_type(page)); 106 unlock_page(page); 107 } 108 if (bio->bi_private) 109 mempool_free(bio->bi_private, bio_post_read_ctx_pool); 110 bio_put(bio); 111 } 112 113 static void bio_post_read_processing(struct bio_post_read_ctx *ctx); 114 115 static void decrypt_work(struct work_struct *work) 116 { 117 struct bio_post_read_ctx *ctx = 118 container_of(work, struct bio_post_read_ctx, work); 119 120 fscrypt_decrypt_bio(ctx->bio); 121 122 bio_post_read_processing(ctx); 123 } 124 125 static void verity_work(struct work_struct *work) 126 { 127 struct bio_post_read_ctx *ctx = 128 container_of(work, struct bio_post_read_ctx, work); 129 130 fsverity_verify_bio(ctx->bio); 131 132 bio_post_read_processing(ctx); 133 } 134 135 static void bio_post_read_processing(struct bio_post_read_ctx *ctx) 136 { 137 /* 138 * We use different work queues for decryption and for verity because 139 * verity may require reading metadata pages that need decryption, and 140 * we shouldn't recurse to the same workqueue. 141 */ 142 switch (++ctx->cur_step) { 143 case STEP_DECRYPT: 144 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) { 145 INIT_WORK(&ctx->work, decrypt_work); 146 fscrypt_enqueue_decrypt_work(&ctx->work); 147 return; 148 } 149 ctx->cur_step++; 150 /* fall-through */ 151 case STEP_VERITY: 152 if (ctx->enabled_steps & (1 << STEP_VERITY)) { 153 INIT_WORK(&ctx->work, verity_work); 154 fsverity_enqueue_verify_work(&ctx->work); 155 return; 156 } 157 ctx->cur_step++; 158 /* fall-through */ 159 default: 160 __read_end_io(ctx->bio); 161 } 162 } 163 164 static bool f2fs_bio_post_read_required(struct bio *bio) 165 { 166 return bio->bi_private && !bio->bi_status; 167 } 168 169 static void f2fs_read_end_io(struct bio *bio) 170 { 171 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio)); 172 173 if (time_to_inject(sbi, FAULT_READ_IO)) { 174 f2fs_show_injection_info(sbi, FAULT_READ_IO); 175 bio->bi_status = BLK_STS_IOERR; 176 } 177 178 if (f2fs_bio_post_read_required(bio)) { 179 struct bio_post_read_ctx *ctx = bio->bi_private; 180 181 ctx->cur_step = STEP_INITIAL; 182 bio_post_read_processing(ctx); 183 return; 184 } 185 186 __read_end_io(bio); 187 } 188 189 static void f2fs_write_end_io(struct bio *bio) 190 { 191 struct f2fs_sb_info *sbi = bio->bi_private; 192 struct bio_vec *bvec; 193 struct bvec_iter_all iter_all; 194 195 if (time_to_inject(sbi, FAULT_WRITE_IO)) { 196 f2fs_show_injection_info(sbi, FAULT_WRITE_IO); 197 bio->bi_status = BLK_STS_IOERR; 198 } 199 200 bio_for_each_segment_all(bvec, bio, iter_all) { 201 struct page *page = bvec->bv_page; 202 enum count_type type = WB_DATA_TYPE(page); 203 204 if (IS_DUMMY_WRITTEN_PAGE(page)) { 205 set_page_private(page, (unsigned long)NULL); 206 ClearPagePrivate(page); 207 unlock_page(page); 208 mempool_free(page, sbi->write_io_dummy); 209 210 if (unlikely(bio->bi_status)) 211 f2fs_stop_checkpoint(sbi, true); 212 continue; 213 } 214 215 fscrypt_finalize_bounce_page(&page); 216 217 if (unlikely(bio->bi_status)) { 218 mapping_set_error(page->mapping, -EIO); 219 if (type == F2FS_WB_CP_DATA) 220 f2fs_stop_checkpoint(sbi, true); 221 } 222 223 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) && 224 page->index != nid_of_node(page)); 225 226 dec_page_count(sbi, type); 227 if (f2fs_in_warm_node_list(sbi, page)) 228 f2fs_del_fsync_node_entry(sbi, page); 229 clear_cold_data(page); 230 end_page_writeback(page); 231 } 232 if (!get_pages(sbi, F2FS_WB_CP_DATA) && 233 wq_has_sleeper(&sbi->cp_wait)) 234 wake_up(&sbi->cp_wait); 235 236 bio_put(bio); 237 } 238 239 /* 240 * Return true, if pre_bio's bdev is same as its target device. 241 */ 242 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi, 243 block_t blk_addr, struct bio *bio) 244 { 245 struct block_device *bdev = sbi->sb->s_bdev; 246 int i; 247 248 if (f2fs_is_multi_device(sbi)) { 249 for (i = 0; i < sbi->s_ndevs; i++) { 250 if (FDEV(i).start_blk <= blk_addr && 251 FDEV(i).end_blk >= blk_addr) { 252 blk_addr -= FDEV(i).start_blk; 253 bdev = FDEV(i).bdev; 254 break; 255 } 256 } 257 } 258 if (bio) { 259 bio_set_dev(bio, bdev); 260 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr); 261 } 262 return bdev; 263 } 264 265 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr) 266 { 267 int i; 268 269 if (!f2fs_is_multi_device(sbi)) 270 return 0; 271 272 for (i = 0; i < sbi->s_ndevs; i++) 273 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr) 274 return i; 275 return 0; 276 } 277 278 static bool __same_bdev(struct f2fs_sb_info *sbi, 279 block_t blk_addr, struct bio *bio) 280 { 281 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL); 282 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno; 283 } 284 285 /* 286 * Low-level block read/write IO operations. 287 */ 288 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages) 289 { 290 struct f2fs_sb_info *sbi = fio->sbi; 291 struct bio *bio; 292 293 bio = f2fs_bio_alloc(sbi, npages, true); 294 295 f2fs_target_device(sbi, fio->new_blkaddr, bio); 296 if (is_read_io(fio->op)) { 297 bio->bi_end_io = f2fs_read_end_io; 298 bio->bi_private = NULL; 299 } else { 300 bio->bi_end_io = f2fs_write_end_io; 301 bio->bi_private = sbi; 302 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, 303 fio->type, fio->temp); 304 } 305 if (fio->io_wbc) 306 wbc_init_bio(fio->io_wbc, bio); 307 308 return bio; 309 } 310 311 static inline void __submit_bio(struct f2fs_sb_info *sbi, 312 struct bio *bio, enum page_type type) 313 { 314 if (!is_read_io(bio_op(bio))) { 315 unsigned int start; 316 317 if (type != DATA && type != NODE) 318 goto submit_io; 319 320 if (test_opt(sbi, LFS) && current->plug) 321 blk_finish_plug(current->plug); 322 323 if (F2FS_IO_ALIGNED(sbi)) 324 goto submit_io; 325 326 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS; 327 start %= F2FS_IO_SIZE(sbi); 328 329 if (start == 0) 330 goto submit_io; 331 332 /* fill dummy pages */ 333 for (; start < F2FS_IO_SIZE(sbi); start++) { 334 struct page *page = 335 mempool_alloc(sbi->write_io_dummy, 336 GFP_NOIO | __GFP_NOFAIL); 337 f2fs_bug_on(sbi, !page); 338 339 zero_user_segment(page, 0, PAGE_SIZE); 340 SetPagePrivate(page); 341 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE); 342 lock_page(page); 343 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) 344 f2fs_bug_on(sbi, 1); 345 } 346 /* 347 * In the NODE case, we lose next block address chain. So, we 348 * need to do checkpoint in f2fs_sync_file. 349 */ 350 if (type == NODE) 351 set_sbi_flag(sbi, SBI_NEED_CP); 352 } 353 submit_io: 354 if (is_read_io(bio_op(bio))) 355 trace_f2fs_submit_read_bio(sbi->sb, type, bio); 356 else 357 trace_f2fs_submit_write_bio(sbi->sb, type, bio); 358 submit_bio(bio); 359 } 360 361 static void __submit_merged_bio(struct f2fs_bio_info *io) 362 { 363 struct f2fs_io_info *fio = &io->fio; 364 365 if (!io->bio) 366 return; 367 368 bio_set_op_attrs(io->bio, fio->op, fio->op_flags); 369 370 if (is_read_io(fio->op)) 371 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio); 372 else 373 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio); 374 375 __submit_bio(io->sbi, io->bio, fio->type); 376 io->bio = NULL; 377 } 378 379 static bool __has_merged_page(struct bio *bio, struct inode *inode, 380 struct page *page, nid_t ino) 381 { 382 struct bio_vec *bvec; 383 struct page *target; 384 struct bvec_iter_all iter_all; 385 386 if (!bio) 387 return false; 388 389 if (!inode && !page && !ino) 390 return true; 391 392 bio_for_each_segment_all(bvec, bio, iter_all) { 393 394 target = bvec->bv_page; 395 if (fscrypt_is_bounce_page(target)) 396 target = fscrypt_pagecache_page(target); 397 398 if (inode && inode == target->mapping->host) 399 return true; 400 if (page && page == target) 401 return true; 402 if (ino && ino == ino_of_node(target)) 403 return true; 404 } 405 406 return false; 407 } 408 409 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi, 410 enum page_type type, enum temp_type temp) 411 { 412 enum page_type btype = PAGE_TYPE_OF_BIO(type); 413 struct f2fs_bio_info *io = sbi->write_io[btype] + temp; 414 415 down_write(&io->io_rwsem); 416 417 /* change META to META_FLUSH in the checkpoint procedure */ 418 if (type >= META_FLUSH) { 419 io->fio.type = META_FLUSH; 420 io->fio.op = REQ_OP_WRITE; 421 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC; 422 if (!test_opt(sbi, NOBARRIER)) 423 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA; 424 } 425 __submit_merged_bio(io); 426 up_write(&io->io_rwsem); 427 } 428 429 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi, 430 struct inode *inode, struct page *page, 431 nid_t ino, enum page_type type, bool force) 432 { 433 enum temp_type temp; 434 bool ret = true; 435 436 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) { 437 if (!force) { 438 enum page_type btype = PAGE_TYPE_OF_BIO(type); 439 struct f2fs_bio_info *io = sbi->write_io[btype] + temp; 440 441 down_read(&io->io_rwsem); 442 ret = __has_merged_page(io->bio, inode, page, ino); 443 up_read(&io->io_rwsem); 444 } 445 if (ret) 446 __f2fs_submit_merged_write(sbi, type, temp); 447 448 /* TODO: use HOT temp only for meta pages now. */ 449 if (type >= META) 450 break; 451 } 452 } 453 454 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type) 455 { 456 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true); 457 } 458 459 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi, 460 struct inode *inode, struct page *page, 461 nid_t ino, enum page_type type) 462 { 463 __submit_merged_write_cond(sbi, inode, page, ino, type, false); 464 } 465 466 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi) 467 { 468 f2fs_submit_merged_write(sbi, DATA); 469 f2fs_submit_merged_write(sbi, NODE); 470 f2fs_submit_merged_write(sbi, META); 471 } 472 473 /* 474 * Fill the locked page with data located in the block address. 475 * A caller needs to unlock the page on failure. 476 */ 477 int f2fs_submit_page_bio(struct f2fs_io_info *fio) 478 { 479 struct bio *bio; 480 struct page *page = fio->encrypted_page ? 481 fio->encrypted_page : fio->page; 482 483 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, 484 fio->is_por ? META_POR : (__is_meta_io(fio) ? 485 META_GENERIC : DATA_GENERIC_ENHANCE))) 486 return -EFSCORRUPTED; 487 488 trace_f2fs_submit_page_bio(page, fio); 489 f2fs_trace_ios(fio, 0); 490 491 /* Allocate a new bio */ 492 bio = __bio_alloc(fio, 1); 493 494 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { 495 bio_put(bio); 496 return -EFAULT; 497 } 498 499 if (fio->io_wbc && !is_read_io(fio->op)) 500 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE); 501 502 bio_set_op_attrs(bio, fio->op, fio->op_flags); 503 504 inc_page_count(fio->sbi, is_read_io(fio->op) ? 505 __read_io_type(page): WB_DATA_TYPE(fio->page)); 506 507 __submit_bio(fio->sbi, bio, fio->type); 508 return 0; 509 } 510 511 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, 512 block_t last_blkaddr, block_t cur_blkaddr) 513 { 514 if (last_blkaddr + 1 != cur_blkaddr) 515 return false; 516 return __same_bdev(sbi, cur_blkaddr, bio); 517 } 518 519 static bool io_type_is_mergeable(struct f2fs_bio_info *io, 520 struct f2fs_io_info *fio) 521 { 522 if (io->fio.op != fio->op) 523 return false; 524 return io->fio.op_flags == fio->op_flags; 525 } 526 527 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, 528 struct f2fs_bio_info *io, 529 struct f2fs_io_info *fio, 530 block_t last_blkaddr, 531 block_t cur_blkaddr) 532 { 533 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) { 534 unsigned int filled_blocks = 535 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size); 536 unsigned int io_size = F2FS_IO_SIZE(sbi); 537 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt; 538 539 /* IOs in bio is aligned and left space of vectors is not enough */ 540 if (!(filled_blocks % io_size) && left_vecs < io_size) 541 return false; 542 } 543 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr)) 544 return false; 545 return io_type_is_mergeable(io, fio); 546 } 547 548 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio, 549 struct page *page, enum temp_type temp) 550 { 551 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; 552 struct bio_entry *be; 553 554 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS); 555 be->bio = bio; 556 bio_get(bio); 557 558 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE) 559 f2fs_bug_on(sbi, 1); 560 561 down_write(&io->bio_list_lock); 562 list_add_tail(&be->list, &io->bio_list); 563 up_write(&io->bio_list_lock); 564 } 565 566 static void del_bio_entry(struct bio_entry *be) 567 { 568 list_del(&be->list); 569 kmem_cache_free(bio_entry_slab, be); 570 } 571 572 static int add_ipu_page(struct f2fs_sb_info *sbi, struct bio **bio, 573 struct page *page) 574 { 575 enum temp_type temp; 576 bool found = false; 577 int ret = -EAGAIN; 578 579 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { 580 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; 581 struct list_head *head = &io->bio_list; 582 struct bio_entry *be; 583 584 down_write(&io->bio_list_lock); 585 list_for_each_entry(be, head, list) { 586 if (be->bio != *bio) 587 continue; 588 589 found = true; 590 591 if (bio_add_page(*bio, page, PAGE_SIZE, 0) == PAGE_SIZE) { 592 ret = 0; 593 break; 594 } 595 596 /* bio is full */ 597 del_bio_entry(be); 598 __submit_bio(sbi, *bio, DATA); 599 break; 600 } 601 up_write(&io->bio_list_lock); 602 } 603 604 if (ret) { 605 bio_put(*bio); 606 *bio = NULL; 607 } 608 609 return ret; 610 } 611 612 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi, 613 struct bio **bio, struct page *page) 614 { 615 enum temp_type temp; 616 bool found = false; 617 struct bio *target = bio ? *bio : NULL; 618 619 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { 620 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; 621 struct list_head *head = &io->bio_list; 622 struct bio_entry *be; 623 624 if (list_empty(head)) 625 continue; 626 627 down_read(&io->bio_list_lock); 628 list_for_each_entry(be, head, list) { 629 if (target) 630 found = (target == be->bio); 631 else 632 found = __has_merged_page(be->bio, NULL, 633 page, 0); 634 if (found) 635 break; 636 } 637 up_read(&io->bio_list_lock); 638 639 if (!found) 640 continue; 641 642 found = false; 643 644 down_write(&io->bio_list_lock); 645 list_for_each_entry(be, head, list) { 646 if (target) 647 found = (target == be->bio); 648 else 649 found = __has_merged_page(be->bio, NULL, 650 page, 0); 651 if (found) { 652 target = be->bio; 653 del_bio_entry(be); 654 break; 655 } 656 } 657 up_write(&io->bio_list_lock); 658 } 659 660 if (found) 661 __submit_bio(sbi, target, DATA); 662 if (bio && *bio) { 663 bio_put(*bio); 664 *bio = NULL; 665 } 666 } 667 668 int f2fs_merge_page_bio(struct f2fs_io_info *fio) 669 { 670 struct bio *bio = *fio->bio; 671 struct page *page = fio->encrypted_page ? 672 fio->encrypted_page : fio->page; 673 674 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, 675 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC)) 676 return -EFSCORRUPTED; 677 678 trace_f2fs_submit_page_bio(page, fio); 679 f2fs_trace_ios(fio, 0); 680 681 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block, 682 fio->new_blkaddr)) 683 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL); 684 alloc_new: 685 if (!bio) { 686 bio = __bio_alloc(fio, BIO_MAX_PAGES); 687 bio_set_op_attrs(bio, fio->op, fio->op_flags); 688 689 add_bio_entry(fio->sbi, bio, page, fio->temp); 690 } else { 691 if (add_ipu_page(fio->sbi, &bio, page)) 692 goto alloc_new; 693 } 694 695 if (fio->io_wbc) 696 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE); 697 698 inc_page_count(fio->sbi, WB_DATA_TYPE(page)); 699 700 *fio->last_block = fio->new_blkaddr; 701 *fio->bio = bio; 702 703 return 0; 704 } 705 706 void f2fs_submit_page_write(struct f2fs_io_info *fio) 707 { 708 struct f2fs_sb_info *sbi = fio->sbi; 709 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); 710 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp; 711 struct page *bio_page; 712 713 f2fs_bug_on(sbi, is_read_io(fio->op)); 714 715 down_write(&io->io_rwsem); 716 next: 717 if (fio->in_list) { 718 spin_lock(&io->io_lock); 719 if (list_empty(&io->io_list)) { 720 spin_unlock(&io->io_lock); 721 goto out; 722 } 723 fio = list_first_entry(&io->io_list, 724 struct f2fs_io_info, list); 725 list_del(&fio->list); 726 spin_unlock(&io->io_lock); 727 } 728 729 verify_fio_blkaddr(fio); 730 731 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page; 732 733 /* set submitted = true as a return value */ 734 fio->submitted = true; 735 736 inc_page_count(sbi, WB_DATA_TYPE(bio_page)); 737 738 if (io->bio && !io_is_mergeable(sbi, io->bio, io, fio, 739 io->last_block_in_bio, fio->new_blkaddr)) 740 __submit_merged_bio(io); 741 alloc_new: 742 if (io->bio == NULL) { 743 if (F2FS_IO_ALIGNED(sbi) && 744 (fio->type == DATA || fio->type == NODE) && 745 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) { 746 dec_page_count(sbi, WB_DATA_TYPE(bio_page)); 747 fio->retry = true; 748 goto skip; 749 } 750 io->bio = __bio_alloc(fio, BIO_MAX_PAGES); 751 io->fio = *fio; 752 } 753 754 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) { 755 __submit_merged_bio(io); 756 goto alloc_new; 757 } 758 759 if (fio->io_wbc) 760 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE); 761 762 io->last_block_in_bio = fio->new_blkaddr; 763 f2fs_trace_ios(fio, 0); 764 765 trace_f2fs_submit_page_write(fio->page, fio); 766 skip: 767 if (fio->in_list) 768 goto next; 769 out: 770 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || 771 !f2fs_is_checkpoint_ready(sbi)) 772 __submit_merged_bio(io); 773 up_write(&io->io_rwsem); 774 } 775 776 static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx) 777 { 778 return fsverity_active(inode) && 779 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE); 780 } 781 782 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr, 783 unsigned nr_pages, unsigned op_flag, 784 pgoff_t first_idx) 785 { 786 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 787 struct bio *bio; 788 struct bio_post_read_ctx *ctx; 789 unsigned int post_read_steps = 0; 790 791 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false); 792 if (!bio) 793 return ERR_PTR(-ENOMEM); 794 f2fs_target_device(sbi, blkaddr, bio); 795 bio->bi_end_io = f2fs_read_end_io; 796 bio_set_op_attrs(bio, REQ_OP_READ, op_flag); 797 798 if (f2fs_encrypted_file(inode)) 799 post_read_steps |= 1 << STEP_DECRYPT; 800 801 if (f2fs_need_verity(inode, first_idx)) 802 post_read_steps |= 1 << STEP_VERITY; 803 804 if (post_read_steps) { 805 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS); 806 if (!ctx) { 807 bio_put(bio); 808 return ERR_PTR(-ENOMEM); 809 } 810 ctx->bio = bio; 811 ctx->enabled_steps = post_read_steps; 812 bio->bi_private = ctx; 813 } 814 815 return bio; 816 } 817 818 /* This can handle encryption stuffs */ 819 static int f2fs_submit_page_read(struct inode *inode, struct page *page, 820 block_t blkaddr) 821 { 822 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 823 struct bio *bio; 824 825 bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0, page->index); 826 if (IS_ERR(bio)) 827 return PTR_ERR(bio); 828 829 /* wait for GCed page writeback via META_MAPPING */ 830 f2fs_wait_on_block_writeback(inode, blkaddr); 831 832 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { 833 bio_put(bio); 834 return -EFAULT; 835 } 836 ClearPageError(page); 837 inc_page_count(sbi, F2FS_RD_DATA); 838 __submit_bio(sbi, bio, DATA); 839 return 0; 840 } 841 842 static void __set_data_blkaddr(struct dnode_of_data *dn) 843 { 844 struct f2fs_node *rn = F2FS_NODE(dn->node_page); 845 __le32 *addr_array; 846 int base = 0; 847 848 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode)) 849 base = get_extra_isize(dn->inode); 850 851 /* Get physical address of data block */ 852 addr_array = blkaddr_in_node(rn); 853 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr); 854 } 855 856 /* 857 * Lock ordering for the change of data block address: 858 * ->data_page 859 * ->node_page 860 * update block addresses in the node page 861 */ 862 void f2fs_set_data_blkaddr(struct dnode_of_data *dn) 863 { 864 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true); 865 __set_data_blkaddr(dn); 866 if (set_page_dirty(dn->node_page)) 867 dn->node_changed = true; 868 } 869 870 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) 871 { 872 dn->data_blkaddr = blkaddr; 873 f2fs_set_data_blkaddr(dn); 874 f2fs_update_extent_cache(dn); 875 } 876 877 /* dn->ofs_in_node will be returned with up-to-date last block pointer */ 878 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) 879 { 880 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 881 int err; 882 883 if (!count) 884 return 0; 885 886 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) 887 return -EPERM; 888 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) 889 return err; 890 891 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, 892 dn->ofs_in_node, count); 893 894 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true); 895 896 for (; count > 0; dn->ofs_in_node++) { 897 block_t blkaddr = datablock_addr(dn->inode, 898 dn->node_page, dn->ofs_in_node); 899 if (blkaddr == NULL_ADDR) { 900 dn->data_blkaddr = NEW_ADDR; 901 __set_data_blkaddr(dn); 902 count--; 903 } 904 } 905 906 if (set_page_dirty(dn->node_page)) 907 dn->node_changed = true; 908 return 0; 909 } 910 911 /* Should keep dn->ofs_in_node unchanged */ 912 int f2fs_reserve_new_block(struct dnode_of_data *dn) 913 { 914 unsigned int ofs_in_node = dn->ofs_in_node; 915 int ret; 916 917 ret = f2fs_reserve_new_blocks(dn, 1); 918 dn->ofs_in_node = ofs_in_node; 919 return ret; 920 } 921 922 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) 923 { 924 bool need_put = dn->inode_page ? false : true; 925 int err; 926 927 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE); 928 if (err) 929 return err; 930 931 if (dn->data_blkaddr == NULL_ADDR) 932 err = f2fs_reserve_new_block(dn); 933 if (err || need_put) 934 f2fs_put_dnode(dn); 935 return err; 936 } 937 938 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index) 939 { 940 struct extent_info ei = {0,0,0}; 941 struct inode *inode = dn->inode; 942 943 if (f2fs_lookup_extent_cache(inode, index, &ei)) { 944 dn->data_blkaddr = ei.blk + index - ei.fofs; 945 return 0; 946 } 947 948 return f2fs_reserve_block(dn, index); 949 } 950 951 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index, 952 int op_flags, bool for_write) 953 { 954 struct address_space *mapping = inode->i_mapping; 955 struct dnode_of_data dn; 956 struct page *page; 957 struct extent_info ei = {0,0,0}; 958 int err; 959 960 page = f2fs_grab_cache_page(mapping, index, for_write); 961 if (!page) 962 return ERR_PTR(-ENOMEM); 963 964 if (f2fs_lookup_extent_cache(inode, index, &ei)) { 965 dn.data_blkaddr = ei.blk + index - ei.fofs; 966 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr, 967 DATA_GENERIC_ENHANCE_READ)) { 968 err = -EFSCORRUPTED; 969 goto put_err; 970 } 971 goto got_it; 972 } 973 974 set_new_dnode(&dn, inode, NULL, NULL, 0); 975 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 976 if (err) 977 goto put_err; 978 f2fs_put_dnode(&dn); 979 980 if (unlikely(dn.data_blkaddr == NULL_ADDR)) { 981 err = -ENOENT; 982 goto put_err; 983 } 984 if (dn.data_blkaddr != NEW_ADDR && 985 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode), 986 dn.data_blkaddr, 987 DATA_GENERIC_ENHANCE)) { 988 err = -EFSCORRUPTED; 989 goto put_err; 990 } 991 got_it: 992 if (PageUptodate(page)) { 993 unlock_page(page); 994 return page; 995 } 996 997 /* 998 * A new dentry page is allocated but not able to be written, since its 999 * new inode page couldn't be allocated due to -ENOSPC. 1000 * In such the case, its blkaddr can be remained as NEW_ADDR. 1001 * see, f2fs_add_link -> f2fs_get_new_data_page -> 1002 * f2fs_init_inode_metadata. 1003 */ 1004 if (dn.data_blkaddr == NEW_ADDR) { 1005 zero_user_segment(page, 0, PAGE_SIZE); 1006 if (!PageUptodate(page)) 1007 SetPageUptodate(page); 1008 unlock_page(page); 1009 return page; 1010 } 1011 1012 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr); 1013 if (err) 1014 goto put_err; 1015 return page; 1016 1017 put_err: 1018 f2fs_put_page(page, 1); 1019 return ERR_PTR(err); 1020 } 1021 1022 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index) 1023 { 1024 struct address_space *mapping = inode->i_mapping; 1025 struct page *page; 1026 1027 page = find_get_page(mapping, index); 1028 if (page && PageUptodate(page)) 1029 return page; 1030 f2fs_put_page(page, 0); 1031 1032 page = f2fs_get_read_data_page(inode, index, 0, false); 1033 if (IS_ERR(page)) 1034 return page; 1035 1036 if (PageUptodate(page)) 1037 return page; 1038 1039 wait_on_page_locked(page); 1040 if (unlikely(!PageUptodate(page))) { 1041 f2fs_put_page(page, 0); 1042 return ERR_PTR(-EIO); 1043 } 1044 return page; 1045 } 1046 1047 /* 1048 * If it tries to access a hole, return an error. 1049 * Because, the callers, functions in dir.c and GC, should be able to know 1050 * whether this page exists or not. 1051 */ 1052 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index, 1053 bool for_write) 1054 { 1055 struct address_space *mapping = inode->i_mapping; 1056 struct page *page; 1057 repeat: 1058 page = f2fs_get_read_data_page(inode, index, 0, for_write); 1059 if (IS_ERR(page)) 1060 return page; 1061 1062 /* wait for read completion */ 1063 lock_page(page); 1064 if (unlikely(page->mapping != mapping)) { 1065 f2fs_put_page(page, 1); 1066 goto repeat; 1067 } 1068 if (unlikely(!PageUptodate(page))) { 1069 f2fs_put_page(page, 1); 1070 return ERR_PTR(-EIO); 1071 } 1072 return page; 1073 } 1074 1075 /* 1076 * Caller ensures that this data page is never allocated. 1077 * A new zero-filled data page is allocated in the page cache. 1078 * 1079 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and 1080 * f2fs_unlock_op(). 1081 * Note that, ipage is set only by make_empty_dir, and if any error occur, 1082 * ipage should be released by this function. 1083 */ 1084 struct page *f2fs_get_new_data_page(struct inode *inode, 1085 struct page *ipage, pgoff_t index, bool new_i_size) 1086 { 1087 struct address_space *mapping = inode->i_mapping; 1088 struct page *page; 1089 struct dnode_of_data dn; 1090 int err; 1091 1092 page = f2fs_grab_cache_page(mapping, index, true); 1093 if (!page) { 1094 /* 1095 * before exiting, we should make sure ipage will be released 1096 * if any error occur. 1097 */ 1098 f2fs_put_page(ipage, 1); 1099 return ERR_PTR(-ENOMEM); 1100 } 1101 1102 set_new_dnode(&dn, inode, ipage, NULL, 0); 1103 err = f2fs_reserve_block(&dn, index); 1104 if (err) { 1105 f2fs_put_page(page, 1); 1106 return ERR_PTR(err); 1107 } 1108 if (!ipage) 1109 f2fs_put_dnode(&dn); 1110 1111 if (PageUptodate(page)) 1112 goto got_it; 1113 1114 if (dn.data_blkaddr == NEW_ADDR) { 1115 zero_user_segment(page, 0, PAGE_SIZE); 1116 if (!PageUptodate(page)) 1117 SetPageUptodate(page); 1118 } else { 1119 f2fs_put_page(page, 1); 1120 1121 /* if ipage exists, blkaddr should be NEW_ADDR */ 1122 f2fs_bug_on(F2FS_I_SB(inode), ipage); 1123 page = f2fs_get_lock_data_page(inode, index, true); 1124 if (IS_ERR(page)) 1125 return page; 1126 } 1127 got_it: 1128 if (new_i_size && i_size_read(inode) < 1129 ((loff_t)(index + 1) << PAGE_SHIFT)) 1130 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT)); 1131 return page; 1132 } 1133 1134 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type) 1135 { 1136 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 1137 struct f2fs_summary sum; 1138 struct node_info ni; 1139 block_t old_blkaddr; 1140 blkcnt_t count = 1; 1141 int err; 1142 1143 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) 1144 return -EPERM; 1145 1146 err = f2fs_get_node_info(sbi, dn->nid, &ni); 1147 if (err) 1148 return err; 1149 1150 dn->data_blkaddr = datablock_addr(dn->inode, 1151 dn->node_page, dn->ofs_in_node); 1152 if (dn->data_blkaddr != NULL_ADDR) 1153 goto alloc; 1154 1155 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) 1156 return err; 1157 1158 alloc: 1159 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); 1160 old_blkaddr = dn->data_blkaddr; 1161 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr, 1162 &sum, seg_type, NULL, false); 1163 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) 1164 invalidate_mapping_pages(META_MAPPING(sbi), 1165 old_blkaddr, old_blkaddr); 1166 f2fs_update_data_blkaddr(dn, dn->data_blkaddr); 1167 1168 /* 1169 * i_size will be updated by direct_IO. Otherwise, we'll get stale 1170 * data from unwritten block via dio_read. 1171 */ 1172 return 0; 1173 } 1174 1175 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from) 1176 { 1177 struct inode *inode = file_inode(iocb->ki_filp); 1178 struct f2fs_map_blocks map; 1179 int flag; 1180 int err = 0; 1181 bool direct_io = iocb->ki_flags & IOCB_DIRECT; 1182 1183 /* convert inline data for Direct I/O*/ 1184 if (direct_io) { 1185 err = f2fs_convert_inline_inode(inode); 1186 if (err) 1187 return err; 1188 } 1189 1190 if (direct_io && allow_outplace_dio(inode, iocb, from)) 1191 return 0; 1192 1193 if (is_inode_flag_set(inode, FI_NO_PREALLOC)) 1194 return 0; 1195 1196 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos); 1197 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from)); 1198 if (map.m_len > map.m_lblk) 1199 map.m_len -= map.m_lblk; 1200 else 1201 map.m_len = 0; 1202 1203 map.m_next_pgofs = NULL; 1204 map.m_next_extent = NULL; 1205 map.m_seg_type = NO_CHECK_TYPE; 1206 map.m_may_create = true; 1207 1208 if (direct_io) { 1209 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint); 1210 flag = f2fs_force_buffered_io(inode, iocb, from) ? 1211 F2FS_GET_BLOCK_PRE_AIO : 1212 F2FS_GET_BLOCK_PRE_DIO; 1213 goto map_blocks; 1214 } 1215 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) { 1216 err = f2fs_convert_inline_inode(inode); 1217 if (err) 1218 return err; 1219 } 1220 if (f2fs_has_inline_data(inode)) 1221 return err; 1222 1223 flag = F2FS_GET_BLOCK_PRE_AIO; 1224 1225 map_blocks: 1226 err = f2fs_map_blocks(inode, &map, 1, flag); 1227 if (map.m_len > 0 && err == -ENOSPC) { 1228 if (!direct_io) 1229 set_inode_flag(inode, FI_NO_PREALLOC); 1230 err = 0; 1231 } 1232 return err; 1233 } 1234 1235 void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock) 1236 { 1237 if (flag == F2FS_GET_BLOCK_PRE_AIO) { 1238 if (lock) 1239 down_read(&sbi->node_change); 1240 else 1241 up_read(&sbi->node_change); 1242 } else { 1243 if (lock) 1244 f2fs_lock_op(sbi); 1245 else 1246 f2fs_unlock_op(sbi); 1247 } 1248 } 1249 1250 /* 1251 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with 1252 * f2fs_map_blocks structure. 1253 * If original data blocks are allocated, then give them to blockdev. 1254 * Otherwise, 1255 * a. preallocate requested block addresses 1256 * b. do not use extent cache for better performance 1257 * c. give the block addresses to blockdev 1258 */ 1259 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, 1260 int create, int flag) 1261 { 1262 unsigned int maxblocks = map->m_len; 1263 struct dnode_of_data dn; 1264 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1265 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE; 1266 pgoff_t pgofs, end_offset, end; 1267 int err = 0, ofs = 1; 1268 unsigned int ofs_in_node, last_ofs_in_node; 1269 blkcnt_t prealloc; 1270 struct extent_info ei = {0,0,0}; 1271 block_t blkaddr; 1272 unsigned int start_pgofs; 1273 1274 if (!maxblocks) 1275 return 0; 1276 1277 map->m_len = 0; 1278 map->m_flags = 0; 1279 1280 /* it only supports block size == page size */ 1281 pgofs = (pgoff_t)map->m_lblk; 1282 end = pgofs + maxblocks; 1283 1284 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) { 1285 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO && 1286 map->m_may_create) 1287 goto next_dnode; 1288 1289 map->m_pblk = ei.blk + pgofs - ei.fofs; 1290 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs); 1291 map->m_flags = F2FS_MAP_MAPPED; 1292 if (map->m_next_extent) 1293 *map->m_next_extent = pgofs + map->m_len; 1294 1295 /* for hardware encryption, but to avoid potential issue in future */ 1296 if (flag == F2FS_GET_BLOCK_DIO) 1297 f2fs_wait_on_block_writeback_range(inode, 1298 map->m_pblk, map->m_len); 1299 goto out; 1300 } 1301 1302 next_dnode: 1303 if (map->m_may_create) 1304 __do_map_lock(sbi, flag, true); 1305 1306 /* When reading holes, we need its node page */ 1307 set_new_dnode(&dn, inode, NULL, NULL, 0); 1308 err = f2fs_get_dnode_of_data(&dn, pgofs, mode); 1309 if (err) { 1310 if (flag == F2FS_GET_BLOCK_BMAP) 1311 map->m_pblk = 0; 1312 if (err == -ENOENT) { 1313 err = 0; 1314 if (map->m_next_pgofs) 1315 *map->m_next_pgofs = 1316 f2fs_get_next_page_offset(&dn, pgofs); 1317 if (map->m_next_extent) 1318 *map->m_next_extent = 1319 f2fs_get_next_page_offset(&dn, pgofs); 1320 } 1321 goto unlock_out; 1322 } 1323 1324 start_pgofs = pgofs; 1325 prealloc = 0; 1326 last_ofs_in_node = ofs_in_node = dn.ofs_in_node; 1327 end_offset = ADDRS_PER_PAGE(dn.node_page, inode); 1328 1329 next_block: 1330 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node); 1331 1332 if (__is_valid_data_blkaddr(blkaddr) && 1333 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) { 1334 err = -EFSCORRUPTED; 1335 goto sync_out; 1336 } 1337 1338 if (__is_valid_data_blkaddr(blkaddr)) { 1339 /* use out-place-update for driect IO under LFS mode */ 1340 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO && 1341 map->m_may_create) { 1342 err = __allocate_data_block(&dn, map->m_seg_type); 1343 if (err) 1344 goto sync_out; 1345 blkaddr = dn.data_blkaddr; 1346 set_inode_flag(inode, FI_APPEND_WRITE); 1347 } 1348 } else { 1349 if (create) { 1350 if (unlikely(f2fs_cp_error(sbi))) { 1351 err = -EIO; 1352 goto sync_out; 1353 } 1354 if (flag == F2FS_GET_BLOCK_PRE_AIO) { 1355 if (blkaddr == NULL_ADDR) { 1356 prealloc++; 1357 last_ofs_in_node = dn.ofs_in_node; 1358 } 1359 } else { 1360 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO && 1361 flag != F2FS_GET_BLOCK_DIO); 1362 err = __allocate_data_block(&dn, 1363 map->m_seg_type); 1364 if (!err) 1365 set_inode_flag(inode, FI_APPEND_WRITE); 1366 } 1367 if (err) 1368 goto sync_out; 1369 map->m_flags |= F2FS_MAP_NEW; 1370 blkaddr = dn.data_blkaddr; 1371 } else { 1372 if (flag == F2FS_GET_BLOCK_BMAP) { 1373 map->m_pblk = 0; 1374 goto sync_out; 1375 } 1376 if (flag == F2FS_GET_BLOCK_PRECACHE) 1377 goto sync_out; 1378 if (flag == F2FS_GET_BLOCK_FIEMAP && 1379 blkaddr == NULL_ADDR) { 1380 if (map->m_next_pgofs) 1381 *map->m_next_pgofs = pgofs + 1; 1382 goto sync_out; 1383 } 1384 if (flag != F2FS_GET_BLOCK_FIEMAP) { 1385 /* for defragment case */ 1386 if (map->m_next_pgofs) 1387 *map->m_next_pgofs = pgofs + 1; 1388 goto sync_out; 1389 } 1390 } 1391 } 1392 1393 if (flag == F2FS_GET_BLOCK_PRE_AIO) 1394 goto skip; 1395 1396 if (map->m_len == 0) { 1397 /* preallocated unwritten block should be mapped for fiemap. */ 1398 if (blkaddr == NEW_ADDR) 1399 map->m_flags |= F2FS_MAP_UNWRITTEN; 1400 map->m_flags |= F2FS_MAP_MAPPED; 1401 1402 map->m_pblk = blkaddr; 1403 map->m_len = 1; 1404 } else if ((map->m_pblk != NEW_ADDR && 1405 blkaddr == (map->m_pblk + ofs)) || 1406 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) || 1407 flag == F2FS_GET_BLOCK_PRE_DIO) { 1408 ofs++; 1409 map->m_len++; 1410 } else { 1411 goto sync_out; 1412 } 1413 1414 skip: 1415 dn.ofs_in_node++; 1416 pgofs++; 1417 1418 /* preallocate blocks in batch for one dnode page */ 1419 if (flag == F2FS_GET_BLOCK_PRE_AIO && 1420 (pgofs == end || dn.ofs_in_node == end_offset)) { 1421 1422 dn.ofs_in_node = ofs_in_node; 1423 err = f2fs_reserve_new_blocks(&dn, prealloc); 1424 if (err) 1425 goto sync_out; 1426 1427 map->m_len += dn.ofs_in_node - ofs_in_node; 1428 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) { 1429 err = -ENOSPC; 1430 goto sync_out; 1431 } 1432 dn.ofs_in_node = end_offset; 1433 } 1434 1435 if (pgofs >= end) 1436 goto sync_out; 1437 else if (dn.ofs_in_node < end_offset) 1438 goto next_block; 1439 1440 if (flag == F2FS_GET_BLOCK_PRECACHE) { 1441 if (map->m_flags & F2FS_MAP_MAPPED) { 1442 unsigned int ofs = start_pgofs - map->m_lblk; 1443 1444 f2fs_update_extent_cache_range(&dn, 1445 start_pgofs, map->m_pblk + ofs, 1446 map->m_len - ofs); 1447 } 1448 } 1449 1450 f2fs_put_dnode(&dn); 1451 1452 if (map->m_may_create) { 1453 __do_map_lock(sbi, flag, false); 1454 f2fs_balance_fs(sbi, dn.node_changed); 1455 } 1456 goto next_dnode; 1457 1458 sync_out: 1459 1460 /* for hardware encryption, but to avoid potential issue in future */ 1461 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) 1462 f2fs_wait_on_block_writeback_range(inode, 1463 map->m_pblk, map->m_len); 1464 1465 if (flag == F2FS_GET_BLOCK_PRECACHE) { 1466 if (map->m_flags & F2FS_MAP_MAPPED) { 1467 unsigned int ofs = start_pgofs - map->m_lblk; 1468 1469 f2fs_update_extent_cache_range(&dn, 1470 start_pgofs, map->m_pblk + ofs, 1471 map->m_len - ofs); 1472 } 1473 if (map->m_next_extent) 1474 *map->m_next_extent = pgofs + 1; 1475 } 1476 f2fs_put_dnode(&dn); 1477 unlock_out: 1478 if (map->m_may_create) { 1479 __do_map_lock(sbi, flag, false); 1480 f2fs_balance_fs(sbi, dn.node_changed); 1481 } 1482 out: 1483 trace_f2fs_map_blocks(inode, map, err); 1484 return err; 1485 } 1486 1487 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len) 1488 { 1489 struct f2fs_map_blocks map; 1490 block_t last_lblk; 1491 int err; 1492 1493 if (pos + len > i_size_read(inode)) 1494 return false; 1495 1496 map.m_lblk = F2FS_BYTES_TO_BLK(pos); 1497 map.m_next_pgofs = NULL; 1498 map.m_next_extent = NULL; 1499 map.m_seg_type = NO_CHECK_TYPE; 1500 map.m_may_create = false; 1501 last_lblk = F2FS_BLK_ALIGN(pos + len); 1502 1503 while (map.m_lblk < last_lblk) { 1504 map.m_len = last_lblk - map.m_lblk; 1505 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); 1506 if (err || map.m_len == 0) 1507 return false; 1508 map.m_lblk += map.m_len; 1509 } 1510 return true; 1511 } 1512 1513 static int __get_data_block(struct inode *inode, sector_t iblock, 1514 struct buffer_head *bh, int create, int flag, 1515 pgoff_t *next_pgofs, int seg_type, bool may_write) 1516 { 1517 struct f2fs_map_blocks map; 1518 int err; 1519 1520 map.m_lblk = iblock; 1521 map.m_len = bh->b_size >> inode->i_blkbits; 1522 map.m_next_pgofs = next_pgofs; 1523 map.m_next_extent = NULL; 1524 map.m_seg_type = seg_type; 1525 map.m_may_create = may_write; 1526 1527 err = f2fs_map_blocks(inode, &map, create, flag); 1528 if (!err) { 1529 map_bh(bh, inode->i_sb, map.m_pblk); 1530 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags; 1531 bh->b_size = (u64)map.m_len << inode->i_blkbits; 1532 } 1533 return err; 1534 } 1535 1536 static int get_data_block(struct inode *inode, sector_t iblock, 1537 struct buffer_head *bh_result, int create, int flag, 1538 pgoff_t *next_pgofs) 1539 { 1540 return __get_data_block(inode, iblock, bh_result, create, 1541 flag, next_pgofs, 1542 NO_CHECK_TYPE, create); 1543 } 1544 1545 static int get_data_block_dio_write(struct inode *inode, sector_t iblock, 1546 struct buffer_head *bh_result, int create) 1547 { 1548 return __get_data_block(inode, iblock, bh_result, create, 1549 F2FS_GET_BLOCK_DIO, NULL, 1550 f2fs_rw_hint_to_seg_type(inode->i_write_hint), 1551 IS_SWAPFILE(inode) ? false : true); 1552 } 1553 1554 static int get_data_block_dio(struct inode *inode, sector_t iblock, 1555 struct buffer_head *bh_result, int create) 1556 { 1557 return __get_data_block(inode, iblock, bh_result, create, 1558 F2FS_GET_BLOCK_DIO, NULL, 1559 f2fs_rw_hint_to_seg_type(inode->i_write_hint), 1560 false); 1561 } 1562 1563 static int get_data_block_bmap(struct inode *inode, sector_t iblock, 1564 struct buffer_head *bh_result, int create) 1565 { 1566 /* Block number less than F2FS MAX BLOCKS */ 1567 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks)) 1568 return -EFBIG; 1569 1570 return __get_data_block(inode, iblock, bh_result, create, 1571 F2FS_GET_BLOCK_BMAP, NULL, 1572 NO_CHECK_TYPE, create); 1573 } 1574 1575 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset) 1576 { 1577 return (offset >> inode->i_blkbits); 1578 } 1579 1580 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk) 1581 { 1582 return (blk << inode->i_blkbits); 1583 } 1584 1585 static int f2fs_xattr_fiemap(struct inode *inode, 1586 struct fiemap_extent_info *fieinfo) 1587 { 1588 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1589 struct page *page; 1590 struct node_info ni; 1591 __u64 phys = 0, len; 1592 __u32 flags; 1593 nid_t xnid = F2FS_I(inode)->i_xattr_nid; 1594 int err = 0; 1595 1596 if (f2fs_has_inline_xattr(inode)) { 1597 int offset; 1598 1599 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), 1600 inode->i_ino, false); 1601 if (!page) 1602 return -ENOMEM; 1603 1604 err = f2fs_get_node_info(sbi, inode->i_ino, &ni); 1605 if (err) { 1606 f2fs_put_page(page, 1); 1607 return err; 1608 } 1609 1610 phys = (__u64)blk_to_logical(inode, ni.blk_addr); 1611 offset = offsetof(struct f2fs_inode, i_addr) + 1612 sizeof(__le32) * (DEF_ADDRS_PER_INODE - 1613 get_inline_xattr_addrs(inode)); 1614 1615 phys += offset; 1616 len = inline_xattr_size(inode); 1617 1618 f2fs_put_page(page, 1); 1619 1620 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED; 1621 1622 if (!xnid) 1623 flags |= FIEMAP_EXTENT_LAST; 1624 1625 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); 1626 if (err || err == 1) 1627 return err; 1628 } 1629 1630 if (xnid) { 1631 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false); 1632 if (!page) 1633 return -ENOMEM; 1634 1635 err = f2fs_get_node_info(sbi, xnid, &ni); 1636 if (err) { 1637 f2fs_put_page(page, 1); 1638 return err; 1639 } 1640 1641 phys = (__u64)blk_to_logical(inode, ni.blk_addr); 1642 len = inode->i_sb->s_blocksize; 1643 1644 f2fs_put_page(page, 1); 1645 1646 flags = FIEMAP_EXTENT_LAST; 1647 } 1648 1649 if (phys) 1650 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); 1651 1652 return (err < 0 ? err : 0); 1653 } 1654 1655 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 1656 u64 start, u64 len) 1657 { 1658 struct buffer_head map_bh; 1659 sector_t start_blk, last_blk; 1660 pgoff_t next_pgofs; 1661 u64 logical = 0, phys = 0, size = 0; 1662 u32 flags = 0; 1663 int ret = 0; 1664 1665 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { 1666 ret = f2fs_precache_extents(inode); 1667 if (ret) 1668 return ret; 1669 } 1670 1671 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR); 1672 if (ret) 1673 return ret; 1674 1675 inode_lock(inode); 1676 1677 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 1678 ret = f2fs_xattr_fiemap(inode, fieinfo); 1679 goto out; 1680 } 1681 1682 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { 1683 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len); 1684 if (ret != -EAGAIN) 1685 goto out; 1686 } 1687 1688 if (logical_to_blk(inode, len) == 0) 1689 len = blk_to_logical(inode, 1); 1690 1691 start_blk = logical_to_blk(inode, start); 1692 last_blk = logical_to_blk(inode, start + len - 1); 1693 1694 next: 1695 memset(&map_bh, 0, sizeof(struct buffer_head)); 1696 map_bh.b_size = len; 1697 1698 ret = get_data_block(inode, start_blk, &map_bh, 0, 1699 F2FS_GET_BLOCK_FIEMAP, &next_pgofs); 1700 if (ret) 1701 goto out; 1702 1703 /* HOLE */ 1704 if (!buffer_mapped(&map_bh)) { 1705 start_blk = next_pgofs; 1706 1707 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode, 1708 F2FS_I_SB(inode)->max_file_blocks)) 1709 goto prep_next; 1710 1711 flags |= FIEMAP_EXTENT_LAST; 1712 } 1713 1714 if (size) { 1715 if (IS_ENCRYPTED(inode)) 1716 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; 1717 1718 ret = fiemap_fill_next_extent(fieinfo, logical, 1719 phys, size, flags); 1720 } 1721 1722 if (start_blk > last_blk || ret) 1723 goto out; 1724 1725 logical = blk_to_logical(inode, start_blk); 1726 phys = blk_to_logical(inode, map_bh.b_blocknr); 1727 size = map_bh.b_size; 1728 flags = 0; 1729 if (buffer_unwritten(&map_bh)) 1730 flags = FIEMAP_EXTENT_UNWRITTEN; 1731 1732 start_blk += logical_to_blk(inode, size); 1733 1734 prep_next: 1735 cond_resched(); 1736 if (fatal_signal_pending(current)) 1737 ret = -EINTR; 1738 else 1739 goto next; 1740 out: 1741 if (ret == 1) 1742 ret = 0; 1743 1744 inode_unlock(inode); 1745 return ret; 1746 } 1747 1748 static inline loff_t f2fs_readpage_limit(struct inode *inode) 1749 { 1750 if (IS_ENABLED(CONFIG_FS_VERITY) && 1751 (IS_VERITY(inode) || f2fs_verity_in_progress(inode))) 1752 return inode->i_sb->s_maxbytes; 1753 1754 return i_size_read(inode); 1755 } 1756 1757 static int f2fs_read_single_page(struct inode *inode, struct page *page, 1758 unsigned nr_pages, 1759 struct f2fs_map_blocks *map, 1760 struct bio **bio_ret, 1761 sector_t *last_block_in_bio, 1762 bool is_readahead) 1763 { 1764 struct bio *bio = *bio_ret; 1765 const unsigned blkbits = inode->i_blkbits; 1766 const unsigned blocksize = 1 << blkbits; 1767 sector_t block_in_file; 1768 sector_t last_block; 1769 sector_t last_block_in_file; 1770 sector_t block_nr; 1771 int ret = 0; 1772 1773 block_in_file = (sector_t)page_index(page); 1774 last_block = block_in_file + nr_pages; 1775 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >> 1776 blkbits; 1777 if (last_block > last_block_in_file) 1778 last_block = last_block_in_file; 1779 1780 /* just zeroing out page which is beyond EOF */ 1781 if (block_in_file >= last_block) 1782 goto zero_out; 1783 /* 1784 * Map blocks using the previous result first. 1785 */ 1786 if ((map->m_flags & F2FS_MAP_MAPPED) && 1787 block_in_file > map->m_lblk && 1788 block_in_file < (map->m_lblk + map->m_len)) 1789 goto got_it; 1790 1791 /* 1792 * Then do more f2fs_map_blocks() calls until we are 1793 * done with this page. 1794 */ 1795 map->m_lblk = block_in_file; 1796 map->m_len = last_block - block_in_file; 1797 1798 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT); 1799 if (ret) 1800 goto out; 1801 got_it: 1802 if ((map->m_flags & F2FS_MAP_MAPPED)) { 1803 block_nr = map->m_pblk + block_in_file - map->m_lblk; 1804 SetPageMappedToDisk(page); 1805 1806 if (!PageUptodate(page) && (!PageSwapCache(page) && 1807 !cleancache_get_page(page))) { 1808 SetPageUptodate(page); 1809 goto confused; 1810 } 1811 1812 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr, 1813 DATA_GENERIC_ENHANCE_READ)) { 1814 ret = -EFSCORRUPTED; 1815 goto out; 1816 } 1817 } else { 1818 zero_out: 1819 zero_user_segment(page, 0, PAGE_SIZE); 1820 if (f2fs_need_verity(inode, page->index) && 1821 !fsverity_verify_page(page)) { 1822 ret = -EIO; 1823 goto out; 1824 } 1825 if (!PageUptodate(page)) 1826 SetPageUptodate(page); 1827 unlock_page(page); 1828 goto out; 1829 } 1830 1831 /* 1832 * This page will go to BIO. Do we need to send this 1833 * BIO off first? 1834 */ 1835 if (bio && !page_is_mergeable(F2FS_I_SB(inode), bio, 1836 *last_block_in_bio, block_nr)) { 1837 submit_and_realloc: 1838 __submit_bio(F2FS_I_SB(inode), bio, DATA); 1839 bio = NULL; 1840 } 1841 if (bio == NULL) { 1842 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages, 1843 is_readahead ? REQ_RAHEAD : 0, page->index); 1844 if (IS_ERR(bio)) { 1845 ret = PTR_ERR(bio); 1846 bio = NULL; 1847 goto out; 1848 } 1849 } 1850 1851 /* 1852 * If the page is under writeback, we need to wait for 1853 * its completion to see the correct decrypted data. 1854 */ 1855 f2fs_wait_on_block_writeback(inode, block_nr); 1856 1857 if (bio_add_page(bio, page, blocksize, 0) < blocksize) 1858 goto submit_and_realloc; 1859 1860 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA); 1861 ClearPageError(page); 1862 *last_block_in_bio = block_nr; 1863 goto out; 1864 confused: 1865 if (bio) { 1866 __submit_bio(F2FS_I_SB(inode), bio, DATA); 1867 bio = NULL; 1868 } 1869 unlock_page(page); 1870 out: 1871 *bio_ret = bio; 1872 return ret; 1873 } 1874 1875 /* 1876 * This function was originally taken from fs/mpage.c, and customized for f2fs. 1877 * Major change was from block_size == page_size in f2fs by default. 1878 * 1879 * Note that the aops->readpages() function is ONLY used for read-ahead. If 1880 * this function ever deviates from doing just read-ahead, it should either 1881 * use ->readpage() or do the necessary surgery to decouple ->readpages() 1882 * from read-ahead. 1883 */ 1884 static int f2fs_mpage_readpages(struct address_space *mapping, 1885 struct list_head *pages, struct page *page, 1886 unsigned nr_pages, bool is_readahead) 1887 { 1888 struct bio *bio = NULL; 1889 sector_t last_block_in_bio = 0; 1890 struct inode *inode = mapping->host; 1891 struct f2fs_map_blocks map; 1892 int ret = 0; 1893 1894 map.m_pblk = 0; 1895 map.m_lblk = 0; 1896 map.m_len = 0; 1897 map.m_flags = 0; 1898 map.m_next_pgofs = NULL; 1899 map.m_next_extent = NULL; 1900 map.m_seg_type = NO_CHECK_TYPE; 1901 map.m_may_create = false; 1902 1903 for (; nr_pages; nr_pages--) { 1904 if (pages) { 1905 page = list_last_entry(pages, struct page, lru); 1906 1907 prefetchw(&page->flags); 1908 list_del(&page->lru); 1909 if (add_to_page_cache_lru(page, mapping, 1910 page_index(page), 1911 readahead_gfp_mask(mapping))) 1912 goto next_page; 1913 } 1914 1915 ret = f2fs_read_single_page(inode, page, nr_pages, &map, &bio, 1916 &last_block_in_bio, is_readahead); 1917 if (ret) { 1918 SetPageError(page); 1919 zero_user_segment(page, 0, PAGE_SIZE); 1920 unlock_page(page); 1921 } 1922 next_page: 1923 if (pages) 1924 put_page(page); 1925 } 1926 BUG_ON(pages && !list_empty(pages)); 1927 if (bio) 1928 __submit_bio(F2FS_I_SB(inode), bio, DATA); 1929 return pages ? 0 : ret; 1930 } 1931 1932 static int f2fs_read_data_page(struct file *file, struct page *page) 1933 { 1934 struct inode *inode = page_file_mapping(page)->host; 1935 int ret = -EAGAIN; 1936 1937 trace_f2fs_readpage(page, DATA); 1938 1939 /* If the file has inline data, try to read it directly */ 1940 if (f2fs_has_inline_data(inode)) 1941 ret = f2fs_read_inline_data(inode, page); 1942 if (ret == -EAGAIN) 1943 ret = f2fs_mpage_readpages(page_file_mapping(page), 1944 NULL, page, 1, false); 1945 return ret; 1946 } 1947 1948 static int f2fs_read_data_pages(struct file *file, 1949 struct address_space *mapping, 1950 struct list_head *pages, unsigned nr_pages) 1951 { 1952 struct inode *inode = mapping->host; 1953 struct page *page = list_last_entry(pages, struct page, lru); 1954 1955 trace_f2fs_readpages(inode, page, nr_pages); 1956 1957 /* If the file has inline data, skip readpages */ 1958 if (f2fs_has_inline_data(inode)) 1959 return 0; 1960 1961 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true); 1962 } 1963 1964 static int encrypt_one_page(struct f2fs_io_info *fio) 1965 { 1966 struct inode *inode = fio->page->mapping->host; 1967 struct page *mpage; 1968 gfp_t gfp_flags = GFP_NOFS; 1969 1970 if (!f2fs_encrypted_file(inode)) 1971 return 0; 1972 1973 /* wait for GCed page writeback via META_MAPPING */ 1974 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr); 1975 1976 retry_encrypt: 1977 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(fio->page, 1978 PAGE_SIZE, 0, 1979 gfp_flags); 1980 if (IS_ERR(fio->encrypted_page)) { 1981 /* flush pending IOs and wait for a while in the ENOMEM case */ 1982 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) { 1983 f2fs_flush_merged_writes(fio->sbi); 1984 congestion_wait(BLK_RW_ASYNC, HZ/50); 1985 gfp_flags |= __GFP_NOFAIL; 1986 goto retry_encrypt; 1987 } 1988 return PTR_ERR(fio->encrypted_page); 1989 } 1990 1991 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr); 1992 if (mpage) { 1993 if (PageUptodate(mpage)) 1994 memcpy(page_address(mpage), 1995 page_address(fio->encrypted_page), PAGE_SIZE); 1996 f2fs_put_page(mpage, 1); 1997 } 1998 return 0; 1999 } 2000 2001 static inline bool check_inplace_update_policy(struct inode *inode, 2002 struct f2fs_io_info *fio) 2003 { 2004 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2005 unsigned int policy = SM_I(sbi)->ipu_policy; 2006 2007 if (policy & (0x1 << F2FS_IPU_FORCE)) 2008 return true; 2009 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi)) 2010 return true; 2011 if (policy & (0x1 << F2FS_IPU_UTIL) && 2012 utilization(sbi) > SM_I(sbi)->min_ipu_util) 2013 return true; 2014 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) && 2015 utilization(sbi) > SM_I(sbi)->min_ipu_util) 2016 return true; 2017 2018 /* 2019 * IPU for rewrite async pages 2020 */ 2021 if (policy & (0x1 << F2FS_IPU_ASYNC) && 2022 fio && fio->op == REQ_OP_WRITE && 2023 !(fio->op_flags & REQ_SYNC) && 2024 !IS_ENCRYPTED(inode)) 2025 return true; 2026 2027 /* this is only set during fdatasync */ 2028 if (policy & (0x1 << F2FS_IPU_FSYNC) && 2029 is_inode_flag_set(inode, FI_NEED_IPU)) 2030 return true; 2031 2032 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) && 2033 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) 2034 return true; 2035 2036 return false; 2037 } 2038 2039 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio) 2040 { 2041 if (f2fs_is_pinned_file(inode)) 2042 return true; 2043 2044 /* if this is cold file, we should overwrite to avoid fragmentation */ 2045 if (file_is_cold(inode)) 2046 return true; 2047 2048 return check_inplace_update_policy(inode, fio); 2049 } 2050 2051 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio) 2052 { 2053 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2054 2055 if (test_opt(sbi, LFS)) 2056 return true; 2057 if (S_ISDIR(inode->i_mode)) 2058 return true; 2059 if (IS_NOQUOTA(inode)) 2060 return true; 2061 if (f2fs_is_atomic_file(inode)) 2062 return true; 2063 if (fio) { 2064 if (is_cold_data(fio->page)) 2065 return true; 2066 if (IS_ATOMIC_WRITTEN_PAGE(fio->page)) 2067 return true; 2068 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) && 2069 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) 2070 return true; 2071 } 2072 return false; 2073 } 2074 2075 static inline bool need_inplace_update(struct f2fs_io_info *fio) 2076 { 2077 struct inode *inode = fio->page->mapping->host; 2078 2079 if (f2fs_should_update_outplace(inode, fio)) 2080 return false; 2081 2082 return f2fs_should_update_inplace(inode, fio); 2083 } 2084 2085 int f2fs_do_write_data_page(struct f2fs_io_info *fio) 2086 { 2087 struct page *page = fio->page; 2088 struct inode *inode = page->mapping->host; 2089 struct dnode_of_data dn; 2090 struct extent_info ei = {0,0,0}; 2091 struct node_info ni; 2092 bool ipu_force = false; 2093 int err = 0; 2094 2095 set_new_dnode(&dn, inode, NULL, NULL, 0); 2096 if (need_inplace_update(fio) && 2097 f2fs_lookup_extent_cache(inode, page->index, &ei)) { 2098 fio->old_blkaddr = ei.blk + page->index - ei.fofs; 2099 2100 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, 2101 DATA_GENERIC_ENHANCE)) 2102 return -EFSCORRUPTED; 2103 2104 ipu_force = true; 2105 fio->need_lock = LOCK_DONE; 2106 goto got_it; 2107 } 2108 2109 /* Deadlock due to between page->lock and f2fs_lock_op */ 2110 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi)) 2111 return -EAGAIN; 2112 2113 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE); 2114 if (err) 2115 goto out; 2116 2117 fio->old_blkaddr = dn.data_blkaddr; 2118 2119 /* This page is already truncated */ 2120 if (fio->old_blkaddr == NULL_ADDR) { 2121 ClearPageUptodate(page); 2122 clear_cold_data(page); 2123 goto out_writepage; 2124 } 2125 got_it: 2126 if (__is_valid_data_blkaddr(fio->old_blkaddr) && 2127 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, 2128 DATA_GENERIC_ENHANCE)) { 2129 err = -EFSCORRUPTED; 2130 goto out_writepage; 2131 } 2132 /* 2133 * If current allocation needs SSR, 2134 * it had better in-place writes for updated data. 2135 */ 2136 if (ipu_force || 2137 (__is_valid_data_blkaddr(fio->old_blkaddr) && 2138 need_inplace_update(fio))) { 2139 err = encrypt_one_page(fio); 2140 if (err) 2141 goto out_writepage; 2142 2143 set_page_writeback(page); 2144 ClearPageError(page); 2145 f2fs_put_dnode(&dn); 2146 if (fio->need_lock == LOCK_REQ) 2147 f2fs_unlock_op(fio->sbi); 2148 err = f2fs_inplace_write_data(fio); 2149 if (err) { 2150 if (f2fs_encrypted_file(inode)) 2151 fscrypt_finalize_bounce_page(&fio->encrypted_page); 2152 if (PageWriteback(page)) 2153 end_page_writeback(page); 2154 } else { 2155 set_inode_flag(inode, FI_UPDATE_WRITE); 2156 } 2157 trace_f2fs_do_write_data_page(fio->page, IPU); 2158 return err; 2159 } 2160 2161 if (fio->need_lock == LOCK_RETRY) { 2162 if (!f2fs_trylock_op(fio->sbi)) { 2163 err = -EAGAIN; 2164 goto out_writepage; 2165 } 2166 fio->need_lock = LOCK_REQ; 2167 } 2168 2169 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni); 2170 if (err) 2171 goto out_writepage; 2172 2173 fio->version = ni.version; 2174 2175 err = encrypt_one_page(fio); 2176 if (err) 2177 goto out_writepage; 2178 2179 set_page_writeback(page); 2180 ClearPageError(page); 2181 2182 /* LFS mode write path */ 2183 f2fs_outplace_write_data(&dn, fio); 2184 trace_f2fs_do_write_data_page(page, OPU); 2185 set_inode_flag(inode, FI_APPEND_WRITE); 2186 if (page->index == 0) 2187 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); 2188 out_writepage: 2189 f2fs_put_dnode(&dn); 2190 out: 2191 if (fio->need_lock == LOCK_REQ) 2192 f2fs_unlock_op(fio->sbi); 2193 return err; 2194 } 2195 2196 static int __write_data_page(struct page *page, bool *submitted, 2197 struct bio **bio, 2198 sector_t *last_block, 2199 struct writeback_control *wbc, 2200 enum iostat_type io_type) 2201 { 2202 struct inode *inode = page->mapping->host; 2203 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2204 loff_t i_size = i_size_read(inode); 2205 const pgoff_t end_index = ((unsigned long long) i_size) 2206 >> PAGE_SHIFT; 2207 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT; 2208 unsigned offset = 0; 2209 bool need_balance_fs = false; 2210 int err = 0; 2211 struct f2fs_io_info fio = { 2212 .sbi = sbi, 2213 .ino = inode->i_ino, 2214 .type = DATA, 2215 .op = REQ_OP_WRITE, 2216 .op_flags = wbc_to_write_flags(wbc), 2217 .old_blkaddr = NULL_ADDR, 2218 .page = page, 2219 .encrypted_page = NULL, 2220 .submitted = false, 2221 .need_lock = LOCK_RETRY, 2222 .io_type = io_type, 2223 .io_wbc = wbc, 2224 .bio = bio, 2225 .last_block = last_block, 2226 }; 2227 2228 trace_f2fs_writepage(page, DATA); 2229 2230 /* we should bypass data pages to proceed the kworkder jobs */ 2231 if (unlikely(f2fs_cp_error(sbi))) { 2232 mapping_set_error(page->mapping, -EIO); 2233 /* 2234 * don't drop any dirty dentry pages for keeping lastest 2235 * directory structure. 2236 */ 2237 if (S_ISDIR(inode->i_mode)) 2238 goto redirty_out; 2239 goto out; 2240 } 2241 2242 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 2243 goto redirty_out; 2244 2245 if (page->index < end_index || f2fs_verity_in_progress(inode)) 2246 goto write; 2247 2248 /* 2249 * If the offset is out-of-range of file size, 2250 * this page does not have to be written to disk. 2251 */ 2252 offset = i_size & (PAGE_SIZE - 1); 2253 if ((page->index >= end_index + 1) || !offset) 2254 goto out; 2255 2256 zero_user_segment(page, offset, PAGE_SIZE); 2257 write: 2258 if (f2fs_is_drop_cache(inode)) 2259 goto out; 2260 /* we should not write 0'th page having journal header */ 2261 if (f2fs_is_volatile_file(inode) && (!page->index || 2262 (!wbc->for_reclaim && 2263 f2fs_available_free_memory(sbi, BASE_CHECK)))) 2264 goto redirty_out; 2265 2266 /* Dentry blocks are controlled by checkpoint */ 2267 if (S_ISDIR(inode->i_mode)) { 2268 fio.need_lock = LOCK_DONE; 2269 err = f2fs_do_write_data_page(&fio); 2270 goto done; 2271 } 2272 2273 if (!wbc->for_reclaim) 2274 need_balance_fs = true; 2275 else if (has_not_enough_free_secs(sbi, 0, 0)) 2276 goto redirty_out; 2277 else 2278 set_inode_flag(inode, FI_HOT_DATA); 2279 2280 err = -EAGAIN; 2281 if (f2fs_has_inline_data(inode)) { 2282 err = f2fs_write_inline_data(inode, page); 2283 if (!err) 2284 goto out; 2285 } 2286 2287 if (err == -EAGAIN) { 2288 err = f2fs_do_write_data_page(&fio); 2289 if (err == -EAGAIN) { 2290 fio.need_lock = LOCK_REQ; 2291 err = f2fs_do_write_data_page(&fio); 2292 } 2293 } 2294 2295 if (err) { 2296 file_set_keep_isize(inode); 2297 } else { 2298 down_write(&F2FS_I(inode)->i_sem); 2299 if (F2FS_I(inode)->last_disk_size < psize) 2300 F2FS_I(inode)->last_disk_size = psize; 2301 up_write(&F2FS_I(inode)->i_sem); 2302 } 2303 2304 done: 2305 if (err && err != -ENOENT) 2306 goto redirty_out; 2307 2308 out: 2309 inode_dec_dirty_pages(inode); 2310 if (err) { 2311 ClearPageUptodate(page); 2312 clear_cold_data(page); 2313 } 2314 2315 if (wbc->for_reclaim) { 2316 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA); 2317 clear_inode_flag(inode, FI_HOT_DATA); 2318 f2fs_remove_dirty_inode(inode); 2319 submitted = NULL; 2320 } 2321 2322 unlock_page(page); 2323 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) && 2324 !F2FS_I(inode)->cp_task) 2325 f2fs_balance_fs(sbi, need_balance_fs); 2326 2327 if (unlikely(f2fs_cp_error(sbi))) { 2328 f2fs_submit_merged_write(sbi, DATA); 2329 f2fs_submit_merged_ipu_write(sbi, bio, NULL); 2330 submitted = NULL; 2331 } 2332 2333 if (submitted) 2334 *submitted = fio.submitted; 2335 2336 return 0; 2337 2338 redirty_out: 2339 redirty_page_for_writepage(wbc, page); 2340 /* 2341 * pageout() in MM traslates EAGAIN, so calls handle_write_error() 2342 * -> mapping_set_error() -> set_bit(AS_EIO, ...). 2343 * file_write_and_wait_range() will see EIO error, which is critical 2344 * to return value of fsync() followed by atomic_write failure to user. 2345 */ 2346 if (!err || wbc->for_reclaim) 2347 return AOP_WRITEPAGE_ACTIVATE; 2348 unlock_page(page); 2349 return err; 2350 } 2351 2352 static int f2fs_write_data_page(struct page *page, 2353 struct writeback_control *wbc) 2354 { 2355 return __write_data_page(page, NULL, NULL, NULL, wbc, FS_DATA_IO); 2356 } 2357 2358 /* 2359 * This function was copied from write_cche_pages from mm/page-writeback.c. 2360 * The major change is making write step of cold data page separately from 2361 * warm/hot data page. 2362 */ 2363 static int f2fs_write_cache_pages(struct address_space *mapping, 2364 struct writeback_control *wbc, 2365 enum iostat_type io_type) 2366 { 2367 int ret = 0; 2368 int done = 0; 2369 struct pagevec pvec; 2370 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 2371 struct bio *bio = NULL; 2372 sector_t last_block; 2373 int nr_pages; 2374 pgoff_t uninitialized_var(writeback_index); 2375 pgoff_t index; 2376 pgoff_t end; /* Inclusive */ 2377 pgoff_t done_index; 2378 int cycled; 2379 int range_whole = 0; 2380 xa_mark_t tag; 2381 int nwritten = 0; 2382 2383 pagevec_init(&pvec); 2384 2385 if (get_dirty_pages(mapping->host) <= 2386 SM_I(F2FS_M_SB(mapping))->min_hot_blocks) 2387 set_inode_flag(mapping->host, FI_HOT_DATA); 2388 else 2389 clear_inode_flag(mapping->host, FI_HOT_DATA); 2390 2391 if (wbc->range_cyclic) { 2392 writeback_index = mapping->writeback_index; /* prev offset */ 2393 index = writeback_index; 2394 if (index == 0) 2395 cycled = 1; 2396 else 2397 cycled = 0; 2398 end = -1; 2399 } else { 2400 index = wbc->range_start >> PAGE_SHIFT; 2401 end = wbc->range_end >> PAGE_SHIFT; 2402 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 2403 range_whole = 1; 2404 cycled = 1; /* ignore range_cyclic tests */ 2405 } 2406 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 2407 tag = PAGECACHE_TAG_TOWRITE; 2408 else 2409 tag = PAGECACHE_TAG_DIRTY; 2410 retry: 2411 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 2412 tag_pages_for_writeback(mapping, index, end); 2413 done_index = index; 2414 while (!done && (index <= end)) { 2415 int i; 2416 2417 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, 2418 tag); 2419 if (nr_pages == 0) 2420 break; 2421 2422 for (i = 0; i < nr_pages; i++) { 2423 struct page *page = pvec.pages[i]; 2424 bool submitted = false; 2425 2426 /* give a priority to WB_SYNC threads */ 2427 if (atomic_read(&sbi->wb_sync_req[DATA]) && 2428 wbc->sync_mode == WB_SYNC_NONE) { 2429 done = 1; 2430 break; 2431 } 2432 2433 done_index = page->index; 2434 retry_write: 2435 lock_page(page); 2436 2437 if (unlikely(page->mapping != mapping)) { 2438 continue_unlock: 2439 unlock_page(page); 2440 continue; 2441 } 2442 2443 if (!PageDirty(page)) { 2444 /* someone wrote it for us */ 2445 goto continue_unlock; 2446 } 2447 2448 if (PageWriteback(page)) { 2449 if (wbc->sync_mode != WB_SYNC_NONE) 2450 f2fs_wait_on_page_writeback(page, 2451 DATA, true, true); 2452 else 2453 goto continue_unlock; 2454 } 2455 2456 if (!clear_page_dirty_for_io(page)) 2457 goto continue_unlock; 2458 2459 ret = __write_data_page(page, &submitted, &bio, 2460 &last_block, wbc, io_type); 2461 if (unlikely(ret)) { 2462 /* 2463 * keep nr_to_write, since vfs uses this to 2464 * get # of written pages. 2465 */ 2466 if (ret == AOP_WRITEPAGE_ACTIVATE) { 2467 unlock_page(page); 2468 ret = 0; 2469 continue; 2470 } else if (ret == -EAGAIN) { 2471 ret = 0; 2472 if (wbc->sync_mode == WB_SYNC_ALL) { 2473 cond_resched(); 2474 congestion_wait(BLK_RW_ASYNC, 2475 HZ/50); 2476 goto retry_write; 2477 } 2478 continue; 2479 } 2480 done_index = page->index + 1; 2481 done = 1; 2482 break; 2483 } else if (submitted) { 2484 nwritten++; 2485 } 2486 2487 if (--wbc->nr_to_write <= 0 && 2488 wbc->sync_mode == WB_SYNC_NONE) { 2489 done = 1; 2490 break; 2491 } 2492 } 2493 pagevec_release(&pvec); 2494 cond_resched(); 2495 } 2496 2497 if (!cycled && !done) { 2498 cycled = 1; 2499 index = 0; 2500 end = writeback_index - 1; 2501 goto retry; 2502 } 2503 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 2504 mapping->writeback_index = done_index; 2505 2506 if (nwritten) 2507 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host, 2508 NULL, 0, DATA); 2509 /* submit cached bio of IPU write */ 2510 if (bio) 2511 f2fs_submit_merged_ipu_write(sbi, &bio, NULL); 2512 2513 return ret; 2514 } 2515 2516 static inline bool __should_serialize_io(struct inode *inode, 2517 struct writeback_control *wbc) 2518 { 2519 if (!S_ISREG(inode->i_mode)) 2520 return false; 2521 if (IS_NOQUOTA(inode)) 2522 return false; 2523 /* to avoid deadlock in path of data flush */ 2524 if (F2FS_I(inode)->cp_task) 2525 return false; 2526 if (wbc->sync_mode != WB_SYNC_ALL) 2527 return true; 2528 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks) 2529 return true; 2530 return false; 2531 } 2532 2533 static int __f2fs_write_data_pages(struct address_space *mapping, 2534 struct writeback_control *wbc, 2535 enum iostat_type io_type) 2536 { 2537 struct inode *inode = mapping->host; 2538 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2539 struct blk_plug plug; 2540 int ret; 2541 bool locked = false; 2542 2543 /* deal with chardevs and other special file */ 2544 if (!mapping->a_ops->writepage) 2545 return 0; 2546 2547 /* skip writing if there is no dirty page in this inode */ 2548 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) 2549 return 0; 2550 2551 /* during POR, we don't need to trigger writepage at all. */ 2552 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 2553 goto skip_write; 2554 2555 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) && 2556 wbc->sync_mode == WB_SYNC_NONE && 2557 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && 2558 f2fs_available_free_memory(sbi, DIRTY_DENTS)) 2559 goto skip_write; 2560 2561 /* skip writing during file defragment */ 2562 if (is_inode_flag_set(inode, FI_DO_DEFRAG)) 2563 goto skip_write; 2564 2565 trace_f2fs_writepages(mapping->host, wbc, DATA); 2566 2567 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */ 2568 if (wbc->sync_mode == WB_SYNC_ALL) 2569 atomic_inc(&sbi->wb_sync_req[DATA]); 2570 else if (atomic_read(&sbi->wb_sync_req[DATA])) 2571 goto skip_write; 2572 2573 if (__should_serialize_io(inode, wbc)) { 2574 mutex_lock(&sbi->writepages); 2575 locked = true; 2576 } 2577 2578 blk_start_plug(&plug); 2579 ret = f2fs_write_cache_pages(mapping, wbc, io_type); 2580 blk_finish_plug(&plug); 2581 2582 if (locked) 2583 mutex_unlock(&sbi->writepages); 2584 2585 if (wbc->sync_mode == WB_SYNC_ALL) 2586 atomic_dec(&sbi->wb_sync_req[DATA]); 2587 /* 2588 * if some pages were truncated, we cannot guarantee its mapping->host 2589 * to detect pending bios. 2590 */ 2591 2592 f2fs_remove_dirty_inode(inode); 2593 return ret; 2594 2595 skip_write: 2596 wbc->pages_skipped += get_dirty_pages(inode); 2597 trace_f2fs_writepages(mapping->host, wbc, DATA); 2598 return 0; 2599 } 2600 2601 static int f2fs_write_data_pages(struct address_space *mapping, 2602 struct writeback_control *wbc) 2603 { 2604 struct inode *inode = mapping->host; 2605 2606 return __f2fs_write_data_pages(mapping, wbc, 2607 F2FS_I(inode)->cp_task == current ? 2608 FS_CP_DATA_IO : FS_DATA_IO); 2609 } 2610 2611 static void f2fs_write_failed(struct address_space *mapping, loff_t to) 2612 { 2613 struct inode *inode = mapping->host; 2614 loff_t i_size = i_size_read(inode); 2615 2616 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */ 2617 if (to > i_size && !f2fs_verity_in_progress(inode)) { 2618 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 2619 down_write(&F2FS_I(inode)->i_mmap_sem); 2620 2621 truncate_pagecache(inode, i_size); 2622 if (!IS_NOQUOTA(inode)) 2623 f2fs_truncate_blocks(inode, i_size, true); 2624 2625 up_write(&F2FS_I(inode)->i_mmap_sem); 2626 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 2627 } 2628 } 2629 2630 static int prepare_write_begin(struct f2fs_sb_info *sbi, 2631 struct page *page, loff_t pos, unsigned len, 2632 block_t *blk_addr, bool *node_changed) 2633 { 2634 struct inode *inode = page->mapping->host; 2635 pgoff_t index = page->index; 2636 struct dnode_of_data dn; 2637 struct page *ipage; 2638 bool locked = false; 2639 struct extent_info ei = {0,0,0}; 2640 int err = 0; 2641 int flag; 2642 2643 /* 2644 * we already allocated all the blocks, so we don't need to get 2645 * the block addresses when there is no need to fill the page. 2646 */ 2647 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE && 2648 !is_inode_flag_set(inode, FI_NO_PREALLOC) && 2649 !f2fs_verity_in_progress(inode)) 2650 return 0; 2651 2652 /* f2fs_lock_op avoids race between write CP and convert_inline_page */ 2653 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode)) 2654 flag = F2FS_GET_BLOCK_DEFAULT; 2655 else 2656 flag = F2FS_GET_BLOCK_PRE_AIO; 2657 2658 if (f2fs_has_inline_data(inode) || 2659 (pos & PAGE_MASK) >= i_size_read(inode)) { 2660 __do_map_lock(sbi, flag, true); 2661 locked = true; 2662 } 2663 restart: 2664 /* check inline_data */ 2665 ipage = f2fs_get_node_page(sbi, inode->i_ino); 2666 if (IS_ERR(ipage)) { 2667 err = PTR_ERR(ipage); 2668 goto unlock_out; 2669 } 2670 2671 set_new_dnode(&dn, inode, ipage, ipage, 0); 2672 2673 if (f2fs_has_inline_data(inode)) { 2674 if (pos + len <= MAX_INLINE_DATA(inode)) { 2675 f2fs_do_read_inline_data(page, ipage); 2676 set_inode_flag(inode, FI_DATA_EXIST); 2677 if (inode->i_nlink) 2678 set_inline_node(ipage); 2679 } else { 2680 err = f2fs_convert_inline_page(&dn, page); 2681 if (err) 2682 goto out; 2683 if (dn.data_blkaddr == NULL_ADDR) 2684 err = f2fs_get_block(&dn, index); 2685 } 2686 } else if (locked) { 2687 err = f2fs_get_block(&dn, index); 2688 } else { 2689 if (f2fs_lookup_extent_cache(inode, index, &ei)) { 2690 dn.data_blkaddr = ei.blk + index - ei.fofs; 2691 } else { 2692 /* hole case */ 2693 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 2694 if (err || dn.data_blkaddr == NULL_ADDR) { 2695 f2fs_put_dnode(&dn); 2696 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, 2697 true); 2698 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO); 2699 locked = true; 2700 goto restart; 2701 } 2702 } 2703 } 2704 2705 /* convert_inline_page can make node_changed */ 2706 *blk_addr = dn.data_blkaddr; 2707 *node_changed = dn.node_changed; 2708 out: 2709 f2fs_put_dnode(&dn); 2710 unlock_out: 2711 if (locked) 2712 __do_map_lock(sbi, flag, false); 2713 return err; 2714 } 2715 2716 static int f2fs_write_begin(struct file *file, struct address_space *mapping, 2717 loff_t pos, unsigned len, unsigned flags, 2718 struct page **pagep, void **fsdata) 2719 { 2720 struct inode *inode = mapping->host; 2721 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2722 struct page *page = NULL; 2723 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT; 2724 bool need_balance = false, drop_atomic = false; 2725 block_t blkaddr = NULL_ADDR; 2726 int err = 0; 2727 2728 trace_f2fs_write_begin(inode, pos, len, flags); 2729 2730 if (!f2fs_is_checkpoint_ready(sbi)) { 2731 err = -ENOSPC; 2732 goto fail; 2733 } 2734 2735 if ((f2fs_is_atomic_file(inode) && 2736 !f2fs_available_free_memory(sbi, INMEM_PAGES)) || 2737 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) { 2738 err = -ENOMEM; 2739 drop_atomic = true; 2740 goto fail; 2741 } 2742 2743 /* 2744 * We should check this at this moment to avoid deadlock on inode page 2745 * and #0 page. The locking rule for inline_data conversion should be: 2746 * lock_page(page #0) -> lock_page(inode_page) 2747 */ 2748 if (index != 0) { 2749 err = f2fs_convert_inline_inode(inode); 2750 if (err) 2751 goto fail; 2752 } 2753 repeat: 2754 /* 2755 * Do not use grab_cache_page_write_begin() to avoid deadlock due to 2756 * wait_for_stable_page. Will wait that below with our IO control. 2757 */ 2758 page = f2fs_pagecache_get_page(mapping, index, 2759 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS); 2760 if (!page) { 2761 err = -ENOMEM; 2762 goto fail; 2763 } 2764 2765 *pagep = page; 2766 2767 err = prepare_write_begin(sbi, page, pos, len, 2768 &blkaddr, &need_balance); 2769 if (err) 2770 goto fail; 2771 2772 if (need_balance && !IS_NOQUOTA(inode) && 2773 has_not_enough_free_secs(sbi, 0, 0)) { 2774 unlock_page(page); 2775 f2fs_balance_fs(sbi, true); 2776 lock_page(page); 2777 if (page->mapping != mapping) { 2778 /* The page got truncated from under us */ 2779 f2fs_put_page(page, 1); 2780 goto repeat; 2781 } 2782 } 2783 2784 f2fs_wait_on_page_writeback(page, DATA, false, true); 2785 2786 if (len == PAGE_SIZE || PageUptodate(page)) 2787 return 0; 2788 2789 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) && 2790 !f2fs_verity_in_progress(inode)) { 2791 zero_user_segment(page, len, PAGE_SIZE); 2792 return 0; 2793 } 2794 2795 if (blkaddr == NEW_ADDR) { 2796 zero_user_segment(page, 0, PAGE_SIZE); 2797 SetPageUptodate(page); 2798 } else { 2799 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, 2800 DATA_GENERIC_ENHANCE_READ)) { 2801 err = -EFSCORRUPTED; 2802 goto fail; 2803 } 2804 err = f2fs_submit_page_read(inode, page, blkaddr); 2805 if (err) 2806 goto fail; 2807 2808 lock_page(page); 2809 if (unlikely(page->mapping != mapping)) { 2810 f2fs_put_page(page, 1); 2811 goto repeat; 2812 } 2813 if (unlikely(!PageUptodate(page))) { 2814 err = -EIO; 2815 goto fail; 2816 } 2817 } 2818 return 0; 2819 2820 fail: 2821 f2fs_put_page(page, 1); 2822 f2fs_write_failed(mapping, pos + len); 2823 if (drop_atomic) 2824 f2fs_drop_inmem_pages_all(sbi, false); 2825 return err; 2826 } 2827 2828 static int f2fs_write_end(struct file *file, 2829 struct address_space *mapping, 2830 loff_t pos, unsigned len, unsigned copied, 2831 struct page *page, void *fsdata) 2832 { 2833 struct inode *inode = page->mapping->host; 2834 2835 trace_f2fs_write_end(inode, pos, len, copied); 2836 2837 /* 2838 * This should be come from len == PAGE_SIZE, and we expect copied 2839 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and 2840 * let generic_perform_write() try to copy data again through copied=0. 2841 */ 2842 if (!PageUptodate(page)) { 2843 if (unlikely(copied != len)) 2844 copied = 0; 2845 else 2846 SetPageUptodate(page); 2847 } 2848 if (!copied) 2849 goto unlock_out; 2850 2851 set_page_dirty(page); 2852 2853 if (pos + copied > i_size_read(inode) && 2854 !f2fs_verity_in_progress(inode)) 2855 f2fs_i_size_write(inode, pos + copied); 2856 unlock_out: 2857 f2fs_put_page(page, 1); 2858 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 2859 return copied; 2860 } 2861 2862 static int check_direct_IO(struct inode *inode, struct iov_iter *iter, 2863 loff_t offset) 2864 { 2865 unsigned i_blkbits = READ_ONCE(inode->i_blkbits); 2866 unsigned blkbits = i_blkbits; 2867 unsigned blocksize_mask = (1 << blkbits) - 1; 2868 unsigned long align = offset | iov_iter_alignment(iter); 2869 struct block_device *bdev = inode->i_sb->s_bdev; 2870 2871 if (align & blocksize_mask) { 2872 if (bdev) 2873 blkbits = blksize_bits(bdev_logical_block_size(bdev)); 2874 blocksize_mask = (1 << blkbits) - 1; 2875 if (align & blocksize_mask) 2876 return -EINVAL; 2877 return 1; 2878 } 2879 return 0; 2880 } 2881 2882 static void f2fs_dio_end_io(struct bio *bio) 2883 { 2884 struct f2fs_private_dio *dio = bio->bi_private; 2885 2886 dec_page_count(F2FS_I_SB(dio->inode), 2887 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ); 2888 2889 bio->bi_private = dio->orig_private; 2890 bio->bi_end_io = dio->orig_end_io; 2891 2892 kvfree(dio); 2893 2894 bio_endio(bio); 2895 } 2896 2897 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode, 2898 loff_t file_offset) 2899 { 2900 struct f2fs_private_dio *dio; 2901 bool write = (bio_op(bio) == REQ_OP_WRITE); 2902 2903 dio = f2fs_kzalloc(F2FS_I_SB(inode), 2904 sizeof(struct f2fs_private_dio), GFP_NOFS); 2905 if (!dio) 2906 goto out; 2907 2908 dio->inode = inode; 2909 dio->orig_end_io = bio->bi_end_io; 2910 dio->orig_private = bio->bi_private; 2911 dio->write = write; 2912 2913 bio->bi_end_io = f2fs_dio_end_io; 2914 bio->bi_private = dio; 2915 2916 inc_page_count(F2FS_I_SB(inode), 2917 write ? F2FS_DIO_WRITE : F2FS_DIO_READ); 2918 2919 submit_bio(bio); 2920 return; 2921 out: 2922 bio->bi_status = BLK_STS_IOERR; 2923 bio_endio(bio); 2924 } 2925 2926 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 2927 { 2928 struct address_space *mapping = iocb->ki_filp->f_mapping; 2929 struct inode *inode = mapping->host; 2930 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2931 struct f2fs_inode_info *fi = F2FS_I(inode); 2932 size_t count = iov_iter_count(iter); 2933 loff_t offset = iocb->ki_pos; 2934 int rw = iov_iter_rw(iter); 2935 int err; 2936 enum rw_hint hint = iocb->ki_hint; 2937 int whint_mode = F2FS_OPTION(sbi).whint_mode; 2938 bool do_opu; 2939 2940 err = check_direct_IO(inode, iter, offset); 2941 if (err) 2942 return err < 0 ? err : 0; 2943 2944 if (f2fs_force_buffered_io(inode, iocb, iter)) 2945 return 0; 2946 2947 do_opu = allow_outplace_dio(inode, iocb, iter); 2948 2949 trace_f2fs_direct_IO_enter(inode, offset, count, rw); 2950 2951 if (rw == WRITE && whint_mode == WHINT_MODE_OFF) 2952 iocb->ki_hint = WRITE_LIFE_NOT_SET; 2953 2954 if (iocb->ki_flags & IOCB_NOWAIT) { 2955 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) { 2956 iocb->ki_hint = hint; 2957 err = -EAGAIN; 2958 goto out; 2959 } 2960 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) { 2961 up_read(&fi->i_gc_rwsem[rw]); 2962 iocb->ki_hint = hint; 2963 err = -EAGAIN; 2964 goto out; 2965 } 2966 } else { 2967 down_read(&fi->i_gc_rwsem[rw]); 2968 if (do_opu) 2969 down_read(&fi->i_gc_rwsem[READ]); 2970 } 2971 2972 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, 2973 iter, rw == WRITE ? get_data_block_dio_write : 2974 get_data_block_dio, NULL, f2fs_dio_submit_bio, 2975 DIO_LOCKING | DIO_SKIP_HOLES); 2976 2977 if (do_opu) 2978 up_read(&fi->i_gc_rwsem[READ]); 2979 2980 up_read(&fi->i_gc_rwsem[rw]); 2981 2982 if (rw == WRITE) { 2983 if (whint_mode == WHINT_MODE_OFF) 2984 iocb->ki_hint = hint; 2985 if (err > 0) { 2986 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO, 2987 err); 2988 if (!do_opu) 2989 set_inode_flag(inode, FI_UPDATE_WRITE); 2990 } else if (err < 0) { 2991 f2fs_write_failed(mapping, offset + count); 2992 } 2993 } 2994 2995 out: 2996 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err); 2997 2998 return err; 2999 } 3000 3001 void f2fs_invalidate_page(struct page *page, unsigned int offset, 3002 unsigned int length) 3003 { 3004 struct inode *inode = page->mapping->host; 3005 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3006 3007 if (inode->i_ino >= F2FS_ROOT_INO(sbi) && 3008 (offset % PAGE_SIZE || length != PAGE_SIZE)) 3009 return; 3010 3011 if (PageDirty(page)) { 3012 if (inode->i_ino == F2FS_META_INO(sbi)) { 3013 dec_page_count(sbi, F2FS_DIRTY_META); 3014 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) { 3015 dec_page_count(sbi, F2FS_DIRTY_NODES); 3016 } else { 3017 inode_dec_dirty_pages(inode); 3018 f2fs_remove_dirty_inode(inode); 3019 } 3020 } 3021 3022 clear_cold_data(page); 3023 3024 if (IS_ATOMIC_WRITTEN_PAGE(page)) 3025 return f2fs_drop_inmem_page(inode, page); 3026 3027 f2fs_clear_page_private(page); 3028 } 3029 3030 int f2fs_release_page(struct page *page, gfp_t wait) 3031 { 3032 /* If this is dirty page, keep PagePrivate */ 3033 if (PageDirty(page)) 3034 return 0; 3035 3036 /* This is atomic written page, keep Private */ 3037 if (IS_ATOMIC_WRITTEN_PAGE(page)) 3038 return 0; 3039 3040 clear_cold_data(page); 3041 f2fs_clear_page_private(page); 3042 return 1; 3043 } 3044 3045 static int f2fs_set_data_page_dirty(struct page *page) 3046 { 3047 struct inode *inode = page_file_mapping(page)->host; 3048 3049 trace_f2fs_set_page_dirty(page, DATA); 3050 3051 if (!PageUptodate(page)) 3052 SetPageUptodate(page); 3053 if (PageSwapCache(page)) 3054 return __set_page_dirty_nobuffers(page); 3055 3056 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) { 3057 if (!IS_ATOMIC_WRITTEN_PAGE(page)) { 3058 f2fs_register_inmem_page(inode, page); 3059 return 1; 3060 } 3061 /* 3062 * Previously, this page has been registered, we just 3063 * return here. 3064 */ 3065 return 0; 3066 } 3067 3068 if (!PageDirty(page)) { 3069 __set_page_dirty_nobuffers(page); 3070 f2fs_update_dirty_page(inode, page); 3071 return 1; 3072 } 3073 return 0; 3074 } 3075 3076 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) 3077 { 3078 struct inode *inode = mapping->host; 3079 3080 if (f2fs_has_inline_data(inode)) 3081 return 0; 3082 3083 /* make sure allocating whole blocks */ 3084 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 3085 filemap_write_and_wait(mapping); 3086 3087 return generic_block_bmap(mapping, block, get_data_block_bmap); 3088 } 3089 3090 #ifdef CONFIG_MIGRATION 3091 #include <linux/migrate.h> 3092 3093 int f2fs_migrate_page(struct address_space *mapping, 3094 struct page *newpage, struct page *page, enum migrate_mode mode) 3095 { 3096 int rc, extra_count; 3097 struct f2fs_inode_info *fi = F2FS_I(mapping->host); 3098 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page); 3099 3100 BUG_ON(PageWriteback(page)); 3101 3102 /* migrating an atomic written page is safe with the inmem_lock hold */ 3103 if (atomic_written) { 3104 if (mode != MIGRATE_SYNC) 3105 return -EBUSY; 3106 if (!mutex_trylock(&fi->inmem_lock)) 3107 return -EAGAIN; 3108 } 3109 3110 /* one extra reference was held for atomic_write page */ 3111 extra_count = atomic_written ? 1 : 0; 3112 rc = migrate_page_move_mapping(mapping, newpage, 3113 page, extra_count); 3114 if (rc != MIGRATEPAGE_SUCCESS) { 3115 if (atomic_written) 3116 mutex_unlock(&fi->inmem_lock); 3117 return rc; 3118 } 3119 3120 if (atomic_written) { 3121 struct inmem_pages *cur; 3122 list_for_each_entry(cur, &fi->inmem_pages, list) 3123 if (cur->page == page) { 3124 cur->page = newpage; 3125 break; 3126 } 3127 mutex_unlock(&fi->inmem_lock); 3128 put_page(page); 3129 get_page(newpage); 3130 } 3131 3132 if (PagePrivate(page)) { 3133 f2fs_set_page_private(newpage, page_private(page)); 3134 f2fs_clear_page_private(page); 3135 } 3136 3137 if (mode != MIGRATE_SYNC_NO_COPY) 3138 migrate_page_copy(newpage, page); 3139 else 3140 migrate_page_states(newpage, page); 3141 3142 return MIGRATEPAGE_SUCCESS; 3143 } 3144 #endif 3145 3146 #ifdef CONFIG_SWAP 3147 /* Copied from generic_swapfile_activate() to check any holes */ 3148 static int check_swap_activate(struct file *swap_file, unsigned int max) 3149 { 3150 struct address_space *mapping = swap_file->f_mapping; 3151 struct inode *inode = mapping->host; 3152 unsigned blocks_per_page; 3153 unsigned long page_no; 3154 unsigned blkbits; 3155 sector_t probe_block; 3156 sector_t last_block; 3157 sector_t lowest_block = -1; 3158 sector_t highest_block = 0; 3159 3160 blkbits = inode->i_blkbits; 3161 blocks_per_page = PAGE_SIZE >> blkbits; 3162 3163 /* 3164 * Map all the blocks into the extent list. This code doesn't try 3165 * to be very smart. 3166 */ 3167 probe_block = 0; 3168 page_no = 0; 3169 last_block = i_size_read(inode) >> blkbits; 3170 while ((probe_block + blocks_per_page) <= last_block && page_no < max) { 3171 unsigned block_in_page; 3172 sector_t first_block; 3173 3174 cond_resched(); 3175 3176 first_block = bmap(inode, probe_block); 3177 if (first_block == 0) 3178 goto bad_bmap; 3179 3180 /* 3181 * It must be PAGE_SIZE aligned on-disk 3182 */ 3183 if (first_block & (blocks_per_page - 1)) { 3184 probe_block++; 3185 goto reprobe; 3186 } 3187 3188 for (block_in_page = 1; block_in_page < blocks_per_page; 3189 block_in_page++) { 3190 sector_t block; 3191 3192 block = bmap(inode, probe_block + block_in_page); 3193 if (block == 0) 3194 goto bad_bmap; 3195 if (block != first_block + block_in_page) { 3196 /* Discontiguity */ 3197 probe_block++; 3198 goto reprobe; 3199 } 3200 } 3201 3202 first_block >>= (PAGE_SHIFT - blkbits); 3203 if (page_no) { /* exclude the header page */ 3204 if (first_block < lowest_block) 3205 lowest_block = first_block; 3206 if (first_block > highest_block) 3207 highest_block = first_block; 3208 } 3209 3210 page_no++; 3211 probe_block += blocks_per_page; 3212 reprobe: 3213 continue; 3214 } 3215 return 0; 3216 3217 bad_bmap: 3218 pr_err("swapon: swapfile has holes\n"); 3219 return -EINVAL; 3220 } 3221 3222 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, 3223 sector_t *span) 3224 { 3225 struct inode *inode = file_inode(file); 3226 int ret; 3227 3228 if (!S_ISREG(inode->i_mode)) 3229 return -EINVAL; 3230 3231 if (f2fs_readonly(F2FS_I_SB(inode)->sb)) 3232 return -EROFS; 3233 3234 ret = f2fs_convert_inline_inode(inode); 3235 if (ret) 3236 return ret; 3237 3238 ret = check_swap_activate(file, sis->max); 3239 if (ret) 3240 return ret; 3241 3242 set_inode_flag(inode, FI_PIN_FILE); 3243 f2fs_precache_extents(inode); 3244 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 3245 return 0; 3246 } 3247 3248 static void f2fs_swap_deactivate(struct file *file) 3249 { 3250 struct inode *inode = file_inode(file); 3251 3252 clear_inode_flag(inode, FI_PIN_FILE); 3253 } 3254 #else 3255 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, 3256 sector_t *span) 3257 { 3258 return -EOPNOTSUPP; 3259 } 3260 3261 static void f2fs_swap_deactivate(struct file *file) 3262 { 3263 } 3264 #endif 3265 3266 const struct address_space_operations f2fs_dblock_aops = { 3267 .readpage = f2fs_read_data_page, 3268 .readpages = f2fs_read_data_pages, 3269 .writepage = f2fs_write_data_page, 3270 .writepages = f2fs_write_data_pages, 3271 .write_begin = f2fs_write_begin, 3272 .write_end = f2fs_write_end, 3273 .set_page_dirty = f2fs_set_data_page_dirty, 3274 .invalidatepage = f2fs_invalidate_page, 3275 .releasepage = f2fs_release_page, 3276 .direct_IO = f2fs_direct_IO, 3277 .bmap = f2fs_bmap, 3278 .swap_activate = f2fs_swap_activate, 3279 .swap_deactivate = f2fs_swap_deactivate, 3280 #ifdef CONFIG_MIGRATION 3281 .migratepage = f2fs_migrate_page, 3282 #endif 3283 }; 3284 3285 void f2fs_clear_page_cache_dirty_tag(struct page *page) 3286 { 3287 struct address_space *mapping = page_mapping(page); 3288 unsigned long flags; 3289 3290 xa_lock_irqsave(&mapping->i_pages, flags); 3291 __xa_clear_mark(&mapping->i_pages, page_index(page), 3292 PAGECACHE_TAG_DIRTY); 3293 xa_unlock_irqrestore(&mapping->i_pages, flags); 3294 } 3295 3296 int __init f2fs_init_post_read_processing(void) 3297 { 3298 bio_post_read_ctx_cache = 3299 kmem_cache_create("f2fs_bio_post_read_ctx", 3300 sizeof(struct bio_post_read_ctx), 0, 0, NULL); 3301 if (!bio_post_read_ctx_cache) 3302 goto fail; 3303 bio_post_read_ctx_pool = 3304 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS, 3305 bio_post_read_ctx_cache); 3306 if (!bio_post_read_ctx_pool) 3307 goto fail_free_cache; 3308 return 0; 3309 3310 fail_free_cache: 3311 kmem_cache_destroy(bio_post_read_ctx_cache); 3312 fail: 3313 return -ENOMEM; 3314 } 3315 3316 void f2fs_destroy_post_read_processing(void) 3317 { 3318 mempool_destroy(bio_post_read_ctx_pool); 3319 kmem_cache_destroy(bio_post_read_ctx_cache); 3320 } 3321 3322 int __init f2fs_init_bio_entry_cache(void) 3323 { 3324 bio_entry_slab = f2fs_kmem_cache_create("bio_entry_slab", 3325 sizeof(struct bio_entry)); 3326 if (!bio_entry_slab) 3327 return -ENOMEM; 3328 return 0; 3329 } 3330 3331 void __exit f2fs_destroy_bio_entry_cache(void) 3332 { 3333 kmem_cache_destroy(bio_entry_slab); 3334 } 3335