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