7 #include <pseh/pseh2.h>
9 #define USE_ROS_CC_AND_FS
11 #define ROUND_DOWN(n, align) \
12 (((ULONG)n) & ~((align) - 1l))
14 #define ROUND_UP(n, align) \
15 ROUND_DOWN(((ULONG)n) + (align) - 1, (align))
20 unsigned char magic0
, res0
, magic1
;
21 unsigned char OEMName
[8];
22 unsigned short BytesPerSector
;
23 unsigned char SectorsPerCluster
;
24 unsigned short ReservedSectors
;
25 unsigned char FATCount
;
26 unsigned short RootEntries
, Sectors
;
28 unsigned short FATSectors
, SectorsPerTrack
, Heads
;
29 unsigned long HiddenSectors
, SectorsHuge
;
30 unsigned char Drive
, Res1
, Sig
;
31 unsigned long VolumeID
;
32 unsigned char VolumeLabel
[11], SysType
[8];
33 unsigned char Res2
[448];
34 unsigned short Signatur1
;
39 unsigned char magic0
, res0
, magic1
; // 0
40 unsigned char OEMName
[8]; // 3
41 unsigned short BytesPerSector
; // 11
42 unsigned char SectorsPerCluster
; // 13
43 unsigned short ReservedSectors
; // 14
44 unsigned char FATCount
; // 16
45 unsigned short RootEntries
, Sectors
; // 17
46 unsigned char Media
; // 21
47 unsigned short FATSectors
, SectorsPerTrack
, Heads
; // 22
48 unsigned long HiddenSectors
, SectorsHuge
; // 28
49 unsigned long FATSectors32
; // 36
50 unsigned short ExtFlag
; // 40
51 unsigned short FSVersion
; // 42
52 unsigned long RootCluster
; // 44
53 unsigned short FSInfoSector
; // 48
54 unsigned short BootBackup
; // 50
55 unsigned char Res3
[12]; // 52
56 unsigned char Drive
; // 64
57 unsigned char Res4
; // 65
58 unsigned char ExtBootSignature
; // 66
59 unsigned long VolumeID
; // 67
60 unsigned char VolumeLabel
[11], SysType
[8]; // 71
61 unsigned char Res2
[420]; // 90
62 unsigned short Signature1
; // 510
65 struct _BootSectorFatX
67 unsigned char SysType
[4]; // 0
68 unsigned long VolumeID
; // 4
69 unsigned long SectorsPerCluster
; // 8
70 unsigned short FATCount
; // 12
71 unsigned long Unknown
; // 14
72 unsigned char Unused
[4078]; // 18
77 unsigned long ExtBootSignature2
; // 0
78 unsigned char Res6
[480]; // 4
79 unsigned long FSINFOSignature
; // 484
80 unsigned long FreeCluster
; // 488
81 unsigned long NextCluster
; // 492
82 unsigned char Res7
[12]; // 496
83 unsigned long Signatur2
; // 508
86 typedef struct _BootSector BootSector
;
92 struct { unsigned char Filename
[8], Ext
[3]; };
93 unsigned char ShortName
[11];
97 unsigned char CreationTimeMs
;
98 unsigned short CreationTime
,CreationDate
,AccessDate
;
101 unsigned short FirstClusterHigh
; // FAT32
102 unsigned short ExtendedAttributes
; // FAT12/FAT16
104 unsigned short UpdateTime
; //time create/update
105 unsigned short UpdateDate
; //date create/update
106 unsigned short FirstCluster
;
107 unsigned long FileSize
;
110 #define FAT_EAFILE "EA DATA. SF"
112 typedef struct _EAFileHeader FAT_EA_FILE_HEADER
, *PFAT_EA_FILE_HEADER
;
116 unsigned short Signature
; // ED
117 unsigned short Unknown
[15];
118 unsigned short EASetTable
[240];
121 typedef struct _EASetHeader FAT_EA_SET_HEADER
, *PFAT_EA_SET_HEADER
;
125 unsigned short Signature
; // EA
126 unsigned short Offset
; // relative offset, same value as in the EASetTable
127 unsigned short Unknown1
[2];
128 char TargetFileName
[12];
129 unsigned short Unknown2
[3];
130 unsigned int EALength
;
134 typedef struct _EAHeader FAT_EA_HEADER
, *PFAT_EA_HEADER
;
138 unsigned char Unknown
;
139 unsigned char EANameLength
;
140 unsigned short EAValueLength
;
145 typedef struct _FATDirEntry FAT_DIR_ENTRY
, *PFAT_DIR_ENTRY
;
149 unsigned char FilenameLength
; // 0
150 unsigned char Attrib
; // 1
151 unsigned char Filename
[42]; // 2
152 unsigned long FirstCluster
; // 44
153 unsigned long FileSize
; // 48
154 unsigned short UpdateTime
; // 52
155 unsigned short UpdateDate
; // 54
156 unsigned short CreationTime
; // 56
157 unsigned short CreationDate
; // 58
158 unsigned short AccessTime
; // 60
159 unsigned short AccessDate
; // 62
164 unsigned char id
; // sequence number for slot
165 WCHAR name0_4
[5]; // first 5 characters in name
166 unsigned char attr
; // attribute byte
167 unsigned char reserved
; // always 0
168 unsigned char alias_checksum
; // checksum for 8.3 alias
169 WCHAR name5_10
[6]; // 6 more characters in name
170 unsigned char start
[2]; // starting cluster number
171 WCHAR name11_12
[2]; // last 2 characters in name
174 typedef struct _slot slot
;
178 #define VFAT_CASE_LOWER_BASE 8 // base is lower case
179 #define VFAT_CASE_LOWER_EXT 16 // extension is lower case
181 #define LONGNAME_MAX_LENGTH 256 // max length for a long filename
183 #define ENTRY_DELETED(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_DELETED(&((DirEntry)->FatX)) : FAT_ENTRY_DELETED(&((DirEntry)->Fat)))
184 #define ENTRY_VOLUME(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_VOLUME(&((DirEntry)->FatX)) : FAT_ENTRY_VOLUME(&((DirEntry)->Fat)))
185 #define ENTRY_END(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_END(&((DirEntry)->FatX)) : FAT_ENTRY_END(&((DirEntry)->Fat)))
187 #define FAT_ENTRY_DELETED(DirEntry) ((DirEntry)->Filename[0] == 0xe5)
188 #define FAT_ENTRY_END(DirEntry) ((DirEntry)->Filename[0] == 0)
189 #define FAT_ENTRY_LONG(DirEntry) (((DirEntry)->Attrib & 0x3f) == 0x0f)
190 #define FAT_ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
192 #define FATX_ENTRY_DELETED(DirEntry) ((DirEntry)->FilenameLength == 0xe5)
193 #define FATX_ENTRY_END(DirEntry) ((DirEntry)->FilenameLength == 0xff)
194 #define FATX_ENTRY_LONG(DirEntry) (FALSE)
195 #define FATX_ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
197 #define FAT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof (FAT_DIR_ENTRY))
198 #define FATX_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof (FATX_DIR_ENTRY))
200 typedef struct _FATXDirEntry FATX_DIR_ENTRY
, *PFATX_DIR_ENTRY
;
208 typedef union _DIR_ENTRY DIR_ENTRY
, *PDIR_ENTRY
;
210 #define BLOCKSIZE 512
218 #define VCB_VOLUME_LOCKED 0x0001
219 #define VCB_DISMOUNT_PENDING 0x0002
220 #define VCB_IS_FATX 0x0004
221 #define VCB_IS_DIRTY 0x4000 /* Volume is dirty */
222 #define VCB_CLEAR_DIRTY 0x8000 /* Clean dirty flag at shutdown */
230 ULONG rootDirectorySectors
;
234 ULONG SectorsPerCluster
;
235 ULONG BytesPerSector
;
236 ULONG BytesPerCluster
;
237 ULONG NumberOfClusters
;
241 } FATINFO
, *PFATINFO
;
244 struct _VFAT_DIRENTRY_CONTEXT
;
246 typedef struct _HASHENTRY
249 struct _VFATFCB
* self
;
250 struct _HASHENTRY
* next
;
254 #define FCB_HASH_TABLE_SIZE 65536
256 typedef struct DEVICE_EXTENSION
*PDEVICE_EXTENSION
;
258 typedef NTSTATUS (*PGET_NEXT_CLUSTER
)(PDEVICE_EXTENSION
,ULONG
,PULONG
);
259 typedef NTSTATUS (*PFIND_AND_MARK_AVAILABLE_CLUSTER
)(PDEVICE_EXTENSION
,PULONG
);
260 typedef NTSTATUS (*PWRITE_CLUSTER
)(PDEVICE_EXTENSION
,ULONG
,ULONG
,PULONG
);
262 typedef NTSTATUS (*PGET_NEXT_DIR_ENTRY
)(PVOID
*,PVOID
*,struct _VFATFCB
*,struct _VFAT_DIRENTRY_CONTEXT
*,BOOLEAN
);
264 typedef struct DEVICE_EXTENSION
266 ERESOURCE DirResource
;
267 ERESOURCE FatResource
;
269 KSPIN_LOCK FcbListLock
;
270 LIST_ENTRY FcbListHead
;
272 struct _HASHENTRY
**FcbHashTable
;
274 PDEVICE_OBJECT StorageDevice
;
275 PFILE_OBJECT FATFileObject
;
277 ULONG LastAvailableCluster
;
278 ULONG AvailableClusters
;
279 BOOLEAN AvailableClustersValid
;
281 struct _VFATFCB
*VolumeFcb
;
283 /* Pointers to functions for manipulating FAT. */
284 PGET_NEXT_CLUSTER GetNextCluster
;
285 PFIND_AND_MARK_AVAILABLE_CLUSTER FindAndMarkAvailableCluster
;
286 PWRITE_CLUSTER WriteCluster
;
287 ULONG CleanShutBitMask
;
289 /* Pointers to functions for manipulating directory entries. */
290 PGET_NEXT_DIR_ENTRY GetNextDirEntry
;
294 LIST_ENTRY VolumeListEntry
;
297 LIST_ENTRY NotifyList
;
298 PNOTIFY_SYNC NotifySync
;
300 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLEANUP */
301 ULONG OpenHandleCount
;
303 /* VPBs for dismount */
306 } DEVICE_EXTENSION
, VCB
, *PVCB
;
310 PDRIVER_OBJECT DriverObject
;
311 PDEVICE_OBJECT DeviceObject
;
313 ERESOURCE VolumeListLock
;
314 LIST_ENTRY VolumeListHead
;
315 NPAGED_LOOKASIDE_LIST FcbLookasideList
;
316 NPAGED_LOOKASIDE_LIST CcbLookasideList
;
317 NPAGED_LOOKASIDE_LIST IrpContextLookasideList
;
318 FAST_IO_DISPATCH FastIoDispatch
;
319 CACHE_MANAGER_CALLBACKS CacheMgrCallbacks
;
320 } VFAT_GLOBAL_DATA
, *PVFAT_GLOBAL_DATA
;
322 extern PVFAT_GLOBAL_DATA VfatGlobalData
;
324 #define FCB_CACHE_INITIALIZED 0x0001
325 #define FCB_DELETE_PENDING 0x0002
326 #define FCB_IS_FAT 0x0004
327 #define FCB_IS_PAGE_FILE 0x0008
328 #define FCB_IS_VOLUME 0x0010
329 #define FCB_IS_DIRTY 0x0020
330 #define FCB_IS_FATX_ENTRY 0x0040
332 typedef struct _VFATFCB
334 /* FCB header required by ROS/NT */
335 FSRTL_COMMON_FCB_HEADER RFCB
;
336 SECTION_OBJECT_POINTERS SectionObjectPointers
;
337 ERESOURCE MainResource
;
338 ERESOURCE PagingIoResource
;
339 /* end FCB header required by ROS/NT */
341 /* directory entry for this file or directory */
344 /* Pointer to attributes in entry */
347 /* long file name, points into PathNameBuffer */
348 UNICODE_STRING LongNameU
;
350 /* short file name */
351 UNICODE_STRING ShortNameU
;
353 /* directory name, points into PathNameBuffer */
354 UNICODE_STRING DirNameU
;
356 /* path + long file name 260 max*/
357 UNICODE_STRING PathNameU
;
359 /* buffer for PathNameU */
360 PWCHAR PathNameBuffer
;
362 /* buffer for ShortNameU */
363 WCHAR ShortNameBuffer
[13];
368 /* List of FCB's for this volume */
369 LIST_ENTRY FcbListEntry
;
371 /* pointer to the parent fcb */
372 struct _VFATFCB
*parentFcb
;
374 /* Flags for the fcb */
377 /* pointer to the file object which has initialized the fcb */
378 PFILE_OBJECT FileObject
;
380 /* Directory index for the short name entry */
383 /* Directory index where the long name starts */
386 /* Share access for the file object */
387 SHARE_ACCESS FCBShareAccess
;
389 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLEANUP */
390 ULONG OpenHandleCount
;
392 /* Entry into the hash table for the path + long name */
395 /* Entry into the hash table for the path + short name */
398 /* List of byte-range locks for this file */
402 * Optimalization: caching of last read/write cluster+offset pair. Can't
403 * be in VFATCCB because it must be reset everytime the allocated clusters
406 FAST_MUTEX LastMutex
;
409 } VFATFCB
, *PVFATFCB
;
411 typedef struct _VFATCCB
413 LARGE_INTEGER CurrentByteOffset
;
414 /* for DirectoryControl */
416 /* for DirectoryControl */
417 UNICODE_STRING SearchPattern
;
418 } VFATCCB
, *PVFATCCB
;
420 #define TAG_CCB 'BCCV'
421 #define TAG_FCB 'BCFV'
422 #define TAG_IRP 'PRIV'
423 #define TAG_VFAT 'TAFV'
425 #define ENTRIES_PER_SECTOR (BLOCKSIZE / sizeof(FATDirEntry))
427 typedef struct __DOSTIME
435 typedef struct __DOSDATE
443 #define IRPCONTEXT_CANWAIT 0x0001
444 #define IRPCONTEXT_PENDINGRETURNED 0x0002
449 PDEVICE_OBJECT DeviceObject
;
450 PDEVICE_EXTENSION DeviceExt
;
452 WORK_QUEUE_ITEM WorkQueueItem
;
453 PIO_STACK_LOCATION Stack
;
456 PFILE_OBJECT FileObject
;
459 } VFAT_IRP_CONTEXT
, *PVFAT_IRP_CONTEXT
;
461 typedef struct _VFAT_DIRENTRY_CONTEXT
466 UNICODE_STRING LongNameU
;
467 UNICODE_STRING ShortNameU
;
468 } VFAT_DIRENTRY_CONTEXT
, *PVFAT_DIRENTRY_CONTEXT
;
470 typedef struct _VFAT_MOVE_CONTEXT
476 } VFAT_MOVE_CONTEXT
, *PVFAT_MOVE_CONTEXT
;
482 IN PDEVICE_OBJECT pDeviceObject
,
483 IN PLARGE_INTEGER ReadOffset
,
486 IN BOOLEAN Override
);
490 IN PVFAT_IRP_CONTEXT IrpContext
,
491 IN PLARGE_INTEGER ReadOffset
,
493 IN ULONG BufferOffset
,
497 VfatWriteDiskPartial(
498 IN PVFAT_IRP_CONTEXT IrpContext
,
499 IN PLARGE_INTEGER WriteOffset
,
500 IN ULONG WriteLength
,
501 IN ULONG BufferOffset
,
505 VfatBlockDeviceIoControl(
506 IN PDEVICE_OBJECT DeviceObject
,
508 IN PVOID InputBuffer
,
509 IN ULONG InputBufferSize
,
510 IN OUT PVOID OutputBuffer
,
511 IN OUT PULONG pOutputBufferSize
,
512 IN BOOLEAN Override
);
518 PVFAT_IRP_CONTEXT IrpContext
);
524 PVFAT_IRP_CONTEXT IrpContext
);
528 PDEVICE_EXTENSION DeviceExt
,
529 PFILE_OBJECT FileObject
);
535 PVFAT_IRP_CONTEXT IrpContext
);
539 PDEVICE_EXTENSION DeviceExt
,
541 PUNICODE_STRING FileToFindU
,
542 PVFAT_DIRENTRY_CONTEXT DirContext
,
547 PFAT_DIR_ENTRY pEntry
,
548 PUNICODE_STRING NameU
);
552 PDEVICE_EXTENSION DeviceExt
,
558 VfatDirectoryControl(
559 PVFAT_IRP_CONTEXT IrpContext
);
562 FsdDosDateTimeToSystemTime(
563 PDEVICE_EXTENSION DeviceExt
,
566 PLARGE_INTEGER SystemTime
);
569 FsdSystemTimeToDosDateTime(
570 PDEVICE_EXTENSION DeviceExt
,
571 PLARGE_INTEGER SystemTime
,
578 vfatDirEntryGetFirstCluster(
579 PDEVICE_EXTENSION pDeviceExt
,
580 PDIR_ENTRY pDirEntry
);
583 VfatIsDirectoryEmpty(
591 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
599 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
606 PDEVICE_EXTENSION DeviceExt
,
607 PUNICODE_STRING PathNameU
,
610 ULONG RequestedOptions
,
612 PVFAT_MOVE_CONTEXT MoveContext
);
626 PDEVICE_EXTENSION DeviceExt
,
633 IN PDEVICE_EXTENSION DeviceExt
,
635 IN PUNICODE_STRING FileName
,
636 IN BOOLEAN CaseChangeOnly
);
640 IN PDEVICE_EXTENSION DeviceExt
,
642 IN PUNICODE_STRING FileName
,
643 IN PVFATFCB ParentFcb
);
648 VfatSetExtendedAttributes(
649 PFILE_OBJECT FileObject
,
656 VfatInitFastIoRoutines(
657 PFAST_IO_DISPATCH FastIoDispatch
);
661 VfatAcquireForLazyWrite(
667 VfatReleaseFromLazyWrite(
672 VfatAcquireForReadAhead(
678 VfatReleaseFromReadAhead(
685 PDEVICE_EXTENSION DeviceExt
,
686 ULONG CurrentCluster
,
690 FAT12FindAndMarkAvailableCluster(
691 PDEVICE_EXTENSION DeviceExt
,
696 PDEVICE_EXTENSION DeviceExt
,
697 ULONG ClusterToWrite
,
703 PDEVICE_EXTENSION DeviceExt
,
704 ULONG CurrentCluster
,
708 FAT16FindAndMarkAvailableCluster(
709 PDEVICE_EXTENSION DeviceExt
,
714 PDEVICE_EXTENSION DeviceExt
,
715 ULONG ClusterToWrite
,
721 PDEVICE_EXTENSION DeviceExt
,
722 ULONG CurrentCluster
,
726 FAT32FindAndMarkAvailableCluster(
727 PDEVICE_EXTENSION DeviceExt
,
732 PDEVICE_EXTENSION DeviceExt
,
733 ULONG ClusterToWrite
,
739 PDEVICE_EXTENSION DeviceExt
,
747 PDEVICE_EXTENSION DeviceExt
,
752 PDEVICE_EXTENSION DeviceExt
,
753 ULONG CurrentCluster
,
757 GetNextClusterExtend(
758 PDEVICE_EXTENSION DeviceExt
,
759 ULONG CurrentCluster
,
763 CountAvailableClusters(
764 PDEVICE_EXTENSION DeviceExt
,
765 PLARGE_INTEGER Clusters
);
769 PDEVICE_EXTENSION DeviceExt
,
770 ULONG ClusterToWrite
,
777 PDEVICE_EXTENSION pVCB
,
778 PUNICODE_STRING pFileNameU
);
782 PDEVICE_EXTENSION pVCB
,
784 PUNICODE_STRING LongName
,
785 PUNICODE_STRING ShortName
,
798 PDEVICE_EXTENSION pVCB
,
803 PDEVICE_EXTENSION pVCB
,
807 vfatGrabFCBFromTable(
808 PDEVICE_EXTENSION pDeviceExt
,
809 PUNICODE_STRING pFileNameU
);
813 PDEVICE_EXTENSION pVCB
);
817 PDEVICE_EXTENSION pVCB
);
828 vfatAttachFCBToFileObject(
829 PDEVICE_EXTENSION vcb
,
831 PFILE_OBJECT fileObject
);
835 PDEVICE_EXTENSION pVCB
,
837 PUNICODE_STRING FileToFindU
,
842 PDEVICE_EXTENSION pVCB
,
843 PVFATFCB
*pParentFCB
,
845 PUNICODE_STRING pFileNameU
);
848 vfatMakeFCBFromDirEntry(
850 PVFATFCB directoryFCB
,
851 PVFAT_DIRENTRY_CONTEXT DirContext
,
857 VfatQueryInformation(
858 PVFAT_IRP_CONTEXT IrpContext
);
862 PVFAT_IRP_CONTEXT IrpContext
);
865 VfatSetAllocationSizeInformation(
866 PFILE_OBJECT FileObject
,
868 PDEVICE_EXTENSION DeviceExt
,
869 PLARGE_INTEGER AllocationSize
);
875 PVFAT_IRP_CONTEXT IrpContext
);
879 PDEVICE_EXTENSION DeviceExt
,
885 VfatFileSystemControl(
886 PVFAT_IRP_CONTEXT IrpContext
);
893 PDRIVER_OBJECT DriverObject
,
894 PUNICODE_STRING RegistryPath
);
901 PVFAT_IRP_CONTEXT IrpContext
);
904 VfatAllocateIrpContext(
905 PDEVICE_OBJECT DeviceObject
,
910 PVFAT_IRP_CONTEXT IrpContext
);
918 PDEVICE_OBJECT DeviceObject
,
935 PVFAT_IRP_CONTEXT IrpContext
);
941 PVFAT_IRP_CONTEXT IrpContext
);
945 PVFAT_IRP_CONTEXT IrpContext
);
949 PDEVICE_EXTENSION DeviceExt
,
951 PULONG CurrentCluster
,
962 PDEVICE_OBJECT DeviceObject
,
969 PUNICODE_STRING PathNameU
,
970 PUNICODE_STRING DirNameU
,
971 PUNICODE_STRING FileNameU
);
985 VfatQueryVolumeInformation(
986 PVFAT_IRP_CONTEXT IrpContext
);
989 VfatSetVolumeInformation(
990 PVFAT_IRP_CONTEXT IrpContext
);
992 #endif /* _FASTFAT_PCH_ */