time32.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #ifndef _LINUX_TIME32_H
  2. #define _LINUX_TIME32_H
  3. /*
  4. * These are all interfaces based on the old time_t definition
  5. * that overflows in 2038 on 32-bit architectures. New code
  6. * should use the replacements based on time64_t and timespec64.
  7. *
  8. * Any interfaces in here that become unused as we migrate
  9. * code to time64_t should get removed.
  10. */
  11. #include <linux/time64.h>
  12. #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
  13. typedef s32 old_time32_t;
  14. struct old_timespec32 {
  15. old_time32_t tv_sec;
  16. s32 tv_nsec;
  17. };
  18. struct old_timeval32 {
  19. old_time32_t tv_sec;
  20. s32 tv_usec;
  21. };
  22. struct old_itimerspec32 {
  23. struct old_timespec32 it_interval;
  24. struct old_timespec32 it_value;
  25. };
  26. extern int get_old_timespec32(struct timespec64 *, const void __user *);
  27. extern int put_old_timespec32(const struct timespec64 *, void __user *);
  28. extern int get_old_itimerspec32(struct itimerspec64 *its,
  29. const struct old_itimerspec32 __user *uits);
  30. extern int put_old_itimerspec32(const struct itimerspec64 *its,
  31. struct old_itimerspec32 __user *uits);
  32. #if __BITS_PER_LONG == 64
  33. /* timespec64 is defined as timespec here */
  34. static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
  35. {
  36. return *(const struct timespec *)&ts64;
  37. }
  38. static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
  39. {
  40. return *(const struct timespec64 *)&ts;
  41. }
  42. #else
  43. static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
  44. {
  45. struct timespec ret;
  46. ret.tv_sec = (time_t)ts64.tv_sec;
  47. ret.tv_nsec = ts64.tv_nsec;
  48. return ret;
  49. }
  50. static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
  51. {
  52. struct timespec64 ret;
  53. ret.tv_sec = ts.tv_sec;
  54. ret.tv_nsec = ts.tv_nsec;
  55. return ret;
  56. }
  57. #endif
  58. static inline int timespec_equal(const struct timespec *a,
  59. const struct timespec *b)
  60. {
  61. return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
  62. }
  63. /*
  64. * lhs < rhs: return <0
  65. * lhs == rhs: return 0
  66. * lhs > rhs: return >0
  67. */
  68. static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs)
  69. {
  70. if (lhs->tv_sec < rhs->tv_sec)
  71. return -1;
  72. if (lhs->tv_sec > rhs->tv_sec)
  73. return 1;
  74. return lhs->tv_nsec - rhs->tv_nsec;
  75. }
  76. extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec);
  77. static inline struct timespec timespec_add(struct timespec lhs,
  78. struct timespec rhs)
  79. {
  80. struct timespec ts_delta;
  81. set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec,
  82. lhs.tv_nsec + rhs.tv_nsec);
  83. return ts_delta;
  84. }
  85. /*
  86. * sub = lhs - rhs, in normalized form
  87. */
  88. static inline struct timespec timespec_sub(struct timespec lhs,
  89. struct timespec rhs)
  90. {
  91. struct timespec ts_delta;
  92. set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec,
  93. lhs.tv_nsec - rhs.tv_nsec);
  94. return ts_delta;
  95. }
  96. /*
  97. * Returns true if the timespec is norm, false if denorm:
  98. */
  99. static inline bool timespec_valid(const struct timespec *ts)
  100. {
  101. /* Dates before 1970 are bogus */
  102. if (ts->tv_sec < 0)
  103. return false;
  104. /* Can't have more nanoseconds then a second */
  105. if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  106. return false;
  107. return true;
  108. }
  109. /**
  110. * timespec_to_ns - Convert timespec to nanoseconds
  111. * @ts: pointer to the timespec variable to be converted
  112. *
  113. * Returns the scalar nanosecond representation of the timespec
  114. * parameter.
  115. */
  116. static inline s64 timespec_to_ns(const struct timespec *ts)
  117. {
  118. return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
  119. }
  120. /**
  121. * ns_to_timespec - Convert nanoseconds to timespec
  122. * @nsec: the nanoseconds value to be converted
  123. *
  124. * Returns the timespec representation of the nsec parameter.
  125. */
  126. extern struct timespec ns_to_timespec(const s64 nsec);
  127. /**
  128. * timespec_add_ns - Adds nanoseconds to a timespec
  129. * @a: pointer to timespec to be incremented
  130. * @ns: unsigned nanoseconds value to be added
  131. *
  132. * This must always be inlined because its used from the x86-64 vdso,
  133. * which cannot call other kernel functions.
  134. */
  135. static __always_inline void timespec_add_ns(struct timespec *a, u64 ns)
  136. {
  137. a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
  138. a->tv_nsec = ns;
  139. }
  140. static inline unsigned long mktime(const unsigned int year,
  141. const unsigned int mon, const unsigned int day,
  142. const unsigned int hour, const unsigned int min,
  143. const unsigned int sec)
  144. {
  145. return mktime64(year, mon, day, hour, min, sec);
  146. }
  147. static inline bool timeval_valid(const struct timeval *tv)
  148. {
  149. /* Dates before 1970 are bogus */
  150. if (tv->tv_sec < 0)
  151. return false;
  152. /* Can't have more microseconds then a second */
  153. if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
  154. return false;
  155. return true;
  156. }
  157. /**
  158. * timeval_to_ns - Convert timeval to nanoseconds
  159. * @ts: pointer to the timeval variable to be converted
  160. *
  161. * Returns the scalar nanosecond representation of the timeval
  162. * parameter.
  163. */
  164. static inline s64 timeval_to_ns(const struct timeval *tv)
  165. {
  166. return ((s64) tv->tv_sec * NSEC_PER_SEC) +
  167. tv->tv_usec * NSEC_PER_USEC;
  168. }
  169. /**
  170. * ns_to_timeval - Convert nanoseconds to timeval
  171. * @nsec: the nanoseconds value to be converted
  172. *
  173. * Returns the timeval representation of the nsec parameter.
  174. */
  175. extern struct timeval ns_to_timeval(const s64 nsec);
  176. extern struct __kernel_old_timeval ns_to_kernel_old_timeval(s64 nsec);
  177. /*
  178. * Old names for the 32-bit time_t interfaces, these will be removed
  179. * when everything uses the new names.
  180. */
  181. #define compat_time_t old_time32_t
  182. #define compat_timeval old_timeval32
  183. #define compat_timespec old_timespec32
  184. #define compat_itimerspec old_itimerspec32
  185. #define ns_to_compat_timeval ns_to_old_timeval32
  186. #define get_compat_itimerspec64 get_old_itimerspec32
  187. #define put_compat_itimerspec64 put_old_itimerspec32
  188. #define compat_get_timespec64 get_old_timespec32
  189. #define compat_put_timespec64 put_old_timespec32
  190. #endif