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