[CDFS_NEW]
[reactos.git] / drivers / filesystems / cdfs_new / cdinit.c
1 /*++
2
3 Copyright (c) 1989-2000 Microsoft Corporation
4
5 Module Name:
6
7 CdInit.c
8
9 Abstract:
10
11 This module implements the DRIVER_INITIALIZATION routine for Cdfs
12
13
14 --*/
15
16 #include "cdprocs.h"
17
18 //
19 // The Bug check file id for this module
20 //
21
22 #define BugCheckFileId (CDFS_BUG_CHECK_CDINIT)
23
24 NTSTATUS
25 NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
26 DriverEntry(
27 IN PDRIVER_OBJECT DriverObject,
28 IN PUNICODE_STRING RegistryPath
29 );
30
31 VOID
32 NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
33 CdUnload(
34 IN PDRIVER_OBJECT DriverObject
35 );
36
37 NTSTATUS
38 CdInitializeGlobalData (
39 IN PDRIVER_OBJECT DriverObject,
40 IN PDEVICE_OBJECT FileSystemDeviceObject
41 #ifdef __REACTOS__
42 ,
43 IN PDEVICE_OBJECT HddFileSystemDeviceObject
44 #endif
45 );
46
47 NTSTATUS
48 NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
49 CdShutdown (
50 IN PDEVICE_OBJECT DeviceObject,
51 IN PIRP Irp
52 );
53
54 #ifdef ALLOC_PRAGMA
55 #pragma alloc_text(INIT, DriverEntry)
56 #pragma alloc_text(PAGE, CdUnload)
57 #pragma alloc_text(PAGE, CdShutdown)
58 #pragma alloc_text(INIT, CdInitializeGlobalData)
59 #endif
60
61 \f
62 //
63 // Local support routine
64 //
65
66 NTSTATUS
67 NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
68 DriverEntry(
69 IN PDRIVER_OBJECT DriverObject,
70 IN PUNICODE_STRING RegistryPath
71 )
72
73 /*++
74
75 Routine Description:
76
77 This is the initialization routine for the Cdrom file system
78 device driver. This routine creates the device object for the FileSystem
79 device and performs all other driver initialization.
80
81 Arguments:
82
83 DriverObject - Pointer to driver object created by the system.
84
85 Return Value:
86
87 NTSTATUS - The function value is the final status from the initialization
88 operation.
89
90 --*/
91
92 {
93 NTSTATUS Status;
94 UNICODE_STRING UnicodeString;
95 PDEVICE_OBJECT CdfsFileSystemDeviceObject;
96 #ifdef __REACTOS__
97 PDEVICE_OBJECT HddFileSystemDeviceObject;
98 #endif
99
100 //
101 // Create the device object.
102 //
103
104 RtlInitUnicodeString( &UnicodeString, L"\\Cdfs" );
105
106 Status = IoCreateDevice( DriverObject,
107 0,
108 &UnicodeString,
109 FILE_DEVICE_CD_ROM_FILE_SYSTEM,
110 0,
111 FALSE,
112 &CdfsFileSystemDeviceObject );
113
114 if (!NT_SUCCESS( Status )) {
115 return Status;
116 }
117
118 #ifdef __REACTOS__
119 //
120 // Create the HDD device object.
121 //
122
123 RtlInitUnicodeString( &UnicodeString, L"\\CdfsHdd" );
124
125 Status = IoCreateDevice( DriverObject,
126 0,
127 &UnicodeString,
128 FILE_DEVICE_DISK_FILE_SYSTEM,
129 0,
130 FALSE,
131 &HddFileSystemDeviceObject );
132
133 if (!NT_SUCCESS( Status )) {
134 IoDeleteDevice (CdfsFileSystemDeviceObject);
135 return Status;
136 }
137 #endif
138 DriverObject->DriverUnload = CdUnload;
139 //
140 // Note that because of the way data caching is done, we set neither
141 // the Direct I/O or Buffered I/O bit in DeviceObject->Flags. If
142 // data is not in the cache, or the request is not buffered, we may,
143 // set up for Direct I/O by hand.
144 //
145
146 //
147 // Initialize the driver object with this driver's entry points.
148 //
149 // NOTE - Each entry in the dispatch table must have an entry in
150 // the Fsp/Fsd dispatch switch statements.
151 //
152
153 DriverObject->MajorFunction[IRP_MJ_CREATE] =
154 DriverObject->MajorFunction[IRP_MJ_CLOSE] =
155 DriverObject->MajorFunction[IRP_MJ_READ] =
156 DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] =
157 DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] =
158 DriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION]=
159 DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] =
160 DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] =
161 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] =
162 DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL] =
163 DriverObject->MajorFunction[IRP_MJ_CLEANUP] =
164 DriverObject->MajorFunction[IRP_MJ_PNP] = (PDRIVER_DISPATCH) CdFsdDispatch;
165 DriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = CdShutdown;
166
167 DriverObject->FastIoDispatch = &CdFastIoDispatch;
168
169 Status = IoRegisterShutdownNotification (CdfsFileSystemDeviceObject);
170 if (!NT_SUCCESS (Status)) {
171 IoDeleteDevice (CdfsFileSystemDeviceObject);
172 #ifdef __REACTOS__
173 IoDeleteDevice (HddFileSystemDeviceObject);
174 #endif
175 return Status;
176 }
177
178 //
179 // Initialize the global data structures
180 //
181
182 #ifndef __REACTOS__
183 Status = CdInitializeGlobalData( DriverObject, CdfsFileSystemDeviceObject );
184 #else
185 Status = CdInitializeGlobalData( DriverObject, CdfsFileSystemDeviceObject, HddFileSystemDeviceObject );
186 #endif
187 if (!NT_SUCCESS (Status)) {
188 IoDeleteDevice (CdfsFileSystemDeviceObject);
189 #ifdef __REACTOS__
190 IoDeleteDevice (HddFileSystemDeviceObject);
191 #endif
192 return Status;
193 }
194
195 //
196 // Register the file system as low priority with the I/O system. This will cause
197 // CDFS to receive mount requests after a) other filesystems currently registered
198 // and b) other normal priority filesystems that may be registered later.
199 //
200
201 CdfsFileSystemDeviceObject->Flags |= DO_LOW_PRIORITY_FILESYSTEM;
202 #ifdef __REACTOS__
203 HddFileSystemDeviceObject->Flags |= DO_LOW_PRIORITY_FILESYSTEM;
204 #endif
205
206 IoRegisterFileSystem( CdfsFileSystemDeviceObject );
207 ObReferenceObject (CdfsFileSystemDeviceObject);
208 #ifdef __REACTOS__
209 IoRegisterFileSystem( HddFileSystemDeviceObject );
210 ObReferenceObject (HddFileSystemDeviceObject);
211 #endif
212
213 //
214 // And return to our caller
215 //
216
217 return( STATUS_SUCCESS );
218 }
219
220 NTSTATUS
221 NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
222 CdShutdown (
223 IN PDEVICE_OBJECT DeviceObject,
224 IN PIRP Irp
225 )
226 /*++
227
228 Routine Description:
229
230 This routine is the shutdown handler for CDFS.
231
232 Arguments:
233
234 DeviceObject - Supplies the registered device object for CDFS.
235 Irp - Shutdown IRP
236
237
238 Return Value:
239
240 None.
241
242 --*/
243 {
244 IoUnregisterFileSystem (DeviceObject);
245 IoDeleteDevice (CdData.FileSystemDeviceObject);
246 #ifdef __REACTOS__
247 IoDeleteDevice (CdData.HddFileSystemDeviceObject);
248 #endif
249
250 CdCompleteRequest( NULL, Irp, STATUS_SUCCESS );
251 return STATUS_SUCCESS;
252 }
253
254
255 VOID
256 NTAPI /* ReactOS Change: GCC Does not support STDCALL by default */
257 CdUnload(
258 IN PDRIVER_OBJECT DriverObject
259 )
260 /*++
261
262 Routine Description:
263
264 This routine unload routine for CDFS.
265
266 Arguments:
267
268 DriverObject - Supplies the driver object for CDFS.
269
270 Return Value:
271
272 None.
273
274 --*/
275 {
276 PIRP_CONTEXT IrpContext;
277
278 //
279 // Free any IRP contexts
280 //
281 while (1) {
282 IrpContext = (PIRP_CONTEXT) PopEntryList( &CdData.IrpContextList) ;
283 if (IrpContext == NULL) {
284 break;
285 }
286 CdFreePool(&IrpContext);
287 }
288
289 IoFreeWorkItem (CdData.CloseItem);
290 ExDeleteResourceLite( &CdData.DataResource );
291 ObDereferenceObject (CdData.FileSystemDeviceObject);
292 #ifdef __REACTOS__
293 ObDereferenceObject (CdData.HddFileSystemDeviceObject);
294 #endif
295 }
296 \f
297 //
298 // Local support routine
299 //
300
301 NTSTATUS
302 CdInitializeGlobalData (
303 IN PDRIVER_OBJECT DriverObject,
304 IN PDEVICE_OBJECT FileSystemDeviceObject
305 #ifdef __REACTOS__
306 ,
307 IN PDEVICE_OBJECT HddFileSystemDeviceObject
308 #endif
309 )
310
311 /*++
312
313 Routine Description:
314
315 This routine initializes the global cdfs data structures.
316
317 Arguments:
318
319 DriverObject - Supplies the driver object for CDFS.
320
321 FileSystemDeviceObject - Supplies the device object for CDFS.
322
323 Return Value:
324
325 None.
326
327 --*/
328
329 {
330 //
331 // Start by initializing the FastIoDispatch Table.
332 //
333
334 RtlZeroMemory( &CdFastIoDispatch, sizeof( FAST_IO_DISPATCH ));
335
336 CdFastIoDispatch.SizeOfFastIoDispatch = sizeof(FAST_IO_DISPATCH);
337 CdFastIoDispatch.FastIoCheckIfPossible = CdFastIoCheckIfPossible; // CheckForFastIo
338 CdFastIoDispatch.FastIoRead = FsRtlCopyRead; // Read
339 CdFastIoDispatch.FastIoQueryBasicInfo = CdFastQueryBasicInfo; // QueryBasicInfo
340 CdFastIoDispatch.FastIoQueryStandardInfo = CdFastQueryStdInfo; // QueryStandardInfo
341 CdFastIoDispatch.FastIoLock = CdFastLock; // Lock
342 CdFastIoDispatch.FastIoUnlockSingle = CdFastUnlockSingle; // UnlockSingle
343 CdFastIoDispatch.FastIoUnlockAll = CdFastUnlockAll; // UnlockAll
344 CdFastIoDispatch.FastIoUnlockAllByKey = CdFastUnlockAllByKey; // UnlockAllByKey
345 CdFastIoDispatch.AcquireFileForNtCreateSection = CdAcquireForCreateSection;
346 CdFastIoDispatch.ReleaseFileForNtCreateSection = CdReleaseForCreateSection;
347 CdFastIoDispatch.FastIoQueryNetworkOpenInfo = CdFastQueryNetworkInfo; // QueryNetworkInfo
348
349 CdFastIoDispatch.MdlRead = FsRtlMdlReadDev;
350 CdFastIoDispatch.MdlReadComplete = FsRtlMdlReadCompleteDev;
351 CdFastIoDispatch.PrepareMdlWrite = FsRtlPrepareMdlWriteDev;
352 CdFastIoDispatch.MdlWriteComplete = FsRtlMdlWriteCompleteDev;
353
354 //
355 // Initialize the CdData structure.
356 //
357
358 RtlZeroMemory( &CdData, sizeof( CD_DATA ));
359
360 CdData.NodeTypeCode = CDFS_NTC_DATA_HEADER;
361 CdData.NodeByteSize = sizeof( CD_DATA );
362
363 CdData.DriverObject = DriverObject;
364 CdData.FileSystemDeviceObject = FileSystemDeviceObject;
365 #ifdef __REACTOS__
366 CdData.HddFileSystemDeviceObject = HddFileSystemDeviceObject;
367 #endif
368
369 InitializeListHead( &CdData.VcbQueue );
370
371 ExInitializeResourceLite( &CdData.DataResource );
372
373 //
374 // Initialize the cache manager callback routines
375 //
376
377 CdData.CacheManagerCallbacks.AcquireForLazyWrite = (PVOID)&CdAcquireForCache;/* ReactOS Change: GCC "assignment from incompatible pointer type" */
378 CdData.CacheManagerCallbacks.ReleaseFromLazyWrite = (PVOID)&CdReleaseFromCache;/* ReactOS Change: GCC "assignment from incompatible pointer type" */
379 CdData.CacheManagerCallbacks.AcquireForReadAhead = (PVOID)&CdAcquireForCache;/* ReactOS Change: GCC "assignment from incompatible pointer type" */
380 CdData.CacheManagerCallbacks.ReleaseFromReadAhead = (PVOID)&CdReleaseFromCache;/* ReactOS Change: GCC "assignment from incompatible pointer type" */
381
382 CdData.CacheManagerVolumeCallbacks.AcquireForLazyWrite = &CdNoopAcquire;
383 CdData.CacheManagerVolumeCallbacks.ReleaseFromLazyWrite = &CdNoopRelease;
384 CdData.CacheManagerVolumeCallbacks.AcquireForReadAhead = &CdNoopAcquire;
385 CdData.CacheManagerVolumeCallbacks.ReleaseFromReadAhead = &CdNoopRelease;
386
387 //
388 // Initialize the lock mutex and the async and delay close queues.
389 //
390
391 ExInitializeFastMutex( &CdData.CdDataMutex );
392 InitializeListHead( &CdData.AsyncCloseQueue );
393 InitializeListHead( &CdData.DelayedCloseQueue );
394
395 CdData.CloseItem = IoAllocateWorkItem (FileSystemDeviceObject);
396 if (CdData.CloseItem == NULL) {
397
398 ExDeleteResourceLite( &CdData.DataResource );
399 return STATUS_INSUFFICIENT_RESOURCES;
400 }
401 //
402 // Do the initialization based on the system size.
403 //
404
405 switch (MmQuerySystemSize()) {
406
407 case MmSmallSystem:
408
409 CdData.IrpContextMaxDepth = 4;
410 CdData.MaxDelayedCloseCount = 8;
411 CdData.MinDelayedCloseCount = 2;
412 break;
413
414 case MmMediumSystem:
415
416 CdData.IrpContextMaxDepth = 8;
417 CdData.MaxDelayedCloseCount = 24;
418 CdData.MinDelayedCloseCount = 6;
419 break;
420
421 case MmLargeSystem:
422
423 CdData.IrpContextMaxDepth = 32;
424 CdData.MaxDelayedCloseCount = 72;
425 CdData.MinDelayedCloseCount = 18;
426 break;
427 }
428 return STATUS_SUCCESS;
429 }
430