Skip to contentSkip to conversation

erofs-utils: lib: Fix 8MB bug on uncompressed extent size

PATCH Superseded Kelvin Zhang via Linux-erofs 2 participants 11 messages Dec 22, 2021 – Feb 9, 2022 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: Fix 8MB bug on uncompressed extent size

Previously, uncompressed extent can be at most 8MB before mkfs.erofs crashes on some error condition. This is due to a minor bug in how compressed indices are encoded. This patch fixes the issue.

---
 include/erofs_fs.h |  2 +-
 lib/compress.c     | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)
include/erofs_fs.h +1 -1
index 9a91877..13eaf24 100644
@@ -353,7 +353,7 @@ enum {
  * compressed block count of a compressed extent (in logical clusters, aka.
  * block count of a pcluster).
  */
-#define Z_EROFS_VLE_DI_D0_CBLKCNT		(1 << 11)
+#define Z_EROFS_VLE_DI_D0_CBLKCNT		(1U << 11)
 
 struct z_erofs_vle_decompressed_index {
 	__le16 di_advise;
lib/compress.c +20 -1
index 98be7a2..23e571c 100644
@@ -97,7 +97,26 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
 		} else if (d0) {
 			type = Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD;
 
-			di.di_u.delta[0] = cpu_to_le16(d0);
+			/* If the |Z_EROFS_VLE_DI_D0_CBLKCNT| bit is set, parser
+			 * will interpret |delta[0]| as size of pcluster, rather
+			 * than distance to last head cluster. Normally this
+			 * isn't a problem, because uncompressed extent size are
+			 * below Z_EROFS_VLE_DI_D0_CBLKCNT * BLOCK_SIZE = 8MB.
+			 * But with large pcluster it's possible to go over this
+			 * number, resulting in corrupted compressed indices.
+			 * To solve this, we use Z_EROFS_VLE_DI_D0_CBLKCNT-1 if
+			 * the uncompressed extent size goes above 8MB. This is
+			 * OK because if kernel sees another non-head cluster
+			 * after going back by |delta[0]| blocks, kernel will
+			 * just keep looking back.
+			 */
+			if (d0 & Z_EROFS_VLE_DI_D0_CBLKCNT) {
+				di.di_u.delta[0] = max(
+					d0 & (~Z_EROFS_VLE_DI_D0_CBLKCNT),
+					Z_EROFS_VLE_DI_D0_CBLKCNT-1);
+			} else {
+				di.di_u.delta[0] = cpu_to_le16(d0);
+			}
 			di.di_u.delta[1] = cpu_to_le16(d1);
 		} else {
 			type = raw ? Z_EROFS_VLE_CLUSTER_TYPE_PLAIN :
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#1 lore raw
Quoted 5 lines · Kelvin Zhang

Previously, uncompressed extent can be at most 8MB before mkfs.erofs crashes on some error condition. This is due to a minor bug in how compressed indices are encoded. This patch fixes the issue.

I have to hold this for a while and look into (evaluate) that when I get a full free time... (maybe a week later.) I understand it's not quite urgent for all of us currently..

I still stuck into ztailpacking inline feature for now...

Thanks,
Gao Xiang

Quoted 52 lines
---
 include/erofs_fs.h |  2 +-
 lib/compress.c     | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)
include/erofs_fs.h +1 -1
index 9a91877..13eaf24 100644
@@ -353,7 +353,7 @@ enum {
  * compressed block count of a compressed extent (in logical clusters, aka.
  * block count of a pcluster).
  */
-#define Z_EROFS_VLE_DI_D0_CBLKCNT		(1 << 11)
+#define Z_EROFS_VLE_DI_D0_CBLKCNT		(1U << 11)
 
 struct z_erofs_vle_decompressed_index {
 	__le16 di_advise;
lib/compress.c +20 -1
index 98be7a2..23e571c 100644
@@ -97,7 +97,26 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
 		} else if (d0) {
 			type = Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD;
 
-			di.di_u.delta[0] = cpu_to_le16(d0);
+			/* If the |Z_EROFS_VLE_DI_D0_CBLKCNT| bit is set, parser
+			 * will interpret |delta[0]| as size of pcluster, rather
+			 * than distance to last head cluster. Normally this
+			 * isn't a problem, because uncompressed extent size are
+			 * below Z_EROFS_VLE_DI_D0_CBLKCNT * BLOCK_SIZE = 8MB.
+			 * But with large pcluster it's possible to go over this
+			 * number, resulting in corrupted compressed indices.
+			 * To solve this, we use Z_EROFS_VLE_DI_D0_CBLKCNT-1 if
+			 * the uncompressed extent size goes above 8MB. This is
+			 * OK because if kernel sees another non-head cluster
+			 * after going back by |delta[0]| blocks, kernel will
+			 * just keep looking back.
+			 */
+			if (d0 & Z_EROFS_VLE_DI_D0_CBLKCNT) {
+				di.di_u.delta[0] = max(
+					d0 & (~Z_EROFS_VLE_DI_D0_CBLKCNT),
+					Z_EROFS_VLE_DI_D0_CBLKCNT-1);
+			} else {
+				di.di_u.delta[0] = cpu_to_le16(d0);
+			}
 			di.di_u.delta[1] = cpu_to_le16(d1);
 		} else {
 			type = raw ? Z_EROFS_VLE_CLUSTER_TYPE_PLAIN :
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#2 lore raw

friendly ping : )

Quoted 69 lines · Gao Xiang
Quoted 5 lines · Kelvin Zhang

Previously, uncompressed extent can be at most 8MB before mkfs.erofs crashes on some error condition. This is due to a minor bug in how compressed indices are encoded. This patch fixes the issue.

I have to hold this for a while and look into (evaluate) that when I get a full free time... (maybe a week later.) I understand it's not quite urgent for all of us currently..

I still stuck into ztailpacking inline feature for now...

Thanks,
Gao Xiang

Quoted 52 lines
---
 include/erofs_fs.h |  2 +-
 lib/compress.c     | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)
include/erofs_fs.h +1 -1
index 9a91877..13eaf24 100644
@@ -353,7 +353,7 @@ enum {
  * compressed block count of a compressed extent (in logical clusters, aka.
  * block count of a pcluster).
  */
-#define Z_EROFS_VLE_DI_D0_CBLKCNT            (1 << 11)
+#define Z_EROFS_VLE_DI_D0_CBLKCNT            (1U << 11)
 
 struct z_erofs_vle_decompressed_index {
      __le16 di_advise;
lib/compress.c +20 -2
index 98be7a2..23e571c 100644
@@ -97,7 +97,26 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
              } else if (d0) {
                      type = Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD;
 
-                     di.di_u.delta[0] = cpu_to_le16(d0);
+                     /* If the |Z_EROFS_VLE_DI_D0_CBLKCNT| bit is set, parser
+                      * will interpret |delta[0]| as size of pcluster, rather
+                      * than distance to last head cluster. Normally this
+                      * isn't a problem, because uncompressed extent size are
+                      * below Z_EROFS_VLE_DI_D0_CBLKCNT * BLOCK_SIZE = 8MB.
+                      * But with large pcluster it's possible to go over this
+                      * number, resulting in corrupted compressed indices.
+                      * To solve this, we use Z_EROFS_VLE_DI_D0_CBLKCNT-1 if
+                      * the uncompressed extent size goes above 8MB. This is
+                      * OK because if kernel sees another non-head cluster
+                      * after going back by |delta[0]| blocks, kernel will
+                      * just keep looking back.
+                      */
+                     if (d0 & Z_EROFS_VLE_DI_D0_CBLKCNT) {
+                             di.di_u.delta[0] = max(
+                                     d0 & (~Z_EROFS_VLE_DI_D0_CBLKCNT),
+                                     Z_EROFS_VLE_DI_D0_CBLKCNT-1);
+                     } else {
+                             di.di_u.delta[0] = cpu_to_le16(d0);
+                     }
                      di.di_u.delta[1] = cpu_to_le16(d1);
              } else {
                      type = raw ? Z_EROFS_VLE_CLUSTER_TYPE_PLAIN :
--

2.34.1.448.ga2b2bfdf31-goog

Signature · 3 lines
Sincerely,

Kelvin Zhang
#3 lore raw

Hi Kelvin,

Quoted 1 line · Kelvin Zhang

friendly ping : )

Thanks for your reminder.

It's still on my TODO list with many other stuffs along with many internal paperwork.
I will finish it no later than the end of our Spring Festival.

Thanks,
Gao Xiang

#4 lore raw
In reply to Kelvin Zhang via Linux-erofs

Hi Kelvin,

Quoted 15 lines · Kelvin Zhang

Previously, uncompressed extent can be at most 8MB before mkfs.erofs crashes on some error condition. This is due to a minor bug in how compressed indices are encoded. This patch fixes the issue.

---
 include/erofs_fs.h |  2 +-
 lib/compress.c     | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)
include/erofs_fs.h +0 -0
index 9a91877..13eaf24 100644

include/erofs_fs.h enum { · line 356

Quoted 5 lines
include/erofs_fs.h +1 -1
@@ -353,7 +353,7 @@ enum {
  * compressed block count of a compressed extent (in logical clusters, aka.
  * block count of a pcluster).
  */
-#define Z_EROFS_VLE_DI_D0_CBLKCNT		(1 << 11)
+#define Z_EROFS_VLE_DI_D0_CBLKCNT		(1U << 11)

If erofs_fs.h update is necessary, I prefer to update in-kernel header first. Would you mind making a kernel patch for this if needed?

Quoted 15 lines
include/erofs_fs.h +0 -0
 struct z_erofs_vle_decompressed_index {
 	__le16 di_advise;
lib/compress.c +3 -1
index 98be7a2..23e571c 100644
@@ -97,7 +97,26 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
 		} else if (d0) {
 			type = Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD;
 
-			di.di_u.delta[0] = cpu_to_le16(d0);
+			/* If the |Z_EROFS_VLE_DI_D0_CBLKCNT| bit is set, parser
+			 * will interpret |delta[0]| as size of pcluster, rather
+			 * than distance to last head cluster. Normally this

lib/compress.c static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx, · line 112

Quoted 10 lines
lib/compress.c +10 -0
+			 * isn't a problem, because uncompressed extent size are
+			 * below Z_EROFS_VLE_DI_D0_CBLKCNT * BLOCK_SIZE = 8MB.
+			 * But with large pcluster it's possible to go over this
+			 * number, resulting in corrupted compressed indices.
+			 * To solve this, we use Z_EROFS_VLE_DI_D0_CBLKCNT-1 if
+			 * the uncompressed extent size goes above 8MB. This is
+			 * OK because if kernel sees another non-head cluster
+			 * after going back by |delta[0]| blocks, kernel will
+			 * just keep looking back.
+			 */

Would you mind updating this into the kernel comment style, I mean /*
* ...
*/
Instead?

lib/compress.c static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx, · line 116

Quoted 4 lines
lib/compress.c +4 -0
+			if (d0 & Z_EROFS_VLE_DI_D0_CBLKCNT) {
+				di.di_u.delta[0] = max(
+					d0 & (~Z_EROFS_VLE_DI_D0_CBLKCNT),
+					Z_EROFS_VLE_DI_D0_CBLKCNT-1);

May I ask if it's actually tested with big pcluster feature? It's lack of cpu_to_le16() convert and even the original Z_EROFS_VLE_DI_D0_CBLKCNT flag.

Thanks,
Gao Xiang

Quoted 8 lines
lib/compress.c +3 -0
+			} else {
+				di.di_u.delta[0] = cpu_to_le16(d0);
+			}
 			di.di_u.delta[1] = cpu_to_le16(d1);
 		} else {
 			type = raw ? Z_EROFS_VLE_CLUSTER_TYPE_PLAIN :
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#5 lore raw
Quoted 59 lines · Gao Xiang

Hi Kelvin,

Quoted 15 lines · Kelvin Zhang

Previously, uncompressed extent can be at most 8MB before mkfs.erofs crashes on some error condition. This is due to a minor bug in how compressed indices are encoded. This patch fixes the issue.

---
 include/erofs_fs.h |  2 +-
 lib/compress.c     | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)
include/erofs_fs.h +0 -0
index 9a91877..13eaf24 100644

include/erofs_fs.h enum { · line 356

Quoted 5 lines
include/erofs_fs.h +1 -1
@@ -353,7 +353,7 @@ enum {
  * compressed block count of a compressed extent (in logical clusters, aka.
  * block count of a pcluster).
  */
-#define Z_EROFS_VLE_DI_D0_CBLKCNT		(1 << 11)
+#define Z_EROFS_VLE_DI_D0_CBLKCNT		(1U << 11)

If erofs_fs.h update is necessary, I prefer to update in-kernel header first. Would you mind making a kernel patch for this if needed?

Quoted 15 lines
include/erofs_fs.h +0 -0
 struct z_erofs_vle_decompressed_index {
 	__le16 di_advise;
lib/compress.c +3 -1
index 98be7a2..23e571c 100644
@@ -97,7 +97,26 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
 		} else if (d0) {
 			type = Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD;
 
-			di.di_u.delta[0] = cpu_to_le16(d0);
+			/* If the |Z_EROFS_VLE_DI_D0_CBLKCNT| bit is set, parser
+			 * will interpret |delta[0]| as size of pcluster, rather
+			 * than distance to last head cluster. Normally this

lib/compress.c static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx, · line 112

Quoted 10 lines
lib/compress.c +10 -0
+			 * isn't a problem, because uncompressed extent size are
+			 * below Z_EROFS_VLE_DI_D0_CBLKCNT * BLOCK_SIZE = 8MB.
+			 * But with large pcluster it's possible to go over this
+			 * number, resulting in corrupted compressed indices.
+			 * To solve this, we use Z_EROFS_VLE_DI_D0_CBLKCNT-1 if
+			 * the uncompressed extent size goes above 8MB. This is
+			 * OK because if kernel sees another non-head cluster
+			 * after going back by |delta[0]| blocks, kernel will
+			 * just keep looking back.
+			 */

Would you mind updating this into the kernel comment style, I mean /*
* ...
*/
Instead?

Quoted 8 lines

lib/compress.c static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx, · line 116

Quoted 4 lines
lib/compress.c +4 -0
+			if (d0 & Z_EROFS_VLE_DI_D0_CBLKCNT) {
+				di.di_u.delta[0] = max(
+					d0 & (~Z_EROFS_VLE_DI_D0_CBLKCNT),
+					Z_EROFS_VLE_DI_D0_CBLKCNT-1);

May I ask if it's actually tested with big pcluster feature? It's lack of cpu_to_le16() convert and even the original Z_EROFS_VLE_DI_D0_CBLKCNT flag.

Sorry this part shouldn't have Z_EROFS_VLE_DI_D0_CBLKCNT flag.

Btw, I think a proper change for this might be just:

	if (d0 > Z_EROFS_VLE_DI_D0_CBLKCNT - 1)
		di.di_u.delta[0] = le16_to_cpu(Z_EROFS_VLE_DI_D0_CBLKCNT - 1);
	else
		di.di_u.delta[0] = cpu_to_le16(d0);

Or using max() to simplify above even more a bit.

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Gao Xiang

#6 lore raw

-1

Quoted 15 lines · Gao Xiang
Quoted 13 lines · Gao Xiang

Hi Kelvin,

Quoted 10 lines · Kelvin Zhang

Previously, uncompressed extent can be at most 8MB before mkfs.erofs crashes on some error condition. This is due to a minor bug in how compressed indices are encoded. This patch fixes the issue.

---
 include/erofs_fs.h |  2 +-
 lib/compress.c     | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
include/erofs_fs.h +0 -0
index 9a91877..13eaf24 100644
@@ -353,7 +353,7 @@ enum {
Quoted 8 lines

include/erofs_fs.h enum { · line 356

Quoted 5 lines
include/erofs_fs.h +1 -1
  * compressed block count of a compressed extent (in logical clusters, aka.
  * block count of a pcluster).
  */
-#define Z_EROFS_VLE_DI_D0_CBLKCNT          (1 << 11)
+#define Z_EROFS_VLE_DI_D0_CBLKCNT          (1U << 11)

If erofs_fs.h update is necessary, I prefer to update in-kernel header first. Would you mind making a kernel patch for this if needed?

Reverted erofs_fs.h change.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
include/erofs_fs.h +0 -0
 struct z_erofs_vle_decompressed_index {
    __le16 di_advise;
lib/compress.c +11 -1
index 98be7a2..23e571c 100644
@@ -97,7 +97,26 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
            } else if (d0) {
                    type = Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD;
 
-                   di.di_u.delta[0] = cpu_to_le16(d0);
+                   /* If the |Z_EROFS_VLE_DI_D0_CBLKCNT| bit is set, parser
+                    * will interpret |delta[0]| as size of pcluster, rather
+                    * than distance to last head cluster. Normally this
+                    * isn't a problem, because uncompressed extent size are
+                    * below Z_EROFS_VLE_DI_D0_CBLKCNT * BLOCK_SIZE = 8MB.
+                    * But with large pcluster it's possible to go over this
+                    * number, resulting in corrupted compressed indices.
+                    * To solve this, we use Z_EROFS_VLE_DI_D0_CBLKCNT-1 if
+                    * the uncompressed extent size goes above 8MB. This is
+                    * OK because if kernel sees another non-head cluster
+                    * after going back by |delta[0]| blocks, kernel will
Quoted 8 lines
Quoted 8 lines

lib/compress.c static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx, · line 112

Quoted 2 lines
lib/compress.c +2 -0
+                    * just keep looking back.
+                    */

Would you mind updating this into the kernel comment style, I mean /*
* ...
*/
Instead?

Done

Quoted 9 lines
Quoted 9 lines

lib/compress.c static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx, · line 116

Quoted 5 lines
lib/compress.c +4 -0
+                   if (d0 & Z_EROFS_VLE_DI_D0_CBLKCNT) {
+                           di.di_u.delta[0] = max(
+                                   d0 & (~Z_EROFS_VLE_DI_D0_CBLKCNT),
+                                   Z_EROFS_VLE_DI_D0_CBLKCNT-1);

May I ask if it's actually tested with big pcluster feature? It's lack of cpu_to_le16() convert and even the original Z_EROFS_VLE_DI_D0_CBLKCNT flag.

Sorry.. It was tested on a Little Endian machine, so I didn't discover the missing cpu_to_le16. Added now.

Quoted 9 lines

Sorry this part shouldn't have Z_EROFS_VLE_DI_D0_CBLKCNT flag.

Btw, I think a proper change for this might be just:

        if (d0 > Z_EROFS_VLE_DI_D0_CBLKCNT - 1)
                di.di_u.delta[0] = le16_to_cpu(Z_EROFS_VLE_DI_D0_CBLKCNT - 1);
        else
                di.di_u.delta[0] = cpu_to_le16(d0);

Or using max() to simplify above even more a bit.

This would work, but it's not optimal. For example, Z_EROFS_VLE_DI_D0_CBLKCNT << 1
is greater than Z_EROFS_VLE_DI_D0_CBLKCNT, but it does not have the 11th bit set.
Using Z_EROFS_VLE_DI_D0_CBLKCNT-1 in this case would cause the kernel to take more hops than necessary when finding the head cluster. A better change would be:

if (d0 > Z_EROFS_VLE_DI_D0_CBLKCNT - 1)
di.di_u.delta[0] = le16_to_cpu(largest number smaller
than d0 that does not have Z_EROFS_VLE_DI_D0_CBLKCNT bit set);

        else
                di.di_u.delta[0] = cpu_to_le16(d0);

But how do we find "largest number smaller than d0 that does not have Z_EROFS_VLE_DI_D0_CBLKCNT bit set" ?
Simple, clear the Z_EROFS_VLE_DI_D0_CBLKCNT bit, and set all bits before that to 1. In code:
d0 & (~ Z_EROFS_VLE_DI_D0_CBLKCNT) | (Z_EROFS_VLE_DI_D0_CBLKCNT-1)

So final answer:

        if (d0 > Z_EROFS_VLE_DI_D0_CBLKCNT - 1)
                di.di_u.delta[0] = le16_to_cpu(d0 & (~
Z_EROFS_VLE_DI_D0_CBLKCNT) | (Z_EROFS_VLE_DI_D0_CBLKCNT-1));
        else
                di.di_u.delta[0] = cpu_to_le16(d0);
Quoted 7 lines

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Gao Xiang

Signature · 3 lines
Sincerely,

Kelvin Zhang
#7 lore raw
Quoted 112 lines · Kelvin Zhang

-1

Quoted 15 lines · Gao Xiang
Quoted 13 lines · Gao Xiang

Hi Kelvin,

Quoted 10 lines · Kelvin Zhang

Previously, uncompressed extent can be at most 8MB before mkfs.erofs crashes on some error condition. This is due to a minor bug in how compressed indices are encoded. This patch fixes the issue.

---
 include/erofs_fs.h |  2 +-
 lib/compress.c     | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
include/erofs_fs.h +0 -0
index 9a91877..13eaf24 100644
@@ -353,7 +353,7 @@ enum {
Quoted 8 lines

include/erofs_fs.h enum { · line 356

Quoted 5 lines
include/erofs_fs.h +1 -1
  * compressed block count of a compressed extent (in logical clusters, aka.
  * block count of a pcluster).
  */
-#define Z_EROFS_VLE_DI_D0_CBLKCNT          (1 << 11)
+#define Z_EROFS_VLE_DI_D0_CBLKCNT          (1U << 11)

If erofs_fs.h update is necessary, I prefer to update in-kernel header first. Would you mind making a kernel patch for this if needed?

Reverted erofs_fs.h change.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
include/erofs_fs.h +0 -0
 struct z_erofs_vle_decompressed_index {
    __le16 di_advise;
lib/compress.c +11 -1
index 98be7a2..23e571c 100644
@@ -97,7 +97,26 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
            } else if (d0) {
                    type = Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD;
 
-                   di.di_u.delta[0] = cpu_to_le16(d0);
+                   /* If the |Z_EROFS_VLE_DI_D0_CBLKCNT| bit is set, parser
+                    * will interpret |delta[0]| as size of pcluster, rather
+                    * than distance to last head cluster. Normally this
+                    * isn't a problem, because uncompressed extent size are
+                    * below Z_EROFS_VLE_DI_D0_CBLKCNT * BLOCK_SIZE = 8MB.
+                    * But with large pcluster it's possible to go over this
+                    * number, resulting in corrupted compressed indices.
+                    * To solve this, we use Z_EROFS_VLE_DI_D0_CBLKCNT-1 if
+                    * the uncompressed extent size goes above 8MB. This is
+                    * OK because if kernel sees another non-head cluster
+                    * after going back by |delta[0]| blocks, kernel will
Quoted 8 lines
Quoted 8 lines

lib/compress.c static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx, · line 112

Quoted 2 lines
lib/compress.c +2 -0
+                    * just keep looking back.
+                    */

Would you mind updating this into the kernel comment style, I mean /*
* ...
*/
Instead?

Done

Quoted 9 lines
Quoted 9 lines

lib/compress.c static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx, · line 116

Quoted 5 lines
lib/compress.c +4 -0
+                   if (d0 & Z_EROFS_VLE_DI_D0_CBLKCNT) {
+                           di.di_u.delta[0] = max(
+                                   d0 & (~Z_EROFS_VLE_DI_D0_CBLKCNT),
+                                   Z_EROFS_VLE_DI_D0_CBLKCNT-1);

May I ask if it's actually tested with big pcluster feature? It's lack of cpu_to_le16() convert and even the original Z_EROFS_VLE_DI_D0_CBLKCNT flag.

Sorry.. It was tested on a Little Endian machine, so I didn't discover the missing cpu_to_le16. Added now.

Quoted 9 lines

Sorry this part shouldn't have Z_EROFS_VLE_DI_D0_CBLKCNT flag.

Btw, I think a proper change for this might be just:

        if (d0 > Z_EROFS_VLE_DI_D0_CBLKCNT - 1)
                di.di_u.delta[0] = le16_to_cpu(Z_EROFS_VLE_DI_D0_CBLKCNT - 1);
        else
                di.di_u.delta[0] = cpu_to_le16(d0);

Or using max() to simplify above even more a bit.

This would work, but it's not optimal. For example, Z_EROFS_VLE_DI_D0_CBLKCNT << 1
is greater than Z_EROFS_VLE_DI_D0_CBLKCNT, but it does not have the 11th bit set.
Using Z_EROFS_VLE_DI_D0_CBLKCNT-1 in this case would cause the kernel to take more hops than necessary when finding the head cluster. A better change would be:

if (d0 > Z_EROFS_VLE_DI_D0_CBLKCNT - 1)
di.di_u.delta[0] = le16_to_cpu(largest number smaller
than d0 that does not have Z_EROFS_VLE_DI_D0_CBLKCNT bit set);

        else
                di.di_u.delta[0] = cpu_to_le16(d0);

But how do we find "largest number smaller than d0 that does not have Z_EROFS_VLE_DI_D0_CBLKCNT bit set" ?
Simple, clear the Z_EROFS_VLE_DI_D0_CBLKCNT bit, and set all bits before that to 1. In code:
d0 & (~ Z_EROFS_VLE_DI_D0_CBLKCNT) | (Z_EROFS_VLE_DI_D0_CBLKCNT-1)

Quoted 8 lines

So final answer:

        if (d0 > Z_EROFS_VLE_DI_D0_CBLKCNT - 1)
                di.di_u.delta[0] = le16_to_cpu(d0 & (~
Z_EROFS_VLE_DI_D0_CBLKCNT) | (Z_EROFS_VLE_DI_D0_CBLKCNT-1));
        else
                di.di_u.delta[0] = cpu_to_le16(d0);

That may work for non-compact indexes, but it's somewhat unsafe for compact indexes (especially compact 2B), since the valid bits for each lcluster are 14 (12 plus 2-bit lcluster type):
for head lclusters, it stores lclusterofs;
for non-head lclusters, it stores delta0 (lookback distance) or
delta1 (lookforward distance)
conditionally.

That is also why Z_EROFS_VLE_DI_D0_CBLKCNT is set as (1 << 11). So in order to make them unique, I suggest just don't reuse higher bits even for non-compact indexes... (I think we could stand just looking back multiple times instead for such large logical extents...)

Actually "* eg. for 4k page-sized cluster, maximum 4K*64k = 256M)" is somewhat outdated now after compact-indexes was introduced. If you have time, could you submit a kernel patch to fix the description together?

Thanks,
Gao Xiang

#8 lore raw

PATCH v3 1/1 erofs-utils: lib: Fix 8MB bug on uncompressed extent size

Previously, uncompressed extent can be at most 8MB before mkfs.erofs crashes on some error condition. This is due to a minor bug in how compressed indices are encoded. This patch fixes the issue.

---
 lib/compress.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
lib/compress.c +17 -1
index 98be7a2..add95f5 100644
@@ -97,7 +97,23 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
 		} else if (d0) {
 			type = Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD;
 
-			di.di_u.delta[0] = cpu_to_le16(d0);
+			/*
+			 * If the |Z_EROFS_VLE_DI_D0_CBLKCNT| bit is set, parser
+			 * will interpret |delta[0]| as size of pcluster, rather
+			 * than distance to last head cluster. Normally this
+			 * isn't a problem, because uncompressed extent size are
+			 * below Z_EROFS_VLE_DI_D0_CBLKCNT * BLOCK_SIZE = 8MB.
+			 * But with large pcluster it's possible to go over this
+			 * number, resulting in corrupted compressed indices.
+			 * To solve this, we replace d0 with
+			 * Z_EROFS_VLE_DI_D0_CBLKCNT-1.
+			 */
+			if (d0 > Z_EROFS_VLE_DI_D0_CBLKCNT) {
+				di.di_u.delta[0] = cpu_to_le16(
+					Z_EROFS_VLE_DI_D0_CBLKCNT-1);
+			} else {
+				di.di_u.delta[0] = cpu_to_le16(d0);
+			}
 			di.di_u.delta[1] = cpu_to_le16(d1);
 		} else {
 			type = raw ? Z_EROFS_VLE_CLUSTER_TYPE_PLAIN :
Signature · 1 line
2.35.0.263.gb82422642f-goog
#9 lore raw
Quoted 20 lines · Kelvin Zhang

Previously, uncompressed extent can be at most 8MB before mkfs.erofs crashes on some error condition. This is due to a minor bug in how compressed indices are encoded. This patch fixes the issue.

---
 lib/compress.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
lib/compress.c +2 -1
index 98be7a2..add95f5 100644
@@ -97,7 +97,23 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
 		} else if (d0) {
 			type = Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD;
 
-			di.di_u.delta[0] = cpu_to_le16(d0);
+			/*
+			 * If the |Z_EROFS_VLE_DI_D0_CBLKCNT| bit is set, parser

lib/compress.c static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx, · line 111

Quoted 10 lines
lib/compress.c +10 -0
+			 * will interpret |delta[0]| as size of pcluster, rather
+			 * than distance to last head cluster. Normally this
+			 * isn't a problem, because uncompressed extent size are
+			 * below Z_EROFS_VLE_DI_D0_CBLKCNT * BLOCK_SIZE = 8MB.
+			 * But with large pcluster it's possible to go over this
+			 * number, resulting in corrupted compressed indices.
+			 * To solve this, we replace d0 with
+			 * Z_EROFS_VLE_DI_D0_CBLKCNT-1.
+			 */
+			if (d0 > Z_EROFS_VLE_DI_D0_CBLKCNT) {

Thanks for the new version.

I think this part would be "if (d0 >= Z_EROFS_VLE_DI_D0_CBLKCNT)", and already applied with minor styling changes:

https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git/commit/?id=789ac9b03c2c0d27c5be81cb8d026e2300ae822e

Thanks,
Gao Xiang

Quoted 10 lines
lib/compress.c +5 -0
+				di.di_u.delta[0] = cpu_to_le16(
+					Z_EROFS_VLE_DI_D0_CBLKCNT-1);
+			} else {
+				di.di_u.delta[0] = cpu_to_le16(d0);
+			}
 			di.di_u.delta[1] = cpu_to_le16(d1);
 		} else {
 			type = raw ? Z_EROFS_VLE_CLUSTER_TYPE_PLAIN :
Signature · 1 line
2.35.0.263.gb82422642f-goog
#10 lore raw

PATCH v2 1/1 erofs-utils: lib: Fix 8MB bug on uncompressed extent size

In reply to Gao Xiang

Previously, uncompressed extent can be at most 8MB before mkfs.erofs crashes on some error condition. This is due to a minor bug in how compressed indices are encoded. This patch fixes the issue.

---
 lib/compress.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
lib/compress.c +31 -1
index 98be7a2..2f7ffa7 100644
@@ -97,7 +97,37 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
 		} else if (d0) {
 			type = Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD;
 
-			di.di_u.delta[0] = cpu_to_le16(d0);
+			/*
+			 * If the |Z_EROFS_VLE_DI_D0_CBLKCNT| bit is set, parser
+			 * will interpret |delta[0]| as size of pcluster, rather
+			 * than distance to last head cluster. Normally this
+			 * isn't a problem, because uncompressed extent size are
+			 * below Z_EROFS_VLE_DI_D0_CBLKCNT * BLOCK_SIZE = 8MB.
+			 * But with large pcluster it's possible to go over this
+			 * number, resulting in corrupted compressed indices.
+			 * To solve this, we replace d0 with a number that's
+			 * smaller and doesn't have the
+			 * Z_EROFS_VLE_DI_D0_CBLKCNT bit set if
+			 * the uncompressed extent size goes above 8MB. This is
+			 * OK because if kernel sees another non-head cluster
+			 * after going back by |delta[0]| blocks, kernel will
+			 * just keep looking back.
+			 * The largest number smaller than d0 that doesn't have
+			 * Z_EROFS_VLE_DI_D0_CBLKCNT bit set is obtained by
+			 * first clearing Z_EROFS_VLE_DI_D0_CBLKCNT bit, then
+			 * set all bits before Z_EROFS_VLE_DI_D0_CBLKCNT to 1.
+			 * Using Z_EROFS_VLE_DI_D0_CBLKCNT-1 would work, but it
+			 * produces suboptimal indices in certain cases. e.g.
+			 * (Z_EROFS_VLE_DI_D0_CBLKCNT<<4)|
+			 * (Z_EROFS_VLE_DI_D0_CBLKCNT)
+			 */
+			if (d0 & Z_EROFS_VLE_DI_D0_CBLKCNT) {
+				di.di_u.delta[0] = cpu_to_le16(
+					(d0 & (~Z_EROFS_VLE_DI_D0_CBLKCNT)) |
+					(Z_EROFS_VLE_DI_D0_CBLKCNT-1));
+			} else {
+				di.di_u.delta[0] = cpu_to_le16(d0);
+			}
 			di.di_u.delta[1] = cpu_to_le16(d1);
 		} else {
 			type = raw ? Z_EROFS_VLE_CLUSTER_TYPE_PLAIN :
Signature · 1 line
2.35.0.263.gb82422642f-goog