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