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 
12 #include <linux/time64.h>
13 
14 #define TIME_T_MAX	(time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
15 
16 #if __BITS_PER_LONG == 64
17 
18 /* timespec64 is defined as timespec here */
timespec64_to_timespec(const struct timespec64 ts64)19 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
20 {
21 	return *(const struct timespec *)&ts64;
22 }
23 
timespec_to_timespec64(const struct timespec ts)24 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
25 {
26 	return *(const struct timespec64 *)&ts;
27 }
28 
29 #else
timespec64_to_timespec(const struct timespec64 ts64)30 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
31 {
32 	struct timespec ret;
33 
34 	ret.tv_sec = (time_t)ts64.tv_sec;
35 	ret.tv_nsec = ts64.tv_nsec;
36 	return ret;
37 }
38 
timespec_to_timespec64(const struct timespec ts)39 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
40 {
41 	struct timespec64 ret;
42 
43 	ret.tv_sec = ts.tv_sec;
44 	ret.tv_nsec = ts.tv_nsec;
45 	return ret;
46 }
47 #endif
48 
timespec_equal(const struct timespec * a,const struct timespec * b)49 static inline int timespec_equal(const struct timespec *a,
50 				 const struct timespec *b)
51 {
52 	return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
53 }
54 
55 /*
56  * lhs < rhs:  return <0
57  * lhs == rhs: return 0
58  * lhs > rhs:  return >0
59  */
timespec_compare(const struct timespec * lhs,const struct timespec * rhs)60 static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs)
61 {
62 	if (lhs->tv_sec < rhs->tv_sec)
63 		return -1;
64 	if (lhs->tv_sec > rhs->tv_sec)
65 		return 1;
66 	return lhs->tv_nsec - rhs->tv_nsec;
67 }
68 
69 extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec);
70 
timespec_add(struct timespec lhs,struct timespec rhs)71 static inline struct timespec timespec_add(struct timespec lhs,
72 						struct timespec rhs)
73 {
74 	struct timespec ts_delta;
75 
76 	set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec,
77 				lhs.tv_nsec + rhs.tv_nsec);
78 	return ts_delta;
79 }
80 
81 /*
82  * sub = lhs - rhs, in normalized form
83  */
timespec_sub(struct timespec lhs,struct timespec rhs)84 static inline struct timespec timespec_sub(struct timespec lhs,
85 						struct timespec rhs)
86 {
87 	struct timespec ts_delta;
88 
89 	set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec,
90 				lhs.tv_nsec - rhs.tv_nsec);
91 	return ts_delta;
92 }
93 
94 /*
95  * Returns true if the timespec is norm, false if denorm:
96  */
timespec_valid(const struct timespec * ts)97 static inline bool timespec_valid(const struct timespec *ts)
98 {
99 	/* Dates before 1970 are bogus */
100 	if (ts->tv_sec < 0)
101 		return false;
102 	/* Can't have more nanoseconds then a second */
103 	if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
104 		return false;
105 	return true;
106 }
107 
timespec_valid_strict(const struct timespec * ts)108 static inline bool timespec_valid_strict(const struct timespec *ts)
109 {
110 	if (!timespec_valid(ts))
111 		return false;
112 	/* Disallow values that could overflow ktime_t */
113 	if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
114 		return false;
115 	return true;
116 }
117 
118 /**
119  * timespec_to_ns - Convert timespec to nanoseconds
120  * @ts:		pointer to the timespec variable to be converted
121  *
122  * Returns the scalar nanosecond representation of the timespec
123  * parameter.
124  */
timespec_to_ns(const struct timespec * ts)125 static inline s64 timespec_to_ns(const struct timespec *ts)
126 {
127 	return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
128 }
129 
130 /**
131  * ns_to_timespec - Convert nanoseconds to timespec
132  * @nsec:	the nanoseconds value to be converted
133  *
134  * Returns the timespec representation of the nsec parameter.
135  */
136 extern struct timespec ns_to_timespec(const s64 nsec);
137 
138 /**
139  * timespec_add_ns - Adds nanoseconds to a timespec
140  * @a:		pointer to timespec to be incremented
141  * @ns:		unsigned nanoseconds value to be added
142  *
143  * This must always be inlined because its used from the x86-64 vdso,
144  * which cannot call other kernel functions.
145  */
timespec_add_ns(struct timespec * a,u64 ns)146 static __always_inline void timespec_add_ns(struct timespec *a, u64 ns)
147 {
148 	a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
149 	a->tv_nsec = ns;
150 }
151 
152 /**
153  * time_to_tm - converts the calendar time to local broken-down time
154  *
155  * @totalsecs	the number of seconds elapsed since 00:00:00 on January 1, 1970,
156  *		Coordinated Universal Time (UTC).
157  * @offset	offset seconds adding to totalsecs.
158  * @result	pointer to struct tm variable to receive broken-down time
159  */
time_to_tm(time_t totalsecs,int offset,struct tm * result)160 static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result)
161 {
162 	time64_to_tm(totalsecs, offset, result);
163 }
164 
mktime(const unsigned int year,const unsigned int mon,const unsigned int day,const unsigned int hour,const unsigned int min,const unsigned int sec)165 static inline unsigned long mktime(const unsigned int year,
166 			const unsigned int mon, const unsigned int day,
167 			const unsigned int hour, const unsigned int min,
168 			const unsigned int sec)
169 {
170 	return mktime64(year, mon, day, hour, min, sec);
171 }
172 
timeval_valid(const struct timeval * tv)173 static inline bool timeval_valid(const struct timeval *tv)
174 {
175 	/* Dates before 1970 are bogus */
176 	if (tv->tv_sec < 0)
177 		return false;
178 
179 	/* Can't have more microseconds then a second */
180 	if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
181 		return false;
182 
183 	return true;
184 }
185 
186 extern struct timespec timespec_trunc(struct timespec t, unsigned int gran);
187 
188 /**
189  * timeval_to_ns - Convert timeval to nanoseconds
190  * @ts:		pointer to the timeval variable to be converted
191  *
192  * Returns the scalar nanosecond representation of the timeval
193  * parameter.
194  */
timeval_to_ns(const struct timeval * tv)195 static inline s64 timeval_to_ns(const struct timeval *tv)
196 {
197 	return ((s64) tv->tv_sec * NSEC_PER_SEC) +
198 		tv->tv_usec * NSEC_PER_USEC;
199 }
200 
201 /**
202  * ns_to_timeval - Convert nanoseconds to timeval
203  * @nsec:	the nanoseconds value to be converted
204  *
205  * Returns the timeval representation of the nsec parameter.
206  */
207 extern struct timeval ns_to_timeval(const s64 nsec);
208 extern struct __kernel_old_timeval ns_to_kernel_old_timeval(s64 nsec);
209 
210 /*
211  * New aliases for compat time functions. These will be used to replace
212  * the compat code so it can be shared between 32-bit and 64-bit builds
213  * both of which provide compatibility with old 32-bit tasks.
214  */
215 #define old_time32_t		compat_time_t
216 #define old_timeval32		compat_timeval
217 #define old_timespec32		compat_timespec
218 #define old_itimerspec32	compat_itimerspec
219 #define ns_to_old_timeval32	ns_to_compat_timeval
220 #define get_old_itimerspec32	get_compat_itimerspec64
221 #define put_old_itimerspec32	put_compat_itimerspec64
222 #define get_old_timespec32	compat_get_timespec64
223 #define put_old_timespec32	compat_put_timespec64
224 
225 #endif
226