Skip to contentSkip to conversation

f2fs: fix to check lz4hc compression when CONFIG_F2FS_FS_LZ4HC is not enabled

PATCH In review Yangtao Li 4 participants 7 messages Jan 24, 2023 – Apr 5, 2023 F2FS file system

lore
Share & tools
lore

Yangtao Li

author
#0 lore raw

PATCH 1/4 f2fs: fix to check lz4hc compression when CONFIG_F2FS_FS_LZ4HC is not enabled

f2fs supports lz4 compression algorithm and lz4hc compression algorithm, which the level parameter needs to be passed in. When CONFIG_F2FS_FS_LZ4HC is not enabled, even if there is no problem with the level parameter, add the level parameter to the lz4 algorithm will cause the mount to fail.

Let's change it to be the same as other compression algorithms. When the kernel does not enable the algorithm, ignore this parameter and print msg.

Fixes 3fde13f817e2 ("f2fs: compress: support compress level")
---
 fs/f2fs/super.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)
fs/f2fs/super.c +14 -17
index d8a65645ee48..ad5df4d5c39a 100644
@@ -588,19 +588,11 @@ static int f2fs_test_compress_extension(struct f2fs_sb_info *sbi)
 	return 0;
 }
 
-#ifdef CONFIG_F2FS_FS_LZ4
+#ifdef CONFIG_F2FS_FS_LZ4HC
 static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
 {
-#ifdef CONFIG_F2FS_FS_LZ4HC
 	unsigned int level;
-#endif
 
-	if (strlen(str) == 3) {
-		F2FS_OPTION(sbi).compress_level = 0;
-		return 0;
-	}
-
-#ifdef CONFIG_F2FS_FS_LZ4HC
 	str += 3;
 
 	if (str[0] != ':') {
@@ -617,10 +609,6 @@ static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
 
 	F2FS_OPTION(sbi).compress_level = level;
 	return 0;
-#else
-	f2fs_info(sbi, "kernel doesn't support lz4hc compression");
-	return -EINVAL;
-#endif
 }
 #endif
 
@@ -1085,10 +1073,19 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
 #endif
 			} else if (!strncmp(name, "lz4", 3)) {
 #ifdef CONFIG_F2FS_FS_LZ4
-				ret = f2fs_set_lz4hc_level(sbi, name);
-				if (ret) {
-					kfree(name);
-					return -EINVAL;
+				if (strlen(name) == 3) {
+					F2FS_OPTION(sbi).compress_level = 0;
+				} else {
+#ifdef CONFIG_F2FS_FS_LZ4HC
+					ret = f2fs_set_lz4hc_level(sbi, name);
+					if (ret) {
+						kfree(name);
+						return -EINVAL;
+					}
+#else
+					f2fs_info(sbi, "kernel doesn't support lz4hc compression");
+					break;
+#endif
 				}
 				F2FS_OPTION(sbi).compress_algorithm =
 								COMPRESS_LZ4;
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/4 f2fs: introduce f2fs_set_compress_level()

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.

---
 fs/f2fs/super.c | 57 +++++++++++++++----------------------------------
 1 file changed, 17 insertions(+), 40 deletions(-)
fs/f2fs/super.c +17 -40
index ad5df4d5c39a..b5fbe9939390 100644
@@ -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;
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH 3/4 f2fs: set default compress option only when sb_has_compression

In reply to Yangtao Li

If the compress feature is not enabled, there is no need to set compress-related parameters.

---
 fs/f2fs/super.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/f2fs/super.c +6 -4
index b5fbe9939390..68ccc9f54c2d 100644
@@ -2039,10 +2039,12 @@ static void default_options(struct f2fs_sb_info *sbi)
 	F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
 	F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
 	F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
-	F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
-	F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
-	F2FS_OPTION(sbi).compress_ext_cnt = 0;
-	F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
+	if (f2fs_sb_has_compression(sbi)) {
+		F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
+		F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
+		F2FS_OPTION(sbi).compress_ext_cnt = 0;
+		F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
+	}
 	F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
 	F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
 
Signature · 1 line
2.25.1
author
#3 lore raw

PATCH 4/4 f2fs: merge lz4hc_compress_pages() to lz4_compress_pages()

In reply to Yangtao Li

Remove unnecessary lz4hc_compress_pages().

---
 fs/f2fs/compress.c | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)
fs/f2fs/compress.c +4 -19
index b196b881f3bb..112f0e208b4e 100644
@@ -264,36 +264,21 @@ static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
 	cc->private = NULL;
 }
 
-#ifdef CONFIG_F2FS_FS_LZ4HC
-static int lz4hc_compress_pages(struct compress_ctx *cc)
+static int lz4_compress_pages(struct compress_ctx *cc)
 {
+	int len;
+#ifdef CONFIG_F2FS_FS_LZ4HC
 	unsigned char level = F2FS_I(cc->inode)->i_compress_flag >>
 						COMPRESS_LEVEL_OFFSET;
-	int len;
 
 	if (level)
 		len = LZ4_compress_HC(cc->rbuf, cc->cbuf->cdata, cc->rlen,
 					cc->clen, level, cc->private);
 	else
+#endif
 		len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
 						cc->clen, cc->private);
-	if (!len)
-		return -EAGAIN;
-
-	cc->clen = len;
-	return 0;
-}
-#endif
-
-static int lz4_compress_pages(struct compress_ctx *cc)
-{
-	int len;
 
-#ifdef CONFIG_F2FS_FS_LZ4HC
-	return lz4hc_compress_pages(cc);
-#endif
-	len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
-						cc->clen, cc->private);
 	if (!len)
 		return -EAGAIN;
 
Signature · 1 line
2.25.1
maintainer
#4 lore raw
In reply to Yangtao Li
Quoted 59 lines · Yangtao Li

f2fs supports lz4 compression algorithm and lz4hc compression algorithm, which the level parameter needs to be passed in. When CONFIG_F2FS_FS_LZ4HC is not enabled, even if there is no problem with the level parameter, add the level parameter to the lz4 algorithm will cause the mount to fail.

Let's change it to be the same as other compression algorithms. When the kernel does not enable the algorithm, ignore this parameter and print msg.

Fixes 3fde13f817e2 ("f2fs: compress: support compress level")
---
  fs/f2fs/super.c | 31 ++++++++++++++-----------------
  1 file changed, 14 insertions(+), 17 deletions(-)
fs/f2fs/super.c +2 -17
index d8a65645ee48..ad5df4d5c39a 100644
@@ -588,19 +588,11 @@ static int f2fs_test_compress_extension(struct f2fs_sb_info *sbi)
  	return 0;
  }
 
-#ifdef CONFIG_F2FS_FS_LZ4
+#ifdef CONFIG_F2FS_FS_LZ4HC
  static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
  {
-#ifdef CONFIG_F2FS_FS_LZ4HC
  	unsigned int level;
-#endif
 
-	if (strlen(str) == 3) {
-		F2FS_OPTION(sbi).compress_level = 0;
-		return 0;
-	}
-
-#ifdef CONFIG_F2FS_FS_LZ4HC
  	str += 3;
 
  	if (str[0] != ':') {
@@ -617,10 +609,6 @@ static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
 
  	F2FS_OPTION(sbi).compress_level = level;
  	return 0;
-#else
-	f2fs_info(sbi, "kernel doesn't support lz4hc compression");
-	return -EINVAL;
-#endif
  }
  #endif
 
@@ -1085,10 +1073,19 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
  #endif
  			} else if (!strncmp(name, "lz4", 3)) {
  #ifdef CONFIG_F2FS_FS_LZ4
-				ret = f2fs_set_lz4hc_level(sbi, name);
-				if (ret) {
-					kfree(name);
-					return -EINVAL;
+				if (strlen(name) == 3) {

fs/f2fs/super.c static int parse_options(struct super_block *sb, char *options, bool is_remount) · line 1086

Quoted 10 lines
fs/f2fs/super.c +10 -0
+					F2FS_OPTION(sbi).compress_level = 0;
+				} else {
+#ifdef CONFIG_F2FS_FS_LZ4HC
+					ret = f2fs_set_lz4hc_level(sbi, name);
+					if (ret) {
+						kfree(name);
+						return -EINVAL;
+					}
+#else
+					f2fs_info(sbi, "kernel doesn't support lz4hc compression");

It needs to check <alg_name>:<compr_level> format to make sure user wants to enable lz4hc w/ specified level, otherwise if parameter is lz4xx, it doesn't make sense to print:

"kernel doesn't support lz4hc compression"

fs/f2fs/super.c static int parse_options(struct super_block *sb, char *options, bool is_remount) · line 1087

Quoted 1 line
fs/f2fs/super.c +1 -0
+					break;

It will cause memory leak for name.

Thanks,

Quoted 4 lines
fs/f2fs/super.c +1 -0
+#endif
  				}
  				F2FS_OPTION(sbi).compress_algorithm =
  								COMPRESS_LZ4;
#5 lore raw
Quoted 4 lines · Chao Yu
Quoted 3 lines · Yangtao Li

f2fs supports lz4 compression algorithm and lz4hc compression algorithm, which the level parameter needs to be passed in. When CONFIG_F2FS_FS_LZ4HC is not enabled, even if there is no problem with the level parameter, add

lz4hc is not a different compression algorithm, but rather just a higher compression level for lz4.

- Eric

#6 lore raw
In reply to Yangtao Li
This series was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:

Hello:

This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:

Quoted 9 lines · On Tue, 24 Jan 2023 23:33:43 +0800 you wrote:

f2fs supports lz4 compression algorithm and lz4hc compression algorithm, which the level parameter needs to be passed in. When CONFIG_F2FS_FS_LZ4HC is not enabled, even if there is no problem with the level parameter, add the level parameter to the lz4 algorithm will cause the mount to fail.

Let's change it to be the same as other compression algorithms. When the kernel does not enable the algorithm, ignore this parameter and print msg.

[...]

Here is the summary with links:
- [f2fs-dev,1/4] f2fs: fix to check lz4hc compression when CONFIG_F2FS_FS_LZ4HC is not enabled (no matching commit)
- [f2fs-dev,2/4] f2fs: introduce f2fs_set_compress_level()
(no matching commit)
- [f2fs-dev,3/4] f2fs: set default compress option only when sb_has_compression

    https://git.kernel.org/jaegeuk/f2fs/c/338abb312bb2

- [f2fs-dev,4/4] f2fs: merge lz4hc_compress_pages() to lz4_compress_pages() (no matching commit)

You are awesome, thank you!

Signature · 10 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html




_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel