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 Public
24 #include <sys/types.h>
30 struct ext2fs_struct_generic_bitmap {
37 errcode_t base_error_code;
42 * Used by previously inlined function, so we have to export this and
43 * not change the function signature
45 void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap,
46 int code, unsigned long arg)
49 if (bitmap->description)
50 com_err(0, bitmap->base_error_code+code,
51 "#%lu for %s", arg, bitmap->description);
53 com_err(0, bitmap->base_error_code + code, "#%lu", arg);
57 static errcode_t check_magic(ext2fs_generic_bitmap bitmap)
59 if (!bitmap || !((bitmap->magic == EXT2_ET_MAGIC_GENERIC_BITMAP) ||
60 (bitmap->magic == EXT2_ET_MAGIC_INODE_BITMAP) ||
61 (bitmap->magic == EXT2_ET_MAGIC_BLOCK_BITMAP)))
62 return EXT2_ET_MAGIC_GENERIC_BITMAP;
66 errcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs,
67 __u32 start, __u32 end, __u32 real_end,
68 const char *descr, char *init_map,
69 ext2fs_generic_bitmap *ret)
71 ext2fs_generic_bitmap bitmap;
75 retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap),
80 bitmap->magic = magic;
82 bitmap->start = start;
84 bitmap->real_end = real_end;
86 case EXT2_ET_MAGIC_INODE_BITMAP:
87 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
89 case EXT2_ET_MAGIC_BLOCK_BITMAP:
90 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
93 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
96 retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
98 ext2fs_free_mem(&bitmap);
101 strcpy(bitmap->description, descr);
103 bitmap->description = 0;
105 size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
106 /* Round up to allow for the BT x86 instruction */
107 size = (size + 7) & ~3;
108 retval = ext2fs_get_mem(size, &bitmap->bitmap);
110 ext2fs_free_mem(&bitmap->description);
111 ext2fs_free_mem(&bitmap);
116 memcpy(bitmap->bitmap, init_map, size);
118 memset(bitmap->bitmap, 0, size);
123 errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
127 ext2fs_generic_bitmap *ret)
129 return ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_GENERIC_BITMAP, 0,
130 start, end, real_end, descr, 0, ret);
133 errcode_t ext2fs_copy_generic_bitmap(ext2fs_generic_bitmap src,
134 ext2fs_generic_bitmap *dest)
136 return (ext2fs_make_generic_bitmap(src->magic, src->fs,
137 src->start, src->end,
139 src->description, src->bitmap,
143 void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
145 if (check_magic(bitmap))
149 if (bitmap->description) {
150 ext2fs_free_mem(&bitmap->description);
151 bitmap->description = 0;
153 if (bitmap->bitmap) {
154 ext2fs_free_mem(&bitmap->bitmap);
157 ext2fs_free_mem(&bitmap);
160 int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
163 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
164 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
167 return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
170 int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
173 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
174 ext2fs_warn_bitmap2(bitmap, EXT2FS_MARK_ERROR, bitno);
177 return ext2fs_set_bit(bitno - bitmap->start, bitmap->bitmap);
180 int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
183 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
184 ext2fs_warn_bitmap2(bitmap, EXT2FS_UNMARK_ERROR, bitno);
187 return ext2fs_clear_bit(bitno - bitmap->start, bitmap->bitmap);
190 __u32 ext2fs_get_generic_bitmap_start(ext2fs_generic_bitmap bitmap)
192 return bitmap->start;
195 __u32 ext2fs_get_generic_bitmap_end(ext2fs_generic_bitmap bitmap)
200 void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap)
202 if (check_magic(bitmap))
205 memset(bitmap->bitmap, 0,
206 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
209 errcode_t ext2fs_fudge_generic_bitmap_end(ext2fs_inode_bitmap bitmap,
210 errcode_t magic, errcode_t neq,
211 ext2_ino_t end, ext2_ino_t *oend)
213 EXT2_CHECK_MAGIC(bitmap, magic);
215 if (end > bitmap->real_end)
223 errcode_t ext2fs_resize_generic_bitmap(errcode_t magic,
224 __u32 new_end, __u32 new_real_end,
225 ext2fs_generic_bitmap bmap)
228 size_t size, new_size;
231 if (!bmap || (bmap->magic != magic))
235 * If we're expanding the bitmap, make sure all of the new
236 * parts of the bitmap are zero.
238 if (new_end > bmap->end) {
239 bitno = bmap->real_end;
242 for (; bitno > bmap->end; bitno--)
243 ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
245 if (new_real_end == bmap->real_end) {
250 size = ((bmap->real_end - bmap->start) / 8) + 1;
251 new_size = ((new_real_end - bmap->start) / 8) + 1;
253 if (size != new_size) {
254 retval = ext2fs_resize_mem(size, new_size, &bmap->bitmap);
259 memset(bmap->bitmap + size, 0, new_size - size);
262 bmap->real_end = new_real_end;
266 errcode_t ext2fs_compare_generic_bitmap(errcode_t magic, errcode_t neq,
267 ext2fs_generic_bitmap bm1,
268 ext2fs_generic_bitmap bm2)
272 if (!bm1 || bm1->magic != magic)
274 if (!bm2 || bm2->magic != magic)
277 if ((bm1->start != bm2->start) ||
278 (bm1->end != bm2->end) ||
279 (memcmp(bm1->bitmap, bm2->bitmap,
280 (size_t) (bm1->end - bm1->start)/8)))
283 for (i = bm1->end - ((bm1->end - bm1->start) % 8); i <= bm1->end; i++)
284 if (ext2fs_fast_test_block_bitmap(bm1, i) !=
285 ext2fs_fast_test_block_bitmap(bm2, i))
291 void ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map)
295 /* Protect loop from wrap-around if map->real_end is maxed */
296 for (i=map->end+1, j = i - map->start;
297 i <= map->real_end && i > map->end;
299 ext2fs_set_bit(j, map->bitmap);
302 errcode_t ext2fs_get_generic_bitmap_range(ext2fs_generic_bitmap bmap,
304 __u32 start, __u32 num,
307 if (!bmap || (bmap->magic != magic))
310 if ((start < bmap->start) || (start+num-1 > bmap->real_end))
311 return EXT2_ET_INVALID_ARGUMENT;
313 memcpy(out, bmap->bitmap + (start >> 3), (num+7) >> 3);
317 errcode_t ext2fs_set_generic_bitmap_range(ext2fs_generic_bitmap bmap,
319 __u32 start, __u32 num,
322 if (!bmap || (bmap->magic != magic))
325 if ((start < bmap->start) || (start+num-1 > bmap->real_end))
326 return EXT2_ET_INVALID_ARGUMENT;
328 memcpy(bmap->bitmap + (start >> 3), in, (num+7) >> 3);
333 * Compare @mem to zero buffer by 256 bytes.
334 * Return 1 if @mem is zeroed memory, otherwise return 0.
336 static int mem_is_zero(const char *mem, size_t len)
338 static const char zero_buf[256];
340 while (len >= sizeof(zero_buf)) {
341 if (memcmp(mem, zero_buf, sizeof(zero_buf)))
343 len -= sizeof(zero_buf);
344 mem += sizeof(zero_buf);
346 /* Deal with leftover bytes. */
348 return !memcmp(mem, zero_buf, len);
353 * Return true if all of the bits in a specified range are clear
355 static int ext2fs_test_clear_generic_bitmap_range(ext2fs_generic_bitmap bitmap,
359 size_t start_byte, len_byte = len >> 3;
360 unsigned int start_bit, len_bit = len % 8;
366 const char *ADDR = bitmap->bitmap;
368 start -= bitmap->start;
369 start_byte = start >> 3;
370 start_bit = start % 8;
372 if (start_bit != 0) {
374 * The compared start block number or start inode number
375 * is not the first bit in a byte.
377 mark_count = 8 - start_bit;
378 if (len < 8 - start_bit) {
379 mark_count = (int)len;
380 mark_bit = len + start_bit - 1;
384 for (i = mark_count; i > 0; i--, mark_bit--)
385 first_bit |= 1 << mark_bit;
388 * Compare blocks or inodes in the first byte.
389 * If there is any marked bit, this function returns 0.
391 if (first_bit & ADDR[start_byte])
393 else if (len <= 8 - start_bit)
397 len_bit = (len - mark_count) % 8;
398 len_byte = (len - mark_count) >> 3;
402 * The compared start block number or start inode number is
403 * the first bit in a byte.
407 * The compared end block number or end inode number is
408 * not the last bit in a byte.
410 for (mark_bit = len_bit - 1; mark_bit >= 0; mark_bit--)
411 last_bit |= 1 << mark_bit;
414 * Compare blocks or inodes in the last byte.
415 * If there is any marked bit, this function returns 0.
417 if (last_bit & ADDR[start_byte + len_byte])
419 else if (len_byte == 0)
423 /* Check whether all bytes are 0 */
424 return mem_is_zero(ADDR + start_byte, len_byte);
427 int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
428 blk_t block, int num)
430 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
431 if ((block < bitmap->start) || (block+num-1 > bitmap->real_end)) {
432 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
433 block, bitmap->description);
436 return ext2fs_test_clear_generic_bitmap_range((ext2fs_generic_bitmap)
440 int ext2fs_test_inode_bitmap_range(ext2fs_inode_bitmap bitmap,
441 ino_t inode, int num)
443 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
444 if ((inode < bitmap->start) || (inode+num-1 > bitmap->real_end)) {
445 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_TEST,
446 inode, bitmap->description);
449 return ext2fs_test_clear_generic_bitmap_range((ext2fs_generic_bitmap)
453 void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
454 blk_t block, int num)
458 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
459 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
460 bitmap->description);
463 for (i=0; i < num; i++)
464 ext2fs_fast_set_bit(block + i - bitmap->start, bitmap->bitmap);
467 void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
468 blk_t block, int num)
472 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
473 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
474 bitmap->description);
477 for (i=0; i < num; i++)
478 ext2fs_fast_clear_bit(block + i - bitmap->start,