Skip to contentSkip to conversation

erofs-utils: lib: fix incorrect mtime under -Edot-omitted

PATCH Superseded Yifan Zhao 2 participants 9 messages Jan 16 – Jan 19 Library code

lore
Share & tools
lore

Yifan Zhao

author
#0 lore raw

PATCH 1/1 erofs-utils: lib: fix incorrect mtime under -Edot-omitted

`-Edot-omitted`` enables `-E48bits``, which requires specific configurations for g_sbi.{epoch, build_time}. Currently, the call to erofs_sb_set_48bit() occurs too late in the execution flow, causing the aforementioned logic to be bypassed and resulting in incorrect mtimes for all inodes.

This patch moves `erofs_sb_set_48bit()`` forward to the options parsing stage to resolve this issue.

---
 lib/importer.c | 2 --
 mkfs/main.c    | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
lib/importer.c +0 -2
index 958a433..cd71a21 100644
@@ -81,8 +81,6 @@ int erofs_importer_init(struct erofs_importer *im)
 			goto out_err;
 	}
 
-	if (params->dot_omitted)
-		erofs_sb_set_48bit(sbi);
 	return 0;
 
 out_err:
mkfs/main.c +2 -0
index 620b1ed..347cdcb 100644
@@ -428,6 +428,8 @@ static int erofs_mkfs_feat_set_dot_omitted(struct erofs_importer_params *params,
 		return -EINVAL;
 
 	mkfs_dot_omitted = en;
+	if (en)
+		erofs_sb_set_48bit(&g_sbi);
 	return 0;
 }
 
Signature · 1 line
2.47.3
author
#1 lore raw

PATCH v2 1/1 erofs-utils: lib: fix incorrect mtime under -Edot-omitted

-Edot-omitted enables -E48bits, which requires specific configurations for g_sbi.{epoch, build_time}. Currently, the call to erofs_sb_set_48bit() occurs too late in the execution flow, causing the aforementioned logic to be bypassed and resulting in incorrect mtimes for all inodes.

This patch moves erofs_sb_set_48bit() forward to the options parsing stage to resolve this issue.

---
v2:
- Fix mis-formatted commit msg, oops..

 lib/importer.c | 2 --
 mkfs/main.c    | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
lib/importer.c +0 -2
index 958a433..cd71a21 100644
@@ -81,8 +81,6 @@ int erofs_importer_init(struct erofs_importer *im)
 			goto out_err;
 	}
 
-	if (params->dot_omitted)
-		erofs_sb_set_48bit(sbi);
 	return 0;
 
 out_err:
mkfs/main.c +2 -0
index 620b1ed..347cdcb 100644
@@ -428,6 +428,8 @@ static int erofs_mkfs_feat_set_dot_omitted(struct erofs_importer_params *params,
 		return -EINVAL;
 
 	mkfs_dot_omitted = en;
+	if (en)
+		erofs_sb_set_48bit(&g_sbi);
 	return 0;
 }
 
Signature · 1 line
2.47.3
#2 lore raw
Quoted 10 lines · Yifan Zhao

-Edot-omitted enables -E48bits, which requires specific configurations for g_sbi.{epoch, build_time}. Currently, the call to erofs_sb_set_48bit() occurs too late in the execution flow, causing the aforementioned logic to be bypassed and resulting in incorrect mtimes for all inodes.

This patch moves erofs_sb_set_48bit() forward to the options parsing stage to resolve this issue.

I think instead we should move this part into lib/importer.c?

        g_sbi.fixed_nsec = 0;
        if (cfg.c_unix_timestamp != -1) {
                mkfs_time = cfg.c_unix_timestamp;
        } else if (!gettimeofday(&t, NULL)) {
                mkfs_time = t.tv_sec;
        }
        if (erofs_sb_has_48bit(&g_sbi)) {
                g_sbi.epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
                g_sbi.build_time = mkfs_time - g_sbi.epoch;
        } else {
                g_sbi.epoch = mkfs_time;
        }

I mean adding mkfs_time in erofs_importer_params.

Thanks,
Gao Xiang

author
#3 lore raw

PATCH v3 1/1 erofs-utils: lib: fix incorrect mtime under -Edot-omitted

-Edot-omitted enables -E48bits, which requires specific configurations for g_sbi.{epoch, build_time}. Currently, the call to erofs_sb_set_48bit() occurs too late in the execution flow, causing the aforementioned logic to be bypassed and resulting in incorrect mtimes for all inodes.

This patch moves time initialization logic into erofs_importer_init() to resolve this issue.

---
 lib/importer.c | 17 +++++++++++++++++
 mkfs/main.c    | 15 ---------------
 2 files changed, 17 insertions(+), 15 deletions(-)
lib/importer.c +17 -0
index 958a433..b62ec20 100644
@@ -2,6 +2,7 @@
 /*
  * Copyright (C) 2025 Alibaba Cloud
  */
+#include <sys/time.h>
 #include "erofs/importer.h"
 #include "erofs/config.h"
 #include "erofs/dedupe.h"
@@ -43,6 +44,8 @@ int erofs_importer_init(struct erofs_importer *im)
 	struct erofs_sb_info *sbi = im->sbi;
 	struct erofs_importer_params *params = im->params;
 	const char *subsys = NULL;
+	struct timeval t;
+	s64 mkfs_time = 0;
 	int err;
 
 	erofs_importer_global_init();
@@ -83,6 +86,20 @@ int erofs_importer_init(struct erofs_importer *im)
 
 	if (params->dot_omitted)
 		erofs_sb_set_48bit(sbi);
+
+	sbi->fixed_nsec = 0;
+	if (cfg.c_unix_timestamp != -1) {
+		mkfs_time = cfg.c_unix_timestamp;
+	} else if (!gettimeofday(&t, NULL)) {
+		mkfs_time = t.tv_sec;
+	}
+	if (erofs_sb_has_48bit(sbi)) {
+		sbi->epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
+		sbi->build_time = mkfs_time - sbi->epoch;
+	} else {
+		sbi->epoch = mkfs_time;
+	}
+
 	return 0;
 
 out_err:
mkfs/main.c +0 -15
index 620b1ed..6ee7f54 100644
@@ -1710,9 +1710,7 @@ int main(int argc, char **argv)
 	};
 	struct erofs_inode *root = NULL;
 	bool tar_index_512b = false;
-	struct timeval t;
 	FILE *blklst = NULL;
-	s64 mkfs_time = 0;
 	int err;
 	u32 crc;
 
@@ -1736,19 +1734,6 @@ int main(int argc, char **argv)
 		goto exit;
 	}
 
-	g_sbi.fixed_nsec = 0;
-	if (cfg.c_unix_timestamp != -1) {
-		mkfs_time = cfg.c_unix_timestamp;
-	} else if (!gettimeofday(&t, NULL)) {
-		mkfs_time = t.tv_sec;
-	}
-	if (erofs_sb_has_48bit(&g_sbi)) {
-		g_sbi.epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
-		g_sbi.build_time = mkfs_time - g_sbi.epoch;
-	} else {
-		g_sbi.epoch = mkfs_time;
-	}
-
 	err = erofs_dev_open(&g_sbi, cfg.c_img_path, O_RDWR |
 				(incremental_mode ? 0 : O_TRUNC));
 	if (err) {
Signature · 1 line
2.47.3
#4 lore raw
Quoted 37 lines · Yifan Zhao

-Edot-omitted enables -E48bits, which requires specific configurations for g_sbi.{epoch, build_time}. Currently, the call to erofs_sb_set_48bit() occurs too late in the execution flow, causing the aforementioned logic to be bypassed and resulting in incorrect mtimes for all inodes.

This patch moves time initialization logic into erofs_importer_init() to resolve this issue.

---
  lib/importer.c | 17 +++++++++++++++++
  mkfs/main.c    | 15 ---------------
  2 files changed, 17 insertions(+), 15 deletions(-)
lib/importer.c +3 -0
index 958a433..b62ec20 100644
@@ -2,6 +2,7 @@
  /*
   * Copyright (C) 2025 Alibaba Cloud
   */
+#include <sys/time.h>
  #include "erofs/importer.h"
  #include "erofs/config.h"
  #include "erofs/dedupe.h"
@@ -43,6 +44,8 @@ int erofs_importer_init(struct erofs_importer *im)
  	struct erofs_sb_info *sbi = im->sbi;
  	struct erofs_importer_params *params = im->params;
  	const char *subsys = NULL;
+	struct timeval t;
+	s64 mkfs_time = 0;
  	int err;
 
  	erofs_importer_global_init();

lib/importer.c int erofs_importer_init(struct erofs_importer *im) · line 92

Quoted 7 lines
lib/importer.c +4 -0
@@ -83,6 +86,20 @@ int erofs_importer_init(struct erofs_importer *im)
 
  	if (params->dot_omitted)
  		erofs_sb_set_48bit(sbi);
+
+	sbi->fixed_nsec = 0;
+	if (cfg.c_unix_timestamp != -1) {
+		mkfs_time = cfg.c_unix_timestamp;

I mean we could

	if (params->mkfs_time != -1) {
		mkfs_time = params->mkfs_time;
		if (erofs_sb_has_48bit(sbi)) {
			sbi->epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
			sbi->build_time = mkfs_time - sbi->epoch;
		} else {
			sbi->epoch = mkfs_time;
		}

	}
Quoted 33 lines
lib/importer.c +10 -0
+	} else if (!gettimeofday(&t, NULL)) {
+		mkfs_time = t.tv_sec;
+	}
+	if (erofs_sb_has_48bit(sbi)) {
+		sbi->epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
+		sbi->build_time = mkfs_time - sbi->epoch;
+	} else {
+		sbi->epoch = mkfs_time;
+	}
+
  	return 0;
 
  out_err:
mkfs/main.c +0 -4
index 620b1ed..6ee7f54 100644
@@ -1710,9 +1710,7 @@ int main(int argc, char **argv)
  	};
  	struct erofs_inode *root = NULL;
  	bool tar_index_512b = false;
-	struct timeval t;
  	FILE *blklst = NULL;
-	s64 mkfs_time = 0;
  	int err;
  	u32 crc;
 
@@ -1736,19 +1734,6 @@ int main(int argc, char **argv)
  		goto exit;
  	}
 
-	g_sbi.fixed_nsec = 0;
-	if (cfg.c_unix_timestamp != -1) {

mkfs/main.c int main(int argc, char **argv) · line 1750

Quoted 10 lines
mkfs/main.c +0 -10
-		mkfs_time = cfg.c_unix_timestamp;
-	} else if (!gettimeofday(&t, NULL)) {
-		mkfs_time = t.tv_sec;
-	}
-	if (erofs_sb_has_48bit(&g_sbi)) {
-		g_sbi.epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
-		g_sbi.build_time = mkfs_time - g_sbi.epoch;
-	} else {
-		g_sbi.epoch = mkfs_time;
-	}

And here

	if (cfg.c_unix_timestamp != -1) {
		params->mkfs_time = cfg.c_unix_timestamp;
	else
		params->mkfs_time = !gettimeofday(&t, NULL) ...

	}

Thanks,
Gao Xiang

Quoted 4 lines
mkfs/main.c +0 -1
-
  	err = erofs_dev_open(&g_sbi, cfg.c_img_path, O_RDWR |
  				(incremental_mode ? 0 : O_TRUNC));
  	if (err) {
author
#5 lore raw

PATCH v4 1/1 erofs-utils: lib: fix incorrect mtime under -Edot-omitted

-Edot-omitted enables -E48bits, which requires specific configurations for g_sbi.{epoch, build_time}. Currently, the call to erofs_sb_set_48bit() occurs too late in the execution flow, causing the aforementioned logic to be bypassed and resulting in incorrect mtimes for all inodes.

This patch moves time initialization logic into erofs_importer_init() to resolve this issue.

---
 include/erofs/importer.h |  1 +
 lib/importer.c           | 10 ++++++++++
 mkfs/main.c              | 16 ++++------------
 3 files changed, 15 insertions(+), 12 deletions(-)
include/erofs/importer.h +1 -0
index 60160d6..2dea963 100644
@@ -41,6 +41,7 @@ struct erofs_importer_params {
 	u32 pclusterblks_def;
 	u32 pclusterblks_packed;
 	s32 pclusterblks_metabox;
+	s64 mkfs_time;
 	char force_inodeversion;
 	bool ignore_mtime;
 	bool no_datainline;
lib/importer.c +10 -0
index 958a433..45c9963 100644
@@ -83,6 +83,16 @@ int erofs_importer_init(struct erofs_importer *im)
 
 	if (params->dot_omitted)
 		erofs_sb_set_48bit(sbi);
+
+	if (params->mkfs_time != -1) {
+		if (erofs_sb_has_48bit(sbi)) {
+			sbi->epoch = max_t(s64, 0, params->mkfs_time - UINT32_MAX);
+			sbi->build_time = params->mkfs_time - sbi->epoch;
+		} else {
+			sbi->epoch = params->mkfs_time;
+		}
+	}
+
 	return 0;
 
 out_err:
mkfs/main.c +4 -12
index bc001a6..34d03ca 100644
@@ -1731,7 +1731,6 @@ int main(int argc, char **argv)
 	bool tar_index_512b = false;
 	struct timeval t;
 	FILE *blklst = NULL;
-	s64 mkfs_time = 0;
 	int err;
 	u32 crc;
 
@@ -1756,17 +1755,10 @@ int main(int argc, char **argv)
 	}
 
 	g_sbi.fixed_nsec = 0;
-	if (cfg.c_unix_timestamp != -1) {
-		mkfs_time = cfg.c_unix_timestamp;
-	} else if (!gettimeofday(&t, NULL)) {
-		mkfs_time = t.tv_sec;
-	}
-	if (erofs_sb_has_48bit(&g_sbi)) {
-		g_sbi.epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
-		g_sbi.build_time = mkfs_time - g_sbi.epoch;
-	} else {
-		g_sbi.epoch = mkfs_time;
-	}
+	if (cfg.c_unix_timestamp != -1)
+		importer_params.mkfs_time = cfg.c_unix_timestamp;
+	else
+		importer_params.mkfs_time = !gettimeofday(&t, NULL) ? t.tv_sec : -1;
 
 	err = erofs_dev_open(&g_sbi, cfg.c_img_path, O_RDWR |
 				(incremental_mode ? 0 : O_TRUNC));
Signature · 1 line
2.47.3
#6 lore raw

Hi Yifan,

Quoted 10 lines · Yifan Zhao

-Edot-omitted enables -E48bits, which requires specific configurations for g_sbi.{epoch, build_time}. Currently, the call to erofs_sb_set_48bit() occurs too late in the execution flow, causing the aforementioned logic to be bypassed and resulting in incorrect mtimes for all inodes.

This patch moves time initialization logic into erofs_importer_init() to resolve this issue.

Can you confirm if the following patch looks good to you? I tried to move c_unix_timestamp into mkfscfg.

From 11e564767f62d494a7beb100c709655c60eb194a Mon Sep 17 00:00:00 2001
From: Yifan Zhao <[email protected]>
Date: Sat, 17 Jan 2026 10:43:56 +0800
Subject: [PATCH v5] erofs-utils: lib: fix incorrect mtime under -Edot-omitted

-Edot-omitted enables -E48bits, which requires specific configurations for g_sbi.{epoch, build_time}. Currently, the call to erofs_sb_set_48bit() occurs too late in the execution flow, causing the aforementioned logic to be bypassed and resulting in incorrect mtimes for all inodes.

This patch moves time initialization logic into erofs_importer_init() to resolve this issue.

---
 include/erofs/config.h   |  1 -
 include/erofs/importer.h |  1 +
 lib/config.c             |  1 -
 lib/importer.c           | 11 +++++++++++
 mkfs/main.c              | 26 +++++++++++---------------
 5 files changed, 23 insertions(+), 17 deletions(-)
include/erofs/config.h +0 -1
index 525a8cd5ebfb..2a84fb515868 100644
@@ -58,7 +58,6 @@ struct erofs_configure {
 	char c_force_chunkformat;
 	u8 c_mkfs_metabox_algid;
 	u32 c_max_decompressed_extent_bytes;
-	u64 c_unix_timestamp;
 	const char *mount_point;
 	u32 c_root_xattr_isize;
 #ifdef EROFS_MT_ENABLED
include/erofs/importer.h +1 -0
index 60160d6bea05..adeea7230447 100644
@@ -41,6 +41,7 @@ struct erofs_importer_params {
 	u32 pclusterblks_def;
 	u32 pclusterblks_packed;
 	s32 pclusterblks_metabox;
+	s64 build_time;
 	char force_inodeversion;
 	bool ignore_mtime;
 	bool no_datainline;
lib/config.c +0 -1
index 16b34fa840d3..5eb0ddeaa851 100644
@@ -29,7 +29,6 @@ void erofs_init_configure(void)
 	cfg.c_dbg_lvl  = EROFS_WARN;
 	cfg.c_version  = PACKAGE_VERSION;
 	cfg.c_dry_run  = false;
-	cfg.c_unix_timestamp = -1;
 	cfg.c_max_decompressed_extent_bytes = -1;
 	erofs_stdout_tty = isatty(STDOUT_FILENO);
 }
lib/importer.c +11 -0
index 958a433b9eaa..d686c519676b 100644
@@ -23,6 +23,7 @@ void erofs_importer_preset(struct erofs_importer_params *params)
 		.fixed_uid = -1,
 		.fixed_gid = -1,
 		.fsalignblks = 1,
+		.build_time = -1,
 	};
 }
 
@@ -83,6 +84,16 @@ int erofs_importer_init(struct erofs_importer *im)
 
 	if (params->dot_omitted)
 		erofs_sb_set_48bit(sbi);
+
+	if (params->build_time != -1) {
+		if (erofs_sb_has_48bit(sbi)) {
+			sbi->epoch = max_t(s64, 0, params->build_time - UINT32_MAX);
+			sbi->build_time = params->build_time - sbi->epoch;
+		} else {
+			sbi->epoch = params->build_time;
+		}
+	}
+
 	return 0;
 
 out_err:
mkfs/main.c +11 -15
index bc001a600e7f..1ad610c6b066 100644
@@ -277,8 +277,10 @@ static struct erofsmkfs_cfg {
 	/* < 0, xattr disabled and >= INT_MAX, always use inline xattrs */
 	long inlinexattr_tolerance;
 	bool inode_metazone;
+	u64 unix_timestamp;
 } mkfscfg = {
 	.inlinexattr_tolerance = 2,
+	.unix_timestamp = -1,
 };
 
 static unsigned int pclustersize_packed, pclustersize_max;
@@ -1099,8 +1101,8 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 			break;
 
 		case 'T':
-			cfg.c_unix_timestamp = strtoull(optarg, &endptr, 0);
-			if (cfg.c_unix_timestamp == -1 || *endptr != '\0') {
+			mkfscfg.unix_timestamp = strtoull(optarg, &endptr, 0);
+			if (mkfscfg.unix_timestamp == -1 || *endptr != '\0') {
 				erofs_err("invalid UNIX timestamp %s", optarg);
 				return -EINVAL;
 			}
@@ -1605,7 +1607,7 @@ int parse_source_date_epoch(void)
 			  source_date_epoch);
 		return -EINVAL;
 	}
-	cfg.c_unix_timestamp = epoch;
+	mkfscfg.unix_timestamp = epoch;
 	cfg.c_timeinherit = TIMESTAMP_CLAMPING;
 	return 0;
 }
@@ -1731,7 +1733,6 @@ int main(int argc, char **argv)
 	bool tar_index_512b = false;
 	struct timeval t;
 	FILE *blklst = NULL;
-	s64 mkfs_time = 0;
 	int err;
 	u32 crc;
 
@@ -1756,17 +1757,12 @@ int main(int argc, char **argv)
 	}
 
 	g_sbi.fixed_nsec = 0;
-	if (cfg.c_unix_timestamp != -1) {
-		mkfs_time = cfg.c_unix_timestamp;
-	} else if (!gettimeofday(&t, NULL)) {
-		mkfs_time = t.tv_sec;
-	}
-	if (erofs_sb_has_48bit(&g_sbi)) {
-		g_sbi.epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
-		g_sbi.build_time = mkfs_time - g_sbi.epoch;
-	} else {
-		g_sbi.epoch = mkfs_time;
-	}
+	if (mkfscfg.unix_timestamp != -1)
+		importer_params.build_time = mkfscfg.unix_timestamp;
+	else if (!gettimeofday(&t, NULL))
+		importer_params.build_time = t.tv_sec;
+	else
+		importer_params.build_time = 0;
 
 	err = erofs_dev_open(&g_sbi, cfg.c_img_path, O_RDWR |
 				(incremental_mode ? 0 : O_TRUNC));

--
2.43.5

#7 lore raw

PATCH v5 1/1 erofs-utils: lib: fix incorrect mtime under -Edot-omitted

From: Yifan Zhao <[email protected]>

-Edot-omitted enables -E48bits, which requires specific configurations for g_sbi.{epoch, build_time}. Currently, the call to erofs_sb_set_48bit() occurs too late in the execution flow, causing the aforementioned logic to be bypassed and resulting in incorrect mtimes for all inodes.

This patch moves time initialization logic into erofs_importer_init() to resolve this issue.

---
it seems the previous reply version was broken due to invalid formats.

 include/erofs/config.h   |  1 -
 include/erofs/importer.h |  1 +
 lib/config.c             |  1 -
 lib/importer.c           | 11 +++++++++++
 mkfs/main.c              | 26 +++++++++++---------------
 5 files changed, 23 insertions(+), 17 deletions(-)
include/erofs/config.h +0 -1
index 525a8cd5ebfb..2a84fb515868 100644
@@ -58,7 +58,6 @@ struct erofs_configure {
 	char c_force_chunkformat;
 	u8 c_mkfs_metabox_algid;
 	u32 c_max_decompressed_extent_bytes;
-	u64 c_unix_timestamp;
 	const char *mount_point;
 	u32 c_root_xattr_isize;
 #ifdef EROFS_MT_ENABLED
include/erofs/importer.h +1 -0
index a525b474f1d5..15e247e51b9c 100644
@@ -41,6 +41,7 @@ struct erofs_importer_params {
 	u32 pclusterblks_def;
 	u32 pclusterblks_packed;
 	s32 pclusterblks_metabox;
+	s64 build_time;
 	char force_inodeversion;
 	bool ignore_mtime;
 	bool no_datainline;
lib/config.c +0 -1
index 16b34fa840d3..5eb0ddeaa851 100644
@@ -29,7 +29,6 @@ void erofs_init_configure(void)
 	cfg.c_dbg_lvl  = EROFS_WARN;
 	cfg.c_version  = PACKAGE_VERSION;
 	cfg.c_dry_run  = false;
-	cfg.c_unix_timestamp = -1;
 	cfg.c_max_decompressed_extent_bytes = -1;
 	erofs_stdout_tty = isatty(STDOUT_FILENO);
 }
lib/importer.c +11 -0
index 958a433b9eaa..d686c519676b 100644
@@ -23,6 +23,7 @@ void erofs_importer_preset(struct erofs_importer_params *params)
 		.fixed_uid = -1,
 		.fixed_gid = -1,
 		.fsalignblks = 1,
+		.build_time = -1,
 	};
 }
 
@@ -83,6 +84,16 @@ int erofs_importer_init(struct erofs_importer *im)
 
 	if (params->dot_omitted)
 		erofs_sb_set_48bit(sbi);
+
+	if (params->build_time != -1) {
+		if (erofs_sb_has_48bit(sbi)) {
+			sbi->epoch = max_t(s64, 0, params->build_time - UINT32_MAX);
+			sbi->build_time = params->build_time - sbi->epoch;
+		} else {
+			sbi->epoch = params->build_time;
+		}
+	}
+
 	return 0;
 
 out_err:
mkfs/main.c +11 -15
index b45368f301f3..683c650db519 100644
@@ -274,8 +274,10 @@ static struct erofsmkfs_cfg {
 	/* < 0, xattr disabled and >= INT_MAX, always use inline xattrs */
 	long inlinexattr_tolerance;
 	bool inode_metazone;
+	u64 unix_timestamp;
 } mkfscfg = {
 	.inlinexattr_tolerance = 2,
+	.unix_timestamp = -1,
 };
 
 static unsigned int pclustersize_packed, pclustersize_max;
@@ -1096,8 +1098,8 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 			break;
 
 		case 'T':
-			cfg.c_unix_timestamp = strtoull(optarg, &endptr, 0);
-			if (cfg.c_unix_timestamp == -1 || *endptr != '\0') {
+			mkfscfg.unix_timestamp = strtoull(optarg, &endptr, 0);
+			if (mkfscfg.unix_timestamp == -1 || *endptr != '\0') {
 				erofs_err("invalid UNIX timestamp %s", optarg);
 				return -EINVAL;
 			}
@@ -1576,7 +1578,7 @@ int parse_source_date_epoch(void)
 			  source_date_epoch);
 		return -EINVAL;
 	}
-	cfg.c_unix_timestamp = epoch;
+	mkfscfg.unix_timestamp = epoch;
 	cfg.c_timeinherit = TIMESTAMP_CLAMPING;
 	return 0;
 }
@@ -1702,7 +1704,6 @@ int main(int argc, char **argv)
 	bool tar_index_512b = false;
 	struct timeval t;
 	FILE *blklst = NULL;
-	s64 mkfs_time = 0;
 	int err;
 	u32 crc;
 
@@ -1727,17 +1728,12 @@ int main(int argc, char **argv)
 	}
 
 	g_sbi.fixed_nsec = 0;
-	if (cfg.c_unix_timestamp != -1) {
-		mkfs_time = cfg.c_unix_timestamp;
-	} else if (!gettimeofday(&t, NULL)) {
-		mkfs_time = t.tv_sec;
-	}
-	if (erofs_sb_has_48bit(&g_sbi)) {
-		g_sbi.epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
-		g_sbi.build_time = mkfs_time - g_sbi.epoch;
-	} else {
-		g_sbi.epoch = mkfs_time;
-	}
+	if (mkfscfg.unix_timestamp != -1)
+		importer_params.build_time = mkfscfg.unix_timestamp;
+	else if (!gettimeofday(&t, NULL))
+		importer_params.build_time = t.tv_sec;
+	else
+		importer_params.build_time = 0;
 
 	err = erofs_dev_open(&g_sbi, cfg.c_img_path, O_RDWR |
 				(incremental_mode ? 0 : O_TRUNC));
Signature · 1 line
2.43.5
author Tested-by on v5 1/1
#8 lore raw
Quoted 8 lines · Gao Xiang

From: Yifan Zhao <[email protected]>

-Edot-omitted enables -E48bits, which requires specific configurations for g_sbi.{epoch, build_time}. Currently, the call to erofs_sb_set_48bit() occurs too late in the execution flow, causing the aforementioned logic to be bypassed and resulting in incorrect mtimes for all inodes.

Quoted 5 lines

This patch moves time initialization logic into erofs_importer_init() to resolve this issue.

Thanks,

Yifan

Quoted 142 lines

---
it seems the previous reply version was broken due to invalid formats.

  include/erofs/config.h   |  1 -
  include/erofs/importer.h |  1 +
  lib/config.c             |  1 -
  lib/importer.c           | 11 +++++++++++
  mkfs/main.c              | 26 +++++++++++---------------
  5 files changed, 23 insertions(+), 17 deletions(-)
include/erofs/config.h +0 -1
index 525a8cd5ebfb..2a84fb515868 100644
@@ -58,7 +58,6 @@ struct erofs_configure {
  	char c_force_chunkformat;
  	u8 c_mkfs_metabox_algid;
  	u32 c_max_decompressed_extent_bytes;
-	u64 c_unix_timestamp;
  	const char *mount_point;
  	u32 c_root_xattr_isize;
  #ifdef EROFS_MT_ENABLED
include/erofs/importer.h +1 -0
index a525b474f1d5..15e247e51b9c 100644
@@ -41,6 +41,7 @@ struct erofs_importer_params {
  	u32 pclusterblks_def;
  	u32 pclusterblks_packed;
  	s32 pclusterblks_metabox;
+	s64 build_time;
  	char force_inodeversion;
  	bool ignore_mtime;
  	bool no_datainline;
lib/config.c +0 -1
index 16b34fa840d3..5eb0ddeaa851 100644
@@ -29,7 +29,6 @@ void erofs_init_configure(void)
  	cfg.c_dbg_lvl  = EROFS_WARN;
  	cfg.c_version  = PACKAGE_VERSION;
  	cfg.c_dry_run  = false;
-	cfg.c_unix_timestamp = -1;
  	cfg.c_max_decompressed_extent_bytes = -1;
  	erofs_stdout_tty = isatty(STDOUT_FILENO);
  }
lib/importer.c +11 -0
index 958a433b9eaa..d686c519676b 100644
@@ -23,6 +23,7 @@ void erofs_importer_preset(struct erofs_importer_params *params)
  		.fixed_uid = -1,
  		.fixed_gid = -1,
  		.fsalignblks = 1,
+		.build_time = -1,
  	};
  }
 
@@ -83,6 +84,16 @@ int erofs_importer_init(struct erofs_importer *im)
 
  	if (params->dot_omitted)
  		erofs_sb_set_48bit(sbi);
+
+	if (params->build_time != -1) {
+		if (erofs_sb_has_48bit(sbi)) {
+			sbi->epoch = max_t(s64, 0, params->build_time - UINT32_MAX);
+			sbi->build_time = params->build_time - sbi->epoch;
+		} else {
+			sbi->epoch = params->build_time;
+		}
+	}
+
  	return 0;
 
  out_err:
mkfs/main.c +11 -15
index b45368f301f3..683c650db519 100644
@@ -274,8 +274,10 @@ static struct erofsmkfs_cfg {
  	/* < 0, xattr disabled and >= INT_MAX, always use inline xattrs */
  	long inlinexattr_tolerance;
  	bool inode_metazone;
+	u64 unix_timestamp;
  } mkfscfg = {
  	.inlinexattr_tolerance = 2,
+	.unix_timestamp = -1,
  };
 
  static unsigned int pclustersize_packed, pclustersize_max;
@@ -1096,8 +1098,8 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
  			break;
 
  		case 'T':
-			cfg.c_unix_timestamp = strtoull(optarg, &endptr, 0);
-			if (cfg.c_unix_timestamp == -1 || *endptr != '\0') {
+			mkfscfg.unix_timestamp = strtoull(optarg, &endptr, 0);
+			if (mkfscfg.unix_timestamp == -1 || *endptr != '\0') {
  				erofs_err("invalid UNIX timestamp %s", optarg);
  				return -EINVAL;
  			}
@@ -1576,7 +1578,7 @@ int parse_source_date_epoch(void)
  			  source_date_epoch);
  		return -EINVAL;
  	}
-	cfg.c_unix_timestamp = epoch;
+	mkfscfg.unix_timestamp = epoch;
  	cfg.c_timeinherit = TIMESTAMP_CLAMPING;
  	return 0;
  }
@@ -1702,7 +1704,6 @@ int main(int argc, char **argv)
  	bool tar_index_512b = false;
  	struct timeval t;
  	FILE *blklst = NULL;
-	s64 mkfs_time = 0;
  	int err;
  	u32 crc;
 
@@ -1727,17 +1728,12 @@ int main(int argc, char **argv)
  	}
 
  	g_sbi.fixed_nsec = 0;
-	if (cfg.c_unix_timestamp != -1) {
-		mkfs_time = cfg.c_unix_timestamp;
-	} else if (!gettimeofday(&t, NULL)) {
-		mkfs_time = t.tv_sec;
-	}
-	if (erofs_sb_has_48bit(&g_sbi)) {
-		g_sbi.epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
-		g_sbi.build_time = mkfs_time - g_sbi.epoch;
-	} else {
-		g_sbi.epoch = mkfs_time;
-	}
+	if (mkfscfg.unix_timestamp != -1)
+		importer_params.build_time = mkfscfg.unix_timestamp;
+	else if (!gettimeofday(&t, NULL))
+		importer_params.build_time = t.tv_sec;
+	else
+		importer_params.build_time = 0;
 
  	err = erofs_dev_open(&g_sbi, cfg.c_img_path, O_RDWR |
  				(incremental_mode ? 0 : O_TRUNC));