[PATCH v2 1/4] erofs-utils: mkfs: validate chunk/pcluster sizes in the end

Gao Xiang hsiangkao at linux.alibaba.com
Tue Mar 14 17:21:18 AEDT 2023


Laterly, erofs-utils will support sub-page block sizes and
an arbitrary block size can be given at any position of the
command line.

Therefore, chunk/pcluster sizes needs to be validated in the end.

Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
v2: no change.
 mkfs/main.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)

diff --git a/mkfs/main.c b/mkfs/main.c
index 94f51df..8e5a421 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -126,6 +126,8 @@ static void usage(void)
 	print_available_compressors(stderr, ", ");
 }
 
+static unsigned int pclustersize_packed, pclustersize_max;
+
 static int parse_extended_opts(const char *opts)
 {
 #define MATCH_EXTENTED_OPT(opt, token, keylen) \
@@ -222,13 +224,12 @@ handle_fragment:
 			cfg.c_fragments = true;
 			if (vallen) {
 				i = strtoull(value, &endptr, 0);
-				if (endptr - value != vallen ||
-				    i < EROFS_BLKSIZ || i % EROFS_BLKSIZ) {
+				if (endptr - value != vallen) {
 					erofs_err("invalid pcluster size for the packed file %s",
 						  next);
 					return -EINVAL;
 				}
-				cfg.c_pclusterblks_packed = i / EROFS_BLKSIZ;
+				pclustersize_packed = i;
 			}
 		}
 
@@ -415,14 +416,12 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 #endif
 		case 'C':
 			i = strtoull(optarg, &endptr, 0);
-			if (*endptr != '\0' ||
-			    i < EROFS_BLKSIZ || i % EROFS_BLKSIZ) {
+			if (*endptr != '\0') {
 				erofs_err("invalid physical clustersize %s",
 					  optarg);
 				return -EINVAL;
 			}
-			cfg.c_pclusterblks_max = i / EROFS_BLKSIZ;
-			cfg.c_pclusterblks_def = cfg.c_pclusterblks_max;
+			pclustersize_max = i;
 			break;
 		case 11:
 			i = strtol(optarg, &endptr, 0);
@@ -436,11 +435,6 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 					  optarg);
 				return -EINVAL;
 			}
-			if (i < EROFS_BLKSIZ) {
-				erofs_err("chunksize %s must be larger than block size",
-					  optarg);
-				return -EINVAL;
-			}
 			erofs_sb_set_chunked_file();
 			break;
 		case 12:
@@ -521,6 +515,32 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 		cfg.c_dbg_lvl = EROFS_ERR;
 		cfg.c_showprogress = false;
 	}
+
+	if (pclustersize_max) {
+		if (pclustersize_max < EROFS_BLKSIZ ||
+		    pclustersize_max % EROFS_BLKSIZ) {
+			erofs_err("invalid physical clustersize %u",
+				  pclustersize_max);
+			return -EINVAL;
+		}
+		cfg.c_pclusterblks_max = pclustersize_max / EROFS_BLKSIZ;
+		cfg.c_pclusterblks_def = cfg.c_pclusterblks_max;
+	}
+	if (cfg.c_chunkbits && 1u << cfg.c_chunkbits < EROFS_BLKSIZ) {
+		erofs_err("chunksize %u must be larger than block size",
+			  1u << cfg.c_chunkbits);
+		return -EINVAL;
+	}
+
+	if (pclustersize_packed) {
+		if (pclustersize_max < EROFS_BLKSIZ ||
+		    pclustersize_max % EROFS_BLKSIZ) {
+			erofs_err("invalid pcluster size for the packed file %u",
+				  pclustersize_packed);
+			return -EINVAL;
+		}
+		cfg.c_pclusterblks_packed = pclustersize_packed / EROFS_BLKSIZ;
+	}
 	return 0;
 }
 
-- 
2.24.4



More information about the Linux-erofs mailing list