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