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