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_COMPLETE 0x0002
451 #define IRPCONTEXT_QUEUE 0x0004
452 #define IRPCONTEXT_PENDINGRETURNED 0x0008
457 PDEVICE_OBJECT DeviceObject
;
458 PDEVICE_EXTENSION DeviceExt
;
460 WORK_QUEUE_ITEM WorkQueueItem
;
461 PIO_STACK_LOCATION Stack
;
464 PFILE_OBJECT FileObject
;
468 } VFAT_IRP_CONTEXT
, *PVFAT_IRP_CONTEXT
;
470 typedef struct _VFAT_DIRENTRY_CONTEXT
475 UNICODE_STRING LongNameU
;
476 UNICODE_STRING ShortNameU
;
477 } VFAT_DIRENTRY_CONTEXT
, *PVFAT_DIRENTRY_CONTEXT
;
479 typedef struct _VFAT_MOVE_CONTEXT
485 } VFAT_MOVE_CONTEXT
, *PVFAT_MOVE_CONTEXT
;
489 VfatMarkIrpContextForQueue(PVFAT_IRP_CONTEXT IrpContext
)
491 PULONG Flags
= &IrpContext
->Flags
;
493 *Flags
&= ~IRPCONTEXT_COMPLETE
;
494 *Flags
|= IRPCONTEXT_QUEUE
;
496 return STATUS_PENDING
;
503 IN PDEVICE_OBJECT pDeviceObject
,
504 IN PLARGE_INTEGER ReadOffset
,
507 IN BOOLEAN Override
);
511 IN PVFAT_IRP_CONTEXT IrpContext
,
512 IN PLARGE_INTEGER ReadOffset
,
514 IN ULONG BufferOffset
,
518 VfatWriteDiskPartial(
519 IN PVFAT_IRP_CONTEXT IrpContext
,
520 IN PLARGE_INTEGER WriteOffset
,
521 IN ULONG WriteLength
,
522 IN ULONG BufferOffset
,
526 VfatBlockDeviceIoControl(
527 IN PDEVICE_OBJECT DeviceObject
,
529 IN PVOID InputBuffer
,
530 IN ULONG InputBufferSize
,
531 IN OUT PVOID OutputBuffer
,
532 IN OUT PULONG pOutputBufferSize
,
533 IN BOOLEAN Override
);
539 PVFAT_IRP_CONTEXT IrpContext
);
545 PVFAT_IRP_CONTEXT IrpContext
);
549 PDEVICE_EXTENSION DeviceExt
,
550 PFILE_OBJECT FileObject
);
556 PVFAT_IRP_CONTEXT IrpContext
);
560 PDEVICE_EXTENSION DeviceExt
,
562 PUNICODE_STRING FileToFindU
,
563 PVFAT_DIRENTRY_CONTEXT DirContext
,
568 PFAT_DIR_ENTRY pEntry
,
569 PUNICODE_STRING NameU
);
573 PDEVICE_EXTENSION DeviceExt
,
579 VfatDirectoryControl(
580 PVFAT_IRP_CONTEXT IrpContext
);
583 FsdDosDateTimeToSystemTime(
584 PDEVICE_EXTENSION DeviceExt
,
587 PLARGE_INTEGER SystemTime
);
590 FsdSystemTimeToDosDateTime(
591 PDEVICE_EXTENSION DeviceExt
,
592 PLARGE_INTEGER SystemTime
,
599 vfatDirEntryGetFirstCluster(
600 PDEVICE_EXTENSION pDeviceExt
,
601 PDIR_ENTRY pDirEntry
);
604 VfatIsDirectoryEmpty(
612 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
620 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
627 PDEVICE_EXTENSION DeviceExt
,
628 PUNICODE_STRING PathNameU
,
631 ULONG RequestedOptions
,
633 PVFAT_MOVE_CONTEXT MoveContext
);
647 PDEVICE_EXTENSION DeviceExt
,
654 IN PDEVICE_EXTENSION DeviceExt
,
656 IN PUNICODE_STRING FileName
,
657 IN BOOLEAN CaseChangeOnly
);
661 IN PDEVICE_EXTENSION DeviceExt
,
663 IN PUNICODE_STRING FileName
,
664 IN PVFATFCB ParentFcb
);
669 VfatSetExtendedAttributes(
670 PFILE_OBJECT FileObject
,
677 VfatInitFastIoRoutines(
678 PFAST_IO_DISPATCH FastIoDispatch
);
682 VfatAcquireForLazyWrite(
688 VfatReleaseFromLazyWrite(
693 VfatAcquireForReadAhead(
699 VfatReleaseFromReadAhead(
706 PDEVICE_EXTENSION DeviceExt
,
707 ULONG CurrentCluster
,
711 FAT12FindAndMarkAvailableCluster(
712 PDEVICE_EXTENSION DeviceExt
,
717 PDEVICE_EXTENSION DeviceExt
,
718 ULONG ClusterToWrite
,
724 PDEVICE_EXTENSION DeviceExt
,
725 ULONG CurrentCluster
,
729 FAT16FindAndMarkAvailableCluster(
730 PDEVICE_EXTENSION DeviceExt
,
735 PDEVICE_EXTENSION DeviceExt
,
736 ULONG ClusterToWrite
,
742 PDEVICE_EXTENSION DeviceExt
,
743 ULONG CurrentCluster
,
747 FAT32FindAndMarkAvailableCluster(
748 PDEVICE_EXTENSION DeviceExt
,
753 PDEVICE_EXTENSION DeviceExt
,
754 ULONG ClusterToWrite
,
760 PDEVICE_EXTENSION DeviceExt
,
768 PDEVICE_EXTENSION DeviceExt
,
773 PDEVICE_EXTENSION DeviceExt
,
774 ULONG CurrentCluster
,
778 GetNextClusterExtend(
779 PDEVICE_EXTENSION DeviceExt
,
780 ULONG CurrentCluster
,
784 CountAvailableClusters(
785 PDEVICE_EXTENSION DeviceExt
,
786 PLARGE_INTEGER Clusters
);
790 PDEVICE_EXTENSION DeviceExt
,
791 ULONG ClusterToWrite
,
798 PDEVICE_EXTENSION pVCB
,
799 PUNICODE_STRING pFileNameU
);
803 PDEVICE_EXTENSION pVCB
,
805 PUNICODE_STRING LongName
,
806 PUNICODE_STRING ShortName
,
819 PDEVICE_EXTENSION pVCB
,
824 PDEVICE_EXTENSION pVCB
,
828 vfatGrabFCBFromTable(
829 PDEVICE_EXTENSION pDeviceExt
,
830 PUNICODE_STRING pFileNameU
);
834 PDEVICE_EXTENSION pVCB
);
838 PDEVICE_EXTENSION pVCB
);
849 vfatAttachFCBToFileObject(
850 PDEVICE_EXTENSION vcb
,
852 PFILE_OBJECT fileObject
);
856 PDEVICE_EXTENSION pVCB
,
858 PUNICODE_STRING FileToFindU
,
863 PDEVICE_EXTENSION pVCB
,
864 PVFATFCB
*pParentFCB
,
866 PUNICODE_STRING pFileNameU
);
869 vfatMakeFCBFromDirEntry(
871 PVFATFCB directoryFCB
,
872 PVFAT_DIRENTRY_CONTEXT DirContext
,
878 VfatGetStandardInformation(
880 PFILE_STANDARD_INFORMATION StandardInfo
,
881 PULONG BufferLength
);
884 VfatGetBasicInformation(
885 PFILE_OBJECT FileObject
,
887 PDEVICE_OBJECT DeviceObject
,
888 PFILE_BASIC_INFORMATION BasicInfo
,
889 PULONG BufferLength
);
892 VfatQueryInformation(
893 PVFAT_IRP_CONTEXT IrpContext
);
897 PVFAT_IRP_CONTEXT IrpContext
);
900 VfatSetAllocationSizeInformation(
901 PFILE_OBJECT FileObject
,
903 PDEVICE_EXTENSION DeviceExt
,
904 PLARGE_INTEGER AllocationSize
);
910 PVFAT_IRP_CONTEXT IrpContext
);
914 PDEVICE_EXTENSION DeviceExt
,
920 VfatFileSystemControl(
921 PVFAT_IRP_CONTEXT IrpContext
);
928 PDRIVER_OBJECT DriverObject
,
929 PUNICODE_STRING RegistryPath
);
940 PDEVICE_OBJECT DeviceObject
,
955 VfatCheckForDismount(
956 IN PDEVICE_EXTENSION DeviceExt
,
963 PVFAT_IRP_CONTEXT IrpContext
);
969 PVFAT_IRP_CONTEXT IrpContext
);
973 PVFAT_IRP_CONTEXT IrpContext
);
977 PDEVICE_EXTENSION DeviceExt
,
979 PULONG CurrentCluster
,
990 PDEVICE_OBJECT DeviceObject
,
997 PUNICODE_STRING PathNameU
,
998 PUNICODE_STRING DirNameU
,
999 PUNICODE_STRING FileNameU
);
1013 VfatQueryVolumeInformation(
1014 PVFAT_IRP_CONTEXT IrpContext
);
1017 VfatSetVolumeInformation(
1018 PVFAT_IRP_CONTEXT IrpContext
);
1020 #endif /* _FASTFAT_PCH_ */