Lines Matching full:sbi
171 bool f2fs_need_SSR(struct f2fs_sb_info *sbi) in f2fs_need_SSR() argument
173 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES); in f2fs_need_SSR()
174 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS); in f2fs_need_SSR()
175 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA); in f2fs_need_SSR()
177 if (f2fs_lfs_mode(sbi)) in f2fs_need_SSR()
179 if (sbi->gc_mode == GC_URGENT_HIGH) in f2fs_need_SSR()
181 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) in f2fs_need_SSR()
184 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs + in f2fs_need_SSR()
185 SM_I(sbi)->min_ssr_sections + reserved_sections(sbi)); in f2fs_need_SSR()
221 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); in __replace_atomic_write_block() local
237 err = f2fs_get_node_info(sbi, dn.nid, &ni, false); in __replace_atomic_write_block()
247 dec_valid_block_count(sbi, inode, 1); in __replace_atomic_write_block()
248 f2fs_invalidate_blocks(sbi, dn.data_blkaddr); in __replace_atomic_write_block()
251 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, in __replace_atomic_write_block()
257 err = inc_valid_block_count(sbi, inode, &count, true); in __replace_atomic_write_block()
265 dec_valid_block_count(sbi, F2FS_I(inode)->cow_inode, count); in __replace_atomic_write_block()
267 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr, in __replace_atomic_write_block()
304 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); in __f2fs_commit_atomic_write() local
339 } else if (!f2fs_is_valid_blkaddr(sbi, blkaddr, in __f2fs_commit_atomic_write()
369 sbi->revoked_atomic_block += fi->atomic_write_cnt; in __f2fs_commit_atomic_write()
371 sbi->committed_atomic_block += fi->atomic_write_cnt; in __f2fs_commit_atomic_write()
386 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); in f2fs_commit_atomic_write() local
395 f2fs_lock_op(sbi); in f2fs_commit_atomic_write()
399 f2fs_unlock_op(sbi); in f2fs_commit_atomic_write()
409 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need) in f2fs_balance_fs() argument
411 if (f2fs_cp_error(sbi)) in f2fs_balance_fs()
414 if (time_to_inject(sbi, FAULT_CHECKPOINT)) in f2fs_balance_fs()
415 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_FAULT_INJECT); in f2fs_balance_fs()
418 if (need && excess_cached_nats(sbi)) in f2fs_balance_fs()
419 f2fs_balance_fs_bg(sbi, false); in f2fs_balance_fs()
421 if (!f2fs_is_checkpoint_ready(sbi)) in f2fs_balance_fs()
428 if (has_enough_free_secs(sbi, 0, 0)) in f2fs_balance_fs()
431 if (test_opt(sbi, GC_MERGE) && sbi->gc_thread && in f2fs_balance_fs()
432 sbi->gc_thread->f2fs_gc_task) { in f2fs_balance_fs()
435 prepare_to_wait(&sbi->gc_thread->fggc_wq, &wait, in f2fs_balance_fs()
437 wake_up(&sbi->gc_thread->gc_wait_queue_head); in f2fs_balance_fs()
439 finish_wait(&sbi->gc_thread->fggc_wq, &wait); in f2fs_balance_fs()
448 f2fs_down_write(&sbi->gc_lock); in f2fs_balance_fs()
449 stat_inc_gc_call_count(sbi, FOREGROUND); in f2fs_balance_fs()
450 f2fs_gc(sbi, &gc_control); in f2fs_balance_fs()
454 static inline bool excess_dirty_threshold(struct f2fs_sb_info *sbi) in excess_dirty_threshold() argument
456 int factor = f2fs_rwsem_is_locked(&sbi->cp_rwsem) ? 3 : 2; in excess_dirty_threshold()
457 unsigned int dents = get_pages(sbi, F2FS_DIRTY_DENTS); in excess_dirty_threshold()
458 unsigned int qdata = get_pages(sbi, F2FS_DIRTY_QDATA); in excess_dirty_threshold()
459 unsigned int nodes = get_pages(sbi, F2FS_DIRTY_NODES); in excess_dirty_threshold()
460 unsigned int meta = get_pages(sbi, F2FS_DIRTY_META); in excess_dirty_threshold()
461 unsigned int imeta = get_pages(sbi, F2FS_DIRTY_IMETA); in excess_dirty_threshold()
463 SEGS_TO_BLKS(sbi, (factor * DEFAULT_DIRTY_THRESHOLD)); in excess_dirty_threshold()
473 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg) in f2fs_balance_fs_bg() argument
475 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) in f2fs_balance_fs_bg()
479 if (!f2fs_available_free_memory(sbi, READ_EXTENT_CACHE)) in f2fs_balance_fs_bg()
480 f2fs_shrink_read_extent_tree(sbi, in f2fs_balance_fs_bg()
484 if (!f2fs_available_free_memory(sbi, AGE_EXTENT_CACHE)) in f2fs_balance_fs_bg()
485 f2fs_shrink_age_extent_tree(sbi, in f2fs_balance_fs_bg()
489 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES)) in f2fs_balance_fs_bg()
490 f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK); in f2fs_balance_fs_bg()
492 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) in f2fs_balance_fs_bg()
493 f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS); in f2fs_balance_fs_bg()
495 f2fs_build_free_nids(sbi, false, false); in f2fs_balance_fs_bg()
497 if (excess_dirty_nats(sbi) || excess_dirty_threshold(sbi) || in f2fs_balance_fs_bg()
498 excess_prefree_segs(sbi) || !f2fs_space_for_roll_forward(sbi)) in f2fs_balance_fs_bg()
502 if (is_inflight_io(sbi, REQ_TIME) || in f2fs_balance_fs_bg()
503 (!f2fs_time_over(sbi, REQ_TIME) && f2fs_rwsem_is_locked(&sbi->cp_rwsem))) in f2fs_balance_fs_bg()
507 if (f2fs_time_over(sbi, CP_TIME)) in f2fs_balance_fs_bg()
511 if (f2fs_available_free_memory(sbi, NAT_ENTRIES) && in f2fs_balance_fs_bg()
512 f2fs_available_free_memory(sbi, INO_ENTRIES)) in f2fs_balance_fs_bg()
516 if (test_opt(sbi, DATA_FLUSH) && from_bg) { in f2fs_balance_fs_bg()
519 mutex_lock(&sbi->flush_lock); in f2fs_balance_fs_bg()
522 f2fs_sync_dirty_inodes(sbi, FILE_INODE, false); in f2fs_balance_fs_bg()
525 mutex_unlock(&sbi->flush_lock); in f2fs_balance_fs_bg()
527 stat_inc_cp_call_count(sbi, BACKGROUND); in f2fs_balance_fs_bg()
528 f2fs_sync_fs(sbi->sb, 1); in f2fs_balance_fs_bg()
531 static int __submit_flush_wait(struct f2fs_sb_info *sbi, in __submit_flush_wait() argument
536 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER), in __submit_flush_wait()
537 test_opt(sbi, FLUSH_MERGE), ret); in __submit_flush_wait()
539 f2fs_update_iostat(sbi, NULL, FS_FLUSH_IO, 0); in __submit_flush_wait()
543 static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino) in submit_flush_wait() argument
548 if (!f2fs_is_multi_device(sbi)) in submit_flush_wait()
549 return __submit_flush_wait(sbi, sbi->sb->s_bdev); in submit_flush_wait()
551 for (i = 0; i < sbi->s_ndevs; i++) { in submit_flush_wait()
552 if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO)) in submit_flush_wait()
554 ret = __submit_flush_wait(sbi, FDEV(i).bdev); in submit_flush_wait()
563 struct f2fs_sb_info *sbi = data; in issue_flush_thread() local
564 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; in issue_flush_thread()
579 ret = submit_flush_wait(sbi, cmd->ino); in issue_flush_thread()
595 int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino) in f2fs_issue_flush() argument
597 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; in f2fs_issue_flush()
601 if (test_opt(sbi, NOBARRIER)) in f2fs_issue_flush()
604 if (!test_opt(sbi, FLUSH_MERGE)) { in f2fs_issue_flush()
606 ret = submit_flush_wait(sbi, ino); in f2fs_issue_flush()
613 f2fs_is_multi_device(sbi)) { in f2fs_issue_flush()
614 ret = submit_flush_wait(sbi, ino); in f2fs_issue_flush()
649 ret = submit_flush_wait(sbi, ino); in f2fs_issue_flush()
666 int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi) in f2fs_create_flush_cmd_control() argument
668 dev_t dev = sbi->sb->s_bdev->bd_dev; in f2fs_create_flush_cmd_control()
671 if (SM_I(sbi)->fcc_info) { in f2fs_create_flush_cmd_control()
672 fcc = SM_I(sbi)->fcc_info; in f2fs_create_flush_cmd_control()
678 fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL); in f2fs_create_flush_cmd_control()
685 SM_I(sbi)->fcc_info = fcc; in f2fs_create_flush_cmd_control()
686 if (!test_opt(sbi, FLUSH_MERGE)) in f2fs_create_flush_cmd_control()
690 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi, in f2fs_create_flush_cmd_control()
702 void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free) in f2fs_destroy_flush_cmd_control() argument
704 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; in f2fs_destroy_flush_cmd_control()
714 SM_I(sbi)->fcc_info = NULL; in f2fs_destroy_flush_cmd_control()
718 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi) in f2fs_flush_device_cache() argument
722 if (!f2fs_is_multi_device(sbi)) in f2fs_flush_device_cache()
725 if (test_opt(sbi, NOBARRIER)) in f2fs_flush_device_cache()
728 for (i = 1; i < sbi->s_ndevs; i++) { in f2fs_flush_device_cache()
731 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device)) in f2fs_flush_device_cache()
735 ret = __submit_flush_wait(sbi, FDEV(i).bdev); in f2fs_flush_device_cache()
741 f2fs_stop_checkpoint(sbi, false, in f2fs_flush_device_cache()
746 spin_lock(&sbi->dev_lock); in f2fs_flush_device_cache()
747 f2fs_clear_bit(i, (char *)&sbi->dirty_device); in f2fs_flush_device_cache()
748 spin_unlock(&sbi->dev_lock); in f2fs_flush_device_cache()
754 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, in __locate_dirty_segment() argument
757 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in __locate_dirty_segment()
760 if (IS_CURSEG(sbi, segno)) in __locate_dirty_segment()
767 struct seg_entry *sentry = get_seg_entry(sbi, segno); in __locate_dirty_segment()
771 f2fs_bug_on(sbi, 1); in __locate_dirty_segment()
777 if (__is_large_section(sbi)) { in __locate_dirty_segment()
778 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno); in __locate_dirty_segment()
780 get_valid_blocks(sbi, segno, true); in __locate_dirty_segment()
782 f2fs_bug_on(sbi, in __locate_dirty_segment()
783 (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) && in __locate_dirty_segment()
785 valid_blocks == CAP_BLKS_PER_SEC(sbi)); in __locate_dirty_segment()
787 if (!IS_CURSEC(sbi, secno)) in __locate_dirty_segment()
793 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, in __remove_dirty_segment() argument
796 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in __remove_dirty_segment()
803 struct seg_entry *sentry = get_seg_entry(sbi, segno); in __remove_dirty_segment()
809 valid_blocks = get_valid_blocks(sbi, segno, true); in __remove_dirty_segment()
811 clear_bit(GET_SEC_FROM_SEG(sbi, segno), in __remove_dirty_segment()
814 clear_bit(segno, SIT_I(sbi)->invalid_segmap); in __remove_dirty_segment()
817 if (__is_large_section(sbi)) { in __remove_dirty_segment()
818 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno); in __remove_dirty_segment()
821 valid_blocks == CAP_BLKS_PER_SEC(sbi)) { in __remove_dirty_segment()
826 if (!IS_CURSEC(sbi, secno)) in __remove_dirty_segment()
837 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno) in locate_dirty_segment() argument
839 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in locate_dirty_segment()
843 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno)) in locate_dirty_segment()
846 usable_blocks = f2fs_usable_blks_in_seg(sbi, segno); in locate_dirty_segment()
849 valid_blocks = get_valid_blocks(sbi, segno, false); in locate_dirty_segment()
850 ckpt_valid_blocks = get_ckpt_valid_blocks(sbi, segno, false); in locate_dirty_segment()
852 if (valid_blocks == 0 && (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) || in locate_dirty_segment()
854 __locate_dirty_segment(sbi, segno, PRE); in locate_dirty_segment()
855 __remove_dirty_segment(sbi, segno, DIRTY); in locate_dirty_segment()
857 __locate_dirty_segment(sbi, segno, DIRTY); in locate_dirty_segment()
860 __remove_dirty_segment(sbi, segno, DIRTY); in locate_dirty_segment()
867 void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi) in f2fs_dirty_to_prefree() argument
869 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in f2fs_dirty_to_prefree()
873 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) { in f2fs_dirty_to_prefree()
874 if (get_valid_blocks(sbi, segno, false)) in f2fs_dirty_to_prefree()
876 if (IS_CURSEG(sbi, segno)) in f2fs_dirty_to_prefree()
878 __locate_dirty_segment(sbi, segno, PRE); in f2fs_dirty_to_prefree()
879 __remove_dirty_segment(sbi, segno, DIRTY); in f2fs_dirty_to_prefree()
884 block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi) in f2fs_get_unusable_blocks() argument
887 (overprovision_segments(sbi) - reserved_segments(sbi)); in f2fs_get_unusable_blocks()
888 block_t ovp_holes = SEGS_TO_BLKS(sbi, ovp_hole_segs); in f2fs_get_unusable_blocks()
889 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in f2fs_get_unusable_blocks()
896 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) { in f2fs_get_unusable_blocks()
897 se = get_seg_entry(sbi, segno); in f2fs_get_unusable_blocks()
899 holes[NODE] += f2fs_usable_blks_in_seg(sbi, segno) - in f2fs_get_unusable_blocks()
902 holes[DATA] += f2fs_usable_blks_in_seg(sbi, segno) - in f2fs_get_unusable_blocks()
913 int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable) in f2fs_disable_cp_again() argument
916 (overprovision_segments(sbi) - reserved_segments(sbi)); in f2fs_disable_cp_again()
918 if (F2FS_OPTION(sbi).unusable_cap_perc == 100) in f2fs_disable_cp_again()
920 if (unusable > F2FS_OPTION(sbi).unusable_cap) in f2fs_disable_cp_again()
922 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK) && in f2fs_disable_cp_again()
923 dirty_segments(sbi) > ovp_hole_segs) in f2fs_disable_cp_again()
925 if (has_not_enough_free_secs(sbi, 0, 0)) in f2fs_disable_cp_again()
931 static unsigned int get_free_segment(struct f2fs_sb_info *sbi) in get_free_segment() argument
933 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in get_free_segment()
937 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) { in get_free_segment()
938 if (get_valid_blocks(sbi, segno, false)) in get_free_segment()
940 if (get_ckpt_valid_blocks(sbi, segno, false)) in get_free_segment()
949 static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi, in __create_discard_cmd() argument
953 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __create_discard_cmd()
957 f2fs_bug_on(sbi, !len); in __create_discard_cmd()
981 static bool f2fs_check_discard_tree(struct f2fs_sb_info *sbi) in f2fs_check_discard_tree() argument
984 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in f2fs_check_discard_tree()
997 f2fs_info(sbi, "broken discard_rbtree, " in f2fs_check_discard_tree()
1009 static struct discard_cmd *__lookup_discard_cmd(struct f2fs_sb_info *sbi, in __lookup_discard_cmd() argument
1012 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __lookup_discard_cmd()
1101 static void __remove_discard_cmd(struct f2fs_sb_info *sbi, in __remove_discard_cmd() argument
1104 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __remove_discard_cmd()
1116 f2fs_bug_on(sbi, dc->ref); in __remove_discard_cmd()
1122 f2fs_info_ratelimited(sbi, in __remove_discard_cmd()
1145 static void __check_sit_bitmap(struct f2fs_sb_info *sbi, in __check_sit_bitmap() argument
1155 segno = GET_SEGNO(sbi, blk); in __check_sit_bitmap()
1156 sentry = get_seg_entry(sbi, segno); in __check_sit_bitmap()
1157 offset = GET_BLKOFF_FROM_SEG0(sbi, blk); in __check_sit_bitmap()
1159 if (end < START_BLOCK(sbi, segno + 1)) in __check_sit_bitmap()
1160 size = GET_BLKOFF_FROM_SEG0(sbi, end); in __check_sit_bitmap()
1162 size = BLKS_PER_SEG(sbi); in __check_sit_bitmap()
1165 f2fs_bug_on(sbi, offset != size); in __check_sit_bitmap()
1166 blk = START_BLOCK(sbi, segno + 1); in __check_sit_bitmap()
1171 static void __init_discard_policy(struct f2fs_sb_info *sbi, in __init_discard_policy() argument
1175 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __init_discard_policy()
1197 if (utilization(sbi) > dcc->discard_urgent_util) { in __init_discard_policy()
1218 static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1223 static void __submit_zone_reset_cmd(struct f2fs_sb_info *sbi, in __submit_zone_reset_cmd() argument
1228 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __submit_zone_reset_cmd()
1248 __check_sit_bitmap(sbi, dc->di.lstart, dc->di.lstart + dc->di.len); in __submit_zone_reset_cmd()
1256 f2fs_update_iostat(sbi, NULL, FS_ZONE_RESET_IO, dc->di.len * F2FS_BLKSIZE); in __submit_zone_reset_cmd()
1261 static int __submit_discard_cmd(struct f2fs_sb_info *sbi, in __submit_discard_cmd() argument
1268 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __submit_discard_cmd()
1278 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) in __submit_discard_cmd()
1282 if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev)) { in __submit_discard_cmd()
1283 int devi = f2fs_bdev_index(sbi, bdev); in __submit_discard_cmd()
1288 if (f2fs_blkz_is_seq(sbi, devi, dc->di.start)) { in __submit_discard_cmd()
1289 __submit_zone_reset_cmd(sbi, dc, flag, in __submit_discard_cmd()
1328 if (time_to_inject(sbi, FAULT_DISCARD)) { in __submit_discard_cmd()
1345 f2fs_bug_on(sbi, !bio); in __submit_discard_cmd()
1364 __check_sit_bitmap(sbi, lstart, lstart + len); in __submit_discard_cmd()
1373 f2fs_update_iostat(sbi, NULL, FS_DISCARD_IO, len * F2FS_BLKSIZE); in __submit_discard_cmd()
1383 __update_discard_tree_range(sbi, bdev, lstart, start, len); in __submit_discard_cmd()
1388 static void __insert_discard_cmd(struct f2fs_sb_info *sbi, in __insert_discard_cmd() argument
1392 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __insert_discard_cmd()
1414 dc = __create_discard_cmd(sbi, bdev, lstart, start, len); in __insert_discard_cmd()
1426 static void __punch_discard_cmd(struct f2fs_sb_info *sbi, in __punch_discard_cmd() argument
1429 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __punch_discard_cmd()
1434 __remove_discard_cmd(sbi, dc); in __punch_discard_cmd()
1449 __insert_discard_cmd(sbi, dc->bdev, blkaddr + 1, in __punch_discard_cmd()
1462 static void __update_discard_tree_range(struct f2fs_sb_info *sbi, in __update_discard_tree_range() argument
1466 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __update_discard_tree_range()
1531 __remove_discard_cmd(sbi, tdc); in __update_discard_tree_range()
1536 __insert_discard_cmd(sbi, bdev, in __update_discard_tree_range()
1549 static void __queue_zone_reset_cmd(struct f2fs_sb_info *sbi, in __queue_zone_reset_cmd() argument
1555 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock); in __queue_zone_reset_cmd()
1556 __insert_discard_cmd(sbi, bdev, lblkstart, blkstart, blklen); in __queue_zone_reset_cmd()
1557 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock); in __queue_zone_reset_cmd()
1561 static void __queue_discard_cmd(struct f2fs_sb_info *sbi, in __queue_discard_cmd() argument
1571 if (f2fs_is_multi_device(sbi)) { in __queue_discard_cmd()
1572 int devi = f2fs_target_device_index(sbi, blkstart); in __queue_discard_cmd()
1576 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock); in __queue_discard_cmd()
1577 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen); in __queue_discard_cmd()
1578 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock); in __queue_discard_cmd()
1581 static void __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi, in __issue_discard_cmd_orderly() argument
1584 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __issue_discard_cmd_orderly()
1606 if (dpolicy->io_aware && !is_idle(sbi, DISCARD_TIME)) { in __issue_discard_cmd_orderly()
1612 err = __submit_discard_cmd(sbi, dpolicy, dc, issued); in __issue_discard_cmd_orderly()
1619 __remove_discard_cmd(sbi, dc); in __issue_discard_cmd_orderly()
1633 static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1636 static int __issue_discard_cmd(struct f2fs_sb_info *sbi, in __issue_discard_cmd() argument
1639 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __issue_discard_cmd()
1647 f2fs_update_time(sbi, UMOUNT_DISCARD_TIMEOUT); in __issue_discard_cmd()
1653 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT)) in __issue_discard_cmd()
1660 __issue_discard_cmd_orderly(sbi, dpolicy, &issued); in __issue_discard_cmd()
1670 f2fs_bug_on(sbi, !f2fs_check_discard_tree(sbi)); in __issue_discard_cmd()
1673 f2fs_bug_on(sbi, dc->state != D_PREP); in __issue_discard_cmd()
1676 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT)) in __issue_discard_cmd()
1680 !is_idle(sbi, DISCARD_TIME)) { in __issue_discard_cmd()
1685 __submit_discard_cmd(sbi, dpolicy, dc, &issued); in __issue_discard_cmd()
1699 __wait_all_discard_cmd(sbi, dpolicy); in __issue_discard_cmd()
1709 static bool __drop_discard_cmd(struct f2fs_sb_info *sbi) in __drop_discard_cmd() argument
1711 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __drop_discard_cmd()
1721 f2fs_bug_on(sbi, dc->state != D_PREP); in __drop_discard_cmd()
1722 __remove_discard_cmd(sbi, dc); in __drop_discard_cmd()
1731 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi) in f2fs_drop_discard_cmd() argument
1733 __drop_discard_cmd(sbi); in f2fs_drop_discard_cmd()
1736 static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi, in __wait_one_discard_bio() argument
1739 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __wait_one_discard_bio()
1744 f2fs_bug_on(sbi, dc->state != D_DONE); in __wait_one_discard_bio()
1749 __remove_discard_cmd(sbi, dc); in __wait_one_discard_bio()
1756 static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi, in __wait_discard_cmd_range() argument
1760 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __wait_discard_cmd_range()
1780 __remove_discard_cmd(sbi, iter); in __wait_discard_cmd_range()
1790 trimmed += __wait_one_discard_bio(sbi, dc); in __wait_discard_cmd_range()
1797 static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi, in __wait_all_discard_cmd() argument
1804 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX); in __wait_all_discard_cmd()
1807 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, MIN_DISCARD_GRANULARITY); in __wait_all_discard_cmd()
1808 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX); in __wait_all_discard_cmd()
1809 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, MIN_DISCARD_GRANULARITY); in __wait_all_discard_cmd()
1810 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX); in __wait_all_discard_cmd()
1816 static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr) in f2fs_wait_discard_bio() argument
1818 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in f2fs_wait_discard_bio()
1823 dc = __lookup_discard_cmd(sbi, blkaddr); in f2fs_wait_discard_bio()
1825 if (dc && f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(dc->bdev)) { in f2fs_wait_discard_bio()
1826 int devi = f2fs_bdev_index(sbi, dc->bdev); in f2fs_wait_discard_bio()
1833 if (f2fs_blkz_is_seq(sbi, devi, dc->di.start)) { in f2fs_wait_discard_bio()
1836 __submit_zone_reset_cmd(sbi, dc, REQ_SYNC, in f2fs_wait_discard_bio()
1841 __wait_one_discard_bio(sbi, dc); in f2fs_wait_discard_bio()
1848 __punch_discard_cmd(sbi, dc, blkaddr); in f2fs_wait_discard_bio()
1857 __wait_one_discard_bio(sbi, dc); in f2fs_wait_discard_bio()
1860 void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi) in f2fs_stop_discard_thread() argument
1862 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in f2fs_stop_discard_thread()
1874 * @sbi: the f2fs_sb_info data for discard cmd to issue
1880 bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi) in f2fs_issue_discard_timeout() argument
1882 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in f2fs_issue_discard_timeout()
1889 __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT, in f2fs_issue_discard_timeout()
1891 __issue_discard_cmd(sbi, &dpolicy); in f2fs_issue_discard_timeout()
1892 dropped = __drop_discard_cmd(sbi); in f2fs_issue_discard_timeout()
1895 __wait_all_discard_cmd(sbi, NULL); in f2fs_issue_discard_timeout()
1897 f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt)); in f2fs_issue_discard_timeout()
1903 struct f2fs_sb_info *sbi = data; in issue_discard_thread() local
1904 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in issue_discard_thread()
1917 if (sbi->gc_mode == GC_URGENT_HIGH || in issue_discard_thread()
1918 !f2fs_available_free_memory(sbi, DISCARD_CACHE)) in issue_discard_thread()
1919 __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, in issue_discard_thread()
1922 __init_discard_policy(sbi, &dpolicy, DPOLICY_BG, in issue_discard_thread()
1930 __wait_all_discard_cmd(sbi, NULL); in issue_discard_thread()
1932 if (f2fs_readonly(sbi->sb)) in issue_discard_thread()
1936 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK) || in issue_discard_thread()
1942 sb_start_intwrite(sbi->sb); in issue_discard_thread()
1944 issued = __issue_discard_cmd(sbi, &dpolicy); in issue_discard_thread()
1946 __wait_all_discard_cmd(sbi, &dpolicy); in issue_discard_thread()
1949 wait_ms = f2fs_time_to_wait(sbi, DISCARD_TIME); in issue_discard_thread()
1958 sb_end_intwrite(sbi->sb); in issue_discard_thread()
1965 static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi, in __f2fs_issue_discard_zone() argument
1973 if (f2fs_is_multi_device(sbi)) { in __f2fs_issue_discard_zone()
1974 devi = f2fs_target_device_index(sbi, blkstart); in __f2fs_issue_discard_zone()
1977 f2fs_err(sbi, "Invalid block %x", blkstart); in __f2fs_issue_discard_zone()
1984 if (f2fs_blkz_is_seq(sbi, devi, blkstart)) { in __f2fs_issue_discard_zone()
1990 f2fs_err(sbi, "(%d) %s: Unaligned zone reset attempted (block %x + %x)", in __f2fs_issue_discard_zone()
1991 devi, sbi->s_ndevs ? FDEV(devi).path : "", in __f2fs_issue_discard_zone()
1996 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) { in __f2fs_issue_discard_zone()
2008 __queue_zone_reset_cmd(sbi, bdev, blkstart, lblkstart, blklen); in __f2fs_issue_discard_zone()
2013 __queue_discard_cmd(sbi, bdev, lblkstart, blklen); in __f2fs_issue_discard_zone()
2018 static int __issue_discard_async(struct f2fs_sb_info *sbi, in __issue_discard_async() argument
2022 if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev)) in __issue_discard_async()
2023 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen); in __issue_discard_async()
2025 __queue_discard_cmd(sbi, bdev, blkstart, blklen); in __issue_discard_async()
2029 static int f2fs_issue_discard(struct f2fs_sb_info *sbi, in f2fs_issue_discard() argument
2039 bdev = f2fs_target_device(sbi, blkstart, NULL); in f2fs_issue_discard()
2044 f2fs_target_device(sbi, i, NULL); in f2fs_issue_discard()
2047 err = __issue_discard_async(sbi, bdev, in f2fs_issue_discard()
2057 se = get_seg_entry(sbi, GET_SEGNO(sbi, i)); in f2fs_issue_discard()
2058 offset = GET_BLKOFF_FROM_SEG0(sbi, i); in f2fs_issue_discard()
2060 if (f2fs_block_unit_discard(sbi) && in f2fs_issue_discard()
2062 sbi->discard_blks--; in f2fs_issue_discard()
2066 err = __issue_discard_async(sbi, bdev, start, len); in f2fs_issue_discard()
2070 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc, in add_discard_addrs() argument
2074 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start); in add_discard_addrs()
2078 unsigned long *dmap = SIT_I(sbi)->tmp_map; in add_discard_addrs()
2082 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list; in add_discard_addrs()
2085 if (se->valid_blocks == BLKS_PER_SEG(sbi) || in add_discard_addrs()
2086 !f2fs_hw_support_discard(sbi) || in add_discard_addrs()
2087 !f2fs_block_unit_discard(sbi)) in add_discard_addrs()
2091 if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks || in add_discard_addrs()
2092 SM_I(sbi)->dcc_info->nr_discards >= in add_discard_addrs()
2093 SM_I(sbi)->dcc_info->max_discards) in add_discard_addrs()
2102 while (force || SM_I(sbi)->dcc_info->nr_discards <= in add_discard_addrs()
2103 SM_I(sbi)->dcc_info->max_discards) { in add_discard_addrs()
2104 start = __find_rev_next_bit(dmap, BLKS_PER_SEG(sbi), end + 1); in add_discard_addrs()
2105 if (start >= BLKS_PER_SEG(sbi)) in add_discard_addrs()
2109 BLKS_PER_SEG(sbi), start + 1); in add_discard_addrs()
2110 if (force && start && end != BLKS_PER_SEG(sbi) && in add_discard_addrs()
2120 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start); in add_discard_addrs()
2127 SM_I(sbi)->dcc_info->nr_discards += end - start; in add_discard_addrs()
2138 void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi) in f2fs_release_discard_addrs() argument
2140 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list); in f2fs_release_discard_addrs()
2151 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi) in set_prefree_as_free_segments() argument
2153 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in set_prefree_as_free_segments()
2157 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi)) in set_prefree_as_free_segments()
2158 __set_test_and_free(sbi, segno, false); in set_prefree_as_free_segments()
2162 void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi, in f2fs_clear_prefree_segments() argument
2165 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in f2fs_clear_prefree_segments()
2168 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in f2fs_clear_prefree_segments()
2173 bool section_alignment = F2FS_OPTION(sbi).discard_unit == in f2fs_clear_prefree_segments()
2176 if (f2fs_lfs_mode(sbi) && __is_large_section(sbi)) in f2fs_clear_prefree_segments()
2186 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1); in f2fs_clear_prefree_segments()
2187 if (start >= MAIN_SEGS(sbi)) in f2fs_clear_prefree_segments()
2189 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi), in f2fs_clear_prefree_segments()
2193 start = rounddown(start, SEGS_PER_SEC(sbi)); in f2fs_clear_prefree_segments()
2194 end = roundup(end, SEGS_PER_SEC(sbi)); in f2fs_clear_prefree_segments()
2202 if (!f2fs_realtime_discard_enable(sbi)) in f2fs_clear_prefree_segments()
2210 if (!f2fs_sb_has_blkzoned(sbi) && in f2fs_clear_prefree_segments()
2211 (!f2fs_lfs_mode(sbi) || !__is_large_section(sbi))) { in f2fs_clear_prefree_segments()
2212 f2fs_issue_discard(sbi, START_BLOCK(sbi, start), in f2fs_clear_prefree_segments()
2213 SEGS_TO_BLKS(sbi, end - start)); in f2fs_clear_prefree_segments()
2217 secno = GET_SEC_FROM_SEG(sbi, start); in f2fs_clear_prefree_segments()
2218 start_segno = GET_SEG_FROM_SEC(sbi, secno); in f2fs_clear_prefree_segments()
2219 if (!IS_CURSEC(sbi, secno) && in f2fs_clear_prefree_segments()
2220 !get_valid_blocks(sbi, start, true)) in f2fs_clear_prefree_segments()
2221 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno), in f2fs_clear_prefree_segments()
2222 BLKS_PER_SEC(sbi)); in f2fs_clear_prefree_segments()
2224 start = start_segno + SEGS_PER_SEC(sbi); in f2fs_clear_prefree_segments()
2232 if (!f2fs_block_unit_discard(sbi)) in f2fs_clear_prefree_segments()
2243 BLKS_PER_SEG(sbi), cur_pos); in f2fs_clear_prefree_segments()
2246 if (f2fs_sb_has_blkzoned(sbi) || in f2fs_clear_prefree_segments()
2250 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos, in f2fs_clear_prefree_segments()
2255 BLKS_PER_SEG(sbi), cur_pos); in f2fs_clear_prefree_segments()
2261 if (cur_pos < BLKS_PER_SEG(sbi)) in f2fs_clear_prefree_segments()
2269 wake_up_discard_thread(sbi, false); in f2fs_clear_prefree_segments()
2272 int f2fs_start_discard_thread(struct f2fs_sb_info *sbi) in f2fs_start_discard_thread() argument
2274 dev_t dev = sbi->sb->s_bdev->bd_dev; in f2fs_start_discard_thread()
2275 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in f2fs_start_discard_thread()
2278 if (f2fs_sb_has_readonly(sbi)) { in f2fs_start_discard_thread()
2279 f2fs_info(sbi, in f2fs_start_discard_thread()
2284 if (!f2fs_realtime_discard_enable(sbi)) in f2fs_start_discard_thread()
2287 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi, in f2fs_start_discard_thread()
2297 static int create_discard_cmd_control(struct f2fs_sb_info *sbi) in create_discard_cmd_control() argument
2302 if (SM_I(sbi)->dcc_info) { in create_discard_cmd_control()
2303 dcc = SM_I(sbi)->dcc_info; in create_discard_cmd_control()
2307 dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL); in create_discard_cmd_control()
2315 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT) in create_discard_cmd_control()
2316 dcc->discard_granularity = BLKS_PER_SEG(sbi); in create_discard_cmd_control()
2317 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION) in create_discard_cmd_control()
2318 dcc->discard_granularity = BLKS_PER_SEC(sbi); in create_discard_cmd_control()
2330 dcc->max_discards = SEGS_TO_BLKS(sbi, MAIN_SEGS(sbi)); in create_discard_cmd_control()
2342 SM_I(sbi)->dcc_info = dcc; in create_discard_cmd_control()
2344 err = f2fs_start_discard_thread(sbi); in create_discard_cmd_control()
2347 SM_I(sbi)->dcc_info = NULL; in create_discard_cmd_control()
2353 static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi) in destroy_discard_cmd_control() argument
2355 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in destroy_discard_cmd_control()
2360 f2fs_stop_discard_thread(sbi); in destroy_discard_cmd_control()
2366 f2fs_issue_discard_timeout(sbi); in destroy_discard_cmd_control()
2369 SM_I(sbi)->dcc_info = NULL; in destroy_discard_cmd_control()
2372 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno) in __mark_sit_entry_dirty() argument
2374 struct sit_info *sit_i = SIT_I(sbi); in __mark_sit_entry_dirty()
2384 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type, in __set_sit_entry_type() argument
2387 struct seg_entry *se = get_seg_entry(sbi, segno); in __set_sit_entry_type()
2391 __mark_sit_entry_dirty(sbi, segno); in __set_sit_entry_type()
2394 static inline unsigned long long get_segment_mtime(struct f2fs_sb_info *sbi, in get_segment_mtime() argument
2397 unsigned int segno = GET_SEGNO(sbi, blkaddr); in get_segment_mtime()
2401 return get_seg_entry(sbi, segno)->mtime; in get_segment_mtime()
2404 static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr, in update_segment_mtime() argument
2408 unsigned int segno = GET_SEGNO(sbi, blkaddr); in update_segment_mtime()
2409 unsigned long long ctime = get_mtime(sbi, false); in update_segment_mtime()
2415 se = get_seg_entry(sbi, segno); in update_segment_mtime()
2423 if (ctime > SIT_I(sbi)->max_mtime) in update_segment_mtime()
2424 SIT_I(sbi)->max_mtime = ctime; in update_segment_mtime()
2427 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del) in update_sit_entry() argument
2437 segno = GET_SEGNO(sbi, blkaddr); in update_sit_entry()
2441 se = get_seg_entry(sbi, segno); in update_sit_entry()
2443 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); in update_sit_entry()
2445 f2fs_bug_on(sbi, (new_vblocks < 0 || in update_sit_entry()
2446 (new_vblocks > f2fs_usable_blks_in_seg(sbi, segno)))); in update_sit_entry()
2457 f2fs_err(sbi, "Inconsistent error when setting bitmap, blk:%u, old bit:%d", in update_sit_entry()
2459 f2fs_bug_on(sbi, 1); in update_sit_entry()
2463 f2fs_err(sbi, "Bitmap was wrongly set, blk:%u", in update_sit_entry()
2465 f2fs_bug_on(sbi, 1); in update_sit_entry()
2470 if (f2fs_block_unit_discard(sbi) && in update_sit_entry()
2472 sbi->discard_blks--; in update_sit_entry()
2478 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) { in update_sit_entry()
2488 f2fs_err(sbi, "Inconsistent error when clearing bitmap, blk:%u, old bit:%d", in update_sit_entry()
2490 f2fs_bug_on(sbi, 1); in update_sit_entry()
2494 f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u", in update_sit_entry()
2496 f2fs_bug_on(sbi, 1); in update_sit_entry()
2499 } else if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { in update_sit_entry()
2507 spin_lock(&sbi->stat_lock); in update_sit_entry()
2508 sbi->unusable_block_count++; in update_sit_entry()
2509 spin_unlock(&sbi->stat_lock); in update_sit_entry()
2513 if (f2fs_block_unit_discard(sbi) && in update_sit_entry()
2515 sbi->discard_blks++; in update_sit_entry()
2520 __mark_sit_entry_dirty(sbi, segno); in update_sit_entry()
2523 SIT_I(sbi)->written_valid_blocks += del; in update_sit_entry()
2525 if (__is_large_section(sbi)) in update_sit_entry()
2526 get_sec_entry(sbi, segno)->valid_blocks += del; in update_sit_entry()
2529 void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr) in f2fs_invalidate_blocks() argument
2531 unsigned int segno = GET_SEGNO(sbi, addr); in f2fs_invalidate_blocks()
2532 struct sit_info *sit_i = SIT_I(sbi); in f2fs_invalidate_blocks()
2534 f2fs_bug_on(sbi, addr == NULL_ADDR); in f2fs_invalidate_blocks()
2538 f2fs_invalidate_internal_cache(sbi, addr); in f2fs_invalidate_blocks()
2543 update_segment_mtime(sbi, addr, 0); in f2fs_invalidate_blocks()
2544 update_sit_entry(sbi, addr, -1); in f2fs_invalidate_blocks()
2547 locate_dirty_segment(sbi, segno); in f2fs_invalidate_blocks()
2552 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr) in f2fs_is_checkpointed_data() argument
2554 struct sit_info *sit_i = SIT_I(sbi); in f2fs_is_checkpointed_data()
2564 segno = GET_SEGNO(sbi, blkaddr); in f2fs_is_checkpointed_data()
2565 se = get_seg_entry(sbi, segno); in f2fs_is_checkpointed_data()
2566 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); in f2fs_is_checkpointed_data()
2576 static unsigned short f2fs_curseg_valid_blocks(struct f2fs_sb_info *sbi, int type) in f2fs_curseg_valid_blocks() argument
2578 struct curseg_info *curseg = CURSEG_I(sbi, type); in f2fs_curseg_valid_blocks()
2580 if (sbi->ckpt->alloc_type[type] == SSR) in f2fs_curseg_valid_blocks()
2581 return BLKS_PER_SEG(sbi); in f2fs_curseg_valid_blocks()
2588 int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra) in f2fs_npages_for_summary_flush() argument
2594 if (sbi->ckpt->alloc_type[i] != SSR && for_ra) in f2fs_npages_for_summary_flush()
2596 le16_to_cpu(F2FS_CKPT(sbi)->cur_data_blkoff[i]); in f2fs_npages_for_summary_flush()
2598 valid_sum_count += f2fs_curseg_valid_blocks(sbi, i); in f2fs_npages_for_summary_flush()
2614 struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno) in f2fs_get_sum_page() argument
2616 if (unlikely(f2fs_cp_error(sbi))) in f2fs_get_sum_page()
2618 return f2fs_get_meta_page_retry(sbi, GET_SUM_BLOCK(sbi, segno)); in f2fs_get_sum_page()
2621 void f2fs_update_meta_page(struct f2fs_sb_info *sbi, in f2fs_update_meta_page() argument
2624 struct page *page = f2fs_grab_meta_page(sbi, blk_addr); in f2fs_update_meta_page()
2631 static void write_sum_page(struct f2fs_sb_info *sbi, in write_sum_page() argument
2634 f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr); in write_sum_page()
2637 static void write_current_sum_page(struct f2fs_sb_info *sbi, in write_current_sum_page() argument
2640 struct curseg_info *curseg = CURSEG_I(sbi, type); in write_current_sum_page()
2641 struct page *page = f2fs_grab_meta_page(sbi, blk_addr); in write_current_sum_page()
2663 static int is_next_segment_free(struct f2fs_sb_info *sbi, in is_next_segment_free() argument
2667 struct free_segmap_info *free_i = FREE_I(sbi); in is_next_segment_free()
2669 if (segno < MAIN_SEGS(sbi) && segno % SEGS_PER_SEC(sbi)) in is_next_segment_free()
2678 static int get_new_segment(struct f2fs_sb_info *sbi, in get_new_segment() argument
2681 struct free_segmap_info *free_i = FREE_I(sbi); in get_new_segment()
2683 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone; in get_new_segment()
2684 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg); in get_new_segment()
2685 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg); in get_new_segment()
2692 if (time_to_inject(sbi, FAULT_NO_SEGMENT)) { in get_new_segment()
2697 if (!new_sec && ((*newseg + 1) % SEGS_PER_SEC(sbi))) { in get_new_segment()
2699 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1); in get_new_segment()
2700 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1)) in get_new_segment()
2709 if (f2fs_sb_has_blkzoned(sbi)) { in get_new_segment()
2711 if (sbi->blkzone_alloc_policy == BLKZONE_ALLOC_PRIOR_CONV || pinning) in get_new_segment()
2714 segno = max(first_zoned_segno(sbi), *newseg); in get_new_segment()
2715 hint = GET_SEC_FROM_SEG(sbi, segno); in get_new_segment()
2720 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint); in get_new_segment()
2723 if (secno >= MAIN_SECS(sbi) && f2fs_sb_has_blkzoned(sbi)) { in get_new_segment()
2725 if (sbi->blkzone_alloc_policy == BLKZONE_ALLOC_ONLY_SEQ) { in get_new_segment()
2726 hint = GET_SEC_FROM_SEG(sbi, first_zoned_segno(sbi)); in get_new_segment()
2727 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint); in get_new_segment()
2730 MAIN_SECS(sbi)); in get_new_segment()
2731 if (secno >= MAIN_SECS(sbi)) { in get_new_segment()
2733 f2fs_bug_on(sbi, 1); in get_new_segment()
2739 if (secno >= MAIN_SECS(sbi)) { in get_new_segment()
2741 MAIN_SECS(sbi)); in get_new_segment()
2742 if (secno >= MAIN_SECS(sbi)) { in get_new_segment()
2744 f2fs_bug_on(sbi, 1); in get_new_segment()
2748 segno = GET_SEG_FROM_SEC(sbi, secno); in get_new_segment()
2749 zoneno = GET_ZONE_FROM_SEC(sbi, secno); in get_new_segment()
2754 if (sbi->secs_per_zone == 1) in get_new_segment()
2759 if (CURSEG_I(sbi, i)->zone == zoneno) in get_new_segment()
2767 hint = (zoneno + 1) * sbi->secs_per_zone; in get_new_segment()
2773 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap)); in get_new_segment()
2777 !f2fs_valid_pinned_area(sbi, START_BLOCK(sbi, segno))) { in get_new_segment()
2781 __set_inuse(sbi, segno); in get_new_segment()
2787 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_NO_SEGMENT); in get_new_segment()
2791 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified) in reset_curseg() argument
2793 struct curseg_info *curseg = CURSEG_I(sbi, type); in reset_curseg()
2803 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno); in reset_curseg()
2810 sanity_check_seg_type(sbi, seg_type); in reset_curseg()
2816 __set_sit_entry_type(sbi, seg_type, curseg->segno, modified); in reset_curseg()
2819 static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type) in __get_next_segno() argument
2821 struct curseg_info *curseg = CURSEG_I(sbi, type); in __get_next_segno()
2824 sanity_check_seg_type(sbi, seg_type); in __get_next_segno()
2825 if (__is_large_section(sbi)) { in __get_next_segno()
2826 if (f2fs_need_rand_seg(sbi)) { in __get_next_segno()
2827 unsigned int hint = GET_SEC_FROM_SEG(sbi, curseg->segno); in __get_next_segno()
2829 if (GET_SEC_FROM_SEG(sbi, curseg->segno + 1) != hint) in __get_next_segno()
2832 GET_SEG_FROM_SEC(sbi, hint + 1) - 1); in __get_next_segno()
2835 } else if (f2fs_need_rand_seg(sbi)) { in __get_next_segno()
2836 return get_random_u32_below(MAIN_SECS(sbi) * SEGS_PER_SEC(sbi)); in __get_next_segno()
2843 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) in __get_next_segno()
2849 if (SIT_I(sbi)->last_victim[ALLOC_NEXT]) in __get_next_segno()
2850 return SIT_I(sbi)->last_victim[ALLOC_NEXT]; in __get_next_segno()
2853 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE) in __get_next_segno()
2863 static int new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) in new_curseg() argument
2865 struct curseg_info *curseg = CURSEG_I(sbi, type); in new_curseg()
2871 write_sum_page(sbi, curseg->sum_blk, GET_SUM_BLOCK(sbi, segno)); in new_curseg()
2873 segno = __get_next_segno(sbi, type); in new_curseg()
2874 ret = get_new_segment(sbi, &segno, new_sec, pinning); in new_curseg()
2882 reset_curseg(sbi, type, 1); in new_curseg()
2884 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) in new_curseg()
2886 get_random_u32_inclusive(1, sbi->max_fragment_chunk); in new_curseg()
2890 static int __next_free_blkoff(struct f2fs_sb_info *sbi, in __next_free_blkoff() argument
2893 struct seg_entry *se = get_seg_entry(sbi, segno); in __next_free_blkoff()
2895 unsigned long *target_map = SIT_I(sbi)->tmp_map; in __next_free_blkoff()
2903 return __find_rev_next_zero_bit(target_map, BLKS_PER_SEG(sbi), start); in __next_free_blkoff()
2906 static int f2fs_find_next_ssr_block(struct f2fs_sb_info *sbi, in f2fs_find_next_ssr_block() argument
2909 return __next_free_blkoff(sbi, seg->segno, seg->next_blkoff + 1); in f2fs_find_next_ssr_block()
2912 bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno) in f2fs_segment_has_free_slot() argument
2914 return __next_free_blkoff(sbi, segno, 0) < BLKS_PER_SEG(sbi); in f2fs_segment_has_free_slot()
2921 static int change_curseg(struct f2fs_sb_info *sbi, int type) in change_curseg() argument
2923 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in change_curseg()
2924 struct curseg_info *curseg = CURSEG_I(sbi, type); in change_curseg()
2929 write_sum_page(sbi, curseg->sum_blk, GET_SUM_BLOCK(sbi, curseg->segno)); in change_curseg()
2931 __set_test_and_inuse(sbi, new_segno); in change_curseg()
2934 __remove_dirty_segment(sbi, new_segno, PRE); in change_curseg()
2935 __remove_dirty_segment(sbi, new_segno, DIRTY); in change_curseg()
2938 reset_curseg(sbi, type, 1); in change_curseg()
2940 curseg->next_blkoff = __next_free_blkoff(sbi, curseg->segno, 0); in change_curseg()
2942 sum_page = f2fs_get_sum_page(sbi, new_segno); in change_curseg()
2954 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2957 static int get_atssr_segment(struct f2fs_sb_info *sbi, int type, in get_atssr_segment() argument
2961 struct curseg_info *curseg = CURSEG_I(sbi, type); in get_atssr_segment()
2966 if (get_ssr_segment(sbi, type, alloc_mode, age)) { in get_atssr_segment()
2967 struct seg_entry *se = get_seg_entry(sbi, curseg->next_segno); in get_atssr_segment()
2970 ret = change_curseg(sbi, type); in get_atssr_segment()
2974 ret = new_curseg(sbi, type, true); in get_atssr_segment()
2976 stat_inc_seg_type(sbi, curseg); in get_atssr_segment()
2980 static int __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi, bool force) in __f2fs_init_atgc_curseg() argument
2982 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC); in __f2fs_init_atgc_curseg()
2985 if (!sbi->am.atgc_enabled && !force) in __f2fs_init_atgc_curseg()
2988 f2fs_down_read(&SM_I(sbi)->curseg_lock); in __f2fs_init_atgc_curseg()
2991 down_write(&SIT_I(sbi)->sentry_lock); in __f2fs_init_atgc_curseg()
2993 ret = get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC, in __f2fs_init_atgc_curseg()
2996 up_write(&SIT_I(sbi)->sentry_lock); in __f2fs_init_atgc_curseg()
2999 f2fs_up_read(&SM_I(sbi)->curseg_lock); in __f2fs_init_atgc_curseg()
3003 int f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi) in f2fs_init_inmem_curseg() argument
3005 return __f2fs_init_atgc_curseg(sbi, false); in f2fs_init_inmem_curseg()
3008 int f2fs_reinit_atgc_curseg(struct f2fs_sb_info *sbi) in f2fs_reinit_atgc_curseg() argument
3012 if (!test_opt(sbi, ATGC)) in f2fs_reinit_atgc_curseg()
3014 if (sbi->am.atgc_enabled) in f2fs_reinit_atgc_curseg()
3016 if (le64_to_cpu(F2FS_CKPT(sbi)->elapsed_time) < in f2fs_reinit_atgc_curseg()
3017 sbi->am.age_threshold) in f2fs_reinit_atgc_curseg()
3020 ret = __f2fs_init_atgc_curseg(sbi, true); in f2fs_reinit_atgc_curseg()
3022 sbi->am.atgc_enabled = true; in f2fs_reinit_atgc_curseg()
3023 f2fs_info(sbi, "reenabled age threshold GC"); in f2fs_reinit_atgc_curseg()
3028 static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type) in __f2fs_save_inmem_curseg() argument
3030 struct curseg_info *curseg = CURSEG_I(sbi, type); in __f2fs_save_inmem_curseg()
3036 if (get_valid_blocks(sbi, curseg->segno, false)) { in __f2fs_save_inmem_curseg()
3037 write_sum_page(sbi, curseg->sum_blk, in __f2fs_save_inmem_curseg()
3038 GET_SUM_BLOCK(sbi, curseg->segno)); in __f2fs_save_inmem_curseg()
3040 mutex_lock(&DIRTY_I(sbi)->seglist_lock); in __f2fs_save_inmem_curseg()
3041 __set_test_and_free(sbi, curseg->segno, true); in __f2fs_save_inmem_curseg()
3042 mutex_unlock(&DIRTY_I(sbi)->seglist_lock); in __f2fs_save_inmem_curseg()
3048 void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi) in f2fs_save_inmem_curseg() argument
3050 __f2fs_save_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED); in f2fs_save_inmem_curseg()
3052 if (sbi->am.atgc_enabled) in f2fs_save_inmem_curseg()
3053 __f2fs_save_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC); in f2fs_save_inmem_curseg()
3056 static void __f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi, int type) in __f2fs_restore_inmem_curseg() argument
3058 struct curseg_info *curseg = CURSEG_I(sbi, type); in __f2fs_restore_inmem_curseg()
3063 if (get_valid_blocks(sbi, curseg->segno, false)) in __f2fs_restore_inmem_curseg()
3066 mutex_lock(&DIRTY_I(sbi)->seglist_lock); in __f2fs_restore_inmem_curseg()
3067 __set_test_and_inuse(sbi, curseg->segno); in __f2fs_restore_inmem_curseg()
3068 mutex_unlock(&DIRTY_I(sbi)->seglist_lock); in __f2fs_restore_inmem_curseg()
3073 void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi) in f2fs_restore_inmem_curseg() argument
3075 __f2fs_restore_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED); in f2fs_restore_inmem_curseg()
3077 if (sbi->am.atgc_enabled) in f2fs_restore_inmem_curseg()
3078 __f2fs_restore_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC); in f2fs_restore_inmem_curseg()
3081 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type, in get_ssr_segment() argument
3084 struct curseg_info *curseg = CURSEG_I(sbi, type); in get_ssr_segment()
3090 sanity_check_seg_type(sbi, seg_type); in get_ssr_segment()
3093 if (!f2fs_get_victim(sbi, &segno, BG_GC, seg_type, in get_ssr_segment()
3121 if (!f2fs_get_victim(sbi, &segno, BG_GC, i, in get_ssr_segment()
3129 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { in get_ssr_segment()
3130 segno = get_free_segment(sbi); in get_ssr_segment()
3139 static bool need_new_seg(struct f2fs_sb_info *sbi, int type) in need_new_seg() argument
3141 struct curseg_info *curseg = CURSEG_I(sbi, type); in need_new_seg()
3143 if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) && in need_new_seg()
3146 if (curseg->alloc_type == LFS && is_next_segment_free(sbi, curseg) && in need_new_seg()
3147 likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED))) in need_new_seg()
3149 if (!f2fs_need_SSR(sbi) || !get_ssr_segment(sbi, type, SSR, 0)) in need_new_seg()
3154 int f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type, in f2fs_allocate_segment_for_resize() argument
3157 struct curseg_info *curseg = CURSEG_I(sbi, type); in f2fs_allocate_segment_for_resize()
3161 f2fs_down_read(&SM_I(sbi)->curseg_lock); in f2fs_allocate_segment_for_resize()
3163 down_write(&SIT_I(sbi)->sentry_lock); in f2fs_allocate_segment_for_resize()
3165 segno = CURSEG_I(sbi, type)->segno; in f2fs_allocate_segment_for_resize()
3169 if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type, SSR, 0)) in f2fs_allocate_segment_for_resize()
3170 ret = change_curseg(sbi, type); in f2fs_allocate_segment_for_resize()
3172 ret = new_curseg(sbi, type, true); in f2fs_allocate_segment_for_resize()
3174 stat_inc_seg_type(sbi, curseg); in f2fs_allocate_segment_for_resize()
3176 locate_dirty_segment(sbi, segno); in f2fs_allocate_segment_for_resize()
3178 up_write(&SIT_I(sbi)->sentry_lock); in f2fs_allocate_segment_for_resize()
3181 f2fs_notice(sbi, "For resize: curseg of type %d: %u ==> %u", in f2fs_allocate_segment_for_resize()
3185 f2fs_up_read(&SM_I(sbi)->curseg_lock); in f2fs_allocate_segment_for_resize()
3189 static int __allocate_new_segment(struct f2fs_sb_info *sbi, int type, in __allocate_new_segment() argument
3192 struct curseg_info *curseg = CURSEG_I(sbi, type); in __allocate_new_segment()
3201 !get_valid_blocks(sbi, curseg->segno, new_sec) && in __allocate_new_segment()
3202 !get_ckpt_valid_blocks(sbi, curseg->segno, new_sec)) in __allocate_new_segment()
3207 err = new_curseg(sbi, type, true); in __allocate_new_segment()
3210 stat_inc_seg_type(sbi, curseg); in __allocate_new_segment()
3211 locate_dirty_segment(sbi, old_segno); in __allocate_new_segment()
3215 int f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force) in f2fs_allocate_new_section() argument
3219 f2fs_down_read(&SM_I(sbi)->curseg_lock); in f2fs_allocate_new_section()
3220 down_write(&SIT_I(sbi)->sentry_lock); in f2fs_allocate_new_section()
3221 ret = __allocate_new_segment(sbi, type, true, force); in f2fs_allocate_new_section()
3222 up_write(&SIT_I(sbi)->sentry_lock); in f2fs_allocate_new_section()
3223 f2fs_up_read(&SM_I(sbi)->curseg_lock); in f2fs_allocate_new_section()
3228 int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi) in f2fs_allocate_pinning_section() argument
3234 f2fs_lock_op(sbi); in f2fs_allocate_pinning_section()
3235 err = f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false); in f2fs_allocate_pinning_section()
3236 f2fs_unlock_op(sbi); in f2fs_allocate_pinning_section()
3238 if (f2fs_sb_has_blkzoned(sbi) && err == -EAGAIN && gc_required) { in f2fs_allocate_pinning_section()
3239 f2fs_down_write(&sbi->gc_lock); in f2fs_allocate_pinning_section()
3240 err = f2fs_gc_range(sbi, 0, GET_SEGNO(sbi, FDEV(0).end_blk), true, 1); in f2fs_allocate_pinning_section()
3241 f2fs_up_write(&sbi->gc_lock); in f2fs_allocate_pinning_section()
3251 int f2fs_allocate_new_segments(struct f2fs_sb_info *sbi) in f2fs_allocate_new_segments() argument
3256 f2fs_down_read(&SM_I(sbi)->curseg_lock); in f2fs_allocate_new_segments()
3257 down_write(&SIT_I(sbi)->sentry_lock); in f2fs_allocate_new_segments()
3259 err += __allocate_new_segment(sbi, i, false, false); in f2fs_allocate_new_segments()
3260 up_write(&SIT_I(sbi)->sentry_lock); in f2fs_allocate_new_segments()
3261 f2fs_up_read(&SM_I(sbi)->curseg_lock); in f2fs_allocate_new_segments()
3266 bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi, in f2fs_exist_trim_candidates() argument
3272 down_write(&SIT_I(sbi)->sentry_lock); in f2fs_exist_trim_candidates()
3274 if (add_discard_addrs(sbi, cpc, true)) { in f2fs_exist_trim_candidates()
3279 up_write(&SIT_I(sbi)->sentry_lock); in f2fs_exist_trim_candidates()
3285 static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi, in __issue_discard_cmd_range() argument
3289 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; in __issue_discard_cmd_range()
3302 f2fs_bug_on(sbi, !f2fs_check_discard_tree(sbi)); in __issue_discard_cmd_range()
3323 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued); in __issue_discard_cmd_range()
3329 __remove_discard_cmd(sbi, dc); in __issue_discard_cmd_range()
3333 trimmed += __wait_all_discard_cmd(sbi, NULL); in __issue_discard_cmd_range()
3340 __remove_discard_cmd(sbi, dc); in __issue_discard_cmd_range()
3353 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range) in f2fs_trim_fs() argument
3363 bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi); in f2fs_trim_fs()
3365 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize) in f2fs_trim_fs()
3368 if (end < MAIN_BLKADDR(sbi)) in f2fs_trim_fs()
3371 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) { in f2fs_trim_fs()
3372 f2fs_warn(sbi, "Found FS corruption, run fsck to fix."); in f2fs_trim_fs()
3377 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start); in f2fs_trim_fs()
3378 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 : in f2fs_trim_fs()
3379 GET_SEGNO(sbi, end); in f2fs_trim_fs()
3381 start_segno = rounddown(start_segno, SEGS_PER_SEC(sbi)); in f2fs_trim_fs()
3382 end_segno = roundup(end_segno + 1, SEGS_PER_SEC(sbi)) - 1; in f2fs_trim_fs()
3390 if (sbi->discard_blks == 0) in f2fs_trim_fs()
3393 f2fs_down_write(&sbi->gc_lock); in f2fs_trim_fs()
3394 stat_inc_cp_call_count(sbi, TOTAL_CALL); in f2fs_trim_fs()
3395 err = f2fs_write_checkpoint(sbi, &cpc); in f2fs_trim_fs()
3396 f2fs_up_write(&sbi->gc_lock); in f2fs_trim_fs()
3406 if (f2fs_realtime_discard_enable(sbi)) in f2fs_trim_fs()
3409 start_block = START_BLOCK(sbi, start_segno); in f2fs_trim_fs()
3410 end_block = START_BLOCK(sbi, end_segno + 1); in f2fs_trim_fs()
3412 __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen); in f2fs_trim_fs()
3413 trimmed = __issue_discard_cmd_range(sbi, &dpolicy, in f2fs_trim_fs()
3416 trimmed += __wait_discard_cmd_range(sbi, &dpolicy, in f2fs_trim_fs()
3424 int f2fs_rw_hint_to_seg_type(struct f2fs_sb_info *sbi, enum rw_hint hint) in f2fs_rw_hint_to_seg_type() argument
3426 if (F2FS_OPTION(sbi).active_logs == 2) in f2fs_rw_hint_to_seg_type()
3428 else if (F2FS_OPTION(sbi).active_logs == 4) in f2fs_rw_hint_to_seg_type()
3468 enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi, in f2fs_io_type_to_rw_hint() argument
3528 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); in __get_age_segment_type() local
3534 if (ei.age <= sbi->hot_data_age_threshold) in __get_age_segment_type()
3536 if (ei.age <= sbi->warm_data_age_threshold) in __get_age_segment_type()
3553 if (fio->sbi->am.atgc_enabled && in __get_segment_type_6()
3555 (fio->sbi->gc_mode != GC_URGENT_HIGH) && in __get_segment_type_6()
3597 switch (F2FS_OPTION(fio->sbi).active_logs) { in __get_segment_type()
3608 f2fs_bug_on(fio->sbi, true); in __get_segment_type()
3616 static void f2fs_randomize_chunk(struct f2fs_sb_info *sbi, in f2fs_randomize_chunk() argument
3624 get_random_u32_inclusive(1, sbi->max_fragment_chunk); in f2fs_randomize_chunk()
3626 get_random_u32_inclusive(1, sbi->max_fragment_hole); in f2fs_randomize_chunk()
3636 int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page, in f2fs_allocate_data_block() argument
3641 struct sit_info *sit_i = SIT_I(sbi); in f2fs_allocate_data_block()
3642 struct curseg_info *curseg = CURSEG_I(sbi, type); in f2fs_allocate_data_block()
3649 f2fs_down_read(&SM_I(sbi)->curseg_lock); in f2fs_allocate_data_block()
3660 f2fs_bug_on(sbi, GET_SEGNO(sbi, old_blkaddr) == NULL_SEGNO); in f2fs_allocate_data_block()
3661 se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr)); in f2fs_allocate_data_block()
3662 sanity_check_seg_type(sbi, se->type); in f2fs_allocate_data_block()
3663 f2fs_bug_on(sbi, IS_NODESEG(se->type)); in f2fs_allocate_data_block()
3665 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); in f2fs_allocate_data_block()
3667 f2fs_bug_on(sbi, curseg->next_blkoff >= BLKS_PER_SEG(sbi)); in f2fs_allocate_data_block()
3669 f2fs_wait_discard_bio(sbi, *new_blkaddr); in f2fs_allocate_data_block()
3673 curseg->next_blkoff = f2fs_find_next_ssr_block(sbi, curseg); in f2fs_allocate_data_block()
3676 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) in f2fs_allocate_data_block()
3677 f2fs_randomize_chunk(sbi, curseg); in f2fs_allocate_data_block()
3679 if (curseg->next_blkoff >= f2fs_usable_blks_in_seg(sbi, curseg->segno)) in f2fs_allocate_data_block()
3681 stat_inc_block_count(sbi, curseg); in f2fs_allocate_data_block()
3684 old_mtime = get_segment_mtime(sbi, old_blkaddr); in f2fs_allocate_data_block()
3686 update_segment_mtime(sbi, old_blkaddr, 0); in f2fs_allocate_data_block()
3689 update_segment_mtime(sbi, *new_blkaddr, old_mtime); in f2fs_allocate_data_block()
3695 update_sit_entry(sbi, *new_blkaddr, 1); in f2fs_allocate_data_block()
3696 update_sit_entry(sbi, old_blkaddr, -1); in f2fs_allocate_data_block()
3704 !((curseg->segno + 1) % sbi->segs_per_sec)) { in f2fs_allocate_data_block()
3705 write_sum_page(sbi, curseg->sum_blk, in f2fs_allocate_data_block()
3706 GET_SUM_BLOCK(sbi, curseg->segno)); in f2fs_allocate_data_block()
3712 ret = get_atssr_segment(sbi, type, se->type, in f2fs_allocate_data_block()
3715 if (need_new_seg(sbi, type)) in f2fs_allocate_data_block()
3716 ret = new_curseg(sbi, type, false); in f2fs_allocate_data_block()
3718 ret = change_curseg(sbi, type); in f2fs_allocate_data_block()
3719 stat_inc_seg_type(sbi, curseg); in f2fs_allocate_data_block()
3732 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); in f2fs_allocate_data_block()
3733 locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr)); in f2fs_allocate_data_block()
3736 atomic64_inc(&sbi->allocated_data_blocks); in f2fs_allocate_data_block()
3741 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); in f2fs_allocate_data_block()
3743 f2fs_inode_chksum_set(sbi, page); in f2fs_allocate_data_block()
3751 io = sbi->write_io[fio->type] + fio->temp; in f2fs_allocate_data_block()
3758 f2fs_up_read(&SM_I(sbi)->curseg_lock); in f2fs_allocate_data_block()
3765 f2fs_up_read(&SM_I(sbi)->curseg_lock); in f2fs_allocate_data_block()
3769 void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino, in f2fs_update_device_state() argument
3772 if (!f2fs_is_multi_device(sbi)) in f2fs_update_device_state()
3776 unsigned int devidx = f2fs_target_device_index(sbi, blkaddr); in f2fs_update_device_state()
3780 f2fs_set_dirty_device(sbi, ino, devidx, FLUSH_INO); in f2fs_update_device_state()
3783 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) { in f2fs_update_device_state()
3784 spin_lock(&sbi->dev_lock); in f2fs_update_device_state()
3785 f2fs_set_bit(devidx, (char *)&sbi->dirty_device); in f2fs_update_device_state()
3786 spin_unlock(&sbi->dev_lock); in f2fs_update_device_state()
3799 bool keep_order = (f2fs_lfs_mode(fio->sbi) && type == CURSEG_COLD_DATA); in do_write_page()
3802 f2fs_down_read(&fio->sbi->io_order_lock); in do_write_page()
3804 if (f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr, in do_write_page()
3809 if (f2fs_in_warm_node_list(fio->sbi, fio->page)) in do_write_page()
3810 f2fs_del_fsync_node_entry(fio->sbi, fio->page); in do_write_page()
3813 if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO) in do_write_page()
3814 f2fs_invalidate_internal_cache(fio->sbi, fio->old_blkaddr); in do_write_page()
3819 f2fs_update_device_state(fio->sbi, fio->ino, fio->new_blkaddr, 1); in do_write_page()
3822 f2fs_up_read(&fio->sbi->io_order_lock); in do_write_page()
3825 void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct folio *folio, in f2fs_do_write_meta_page() argument
3829 .sbi = sbi, in f2fs_do_write_meta_page()
3841 if (unlikely(folio->index >= MAIN_BLKADDR(sbi))) in f2fs_do_write_meta_page()
3847 stat_inc_meta_count(sbi, folio->index); in f2fs_do_write_meta_page()
3848 f2fs_update_iostat(sbi, NULL, io_type, F2FS_BLKSIZE); in f2fs_do_write_meta_page()
3858 f2fs_update_iostat(fio->sbi, NULL, fio->io_type, F2FS_BLKSIZE); in f2fs_do_write_node_page()
3864 struct f2fs_sb_info *sbi = fio->sbi; in f2fs_outplace_write_data() local
3867 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR); in f2fs_outplace_write_data()
3874 f2fs_update_iostat(sbi, dn->inode, fio->io_type, F2FS_BLKSIZE); in f2fs_outplace_write_data()
3880 struct f2fs_sb_info *sbi = fio->sbi; in f2fs_inplace_write_data() local
3887 segno = GET_SEGNO(sbi, fio->new_blkaddr); in f2fs_inplace_write_data()
3889 if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) { in f2fs_inplace_write_data()
3890 set_sbi_flag(sbi, SBI_NEED_FSCK); in f2fs_inplace_write_data()
3891 f2fs_warn(sbi, "%s: incorrect segment(%u) type, run fsck to fix.", in f2fs_inplace_write_data()
3894 f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUM_TYPE); in f2fs_inplace_write_data()
3898 if (f2fs_cp_error(sbi)) { in f2fs_inplace_write_data()
3904 f2fs_truncate_meta_inode_pages(sbi, fio->new_blkaddr, 1); in f2fs_inplace_write_data()
3906 stat_inc_inplace_blocks(fio->sbi); in f2fs_inplace_write_data()
3908 if (fio->bio && !IS_F2FS_IPU_NOCACHE(sbi)) in f2fs_inplace_write_data()
3913 f2fs_update_device_state(fio->sbi, fio->ino, in f2fs_inplace_write_data()
3915 f2fs_update_iostat(fio->sbi, fio->page->mapping->host, in f2fs_inplace_write_data()
3931 static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi, in __f2fs_get_curseg() argument
3937 if (CURSEG_I(sbi, i)->segno == segno) in __f2fs_get_curseg()
3943 void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, in f2fs_do_replace_block() argument
3948 struct sit_info *sit_i = SIT_I(sbi); in f2fs_do_replace_block()
3956 segno = GET_SEGNO(sbi, new_blkaddr); in f2fs_do_replace_block()
3957 se = get_seg_entry(sbi, segno); in f2fs_do_replace_block()
3960 f2fs_down_write(&SM_I(sbi)->curseg_lock); in f2fs_do_replace_block()
3964 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) { in f2fs_do_replace_block()
3971 if (IS_CURSEG(sbi, segno)) { in f2fs_do_replace_block()
3973 type = __f2fs_get_curseg(sbi, segno); in f2fs_do_replace_block()
3974 f2fs_bug_on(sbi, type == NO_CHECK_TYPE); in f2fs_do_replace_block()
3980 f2fs_bug_on(sbi, !IS_DATASEG(type)); in f2fs_do_replace_block()
3981 curseg = CURSEG_I(sbi, type); in f2fs_do_replace_block()
3993 if (change_curseg(sbi, type)) in f2fs_do_replace_block()
3997 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); in f2fs_do_replace_block()
4002 update_segment_mtime(sbi, new_blkaddr, 0); in f2fs_do_replace_block()
4003 update_sit_entry(sbi, new_blkaddr, 1); in f2fs_do_replace_block()
4005 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) { in f2fs_do_replace_block()
4006 f2fs_invalidate_internal_cache(sbi, old_blkaddr); in f2fs_do_replace_block()
4008 update_segment_mtime(sbi, old_blkaddr, 0); in f2fs_do_replace_block()
4009 update_sit_entry(sbi, old_blkaddr, -1); in f2fs_do_replace_block()
4012 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); in f2fs_do_replace_block()
4013 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr)); in f2fs_do_replace_block()
4015 locate_dirty_segment(sbi, old_cursegno); in f2fs_do_replace_block()
4020 if (change_curseg(sbi, type)) in f2fs_do_replace_block()
4030 f2fs_up_write(&SM_I(sbi)->curseg_lock); in f2fs_do_replace_block()
4033 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn, in f2fs_replace_block() argument
4042 f2fs_do_replace_block(sbi, &sum, old_addr, new_addr, in f2fs_replace_block()
4052 struct f2fs_sb_info *sbi = F2FS_P_SB(page); in f2fs_wait_on_page_writeback() local
4055 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, type); in f2fs_wait_on_page_writeback()
4057 f2fs_submit_merged_ipu_write(sbi, NULL, page); in f2fs_wait_on_page_writeback()
4060 f2fs_bug_on(sbi, locked && in f2fs_wait_on_page_writeback()
4070 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); in f2fs_wait_on_block_writeback() local
4079 cpage = find_lock_page(META_MAPPING(sbi), blkaddr); in f2fs_wait_on_block_writeback()
4089 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); in f2fs_wait_on_block_writeback_range() local
4098 f2fs_truncate_meta_inode_pages(sbi, blkaddr, len); in f2fs_wait_on_block_writeback_range()
4101 static int read_compacted_summaries(struct f2fs_sb_info *sbi) in read_compacted_summaries() argument
4103 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); in read_compacted_summaries()
4110 start = start_sum_block(sbi); in read_compacted_summaries()
4112 page = f2fs_get_meta_page(sbi, start++); in read_compacted_summaries()
4118 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); in read_compacted_summaries()
4122 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); in read_compacted_summaries()
4131 seg_i = CURSEG_I(sbi, i); in read_compacted_summaries()
4135 reset_curseg(sbi, i, 0); in read_compacted_summaries()
4140 blk_off = BLKS_PER_SEG(sbi); in read_compacted_summaries()
4155 page = f2fs_get_meta_page(sbi, start++); in read_compacted_summaries()
4166 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type) in read_normal_summaries() argument
4168 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); in read_normal_summaries()
4182 if (__exist_node_summaries(sbi)) in read_normal_summaries()
4183 blk_addr = sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type); in read_normal_summaries()
4185 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); in read_normal_summaries()
4191 if (__exist_node_summaries(sbi)) in read_normal_summaries()
4192 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, in read_normal_summaries()
4195 blk_addr = GET_SUM_BLOCK(sbi, segno); in read_normal_summaries()
4198 new = f2fs_get_meta_page(sbi, blk_addr); in read_normal_summaries()
4204 if (__exist_node_summaries(sbi)) { in read_normal_summaries()
4208 for (i = 0; i < BLKS_PER_SEG(sbi); i++, ns++) { in read_normal_summaries()
4213 err = f2fs_restore_node_summary(sbi, segno, sum); in read_normal_summaries()
4220 curseg = CURSEG_I(sbi, type); in read_normal_summaries()
4231 reset_curseg(sbi, type, 0); in read_normal_summaries()
4240 static int restore_curseg_summaries(struct f2fs_sb_info *sbi) in restore_curseg_summaries() argument
4242 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal; in restore_curseg_summaries()
4243 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal; in restore_curseg_summaries()
4247 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) { in restore_curseg_summaries()
4248 int npages = f2fs_npages_for_summary_flush(sbi, true); in restore_curseg_summaries()
4251 f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages, in restore_curseg_summaries()
4255 err = read_compacted_summaries(sbi); in restore_curseg_summaries()
4261 if (__exist_node_summaries(sbi)) in restore_curseg_summaries()
4262 f2fs_ra_meta_pages(sbi, in restore_curseg_summaries()
4263 sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type), in restore_curseg_summaries()
4267 err = read_normal_summaries(sbi, type); in restore_curseg_summaries()
4275 f2fs_err(sbi, "invalid journal entries nats %u sits %u", in restore_curseg_summaries()
4283 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr) in write_compacted_summaries() argument
4292 page = f2fs_grab_meta_page(sbi, blkaddr++); in write_compacted_summaries()
4297 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); in write_compacted_summaries()
4302 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); in write_compacted_summaries()
4308 seg_i = CURSEG_I(sbi, i); in write_compacted_summaries()
4309 for (j = 0; j < f2fs_curseg_valid_blocks(sbi, i); j++) { in write_compacted_summaries()
4311 page = f2fs_grab_meta_page(sbi, blkaddr++); in write_compacted_summaries()
4335 static void write_normal_summaries(struct f2fs_sb_info *sbi, in write_normal_summaries() argument
4346 write_current_sum_page(sbi, i, blkaddr + (i - type)); in write_normal_summaries()
4349 void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk) in f2fs_write_data_summaries() argument
4351 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) in f2fs_write_data_summaries()
4352 write_compacted_summaries(sbi, start_blk); in f2fs_write_data_summaries()
4354 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA); in f2fs_write_data_summaries()
4357 void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk) in f2fs_write_node_summaries() argument
4359 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE); in f2fs_write_node_summaries()
4384 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi, in get_current_sit_page() argument
4387 return f2fs_get_meta_page(sbi, current_sit_addr(sbi, segno)); in get_current_sit_page()
4390 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi, in get_next_sit_page() argument
4393 struct sit_info *sit_i = SIT_I(sbi); in get_next_sit_page()
4397 src_off = current_sit_addr(sbi, start); in get_next_sit_page()
4398 dst_off = next_sit_addr(sbi, src_off); in get_next_sit_page()
4400 page = f2fs_grab_meta_page(sbi, dst_off); in get_next_sit_page()
4401 seg_info_to_sit_page(sbi, page, start); in get_next_sit_page()
4463 static void add_sits_in_set(struct f2fs_sb_info *sbi) in add_sits_in_set() argument
4465 struct f2fs_sm_info *sm_info = SM_I(sbi); in add_sits_in_set()
4467 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap; in add_sits_in_set()
4470 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi)) in add_sits_in_set()
4474 static void remove_sits_in_journal(struct f2fs_sb_info *sbi) in remove_sits_in_journal() argument
4476 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); in remove_sits_in_journal()
4486 dirtied = __mark_sit_entry_dirty(sbi, segno); in remove_sits_in_journal()
4489 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set); in remove_sits_in_journal()
4499 void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc) in f2fs_flush_sit_entries() argument
4501 struct sit_info *sit_i = SIT_I(sbi); in f2fs_flush_sit_entries()
4503 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); in f2fs_flush_sit_entries()
4506 struct list_head *head = &SM_I(sbi)->sit_entry_set; in f2fs_flush_sit_entries()
4507 bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS); in f2fs_flush_sit_entries()
4519 add_sits_in_set(sbi); in f2fs_flush_sit_entries()
4528 remove_sits_in_journal(sbi); in f2fs_flush_sit_entries()
4540 (unsigned long)MAIN_SEGS(sbi)); in f2fs_flush_sit_entries()
4550 page = get_next_sit_page(sbi, start_segno); in f2fs_flush_sit_entries()
4558 se = get_seg_entry(sbi, segno); in f2fs_flush_sit_entries()
4562 f2fs_bug_on(sbi, 1); in f2fs_flush_sit_entries()
4568 add_discard_addrs(sbi, cpc, false); in f2fs_flush_sit_entries()
4574 f2fs_bug_on(sbi, offset < 0); in f2fs_flush_sit_entries()
4579 check_block_count(sbi, segno, in f2fs_flush_sit_entries()
4585 check_block_count(sbi, segno, in f2fs_flush_sit_entries()
4599 f2fs_bug_on(sbi, ses->entry_cnt); in f2fs_flush_sit_entries()
4603 f2fs_bug_on(sbi, !list_empty(head)); in f2fs_flush_sit_entries()
4604 f2fs_bug_on(sbi, sit_i->dirty_sentries); in f2fs_flush_sit_entries()
4610 add_discard_addrs(sbi, cpc, false); in f2fs_flush_sit_entries()
4616 set_prefree_as_free_segments(sbi); in f2fs_flush_sit_entries()
4619 static int build_sit_info(struct f2fs_sb_info *sbi) in build_sit_info() argument
4621 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); in build_sit_info()
4626 unsigned int discard_map = f2fs_block_unit_discard(sbi) ? 1 : 0; in build_sit_info()
4629 sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL); in build_sit_info()
4633 SM_I(sbi)->sit_info = sit_i; in build_sit_info()
4636 f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry), in build_sit_info()
4637 MAIN_SEGS(sbi)), in build_sit_info()
4642 main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); in build_sit_info()
4643 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size, in build_sit_info()
4649 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (3 + discard_map); in build_sit_info()
4651 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (2 + discard_map); in build_sit_info()
4653 sit_i->bitmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL); in build_sit_info()
4659 for (start = 0; start < MAIN_SEGS(sbi); start++) { in build_sit_info()
4677 sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); in build_sit_info()
4681 if (__is_large_section(sbi)) { in build_sit_info()
4683 f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry), in build_sit_info()
4684 MAIN_SECS(sbi)), in build_sit_info()
4694 sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP); in build_sit_info()
4695 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); in build_sit_info()
4707 sit_i->invalid_segmap = f2fs_kvzalloc(sbi, in build_sit_info()
4714 sit_i->sit_blocks = SEGS_TO_BLKS(sbi, sit_segs); in build_sit_info()
4719 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time); in build_sit_info()
4725 static int build_free_segmap(struct f2fs_sb_info *sbi) in build_free_segmap() argument
4731 free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL); in build_free_segmap()
4735 SM_I(sbi)->free_info = free_i; in build_free_segmap()
4737 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); in build_free_segmap()
4738 free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL); in build_free_segmap()
4742 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); in build_free_segmap()
4743 free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL); in build_free_segmap()
4752 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi)); in build_free_segmap()
4759 static int build_curseg(struct f2fs_sb_info *sbi) in build_curseg() argument
4764 array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE, in build_curseg()
4769 SM_I(sbi)->curseg_array = array; in build_curseg()
4773 array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL); in build_curseg()
4777 array[i].journal = f2fs_kzalloc(sbi, in build_curseg()
4789 return restore_curseg_summaries(sbi); in build_curseg()
4792 static int build_sit_entries(struct f2fs_sb_info *sbi) in build_sit_entries() argument
4794 struct sit_info *sit_i = SIT_I(sbi); in build_sit_entries()
4795 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); in build_sit_entries()
4799 int sit_blk_cnt = SIT_BLK_CNT(sbi); in build_sit_entries()
4806 readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS, in build_sit_entries()
4812 for (; start < end && start < MAIN_SEGS(sbi); start++) { in build_sit_entries()
4817 page = get_current_sit_page(sbi, start); in build_sit_entries()
4824 err = check_block_count(sbi, start, &sit); in build_sit_entries()
4830 f2fs_err(sbi, "Invalid segment type: %u, segno: %u", in build_sit_entries()
4832 f2fs_handle_error(sbi, in build_sit_entries()
4839 if (!f2fs_block_unit_discard(sbi)) in build_sit_entries()
4843 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) { in build_sit_entries()
4850 sbi->discard_blks += BLKS_PER_SEG(sbi) - in build_sit_entries()
4853 if (__is_large_section(sbi)) in build_sit_entries()
4854 get_sec_entry(sbi, start)->valid_blocks += in build_sit_entries()
4865 if (start >= MAIN_SEGS(sbi)) { in build_sit_entries()
4866 f2fs_err(sbi, "Wrong journal entry on segno %u", in build_sit_entries()
4869 f2fs_handle_error(sbi, ERROR_CORRUPTED_JOURNAL); in build_sit_entries()
4880 err = check_block_count(sbi, start, &sit); in build_sit_entries()
4886 f2fs_err(sbi, "Invalid segment type: %u, segno: %u", in build_sit_entries()
4889 f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUM_TYPE); in build_sit_entries()
4895 if (f2fs_block_unit_discard(sbi)) { in build_sit_entries()
4896 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) { in build_sit_entries()
4901 sbi->discard_blks += old_valid_blocks; in build_sit_entries()
4902 sbi->discard_blks -= se->valid_blocks; in build_sit_entries()
4906 if (__is_large_section(sbi)) { in build_sit_entries()
4907 get_sec_entry(sbi, start)->valid_blocks += in build_sit_entries()
4909 get_sec_entry(sbi, start)->valid_blocks -= in build_sit_entries()
4918 if (sit_valid_blocks[NODE] != valid_node_count(sbi)) { in build_sit_entries()
4919 f2fs_err(sbi, "SIT is corrupted node# %u vs %u", in build_sit_entries()
4920 sit_valid_blocks[NODE], valid_node_count(sbi)); in build_sit_entries()
4921 f2fs_handle_error(sbi, ERROR_INCONSISTENT_NODE_COUNT); in build_sit_entries()
4926 valid_user_blocks(sbi)) { in build_sit_entries()
4927 f2fs_err(sbi, "SIT is corrupted data# %u %u vs %u", in build_sit_entries()
4929 valid_user_blocks(sbi)); in build_sit_entries()
4930 f2fs_handle_error(sbi, ERROR_INCONSISTENT_BLOCK_COUNT); in build_sit_entries()
4937 static void init_free_segmap(struct f2fs_sb_info *sbi) in init_free_segmap() argument
4943 for (start = 0; start < MAIN_SEGS(sbi); start++) { in init_free_segmap()
4944 if (f2fs_usable_blks_in_seg(sbi, start) == 0) in init_free_segmap()
4946 sentry = get_seg_entry(sbi, start); in init_free_segmap()
4948 __set_free(sbi, start); in init_free_segmap()
4950 SIT_I(sbi)->written_valid_blocks += in init_free_segmap()
4956 struct curseg_info *curseg_t = CURSEG_I(sbi, type); in init_free_segmap()
4958 __set_test_and_inuse(sbi, curseg_t->segno); in init_free_segmap()
4962 static void init_dirty_segmap(struct f2fs_sb_info *sbi) in init_dirty_segmap() argument
4964 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in init_dirty_segmap()
4965 struct free_segmap_info *free_i = FREE_I(sbi); in init_dirty_segmap()
4971 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset); in init_dirty_segmap()
4972 if (segno >= MAIN_SEGS(sbi)) in init_dirty_segmap()
4975 valid_blocks = get_valid_blocks(sbi, segno, false); in init_dirty_segmap()
4976 usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno); in init_dirty_segmap()
4980 f2fs_bug_on(sbi, 1); in init_dirty_segmap()
4984 __locate_dirty_segment(sbi, segno, DIRTY); in init_dirty_segmap()
4988 if (!__is_large_section(sbi)) in init_dirty_segmap()
4992 for (segno = 0; segno < MAIN_SEGS(sbi); segno += SEGS_PER_SEC(sbi)) { in init_dirty_segmap()
4993 valid_blocks = get_valid_blocks(sbi, segno, true); in init_dirty_segmap()
4994 secno = GET_SEC_FROM_SEG(sbi, segno); in init_dirty_segmap()
4996 if (!valid_blocks || valid_blocks == CAP_BLKS_PER_SEC(sbi)) in init_dirty_segmap()
4998 if (IS_CURSEC(sbi, secno)) in init_dirty_segmap()
5005 static int init_victim_secmap(struct f2fs_sb_info *sbi) in init_victim_secmap() argument
5007 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in init_victim_secmap()
5008 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); in init_victim_secmap()
5010 dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL); in init_victim_secmap()
5014 dirty_i->pinned_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL); in init_victim_secmap()
5023 static int build_dirty_segmap(struct f2fs_sb_info *sbi) in build_dirty_segmap() argument
5029 dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info), in build_dirty_segmap()
5034 SM_I(sbi)->dirty_info = dirty_i; in build_dirty_segmap()
5037 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); in build_dirty_segmap()
5040 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size, in build_dirty_segmap()
5046 if (__is_large_section(sbi)) { in build_dirty_segmap()
5047 bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); in build_dirty_segmap()
5048 dirty_i->dirty_secmap = f2fs_kvzalloc(sbi, in build_dirty_segmap()
5054 init_dirty_segmap(sbi); in build_dirty_segmap()
5055 return init_victim_secmap(sbi); in build_dirty_segmap()
5058 static int sanity_check_curseg(struct f2fs_sb_info *sbi) in sanity_check_curseg() argument
5067 struct curseg_info *curseg = CURSEG_I(sbi, i); in sanity_check_curseg()
5068 struct seg_entry *se = get_seg_entry(sbi, curseg->segno); in sanity_check_curseg()
5071 if (f2fs_sb_has_readonly(sbi) && in sanity_check_curseg()
5075 sanity_check_seg_type(sbi, curseg->seg_type); in sanity_check_curseg()
5078 f2fs_err(sbi, in sanity_check_curseg()
5081 f2fs_handle_error(sbi, ERROR_INVALID_CURSEG); in sanity_check_curseg()
5091 for (blkofs += 1; blkofs < BLKS_PER_SEG(sbi); blkofs++) { in sanity_check_curseg()
5095 f2fs_err(sbi, in sanity_check_curseg()
5099 f2fs_handle_error(sbi, ERROR_INVALID_CURSEG); in sanity_check_curseg()
5107 static int check_zone_write_pointer(struct f2fs_sb_info *sbi, in check_zone_write_pointer() argument
5113 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT; in check_zone_write_pointer()
5121 zone_segno = GET_SEGNO(sbi, zone_block); in check_zone_write_pointer()
5127 if (zone_segno >= MAIN_SEGS(sbi)) in check_zone_write_pointer()
5133 valid_block_cnt = get_valid_blocks(sbi, zone_segno, true); in check_zone_write_pointer()
5134 if (IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, zone_segno))) { in check_zone_write_pointer()
5135 f2fs_notice(sbi, "Open zones: valid block[0x%x,0x%x] cond[%s]", in check_zone_write_pointer()
5146 f2fs_notice(sbi, "Zone without valid block has non-zero write " in check_zone_write_pointer()
5149 ret = __f2fs_issue_discard_zone(sbi, fdev->bdev, zone_block, in check_zone_write_pointer()
5152 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)", in check_zone_write_pointer()
5164 f2fs_notice(sbi, "Valid blocks are not aligned with write " in check_zone_write_pointer()
5177 f2fs_err(sbi, "Fill up zone failed: %s (errno=%d)", in check_zone_write_pointer()
5180 f2fs_err(sbi, "Finishing zone failed: %s (errno=%d)", in check_zone_write_pointer()
5187 static struct f2fs_dev_info *get_target_zoned_dev(struct f2fs_sb_info *sbi, in get_target_zoned_dev() argument
5192 for (i = 0; i < sbi->s_ndevs; i++) { in get_target_zoned_dev()
5195 if (sbi->s_ndevs == 1 || (FDEV(i).start_blk <= zone_blkaddr && in get_target_zoned_dev()
5210 static int fix_curseg_write_pointer(struct f2fs_sb_info *sbi, int type) in fix_curseg_write_pointer() argument
5212 struct curseg_info *cs = CURSEG_I(sbi, type); in fix_curseg_write_pointer()
5217 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT; in fix_curseg_write_pointer()
5221 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno); in fix_curseg_write_pointer()
5222 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section)); in fix_curseg_write_pointer()
5224 zbd = get_target_zoned_dev(sbi, cs_zone_block); in fix_curseg_write_pointer()
5234 f2fs_err(sbi, "Report zone failed: %s errno=(%d)", in fix_curseg_write_pointer()
5246 if (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { in fix_curseg_write_pointer()
5248 wp_segno = GET_SEGNO(sbi, wp_block); in fix_curseg_write_pointer()
5249 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno); in fix_curseg_write_pointer()
5256 f2fs_notice(sbi, "Unaligned curseg[%d] with write pointer: " in fix_curseg_write_pointer()
5263 cs->segno != GET_SEG_FROM_SEC(sbi, GET_ZONE_FROM_SEC(sbi, cs_section))) { in fix_curseg_write_pointer()
5266 f2fs_allocate_new_section(sbi, type, true); in fix_curseg_write_pointer()
5267 f2fs_notice(sbi, "Assign new section to curseg[%d]: " in fix_curseg_write_pointer()
5274 if (check_zone_write_pointer(sbi, zbd, &zone)) in fix_curseg_write_pointer()
5278 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno); in fix_curseg_write_pointer()
5279 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section)); in fix_curseg_write_pointer()
5281 zbd = get_target_zoned_dev(sbi, cs_zone_block); in fix_curseg_write_pointer()
5290 f2fs_err(sbi, "Report zone failed: %s errno=(%d)", in fix_curseg_write_pointer()
5299 f2fs_notice(sbi, in fix_curseg_write_pointer()
5303 err = __f2fs_issue_discard_zone(sbi, zbd->bdev, cs_zone_block, in fix_curseg_write_pointer()
5306 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)", in fix_curseg_write_pointer()
5315 int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi) in f2fs_fix_curseg_write_pointer() argument
5320 ret = fix_curseg_write_pointer(sbi, i); in f2fs_fix_curseg_write_pointer()
5329 struct f2fs_sb_info *sbi; member
5340 return check_zone_write_pointer(args->sbi, args->fdev, zone); in check_zone_write_pointer_cb()
5343 int f2fs_check_write_pointer(struct f2fs_sb_info *sbi) in f2fs_check_write_pointer() argument
5348 for (i = 0; i < sbi->s_ndevs; i++) { in f2fs_check_write_pointer()
5352 args.sbi = sbi; in f2fs_check_write_pointer()
5372 struct f2fs_sb_info *sbi, unsigned int segno) in f2fs_usable_zone_blks_in_seg() argument
5377 if (!sbi->unusable_blocks_per_sec) in f2fs_usable_zone_blks_in_seg()
5378 return BLKS_PER_SEG(sbi); in f2fs_usable_zone_blks_in_seg()
5380 secno = GET_SEC_FROM_SEG(sbi, segno); in f2fs_usable_zone_blks_in_seg()
5381 seg_start = START_BLOCK(sbi, segno); in f2fs_usable_zone_blks_in_seg()
5382 sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno)); in f2fs_usable_zone_blks_in_seg()
5383 sec_cap_blkaddr = sec_start_blkaddr + CAP_BLKS_PER_SEC(sbi); in f2fs_usable_zone_blks_in_seg()
5393 if (seg_start + BLKS_PER_SEG(sbi) > sec_cap_blkaddr) in f2fs_usable_zone_blks_in_seg()
5396 return BLKS_PER_SEG(sbi); in f2fs_usable_zone_blks_in_seg()
5399 int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi) in f2fs_fix_curseg_write_pointer() argument
5404 int f2fs_check_write_pointer(struct f2fs_sb_info *sbi) in f2fs_check_write_pointer() argument
5409 static inline unsigned int f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info *sbi, in f2fs_usable_zone_blks_in_seg() argument
5416 unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi, in f2fs_usable_blks_in_seg() argument
5419 if (f2fs_sb_has_blkzoned(sbi)) in f2fs_usable_blks_in_seg()
5420 return f2fs_usable_zone_blks_in_seg(sbi, segno); in f2fs_usable_blks_in_seg()
5422 return BLKS_PER_SEG(sbi); in f2fs_usable_blks_in_seg()
5425 unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi) in f2fs_usable_segs_in_sec() argument
5427 if (f2fs_sb_has_blkzoned(sbi)) in f2fs_usable_segs_in_sec()
5428 return CAP_SEGS_PER_SEC(sbi); in f2fs_usable_segs_in_sec()
5430 return SEGS_PER_SEC(sbi); in f2fs_usable_segs_in_sec()
5436 static void init_min_max_mtime(struct f2fs_sb_info *sbi) in init_min_max_mtime() argument
5438 struct sit_info *sit_i = SIT_I(sbi); in init_min_max_mtime()
5445 for (segno = 0; segno < MAIN_SEGS(sbi); segno += SEGS_PER_SEC(sbi)) { in init_min_max_mtime()
5449 for (i = 0; i < SEGS_PER_SEC(sbi); i++) in init_min_max_mtime()
5450 mtime += get_seg_entry(sbi, segno + i)->mtime; in init_min_max_mtime()
5452 mtime = div_u64(mtime, SEGS_PER_SEC(sbi)); in init_min_max_mtime()
5457 sit_i->max_mtime = get_mtime(sbi, false); in init_min_max_mtime()
5462 int f2fs_build_segment_manager(struct f2fs_sb_info *sbi) in f2fs_build_segment_manager() argument
5464 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); in f2fs_build_segment_manager()
5465 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); in f2fs_build_segment_manager()
5469 sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL); in f2fs_build_segment_manager()
5474 sbi->sm_info = sm_info; in f2fs_build_segment_manager()
5487 if (!f2fs_lfs_mode(sbi)) in f2fs_build_segment_manager()
5491 sm_info->min_seq_blocks = BLKS_PER_SEG(sbi); in f2fs_build_segment_manager()
5493 sm_info->min_ssr_sections = reserved_sections(sbi); in f2fs_build_segment_manager()
5499 err = f2fs_create_flush_cmd_control(sbi); in f2fs_build_segment_manager()
5503 err = create_discard_cmd_control(sbi); in f2fs_build_segment_manager()
5507 err = build_sit_info(sbi); in f2fs_build_segment_manager()
5510 err = build_free_segmap(sbi); in f2fs_build_segment_manager()
5513 err = build_curseg(sbi); in f2fs_build_segment_manager()
5518 err = build_sit_entries(sbi); in f2fs_build_segment_manager()
5522 init_free_segmap(sbi); in f2fs_build_segment_manager()
5523 err = build_dirty_segmap(sbi); in f2fs_build_segment_manager()
5527 err = sanity_check_curseg(sbi); in f2fs_build_segment_manager()
5531 init_min_max_mtime(sbi); in f2fs_build_segment_manager()
5535 static void discard_dirty_segmap(struct f2fs_sb_info *sbi, in discard_dirty_segmap() argument
5538 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in discard_dirty_segmap()
5546 static void destroy_victim_secmap(struct f2fs_sb_info *sbi) in destroy_victim_secmap() argument
5548 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in destroy_victim_secmap()
5554 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi) in destroy_dirty_segmap() argument
5556 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in destroy_dirty_segmap()
5564 discard_dirty_segmap(sbi, i); in destroy_dirty_segmap()
5566 if (__is_large_section(sbi)) { in destroy_dirty_segmap()
5572 destroy_victim_secmap(sbi); in destroy_dirty_segmap()
5573 SM_I(sbi)->dirty_info = NULL; in destroy_dirty_segmap()
5577 static void destroy_curseg(struct f2fs_sb_info *sbi) in destroy_curseg() argument
5579 struct curseg_info *array = SM_I(sbi)->curseg_array; in destroy_curseg()
5584 SM_I(sbi)->curseg_array = NULL; in destroy_curseg()
5592 static void destroy_free_segmap(struct f2fs_sb_info *sbi) in destroy_free_segmap() argument
5594 struct free_segmap_info *free_i = SM_I(sbi)->free_info; in destroy_free_segmap()
5598 SM_I(sbi)->free_info = NULL; in destroy_free_segmap()
5604 static void destroy_sit_info(struct f2fs_sb_info *sbi) in destroy_sit_info() argument
5606 struct sit_info *sit_i = SIT_I(sbi); in destroy_sit_info()
5619 SM_I(sbi)->sit_info = NULL; in destroy_sit_info()
5628 void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi) in f2fs_destroy_segment_manager() argument
5630 struct f2fs_sm_info *sm_info = SM_I(sbi); in f2fs_destroy_segment_manager()
5634 f2fs_destroy_flush_cmd_control(sbi, true); in f2fs_destroy_segment_manager()
5635 destroy_discard_cmd_control(sbi); in f2fs_destroy_segment_manager()
5636 destroy_dirty_segmap(sbi); in f2fs_destroy_segment_manager()
5637 destroy_curseg(sbi); in f2fs_destroy_segment_manager()
5638 destroy_free_segmap(sbi); in f2fs_destroy_segment_manager()
5639 destroy_sit_info(sbi); in f2fs_destroy_segment_manager()
5640 sbi->sm_info = NULL; in f2fs_destroy_segment_manager()