When mounting the EROFS file system, it is necessary to check the
available compression algorithms. At this time, the for_each_set_bit
function can be used to simplify the code logic.

Signed-off-by: Yuwen Chen <[email protected]>
---

v2 -> v3:
    - rebase the patch
    - remove the unnecessary judgment logic

v1 -> v2:
    - revert the modifications to the fs/erofs/internal.h

 fs/erofs/decompressor.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/fs/erofs/decompressor.c b/fs/erofs/decompressor.c
index d5d0902763917..15b464a589939 100644
--- a/fs/erofs/decompressor.c
+++ b/fs/erofs/decompressor.c
@@ -452,7 +452,7 @@ int z_erofs_parse_cfgs(struct super_block *sb, struct 
erofs_super_block *dsb)
 {
        struct erofs_sb_info *sbi = EROFS_SB(sb);
        struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
-       unsigned int algs, alg;
+       unsigned long algs, alg;
        erofs_off_t offset;
        int size, ret = 0;
 
@@ -461,33 +461,30 @@ int z_erofs_parse_cfgs(struct super_block *sb, struct 
erofs_super_block *dsb)
                return z_erofs_load_lz4_config(sb, dsb, NULL, 0);
        }
 
-       sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
-       if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
-               erofs_err(sb, "unidentified algorithms %x, please upgrade 
kernel",
-                         sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
+       algs = le16_to_cpu(dsb->u1.available_compr_algs);
+       sbi->available_compr_algs = algs;
+       if (algs & ~Z_EROFS_ALL_COMPR_ALGS) {
+               erofs_err(sb, "unidentified algorithms %lx, please upgrade 
kernel",
+                         algs & ~Z_EROFS_ALL_COMPR_ALGS);
                return -EOPNOTSUPP;
        }
 
        (void)erofs_init_metabuf(&buf, sb, false);
        offset = EROFS_SUPER_OFFSET + sbi->sb_size;
-       alg = 0;
-       for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
+       for_each_set_bit(alg, &algs, Z_EROFS_COMPRESSION_MAX) {
                const struct z_erofs_decompressor *dec = z_erofs_decomp[alg];
                void *data;
 
-               if (!(algs & 1))
-                       continue;
-
                data = erofs_read_metadata(sb, &buf, &offset, &size);
                if (IS_ERR(data)) {
                        ret = PTR_ERR(data);
                        break;
                }
 
-               if (alg < Z_EROFS_COMPRESSION_MAX && dec && dec->config) {
+               if (dec && dec->config) {
                        ret = dec->config(sb, dsb, data, size);
                } else {
-                       erofs_err(sb, "algorithm %d isn't enabled on this 
kernel",
+                       erofs_err(sb, "algorithm %ld isn't enabled on this 
kernel",
                                  alg);
                        ret = -EOPNOTSUPP;
                }
-- 
2.34.1


Reply via email to