Skip to content

Commit a1f72ac

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: fix to update mtime correctly
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>
1 parent 1061fd4 commit a1f72ac

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎fs/f2fs/checkpoint.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
12331233
* modify checkpoint
12341234
* version number is already updated
12351235
*/
1236-
ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
1236+
ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
12371237
ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
12381238
for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
12391239
ckpt->cur_node_segno[i] =

‎fs/f2fs/segment.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,8 +1822,9 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
18221822
(new_vblocks > sbi->blocks_per_seg)));
18231823

18241824
se->valid_blocks = new_vblocks;
1825-
se->mtime = get_mtime(sbi);
1826-
SIT_I(sbi)->max_mtime = se->mtime;
1825+
se->mtime = get_mtime(sbi, false);
1826+
if (se->mtime > SIT_I(sbi)->max_mtime)
1827+
SIT_I(sbi)->max_mtime = se->mtime;
18271828

18281829
/* Update valid block bitmap */
18291830
if (del > 0) {
@@ -3884,7 +3885,7 @@ static void init_min_max_mtime(struct f2fs_sb_info *sbi)
38843885
if (sit_i->min_mtime > mtime)
38853886
sit_i->min_mtime = mtime;
38863887
}
3887-
sit_i->max_mtime = get_mtime(sbi);
3888+
sit_i->max_mtime = get_mtime(sbi, false);
38883889
up_write(&sit_i->sentry_lock);
38893890
}
38903891

‎fs/f2fs/segment.h‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,12 +745,23 @@ static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
745745
#endif
746746
}
747747

748-
static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi)
748+
static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi,
749+
bool base_time)
749750
{
750751
struct sit_info *sit_i = SIT_I(sbi);
751-
time64_t now = ktime_get_real_seconds();
752+
time64_t diff, now = ktime_get_real_seconds();
752753

753-
return sit_i->elapsed_time + now - sit_i->mounted_time;
754+
if (now >= sit_i->mounted_time)
755+
return sit_i->elapsed_time + now - sit_i->mounted_time;
756+
757+
/* system time is set to the past */
758+
if (!base_time) {
759+
diff = sit_i->mounted_time - now;
760+
if (sit_i->elapsed_time >= diff)
761+
return sit_i->elapsed_time - diff;
762+
return 0;
763+
}
764+
return sit_i->elapsed_time;
754765
}
755766

756767
static inline void set_summary(struct f2fs_summary *sum, nid_t nid,

0 commit comments

Comments
 (0)