blob: ec1888cf5378408d203ad5325d123a75493bd7e7 [file] [log] [blame]
John Stultz361a3bf2014-07-16 21:03:58 +00001#ifndef _LINUX_TIME64_H
2#define _LINUX_TIME64_H
3
4#include <uapi/linux/time.h>
Xunlei Pang30f3b3f2015-04-09 09:04:40 +08005#include <linux/math64.h>
John Stultz361a3bf2014-07-16 21:03:58 +00006
7typedef __s64 time64_t;
Vegard Nossum469e857f32016-08-12 20:14:09 +02008typedef __u64 timeu64_t;
John Stultz361a3bf2014-07-16 21:03:58 +00009
John Stultz361a3bf2014-07-16 21:03:58 +000010#if __BITS_PER_LONG == 64
Arnd Bergmannabc8f962017-10-19 13:14:48 +020011/* this trick allows us to optimize out timespec64_to_timespec */
John Stultz361a3bf2014-07-16 21:03:58 +000012# define timespec64 timespec
Baolin Wang19a46fe2015-07-29 19:58:15 +080013#define itimerspec64 itimerspec
John Stultz361a3bf2014-07-16 21:03:58 +000014#else
15struct timespec64 {
16 time64_t tv_sec; /* seconds */
17 long tv_nsec; /* nanoseconds */
18};
Baolin Wang19a46fe2015-07-29 19:58:15 +080019
20struct itimerspec64 {
21 struct timespec64 it_interval;
22 struct timespec64 it_value;
23};
24
John Stultz361a3bf2014-07-16 21:03:58 +000025#endif
26
27/* Parameters used to convert the timespec values: */
28#define MSEC_PER_SEC 1000L
29#define USEC_PER_MSEC 1000L
30#define NSEC_PER_USEC 1000L
31#define NSEC_PER_MSEC 1000000L
32#define USEC_PER_SEC 1000000L
33#define NSEC_PER_SEC 1000000000L
34#define FSEC_PER_SEC 1000000000000000LL
35
36/* Located here for timespec[64]_valid_strict */
John Stultz833f32d2015-06-11 15:54:55 -070037#define TIME64_MAX ((s64)~((u64)1 << 63))
John Stultz361a3bf2014-07-16 21:03:58 +000038#define KTIME_MAX ((s64)~((u64)1 << 63))
39#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
40
John Stultz361a3bf2014-07-16 21:03:58 +000041static inline int timespec64_equal(const struct timespec64 *a,
42 const struct timespec64 *b)
43{
44 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
45}
46
47/*
48 * lhs < rhs: return <0
49 * lhs == rhs: return 0
50 * lhs > rhs: return >0
51 */
52static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
53{
54 if (lhs->tv_sec < rhs->tv_sec)
55 return -1;
56 if (lhs->tv_sec > rhs->tv_sec)
57 return 1;
58 return lhs->tv_nsec - rhs->tv_nsec;
59}
60
61extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
62
John Stultz361a3bf2014-07-16 21:03:58 +000063static inline struct timespec64 timespec64_add(struct timespec64 lhs,
64 struct timespec64 rhs)
65{
66 struct timespec64 ts_delta;
67 set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
68 lhs.tv_nsec + rhs.tv_nsec);
69 return ts_delta;
70}
71
72/*
73 * sub = lhs - rhs, in normalized form
74 */
75static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
76 struct timespec64 rhs)
77{
78 struct timespec64 ts_delta;
79 set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
80 lhs.tv_nsec - rhs.tv_nsec);
81 return ts_delta;
82}
83
84/*
85 * Returns true if the timespec64 is norm, false if denorm:
86 */
87static inline bool timespec64_valid(const struct timespec64 *ts)
88{
89 /* Dates before 1970 are bogus */
90 if (ts->tv_sec < 0)
91 return false;
92 /* Can't have more nanoseconds then a second */
93 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
94 return false;
95 return true;
96}
97
98static inline bool timespec64_valid_strict(const struct timespec64 *ts)
99{
100 if (!timespec64_valid(ts))
101 return false;
102 /* Disallow values that could overflow ktime_t */
103 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
104 return false;
105 return true;
106}
107
108/**
109 * timespec64_to_ns - Convert timespec64 to nanoseconds
110 * @ts: pointer to the timespec64 variable to be converted
111 *
112 * Returns the scalar nanosecond representation of the timespec64
113 * parameter.
114 */
115static inline s64 timespec64_to_ns(const struct timespec64 *ts)
116{
117 return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
118}
119
120/**
121 * ns_to_timespec64 - Convert nanoseconds to timespec64
122 * @nsec: the nanoseconds value to be converted
123 *
124 * Returns the timespec64 representation of the nsec parameter.
125 */
126extern struct timespec64 ns_to_timespec64(const s64 nsec);
127
128/**
129 * timespec64_add_ns - Adds nanoseconds to a timespec64
130 * @a: pointer to timespec64 to be incremented
131 * @ns: unsigned nanoseconds value to be added
132 *
133 * This must always be inlined because its used from the x86-64 vdso,
134 * which cannot call other kernel functions.
135 */
136static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
137{
138 a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
139 a->tv_nsec = ns;
140}
141
Deepa Dinamani8e4f70e2016-05-19 17:09:08 -0700142/*
143 * timespec64_add_safe assumes both values are positive and checks for
144 * overflow. It will return TIME64_MAX in case of overflow.
145 */
146extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
147 const struct timespec64 rhs);
148
John Stultz361a3bf2014-07-16 21:03:58 +0000149#endif /* _LINUX_TIME64_H */