1 /* $Id: vfat.h,v 1.65 2004/07/05 21:39:02 hbirr Exp $ */
5 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
6 #define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
10 unsigned char magic0
, res0
, magic1
;
11 unsigned char OEMName
[8];
12 unsigned short BytesPerSector
;
13 unsigned char SectorsPerCluster
;
14 unsigned short ReservedSectors
;
15 unsigned char FATCount
;
16 unsigned short RootEntries
, Sectors
;
18 unsigned short FATSectors
, SectorsPerTrack
, Heads
;
19 unsigned long HiddenSectors
, SectorsHuge
;
20 unsigned char Drive
, Res1
, Sig
;
21 unsigned long VolumeID
;
22 unsigned char VolumeLabel
[11], SysType
[8];
23 unsigned char Res2
[448];
24 unsigned short Signatur1
;
25 } __attribute__((packed
));
29 unsigned char magic0
, res0
, magic1
; // 0
30 unsigned char OEMName
[8]; // 3
31 unsigned short BytesPerSector
; // 11
32 unsigned char SectorsPerCluster
; // 13
33 unsigned short ReservedSectors
; // 14
34 unsigned char FATCount
; // 16
35 unsigned short RootEntries
, Sectors
; // 17
36 unsigned char Media
; // 21
37 unsigned short FATSectors
, SectorsPerTrack
, Heads
; // 22
38 unsigned long HiddenSectors
, SectorsHuge
; // 28
39 unsigned long FATSectors32
; // 36
40 unsigned short ExtFlag
; // 40
41 unsigned short FSVersion
; // 42
42 unsigned long RootCluster
; // 44
43 unsigned short FSInfoSector
; // 48
44 unsigned short BootBackup
; // 50
45 unsigned char Res3
[12]; // 52
46 unsigned char Drive
; // 64
47 unsigned char Res4
; // 65
48 unsigned char ExtBootSignature
; // 66
49 unsigned long VolumeID
; // 67
50 unsigned char VolumeLabel
[11], SysType
[8]; // 71
51 unsigned char Res2
[420]; // 90
52 unsigned short Signature1
; // 510
53 } __attribute__((packed
));
57 unsigned long ExtBootSignature2
; // 0
58 unsigned char Res6
[480]; // 4
59 unsigned long FSINFOSignature
; // 484
60 unsigned long FreeCluster
; // 488
61 unsigned long NextCluster
; // 492
62 unsigned char Res7
[12]; // 496
63 unsigned long Signatur2
; // 508
64 } __attribute__((packed
));
66 typedef struct _BootSector BootSector
;
68 #define VFAT_CASE_LOWER_BASE 8 // base is lower case
69 #define VFAT_CASE_LOWER_EXT 16 // extension is lower case
71 #define ENTRY_DELETED(DirEntry) ((DirEntry)->Filename[0] == 0xe5)
72 #define ENTRY_END(DirEntry) ((DirEntry)->Filename[0] == 0)
73 #define ENTRY_LONG(DirEntry) (((DirEntry)->Attrib & 0x3f) == 0x0f)
74 #define ENTRY_VOLUME(DirEntry) (((DirEntry)->Attrib & 0x1f) == 0x08)
78 unsigned char Filename
[8], Ext
[3];
81 unsigned char CreationTimeMs
;
82 unsigned short CreationTime
,CreationDate
,AccessDate
;
83 unsigned short FirstClusterHigh
; // higher
84 unsigned short UpdateTime
; //time create/update
85 unsigned short UpdateDate
; //date create/update
86 unsigned short FirstCluster
;
87 unsigned long FileSize
;
88 } __attribute__((packed
));
90 typedef struct _FATDirEntry FATDirEntry
, FAT_DIR_ENTRY
, *PFAT_DIR_ENTRY
;
94 unsigned char id
; // sequence number for slot
95 WCHAR name0_4
[5]; // first 5 characters in name
96 unsigned char attr
; // attribute byte
97 unsigned char reserved
; // always 0
98 unsigned char alias_checksum
; // checksum for 8.3 alias
99 WCHAR name5_10
[6]; // 6 more characters in name
100 unsigned char start
[2]; // starting cluster number
101 WCHAR name11_12
[2]; // last 2 characters in name
102 } __attribute__((packed
));
105 typedef struct _slot slot
;
107 #define BLOCKSIZE 512
113 #define VCB_VOLUME_LOCKED 0x0001
114 #define VCB_DISMOUNT_PENDING 0x0002
122 ULONG rootDirectorySectors
;
126 ULONG SectorsPerCluster
;
127 ULONG BytesPerSector
;
128 ULONG BytesPerCluster
;
129 ULONG NumberOfClusters
;
133 } FATINFO
, *PFATINFO
;
137 typedef struct _HASHENTRY
140 struct _VFATFCB
* self
;
141 struct _HASHENTRY
* next
;
145 #define FCB_HASH_TABLE_SIZE 1024
149 ERESOURCE DirResource
;
150 ERESOURCE FatResource
;
152 KSPIN_LOCK FcbListLock
;
153 LIST_ENTRY FcbListHead
;
154 struct _HASHENTRY
* FcbHashTable
[FCB_HASH_TABLE_SIZE
];
156 PDEVICE_OBJECT StorageDevice
;
157 PFILE_OBJECT FATFileObject
;
159 ULONG LastAvailableCluster
;
160 ULONG AvailableClusters
;
161 BOOLEAN AvailableClustersValid
;
163 struct _VFATFCB
* VolumeFcb
;
165 LIST_ENTRY VolumeListEntry
;
166 } DEVICE_EXTENSION
, *PDEVICE_EXTENSION
, VCB
, *PVCB
;
170 PDRIVER_OBJECT DriverObject
;
171 PDEVICE_OBJECT DeviceObject
;
173 ERESOURCE VolumeListLock
;
174 LIST_ENTRY VolumeListHead
;
175 NPAGED_LOOKASIDE_LIST FcbLookasideList
;
176 NPAGED_LOOKASIDE_LIST CcbLookasideList
;
177 NPAGED_LOOKASIDE_LIST IrpContextLookasideList
;
178 } VFAT_GLOBAL_DATA
, *PVFAT_GLOBAL_DATA
;
180 extern PVFAT_GLOBAL_DATA VfatGlobalData
;
182 #define FCB_CACHE_INITIALIZED 0x0001
183 #define FCB_DELETE_PENDING 0x0002
184 #define FCB_IS_FAT 0x0004
185 #define FCB_IS_PAGE_FILE 0x0008
186 #define FCB_IS_VOLUME 0x0010
187 #define FCB_IS_DIRTY 0x0020
189 typedef struct _VFATFCB
191 /* FCB header required by ROS/NT */
192 FSRTL_COMMON_FCB_HEADER RFCB
;
193 SECTION_OBJECT_POINTERS SectionObjectPointers
;
194 ERESOURCE MainResource
;
195 ERESOURCE PagingIoResource
;
196 /* end FCB header required by ROS/NT */
198 /* directory entry for this file or directory */
201 /* long file name, points into PathNameBuffer */
202 UNICODE_STRING LongNameU
;
204 /* short file name */
205 UNICODE_STRING ShortNameU
;
207 /* directory name, points into PathNameBuffer */
208 UNICODE_STRING DirNameU
;
210 /* path + long file name 260 max*/
211 UNICODE_STRING PathNameU
;
213 /* buffer for PathNameU */
214 WCHAR PathNameBuffer
[MAX_PATH
];
216 /* buffer for ShortNameU */
217 WCHAR ShortNameBuffer
[13];
222 /* List of FCB's for this volume */
223 LIST_ENTRY FcbListEntry
;
225 /* pointer to the parent fcb */
226 struct _VFATFCB
* parentFcb
;
228 /* Flags for the fcb */
231 /* pointer to the file object which has initialized the fcb */
232 PFILE_OBJECT FileObject
;
234 /* Directory index for the short name entry */
237 /* Directory index where the long name starts */
240 /* Share access for the file object */
241 SHARE_ACCESS FCBShareAccess
;
243 /* Entry into the hash table for the path + long name */
246 /* Entry into the hash table for the path + short name */
249 /* List of byte-range locks for this file */
252 } VFATFCB
, *PVFATFCB
;
254 typedef struct _VFATCCB
256 LARGE_INTEGER CurrentByteOffset
;
257 /* for DirectoryControl */
259 /* for DirectoryControl */
260 UNICODE_STRING SearchPattern
;
264 } VFATCCB
, *PVFATCCB
;
267 #define TAG(A, B, C, D) (ULONG)(((A)<<0) + ((B)<<8) + ((C)<<16) + ((D)<<24))
270 #define TAG_CCB TAG('V', 'C', 'C', 'B')
271 #define TAG_FCB TAG('V', 'F', 'C', 'B')
272 #define TAG_IRP TAG('V', 'I', 'R', 'P')
274 #define ENTRIES_PER_SECTOR (BLOCKSIZE / sizeof(FATDirEntry))
276 typedef struct __DOSTIME
284 typedef struct __DOSDATE
292 #define IRPCONTEXT_CANWAIT 0x0001
293 #define IRPCONTEXT_PENDINGRETURNED 0x0002
298 PDEVICE_OBJECT DeviceObject
;
299 PDEVICE_EXTENSION DeviceExt
;
301 WORK_QUEUE_ITEM WorkQueueItem
;
302 PIO_STACK_LOCATION Stack
;
305 PFILE_OBJECT FileObject
;
308 } VFAT_IRP_CONTEXT
, *PVFAT_IRP_CONTEXT
;
310 typedef struct _VFAT_DIRENTRY_CONTEXT
314 FAT_DIR_ENTRY FatDirEntry
;
315 UNICODE_STRING LongNameU
;
316 UNICODE_STRING ShortNameU
;
317 } VFAT_DIRENTRY_CONTEXT
, *PVFAT_DIRENTRY_CONTEXT
;
320 /* ------------------------------------------------------ shutdown.c */
322 NTSTATUS STDCALL
VfatShutdown (PDEVICE_OBJECT DeviceObject
,
325 /* -------------------------------------------------------- volume.c */
327 NTSTATUS
VfatQueryVolumeInformation (PVFAT_IRP_CONTEXT IrpContext
);
329 NTSTATUS
VfatSetVolumeInformation (PVFAT_IRP_CONTEXT IrpContext
);
331 /* ------------------------------------------------------ blockdev.c */
333 NTSTATUS
VfatReadDisk(IN PDEVICE_OBJECT pDeviceObject
,
334 IN PLARGE_INTEGER ReadOffset
,
337 IN BOOLEAN Override
);
339 NTSTATUS
VfatReadDiskPartial (IN PVFAT_IRP_CONTEXT IrpContext
,
340 IN PLARGE_INTEGER ReadOffset
,
342 IN ULONG BufferOffset
,
345 NTSTATUS
VfatWriteDiskPartial(IN PVFAT_IRP_CONTEXT IrpContext
,
346 IN PLARGE_INTEGER WriteOffset
,
347 IN ULONG WriteLength
,
348 IN ULONG BufferOffset
,
351 NTSTATUS
VfatBlockDeviceIoControl (IN PDEVICE_OBJECT DeviceObject
,
353 IN PVOID InputBuffer
,
354 IN ULONG InputBufferSize
,
355 IN OUT PVOID OutputBuffer
,
356 IN OUT PULONG pOutputBufferSize
,
357 IN BOOLEAN Override
);
359 /* ----------------------------------------------------------- dir.c */
361 NTSTATUS
VfatDirectoryControl (PVFAT_IRP_CONTEXT
);
363 BOOL
FsdDosDateTimeToFileTime (WORD wDosDate
,
367 BOOL
FsdFileTimeToDosDateTime (TIME
*FileTime
,
371 /* -------------------------------------------------------- create.c */
373 NTSTATUS
VfatCreate (PVFAT_IRP_CONTEXT IrpContext
);
375 NTSTATUS
VfatOpenFile (PDEVICE_EXTENSION DeviceExt
,
376 PFILE_OBJECT FileObject
,
377 PUNICODE_STRING FileNameU
);
379 NTSTATUS
FindFile (PDEVICE_EXTENSION DeviceExt
,
381 PUNICODE_STRING FileToFindU
,
382 PVFAT_DIRENTRY_CONTEXT DirContext
,
385 VOID
vfat8Dot3ToString (PFAT_DIR_ENTRY pEntry
,
386 PUNICODE_STRING NameU
);
388 NTSTATUS
ReadVolumeLabel(PDEVICE_EXTENSION DeviceExt
,
391 /* --------------------------------------------------------- close.c */
393 NTSTATUS
VfatClose (PVFAT_IRP_CONTEXT IrpContext
);
395 NTSTATUS
VfatCloseFile(PDEVICE_EXTENSION DeviceExt
,
396 PFILE_OBJECT FileObject
);
398 /* ------------------------------------------------------- cleanup.c */
400 NTSTATUS
VfatCleanup (PVFAT_IRP_CONTEXT IrpContext
);
402 /* --------------------------------------------------------- fsctl.c */
404 NTSTATUS
VfatFileSystemControl (PVFAT_IRP_CONTEXT IrpContext
);
406 /* --------------------------------------------------------- finfo.c */
408 NTSTATUS
VfatQueryInformation (PVFAT_IRP_CONTEXT IrpContext
);
410 NTSTATUS
VfatSetInformation (PVFAT_IRP_CONTEXT IrpContext
);
413 VfatSetAllocationSizeInformation(PFILE_OBJECT FileObject
,
415 PDEVICE_EXTENSION DeviceExt
,
416 PLARGE_INTEGER AllocationSize
);
418 /* --------------------------------------------------------- iface.c */
420 NTSTATUS STDCALL
DriverEntry (PDRIVER_OBJECT DriverObject
,
421 PUNICODE_STRING RegistryPath
);
423 /* --------------------------------------------------------- dirwr.c */
425 NTSTATUS
VfatAddEntry (PDEVICE_EXTENSION DeviceExt
,
426 PUNICODE_STRING PathNameU
,
427 PFILE_OBJECT pFileObject
,
428 ULONG RequestedOptions
,
431 NTSTATUS
VfatUpdateEntry (PVFATFCB pFcb
);
433 NTSTATUS
VfatDelEntry(PDEVICE_EXTENSION
, PVFATFCB
);
435 /* -------------------------------------------------------- string.c */
438 vfatSplitPathName(PUNICODE_STRING PathNameU
,
439 PUNICODE_STRING DirNameU
,
440 PUNICODE_STRING FileNameU
);
442 BOOLEAN
vfatIsLongIllegal(WCHAR c
);
444 BOOLEAN
wstrcmpjoki (PWSTR s1
,
447 /* ----------------------------------------------------------- fat.c */
449 NTSTATUS
OffsetToCluster (PDEVICE_EXTENSION DeviceExt
,
455 ULONGLONG
ClusterToSector (PDEVICE_EXTENSION DeviceExt
,
458 NTSTATUS
GetNextCluster (PDEVICE_EXTENSION DeviceExt
,
459 ULONG CurrentCluster
,
463 NTSTATUS
CountAvailableClusters (PDEVICE_EXTENSION DeviceExt
,
464 PLARGE_INTEGER Clusters
);
467 WriteCluster(PDEVICE_EXTENSION DeviceExt
,
468 ULONG ClusterToWrite
,
471 /* ------------------------------------------------------ direntry.c */
473 ULONG
vfatDirEntryGetFirstCluster (PDEVICE_EXTENSION pDeviceExt
,
474 PFAT_DIR_ENTRY pDirEntry
);
476 BOOL
VfatIsDirectoryEmpty(PVFATFCB Fcb
);
478 NTSTATUS
vfatGetNextDirEntry(PVOID
* pContext
,
481 IN PVFAT_DIRENTRY_CONTEXT DirContext
,
484 /* ----------------------------------------------------------- fcb.c */
486 PVFATFCB
vfatNewFCB (PUNICODE_STRING pFileNameU
);
488 VOID
vfatDestroyFCB (PVFATFCB pFCB
);
490 VOID
vfatDestroyCCB(PVFATCCB pCcb
);
492 VOID
vfatGrabFCB (PDEVICE_EXTENSION pVCB
,
495 VOID
vfatReleaseFCB (PDEVICE_EXTENSION pVCB
,
498 VOID
vfatAddFCBToTable (PDEVICE_EXTENSION pVCB
,
501 PVFATFCB
vfatGrabFCBFromTable (PDEVICE_EXTENSION pDeviceExt
,
502 PUNICODE_STRING pFileNameU
);
504 PVFATFCB
vfatMakeRootFCB (PDEVICE_EXTENSION pVCB
);
506 PVFATFCB
vfatOpenRootFCB (PDEVICE_EXTENSION pVCB
);
508 BOOL
vfatFCBIsDirectory (PVFATFCB FCB
);
510 BOOL
vfatFCBIsRoot(PVFATFCB FCB
);
512 NTSTATUS
vfatAttachFCBToFileObject (PDEVICE_EXTENSION vcb
,
514 PFILE_OBJECT fileObject
);
516 NTSTATUS
vfatDirFindFile (PDEVICE_EXTENSION pVCB
,
518 PUNICODE_STRING FileToFindU
,
521 NTSTATUS
vfatGetFCBForFile (PDEVICE_EXTENSION pVCB
,
522 PVFATFCB
*pParentFCB
,
524 PUNICODE_STRING pFileNameU
);
526 NTSTATUS
vfatMakeFCBFromDirEntry (PVCB vcb
,
527 PVFATFCB directoryFCB
,
528 PVFAT_DIRENTRY_CONTEXT DirContext
,
531 /* ------------------------------------------------------------ rw.c */
533 NTSTATUS
VfatRead (PVFAT_IRP_CONTEXT IrpContext
);
535 NTSTATUS
VfatWrite (PVFAT_IRP_CONTEXT IrpContext
);
537 NTSTATUS
NextCluster(PDEVICE_EXTENSION DeviceExt
,
539 PULONG CurrentCluster
,
542 /* ----------------------------------------------------------- misc.c */
544 NTSTATUS
VfatQueueRequest(PVFAT_IRP_CONTEXT IrpContext
);
546 PVFAT_IRP_CONTEXT
VfatAllocateIrpContext(PDEVICE_OBJECT DeviceObject
,
549 VOID
VfatFreeIrpContext(PVFAT_IRP_CONTEXT IrpContext
);
551 NTSTATUS STDCALL
VfatBuildRequest (PDEVICE_OBJECT DeviceObject
,
554 PVOID
VfatGetUserBuffer(IN PIRP
);
556 NTSTATUS
VfatLockUserBuffer(IN PIRP
, IN ULONG
,
560 VfatSetExtendedAttributes(PFILE_OBJECT FileObject
,
563 /* ------------------------------------------------------------- flush.c */
565 NTSTATUS
VfatFlush(PVFAT_IRP_CONTEXT IrpContext
);
567 NTSTATUS
VfatFlushVolume(PDEVICE_EXTENSION DeviceExt
, PVFATFCB VolumeFcb
);