time64.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #ifndef _LINUX_TIME64_H
  2. #define _LINUX_TIME64_H
  3. #include <uapi/linux/time.h>
  4. #include <linux/math64.h>
  5. typedef __s64 time64_t;
  6. /*
  7. * This wants to go into uapi/linux/time.h once we agreed about the
  8. * userspace interfaces.
  9. */
  10. #if __BITS_PER_LONG == 64
  11. # define timespec64 timespec
  12. #define itimerspec64 itimerspec
  13. #else
  14. struct timespec64 {
  15. time64_t tv_sec; /* seconds */
  16. long tv_nsec; /* nanoseconds */
  17. };
  18. struct itimerspec64 {
  19. struct timespec64 it_interval;
  20. struct timespec64 it_value;
  21. };
  22. #endif
  23. /* Parameters used to convert the timespec values: */
  24. #define MSEC_PER_SEC 1000L
  25. #define USEC_PER_MSEC 1000L
  26. #define NSEC_PER_USEC 1000L
  27. #define NSEC_PER_MSEC 1000000L
  28. #define USEC_PER_SEC 1000000L
  29. #define NSEC_PER_SEC 1000000000L
  30. #define FSEC_PER_SEC 1000000000000000LL
  31. /* Located here for timespec[64]_valid_strict */
  32. #define TIME64_MAX ((s64)~((u64)1 << 63))
  33. #define KTIME_MAX ((s64)~((u64)1 << 63))
  34. #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
  35. #if __BITS_PER_LONG == 64
  36. static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
  37. {
  38. return ts64;
  39. }
  40. static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
  41. {
  42. return ts;
  43. }
  44. static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
  45. {
  46. return *its64;
  47. }
  48. static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
  49. {
  50. return *its;
  51. }
  52. # define timespec64_equal timespec_equal
  53. # define timespec64_compare timespec_compare
  54. # define set_normalized_timespec64 set_normalized_timespec
  55. # define timespec64_add_safe timespec_add_safe
  56. # define timespec64_add timespec_add
  57. # define timespec64_sub timespec_sub
  58. # define timespec64_valid timespec_valid
  59. # define timespec64_valid_strict timespec_valid_strict
  60. # define timespec64_to_ns timespec_to_ns
  61. # define ns_to_timespec64 ns_to_timespec
  62. # define timespec64_add_ns timespec_add_ns
  63. #else
  64. static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
  65. {
  66. struct timespec ret;
  67. ret.tv_sec = (time_t)ts64.tv_sec;
  68. ret.tv_nsec = ts64.tv_nsec;
  69. return ret;
  70. }
  71. static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
  72. {
  73. struct timespec64 ret;
  74. ret.tv_sec = ts.tv_sec;
  75. ret.tv_nsec = ts.tv_nsec;
  76. return ret;
  77. }
  78. static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
  79. {
  80. struct itimerspec ret;
  81. ret.it_interval = timespec64_to_timespec(its64->it_interval);
  82. ret.it_value = timespec64_to_timespec(its64->it_value);
  83. return ret;
  84. }
  85. static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
  86. {
  87. struct itimerspec64 ret;
  88. ret.it_interval = timespec_to_timespec64(its->it_interval);
  89. ret.it_value = timespec_to_timespec64(its->it_value);
  90. return ret;
  91. }
  92. static inline int timespec64_equal(const struct timespec64 *a,
  93. const struct timespec64 *b)
  94. {
  95. return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
  96. }
  97. /*
  98. * lhs < rhs: return <0
  99. * lhs == rhs: return 0
  100. * lhs > rhs: return >0
  101. */
  102. static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
  103. {
  104. if (lhs->tv_sec < rhs->tv_sec)
  105. return -1;
  106. if (lhs->tv_sec > rhs->tv_sec)
  107. return 1;
  108. return lhs->tv_nsec - rhs->tv_nsec;
  109. }
  110. extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
  111. /*
  112. * timespec64_add_safe assumes both values are positive and checks for
  113. * overflow. It will return TIME_T_MAX if the returned value would be
  114. * smaller then either of the arguments.
  115. */
  116. extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
  117. const struct timespec64 rhs);
  118. static inline struct timespec64 timespec64_add(struct timespec64 lhs,
  119. struct timespec64 rhs)
  120. {
  121. struct timespec64 ts_delta;
  122. set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
  123. lhs.tv_nsec + rhs.tv_nsec);
  124. return ts_delta;
  125. }
  126. /*
  127. * sub = lhs - rhs, in normalized form
  128. */
  129. static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
  130. struct timespec64 rhs)
  131. {
  132. struct timespec64 ts_delta;
  133. set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
  134. lhs.tv_nsec - rhs.tv_nsec);
  135. return ts_delta;
  136. }
  137. /*
  138. * Returns true if the timespec64 is norm, false if denorm:
  139. */
  140. static inline bool timespec64_valid(const struct timespec64 *ts)
  141. {
  142. /* Dates before 1970 are bogus */
  143. if (ts->tv_sec < 0)
  144. return false;
  145. /* Can't have more nanoseconds then a second */
  146. if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  147. return false;
  148. return true;
  149. }
  150. static inline bool timespec64_valid_strict(const struct timespec64 *ts)
  151. {
  152. if (!timespec64_valid(ts))
  153. return false;
  154. /* Disallow values that could overflow ktime_t */
  155. if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
  156. return false;
  157. return true;
  158. }
  159. /**
  160. * timespec64_to_ns - Convert timespec64 to nanoseconds
  161. * @ts: pointer to the timespec64 variable to be converted
  162. *
  163. * Returns the scalar nanosecond representation of the timespec64
  164. * parameter.
  165. */
  166. static inline s64 timespec64_to_ns(const struct timespec64 *ts)
  167. {
  168. return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
  169. }
  170. /**
  171. * ns_to_timespec64 - Convert nanoseconds to timespec64
  172. * @nsec: the nanoseconds value to be converted
  173. *
  174. * Returns the timespec64 representation of the nsec parameter.
  175. */
  176. extern struct timespec64 ns_to_timespec64(const s64 nsec);
  177. /**
  178. * timespec64_add_ns - Adds nanoseconds to a timespec64
  179. * @a: pointer to timespec64 to be incremented
  180. * @ns: unsigned nanoseconds value to be added
  181. *
  182. * This must always be inlined because its used from the x86-64 vdso,
  183. * which cannot call other kernel functions.
  184. */
  185. static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
  186. {
  187. a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
  188. a->tv_nsec = ns;
  189. }
  190. #endif
  191. #endif /* _LINUX_TIME64_H */