[PATCH 2/2] f2fs: fix to update mtime correctly

From: Chao Yu
Date: Wed May 30 2018 - 06:12:04 EST


If we change system time to the past, get_mtime() will return a
overflowed time, and SIT_I(sbi)->max_mtime will be udpated
incorrectly, this patch fixes the two issues.

Signed-off-by: Chao Yu <yuchao0@xxxxxxxxxx>
---
fs/f2fs/segment.c | 3 ++-
fs/f2fs/segment.h | 14 +++++++++++---
2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 8ef87f6e0fb2..91af3e8c9065 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -1821,7 +1821,8 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)

se->valid_blocks = new_vblocks;
se->mtime = get_mtime(sbi);
- SIT_I(sbi)->max_mtime = se->mtime;
+ if (se->mtime > SIT_I(sbi)->max_mtime)
+ SIT_I(sbi)->max_mtime = se->mtime;

/* Update valid block bitmap */
if (del > 0) {
diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h
index c574131ac7e1..fe4ea82b08aa 100644
--- a/fs/f2fs/segment.h
+++ b/fs/f2fs/segment.h
@@ -748,9 +748,17 @@ static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi)
{
struct sit_info *sit_i = SIT_I(sbi);
- time64_t now = ktime_get_real_seconds();
-
- return sit_i->elapsed_time + now - sit_i->mounted_time;
+ time64_t diff = 0, now = ktime_get_real_seconds();
+
+ if (now >= sit_i->mounted_time) {
+ diff = now - sit_i->mounted_time;
+ } else {
+ /* system time is set to the past */
+ diff = sit_i->mounted_time - now;
+ if (sit_i->elapsed_time >= diff)
+ return sit_i->elapsed_time - diff;
+ }
+ return sit_i->elapsed_time + diff;
}

static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
--
2.17.0.391.g1f1cddd558b5