Re: [PATCH] f2fs: fix to set DISCARD opt

From: Chao Yu
Date: Wed Nov 23 2022 - 10:12:12 EST


On 2022/11/16 2:40, Yangtao Li wrote:
Some minor modifications to discard opt and related parameters:

1.introduce f2fs_is_readonly() and use it to simplify code
2.The FLUSH_MERGE opt is set by default only in non-ro mode.
3.When ro and DISCARD are set at the same time, an error is reported.
4.Display discard_unit mount opt when has discard opt.
5.clear DISCARD when remount as ro.

Signed-off-by: Yangtao Li <frank.li@xxxxxxxx>
---
fs/f2fs/f2fs.h | 5 +++++
fs/f2fs/super.c | 53 +++++++++++++++++++++++++++----------------------
2 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index b89b5d755ce0..be23059344b4 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4579,4 +4579,9 @@ static inline void f2fs_handle_page_eio(struct f2fs_sb_info *sbi, pgoff_t ofs,
#define EFSBADCRC EBADMSG /* Bad CRC detected */
#define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
+static inline bool f2fs_is_readonly(struct f2fs_sb_info *sbi)
+{
+ return !!f2fs_sb_has_readonly(sbi) || f2fs_readonly(sbi->sb);
+}
+
#endif /* _LINUX_F2FS_H */
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 75027ff85cd9..baa8f0860192 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -1353,12 +1353,16 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
return -EINVAL;
}
- if ((f2fs_sb_has_readonly(sbi) || f2fs_readonly(sbi->sb)) &&
- test_opt(sbi, FLUSH_MERGE)) {
+ if (f2fs_is_readonly(sbi) && test_opt(sbi, FLUSH_MERGE)) {
f2fs_err(sbi, "FLUSH_MERGE not compatible with readonly mode");
return -EINVAL;
}
+ if (f2fs_is_readonly(sbi) && test_opt(sbi, DISCARD)) {
+ f2fs_err(sbi, "DISCARD not compatible with readonly mode");
+ return -EINVAL;
+ }

Well, it looks ext4 support mounting image w/ both discard and ro option.

And I guess for the case device is rw, and filesystem is ro, it may allow
filesystem itself issue command to trim device, and that won't break semantic
of readonly filesystem?

+
if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
f2fs_err(sbi, "Allow to mount readonly mode only");
return -EROFS;
@@ -2035,12 +2039,14 @@ static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
if (test_opt(sbi, ATGC))
seq_puts(seq, ",atgc");
- if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK)
- seq_printf(seq, ",discard_unit=%s", "block");
- else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
- seq_printf(seq, ",discard_unit=%s", "segment");
- else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
- seq_printf(seq, ",discard_unit=%s", "section");
+ if (test_opt(sbi, DISCARD)) {
+ if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK)
+ seq_printf(seq, ",discard_unit=%s", "block");
+ else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
+ seq_printf(seq, ",discard_unit=%s", "segment");
+ else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
+ seq_printf(seq, ",discard_unit=%s", "section");
+ }
if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL)
seq_printf(seq, ",memory=%s", "normal");
@@ -2081,9 +2087,10 @@ static void default_options(struct f2fs_sb_info *sbi)
set_opt(sbi, MERGE_CHECKPOINT);
F2FS_OPTION(sbi).unusable_cap = 0;
sbi->sb->s_flags |= SB_LAZYTIME;
- if (!f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb))
+ if (!f2fs_is_readonly(sbi))
set_opt(sbi, FLUSH_MERGE);
- if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
+ if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
+ !f2fs_is_readonly(sbi))
set_opt(sbi, DISCARD);
if (f2fs_sb_has_blkzoned(sbi)) {
F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
@@ -2221,7 +2228,6 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
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;
@@ -2398,19 +2404,18 @@ 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 ((*flags & SB_RDONLY) || !test_opt(sbi, DISCARD)) {
+ clear_opt(sbi, DISCARD);
+ 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;
+ } else {
+ err = f2fs_start_discard_thread(sbi);
+ if (err)
+ goto restore_flush;
+ need_stop_discard = true;
}
if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) {