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

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