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.
19 #ifndef __KERNCOMPAT_H__
20 #define __KERNCOMPAT_H__
30 #include <linux/types.h>
36 #ifndef BTRFS_DISABLE_BACKTRACE
37 #define BTRFS_DISABLE_BACKTRACE
39 #define __always_inline __inline __attribute__ ((__always_inline__))
42 #ifndef BTRFS_DISABLE_BACKTRACE
46 #define ptr_to_u64(x) ((u64)(uintptr_t)x)
47 #define u64_to_ptr(x) ((void *)(uintptr_t)x)
56 #define get_cpu_var(p) (p)
57 #define __get_cpu_var(p) (p)
58 #define BITS_PER_BYTE 8
59 #define BITS_PER_LONG (__SIZEOF_LONG__ * BITS_PER_BYTE)
60 #define __GFP_BITS_SHIFT 20
61 #define __GFP_BITS_MASK ((int)((1 << __GFP_BITS_SHIFT) - 1))
65 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
68 #define ULONG_MAX (~0UL)
71 #define __token_glue(a,b,c) ___token_glue(a,b,c)
72 #define ___token_glue(a,b,c) a ## b ## c
73 #ifdef DEBUG_BUILD_CHECKS
74 #define BUILD_ASSERT(x) extern int __token_glue(compile_time_assert_,__LINE__,__COUNTER__)[1-2*!(x)] __attribute__((unused))
76 #define BUILD_ASSERT(x)
79 #ifndef BTRFS_DISABLE_BACKTRACE
80 #define MAX_BACKTRACE 16
81 static inline void print_trace(void)
83 void *array[MAX_BACKTRACE];
86 size = backtrace(array, MAX_BACKTRACE);
87 backtrace_symbols_fd(array, size, 2);
91 static inline void warning_trace(const char *assertion, const char *filename,
92 const char *func, unsigned line, long val)
97 "%s:%d: %s: Warning: assertion `%s` failed, value %ld\n",
98 filename, line, func, assertion, val);
99 #ifndef BTRFS_DISABLE_BACKTRACE
104 static inline void bugon_trace(const char *assertion, const char *filename,
105 const char *func, unsigned line, long val)
110 "%s:%d: %s: BUG_ON `%s` triggered, value %ld\n",
111 filename, line, func, assertion, val);
112 #ifndef BTRFS_DISABLE_BACKTRACE
120 #define __force __attribute__((force))
121 #define __bitwise__ __attribute__((bitwise))
129 * Since we're using primitive definitions from kernel-space, we need to
130 * define __KERNEL__ so that system header files know which definitions
134 #include <asm/types.h>
143 * Continuing to define __KERNEL__ breaks others parts of the code, so
144 * we can just undefine it now that we have the correct headers...
148 typedef unsigned int u32;
149 typedef unsigned int __u32;
150 typedef unsigned long long u64;
151 typedef unsigned char u8;
152 typedef unsigned short u16;
153 typedef long long s64;
158 struct vma_shared { int prio_tree_node; };
159 struct vm_area_struct {
160 unsigned long vm_pgoff;
161 unsigned long vm_start;
162 unsigned long vm_end;
163 struct vma_shared shared;
174 #define mutex_init(m) \
179 static inline void mutex_lock(struct mutex *m)
184 static inline void mutex_unlock(struct mutex *m)
189 static inline int mutex_is_locked(struct mutex *m)
191 return (m->lock != 1);
194 #define cond_resched() do { } while (0)
195 #define preempt_enable() do { } while (0)
196 #define preempt_disable() do { } while (0)
198 #define BITOP_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
199 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
201 #ifndef __attribute_const__
202 #define __attribute_const__ __attribute__((__const__))
206 * __set_bit - Set a bit in memory
207 * @nr: the bit to set
208 * @addr: the address to start counting from
210 * Unlike set_bit(), this function is non-atomic and may be reordered.
211 * If it's called on the same region of memory simultaneously, the effect
212 * may be that only one operation succeeds.
214 static inline void __set_bit(int nr, volatile unsigned long *addr)
216 unsigned long mask = BITOP_MASK(nr);
217 unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
222 static inline void __clear_bit(int nr, volatile unsigned long *addr)
224 unsigned long mask = BITOP_MASK(nr);
225 unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
231 * test_bit - Determine whether a bit is set
232 * @nr: bit number to test
233 * @addr: Address to start counting from
235 static inline int test_bit(int nr, const volatile unsigned long *addr)
237 return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
243 #define MAX_ERRNO 4095
244 #define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO)
246 static inline void *ERR_PTR(long error)
248 return (void *) error;
251 static inline long PTR_ERR(const void *ptr)
256 static inline int IS_ERR(const void *ptr)
258 return IS_ERR_VALUE((unsigned long)ptr);
261 static inline int IS_ERR_OR_NULL(const void *ptr)
263 return !ptr || IS_ERR(ptr);
267 * This looks more complex than it should be. But we need to
268 * get the type for the ~ right in round_down (it needs to be
269 * as wide as the result!), and we want to evaluate the macro
270 * arguments just once each.
272 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
273 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
274 #define round_down(x, y) ((x) & ~__round_mask(x, y))
279 #define printk(fmt, args...) fprintf(stderr, fmt, ##args)
286 #define kmalloc(x, y) malloc(x)
287 #define kzalloc(x, y) calloc(1, x)
288 #define kstrdup(x, y) strdup(x)
289 #define kfree(x) free(x)
290 #define vmalloc(x) malloc(x)
291 #define vfree(x) free(x)
293 #ifndef BTRFS_DISABLE_BACKTRACE
294 static inline void assert_trace(const char *assertion, const char *filename,
295 const char *func, unsigned line, long val)
300 "%s:%d: %s: Assertion `%s` failed, value %ld\n",
301 filename, line, func, assertion, val);
302 #ifndef BTRFS_DISABLE_BACKTRACE
308 #define ASSERT(c) assert_trace(#c, __FILE__, __func__, __LINE__, (long)(c))
310 #define ASSERT(c) assert(c)
313 #define BUG_ON(c) bugon_trace(#c, __FILE__, __func__, __LINE__, (long)(c))
314 #define BUG() BUG_ON(1)
315 #define WARN_ON(c) warning_trace(#c, __FILE__, __func__, __LINE__, (long)(c))
317 #define container_of(ptr, type, member) ({ \
318 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
319 (type *)( (char *)__mptr - offsetof(type,member) );})
321 #define __bitwise __bitwise__
326 /* Alignment check */
327 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
329 static inline int is_power_of_2(unsigned long n)
331 return (n != 0 && ((n & (n - 1)) == 0));
334 typedef u16 __bitwise __le16;
335 typedef u16 __bitwise __be16;
336 typedef u32 __bitwise __le32;
337 typedef u32 __bitwise __be32;
338 typedef u64 __bitwise __le64;
339 typedef u64 __bitwise __be64;
341 /* Macros to generate set/get funcs for the struct fields
342 * assume there is a lefoo_to_cpu for every type, so lets make a simple
345 #define le8_to_cpu(v) (v)
346 #define cpu_to_le8(v) (v)
349 #if __BYTE_ORDER == __BIG_ENDIAN
350 #define cpu_to_le64(x) ((__force __le64)(u64)(bswap_64(x)))
351 #define le64_to_cpu(x) ((__force u64)(__le64)(bswap_64(x)))
352 #define cpu_to_le32(x) ((__force __le32)(u32)(bswap_32(x)))
353 #define le32_to_cpu(x) ((__force u32)(__le32)(bswap_32(x)))
354 #define cpu_to_le16(x) ((__force __le16)(u16)(bswap_16(x)))
355 #define le16_to_cpu(x) ((__force u16)(__le16)(bswap_16(x)))
357 #define cpu_to_le64(x) ((__force __le64)(u64)(x))
358 #define le64_to_cpu(x) ((__force u64)(__le64)(x))
359 #define cpu_to_le32(x) ((__force __le32)(u32)(x))
360 #define le32_to_cpu(x) ((__force u32)(__le32)(x))
361 #define cpu_to_le16(x) ((__force __le16)(u16)(x))
362 #define le16_to_cpu(x) ((__force u16)(__le16)(x))
365 struct __una_u16 { __le16 x; } __attribute__((__packed__));
366 struct __una_u32 { __le32 x; } __attribute__((__packed__));
367 struct __una_u64 { __le64 x; } __attribute__((__packed__));
369 #define get_unaligned_le8(p) (*((u8 *)(p)))
370 #define get_unaligned_8(p) (*((u8 *)(p)))
371 #define put_unaligned_le8(val,p) ((*((u8 *)(p))) = (val))
372 #define put_unaligned_8(val,p) ((*((u8 *)(p))) = (val))
373 #define get_unaligned_le16(p) le16_to_cpu(((const struct __una_u16 *)(p))->x)
374 #define get_unaligned_16(p) (((const struct __una_u16 *)(p))->x)
375 #define put_unaligned_le16(val,p) (((struct __una_u16 *)(p))->x = cpu_to_le16(val))
376 #define put_unaligned_16(val,p) (((struct __una_u16 *)(p))->x = (val))
377 #define get_unaligned_le32(p) le32_to_cpu(((const struct __una_u32 *)(p))->x)
378 #define get_unaligned_32(p) (((const struct __una_u32 *)(p))->x)
379 #define put_unaligned_le32(val,p) (((struct __una_u32 *)(p))->x = cpu_to_le32(val))
380 #define put_unaligned_32(val,p) (((struct __una_u32 *)(p))->x = (val))
381 #define get_unaligned_le64(p) le64_to_cpu(((const struct __una_u64 *)(p))->x)
382 #define get_unaligned_64(p) (((const struct __una_u64 *)(p))->x)
383 #define put_unaligned_le64(val,p) (((struct __una_u64 *)(p))->x = cpu_to_le64(val))
384 #define put_unaligned_64(val,p) (((struct __una_u64 *)(p))->x = (val))