3 * Copyright (C) 2002 ReactOS Team
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.
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.
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.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: drivers/fs/vfat/fsctl.c
23 * PURPOSE: VFAT Filesystem
26 /* INCLUDES *****************************************************************/
33 extern VFAT_DISPATCH FatXDispatch
;
34 extern VFAT_DISPATCH FatDispatch
;
36 /* FUNCTIONS ****************************************************************/
38 #define CACHEPAGESIZE(pDeviceExt) ((pDeviceExt)->FatInfo.BytesPerCluster > PAGE_SIZE ? \
39 (pDeviceExt)->FatInfo.BytesPerCluster : PAGE_SIZE)
44 PDEVICE_OBJECT DeviceToMount
,
45 PBOOLEAN RecognizedFS
,
50 PARTITION_INFORMATION PartitionInfo
;
51 DISK_GEOMETRY DiskGeometry
;
56 struct _BootSector
* Boot
;
57 struct _BootSectorFatX
* BootFatX
;
58 BOOLEAN PartitionInfoIsValid
= FALSE
;
60 DPRINT("VfatHasFileSystem\n");
62 *RecognizedFS
= FALSE
;
64 Size
= sizeof(DISK_GEOMETRY
);
65 Status
= VfatBlockDeviceIoControl(DeviceToMount
,
66 IOCTL_DISK_GET_DRIVE_GEOMETRY
,
72 if (!NT_SUCCESS(Status
))
74 DPRINT("VfatBlockDeviceIoControl failed (%x)\n", Status
);
78 FatInfo
.FixedMedia
= DiskGeometry
.MediaType
== FixedMedia
? TRUE
: FALSE
;
79 if (DiskGeometry
.MediaType
== FixedMedia
|| DiskGeometry
.MediaType
== RemovableMedia
)
81 // We have found a hard disk
82 Size
= sizeof(PARTITION_INFORMATION
);
83 Status
= VfatBlockDeviceIoControl(DeviceToMount
,
84 IOCTL_DISK_GET_PARTITION_INFO
,
90 if (!NT_SUCCESS(Status
))
92 DPRINT("VfatBlockDeviceIoControl failed (%x)\n", Status
);
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
)
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
)
114 PartitionInfoIsValid
= TRUE
;
115 *RecognizedFS
= TRUE
;
118 else if (DiskGeometry
.MediaType
== RemovableMedia
&&
119 PartitionInfo
.PartitionNumber
> 0 &&
120 PartitionInfo
.StartingOffset
.QuadPart
== 0 &&
121 PartitionInfo
.PartitionLength
.QuadPart
> 0)
123 /* This is possible a removable media formated as super floppy */
124 PartitionInfoIsValid
= TRUE
;
125 *RecognizedFS
= TRUE
;
130 *RecognizedFS
= TRUE
;
135 Boot
= ExAllocatePoolWithTag(NonPagedPool
, DiskGeometry
.BytesPerSector
, TAG_VFAT
);
138 return STATUS_INSUFFICIENT_RESOURCES
;
143 /* Try to recognize FAT12/FAT16/FAT32 partitions */
144 Status
= VfatReadDisk(DeviceToMount
, &Offset
, DiskGeometry
.BytesPerSector
, (PUCHAR
) Boot
, Override
);
145 if (NT_SUCCESS(Status
))
147 if (Boot
->Signatur1
!= 0xaa55)
149 *RecognizedFS
= FALSE
;
153 Boot
->BytesPerSector
!= 512 &&
154 Boot
->BytesPerSector
!= 1024 &&
155 Boot
->BytesPerSector
!= 2048 &&
156 Boot
->BytesPerSector
!= 4096)
158 DPRINT1("BytesPerSector %u\n", Boot
->BytesPerSector
);
159 *RecognizedFS
= FALSE
;
163 Boot
->FATCount
!= 1 &&
166 DPRINT1("FATCount %u\n", Boot
->FATCount
);
167 *RecognizedFS
= FALSE
;
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 &&
181 DPRINT1("Media %02x\n", Boot
->Media
);
182 *RecognizedFS
= FALSE
;
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)
195 DPRINT1("SectorsPerCluster %02x\n", Boot
->SectorsPerCluster
);
196 *RecognizedFS
= FALSE
;
200 Boot
->BytesPerSector
* Boot
->SectorsPerCluster
> 32 * 1024)
202 DPRINT1("ClusterSize %dx\n", Boot
->BytesPerSector
* Boot
->SectorsPerCluster
);
203 *RecognizedFS
= FALSE
;
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)
224 FatInfo
.FatType
= FAT12
;
225 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
226 RtlCopyMemory(&FatInfo
.VolumeLabel
, &Boot
->VolumeLabel
, sizeof(FatInfo
.VolumeLabel
));
228 else if (FatInfo
.NumberOfClusters
>= 65525)
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 FatInfo
.FSInfoSector
= ((struct _BootSector32
*) Boot
)->FSInfoSector
;
236 RtlCopyMemory(&FatInfo
.VolumeLabel
, &((struct _BootSector32
*)Boot
)->VolumeLabel
, sizeof(FatInfo
.VolumeLabel
));
241 FatInfo
.FatType
= FAT16
;
242 FatInfo
.RootCluster
= FatInfo
.rootStart
/ FatInfo
.SectorsPerCluster
;
243 RtlCopyMemory(&FatInfo
.VolumeLabel
, &Boot
->VolumeLabel
, sizeof(FatInfo
.VolumeLabel
));
246 if (PartitionInfoIsValid
&&
247 FatInfo
.Sectors
> PartitionInfo
.PartitionLength
.QuadPart
/ FatInfo
.BytesPerSector
)
249 *RecognizedFS
= FALSE
;
252 if (pFatInfo
&& *RecognizedFS
)
262 if (!*RecognizedFS
&& PartitionInfoIsValid
)
264 BootFatX
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(struct _BootSectorFatX
), TAG_VFAT
);
265 if (BootFatX
== NULL
)
268 return STATUS_INSUFFICIENT_RESOURCES
;
273 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
274 Status
= VfatReadDisk(DeviceToMount
, &Offset
, sizeof(struct _BootSectorFatX
), (PUCHAR
) BootFatX
, Override
);
275 if (NT_SUCCESS(Status
))
277 *RecognizedFS
= TRUE
;
278 if (BootFatX
->SysType
[0] != 'F' ||
279 BootFatX
->SysType
[1] != 'A' ||
280 BootFatX
->SysType
[2] != 'T' ||
281 BootFatX
->SysType
[3] != 'X')
283 DPRINT1("SysType %c%c%c%c\n", BootFatX
->SysType
[0], BootFatX
->SysType
[1], BootFatX
->SysType
[2], BootFatX
->SysType
[3]);
288 BootFatX
->SectorsPerCluster
!= 1 &&
289 BootFatX
->SectorsPerCluster
!= 2 &&
290 BootFatX
->SectorsPerCluster
!= 4 &&
291 BootFatX
->SectorsPerCluster
!= 8 &&
292 BootFatX
->SectorsPerCluster
!= 16 &&
293 BootFatX
->SectorsPerCluster
!= 32 &&
294 BootFatX
->SectorsPerCluster
!= 64 &&
295 BootFatX
->SectorsPerCluster
!= 128)
297 DPRINT1("SectorsPerCluster %lu\n", BootFatX
->SectorsPerCluster
);
303 FatInfo
.BytesPerSector
= DiskGeometry
.BytesPerSector
;
304 FatInfo
.SectorsPerCluster
= BootFatX
->SectorsPerCluster
;
305 FatInfo
.rootDirectorySectors
= BootFatX
->SectorsPerCluster
;
306 FatInfo
.BytesPerCluster
= BootFatX
->SectorsPerCluster
* DiskGeometry
.BytesPerSector
;
307 FatInfo
.Sectors
= (ULONG
)(PartitionInfo
.PartitionLength
.QuadPart
/ DiskGeometry
.BytesPerSector
);
308 if (FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
< 65525)
311 FatInfo
.FatType
= FATX16
;
316 FatInfo
.FatType
= FATX32
;
318 FatInfo
.VolumeID
= BootFatX
->VolumeID
;
319 FatInfo
.FATStart
= sizeof(struct _BootSectorFatX
) / DiskGeometry
.BytesPerSector
;
320 FatInfo
.FATCount
= BootFatX
->FATCount
;
322 ROUND_UP(FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
* (FatInfo
.FatType
== FATX16
? 2 : 4), 4096) /
323 FatInfo
.BytesPerSector
;
324 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
325 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
326 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
327 FatInfo
.NumberOfClusters
= (FatInfo
.Sectors
- FatInfo
.dataStart
) / FatInfo
.SectorsPerCluster
;
329 if (pFatInfo
&& *RecognizedFS
)
335 ExFreePool(BootFatX
);
338 DPRINT("VfatHasFileSystem done\n");
343 * FUNCTION: Read the volume label
344 * WARNING: Read this comment carefully before using it (and using it wrong)
345 * Device parameter is expected to be the lower DO is start isn't 0
346 * otherwise, it is expected to be the VCB is start is 0
347 * Start parameter is expected to be, in bytes, the beginning of the root start.
348 * Set it to 0 if you wish to use the associated FCB with caching.
349 * In that specific case, Device parameter is expected to be the VCB!
350 * VolumeLabel parameter is expected to be a preallocated UNICODE_STRING (ie, with buffer)
351 * Its buffer has to be able to contain MAXIMUM_VOLUME_LABEL_LENGTH bytes
359 PUNICODE_STRING VolumeLabel
)
361 PDEVICE_EXTENSION DeviceExt
;
362 PDEVICE_OBJECT DeviceObject
;
363 PVOID Context
= NULL
;
367 LARGE_INTEGER FileOffset
;
369 ULONG EntriesPerPage
;
371 BOOLEAN NoCache
= (Start
!= 0);
373 NTSTATUS Status
= STATUS_SUCCESS
;
377 SizeDirEntry
= sizeof(FATX_DIR_ENTRY
);
378 EntriesPerPage
= FATX_ENTRIES_PER_PAGE
;
382 SizeDirEntry
= sizeof(FAT_DIR_ENTRY
);
383 EntriesPerPage
= FAT_ENTRIES_PER_PAGE
;
386 FileOffset
.QuadPart
= Start
;
391 /* FIXME: Check we really have a VCB
395 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
, TRUE
);
396 pFcb
= vfatOpenRootFCB(DeviceExt
);
397 ExReleaseResourceLite(&DeviceExt
->DirResource
);
401 CcMapData(pFcb
->FileObject
, &FileOffset
, SizeDirEntry
, MAP_WAIT
, &Context
, (PVOID
*)&Entry
);
403 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
405 Status
= _SEH2_GetExceptionCode();
411 DeviceObject
= Device
;
413 ASSERT(DeviceObject
->Type
== 3);
415 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, PAGE_SIZE
, TAG_VFAT
);
418 Status
= VfatReadDisk(DeviceObject
, &FileOffset
, PAGE_SIZE
, (PUCHAR
)Buffer
, TRUE
);
419 if (!NT_SUCCESS(Status
))
421 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
430 Status
= STATUS_INSUFFICIENT_RESOURCES
;
434 if (NT_SUCCESS(Status
))
438 if (ENTRY_VOLUME(IsFatX
, Entry
))
440 /* copy volume label */
443 StringO
.Buffer
= (PCHAR
)Entry
->FatX
.Filename
;
444 StringO
.MaximumLength
= StringO
.Length
= Entry
->FatX
.FilenameLength
;
445 RtlOemStringToUnicodeString(VolumeLabel
, &StringO
, FALSE
);
449 vfat8Dot3ToString(&Entry
->Fat
, VolumeLabel
);
453 if (ENTRY_END(IsFatX
, Entry
))
458 Entry
= (PDIR_ENTRY
)((ULONG_PTR
)Entry
+ SizeDirEntry
);
459 if ((DirIndex
% EntriesPerPage
) == 0)
461 FileOffset
.u
.LowPart
+= PAGE_SIZE
;
465 CcUnpinData(Context
);
469 CcMapData(pFcb
->FileObject
, &FileOffset
, SizeDirEntry
, MAP_WAIT
, &Context
, (PVOID
*)&Entry
);
471 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
473 Status
= _SEH2_GetExceptionCode();
476 if (!NT_SUCCESS(Status
))
484 Status
= VfatReadDisk(DeviceObject
, &FileOffset
, PAGE_SIZE
, (PUCHAR
)Buffer
, TRUE
);
485 if (!NT_SUCCESS(Status
))
495 CcUnpinData(Context
);
499 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
505 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
, TRUE
);
506 vfatReleaseFCB(DeviceExt
, pFcb
);
507 ExReleaseResourceLite(&DeviceExt
->DirResource
);
510 return STATUS_SUCCESS
;
515 * FUNCTION: Mount the filesystem
520 PVFAT_IRP_CONTEXT IrpContext
)
522 PDEVICE_OBJECT DeviceObject
= NULL
;
523 PDEVICE_EXTENSION DeviceExt
= NULL
;
524 BOOLEAN RecognizedFS
;
527 PVFATFCB VolumeFcb
= NULL
;
529 PDEVICE_OBJECT DeviceToMount
;
531 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\$$Fat$$");
532 UNICODE_STRING VolumeNameU
= RTL_CONSTANT_STRING(L
"\\$$Volume$$");
533 UNICODE_STRING VolumeLabelU
;
539 DPRINT("VfatMount(IrpContext %p)\n", IrpContext
);
543 if (IrpContext
->DeviceObject
!= VfatGlobalData
->DeviceObject
)
545 Status
= STATUS_INVALID_DEVICE_REQUEST
;
549 DeviceToMount
= IrpContext
->Stack
->Parameters
.MountVolume
.DeviceObject
;
550 Vpb
= IrpContext
->Stack
->Parameters
.MountVolume
.Vpb
;
552 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &FatInfo
, FALSE
);
553 if (!NT_SUCCESS(Status
))
558 if (RecognizedFS
== FALSE
)
560 DPRINT("VFAT: Unrecognized Volume\n");
561 Status
= STATUS_UNRECOGNIZED_VOLUME
;
565 /* Use prime numbers for the table size */
566 if (FatInfo
.FatType
== FAT12
)
568 HashTableSize
= 4099; // 4096 = 4 * 1024
570 else if (FatInfo
.FatType
== FAT16
||
571 FatInfo
.FatType
== FATX16
)
573 HashTableSize
= 16411; // 16384 = 16 * 1024
577 HashTableSize
= 65537; // 65536 = 64 * 1024;
579 DPRINT("VFAT: Recognized volume\n");
580 Status
= IoCreateDevice(VfatGlobalData
->DriverObject
,
581 ROUND_UP(sizeof (DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
,
583 FILE_DEVICE_DISK_FILE_SYSTEM
,
584 DeviceToMount
->Characteristics
,
587 if (!NT_SUCCESS(Status
))
592 DeviceExt
= DeviceObject
->DeviceExtension
;
593 RtlZeroMemory(DeviceExt
, ROUND_UP(sizeof(DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
);
594 DeviceExt
->FcbHashTable
= (HASHENTRY
**)((ULONG_PTR
)DeviceExt
+ ROUND_UP(sizeof(DEVICE_EXTENSION
), sizeof(ULONG
)));
595 DeviceExt
->HashTableSize
= HashTableSize
;
596 DeviceExt
->VolumeDevice
= DeviceObject
;
598 /* use same vpb as device disk */
599 DeviceObject
->Vpb
= Vpb
;
600 DeviceToMount
->Vpb
= Vpb
;
602 RtlCopyMemory(&DeviceExt
->FatInfo
, &FatInfo
, sizeof(FATINFO
));
604 DPRINT("BytesPerSector: %u\n", DeviceExt
->FatInfo
.BytesPerSector
);
605 DPRINT("SectorsPerCluster: %u\n", DeviceExt
->FatInfo
.SectorsPerCluster
);
606 DPRINT("FATCount: %u\n", DeviceExt
->FatInfo
.FATCount
);
607 DPRINT("FATSectors: %u\n", DeviceExt
->FatInfo
.FATSectors
);
608 DPRINT("RootStart: %u\n", DeviceExt
->FatInfo
.rootStart
);
609 DPRINT("DataStart: %u\n", DeviceExt
->FatInfo
.dataStart
);
610 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
612 DPRINT("RootCluster: %u\n", DeviceExt
->FatInfo
.RootCluster
);
615 switch (DeviceExt
->FatInfo
.FatType
)
618 DeviceExt
->GetNextCluster
= FAT12GetNextCluster
;
619 DeviceExt
->FindAndMarkAvailableCluster
= FAT12FindAndMarkAvailableCluster
;
620 DeviceExt
->WriteCluster
= FAT12WriteCluster
;
621 /* We don't define dirty bit functions here
622 * FAT12 doesn't have such bit and they won't get called
628 DeviceExt
->GetNextCluster
= FAT16GetNextCluster
;
629 DeviceExt
->FindAndMarkAvailableCluster
= FAT16FindAndMarkAvailableCluster
;
630 DeviceExt
->WriteCluster
= FAT16WriteCluster
;
631 DeviceExt
->GetDirtyStatus
= FAT16GetDirtyStatus
;
632 DeviceExt
->SetDirtyStatus
= FAT16SetDirtyStatus
;
637 DeviceExt
->GetNextCluster
= FAT32GetNextCluster
;
638 DeviceExt
->FindAndMarkAvailableCluster
= FAT32FindAndMarkAvailableCluster
;
639 DeviceExt
->WriteCluster
= FAT32WriteCluster
;
640 DeviceExt
->GetDirtyStatus
= FAT32GetDirtyStatus
;
641 DeviceExt
->SetDirtyStatus
= FAT32SetDirtyStatus
;
645 if (DeviceExt
->FatInfo
.FatType
== FATX16
||
646 DeviceExt
->FatInfo
.FatType
== FATX32
)
648 DeviceExt
->Flags
|= VCB_IS_FATX
;
649 DeviceExt
->BaseDateYear
= 2000;
650 RtlCopyMemory(&DeviceExt
->Dispatch
, &FatXDispatch
, sizeof(VFAT_DISPATCH
));
654 DeviceExt
->BaseDateYear
= 1980;
655 RtlCopyMemory(&DeviceExt
->Dispatch
, &FatDispatch
, sizeof(VFAT_DISPATCH
));
658 DeviceExt
->StorageDevice
= DeviceToMount
;
659 DeviceExt
->StorageDevice
->Vpb
->DeviceObject
= DeviceObject
;
660 DeviceExt
->StorageDevice
->Vpb
->RealDevice
= DeviceExt
->StorageDevice
;
661 DeviceExt
->StorageDevice
->Vpb
->Flags
|= VPB_MOUNTED
;
662 DeviceObject
->StackSize
= DeviceExt
->StorageDevice
->StackSize
+ 1;
663 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
665 DPRINT("FsDeviceObject %p\n", DeviceObject
);
667 /* Initialize this resource early ... it's used in VfatCleanup */
668 ExInitializeResourceLite(&DeviceExt
->DirResource
);
670 DeviceExt
->IoVPB
= DeviceObject
->Vpb
;
671 DeviceExt
->SpareVPB
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(VPB
), TAG_VFAT
);
672 if (DeviceExt
->SpareVPB
== NULL
)
674 Status
= STATUS_INSUFFICIENT_RESOURCES
;
678 DeviceExt
->Statistics
= ExAllocatePoolWithTag(NonPagedPool
,
679 sizeof(STATISTICS
) * VfatGlobalData
->NumberProcessors
,
681 if (DeviceExt
->Statistics
== NULL
)
683 Status
= STATUS_INSUFFICIENT_RESOURCES
;
687 RtlZeroMemory(DeviceExt
->Statistics
, sizeof(STATISTICS
) * VfatGlobalData
->NumberProcessors
);
688 for (i
= 0; i
< VfatGlobalData
->NumberProcessors
; ++i
)
690 DeviceExt
->Statistics
[i
].Base
.FileSystemType
= FILESYSTEM_STATISTICS_TYPE_FAT
;
691 DeviceExt
->Statistics
[i
].Base
.Version
= 1;
692 DeviceExt
->Statistics
[i
].Base
.SizeOfCompleteStructure
= sizeof(STATISTICS
);
695 DeviceExt
->FATFileObject
= IoCreateStreamFileObject(NULL
, DeviceExt
->StorageDevice
);
696 Fcb
= vfatNewFCB(DeviceExt
, &NameU
);
699 Status
= STATUS_INSUFFICIENT_RESOURCES
;
703 Ccb
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
706 Status
= STATUS_INSUFFICIENT_RESOURCES
;
710 RtlZeroMemory(Ccb
, sizeof (VFATCCB
));
711 DeviceExt
->FATFileObject
->FsContext
= Fcb
;
712 DeviceExt
->FATFileObject
->FsContext2
= Ccb
;
713 DeviceExt
->FATFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
714 DeviceExt
->FATFileObject
->PrivateCacheMap
= NULL
;
715 DeviceExt
->FATFileObject
->Vpb
= DeviceObject
->Vpb
;
716 Fcb
->FileObject
= DeviceExt
->FATFileObject
;
718 Fcb
->Flags
|= FCB_IS_FAT
;
720 Fcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.FATSectors
* DeviceExt
->FatInfo
.BytesPerSector
;
721 Fcb
->RFCB
.ValidDataLength
= Fcb
->RFCB
.FileSize
;
722 Fcb
->RFCB
.AllocationSize
= Fcb
->RFCB
.FileSize
;
726 CcInitializeCacheMap(DeviceExt
->FATFileObject
,
727 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
729 &VfatGlobalData
->CacheMgrCallbacks
,
732 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
734 Status
= _SEH2_GetExceptionCode();
739 DeviceExt
->LastAvailableCluster
= 2;
740 CountAvailableClusters(DeviceExt
, NULL
);
741 ExInitializeResourceLite(&DeviceExt
->FatResource
);
743 InitializeListHead(&DeviceExt
->FcbListHead
);
745 VolumeFcb
= vfatNewFCB(DeviceExt
, &VolumeNameU
);
746 if (VolumeFcb
== NULL
)
748 Status
= STATUS_INSUFFICIENT_RESOURCES
;
752 VolumeFcb
->Flags
= FCB_IS_VOLUME
;
753 VolumeFcb
->RFCB
.FileSize
.QuadPart
= (LONGLONG
) DeviceExt
->FatInfo
.Sectors
* DeviceExt
->FatInfo
.BytesPerSector
;
754 VolumeFcb
->RFCB
.ValidDataLength
= VolumeFcb
->RFCB
.FileSize
;
755 VolumeFcb
->RFCB
.AllocationSize
= VolumeFcb
->RFCB
.FileSize
;
756 DeviceExt
->VolumeFcb
= VolumeFcb
;
758 ExAcquireResourceExclusiveLite(&VfatGlobalData
->VolumeListLock
, TRUE
);
759 InsertHeadList(&VfatGlobalData
->VolumeListHead
, &DeviceExt
->VolumeListEntry
);
760 ExReleaseResourceLite(&VfatGlobalData
->VolumeListLock
);
762 /* read serial number */
763 DeviceObject
->Vpb
->SerialNumber
= DeviceExt
->FatInfo
.VolumeID
;
765 /* read volume label */
766 VolumeLabelU
.Buffer
= DeviceObject
->Vpb
->VolumeLabel
;
767 VolumeLabelU
.Length
= 0;
768 VolumeLabelU
.MaximumLength
= sizeof(DeviceObject
->Vpb
->VolumeLabel
);
769 ReadVolumeLabel(DeviceExt
, 0, vfatVolumeIsFatX(DeviceExt
), &VolumeLabelU
);
770 Vpb
->VolumeLabelLength
= VolumeLabelU
.Length
;
772 /* read dirty bit status */
773 Status
= GetDirtyStatus(DeviceExt
, &Dirty
);
774 if (NT_SUCCESS(Status
))
776 /* The volume wasn't dirty, it was properly dismounted */
779 /* Mark it dirty now! */
780 SetDirtyStatus(DeviceExt
, TRUE
);
781 VolumeFcb
->Flags
|= VCB_CLEAR_DIRTY
;
785 DPRINT1("Mounting a dirty volume\n");
789 VolumeFcb
->Flags
|= VCB_IS_DIRTY
;
790 if (BooleanFlagOn(Vpb
->RealDevice
->Flags
, DO_SYSTEM_BOOT_PARTITION
))
792 SetFlag(DeviceExt
->Flags
, VCB_IS_SYS_OR_HAS_PAGE
);
795 FsRtlNotifyVolumeEvent(DeviceExt
->FATFileObject
, FSRTL_VOLUME_MOUNT
);
796 FsRtlNotifyInitializeSync(&DeviceExt
->NotifySync
);
797 InitializeListHead(&DeviceExt
->NotifyList
);
799 DPRINT("Mount success\n");
801 Status
= STATUS_SUCCESS
;
804 if (!NT_SUCCESS(Status
))
807 if (DeviceExt
&& DeviceExt
->FATFileObject
)
808 ObDereferenceObject (DeviceExt
->FATFileObject
);
809 if (DeviceExt
&& DeviceExt
->SpareVPB
)
810 ExFreePoolWithTag(DeviceExt
->SpareVPB
, TAG_VFAT
);
811 if (DeviceExt
&& DeviceExt
->Statistics
)
812 ExFreePoolWithTag(DeviceExt
->Statistics
, TAG_STATS
);
818 IoDeleteDevice(DeviceObject
);
826 * FUNCTION: Verify the filesystem
831 PVFAT_IRP_CONTEXT IrpContext
)
833 PDEVICE_OBJECT DeviceToVerify
;
836 BOOLEAN RecognizedFS
;
837 PDEVICE_EXTENSION DeviceExt
;
840 ULONG ChangeCount
, BufSize
= sizeof(ChangeCount
);
842 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext
);
844 DeviceToVerify
= IrpContext
->Stack
->Parameters
.VerifyVolume
.DeviceObject
;
845 DeviceExt
= DeviceToVerify
->DeviceExtension
;
846 Vpb
= IrpContext
->Stack
->Parameters
.VerifyVolume
.Vpb
;
847 AllowRaw
= BooleanFlagOn(IrpContext
->Stack
->Flags
, SL_ALLOW_RAW_MOUNT
);
849 if (!BooleanFlagOn(Vpb
->RealDevice
->Flags
, DO_VERIFY_VOLUME
))
851 DPRINT("Already verified\n");
852 return STATUS_SUCCESS
;
855 Status
= VfatBlockDeviceIoControl(DeviceExt
->StorageDevice
,
856 IOCTL_DISK_CHECK_VERIFY
,
862 if (!NT_SUCCESS(Status
) && Status
!= STATUS_VERIFY_REQUIRED
)
864 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status
);
865 Status
= (AllowRaw
? STATUS_WRONG_VOLUME
: Status
);
869 Status
= VfatHasFileSystem(DeviceExt
->StorageDevice
, &RecognizedFS
, &FatInfo
, TRUE
);
870 if (!NT_SUCCESS(Status
) || RecognizedFS
== FALSE
)
872 if (NT_SUCCESS(Status
) || AllowRaw
)
874 Status
= STATUS_WRONG_VOLUME
;
877 else if (sizeof(FATINFO
) == RtlCompareMemory(&FatInfo
, &DeviceExt
->FatInfo
, sizeof(FATINFO
)))
879 WCHAR BufferU
[MAXIMUM_VOLUME_LABEL_LENGTH
/ sizeof(WCHAR
)];
880 UNICODE_STRING VolumeLabelU
;
881 UNICODE_STRING VpbLabelU
;
883 VolumeLabelU
.Buffer
= BufferU
;
884 VolumeLabelU
.Length
= 0;
885 VolumeLabelU
.MaximumLength
= sizeof(BufferU
);
886 Status
= ReadVolumeLabel(DeviceExt
->StorageDevice
, FatInfo
.rootStart
* FatInfo
.BytesPerSector
, (FatInfo
.FatType
>= FATX16
), &VolumeLabelU
);
887 if (!NT_SUCCESS(Status
))
891 Status
= STATUS_WRONG_VOLUME
;
896 VpbLabelU
.Buffer
= Vpb
->VolumeLabel
;
897 VpbLabelU
.Length
= Vpb
->VolumeLabelLength
;
898 VpbLabelU
.MaximumLength
= sizeof(Vpb
->VolumeLabel
);
900 if (RtlCompareUnicodeString(&VpbLabelU
, &VolumeLabelU
, FALSE
) != 0)
902 Status
= STATUS_WRONG_VOLUME
;
906 DPRINT1("Same volume\n");
912 Status
= STATUS_WRONG_VOLUME
;
916 Vpb
->RealDevice
->Flags
&= ~DO_VERIFY_VOLUME
;
925 PVFAT_IRP_CONTEXT IrpContext
)
927 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext
);
928 return STATUS_INVALID_DEVICE_REQUEST
;
934 VfatGetRetrievalPointers(
935 PVFAT_IRP_CONTEXT IrpContext
)
937 PIO_STACK_LOCATION Stack
;
939 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers
;
940 PFILE_OBJECT FileObject
;
941 ULONG MaxExtentCount
;
943 PDEVICE_EXTENSION DeviceExt
;
945 ULONG CurrentCluster
;
949 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext
);
951 DeviceExt
= IrpContext
->DeviceExt
;
952 FileObject
= IrpContext
->FileObject
;
953 Stack
= IrpContext
->Stack
;
954 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(STARTING_VCN_INPUT_BUFFER
) ||
955 Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
== NULL
)
957 return STATUS_INVALID_PARAMETER
;
960 if (IrpContext
->Irp
->UserBuffer
== NULL
||
961 Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(RETRIEVAL_POINTERS_BUFFER
))
963 return STATUS_BUFFER_TOO_SMALL
;
966 Fcb
= FileObject
->FsContext
;
968 ExAcquireResourceSharedLite(&Fcb
->MainResource
, TRUE
);
970 Vcn
= ((PSTARTING_VCN_INPUT_BUFFER
)Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
)->StartingVcn
;
971 RetrievalPointers
= IrpContext
->Irp
->UserBuffer
;
973 MaxExtentCount
= ((Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
- sizeof(RetrievalPointers
->ExtentCount
) - sizeof(RetrievalPointers
->StartingVcn
)) / sizeof(RetrievalPointers
->Extents
[0]));
975 if (Vcn
.QuadPart
>= Fcb
->RFCB
.AllocationSize
.QuadPart
/ DeviceExt
->FatInfo
.BytesPerCluster
)
977 Status
= STATUS_INVALID_PARAMETER
;
981 CurrentCluster
= FirstCluster
= vfatDirEntryGetFirstCluster(DeviceExt
, &Fcb
->entry
);
982 Status
= OffsetToCluster(DeviceExt
, FirstCluster
,
983 Vcn
.u
.LowPart
* DeviceExt
->FatInfo
.BytesPerCluster
,
984 &CurrentCluster
, FALSE
);
985 if (!NT_SUCCESS(Status
))
990 RetrievalPointers
->StartingVcn
= Vcn
;
991 RetrievalPointers
->ExtentCount
= 0;
992 RetrievalPointers
->Extents
[0].Lcn
.u
.HighPart
= 0;
993 RetrievalPointers
->Extents
[0].Lcn
.u
.LowPart
= CurrentCluster
- 2;
995 while (CurrentCluster
!= 0xffffffff && RetrievalPointers
->ExtentCount
< MaxExtentCount
)
997 LastCluster
= CurrentCluster
;
998 Status
= NextCluster(DeviceExt
, CurrentCluster
, &CurrentCluster
, FALSE
);
1000 if (!NT_SUCCESS(Status
))
1005 if (LastCluster
+ 1 != CurrentCluster
)
1007 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].NextVcn
= Vcn
;
1008 RetrievalPointers
->ExtentCount
++;
1009 if (RetrievalPointers
->ExtentCount
< MaxExtentCount
)
1011 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.HighPart
= 0;
1012 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.LowPart
= CurrentCluster
- 2;
1017 IrpContext
->Irp
->IoStatus
.Information
= sizeof(RETRIEVAL_POINTERS_BUFFER
) + (sizeof(RetrievalPointers
->Extents
[0]) * (RetrievalPointers
->ExtentCount
- 1));
1018 Status
= STATUS_SUCCESS
;
1021 ExReleaseResourceLite(&Fcb
->MainResource
);
1029 PVFAT_IRP_CONTEXT IrpContext
)
1031 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext
);
1032 return STATUS_INVALID_DEVICE_REQUEST
;
1038 PVFAT_IRP_CONTEXT IrpContext
)
1042 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext
);
1044 if (IrpContext
->Stack
->Parameters
.FileSystemControl
.OutputBufferLength
!= sizeof(ULONG
))
1045 return STATUS_INVALID_BUFFER_SIZE
;
1046 else if (!IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
)
1047 return STATUS_INVALID_USER_BUFFER
;
1049 Flags
= (PULONG
)IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
;
1052 if (BooleanFlagOn(IrpContext
->DeviceExt
->VolumeFcb
->Flags
, VCB_IS_DIRTY
) &&
1053 !BooleanFlagOn(IrpContext
->DeviceExt
->VolumeFcb
->Flags
, VCB_CLEAR_DIRTY
))
1055 *Flags
|= VOLUME_IS_DIRTY
;
1058 IrpContext
->Irp
->IoStatus
.Information
= sizeof(ULONG
);
1060 return STATUS_SUCCESS
;
1065 VfatMarkVolumeDirty(
1066 PVFAT_IRP_CONTEXT IrpContext
)
1068 PDEVICE_EXTENSION DeviceExt
;
1069 NTSTATUS Status
= STATUS_SUCCESS
;
1071 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext
);
1072 DeviceExt
= IrpContext
->DeviceExt
;
1074 if (!BooleanFlagOn(DeviceExt
->VolumeFcb
->Flags
, VCB_IS_DIRTY
))
1076 Status
= SetDirtyStatus(DeviceExt
, TRUE
);
1079 DeviceExt
->VolumeFcb
->Flags
&= ~VCB_CLEAR_DIRTY
;
1086 VfatLockOrUnlockVolume(
1087 PVFAT_IRP_CONTEXT IrpContext
,
1090 PFILE_OBJECT FileObject
;
1091 PDEVICE_EXTENSION DeviceExt
;
1095 DPRINT("VfatLockOrUnlockVolume(%p, %d)\n", IrpContext
, Lock
);
1097 DeviceExt
= IrpContext
->DeviceExt
;
1098 FileObject
= IrpContext
->FileObject
;
1099 Fcb
= FileObject
->FsContext
;
1100 Vpb
= DeviceExt
->FATFileObject
->Vpb
;
1102 /* Only allow locking with the volume open */
1103 if (!BooleanFlagOn(Fcb
->Flags
, FCB_IS_VOLUME
))
1105 return STATUS_ACCESS_DENIED
;
1108 /* Bail out if it's already in the demanded state */
1109 if ((BooleanFlagOn(DeviceExt
->Flags
, VCB_VOLUME_LOCKED
) && Lock
) ||
1110 (!BooleanFlagOn(DeviceExt
->Flags
, VCB_VOLUME_LOCKED
) && !Lock
))
1112 return STATUS_ACCESS_DENIED
;
1115 /* Bail out if it's already in the demanded state */
1116 if ((BooleanFlagOn(Vpb
->Flags
, VPB_LOCKED
) && Lock
) ||
1117 (!BooleanFlagOn(Vpb
->Flags
, VPB_LOCKED
) && !Lock
))
1119 return STATUS_ACCESS_DENIED
;
1122 /* Deny locking if we're not alone */
1123 if (Lock
&& DeviceExt
->OpenHandleCount
!= 1)
1125 PLIST_ENTRY ListEntry
;
1128 /* FIXME: Hack that allows locking the system volume on
1129 * boot so that autochk can run properly
1130 * That hack is, on purpose, really restrictive
1131 * it will only allow locking with two directories
1132 * open: current directory of smss and autochk.
1134 BOOLEAN ForceLock
= TRUE
;
1135 ULONG HandleCount
= 0;
1137 /* Only allow boot volume */
1138 if (BooleanFlagOn(DeviceExt
->Flags
, VCB_IS_SYS_OR_HAS_PAGE
))
1140 /* We'll browse all the FCB */
1141 ListEntry
= DeviceExt
->FcbListHead
.Flink
;
1142 while (ListEntry
!= &DeviceExt
->FcbListHead
)
1144 Fcb
= CONTAINING_RECORD(ListEntry
, VFATFCB
, FcbListEntry
);
1145 ListEntry
= ListEntry
->Flink
;
1147 /* If no handle: that FCB is no problem for locking
1150 if (Fcb
->OpenHandleCount
== 0)
1155 /* Not a dir? We're no longer at boot */
1156 if (!vfatFCBIsDirectory(Fcb
))
1162 /* If we have cached initialized and several handles, we're
1163 not in the boot case
1165 if (Fcb
->FileObject
!= NULL
&& Fcb
->OpenHandleCount
> 1)
1171 /* Count the handles */
1172 HandleCount
+= Fcb
->OpenHandleCount
;
1173 /* More than two handles? Then, we're not booting anymore */
1174 if (HandleCount
> 2)
1186 /* Here comes the hack, ignore the failure! */
1191 DPRINT1("Can't lock: %u opened\n", DeviceExt
->OpenHandleCount
);
1193 ListEntry
= DeviceExt
->FcbListHead
.Flink
;
1194 while (ListEntry
!= &DeviceExt
->FcbListHead
)
1196 Fcb
= CONTAINING_RECORD(ListEntry
, VFATFCB
, FcbListEntry
);
1197 ListEntry
= ListEntry
->Flink
;
1199 if (Fcb
->OpenHandleCount
> 0)
1201 DPRINT1("Opened (%u - %u): %wZ\n", Fcb
->OpenHandleCount
, Fcb
->RefCount
, &Fcb
->PathNameU
);
1205 return STATUS_ACCESS_DENIED
;
1208 /* End of the hack: be verbose about its usage,
1209 * just in case we would mess up everything!
1214 DPRINT1("HACK: Using lock-hack!\n");
1219 /* Finally, proceed */
1222 DeviceExt
->Flags
|= VCB_VOLUME_LOCKED
;
1223 Vpb
->Flags
|= VPB_LOCKED
;
1227 DeviceExt
->Flags
&= ~VCB_VOLUME_LOCKED
;
1228 Vpb
->Flags
&= ~VPB_LOCKED
;
1231 return STATUS_SUCCESS
;
1237 PVFAT_IRP_CONTEXT IrpContext
)
1239 PDEVICE_EXTENSION DeviceExt
;
1240 PLIST_ENTRY NextEntry
;
1242 PFILE_OBJECT FileObject
;
1244 DPRINT("VfatDismountVolume(%p)\n", IrpContext
);
1246 DeviceExt
= IrpContext
->DeviceExt
;
1247 FileObject
= IrpContext
->FileObject
;
1249 /* We HAVE to be locked. Windows also allows dismount with no lock
1250 * but we're here mainly for 1st stage, so KISS
1252 if (!BooleanFlagOn(DeviceExt
->Flags
, VCB_VOLUME_LOCKED
))
1254 return STATUS_ACCESS_DENIED
;
1257 /* Deny dismount of boot volume */
1258 if (BooleanFlagOn(DeviceExt
->Flags
, VCB_IS_SYS_OR_HAS_PAGE
))
1260 return STATUS_ACCESS_DENIED
;
1263 /* Race condition? */
1264 if (BooleanFlagOn(DeviceExt
->Flags
, VCB_DISMOUNT_PENDING
))
1266 return STATUS_VOLUME_DISMOUNTED
;
1269 /* Notify we'll dismount. Pass that point there's no reason we fail */
1270 FsRtlNotifyVolumeEvent(IrpContext
->Stack
->FileObject
, FSRTL_VOLUME_DISMOUNT
);
1272 ExAcquireResourceExclusiveLite(&DeviceExt
->FatResource
, TRUE
);
1274 /* We're performing a clean shutdown */
1275 if (BooleanFlagOn(DeviceExt
->VolumeFcb
->Flags
, VCB_CLEAR_DIRTY
))
1277 /* Drop the dirty bit */
1278 if (NT_SUCCESS(SetDirtyStatus(DeviceExt
, FALSE
)))
1279 DeviceExt
->VolumeFcb
->Flags
&= ~VCB_IS_DIRTY
;
1282 /* Flush volume & files */
1283 VfatFlushVolume(DeviceExt
, (PVFATFCB
)FileObject
->FsContext
);
1285 /* Rebrowse the FCB in order to free them now */
1286 while (!IsListEmpty(&DeviceExt
->FcbListHead
))
1288 NextEntry
= RemoveTailList(&DeviceExt
->FcbListHead
);
1289 Fcb
= CONTAINING_RECORD(NextEntry
, VFATFCB
, FcbListEntry
);
1290 vfatDestroyFCB(Fcb
);
1293 /* Mark we're being dismounted */
1294 DeviceExt
->Flags
|= VCB_DISMOUNT_PENDING
;
1295 #ifndef ENABLE_SWAPOUT
1296 IrpContext
->DeviceObject
->Vpb
->Flags
&= ~VPB_MOUNTED
;
1299 ExReleaseResourceLite(&DeviceExt
->FatResource
);
1301 /* Release a few resources and quit, we're done */
1302 ExFreePoolWithTag(DeviceExt
->Statistics
, TAG_STATS
);
1303 ExDeleteResourceLite(&DeviceExt
->DirResource
);
1304 ExDeleteResourceLite(&DeviceExt
->FatResource
);
1305 ObDereferenceObject(DeviceExt
->FATFileObject
);
1307 return STATUS_SUCCESS
;
1313 PVFAT_IRP_CONTEXT IrpContext
)
1318 PDEVICE_EXTENSION DeviceExt
;
1320 DeviceExt
= IrpContext
->DeviceExt
;
1321 Length
= IrpContext
->Stack
->Parameters
.FileSystemControl
.OutputBufferLength
;
1322 Buffer
= IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
;
1324 if (Length
< sizeof(FILESYSTEM_STATISTICS
))
1326 return STATUS_BUFFER_TOO_SMALL
;
1331 return STATUS_INVALID_USER_BUFFER
;
1334 if (Length
>= sizeof(STATISTICS
) * VfatGlobalData
->NumberProcessors
)
1336 Length
= sizeof(STATISTICS
) * VfatGlobalData
->NumberProcessors
;
1337 Status
= STATUS_SUCCESS
;
1341 Status
= STATUS_BUFFER_OVERFLOW
;
1344 RtlCopyMemory(Buffer
, DeviceExt
->Statistics
, Length
);
1345 IrpContext
->Irp
->IoStatus
.Information
= Length
;
1351 * FUNCTION: File system control
1354 VfatFileSystemControl(
1355 PVFAT_IRP_CONTEXT IrpContext
)
1359 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext
);
1362 ASSERT(IrpContext
->Irp
);
1363 ASSERT(IrpContext
->Stack
);
1365 IrpContext
->Irp
->IoStatus
.Information
= 0;
1367 switch (IrpContext
->MinorFunction
)
1369 case IRP_MN_KERNEL_CALL
:
1370 case IRP_MN_USER_FS_REQUEST
:
1371 switch(IrpContext
->Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
1373 case FSCTL_GET_VOLUME_BITMAP
:
1374 Status
= VfatGetVolumeBitmap(IrpContext
);
1377 case FSCTL_GET_RETRIEVAL_POINTERS
:
1378 Status
= VfatGetRetrievalPointers(IrpContext
);
1381 case FSCTL_MOVE_FILE
:
1382 Status
= VfatMoveFile(IrpContext
);
1385 case FSCTL_IS_VOLUME_DIRTY
:
1386 Status
= VfatIsVolumeDirty(IrpContext
);
1389 case FSCTL_MARK_VOLUME_DIRTY
:
1390 Status
= VfatMarkVolumeDirty(IrpContext
);
1393 case FSCTL_LOCK_VOLUME
:
1394 Status
= VfatLockOrUnlockVolume(IrpContext
, TRUE
);
1397 case FSCTL_UNLOCK_VOLUME
:
1398 Status
= VfatLockOrUnlockVolume(IrpContext
, FALSE
);
1401 case FSCTL_DISMOUNT_VOLUME
:
1402 Status
= VfatDismountVolume(IrpContext
);
1405 case FSCTL_FILESYSTEM_GET_STATISTICS
:
1406 Status
= VfatGetStatistics(IrpContext
);
1410 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1414 case IRP_MN_MOUNT_VOLUME
:
1415 Status
= VfatMount(IrpContext
);
1418 case IRP_MN_VERIFY_VOLUME
:
1419 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
1420 Status
= VfatVerify(IrpContext
);
1424 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext
->MinorFunction
);
1425 Status
= STATUS_INVALID_DEVICE_REQUEST
;