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