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