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