]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/kernel.h
Update bcachefs sources to 3ca08ab51ec9 bcachefs: six locks: Simplify optimistic...
[bcachefs-tools-debian] / include / linux / kernel.h
1 #ifndef __TOOLS_LINUX_KERNEL_H
2 #define __TOOLS_LINUX_KERNEL_H
3
4 #include <assert.h>
5 #include <errno.h>
6 #include <stdarg.h>
7 #include <stddef.h>
8 #include <stdio.h>
9 #include <limits.h>
10
11 #include <linux/bug.h>
12 #include <linux/byteorder.h>
13 #include <linux/compiler.h>
14 #include <linux/math.h>
15
16 #define BIT(nr)                 (1UL << (nr))
17 #define BIT_ULL(nr)             (1ULL << (nr))
18
19 #define __ARG_PLACEHOLDER_1 0,
20 #define __take_second_arg(__ignored, val, ...) val
21
22 #define __and(x, y)                     ___and(x, y)
23 #define ___and(x, y)                    ____and(__ARG_PLACEHOLDER_##x, y)
24 #define ____and(arg1_or_junk, y)        __take_second_arg(arg1_or_junk y, 0)
25
26 #define __or(x, y)                      ___or(x, y)
27 #define ___or(x, y)                     ____or(__ARG_PLACEHOLDER_##x, y)
28 #define ____or(arg1_or_junk, y)         __take_second_arg(arg1_or_junk 1, y)
29
30 #define __is_defined(x)                 ___is_defined(x)
31 #define ___is_defined(val)              ____is_defined(__ARG_PLACEHOLDER_##val)
32 #define ____is_defined(arg1_or_junk)    __take_second_arg(arg1_or_junk 1, 0)
33
34 /*
35  * IS_BUILTIN(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y', 0
36  * otherwise. For boolean options, this is equivalent to
37  * IS_ENABLED(CONFIG_FOO).
38  */
39 #define IS_BUILTIN(option) __is_defined(option)
40
41 /*
42  * IS_MODULE(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'm', 0
43  * otherwise.
44  */
45 #define IS_MODULE(option) __is_defined(option##_MODULE)
46
47 /*
48  * IS_REACHABLE(CONFIG_FOO) evaluates to 1 if the currently compiled
49  * code can call a function defined in code compiled based on CONFIG_FOO.
50  * This is similar to IS_ENABLED(), but returns false when invoked from
51  * built-in code when CONFIG_FOO is set to 'm'.
52  */
53 #define IS_REACHABLE(option) __or(IS_BUILTIN(option), \
54                                 __and(IS_MODULE(option), __is_defined(MODULE)))
55
56 /*
57  * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y' or 'm',
58  * 0 otherwise.
59  */
60 #define IS_ENABLED(option) __or(IS_BUILTIN(option), IS_MODULE(option))
61 #define EXPORT_SYMBOL(sym)
62
63 #define U8_MAX          ((u8)~0U)
64 #define S8_MAX          ((s8)(U8_MAX>>1))
65 #define S8_MIN          ((s8)(-S8_MAX - 1))
66 #define U16_MAX         ((u16)~0U)
67 #define S16_MAX         ((s16)(U16_MAX>>1))
68 #define S16_MIN         ((s16)(-S16_MAX - 1))
69 #define U32_MAX         ((u32)~0U)
70 #define S32_MAX         ((s32)(U32_MAX>>1))
71 #define S32_MIN         ((s32)(-S32_MAX - 1))
72 #define U64_MAX         ((u64)~0ULL)
73 #define S64_MAX         ((s64)(U64_MAX>>1))
74 #define S64_MIN         ((s64)(-S64_MAX - 1))
75
76 #define ALIGN(x, a)     __ALIGN_MASK(x, (typeof(x))(a)-1)
77 #define __ALIGN_MASK(x, mask)   (((x)+(mask))&~(mask))
78
79 #define PTR_ALIGN(p, a)         ((typeof(p))ALIGN((unsigned long)(p), (a)))
80 #define IS_ALIGNED(x, a)                (((x) & ((typeof(x))(a) - 1)) == 0)
81
82 #define __must_be_array(a)      BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
83 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
84
85 #ifndef offsetof
86 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
87 #endif
88
89 #ifndef container_of
90 /**
91  * container_of - cast a member of a structure out to the containing structure
92  * @ptr:        the pointer to the member.
93  * @type:       the type of the container struct this is embedded in.
94  * @member:     the name of the member within the struct.
95  *
96  */
97 #define container_of(ptr, type, member) ({                      \
98         const typeof(((type *)0)->member) * __mptr = (ptr);     \
99         (type *)((char *)__mptr - offsetof(type, member)); })
100 #endif
101
102 #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
103         union { \
104                 struct { MEMBERS } ATTRS; \
105                 struct TAG { MEMBERS } ATTRS NAME; \
106         }
107 #define struct_group(NAME, MEMBERS...)  \
108         __struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
109
110 #define max(x, y) ({                            \
111         typeof(x) _max1 = (x);                  \
112         typeof(y) _max2 = (y);                  \
113         (void) (&_max1 == &_max2);              \
114         _max1 > _max2 ? _max1 : _max2; })
115
116 #define min(x, y) ({                            \
117         typeof(x) _min1 = (x);                  \
118         typeof(y) _min2 = (y);                  \
119         (void) (&_min1 == &_min2);              \
120         _min1 < _min2 ? _min1 : _min2; })
121
122 #define min_t(type, x, y) ({                    \
123         type __min1 = (x);                      \
124         type __min2 = (y);                      \
125         __min1 < __min2 ? __min1: __min2; })
126
127 #define max_t(type, x, y) ({                    \
128         type __max1 = (x);                      \
129         type __max2 = (y);                      \
130         __max1 > __max2 ? __max1: __max2; })
131
132 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
133
134 #define swap(a, b) \
135         do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
136
137 /* This counts to 12. Any more, it will return 13th argument. */
138 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
139 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
140
141 #define _RET_IP_                (unsigned long)__builtin_return_address(0)
142 #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
143
144 #define might_sleep()
145
146 #define cpu_relax()             barrier()
147 #define cpu_relax_lowlatency()  barrier()
148
149 #define panic(fmt, ...)                                 \
150 do {                                                    \
151         printf(fmt, ##__VA_ARGS__);                     \
152         BUG();                                          \
153 } while (0)
154
155 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
156 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
157
158 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
159 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
160
161 /**
162  * kstrtoul - convert a string to an unsigned long
163  * @s: The start of the string. The string must be null-terminated, and may also
164  *  include a single newline before its terminating null. The first character
165  *  may also be a plus sign, but not a minus sign.
166  * @base: The number base to use. The maximum supported base is 16. If base is
167  *  given as 0, then the base of the string is automatically detected with the
168  *  conventional semantics - If it begins with 0x the number will be parsed as a
169  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
170  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
171  * @res: Where to write the result of the conversion on success.
172  *
173  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
174  * Used as a replacement for the obsolete simple_strtoull. Return code must
175  * be checked.
176 */
177 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
178 {
179         /*
180          * We want to shortcut function call, but
181          * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
182          */
183         if (sizeof(unsigned long) == sizeof(unsigned long long) &&
184             __alignof__(unsigned long) == __alignof__(unsigned long long))
185                 return kstrtoull(s, base, (unsigned long long *)res);
186         else
187                 return _kstrtoul(s, base, res);
188 }
189
190 /**
191  * kstrtol - convert a string to a long
192  * @s: The start of the string. The string must be null-terminated, and may also
193  *  include a single newline before its terminating null. The first character
194  *  may also be a plus sign or a minus sign.
195  * @base: The number base to use. The maximum supported base is 16. If base is
196  *  given as 0, then the base of the string is automatically detected with the
197  *  conventional semantics - If it begins with 0x the number will be parsed as a
198  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
199  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
200  * @res: Where to write the result of the conversion on success.
201  *
202  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
203  * Used as a replacement for the obsolete simple_strtoull. Return code must
204  * be checked.
205  */
206 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
207 {
208         /*
209          * We want to shortcut function call, but
210          * __builtin_types_compatible_p(long, long long) = 0.
211          */
212         if (sizeof(long) == sizeof(long long) &&
213             __alignof__(long) == __alignof__(long long))
214                 return kstrtoll(s, base, (long long *)res);
215         else
216                 return _kstrtol(s, base, res);
217 }
218
219 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
220 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
221
222 static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
223 {
224         return kstrtoull(s, base, res);
225 }
226
227 static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
228 {
229         return kstrtoll(s, base, res);
230 }
231
232 static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
233 {
234         return kstrtouint(s, base, res);
235 }
236
237 static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
238 {
239         return kstrtoint(s, base, res);
240 }
241
242 struct printbuf;
243 extern void prt_u64(struct printbuf *out, u64 num);
244
245 extern __printf(2, 0) void prt_vprintf(struct printbuf *out, const char *fmt, va_list args);
246 extern __printf(2, 3) void prt_printf(struct printbuf *out, const char *fmt, ...);
247
248 static const char hex_asc[] = "0123456789abcdef";
249 #define hex_asc_lo(x)   hex_asc[((x) & 0x0f)]
250 #define hex_asc_hi(x)   hex_asc[((x) & 0xf0) >> 4]
251 static const char hex_asc_upper[] = "0123456789ABCDEF";
252 #define hex_asc_upper_lo(x)     hex_asc_upper[((x) & 0x0f)]
253 #define hex_asc_upper_hi(x)     hex_asc_upper[((x) & 0xf0) >> 4]
254
255 /* The hash is always the low bits of hash_len */
256 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
257  #define HASH_LEN_DECLARE u32 hash; u32 len
258 #else
259  #define HASH_LEN_DECLARE u32 len; u32 hash
260 #endif
261
262 struct qstr {
263         union {
264                 struct {
265                         HASH_LEN_DECLARE;
266                 };
267                 u64 hash_len;
268         };
269         const unsigned char *name;
270 };
271
272 #define QSTR_INIT(n,l) { { { .len = l } }, .name = n }
273
274 #define POISON_FREE 0x6b
275
276 static inline void dump_stack(void) {}
277
278 #define unsafe_memcpy(dst, src, bytes, justification)           \
279         memcpy(dst, src, bytes)
280
281 #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
282         __DECLARE_FLEX_ARRAY(TYPE, NAME)
283
284 #endif