Implemented basic disk write support in EXT2.
[tpg/acess2.git] / Kernel / vfs / fs / ext2.c
index 1da9c0f..da941f7 100644 (file)
@@ -201,9 +201,22 @@ Uint64 Ext2_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
        // Get Inode\r
        Ext2_int_GetInode(Node, &inode);\r
        \r
+       // Sanity Checks\r
+       if(Offset >= inode.i_size) {\r
+               LEAVE('i', 0);\r
+               return 0;\r
+       }\r
+       if(Offset + Length > inode.i_size)\r
+               Length = inode.i_size - Offset;\r
+       \r
        block = Offset / disk->BlockSize;\r
        Offset = Offset / disk->BlockSize;\r
        base = Ext2_int_GetBlockAddr(disk, inode.i_block, block);\r
+       if(base == 0) {\r
+               Warning("[EXT2 ] NULL Block Detected in INode 0x%llx\n", Node->Inode);\r
+               LEAVE('i', 0);\r
+               return 0;\r
+       }\r
        \r
        // Read only block\r
        if(Length <= disk->BlockSize - Offset)\r
@@ -224,6 +237,11 @@ Uint64 Ext2_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
        while(remLen > disk->BlockSize)\r
        {\r
                base = Ext2_int_GetBlockAddr(disk, inode.i_block, block);\r
+               if(base == 0) {\r
+                       Warning("[EXT2 ] NULL Block Detected in INode 0x%llx\n", Node->Inode);\r
+                       LEAVE('i', 0);\r
+                       return 0;\r
+               }\r
                VFS_ReadAt( disk->FD, base, disk->BlockSize, Buffer);\r
                Buffer += disk->BlockSize;\r
                remLen -= disk->BlockSize;\r
@@ -252,18 +270,25 @@ Uint64 Ext2_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
        Uint64  allocSize;\r
         int    bNewBlocks = 0;\r
        \r
+       Debug_HexDump("Ext2_Write", Buffer, Length);\r
+       \r
        Ext2_int_GetInode(Node, &inode);\r
        \r
-       // Round size up to block size\r
-       // block size is a power of two, so this will work\r
+       // Get the ammount of space already allocated\r
+       // - Round size up to block size\r
+       // - block size is a power of two, so this will work\r
        allocSize = (inode.i_size + disk->BlockSize) & ~(disk->BlockSize-1);\r
+       \r
+       // Are we writing to inside the allocated space?\r
        if( Offset < allocSize )\r
        {\r
+               // Will we go out of it?\r
                if(Offset + Length > allocSize) {\r
                        bNewBlocks = 1;\r
                        retLen = allocSize - Offset;\r
                } else\r
                        retLen = Length;\r
+               \r
                // Within the allocated space\r
                block = Offset / disk->BlockSize;\r
                Offset %= disk->BlockSize;\r
@@ -272,7 +297,7 @@ Uint64 Ext2_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
                // Write only block (if only one)\r
                if(Offset + retLen <= disk->BlockSize) {\r
                        VFS_WriteAt(disk->FD, base+Offset, retLen, Buffer);\r
-                       if(bNewBlocks)  return Length;\r
+                       if(!bNewBlocks) return Length;\r
                        goto addBlocks; // Ugh! A goto, but it seems unavoidable\r
                }\r
                \r
@@ -295,7 +320,7 @@ Uint64 Ext2_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
                // Write last block\r
                base = Ext2_int_GetBlockAddr(disk, inode.i_block, block);\r
                VFS_WriteAt(disk->FD, base, retLen, Buffer);\r
-               if(bNewBlocks)  return Length;  // Writing in only allocated space\r
+               if(!bNewBlocks) return Length;  // Writing in only allocated space\r
        }\r
        \r
 addBlocks:\r

UCC git Repository :: git.ucc.asn.au