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