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 PartitionInfoIsValid
= TRUE
;
93 DPRINT("Partition Information:\n");
94 DPRINT("StartingOffset %I64x\n", PartitionInfo
.StartingOffset
.QuadPart
/ 512);
95 DPRINT("PartitionLength %I64x\n", PartitionInfo
.PartitionLength
.QuadPart
/ 512);
96 DPRINT("HiddenSectors %u\n", PartitionInfo
.HiddenSectors
);
97 DPRINT("PartitionNumber %u\n", PartitionInfo
.PartitionNumber
);
98 DPRINT("PartitionType %u\n", PartitionInfo
.PartitionType
);
99 DPRINT("BootIndicator %u\n", PartitionInfo
.BootIndicator
);
100 DPRINT("RecognizedPartition %u\n", PartitionInfo
.RecognizedPartition
);
101 DPRINT("RewritePartition %u\n", PartitionInfo
.RewritePartition
);
102 if (PartitionInfo
.PartitionType
)
104 if (PartitionInfo
.PartitionType
== PARTITION_FAT_12
||
105 PartitionInfo
.PartitionType
== PARTITION_FAT_16
||
106 PartitionInfo
.PartitionType
== PARTITION_HUGE
||
107 PartitionInfo
.PartitionType
== PARTITION_FAT32
||
108 PartitionInfo
.PartitionType
== PARTITION_FAT32_XINT13
||
109 PartitionInfo
.PartitionType
== PARTITION_XINT13
)
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 *RecognizedFS
= TRUE
;
123 else if (DiskGeometry
.MediaType
== Unknown
)
126 * Floppy disk driver can return Unknown as media type if it
127 * doesn't know yet what floppy in the drive really is. This is
128 * perfectly correct to do under Windows.
130 *RecognizedFS
= TRUE
;
131 DiskGeometry
.BytesPerSector
= 512;
135 *RecognizedFS
= TRUE
;
140 Boot
= ExAllocatePoolWithTag(NonPagedPool
, DiskGeometry
.BytesPerSector
, TAG_VFAT
);
143 return STATUS_INSUFFICIENT_RESOURCES
;
148 /* Try to recognize FAT12/FAT16/FAT32 partitions */
149 Status
= VfatReadDisk(DeviceToMount
, &Offset
, DiskGeometry
.BytesPerSector
, (PUCHAR
) Boot
, FALSE
);
150 if (NT_SUCCESS(Status
))
152 if (Boot
->Signatur1
!= 0xaa55)
154 *RecognizedFS
= FALSE
;
158 Boot
->BytesPerSector
!= 512 &&
159 Boot
->BytesPerSector
!= 1024 &&
160 Boot
->BytesPerSector
!= 2048 &&
161 Boot
->BytesPerSector
!= 4096)
163 DPRINT1("BytesPerSector %u\n", Boot
->BytesPerSector
);
164 *RecognizedFS
= FALSE
;
168 Boot
->FATCount
!= 1 &&
171 DPRINT1("FATCount %u\n", Boot
->FATCount
);
172 *RecognizedFS
= FALSE
;
176 Boot
->Media
!= 0xf0 &&
177 Boot
->Media
!= 0xf8 &&
178 Boot
->Media
!= 0xf9 &&
179 Boot
->Media
!= 0xfa &&
180 Boot
->Media
!= 0xfb &&
181 Boot
->Media
!= 0xfc &&
182 Boot
->Media
!= 0xfd &&
183 Boot
->Media
!= 0xfe &&
186 DPRINT1("Media %02x\n", Boot
->Media
);
187 *RecognizedFS
= FALSE
;
191 Boot
->SectorsPerCluster
!= 1 &&
192 Boot
->SectorsPerCluster
!= 2 &&
193 Boot
->SectorsPerCluster
!= 4 &&
194 Boot
->SectorsPerCluster
!= 8 &&
195 Boot
->SectorsPerCluster
!= 16 &&
196 Boot
->SectorsPerCluster
!= 32 &&
197 Boot
->SectorsPerCluster
!= 64 &&
198 Boot
->SectorsPerCluster
!= 128)
200 DPRINT1("SectorsPerCluster %02x\n", Boot
->SectorsPerCluster
);
201 *RecognizedFS
= FALSE
;
205 Boot
->BytesPerSector
* Boot
->SectorsPerCluster
> 32 * 1024)
207 DPRINT1("ClusterSize %dx\n", Boot
->BytesPerSector
* Boot
->SectorsPerCluster
);
208 *RecognizedFS
= FALSE
;
213 FatInfo
.VolumeID
= Boot
->VolumeID
;
214 FatInfo
.FATStart
= Boot
->ReservedSectors
;
215 FatInfo
.FATCount
= Boot
->FATCount
;
216 FatInfo
.FATSectors
= Boot
->FATSectors
? Boot
->FATSectors
: ((struct _BootSector32
*) Boot
)->FATSectors32
;
217 FatInfo
.BytesPerSector
= Boot
->BytesPerSector
;
218 FatInfo
.SectorsPerCluster
= Boot
->SectorsPerCluster
;
219 FatInfo
.BytesPerCluster
= FatInfo
.BytesPerSector
* FatInfo
.SectorsPerCluster
;
220 FatInfo
.rootDirectorySectors
= ((Boot
->RootEntries
* 32) + Boot
->BytesPerSector
- 1) / Boot
->BytesPerSector
;
221 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
222 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
223 FatInfo
.Sectors
= Sectors
= Boot
->Sectors
? Boot
->Sectors
: Boot
->SectorsHuge
;
224 Sectors
-= Boot
->ReservedSectors
+ FatInfo
.FATCount
* FatInfo
.FATSectors
+ FatInfo
.rootDirectorySectors
;
225 FatInfo
.NumberOfClusters
= Sectors
/ Boot
->SectorsPerCluster
;
226 if (FatInfo
.NumberOfClusters
< 4085)
229 FatInfo
.FatType
= FAT12
;
230 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
232 else if (FatInfo
.NumberOfClusters
>= 65525)
235 FatInfo
.FatType
= FAT32
;
236 FatInfo
.RootCluster
= ((struct _BootSector32
*) Boot
)->RootCluster
;
237 FatInfo
.rootStart
= FatInfo
.dataStart
+ ((FatInfo
.RootCluster
- 2) * FatInfo
.SectorsPerCluster
);
238 FatInfo
.VolumeID
= ((struct _BootSector32
*) Boot
)->VolumeID
;
243 FatInfo
.FatType
= FAT16
;
244 FatInfo
.RootCluster
= FatInfo
.rootStart
/ FatInfo
.SectorsPerCluster
;
247 if (PartitionInfoIsValid
&&
248 FatInfo
.Sectors
> PartitionInfo
.PartitionLength
.QuadPart
/ FatInfo
.BytesPerSector
)
250 *RecognizedFS
= FALSE
;
253 if (pFatInfo
&& *RecognizedFS
)
263 if (!*RecognizedFS
&& PartitionInfoIsValid
)
265 BootFatX
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(struct _BootSectorFatX
), TAG_VFAT
);
266 if (BootFatX
== NULL
)
269 return STATUS_INSUFFICIENT_RESOURCES
;
274 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
275 Status
= VfatReadDisk(DeviceToMount
, &Offset
, sizeof(struct _BootSectorFatX
), (PUCHAR
) BootFatX
, FALSE
);
276 if (NT_SUCCESS(Status
))
278 *RecognizedFS
= TRUE
;
279 if (BootFatX
->SysType
[0] != 'F' ||
280 BootFatX
->SysType
[1] != 'A' ||
281 BootFatX
->SysType
[2] != 'T' ||
282 BootFatX
->SysType
[3] != 'X')
284 DPRINT1("SysType %c%c%c%c\n", BootFatX
->SysType
[0], BootFatX
->SysType
[1], BootFatX
->SysType
[2], BootFatX
->SysType
[3]);
289 BootFatX
->SectorsPerCluster
!= 1 &&
290 BootFatX
->SectorsPerCluster
!= 2 &&
291 BootFatX
->SectorsPerCluster
!= 4 &&
292 BootFatX
->SectorsPerCluster
!= 8 &&
293 BootFatX
->SectorsPerCluster
!= 16 &&
294 BootFatX
->SectorsPerCluster
!= 32 &&
295 BootFatX
->SectorsPerCluster
!= 64 &&
296 BootFatX
->SectorsPerCluster
!= 128)
298 DPRINT1("SectorsPerCluster %lu\n", BootFatX
->SectorsPerCluster
);
304 FatInfo
.BytesPerSector
= DiskGeometry
.BytesPerSector
;
305 FatInfo
.SectorsPerCluster
= BootFatX
->SectorsPerCluster
;
306 FatInfo
.rootDirectorySectors
= BootFatX
->SectorsPerCluster
;
307 FatInfo
.BytesPerCluster
= BootFatX
->SectorsPerCluster
* DiskGeometry
.BytesPerSector
;
308 FatInfo
.Sectors
= (ULONG
)(PartitionInfo
.PartitionLength
.QuadPart
/ DiskGeometry
.BytesPerSector
);
309 if (FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
< 65525)
312 FatInfo
.FatType
= FATX16
;
317 FatInfo
.FatType
= FATX32
;
319 FatInfo
.VolumeID
= BootFatX
->VolumeID
;
320 FatInfo
.FATStart
= sizeof(struct _BootSectorFatX
) / DiskGeometry
.BytesPerSector
;
321 FatInfo
.FATCount
= BootFatX
->FATCount
;
323 ROUND_UP(FatInfo
.Sectors
/ FatInfo
.SectorsPerCluster
* (FatInfo
.FatType
== FATX16
? 2 : 4), 4096) /
324 FatInfo
.BytesPerSector
;
325 FatInfo
.rootStart
= FatInfo
.FATStart
+ FatInfo
.FATCount
* FatInfo
.FATSectors
;
326 FatInfo
.RootCluster
= (FatInfo
.rootStart
- 1) / FatInfo
.SectorsPerCluster
;
327 FatInfo
.dataStart
= FatInfo
.rootStart
+ FatInfo
.rootDirectorySectors
;
328 FatInfo
.NumberOfClusters
= (FatInfo
.Sectors
- FatInfo
.dataStart
) / FatInfo
.SectorsPerCluster
;
330 if (pFatInfo
&& *RecognizedFS
)
336 ExFreePool(BootFatX
);
339 DPRINT("VfatHasFileSystem done\n");
344 * FUNCTION: Mounts the device
349 PDEVICE_EXTENSION DeviceExt
,
350 PDEVICE_OBJECT DeviceToMount
)
353 BOOLEAN RecognizedFS
;
355 DPRINT("Mounting VFAT device...\n");
357 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &DeviceExt
->FatInfo
);
358 if (!NT_SUCCESS(Status
))
362 DPRINT("MountVfatdev %u, PAGE_SIZE = %d\n", DeviceExt
->FatInfo
.BytesPerCluster
, PAGE_SIZE
);
364 return STATUS_SUCCESS
;
369 * FUNCTION: Mount the filesystem
374 PVFAT_IRP_CONTEXT IrpContext
)
376 PDEVICE_OBJECT DeviceObject
= NULL
;
377 PDEVICE_EXTENSION DeviceExt
= NULL
;
378 BOOLEAN RecognizedFS
;
381 PVFATFCB VolumeFcb
= NULL
;
383 PDEVICE_OBJECT DeviceToMount
;
385 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\$$Fat$$");
386 UNICODE_STRING VolumeNameU
= RTL_CONSTANT_STRING(L
"\\$$Volume$$");
391 DPRINT("VfatMount(IrpContext %p)\n", IrpContext
);
395 if (IrpContext
->DeviceObject
!= VfatGlobalData
->DeviceObject
)
397 Status
= STATUS_INVALID_DEVICE_REQUEST
;
401 DeviceToMount
= IrpContext
->Stack
->Parameters
.MountVolume
.DeviceObject
;
402 Vpb
= IrpContext
->Stack
->Parameters
.MountVolume
.Vpb
;
404 Status
= VfatHasFileSystem(DeviceToMount
, &RecognizedFS
, &FatInfo
);
405 if (!NT_SUCCESS(Status
))
410 if (RecognizedFS
== FALSE
)
412 DPRINT("VFAT: Unrecognized Volume\n");
413 Status
= STATUS_UNRECOGNIZED_VOLUME
;
417 /* Use prime numbers for the table size */
418 if (FatInfo
.FatType
== FAT12
)
420 HashTableSize
= 4099; // 4096 = 4 * 1024
422 else if (FatInfo
.FatType
== FAT16
||
423 FatInfo
.FatType
== FATX16
)
425 HashTableSize
= 16411; // 16384 = 16 * 1024
429 HashTableSize
= 65537; // 65536 = 64 * 1024;
431 HashTableSize
= FCB_HASH_TABLE_SIZE
;
432 DPRINT("VFAT: Recognized volume\n");
433 Status
= IoCreateDevice(VfatGlobalData
->DriverObject
,
434 ROUND_UP(sizeof (DEVICE_EXTENSION
), sizeof(ULONG
)) + sizeof(HASHENTRY
*) * HashTableSize
,
436 FILE_DEVICE_DISK_FILE_SYSTEM
,
437 DeviceToMount
->Characteristics
,
440 if (!NT_SUCCESS(Status
))
445 DeviceObject
->Flags
= DeviceObject
->Flags
| DO_DIRECT_IO
;
446 DeviceExt
= (PVOID
) 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
->FATFileObject
= IoCreateStreamFileObject(NULL
, DeviceExt
->StorageDevice
);
525 Fcb
= vfatNewFCB(DeviceExt
, &NameU
);
528 Status
= STATUS_INSUFFICIENT_RESOURCES
;
532 Ccb
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
535 Status
= STATUS_INSUFFICIENT_RESOURCES
;
539 RtlZeroMemory(Ccb
, sizeof (VFATCCB
));
540 DeviceExt
->FATFileObject
->FsContext
= Fcb
;
541 DeviceExt
->FATFileObject
->FsContext2
= Ccb
;
542 DeviceExt
->FATFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
543 DeviceExt
->FATFileObject
->PrivateCacheMap
= NULL
;
544 DeviceExt
->FATFileObject
->Vpb
= DeviceObject
->Vpb
;
545 Fcb
->FileObject
= DeviceExt
->FATFileObject
;
547 Fcb
->Flags
|= FCB_IS_FAT
;
549 Fcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.FATSectors
* DeviceExt
->FatInfo
.BytesPerSector
;
550 Fcb
->RFCB
.ValidDataLength
= Fcb
->RFCB
.FileSize
;
551 Fcb
->RFCB
.AllocationSize
= Fcb
->RFCB
.FileSize
;
553 CcInitializeCacheMap(DeviceExt
->FATFileObject
,
554 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
556 &VfatGlobalData
->CacheMgrCallbacks
,
559 DeviceExt
->LastAvailableCluster
= 2;
560 ExInitializeResourceLite(&DeviceExt
->FatResource
);
562 InitializeListHead(&DeviceExt
->FcbListHead
);
564 VolumeFcb
= vfatNewFCB(DeviceExt
, &VolumeNameU
);
565 if (VolumeFcb
== NULL
)
567 Status
= STATUS_INSUFFICIENT_RESOURCES
;
571 VolumeFcb
->Flags
= FCB_IS_VOLUME
;
572 VolumeFcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->FatInfo
.Sectors
* DeviceExt
->FatInfo
.BytesPerSector
;
573 VolumeFcb
->RFCB
.ValidDataLength
= VolumeFcb
->RFCB
.FileSize
;
574 VolumeFcb
->RFCB
.AllocationSize
= VolumeFcb
->RFCB
.FileSize
;
575 DeviceExt
->VolumeFcb
= VolumeFcb
;
577 ExAcquireResourceExclusiveLite(&VfatGlobalData
->VolumeListLock
, TRUE
);
578 InsertHeadList(&VfatGlobalData
->VolumeListHead
, &DeviceExt
->VolumeListEntry
);
579 ExReleaseResourceLite(&VfatGlobalData
->VolumeListLock
);
581 /* read serial number */
582 DeviceObject
->Vpb
->SerialNumber
= DeviceExt
->FatInfo
.VolumeID
;
584 /* read volume label */
585 ReadVolumeLabel(DeviceExt
, DeviceObject
->Vpb
);
587 /* read clean shutdown bit status */
588 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
589 if (NT_SUCCESS(Status
))
591 if (eocMark
& DeviceExt
->CleanShutBitMask
)
593 /* unset clean shutdown bit */
594 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
595 WriteCluster(DeviceExt
, 1, eocMark
);
596 VolumeFcb
->Flags
|= VCB_CLEAR_DIRTY
;
600 VolumeFcb
->Flags
|= VCB_IS_DIRTY
;
602 FsRtlNotifyVolumeEvent(DeviceExt
->FATFileObject
, FSRTL_VOLUME_MOUNT
);
603 FsRtlNotifyInitializeSync(&DeviceExt
->NotifySync
);
604 InitializeListHead(&DeviceExt
->NotifyList
);
606 Status
= STATUS_SUCCESS
;
609 if (!NT_SUCCESS(Status
))
612 if (DeviceExt
&& DeviceExt
->FATFileObject
)
613 ObDereferenceObject (DeviceExt
->FATFileObject
);
619 IoDeleteDevice(DeviceObject
);
627 * FUNCTION: Verify the filesystem
632 PVFAT_IRP_CONTEXT IrpContext
)
634 PDEVICE_OBJECT DeviceToVerify
;
635 NTSTATUS Status
= STATUS_SUCCESS
;
637 BOOLEAN RecognizedFS
;
638 PDEVICE_EXTENSION DeviceExt
= IrpContext
->DeviceExt
;
640 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext
);
642 DeviceToVerify
= IrpContext
->Stack
->Parameters
.VerifyVolume
.DeviceObject
;
643 Status
= VfatBlockDeviceIoControl(DeviceToVerify
,
644 IOCTL_DISK_CHECK_VERIFY
,
650 DeviceToVerify
->Flags
&= ~DO_VERIFY_VOLUME
;
651 if (!NT_SUCCESS(Status
) && Status
!= STATUS_VERIFY_REQUIRED
)
653 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status
);
654 Status
= STATUS_WRONG_VOLUME
;
658 Status
= VfatHasFileSystem(DeviceToVerify
, &RecognizedFS
, &FatInfo
);
659 if (!NT_SUCCESS(Status
) || RecognizedFS
== FALSE
)
661 Status
= STATUS_WRONG_VOLUME
;
663 else if (sizeof(FATINFO
) == RtlCompareMemory(&FatInfo
, &DeviceExt
->FatInfo
, sizeof(FATINFO
)))
667 * Preformated floppy disks have very often a serial number of 0000:0000.
668 * We should calculate a crc sum over the sectors from the root directory as secondary volume number.
669 * Each write to the root directory must update this crc sum.
674 Status
= STATUS_WRONG_VOLUME
;
685 PVFAT_IRP_CONTEXT IrpContext
)
687 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext
);
688 return STATUS_INVALID_DEVICE_REQUEST
;
694 VfatGetRetrievalPointers(
695 PVFAT_IRP_CONTEXT IrpContext
)
697 PIO_STACK_LOCATION Stack
;
699 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers
;
700 PFILE_OBJECT FileObject
;
701 ULONG MaxExtentCount
;
703 PDEVICE_EXTENSION DeviceExt
;
705 ULONG CurrentCluster
;
709 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext
);
711 DeviceExt
= IrpContext
->DeviceExt
;
712 FileObject
= IrpContext
->FileObject
;
713 Stack
= IrpContext
->Stack
;
714 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
< sizeof(STARTING_VCN_INPUT_BUFFER
) ||
715 Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
== NULL
)
717 return STATUS_INVALID_PARAMETER
;
720 if (IrpContext
->Irp
->UserBuffer
== NULL
||
721 Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(RETRIEVAL_POINTERS_BUFFER
))
723 return STATUS_BUFFER_TOO_SMALL
;
726 Fcb
= FileObject
->FsContext
;
728 ExAcquireResourceSharedLite(&Fcb
->MainResource
, TRUE
);
730 Vcn
= ((PSTARTING_VCN_INPUT_BUFFER
)Stack
->Parameters
.DeviceIoControl
.Type3InputBuffer
)->StartingVcn
;
731 RetrievalPointers
= IrpContext
->Irp
->UserBuffer
;
733 MaxExtentCount
= ((Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
- sizeof(RetrievalPointers
->ExtentCount
) - sizeof(RetrievalPointers
->StartingVcn
)) / sizeof(RetrievalPointers
->Extents
[0]));
735 if (Vcn
.QuadPart
>= Fcb
->RFCB
.AllocationSize
.QuadPart
/ DeviceExt
->FatInfo
.BytesPerCluster
)
737 Status
= STATUS_INVALID_PARAMETER
;
741 CurrentCluster
= FirstCluster
= vfatDirEntryGetFirstCluster(DeviceExt
, &Fcb
->entry
);
742 Status
= OffsetToCluster(DeviceExt
, FirstCluster
,
743 Vcn
.u
.LowPart
* DeviceExt
->FatInfo
.BytesPerCluster
,
744 &CurrentCluster
, FALSE
);
745 if (!NT_SUCCESS(Status
))
750 RetrievalPointers
->StartingVcn
= Vcn
;
751 RetrievalPointers
->ExtentCount
= 0;
752 RetrievalPointers
->Extents
[0].Lcn
.u
.HighPart
= 0;
753 RetrievalPointers
->Extents
[0].Lcn
.u
.LowPart
= CurrentCluster
- 2;
755 while (CurrentCluster
!= 0xffffffff && RetrievalPointers
->ExtentCount
< MaxExtentCount
)
757 LastCluster
= CurrentCluster
;
758 Status
= NextCluster(DeviceExt
, CurrentCluster
, &CurrentCluster
, FALSE
);
760 if (!NT_SUCCESS(Status
))
765 if (LastCluster
+ 1 != CurrentCluster
)
767 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].NextVcn
= Vcn
;
768 RetrievalPointers
->ExtentCount
++;
769 if (RetrievalPointers
->ExtentCount
< MaxExtentCount
)
771 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.HighPart
= 0;
772 RetrievalPointers
->Extents
[RetrievalPointers
->ExtentCount
].Lcn
.u
.LowPart
= CurrentCluster
- 2;
777 IrpContext
->Irp
->IoStatus
.Information
= sizeof(RETRIEVAL_POINTERS_BUFFER
) + (sizeof(RetrievalPointers
->Extents
[0]) * (RetrievalPointers
->ExtentCount
- 1));
778 Status
= STATUS_SUCCESS
;
781 ExReleaseResourceLite(&Fcb
->MainResource
);
789 PVFAT_IRP_CONTEXT IrpContext
)
791 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext
);
792 return STATUS_INVALID_DEVICE_REQUEST
;
798 PVFAT_IRP_CONTEXT IrpContext
)
802 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext
);
804 if (IrpContext
->Stack
->Parameters
.FileSystemControl
.OutputBufferLength
!= sizeof(ULONG
))
805 return STATUS_INVALID_BUFFER_SIZE
;
806 else if (!IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
)
807 return STATUS_INVALID_USER_BUFFER
;
809 Flags
= (PULONG
)IrpContext
->Irp
->AssociatedIrp
.SystemBuffer
;
812 if ((IrpContext
->DeviceExt
->VolumeFcb
->Flags
& VCB_IS_DIRTY
) &&
813 !(IrpContext
->DeviceExt
->VolumeFcb
->Flags
& VCB_CLEAR_DIRTY
))
815 *Flags
|= VOLUME_IS_DIRTY
;
818 return STATUS_SUCCESS
;
824 PVFAT_IRP_CONTEXT IrpContext
)
827 PDEVICE_EXTENSION DeviceExt
;
828 NTSTATUS Status
= STATUS_SUCCESS
;
830 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext
);
831 DeviceExt
= IrpContext
->DeviceExt
;
833 if (!(DeviceExt
->VolumeFcb
->Flags
& VCB_IS_DIRTY
))
835 Status
= GetNextCluster(DeviceExt
, 1, &eocMark
);
836 if (NT_SUCCESS(Status
))
838 /* unset clean shutdown bit */
839 eocMark
&= ~DeviceExt
->CleanShutBitMask
;
840 Status
= WriteCluster(DeviceExt
, 1, eocMark
);
844 DeviceExt
->VolumeFcb
->Flags
&= ~VCB_CLEAR_DIRTY
;
850 * FUNCTION: File system control
853 VfatFileSystemControl(
854 PVFAT_IRP_CONTEXT IrpContext
)
858 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext
);
861 ASSERT(IrpContext
->Irp
);
862 ASSERT(IrpContext
->Stack
);
864 IrpContext
->Irp
->IoStatus
.Information
= 0;
866 switch (IrpContext
->MinorFunction
)
868 case IRP_MN_KERNEL_CALL
:
869 case IRP_MN_USER_FS_REQUEST
:
870 switch(IrpContext
->Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
872 case FSCTL_GET_VOLUME_BITMAP
:
873 Status
= VfatGetVolumeBitmap(IrpContext
);
876 case FSCTL_GET_RETRIEVAL_POINTERS
:
877 Status
= VfatGetRetrievalPointers(IrpContext
);
880 case FSCTL_MOVE_FILE
:
881 Status
= VfatMoveFile(IrpContext
);
884 case FSCTL_IS_VOLUME_DIRTY
:
885 Status
= VfatIsVolumeDirty(IrpContext
);
888 case FSCTL_MARK_VOLUME_DIRTY
:
889 Status
= VfatMarkVolumeDirty(IrpContext
);
893 Status
= STATUS_INVALID_DEVICE_REQUEST
;
897 case IRP_MN_MOUNT_VOLUME
:
898 Status
= VfatMount(IrpContext
);
901 case IRP_MN_VERIFY_VOLUME
:
902 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
903 Status
= VfatVerify(IrpContext
);
907 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext
->MinorFunction
);
908 Status
= STATUS_INVALID_DEVICE_REQUEST
;
912 IrpContext
->Irp
->IoStatus
.Status
= Status
;
914 IoCompleteRequest(IrpContext
->Irp
, IO_NO_INCREMENT
);
915 VfatFreeIrpContext(IrpContext
);