lloff_t Ext2Partition::extent_binarysearch(EXT4_EXTENT_HEADER *header, lloff_t lbn, bool isallocated) { EXT4_EXTENT *extent; EXT4_EXTENT_IDX *index; EXT4_EXTENT_HEADER *child; lloff_t physical_block = 0; lloff_t block; if(header->eh_magic != EXT4_EXT_MAGIC) { LOG("Invalid magic in Extent Header: %X\n", header->eh_magic); return 0; } extent = EXT_FIRST_EXTENT(header); // LOG("HEADER: magic %x Entries: %d depth %d\n", header->eh_magic, header->eh_entries, header->eh_depth); if(header->eh_depth == 0) { for(int i = 0; i < header->eh_entries; i++) { // LOG("EXTENT: Block: %d Length: %d LBN: %d\n", extent->ee_block, extent->ee_len, lbn); if((lbn >= extent->ee_block) && (lbn < (extent->ee_block + extent->ee_len))) { physical_block = ext_to_block(extent) + lbn; physical_block = physical_block - (lloff_t)extent->ee_block; if(isallocated) delete [] header; // LOG("Physical Block: %d\n", physical_block); return physical_block; } extent++; // Pointer increment by size of Extent. } return 0; } index = EXT_FIRST_INDEX(header); for(int i = 0; i < header->eh_entries; i++) { // LOG("INDEX: Block: %d Leaf: %d \n", index->ei_block, index->ei_leaf_lo); if((i == (header->eh_entries - 1)) || (lbn < (index + 1)->ei_block)) { child = (EXT4_EXTENT_HEADER *) new char [blocksize]; block = idx_to_block(index); ext2_readblock(block, (void *) child); return extent_binarysearch(child, lbn, true); } index++; } // We reach here if we do not find the key if(isallocated) delete [] header; return physical_block; }
uint64_t Partition::ExtentBinarySearch(EXT4_EXTENT_HEADER *header, uint64_t lbn, bool isallocated) { EXT4_EXTENT *extent; EXT4_EXTENT_IDX *index; EXT4_EXTENT_HEADER *child; uint64_t physical_block = 0; uint64_t block; if(header->eh_magic != EXT4_EXT_MAGIC) { LOG("Invalid magic in Extent Header: %X\n", header->eh_magic); return 0; } extent = EXT_FIRST_EXTENT(header); if(header->eh_depth == 0) { for(int i = 0; i < header->eh_entries; i++) { if((lbn >= extent->ee_block) && (lbn < (extent->ee_block + extent->ee_len))) { physical_block = ext_to_block(extent) + lbn; physical_block = physical_block - (uint64_t)extent->ee_block; if(isallocated) delete [] header; return physical_block; } extent++; } return 0; } index = EXT_FIRST_INDEX(header); for(int i = 0; i < header->eh_entries; i++) { if(lbn >= index->ei_block) { child = (EXT4_EXTENT_HEADER *) new char [blocksize]; block = idx_to_block(index); ReadBlock(block, (void *) child); return ExtentBinarySearch(child, lbn, true); } } if(isallocated) delete [] header; return physical_block; }