bootstrap
[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)
62      {
63         return;
64      }
65    
66    ObReferenceObjectByPointer(FileObject,
67                               STANDARD_RIGHTS_REQUIRED,
68                               IoFileObjectType,
69                               UserMode);
70    
71    Irp = IoBuildSynchronousFsdRequest(IRP_MJ_CLEANUP,
72                                       FileObject->DeviceObject,
73                                       NULL,
74                                       0,
75                                       NULL,
76                                       NULL,
77                                       NULL);
78    StackPtr = IoGetNextIrpStackLocation(Irp);
79    StackPtr->FileObject = FileObject;
80    
81    Status = IoCallDriver(FileObject->DeviceObject, Irp);
82 }
83
84 VOID STDCALL
85 IopDeleteFile(PVOID ObjectBody)
86 {
87    PFILE_OBJECT FileObject = (PFILE_OBJECT)ObjectBody;
88    PIRP Irp;
89    PIO_STACK_LOCATION StackPtr;
90    NTSTATUS Status;
91    
92    DPRINT("IopDeleteFile()\n");
93    
94    ObReferenceObjectByPointer(ObjectBody,
95                               STANDARD_RIGHTS_REQUIRED,
96                               IoFileObjectType,
97                               UserMode);
98    
99    Irp = IoBuildSynchronousFsdRequest(IRP_MJ_CLOSE,
100                                       FileObject->DeviceObject,
101                                       NULL,
102                                       0,
103                                       NULL,
104                                       NULL,
105                                       NULL);
106    StackPtr = IoGetNextIrpStackLocation(Irp);
107    StackPtr->FileObject = FileObject;
108    
109    Status = IoCallDriver(FileObject->DeviceObject, Irp);
110    
111    if (FileObject->FileName.Buffer != NULL)
112      {
113         ExFreePool(FileObject->FileName.Buffer);
114         FileObject->FileName.Buffer = 0;
115      }
116 }
117
118 #endif /* LIBCAPTIVE */
119
120 VOID IoInit (VOID)
121 {
122 #ifndef LIBCAPTIVE
123   OBJECT_ATTRIBUTES ObjectAttributes;
124   UNICODE_STRING DirName;
125   UNICODE_STRING LinkName;
126   HANDLE Handle;
127
128   IopInitDriverImplementation();
129 #endif /* LIBCAPTIVE */
130   
131   /*
132    * Register iomgr types: DeviceObjectType
133    */
134   IoDeviceObjectType = ExAllocatePool (NonPagedPool, 
135                                        sizeof (OBJECT_TYPE));
136   
137   IoDeviceObjectType->Tag = TAG_DEVICE_TYPE;
138   IoDeviceObjectType->TotalObjects = 0;
139   IoDeviceObjectType->TotalHandles = 0;
140   IoDeviceObjectType->MaxObjects = ULONG_MAX;
141   IoDeviceObjectType->MaxHandles = ULONG_MAX;
142   IoDeviceObjectType->PagedPoolCharge = 0;
143   IoDeviceObjectType->NonpagedPoolCharge = sizeof (DEVICE_OBJECT);
144   IoDeviceObjectType->Mapping = &IopFileMapping;
145   IoDeviceObjectType->Dump = NULL;
146   IoDeviceObjectType->Open = NULL;
147   IoDeviceObjectType->Close = NULL;
148   IoDeviceObjectType->Delete = NULL;
149   IoDeviceObjectType->Parse = NULL;
150   IoDeviceObjectType->Security = NULL;
151   IoDeviceObjectType->QueryName = NULL;
152   IoDeviceObjectType->OkayToClose = NULL;
153 #ifndef LIBCAPTIVE
154   IoDeviceObjectType->Create = IopCreateDevice;
155 #else /* !LIBCAPTIVE */
156   IoDeviceObjectType->Create = NULL;
157 #endif /* !LIBCAPTIVE */
158   IoDeviceObjectType->DuplicationNotify = NULL;
159   
160   RtlInitUnicodeStringFromLiteral(&IoDeviceObjectType->TypeName, REACTOS_UCS2(L"Device"));
161
162   /*
163    * Register iomgr types: FileObjectType
164    * (alias DriverObjectType)
165    */
166   IoFileObjectType = ExAllocatePool (NonPagedPool, sizeof (OBJECT_TYPE));
167   
168   IoFileObjectType->Tag = TAG_FILE_TYPE;
169   IoFileObjectType->TotalObjects = 0;
170   IoFileObjectType->TotalHandles = 0;
171   IoFileObjectType->MaxObjects = ULONG_MAX;
172   IoFileObjectType->MaxHandles = ULONG_MAX;
173   IoFileObjectType->PagedPoolCharge = 0;
174   IoFileObjectType->NonpagedPoolCharge = sizeof(FILE_OBJECT);
175   IoFileObjectType->Mapping = &IopFileMapping;
176   IoFileObjectType->Dump = NULL;
177   IoFileObjectType->Open = NULL;
178 #ifndef LIBCAPTIVE
179   IoFileObjectType->Close = IopCloseFile;
180   IoFileObjectType->Delete = IopDeleteFile;
181 #else /* !LIBCAPTIVE */
182   IoFileObjectType->Close = NULL;
183   IoFileObjectType->Delete = NULL;
184 #endif /* !LIBCAPTIVE */
185   IoFileObjectType->Parse = NULL;
186   IoFileObjectType->Security = NULL;
187   IoFileObjectType->QueryName = NULL;
188   IoFileObjectType->OkayToClose = NULL;
189 #ifndef LIBCAPTIVE
190   IoFileObjectType->Create = IopCreateFile;
191 #else /* !LIBCAPTIVE */
192   IoFileObjectType->Create = NULL;
193 #endif /* !LIBCAPTIVE */
194   IoFileObjectType->DuplicationNotify = NULL;
195   
196   RtlInitUnicodeStringFromLiteral(&IoFileObjectType->TypeName, REACTOS_UCS2(L"File"));
197
198 #ifndef LIBCAPTIVE
199   /*
200    * Create the '\Driver' object directory
201    */
202   RtlInitUnicodeStringFromLiteral(&DirName, L"\\Driver");
203   InitializeObjectAttributes(&ObjectAttributes,
204                              &DirName,
205                              0,
206                              NULL,
207                              NULL);
208   NtCreateDirectoryObject(&Handle,
209                           0,
210                           &ObjectAttributes);
211
212   /*
213    * Create the '\FileSystem' object directory
214    */
215   RtlInitUnicodeStringFromLiteral(&DirName,
216                        L"\\FileSystem");
217   InitializeObjectAttributes(&ObjectAttributes,
218                              &DirName,
219                              0,
220                              NULL,
221                              NULL);
222   NtCreateDirectoryObject(&Handle,
223                           0,
224                           &ObjectAttributes);
225
226   /*
227    * Create the '\Device' directory
228    */
229   RtlInitUnicodeStringFromLiteral(&DirName,
230                        L"\\Device");
231   InitializeObjectAttributes(&ObjectAttributes,
232                              &DirName,
233                              0,
234                              NULL,
235                              NULL);
236   ZwCreateDirectoryObject(&Handle,
237                           0,
238                           &ObjectAttributes);
239
240   /*
241    * Create the '\??' directory
242    */
243   RtlInitUnicodeStringFromLiteral(&DirName,
244                        L"\\??");
245   InitializeObjectAttributes(&ObjectAttributes,
246                              &DirName,
247                              0,
248                              NULL,
249                              NULL);
250   ZwCreateDirectoryObject(&Handle,
251                           0,
252                           &ObjectAttributes);
253
254   /*
255    * Create the '\ArcName' directory
256    */
257   RtlInitUnicodeStringFromLiteral(&DirName,
258                        L"\\ArcName");
259   InitializeObjectAttributes(&ObjectAttributes,
260                              &DirName,
261                              0,
262                              NULL,
263                              NULL);
264   ZwCreateDirectoryObject(&Handle,
265                           0,
266                           &ObjectAttributes);
267
268   /*
269    * Initialize remaining subsubsystem
270    */
271   IoInitCancelHandling();
272   IoInitSymbolicLinkImplementation();
273   IoInitFileSystemImplementation();
274 #endif /* LIBCAPTIVE */
275   IoInitVpbImplementation();
276   IoInitShutdownNotification();
277
278   /*
279    * Create link from '\DosDevices' to '\??' directory
280    */
281 #ifndef LIBCAPTIVE
282   RtlInitUnicodeStringFromLiteral(&LinkName,
283                        L"\\DosDevices");
284   RtlInitUnicodeStringFromLiteral(&DirName,
285                        L"\\??");
286   IoCreateSymbolicLink(&LinkName,
287                        &DirName);
288
289   /*
290    * Initialize PnP manager
291    */
292   PnpInit();
293 #endif /* LIBCAPTIVE */
294 }
295
296 #ifndef LIBCAPTIVE
297
298 PGENERIC_MAPPING STDCALL
299 IoGetFileObjectGenericMapping(VOID)
300 {
301   return(&IopFileMapping);
302 }
303
304 #endif /* LIBCAPTIVE */
305
306 /* EOF */