[v3,4/5] vfs: Add timestamp_truncate() api

Submitted by Deepa Dinamani on Feb. 18, 2017, 11:30 p.m.

Details

Message ID 1487460608-15697-5-git-send-email-deepa.kernel@gmail.com
State New
Headers show

Commit Message

Deepa Dinamani Feb. 18, 2017, 11:30 p.m.
timespec_trunc() function is used to truncate a
filesystem timestamp to the right granularity.
But, the function does not clamp tv_sec part of the
timestamps according to the filesystem timestamp limits.

Also, timespec_trunc() is exclusively used for filesystem
timestamps. Move the api to be part of vfs.

The replacement api: timestamp_truncate() also alters the
signature of the function to accommodate filesystem
timestamp clamping according to flesystem limits.

Note that the clamp_t macro is used for clamping here as vfs
is not yet using struct timespec64 internally. This is
required for compilation purposes.
Also note that clamp won't do the right thing for timestamps
beyond 2038 on 32-bit machines until the vfs uses timespec64.
After the vfs is transitioned to use timespec64 for timestamps,
clamp_t() can be replaced by clamp().

Signed-off-by: Deepa Dinamani <deepa.kernel@gmail.com>
---
 fs/inode.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

Comments

kbuild test robot Feb. 19, 2017, 6:26 a.m.
Hi Deepa,

[auto build test WARNING on linus/master]
[also build test WARNING on v4.10-rc8 next-20170217]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Deepa-Dinamani/vfs-Add-timestamp-range-check-support/20170219-073734
reproduce: make htmldocs

All warnings (new ones prefixed by >>):

   fs/inode.c:1679: warning: No description found for parameter 'rcu'
   fs/inode.c:2125: warning: No description found for parameter 'inode'
>> fs/inode.c:2125: warning: Excess function parameter 'gran' description in 'timestamp_truncate'
   include/linux/jbd2.h:442: warning: No description found for parameter 'i_transaction'
   include/linux/jbd2.h:442: warning: No description found for parameter 'i_next_transaction'
   include/linux/jbd2.h:442: warning: No description found for parameter 'i_list'
   include/linux/jbd2.h:442: warning: No description found for parameter 'i_vfs_inode'
   include/linux/jbd2.h:442: warning: No description found for parameter 'i_flags'
   include/linux/jbd2.h:494: warning: No description found for parameter 'h_rsv_handle'
   include/linux/jbd2.h:494: warning: No description found for parameter 'h_reserved'
   include/linux/jbd2.h:494: warning: No description found for parameter 'h_type'
   include/linux/jbd2.h:494: warning: No description found for parameter 'h_line_no'
   include/linux/jbd2.h:494: warning: No description found for parameter 'h_start_jiffies'
   include/linux/jbd2.h:494: warning: No description found for parameter 'h_requested_credits'
   include/linux/jbd2.h:1047: warning: No description found for parameter 'j_chkpt_bhs[JBD2_NR_BATCH]'
   include/linux/jbd2.h:1047: warning: No description found for parameter 'j_devname[BDEVNAME_SIZE+24]'
   include/linux/jbd2.h:1047: warning: No description found for parameter 'j_average_commit_time'
   include/linux/jbd2.h:1047: warning: No description found for parameter 'j_min_batch_time'
   include/linux/jbd2.h:1047: warning: No description found for parameter 'j_max_batch_time'
   include/linux/jbd2.h:1047: warning: No description found for parameter 'j_commit_callback'
   include/linux/jbd2.h:1047: warning: No description found for parameter 'j_failed_commit'
   include/linux/jbd2.h:1047: warning: No description found for parameter 'j_chksum_driver'
   include/linux/jbd2.h:1047: warning: No description found for parameter 'j_csum_seed'
   fs/jbd2/transaction.c:428: warning: No description found for parameter 'rsv_blocks'
   fs/jbd2/transaction.c:428: warning: No description found for parameter 'gfp_mask'
   fs/jbd2/transaction.c:428: warning: No description found for parameter 'type'
   fs/jbd2/transaction.c:428: warning: No description found for parameter 'line_no'
   fs/jbd2/transaction.c:504: warning: No description found for parameter 'type'
   fs/jbd2/transaction.c:504: warning: No description found for parameter 'line_no'
   fs/jbd2/transaction.c:634: warning: No description found for parameter 'gfp_mask'

vim +2125 fs/inode.c

  2109	
  2110	void inode_nohighmem(struct inode *inode)
  2111	{
  2112		mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
  2113	}
  2114	EXPORT_SYMBOL(inode_nohighmem);
  2115	
  2116	/**
  2117	 * fs_timespec_trunc - Truncate timespec to a granularity
  2118	 * @t: Timespec
  2119	 * @gran: Granularity in ns.
  2120	 *
  2121	 * Truncate a timespec to a granularity. Always rounds down. gran must
  2122	 * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
  2123	 */
  2124	struct timespec timestamp_truncate(struct timespec t, struct inode *inode)
> 2125	{
  2126		struct super_block *sb = inode->i_sb;
  2127		unsigned int gran = sb->s_time_gran;
  2128	
  2129		t.tv_sec = clamp_t(time64_t, t.tv_sec, sb->s_time_min, sb->s_time_max);
  2130	
  2131		/* Avoid division in the common cases 1 ns and 1 s. */
  2132		if (gran == 1) {
  2133			/* nothing */

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
Deepa Dinamani Feb. 21, 2017, 6:18 p.m.
>>> fs/inode.c:2125: warning: Excess function parameter 'gran' description in 'timestamp_truncate'
>   2116  /**
>   2117   * fs_timespec_trunc - Truncate timespec to a granularity
>   2118   * @t: Timespec
>   2119   * @gran: Granularity in ns.
>   2120   *
>   2121   * Truncate a timespec to a granularity. Always rounds down. gran must
>   2122   * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
>   2123   */
>   2124  struct timespec timestamp_truncate(struct timespec t, struct inode *inode)
>> 2125  {
>   2126          struct super_block *sb = inode->i_sb;
>   2127          unsigned int gran = sb->s_time_gran;

This because the comment does not match the actual function signature:
The gran param mentioned in the comment is not actually part of
function signature.

I will fix the comment in the next version.

-Deepa

Patch hide | download patch | download mbox

diff --git a/fs/inode.c b/fs/inode.c
index 0573a3e..6a1bc12 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -2114,6 +2114,36 @@  void inode_nohighmem(struct inode *inode)
 EXPORT_SYMBOL(inode_nohighmem);
 
 /**
+ * fs_timespec_trunc - Truncate timespec to a granularity
+ * @t: Timespec
+ * @gran: Granularity in ns.
+ *
+ * Truncate a timespec to a granularity. Always rounds down. gran must
+ * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
+ */
+struct timespec timestamp_truncate(struct timespec t, struct inode *inode)
+{
+	struct super_block *sb = inode->i_sb;
+	unsigned int gran = sb->s_time_gran;
+
+	t.tv_sec = clamp_t(time64_t, t.tv_sec, sb->s_time_min, sb->s_time_max);
+
+	/* 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, "illegal file time granularity: %u", gran);
+	}
+	return t;
+}
+EXPORT_SYMBOL(timestamp_truncate);
+
+
+/**
  * current_time - Return FS time
  * @inode: inode.
  *
@@ -2132,6 +2162,6 @@  struct timespec current_time(struct inode *inode)
 		return now;
 	}
 
-	return timespec_trunc(now, inode->i_sb->s_time_gran);
+	return timestamp_truncate(now, inode);
 }
 EXPORT_SYMBOL(current_time);