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 two 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,
113 * The inline routines themselves...
115 * If NO_INLINE_FUNCS is defined, then we won't try to do inline
116 * functions at all; they will be included as normal functions in
119 #ifdef NO_INLINE_FUNCS
120 #if (defined(__GNUC__) && (defined(__i386__) || defined(__i486__) || \
121 defined(__i586__) || defined(__mc68000__)))
122 /* This prevents bitops.c from trying to include the C */
123 /* function version of these functions */
124 #define _EXT2_HAVE_ASM_BITOPS_
126 #endif /* NO_INLINE_FUNCS */
128 #if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
129 #ifdef INCLUDE_INLINE_FUNCS
130 #define _INLINE_ extern
133 #define _INLINE_ extern __inline__
134 #else /* For Watcom C */
135 #define _INLINE_ extern inline
140 * Fast bit set/clear functions that doesn't need to return the
141 * previous bit value.
144 _INLINE_ void ext2fs_fast_set_bit(unsigned int nr,void * addr)
146 unsigned char *ADDR = (unsigned char *) addr;
149 *ADDR |= (1 << (nr & 0x07));
152 _INLINE_ void ext2fs_fast_clear_bit(unsigned int nr, void * addr)
154 unsigned char *ADDR = (unsigned char *) addr;
157 *ADDR &= ~(1 << (nr & 0x07));
161 #if ((defined __GNUC__) && !defined(_EXT2_USE_C_VERSIONS_) && \
162 (defined(__i386__) || defined(__i486__) || defined(__i586__)))
164 #define _EXT2_HAVE_ASM_BITOPS_
165 #define _EXT2_HAVE_ASM_SWAB_
166 #define _EXT2_HAVE_ASM_FINDBIT_
169 * These are done by inline assembly for speed reasons.....
171 * All bitoperations return 0 if the bit was cleared before the
172 * operation and != 0 if it was not. Bit 0 is the LSB of addr; bit 32
173 * is the LSB of (addr+1).
177 * Some hacks to defeat gcc over-optimizations..
179 struct __dummy_h { unsigned long a[100]; };
180 #define EXT2FS_ADDR (*(struct __dummy_h *) addr)
181 #define EXT2FS_CONST_ADDR (*(const struct __dummy_h *) addr)
183 _INLINE_ int ext2fs_set_bit(unsigned int nr, void * addr)
187 addr = (void *) (((unsigned char *) addr) + (nr >> 3));
188 __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0"
189 :"=r" (oldbit),"+m" (EXT2FS_ADDR)
194 _INLINE_ int ext2fs_clear_bit(unsigned int nr, void * addr)
198 addr = (void *) (((unsigned char *) addr) + (nr >> 3));
199 __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0"
200 :"=r" (oldbit),"+m" (EXT2FS_ADDR)
205 _INLINE_ int ext2fs_test_bit(unsigned int nr, const void * addr)
209 addr = (const void *) (((const unsigned char *) addr) + (nr >> 3));
210 __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
212 :"m" (EXT2FS_CONST_ADDR),"r" (nr & 7));
217 _INLINE_ int ext2fs_find_first_bit_set(void * addr, unsigned size)
224 /* This looks at memory. Mark it volatile to tell gcc not to move it around */
225 __asm__ __volatile__(
227 "xorl %%eax,%%eax\n\t"
228 "xorl %%edx,%%edx\n\t"
231 "movl -4(%%edi),%%eax\n\t"
234 "1:\tsubl %%esi,%%edi\n\t"
237 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
238 :"1" ((size + 31) >> 5), "2" (addr), "S" (addr));
242 _INLINE_ int ext2fs_find_next_bit_set (void * addr, int size, int offset)
244 unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
245 int set = 0, bit = offset & 31, res;
249 * Look for zero in first byte
251 __asm__("bsfl %1,%0\n\t"
257 if (set < (32 - bit))
263 * No bit found yet, search remaining full bytes for a bit
265 res = ext2fs_find_first_bit_set(p, size - 32 * (p - (unsigned long *) addr));
266 return (offset + set + res);
270 #ifdef EXT2FS_ENABLE_SWAPFS
271 _INLINE_ __u32 ext2fs_swab32(__u32 val)
273 #ifdef EXT2FS_REQUIRE_486
274 __asm__("bswap %0" : "=r" (val) : "0" (val));
276 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
277 "rorl $16,%0\n\t" /* swap words */
278 "xchgb %b0,%h0" /* swap higher bytes */
285 _INLINE_ __u16 ext2fs_swab16(__u16 val)
287 __asm__("xchgb %b0,%h0" /* swap bytes */ \
293 _INLINE_ __u64 ext2fs_swab64(__u64 val)
295 return (ext2fs_swab32(val >> 32) |
296 (((__u64)ext2fs_swab32(val & 0xFFFFFFFFUL)) << 32));
304 #if ((defined __GNUC__) && !defined(_EXT2_USE_C_VERSIONS_) && \
305 (defined(__mc68000__)))
307 #define _EXT2_HAVE_ASM_BITOPS_
309 _INLINE_ int ext2fs_set_bit(unsigned int nr,void * addr)
313 __asm__ __volatile__ ("bfset %2@{%1:#1}; sne %0"
314 : "=d" (retval) : "d" (nr^7), "a" (addr));
319 _INLINE_ int ext2fs_clear_bit(unsigned int nr, void * addr)
323 __asm__ __volatile__ ("bfclr %2@{%1:#1}; sne %0"
324 : "=d" (retval) : "d" (nr^7), "a" (addr));
329 _INLINE_ int ext2fs_test_bit(unsigned int nr, const void * addr)
333 __asm__ __volatile__ ("bftst %2@{%1:#1}; sne %0"
334 : "=d" (retval) : "d" (nr^7), "a" (addr));
339 #endif /* __mc68000__ */
342 #if !defined(_EXT2_HAVE_ASM_SWAB_) && defined(EXT2FS_ENABLE_SWAPFS)
344 _INLINE_ __u16 ext2fs_swab16(__u16 val)
346 return (val >> 8) | (val << 8);
349 _INLINE_ __u32 ext2fs_swab32(__u32 val)
351 return ((val>>24) | ((val>>8)&0xFF00) |
352 ((val<<8)&0xFF0000) | (val<<24));
355 #endif /* !_EXT2_HAVE_ASM_SWAB */
357 #if !defined(_EXT2_HAVE_ASM_FINDBIT_)
358 _INLINE_ int ext2fs_find_first_bit_set(void * addr, unsigned size)
360 char *cp = (char *) addr;
366 while ((size > res) && (*cp == 0)) {
377 _INLINE_ int ext2fs_find_next_bit_set (void * addr, int size, int offset)
380 int set = 0, bit = offset & 7, res = 0, d0;
383 p = ((unsigned char *) addr) + res;
386 set = ffs(*p & ~((1 << bit) - 1));
388 return (offset & ~7) + set - 1;
392 while ((size > res) && (*p == 0)) {
400 return (res + d0 - 1);
404 _INLINE_ int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
407 _INLINE_ int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
410 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
411 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
414 return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
417 _INLINE_ int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap,
420 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap)
425 _INLINE_ int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
428 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
432 _INLINE_ int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
435 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
439 _INLINE_ int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
442 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
446 _INLINE_ int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
449 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
453 _INLINE_ int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
456 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
460 _INLINE_ void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
463 #ifdef EXT2FS_DEBUG_FAST_OPS
464 if ((block < bitmap->start) || (block > bitmap->end)) {
465 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
466 bitmap->description);
470 ext2fs_fast_set_bit(block - bitmap->start, bitmap->bitmap);
473 _INLINE_ void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
476 #ifdef EXT2FS_DEBUG_FAST_OPS
477 if ((block < bitmap->start) || (block > bitmap->end)) {
478 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK,
479 block, bitmap->description);
483 ext2fs_fast_clear_bit(block - bitmap->start, bitmap->bitmap);
486 _INLINE_ int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
489 #ifdef EXT2FS_DEBUG_FAST_OPS
490 if ((block < bitmap->start) || (block > bitmap->end)) {
491 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
492 block, bitmap->description);
496 return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap);
499 _INLINE_ void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
502 #ifdef EXT2FS_DEBUG_FAST_OPS
503 if ((inode < bitmap->start) || (inode > bitmap->end)) {
504 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_MARK,
505 inode, bitmap->description);
509 ext2fs_fast_set_bit(inode - bitmap->start, bitmap->bitmap);
512 _INLINE_ void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
515 #ifdef EXT2FS_DEBUG_FAST_OPS
516 if ((inode < bitmap->start) || (inode > bitmap->end)) {
517 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_UNMARK,
518 inode, bitmap->description);
522 ext2fs_fast_clear_bit(inode - bitmap->start, bitmap->bitmap);
525 _INLINE_ int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
528 #ifdef EXT2FS_DEBUG_FAST_OPS
529 if ((inode < bitmap->start) || (inode > bitmap->end)) {
530 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_TEST,
531 inode, bitmap->description);
535 return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap);
538 _INLINE_ blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
540 return bitmap->start;
543 _INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
545 return bitmap->start;
548 _INLINE_ blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
553 _INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
558 _INLINE_ int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
559 blk_t block, int num)
563 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
564 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
565 block, bitmap->description);
568 for (i=0; i < num; i++) {
569 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
575 _INLINE_ int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
576 blk_t block, int num)
580 #ifdef EXT2FS_DEBUG_FAST_OPS
581 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
582 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
583 block, bitmap->description);
587 for (i=0; i < num; i++) {
588 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
594 _INLINE_ void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
595 blk_t block, int num)
599 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
600 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
601 bitmap->description);
604 for (i=0; i < num; i++)
605 ext2fs_fast_set_bit(block + i - bitmap->start, bitmap->bitmap);
608 _INLINE_ void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
609 blk_t block, int num)
613 #ifdef EXT2FS_DEBUG_FAST_OPS
614 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
615 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
616 bitmap->description);
620 for (i=0; i < num; i++)
621 ext2fs_fast_set_bit(block + i - bitmap->start, bitmap->bitmap);
624 _INLINE_ void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
625 blk_t block, int num)
629 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
630 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
631 bitmap->description);
634 for (i=0; i < num; i++)
635 ext2fs_fast_clear_bit(block + i - bitmap->start,
639 _INLINE_ void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
640 blk_t block, int num)
644 #ifdef EXT2FS_DEBUG_FAST_OPS
645 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
646 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
647 bitmap->description);
651 for (i=0; i < num; i++)
652 ext2fs_fast_clear_bit(block + i - bitmap->start,