linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] time.c::timespec_trunc: fix nanosecond file time rounding
@ 2015-06-09 17:36 Karsten Blees
  2015-06-16 17:07 ` John Stultz
  0 siblings, 1 reply; 6+ messages in thread
From: Karsten Blees @ 2015-06-09 17:36 UTC (permalink / raw)
  To: linux-kernel; +Cc: Thomas Gleixner, John Stultz

From: Karsten Blees <blees@dcon.de>
Date: Tue, 9 Jun 2015 10:50:28 +0200

The rounding optimization in timespec_trunc() is based on the incorrect
assumptions that current_kernel_time() is rounded to jiffies resolution,
and that jiffies resolution is a multiple of all potential file time
granularities.

Thus, sub-second portions of in-core file times are not rounded to on-disk
granularity. I.e. file times may change when the inode is re-read from disk
or when the file system is remounted.

File systems with on-disk resolutions of exactly 1 ns or 1 s are not
affected by this.

Steps to reproduce with e.g. UDF:

  $ dd if=/dev/zero of=udfdisk count=10000 && mkudffs udfdisk
  $ mkdir udf && mount udfdisk udf
  $ touch udf/test && stat -c %y udf/test
  2015-06-09 10:22:56.130006767 +0200
  $ umount udf && mount udfdisk udf
  $ stat -c %y udf/test
  2015-06-09 10:22:56.130006000 +0200

Remounting rounds the mtime to 1µs.

Fix the rounding in timespec_trunc() and update the documentation.

Note: This does _not_ fix the issue for FAT's 2 second mtime resolution,
as struct super_block.s_time_gran isn't prepared to handle different
ctime / mtime / atime resolutions nor resolutions > 1 second.

Signed-off-by: Karsten Blees <blees@dcon.de>
---

This issue came up in a recent discussion on the git ML about enabling
nanosecond file times on Windows, see

http://thread.gmane.org/gmane.comp.version-control.msysgit/21290/focus=21315


 kernel/time/time.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)

diff --git a/kernel/time/time.c b/kernel/time/time.c
index 972e3bb..362ee06 100644
--- a/kernel/time/time.c
+++ b/kernel/time/time.c
@@ -287,23 +287,14 @@ EXPORT_SYMBOL(jiffies_to_usecs);
  * @t: Timespec
  * @gran: Granularity in ns.
  *
- * Truncate a timespec to a granularity. gran must be smaller than a second.
- * Always rounds down.
- *
- * This function should be only used for timestamps returned by
- * current_kernel_time() or CURRENT_TIME, not with do_gettimeofday() because
- * it doesn't handle the better resolution of the latter.
+ * Truncate a timespec to a granularity. gran must not be greater than a
+ * second (10^9 ns). Always rounds down.
  */
 struct timespec timespec_trunc(struct timespec t, unsigned gran)
 {
-	/*
-	 * Division is pretty slow so avoid it for common cases.
-	 * Currently current_kernel_time() never returns better than
-	 * jiffies resolution. Exploit that.
-	 */
-	if (gran <= jiffies_to_usecs(1) * 1000) {
+	if (gran <= 1) {
 		/* nothing */
-	} else if (gran == 1000000000) {
+	} else if (gran >= 1000000000) {
 		t.tv_nsec = 0;
 	} else {
 		t.tv_nsec -= t.tv_nsec % gran;
-- 
2.0.0.791.g124e248


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-07-01 18:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-09 17:36 [PATCH] time.c::timespec_trunc: fix nanosecond file time rounding Karsten Blees
2015-06-16 17:07 ` John Stultz
2015-06-16 22:39   ` Karsten Blees
2015-06-16 23:08     ` John Stultz
2015-06-25 12:13       ` [PATCH v2] " Karsten Blees
2015-07-01 18:07         ` John Stultz

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).