fat: timestamp updates
Subsystems (2)
v3 cover fat: timestamp updates
vfat/msdos timestamps are stored on-disk with several different granularities, some of them lower resolution than timespec_trunc() can provide. In addition, 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.
These patches allow finer granularity for the timestamps where possible and add fat-specific ->update_time inode operation and fat_truncate_time functions to truncate each timestamp correctly, giving consistent times across remounts.
Frank Sorenson (4):
fat: set the s_time_gran for msdos or vfat mounts
fat: create function to calculate timezone offset
fat: add functions to update and truncate the timestamps appropriately
fat: change timestamp updates to fat_update_time or fat_truncate_time
fs/fat/dir.c | 2 +- fs/fat/fat.h | 14 ++++++++++ fs/fat/file.c | 18 ++++++++++--- fs/fat/inode.c | 6 ++--- fs/fat/misc.c | 74 +++++++++++++++++++++++++++++++++++++++++++++------- fs/fat/namei_msdos.c | 17 ++++++------ fs/fat/namei_vfat.c | 15 ++++++----- 7 files changed, 114 insertions(+), 32 deletions(-)
Signature · 1 line
2.13.6
PATCH v3 1/4 fat: set the s_time_gran for msdos or vfat mounts
For vfat, ctime granularity is 10ms; set the super_block's s_time_gran to 10ms for vfat and 1 second for msdos mounts.
--- fs/fat/inode.c | 1 + 1 file changed, 1 insertion(+)
fs/fat/inode.c +1 -0
@@ -1626,6 +1626,7 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat, sb->s_magic =MSDOS_SUPER_MAGIC ; sb->s_op = &fat_sops; sb->s_export_op = &fat_export_ops; + sb->s_time_gran = isvfat ?10000000 :1000000000 ;mutex_init (&sbi->nfs_build_inode_lock);ratelimit_state_init (&sbi->ratelimit,DEFAULT_RATELIMIT_INTERVAL ,DEFAULT_RATELIMIT_BURST );
Signature · 1 line
2.13.6
PATCH v3 2/4 fat: create function to calculate timezone offset
In reply to Frank SorensonAdd a function to calculate the timezone offset in minutes.
--- fs/fat/fat.h | 12 ++++++++++++ fs/fat/misc.c | 12 ++---------- 2 files changed, 14 insertions(+), 10 deletions(-)
fs/fat/fat.h +12 -0
@@ -412,6 +412,18 @@ void fat_msg(struct super_block *sb, const char *level, const char *fmt, ...); }while (0 ) extern int fat_clusters_flush(struct super_block *sb);extern int fat_chain_add (struct inode *inode,int new_dclus,int nr_cluster); + +#define SECS_PER_MIN 60 +#define SECS_PER_HOUR (60 * 60) +#define SECS_PER_DAY (SECS_PER_HOUR * 24) + +static inline int fat_tz_offset (struct msdos_sb_info *sbi) +{ +return (sbi->options.tz_set ? + - sbi->options.time_offset : + sys_tz.tz_minuteswest) *SECS_PER_MIN ; +} +extern void fat_time_fat2unix (struct msdos_sb_info *sbi,struct timespec64 *ts,__le16 __time,__le16 __date,u8 time_cs); extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
fs/fat/misc.c +2 -10
@@ -170,9 +170,6 @@ int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)* time: 5 - 10: min (0 - 59) * time: 11 - 15: hour (0 - 23) */ -#define SECS_PER_MIN 60 -#define SECS_PER_HOUR (60 * 60) -#define SECS_PER_DAY (SECS_PER_HOUR * 24)/* days between 1.1.70 and 1.1.80 (2 leap days) */ #define DAYS_DELTA (365 * 10 + 2)/* 120 (2100 - 1980) isn't leap year */
@@ -210,10 +207,7 @@ void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec64 *ts, + days_in_year[month] + day + DAYS_DELTA) * SECS_PER_DAY; -if (!sbi->options.tz_set) - second += sys_tz.tz_minuteswest *SECS_PER_MIN ; -else - second -= sbi->options.time_offset *SECS_PER_MIN ; + second +=fat_tz_offset (sbi);if (time_cs) { ts->tv_sec = second + (time_cs /100 );
@@ -229,9 +223,7 @@ void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts, __le16 *time, __le16 *date, u8 *time_cs) {struct tm tm; -time64_to_tm (ts->tv_sec, - (sbi->options.tz_set ? sbi->options.time_offset : - -sys_tz.tz_minuteswest) *SECS_PER_MIN , &tm); +time64_to_tm (ts->tv_sec, -fat_tz_offset (sbi), &tm);/* FAT can only support year between 1980 to 2107 */ if (tm.tm_year <1980 -1900 ) {
Signature · 1 line
2.13.6
PATCH v3 3/4 fat: add functions to update and truncate the timestamps appropriately
In reply to Frank SorensonAdd the fat-specific inode_operation ->update_time() and fat_truncate_time() function to truncate the inode timestamps with the appropriate granularity.
--- fs/fat/fat.h | 2 ++ fs/fat/file.c | 1 + fs/fat/misc.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/fat/namei_msdos.c | 1 + fs/fat/namei_vfat.c | 1 + 5 files changed, 67 insertions(+)
fs/fat/fat.h +2 -0
@@ -428,6 +428,8 @@ extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec64 *ts, __le16 __time, __le16 __date, u8 time_cs);extern void fat_time_unix2fat (struct msdos_sb_info *sbi,struct timespec64 *ts,__le16 *time,__le16 *date,u8 *time_cs); +extern int fat_truncate_time (struct inode *inode,struct timespec64 *now,int flags); +extern int fat_update_time (struct inode *inode,struct timespec64 *now,int flags);extern int fat_sync_bhs (struct buffer_head **bhs,int nr_bhs);int fat_cache_init (void );
fs/fat/file.c +1 -0
@@ -552,4 +552,5 @@ EXPORT_SYMBOL_GPL(fat_setattr);const struct inode_operations fat_file_inode_operations = { .setattr = fat_setattr, .getattr = fat_getattr, + .update_time = fat_update_time, };
fs/fat/misc.c +62 -0
68 lines not on this page · raw message
fs/fat/namei_msdos.c +1 -0
@@ -637,6 +637,7 @@ static const struct inode_operations msdos_dir_inode_operations = { .rename = msdos_rename, .setattr = fat_setattr, .getattr = fat_getattr, + .update_time = fat_update_time, };static void setup (struct super_block *sb)
fs/fat/namei_vfat.c +1 -0
@@ -1032,6 +1032,7 @@ static const struct inode_operations vfat_dir_inode_operations = { .rename = vfat_rename, .setattr = fat_setattr, .getattr = fat_getattr, + .update_time = fat_update_time, };static void setup (struct super_block *sb)
Signature · 1 line
2.13.6
PATCH v3 4/4 fat: change timestamp updates to fat_update_time or fat_truncate_time
In reply to Frank SorensonMake the inode timestamp updates use fat_update_time or fat_truncate_time, depending on whether to call mark_inode_dirty.
--- fs/fat/dir.c | 2 +- fs/fat/file.c | 17 ++++++++++++++--- fs/fat/inode.c | 5 ++--- fs/fat/namei_msdos.c | 16 ++++++++-------- fs/fat/namei_vfat.c | 14 +++++++------- 5 files changed, 32 insertions(+), 22 deletions(-)
fs/fat/dir.c +1 -1
@@ -1071,7 +1071,7 @@ int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo) } } - dir->i_mtime = dir->i_atime =current_time (dir); +fat_truncate_time (dir,NULL ,S_ATIME |S_MTIME );if (IS_DIRSYNC (dir)) (void )fat_sync_inode (dir);else
fs/fat/file.c +14 -3
@@ -227,8 +227,7 @@ static int fat_cont_expand(struct inode *inode, loff_t size)if (err) goto out; - inode->i_ctime = inode->i_mtime =current_time (inode); -mark_inode_dirty (inode); +fat_update_time (inode,NULL ,S_CTIME |S_MTIME );if (IS_SYNC (inode)) {int err2;
@@ -330,7 +329,7 @@ static int fat_free(struct inode *inode, int skip)MSDOS_I (inode)->i_logstart =0 ; }MSDOS_I (inode)->i_attrs |=ATTR_ARCH ; - inode->i_ctime = inode->i_mtime =current_time (inode); +fat_truncate_time (inode,NULL ,S_CTIME |S_MTIME );if (wait) { err =fat_sync_inode (inode);if (err) {
@@ -542,6 +541,18 @@ int fat_setattr(struct dentry *dentry, struct iattr *attr)up_write (&MSDOS_I (inode)->truncate_lock); } +/* +* setattr_copy can't truncate these appropriately, so we'll +* copy them ourselves +*/ +if (attr->ia_valid &ATTR_ATIME ) +fat_truncate_time (inode, &attr->ia_atime,S_ATIME ); +if (attr->ia_valid &ATTR_CTIME ) +fat_truncate_time (inode, &attr->ia_ctime,S_CTIME ); +if (attr->ia_valid &ATTR_MTIME ) +fat_truncate_time (inode, &attr->ia_mtime,S_MTIME ); + attr->ia_valid &= ~(ATTR_ATIME |ATTR_CTIME |ATTR_MTIME ); +setattr_copy (inode, attr);mark_inode_dirty (inode); out:
fs/fat/inode.c +2 -3
@@ -244,9 +244,8 @@ static int fat_write_end(struct file *file, struct address_space *mapping,if (err < len)fat_write_failed (mapping, pos + len);if (!(err <0 ) && !(MSDOS_I (inode)->i_attrs &ATTR_ARCH )) { - inode->i_mtime = inode->i_ctime =current_time (inode);MSDOS_I (inode)->i_attrs |=ATTR_ARCH ; -mark_inode_dirty (inode); +fat_update_time (inode,NULL ,S_CTIME |S_MTIME ); }return err; }
@@ -564,7 +563,7 @@ int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de) de->cdate, de->ctime_cs);fat_time_fat2unix (sbi, &inode->i_atime,0 , de->adate,0 ); }else - inode->i_ctime = inode->i_atime = inode->i_mtime; +fat_update_time (inode, &inode->i_mtime,S_ATIME |S_CTIME );return 0 ; }
fs/fat/namei_msdos.c +8 -8
@@ -250,7 +250,7 @@ static int msdos_add_entry(struct inode *dir, const unsigned char *name,if (err)return err; - dir->i_ctime = dir->i_mtime = *ts; +fat_truncate_time (dir, ts,S_CTIME |S_MTIME );if (IS_DIRSYNC (dir)) (void )fat_sync_inode (dir);else
@@ -294,7 +294,7 @@ static int msdos_create(struct inode *dir, struct dentry *dentry, umode_t mode, err =PTR_ERR (inode); goto out; } - inode->i_mtime = inode->i_atime = inode->i_ctime = ts; +fat_truncate_time (inode, &ts,S_ATIME |S_CTIME |S_MTIME );/* timestamp is already written, so mark_inode_dirty() is unneeded. */ d_instantiate (dentry, inode);
@@ -327,7 +327,7 @@ static int msdos_rmdir(struct inode *dir, struct dentry *dentry)drop_nlink (dir);clear_nlink (inode); - inode->i_ctime =current_time (inode); +fat_update_time (inode,NULL ,S_CTIME );fat_detach (inode); out:mutex_unlock (&MSDOS_SB (sb)->s_lock);
@@ -380,7 +380,7 @@ static int msdos_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) goto out; }set_nlink (inode,2 ); - inode->i_mtime = inode->i_atime = inode->i_ctime = ts; +fat_truncate_time (inode, &ts,S_ATIME |S_CTIME |S_MTIME );/* timestamp is already written, so mark_inode_dirty() is unneeded. */ d_instantiate (dentry, inode);
@@ -413,7 +413,7 @@ static int msdos_unlink(struct inode *dir, struct dentry *dentry)if (err) goto out;clear_nlink (inode); - inode->i_ctime =current_time (inode); +fat_update_time (inode,NULL ,S_CTIME );fat_detach (inode); out:mutex_unlock (&MSDOS_SB (sb)->s_lock);
@@ -478,7 +478,7 @@ static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name,mark_inode_dirty (old_inode);inode_inc_iversion (old_dir); - old_dir->i_ctime = old_dir->i_mtime =current_time (old_dir); +fat_truncate_time (old_dir,NULL ,S_CTIME |S_MTIME );if (IS_DIRSYNC (old_dir)) (void )fat_sync_inode (old_dir);else
@@ -538,7 +538,7 @@ static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name,if (err) goto error_dotdot;inode_inc_iversion (old_dir); - old_dir->i_ctime = old_dir->i_mtime = ts; +fat_truncate_time (old_dir, &ts,S_CTIME |S_MTIME );if (IS_DIRSYNC (old_dir)) (void )fat_sync_inode (old_dir);else
@@ -548,7 +548,7 @@ static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name,drop_nlink (new_inode);if (is_dir)drop_nlink (new_inode); - new_inode->i_ctime = ts; +fat_truncate_time (new_inode, &ts,S_CTIME ); } out:brelse (sinfo.bh);
fs/fat/namei_vfat.c +7 -7
@@ -678,7 +678,7 @@ static int vfat_add_entry(struct inode *dir, const struct qstr *qname, goto cleanup;/* update timestamp */ - dir->i_ctime = dir->i_mtime = dir->i_atime = *ts; +fat_truncate_time (dir, ts,S_CTIME |S_MTIME );if (IS_DIRSYNC (dir)) (void )fat_sync_inode (dir);else
@@ -779,7 +779,7 @@ static int vfat_create(struct inode *dir, struct dentry *dentry, umode_t mode, goto out; }inode_inc_iversion (inode); - inode->i_mtime = inode->i_atime = inode->i_ctime = ts; +fat_truncate_time (inode, &ts,S_ATIME |S_CTIME |S_MTIME );/* timestamp is already written, so mark_inode_dirty() is unneeded. */ d_instantiate (dentry, inode);
@@ -810,7 +810,7 @@ static int vfat_rmdir(struct inode *dir, struct dentry *dentry)drop_nlink (dir);clear_nlink (inode); - inode->i_mtime = inode->i_atime =current_time (inode); +fat_update_time (inode,NULL ,S_ATIME |S_MTIME );fat_detach (inode);vfat_d_version_set (dentry,inode_query_iversion (dir)); out:
@@ -836,7 +836,7 @@ static int vfat_unlink(struct inode *dir, struct dentry *dentry)if (err) goto out;clear_nlink (inode); - inode->i_mtime = inode->i_atime =current_time (inode); +fat_update_time (inode,NULL ,S_ATIME |S_MTIME );fat_detach (inode);vfat_d_version_set (dentry,inode_query_iversion (dir)); out:
@@ -876,7 +876,7 @@ static int vfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) }inode_inc_iversion (inode);set_nlink (inode,2 ); - inode->i_mtime = inode->i_atime = inode->i_ctime = ts; +fat_truncate_time (inode, &ts,S_ATIME |S_CTIME |S_MTIME );/* timestamp is already written, so mark_inode_dirty() is unneeded. */ d_instantiate (dentry, inode);
@@ -969,7 +969,7 @@ static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,if (err) goto error_dotdot;inode_inc_iversion (old_dir); - old_dir->i_ctime = old_dir->i_mtime = ts; +fat_truncate_time (old_dir, &ts,S_CTIME |S_MTIME );if (IS_DIRSYNC (old_dir)) (void )fat_sync_inode (old_dir);else
@@ -979,7 +979,7 @@ static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,drop_nlink (new_inode);if (is_dir)drop_nlink (new_inode); - new_inode->i_ctime = ts; +fat_truncate_time (new_inode, &ts,S_CTIME ); } out:brelse (sinfo.bh);
Signature · 1 line
2.13.6
OGAWA Hirofumi
maintainerQuoted 8 lines · Frank Sorenson
vfat/msdos timestamps are stored on-disk with several different granularities, some of them lower resolution than
timespec_trunc()can provide. In addition, 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 4 lines
These patches allow finer granularity for the timestamps where possible and add fat-specific ->update_time inode operation and
fat_truncate_timefunctions to truncate each timestamp correctly, giving consistent times across remounts.
Looks like basically very good, however there are some issues. Those small stuff would be easier to tell as patch, instead of commenting inline.
Please check the following patch and comment of it. Only noticeable one is atime bug (6 in comment), looks like it is only missed to test well, and need to re-test (in my quick test, looks works with this patch).
Other stuff are tweaks and optimizations basically.
Thanks.
[PATCH] fat: Fix/cleanup FAT timestamp works
1) Move SECS_PER_* family to misc.c, only used in misc.c
2) Don't use fat_update_time() to replace mark_inode_dirty(), mark_inode_dirty_sync() and mark_inode_dirty() are different.
3) Set sb->s_time_gran, we use this only for ctime in vfat mode. So nothing benefit to add additional overhead to get current time.
Instead, just truncate ctime manually.
4) Add i_version support to update_time(). FAT doesn't support i_version, but user still can set it.
5) Make sure to use 64bits wide to mask 2secs (i.e. ~1ULL, instead ~1)
6) Fix atime bug
- on 32bit arch, have to use div64 stuff
- missing to back to unix time after converting localtime
7) Remove needless noinline. gcc should be able to choice to inline it or not, without any issue.
---
fs/fat/fat.h | 12 ------------ fs/fat/file.c | 3 ++- fs/fat/inode.c | 11 ++++++++--- fs/fat/misc.c | 45 ++++++++++++++++++++++++++++++++++----------- fs/fat/namei_msdos.c | 4 ++-- fs/fat/namei_vfat.c | 4 ++-- 6 files changed, 48 insertions(+), 31 deletions(-)
fs/fat/fat.h renamed from fs/fat/fat.h~fat-timestamp-fix-tweaks +0 -12
@@ -412,18 +412,6 @@ void fat_msg(struct super_block *sb, con }while (0 ) extern int fat_clusters_flush(struct super_block *sb);extern int fat_chain_add (struct inode *inode,int new_dclus,int nr_cluster); - -#define SECS_PER_MIN 60 -#define SECS_PER_HOUR (60 * 60) -#define SECS_PER_DAY (SECS_PER_HOUR * 24) - -static inline int fat_tz_offset (struct msdos_sb_info *sbi) -{ -return (sbi->options.tz_set ? - - sbi->options.time_offset : - sys_tz.tz_minuteswest) *SECS_PER_MIN ; -} -extern void fat_time_fat2unix (struct msdos_sb_info *sbi,struct timespec64 *ts,__le16 __time,__le16 __date,u8 time_cs); extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
fs/fat/file.c renamed from fs/fat/file.c~fat-timestamp-fix-tweaks +2 -1
@@ -227,7 +227,8 @@ static int fat_cont_expand(struct inodeif (err) goto out; -fat_update_time (inode,NULL ,S_CTIME |S_MTIME ); +fat_truncate_time (inode,NULL ,S_CTIME |S_MTIME ); +mark_inode_dirty (inode);if (IS_SYNC (inode)) {int err2;
fs/fat/inode.c renamed from fs/fat/inode.c~fat-timestamp-fix-tweaks +8 -3
@@ -244,8 +244,9 @@ static int fat_write_end(struct file *fiif (err < len)fat_write_failed (mapping, pos + len);if (!(err <0 ) && !(MSDOS_I (inode)->i_attrs &ATTR_ARCH )) { +fat_truncate_time (inode,NULL ,S_CTIME |S_MTIME );MSDOS_I (inode)->i_attrs |=ATTR_ARCH ; -fat_update_time (inode,NULL ,S_CTIME |S_MTIME ); +mark_inode_dirty (inode); }return err; }
@@ -563,7 +564,7 @@ int fat_fill_inode(struct inode *inode, de->cdate, de->ctime_cs);fat_time_fat2unix (sbi, &inode->i_atime,0 , de->adate,0 ); }else -fat_update_time (inode, &inode->i_mtime,S_ATIME |S_CTIME ); +fat_truncate_time (inode, &inode->i_mtime,S_ATIME |S_CTIME );return 0 ; }
@@ -1625,7 +1626,11 @@ int fat_fill_super(struct super_block *s sb->s_magic =MSDOS_SUPER_MAGIC ; sb->s_op = &fat_sops; sb->s_export_op = &fat_export_ops; - sb->s_time_gran = isvfat ?10000000 :1000000000 ; +/* +* timestamp is complex and truncated by fat itself, so we set +* 1 here to be fast. +*/ + sb->s_time_gran =1 ;mutex_init (&sbi->nfs_build_inode_lock);ratelimit_state_init (&sbi->ratelimit,DEFAULT_RATELIMIT_INTERVAL ,DEFAULT_RATELIMIT_BURST );
fs/fat/misc.c renamed from fs/fat/misc.c~fat-timestamp-fix-tweaks +34 -11
102 lines not on this page · raw message
fs/fat/namei_msdos.c renamed from fs/fat/namei_msdos.c~fat-timestamp-fix-tweaks +2 -2
@@ -327,7 +327,7 @@ static int msdos_rmdir(struct inode *dirdrop_nlink (dir);clear_nlink (inode); -fat_update_time (inode,NULL ,S_CTIME ); +fat_truncate_time (inode,NULL ,S_CTIME );fat_detach (inode); out:mutex_unlock (&MSDOS_SB (sb)->s_lock);
@@ -413,7 +413,7 @@ static int msdos_unlink(struct inode *diif (err) goto out;clear_nlink (inode); -fat_update_time (inode,NULL ,S_CTIME ); +fat_truncate_time (inode,NULL ,S_CTIME );fat_detach (inode); out:mutex_unlock (&MSDOS_SB (sb)->s_lock);
fs/fat/namei_vfat.c renamed from fs/fat/namei_vfat.c~fat-timestamp-fix-tweaks +2 -2
@@ -810,7 +810,7 @@ static int vfat_rmdir(struct inode *dir,drop_nlink (dir);clear_nlink (inode); -fat_update_time (inode,NULL ,S_ATIME |S_MTIME ); +fat_truncate_time (inode,NULL ,S_ATIME |S_MTIME );fat_detach (inode);vfat_d_version_set (dentry,inode_query_iversion (dir)); out:
@@ -836,7 +836,7 @@ static int vfat_unlink(struct inode *dirif (err) goto out;clear_nlink (inode); -fat_update_time (inode,NULL ,S_ATIME |S_MTIME ); +fat_truncate_time (inode,NULL ,S_ATIME |S_MTIME );fat_detach (inode);vfat_d_version_set (dentry,inode_query_iversion (dir)); out:
_
Signature · 1 line
OGAWA Hirofumi <[email protected]>