Lines Matching full:sbi
33 struct f2fs_sb_info *sbi = data; in gc_thread_func() local
34 struct f2fs_gc_kthread *gc_th = sbi->gc_thread; in gc_thread_func()
35 wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head; in gc_thread_func()
36 wait_queue_head_t *fggc_wq = &sbi->gc_thread->fggc_wq; in gc_thread_func()
55 if (test_opt(sbi, GC_MERGE) && waitqueue_active(fggc_wq)) in gc_thread_func()
62 if (f2fs_readonly(sbi->sb)) { in gc_thread_func()
63 stat_other_skip_bggc_count(sbi); in gc_thread_func()
69 if (sbi->sb->s_writers.frozen >= SB_FREEZE_WRITE) { in gc_thread_func()
71 stat_other_skip_bggc_count(sbi); in gc_thread_func()
75 if (time_to_inject(sbi, FAULT_CHECKPOINT)) in gc_thread_func()
76 f2fs_stop_checkpoint(sbi, false, in gc_thread_func()
79 if (!sb_start_write_trylock(sbi->sb)) { in gc_thread_func()
80 stat_other_skip_bggc_count(sbi); in gc_thread_func()
97 if (sbi->gc_mode == GC_URGENT_HIGH || in gc_thread_func()
98 sbi->gc_mode == GC_URGENT_MID) { in gc_thread_func()
100 f2fs_down_write(&sbi->gc_lock); in gc_thread_func()
105 f2fs_down_write(&sbi->gc_lock); in gc_thread_func()
107 } else if (!f2fs_down_write_trylock(&sbi->gc_lock)) { in gc_thread_func()
108 stat_other_skip_bggc_count(sbi); in gc_thread_func()
112 if (!is_idle(sbi, GC_TIME)) { in gc_thread_func()
114 f2fs_up_write(&sbi->gc_lock); in gc_thread_func()
115 stat_io_skip_bggc_count(sbi); in gc_thread_func()
119 if (has_enough_invalid_blocks(sbi)) in gc_thread_func()
124 stat_inc_gc_call_count(sbi, foreground ? in gc_thread_func()
127 sync_mode = F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC; in gc_thread_func()
138 if (f2fs_gc(sbi, &gc_control)) { in gc_thread_func()
151 trace_f2fs_background_gc(sbi->sb, wait_ms, in gc_thread_func()
152 prefree_segments(sbi), free_segments(sbi)); in gc_thread_func()
155 f2fs_balance_fs_bg(sbi, true); in gc_thread_func()
157 if (sbi->gc_mode != GC_NORMAL) { in gc_thread_func()
158 spin_lock(&sbi->gc_remaining_trials_lock); in gc_thread_func()
159 if (sbi->gc_remaining_trials) { in gc_thread_func()
160 sbi->gc_remaining_trials--; in gc_thread_func()
161 if (!sbi->gc_remaining_trials) in gc_thread_func()
162 sbi->gc_mode = GC_NORMAL; in gc_thread_func()
164 spin_unlock(&sbi->gc_remaining_trials_lock); in gc_thread_func()
166 sb_end_write(sbi->sb); in gc_thread_func()
172 int f2fs_start_gc_thread(struct f2fs_sb_info *sbi) in f2fs_start_gc_thread() argument
175 dev_t dev = sbi->sb->s_bdev->bd_dev; in f2fs_start_gc_thread()
177 gc_th = f2fs_kmalloc(sbi, sizeof(struct f2fs_gc_kthread), GFP_KERNEL); in f2fs_start_gc_thread()
188 sbi->gc_thread = gc_th; in f2fs_start_gc_thread()
189 init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head); in f2fs_start_gc_thread()
190 init_waitqueue_head(&sbi->gc_thread->fggc_wq); in f2fs_start_gc_thread()
191 sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi, in f2fs_start_gc_thread()
197 sbi->gc_thread = NULL; in f2fs_start_gc_thread()
204 void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi) in f2fs_stop_gc_thread() argument
206 struct f2fs_gc_kthread *gc_th = sbi->gc_thread; in f2fs_stop_gc_thread()
213 sbi->gc_thread = NULL; in f2fs_stop_gc_thread()
216 static int select_gc_type(struct f2fs_sb_info *sbi, int gc_type) in select_gc_type() argument
221 if (sbi->am.atgc_enabled) in select_gc_type()
229 switch (sbi->gc_mode) { in select_gc_type()
245 static void select_policy(struct f2fs_sb_info *sbi, int gc_type, in select_policy() argument
248 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in select_policy()
261 p->gc_mode = select_gc_type(sbi, gc_type); in select_policy()
262 p->ofs_unit = sbi->segs_per_sec; in select_policy()
263 if (__is_large_section(sbi)) { in select_policy()
266 0, MAIN_SECS(sbi)); in select_policy()
278 (sbi->gc_mode != GC_URGENT_HIGH) && in select_policy()
280 p->max_search > sbi->max_victim_search) in select_policy()
281 p->max_search = sbi->max_victim_search; in select_policy()
284 if (f2fs_need_rand_seg(sbi)) in select_policy()
285 p->offset = get_random_u32_below(MAIN_SECS(sbi) * sbi->segs_per_sec); in select_policy()
286 else if (test_opt(sbi, NOHEAP) && in select_policy()
290 p->offset = SIT_I(sbi)->last_victim[p->gc_mode]; in select_policy()
293 static unsigned int get_max_cost(struct f2fs_sb_info *sbi, in get_max_cost() argument
298 return sbi->blocks_per_seg; in get_max_cost()
304 return 2 * sbi->blocks_per_seg * p->ofs_unit; in get_max_cost()
313 static unsigned int check_bg_victims(struct f2fs_sb_info *sbi) in check_bg_victims() argument
315 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in check_bg_victims()
323 for_each_set_bit(secno, dirty_i->victim_secmap, MAIN_SECS(sbi)) { in check_bg_victims()
324 if (sec_usage_check(sbi, secno)) in check_bg_victims()
327 return GET_SEG_FROM_SEC(sbi, secno); in check_bg_victims()
332 static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno) in get_cb_cost() argument
334 struct sit_info *sit_i = SIT_I(sbi); in get_cb_cost()
335 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno); in get_cb_cost()
336 unsigned int start = GET_SEG_FROM_SEC(sbi, secno); in get_cb_cost()
342 unsigned int usable_segs_per_sec = f2fs_usable_segs_in_sec(sbi, segno); in get_cb_cost()
345 mtime += get_seg_entry(sbi, start + i)->mtime; in get_cb_cost()
346 vblocks = get_valid_blocks(sbi, segno, true); in get_cb_cost()
351 u = (vblocks * 100) >> sbi->log_blocks_per_seg; in get_cb_cost()
365 static inline unsigned int get_gc_cost(struct f2fs_sb_info *sbi, in get_gc_cost() argument
369 return get_seg_entry(sbi, segno)->ckpt_valid_blocks; in get_gc_cost()
373 return get_valid_blocks(sbi, segno, true); in get_gc_cost()
375 return get_cb_cost(sbi, segno); in get_gc_cost()
377 f2fs_bug_on(sbi, 1); in get_gc_cost()
393 static bool f2fs_check_victim_tree(struct f2fs_sb_info *sbi, in f2fs_check_victim_tree() argument
409 f2fs_info(sbi, "broken victim_rbtree, " in f2fs_check_victim_tree()
420 static struct victim_entry *__lookup_victim_entry(struct f2fs_sb_info *sbi, in __lookup_victim_entry() argument
423 struct atgc_management *am = &sbi->am; in __lookup_victim_entry()
438 static struct victim_entry *__create_victim_entry(struct f2fs_sb_info *sbi, in __create_victim_entry() argument
441 struct atgc_management *am = &sbi->am; in __create_victim_entry()
455 static void __insert_victim_entry(struct f2fs_sb_info *sbi, in __insert_victim_entry() argument
458 struct atgc_management *am = &sbi->am; in __insert_victim_entry()
478 ve = __create_victim_entry(sbi, mtime, segno); in __insert_victim_entry()
484 static void add_victim_entry(struct f2fs_sb_info *sbi, in add_victim_entry() argument
487 struct sit_info *sit_i = SIT_I(sbi); in add_victim_entry()
488 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno); in add_victim_entry()
489 unsigned int start = GET_SEG_FROM_SEC(sbi, secno); in add_victim_entry()
493 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { in add_victim_entry()
495 get_valid_blocks(sbi, segno, true) == 0) in add_victim_entry()
499 for (i = 0; i < sbi->segs_per_sec; i++) in add_victim_entry()
500 mtime += get_seg_entry(sbi, start + i)->mtime; in add_victim_entry()
501 mtime = div_u64(mtime, sbi->segs_per_sec); in add_victim_entry()
517 __insert_victim_entry(sbi, mtime, segno); in add_victim_entry()
520 static void atgc_lookup_victim(struct f2fs_sb_info *sbi, in atgc_lookup_victim() argument
523 struct sit_info *sit_i = SIT_I(sbi); in atgc_lookup_victim()
524 struct atgc_management *am = &sbi->am; in atgc_lookup_victim()
532 unsigned int sec_blocks = CAP_BLKS_PER_SEC(sbi); in atgc_lookup_victim()
564 vblocks = get_valid_blocks(sbi, ve->segno, true); in atgc_lookup_victim()
565 f2fs_bug_on(sbi, !vblocks || vblocks == sec_blocks); in atgc_lookup_victim()
571 f2fs_bug_on(sbi, age + u >= UINT_MAX); in atgc_lookup_victim()
593 static void atssr_lookup_victim(struct f2fs_sb_info *sbi, in atssr_lookup_victim() argument
596 struct sit_info *sit_i = SIT_I(sbi); in atssr_lookup_victim()
597 struct atgc_management *am = &sbi->am; in atssr_lookup_victim()
602 unsigned int seg_blocks = sbi->blocks_per_seg; in atssr_lookup_victim()
615 ve = __lookup_victim_entry(sbi, p->age); in atssr_lookup_victim()
628 vblocks = get_seg_entry(sbi, ve->segno)->ckpt_valid_blocks; in atssr_lookup_victim()
629 f2fs_bug_on(sbi, !vblocks); in atssr_lookup_victim()
658 static void lookup_victim_by_age(struct f2fs_sb_info *sbi, in lookup_victim_by_age() argument
661 f2fs_bug_on(sbi, !f2fs_check_victim_tree(sbi, &sbi->am.root)); in lookup_victim_by_age()
664 atgc_lookup_victim(sbi, p); in lookup_victim_by_age()
666 atssr_lookup_victim(sbi, p); in lookup_victim_by_age()
668 f2fs_bug_on(sbi, 1); in lookup_victim_by_age()
671 static void release_victim_entry(struct f2fs_sb_info *sbi) in release_victim_entry() argument
673 struct atgc_management *am = &sbi->am; in release_victim_entry()
684 f2fs_bug_on(sbi, am->victim_count); in release_victim_entry()
685 f2fs_bug_on(sbi, !list_empty(&am->victim_list)); in release_victim_entry()
688 static bool f2fs_pin_section(struct f2fs_sb_info *sbi, unsigned int segno) in f2fs_pin_section() argument
690 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in f2fs_pin_section()
691 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno); in f2fs_pin_section()
713 static void f2fs_unpin_all_sections(struct f2fs_sb_info *sbi, bool enable) in f2fs_unpin_all_sections() argument
715 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); in f2fs_unpin_all_sections()
717 if (f2fs_pinned_section_exists(DIRTY_I(sbi))) { in f2fs_unpin_all_sections()
718 memset(DIRTY_I(sbi)->pinned_secmap, 0, bitmap_size); in f2fs_unpin_all_sections()
719 DIRTY_I(sbi)->pinned_secmap_cnt = 0; in f2fs_unpin_all_sections()
721 DIRTY_I(sbi)->enable_pin_section = enable; in f2fs_unpin_all_sections()
744 int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result, in f2fs_get_victim() argument
748 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); in f2fs_get_victim()
749 struct sit_info *sm = SIT_I(sbi); in f2fs_get_victim()
758 last_segment = MAIN_SECS(sbi) * sbi->segs_per_sec; in f2fs_get_victim()
762 p.age_threshold = sbi->am.age_threshold; in f2fs_get_victim()
765 select_policy(sbi, gc_type, type, &p); in f2fs_get_victim()
768 p.min_cost = get_max_cost(sbi, &p); in f2fs_get_victim()
774 SIT_I(sbi)->dirty_min_mtime = ULLONG_MAX; in f2fs_get_victim()
777 if (!get_valid_blocks(sbi, *result, false)) { in f2fs_get_victim()
782 if (sec_usage_check(sbi, GET_SEC_FROM_SEG(sbi, *result))) in f2fs_get_victim()
793 if (__is_large_section(sbi) && p.alloc_mode == LFS) { in f2fs_get_victim()
794 if (sbi->next_victim_seg[BG_GC] != NULL_SEGNO) { in f2fs_get_victim()
795 p.min_segno = sbi->next_victim_seg[BG_GC]; in f2fs_get_victim()
797 sbi->next_victim_seg[BG_GC] = NULL_SEGNO; in f2fs_get_victim()
801 sbi->next_victim_seg[FG_GC] != NULL_SEGNO) { in f2fs_get_victim()
802 p.min_segno = sbi->next_victim_seg[FG_GC]; in f2fs_get_victim()
804 sbi->next_victim_seg[FG_GC] = NULL_SEGNO; in f2fs_get_victim()
811 p.min_segno = check_bg_victims(sbi); in f2fs_get_victim()
849 secno = GET_SEC_FROM_SEG(sbi, segno); in f2fs_get_victim()
851 if (sec_usage_check(sbi, secno)) in f2fs_get_victim()
855 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { in f2fs_get_victim()
861 if (get_ckpt_valid_blocks(sbi, segno, true)) in f2fs_get_victim()
869 if (!f2fs_segment_has_free_slot(sbi, segno)) in f2fs_get_victim()
881 add_victim_entry(sbi, &p, segno); in f2fs_get_victim()
885 cost = get_gc_cost(sbi, segno, &p); in f2fs_get_victim()
899 (MAIN_SECS(sbi) * sbi->segs_per_sec); in f2fs_get_victim()
906 lookup_victim_by_age(sbi, &p); in f2fs_get_victim()
907 release_victim_entry(sbi); in f2fs_get_victim()
921 secno = GET_SEC_FROM_SEG(sbi, p.min_segno); in f2fs_get_victim()
923 sbi->cur_victim_sec = secno; in f2fs_get_victim()
932 trace_f2fs_get_victim(sbi->sb, type, gc_type, &p, in f2fs_get_victim()
933 sbi->cur_victim_sec, in f2fs_get_victim()
934 prefree_segments(sbi), free_segments(sbi)); in f2fs_get_victim()
978 static int check_valid_map(struct f2fs_sb_info *sbi, in check_valid_map() argument
981 struct sit_info *sit_i = SIT_I(sbi); in check_valid_map()
986 sentry = get_seg_entry(sbi, segno); in check_valid_map()
997 static int gc_node_segment(struct f2fs_sb_info *sbi, in gc_node_segment() argument
1006 unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno); in gc_node_segment()
1008 start_addr = START_BLOCK(sbi, segno); in gc_node_segment()
1014 atomic_inc(&sbi->wb_sync_req[NODE]); in gc_node_segment()
1023 if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) in gc_node_segment()
1026 if (check_valid_map(sbi, segno, off) == 0) in gc_node_segment()
1030 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1, in gc_node_segment()
1036 f2fs_ra_node_page(sbi, nid); in gc_node_segment()
1041 node_page = f2fs_get_node_page(sbi, nid); in gc_node_segment()
1046 if (check_valid_map(sbi, segno, off) == 0) { in gc_node_segment()
1051 if (f2fs_get_node_info(sbi, nid, &ni, false)) { in gc_node_segment()
1064 stat_inc_node_blk_count(sbi, 1, gc_type); in gc_node_segment()
1071 atomic_dec(&sbi->wb_sync_req[NODE]); in gc_node_segment()
1104 static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, in is_alive() argument
1115 node_page = f2fs_get_node_page(sbi, nid); in is_alive()
1119 if (f2fs_get_node_info(sbi, nid, dni, false)) { in is_alive()
1125 f2fs_warn(sbi, "%s: valid data with mismatched node version.", in is_alive()
1127 set_sbi_flag(sbi, SBI_NEED_FSCK); in is_alive()
1130 if (f2fs_check_nid_range(sbi, dni->ino)) { in is_alive()
1144 f2fs_err(sbi, "Inconsistent blkaddr offset: base:%u, ofs_in_node:%u, max:%u, ino:%u, nid:%u", in is_alive()
1156 unsigned int segno = GET_SEGNO(sbi, blkaddr); in is_alive()
1157 unsigned long offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); in is_alive()
1159 if (unlikely(check_valid_map(sbi, segno, offset))) { in is_alive()
1160 if (!test_and_set_bit(segno, SIT_I(sbi)->invalid_segmap)) { in is_alive()
1161 f2fs_err(sbi, "mismatched blkaddr %u (source_blkaddr %u) in seg %u", in is_alive()
1163 set_sbi_flag(sbi, SBI_NEED_FSCK); in is_alive()
1174 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); in ra_data_block() local
1179 .sbi = sbi, in ra_data_block()
1197 if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr, in ra_data_block()
1200 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR); in ra_data_block()
1216 if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr, in ra_data_block()
1219 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR); in ra_data_block()
1235 fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(sbi), in ra_data_block()
1249 f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE); in ra_data_block()
1250 f2fs_update_iostat(sbi, NULL, FS_GDATA_READ_IO, F2FS_BLKSIZE); in ra_data_block()
1268 .sbi = F2FS_I_SB(inode), in move_data_block()
1284 bool lfs_mode = f2fs_lfs_mode(fio.sbi); in move_data_block()
1285 int type = fio.sbi->am.atgc_enabled && (gc_type == BG_GC) && in move_data_block()
1286 (fio.sbi->gc_mode != GC_URGENT_HIGH) ? in move_data_block()
1322 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false); in move_data_block()
1331 f2fs_down_write(&fio.sbi->io_order_lock); in move_data_block()
1333 mpage = f2fs_grab_cache_page(META_MAPPING(fio.sbi), in move_data_block()
1350 f2fs_update_iostat(fio.sbi, inode, FS_DATA_READ_IO, in move_data_block()
1352 f2fs_update_iostat(fio.sbi, NULL, FS_GDATA_READ_IO, in move_data_block()
1356 if (unlikely(mpage->mapping != META_MAPPING(fio.sbi) || in move_data_block()
1367 f2fs_allocate_data_block(fio.sbi, NULL, fio.old_blkaddr, &newaddr, in move_data_block()
1370 fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(fio.sbi), in move_data_block()
1384 f2fs_invalidate_internal_cache(fio.sbi, fio.old_blkaddr); in move_data_block()
1388 dec_page_count(fio.sbi, F2FS_DIRTY_META); in move_data_block()
1403 f2fs_update_iostat(fio.sbi, NULL, FS_GC_DATA_IO, F2FS_BLKSIZE); in move_data_block()
1411 f2fs_do_replace_block(fio.sbi, &sum, newaddr, fio.old_blkaddr, in move_data_block()
1415 f2fs_up_write(&fio.sbi->io_order_lock); in move_data_block()
1451 .sbi = F2FS_I_SB(inode), in move_data_page()
1499 static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, in gc_data_segment() argument
1503 struct super_block *sb = sbi->sb; in gc_data_segment()
1509 unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno); in gc_data_segment()
1511 start_addr = START_BLOCK(sbi, segno); in gc_data_segment()
1529 if ((gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) || in gc_data_segment()
1530 (!force_migrate && get_valid_blocks(sbi, segno, true) == in gc_data_segment()
1531 CAP_BLKS_PER_SEC(sbi))) in gc_data_segment()
1534 if (check_valid_map(sbi, segno, off) == 0) in gc_data_segment()
1538 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1, in gc_data_segment()
1544 f2fs_ra_node_page(sbi, nid); in gc_data_segment()
1549 if (!is_alive(sbi, entry, &dni, start_addr + off, &nofs)) in gc_data_segment()
1553 f2fs_ra_node_page(sbi, dni.ino); in gc_data_segment()
1576 sbi->skipped_gc_rwsem++; in gc_data_segment()
1617 sbi->skipped_gc_rwsem++; in gc_data_segment()
1622 sbi->skipped_gc_rwsem++; in gc_data_segment()
1650 stat_inc_data_blk_count(sbi, 1, gc_type); in gc_data_segment()
1660 static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim, in __get_victim() argument
1663 struct sit_info *sit_i = SIT_I(sbi); in __get_victim()
1667 ret = f2fs_get_victim(sbi, victim, gc_type, NO_CHECK_TYPE, LFS, 0); in __get_victim()
1672 static int do_garbage_collect(struct f2fs_sb_info *sbi, in do_garbage_collect() argument
1681 unsigned int end_segno = start_segno + sbi->segs_per_sec; in do_garbage_collect()
1683 unsigned char type = IS_DATASEG(get_seg_entry(sbi, segno)->type) ? in do_garbage_collect()
1688 if (__is_large_section(sbi)) in do_garbage_collect()
1689 end_segno = rounddown(end_segno, sbi->segs_per_sec); in do_garbage_collect()
1696 if (f2fs_sb_has_blkzoned(sbi)) in do_garbage_collect()
1697 end_segno -= sbi->segs_per_sec - in do_garbage_collect()
1698 f2fs_usable_segs_in_sec(sbi, segno); in do_garbage_collect()
1700 sanity_check_seg_type(sbi, get_seg_entry(sbi, segno)->type); in do_garbage_collect()
1703 if (__is_large_section(sbi)) in do_garbage_collect()
1704 f2fs_ra_meta_pages(sbi, GET_SUM_BLOCK(sbi, segno), in do_garbage_collect()
1709 sum_page = f2fs_get_sum_page(sbi, segno++); in do_garbage_collect()
1715 sum_page = find_get_page(META_MAPPING(sbi), in do_garbage_collect()
1716 GET_SUM_BLOCK(sbi, segno)); in do_garbage_collect()
1730 sum_page = find_get_page(META_MAPPING(sbi), in do_garbage_collect()
1731 GET_SUM_BLOCK(sbi, segno)); in do_garbage_collect()
1734 if (get_valid_blocks(sbi, segno, false) == 0) in do_garbage_collect()
1736 if (gc_type == BG_GC && __is_large_section(sbi) && in do_garbage_collect()
1737 migrated >= sbi->migration_granularity) in do_garbage_collect()
1739 if (!PageUptodate(sum_page) || unlikely(f2fs_cp_error(sbi))) in do_garbage_collect()
1744 f2fs_err(sbi, "Inconsistent segment (%u) type [%d, %d] in SSA and SIT", in do_garbage_collect()
1746 set_sbi_flag(sbi, SBI_NEED_FSCK); in do_garbage_collect()
1747 f2fs_stop_checkpoint(sbi, false, in do_garbage_collect()
1760 submitted += gc_node_segment(sbi, sum->entries, segno, in do_garbage_collect()
1763 submitted += gc_data_segment(sbi, sum->entries, gc_list, in do_garbage_collect()
1767 stat_inc_gc_seg_count(sbi, data_type, gc_type); in do_garbage_collect()
1768 sbi->gc_reclaimed_segs[sbi->gc_mode]++; in do_garbage_collect()
1773 get_valid_blocks(sbi, segno, false) == 0) in do_garbage_collect()
1776 if (__is_large_section(sbi)) in do_garbage_collect()
1777 sbi->next_victim_seg[gc_type] = in do_garbage_collect()
1784 f2fs_submit_merged_write(sbi, data_type); in do_garbage_collect()
1789 stat_inc_gc_sec_count(sbi, data_type, gc_type); in do_garbage_collect()
1794 int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control) in f2fs_gc() argument
1808 trace_f2fs_gc_begin(sbi->sb, gc_type, gc_control->no_bg_gc, in f2fs_gc()
1810 get_pages(sbi, F2FS_DIRTY_NODES), in f2fs_gc()
1811 get_pages(sbi, F2FS_DIRTY_DENTS), in f2fs_gc()
1812 get_pages(sbi, F2FS_DIRTY_IMETA), in f2fs_gc()
1813 free_sections(sbi), in f2fs_gc()
1814 free_segments(sbi), in f2fs_gc()
1815 reserved_segments(sbi), in f2fs_gc()
1816 prefree_segments(sbi)); in f2fs_gc()
1818 cpc.reason = __get_cp_reason(sbi); in f2fs_gc()
1820 sbi->skipped_gc_rwsem = 0; in f2fs_gc()
1821 if (unlikely(!(sbi->sb->s_flags & SB_ACTIVE))) { in f2fs_gc()
1825 if (unlikely(f2fs_cp_error(sbi))) { in f2fs_gc()
1831 if (has_not_enough_free_secs(sbi, 0, 0)) { in f2fs_gc()
1839 if (prefree_segments(sbi)) { in f2fs_gc()
1840 stat_inc_cp_call_count(sbi, TOTAL_CALL); in f2fs_gc()
1841 ret = f2fs_write_checkpoint(sbi, &cpc); in f2fs_gc()
1855 ret = __get_victim(sbi, &segno, gc_type); in f2fs_gc()
1859 f2fs_pinned_section_exists(DIRTY_I(sbi))) { in f2fs_gc()
1860 f2fs_unpin_all_sections(sbi, false); in f2fs_gc()
1866 seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type, in f2fs_gc()
1873 if (seg_freed == f2fs_usable_segs_in_sec(sbi, segno)) { in f2fs_gc()
1879 sbi->cur_victim_sec = NULL_SEGNO; in f2fs_gc()
1881 if (has_enough_free_secs(sbi, sec_freed, 0)) { in f2fs_gc()
1887 if (sbi->skipped_gc_rwsem) in f2fs_gc()
1892 stat_inc_cp_call_count(sbi, TOTAL_CALL); in f2fs_gc()
1893 ret = f2fs_write_checkpoint(sbi, &cpc); in f2fs_gc()
1896 } else if (has_enough_free_secs(sbi, 0, 0)) { in f2fs_gc()
1900 __get_secs_required(sbi, NULL, &upper_secs, NULL); in f2fs_gc()
1906 if (free_sections(sbi) <= upper_secs + NR_GC_CHECKPOINT_SECS && in f2fs_gc()
1907 prefree_segments(sbi)) { in f2fs_gc()
1908 stat_inc_cp_call_count(sbi, TOTAL_CALL); in f2fs_gc()
1909 ret = f2fs_write_checkpoint(sbi, &cpc); in f2fs_gc()
1920 SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0; in f2fs_gc()
1921 SIT_I(sbi)->last_victim[FLUSH_DEVICE] = gc_control->victim_segno; in f2fs_gc()
1924 f2fs_unpin_all_sections(sbi, true); in f2fs_gc()
1926 trace_f2fs_gc_end(sbi->sb, ret, total_freed, total_sec_freed, in f2fs_gc()
1927 get_pages(sbi, F2FS_DIRTY_NODES), in f2fs_gc()
1928 get_pages(sbi, F2FS_DIRTY_DENTS), in f2fs_gc()
1929 get_pages(sbi, F2FS_DIRTY_IMETA), in f2fs_gc()
1930 free_sections(sbi), in f2fs_gc()
1931 free_segments(sbi), in f2fs_gc()
1932 reserved_segments(sbi), in f2fs_gc()
1933 prefree_segments(sbi)); in f2fs_gc()
1935 f2fs_up_write(&sbi->gc_lock); in f2fs_gc()
1956 static void init_atgc_management(struct f2fs_sb_info *sbi) in init_atgc_management() argument
1958 struct atgc_management *am = &sbi->am; in init_atgc_management()
1960 if (test_opt(sbi, ATGC) && in init_atgc_management()
1961 SIT_I(sbi)->elapsed_time >= DEF_GC_THREAD_AGE_THRESHOLD) in init_atgc_management()
1974 void f2fs_build_gc_manager(struct f2fs_sb_info *sbi) in f2fs_build_gc_manager() argument
1976 sbi->gc_pin_file_threshold = DEF_GC_FAILED_PINNED_FILES; in f2fs_build_gc_manager()
1979 if (f2fs_is_multi_device(sbi) && !__is_large_section(sbi)) in f2fs_build_gc_manager()
1980 SIT_I(sbi)->last_victim[ALLOC_NEXT] = in f2fs_build_gc_manager()
1981 GET_SEGNO(sbi, FDEV(0).end_blk) + 1; in f2fs_build_gc_manager()
1983 init_atgc_management(sbi); in f2fs_build_gc_manager()
1986 static int free_segment_range(struct f2fs_sb_info *sbi, in free_segment_range() argument
1996 MAIN_SECS(sbi) -= secs; in free_segment_range()
1997 start = MAIN_SECS(sbi) * sbi->segs_per_sec; in free_segment_range()
1998 end = MAIN_SEGS(sbi) - 1; in free_segment_range()
2000 mutex_lock(&DIRTY_I(sbi)->seglist_lock); in free_segment_range()
2002 if (SIT_I(sbi)->last_victim[gc_mode] >= start) in free_segment_range()
2003 SIT_I(sbi)->last_victim[gc_mode] = 0; in free_segment_range()
2006 if (sbi->next_victim_seg[gc_type] >= start) in free_segment_range()
2007 sbi->next_victim_seg[gc_type] = NULL_SEGNO; in free_segment_range()
2008 mutex_unlock(&DIRTY_I(sbi)->seglist_lock); in free_segment_range()
2012 f2fs_allocate_segment_for_resize(sbi, type, start, end); in free_segment_range()
2015 for (segno = start; segno <= end; segno += sbi->segs_per_sec) { in free_segment_range()
2021 do_garbage_collect(sbi, segno, &gc_list, FG_GC, true); in free_segment_range()
2024 if (!gc_only && get_valid_blocks(sbi, segno, true)) { in free_segment_range()
2036 stat_inc_cp_call_count(sbi, TOTAL_CALL); in free_segment_range()
2037 err = f2fs_write_checkpoint(sbi, &cpc); in free_segment_range()
2041 next_inuse = find_next_inuse(FREE_I(sbi), end + 1, start); in free_segment_range()
2043 f2fs_err(sbi, "segno %u should be free but still inuse!", in free_segment_range()
2045 f2fs_bug_on(sbi, 1); in free_segment_range()
2048 MAIN_SECS(sbi) += secs; in free_segment_range()
2052 static void update_sb_metadata(struct f2fs_sb_info *sbi, int secs) in update_sb_metadata() argument
2054 struct f2fs_super_block *raw_sb = F2FS_RAW_SUPER(sbi); in update_sb_metadata()
2059 int segs = secs * sbi->segs_per_sec; in update_sb_metadata()
2061 f2fs_down_write(&sbi->sb_lock); in update_sb_metadata()
2072 (long long)segs * sbi->blocks_per_seg); in update_sb_metadata()
2073 if (f2fs_is_multi_device(sbi)) { in update_sb_metadata()
2074 int last_dev = sbi->s_ndevs - 1; in update_sb_metadata()
2082 f2fs_up_write(&sbi->sb_lock); in update_sb_metadata()
2085 static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs) in update_fs_metadata() argument
2087 int segs = secs * sbi->segs_per_sec; in update_fs_metadata()
2088 long long blks = (long long)segs * sbi->blocks_per_seg; in update_fs_metadata()
2090 le64_to_cpu(F2FS_CKPT(sbi)->user_block_count); in update_fs_metadata()
2092 SM_I(sbi)->segment_count = (int)SM_I(sbi)->segment_count + segs; in update_fs_metadata()
2093 MAIN_SEGS(sbi) = (int)MAIN_SEGS(sbi) + segs; in update_fs_metadata()
2094 MAIN_SECS(sbi) += secs; in update_fs_metadata()
2095 FREE_I(sbi)->free_sections = (int)FREE_I(sbi)->free_sections + secs; in update_fs_metadata()
2096 FREE_I(sbi)->free_segments = (int)FREE_I(sbi)->free_segments + segs; in update_fs_metadata()
2097 F2FS_CKPT(sbi)->user_block_count = cpu_to_le64(user_block_count + blks); in update_fs_metadata()
2099 if (f2fs_is_multi_device(sbi)) { in update_fs_metadata()
2100 int last_dev = sbi->s_ndevs - 1; in update_fs_metadata()
2108 div_u64(blks, sbi->blocks_per_blkz); in update_fs_metadata()
2115 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp)); in f2fs_resize_fs() local
2122 old_block_count = le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count); in f2fs_resize_fs()
2126 if (f2fs_is_multi_device(sbi)) { in f2fs_resize_fs()
2127 int last_dev = sbi->s_ndevs - 1; in f2fs_resize_fs()
2130 if (block_count + last_segs * sbi->blocks_per_seg <= in f2fs_resize_fs()
2136 div_u64_rem(block_count, BLKS_PER_SEC(sbi), &rem); in f2fs_resize_fs()
2143 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) { in f2fs_resize_fs()
2144 f2fs_err(sbi, "Should run fsck to repair first."); in f2fs_resize_fs()
2148 if (test_opt(sbi, DISABLE_CHECKPOINT)) { in f2fs_resize_fs()
2149 f2fs_err(sbi, "Checkpoint should be enabled."); in f2fs_resize_fs()
2158 secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi)); in f2fs_resize_fs()
2161 if (!f2fs_down_write_trylock(&sbi->gc_lock)) { in f2fs_resize_fs()
2167 f2fs_lock_op(sbi); in f2fs_resize_fs()
2169 spin_lock(&sbi->stat_lock); in f2fs_resize_fs()
2170 if (shrunk_blocks + valid_user_blocks(sbi) + in f2fs_resize_fs()
2171 sbi->current_reserved_blocks + sbi->unusable_block_count + in f2fs_resize_fs()
2172 F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count) in f2fs_resize_fs()
2174 spin_unlock(&sbi->stat_lock); in f2fs_resize_fs()
2179 err = free_segment_range(sbi, secs, true); in f2fs_resize_fs()
2182 f2fs_unlock_op(sbi); in f2fs_resize_fs()
2183 f2fs_up_write(&sbi->gc_lock); in f2fs_resize_fs()
2189 err = freeze_super(sbi->sb, FREEZE_HOLDER_USERSPACE); in f2fs_resize_fs()
2193 if (f2fs_readonly(sbi->sb)) { in f2fs_resize_fs()
2194 err = thaw_super(sbi->sb, FREEZE_HOLDER_USERSPACE); in f2fs_resize_fs()
2200 f2fs_down_write(&sbi->gc_lock); in f2fs_resize_fs()
2201 f2fs_down_write(&sbi->cp_global_sem); in f2fs_resize_fs()
2203 spin_lock(&sbi->stat_lock); in f2fs_resize_fs()
2204 if (shrunk_blocks + valid_user_blocks(sbi) + in f2fs_resize_fs()
2205 sbi->current_reserved_blocks + sbi->unusable_block_count + in f2fs_resize_fs()
2206 F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count) in f2fs_resize_fs()
2209 sbi->user_block_count -= shrunk_blocks; in f2fs_resize_fs()
2210 spin_unlock(&sbi->stat_lock); in f2fs_resize_fs()
2214 set_sbi_flag(sbi, SBI_IS_RESIZEFS); in f2fs_resize_fs()
2215 err = free_segment_range(sbi, secs, false); in f2fs_resize_fs()
2219 update_sb_metadata(sbi, -secs); in f2fs_resize_fs()
2221 err = f2fs_commit_super(sbi, false); in f2fs_resize_fs()
2223 update_sb_metadata(sbi, secs); in f2fs_resize_fs()
2227 update_fs_metadata(sbi, -secs); in f2fs_resize_fs()
2228 clear_sbi_flag(sbi, SBI_IS_RESIZEFS); in f2fs_resize_fs()
2229 set_sbi_flag(sbi, SBI_IS_DIRTY); in f2fs_resize_fs()
2231 stat_inc_cp_call_count(sbi, TOTAL_CALL); in f2fs_resize_fs()
2232 err = f2fs_write_checkpoint(sbi, &cpc); in f2fs_resize_fs()
2234 update_fs_metadata(sbi, secs); in f2fs_resize_fs()
2235 update_sb_metadata(sbi, secs); in f2fs_resize_fs()
2236 f2fs_commit_super(sbi, false); in f2fs_resize_fs()
2239 clear_sbi_flag(sbi, SBI_IS_RESIZEFS); in f2fs_resize_fs()
2241 set_sbi_flag(sbi, SBI_NEED_FSCK); in f2fs_resize_fs()
2242 f2fs_err(sbi, "resize_fs failed, should run fsck to repair!"); in f2fs_resize_fs()
2244 spin_lock(&sbi->stat_lock); in f2fs_resize_fs()
2245 sbi->user_block_count += shrunk_blocks; in f2fs_resize_fs()
2246 spin_unlock(&sbi->stat_lock); in f2fs_resize_fs()
2249 f2fs_up_write(&sbi->cp_global_sem); in f2fs_resize_fs()
2250 f2fs_up_write(&sbi->gc_lock); in f2fs_resize_fs()
2251 thaw_super(sbi->sb, FREEZE_HOLDER_USERSPACE); in f2fs_resize_fs()