2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
30 #include <linux/types.h>
32 #ifndef BTRFS_DISABLE_BACKTRACE
36 #define ptr_to_u64(x) ((u64)(uintptr_t)x)
37 #define u64_to_ptr(x) ((void *)(uintptr_t)x)
46 #define get_cpu_var(p) (p)
47 #define __get_cpu_var(p) (p)
48 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
49 #define __GFP_BITS_SHIFT 20
50 #define __GFP_BITS_MASK ((int)((1 << __GFP_BITS_SHIFT) - 1))
54 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
57 #define ULONG_MAX (~0UL)
60 #ifndef BTRFS_DISABLE_BACKTRACE
61 #define MAX_BACKTRACE 16
62 static inline void print_trace(void)
64 void *array[MAX_BACKTRACE];
67 size = backtrace(array, MAX_BACKTRACE);
68 backtrace_symbols_fd(array, size, 2);
71 static inline void assert_trace(const char *assertion, const char *filename,
72 const char *func, unsigned line, int val)
77 fprintf(stderr, "%s:%d: %s: Assertion `%s` failed.\n",
78 filename, line, func, assertion);
80 fprintf(stderr, "%s:%d: %s: Assertion failed.\n", filename,
86 #define BUG() assert_trace(NULL, __FILE__, __func__, __LINE__, 0)
88 #define BUG() assert(0)
92 #define __force __attribute__((force))
93 #define __bitwise__ __attribute__((bitwise))
101 * Since we're using primitive definitions from kernel-space, we need to
102 * define __KERNEL__ so that system header files know which definitions
106 #include <asm/types.h>
115 * Continuing to define __KERNEL__ breaks others parts of the code, so
116 * we can just undefine it now that we have the correct headers...
120 typedef unsigned int u32;
121 typedef unsigned int __u32;
122 typedef unsigned long long u64;
123 typedef unsigned char u8;
124 typedef unsigned short u16;
125 typedef long long s64;
130 struct vma_shared { int prio_tree_node; };
131 struct vm_area_struct {
132 unsigned long vm_pgoff;
133 unsigned long vm_start;
134 unsigned long vm_end;
135 struct vma_shared shared;
146 #define mutex_init(m) \
151 static inline void mutex_lock(struct mutex *m)
156 static inline void mutex_unlock(struct mutex *m)
161 static inline int mutex_is_locked(struct mutex *m)
163 return (m->lock != 1);
166 #define cond_resched() do { } while (0)
167 #define preempt_enable() do { } while (0)
168 #define preempt_disable() do { } while (0)
170 #define BITOP_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
171 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
173 #ifndef __attribute_const__
174 #define __attribute_const__ __attribute__((__const__))
178 * __set_bit - Set a bit in memory
179 * @nr: the bit to set
180 * @addr: the address to start counting from
182 * Unlike set_bit(), this function is non-atomic and may be reordered.
183 * If it's called on the same region of memory simultaneously, the effect
184 * may be that only one operation succeeds.
186 static inline void __set_bit(int nr, volatile unsigned long *addr)
188 unsigned long mask = BITOP_MASK(nr);
189 unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
194 static inline void __clear_bit(int nr, volatile unsigned long *addr)
196 unsigned long mask = BITOP_MASK(nr);
197 unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
203 * test_bit - Determine whether a bit is set
204 * @nr: bit number to test
205 * @addr: Address to start counting from
207 static inline int test_bit(int nr, const volatile unsigned long *addr)
209 return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
215 #define MAX_ERRNO 4095
216 #define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO)
218 static inline void *ERR_PTR(long error)
220 return (void *) error;
223 static inline long PTR_ERR(const void *ptr)
228 static inline long IS_ERR(const void *ptr)
230 return IS_ERR_VALUE((unsigned long)ptr);
236 #define min(x,y) ({ \
237 typeof(x) _x = (x); \
238 typeof(y) _y = (y); \
239 (void) (&_x == &_y); \
240 _x < _y ? _x : _y; })
242 #define max(x,y) ({ \
243 typeof(x) _x = (x); \
244 typeof(y) _y = (y); \
245 (void) (&_x == &_y); \
246 _x > _y ? _x : _y; })
248 #define min_t(type,x,y) \
249 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
250 #define max_t(type,x,y) \
251 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
254 * This looks more complex than it should be. But we need to
255 * get the type for the ~ right in round_down (it needs to be
256 * as wide as the result!), and we want to evaluate the macro
257 * arguments just once each.
259 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
260 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
261 #define round_down(x, y) ((x) & ~__round_mask(x, y))
266 #define printk(fmt, args...) fprintf(stderr, fmt, ##args)
273 #define kmalloc(x, y) malloc(x)
274 #define kzalloc(x, y) calloc(1, x)
275 #define kstrdup(x, y) strdup(x)
276 #define kfree(x) free(x)
277 #define vmalloc(x) malloc(x)
278 #define vfree(x) free(x)
280 #ifndef BTRFS_DISABLE_BACKTRACE
281 #define BUG_ON(c) assert_trace(#c, __FILE__, __func__, __LINE__, !(c))
283 #define BUG_ON(c) assert(!(c))
286 #define WARN_ON(c) BUG_ON(c)
288 #ifndef BTRFS_DISABLE_BACKTRACE
289 #define ASSERT(c) assert_trace(#c, __FILE__, __func__, __LINE__, (c))
291 #define ASSERT(c) assert(c)
294 #define container_of(ptr, type, member) ({ \
295 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
296 (type *)( (char *)__mptr - offsetof(type,member) );})
298 #define __bitwise __bitwise__
303 typedef u16 __bitwise __le16;
304 typedef u16 __bitwise __be16;
305 typedef u32 __bitwise __le32;
306 typedef u32 __bitwise __be32;
307 typedef u64 __bitwise __le64;
308 typedef u64 __bitwise __be64;
310 /* Macros to generate set/get funcs for the struct fields
311 * assume there is a lefoo_to_cpu for every type, so lets make a simple
314 #define le8_to_cpu(v) (v)
315 #define cpu_to_le8(v) (v)
318 #if __BYTE_ORDER == __BIG_ENDIAN
319 #define cpu_to_le64(x) ((__force __le64)(u64)(bswap_64(x)))
320 #define le64_to_cpu(x) ((__force u64)(__le64)(bswap_64(x)))
321 #define cpu_to_le32(x) ((__force __le32)(u32)(bswap_32(x)))
322 #define le32_to_cpu(x) ((__force u32)(__le32)(bswap_32(x)))
323 #define cpu_to_le16(x) ((__force __le16)(u16)(bswap_16(x)))
324 #define le16_to_cpu(x) ((__force u16)(__le16)(bswap_16(x)))
326 #define cpu_to_le64(x) ((__force __le64)(u64)(x))
327 #define le64_to_cpu(x) ((__force u64)(__le64)(x))
328 #define cpu_to_le32(x) ((__force __le32)(u32)(x))
329 #define le32_to_cpu(x) ((__force u32)(__le32)(x))
330 #define cpu_to_le16(x) ((__force __le16)(u16)(x))
331 #define le16_to_cpu(x) ((__force u16)(__le16)(x))
334 struct __una_u16 { __le16 x; } __attribute__((__packed__));
335 struct __una_u32 { __le32 x; } __attribute__((__packed__));
336 struct __una_u64 { __le64 x; } __attribute__((__packed__));
338 #define get_unaligned_le8(p) (*((u8 *)(p)))
339 #define put_unaligned_le8(val,p) ((*((u8 *)(p))) = (val))
340 #define get_unaligned_le16(p) le16_to_cpu(((const struct __una_u16 *)(p))->x)
341 #define put_unaligned_le16(val,p) (((struct __una_u16 *)(p))->x = cpu_to_le16(val))
342 #define get_unaligned_le32(p) le32_to_cpu(((const struct __una_u32 *)(p))->x)
343 #define put_unaligned_le32(val,p) (((struct __una_u32 *)(p))->x = cpu_to_le32(val))
344 #define get_unaligned_le64(p) le64_to_cpu(((const struct __una_u64 *)(p))->x)
345 #define put_unaligned_le64(val,p) (((struct __una_u64 *)(p))->x = cpu_to_le64(val))