[FASTFAT]
[reactos.git] / reactos / drivers / filesystems / fastfat / fsctl.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: drivers/fs/vfat/fsctl.c
23 * PURPOSE: VFAT Filesystem
24 */
25
26 /* INCLUDES *****************************************************************/
27
28 #include "vfat.h"
29
30 #define NDEBUG
31 #include <debug.h>
32
33 extern VFAT_DISPATCH FatXDispatch;
34 extern VFAT_DISPATCH FatDispatch;
35
36 /* FUNCTIONS ****************************************************************/
37
38 #define CACHEPAGESIZE(pDeviceExt) ((pDeviceExt)->FatInfo.BytesPerCluster > PAGE_SIZE ? \
39 (pDeviceExt)->FatInfo.BytesPerCluster : PAGE_SIZE)
40
41 static
42 NTSTATUS
43 VfatHasFileSystem(
44 PDEVICE_OBJECT DeviceToMount,
45 PBOOLEAN RecognizedFS,
46 PFATINFO pFatInfo,
47 BOOLEAN Override)
48 {
49 NTSTATUS Status;
50 PARTITION_INFORMATION PartitionInfo;
51 DISK_GEOMETRY DiskGeometry;
52 FATINFO FatInfo;
53 ULONG Size;
54 ULONG Sectors;
55 LARGE_INTEGER Offset;
56 struct _BootSector* Boot;
57 struct _BootSectorFatX* BootFatX;
58 BOOLEAN PartitionInfoIsValid = FALSE;
59
60 DPRINT("VfatHasFileSystem\n");
61
62 *RecognizedFS = FALSE;
63
64 Size = sizeof(DISK_GEOMETRY);
65 Status = VfatBlockDeviceIoControl(DeviceToMount,
66 IOCTL_DISK_GET_DRIVE_GEOMETRY,
67 NULL,
68 0,
69 &DiskGeometry,
70 &Size,
71 Override);
72 if (!NT_SUCCESS(Status))
73 {
74 DPRINT("VfatBlockDeviceIoControl failed (%x)\n", Status);
75 return Status;
76 }
77
78 FatInfo.FixedMedia = DiskGeometry.MediaType == FixedMedia ? TRUE : FALSE;
79 if (DiskGeometry.MediaType == FixedMedia || DiskGeometry.MediaType == RemovableMedia)
80 {
81 // We have found a hard disk
82 Size = sizeof(PARTITION_INFORMATION);
83 Status = VfatBlockDeviceIoControl(DeviceToMount,
84 IOCTL_DISK_GET_PARTITION_INFO,
85 NULL,
86 0,
87 &PartitionInfo,
88 &Size,
89 Override);
90 if (!NT_SUCCESS(Status))
91 {
92 DPRINT("VfatBlockDeviceIoControl failed (%x)\n", Status);
93 return Status;
94 }
95
96 DPRINT("Partition Information:\n");
97 DPRINT("StartingOffset %I64x\n", PartitionInfo.StartingOffset.QuadPart / 512);
98 DPRINT("PartitionLength %I64x\n", PartitionInfo.PartitionLength.QuadPart / 512);
99 DPRINT("HiddenSectors %u\n", PartitionInfo.HiddenSectors);
100 DPRINT("PartitionNumber %u\n", PartitionInfo.PartitionNumber);
101 DPRINT("PartitionType %u\n", PartitionInfo.PartitionType);
102 DPRINT("BootIndicator %u\n", PartitionInfo.BootIndicator);
103 DPRINT("RecognizedPartition %u\n", PartitionInfo.RecognizedPartition);
104 DPRINT("RewritePartition %u\n", PartitionInfo.RewritePartition);
105 if (PartitionInfo.PartitionType)
106 {
107 if (PartitionInfo.PartitionType == PARTITION_FAT_12 ||
108 PartitionInfo.PartitionType == PARTITION_FAT_16 ||
109 PartitionInfo.PartitionType == PARTITION_HUGE ||
110 PartitionInfo.PartitionType == PARTITION_FAT32 ||
111 PartitionInfo.PartitionType == PARTITION_FAT32_XINT13 ||
112 PartitionInfo.PartitionType == PARTITION_XINT13)
113 {
114 PartitionInfoIsValid = TRUE;
115 *RecognizedFS = TRUE;
116 }
117 }
118 else if (DiskGeometry.MediaType == RemovableMedia &&
119 PartitionInfo.PartitionNumber > 0 &&
120 PartitionInfo.StartingOffset.QuadPart == 0 &&
121 PartitionInfo.PartitionLength.QuadPart > 0)
122 {
123 /* This is possible a removable media formated as super floppy */
124 PartitionInfoIsValid = TRUE;
125 *RecognizedFS = TRUE;
126 }
127 }
128 else
129 {
130 *RecognizedFS = TRUE;
131 }
132
133 if (*RecognizedFS)
134 {
135 Boot = ExAllocatePoolWithTag(NonPagedPool, DiskGeometry.BytesPerSector, TAG_VFAT);
136 if (Boot == NULL)
137 {
138 return STATUS_INSUFFICIENT_RESOURCES;
139 }
140
141 Offset.QuadPart = 0;
142
143 /* Try to recognize FAT12/FAT16/FAT32 partitions */
144 Status = VfatReadDisk(DeviceToMount, &Offset, DiskGeometry.BytesPerSector, (PUCHAR) Boot, Override);
145 if (NT_SUCCESS(Status))
146 {
147 if (Boot->Signatur1 != 0xaa55)
148 {
149 *RecognizedFS = FALSE;
150 }
151
152 if (*RecognizedFS &&
153 Boot->BytesPerSector != 512 &&
154 Boot->BytesPerSector != 1024 &&
155 Boot->BytesPerSector != 2048 &&
156 Boot->BytesPerSector != 4096)
157 {
158 DPRINT1("BytesPerSector %u\n", Boot->BytesPerSector);
159 *RecognizedFS = FALSE;
160 }
161
162 if (*RecognizedFS &&
163 Boot->FATCount != 1 &&
164 Boot->FATCount != 2)
165 {
166 DPRINT1("FATCount %u\n", Boot->FATCount);
167 *RecognizedFS = FALSE;
168 }
169
170 if (*RecognizedFS &&
171 Boot->Media != 0xf0 &&
172 Boot->Media != 0xf8 &&
173 Boot->Media != 0xf9 &&
174 Boot->Media != 0xfa &&
175 Boot->Media != 0xfb &&
176 Boot->Media != 0xfc &&
177 Boot->Media != 0xfd &&
178 Boot->Media != 0xfe &&
179 Boot->Media != 0xff)
180 {
181 DPRINT1("Media %02x\n", Boot->Media);
182 *RecognizedFS = FALSE;
183 }
184
185 if (*RecognizedFS &&
186 Boot->SectorsPerCluster != 1 &&
187 Boot->SectorsPerCluster != 2 &&
188 Boot->SectorsPerCluster != 4 &&
189 Boot->SectorsPerCluster != 8 &&
190 Boot->SectorsPerCluster != 16 &&
191 Boot->SectorsPerCluster != 32 &&
192 Boot->SectorsPerCluster != 64 &&
193 Boot->SectorsPerCluster != 128)
194 {
195 DPRINT1("SectorsPerCluster %02x\n", Boot->SectorsPerCluster);
196 *RecognizedFS = FALSE;
197 }
198
199 if (*RecognizedFS &&
200 Boot->BytesPerSector * Boot->SectorsPerCluster > 32 * 1024)
201 {
202 DPRINT1("ClusterSize %dx\n", Boot->BytesPerSector * Boot->SectorsPerCluster);
203 *RecognizedFS = FALSE;
204 }
205
206 if (*RecognizedFS)
207 {
208 FatInfo.VolumeID = Boot->VolumeID;
209 FatInfo.FATStart = Boot->ReservedSectors;
210 FatInfo.FATCount = Boot->FATCount;
211 FatInfo.FATSectors = Boot->FATSectors ? Boot->FATSectors : ((struct _BootSector32*) Boot)->FATSectors32;
212 FatInfo.BytesPerSector = Boot->BytesPerSector;
213 FatInfo.SectorsPerCluster = Boot->SectorsPerCluster;
214 FatInfo.BytesPerCluster = FatInfo.BytesPerSector * FatInfo.SectorsPerCluster;
215 FatInfo.rootDirectorySectors = ((Boot->RootEntries * 32) + Boot->BytesPerSector - 1) / Boot->BytesPerSector;
216 FatInfo.rootStart = FatInfo.FATStart + FatInfo.FATCount * FatInfo.FATSectors;
217 FatInfo.dataStart = FatInfo.rootStart + FatInfo.rootDirectorySectors;
218 FatInfo.Sectors = Sectors = Boot->Sectors ? Boot->Sectors : Boot->SectorsHuge;
219 Sectors -= Boot->ReservedSectors + FatInfo.FATCount * FatInfo.FATSectors + FatInfo.rootDirectorySectors;
220 FatInfo.NumberOfClusters = Sectors / Boot->SectorsPerCluster;
221 if (FatInfo.NumberOfClusters < 4085)
222 {
223 DPRINT("FAT12\n");
224 FatInfo.FatType = FAT12;
225 FatInfo.RootCluster = (FatInfo.rootStart - 1) / FatInfo.SectorsPerCluster;
226 RtlCopyMemory(&FatInfo.VolumeLabel, &Boot->VolumeLabel, sizeof(FatInfo.VolumeLabel));
227 }
228 else if (FatInfo.NumberOfClusters >= 65525)
229 {
230 DPRINT("FAT32\n");
231 FatInfo.FatType = FAT32;
232 FatInfo.RootCluster = ((struct _BootSector32*) Boot)->RootCluster;
233 FatInfo.rootStart = FatInfo.dataStart + ((FatInfo.RootCluster - 2) * FatInfo.SectorsPerCluster);
234 FatInfo.VolumeID = ((struct _BootSector32*) Boot)->VolumeID;
235 RtlCopyMemory(&FatInfo.VolumeLabel, &((struct _BootSector32*)Boot)->VolumeLabel, sizeof(FatInfo.VolumeLabel));
236 }
237 else
238 {
239 DPRINT("FAT16\n");
240 FatInfo.FatType = FAT16;
241 FatInfo.RootCluster = FatInfo.rootStart / FatInfo.SectorsPerCluster;
242 RtlCopyMemory(&FatInfo.VolumeLabel, &Boot->VolumeLabel, sizeof(FatInfo.VolumeLabel));
243 }
244
245 if (PartitionInfoIsValid &&
246 FatInfo.Sectors > PartitionInfo.PartitionLength.QuadPart / FatInfo.BytesPerSector)
247 {
248 *RecognizedFS = FALSE;
249 }
250
251 if (pFatInfo && *RecognizedFS)
252 {
253 *pFatInfo = FatInfo;
254 }
255 }
256 }
257
258 ExFreePool(Boot);
259 }
260
261 if (!*RecognizedFS && PartitionInfoIsValid)
262 {
263 BootFatX = ExAllocatePoolWithTag(NonPagedPool, sizeof(struct _BootSectorFatX), TAG_VFAT);
264 if (BootFatX == NULL)
265 {
266 *RecognizedFS=FALSE;
267 return STATUS_INSUFFICIENT_RESOURCES;
268 }
269
270 Offset.QuadPart = 0;
271
272 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
273 Status = VfatReadDisk(DeviceToMount, &Offset, sizeof(struct _BootSectorFatX), (PUCHAR) BootFatX, Override);
274 if (NT_SUCCESS(Status))
275 {
276 *RecognizedFS = TRUE;
277 if (BootFatX->SysType[0] != 'F' ||
278 BootFatX->SysType[1] != 'A' ||
279 BootFatX->SysType[2] != 'T' ||
280 BootFatX->SysType[3] != 'X')
281 {
282 DPRINT1("SysType %c%c%c%c\n", BootFatX->SysType[0], BootFatX->SysType[1], BootFatX->SysType[2], BootFatX->SysType[3]);
283 *RecognizedFS=FALSE;
284 }
285
286 if (*RecognizedFS &&
287 BootFatX->SectorsPerCluster != 1 &&
288 BootFatX->SectorsPerCluster != 2 &&
289 BootFatX->SectorsPerCluster != 4 &&
290 BootFatX->SectorsPerCluster != 8 &&
291 BootFatX->SectorsPerCluster != 16 &&
292 BootFatX->SectorsPerCluster != 32 &&
293 BootFatX->SectorsPerCluster != 64 &&
294 BootFatX->SectorsPerCluster != 128)
295 {
296 DPRINT1("SectorsPerCluster %lu\n", BootFatX->SectorsPerCluster);
297 *RecognizedFS=FALSE;
298 }
299
300 if (*RecognizedFS)
301 {
302 FatInfo.BytesPerSector = DiskGeometry.BytesPerSector;
303 FatInfo.SectorsPerCluster = BootFatX->SectorsPerCluster;
304 FatInfo.rootDirectorySectors = BootFatX->SectorsPerCluster;
305 FatInfo.BytesPerCluster = BootFatX->SectorsPerCluster * DiskGeometry.BytesPerSector;
306 FatInfo.Sectors = (ULONG)(PartitionInfo.PartitionLength.QuadPart / DiskGeometry.BytesPerSector);
307 if (FatInfo.Sectors / FatInfo.SectorsPerCluster < 65525)
308 {
309 DPRINT("FATX16\n");
310 FatInfo.FatType = FATX16;
311 }
312 else
313 {
314 DPRINT("FATX32\n");
315 FatInfo.FatType = FATX32;
316 }
317 FatInfo.VolumeID = BootFatX->VolumeID;
318 FatInfo.FATStart = sizeof(struct _BootSectorFatX) / DiskGeometry.BytesPerSector;
319 FatInfo.FATCount = BootFatX->FATCount;
320 FatInfo.FATSectors =
321 ROUND_UP(FatInfo.Sectors / FatInfo.SectorsPerCluster * (FatInfo.FatType == FATX16 ? 2 : 4), 4096) /
322 FatInfo.BytesPerSector;
323 FatInfo.rootStart = FatInfo.FATStart + FatInfo.FATCount * FatInfo.FATSectors;
324 FatInfo.RootCluster = (FatInfo.rootStart - 1) / FatInfo.SectorsPerCluster;
325 FatInfo.dataStart = FatInfo.rootStart + FatInfo.rootDirectorySectors;
326 FatInfo.NumberOfClusters = (FatInfo.Sectors - FatInfo.dataStart) / FatInfo.SectorsPerCluster;
327
328 if (pFatInfo && *RecognizedFS)
329 {
330 *pFatInfo = FatInfo;
331 }
332 }
333 }
334 ExFreePool(BootFatX);
335 }
336
337 DPRINT("VfatHasFileSystem done\n");
338 return Status;
339 }
340
341 /*
342 * FUNCTION: Read the volume label
343 * WARNING: Read this comment carefully before using it (and using it wrong)
344 * Device parameter is expected to be the lower DO is start isn't 0
345 * otherwise, it is expected to be the VCB is start is 0
346 * Start parameter is expected to be, in bytes, the beginning of the root start.
347 * Set it to 0 if you wish to use the associated FCB with caching.
348 * In that specific case, Device parameter is expected to be the VCB!
349 * VolumeLabel parameter is expected to be a preallocated UNICODE_STRING (ie, with buffer)
350 * Its buffer has to be able to contain MAXIMUM_VOLUME_LABEL_LENGTH bytes
351 */
352 static
353 NTSTATUS
354 ReadVolumeLabel(
355 PVOID Device,
356 ULONG Start,
357 BOOLEAN IsFatX,
358 PUNICODE_STRING VolumeLabel)
359 {
360 PDEVICE_EXTENSION DeviceExt;
361 PDEVICE_OBJECT DeviceObject;
362 PVOID Context = NULL;
363 ULONG DirIndex = 0;
364 PDIR_ENTRY Entry;
365 PVFATFCB pFcb;
366 LARGE_INTEGER FileOffset;
367 ULONG SizeDirEntry;
368 ULONG EntriesPerPage;
369 OEM_STRING StringO;
370 BOOLEAN NoCache = (Start != 0);
371 PVOID Buffer;
372 NTSTATUS Status = STATUS_SUCCESS;
373
374 if (IsFatX)
375 {
376 SizeDirEntry = sizeof(FATX_DIR_ENTRY);
377 EntriesPerPage = FATX_ENTRIES_PER_PAGE;
378 }
379 else
380 {
381 SizeDirEntry = sizeof(FAT_DIR_ENTRY);
382 EntriesPerPage = FAT_ENTRIES_PER_PAGE;
383 }
384
385 FileOffset.QuadPart = Start;
386 if (!NoCache)
387 {
388 DeviceExt = Device;
389
390 /* FIXME: Check we really have a VCB
391 ASSERT();
392 */
393
394 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
395 pFcb = vfatOpenRootFCB(DeviceExt);
396 ExReleaseResourceLite(&DeviceExt->DirResource);
397
398 _SEH2_TRY
399 {
400 CcMapData(pFcb->FileObject, &FileOffset, SizeDirEntry, MAP_WAIT, &Context, (PVOID*)&Entry);
401 }
402 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
403 {
404 Status = _SEH2_GetExceptionCode();
405 }
406 _SEH2_END;
407 }
408 else
409 {
410 DeviceObject = Device;
411
412 ASSERT(DeviceObject->Type == 3);
413
414 Buffer = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, TAG_VFAT);
415 if (Buffer != NULL)
416 {
417 Status = VfatReadDisk(DeviceObject, &FileOffset, PAGE_SIZE, (PUCHAR)Buffer, TRUE);
418 if (!NT_SUCCESS(Status))
419 {
420 ExFreePoolWithTag(Buffer, TAG_VFAT);
421 }
422 else
423 {
424 Entry = Buffer;
425 }
426 }
427 else
428 {
429 Status = STATUS_INSUFFICIENT_RESOURCES;
430 }
431 }
432
433 if (NT_SUCCESS(Status))
434 {
435 while (TRUE)
436 {
437 if (ENTRY_VOLUME(IsFatX, Entry))
438 {
439 /* copy volume label */
440 if (IsFatX)
441 {
442 StringO.Buffer = (PCHAR)Entry->FatX.Filename;
443 StringO.MaximumLength = StringO.Length = Entry->FatX.FilenameLength;
444 RtlOemStringToUnicodeString(VolumeLabel, &StringO, FALSE);
445 }
446 else
447 {
448 vfat8Dot3ToString(&Entry->Fat, VolumeLabel);
449 }
450 break;
451 }
452 if (ENTRY_END(IsFatX, Entry))
453 {
454 break;
455 }
456 DirIndex++;
457 Entry = (PDIR_ENTRY)((ULONG_PTR)Entry + SizeDirEntry);
458 if ((DirIndex % EntriesPerPage) == 0)
459 {
460 FileOffset.u.LowPart += PAGE_SIZE;
461
462 if (!NoCache)
463 {
464 CcUnpinData(Context);
465
466 _SEH2_TRY
467 {
468 CcMapData(pFcb->FileObject, &FileOffset, SizeDirEntry, MAP_WAIT, &Context, (PVOID*)&Entry);
469 }
470 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
471 {
472 Status = _SEH2_GetExceptionCode();
473 }
474 _SEH2_END;
475 if (!NT_SUCCESS(Status))
476 {
477 Context = NULL;
478 break;
479 }
480 }
481 else
482 {
483 Status = VfatReadDisk(DeviceObject, &FileOffset, PAGE_SIZE, (PUCHAR)Buffer, TRUE);
484 if (!NT_SUCCESS(Status))
485 {
486 break;
487 }
488 Entry = Buffer;
489 }
490 }
491 }
492 if (Context)
493 {
494 CcUnpinData(Context);
495 }
496 else if (NoCache)
497 {
498 ExFreePoolWithTag(Buffer, TAG_VFAT);
499 }
500 }
501
502 if (!NoCache)
503 {
504 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
505 vfatReleaseFCB(DeviceExt, pFcb);
506 ExReleaseResourceLite(&DeviceExt->DirResource);
507 }
508
509 return STATUS_SUCCESS;
510 }
511
512
513 /*
514 * FUNCTION: Mount the filesystem
515 */
516 static
517 NTSTATUS
518 VfatMount(
519 PVFAT_IRP_CONTEXT IrpContext)
520 {
521 PDEVICE_OBJECT DeviceObject = NULL;
522 PDEVICE_EXTENSION DeviceExt = NULL;
523 BOOLEAN RecognizedFS;
524 NTSTATUS Status;
525 PVFATFCB Fcb = NULL;
526 PVFATFCB VolumeFcb = NULL;
527 PVFATCCB Ccb = NULL;
528 PDEVICE_OBJECT DeviceToMount;
529 PVPB Vpb;
530 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\$$Fat$$");
531 UNICODE_STRING VolumeNameU = RTL_CONSTANT_STRING(L"\\$$Volume$$");
532 UNICODE_STRING VolumeLabelU;
533 ULONG HashTableSize;
534 ULONG eocMark;
535 FATINFO FatInfo;
536
537 DPRINT("VfatMount(IrpContext %p)\n", IrpContext);
538
539 ASSERT(IrpContext);
540
541 if (IrpContext->DeviceObject != VfatGlobalData->DeviceObject)
542 {
543 Status = STATUS_INVALID_DEVICE_REQUEST;
544 goto ByeBye;
545 }
546
547 DeviceToMount = IrpContext->Stack->Parameters.MountVolume.DeviceObject;
548 Vpb = IrpContext->Stack->Parameters.MountVolume.Vpb;
549
550 Status = VfatHasFileSystem(DeviceToMount, &RecognizedFS, &FatInfo, FALSE);
551 if (!NT_SUCCESS(Status))
552 {
553 goto ByeBye;
554 }
555
556 if (RecognizedFS == FALSE)
557 {
558 DPRINT("VFAT: Unrecognized Volume\n");
559 Status = STATUS_UNRECOGNIZED_VOLUME;
560 goto ByeBye;
561 }
562
563 /* Use prime numbers for the table size */
564 if (FatInfo.FatType == FAT12)
565 {
566 HashTableSize = 4099; // 4096 = 4 * 1024
567 }
568 else if (FatInfo.FatType == FAT16 ||
569 FatInfo.FatType == FATX16)
570 {
571 HashTableSize = 16411; // 16384 = 16 * 1024
572 }
573 else
574 {
575 HashTableSize = 65537; // 65536 = 64 * 1024;
576 }
577 DPRINT("VFAT: Recognized volume\n");
578 Status = IoCreateDevice(VfatGlobalData->DriverObject,
579 ROUND_UP(sizeof (DEVICE_EXTENSION), sizeof(ULONG)) + sizeof(HASHENTRY*) * HashTableSize,
580 NULL,
581 FILE_DEVICE_DISK_FILE_SYSTEM,
582 DeviceToMount->Characteristics,
583 FALSE,
584 &DeviceObject);
585 if (!NT_SUCCESS(Status))
586 {
587 goto ByeBye;
588 }
589
590 DeviceExt = DeviceObject->DeviceExtension;
591 RtlZeroMemory(DeviceExt, ROUND_UP(sizeof(DEVICE_EXTENSION), sizeof(ULONG)) + sizeof(HASHENTRY*) * HashTableSize);
592 DeviceExt->FcbHashTable = (HASHENTRY**)((ULONG_PTR)DeviceExt + ROUND_UP(sizeof(DEVICE_EXTENSION), sizeof(ULONG)));
593 DeviceExt->HashTableSize = HashTableSize;
594 DeviceExt->VolumeDevice = DeviceObject;
595
596 /* use same vpb as device disk */
597 DeviceObject->Vpb = Vpb;
598 DeviceToMount->Vpb = Vpb;
599
600 RtlCopyMemory(&DeviceExt->FatInfo, &FatInfo, sizeof(FATINFO));
601
602 DPRINT("BytesPerSector: %u\n", DeviceExt->FatInfo.BytesPerSector);
603 DPRINT("SectorsPerCluster: %u\n", DeviceExt->FatInfo.SectorsPerCluster);
604 DPRINT("FATCount: %u\n", DeviceExt->FatInfo.FATCount);
605 DPRINT("FATSectors: %u\n", DeviceExt->FatInfo.FATSectors);
606 DPRINT("RootStart: %u\n", DeviceExt->FatInfo.rootStart);
607 DPRINT("DataStart: %u\n", DeviceExt->FatInfo.dataStart);
608 if (DeviceExt->FatInfo.FatType == FAT32)
609 {
610 DPRINT("RootCluster: %u\n", DeviceExt->FatInfo.RootCluster);
611 }
612
613 switch (DeviceExt->FatInfo.FatType)
614 {
615 case FAT12:
616 DeviceExt->GetNextCluster = FAT12GetNextCluster;
617 DeviceExt->FindAndMarkAvailableCluster = FAT12FindAndMarkAvailableCluster;
618 DeviceExt->WriteCluster = FAT12WriteCluster;
619 DeviceExt->CleanShutBitMask = 0;
620 break;
621
622 case FAT16:
623 case FATX16:
624 DeviceExt->GetNextCluster = FAT16GetNextCluster;
625 DeviceExt->FindAndMarkAvailableCluster = FAT16FindAndMarkAvailableCluster;
626 DeviceExt->WriteCluster = FAT16WriteCluster;
627 DeviceExt->CleanShutBitMask = 0x8000;
628 break;
629
630 case FAT32:
631 case FATX32:
632 DeviceExt->GetNextCluster = FAT32GetNextCluster;
633 DeviceExt->FindAndMarkAvailableCluster = FAT32FindAndMarkAvailableCluster;
634 DeviceExt->WriteCluster = FAT32WriteCluster;
635 DeviceExt->CleanShutBitMask = 0x80000000;
636 break;
637 }
638
639 if (DeviceExt->FatInfo.FatType == FATX16 ||
640 DeviceExt->FatInfo.FatType == FATX32)
641 {
642 DeviceExt->Flags |= VCB_IS_FATX;
643 DeviceExt->BaseDateYear = 2000;
644 RtlCopyMemory(&DeviceExt->Dispatch, &FatXDispatch, sizeof(VFAT_DISPATCH));
645 }
646 else
647 {
648 DeviceExt->BaseDateYear = 1980;
649 RtlCopyMemory(&DeviceExt->Dispatch, &FatDispatch, sizeof(VFAT_DISPATCH));
650 }
651
652 DeviceExt->StorageDevice = DeviceToMount;
653 DeviceExt->StorageDevice->Vpb->DeviceObject = DeviceObject;
654 DeviceExt->StorageDevice->Vpb->RealDevice = DeviceExt->StorageDevice;
655 DeviceExt->StorageDevice->Vpb->Flags |= VPB_MOUNTED;
656 DeviceObject->StackSize = DeviceExt->StorageDevice->StackSize + 1;
657 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
658
659 DPRINT("FsDeviceObject %p\n", DeviceObject);
660
661 /* Initialize this resource early ... it's used in VfatCleanup */
662 ExInitializeResourceLite(&DeviceExt->DirResource);
663
664 DeviceExt->IoVPB = DeviceObject->Vpb;
665 DeviceExt->SpareVPB = ExAllocatePoolWithTag(NonPagedPool, sizeof(VPB), TAG_VFAT);
666 if (DeviceExt->SpareVPB == NULL)
667 {
668 Status = STATUS_INSUFFICIENT_RESOURCES;
669 goto ByeBye;
670 }
671
672 DeviceExt->FATFileObject = IoCreateStreamFileObject(NULL, DeviceExt->StorageDevice);
673 Fcb = vfatNewFCB(DeviceExt, &NameU);
674 if (Fcb == NULL)
675 {
676 Status = STATUS_INSUFFICIENT_RESOURCES;
677 goto ByeBye;
678 }
679
680 Ccb = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
681 if (Ccb == NULL)
682 {
683 Status = STATUS_INSUFFICIENT_RESOURCES;
684 goto ByeBye;
685 }
686
687 RtlZeroMemory(Ccb, sizeof (VFATCCB));
688 DeviceExt->FATFileObject->FsContext = Fcb;
689 DeviceExt->FATFileObject->FsContext2 = Ccb;
690 DeviceExt->FATFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
691 DeviceExt->FATFileObject->PrivateCacheMap = NULL;
692 DeviceExt->FATFileObject->Vpb = DeviceObject->Vpb;
693 Fcb->FileObject = DeviceExt->FATFileObject;
694
695 Fcb->Flags |= FCB_IS_FAT;
696
697 Fcb->RFCB.FileSize.QuadPart = DeviceExt->FatInfo.FATSectors * DeviceExt->FatInfo.BytesPerSector;
698 Fcb->RFCB.ValidDataLength = Fcb->RFCB.FileSize;
699 Fcb->RFCB.AllocationSize = Fcb->RFCB.FileSize;
700
701 _SEH2_TRY
702 {
703 CcInitializeCacheMap(DeviceExt->FATFileObject,
704 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
705 TRUE,
706 &VfatGlobalData->CacheMgrCallbacks,
707 Fcb);
708 }
709 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
710 {
711 Status = _SEH2_GetExceptionCode();
712 goto ByeBye;
713 }
714 _SEH2_END;
715
716 DeviceExt->LastAvailableCluster = 2;
717 ExInitializeResourceLite(&DeviceExt->FatResource);
718
719 InitializeListHead(&DeviceExt->FcbListHead);
720
721 VolumeFcb = vfatNewFCB(DeviceExt, &VolumeNameU);
722 if (VolumeFcb == NULL)
723 {
724 Status = STATUS_INSUFFICIENT_RESOURCES;
725 goto ByeBye;
726 }
727
728 VolumeFcb->Flags = FCB_IS_VOLUME;
729 VolumeFcb->RFCB.FileSize.QuadPart = (LONGLONG) DeviceExt->FatInfo.Sectors * DeviceExt->FatInfo.BytesPerSector;
730 VolumeFcb->RFCB.ValidDataLength = VolumeFcb->RFCB.FileSize;
731 VolumeFcb->RFCB.AllocationSize = VolumeFcb->RFCB.FileSize;
732 DeviceExt->VolumeFcb = VolumeFcb;
733
734 ExAcquireResourceExclusiveLite(&VfatGlobalData->VolumeListLock, TRUE);
735 InsertHeadList(&VfatGlobalData->VolumeListHead, &DeviceExt->VolumeListEntry);
736 ExReleaseResourceLite(&VfatGlobalData->VolumeListLock);
737
738 /* read serial number */
739 DeviceObject->Vpb->SerialNumber = DeviceExt->FatInfo.VolumeID;
740
741 /* read volume label */
742 VolumeLabelU.Buffer = DeviceObject->Vpb->VolumeLabel;
743 VolumeLabelU.Length = 0;
744 VolumeLabelU.MaximumLength = sizeof(DeviceObject->Vpb->VolumeLabel);
745 ReadVolumeLabel(DeviceExt, 0, vfatVolumeIsFatX(DeviceExt), &VolumeLabelU);
746 Vpb->VolumeLabelLength = VolumeLabelU.Length;
747
748 /* read clean shutdown bit status */
749 Status = GetNextCluster(DeviceExt, 1, &eocMark);
750 if (NT_SUCCESS(Status))
751 {
752 if (eocMark & DeviceExt->CleanShutBitMask)
753 {
754 /* unset clean shutdown bit */
755 eocMark &= ~DeviceExt->CleanShutBitMask;
756 WriteCluster(DeviceExt, 1, eocMark);
757 VolumeFcb->Flags |= VCB_CLEAR_DIRTY;
758 }
759 }
760
761 VolumeFcb->Flags |= VCB_IS_DIRTY;
762
763 FsRtlNotifyVolumeEvent(DeviceExt->FATFileObject, FSRTL_VOLUME_MOUNT);
764 FsRtlNotifyInitializeSync(&DeviceExt->NotifySync);
765 InitializeListHead(&DeviceExt->NotifyList);
766
767 DPRINT("Mount success\n");
768
769 Status = STATUS_SUCCESS;
770
771 ByeBye:
772 if (!NT_SUCCESS(Status))
773 {
774 /* Cleanup */
775 if (DeviceExt && DeviceExt->FATFileObject)
776 ObDereferenceObject (DeviceExt->FATFileObject);
777 if (DeviceExt && DeviceExt->SpareVPB)
778 ExFreePoolWithTag(DeviceExt->SpareVPB, TAG_VFAT);
779 if (Fcb)
780 vfatDestroyFCB(Fcb);
781 if (Ccb)
782 vfatDestroyCCB(Ccb);
783 if (DeviceObject)
784 IoDeleteDevice(DeviceObject);
785 }
786
787 return Status;
788 }
789
790
791 /*
792 * FUNCTION: Verify the filesystem
793 */
794 static
795 NTSTATUS
796 VfatVerify(
797 PVFAT_IRP_CONTEXT IrpContext)
798 {
799 PDEVICE_OBJECT DeviceToVerify;
800 NTSTATUS Status;
801 FATINFO FatInfo;
802 BOOLEAN RecognizedFS;
803 PDEVICE_EXTENSION DeviceExt;
804 BOOLEAN AllowRaw;
805 PVPB Vpb;
806 ULONG ChangeCount, BufSize = sizeof(ChangeCount);
807
808 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext);
809
810 DeviceToVerify = IrpContext->Stack->Parameters.VerifyVolume.DeviceObject;
811 DeviceExt = DeviceToVerify->DeviceExtension;
812 Vpb = IrpContext->Stack->Parameters.VerifyVolume.Vpb;
813 AllowRaw = BooleanFlagOn(IrpContext->Stack->Flags, SL_ALLOW_RAW_MOUNT);
814
815 if (!BooleanFlagOn(Vpb->RealDevice->Flags, DO_VERIFY_VOLUME))
816 {
817 DPRINT("Already verified\n");
818 return STATUS_SUCCESS;
819 }
820
821 Status = VfatBlockDeviceIoControl(DeviceExt->StorageDevice,
822 IOCTL_DISK_CHECK_VERIFY,
823 NULL,
824 0,
825 &ChangeCount,
826 &BufSize,
827 TRUE);
828 if (!NT_SUCCESS(Status) && Status != STATUS_VERIFY_REQUIRED)
829 {
830 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status);
831 Status = (AllowRaw ? STATUS_WRONG_VOLUME : Status);
832 }
833 else
834 {
835 Status = VfatHasFileSystem(DeviceExt->StorageDevice, &RecognizedFS, &FatInfo, TRUE);
836 if (!NT_SUCCESS(Status) || RecognizedFS == FALSE)
837 {
838 if (NT_SUCCESS(Status) || AllowRaw)
839 {
840 Status = STATUS_WRONG_VOLUME;
841 }
842 }
843 else if (sizeof(FATINFO) == RtlCompareMemory(&FatInfo, &DeviceExt->FatInfo, sizeof(FATINFO)))
844 {
845 WCHAR BufferU[MAXIMUM_VOLUME_LABEL_LENGTH / sizeof(WCHAR)];
846 UNICODE_STRING VolumeLabelU;
847 UNICODE_STRING VpbLabelU;
848
849 VolumeLabelU.Buffer = BufferU;
850 VolumeLabelU.Length = 0;
851 VolumeLabelU.MaximumLength = sizeof(BufferU);
852 Status = ReadVolumeLabel(DeviceExt->StorageDevice, FatInfo.rootStart * FatInfo.BytesPerSector, (FatInfo.FatType >= FATX16), &VolumeLabelU);
853 if (!NT_SUCCESS(Status))
854 {
855 if (AllowRaw)
856 {
857 Status = STATUS_WRONG_VOLUME;
858 }
859 }
860 else
861 {
862 VpbLabelU.Buffer = Vpb->VolumeLabel;
863 VpbLabelU.Length = Vpb->VolumeLabelLength;
864 VpbLabelU.MaximumLength = sizeof(Vpb->VolumeLabel);
865
866 if (RtlCompareUnicodeString(&VpbLabelU, &VolumeLabelU, FALSE) != 0)
867 {
868 Status = STATUS_WRONG_VOLUME;
869 }
870 else
871 {
872 DPRINT1("Same volume\n");
873 }
874 }
875 }
876 else
877 {
878 Status = STATUS_WRONG_VOLUME;
879 }
880 }
881
882 Vpb->RealDevice->Flags &= ~DO_VERIFY_VOLUME;
883
884 return Status;
885 }
886
887
888 static
889 NTSTATUS
890 VfatGetVolumeBitmap(
891 PVFAT_IRP_CONTEXT IrpContext)
892 {
893 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext);
894 return STATUS_INVALID_DEVICE_REQUEST;
895 }
896
897
898 static
899 NTSTATUS
900 VfatGetRetrievalPointers(
901 PVFAT_IRP_CONTEXT IrpContext)
902 {
903 PIO_STACK_LOCATION Stack;
904 LARGE_INTEGER Vcn;
905 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers;
906 PFILE_OBJECT FileObject;
907 ULONG MaxExtentCount;
908 PVFATFCB Fcb;
909 PDEVICE_EXTENSION DeviceExt;
910 ULONG FirstCluster;
911 ULONG CurrentCluster;
912 ULONG LastCluster;
913 NTSTATUS Status;
914
915 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext);
916
917 DeviceExt = IrpContext->DeviceExt;
918 FileObject = IrpContext->FileObject;
919 Stack = IrpContext->Stack;
920 if (Stack->Parameters.DeviceIoControl.InputBufferLength < sizeof(STARTING_VCN_INPUT_BUFFER) ||
921 Stack->Parameters.DeviceIoControl.Type3InputBuffer == NULL)
922 {
923 return STATUS_INVALID_PARAMETER;
924 }
925
926 if (IrpContext->Irp->UserBuffer == NULL ||
927 Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(RETRIEVAL_POINTERS_BUFFER))
928 {
929 return STATUS_BUFFER_TOO_SMALL;
930 }
931
932 Fcb = FileObject->FsContext;
933
934 ExAcquireResourceSharedLite(&Fcb->MainResource, TRUE);
935
936 Vcn = ((PSTARTING_VCN_INPUT_BUFFER)Stack->Parameters.DeviceIoControl.Type3InputBuffer)->StartingVcn;
937 RetrievalPointers = IrpContext->Irp->UserBuffer;
938
939 MaxExtentCount = ((Stack->Parameters.DeviceIoControl.OutputBufferLength - sizeof(RetrievalPointers->ExtentCount) - sizeof(RetrievalPointers->StartingVcn)) / sizeof(RetrievalPointers->Extents[0]));
940
941 if (Vcn.QuadPart >= Fcb->RFCB.AllocationSize.QuadPart / DeviceExt->FatInfo.BytesPerCluster)
942 {
943 Status = STATUS_INVALID_PARAMETER;
944 goto ByeBye;
945 }
946
947 CurrentCluster = FirstCluster = vfatDirEntryGetFirstCluster(DeviceExt, &Fcb->entry);
948 Status = OffsetToCluster(DeviceExt, FirstCluster,
949 Vcn.u.LowPart * DeviceExt->FatInfo.BytesPerCluster,
950 &CurrentCluster, FALSE);
951 if (!NT_SUCCESS(Status))
952 {
953 goto ByeBye;
954 }
955
956 RetrievalPointers->StartingVcn = Vcn;
957 RetrievalPointers->ExtentCount = 0;
958 RetrievalPointers->Extents[0].Lcn.u.HighPart = 0;
959 RetrievalPointers->Extents[0].Lcn.u.LowPart = CurrentCluster - 2;
960 LastCluster = 0;
961 while (CurrentCluster != 0xffffffff && RetrievalPointers->ExtentCount < MaxExtentCount)
962 {
963 LastCluster = CurrentCluster;
964 Status = NextCluster(DeviceExt, CurrentCluster, &CurrentCluster, FALSE);
965 Vcn.QuadPart++;
966 if (!NT_SUCCESS(Status))
967 {
968 goto ByeBye;
969 }
970
971 if (LastCluster + 1 != CurrentCluster)
972 {
973 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].NextVcn = Vcn;
974 RetrievalPointers->ExtentCount++;
975 if (RetrievalPointers->ExtentCount < MaxExtentCount)
976 {
977 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].Lcn.u.HighPart = 0;
978 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].Lcn.u.LowPart = CurrentCluster - 2;
979 }
980 }
981 }
982
983 IrpContext->Irp->IoStatus.Information = sizeof(RETRIEVAL_POINTERS_BUFFER) + (sizeof(RetrievalPointers->Extents[0]) * (RetrievalPointers->ExtentCount - 1));
984 Status = STATUS_SUCCESS;
985
986 ByeBye:
987 ExReleaseResourceLite(&Fcb->MainResource);
988
989 return Status;
990 }
991
992 static
993 NTSTATUS
994 VfatMoveFile(
995 PVFAT_IRP_CONTEXT IrpContext)
996 {
997 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext);
998 return STATUS_INVALID_DEVICE_REQUEST;
999 }
1000
1001 static
1002 NTSTATUS
1003 VfatIsVolumeDirty(
1004 PVFAT_IRP_CONTEXT IrpContext)
1005 {
1006 PULONG Flags;
1007
1008 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext);
1009
1010 if (IrpContext->Stack->Parameters.FileSystemControl.OutputBufferLength != sizeof(ULONG))
1011 return STATUS_INVALID_BUFFER_SIZE;
1012 else if (!IrpContext->Irp->AssociatedIrp.SystemBuffer)
1013 return STATUS_INVALID_USER_BUFFER;
1014
1015 Flags = (PULONG)IrpContext->Irp->AssociatedIrp.SystemBuffer;
1016 *Flags = 0;
1017
1018 if (BooleanFlagOn(IrpContext->DeviceExt->VolumeFcb->Flags, VCB_IS_DIRTY) &&
1019 !BooleanFlagOn(IrpContext->DeviceExt->VolumeFcb->Flags, VCB_CLEAR_DIRTY))
1020 {
1021 *Flags |= VOLUME_IS_DIRTY;
1022 }
1023
1024 IrpContext->Irp->IoStatus.Information = sizeof(ULONG);
1025
1026 return STATUS_SUCCESS;
1027 }
1028
1029 static
1030 NTSTATUS
1031 VfatMarkVolumeDirty(
1032 PVFAT_IRP_CONTEXT IrpContext)
1033 {
1034 ULONG eocMark;
1035 PDEVICE_EXTENSION DeviceExt;
1036 NTSTATUS Status = STATUS_SUCCESS;
1037
1038 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext);
1039 DeviceExt = IrpContext->DeviceExt;
1040
1041 if (!BooleanFlagOn(DeviceExt->VolumeFcb->Flags, VCB_IS_DIRTY))
1042 {
1043 Status = GetNextCluster(DeviceExt, 1, &eocMark);
1044 if (NT_SUCCESS(Status))
1045 {
1046 /* unset clean shutdown bit */
1047 eocMark &= ~DeviceExt->CleanShutBitMask;
1048 Status = WriteCluster(DeviceExt, 1, eocMark);
1049 }
1050 }
1051
1052 DeviceExt->VolumeFcb->Flags &= ~VCB_CLEAR_DIRTY;
1053
1054 return Status;
1055 }
1056
1057 static
1058 NTSTATUS
1059 VfatLockOrUnlockVolume(
1060 PVFAT_IRP_CONTEXT IrpContext,
1061 BOOLEAN Lock)
1062 {
1063 PFILE_OBJECT FileObject;
1064 PDEVICE_EXTENSION DeviceExt;
1065 PVFATFCB Fcb;
1066 PVPB Vpb;
1067
1068 DPRINT("VfatLockOrUnlockVolume(%p, %d)\n", IrpContext, Lock);
1069
1070 DeviceExt = IrpContext->DeviceExt;
1071 FileObject = IrpContext->FileObject;
1072 Fcb = FileObject->FsContext;
1073 Vpb = DeviceExt->FATFileObject->Vpb;
1074
1075 /* Only allow locking with the volume open */
1076 if (!BooleanFlagOn(Fcb->Flags, FCB_IS_VOLUME))
1077 {
1078 return STATUS_ACCESS_DENIED;
1079 }
1080
1081 /* Bail out if it's already in the demanded state */
1082 if ((BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED) && Lock) ||
1083 (!BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED) && !Lock))
1084 {
1085 return STATUS_ACCESS_DENIED;
1086 }
1087
1088 /* Bail out if it's already in the demanded state */
1089 if ((BooleanFlagOn(Vpb->Flags, VPB_LOCKED) && Lock) ||
1090 (!BooleanFlagOn(Vpb->Flags, VPB_LOCKED) && !Lock))
1091 {
1092 return STATUS_ACCESS_DENIED;
1093 }
1094
1095 /* Deny locking if we're not alone */
1096 if (Lock && DeviceExt->OpenHandleCount != 1)
1097 {
1098 return STATUS_ACCESS_DENIED;
1099 }
1100
1101 /* Finally, proceed */
1102 if (Lock)
1103 {
1104 DeviceExt->Flags |= VCB_VOLUME_LOCKED;
1105 Vpb->Flags |= VPB_LOCKED;
1106 }
1107 else
1108 {
1109 DeviceExt->Flags &= ~VCB_VOLUME_LOCKED;
1110 Vpb->Flags &= ~VPB_LOCKED;
1111 }
1112
1113 return STATUS_SUCCESS;
1114 }
1115
1116 static
1117 NTSTATUS
1118 VfatDismountVolume(
1119 PVFAT_IRP_CONTEXT IrpContext)
1120 {
1121 PDEVICE_EXTENSION DeviceExt;
1122 PLIST_ENTRY NextEntry;
1123 PVFATFCB Fcb;
1124 PFILE_OBJECT FileObject;
1125 ULONG eocMark;
1126 NTSTATUS Status;
1127
1128 DPRINT("VfatDismountVolume(%p)\n", IrpContext);
1129
1130 DeviceExt = IrpContext->DeviceExt;
1131 FileObject = IrpContext->FileObject;
1132
1133 /* We HAVE to be locked. Windows also allows dismount with no lock
1134 * but we're here mainly for 1st stage, so KISS
1135 */
1136 if (!BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED))
1137 {
1138 return STATUS_ACCESS_DENIED;
1139 }
1140
1141 /* Race condition? */
1142 if (BooleanFlagOn(DeviceExt->Flags, VCB_DISMOUNT_PENDING))
1143 {
1144 return STATUS_VOLUME_DISMOUNTED;
1145 }
1146
1147 /* Notify we'll dismount. Pass that point there's no reason we fail */
1148 FsRtlNotifyVolumeEvent(IrpContext->Stack->FileObject, FSRTL_VOLUME_DISMOUNT);
1149
1150 ExAcquireResourceExclusiveLite(&DeviceExt->FatResource, TRUE);
1151
1152 if (BooleanFlagOn(DeviceExt->VolumeFcb->Flags, VCB_CLEAR_DIRTY))
1153 {
1154 /* Set clean shutdown bit */
1155 Status = GetNextCluster(DeviceExt, 1, &eocMark);
1156 if (NT_SUCCESS(Status))
1157 {
1158 eocMark |= DeviceExt->CleanShutBitMask;
1159 if (NT_SUCCESS(WriteCluster(DeviceExt, 1, eocMark)))
1160 DeviceExt->VolumeFcb->Flags &= ~VCB_IS_DIRTY;
1161 }
1162 }
1163
1164 /* Flush volume & files */
1165 VfatFlushVolume(DeviceExt, (PVFATFCB)FileObject->FsContext);
1166
1167 /* Rebrowse the FCB in order to free them now */
1168 while (!IsListEmpty(&DeviceExt->FcbListHead))
1169 {
1170 NextEntry = RemoveHeadList(&DeviceExt->FcbListHead);
1171 Fcb = CONTAINING_RECORD(NextEntry, VFATFCB, FcbListEntry);
1172 vfatDestroyFCB(Fcb);
1173 }
1174
1175 /* Mark we're being dismounted */
1176 DeviceExt->Flags |= VCB_DISMOUNT_PENDING;
1177 #ifndef ENABLE_SWAPOUT
1178 IrpContext->DeviceObject->Vpb->Flags &= ~VPB_MOUNTED;
1179 #endif
1180
1181 ExReleaseResourceLite(&DeviceExt->FatResource);
1182
1183 /* Release a few resources and quit, we're done */
1184 ExDeleteResourceLite(&DeviceExt->DirResource);
1185 ExDeleteResourceLite(&DeviceExt->FatResource);
1186 ObDereferenceObject(DeviceExt->FATFileObject);
1187
1188 return STATUS_SUCCESS;
1189 }
1190
1191 /*
1192 * FUNCTION: File system control
1193 */
1194 NTSTATUS
1195 VfatFileSystemControl(
1196 PVFAT_IRP_CONTEXT IrpContext)
1197 {
1198 NTSTATUS Status;
1199
1200 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext);
1201
1202 ASSERT(IrpContext);
1203 ASSERT(IrpContext->Irp);
1204 ASSERT(IrpContext->Stack);
1205
1206 IrpContext->Irp->IoStatus.Information = 0;
1207
1208 switch (IrpContext->MinorFunction)
1209 {
1210 case IRP_MN_KERNEL_CALL:
1211 case IRP_MN_USER_FS_REQUEST:
1212 switch(IrpContext->Stack->Parameters.DeviceIoControl.IoControlCode)
1213 {
1214 case FSCTL_GET_VOLUME_BITMAP:
1215 Status = VfatGetVolumeBitmap(IrpContext);
1216 break;
1217
1218 case FSCTL_GET_RETRIEVAL_POINTERS:
1219 Status = VfatGetRetrievalPointers(IrpContext);
1220 break;
1221
1222 case FSCTL_MOVE_FILE:
1223 Status = VfatMoveFile(IrpContext);
1224 break;
1225
1226 case FSCTL_IS_VOLUME_DIRTY:
1227 Status = VfatIsVolumeDirty(IrpContext);
1228 break;
1229
1230 case FSCTL_MARK_VOLUME_DIRTY:
1231 Status = VfatMarkVolumeDirty(IrpContext);
1232 break;
1233
1234 case FSCTL_LOCK_VOLUME:
1235 Status = VfatLockOrUnlockVolume(IrpContext, TRUE);
1236 break;
1237
1238 case FSCTL_UNLOCK_VOLUME:
1239 Status = VfatLockOrUnlockVolume(IrpContext, FALSE);
1240 break;
1241
1242 case FSCTL_DISMOUNT_VOLUME:
1243 Status = VfatDismountVolume(IrpContext);
1244 break;
1245
1246 default:
1247 Status = STATUS_INVALID_DEVICE_REQUEST;
1248 }
1249 break;
1250
1251 case IRP_MN_MOUNT_VOLUME:
1252 Status = VfatMount(IrpContext);
1253 break;
1254
1255 case IRP_MN_VERIFY_VOLUME:
1256 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
1257 Status = VfatVerify(IrpContext);
1258 break;
1259
1260 default:
1261 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext->MinorFunction);
1262 Status = STATUS_INVALID_DEVICE_REQUEST;
1263 break;
1264 }
1265
1266 return Status;
1267 }