[2/4] f2fs: introduce f2fs_set_compress_level()

Message ID 20230124153346.74881-2-frank.li@vivo.com
State New
Headers
Series [1/4] f2fs: fix to check lz4hc compression when CONFIG_F2FS_FS_LZ4HC is not enabled |

Commit Message

李扬韬 Jan. 24, 2023, 3:33 p.m. UTC
  f2fs_set_lz4hc_level() and f2fs_set_zstd_level() have common code,
let's introduce f2fs_set_compress_level() to do sanity compress level
check.

Signed-off-by: Yangtao Li <frank.li@vivo.com>
---
 fs/f2fs/super.c | 57 +++++++++++++++----------------------------------
 1 file changed, 17 insertions(+), 40 deletions(-)
  

Patch

diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index ad5df4d5c39a..b5fbe9939390 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -588,40 +588,12 @@  static int f2fs_test_compress_extension(struct f2fs_sb_info *sbi)
 	return 0;
 }
 
-#ifdef CONFIG_F2FS_FS_LZ4HC
-static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
+static int __maybe_unused f2fs_set_compress_level(struct f2fs_sb_info *sbi,
+					const char *str, const char *alg_name,
+					unsigned int min, unsigned int max)
 {
 	unsigned int level;
-
-	str += 3;
-
-	if (str[0] != ':') {
-		f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
-		return -EINVAL;
-	}
-	if (kstrtouint(str + 1, 10, &level))
-		return -EINVAL;
-
-	if (level < LZ4HC_MIN_CLEVEL || level > LZ4HC_MAX_CLEVEL) {
-		f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
-		return -EINVAL;
-	}
-
-	F2FS_OPTION(sbi).compress_level = level;
-	return 0;
-}
-#endif
-
-#ifdef CONFIG_F2FS_FS_ZSTD
-static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
-{
-	unsigned int level;
-	int len = 4;
-
-	if (strlen(str) == len) {
-		F2FS_OPTION(sbi).compress_level = 0;
-		return 0;
-	}
+	int len = strlen(alg_name);
 
 	str += len;
 
@@ -632,8 +604,8 @@  static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
 	if (kstrtouint(str + 1, 10, &level))
 		return -EINVAL;
 
-	if (!level || level > zstd_max_clevel()) {
-		f2fs_info(sbi, "invalid zstd compress level: %d", level);
+	if (level < min || level > max) {
+		f2fs_info(sbi, "invalid %s compress level: %d", alg_name, level);
 		return -EINVAL;
 	}
 
@@ -641,7 +613,6 @@  static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
 	return 0;
 }
 #endif
-#endif
 
 static int parse_options(struct super_block *sb, char *options, bool is_remount)
 {
@@ -1077,7 +1048,8 @@  static int parse_options(struct super_block *sb, char *options, bool is_remount)
 					F2FS_OPTION(sbi).compress_level = 0;
 				} else {
 #ifdef CONFIG_F2FS_FS_LZ4HC
-					ret = f2fs_set_lz4hc_level(sbi, name);
+					ret = f2fs_set_compress_level(sbi, name, "lz4",
+						LZ4HC_MIN_CLEVEL, LZ4HC_MAX_CLEVEL);
 					if (ret) {
 						kfree(name);
 						return -EINVAL;
@@ -1094,10 +1066,15 @@  static int parse_options(struct super_block *sb, char *options, bool is_remount)
 #endif
 			} else if (!strncmp(name, "zstd", 4)) {
 #ifdef CONFIG_F2FS_FS_ZSTD
-				ret = f2fs_set_zstd_level(sbi, name);
-				if (ret) {
-					kfree(name);
-					return -EINVAL;
+				if (strlen(name) == 4) {
+					F2FS_OPTION(sbi).compress_level = 0;
+				} else {
+					ret = f2fs_set_compress_level(sbi, name, "zstd", 1,
+						zstd_max_clevel());
+					if (ret) {
+						kfree(name);
+						return -EINVAL;
+					}
 				}
 				F2FS_OPTION(sbi).compress_algorithm =
 								COMPRESS_ZSTD;