7 #include <pseh/pseh2.h>
9 #define USE_ROS_CC_AND_FS
12 #define ENABLE_SWAPOUT
16 #define ROUND_DOWN(n, align) \
17 (((ULONG)n) & ~((align) - 1l))
19 #define ROUND_UP(n, align) \
20 ROUND_DOWN(((ULONG)n) + (align) - 1, (align))
25 unsigned char magic0
, res0
, magic1
;
26 unsigned char OEMName
[8];
27 unsigned short BytesPerSector
;
28 unsigned char SectorsPerCluster
;
29 unsigned short ReservedSectors
;
30 unsigned char FATCount
;
31 unsigned short RootEntries
, Sectors
;
33 unsigned short FATSectors
, SectorsPerTrack
, Heads
;
34 unsigned long HiddenSectors
, SectorsHuge
;
35 unsigned char Drive
, Res1
, Sig
;
36 unsigned long VolumeID
;
37 unsigned char VolumeLabel
[11], SysType
[8];
38 unsigned char Res2
[448];
39 unsigned short Signatur1
;
44 unsigned char magic0
, res0
, magic1
; // 0
45 unsigned char OEMName
[8]; // 3
46 unsigned short BytesPerSector
; // 11
47 unsigned char SectorsPerCluster
; // 13
48 unsigned short ReservedSectors
; // 14
49 unsigned char FATCount
; // 16
50 unsigned short RootEntries
, Sectors
; // 17
51 unsigned char Media
; // 21
52 unsigned short FATSectors
, SectorsPerTrack
, Heads
; // 22
53 unsigned long HiddenSectors
, SectorsHuge
; // 28
54 unsigned long FATSectors32
; // 36
55 unsigned short ExtFlag
; // 40
56 unsigned short FSVersion
; // 42
57 unsigned long RootCluster
; // 44
58 unsigned short FSInfoSector
; // 48
59 unsigned short BootBackup
; // 50
60 unsigned char Res3
[12]; // 52
61 unsigned char Drive
; // 64
62 unsigned char Res4
; // 65
63 unsigned char ExtBootSignature
; // 66
64 unsigned long VolumeID
; // 67
65 unsigned char VolumeLabel
[11], SysType
[8]; // 71
66 unsigned char Res2
[420]; // 90
67 unsigned short Signature1
; // 510
70 struct _BootSectorFatX
72 unsigned char SysType
[4]; // 0
73 unsigned long VolumeID
; // 4
74 unsigned long SectorsPerCluster
; // 8
75 unsigned short FATCount
; // 12
76 unsigned long Unknown
; // 14
77 unsigned char Unused
[4078]; // 18
82 unsigned long ExtBootSignature2
; // 0
83 unsigned char Res6
[480]; // 4
84 unsigned long FSINFOSignature
; // 484
85 unsigned long FreeCluster
; // 488
86 unsigned long NextCluster
; // 492
87 unsigned char Res7
[12]; // 496
88 unsigned long Signatur2
; // 508
91 typedef struct _BootSector BootSector
;
97 struct { unsigned char Filename
[8], Ext
[3]; };
98 unsigned char ShortName
[11];
100 unsigned char Attrib
;
102 unsigned char CreationTimeMs
;
103 unsigned short CreationTime
,CreationDate
,AccessDate
;
106 unsigned short FirstClusterHigh
; // FAT32
107 unsigned short ExtendedAttributes
; // FAT12/FAT16
109 unsigned short UpdateTime
; //time create/update
110 unsigned short UpdateDate
; //date create/update
111 unsigned short FirstCluster
;
112 unsigned long FileSize
;
115 #define FAT_EAFILE "EA DATA. SF"
117 typedef struct _EAFileHeader FAT_EA_FILE_HEADER
, *PFAT_EA_FILE_HEADER
;
121 unsigned short Signature
; // ED
122 unsigned short Unknown
[15];
123 unsigned short EASetTable
[240];
126 typedef struct _EASetHeader FAT_EA_SET_HEADER
, *PFAT_EA_SET_HEADER
;
130 unsigned short Signature
; // EA
131 unsigned short Offset
; // relative offset, same value as in the EASetTable
132 unsigned short Unknown1
[2];
133 char TargetFileName
[12];
134 unsigned short Unknown2
[3];
135 unsigned int EALength
;
139 typedef struct _EAHeader FAT_EA_HEADER
, *PFAT_EA_HEADER
;
143 unsigned char Unknown
;
144 unsigned char EANameLength
;
145 unsigned short EAValueLength
;
150 typedef struct _FATDirEntry FAT_DIR_ENTRY
, *PFAT_DIR_ENTRY
;
154 unsigned char FilenameLength
; // 0
155 unsigned char Attrib
; // 1
156 unsigned char Filename
[42]; // 2
157 unsigned long FirstCluster
; // 44
158 unsigned long FileSize
; // 48
159 unsigned short UpdateTime
; // 52
160 unsigned short UpdateDate
; // 54
161 unsigned short CreationTime
; // 56
162 unsigned short CreationDate
; // 58
163 unsigned short AccessTime
; // 60
164 unsigned short AccessDate
; // 62
169 unsigned char id
; // sequence number for slot
170 WCHAR name0_4
[5]; // first 5 characters in name
171 unsigned char attr
; // attribute byte
172 unsigned char reserved
; // always 0
173 unsigned char alias_checksum
; // checksum for 8.3 alias
174 WCHAR name5_10
[6]; // 6 more characters in name
175 unsigned char start
[2]; // starting cluster number
176 WCHAR name11_12
[2]; // last 2 characters in name
179 typedef struct _slot slot
;
183 #define VFAT_CASE_LOWER_BASE 8 // base is lower case
184 #define VFAT_CASE_LOWER_EXT 16 // extension is lower case
186 #define LONGNAME_MAX_LENGTH 256 // max length for a long filename
188 #define ENTRY_DELETED(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_DELETED(&((DirEntry)->FatX)) : FAT_ENTRY_DELETED(&((DirEntry)->Fat)))
189 #define ENTRY_VOLUME(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_VOLUME(&((DirEntry)->FatX)) : FAT_ENTRY_VOLUME(&((DirEntry)->Fat)))
190 #define ENTRY_END(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_END(&((DirEntry)->FatX)) : FAT_ENTRY_END(&((DirEntry)->Fat)))
192 #define FAT_ENTRY_DELETED(DirEntry) ((DirEntry)->Filename[0] == 0xe5)
193 #define FAT_ENTRY_END(DirEntry) ((DirEntry)->Filename[0] == 0)
194 #define FAT_ENTRY_LONG(DirEntry) (((DirEntry)->Attrib & 0x3f) == 0x0f)
195 #define FAT_ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
197 #define FATX_ENTRY_DELETED(DirEntry) ((DirEntry)->FilenameLength == 0xe5)
198 #define FATX_ENTRY_END(DirEntry) ((DirEntry)->FilenameLength == 0xff)
199 #define FATX_ENTRY_LONG(DirEntry) (FALSE)
200 #define FATX_ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
202 #define FAT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof (FAT_DIR_ENTRY))
203 #define FATX_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof (FATX_DIR_ENTRY))
205 typedef struct _FATXDirEntry FATX_DIR_ENTRY
, *PFATX_DIR_ENTRY
;
213 typedef union _DIR_ENTRY DIR_ENTRY
, *PDIR_ENTRY
;
215 #define BLOCKSIZE 512
223 #define VCB_VOLUME_LOCKED 0x0001
224 #define VCB_DISMOUNT_PENDING 0x0002
225 #define VCB_IS_FATX 0x0004
226 #define VCB_IS_DIRTY 0x4000 /* Volume is dirty */
227 #define VCB_CLEAR_DIRTY 0x8000 /* Clean dirty flag at shutdown */
235 ULONG rootDirectorySectors
;
239 ULONG SectorsPerCluster
;
240 ULONG BytesPerSector
;
241 ULONG BytesPerCluster
;
242 ULONG NumberOfClusters
;
246 } FATINFO
, *PFATINFO
;
249 struct _VFAT_DIRENTRY_CONTEXT
;
251 typedef struct _HASHENTRY
254 struct _VFATFCB
* self
;
255 struct _HASHENTRY
* next
;
259 #define FCB_HASH_TABLE_SIZE 65536
261 typedef struct DEVICE_EXTENSION
*PDEVICE_EXTENSION
;
263 typedef NTSTATUS (*PGET_NEXT_CLUSTER
)(PDEVICE_EXTENSION
,ULONG
,PULONG
);
264 typedef NTSTATUS (*PFIND_AND_MARK_AVAILABLE_CLUSTER
)(PDEVICE_EXTENSION
,PULONG
);
265 typedef NTSTATUS (*PWRITE_CLUSTER
)(PDEVICE_EXTENSION
,ULONG
,ULONG
,PULONG
);
267 typedef NTSTATUS (*PGET_NEXT_DIR_ENTRY
)(PVOID
*,PVOID
*,struct _VFATFCB
*,struct _VFAT_DIRENTRY_CONTEXT
*,BOOLEAN
);
269 typedef struct DEVICE_EXTENSION
271 ERESOURCE DirResource
;
272 ERESOURCE FatResource
;
274 KSPIN_LOCK FcbListLock
;
275 LIST_ENTRY FcbListHead
;
277 struct _HASHENTRY
**FcbHashTable
;
279 PDEVICE_OBJECT VolumeDevice
;
280 PDEVICE_OBJECT StorageDevice
;
281 PFILE_OBJECT FATFileObject
;
283 ULONG LastAvailableCluster
;
284 ULONG AvailableClusters
;
285 BOOLEAN AvailableClustersValid
;
287 struct _VFATFCB
*VolumeFcb
;
289 /* Pointers to functions for manipulating FAT. */
290 PGET_NEXT_CLUSTER GetNextCluster
;
291 PFIND_AND_MARK_AVAILABLE_CLUSTER FindAndMarkAvailableCluster
;
292 PWRITE_CLUSTER WriteCluster
;
293 ULONG CleanShutBitMask
;
295 /* Pointers to functions for manipulating directory entries. */
296 PGET_NEXT_DIR_ENTRY GetNextDirEntry
;
300 LIST_ENTRY VolumeListEntry
;
303 LIST_ENTRY NotifyList
;
304 PNOTIFY_SYNC NotifySync
;
306 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLOSE */
307 ULONG OpenHandleCount
;
309 /* VPBs for dismount */
312 } DEVICE_EXTENSION
, VCB
, *PVCB
;
316 PDRIVER_OBJECT DriverObject
;
317 PDEVICE_OBJECT DeviceObject
;
319 ERESOURCE VolumeListLock
;
320 LIST_ENTRY VolumeListHead
;
321 NPAGED_LOOKASIDE_LIST FcbLookasideList
;
322 NPAGED_LOOKASIDE_LIST CcbLookasideList
;
323 NPAGED_LOOKASIDE_LIST IrpContextLookasideList
;
324 FAST_IO_DISPATCH FastIoDispatch
;
325 CACHE_MANAGER_CALLBACKS CacheMgrCallbacks
;
326 } VFAT_GLOBAL_DATA
, *PVFAT_GLOBAL_DATA
;
328 extern PVFAT_GLOBAL_DATA VfatGlobalData
;
330 #define FCB_CACHE_INITIALIZED 0x0001
331 #define FCB_DELETE_PENDING 0x0002
332 #define FCB_IS_FAT 0x0004
333 #define FCB_IS_PAGE_FILE 0x0008
334 #define FCB_IS_VOLUME 0x0010
335 #define FCB_IS_DIRTY 0x0020
336 #define FCB_IS_FATX_ENTRY 0x0040
338 typedef struct _VFATFCB
340 /* FCB header required by ROS/NT */
341 FSRTL_COMMON_FCB_HEADER RFCB
;
342 SECTION_OBJECT_POINTERS SectionObjectPointers
;
343 ERESOURCE MainResource
;
344 ERESOURCE PagingIoResource
;
345 /* end FCB header required by ROS/NT */
347 /* directory entry for this file or directory */
350 /* Pointer to attributes in entry */
353 /* long file name, points into PathNameBuffer */
354 UNICODE_STRING LongNameU
;
356 /* short file name */
357 UNICODE_STRING ShortNameU
;
359 /* directory name, points into PathNameBuffer */
360 UNICODE_STRING DirNameU
;
362 /* path + long file name 260 max*/
363 UNICODE_STRING PathNameU
;
365 /* buffer for PathNameU */
366 PWCHAR PathNameBuffer
;
368 /* buffer for ShortNameU */
369 WCHAR ShortNameBuffer
[13];
374 /* List of FCB's for this volume */
375 LIST_ENTRY FcbListEntry
;
377 /* pointer to the parent fcb */
378 struct _VFATFCB
*parentFcb
;
380 /* Flags for the fcb */
383 /* pointer to the file object which has initialized the fcb */
384 PFILE_OBJECT FileObject
;
386 /* Directory index for the short name entry */
389 /* Directory index where the long name starts */
392 /* Share access for the file object */
393 SHARE_ACCESS FCBShareAccess
;
395 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLEANUP */
396 ULONG OpenHandleCount
;
398 /* Entry into the hash table for the path + long name */
401 /* Entry into the hash table for the path + short name */
404 /* List of byte-range locks for this file */
408 * Optimalization: caching of last read/write cluster+offset pair. Can't
409 * be in VFATCCB because it must be reset everytime the allocated clusters
412 FAST_MUTEX LastMutex
;
415 } VFATFCB
, *PVFATFCB
;
417 typedef struct _VFATCCB
419 LARGE_INTEGER CurrentByteOffset
;
420 /* for DirectoryControl */
422 /* for DirectoryControl */
423 UNICODE_STRING SearchPattern
;
424 } VFATCCB
, *PVFATCCB
;
426 #define TAG_CCB 'BCCV'
427 #define TAG_FCB 'BCFV'
428 #define TAG_IRP 'PRIV'
429 #define TAG_VFAT 'TAFV'
431 #define ENTRIES_PER_SECTOR (BLOCKSIZE / sizeof(FATDirEntry))
433 typedef struct __DOSTIME
441 typedef struct __DOSDATE
449 #define IRPCONTEXT_CANWAIT 0x0001
450 #define IRPCONTEXT_PENDINGRETURNED 0x0002
455 PDEVICE_OBJECT DeviceObject
;
456 PDEVICE_EXTENSION DeviceExt
;
458 WORK_QUEUE_ITEM WorkQueueItem
;
459 PIO_STACK_LOCATION Stack
;
462 PFILE_OBJECT FileObject
;
465 } VFAT_IRP_CONTEXT
, *PVFAT_IRP_CONTEXT
;
467 typedef struct _VFAT_DIRENTRY_CONTEXT
472 UNICODE_STRING LongNameU
;
473 UNICODE_STRING ShortNameU
;
474 } VFAT_DIRENTRY_CONTEXT
, *PVFAT_DIRENTRY_CONTEXT
;
476 typedef struct _VFAT_MOVE_CONTEXT
482 } VFAT_MOVE_CONTEXT
, *PVFAT_MOVE_CONTEXT
;
488 IN PDEVICE_OBJECT pDeviceObject
,
489 IN PLARGE_INTEGER ReadOffset
,
492 IN BOOLEAN Override
);
496 IN PVFAT_IRP_CONTEXT IrpContext
,
497 IN PLARGE_INTEGER ReadOffset
,
499 IN ULONG BufferOffset
,
503 VfatWriteDiskPartial(
504 IN PVFAT_IRP_CONTEXT IrpContext
,
505 IN PLARGE_INTEGER WriteOffset
,
506 IN ULONG WriteLength
,
507 IN ULONG BufferOffset
,
511 VfatBlockDeviceIoControl(
512 IN PDEVICE_OBJECT DeviceObject
,
514 IN PVOID InputBuffer
,
515 IN ULONG InputBufferSize
,
516 IN OUT PVOID OutputBuffer
,
517 IN OUT PULONG pOutputBufferSize
,
518 IN BOOLEAN Override
);
524 PVFAT_IRP_CONTEXT IrpContext
);
530 PVFAT_IRP_CONTEXT IrpContext
);
534 PDEVICE_EXTENSION DeviceExt
,
535 PFILE_OBJECT FileObject
);
541 PVFAT_IRP_CONTEXT IrpContext
);
545 PDEVICE_EXTENSION DeviceExt
,
547 PUNICODE_STRING FileToFindU
,
548 PVFAT_DIRENTRY_CONTEXT DirContext
,
553 PFAT_DIR_ENTRY pEntry
,
554 PUNICODE_STRING NameU
);
558 PDEVICE_EXTENSION DeviceExt
,
564 VfatDirectoryControl(
565 PVFAT_IRP_CONTEXT IrpContext
);
568 FsdDosDateTimeToSystemTime(
569 PDEVICE_EXTENSION DeviceExt
,
572 PLARGE_INTEGER SystemTime
);
575 FsdSystemTimeToDosDateTime(
576 PDEVICE_EXTENSION DeviceExt
,
577 PLARGE_INTEGER SystemTime
,
584 vfatDirEntryGetFirstCluster(
585 PDEVICE_EXTENSION pDeviceExt
,
586 PDIR_ENTRY pDirEntry
);
589 VfatIsDirectoryEmpty(
597 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
605 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
612 PDEVICE_EXTENSION DeviceExt
,
613 PUNICODE_STRING PathNameU
,
616 ULONG RequestedOptions
,
618 PVFAT_MOVE_CONTEXT MoveContext
);
632 PDEVICE_EXTENSION DeviceExt
,
639 IN PDEVICE_EXTENSION DeviceExt
,
641 IN PUNICODE_STRING FileName
,
642 IN BOOLEAN CaseChangeOnly
);
646 IN PDEVICE_EXTENSION DeviceExt
,
648 IN PUNICODE_STRING FileName
,
649 IN PVFATFCB ParentFcb
);
654 VfatSetExtendedAttributes(
655 PFILE_OBJECT FileObject
,
662 VfatInitFastIoRoutines(
663 PFAST_IO_DISPATCH FastIoDispatch
);
667 VfatAcquireForLazyWrite(
673 VfatReleaseFromLazyWrite(
678 VfatAcquireForReadAhead(
684 VfatReleaseFromReadAhead(
691 PDEVICE_EXTENSION DeviceExt
,
692 ULONG CurrentCluster
,
696 FAT12FindAndMarkAvailableCluster(
697 PDEVICE_EXTENSION DeviceExt
,
702 PDEVICE_EXTENSION DeviceExt
,
703 ULONG ClusterToWrite
,
709 PDEVICE_EXTENSION DeviceExt
,
710 ULONG CurrentCluster
,
714 FAT16FindAndMarkAvailableCluster(
715 PDEVICE_EXTENSION DeviceExt
,
720 PDEVICE_EXTENSION DeviceExt
,
721 ULONG ClusterToWrite
,
727 PDEVICE_EXTENSION DeviceExt
,
728 ULONG CurrentCluster
,
732 FAT32FindAndMarkAvailableCluster(
733 PDEVICE_EXTENSION DeviceExt
,
738 PDEVICE_EXTENSION DeviceExt
,
739 ULONG ClusterToWrite
,
745 PDEVICE_EXTENSION DeviceExt
,
753 PDEVICE_EXTENSION DeviceExt
,
758 PDEVICE_EXTENSION DeviceExt
,
759 ULONG CurrentCluster
,
763 GetNextClusterExtend(
764 PDEVICE_EXTENSION DeviceExt
,
765 ULONG CurrentCluster
,
769 CountAvailableClusters(
770 PDEVICE_EXTENSION DeviceExt
,
771 PLARGE_INTEGER Clusters
);
775 PDEVICE_EXTENSION DeviceExt
,
776 ULONG ClusterToWrite
,
783 PDEVICE_EXTENSION pVCB
,
784 PUNICODE_STRING pFileNameU
);
788 PDEVICE_EXTENSION pVCB
,
790 PUNICODE_STRING LongName
,
791 PUNICODE_STRING ShortName
,
804 PDEVICE_EXTENSION pVCB
,
809 PDEVICE_EXTENSION pVCB
,
813 vfatGrabFCBFromTable(
814 PDEVICE_EXTENSION pDeviceExt
,
815 PUNICODE_STRING pFileNameU
);
819 PDEVICE_EXTENSION pVCB
);
823 PDEVICE_EXTENSION pVCB
);
834 vfatAttachFCBToFileObject(
835 PDEVICE_EXTENSION vcb
,
837 PFILE_OBJECT fileObject
);
841 PDEVICE_EXTENSION pVCB
,
843 PUNICODE_STRING FileToFindU
,
848 PDEVICE_EXTENSION pVCB
,
849 PVFATFCB
*pParentFCB
,
851 PUNICODE_STRING pFileNameU
);
854 vfatMakeFCBFromDirEntry(
856 PVFATFCB directoryFCB
,
857 PVFAT_DIRENTRY_CONTEXT DirContext
,
863 VfatQueryInformation(
864 PVFAT_IRP_CONTEXT IrpContext
);
868 PVFAT_IRP_CONTEXT IrpContext
);
871 VfatSetAllocationSizeInformation(
872 PFILE_OBJECT FileObject
,
874 PDEVICE_EXTENSION DeviceExt
,
875 PLARGE_INTEGER AllocationSize
);
881 PVFAT_IRP_CONTEXT IrpContext
);
885 PDEVICE_EXTENSION DeviceExt
,
891 VfatFileSystemControl(
892 PVFAT_IRP_CONTEXT IrpContext
);
899 PDRIVER_OBJECT DriverObject
,
900 PUNICODE_STRING RegistryPath
);
907 PVFAT_IRP_CONTEXT IrpContext
);
910 VfatAllocateIrpContext(
911 PDEVICE_OBJECT DeviceObject
,
916 PVFAT_IRP_CONTEXT IrpContext
);
924 PDEVICE_OBJECT DeviceObject
,
938 VfatCheckForDismount(
939 IN PDEVICE_EXTENSION DeviceExt
,
946 PVFAT_IRP_CONTEXT IrpContext
);
952 PVFAT_IRP_CONTEXT IrpContext
);
956 PVFAT_IRP_CONTEXT IrpContext
);
960 PDEVICE_EXTENSION DeviceExt
,
962 PULONG CurrentCluster
,
973 PDEVICE_OBJECT DeviceObject
,
980 PUNICODE_STRING PathNameU
,
981 PUNICODE_STRING DirNameU
,
982 PUNICODE_STRING FileNameU
);
996 VfatQueryVolumeInformation(
997 PVFAT_IRP_CONTEXT IrpContext
);
1000 VfatSetVolumeInformation(
1001 PVFAT_IRP_CONTEXT IrpContext
);
1003 #endif /* _FASTFAT_PCH_ */