cb4fae1200f936cac5eb0210546c63c7c8175211
[tpg/acess2.git] / KernelLand / Kernel / vfs / handle.c
1 /*
2  * Acess2 VFS
3  * - AllocHandle, GetHandle
4  */
5 #define SANITY  1
6 #define DEBUG   0
7 #include <acess.h>
8 #include <mm_virt.h>
9 #include "vfs.h"
10 #include "vfs_int.h"
11 #include "vfs_ext.h"
12 #include <threads.h>    // GetMaxFD
13 #include <vfs_threads.h>        // Handle maintainance
14
15 // === CONSTANTS ===
16 #define MAX_KERNEL_FILES        128
17
18 // === PROTOTYPES ===
19 #if 0
20 tVFS_Handle     *VFS_GetHandle(int FD);
21 #endif
22 inline void     _ReferenceNode(tVFS_Node *Node);
23  int    VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
24
25 // === GLOBALS ===
26 tVFS_Handle     *gaUserHandles = (void*)MM_PPD_HANDLES;
27 tVFS_Handle     *gaKernelHandles = (void*)MM_KERNEL_VFS;
28
29 // === CODE ===
30 inline void _ReferenceNode(tVFS_Node *Node)
31 {
32         if( !MM_GetPhysAddr(Node->Type) ) {
33                 Log_Error("VFS", "Node %p's type is invalid (%p bad pointer) - %P corrupted",
34                         Node, Node->Type, MM_GetPhysAddr(&Node->Type));
35                 return ;
36         }
37         if( Node->Type && Node->Type->Reference )
38                 Node->Type->Reference( Node );
39         else
40                 Node->ReferenceCount ++;
41 }
42
43 /**
44  * \fn tVFS_Handle *VFS_GetHandle(int FD)
45  * \brief Gets a pointer to the handle information structure
46  */
47 tVFS_Handle *VFS_GetHandle(int FD)
48 {
49         tVFS_Handle     *h;
50         
51         //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
52         
53         if(FD < 0)      return NULL;
54         
55         if(FD & VFS_KERNEL_FLAG) {
56                 FD &= (VFS_KERNEL_FLAG - 1);
57                 if(FD >= MAX_KERNEL_FILES)      return NULL;
58                 h = &gaKernelHandles[ FD ];
59         } else {
60                 if(FD >= *Threads_GetMaxFD())   return NULL;
61                 h = &gaUserHandles[ FD ];
62         }
63         
64         if(h->Node == NULL)     return NULL;
65         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
66         return h;
67 }
68
69 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
70 {
71          int    i;
72         
73         // Check for a user open
74         if(bIsUser)
75         {
76                  int    max_handles = *Threads_GetMaxFD();
77                 // Allocate Buffer
78                 if( MM_GetPhysAddr( gaUserHandles ) == 0 )
79                 {
80                         Uint    addr, size;
81                         size = max_handles * sizeof(tVFS_Handle);
82                         for(addr = 0; addr < size; addr += 0x1000)
83                         {
84                                 if( !MM_Allocate( (tVAddr)gaUserHandles + addr ) )
85                                 {
86                                         Warning("OOM - VFS_AllocHandle");
87                                         Threads_Exit(0, 0xFF);  // Terminate user
88                                 }
89                         }
90                         memset( gaUserHandles, 0, size );
91                 }
92                 // Get a handle
93                 for( i = 0; i < max_handles; i ++ )
94                 {
95                         if(gaUserHandles[i].Node)       continue;
96                         gaUserHandles[i].Node = Node;
97                         gaUserHandles[i].Position = 0;
98                         gaUserHandles[i].Mode = Mode;
99                         return i;
100                 }
101         }
102         else
103         {
104                 // Allocate space if not already
105                 if( MM_GetPhysAddr( gaKernelHandles ) == 0 )
106                 {
107                         Uint    addr, size;
108                         size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
109                         for(addr = 0; addr < size; addr += 0x1000)
110                         {
111                                 if( !MM_Allocate( (tVAddr)gaKernelHandles + addr ) )
112                                 {
113                                         Panic("OOM - VFS_AllocHandle");
114                                         Threads_Exit(0, 0xFF);  // Terminate application (get some space back)
115                                 }
116                         }
117                         memset( gaKernelHandles, 0, size );
118                 }
119                 // Get a handle
120                 for(i=0;i<MAX_KERNEL_FILES;i++)
121                 {
122                         if(gaKernelHandles[i].Node)     continue;
123                         gaKernelHandles[i].Node = Node;
124                         gaKernelHandles[i].Position = 0;
125                         gaKernelHandles[i].Mode = Mode;
126                         return i|VFS_KERNEL_FLAG;
127                 }
128         }
129         
130         return -1;
131 }
132
133 void VFS_ReferenceUserHandles(void)
134 {
135          int    i;
136          int    max_handles = *Threads_GetMaxFD();
137
138         // Check if this process has any handles
139         if( MM_GetPhysAddr( gaUserHandles ) == 0 )
140                 return ;
141         
142         for( i = 0; i < max_handles; i ++ )
143         {
144                 tVFS_Handle     *h;
145                 h = &gaUserHandles[i];
146                 if( !h->Node )
147                         continue ;
148                 _ReferenceNode(h->Node);
149                 h->Mount->OpenHandleCount ++;
150         }
151 }
152
153 void VFS_CloseAllUserHandles(void)
154 {
155          int    i;
156          int    max_handles = *Threads_GetMaxFD();
157
158         // Check if this process has any handles
159         if( MM_GetPhysAddr( gaUserHandles ) == 0 )
160                 return ;
161         
162         for( i = 0; i < max_handles; i ++ )
163         {
164                 tVFS_Handle     *h;
165                 h = &gaUserHandles[i];
166                 if( !h->Node )
167                         continue ;
168                 _CloseNode(h->Node);
169         }
170 }
171
172 /**
173  * \brief Take a backup of a set of file descriptors
174  */
175 void *VFS_SaveHandles(int NumFDs, int *FDs)
176 {
177         tVFS_Handle     *ret;
178          int    i;
179          int    max_handles = *Threads_GetMaxFD();
180         
181         // Check if this process has any handles
182         if( MM_GetPhysAddr( gaUserHandles ) == 0 )
183                 return NULL;
184
185         // Allocate
186         ret = malloc( NumFDs * sizeof(tVFS_Handle) );
187         if( !ret )
188                 return NULL;    
189
190         if( NumFDs > max_handles )
191                 NumFDs = max_handles;
192
193         // Take copies of the handles
194         for( i = 0; i < NumFDs; i ++ )
195         {
196                 tVFS_Handle     *h;
197                 if( FDs == NULL )
198                         h = &gaUserHandles[i];
199                 else if( FDs[i] == -1 )
200                 {
201                         Log_Warning("VFS", "VFS_SaveHandles - Slot %i error FD (-1), ignorning", i);
202                         memset(&ret[i], 0, sizeof(tVFS_Handle));
203                         continue ;
204                 }
205                 else
206                 {
207                         h = VFS_GetHandle(FDs[i] & (VFS_KERNEL_FLAG - 1));
208                         if(!h) {
209                                 Log_Warning("VFS", "VFS_SaveHandles - Invalid FD %i",
210                                         FDs[i] & (VFS_KERNEL_FLAG - 1) );
211                                 free(ret);
212                                 return NULL;
213                         }
214                 }
215                 memcpy( &ret[i], h, sizeof(tVFS_Handle) );
216                 
217                 // Reference node
218                 if( !h->Node )
219                         continue ;
220                 _ReferenceNode(h->Node);
221                 h->Mount->OpenHandleCount ++;
222         }       
223
224         return ret;
225 }
226
227 void VFS_RestoreHandles(int NumFDs, void *Handles)
228 {
229         tVFS_Handle     *handles = Handles;
230          int    i;
231
232         // NULL = nothing to do
233         if( !Handles )
234                 return ;        
235
236         // Check if there is already a set of handles
237         if( MM_GetPhysAddr( gaUserHandles ) != 0 )
238                 return ;
239         
240         
241         // Allocate user handle area
242         {
243                 Uint    addr, size;
244                  int    max_handles = *Threads_GetMaxFD();
245                 size = max_handles * sizeof(tVFS_Handle);
246                 for(addr = 0; addr < size; addr += 0x1000)
247                 {
248                         if( !MM_Allocate( (tVAddr)gaUserHandles + addr ) )
249                         {
250                                 Warning("OOM - VFS_AllocHandle");
251                                 Threads_Exit(0, 0xFF);  // Terminate user
252                         }
253                 }
254                 memset( gaUserHandles, 0, size );
255         }
256         
257         // Restore handles
258         memcpy( gaUserHandles, handles, NumFDs * sizeof(tVFS_Handle) );
259         // Reference when copied
260         for( i = 0; i < NumFDs; i ++ )
261         {
262                 tVFS_Handle     *h = &handles[i];
263         
264                 if( !h->Node )
265                         continue ;
266                 _ReferenceNode(h->Node);
267                 h->Mount->OpenHandleCount ++;
268         }
269 }
270
271 void VFS_FreeSavedHandles(int NumFDs, void *Handles)
272 {
273         tVFS_Handle     *handles = Handles;
274          int    i;
275
276         // NULL = nothing to do
277         if( !Handles )
278                 return ;        
279         
280         // Dereference all saved nodes
281         for( i = 0; i < NumFDs; i ++ )
282         {
283                 tVFS_Handle     *h = &handles[i];
284         
285                 if( !h->Node )
286                         continue ;
287                 _CloseNode(h->Node);
288                 
289                 ASSERT(h->Mount->OpenHandleCount > 0);
290                 LOG("dec. mntpt '%s' to %i", h->Mount->MountPoint, h->Mount->OpenHandleCount-1);
291                 h->Mount->OpenHandleCount --;
292         }
293         free( Handles );
294 }

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