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 *****************************************************************/
31 /* FUNCTIONS ****************************************************************/
33 #define CACHEPAGESIZE(pDeviceExt) ((pDeviceExt)->FatInfo.BytesPerCluster > PAGE_SIZE ? \
34 (pDeviceExt)->FatInfo.BytesPerCluster : PAGE_SIZE)
39 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
);
72 FatInfo
.FixedMedia
= DiskGeometry
.MediaType
== FixedMedia
? TRUE
: FALSE
;
73 if (DiskGeometry
.MediaType
== FixedMedia
|| DiskGeometry
.MediaType
== RemovableMedia
)
75 // We have found a hard disk
76 Size
= sizeof(PARTITION_INFORMATION
);
77 Status
= VfatBlockDeviceIoControl(DeviceToMount
,
78 IOCTL_DISK_GET_PARTITION_INFO
,
84 if (!NT_SUCCESS(Status
))
86 DPRINT("VfatBlockDeviceIoControl faild (%x)\n", Status
);
90 PartitionInfoIsValid
= TRUE
;
91 DPRINT("Partition Information:\n");
92 DPRINT("StartingOffset %I64x\n", PartitionInfo
.StartingOffset
.QuadPart
/ 512);
93 DPRINT("PartitionLength %I64x\n", PartitionInfo
.PartitionLength
.QuadPart
/ 512);
94 DPRINT("HiddenSectors %u\n", PartitionInfo
.HiddenSectors
);
95 DPRINT("PartitionNumber %u\n", PartitionInfo
.PartitionNumber
);
96 DPRINT("PartitionType %u\n", PartitionInfo
.PartitionType
);
97 DPRINT("BootIndicator %u\n", PartitionInfo
.BootIndicator
);
98 DPRINT("RecognizedPartition %u\n", PartitionInfo
.RecognizedPartition
);
99 DPRINT("RewritePartition %u\n", PartitionInfo
.RewritePartition
);
100 if (PartitionInfo
.PartitionType
)
102 if (PartitionInfo
.PartitionType
== PARTITION_FAT_12
||
103 PartitionInfo
.PartitionType
== PARTITION_FAT_16
||
104 PartitionInfo
.PartitionType
== PARTITION_HUGE
||
105 PartitionInfo
.PartitionType
== PARTITION_FAT32
||
106 PartitionInfo
.PartitionType
== PARTITION_FAT32_XINT13
||
107 PartitionInfo
.PartitionType
== PARTITION_XINT13
)
109 *RecognizedFS
= TRUE
;
112 else if (DiskGeometry
.MediaType
== RemovableMedia
&&
113 PartitionInfo
.PartitionNumber
> 0 &&
114 PartitionInfo
.StartingOffset
.QuadPart
== 0 &&
115 PartitionInfo
.PartitionLength
.QuadPart
> 0)
117 /* This is possible a removable media formated as super floppy */
118 *RecognizedFS
= TRUE
;
121 else if (DiskGeometry
.MediaType
== Unknown
)
124 * Floppy disk driver can return Unknown as media type if it
125 * doesn't know yet what floppy in the drive really is. This is
126 * perfectly correct to do under Windows.
128 *RecognizedFS
= TRUE
;
129 DiskGeometry
.BytesPerSector
= 512;
133 *RecognizedFS
= TRUE
;
138 Boot
= ExAllocatePoolWithTag(NonPagedPool
, DiskGeometry
.BytesPerSector
, TAG_VFAT
);
141 return STATUS_INSUFFICIENT_RESOURCES
;
146 /* Try to recognize FAT12/FAT16/FAT32 partitions */
147 Status
= VfatReadDisk(DeviceToMount
, &Offset
, DiskGeometry
.BytesPerSector
, (PUCHAR
) Boot
, FALSE
);
148 if (NT_SUCCESS(Status
))
150 if (Boot
->Signatur1
!= 0xaa55)
152 *RecognizedFS
= FALSE
;
156 Boot
->BytesPerSector
!= 512 &&
157 Boot
->BytesPerSector
!= 1024 &&
158 Boot
->BytesPerSector
!= 2048 &&
159 Boot
->BytesPerSector
!= 4096)
161 DPRINT1("BytesPerSector %u\n", Boot
->BytesPerSector
);
162 *RecognizedFS
= FALSE
;
166 Boot
->FATCount
!= 1 &&
169 DPRINT1("FATCount %u\n", Boot
->FATCount
);
170 *RecognizedFS
= FALSE
;
174 Boot
->Media
!= 0xf0 &&
175 Boot
->Media
!= 0xf8 &&
176 Boot
->Media
!= 0xf9 &&
177 Boot
->Media
!= 0xfa &&
178 Boot
->Media
!= 0xfb &&
179 Boot
->Media
!= 0xfc &&
180 Boot
->Media
!= 0xfd &&
181 Boot
->Media
!= 0xfe &&
184 DPRINT1("Media %02x\n", Boot
->Media
);
185 *RecognizedFS
= FALSE
;
189 Boot
->SectorsPerCluster
!= 1 &&
190 Boot
->SectorsPerCluster
!= 2 &&
191 Boot
->SectorsPerCluster
!= 4 &&
192 Boot
->SectorsPerCluster
!= 8 &&
193 Boot
->SectorsPerCluster
!= 16 &&
194 Boot
->SectorsPerCluster
!= 32 &&
195 Boot
->SectorsPerCluster
!= 64 &&
196 Boot
->SectorsPerCluster
!= 128)
198 DPRINT1("SectorsPerCluster %02x\n", Boot
->SectorsPerCluster
);
199 *RecognizedFS
= FALSE
;
203 Boot
->BytesPerSector
* Boot
->SectorsPerCluster
> 32 * 1024)
205 DPRINT1("ClusterSize %dx\n", Boot
->BytesPerSector
* Boot
->SectorsPerCluster
);
206 *RecognizedFS
= FALSE
;
211 FatInfo
.VolumeID
= Boot
->VolumeID
;
212 FatInfo
.FATStart
= Boot
->ReservedSectors
;
213 FatInfo
.FATCount
= Boot
->FATCount
;
214 FatInfo
.FATSectors
= Boot
->FATSectors
? Boot
->FATSectors
: ((struct _BootSector32
*) Boot
)->FATSectors32
;
215 FatInfo
.BytesPerSector
= Boot
->BytesPerSector
;
216 FatInfo
.SectorsPerCluster
= Boot
->SectorsPerCluster
;
217 FatInfo
.BytesPerCluster
= FatInfo
.BytesPerSector
* FatInfo
.SectorsPerCluster
;
218 FatInfo
.rootDirectorySectors
= ((Boot
->RootEntries
* 32) + Boot
->BytesPerSector
- 1) / Boot
->BytesPerSector
;
219 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
220 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
221 FatInfo
.Sectors
= Sectors
= Boot
->Sectors
? Boot
->Sectors
: Boot
->SectorsHuge
;
222 Sectors
-= Boot
->ReservedSectors
+ FatInfo
.FATCount
* FatInfo
.FATSectors
+ FatInfo
.rootDirectorySectors
;
223 FatInfo
.NumberOfClusters
= Sectors
/ Boot
->SectorsPerCluster
;
224 if (FatInfo
.NumberOfClusters
< 4085)
227 FatInfo
.FatType
= FAT12
;
228 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
230 else if (FatInfo
.NumberOfClusters
>= 65525)
233 FatInfo
.FatType
= FAT32
;
234 FatInfo
.RootCluster
= ((struct _BootSector32
*) Boot
)->RootCluster
;
235 FatInfo
.rootStart
= FatInfo
.dataStart
+ ((FatInfo
.RootCluster
- 2) * FatInfo
.SectorsPerCluster
);
236 FatInfo
.VolumeID
= ((struct _BootSector32
*) Boot
)->VolumeID
;
241 FatInfo
.FatType
= FAT16
;
242 FatInfo
.RootCluster
= FatInfo
.rootStart
/ FatInfo
.SectorsPerCluster
;
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
, FALSE
);
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 VfatMountDevice(PDEVICE_EXTENSION DeviceExt
,
343 PDEVICE_OBJECT DeviceToMount
)
345 * FUNCTION: Mounts the device
349 BOOLEAN RecognizedFS
;
351 DPRINT("Mounting VFAT device...\n");
353 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &DeviceExt
->FatInfo
);
354 if (!NT_SUCCESS(Status
))
358 DPRINT("MountVfatdev %u, PAGE_SIZE = %d\n", DeviceExt
->FatInfo
.BytesPerCluster
, PAGE_SIZE
);
361 return(STATUS_SUCCESS
);
366 VfatMount (PVFAT_IRP_CONTEXT IrpContext
)
368 * FUNCTION: Mount the filesystem
371 PDEVICE_OBJECT DeviceObject
= NULL
;
372 PDEVICE_EXTENSION DeviceExt
= NULL
;
373 BOOLEAN RecognizedFS
;
376 PVFATFCB VolumeFcb
= NULL
;
378 PDEVICE_OBJECT DeviceToMount
;
380 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\$$Fat$$");
381 UNICODE_STRING VolumeNameU
= RTL_CONSTANT_STRING(L
"\\$$Volume$$");
386 DPRINT("VfatMount(IrpContext %p)\n", IrpContext
);
390 if (IrpContext
->DeviceObject
!= VfatGlobalData
->DeviceObject
)
392 Status
= STATUS_INVALID_DEVICE_REQUEST
;
396 DeviceToMount
= IrpContext
->Stack
->Parameters
.MountVolume
.DeviceObject
;
397 Vpb
= IrpContext
->Stack
->Parameters
.MountVolume
.Vpb
;
399 Status
= VfatHasFileSystem (DeviceToMount
, &RecognizedFS
, &FatInfo
);
400 if (!NT_SUCCESS(Status
))
405 if (RecognizedFS
== FALSE
)
407 DPRINT("VFAT: Unrecognized Volume\n");
408 Status
= STATUS_UNRECOGNIZED_VOLUME
;
412 /* Use prime numbers for the table size */
413 if (FatInfo
.FatType
== FAT12
)
415 HashTableSize
= 4099; // 4096 = 4 * 1024
417 else if (FatInfo
.FatType
== FAT16
||
418 FatInfo
.FatType
== FATX16
)
420 HashTableSize
= 16411; // 16384 = 16 * 1024
424 HashTableSize
= 65537; // 65536 = 64 * 1024;
426 HashTableSize
= FCB_HASH_TABLE_SIZE
;
427 DPRINT("VFAT: Recognized volume\n");
428 Status
= IoCreateDevice(VfatGlobalData
->DriverObject
,
429 ROUND_UP(sizeof (DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
,
431 FILE_DEVICE_DISK_FILE_SYSTEM
,
432 DeviceToMount
->Characteristics
,
435 if (!NT_SUCCESS(Status
))
440 DeviceObject
->Flags
= DeviceObject
->Flags
| DO_DIRECT_IO
;
441 DeviceExt
= (PVOID
) DeviceObject
->DeviceExtension
;
442 RtlZeroMemory(DeviceExt
, ROUND_UP(sizeof(DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
);
443 DeviceExt
->FcbHashTable
= (HASHENTRY
**)((ULONG_PTR
)DeviceExt
+ ROUND_UP(sizeof(DEVICE_EXTENSION
), sizeof(ULONG
)));
444 DeviceExt
->HashTableSize
= HashTableSize
;
446 /* use same vpb as device disk */
447 DeviceObject
->Vpb
= Vpb
;
448 DeviceToMount
->Vpb
= Vpb
;
450 Status
= VfatMountDevice(DeviceExt
, DeviceToMount
);
451 if (!NT_SUCCESS(Status
))
453 /* FIXME: delete device object */
457 DPRINT("BytesPerSector: %u\n", DeviceExt
->FatInfo
.BytesPerSector
);
458 DPRINT("SectorsPerCluster: %u\n", DeviceExt
->FatInfo
.SectorsPerCluster
);
459 DPRINT("FATCount: %u\n", DeviceExt
->FatInfo
.FATCount
);
460 DPRINT("FATSectors: %u\n", DeviceExt
->FatInfo
.FATSectors
);
461 DPRINT("RootStart: %u\n", DeviceExt
->FatInfo
.rootStart
);
462 DPRINT("DataStart: %u\n", DeviceExt
->FatInfo
.dataStart
);
463 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
465 DPRINT("RootCluster: %u\n", DeviceExt
->FatInfo
.RootCluster
);
468 switch (DeviceExt
->FatInfo
.FatType
)
471 DeviceExt
->GetNextCluster
= FAT12GetNextCluster
;
472 DeviceExt
->FindAndMarkAvailableCluster
= FAT12FindAndMarkAvailableCluster
;
473 DeviceExt
->WriteCluster
= FAT12WriteCluster
;
474 DeviceExt
->CleanShutBitMask
= 0;
479 DeviceExt
->GetNextCluster
= FAT16GetNextCluster
;
480 DeviceExt
->FindAndMarkAvailableCluster
= FAT16FindAndMarkAvailableCluster
;
481 DeviceExt
->WriteCluster
= FAT16WriteCluster
;
482 DeviceExt
->CleanShutBitMask
= 0x8000;
487 DeviceExt
->GetNextCluster
= FAT32GetNextCluster
;
488 DeviceExt
->FindAndMarkAvailableCluster
= FAT32FindAndMarkAvailableCluster
;
489 DeviceExt
->WriteCluster
= FAT32WriteCluster
;
490 DeviceExt
->CleanShutBitMask
= 0x80000000;
494 if (DeviceExt
->FatInfo
.FatType
== FATX16
495 || DeviceExt
->FatInfo
.FatType
== FATX32
)
497 DeviceExt
->Flags
|= VCB_IS_FATX
;
498 DeviceExt
->GetNextDirEntry
= FATXGetNextDirEntry
;
499 DeviceExt
->BaseDateYear
= 2000;
503 DeviceExt
->GetNextDirEntry
= FATGetNextDirEntry
;
504 DeviceExt
->BaseDateYear
= 1980;
507 DeviceExt
->StorageDevice
= DeviceToMount
;
508 DeviceExt
->StorageDevice
->Vpb
->DeviceObject
= DeviceObject
;
509 DeviceExt
->StorageDevice
->Vpb
->RealDevice
= DeviceExt
->StorageDevice
;
510 DeviceExt
->StorageDevice
->Vpb
->Flags
|= VPB_MOUNTED
;
511 DeviceObject
->StackSize
= DeviceExt
->StorageDevice
->StackSize
+ 1;
512 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
514 DPRINT("FsDeviceObject %p\n", DeviceObject
);
516 /* Initialize this resource early ... it's used in VfatCleanup */
517 ExInitializeResourceLite(&DeviceExt
->DirResource
);
519 DeviceExt
->FATFileObject
= IoCreateStreamFileObject(NULL
, DeviceExt
->StorageDevice
);
520 Fcb
= vfatNewFCB(DeviceExt
, &NameU
);
523 Status
= STATUS_INSUFFICIENT_RESOURCES
;
526 Ccb
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
529 Status
= STATUS_INSUFFICIENT_RESOURCES
;
533 RtlZeroMemory(Ccb
, sizeof (VFATCCB
));
534 DeviceExt
->FATFileObject
->FsContext
= Fcb
;
535 DeviceExt
->FATFileObject
->FsContext2
= Ccb
;
536 DeviceExt
->FATFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
537 DeviceExt
->FATFileObject
->PrivateCacheMap
= NULL
;
538 DeviceExt
->FATFileObject
->Vpb
= DeviceObject
->Vpb
;
539 Fcb
->FileObject
= DeviceExt
->FATFileObject
;
541 Fcb
->Flags
|= FCB_IS_FAT
;
543 Fcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.FATSectors
* DeviceExt
->FatInfo
.BytesPerSector
;
544 Fcb
->RFCB
.ValidDataLength
= Fcb
->RFCB
.FileSize
;
545 Fcb
->RFCB
.AllocationSize
= Fcb
->RFCB
.FileSize
;
547 CcInitializeCacheMap(DeviceExt
->FATFileObject
,
548 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
550 &VfatGlobalData
->CacheMgrCallbacks
,
553 DeviceExt
->LastAvailableCluster
= 2;
554 ExInitializeResourceLite(&DeviceExt
->FatResource
);
556 InitializeListHead(&DeviceExt
->FcbListHead
);
558 VolumeFcb
= vfatNewFCB(DeviceExt
, &VolumeNameU
);
559 if (VolumeFcb
== NULL
)
561 Status
= STATUS_INSUFFICIENT_RESOURCES
;
564 VolumeFcb
->Flags
= FCB_IS_VOLUME
;
565 VolumeFcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.Sectors
* DeviceExt
->FatInfo
.BytesPerSector
;
566 VolumeFcb
->RFCB
.ValidDataLength
= VolumeFcb
->RFCB
.FileSize
;
567 VolumeFcb
->RFCB
.AllocationSize
= VolumeFcb
->RFCB
.FileSize
;
568 DeviceExt
->VolumeFcb
= VolumeFcb
;
570 ExAcquireResourceExclusiveLite(&VfatGlobalData
->VolumeListLock
, TRUE
);
571 InsertHeadList(&VfatGlobalData
->VolumeListHead
, &DeviceExt
->VolumeListEntry
);
572 ExReleaseResourceLite(&VfatGlobalData
->VolumeListLock
);
574 /* read serial number */
575 DeviceObject
->Vpb
->SerialNumber
= DeviceExt
->FatInfo
.VolumeID
;
577 /* read volume label */
578 ReadVolumeLabel(DeviceExt
, DeviceObject
->Vpb
);
580 /* read clean shutdown bit status */
581 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
582 if (NT_SUCCESS(Status
))
584 if (eocMark
& DeviceExt
->CleanShutBitMask
)
586 /* unset clean shutdown bit */
587 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
588 WriteCluster(DeviceExt
, 1, eocMark
);
589 VolumeFcb
->Flags
|= VCB_CLEAR_DIRTY
;
592 VolumeFcb
->Flags
|= VCB_IS_DIRTY
;
594 FsRtlNotifyVolumeEvent(DeviceExt
->FATFileObject
, FSRTL_VOLUME_MOUNT
);
596 Status
= STATUS_SUCCESS
;
599 if (!NT_SUCCESS(Status
))
602 if (DeviceExt
&& DeviceExt
->FATFileObject
)
603 ObDereferenceObject (DeviceExt
->FATFileObject
);
609 IoDeleteDevice(DeviceObject
);
611 vfatDestroyFCB(VolumeFcb
);
618 VfatVerify (PVFAT_IRP_CONTEXT IrpContext
)
620 * FUNCTION: Verify the filesystem
623 PDEVICE_OBJECT DeviceToVerify
;
624 NTSTATUS Status
= STATUS_SUCCESS
;
626 BOOLEAN RecognizedFS
;
627 PDEVICE_EXTENSION DeviceExt
= IrpContext
->DeviceExt
;
629 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext
);
631 DeviceToVerify
= IrpContext
->Stack
->Parameters
.VerifyVolume
.DeviceObject
;
632 Status
= VfatBlockDeviceIoControl(DeviceToVerify
,
633 IOCTL_DISK_CHECK_VERIFY
,
639 DeviceToVerify
->Flags
&= ~DO_VERIFY_VOLUME
;
640 if (!NT_SUCCESS(Status
) && Status
!= STATUS_VERIFY_REQUIRED
)
642 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status
);
643 Status
= STATUS_WRONG_VOLUME
;
647 Status
= VfatHasFileSystem(DeviceToVerify
, &RecognizedFS
, &FatInfo
);
648 if (!NT_SUCCESS(Status
) || RecognizedFS
== FALSE
)
650 Status
= STATUS_WRONG_VOLUME
;
652 else if (sizeof(FATINFO
) == RtlCompareMemory(&FatInfo
, &DeviceExt
->FatInfo
, sizeof(FATINFO
)))
656 * Preformated floppy disks have very often a serial number of 0000:0000.
657 * We should calculate a crc sum over the sectors from the root directory as secondary volume number.
658 * Each write to the root directory must update this crc sum.
664 Status
= STATUS_WRONG_VOLUME
;
673 VfatGetVolumeBitmap(PVFAT_IRP_CONTEXT IrpContext
)
675 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext
);
677 return STATUS_INVALID_DEVICE_REQUEST
;
682 VfatGetRetrievalPointers(PVFAT_IRP_CONTEXT IrpContext
)
684 PIO_STACK_LOCATION Stack
;
686 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers
;
687 PFILE_OBJECT FileObject
;
688 ULONG MaxExtentCount
;
690 PDEVICE_EXTENSION DeviceExt
;
692 ULONG CurrentCluster
;
696 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext
);
698 DeviceExt
= IrpContext
->DeviceExt
;
699 FileObject
= IrpContext
->FileObject
;
700 Stack
= IrpContext
->Stack
;
701 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(STARTING_VCN_INPUT_BUFFER
) ||
702 Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
== NULL
)
704 return STATUS_INVALID_PARAMETER
;
706 if (IrpContext
->Irp
->UserBuffer
== NULL
||
707 Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(RETRIEVAL_POINTERS_BUFFER
))
709 return STATUS_BUFFER_TOO_SMALL
;
712 Fcb
= FileObject
->FsContext
;
714 ExAcquireResourceSharedLite(&Fcb
->MainResource
, TRUE
);
716 Vcn
= ((PSTARTING_VCN_INPUT_BUFFER
)Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
)->StartingVcn
;
717 RetrievalPointers
= IrpContext
->Irp
->UserBuffer
;
719 MaxExtentCount
= ((Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
- sizeof(RetrievalPointers
->ExtentCount
) - sizeof(RetrievalPointers
->StartingVcn
)) / sizeof(RetrievalPointers
->Extents
[0]));
722 if (Vcn
.QuadPart
>= Fcb
->RFCB
.AllocationSize
.QuadPart
/ DeviceExt
->FatInfo
.BytesPerCluster
)
724 Status
= STATUS_INVALID_PARAMETER
;
728 CurrentCluster
= FirstCluster
= vfatDirEntryGetFirstCluster(DeviceExt
, &Fcb
->entry
);
729 Status
= OffsetToCluster(DeviceExt
, FirstCluster
,
730 Vcn
.u
.LowPart
* DeviceExt
->FatInfo
.BytesPerCluster
,
731 &CurrentCluster
, FALSE
);
732 if (!NT_SUCCESS(Status
))
737 RetrievalPointers
->StartingVcn
= Vcn
;
738 RetrievalPointers
->ExtentCount
= 0;
739 RetrievalPointers
->Extents
[0].Lcn
.u
.HighPart
= 0;
740 RetrievalPointers
->Extents
[0].Lcn
.u
.LowPart
= CurrentCluster
- 2;
742 while (CurrentCluster
!= 0xffffffff && RetrievalPointers
->ExtentCount
< MaxExtentCount
)
745 LastCluster
= CurrentCluster
;
746 Status
= NextCluster(DeviceExt
, CurrentCluster
, &CurrentCluster
, FALSE
);
748 if (!NT_SUCCESS(Status
))
753 if (LastCluster
+ 1 != CurrentCluster
)
755 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].NextVcn
= Vcn
;
756 RetrievalPointers
->ExtentCount
++;
757 if (RetrievalPointers
->ExtentCount
< MaxExtentCount
)
759 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.HighPart
= 0;
760 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.LowPart
= CurrentCluster
- 2;
765 IrpContext
->Irp
->IoStatus
.Information
= sizeof(RETRIEVAL_POINTERS_BUFFER
) + (sizeof(RetrievalPointers
->Extents
[0]) * (RetrievalPointers
->ExtentCount
- 1));
766 Status
= STATUS_SUCCESS
;
769 ExReleaseResourceLite(&Fcb
->MainResource
);
775 VfatMoveFile(PVFAT_IRP_CONTEXT IrpContext
)
777 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext
);
779 return STATUS_INVALID_DEVICE_REQUEST
;
783 VfatIsVolumeDirty(PVFAT_IRP_CONTEXT IrpContext
)
787 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext
);
789 if (IrpContext
->Stack
->Parameters
.FileSystemControl
.OutputBufferLength
!= sizeof(ULONG
))
790 return STATUS_INVALID_BUFFER_SIZE
;
791 else if (!IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
)
792 return STATUS_INVALID_USER_BUFFER
;
794 Flags
= (PULONG
)IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
;
797 if (IrpContext
->DeviceExt
->VolumeFcb
->Flags
& VCB_IS_DIRTY
798 && !(IrpContext
->DeviceExt
->VolumeFcb
->Flags
& VCB_CLEAR_DIRTY
))
800 *Flags
|= VOLUME_IS_DIRTY
;
803 return STATUS_SUCCESS
;
807 VfatMarkVolumeDirty(PVFAT_IRP_CONTEXT IrpContext
)
810 PDEVICE_EXTENSION DeviceExt
;
811 NTSTATUS Status
= STATUS_SUCCESS
;
813 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext
);
814 DeviceExt
= IrpContext
->DeviceExt
;
816 if (!(DeviceExt
->VolumeFcb
->Flags
& VCB_IS_DIRTY
))
818 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
819 if (NT_SUCCESS(Status
))
821 /* unset clean shutdown bit */
822 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
823 Status
= WriteCluster(DeviceExt
, 1, eocMark
);
827 DeviceExt
->VolumeFcb
->Flags
&= ~VCB_CLEAR_DIRTY
;
832 NTSTATUS
VfatFileSystemControl(PVFAT_IRP_CONTEXT IrpContext
)
834 * FUNCTION: File system control
840 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext
);
843 ASSERT(IrpContext
->Irp
);
844 ASSERT(IrpContext
->Stack
);
846 IrpContext
->Irp
->IoStatus
.Information
= 0;
848 switch (IrpContext
->MinorFunction
)
850 case IRP_MN_KERNEL_CALL
:
851 case IRP_MN_USER_FS_REQUEST
:
852 switch(IrpContext
->Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
854 case FSCTL_GET_VOLUME_BITMAP
:
855 Status
= VfatGetVolumeBitmap(IrpContext
);
857 case FSCTL_GET_RETRIEVAL_POINTERS
:
858 Status
= VfatGetRetrievalPointers(IrpContext
);
860 case FSCTL_MOVE_FILE
:
861 Status
= VfatMoveFile(IrpContext
);
863 case FSCTL_IS_VOLUME_DIRTY
:
864 Status
= VfatIsVolumeDirty(IrpContext
);
866 case FSCTL_MARK_VOLUME_DIRTY
:
867 Status
= VfatMarkVolumeDirty(IrpContext
);
870 Status
= STATUS_INVALID_DEVICE_REQUEST
;
874 case IRP_MN_MOUNT_VOLUME
:
875 Status
= VfatMount(IrpContext
);
878 case IRP_MN_VERIFY_VOLUME
:
879 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
880 Status
= VfatVerify(IrpContext
);
884 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext
->MinorFunction
);
885 Status
= STATUS_INVALID_DEVICE_REQUEST
;
889 IrpContext
->Irp
->IoStatus
.Status
= Status
;
891 IoCompleteRequest (IrpContext
->Irp
, IO_NO_INCREMENT
);
892 VfatFreeIrpContext(IrpContext
);