f2fs: fix to update mtime correctly
authorChao Yu <yuchao0@huawei.com>
Mon, 4 Jun 2018 15:20:17 +0000 (23:20 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Mon, 4 Jun 2018 21:31:11 +0000 (14:31 -0700)
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@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/checkpoint.c
fs/f2fs/segment.c
fs/f2fs/segment.h

index 8eb184c3de84d31ba1f97b8e17a2e3ea5fbe5982..40b39db317ea5c192c9cb0375bb3a101bd81f99f 100644 (file)
@@ -1233,7 +1233,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
         * modify checkpoint
         * version number is already updated
         */
-       ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
+       ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
        ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
        for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
                ckpt->cur_node_segno[i] =
index f0f9593185a74bac914b556b14303b5377af218f..6dbdf2c48fbae340506354e19cf580487c2fc798 100644 (file)
@@ -1822,8 +1822,9 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
                                (new_vblocks > sbi->blocks_per_seg)));
 
        se->valid_blocks = new_vblocks;
-       se->mtime = get_mtime(sbi);
-       SIT_I(sbi)->max_mtime = se->mtime;
+       se->mtime = get_mtime(sbi, false);
+       if (se->mtime > SIT_I(sbi)->max_mtime)
+               SIT_I(sbi)->max_mtime = se->mtime;
 
        /* Update valid block bitmap */
        if (del > 0) {
@@ -3884,7 +3885,7 @@ static void init_min_max_mtime(struct f2fs_sb_info *sbi)
                if (sit_i->min_mtime > mtime)
                        sit_i->min_mtime = mtime;
        }
-       sit_i->max_mtime = get_mtime(sbi);
+       sit_i->max_mtime = get_mtime(sbi, false);
        up_write(&sit_i->sentry_lock);
 }
 
index c574131ac7e1ed72335a797f7c7ec237caa0625a..f18fc82fbe998c26784d2c60a735e3aec1bdde67 100644 (file)
@@ -745,12 +745,23 @@ static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
 #endif
 }
 
-static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi)
+static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi,
+                                               bool base_time)
 {
        struct sit_info *sit_i = SIT_I(sbi);
-       time64_t now = ktime_get_real_seconds();
+       time64_t diff, now = ktime_get_real_seconds();
 
-       return sit_i->elapsed_time + now - sit_i->mounted_time;
+       if (now >= sit_i->mounted_time)
+               return sit_i->elapsed_time + now - sit_i->mounted_time;
+
+       /* system time is set to the past */
+       if (!base_time) {
+               diff = sit_i->mounted_time - now;
+               if (sit_i->elapsed_time >= diff)
+                       return sit_i->elapsed_time - diff;
+               return 0;
+       }
+       return sit_i->elapsed_time;
 }
 
 static inline void set_summary(struct f2fs_summary *sum, nid_t nid,