Skip to content

Commit c571fbb

Browse files
Sheng YongJaegeuk Kim
authored andcommitted
f2fs: add helper to check compression level
This patch adds a helper function to check if compression level is valid. Signed-off-by: Sheng Yong <shengyong@oppo.com> Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 94c8431 commit c571fbb

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

fs/f2fs/compress.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct f2fs_compress_ops {
5555
int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
5656
void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
5757
int (*decompress_pages)(struct decompress_io_ctx *dic);
58+
bool (*is_level_valid)(int level);
5859
};
5960

6061
static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
@@ -308,11 +309,21 @@ static int lz4_decompress_pages(struct decompress_io_ctx *dic)
308309
return 0;
309310
}
310311

312+
static bool lz4_is_level_valid(int lvl)
313+
{
314+
#ifdef CONFIG_F2FS_FS_LZ4HC
315+
return !lvl || (lvl >= LZ4HC_MIN_CLEVEL && lvl <= LZ4HC_MAX_CLEVEL);
316+
#else
317+
return lvl == 0;
318+
#endif
319+
}
320+
311321
static const struct f2fs_compress_ops f2fs_lz4_ops = {
312322
.init_compress_ctx = lz4_init_compress_ctx,
313323
.destroy_compress_ctx = lz4_destroy_compress_ctx,
314324
.compress_pages = lz4_compress_pages,
315325
.decompress_pages = lz4_decompress_pages,
326+
.is_level_valid = lz4_is_level_valid,
316327
};
317328
#endif
318329

@@ -476,13 +487,19 @@ static int zstd_decompress_pages(struct decompress_io_ctx *dic)
476487
return 0;
477488
}
478489

490+
static bool zstd_is_level_valid(int lvl)
491+
{
492+
return lvl >= zstd_min_clevel() && lvl <= zstd_max_clevel();
493+
}
494+
479495
static const struct f2fs_compress_ops f2fs_zstd_ops = {
480496
.init_compress_ctx = zstd_init_compress_ctx,
481497
.destroy_compress_ctx = zstd_destroy_compress_ctx,
482498
.compress_pages = zstd_compress_pages,
483499
.init_decompress_ctx = zstd_init_decompress_ctx,
484500
.destroy_decompress_ctx = zstd_destroy_decompress_ctx,
485501
.decompress_pages = zstd_decompress_pages,
502+
.is_level_valid = zstd_is_level_valid,
486503
};
487504
#endif
488505

@@ -541,6 +558,16 @@ bool f2fs_is_compress_backend_ready(struct inode *inode)
541558
return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
542559
}
543560

561+
bool f2fs_is_compress_level_valid(int alg, int lvl)
562+
{
563+
const struct f2fs_compress_ops *cops = f2fs_cops[alg];
564+
565+
if (cops->is_level_valid)
566+
return cops->is_level_valid(lvl);
567+
568+
return lvl == 0;
569+
}
570+
544571
static mempool_t *compress_page_pool;
545572
static int num_compress_pages = 512;
546573
module_param(num_compress_pages, uint, 0444);

fs/f2fs/f2fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4240,6 +4240,7 @@ bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
42404240
int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
42414241
void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
42424242
bool f2fs_is_compress_backend_ready(struct inode *inode);
4243+
bool f2fs_is_compress_level_valid(int alg, int lvl);
42434244
int __init f2fs_init_compress_mempool(void);
42444245
void f2fs_destroy_compress_mempool(void);
42454246
void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task);
@@ -4304,6 +4305,7 @@ static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
43044305
/* not support compression */
43054306
return false;
43064307
}
4308+
static inline bool f2fs_is_compress_level_valid(int alg, int lvl) { return false; }
43074309
static inline struct page *f2fs_compress_control_page(struct page *page)
43084310
{
43094311
WARN_ON_ONCE(1);

fs/f2fs/super.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
604604
if (kstrtouint(str + 1, 10, &level))
605605
return -EINVAL;
606606

607-
if (level < LZ4HC_MIN_CLEVEL || level > LZ4HC_MAX_CLEVEL) {
607+
if (!f2fs_is_compress_level_valid(COMPRESS_LZ4, level)) {
608608
f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
609609
return -EINVAL;
610610
}
@@ -642,7 +642,7 @@ static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
642642
if (kstrtouint(str + 1, 10, &level))
643643
return -EINVAL;
644644

645-
if (level < zstd_min_clevel() || level > zstd_max_clevel()) {
645+
if (!f2fs_is_compress_level_valid(COMPRESS_ZSTD, level)) {
646646
f2fs_info(sbi, "invalid zstd compress level: %d", level);
647647
return -EINVAL;
648648
}

0 commit comments

Comments
 (0)