1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * fs/f2fs/checkpoint.c 4 * 5 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 6 * http://www.samsung.com/ 7 */ 8 #include <linux/fs.h> 9 #include <linux/bio.h> 10 #include <linux/mpage.h> 11 #include <linux/writeback.h> 12 #include <linux/blkdev.h> 13 #include <linux/f2fs_fs.h> 14 #include <linux/pagevec.h> 15 #include <linux/swap.h> 16 #include <linux/kthread.h> 17 18 #include "f2fs.h" 19 #include "node.h" 20 #include "segment.h" 21 #include "iostat.h" 22 #include <trace/events/f2fs.h> 23 24 #define DEFAULT_CHECKPOINT_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3)) 25 26 static struct kmem_cache *ino_entry_slab; 27 struct kmem_cache *f2fs_inode_entry_slab; 28 29 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io, 30 unsigned char reason) 31 { 32 f2fs_build_fault_attr(sbi, 0, 0); 33 if (!end_io) 34 f2fs_flush_merged_writes(sbi); 35 f2fs_handle_critical_error(sbi, reason, end_io); 36 } 37 38 /* 39 * We guarantee no failure on the returned page. 40 */ 41 struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 42 { 43 struct address_space *mapping = META_MAPPING(sbi); 44 struct page *page; 45 repeat: 46 page = f2fs_grab_cache_page(mapping, index, false); 47 if (!page) { 48 cond_resched(); 49 goto repeat; 50 } 51 f2fs_wait_on_page_writeback(page, META, true, true); 52 if (!PageUptodate(page)) 53 SetPageUptodate(page); 54 return page; 55 } 56 57 static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index, 58 bool is_meta) 59 { 60 struct address_space *mapping = META_MAPPING(sbi); 61 struct page *page; 62 struct f2fs_io_info fio = { 63 .sbi = sbi, 64 .type = META, 65 .op = REQ_OP_READ, 66 .op_flags = REQ_META | REQ_PRIO, 67 .old_blkaddr = index, 68 .new_blkaddr = index, 69 .encrypted_page = NULL, 70 .is_por = !is_meta ? 1 : 0, 71 }; 72 int err; 73 74 if (unlikely(!is_meta)) 75 fio.op_flags &= ~REQ_META; 76 repeat: 77 page = f2fs_grab_cache_page(mapping, index, false); 78 if (!page) { 79 cond_resched(); 80 goto repeat; 81 } 82 if (PageUptodate(page)) 83 goto out; 84 85 fio.page = page; 86 87 err = f2fs_submit_page_bio(&fio); 88 if (err) { 89 f2fs_put_page(page, 1); 90 return ERR_PTR(err); 91 } 92 93 f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, F2FS_BLKSIZE); 94 95 lock_page(page); 96 if (unlikely(page->mapping != mapping)) { 97 f2fs_put_page(page, 1); 98 goto repeat; 99 } 100 101 if (unlikely(!PageUptodate(page))) { 102 f2fs_handle_page_eio(sbi, page->index, META); 103 f2fs_put_page(page, 1); 104 return ERR_PTR(-EIO); 105 } 106 out: 107 return page; 108 } 109 110 struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 111 { 112 return __get_meta_page(sbi, index, true); 113 } 114 115 struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index) 116 { 117 struct page *page; 118 int count = 0; 119 120 retry: 121 page = __get_meta_page(sbi, index, true); 122 if (IS_ERR(page)) { 123 if (PTR_ERR(page) == -EIO && 124 ++count <= DEFAULT_RETRY_IO_COUNT) 125 goto retry; 126 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_META_PAGE); 127 } 128 return page; 129 } 130 131 /* for POR only */ 132 struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index) 133 { 134 return __get_meta_page(sbi, index, false); 135 } 136 137 static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr, 138 int type) 139 { 140 struct seg_entry *se; 141 unsigned int segno, offset; 142 bool exist; 143 144 if (type == DATA_GENERIC) 145 return true; 146 147 segno = GET_SEGNO(sbi, blkaddr); 148 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 149 se = get_seg_entry(sbi, segno); 150 151 exist = f2fs_test_bit(offset, se->cur_valid_map); 152 153 /* skip data, if we already have an error in checkpoint. */ 154 if (unlikely(f2fs_cp_error(sbi))) 155 return exist; 156 157 if ((exist && type == DATA_GENERIC_ENHANCE_UPDATE) || 158 (!exist && type == DATA_GENERIC_ENHANCE)) 159 goto out_err; 160 if (!exist && type != DATA_GENERIC_ENHANCE_UPDATE) 161 goto out_handle; 162 return exist; 163 164 out_err: 165 f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d", 166 blkaddr, exist); 167 set_sbi_flag(sbi, SBI_NEED_FSCK); 168 dump_stack(); 169 out_handle: 170 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR); 171 return exist; 172 } 173 174 static bool __f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 175 block_t blkaddr, int type) 176 { 177 switch (type) { 178 case META_NAT: 179 break; 180 case META_SIT: 181 if (unlikely(blkaddr >= SIT_BLK_CNT(sbi))) 182 goto check_only; 183 break; 184 case META_SSA: 185 if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) || 186 blkaddr < SM_I(sbi)->ssa_blkaddr)) 187 goto check_only; 188 break; 189 case META_CP: 190 if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr || 191 blkaddr < __start_cp_addr(sbi))) 192 goto check_only; 193 break; 194 case META_POR: 195 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 196 blkaddr < MAIN_BLKADDR(sbi))) 197 goto check_only; 198 break; 199 case DATA_GENERIC: 200 case DATA_GENERIC_ENHANCE: 201 case DATA_GENERIC_ENHANCE_READ: 202 case DATA_GENERIC_ENHANCE_UPDATE: 203 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 204 blkaddr < MAIN_BLKADDR(sbi))) { 205 206 /* Skip to emit an error message. */ 207 if (unlikely(f2fs_cp_error(sbi))) 208 return false; 209 210 f2fs_warn(sbi, "access invalid blkaddr:%u", 211 blkaddr); 212 set_sbi_flag(sbi, SBI_NEED_FSCK); 213 dump_stack(); 214 goto err; 215 } else { 216 return __is_bitmap_valid(sbi, blkaddr, type); 217 } 218 break; 219 case META_GENERIC: 220 if (unlikely(blkaddr < SEG0_BLKADDR(sbi) || 221 blkaddr >= MAIN_BLKADDR(sbi))) 222 goto err; 223 break; 224 default: 225 BUG(); 226 } 227 228 return true; 229 err: 230 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR); 231 check_only: 232 return false; 233 } 234 235 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 236 block_t blkaddr, int type) 237 { 238 if (time_to_inject(sbi, FAULT_BLKADDR_VALIDITY)) 239 return false; 240 return __f2fs_is_valid_blkaddr(sbi, blkaddr, type); 241 } 242 243 bool f2fs_is_valid_blkaddr_raw(struct f2fs_sb_info *sbi, 244 block_t blkaddr, int type) 245 { 246 return __f2fs_is_valid_blkaddr(sbi, blkaddr, type); 247 } 248 249 /* 250 * Readahead CP/NAT/SIT/SSA/POR pages 251 */ 252 int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, 253 int type, bool sync) 254 { 255 struct page *page; 256 block_t blkno = start; 257 struct f2fs_io_info fio = { 258 .sbi = sbi, 259 .type = META, 260 .op = REQ_OP_READ, 261 .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD, 262 .encrypted_page = NULL, 263 .in_list = 0, 264 .is_por = (type == META_POR) ? 1 : 0, 265 }; 266 struct blk_plug plug; 267 int err; 268 269 if (unlikely(type == META_POR)) 270 fio.op_flags &= ~REQ_META; 271 272 blk_start_plug(&plug); 273 for (; nrpages-- > 0; blkno++) { 274 275 if (!f2fs_is_valid_blkaddr(sbi, blkno, type)) 276 goto out; 277 278 switch (type) { 279 case META_NAT: 280 if (unlikely(blkno >= 281 NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))) 282 blkno = 0; 283 /* get nat block addr */ 284 fio.new_blkaddr = current_nat_addr(sbi, 285 blkno * NAT_ENTRY_PER_BLOCK); 286 break; 287 case META_SIT: 288 if (unlikely(blkno >= TOTAL_SEGS(sbi))) 289 goto out; 290 /* get sit block addr */ 291 fio.new_blkaddr = current_sit_addr(sbi, 292 blkno * SIT_ENTRY_PER_BLOCK); 293 break; 294 case META_SSA: 295 case META_CP: 296 case META_POR: 297 fio.new_blkaddr = blkno; 298 break; 299 default: 300 BUG(); 301 } 302 303 page = f2fs_grab_cache_page(META_MAPPING(sbi), 304 fio.new_blkaddr, false); 305 if (!page) 306 continue; 307 if (PageUptodate(page)) { 308 f2fs_put_page(page, 1); 309 continue; 310 } 311 312 fio.page = page; 313 err = f2fs_submit_page_bio(&fio); 314 f2fs_put_page(page, err ? 1 : 0); 315 316 if (!err) 317 f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, 318 F2FS_BLKSIZE); 319 } 320 out: 321 blk_finish_plug(&plug); 322 return blkno - start; 323 } 324 325 void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index, 326 unsigned int ra_blocks) 327 { 328 struct page *page; 329 bool readahead = false; 330 331 if (ra_blocks == RECOVERY_MIN_RA_BLOCKS) 332 return; 333 334 page = find_get_page(META_MAPPING(sbi), index); 335 if (!page || !PageUptodate(page)) 336 readahead = true; 337 f2fs_put_page(page, 0); 338 339 if (readahead) 340 f2fs_ra_meta_pages(sbi, index, ra_blocks, META_POR, true); 341 } 342 343 static int __f2fs_write_meta_page(struct page *page, 344 struct writeback_control *wbc, 345 enum iostat_type io_type) 346 { 347 struct f2fs_sb_info *sbi = F2FS_P_SB(page); 348 349 trace_f2fs_writepage(page_folio(page), META); 350 351 if (unlikely(f2fs_cp_error(sbi))) { 352 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) { 353 ClearPageUptodate(page); 354 dec_page_count(sbi, F2FS_DIRTY_META); 355 unlock_page(page); 356 return 0; 357 } 358 goto redirty_out; 359 } 360 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 361 goto redirty_out; 362 if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0)) 363 goto redirty_out; 364 365 f2fs_do_write_meta_page(sbi, page, io_type); 366 dec_page_count(sbi, F2FS_DIRTY_META); 367 368 if (wbc->for_reclaim) 369 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META); 370 371 unlock_page(page); 372 373 if (unlikely(f2fs_cp_error(sbi))) 374 f2fs_submit_merged_write(sbi, META); 375 376 return 0; 377 378 redirty_out: 379 redirty_page_for_writepage(wbc, page); 380 return AOP_WRITEPAGE_ACTIVATE; 381 } 382 383 static int f2fs_write_meta_page(struct page *page, 384 struct writeback_control *wbc) 385 { 386 return __f2fs_write_meta_page(page, wbc, FS_META_IO); 387 } 388 389 static int f2fs_write_meta_pages(struct address_space *mapping, 390 struct writeback_control *wbc) 391 { 392 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 393 long diff, written; 394 395 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 396 goto skip_write; 397 398 /* collect a number of dirty meta pages and write together */ 399 if (wbc->sync_mode != WB_SYNC_ALL && 400 get_pages(sbi, F2FS_DIRTY_META) < 401 nr_pages_to_skip(sbi, META)) 402 goto skip_write; 403 404 /* if locked failed, cp will flush dirty pages instead */ 405 if (!f2fs_down_write_trylock(&sbi->cp_global_sem)) 406 goto skip_write; 407 408 trace_f2fs_writepages(mapping->host, wbc, META); 409 diff = nr_pages_to_write(sbi, META, wbc); 410 written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO); 411 f2fs_up_write(&sbi->cp_global_sem); 412 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 413 return 0; 414 415 skip_write: 416 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 417 trace_f2fs_writepages(mapping->host, wbc, META); 418 return 0; 419 } 420 421 long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 422 long nr_to_write, enum iostat_type io_type) 423 { 424 struct address_space *mapping = META_MAPPING(sbi); 425 pgoff_t index = 0, prev = ULONG_MAX; 426 struct folio_batch fbatch; 427 long nwritten = 0; 428 int nr_folios; 429 struct writeback_control wbc = { 430 .for_reclaim = 0, 431 }; 432 struct blk_plug plug; 433 434 folio_batch_init(&fbatch); 435 436 blk_start_plug(&plug); 437 438 while ((nr_folios = filemap_get_folios_tag(mapping, &index, 439 (pgoff_t)-1, 440 PAGECACHE_TAG_DIRTY, &fbatch))) { 441 int i; 442 443 for (i = 0; i < nr_folios; i++) { 444 struct folio *folio = fbatch.folios[i]; 445 446 if (nr_to_write != LONG_MAX && i != 0 && 447 folio->index != prev + 448 folio_nr_pages(fbatch.folios[i-1])) { 449 folio_batch_release(&fbatch); 450 goto stop; 451 } 452 453 folio_lock(folio); 454 455 if (unlikely(folio->mapping != mapping)) { 456 continue_unlock: 457 folio_unlock(folio); 458 continue; 459 } 460 if (!folio_test_dirty(folio)) { 461 /* someone wrote it for us */ 462 goto continue_unlock; 463 } 464 465 f2fs_wait_on_page_writeback(&folio->page, META, 466 true, true); 467 468 if (!folio_clear_dirty_for_io(folio)) 469 goto continue_unlock; 470 471 if (__f2fs_write_meta_page(&folio->page, &wbc, 472 io_type)) { 473 folio_unlock(folio); 474 break; 475 } 476 nwritten += folio_nr_pages(folio); 477 prev = folio->index; 478 if (unlikely(nwritten >= nr_to_write)) 479 break; 480 } 481 folio_batch_release(&fbatch); 482 cond_resched(); 483 } 484 stop: 485 if (nwritten) 486 f2fs_submit_merged_write(sbi, type); 487 488 blk_finish_plug(&plug); 489 490 return nwritten; 491 } 492 493 static bool f2fs_dirty_meta_folio(struct address_space *mapping, 494 struct folio *folio) 495 { 496 trace_f2fs_set_page_dirty(folio, META); 497 498 if (!folio_test_uptodate(folio)) 499 folio_mark_uptodate(folio); 500 if (filemap_dirty_folio(mapping, folio)) { 501 inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_META); 502 set_page_private_reference(&folio->page); 503 return true; 504 } 505 return false; 506 } 507 508 const struct address_space_operations f2fs_meta_aops = { 509 .writepage = f2fs_write_meta_page, 510 .writepages = f2fs_write_meta_pages, 511 .dirty_folio = f2fs_dirty_meta_folio, 512 .invalidate_folio = f2fs_invalidate_folio, 513 .release_folio = f2fs_release_folio, 514 .migrate_folio = filemap_migrate_folio, 515 }; 516 517 static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, 518 unsigned int devidx, int type) 519 { 520 struct inode_management *im = &sbi->im[type]; 521 struct ino_entry *e = NULL, *new = NULL; 522 523 if (type == FLUSH_INO) { 524 rcu_read_lock(); 525 e = radix_tree_lookup(&im->ino_root, ino); 526 rcu_read_unlock(); 527 } 528 529 retry: 530 if (!e) 531 new = f2fs_kmem_cache_alloc(ino_entry_slab, 532 GFP_NOFS, true, NULL); 533 534 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 535 536 spin_lock(&im->ino_lock); 537 e = radix_tree_lookup(&im->ino_root, ino); 538 if (!e) { 539 if (!new) { 540 spin_unlock(&im->ino_lock); 541 radix_tree_preload_end(); 542 goto retry; 543 } 544 e = new; 545 if (unlikely(radix_tree_insert(&im->ino_root, ino, e))) 546 f2fs_bug_on(sbi, 1); 547 548 memset(e, 0, sizeof(struct ino_entry)); 549 e->ino = ino; 550 551 list_add_tail(&e->list, &im->ino_list); 552 if (type != ORPHAN_INO) 553 im->ino_num++; 554 } 555 556 if (type == FLUSH_INO) 557 f2fs_set_bit(devidx, (char *)&e->dirty_device); 558 559 spin_unlock(&im->ino_lock); 560 radix_tree_preload_end(); 561 562 if (new && e != new) 563 kmem_cache_free(ino_entry_slab, new); 564 } 565 566 static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 567 { 568 struct inode_management *im = &sbi->im[type]; 569 struct ino_entry *e; 570 571 spin_lock(&im->ino_lock); 572 e = radix_tree_lookup(&im->ino_root, ino); 573 if (e) { 574 list_del(&e->list); 575 radix_tree_delete(&im->ino_root, ino); 576 im->ino_num--; 577 spin_unlock(&im->ino_lock); 578 kmem_cache_free(ino_entry_slab, e); 579 return; 580 } 581 spin_unlock(&im->ino_lock); 582 } 583 584 void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 585 { 586 /* add new dirty ino entry into list */ 587 __add_ino_entry(sbi, ino, 0, type); 588 } 589 590 void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 591 { 592 /* remove dirty ino entry from list */ 593 __remove_ino_entry(sbi, ino, type); 594 } 595 596 /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */ 597 bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 598 { 599 struct inode_management *im = &sbi->im[mode]; 600 struct ino_entry *e; 601 602 spin_lock(&im->ino_lock); 603 e = radix_tree_lookup(&im->ino_root, ino); 604 spin_unlock(&im->ino_lock); 605 return e ? true : false; 606 } 607 608 void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all) 609 { 610 struct ino_entry *e, *tmp; 611 int i; 612 613 for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) { 614 struct inode_management *im = &sbi->im[i]; 615 616 spin_lock(&im->ino_lock); 617 list_for_each_entry_safe(e, tmp, &im->ino_list, list) { 618 list_del(&e->list); 619 radix_tree_delete(&im->ino_root, e->ino); 620 kmem_cache_free(ino_entry_slab, e); 621 im->ino_num--; 622 } 623 spin_unlock(&im->ino_lock); 624 } 625 } 626 627 void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 628 unsigned int devidx, int type) 629 { 630 __add_ino_entry(sbi, ino, devidx, type); 631 } 632 633 bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 634 unsigned int devidx, int type) 635 { 636 struct inode_management *im = &sbi->im[type]; 637 struct ino_entry *e; 638 bool is_dirty = false; 639 640 spin_lock(&im->ino_lock); 641 e = radix_tree_lookup(&im->ino_root, ino); 642 if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device)) 643 is_dirty = true; 644 spin_unlock(&im->ino_lock); 645 return is_dirty; 646 } 647 648 int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi) 649 { 650 struct inode_management *im = &sbi->im[ORPHAN_INO]; 651 int err = 0; 652 653 spin_lock(&im->ino_lock); 654 655 if (time_to_inject(sbi, FAULT_ORPHAN)) { 656 spin_unlock(&im->ino_lock); 657 return -ENOSPC; 658 } 659 660 if (unlikely(im->ino_num >= sbi->max_orphans)) 661 err = -ENOSPC; 662 else 663 im->ino_num++; 664 spin_unlock(&im->ino_lock); 665 666 return err; 667 } 668 669 void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi) 670 { 671 struct inode_management *im = &sbi->im[ORPHAN_INO]; 672 673 spin_lock(&im->ino_lock); 674 f2fs_bug_on(sbi, im->ino_num == 0); 675 im->ino_num--; 676 spin_unlock(&im->ino_lock); 677 } 678 679 void f2fs_add_orphan_inode(struct inode *inode) 680 { 681 /* add new orphan ino entry into list */ 682 __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO); 683 f2fs_update_inode_page(inode); 684 } 685 686 void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 687 { 688 /* remove orphan entry from orphan list */ 689 __remove_ino_entry(sbi, ino, ORPHAN_INO); 690 } 691 692 static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 693 { 694 struct inode *inode; 695 struct node_info ni; 696 int err; 697 698 inode = f2fs_iget_retry(sbi->sb, ino); 699 if (IS_ERR(inode)) { 700 /* 701 * there should be a bug that we can't find the entry 702 * to orphan inode. 703 */ 704 f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); 705 return PTR_ERR(inode); 706 } 707 708 err = f2fs_dquot_initialize(inode); 709 if (err) { 710 iput(inode); 711 goto err_out; 712 } 713 714 clear_nlink(inode); 715 716 /* truncate all the data during iput */ 717 iput(inode); 718 719 err = f2fs_get_node_info(sbi, ino, &ni, false); 720 if (err) 721 goto err_out; 722 723 /* ENOMEM was fully retried in f2fs_evict_inode. */ 724 if (ni.blk_addr != NULL_ADDR) { 725 err = -EIO; 726 goto err_out; 727 } 728 return 0; 729 730 err_out: 731 set_sbi_flag(sbi, SBI_NEED_FSCK); 732 f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.", 733 __func__, ino); 734 return err; 735 } 736 737 int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) 738 { 739 block_t start_blk, orphan_blocks, i, j; 740 int err = 0; 741 742 if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 743 return 0; 744 745 if (f2fs_hw_is_readonly(sbi)) { 746 f2fs_info(sbi, "write access unavailable, skipping orphan cleanup"); 747 return 0; 748 } 749 750 if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) 751 f2fs_info(sbi, "orphan cleanup on readonly fs"); 752 753 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 754 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 755 756 f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); 757 758 for (i = 0; i < orphan_blocks; i++) { 759 struct page *page; 760 struct f2fs_orphan_block *orphan_blk; 761 762 page = f2fs_get_meta_page(sbi, start_blk + i); 763 if (IS_ERR(page)) { 764 err = PTR_ERR(page); 765 goto out; 766 } 767 768 orphan_blk = (struct f2fs_orphan_block *)page_address(page); 769 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 770 nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 771 772 err = recover_orphan_inode(sbi, ino); 773 if (err) { 774 f2fs_put_page(page, 1); 775 goto out; 776 } 777 } 778 f2fs_put_page(page, 1); 779 } 780 /* clear Orphan Flag */ 781 clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG); 782 out: 783 set_sbi_flag(sbi, SBI_IS_RECOVERED); 784 785 return err; 786 } 787 788 static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 789 { 790 struct list_head *head; 791 struct f2fs_orphan_block *orphan_blk = NULL; 792 unsigned int nentries = 0; 793 unsigned short index = 1; 794 unsigned short orphan_blocks; 795 struct page *page = NULL; 796 struct ino_entry *orphan = NULL; 797 struct inode_management *im = &sbi->im[ORPHAN_INO]; 798 799 orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); 800 801 /* 802 * we don't need to do spin_lock(&im->ino_lock) here, since all the 803 * orphan inode operations are covered under f2fs_lock_op(). 804 * And, spin_lock should be avoided due to page operations below. 805 */ 806 head = &im->ino_list; 807 808 /* loop for each orphan inode entry and write them in journal block */ 809 list_for_each_entry(orphan, head, list) { 810 if (!page) { 811 page = f2fs_grab_meta_page(sbi, start_blk++); 812 orphan_blk = 813 (struct f2fs_orphan_block *)page_address(page); 814 memset(orphan_blk, 0, sizeof(*orphan_blk)); 815 } 816 817 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 818 819 if (nentries == F2FS_ORPHANS_PER_BLOCK) { 820 /* 821 * an orphan block is full of 1020 entries, 822 * then we need to flush current orphan blocks 823 * and bring another one in memory 824 */ 825 orphan_blk->blk_addr = cpu_to_le16(index); 826 orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 827 orphan_blk->entry_count = cpu_to_le32(nentries); 828 set_page_dirty(page); 829 f2fs_put_page(page, 1); 830 index++; 831 nentries = 0; 832 page = NULL; 833 } 834 } 835 836 if (page) { 837 orphan_blk->blk_addr = cpu_to_le16(index); 838 orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 839 orphan_blk->entry_count = cpu_to_le32(nentries); 840 set_page_dirty(page); 841 f2fs_put_page(page, 1); 842 } 843 } 844 845 static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi, 846 struct f2fs_checkpoint *ckpt) 847 { 848 unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset); 849 __u32 chksum; 850 851 chksum = f2fs_crc32(sbi, ckpt, chksum_ofs); 852 if (chksum_ofs < CP_CHKSUM_OFFSET) { 853 chksum_ofs += sizeof(chksum); 854 chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs, 855 F2FS_BLKSIZE - chksum_ofs); 856 } 857 return chksum; 858 } 859 860 static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr, 861 struct f2fs_checkpoint **cp_block, struct page **cp_page, 862 unsigned long long *version) 863 { 864 size_t crc_offset = 0; 865 __u32 crc; 866 867 *cp_page = f2fs_get_meta_page(sbi, cp_addr); 868 if (IS_ERR(*cp_page)) 869 return PTR_ERR(*cp_page); 870 871 *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page); 872 873 crc_offset = le32_to_cpu((*cp_block)->checksum_offset); 874 if (crc_offset < CP_MIN_CHKSUM_OFFSET || 875 crc_offset > CP_CHKSUM_OFFSET) { 876 f2fs_put_page(*cp_page, 1); 877 f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset); 878 return -EINVAL; 879 } 880 881 crc = f2fs_checkpoint_chksum(sbi, *cp_block); 882 if (crc != cur_cp_crc(*cp_block)) { 883 f2fs_put_page(*cp_page, 1); 884 f2fs_warn(sbi, "invalid crc value"); 885 return -EINVAL; 886 } 887 888 *version = cur_cp_version(*cp_block); 889 return 0; 890 } 891 892 static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 893 block_t cp_addr, unsigned long long *version) 894 { 895 struct page *cp_page_1 = NULL, *cp_page_2 = NULL; 896 struct f2fs_checkpoint *cp_block = NULL; 897 unsigned long long cur_version = 0, pre_version = 0; 898 unsigned int cp_blocks; 899 int err; 900 901 err = get_checkpoint_version(sbi, cp_addr, &cp_block, 902 &cp_page_1, version); 903 if (err) 904 return NULL; 905 906 cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count); 907 908 if (cp_blocks > BLKS_PER_SEG(sbi) || cp_blocks <= F2FS_CP_PACKS) { 909 f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u", 910 le32_to_cpu(cp_block->cp_pack_total_block_count)); 911 goto invalid_cp; 912 } 913 pre_version = *version; 914 915 cp_addr += cp_blocks - 1; 916 err = get_checkpoint_version(sbi, cp_addr, &cp_block, 917 &cp_page_2, version); 918 if (err) 919 goto invalid_cp; 920 cur_version = *version; 921 922 if (cur_version == pre_version) { 923 *version = cur_version; 924 f2fs_put_page(cp_page_2, 1); 925 return cp_page_1; 926 } 927 f2fs_put_page(cp_page_2, 1); 928 invalid_cp: 929 f2fs_put_page(cp_page_1, 1); 930 return NULL; 931 } 932 933 int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi) 934 { 935 struct f2fs_checkpoint *cp_block; 936 struct f2fs_super_block *fsb = sbi->raw_super; 937 struct page *cp1, *cp2, *cur_page; 938 unsigned long blk_size = sbi->blocksize; 939 unsigned long long cp1_version = 0, cp2_version = 0; 940 unsigned long long cp_start_blk_no; 941 unsigned int cp_blks = 1 + __cp_payload(sbi); 942 block_t cp_blk_no; 943 int i; 944 int err; 945 946 sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks), 947 GFP_KERNEL); 948 if (!sbi->ckpt) 949 return -ENOMEM; 950 /* 951 * Finding out valid cp block involves read both 952 * sets( cp pack 1 and cp pack 2) 953 */ 954 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 955 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 956 957 /* The second checkpoint pack should start at the next segment */ 958 cp_start_blk_no += ((unsigned long long)1) << 959 le32_to_cpu(fsb->log_blocks_per_seg); 960 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 961 962 if (cp1 && cp2) { 963 if (ver_after(cp2_version, cp1_version)) 964 cur_page = cp2; 965 else 966 cur_page = cp1; 967 } else if (cp1) { 968 cur_page = cp1; 969 } else if (cp2) { 970 cur_page = cp2; 971 } else { 972 err = -EFSCORRUPTED; 973 goto fail_no_cp; 974 } 975 976 cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 977 memcpy(sbi->ckpt, cp_block, blk_size); 978 979 if (cur_page == cp1) 980 sbi->cur_cp_pack = 1; 981 else 982 sbi->cur_cp_pack = 2; 983 984 /* Sanity checking of checkpoint */ 985 if (f2fs_sanity_check_ckpt(sbi)) { 986 err = -EFSCORRUPTED; 987 goto free_fail_no_cp; 988 } 989 990 if (cp_blks <= 1) 991 goto done; 992 993 cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 994 if (cur_page == cp2) 995 cp_blk_no += BIT(le32_to_cpu(fsb->log_blocks_per_seg)); 996 997 for (i = 1; i < cp_blks; i++) { 998 void *sit_bitmap_ptr; 999 unsigned char *ckpt = (unsigned char *)sbi->ckpt; 1000 1001 cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i); 1002 if (IS_ERR(cur_page)) { 1003 err = PTR_ERR(cur_page); 1004 goto free_fail_no_cp; 1005 } 1006 sit_bitmap_ptr = page_address(cur_page); 1007 memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 1008 f2fs_put_page(cur_page, 1); 1009 } 1010 done: 1011 f2fs_put_page(cp1, 1); 1012 f2fs_put_page(cp2, 1); 1013 return 0; 1014 1015 free_fail_no_cp: 1016 f2fs_put_page(cp1, 1); 1017 f2fs_put_page(cp2, 1); 1018 fail_no_cp: 1019 kvfree(sbi->ckpt); 1020 return err; 1021 } 1022 1023 static void __add_dirty_inode(struct inode *inode, enum inode_type type) 1024 { 1025 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1026 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 1027 1028 if (is_inode_flag_set(inode, flag)) 1029 return; 1030 1031 set_inode_flag(inode, flag); 1032 list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]); 1033 stat_inc_dirty_inode(sbi, type); 1034 } 1035 1036 static void __remove_dirty_inode(struct inode *inode, enum inode_type type) 1037 { 1038 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 1039 1040 if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag)) 1041 return; 1042 1043 list_del_init(&F2FS_I(inode)->dirty_list); 1044 clear_inode_flag(inode, flag); 1045 stat_dec_dirty_inode(F2FS_I_SB(inode), type); 1046 } 1047 1048 void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio) 1049 { 1050 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1051 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 1052 1053 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 1054 !S_ISLNK(inode->i_mode)) 1055 return; 1056 1057 spin_lock(&sbi->inode_lock[type]); 1058 if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH)) 1059 __add_dirty_inode(inode, type); 1060 inode_inc_dirty_pages(inode); 1061 spin_unlock(&sbi->inode_lock[type]); 1062 1063 set_page_private_reference(&folio->page); 1064 } 1065 1066 void f2fs_remove_dirty_inode(struct inode *inode) 1067 { 1068 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1069 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 1070 1071 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 1072 !S_ISLNK(inode->i_mode)) 1073 return; 1074 1075 if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH)) 1076 return; 1077 1078 spin_lock(&sbi->inode_lock[type]); 1079 __remove_dirty_inode(inode, type); 1080 spin_unlock(&sbi->inode_lock[type]); 1081 } 1082 1083 int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type, 1084 bool from_cp) 1085 { 1086 struct list_head *head; 1087 struct inode *inode; 1088 struct f2fs_inode_info *fi; 1089 bool is_dir = (type == DIR_INODE); 1090 unsigned long ino = 0; 1091 1092 trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, 1093 get_pages(sbi, is_dir ? 1094 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 1095 retry: 1096 if (unlikely(f2fs_cp_error(sbi))) { 1097 trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 1098 get_pages(sbi, is_dir ? 1099 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 1100 return -EIO; 1101 } 1102 1103 spin_lock(&sbi->inode_lock[type]); 1104 1105 head = &sbi->inode_list[type]; 1106 if (list_empty(head)) { 1107 spin_unlock(&sbi->inode_lock[type]); 1108 trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 1109 get_pages(sbi, is_dir ? 1110 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 1111 return 0; 1112 } 1113 fi = list_first_entry(head, struct f2fs_inode_info, dirty_list); 1114 inode = igrab(&fi->vfs_inode); 1115 spin_unlock(&sbi->inode_lock[type]); 1116 if (inode) { 1117 unsigned long cur_ino = inode->i_ino; 1118 1119 if (from_cp) 1120 F2FS_I(inode)->cp_task = current; 1121 F2FS_I(inode)->wb_task = current; 1122 1123 filemap_fdatawrite(inode->i_mapping); 1124 1125 F2FS_I(inode)->wb_task = NULL; 1126 if (from_cp) 1127 F2FS_I(inode)->cp_task = NULL; 1128 1129 iput(inode); 1130 /* We need to give cpu to another writers. */ 1131 if (ino == cur_ino) 1132 cond_resched(); 1133 else 1134 ino = cur_ino; 1135 } else { 1136 /* 1137 * We should submit bio, since it exists several 1138 * writebacking dentry pages in the freeing inode. 1139 */ 1140 f2fs_submit_merged_write(sbi, DATA); 1141 cond_resched(); 1142 } 1143 goto retry; 1144 } 1145 1146 static int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi) 1147 { 1148 struct list_head *head = &sbi->inode_list[DIRTY_META]; 1149 struct inode *inode; 1150 struct f2fs_inode_info *fi; 1151 s64 total = get_pages(sbi, F2FS_DIRTY_IMETA); 1152 1153 while (total--) { 1154 if (unlikely(f2fs_cp_error(sbi))) 1155 return -EIO; 1156 1157 spin_lock(&sbi->inode_lock[DIRTY_META]); 1158 if (list_empty(head)) { 1159 spin_unlock(&sbi->inode_lock[DIRTY_META]); 1160 return 0; 1161 } 1162 fi = list_first_entry(head, struct f2fs_inode_info, 1163 gdirty_list); 1164 inode = igrab(&fi->vfs_inode); 1165 spin_unlock(&sbi->inode_lock[DIRTY_META]); 1166 if (inode) { 1167 sync_inode_metadata(inode, 0); 1168 1169 /* it's on eviction */ 1170 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) 1171 f2fs_update_inode_page(inode); 1172 iput(inode); 1173 } 1174 } 1175 return 0; 1176 } 1177 1178 static void __prepare_cp_block(struct f2fs_sb_info *sbi) 1179 { 1180 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1181 struct f2fs_nm_info *nm_i = NM_I(sbi); 1182 nid_t last_nid = nm_i->next_scan_nid; 1183 1184 next_free_nid(sbi, &last_nid); 1185 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 1186 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 1187 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 1188 ckpt->next_free_nid = cpu_to_le32(last_nid); 1189 1190 /* update user_block_counts */ 1191 sbi->last_valid_block_count = sbi->total_valid_block_count; 1192 percpu_counter_set(&sbi->alloc_valid_block_count, 0); 1193 percpu_counter_set(&sbi->rf_node_block_count, 0); 1194 } 1195 1196 static bool __need_flush_quota(struct f2fs_sb_info *sbi) 1197 { 1198 bool ret = false; 1199 1200 if (!is_journalled_quota(sbi)) 1201 return false; 1202 1203 if (!f2fs_down_write_trylock(&sbi->quota_sem)) 1204 return true; 1205 if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) { 1206 ret = false; 1207 } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) { 1208 ret = false; 1209 } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) { 1210 clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1211 ret = true; 1212 } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) { 1213 ret = true; 1214 } 1215 f2fs_up_write(&sbi->quota_sem); 1216 return ret; 1217 } 1218 1219 /* 1220 * Freeze all the FS-operations for checkpoint. 1221 */ 1222 static int block_operations(struct f2fs_sb_info *sbi) 1223 { 1224 struct writeback_control wbc = { 1225 .sync_mode = WB_SYNC_ALL, 1226 .nr_to_write = LONG_MAX, 1227 .for_reclaim = 0, 1228 }; 1229 int err = 0, cnt = 0; 1230 1231 /* 1232 * Let's flush inline_data in dirty node pages. 1233 */ 1234 f2fs_flush_inline_data(sbi); 1235 1236 retry_flush_quotas: 1237 f2fs_lock_all(sbi); 1238 if (__need_flush_quota(sbi)) { 1239 int locked; 1240 1241 if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) { 1242 set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1243 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1244 goto retry_flush_dents; 1245 } 1246 f2fs_unlock_all(sbi); 1247 1248 /* only failed during mount/umount/freeze/quotactl */ 1249 locked = down_read_trylock(&sbi->sb->s_umount); 1250 f2fs_quota_sync(sbi->sb, -1); 1251 if (locked) 1252 up_read(&sbi->sb->s_umount); 1253 cond_resched(); 1254 goto retry_flush_quotas; 1255 } 1256 1257 retry_flush_dents: 1258 /* write all the dirty dentry pages */ 1259 if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 1260 f2fs_unlock_all(sbi); 1261 err = f2fs_sync_dirty_inodes(sbi, DIR_INODE, true); 1262 if (err) 1263 return err; 1264 cond_resched(); 1265 goto retry_flush_quotas; 1266 } 1267 1268 /* 1269 * POR: we should ensure that there are no dirty node pages 1270 * until finishing nat/sit flush. inode->i_blocks can be updated. 1271 */ 1272 f2fs_down_write(&sbi->node_change); 1273 1274 if (get_pages(sbi, F2FS_DIRTY_IMETA)) { 1275 f2fs_up_write(&sbi->node_change); 1276 f2fs_unlock_all(sbi); 1277 err = f2fs_sync_inode_meta(sbi); 1278 if (err) 1279 return err; 1280 cond_resched(); 1281 goto retry_flush_quotas; 1282 } 1283 1284 retry_flush_nodes: 1285 f2fs_down_write(&sbi->node_write); 1286 1287 if (get_pages(sbi, F2FS_DIRTY_NODES)) { 1288 f2fs_up_write(&sbi->node_write); 1289 atomic_inc(&sbi->wb_sync_req[NODE]); 1290 err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO); 1291 atomic_dec(&sbi->wb_sync_req[NODE]); 1292 if (err) { 1293 f2fs_up_write(&sbi->node_change); 1294 f2fs_unlock_all(sbi); 1295 return err; 1296 } 1297 cond_resched(); 1298 goto retry_flush_nodes; 1299 } 1300 1301 /* 1302 * sbi->node_change is used only for AIO write_begin path which produces 1303 * dirty node blocks and some checkpoint values by block allocation. 1304 */ 1305 __prepare_cp_block(sbi); 1306 f2fs_up_write(&sbi->node_change); 1307 return err; 1308 } 1309 1310 static void unblock_operations(struct f2fs_sb_info *sbi) 1311 { 1312 f2fs_up_write(&sbi->node_write); 1313 f2fs_unlock_all(sbi); 1314 } 1315 1316 void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type) 1317 { 1318 DEFINE_WAIT(wait); 1319 1320 for (;;) { 1321 if (!get_pages(sbi, type)) 1322 break; 1323 1324 if (unlikely(f2fs_cp_error(sbi) && 1325 !is_sbi_flag_set(sbi, SBI_IS_CLOSE))) 1326 break; 1327 1328 if (type == F2FS_DIRTY_META) 1329 f2fs_sync_meta_pages(sbi, META, LONG_MAX, 1330 FS_CP_META_IO); 1331 else if (type == F2FS_WB_CP_DATA) 1332 f2fs_submit_merged_write(sbi, DATA); 1333 1334 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 1335 io_schedule_timeout(DEFAULT_IO_TIMEOUT); 1336 } 1337 finish_wait(&sbi->cp_wait, &wait); 1338 } 1339 1340 static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1341 { 1342 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 1343 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1344 unsigned long flags; 1345 1346 if (cpc->reason & CP_UMOUNT) { 1347 if (le32_to_cpu(ckpt->cp_pack_total_block_count) + 1348 NM_I(sbi)->nat_bits_blocks > BLKS_PER_SEG(sbi)) { 1349 clear_ckpt_flags(sbi, CP_NAT_BITS_FLAG); 1350 f2fs_notice(sbi, "Disable nat_bits due to no space"); 1351 } else if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG) && 1352 f2fs_nat_bitmap_enabled(sbi)) { 1353 f2fs_enable_nat_bits(sbi); 1354 set_ckpt_flags(sbi, CP_NAT_BITS_FLAG); 1355 f2fs_notice(sbi, "Rebuild and enable nat_bits"); 1356 } 1357 } 1358 1359 spin_lock_irqsave(&sbi->cp_lock, flags); 1360 1361 if (cpc->reason & CP_TRIMMED) 1362 __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 1363 else 1364 __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 1365 1366 if (cpc->reason & CP_UMOUNT) 1367 __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1368 else 1369 __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1370 1371 if (cpc->reason & CP_FASTBOOT) 1372 __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1373 else 1374 __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1375 1376 if (orphan_num) 1377 __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1378 else 1379 __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1380 1381 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 1382 __set_ckpt_flags(ckpt, CP_FSCK_FLAG); 1383 1384 if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS)) 1385 __set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1386 else 1387 __clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1388 1389 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED)) 1390 __set_ckpt_flags(ckpt, CP_DISABLED_FLAG); 1391 else 1392 __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG); 1393 1394 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK)) 1395 __set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1396 else 1397 __clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1398 1399 if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) 1400 __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1401 else 1402 __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1403 1404 if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) 1405 __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1406 1407 /* set this flag to activate crc|cp_ver for recovery */ 1408 __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG); 1409 __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG); 1410 1411 spin_unlock_irqrestore(&sbi->cp_lock, flags); 1412 } 1413 1414 static void commit_checkpoint(struct f2fs_sb_info *sbi, 1415 void *src, block_t blk_addr) 1416 { 1417 struct writeback_control wbc = { 1418 .for_reclaim = 0, 1419 }; 1420 1421 /* 1422 * filemap_get_folios_tag and lock_page again will take 1423 * some extra time. Therefore, f2fs_update_meta_pages and 1424 * f2fs_sync_meta_pages are combined in this function. 1425 */ 1426 struct page *page = f2fs_grab_meta_page(sbi, blk_addr); 1427 int err; 1428 1429 f2fs_wait_on_page_writeback(page, META, true, true); 1430 1431 memcpy(page_address(page), src, PAGE_SIZE); 1432 1433 set_page_dirty(page); 1434 if (unlikely(!clear_page_dirty_for_io(page))) 1435 f2fs_bug_on(sbi, 1); 1436 1437 /* writeout cp pack 2 page */ 1438 err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO); 1439 if (unlikely(err && f2fs_cp_error(sbi))) { 1440 f2fs_put_page(page, 1); 1441 return; 1442 } 1443 1444 f2fs_bug_on(sbi, err); 1445 f2fs_put_page(page, 0); 1446 1447 /* submit checkpoint (with barrier if NOBARRIER is not set) */ 1448 f2fs_submit_merged_write(sbi, META_FLUSH); 1449 } 1450 1451 static inline u64 get_sectors_written(struct block_device *bdev) 1452 { 1453 return (u64)part_stat_read(bdev, sectors[STAT_WRITE]); 1454 } 1455 1456 u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi) 1457 { 1458 if (f2fs_is_multi_device(sbi)) { 1459 u64 sectors = 0; 1460 int i; 1461 1462 for (i = 0; i < sbi->s_ndevs; i++) 1463 sectors += get_sectors_written(FDEV(i).bdev); 1464 1465 return sectors; 1466 } 1467 1468 return get_sectors_written(sbi->sb->s_bdev); 1469 } 1470 1471 static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1472 { 1473 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1474 struct f2fs_nm_info *nm_i = NM_I(sbi); 1475 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags; 1476 block_t start_blk; 1477 unsigned int data_sum_blocks, orphan_blocks; 1478 __u32 crc32 = 0; 1479 int i; 1480 int cp_payload_blks = __cp_payload(sbi); 1481 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 1482 u64 kbytes_written; 1483 int err; 1484 1485 /* Flush all the NAT/SIT pages */ 1486 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1487 1488 /* start to update checkpoint, cp ver is already updated previously */ 1489 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true)); 1490 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 1491 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 1492 struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_NODE); 1493 1494 ckpt->cur_node_segno[i] = cpu_to_le32(curseg->segno); 1495 ckpt->cur_node_blkoff[i] = cpu_to_le16(curseg->next_blkoff); 1496 ckpt->alloc_type[i + CURSEG_HOT_NODE] = curseg->alloc_type; 1497 } 1498 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 1499 struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_DATA); 1500 1501 ckpt->cur_data_segno[i] = cpu_to_le32(curseg->segno); 1502 ckpt->cur_data_blkoff[i] = cpu_to_le16(curseg->next_blkoff); 1503 ckpt->alloc_type[i + CURSEG_HOT_DATA] = curseg->alloc_type; 1504 } 1505 1506 /* 2 cp + n data seg summary + orphan inode blocks */ 1507 data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false); 1508 spin_lock_irqsave(&sbi->cp_lock, flags); 1509 if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 1510 __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1511 else 1512 __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1513 spin_unlock_irqrestore(&sbi->cp_lock, flags); 1514 1515 orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); 1516 ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 1517 orphan_blocks); 1518 1519 if (__remain_node_summaries(cpc->reason)) 1520 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 1521 cp_payload_blks + data_sum_blocks + 1522 orphan_blocks + NR_CURSEG_NODE_TYPE); 1523 else 1524 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 1525 cp_payload_blks + data_sum_blocks + 1526 orphan_blocks); 1527 1528 /* update ckpt flag for checkpoint */ 1529 update_ckpt_flags(sbi, cpc); 1530 1531 /* update SIT/NAT bitmap */ 1532 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 1533 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 1534 1535 crc32 = f2fs_checkpoint_chksum(sbi, ckpt); 1536 *((__le32 *)((unsigned char *)ckpt + 1537 le32_to_cpu(ckpt->checksum_offset))) 1538 = cpu_to_le32(crc32); 1539 1540 start_blk = __start_cp_next_addr(sbi); 1541 1542 /* write nat bits */ 1543 if ((cpc->reason & CP_UMOUNT) && 1544 is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG)) { 1545 __u64 cp_ver = cur_cp_version(ckpt); 1546 block_t blk; 1547 1548 cp_ver |= ((__u64)crc32 << 32); 1549 *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver); 1550 1551 blk = start_blk + BLKS_PER_SEG(sbi) - nm_i->nat_bits_blocks; 1552 for (i = 0; i < nm_i->nat_bits_blocks; i++) 1553 f2fs_update_meta_page(sbi, nm_i->nat_bits + 1554 (i << F2FS_BLKSIZE_BITS), blk + i); 1555 } 1556 1557 /* write out checkpoint buffer at block 0 */ 1558 f2fs_update_meta_page(sbi, ckpt, start_blk++); 1559 1560 for (i = 1; i < 1 + cp_payload_blks; i++) 1561 f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, 1562 start_blk++); 1563 1564 if (orphan_num) { 1565 write_orphan_inodes(sbi, start_blk); 1566 start_blk += orphan_blocks; 1567 } 1568 1569 f2fs_write_data_summaries(sbi, start_blk); 1570 start_blk += data_sum_blocks; 1571 1572 /* Record write statistics in the hot node summary */ 1573 kbytes_written = sbi->kbytes_written; 1574 kbytes_written += (f2fs_get_sectors_written(sbi) - 1575 sbi->sectors_written_start) >> 1; 1576 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); 1577 1578 if (__remain_node_summaries(cpc->reason)) { 1579 f2fs_write_node_summaries(sbi, start_blk); 1580 start_blk += NR_CURSEG_NODE_TYPE; 1581 } 1582 1583 /* Here, we have one bio having CP pack except cp pack 2 page */ 1584 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1585 /* Wait for all dirty meta pages to be submitted for IO */ 1586 f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META); 1587 1588 /* wait for previous submitted meta pages writeback */ 1589 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 1590 1591 /* flush all device cache */ 1592 err = f2fs_flush_device_cache(sbi); 1593 if (err) 1594 return err; 1595 1596 /* barrier and flush checkpoint cp pack 2 page if it can */ 1597 commit_checkpoint(sbi, ckpt, start_blk); 1598 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 1599 1600 /* 1601 * invalidate intermediate page cache borrowed from meta inode which are 1602 * used for migration of encrypted, verity or compressed inode's blocks. 1603 */ 1604 if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) || 1605 f2fs_sb_has_compression(sbi)) 1606 f2fs_bug_on(sbi, 1607 invalidate_inode_pages2_range(META_MAPPING(sbi), 1608 MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1)); 1609 1610 f2fs_release_ino_entry(sbi, false); 1611 1612 f2fs_reset_fsync_node_info(sbi); 1613 1614 clear_sbi_flag(sbi, SBI_IS_DIRTY); 1615 clear_sbi_flag(sbi, SBI_NEED_CP); 1616 clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1617 1618 spin_lock(&sbi->stat_lock); 1619 sbi->unusable_block_count = 0; 1620 spin_unlock(&sbi->stat_lock); 1621 1622 __set_cp_next_pack(sbi); 1623 1624 /* 1625 * redirty superblock if metadata like node page or inode cache is 1626 * updated during writing checkpoint. 1627 */ 1628 if (get_pages(sbi, F2FS_DIRTY_NODES) || 1629 get_pages(sbi, F2FS_DIRTY_IMETA)) 1630 set_sbi_flag(sbi, SBI_IS_DIRTY); 1631 1632 f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS)); 1633 1634 return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0; 1635 } 1636 1637 int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1638 { 1639 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1640 unsigned long long ckpt_ver; 1641 int err = 0; 1642 1643 if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi)) 1644 return -EROFS; 1645 1646 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 1647 if (cpc->reason != CP_PAUSE) 1648 return 0; 1649 f2fs_warn(sbi, "Start checkpoint disabled!"); 1650 } 1651 if (cpc->reason != CP_RESIZE) 1652 f2fs_down_write(&sbi->cp_global_sem); 1653 1654 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1655 ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) || 1656 ((cpc->reason & CP_DISCARD) && !sbi->discard_blks))) 1657 goto out; 1658 if (unlikely(f2fs_cp_error(sbi))) { 1659 err = -EIO; 1660 goto out; 1661 } 1662 1663 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); 1664 1665 err = block_operations(sbi); 1666 if (err) 1667 goto out; 1668 1669 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); 1670 1671 f2fs_flush_merged_writes(sbi); 1672 1673 /* this is the case of multiple fstrims without any changes */ 1674 if (cpc->reason & CP_DISCARD) { 1675 if (!f2fs_exist_trim_candidates(sbi, cpc)) { 1676 unblock_operations(sbi); 1677 goto out; 1678 } 1679 1680 if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 && 1681 SIT_I(sbi)->dirty_sentries == 0 && 1682 prefree_segments(sbi) == 0) { 1683 f2fs_flush_sit_entries(sbi, cpc); 1684 f2fs_clear_prefree_segments(sbi, cpc); 1685 unblock_operations(sbi); 1686 goto out; 1687 } 1688 } 1689 1690 /* 1691 * update checkpoint pack index 1692 * Increase the version number so that 1693 * SIT entries and seg summaries are written at correct place 1694 */ 1695 ckpt_ver = cur_cp_version(ckpt); 1696 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 1697 1698 /* write cached NAT/SIT entries to NAT/SIT area */ 1699 err = f2fs_flush_nat_entries(sbi, cpc); 1700 if (err) { 1701 f2fs_err(sbi, "f2fs_flush_nat_entries failed err:%d, stop checkpoint", err); 1702 f2fs_bug_on(sbi, !f2fs_cp_error(sbi)); 1703 goto stop; 1704 } 1705 1706 f2fs_flush_sit_entries(sbi, cpc); 1707 1708 /* save inmem log status */ 1709 f2fs_save_inmem_curseg(sbi); 1710 1711 err = do_checkpoint(sbi, cpc); 1712 if (err) { 1713 f2fs_err(sbi, "do_checkpoint failed err:%d, stop checkpoint", err); 1714 f2fs_bug_on(sbi, !f2fs_cp_error(sbi)); 1715 f2fs_release_discard_addrs(sbi); 1716 } else { 1717 f2fs_clear_prefree_segments(sbi, cpc); 1718 } 1719 1720 f2fs_restore_inmem_curseg(sbi); 1721 f2fs_reinit_atgc_curseg(sbi); 1722 stat_inc_cp_count(sbi); 1723 stop: 1724 unblock_operations(sbi); 1725 1726 if (cpc->reason & CP_RECOVERY) 1727 f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver); 1728 1729 /* update CP_TIME to trigger checkpoint periodically */ 1730 f2fs_update_time(sbi, CP_TIME); 1731 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 1732 out: 1733 if (cpc->reason != CP_RESIZE) 1734 f2fs_up_write(&sbi->cp_global_sem); 1735 return err; 1736 } 1737 1738 void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi) 1739 { 1740 int i; 1741 1742 for (i = 0; i < MAX_INO_ENTRY; i++) { 1743 struct inode_management *im = &sbi->im[i]; 1744 1745 INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); 1746 spin_lock_init(&im->ino_lock); 1747 INIT_LIST_HEAD(&im->ino_list); 1748 im->ino_num = 0; 1749 } 1750 1751 sbi->max_orphans = (BLKS_PER_SEG(sbi) - F2FS_CP_PACKS - 1752 NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) * 1753 F2FS_ORPHANS_PER_BLOCK; 1754 } 1755 1756 int __init f2fs_create_checkpoint_caches(void) 1757 { 1758 ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 1759 sizeof(struct ino_entry)); 1760 if (!ino_entry_slab) 1761 return -ENOMEM; 1762 f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", 1763 sizeof(struct inode_entry)); 1764 if (!f2fs_inode_entry_slab) { 1765 kmem_cache_destroy(ino_entry_slab); 1766 return -ENOMEM; 1767 } 1768 return 0; 1769 } 1770 1771 void f2fs_destroy_checkpoint_caches(void) 1772 { 1773 kmem_cache_destroy(ino_entry_slab); 1774 kmem_cache_destroy(f2fs_inode_entry_slab); 1775 } 1776 1777 static int __write_checkpoint_sync(struct f2fs_sb_info *sbi) 1778 { 1779 struct cp_control cpc = { .reason = CP_SYNC, }; 1780 int err; 1781 1782 f2fs_down_write(&sbi->gc_lock); 1783 err = f2fs_write_checkpoint(sbi, &cpc); 1784 f2fs_up_write(&sbi->gc_lock); 1785 1786 return err; 1787 } 1788 1789 static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi) 1790 { 1791 struct ckpt_req_control *cprc = &sbi->cprc_info; 1792 struct ckpt_req *req, *next; 1793 struct llist_node *dispatch_list; 1794 u64 sum_diff = 0, diff, count = 0; 1795 int ret; 1796 1797 dispatch_list = llist_del_all(&cprc->issue_list); 1798 if (!dispatch_list) 1799 return; 1800 dispatch_list = llist_reverse_order(dispatch_list); 1801 1802 ret = __write_checkpoint_sync(sbi); 1803 atomic_inc(&cprc->issued_ckpt); 1804 1805 llist_for_each_entry_safe(req, next, dispatch_list, llnode) { 1806 diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time); 1807 req->ret = ret; 1808 complete(&req->wait); 1809 1810 sum_diff += diff; 1811 count++; 1812 } 1813 atomic_sub(count, &cprc->queued_ckpt); 1814 atomic_add(count, &cprc->total_ckpt); 1815 1816 spin_lock(&cprc->stat_lock); 1817 cprc->cur_time = (unsigned int)div64_u64(sum_diff, count); 1818 if (cprc->peak_time < cprc->cur_time) 1819 cprc->peak_time = cprc->cur_time; 1820 spin_unlock(&cprc->stat_lock); 1821 } 1822 1823 static int issue_checkpoint_thread(void *data) 1824 { 1825 struct f2fs_sb_info *sbi = data; 1826 struct ckpt_req_control *cprc = &sbi->cprc_info; 1827 wait_queue_head_t *q = &cprc->ckpt_wait_queue; 1828 repeat: 1829 if (kthread_should_stop()) 1830 return 0; 1831 1832 if (!llist_empty(&cprc->issue_list)) 1833 __checkpoint_and_complete_reqs(sbi); 1834 1835 wait_event_interruptible(*q, 1836 kthread_should_stop() || !llist_empty(&cprc->issue_list)); 1837 goto repeat; 1838 } 1839 1840 static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi, 1841 struct ckpt_req *wait_req) 1842 { 1843 struct ckpt_req_control *cprc = &sbi->cprc_info; 1844 1845 if (!llist_empty(&cprc->issue_list)) { 1846 __checkpoint_and_complete_reqs(sbi); 1847 } else { 1848 /* already dispatched by issue_checkpoint_thread */ 1849 if (wait_req) 1850 wait_for_completion(&wait_req->wait); 1851 } 1852 } 1853 1854 static void init_ckpt_req(struct ckpt_req *req) 1855 { 1856 memset(req, 0, sizeof(struct ckpt_req)); 1857 1858 init_completion(&req->wait); 1859 req->queue_time = ktime_get(); 1860 } 1861 1862 int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi) 1863 { 1864 struct ckpt_req_control *cprc = &sbi->cprc_info; 1865 struct ckpt_req req; 1866 struct cp_control cpc; 1867 1868 cpc.reason = __get_cp_reason(sbi); 1869 if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) { 1870 int ret; 1871 1872 f2fs_down_write(&sbi->gc_lock); 1873 ret = f2fs_write_checkpoint(sbi, &cpc); 1874 f2fs_up_write(&sbi->gc_lock); 1875 1876 return ret; 1877 } 1878 1879 if (!cprc->f2fs_issue_ckpt) 1880 return __write_checkpoint_sync(sbi); 1881 1882 init_ckpt_req(&req); 1883 1884 llist_add(&req.llnode, &cprc->issue_list); 1885 atomic_inc(&cprc->queued_ckpt); 1886 1887 /* 1888 * update issue_list before we wake up issue_checkpoint thread, 1889 * this smp_mb() pairs with another barrier in ___wait_event(), 1890 * see more details in comments of waitqueue_active(). 1891 */ 1892 smp_mb(); 1893 1894 if (waitqueue_active(&cprc->ckpt_wait_queue)) 1895 wake_up(&cprc->ckpt_wait_queue); 1896 1897 if (cprc->f2fs_issue_ckpt) 1898 wait_for_completion(&req.wait); 1899 else 1900 flush_remained_ckpt_reqs(sbi, &req); 1901 1902 return req.ret; 1903 } 1904 1905 int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi) 1906 { 1907 dev_t dev = sbi->sb->s_bdev->bd_dev; 1908 struct ckpt_req_control *cprc = &sbi->cprc_info; 1909 1910 if (cprc->f2fs_issue_ckpt) 1911 return 0; 1912 1913 cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi, 1914 "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev)); 1915 if (IS_ERR(cprc->f2fs_issue_ckpt)) { 1916 int err = PTR_ERR(cprc->f2fs_issue_ckpt); 1917 1918 cprc->f2fs_issue_ckpt = NULL; 1919 return err; 1920 } 1921 1922 set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio); 1923 1924 return 0; 1925 } 1926 1927 void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi) 1928 { 1929 struct ckpt_req_control *cprc = &sbi->cprc_info; 1930 struct task_struct *ckpt_task; 1931 1932 if (!cprc->f2fs_issue_ckpt) 1933 return; 1934 1935 ckpt_task = cprc->f2fs_issue_ckpt; 1936 cprc->f2fs_issue_ckpt = NULL; 1937 kthread_stop(ckpt_task); 1938 1939 f2fs_flush_ckpt_thread(sbi); 1940 } 1941 1942 void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi) 1943 { 1944 struct ckpt_req_control *cprc = &sbi->cprc_info; 1945 1946 flush_remained_ckpt_reqs(sbi, NULL); 1947 1948 /* Let's wait for the previous dispatched checkpoint. */ 1949 while (atomic_read(&cprc->queued_ckpt)) 1950 io_schedule_timeout(DEFAULT_IO_TIMEOUT); 1951 } 1952 1953 void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi) 1954 { 1955 struct ckpt_req_control *cprc = &sbi->cprc_info; 1956 1957 atomic_set(&cprc->issued_ckpt, 0); 1958 atomic_set(&cprc->total_ckpt, 0); 1959 atomic_set(&cprc->queued_ckpt, 0); 1960 cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO; 1961 init_waitqueue_head(&cprc->ckpt_wait_queue); 1962 init_llist_head(&cprc->issue_list); 1963 spin_lock_init(&cprc->stat_lock); 1964 } 1965