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