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 /* Unregister 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 /* Send BTRFS command */
170 Status
= FsRecBtrfsFsControl(DeviceObject
, Irp
);
173 case FS_TYPE_REISERFS
:
175 /* Send REISERFS command */
176 Status
= FsRecReiserfsFsControl(DeviceObject
, Irp
);
181 /* Send FFS command */
182 Status
= FsRecFfsFsControl(DeviceObject
, Irp
);
187 /* Unrecognized FS */
188 Status
= STATUS_INVALID_DEVICE_REQUEST
;
191 /* Complete the IRP */
192 Irp
->IoStatus
.Status
= Status
;
193 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
197 DRIVER_UNLOAD FsRecUnload
;
200 FsRecUnload(IN PDRIVER_OBJECT DriverObject
)
204 /* Loop all driver device objects */
205 while (DriverObject
->DeviceObject
)
207 /* Delete this device */
208 IoDeleteDevice(DriverObject
->DeviceObject
);
212 ExFreePool(FsRecLoadSync
);
217 FsRecRegisterFs(IN PDRIVER_OBJECT DriverObject
,
218 IN PDEVICE_OBJECT ParentObject OPTIONAL
,
219 OUT PDEVICE_OBJECT
*NewDeviceObject OPTIONAL
,
221 IN PCWSTR RecognizerName
,
223 IN DEVICE_TYPE DeviceType
,
224 IN ULONG AdditionalFlags
)
226 OBJECT_ATTRIBUTES ObjectAttributes
;
227 IO_STATUS_BLOCK IoStatus
;
228 PDEVICE_EXTENSION DeviceExtension
;
229 UNICODE_STRING DeviceName
;
230 PDEVICE_OBJECT DeviceObject
;
235 if (NewDeviceObject
) *NewDeviceObject
= NULL
;
237 /* Setup the attributes */
238 RtlInitUnicodeString(&DeviceName
, FsName
);
239 InitializeObjectAttributes(&ObjectAttributes
,
241 OBJ_CASE_INSENSITIVE
,
245 /* Open the device */
246 Status
= ZwCreateFile(&FileHandle
,
252 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
257 if (NT_SUCCESS(Status
))
259 /* We succeeded, close the handle */
262 else if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
264 /* We failed with anything else then what we want to fail with */
265 Status
= STATUS_SUCCESS
;
268 /* If we succeeded, there's no point in trying this again */
269 if (NT_SUCCESS(Status
)) return STATUS_IMAGE_ALREADY_LOADED
;
271 /* Create recognizer device object */
272 RtlInitUnicodeString(&DeviceName
, RecognizerName
);
273 Status
= IoCreateDevice(DriverObject
,
274 sizeof(DEVICE_EXTENSION
),
280 if (NT_SUCCESS(Status
))
282 /* Set additional flags in the device object */
283 DeviceObject
->Flags
|= AdditionalFlags
;
285 /* Get the device extension and set it up */
286 DeviceExtension
= DeviceObject
->DeviceExtension
;
287 DeviceExtension
->FsType
= FsType
;
288 DeviceExtension
->State
= Pending
;
290 /* Do we have a parent? */
294 DeviceExtension
->Alternate
=
295 ((PDEVICE_EXTENSION
)ParentObject
->DeviceExtension
)->Alternate
;
296 ((PDEVICE_EXTENSION
)ParentObject
->DeviceExtension
)->Alternate
=
301 /* Otherwise, we're the only one */
302 DeviceExtension
->Alternate
= DeviceObject
;
305 /* Return the DO if needed */
306 if (NewDeviceObject
) *NewDeviceObject
= DeviceObject
;
308 /* Register the file system */
309 IoRegisterFileSystem(DeviceObject
);
318 DriverEntry(IN PDRIVER_OBJECT DriverObject
,
319 IN PUNICODE_STRING RegistryPath
)
321 ULONG DeviceCount
= 0;
323 PDEVICE_OBJECT CdfsObject
;
324 PDEVICE_OBJECT UdfsObject
;
327 UNREFERENCED_PARAMETER(RegistryPath
);
329 /* Page the entire driver */
330 MmPageEntireDriver(DriverEntry
);
332 /* Allocate the lock */
333 FsRecLoadSync
= ExAllocatePoolWithTag(NonPagedPool
,
336 if (!FsRecLoadSync
) return STATUS_INSUFFICIENT_RESOURCES
;
339 KeInitializeEvent(FsRecLoadSync
, SynchronizationEvent
, TRUE
);
341 /* Setup the major functions */
342 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = FsRecCreate
;
343 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = FsRecClose
;
344 DriverObject
->MajorFunction
[IRP_MJ_CLEANUP
] = FsRecClose
;
345 DriverObject
->MajorFunction
[IRP_MJ_FILE_SYSTEM_CONTROL
] = FsRecFsControl
;
346 DriverObject
->DriverUnload
= FsRecUnload
;
348 /* Register CDFS for CDs */
349 Status
= FsRecRegisterFs(DriverObject
,
353 L
"\\FileSystem\\CdfsRecognizer",
355 FILE_DEVICE_CD_ROM_FILE_SYSTEM
,
356 DO_LOW_PRIORITY_FILESYSTEM
);
357 if (NT_SUCCESS(Status
)) DeviceCount
++;
359 /* Register CDFS for HDDs */
360 Status
= FsRecRegisterFs(DriverObject
,
364 L
"\\FileSystem\\CdfsHddRecognizer",
366 FILE_DEVICE_DISK_FILE_SYSTEM
,
367 DO_LOW_PRIORITY_FILESYSTEM
);
368 if (NT_SUCCESS(Status
)) DeviceCount
++;
370 /* Register UDFS for CDs */
371 Status
= FsRecRegisterFs(DriverObject
,
375 L
"\\FileSystem\\UdfsCdRomRecognizer",
377 FILE_DEVICE_CD_ROM_FILE_SYSTEM
,
379 if (NT_SUCCESS(Status
)) DeviceCount
++;
381 /* Register UDFS for HDDs */
382 Status
= FsRecRegisterFs(DriverObject
,
386 L
"\\FileSystem\\UdfsDiskRecognizer",
388 FILE_DEVICE_DISK_FILE_SYSTEM
,
390 if (NT_SUCCESS(Status
)) DeviceCount
++;
393 Status
= FsRecRegisterFs(DriverObject
,
397 L
"\\FileSystem\\FatRecognizer",
399 FILE_DEVICE_DISK_FILE_SYSTEM
,
401 if (NT_SUCCESS(Status
)) DeviceCount
++;
404 Status
= FsRecRegisterFs(DriverObject
,
408 L
"\\FileSystem\\NtfsRecognizer",
410 FILE_DEVICE_DISK_FILE_SYSTEM
,
412 if (NT_SUCCESS(Status
)) DeviceCount
++;
415 Status
= FsRecRegisterFs(DriverObject
,
419 L
"\\FileSystem\\Ext2Recognizer",
421 FILE_DEVICE_DISK_FILE_SYSTEM
,
423 if (NT_SUCCESS(Status
)) DeviceCount
++;
426 Status
= FsRecRegisterFs(DriverObject
,
430 L
"\\FileSystem\\BtrfsRecognizer",
432 FILE_DEVICE_DISK_FILE_SYSTEM
,
434 if (NT_SUCCESS(Status
)) DeviceCount
++;
436 /* Register REISERFS */
437 Status
= FsRecRegisterFs(DriverObject
,
441 L
"\\FileSystem\\ReiserfsRecognizer",
443 FILE_DEVICE_DISK_FILE_SYSTEM
,
445 if (NT_SUCCESS(Status
)) DeviceCount
++;
448 Status
= FsRecRegisterFs(DriverObject
,
452 L
"\\FileSystem\\FfsRecognizer",
454 FILE_DEVICE_DISK_FILE_SYSTEM
,
456 if (NT_SUCCESS(Status
)) DeviceCount
++;
458 /* Return appropriate Status */
459 return (DeviceCount
> 0) ? STATUS_SUCCESS
: STATUS_IMAGE_ALREADY_LOADED
;