ReactOS  0.4.15-dev-5487-ge7bbbf0
fs_rec.c
Go to the documentation of this file.
1 /*
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)
7  * Eric Kohl
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include "fs_rec.h"
13 
14 #define NDEBUG
15 #include <debug.h>
16 
18 
19 /* FUNCTIONS ****************************************************************/
20 
22 NTAPI
24  IN PWCHAR DriverServiceName)
25 {
27  PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
28  UNICODE_STRING DriverName;
29 
30  PAGED_CODE();
31 
32  /* Make sure we haven't already been called */
33  if (DeviceExtension->State != Loaded)
34  {
35  /* Acquire the load lock */
38  Executive,
39  KernelMode,
40  FALSE,
41  NULL);
42 
43  /* Make sure we're active */
44  if (DeviceExtension->State == Pending)
45  {
46  /* Load the FS driver */
47  RtlInitUnicodeString(&DriverName, DriverServiceName);
48  Status = ZwLoadDriver(&DriverName);
49 
50  /* Loop all the linked recognizer objects */
51  while (DeviceExtension->State != Unloading)
52  {
53  /* Set them to the unload state */
54  DeviceExtension->State = Unloading;
55 
56  /* Go to the next one */
57  DeviceObject = DeviceExtension->Alternate;
58  DeviceExtension = DeviceObject->DeviceExtension;
59  }
60  }
61 
62  /* Make sure that we haven't already loaded the FS */
63  if (DeviceExtension->State != Loaded)
64  {
65  /* Unregister us, and set us as loaded */
67  DeviceExtension->State = Loaded;
68  }
69 
70  /* Release the lock */
73  }
74 
75  return Status;
76 }
77 
80 NTAPI
82  IN PIRP Irp)
83 {
86  PAGED_CODE();
87 
89 
90  /* Make sure we have a file name */
91  if (IoStack->FileObject->FileName.Length)
92  {
93  /* Fail the request */
95  }
96  else
97  {
98  /* Let it through */
100  }
101 
102  /* Complete the IRP */
103  Irp->IoStatus.Status = Status;
104  Irp->IoStatus.Information = FILE_OPENED;
106  return Status;
107 }
108 
110 NTSTATUS
111 NTAPI
113  IN PIRP Irp)
114 {
115  PAGED_CODE();
116 
118 
119  /* Just complete the IRP and return success */
121  return STATUS_SUCCESS;
122 }
123 
125 NTSTATUS
126 NTAPI
128  IN PIRP Irp)
129 {
130  PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
132  PAGED_CODE();
133 
134  /* Check the file system type */
135  switch (DeviceExtension->FsType)
136  {
137  case FS_TYPE_VFAT:
138 
139  /* Send FAT command */
141  break;
142 
143  case FS_TYPE_NTFS:
144 
145  /* Send NTFS command */
147  break;
148 
149  case FS_TYPE_CDFS:
150 
151  /* Send CDFS command */
153  break;
154 
155  case FS_TYPE_UDFS:
156 
157  /* Send UDFS command */
159  break;
160 
161  case FS_TYPE_EXT2:
162 
163  /* Send EXT2 command */
165  break;
166 
167  case FS_TYPE_BTRFS:
168 
169  /* Send BTRFS command */
171  break;
172 
173  case FS_TYPE_REISERFS:
174 
175  /* Send REISERFS command */
177  break;
178 
179  case FS_TYPE_FFS:
180 
181  /* Send FFS command */
183  break;
184 
185  case FS_TYPE_FATX:
186 
187  /* Send FATX command */
189  break;
190 
191  default:
192 
193  /* Unrecognized FS */
195  }
196 
197  /* Complete the IRP */
198  Irp->IoStatus.Status = Status;
200  return Status;
201 }
202 
203 DRIVER_UNLOAD FsRecUnload;
204 VOID
205 NTAPI
207 {
208  PAGED_CODE();
209 
210  /* Loop all driver device objects */
211  while (DriverObject->DeviceObject)
212  {
213  /* Delete this device */
214  IoDeleteDevice(DriverObject->DeviceObject);
215  }
216 
217  /* Free the lock */
219 }
220 
221 NTSTATUS
222 NTAPI
224  IN PDEVICE_OBJECT ParentObject OPTIONAL,
225  OUT PDEVICE_OBJECT *NewDeviceObject OPTIONAL,
226  IN PCWSTR FsName,
227  IN PCWSTR RecognizerName,
228  IN ULONG FsType,
230  IN ULONG AdditionalFlags)
231 {
234  PDEVICE_EXTENSION DeviceExtension;
239 
240  /* Assume failure */
241  if (NewDeviceObject) *NewDeviceObject = NULL;
242 
243  /* Setup the attributes */
246  &DeviceName,
248  0,
249  NULL);
250 
251  /* Open the device */
252  Status = ZwCreateFile(&FileHandle,
253  SYNCHRONIZE,
255  &IoStatus,
256  NULL,
257  0,
259  FILE_OPEN,
260  0,
261  NULL,
262  0);
263  if (NT_SUCCESS(Status))
264  {
265  /* We succeeded, close the handle */
267  }
269  {
270  /* We failed with anything else then what we want to fail with */
272  }
273 
274  /* If we succeeded, there's no point in trying this again */
276 
277  /* Create recognizer device object */
278  RtlInitUnicodeString(&DeviceName, RecognizerName);
280  sizeof(DEVICE_EXTENSION),
281  &DeviceName,
282  DeviceType,
283  0,
284  FALSE,
285  &DeviceObject);
286  if (NT_SUCCESS(Status))
287  {
288  /* Set additional flags in the device object */
289  DeviceObject->Flags |= AdditionalFlags;
290 
291  /* Get the device extension and set it up */
292  DeviceExtension = DeviceObject->DeviceExtension;
293  DeviceExtension->FsType = FsType;
294  DeviceExtension->State = Pending;
295 
296  /* Do we have a parent? */
297  if (ParentObject)
298  {
299  /* Link it in */
300  DeviceExtension->Alternate =
301  ((PDEVICE_EXTENSION)ParentObject->DeviceExtension)->Alternate;
302  ((PDEVICE_EXTENSION)ParentObject->DeviceExtension)->Alternate =
303  DeviceObject;
304  }
305  else
306  {
307  /* Otherwise, we're the only one */
308  DeviceExtension->Alternate = DeviceObject;
309  }
310 
311  /* Return the DO if needed */
312  if (NewDeviceObject) *NewDeviceObject = DeviceObject;
313 
314  /* Register the file system */
316  }
317 
318  /* Return Status */
319  return Status;
320 }
321 
322 NTSTATUS
323 NTAPI
326 {
328  ULONG DeviceCount = 0;
329  PDEVICE_OBJECT CdfsObject;
330  PDEVICE_OBJECT UdfsObject;
331  PDEVICE_OBJECT FatObject;
332 
333  PAGED_CODE();
334 
336 
337  /* Page the entire driver */
339 
340  /* Allocate the lock */
342  sizeof(KEVENT),
343  FSREC_TAG);
345 
346  /* Initialize it */
348 
349  /* Setup the major functions */
350  DriverObject->MajorFunction[IRP_MJ_CREATE] = FsRecCreate;
351  DriverObject->MajorFunction[IRP_MJ_CLOSE] = FsRecClose;
352  DriverObject->MajorFunction[IRP_MJ_CLEANUP] = FsRecClose;
354  DriverObject->DriverUnload = FsRecUnload;
355 
356  /* Register CDFS for CDs */
358  NULL,
359  &CdfsObject,
360  L"\\Cdfs",
361  L"\\FileSystem\\CdfsRecognizer",
362  FS_TYPE_CDFS,
365  if (NT_SUCCESS(Status)) DeviceCount++;
366 
367  /* Register CDFS for HDDs */
369  CdfsObject,
370  NULL,
371  L"\\CdfsHdd",
372  L"\\FileSystem\\CdfsHddRecognizer",
373  FS_TYPE_CDFS,
376  if (NT_SUCCESS(Status)) DeviceCount++;
377 
378  /* Register UDFS for CDs */
380  NULL,
381  &UdfsObject,
382  L"\\UdfsCdRom",
383  L"\\FileSystem\\UdfsCdRomRecognizer",
384  FS_TYPE_UDFS,
386  0);
387  if (NT_SUCCESS(Status)) DeviceCount++;
388 
389  /* Register UDFS for HDDs */
391  UdfsObject,
392  NULL,
393  L"\\UdfsDisk",
394  L"\\FileSystem\\UdfsDiskRecognizer",
395  FS_TYPE_UDFS,
397  0);
398  if (NT_SUCCESS(Status)) DeviceCount++;
399 
400  /* Register FAT */
402  NULL,
403  &FatObject,
404  L"\\Fat",
405  L"\\FileSystem\\FatRecognizer",
406  FS_TYPE_VFAT,
408  0);
409  if (NT_SUCCESS(Status)) DeviceCount++;
410 
411  /* Register FAT for CDs */
413  FatObject,
414  NULL,
415  L"\\FatCdrom",
416  L"\\FileSystem\\FatCdRomRecognizer",
417  FS_TYPE_VFAT,
419  0);
420  if (NT_SUCCESS(Status)) DeviceCount++;
421 
422  /* Register NTFS */
424  NULL,
425  NULL,
426  L"\\Ntfs",
427  L"\\FileSystem\\NtfsRecognizer",
428  FS_TYPE_NTFS,
430  0);
431  if (NT_SUCCESS(Status)) DeviceCount++;
432 
433  /* Register EXT2 */
435  NULL,
436  NULL,
437  L"\\Ext2fs",
438  L"\\FileSystem\\Ext2Recognizer",
439  FS_TYPE_EXT2,
441  0);
442  if (NT_SUCCESS(Status)) DeviceCount++;
443 
444  /* Register BTRFS */
446  NULL,
447  NULL,
448  L"\\Btrfs",
449  L"\\FileSystem\\BtrfsRecognizer",
452  0);
453  if (NT_SUCCESS(Status)) DeviceCount++;
454 
455  /* Register REISERFS */
457  NULL,
458  NULL,
459  L"\\Reiserfs",
460  L"\\FileSystem\\ReiserfsRecognizer",
463  0);
464  if (NT_SUCCESS(Status)) DeviceCount++;
465 
466  /* Register FFS */
468  NULL,
469  NULL,
470  L"\\ffs",
471  L"\\FileSystem\\FfsRecognizer",
472  FS_TYPE_FFS,
474  0);
475  if (NT_SUCCESS(Status)) DeviceCount++;
476 
477  /* Register FATX */
479  NULL,
480  NULL,
481  L"\\FatX",
482  L"\\FileSystem\\FatXRecognizer",
483  FS_TYPE_FATX,
485  0);
486  if (NT_SUCCESS(Status)) DeviceCount++;
487 
488  /* Return appropriate Status */
490 }
491 
492 /* EOF */
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
const uint16_t * PCWSTR
Definition: typedefs.h:57
#define IN
Definition: typedefs.h:39
#define IRP_MJ_CREATE
Definition: rdpdr.c:44
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
DRIVER_DISPATCH FsRecFsControl
Definition: fs_rec.c:124
NTSTATUS NTAPI FsRecVfatFsControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: fat.c:100
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define TRUE
Definition: types.h:120
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
NTSTATUS NTAPI FsRecExt2FsControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: ext2.c:30
VOID NTAPI IoUnregisterFileSystem(IN PDEVICE_OBJECT DeviceObject)
Definition: volume.c:1056
LONG NTSTATUS
Definition: precomp.h:26
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:213
#define FILE_OPENED
Definition: nt_native.h:769
#define STATUS_INVALID_DEVICE_REQUEST
Definition: udferr_usr.h:138
NTSTATUS NTAPI FsRecUdfsFsControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: udfs.c:106
uint16_t * PWCHAR
Definition: typedefs.h:56
NTSTATUS NTAPI DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
Definition: fs_rec.c:324
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
NTSTATUS NTAPI FsRecFatxFsControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: fatx.c:66
#define FILE_SHARE_READ
Definition: compat.h:136
Definition: fs_rec.h:187
NTSTATUS NTAPI FsRecNtfsFsControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: ntfs.c:52
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
#define L(x)
Definition: ntvdm.h:50
_Must_inspect_result_ _In_ PWDFDEVICE_INIT _In_opt_ PCUNICODE_STRING DeviceName
Definition: wdfdevice.h:3272
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define FALSE
Definition: types.h:117
_In_ PIRP Irp
Definition: csq.h:116
PKEVENT FsRecLoadSync
Definition: fs_rec.c:17
NTSTATUS NTAPI FsRecCdfsFsControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: cdfs.c:66
ULONG DeviceCount
Definition: mpu401.c:26
DRIVER_DISPATCH FsRecClose
Definition: fs_rec.c:109
#define IoCompleteRequest
Definition: irp.c:1240
DeviceType
Definition: mmdrv.h:41
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
#define FILE_DEVICE_DISK_FILE_SYSTEM
Definition: winioctl.h:114
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define IRP_MJ_FILE_SYSTEM_CONTROL
PVOID NTAPI MmPageEntireDriver(IN PVOID AddressWithinSection)
Definition: sysldr.c:3459
#define FSREC_TAG
Definition: fs_rec.h:16
#define STATUS_OBJECT_PATH_NOT_FOUND
Definition: udferr_usr.h:151
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define IRP_MJ_CLOSE
Definition: rdpdr.c:45
DRIVER_UNLOAD FsRecUnload
Definition: fs_rec.c:203
DRIVER_DISPATCH FsRecCreate
Definition: fs_rec.c:78
NTSTATUS NTAPI FsRecFfsFsControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: ffs.c:43
#define KeEnterCriticalRegion()
Definition: ke_x.h:88
#define SYNCHRONIZE
Definition: nt_native.h:61
DRIVER_DISPATCH(nfs41_FsdDispatch)
#define DO_LOW_PRIORITY_FILESYSTEM
#define FILE_OPEN
Definition: from_kernel.h:54
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2793
VOID NTAPI IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject)
Definition: device.c:1251
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1230
PFILE_OBJECT FileObject
Definition: iotypes.h:3169
#define KeLeaveCriticalRegion()
Definition: ke_x.h:119
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define STATUS_OBJECT_NAME_NOT_FOUND
Definition: udferr_usr.h:149
#define DEVICE_TYPE
Definition: guid.c:10
#define NULL
Definition: types.h:112
NTSTATUS NTAPI FsRecRegisterFs(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT ParentObject OPTIONAL, OUT PDEVICE_OBJECT *NewDeviceObject OPTIONAL, IN PCWSTR FsName, IN PCWSTR RecognizerName, IN ULONG FsType, IN DEVICE_TYPE DeviceType, IN ULONG AdditionalFlags)
Definition: fs_rec.c:223
VOID NTAPI IoRegisterFileSystem(IN PDEVICE_OBJECT DeviceObject)
Definition: volume.c:987
NTSTATUS NTAPI FsRecLoadFileSystem(IN PDEVICE_OBJECT DeviceObject, IN PWCHAR DriverServiceName)
Definition: fs_rec.c:23
#define IRP_MJ_CLEANUP
#define OUT
Definition: typedefs.h:40
#define FILE_DEVICE_CD_ROM_FILE_SYSTEM
Definition: winioctl.h:109
__in UCHAR __in POWER_STATE __in_opt PVOID __in PIO_STATUS_BLOCK IoStatus
Definition: mxum.h:155
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:598
NTSTATUS NTAPI IoCreateDevice(IN PDRIVER_OBJECT DriverObject, IN ULONG DeviceExtensionSize, IN PUNICODE_STRING DeviceName, IN DEVICE_TYPE DeviceType, IN ULONG DeviceCharacteristics, IN BOOLEAN Exclusive, OUT PDEVICE_OBJECT *DeviceObject)
Definition: device.c:1031
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define STATUS_SUCCESS
Definition: shellext.h:65
NTSTATUS NTAPI FsRecBtrfsFsControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: btrfs.c:30
struct _BEEP_DEVICE_EXTENSION * PDEVICE_EXTENSION
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
NTSTATUS NTAPI FsRecReiserfsFsControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: reiserfs.c:43
#define STATUS_IMAGE_ALREADY_LOADED
Definition: ntstatus.h:506
#define PAGED_CODE()
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68