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(int nr
,void * addr
);
18 extern int ext2fs_clear_bit(int nr
, void * addr
);
19 extern int ext2fs_test_bit(int nr
, const void * addr
);
20 extern __u16
ext2fs_swab16(__u16 val
);
21 extern __u32
ext2fs_swab32(__u32 val
);
24 * EXT2FS bitmap manipulation routines.
27 /* Support for sending warning messages from the inline subroutines */
28 extern const char *ext2fs_block_string
;
29 extern const char *ext2fs_inode_string
;
30 extern const char *ext2fs_mark_string
;
31 extern const char *ext2fs_unmark_string
;
32 extern const char *ext2fs_test_string
;
33 extern void ext2fs_warn_bitmap(errcode_t errcode
, unsigned long arg
,
34 const char *description
);
35 extern void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap
,
36 int code
, unsigned long arg
);
38 extern void ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap
, blk_t block
);
39 extern void ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap
,
41 extern int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap
, blk_t block
);
43 extern void ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap
, ino_t inode
);
44 extern void ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap
,
46 extern int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap
, ino_t inode
);
48 extern void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap
,
50 extern void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap
,
52 extern int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap
,
55 extern void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap
,
57 extern void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap
,
59 extern int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap
,
61 extern blk_t
ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap
);
62 extern ino_t
ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap
);
63 extern blk_t
ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap
);
64 extern ino_t
ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap
);
66 extern void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap
,
67 blk_t block
, int num
);
68 extern void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap
,
69 blk_t block
, int num
);
70 extern int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap
,
71 blk_t block
, int num
);
72 extern void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap
,
73 blk_t block
, int num
);
74 extern void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap
,
75 blk_t block
, int num
);
76 extern int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap
,
77 blk_t block
, int num
);
80 * The inline routines themselves...
82 * If NO_INLINE_FUNCS is defined, then we won't try to do inline
83 * functions at all; they will be included as normal functions in
86 #ifdef NO_INLINE_FUNCS
87 #if (defined(__i386__) || defined(__i486__) || defined(__i586__) || \
88 defined(__mc68000__) || defined(__sparc__))
89 /* This prevents bitops.c from trying to include the C */
90 /* function version of these functions */
91 #define _EXT2_HAVE_ASM_BITOPS_
93 #endif /* NO_INLINE_FUNCS */
95 #if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
96 #ifdef INCLUDE_INLINE_FUNCS
97 #define _INLINE_ extern
99 #define _INLINE_ extern __inline__
102 #if (defined(__i386__) || defined(__i486__) || defined(__i586__))
104 #define _EXT2_HAVE_ASM_BITOPS_
107 * These are done by inline assembly for speed reasons.....
109 * All bitoperations return 0 if the bit was cleared before the
110 * operation and != 0 if it was not. Bit 0 is the LSB of addr; bit 32
111 * is the LSB of (addr+1).
115 * Some hacks to defeat gcc over-optimizations..
117 struct __dummy_h
{ unsigned long a
[100]; };
118 #define EXT2FS_ADDR (*(struct __dummy_h *) addr)
119 #define EXT2FS_CONST_ADDR (*(const struct __dummy_h *) addr)
121 _INLINE_
int ext2fs_set_bit(int nr
, void * addr
)
125 __asm__
__volatile__("btsl %2,%1\n\tsbbl %0,%0"
126 :"=r" (oldbit
),"=m" (EXT2FS_ADDR
)
131 _INLINE_
int ext2fs_clear_bit(int nr
, void * addr
)
135 __asm__
__volatile__("btrl %2,%1\n\tsbbl %0,%0"
136 :"=r" (oldbit
),"=m" (EXT2FS_ADDR
)
141 _INLINE_
int ext2fs_test_bit(int nr
, const void * addr
)
145 __asm__
__volatile__("btl %2,%1\n\tsbbl %0,%0"
147 :"m" (EXT2FS_CONST_ADDR
),"r" (nr
));
157 #define _EXT2_HAVE_ASM_BITOPS_
159 _INLINE_
int ext2fs_set_bit(int nr
,void * addr
)
163 __asm__
__volatile__ ("bfset %2@{%1:#1}; sne %0"
164 : "=d" (retval
) : "d" (nr
^7), "a" (addr
));
169 _INLINE_
int ext2fs_clear_bit(int nr
, void * addr
)
173 __asm__
__volatile__ ("bfclr %2@{%1:#1}; sne %0"
174 : "=d" (retval
) : "d" (nr
^7), "a" (addr
));
179 _INLINE_
int ext2fs_test_bit(int nr
, const void * addr
)
183 __asm__
__volatile__ ("bftst %2@{%1:#1}; sne %0"
184 : "=d" (retval
) : "d" (nr
^7), "a" (addr
));
189 #endif /* __mc68000__ */
193 #define _EXT2_HAVE_ASM_BITOPS_
195 #ifndef EXT2_OLD_BITOPS
198 * Do the bitops so that we are compatible with the standard i386
202 _INLINE_
int ext2fs_set_bit(int nr
,void * addr
)
206 unsigned char *ADDR
= (unsigned char *) addr
;
209 mask
= 1 << (nr
& 0x07);
210 __asm__
__volatile__("ldub [%0], %%g6\n\t"
211 "or %%g6, %2, %%g5\n\t"
215 : "0" (ADDR
), "r" (mask
)
220 unsigned char *ADDR
= (unsigned char *) addr
;
223 mask
= 1 << (nr
& 0x07);
224 retval
= (mask
& *ADDR
) != 0;
230 _INLINE_
int ext2fs_clear_bit(int nr
, void * addr
)
234 unsigned char *ADDR
= (unsigned char *) addr
;
237 mask
= 1 << (nr
& 0x07);
238 __asm__
__volatile__("ldub [%0], %%g6\n\t"
239 "andn %%g6, %2, %%g5\n\t"
243 : "0" (ADDR
), "r" (mask
)
249 unsigned char *ADDR
= (unsigned char *) addr
;
252 mask
= 1 << (nr
& 0x07);
253 retval
= (mask
& *ADDR
) != 0;
259 _INLINE_
int ext2fs_test_bit(int nr
, const void * addr
)
262 const unsigned char *ADDR
= (const unsigned char *) addr
;
265 mask
= 1 << (nr
& 0x07);
266 return ((mask
& *ADDR
) != 0);
271 /* Do things the old, unplesant way. */
273 _INLINE_
int ext2fs_set_bit(int nr
, void *addr
)
276 unsigned long *ADDR
= (unsigned long *) addr
;
279 mask
= 1 << (nr
& 31);
280 retval
= ((mask
& *ADDR
) != 0);
285 _INLINE_
int ext2fs_clear_bit(int nr
, void *addr
)
288 unsigned long *ADDR
= (unsigned long *) addr
;
291 mask
= 1 << (nr
& 31);
292 retval
= ((mask
& *ADDR
) != 0);
297 _INLINE_
int ext2fs_test_bit(int nr
, const void *addr
)
300 const unsigned long *ADDR
= (const unsigned long *) addr
;
303 mask
= 1 << (nr
& 31);
304 return ((mask
& *ADDR
) != 0);
308 #endif /* __sparc__ */
310 #ifndef _EXT2_HAVE_ASM_SWAB
312 _INLINE_ __u16
ext2fs_swab16(__u16 val
)
314 return (val
>> 8) | (val
<< 8);
317 _INLINE_ __u32
ext2fs_swab32(__u32 val
)
319 return ((val
>>24) | ((val
>>8)&0xFF00) |
320 ((val
<<8)&0xFF0000) | (val
<<24));
323 #endif /* !_EXT2_HAVE_ASM_SWAB */
325 _INLINE_
void ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap
,
327 _INLINE_
void ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap
,
329 _INLINE_
int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap
,
332 _INLINE_
void ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap
,
335 if ((bitno
< bitmap
->start
) || (bitno
> bitmap
->end
)) {
336 ext2fs_warn_bitmap2(bitmap
, EXT2FS_MARK_ERROR
, bitno
);
339 ext2fs_set_bit(bitno
- bitmap
->start
, bitmap
->bitmap
);
342 _INLINE_
void ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap
,
345 if ((bitno
< bitmap
->start
) || (bitno
> bitmap
->end
)) {
346 ext2fs_warn_bitmap2(bitmap
, EXT2FS_UNMARK_ERROR
, bitno
);
349 ext2fs_clear_bit(bitno
- bitmap
->start
, bitmap
->bitmap
);
352 _INLINE_
int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap
,
355 if ((bitno
< bitmap
->start
) || (bitno
> bitmap
->end
)) {
356 ext2fs_warn_bitmap2(bitmap
, EXT2FS_UNMARK_ERROR
, bitno
);
359 return ext2fs_test_bit(bitno
- bitmap
->start
, bitmap
->bitmap
);
362 _INLINE_
void ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap
,
365 ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap
) bitmap
, block
);
368 _INLINE_
void ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap
,
371 ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap
) bitmap
, block
);
374 _INLINE_
int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap
,
377 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap
) bitmap
,
381 _INLINE_
void ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap
,
384 ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap
) bitmap
, inode
);
387 _INLINE_
void ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap
,
390 ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap
) bitmap
, inode
);
393 _INLINE_
int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap
,
396 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap
) bitmap
,
400 _INLINE_
void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap
,
403 #ifdef EXT2FS_DEBUG_FAST_OPS
404 if ((block
< bitmap
->start
) || (block
> bitmap
->end
)) {
405 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK
, block
,
406 bitmap
->description
);
410 ext2fs_set_bit(block
- bitmap
->start
, bitmap
->bitmap
);
413 _INLINE_
void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap
,
416 #ifdef EXT2FS_DEBUG_FAST_OPS
417 if ((block
< bitmap
->start
) || (block
> bitmap
->end
)) {
418 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK
,
419 block
, bitmap
->description
);
423 ext2fs_clear_bit(block
- bitmap
->start
, bitmap
->bitmap
);
426 _INLINE_
int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap
,
429 #ifdef EXT2FS_DEBUG_FAST_OPS
430 if ((block
< bitmap
->start
) || (block
> bitmap
->end
)) {
431 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST
,
432 block
, bitmap
->description
);
436 return ext2fs_test_bit(block
- bitmap
->start
, bitmap
->bitmap
);
439 _INLINE_
void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap
,
442 #ifdef EXT2FS_DEBUG_FAST_OPS
443 if ((inode
< bitmap
->start
) || (inode
> bitmap
->end
)) {
444 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_MARK
,
445 inode
, bitmap
->description
);
449 ext2fs_set_bit(inode
- bitmap
->start
, bitmap
->bitmap
);
452 _INLINE_
void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap
,
455 #ifdef EXT2FS_DEBUG_FAST_OPS
456 if ((inode
< bitmap
->start
) || (inode
> bitmap
->end
)) {
457 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_UNMARK
,
458 inode
, bitmap
->description
);
462 ext2fs_clear_bit(inode
- bitmap
->start
, bitmap
->bitmap
);
465 _INLINE_
int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap
,
468 #ifdef EXT2FS_DEBUG_FAST_OPS
469 if ((inode
< bitmap
->start
) || (inode
> bitmap
->end
)) {
470 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_TEST
,
471 inode
, bitmap
->description
);
475 return ext2fs_test_bit(inode
- bitmap
->start
, bitmap
->bitmap
);
478 _INLINE_ blk_t
ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap
)
480 return bitmap
->start
;
483 _INLINE_ ino_t
ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap
)
485 return bitmap
->start
;
488 _INLINE_ blk_t
ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap
)
493 _INLINE_ ino_t
ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap
)
498 _INLINE_
int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap
,
499 blk_t block
, int num
)
503 if ((block
< bitmap
->start
) || (block
+num
-1 > bitmap
->end
)) {
504 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST
,
505 block
, bitmap
->description
);
508 for (i
=0; i
< num
; i
++) {
509 if (ext2fs_fast_test_block_bitmap(bitmap
, block
+i
))
515 _INLINE_
int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap
,
516 blk_t block
, int num
)
520 #ifdef EXT2FS_DEBUG_FAST_OPS
521 if ((block
< bitmap
->start
) || (block
+num
-1 > bitmap
->end
)) {
522 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST
,
523 block
, bitmap
->description
);
527 for (i
=0; i
< num
; i
++) {
528 if (ext2fs_fast_test_block_bitmap(bitmap
, block
+i
))
534 _INLINE_
void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap
,
535 blk_t block
, int num
)
539 if ((block
< bitmap
->start
) || (block
+num
-1 > bitmap
->end
)) {
540 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK
, block
,
541 bitmap
->description
);
544 for (i
=0; i
< num
; i
++)
545 ext2fs_set_bit(block
+ i
- bitmap
->start
, bitmap
->bitmap
);
548 _INLINE_
void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap
,
549 blk_t block
, int num
)
553 #ifdef EXT2FS_DEBUG_FAST_OPS
554 if ((block
< bitmap
->start
) || (block
+num
-1 > bitmap
->end
)) {
555 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK
, block
,
556 bitmap
->description
);
560 for (i
=0; i
< num
; i
++)
561 ext2fs_set_bit(block
+ i
- bitmap
->start
, bitmap
->bitmap
);
564 _INLINE_
void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap
,
565 blk_t block
, int num
)
569 if ((block
< bitmap
->start
) || (block
+num
-1 > bitmap
->end
)) {
570 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK
, block
,
571 bitmap
->description
);
574 for (i
=0; i
< num
; i
++)
575 ext2fs_clear_bit(block
+ i
- bitmap
->start
, bitmap
->bitmap
);
578 _INLINE_
void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap
,
579 blk_t block
, int num
)
583 #ifdef EXT2FS_DEBUG_FAST_OPS
584 if ((block
< bitmap
->start
) || (block
+num
-1 > bitmap
->end
)) {
585 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK
, block
,
586 bitmap
->description
);
590 for (i
=0; i
< num
; i
++)
591 ext2fs_clear_bit(block
+ i
- bitmap
->start
, bitmap
->bitmap
);