btrfs-progs: add option to disable backtrace usage
[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
20 #define __KERNCOMPAT
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 #ifndef BTRFS_DISABLE_BACKTRACE
33 #include <execinfo.h>
34 #endif
35
36 #define ptr_to_u64(x)   ((u64)(uintptr_t)x)
37 #define u64_to_ptr(x)   ((void *)(uintptr_t)x)
38
39 #ifndef READ
40 #define READ 0
41 #define WRITE 1
42 #define READA 2
43 #endif
44
45 #define gfp_t int
46 #define get_cpu_var(p) (p)
47 #define __get_cpu_var(p) (p)
48 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
49 #define __GFP_BITS_SHIFT 20
50 #define __GFP_BITS_MASK ((int)((1 << __GFP_BITS_SHIFT) - 1))
51 #define GFP_KERNEL 0
52 #define GFP_NOFS 0
53 #define __read_mostly
54 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
55
56 #ifndef ULONG_MAX
57 #define ULONG_MAX       (~0UL)
58 #endif
59
60 #ifndef BTRFS_DISABLE_BACKTRACE
61 #define MAX_BACKTRACE   16
62 static inline void print_trace(void)
63 {
64         void *array[MAX_BACKTRACE];
65         size_t size;
66
67         size = backtrace(array, MAX_BACKTRACE);
68         backtrace_symbols_fd(array, size, 2);
69 }
70
71 static inline void assert_trace(const char *assertion, const char *filename,
72                               const char *func, unsigned line, int val)
73 {
74         if (val)
75                 return;
76         if (assertion)
77                 fprintf(stderr, "%s:%d: %s: Assertion `%s` failed.\n",
78                         filename, line, func, assertion);
79         else
80                 fprintf(stderr, "%s:%d: %s: Assertion failed.\n", filename,
81                         line, func);
82         print_trace();
83         exit(1);
84 }
85
86 #define BUG() assert_trace(NULL, __FILE__, __func__, __LINE__, 0)
87 #else
88 #define BUG() assert(0)
89 #endif
90
91 #ifdef __CHECKER__
92 #define __force    __attribute__((force))
93 #define __bitwise__ __attribute__((bitwise))
94 #else
95 #define __force
96 #define __bitwise__
97 #endif
98
99 #ifndef __CHECKER__
100 /*
101  * Since we're using primitive definitions from kernel-space, we need to
102  * define __KERNEL__ so that system header files know which definitions
103  * to use.
104  */
105 #define __KERNEL__
106 #include <asm/types.h>
107 typedef __u32 u32;
108 typedef __u64 u64;
109 typedef __u16 u16;
110 typedef __u8 u8;
111 /*
112  * Continuing to define __KERNEL__ breaks others parts of the code, so
113  * we can just undefine it now that we have the correct headers...
114  */
115 #undef __KERNEL__
116 #else
117 typedef unsigned int u32;
118 typedef unsigned int __u32;
119 typedef unsigned long long u64;
120 typedef unsigned char u8;
121 typedef unsigned short u16;
122 #endif
123
124
125 struct vma_shared { int prio_tree_node; };
126 struct vm_area_struct {
127         unsigned long vm_pgoff;
128         unsigned long vm_start;
129         unsigned long vm_end;
130         struct vma_shared shared;
131 };
132
133 struct page {
134         unsigned long index;
135 };
136
137 struct mutex {
138         unsigned long lock;
139 };
140
141 #define mutex_init(m)                                           \
142 do {                                                            \
143         (m)->lock = 1;                                          \
144 } while (0)
145
146 static inline void mutex_lock(struct mutex *m)
147 {
148         m->lock--;
149 }
150
151 static inline void mutex_unlock(struct mutex *m)
152 {
153         m->lock++;
154 }
155
156 static inline int mutex_is_locked(struct mutex *m)
157 {
158         return (m->lock != 1);
159 }
160
161 #define cond_resched()          do { } while (0)
162 #define preempt_enable()        do { } while (0)
163 #define preempt_disable()       do { } while (0)
164
165 #define BITOP_MASK(nr)          (1UL << ((nr) % BITS_PER_LONG))
166 #define BITOP_WORD(nr)          ((nr) / BITS_PER_LONG)
167
168 #ifndef __attribute_const__
169 #define __attribute_const__     __attribute__((__const__))
170 #endif
171
172 /**
173  * __set_bit - Set a bit in memory
174  * @nr: the bit to set
175  * @addr: the address to start counting from
176  *
177  * Unlike set_bit(), this function is non-atomic and may be reordered.
178  * If it's called on the same region of memory simultaneously, the effect
179  * may be that only one operation succeeds.
180  */
181 static inline void __set_bit(int nr, volatile unsigned long *addr)
182 {
183         unsigned long mask = BITOP_MASK(nr);
184         unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
185
186         *p  |= mask;
187 }
188
189 static inline void __clear_bit(int nr, volatile unsigned long *addr)
190 {
191         unsigned long mask = BITOP_MASK(nr);
192         unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
193
194         *p &= ~mask;
195 }
196
197 /**
198  * test_bit - Determine whether a bit is set
199  * @nr: bit number to test
200  * @addr: Address to start counting from
201  */
202 static inline int test_bit(int nr, const volatile unsigned long *addr)
203 {
204         return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
205 }
206
207 /*
208  * error pointer
209  */
210 #define MAX_ERRNO       4095
211 #define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO)
212
213 static inline void *ERR_PTR(long error)
214 {
215         return (void *) error;
216 }
217
218 static inline long PTR_ERR(const void *ptr)
219 {
220         return (long) ptr;
221 }
222
223 static inline long IS_ERR(const void *ptr)
224 {
225         return IS_ERR_VALUE((unsigned long)ptr);
226 }
227
228 /*
229  * max/min macro
230  */
231 #define min(x,y) ({ \
232         typeof(x) _x = (x);     \
233         typeof(y) _y = (y);     \
234         (void) (&_x == &_y);            \
235         _x < _y ? _x : _y; })
236
237 #define max(x,y) ({ \
238         typeof(x) _x = (x);     \
239         typeof(y) _y = (y);     \
240         (void) (&_x == &_y);            \
241         _x > _y ? _x : _y; })
242
243 #define min_t(type,x,y) \
244         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
245 #define max_t(type,x,y) \
246         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
247
248 /*
249  * This looks more complex than it should be. But we need to
250  * get the type for the ~ right in round_down (it needs to be
251  * as wide as the result!), and we want to evaluate the macro
252  * arguments just once each.
253  */
254 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
255 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
256 #define round_down(x, y) ((x) & ~__round_mask(x, y))
257
258 /*
259  * printk
260  */
261 #define printk(fmt, args...) fprintf(stderr, fmt, ##args)
262 #define KERN_CRIT       ""
263 #define KERN_ERR        ""
264
265 /*
266  * kmalloc/kfree
267  */
268 #define kmalloc(x, y) malloc(x)
269 #define kzalloc(x, y) calloc(1, x)
270 #define kstrdup(x, y) strdup(x)
271 #define kfree(x) free(x)
272
273 #ifndef BTRFS_DISABLE_BACKTRACE
274 #define BUG_ON(c) assert_trace(#c, __FILE__, __func__, __LINE__, !(c))
275 #else
276 #define BUG_ON(c) assert(!(c))
277 #endif
278
279 #define WARN_ON(c) BUG_ON(c)
280
281 #ifndef BTRFS_DISABLE_BACKTRACE
282 #define ASSERT(c) assert_trace(#c, __FILE__, __func__, __LINE__, (c))
283 #else
284 #define ASSERT(c) assert(c)
285 #endif
286
287 #define container_of(ptr, type, member) ({                      \
288         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
289                 (type *)( (char *)__mptr - offsetof(type,member) );})
290 #ifdef __CHECKER__
291 #define __bitwise __bitwise__
292 #else
293 #define __bitwise
294 #endif
295
296 typedef u16 __bitwise __le16;
297 typedef u16 __bitwise __be16;
298 typedef u32 __bitwise __le32;
299 typedef u32 __bitwise __be32;
300 typedef u64 __bitwise __le64;
301 typedef u64 __bitwise __be64;
302
303 /* Macros to generate set/get funcs for the struct fields
304  * assume there is a lefoo_to_cpu for every type, so lets make a simple
305  * one for u8:
306  */
307 #define le8_to_cpu(v) (v)
308 #define cpu_to_le8(v) (v)
309 #define __le8 u8
310
311 #if __BYTE_ORDER == __BIG_ENDIAN
312 #define cpu_to_le64(x) ((__force __le64)(u64)(bswap_64(x)))
313 #define le64_to_cpu(x) ((__force u64)(__le64)(bswap_64(x)))
314 #define cpu_to_le32(x) ((__force __le32)(u32)(bswap_32(x)))
315 #define le32_to_cpu(x) ((__force u32)(__le32)(bswap_32(x)))
316 #define cpu_to_le16(x) ((__force __le16)(u16)(bswap_16(x)))
317 #define le16_to_cpu(x) ((__force u16)(__le16)(bswap_16(x)))
318 #else
319 #define cpu_to_le64(x) ((__force __le64)(u64)(x))
320 #define le64_to_cpu(x) ((__force u64)(__le64)(x))
321 #define cpu_to_le32(x) ((__force __le32)(u32)(x))
322 #define le32_to_cpu(x) ((__force u32)(__le32)(x))
323 #define cpu_to_le16(x) ((__force __le16)(u16)(x))
324 #define le16_to_cpu(x) ((__force u16)(__le16)(x))
325 #endif
326
327 struct __una_u16 { __le16 x; } __attribute__((__packed__));
328 struct __una_u32 { __le32 x; } __attribute__((__packed__));
329 struct __una_u64 { __le64 x; } __attribute__((__packed__));
330
331 #define get_unaligned_le8(p) (*((u8 *)(p)))
332 #define put_unaligned_le8(val,p) ((*((u8 *)(p))) = (val))
333 #define get_unaligned_le16(p) le16_to_cpu(((const struct __una_u16 *)(p))->x)
334 #define put_unaligned_le16(val,p) (((struct __una_u16 *)(p))->x = cpu_to_le16(val))
335 #define get_unaligned_le32(p) le32_to_cpu(((const struct __una_u32 *)(p))->x)
336 #define put_unaligned_le32(val,p) (((struct __una_u32 *)(p))->x = cpu_to_le32(val))
337 #define get_unaligned_le64(p) le64_to_cpu(((const struct __una_u64 *)(p))->x)
338 #define put_unaligned_le64(val,p) (((struct __una_u64 *)(p))->x = cpu_to_le64(val))
339 #endif
340
341 #ifndef noinline
342 #define noinline
343 #endif