7 #include <pseh/pseh2.h>
10 #define INIT_SECTION __attribute__((section ("INIT")))
12 #define INIT_SECTION /* Done via alloc_text for MSC */
15 #define USE_ROS_CC_AND_FS
18 #define ENABLE_SWAPOUT
22 #define ROUND_DOWN(n, align) \
23 (((ULONG)n) & ~((align) - 1l))
25 #define ROUND_UP(n, align) \
26 ROUND_DOWN(((ULONG)n) + (align) - 1, (align))
28 #define ROUND_DOWN_64(n, align) \
29 (((ULONGLONG)n) & ~((align) - 1LL))
31 #define ROUND_UP_64(n, align) \
32 ROUND_DOWN_64(((ULONGLONG)n) + (align) - 1LL, (align))
37 unsigned char magic0
, res0
, magic1
;
38 unsigned char OEMName
[8];
39 unsigned short BytesPerSector
;
40 unsigned char SectorsPerCluster
;
41 unsigned short ReservedSectors
;
42 unsigned char FATCount
;
43 unsigned short RootEntries
, Sectors
;
45 unsigned short FATSectors
, SectorsPerTrack
, Heads
;
46 unsigned long HiddenSectors
, SectorsHuge
;
47 unsigned char Drive
, Res1
, Sig
;
48 unsigned long VolumeID
;
49 unsigned char VolumeLabel
[11], SysType
[8];
50 unsigned char Res2
[448];
51 unsigned short Signatur1
;
56 unsigned char magic0
, res0
, magic1
; // 0
57 unsigned char OEMName
[8]; // 3
58 unsigned short BytesPerSector
; // 11
59 unsigned char SectorsPerCluster
; // 13
60 unsigned short ReservedSectors
; // 14
61 unsigned char FATCount
; // 16
62 unsigned short RootEntries
, Sectors
; // 17
63 unsigned char Media
; // 21
64 unsigned short FATSectors
, SectorsPerTrack
, Heads
; // 22
65 unsigned long HiddenSectors
, SectorsHuge
; // 28
66 unsigned long FATSectors32
; // 36
67 unsigned short ExtFlag
; // 40
68 unsigned short FSVersion
; // 42
69 unsigned long RootCluster
; // 44
70 unsigned short FSInfoSector
; // 48
71 unsigned short BootBackup
; // 50
72 unsigned char Res3
[12]; // 52
73 unsigned char Drive
; // 64
74 unsigned char Res4
; // 65
75 unsigned char ExtBootSignature
; // 66
76 unsigned long VolumeID
; // 67
77 unsigned char VolumeLabel
[11], SysType
[8]; // 71
78 unsigned char Res2
[420]; // 90
79 unsigned short Signature1
; // 510
82 struct _BootSectorFatX
84 unsigned char SysType
[4]; // 0
85 unsigned long VolumeID
; // 4
86 unsigned long SectorsPerCluster
; // 8
87 unsigned short FATCount
; // 12
88 unsigned long Unknown
; // 14
89 unsigned char Unused
[4078]; // 18
94 unsigned long ExtBootSignature2
; // 0
95 unsigned char Res6
[480]; // 4
96 unsigned long FSINFOSignature
; // 484
97 unsigned long FreeCluster
; // 488
98 unsigned long NextCluster
; // 492
99 unsigned char Res7
[12]; // 496
100 unsigned long Signatur2
; // 508
103 typedef struct _BootSector BootSector
;
109 struct { unsigned char Filename
[8], Ext
[3]; };
110 unsigned char ShortName
[11];
112 unsigned char Attrib
;
114 unsigned char CreationTimeMs
;
115 unsigned short CreationTime
,CreationDate
,AccessDate
;
118 unsigned short FirstClusterHigh
; // FAT32
119 unsigned short ExtendedAttributes
; // FAT12/FAT16
121 unsigned short UpdateTime
; //time create/update
122 unsigned short UpdateDate
; //date create/update
123 unsigned short FirstCluster
;
124 unsigned long FileSize
;
127 #define FAT_EAFILE "EA DATA. SF"
129 typedef struct _EAFileHeader FAT_EA_FILE_HEADER
, *PFAT_EA_FILE_HEADER
;
133 unsigned short Signature
; // ED
134 unsigned short Unknown
[15];
135 unsigned short EASetTable
[240];
138 typedef struct _EASetHeader FAT_EA_SET_HEADER
, *PFAT_EA_SET_HEADER
;
142 unsigned short Signature
; // EA
143 unsigned short Offset
; // relative offset, same value as in the EASetTable
144 unsigned short Unknown1
[2];
145 char TargetFileName
[12];
146 unsigned short Unknown2
[3];
147 unsigned int EALength
;
151 typedef struct _EAHeader FAT_EA_HEADER
, *PFAT_EA_HEADER
;
155 unsigned char Unknown
;
156 unsigned char EANameLength
;
157 unsigned short EAValueLength
;
162 typedef struct _FATDirEntry FAT_DIR_ENTRY
, *PFAT_DIR_ENTRY
;
166 unsigned char FilenameLength
; // 0
167 unsigned char Attrib
; // 1
168 unsigned char Filename
[42]; // 2
169 unsigned long FirstCluster
; // 44
170 unsigned long FileSize
; // 48
171 unsigned short UpdateTime
; // 52
172 unsigned short UpdateDate
; // 54
173 unsigned short CreationTime
; // 56
174 unsigned short CreationDate
; // 58
175 unsigned short AccessTime
; // 60
176 unsigned short AccessDate
; // 62
181 unsigned char id
; // sequence number for slot
182 WCHAR name0_4
[5]; // first 5 characters in name
183 unsigned char attr
; // attribute byte
184 unsigned char reserved
; // always 0
185 unsigned char alias_checksum
; // checksum for 8.3 alias
186 WCHAR name5_10
[6]; // 6 more characters in name
187 unsigned char start
[2]; // starting cluster number
188 WCHAR name11_12
[2]; // last 2 characters in name
191 typedef struct _slot slot
;
195 #define VFAT_CASE_LOWER_BASE 8 // base is lower case
196 #define VFAT_CASE_LOWER_EXT 16 // extension is lower case
198 #define LONGNAME_MAX_LENGTH 256 // max length for a long filename
200 #define ENTRY_DELETED(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_DELETED(&((DirEntry)->FatX)) : FAT_ENTRY_DELETED(&((DirEntry)->Fat)))
201 #define ENTRY_VOLUME(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_VOLUME(&((DirEntry)->FatX)) : FAT_ENTRY_VOLUME(&((DirEntry)->Fat)))
202 #define ENTRY_END(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_END(&((DirEntry)->FatX)) : FAT_ENTRY_END(&((DirEntry)->Fat)))
204 #define FAT_ENTRY_DELETED(DirEntry) ((DirEntry)->Filename[0] == 0xe5)
205 #define FAT_ENTRY_END(DirEntry) ((DirEntry)->Filename[0] == 0)
206 #define FAT_ENTRY_LONG(DirEntry) (((DirEntry)->Attrib & 0x3f) == 0x0f)
207 #define FAT_ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
209 #define FATX_ENTRY_DELETED(DirEntry) ((DirEntry)->FilenameLength == 0xe5)
210 #define FATX_ENTRY_END(DirEntry) ((DirEntry)->FilenameLength == 0xff)
211 #define FATX_ENTRY_LONG(DirEntry) (FALSE)
212 #define FATX_ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
214 #define FAT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof (FAT_DIR_ENTRY))
215 #define FATX_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof (FATX_DIR_ENTRY))
217 typedef struct _FATXDirEntry FATX_DIR_ENTRY
, *PFATX_DIR_ENTRY
;
225 typedef union _DIR_ENTRY DIR_ENTRY
, *PDIR_ENTRY
;
227 #define BLOCKSIZE 512
235 #define VCB_VOLUME_LOCKED 0x0001
236 #define VCB_DISMOUNT_PENDING 0x0002
237 #define VCB_IS_FATX 0x0004
238 #define VCB_IS_DIRTY 0x4000 /* Volume is dirty */
239 #define VCB_CLEAR_DIRTY 0x8000 /* Clean dirty flag at shutdown */
247 ULONG rootDirectorySectors
;
251 ULONG SectorsPerCluster
;
252 ULONG BytesPerSector
;
253 ULONG BytesPerCluster
;
254 ULONG NumberOfClusters
;
258 } FATINFO
, *PFATINFO
;
261 struct _VFAT_DIRENTRY_CONTEXT
;
263 typedef struct _HASHENTRY
266 struct _VFATFCB
* self
;
267 struct _HASHENTRY
* next
;
271 typedef struct DEVICE_EXTENSION
*PDEVICE_EXTENSION
;
273 typedef NTSTATUS (*PGET_NEXT_CLUSTER
)(PDEVICE_EXTENSION
,ULONG
,PULONG
);
274 typedef NTSTATUS (*PFIND_AND_MARK_AVAILABLE_CLUSTER
)(PDEVICE_EXTENSION
,PULONG
);
275 typedef NTSTATUS (*PWRITE_CLUSTER
)(PDEVICE_EXTENSION
,ULONG
,ULONG
,PULONG
);
277 typedef NTSTATUS (*PGET_NEXT_DIR_ENTRY
)(PVOID
*,PVOID
*,struct _VFATFCB
*,struct _VFAT_DIRENTRY_CONTEXT
*,BOOLEAN
);
279 typedef struct DEVICE_EXTENSION
281 ERESOURCE DirResource
;
282 ERESOURCE FatResource
;
284 KSPIN_LOCK FcbListLock
;
285 LIST_ENTRY FcbListHead
;
287 struct _HASHENTRY
**FcbHashTable
;
289 PDEVICE_OBJECT VolumeDevice
;
290 PDEVICE_OBJECT StorageDevice
;
291 PFILE_OBJECT FATFileObject
;
293 ULONG LastAvailableCluster
;
294 ULONG AvailableClusters
;
295 BOOLEAN AvailableClustersValid
;
297 struct _VFATFCB
*VolumeFcb
;
299 /* Pointers to functions for manipulating FAT. */
300 PGET_NEXT_CLUSTER GetNextCluster
;
301 PFIND_AND_MARK_AVAILABLE_CLUSTER FindAndMarkAvailableCluster
;
302 PWRITE_CLUSTER WriteCluster
;
303 ULONG CleanShutBitMask
;
305 /* Pointers to functions for manipulating directory entries. */
306 PGET_NEXT_DIR_ENTRY GetNextDirEntry
;
310 LIST_ENTRY VolumeListEntry
;
313 LIST_ENTRY NotifyList
;
314 PNOTIFY_SYNC NotifySync
;
316 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLOSE */
317 ULONG OpenHandleCount
;
319 /* VPBs for dismount */
322 } DEVICE_EXTENSION
, VCB
, *PVCB
;
324 #define VFAT_BREAK_ON_CORRUPTION 1
328 PDRIVER_OBJECT DriverObject
;
329 PDEVICE_OBJECT DeviceObject
;
331 ERESOURCE VolumeListLock
;
332 LIST_ENTRY VolumeListHead
;
333 NPAGED_LOOKASIDE_LIST FcbLookasideList
;
334 NPAGED_LOOKASIDE_LIST CcbLookasideList
;
335 NPAGED_LOOKASIDE_LIST IrpContextLookasideList
;
336 FAST_IO_DISPATCH FastIoDispatch
;
337 CACHE_MANAGER_CALLBACKS CacheMgrCallbacks
;
338 } VFAT_GLOBAL_DATA
, *PVFAT_GLOBAL_DATA
;
340 extern PVFAT_GLOBAL_DATA VfatGlobalData
;
342 #define FCB_CACHE_INITIALIZED 0x0001
343 #define FCB_DELETE_PENDING 0x0002
344 #define FCB_IS_FAT 0x0004
345 #define FCB_IS_PAGE_FILE 0x0008
346 #define FCB_IS_VOLUME 0x0010
347 #define FCB_IS_DIRTY 0x0020
348 #define FCB_IS_FATX_ENTRY 0x0040
350 typedef struct _VFATFCB
352 /* FCB header required by ROS/NT */
353 FSRTL_COMMON_FCB_HEADER RFCB
;
354 SECTION_OBJECT_POINTERS SectionObjectPointers
;
355 ERESOURCE MainResource
;
356 ERESOURCE PagingIoResource
;
357 /* end FCB header required by ROS/NT */
359 /* directory entry for this file or directory */
362 /* Pointer to attributes in entry */
365 /* long file name, points into PathNameBuffer */
366 UNICODE_STRING LongNameU
;
368 /* short file name */
369 UNICODE_STRING ShortNameU
;
371 /* directory name, points into PathNameBuffer */
372 UNICODE_STRING DirNameU
;
374 /* path + long file name 260 max*/
375 UNICODE_STRING PathNameU
;
377 /* buffer for PathNameU */
378 PWCHAR PathNameBuffer
;
380 /* buffer for ShortNameU */
381 WCHAR ShortNameBuffer
[13];
386 /* List of FCB's for this volume */
387 LIST_ENTRY FcbListEntry
;
389 /* List of FCB's for the parent */
390 LIST_ENTRY ParentListEntry
;
392 /* pointer to the parent fcb */
393 struct _VFATFCB
*parentFcb
;
395 /* List for the children */
396 LIST_ENTRY ParentListHead
;
398 /* Flags for the fcb */
401 /* pointer to the file object which has initialized the fcb */
402 PFILE_OBJECT FileObject
;
404 /* Directory index for the short name entry */
407 /* Directory index where the long name starts */
410 /* Share access for the file object */
411 SHARE_ACCESS FCBShareAccess
;
413 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLEANUP */
414 ULONG OpenHandleCount
;
416 /* Entry into the hash table for the path + long name */
419 /* Entry into the hash table for the path + short name */
422 /* List of byte-range locks for this file */
426 * Optimalization: caching of last read/write cluster+offset pair. Can't
427 * be in VFATCCB because it must be reset everytime the allocated clusters
430 FAST_MUTEX LastMutex
;
433 } VFATFCB
, *PVFATFCB
;
435 typedef struct _VFATCCB
437 LARGE_INTEGER CurrentByteOffset
;
438 /* for DirectoryControl */
440 /* for DirectoryControl */
441 UNICODE_STRING SearchPattern
;
442 } VFATCCB
, *PVFATCCB
;
444 #define TAG_CCB 'BCCV'
445 #define TAG_FCB 'BCFV'
446 #define TAG_IRP 'PRIV'
447 #define TAG_VFAT 'TAFV'
449 #define ENTRIES_PER_SECTOR (BLOCKSIZE / sizeof(FATDirEntry))
451 typedef struct __DOSTIME
459 typedef struct __DOSDATE
467 #define IRPCONTEXT_CANWAIT 0x0001
468 #define IRPCONTEXT_COMPLETE 0x0002
469 #define IRPCONTEXT_QUEUE 0x0004
470 #define IRPCONTEXT_PENDINGRETURNED 0x0008
475 PDEVICE_OBJECT DeviceObject
;
476 PDEVICE_EXTENSION DeviceExt
;
478 WORK_QUEUE_ITEM WorkQueueItem
;
479 PIO_STACK_LOCATION Stack
;
482 PFILE_OBJECT FileObject
;
486 } VFAT_IRP_CONTEXT
, *PVFAT_IRP_CONTEXT
;
488 typedef struct _VFAT_DIRENTRY_CONTEXT
493 UNICODE_STRING LongNameU
;
494 UNICODE_STRING ShortNameU
;
495 } VFAT_DIRENTRY_CONTEXT
, *PVFAT_DIRENTRY_CONTEXT
;
497 typedef struct _VFAT_MOVE_CONTEXT
504 } VFAT_MOVE_CONTEXT
, *PVFAT_MOVE_CONTEXT
;
508 VfatMarkIrpContextForQueue(PVFAT_IRP_CONTEXT IrpContext
)
510 PULONG Flags
= &IrpContext
->Flags
;
512 *Flags
&= ~IRPCONTEXT_COMPLETE
;
513 *Flags
|= IRPCONTEXT_QUEUE
;
515 return STATUS_PENDING
;
522 IN PDEVICE_OBJECT pDeviceObject
,
523 IN PLARGE_INTEGER ReadOffset
,
526 IN BOOLEAN Override
);
530 IN PVFAT_IRP_CONTEXT IrpContext
,
531 IN PLARGE_INTEGER ReadOffset
,
533 IN ULONG BufferOffset
,
537 VfatWriteDiskPartial(
538 IN PVFAT_IRP_CONTEXT IrpContext
,
539 IN PLARGE_INTEGER WriteOffset
,
540 IN ULONG WriteLength
,
541 IN ULONG BufferOffset
,
545 VfatBlockDeviceIoControl(
546 IN PDEVICE_OBJECT DeviceObject
,
548 IN PVOID InputBuffer
,
549 IN ULONG InputBufferSize
,
550 IN OUT PVOID OutputBuffer
,
551 IN OUT PULONG pOutputBufferSize
,
552 IN BOOLEAN Override
);
558 PVFAT_IRP_CONTEXT IrpContext
);
564 PVFAT_IRP_CONTEXT IrpContext
);
568 PDEVICE_EXTENSION DeviceExt
,
569 PFILE_OBJECT FileObject
);
575 PVFAT_IRP_CONTEXT IrpContext
);
579 PDEVICE_EXTENSION DeviceExt
,
581 PUNICODE_STRING FileToFindU
,
582 PVFAT_DIRENTRY_CONTEXT DirContext
,
587 PFAT_DIR_ENTRY pEntry
,
588 PUNICODE_STRING NameU
);
592 PDEVICE_EXTENSION DeviceExt
,
598 VfatDirectoryControl(
599 PVFAT_IRP_CONTEXT IrpContext
);
602 FsdDosDateTimeToSystemTime(
603 PDEVICE_EXTENSION DeviceExt
,
606 PLARGE_INTEGER SystemTime
);
609 FsdSystemTimeToDosDateTime(
610 PDEVICE_EXTENSION DeviceExt
,
611 PLARGE_INTEGER SystemTime
,
618 vfatDirEntryGetFirstCluster(
619 PDEVICE_EXTENSION pDeviceExt
,
620 PDIR_ENTRY pDirEntry
);
623 VfatIsDirectoryEmpty(
631 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
639 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
646 PDEVICE_EXTENSION DeviceExt
,
647 PUNICODE_STRING PathNameU
,
650 ULONG RequestedOptions
,
652 PVFAT_MOVE_CONTEXT MoveContext
);
666 PDEVICE_EXTENSION DeviceExt
,
673 IN PDEVICE_EXTENSION DeviceExt
,
675 IN PUNICODE_STRING FileName
,
676 IN BOOLEAN CaseChangeOnly
);
680 IN PDEVICE_EXTENSION DeviceExt
,
682 IN PUNICODE_STRING FileName
,
683 IN PVFATFCB ParentFcb
);
688 VfatSetExtendedAttributes(
689 PFILE_OBJECT FileObject
,
696 VfatInitFastIoRoutines(
697 PFAST_IO_DISPATCH FastIoDispatch
);
701 VfatAcquireForLazyWrite(
707 VfatReleaseFromLazyWrite(
712 VfatAcquireForReadAhead(
718 VfatReleaseFromReadAhead(
725 PDEVICE_EXTENSION DeviceExt
,
726 ULONG CurrentCluster
,
730 FAT12FindAndMarkAvailableCluster(
731 PDEVICE_EXTENSION DeviceExt
,
736 PDEVICE_EXTENSION DeviceExt
,
737 ULONG ClusterToWrite
,
743 PDEVICE_EXTENSION DeviceExt
,
744 ULONG CurrentCluster
,
748 FAT16FindAndMarkAvailableCluster(
749 PDEVICE_EXTENSION DeviceExt
,
754 PDEVICE_EXTENSION DeviceExt
,
755 ULONG ClusterToWrite
,
761 PDEVICE_EXTENSION DeviceExt
,
762 ULONG CurrentCluster
,
766 FAT32FindAndMarkAvailableCluster(
767 PDEVICE_EXTENSION DeviceExt
,
772 PDEVICE_EXTENSION DeviceExt
,
773 ULONG ClusterToWrite
,
779 PDEVICE_EXTENSION DeviceExt
,
787 PDEVICE_EXTENSION DeviceExt
,
792 PDEVICE_EXTENSION DeviceExt
,
793 ULONG CurrentCluster
,
797 GetNextClusterExtend(
798 PDEVICE_EXTENSION DeviceExt
,
799 ULONG CurrentCluster
,
803 CountAvailableClusters(
804 PDEVICE_EXTENSION DeviceExt
,
805 PLARGE_INTEGER Clusters
);
809 PDEVICE_EXTENSION DeviceExt
,
810 ULONG ClusterToWrite
,
817 PDEVICE_EXTENSION pVCB
,
818 PUNICODE_STRING pFileNameU
);
822 PDEVICE_EXTENSION pVCB
,
824 PVFAT_DIRENTRY_CONTEXT DirContext
,
837 PDEVICE_EXTENSION pVCB
,
842 PDEVICE_EXTENSION pVCB
,
846 vfatGrabFCBFromTable(
847 PDEVICE_EXTENSION pDeviceExt
,
848 PUNICODE_STRING pFileNameU
);
852 PDEVICE_EXTENSION pVCB
);
856 PDEVICE_EXTENSION pVCB
);
867 vfatAttachFCBToFileObject(
868 PDEVICE_EXTENSION vcb
,
870 PFILE_OBJECT fileObject
);
874 PDEVICE_EXTENSION pVCB
,
876 PUNICODE_STRING FileToFindU
,
881 PDEVICE_EXTENSION pVCB
,
882 PVFATFCB
*pParentFCB
,
884 PUNICODE_STRING pFileNameU
);
887 vfatMakeFCBFromDirEntry(
889 PVFATFCB directoryFCB
,
890 PVFAT_DIRENTRY_CONTEXT DirContext
,
896 VfatGetStandardInformation(
898 PFILE_STANDARD_INFORMATION StandardInfo
,
899 PULONG BufferLength
);
902 VfatGetBasicInformation(
903 PFILE_OBJECT FileObject
,
905 PDEVICE_OBJECT DeviceObject
,
906 PFILE_BASIC_INFORMATION BasicInfo
,
907 PULONG BufferLength
);
910 VfatQueryInformation(
911 PVFAT_IRP_CONTEXT IrpContext
);
915 PVFAT_IRP_CONTEXT IrpContext
);
918 VfatSetAllocationSizeInformation(
919 PFILE_OBJECT FileObject
,
921 PDEVICE_EXTENSION DeviceExt
,
922 PLARGE_INTEGER AllocationSize
);
928 PVFAT_IRP_CONTEXT IrpContext
);
932 PDEVICE_EXTENSION DeviceExt
,
938 VfatFileSystemControl(
939 PVFAT_IRP_CONTEXT IrpContext
);
946 PDRIVER_OBJECT DriverObject
,
947 PUNICODE_STRING RegistryPath
);
958 PDEVICE_OBJECT DeviceObject
,
973 VfatCheckForDismount(
974 IN PDEVICE_EXTENSION DeviceExt
,
981 PVFAT_IRP_CONTEXT IrpContext
);
987 PVFAT_IRP_CONTEXT IrpContext
);
991 PVFAT_IRP_CONTEXT IrpContext
);
995 PDEVICE_EXTENSION DeviceExt
,
997 PULONG CurrentCluster
,
1008 PDEVICE_OBJECT DeviceObject
,
1015 PUNICODE_STRING PathNameU
,
1016 PUNICODE_STRING DirNameU
,
1017 PUNICODE_STRING FileNameU
);
1031 VfatQueryVolumeInformation(
1032 PVFAT_IRP_CONTEXT IrpContext
);
1035 VfatSetVolumeInformation(
1036 PVFAT_IRP_CONTEXT IrpContext
);
1038 #endif /* _FASTFAT_PCH_ */