f2fs: add sanity compress level check for compressed file
PATCH v2 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.
Signed-off-by Yangtao Li <[email protected]>
---
v2:
-convert to zstd_max_clevel()
fs/f2fs/inode.c | 96 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 29 deletions(-)
fs/f2fs/inode.c +67 -29
@@ -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,69 @@ 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 : +#ifdef CONFIG_F2FS_FS_ZSTD +if (!compress_level || compress_level >zstd_max_clevel ()) + goto err; +#endif +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);
@@ -286,35 +351,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; - } - } + i_log_cluster_size)) +return sanity_check_compress_inode (inode, ri);return true; }
Signature · 1 line
2.35.1
Chao Yu
maintainerQuoted 57 lines · Yangtao Li
Commit
3fde13f817e2("f2fs: compress: support compress level") forgot to do basic compress level check, let's add it.Signed-off-by Yangtao Li <[email protected]>---
v2:(quoted diff) +0
-1-convert to zstd_max_clevel() fs/f2fs/inode.c | 96 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 29 deletions(-)fs/f2fs/inode.c +32
-0@@ -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,69 @@ 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; + }
fs/f2fs/inode.c void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page) · line 246
Quoted 10 lines
fs/f2fs/inode.c +10
-0+ + 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 :
It needs to cover below codes w/ CONFIG_F2FS_FS_LZ4HC and CONFIG_F2FS_FS_LZ4?
Quoted 10 lines
fs/f2fs/inode.c +10
-0+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; +#endif +break ;
fs/f2fs/inode.c void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page) · line 266
Quoted 10 lines
fs/f2fs/inode.c +10
-0+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);
Move above codes to default case?
Thanks,
Quoted 44 lines
fs/f2fs/inode.c +5
-29+return false; +} +static bool sanity_check_inode (struct inode *inode,struct page *node_page) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);@@ -286,35 +351,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; - } - } + i_log_cluster_size)) +return sanity_check_compress_inode (inode, ri);return true; }