Skip to contentSkip to conversation

erofs-utils: lib: wrap up zeropadding calculation

PATCH New Gao Xiang 1 participant 2 messages Jun 4, 2024 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/2 erofs-utils: lib: wrap up zeropadding calculation

Use a simple helper instead of open-coding.

---
 lib/decompress.c | 46 ++++++++++++++++++----------------------------
 1 file changed, 18 insertions(+), 28 deletions(-)
lib/decompress.c +18 -28
index 58ce7e5..e65b924 100644
@@ -9,6 +9,15 @@
 #include "erofs/err.h"
 #include "erofs/print.h"
 
+static unsigned int z_erofs_fixup_insize(const u8 *padbuf, unsigned int padbufsize)
+{
+	unsigned int inputmargin;
+
+	for (inputmargin = 0; inputmargin < padbufsize &&
+	     !padbuf[inputmargin]; ++inputmargin);
+	return inputmargin;
+}
+
 #ifdef HAVE_LIBZSTD
 #include <zstd.h>
 #include <zstd_errors.h>
@@ -16,7 +25,6 @@
 /* also a very preliminary userspace version */
 static int z_erofs_decompress_zstd(struct z_erofs_decompress_req *rq)
 {
-	struct erofs_sb_info *sbi = rq->sbi;
 	int ret = 0;
 	char *dest = rq->out;
 	char *src = rq->in;
@@ -24,10 +32,7 @@ static int z_erofs_decompress_zstd(struct z_erofs_decompress_req *rq)
 	unsigned int inputmargin = 0;
 	unsigned long long total;
 
-	while (!src[inputmargin & (erofs_blksiz(sbi) - 1)])
-		if (!(++inputmargin & (erofs_blksiz(sbi) - 1)))
-			break;
-
+	inputmargin = z_erofs_fixup_insize((u8 *)src, rq->inputsize);
 	if (inputmargin >= rq->inputsize)
 		return -EFSCORRUPTED;
 
@@ -78,19 +83,15 @@ out:
 
 static int z_erofs_decompress_deflate(struct z_erofs_decompress_req *rq)
 {
-	struct erofs_sb_info *sbi = rq->sbi;
 	u8 *dest = (u8 *)rq->out;
 	u8 *src = (u8 *)rq->in;
 	u8 *buff = NULL;
 	size_t actual_out;
-	unsigned int inputmargin = 0;
+	unsigned int inputmargin;
 	struct libdeflate_decompressor *inf;
 	enum libdeflate_result ret;
 
-	while (!src[inputmargin & (erofs_blksiz(sbi) - 1)])
-		if (!(++inputmargin & (erofs_blksiz(sbi) - 1)))
-			break;
-
+	inputmargin = z_erofs_fixup_insize(src, rq->inputsize);
 	if (inputmargin >= rq->inputsize)
 		return -EFSCORRUPTED;
 
@@ -160,18 +161,14 @@ static int zerr(int ret)
 
 static int z_erofs_decompress_deflate(struct z_erofs_decompress_req *rq)
 {
-	struct erofs_sb_info *sbi = rq->sbi;
 	u8 *dest = (u8 *)rq->out;
 	u8 *src = (u8 *)rq->in;
 	u8 *buff = NULL;
-	unsigned int inputmargin = 0;
+	unsigned int inputmargin;
 	z_stream strm;
 	int ret;
 
-	while (!src[inputmargin & (erofs_blksiz(sbi) - 1)])
-		if (!(++inputmargin & (erofs_blksiz(sbi) - 1)))
-			break;
-
+	inputmargin = z_erofs_fixup_insize(src, rq->inputsize);
 	if (inputmargin >= rq->inputsize)
 		return -EFSCORRUPTED;
 
@@ -225,18 +222,14 @@ out_inflate_end:
 static int z_erofs_decompress_lzma(struct z_erofs_decompress_req *rq)
 {
 	int ret = 0;
-	struct erofs_sb_info *sbi = rq->sbi;
 	u8 *dest = (u8 *)rq->out;
 	u8 *src = (u8 *)rq->in;
 	u8 *buff = NULL;
-	unsigned int inputmargin = 0;
+	unsigned int inputmargin;
 	lzma_stream strm;
 	lzma_ret ret2;
 
-	while (!src[inputmargin & (erofs_blksiz(sbi) - 1)])
-		if (!(++inputmargin & (erofs_blksiz(sbi) - 1)))
-			break;
-
+	inputmargin = z_erofs_fixup_insize(src, rq->inputsize);
 	if (inputmargin >= rq->inputsize)
 		return -EFSCORRUPTED;
 
@@ -297,12 +290,9 @@ static int z_erofs_decompress_lz4(struct z_erofs_decompress_req *rq)
 	if (erofs_sb_has_lz4_0padding(sbi)) {
 		support_0padding = true;
 
-		while (!src[inputmargin & (erofs_blksiz(sbi) - 1)])
-			if (!(++inputmargin & (erofs_blksiz(sbi) - 1)))
-				break;
-
+		inputmargin = z_erofs_fixup_insize((u8 *)src, rq->inputsize);
 		if (inputmargin >= rq->inputsize)
-			return -EIO;
+			return -EFSCORRUPTED;
 	}
 
 	if (rq->decodedskip) {
Signature · 1 line
2.39.3
author
#1 lore raw

PATCH 2/2 erofs-utils: record sb_size instead of sb_extslots

Just follow the kernel implementation.

---
 dump/main.c              | 4 ++--
 include/erofs/internal.h | 7 ++++---
 lib/super.c              | 7 ++++++-
 3 files changed, 12 insertions(+), 6 deletions(-)
dump/main.c +2 -2
index dd2c620..50f4662 100644
@@ -654,8 +654,8 @@ static void erofsdump_show_superblock(void)
 		fprintf(stdout, "Filesystem lz4_max_distance:                  %u\n",
 			sbi.lz4_max_distance | 0U);
 	}
-	fprintf(stdout, "Filesystem sb_extslots:                       %u\n",
-			sbi.extslots | 0U);
+	fprintf(stdout, "Filesystem sb_size:                           %u\n",
+			sbi.sb_size | 0U);
 	fprintf(stdout, "Filesystem inode count:                       %llu\n",
 			sbi.inos | 0ULL);
 	fprintf(stdout, "Filesystem created:                           %s",
include/erofs/internal.h +4 -3
index 46345e0..9fdff71 100644
@@ -84,13 +84,14 @@ struct erofs_sb_info {
 
 	u32 feature_compat;
 	u32 feature_incompat;
-	u64 build_time;
-	u32 build_time_nsec;
 
-	u8  extslots;
 	unsigned char islotbits;
 	unsigned char blkszbits;
 
+	u32 sb_size;			/* total superblock size */
+	u32 build_time_nsec;
+	u64 build_time;
+
 	/* what we really care is nid, rather than ino.. */
 	erofs_nid_t root_nid;
 	/* used for statfs, f_files - f_favail */
lib/super.c +6 -1
index f952f7e..4d16d29 100644
@@ -104,6 +104,12 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 		return ret;
 	}
 
+	sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
+	if (sbi->sb_size > (1 << sbi->blkszbits) - EROFS_SUPER_OFFSET) {
+		erofs_err("invalid sb_extslots %u (more than a fs block)",
+			  dsb->sb_extslots);
+		return -EINVAL;
+	}
 	sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
 	sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
 	sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
@@ -114,7 +120,6 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 	sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
 	sbi->inos = le64_to_cpu(dsb->inos);
 	sbi->checksum = le32_to_cpu(dsb->checksum);
-	sbi->extslots = dsb->sb_extslots;
 
 	sbi->build_time = le64_to_cpu(dsb->build_time);
 	sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
Signature · 1 line
2.39.3