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 faild (%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 faild (%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
;
124 else if (DiskGeometry
.MediaType
== Unknown
)
127 * Floppy disk driver can return Unknown as media type if it
128 * doesn't know yet what floppy in the drive really is. This is
129 * perfectly correct to do under Windows.
131 *RecognizedFS
= TRUE
;
132 DiskGeometry
.BytesPerSector
= 512;
136 *RecognizedFS
= TRUE
;
141 Boot
= ExAllocatePoolWithTag(NonPagedPool
, DiskGeometry
.BytesPerSector
, TAG_VFAT
);
144 return STATUS_INSUFFICIENT_RESOURCES
;
149 /* Try to recognize FAT12/FAT16/FAT32 partitions */
150 Status
= VfatReadDisk(DeviceToMount
, &Offset
, DiskGeometry
.BytesPerSector
, (PUCHAR
) Boot
, FALSE
);
151 if (NT_SUCCESS(Status
))
153 if (Boot
->Signatur1
!= 0xaa55)
155 *RecognizedFS
= FALSE
;
159 Boot
->BytesPerSector
!= 512 &&
160 Boot
->BytesPerSector
!= 1024 &&
161 Boot
->BytesPerSector
!= 2048 &&
162 Boot
->BytesPerSector
!= 4096)
164 DPRINT1("BytesPerSector %u\n", Boot
->BytesPerSector
);
165 *RecognizedFS
= FALSE
;
169 Boot
->FATCount
!= 1 &&
172 DPRINT1("FATCount %u\n", Boot
->FATCount
);
173 *RecognizedFS
= FALSE
;
177 Boot
->Media
!= 0xf0 &&
178 Boot
->Media
!= 0xf8 &&
179 Boot
->Media
!= 0xf9 &&
180 Boot
->Media
!= 0xfa &&
181 Boot
->Media
!= 0xfb &&
182 Boot
->Media
!= 0xfc &&
183 Boot
->Media
!= 0xfd &&
184 Boot
->Media
!= 0xfe &&
187 DPRINT1("Media %02x\n", Boot
->Media
);
188 *RecognizedFS
= FALSE
;
192 Boot
->SectorsPerCluster
!= 1 &&
193 Boot
->SectorsPerCluster
!= 2 &&
194 Boot
->SectorsPerCluster
!= 4 &&
195 Boot
->SectorsPerCluster
!= 8 &&
196 Boot
->SectorsPerCluster
!= 16 &&
197 Boot
->SectorsPerCluster
!= 32 &&
198 Boot
->SectorsPerCluster
!= 64 &&
199 Boot
->SectorsPerCluster
!= 128)
201 DPRINT1("SectorsPerCluster %02x\n", Boot
->SectorsPerCluster
);
202 *RecognizedFS
= FALSE
;
206 Boot
->BytesPerSector
* Boot
->SectorsPerCluster
> 32 * 1024)
208 DPRINT1("ClusterSize %dx\n", Boot
->BytesPerSector
* Boot
->SectorsPerCluster
);
209 *RecognizedFS
= FALSE
;
214 FatInfo
.VolumeID
= Boot
->VolumeID
;
215 FatInfo
.FATStart
= Boot
->ReservedSectors
;
216 FatInfo
.FATCount
= Boot
->FATCount
;
217 FatInfo
.FATSectors
= Boot
->FATSectors
? Boot
->FATSectors
: ((struct _BootSector32
*) Boot
)->FATSectors32
;
218 FatInfo
.BytesPerSector
= Boot
->BytesPerSector
;
219 FatInfo
.SectorsPerCluster
= Boot
->SectorsPerCluster
;
220 FatInfo
.BytesPerCluster
= FatInfo
.BytesPerSector
* FatInfo
.SectorsPerCluster
;
221 FatInfo
.rootDirectorySectors
= ((Boot
->RootEntries
* 32) + Boot
->BytesPerSector
- 1) / Boot
->BytesPerSector
;
222 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
223 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
224 FatInfo
.Sectors
= Sectors
= Boot
->Sectors
? Boot
->Sectors
: Boot
->SectorsHuge
;
225 Sectors
-= Boot
->ReservedSectors
+ FatInfo
.FATCount
* FatInfo
.FATSectors
+ FatInfo
.rootDirectorySectors
;
226 FatInfo
.NumberOfClusters
= Sectors
/ Boot
->SectorsPerCluster
;
227 if (FatInfo
.NumberOfClusters
< 4085)
230 FatInfo
.FatType
= FAT12
;
231 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
233 else if (FatInfo
.NumberOfClusters
>= 65525)
236 FatInfo
.FatType
= FAT32
;
237 FatInfo
.RootCluster
= ((struct _BootSector32
*) Boot
)->RootCluster
;
238 FatInfo
.rootStart
= FatInfo
.dataStart
+ ((FatInfo
.RootCluster
- 2) * FatInfo
.SectorsPerCluster
);
239 FatInfo
.VolumeID
= ((struct _BootSector32
*) Boot
)->VolumeID
;
244 FatInfo
.FatType
= FAT16
;
245 FatInfo
.RootCluster
= FatInfo
.rootStart
/ FatInfo
.SectorsPerCluster
;
248 if (PartitionInfoIsValid
&&
249 FatInfo
.Sectors
> PartitionInfo
.PartitionLength
.QuadPart
/ FatInfo
.BytesPerSector
)
251 *RecognizedFS
= FALSE
;
254 if (pFatInfo
&& *RecognizedFS
)
264 if (!*RecognizedFS
&& PartitionInfoIsValid
)
266 BootFatX
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(struct _BootSectorFatX
), TAG_VFAT
);
267 if (BootFatX
== NULL
)
270 return STATUS_INSUFFICIENT_RESOURCES
;
275 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
276 Status
= VfatReadDisk(DeviceToMount
, &Offset
, sizeof(struct _BootSectorFatX
), (PUCHAR
) BootFatX
, FALSE
);
277 if (NT_SUCCESS(Status
))
279 *RecognizedFS
= TRUE
;
280 if (BootFatX
->SysType
[0] != 'F' ||
281 BootFatX
->SysType
[1] != 'A' ||
282 BootFatX
->SysType
[2] != 'T' ||
283 BootFatX
->SysType
[3] != 'X')
285 DPRINT1("SysType %c%c%c%c\n", BootFatX
->SysType
[0], BootFatX
->SysType
[1], BootFatX
->SysType
[2], BootFatX
->SysType
[3]);
290 BootFatX
->SectorsPerCluster
!= 1 &&
291 BootFatX
->SectorsPerCluster
!= 2 &&
292 BootFatX
->SectorsPerCluster
!= 4 &&
293 BootFatX
->SectorsPerCluster
!= 8 &&
294 BootFatX
->SectorsPerCluster
!= 16 &&
295 BootFatX
->SectorsPerCluster
!= 32 &&
296 BootFatX
->SectorsPerCluster
!= 64 &&
297 BootFatX
->SectorsPerCluster
!= 128)
299 DPRINT1("SectorsPerCluster %lu\n", BootFatX
->SectorsPerCluster
);
305 FatInfo
.BytesPerSector
= DiskGeometry
.BytesPerSector
;
306 FatInfo
.SectorsPerCluster
= BootFatX
->SectorsPerCluster
;
307 FatInfo
.rootDirectorySectors
= BootFatX
->SectorsPerCluster
;
308 FatInfo
.BytesPerCluster
= BootFatX
->SectorsPerCluster
* DiskGeometry
.BytesPerSector
;
309 FatInfo
.Sectors
= (ULONG
)(PartitionInfo
.PartitionLength
.QuadPart
/ DiskGeometry
.BytesPerSector
);
310 if (FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
< 65525)
313 FatInfo
.FatType
= FATX16
;
318 FatInfo
.FatType
= FATX32
;
320 FatInfo
.VolumeID
= BootFatX
->VolumeID
;
321 FatInfo
.FATStart
= sizeof(struct _BootSectorFatX
) / DiskGeometry
.BytesPerSector
;
322 FatInfo
.FATCount
= BootFatX
->FATCount
;
324 ROUND_UP(FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
* (FatInfo
.FatType
== FATX16
? 2 : 4), 4096) /
325 FatInfo
.BytesPerSector
;
326 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
327 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
328 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
329 FatInfo
.NumberOfClusters
= (FatInfo
.Sectors
- FatInfo
.dataStart
) / FatInfo
.SectorsPerCluster
;
331 if (pFatInfo
&& *RecognizedFS
)
337 ExFreePool(BootFatX
);
340 DPRINT("VfatHasFileSystem done\n");
345 * FUNCTION: Mounts the device
350 PDEVICE_EXTENSION DeviceExt
,
351 PDEVICE_OBJECT DeviceToMount
)
354 BOOLEAN RecognizedFS
;
356 DPRINT("Mounting VFAT device...\n");
358 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &DeviceExt
->FatInfo
);
359 if (!NT_SUCCESS(Status
))
363 DPRINT("MountVfatdev %u, PAGE_SIZE = %d\n", DeviceExt
->FatInfo
.BytesPerCluster
, PAGE_SIZE
);
365 return STATUS_SUCCESS
;
370 * FUNCTION: Mount the filesystem
375 PVFAT_IRP_CONTEXT IrpContext
)
377 PDEVICE_OBJECT DeviceObject
= NULL
;
378 PDEVICE_EXTENSION DeviceExt
= NULL
;
379 BOOLEAN RecognizedFS
;
382 PVFATFCB VolumeFcb
= NULL
;
384 PDEVICE_OBJECT DeviceToMount
;
386 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\$$Fat$$");
387 UNICODE_STRING VolumeNameU
= RTL_CONSTANT_STRING(L
"\\$$Volume$$");
392 DPRINT("VfatMount(IrpContext %p)\n", IrpContext
);
396 if (IrpContext
->DeviceObject
!= VfatGlobalData
->DeviceObject
)
398 Status
= STATUS_INVALID_DEVICE_REQUEST
;
402 DeviceToMount
= IrpContext
->Stack
->Parameters
.MountVolume
.DeviceObject
;
403 Vpb
= IrpContext
->Stack
->Parameters
.MountVolume
.Vpb
;
405 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &FatInfo
);
406 if (!NT_SUCCESS(Status
))
411 if (RecognizedFS
== FALSE
)
413 DPRINT("VFAT: Unrecognized Volume\n");
414 Status
= STATUS_UNRECOGNIZED_VOLUME
;
418 /* Use prime numbers for the table size */
419 if (FatInfo
.FatType
== FAT12
)
421 HashTableSize
= 4099; // 4096 = 4 * 1024
423 else if (FatInfo
.FatType
== FAT16
||
424 FatInfo
.FatType
== FATX16
)
426 HashTableSize
= 16411; // 16384 = 16 * 1024
430 HashTableSize
= 65537; // 65536 = 64 * 1024;
432 HashTableSize
= FCB_HASH_TABLE_SIZE
;
433 DPRINT("VFAT: Recognized volume\n");
434 Status
= IoCreateDevice(VfatGlobalData
->DriverObject
,
435 ROUND_UP(sizeof (DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
,
437 FILE_DEVICE_DISK_FILE_SYSTEM
,
438 DeviceToMount
->Characteristics
,
441 if (!NT_SUCCESS(Status
))
446 DeviceExt
= DeviceObject
->DeviceExtension
;
447 RtlZeroMemory(DeviceExt
, ROUND_UP(sizeof(DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
);
448 DeviceExt
->FcbHashTable
= (HASHENTRY
**)((ULONG_PTR
)DeviceExt
+ ROUND_UP(sizeof(DEVICE_EXTENSION
), sizeof(ULONG
)));
449 DeviceExt
->HashTableSize
= HashTableSize
;
451 /* use same vpb as device disk */
452 DeviceObject
->Vpb
= Vpb
;
453 DeviceToMount
->Vpb
= Vpb
;
455 Status
= VfatMountDevice(DeviceExt
, DeviceToMount
);
456 if (!NT_SUCCESS(Status
))
458 /* FIXME: delete device object */
462 DPRINT("BytesPerSector: %u\n", DeviceExt
->FatInfo
.BytesPerSector
);
463 DPRINT("SectorsPerCluster: %u\n", DeviceExt
->FatInfo
.SectorsPerCluster
);
464 DPRINT("FATCount: %u\n", DeviceExt
->FatInfo
.FATCount
);
465 DPRINT("FATSectors: %u\n", DeviceExt
->FatInfo
.FATSectors
);
466 DPRINT("RootStart: %u\n", DeviceExt
->FatInfo
.rootStart
);
467 DPRINT("DataStart: %u\n", DeviceExt
->FatInfo
.dataStart
);
468 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
470 DPRINT("RootCluster: %u\n", DeviceExt
->FatInfo
.RootCluster
);
473 switch (DeviceExt
->FatInfo
.FatType
)
476 DeviceExt
->GetNextCluster
= FAT12GetNextCluster
;
477 DeviceExt
->FindAndMarkAvailableCluster
= FAT12FindAndMarkAvailableCluster
;
478 DeviceExt
->WriteCluster
= FAT12WriteCluster
;
479 DeviceExt
->CleanShutBitMask
= 0;
484 DeviceExt
->GetNextCluster
= FAT16GetNextCluster
;
485 DeviceExt
->FindAndMarkAvailableCluster
= FAT16FindAndMarkAvailableCluster
;
486 DeviceExt
->WriteCluster
= FAT16WriteCluster
;
487 DeviceExt
->CleanShutBitMask
= 0x8000;
492 DeviceExt
->GetNextCluster
= FAT32GetNextCluster
;
493 DeviceExt
->FindAndMarkAvailableCluster
= FAT32FindAndMarkAvailableCluster
;
494 DeviceExt
->WriteCluster
= FAT32WriteCluster
;
495 DeviceExt
->CleanShutBitMask
= 0x80000000;
499 if (DeviceExt
->FatInfo
.FatType
== FATX16
||
500 DeviceExt
->FatInfo
.FatType
== FATX32
)
502 DeviceExt
->Flags
|= VCB_IS_FATX
;
503 DeviceExt
->GetNextDirEntry
= FATXGetNextDirEntry
;
504 DeviceExt
->BaseDateYear
= 2000;
508 DeviceExt
->GetNextDirEntry
= FATGetNextDirEntry
;
509 DeviceExt
->BaseDateYear
= 1980;
512 DeviceExt
->StorageDevice
= DeviceToMount
;
513 DeviceExt
->StorageDevice
->Vpb
->DeviceObject
= DeviceObject
;
514 DeviceExt
->StorageDevice
->Vpb
->RealDevice
= DeviceExt
->StorageDevice
;
515 DeviceExt
->StorageDevice
->Vpb
->Flags
|= VPB_MOUNTED
;
516 DeviceObject
->StackSize
= DeviceExt
->StorageDevice
->StackSize
+ 1;
517 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
519 DPRINT("FsDeviceObject %p\n", DeviceObject
);
521 /* Initialize this resource early ... it's used in VfatCleanup */
522 ExInitializeResourceLite(&DeviceExt
->DirResource
);
524 DeviceExt
->IoVPB
= DeviceObject
->Vpb
;
525 DeviceExt
->SpareVPB
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(VPB
), TAG_VFAT
);
526 if (DeviceExt
->SpareVPB
== NULL
)
528 Status
= STATUS_INSUFFICIENT_RESOURCES
;
532 DeviceExt
->FATFileObject
= IoCreateStreamFileObject(NULL
, DeviceExt
->StorageDevice
);
533 Fcb
= vfatNewFCB(DeviceExt
, &NameU
);
536 Status
= STATUS_INSUFFICIENT_RESOURCES
;
540 Ccb
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
543 Status
= STATUS_INSUFFICIENT_RESOURCES
;
547 RtlZeroMemory(Ccb
, sizeof (VFATCCB
));
548 DeviceExt
->FATFileObject
->FsContext
= Fcb
;
549 DeviceExt
->FATFileObject
->FsContext2
= Ccb
;
550 DeviceExt
->FATFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
551 DeviceExt
->FATFileObject
->PrivateCacheMap
= NULL
;
552 DeviceExt
->FATFileObject
->Vpb
= DeviceObject
->Vpb
;
553 Fcb
->FileObject
= DeviceExt
->FATFileObject
;
555 Fcb
->Flags
|= FCB_IS_FAT
;
557 Fcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.FATSectors
* DeviceExt
->FatInfo
.BytesPerSector
;
558 Fcb
->RFCB
.ValidDataLength
= Fcb
->RFCB
.FileSize
;
559 Fcb
->RFCB
.AllocationSize
= Fcb
->RFCB
.FileSize
;
563 CcInitializeCacheMap(DeviceExt
->FATFileObject
,
564 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
566 &VfatGlobalData
->CacheMgrCallbacks
,
569 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
571 Status
= _SEH2_GetExceptionCode();
576 DeviceExt
->LastAvailableCluster
= 2;
577 ExInitializeResourceLite(&DeviceExt
->FatResource
);
579 InitializeListHead(&DeviceExt
->FcbListHead
);
581 VolumeFcb
= vfatNewFCB(DeviceExt
, &VolumeNameU
);
582 if (VolumeFcb
== NULL
)
584 Status
= STATUS_INSUFFICIENT_RESOURCES
;
588 VolumeFcb
->Flags
= FCB_IS_VOLUME
;
589 VolumeFcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.Sectors
* DeviceExt
->FatInfo
.BytesPerSector
;
590 VolumeFcb
->RFCB
.ValidDataLength
= VolumeFcb
->RFCB
.FileSize
;
591 VolumeFcb
->RFCB
.AllocationSize
= VolumeFcb
->RFCB
.FileSize
;
592 DeviceExt
->VolumeFcb
= VolumeFcb
;
594 ExAcquireResourceExclusiveLite(&VfatGlobalData
->VolumeListLock
, TRUE
);
595 InsertHeadList(&VfatGlobalData
->VolumeListHead
, &DeviceExt
->VolumeListEntry
);
596 ExReleaseResourceLite(&VfatGlobalData
->VolumeListLock
);
598 /* read serial number */
599 DeviceObject
->Vpb
->SerialNumber
= DeviceExt
->FatInfo
.VolumeID
;
601 /* read volume label */
602 ReadVolumeLabel(DeviceExt
, DeviceObject
->Vpb
);
604 /* read clean shutdown bit status */
605 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
606 if (NT_SUCCESS(Status
))
608 if (eocMark
& DeviceExt
->CleanShutBitMask
)
610 /* unset clean shutdown bit */
611 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
612 WriteCluster(DeviceExt
, 1, eocMark
);
613 VolumeFcb
->Flags
|= VCB_CLEAR_DIRTY
;
617 VolumeFcb
->Flags
|= VCB_IS_DIRTY
;
619 FsRtlNotifyVolumeEvent(DeviceExt
->FATFileObject
, FSRTL_VOLUME_MOUNT
);
620 FsRtlNotifyInitializeSync(&DeviceExt
->NotifySync
);
621 InitializeListHead(&DeviceExt
->NotifyList
);
623 DPRINT("Mount success\n");
625 Status
= STATUS_SUCCESS
;
628 if (!NT_SUCCESS(Status
))
631 if (DeviceExt
&& DeviceExt
->FATFileObject
)
632 ObDereferenceObject (DeviceExt
->FATFileObject
);
633 if (DeviceExt
&& DeviceExt
->SpareVPB
)
634 ExFreePoolWithTag(DeviceExt
->SpareVPB
, TAG_VFAT
);
640 IoDeleteDevice(DeviceObject
);
648 * FUNCTION: Verify the filesystem
653 PVFAT_IRP_CONTEXT IrpContext
)
655 PDEVICE_OBJECT DeviceToVerify
;
656 NTSTATUS Status
= STATUS_SUCCESS
;
658 BOOLEAN RecognizedFS
;
659 PDEVICE_EXTENSION DeviceExt
= IrpContext
->DeviceExt
;
661 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext
);
663 DeviceToVerify
= IrpContext
->Stack
->Parameters
.VerifyVolume
.DeviceObject
;
664 Status
= VfatBlockDeviceIoControl(DeviceToVerify
,
665 IOCTL_DISK_CHECK_VERIFY
,
671 DeviceToVerify
->Flags
&= ~DO_VERIFY_VOLUME
;
672 if (!NT_SUCCESS(Status
) && Status
!= STATUS_VERIFY_REQUIRED
)
674 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status
);
675 Status
= STATUS_WRONG_VOLUME
;
679 Status
= VfatHasFileSystem(DeviceToVerify
, &RecognizedFS
, &FatInfo
);
680 if (!NT_SUCCESS(Status
) || RecognizedFS
== FALSE
)
682 Status
= STATUS_WRONG_VOLUME
;
684 else if (sizeof(FATINFO
) == RtlCompareMemory(&FatInfo
, &DeviceExt
->FatInfo
, sizeof(FATINFO
)))
688 * Preformated floppy disks have very often a serial number of 0000:0000.
689 * We should calculate a crc sum over the sectors from the root directory as secondary volume number.
690 * Each write to the root directory must update this crc sum.
695 Status
= STATUS_WRONG_VOLUME
;
706 PVFAT_IRP_CONTEXT IrpContext
)
708 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext
);
709 return STATUS_INVALID_DEVICE_REQUEST
;
715 VfatGetRetrievalPointers(
716 PVFAT_IRP_CONTEXT IrpContext
)
718 PIO_STACK_LOCATION Stack
;
720 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers
;
721 PFILE_OBJECT FileObject
;
722 ULONG MaxExtentCount
;
724 PDEVICE_EXTENSION DeviceExt
;
726 ULONG CurrentCluster
;
730 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext
);
732 DeviceExt
= IrpContext
->DeviceExt
;
733 FileObject
= IrpContext
->FileObject
;
734 Stack
= IrpContext
->Stack
;
735 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(STARTING_VCN_INPUT_BUFFER
) ||
736 Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
== NULL
)
738 return STATUS_INVALID_PARAMETER
;
741 if (IrpContext
->Irp
->UserBuffer
== NULL
||
742 Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(RETRIEVAL_POINTERS_BUFFER
))
744 return STATUS_BUFFER_TOO_SMALL
;
747 Fcb
= FileObject
->FsContext
;
749 ExAcquireResourceSharedLite(&Fcb
->MainResource
, TRUE
);
751 Vcn
= ((PSTARTING_VCN_INPUT_BUFFER
)Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
)->StartingVcn
;
752 RetrievalPointers
= IrpContext
->Irp
->UserBuffer
;
754 MaxExtentCount
= ((Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
- sizeof(RetrievalPointers
->ExtentCount
) - sizeof(RetrievalPointers
->StartingVcn
)) / sizeof(RetrievalPointers
->Extents
[0]));
756 if (Vcn
.QuadPart
>= Fcb
->RFCB
.AllocationSize
.QuadPart
/ DeviceExt
->FatInfo
.BytesPerCluster
)
758 Status
= STATUS_INVALID_PARAMETER
;
762 CurrentCluster
= FirstCluster
= vfatDirEntryGetFirstCluster(DeviceExt
, &Fcb
->entry
);
763 Status
= OffsetToCluster(DeviceExt
, FirstCluster
,
764 Vcn
.u
.LowPart
* DeviceExt
->FatInfo
.BytesPerCluster
,
765 &CurrentCluster
, FALSE
);
766 if (!NT_SUCCESS(Status
))
771 RetrievalPointers
->StartingVcn
= Vcn
;
772 RetrievalPointers
->ExtentCount
= 0;
773 RetrievalPointers
->Extents
[0].Lcn
.u
.HighPart
= 0;
774 RetrievalPointers
->Extents
[0].Lcn
.u
.LowPart
= CurrentCluster
- 2;
776 while (CurrentCluster
!= 0xffffffff && RetrievalPointers
->ExtentCount
< MaxExtentCount
)
778 LastCluster
= CurrentCluster
;
779 Status
= NextCluster(DeviceExt
, CurrentCluster
, &CurrentCluster
, FALSE
);
781 if (!NT_SUCCESS(Status
))
786 if (LastCluster
+ 1 != CurrentCluster
)
788 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].NextVcn
= Vcn
;
789 RetrievalPointers
->ExtentCount
++;
790 if (RetrievalPointers
->ExtentCount
< MaxExtentCount
)
792 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.HighPart
= 0;
793 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.LowPart
= CurrentCluster
- 2;
798 IrpContext
->Irp
->IoStatus
.Information
= sizeof(RETRIEVAL_POINTERS_BUFFER
) + (sizeof(RetrievalPointers
->Extents
[0]) * (RetrievalPointers
->ExtentCount
- 1));
799 Status
= STATUS_SUCCESS
;
802 ExReleaseResourceLite(&Fcb
->MainResource
);
810 PVFAT_IRP_CONTEXT IrpContext
)
812 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext
);
813 return STATUS_INVALID_DEVICE_REQUEST
;
819 PVFAT_IRP_CONTEXT IrpContext
)
823 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext
);
825 if (IrpContext
->Stack
->Parameters
.FileSystemControl
.OutputBufferLength
!= sizeof(ULONG
))
826 return STATUS_INVALID_BUFFER_SIZE
;
827 else if (!IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
)
828 return STATUS_INVALID_USER_BUFFER
;
830 Flags
= (PULONG
)IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
;
833 if ((IrpContext
->DeviceExt
->VolumeFcb
->Flags
& VCB_IS_DIRTY
) &&
834 !(IrpContext
->DeviceExt
->VolumeFcb
->Flags
& VCB_CLEAR_DIRTY
))
836 *Flags
|= VOLUME_IS_DIRTY
;
839 return STATUS_SUCCESS
;
845 PVFAT_IRP_CONTEXT IrpContext
)
848 PDEVICE_EXTENSION DeviceExt
;
849 NTSTATUS Status
= STATUS_SUCCESS
;
851 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext
);
852 DeviceExt
= IrpContext
->DeviceExt
;
854 if (!(DeviceExt
->VolumeFcb
->Flags
& VCB_IS_DIRTY
))
856 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
857 if (NT_SUCCESS(Status
))
859 /* unset clean shutdown bit */
860 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
861 Status
= WriteCluster(DeviceExt
, 1, eocMark
);
865 DeviceExt
->VolumeFcb
->Flags
&= ~VCB_CLEAR_DIRTY
;
872 VfatLockOrUnlockVolume(
873 PVFAT_IRP_CONTEXT IrpContext
,
876 PFILE_OBJECT FileObject
;
877 PDEVICE_EXTENSION DeviceExt
;
880 DPRINT("VfatLockOrUnlockVolume(%p, %d)\n", IrpContext
, Lock
);
882 DeviceExt
= IrpContext
->DeviceExt
;
883 FileObject
= IrpContext
->FileObject
;
884 Fcb
= FileObject
->FsContext
;
886 /* Only allow locking with the volume open */
887 if (!(Fcb
->Flags
& FCB_IS_VOLUME
))
889 return STATUS_ACCESS_DENIED
;
892 /* Bail out if it's already in the demanded state */
893 if (((DeviceExt
->Flags
& VCB_VOLUME_LOCKED
) && Lock
) ||
894 (!(DeviceExt
->Flags
& VCB_VOLUME_LOCKED
) && !Lock
))
896 return STATUS_ACCESS_DENIED
;
899 /* Deny locking if we're not alone */
900 if (Lock
&& DeviceExt
->OpenHandleCount
!= 1)
902 return STATUS_ACCESS_DENIED
;
905 /* Finally, proceed */
908 DeviceExt
->Flags
|= VCB_VOLUME_LOCKED
;
912 DeviceExt
->Flags
&= ~VCB_VOLUME_LOCKED
;
915 return STATUS_SUCCESS
;
921 PVFAT_IRP_CONTEXT IrpContext
)
923 PDEVICE_EXTENSION DeviceExt
;
924 PLIST_ENTRY NextEntry
;
926 PFILE_OBJECT FileObject
;
928 DPRINT("VfatDismountVolume(%p)\n", IrpContext
);
930 DeviceExt
= IrpContext
->DeviceExt
;
931 FileObject
= IrpContext
->FileObject
;
933 /* We HAVE to be locked. Windows also allows dismount with no lock
934 * but we're here mainly for 1st stage, so KISS
936 if (!(DeviceExt
->Flags
& VCB_VOLUME_LOCKED
))
938 return STATUS_ACCESS_DENIED
;
941 /* Race condition? */
942 if (DeviceExt
->Flags
& VCB_DISMOUNT_PENDING
)
944 return STATUS_VOLUME_DISMOUNTED
;
947 /* Notify we'll dismount. Pass that point there's no reason we fail */
948 FsRtlNotifyVolumeEvent(IrpContext
->Stack
->FileObject
, FSRTL_VOLUME_DISMOUNT
);
950 ExAcquireResourceExclusiveLite(&DeviceExt
->FatResource
, TRUE
);
952 /* Flush volume & files */
953 VfatFlushVolume(DeviceExt
, (PVFATFCB
)FileObject
->FsContext
);
955 /* Rebrowse the FCB in order to free them now */
956 while (!IsListEmpty(&DeviceExt
->FcbListHead
))
958 NextEntry
= RemoveHeadList(&DeviceExt
->FcbListHead
);
959 Fcb
= CONTAINING_RECORD(NextEntry
, VFATFCB
, FcbListEntry
);
963 /* Mark we're being dismounted */
964 DeviceExt
->Flags
|= VCB_DISMOUNT_PENDING
;
965 IrpContext
->DeviceObject
->Vpb
->Flags
&= ~VPB_MOUNTED
;
967 ExReleaseResourceLite(&DeviceExt
->FatResource
);
969 /* Release a few resources and quit, we're done */
970 ExDeleteResourceLite(&DeviceExt
->DirResource
);
971 ExDeleteResourceLite(&DeviceExt
->FatResource
);
972 ObDereferenceObject(DeviceExt
->FATFileObject
);
974 return STATUS_SUCCESS
;
978 * FUNCTION: File system control
981 VfatFileSystemControl(
982 PVFAT_IRP_CONTEXT IrpContext
)
986 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext
);
989 ASSERT(IrpContext
->Irp
);
990 ASSERT(IrpContext
->Stack
);
992 IrpContext
->Irp
->IoStatus
.Information
= 0;
994 switch (IrpContext
->MinorFunction
)
996 case IRP_MN_KERNEL_CALL
:
997 case IRP_MN_USER_FS_REQUEST
:
998 switch(IrpContext
->Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
1000 case FSCTL_GET_VOLUME_BITMAP
:
1001 Status
= VfatGetVolumeBitmap(IrpContext
);
1004 case FSCTL_GET_RETRIEVAL_POINTERS
:
1005 Status
= VfatGetRetrievalPointers(IrpContext
);
1008 case FSCTL_MOVE_FILE
:
1009 Status
= VfatMoveFile(IrpContext
);
1012 case FSCTL_IS_VOLUME_DIRTY
:
1013 Status
= VfatIsVolumeDirty(IrpContext
);
1016 case FSCTL_MARK_VOLUME_DIRTY
:
1017 Status
= VfatMarkVolumeDirty(IrpContext
);
1020 case FSCTL_LOCK_VOLUME
:
1021 Status
= VfatLockOrUnlockVolume(IrpContext
, TRUE
);
1024 case FSCTL_UNLOCK_VOLUME
:
1025 Status
= VfatLockOrUnlockVolume(IrpContext
, FALSE
);
1028 case FSCTL_DISMOUNT_VOLUME
:
1029 Status
= VfatDismountVolume(IrpContext
);
1033 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1037 case IRP_MN_MOUNT_VOLUME
:
1038 Status
= VfatMount(IrpContext
);
1041 case IRP_MN_VERIFY_VOLUME
:
1042 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
1043 Status
= VfatVerify(IrpContext
);
1047 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext
->MinorFunction
);
1048 Status
= STATUS_INVALID_DEVICE_REQUEST
;
1052 IrpContext
->Irp
->IoStatus
.Status
= Status
;
1054 IoCompleteRequest(IrpContext
->Irp
, IO_NO_INCREMENT
);
1055 VfatFreeIrpContext(IrpContext
);