Skip to content

Commit d147ea4

Browse files
author
Jaegeuk Kim
committed
f2fs: introduce f2fs_gc_control to consolidate f2fs_gc parameters
No functional change. - remove checkpoint=disable check for f2fs_write_checkpoint - get sec_freed all the time Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 64e3ed0 commit d147ea4

6 files changed

Lines changed: 98 additions & 51 deletions

File tree

fs/f2fs/f2fs.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,14 @@ struct atgc_management {
12651265
unsigned long long age_threshold; /* age threshold */
12661266
};
12671267

1268+
struct f2fs_gc_control {
1269+
unsigned int victim_segno; /* target victim segment number */
1270+
int init_gc_type; /* FG_GC or BG_GC */
1271+
bool no_bg_gc; /* check the space and stop bg_gc */
1272+
bool should_migrate_blocks; /* should migrate blocks */
1273+
bool err_gc_skipped; /* return EAGAIN if GC skipped */
1274+
};
1275+
12681276
/* For s_flag in struct f2fs_sb_info */
12691277
enum {
12701278
SBI_IS_DIRTY, /* dirty flag for checkpoint */
@@ -3761,8 +3769,7 @@ extern const struct iomap_ops f2fs_iomap_ops;
37613769
int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
37623770
void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
37633771
block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3764-
int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background, bool force,
3765-
unsigned int segno);
3772+
int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control);
37663773
void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
37673774
int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
37683775
int __init f2fs_create_garbage_collection_cache(void);

fs/f2fs/file.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,10 @@ static int expand_inode_data(struct inode *inode, loff_t offset,
16471647
struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
16481648
.m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
16491649
.m_may_create = true };
1650+
struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1651+
.init_gc_type = FG_GC,
1652+
.should_migrate_blocks = false,
1653+
.err_gc_skipped = true };
16501654
pgoff_t pg_start, pg_end;
16511655
loff_t new_size = i_size_read(inode);
16521656
loff_t off_end;
@@ -1684,7 +1688,7 @@ static int expand_inode_data(struct inode *inode, loff_t offset,
16841688
if (has_not_enough_free_secs(sbi, 0,
16851689
GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
16861690
f2fs_down_write(&sbi->gc_lock);
1687-
err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
1691+
err = f2fs_gc(sbi, &gc_control);
16881692
if (err && err != -ENODATA)
16891693
goto out_err;
16901694
}
@@ -2344,6 +2348,9 @@ static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
23442348
{
23452349
struct inode *inode = file_inode(filp);
23462350
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2351+
struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2352+
.no_bg_gc = false,
2353+
.should_migrate_blocks = false };
23472354
__u32 sync;
23482355
int ret;
23492356

@@ -2369,7 +2376,9 @@ static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
23692376
f2fs_down_write(&sbi->gc_lock);
23702377
}
23712378

2372-
ret = f2fs_gc(sbi, sync, true, false, NULL_SEGNO);
2379+
gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2380+
gc_control.err_gc_skipped = sync;
2381+
ret = f2fs_gc(sbi, &gc_control);
23732382
out:
23742383
mnt_drop_write_file(filp);
23752384
return ret;
@@ -2378,6 +2387,11 @@ static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
23782387
static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
23792388
{
23802389
struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2390+
struct f2fs_gc_control gc_control = {
2391+
.init_gc_type = range->sync ? FG_GC : BG_GC,
2392+
.no_bg_gc = false,
2393+
.should_migrate_blocks = false,
2394+
.err_gc_skipped = range->sync };
23812395
u64 end;
23822396
int ret;
23832397

@@ -2405,8 +2419,8 @@ static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
24052419
f2fs_down_write(&sbi->gc_lock);
24062420
}
24072421

2408-
ret = f2fs_gc(sbi, range->sync, true, false,
2409-
GET_SEGNO(sbi, range->start));
2422+
gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2423+
ret = f2fs_gc(sbi, &gc_control);
24102424
if (ret) {
24112425
if (ret == -EBUSY)
24122426
ret = -EAGAIN;
@@ -2820,6 +2834,10 @@ static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
28202834
unsigned int start_segno = 0, end_segno = 0;
28212835
unsigned int dev_start_segno = 0, dev_end_segno = 0;
28222836
struct f2fs_flush_device range;
2837+
struct f2fs_gc_control gc_control = {
2838+
.init_gc_type = FG_GC,
2839+
.should_migrate_blocks = true,
2840+
.err_gc_skipped = true };
28232841
int ret;
28242842

28252843
if (!capable(CAP_SYS_ADMIN))
@@ -2863,7 +2881,9 @@ static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
28632881
sm->last_victim[GC_CB] = end_segno + 1;
28642882
sm->last_victim[GC_GREEDY] = end_segno + 1;
28652883
sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2866-
ret = f2fs_gc(sbi, true, true, true, start_segno);
2884+
2885+
gc_control.victim_segno = start_segno;
2886+
ret = f2fs_gc(sbi, &gc_control);
28672887
if (ret == -EAGAIN)
28682888
ret = 0;
28692889
else if (ret < 0)

fs/f2fs/gc.c

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ static int gc_thread_func(void *data)
3535
wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head;
3636
wait_queue_head_t *fggc_wq = &sbi->gc_thread->fggc_wq;
3737
unsigned int wait_ms;
38+
struct f2fs_gc_control gc_control = {
39+
.victim_segno = NULL_SEGNO,
40+
.should_migrate_blocks = false };
3841

3942
wait_ms = gc_th->min_sleep_time;
4043

@@ -141,8 +144,12 @@ static int gc_thread_func(void *data)
141144
if (foreground)
142145
sync_mode = false;
143146

147+
gc_control.init_gc_type = sync_mode ? FG_GC : BG_GC;
148+
gc_control.no_bg_gc = foreground;
149+
gc_control.err_gc_skipped = sync_mode;
150+
144151
/* if return value is not zero, no victim was selected */
145-
if (f2fs_gc(sbi, sync_mode, !foreground, false, NULL_SEGNO))
152+
if (f2fs_gc(sbi, &gc_control))
146153
wait_ms = gc_th->no_gc_sleep_time;
147154

148155
if (foreground)
@@ -1740,21 +1747,20 @@ static int do_garbage_collect(struct f2fs_sb_info *sbi,
17401747
return seg_freed;
17411748
}
17421749

1743-
int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
1744-
bool background, bool force, unsigned int segno)
1750+
int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
17451751
{
1746-
int gc_type = sync ? FG_GC : BG_GC;
1752+
int gc_type = gc_control->init_gc_type;
1753+
unsigned int segno = gc_control->victim_segno;
17471754
int sec_freed = 0, seg_freed = 0, total_freed = 0;
17481755
int ret = 0;
17491756
struct cp_control cpc;
1750-
unsigned int init_segno = segno;
17511757
struct gc_inode_list gc_list = {
17521758
.ilist = LIST_HEAD_INIT(gc_list.ilist),
17531759
.iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS),
17541760
};
17551761
unsigned int skipped_round = 0, round = 0;
17561762

1757-
trace_f2fs_gc_begin(sbi->sb, sync, background,
1763+
trace_f2fs_gc_begin(sbi->sb, gc_type, gc_control->no_bg_gc,
17581764
get_pages(sbi, F2FS_DIRTY_NODES),
17591765
get_pages(sbi, F2FS_DIRTY_DENTS),
17601766
get_pages(sbi, F2FS_DIRTY_IMETA),
@@ -1781,8 +1787,7 @@ int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
17811787
* threshold, we can make them free by checkpoint. Then, we
17821788
* secure free segments which doesn't need fggc any more.
17831789
*/
1784-
if (prefree_segments(sbi) &&
1785-
!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
1790+
if (prefree_segments(sbi)) {
17861791
ret = f2fs_write_checkpoint(sbi, &cpc);
17871792
if (ret)
17881793
goto stop;
@@ -1792,7 +1797,7 @@ int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
17921797
}
17931798

17941799
/* f2fs_balance_fs doesn't need to do BG_GC in critical path. */
1795-
if (gc_type == BG_GC && !background) {
1800+
if (gc_type == BG_GC && gc_control->no_bg_gc) {
17961801
ret = -EINVAL;
17971802
goto stop;
17981803
}
@@ -1808,45 +1813,48 @@ int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
18081813
goto stop;
18091814
}
18101815

1811-
seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type, force);
1812-
if (gc_type == FG_GC &&
1813-
seg_freed == f2fs_usable_segs_in_sec(sbi, segno))
1814-
sec_freed++;
1816+
seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
1817+
gc_control->should_migrate_blocks);
18151818
total_freed += seg_freed;
18161819

1817-
if (gc_type == FG_GC) {
1818-
if (sbi->skipped_gc_rwsem)
1819-
skipped_round++;
1820-
round++;
1821-
}
1820+
if (seg_freed == f2fs_usable_segs_in_sec(sbi, segno))
1821+
sec_freed++;
18221822

18231823
if (gc_type == FG_GC)
18241824
sbi->cur_victim_sec = NULL_SEGNO;
18251825

1826-
if (sync)
1826+
if (gc_control->init_gc_type == FG_GC)
18271827
goto stop;
18281828

1829-
if (!has_not_enough_free_secs(sbi, sec_freed, 0))
1829+
if (!has_not_enough_free_secs(sbi,
1830+
(gc_type == FG_GC) ? sec_freed : 0, 0))
18301831
goto stop;
18311832

1832-
if (skipped_round <= MAX_SKIP_GC_COUNT || skipped_round * 2 < round) {
1833-
1834-
/* Write checkpoint to reclaim prefree segments */
1835-
if (free_sections(sbi) < NR_CURSEG_PERSIST_TYPE &&
1836-
prefree_segments(sbi) &&
1837-
!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
1833+
/* FG_GC stops GC by skip_count */
1834+
if (gc_type == FG_GC) {
1835+
if (sbi->skipped_gc_rwsem)
1836+
skipped_round++;
1837+
round++;
1838+
if (skipped_round > MAX_SKIP_GC_COUNT &&
1839+
skipped_round * 2 >= round) {
18381840
ret = f2fs_write_checkpoint(sbi, &cpc);
1839-
if (ret)
1840-
goto stop;
1841+
goto stop;
18411842
}
1842-
segno = NULL_SEGNO;
1843-
goto gc_more;
18441843
}
1845-
if (gc_type == FG_GC && !is_sbi_flag_set(sbi, SBI_CP_DISABLED))
1844+
1845+
/* Write checkpoint to reclaim prefree segments */
1846+
if (free_sections(sbi) < NR_CURSEG_PERSIST_TYPE &&
1847+
prefree_segments(sbi)) {
18461848
ret = f2fs_write_checkpoint(sbi, &cpc);
1849+
if (ret)
1850+
goto stop;
1851+
}
1852+
segno = NULL_SEGNO;
1853+
goto gc_more;
1854+
18471855
stop:
18481856
SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0;
1849-
SIT_I(sbi)->last_victim[FLUSH_DEVICE] = init_segno;
1857+
SIT_I(sbi)->last_victim[FLUSH_DEVICE] = gc_control->victim_segno;
18501858

18511859
if (gc_type == FG_GC)
18521860
f2fs_unpin_all_sections(sbi, true);
@@ -1864,7 +1872,7 @@ int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
18641872

18651873
put_gc_inode(&gc_list);
18661874

1867-
if (sync && !ret)
1875+
if (gc_control->err_gc_skipped && !ret)
18681876
ret = sec_freed ? 0 : -EAGAIN;
18691877
return ret;
18701878
}

fs/f2fs/segment.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,14 @@ void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
399399
io_schedule();
400400
finish_wait(&sbi->gc_thread->fggc_wq, &wait);
401401
} else {
402+
struct f2fs_gc_control gc_control = {
403+
.victim_segno = NULL_SEGNO,
404+
.init_gc_type = BG_GC,
405+
.no_bg_gc = true,
406+
.should_migrate_blocks = false,
407+
.err_gc_skipped = false };
402408
f2fs_down_write(&sbi->gc_lock);
403-
f2fs_gc(sbi, false, false, false, NULL_SEGNO);
409+
f2fs_gc(sbi, &gc_control);
404410
}
405411
}
406412
}

fs/f2fs/super.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2076,8 +2076,14 @@ static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
20762076
sbi->gc_mode = GC_URGENT_HIGH;
20772077

20782078
while (!f2fs_time_over(sbi, DISABLE_TIME)) {
2079+
struct f2fs_gc_control gc_control = {
2080+
.victim_segno = NULL_SEGNO,
2081+
.init_gc_type = FG_GC,
2082+
.should_migrate_blocks = false,
2083+
.err_gc_skipped = true };
2084+
20792085
f2fs_down_write(&sbi->gc_lock);
2080-
err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
2086+
err = f2fs_gc(sbi, &gc_control);
20812087
if (err == -ENODATA) {
20822088
err = 0;
20832089
break;

include/trace/events/f2fs.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -644,19 +644,19 @@ TRACE_EVENT(f2fs_background_gc,
644644

645645
TRACE_EVENT(f2fs_gc_begin,
646646

647-
TP_PROTO(struct super_block *sb, bool sync, bool background,
647+
TP_PROTO(struct super_block *sb, int gc_type, bool no_bg_gc,
648648
long long dirty_nodes, long long dirty_dents,
649649
long long dirty_imeta, unsigned int free_sec,
650650
unsigned int free_seg, int reserved_seg,
651651
unsigned int prefree_seg),
652652

653-
TP_ARGS(sb, sync, background, dirty_nodes, dirty_dents, dirty_imeta,
653+
TP_ARGS(sb, gc_type, no_bg_gc, dirty_nodes, dirty_dents, dirty_imeta,
654654
free_sec, free_seg, reserved_seg, prefree_seg),
655655

656656
TP_STRUCT__entry(
657657
__field(dev_t, dev)
658-
__field(bool, sync)
659-
__field(bool, background)
658+
__field(int, gc_type)
659+
__field(bool, no_bg_gc)
660660
__field(long long, dirty_nodes)
661661
__field(long long, dirty_dents)
662662
__field(long long, dirty_imeta)
@@ -668,8 +668,8 @@ TRACE_EVENT(f2fs_gc_begin,
668668

669669
TP_fast_assign(
670670
__entry->dev = sb->s_dev;
671-
__entry->sync = sync;
672-
__entry->background = background;
671+
__entry->gc_type = gc_type;
672+
__entry->no_bg_gc = no_bg_gc;
673673
__entry->dirty_nodes = dirty_nodes;
674674
__entry->dirty_dents = dirty_dents;
675675
__entry->dirty_imeta = dirty_imeta;
@@ -679,12 +679,12 @@ TRACE_EVENT(f2fs_gc_begin,
679679
__entry->prefree_seg = prefree_seg;
680680
),
681681

682-
TP_printk("dev = (%d,%d), sync = %d, background = %d, nodes = %lld, "
682+
TP_printk("dev = (%d,%d), gc_type = %s, no_background_GC = %d, nodes = %lld, "
683683
"dents = %lld, imeta = %lld, free_sec:%u, free_seg:%u, "
684684
"rsv_seg:%d, prefree_seg:%u",
685685
show_dev(__entry->dev),
686-
__entry->sync,
687-
__entry->background,
686+
show_gc_type(__entry->gc_type),
687+
(__entry->gc_type == BG_GC) ? __entry->no_bg_gc : -1,
688688
__entry->dirty_nodes,
689689
__entry->dirty_dents,
690690
__entry->dirty_imeta,

0 commit comments

Comments
 (0)