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