btrfs-progs: Fix disable backtrace assert error
[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 #ifdef DEBUG_BUILD_CHECKS
74 #define BUILD_ASSERT(x)         extern int __token_glue(compile_time_assert_,__LINE__,__COUNTER__)[1-2*!(x)] __attribute__((unused))
75 #else
76 #define BUILD_ASSERT(x)
77 #endif
78
79 #ifndef BTRFS_DISABLE_BACKTRACE
80 #define MAX_BACKTRACE   16
81 static inline void print_trace(void)
82 {
83         void *array[MAX_BACKTRACE];
84         int size;
85
86         size = backtrace(array, MAX_BACKTRACE);
87         backtrace_symbols_fd(array, size, 2);
88 }
89 #endif
90
91 static inline void warning_trace(const char *assertion, const char *filename,
92                               const char *func, unsigned line, long val)
93 {
94         if (!val)
95                 return;
96         if (assertion)
97                 fprintf(stderr,
98                         "%s:%d: %s: Warning: assertion `%s` failed, value %ld\n",
99                         filename, line, func, assertion, val);
100         else
101                 fprintf(stderr,
102                         "%s:%d: %s: Warning: assertion failed, value %ld.\n",
103                         filename, line, func, val);
104 #ifndef BTRFS_DISABLE_BACKTRACE
105         print_trace();
106 #endif
107 }
108
109
110 #ifdef __CHECKER__
111 #define __force    __attribute__((force))
112 #define __bitwise__ __attribute__((bitwise))
113 #else
114 #define __force
115 #define __bitwise__
116 #endif
117
118 #ifndef __CHECKER__
119 /*
120  * Since we're using primitive definitions from kernel-space, we need to
121  * define __KERNEL__ so that system header files know which definitions
122  * to use.
123  */
124 #define __KERNEL__
125 #include <asm/types.h>
126 typedef __u32 u32;
127 typedef __u64 u64;
128 typedef __u16 u16;
129 typedef __u8 u8;
130 typedef __s64 s64;
131 typedef __s32 s32;
132
133 /*
134  * Continuing to define __KERNEL__ breaks others parts of the code, so
135  * we can just undefine it now that we have the correct headers...
136  */
137 #undef __KERNEL__
138 #else
139 typedef unsigned int u32;
140 typedef unsigned int __u32;
141 typedef unsigned long long u64;
142 typedef unsigned char u8;
143 typedef unsigned short u16;
144 typedef long long s64;
145 typedef int s32;
146 #endif
147
148
149 struct vma_shared { int prio_tree_node; };
150 struct vm_area_struct {
151         unsigned long vm_pgoff;
152         unsigned long vm_start;
153         unsigned long vm_end;
154         struct vma_shared shared;
155 };
156
157 struct page {
158         unsigned long index;
159 };
160
161 struct mutex {
162         unsigned long lock;
163 };
164
165 #define mutex_init(m)                                           \
166 do {                                                            \
167         (m)->lock = 1;                                          \
168 } while (0)
169
170 static inline void mutex_lock(struct mutex *m)
171 {
172         m->lock--;
173 }
174
175 static inline void mutex_unlock(struct mutex *m)
176 {
177         m->lock++;
178 }
179
180 static inline int mutex_is_locked(struct mutex *m)
181 {
182         return (m->lock != 1);
183 }
184
185 #define cond_resched()          do { } while (0)
186 #define preempt_enable()        do { } while (0)
187 #define preempt_disable()       do { } while (0)
188
189 #define BITOP_MASK(nr)          (1UL << ((nr) % BITS_PER_LONG))
190 #define BITOP_WORD(nr)          ((nr) / BITS_PER_LONG)
191
192 #ifndef __attribute_const__
193 #define __attribute_const__     __attribute__((__const__))
194 #endif
195
196 /**
197  * __set_bit - Set a bit in memory
198  * @nr: the bit to set
199  * @addr: the address to start counting from
200  *
201  * Unlike set_bit(), this function is non-atomic and may be reordered.
202  * If it's called on the same region of memory simultaneously, the effect
203  * may be that only one operation succeeds.
204  */
205 static inline void __set_bit(int nr, volatile unsigned long *addr)
206 {
207         unsigned long mask = BITOP_MASK(nr);
208         unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
209
210         *p  |= mask;
211 }
212
213 static inline void __clear_bit(int nr, volatile unsigned long *addr)
214 {
215         unsigned long mask = BITOP_MASK(nr);
216         unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
217
218         *p &= ~mask;
219 }
220
221 /**
222  * test_bit - Determine whether a bit is set
223  * @nr: bit number to test
224  * @addr: Address to start counting from
225  */
226 static inline int test_bit(int nr, const volatile unsigned long *addr)
227 {
228         return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
229 }
230
231 /*
232  * error pointer
233  */
234 #define MAX_ERRNO       4095
235 #define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO)
236
237 static inline void *ERR_PTR(long error)
238 {
239         return (void *) error;
240 }
241
242 static inline long PTR_ERR(const void *ptr)
243 {
244         return (long) ptr;
245 }
246
247 static inline int IS_ERR(const void *ptr)
248 {
249         return IS_ERR_VALUE((unsigned long)ptr);
250 }
251
252 static inline int IS_ERR_OR_NULL(const void *ptr)
253 {
254         return !ptr || IS_ERR(ptr);
255 }
256
257 /*
258  * This looks more complex than it should be. But we need to
259  * get the type for the ~ right in round_down (it needs to be
260  * as wide as the result!), and we want to evaluate the macro
261  * arguments just once each.
262  */
263 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
264 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
265 #define round_down(x, y) ((x) & ~__round_mask(x, y))
266
267 /*
268  * printk
269  */
270 #define printk(fmt, args...) fprintf(stderr, fmt, ##args)
271 #define KERN_CRIT       ""
272 #define KERN_ERR        ""
273
274 /*
275  * kmalloc/kfree
276  */
277 #define kmalloc(x, y) malloc(x)
278 #define kzalloc(x, y) calloc(1, x)
279 #define kstrdup(x, y) strdup(x)
280 #define kfree(x) free(x)
281 #define vmalloc(x) malloc(x)
282 #define vfree(x) free(x)
283
284 #ifndef BTRFS_DISABLE_BACKTRACE
285 static inline void assert_trace(const char *assertion, const char *filename,
286                               const char *func, unsigned line, long val)
287 {
288         if (!val)
289                 return;
290         warning_trace(assertion, filename, func, line, val);
291         abort();
292         exit(1);
293 }
294 #define ASSERT(c) assert_trace(#c, __FILE__, __func__, __LINE__, (long)!(c))
295 #define BUG_ON(c) assert_trace(#c, __FILE__, __func__, __LINE__, (long)(c))
296 #else
297 #define ASSERT(c) assert(c)
298 #define BUG_ON(c) ASSERT(!(c))
299 #endif
300
301 #define BUG() BUG_ON(1)
302 #define WARN_ON(c) warning_trace(#c, __FILE__, __func__, __LINE__, (long)(c))
303
304 #define container_of(ptr, type, member) ({                      \
305         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
306                 (type *)( (char *)__mptr - offsetof(type,member) );})
307 #ifdef __CHECKER__
308 #define __bitwise __bitwise__
309 #else
310 #define __bitwise
311 #endif
312
313 /* Alignment check */
314 #define IS_ALIGNED(x, a)                (((x) & ((typeof(x))(a) - 1)) == 0)
315
316 static inline int is_power_of_2(unsigned long n)
317 {
318         return (n != 0 && ((n & (n - 1)) == 0));
319 }
320
321 typedef u16 __bitwise __le16;
322 typedef u16 __bitwise __be16;
323 typedef u32 __bitwise __le32;
324 typedef u32 __bitwise __be32;
325 typedef u64 __bitwise __le64;
326 typedef u64 __bitwise __be64;
327
328 /* Macros to generate set/get funcs for the struct fields
329  * assume there is a lefoo_to_cpu for every type, so lets make a simple
330  * one for u8:
331  */
332 #define le8_to_cpu(v) (v)
333 #define cpu_to_le8(v) (v)
334 #define __le8 u8
335
336 #if __BYTE_ORDER == __BIG_ENDIAN
337 #define cpu_to_le64(x) ((__force __le64)(u64)(bswap_64(x)))
338 #define le64_to_cpu(x) ((__force u64)(__le64)(bswap_64(x)))
339 #define cpu_to_le32(x) ((__force __le32)(u32)(bswap_32(x)))
340 #define le32_to_cpu(x) ((__force u32)(__le32)(bswap_32(x)))
341 #define cpu_to_le16(x) ((__force __le16)(u16)(bswap_16(x)))
342 #define le16_to_cpu(x) ((__force u16)(__le16)(bswap_16(x)))
343 #else
344 #define cpu_to_le64(x) ((__force __le64)(u64)(x))
345 #define le64_to_cpu(x) ((__force u64)(__le64)(x))
346 #define cpu_to_le32(x) ((__force __le32)(u32)(x))
347 #define le32_to_cpu(x) ((__force u32)(__le32)(x))
348 #define cpu_to_le16(x) ((__force __le16)(u16)(x))
349 #define le16_to_cpu(x) ((__force u16)(__le16)(x))
350 #endif
351
352 struct __una_u16 { __le16 x; } __attribute__((__packed__));
353 struct __una_u32 { __le32 x; } __attribute__((__packed__));
354 struct __una_u64 { __le64 x; } __attribute__((__packed__));
355
356 #define get_unaligned_le8(p) (*((u8 *)(p)))
357 #define get_unaligned_8(p) (*((u8 *)(p)))
358 #define put_unaligned_le8(val,p) ((*((u8 *)(p))) = (val))
359 #define put_unaligned_8(val,p) ((*((u8 *)(p))) = (val))
360 #define get_unaligned_le16(p) le16_to_cpu(((const struct __una_u16 *)(p))->x)
361 #define get_unaligned_16(p) (((const struct __una_u16 *)(p))->x)
362 #define put_unaligned_le16(val,p) (((struct __una_u16 *)(p))->x = cpu_to_le16(val))
363 #define put_unaligned_16(val,p) (((struct __una_u16 *)(p))->x = (val))
364 #define get_unaligned_le32(p) le32_to_cpu(((const struct __una_u32 *)(p))->x)
365 #define get_unaligned_32(p) (((const struct __una_u32 *)(p))->x)
366 #define put_unaligned_le32(val,p) (((struct __una_u32 *)(p))->x = cpu_to_le32(val))
367 #define put_unaligned_32(val,p) (((struct __una_u32 *)(p))->x = (val))
368 #define get_unaligned_le64(p) le64_to_cpu(((const struct __una_u64 *)(p))->x)
369 #define get_unaligned_64(p) (((const struct __una_u64 *)(p))->x)
370 #define put_unaligned_le64(val,p) (((struct __una_u64 *)(p))->x = cpu_to_le64(val))
371 #define put_unaligned_64(val,p) (((struct __una_u64 *)(p))->x = (val))
372
373 #ifndef true
374 #define true 1
375 #define false 0
376 #endif
377
378 #ifndef noinline
379 #define noinline
380 #endif
381
382 #endif