]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/time64.h
Update bcachefs sources to 14ce2a2031 bcachefs: fixes for building in userspace
[bcachefs-tools-debian] / include / linux / time64.h
1 #ifndef _LINUX_TIME64_H
2 #define _LINUX_TIME64_H
3
4 #include <linux/types.h>
5
6 typedef __s64 time64_t;
7
8 /*
9  * This wants to go into uapi/linux/time.h once we agreed about the
10  * userspace interfaces.
11  */
12 #if __BITS_PER_LONG == 64
13 # define timespec64 timespec
14 #else
15 struct timespec64 {
16         time64_t        tv_sec;                 /* seconds */
17         long            tv_nsec;                /* nanoseconds */
18 };
19
20 struct itimerspec64 {
21         struct timespec64 it_interval;
22         struct timespec64 it_value;
23 };
24
25 #endif
26
27 /* Parameters used to convert the timespec values: */
28 #define MSEC_PER_SEC    1000L
29 #define USEC_PER_MSEC   1000L
30 #define NSEC_PER_USEC   1000L
31 #define NSEC_PER_MSEC   1000000L
32 #define USEC_PER_SEC    1000000L
33 #define NSEC_PER_SEC    1000000000L
34 #define FSEC_PER_SEC    1000000000000000LL
35
36 /* Located here for timespec[64]_valid_strict */
37 #define TIME64_MAX                      ((s64)~((u64)1 << 63))
38 #define KTIME_MAX                       ((s64)~((u64)1 << 63))
39 #define KTIME_SEC_MAX                   (KTIME_MAX / NSEC_PER_SEC)
40
41 static inline struct timespec ns_to_timespec(const u64 nsec)
42 {
43         return (struct timespec) {
44                 .tv_sec = nsec / NSEC_PER_SEC,
45                 .tv_nsec = nsec % NSEC_PER_SEC,
46         };
47 }
48
49 static inline s64 timespec_to_ns(const struct timespec *ts)
50 {
51         return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
52 }
53
54 #if __BITS_PER_LONG == 64
55
56 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
57 {
58         return ts64;
59 }
60
61 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
62 {
63         return ts;
64 }
65
66 # define timespec64_equal               timespec_equal
67 # define timespec64_compare             timespec_compare
68 # define set_normalized_timespec64      set_normalized_timespec
69 # define timespec64_add                 timespec_add
70 # define timespec64_sub                 timespec_sub
71 # define timespec64_valid               timespec_valid
72 # define timespec64_valid_strict        timespec_valid_strict
73 # define timespec64_to_ns               timespec_to_ns
74 # define ns_to_timespec64               ns_to_timespec
75 # define timespec64_add_ns              timespec_add_ns
76
77 #else
78
79 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
80 {
81         struct timespec ret;
82
83         ret.tv_sec = (time_t)ts64.tv_sec;
84         ret.tv_nsec = ts64.tv_nsec;
85         return ret;
86 }
87
88 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
89 {
90         struct timespec64 ret;
91
92         ret.tv_sec = ts.tv_sec;
93         ret.tv_nsec = ts.tv_nsec;
94         return ret;
95 }
96
97 static inline int timespec64_equal(const struct timespec64 *a,
98                                    const struct timespec64 *b)
99 {
100         return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
101 }
102
103 /*
104  * lhs < rhs:  return <0
105  * lhs == rhs: return 0
106  * lhs > rhs:  return >0
107  */
108 static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
109 {
110         if (lhs->tv_sec < rhs->tv_sec)
111                 return -1;
112         if (lhs->tv_sec > rhs->tv_sec)
113                 return 1;
114         return lhs->tv_nsec - rhs->tv_nsec;
115 }
116
117 extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
118
119 static inline struct timespec64 timespec64_add(struct timespec64 lhs,
120                                                 struct timespec64 rhs)
121 {
122         struct timespec64 ts_delta;
123         set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
124                                 lhs.tv_nsec + rhs.tv_nsec);
125         return ts_delta;
126 }
127
128 /*
129  * sub = lhs - rhs, in normalized form
130  */
131 static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
132                                                 struct timespec64 rhs)
133 {
134         struct timespec64 ts_delta;
135         set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
136                                 lhs.tv_nsec - rhs.tv_nsec);
137         return ts_delta;
138 }
139
140 /*
141  * Returns true if the timespec64 is norm, false if denorm:
142  */
143 static inline bool timespec64_valid(const struct timespec64 *ts)
144 {
145         /* Dates before 1970 are bogus */
146         if (ts->tv_sec < 0)
147                 return false;
148         /* Can't have more nanoseconds then a second */
149         if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
150                 return false;
151         return true;
152 }
153
154 static inline bool timespec64_valid_strict(const struct timespec64 *ts)
155 {
156         if (!timespec64_valid(ts))
157                 return false;
158         /* Disallow values that could overflow ktime_t */
159         if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
160                 return false;
161         return true;
162 }
163
164 /**
165  * timespec64_to_ns - Convert timespec64 to nanoseconds
166  * @ts:         pointer to the timespec64 variable to be converted
167  *
168  * Returns the scalar nanosecond representation of the timespec64
169  * parameter.
170  */
171 static inline s64 timespec64_to_ns(const struct timespec64 *ts)
172 {
173         return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
174 }
175
176 /**
177  * ns_to_timespec64 - Convert nanoseconds to timespec64
178  * @nsec:       the nanoseconds value to be converted
179  *
180  * Returns the timespec64 representation of the nsec parameter.
181  */
182 extern struct timespec64 ns_to_timespec64(const s64 nsec);
183
184 /**
185  * timespec64_add_ns - Adds nanoseconds to a timespec64
186  * @a:          pointer to timespec64 to be incremented
187  * @ns:         unsigned nanoseconds value to be added
188  *
189  * This must always be inlined because its used from the x86-64 vdso,
190  * which cannot call other kernel functions.
191  */
192 static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
193 {
194         a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
195         a->tv_nsec = ns;
196 }
197
198 #endif
199
200 /*
201  * timespec64_add_safe assumes both values are positive and checks for
202  * overflow. It will return TIME64_MAX in case of overflow.
203  */
204 extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
205                                          const struct timespec64 rhs);
206
207 static inline struct timespec timespec_trunc(struct timespec t, unsigned gran)
208 {
209         /* Avoid division in the common cases 1 ns and 1 s. */
210         if (gran == 1) {
211                 /* nothing */
212         } else if (gran == NSEC_PER_SEC) {
213                 t.tv_nsec = 0;
214         } else if (gran > 1 && gran < NSEC_PER_SEC) {
215                 t.tv_nsec -= t.tv_nsec % gran;
216         } else {
217                 WARN(1, "illegal file time granularity: %u", gran);
218         }
219         return t;
220 }
221
222 #endif /* _LINUX_TIME64_H */