hfs: stop using timespec based interfaces
Subsystems (3)
PATCH 1/3 hfs: stop using timespec based interfaces
The native HFS timestamps overflow in year 2040, two years after the Unix y2038 overflow. However, the way that the conversion between on-disk timestamps and in-kernel timestamps was implemented, 64-bit machines actually ended up converting negative UTC timestamps (1902 through 1969) into times between 2038 and 2106.
Rather than making all machines faithfully represent timestamps in the ancient past but break after 2040, this changes the file system to always use the unsigned UTC interpretation, reading back times between 1970 and 2106.
--- fs/hfs/hfs_fs.h | 6 ++++-- fs/hfs/inode.c | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-)
fs/hfs/hfs_fs.h +4 -2
@@ -245,6 +245,8 @@ extern void hfs_mark_mdb_dirty(struct super_block *sb);* Unix: unsigned lil-endian since 00:00 GMT, Jan. 1, 1970 * mac: unsigned big-endian since 00:00 GMT, Jan. 1, 1904 * +* We treat all timestamps before 1970 as times after 2038, so this +* actually works until year 2106 */ #define __hfs_u_to_mtime (sec) cpu_to_be32(sec + 2082844800U - sys_tz.tz_minuteswest * 60)#define __hfs_m_to_utime (sec) (be32_to_cpu(sec) - 2082844800U + sys_tz.tz_minuteswest * 60)
@@ -252,9 +254,9 @@ extern void hfs_mark_mdb_dirty(struct super_block *sb);#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 timespec){ .tv_sec = __hfs_m_to_utime(time) } +#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(get_seconds()) +#define hfs_mtime () __hfs_u_to_mtime(ktime_get_real_seconds())static inline const char *hfs_mdb_name (struct super_block *sb) {
fs/hfs/inode.c +2 -2
@@ -351,7 +351,7 @@ static int hfs_read_inode(struct inode *inode, void *data) inode->i_mode &= ~hsb->s_file_umask; inode->i_mode |=S_IFREG ; inode->i_ctime = inode->i_atime = inode->i_mtime = -timespec_to_timespec64 (hfs_m_to_utime (rec->file.MdDat)); +hfs_m_to_utime (rec->file.MdDat); inode->i_op = &hfs_file_inode_operations; inode->i_fop = &hfs_file_operations; inode->i_mapping->a_ops = &hfs_aops;
@@ -362,7 +362,7 @@ static int hfs_read_inode(struct inode *inode, void *data)HFS_I (inode)->fs_blocks =0 ; inode->i_mode =S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask); inode->i_ctime = inode->i_atime = inode->i_mtime = -timespec_to_timespec64 (hfs_m_to_utime (rec->dir.MdDat)); +hfs_m_to_utime (rec->dir.MdDat); inode->i_op = &hfs_dir_inode_operations; inode->i_fop = &hfs_dir_operations;break ;
Signature · 1 line
2.9.0
PATCH 2/3 hfsplus: stop using timespec based interfaces
The native HFS+ timestamps overflow in year 2040, two years after the Unix y2038 overflow. However, the way that the conversion between on-disk timestamps and in-kernel timestamps was implemented, 64-bit machines actually ended up converting negative UTC timestamps (1902 through 1969) into times between 2038 and 2106.
Rather than making all machines faithfully represent timestamps in the ancient past but break after 2040, this changes the file system to always use the unsigned UTC interpretation, reading back times between 1970 and 2106.
--- fs/hfsplus/hfsplus_fs.h | 7 ++++--- fs/hfsplus/inode.c | 12 ++++++------ 2 files changed, 10 insertions(+), 9 deletions(-)
fs/hfsplus/hfsplus_fs.h +4 -3
@@ -530,13 +530,14 @@ int hfsplus_submit_bio(struct super_block *sb, sector_t sector, void *buf, void **data, int op, int op_flags);int hfsplus_read_wrapper (struct super_block *sb); -/* time macros */ +/* time macros: convert between 1904-2040 and 1970-2106 range, +* pre-1970 timestamps are interpreted as post-2038 times after wrap-around */ #define __hfsp_mt2ut (t) (be32_to_cpu(t) - 2082844800U)#define __hfsp_ut2mt (t) (cpu_to_be32(t + 2082844800U))/* compatibility */ -#define hfsp_mt2ut (t) (struct timespec){ .tv_sec = __hfsp_mt2ut(t) } +#define hfsp_mt2ut (t) (struct timespec64){ .tv_sec = __hfsp_mt2ut(t) }#define hfsp_ut2mt (t) __hfsp_ut2mt((t).tv_sec) -#define hfsp_now2mt () __hfsp_ut2mt(get_seconds()) +#define hfsp_now2mt () __hfsp_ut2mt(ktime_get_real_seconds())#endif
fs/hfsplus/inode.c +6 -6
@@ -493,9 +493,9 @@ int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)hfsplus_get_perms (inode, &folder->permissions,1 );set_nlink (inode,1 ); inode->i_size =2 +be32_to_cpu (folder->valence); - inode->i_atime =timespec_to_timespec64 (hfsp_mt2ut (folder->access_date)); - inode->i_mtime =timespec_to_timespec64 (hfsp_mt2ut (folder->content_mod_date)); - inode->i_ctime =timespec_to_timespec64 (hfsp_mt2ut (folder->attribute_mod_date)); + inode->i_atime =hfsp_mt2ut (folder->access_date); + inode->i_mtime =hfsp_mt2ut (folder->content_mod_date); + inode->i_ctime =hfsp_mt2ut (folder->attribute_mod_date);HFSPLUS_I (inode)->create_date = folder->create_date;HFSPLUS_I (inode)->fs_blocks =0 ;if (folder->flags &cpu_to_be16 (HFSPLUS_HAS_FOLDER_COUNT )) {
@@ -531,9 +531,9 @@ int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)init_special_inode (inode, inode->i_mode,be32_to_cpu (file->permissions.dev)); } - inode->i_atime =timespec_to_timespec64 (hfsp_mt2ut (file->access_date)); - inode->i_mtime =timespec_to_timespec64 (hfsp_mt2ut (file->content_mod_date)); - inode->i_ctime =timespec_to_timespec64 (hfsp_mt2ut (file->attribute_mod_date)); + inode->i_atime =hfsp_mt2ut (file->access_date); + inode->i_mtime =hfsp_mt2ut (file->content_mod_date); + inode->i_ctime =hfsp_mt2ut (file->attribute_mod_date);HFSPLUS_I (inode)->create_date = file->create_date; }else {pr_err ("bad catalog entry used to create inode\n" );
Signature · 1 line
2.9.0
Re: [PATCH 2/3] hfsplus: stop using timespec based interfaces
Quoted 22 lines · Arnd Bergmann
The native HFS+ timestamps overflow in year 2040, two years after the Unix y2038 overflow. However, the way that the conversion between on-disk timestamps and in-kernel timestamps was implemented, 64-bit machines actually ended up converting negative UTC timestamps (1902 through 1969) into times between 2038 and 2106.
Rather than making all machines faithfully represent timestamps in the ancient past but break after 2040, this changes the file system to always use the unsigned UTC interpretation, reading back times between 1970 and 2106.
Signed-off-by Arnd Bergmann <[email protected]>--- fs/hfsplus/hfsplus_fs.h | 7 ++++--- fs/hfsplus/inode.c | 12 ++++++------ 2 files changed, 10 insertions(+), 9 deletions(-)fs/hfsplus/hfsplus_fs.h +0
-0
fs/hfsplus/hfsplus_fs.h int hfsplus_submit_bio(struct super_block *sb, sector_t sector, void *buf, · line 534
Quoted 6 lines
fs/hfsplus/hfsplus_fs.h +2
-1@@ -530,13 +530,14 @@ int hfsplus_submit_bio(struct super_block *sb, sector_t sector, void *buf, void **data, int op, int op_flags);int hfsplus_read_wrapper (struct super_block *sb); -/* time macros */ +/* time macros: convert between 1904-2040 and 1970-2106 range, +* pre-1970 timestamps are interpreted as post-2038 times after wrap-around
^^^^
If we are talking about on-disk timestamps, this should be 2040.
Everything else looks fine, if it helps you can use:
Quoted 45 lines
fs/hfsplus/hfsplus_fs.h +2
-2*/ #define __hfsp_mt2ut (t) (be32_to_cpu(t) - 2082844800U)#define __hfsp_ut2mt (t) (cpu_to_be32(t + 2082844800U))/* compatibility */ -#define hfsp_mt2ut (t) (struct timespec){ .tv_sec = __hfsp_mt2ut(t) } +#define hfsp_mt2ut (t) (struct timespec64){ .tv_sec = __hfsp_mt2ut(t) }#define hfsp_ut2mt (t) __hfsp_ut2mt((t).tv_sec) -#define hfsp_now2mt () __hfsp_ut2mt(get_seconds()) +#define hfsp_now2mt () __hfsp_ut2mt(ktime_get_real_seconds())#endif
fs/hfsplus/inode.c +6
-6@@ -493,9 +493,9 @@ int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)hfsplus_get_perms (inode, &folder->permissions,1 );set_nlink (inode,1 ); inode->i_size =2 +be32_to_cpu (folder->valence); - inode->i_atime =timespec_to_timespec64 (hfsp_mt2ut (folder->access_date)); - inode->i_mtime =timespec_to_timespec64 (hfsp_mt2ut (folder->content_mod_date)); - inode->i_ctime =timespec_to_timespec64 (hfsp_mt2ut (folder->attribute_mod_date)); + inode->i_atime =hfsp_mt2ut (folder->access_date); + inode->i_mtime =hfsp_mt2ut (folder->content_mod_date); + inode->i_ctime =hfsp_mt2ut (folder->attribute_mod_date);HFSPLUS_I (inode)->create_date = folder->create_date;HFSPLUS_I (inode)->fs_blocks =0 ;if (folder->flags &cpu_to_be16 (HFSPLUS_HAS_FOLDER_COUNT )) {@@ -531,9 +531,9 @@ int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)init_special_inode (inode, inode->i_mode,be32_to_cpu (file->permissions.dev)); } - inode->i_atime =timespec_to_timespec64 (hfsp_mt2ut (file->access_date)); - inode->i_mtime =timespec_to_timespec64 (hfsp_mt2ut (file->content_mod_date)); - inode->i_ctime =timespec_to_timespec64 (hfsp_mt2ut (file->attribute_mod_date)); + inode->i_atime =hfsp_mt2ut (file->access_date); + inode->i_mtime =hfsp_mt2ut (file->content_mod_date); + inode->i_ctime =hfsp_mt2ut (file->attribute_mod_date);HFSPLUS_I (inode)->create_date = file->create_date; }else {pr_err ("bad catalog entry used to create inode\n" );Signature · 1 line
2.9.0
PATCH 3/3 hfsplus: return inode birthtime for statx
In reply to Arnd BergmannWe have the data in the kernel, so we might just as well provide it to user space.
--- fs/hfsplus/dir.c | 1 + fs/hfsplus/hfsplus_fs.h | 2 ++ fs/hfsplus/inode.c | 14 ++++++++++++++ 3 files changed, 17 insertions(+)
fs/hfsplus/dir.c +1 -0
@@ -566,6 +566,7 @@ const struct inode_operations hfsplus_dir_inode_operations = { .symlink = hfsplus_symlink, .mknod = hfsplus_mknod, .rename = hfsplus_rename, + .getattr = hfsplus_getattr, .listxattr = hfsplus_listxattr, #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL .get_acl = hfsplus_get_posix_acl,
fs/hfsplus/hfsplus_fs.h +2 -0
@@ -489,6 +489,8 @@ int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd);int hfsplus_cat_write_inode (struct inode *inode);int hfsplus_file_fsync (struct file *file,loff_t start,loff_t end,int datasync); +int hfsplus_getattr (const struct path *path,struct kstat *stat, +u32 request_mask,unsigned int query_flags);/* ioctl.c */ long hfsplus_ioctl (struct file *filp,unsigned int cmd,unsigned long arg);
fs/hfsplus/inode.c +14 -0
@@ -276,6 +276,19 @@ static int hfsplus_setattr(struct dentry *dentry, struct iattr *attr)return 0 ; } +int hfsplus_getattr (const struct path *path,struct kstat *stat, +u32 request_mask,unsigned int query_flags) +{ +struct inode *inode =d_backing_inode (path->dentry); + +generic_fillattr (inode, stat); + + stat->btime =hfsp_mt2ut (HFSPLUS_I (inode)->create_date); + stat->result_mask |=STATX_BTIME ; + +return 0 ; +} +int hfsplus_file_fsync (struct file *file,loff_t start,loff_t end,int datasync) {
@@ -335,6 +348,7 @@ int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,static const struct inode_operations hfsplus_file_inode_operations = { .setattr = hfsplus_setattr, + .getattr = hfsplus_getattr, .listxattr = hfsplus_listxattr, #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL .get_acl = hfsplus_get_posix_acl,
Signature · 1 line
2.9.0
Re: [PATCH 3/3] hfsplus: return inode birthtime for statx
Hi:
Quoted 40 lines · Arnd Bergmann
We have the data in the kernel, so we might just as well provide it to user space.
Signed-off-by Arnd Bergmann <[email protected]>--- fs/hfsplus/dir.c | 1 + fs/hfsplus/hfsplus_fs.h | 2 ++ fs/hfsplus/inode.c | 14 ++++++++++++++ 3 files changed, 17 insertions(+)fs/hfsplus/dir.c +1
-0@@ -566,6 +566,7 @@ const struct inode_operations hfsplus_dir_inode_operations = { .symlink = hfsplus_symlink, .mknod = hfsplus_mknod, .rename = hfsplus_rename, + .getattr = hfsplus_getattr, .listxattr = hfsplus_listxattr, #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL .get_acl = hfsplus_get_posix_acl,fs/hfsplus/hfsplus_fs.h +2
-0@@ -489,6 +489,8 @@ int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd);int hfsplus_cat_write_inode (struct inode *inode);int hfsplus_file_fsync (struct file *file,loff_t start,loff_t end,int datasync); +int hfsplus_getattr (const struct path *path,struct kstat *stat, +u32 request_mask,unsigned int query_flags);/* ioctl.c */ long hfsplus_ioctl (struct file *filp,unsigned int cmd,unsigned long arg);fs/hfsplus/inode.c +0
-0
fs/hfsplus/inode.c static int hfsplus_setattr(struct dentry *dentry, struct iattr *attr) · line 282
Quoted 7 lines
fs/hfsplus/inode.c +4
-0@@ -276,6 +276,19 @@ static int hfsplus_setattr(struct dentry *dentry, struct iattr *attr)return 0 ; } +int hfsplus_getattr (const struct path *path,struct kstat *stat, +u32 request_mask,unsigned int query_flags) +{ +struct inode *inode =d_backing_inode (path->dentry);
I think d_inode() is better. They work the same, but "normal filesystems should not use this", according to the d_backing_inode() documentation.
Quoted 23 lines
fs/hfsplus/inode.c +10
-0+ +generic_fillattr (inode, stat); + + stat->btime =hfsp_mt2ut (HFSPLUS_I (inode)->create_date); + stat->result_mask |=STATX_BTIME ; + +return 0 ; +} +int hfsplus_file_fsync (struct file *file,loff_t start,loff_t end,int datasync) {@@ -335,6 +348,7 @@ int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,static const struct inode_operations hfsplus_file_inode_operations = { .setattr = hfsplus_setattr, + .getattr = hfsplus_getattr, .listxattr = hfsplus_listxattr, #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL .get_acl = hfsplus_get_posix_acl,Signature · 1 line
2.9.0
What about symlinks and special files?
Thanks,
Ernest
Re: [PATCH 3/3] hfsplus: return inode birthtime for statx
Quoted 6 lines · Ernesto A. Fernández
Quoted 6 lines
fs/hfsplus/inode.c +0
-0@@ -276,6 +276,19 @@ static int hfsplus_setattr(struct dentry *dentry, struct iattr *attr)return 0 ; }
Quoted 8 lines
fs/hfsplus/inode.c static int hfsplus_setattr(struct dentry *dentry, struct iattr *attr) · line 282
Quoted 4 lines
fs/hfsplus/inode.c +4
-0+int hfsplus_getattr (const struct path *path,struct kstat *stat, +u32 request_mask,unsigned int query_flags) +{ +struct inode *inode =d_backing_inode (path->dentry);I think
d_inode()is better. They work the same, but "normal filesystems should not use this", according to thed_backing_inode()documentation.
Right, definitely. I copied it from vfs_getattr_nosec() without thinking about it much. I see how David Howells put that d_backing_inode() there, but still don't understand it.
Quoted 13 lines
Quoted 11 lines
fs/hfsplus/inode.c +1
-1@@ -335,6 +348,7 @@ int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,static const struct inode_operations hfsplus_file_inode_operations = { .setattr = hfsplus_setattr, + .getattr = hfsplus_getattr, .listxattr = hfsplus_listxattr, #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL .get_acl = hfsplus_get_posix_acl, --2.9.0
What about symlinks and special files?
My mistake again, thanks for pointing that out. Doing the symlinks correctly here would actually add a bit more complexity as they use the generic page_symlink_inode_operations at the moment.
I think I'd rather just retract this patch and let someone else handle it if they actually want this feature. I only added it because it seemed trivial to do, but that was clearly not true. ;-)
Arnd
Viacheslav Dubeyko
maintainerQuoted 8 lines · Arnd Bergmann
The native HFS timestamps overflow in year 2040, two years after the Unix
y2038 overflow. However, the way that the conversion between on-disk timestamps and in-kernel timestamps was implemented, 64-bit machines actually ended up converting negative UTC timestamps (1902 through 1969)
into times between 2038 and 2106.
Quoted 7 lines
Rather than making all machines faithfully represent timestamps in the
ancient past but break after 2040, this changes the file system to always use the unsigned UTC interpretation, reading back times between
1970 and 2106.
The trouble with HFS and HFS+ that the specification [1] declares this:
"HFS Plus stores dates in several data structures, including the volume header and catalog records. These dates are stored in unsigned 32-bit integers (UInt32) containing the number of seconds since midnight, January 1, 1904, GMT. This is slightly different from HFS, where the value represents local time. The maximum representable date is February 6, 2040 at 06:28:15 GMT."
So, I am not sure that we are able to support later dates because such timestamps cannot be stored on HFS/HFS+ volumes and will be incompatible with Mac OS X. Also, I am not sure that anybody will use HFS/HFS+ after 2040.
Thanks,
Vyacheslav Dubeyko.
[1] https://developer.apple.com/library/archive/technotes/tn/tn1150.html
Quoted 68 lines
Signed-off-by Arnd Bergmann <[email protected]>---
fs/hfs/hfs_fs.h | 6 ++++--
fs/hfs/inode.c | 4 ++--2 files changed, 6 insertions(+), 4 deletions(-)fs/hfs/hfs_fs.h +0
-0@@ -245,6 +245,8 @@ extern void hfs_mark_mdb_dirty(struct super_block *sb); * Unix: unsigned lil-endian since 00:00 GMT, Jan. 1,1970
* mac: unsigned big-endian since 00:00 GMT, Jan. 1,
1904
*
+ * We treat all timestamps before 1970 as times after 2038, so this
+ * actually works until year 2106
*/
#define__hfs_u_to_mtime(sec)cpu_to_be32(sec + 2082844800U -
sys_tz.tz_minuteswest * 60)
#define__hfs_m_to_utime(sec)(be32_to_cpu(sec)-
2082844800U + sys_tz.tz_minuteswest * 60)fs/hfs/hfs_fs.h +0
-0@@ -252,9 +254,9 @@ extern void hfs_mark_mdb_dirty(struct super_block *sb); #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)
-#definehfs_m_to_utime(time)(struct timespec){ .tv_sec =__hfs_m_to_utime(time)}
+#definehfs_m_to_utime(time)(struct timespec64){ .tv_sec =__hfs_m_to_utime(time)}
#definehfs_u_to_mtime(time)__hfs_u_to_mtime((time).tv_sec)fs/hfs/hfs_fs.h +1
-1-#define hfs_mtime () __hfs_u_to_mtime(get_seconds()) +#define hfs_mtime () __hfs_u_to_mtime(ktime_get_real_s
econds())
static inline const char *hfs_mdb_name(structsuper_block*sb) {fs/hfs/inode.c +0
-0@@ -351,7 +351,7 @@ static int hfs_read_inode(struct inode *inode, void *data) inode->i_mode &= ~hsb->s_file_umask; inode->i_mode |= S_IFREG;
inode->i_ctime=inode->i_atime=inode->i_mtime=fs/hfs/inode.c +0
-1- timespec_to_timespec64(hfs_m_to_utim
e(rec->file.MdDat));fs/hfs/inode.c +1
-0+hfs_m_to_utime (rec->file.MdDat);
inode->i_op=&hfs_file_inode_operations;inode->i_fop=&hfs_file_operations;inode->i_mapping->a_ops=&hfs_aops;fs/hfs/inode.c +0
-0@@ -362,7 +362,7 @@ static int hfs_read_inode(struct inode *inode, void *data) HFS_I(inode)->fs_blocks = 0;
inode->i_mode=S_IFDIR| (S_IRWXUGO& ~hsb-Quoted 1 line
s_dir_umask);
inode->i_ctime=inode->i_atime=inode->i_mtime=fs/hfs/inode.c +0
-1- timespec_to_timespec64(hfs_m_to_utim
e(rec->dir.MdDat));fs/hfs/inode.c +1
-0+hfs_m_to_utime (rec->dir.MdDat);
inode->i_op=&hfs_dir_inode_operations;inode->i_fop=&hfs_dir_operations; break;
Quoted 26 lines · Viacheslav Dubeyko
Quoted 8 lines · Arnd Bergmann
The native HFS timestamps overflow in year 2040, two years after the Unix
y2038 overflow. However, the way that the conversion between on-disk timestamps and in-kernel timestamps was implemented, 64-bit machines actually ended up converting negative UTC timestamps (1902 through 1969)
into times between 2038 and 2106.Quoted 7 lines
Rather than making all machines faithfully represent timestamps in the
ancient past but break after 2040, this changes the file system to always use the unsigned UTC interpretation, reading back times between
1970 and 2106.The trouble with HFS and HFS+ that the specification [1] declares this:
"HFS Plus stores dates in several data structures, including the volume header and catalog records. These dates are stored in unsigned 32-bit integers (UInt32) containing the number of seconds since midnight, January 1, 1904, GMT. This is slightly different from HFS, where the value represents local time. The maximum representable date is February 6, 2040 at 06:28:15 GMT."
Quoted 3 lines
So, I am not sure that we are able to support later dates because such timestamps cannot be stored on HFS/HFS+ volumes and will be incompatible with Mac OS X.
We never followed that interpretation in Linux though. As I wrote, on 64-bit machines, these two calculations (hfs and hfs+, respectively)
#define __hfs_m_to_utime(sec) (be32_to_cpu(sec) - 2082844800U +
sys_tz.tz_minuteswest * 60)
#define __hfsp_mt2ut(t) (be32_to_cpu(t) - 2082844800U)
just wrap around when reading the timestamps before 1970 from disk. On 32-bit machines they get wrapped another time when we assign them to a signed 32-bit time_t.
Quoted 1 line
Also, I am not sure that anybody will use HFS/HFS+ after 2040.
I'm trying to fix all file systems to be unambiguous regarding inode timestamps. This means it should behave the same way on 32-bit and 64-bit kernels, and if possible in a sane way.
Even if you don't care about running HFS in the future, you can trivially create files with arbitrary timestamps, just try
touch -d "Jan 1 1901" 1901
touch -d "Jan 1 1905" 1905
touch -d "Jan 1 1969" 1969
touch -d "Jan 1 2038" 2038
touch -d "Jan 1 2040" 2040
touch -d "Jan 1 2106" 2106
touch -d "Jan 1 2107" 2107
on HFS and do an 'ls -l' after an unmount/remount.
If you think it's important that we change the current behavior to be compatible with MacOS and represent the 1904..2040 time range rather than 1970..2106, we can definitely do that as well, using this patch:
fs/hfs/hfs_fs.h +0 -0
@@ -249,7 +249,7 @@ extern void hfs_mark_mdb_dirty(struct super_block *sb);* actually works until year 2106 */ #define __hfs_u_to_mtime (sec) cpu_to_be32(sec + 2082844800U -
sys_tz.tz_minuteswest * 60)
-#define __hfs_m_to_utime(sec) (be32_to_cpu(sec) - 2082844800U + sys_tz.tz_minuteswest * 60)
+#define __hfs_m_to_utime(sec) ((time64_t)be32_to_cpu(sec) - 2082844800U + sys_tz.tz_minuteswest * 60)
#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)
fs/hfsplus/hfsplus_fs.h +1 -1
@@ -534,7 +534,7 @@ int hfsplus_read_wrapper(struct super_block *sb);/* time macros: convert between 1904-2040 and 1970-2106 range, * pre-1970 timestamps are interpreted as post-2038 times after wrap-around */ -#define __hfsp_mt2ut (t) (be32_to_cpu(t) - 2082844800U) +#define __hfsp_mt2ut (t) ((time64_t)be32_to_cpu(t) - 2082844800U)#define __hfsp_ut2mt (t) (cpu_to_be32(t + 2082844800U))/* compatibility */
I can submit that separately so that it can get backported into stable kernels if you like, with the type changes as a follow-up on top.
Arnd
Viacheslav Dubeyko
maintainerQuoted 48 lines · Arnd Bergmann
Quoted 38 lines · Viacheslav Dubeyko
Quoted 13 lines · Arnd Bergmann
The native HFS timestamps overflow in year 2040, two years after the
Unix
y2038 overflow. However, the way that the conversion between on- disk
timestamps and in-kernel timestamps was implemented, 64-bit machines
actually ended up converting negative UTC timestamps (1902 through
1969)
into times between 2038 and 2106.Quoted 9 lines
Rather than making all machines faithfully represent timestamps in
the
ancient past but break after 2040, this changes the file system to
always use the unsigned UTC interpretation, reading back times between
1970 and 2106.The trouble with HFS and HFS+ that the specification [1] declares this:
"HFS Plus stores dates in several data structures, including the volume
header and catalog records. These dates are stored in unsigned 32- bit
integers (UInt32) containing the number of seconds since midnight, January 1, 1904, GMT. This is slightly different from HFS, where the
value represents local time. The maximum representable date is February
6, 2040 at 06:28:15 GMT."Quoted 4 lines
So, I am not sure that we are able to support later dates because such
timestamps cannot be stored on HFS/HFS+ volumes and will be incompatible with Mac OS X.We never followed that interpretation in Linux though. As I wrote, on 64-bit machines, these two calculations (hfs and hfs+, respectively)
Quoted 8 lines
#define __hfs_m_to_utime(sec) (be32_to_cpu(sec) - 2082844800U + sys_tz.tz_minuteswest * 60)
#define __hfsp_mt2ut(t) (be32_to_cpu(t) - 2082844800U)just wrap around when reading the timestamps before 1970 from disk. On 32-bit machines they get wrapped another time when we assign them to a signed 32-bit
time_t.
The whole patchset looks reasonable for me. I simply guess what the correct behaviour of HFS/HFS+ file system driver could look like for the case of achieving 2040 year. So, maybe the good way could be to mount in the READ-ONLY mode. What do you think?
Quoted 58 lines
Quoted 2 lines
Also, I am not sure that anybody will use HFS/HFS+ after 2040.
I'm trying to fix all file systems to be unambiguous regarding inode timestamps. This means it should behave the same way on 32-bit and 64-bit kernels, and if possible in a sane way.
Even if you don't care about running HFS in the future, you can trivially create files with arbitrary timestamps, just try
touch -d "Jan 1 1901" 1901
touch -d "Jan 1 1905" 1905
touch -d "Jan 1 1969" 1969
touch -d "Jan 1 2038" 2038
touch -d "Jan 1 2040" 2040
touch -d "Jan 1 2106" 2106
touch -d "Jan 1 2107" 2107on HFS and do an 'ls -l' after an unmount/remount.
If you think it's important that we change the current behavior to be compatible with MacOS and represent the 1904..2040 time range rather than 1970..2106, we can definitely do that as well, using this patch:
fs/hfs/hfs_fs.h +1
-1@@ -249,7 +249,7 @@ extern void hfs_mark_mdb_dirty(struct super_block*sb); * actually works until year 2106 */ #define __hfs_u_to_mtime(sec) cpu_to_be32(sec + 2082844800U - sys_tz.tz_minuteswest * 60) -#define __hfs_m_to_utime (sec) (be32_to_cpu(sec) - 2082844800U + sys_tz.tz_minuteswest * 60) +#define __hfs_m_to_utime (sec) ((time64_t)be32_to_cpu(sec) -2082844800U + sys_tz.tz_minuteswest * 60)
#define HFS_I(inode) (container_of(inode, struct
hfs_inode_info,vfs_inode))
#define HFS_SB(sb) ((structhfs_sb_info*)(sb)->s_fs_info)fs/hfsplus/hfsplus_fs.h +0
-0@@ -534,7 +534,7 @@ int hfsplus_read_wrapper(struct super_block *sb); /* time macros: convert between 1904-2040 and 1970-2106 range,* pre-1970 timestamps are interpreted as post-2038 times after wrap-around */
-#define __hfsp_mt2ut(t) (be32_to_cpu(t) - 2082844800U)
+#define __hfsp_mt2ut(t) ((time64_t)be32_to_cpu(t) - 2082844800U)
#define __hfsp_ut2mt(t) (cpu_to_be32(t + 2082844800U))
Quoted 6 lines
/* compatibility */
I can submit that separately so that it can get backported into stable kernels if you like, with the type changes as a follow-up on top.
Sounds good.
Thanks,
Vyacheslav Dubeyko.
Quoted 16 lines · Viacheslav Dubeyko
Quoted 6 lines · Arnd Bergmann
On Tue, Jun 19, 2018 at 7:03 PM, Viacheslav Dubeyko <[email protected]> wrote:
We never followed that interpretation in Linux though. As I wrote, on 64-bit machines, these two calculations (hfs and hfs+, respectively)
Quoted 8 lines
#define
__hfs_m_to_utime(sec)(be32_to_cpu(sec)- 2082844800U +
sys_tz.tz_minuteswest * 60)
#define__hfsp_mt2ut(t)(be32_to_cpu(t)- 2082844800U)just wrap around when reading the timestamps before 1970 from disk. On 32-bit machines they get wrapped another time when we assign them to a signed 32-bit
time_t.
Quoted 4 lines
The whole patchset looks reasonable for me. I simply guess what the correct behaviour of HFS/HFS+ file system driver could look like for the case of achieving 2040 year. So, maybe the good way could be to mount in the READ-ONLY mode. What do you think?
We've discussed doing that in VFS before, this is something we need to revisit, but I'd like to do it in common code rather than in every file system with a particular limit.
Deepa has a patch set to introduce minimum/maximum timestamps in the superblock for this. We definitely want to use that for limiting the range of utimensat() arguments from user space, and the idea we had discussed in the past was to have a way to enforce read-only mounting of file systems that cannot write current i_mtime values past a certain (user-defined) future date.
We actually need something like that soon, as there are some organizations that want to support super-long service lifetimes for Linux systems (e.g. cars, industrial machines, ...) and want an early-fail behavior to ensure that everything that works today can in principle keep working for the foreseeable future, while everything that is known to break can be forced to break already.
This is clearly not a priority for HFS in particular, but there is no reason for HFS to be different from ext3 here, which has a similar problem (timestamps are defined to range from 1902 to 2038).
Quoted 10 lines
Quoted 10 lines
/* time macros: convert between 1904-2040 and 1970-2106 range,
* pre-1970 timestamps are interpreted as post-2038 times after
wrap-around */
-#define__hfsp_mt2ut(t)(be32_to_cpu(t)-
2082844800U)
+#define__hfsp_mt2ut(t)((time64_t)be32_to_cpu(t) -
2082844800U)
#define__hfsp_ut2mt(t)(cpu_to_be32(t +
2082844800U))
Quoted 8 lines
Quoted 6 lines
/* compatibility */
I can submit that separately so that it can get backported into stable kernels if you like, with the type changes as a follow-up on top.
Sounds good.
Ok, I'll send an updated version with that patch first then.
Arnd
Quoted 16 lines · Arnd Bergmann
Quoted 15 lines · Viacheslav Dubeyko
Quoted 14 lines · Arnd Bergmann
On Tue, Jun 19, 2018 at 7:03 PM, Viacheslav Dubeyko <[email protected]> wrote:
/* time macros: convert between 1904-2040 and 1970-2106 range,
* pre-1970 timestamps are interpreted as post-2038 times after
wrap-around */
-#define__hfsp_mt2ut(t)(be32_to_cpu(t)-
2082844800U)
+#define__hfsp_mt2ut(t)((time64_t)be32_to_cpu(t) -
2082844800U)
#define__hfsp_ut2mt(t)(cpu_to_be32(t +
2082844800U))/* compatibility */
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
I can submit that separately so that it can get backported into stable kernels if you like, with the type changes as a follow-up on top.
Sounds good.
Ok, I'll send an updated version with that patch first then.
I've now sent that patch with additional information that I got from reading the XNU sources. Interestingly, that also uses the 1970-2106 time range that I had in my original series, not the 1904-2040 time range that is documented.
Arnd
If you decide to go with this series after all:
Quoted 22 lines · Arnd Bergmann
The native HFS timestamps overflow in year 2040, two years after the Unix y2038 overflow. However, the way that the conversion between on-disk timestamps and in-kernel timestamps was implemented, 64-bit machines actually ended up converting negative UTC timestamps (1902 through 1969) into times between 2038 and 2106.
Rather than making all machines faithfully represent timestamps in the ancient past but break after 2040, this changes the file system to always use the unsigned UTC interpretation, reading back times between 1970 and 2106.
Signed-off-by Arnd Bergmann <[email protected]>--- fs/hfs/hfs_fs.h | 6 ++++-- fs/hfs/inode.c | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-)fs/hfs/hfs_fs.h +0
-0
fs/hfs/hfs_fs.h extern void hfs_mark_mdb_dirty(struct super_block *sb); · line 248
Quoted 4 lines
fs/hfs/hfs_fs.h +1
-0@@ -245,6 +245,8 @@ extern void hfs_mark_mdb_dirty(struct super_block *sb);* Unix: unsigned lil-endian since 00:00 GMT, Jan. 1, 1970 * mac: unsigned big-endian since 00:00 GMT, Jan. 1, 1904 * +* We treat all timestamps before 1970 as times after 2038, so this
^^^^
Same as the hfsplus patch, I find this confusing. I think it should say 2040.
Everything else looks good.
Quoted 41 lines
fs/hfs/hfs_fs.h +3
-2+* actually works until year 2106 */ #define __hfs_u_to_mtime (sec) cpu_to_be32(sec + 2082844800U - sys_tz.tz_minuteswest * 60)#define __hfs_m_to_utime (sec) (be32_to_cpu(sec) - 2082844800U + sys_tz.tz_minuteswest * 60)@@ -252,9 +254,9 @@ extern void hfs_mark_mdb_dirty(struct super_block *sb);#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 timespec){ .tv_sec = __hfs_m_to_utime(time) } +#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(get_seconds()) +#define hfs_mtime () __hfs_u_to_mtime(ktime_get_real_seconds())static inline const char *hfs_mdb_name (struct super_block *sb) {fs/hfs/inode.c +2
-2@@ -351,7 +351,7 @@ static int hfs_read_inode(struct inode *inode, void *data) inode->i_mode &= ~hsb->s_file_umask; inode->i_mode |=S_IFREG ; inode->i_ctime = inode->i_atime = inode->i_mtime = -timespec_to_timespec64 (hfs_m_to_utime (rec->file.MdDat)); +hfs_m_to_utime (rec->file.MdDat); inode->i_op = &hfs_file_inode_operations; inode->i_fop = &hfs_file_operations; inode->i_mapping->a_ops = &hfs_aops;@@ -362,7 +362,7 @@ static int hfs_read_inode(struct inode *inode, void *data)HFS_I (inode)->fs_blocks =0 ; inode->i_mode =S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask); inode->i_ctime = inode->i_atime = inode->i_mtime = -timespec_to_timespec64 (hfs_m_to_utime (rec->dir.MdDat)); +hfs_m_to_utime (rec->dir.MdDat); inode->i_op = &hfs_dir_inode_operations; inode->i_fop = &hfs_dir_operations;break ;Signature · 1 line
2.9.0