Skip to content

Commit 6bb885e

Browse files
fsorensontorvalds
authored andcommitted
fat: add functions to update and truncate timestamps appropriately
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>
1 parent d9f4d94 commit 6bb885e

6 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎fs/fat/fat.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec64 *ts,
416416
__le16 __time, __le16 __date, u8 time_cs);
417417
extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
418418
__le16 *time, __le16 *date, u8 *time_cs);
419+
extern int fat_truncate_time(struct inode *inode, struct timespec64 *now,
420+
int flags);
421+
extern int fat_update_time(struct inode *inode, struct timespec64 *now,
422+
int flags);
419423
extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
420424

421425
int fat_cache_init(void);

‎fs/fat/file.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,4 +552,5 @@ EXPORT_SYMBOL_GPL(fat_setattr);
552552
const struct inode_operations fat_file_inode_operations = {
553553
.setattr = fat_setattr,
554554
.getattr = fat_getattr,
555+
.update_time = fat_update_time,
555556
};

‎fs/fat/inode.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,11 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
16261626
sb->s_magic = MSDOS_SUPER_MAGIC;
16271627
sb->s_op = &fat_sops;
16281628
sb->s_export_op = &fat_export_ops;
1629+
/*
1630+
* fat timestamps are complex and truncated by fat itself, so
1631+
* we set 1 here to be fast
1632+
*/
1633+
sb->s_time_gran = 1;
16291634
mutex_init(&sbi->nfs_build_inode_lock);
16301635
ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
16311636
DEFAULT_RATELIMIT_BURST);

‎fs/fat/misc.c‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include "fat.h"
10+
#include <linux/iversion.h>
1011

1112
/*
1213
* 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,
265266
}
266267
EXPORT_SYMBOL_GPL(fat_time_unix2fat);
267268

269+
static inline struct timespec64 fat_timespec64_trunc_2secs(struct timespec64 ts)
270+
{
271+
return (struct timespec64){ ts.tv_sec & ~1ULL, 0 };
272+
}
273+
/*
274+
* truncate the various times with appropriate granularity:
275+
* root inode:
276+
* all times always 0
277+
* all other inodes:
278+
* mtime - 2 seconds
279+
* ctime
280+
* msdos - 2 seconds
281+
* vfat - 10 milliseconds
282+
* atime - 24 hours (00:00:00 in local timezone)
283+
*/
284+
int fat_truncate_time(struct inode *inode, struct timespec64 *now, int flags)
285+
{
286+
struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
287+
struct timespec64 ts;
288+
289+
if (inode->i_ino == MSDOS_ROOT_INO)
290+
return 0;
291+
292+
if (now == NULL) {
293+
now = &ts;
294+
ts = current_time(inode);
295+
}
296+
297+
if (flags & S_ATIME) {
298+
/* to localtime */
299+
time64_t seconds = now->tv_sec - fat_tz_offset(sbi);
300+
s32 remainder;
301+
302+
div_s64_rem(seconds, SECS_PER_DAY, &remainder);
303+
/* to day boundary, and back to unix time */
304+
seconds = seconds + fat_tz_offset(sbi) - remainder;
305+
306+
inode->i_atime = (struct timespec64){ seconds, 0 };
307+
}
308+
if (flags & S_CTIME) {
309+
if (sbi->options.isvfat)
310+
inode->i_ctime = timespec64_trunc(*now, 10000000);
311+
else
312+
inode->i_ctime = fat_timespec64_trunc_2secs(*now);
313+
}
314+
if (flags & S_MTIME)
315+
inode->i_mtime = fat_timespec64_trunc_2secs(*now);
316+
317+
return 0;
318+
}
319+
EXPORT_SYMBOL_GPL(fat_truncate_time);
320+
321+
int fat_update_time(struct inode *inode, struct timespec64 *now, int flags)
322+
{
323+
int iflags = I_DIRTY_TIME;
324+
bool dirty = false;
325+
326+
if (inode->i_ino == MSDOS_ROOT_INO)
327+
return 0;
328+
329+
fat_truncate_time(inode, now, flags);
330+
if (flags & S_VERSION)
331+
dirty = inode_maybe_inc_iversion(inode, false);
332+
if ((flags & (S_ATIME | S_CTIME | S_MTIME)) &&
333+
!(inode->i_sb->s_flags & SB_LAZYTIME))
334+
dirty = true;
335+
336+
if (dirty)
337+
iflags |= I_DIRTY_SYNC;
338+
__mark_inode_dirty(inode, iflags);
339+
return 0;
340+
}
341+
EXPORT_SYMBOL_GPL(fat_update_time);
342+
268343
int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
269344
{
270345
int i, err = 0;

‎fs/fat/namei_msdos.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ static const struct inode_operations msdos_dir_inode_operations = {
637637
.rename = msdos_rename,
638638
.setattr = fat_setattr,
639639
.getattr = fat_getattr,
640+
.update_time = fat_update_time,
640641
};
641642

642643
static void setup(struct super_block *sb)

‎fs/fat/namei_vfat.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,7 @@ static const struct inode_operations vfat_dir_inode_operations = {
10321032
.rename = vfat_rename,
10331033
.setattr = fat_setattr,
10341034
.getattr = fat_getattr,
1035+
.update_time = fat_update_time,
10351036
};
10361037

10371038
static void setup(struct super_block *sb)

0 commit comments

Comments
 (0)