Skip to contentSkip to conversation

f2fs: add sanity compress level check for compressed file

PATCH RESEND Superseded Yangtao Li 2 participants 5 messages Mar 30, 2023 – Apr 4, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yangtao Li

author
#0 lore raw

PATCH RESEND 1/1 f2fs: add sanity compress level check for compressed file

Commit 3fde13f817e2 ("f2fs: compress: support compress level") forgot to do basic compress level check, let's add it.

---
 fs/f2fs/inode.c             | 94 +++++++++++++++++++++++++------------
 include/linux/zstd_lib.h    |  3 ++
 lib/zstd/compress/clevels.h |  4 --
 3 files changed, 67 insertions(+), 34 deletions(-)
fs/f2fs/inode.c +64 -30
index bb5b365a195d..e63f75168700 100644
@@ -10,6 +10,8 @@
 #include <linux/buffer_head.h>
 #include <linux/writeback.h>
 #include <linux/sched/mm.h>
+#include <linux/lz4.h>
+#include <linux/zstd.h>
 
 #include "f2fs.h"
 #include "node.h"
@@ -202,6 +204,66 @@ void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
 	ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
 }
 
+static bool sanity_check_compress_inode(struct inode *inode,
+			struct f2fs_inode *ri)
+{
+	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+	unsigned char compress_level;
+
+	if (ri->i_compress_algorithm >= COMPRESS_MAX) {
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		f2fs_warn(sbi,
+			"%s: inode (ino=%lx) has unsupported compress algorithm: %u, run fsck to fix",
+			__func__, inode->i_ino, ri->i_compress_algorithm);
+		return false;
+	}
+	if (le64_to_cpu(ri->i_compr_blocks) >
+			SECTOR_TO_BLOCK(inode->i_blocks)) {
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		f2fs_warn(sbi,
+			"%s: inode (ino=%lx) has inconsistent i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix",
+			__func__, inode->i_ino, le64_to_cpu(ri->i_compr_blocks),
+			SECTOR_TO_BLOCK(inode->i_blocks));
+		return false;
+	}
+	if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
+		ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) {
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		f2fs_warn(sbi,
+			"%s: inode (ino=%lx) has unsupported log cluster size: %u, run fsck to fix",
+			__func__, inode->i_ino, ri->i_log_cluster_size);
+		return false;
+	}
+
+	compress_level = le16_to_cpu(ri->i_compress_flag) >> COMPRESS_LEVEL_OFFSET;
+	switch (ri->i_compress_algorithm) {
+	case COMPRESS_LZO:
+	case COMPRESS_LZORLE:
+		if (compress_level)
+			goto err;
+		break;
+	case COMPRESS_LZ4:
+		if ((compress_level && compress_level < LZ4HC_MIN_CLEVEL) ||
+				compress_level > LZ4HC_MAX_CLEVEL)
+			goto err;
+		break;
+	case COMPRESS_ZSTD:
+		if (!compress_level || compress_level > ZSTD_MAX_CLEVEL)
+			goto err;
+		break;
+	default:
+		goto err;
+	}
+
+	return true;
+
+err:
+	set_sbi_flag(sbi, SBI_NEED_FSCK);
+	f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported compress level: %u, run fsck to fix",
+		  __func__, inode->i_ino, compress_level);
+	return false;
+}
+
 static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
@@ -285,36 +347,8 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 
 	if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) &&
 			fi->i_flags & F2FS_COMPR_FL &&
-			F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
-						i_log_cluster_size)) {
-		if (ri->i_compress_algorithm >= COMPRESS_MAX) {
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
-			f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported "
-				"compress algorithm: %u, run fsck to fix",
-				  __func__, inode->i_ino,
-				  ri->i_compress_algorithm);
-			return false;
-		}
-		if (le64_to_cpu(ri->i_compr_blocks) >
-				SECTOR_TO_BLOCK(inode->i_blocks)) {
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
-			f2fs_warn(sbi, "%s: inode (ino=%lx) has inconsistent "
-				"i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix",
-				  __func__, inode->i_ino,
-				  le64_to_cpu(ri->i_compr_blocks),
-				  SECTOR_TO_BLOCK(inode->i_blocks));
-			return false;
-		}
-		if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
-			ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) {
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
-			f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported "
-				"log cluster size: %u, run fsck to fix",
-				  __func__, inode->i_ino,
-				  ri->i_log_cluster_size);
-			return false;
-		}
-	}
+			F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_log_cluster_size))
+		sanity_check_compress_inode(inode, ri);
 
 	return true;
 }
include/linux/zstd_lib.h +3 -0
index 79d55465d5c1..ff55f41c73d3 100644
@@ -88,6 +88,9 @@ ZSTDLIB_API const char* ZSTD_versionString(void);
 #  define ZSTD_CLEVEL_DEFAULT 3
 #endif
 
+/*-=====  Pre-defined compression levels  =====-*/
+#define ZSTD_MAX_CLEVEL     22
+
 /* *************************************
  *  Constants
  ***************************************/
lib/zstd/compress/clevels.h +0 -4
index d9a76112ec3a..b040d9d29089 100644
@@ -14,10 +14,6 @@
 #define ZSTD_STATIC_LINKING_ONLY  /* ZSTD_compressionParameters  */
 #include <linux/zstd.h>
 
-/*-=====  Pre-defined compression levels  =====-*/
-
-#define ZSTD_MAX_CLEVEL     22
-
 __attribute__((__unused__))
 
 static const ZSTD_compressionParameters ZSTD_defaultCParameters[4][ZSTD_MAX_CLEVEL+1] = {
Signature · 8 lines
2.35.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
maintainer
#1 lore raw
Quoted 49 lines · Yangtao Li

Commit 3fde13f817e2 ("f2fs: compress: support compress level") forgot to do basic compress level check, let's add it.

---
  fs/f2fs/inode.c             | 94 +++++++++++++++++++++++++------------
  include/linux/zstd_lib.h    |  3 ++
  lib/zstd/compress/clevels.h |  4 --
  3 files changed, 67 insertions(+), 34 deletions(-)
fs/f2fs/inode.c +24 -0
index bb5b365a195d..e63f75168700 100644
@@ -10,6 +10,8 @@
  #include <linux/buffer_head.h>
  #include <linux/writeback.h>
  #include <linux/sched/mm.h>
+#include <linux/lz4.h>
+#include <linux/zstd.h>
 
  #include "f2fs.h"
  #include "node.h"
@@ -202,6 +204,66 @@ void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
  	ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
  }
 
+static bool sanity_check_compress_inode(struct inode *inode,
+			struct f2fs_inode *ri)
+{
+	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+	unsigned char compress_level;
+
+	if (ri->i_compress_algorithm >= COMPRESS_MAX) {
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		f2fs_warn(sbi,
+			"%s: inode (ino=%lx) has unsupported compress algorithm: %u, run fsck to fix",
+			__func__, inode->i_ino, ri->i_compress_algorithm);
+		return false;
+	}
+	if (le64_to_cpu(ri->i_compr_blocks) >
+			SECTOR_TO_BLOCK(inode->i_blocks)) {
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		f2fs_warn(sbi,
+			"%s: inode (ino=%lx) has inconsistent i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix",
+			__func__, inode->i_ino, le64_to_cpu(ri->i_compr_blocks),
+			SECTOR_TO_BLOCK(inode->i_blocks));
+		return false;
+	}

fs/f2fs/inode.c void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page) · line 238

Quoted 10 lines
fs/f2fs/inode.c +10 -0
+	if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
+		ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) {
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		f2fs_warn(sbi,
+			"%s: inode (ino=%lx) has unsupported log cluster size: %u, run fsck to fix",
+			__func__, inode->i_ino, ri->i_log_cluster_size);
+		return false;
+	}
+
+	compress_level = le16_to_cpu(ri->i_compress_flag) >> COMPRESS_LEVEL_OFFSET;

Exceed 80 lines.

Quoted 56 lines
fs/f2fs/inode.c +28 -21
+	switch (ri->i_compress_algorithm) {
+	case COMPRESS_LZO:
+	case COMPRESS_LZORLE:
+		if (compress_level)
+			goto err;
+		break;
+	case COMPRESS_LZ4:
+		if ((compress_level && compress_level < LZ4HC_MIN_CLEVEL) ||
+				compress_level > LZ4HC_MAX_CLEVEL)
+			goto err;
+		break;
+	case COMPRESS_ZSTD:
+		if (!compress_level || compress_level > ZSTD_MAX_CLEVEL)
+			goto err;
+		break;
+	default:
+		goto err;
+	}
+
+	return true;
+
+err:
+	set_sbi_flag(sbi, SBI_NEED_FSCK);
+	f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported compress level: %u, run fsck to fix",
+		  __func__, inode->i_ino, compress_level);
+	return false;
+}
+
  static bool sanity_check_inode(struct inode *inode, struct page *node_page)
  {
  	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
@@ -285,36 +347,8 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 
  	if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) &&
  			fi->i_flags & F2FS_COMPR_FL &&
-			F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
-						i_log_cluster_size)) {
-		if (ri->i_compress_algorithm >= COMPRESS_MAX) {
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
-			f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported "
-				"compress algorithm: %u, run fsck to fix",
-				  __func__, inode->i_ino,
-				  ri->i_compress_algorithm);
-			return false;
-		}
-		if (le64_to_cpu(ri->i_compr_blocks) >
-				SECTOR_TO_BLOCK(inode->i_blocks)) {
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
-			f2fs_warn(sbi, "%s: inode (ino=%lx) has inconsistent "
-				"i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix",
-				  __func__, inode->i_ino,
-				  le64_to_cpu(ri->i_compr_blocks),
-				  SECTOR_TO_BLOCK(inode->i_blocks));
-			return false;
-		}
-		if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE ||

fs/f2fs/inode.c static bool sanity_check_inode(struct inode *inode, struct page *node_page) · line 350

Quoted 10 lines
fs/f2fs/inode.c +1 -9
-			ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) {
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
-			f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported "
-				"log cluster size: %u, run fsck to fix",
-				  __func__, inode->i_ino,
-				  ri->i_log_cluster_size);
-			return false;
-		}
-	}
+			F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_log_cluster_size))

Exceed 80 lines.

fs/f2fs/inode.c static bool sanity_check_inode(struct inode *inode, struct page *node_page) · line 351

Quoted 1 line
fs/f2fs/inode.c +1 -0
+		sanity_check_compress_inode(inode, ri);

Missed to check return value?

Quoted 22 lines
fs/f2fs/inode.c +0 -0
  	return true;
  }
include/linux/zstd_lib.h +3 -0
index 79d55465d5c1..ff55f41c73d3 100644
@@ -88,6 +88,9 @@ ZSTDLIB_API const char* ZSTD_versionString(void);
  #  define ZSTD_CLEVEL_DEFAULT 3
  #endif
 
+/*-=====  Pre-defined compression levels  =====-*/
+#define ZSTD_MAX_CLEVEL     22
+
  /* *************************************
   *  Constants
   ***************************************/
lib/zstd/compress/clevels.h +0 -0
index d9a76112ec3a..b040d9d29089 100644

lib/zstd/compress/clevels.h line 19

Quoted 6 lines
lib/zstd/compress/clevels.h +0 -3
@@ -14,10 +14,6 @@
  #define ZSTD_STATIC_LINKING_ONLY  /* ZSTD_compressionParameters  */
  #include <linux/zstd.h>
 
-/*-=====  Pre-defined compression levels  =====-*/
-
-#define ZSTD_MAX_CLEVEL     22

Why not zstd_max_clevel()?

Thanks,

Quoted 4 lines
lib/zstd/compress/clevels.h +0 -1
-
  __attribute__((__unused__))
 
  static const ZSTD_compressionParameters ZSTD_defaultCParameters[4][ZSTD_MAX_CLEVEL+1] = {
maintainer
#2 lore raw
Quoted 54 lines · Chao Yu
Quoted 53 lines · Yangtao Li

Commit 3fde13f817e2 ("f2fs: compress: support compress level") forgot to do basic compress level check, let's add it.

---
   fs/f2fs/inode.c             | 94 +++++++++++++++++++++++++------------
   include/linux/zstd_lib.h    |  3 ++
   lib/zstd/compress/clevels.h |  4 --
   3 files changed, 67 insertions(+), 34 deletions(-)
fs/f2fs/inode.c +28 -0
index bb5b365a195d..e63f75168700 100644
@@ -10,6 +10,8 @@
   #include <linux/buffer_head.h>
   #include <linux/writeback.h>
   #include <linux/sched/mm.h>
+#include <linux/lz4.h>
+#include <linux/zstd.h>
 
   #include "f2fs.h"
   #include "node.h"
@@ -202,6 +204,66 @@ void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
   	ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
   }
 
+static bool sanity_check_compress_inode(struct inode *inode,
+			struct f2fs_inode *ri)
+{
+	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+	unsigned char compress_level;
+
+	if (ri->i_compress_algorithm >= COMPRESS_MAX) {
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		f2fs_warn(sbi,
+			"%s: inode (ino=%lx) has unsupported compress algorithm: %u, run fsck to fix",
+			__func__, inode->i_ino, ri->i_compress_algorithm);
+		return false;
+	}
+	if (le64_to_cpu(ri->i_compr_blocks) >
+			SECTOR_TO_BLOCK(inode->i_blocks)) {
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		f2fs_warn(sbi,
+			"%s: inode (ino=%lx) has inconsistent i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix",
+			__func__, inode->i_ino, le64_to_cpu(ri->i_compr_blocks),
+			SECTOR_TO_BLOCK(inode->i_blocks));
+		return false;
+	}
+	if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
+		ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) {
+		set_sbi_flag(sbi, SBI_NEED_FSCK);
+		f2fs_warn(sbi,
Quoted 8 lines

fs/f2fs/inode.c void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page) · line 238

Quoted 6 lines
fs/f2fs/inode.c +6 -0
+			"%s: inode (ino=%lx) has unsupported log cluster size: %u, run fsck to fix",
+			__func__, inode->i_ino, ri->i_log_cluster_size);
+		return false;
+	}
+
+	compress_level = le16_to_cpu(ri->i_compress_flag) >> COMPRESS_LEVEL_OFFSET;

Exceed 80 lines.

Sorry, colunms... out of my mind.

Quoted 61 lines
Quoted 61 lines
fs/f2fs/inode.c +28 -25
+	switch (ri->i_compress_algorithm) {
+	case COMPRESS_LZO:
+	case COMPRESS_LZORLE:
+		if (compress_level)
+			goto err;
+		break;
+	case COMPRESS_LZ4:
+		if ((compress_level && compress_level < LZ4HC_MIN_CLEVEL) ||
+				compress_level > LZ4HC_MAX_CLEVEL)
+			goto err;
+		break;
+	case COMPRESS_ZSTD:
+		if (!compress_level || compress_level > ZSTD_MAX_CLEVEL)
+			goto err;
+		break;
+	default:
+		goto err;
+	}
+
+	return true;
+
+err:
+	set_sbi_flag(sbi, SBI_NEED_FSCK);
+	f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported compress level: %u, run fsck to fix",
+		  __func__, inode->i_ino, compress_level);
+	return false;
+}
+
   static bool sanity_check_inode(struct inode *inode, struct page *node_page)
   {
   	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
@@ -285,36 +347,8 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
 
   	if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) &&
   			fi->i_flags & F2FS_COMPR_FL &&
-			F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
-						i_log_cluster_size)) {
-		if (ri->i_compress_algorithm >= COMPRESS_MAX) {
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
-			f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported "
-				"compress algorithm: %u, run fsck to fix",
-				  __func__, inode->i_ino,
-				  ri->i_compress_algorithm);
-			return false;
-		}
-		if (le64_to_cpu(ri->i_compr_blocks) >
-				SECTOR_TO_BLOCK(inode->i_blocks)) {
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
-			f2fs_warn(sbi, "%s: inode (ino=%lx) has inconsistent "
-				"i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix",
-				  __func__, inode->i_ino,
-				  le64_to_cpu(ri->i_compr_blocks),
-				  SECTOR_TO_BLOCK(inode->i_blocks));
-			return false;
-		}
-		if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
-			ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) {
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
-			f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported "
-				"log cluster size: %u, run fsck to fix",
Quoted 8 lines

fs/f2fs/inode.c static bool sanity_check_inode(struct inode *inode, struct page *node_page) · line 350

Quoted 6 lines
fs/f2fs/inode.c +1 -5
-				  __func__, inode->i_ino,
-				  ri->i_log_cluster_size);
-			return false;
-		}
-	}
+			F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_log_cluster_size))

Exceed 80 lines.

Ditto.

Thanks,

Quoted 48 lines

fs/f2fs/inode.c static bool sanity_check_inode(struct inode *inode, struct page *node_page) · line 351

Quoted 2 lines
fs/f2fs/inode.c +1 -0
+		sanity_check_compress_inode(inode, ri);

Missed to check return value?

Quoted 22 lines
fs/f2fs/inode.c +0 -0
   	return true;
   }
include/linux/zstd_lib.h +3 -0
index 79d55465d5c1..ff55f41c73d3 100644
@@ -88,6 +88,9 @@ ZSTDLIB_API const char* ZSTD_versionString(void);
   #  define ZSTD_CLEVEL_DEFAULT 3
   #endif
 
+/*-=====  Pre-defined compression levels  =====-*/
+#define ZSTD_MAX_CLEVEL     22
+
   /* *************************************
    *  Constants
    ***************************************/
lib/zstd/compress/clevels.h +0 -0
index d9a76112ec3a..b040d9d29089 100644

lib/zstd/compress/clevels.h line 19

Quoted 6 lines
lib/zstd/compress/clevels.h +0 -3
@@ -14,10 +14,6 @@
   #define ZSTD_STATIC_LINKING_ONLY  /* ZSTD_compressionParameters  */
   #include <linux/zstd.h>
 
-/*-=====  Pre-defined compression levels  =====-*/
-
-#define ZSTD_MAX_CLEVEL     22

Why not zstd_max_clevel()?

Thanks,

lib/zstd/compress/clevels.h line 19

Quoted 4 lines
lib/zstd/compress/clevels.h +0 -1
-
   __attribute__((__unused__))
 
   static const ZSTD_compressionParameters ZSTD_defaultCParameters[4][ZSTD_MAX_CLEVEL+1] = {

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

author
#3 lore raw
In reply to Yangtao Li

Hi Chao,

Quoted 1 line

Why not zstd_max_clevel()?

zstd_max_clevel() is only defined when CONFIG_F2FS_FS_ZSTD is enabled, using zstd_max_clevel() will result in compile errors otherwise.

If using the following code,

----------------------------------------------------------------------------

        switch (ri->i_compress_algorithm) {
        case COMPRESS_LZO:
        case COMPRESS_LZORLE:
                if (compress_level)
                        goto err;
                break;
        case COMPRESS_LZ4:
                if ((compress_level && compress_level < LZ4HC_MIN_CLEVEL) ||
                                compress_level > LZ4HC_MAX_CLEVEL)
                        goto err;
                break;
#ifdef CONFIG_F2FS_FS_ZSTD
        case COMPRESS_ZSTD:
                if (!compress_level || compress_level > zstd_max_clevel())
                        goto err;
                break;
#endif
        default:
                goto err;
        }

----------------------------------------------------------------------------

then we will get this result:

F2FS-fs (loop0): sanity_check_compress_inode: inode (ino=4) has
unsupported compress level: 0, run fsck to fix

Another way is to use the following code, which ignores the check for level when CONFIG_F2FS_FS_ZSTD is not enabled.

----------------------------------------------------------------------------

        switch (ri->i_compress_algorithm) {
        case COMPRESS_LZO:
        case COMPRESS_LZORLE:
                if (compress_level)
                        goto err;
                break;
        case COMPRESS_LZ4:
                if ((compress_level && compress_level < LZ4HC_MIN_CLEVEL) ||
                                compress_level > LZ4HC_MAX_CLEVEL)
                        goto err;
                break;
        case COMPRESS_ZSTD:
#ifdef CONFIG_F2FS_FS_ZSTD
                if (!compress_level || compress_level > zstd_max_clevel())
                        goto err;
                break;
#else
                return true;
#endif
        default:
                goto err;
        }

----------------------------------------------------------------------------

Perhaps exporting ZSTD_MAX_CLEVEL is a better choice?

Thx,
Yangtao

maintainer
#4 lore raw
Quoted 21 lines · Yangtao Li

Hi Chao,

Quoted 1 line

Why not zstd_max_clevel()?

zstd_max_clevel() is only defined when CONFIG_F2FS_FS_ZSTD is enabled, using zstd_max_clevel() will result in compile errors otherwise.

If using the following code,

----------------------------------------------------------------------------

         switch (ri->i_compress_algorithm) {
         case COMPRESS_LZO:
         case COMPRESS_LZORLE:
                 if (compress_level)
                         goto err;
                 break;
         case COMPRESS_LZ4:
                 if ((compress_level && compress_level < LZ4HC_MIN_CLEVEL) ||
                                 compress_level > LZ4HC_MAX_CLEVEL)
                         goto err;
                 break;
Quoted 2 lines

#ifdef CONFIG_F2FS_FS_ZSTD
case COMPRESS_ZSTD:

Hi Yangtao,

How about:

#ifdef CONFIG_F2FS_FS_ZSTD
	if (!compress_level || compress_level > zstd_max_clevel())
		goto err;
#endif
	break;
Quoted 46 lines
                 if (!compress_level || compress_level > zstd_max_clevel())
                         goto err;
                 break;
#endif
         default:
                 goto err;
         }

----------------------------------------------------------------------------

then we will get this result:

F2FS-fs (loop0): sanity_check_compress_inode: inode (ino=4) has
unsupported compress level: 0, run fsck to fix

Another way is to use the following code, which ignores the check for level when CONFIG_F2FS_FS_ZSTD is not enabled.

----------------------------------------------------------------------------

         switch (ri->i_compress_algorithm) {
         case COMPRESS_LZO:
         case COMPRESS_LZORLE:
                 if (compress_level)
                         goto err;
                 break;
         case COMPRESS_LZ4:
                 if ((compress_level && compress_level < LZ4HC_MIN_CLEVEL) ||
                                 compress_level > LZ4HC_MAX_CLEVEL)
                         goto err;
                 break;
         case COMPRESS_ZSTD:
#ifdef CONFIG_F2FS_FS_ZSTD
                 if (!compress_level || compress_level > zstd_max_clevel())
                         goto err;
                 break;
#else
                 return true;
#endif
         default:
                 goto err;
         }

----------------------------------------------------------------------------

Perhaps exporting ZSTD_MAX_CLEVEL is a better choice?

Thx,
Yangtao