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