* [PATCH 2/3] hfs: store timestamps after February 2040 in spare catalog fields
2026-11-24 22:53 [PATCH 0/3] hfs: fix timestamp wrapping and support timestamps after 2040 Viacheslav Dubeyko
2026-11-24 22:53 ` [PATCH 1/3] hfs: fix timestamp wrapped issue Viacheslav Dubeyko
@ 2026-11-24 22:53 ` Viacheslav Dubeyko
2026-11-24 22:53 ` [PATCH 3/3] hfs: add KUnit tests for date/time conversion Viacheslav Dubeyko
2 siblings, 0 replies; 4+ messages in thread
From: Viacheslav Dubeyko @ 2026-11-24 22:53 UTC (permalink / raw)
To: glaubitz, frank.li
Cc: linux-fsdevel, linux-kernel, vdubeyko, Viacheslav Dubeyko
The timestamp wrapping issue has been fixed but it
restricts the supported time range to the one that the 32-bit
on-disk field can represent: from January 1, 1904 till
February 6, 2040 (local time). Any timestamp after
February 6, 2040 is clamped now.
HFS has no extended attributes on the volume. Extend the supported
time range by means of spare bytes of the catalog record. If the
modification date cannot be represented by the 32-bit on-disk
field, then the maximal value (0xFFFFFFFF, HFS_EXT_TIMESTAMP_MARK)
is stored into MdDat and the real value is stored into the spare
bytes of the same catalog record as big-endian 64-bit number of
seconds since January 1, 1904 (local time, as MdDat itself):
file record: FndrInfo.fdIconID and FndrInfo.fdUnused[0..5]
directory record: Resrv[0..7]
The fsck_hfs tool checks that StBlk, RStBlk, and Resrv of the file
record are zero. As a result, these fields cannot be used. However,
fsck_hfs does not check the Finder info of the file record and
the reserved field of the directory record. Apple uses the same
8 bytes of the extended Finder info in HFS+ (document ID and date
added).
Read path: if MdDat is equal to HFS_EXT_TIMESTAMP_MARK and
the spare bytes contain the value that cannot be represented
by MdDat, then hfs_cat_read_mtime() returns the extended value.
Otherwise, the value of MdDat is used. If another implementation
(for example, Mac OS) updates MdDat, then the mark is removed and
the stale value of the spare bytes is ignored. If the spare bytes
contain zero or garbage, then HFS_MAX_TIMESTAMP_SECS is used.
Write path: hfs_cat_write_mtime() stores the modification date
into the catalog record. The spare bytes are cleared if the
timestamp becomes representable by MdDat again. The spare bytes
are not touched for the records with regular MdDat because
the record could be created by another implementation.
The extended value is updated together with MdDat in the same
catalog record. It means that it is protected by the catalog
tree lock and no additional locking or b-tree update is required.
Only MdDat is extended because HFS driver uses it for mtime,
atime, and ctime. The CrDat and BkDat fields are not exposed by
HFS driver and they are clamped. The hfs_cat_build_record()
uses the timestamp of the inode for the new record. As a result,
sb->s_time_max is set to HFS_EXT_MAX_TIMESTAMP_SECS.
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/catalog.c | 10 +++---
fs/hfs/hfs_fs.h | 58 +++++++++++++++++++++++++++++--
fs/hfs/inode.c | 88 +++++++++++++++++++++++++++++++++++++++++++++---
fs/hfs/super.c | 6 +++-
4 files changed, 149 insertions(+), 13 deletions(-)
diff --git a/fs/hfs/catalog.c b/fs/hfs/catalog.c
index 490c5e130da3..76924783b136 100644
--- a/fs/hfs/catalog.c
+++ b/fs/hfs/catalog.c
@@ -35,14 +35,14 @@ void hfs_cat_build_key(struct super_block *sb, btree_key *key, u32 parent, const
static int hfs_cat_build_record(hfs_cat_rec *rec, u32 cnid, struct inode *inode)
{
- __be32 mtime = hfs_mtime();
+ time64_t mtime = inode_get_mtime_sec(inode);
memset(rec, 0, sizeof(*rec));
if (S_ISDIR(inode->i_mode)) {
rec->type = HFS_CDR_DIR;
rec->dir.DirID = cpu_to_be32(cnid);
- rec->dir.CrDat = mtime;
- rec->dir.MdDat = mtime;
+ rec->dir.CrDat = __hfs_u_to_mtime(mtime);
+ hfs_cat_write_mtime(rec, mtime);
rec->dir.BkDat = 0;
rec->dir.UsrInfo.frView = cpu_to_be16(0xff);
return sizeof(struct hfs_cat_dir);
@@ -53,8 +53,8 @@ static int hfs_cat_build_record(hfs_cat_rec *rec, u32 cnid, struct inode *inode)
if (!(inode->i_mode & S_IWUSR))
rec->file.Flags |= HFS_FIL_LOCK;
rec->file.FlNum = cpu_to_be32(cnid);
- rec->file.CrDat = mtime;
- rec->file.MdDat = mtime;
+ rec->file.CrDat = __hfs_u_to_mtime(mtime);
+ hfs_cat_write_mtime(rec, mtime);
rec->file.BkDat = 0;
rec->file.UsrWds.fdType = HFS_SB(inode->i_sb)->s_type;
rec->file.UsrWds.fdCreator = HFS_SB(inode->i_sb)->s_creator;
diff --git a/fs/hfs/hfs_fs.h b/fs/hfs/hfs_fs.h
index 0d710a45c61d..a07675e29fe6 100644
--- a/fs/hfs/hfs_fs.h
+++ b/fs/hfs/hfs_fs.h
@@ -219,6 +219,8 @@ extern void hfs_inode_write_fork(struct inode *inode, struct hfs_extent *ext,
extern int hfs_write_inode(struct inode *inode, struct writeback_control *wbc);
extern int hfs_inode_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
struct iattr *attr);
+time64_t hfs_cat_read_mtime(hfs_cat_rec *rec);
+void hfs_cat_write_mtime(hfs_cat_rec *rec, time64_t ut);
extern void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext,
__be32 __log_size, __be32 phys_size,
u32 clump_size);
@@ -286,11 +288,63 @@ static inline __be32 __hfs_u_to_mtime(time64_t ut)
return cpu_to_be32(lower_32_bits(ut));
}
+
+/*
+ * Linux specific extension of HFS on-disk layout that makes possible
+ * to support timestamps after HFS_MAX_TIMESTAMP_SECS.
+ *
+ * If the modification date cannot be represented by the 32-bit on-disk
+ * field, then the maximal value (HFS_EXT_TIMESTAMP_MARK) is stored into
+ * MdDat of the catalog record and the real value is stored into
+ * the spare bytes of the same catalog record as big-endian 64-bit
+ * number of seconds since January 1, 1904 (local time):
+ *
+ * file record: FndrInfo.fdIconID and FndrInfo.fdUnused[0..5]
+ * directory record: Resrv[0..7]
+ *
+ * fsck_hfs does not check these bytes. Another implementation that
+ * updates MdDat removes the mark. As a result, the stale value in
+ * the spare bytes is ignored.
+ */
+#define HFS_EXT_TIMESTAMP_MARK cpu_to_be32(U32_MAX)
+#define HFS_EXT_TIMESTAMP_SIZE sizeof(__be64)
+/* settimeofday() limits sys_tz.tz_minuteswest by +/-15 hours */
+#define HFS_MAX_TZ_SECS (15 * 60 * 60)
+/* maximal 64-bit on-disk value that can be converted into time64_t */
+#define HFS_EXT_MAX_MAC_DATE ((u64)TIME64_MAX - HFS_MAX_TZ_SECS)
+#define HFS_EXT_MAX_TIMESTAMP_SECS \
+ ((time64_t)HFS_EXT_MAX_MAC_DATE - HFS_UTC_OFFSET - HFS_MAX_TZ_SECS)
+
+static inline bool hfs_mt_is_ext_timestamp(__be32 mt)
+{
+ return mt == HFS_EXT_TIMESTAMP_MARK;
+}
+
+static inline bool hfs_ext_mt_is_valid(u64 mt)
+{
+ return mt > U32_MAX && mt <= HFS_EXT_MAX_MAC_DATE;
+}
+
+static inline time64_t __hfs_ext_m_to_utime(u64 mt)
+{
+ time64_t ut = (time64_t)mt - HFS_UTC_OFFSET;
+
+ return ut + sys_tz.tz_minuteswest * 60;
+}
+
+static inline u64 __hfs_u_to_ext_mtime(time64_t ut)
+{
+ ut = clamp_t(time64_t, ut,
+ HFS_MIN_TIMESTAMP_SECS - HFS_MAX_TZ_SECS,
+ HFS_EXT_MAX_TIMESTAMP_SECS);
+ ut -= sys_tz.tz_minuteswest * 60;
+ ut += HFS_UTC_OFFSET;
+
+ return ut < 0 ? 0 : 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)
-#define hfs_m_to_utime(time) (struct timespec64){ .tv_sec = __hfs_m_to_utime(time) }
-#define hfs_u_to_mtime(time) __hfs_u_to_mtime((time).tv_sec)
#define hfs_mtime() __hfs_u_to_mtime(ktime_get_real_seconds())
static inline const char *hfs_mdb_name(struct super_block *sb)
diff --git a/fs/hfs/inode.c b/fs/hfs/inode.c
index f1122ffae712..388fe24bf093 100644
--- a/fs/hfs/inode.c
+++ b/fs/hfs/inode.c
@@ -19,6 +19,8 @@
#include <linux/xattr.h>
#include <linux/blkdev.h>
#include <linux/fileattr.h>
+#include <linux/unaligned.h>
+#include <kunit/visibility.h>
#include "hfs_fs.h"
#include "btree.h"
@@ -343,12 +345,88 @@ static int hfs_test_inode(struct inode *inode, void *data)
/*
* hfs_read_inode
*/
+/*
+ * The spare bytes of the catalog record that keep the extended
+ * modification date: fdIconID and fdUnused[0..5] of the file record,
+ * Resrv[0..7] of the directory record.
+ */
+static void *hfs_cat_ext_mtime(hfs_cat_rec *rec)
+{
+ if (rec->type == HFS_CDR_DIR)
+ return rec->dir.Resrv;
+
+ return &rec->file.FndrInfo;
+}
+
+/*
+ * hfs_cat_read_mtime - get modification date of the catalog record
+ * @rec: file or directory catalog record
+ *
+ * If MdDat is equal to HFS_EXT_TIMESTAMP_MARK and the spare bytes
+ * contain the valid extended value, then the extended value is returned.
+ * Otherwise, the value of MdDat is returned.
+ */
+time64_t hfs_cat_read_mtime(hfs_cat_rec *rec)
+{
+ __be32 mt;
+ u64 ext_mt;
+
+ if (rec->type == HFS_CDR_DIR)
+ mt = rec->dir.MdDat;
+ else
+ mt = rec->file.MdDat;
+
+ if (hfs_mt_is_ext_timestamp(mt)) {
+ ext_mt = get_unaligned_be64(hfs_cat_ext_mtime(rec));
+ if (hfs_ext_mt_is_valid(ext_mt))
+ return __hfs_ext_m_to_utime(ext_mt);
+ }
+
+ return __hfs_m_to_utime(mt);
+}
+EXPORT_SYMBOL_IF_KUNIT(hfs_cat_read_mtime);
+
+/*
+ * hfs_cat_write_mtime - set modification date of the catalog record
+ * @rec: file or directory catalog record
+ * @ut: modification date (seconds since January 1, 1970)
+ *
+ * If @ut cannot be represented by MdDat, then HFS_EXT_TIMESTAMP_MARK
+ * is stored into MdDat and the real value is stored into the spare
+ * bytes of the record. The spare bytes are cleared only if they keep
+ * the stale extended value. Otherwise, they are left untouched because
+ * the record could be created by another implementation.
+ */
+void hfs_cat_write_mtime(hfs_cat_rec *rec, time64_t ut)
+{
+ __be32 *mt;
+ void *ext_mt = hfs_cat_ext_mtime(rec);
+ u64 value = __hfs_u_to_ext_mtime(ut);
+
+ if (rec->type == HFS_CDR_DIR)
+ mt = &rec->dir.MdDat;
+ else
+ mt = &rec->file.MdDat;
+
+ if (value > U32_MAX) {
+ *mt = HFS_EXT_TIMESTAMP_MARK;
+ put_unaligned_be64(value, ext_mt);
+ return;
+ }
+
+ if (hfs_mt_is_ext_timestamp(*mt) || value == U32_MAX)
+ memset(ext_mt, 0, HFS_EXT_TIMESTAMP_SIZE);
+
+ *mt = cpu_to_be32(value);
+}
+EXPORT_SYMBOL_IF_KUNIT(hfs_cat_write_mtime);
+
static int hfs_read_inode(struct inode *inode, void *data)
{
struct hfs_iget_data *idata = data;
struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
hfs_cat_rec *rec;
- struct timespec64 mtime;
+ struct timespec64 mtime = {};
HFS_I(inode)->flags = 0;
HFS_I(inode)->rsrc_inode = NULL;
@@ -385,7 +463,7 @@ static int hfs_read_inode(struct inode *inode, void *data)
inode->i_mode |= S_IWUGO;
inode->i_mode &= ~hsb->s_file_umask;
inode->i_mode |= S_IFREG;
- mtime = hfs_m_to_utime(rec->file.MdDat);
+ mtime.tv_sec = hfs_cat_read_mtime(rec);
inode_set_ctime_to_ts(inode, mtime);
inode_set_atime_to_ts(inode, mtime);
inode_set_mtime_to_ts(inode, mtime);
@@ -401,7 +479,7 @@ static int hfs_read_inode(struct inode *inode, void *data)
inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
HFS_I(inode)->fs_blocks = 0;
inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask);
- mtime = hfs_m_to_utime(rec->dir.MdDat);
+ mtime.tv_sec = hfs_cat_read_mtime(rec);
inode_set_ctime_to_ts(inode, mtime);
inode_set_atime_to_ts(inode, mtime);
inode_set_mtime_to_ts(inode, mtime);
@@ -581,7 +659,7 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
be32_to_cpu(rec.dir.DirID) != inode->i_ino) {
}
- rec.dir.MdDat = hfs_u_to_mtime(inode_get_mtime(inode));
+ hfs_cat_write_mtime(&rec, inode_get_mtime_sec(inode));
rec.dir.Val = cpu_to_be16(inode->i_size - 2);
hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
@@ -609,7 +687,7 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
else
rec.file.Flags |= HFS_FIL_LOCK;
hfs_inode_write_fork(inode, rec.file.ExtRec, &rec.file.LgLen, &rec.file.PyLen);
- rec.file.MdDat = hfs_u_to_mtime(inode_get_mtime(inode));
+ hfs_cat_write_mtime(&rec, inode_get_mtime_sec(inode));
hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
sizeof(struct hfs_cat_file));
diff --git a/fs/hfs/super.c b/fs/hfs/super.c
index a63b92709984..f4983e39daf5 100644
--- a/fs/hfs/super.c
+++ b/fs/hfs/super.c
@@ -348,7 +348,11 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
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;
+ /*
+ * Timestamps after HFS_MAX_TIMESTAMP_SECS are stored in
+ * the spare bytes of the catalog record.
+ */
+ sb->s_time_max = HFS_EXT_MAX_TIMESTAMP_SECS;
mutex_init(&sbi->mdb_lock);
mutex_init(&sbi->bitmap_lock);
--
2.53.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 3/3] hfs: add KUnit tests for date/time conversion
2026-11-24 22:53 [PATCH 0/3] hfs: fix timestamp wrapping and support timestamps after 2040 Viacheslav Dubeyko
2026-11-24 22:53 ` [PATCH 1/3] hfs: fix timestamp wrapped issue Viacheslav Dubeyko
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 ` Viacheslav Dubeyko
2 siblings, 0 replies; 4+ messages in thread
From: Viacheslav Dubeyko @ 2026-11-24 22:53 UTC (permalink / raw)
To: glaubitz, frank.li
Cc: linux-fsdevel, linux-kernel, vdubeyko, Viacheslav Dubeyko
Add KUnit tests for the HFS date/time conversion logic:
(1) the time range constants and the location of the spare
bytes in the catalog records;
(2) conversion of on-disk (1904-based) timestamps into
1970-based ones and vice versa;
(3) clamping of the timestamps out of the supported range
instead of wrapping;
(4) round trip of the timestamps from the supported range;
(5) conversion and validation of the extended 64-bit timestamps;
(6) storing the timestamps after February 2040 in the spare
bytes of the file and directory records without touching
other fields;
(7) removing the stale extended value if the timestamp becomes
representable by MdDat again;
(8) ignoring the extended value if another implementation
has updated MdDat;
(9) the mark without the valid extended value;
(10) preserving the spare bytes of another implementation.
HFS keeps timestamps in local time. So, the expected values are
shifted by the local time zone offset.
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/Makefile | 2 +-
fs/hfs/hfs_time_test.c | 400 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 401 insertions(+), 1 deletion(-)
create mode 100644 fs/hfs/hfs_time_test.c
diff --git a/fs/hfs/Makefile b/fs/hfs/Makefile
index a7c9ce6b4609..be68c5305f04 100644
--- a/fs/hfs/Makefile
+++ b/fs/hfs/Makefile
@@ -10,4 +10,4 @@ hfs-objs := bitmap.o bfind.o bnode.o brec.o btree.o \
part_tbl.o string.o super.o sysdep.o trans.o
# KUnit tests
-obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o
+obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o hfs_time_test.o
diff --git a/fs/hfs/hfs_time_test.c b/fs/hfs/hfs_time_test.c
new file mode 100644
index 000000000000..508ecfbfdec4
--- /dev/null
+++ b/fs/hfs/hfs_time_test.c
@@ -0,0 +1,400 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests for HFS date/time conversion
+ *
+ * Copyright (C) 2026 Viacheslav Dubeyko <slava@dubeyko.com>
+ */
+
+#include <kunit/test.h>
+#include <linux/unaligned.h>
+#include "hfs_fs.h"
+
+/* January 1, 1970, 00:00:00 UTC */
+#define TEST_UNIX_EPOCH_SECS 0LL
+/* December 31, 1969, 23:59:59 UTC */
+#define TEST_BEFORE_UNIX_EPOCH_SECS -1LL
+/* January 1, 2000, 00:00:00 UTC */
+#define TEST_Y2000_SECS 946684800LL
+/* January 19, 2038, 03:14:08 UTC (signed 32-bit overflow) */
+#define TEST_Y2038_SECS 2147483648LL
+/* January 1, 2026, 00:00:00 UTC */
+#define TEST_Y2026_SECS 1767225600LL
+/* January 1, 2100, 00:00:00 UTC */
+#define TEST_Y2100_SECS 4102444800LL
+/* January 1, 1900, 00:00:00 UTC */
+#define TEST_Y1900_SECS -2208988800LL
+
+/* on-disk (1904-based) values of the timestamps above */
+#define TEST_Y2000_MT 3029529600U
+#define TEST_Y2026_MT 3850070400U
+#define TEST_Y2038_MT 4230328448U
+#define TEST_Y2100_EXT_MT 6185289600ULL
+
+/* garbage of another implementation in the spare bytes */
+#define TEST_FOREIGN_BYTE 0x5A
+
+/*
+ * HFS keeps timestamps in local time. The expected values
+ * are shifted by the local time zone offset.
+ */
+static time64_t tz_secs(void)
+{
+ return sys_tz.tz_minuteswest * 60;
+}
+
+static u32 mt_raw(__be32 mt)
+{
+ return be32_to_cpu(mt);
+}
+
+static hfs_cat_rec *alloc_cat_rec(struct kunit *test, s8 type)
+{
+ hfs_cat_rec *rec;
+
+ rec = kunit_kzalloc(test, sizeof(*rec), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, rec);
+ rec->type = type;
+
+ return rec;
+}
+
+static u64 file_ext_mt(hfs_cat_rec *rec)
+{
+ return get_unaligned_be64(&rec->file.FndrInfo);
+}
+
+static u64 dir_ext_mt(hfs_cat_rec *rec)
+{
+ return get_unaligned_be64(rec->dir.Resrv);
+}
+
+/* Test the time range constants */
+static void hfs_time_constants_test(struct kunit *test)
+{
+ /* 32-bit unsigned on-disk field covers exactly U32_MAX seconds */
+ KUNIT_EXPECT_EQ(test, (s64)U32_MAX,
+ HFS_MAX_TIMESTAMP_SECS - HFS_MIN_TIMESTAMP_SECS);
+ KUNIT_EXPECT_EQ(test, -(s64)HFS_UTC_OFFSET, HFS_MIN_TIMESTAMP_SECS);
+
+ /* spare bytes extend the supported range beyond February 2040 */
+ KUNIT_EXPECT_GT(test, HFS_EXT_MAX_TIMESTAMP_SECS,
+ HFS_MAX_TIMESTAMP_SECS);
+
+ /* the extended timestamp mark is the maximal on-disk value */
+ KUNIT_EXPECT_EQ(test, U32_MAX, mt_raw(HFS_EXT_TIMESTAMP_MARK));
+ KUNIT_EXPECT_EQ(test, 8, (int)HFS_EXT_TIMESTAMP_SIZE);
+
+ /* the spare bytes do not overlap the used fields */
+ KUNIT_EXPECT_GE(test, offsetof(struct hfs_fxinfo, fdUnused) + 6,
+ HFS_EXT_TIMESTAMP_SIZE);
+ KUNIT_EXPECT_GE(test, offsetof(struct hfs_fxinfo, fdComment),
+ HFS_EXT_TIMESTAMP_SIZE + 2);
+ KUNIT_EXPECT_GE(test, sizeof(((struct hfs_cat_dir *)0)->Resrv),
+ HFS_EXT_TIMESTAMP_SIZE);
+}
+
+/* Test conversion of on-disk (1904-based) timestamp into 1970-based one */
+static void hfs_m_to_utime_test(struct kunit *test)
+{
+ time64_t tz = tz_secs();
+
+ KUNIT_EXPECT_EQ(test, HFS_MIN_TIMESTAMP_SECS + tz,
+ __hfs_m_to_utime(cpu_to_be32(0)));
+ KUNIT_EXPECT_EQ(test, TEST_UNIX_EPOCH_SECS + tz,
+ __hfs_m_to_utime(cpu_to_be32(HFS_UTC_OFFSET)));
+ KUNIT_EXPECT_EQ(test, TEST_BEFORE_UNIX_EPOCH_SECS + tz,
+ __hfs_m_to_utime(cpu_to_be32(HFS_UTC_OFFSET - 1)));
+ KUNIT_EXPECT_EQ(test, TEST_Y2000_SECS + tz,
+ __hfs_m_to_utime(cpu_to_be32(TEST_Y2000_MT)));
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_SECS + tz,
+ __hfs_m_to_utime(cpu_to_be32(TEST_Y2026_MT)));
+ KUNIT_EXPECT_EQ(test, TEST_Y2038_SECS + tz,
+ __hfs_m_to_utime(cpu_to_be32(TEST_Y2038_MT)));
+ KUNIT_EXPECT_EQ(test, HFS_MAX_TIMESTAMP_SECS + tz,
+ __hfs_m_to_utime(HFS_EXT_TIMESTAMP_MARK));
+}
+
+/* Test conversion of 1970-based timestamp into on-disk (1904-based) one */
+static void hfs_u_to_mtime_test(struct kunit *test)
+{
+ time64_t tz = tz_secs();
+
+ KUNIT_EXPECT_EQ(test, 0U,
+ mt_raw(__hfs_u_to_mtime(HFS_MIN_TIMESTAMP_SECS + tz)));
+ KUNIT_EXPECT_EQ(test, HFS_UTC_OFFSET,
+ mt_raw(__hfs_u_to_mtime(TEST_UNIX_EPOCH_SECS + tz)));
+ KUNIT_EXPECT_EQ(test, HFS_UTC_OFFSET - 1,
+ mt_raw(__hfs_u_to_mtime(TEST_BEFORE_UNIX_EPOCH_SECS + tz)));
+ KUNIT_EXPECT_EQ(test, TEST_Y2000_MT,
+ mt_raw(__hfs_u_to_mtime(TEST_Y2000_SECS + tz)));
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_MT,
+ mt_raw(__hfs_u_to_mtime(TEST_Y2026_SECS + tz)));
+ KUNIT_EXPECT_EQ(test, TEST_Y2038_MT,
+ mt_raw(__hfs_u_to_mtime(TEST_Y2038_SECS + tz)));
+ KUNIT_EXPECT_EQ(test, U32_MAX,
+ mt_raw(__hfs_u_to_mtime(HFS_MAX_TIMESTAMP_SECS + tz)));
+}
+
+/* Test that out of range timestamps are clamped instead of wrapped */
+static void hfs_u_to_mtime_clamp_test(struct kunit *test)
+{
+ time64_t tz = tz_secs();
+
+ KUNIT_EXPECT_EQ(test, 0U,
+ mt_raw(__hfs_u_to_mtime(TEST_Y1900_SECS + tz)));
+ KUNIT_EXPECT_EQ(test, 0U,
+ mt_raw(__hfs_u_to_mtime(HFS_MIN_TIMESTAMP_SECS + tz - 1)));
+ KUNIT_EXPECT_EQ(test, U32_MAX,
+ mt_raw(__hfs_u_to_mtime(HFS_MAX_TIMESTAMP_SECS + tz + 1)));
+ KUNIT_EXPECT_EQ(test, U32_MAX,
+ mt_raw(__hfs_u_to_mtime(TEST_Y2100_SECS + tz)));
+}
+
+/* Test round trip of the timestamps from the supported range */
+static void hfs_time_round_trip_test(struct kunit *test)
+{
+ static const time64_t values[] = {
+ HFS_MIN_TIMESTAMP_SECS,
+ TEST_BEFORE_UNIX_EPOCH_SECS,
+ TEST_UNIX_EPOCH_SECS,
+ TEST_Y2000_SECS,
+ TEST_Y2026_SECS,
+ TEST_Y2038_SECS,
+ HFS_MAX_TIMESTAMP_SECS,
+ };
+ time64_t tz = tz_secs();
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(values); i++) {
+ time64_t ut = values[i] + tz;
+
+ KUNIT_EXPECT_EQ(test, ut,
+ __hfs_m_to_utime(__hfs_u_to_mtime(ut)));
+ }
+}
+
+/* Test conversion of the extended (64-bit) on-disk timestamps */
+static void hfs_ext_mtime_test(struct kunit *test)
+{
+ time64_t tz = tz_secs();
+
+ KUNIT_EXPECT_EQ(test, TEST_Y2100_EXT_MT,
+ __hfs_u_to_ext_mtime(TEST_Y2100_SECS + tz));
+ KUNIT_EXPECT_EQ(test, TEST_Y2100_SECS + tz,
+ __hfs_ext_m_to_utime(TEST_Y2100_EXT_MT));
+
+ /* the extended value is the continuation of the 32-bit one */
+ KUNIT_EXPECT_EQ(test, (u64)TEST_Y2026_MT,
+ __hfs_u_to_ext_mtime(TEST_Y2026_SECS + tz));
+ KUNIT_EXPECT_EQ(test, (u64)U32_MAX + 1,
+ __hfs_u_to_ext_mtime(HFS_MAX_TIMESTAMP_SECS + tz + 1));
+
+ /* clamping */
+ KUNIT_EXPECT_EQ(test, 0ULL, __hfs_u_to_ext_mtime(TIME64_MIN));
+ KUNIT_EXPECT_LE(test, __hfs_u_to_ext_mtime(TIME64_MAX),
+ HFS_EXT_MAX_MAC_DATE);
+ KUNIT_EXPECT_TRUE(test,
+ hfs_ext_mt_is_valid(__hfs_u_to_ext_mtime(TIME64_MAX)));
+ KUNIT_EXPECT_EQ(test,
+ HFS_EXT_MAX_TIMESTAMP_SECS,
+ __hfs_ext_m_to_utime(__hfs_u_to_ext_mtime(TIME64_MAX)));
+}
+
+/* Test validation of the extended on-disk timestamps */
+static void hfs_ext_mt_is_valid_test(struct kunit *test)
+{
+ KUNIT_EXPECT_FALSE(test, hfs_ext_mt_is_valid(0));
+ KUNIT_EXPECT_FALSE(test, hfs_ext_mt_is_valid(TEST_Y2026_MT));
+ KUNIT_EXPECT_FALSE(test, hfs_ext_mt_is_valid(U32_MAX));
+ KUNIT_EXPECT_TRUE(test, hfs_ext_mt_is_valid((u64)U32_MAX + 1));
+ KUNIT_EXPECT_TRUE(test, hfs_ext_mt_is_valid(TEST_Y2100_EXT_MT));
+ KUNIT_EXPECT_TRUE(test, hfs_ext_mt_is_valid(HFS_EXT_MAX_MAC_DATE));
+ KUNIT_EXPECT_FALSE(test, hfs_ext_mt_is_valid(HFS_EXT_MAX_MAC_DATE + 1));
+ KUNIT_EXPECT_FALSE(test, hfs_ext_mt_is_valid(U64_MAX));
+
+ KUNIT_EXPECT_TRUE(test,
+ hfs_mt_is_ext_timestamp(HFS_EXT_TIMESTAMP_MARK));
+ KUNIT_EXPECT_FALSE(test,
+ hfs_mt_is_ext_timestamp(cpu_to_be32(TEST_Y2026_MT)));
+ KUNIT_EXPECT_FALSE(test, hfs_mt_is_ext_timestamp(cpu_to_be32(0)));
+}
+
+/* Test regular timestamp in the file record */
+static void hfs_cat_file_regular_mtime_test(struct kunit *test)
+{
+ hfs_cat_rec *rec = alloc_cat_rec(test, HFS_CDR_FIL);
+ time64_t tz = tz_secs();
+
+ hfs_cat_write_mtime(rec, TEST_Y2026_SECS + tz);
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_MT, mt_raw(rec->file.MdDat));
+ KUNIT_EXPECT_EQ(test, 0ULL, file_ext_mt(rec));
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_SECS + tz, hfs_cat_read_mtime(rec));
+}
+
+/* Test timestamp after February 2040 in the file record */
+static void hfs_cat_file_ext_mtime_test(struct kunit *test)
+{
+ hfs_cat_rec *rec = alloc_cat_rec(test, HFS_CDR_FIL);
+ time64_t tz = tz_secs();
+
+ rec->file.FlNum = cpu_to_be32(HFS_FIRSTUSER_CNID);
+ rec->file.CrDat = cpu_to_be32(TEST_Y2000_MT);
+ rec->file.FndrInfo.fdUnused[6] = TEST_FOREIGN_BYTE;
+ rec->file.FndrInfo.fdUnused[7] = TEST_FOREIGN_BYTE;
+ rec->file.FndrInfo.fdComment = cpu_to_be16(0x1234);
+ rec->file.FndrInfo.fdPutAway = cpu_to_be32(0xDEADBEEF);
+
+ hfs_cat_write_mtime(rec, TEST_Y2100_SECS + tz);
+ KUNIT_EXPECT_TRUE(test, hfs_mt_is_ext_timestamp(rec->file.MdDat));
+ KUNIT_EXPECT_EQ(test, TEST_Y2100_EXT_MT, file_ext_mt(rec));
+ KUNIT_EXPECT_EQ(test, TEST_Y2100_SECS + tz, hfs_cat_read_mtime(rec));
+
+ /* other fields are not touched */
+ KUNIT_EXPECT_EQ(test, HFS_FIRSTUSER_CNID, be32_to_cpu(rec->file.FlNum));
+ KUNIT_EXPECT_EQ(test, TEST_Y2000_MT, mt_raw(rec->file.CrDat));
+ KUNIT_EXPECT_EQ(test, TEST_FOREIGN_BYTE,
+ rec->file.FndrInfo.fdUnused[6]);
+ KUNIT_EXPECT_EQ(test, TEST_FOREIGN_BYTE,
+ rec->file.FndrInfo.fdUnused[7]);
+ KUNIT_EXPECT_EQ(test, 0x1234, be16_to_cpu(rec->file.FndrInfo.fdComment));
+ KUNIT_EXPECT_EQ(test, 0xDEADBEEF,
+ be32_to_cpu(rec->file.FndrInfo.fdPutAway));
+ KUNIT_EXPECT_EQ(test, 0U, rec->file.Resrv);
+
+ /* timestamp becomes representable again: stale value is removed */
+ hfs_cat_write_mtime(rec, TEST_Y2026_SECS + tz);
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_MT, mt_raw(rec->file.MdDat));
+ KUNIT_EXPECT_EQ(test, 0ULL, file_ext_mt(rec));
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_SECS + tz, hfs_cat_read_mtime(rec));
+ KUNIT_EXPECT_EQ(test, TEST_FOREIGN_BYTE,
+ rec->file.FndrInfo.fdUnused[6]);
+ KUNIT_EXPECT_EQ(test, 0x1234, be16_to_cpu(rec->file.FndrInfo.fdComment));
+}
+
+/* Test timestamp after February 2040 in the directory record */
+static void hfs_cat_dir_ext_mtime_test(struct kunit *test)
+{
+ hfs_cat_rec *rec = alloc_cat_rec(test, HFS_CDR_DIR);
+ time64_t tz = tz_secs();
+ int i;
+
+ rec->dir.DirID = cpu_to_be32(HFS_FIRSTUSER_CNID);
+ rec->dir.FndrInfo.frPutAway = cpu_to_be32(0xDEADBEEF);
+ for (i = HFS_EXT_TIMESTAMP_SIZE; i < sizeof(rec->dir.Resrv); i++)
+ rec->dir.Resrv[i] = TEST_FOREIGN_BYTE;
+
+ hfs_cat_write_mtime(rec, TEST_Y2026_SECS + tz);
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_MT, mt_raw(rec->dir.MdDat));
+ KUNIT_EXPECT_EQ(test, 0ULL, dir_ext_mt(rec));
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_SECS + tz, hfs_cat_read_mtime(rec));
+
+ hfs_cat_write_mtime(rec, TEST_Y2100_SECS + tz);
+ KUNIT_EXPECT_TRUE(test, hfs_mt_is_ext_timestamp(rec->dir.MdDat));
+ KUNIT_EXPECT_EQ(test, TEST_Y2100_EXT_MT, dir_ext_mt(rec));
+ KUNIT_EXPECT_EQ(test, TEST_Y2100_SECS + tz, hfs_cat_read_mtime(rec));
+
+ /* other fields are not touched */
+ KUNIT_EXPECT_EQ(test, HFS_FIRSTUSER_CNID, be32_to_cpu(rec->dir.DirID));
+ KUNIT_EXPECT_EQ(test, 0xDEADBEEF,
+ be32_to_cpu(rec->dir.FndrInfo.frPutAway));
+ for (i = HFS_EXT_TIMESTAMP_SIZE; i < sizeof(rec->dir.Resrv); i++)
+ KUNIT_EXPECT_EQ(test, TEST_FOREIGN_BYTE, rec->dir.Resrv[i]);
+
+ hfs_cat_write_mtime(rec, TEST_Y2000_SECS + tz);
+ KUNIT_EXPECT_EQ(test, TEST_Y2000_MT, mt_raw(rec->dir.MdDat));
+ KUNIT_EXPECT_EQ(test, 0ULL, dir_ext_mt(rec));
+ KUNIT_EXPECT_EQ(test, TEST_Y2000_SECS + tz, hfs_cat_read_mtime(rec));
+}
+
+/* Test that another implementation updating MdDat invalidates the value */
+static void hfs_cat_foreign_mtime_update_test(struct kunit *test)
+{
+ hfs_cat_rec *rec = alloc_cat_rec(test, HFS_CDR_FIL);
+ time64_t tz = tz_secs();
+
+ hfs_cat_write_mtime(rec, TEST_Y2100_SECS + tz);
+ KUNIT_EXPECT_TRUE(test, hfs_mt_is_ext_timestamp(rec->file.MdDat));
+
+ /* Mac OS updates MdDat but keeps the spare bytes */
+ rec->file.MdDat = cpu_to_be32(TEST_Y2026_MT);
+ KUNIT_EXPECT_EQ(test, TEST_Y2100_EXT_MT, file_ext_mt(rec));
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_SECS + tz, hfs_cat_read_mtime(rec));
+}
+
+/* Test the mark without the valid extended value */
+static void hfs_cat_mark_without_ext_test(struct kunit *test)
+{
+ hfs_cat_rec *rec = alloc_cat_rec(test, HFS_CDR_FIL);
+ time64_t tz = tz_secs();
+
+ /* the spare bytes are zero */
+ rec->file.MdDat = HFS_EXT_TIMESTAMP_MARK;
+ KUNIT_EXPECT_EQ(test, HFS_MAX_TIMESTAMP_SECS + tz,
+ hfs_cat_read_mtime(rec));
+
+ /* the value is representable by MdDat */
+ put_unaligned_be64(TEST_Y2026_MT, &rec->file.FndrInfo);
+ KUNIT_EXPECT_EQ(test, HFS_MAX_TIMESTAMP_SECS + tz,
+ hfs_cat_read_mtime(rec));
+
+ /* the value cannot be converted into time64_t */
+ put_unaligned_be64(U64_MAX, &rec->file.FndrInfo);
+ KUNIT_EXPECT_EQ(test, HFS_MAX_TIMESTAMP_SECS + tz,
+ hfs_cat_read_mtime(rec));
+}
+
+/* Test that the spare bytes of another implementation are preserved */
+static void hfs_cat_foreign_spare_bytes_test(struct kunit *test)
+{
+ hfs_cat_rec *rec = alloc_cat_rec(test, HFS_CDR_FIL);
+ time64_t tz = tz_secs();
+ u64 foreign;
+
+ memset(&rec->file.FndrInfo, TEST_FOREIGN_BYTE, HFS_EXT_TIMESTAMP_SIZE);
+ foreign = file_ext_mt(rec);
+ rec->file.MdDat = cpu_to_be32(TEST_Y2000_MT);
+
+ hfs_cat_write_mtime(rec, TEST_Y2026_SECS + tz);
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_MT, mt_raw(rec->file.MdDat));
+ KUNIT_EXPECT_EQ(test, foreign, file_ext_mt(rec));
+ KUNIT_EXPECT_EQ(test, TEST_Y2026_SECS + tz, hfs_cat_read_mtime(rec));
+
+ /*
+ * The maximal regular value is equal to the mark. The spare bytes
+ * must be cleared, otherwise they will be treated as the extended
+ * value.
+ */
+ hfs_cat_write_mtime(rec, HFS_MAX_TIMESTAMP_SECS + tz);
+ KUNIT_EXPECT_TRUE(test, hfs_mt_is_ext_timestamp(rec->file.MdDat));
+ KUNIT_EXPECT_EQ(test, 0ULL, file_ext_mt(rec));
+ KUNIT_EXPECT_EQ(test, HFS_MAX_TIMESTAMP_SECS + tz,
+ hfs_cat_read_mtime(rec));
+}
+
+static struct kunit_case hfs_time_test_cases[] = {
+ KUNIT_CASE(hfs_time_constants_test),
+ KUNIT_CASE(hfs_m_to_utime_test),
+ KUNIT_CASE(hfs_u_to_mtime_test),
+ KUNIT_CASE(hfs_u_to_mtime_clamp_test),
+ KUNIT_CASE(hfs_time_round_trip_test),
+ KUNIT_CASE(hfs_ext_mtime_test),
+ KUNIT_CASE(hfs_ext_mt_is_valid_test),
+ KUNIT_CASE(hfs_cat_file_regular_mtime_test),
+ KUNIT_CASE(hfs_cat_file_ext_mtime_test),
+ KUNIT_CASE(hfs_cat_dir_ext_mtime_test),
+ KUNIT_CASE(hfs_cat_foreign_mtime_update_test),
+ KUNIT_CASE(hfs_cat_mark_without_ext_test),
+ KUNIT_CASE(hfs_cat_foreign_spare_bytes_test),
+ {}
+};
+
+static struct kunit_suite hfs_time_test_suite = {
+ .name = "hfs_time",
+ .test_cases = hfs_time_test_cases,
+};
+
+kunit_test_suite(hfs_time_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests for HFS date/time conversion");
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
--
2.53.0
^ permalink raw reply [flat|nested] 4+ messages in thread