Skip to content

Commit efb4fb0

Browse files
committed
erofs: simplify compression configuration parser
Move erofs_load_compr_cfgs() into decompressor.c as well as introduce a callback instead of a hard-coded switch for each algorithm for simplicity. Reviewed-by: Chao Yu <[email protected]> Signed-off-by: Gao Xiang <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 798eeca commit efb4fb0

File tree

6 files changed

+79
-108
lines changed

6 files changed

+79
-108
lines changed

fs/erofs/compress.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ struct z_erofs_decompress_req {
2121
};
2222

2323
struct z_erofs_decompressor {
24+
int (*config)(struct super_block *sb, struct erofs_super_block *dsb,
25+
void *data, int size);
2426
int (*decompress)(struct z_erofs_decompress_req *rq,
2527
struct page **pagepool);
2628
char *name;
@@ -92,6 +94,10 @@ int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
9294
extern const struct z_erofs_decompressor erofs_decompressors[];
9395

9496
/* prototypes for specific algorithms */
97+
int z_erofs_load_lzma_config(struct super_block *sb,
98+
struct erofs_super_block *dsb, void *data, int size);
99+
int z_erofs_load_deflate_config(struct super_block *sb,
100+
struct erofs_super_block *dsb, void *data, int size);
95101
int z_erofs_lzma_decompress(struct z_erofs_decompress_req *rq,
96102
struct page **pagepool);
97103
int z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,

fs/erofs/decompressor.c

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ struct z_erofs_lz4_decompress_ctx {
2424
unsigned int oend;
2525
};
2626

27-
int z_erofs_load_lz4_config(struct super_block *sb,
28-
struct erofs_super_block *dsb,
29-
struct z_erofs_lz4_cfgs *lz4, int size)
27+
static int z_erofs_load_lz4_config(struct super_block *sb,
28+
struct erofs_super_block *dsb, void *data, int size)
3029
{
3130
struct erofs_sb_info *sbi = EROFS_SB(sb);
31+
struct z_erofs_lz4_cfgs *lz4 = data;
3232
u16 distance;
3333

3434
if (lz4) {
@@ -370,19 +370,75 @@ const struct z_erofs_decompressor erofs_decompressors[] = {
370370
.name = "interlaced"
371371
},
372372
[Z_EROFS_COMPRESSION_LZ4] = {
373+
.config = z_erofs_load_lz4_config,
373374
.decompress = z_erofs_lz4_decompress,
374375
.name = "lz4"
375376
},
376377
#ifdef CONFIG_EROFS_FS_ZIP_LZMA
377378
[Z_EROFS_COMPRESSION_LZMA] = {
379+
.config = z_erofs_load_lzma_config,
378380
.decompress = z_erofs_lzma_decompress,
379381
.name = "lzma"
380382
},
381383
#endif
382384
#ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
383385
[Z_EROFS_COMPRESSION_DEFLATE] = {
386+
.config = z_erofs_load_deflate_config,
384387
.decompress = z_erofs_deflate_decompress,
385388
.name = "deflate"
386389
},
387390
#endif
388391
};
392+
393+
int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb)
394+
{
395+
struct erofs_sb_info *sbi = EROFS_SB(sb);
396+
struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
397+
unsigned int algs, alg;
398+
erofs_off_t offset;
399+
int size, ret = 0;
400+
401+
if (!erofs_sb_has_compr_cfgs(sbi)) {
402+
sbi->available_compr_algs = Z_EROFS_COMPRESSION_LZ4;
403+
return z_erofs_load_lz4_config(sb, dsb, NULL, 0);
404+
}
405+
406+
sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
407+
if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
408+
erofs_err(sb, "unidentified algorithms %x, please upgrade kernel",
409+
sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
410+
return -EOPNOTSUPP;
411+
}
412+
413+
erofs_init_metabuf(&buf, sb);
414+
offset = EROFS_SUPER_OFFSET + sbi->sb_size;
415+
alg = 0;
416+
for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
417+
void *data;
418+
419+
if (!(algs & 1))
420+
continue;
421+
422+
data = erofs_read_metadata(sb, &buf, &offset, &size);
423+
if (IS_ERR(data)) {
424+
ret = PTR_ERR(data);
425+
break;
426+
}
427+
428+
if (alg >= ARRAY_SIZE(erofs_decompressors) ||
429+
!erofs_decompressors[alg].config) {
430+
erofs_err(sb, "algorithm %d isn't enabled on this kernel",
431+
alg);
432+
ret = -EOPNOTSUPP;
433+
} else {
434+
ret = erofs_decompressors[alg].config(sb,
435+
dsb, data, size);
436+
}
437+
438+
kfree(data);
439+
if (ret)
440+
break;
441+
}
442+
erofs_put_metabuf(&buf);
443+
return ret;
444+
}

fs/erofs/decompressor_deflate.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ int __init z_erofs_deflate_init(void)
7777
}
7878

7979
int z_erofs_load_deflate_config(struct super_block *sb,
80-
struct erofs_super_block *dsb,
81-
struct z_erofs_deflate_cfgs *dfl, int size)
80+
struct erofs_super_block *dsb, void *data, int size)
8281
{
82+
struct z_erofs_deflate_cfgs *dfl = data;
83+
8384
if (!dfl || size < sizeof(struct z_erofs_deflate_cfgs)) {
8485
erofs_err(sb, "invalid deflate cfgs, size=%u", size);
8586
return -EINVAL;

fs/erofs/decompressor_lzma.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ int __init z_erofs_lzma_init(void)
7272
}
7373

7474
int z_erofs_load_lzma_config(struct super_block *sb,
75-
struct erofs_super_block *dsb,
76-
struct z_erofs_lzma_cfgs *lzma, int size)
75+
struct erofs_super_block *dsb, void *data, int size)
7776
{
7877
static DEFINE_MUTEX(lzma_resize_mutex);
78+
struct z_erofs_lzma_cfgs *lzma = data;
7979
unsigned int dict_size, i;
8080
struct z_erofs_lzma *strm, *head = NULL;
8181
int err;

fs/erofs/internal.h

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,6 @@ int __init z_erofs_init_zip_subsystem(void);
469469
void z_erofs_exit_zip_subsystem(void);
470470
int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
471471
struct erofs_workgroup *egrp);
472-
int z_erofs_load_lz4_config(struct super_block *sb,
473-
struct erofs_super_block *dsb,
474-
struct z_erofs_lz4_cfgs *lz4, int len);
475472
int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
476473
int flags);
477474
void *erofs_get_pcpubuf(unsigned int requiredpages);
@@ -480,23 +477,14 @@ int erofs_pcpubuf_growsize(unsigned int nrpages);
480477
void __init erofs_pcpubuf_init(void);
481478
void erofs_pcpubuf_exit(void);
482479
int erofs_init_managed_cache(struct super_block *sb);
480+
int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb);
483481
#else
484482
static inline void erofs_shrinker_register(struct super_block *sb) {}
485483
static inline void erofs_shrinker_unregister(struct super_block *sb) {}
486484
static inline int erofs_init_shrinker(void) { return 0; }
487485
static inline void erofs_exit_shrinker(void) {}
488486
static inline int z_erofs_init_zip_subsystem(void) { return 0; }
489487
static inline void z_erofs_exit_zip_subsystem(void) {}
490-
static inline int z_erofs_load_lz4_config(struct super_block *sb,
491-
struct erofs_super_block *dsb,
492-
struct z_erofs_lz4_cfgs *lz4, int len)
493-
{
494-
if (lz4 || dsb->u1.lz4_max_distance) {
495-
erofs_err(sb, "lz4 algorithm isn't enabled");
496-
return -EINVAL;
497-
}
498-
return 0;
499-
}
500488
static inline void erofs_pcpubuf_init(void) {}
501489
static inline void erofs_pcpubuf_exit(void) {}
502490
static inline int erofs_init_managed_cache(struct super_block *sb) { return 0; }
@@ -505,41 +493,17 @@ static inline int erofs_init_managed_cache(struct super_block *sb) { return 0; }
505493
#ifdef CONFIG_EROFS_FS_ZIP_LZMA
506494
int __init z_erofs_lzma_init(void);
507495
void z_erofs_lzma_exit(void);
508-
int z_erofs_load_lzma_config(struct super_block *sb,
509-
struct erofs_super_block *dsb,
510-
struct z_erofs_lzma_cfgs *lzma, int size);
511496
#else
512497
static inline int z_erofs_lzma_init(void) { return 0; }
513498
static inline int z_erofs_lzma_exit(void) { return 0; }
514-
static inline int z_erofs_load_lzma_config(struct super_block *sb,
515-
struct erofs_super_block *dsb,
516-
struct z_erofs_lzma_cfgs *lzma, int size) {
517-
if (lzma) {
518-
erofs_err(sb, "lzma algorithm isn't enabled");
519-
return -EINVAL;
520-
}
521-
return 0;
522-
}
523499
#endif /* !CONFIG_EROFS_FS_ZIP_LZMA */
524500

525501
#ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
526502
int __init z_erofs_deflate_init(void);
527503
void z_erofs_deflate_exit(void);
528-
int z_erofs_load_deflate_config(struct super_block *sb,
529-
struct erofs_super_block *dsb,
530-
struct z_erofs_deflate_cfgs *dfl, int size);
531504
#else
532505
static inline int z_erofs_deflate_init(void) { return 0; }
533506
static inline int z_erofs_deflate_exit(void) { return 0; }
534-
static inline int z_erofs_load_deflate_config(struct super_block *sb,
535-
struct erofs_super_block *dsb,
536-
struct z_erofs_deflate_cfgs *dfl, int size) {
537-
if (dfl) {
538-
erofs_err(sb, "deflate algorithm isn't enabled");
539-
return -EINVAL;
540-
}
541-
return 0;
542-
}
543507
#endif /* !CONFIG_EROFS_FS_ZIP_DEFLATE */
544508

545509
#ifdef CONFIG_EROFS_FS_ONDEMAND

fs/erofs/super.c

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -156,68 +156,15 @@ void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
156156
return buffer;
157157
}
158158

159-
#ifdef CONFIG_EROFS_FS_ZIP
160-
static int erofs_load_compr_cfgs(struct super_block *sb,
161-
struct erofs_super_block *dsb)
159+
#ifndef CONFIG_EROFS_FS_ZIP
160+
static int z_erofs_parse_cfgs(struct super_block *sb,
161+
struct erofs_super_block *dsb)
162162
{
163-
struct erofs_sb_info *sbi = EROFS_SB(sb);
164-
struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
165-
unsigned int algs, alg;
166-
erofs_off_t offset;
167-
int size, ret = 0;
168-
169-
sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
170-
if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
171-
erofs_err(sb, "try to load compressed fs with unsupported algorithms %x",
172-
sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
173-
return -EINVAL;
174-
}
175-
176-
erofs_init_metabuf(&buf, sb);
177-
offset = EROFS_SUPER_OFFSET + sbi->sb_size;
178-
alg = 0;
179-
for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
180-
void *data;
181-
182-
if (!(algs & 1))
183-
continue;
184-
185-
data = erofs_read_metadata(sb, &buf, &offset, &size);
186-
if (IS_ERR(data)) {
187-
ret = PTR_ERR(data);
188-
break;
189-
}
163+
if (!dsb->u1.available_compr_algs)
164+
return 0;
190165

191-
switch (alg) {
192-
case Z_EROFS_COMPRESSION_LZ4:
193-
ret = z_erofs_load_lz4_config(sb, dsb, data, size);
194-
break;
195-
case Z_EROFS_COMPRESSION_LZMA:
196-
ret = z_erofs_load_lzma_config(sb, dsb, data, size);
197-
break;
198-
case Z_EROFS_COMPRESSION_DEFLATE:
199-
ret = z_erofs_load_deflate_config(sb, dsb, data, size);
200-
break;
201-
default:
202-
DBG_BUGON(1);
203-
ret = -EFAULT;
204-
}
205-
kfree(data);
206-
if (ret)
207-
break;
208-
}
209-
erofs_put_metabuf(&buf);
210-
return ret;
211-
}
212-
#else
213-
static int erofs_load_compr_cfgs(struct super_block *sb,
214-
struct erofs_super_block *dsb)
215-
{
216-
if (dsb->u1.available_compr_algs) {
217-
erofs_err(sb, "try to load compressed fs when compression is disabled");
218-
return -EINVAL;
219-
}
220-
return 0;
166+
erofs_err(sb, "compression disabled, unable to mount compressed EROFS");
167+
return -EOPNOTSUPP;
221168
}
222169
#endif
223170

@@ -406,10 +353,7 @@ static int erofs_read_superblock(struct super_block *sb)
406353
}
407354

408355
/* parse on-disk compression configurations */
409-
if (erofs_sb_has_compr_cfgs(sbi))
410-
ret = erofs_load_compr_cfgs(sb, dsb);
411-
else
412-
ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
356+
ret = z_erofs_parse_cfgs(sb, dsb);
413357
if (ret < 0)
414358
goto out;
415359

0 commit comments

Comments
 (0)