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
;
299 } DEVICE_EXTENSION
, VCB
, *PVCB
;
303 PDRIVER_OBJECT DriverObject
;
304 PDEVICE_OBJECT DeviceObject
;
306 ERESOURCE VolumeListLock
;
307 LIST_ENTRY VolumeListHead
;
308 NPAGED_LOOKASIDE_LIST FcbLookasideList
;
309 NPAGED_LOOKASIDE_LIST CcbLookasideList
;
310 NPAGED_LOOKASIDE_LIST IrpContextLookasideList
;
311 FAST_IO_DISPATCH FastIoDispatch
;
312 CACHE_MANAGER_CALLBACKS CacheMgrCallbacks
;
313 } VFAT_GLOBAL_DATA
, *PVFAT_GLOBAL_DATA
;
315 extern PVFAT_GLOBAL_DATA VfatGlobalData
;
317 #define FCB_CACHE_INITIALIZED 0x0001
318 #define FCB_DELETE_PENDING 0x0002
319 #define FCB_IS_FAT 0x0004
320 #define FCB_IS_PAGE_FILE 0x0008
321 #define FCB_IS_VOLUME 0x0010
322 #define FCB_IS_DIRTY 0x0020
323 #define FCB_IS_FATX_ENTRY 0x0040
325 typedef struct _VFATFCB
327 /* FCB header required by ROS/NT */
328 FSRTL_COMMON_FCB_HEADER RFCB
;
329 SECTION_OBJECT_POINTERS SectionObjectPointers
;
330 ERESOURCE MainResource
;
331 ERESOURCE PagingIoResource
;
332 /* end FCB header required by ROS/NT */
334 /* directory entry for this file or directory */
337 /* Pointer to attributes in entry */
340 /* long file name, points into PathNameBuffer */
341 UNICODE_STRING LongNameU
;
343 /* short file name */
344 UNICODE_STRING ShortNameU
;
346 /* directory name, points into PathNameBuffer */
347 UNICODE_STRING DirNameU
;
349 /* path + long file name 260 max*/
350 UNICODE_STRING PathNameU
;
352 /* buffer for PathNameU */
353 PWCHAR PathNameBuffer
;
355 /* buffer for ShortNameU */
356 WCHAR ShortNameBuffer
[13];
361 /* List of FCB's for this volume */
362 LIST_ENTRY FcbListEntry
;
364 /* pointer to the parent fcb */
365 struct _VFATFCB
*parentFcb
;
367 /* Flags for the fcb */
370 /* pointer to the file object which has initialized the fcb */
371 PFILE_OBJECT FileObject
;
373 /* Directory index for the short name entry */
376 /* Directory index where the long name starts */
379 /* Share access for the file object */
380 SHARE_ACCESS FCBShareAccess
;
382 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLEANUP */
383 ULONG OpenHandleCount
;
385 /* Entry into the hash table for the path + long name */
388 /* Entry into the hash table for the path + short name */
391 /* List of byte-range locks for this file */
395 * Optimalization: caching of last read/write cluster+offset pair. Can't
396 * be in VFATCCB because it must be reset everytime the allocated clusters
399 FAST_MUTEX LastMutex
;
402 } VFATFCB
, *PVFATFCB
;
404 typedef struct _VFATCCB
406 LARGE_INTEGER CurrentByteOffset
;
407 /* for DirectoryControl */
409 /* for DirectoryControl */
410 UNICODE_STRING SearchPattern
;
411 } VFATCCB
, *PVFATCCB
;
413 #define TAG_CCB 'BCCV'
414 #define TAG_FCB 'BCFV'
415 #define TAG_IRP 'PRIV'
416 #define TAG_VFAT 'TAFV'
418 #define ENTRIES_PER_SECTOR (BLOCKSIZE / sizeof(FATDirEntry))
420 typedef struct __DOSTIME
428 typedef struct __DOSDATE
436 #define IRPCONTEXT_CANWAIT 0x0001
437 #define IRPCONTEXT_PENDINGRETURNED 0x0002
442 PDEVICE_OBJECT DeviceObject
;
443 PDEVICE_EXTENSION DeviceExt
;
445 WORK_QUEUE_ITEM WorkQueueItem
;
446 PIO_STACK_LOCATION Stack
;
449 PFILE_OBJECT FileObject
;
452 } VFAT_IRP_CONTEXT
, *PVFAT_IRP_CONTEXT
;
454 typedef struct _VFAT_DIRENTRY_CONTEXT
459 UNICODE_STRING LongNameU
;
460 UNICODE_STRING ShortNameU
;
461 } VFAT_DIRENTRY_CONTEXT
, *PVFAT_DIRENTRY_CONTEXT
;
463 typedef struct _VFAT_MOVE_CONTEXT
469 } VFAT_MOVE_CONTEXT
, *PVFAT_MOVE_CONTEXT
;
475 IN PDEVICE_OBJECT pDeviceObject
,
476 IN PLARGE_INTEGER ReadOffset
,
479 IN BOOLEAN Override
);
483 IN PVFAT_IRP_CONTEXT IrpContext
,
484 IN PLARGE_INTEGER ReadOffset
,
486 IN ULONG BufferOffset
,
490 VfatWriteDiskPartial(
491 IN PVFAT_IRP_CONTEXT IrpContext
,
492 IN PLARGE_INTEGER WriteOffset
,
493 IN ULONG WriteLength
,
494 IN ULONG BufferOffset
,
498 VfatBlockDeviceIoControl(
499 IN PDEVICE_OBJECT DeviceObject
,
501 IN PVOID InputBuffer
,
502 IN ULONG InputBufferSize
,
503 IN OUT PVOID OutputBuffer
,
504 IN OUT PULONG pOutputBufferSize
,
505 IN BOOLEAN Override
);
511 PVFAT_IRP_CONTEXT IrpContext
);
517 PVFAT_IRP_CONTEXT IrpContext
);
521 PDEVICE_EXTENSION DeviceExt
,
522 PFILE_OBJECT FileObject
);
528 PVFAT_IRP_CONTEXT IrpContext
);
532 PDEVICE_EXTENSION DeviceExt
,
534 PUNICODE_STRING FileToFindU
,
535 PVFAT_DIRENTRY_CONTEXT DirContext
,
540 PFAT_DIR_ENTRY pEntry
,
541 PUNICODE_STRING NameU
);
545 PDEVICE_EXTENSION DeviceExt
,
551 VfatDirectoryControl(
552 PVFAT_IRP_CONTEXT IrpContext
);
555 FsdDosDateTimeToSystemTime(
556 PDEVICE_EXTENSION DeviceExt
,
559 PLARGE_INTEGER SystemTime
);
562 FsdSystemTimeToDosDateTime(
563 PDEVICE_EXTENSION DeviceExt
,
564 PLARGE_INTEGER SystemTime
,
571 vfatDirEntryGetFirstCluster(
572 PDEVICE_EXTENSION pDeviceExt
,
573 PDIR_ENTRY pDirEntry
);
576 VfatIsDirectoryEmpty(
584 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
592 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
599 PDEVICE_EXTENSION DeviceExt
,
600 PUNICODE_STRING PathNameU
,
603 ULONG RequestedOptions
,
605 PVFAT_MOVE_CONTEXT MoveContext
);
619 PDEVICE_EXTENSION DeviceExt
,
626 IN PDEVICE_EXTENSION DeviceExt
,
628 IN PUNICODE_STRING FileName
,
629 IN BOOLEAN CaseChangeOnly
);
633 IN PDEVICE_EXTENSION DeviceExt
,
635 IN PUNICODE_STRING FileName
,
636 IN PVFATFCB ParentFcb
);
641 VfatSetExtendedAttributes(
642 PFILE_OBJECT FileObject
,
649 VfatInitFastIoRoutines(
650 PFAST_IO_DISPATCH FastIoDispatch
);
654 VfatAcquireForLazyWrite(
660 VfatReleaseFromLazyWrite(
665 VfatAcquireForReadAhead(
671 VfatReleaseFromReadAhead(
678 PDEVICE_EXTENSION DeviceExt
,
679 ULONG CurrentCluster
,
683 FAT12FindAndMarkAvailableCluster(
684 PDEVICE_EXTENSION DeviceExt
,
689 PDEVICE_EXTENSION DeviceExt
,
690 ULONG ClusterToWrite
,
696 PDEVICE_EXTENSION DeviceExt
,
697 ULONG CurrentCluster
,
701 FAT16FindAndMarkAvailableCluster(
702 PDEVICE_EXTENSION DeviceExt
,
707 PDEVICE_EXTENSION DeviceExt
,
708 ULONG ClusterToWrite
,
714 PDEVICE_EXTENSION DeviceExt
,
715 ULONG CurrentCluster
,
719 FAT32FindAndMarkAvailableCluster(
720 PDEVICE_EXTENSION DeviceExt
,
725 PDEVICE_EXTENSION DeviceExt
,
726 ULONG ClusterToWrite
,
732 PDEVICE_EXTENSION DeviceExt
,
740 PDEVICE_EXTENSION DeviceExt
,
745 PDEVICE_EXTENSION DeviceExt
,
746 ULONG CurrentCluster
,
750 GetNextClusterExtend(
751 PDEVICE_EXTENSION DeviceExt
,
752 ULONG CurrentCluster
,
756 CountAvailableClusters(
757 PDEVICE_EXTENSION DeviceExt
,
758 PLARGE_INTEGER Clusters
);
762 PDEVICE_EXTENSION DeviceExt
,
763 ULONG ClusterToWrite
,
770 PDEVICE_EXTENSION pVCB
,
771 PUNICODE_STRING pFileNameU
);
775 PDEVICE_EXTENSION pVCB
,
777 PUNICODE_STRING LongName
,
778 PUNICODE_STRING ShortName
,
791 PDEVICE_EXTENSION pVCB
,
796 PDEVICE_EXTENSION pVCB
,
800 vfatGrabFCBFromTable(
801 PDEVICE_EXTENSION pDeviceExt
,
802 PUNICODE_STRING pFileNameU
);
806 PDEVICE_EXTENSION pVCB
);
810 PDEVICE_EXTENSION pVCB
);
821 vfatAttachFCBToFileObject(
822 PDEVICE_EXTENSION vcb
,
824 PFILE_OBJECT fileObject
);
828 PDEVICE_EXTENSION pVCB
,
830 PUNICODE_STRING FileToFindU
,
835 PDEVICE_EXTENSION pVCB
,
836 PVFATFCB
*pParentFCB
,
838 PUNICODE_STRING pFileNameU
);
841 vfatMakeFCBFromDirEntry(
843 PVFATFCB directoryFCB
,
844 PVFAT_DIRENTRY_CONTEXT DirContext
,
850 VfatQueryInformation(
851 PVFAT_IRP_CONTEXT IrpContext
);
855 PVFAT_IRP_CONTEXT IrpContext
);
858 VfatSetAllocationSizeInformation(
859 PFILE_OBJECT FileObject
,
861 PDEVICE_EXTENSION DeviceExt
,
862 PLARGE_INTEGER AllocationSize
);
868 PVFAT_IRP_CONTEXT IrpContext
);
872 PDEVICE_EXTENSION DeviceExt
,
878 VfatFileSystemControl(
879 PVFAT_IRP_CONTEXT IrpContext
);
886 PDRIVER_OBJECT DriverObject
,
887 PUNICODE_STRING RegistryPath
);
894 PVFAT_IRP_CONTEXT IrpContext
);
897 VfatAllocateIrpContext(
898 PDEVICE_OBJECT DeviceObject
,
903 PVFAT_IRP_CONTEXT IrpContext
);
911 PDEVICE_OBJECT DeviceObject
,
928 PVFAT_IRP_CONTEXT IrpContext
);
934 PVFAT_IRP_CONTEXT IrpContext
);
938 PVFAT_IRP_CONTEXT IrpContext
);
942 PDEVICE_EXTENSION DeviceExt
,
944 PULONG CurrentCluster
,
955 PDEVICE_OBJECT DeviceObject
,
962 PUNICODE_STRING PathNameU
,
963 PUNICODE_STRING DirNameU
,
964 PUNICODE_STRING FileNameU
);
978 VfatQueryVolumeInformation(
979 PVFAT_IRP_CONTEXT IrpContext
);
982 VfatSetVolumeInformation(
983 PVFAT_IRP_CONTEXT IrpContext
);
985 #endif /* _FASTFAT_PCH_ */