[CDFS]
[reactos.git] / reactos / drivers / filesystems / fs_rec / fs_rec.c
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
17 PKEVENT FsRecLoadSync;
18
19 /* FUNCTIONS ****************************************************************/
20
21 NTSTATUS
22 NTAPI
23 FsRecLoadFileSystem(IN PDEVICE_OBJECT DeviceObject,
24 IN PWCHAR DriverServiceName)
25 {
26 UNICODE_STRING DriverName;
27 PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
28 NTSTATUS Status = STATUS_IMAGE_ALREADY_LOADED;
29 PAGED_CODE();
30
31 /* Make sure we haven't already been called */
32 if (DeviceExtension->State != Loaded)
33 {
34 /* Acquire the load lock */
35 KeWaitForSingleObject(FsRecLoadSync,
36 Executive,
37 KernelMode,
38 FALSE,
39 NULL);
40 KeEnterCriticalRegion();
41
42 /* Make sure we're active */
43 if (DeviceExtension->State == Pending)
44 {
45 /* Load the FS driver */
46 RtlInitUnicodeString(&DriverName, DriverServiceName);
47 Status = ZwLoadDriver(&DriverName);
48
49 /* Loop all the linked recognizer objects */
50 while (DeviceExtension->State != Unloading)
51 {
52 /* Set them to the unload state */
53 DeviceExtension->State = Unloading;
54
55 /* Go to the next one */
56 DeviceObject = DeviceExtension->Alternate;
57 DeviceExtension = DeviceObject->DeviceExtension;
58 }
59 }
60
61 /* Make sure that we haven't already loaded the FS */
62 if (DeviceExtension->State != Loaded)
63 {
64 /* Unregister us, and set us as loaded */
65 IoUnregisterFileSystem(DeviceObject);
66 DeviceExtension->State = Loaded;
67 }
68
69 /* Release the lock */
70 KeSetEvent(FsRecLoadSync, 0, FALSE);
71 KeLeaveCriticalRegion();
72 }
73
74 /* Return */
75 return Status;
76 }
77
78 DRIVER_DISPATCH FsRecCreate;
79 NTSTATUS
80 NTAPI
81 FsRecCreate(IN PDEVICE_OBJECT DeviceObject,
82 IN PIRP Irp)
83 {
84 PIO_STACK_LOCATION IoStack = IoGetCurrentIrpStackLocation(Irp);
85 NTSTATUS Status;
86 PAGED_CODE();
87
88 UNREFERENCED_PARAMETER(DeviceObject);
89
90 /* Make sure we have a file name */
91 if (IoStack->FileObject->FileName.Length)
92 {
93 /* Fail the request */
94 Status = STATUS_OBJECT_PATH_NOT_FOUND;
95 }
96 else
97 {
98 /* Let it through */
99 Status = STATUS_SUCCESS;
100 }
101
102 /* Complete the IRP */
103 Irp->IoStatus.Status = Status;
104 Irp->IoStatus.Information = FILE_OPENED;
105 IoCompleteRequest(Irp, IO_NO_INCREMENT);
106 return Status;
107 }
108
109 DRIVER_DISPATCH FsRecClose;
110 NTSTATUS
111 NTAPI
112 FsRecClose(IN PDEVICE_OBJECT DeviceObject,
113 IN PIRP Irp)
114 {
115 PAGED_CODE();
116
117 UNREFERENCED_PARAMETER(DeviceObject);
118
119 /* Just complete the IRP and return success */
120 IoCompleteRequest(Irp, IO_NO_INCREMENT);
121 return STATUS_SUCCESS;
122 }
123
124 DRIVER_DISPATCH FsRecFsControl;
125 NTSTATUS
126 NTAPI
127 FsRecFsControl(IN PDEVICE_OBJECT DeviceObject,
128 IN PIRP Irp)
129 {
130 PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
131 NTSTATUS Status;
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 */
140 Status = FsRecVfatFsControl(DeviceObject, Irp);
141 break;
142
143 case FS_TYPE_NTFS:
144
145 /* Send NTFS command */
146 Status = FsRecNtfsFsControl(DeviceObject, Irp);
147 break;
148
149 case FS_TYPE_CDFS:
150
151 /* Send CDFS command */
152 Status = FsRecCdfsFsControl(DeviceObject, Irp);
153 break;
154
155 case FS_TYPE_UDFS:
156
157 /* Send UDFS command */
158 Status = FsRecUdfsFsControl(DeviceObject, Irp);
159 break;
160
161 case FS_TYPE_EXT2:
162
163 /* Send EXT2 command */
164 Status = FsRecExt2FsControl(DeviceObject, Irp);
165 break;
166
167 case FS_TYPE_BTRFS:
168
169 /* Send BTRFS command */
170 Status = FsRecBtrfsFsControl(DeviceObject, Irp);
171 break;
172
173 case FS_TYPE_REISERFS:
174
175 /* Send REISERFS command */
176 Status = FsRecReiserfsFsControl(DeviceObject, Irp);
177 break;
178
179 case FS_TYPE_FFS:
180
181 /* Send FFS command */
182 Status = FsRecFfsFsControl(DeviceObject, Irp);
183 break;
184
185 default:
186
187 /* Unrecognized FS */
188 Status = STATUS_INVALID_DEVICE_REQUEST;
189 }
190
191 /* Complete the IRP */
192 Irp->IoStatus.Status = Status;
193 IoCompleteRequest(Irp, IO_NO_INCREMENT);
194 return Status;
195 }
196
197 DRIVER_UNLOAD FsRecUnload;
198 VOID
199 NTAPI
200 FsRecUnload(IN PDRIVER_OBJECT DriverObject)
201 {
202 PAGED_CODE();
203
204 /* Loop all driver device objects */
205 while (DriverObject->DeviceObject)
206 {
207 /* Delete this device */
208 IoDeleteDevice(DriverObject->DeviceObject);
209 }
210
211 /* Free the lock */
212 ExFreePool(FsRecLoadSync);
213 }
214
215 NTSTATUS
216 NTAPI
217 FsRecRegisterFs(IN PDRIVER_OBJECT DriverObject,
218 IN PDEVICE_OBJECT ParentObject OPTIONAL,
219 OUT PDEVICE_OBJECT *NewDeviceObject OPTIONAL,
220 IN PCWSTR FsName,
221 IN PCWSTR RecognizerName,
222 IN ULONG FsType,
223 IN DEVICE_TYPE DeviceType)
224 {
225 OBJECT_ATTRIBUTES ObjectAttributes;
226 IO_STATUS_BLOCK IoStatus;
227 PDEVICE_EXTENSION DeviceExtension;
228 UNICODE_STRING DeviceName;
229 PDEVICE_OBJECT DeviceObject;
230 HANDLE FileHandle;
231 NTSTATUS Status;
232
233 /* Assume failure */
234 if (NewDeviceObject) *NewDeviceObject = NULL;
235
236 /* Setup the attributes */
237 RtlInitUnicodeString(&DeviceName, FsName);
238 InitializeObjectAttributes(&ObjectAttributes,
239 &DeviceName,
240 OBJ_CASE_INSENSITIVE,
241 0,
242 NULL);
243
244 /* Open the device */
245 Status = ZwCreateFile(&FileHandle,
246 SYNCHRONIZE,
247 &ObjectAttributes,
248 &IoStatus,
249 NULL,
250 0,
251 FILE_SHARE_READ | FILE_SHARE_WRITE,
252 FILE_OPEN,
253 0,
254 NULL,
255 0);
256 if (NT_SUCCESS(Status))
257 {
258 /* We succeeded, close the handle */
259 ZwClose(FileHandle);
260 }
261 else if (Status != STATUS_OBJECT_NAME_NOT_FOUND)
262 {
263 /* We failed with anything else then what we want to fail with */
264 Status = STATUS_SUCCESS;
265 }
266
267 /* If we succeeded, there's no point in trying this again */
268 if (NT_SUCCESS(Status)) return STATUS_IMAGE_ALREADY_LOADED;
269
270 /* Create recognizer device object */
271 RtlInitUnicodeString(&DeviceName, RecognizerName);
272 Status = IoCreateDevice(DriverObject,
273 sizeof(DEVICE_EXTENSION),
274 &DeviceName,
275 DeviceType,
276 0,
277 FALSE,
278 &DeviceObject);
279 if (NT_SUCCESS(Status))
280 {
281 /* Get the device extension and set it up */
282 DeviceExtension = DeviceObject->DeviceExtension;
283 DeviceExtension->FsType = FsType;
284 DeviceExtension->State = Pending;
285
286 /* Do we have a parent? */
287 if (ParentObject)
288 {
289 /* Link it in */
290 DeviceExtension->Alternate =
291 ((PDEVICE_EXTENSION)ParentObject->DeviceExtension)->Alternate;
292 ((PDEVICE_EXTENSION)ParentObject->DeviceExtension)->Alternate =
293 DeviceObject;
294 }
295 else
296 {
297 /* Otherwise, we're the only one */
298 DeviceExtension->Alternate = DeviceObject;
299 }
300
301 /* Return the DO if needed */
302 if (NewDeviceObject) *NewDeviceObject = DeviceObject;
303
304 /* Register the file system */
305 IoRegisterFileSystem(DeviceObject);
306 }
307
308 /* Return Status */
309 return Status;
310 }
311
312 NTSTATUS
313 NTAPI
314 DriverEntry(IN PDRIVER_OBJECT DriverObject,
315 IN PUNICODE_STRING RegistryPath)
316 {
317 ULONG DeviceCount = 0;
318 NTSTATUS Status;
319 PDEVICE_OBJECT UdfsObject;
320 PAGED_CODE();
321
322 UNREFERENCED_PARAMETER(RegistryPath);
323
324 /* Page the entire driver */
325 MmPageEntireDriver(DriverEntry);
326
327 /* Allocate the lock */
328 FsRecLoadSync = ExAllocatePoolWithTag(NonPagedPool,
329 sizeof(KEVENT),
330 FSREC_TAG);
331 if (!FsRecLoadSync) return STATUS_INSUFFICIENT_RESOURCES;
332
333 /* Initialize it */
334 KeInitializeEvent(FsRecLoadSync, SynchronizationEvent, TRUE);
335
336 /* Setup the major functions */
337 DriverObject->MajorFunction[IRP_MJ_CREATE] = FsRecCreate;
338 DriverObject->MajorFunction[IRP_MJ_CLOSE] = FsRecClose;
339 DriverObject->MajorFunction[IRP_MJ_CLEANUP] = FsRecClose;
340 DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] = FsRecFsControl;
341 DriverObject->DriverUnload = FsRecUnload;
342
343 /* Register CDFS */
344 Status = FsRecRegisterFs(DriverObject,
345 NULL,
346 NULL,
347 L"\\Cdfs",
348 L"\\FileSystem\\CdfsRecognizer",
349 FS_TYPE_CDFS,
350 FILE_DEVICE_CD_ROM_FILE_SYSTEM);
351 if (NT_SUCCESS(Status)) DeviceCount++;
352
353 /* Register UDFS for CDs */
354 Status = FsRecRegisterFs(DriverObject,
355 NULL,
356 &UdfsObject,
357 L"\\UdfsCdRom",
358 L"\\FileSystem\\UdfsCdRomRecognizer",
359 FS_TYPE_UDFS,
360 FILE_DEVICE_CD_ROM_FILE_SYSTEM);
361 if (NT_SUCCESS(Status)) DeviceCount++;
362
363 /* Register UDFS for HDDs */
364 Status = FsRecRegisterFs(DriverObject,
365 UdfsObject,
366 NULL,
367 L"\\UdfsDisk",
368 L"\\FileSystem\\UdfsDiskRecognizer",
369 FS_TYPE_UDFS,
370 FILE_DEVICE_DISK_FILE_SYSTEM);
371 if (NT_SUCCESS(Status)) DeviceCount++;
372
373 /* Register FAT */
374 Status = FsRecRegisterFs(DriverObject,
375 NULL,
376 NULL,
377 L"\\Fat",
378 L"\\FileSystem\\FatRecognizer",
379 FS_TYPE_VFAT,
380 FILE_DEVICE_DISK_FILE_SYSTEM);
381 if (NT_SUCCESS(Status)) DeviceCount++;
382
383 /* Register NTFS */
384 Status = FsRecRegisterFs(DriverObject,
385 NULL,
386 NULL,
387 L"\\Ntfs",
388 L"\\FileSystem\\NtfsRecognizer",
389 FS_TYPE_NTFS,
390 FILE_DEVICE_DISK_FILE_SYSTEM);
391 if (NT_SUCCESS(Status)) DeviceCount++;
392
393 /* Register EXT2 */
394 Status = FsRecRegisterFs(DriverObject,
395 NULL,
396 NULL,
397 L"\\Ext2fs",
398 L"\\FileSystem\\Ext2Recognizer",
399 FS_TYPE_EXT2,
400 FILE_DEVICE_DISK_FILE_SYSTEM);
401 if (NT_SUCCESS(Status)) DeviceCount++;
402
403 /* Register BTRFS */
404 Status = FsRecRegisterFs(DriverObject,
405 NULL,
406 NULL,
407 L"\\Btrfs",
408 L"\\FileSystem\\BtrfsRecognizer",
409 FS_TYPE_BTRFS,
410 FILE_DEVICE_DISK_FILE_SYSTEM);
411 if (NT_SUCCESS(Status)) DeviceCount++;
412
413 /* Register REISERFS */
414 Status = FsRecRegisterFs(DriverObject,
415 NULL,
416 NULL,
417 L"\\Reiserfs",
418 L"\\FileSystem\\ReiserfsRecognizer",
419 FS_TYPE_REISERFS,
420 FILE_DEVICE_DISK_FILE_SYSTEM);
421 if (NT_SUCCESS(Status)) DeviceCount++;
422
423 /* Register FFS */
424 Status = FsRecRegisterFs(DriverObject,
425 NULL,
426 NULL,
427 L"\\ffs",
428 L"\\FileSystem\\FfsRecognizer",
429 FS_TYPE_FFS,
430 FILE_DEVICE_DISK_FILE_SYSTEM);
431 if (NT_SUCCESS(Status)) DeviceCount++;
432
433 /* Return appropriate Status */
434 return (DeviceCount > 0) ? STATUS_SUCCESS : STATUS_IMAGE_ALREADY_LOADED;
435 }
436
437 /* EOF */