* Acess2 VFS
* - AllocHandle, GetHandle
*/
+#define SANITY 1
#define DEBUG 0
#include <acess.h>
#include <mm_virt.h>
#define MAX_KERNEL_FILES 128
// === PROTOTYPES ===
-#if 0
-tVFS_Handle *VFS_GetHandle(int FD);
-#endif
- int VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
// === GLOBALS ===
tVFS_Handle *gaUserHandles = (void*)MM_PPD_HANDLES;
return h;
}
-int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
+int VFS_SetHandle(int FD, tVFS_Node *Node, int Mode)
{
- int i;
+ tVFS_Handle *h;
+ if(FD < 0) return -1;
+ if( FD & VFS_KERNEL_FLAG ) {
+ FD &= (VFS_KERNEL_FLAG -1);
+ if( FD >= MAX_KERNEL_FILES ) return -1;
+ h = &gaKernelHandles[FD];
+ }
+ else {
+ if( FD >= *Threads_GetMaxFD()) return -1;
+ h = &gaUserHandles[FD];
+ }
+ h->Node = Node;
+ h->Mode = Mode;
+ return FD;
+}
+
+int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
+{
// Check for a user open
if(bIsUser)
{
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);
memset( gaUserHandles, 0, size );
}
// Get a handle
- for( i = 0; i < max_handles; i ++ )
+ for( int i = 0; i < max_handles; i ++ )
{
if(gaUserHandles[i].Node) continue;
gaUserHandles[i].Node = Node;
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);
memset( gaKernelHandles, 0, size );
}
// Get a handle
- for(i=0;i<MAX_KERNEL_FILES;i++)
+ for(int i=0;i<MAX_KERNEL_FILES;i++)
{
if(gaKernelHandles[i].Node) continue;
gaKernelHandles[i].Node = Node;
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 ++ )
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 ++;
}
}
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 ++ )
h = &gaUserHandles[i];
if( !h->Node )
continue ;
- if( h->Node->Type && h->Node->Type->Close )
- h->Node->Type->Close( h->Node );
+ _CloseNode(h->Node);
}
}
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
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",
// 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 ++;
}
return ret;
return ;
// Check if there is already a set of handles
- if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) != 0 )
+ if( MM_GetPhysAddr( gaUserHandles ) != 0 )
return ;
if( !h->Node )
continue ;
- if( h->Node->Type && h->Node->Type->Reference )
- h->Node->Type->Reference( h->Node );
+ _ReferenceNode(h->Node);
+ h->Mount->OpenHandleCount ++;
}
}
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 );
}