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