void read_bitmap(char* device, file_system_info fs_info, unsigned long* bitmap, int pui) { off_t a = 0, b = 0; off_t block = 0;; int start = 0; int bit_size = 1; pc_init_bitmap(bitmap, 0x00, fs_info.totalblock); fs_open(device); /// init progress progress_bar prog; /// progress_bar structure defined in progress.h progress_init(&prog, start, fs_info.totalblock, fs_info.totalblock, BITMAP, bit_size); pc_init_bitmap(bitmap, 0x00, fs_info.totalblock); while (exfat_find_used_sectors(&ef, &a, &b) == 0) { printf("block %" PRId64 " %" PRId64 " \n", a, b); for (block = a; block <= b; block++) { pc_set_bit((uint64_t)block, bitmap); log_mesg(3, 0, 0, fs_opt.debug, "%s: used block %" PRId64 " \n", __FILE__, block); /// update progress update_pui(&prog, block, block, 0); } } fs_close(); /// update progress update_pui(&prog, 1, 1, 1); }
/// get bitmap from image file to restore data void load_image_bitmap(int* ret, cmd_opt opt, file_system_info fs_info, image_options img_opt, unsigned long* bitmap) { switch(img_opt.bitmap_mode) { case BM_BIT: load_image_bitmap_bits(ret, opt, fs_info, bitmap); break; case BM_BYTE: load_image_bitmap_bytes(ret, opt, fs_info, bitmap); break; case BM_NONE: // All blocks are present pc_init_bitmap(bitmap, 0xFF, fs_info.totalblock); break; default: log_mesg(0, 1, 1, opt.debug, "ERROR: unknown bitmap mode [%d]\n", img_opt.bitmap_mode); break; } }
// reference dumpe2fs void read_bitmap(char* device, file_system_info fs_info, unsigned long* bitmap, int pui) { errcode_t retval; unsigned long group; unsigned long long current_block, block; unsigned long long lfree, gfree; char *block_bitmap = NULL; int block_nbytes; unsigned long long blk_itr; int bg_flags = 0; int start = 0; int bit_size = 1; int B_UN_INIT = 0; int ext4_gfree_mismatch = 0; log_mesg(2, 0, 0, fs_opt.debug, "%s: read_bitmap %p\n", __FILE__, bitmap); fs_open(device); retval = ext2fs_read_bitmaps(fs); /// open extfs bitmap if (retval) log_mesg(0, 1, 1, fs_opt.debug, "%s: Couldn't find valid filesystem bitmap.\n", __FILE__); block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; if (fs->block_map) block_bitmap = malloc(block_nbytes); /// initial image bitmap as 1 (all block are used) pc_init_bitmap(bitmap, 0xFF, fs_info.totalblock); lfree = 0; current_block = 0; blk_itr = fs->super->s_first_data_block; /// init progress progress_bar prog; /// progress_bar structure defined in progress.h progress_init(&prog, start, fs_info.totalblock, fs_info.totalblock, BITMAP, bit_size); /// each group for (group = 0; group < fs->group_desc_count; group++) { gfree = 0; B_UN_INIT = 0; if (block_bitmap) { #ifdef EXTFS_1_41 ext2fs_get_block_bitmap_range(fs->block_map, blk_itr, block_nbytes << 3, block_bitmap); #else ext2fs_get_block_bitmap_range2(fs->block_map, blk_itr, block_nbytes << 3, block_bitmap); #endif if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM){ #ifdef EXTFS_1_41 bg_flags = fs->group_desc[group].bg_flags; #else bg_flags = ext2fs_bg_flags(fs, group); #endif if (bg_flags&EXT2_BG_BLOCK_UNINIT){ log_mesg(1, 0, 0, fs_opt.debug, "%s: BLOCK_UNINIT for group %lu\n", __FILE__, group); B_UN_INIT = 1; } else { log_mesg(2, 0, 0, fs_opt.debug, "%s: BLOCK_INIT for group %lu\n", __FILE__, group); } } /// each block in group for (block = 0; ((block < fs->super->s_blocks_per_group) && (current_block < (fs_info.totalblock-1))); block++) { current_block = block + blk_itr; if (in_use (block_bitmap, block)){ pc_set_bit(current_block, bitmap, fs_info.totalblock); log_mesg(3, 0, 0, fs_opt.debug, "%s: used block %llu at group %lu\n", __FILE__, current_block, group); } else { lfree++; gfree++; pc_clear_bit(current_block, bitmap, fs_info.totalblock); log_mesg(3, 0, 0, fs_opt.debug, "%s: free block %llu at group %lu init %i\n", __FILE__, current_block, group, (int)B_UN_INIT); } /// update progress update_pui(&prog, current_block, current_block, 0);//keep update } blk_itr += fs->super->s_blocks_per_group; } log_mesg(2, 0, 0, fs_opt.debug, "%s: free bitmap (gfree = %lli, bg_blocks_count = %lli)at %lu group.\n", __FILE__, gfree, ext2fs_bg_free_blocks_count(fs, group), group); /// check free blocks in group #ifdef EXTFS_1_41 if (gfree != fs->group_desc[group].bg_free_blocks_count){ #else if (gfree != ext2fs_bg_free_blocks_count(fs, group)){ #endif if (!B_UN_INIT) log_mesg(0, 1, 1, fs_opt.debug, "%s: bitmap error at %lu group.\n", __FILE__, group); else ext4_gfree_mismatch = 1; } } /// check all free blocks in partition if (lfree != ext2fs_free_blocks_count(fs->super)) { if ((fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && (ext4_gfree_mismatch)) log_mesg(1, 0, 0, fs_opt.debug, "%s: EXT4 bitmap metadata mismatch\n", __FILE__); else log_mesg(0, 1, 1, fs_opt.debug, "%s: bitmap free count err, partclone get free:%llu but extfs get %llu.\nPlease run fsck to check and repair the file system\n", __FILE__, lfree, ext2fs_free_blocks_count(fs->super)); } fs_close(); /// update progress update_pui(&prog, 1, 1, 1);//finish free(block_bitmap); } /// get extfs type static int test_extfs_type(char* device){ int ext2 = 1; int ext3 = 2; int ext4 = 3; int device_type; fs_open(device); if(fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM){ log_mesg(1, 0, 0, fs_opt.debug, "%s: test feature as EXT4\n", __FILE__); device_type = ext4; } else if (fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL){ log_mesg(1, 0, 0, fs_opt.debug, "%s: test feature as EXT3\n", __FILE__); device_type = ext3; } else { log_mesg(1, 0, 0, fs_opt.debug, "%s: test feature as EXT2\n", __FILE__); device_type = ext2; } fs_close(); return device_type; }
void read_bitmap(char* device, file_system_info fs_info, unsigned long* bitmap, int pui) { xfs_agnumber_t agno = 0; xfs_agblock_t first_agbno; xfs_agnumber_t num_ags; ag_header_t ag_hdr; xfs_daddr_t read_ag_off; int read_ag_length; void *read_ag_buf = NULL; xfs_off_t read_ag_position; /* xfs_types.h: typedef __s64 */ uint64_t sk, res; void *btree_buf_data = NULL; int btree_buf_length; xfs_off_t btree_buf_position; xfs_agblock_t bno; uint current_level; xfs_daddr_t begin, next_begin, ag_begin, new_begin, ag_end; /* xfs_types.h: typedef __s64*/ xfs_off_t pos; xfs_alloc_ptr_t *ptr; xfs_alloc_rec_t *rec_ptr; int length; int i; uint64_t size, sizeb; xfs_off_t w_position; int w_length; int wblocks; int w_size = 1 * 1024 * 1024; uint64_t numblocks = 0; xfs_off_t logstart, logend; xfs_off_t logstart_pos, logend_pos; int log_length; struct xfs_btree_block *block; uint64_t current_block, block_count, prog_cur_block = 0; int start = 0; int bit_size = 1; progress_bar prog; uint64_t bused = 0; uint64_t bfree = 0; /// init progress progress_init(&prog, start, fs_info.totalblock, fs_info.totalblock, BITMAP, bit_size); pc_init_bitmap(bitmap, 0x00, fs_info.totalblock); fs_open(device); first_agbno = (((XFS_AGFL_DADDR(mp) + 1) * source_sectorsize) + first_residue) / source_blocksize; num_ags = mp->m_sb.sb_agcount; log_mesg(1, 0, 0, fs_opt.debug, "ags = %i\n", num_ags); for (agno = 0; agno < num_ags ; agno++) { /* read in first blocks of the ag */ /* initial settings */ log_mesg(2, 0, 0, fs_opt.debug, "read ag %i header\n", agno); read_ag_off = XFS_AG_DADDR(mp, agno, XFS_SB_DADDR); read_ag_length = first_agbno * source_blocksize; read_ag_position = (xfs_off_t) read_ag_off * (xfs_off_t) BBSIZE; read_ag_buf = malloc(read_ag_length); if(read_ag_buf == NULL){ log_mesg(0, 1, 1, fs_opt.debug, "%s, %i, ERROR:%s", __func__, __LINE__, strerror(errno)); } memset(read_ag_buf, 0, read_ag_length); log_mesg(2, 0, 0, fs_opt.debug, "seek to read_ag_position %lli\n", read_ag_position); sk = lseek(source_fd, read_ag_position, SEEK_SET); current_block = (sk/source_blocksize); block_count = (read_ag_length/source_blocksize); set_bitmap(bitmap, sk, read_ag_length); log_mesg(2, 0, 0, fs_opt.debug, "read ag header fd = %llu(%i), length = %i(%i)\n", sk, current_block, read_ag_length, block_count); if ((res = read(source_fd, read_ag_buf, read_ag_length)) < 0) { log_mesg(1, 0, 1, fs_opt.debug, "read failure at offset %lld\n", read_ag_position); } ag_hdr.xfs_sb = (xfs_dsb_t *) (read_ag_buf); ASSERT(be32_to_cpu(ag_hdr.xfs_sb->sb_magicnum) == XFS_SB_MAGIC); ag_hdr.xfs_agf = (xfs_agf_t *) (read_ag_buf + source_sectorsize); ASSERT(be32_to_cpu(ag_hdr.xfs_agf->agf_magicnum) == XFS_AGF_MAGIC); ag_hdr.xfs_agi = (xfs_agi_t *) (read_ag_buf + 2 * source_sectorsize); ASSERT(be32_to_cpu(ag_hdr.xfs_agi->agi_magicnum) == XFS_AGI_MAGIC); ag_hdr.xfs_agfl = (xfs_agfl_t *) (read_ag_buf + 3 * source_sectorsize); log_mesg(2, 0, 0, fs_opt.debug, "ag header read ok\n"); /* save what we need (agf) in the btree buffer */ btree_buf_data = malloc(source_blocksize); if(btree_buf_data == NULL){ log_mesg(0, 1, 1, fs_opt.debug, "%s, %i, ERROR:%s", __func__, __LINE__, strerror(errno)); } memset(btree_buf_data, 0, source_blocksize); memmove(btree_buf_data, ag_hdr.xfs_agf, source_sectorsize); ag_hdr.xfs_agf = (xfs_agf_t *) btree_buf_data; btree_buf_length = source_blocksize; ///* traverse btree until we get to the leftmost leaf node */ bno = be32_to_cpu(ag_hdr.xfs_agf->agf_roots[XFS_BTNUM_BNOi]); current_level = 0; ag_end = XFS_AGB_TO_DADDR(mp, agno, be32_to_cpu(ag_hdr.xfs_agf->agf_length) - 1) + source_blocksize / BBSIZE; for (;;) { /* none of this touches the w_buf buffer */ current_level++; btree_buf_position = pos = (xfs_off_t)XFS_AGB_TO_DADDR(mp,agno,bno) << BBSHIFT; btree_buf_length = source_blocksize; sk = lseek(source_fd, btree_buf_position, SEEK_SET); current_block = (sk/source_blocksize); block_count = (btree_buf_length/source_blocksize); set_bitmap(bitmap, sk, btree_buf_length); log_mesg(2, 0, 0, fs_opt.debug, "read btree sf = %llu(%i), length = %i(%i)\n", sk, current_block, btree_buf_length, block_count); read(source_fd, btree_buf_data, btree_buf_length); block = (struct xfs_btree_block *)((char *)btree_buf_data + pos - btree_buf_position); if (be16_to_cpu(block->bb_level) == 0) break; ptr = XFS_ALLOC_PTR_ADDR(mp, block, 1, mp->m_alloc_mxr[1]); bno = be32_to_cpu(ptr[0]); } log_mesg(2, 0, 0, fs_opt.debug, "btree read done\n"); /* align first data copy but don't overwrite ag header */ pos = read_ag_position >> BBSHIFT; length = read_ag_length >> BBSHIFT; next_begin = pos + length; ag_begin = next_begin; ///* handle the rest of the ag */ for (;;) { if (be16_to_cpu(block->bb_level) != 0) { log_mesg(0, 1, 1, fs_opt.debug, "WARNING: source filesystem inconsistent.\nA leaf btree rec isn't a leaf. Aborting now.\n"); } rec_ptr = XFS_ALLOC_REC_ADDR(mp, block, 1); for (i = 0; i < be16_to_cpu(block->bb_numrecs); i++, rec_ptr++) { /* calculate in daddr's */ begin = next_begin; /* * protect against pathological case of a * hole right after the ag header in a * mis-aligned case */ if (begin < ag_begin) begin = ag_begin; /* * round size up to ensure we copy a * range bigger than required */ log_mesg(3, 0, 0, fs_opt.debug, "XFS_AGB_TO_DADDR = %llu, agno = %i, be32_to_cpu=%llu\n", XFS_AGB_TO_DADDR(mp, agno, be32_to_cpu(rec_ptr->ar_startblock)), agno, be32_to_cpu(rec_ptr->ar_startblock)); sizeb = XFS_AGB_TO_DADDR(mp, agno, be32_to_cpu(rec_ptr->ar_startblock)) - begin; size = roundup(sizeb <<BBSHIFT, source_sectorsize); log_mesg(3, 0, 0, fs_opt.debug, "BB = %i size %i and sizeb %llu brgin = %llu\n", BBSHIFT, size, sizeb, begin); if (size > 0) { /* copy extent */ log_mesg(2, 0, 0, fs_opt.debug, "copy extent\n"); w_position = (xfs_off_t)begin << BBSHIFT; while (size > 0) { /* * let lower layer do alignment */ if (size > w_size) { w_length = w_size; size -= w_size; sizeb -= wblocks; numblocks += wblocks; } else { w_length = size; numblocks += sizeb; size = 0; } //read_wbuf(source_fd, &w_buf, mp); sk = lseek(source_fd, w_position, SEEK_SET); current_block = (sk/source_blocksize); block_count = (w_length/source_blocksize); set_bitmap(bitmap, sk, w_length); log_mesg(2, 0, 0, fs_opt.debug, "read ext sourcefd to w_buf source_fd=%llu(%i), length=%i(%i)\n", sk, current_block, w_length, block_count); sk = lseek(source_fd, w_length, SEEK_CUR); w_position += w_length; } } /* round next starting point down */ new_begin = XFS_AGB_TO_DADDR(mp, agno, be32_to_cpu(rec_ptr->ar_startblock) + be32_to_cpu(rec_ptr->ar_blockcount)); next_begin = rounddown(new_begin, source_sectorsize >> BBSHIFT); } if (be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK){ log_mesg(2, 0, 0, fs_opt.debug, "NULLAGBLOCK\n"); break; } /* read in next btree record block */ btree_buf_position = pos = (xfs_off_t)XFS_AGB_TO_DADDR(mp, agno, be32_to_cpu(block->bb_u.s.bb_rightsib)) << BBSHIFT; btree_buf_length = source_blocksize; /* let read_wbuf handle alignment */ //read_wbuf(source_fd, &btree_buf, mp); sk = lseek(source_fd, btree_buf_position, SEEK_SET); current_block = (sk/source_blocksize); block_count = (btree_buf_length/source_blocksize); set_bitmap(bitmap, sk, btree_buf_length); log_mesg(2, 0, 0, fs_opt.debug, "read btreebuf fd = %llu(%i), length = %i(%i) \n", sk, current_block, btree_buf_length, block_count); read(source_fd, btree_buf_data, btree_buf_length); block = (struct xfs_btree_block *)((char *) btree_buf_data + pos - btree_buf_position); ASSERT(be32_to_cpu(block->bb_magic) == XFS_ABTB_MAGIC); } /* * write out range of used blocks after last range * of free blocks in AG */ if (next_begin < ag_end) { begin = next_begin; sizeb = ag_end - begin; size = roundup(sizeb << BBSHIFT, source_sectorsize); if (size > 0) { /* copy extent */ w_position = (xfs_off_t) begin << BBSHIFT; while (size > 0) { /* * let lower layer do alignment */ if (size > w_size) { w_length = w_size; size -= w_size; sizeb -= wblocks; numblocks += wblocks; } else { w_length = size; numblocks += sizeb; size = 0; } sk = lseek(source_fd, w_position, SEEK_SET); current_block = (sk/source_blocksize); block_count = (w_length/source_blocksize); set_bitmap(bitmap, sk, w_length); log_mesg(2, 0, 0, fs_opt.debug, "read ext fd = %llu(%i), length = %i(%i)\n", sk, current_block, w_length, block_count); //read_wbuf(source_fd, &w_buf, mp); lseek(source_fd, w_length, SEEK_CUR); w_position += w_length; } } } log_mesg(2, 0, 0, fs_opt.debug, "write a clean log\n"); log_length = 1 * 1024 * 1024; logstart = XFS_FSB_TO_DADDR(mp, mp->m_sb.sb_logstart) << BBSHIFT; logstart_pos = rounddown(logstart, (xfs_off_t)log_length); if (logstart % log_length) { /* unaligned */ sk = lseek(source_fd, logstart_pos, SEEK_SET); current_block = (sk/source_blocksize); block_count = (log_length/source_blocksize); set_bitmap(bitmap, sk, log_length); log_mesg(2, 0, 0, fs_opt.debug, "read log start from %llu(%i) %i(%i)\n", sk, current_block, log_length, block_count); sk = lseek(source_fd, log_length, SEEK_CUR); } logend = XFS_FSB_TO_DADDR(mp, mp->m_sb.sb_logstart) << BBSHIFT; logend += XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks); logend_pos = rounddown(logend, (xfs_off_t)log_length); if (logend % log_length) { /* unaligned */ sk = lseek(source_fd, logend_pos, SEEK_SET); current_block = (sk/source_blocksize); block_count = (log_length/source_blocksize); set_bitmap(bitmap, sk, log_length); log_mesg(2, 0, 0, fs_opt.debug, "read log end from %llu(%i) %i(%i)\n", sk, current_block, log_length, block_count); sk = lseek(source_fd, log_length, SEEK_CUR); } log_mesg(2, 0, 0, fs_opt.debug, "write a clean log done\n"); prog_cur_block = fs_info.totalblock/num_ags*(agno+1)-1; update_pui(&prog, prog_cur_block, prog_cur_block, 0); } for(current_block = 0; current_block <= fs_info.totalblock; current_block++){ if(pc_test_bit(current_block, bitmap)) bused++; else bfree++; } log_mesg(0, 0, 0, fs_opt.debug, "bused = %lli, bfree = %lli\n", bused, bfree); fs_close(); update_pui(&prog, 1, 1, 1); }