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))
31 unsigned char magic0
, res0
, magic1
;
32 unsigned char OEMName
[8];
33 unsigned short BytesPerSector
;
34 unsigned char SectorsPerCluster
;
35 unsigned short ReservedSectors
;
36 unsigned char FATCount
;
37 unsigned short RootEntries
, Sectors
;
39 unsigned short FATSectors
, SectorsPerTrack
, Heads
;
40 unsigned long HiddenSectors
, SectorsHuge
;
41 unsigned char Drive
, Res1
, Sig
;
42 unsigned long VolumeID
;
43 unsigned char VolumeLabel
[11], SysType
[8];
44 unsigned char Res2
[448];
45 unsigned short Signatur1
;
50 unsigned char magic0
, res0
, magic1
; // 0
51 unsigned char OEMName
[8]; // 3
52 unsigned short BytesPerSector
; // 11
53 unsigned char SectorsPerCluster
; // 13
54 unsigned short ReservedSectors
; // 14
55 unsigned char FATCount
; // 16
56 unsigned short RootEntries
, Sectors
; // 17
57 unsigned char Media
; // 21
58 unsigned short FATSectors
, SectorsPerTrack
, Heads
; // 22
59 unsigned long HiddenSectors
, SectorsHuge
; // 28
60 unsigned long FATSectors32
; // 36
61 unsigned short ExtFlag
; // 40
62 unsigned short FSVersion
; // 42
63 unsigned long RootCluster
; // 44
64 unsigned short FSInfoSector
; // 48
65 unsigned short BootBackup
; // 50
66 unsigned char Res3
[12]; // 52
67 unsigned char Drive
; // 64
68 unsigned char Res4
; // 65
69 unsigned char ExtBootSignature
; // 66
70 unsigned long VolumeID
; // 67
71 unsigned char VolumeLabel
[11], SysType
[8]; // 71
72 unsigned char Res2
[420]; // 90
73 unsigned short Signature1
; // 510
76 struct _BootSectorFatX
78 unsigned char SysType
[4]; // 0
79 unsigned long VolumeID
; // 4
80 unsigned long SectorsPerCluster
; // 8
81 unsigned short FATCount
; // 12
82 unsigned long Unknown
; // 14
83 unsigned char Unused
[4078]; // 18
88 unsigned long ExtBootSignature2
; // 0
89 unsigned char Res6
[480]; // 4
90 unsigned long FSINFOSignature
; // 484
91 unsigned long FreeCluster
; // 488
92 unsigned long NextCluster
; // 492
93 unsigned char Res7
[12]; // 496
94 unsigned long Signatur2
; // 508
97 typedef struct _BootSector BootSector
;
103 struct { unsigned char Filename
[8], Ext
[3]; };
104 unsigned char ShortName
[11];
106 unsigned char Attrib
;
108 unsigned char CreationTimeMs
;
109 unsigned short CreationTime
,CreationDate
,AccessDate
;
112 unsigned short FirstClusterHigh
; // FAT32
113 unsigned short ExtendedAttributes
; // FAT12/FAT16
115 unsigned short UpdateTime
; //time create/update
116 unsigned short UpdateDate
; //date create/update
117 unsigned short FirstCluster
;
118 unsigned long FileSize
;
121 #define FAT_EAFILE "EA DATA. SF"
123 typedef struct _EAFileHeader FAT_EA_FILE_HEADER
, *PFAT_EA_FILE_HEADER
;
127 unsigned short Signature
; // ED
128 unsigned short Unknown
[15];
129 unsigned short EASetTable
[240];
132 typedef struct _EASetHeader FAT_EA_SET_HEADER
, *PFAT_EA_SET_HEADER
;
136 unsigned short Signature
; // EA
137 unsigned short Offset
; // relative offset, same value as in the EASetTable
138 unsigned short Unknown1
[2];
139 char TargetFileName
[12];
140 unsigned short Unknown2
[3];
141 unsigned int EALength
;
145 typedef struct _EAHeader FAT_EA_HEADER
, *PFAT_EA_HEADER
;
149 unsigned char Unknown
;
150 unsigned char EANameLength
;
151 unsigned short EAValueLength
;
156 typedef struct _FATDirEntry FAT_DIR_ENTRY
, *PFAT_DIR_ENTRY
;
160 unsigned char FilenameLength
; // 0
161 unsigned char Attrib
; // 1
162 unsigned char Filename
[42]; // 2
163 unsigned long FirstCluster
; // 44
164 unsigned long FileSize
; // 48
165 unsigned short UpdateTime
; // 52
166 unsigned short UpdateDate
; // 54
167 unsigned short CreationTime
; // 56
168 unsigned short CreationDate
; // 58
169 unsigned short AccessTime
; // 60
170 unsigned short AccessDate
; // 62
175 unsigned char id
; // sequence number for slot
176 WCHAR name0_4
[5]; // first 5 characters in name
177 unsigned char attr
; // attribute byte
178 unsigned char reserved
; // always 0
179 unsigned char alias_checksum
; // checksum for 8.3 alias
180 WCHAR name5_10
[6]; // 6 more characters in name
181 unsigned char start
[2]; // starting cluster number
182 WCHAR name11_12
[2]; // last 2 characters in name
185 typedef struct _slot slot
;
189 #define VFAT_CASE_LOWER_BASE 8 // base is lower case
190 #define VFAT_CASE_LOWER_EXT 16 // extension is lower case
192 #define LONGNAME_MAX_LENGTH 256 // max length for a long filename
194 #define ENTRY_DELETED(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_DELETED(&((DirEntry)->FatX)) : FAT_ENTRY_DELETED(&((DirEntry)->Fat)))
195 #define ENTRY_VOLUME(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_VOLUME(&((DirEntry)->FatX)) : FAT_ENTRY_VOLUME(&((DirEntry)->Fat)))
196 #define ENTRY_END(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_END(&((DirEntry)->FatX)) : FAT_ENTRY_END(&((DirEntry)->Fat)))
198 #define FAT_ENTRY_DELETED(DirEntry) ((DirEntry)->Filename[0] == 0xe5)
199 #define FAT_ENTRY_END(DirEntry) ((DirEntry)->Filename[0] == 0)
200 #define FAT_ENTRY_LONG(DirEntry) (((DirEntry)->Attrib & 0x3f) == 0x0f)
201 #define FAT_ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
203 #define FATX_ENTRY_DELETED(DirEntry) ((DirEntry)->FilenameLength == 0xe5)
204 #define FATX_ENTRY_END(DirEntry) ((DirEntry)->FilenameLength == 0xff)
205 #define FATX_ENTRY_LONG(DirEntry) (FALSE)
206 #define FATX_ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
208 #define FAT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof (FAT_DIR_ENTRY))
209 #define FATX_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof (FATX_DIR_ENTRY))
211 typedef struct _FATXDirEntry FATX_DIR_ENTRY
, *PFATX_DIR_ENTRY
;
219 typedef union _DIR_ENTRY DIR_ENTRY
, *PDIR_ENTRY
;
221 #define BLOCKSIZE 512
229 #define VCB_VOLUME_LOCKED 0x0001
230 #define VCB_DISMOUNT_PENDING 0x0002
231 #define VCB_IS_FATX 0x0004
232 #define VCB_IS_DIRTY 0x4000 /* Volume is dirty */
233 #define VCB_CLEAR_DIRTY 0x8000 /* Clean dirty flag at shutdown */
241 ULONG rootDirectorySectors
;
245 ULONG SectorsPerCluster
;
246 ULONG BytesPerSector
;
247 ULONG BytesPerCluster
;
248 ULONG NumberOfClusters
;
252 } FATINFO
, *PFATINFO
;
255 struct _VFAT_DIRENTRY_CONTEXT
;
257 typedef struct _HASHENTRY
260 struct _VFATFCB
* self
;
261 struct _HASHENTRY
* next
;
265 typedef struct DEVICE_EXTENSION
*PDEVICE_EXTENSION
;
267 typedef NTSTATUS (*PGET_NEXT_CLUSTER
)(PDEVICE_EXTENSION
,ULONG
,PULONG
);
268 typedef NTSTATUS (*PFIND_AND_MARK_AVAILABLE_CLUSTER
)(PDEVICE_EXTENSION
,PULONG
);
269 typedef NTSTATUS (*PWRITE_CLUSTER
)(PDEVICE_EXTENSION
,ULONG
,ULONG
,PULONG
);
271 typedef NTSTATUS (*PGET_NEXT_DIR_ENTRY
)(PVOID
*,PVOID
*,struct _VFATFCB
*,struct _VFAT_DIRENTRY_CONTEXT
*,BOOLEAN
);
273 typedef struct DEVICE_EXTENSION
275 ERESOURCE DirResource
;
276 ERESOURCE FatResource
;
278 KSPIN_LOCK FcbListLock
;
279 LIST_ENTRY FcbListHead
;
281 struct _HASHENTRY
**FcbHashTable
;
283 PDEVICE_OBJECT VolumeDevice
;
284 PDEVICE_OBJECT StorageDevice
;
285 PFILE_OBJECT FATFileObject
;
287 ULONG LastAvailableCluster
;
288 ULONG AvailableClusters
;
289 BOOLEAN AvailableClustersValid
;
291 struct _VFATFCB
*VolumeFcb
;
293 /* Pointers to functions for manipulating FAT. */
294 PGET_NEXT_CLUSTER GetNextCluster
;
295 PFIND_AND_MARK_AVAILABLE_CLUSTER FindAndMarkAvailableCluster
;
296 PWRITE_CLUSTER WriteCluster
;
297 ULONG CleanShutBitMask
;
299 /* Pointers to functions for manipulating directory entries. */
300 PGET_NEXT_DIR_ENTRY GetNextDirEntry
;
304 LIST_ENTRY VolumeListEntry
;
307 LIST_ENTRY NotifyList
;
308 PNOTIFY_SYNC NotifySync
;
310 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLOSE */
311 ULONG OpenHandleCount
;
313 /* VPBs for dismount */
316 } DEVICE_EXTENSION
, VCB
, *PVCB
;
318 #define VFAT_BREAK_ON_CORRUPTION 1
322 PDRIVER_OBJECT DriverObject
;
323 PDEVICE_OBJECT DeviceObject
;
325 ERESOURCE VolumeListLock
;
326 LIST_ENTRY VolumeListHead
;
327 NPAGED_LOOKASIDE_LIST FcbLookasideList
;
328 NPAGED_LOOKASIDE_LIST CcbLookasideList
;
329 NPAGED_LOOKASIDE_LIST IrpContextLookasideList
;
330 FAST_IO_DISPATCH FastIoDispatch
;
331 CACHE_MANAGER_CALLBACKS CacheMgrCallbacks
;
332 } VFAT_GLOBAL_DATA
, *PVFAT_GLOBAL_DATA
;
334 extern PVFAT_GLOBAL_DATA VfatGlobalData
;
336 #define FCB_CACHE_INITIALIZED 0x0001
337 #define FCB_DELETE_PENDING 0x0002
338 #define FCB_IS_FAT 0x0004
339 #define FCB_IS_PAGE_FILE 0x0008
340 #define FCB_IS_VOLUME 0x0010
341 #define FCB_IS_DIRTY 0x0020
342 #define FCB_IS_FATX_ENTRY 0x0040
344 typedef struct _VFATFCB
346 /* FCB header required by ROS/NT */
347 FSRTL_COMMON_FCB_HEADER RFCB
;
348 SECTION_OBJECT_POINTERS SectionObjectPointers
;
349 ERESOURCE MainResource
;
350 ERESOURCE PagingIoResource
;
351 /* end FCB header required by ROS/NT */
353 /* directory entry for this file or directory */
356 /* Pointer to attributes in entry */
359 /* long file name, points into PathNameBuffer */
360 UNICODE_STRING LongNameU
;
362 /* short file name */
363 UNICODE_STRING ShortNameU
;
365 /* directory name, points into PathNameBuffer */
366 UNICODE_STRING DirNameU
;
368 /* path + long file name 260 max*/
369 UNICODE_STRING PathNameU
;
371 /* buffer for PathNameU */
372 PWCHAR PathNameBuffer
;
374 /* buffer for ShortNameU */
375 WCHAR ShortNameBuffer
[13];
380 /* List of FCB's for this volume */
381 LIST_ENTRY FcbListEntry
;
383 /* pointer to the parent fcb */
384 struct _VFATFCB
*parentFcb
;
386 /* Flags for the fcb */
389 /* pointer to the file object which has initialized the fcb */
390 PFILE_OBJECT FileObject
;
392 /* Directory index for the short name entry */
395 /* Directory index where the long name starts */
398 /* Share access for the file object */
399 SHARE_ACCESS FCBShareAccess
;
401 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLEANUP */
402 ULONG OpenHandleCount
;
404 /* Entry into the hash table for the path + long name */
407 /* Entry into the hash table for the path + short name */
410 /* List of byte-range locks for this file */
414 * Optimalization: caching of last read/write cluster+offset pair. Can't
415 * be in VFATCCB because it must be reset everytime the allocated clusters
418 FAST_MUTEX LastMutex
;
421 } VFATFCB
, *PVFATFCB
;
423 typedef struct _VFATCCB
425 LARGE_INTEGER CurrentByteOffset
;
426 /* for DirectoryControl */
428 /* for DirectoryControl */
429 UNICODE_STRING SearchPattern
;
430 } VFATCCB
, *PVFATCCB
;
432 #define TAG_CCB 'BCCV'
433 #define TAG_FCB 'BCFV'
434 #define TAG_IRP 'PRIV'
435 #define TAG_VFAT 'TAFV'
437 #define ENTRIES_PER_SECTOR (BLOCKSIZE / sizeof(FATDirEntry))
439 typedef struct __DOSTIME
447 typedef struct __DOSDATE
455 #define IRPCONTEXT_CANWAIT 0x0001
456 #define IRPCONTEXT_COMPLETE 0x0002
457 #define IRPCONTEXT_QUEUE 0x0004
458 #define IRPCONTEXT_PENDINGRETURNED 0x0008
463 PDEVICE_OBJECT DeviceObject
;
464 PDEVICE_EXTENSION DeviceExt
;
466 WORK_QUEUE_ITEM WorkQueueItem
;
467 PIO_STACK_LOCATION Stack
;
470 PFILE_OBJECT FileObject
;
474 } VFAT_IRP_CONTEXT
, *PVFAT_IRP_CONTEXT
;
476 typedef struct _VFAT_DIRENTRY_CONTEXT
481 UNICODE_STRING LongNameU
;
482 UNICODE_STRING ShortNameU
;
483 } VFAT_DIRENTRY_CONTEXT
, *PVFAT_DIRENTRY_CONTEXT
;
485 typedef struct _VFAT_MOVE_CONTEXT
491 } VFAT_MOVE_CONTEXT
, *PVFAT_MOVE_CONTEXT
;
495 VfatMarkIrpContextForQueue(PVFAT_IRP_CONTEXT IrpContext
)
497 PULONG Flags
= &IrpContext
->Flags
;
499 *Flags
&= ~IRPCONTEXT_COMPLETE
;
500 *Flags
|= IRPCONTEXT_QUEUE
;
502 return STATUS_PENDING
;
509 IN PDEVICE_OBJECT pDeviceObject
,
510 IN PLARGE_INTEGER ReadOffset
,
513 IN BOOLEAN Override
);
517 IN PVFAT_IRP_CONTEXT IrpContext
,
518 IN PLARGE_INTEGER ReadOffset
,
520 IN ULONG BufferOffset
,
524 VfatWriteDiskPartial(
525 IN PVFAT_IRP_CONTEXT IrpContext
,
526 IN PLARGE_INTEGER WriteOffset
,
527 IN ULONG WriteLength
,
528 IN ULONG BufferOffset
,
532 VfatBlockDeviceIoControl(
533 IN PDEVICE_OBJECT DeviceObject
,
535 IN PVOID InputBuffer
,
536 IN ULONG InputBufferSize
,
537 IN OUT PVOID OutputBuffer
,
538 IN OUT PULONG pOutputBufferSize
,
539 IN BOOLEAN Override
);
545 PVFAT_IRP_CONTEXT IrpContext
);
551 PVFAT_IRP_CONTEXT IrpContext
);
555 PDEVICE_EXTENSION DeviceExt
,
556 PFILE_OBJECT FileObject
);
562 PVFAT_IRP_CONTEXT IrpContext
);
566 PDEVICE_EXTENSION DeviceExt
,
568 PUNICODE_STRING FileToFindU
,
569 PVFAT_DIRENTRY_CONTEXT DirContext
,
574 PFAT_DIR_ENTRY pEntry
,
575 PUNICODE_STRING NameU
);
579 PDEVICE_EXTENSION DeviceExt
,
585 VfatDirectoryControl(
586 PVFAT_IRP_CONTEXT IrpContext
);
589 FsdDosDateTimeToSystemTime(
590 PDEVICE_EXTENSION DeviceExt
,
593 PLARGE_INTEGER SystemTime
);
596 FsdSystemTimeToDosDateTime(
597 PDEVICE_EXTENSION DeviceExt
,
598 PLARGE_INTEGER SystemTime
,
605 vfatDirEntryGetFirstCluster(
606 PDEVICE_EXTENSION pDeviceExt
,
607 PDIR_ENTRY pDirEntry
);
610 VfatIsDirectoryEmpty(
618 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
626 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
633 PDEVICE_EXTENSION DeviceExt
,
634 PUNICODE_STRING PathNameU
,
637 ULONG RequestedOptions
,
639 PVFAT_MOVE_CONTEXT MoveContext
);
653 PDEVICE_EXTENSION DeviceExt
,
660 IN PDEVICE_EXTENSION DeviceExt
,
662 IN PUNICODE_STRING FileName
,
663 IN BOOLEAN CaseChangeOnly
);
667 IN PDEVICE_EXTENSION DeviceExt
,
669 IN PUNICODE_STRING FileName
,
670 IN PVFATFCB ParentFcb
);
675 VfatSetExtendedAttributes(
676 PFILE_OBJECT FileObject
,
683 VfatInitFastIoRoutines(
684 PFAST_IO_DISPATCH FastIoDispatch
);
688 VfatAcquireForLazyWrite(
694 VfatReleaseFromLazyWrite(
699 VfatAcquireForReadAhead(
705 VfatReleaseFromReadAhead(
712 PDEVICE_EXTENSION DeviceExt
,
713 ULONG CurrentCluster
,
717 FAT12FindAndMarkAvailableCluster(
718 PDEVICE_EXTENSION DeviceExt
,
723 PDEVICE_EXTENSION DeviceExt
,
724 ULONG ClusterToWrite
,
730 PDEVICE_EXTENSION DeviceExt
,
731 ULONG CurrentCluster
,
735 FAT16FindAndMarkAvailableCluster(
736 PDEVICE_EXTENSION DeviceExt
,
741 PDEVICE_EXTENSION DeviceExt
,
742 ULONG ClusterToWrite
,
748 PDEVICE_EXTENSION DeviceExt
,
749 ULONG CurrentCluster
,
753 FAT32FindAndMarkAvailableCluster(
754 PDEVICE_EXTENSION DeviceExt
,
759 PDEVICE_EXTENSION DeviceExt
,
760 ULONG ClusterToWrite
,
766 PDEVICE_EXTENSION DeviceExt
,
774 PDEVICE_EXTENSION DeviceExt
,
779 PDEVICE_EXTENSION DeviceExt
,
780 ULONG CurrentCluster
,
784 GetNextClusterExtend(
785 PDEVICE_EXTENSION DeviceExt
,
786 ULONG CurrentCluster
,
790 CountAvailableClusters(
791 PDEVICE_EXTENSION DeviceExt
,
792 PLARGE_INTEGER Clusters
);
796 PDEVICE_EXTENSION DeviceExt
,
797 ULONG ClusterToWrite
,
804 PDEVICE_EXTENSION pVCB
,
805 PUNICODE_STRING pFileNameU
);
809 PDEVICE_EXTENSION pVCB
,
811 PVFAT_DIRENTRY_CONTEXT DirContext
,
824 PDEVICE_EXTENSION pVCB
,
829 PDEVICE_EXTENSION pVCB
,
833 vfatGrabFCBFromTable(
834 PDEVICE_EXTENSION pDeviceExt
,
835 PUNICODE_STRING pFileNameU
);
839 PDEVICE_EXTENSION pVCB
);
843 PDEVICE_EXTENSION pVCB
);
854 vfatAttachFCBToFileObject(
855 PDEVICE_EXTENSION vcb
,
857 PFILE_OBJECT fileObject
);
861 PDEVICE_EXTENSION pVCB
,
863 PUNICODE_STRING FileToFindU
,
868 PDEVICE_EXTENSION pVCB
,
869 PVFATFCB
*pParentFCB
,
871 PUNICODE_STRING pFileNameU
);
874 vfatMakeFCBFromDirEntry(
876 PVFATFCB directoryFCB
,
877 PVFAT_DIRENTRY_CONTEXT DirContext
,
883 VfatGetStandardInformation(
885 PFILE_STANDARD_INFORMATION StandardInfo
,
886 PULONG BufferLength
);
889 VfatGetBasicInformation(
890 PFILE_OBJECT FileObject
,
892 PDEVICE_OBJECT DeviceObject
,
893 PFILE_BASIC_INFORMATION BasicInfo
,
894 PULONG BufferLength
);
897 VfatQueryInformation(
898 PVFAT_IRP_CONTEXT IrpContext
);
902 PVFAT_IRP_CONTEXT IrpContext
);
905 VfatSetAllocationSizeInformation(
906 PFILE_OBJECT FileObject
,
908 PDEVICE_EXTENSION DeviceExt
,
909 PLARGE_INTEGER AllocationSize
);
915 PVFAT_IRP_CONTEXT IrpContext
);
919 PDEVICE_EXTENSION DeviceExt
,
925 VfatFileSystemControl(
926 PVFAT_IRP_CONTEXT IrpContext
);
933 PDRIVER_OBJECT DriverObject
,
934 PUNICODE_STRING RegistryPath
);
945 PDEVICE_OBJECT DeviceObject
,
960 VfatCheckForDismount(
961 IN PDEVICE_EXTENSION DeviceExt
,
968 PVFAT_IRP_CONTEXT IrpContext
);
974 PVFAT_IRP_CONTEXT IrpContext
);
978 PVFAT_IRP_CONTEXT IrpContext
);
982 PDEVICE_EXTENSION DeviceExt
,
984 PULONG CurrentCluster
,
995 PDEVICE_OBJECT DeviceObject
,
1002 PUNICODE_STRING PathNameU
,
1003 PUNICODE_STRING DirNameU
,
1004 PUNICODE_STRING FileNameU
);
1018 VfatQueryVolumeInformation(
1019 PVFAT_IRP_CONTEXT IrpContext
);
1022 VfatSetVolumeInformation(
1023 PVFAT_IRP_CONTEXT IrpContext
);
1025 #endif /* _FASTFAT_PCH_ */