Lines Matching defs:sbi

33 	struct f2fs_sb_info *sbi = data;
34 struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
35 wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head;
36 wait_queue_head_t *fggc_wq = &sbi->gc_thread->fggc_wq;
55 if (test_opt(sbi, GC_MERGE) && waitqueue_active(fggc_wq))
62 if (f2fs_readonly(sbi->sb)) {
63 stat_other_skip_bggc_count(sbi);
69 if (sbi->sb->s_writers.frozen >= SB_FREEZE_WRITE) {
71 stat_other_skip_bggc_count(sbi);
75 if (time_to_inject(sbi, FAULT_CHECKPOINT))
76 f2fs_stop_checkpoint(sbi, false,
79 if (!sb_start_write_trylock(sbi->sb)) {
80 stat_other_skip_bggc_count(sbi);
99 if (sbi->gc_mode == GC_URGENT_HIGH ||
100 sbi->gc_mode == GC_URGENT_MID) {
102 f2fs_down_write(&sbi->gc_lock);
107 f2fs_down_write(&sbi->gc_lock);
109 } else if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
110 stat_other_skip_bggc_count(sbi);
114 if (!is_idle(sbi, GC_TIME)) {
116 f2fs_up_write(&sbi->gc_lock);
117 stat_io_skip_bggc_count(sbi);
121 if (f2fs_sb_has_blkzoned(sbi)) {
122 if (has_enough_free_blocks(sbi,
125 f2fs_up_write(&sbi->gc_lock);
132 if (need_to_boost_gc(sbi)) {
134 if (f2fs_sb_has_blkzoned(sbi))
140 stat_inc_gc_call_count(sbi, foreground ?
143 sync_mode = (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC) ||
155 if (f2fs_gc(sbi, &gc_control)) {
168 trace_f2fs_background_gc(sbi->sb, wait_ms,
169 prefree_segments(sbi), free_segments(sbi));
172 f2fs_balance_fs_bg(sbi, true);
174 if (sbi->gc_mode != GC_NORMAL) {
175 spin_lock(&sbi->gc_remaining_trials_lock);
176 if (sbi->gc_remaining_trials) {
177 sbi->gc_remaining_trials--;
178 if (!sbi->gc_remaining_trials)
179 sbi->gc_mode = GC_NORMAL;
181 spin_unlock(&sbi->gc_remaining_trials_lock);
183 sb_end_write(sbi->sb);
189 int f2fs_start_gc_thread(struct f2fs_sb_info *sbi)
192 dev_t dev = sbi->sb->s_bdev->bd_dev;
194 gc_th = f2fs_kmalloc(sbi, sizeof(struct f2fs_gc_kthread), GFP_KERNEL);
201 if (f2fs_sb_has_blkzoned(sbi)) {
217 sbi->gc_thread = gc_th;
218 init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
219 init_waitqueue_head(&sbi->gc_thread->fggc_wq);
220 sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi,
226 sbi->gc_thread = NULL;
233 void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi)
235 struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
242 sbi->gc_thread = NULL;
245 static int select_gc_type(struct f2fs_sb_info *sbi, int gc_type)
250 if (sbi->am.atgc_enabled)
258 switch (sbi->gc_mode) {
274 static void select_policy(struct f2fs_sb_info *sbi, int gc_type,
277 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
290 p->gc_mode = select_gc_type(sbi, gc_type);
291 p->ofs_unit = SEGS_PER_SEC(sbi);
292 if (__is_large_section(sbi)) {
295 0, MAIN_SECS(sbi));
307 (sbi->gc_mode != GC_URGENT_HIGH) &&
309 p->max_search > sbi->max_victim_search)
310 p->max_search = sbi->max_victim_search;
313 if (f2fs_need_rand_seg(sbi))
314 p->offset = get_random_u32_below(MAIN_SECS(sbi) *
315 SEGS_PER_SEC(sbi));
319 p->offset = SIT_I(sbi)->last_victim[p->gc_mode];
322 static unsigned int get_max_cost(struct f2fs_sb_info *sbi,
327 return BLKS_PER_SEG(sbi);
333 return SEGS_TO_BLKS(sbi, 2 * p->ofs_unit);
342 static unsigned int check_bg_victims(struct f2fs_sb_info *sbi)
344 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
352 for_each_set_bit(secno, dirty_i->victim_secmap, MAIN_SECS(sbi)) {
353 if (sec_usage_check(sbi, secno))
356 return GET_SEG_FROM_SEC(sbi, secno);
361 static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno)
363 struct sit_info *sit_i = SIT_I(sbi);
364 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
365 unsigned int start = GET_SEG_FROM_SEC(sbi, secno);
371 unsigned int usable_segs_per_sec = f2fs_usable_segs_in_sec(sbi);
374 mtime += get_seg_entry(sbi, start + i)->mtime;
375 vblocks = get_valid_blocks(sbi, segno, true);
380 u = BLKS_TO_SEGS(sbi, vblocks * 100);
394 static inline unsigned int get_gc_cost(struct f2fs_sb_info *sbi,
398 return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
400 if (p->one_time_gc && (get_valid_blocks(sbi, segno, true) >=
401 CAP_BLKS_PER_SEC(sbi) * sbi->gc_thread->valid_thresh_ratio /
407 return get_valid_blocks(sbi, segno, true);
409 return get_cb_cost(sbi, segno);
411 f2fs_bug_on(sbi, 1);
427 static bool f2fs_check_victim_tree(struct f2fs_sb_info *sbi,
443 f2fs_info(sbi, "broken victim_rbtree, "
454 static struct victim_entry *__lookup_victim_entry(struct f2fs_sb_info *sbi,
457 struct atgc_management *am = &sbi->am;
472 static struct victim_entry *__create_victim_entry(struct f2fs_sb_info *sbi,
475 struct atgc_management *am = &sbi->am;
489 static void __insert_victim_entry(struct f2fs_sb_info *sbi,
492 struct atgc_management *am = &sbi->am;
512 ve = __create_victim_entry(sbi, mtime, segno);
518 static void add_victim_entry(struct f2fs_sb_info *sbi,
521 struct sit_info *sit_i = SIT_I(sbi);
522 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
523 unsigned int start = GET_SEG_FROM_SEC(sbi, secno);
527 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
529 get_valid_blocks(sbi, segno, true) == 0)
533 for (i = 0; i < SEGS_PER_SEC(sbi); i++)
534 mtime += get_seg_entry(sbi, start + i)->mtime;
535 mtime = div_u64(mtime, SEGS_PER_SEC(sbi));
551 __insert_victim_entry(sbi, mtime, segno);
554 static void atgc_lookup_victim(struct f2fs_sb_info *sbi,
557 struct sit_info *sit_i = SIT_I(sbi);
558 struct atgc_management *am = &sbi->am;
566 unsigned int sec_blocks = CAP_BLKS_PER_SEC(sbi);
598 vblocks = get_valid_blocks(sbi, ve->segno, true);
599 f2fs_bug_on(sbi, !vblocks || vblocks == sec_blocks);
605 f2fs_bug_on(sbi, age + u >= UINT_MAX);
627 static void atssr_lookup_victim(struct f2fs_sb_info *sbi,
630 struct sit_info *sit_i = SIT_I(sbi);
631 struct atgc_management *am = &sbi->am;
648 ve = __lookup_victim_entry(sbi, p->age);
661 vblocks = get_seg_entry(sbi, ve->segno)->ckpt_valid_blocks;
662 f2fs_bug_on(sbi, !vblocks);
665 if (vblocks == BLKS_PER_SEG(sbi))
691 static void lookup_victim_by_age(struct f2fs_sb_info *sbi,
694 f2fs_bug_on(sbi, !f2fs_check_victim_tree(sbi, &sbi->am.root));
697 atgc_lookup_victim(sbi, p);
699 atssr_lookup_victim(sbi, p);
701 f2fs_bug_on(sbi, 1);
704 static void release_victim_entry(struct f2fs_sb_info *sbi)
706 struct atgc_management *am = &sbi->am;
717 f2fs_bug_on(sbi, am->victim_count);
718 f2fs_bug_on(sbi, !list_empty(&am->victim_list));
721 static bool f2fs_pin_section(struct f2fs_sb_info *sbi, unsigned int segno)
723 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
724 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
746 static void f2fs_unpin_all_sections(struct f2fs_sb_info *sbi, bool enable)
748 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
750 if (f2fs_pinned_section_exists(DIRTY_I(sbi))) {
751 memset(DIRTY_I(sbi)->pinned_secmap, 0, bitmap_size);
752 DIRTY_I(sbi)->pinned_secmap_cnt = 0;
754 DIRTY_I(sbi)->enable_pin_section = enable;
777 int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
781 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
782 struct sit_info *sm = SIT_I(sbi);
791 last_segment = MAIN_SECS(sbi) * SEGS_PER_SEC(sbi);
795 p.age_threshold = sbi->am.age_threshold;
799 select_policy(sbi, gc_type, type, &p);
802 p.min_cost = get_max_cost(sbi, &p);
808 SIT_I(sbi)->dirty_min_mtime = ULLONG_MAX;
811 if (!get_valid_blocks(sbi, *result, false)) {
816 if (sec_usage_check(sbi, GET_SEC_FROM_SEG(sbi, *result)))
827 if (__is_large_section(sbi) && p.alloc_mode == LFS) {
828 if (sbi->next_victim_seg[BG_GC] != NULL_SEGNO) {
829 p.min_segno = sbi->next_victim_seg[BG_GC];
831 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
835 sbi->next_victim_seg[FG_GC] != NULL_SEGNO) {
836 p.min_segno = sbi->next_victim_seg[FG_GC];
838 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
845 p.min_segno = check_bg_victims(sbi);
883 secno = GET_SEC_FROM_SEG(sbi, segno);
885 if (sec_usage_check(sbi, secno))
889 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
895 if (get_ckpt_valid_blocks(sbi, segno, true))
903 if (!f2fs_segment_has_free_slot(sbi, segno))
915 add_victim_entry(sbi, &p, segno);
919 cost = get_gc_cost(sbi, segno, &p);
933 (MAIN_SECS(sbi) * SEGS_PER_SEC(sbi));
940 lookup_victim_by_age(sbi, &p);
941 release_victim_entry(sbi);
955 secno = GET_SEC_FROM_SEG(sbi, p.min_segno);
957 sbi->cur_victim_sec = secno;
966 trace_f2fs_get_victim(sbi->sb, type, gc_type, &p,
967 sbi->cur_victim_sec,
968 prefree_segments(sbi), free_segments(sbi));
1012 static int check_valid_map(struct f2fs_sb_info *sbi,
1015 struct sit_info *sit_i = SIT_I(sbi);
1020 sentry = get_seg_entry(sbi, segno);
1031 static int gc_node_segment(struct f2fs_sb_info *sbi,
1040 unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
1042 start_addr = START_BLOCK(sbi, segno);
1048 atomic_inc(&sbi->wb_sync_req[NODE]);
1057 if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0))
1060 if (check_valid_map(sbi, segno, off) == 0)
1064 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
1070 f2fs_ra_node_page(sbi, nid);
1075 node_page = f2fs_get_node_page(sbi, nid);
1080 if (check_valid_map(sbi, segno, off) == 0) {
1085 if (f2fs_get_node_info(sbi, nid, &ni, false)) {
1098 stat_inc_node_blk_count(sbi, 1, gc_type);
1105 atomic_dec(&sbi->wb_sync_req[NODE]);
1138 static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
1149 node_page = f2fs_get_node_page(sbi, nid);
1153 if (f2fs_get_node_info(sbi, nid, dni, false)) {
1159 f2fs_warn(sbi, "%s: valid data with mismatched node version.",
1161 set_sbi_flag(sbi, SBI_NEED_FSCK);
1164 if (f2fs_check_nid_range(sbi, dni->ino)) {
1178 f2fs_err(sbi, "Inconsistent blkaddr offset: base:%u, ofs_in_node:%u, max:%u, ino:%u, nid:%u",
1190 unsigned int segno = GET_SEGNO(sbi, blkaddr);
1191 unsigned long offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1193 if (unlikely(check_valid_map(sbi, segno, offset))) {
1194 if (!test_and_set_bit(segno, SIT_I(sbi)->invalid_segmap)) {
1195 f2fs_err(sbi, "mismatched blkaddr %u (source_blkaddr %u) in seg %u",
1197 set_sbi_flag(sbi, SBI_NEED_FSCK);
1208 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1214 .sbi = sbi,
1231 if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr,
1249 if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr,
1267 fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(sbi),
1281 f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE);
1282 f2fs_update_iostat(sbi, NULL, FS_GDATA_READ_IO, F2FS_BLKSIZE);
1302 .sbi = F2FS_I_SB(inode),
1317 bool lfs_mode = f2fs_lfs_mode(fio.sbi);
1318 int type = fio.sbi->am.atgc_enabled && (gc_type == BG_GC) &&
1319 (fio.sbi->gc_mode != GC_URGENT_HIGH) ?
1355 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
1364 f2fs_down_write(&fio.sbi->io_order_lock);
1366 mpage = f2fs_grab_cache_page(META_MAPPING(fio.sbi),
1383 f2fs_update_iostat(fio.sbi, inode, FS_DATA_READ_IO,
1385 f2fs_update_iostat(fio.sbi, NULL, FS_GDATA_READ_IO,
1389 if (unlikely(mpage->mapping != META_MAPPING(fio.sbi) ||
1400 err = f2fs_allocate_data_block(fio.sbi, NULL, fio.old_blkaddr, &newaddr,
1408 fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(fio.sbi),
1422 f2fs_invalidate_internal_cache(fio.sbi, fio.old_blkaddr);
1426 dec_page_count(fio.sbi, F2FS_DIRTY_META);
1435 f2fs_update_iostat(fio.sbi, NULL, FS_GC_DATA_IO, F2FS_BLKSIZE);
1443 f2fs_do_replace_block(fio.sbi, &sum, newaddr, fio.old_blkaddr,
1447 f2fs_up_write(&fio.sbi->io_order_lock);
1483 .sbi = F2FS_I_SB(inode),
1531 static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
1535 struct super_block *sb = sbi->sb;
1541 unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
1543 start_addr = START_BLOCK(sbi, segno);
1561 if ((gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) ||
1562 (!force_migrate && get_valid_blocks(sbi, segno, true) ==
1563 CAP_BLKS_PER_SEC(sbi)))
1566 if (check_valid_map(sbi, segno, off) == 0)
1570 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
1576 f2fs_ra_node_page(sbi, nid);
1581 if (!is_alive(sbi, entry, &dni, start_addr + off, &nofs))
1585 f2fs_ra_node_page(sbi, dni.ino);
1606 set_sbi_flag(sbi, SBI_NEED_FSCK);
1607 f2fs_err_ratelimited(sbi,
1623 sbi->skipped_gc_rwsem++;
1664 sbi->skipped_gc_rwsem++;
1669 sbi->skipped_gc_rwsem++;
1697 stat_inc_data_blk_count(sbi, 1, gc_type);
1707 static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim,
1710 struct sit_info *sit_i = SIT_I(sbi);
1714 ret = f2fs_get_victim(sbi, victim, gc_type, NO_CHECK_TYPE,
1720 static int do_garbage_collect(struct f2fs_sb_info *sbi,
1729 unsigned int end_segno = start_segno + SEGS_PER_SEC(sbi);
1732 unsigned char type = IS_DATASEG(get_seg_entry(sbi, segno)->type) ?
1737 if (__is_large_section(sbi)) {
1738 sec_end_segno = rounddown(end_segno, SEGS_PER_SEC(sbi));
1746 if (f2fs_sb_has_blkzoned(sbi))
1747 sec_end_segno -= SEGS_PER_SEC(sbi) -
1748 f2fs_usable_segs_in_sec(sbi);
1752 sbi->migration_window_granularity;
1754 if (f2fs_sb_has_blkzoned(sbi) &&
1755 !has_enough_free_blocks(sbi,
1756 sbi->gc_thread->boost_zoned_gc_percent))
1767 sanity_check_seg_type(sbi, get_seg_entry(sbi, segno)->type);
1770 if (__is_large_section(sbi))
1771 f2fs_ra_meta_pages(sbi, GET_SUM_BLOCK(sbi, segno),
1776 sum_page = f2fs_get_sum_page(sbi, segno++);
1782 sum_page = find_get_page(META_MAPPING(sbi),
1783 GET_SUM_BLOCK(sbi, segno));
1797 sum_page = find_get_page(META_MAPPING(sbi),
1798 GET_SUM_BLOCK(sbi, segno));
1801 if (get_valid_blocks(sbi, segno, false) == 0)
1803 if (gc_type == BG_GC && __is_large_section(sbi) &&
1804 migrated >= sbi->migration_granularity)
1806 if (!PageUptodate(sum_page) || unlikely(f2fs_cp_error(sbi)))
1811 f2fs_err(sbi, "Inconsistent segment (%u) type [%d, %d] in SSA and SIT",
1813 f2fs_stop_checkpoint(sbi, false,
1826 submitted += gc_node_segment(sbi, sum->entries, segno,
1829 submitted += gc_data_segment(sbi, sum->entries, gc_list,
1833 stat_inc_gc_seg_count(sbi, data_type, gc_type);
1834 sbi->gc_reclaimed_segs[sbi->gc_mode]++;
1839 get_valid_blocks(sbi, segno, false) == 0)
1842 if (__is_large_section(sbi))
1843 sbi->next_victim_seg[gc_type] =
1851 f2fs_submit_merged_write(sbi, data_type);
1856 stat_inc_gc_sec_count(sbi, data_type, gc_type);
1861 int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
1875 trace_f2fs_gc_begin(sbi->sb, gc_type, gc_control->no_bg_gc,
1877 get_pages(sbi, F2FS_DIRTY_NODES),
1878 get_pages(sbi, F2FS_DIRTY_DENTS),
1879 get_pages(sbi, F2FS_DIRTY_IMETA),
1880 free_sections(sbi),
1881 free_segments(sbi),
1882 reserved_segments(sbi),
1883 prefree_segments(sbi));
1885 cpc.reason = __get_cp_reason(sbi);
1887 sbi->skipped_gc_rwsem = 0;
1888 if (unlikely(!(sbi->sb->s_flags & SB_ACTIVE))) {
1892 if (unlikely(f2fs_cp_error(sbi))) {
1898 if (has_not_enough_free_secs(sbi, 0, 0)) {
1906 if (prefree_segments(sbi)) {
1907 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1908 ret = f2fs_write_checkpoint(sbi, &cpc);
1922 ret = __get_victim(sbi, &segno, gc_type, gc_control->one_time);
1926 f2fs_pinned_section_exists(DIRTY_I(sbi))) {
1927 f2fs_unpin_all_sections(sbi, false);
1933 seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
1941 if (seg_freed == f2fs_usable_segs_in_sec(sbi)) {
1950 sbi->cur_victim_sec = NULL_SEGNO;
1952 if (has_enough_free_secs(sbi, sec_freed, 0)) {
1958 if (sbi->skipped_gc_rwsem)
1963 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1964 ret = f2fs_write_checkpoint(sbi, &cpc);
1967 } else if (has_enough_free_secs(sbi, 0, 0)) {
1971 __get_secs_required(sbi, NULL, &upper_secs, NULL);
1977 if (free_sections(sbi) <= upper_secs + NR_GC_CHECKPOINT_SECS &&
1978 prefree_segments(sbi)) {
1979 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1980 ret = f2fs_write_checkpoint(sbi, &cpc);
1991 SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0;
1992 SIT_I(sbi)->last_victim[FLUSH_DEVICE] = gc_control->victim_segno;
1995 f2fs_unpin_all_sections(sbi, true);
1997 trace_f2fs_gc_end(sbi->sb, ret, total_freed, total_sec_freed,
1998 get_pages(sbi, F2FS_DIRTY_NODES),
1999 get_pages(sbi, F2FS_DIRTY_DENTS),
2000 get_pages(sbi, F2FS_DIRTY_IMETA),
2001 free_sections(sbi),
2002 free_segments(sbi),
2003 reserved_segments(sbi),
2004 prefree_segments(sbi));
2006 f2fs_up_write(&sbi->gc_lock);
2027 static void init_atgc_management(struct f2fs_sb_info *sbi)
2029 struct atgc_management *am = &sbi->am;
2031 if (test_opt(sbi, ATGC) &&
2032 SIT_I(sbi)->elapsed_time >= DEF_GC_THREAD_AGE_THRESHOLD)
2045 void f2fs_build_gc_manager(struct f2fs_sb_info *sbi)
2047 sbi->gc_pin_file_threshold = DEF_GC_FAILED_PINNED_FILES;
2050 if (f2fs_is_multi_device(sbi) && !__is_large_section(sbi))
2051 SIT_I(sbi)->last_victim[ALLOC_NEXT] =
2052 GET_SEGNO(sbi, FDEV(0).end_blk) + 1;
2054 init_atgc_management(sbi);
2057 int f2fs_gc_range(struct f2fs_sb_info *sbi,
2064 if (unlikely(f2fs_cp_error(sbi)))
2067 for (segno = start_seg; segno <= end_seg; segno += SEGS_PER_SEC(sbi)) {
2073 do_garbage_collect(sbi, segno, &gc_list, FG_GC, true, false);
2076 if (!dry_run && get_valid_blocks(sbi, segno, true))
2079 !get_valid_blocks(sbi, segno, true) && --gc_secs == 0)
2089 static int free_segment_range(struct f2fs_sb_info *sbi,
2099 MAIN_SECS(sbi) -= secs;
2100 start = MAIN_SECS(sbi) * SEGS_PER_SEC(sbi);
2101 end = MAIN_SEGS(sbi) - 1;
2103 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2105 if (SIT_I(sbi)->last_victim[gc_mode] >= start)
2106 SIT_I(sbi)->last_victim[gc_mode] = 0;
2109 if (sbi->next_victim_seg[gc_type] >= start)
2110 sbi->next_victim_seg[gc_type] = NULL_SEGNO;
2111 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2115 err = f2fs_allocate_segment_for_resize(sbi, type, start, end);
2121 err = f2fs_gc_range(sbi, start, end, dry_run, 0);
2125 stat_inc_cp_call_count(sbi, TOTAL_CALL);
2126 err = f2fs_write_checkpoint(sbi, &cpc);
2130 next_inuse = find_next_inuse(FREE_I(sbi), end + 1, start);
2132 f2fs_err(sbi, "segno %u should be free but still inuse!",
2134 f2fs_bug_on(sbi, 1);
2137 MAIN_SECS(sbi) += secs;
2141 static void update_sb_metadata(struct f2fs_sb_info *sbi, int secs)
2143 struct f2fs_super_block *raw_sb = F2FS_RAW_SUPER(sbi);
2148 int segs = secs * SEGS_PER_SEC(sbi);
2150 f2fs_down_write(&sbi->sb_lock);
2161 (long long)SEGS_TO_BLKS(sbi, segs));
2162 if (f2fs_is_multi_device(sbi)) {
2163 int last_dev = sbi->s_ndevs - 1;
2171 f2fs_up_write(&sbi->sb_lock);
2174 static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
2176 int segs = secs * SEGS_PER_SEC(sbi);
2177 long long blks = SEGS_TO_BLKS(sbi, segs);
2179 le64_to_cpu(F2FS_CKPT(sbi)->user_block_count);
2181 SM_I(sbi)->segment_count = (int)SM_I(sbi)->segment_count + segs;
2182 MAIN_SEGS(sbi) = (int)MAIN_SEGS(sbi) + segs;
2183 MAIN_SECS(sbi) += secs;
2184 FREE_I(sbi)->free_sections = (int)FREE_I(sbi)->free_sections + secs;
2185 FREE_I(sbi)->free_segments = (int)FREE_I(sbi)->free_segments + segs;
2186 F2FS_CKPT(sbi)->user_block_count = cpu_to_le64(user_block_count + blks);
2188 if (f2fs_is_multi_device(sbi)) {
2189 int last_dev = sbi->s_ndevs - 1;
2197 div_u64(blks, sbi->blocks_per_blkz);
2204 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2211 old_block_count = le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count);
2215 if (f2fs_is_multi_device(sbi)) {
2216 int last_dev = sbi->s_ndevs - 1;
2219 if (block_count + SEGS_TO_BLKS(sbi, last_segs) <=
2225 div_u64_rem(block_count, BLKS_PER_SEC(sbi), &rem);
2232 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
2233 f2fs_err(sbi, "Should run fsck to repair first.");
2237 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2238 f2fs_err(sbi, "Checkpoint should be enabled.");
2247 secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi));
2250 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2256 f2fs_lock_op(sbi);
2258 spin_lock(&sbi->stat_lock);
2259 if (shrunk_blocks + valid_user_blocks(sbi) +
2260 sbi->current_reserved_blocks + sbi->unusable_block_count +
2261 F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count)
2263 spin_unlock(&sbi->stat_lock);
2268 err = free_segment_range(sbi, secs, true);
2271 f2fs_unlock_op(sbi);
2272 f2fs_up_write(&sbi->gc_lock);
2278 err = freeze_super(sbi->sb, FREEZE_HOLDER_USERSPACE);
2282 if (f2fs_readonly(sbi->sb)) {
2283 err = thaw_super(sbi->sb, FREEZE_HOLDER_USERSPACE);
2289 f2fs_down_write(&sbi->gc_lock);
2290 f2fs_down_write(&sbi->cp_global_sem);
2292 spin_lock(&sbi->stat_lock);
2293 if (shrunk_blocks + valid_user_blocks(sbi) +
2294 sbi->current_reserved_blocks + sbi->unusable_block_count +
2295 F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count)
2298 sbi->user_block_count -= shrunk_blocks;
2299 spin_unlock(&sbi->stat_lock);
2303 set_sbi_flag(sbi, SBI_IS_RESIZEFS);
2304 err = free_segment_range(sbi, secs, false);
2308 update_sb_metadata(sbi, -secs);
2310 err = f2fs_commit_super(sbi, false);
2312 update_sb_metadata(sbi, secs);
2316 update_fs_metadata(sbi, -secs);
2317 clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
2318 set_sbi_flag(sbi, SBI_IS_DIRTY);
2320 stat_inc_cp_call_count(sbi, TOTAL_CALL);
2321 err = f2fs_write_checkpoint(sbi, &cpc);
2323 update_fs_metadata(sbi, secs);
2324 update_sb_metadata(sbi, secs);
2325 f2fs_commit_super(sbi, false);
2328 clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
2330 set_sbi_flag(sbi, SBI_NEED_FSCK);
2331 f2fs_err(sbi, "resize_fs failed, should run fsck to repair!");
2333 spin_lock(&sbi->stat_lock);
2334 sbi->user_block_count += shrunk_blocks;
2335 spin_unlock(&sbi->stat_lock);
2338 f2fs_up_write(&sbi->cp_global_sem);
2339 f2fs_up_write(&sbi->gc_lock);
2340 thaw_super(sbi->sb, FREEZE_HOLDER_USERSPACE);