2 * bitops.h --- Bitmap frobbing code. The byte swapping routines are
5 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
12 * i386 bitops operations taken from <asm/bitops.h>, Copyright 1992,
17 extern int ext2fs_set_bit(unsigned int nr,void * addr);
18 extern int ext2fs_clear_bit(unsigned int nr, void * addr);
19 extern int ext2fs_test_bit(unsigned int nr, const void * addr);
20 extern void ext2fs_fast_set_bit(unsigned int nr,void * addr);
21 extern void ext2fs_fast_clear_bit(unsigned int nr, void * addr);
22 extern __u16 ext2fs_swab16(__u16 val);
23 extern __u32 ext2fs_swab32(__u32 val);
24 extern __u64 ext2fs_swab64(__u64 val);
26 #ifdef WORDS_BIGENDIAN
27 #define ext2fs_cpu_to_le64(x) ext2fs_swab64((x))
28 #define ext2fs_le64_to_cpu(x) ext2fs_swab64((x))
29 #define ext2fs_cpu_to_le32(x) ext2fs_swab32((x))
30 #define ext2fs_le32_to_cpu(x) ext2fs_swab32((x))
31 #define ext2fs_cpu_to_le16(x) ext2fs_swab16((x))
32 #define ext2fs_le16_to_cpu(x) ext2fs_swab16((x))
33 #define ext2fs_cpu_to_be32(x) ((__u32)(x))
34 #define ext2fs_be32_to_cpu(x) ((__u32)(x))
35 #define ext2fs_cpu_to_be16(x) ((__u16)(x))
36 #define ext2fs_be16_to_cpu(x) ((__u16)(x))
38 #define ext2fs_cpu_to_le64(x) ((__u64)(x))
39 #define ext2fs_le64_to_cpu(x) ((__u64)(x))
40 #define ext2fs_cpu_to_le32(x) ((__u32)(x))
41 #define ext2fs_le32_to_cpu(x) ((__u32)(x))
42 #define ext2fs_cpu_to_le16(x) ((__u16)(x))
43 #define ext2fs_le16_to_cpu(x) ((__u16)(x))
44 #define ext2fs_cpu_to_be32(x) ext2fs_swab32((x))
45 #define ext2fs_be32_to_cpu(x) ext2fs_swab32((x))
46 #define ext2fs_cpu_to_be16(x) ext2fs_swab16((x))
47 #define ext2fs_be16_to_cpu(x) ext2fs_swab16((x))
51 * EXT2FS bitmap manipulation routines.
54 /* Support for sending warning messages from the inline subroutines */
55 extern const char *ext2fs_block_string;
56 extern const char *ext2fs_inode_string;
57 extern const char *ext2fs_mark_string;
58 extern const char *ext2fs_unmark_string;
59 extern const char *ext2fs_test_string;
60 extern void ext2fs_warn_bitmap(errcode_t errcode, unsigned long arg,
61 const char *description);
62 extern void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap,
63 int code, unsigned long arg);
65 extern int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap, blk_t block);
66 extern int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
68 extern int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap, blk_t block);
70 extern int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap, ext2_ino_t inode);
71 extern int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
73 extern int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap, ext2_ino_t inode);
75 extern void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
77 extern void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
79 extern int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
82 extern void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
84 extern void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
86 extern int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
88 extern blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap);
89 extern ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap);
90 extern blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap);
91 extern ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap);
93 extern void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
94 blk_t block, int num);
95 extern void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
96 blk_t block, int num);
97 extern int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
98 blk_t block, int num);
99 extern void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
100 blk_t block, int num);
101 extern void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
102 blk_t block, int num);
103 extern int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
104 blk_t block, int num);
105 extern void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map);
107 /* These routines moved to gen_bitmap.c */
108 extern int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
110 extern int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
112 extern int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
116 * The inline routines themselves...
118 * If NO_INLINE_FUNCS is defined, then we won't try to do inline
119 * functions at all; they will be included as normal functions in
122 #ifdef NO_INLINE_FUNCS
123 #if (defined(__GNUC__) && (defined(__i386__) || defined(__i486__) || \
124 defined(__i586__) || defined(__mc68000__)))
125 /* This prevents bitops.c from trying to include the C */
126 /* function version of these functions */
127 #define _EXT2_HAVE_ASM_BITOPS_
129 #endif /* NO_INLINE_FUNCS */
131 #if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
132 #ifdef INCLUDE_INLINE_FUNCS
133 #define _INLINE_ extern
136 #define _INLINE_ extern __inline__
137 #else /* For Watcom C */
138 #define _INLINE_ extern inline
143 * Fast bit set/clear functions that doesn't need to return the
144 * previous bit value.
147 _INLINE_ void ext2fs_fast_set_bit(unsigned int nr,void * addr)
149 unsigned char *ADDR = (unsigned char *) addr;
152 *ADDR |= (1 << (nr & 0x07));
155 _INLINE_ void ext2fs_fast_clear_bit(unsigned int nr, void * addr)
157 unsigned char *ADDR = (unsigned char *) addr;
160 *ADDR &= ~(1 << (nr & 0x07));
164 #if ((defined __GNUC__) && !defined(_EXT2_USE_C_VERSIONS_) && \
165 (defined(__i386__) || defined(__i486__) || defined(__i586__)))
167 #define _EXT2_HAVE_ASM_BITOPS_
168 #define _EXT2_HAVE_ASM_SWAB_
169 #define _EXT2_HAVE_ASM_FINDBIT_
172 * These are done by inline assembly for speed reasons.....
174 * All bitoperations return 0 if the bit was cleared before the
175 * operation and != 0 if it was not. Bit 0 is the LSB of addr; bit 32
176 * is the LSB of (addr+1).
180 * Some hacks to defeat gcc over-optimizations..
182 struct __dummy_h { unsigned long a[100]; };
183 #define EXT2FS_ADDR (*(struct __dummy_h *) addr)
184 #define EXT2FS_CONST_ADDR (*(const struct __dummy_h *) addr)
186 _INLINE_ int ext2fs_set_bit(unsigned int nr, void * addr)
190 addr = (void *) (((unsigned char *) addr) + (nr >> 3));
191 __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0"
192 :"=r" (oldbit),"+m" (EXT2FS_ADDR)
197 _INLINE_ int ext2fs_clear_bit(unsigned int nr, void * addr)
201 addr = (void *) (((unsigned char *) addr) + (nr >> 3));
202 __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0"
203 :"=r" (oldbit),"+m" (EXT2FS_ADDR)
208 _INLINE_ int ext2fs_test_bit(unsigned int nr, const void * addr)
212 addr = (const void *) (((const unsigned char *) addr) + (nr >> 3));
213 __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
215 :"m" (EXT2FS_CONST_ADDR),"r" (nr & 7));
220 _INLINE_ int ext2fs_find_first_bit_set(void * addr, unsigned size)
227 /* This looks at memory. Mark it volatile to tell gcc not to move it around */
228 __asm__ __volatile__(
230 "xorl %%eax,%%eax\n\t"
231 "xorl %%edx,%%edx\n\t"
234 "movl -4(%%edi),%%eax\n\t"
237 "1:\tsubl %%esi,%%edi\n\t"
240 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
241 :"1" ((size + 31) >> 5), "2" (addr), "S" (addr));
245 _INLINE_ int ext2fs_find_next_bit_set (void * addr, int size, int offset)
247 unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
248 int set = 0, bit = offset & 31, res;
252 * Look for zero in first byte
254 __asm__("bsfl %1,%0\n\t"
260 if (set < (32 - bit))
266 * No bit found yet, search remaining full bytes for a bit
268 res = ext2fs_find_first_bit_set(p, size - 32 * (p - (unsigned long *) addr));
269 return (offset + set + res);
273 #ifdef EXT2FS_ENABLE_SWAPFS
274 _INLINE_ __u32 ext2fs_swab32(__u32 val)
276 #ifdef EXT2FS_REQUIRE_486
277 __asm__("bswap %0" : "=r" (val) : "0" (val));
279 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
280 "rorl $16,%0\n\t" /* swap words */
281 "xchgb %b0,%h0" /* swap higher bytes */
288 _INLINE_ __u16 ext2fs_swab16(__u16 val)
290 __asm__("xchgb %b0,%h0" /* swap bytes */ \
296 _INLINE_ __u64 ext2fs_swab64(__u64 val)
298 return (ext2fs_swab32(val >> 32) |
299 (((__u64)ext2fs_swab32(val & 0xFFFFFFFFUL)) << 32));
307 #if ((defined __GNUC__) && !defined(_EXT2_USE_C_VERSIONS_) && \
308 (defined(__mc68000__)))
310 #define _EXT2_HAVE_ASM_BITOPS_
312 _INLINE_ int ext2fs_set_bit(unsigned int nr,void * addr)
316 __asm__ __volatile__ ("bfset %2@{%1:#1}; sne %0"
317 : "=d" (retval) : "d" (nr^7), "a" (addr));
322 _INLINE_ int ext2fs_clear_bit(unsigned int nr, void * addr)
326 __asm__ __volatile__ ("bfclr %2@{%1:#1}; sne %0"
327 : "=d" (retval) : "d" (nr^7), "a" (addr));
332 _INLINE_ int ext2fs_test_bit(unsigned int nr, const void * addr)
336 __asm__ __volatile__ ("bftst %2@{%1:#1}; sne %0"
337 : "=d" (retval) : "d" (nr^7), "a" (addr));
342 #endif /* __mc68000__ */
345 #if !defined(_EXT2_HAVE_ASM_SWAB_) && defined(EXT2FS_ENABLE_SWAPFS)
347 _INLINE_ __u16 ext2fs_swab16(__u16 val)
349 return (val >> 8) | (val << 8);
352 _INLINE_ __u32 ext2fs_swab32(__u32 val)
354 return ((val>>24) | ((val>>8)&0xFF00) |
355 ((val<<8)&0xFF0000) | (val<<24));
358 #endif /* !_EXT2_HAVE_ASM_SWAB */
360 #if !defined(_EXT2_HAVE_ASM_FINDBIT_)
361 _INLINE_ int ext2fs_find_first_bit_set(void * addr, unsigned size)
363 char *cp = (char *) addr;
369 while ((size > res) && (*cp == 0)) {
380 _INLINE_ int ext2fs_find_next_bit_set (void * addr, int size, int offset)
383 int set = 0, bit = offset & 7, res = 0, d0;
386 p = ((unsigned char *) addr) + res;
389 set = ffs(*p & ~((1 << bit) - 1));
391 return (offset & ~7) + set - 1;
395 while ((size > res) && (*p == 0)) {
403 return (res + d0 - 1);
407 _INLINE_ int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap,
410 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
414 _INLINE_ int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
417 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
421 _INLINE_ int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
424 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
428 _INLINE_ int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
431 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
435 _INLINE_ int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
438 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
442 _INLINE_ int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
445 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
449 _INLINE_ void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
452 #ifdef EXT2FS_DEBUG_FAST_OPS
453 if ((block < bitmap->start) || (block > bitmap->end)) {
454 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
455 bitmap->description);
459 ext2fs_fast_set_bit(block - bitmap->start, bitmap->bitmap);
462 _INLINE_ void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
465 #ifdef EXT2FS_DEBUG_FAST_OPS
466 if ((block < bitmap->start) || (block > bitmap->end)) {
467 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK,
468 block, bitmap->description);
472 ext2fs_fast_clear_bit(block - bitmap->start, bitmap->bitmap);
475 _INLINE_ int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
478 #ifdef EXT2FS_DEBUG_FAST_OPS
479 if ((block < bitmap->start) || (block > bitmap->end)) {
480 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
481 block, bitmap->description);
485 return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap);
488 _INLINE_ void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
491 #ifdef EXT2FS_DEBUG_FAST_OPS
492 if ((inode < bitmap->start) || (inode > bitmap->end)) {
493 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_MARK,
494 inode, bitmap->description);
498 ext2fs_fast_set_bit(inode - bitmap->start, bitmap->bitmap);
501 _INLINE_ void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
504 #ifdef EXT2FS_DEBUG_FAST_OPS
505 if ((inode < bitmap->start) || (inode > bitmap->end)) {
506 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_UNMARK,
507 inode, bitmap->description);
511 ext2fs_fast_clear_bit(inode - bitmap->start, bitmap->bitmap);
514 _INLINE_ int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
517 #ifdef EXT2FS_DEBUG_FAST_OPS
518 if ((inode < bitmap->start) || (inode > bitmap->end)) {
519 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_TEST,
520 inode, bitmap->description);
524 return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap);
527 _INLINE_ blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
529 return bitmap->start;
532 _INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
534 return bitmap->start;
537 _INLINE_ blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
542 _INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
547 _INLINE_ int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
548 blk_t block, int num)
552 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
553 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
554 block, bitmap->description);
557 for (i=0; i < num; i++) {
558 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
564 _INLINE_ int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
565 blk_t block, int num)
569 #ifdef EXT2FS_DEBUG_FAST_OPS
570 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
571 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
572 block, bitmap->description);
576 for (i=0; i < num; i++) {
577 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
583 _INLINE_ void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
584 blk_t block, int num)
588 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
589 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
590 bitmap->description);
593 for (i=0; i < num; i++)
594 ext2fs_fast_set_bit(block + i - bitmap->start, bitmap->bitmap);
597 _INLINE_ void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
598 blk_t block, int num)
602 #ifdef EXT2FS_DEBUG_FAST_OPS
603 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
604 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
605 bitmap->description);
609 for (i=0; i < num; i++)
610 ext2fs_fast_set_bit(block + i - bitmap->start, bitmap->bitmap);
613 _INLINE_ void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
614 blk_t block, int num)
618 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
619 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
620 bitmap->description);
623 for (i=0; i < num; i++)
624 ext2fs_fast_clear_bit(block + i - bitmap->start,
628 _INLINE_ void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
629 blk_t block, int num)
633 #ifdef EXT2FS_DEBUG_FAST_OPS
634 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
635 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
636 bitmap->description);
640 for (i=0; i < num; i++)
641 ext2fs_fast_clear_bit(block + i - bitmap->start,