All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>,
	Chuck Lever <chuck.lever@oracle.com>, Neil Brown <neilb@suse.de>,
	Olga Kornievskaia <kolga@netapp.com>,
	Dai Ngo <Dai.Ngo@oracle.com>, Tom Talpey <tom@talpey.com>,
	Chandan Babu R <chandan.babu@oracle.com>,
	"Darrick J. Wong" <djwong@kernel.org>,
	Dave Chinner <david@fromorbit.com>, Jan Kara <jack@suse.cz>,
	Linus Torvalds <torvalds@linux-foundation.org>
Cc: Kent Overstreet <kent.overstreet@linux.dev>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-nfs@vger.kernel.org, linux-xfs@vger.kernel.org,
	Jeff Layton <jlayton@kernel.org>
Subject: [PATCH v8 4/5] fs: add timestamp_truncate_to_gran helper
Date: Fri, 22 Sep 2023 13:14:43 -0400	[thread overview]
Message-ID: <20230922-ctime-v8-4-45f0c236ede1@kernel.org> (raw)
In-Reply-To: <20230922-ctime-v8-0-45f0c236ede1@kernel.org>

In a future patch, we're going to need to truncate fine-grained
timestamps down to jiffies granularity. Add a new helper that allows
truncating down to an arbitrary granularity.

Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/inode.c         | 38 +++++++++++++++++++++++++++-----------
 include/linux/fs.h |  1 +
 2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/fs/inode.c b/fs/inode.c
index 293f9ba623d1..ae6baa5b17c5 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -2521,6 +2521,29 @@ void inode_nohighmem(struct inode *inode)
 }
 EXPORT_SYMBOL(inode_nohighmem);
 
+/**
+ * timestamp_truncate_to_gran - Truncate timespec to a granularity
+ * @t: Timespec
+ * @gran: the specified granularity (in ns)
+ *
+ * Truncate a timespec to the specified granularity. Always rounds down.
+ * gran must not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
+ */
+struct timespec64 timestamp_truncate_to_gran(struct timespec64 t, unsigned int gran)
+{
+	/* Avoid division in the common cases 1 ns and 1 s. */
+	if (gran == 1)
+		; /* nothing */
+	else if (gran == NSEC_PER_SEC)
+		t.tv_nsec = 0;
+	else if (gran > 1 && gran < NSEC_PER_SEC)
+		t.tv_nsec -= t.tv_nsec % gran;
+	else
+		WARN(1, "invalid file time granularity: %u", gran);
+	return t;
+}
+EXPORT_SYMBOL(timestamp_truncate_to_gran);
+
 /**
  * timestamp_truncate - Truncate timespec to a granularity
  * @t: Timespec
@@ -2536,19 +2559,12 @@ struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode)
 	unsigned int gran = sb->s_time_gran;
 
 	t.tv_sec = clamp(t.tv_sec, sb->s_time_min, sb->s_time_max);
-	if (unlikely(t.tv_sec == sb->s_time_max || t.tv_sec == sb->s_time_min))
+	if (unlikely(t.tv_sec == sb->s_time_max || t.tv_sec == sb->s_time_min)) {
 		t.tv_nsec = 0;
+		return t;
+	}
 
-	/* Avoid division in the common cases 1 ns and 1 s. */
-	if (gran == 1)
-		; /* nothing */
-	else if (gran == NSEC_PER_SEC)
-		t.tv_nsec = 0;
-	else if (gran > 1 && gran < NSEC_PER_SEC)
-		t.tv_nsec -= t.tv_nsec % gran;
-	else
-		WARN(1, "invalid file time granularity: %u", gran);
-	return t;
+	return timestamp_truncate_to_gran(t, gran);
 }
 EXPORT_SYMBOL(timestamp_truncate);
 
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 91239a4c1a65..fa696322dae3 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -748,6 +748,7 @@ struct inode {
 	void			*i_private; /* fs or device private pointer */
 } __randomize_layout;
 
+struct timespec64 timestamp_truncate_to_gran(struct timespec64 t, unsigned int gran);
 struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode);
 
 static inline unsigned int i_blocksize(const struct inode *node)

-- 
2.41.0


  parent reply	other threads:[~2023-09-22 17:15 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-22 17:14 [PATCH v8 0/5] fs: multigrain timestamps for XFS's change_cookie Jeff Layton
2023-09-22 17:14 ` [PATCH v8 1/5] fs: add infrastructure for multigrain timestamps Jeff Layton
2023-09-22 17:31   ` Kent Overstreet
2023-09-22 18:22     ` Jeff Layton
2023-09-22 17:14 ` [PATCH v8 2/5] fs: optimize away some fine-grained timestamp updates Jeff Layton
2023-09-22 17:14 ` [PATCH v8 3/5] fs: have setattr_copy handle multigrain timestamps appropriately Jeff Layton
2023-09-22 17:14 ` Jeff Layton [this message]
2023-09-22 17:14 ` [PATCH v8 5/5] xfs: switch to multigrain timestamps Jeff Layton
2023-09-23  7:15 ` [PATCH v8 0/5] fs: multigrain timestamps for XFS's change_cookie Amir Goldstein
2023-09-23 10:22   ` Jeff Layton
2023-09-23 14:58     ` Amir Goldstein
2023-09-25 10:08       ` Jeff Layton
2023-09-23 10:46   ` Jeff Layton
2023-09-23 14:52     ` Amir Goldstein
2023-09-24 22:18       ` Dave Chinner
2023-09-25 10:14         ` Jeff Layton
2023-09-25 22:32           ` Dave Chinner
2023-09-26 11:31             ` Jeff Layton
2023-09-26 23:33               ` Dave Chinner
2023-09-27 10:26                 ` Jeff Layton
2023-09-23 20:43     ` Amir Goldstein
2023-09-24 11:31 ` Christian Brauner
2023-09-24 22:44   ` NeilBrown
2023-09-25 10:17     ` Jeff Layton
2023-09-26 12:10       ` Christian Brauner
2023-09-26 12:18     ` Christian Brauner
2023-09-26 12:51       ` Jeff Layton
2023-09-26 14:29         ` 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=20230922-ctime-v8-4-45f0c236ede1@kernel.org \
    --to=jlayton@kernel.org \
    --cc=Dai.Ngo@oracle.com \
    --cc=brauner@kernel.org \
    --cc=chandan.babu@oracle.com \
    --cc=chuck.lever@oracle.com \
    --cc=david@fromorbit.com \
    --cc=djwong@kernel.org \
    --cc=jack@suse.cz \
    --cc=kent.overstreet@linux.dev \
    --cc=kolga@netapp.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=neilb@suse.de \
    --cc=tom@talpey.com \
    --cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.