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)
37 VfatHasFileSystem(PDEVICE_OBJECT DeviceToMount
,
38 PBOOLEAN RecognizedFS
,
42 PARTITION_INFORMATION PartitionInfo
;
43 DISK_GEOMETRY DiskGeometry
;
48 struct _BootSector
* Boot
;
49 struct _BootSectorFatX
* BootFatX
;
50 BOOLEAN PartitionInfoIsValid
= FALSE
;
52 DPRINT("VfatHasFileSystem\n");
54 *RecognizedFS
= FALSE
;
56 Size
= sizeof(DISK_GEOMETRY
);
57 Status
= VfatBlockDeviceIoControl(DeviceToMount
,
58 IOCTL_DISK_GET_DRIVE_GEOMETRY
,
64 if (!NT_SUCCESS(Status
))
66 DPRINT("VfatBlockDeviceIoControl faild (%x)\n", Status
);
69 FatInfo
.FixedMedia
= DiskGeometry
.MediaType
== FixedMedia
? TRUE
: FALSE
;
70 if (DiskGeometry
.MediaType
== FixedMedia
|| DiskGeometry
.MediaType
== RemovableMedia
)
72 // We have found a hard disk
73 Size
= sizeof(PARTITION_INFORMATION
);
74 Status
= VfatBlockDeviceIoControl(DeviceToMount
,
75 IOCTL_DISK_GET_PARTITION_INFO
,
81 if (!NT_SUCCESS(Status
))
83 DPRINT("VfatBlockDeviceIoControl faild (%x)\n", Status
);
86 PartitionInfoIsValid
= TRUE
;
87 DPRINT("Partition Information:\n");
88 DPRINT("StartingOffset %I64x\n", PartitionInfo
.StartingOffset
.QuadPart
/ 512);
89 DPRINT("PartitionLength %I64x\n", PartitionInfo
.PartitionLength
.QuadPart
/ 512);
90 DPRINT("HiddenSectors %u\n", PartitionInfo
.HiddenSectors
);
91 DPRINT("PartitionNumber %u\n", PartitionInfo
.PartitionNumber
);
92 DPRINT("PartitionType %u\n", PartitionInfo
.PartitionType
);
93 DPRINT("BootIndicator %u\n", PartitionInfo
.BootIndicator
);
94 DPRINT("RecognizedPartition %u\n", PartitionInfo
.RecognizedPartition
);
95 DPRINT("RewritePartition %u\n", PartitionInfo
.RewritePartition
);
96 if (PartitionInfo
.PartitionType
)
98 if (PartitionInfo
.PartitionType
== PARTITION_FAT_12
||
99 PartitionInfo
.PartitionType
== PARTITION_FAT_16
||
100 PartitionInfo
.PartitionType
== PARTITION_HUGE
||
101 PartitionInfo
.PartitionType
== PARTITION_FAT32
||
102 PartitionInfo
.PartitionType
== PARTITION_FAT32_XINT13
||
103 PartitionInfo
.PartitionType
== PARTITION_XINT13
)
105 *RecognizedFS
= TRUE
;
108 else if (DiskGeometry
.MediaType
== RemovableMedia
&&
109 PartitionInfo
.PartitionNumber
> 0 &&
110 PartitionInfo
.StartingOffset
.QuadPart
== 0 &&
111 PartitionInfo
.PartitionLength
.QuadPart
> 0)
113 /* This is possible a removable media formated as super floppy */
114 *RecognizedFS
= TRUE
;
117 else if (DiskGeometry
.MediaType
== Unknown
)
120 * Floppy disk driver can return Unknown as media type if it
121 * doesn't know yet what floppy in the drive really is. This is
122 * perfectly correct to do under Windows.
124 *RecognizedFS
= TRUE
;
125 DiskGeometry
.BytesPerSector
= 512;
129 *RecognizedFS
= TRUE
;
134 Boot
= ExAllocatePoolWithTag(NonPagedPool
, DiskGeometry
.BytesPerSector
, TAG_VFAT
);
137 return STATUS_INSUFFICIENT_RESOURCES
;
142 /* Try to recognize FAT12/FAT16/FAT32 partitions */
143 Status
= VfatReadDisk(DeviceToMount
, &Offset
, DiskGeometry
.BytesPerSector
, (PUCHAR
) Boot
, FALSE
);
144 if (NT_SUCCESS(Status
))
146 if (Boot
->Signatur1
!= 0xaa55)
148 *RecognizedFS
= FALSE
;
151 Boot
->BytesPerSector
!= 512 &&
152 Boot
->BytesPerSector
!= 1024 &&
153 Boot
->BytesPerSector
!= 2048 &&
154 Boot
->BytesPerSector
!= 4096)
156 DPRINT1("BytesPerSector %u\n", Boot
->BytesPerSector
);
157 *RecognizedFS
= FALSE
;
161 Boot
->FATCount
!= 1 &&
164 DPRINT1("FATCount %u\n", Boot
->FATCount
);
165 *RecognizedFS
= FALSE
;
169 Boot
->Media
!= 0xf0 &&
170 Boot
->Media
!= 0xf8 &&
171 Boot
->Media
!= 0xf9 &&
172 Boot
->Media
!= 0xfa &&
173 Boot
->Media
!= 0xfb &&
174 Boot
->Media
!= 0xfc &&
175 Boot
->Media
!= 0xfd &&
176 Boot
->Media
!= 0xfe &&
179 DPRINT1("Media %02x\n", Boot
->Media
);
180 *RecognizedFS
= FALSE
;
184 Boot
->SectorsPerCluster
!= 1 &&
185 Boot
->SectorsPerCluster
!= 2 &&
186 Boot
->SectorsPerCluster
!= 4 &&
187 Boot
->SectorsPerCluster
!= 8 &&
188 Boot
->SectorsPerCluster
!= 16 &&
189 Boot
->SectorsPerCluster
!= 32 &&
190 Boot
->SectorsPerCluster
!= 64 &&
191 Boot
->SectorsPerCluster
!= 128)
193 DPRINT1("SectorsPerCluster %02x\n", Boot
->SectorsPerCluster
);
194 *RecognizedFS
= FALSE
;
198 Boot
->BytesPerSector
* Boot
->SectorsPerCluster
> 32 * 1024)
200 DPRINT1("ClusterSize %dx\n", Boot
->BytesPerSector
* Boot
->SectorsPerCluster
);
201 *RecognizedFS
= FALSE
;
206 FatInfo
.VolumeID
= Boot
->VolumeID
;
207 FatInfo
.FATStart
= Boot
->ReservedSectors
;
208 FatInfo
.FATCount
= Boot
->FATCount
;
209 FatInfo
.FATSectors
= Boot
->FATSectors
? Boot
->FATSectors
: ((struct _BootSector32
*) Boot
)->FATSectors32
;
210 FatInfo
.BytesPerSector
= Boot
->BytesPerSector
;
211 FatInfo
.SectorsPerCluster
= Boot
->SectorsPerCluster
;
212 FatInfo
.BytesPerCluster
= FatInfo
.BytesPerSector
* FatInfo
.SectorsPerCluster
;
213 FatInfo
.rootDirectorySectors
= ((Boot
->RootEntries
* 32) + Boot
->BytesPerSector
- 1) / Boot
->BytesPerSector
;
214 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
215 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
216 FatInfo
.Sectors
= Sectors
= Boot
->Sectors
? Boot
->Sectors
: Boot
->SectorsHuge
;
217 Sectors
-= Boot
->ReservedSectors
+ FatInfo
.FATCount
* FatInfo
.FATSectors
+ FatInfo
.rootDirectorySectors
;
218 FatInfo
.NumberOfClusters
= Sectors
/ Boot
->SectorsPerCluster
;
219 if (FatInfo
.NumberOfClusters
< 4085)
222 FatInfo
.FatType
= FAT12
;
223 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
225 else if (FatInfo
.NumberOfClusters
>= 65525)
228 FatInfo
.FatType
= FAT32
;
229 FatInfo
.RootCluster
= ((struct _BootSector32
*) Boot
)->RootCluster
;
230 FatInfo
.rootStart
= FatInfo
.dataStart
+ ((FatInfo
.RootCluster
- 2) * FatInfo
.SectorsPerCluster
);
231 FatInfo
.VolumeID
= ((struct _BootSector32
*) Boot
)->VolumeID
;
236 FatInfo
.FatType
= FAT16
;
237 FatInfo
.RootCluster
= FatInfo
.rootStart
/ FatInfo
.SectorsPerCluster
;
239 if (PartitionInfoIsValid
&&
240 FatInfo
.Sectors
> PartitionInfo
.PartitionLength
.QuadPart
/ FatInfo
.BytesPerSector
)
242 *RecognizedFS
= FALSE
;
245 if (pFatInfo
&& *RecognizedFS
)
255 if (!*RecognizedFS
&& PartitionInfoIsValid
)
257 BootFatX
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(struct _BootSectorFatX
), TAG_VFAT
);
258 if (BootFatX
== NULL
)
261 return STATUS_INSUFFICIENT_RESOURCES
;
266 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
267 Status
= VfatReadDisk(DeviceToMount
, &Offset
, sizeof(struct _BootSectorFatX
), (PUCHAR
) BootFatX
, FALSE
);
268 if (NT_SUCCESS(Status
))
270 *RecognizedFS
= TRUE
;
271 if (BootFatX
->SysType
[0] != 'F' ||
272 BootFatX
->SysType
[1] != 'A' ||
273 BootFatX
->SysType
[2] != 'T' ||
274 BootFatX
->SysType
[3] != 'X')
276 DPRINT1("SysType %c%c%c%c\n", BootFatX
->SysType
[0], BootFatX
->SysType
[1], BootFatX
->SysType
[2], BootFatX
->SysType
[3]);
281 BootFatX
->SectorsPerCluster
!= 1 &&
282 BootFatX
->SectorsPerCluster
!= 2 &&
283 BootFatX
->SectorsPerCluster
!= 4 &&
284 BootFatX
->SectorsPerCluster
!= 8 &&
285 BootFatX
->SectorsPerCluster
!= 16 &&
286 BootFatX
->SectorsPerCluster
!= 32 &&
287 BootFatX
->SectorsPerCluster
!= 64 &&
288 BootFatX
->SectorsPerCluster
!= 128)
290 DPRINT1("SectorsPerCluster %lu\n", BootFatX
->SectorsPerCluster
);
296 FatInfo
.BytesPerSector
= DiskGeometry
.BytesPerSector
;
297 FatInfo
.SectorsPerCluster
= BootFatX
->SectorsPerCluster
;
298 FatInfo
.rootDirectorySectors
= BootFatX
->SectorsPerCluster
;
299 FatInfo
.BytesPerCluster
= BootFatX
->SectorsPerCluster
* DiskGeometry
.BytesPerSector
;
300 FatInfo
.Sectors
= (ULONG
)(PartitionInfo
.PartitionLength
.QuadPart
/ DiskGeometry
.BytesPerSector
);
301 if (FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
< 65525)
304 FatInfo
.FatType
= FATX16
;
309 FatInfo
.FatType
= FATX32
;
311 FatInfo
.VolumeID
= BootFatX
->VolumeID
;
312 FatInfo
.FATStart
= sizeof(struct _BootSectorFatX
) / DiskGeometry
.BytesPerSector
;
313 FatInfo
.FATCount
= BootFatX
->FATCount
;
315 ROUND_UP(FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
* (FatInfo
.FatType
== FATX16
? 2 : 4), 4096) /
316 FatInfo
.BytesPerSector
;
317 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
318 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
319 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
320 FatInfo
.NumberOfClusters
= (FatInfo
.Sectors
- FatInfo
.dataStart
) / FatInfo
.SectorsPerCluster
;
322 if (pFatInfo
&& *RecognizedFS
)
328 ExFreePool(BootFatX
);
331 DPRINT("VfatHasFileSystem done\n");
336 VfatMountDevice(PDEVICE_EXTENSION DeviceExt
,
337 PDEVICE_OBJECT DeviceToMount
)
339 * FUNCTION: Mounts the device
343 BOOLEAN RecognizedFS
;
345 DPRINT("Mounting VFAT device...\n");
347 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &DeviceExt
->FatInfo
);
348 if (!NT_SUCCESS(Status
))
352 DPRINT("MountVfatdev %u, PAGE_SIZE = %d\n", DeviceExt
->FatInfo
.BytesPerCluster
, PAGE_SIZE
);
355 return(STATUS_SUCCESS
);
360 VfatMount (PVFAT_IRP_CONTEXT IrpContext
)
362 * FUNCTION: Mount the filesystem
365 PDEVICE_OBJECT DeviceObject
= NULL
;
366 PDEVICE_EXTENSION DeviceExt
= NULL
;
367 BOOLEAN RecognizedFS
;
370 PVFATFCB VolumeFcb
= NULL
;
372 PDEVICE_OBJECT DeviceToMount
;
374 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\$$Fat$$");
375 UNICODE_STRING VolumeNameU
= RTL_CONSTANT_STRING(L
"\\$$Volume$$");
380 DPRINT("VfatMount(IrpContext %p)\n", IrpContext
);
384 if (IrpContext
->DeviceObject
!= VfatGlobalData
->DeviceObject
)
386 Status
= STATUS_INVALID_DEVICE_REQUEST
;
390 DeviceToMount
= IrpContext
->Stack
->Parameters
.MountVolume
.DeviceObject
;
391 Vpb
= IrpContext
->Stack
->Parameters
.MountVolume
.Vpb
;
393 Status
= VfatHasFileSystem (DeviceToMount
, &RecognizedFS
, &FatInfo
);
394 if (!NT_SUCCESS(Status
))
399 if (RecognizedFS
== FALSE
)
401 DPRINT("VFAT: Unrecognized Volume\n");
402 Status
= STATUS_UNRECOGNIZED_VOLUME
;
406 /* Use prime numbers for the table size */
407 if (FatInfo
.FatType
== FAT12
)
409 HashTableSize
= 4099; // 4096 = 4 * 1024
411 else if (FatInfo
.FatType
== FAT16
||
412 FatInfo
.FatType
== FATX16
)
414 HashTableSize
= 16411; // 16384 = 16 * 1024
418 HashTableSize
= 65537; // 65536 = 64 * 1024;
420 HashTableSize
= FCB_HASH_TABLE_SIZE
;
421 DPRINT("VFAT: Recognized volume\n");
422 Status
= IoCreateDevice(VfatGlobalData
->DriverObject
,
423 ROUND_UP(sizeof (DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
,
425 FILE_DEVICE_DISK_FILE_SYSTEM
,
426 DeviceToMount
->Characteristics
,
429 if (!NT_SUCCESS(Status
))
434 DeviceObject
->Flags
= DeviceObject
->Flags
| DO_DIRECT_IO
;
435 DeviceExt
= (PVOID
) 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
;
440 /* use same vpb as device disk */
441 DeviceObject
->Vpb
= Vpb
;
442 DeviceToMount
->Vpb
= Vpb
;
444 Status
= VfatMountDevice(DeviceExt
, DeviceToMount
);
445 if (!NT_SUCCESS(Status
))
447 /* FIXME: delete device object */
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
->FATFileObject
= IoCreateStreamFileObject(NULL
, DeviceExt
->StorageDevice
);
514 Fcb
= vfatNewFCB(DeviceExt
, &NameU
);
517 Status
= STATUS_INSUFFICIENT_RESOURCES
;
520 Ccb
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
523 Status
= STATUS_INSUFFICIENT_RESOURCES
;
527 RtlZeroMemory(Ccb
, sizeof (VFATCCB
));
528 DeviceExt
->FATFileObject
->FsContext
= Fcb
;
529 DeviceExt
->FATFileObject
->FsContext2
= Ccb
;
530 DeviceExt
->FATFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
531 DeviceExt
->FATFileObject
->PrivateCacheMap
= NULL
;
532 DeviceExt
->FATFileObject
->Vpb
= DeviceObject
->Vpb
;
533 Fcb
->FileObject
= DeviceExt
->FATFileObject
;
535 Fcb
->Flags
|= FCB_IS_FAT
;
537 Fcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.FATSectors
* DeviceExt
->FatInfo
.BytesPerSector
;
538 Fcb
->RFCB
.ValidDataLength
= Fcb
->RFCB
.FileSize
;
539 Fcb
->RFCB
.AllocationSize
= Fcb
->RFCB
.FileSize
;
541 CcInitializeCacheMap(DeviceExt
->FATFileObject
,
542 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
544 &VfatGlobalData
->CacheMgrCallbacks
,
547 DeviceExt
->LastAvailableCluster
= 2;
548 ExInitializeResourceLite(&DeviceExt
->FatResource
);
550 InitializeListHead(&DeviceExt
->FcbListHead
);
552 VolumeFcb
= vfatNewFCB(DeviceExt
, &VolumeNameU
);
553 if (VolumeFcb
== NULL
)
555 Status
= STATUS_INSUFFICIENT_RESOURCES
;
558 VolumeFcb
->Flags
= FCB_IS_VOLUME
;
559 VolumeFcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.Sectors
* DeviceExt
->FatInfo
.BytesPerSector
;
560 VolumeFcb
->RFCB
.ValidDataLength
= VolumeFcb
->RFCB
.FileSize
;
561 VolumeFcb
->RFCB
.AllocationSize
= VolumeFcb
->RFCB
.FileSize
;
562 DeviceExt
->VolumeFcb
= VolumeFcb
;
564 ExAcquireResourceExclusiveLite(&VfatGlobalData
->VolumeListLock
, TRUE
);
565 InsertHeadList(&VfatGlobalData
->VolumeListHead
, &DeviceExt
->VolumeListEntry
);
566 ExReleaseResourceLite(&VfatGlobalData
->VolumeListLock
);
568 /* read serial number */
569 DeviceObject
->Vpb
->SerialNumber
= DeviceExt
->FatInfo
.VolumeID
;
571 /* read volume label */
572 ReadVolumeLabel(DeviceExt
, DeviceObject
->Vpb
);
574 /* read clean shutdown bit status */
575 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
576 if (NT_SUCCESS(Status
))
578 if (eocMark
& DeviceExt
->CleanShutBitMask
)
580 /* unset clean shutdown bit */
581 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
582 WriteCluster(DeviceExt
, 1, eocMark
);
583 VolumeFcb
->Flags
|= VCB_CLEAR_DIRTY
;
586 VolumeFcb
->Flags
|= VCB_IS_DIRTY
;
588 FsRtlNotifyVolumeEvent(DeviceExt
->FATFileObject
, FSRTL_VOLUME_MOUNT
);
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_KERNEL_CALL
:
845 case IRP_MN_USER_FS_REQUEST
:
846 switch(IrpContext
->Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
848 case FSCTL_GET_VOLUME_BITMAP
:
849 Status
= VfatGetVolumeBitmap(IrpContext
);
851 case FSCTL_GET_RETRIEVAL_POINTERS
:
852 Status
= VfatGetRetrievalPointers(IrpContext
);
854 case FSCTL_MOVE_FILE
:
855 Status
= VfatMoveFile(IrpContext
);
857 case FSCTL_IS_VOLUME_DIRTY
:
858 Status
= VfatIsVolumeDirty(IrpContext
);
860 case FSCTL_MARK_VOLUME_DIRTY
:
861 Status
= VfatMarkVolumeDirty(IrpContext
);
864 Status
= STATUS_INVALID_DEVICE_REQUEST
;
868 case IRP_MN_MOUNT_VOLUME
:
869 Status
= VfatMount(IrpContext
);
872 case IRP_MN_VERIFY_VOLUME
:
873 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
874 Status
= VfatVerify(IrpContext
);
878 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext
->MinorFunction
);
879 Status
= STATUS_INVALID_DEVICE_REQUEST
;
883 IrpContext
->Irp
->IoStatus
.Status
= Status
;
885 IoCompleteRequest (IrpContext
->Irp
, IO_NO_INCREMENT
);
886 VfatFreeIrpContext(IrpContext
);