Kernel - Added return value to module cleanup, fixed LVM bugs
[tpg/acess2.git] / KernelLand / Modules / Filesystems / FAT / fat.c
index 0706697..d602510 100644 (file)
@@ -43,7 +43,7 @@ void  FAT_CloseFile(tVFS_Node *node);
  int   giFAT_MaxCachedClusters = 1024*512/4;\r
 \r
 // === SEMI-GLOBALS ===\r
-MODULE_DEFINE(0, VER2(1,1) /*v1.01*/, VFAT, FAT_Install, NULL, NULL);\r
+MODULE_DEFINE(0, VER2(0,80) /*v0.80*/, VFAT, FAT_Install, NULL, NULL);\r
 tFAT_VolInfo   gFAT_Disks[8];\r
  int   giFAT_PartCount = 0;\r
 tVFS_Driver    gFAT_FSInfo = {"fat", 0, FAT_InitDevice, FAT_Unmount, FAT_GetNodeFromINode, NULL};\r
@@ -105,6 +105,7 @@ tVFS_Node *FAT_InitDevice(const char *Device, const char **Options)
        \r
        if(bs->bps == 0 || bs->spc == 0) {\r
                Log_Notice("FAT", "Error in FAT Boot Sector (zero BPS/SPC)");\r
+               VFS_Close(diskInfo->fileHandle);\r
                return NULL;\r
        }\r
        \r
@@ -185,6 +186,7 @@ tVFS_Node *FAT_InitDevice(const char *Device, const char **Options)
                diskInfo->FATCache = (Uint32*)malloc(sizeof(Uint32)*diskInfo->ClusterCount);\r
                if(diskInfo->FATCache == NULL) {\r
                        Log_Warning("FAT", "Heap Exhausted");\r
+                       VFS_Cose(diskInfo->fileHandle);\r
                        return NULL;\r
                }\r
                Ofs = bs->resvSectCount*512;\r
@@ -295,7 +297,7 @@ int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Clu
        ENTER("pNode XOffset", Node, Offset);\r
        \r
        cluster = base_cluster = Node->Inode & 0xFFFFFFF;       // Cluster ID\r
-       LOG("base cluster = 0x%07x", cluster);\r
+//     LOG("base cluster = 0x%07x", cluster);\r
        \r
        // Do Cluster Skip\r
        // - Pre FAT32 had a reserved area for the root.\r
@@ -315,12 +317,12 @@ int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Clu
        }\r
        else {\r
                // TODO: Bounds checking on root\r
-               LOG("Root cluster count %i", disk->bootsect.files_in_root*32/disk->BytesPerCluster);\r
+//             LOG("Root cluster count %i", disk->bootsect.files_in_root*32/disk->BytesPerCluster);\r
                // Increment by clusters in offset\r
                cluster += Offset / disk->BytesPerCluster;\r
        }\r
        \r
-       LOG("cluster = 0x%07x", cluster);\r
+//     LOG("cluster = 0x%07x", cluster);\r
        \r
        // Bounds Checking (Used to spot corruption)\r
        if(cluster > disk->ClusterCount + 2)\r
@@ -483,9 +485,12 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
         int    remLength = Length;\r
        Uint32  cluster, tmpCluster;\r
         int    bNewCluster = 0;\r
+       off_t   original_offset = Offset;\r
        \r
        if(Offset > Node->Size) return 0;\r
        \r
+       ENTER("pNode Xoffset xlength pbuffer", Node, Offset, Length, Buffer);\r
+       \r
        // Seek Clusters\r
        cluster = Node->Inode & 0xFFFFFFFF;\r
        while( Offset > disk->BytesPerCluster )\r
@@ -493,6 +498,7 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
                cluster = FAT_int_GetFatValue( disk, cluster );\r
                if(cluster == -1) {\r
                        Log_Warning("FAT", "EOC Unexpectedly Reached");\r
+                       LEAVE('i', 0);\r
                        return 0;\r
                }\r
                Offset -= disk->BytesPerCluster;\r
@@ -500,7 +506,10 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
        if( Offset == disk->BytesPerCluster )\r
        {\r
                Uint32  tmp = FAT_int_AllocateCluster(disk, cluster);\r
-               if(!tmp)        return 0;\r
+               if(!tmp) {\r
+                       LEAVE('i', 0);\r
+                       return 0;\r
+               }\r
                cluster = tmp;\r
                Offset -= disk->BytesPerCluster;\r
        }\r
@@ -509,17 +518,20 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
        {\r
                char    tmpBuf[disk->BytesPerCluster];\r
                \r
+               LOG("Read-Modify-Write single");\r
+               \r
                // Read-Modify-Write\r
                FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
                memcpy( tmpBuf + Offset, Buffer, Length );\r
                FAT_int_WriteCluster( disk, cluster, tmpBuf );\r
-               \r
-               return Length;\r
+               goto return_full;\r
        }\r
        \r
        // Clean up changes within a cluster\r
        if( Offset )\r
        {       \r
+               LOG("Read-Modify-Write first");\r
+\r
                // Read-Modify-Write\r
                FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
                memcpy( tmpBuf + Offset, Buffer, disk->BytesPerCluster - Offset );\r
@@ -532,9 +544,8 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
                tmpCluster = FAT_int_GetFatValue(disk, cluster);\r
                if(tmpCluster == -1) {\r
                        tmpCluster = FAT_int_AllocateCluster(disk, cluster);\r
-                       if( tmpCluster == 0 ) {\r
-                               return Length - remLength;\r
-                       }\r
+                       if( tmpCluster == 0 )\r
+                               goto ret_incomplete;\r
                }\r
                cluster = tmpCluster;\r
        }\r
@@ -543,28 +554,47 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
        {\r
                FAT_int_WriteCluster( disk, cluster, Buffer );\r
                Buffer += disk->BytesPerCluster;\r
+               remLength -= disk->BytesPerCluster;\r
                \r
                // Get next cluster (allocating if needed)\r
                tmpCluster = FAT_int_GetFatValue(disk, cluster);\r
                if(tmpCluster == -1) {\r
                        bNewCluster = 1;\r
                        tmpCluster = FAT_int_AllocateCluster(disk, cluster);\r
-                       if( tmpCluster == 0 ) {\r
-                               return Length - remLength;\r
-                       }\r
+                       if( tmpCluster == 0 )\r
+                               goto ret_incomplete;\r
                }\r
                cluster = tmpCluster;\r
        }\r
        \r
        // Finish off\r
-       if( bNewCluster )\r
-               memset(tmpBuf, 0, disk->BytesPerCluster);\r
-       else\r
-               FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
-       memcpy( tmpBuf, Buffer, remLength );\r
-       FAT_int_WriteCluster( disk, cluster, tmpBuf );\r
-       free( tmpBuf );\r
-       \r
+       if( remLength )\r
+       {\r
+               if( bNewCluster )\r
+                       memset(tmpBuf, 0, disk->BytesPerCluster);\r
+               else\r
+                       FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
+               memcpy( tmpBuf, Buffer, remLength );\r
+               FAT_int_WriteCluster( disk, cluster, tmpBuf );\r
+       }\r
+\r
+return_full:\r
+       if( original_offset + Length > Node->Size ) {\r
+               Node->Size = original_offset + Length;\r
+               LOG("Updated size to %x", Node->Size);\r
+               Node->ImplInt |= FAT_FLAG_DIRTY;\r
+       }\r
+\r
+       LEAVE('i', Length);\r
+       return Length;\r
+ret_incomplete:\r
+       LOG("Write incomplete");\r
+       Length -= remLength;\r
+       if( original_offset + Length > Node->Size ) {\r
+               Node->Size = original_offset + Length;  \r
+               Node->ImplInt |= FAT_FLAG_DIRTY;\r
+       }\r
+       LEAVE('i', Length);\r
        return Length;\r
 }\r
 #endif\r
@@ -577,7 +607,9 @@ void FAT_CloseFile(tVFS_Node *Node)
 {\r
        tFAT_VolInfo    *disk = Node->ImplPtr;\r
        if(Node == NULL)        return ;\r
-       \r
+\r
+       ENTER("pNode", Node);   \r
+\r
        #if SUPPORT_WRITE\r
        // Update the node if it's dirty (don't bother if it's marked for\r
        // deletion)\r
@@ -589,6 +621,7 @@ void FAT_CloseFile(tVFS_Node *Node)
                dirnode = FAT_int_CreateIncompleteDirNode(disk, Node->Inode >> 32);\r
                if( !dirnode ) {\r
                        Log_Error("FAT", "Can't get node for directory cluster #0x%x", Node->Inode>>32);\r
+                       LEAVE('-');\r
                        return ;\r
                }\r
 \r
@@ -602,22 +635,22 @@ void FAT_CloseFile(tVFS_Node *Node)
                Node->ImplInt &= ~FAT_FLAG_DIRTY;\r
        }\r
        #endif\r
+\r
+       Uint32  cluster = Node->Inode;\r
+       Uint32  implint = Node->ImplInt;\r
        \r
-       // TODO: Make this more thread safe somehow, probably by moving the\r
-       // Inode_UncacheNode higher up and saving the cluster value somewhere\r
-       if( Node->ReferenceCount == 1 )\r
-       {               \r
-               #if SUPPORT_WRITE\r
+       #if SUPPORT_WRITE\r
+       if( FAT_int_DerefNode(Node) == 1 )\r
+       {\r
+               LOG("implint = %x", implint);\r
                // Delete File\r
-               if( Node->ImplInt & FAT_FLAG_DELETE ) {\r
+               if( implint & FAT_FLAG_DELETE ) {\r
+                       Log_Debug("FAT", "Deallocating chain stating at 0x%07x", cluster);\r
                        // Since the node is marked, we only need to remove it's data\r
-                       Uint32  cluster = Node->Inode & 0xFFFFFFFF;\r
                        while( cluster != -1 )\r
-                               cluster = FAT_int_FreeCluster(Node->ImplPtr, cluster);\r
+                               cluster = FAT_int_FreeCluster(disk, cluster);\r
                }\r
-               #endif\r
        }\r
-       \r
-       FAT_int_DerefNode(Node);\r
-       return ;\r
+       #endif\r
+       LEAVE('-');\r
 }\r

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