From: Viacheslav Dubeyko <slava@dubeyko.com>
To: glaubitz@physik.fu-berlin.de, frank.li@vivo.com
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
vdubeyko@coreweave.com, Viacheslav Dubeyko <slava@dubeyko.com>
Subject: [PATCH 1/3] hfs: fix timestamp wrapped issue
Date: Tue, 24 Nov 2026 14:53:28 -0800 [thread overview]
Message-ID: <20261124225330.3879708-2-slava@dubeyko.com> (raw)
In-Reply-To: <20261124225330.3879708-1-slava@dubeyko.com>
The main reason of the issue is the logic:
cpu_to_be32(lower_32_bits(ut) + HFS_UTC_OFFSET)
At first, we take the lower 32 bits of the value and, then,
we add the time offset. However, if we have negative value
(timestamp before January 1, 1970), then we make completely
wrong calculation. The reverse conversion has the same issue
because it treats any on-disk timestamp under HFS_UTC_OFFSET
(January 1, 1970) as a time between 2040 and 2106. Moreover,
any timestamp after February 2106 is silently wrapped.
This patch corrects the logic of __hfs_m_to_utime() and
__hfs_u_to_mtime(). Now, the on-disk timestamp is interpreted
as unsigned number of seconds since January 1, 1904. As a result,
the supported time range is from January 1, 1904 till
February 6, 2040. HFS keeps timestamps in local time. So,
the range is shifted by the local time zone offset.
__hfs_u_to_mtime() clamps the value into the supported range
instead of wrapping it.
The hfs_fill_super() logic defines sb->s_time_min,
sb->s_time_max, and sb->s_time_gran. It means that VFS clamps
the timestamps that are out of the supported range.
This is the HFS port of the HFS+ patch
"hfsplus: fix timestamp wrapped issue".
Signed-off-by: Viacheslav Dubeyko <slava@dubeyko.com>
cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
cc: Yangtao Li <frank.li@vivo.com>
cc: linux-fsdevel@vger.kernel.org
---
fs/hfs/hfs_fs.h | 16 ++++++++++++++--
fs/hfs/super.c | 4 ++++
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/fs/hfs/hfs_fs.h b/fs/hfs/hfs_fs.h
index 2d56f8b1a5fa..0d710a45c61d 100644
--- a/fs/hfs/hfs_fs.h
+++ b/fs/hfs/hfs_fs.h
@@ -261,9 +261,18 @@ extern int hfs_mac2asc(struct super_block *sb,
/* super.c */
extern void hfs_mark_mdb_dirty(struct super_block *sb);
+/*
+ * time helpers: convert between 1904-base and 1970-base timestamps
+ *
+ * HFS keeps timestamps as unsigned 32-bit number of seconds since
+ * January 1, 1904 in local time. The on-disk timestamps cover the range
+ * from HFS_MIN_TIMESTAMP_SECS (Jan. 1, 1904) till HFS_MAX_TIMESTAMP_SECS
+ * (Feb. 6, 2040) shifted by the local time zone offset. The timestamps
+ * out of this range are clamped.
+ */
static inline time64_t __hfs_m_to_utime(__be32 mt)
{
- time64_t ut = (u32)(be32_to_cpu(mt) - HFS_UTC_OFFSET);
+ time64_t ut = (time64_t)be32_to_cpu(mt) - HFS_UTC_OFFSET;
return ut + sys_tz.tz_minuteswest * 60;
}
@@ -271,8 +280,11 @@ static inline time64_t __hfs_m_to_utime(__be32 mt)
static inline __be32 __hfs_u_to_mtime(time64_t ut)
{
ut -= sys_tz.tz_minuteswest * 60;
+ ut = clamp_t(time64_t, ut,
+ HFS_MIN_TIMESTAMP_SECS, HFS_MAX_TIMESTAMP_SECS);
+ ut += HFS_UTC_OFFSET;
- return cpu_to_be32(lower_32_bits(ut) + HFS_UTC_OFFSET);
+ return cpu_to_be32(lower_32_bits(ut));
}
#define HFS_I(inode) (container_of(inode, struct hfs_inode_info, vfs_inode))
#define HFS_SB(sb) ((struct hfs_sb_info *)(sb)->s_fs_info)
diff --git a/fs/hfs/super.c b/fs/hfs/super.c
index c0c3541e3d34..a63b92709984 100644
--- a/fs/hfs/super.c
+++ b/fs/hfs/super.c
@@ -345,6 +345,10 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
sb->s_op = &hfs_super_operations;
sb->s_xattr = hfs_xattr_handlers;
sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
+ sb->s_time_gran = NSEC_PER_SEC;
+ /* on-disk timestamps are in local time */
+ sb->s_time_min = HFS_MIN_TIMESTAMP_SECS + sys_tz.tz_minuteswest * 60;
+ sb->s_time_max = HFS_MAX_TIMESTAMP_SECS + sys_tz.tz_minuteswest * 60;
mutex_init(&sbi->mdb_lock);
mutex_init(&sbi->bitmap_lock);
--
2.53.0
next prev parent reply other threads:[~2026-11-24 22:53 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 22:53 [PATCH 0/3] hfs: fix timestamp wrapping and support timestamps after 2040 Viacheslav Dubeyko
2026-11-24 22:53 ` Viacheslav Dubeyko [this message]
2026-11-24 22:53 ` [PATCH 2/3] hfs: store timestamps after February 2040 in spare catalog fields Viacheslav Dubeyko
2026-11-24 22:53 ` [PATCH 3/3] hfs: add KUnit tests for date/time conversion Viacheslav Dubeyko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124225330.3879708-2-slava@dubeyko.com \
--to=slava@dubeyko.com \
--cc=frank.li@vivo.com \
--cc=glaubitz@physik.fu-berlin.de \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=vdubeyko@coreweave.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®