Skip to contentSkip to conversation

fs: fixes for multigrain ctime code

PATCH Applied Jeff Layton 3 participants 8 messages Sep 7, 2023 – Sep 8, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover fs: fixes for multigrain ctime code

The kernel test robot noted some test failures with the LTP mount03 test on tmpfs. From the test output, it looked like the atime had gone backward.

One way this could happen would be for tmpfs to get a new inode from the slab that had a ctime that appeared to be in the future. inode_update_ctime_current would just return that time and then the mtime and atime would be set to the same value. Then later, the atime gets overwritten by "now" which is still lower than the garbage ctime value.

I've not been able to reproduce this on my test rig, so I'm not certain this fixes the problem that was reported. I'm hopeful though, so I've left the KTR tags in place.

---
Jeff Layton (2):
fs: initialize inode->__i_ctime to the epoch
fs: don't update the atime if existing atime is newer than "now"

 fs/inode.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

---
base-commit: 7ba2090ca64ea1aa435744884124387db1fac70f
change-id: 20230907-ctime-fixes-ec6319ca01be

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
#1 lore raw

PATCH 1/2 fs: initialize inode->__i_ctime to the epoch

With the advent of multigrain timestamps, we use inode_set_ctime_current to set the ctime, which can skip updating if the existing ctime appears to be in the future. Because we don't initialize this field at allocation time, that could prevent the ctime from being initialized properly when the inode is instantiated.

Always initialize the ctime field to the epoch so that the filesystem can set the timestamps properly later.

---
 fs/inode.c | 2 ++
 1 file changed, 2 insertions(+)
fs/inode.c +2 -0
index 35fd688168c5..54237f4242ff 100644
@@ -168,6 +168,8 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
 	inode->i_fop = &no_open_fops;
 	inode->i_ino = 0;
 	inode->__i_nlink = 1;
+	inode->__i_ctime.tv_sec = 0;
+	inode->__i_ctime.tv_nsec = 0;
 	inode->i_opflags = 0;
 	if (sb->s_xattr)
 		inode->i_opflags |= IOP_XATTR;
Signature · 1 line
2.41.0
reviewer
#2 lore raw

Re: [PATCH 1/2] fs: initialize inode->__i_ctime to the epoch

Quoted 6 lines · Jeff Layton

With the advent of multigrain timestamps, we use inode_set_ctime_current to set the ctime, which can skip updating if the existing ctime appears to be in the future. Because we don't initialize this field at allocation time, that could prevent the ctime from being initialized properly when the inode is instantiated.

Quoted 6 lines

Always initialize the ctime field to the epoch so that the filesystem can set the timestamps properly later.

Looks good but don't you need the same treatment to atime after your patch 2/2?

Honza

Quoted 21 lines
---
 fs/inode.c | 2 ++
 1 file changed, 2 insertions(+)
fs/inode.c +2 -0
index 35fd688168c5..54237f4242ff 100644
@@ -168,6 +168,8 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
 	inode->i_fop = &no_open_fops;
 	inode->i_ino = 0;
 	inode->__i_nlink = 1;
+	inode->__i_ctime.tv_sec = 0;
+	inode->__i_ctime.tv_nsec = 0;
 	inode->i_opflags = 0;
 	if (sb->s_xattr)
 		inode->i_opflags |= IOP_XATTR;
Signature · 2 lines
-- 
2.41.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw

Re: [PATCH 1/2] fs: initialize inode->__i_ctime to the epoch

Quoted 10 lines · Jan Kara
Quoted 9 lines · Jeff Layton

With the advent of multigrain timestamps, we use inode_set_ctime_current to set the ctime, which can skip updating if the existing ctime appears to be in the future. Because we don't initialize this field at allocation time, that could prevent the ctime from being initialized properly when the inode is instantiated.

Always initialize the ctime field to the epoch so that the filesystem can set the timestamps properly later.

Quoted 8 lines
Quoted 3 lines

Looks good but don't you need the same treatment to atime after your patch 2/2?

I don't think so. Most filesystems are doing something along the lines of this when allocating a new inode:

    inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);

...and I think they pretty much all have to initialize i_atime properly, since someone could stat the inode before an atime update occurs.

Quoted 21 lines
Quoted 21 lines
---
 fs/inode.c | 2 ++
 1 file changed, 2 insertions(+)
fs/inode.c +2 -0
index 35fd688168c5..54237f4242ff 100644
@@ -168,6 +168,8 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
 	inode->i_fop = &no_open_fops;
 	inode->i_ino = 0;
 	inode->__i_nlink = 1;
+	inode->__i_ctime.tv_sec = 0;
+	inode->__i_ctime.tv_nsec = 0;
 	inode->i_opflags = 0;
 	if (sb->s_xattr)
 		inode->i_opflags |= IOP_XATTR;
Signature · 2 lines
-- 
2.41.0
Signature · 1 line
Jeff Layton <[email protected]>
reviewer Reviewed-by
#4 lore raw

Re: [PATCH 1/2] fs: initialize inode->__i_ctime to the epoch

Quoted 20 lines · Jeff Layton
Quoted 10 lines · Jan Kara
Quoted 9 lines · Jeff Layton

With the advent of multigrain timestamps, we use inode_set_ctime_current to set the ctime, which can skip updating if the existing ctime appears to be in the future. Because we don't initialize this field at allocation time, that could prevent the ctime from being initialized properly when the inode is instantiated.

Always initialize the ctime field to the epoch so that the filesystem can set the timestamps properly later.

Quoted 8 lines
Quoted 3 lines

Looks good but don't you need the same treatment to atime after your patch 2/2?

Quoted 7 lines

I don't think so. Most filesystems are doing something along the lines of this when allocating a new inode:

    inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);

...and I think they pretty much all have to initialize i_atime properly, since someone could stat the inode before an atime update occurs.

Ah, right. Feel free to add:

Honza

Quoted 24 lines
Quoted 21 lines
Quoted 21 lines
---
 fs/inode.c | 2 ++
 1 file changed, 2 insertions(+)
fs/inode.c +2 -0
index 35fd688168c5..54237f4242ff 100644
@@ -168,6 +168,8 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
 	inode->i_fop = &no_open_fops;
 	inode->i_ino = 0;
 	inode->__i_nlink = 1;
+	inode->__i_ctime.tv_sec = 0;
+	inode->__i_ctime.tv_nsec = 0;
 	inode->i_opflags = 0;
 	if (sb->s_xattr)
 		inode->i_opflags |= IOP_XATTR;
Signature · 2 lines
-- 
2.41.0
Signature · 1 line
Jeff Layton <[email protected]>
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw

PATCH 2/2 fs: don't update the atime if existing atime is newer than "now"

In reply to Jeff Layton

It's possible for the atime to be updated with a fine-grained timestamp and then later get an update that uses a coarse-grained timestamp which makes the atime appear to go backward.

Fix this by only updating the atime if "now" is later than the current value.

---
 fs/inode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/inode.c +2 -2
index 54237f4242ff..cf4726b7f4b5 100644
@@ -1905,7 +1905,7 @@ int inode_update_timestamps(struct inode *inode, int flags)
 	}
 
 	if (flags & S_ATIME) {
-		if (!timespec64_equal(&now, &inode->i_atime)) {
+		if (timespec64_compare(&inode->i_atime, &now) < 0) {
 			inode->i_atime = now;
 			updated |= S_ATIME;
 		}
@@ -1991,7 +1991,7 @@ bool atime_needs_update(const struct path *path, struct inode *inode)
 	if (!relatime_need_update(mnt, inode, now))
 		return false;
 
-	if (timespec64_equal(&inode->i_atime, &now))
+	if (timespec64_compare(&inode->i_atime, &now) >= 0)
 		return false;
 
 	return true;
Signature · 1 line
2.41.0
reviewer Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] fs: don't update the atime if existing atime is newer than "now"

Quoted 10 lines · Jeff Layton

It's possible for the atime to be updated with a fine-grained timestamp and then later get an update that uses a coarse-grained timestamp which makes the atime appear to go backward.

Fix this by only updating the atime if "now" is later than the current value.

Looks good. Feel free to add:

Honza

Quoted 30 lines
---
 fs/inode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/inode.c +2 -2
index 54237f4242ff..cf4726b7f4b5 100644
@@ -1905,7 +1905,7 @@ int inode_update_timestamps(struct inode *inode, int flags)
 	}
 
 	if (flags & S_ATIME) {
-		if (!timespec64_equal(&now, &inode->i_atime)) {
+		if (timespec64_compare(&inode->i_atime, &now) < 0) {
 			inode->i_atime = now;
 			updated |= S_ATIME;
 		}
@@ -1991,7 +1991,7 @@ bool atime_needs_update(const struct path *path, struct inode *inode)
 	if (!relatime_need_update(mnt, inode, now))
 		return false;
 
-	if (timespec64_equal(&inode->i_atime, &now))
+	if (timespec64_compare(&inode->i_atime, &now) >= 0)
 		return false;
 
 	return true;
Signature · 2 lines
-- 
2.41.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#7 lore raw
In reply to Jeff Layton
Quoted 4 lines · Jeff Layton

The kernel test robot noted some test failures with the LTP mount03 test on tmpfs. From the test output, it looked like the atime had gone backward.

Quoted 8 lines

One way this could happen would be for tmpfs to get a new inode from the slab that had a ctime that appeared to be in the future. inode_update_ctime_current would just return that time and then the mtime and atime would be set to the same value. Then later, the atime gets overwritten by "now" which is still lower than the garbage ctime value.

[...]

Picked up so we can get some -next testing. Hopefully that test thing can verify the fix.

---

Applied to the vfs.ctime branch of the vfs/vfs.git tree. Patches in the vfs.ctime branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.ctime

[1/2] fs: initialize inode->__i_ctime to the epoch

      https://git.kernel.org/vfs/vfs/c/7651a330dcbd
[2/2] fs: don't update the atime if existing atime is newer than "now"
      https://git.kernel.org/vfs/vfs/c/4c950d80d98d