]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - include/linux/time64.h
add missing include
[bcachefs-tools-debian] / include / linux / time64.h
index 9d8a3efecc6e0aea3ccedd6265c1216d1c787553..9e5d90e6ac691dbe164d198ff19a165d73281146 100644 (file)
@@ -38,6 +38,19 @@ struct itimerspec64 {
 #define KTIME_MAX                      ((s64)~((u64)1 << 63))
 #define KTIME_SEC_MAX                  (KTIME_MAX / NSEC_PER_SEC)
 
+static inline struct timespec ns_to_timespec(const u64 nsec)
+{
+       return (struct timespec) {
+               .tv_sec = nsec / NSEC_PER_SEC,
+               .tv_nsec = nsec % NSEC_PER_SEC,
+       };
+}
+
+static inline s64 timespec_to_ns(const struct timespec *ts)
+{
+       return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
+}
+
 #if __BITS_PER_LONG == 64
 
 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
@@ -178,8 +191,8 @@ extern struct timespec64 ns_to_timespec64(const s64 nsec);
  */
 static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
 {
-       a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
-       a->tv_nsec = ns;
+       a->tv_sec += (a->tv_nsec + ns) / NSEC_PER_SEC;
+       a->tv_nsec += (a->tv_nsec + ns) % NSEC_PER_SEC;
 }
 
 #endif
@@ -191,4 +204,19 @@ static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
 extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
                                         const struct timespec64 rhs);
 
+static inline struct timespec timespec_trunc(struct timespec t, unsigned 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, "illegal file time granularity: %u", gran);
+       }
+       return t;
+}
+
 #endif /* _LINUX_TIME64_H */