fat: add functions to update and truncate timestamps appropriately
authorFrank Sorenson <sorenson@redhat.com>
Tue, 30 Oct 2018 22:06:53 +0000 (15:06 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 31 Oct 2018 15:54:14 +0000 (08:54 -0700)
Add the fat-specific inode_operation ->update_time() and
fat_truncate_time() function to truncate the inode timestamps from 1
nanosecond to the appropriate granularity.

Link: http://lkml.kernel.org/r/38af1ba3c3cf0d7381ce7b63077ef8af75901532.1538363961.git.sorenson@redhat.com
Signed-off-by: Frank Sorenson <sorenson@redhat.com>
Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/fat/fat.h
fs/fat/file.c
fs/fat/inode.c
fs/fat/misc.c
fs/fat/namei_msdos.c
fs/fat/namei_vfat.c

index 9d7d2d5da28bf9e6cfb614a267274612e8579247..4e1b2f6df5e6a63f7db7f5b92d96c443de915171 100644 (file)
@@ -416,6 +416,10 @@ 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);
index 4f3d72fb1e60d64a71f92e632da19ac4ac132ebc..19b6b05664115173175a45a22d09829efd26ae0a 100644 (file)
@@ -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,
 };
index d6b81e31f9f5d827be92ffad4cf240fa32e5f767..3bf60f19b6d35fd8ee68e197cdf7eb042235c906 100644 (file)
@@ -1626,6 +1626,11 @@ 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;
+       /*
+        * fat timestamps are 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);
index 2eca073fe785be390fc8d74e56df98d7b4c1a3d5..fce0a76f3f1e4c2617934a93c027d0af0d074b45 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 #include "fat.h"
+#include <linux/iversion.h>
 
 /*
  * fat_fs_error reports a file system problem that might indicate fa data
@@ -265,6 +266,80 @@ void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
 }
 EXPORT_SYMBOL_GPL(fat_time_unix2fat);
 
+static inline struct timespec64 fat_timespec64_trunc_2secs(struct timespec64 ts)
+{
+       return (struct timespec64){ ts.tv_sec & ~1ULL, 0 };
+}
+/*
+ * truncate the various times with appropriate granularity:
+ *   root inode:
+ *     all times always 0
+ *   all other inodes:
+ *     mtime - 2 seconds
+ *     ctime
+ *       msdos - 2 seconds
+ *       vfat  - 10 milliseconds
+ *     atime - 24 hours (00:00:00 in local timezone)
+ */
+int fat_truncate_time(struct inode *inode, struct timespec64 *now, int flags)
+{
+       struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
+       struct timespec64 ts;
+
+       if (inode->i_ino == MSDOS_ROOT_INO)
+               return 0;
+
+       if (now == NULL) {
+               now = &ts;
+               ts = current_time(inode);
+       }
+
+       if (flags & S_ATIME) {
+               /* to localtime */
+               time64_t seconds = now->tv_sec - fat_tz_offset(sbi);
+               s32 remainder;
+
+               div_s64_rem(seconds, SECS_PER_DAY, &remainder);
+               /* to day boundary, and back to unix time */
+               seconds = seconds + fat_tz_offset(sbi) - remainder;
+
+               inode->i_atime = (struct timespec64){ seconds, 0 };
+       }
+       if (flags & S_CTIME) {
+               if (sbi->options.isvfat)
+                       inode->i_ctime = timespec64_trunc(*now, 10000000);
+               else
+                       inode->i_ctime = fat_timespec64_trunc_2secs(*now);
+       }
+       if (flags & S_MTIME)
+               inode->i_mtime = fat_timespec64_trunc_2secs(*now);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(fat_truncate_time);
+
+int fat_update_time(struct inode *inode, struct timespec64 *now, int flags)
+{
+       int iflags = I_DIRTY_TIME;
+       bool dirty = false;
+
+       if (inode->i_ino == MSDOS_ROOT_INO)
+               return 0;
+
+       fat_truncate_time(inode, now, flags);
+       if (flags & S_VERSION)
+               dirty = inode_maybe_inc_iversion(inode, false);
+       if ((flags & (S_ATIME | S_CTIME | S_MTIME)) &&
+           !(inode->i_sb->s_flags & SB_LAZYTIME))
+               dirty = true;
+
+       if (dirty)
+               iflags |= I_DIRTY_SYNC;
+       __mark_inode_dirty(inode, iflags);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(fat_update_time);
+
 int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
 {
        int i, err = 0;
index efb8c40c9d27467d0d58d10dd5b1ba313c06d9d6..effbdd5fbf6e5a54787587acebe0e934787faf26 100644 (file)
@@ -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)
index 82cd1e69cbdf283322e8d7fa8566a22cca54fc0a..1daa57cf4bf35929d8cab8ab5ea10ee35556434b 100644 (file)
@@ -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)