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