blob: ad33260618f766e8c2814530dc177961c8be485e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
John Stultz361a3bf2014-07-16 21:03:58 +00002#ifndef _LINUX_TIME64_H
3#define _LINUX_TIME64_H
4
5#include <uapi/linux/time.h>
Xunlei Pang30f3b3f2015-04-09 09:04:40 +08006#include <linux/math64.h>
John Stultz361a3bf2014-07-16 21:03:58 +00007
8typedef __s64 time64_t;
Vegard Nossum469e857f32016-08-12 20:14:09 +02009typedef __u64 timeu64_t;
John Stultz361a3bf2014-07-16 21:03:58 +000010
11/*
12 * This wants to go into uapi/linux/time.h once we agreed about the
13 * userspace interfaces.
14 */
15#if __BITS_PER_LONG == 64
16# define timespec64 timespec
Baolin Wang19a46fe2015-07-29 19:58:15 +080017#define itimerspec64 itimerspec
John Stultz361a3bf2014-07-16 21:03:58 +000018#else
19struct timespec64 {
20 time64_t tv_sec; /* seconds */
21 long tv_nsec; /* nanoseconds */
22};
Baolin Wang19a46fe2015-07-29 19:58:15 +080023
24struct itimerspec64 {
25 struct timespec64 it_interval;
26 struct timespec64 it_value;
27};
28
John Stultz361a3bf2014-07-16 21:03:58 +000029#endif
30
31/* Parameters used to convert the timespec values: */
32#define MSEC_PER_SEC 1000L
33#define USEC_PER_MSEC 1000L
34#define NSEC_PER_USEC 1000L
35#define NSEC_PER_MSEC 1000000L
36#define USEC_PER_SEC 1000000L
37#define NSEC_PER_SEC 1000000000L
38#define FSEC_PER_SEC 1000000000000000LL
39
40/* Located here for timespec[64]_valid_strict */
John Stultz833f32d2015-06-11 15:54:55 -070041#define TIME64_MAX ((s64)~((u64)1 << 63))
John Stultz361a3bf2014-07-16 21:03:58 +000042#define KTIME_MAX ((s64)~((u64)1 << 63))
43#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
44
45#if __BITS_PER_LONG == 64
46
John Stultz49cd6f82014-07-16 21:03:59 +000047static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
48{
49 return ts64;
50}
51
52static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
53{
54 return ts;
55}
56
Baolin Wang19a46fe2015-07-29 19:58:15 +080057static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
58{
59 return *its64;
60}
61
62static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
63{
64 return *its;
65}
66
John Stultz361a3bf2014-07-16 21:03:58 +000067# define timespec64_equal timespec_equal
68# define timespec64_compare timespec_compare
69# define set_normalized_timespec64 set_normalized_timespec
John Stultz361a3bf2014-07-16 21:03:58 +000070# define timespec64_add timespec_add
71# define timespec64_sub timespec_sub
72# define timespec64_valid timespec_valid
73# define timespec64_valid_strict timespec_valid_strict
74# define timespec64_to_ns timespec_to_ns
75# define ns_to_timespec64 ns_to_timespec
76# define timespec64_add_ns timespec_add_ns
77
78#else
79
John Stultz49cd6f82014-07-16 21:03:59 +000080static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
81{
82 struct timespec ret;
83
84 ret.tv_sec = (time_t)ts64.tv_sec;
85 ret.tv_nsec = ts64.tv_nsec;
86 return ret;
87}
88
89static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
90{
91 struct timespec64 ret;
92
93 ret.tv_sec = ts.tv_sec;
94 ret.tv_nsec = ts.tv_nsec;
95 return ret;
96}
97
Baolin Wang19a46fe2015-07-29 19:58:15 +080098static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
99{
100 struct itimerspec ret;
101
102 ret.it_interval = timespec64_to_timespec(its64->it_interval);
103 ret.it_value = timespec64_to_timespec(its64->it_value);
104 return ret;
105}
106
107static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
108{
109 struct itimerspec64 ret;
110
111 ret.it_interval = timespec_to_timespec64(its->it_interval);
112 ret.it_value = timespec_to_timespec64(its->it_value);
113 return ret;
114}
115
John Stultz361a3bf2014-07-16 21:03:58 +0000116static inline int timespec64_equal(const struct timespec64 *a,
117 const struct timespec64 *b)
118{
119 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
120}
121
122/*
123 * lhs < rhs: return <0
124 * lhs == rhs: return 0
125 * lhs > rhs: return >0
126 */
127static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
128{
129 if (lhs->tv_sec < rhs->tv_sec)
130 return -1;
131 if (lhs->tv_sec > rhs->tv_sec)
132 return 1;
133 return lhs->tv_nsec - rhs->tv_nsec;
134}
135
136extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
137
John Stultz361a3bf2014-07-16 21:03:58 +0000138static inline struct timespec64 timespec64_add(struct timespec64 lhs,
139 struct timespec64 rhs)
140{
141 struct timespec64 ts_delta;
142 set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
143 lhs.tv_nsec + rhs.tv_nsec);
144 return ts_delta;
145}
146
147/*
148 * sub = lhs - rhs, in normalized form
149 */
150static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
151 struct timespec64 rhs)
152{
153 struct timespec64 ts_delta;
154 set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
155 lhs.tv_nsec - rhs.tv_nsec);
156 return ts_delta;
157}
158
159/*
160 * Returns true if the timespec64 is norm, false if denorm:
161 */
162static inline bool timespec64_valid(const struct timespec64 *ts)
163{
164 /* Dates before 1970 are bogus */
165 if (ts->tv_sec < 0)
166 return false;
167 /* Can't have more nanoseconds then a second */
168 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
169 return false;
170 return true;
171}
172
173static inline bool timespec64_valid_strict(const struct timespec64 *ts)
174{
175 if (!timespec64_valid(ts))
176 return false;
177 /* Disallow values that could overflow ktime_t */
178 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
179 return false;
180 return true;
181}
182
183/**
184 * timespec64_to_ns - Convert timespec64 to nanoseconds
185 * @ts: pointer to the timespec64 variable to be converted
186 *
187 * Returns the scalar nanosecond representation of the timespec64
188 * parameter.
189 */
190static inline s64 timespec64_to_ns(const struct timespec64 *ts)
191{
192 return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
193}
194
195/**
196 * ns_to_timespec64 - Convert nanoseconds to timespec64
197 * @nsec: the nanoseconds value to be converted
198 *
199 * Returns the timespec64 representation of the nsec parameter.
200 */
201extern struct timespec64 ns_to_timespec64(const s64 nsec);
202
203/**
204 * timespec64_add_ns - Adds nanoseconds to a timespec64
205 * @a: pointer to timespec64 to be incremented
206 * @ns: unsigned nanoseconds value to be added
207 *
208 * This must always be inlined because its used from the x86-64 vdso,
209 * which cannot call other kernel functions.
210 */
211static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
212{
213 a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
214 a->tv_nsec = ns;
215}
216
217#endif
218
Deepa Dinamani8e4f70e2016-05-19 17:09:08 -0700219/*
220 * timespec64_add_safe assumes both values are positive and checks for
221 * overflow. It will return TIME64_MAX in case of overflow.
222 */
223extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
224 const struct timespec64 rhs);
225
John Stultz361a3bf2014-07-16 21:03:58 +0000226#endif /* _LINUX_TIME64_H */