Uint64 base; // Block's Base Address
int block = 0, ofs = 0;
Uint size;
- void *blockData;
int bestMatch = -1;
int bestSize=0, bestBlock=0, bestOfs=0, bestNeedsSplit=0;
int nEntries;
ENTER("pNode sName pChild",
Node, Name, Child);
- blockData = malloc(disk->BlockSize);
+ void *blockData = malloc(disk->BlockSize);
// Read child inode (get's the file type)
Ext2_int_ReadInode(disk, Child->Inode, &inode);
LOG(" name='%.*s'", dirent->name_len, dirent->name);
if(strncmp(Name, dirent->name, dirent->name_len) == 0) {
//Ext2_int_UnlockInode(disk, Node->Inode);
- Mutex_Release(&Node->Lock);
- LEAVE('i', 1);
- return 1; // ERR_???
+ goto _err;
}
int spare_space = dirent->rec_len - (dirent->name_len + EXT2_DIRENT_SIZE);
Child->Flags |= VFS_FFLAG_DIRTY;
//Ext2_int_UnlockInode(disk, Node->Inode);
+ free(blockData);
Mutex_Release(&Node->Lock);
LEAVE('i', 0);
return 0;
+_err:
+ free(blockData);
+ Mutex_Release(&Node->Lock);
+ LEAVE('i', 1);
+ return 1; // ERR_???
}
\r
int Ext2_int_WritebackNode(tExt2_Disk *Disk, tVFS_Node *Node)\r
{\r
- tExt2_Inode inode;\r
+ tExt2_Inode inode = {0};\r
\r
if( Disk != Node->ImplPtr ) {\r
Log_Error("Ext2", "Ext2_int_WritebackNode - Disk != Node->ImplPtr");\r
VFS_ReadAt(Disk->FD, Disk->BlockSize*bg->bg_inode_bitmap+ofs, sector_size, buf);\r
\r
int byte, bit;\r
- for( byte = 0; byte < sector_size && buf[byte] != 0xFF; byte ++ )\r
+ for( byte = 0; byte < sector_size && buf[byte] == 0xFF; byte ++ )\r
;\r
if( byte < sector_size )\r
{\r
// Update Primary\r
VFS_WriteAt(Disk->FD, 1024, 1024, &Disk->SuperBlock);\r
\r
+ // - Update block groups while we're at it\r
+ VFS_WriteAt(\r
+ Disk->FD,\r
+ Disk->SuperBlock.s_first_data_block * Disk->BlockSize + 1024,\r
+ sizeof(tExt2_Group)*Disk->GroupCount,\r
+ Disk->Groups\r
+ );\r
+ \r
// Secondaries\r
// at Block Group 1, 3^n, 5^n, 7^n\r
\r
Uint64 allocSize;
int bNewBlocks = 0;
- Debug_HexDump("Ext2_Write", Buffer, Length);
+ //Debug_HexDump("Ext2_Write", Buffer, Length);
// TODO: Handle (Flags & VFS_IOFLAG_NOBLOCK)
if(Disk->SuperBlock.s_free_blocks_count == 0)
return 0;
- // First: Check the next block after \a PrevBlock
- if( (PrevBlock + 1) % Disk->SuperBlock.s_blocks_per_group != 0
- && Disk->Groups[blockgroup].bg_free_blocks_count > 0 )
+ // First: Check the next block after `PrevBlock`
+ int iblock = (PrevBlock + 1) % Disk->SuperBlock.s_blocks_per_group;
+ if( iblock != 0 && Disk->Groups[blockgroup].bg_free_blocks_count > 0 )
{
+ LOG("Checking %i:%i", blockgroup, iblock);
+
bg = &Disk->Groups[blockgroup];
+
const int sector_size = 512;
Uint8 buf[sector_size];
- int iblock = (PrevBlock + 1) % Disk->SuperBlock.s_blocks_per_group;
- int byte = iblock / 8;
- int ofs = byte / sector_size * sector_size;
+ int byte = (iblock/8) % sector_size;
+ Uint8 bit = 1 << (iblock % 8);
+ int ofs = (iblock/8) / sector_size * sector_size;
byte %= sector_size;
- VFS_ReadAt(Disk->FD, Disk->BlockSize*bg->bg_block_bitmap+ofs, sector_size, buf);
-
- if( (buf[byte] & (1 << (iblock%8))) == 0 )
+ Uint64 vol_ofs = Disk->BlockSize*bg->bg_block_bitmap+ofs;
+ VFS_ReadAt(Disk->FD, vol_ofs, sector_size, buf);
+
+ LOG("buf@%llx[%i] = %02x (& %02x)", vol_ofs, byte, buf[byte], bit);
+
+ if( (buf[byte] & bit) == 0 )
{
// Free block - nice and contig allocation
- buf[byte] |= (1 << (iblock%8));
- VFS_WriteAt(Disk->FD, Disk->BlockSize*bg->bg_block_bitmap+ofs, sector_size, buf);
+ buf[byte] |= bit;
+ VFS_WriteAt(Disk->FD, vol_ofs, sector_size, buf);
bg->bg_free_blocks_count --;
Disk->SuperBlock.s_free_blocks_count --;
blockgroup ++;
if( Disk->Groups[blockgroup].bg_free_blocks_count == 0 )
{
+ LOG("Roll over");
blockgroup = 0;
while( blockgroup < firstgroup && Disk->Groups[blockgroup].bg_free_blocks_count == 0 )
blockgroup ++;
Disk);
return 0;
}
+ LOG("BG%i has free blocks", blockgroup);
// Search the bitmap for a free block
bg = &Disk->Groups[blockgroup];
do {
const int sector_size = 512;
Uint8 buf[sector_size];
- VFS_ReadAt(Disk->FD, Disk->BlockSize*bg->bg_block_bitmap+ofs, sector_size, buf);
+ Uint64 vol_ofs = Disk->BlockSize*bg->bg_block_bitmap+ofs;
+ VFS_ReadAt(Disk->FD, vol_ofs, sector_size, buf);
int byte, bit;
- for( byte = 0; byte < sector_size && buf[byte] != 0xFF; byte ++ )
+ for( byte = 0; byte < sector_size && buf[byte] == 0xFF; byte ++ )
;
if( byte < sector_size )
{
+ LOG("buf@%llx[%i] = %02x", vol_ofs, byte, buf[byte]);
for( bit = 0; bit < 8 && buf[byte] & (1 << bit); bit ++)
;
ASSERT(bit != 8);
buf[byte] |= 1 << bit;
- VFS_WriteAt(Disk->FD, Disk->BlockSize*bg->bg_block_bitmap+ofs, sector_size, buf);
+ VFS_WriteAt(Disk->FD, vol_ofs, sector_size, buf);
bg->bg_free_blocks_count --;
Disk->SuperBlock.s_free_blocks_count --;