Skip to contentSkip to conversation

fat: truncate timestamps returned from fat_getattr

RFC PATCH In review Frank Sorenson 2 participants 3 messages Mar 26, 2018 – Apr 2, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 fat: truncate timestamps returned from fat_getattr

Author: Frank Sorenson <[email protected]>
Date:   2018-03-23 23:28:32 -0500

fat: add function to truncate timestamps returned from fat_getattr

vfat/msdos timestamps are stored on-disk with several different granularities, some of them lower resolution than timespec_trunc() can provide. However, they are only truncated as they are written to disk, so the timestamps in-memory for new or modified files/directories may be different from the same timestamps after a remount, as the now-truncated times are re-read from the on-disk format.

This patch adds a function to adjust the timestamps returned from fat_getattr() so that userspace sees consistent times across a remount. The existing functions are used to convert the timespec to the on-disk representation, then back to timespec again.

fs/fat/fat.h +1 -0
index 8fc1093da47d..0367be021b23 100644
@@ -410,6 +410,7 @@ extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,
 			      __le16 __time, __le16 __date, u8 time_cs);
 extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
 			      __le16 *time, __le16 *date, u8 *time_cs);
+extern void fat_trunc_kstat_times(struct msdos_sb_info *sbi, struct kstat *stat);
 extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
 
 int fat_cache_init(void);
fs/fat/file.c +1 -0
index 4724cc9ad650..11639e393ef7 100644
@@ -370,6 +370,7 @@ int fat_getattr(const struct path *path, struct kstat *stat,
 {
 	struct inode *inode = d_inode(path->dentry);
 	generic_fillattr(inode, stat);
+	fat_trunc_kstat_times(MSDOS_SB(inode->i_sb), stat);
 	stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
 
 	if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
fs/fat/misc.c +23 -0
index f9bdc1e01c98..52f40edefd12 100644
@@ -262,6 +262,29 @@ void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
 }
 EXPORT_SYMBOL_GPL(fat_time_unix2fat);
 
+void fat_trunc_kstat_times(struct msdos_sb_info *sbi, struct kstat *stat)
+{
+	if (stat->ino == MSDOS_ROOT_INO)
+		stat->mtime = stat->ctime = stat->atime =
+			(struct timespec){0, 0};
+	else {
+		__le16 time;
+		__le16 date;
+		u8 ctime_cs;
+
+		fat_time_unix2fat(sbi, &stat->mtime, &time, &date, NULL);
+		fat_time_fat2unix(sbi, &stat->mtime, time, date, 0);
+
+		fat_time_unix2fat(sbi, &stat->ctime, &time, &date,
+			sbi->options.isvfat ? &ctime_cs :  NULL);
+		fat_time_fat2unix(sbi, &stat->ctime, time, date,
+			sbi->options.isvfat ? ctime_cs : 0);
+
+		fat_time_unix2fat(sbi, &stat->atime, &time, &date, NULL);
+		fat_time_fat2unix(sbi, &stat->atime, 0, date, 0);
+	}
+}
+
 int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
 {
 	int i, err = 0;
maintainer
#1 lore raw

Frank Sorenson <[email protected]> writes:

Hi,

Quoted 13 lines
Author: Frank Sorenson <[email protected]>
Date:   2018-03-23 23:28:32 -0500

fat: add function to truncate timestamps returned from fat_getattr

vfat/msdos timestamps are stored on-disk with several different granularities, some of them lower resolution than timespec_trunc() can provide. However, they are only truncated as they are written to disk, so the timestamps in-memory for new or modified files/directories may be different from the same timestamps after a remount, as the now-truncated times are re-read from the on-disk format.

Quoted 6 lines

This patch adds a function to adjust the timestamps returned from fat_getattr() so that userspace sees consistent times across a remount. The existing functions are used to convert the timespec to the on-disk representation, then back to timespec again.

Already tried to use ->update_time() callback? With quick look though, it seems to be possible to control timestamp granularity with ->update_time(), and add fat_current_time() or such to get proper timestamp for FAT.

I think we should give consistent in-core inode timestamp too. For example, overlayfs may be copy timestamps from in-core inode, etc., then this stat workaround will not work.

Thanks.

Quoted 58 lines
fs/fat/fat.h +1 -0
index 8fc1093da47d..0367be021b23 100644
@@ -410,6 +410,7 @@ extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,
 			      __le16 __time, __le16 __date, u8 time_cs);
 extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
 			      __le16 *time, __le16 *date, u8 *time_cs);
+extern void fat_trunc_kstat_times(struct msdos_sb_info *sbi, struct kstat *stat);
 extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
 
 int fat_cache_init(void);
fs/fat/file.c +1 -0
index 4724cc9ad650..11639e393ef7 100644
@@ -370,6 +370,7 @@ int fat_getattr(const struct path *path, struct kstat *stat,
 {
 	struct inode *inode = d_inode(path->dentry);
 	generic_fillattr(inode, stat);
+	fat_trunc_kstat_times(MSDOS_SB(inode->i_sb), stat);
 	stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
 
 	if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
fs/fat/misc.c +23 -0
index f9bdc1e01c98..52f40edefd12 100644
@@ -262,6 +262,29 @@ void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
 }
 EXPORT_SYMBOL_GPL(fat_time_unix2fat);
 
+void fat_trunc_kstat_times(struct msdos_sb_info *sbi, struct kstat *stat)
+{
+	if (stat->ino == MSDOS_ROOT_INO)
+		stat->mtime = stat->ctime = stat->atime =
+			(struct timespec){0, 0};
+	else {
+		__le16 time;
+		__le16 date;
+		u8 ctime_cs;
+
+		fat_time_unix2fat(sbi, &stat->mtime, &time, &date, NULL);
+		fat_time_fat2unix(sbi, &stat->mtime, time, date, 0);
+
+		fat_time_unix2fat(sbi, &stat->ctime, &time, &date,
+			sbi->options.isvfat ? &ctime_cs :  NULL);
+		fat_time_fat2unix(sbi, &stat->ctime, time, date,
+			sbi->options.isvfat ? ctime_cs : 0);
+
+		fat_time_unix2fat(sbi, &stat->atime, &time, &date, NULL);
+		fat_time_fat2unix(sbi, &stat->atime, 0, date, 0);
+	}
+}
+
 int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
 {
 	int i, err = 0;
Signature · 1 line
OGAWA Hirofumi <[email protected]>
#2 lore raw
Quoted 10 lines · OGAWA Hirofumi

Frank Sorenson <[email protected]> writes:

Already tried to use ->update_time() callback? With quick look though, it seems to be possible to control timestamp granularity with ->update_time(), and add fat_current_time() or such to get proper timestamp for FAT.

I think we should give consistent in-core inode timestamp too. For example, overlayfs may be copy timestamps from in-core inode, etc., then this stat workaround will not work.

I'll see what I can do with update_time. Thanks.

Frank