Skip to contentSkip to conversation

libfs: fix multigrain timestamps of tmpfs directories

PATCH In review Christian Brauner 2 participants 4 messages Nov 25

Subsystems (3)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

cover libfs: fix multigrain timestamps of tmpfs directories

simple_link() and __simple_unlink() copy the child's new ctime into the parent directory. That doesn't work with multigrain timestamps. The child's ctime is only fine-grained if the child itself was queried and copying it clears the I_CTIME_QUERIED flag of the directory.

tmpfs is the only multigrain filesystem that uses these helpers. So since v6.13 a tmpfs directory whose timestamps were queried can come out of link(), unlink(), rmdir() or a rename over an existing entry with the same ctime and mtime as before. If the renamed entry comes from another directory size and link count stay the same as well and userspace has no way to tell that the directory changed at all. In a VM that happened in 1738 of 2000 tries.

Give the directory its own inode_set_ctime_current() call like the other multigrain filesystems do and add a selftest that checks the timestamps of the parent directory after each directory operation.

---
Christian Brauner (2):
libfs: don't copy the child's ctime to the parent directory selftests/filesystems: test multigrain timestamps of tmpfs directories

 fs/libfs.c                                        |   8 +-
 tools/testing/selftests/filesystems/.gitignore    |   1 +
 tools/testing/selftests/filesystems/Makefile      |   1 +
 tools/testing/selftests/filesystems/mgtime_test.c | 244 ++++++++++++++++++++++
 4 files changed, 250 insertions(+), 4 deletions(-)

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261125-work-libfs-mgtime-513953909f3d

author maintainer
#1 lore raw

PATCH 1/2 libfs: don't copy the child's ctime to the parent directory

simple_link() and __simple_unlink() copy the child's new ctime into the parent directory. With multigrain timestamps that ctime is only fine-grained if the child was queried and copying it drops the parent's I_CTIME_QUERIED flag. So a queried tmpfs directory keeps its ctime and mtime across link(), unlink() and rmdir() as long as the coarse clock hasn't ticked. shmem_rename2() unlinks the victim of a rename the same way, so a rename over an entry from another directory changes nothing at all that userspace could see, size and link count included.

Give the directory its own inode_set_ctime_current() call.

Fixes 234d8895e3ad ("tmpfs: add support for multigrain timestamps")
Cc [email protected] # v6.13+
---
 fs/libfs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/libfs.c +4 -4
index 96a6e9b1535fb..07331e829c133 100644
@@ -754,8 +754,8 @@ int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *den
 {
 	struct inode *inode = d_inode(old_dentry);
 
-	inode_set_mtime_to_ts(dir,
-			      inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
+	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
+	inode_set_ctime_current(inode);
 	inc_nlink(inode);
 	ihold(inode);
 	d_make_persistent(dentry, inode);
@@ -788,8 +788,8 @@ void __simple_unlink(struct inode *dir, struct dentry *dentry)
 {
 	struct inode *inode = d_inode(dentry);
 
-	inode_set_mtime_to_ts(dir,
-			      inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
+	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
+	inode_set_ctime_current(inode);
 	drop_nlink(inode);
 }
 EXPORT_SYMBOL(__simple_unlink);
Signature · 1 line
2.53.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] libfs: don't copy the child's ctime to the parent directory

Quoted 34 lines · Christian Brauner

simple_link() and __simple_unlink() copy the child's new ctime into the parent directory. With multigrain timestamps that ctime is only fine-grained if the child was queried and copying it drops the parent's I_CTIME_QUERIED flag. So a queried tmpfs directory keeps its ctime and mtime across link(), unlink() and rmdir() as long as the coarse clock hasn't ticked. shmem_rename2() unlinks the victim of a rename the same way, so a rename over an entry from another directory changes nothing at all that userspace could see, size and link count included.

Give the directory its own inode_set_ctime_current() call.

Fixes 234d8895e3ad ("tmpfs: add support for multigrain timestamps")
Cc [email protected] # v6.13+
---
 fs/libfs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/libfs.c +2 -2
index 96a6e9b1535fb..07331e829c133 100644
@@ -754,8 +754,8 @@ int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *den
 {
 	struct inode *inode = d_inode(old_dentry);
 
-	inode_set_mtime_to_ts(dir,
-			      inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
+	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
+	inode_set_ctime_current(inode);
 	inc_nlink(inode);
 	ihold(inode);
 	d_make_persistent(dentry, inode);

fs/libfs.c void __simple_unlink(struct inode *dir, struct dentry *dentry) · line 795

Quoted 10 lines
fs/libfs.c +2 -2
@@ -788,8 +788,8 @@ void __simple_unlink(struct inode *dir, struct dentry *dentry)
 {
 	struct inode *inode = d_inode(dentry);
 
-	inode_set_mtime_to_ts(dir,
-			      inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
+	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
+	inode_set_ctime_current(inode);
 	drop_nlink(inode);
 }
 EXPORT_SYMBOL(__simple_unlink);

Good catch! Not sure how I missed that.

author maintainer
#3 lore raw

PATCH 2/2 selftests/filesystems: test multigrain timestamps of tmpfs directories

In reply to Christian Brauner

Check that a tmpfs directory whose timestamps were queried shows a new ctime and mtime after:

- creating a file or a directory
- link(), unlink() and rmdir()
- a rename, a rename over a file or an empty directory, and RENAME_EXCHANGE

Each case is tried a hundred times right after the directory changed so that most tries land in the same coarse clock tick.

---
 tools/testing/selftests/filesystems/.gitignore    |   1 +
 tools/testing/selftests/filesystems/Makefile      |   1 +
 tools/testing/selftests/filesystems/mgtime_test.c | 244 ++++++++++++++++++++++
 3 files changed, 246 insertions(+)
tools/testing/selftests/filesystems/.gitignore +1 -0
index cf79000e4092a..091095198a855 100644
@@ -4,6 +4,7 @@ devpts_pts
 fclog
 anon_inode_test
 kernfs_test
+mgtime_test
 idmapped_tmpfile
 ustat_test
 rw_hint_test
tools/testing/selftests/filesystems/Makefile +1 -0
index fbd5c27505ef5..1474b05874dc2 100644
@@ -4,6 +4,7 @@ CFLAGS += $(KHDR_INCLUDES)
 TEST_GEN_PROGS := devpts_pts anon_inode_test kernfs_test fclog ustat_test rw_hint_test
 TEST_GEN_PROGS += idmapped_tmpfile
 TEST_GEN_PROGS_EXTENDED := dnotify_test
+TEST_GEN_PROGS += mgtime_test
 
 include ../lib.mk
 
tools/testing/selftests/filesystems/mgtime_test.c new file +244 -0

244 lines not on this page · raw message

Signature · 1 line
2.53.0