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