8 #define USE_ROS_CC_AND_FS
10 #define ROUND_DOWN(n, align) \
11 (((ULONG)n) & ~((align) - 1l))
13 #define ROUND_UP(n, align) \
14 ROUND_DOWN(((ULONG)n) + (align) - 1, (align))
19 unsigned char magic0
, res0
, magic1
;
20 unsigned char OEMName
[8];
21 unsigned short BytesPerSector
;
22 unsigned char SectorsPerCluster
;
23 unsigned short ReservedSectors
;
24 unsigned char FATCount
;
25 unsigned short RootEntries
, Sectors
;
27 unsigned short FATSectors
, SectorsPerTrack
, Heads
;
28 unsigned long HiddenSectors
, SectorsHuge
;
29 unsigned char Drive
, Res1
, Sig
;
30 unsigned long VolumeID
;
31 unsigned char VolumeLabel
[11], SysType
[8];
32 unsigned char Res2
[448];
33 unsigned short Signatur1
;
38 unsigned char magic0
, res0
, magic1
; // 0
39 unsigned char OEMName
[8]; // 3
40 unsigned short BytesPerSector
; // 11
41 unsigned char SectorsPerCluster
; // 13
42 unsigned short ReservedSectors
; // 14
43 unsigned char FATCount
; // 16
44 unsigned short RootEntries
, Sectors
; // 17
45 unsigned char Media
; // 21
46 unsigned short FATSectors
, SectorsPerTrack
, Heads
; // 22
47 unsigned long HiddenSectors
, SectorsHuge
; // 28
48 unsigned long FATSectors32
; // 36
49 unsigned short ExtFlag
; // 40
50 unsigned short FSVersion
; // 42
51 unsigned long RootCluster
; // 44
52 unsigned short FSInfoSector
; // 48
53 unsigned short BootBackup
; // 50
54 unsigned char Res3
[12]; // 52
55 unsigned char Drive
; // 64
56 unsigned char Res4
; // 65
57 unsigned char ExtBootSignature
; // 66
58 unsigned long VolumeID
; // 67
59 unsigned char VolumeLabel
[11], SysType
[8]; // 71
60 unsigned char Res2
[420]; // 90
61 unsigned short Signature1
; // 510
64 struct _BootSectorFatX
66 unsigned char SysType
[4]; // 0
67 unsigned long VolumeID
; // 4
68 unsigned long SectorsPerCluster
; // 8
69 unsigned short FATCount
; // 12
70 unsigned long Unknown
; // 14
71 unsigned char Unused
[4078]; // 18
76 unsigned long ExtBootSignature2
; // 0
77 unsigned char Res6
[480]; // 4
78 unsigned long FSINFOSignature
; // 484
79 unsigned long FreeCluster
; // 488
80 unsigned long NextCluster
; // 492
81 unsigned char Res7
[12]; // 496
82 unsigned long Signatur2
; // 508
85 typedef struct _BootSector BootSector
;
91 struct { unsigned char Filename
[8], Ext
[3]; };
92 unsigned char ShortName
[11];
96 unsigned char CreationTimeMs
;
97 unsigned short CreationTime
,CreationDate
,AccessDate
;
100 unsigned short FirstClusterHigh
; // FAT32
101 unsigned short ExtendedAttributes
; // FAT12/FAT16
103 unsigned short UpdateTime
; //time create/update
104 unsigned short UpdateDate
; //date create/update
105 unsigned short FirstCluster
;
106 unsigned long FileSize
;
109 #define FAT_EAFILE "EA DATA. SF"
111 typedef struct _EAFileHeader FAT_EA_FILE_HEADER
, *PFAT_EA_FILE_HEADER
;
115 unsigned short Signature
; // ED
116 unsigned short Unknown
[15];
117 unsigned short EASetTable
[240];
120 typedef struct _EASetHeader FAT_EA_SET_HEADER
, *PFAT_EA_SET_HEADER
;
124 unsigned short Signature
; // EA
125 unsigned short Offset
; // relative offset, same value as in the EASetTable
126 unsigned short Unknown1
[2];
127 char TargetFileName
[12];
128 unsigned short Unknown2
[3];
129 unsigned int EALength
;
133 typedef struct _EAHeader FAT_EA_HEADER
, *PFAT_EA_HEADER
;
137 unsigned char Unknown
;
138 unsigned char EANameLength
;
139 unsigned short EAValueLength
;
144 typedef struct _FATDirEntry FAT_DIR_ENTRY
, *PFAT_DIR_ENTRY
;
148 unsigned char FilenameLength
; // 0
149 unsigned char Attrib
; // 1
150 unsigned char Filename
[42]; // 2
151 unsigned long FirstCluster
; // 44
152 unsigned long FileSize
; // 48
153 unsigned short UpdateTime
; // 52
154 unsigned short UpdateDate
; // 54
155 unsigned short CreationTime
; // 56
156 unsigned short CreationDate
; // 58
157 unsigned short AccessTime
; // 60
158 unsigned short AccessDate
; // 62
163 unsigned char id
; // sequence number for slot
164 WCHAR name0_4
[5]; // first 5 characters in name
165 unsigned char attr
; // attribute byte
166 unsigned char reserved
; // always 0
167 unsigned char alias_checksum
; // checksum for 8.3 alias
168 WCHAR name5_10
[6]; // 6 more characters in name
169 unsigned char start
[2]; // starting cluster number
170 WCHAR name11_12
[2]; // last 2 characters in name
173 typedef struct _slot slot
;
177 #define VFAT_CASE_LOWER_BASE 8 // base is lower case
178 #define VFAT_CASE_LOWER_EXT 16 // extension is lower case
180 #define LONGNAME_MAX_LENGTH 256 // max length for a long filename
182 #define ENTRY_DELETED(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_DELETED(&((DirEntry)->FatX)) : FAT_ENTRY_DELETED(&((DirEntry)->Fat)))
183 #define ENTRY_VOLUME(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_VOLUME(&((DirEntry)->FatX)) : FAT_ENTRY_VOLUME(&((DirEntry)->Fat)))
184 #define ENTRY_END(DeviceExt, DirEntry) ((DeviceExt)->Flags & VCB_IS_FATX ? FATX_ENTRY_END(&((DirEntry)->FatX)) : FAT_ENTRY_END(&((DirEntry)->Fat)))
186 #define FAT_ENTRY_DELETED(DirEntry) ((DirEntry)->Filename[0] == 0xe5)
187 #define FAT_ENTRY_END(DirEntry) ((DirEntry)->Filename[0] == 0)
188 #define FAT_ENTRY_LONG(DirEntry) (((DirEntry)->Attrib & 0x3f) == 0x0f)
189 #define FAT_ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
191 #define FATX_ENTRY_DELETED(DirEntry) ((DirEntry)->FilenameLength == 0xe5)
192 #define FATX_ENTRY_END(DirEntry) ((DirEntry)->FilenameLength == 0xff)
193 #define FATX_ENTRY_LONG(DirEntry) (FALSE)
194 #define FATX_ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
196 #define FAT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof (FAT_DIR_ENTRY))
197 #define FATX_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof (FATX_DIR_ENTRY))
199 typedef struct _FATXDirEntry FATX_DIR_ENTRY
, *PFATX_DIR_ENTRY
;
207 typedef union _DIR_ENTRY DIR_ENTRY
, *PDIR_ENTRY
;
209 #define BLOCKSIZE 512
217 #define VCB_VOLUME_LOCKED 0x0001
218 #define VCB_DISMOUNT_PENDING 0x0002
219 #define VCB_IS_FATX 0x0004
220 #define VCB_IS_DIRTY 0x4000 /* Volume is dirty */
221 #define VCB_CLEAR_DIRTY 0x8000 /* Clean dirty flag at shutdown */
229 ULONG rootDirectorySectors
;
233 ULONG SectorsPerCluster
;
234 ULONG BytesPerSector
;
235 ULONG BytesPerCluster
;
236 ULONG NumberOfClusters
;
240 } FATINFO
, *PFATINFO
;
243 struct _VFAT_DIRENTRY_CONTEXT
;
245 typedef struct _HASHENTRY
248 struct _VFATFCB
* self
;
249 struct _HASHENTRY
* next
;
253 #define FCB_HASH_TABLE_SIZE 65536
255 typedef struct DEVICE_EXTENSION
*PDEVICE_EXTENSION
;
257 typedef NTSTATUS (*PGET_NEXT_CLUSTER
)(PDEVICE_EXTENSION
,ULONG
,PULONG
);
258 typedef NTSTATUS (*PFIND_AND_MARK_AVAILABLE_CLUSTER
)(PDEVICE_EXTENSION
,PULONG
);
259 typedef NTSTATUS (*PWRITE_CLUSTER
)(PDEVICE_EXTENSION
,ULONG
,ULONG
,PULONG
);
261 typedef NTSTATUS (*PGET_NEXT_DIR_ENTRY
)(PVOID
*,PVOID
*,struct _VFATFCB
*,struct _VFAT_DIRENTRY_CONTEXT
*,BOOLEAN
);
263 typedef struct DEVICE_EXTENSION
265 ERESOURCE DirResource
;
266 ERESOURCE FatResource
;
268 KSPIN_LOCK FcbListLock
;
269 LIST_ENTRY FcbListHead
;
271 struct _HASHENTRY
**FcbHashTable
;
273 PDEVICE_OBJECT StorageDevice
;
274 PFILE_OBJECT FATFileObject
;
276 ULONG LastAvailableCluster
;
277 ULONG AvailableClusters
;
278 BOOLEAN AvailableClustersValid
;
280 struct _VFATFCB
*VolumeFcb
;
282 /* Pointers to functions for manipulating FAT. */
283 PGET_NEXT_CLUSTER GetNextCluster
;
284 PFIND_AND_MARK_AVAILABLE_CLUSTER FindAndMarkAvailableCluster
;
285 PWRITE_CLUSTER WriteCluster
;
286 ULONG CleanShutBitMask
;
288 /* Pointers to functions for manipulating directory entries. */
289 PGET_NEXT_DIR_ENTRY GetNextDirEntry
;
293 LIST_ENTRY VolumeListEntry
;
294 } DEVICE_EXTENSION
, VCB
, *PVCB
;
298 PDRIVER_OBJECT DriverObject
;
299 PDEVICE_OBJECT DeviceObject
;
301 ERESOURCE VolumeListLock
;
302 LIST_ENTRY VolumeListHead
;
303 NPAGED_LOOKASIDE_LIST FcbLookasideList
;
304 NPAGED_LOOKASIDE_LIST CcbLookasideList
;
305 NPAGED_LOOKASIDE_LIST IrpContextLookasideList
;
306 FAST_IO_DISPATCH FastIoDispatch
;
307 CACHE_MANAGER_CALLBACKS CacheMgrCallbacks
;
308 } VFAT_GLOBAL_DATA
, *PVFAT_GLOBAL_DATA
;
310 extern PVFAT_GLOBAL_DATA VfatGlobalData
;
312 #define FCB_CACHE_INITIALIZED 0x0001
313 #define FCB_DELETE_PENDING 0x0002
314 #define FCB_IS_FAT 0x0004
315 #define FCB_IS_PAGE_FILE 0x0008
316 #define FCB_IS_VOLUME 0x0010
317 #define FCB_IS_DIRTY 0x0020
318 #define FCB_IS_FATX_ENTRY 0x0040
320 typedef struct _VFATFCB
322 /* FCB header required by ROS/NT */
323 FSRTL_COMMON_FCB_HEADER RFCB
;
324 SECTION_OBJECT_POINTERS SectionObjectPointers
;
325 ERESOURCE MainResource
;
326 ERESOURCE PagingIoResource
;
327 /* end FCB header required by ROS/NT */
329 /* directory entry for this file or directory */
332 /* Pointer to attributes in entry */
335 /* long file name, points into PathNameBuffer */
336 UNICODE_STRING LongNameU
;
338 /* short file name */
339 UNICODE_STRING ShortNameU
;
341 /* directory name, points into PathNameBuffer */
342 UNICODE_STRING DirNameU
;
344 /* path + long file name 260 max*/
345 UNICODE_STRING PathNameU
;
347 /* buffer for PathNameU */
348 PWCHAR PathNameBuffer
;
350 /* buffer for ShortNameU */
351 WCHAR ShortNameBuffer
[13];
356 /* List of FCB's for this volume */
357 LIST_ENTRY FcbListEntry
;
359 /* pointer to the parent fcb */
360 struct _VFATFCB
*parentFcb
;
362 /* Flags for the fcb */
365 /* pointer to the file object which has initialized the fcb */
366 PFILE_OBJECT FileObject
;
368 /* Directory index for the short name entry */
371 /* Directory index where the long name starts */
374 /* Share access for the file object */
375 SHARE_ACCESS FCBShareAccess
;
377 /* Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLEANUP */
378 ULONG OpenHandleCount
;
380 /* Entry into the hash table for the path + long name */
383 /* Entry into the hash table for the path + short name */
386 /* List of byte-range locks for this file */
390 * Optimalization: caching of last read/write cluster+offset pair. Can't
391 * be in VFATCCB because it must be reset everytime the allocated clusters
394 FAST_MUTEX LastMutex
;
397 } VFATFCB
, *PVFATFCB
;
399 typedef struct _VFATCCB
401 LARGE_INTEGER CurrentByteOffset
;
402 /* for DirectoryControl */
404 /* for DirectoryControl */
405 UNICODE_STRING SearchPattern
;
406 } VFATCCB
, *PVFATCCB
;
408 #define TAG_CCB 'BCCV'
409 #define TAG_FCB 'BCFV'
410 #define TAG_IRP 'PRIV'
411 #define TAG_VFAT 'TAFV'
413 #define ENTRIES_PER_SECTOR (BLOCKSIZE / sizeof(FATDirEntry))
415 typedef struct __DOSTIME
423 typedef struct __DOSDATE
431 #define IRPCONTEXT_CANWAIT 0x0001
432 #define IRPCONTEXT_PENDINGRETURNED 0x0002
437 PDEVICE_OBJECT DeviceObject
;
438 PDEVICE_EXTENSION DeviceExt
;
440 WORK_QUEUE_ITEM WorkQueueItem
;
441 PIO_STACK_LOCATION Stack
;
444 PFILE_OBJECT FileObject
;
447 } VFAT_IRP_CONTEXT
, *PVFAT_IRP_CONTEXT
;
449 typedef struct _VFAT_DIRENTRY_CONTEXT
454 UNICODE_STRING LongNameU
;
455 UNICODE_STRING ShortNameU
;
456 } VFAT_DIRENTRY_CONTEXT
, *PVFAT_DIRENTRY_CONTEXT
;
463 IN PDEVICE_OBJECT pDeviceObject
,
464 IN PLARGE_INTEGER ReadOffset
,
467 IN BOOLEAN Override
);
471 IN PVFAT_IRP_CONTEXT IrpContext
,
472 IN PLARGE_INTEGER ReadOffset
,
474 IN ULONG BufferOffset
,
478 VfatWriteDiskPartial(
479 IN PVFAT_IRP_CONTEXT IrpContext
,
480 IN PLARGE_INTEGER WriteOffset
,
481 IN ULONG WriteLength
,
482 IN ULONG BufferOffset
,
486 VfatBlockDeviceIoControl(
487 IN PDEVICE_OBJECT DeviceObject
,
489 IN PVOID InputBuffer
,
490 IN ULONG InputBufferSize
,
491 IN OUT PVOID OutputBuffer
,
492 IN OUT PULONG pOutputBufferSize
,
493 IN BOOLEAN Override
);
499 PVFAT_IRP_CONTEXT IrpContext
);
505 PVFAT_IRP_CONTEXT IrpContext
);
509 PDEVICE_EXTENSION DeviceExt
,
510 PFILE_OBJECT FileObject
);
516 PVFAT_IRP_CONTEXT IrpContext
);
520 PDEVICE_EXTENSION DeviceExt
,
522 PUNICODE_STRING FileToFindU
,
523 PVFAT_DIRENTRY_CONTEXT DirContext
,
528 PFAT_DIR_ENTRY pEntry
,
529 PUNICODE_STRING NameU
);
533 PDEVICE_EXTENSION DeviceExt
,
539 VfatDirectoryControl(
540 PVFAT_IRP_CONTEXT IrpContext
);
543 FsdDosDateTimeToSystemTime(
544 PDEVICE_EXTENSION DeviceExt
,
547 PLARGE_INTEGER SystemTime
);
550 FsdSystemTimeToDosDateTime(
551 PDEVICE_EXTENSION DeviceExt
,
552 PLARGE_INTEGER SystemTime
,
559 vfatDirEntryGetFirstCluster(
560 PDEVICE_EXTENSION pDeviceExt
,
561 PDIR_ENTRY pDirEntry
);
564 VfatIsDirectoryEmpty(
572 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
580 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
587 PDEVICE_EXTENSION DeviceExt
,
588 PUNICODE_STRING PathNameU
,
591 ULONG RequestedOptions
,
605 PDEVICE_EXTENSION DeviceExt
,
613 VfatSetExtendedAttributes(
614 PFILE_OBJECT FileObject
,
621 VfatInitFastIoRoutines(
622 PFAST_IO_DISPATCH FastIoDispatch
);
626 VfatAcquireForLazyWrite(
632 VfatReleaseFromLazyWrite(
637 VfatAcquireForReadAhead(
643 VfatReleaseFromReadAhead(
650 PDEVICE_EXTENSION DeviceExt
,
651 ULONG CurrentCluster
,
655 FAT12FindAndMarkAvailableCluster(
656 PDEVICE_EXTENSION DeviceExt
,
661 PDEVICE_EXTENSION DeviceExt
,
662 ULONG ClusterToWrite
,
668 PDEVICE_EXTENSION DeviceExt
,
669 ULONG CurrentCluster
,
673 FAT16FindAndMarkAvailableCluster(
674 PDEVICE_EXTENSION DeviceExt
,
679 PDEVICE_EXTENSION DeviceExt
,
680 ULONG ClusterToWrite
,
686 PDEVICE_EXTENSION DeviceExt
,
687 ULONG CurrentCluster
,
691 FAT32FindAndMarkAvailableCluster(
692 PDEVICE_EXTENSION DeviceExt
,
697 PDEVICE_EXTENSION DeviceExt
,
698 ULONG ClusterToWrite
,
704 PDEVICE_EXTENSION DeviceExt
,
712 PDEVICE_EXTENSION DeviceExt
,
717 PDEVICE_EXTENSION DeviceExt
,
718 ULONG CurrentCluster
,
722 GetNextClusterExtend(
723 PDEVICE_EXTENSION DeviceExt
,
724 ULONG CurrentCluster
,
728 CountAvailableClusters(
729 PDEVICE_EXTENSION DeviceExt
,
730 PLARGE_INTEGER Clusters
);
734 PDEVICE_EXTENSION DeviceExt
,
735 ULONG ClusterToWrite
,
742 PDEVICE_EXTENSION pVCB
,
743 PUNICODE_STRING pFileNameU
);
755 PDEVICE_EXTENSION pVCB
,
760 PDEVICE_EXTENSION pVCB
,
765 PDEVICE_EXTENSION pVCB
,
769 vfatGrabFCBFromTable(
770 PDEVICE_EXTENSION pDeviceExt
,
771 PUNICODE_STRING pFileNameU
);
775 PDEVICE_EXTENSION pVCB
);
779 PDEVICE_EXTENSION pVCB
);
790 vfatAttachFCBToFileObject(
791 PDEVICE_EXTENSION vcb
,
793 PFILE_OBJECT fileObject
);
797 PDEVICE_EXTENSION pVCB
,
799 PUNICODE_STRING FileToFindU
,
804 PDEVICE_EXTENSION pVCB
,
805 PVFATFCB
*pParentFCB
,
807 PUNICODE_STRING pFileNameU
);
810 vfatMakeFCBFromDirEntry(
812 PVFATFCB directoryFCB
,
813 PVFAT_DIRENTRY_CONTEXT DirContext
,
819 VfatQueryInformation(
820 PVFAT_IRP_CONTEXT IrpContext
);
824 PVFAT_IRP_CONTEXT IrpContext
);
827 VfatSetAllocationSizeInformation(
828 PFILE_OBJECT FileObject
,
830 PDEVICE_EXTENSION DeviceExt
,
831 PLARGE_INTEGER AllocationSize
);
837 PVFAT_IRP_CONTEXT IrpContext
);
841 PDEVICE_EXTENSION DeviceExt
,
847 VfatFileSystemControl(
848 PVFAT_IRP_CONTEXT IrpContext
);
855 PDRIVER_OBJECT DriverObject
,
856 PUNICODE_STRING RegistryPath
);
863 PVFAT_IRP_CONTEXT IrpContext
);
866 VfatAllocateIrpContext(
867 PDEVICE_OBJECT DeviceObject
,
872 PVFAT_IRP_CONTEXT IrpContext
);
880 PDEVICE_OBJECT DeviceObject
,
897 PVFAT_IRP_CONTEXT IrpContext
);
903 PVFAT_IRP_CONTEXT IrpContext
);
907 PVFAT_IRP_CONTEXT IrpContext
);
911 PDEVICE_EXTENSION DeviceExt
,
913 PULONG CurrentCluster
,
924 PDEVICE_OBJECT DeviceObject
,
931 PUNICODE_STRING PathNameU
,
932 PUNICODE_STRING DirNameU
,
933 PUNICODE_STRING FileNameU
);
947 VfatQueryVolumeInformation(
948 PVFAT_IRP_CONTEXT IrpContext
);
951 VfatSetVolumeInformation(
952 PVFAT_IRP_CONTEXT IrpContext
);
954 #endif /* _FASTFAT_PCH_ */