Whamcloud - gitweb
LU-13590 kernel: RHEL 7.9 server support
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-time.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/include/libcfs/linux/linux-time.h
33  *
34  * Implementation of portable time API for Linux (kernel and user-level).
35  *
36  * Author: Nikita Danilov <nikita@clusterfs.com>
37  */
38
39 #ifndef __LIBCFS_LINUX_LINUX_TIME_H__
40 #define __LIBCFS_LINUX_LINUX_TIME_H__
41
42 /* Portable time API */
43 #include <linux/hrtimer.h>
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/version.h>
47 #include <linux/jiffies.h>
48 #include <linux/hrtimer.h>
49 #include <linux/types.h>
50 #include <linux/time.h>
51 #include <asm/div64.h>
52
53 /*
54  * Generic kernel stuff
55  */
56 #ifndef HAVE_TIMESPEC64
57
58 typedef __s64 time64_t;
59
60 #if __BITS_PER_LONG == 64
61
62 # define timespec64 timespec
63
64 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
65 {
66         return ts;
67 }
68
69 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts)
70 {
71         return ts;
72 }
73
74 #else
75 struct timespec64 {
76         time64_t        tv_sec;         /* seconds */
77         long            tv_nsec;        /* nanoseconds */
78 };
79
80 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
81 {
82         struct timespec64 ret;
83
84         ret.tv_sec = ts.tv_sec;
85         ret.tv_nsec = ts.tv_nsec;
86         return ret;
87 }
88
89 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
90 {
91         struct timespec ret;
92
93         ret.tv_sec = (time_t)ts64.tv_sec;
94         ret.tv_nsec = ts64.tv_nsec;
95         return ret;
96 }
97 #endif /* __BITS_PER_LONG != 64 */
98
99 #endif /* HAVE_TIMESPEC64 */
100
101 #ifndef HAVE_NS_TO_TIMESPEC64
102 static inline struct timespec64 ns_to_timespec64(const s64 nsec)
103 {
104         struct timespec64 ts;
105         s32 rem;
106
107         if (!nsec)
108                 return (struct timespec64) {0, 0};
109
110         ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
111         if (unlikely(rem < 0)) {
112                 ts.tv_sec--;
113                 rem += NSEC_PER_SEC;
114         }
115         ts.tv_nsec = rem;
116
117         return ts;
118 }
119 #endif
120
121 #ifndef HAVE_KTIME_ADD
122 # define ktime_add(lhs, rhs) ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
123 #endif /* !HAVE_KTIME_ADD */
124
125 #ifndef HAVE_KTIME_AFTER
126 static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
127 {
128         return cmp1.tv64 > cmp2.tv64;
129 }
130 #endif /* !HAVE_KTIME_AFTER */
131
132 #ifndef HAVE_KTIME_BEFORE
133 static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
134 {
135         return cmp1.tv64 < cmp2.tv64;
136 }
137 #endif /* !HAVE_KTIME_BEFORE */
138
139 #ifndef HAVE_KTIME_COMPARE
140 static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
141 {
142         if (cmp1.tv64 < cmp2.tv64)
143                 return -1;
144         if (cmp1.tv64 > cmp2.tv64)
145                 return 1;
146         return 0;
147 }
148 #endif /* !HAVE_KTIME_COMPARE */
149
150 #ifndef HAVE_KTIME_GET_TS64
151 void ktime_get_ts64(struct timespec64 *ts);
152 #endif /* HAVE_KTIME_GET_TS */
153
154 #ifndef HAVE_KTIME_GET_REAL_TS64
155 void ktime_get_real_ts64(struct timespec64 *ts);
156 #endif /* HAVE_KTIME_GET_REAL_TS */
157
158 #ifndef HAVE_KTIME_GET_REAL_SECONDS
159 time64_t ktime_get_real_seconds(void);
160 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
161
162 #ifndef HAVE_KTIME_GET_SECONDS
163 time64_t ktime_get_seconds(void);
164 #endif /* HAVE_KTIME_GET_SECONDS */
165
166 #ifdef NEED_KTIME_GET_NS
167 static inline u64 ktime_get_ns(void)
168 {
169         return ktime_to_ns(ktime_get());
170 }
171 #endif /* NEED_KTIME_GET_NS */
172
173 #ifdef NEED_KTIME_GET_REAL_NS
174 static inline u64 ktime_get_real_ns(void)
175 {
176         return ktime_to_ns(ktime_get_real());
177 }
178 #endif /* NEED_KTIME_GET_REAL_NS */
179
180 #ifndef HAVE_KTIME_MS_DELTA
181 static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
182 {
183         return ktime_to_ms(ktime_sub(later, earlier));
184 }
185 #endif /* HAVE_KTIME_MS_DELTA */
186
187 #ifndef HAVE_KTIME_TO_TIMESPEC64
188 static inline struct timespec64 ktime_to_timespec64(ktime_t kt)
189 {
190         struct timespec ts = ns_to_timespec((kt).tv64);
191
192         return timespec_to_timespec64(ts);
193 }
194 #endif /* HAVE_KTIME_TO_TIMESPEC64 */
195
196 #ifndef HAVE_TIMESPEC64_SUB
197 static inline struct timespec64
198 timespec64_sub(struct timespec64 later, struct timespec64 earlier)
199 {
200         struct timespec diff;
201
202         diff = timespec_sub(timespec64_to_timespec(later),
203                             timespec64_to_timespec(earlier));
204         return timespec_to_timespec64(diff);
205 }
206 #endif
207
208 #ifndef HAVE_TIMESPEC64_TO_KTIME
209 static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
210 {
211         return ktime_set(ts.tv_sec, ts.tv_nsec);
212 }
213 #endif
214
215 static inline unsigned long cfs_time_seconds(time64_t seconds)
216 {
217         return nsecs_to_jiffies(seconds * NSEC_PER_SEC);
218 }
219
220 #ifdef HAVE_NEW_DEFINE_TIMER
221 # ifndef TIMER_DATA_TYPE
222 # define TIMER_DATA_TYPE struct timer_list *
223 # endif
224
225 #define CFS_DEFINE_TIMER(_name, _function, _expires, _data) \
226         DEFINE_TIMER((_name), (_function))
227 #else
228 # ifndef TIMER_DATA_TYPE
229 # define TIMER_DATA_TYPE unsigned long
230 # endif
231
232 #define CFS_DEFINE_TIMER(_name, _function, _expires, _data) \
233         DEFINE_TIMER((_name), (_function), (_expires), (_data))
234 #endif
235
236 #ifdef HAVE_TIMER_SETUP
237 #define cfs_timer_cb_arg_t struct timer_list *
238 #define cfs_from_timer(var, callback_timer, timer_fieldname) \
239         from_timer(var, callback_timer, timer_fieldname)
240 #define cfs_timer_setup(timer, callback, data, flags) \
241         timer_setup((timer), (callback), (flags))
242 #define cfs_timer_cb_arg(var, timer_fieldname) (&(var)->timer_fieldname)
243 #else
244 #define cfs_timer_cb_arg_t unsigned long
245 #define cfs_from_timer(var, data, timer_fieldname) (typeof(var))(data)
246 #define cfs_timer_setup(timer, callback, data, flags) \
247         setup_timer((timer), (callback), (data))
248 #define cfs_timer_cb_arg(var, timer_fieldname) (cfs_timer_cb_arg_t)(var)
249 #endif
250
251 #endif /* __LIBCFS_LINUX_LINUX_TIME_H__ */