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
;
302 } DEVICE_EXTENSION
, VCB
, *PVCB
;
306 PDRIVER_OBJECT DriverObject
;
307 PDEVICE_OBJECT DeviceObject
;
309 ERESOURCE VolumeListLock
;
310 LIST_ENTRY VolumeListHead
;
311 NPAGED_LOOKASIDE_LIST FcbLookasideList
;
312 NPAGED_LOOKASIDE_LIST CcbLookasideList
;
313 NPAGED_LOOKASIDE_LIST IrpContextLookasideList
;
314 FAST_IO_DISPATCH FastIoDispatch
;
315 CACHE_MANAGER_CALLBACKS CacheMgrCallbacks
;
316 } VFAT_GLOBAL_DATA
, *PVFAT_GLOBAL_DATA
;
318 extern PVFAT_GLOBAL_DATA VfatGlobalData
;
320 #define FCB_CACHE_INITIALIZED 0x0001
321 #define FCB_DELETE_PENDING 0x0002
322 #define FCB_IS_FAT 0x0004
323 #define FCB_IS_PAGE_FILE 0x0008
324 #define FCB_IS_VOLUME 0x0010
325 #define FCB_IS_DIRTY 0x0020
326 #define FCB_IS_FATX_ENTRY 0x0040
328 typedef struct _VFATFCB
330 /* FCB header required by ROS/NT */
331 FSRTL_COMMON_FCB_HEADER RFCB
;
332 SECTION_OBJECT_POINTERS SectionObjectPointers
;
333 ERESOURCE MainResource
;
334 ERESOURCE PagingIoResource
;
335 /* end FCB header required by ROS/NT */
337 /* directory entry for this file or directory */
340 /* Pointer to attributes in entry */
343 /* long file name, points into PathNameBuffer */
344 UNICODE_STRING LongNameU
;
346 /* short file name */
347 UNICODE_STRING ShortNameU
;
349 /* directory name, points into PathNameBuffer */
350 UNICODE_STRING DirNameU
;
352 /* path + long file name 260 max*/
353 UNICODE_STRING PathNameU
;
355 /* buffer for PathNameU */
356 PWCHAR PathNameBuffer
;
358 /* buffer for ShortNameU */
359 WCHAR ShortNameBuffer
[13];
364 /* List of FCB's for this volume */
365 LIST_ENTRY FcbListEntry
;
367 /* pointer to the parent fcb */
368 struct _VFATFCB
*parentFcb
;
370 /* Flags for the fcb */
373 /* pointer to the file object which has initialized the fcb */
374 PFILE_OBJECT FileObject
;
376 /* Directory index for the short name entry */
379 /* Directory index where the long name starts */
382 /* Share access for the file object */
383 SHARE_ACCESS FCBShareAccess
;
385 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLEANUP */
386 ULONG OpenHandleCount
;
388 /* Entry into the hash table for the path + long name */
391 /* Entry into the hash table for the path + short name */
394 /* List of byte-range locks for this file */
398 * Optimalization: caching of last read/write cluster+offset pair. Can't
399 * be in VFATCCB because it must be reset everytime the allocated clusters
402 FAST_MUTEX LastMutex
;
405 } VFATFCB
, *PVFATFCB
;
407 typedef struct _VFATCCB
409 LARGE_INTEGER CurrentByteOffset
;
410 /* for DirectoryControl */
412 /* for DirectoryControl */
413 UNICODE_STRING SearchPattern
;
414 } VFATCCB
, *PVFATCCB
;
416 #define TAG_CCB 'BCCV'
417 #define TAG_FCB 'BCFV'
418 #define TAG_IRP 'PRIV'
419 #define TAG_VFAT 'TAFV'
421 #define ENTRIES_PER_SECTOR (BLOCKSIZE / sizeof(FATDirEntry))
423 typedef struct __DOSTIME
431 typedef struct __DOSDATE
439 #define IRPCONTEXT_CANWAIT 0x0001
440 #define IRPCONTEXT_PENDINGRETURNED 0x0002
445 PDEVICE_OBJECT DeviceObject
;
446 PDEVICE_EXTENSION DeviceExt
;
448 WORK_QUEUE_ITEM WorkQueueItem
;
449 PIO_STACK_LOCATION Stack
;
452 PFILE_OBJECT FileObject
;
455 } VFAT_IRP_CONTEXT
, *PVFAT_IRP_CONTEXT
;
457 typedef struct _VFAT_DIRENTRY_CONTEXT
462 UNICODE_STRING LongNameU
;
463 UNICODE_STRING ShortNameU
;
464 } VFAT_DIRENTRY_CONTEXT
, *PVFAT_DIRENTRY_CONTEXT
;
466 typedef struct _VFAT_MOVE_CONTEXT
472 } VFAT_MOVE_CONTEXT
, *PVFAT_MOVE_CONTEXT
;
478 IN PDEVICE_OBJECT pDeviceObject
,
479 IN PLARGE_INTEGER ReadOffset
,
482 IN BOOLEAN Override
);
486 IN PVFAT_IRP_CONTEXT IrpContext
,
487 IN PLARGE_INTEGER ReadOffset
,
489 IN ULONG BufferOffset
,
493 VfatWriteDiskPartial(
494 IN PVFAT_IRP_CONTEXT IrpContext
,
495 IN PLARGE_INTEGER WriteOffset
,
496 IN ULONG WriteLength
,
497 IN ULONG BufferOffset
,
501 VfatBlockDeviceIoControl(
502 IN PDEVICE_OBJECT DeviceObject
,
504 IN PVOID InputBuffer
,
505 IN ULONG InputBufferSize
,
506 IN OUT PVOID OutputBuffer
,
507 IN OUT PULONG pOutputBufferSize
,
508 IN BOOLEAN Override
);
514 PVFAT_IRP_CONTEXT IrpContext
);
520 PVFAT_IRP_CONTEXT IrpContext
);
524 PDEVICE_EXTENSION DeviceExt
,
525 PFILE_OBJECT FileObject
);
531 PVFAT_IRP_CONTEXT IrpContext
);
535 PDEVICE_EXTENSION DeviceExt
,
537 PUNICODE_STRING FileToFindU
,
538 PVFAT_DIRENTRY_CONTEXT DirContext
,
543 PFAT_DIR_ENTRY pEntry
,
544 PUNICODE_STRING NameU
);
548 PDEVICE_EXTENSION DeviceExt
,
554 VfatDirectoryControl(
555 PVFAT_IRP_CONTEXT IrpContext
);
558 FsdDosDateTimeToSystemTime(
559 PDEVICE_EXTENSION DeviceExt
,
562 PLARGE_INTEGER SystemTime
);
565 FsdSystemTimeToDosDateTime(
566 PDEVICE_EXTENSION DeviceExt
,
567 PLARGE_INTEGER SystemTime
,
574 vfatDirEntryGetFirstCluster(
575 PDEVICE_EXTENSION pDeviceExt
,
576 PDIR_ENTRY pDirEntry
);
579 VfatIsDirectoryEmpty(
587 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
595 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
602 PDEVICE_EXTENSION DeviceExt
,
603 PUNICODE_STRING PathNameU
,
606 ULONG RequestedOptions
,
608 PVFAT_MOVE_CONTEXT MoveContext
);
622 PDEVICE_EXTENSION DeviceExt
,
629 IN PDEVICE_EXTENSION DeviceExt
,
631 IN PUNICODE_STRING FileName
,
632 IN BOOLEAN CaseChangeOnly
);
636 IN PDEVICE_EXTENSION DeviceExt
,
638 IN PUNICODE_STRING FileName
,
639 IN PVFATFCB ParentFcb
);
644 VfatSetExtendedAttributes(
645 PFILE_OBJECT FileObject
,
652 VfatInitFastIoRoutines(
653 PFAST_IO_DISPATCH FastIoDispatch
);
657 VfatAcquireForLazyWrite(
663 VfatReleaseFromLazyWrite(
668 VfatAcquireForReadAhead(
674 VfatReleaseFromReadAhead(
681 PDEVICE_EXTENSION DeviceExt
,
682 ULONG CurrentCluster
,
686 FAT12FindAndMarkAvailableCluster(
687 PDEVICE_EXTENSION DeviceExt
,
692 PDEVICE_EXTENSION DeviceExt
,
693 ULONG ClusterToWrite
,
699 PDEVICE_EXTENSION DeviceExt
,
700 ULONG CurrentCluster
,
704 FAT16FindAndMarkAvailableCluster(
705 PDEVICE_EXTENSION DeviceExt
,
710 PDEVICE_EXTENSION DeviceExt
,
711 ULONG ClusterToWrite
,
717 PDEVICE_EXTENSION DeviceExt
,
718 ULONG CurrentCluster
,
722 FAT32FindAndMarkAvailableCluster(
723 PDEVICE_EXTENSION DeviceExt
,
728 PDEVICE_EXTENSION DeviceExt
,
729 ULONG ClusterToWrite
,
735 PDEVICE_EXTENSION DeviceExt
,
743 PDEVICE_EXTENSION DeviceExt
,
748 PDEVICE_EXTENSION DeviceExt
,
749 ULONG CurrentCluster
,
753 GetNextClusterExtend(
754 PDEVICE_EXTENSION DeviceExt
,
755 ULONG CurrentCluster
,
759 CountAvailableClusters(
760 PDEVICE_EXTENSION DeviceExt
,
761 PLARGE_INTEGER Clusters
);
765 PDEVICE_EXTENSION DeviceExt
,
766 ULONG ClusterToWrite
,
773 PDEVICE_EXTENSION pVCB
,
774 PUNICODE_STRING pFileNameU
);
778 PDEVICE_EXTENSION pVCB
,
780 PUNICODE_STRING LongName
,
781 PUNICODE_STRING ShortName
,
794 PDEVICE_EXTENSION pVCB
,
799 PDEVICE_EXTENSION pVCB
,
803 vfatGrabFCBFromTable(
804 PDEVICE_EXTENSION pDeviceExt
,
805 PUNICODE_STRING pFileNameU
);
809 PDEVICE_EXTENSION pVCB
);
813 PDEVICE_EXTENSION pVCB
);
824 vfatAttachFCBToFileObject(
825 PDEVICE_EXTENSION vcb
,
827 PFILE_OBJECT fileObject
);
831 PDEVICE_EXTENSION pVCB
,
833 PUNICODE_STRING FileToFindU
,
838 PDEVICE_EXTENSION pVCB
,
839 PVFATFCB
*pParentFCB
,
841 PUNICODE_STRING pFileNameU
);
844 vfatMakeFCBFromDirEntry(
846 PVFATFCB directoryFCB
,
847 PVFAT_DIRENTRY_CONTEXT DirContext
,
853 VfatQueryInformation(
854 PVFAT_IRP_CONTEXT IrpContext
);
858 PVFAT_IRP_CONTEXT IrpContext
);
861 VfatSetAllocationSizeInformation(
862 PFILE_OBJECT FileObject
,
864 PDEVICE_EXTENSION DeviceExt
,
865 PLARGE_INTEGER AllocationSize
);
871 PVFAT_IRP_CONTEXT IrpContext
);
875 PDEVICE_EXTENSION DeviceExt
,
881 VfatFileSystemControl(
882 PVFAT_IRP_CONTEXT IrpContext
);
889 PDRIVER_OBJECT DriverObject
,
890 PUNICODE_STRING RegistryPath
);
897 PVFAT_IRP_CONTEXT IrpContext
);
900 VfatAllocateIrpContext(
901 PDEVICE_OBJECT DeviceObject
,
906 PVFAT_IRP_CONTEXT IrpContext
);
914 PDEVICE_OBJECT DeviceObject
,
931 PVFAT_IRP_CONTEXT IrpContext
);
937 PVFAT_IRP_CONTEXT IrpContext
);
941 PVFAT_IRP_CONTEXT IrpContext
);
945 PDEVICE_EXTENSION DeviceExt
,
947 PULONG CurrentCluster
,
958 PDEVICE_OBJECT DeviceObject
,
965 PUNICODE_STRING PathNameU
,
966 PUNICODE_STRING DirNameU
,
967 PUNICODE_STRING FileNameU
);
981 VfatQueryVolumeInformation(
982 PVFAT_IRP_CONTEXT IrpContext
);
985 VfatSetVolumeInformation(
986 PVFAT_IRP_CONTEXT IrpContext
);
988 #endif /* _FASTFAT_PCH_ */