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