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 /* FUNCTIONS ****************************************************************/
35 #define CACHEPAGESIZE(pDeviceExt) ((pDeviceExt)->FatInfo.BytesPerCluster > PAGE_SIZE ? \
36 (pDeviceExt)->FatInfo.BytesPerCluster : PAGE_SIZE)
41 PDEVICE_OBJECT DeviceToMount
,
42 PBOOLEAN RecognizedFS
,
46 PARTITION_INFORMATION PartitionInfo
;
47 DISK_GEOMETRY DiskGeometry
;
52 struct _BootSector
* Boot
;
53 struct _BootSectorFatX
* BootFatX
;
54 BOOLEAN PartitionInfoIsValid
= FALSE
;
56 DPRINT("VfatHasFileSystem\n");
58 *RecognizedFS
= FALSE
;
60 Size
= sizeof(DISK_GEOMETRY
);
61 Status
= VfatBlockDeviceIoControl(DeviceToMount
,
62 IOCTL_DISK_GET_DRIVE_GEOMETRY
,
68 if (!NT_SUCCESS(Status
))
70 DPRINT("VfatBlockDeviceIoControl failed (%x)\n", Status
);
74 FatInfo
.FixedMedia
= DiskGeometry
.MediaType
== FixedMedia
? TRUE
: FALSE
;
75 if (DiskGeometry
.MediaType
== FixedMedia
|| DiskGeometry
.MediaType
== RemovableMedia
)
77 // We have found a hard disk
78 Size
= sizeof(PARTITION_INFORMATION
);
79 Status
= VfatBlockDeviceIoControl(DeviceToMount
,
80 IOCTL_DISK_GET_PARTITION_INFO
,
86 if (!NT_SUCCESS(Status
))
88 DPRINT("VfatBlockDeviceIoControl failed (%x)\n", Status
);
92 DPRINT("Partition Information:\n");
93 DPRINT("StartingOffset %I64x\n", PartitionInfo
.StartingOffset
.QuadPart
/ 512);
94 DPRINT("PartitionLength %I64x\n", PartitionInfo
.PartitionLength
.QuadPart
/ 512);
95 DPRINT("HiddenSectors %u\n", PartitionInfo
.HiddenSectors
);
96 DPRINT("PartitionNumber %u\n", PartitionInfo
.PartitionNumber
);
97 DPRINT("PartitionType %u\n", PartitionInfo
.PartitionType
);
98 DPRINT("BootIndicator %u\n", PartitionInfo
.BootIndicator
);
99 DPRINT("RecognizedPartition %u\n", PartitionInfo
.RecognizedPartition
);
100 DPRINT("RewritePartition %u\n", PartitionInfo
.RewritePartition
);
101 if (PartitionInfo
.PartitionType
)
103 if (PartitionInfo
.PartitionType
== PARTITION_FAT_12
||
104 PartitionInfo
.PartitionType
== PARTITION_FAT_16
||
105 PartitionInfo
.PartitionType
== PARTITION_HUGE
||
106 PartitionInfo
.PartitionType
== PARTITION_FAT32
||
107 PartitionInfo
.PartitionType
== PARTITION_FAT32_XINT13
||
108 PartitionInfo
.PartitionType
== PARTITION_XINT13
)
110 PartitionInfoIsValid
= TRUE
;
111 *RecognizedFS
= TRUE
;
114 else if (DiskGeometry
.MediaType
== RemovableMedia
&&
115 PartitionInfo
.PartitionNumber
> 0 &&
116 PartitionInfo
.StartingOffset
.QuadPart
== 0 &&
117 PartitionInfo
.PartitionLength
.QuadPart
> 0)
119 /* This is possible a removable media formated as super floppy */
120 PartitionInfoIsValid
= TRUE
;
121 *RecognizedFS
= TRUE
;
126 *RecognizedFS
= TRUE
;
131 Boot
= ExAllocatePoolWithTag(NonPagedPool
, DiskGeometry
.BytesPerSector
, TAG_VFAT
);
134 return STATUS_INSUFFICIENT_RESOURCES
;
139 /* Try to recognize FAT12/FAT16/FAT32 partitions */
140 Status
= VfatReadDisk(DeviceToMount
, &Offset
, DiskGeometry
.BytesPerSector
, (PUCHAR
) Boot
, FALSE
);
141 if (NT_SUCCESS(Status
))
143 if (Boot
->Signatur1
!= 0xaa55)
145 *RecognizedFS
= FALSE
;
149 Boot
->BytesPerSector
!= 512 &&
150 Boot
->BytesPerSector
!= 1024 &&
151 Boot
->BytesPerSector
!= 2048 &&
152 Boot
->BytesPerSector
!= 4096)
154 DPRINT1("BytesPerSector %u\n", Boot
->BytesPerSector
);
155 *RecognizedFS
= FALSE
;
159 Boot
->FATCount
!= 1 &&
162 DPRINT1("FATCount %u\n", Boot
->FATCount
);
163 *RecognizedFS
= FALSE
;
167 Boot
->Media
!= 0xf0 &&
168 Boot
->Media
!= 0xf8 &&
169 Boot
->Media
!= 0xf9 &&
170 Boot
->Media
!= 0xfa &&
171 Boot
->Media
!= 0xfb &&
172 Boot
->Media
!= 0xfc &&
173 Boot
->Media
!= 0xfd &&
174 Boot
->Media
!= 0xfe &&
177 DPRINT1("Media %02x\n", Boot
->Media
);
178 *RecognizedFS
= FALSE
;
182 Boot
->SectorsPerCluster
!= 1 &&
183 Boot
->SectorsPerCluster
!= 2 &&
184 Boot
->SectorsPerCluster
!= 4 &&
185 Boot
->SectorsPerCluster
!= 8 &&
186 Boot
->SectorsPerCluster
!= 16 &&
187 Boot
->SectorsPerCluster
!= 32 &&
188 Boot
->SectorsPerCluster
!= 64 &&
189 Boot
->SectorsPerCluster
!= 128)
191 DPRINT1("SectorsPerCluster %02x\n", Boot
->SectorsPerCluster
);
192 *RecognizedFS
= FALSE
;
196 Boot
->BytesPerSector
* Boot
->SectorsPerCluster
> 32 * 1024)
198 DPRINT1("ClusterSize %dx\n", Boot
->BytesPerSector
* Boot
->SectorsPerCluster
);
199 *RecognizedFS
= FALSE
;
204 FatInfo
.VolumeID
= Boot
->VolumeID
;
205 FatInfo
.FATStart
= Boot
->ReservedSectors
;
206 FatInfo
.FATCount
= Boot
->FATCount
;
207 FatInfo
.FATSectors
= Boot
->FATSectors
? Boot
->FATSectors
: ((struct _BootSector32
*) Boot
)->FATSectors32
;
208 FatInfo
.BytesPerSector
= Boot
->BytesPerSector
;
209 FatInfo
.SectorsPerCluster
= Boot
->SectorsPerCluster
;
210 FatInfo
.BytesPerCluster
= FatInfo
.BytesPerSector
* FatInfo
.SectorsPerCluster
;
211 FatInfo
.rootDirectorySectors
= ((Boot
->RootEntries
* 32) + Boot
->BytesPerSector
- 1) / Boot
->BytesPerSector
;
212 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
213 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
214 FatInfo
.Sectors
= Sectors
= Boot
->Sectors
? Boot
->Sectors
: Boot
->SectorsHuge
;
215 Sectors
-= Boot
->ReservedSectors
+ FatInfo
.FATCount
* FatInfo
.FATSectors
+ FatInfo
.rootDirectorySectors
;
216 FatInfo
.NumberOfClusters
= Sectors
/ Boot
->SectorsPerCluster
;
217 if (FatInfo
.NumberOfClusters
< 4085)
220 FatInfo
.FatType
= FAT12
;
221 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
223 else if (FatInfo
.NumberOfClusters
>= 65525)
226 FatInfo
.FatType
= FAT32
;
227 FatInfo
.RootCluster
= ((struct _BootSector32
*) Boot
)->RootCluster
;
228 FatInfo
.rootStart
= FatInfo
.dataStart
+ ((FatInfo
.RootCluster
- 2) * FatInfo
.SectorsPerCluster
);
229 FatInfo
.VolumeID
= ((struct _BootSector32
*) Boot
)->VolumeID
;
234 FatInfo
.FatType
= FAT16
;
235 FatInfo
.RootCluster
= FatInfo
.rootStart
/ FatInfo
.SectorsPerCluster
;
238 if (PartitionInfoIsValid
&&
239 FatInfo
.Sectors
> PartitionInfo
.PartitionLength
.QuadPart
/ FatInfo
.BytesPerSector
)
241 *RecognizedFS
= FALSE
;
244 if (pFatInfo
&& *RecognizedFS
)
254 if (!*RecognizedFS
&& PartitionInfoIsValid
)
256 BootFatX
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(struct _BootSectorFatX
), TAG_VFAT
);
257 if (BootFatX
== NULL
)
260 return STATUS_INSUFFICIENT_RESOURCES
;
265 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
266 Status
= VfatReadDisk(DeviceToMount
, &Offset
, sizeof(struct _BootSectorFatX
), (PUCHAR
) BootFatX
, FALSE
);
267 if (NT_SUCCESS(Status
))
269 *RecognizedFS
= TRUE
;
270 if (BootFatX
->SysType
[0] != 'F' ||
271 BootFatX
->SysType
[1] != 'A' ||
272 BootFatX
->SysType
[2] != 'T' ||
273 BootFatX
->SysType
[3] != 'X')
275 DPRINT1("SysType %c%c%c%c\n", BootFatX
->SysType
[0], BootFatX
->SysType
[1], BootFatX
->SysType
[2], BootFatX
->SysType
[3]);
280 BootFatX
->SectorsPerCluster
!= 1 &&
281 BootFatX
->SectorsPerCluster
!= 2 &&
282 BootFatX
->SectorsPerCluster
!= 4 &&
283 BootFatX
->SectorsPerCluster
!= 8 &&
284 BootFatX
->SectorsPerCluster
!= 16 &&
285 BootFatX
->SectorsPerCluster
!= 32 &&
286 BootFatX
->SectorsPerCluster
!= 64 &&
287 BootFatX
->SectorsPerCluster
!= 128)
289 DPRINT1("SectorsPerCluster %lu\n", BootFatX
->SectorsPerCluster
);
295 FatInfo
.BytesPerSector
= DiskGeometry
.BytesPerSector
;
296 FatInfo
.SectorsPerCluster
= BootFatX
->SectorsPerCluster
;
297 FatInfo
.rootDirectorySectors
= BootFatX
->SectorsPerCluster
;
298 FatInfo
.BytesPerCluster
= BootFatX
->SectorsPerCluster
* DiskGeometry
.BytesPerSector
;
299 FatInfo
.Sectors
= (ULONG
)(PartitionInfo
.PartitionLength
.QuadPart
/ DiskGeometry
.BytesPerSector
);
300 if (FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
< 65525)
303 FatInfo
.FatType
= FATX16
;
308 FatInfo
.FatType
= FATX32
;
310 FatInfo
.VolumeID
= BootFatX
->VolumeID
;
311 FatInfo
.FATStart
= sizeof(struct _BootSectorFatX
) / DiskGeometry
.BytesPerSector
;
312 FatInfo
.FATCount
= BootFatX
->FATCount
;
314 ROUND_UP(FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
* (FatInfo
.FatType
== FATX16
? 2 : 4), 4096) /
315 FatInfo
.BytesPerSector
;
316 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
317 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
318 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
319 FatInfo
.NumberOfClusters
= (FatInfo
.Sectors
- FatInfo
.dataStart
) / FatInfo
.SectorsPerCluster
;
321 if (pFatInfo
&& *RecognizedFS
)
327 ExFreePool(BootFatX
);
330 DPRINT("VfatHasFileSystem done\n");
335 * FUNCTION: Mounts the device
340 PDEVICE_EXTENSION DeviceExt
,
341 PDEVICE_OBJECT DeviceToMount
)
344 BOOLEAN RecognizedFS
;
346 DPRINT("Mounting VFAT device...\n");
348 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &DeviceExt
->FatInfo
);
349 if (!NT_SUCCESS(Status
))
353 DPRINT("MountVfatdev %u, PAGE_SIZE = %d\n", DeviceExt
->FatInfo
.BytesPerCluster
, PAGE_SIZE
);
355 return STATUS_SUCCESS
;
360 * FUNCTION: Mount the filesystem
365 PVFAT_IRP_CONTEXT IrpContext
)
367 PDEVICE_OBJECT DeviceObject
= NULL
;
368 PDEVICE_EXTENSION DeviceExt
= NULL
;
369 BOOLEAN RecognizedFS
;
372 PVFATFCB VolumeFcb
= NULL
;
374 PDEVICE_OBJECT DeviceToMount
;
376 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\$$Fat$$");
377 UNICODE_STRING VolumeNameU
= RTL_CONSTANT_STRING(L
"\\$$Volume$$");
382 DPRINT("VfatMount(IrpContext %p)\n", IrpContext
);
386 if (IrpContext
->DeviceObject
!= VfatGlobalData
->DeviceObject
)
388 Status
= STATUS_INVALID_DEVICE_REQUEST
;
392 DeviceToMount
= IrpContext
->Stack
->Parameters
.MountVolume
.DeviceObject
;
393 Vpb
= IrpContext
->Stack
->Parameters
.MountVolume
.Vpb
;
395 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &FatInfo
);
396 if (!NT_SUCCESS(Status
))
401 if (RecognizedFS
== FALSE
)
403 DPRINT("VFAT: Unrecognized Volume\n");
404 Status
= STATUS_UNRECOGNIZED_VOLUME
;
408 /* Use prime numbers for the table size */
409 if (FatInfo
.FatType
== FAT12
)
411 HashTableSize
= 4099; // 4096 = 4 * 1024
413 else if (FatInfo
.FatType
== FAT16
||
414 FatInfo
.FatType
== FATX16
)
416 HashTableSize
= 16411; // 16384 = 16 * 1024
420 HashTableSize
= 65537; // 65536 = 64 * 1024;
422 DPRINT("VFAT: Recognized volume\n");
423 Status
= IoCreateDevice(VfatGlobalData
->DriverObject
,
424 ROUND_UP(sizeof (DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
,
426 FILE_DEVICE_DISK_FILE_SYSTEM
,
427 DeviceToMount
->Characteristics
,
430 if (!NT_SUCCESS(Status
))
435 DeviceExt
= DeviceObject
->DeviceExtension
;
436 RtlZeroMemory(DeviceExt
, ROUND_UP(sizeof(DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
);
437 DeviceExt
->FcbHashTable
= (HASHENTRY
**)((ULONG_PTR
)DeviceExt
+ ROUND_UP(sizeof(DEVICE_EXTENSION
), sizeof(ULONG
)));
438 DeviceExt
->HashTableSize
= HashTableSize
;
439 DeviceExt
->VolumeDevice
= DeviceObject
;
441 /* use same vpb as device disk */
442 DeviceObject
->Vpb
= Vpb
;
443 DeviceToMount
->Vpb
= Vpb
;
445 Status
= VfatMountDevice(DeviceExt
, DeviceToMount
);
446 if (!NT_SUCCESS(Status
))
451 DPRINT("BytesPerSector: %u\n", DeviceExt
->FatInfo
.BytesPerSector
);
452 DPRINT("SectorsPerCluster: %u\n", DeviceExt
->FatInfo
.SectorsPerCluster
);
453 DPRINT("FATCount: %u\n", DeviceExt
->FatInfo
.FATCount
);
454 DPRINT("FATSectors: %u\n", DeviceExt
->FatInfo
.FATSectors
);
455 DPRINT("RootStart: %u\n", DeviceExt
->FatInfo
.rootStart
);
456 DPRINT("DataStart: %u\n", DeviceExt
->FatInfo
.dataStart
);
457 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
459 DPRINT("RootCluster: %u\n", DeviceExt
->FatInfo
.RootCluster
);
462 switch (DeviceExt
->FatInfo
.FatType
)
465 DeviceExt
->GetNextCluster
= FAT12GetNextCluster
;
466 DeviceExt
->FindAndMarkAvailableCluster
= FAT12FindAndMarkAvailableCluster
;
467 DeviceExt
->WriteCluster
= FAT12WriteCluster
;
468 DeviceExt
->CleanShutBitMask
= 0;
473 DeviceExt
->GetNextCluster
= FAT16GetNextCluster
;
474 DeviceExt
->FindAndMarkAvailableCluster
= FAT16FindAndMarkAvailableCluster
;
475 DeviceExt
->WriteCluster
= FAT16WriteCluster
;
476 DeviceExt
->CleanShutBitMask
= 0x8000;
481 DeviceExt
->GetNextCluster
= FAT32GetNextCluster
;
482 DeviceExt
->FindAndMarkAvailableCluster
= FAT32FindAndMarkAvailableCluster
;
483 DeviceExt
->WriteCluster
= FAT32WriteCluster
;
484 DeviceExt
->CleanShutBitMask
= 0x80000000;
488 if (DeviceExt
->FatInfo
.FatType
== FATX16
||
489 DeviceExt
->FatInfo
.FatType
== FATX32
)
491 DeviceExt
->Flags
|= VCB_IS_FATX
;
492 DeviceExt
->GetNextDirEntry
= FATXGetNextDirEntry
;
493 DeviceExt
->BaseDateYear
= 2000;
497 DeviceExt
->GetNextDirEntry
= FATGetNextDirEntry
;
498 DeviceExt
->BaseDateYear
= 1980;
501 DeviceExt
->StorageDevice
= DeviceToMount
;
502 DeviceExt
->StorageDevice
->Vpb
->DeviceObject
= DeviceObject
;
503 DeviceExt
->StorageDevice
->Vpb
->RealDevice
= DeviceExt
->StorageDevice
;
504 DeviceExt
->StorageDevice
->Vpb
->Flags
|= VPB_MOUNTED
;
505 DeviceObject
->StackSize
= DeviceExt
->StorageDevice
->StackSize
+ 1;
506 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
508 DPRINT("FsDeviceObject %p\n", DeviceObject
);
510 /* Initialize this resource early ... it's used in VfatCleanup */
511 ExInitializeResourceLite(&DeviceExt
->DirResource
);
513 DeviceExt
->IoVPB
= DeviceObject
->Vpb
;
514 DeviceExt
->SpareVPB
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(VPB
), TAG_VFAT
);
515 if (DeviceExt
->SpareVPB
== NULL
)
517 Status
= STATUS_INSUFFICIENT_RESOURCES
;
521 DeviceExt
->FATFileObject
= IoCreateStreamFileObject(NULL
, DeviceExt
->StorageDevice
);
522 Fcb
= vfatNewFCB(DeviceExt
, &NameU
);
525 Status
= STATUS_INSUFFICIENT_RESOURCES
;
529 Ccb
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
532 Status
= STATUS_INSUFFICIENT_RESOURCES
;
536 RtlZeroMemory(Ccb
, sizeof (VFATCCB
));
537 DeviceExt
->FATFileObject
->FsContext
= Fcb
;
538 DeviceExt
->FATFileObject
->FsContext2
= Ccb
;
539 DeviceExt
->FATFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
540 DeviceExt
->FATFileObject
->PrivateCacheMap
= NULL
;
541 DeviceExt
->FATFileObject
->Vpb
= DeviceObject
->Vpb
;
542 Fcb
->FileObject
= DeviceExt
->FATFileObject
;
544 Fcb
->Flags
|= FCB_IS_FAT
;
546 Fcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.FATSectors
* DeviceExt
->FatInfo
.BytesPerSector
;
547 Fcb
->RFCB
.ValidDataLength
= Fcb
->RFCB
.FileSize
;
548 Fcb
->RFCB
.AllocationSize
= Fcb
->RFCB
.FileSize
;
552 CcInitializeCacheMap(DeviceExt
->FATFileObject
,
553 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
555 &VfatGlobalData
->CacheMgrCallbacks
,
558 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
560 Status
= _SEH2_GetExceptionCode();
565 DeviceExt
->LastAvailableCluster
= 2;
566 ExInitializeResourceLite(&DeviceExt
->FatResource
);
568 InitializeListHead(&DeviceExt
->FcbListHead
);
570 VolumeFcb
= vfatNewFCB(DeviceExt
, &VolumeNameU
);
571 if (VolumeFcb
== NULL
)
573 Status
= STATUS_INSUFFICIENT_RESOURCES
;
577 VolumeFcb
->Flags
= FCB_IS_VOLUME
;
578 VolumeFcb
->RFCB
.FileSize
.QuadPart
= (LONGLONG
) DeviceExt
->FatInfo
.Sectors
* DeviceExt
->FatInfo
.BytesPerSector
;
579 VolumeFcb
->RFCB
.ValidDataLength
= VolumeFcb
->RFCB
.FileSize
;
580 VolumeFcb
->RFCB
.AllocationSize
= VolumeFcb
->RFCB
.FileSize
;
581 DeviceExt
->VolumeFcb
= VolumeFcb
;
583 ExAcquireResourceExclusiveLite(&VfatGlobalData
->VolumeListLock
, TRUE
);
584 InsertHeadList(&VfatGlobalData
->VolumeListHead
, &DeviceExt
->VolumeListEntry
);
585 ExReleaseResourceLite(&VfatGlobalData
->VolumeListLock
);
587 /* read serial number */
588 DeviceObject
->Vpb
->SerialNumber
= DeviceExt
->FatInfo
.VolumeID
;
590 /* read volume label */
591 ReadVolumeLabel(DeviceExt
, DeviceObject
->Vpb
);
593 /* read clean shutdown bit status */
594 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
595 if (NT_SUCCESS(Status
))
597 if (eocMark
& DeviceExt
->CleanShutBitMask
)
599 /* unset clean shutdown bit */
600 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
601 WriteCluster(DeviceExt
, 1, eocMark
);
602 VolumeFcb
->Flags
|= VCB_CLEAR_DIRTY
;
606 VolumeFcb
->Flags
|= VCB_IS_DIRTY
;
608 FsRtlNotifyVolumeEvent(DeviceExt
->FATFileObject
, FSRTL_VOLUME_MOUNT
);
609 FsRtlNotifyInitializeSync(&DeviceExt
->NotifySync
);
610 InitializeListHead(&DeviceExt
->NotifyList
);
612 DPRINT("Mount success\n");
614 Status
= STATUS_SUCCESS
;
617 if (!NT_SUCCESS(Status
))
620 if (DeviceExt
&& DeviceExt
->FATFileObject
)
621 ObDereferenceObject (DeviceExt
->FATFileObject
);
622 if (DeviceExt
&& DeviceExt
->SpareVPB
)
623 ExFreePoolWithTag(DeviceExt
->SpareVPB
, TAG_VFAT
);
629 IoDeleteDevice(DeviceObject
);
637 * FUNCTION: Verify the filesystem
642 PVFAT_IRP_CONTEXT IrpContext
)
644 PDEVICE_OBJECT DeviceToVerify
;
645 NTSTATUS Status
= STATUS_SUCCESS
;
647 BOOLEAN RecognizedFS
;
648 PDEVICE_EXTENSION DeviceExt
= IrpContext
->DeviceExt
;
650 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext
);
652 DeviceToVerify
= IrpContext
->Stack
->Parameters
.VerifyVolume
.DeviceObject
;
653 Status
= VfatBlockDeviceIoControl(DeviceToVerify
,
654 IOCTL_DISK_CHECK_VERIFY
,
660 DeviceToVerify
->Flags
&= ~DO_VERIFY_VOLUME
;
661 if (!NT_SUCCESS(Status
) && Status
!= STATUS_VERIFY_REQUIRED
)
663 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status
);
664 Status
= STATUS_WRONG_VOLUME
;
668 Status
= VfatHasFileSystem(DeviceToVerify
, &RecognizedFS
, &FatInfo
);
669 if (!NT_SUCCESS(Status
) || RecognizedFS
== FALSE
)
671 Status
= STATUS_WRONG_VOLUME
;
673 else if (sizeof(FATINFO
) == RtlCompareMemory(&FatInfo
, &DeviceExt
->FatInfo
, sizeof(FATINFO
)))
677 * Preformatted floppy disks have very often a serial number of 0000:0000.
678 * We should calculate a crc sum over the sectors from the root directory as secondary volume number.
679 * Each write to the root directory must update this crc sum.
684 Status
= STATUS_WRONG_VOLUME
;
695 PVFAT_IRP_CONTEXT IrpContext
)
697 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext
);
698 return STATUS_INVALID_DEVICE_REQUEST
;
704 VfatGetRetrievalPointers(
705 PVFAT_IRP_CONTEXT IrpContext
)
707 PIO_STACK_LOCATION Stack
;
709 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers
;
710 PFILE_OBJECT FileObject
;
711 ULONG MaxExtentCount
;
713 PDEVICE_EXTENSION DeviceExt
;
715 ULONG CurrentCluster
;
719 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext
);
721 DeviceExt
= IrpContext
->DeviceExt
;
722 FileObject
= IrpContext
->FileObject
;
723 Stack
= IrpContext
->Stack
;
724 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(STARTING_VCN_INPUT_BUFFER
) ||
725 Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
== NULL
)
727 return STATUS_INVALID_PARAMETER
;
730 if (IrpContext
->Irp
->UserBuffer
== NULL
||
731 Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(RETRIEVAL_POINTERS_BUFFER
))
733 return STATUS_BUFFER_TOO_SMALL
;
736 Fcb
= FileObject
->FsContext
;
738 ExAcquireResourceSharedLite(&Fcb
->MainResource
, TRUE
);
740 Vcn
= ((PSTARTING_VCN_INPUT_BUFFER
)Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
)->StartingVcn
;
741 RetrievalPointers
= IrpContext
->Irp
->UserBuffer
;
743 MaxExtentCount
= ((Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
- sizeof(RetrievalPointers
->ExtentCount
) - sizeof(RetrievalPointers
->StartingVcn
)) / sizeof(RetrievalPointers
->Extents
[0]));
745 if (Vcn
.QuadPart
>= Fcb
->RFCB
.AllocationSize
.QuadPart
/ DeviceExt
->FatInfo
.BytesPerCluster
)
747 Status
= STATUS_INVALID_PARAMETER
;
751 CurrentCluster
= FirstCluster
= vfatDirEntryGetFirstCluster(DeviceExt
, &Fcb
->entry
);
752 Status
= OffsetToCluster(DeviceExt
, FirstCluster
,
753 Vcn
.u
.LowPart
* DeviceExt
->FatInfo
.BytesPerCluster
,
754 &CurrentCluster
, FALSE
);
755 if (!NT_SUCCESS(Status
))
760 RetrievalPointers
->StartingVcn
= Vcn
;
761 RetrievalPointers
->ExtentCount
= 0;
762 RetrievalPointers
->Extents
[0].Lcn
.u
.HighPart
= 0;
763 RetrievalPointers
->Extents
[0].Lcn
.u
.LowPart
= CurrentCluster
- 2;
765 while (CurrentCluster
!= 0xffffffff && RetrievalPointers
->ExtentCount
< MaxExtentCount
)
767 LastCluster
= CurrentCluster
;
768 Status
= NextCluster(DeviceExt
, CurrentCluster
, &CurrentCluster
, FALSE
);
770 if (!NT_SUCCESS(Status
))
775 if (LastCluster
+ 1 != CurrentCluster
)
777 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].NextVcn
= Vcn
;
778 RetrievalPointers
->ExtentCount
++;
779 if (RetrievalPointers
->ExtentCount
< MaxExtentCount
)
781 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.HighPart
= 0;
782 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.LowPart
= CurrentCluster
- 2;
787 IrpContext
->Irp
->IoStatus
.Information
= sizeof(RETRIEVAL_POINTERS_BUFFER
) + (sizeof(RetrievalPointers
->Extents
[0]) * (RetrievalPointers
->ExtentCount
- 1));
788 Status
= STATUS_SUCCESS
;
791 ExReleaseResourceLite(&Fcb
->MainResource
);
799 PVFAT_IRP_CONTEXT IrpContext
)
801 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext
);
802 return STATUS_INVALID_DEVICE_REQUEST
;
808 PVFAT_IRP_CONTEXT IrpContext
)
812 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext
);
814 if (IrpContext
->Stack
->Parameters
.FileSystemControl
.OutputBufferLength
!= sizeof(ULONG
))
815 return STATUS_INVALID_BUFFER_SIZE
;
816 else if (!IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
)
817 return STATUS_INVALID_USER_BUFFER
;
819 Flags
= (PULONG
)IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
;
822 if ((IrpContext
->DeviceExt
->VolumeFcb
->Flags
& VCB_IS_DIRTY
) &&
823 !(IrpContext
->DeviceExt
->VolumeFcb
->Flags
& VCB_CLEAR_DIRTY
))
825 *Flags
|= VOLUME_IS_DIRTY
;
828 return STATUS_SUCCESS
;
834 PVFAT_IRP_CONTEXT IrpContext
)
837 PDEVICE_EXTENSION DeviceExt
;
838 NTSTATUS Status
= STATUS_SUCCESS
;
840 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext
);
841 DeviceExt
= IrpContext
->DeviceExt
;
843 if (!(DeviceExt
->VolumeFcb
->Flags
& VCB_IS_DIRTY
))
845 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
846 if (NT_SUCCESS(Status
))
848 /* unset clean shutdown bit */
849 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
850 Status
= WriteCluster(DeviceExt
, 1, eocMark
);
854 DeviceExt
->VolumeFcb
->Flags
&= ~VCB_CLEAR_DIRTY
;
861 VfatLockOrUnlockVolume(
862 PVFAT_IRP_CONTEXT IrpContext
,
865 PFILE_OBJECT FileObject
;
866 PDEVICE_EXTENSION DeviceExt
;
869 DPRINT("VfatLockOrUnlockVolume(%p, %d)\n", IrpContext
, Lock
);
871 DeviceExt
= IrpContext
->DeviceExt
;
872 FileObject
= IrpContext
->FileObject
;
873 Fcb
= FileObject
->FsContext
;
875 /* Only allow locking with the volume open */
876 if (!(Fcb
->Flags
& FCB_IS_VOLUME
))
878 return STATUS_ACCESS_DENIED
;
881 /* Bail out if it's already in the demanded state */
882 if (((DeviceExt
->Flags
& VCB_VOLUME_LOCKED
) && Lock
) ||
883 (!(DeviceExt
->Flags
& VCB_VOLUME_LOCKED
) && !Lock
))
885 return STATUS_ACCESS_DENIED
;
888 /* Deny locking if we're not alone */
889 if (Lock
&& DeviceExt
->OpenHandleCount
!= 1)
891 return STATUS_ACCESS_DENIED
;
894 /* Finally, proceed */
897 DeviceExt
->Flags
|= VCB_VOLUME_LOCKED
;
901 DeviceExt
->Flags
&= ~VCB_VOLUME_LOCKED
;
904 return STATUS_SUCCESS
;
910 PVFAT_IRP_CONTEXT IrpContext
)
912 PDEVICE_EXTENSION DeviceExt
;
913 PLIST_ENTRY NextEntry
;
915 PFILE_OBJECT FileObject
;
919 DPRINT("VfatDismountVolume(%p)\n", IrpContext
);
921 DeviceExt
= IrpContext
->DeviceExt
;
922 FileObject
= IrpContext
->FileObject
;
924 /* We HAVE to be locked. Windows also allows dismount with no lock
925 * but we're here mainly for 1st stage, so KISS
927 if (!(DeviceExt
->Flags
& VCB_VOLUME_LOCKED
))
929 return STATUS_ACCESS_DENIED
;
932 /* Race condition? */
933 if (DeviceExt
->Flags
& VCB_DISMOUNT_PENDING
)
935 return STATUS_VOLUME_DISMOUNTED
;
938 /* Notify we'll dismount. Pass that point there's no reason we fail */
939 FsRtlNotifyVolumeEvent(IrpContext
->Stack
->FileObject
, FSRTL_VOLUME_DISMOUNT
);
941 ExAcquireResourceExclusiveLite(&DeviceExt
->FatResource
, TRUE
);
943 if (DeviceExt
->VolumeFcb
->Flags
& VCB_CLEAR_DIRTY
)
945 /* Set clean shutdown bit */
946 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
947 if (NT_SUCCESS(Status
))
949 eocMark
|= DeviceExt
->CleanShutBitMask
;
950 if (NT_SUCCESS(WriteCluster(DeviceExt
, 1, eocMark
)))
951 DeviceExt
->VolumeFcb
->Flags
&= ~VCB_IS_DIRTY
;
955 /* Flush volume & files */
956 VfatFlushVolume(DeviceExt
, (PVFATFCB
)FileObject
->FsContext
);
958 /* Rebrowse the FCB in order to free them now */
959 while (!IsListEmpty(&DeviceExt
->FcbListHead
))
961 NextEntry
= RemoveHeadList(&DeviceExt
->FcbListHead
);
962 Fcb
= CONTAINING_RECORD(NextEntry
, VFATFCB
, FcbListEntry
);
966 /* Mark we're being dismounted */
967 DeviceExt
->Flags
|= VCB_DISMOUNT_PENDING
;
968 #ifndef ENABLE_SWAPOUT
969 IrpContext
->DeviceObject
->Vpb
->Flags
&= ~VPB_MOUNTED
;
972 ExReleaseResourceLite(&DeviceExt
->FatResource
);
974 /* Release a few resources and quit, we're done */
975 ExDeleteResourceLite(&DeviceExt
->DirResource
);
976 ExDeleteResourceLite(&DeviceExt
->FatResource
);
977 ObDereferenceObject(DeviceExt
->FATFileObject
);
979 return STATUS_SUCCESS
;
983 * FUNCTION: File system control
986 VfatFileSystemControl(
987 PVFAT_IRP_CONTEXT IrpContext
)
991 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext
);
994 ASSERT(IrpContext
->Irp
);
995 ASSERT(IrpContext
->Stack
);
997 IrpContext
->Irp
->IoStatus
.Information
= 0;
999 switch (IrpContext
->MinorFunction
)
1001 case IRP_MN_KERNEL_CALL
:
1002 case IRP_MN_USER_FS_REQUEST
:
1003 switch(IrpContext
->Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
1005 case FSCTL_GET_VOLUME_BITMAP
:
1006 Status
= VfatGetVolumeBitmap(IrpContext
);
1009 case FSCTL_GET_RETRIEVAL_POINTERS
:
1010 Status
= VfatGetRetrievalPointers(IrpContext
);
1013 case FSCTL_MOVE_FILE
:
1014 Status
= VfatMoveFile(IrpContext
);
1017 case FSCTL_IS_VOLUME_DIRTY
:
1018 Status
= VfatIsVolumeDirty(IrpContext
);
1021 case FSCTL_MARK_VOLUME_DIRTY
:
1022 Status
= VfatMarkVolumeDirty(IrpContext
);
1025 case FSCTL_LOCK_VOLUME
:
1026 Status
= VfatLockOrUnlockVolume(IrpContext
, TRUE
);
1029 case FSCTL_UNLOCK_VOLUME
:
1030 Status
= VfatLockOrUnlockVolume(IrpContext
, FALSE
);
1033 case FSCTL_DISMOUNT_VOLUME
:
1034 Status
= VfatDismountVolume(IrpContext
);
1038 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1042 case IRP_MN_MOUNT_VOLUME
:
1043 Status
= VfatMount(IrpContext
);
1046 case IRP_MN_VERIFY_VOLUME
:
1047 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
1048 Status
= VfatVerify(IrpContext
);
1052 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext
->MinorFunction
);
1053 Status
= STATUS_INVALID_DEVICE_REQUEST
;