1 vfs: don't let the dirty time inodes get more than a day stale
3 Guarantee that the on-disk timestamps will be no more than 24 hours
6 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
8 fs/fs-writeback.c | 1 +
9 fs/inode.c | 28 +++++++++++++++++++++++-----
10 include/linux/fs.h | 1 +
11 3 files changed, 25 insertions(+), 5 deletions(-)
13 diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
14 index ce7de22..eb04277 100644
15 --- a/fs/fs-writeback.c
16 +++ b/fs/fs-writeback.c
17 @@ -1141,6 +1141,7 @@ void __mark_inode_dirty(struct inode *inode, int flags)
18 if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
19 trace_writeback_dirty_inode_start(inode, flags);
21 + inode->i_ts_dirty_day = 0;
22 if (sb->s_op->dirty_inode)
23 sb->s_op->dirty_inode(inode, flags);
25 diff --git a/fs/inode.c b/fs/inode.c
26 index 2093a84..321c7d7 100644
29 @@ -1511,6 +1511,8 @@ static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
31 static int update_time(struct inode *inode, struct timespec *time, int flags)
33 + struct timespec uptime;
34 + unsigned short days_since_boot;
37 if (inode->i_op->update_time) {
38 @@ -1525,17 +1527,33 @@ static int update_time(struct inode *inode, struct timespec *time, int flags)
40 inode->i_ctime = *time;
42 - inode->i_mtime = *time;
43 + inode->i_mtime = *time;
46 + * If i_ts_dirty_day is zero, then either we have not deferred
47 + * timestamp updates, or the system has been up for less than
48 + * a day (so days_since_boot is zero), so we defer timestamp
49 + * updates in that case and set the I_DIRTY_TIME flag. If a
50 + * day or more has passed, then i_ts_dirty_day will be
51 + * different from days_since_boot, and then we should update
52 + * the on-disk inode and then we can clear i_ts_dirty_day.
54 if ((inode->i_sb->s_flags & MS_LAZYTIME) &&
55 !(flags & S_VERSION)) {
56 if (inode->i_state & I_DIRTY_TIME)
58 - spin_lock(&inode->i_lock);
59 - inode->i_state |= I_DIRTY_TIME;
60 - spin_unlock(&inode->i_lock);
62 + get_monotonic_boottime(&uptime);
63 + days_since_boot = div_u64(uptime.tv_sec, 86400);
64 + if (!inode->i_ts_dirty_day ||
65 + inode->i_ts_dirty_day == days_since_boot) {
66 + spin_lock(&inode->i_lock);
67 + inode->i_state |= I_DIRTY_TIME;
68 + spin_unlock(&inode->i_lock);
69 + inode->i_ts_dirty_day = days_since_boot;
73 + inode->i_ts_dirty_day = 0;
74 if (inode->i_op->write_time)
75 return inode->i_op->write_time(inode);
76 mark_inode_dirty_sync(inode);
77 diff --git a/include/linux/fs.h b/include/linux/fs.h
78 index 489b2f2..e3574cd 100644
79 --- a/include/linux/fs.h
80 +++ b/include/linux/fs.h
81 @@ -575,6 +575,7 @@ struct inode {
82 struct timespec i_ctime;
83 spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
84 unsigned short i_bytes;
85 + unsigned short i_ts_dirty_day;
86 unsigned int i_blkbits;