blob: 96db548d664c1795da806892e047846a8c917e14 [file] [log] [blame]
Mike Frysinger1fd98e02005-05-09 22:10:42 +00001/*
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
Mike Frysingerf98f5e32005-06-11 00:11:46 +000016#include <strings.h>
Mike Frysinger1fd98e02005-05-09 22:10:42 +000017
18extern int ext2fs_set_bit(int nr,void * addr);
19extern int ext2fs_clear_bit(int nr, void * addr);
20extern int ext2fs_test_bit(int nr, const void * addr);
21extern __u16 ext2fs_swab16(__u16 val);
22extern __u32 ext2fs_swab32(__u32 val);
23
24#ifdef WORDS_BIGENDIAN
25#define ext2fs_cpu_to_le32(x) ext2fs_swab32((x))
26#define ext2fs_le32_to_cpu(x) ext2fs_swab32((x))
27#define ext2fs_cpu_to_le16(x) ext2fs_swab16((x))
28#define ext2fs_le16_to_cpu(x) ext2fs_swab16((x))
29#define ext2fs_cpu_to_be32(x) ((__u32)(x))
30#define ext2fs_be32_to_cpu(x) ((__u32)(x))
31#define ext2fs_cpu_to_be16(x) ((__u16)(x))
32#define ext2fs_be16_to_cpu(x) ((__u16)(x))
33#else
34#define ext2fs_cpu_to_le32(x) ((__u32)(x))
35#define ext2fs_le32_to_cpu(x) ((__u32)(x))
36#define ext2fs_cpu_to_le16(x) ((__u16)(x))
37#define ext2fs_le16_to_cpu(x) ((__u16)(x))
38#define ext2fs_cpu_to_be32(x) ext2fs_swab32((x))
39#define ext2fs_be32_to_cpu(x) ext2fs_swab32((x))
40#define ext2fs_cpu_to_be16(x) ext2fs_swab16((x))
41#define ext2fs_be16_to_cpu(x) ext2fs_swab16((x))
42#endif
43
44/*
45 * EXT2FS bitmap manipulation routines.
46 */
47
48/* Support for sending warning messages from the inline subroutines */
49extern const char *ext2fs_block_string;
50extern const char *ext2fs_inode_string;
51extern const char *ext2fs_mark_string;
52extern const char *ext2fs_unmark_string;
53extern const char *ext2fs_test_string;
54extern void ext2fs_warn_bitmap(errcode_t errcode, unsigned long arg,
55 const char *description);
56extern void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap,
57 int code, unsigned long arg);
58
59extern int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap, blk_t block);
60extern int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
61 blk_t block);
62extern int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap, blk_t block);
63
64extern int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap, ext2_ino_t inode);
65extern int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
66 ext2_ino_t inode);
67extern int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap, ext2_ino_t inode);
68
69extern void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
70 blk_t block);
71extern void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
72 blk_t block);
73extern int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
74 blk_t block);
75
76extern void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
77 ext2_ino_t inode);
78extern void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
79 ext2_ino_t inode);
80extern int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
81 ext2_ino_t inode);
82extern blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap);
83extern ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap);
84extern blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap);
85extern ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap);
86
87extern void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
88 blk_t block, int num);
89extern void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
90 blk_t block, int num);
91extern int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
92 blk_t block, int num);
93extern 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);
99extern void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map);
100
101/* These two routines moved to gen_bitmap.c */
102extern int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
103 __u32 bitno);
104extern int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
105 blk_t bitno);
106/*
107 * The inline routines themselves...
108 *
109 * If NO_INLINE_FUNCS is defined, then we won't try to do inline
110 * functions at all; they will be included as normal functions in
111 * inline.c
112 */
113#ifdef NO_INLINE_FUNCS
114#if (defined(__GNUC__) && (defined(__i386__) || defined(__i486__) || \
115 defined(__i586__) || defined(__mc68000__) || \
116 defined(__sparc__)))
117 /* This prevents bitops.c from trying to include the C */
118 /* function version of these functions */
119#define _EXT2_HAVE_ASM_BITOPS_
120#endif
121#endif /* NO_INLINE_FUNCS */
122
123#if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
124#ifdef INCLUDE_INLINE_FUNCS
125#define _INLINE_ extern
126#else
127#ifdef __GNUC__
128#define _INLINE_ extern __inline__
129#else /* For Watcom C */
130#define _INLINE_ extern inline
131#endif
132#endif
133
134#if ((defined __GNUC__) && !defined(_EXT2_USE_C_VERSIONS_) && \
135 (defined(__i386__) || defined(__i486__) || defined(__i586__)))
136
137#define _EXT2_HAVE_ASM_BITOPS_
138#define _EXT2_HAVE_ASM_SWAB_
139#define _EXT2_HAVE_ASM_FINDBIT_
140
141/*
142 * These are done by inline assembly for speed reasons.....
143 *
144 * All bitoperations return 0 if the bit was cleared before the
145 * operation and != 0 if it was not. Bit 0 is the LSB of addr; bit 32
146 * is the LSB of (addr+1).
147 */
148
149/*
150 * Some hacks to defeat gcc over-optimizations..
151 */
152struct __dummy_h { unsigned long a[100]; };
153#define EXT2FS_ADDR (*(struct __dummy_h *) addr)
154#define EXT2FS_CONST_ADDR (*(const struct __dummy_h *) addr)
155
156_INLINE_ int ext2fs_set_bit(int nr, void * addr)
157{
158 int oldbit;
159
160 __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0"
161 :"=r" (oldbit),"=m" (EXT2FS_ADDR)
162 :"r" (nr));
163 return oldbit;
164}
165
166_INLINE_ int ext2fs_clear_bit(int nr, void * addr)
167{
168 int oldbit;
169
170 __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0"
171 :"=r" (oldbit),"=m" (EXT2FS_ADDR)
172 :"r" (nr));
173 return oldbit;
174}
175
176_INLINE_ int ext2fs_test_bit(int nr, const void * addr)
177{
178 int oldbit;
179
180 __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
181 :"=r" (oldbit)
182 :"m" (EXT2FS_CONST_ADDR),"r" (nr));
183 return oldbit;
184}
185
186#if 0
187_INLINE_ int ext2fs_find_first_bit_set(void * addr, unsigned size)
188{
189 int d0, d1, d2;
190 int res;
191
192 if (!size)
193 return 0;
194 /* This looks at memory. Mark it volatile to tell gcc not to move it around */
195 __asm__ __volatile__(
196 "cld\n\t"
197 "xorl %%eax,%%eax\n\t"
198 "xorl %%edx,%%edx\n\t"
199 "repe; scasl\n\t"
200 "je 1f\n\t"
201 "movl -4(%%edi),%%eax\n\t"
202 "subl $4,%%edi\n\t"
203 "bsfl %%eax,%%edx\n"
204 "1:\tsubl %%esi,%%edi\n\t"
205 "shll $3,%%edi\n\t"
206 "addl %%edi,%%edx"
207 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
208 :"1" ((size + 31) >> 5), "2" (addr), "S" (addr));
209 return res;
210}
211
212_INLINE_ int ext2fs_find_next_bit_set (void * addr, int size, int offset)
213{
214 unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
215 int set = 0, bit = offset & 31, res;
216
217 if (bit) {
218 /*
219 * Look for zero in first byte
220 */
221 __asm__("bsfl %1,%0\n\t"
222 "jne 1f\n\t"
223 "movl $32, %0\n"
224 "1:"
225 : "=r" (set)
226 : "r" (*p >> bit));
227 if (set < (32 - bit))
228 return set + offset;
229 set = 32 - bit;
230 p++;
231 }
232 /*
233 * No bit found yet, search remaining full bytes for a bit
234 */
235 res = ext2fs_find_first_bit_set(p, size - 32 * (p - (unsigned long *) addr));
236 return (offset + set + res);
237}
238#endif
239
240#ifdef EXT2FS_ENABLE_SWAPFS
241_INLINE_ __u32 ext2fs_swab32(__u32 val)
242{
243#ifdef EXT2FS_REQUIRE_486
244 __asm__("bswap %0" : "=r" (val) : "0" (val));
245#else
246 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
247 "rorl $16,%0\n\t" /* swap words */
248 "xchgb %b0,%h0" /* swap higher bytes */
249 :"=q" (val)
250 : "0" (val));
251#endif
252 return val;
253}
254
255_INLINE_ __u16 ext2fs_swab16(__u16 val)
256{
257 __asm__("xchgb %b0,%h0" /* swap bytes */ \
258 : "=q" (val) \
259 : "0" (val)); \
260 return val;
261}
262#endif
263
264#undef EXT2FS_ADDR
265
266#endif /* i386 */
267
268#ifdef __mc68000__
269
270#define _EXT2_HAVE_ASM_BITOPS_
271
272_INLINE_ int ext2fs_set_bit(int nr,void * addr)
273{
274 char retval;
275
276 __asm__ __volatile__ ("bfset %2@{%1:#1}; sne %0"
277 : "=d" (retval) : "d" (nr^7), "a" (addr));
278
279 return retval;
280}
281
282_INLINE_ int ext2fs_clear_bit(int nr, void * addr)
283{
284 char retval;
285
286 __asm__ __volatile__ ("bfclr %2@{%1:#1}; sne %0"
287 : "=d" (retval) : "d" (nr^7), "a" (addr));
288
289 return retval;
290}
291
292_INLINE_ int ext2fs_test_bit(int nr, const void * addr)
293{
294 char retval;
295
296 __asm__ __volatile__ ("bftst %2@{%1:#1}; sne %0"
297 : "=d" (retval) : "d" (nr^7), "a" (addr));
298
299 return retval;
300}
301
302#endif /* __mc68000__ */
303
304
305#if !defined(_EXT2_HAVE_ASM_SWAB_) && defined(EXT2FS_ENABLE_SWAPFS)
306
307_INLINE_ __u16 ext2fs_swab16(__u16 val)
308{
309 return (val >> 8) | (val << 8);
310}
311
312_INLINE_ __u32 ext2fs_swab32(__u32 val)
313{
314 return ((val>>24) | ((val>>8)&0xFF00) |
315 ((val<<8)&0xFF0000) | (val<<24));
316}
317
318#endif /* !_EXT2_HAVE_ASM_SWAB */
319
320#if !defined(_EXT2_HAVE_ASM_FINDBIT_)
321_INLINE_ int ext2fs_find_first_bit_set(void * addr, unsigned size)
322{
Mike Frysingera34f9992005-06-11 00:50:59 +0000323 unsigned char *cp = (unsigned char *) addr;
324 int res = 0, d0;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000325
326 if (!size)
327 return 0;
328
329 while ((size > res) && (*cp == 0)) {
330 cp++;
331 res += 8;
332 }
333 d0 = ffs(*cp);
334 if (d0 == 0)
335 return size;
Mike Frysingera34f9992005-06-11 00:50:59 +0000336
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000337 return res + d0 - 1;
338}
339
340_INLINE_ int ext2fs_find_next_bit_set (void * addr, int size, int offset)
341{
342 unsigned char * p;
343 int set = 0, bit = offset & 7, res = 0, d0;
344
345 res = offset >> 3;
346 p = ((unsigned char *) addr) + res;
347
348 if (bit) {
349 set = ffs(*p & ~((1 << bit) - 1));
350 if (set)
351 return (offset & ~7) + set - 1;
352 p++;
353 res += 8;
354 }
355 while ((size > res) && (*p == 0)) {
356 p++;
357 res += 8;
358 }
359 d0 = ffs(*p);
360 if (d0 == 0)
361 return size;
362
363 return (res + d0 - 1);
364}
365#endif
366
367_INLINE_ int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
368 blk_t bitno);
369
370_INLINE_ int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
371 blk_t bitno)
372{
373 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
374 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
375 return 0;
376 }
377 return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
378}
379
380_INLINE_ int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap,
381 blk_t block)
382{
383 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap)
384 bitmap,
385 block);
386}
387
388_INLINE_ int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
389 blk_t block)
390{
391 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
392 block);
393}
394
395_INLINE_ int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
396 blk_t block)
397{
398 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
399 block);
400}
401
402_INLINE_ int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
403 ext2_ino_t inode)
404{
405 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
406 inode);
407}
408
409_INLINE_ int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
410 ext2_ino_t inode)
411{
412 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
413 inode);
414}
415
416_INLINE_ int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
417 ext2_ino_t inode)
418{
419 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
420 inode);
421}
422
423_INLINE_ void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
424 blk_t block)
425{
426#ifdef EXT2FS_DEBUG_FAST_OPS
427 if ((block < bitmap->start) || (block > bitmap->end)) {
428 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
429 bitmap->description);
430 return;
431 }
432#endif
433 ext2fs_set_bit(block - bitmap->start, bitmap->bitmap);
434}
435
436_INLINE_ void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
437 blk_t block)
438{
439#ifdef EXT2FS_DEBUG_FAST_OPS
440 if ((block < bitmap->start) || (block > bitmap->end)) {
441 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK,
442 block, bitmap->description);
443 return;
444 }
445#endif
446 ext2fs_clear_bit(block - bitmap->start, bitmap->bitmap);
447}
448
449_INLINE_ int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
450 blk_t block)
451{
452#ifdef EXT2FS_DEBUG_FAST_OPS
453 if ((block < bitmap->start) || (block > bitmap->end)) {
454 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
455 block, bitmap->description);
456 return 0;
457 }
458#endif
459 return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap);
460}
461
462_INLINE_ void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
463 ext2_ino_t inode)
464{
465#ifdef EXT2FS_DEBUG_FAST_OPS
466 if ((inode < bitmap->start) || (inode > bitmap->end)) {
467 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_MARK,
468 inode, bitmap->description);
469 return;
470 }
471#endif
472 ext2fs_set_bit(inode - bitmap->start, bitmap->bitmap);
473}
474
475_INLINE_ void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
476 ext2_ino_t inode)
477{
478#ifdef EXT2FS_DEBUG_FAST_OPS
479 if ((inode < bitmap->start) || (inode > bitmap->end)) {
480 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_UNMARK,
481 inode, bitmap->description);
482 return;
483 }
484#endif
485 ext2fs_clear_bit(inode - bitmap->start, bitmap->bitmap);
486}
487
488_INLINE_ int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
489 ext2_ino_t inode)
490{
491#ifdef EXT2FS_DEBUG_FAST_OPS
492 if ((inode < bitmap->start) || (inode > bitmap->end)) {
493 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_TEST,
494 inode, bitmap->description);
495 return 0;
496 }
497#endif
498 return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap);
499}
500
501_INLINE_ blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
502{
503 return bitmap->start;
504}
505
506_INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
507{
508 return bitmap->start;
509}
510
511_INLINE_ blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
512{
513 return bitmap->end;
514}
515
516_INLINE_ ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
517{
518 return bitmap->end;
519}
520
521_INLINE_ int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
522 blk_t block, int num)
523{
524 int i;
525
526 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
527 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
528 block, bitmap->description);
529 return 0;
530 }
531 for (i=0; i < num; i++) {
532 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
533 return 0;
534 }
535 return 1;
536}
537
538_INLINE_ int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
539 blk_t block, int num)
540{
541 int i;
542
543#ifdef EXT2FS_DEBUG_FAST_OPS
544 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
545 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
546 block, bitmap->description);
547 return 0;
548 }
549#endif
550 for (i=0; i < num; i++) {
551 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
552 return 0;
553 }
554 return 1;
555}
556
557_INLINE_ void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
558 blk_t block, int num)
559{
560 int i;
561
562 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
563 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
564 bitmap->description);
565 return;
566 }
567 for (i=0; i < num; i++)
568 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
569}
570
571_INLINE_ void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
572 blk_t block, int num)
573{
574 int i;
575
576#ifdef EXT2FS_DEBUG_FAST_OPS
577 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
578 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
579 bitmap->description);
580 return;
581 }
582#endif
583 for (i=0; i < num; i++)
584 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
585}
586
587_INLINE_ void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
588 blk_t block, int num)
589{
590 int i;
591
592 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
593 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
594 bitmap->description);
595 return;
596 }
597 for (i=0; i < num; i++)
598 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
599}
600
601_INLINE_ void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
602 blk_t block, int num)
603{
604 int i;
605
606#ifdef EXT2FS_DEBUG_FAST_OPS
607 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
608 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
609 bitmap->description);
610 return;
611 }
612#endif
613 for (i=0; i < num; i++)
614 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
615}
616#undef _INLINE_
617#endif
618