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