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 RtlCopyMemory(&FatInfo
.VolumeLabel
, &((struct _BootSector32
*)Boot
)->VolumeLabel
, sizeof(FatInfo
.VolumeLabel
));
240 FatInfo
.FatType
= FAT16
;
241 FatInfo
.RootCluster
= FatInfo
.rootStart
/ FatInfo
.SectorsPerCluster
;
242 RtlCopyMemory(&FatInfo
.VolumeLabel
, &Boot
->VolumeLabel
, sizeof(FatInfo
.VolumeLabel
));
245 if (PartitionInfoIsValid
&&
246 FatInfo
.Sectors
> PartitionInfo
.PartitionLength
.QuadPart
/ FatInfo
.BytesPerSector
)
248 *RecognizedFS
= FALSE
;
251 if (pFatInfo
&& *RecognizedFS
)
261 if (!*RecognizedFS
&& PartitionInfoIsValid
)
263 BootFatX
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(struct _BootSectorFatX
), TAG_VFAT
);
264 if (BootFatX
== NULL
)
267 return STATUS_INSUFFICIENT_RESOURCES
;
272 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
273 Status
= VfatReadDisk(DeviceToMount
, &Offset
, sizeof(struct _BootSectorFatX
), (PUCHAR
) BootFatX
, Override
);
274 if (NT_SUCCESS(Status
))
276 *RecognizedFS
= TRUE
;
277 if (BootFatX
->SysType
[0] != 'F' ||
278 BootFatX
->SysType
[1] != 'A' ||
279 BootFatX
->SysType
[2] != 'T' ||
280 BootFatX
->SysType
[3] != 'X')
282 DPRINT1("SysType %c%c%c%c\n", BootFatX
->SysType
[0], BootFatX
->SysType
[1], BootFatX
->SysType
[2], BootFatX
->SysType
[3]);
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)
296 DPRINT1("SectorsPerCluster %lu\n", BootFatX
->SectorsPerCluster
);
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)
310 FatInfo
.FatType
= FATX16
;
315 FatInfo
.FatType
= FATX32
;
317 FatInfo
.VolumeID
= BootFatX
->VolumeID
;
318 FatInfo
.FATStart
= sizeof(struct _BootSectorFatX
) / DiskGeometry
.BytesPerSector
;
319 FatInfo
.FATCount
= BootFatX
->FATCount
;
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
;
328 if (pFatInfo
&& *RecognizedFS
)
334 ExFreePool(BootFatX
);
337 DPRINT("VfatHasFileSystem done\n");
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
358 PUNICODE_STRING VolumeLabel
)
360 PDEVICE_EXTENSION DeviceExt
;
361 PDEVICE_OBJECT DeviceObject
;
362 PVOID Context
= NULL
;
366 LARGE_INTEGER FileOffset
;
368 ULONG EntriesPerPage
;
370 BOOLEAN NoCache
= (Start
!= 0);
372 NTSTATUS Status
= STATUS_SUCCESS
;
376 SizeDirEntry
= sizeof(FATX_DIR_ENTRY
);
377 EntriesPerPage
= FATX_ENTRIES_PER_PAGE
;
381 SizeDirEntry
= sizeof(FAT_DIR_ENTRY
);
382 EntriesPerPage
= FAT_ENTRIES_PER_PAGE
;
385 FileOffset
.QuadPart
= Start
;
390 /* FIXME: Check we really have a VCB
394 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
, TRUE
);
395 pFcb
= vfatOpenRootFCB(DeviceExt
);
396 ExReleaseResourceLite(&DeviceExt
->DirResource
);
400 CcMapData(pFcb
->FileObject
, &FileOffset
, SizeDirEntry
, MAP_WAIT
, &Context
, (PVOID
*)&Entry
);
402 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
404 Status
= _SEH2_GetExceptionCode();
410 DeviceObject
= Device
;
412 ASSERT(DeviceObject
->Type
== 3);
414 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, PAGE_SIZE
, TAG_VFAT
);
417 Status
= VfatReadDisk(DeviceObject
, &FileOffset
, PAGE_SIZE
, (PUCHAR
)Buffer
, TRUE
);
418 if (!NT_SUCCESS(Status
))
420 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
429 Status
= STATUS_INSUFFICIENT_RESOURCES
;
433 if (NT_SUCCESS(Status
))
437 if (ENTRY_VOLUME(IsFatX
, Entry
))
439 /* copy volume label */
442 StringO
.Buffer
= (PCHAR
)Entry
->FatX
.Filename
;
443 StringO
.MaximumLength
= StringO
.Length
= Entry
->FatX
.FilenameLength
;
444 RtlOemStringToUnicodeString(VolumeLabel
, &StringO
, FALSE
);
448 vfat8Dot3ToString(&Entry
->Fat
, VolumeLabel
);
452 if (ENTRY_END(IsFatX
, Entry
))
457 Entry
= (PDIR_ENTRY
)((ULONG_PTR
)Entry
+ SizeDirEntry
);
458 if ((DirIndex
% EntriesPerPage
) == 0)
460 FileOffset
.u
.LowPart
+= PAGE_SIZE
;
464 CcUnpinData(Context
);
468 CcMapData(pFcb
->FileObject
, &FileOffset
, SizeDirEntry
, MAP_WAIT
, &Context
, (PVOID
*)&Entry
);
470 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
472 Status
= _SEH2_GetExceptionCode();
475 if (!NT_SUCCESS(Status
))
483 Status
= VfatReadDisk(DeviceObject
, &FileOffset
, PAGE_SIZE
, (PUCHAR
)Buffer
, TRUE
);
484 if (!NT_SUCCESS(Status
))
494 CcUnpinData(Context
);
498 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
504 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
, TRUE
);
505 vfatReleaseFCB(DeviceExt
, pFcb
);
506 ExReleaseResourceLite(&DeviceExt
->DirResource
);
509 return STATUS_SUCCESS
;
514 * FUNCTION: Mount the filesystem
519 PVFAT_IRP_CONTEXT IrpContext
)
521 PDEVICE_OBJECT DeviceObject
= NULL
;
522 PDEVICE_EXTENSION DeviceExt
= NULL
;
523 BOOLEAN RecognizedFS
;
526 PVFATFCB VolumeFcb
= NULL
;
528 PDEVICE_OBJECT DeviceToMount
;
530 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\$$Fat$$");
531 UNICODE_STRING VolumeNameU
= RTL_CONSTANT_STRING(L
"\\$$Volume$$");
532 UNICODE_STRING VolumeLabelU
;
537 DPRINT("VfatMount(IrpContext %p)\n", IrpContext
);
541 if (IrpContext
->DeviceObject
!= VfatGlobalData
->DeviceObject
)
543 Status
= STATUS_INVALID_DEVICE_REQUEST
;
547 DeviceToMount
= IrpContext
->Stack
->Parameters
.MountVolume
.DeviceObject
;
548 Vpb
= IrpContext
->Stack
->Parameters
.MountVolume
.Vpb
;
550 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &FatInfo
, FALSE
);
551 if (!NT_SUCCESS(Status
))
556 if (RecognizedFS
== FALSE
)
558 DPRINT("VFAT: Unrecognized Volume\n");
559 Status
= STATUS_UNRECOGNIZED_VOLUME
;
563 /* Use prime numbers for the table size */
564 if (FatInfo
.FatType
== FAT12
)
566 HashTableSize
= 4099; // 4096 = 4 * 1024
568 else if (FatInfo
.FatType
== FAT16
||
569 FatInfo
.FatType
== FATX16
)
571 HashTableSize
= 16411; // 16384 = 16 * 1024
575 HashTableSize
= 65537; // 65536 = 64 * 1024;
577 DPRINT("VFAT: Recognized volume\n");
578 Status
= IoCreateDevice(VfatGlobalData
->DriverObject
,
579 ROUND_UP(sizeof (DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
,
581 FILE_DEVICE_DISK_FILE_SYSTEM
,
582 DeviceToMount
->Characteristics
,
585 if (!NT_SUCCESS(Status
))
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
;
596 /* use same vpb as device disk */
597 DeviceObject
->Vpb
= Vpb
;
598 DeviceToMount
->Vpb
= Vpb
;
600 RtlCopyMemory(&DeviceExt
->FatInfo
, &FatInfo
, sizeof(FATINFO
));
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
)
610 DPRINT("RootCluster: %u\n", DeviceExt
->FatInfo
.RootCluster
);
613 switch (DeviceExt
->FatInfo
.FatType
)
616 DeviceExt
->GetNextCluster
= FAT12GetNextCluster
;
617 DeviceExt
->FindAndMarkAvailableCluster
= FAT12FindAndMarkAvailableCluster
;
618 DeviceExt
->WriteCluster
= FAT12WriteCluster
;
619 DeviceExt
->CleanShutBitMask
= 0;
624 DeviceExt
->GetNextCluster
= FAT16GetNextCluster
;
625 DeviceExt
->FindAndMarkAvailableCluster
= FAT16FindAndMarkAvailableCluster
;
626 DeviceExt
->WriteCluster
= FAT16WriteCluster
;
627 DeviceExt
->CleanShutBitMask
= 0x8000;
632 DeviceExt
->GetNextCluster
= FAT32GetNextCluster
;
633 DeviceExt
->FindAndMarkAvailableCluster
= FAT32FindAndMarkAvailableCluster
;
634 DeviceExt
->WriteCluster
= FAT32WriteCluster
;
635 DeviceExt
->CleanShutBitMask
= 0x80000000;
639 if (DeviceExt
->FatInfo
.FatType
== FATX16
||
640 DeviceExt
->FatInfo
.FatType
== FATX32
)
642 DeviceExt
->Flags
|= VCB_IS_FATX
;
643 DeviceExt
->BaseDateYear
= 2000;
644 RtlCopyMemory(&DeviceExt
->Dispatch
, &FatXDispatch
, sizeof(VFAT_DISPATCH
));
648 DeviceExt
->BaseDateYear
= 1980;
649 RtlCopyMemory(&DeviceExt
->Dispatch
, &FatDispatch
, sizeof(VFAT_DISPATCH
));
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
;
659 DPRINT("FsDeviceObject %p\n", DeviceObject
);
661 /* Initialize this resource early ... it's used in VfatCleanup */
662 ExInitializeResourceLite(&DeviceExt
->DirResource
);
664 DeviceExt
->IoVPB
= DeviceObject
->Vpb
;
665 DeviceExt
->SpareVPB
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(VPB
), TAG_VFAT
);
666 if (DeviceExt
->SpareVPB
== NULL
)
668 Status
= STATUS_INSUFFICIENT_RESOURCES
;
672 DeviceExt
->FATFileObject
= IoCreateStreamFileObject(NULL
, DeviceExt
->StorageDevice
);
673 Fcb
= vfatNewFCB(DeviceExt
, &NameU
);
676 Status
= STATUS_INSUFFICIENT_RESOURCES
;
680 Ccb
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
683 Status
= STATUS_INSUFFICIENT_RESOURCES
;
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
;
695 Fcb
->Flags
|= FCB_IS_FAT
;
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
;
703 CcInitializeCacheMap(DeviceExt
->FATFileObject
,
704 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
706 &VfatGlobalData
->CacheMgrCallbacks
,
709 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
711 Status
= _SEH2_GetExceptionCode();
716 DeviceExt
->LastAvailableCluster
= 2;
717 ExInitializeResourceLite(&DeviceExt
->FatResource
);
719 InitializeListHead(&DeviceExt
->FcbListHead
);
721 VolumeFcb
= vfatNewFCB(DeviceExt
, &VolumeNameU
);
722 if (VolumeFcb
== NULL
)
724 Status
= STATUS_INSUFFICIENT_RESOURCES
;
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
;
734 ExAcquireResourceExclusiveLite(&VfatGlobalData
->VolumeListLock
, TRUE
);
735 InsertHeadList(&VfatGlobalData
->VolumeListHead
, &DeviceExt
->VolumeListEntry
);
736 ExReleaseResourceLite(&VfatGlobalData
->VolumeListLock
);
738 /* read serial number */
739 DeviceObject
->Vpb
->SerialNumber
= DeviceExt
->FatInfo
.VolumeID
;
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
;
748 /* read clean shutdown bit status */
749 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
750 if (NT_SUCCESS(Status
))
752 if (eocMark
& DeviceExt
->CleanShutBitMask
)
754 /* unset clean shutdown bit */
755 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
756 WriteCluster(DeviceExt
, 1, eocMark
);
757 VolumeFcb
->Flags
|= VCB_CLEAR_DIRTY
;
761 VolumeFcb
->Flags
|= VCB_IS_DIRTY
;
763 FsRtlNotifyVolumeEvent(DeviceExt
->FATFileObject
, FSRTL_VOLUME_MOUNT
);
764 FsRtlNotifyInitializeSync(&DeviceExt
->NotifySync
);
765 InitializeListHead(&DeviceExt
->NotifyList
);
767 DPRINT("Mount success\n");
769 Status
= STATUS_SUCCESS
;
772 if (!NT_SUCCESS(Status
))
775 if (DeviceExt
&& DeviceExt
->FATFileObject
)
776 ObDereferenceObject (DeviceExt
->FATFileObject
);
777 if (DeviceExt
&& DeviceExt
->SpareVPB
)
778 ExFreePoolWithTag(DeviceExt
->SpareVPB
, TAG_VFAT
);
784 IoDeleteDevice(DeviceObject
);
792 * FUNCTION: Verify the filesystem
797 PVFAT_IRP_CONTEXT IrpContext
)
799 PDEVICE_OBJECT DeviceToVerify
;
802 BOOLEAN RecognizedFS
;
803 PDEVICE_EXTENSION DeviceExt
;
806 ULONG ChangeCount
, BufSize
= sizeof(ChangeCount
);
808 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext
);
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
);
815 if (!BooleanFlagOn(Vpb
->RealDevice
->Flags
, DO_VERIFY_VOLUME
))
817 DPRINT("Already verified\n");
818 return STATUS_SUCCESS
;
821 Status
= VfatBlockDeviceIoControl(DeviceExt
->StorageDevice
,
822 IOCTL_DISK_CHECK_VERIFY
,
828 if (!NT_SUCCESS(Status
) && Status
!= STATUS_VERIFY_REQUIRED
)
830 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status
);
831 Status
= (AllowRaw
? STATUS_WRONG_VOLUME
: Status
);
835 Status
= VfatHasFileSystem(DeviceExt
->StorageDevice
, &RecognizedFS
, &FatInfo
, TRUE
);
836 if (!NT_SUCCESS(Status
) || RecognizedFS
== FALSE
)
838 if (NT_SUCCESS(Status
) || AllowRaw
)
840 Status
= STATUS_WRONG_VOLUME
;
843 else if (sizeof(FATINFO
) == RtlCompareMemory(&FatInfo
, &DeviceExt
->FatInfo
, sizeof(FATINFO
)))
845 WCHAR BufferU
[MAXIMUM_VOLUME_LABEL_LENGTH
/ sizeof(WCHAR
)];
846 UNICODE_STRING VolumeLabelU
;
847 UNICODE_STRING VpbLabelU
;
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
))
857 Status
= STATUS_WRONG_VOLUME
;
862 VpbLabelU
.Buffer
= Vpb
->VolumeLabel
;
863 VpbLabelU
.Length
= Vpb
->VolumeLabelLength
;
864 VpbLabelU
.MaximumLength
= sizeof(Vpb
->VolumeLabel
);
866 if (RtlCompareUnicodeString(&VpbLabelU
, &VolumeLabelU
, FALSE
) != 0)
868 Status
= STATUS_WRONG_VOLUME
;
872 DPRINT1("Same volume\n");
878 Status
= STATUS_WRONG_VOLUME
;
882 Vpb
->RealDevice
->Flags
&= ~DO_VERIFY_VOLUME
;
891 PVFAT_IRP_CONTEXT IrpContext
)
893 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext
);
894 return STATUS_INVALID_DEVICE_REQUEST
;
900 VfatGetRetrievalPointers(
901 PVFAT_IRP_CONTEXT IrpContext
)
903 PIO_STACK_LOCATION Stack
;
905 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers
;
906 PFILE_OBJECT FileObject
;
907 ULONG MaxExtentCount
;
909 PDEVICE_EXTENSION DeviceExt
;
911 ULONG CurrentCluster
;
915 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext
);
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
)
923 return STATUS_INVALID_PARAMETER
;
926 if (IrpContext
->Irp
->UserBuffer
== NULL
||
927 Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(RETRIEVAL_POINTERS_BUFFER
))
929 return STATUS_BUFFER_TOO_SMALL
;
932 Fcb
= FileObject
->FsContext
;
934 ExAcquireResourceSharedLite(&Fcb
->MainResource
, TRUE
);
936 Vcn
= ((PSTARTING_VCN_INPUT_BUFFER
)Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
)->StartingVcn
;
937 RetrievalPointers
= IrpContext
->Irp
->UserBuffer
;
939 MaxExtentCount
= ((Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
- sizeof(RetrievalPointers
->ExtentCount
) - sizeof(RetrievalPointers
->StartingVcn
)) / sizeof(RetrievalPointers
->Extents
[0]));
941 if (Vcn
.QuadPart
>= Fcb
->RFCB
.AllocationSize
.QuadPart
/ DeviceExt
->FatInfo
.BytesPerCluster
)
943 Status
= STATUS_INVALID_PARAMETER
;
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
))
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;
961 while (CurrentCluster
!= 0xffffffff && RetrievalPointers
->ExtentCount
< MaxExtentCount
)
963 LastCluster
= CurrentCluster
;
964 Status
= NextCluster(DeviceExt
, CurrentCluster
, &CurrentCluster
, FALSE
);
966 if (!NT_SUCCESS(Status
))
971 if (LastCluster
+ 1 != CurrentCluster
)
973 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].NextVcn
= Vcn
;
974 RetrievalPointers
->ExtentCount
++;
975 if (RetrievalPointers
->ExtentCount
< MaxExtentCount
)
977 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.HighPart
= 0;
978 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.LowPart
= CurrentCluster
- 2;
983 IrpContext
->Irp
->IoStatus
.Information
= sizeof(RETRIEVAL_POINTERS_BUFFER
) + (sizeof(RetrievalPointers
->Extents
[0]) * (RetrievalPointers
->ExtentCount
- 1));
984 Status
= STATUS_SUCCESS
;
987 ExReleaseResourceLite(&Fcb
->MainResource
);
995 PVFAT_IRP_CONTEXT IrpContext
)
997 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext
);
998 return STATUS_INVALID_DEVICE_REQUEST
;
1004 PVFAT_IRP_CONTEXT IrpContext
)
1008 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext
);
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
;
1015 Flags
= (PULONG
)IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
;
1018 if (BooleanFlagOn(IrpContext
->DeviceExt
->VolumeFcb
->Flags
, VCB_IS_DIRTY
) &&
1019 !BooleanFlagOn(IrpContext
->DeviceExt
->VolumeFcb
->Flags
, VCB_CLEAR_DIRTY
))
1021 *Flags
|= VOLUME_IS_DIRTY
;
1024 IrpContext
->Irp
->IoStatus
.Information
= sizeof(ULONG
);
1026 return STATUS_SUCCESS
;
1031 VfatMarkVolumeDirty(
1032 PVFAT_IRP_CONTEXT IrpContext
)
1035 PDEVICE_EXTENSION DeviceExt
;
1036 NTSTATUS Status
= STATUS_SUCCESS
;
1038 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext
);
1039 DeviceExt
= IrpContext
->DeviceExt
;
1041 if (!BooleanFlagOn(DeviceExt
->VolumeFcb
->Flags
, VCB_IS_DIRTY
))
1043 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
1044 if (NT_SUCCESS(Status
))
1046 /* unset clean shutdown bit */
1047 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
1048 Status
= WriteCluster(DeviceExt
, 1, eocMark
);
1052 DeviceExt
->VolumeFcb
->Flags
&= ~VCB_CLEAR_DIRTY
;
1059 VfatLockOrUnlockVolume(
1060 PVFAT_IRP_CONTEXT IrpContext
,
1063 PFILE_OBJECT FileObject
;
1064 PDEVICE_EXTENSION DeviceExt
;
1068 DPRINT("VfatLockOrUnlockVolume(%p, %d)\n", IrpContext
, Lock
);
1070 DeviceExt
= IrpContext
->DeviceExt
;
1071 FileObject
= IrpContext
->FileObject
;
1072 Fcb
= FileObject
->FsContext
;
1073 Vpb
= DeviceExt
->FATFileObject
->Vpb
;
1075 /* Only allow locking with the volume open */
1076 if (!BooleanFlagOn(Fcb
->Flags
, FCB_IS_VOLUME
))
1078 return STATUS_ACCESS_DENIED
;
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
))
1085 return STATUS_ACCESS_DENIED
;
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
))
1092 return STATUS_ACCESS_DENIED
;
1095 /* Deny locking if we're not alone */
1096 if (Lock
&& DeviceExt
->OpenHandleCount
!= 1)
1098 return STATUS_ACCESS_DENIED
;
1101 /* Finally, proceed */
1104 DeviceExt
->Flags
|= VCB_VOLUME_LOCKED
;
1105 Vpb
->Flags
|= VPB_LOCKED
;
1109 DeviceExt
->Flags
&= ~VCB_VOLUME_LOCKED
;
1110 Vpb
->Flags
&= ~VPB_LOCKED
;
1113 return STATUS_SUCCESS
;
1119 PVFAT_IRP_CONTEXT IrpContext
)
1121 PDEVICE_EXTENSION DeviceExt
;
1122 PLIST_ENTRY NextEntry
;
1124 PFILE_OBJECT FileObject
;
1128 DPRINT("VfatDismountVolume(%p)\n", IrpContext
);
1130 DeviceExt
= IrpContext
->DeviceExt
;
1131 FileObject
= IrpContext
->FileObject
;
1133 /* We HAVE to be locked. Windows also allows dismount with no lock
1134 * but we're here mainly for 1st stage, so KISS
1136 if (!BooleanFlagOn(DeviceExt
->Flags
, VCB_VOLUME_LOCKED
))
1138 return STATUS_ACCESS_DENIED
;
1141 /* Race condition? */
1142 if (BooleanFlagOn(DeviceExt
->Flags
, VCB_DISMOUNT_PENDING
))
1144 return STATUS_VOLUME_DISMOUNTED
;
1147 /* Notify we'll dismount. Pass that point there's no reason we fail */
1148 FsRtlNotifyVolumeEvent(IrpContext
->Stack
->FileObject
, FSRTL_VOLUME_DISMOUNT
);
1150 ExAcquireResourceExclusiveLite(&DeviceExt
->FatResource
, TRUE
);
1152 if (BooleanFlagOn(DeviceExt
->VolumeFcb
->Flags
, VCB_CLEAR_DIRTY
))
1154 /* Set clean shutdown bit */
1155 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
1156 if (NT_SUCCESS(Status
))
1158 eocMark
|= DeviceExt
->CleanShutBitMask
;
1159 if (NT_SUCCESS(WriteCluster(DeviceExt
, 1, eocMark
)))
1160 DeviceExt
->VolumeFcb
->Flags
&= ~VCB_IS_DIRTY
;
1164 /* Flush volume & files */
1165 VfatFlushVolume(DeviceExt
, (PVFATFCB
)FileObject
->FsContext
);
1167 /* Rebrowse the FCB in order to free them now */
1168 while (!IsListEmpty(&DeviceExt
->FcbListHead
))
1170 NextEntry
= RemoveHeadList(&DeviceExt
->FcbListHead
);
1171 Fcb
= CONTAINING_RECORD(NextEntry
, VFATFCB
, FcbListEntry
);
1172 vfatDestroyFCB(Fcb
);
1175 /* Mark we're being dismounted */
1176 DeviceExt
->Flags
|= VCB_DISMOUNT_PENDING
;
1177 #ifndef ENABLE_SWAPOUT
1178 IrpContext
->DeviceObject
->Vpb
->Flags
&= ~VPB_MOUNTED
;
1181 ExReleaseResourceLite(&DeviceExt
->FatResource
);
1183 /* Release a few resources and quit, we're done */
1184 ExDeleteResourceLite(&DeviceExt
->DirResource
);
1185 ExDeleteResourceLite(&DeviceExt
->FatResource
);
1186 ObDereferenceObject(DeviceExt
->FATFileObject
);
1188 return STATUS_SUCCESS
;
1192 * FUNCTION: File system control
1195 VfatFileSystemControl(
1196 PVFAT_IRP_CONTEXT IrpContext
)
1200 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext
);
1203 ASSERT(IrpContext
->Irp
);
1204 ASSERT(IrpContext
->Stack
);
1206 IrpContext
->Irp
->IoStatus
.Information
= 0;
1208 switch (IrpContext
->MinorFunction
)
1210 case IRP_MN_KERNEL_CALL
:
1211 case IRP_MN_USER_FS_REQUEST
:
1212 switch(IrpContext
->Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
1214 case FSCTL_GET_VOLUME_BITMAP
:
1215 Status
= VfatGetVolumeBitmap(IrpContext
);
1218 case FSCTL_GET_RETRIEVAL_POINTERS
:
1219 Status
= VfatGetRetrievalPointers(IrpContext
);
1222 case FSCTL_MOVE_FILE
:
1223 Status
= VfatMoveFile(IrpContext
);
1226 case FSCTL_IS_VOLUME_DIRTY
:
1227 Status
= VfatIsVolumeDirty(IrpContext
);
1230 case FSCTL_MARK_VOLUME_DIRTY
:
1231 Status
= VfatMarkVolumeDirty(IrpContext
);
1234 case FSCTL_LOCK_VOLUME
:
1235 Status
= VfatLockOrUnlockVolume(IrpContext
, TRUE
);
1238 case FSCTL_UNLOCK_VOLUME
:
1239 Status
= VfatLockOrUnlockVolume(IrpContext
, FALSE
);
1242 case FSCTL_DISMOUNT_VOLUME
:
1243 Status
= VfatDismountVolume(IrpContext
);
1247 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1251 case IRP_MN_MOUNT_VOLUME
:
1252 Status
= VfatMount(IrpContext
);
1255 case IRP_MN_VERIFY_VOLUME
:
1256 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
1257 Status
= VfatVerify(IrpContext
);
1261 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext
->MinorFunction
);
1262 Status
= STATUS_INVALID_DEVICE_REQUEST
;