Skip to content

Commit

Permalink
f2fs: Don't create discard thread when device doesn't support realtim…
Browse files Browse the repository at this point in the history
…e discard

Don't create discard thread when device doesn't support realtime discard
or user specifies nodiscard mount option.

Signed-off-by: Fengnan Chang <changfengnan@vivo.com>
Signed-off-by: Yangtao Li <frank.li@vivo.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
  • Loading branch information
Fengnan Chang authored and Jaegeuk Kim committed Aug 30, 2021
1 parent 94c821f commit 4d67490
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions fs/f2fs/f2fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3448,6 +3448,7 @@ int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
Expand Down
25 changes: 19 additions & 6 deletions fs/f2fs/segment.c
Original file line number Diff line number Diff line change
Expand Up @@ -2115,9 +2115,25 @@ void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
wake_up_discard_thread(sbi, false);
}

static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
int f2fs_start_discard_thread(struct f2fs_sb_info *sbi)
{
dev_t dev = sbi->sb->s_bdev->bd_dev;
struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
int err = 0;

if (!f2fs_realtime_discard_enable(sbi))
return 0;

dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
"f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
if (IS_ERR(dcc->f2fs_issue_discard))
err = PTR_ERR(dcc->f2fs_issue_discard);

return err;
}

static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
{
struct discard_cmd_control *dcc;
int err = 0, i;

Expand Down Expand Up @@ -2155,13 +2171,10 @@ static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
init_waitqueue_head(&dcc->discard_wait_queue);
SM_I(sbi)->dcc_info = dcc;
init_thread:
dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
"f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
if (IS_ERR(dcc->f2fs_issue_discard)) {
err = PTR_ERR(dcc->f2fs_issue_discard);
err = f2fs_start_discard_thread(sbi);
if (err) {
kfree(dcc);
SM_I(sbi)->dcc_info = NULL;
return err;
}

return err;
Expand Down
27 changes: 26 additions & 1 deletion fs/f2fs/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -2105,12 +2105,15 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
bool need_restart_gc = false, need_stop_gc = false;
bool need_restart_ckpt = false, need_stop_ckpt = false;
bool need_restart_flush = false, need_stop_flush = false;
bool need_restart_discard = false, need_stop_discard = false;
bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT);
bool no_io_align = !F2FS_IO_ALIGNED(sbi);
bool no_atgc = !test_opt(sbi, ATGC);
bool no_discard = !test_opt(sbi, DISCARD);
bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
bool block_unit_discard = f2fs_block_unit_discard(sbi);
struct discard_cmd_control *dcc;
#ifdef CONFIG_QUOTA
int i, j;
#endif
Expand Down Expand Up @@ -2282,11 +2285,26 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
need_stop_flush = true;
}

if (no_discard == !!test_opt(sbi, DISCARD)) {
if (test_opt(sbi, DISCARD)) {
err = f2fs_start_discard_thread(sbi);
if (err)
goto restore_flush;
need_stop_discard = true;
} else {
dcc = SM_I(sbi)->dcc_info;
f2fs_stop_discard_thread(sbi);
if (atomic_read(&dcc->discard_cmd_cnt))
f2fs_issue_discard_timeout(sbi);
need_restart_discard = true;
}
}

if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) {
if (test_opt(sbi, DISABLE_CHECKPOINT)) {
err = f2fs_disable_checkpoint(sbi);
if (err)
goto restore_flush;
goto restore_discard;
} else {
f2fs_enable_checkpoint(sbi);
}
Expand All @@ -2306,6 +2324,13 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
adjust_unusable_cap_perc(sbi);
*flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
return 0;
restore_discard:
if (need_restart_discard) {
if (f2fs_start_discard_thread(sbi))
f2fs_warn(sbi, "discard has been stopped");
} else if (need_stop_discard) {
f2fs_stop_discard_thread(sbi);
}
restore_flush:
if (need_restart_flush) {
if (f2fs_create_flush_cmd_control(sbi))
Expand Down

0 comments on commit 4d67490

Please sign in to comment.