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