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
;
468 IN PDEVICE_OBJECT pDeviceObject
,
469 IN PLARGE_INTEGER ReadOffset
,
472 IN BOOLEAN Override
);
476 IN PVFAT_IRP_CONTEXT IrpContext
,
477 IN PLARGE_INTEGER ReadOffset
,
479 IN ULONG BufferOffset
,
483 VfatWriteDiskPartial(
484 IN PVFAT_IRP_CONTEXT IrpContext
,
485 IN PLARGE_INTEGER WriteOffset
,
486 IN ULONG WriteLength
,
487 IN ULONG BufferOffset
,
491 VfatBlockDeviceIoControl(
492 IN PDEVICE_OBJECT DeviceObject
,
494 IN PVOID InputBuffer
,
495 IN ULONG InputBufferSize
,
496 IN OUT PVOID OutputBuffer
,
497 IN OUT PULONG pOutputBufferSize
,
498 IN BOOLEAN Override
);
504 PVFAT_IRP_CONTEXT IrpContext
);
510 PVFAT_IRP_CONTEXT IrpContext
);
514 PDEVICE_EXTENSION DeviceExt
,
515 PFILE_OBJECT FileObject
);
521 PVFAT_IRP_CONTEXT IrpContext
);
525 PDEVICE_EXTENSION DeviceExt
,
527 PUNICODE_STRING FileToFindU
,
528 PVFAT_DIRENTRY_CONTEXT DirContext
,
533 PFAT_DIR_ENTRY pEntry
,
534 PUNICODE_STRING NameU
);
538 PDEVICE_EXTENSION DeviceExt
,
544 VfatDirectoryControl(
545 PVFAT_IRP_CONTEXT IrpContext
);
548 FsdDosDateTimeToSystemTime(
549 PDEVICE_EXTENSION DeviceExt
,
552 PLARGE_INTEGER SystemTime
);
555 FsdSystemTimeToDosDateTime(
556 PDEVICE_EXTENSION DeviceExt
,
557 PLARGE_INTEGER SystemTime
,
564 vfatDirEntryGetFirstCluster(
565 PDEVICE_EXTENSION pDeviceExt
,
566 PDIR_ENTRY pDirEntry
);
569 VfatIsDirectoryEmpty(
577 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
585 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
592 PDEVICE_EXTENSION DeviceExt
,
593 PUNICODE_STRING PathNameU
,
596 ULONG RequestedOptions
,
610 PDEVICE_EXTENSION DeviceExt
,
618 VfatSetExtendedAttributes(
619 PFILE_OBJECT FileObject
,
626 VfatInitFastIoRoutines(
627 PFAST_IO_DISPATCH FastIoDispatch
);
631 VfatAcquireForLazyWrite(
637 VfatReleaseFromLazyWrite(
642 VfatAcquireForReadAhead(
648 VfatReleaseFromReadAhead(
655 PDEVICE_EXTENSION DeviceExt
,
656 ULONG CurrentCluster
,
660 FAT12FindAndMarkAvailableCluster(
661 PDEVICE_EXTENSION DeviceExt
,
666 PDEVICE_EXTENSION DeviceExt
,
667 ULONG ClusterToWrite
,
673 PDEVICE_EXTENSION DeviceExt
,
674 ULONG CurrentCluster
,
678 FAT16FindAndMarkAvailableCluster(
679 PDEVICE_EXTENSION DeviceExt
,
684 PDEVICE_EXTENSION DeviceExt
,
685 ULONG ClusterToWrite
,
691 PDEVICE_EXTENSION DeviceExt
,
692 ULONG CurrentCluster
,
696 FAT32FindAndMarkAvailableCluster(
697 PDEVICE_EXTENSION DeviceExt
,
702 PDEVICE_EXTENSION DeviceExt
,
703 ULONG ClusterToWrite
,
709 PDEVICE_EXTENSION DeviceExt
,
717 PDEVICE_EXTENSION DeviceExt
,
722 PDEVICE_EXTENSION DeviceExt
,
723 ULONG CurrentCluster
,
727 GetNextClusterExtend(
728 PDEVICE_EXTENSION DeviceExt
,
729 ULONG CurrentCluster
,
733 CountAvailableClusters(
734 PDEVICE_EXTENSION DeviceExt
,
735 PLARGE_INTEGER Clusters
);
739 PDEVICE_EXTENSION DeviceExt
,
740 ULONG ClusterToWrite
,
747 PDEVICE_EXTENSION pVCB
,
748 PUNICODE_STRING pFileNameU
);
760 PDEVICE_EXTENSION pVCB
,
765 PDEVICE_EXTENSION pVCB
,
770 PDEVICE_EXTENSION pVCB
,
774 vfatGrabFCBFromTable(
775 PDEVICE_EXTENSION pDeviceExt
,
776 PUNICODE_STRING pFileNameU
);
780 PDEVICE_EXTENSION pVCB
);
784 PDEVICE_EXTENSION pVCB
);
795 vfatAttachFCBToFileObject(
796 PDEVICE_EXTENSION vcb
,
798 PFILE_OBJECT fileObject
);
802 PDEVICE_EXTENSION pVCB
,
804 PUNICODE_STRING FileToFindU
,
809 PDEVICE_EXTENSION pVCB
,
810 PVFATFCB
*pParentFCB
,
812 PUNICODE_STRING pFileNameU
);
815 vfatMakeFCBFromDirEntry(
817 PVFATFCB directoryFCB
,
818 PVFAT_DIRENTRY_CONTEXT DirContext
,
824 VfatQueryInformation(
825 PVFAT_IRP_CONTEXT IrpContext
);
829 PVFAT_IRP_CONTEXT IrpContext
);
832 VfatSetAllocationSizeInformation(
833 PFILE_OBJECT FileObject
,
835 PDEVICE_EXTENSION DeviceExt
,
836 PLARGE_INTEGER AllocationSize
);
842 PVFAT_IRP_CONTEXT IrpContext
);
846 PDEVICE_EXTENSION DeviceExt
,
852 VfatFileSystemControl(
853 PVFAT_IRP_CONTEXT IrpContext
);
860 PDRIVER_OBJECT DriverObject
,
861 PUNICODE_STRING RegistryPath
);
868 PVFAT_IRP_CONTEXT IrpContext
);
871 VfatAllocateIrpContext(
872 PDEVICE_OBJECT DeviceObject
,
877 PVFAT_IRP_CONTEXT IrpContext
);
885 PDEVICE_OBJECT DeviceObject
,
902 PVFAT_IRP_CONTEXT IrpContext
);
908 PVFAT_IRP_CONTEXT IrpContext
);
912 PVFAT_IRP_CONTEXT IrpContext
);
916 PDEVICE_EXTENSION DeviceExt
,
918 PULONG CurrentCluster
,
929 PDEVICE_OBJECT DeviceObject
,
936 PUNICODE_STRING PathNameU
,
937 PUNICODE_STRING DirNameU
,
938 PUNICODE_STRING FileNameU
);
952 VfatQueryVolumeInformation(
953 PVFAT_IRP_CONTEXT IrpContext
);
956 VfatSetVolumeInformation(
957 PVFAT_IRP_CONTEXT IrpContext
);
959 #endif /* _FASTFAT_PCH_ */