btrfs-progs: kerncompat: add build-time assertion support
[platform/upstream/btrfs-progs.git] / kerncompat.h
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #ifndef __KERNCOMPAT_H__
20 #define __KERNCOMPAT_H__
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <endian.h>
27 #include <byteswap.h>
28 #include <assert.h>
29 #include <stddef.h>
30 #include <linux/types.h>
31 #include <stdint.h>
32
33 #include <features.h>
34
35 #ifndef __GLIBC__
36 #ifndef BTRFS_DISABLE_BACKTRACE
37 #define BTRFS_DISABLE_BACKTRACE
38 #endif
39 #define __always_inline __inline __attribute__ ((__always_inline__))
40 #endif
41
42 #ifndef BTRFS_DISABLE_BACKTRACE
43 #include <execinfo.h>
44 #endif
45
46 #define ptr_to_u64(x)   ((u64)(uintptr_t)x)
47 #define u64_to_ptr(x)   ((void *)(uintptr_t)x)
48
49 #ifndef READ
50 #define READ 0
51 #define WRITE 1
52 #define READA 2
53 #endif
54
55 #define gfp_t int
56 #define get_cpu_var(p) (p)
57 #define __get_cpu_var(p) (p)
58 #define BITS_PER_BYTE 8
59 #define BITS_PER_LONG (__SIZEOF_LONG__ * BITS_PER_BYTE)
60 #define __GFP_BITS_SHIFT 20
61 #define __GFP_BITS_MASK ((int)((1 << __GFP_BITS_SHIFT) - 1))
62 #define GFP_KERNEL 0
63 #define GFP_NOFS 0
64 #define __read_mostly
65 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
66
67 #ifndef ULONG_MAX
68 #define ULONG_MAX       (~0UL)
69 #endif
70
71 #define __token_glue(a,b,c)     ___token_glue(a,b,c)
72 #define ___token_glue(a,b,c)    a ## b ## c
73 #define BUILD_ASSERT(x)         extern int __token_glue(compile_time_assert_,__LINE__,__COUNTER__)[1-2*!(x)] __attribute__((unused))
74
75 #ifndef BTRFS_DISABLE_BACKTRACE
76 #define MAX_BACKTRACE   16
77 static inline void print_trace(void)
78 {
79         void *array[MAX_BACKTRACE];
80         int size;
81
82         size = backtrace(array, MAX_BACKTRACE);
83         backtrace_symbols_fd(array, size, 2);
84 }
85
86 static inline void assert_trace(const char *assertion, const char *filename,
87                               const char *func, unsigned line, int val)
88 {
89         if (val)
90                 return;
91         if (assertion)
92                 fprintf(stderr, "%s:%d: %s: Assertion `%s` failed, value %d\n",
93                         filename, line, func, assertion, val);
94         else
95                 fprintf(stderr, "%s:%d: %s: Assertion failed, value %d.\n",
96                         filename, line, func, val);
97         print_trace();
98         abort();
99         exit(1);
100 }
101
102 static inline void warning_trace(const char *assertion, const char *filename,
103                               const char *func, unsigned line, int val,
104                               int trace)
105 {
106         if (val)
107                 return;
108         if (assertion)
109                 fprintf(stderr,
110                         "%s:%d: %s: Warning: assertion `%s` failed, value %d\n",
111                         filename, line, func, assertion, val);
112         else
113                 fprintf(stderr,
114                         "%s:%d: %s: Warning: assertion failed, value %d.\n",
115                         filename, line, func, val);
116         if (trace)
117                 print_trace();
118 }
119
120 #define BUG() assert_trace(NULL, __FILE__, __func__, __LINE__, 0)
121 #else
122 #define BUG() assert(0)
123 #endif
124
125 #ifdef __CHECKER__
126 #define __force    __attribute__((force))
127 #define __bitwise__ __attribute__((bitwise))
128 #else
129 #define __force
130 #define __bitwise__
131 #endif
132
133 #ifndef __CHECKER__
134 /*
135  * Since we're using primitive definitions from kernel-space, we need to
136  * define __KERNEL__ so that system header files know which definitions
137  * to use.
138  */
139 #define __KERNEL__
140 #include <asm/types.h>
141 typedef __u32 u32;
142 typedef __u64 u64;
143 typedef __u16 u16;
144 typedef __u8 u8;
145 typedef __s64 s64;
146 typedef __s32 s32;
147
148 /*
149  * Continuing to define __KERNEL__ breaks others parts of the code, so
150  * we can just undefine it now that we have the correct headers...
151  */
152 #undef __KERNEL__
153 #else
154 typedef unsigned int u32;
155 typedef unsigned int __u32;
156 typedef unsigned long long u64;
157 typedef unsigned char u8;
158 typedef unsigned short u16;
159 typedef long long s64;
160 typedef int s32;
161 #endif
162
163
164 struct vma_shared { int prio_tree_node; };
165 struct vm_area_struct {
166         unsigned long vm_pgoff;
167         unsigned long vm_start;
168         unsigned long vm_end;
169         struct vma_shared shared;
170 };
171
172 struct page {
173         unsigned long index;
174 };
175
176 struct mutex {
177         unsigned long lock;
178 };
179
180 #define mutex_init(m)                                           \
181 do {                                                            \
182         (m)->lock = 1;                                          \
183 } while (0)
184
185 static inline void mutex_lock(struct mutex *m)
186 {
187         m->lock--;
188 }
189
190 static inline void mutex_unlock(struct mutex *m)
191 {
192         m->lock++;
193 }
194
195 static inline int mutex_is_locked(struct mutex *m)
196 {
197         return (m->lock != 1);
198 }
199
200 #define cond_resched()          do { } while (0)
201 #define preempt_enable()        do { } while (0)
202 #define preempt_disable()       do { } while (0)
203
204 #define BITOP_MASK(nr)          (1UL << ((nr) % BITS_PER_LONG))
205 #define BITOP_WORD(nr)          ((nr) / BITS_PER_LONG)
206
207 #ifndef __attribute_const__
208 #define __attribute_const__     __attribute__((__const__))
209 #endif
210
211 /**
212  * __set_bit - Set a bit in memory
213  * @nr: the bit to set
214  * @addr: the address to start counting from
215  *
216  * Unlike set_bit(), this function is non-atomic and may be reordered.
217  * If it's called on the same region of memory simultaneously, the effect
218  * may be that only one operation succeeds.
219  */
220 static inline void __set_bit(int nr, volatile unsigned long *addr)
221 {
222         unsigned long mask = BITOP_MASK(nr);
223         unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
224
225         *p  |= mask;
226 }
227
228 static inline void __clear_bit(int nr, volatile unsigned long *addr)
229 {
230         unsigned long mask = BITOP_MASK(nr);
231         unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
232
233         *p &= ~mask;
234 }
235
236 /**
237  * test_bit - Determine whether a bit is set
238  * @nr: bit number to test
239  * @addr: Address to start counting from
240  */
241 static inline int test_bit(int nr, const volatile unsigned long *addr)
242 {
243         return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
244 }
245
246 /*
247  * error pointer
248  */
249 #define MAX_ERRNO       4095
250 #define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO)
251
252 static inline void *ERR_PTR(long error)
253 {
254         return (void *) error;
255 }
256
257 static inline long PTR_ERR(const void *ptr)
258 {
259         return (long) ptr;
260 }
261
262 static inline long IS_ERR(const void *ptr)
263 {
264         return IS_ERR_VALUE((unsigned long)ptr);
265 }
266
267 /*
268  * This looks more complex than it should be. But we need to
269  * get the type for the ~ right in round_down (it needs to be
270  * as wide as the result!), and we want to evaluate the macro
271  * arguments just once each.
272  */
273 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
274 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
275 #define round_down(x, y) ((x) & ~__round_mask(x, y))
276
277 /*
278  * printk
279  */
280 #define printk(fmt, args...) fprintf(stderr, fmt, ##args)
281 #define KERN_CRIT       ""
282 #define KERN_ERR        ""
283
284 /*
285  * kmalloc/kfree
286  */
287 #define kmalloc(x, y) malloc(x)
288 #define kzalloc(x, y) calloc(1, x)
289 #define kstrdup(x, y) strdup(x)
290 #define kfree(x) free(x)
291 #define vmalloc(x) malloc(x)
292 #define vfree(x) free(x)
293
294 #ifndef BTRFS_DISABLE_BACKTRACE
295 #define BUG_ON(c) assert_trace(#c, __FILE__, __func__, __LINE__, !(c))
296 #define WARN_ON(c) warning_trace(#c, __FILE__, __func__, __LINE__, !(c), 1)
297 #else
298 #define BUG_ON(c) assert(!(c))
299 #define WARN_ON(c) warning_trace(#c, __FILE__, __func__, __LINE__, !(c), 0)
300 #endif
301
302 #ifndef BTRFS_DISABLE_BACKTRACE
303 #define ASSERT(c) assert_trace(#c, __FILE__, __func__, __LINE__, (c))
304 #else
305 #define ASSERT(c) assert(c)
306 #endif
307
308 #define container_of(ptr, type, member) ({                      \
309         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
310                 (type *)( (char *)__mptr - offsetof(type,member) );})
311 #ifdef __CHECKER__
312 #define __bitwise __bitwise__
313 #else
314 #define __bitwise
315 #endif
316
317 /* Alignment check */
318 #define IS_ALIGNED(x, a)                (((x) & ((typeof(x))(a) - 1)) == 0)
319
320 static inline int is_power_of_2(unsigned long n)
321 {
322         return (n != 0 && ((n & (n - 1)) == 0));
323 }
324
325 typedef u16 __bitwise __le16;
326 typedef u16 __bitwise __be16;
327 typedef u32 __bitwise __le32;
328 typedef u32 __bitwise __be32;
329 typedef u64 __bitwise __le64;
330 typedef u64 __bitwise __be64;
331
332 /* Macros to generate set/get funcs for the struct fields
333  * assume there is a lefoo_to_cpu for every type, so lets make a simple
334  * one for u8:
335  */
336 #define le8_to_cpu(v) (v)
337 #define cpu_to_le8(v) (v)
338 #define __le8 u8
339
340 #if __BYTE_ORDER == __BIG_ENDIAN
341 #define cpu_to_le64(x) ((__force __le64)(u64)(bswap_64(x)))
342 #define le64_to_cpu(x) ((__force u64)(__le64)(bswap_64(x)))
343 #define cpu_to_le32(x) ((__force __le32)(u32)(bswap_32(x)))
344 #define le32_to_cpu(x) ((__force u32)(__le32)(bswap_32(x)))
345 #define cpu_to_le16(x) ((__force __le16)(u16)(bswap_16(x)))
346 #define le16_to_cpu(x) ((__force u16)(__le16)(bswap_16(x)))
347 #else
348 #define cpu_to_le64(x) ((__force __le64)(u64)(x))
349 #define le64_to_cpu(x) ((__force u64)(__le64)(x))
350 #define cpu_to_le32(x) ((__force __le32)(u32)(x))
351 #define le32_to_cpu(x) ((__force u32)(__le32)(x))
352 #define cpu_to_le16(x) ((__force __le16)(u16)(x))
353 #define le16_to_cpu(x) ((__force u16)(__le16)(x))
354 #endif
355
356 struct __una_u16 { __le16 x; } __attribute__((__packed__));
357 struct __una_u32 { __le32 x; } __attribute__((__packed__));
358 struct __una_u64 { __le64 x; } __attribute__((__packed__));
359
360 #define get_unaligned_le8(p) (*((u8 *)(p)))
361 #define get_unaligned_8(p) (*((u8 *)(p)))
362 #define put_unaligned_le8(val,p) ((*((u8 *)(p))) = (val))
363 #define put_unaligned_8(val,p) ((*((u8 *)(p))) = (val))
364 #define get_unaligned_le16(p) le16_to_cpu(((const struct __una_u16 *)(p))->x)
365 #define get_unaligned_16(p) (((const struct __una_u16 *)(p))->x)
366 #define put_unaligned_le16(val,p) (((struct __una_u16 *)(p))->x = cpu_to_le16(val))
367 #define put_unaligned_16(val,p) (((struct __una_u16 *)(p))->x = (val))
368 #define get_unaligned_le32(p) le32_to_cpu(((const struct __una_u32 *)(p))->x)
369 #define get_unaligned_32(p) (((const struct __una_u32 *)(p))->x)
370 #define put_unaligned_le32(val,p) (((struct __una_u32 *)(p))->x = cpu_to_le32(val))
371 #define put_unaligned_32(val,p) (((struct __una_u32 *)(p))->x = (val))
372 #define get_unaligned_le64(p) le64_to_cpu(((const struct __una_u64 *)(p))->x)
373 #define get_unaligned_64(p) (((const struct __una_u64 *)(p))->x)
374 #define put_unaligned_le64(val,p) (((struct __una_u64 *)(p))->x = cpu_to_le64(val))
375 #define put_unaligned_64(val,p) (((struct __una_u64 *)(p))->x = (val))
376
377 #ifndef true
378 #define true 1
379 #define false 0
380 #endif
381
382 #ifndef noinline
383 #define noinline
384 #endif
385
386 #endif