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

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