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