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.
25 #include <sys/types.h>
31 struct ext2fs_struct_generic_bitmap {
38 errcode_t base_error_code;
42 #define EXT2FS_IS_32_BITMAP(bmap) \
43 (((bmap)->magic == EXT2_ET_MAGIC_GENERIC_BITMAP) || \
44 ((bmap)->magic == EXT2_ET_MAGIC_BLOCK_BITMAP) || \
45 ((bmap)->magic == EXT2_ET_MAGIC_INODE_BITMAP))
47 #define EXT2FS_IS_64_BITMAP(bmap) \
48 (((bmap)->magic == EXT2_ET_MAGIC_GENERIC_BITMAP64) || \
49 ((bmap)->magic == EXT2_ET_MAGIC_BLOCK_BITMAP64) || \
50 ((bmap)->magic == EXT2_ET_MAGIC_INODE_BITMAP64))
53 * Used by previously inlined function, so we have to export this and
54 * not change the function signature
56 void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap,
57 int code, unsigned long arg)
60 if (bitmap->description)
61 com_err(0, bitmap->base_error_code+code,
62 "#%lu for %s", arg, bitmap->description);
64 com_err(0, bitmap->base_error_code + code, "#%lu", arg);
68 static errcode_t check_magic(ext2fs_generic_bitmap bitmap)
70 if (!bitmap || !((bitmap->magic == EXT2_ET_MAGIC_GENERIC_BITMAP) ||
71 (bitmap->magic == EXT2_ET_MAGIC_INODE_BITMAP) ||
72 (bitmap->magic == EXT2_ET_MAGIC_BLOCK_BITMAP)))
73 return EXT2_ET_MAGIC_GENERIC_BITMAP;
77 errcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs,
78 __u32 start, __u32 end, __u32 real_end,
79 const char *descr, char *init_map,
80 ext2fs_generic_bitmap *ret)
82 ext2fs_generic_bitmap bitmap;
86 retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap),
91 bitmap->magic = magic;
93 bitmap->start = start;
95 bitmap->real_end = real_end;
97 case EXT2_ET_MAGIC_INODE_BITMAP:
98 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
100 case EXT2_ET_MAGIC_BLOCK_BITMAP:
101 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
104 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
107 retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
109 ext2fs_free_mem(&bitmap);
112 strcpy(bitmap->description, descr);
114 bitmap->description = 0;
116 size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
117 /* Round up to allow for the BT x86 instruction */
118 size = (size + 7) & ~3;
119 retval = ext2fs_get_mem(size, &bitmap->bitmap);
121 ext2fs_free_mem(&bitmap->description);
122 ext2fs_free_mem(&bitmap);
127 memcpy(bitmap->bitmap, init_map, size);
129 memset(bitmap->bitmap, 0, size);
134 errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
138 ext2fs_generic_bitmap *ret)
140 return ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_GENERIC_BITMAP, 0,
141 start, end, real_end, descr, 0, ret);
144 errcode_t ext2fs_copy_generic_bitmap(ext2fs_generic_bitmap src,
145 ext2fs_generic_bitmap *dest)
147 return (ext2fs_make_generic_bitmap(src->magic, src->fs,
148 src->start, src->end,
150 src->description, src->bitmap,
154 void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
156 if (check_magic(bitmap))
160 if (bitmap->description) {
161 ext2fs_free_mem(&bitmap->description);
162 bitmap->description = 0;
164 if (bitmap->bitmap) {
165 ext2fs_free_mem(&bitmap->bitmap);
168 ext2fs_free_mem(&bitmap);
171 int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
174 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
175 if (EXT2FS_IS_64_BITMAP(bitmap)) {
176 ext2fs_warn_bitmap32(bitmap, __func__);
177 return ext2fs_test_generic_bmap(bitmap, bitno);
180 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
181 "test_bitmap(%lu)", (unsigned long) bitno);
186 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
187 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
190 return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
193 int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
196 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
197 if (EXT2FS_IS_64_BITMAP(bitmap)) {
198 ext2fs_warn_bitmap32(bitmap, __func__);
199 return ext2fs_mark_generic_bmap(bitmap, bitno);
202 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
203 "mark_bitmap(%lu)", (unsigned long) bitno);
208 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
209 ext2fs_warn_bitmap2(bitmap, EXT2FS_MARK_ERROR, bitno);
212 return ext2fs_set_bit(bitno - bitmap->start, bitmap->bitmap);
215 int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
218 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
219 if (EXT2FS_IS_64_BITMAP(bitmap)) {
220 ext2fs_warn_bitmap32(bitmap, __func__);
221 return ext2fs_unmark_generic_bmap(bitmap, bitno);
224 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
225 "mark_bitmap(%lu)", (unsigned long) bitno);
230 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
231 ext2fs_warn_bitmap2(bitmap, EXT2FS_UNMARK_ERROR, bitno);
234 return ext2fs_clear_bit(bitno - bitmap->start, bitmap->bitmap);
237 __u32 ext2fs_get_generic_bitmap_start(ext2fs_generic_bitmap bitmap)
239 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
240 if (EXT2FS_IS_64_BITMAP(bitmap)) {
241 ext2fs_warn_bitmap32(bitmap, __func__);
242 return ext2fs_get_generic_bmap_start(bitmap);
245 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
251 return bitmap->start;
254 __u32 ext2fs_get_generic_bitmap_end(ext2fs_generic_bitmap bitmap)
256 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
257 if (EXT2FS_IS_64_BITMAP(bitmap)) {
258 ext2fs_warn_bitmap32(bitmap, __func__);
259 return ext2fs_get_generic_bmap_end(bitmap);
262 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
270 void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap)
272 if (!EXT2FS_IS_32_BITMAP(bitmap)) {
273 if (EXT2FS_IS_64_BITMAP(bitmap)) {
274 ext2fs_warn_bitmap32(bitmap, __func__);
275 ext2fs_clear_generic_bmap(bitmap);
279 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
280 "clear_generic_bitmap");
285 memset(bitmap->bitmap, 0,
286 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
289 errcode_t ext2fs_fudge_generic_bitmap_end(ext2fs_inode_bitmap bitmap,
290 errcode_t magic, errcode_t neq,
291 ext2_ino_t end, ext2_ino_t *oend)
293 EXT2_CHECK_MAGIC(bitmap, magic);
295 if (end > bitmap->real_end)
303 errcode_t ext2fs_resize_generic_bitmap(errcode_t magic,
304 __u32 new_end, __u32 new_real_end,
305 ext2fs_generic_bitmap bmap)
308 size_t size, new_size;
311 if (!bmap || (bmap->magic != magic))
315 * If we're expanding the bitmap, make sure all of the new
316 * parts of the bitmap are zero.
318 if (new_end > bmap->end) {
319 bitno = bmap->real_end;
322 for (; bitno > bmap->end; bitno--)
323 ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
325 if (new_real_end == bmap->real_end) {
330 size = ((bmap->real_end - bmap->start) / 8) + 1;
331 new_size = ((new_real_end - bmap->start) / 8) + 1;
333 if (size != new_size) {
334 retval = ext2fs_resize_mem(size, new_size, &bmap->bitmap);
339 memset(bmap->bitmap + size, 0, new_size - size);
342 bmap->real_end = new_real_end;
346 errcode_t ext2fs_compare_generic_bitmap(errcode_t magic, errcode_t neq,
347 ext2fs_generic_bitmap bm1,
348 ext2fs_generic_bitmap bm2)
352 if (!bm1 || bm1->magic != magic)
354 if (!bm2 || bm2->magic != magic)
357 if ((bm1->start != bm2->start) ||
358 (bm1->end != bm2->end) ||
359 (memcmp(bm1->bitmap, bm2->bitmap,
360 (size_t) (bm1->end - bm1->start)/8)))
363 for (i = bm1->end - ((bm1->end - bm1->start) % 8); i <= bm1->end; i++)
364 if (ext2fs_fast_test_block_bitmap(bm1, i) !=
365 ext2fs_fast_test_block_bitmap(bm2, i))
371 void ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map)
375 /* Protect loop from wrap-around if map->real_end is maxed */
376 for (i=map->end+1, j = i - map->start;
377 i <= map->real_end && i > map->end;
379 ext2fs_set_bit(j, map->bitmap);
382 errcode_t ext2fs_get_generic_bitmap_range(ext2fs_generic_bitmap bmap,
384 __u32 start, __u32 num,
387 if (!bmap || (bmap->magic != magic))
390 if ((start < bmap->start) || (start+num-1 > bmap->real_end))
391 return EXT2_ET_INVALID_ARGUMENT;
393 memcpy(out, bmap->bitmap + (start >> 3), (num+7) >> 3);
397 errcode_t ext2fs_set_generic_bitmap_range(ext2fs_generic_bitmap bmap,
399 __u32 start, __u32 num,
402 if (!bmap || (bmap->magic != magic))
405 if ((start < bmap->start) || (start+num-1 > bmap->real_end))
406 return EXT2_ET_INVALID_ARGUMENT;
408 memcpy(bmap->bitmap + (start >> 3), in, (num+7) >> 3);
413 * Compare @mem to zero buffer by 256 bytes.
414 * Return 1 if @mem is zeroed memory, otherwise return 0.
416 int ext2fs_mem_is_zero(const char *mem, size_t len)
418 static const char zero_buf[256];
420 while (len >= sizeof(zero_buf)) {
421 if (memcmp(mem, zero_buf, sizeof(zero_buf)))
423 len -= sizeof(zero_buf);
424 mem += sizeof(zero_buf);
426 /* Deal with leftover bytes. */
428 return !memcmp(mem, zero_buf, len);
433 * Return true if all of the bits in a specified range are clear
435 static int ext2fs_test_clear_generic_bitmap_range(ext2fs_generic_bitmap bitmap,
439 size_t start_byte, len_byte = len >> 3;
440 unsigned int start_bit, len_bit = len % 8;
446 const char *ADDR = bitmap->bitmap;
448 start -= bitmap->start;
449 start_byte = start >> 3;
450 start_bit = start % 8;
452 if (start_bit != 0) {
454 * The compared start block number or start inode number
455 * is not the first bit in a byte.
457 mark_count = 8 - start_bit;
458 if (len < 8 - start_bit) {
459 mark_count = (int)len;
460 mark_bit = len + start_bit - 1;
464 for (i = mark_count; i > 0; i--, mark_bit--)
465 first_bit |= 1 << mark_bit;
468 * Compare blocks or inodes in the first byte.
469 * If there is any marked bit, this function returns 0.
471 if (first_bit & ADDR[start_byte])
473 else if (len <= 8 - start_bit)
477 len_bit = (len - mark_count) % 8;
478 len_byte = (len - mark_count) >> 3;
482 * The compared start block number or start inode number is
483 * the first bit in a byte.
487 * The compared end block number or end inode number is
488 * not the last bit in a byte.
490 for (mark_bit = len_bit - 1; mark_bit >= 0; mark_bit--)
491 last_bit |= 1 << mark_bit;
494 * Compare blocks or inodes in the last byte.
495 * If there is any marked bit, this function returns 0.
497 if (last_bit & ADDR[start_byte + len_byte])
499 else if (len_byte == 0)
503 /* Check whether all bytes are 0 */
504 return ext2fs_mem_is_zero(ADDR + start_byte, len_byte);
507 errcode_t ext2fs_find_first_zero_generic_bitmap(ext2fs_generic_bitmap bitmap,
508 __u32 start, __u32 end,
513 if (start < bitmap->start || end > bitmap->end || start > end) {
514 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, start);
518 while (start <= end) {
519 b = ext2fs_test_bit(start - bitmap->start, bitmap->bitmap);
530 errcode_t ext2fs_find_first_set_generic_bitmap(ext2fs_generic_bitmap bitmap,
531 __u32 start, __u32 end,
536 if (start < bitmap->start || end > bitmap->end || start > end) {
537 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, start);
541 while (start <= end) {
542 b = ext2fs_test_bit(start - bitmap->start, bitmap->bitmap);
553 int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
554 blk_t block, int num)
556 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
557 if ((block < bitmap->start) || (block+num-1 > bitmap->real_end)) {
558 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
559 block, bitmap->description);
562 return ext2fs_test_clear_generic_bitmap_range((ext2fs_generic_bitmap)
566 int ext2fs_test_inode_bitmap_range(ext2fs_inode_bitmap bitmap,
567 ext2_ino_t inode, int num)
569 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
570 if ((inode < bitmap->start) || (inode+num-1 > bitmap->real_end)) {
571 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_TEST,
572 inode, bitmap->description);
575 return ext2fs_test_clear_generic_bitmap_range((ext2fs_generic_bitmap)
579 void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
580 blk_t block, int num)
584 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
585 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
586 bitmap->description);
589 for (i=0; i < num; i++)
590 ext2fs_fast_set_bit(block + i - bitmap->start, bitmap->bitmap);
593 void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
594 blk_t block, int num)
598 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
599 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
600 bitmap->description);
603 for (i=0; i < num; i++)
604 ext2fs_fast_clear_bit(block + i - bitmap->start,