void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map) { __u32 i, j; for (i=map->end+1, j = i - map->start; i <= map->real_end; i++, j++) ext2fs_set_bit(j, map->bitmap); return; }
int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap, __u32 bitno) { if ((bitno < bitmap->start) || (bitno > bitmap->end)) { ext2fs_warn_bitmap2(bitmap, EXT2FS_MARK_ERROR, bitno); return 0; } return ext2fs_set_bit(bitno - bitmap->start, bitmap->bitmap); }
void ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map) { __u32 i, j; /* Protect loop from wrap-around if map->real_end is maxed */ for (i=map->end+1, j = i - map->start; i <= map->real_end && i > map->end; i++, j++) ext2fs_set_bit(j, map->bitmap); }
errcode_t ext2fs_write_block_bitmap (ext2_filsys fs) { dgrp_t i; unsigned int j; int nbytes; unsigned int nbits; errcode_t retval; char * block_bitmap = fs->block_map->bitmap; char * bitmap_block = NULL; blk_t blk; EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); if (!(fs->flags & EXT2_FLAG_RW)) return EXT2_ET_RO_FILSYS; if (!block_bitmap) return 0; nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; bitmap_block = memalign(getpagesize(), fs->blocksize); memset(bitmap_block, 0xff, fs->blocksize); retval = io_channel_reopen(fs->io, O_RDWR | O_DIRECT); if (retval) return retval; for (i = 0; i < fs->group_desc_count; i++) { memcpy(bitmap_block, block_bitmap, nbytes); if (i == fs->group_desc_count - 1) { /* Force bitmap padding for the last group */ nbits = ((fs->super->s_blocks_count - fs->super->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(fs->super)); if (nbits) for (j = nbits; j < fs->blocksize * 8; j++) ext2fs_set_bit(j, bitmap_block); } blk = fs->group_desc[i].bg_block_bitmap; if (blk) { #ifdef EXT2_BIG_ENDIAN_BITMAPS if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) || (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))) ext2fs_swap_bitmap(fs, bitmap_block, nbytes); #endif retval = io_channel_write_blk(fs->io, blk, 1, bitmap_block); if (retval) return EXT2_ET_BLOCK_BITMAP_WRITE; } block_bitmap += nbytes; } io_channel_reopen(fs->io, O_RDWR); fs->flags &= ~EXT2_FLAG_BB_DIRTY; free(bitmap_block); return 0; }
int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap, __u32 bitno) { if (!EXT2FS_IS_32_BITMAP(bitmap)) { if (EXT2FS_IS_64_BITMAP(bitmap)) { ext2fs_warn_bitmap32(bitmap, __func__); return ext2fs_mark_generic_bmap(bitmap, bitno); } #ifndef OMIT_COM_ERR com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP, "mark_bitmap(%lu)", (unsigned long) bitno); #endif return 0; } if ((bitno < bitmap->start) || (bitno > bitmap->end)) { ext2fs_warn_bitmap2(bitmap, EXT2FS_MARK_ERROR, bitno); return 0; } return ext2fs_set_bit(bitno - bitmap->start, bitmap->bitmap); }
static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) { dgrp_t i; unsigned int j; int block_nbytes, inode_nbytes; unsigned int nbits; errcode_t retval; char *block_buf = NULL, *inode_buf = NULL; int csum_flag = 0; blk64_t blk; blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block); ext2_ino_t ino_itr = 1; EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); if (!(fs->flags & EXT2_FLAG_RW)) return EXT2_ET_RO_FILSYS; if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) csum_flag = 1; inode_nbytes = block_nbytes = 0; if (do_block) { block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8; retval = io_channel_alloc_buf(fs->io, 0, &block_buf); if (retval) goto errout; memset(block_buf, 0xff, fs->blocksize); } if (do_inode) { inode_nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8); retval = io_channel_alloc_buf(fs->io, 0, &inode_buf); if (retval) goto errout; memset(inode_buf, 0xff, fs->blocksize); } for (i = 0; i < fs->group_desc_count; i++) { if (!do_block) goto skip_block_bitmap; if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) ) goto skip_this_block_bitmap; retval = ext2fs_get_block_bitmap_range2(fs->block_map, blk_itr, block_nbytes << 3, block_buf); if (retval) goto errout; if (i == fs->group_desc_count - 1) { /* Force bitmap padding for the last group */ nbits = EXT2FS_NUM_B2C(fs, ((ext2fs_blocks_count(fs->super) - (__u64) fs->super->s_first_data_block) % (__u64) EXT2_BLOCKS_PER_GROUP(fs->super))); if (nbits) for (j = nbits; j < fs->blocksize * 8; j++) ext2fs_set_bit(j, block_buf); } blk = ext2fs_block_bitmap_loc(fs, i); if (blk) { retval = io_channel_write_blk64(fs->io, blk, 1, block_buf); if (retval) { retval = EXT2_ET_BLOCK_BITMAP_WRITE; goto errout; } } skip_this_block_bitmap: blk_itr += block_nbytes << 3; skip_block_bitmap: if (!do_inode) continue; if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) ) goto skip_this_inode_bitmap; retval = ext2fs_get_inode_bitmap_range2(fs->inode_map, ino_itr, inode_nbytes << 3, inode_buf); if (retval) goto errout; blk = ext2fs_inode_bitmap_loc(fs, i); if (blk) { retval = io_channel_write_blk64(fs->io, blk, 1, inode_buf); if (retval) { retval = EXT2_ET_INODE_BITMAP_WRITE; goto errout; } } skip_this_inode_bitmap: ino_itr += inode_nbytes << 3; } if (do_block) { fs->flags &= ~EXT2_FLAG_BB_DIRTY; ext2fs_free_mem(&block_buf); } if (do_inode) { fs->flags &= ~EXT2_FLAG_IB_DIRTY; ext2fs_free_mem(&inode_buf); } return 0; errout: if (inode_buf) ext2fs_free_mem(&inode_buf); if (block_buf) ext2fs_free_mem(&block_buf); return retval; }
main(int argc, char **argv) { int i, j, size; unsigned char testarray[12]; unsigned char *bigarray; size = sizeof(bitarray)*8; #if 0 i = ext2fs_find_first_bit_set(bitarray, size); while (i < size) { printf("Bit set: %d\n", i); i = ext2fs_find_next_bit_set(bitarray, size, i+1); } #endif /* Test test_bit */ for (i=0,j=0; i < size; i++) { if (ext2fs_test_bit(i, bitarray)) { if (bits_list[j] == i) { j++; } else { printf("Bit %d set, not expected\n", i); exit(1); } } else { if (bits_list[j] == i) { printf("Expected bit %d to be clear.\n", i); exit(1); } } } printf("ext2fs_test_bit appears to be correct\n"); /* Test ext2fs_set_bit */ memset(testarray, 0, sizeof(testarray)); for (i=0; bits_list[i] > 0; i++) { ext2fs_set_bit(bits_list[i], testarray); } if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) { printf("ext2fs_set_bit test succeeded.\n"); } else { printf("ext2fs_set_bit test failed.\n"); for (i=0; i < sizeof(testarray); i++) { printf("%02x ", testarray[i]); } printf("\n"); exit(1); } for (i=0; bits_list[i] > 0; i++) { ext2fs_clear_bit(bits_list[i], testarray); } for (i=0; i < sizeof(testarray); i++) { if (testarray[i]) { printf("ext2fs_clear_bit failed, " "testarray[%d] is %d\n", i, testarray[i]); exit(1); } } printf("ext2fs_clear_bit test succeed.\n"); /* Do bigarray test */ bigarray = malloc(1 << 29); if (!bigarray) { fprintf(stderr, "Failed to allocate scratch memory!\n"); exit(1); } bigarray[BIG_TEST_BIT >> 3] = 0; ext2fs_set_bit(BIG_TEST_BIT, bigarray); printf("big bit number (%u) test: %d, expected %d\n", BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3], (1 << (BIG_TEST_BIT & 7))); if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7))) exit(1); ext2fs_clear_bit(BIG_TEST_BIT, bigarray); printf("big bit number (%u) test: %d, expected 0\n", BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3], 0); if (bigarray[BIG_TEST_BIT >> 3] != 0) exit(1); printf("ext2fs_set_bit big_test successful\n"); /* Now test ext2fs_fast_set_bit */ memset(testarray, 0, sizeof(testarray)); for (i=0; bits_list[i] > 0; i++) { ext2fs_fast_set_bit(bits_list[i], testarray); } if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) { printf("ext2fs_fast_set_bit test succeeded.\n"); } else { printf("ext2fs_fast_set_bit test failed.\n"); for (i=0; i < sizeof(testarray); i++) { printf("%02x ", testarray[i]); } printf("\n"); exit(1); } for (i=0; bits_list[i] > 0; i++) { ext2fs_clear_bit(bits_list[i], testarray); } for (i=0; i < sizeof(testarray); i++) { if (testarray[i]) { printf("ext2fs_clear_bit failed, " "testarray[%d] is %d\n", i, testarray[i]); exit(1); } } printf("ext2fs_clear_bit test succeed.\n"); bigarray[BIG_TEST_BIT >> 3] = 0; ext2fs_fast_set_bit(BIG_TEST_BIT, bigarray); printf("big bit number (%u) test: %d, expected %d\n", BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3], (1 << (BIG_TEST_BIT & 7))); if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7))) exit(1); ext2fs_fast_clear_bit(BIG_TEST_BIT, bigarray); printf("big bit number (%u) test: %d, expected 0\n", BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3], 0); if (bigarray[BIG_TEST_BIT >> 3] != 0) exit(1); printf("ext2fs_fast_set_bit big_test successful\n"); exit(0); }
static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) { dgrp_t i; unsigned int j; int block_nbytes, inode_nbytes; unsigned int nbits; errcode_t retval; char *block_bitmap, *inode_bitmap; char *block_buf, *inode_buf; int lazy_flag = 0; blk_t blk; EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); if (!(fs->flags & EXT2_FLAG_RW)) return EXT2_ET_RO_FILSYS; if (EXT2_HAS_COMPAT_FEATURE(fs->super, EXT2_FEATURE_COMPAT_LAZY_BG)) lazy_flag = 1; inode_nbytes = block_nbytes = 0; block_bitmap = inode_bitmap = 0; if (do_block) { block_bitmap = fs->block_map->bitmap; block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; retval = ext2fs_get_mem(fs->blocksize, &block_buf); if (retval) return retval; memset(block_buf, 0xff, fs->blocksize); } if (do_inode) { inode_bitmap = fs->inode_map->bitmap; inode_nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8); retval = ext2fs_get_mem(fs->blocksize, &inode_buf); if (retval) return retval; memset(inode_buf, 0xff, fs->blocksize); } for (i = 0; i < fs->group_desc_count; i++) { if (!block_bitmap || !do_block) goto skip_block_bitmap; if (lazy_flag && fs->group_desc[i].bg_flags & EXT2_BG_BLOCK_UNINIT) goto skip_this_block_bitmap; memcpy(block_buf, block_bitmap, block_nbytes); if (i == fs->group_desc_count - 1) { /* Force bitmap padding for the last group */ nbits = ((fs->super->s_blocks_count - fs->super->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(fs->super)); if (nbits) for (j = nbits; j < fs->blocksize * 8; j++) ext2fs_set_bit(j, block_buf); } blk = fs->group_desc[i].bg_block_bitmap; if (blk) { #ifdef EXT2_BIG_ENDIAN_BITMAPS if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) || (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))) ext2fs_swap_bitmap(fs, block_buf, block_nbytes); #endif retval = io_channel_write_blk(fs->io, blk, 1, block_buf); if (retval) return EXT2_ET_BLOCK_BITMAP_WRITE; } skip_this_block_bitmap: block_bitmap += block_nbytes; skip_block_bitmap: if (!inode_bitmap || !do_inode) continue; if (lazy_flag && fs->group_desc[i].bg_flags & EXT2_BG_INODE_UNINIT) goto skip_this_inode_bitmap; memcpy(inode_buf, inode_bitmap, inode_nbytes); blk = fs->group_desc[i].bg_inode_bitmap; if (blk) { #ifdef EXT2_BIG_ENDIAN_BITMAPS if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) || (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))) ext2fs_swap_bitmap(fs, inode_buf, inode_nbytes); #endif retval = io_channel_write_blk(fs->io, blk, 1, inode_buf); if (retval) return EXT2_ET_INODE_BITMAP_WRITE; } skip_this_inode_bitmap: inode_bitmap += inode_nbytes; } if (do_block) { fs->flags &= ~EXT2_FLAG_BB_DIRTY; ext2fs_free_mem(&block_buf); } if (do_inode) { fs->flags &= ~EXT2_FLAG_IB_DIRTY; ext2fs_free_mem(&inode_buf); } return 0; }