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