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