linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Jeff Layton <jlayton@kernel.org>
Subject: [PATCH] fs: compare truncated timestamps in current_mgtime
Date: Fri, 28 Jul 2023 09:21:37 -0400	[thread overview]
Message-ID: <20230728-mgctime-v1-1-5b0ddc5df08e@kernel.org> (raw)

current_mgtime compares the ctime (which has already been truncated) to
the value from ktime_get_coarse_real_ts64 (which has not). All of the
existing filesystems that enable mgtime have 1ns granularity, so this is
not a problem today, but it is more correct to compare truncated
timestamps instead.

Do the truncate earlier, so we're comparing like things.

Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/inode.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/fs/inode.c b/fs/inode.c
index 369621e7faf5..8199d0e02cce 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -2097,28 +2097,28 @@ EXPORT_SYMBOL(file_remove_privs);
  */
 static struct timespec64 current_mgtime(struct inode *inode)
 {
-	struct timespec64 now;
+	struct timespec64 now, ctime;
 	atomic_long_t *pnsec = (atomic_long_t *)&inode->__i_ctime.tv_nsec;
 	long nsec = atomic_long_read(pnsec);
 
 	if (nsec & I_CTIME_QUERIED) {
 		ktime_get_real_ts64(&now);
-	} else {
-		struct timespec64 ctime;
+		return timestamp_truncate(now, inode);
+	}
 
-		ktime_get_coarse_real_ts64(&now);
+	ktime_get_coarse_real_ts64(&now);
+	now = timestamp_truncate(now, inode);
 
-		/*
-		 * If we've recently fetched a fine-grained timestamp
-		 * then the coarse-grained one may still be earlier than the
-		 * existing one. Just keep the existing ctime if so.
-		 */
-		ctime = inode_get_ctime(inode);
-		if (timespec64_compare(&ctime, &now) > 0)
-			now = ctime;
-	}
+	/*
+	 * If we've recently fetched a fine-grained timestamp
+	 * then the coarse-grained one may still be earlier than the
+	 * existing ctime. Just keep the existing value if so.
+	 */
+	ctime = inode_get_ctime(inode);
+	if (timespec64_compare(&ctime, &now) > 0)
+		now = ctime;
 
-	return timestamp_truncate(now, inode);
+	return now;
 }
 
 /**

---
base-commit: 4ce0966ed7c04881c5f352e0bb53af9b38f94253
change-id: 20230728-mgctime-5e0ec0e89b04

Best regards,
-- 
Jeff Layton <jlayton@kernel.org>


             reply	other threads:[~2023-07-28 13:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-28 13:21 Jeff Layton [this message]
2023-07-28 15:21 ` [PATCH] fs: compare truncated timestamps in current_mgtime Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230728-mgctime-v1-1-5b0ddc5df08e@kernel.org \
    --to=jlayton@kernel.org \
    --cc=brauner@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).