Merge branch 'master' of git://localhost/acess2
[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  int    VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
23
24 // === GLOBALS ===
25 tVFS_Handle     *gaUserHandles = (void*)MM_PPD_HANDLES;
26 tVFS_Handle     *gaKernelHandles = (void*)MM_KERNEL_VFS;
27
28 // === CODE ===
29 inline void _ReferenceNode(tVFS_Node *Node)
30 {
31         if( !MM_GetPhysAddr(Node->Type) ) {
32                 Log_Error("VFS", "Node %p's type is invalid (%p bad pointer) - %P corrupted",
33                         Node, Node->Type, MM_GetPhysAddr(&Node->Type));
34                 return ;
35         }
36         if( Node->Type && Node->Type->Reference )
37                 Node->Type->Reference( Node );
38         else
39                 Node->ReferenceCount ++;
40 }
41
42 /**
43  * \fn tVFS_Handle *VFS_GetHandle(int FD)
44  * \brief Gets a pointer to the handle information structure
45  */
46 tVFS_Handle *VFS_GetHandle(int FD)
47 {
48         tVFS_Handle     *h;
49         
50         //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
51         
52         if(FD < 0)      return NULL;
53         
54         if(FD & VFS_KERNEL_FLAG) {
55                 FD &= (VFS_KERNEL_FLAG - 1);
56                 if(FD >= MAX_KERNEL_FILES)      return NULL;
57                 h = &gaKernelHandles[ FD ];
58         } else {
59                 if(FD >= *Threads_GetMaxFD())   return NULL;
60                 h = &gaUserHandles[ FD ];
61         }
62         
63         if(h->Node == NULL)     return NULL;
64         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
65         return h;
66 }
67
68 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
69 {
70          int    i;
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( 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(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    i;
178          int    max_handles = *Threads_GetMaxFD();
179         
180         // Check if this process has any handles
181         if( MM_GetPhysAddr( gaUserHandles ) == 0 )
182                 return NULL;
183
184         // Allocate
185         ret = malloc( NumFDs * sizeof(tVFS_Handle) );
186         if( !ret )
187                 return NULL;    
188
189         if( NumFDs > max_handles )
190                 NumFDs = max_handles;
191
192         // Take copies of the handles
193         for( i = 0; i < NumFDs; i ++ )
194         {
195                 tVFS_Handle     *h;
196                 if( FDs == NULL )
197                         h = &gaUserHandles[i];
198                 else {
199                         h = VFS_GetHandle(FDs[i] & (VFS_KERNEL_FLAG - 1));
200                         if(!h) {
201                                 Log_Warning("VFS", "VFS_SaveHandles - Invalid FD %i",
202                                         FDs[i] & (VFS_KERNEL_FLAG - 1) );
203                                 free(ret);
204                                 return NULL;
205                         }
206                 }
207                 memcpy( &ret[i], h, sizeof(tVFS_Handle) );
208                 
209                 // Reference node
210                 if( !h->Node )
211                         continue ;
212                 _ReferenceNode(h->Node);
213                 h->Mount->OpenHandleCount ++;
214         }       
215
216         return ret;
217 }
218
219 void VFS_RestoreHandles(int NumFDs, void *Handles)
220 {
221         tVFS_Handle     *handles = Handles;
222          int    i;
223
224         // NULL = nothing to do
225         if( !Handles )
226                 return ;        
227
228         // Check if there is already a set of handles
229         if( MM_GetPhysAddr( gaUserHandles ) != 0 )
230                 return ;
231         
232         
233         // Allocate user handle area
234         {
235                 Uint    addr, size;
236                  int    max_handles = *Threads_GetMaxFD();
237                 size = max_handles * sizeof(tVFS_Handle);
238                 for(addr = 0; addr < size; addr += 0x1000)
239                 {
240                         if( !MM_Allocate( (tVAddr)gaUserHandles + addr ) )
241                         {
242                                 Warning("OOM - VFS_AllocHandle");
243                                 Threads_Exit(0, 0xFF);  // Terminate user
244                         }
245                 }
246                 memset( gaUserHandles, 0, size );
247         }
248         
249         // Restore handles
250         memcpy( gaUserHandles, handles, NumFDs * sizeof(tVFS_Handle) );
251         // Reference when copied
252         for( i = 0; i < NumFDs; i ++ )
253         {
254                 tVFS_Handle     *h = &handles[i];
255         
256                 if( !h->Node )
257                         continue ;
258                 _ReferenceNode(h->Node);
259                 h->Mount->OpenHandleCount ++;
260         }
261 }
262
263 void VFS_FreeSavedHandles(int NumFDs, void *Handles)
264 {
265         tVFS_Handle     *handles = Handles;
266          int    i;
267
268         // NULL = nothing to do
269         if( !Handles )
270                 return ;        
271         
272         // Dereference all saved nodes
273         for( i = 0; i < NumFDs; i ++ )
274         {
275                 tVFS_Handle     *h = &handles[i];
276         
277                 if( !h->Node )
278                         continue ;
279                 _CloseNode(h->Node);
280                 
281                 ASSERT(h->Mount->OpenHandleCount > 0);
282                 LOG("dec. mntpt '%s' to %i", h->Mount->MountPoint, h->Mount->OpenHandleCount-1);
283                 h->Mount->OpenHandleCount --;
284         }
285         free( Handles );
286 }

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