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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 *****************************************************************/
31 /* FUNCTIONS ****************************************************************/
33 #define CACHEPAGESIZE(pDeviceExt) ((pDeviceExt)->FatInfo.BytesPerCluster > PAGE_SIZE ? \
34 (pDeviceExt)->FatInfo.BytesPerCluster : PAGE_SIZE)
36 #define VOLUME_IS_DIRTY 0x00000001
39 VfatHasFileSystem(PDEVICE_OBJECT DeviceToMount
,
40 PBOOLEAN RecognizedFS
,
44 PARTITION_INFORMATION PartitionInfo
;
45 DISK_GEOMETRY DiskGeometry
;
50 struct _BootSector
* Boot
;
51 struct _BootSectorFatX
* BootFatX
;
52 BOOLEAN PartitionInfoIsValid
= FALSE
;
54 DPRINT("VfatHasFileSystem\n");
56 *RecognizedFS
= FALSE
;
58 Size
= sizeof(DISK_GEOMETRY
);
59 Status
= VfatBlockDeviceIoControl(DeviceToMount
,
60 IOCTL_DISK_GET_DRIVE_GEOMETRY
,
66 if (!NT_SUCCESS(Status
))
68 DPRINT("VfatBlockDeviceIoControl faild (%x)\n", Status
);
71 FatInfo
.FixedMedia
= DiskGeometry
.MediaType
== FixedMedia
? TRUE
: FALSE
;
72 if (DiskGeometry
.MediaType
== FixedMedia
|| DiskGeometry
.MediaType
== RemovableMedia
)
74 // We have found a hard disk
75 Size
= sizeof(PARTITION_INFORMATION
);
76 Status
= VfatBlockDeviceIoControl(DeviceToMount
,
77 IOCTL_DISK_GET_PARTITION_INFO
,
83 if (!NT_SUCCESS(Status
))
85 DPRINT("VfatBlockDeviceIoControl faild (%x)\n", Status
);
88 PartitionInfoIsValid
= TRUE
;
89 DPRINT("Partition Information:\n");
90 DPRINT("StartingOffset %u\n", PartitionInfo
.StartingOffset
.QuadPart
/ 512);
91 DPRINT("PartitionLength %u\n", PartitionInfo
.PartitionLength
.QuadPart
/ 512);
92 DPRINT("HiddenSectors %u\n", PartitionInfo
.HiddenSectors
);
93 DPRINT("PartitionNumber %u\n", PartitionInfo
.PartitionNumber
);
94 DPRINT("PartitionType %u\n", PartitionInfo
.PartitionType
);
95 DPRINT("BootIndicator %u\n", PartitionInfo
.BootIndicator
);
96 DPRINT("RecognizedPartition %u\n", PartitionInfo
.RecognizedPartition
);
97 DPRINT("RewritePartition %u\n", PartitionInfo
.RewritePartition
);
98 if (PartitionInfo
.PartitionType
)
100 if (PartitionInfo
.PartitionType
== PARTITION_FAT_12
||
101 PartitionInfo
.PartitionType
== PARTITION_FAT_16
||
102 PartitionInfo
.PartitionType
== PARTITION_HUGE
||
103 PartitionInfo
.PartitionType
== PARTITION_FAT32
||
104 PartitionInfo
.PartitionType
== PARTITION_FAT32_XINT13
||
105 PartitionInfo
.PartitionType
== PARTITION_XINT13
)
107 *RecognizedFS
= TRUE
;
110 else if (DiskGeometry
.MediaType
== RemovableMedia
&&
111 PartitionInfo
.PartitionNumber
> 0 &&
112 PartitionInfo
.StartingOffset
.QuadPart
== 0 &&
113 PartitionInfo
.PartitionLength
.QuadPart
> 0)
115 /* This is possible a removable media formated as super floppy */
116 *RecognizedFS
= TRUE
;
119 else if (DiskGeometry
.MediaType
== Unknown
)
122 * Floppy disk driver can return Unknown as media type if it
123 * doesn't know yet what floppy in the drive really is. This is
124 * perfectly correct to do under Windows.
126 *RecognizedFS
= TRUE
;
127 DiskGeometry
.BytesPerSector
= 512;
131 *RecognizedFS
= TRUE
;
136 Boot
= ExAllocatePoolWithTag(NonPagedPool
, DiskGeometry
.BytesPerSector
, TAG_VFAT
);
139 return STATUS_INSUFFICIENT_RESOURCES
;
144 /* Try to recognize FAT12/FAT16/FAT32 partitions */
145 Status
= VfatReadDisk(DeviceToMount
, &Offset
, DiskGeometry
.BytesPerSector
, (PUCHAR
) Boot
, FALSE
);
146 if (NT_SUCCESS(Status
))
148 if (Boot
->Signatur1
!= 0xaa55)
150 *RecognizedFS
= FALSE
;
153 Boot
->BytesPerSector
!= 512 &&
154 Boot
->BytesPerSector
!= 1024 &&
155 Boot
->BytesPerSector
!= 2048 &&
156 Boot
->BytesPerSector
!= 4096)
158 DPRINT1("BytesPerSector %d\n", Boot
->BytesPerSector
);
159 *RecognizedFS
= FALSE
;
163 Boot
->FATCount
!= 1 &&
166 DPRINT1("FATCount %d\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
;
227 else if (FatInfo
.NumberOfClusters
>= 65525)
230 FatInfo
.FatType
= FAT32
;
231 FatInfo
.RootCluster
= ((struct _BootSector32
*) Boot
)->RootCluster
;
232 FatInfo
.rootStart
= FatInfo
.dataStart
+ ((FatInfo
.RootCluster
- 2) * FatInfo
.SectorsPerCluster
);
233 FatInfo
.VolumeID
= ((struct _BootSector32
*) Boot
)->VolumeID
;
238 FatInfo
.FatType
= FAT16
;
239 FatInfo
.RootCluster
= FatInfo
.rootStart
/ FatInfo
.SectorsPerCluster
;
241 if (PartitionInfoIsValid
&&
242 FatInfo
.Sectors
> PartitionInfo
.PartitionLength
.QuadPart
/ FatInfo
.BytesPerSector
)
244 *RecognizedFS
= FALSE
;
247 if (pFatInfo
&& *RecognizedFS
)
257 if (!*RecognizedFS
&& PartitionInfoIsValid
)
259 BootFatX
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(struct _BootSectorFatX
), TAG_VFAT
);
260 if (BootFatX
== NULL
)
263 return STATUS_INSUFFICIENT_RESOURCES
;
268 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
269 Status
= VfatReadDisk(DeviceToMount
, &Offset
, sizeof(struct _BootSectorFatX
), (PUCHAR
) BootFatX
, FALSE
);
270 if (NT_SUCCESS(Status
))
272 *RecognizedFS
= TRUE
;
273 if (BootFatX
->SysType
[0] != 'F' ||
274 BootFatX
->SysType
[1] != 'A' ||
275 BootFatX
->SysType
[2] != 'T' ||
276 BootFatX
->SysType
[3] != 'X')
278 DPRINT1("SysType %c%c%c%c\n", BootFatX
->SysType
[0], BootFatX
->SysType
[1], BootFatX
->SysType
[2], BootFatX
->SysType
[3]);
283 BootFatX
->SectorsPerCluster
!= 1 &&
284 BootFatX
->SectorsPerCluster
!= 2 &&
285 BootFatX
->SectorsPerCluster
!= 4 &&
286 BootFatX
->SectorsPerCluster
!= 8 &&
287 BootFatX
->SectorsPerCluster
!= 16 &&
288 BootFatX
->SectorsPerCluster
!= 32 &&
289 BootFatX
->SectorsPerCluster
!= 64 &&
290 BootFatX
->SectorsPerCluster
!= 128)
292 DPRINT1("SectorsPerCluster %lu\n", BootFatX
->SectorsPerCluster
);
298 FatInfo
.BytesPerSector
= DiskGeometry
.BytesPerSector
;
299 FatInfo
.SectorsPerCluster
= BootFatX
->SectorsPerCluster
;
300 FatInfo
.rootDirectorySectors
= BootFatX
->SectorsPerCluster
;
301 FatInfo
.BytesPerCluster
= BootFatX
->SectorsPerCluster
* DiskGeometry
.BytesPerSector
;
302 FatInfo
.Sectors
= (ULONG
)(PartitionInfo
.PartitionLength
.QuadPart
/ DiskGeometry
.BytesPerSector
);
303 if (FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
< 65525)
306 FatInfo
.FatType
= FATX16
;
311 FatInfo
.FatType
= FATX32
;
313 FatInfo
.VolumeID
= BootFatX
->VolumeID
;
314 FatInfo
.FATStart
= sizeof(struct _BootSectorFatX
) / DiskGeometry
.BytesPerSector
;
315 FatInfo
.FATCount
= BootFatX
->FATCount
;
317 ROUND_UP(FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
* (FatInfo
.FatType
== FATX16
? 2 : 4), 4096) /
318 FatInfo
.BytesPerSector
;
319 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
320 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
321 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
322 FatInfo
.NumberOfClusters
= (FatInfo
.Sectors
- FatInfo
.dataStart
) / FatInfo
.SectorsPerCluster
;
324 if (pFatInfo
&& *RecognizedFS
)
330 ExFreePool(BootFatX
);
333 DPRINT("VfatHasFileSystem done\n");
338 VfatMountDevice(PDEVICE_EXTENSION DeviceExt
,
339 PDEVICE_OBJECT DeviceToMount
)
341 * FUNCTION: Mounts the device
345 BOOLEAN RecognizedFS
;
347 DPRINT("Mounting VFAT device...\n");
349 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &DeviceExt
->FatInfo
);
350 if (!NT_SUCCESS(Status
))
354 DPRINT("MountVfatdev %d, PAGE_SIZE = %d\n", DeviceExt
->FatInfo
.BytesPerCluster
, PAGE_SIZE
);
357 return(STATUS_SUCCESS
);
362 VfatMount (PVFAT_IRP_CONTEXT IrpContext
)
364 * FUNCTION: Mount the filesystem
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 HashTableSize
= FCB_HASH_TABLE_SIZE
;
423 DPRINT("VFAT: Recognized volume\n");
424 Status
= IoCreateDevice(VfatGlobalData
->DriverObject
,
425 ROUND_UP(sizeof (DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
,
427 FILE_DEVICE_DISK_FILE_SYSTEM
,
431 if (!NT_SUCCESS(Status
))
436 DeviceObject
->Flags
= DeviceObject
->Flags
| DO_DIRECT_IO
;
437 DeviceExt
= (PVOID
) DeviceObject
->DeviceExtension
;
438 RtlZeroMemory(DeviceExt
, ROUND_UP(sizeof(DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
);
439 DeviceExt
->FcbHashTable
= (HASHENTRY
**)((ULONG_PTR
)DeviceExt
+ ROUND_UP(sizeof(DEVICE_EXTENSION
), sizeof(ULONG
)));
440 DeviceExt
->HashTableSize
= HashTableSize
;
442 /* use same vpb as device disk */
443 DeviceObject
->Vpb
= Vpb
;
444 DeviceToMount
->Vpb
= Vpb
;
446 Status
= VfatMountDevice(DeviceExt
, DeviceToMount
);
447 if (!NT_SUCCESS(Status
))
449 /* FIXME: delete device object */
453 DPRINT("BytesPerSector: %d\n", DeviceExt
->FatInfo
.BytesPerSector
);
454 DPRINT("SectorsPerCluster: %d\n", DeviceExt
->FatInfo
.SectorsPerCluster
);
455 DPRINT("FATCount: %d\n", DeviceExt
->FatInfo
.FATCount
);
456 DPRINT("FATSectors: %d\n", DeviceExt
->FatInfo
.FATSectors
);
457 DPRINT("RootStart: %d\n", DeviceExt
->FatInfo
.rootStart
);
458 DPRINT("DataStart: %d\n", DeviceExt
->FatInfo
.dataStart
);
459 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
461 DPRINT("RootCluster: %d\n", DeviceExt
->FatInfo
.RootCluster
);
464 switch (DeviceExt
->FatInfo
.FatType
)
467 DeviceExt
->GetNextCluster
= FAT12GetNextCluster
;
468 DeviceExt
->FindAndMarkAvailableCluster
= FAT12FindAndMarkAvailableCluster
;
469 DeviceExt
->WriteCluster
= FAT12WriteCluster
;
470 DeviceExt
->CleanShutBitMask
= 0;
475 DeviceExt
->GetNextCluster
= FAT16GetNextCluster
;
476 DeviceExt
->FindAndMarkAvailableCluster
= FAT16FindAndMarkAvailableCluster
;
477 DeviceExt
->WriteCluster
= FAT16WriteCluster
;
478 DeviceExt
->CleanShutBitMask
= 0x8000;
483 DeviceExt
->GetNextCluster
= FAT32GetNextCluster
;
484 DeviceExt
->FindAndMarkAvailableCluster
= FAT32FindAndMarkAvailableCluster
;
485 DeviceExt
->WriteCluster
= FAT32WriteCluster
;
486 DeviceExt
->CleanShutBitMask
= 0x80000000;
490 if (DeviceExt
->FatInfo
.FatType
== FATX16
491 || DeviceExt
->FatInfo
.FatType
== FATX32
)
493 DeviceExt
->Flags
|= VCB_IS_FATX
;
494 DeviceExt
->GetNextDirEntry
= FATXGetNextDirEntry
;
495 DeviceExt
->BaseDateYear
= 2000;
499 DeviceExt
->GetNextDirEntry
= FATGetNextDirEntry
;
500 DeviceExt
->BaseDateYear
= 1980;
503 DeviceExt
->StorageDevice
= DeviceToMount
;
504 DeviceExt
->StorageDevice
->Vpb
->DeviceObject
= DeviceObject
;
505 DeviceExt
->StorageDevice
->Vpb
->RealDevice
= DeviceExt
->StorageDevice
;
506 DeviceExt
->StorageDevice
->Vpb
->Flags
|= VPB_MOUNTED
;
507 DeviceObject
->StackSize
= DeviceExt
->StorageDevice
->StackSize
+ 1;
508 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
510 DPRINT("FsDeviceObject %p\n", DeviceObject
);
512 /* Initialize this resource early ... it's used in VfatCleanup */
513 ExInitializeResourceLite(&DeviceExt
->DirResource
);
515 DeviceExt
->FATFileObject
= IoCreateStreamFileObject(NULL
, DeviceExt
->StorageDevice
);
516 Fcb
= vfatNewFCB(DeviceExt
, &NameU
);
519 Status
= STATUS_INSUFFICIENT_RESOURCES
;
522 Ccb
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
525 Status
= STATUS_INSUFFICIENT_RESOURCES
;
529 RtlZeroMemory(Ccb
, sizeof (VFATCCB
));
530 DeviceExt
->FATFileObject
->FsContext
= Fcb
;
531 DeviceExt
->FATFileObject
->FsContext2
= Ccb
;
532 DeviceExt
->FATFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
533 DeviceExt
->FATFileObject
->PrivateCacheMap
= NULL
;
534 DeviceExt
->FATFileObject
->Vpb
= DeviceObject
->Vpb
;
535 Fcb
->FileObject
= DeviceExt
->FATFileObject
;
537 Fcb
->Flags
|= FCB_IS_FAT
;
539 Fcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.FATSectors
* DeviceExt
->FatInfo
.BytesPerSector
;
540 Fcb
->RFCB
.ValidDataLength
= Fcb
->RFCB
.FileSize
;
541 Fcb
->RFCB
.AllocationSize
= Fcb
->RFCB
.FileSize
;
543 CcInitializeCacheMap(DeviceExt
->FATFileObject
,
544 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
546 &VfatGlobalData
->CacheMgrCallbacks
,
549 DeviceExt
->LastAvailableCluster
= 2;
550 ExInitializeResourceLite(&DeviceExt
->FatResource
);
552 InitializeListHead(&DeviceExt
->FcbListHead
);
554 VolumeFcb
= vfatNewFCB(DeviceExt
, &VolumeNameU
);
555 if (VolumeFcb
== NULL
)
557 Status
= STATUS_INSUFFICIENT_RESOURCES
;
560 VolumeFcb
->Flags
= FCB_IS_VOLUME
;
561 VolumeFcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.Sectors
* DeviceExt
->FatInfo
.BytesPerSector
;
562 VolumeFcb
->RFCB
.ValidDataLength
= VolumeFcb
->RFCB
.FileSize
;
563 VolumeFcb
->RFCB
.AllocationSize
= VolumeFcb
->RFCB
.FileSize
;
564 DeviceExt
->VolumeFcb
= VolumeFcb
;
566 ExAcquireResourceExclusiveLite(&VfatGlobalData
->VolumeListLock
, TRUE
);
567 InsertHeadList(&VfatGlobalData
->VolumeListHead
, &DeviceExt
->VolumeListEntry
);
568 ExReleaseResourceLite(&VfatGlobalData
->VolumeListLock
);
570 /* read serial number */
571 DeviceObject
->Vpb
->SerialNumber
= DeviceExt
->FatInfo
.VolumeID
;
573 /* read volume label */
574 ReadVolumeLabel(DeviceExt
, DeviceObject
->Vpb
);
576 /* read clean shutdown bit status */
577 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
578 if (NT_SUCCESS(Status
))
580 if (eocMark
& DeviceExt
->CleanShutBitMask
)
582 /* unset clean shutdown bit */
583 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
584 WriteCluster(DeviceExt
, 1, eocMark
);
585 VolumeFcb
->Flags
|= VCB_CLEAR_DIRTY
;
588 VolumeFcb
->Flags
|= VCB_IS_DIRTY
;
590 Status
= STATUS_SUCCESS
;
593 if (!NT_SUCCESS(Status
))
596 if (DeviceExt
&& DeviceExt
->FATFileObject
)
597 ObDereferenceObject (DeviceExt
->FATFileObject
);
603 IoDeleteDevice(DeviceObject
);
605 vfatDestroyFCB(VolumeFcb
);
612 VfatVerify (PVFAT_IRP_CONTEXT IrpContext
)
614 * FUNCTION: Verify the filesystem
617 PDEVICE_OBJECT DeviceToVerify
;
618 NTSTATUS Status
= STATUS_SUCCESS
;
620 BOOLEAN RecognizedFS
;
621 PDEVICE_EXTENSION DeviceExt
= IrpContext
->DeviceExt
;
623 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext
);
625 DeviceToVerify
= IrpContext
->Stack
->Parameters
.VerifyVolume
.DeviceObject
;
626 Status
= VfatBlockDeviceIoControl(DeviceToVerify
,
627 IOCTL_DISK_CHECK_VERIFY
,
633 DeviceToVerify
->Flags
&= ~DO_VERIFY_VOLUME
;
634 if (!NT_SUCCESS(Status
) && Status
!= STATUS_VERIFY_REQUIRED
)
636 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status
);
637 Status
= STATUS_WRONG_VOLUME
;
641 Status
= VfatHasFileSystem(DeviceToVerify
, &RecognizedFS
, &FatInfo
);
642 if (!NT_SUCCESS(Status
) || RecognizedFS
== FALSE
)
644 Status
= STATUS_WRONG_VOLUME
;
646 else if (sizeof(FATINFO
) == RtlCompareMemory(&FatInfo
, &DeviceExt
->FatInfo
, sizeof(FATINFO
)))
650 * Preformated floppy disks have very often a serial number of 0000:0000.
651 * We should calculate a crc sum over the sectors from the root directory as secondary volume number.
652 * Each write to the root directory must update this crc sum.
658 Status
= STATUS_WRONG_VOLUME
;
667 VfatGetVolumeBitmap(PVFAT_IRP_CONTEXT IrpContext
)
669 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext
);
671 return STATUS_INVALID_DEVICE_REQUEST
;
676 VfatGetRetrievalPointers(PVFAT_IRP_CONTEXT IrpContext
)
678 PIO_STACK_LOCATION Stack
;
680 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers
;
681 PFILE_OBJECT FileObject
;
682 ULONG MaxExtentCount
;
684 PDEVICE_EXTENSION DeviceExt
;
686 ULONG CurrentCluster
;
690 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext
);
692 DeviceExt
= IrpContext
->DeviceExt
;
693 FileObject
= IrpContext
->FileObject
;
694 Stack
= IrpContext
->Stack
;
695 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(STARTING_VCN_INPUT_BUFFER
) ||
696 Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
== NULL
)
698 return STATUS_INVALID_PARAMETER
;
700 if (IrpContext
->Irp
->UserBuffer
== NULL
||
701 Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(RETRIEVAL_POINTERS_BUFFER
))
703 return STATUS_BUFFER_TOO_SMALL
;
706 Fcb
= FileObject
->FsContext
;
708 ExAcquireResourceSharedLite(&Fcb
->MainResource
, TRUE
);
710 Vcn
= ((PSTARTING_VCN_INPUT_BUFFER
)Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
)->StartingVcn
;
711 RetrievalPointers
= IrpContext
->Irp
->UserBuffer
;
713 MaxExtentCount
= ((Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
- sizeof(RetrievalPointers
->ExtentCount
) - sizeof(RetrievalPointers
->StartingVcn
)) / sizeof(RetrievalPointers
->Extents
[0]));
716 if (Vcn
.QuadPart
>= Fcb
->RFCB
.AllocationSize
.QuadPart
/ DeviceExt
->FatInfo
.BytesPerCluster
)
718 Status
= STATUS_INVALID_PARAMETER
;
722 CurrentCluster
= FirstCluster
= vfatDirEntryGetFirstCluster(DeviceExt
, &Fcb
->entry
);
723 Status
= OffsetToCluster(DeviceExt
, FirstCluster
,
724 Vcn
.u
.LowPart
* DeviceExt
->FatInfo
.BytesPerCluster
,
725 &CurrentCluster
, FALSE
);
726 if (!NT_SUCCESS(Status
))
731 RetrievalPointers
->StartingVcn
= Vcn
;
732 RetrievalPointers
->ExtentCount
= 0;
733 RetrievalPointers
->Extents
[0].Lcn
.u
.HighPart
= 0;
734 RetrievalPointers
->Extents
[0].Lcn
.u
.LowPart
= CurrentCluster
- 2;
736 while (CurrentCluster
!= 0xffffffff && RetrievalPointers
->ExtentCount
< MaxExtentCount
)
739 LastCluster
= CurrentCluster
;
740 Status
= NextCluster(DeviceExt
, CurrentCluster
, &CurrentCluster
, FALSE
);
742 if (!NT_SUCCESS(Status
))
747 if (LastCluster
+ 1 != CurrentCluster
)
749 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].NextVcn
= Vcn
;
750 RetrievalPointers
->ExtentCount
++;
751 if (RetrievalPointers
->ExtentCount
< MaxExtentCount
)
753 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.HighPart
= 0;
754 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.LowPart
= CurrentCluster
- 2;
759 IrpContext
->Irp
->IoStatus
.Information
= sizeof(RETRIEVAL_POINTERS_BUFFER
) + (sizeof(RetrievalPointers
->Extents
[0]) * (RetrievalPointers
->ExtentCount
- 1));
760 Status
= STATUS_SUCCESS
;
763 ExReleaseResourceLite(&Fcb
->MainResource
);
769 VfatMoveFile(PVFAT_IRP_CONTEXT IrpContext
)
771 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext
);
773 return STATUS_INVALID_DEVICE_REQUEST
;
777 VfatIsVolumeDirty(PVFAT_IRP_CONTEXT IrpContext
)
781 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext
);
783 if (IrpContext
->Stack
->Parameters
.FileSystemControl
.OutputBufferLength
!= sizeof(ULONG
))
784 return STATUS_INVALID_BUFFER_SIZE
;
785 else if (!IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
)
786 return STATUS_INVALID_USER_BUFFER
;
788 Flags
= (PULONG
)IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
;
791 if (IrpContext
->DeviceExt
->VolumeFcb
->Flags
& VCB_IS_DIRTY
792 && !(IrpContext
->DeviceExt
->VolumeFcb
->Flags
& VCB_CLEAR_DIRTY
))
794 *Flags
|= VOLUME_IS_DIRTY
;
797 return STATUS_SUCCESS
;
801 VfatMarkVolumeDirty(PVFAT_IRP_CONTEXT IrpContext
)
804 PDEVICE_EXTENSION DeviceExt
;
805 NTSTATUS Status
= STATUS_SUCCESS
;
807 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext
);
808 DeviceExt
= IrpContext
->DeviceExt
;
810 if (!(DeviceExt
->VolumeFcb
->Flags
& VCB_IS_DIRTY
))
812 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
813 if (NT_SUCCESS(Status
))
815 /* unset clean shutdown bit */
816 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
817 Status
= WriteCluster(DeviceExt
, 1, eocMark
);
821 DeviceExt
->VolumeFcb
->Flags
&= ~VCB_CLEAR_DIRTY
;
826 NTSTATUS
VfatFileSystemControl(PVFAT_IRP_CONTEXT IrpContext
)
828 * FUNCTION: File system control
834 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext
);
837 ASSERT(IrpContext
->Irp
);
838 ASSERT(IrpContext
->Stack
);
840 IrpContext
->Irp
->IoStatus
.Information
= 0;
842 switch (IrpContext
->MinorFunction
)
844 case IRP_MN_USER_FS_REQUEST
:
845 switch(IrpContext
->Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
847 case FSCTL_GET_VOLUME_BITMAP
:
848 Status
= VfatGetVolumeBitmap(IrpContext
);
850 case FSCTL_GET_RETRIEVAL_POINTERS
:
851 Status
= VfatGetRetrievalPointers(IrpContext
);
853 case FSCTL_MOVE_FILE
:
854 Status
= VfatMoveFile(IrpContext
);
856 case FSCTL_IS_VOLUME_DIRTY
:
857 Status
= VfatIsVolumeDirty(IrpContext
);
859 case FSCTL_MARK_VOLUME_DIRTY
:
860 Status
= VfatMarkVolumeDirty(IrpContext
);
863 Status
= STATUS_INVALID_DEVICE_REQUEST
;
867 case IRP_MN_MOUNT_VOLUME
:
868 Status
= VfatMount(IrpContext
);
871 case IRP_MN_VERIFY_VOLUME
:
872 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
873 Status
= VfatVerify(IrpContext
);
877 DPRINT("VFAT FSC: MinorFunction %d\n", IrpContext
->MinorFunction
);
878 Status
= STATUS_INVALID_DEVICE_REQUEST
;
882 IrpContext
->Irp
->IoStatus
.Status
= Status
;
884 IoCompleteRequest (IrpContext
->Irp
, IO_NO_INCREMENT
);
885 VfatFreeIrpContext(IrpContext
);