branch update for HEAD-2002110701
[reactos.git] / ntoskrnl / io / iomgr.c
1 /* $Id$
2  *
3  * COPYRIGHT:            See COPYING in the top level directory
4  * PROJECT:              ReactOS kernel
5  * FILE:                 ntoskrnl/io/iomgr.c
6  * PURPOSE:              Initializes the io manager
7  * PROGRAMMER:           David Welch (welch@mcmail.com)
8  * REVISION HISTORY:
9  *             29/07/98: Created
10  */
11
12 /* INCLUDES ****************************************************************/
13
14 #include <limits.h>
15 #include <ddk/ntddk.h>
16 #include <internal/ob.h>
17 #include <internal/io.h>
18 #include <internal/pool.h>
19
20 #define NDEBUG
21 #include <internal/debug.h>
22
23 /* GLOBALS *******************************************************************/
24
25 #define TAG_DEVICE_TYPE     TAG('D', 'E', 'V', 'T')
26 #define TAG_FILE_TYPE       TAG('F', 'I', 'L', 'E')
27
28 /* DATA ********************************************************************/
29
30
31 POBJECT_TYPE EXPORTED IoDeviceObjectType = NULL;
32 POBJECT_TYPE EXPORTED IoFileObjectType = NULL;
33 #ifndef LIBCAPTIVE
34 ULONG        EXPORTED IoReadOperationCount = 0; /* FIXME: unknown type */
35 ULONG        EXPORTED IoReadTransferCount = 0;  /* FIXME: unknown type */
36 ULONG        EXPORTED IoWriteOperationCount = 0; /* FIXME: unknown type */
37 ULONG        EXPORTED IoWriteTransferCount = 0; /* FIXME: unknown type */
38 ULONG        EXPORTED IoStatisticsLock = 0;     /* FIXME: unknown type */
39 #endif /* LIBCAPTIVE */
40
41 static GENERIC_MAPPING IopFileMapping = {FILE_GENERIC_READ,
42                                          FILE_GENERIC_WRITE,
43                                          FILE_GENERIC_EXECUTE,
44                                          FILE_ALL_ACCESS};
45
46 /* FUNCTIONS ****************************************************************/
47
48 #ifndef LIBCAPTIVE
49
50 VOID STDCALL
51 IopCloseFile(PVOID ObjectBody,
52              ULONG HandleCount)
53 {
54    PFILE_OBJECT FileObject = (PFILE_OBJECT)ObjectBody;
55    PIRP Irp;
56    PIO_STACK_LOCATION StackPtr;
57    NTSTATUS Status;
58    
59    DPRINT("IopCloseFile()\n");
60    
61    if (HandleCount > 0 || FileObject->DeviceObject == NULL)
62      {
63         return;
64      }
65    
66    ObReferenceObjectByPointer(FileObject,
67                               STANDARD_RIGHTS_REQUIRED,
68                               IoFileObjectType,
69                               UserMode);
70    KeResetEvent( &FileObject->Event );
71   
72    Irp = IoBuildSynchronousFsdRequest(IRP_MJ_CLEANUP,
73                                       FileObject->DeviceObject,
74                                       NULL,
75                                       0,
76                                       NULL,
77                                       NULL,
78                                       NULL);
79    StackPtr = IoGetNextIrpStackLocation(Irp);
80    StackPtr->FileObject = FileObject;
81    
82    Status = IoCallDriver(FileObject->DeviceObject, Irp);
83    if (Status == STATUS_PENDING)
84    {
85       KeWaitForSingleObject(&FileObject->Event, Executive, KernelMode, FALSE, NULL);
86    }
87 }
88
89 VOID STDCALL
90 IopDeleteFile(PVOID ObjectBody)
91 {
92    PFILE_OBJECT FileObject = (PFILE_OBJECT)ObjectBody;
93    PIRP Irp;
94    PIO_STACK_LOCATION StackPtr;
95    NTSTATUS Status;
96    
97    DPRINT("IopDeleteFile()\n");
98
99    if (FileObject->DeviceObject)
100    {
101      ObReferenceObjectByPointer(ObjectBody,
102                                 STANDARD_RIGHTS_REQUIRED,
103                                 IoFileObjectType,
104                                 UserMode);
105    
106      KeResetEvent( &FileObject->Event );
107      Irp = IoBuildSynchronousFsdRequest(IRP_MJ_CLOSE,
108                                         FileObject->DeviceObject,
109                                         NULL,
110                                         0,
111                                         NULL,
112                                         NULL,
113                                         NULL);
114      StackPtr = IoGetNextIrpStackLocation(Irp);
115      StackPtr->FileObject = FileObject;
116    
117      Status = IoCallDriver(FileObject->DeviceObject, Irp);
118      if (Status == STATUS_PENDING)
119      {
120         KeWaitForSingleObject(&FileObject->Event, Executive, KernelMode, FALSE, NULL);
121      }
122    }
123    
124    if (FileObject->FileName.Buffer != NULL)
125      {
126         ExFreePool(FileObject->FileName.Buffer);
127         FileObject->FileName.Buffer = 0;
128      }
129 }
130
131 #endif /* LIBCAPTIVE */
132
133 VOID IoInit (VOID)
134 {
135   OBJECT_ATTRIBUTES ObjectAttributes;
136   UNICODE_STRING DirName;
137 #ifndef LIBCAPTIVE
138   UNICODE_STRING LinkName;
139 #endif /* LIBCAPTIVE */
140   HANDLE Handle;
141
142 #ifndef LIBCAPTIVE
143   IopInitDriverImplementation();
144 #endif /* LIBCAPTIVE */
145   
146   /*
147    * Register iomgr types: DeviceObjectType
148    */
149   IoDeviceObjectType = ExAllocatePool (NonPagedPool, 
150                                        sizeof (OBJECT_TYPE));
151   
152   IoDeviceObjectType->Tag = TAG_DEVICE_TYPE;
153   IoDeviceObjectType->TotalObjects = 0;
154   IoDeviceObjectType->TotalHandles = 0;
155   IoDeviceObjectType->MaxObjects = ULONG_MAX;
156   IoDeviceObjectType->MaxHandles = ULONG_MAX;
157   IoDeviceObjectType->PagedPoolCharge = 0;
158   IoDeviceObjectType->NonpagedPoolCharge = sizeof (DEVICE_OBJECT);
159   IoDeviceObjectType->Mapping = &IopFileMapping;
160   IoDeviceObjectType->Dump = NULL;
161   IoDeviceObjectType->Open = NULL;
162   IoDeviceObjectType->Close = NULL;
163   IoDeviceObjectType->Delete = NULL;
164   IoDeviceObjectType->Parse = NULL;
165   IoDeviceObjectType->Security = NULL;
166   IoDeviceObjectType->QueryName = NULL;
167   IoDeviceObjectType->OkayToClose = NULL;
168   IoDeviceObjectType->Create = IopCreateDevice;
169   IoDeviceObjectType->DuplicationNotify = NULL;
170   
171   RtlInitUnicodeStringFromLiteral(&IoDeviceObjectType->TypeName, REACTOS_UCS2(L"Device"));
172
173   /*
174    * Register iomgr types: FileObjectType
175    * (alias DriverObjectType)
176    */
177   IoFileObjectType = ExAllocatePool (NonPagedPool, sizeof (OBJECT_TYPE));
178   
179   IoFileObjectType->Tag = TAG_FILE_TYPE;
180   IoFileObjectType->TotalObjects = 0;
181   IoFileObjectType->TotalHandles = 0;
182   IoFileObjectType->MaxObjects = ULONG_MAX;
183   IoFileObjectType->MaxHandles = ULONG_MAX;
184   IoFileObjectType->PagedPoolCharge = 0;
185   IoFileObjectType->NonpagedPoolCharge = sizeof(FILE_OBJECT);
186   IoFileObjectType->Mapping = &IopFileMapping;
187   IoFileObjectType->Dump = NULL;
188   IoFileObjectType->Open = NULL;
189 #ifndef LIBCAPTIVE
190   IoFileObjectType->Close = IopCloseFile;
191   IoFileObjectType->Delete = IopDeleteFile;
192 #else /* !LIBCAPTIVE */
193   IoFileObjectType->Close = NULL;
194   IoFileObjectType->Delete = NULL;
195 #endif /* !LIBCAPTIVE */
196   IoFileObjectType->Parse = NULL;
197   IoFileObjectType->Security = NULL;
198   IoFileObjectType->QueryName = NULL;
199   IoFileObjectType->OkayToClose = NULL;
200   IoFileObjectType->Create = IopCreateFile;
201   IoFileObjectType->DuplicationNotify = NULL;
202   
203   RtlInitUnicodeStringFromLiteral(&IoFileObjectType->TypeName, REACTOS_UCS2(L"File"));
204
205   /*
206    * Create the '\Driver' object directory
207    */
208   RtlInitUnicodeStringFromLiteral(&DirName, REACTOS_UCS2(L"\\Driver"));
209   InitializeObjectAttributes(&ObjectAttributes,
210                              &DirName,
211                              0,
212                              NULL,
213                              NULL);
214   NtCreateDirectoryObject(&Handle,
215                           0,
216                           &ObjectAttributes);
217
218   /*
219    * Create the '\FileSystem' object directory
220    */
221   RtlInitUnicodeStringFromLiteral(&DirName,
222                        REACTOS_UCS2(L"\\FileSystem"));
223   InitializeObjectAttributes(&ObjectAttributes,
224                              &DirName,
225                              0,
226                              NULL,
227                              NULL);
228   NtCreateDirectoryObject(&Handle,
229                           0,
230                           &ObjectAttributes);
231
232   /*
233    * Create the '\Device' directory
234    */
235   RtlInitUnicodeStringFromLiteral(&DirName,
236                        REACTOS_UCS2(L"\\Device"));
237   InitializeObjectAttributes(&ObjectAttributes,
238                              &DirName,
239                              0,
240                              NULL,
241                              NULL);
242   ZwCreateDirectoryObject(&Handle,
243                           0,
244                           &ObjectAttributes);
245
246 #ifndef LIBCAPTIVE
247   /*
248    * Create the '\??' directory
249    */
250   RtlInitUnicodeStringFromLiteral(&DirName,
251                        REACTOS_UCS2(L"\\??"));
252   InitializeObjectAttributes(&ObjectAttributes,
253                              &DirName,
254                              0,
255                              NULL,
256                              NULL);
257   ZwCreateDirectoryObject(&Handle,
258                           0,
259                           &ObjectAttributes);
260
261   /*
262    * Create the '\ArcName' directory
263    */
264   RtlInitUnicodeStringFromLiteral(&DirName,
265                        REACTOS_UCS2(L"\\ArcName"));
266   InitializeObjectAttributes(&ObjectAttributes,
267                              &DirName,
268                              0,
269                              NULL,
270                              NULL);
271   ZwCreateDirectoryObject(&Handle,
272                           0,
273                           &ObjectAttributes);
274
275   /*
276    * Initialize remaining subsubsystem
277    */
278   IoInitCancelHandling();
279   IoInitSymbolicLinkImplementation();
280 #endif /* LIBCAPTIVE */
281   IoInitFileSystemImplementation();
282   IoInitVpbImplementation();
283   IoInitShutdownNotification();
284
285   /*
286    * Create link from '\DosDevices' to '\??' directory
287    */
288 #ifndef LIBCAPTIVE
289   RtlInitUnicodeStringFromLiteral(&LinkName,
290                        L"\\DosDevices");
291   RtlInitUnicodeStringFromLiteral(&DirName,
292                        L"\\??");
293   IoCreateSymbolicLink(&LinkName,
294                        &DirName);
295
296   /*
297    * Initialize PnP manager
298    */
299   PnpInit();
300 #endif /* LIBCAPTIVE */
301 }
302
303 #ifndef LIBCAPTIVE
304
305 PGENERIC_MAPPING STDCALL
306 IoGetFileObjectGenericMapping(VOID)
307 {
308   return(&IopFileMapping);
309 }
310
311 #endif /* LIBCAPTIVE */
312
313 /* EOF */