bitops.h revision e01f9be336274e527cdb794cca2b0c1501e8b0d7
1/* 2 * bitops.h --- Bitmap frobbing code. The byte swapping routines are 3 * also included here. 4 * 5 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. 6 * 7 * %Begin-Header% 8 * This file may be redistributed under the terms of the GNU Public 9 * License. 10 * %End-Header% 11 * 12 * i386 bitops operations taken from <asm/bitops.h>, Copyright 1992, 13 * Linus Torvalds. 14 */ 15 16 17extern int ext2fs_set_bit(unsigned int nr,void * addr); 18extern int ext2fs_clear_bit(unsigned int nr, void * addr); 19extern int ext2fs_test_bit(unsigned int nr, const void * addr); 20extern void ext2fs_fast_set_bit(unsigned int nr,void * addr); 21extern void ext2fs_fast_clear_bit(unsigned int nr, void * addr); 22extern __u16 ext2fs_swab16(__u16 val); 23extern __u32 ext2fs_swab32(__u32 val); 24extern __u64 ext2fs_swab64(__u64 val); 25 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)) 37#else 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)) 48#endif 49 50/* 51 * EXT2FS bitmap manipulation routines. 52 */ 53 54/* Support for sending warning messages from the inline subroutines */ 55extern const char *ext2fs_block_string; 56extern const char *ext2fs_inode_string; 57extern const char *ext2fs_mark_string; 58extern const char *ext2fs_unmark_string; 59extern const char *ext2fs_test_string; 60extern void ext2fs_warn_bitmap(errcode_t errcode, unsigned long arg, 61 const char *description); 62extern void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap, 63 int code, unsigned long arg); 64 65extern int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap, blk_t block); 66extern int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap, 67 blk_t block); 68extern int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap, blk_t block); 69 70extern int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap, ext2_ino_t inode); 71extern int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap, 72 ext2_ino_t inode); 73extern int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap, ext2_ino_t inode); 74 75extern 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, 83 ext2_ino_t inode); 84extern void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap, 85 ext2_ino_t inode); 86extern int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap, 87 ext2_ino_t inode); 88extern blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap); 89extern ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap); 90extern blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap); 91extern ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap); 92 93extern void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap, 94 blk_t block, int num); 95extern void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap, 96 blk_t block, int num); 97extern int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap, 98 blk_t block, int num); 99extern void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap, 100 blk_t block, int num); 101extern void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap, 102 blk_t block, int num); 103extern int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap, 104 blk_t block, int num); 105extern void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map); 106 107/* These routines moved to gen_bitmap.c */ 108extern int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap, 109 __u32 bitno); 110extern int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap, 111 blk_t bitno); 112extern int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap, 113 blk_t bitno); 114extern int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap, 115 blk_t block, int num); 116extern __u32 ext2fs_get_generic_bitmap_start(ext2fs_generic_bitmap bitmap); 117extern __u32 ext2fs_get_generic_bitmap_end(ext2fs_generic_bitmap bitmap); 118 119/* 120 * The inline routines themselves... 121 * 122 * If NO_INLINE_FUNCS is defined, then we won't try to do inline 123 * functions at all; they will be included as normal functions in 124 * inline.c 125 */ 126#ifdef NO_INLINE_FUNCS 127#if (defined(__GNUC__) && (defined(__i386__) || defined(__i486__) || \ 128 defined(__i586__) || defined(__mc68000__))) 129 /* This prevents bitops.c from trying to include the C */ 130 /* function version of these functions */ 131#define _EXT2_HAVE_ASM_BITOPS_ 132#endif 133#endif /* NO_INLINE_FUNCS */ 134 135#if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS)) 136#ifdef INCLUDE_INLINE_FUNCS 137#define _INLINE_ extern 138#else 139#ifdef __GNUC__ 140#define _INLINE_ extern __inline__ 141#else /* For Watcom C */ 142#define _INLINE_ extern inline 143#endif 144#endif 145 146/* 147 * Fast bit set/clear functions that doesn't need to return the 148 * previous bit value. 149 */ 150 151_INLINE_ void ext2fs_fast_set_bit(unsigned int nr,void * addr) 152{ 153 unsigned char *ADDR = (unsigned char *) addr; 154 155 ADDR += nr >> 3; 156 *ADDR |= (1 << (nr & 0x07)); 157} 158 159_INLINE_ void ext2fs_fast_clear_bit(unsigned int nr, void * addr) 160{ 161 unsigned char *ADDR = (unsigned char *) addr; 162 163 ADDR += nr >> 3; 164 *ADDR &= ~(1 << (nr & 0x07)); 165} 166 167 168#if ((defined __GNUC__) && !defined(_EXT2_USE_C_VERSIONS_) && \ 169 (defined(__i386__) || defined(__i486__) || defined(__i586__))) 170 171#define _EXT2_HAVE_ASM_BITOPS_ 172#define _EXT2_HAVE_ASM_SWAB_ 173 174/* 175 * These are done by inline assembly for speed reasons..... 176 * 177 * All bitoperations return 0 if the bit was cleared before the 178 * operation and != 0 if it was not. Bit 0 is the LSB of addr; bit 32 179 * is the LSB of (addr+1). 180 */ 181 182/* 183 * Some hacks to defeat gcc over-optimizations.. 184 */ 185struct __dummy_h { unsigned long a[100]; }; 186#define EXT2FS_ADDR (*(struct __dummy_h *) addr) 187#define EXT2FS_CONST_ADDR (*(const struct __dummy_h *) addr) 188 189_INLINE_ int ext2fs_set_bit(unsigned int nr, void * addr) 190{ 191 int oldbit; 192 193 addr = (void *) (((unsigned char *) addr) + (nr >> 3)); 194 __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0" 195 :"=r" (oldbit),"+m" (EXT2FS_ADDR) 196 :"r" (nr & 7)); 197 return oldbit; 198} 199 200_INLINE_ int ext2fs_clear_bit(unsigned int nr, void * addr) 201{ 202 int oldbit; 203 204 addr = (void *) (((unsigned char *) addr) + (nr >> 3)); 205 __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0" 206 :"=r" (oldbit),"+m" (EXT2FS_ADDR) 207 :"r" (nr & 7)); 208 return oldbit; 209} 210 211_INLINE_ int ext2fs_test_bit(unsigned int nr, const void * addr) 212{ 213 int oldbit; 214 215 addr = (const void *) (((const unsigned char *) addr) + (nr >> 3)); 216 __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0" 217 :"=r" (oldbit) 218 :"m" (EXT2FS_CONST_ADDR),"r" (nr & 7)); 219 return oldbit; 220} 221 222_INLINE_ __u32 ext2fs_swab32(__u32 val) 223{ 224#ifdef EXT2FS_REQUIRE_486 225 __asm__("bswap %0" : "=r" (val) : "0" (val)); 226#else 227 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */ 228 "rorl $16,%0\n\t" /* swap words */ 229 "xchgb %b0,%h0" /* swap higher bytes */ 230 :"=q" (val) 231 : "0" (val)); 232#endif 233 return val; 234} 235 236_INLINE_ __u16 ext2fs_swab16(__u16 val) 237{ 238 __asm__("xchgb %b0,%h0" /* swap bytes */ \ 239 : "=q" (val) \ 240 : "0" (val)); \ 241 return val; 242} 243 244#undef EXT2FS_ADDR 245 246#endif /* i386 */ 247 248#if ((defined __GNUC__) && !defined(_EXT2_USE_C_VERSIONS_) && \ 249 (defined(__mc68000__))) 250 251#define _EXT2_HAVE_ASM_BITOPS_ 252 253_INLINE_ int ext2fs_set_bit(unsigned int nr,void * addr) 254{ 255 char retval; 256 257 __asm__ __volatile__ ("bfset %2@{%1:#1}; sne %0" 258 : "=d" (retval) : "d" (nr^7), "a" (addr)); 259 260 return retval; 261} 262 263_INLINE_ int ext2fs_clear_bit(unsigned int nr, void * addr) 264{ 265 char retval; 266 267 __asm__ __volatile__ ("bfclr %2@{%1:#1}; sne %0" 268 : "=d" (retval) : "d" (nr^7), "a" (addr)); 269 270 return retval; 271} 272 273_INLINE_ int ext2fs_test_bit(unsigned int nr, const void * addr) 274{ 275 char retval; 276 277 __asm__ __volatile__ ("bftst %2@{%1:#1}; sne %0" 278 : "=d" (retval) : "d" (nr^7), "a" (addr)); 279 280 return retval; 281} 282 283#endif /* __mc68000__ */ 284 285 286#if !defined(_EXT2_HAVE_ASM_SWAB_) 287 288_INLINE_ __u16 ext2fs_swab16(__u16 val) 289{ 290 return (val >> 8) | (val << 8); 291} 292 293_INLINE_ __u32 ext2fs_swab32(__u32 val) 294{ 295 return ((val>>24) | ((val>>8)&0xFF00) | 296 ((val<<8)&0xFF0000) | (val<<24)); 297} 298 299#endif /* !_EXT2_HAVE_ASM_SWAB */ 300 301_INLINE_ __u64 ext2fs_swab64(__u64 val) 302{ 303 return (ext2fs_swab32(val >> 32) | 304 (((__u64)ext2fs_swab32(val & 0xFFFFFFFFUL)) << 32)); 305} 306 307_INLINE_ int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap, 308 blk_t block) 309{ 310 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap, 311 block); 312} 313 314_INLINE_ int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap, 315 blk_t block) 316{ 317 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap, 318 block); 319} 320 321_INLINE_ int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap, 322 blk_t block) 323{ 324 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap, 325 block); 326} 327 328_INLINE_ int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap, 329 ext2_ino_t inode) 330{ 331 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap, 332 inode); 333} 334 335_INLINE_ int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap, 336 ext2_ino_t inode) 337{ 338 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap, 339 inode); 340} 341 342_INLINE_ int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap, 343 ext2_ino_t inode) 344{ 345 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap, 346 inode); 347} 348 349_INLINE_ void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap, 350 blk_t block) 351{ 352 ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap, block); 353} 354 355_INLINE_ void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap, 356 blk_t block) 357{ 358 ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap, block); 359} 360 361_INLINE_ int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap, 362 blk_t block) 363{ 364 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap, 365 block); 366} 367 368_INLINE_ void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap, 369 ext2_ino_t inode) 370{ 371 ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap, inode); 372} 373 374_INLINE_ void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap, 375 ext2_ino_t inode) 376{ 377 ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap, inode); 378} 379 380_INLINE_ int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap, 381 ext2_ino_t inode) 382{ 383 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap, 384 inode); 385} 386 387_INLINE_ blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap) 388{ 389 return ext2fs_get_generic_bitmap_start((ext2fs_generic_bitmap) bitmap); 390} 391 392_INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap) 393{ 394 return ext2fs_get_generic_bitmap_start((ext2fs_generic_bitmap) bitmap); 395} 396 397_INLINE_ blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap) 398{ 399 return ext2fs_get_generic_bitmap_end((ext2fs_generic_bitmap) bitmap); 400} 401 402_INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap) 403{ 404 return ext2fs_get_generic_bitmap_end((ext2fs_generic_bitmap) bitmap); 405} 406 407_INLINE_ int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap, 408 blk_t block, int num) 409{ 410 return ext2fs_test_block_bitmap_range(bitmap, block, num); 411} 412 413_INLINE_ void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap, 414 blk_t block, int num) 415{ 416 ext2fs_mark_block_bitmap_range(bitmap, block, num); 417} 418 419_INLINE_ void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap, 420 blk_t block, int num) 421{ 422 ext2fs_unmark_block_bitmap_range(bitmap, block, num); 423} 424#undef _INLINE_ 425#endif 426 427