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