2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS File System Recognizer
4 * FILE: drivers/filesystems/fs_rec/fs_rec.c
5 * PURPOSE: Main Driver Entrypoint and FS Registration
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
10 /* INCLUDES *****************************************************************/
17 PKEVENT FsRecLoadSync
;
19 /* FUNCTIONS ****************************************************************/
23 FsRecLoadFileSystem(IN PDEVICE_OBJECT DeviceObject
,
24 IN PWCHAR DriverServiceName
)
26 UNICODE_STRING DriverName
;
27 PDEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
28 NTSTATUS Status
= STATUS_IMAGE_ALREADY_LOADED
;
31 /* Make sure we haven't already been called */
32 if (DeviceExtension
->State
!= Loaded
)
34 /* Acquire the load lock */
35 KeWaitForSingleObject(FsRecLoadSync
,
40 KeEnterCriticalRegion();
42 /* Make sure we're active */
43 if (DeviceExtension
->State
== Pending
)
45 /* Load the FS driver */
46 RtlInitUnicodeString(&DriverName
, DriverServiceName
);
47 Status
= ZwLoadDriver(&DriverName
);
49 /* Loop all the linked recognizer objects */
50 while (DeviceExtension
->State
!= Unloading
)
52 /* Set them to the unload state */
53 DeviceExtension
->State
= Unloading
;
55 /* Go to the next one */
56 DeviceObject
= DeviceExtension
->Alternate
;
57 DeviceExtension
= DeviceObject
->DeviceExtension
;
61 /* Make sure that we haven't already loaded the FS */
62 if (DeviceExtension
->State
!= Loaded
)
64 /* Unregiser us, and set us as loaded */
65 IoUnregisterFileSystem(DeviceObject
);
66 DeviceExtension
->State
= Loaded
;
69 /* Release the lock */
70 KeSetEvent(FsRecLoadSync
, 0, FALSE
);
71 KeLeaveCriticalRegion();
78 DRIVER_DISPATCH FsRecCreate
;
81 FsRecCreate(IN PDEVICE_OBJECT DeviceObject
,
84 PIO_STACK_LOCATION IoStack
= IoGetCurrentIrpStackLocation(Irp
);
88 UNREFERENCED_PARAMETER(DeviceObject
);
90 /* Make sure we have a file name */
91 if (IoStack
->FileObject
->FileName
.Length
)
93 /* Fail the request */
94 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
99 Status
= STATUS_SUCCESS
;
102 /* Complete the IRP */
103 Irp
->IoStatus
.Status
= Status
;
104 Irp
->IoStatus
.Information
= FILE_OPENED
;
105 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
109 DRIVER_DISPATCH FsRecClose
;
112 FsRecClose(IN PDEVICE_OBJECT DeviceObject
,
117 UNREFERENCED_PARAMETER(DeviceObject
);
119 /* Just complete the IRP and return success */
120 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
121 return STATUS_SUCCESS
;
124 DRIVER_DISPATCH FsRecFsControl
;
127 FsRecFsControl(IN PDEVICE_OBJECT DeviceObject
,
130 PDEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
134 /* Check the file system type */
135 switch (DeviceExtension
->FsType
)
139 /* Send FAT command */
140 Status
= FsRecVfatFsControl(DeviceObject
, Irp
);
145 /* Send NTFS command */
146 Status
= FsRecNtfsFsControl(DeviceObject
, Irp
);
151 /* Send CDFS command */
152 Status
= FsRecCdfsFsControl(DeviceObject
, Irp
);
157 /* Send UDFS command */
158 Status
= FsRecUdfsFsControl(DeviceObject
, Irp
);
163 /* Send EXT2 command */
164 Status
= FsRecExt2FsControl(DeviceObject
, Irp
);
169 /* Unrecognized FS */
170 Status
= STATUS_INVALID_DEVICE_REQUEST
;
173 /* Complete the IRP */
174 Irp
->IoStatus
.Status
= Status
;
175 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
179 DRIVER_UNLOAD FsRecUnload
;
182 FsRecUnload(IN PDRIVER_OBJECT DriverObject
)
186 /* Loop all driver device objects */
187 while (DriverObject
->DeviceObject
)
189 /* Delete this device */
190 IoDeleteDevice(DriverObject
->DeviceObject
);
194 ExFreePool(FsRecLoadSync
);
199 FsRecRegisterFs(IN PDRIVER_OBJECT DriverObject
,
200 IN PDEVICE_OBJECT ParentObject OPTIONAL
,
201 OUT PDEVICE_OBJECT
*NewDeviceObject OPTIONAL
,
203 IN PCWSTR RecognizerName
,
205 IN DEVICE_TYPE DeviceType
)
207 OBJECT_ATTRIBUTES ObjectAttributes
;
208 IO_STATUS_BLOCK IoStatus
;
209 PDEVICE_EXTENSION DeviceExtension
;
210 UNICODE_STRING DeviceName
;
211 PDEVICE_OBJECT DeviceObject
;
216 if (NewDeviceObject
) *NewDeviceObject
= NULL
;
218 /* Setup the attributes */
219 RtlInitUnicodeString(&DeviceName
, FsName
);
220 InitializeObjectAttributes(&ObjectAttributes
,
222 OBJ_CASE_INSENSITIVE
,
226 /* Open the device */
227 Status
= ZwCreateFile(&FileHandle
,
233 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
238 if (NT_SUCCESS(Status
))
240 /* We suceeded, close the handle */
243 else if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
245 /* We failed with anything else then what we want to fail with */
246 Status
= STATUS_SUCCESS
;
249 /* If we succeeded, there's no point in trying this again */
250 if (NT_SUCCESS(Status
)) return STATUS_IMAGE_ALREADY_LOADED
;
252 /* Create recognizer device object */
253 RtlInitUnicodeString(&DeviceName
, RecognizerName
);
254 Status
= IoCreateDevice(DriverObject
,
255 sizeof(DEVICE_EXTENSION
),
261 if (NT_SUCCESS(Status
))
263 /* Get the device extension and set it up */
264 DeviceExtension
= DeviceObject
->DeviceExtension
;
265 DeviceExtension
->FsType
= FsType
;
266 DeviceExtension
->State
= Pending
;
268 /* Do we have a parent? */
272 DeviceExtension
->Alternate
=
273 ((PDEVICE_EXTENSION
)ParentObject
->DeviceExtension
)->Alternate
;
274 ((PDEVICE_EXTENSION
)ParentObject
->DeviceExtension
)->Alternate
=
279 /* Otherwise, we're the only one */
280 DeviceExtension
->Alternate
= DeviceObject
;
283 /* Return the DO if needed */
284 if (NewDeviceObject
) *NewDeviceObject
= DeviceObject
;
286 /* Register the file system */
287 IoRegisterFileSystem(DeviceObject
);
296 DriverEntry(IN PDRIVER_OBJECT DriverObject
,
297 IN PUNICODE_STRING RegistryPath
)
299 ULONG DeviceCount
= 0;
301 PDEVICE_OBJECT UdfsObject
;
304 UNREFERENCED_PARAMETER(RegistryPath
);
306 /* Page the entire driver */
307 MmPageEntireDriver(DriverEntry
);
309 /* Allocate the lock */
310 FsRecLoadSync
= ExAllocatePoolWithTag(NonPagedPool
,
313 if (!FsRecLoadSync
) return STATUS_INSUFFICIENT_RESOURCES
;
316 KeInitializeEvent(FsRecLoadSync
, SynchronizationEvent
, TRUE
);
318 /* Setup the major functions */
319 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = FsRecCreate
;
320 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = FsRecClose
;
321 DriverObject
->MajorFunction
[IRP_MJ_CLEANUP
] = FsRecClose
;
322 DriverObject
->MajorFunction
[IRP_MJ_FILE_SYSTEM_CONTROL
] = FsRecFsControl
;
323 DriverObject
->DriverUnload
= FsRecUnload
;
326 Status
= FsRecRegisterFs(DriverObject
,
330 L
"\\FileSystem\\CdfsRecognizer",
332 FILE_DEVICE_CD_ROM_FILE_SYSTEM
);
333 if (NT_SUCCESS(Status
)) DeviceCount
++;
335 /* Register UDFS for CDs */
336 Status
= FsRecRegisterFs(DriverObject
,
340 L
"\\FileSystem\\UdfsCdRomRecognizer",
342 FILE_DEVICE_CD_ROM_FILE_SYSTEM
);
343 if (NT_SUCCESS(Status
)) DeviceCount
++;
345 /* Register UDFS for HDDs */
346 Status
= FsRecRegisterFs(DriverObject
,
350 L
"\\FileSystem\\UdfsDiskRecognizer",
352 FILE_DEVICE_DISK_FILE_SYSTEM
);
353 if (NT_SUCCESS(Status
)) DeviceCount
++;
356 Status
= FsRecRegisterFs(DriverObject
,
360 L
"\\FileSystem\\FatRecognizer",
362 FILE_DEVICE_DISK_FILE_SYSTEM
);
363 if (NT_SUCCESS(Status
)) DeviceCount
++;
366 Status
= FsRecRegisterFs(DriverObject
,
370 L
"\\FileSystem\\NtfsRecognizer",
372 FILE_DEVICE_DISK_FILE_SYSTEM
);
373 if (NT_SUCCESS(Status
)) DeviceCount
++;
376 Status
= FsRecRegisterFs(DriverObject
,
380 L
"\\FileSystem\\Ext2Recognizer",
382 FILE_DEVICE_DISK_FILE_SYSTEM
);
383 if (NT_SUCCESS(Status
)) DeviceCount
++;
385 /* Return appropriate Status */
386 return (DeviceCount
> 0) ? STATUS_SUCCESS
: STATUS_IMAGE_ALREADY_LOADED
;