blob: 3e8132decd7bbedb7494b1f14ab4c09374868bd7 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
Theodore Ts'o50e1e101997-04-26 13:58:21 +00002 * bitops.h --- Bitmap frobbing code. The byte swapping routines are
3 * also included here.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04004 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00005 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
6 *
7 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04008 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
Theodore Ts'o21c84b71997-04-29 16:15:03 +000010 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000011 */
12
Theodore Ts'o2cb403b2002-06-28 08:10:29 -040013#ifdef WORDS_BIGENDIAN
Andreas Dilgera62e3f02006-08-06 01:39:30 -040014#define ext2fs_cpu_to_le64(x) ext2fs_swab64((x))
15#define ext2fs_le64_to_cpu(x) ext2fs_swab64((x))
Theodore Ts'o2cb403b2002-06-28 08:10:29 -040016#define ext2fs_cpu_to_le32(x) ext2fs_swab32((x))
17#define ext2fs_le32_to_cpu(x) ext2fs_swab32((x))
18#define ext2fs_cpu_to_le16(x) ext2fs_swab16((x))
19#define ext2fs_le16_to_cpu(x) ext2fs_swab16((x))
JP Abgralle0ed7402014-03-19 19:08:39 -070020#define ext2fs_cpu_to_be64(x) ((__u64)(x))
21#define ext2fs_be64_to_cpu(x) ((__u64)(x))
Theodore Ts'o2cb403b2002-06-28 08:10:29 -040022#define ext2fs_cpu_to_be32(x) ((__u32)(x))
23#define ext2fs_be32_to_cpu(x) ((__u32)(x))
24#define ext2fs_cpu_to_be16(x) ((__u16)(x))
25#define ext2fs_be16_to_cpu(x) ((__u16)(x))
26#else
Andreas Dilgera62e3f02006-08-06 01:39:30 -040027#define ext2fs_cpu_to_le64(x) ((__u64)(x))
28#define ext2fs_le64_to_cpu(x) ((__u64)(x))
Theodore Ts'o2cb403b2002-06-28 08:10:29 -040029#define ext2fs_cpu_to_le32(x) ((__u32)(x))
30#define ext2fs_le32_to_cpu(x) ((__u32)(x))
31#define ext2fs_cpu_to_le16(x) ((__u16)(x))
32#define ext2fs_le16_to_cpu(x) ((__u16)(x))
JP Abgralle0ed7402014-03-19 19:08:39 -070033#define ext2fs_cpu_to_be64(x) ext2fs_swab64((x))
34#define ext2fs_be64_to_cpu(x) ext2fs_swab64((x))
Theodore Ts'o2cb403b2002-06-28 08:10:29 -040035#define ext2fs_cpu_to_be32(x) ext2fs_swab32((x))
36#define ext2fs_be32_to_cpu(x) ext2fs_swab32((x))
37#define ext2fs_cpu_to_be16(x) ext2fs_swab16((x))
38#define ext2fs_be16_to_cpu(x) ext2fs_swab16((x))
39#endif
40
Theodore Ts'o3839e651997-04-26 13:21:57 +000041/*
42 * EXT2FS bitmap manipulation routines.
43 */
44
45/* Support for sending warning messages from the inline subroutines */
46extern const char *ext2fs_block_string;
47extern const char *ext2fs_inode_string;
48extern const char *ext2fs_mark_string;
49extern const char *ext2fs_unmark_string;
50extern const char *ext2fs_test_string;
Theodore Ts'of3db3561997-04-26 13:34:30 +000051extern void ext2fs_warn_bitmap(errcode_t errcode, unsigned long arg,
52 const char *description);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000053extern void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap,
54 int code, unsigned long arg);
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
JP Abgralle0ed7402014-03-19 19:08:39 -070056#ifdef NO_INLINE_FUNCS
57extern void ext2fs_fast_set_bit(unsigned int nr,void * addr);
58extern void ext2fs_fast_clear_bit(unsigned int nr, void * addr);
59extern void ext2fs_fast_set_bit64(__u64 nr,void * addr);
60extern void ext2fs_fast_clear_bit64(__u64 nr, void * addr);
61extern __u16 ext2fs_swab16(__u16 val);
62extern __u32 ext2fs_swab32(__u32 val);
63extern __u64 ext2fs_swab64(__u64 val);
64
Theodore Ts'o28ffafb2000-02-08 19:14:02 +000065extern int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap, blk_t block);
66extern int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
Theodore Ts'of3db3561997-04-26 13:34:30 +000067 blk_t block);
68extern int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap, blk_t block);
69
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000070extern int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap, ext2_ino_t inode);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +000071extern int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000072 ext2_ino_t inode);
73extern int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap, ext2_ino_t inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000074
Theodore Ts'o50e1e101997-04-26 13:58:21 +000075extern void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
76 blk_t block);
77extern void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
78 blk_t block);
79extern int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
80 blk_t block);
81
82extern void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000083 ext2_ino_t inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000084extern void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000085 ext2_ino_t inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000086extern int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000087 ext2_ino_t inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000088extern blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000089extern ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000090extern blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000091extern ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap);
Theodore Ts'o21c84b71997-04-29 16:15:03 +000092
JP Abgralle0ed7402014-03-19 19:08:39 -070093extern void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
94 blk_t block, int num);
95extern void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
96 blk_t block, int num);
97extern int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
98 blk_t block, int num);
99#endif
100
101/* These functions routines moved to gen_bitmap.c */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000102extern void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
103 blk_t block, int num);
104extern void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
105 blk_t block, int num);
106extern int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
107 blk_t block, int num);
Theodore Ts'oef1a5262009-07-09 14:32:52 -0400108extern int ext2fs_test_inode_bitmap_range(ext2fs_inode_bitmap bitmap,
109 ino_t inode, int num);
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500110extern int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
111 __u32 bitno);
112extern int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
113 blk_t bitno);
Theodore Ts'ob15beaa2007-07-22 17:42:52 -0400114extern int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
115 blk_t bitno);
Theodore Ts'o8df18272007-07-22 18:54:10 -0400116extern int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
117 blk_t block, int num);
JP Abgralle0ed7402014-03-19 19:08:39 -0700118extern void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map);
Theodore Ts'o271a3752007-07-22 19:16:08 -0400119extern __u32 ext2fs_get_generic_bitmap_start(ext2fs_generic_bitmap bitmap);
120extern __u32 ext2fs_get_generic_bitmap_end(ext2fs_generic_bitmap bitmap);
Theodore Ts'ob15beaa2007-07-22 17:42:52 -0400121
JP Abgralle0ed7402014-03-19 19:08:39 -0700122/* 64-bit versions */
123
124#ifdef NO_INLINE_FUNCS
125extern int ext2fs_mark_block_bitmap2(ext2fs_block_bitmap bitmap,
126 blk64_t block);
127extern int ext2fs_unmark_block_bitmap2(ext2fs_block_bitmap bitmap,
128 blk64_t block);
129extern int ext2fs_test_block_bitmap2(ext2fs_block_bitmap bitmap,
130 blk64_t block);
131
132extern int ext2fs_mark_inode_bitmap2(ext2fs_inode_bitmap bitmap,
133 ext2_ino_t inode);
134extern int ext2fs_unmark_inode_bitmap2(ext2fs_inode_bitmap bitmap,
135 ext2_ino_t inode);
136extern int ext2fs_test_inode_bitmap2(ext2fs_inode_bitmap bitmap,
137 ext2_ino_t inode);
138
139extern void ext2fs_fast_mark_block_bitmap2(ext2fs_block_bitmap bitmap,
140 blk64_t block);
141extern void ext2fs_fast_unmark_block_bitmap2(ext2fs_block_bitmap bitmap,
142 blk64_t block);
143extern int ext2fs_fast_test_block_bitmap2(ext2fs_block_bitmap bitmap,
144 blk64_t block);
145
146extern void ext2fs_fast_mark_inode_bitmap2(ext2fs_inode_bitmap bitmap,
147 ext2_ino_t inode);
148extern void ext2fs_fast_unmark_inode_bitmap2(ext2fs_inode_bitmap bitmap,
149 ext2_ino_t inode);
150extern int ext2fs_fast_test_inode_bitmap2(ext2fs_inode_bitmap bitmap,
151 ext2_ino_t inode);
152extern errcode_t ext2fs_find_first_zero_block_bitmap2(ext2fs_block_bitmap bitmap,
153 blk64_t start,
154 blk64_t end,
155 blk64_t *out);
156extern errcode_t ext2fs_find_first_zero_inode_bitmap2(ext2fs_inode_bitmap bitmap,
157 ext2_ino_t start,
158 ext2_ino_t end,
159 ext2_ino_t *out);
160extern blk64_t ext2fs_get_block_bitmap_start2(ext2fs_block_bitmap bitmap);
161extern ext2_ino_t ext2fs_get_inode_bitmap_start2(ext2fs_inode_bitmap bitmap);
162extern blk64_t ext2fs_get_block_bitmap_end2(ext2fs_block_bitmap bitmap);
163extern ext2_ino_t ext2fs_get_inode_bitmap_end2(ext2fs_inode_bitmap bitmap);
164
165extern int ext2fs_fast_test_block_bitmap_range2(ext2fs_block_bitmap bitmap,
166 blk64_t block,
167 unsigned int num);
168extern void ext2fs_fast_mark_block_bitmap_range2(ext2fs_block_bitmap bitmap,
169 blk64_t block,
170 unsigned int num);
171extern void ext2fs_fast_unmark_block_bitmap_range2(ext2fs_block_bitmap bitmap,
172 blk64_t block,
173 unsigned int num);
174#endif
175
176/* These routines moved to gen_bitmap64.c */
177extern void ext2fs_clear_generic_bmap(ext2fs_generic_bitmap bitmap);
178extern errcode_t ext2fs_compare_generic_bmap(errcode_t neq,
179 ext2fs_generic_bitmap bm1,
180 ext2fs_generic_bitmap bm2);
181extern void ext2fs_set_generic_bmap_padding(ext2fs_generic_bitmap bmap);
182extern int ext2fs_mark_generic_bmap(ext2fs_generic_bitmap bitmap,
183 blk64_t bitno);
184extern int ext2fs_unmark_generic_bmap(ext2fs_generic_bitmap bitmap,
185 blk64_t bitno);
186extern int ext2fs_test_generic_bmap(ext2fs_generic_bitmap bitmap,
187 blk64_t bitno);
188extern int ext2fs_test_block_bitmap_range2(ext2fs_block_bitmap bitmap,
189 blk64_t block, unsigned int num);
190extern __u64 ext2fs_get_generic_bmap_start(ext2fs_generic_bitmap bitmap);
191extern __u64 ext2fs_get_generic_bmap_end(ext2fs_generic_bitmap bitmap);
192extern int ext2fs_test_block_bitmap_range2(ext2fs_block_bitmap bitmap,
193 blk64_t block, unsigned int num);
194extern void ext2fs_mark_block_bitmap_range2(ext2fs_block_bitmap bitmap,
195 blk64_t block, unsigned int num);
196extern void ext2fs_unmark_block_bitmap_range2(ext2fs_block_bitmap bitmap,
197 blk64_t block, unsigned int num);
198extern errcode_t ext2fs_find_first_zero_generic_bmap(ext2fs_generic_bitmap bitmap,
199 __u64 start, __u64 end,
200 __u64 *out);
201
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202/*
203 * The inline routines themselves...
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400204 *
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205 * If NO_INLINE_FUNCS is defined, then we won't try to do inline
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000206 * functions at all; they will be included as normal functions in
207 * inline.c
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000209#ifdef NO_INLINE_FUNCS
Theodore Ts'o5953b9d1998-02-01 13:23:02 +0000210#if (defined(__GNUC__) && (defined(__i386__) || defined(__i486__) || \
JP Abgralle0ed7402014-03-19 19:08:39 -0700211 defined(__i586__)))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000212 /* This prevents bitops.c from trying to include the C */
213 /* function version of these functions */
214#define _EXT2_HAVE_ASM_BITOPS_
215#endif
216#endif /* NO_INLINE_FUNCS */
217
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218#if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
219#ifdef INCLUDE_INLINE_FUNCS
JP Abgralle0ed7402014-03-19 19:08:39 -0700220#if (__STDC_VERSION__ >= 199901L)
221#define _INLINE_ extern inline
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700223#define _INLINE_ inline
224#endif
225#else /* !INCLUDE_INLINE FUNCS */
226#if (__STDC_VERSION__ >= 199901L)
227#define _INLINE_ inline
228#else /* not C99 */
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000229#ifdef __GNUC__
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230#define _INLINE_ extern __inline__
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000231#else /* For Watcom C */
232#define _INLINE_ extern inline
JP Abgralle0ed7402014-03-19 19:08:39 -0700233#endif /* __GNUC__ */
234#endif /* __STDC_VERSION__ >= 199901L */
235#endif /* INCLUDE_INLINE_FUNCS */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500237/*
238 * Fast bit set/clear functions that doesn't need to return the
239 * previous bit value.
240 */
241
242_INLINE_ void ext2fs_fast_set_bit(unsigned int nr,void * addr)
243{
244 unsigned char *ADDR = (unsigned char *) addr;
245
246 ADDR += nr >> 3;
JP Abgralle0ed7402014-03-19 19:08:39 -0700247 *ADDR |= (unsigned char) (1 << (nr & 0x07));
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500248}
249
250_INLINE_ void ext2fs_fast_clear_bit(unsigned int nr, void * addr)
251{
252 unsigned char *ADDR = (unsigned char *) addr;
253
254 ADDR += nr >> 3;
JP Abgralle0ed7402014-03-19 19:08:39 -0700255 *ADDR &= (unsigned char) ~(1 << (nr & 0x07));
256}
257
258
259_INLINE_ void ext2fs_fast_set_bit64(__u64 nr, void * addr)
260{
261 unsigned char *ADDR = (unsigned char *) addr;
262
263 ADDR += nr >> 3;
264 *ADDR |= (unsigned char) (1 << (nr & 0x07));
265}
266
267_INLINE_ void ext2fs_fast_clear_bit64(__u64 nr, void * addr)
268{
269 unsigned char *ADDR = (unsigned char *) addr;
270
271 ADDR += nr >> 3;
272 *ADDR &= (unsigned char) ~(1 << (nr & 0x07));
Valerie Aurora Henson8717c3a2009-06-01 16:15:40 -0400273}
274
275
Theodore Ts'oac493822001-07-04 14:04:58 -0400276#if ((defined __GNUC__) && !defined(_EXT2_USE_C_VERSIONS_) && \
277 (defined(__i386__) || defined(__i486__) || defined(__i586__)))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000278
279#define _EXT2_HAVE_ASM_BITOPS_
Theodore Ts'o9ec53cf2001-02-20 16:28:40 +0000280#define _EXT2_HAVE_ASM_SWAB_
281
Theodore Ts'o3839e651997-04-26 13:21:57 +0000282/*
283 * These are done by inline assembly for speed reasons.....
284 *
285 * All bitoperations return 0 if the bit was cleared before the
286 * operation and != 0 if it was not. Bit 0 is the LSB of addr; bit 32
287 * is the LSB of (addr+1).
288 */
289
290/*
291 * Some hacks to defeat gcc over-optimizations..
292 */
293struct __dummy_h { unsigned long a[100]; };
Theodore Ts'o74becf31997-04-26 14:37:06 +0000294#define EXT2FS_ADDR (*(struct __dummy_h *) addr)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400295#define EXT2FS_CONST_ADDR (*(const struct __dummy_h *) addr)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296
Theodore Ts'o80af12e2005-06-30 19:40:18 -0400297_INLINE_ int ext2fs_set_bit(unsigned int nr, void * addr)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298{
299 int oldbit;
300
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500301 addr = (void *) (((unsigned char *) addr) + (nr >> 3));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000302 __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0"
Theodore Ts'o8ad26b02006-03-29 14:23:00 -0500303 :"=r" (oldbit),"+m" (EXT2FS_ADDR)
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500304 :"r" (nr & 7));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 return oldbit;
306}
307
Theodore Ts'o80af12e2005-06-30 19:40:18 -0400308_INLINE_ int ext2fs_clear_bit(unsigned int nr, void * addr)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309{
310 int oldbit;
311
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500312 addr = (void *) (((unsigned char *) addr) + (nr >> 3));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313 __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0"
Theodore Ts'o8ad26b02006-03-29 14:23:00 -0500314 :"=r" (oldbit),"+m" (EXT2FS_ADDR)
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500315 :"r" (nr & 7));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000316 return oldbit;
317}
318
Theodore Ts'o80af12e2005-06-30 19:40:18 -0400319_INLINE_ int ext2fs_test_bit(unsigned int nr, const void * addr)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320{
321 int oldbit;
322
Theodore Ts'odec5cd12006-11-14 23:14:12 -0500323 addr = (const void *) (((const unsigned char *) addr) + (nr >> 3));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000324 __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
325 :"=r" (oldbit)
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500326 :"m" (EXT2FS_CONST_ADDR),"r" (nr & 7));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000327 return oldbit;
328}
329
Theodore Ts'o69fbde42008-05-23 21:35:50 -0400330_INLINE_ __u32 ext2fs_swab32(__u32 val)
331{
332#ifdef EXT2FS_REQUIRE_486
333 __asm__("bswap %0" : "=r" (val) : "0" (val));
334#else
335 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
336 "rorl $16,%0\n\t" /* swap words */
337 "xchgb %b0,%h0" /* swap higher bytes */
338 :"=q" (val)
339 : "0" (val));
340#endif
341 return val;
342}
343
344_INLINE_ __u16 ext2fs_swab16(__u16 val)
345{
346 __asm__("xchgb %b0,%h0" /* swap bytes */ \
347 : "=q" (val) \
348 : "0" (val)); \
349 return val;
350}
351
Theodore Ts'o74becf31997-04-26 14:37:06 +0000352#undef EXT2FS_ADDR
Theodore Ts'o3839e651997-04-26 13:21:57 +0000353
354#endif /* i386 */
355
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000356
Theodore Ts'o69fbde42008-05-23 21:35:50 -0400357#if !defined(_EXT2_HAVE_ASM_SWAB_)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000358
359_INLINE_ __u16 ext2fs_swab16(__u16 val)
360{
JP Abgralle0ed7402014-03-19 19:08:39 -0700361 return (val >> 8) | (__u16) (val << 8);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000362}
363
364_INLINE_ __u32 ext2fs_swab32(__u32 val)
365{
366 return ((val>>24) | ((val>>8)&0xFF00) |
367 ((val<<8)&0xFF0000) | (val<<24));
368}
369
370#endif /* !_EXT2_HAVE_ASM_SWAB */
371
Theodore Ts'oe01f9be2008-09-02 08:49:10 -0400372_INLINE_ __u64 ext2fs_swab64(__u64 val)
373{
JP Abgralle0ed7402014-03-19 19:08:39 -0700374 return (ext2fs_swab32((__u32) (val >> 32)) |
Theodore Ts'oe01f9be2008-09-02 08:49:10 -0400375 (((__u64)ext2fs_swab32(val & 0xFFFFFFFFUL)) << 32));
376}
377
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000378_INLINE_ int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000379 blk_t block)
380{
Theodore Ts'ob15beaa2007-07-22 17:42:52 -0400381 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000382 block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000383}
384
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000385_INLINE_ int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000386 blk_t block)
387{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400388 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000389 block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000390}
391
392_INLINE_ int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
393 blk_t block)
394{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400395 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000396 block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000397}
398
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000399_INLINE_ int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000400 ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000401{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400402 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000403 inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000404}
405
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000406_INLINE_ int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000407 ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000408{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400409 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000410 inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000411}
412
Theodore Ts'of3db3561997-04-26 13:34:30 +0000413_INLINE_ int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000414 ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400416 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000417 inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418}
419
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000420_INLINE_ void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
421 blk_t block)
422{
Theodore Ts'o7bf257a2007-07-22 17:57:13 -0400423 ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap, block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000424}
425
426_INLINE_ void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
427 blk_t block)
428{
Theodore Ts'o7bf257a2007-07-22 17:57:13 -0400429 ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap, block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000430}
431
432_INLINE_ int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
433 blk_t block)
434{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400435 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Theodore Ts'o7bf257a2007-07-22 17:57:13 -0400436 block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000437}
438
439_INLINE_ void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000440 ext2_ino_t inode)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000441{
Theodore Ts'o7bf257a2007-07-22 17:57:13 -0400442 ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap, inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000443}
444
445_INLINE_ void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000446 ext2_ino_t inode)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000447{
Theodore Ts'o7bf257a2007-07-22 17:57:13 -0400448 ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap, inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000449}
450
451_INLINE_ int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000452 ext2_ino_t inode)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000453{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400454 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Theodore Ts'o7bf257a2007-07-22 17:57:13 -0400455 inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000456}
457
458_INLINE_ blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
459{
Theodore Ts'o271a3752007-07-22 19:16:08 -0400460 return ext2fs_get_generic_bitmap_start((ext2fs_generic_bitmap) bitmap);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000461}
462
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000463_INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000464{
Theodore Ts'o271a3752007-07-22 19:16:08 -0400465 return ext2fs_get_generic_bitmap_start((ext2fs_generic_bitmap) bitmap);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000466}
467
468_INLINE_ blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
469{
Theodore Ts'o271a3752007-07-22 19:16:08 -0400470 return ext2fs_get_generic_bitmap_end((ext2fs_generic_bitmap) bitmap);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000471}
472
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000473_INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000474{
Theodore Ts'o271a3752007-07-22 19:16:08 -0400475 return ext2fs_get_generic_bitmap_end((ext2fs_generic_bitmap) bitmap);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000476}
477
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000478_INLINE_ int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
479 blk_t block, int num)
480{
Theodore Ts'o8df18272007-07-22 18:54:10 -0400481 return ext2fs_test_block_bitmap_range(bitmap, block, num);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000482}
483
484_INLINE_ void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
485 blk_t block, int num)
486{
Theodore Ts'o8df18272007-07-22 18:54:10 -0400487 ext2fs_mark_block_bitmap_range(bitmap, block, num);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000488}
489
490_INLINE_ void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
491 blk_t block, int num)
492{
Theodore Ts'o8df18272007-07-22 18:54:10 -0400493 ext2fs_unmark_block_bitmap_range(bitmap, block, num);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000494}
JP Abgralle0ed7402014-03-19 19:08:39 -0700495
496/* 64-bit versions */
497
498_INLINE_ int ext2fs_mark_block_bitmap2(ext2fs_block_bitmap bitmap,
499 blk64_t block)
500{
501 return ext2fs_mark_generic_bmap((ext2fs_generic_bitmap) bitmap,
502 block);
503}
504
505_INLINE_ int ext2fs_unmark_block_bitmap2(ext2fs_block_bitmap bitmap,
506 blk64_t block)
507{
508 return ext2fs_unmark_generic_bmap((ext2fs_generic_bitmap) bitmap, block);
509}
510
511_INLINE_ int ext2fs_test_block_bitmap2(ext2fs_block_bitmap bitmap,
512 blk64_t block)
513{
514 return ext2fs_test_generic_bmap((ext2fs_generic_bitmap) bitmap,
515 block);
516}
517
518_INLINE_ int ext2fs_mark_inode_bitmap2(ext2fs_inode_bitmap bitmap,
519 ext2_ino_t inode)
520{
521 return ext2fs_mark_generic_bmap((ext2fs_generic_bitmap) bitmap,
522 inode);
523}
524
525_INLINE_ int ext2fs_unmark_inode_bitmap2(ext2fs_inode_bitmap bitmap,
526 ext2_ino_t inode)
527{
528 return ext2fs_unmark_generic_bmap((ext2fs_generic_bitmap) bitmap,
529 inode);
530}
531
532_INLINE_ int ext2fs_test_inode_bitmap2(ext2fs_inode_bitmap bitmap,
533 ext2_ino_t inode)
534{
535 return ext2fs_test_generic_bmap((ext2fs_generic_bitmap) bitmap,
536 inode);
537}
538
539_INLINE_ void ext2fs_fast_mark_block_bitmap2(ext2fs_block_bitmap bitmap,
540 blk64_t block)
541{
542 ext2fs_mark_generic_bmap((ext2fs_generic_bitmap) bitmap, block);
543}
544
545_INLINE_ void ext2fs_fast_unmark_block_bitmap2(ext2fs_block_bitmap bitmap,
546 blk64_t block)
547{
548 ext2fs_unmark_generic_bmap((ext2fs_generic_bitmap) bitmap, block);
549}
550
551_INLINE_ int ext2fs_fast_test_block_bitmap2(ext2fs_block_bitmap bitmap,
552 blk64_t block)
553{
554 return ext2fs_test_generic_bmap((ext2fs_generic_bitmap) bitmap,
555 block);
556}
557
558_INLINE_ void ext2fs_fast_mark_inode_bitmap2(ext2fs_inode_bitmap bitmap,
559 ext2_ino_t inode)
560{
561 ext2fs_mark_generic_bmap((ext2fs_generic_bitmap) bitmap, inode);
562}
563
564_INLINE_ void ext2fs_fast_unmark_inode_bitmap2(ext2fs_inode_bitmap bitmap,
565 ext2_ino_t inode)
566{
567 ext2fs_unmark_generic_bmap((ext2fs_generic_bitmap) bitmap, inode);
568}
569
570_INLINE_ int ext2fs_fast_test_inode_bitmap2(ext2fs_inode_bitmap bitmap,
571 ext2_ino_t inode)
572{
573 return ext2fs_test_generic_bmap((ext2fs_generic_bitmap) bitmap,
574 inode);
575}
576
577_INLINE_ errcode_t ext2fs_find_first_zero_block_bitmap2(ext2fs_block_bitmap bitmap,
578 blk64_t start,
579 blk64_t end,
580 blk64_t *out)
581{
582 __u64 o;
583 errcode_t rv;
584
585 rv = ext2fs_find_first_zero_generic_bmap((ext2fs_generic_bitmap) bitmap,
586 start, end, &o);
587 if (!rv)
588 *out = o;
589 return rv;
590}
591
592_INLINE_ errcode_t ext2fs_find_first_zero_inode_bitmap2(ext2fs_inode_bitmap bitmap,
593 ext2_ino_t start,
594 ext2_ino_t end,
595 ext2_ino_t *out)
596{
597 __u64 o;
598 errcode_t rv;
599
600 rv = ext2fs_find_first_zero_generic_bmap((ext2fs_generic_bitmap) bitmap,
601 start, end, &o);
602 if (!rv)
603 *out = (ext2_ino_t) o;
604 return rv;
605}
606
607_INLINE_ blk64_t ext2fs_get_block_bitmap_start2(ext2fs_block_bitmap bitmap)
608{
609 return ext2fs_get_generic_bmap_start((ext2fs_generic_bitmap) bitmap);
610}
611
612_INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_start2(ext2fs_inode_bitmap bitmap)
613{
614 return (ext2_ino_t) ext2fs_get_generic_bmap_start((ext2fs_generic_bitmap) bitmap);
615}
616
617_INLINE_ blk64_t ext2fs_get_block_bitmap_end2(ext2fs_block_bitmap bitmap)
618{
619 return ext2fs_get_generic_bmap_end((ext2fs_generic_bitmap) bitmap);
620}
621
622_INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_end2(ext2fs_inode_bitmap bitmap)
623{
624 return (ext2_ino_t) ext2fs_get_generic_bmap_end((ext2fs_generic_bitmap) bitmap);
625}
626
627_INLINE_ int ext2fs_fast_test_block_bitmap_range2(ext2fs_block_bitmap bitmap,
628 blk64_t block,
629 unsigned int num)
630{
631 return ext2fs_test_block_bitmap_range2(bitmap, block, num);
632}
633
634_INLINE_ void ext2fs_fast_mark_block_bitmap_range2(ext2fs_block_bitmap bitmap,
635 blk64_t block,
636 unsigned int num)
637{
638 ext2fs_mark_block_bitmap_range2(bitmap, block, num);
639}
640
641_INLINE_ void ext2fs_fast_unmark_block_bitmap_range2(ext2fs_block_bitmap bitmap,
642 blk64_t block,
643 unsigned int num)
644{
645 ext2fs_unmark_block_bitmap_range2(bitmap, block, num);
646}
647
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648#undef _INLINE_
649#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000650
JP Abgralle0ed7402014-03-19 19:08:39 -0700651#ifndef _EXT2_HAVE_ASM_BITOPS_
652extern int ext2fs_set_bit(unsigned int nr,void * addr);
653extern int ext2fs_clear_bit(unsigned int nr, void * addr);
654extern int ext2fs_test_bit(unsigned int nr, const void * addr);
655#endif
656
657extern int ext2fs_set_bit64(__u64 nr,void * addr);
658extern int ext2fs_clear_bit64(__u64 nr, void * addr);
659extern int ext2fs_test_bit64(__u64 nr, const void * addr);
660extern unsigned int ext2fs_bitcount(const void *addr, unsigned int nbytes);