]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/kernel.h
New upstream snapshot
[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
15 #define IS_ENABLED(opt)         0
16 #define EXPORT_SYMBOL(sym)
17
18 #define U8_MAX          ((u8)~0U)
19 #define S8_MAX          ((s8)(U8_MAX>>1))
20 #define S8_MIN          ((s8)(-S8_MAX - 1))
21 #define U16_MAX         ((u16)~0U)
22 #define S16_MAX         ((s16)(U16_MAX>>1))
23 #define S16_MIN         ((s16)(-S16_MAX - 1))
24 #define U32_MAX         ((u32)~0U)
25 #define S32_MAX         ((s32)(U32_MAX>>1))
26 #define S32_MIN         ((s32)(-S32_MAX - 1))
27 #define U64_MAX         ((u64)~0ULL)
28 #define S64_MAX         ((s64)(U64_MAX>>1))
29 #define S64_MIN         ((s64)(-S64_MAX - 1))
30
31 #define ALIGN(x, a)     __ALIGN_MASK(x, (typeof(x))(a)-1)
32 #define __ALIGN_MASK(x, mask)   (((x)+(mask))&~(mask))
33
34 #define PTR_ALIGN(p, a)         ((typeof(p))ALIGN((unsigned long)(p), (a)))
35 #define IS_ALIGNED(x, a)                (((x) & ((typeof(x))(a) - 1)) == 0)
36
37 #define __must_be_array(a)      BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
38 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
39
40 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
41
42 #ifndef offsetof
43 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
44 #endif
45
46 #ifndef container_of
47 /**
48  * container_of - cast a member of a structure out to the containing structure
49  * @ptr:        the pointer to the member.
50  * @type:       the type of the container struct this is embedded in.
51  * @member:     the name of the member within the struct.
52  *
53  */
54 #define container_of(ptr, type, member) ({                      \
55         const typeof(((type *)0)->member) * __mptr = (ptr);     \
56         (type *)((char *)__mptr - offsetof(type, member)); })
57 #endif
58
59 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
60 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
61 #define round_down(x, y) ((x) & ~__round_mask(x, y))
62
63 #define roundup(x, y)                                   \
64 ({                                                      \
65         const typeof(y) __y = y;                        \
66         (((x) + (__y - 1)) / __y) * __y;                \
67 })
68
69 #define max(x, y) ({                            \
70         typeof(x) _max1 = (x);                  \
71         typeof(y) _max2 = (y);                  \
72         (void) (&_max1 == &_max2);              \
73         _max1 > _max2 ? _max1 : _max2; })
74
75 #define min(x, y) ({                            \
76         typeof(x) _min1 = (x);                  \
77         typeof(y) _min2 = (y);                  \
78         (void) (&_min1 == &_min2);              \
79         _min1 < _min2 ? _min1 : _min2; })
80
81 #define min_t(type, x, y) ({                    \
82         type __min1 = (x);                      \
83         type __min2 = (y);                      \
84         __min1 < __min2 ? __min1: __min2; })
85
86 #define max_t(type, x, y) ({                    \
87         type __max1 = (x);                      \
88         type __max2 = (y);                      \
89         __max1 > __max2 ? __max1: __max2; })
90
91 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
92
93 #define swap(a, b) \
94         do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
95
96 /* This counts to 12. Any more, it will return 13th argument. */
97 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
98 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
99
100 #define _RET_IP_                (unsigned long)__builtin_return_address(0)
101 #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
102
103 #define might_sleep()
104
105 #define cpu_relax()             do {} while (0)
106 #define cpu_relax_lowlatency()  do {} while (0)
107
108 #define panic(fmt, ...)                                 \
109 do {                                                    \
110         printf(fmt, ##__VA_ARGS__);                     \
111         BUG();                                          \
112 } while (0)
113
114 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
115 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
116
117 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
118 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
119
120 /**
121  * kstrtoul - convert a string to an unsigned long
122  * @s: The start of the string. The string must be null-terminated, and may also
123  *  include a single newline before its terminating null. The first character
124  *  may also be a plus sign, but not a minus sign.
125  * @base: The number base to use. The maximum supported base is 16. If base is
126  *  given as 0, then the base of the string is automatically detected with the
127  *  conventional semantics - If it begins with 0x the number will be parsed as a
128  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
129  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
130  * @res: Where to write the result of the conversion on success.
131  *
132  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
133  * Used as a replacement for the obsolete simple_strtoull. Return code must
134  * be checked.
135 */
136 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
137 {
138         /*
139          * We want to shortcut function call, but
140          * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
141          */
142         if (sizeof(unsigned long) == sizeof(unsigned long long) &&
143             __alignof__(unsigned long) == __alignof__(unsigned long long))
144                 return kstrtoull(s, base, (unsigned long long *)res);
145         else
146                 return _kstrtoul(s, base, res);
147 }
148
149 /**
150  * kstrtol - convert a string to a long
151  * @s: The start of the string. The string must be null-terminated, and may also
152  *  include a single newline before its terminating null. The first character
153  *  may also be a plus sign or a minus sign.
154  * @base: The number base to use. The maximum supported base is 16. If base is
155  *  given as 0, then the base of the string is automatically detected with the
156  *  conventional semantics - If it begins with 0x the number will be parsed as a
157  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
158  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
159  * @res: Where to write the result of the conversion on success.
160  *
161  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
162  * Used as a replacement for the obsolete simple_strtoull. Return code must
163  * be checked.
164  */
165 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
166 {
167         /*
168          * We want to shortcut function call, but
169          * __builtin_types_compatible_p(long, long long) = 0.
170          */
171         if (sizeof(long) == sizeof(long long) &&
172             __alignof__(long) == __alignof__(long long))
173                 return kstrtoll(s, base, (long long *)res);
174         else
175                 return _kstrtol(s, base, res);
176 }
177
178 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
179 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
180
181 static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
182 {
183         return kstrtoull(s, base, res);
184 }
185
186 static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
187 {
188         return kstrtoll(s, base, res);
189 }
190
191 static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
192 {
193         return kstrtouint(s, base, res);
194 }
195
196 static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
197 {
198         return kstrtoint(s, base, res);
199 }
200
201 /* The hash is always the low bits of hash_len */
202 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
203  #define HASH_LEN_DECLARE u32 hash; u32 len
204 #else
205  #define HASH_LEN_DECLARE u32 len; u32 hash
206 #endif
207
208 struct qstr {
209         union {
210                 struct {
211                         HASH_LEN_DECLARE;
212                 };
213                 u64 hash_len;
214         };
215         const unsigned char *name;
216 };
217
218 #define QSTR_INIT(n,l) { { { .len = l } }, .name = n }
219
220 #define POISON_FREE 0x6b
221
222 static inline void dump_stack(void) {}
223
224 #endif