X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fvfs%2Ffs%2Fext2.c;h=da941f723fb328507a1eb0bca551e4a7bfbbcb50;hb=1ca0233bb1e142c536d09c35ce8dcdb209a2938b;hp=1da9c0f89f98f30c30c0de7bffc52b03c91b04b4;hpb=8f14093202765d2d363584da819b68ec8c11018c;p=tpg%2Facess2.git diff --git a/Kernel/vfs/fs/ext2.c b/Kernel/vfs/fs/ext2.c index 1da9c0f8..da941f72 100644 --- a/Kernel/vfs/fs/ext2.c +++ b/Kernel/vfs/fs/ext2.c @@ -201,9 +201,22 @@ Uint64 Ext2_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) // Get Inode Ext2_int_GetInode(Node, &inode); + // Sanity Checks + if(Offset >= inode.i_size) { + LEAVE('i', 0); + return 0; + } + if(Offset + Length > inode.i_size) + Length = inode.i_size - Offset; + block = Offset / disk->BlockSize; Offset = Offset / disk->BlockSize; base = Ext2_int_GetBlockAddr(disk, inode.i_block, block); + if(base == 0) { + Warning("[EXT2 ] NULL Block Detected in INode 0x%llx\n", Node->Inode); + LEAVE('i', 0); + return 0; + } // Read only block if(Length <= disk->BlockSize - Offset) @@ -224,6 +237,11 @@ Uint64 Ext2_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) while(remLen > disk->BlockSize) { base = Ext2_int_GetBlockAddr(disk, inode.i_block, block); + if(base == 0) { + Warning("[EXT2 ] NULL Block Detected in INode 0x%llx\n", Node->Inode); + LEAVE('i', 0); + return 0; + } VFS_ReadAt( disk->FD, base, disk->BlockSize, Buffer); Buffer += disk->BlockSize; remLen -= disk->BlockSize; @@ -252,18 +270,25 @@ Uint64 Ext2_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) Uint64 allocSize; int bNewBlocks = 0; + Debug_HexDump("Ext2_Write", Buffer, Length); + Ext2_int_GetInode(Node, &inode); - // Round size up to block size - // block size is a power of two, so this will work + // Get the ammount of space already allocated + // - Round size up to block size + // - block size is a power of two, so this will work allocSize = (inode.i_size + disk->BlockSize) & ~(disk->BlockSize-1); + + // Are we writing to inside the allocated space? if( Offset < allocSize ) { + // Will we go out of it? if(Offset + Length > allocSize) { bNewBlocks = 1; retLen = allocSize - Offset; } else retLen = Length; + // Within the allocated space block = Offset / disk->BlockSize; Offset %= disk->BlockSize; @@ -272,7 +297,7 @@ Uint64 Ext2_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) // Write only block (if only one) if(Offset + retLen <= disk->BlockSize) { VFS_WriteAt(disk->FD, base+Offset, retLen, Buffer); - if(bNewBlocks) return Length; + if(!bNewBlocks) return Length; goto addBlocks; // Ugh! A goto, but it seems unavoidable } @@ -295,7 +320,7 @@ Uint64 Ext2_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) // Write last block base = Ext2_int_GetBlockAddr(disk, inode.i_block, block); VFS_WriteAt(disk->FD, base, retLen, Buffer); - if(bNewBlocks) return Length; // Writing in only allocated space + if(!bNewBlocks) return Length; // Writing in only allocated space } addBlocks: