e6e9987b51cb14bf3fadf2c319cbc0499185f996
[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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <endian.h>
26 #include <byteswap.h>
27
28 #ifndef READ
29 #define READ 0
30 #define WRITE 1
31 #define READA 2
32 #endif
33
34 #define gfp_t int
35 #define get_cpu_var(p) (p)
36 #define __get_cpu_var(p) (p)
37 #define BITS_PER_LONG (sizeof(long) * 8)
38 #define __GFP_BITS_SHIFT 20
39 #define __GFP_BITS_MASK ((int)((1 << __GFP_BITS_SHIFT) - 1))
40 #define GFP_KERNEL 0
41 #define GFP_NOFS 0
42 #define __read_mostly
43 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
44 #define ULONG_MAX       (~0UL)
45 #define BUG() abort()
46 #ifdef __CHECKER__
47 #define __force    __attribute__((force))
48 #define __bitwise__ __attribute__((bitwise))
49 #else
50 #define __force
51 #define __bitwise__
52 #endif
53
54 #ifndef __CHECKER__
55 #include <asm/types.h>
56 typedef __u32 u32;
57 typedef __u64 u64;
58 typedef __u16 u16;
59 typedef __u8 u8;
60 #else
61 typedef unsigned int u32;
62 typedef unsigned int __u32;
63 typedef unsigned long long u64;
64 typedef unsigned char u8;
65 typedef unsigned short u16;
66 #endif
67
68
69 struct vma_shared { int prio_tree_node; };
70 struct vm_area_struct {
71         unsigned long vm_pgoff;
72         unsigned long vm_start;
73         unsigned long vm_end;
74         struct vma_shared shared;
75 };
76
77 struct page {
78         unsigned long index;
79 };
80
81 struct mutex {
82         unsigned long lock;
83 };
84
85 #define mutex_init(m)                                           \
86 do {                                                            \
87         (m)->lock = 1;                                          \
88 } while (0)
89
90 static inline void mutex_lock(struct mutex *m)
91 {
92         m->lock--;
93 }
94
95 static inline void mutex_unlock(struct mutex *m)
96 {
97         m->lock++;
98 }
99
100 static inline int mutex_is_locked(struct mutex *m)
101 {
102         return (m->lock != 1);
103 }
104
105 #define cond_resched()          do { } while (0)
106 #define preempt_enable()        do { } while (0)
107 #define preempt_disable()       do { } while (0)
108
109 #define BITOP_MASK(nr)          (1UL << ((nr) % BITS_PER_LONG))
110 #define BITOP_WORD(nr)          ((nr) / BITS_PER_LONG)
111
112 /**
113  * __set_bit - Set a bit in memory
114  * @nr: the bit to set
115  * @addr: the address to start counting from
116  *
117  * Unlike set_bit(), this function is non-atomic and may be reordered.
118  * If it's called on the same region of memory simultaneously, the effect
119  * may be that only one operation succeeds.
120  */
121 static inline void __set_bit(int nr, volatile unsigned long *addr)
122 {
123         unsigned long mask = BITOP_MASK(nr);
124         unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
125
126         *p  |= mask;
127 }
128
129 static inline void __clear_bit(int nr, volatile unsigned long *addr)
130 {
131         unsigned long mask = BITOP_MASK(nr);
132         unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
133
134         *p &= ~mask;
135 }
136
137 /**
138  * test_bit - Determine whether a bit is set
139  * @nr: bit number to test
140  * @addr: Address to start counting from
141  */
142 static inline int test_bit(int nr, const volatile unsigned long *addr)
143 {
144         return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
145 }
146
147 /*
148  * error pointer
149  */
150 #define MAX_ERRNO       4095
151 #define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO)
152
153 static inline void *ERR_PTR(long error)
154 {
155         return (void *) error;
156 }
157
158 static inline long PTR_ERR(const void *ptr)
159 {
160         return (long) ptr;
161 }
162
163 static inline long IS_ERR(const void *ptr)
164 {
165         return IS_ERR_VALUE((unsigned long)ptr);
166 }
167
168 /*
169  * max/min macro
170  */
171 #define min(x,y) ({ \
172         typeof(x) _x = (x);     \
173         typeof(y) _y = (y);     \
174         (void) (&_x == &_y);            \
175         _x < _y ? _x : _y; })
176
177 #define max(x,y) ({ \
178         typeof(x) _x = (x);     \
179         typeof(y) _y = (y);     \
180         (void) (&_x == &_y);            \
181         _x > _y ? _x : _y; })
182
183 #define min_t(type,x,y) \
184         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
185 #define max_t(type,x,y) \
186         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
187
188 /*
189  * printk
190  */
191 #define printk(fmt, args...) fprintf(stderr, fmt, ##args)
192 #define KERN_CRIT       ""
193
194 /*
195  * kmalloc/kfree
196  */
197 #define kmalloc(x, y) malloc(x)
198 #define kzalloc(x, y) calloc(1, x)
199 #define kstrdup(x, y) strdup(x)
200 #define kfree(x) free(x)
201
202 #define BUG_ON(c) do { if (c) abort(); } while (0)
203 #define WARN_ON(c) do { if (c) abort(); } while (0)
204
205 #undef offsetof
206 #ifdef __compiler_offsetof
207 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
208 #else
209 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
210 #endif
211
212 #define container_of(ptr, type, member) ({                      \
213         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
214                 (type *)( (char *)__mptr - offsetof(type,member) );})
215 #ifdef __CHECKER__
216 #define __CHECK_ENDIAN__
217 #define __bitwise __bitwise__
218 #else
219 #define __bitwise
220 #endif
221
222 typedef u16 __bitwise __le16;
223 typedef u16 __bitwise __be16;
224 typedef u32 __bitwise __le32;
225 typedef u32 __bitwise __be32;
226 typedef u64 __bitwise __le64;
227 typedef u64 __bitwise __be64;
228
229 /* Macros to generate set/get funcs for the struct fields
230  * assume there is a lefoo_to_cpu for every type, so lets make a simple
231  * one for u8:
232  */
233 #define le8_to_cpu(v) (v)
234 #define cpu_to_le8(v) (v)
235 #define __le8 u8
236
237 #if __BYTE_ORDER == __BIG_ENDIAN
238 #define cpu_to_le64(x) ((__force __le64)(u64)(bswap_64(x)))
239 #define le64_to_cpu(x) ((__force u64)(__le64)(bswap_64(x)))
240 #define cpu_to_le32(x) ((__force __le32)(u32)(bswap_32(x)))
241 #define le32_to_cpu(x) ((__force u32)(__le32)(bswap_32(x)))
242 #define cpu_to_le16(x) ((__force __le16)(u16)(bswap_16(x)))
243 #define le16_to_cpu(x) ((__force u16)(__le16)(bswap_16(x)))
244 #else
245 #define cpu_to_le64(x) ((__force __le64)(u64)(x))
246 #define le64_to_cpu(x) ((__force u64)(__le64)(x))
247 #define cpu_to_le32(x) ((__force __le32)(u32)(x))
248 #define le32_to_cpu(x) ((__force u32)(__le32)(x))
249 #define cpu_to_le16(x) ((__force __le16)(u16)(x))
250 #define le16_to_cpu(x) ((__force u16)(__le16)(x))
251 #endif
252 #endif
253
254 #ifndef noinline
255 #define noinline
256 #endif