Merge branch 'master' of git://localhost/acess2
[tpg/acess2.git] / KernelLand / Kernel / vfs / handle.c
index 6d2b83f..cb4fae1 100644 (file)
@@ -2,6 +2,7 @@
  * Acess2 VFS
  * - AllocHandle, GetHandle
  */
+#define SANITY 1
 #define DEBUG  0
 #include <acess.h>
 #include <mm_virt.h>
@@ -18,6 +19,7 @@
 #if 0
 tVFS_Handle    *VFS_GetHandle(int FD);
 #endif
+inline void    _ReferenceNode(tVFS_Node *Node);
  int   VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
 
 // === GLOBALS ===
@@ -25,6 +27,19 @@ tVFS_Handle  *gaUserHandles = (void*)MM_PPD_HANDLES;
 tVFS_Handle    *gaKernelHandles = (void*)MM_KERNEL_VFS;
 
 // === CODE ===
+inline void _ReferenceNode(tVFS_Node *Node)
+{
+       if( !MM_GetPhysAddr(Node->Type) ) {
+               Log_Error("VFS", "Node %p's type is invalid (%p bad pointer) - %P corrupted",
+                       Node, Node->Type, MM_GetPhysAddr(&Node->Type));
+               return ;
+       }
+       if( Node->Type && Node->Type->Reference )
+               Node->Type->Reference( Node );
+       else
+               Node->ReferenceCount ++;
+}
+
 /**
  * \fn tVFS_Handle *VFS_GetHandle(int FD)
  * \brief Gets a pointer to the handle information structure
@@ -60,7 +75,7 @@ int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
        {
                 int    max_handles = *Threads_GetMaxFD();
                // Allocate Buffer
-               if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) == 0 )
+               if( MM_GetPhysAddr( gaUserHandles ) == 0 )
                {
                        Uint    addr, size;
                        size = max_handles * sizeof(tVFS_Handle);
@@ -87,7 +102,7 @@ int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
        else
        {
                // Allocate space if not already
-               if( MM_GetPhysAddr( (tVAddr)gaKernelHandles ) == 0 )
+               if( MM_GetPhysAddr( gaKernelHandles ) == 0 )
                {
                        Uint    addr, size;
                        size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
@@ -121,7 +136,7 @@ void VFS_ReferenceUserHandles(void)
         int    max_handles = *Threads_GetMaxFD();
 
        // Check if this process has any handles
-       if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) == 0 )
+       if( MM_GetPhysAddr( gaUserHandles ) == 0 )
                return ;
        
        for( i = 0; i < max_handles; i ++ )
@@ -130,8 +145,8 @@ void VFS_ReferenceUserHandles(void)
                h = &gaUserHandles[i];
                if( !h->Node )
                        continue ;
-               if( h->Node->Type && h->Node->Type->Reference )
-                       h->Node->Type->Reference( h->Node );
+               _ReferenceNode(h->Node);
+               h->Mount->OpenHandleCount ++;
        }
 }
 
@@ -141,7 +156,7 @@ void VFS_CloseAllUserHandles(void)
         int    max_handles = *Threads_GetMaxFD();
 
        // Check if this process has any handles
-       if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) == 0 )
+       if( MM_GetPhysAddr( gaUserHandles ) == 0 )
                return ;
        
        for( i = 0; i < max_handles; i ++ )
@@ -150,8 +165,7 @@ void VFS_CloseAllUserHandles(void)
                h = &gaUserHandles[i];
                if( !h->Node )
                        continue ;
-               if( h->Node->Type && h->Node->Type->Close )
-                       h->Node->Type->Close( h->Node );
+               _CloseNode(h->Node);
        }
 }
 
@@ -165,7 +179,7 @@ void *VFS_SaveHandles(int NumFDs, int *FDs)
         int    max_handles = *Threads_GetMaxFD();
        
        // Check if this process has any handles
-       if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) == 0 )
+       if( MM_GetPhysAddr( gaUserHandles ) == 0 )
                return NULL;
 
        // Allocate
@@ -182,7 +196,14 @@ void *VFS_SaveHandles(int NumFDs, int *FDs)
                tVFS_Handle     *h;
                if( FDs == NULL )
                        h = &gaUserHandles[i];
-               else {
+               else if( FDs[i] == -1 )
+               {
+                       Log_Warning("VFS", "VFS_SaveHandles - Slot %i error FD (-1), ignorning", i);
+                       memset(&ret[i], 0, sizeof(tVFS_Handle));
+                       continue ;
+               }
+               else
+               {
                        h = VFS_GetHandle(FDs[i] & (VFS_KERNEL_FLAG - 1));
                        if(!h) {
                                Log_Warning("VFS", "VFS_SaveHandles - Invalid FD %i",
@@ -196,8 +217,7 @@ void *VFS_SaveHandles(int NumFDs, int *FDs)
                // Reference node
                if( !h->Node )
                        continue ;
-               if( h->Node->Type && h->Node->Type->Reference )
-                       h->Node->Type->Reference( h->Node );
+               _ReferenceNode(h->Node);
                h->Mount->OpenHandleCount ++;
        }       
 
@@ -214,7 +234,7 @@ void VFS_RestoreHandles(int NumFDs, void *Handles)
                return ;        
 
        // Check if there is already a set of handles
-       if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) != 0 )
+       if( MM_GetPhysAddr( gaUserHandles ) != 0 )
                return ;
        
        
@@ -243,8 +263,7 @@ void VFS_RestoreHandles(int NumFDs, void *Handles)
        
                if( !h->Node )
                        continue ;
-               if( h->Node->Type && h->Node->Type->Reference )
-                       h->Node->Type->Reference( h->Node );
+               _ReferenceNode(h->Node);
                h->Mount->OpenHandleCount ++;
        }
 }
@@ -265,8 +284,10 @@ void VFS_FreeSavedHandles(int NumFDs, void *Handles)
        
                if( !h->Node )
                        continue ;
-               if( h->Node->Type && h->Node->Type->Close )
-                       h->Node->Type->Close( h->Node );
+               _CloseNode(h->Node);
+               
+               ASSERT(h->Mount->OpenHandleCount > 0);
+               LOG("dec. mntpt '%s' to %i", h->Mount->MountPoint, h->Mount->OpenHandleCount-1);
                h->Mount->OpenHandleCount --;
        }
        free( Handles );

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