2 * gen_bitmap.c --- Generic (32-bit) bitmap routines
4 * Copyright (C) 2001 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
24 #include <sys/types.h>
30 struct ext2fs_struct_generic_bitmap {
37 errcode_t base_error_code;
41 #define EXT2FS_IS_32_BITMAP(bmap) \
42 (((bmap)->magic == EXT2_ET_MAGIC_GENERIC_BITMAP) || \
43 ((bmap)->magic == EXT2_ET_MAGIC_BLOCK_BITMAP) || \
44 ((bmap)->magic == EXT2_ET_MAGIC_INODE_BITMAP))
46 #define EXT2FS_IS_64_BITMAP(bmap) \
47 (((bmap)->magic == EXT2_ET_MAGIC_GENERIC_BITMAP64) || \
48 ((bmap)->magic == EXT2_ET_MAGIC_BLOCK_BITMAP64) || \
49 ((bmap)->magic == EXT2_ET_MAGIC_INODE_BITMAP64))
52 * Used by previously inlined function, so we have to export this and
53 * not change the function signature
55 void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap,
56 int code, unsigned long arg)
59 if (bitmap->description)
60 com_err(0, bitmap->base_error_code+code,
61 "#%lu for %s", arg, bitmap->description);
63 com_err(0, bitmap->base_error_code + code, "#%lu", arg);
67 static errcode_t check_magic(ext2fs_generic_bitmap bitmap)
69 if (!bitmap || !((bitmap->magic == EXT2_ET_MAGIC_GENERIC_BITMAP) ||
70 (bitmap->magic == EXT2_ET_MAGIC_INODE_BITMAP) ||
71 (bitmap->magic == EXT2_ET_MAGIC_BLOCK_BITMAP)))
72 return EXT2_ET_MAGIC_GENERIC_BITMAP;
76 errcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs,
77 __u32 start, __u32 end, __u32 real_end,
78 const char *descr, char *init_map,
79 ext2fs_generic_bitmap *ret)
81 ext2fs_generic_bitmap bitmap;
85 retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap),
90 bitmap->magic = magic;
92 bitmap->start = start;
94 bitmap->real_end = real_end;
96 case EXT2_ET_MAGIC_INODE_BITMAP:
97 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
99 case EXT2_ET_MAGIC_BLOCK_BITMAP:
100 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
103 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
106 retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
108 ext2fs_free_mem(&bitmap);
111 strcpy(bitmap->description, descr);
113 bitmap->description = 0;
115 size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
116 /* Round up to allow for the BT x86 instruction */
117 size = (size + 7) & ~3;
118 retval = ext2fs_get_mem(size, &bitmap->bitmap);
120 ext2fs_free_mem(&bitmap->description);
121 ext2fs_free_mem(&bitmap);
126 memcpy(bitmap->bitmap, init_map, size);
128 memset(bitmap->bitmap, 0, size);
133 errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
137 ext2fs_generic_bitmap *ret)
139 return ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_GENERIC_BITMAP, 0,
140 start, end, real_end, descr, 0, ret);
143 errcode_t ext2fs_copy_generic_bitmap(ext2fs_generic_bitmap src,
144 ext2fs_generic_bitmap *dest)
146 return (ext2fs_make_generic_bitmap(src->magic, src->fs,
147 src->start, src->end,
149 src->description, src->bitmap,
153 void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
155 if (check_magic(bitmap))
159 if (bitmap->description) {
160 ext2fs_free_mem(&bitmap->description);
161 bitmap->description = 0;
163 if (bitmap->bitmap) {
164 ext2fs_free_mem(&bitmap->bitmap);
167 ext2fs_free_mem(&bitmap);
170 int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
173 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
174 if (EXT2FS_IS_64_BITMAP(bitmap)) {
175 ext2fs_warn_bitmap32(bitmap, __func__);
176 return ext2fs_test_generic_bmap(bitmap, bitno);
179 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
180 "test_bitmap(%lu)", (unsigned long) bitno);
185 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
186 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
189 return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
192 int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
195 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
196 if (EXT2FS_IS_64_BITMAP(bitmap)) {
197 ext2fs_warn_bitmap32(bitmap, __func__);
198 return ext2fs_mark_generic_bmap(bitmap, bitno);
201 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
202 "mark_bitmap(%lu)", (unsigned long) bitno);
207 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
208 ext2fs_warn_bitmap2(bitmap, EXT2FS_MARK_ERROR, bitno);
211 return ext2fs_set_bit(bitno - bitmap->start, bitmap->bitmap);
214 int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
217 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
218 if (EXT2FS_IS_64_BITMAP(bitmap)) {
219 ext2fs_warn_bitmap32(bitmap, __func__);
220 return ext2fs_unmark_generic_bmap(bitmap, bitno);
223 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
224 "mark_bitmap(%lu)", (unsigned long) bitno);
229 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
230 ext2fs_warn_bitmap2(bitmap, EXT2FS_UNMARK_ERROR, bitno);
233 return ext2fs_clear_bit(bitno - bitmap->start, bitmap->bitmap);
236 __u32 ext2fs_get_generic_bitmap_start(ext2fs_generic_bitmap bitmap)
238 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
239 if (EXT2FS_IS_64_BITMAP(bitmap)) {
240 ext2fs_warn_bitmap32(bitmap, __func__);
241 return ext2fs_get_generic_bmap_start(bitmap);
244 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
250 return bitmap->start;
253 __u32 ext2fs_get_generic_bitmap_end(ext2fs_generic_bitmap bitmap)
255 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
256 if (EXT2FS_IS_64_BITMAP(bitmap)) {
257 ext2fs_warn_bitmap32(bitmap, __func__);
258 return ext2fs_get_generic_bmap_end(bitmap);
261 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
269 void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap)
271 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
272 if (EXT2FS_IS_64_BITMAP(bitmap)) {
273 ext2fs_warn_bitmap32(bitmap, __func__);
274 ext2fs_clear_generic_bmap(bitmap);
278 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
279 "clear_generic_bitmap");
284 memset(bitmap->bitmap, 0,
285 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
288 errcode_t ext2fs_fudge_generic_bitmap_end(ext2fs_inode_bitmap bitmap,
289 errcode_t magic, errcode_t neq,
290 ext2_ino_t end, ext2_ino_t *oend)
292 EXT2_CHECK_MAGIC(bitmap, magic);
294 if (end > bitmap->real_end)
302 errcode_t ext2fs_resize_generic_bitmap(errcode_t magic,
303 __u32 new_end, __u32 new_real_end,
304 ext2fs_generic_bitmap bmap)
307 size_t size, new_size;
310 if (!bmap || (bmap->magic != magic))
314 * If we're expanding the bitmap, make sure all of the new
315 * parts of the bitmap are zero.
317 if (new_end > bmap->end) {
318 bitno = bmap->real_end;
321 for (; bitno > bmap->end; bitno--)
322 ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
324 if (new_real_end == bmap->real_end) {
329 size = ((bmap->real_end - bmap->start) / 8) + 1;
330 new_size = ((new_real_end - bmap->start) / 8) + 1;
332 if (size != new_size) {
333 retval = ext2fs_resize_mem(size, new_size, &bmap->bitmap);
338 memset(bmap->bitmap + size, 0, new_size - size);
341 bmap->real_end = new_real_end;
345 errcode_t ext2fs_compare_generic_bitmap(errcode_t magic, errcode_t neq,
346 ext2fs_generic_bitmap bm1,
347 ext2fs_generic_bitmap bm2)
351 if (!bm1 || bm1->magic != magic)
353 if (!bm2 || bm2->magic != magic)
356 if ((bm1->start != bm2->start) ||
357 (bm1->end != bm2->end) ||
358 (memcmp(bm1->bitmap, bm2->bitmap,
359 (size_t) (bm1->end - bm1->start)/8)))
362 for (i = bm1->end - ((bm1->end - bm1->start) % 8); i <= bm1->end; i++)
363 if (ext2fs_fast_test_block_bitmap(bm1, i) !=
364 ext2fs_fast_test_block_bitmap(bm2, i))
370 void ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map)
374 /* Protect loop from wrap-around if map->real_end is maxed */
375 for (i=map->end+1, j = i - map->start;
376 i <= map->real_end && i > map->end;
378 ext2fs_set_bit(j, map->bitmap);
381 errcode_t ext2fs_get_generic_bitmap_range(ext2fs_generic_bitmap bmap,
383 __u32 start, __u32 num,
386 if (!bmap || (bmap->magic != magic))
389 if ((start < bmap->start) || (start+num-1 > bmap->real_end))
390 return EXT2_ET_INVALID_ARGUMENT;
392 memcpy(out, bmap->bitmap + (start >> 3), (num+7) >> 3);
396 errcode_t ext2fs_set_generic_bitmap_range(ext2fs_generic_bitmap bmap,
398 __u32 start, __u32 num,
401 if (!bmap || (bmap->magic != magic))
404 if ((start < bmap->start) || (start+num-1 > bmap->real_end))
405 return EXT2_ET_INVALID_ARGUMENT;
407 memcpy(bmap->bitmap + (start >> 3), in, (num+7) >> 3);
412 * Compare @mem to zero buffer by 256 bytes.
413 * Return 1 if @mem is zeroed memory, otherwise return 0.
415 int ext2fs_mem_is_zero(const char *mem, size_t len)
417 static const char zero_buf[256];
419 while (len >= sizeof(zero_buf)) {
420 if (memcmp(mem, zero_buf, sizeof(zero_buf)))
422 len -= sizeof(zero_buf);
423 mem += sizeof(zero_buf);
425 /* Deal with leftover bytes. */
427 return !memcmp(mem, zero_buf, len);
432 * Return true if all of the bits in a specified range are clear
434 static int ext2fs_test_clear_generic_bitmap_range(ext2fs_generic_bitmap bitmap,
438 size_t start_byte, len_byte = len >> 3;
439 unsigned int start_bit, len_bit = len % 8;
445 const char *ADDR = bitmap->bitmap;
447 start -= bitmap->start;
448 start_byte = start >> 3;
449 start_bit = start % 8;
451 if (start_bit != 0) {
453 * The compared start block number or start inode number
454 * is not the first bit in a byte.
456 mark_count = 8 - start_bit;
457 if (len < 8 - start_bit) {
458 mark_count = (int)len;
459 mark_bit = len + start_bit - 1;
463 for (i = mark_count; i > 0; i--, mark_bit--)
464 first_bit |= 1 << mark_bit;
467 * Compare blocks or inodes in the first byte.
468 * If there is any marked bit, this function returns 0.
470 if (first_bit & ADDR[start_byte])
472 else if (len <= 8 - start_bit)
476 len_bit = (len - mark_count) % 8;
477 len_byte = (len - mark_count) >> 3;
481 * The compared start block number or start inode number is
482 * the first bit in a byte.
486 * The compared end block number or end inode number is
487 * not the last bit in a byte.
489 for (mark_bit = len_bit - 1; mark_bit >= 0; mark_bit--)
490 last_bit |= 1 << mark_bit;
493 * Compare blocks or inodes in the last byte.
494 * If there is any marked bit, this function returns 0.
496 if (last_bit & ADDR[start_byte + len_byte])
498 else if (len_byte == 0)
502 /* Check whether all bytes are 0 */
503 return ext2fs_mem_is_zero(ADDR + start_byte, len_byte);
506 int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
507 blk_t block, int num)
509 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
510 if ((block < bitmap->start) || (block+num-1 > bitmap->real_end)) {
511 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
512 block, bitmap->description);
515 return ext2fs_test_clear_generic_bitmap_range((ext2fs_generic_bitmap)
519 int ext2fs_test_inode_bitmap_range(ext2fs_inode_bitmap bitmap,
520 ino_t inode, int num)
522 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
523 if ((inode < bitmap->start) || (inode+num-1 > bitmap->real_end)) {
524 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_TEST,
525 inode, bitmap->description);
528 return ext2fs_test_clear_generic_bitmap_range((ext2fs_generic_bitmap)
532 void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
533 blk_t block, int num)
537 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
538 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
539 bitmap->description);
542 for (i=0; i < num; i++)
543 ext2fs_fast_set_bit(block + i - bitmap->start, bitmap->bitmap);
546 void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
547 blk_t block, int num)
551 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
552 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
553 bitmap->description);
556 for (i=0; i < num; i++)
557 ext2fs_fast_clear_bit(block + i - bitmap->start,