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