erofs-utils: lib: fix up compact indexes for block size < 4096
authorGao Xiang <hsiangkao@linux.alibaba.com>
Thu, 23 Nov 2023 05:22:44 +0000 (13:22 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Sat, 13 Jan 2024 17:02:28 +0000 (01:02 +0800)
Let's keep in sync with kernel commit 8d2517aaeea3 ("erofs: fix up
compacted indexes for block size < 4096").

Original kernel commit:

Previously, the block size always equaled to PAGE_SIZE, therefore
`lclusterbits` couldn't be less than 12.

Since sub-page compressed blocks are now considered, `lobits` for
a lcluster in each pack cannot always be `lclusterbits` as before.
Otherwise, there is no enough room for the special value
`Z_EROFS_LI_D0_CBLKCNT`.

To support smaller block sizes, `lobits` for each compacted lcluster is
now calculated as:
   lobits = max(lclusterbits, ilog2(Z_EROFS_LI_D0_CBLKCNT) + 1)

Reviewed-by: Yue Hu <huyue2@coolpad.com>
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20231123052245.868698-1-hsiangkao@linux.alibaba.com
lib/zmap.c

index 81fa22b6c860e5b8ed62759f4d5ba4ef8eb2f7b3..12eb426687115c12b3604befbb7327d8c0e9da91 100644 (file)
@@ -197,29 +197,26 @@ static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
 }
 
 static unsigned int decode_compactedbits(unsigned int lobits,
-                                        unsigned int lomask,
                                         u8 *in, unsigned int pos, u8 *type)
 {
        const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
-       const unsigned int lo = v & lomask;
+       const unsigned int lo = v & ((1 << lobits) - 1);
 
        *type = (v >> lobits) & 3;
        return lo;
 }
 
-static int get_compacted_la_distance(unsigned int lclusterbits,
+static int get_compacted_la_distance(unsigned int lobits,
                                     unsigned int encodebits,
                                     unsigned int vcnt, u8 *in, int i)
 {
-       const unsigned int lomask = (1 << lclusterbits) - 1;
        unsigned int lo, d1 = 0;
        u8 type;
 
        DBG_BUGON(i >= vcnt);
 
        do {
-               lo = decode_compactedbits(lclusterbits, lomask,
-                                         in, encodebits * i, &type);
+               lo = decode_compactedbits(lobits, in, encodebits * i, &type);
 
                if (type != Z_EROFS_LCLUSTER_TYPE_NONHEAD)
                        return d1;
@@ -238,15 +235,14 @@ static int unpack_compacted_index(struct z_erofs_maprecorder *m,
 {
        struct erofs_inode *const vi = m->inode;
        const unsigned int lclusterbits = vi->z_logical_clusterbits;
-       const unsigned int lomask = (1 << lclusterbits) - 1;
-       unsigned int vcnt, base, lo, encodebits, nblk, eofs;
+       unsigned int vcnt, base, lo, lobits, encodebits, nblk, eofs;
        int i;
        u8 *in, type;
        bool big_pcluster;
 
        if (1 << amortizedshift == 4 && lclusterbits <= 14)
                vcnt = 2;
-       else if (1 << amortizedshift == 2 && lclusterbits == 12)
+       else if (1 << amortizedshift == 2 && lclusterbits <= 12)
                vcnt = 16;
        else
                return -EOPNOTSUPP;
@@ -255,6 +251,7 @@ static int unpack_compacted_index(struct z_erofs_maprecorder *m,
        m->nextpackoff = round_down(pos, vcnt << amortizedshift) +
                         (vcnt << amortizedshift);
        big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
+       lobits = max(lclusterbits, ilog2(Z_EROFS_LI_D0_CBLKCNT) + 1U);
        encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
        eofs = erofs_blkoff(vi->sbi, pos);
        base = round_down(eofs, vcnt << amortizedshift);
@@ -262,15 +259,14 @@ static int unpack_compacted_index(struct z_erofs_maprecorder *m,
 
        i = (eofs - base) >> amortizedshift;
 
-       lo = decode_compactedbits(lclusterbits, lomask,
-                                 in, encodebits * i, &type);
+       lo = decode_compactedbits(lobits, in, encodebits * i, &type);
        m->type = type;
        if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
                m->clusterofs = 1 << lclusterbits;
 
                /* figure out lookahead_distance: delta[1] if needed */
                if (lookahead)
-                       m->delta[1] = get_compacted_la_distance(lclusterbits,
+                       m->delta[1] = get_compacted_la_distance(lobits,
                                                encodebits, vcnt, in, i);
                if (lo & Z_EROFS_LI_D0_CBLKCNT) {
                        if (!big_pcluster) {
@@ -289,8 +285,8 @@ static int unpack_compacted_index(struct z_erofs_maprecorder *m,
                 * of which lo saves delta[1] rather than delta[0].
                 * Hence, get delta[0] by the previous lcluster indirectly.
                 */
-               lo = decode_compactedbits(lclusterbits, lomask,
-                                         in, encodebits * (i - 1), &type);
+               lo = decode_compactedbits(lobits, in,
+                                         encodebits * (i - 1), &type);
                if (type != Z_EROFS_LCLUSTER_TYPE_NONHEAD)
                        lo = 0;
                else if (lo & Z_EROFS_LI_D0_CBLKCNT)
@@ -305,8 +301,8 @@ static int unpack_compacted_index(struct z_erofs_maprecorder *m,
                nblk = 1;
                while (i > 0) {
                        --i;
-                       lo = decode_compactedbits(lclusterbits, lomask,
-                                                 in, encodebits * i, &type);
+                       lo = decode_compactedbits(lobits, in,
+                                                 encodebits * i, &type);
                        if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD)
                                i -= lo;
 
@@ -317,8 +313,8 @@ static int unpack_compacted_index(struct z_erofs_maprecorder *m,
                nblk = 0;
                while (i > 0) {
                        --i;
-                       lo = decode_compactedbits(lclusterbits, lomask,
-                                                 in, encodebits * i, &type);
+                       lo = decode_compactedbits(lobits, in,
+                                                 encodebits * i, &type);
                        if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
                                if (lo & Z_EROFS_LI_D0_CBLKCNT) {
                                        --i;