All:
[reactos.git] / reactos / drivers / fs / vfat / vfat.h
1 /* $Id: vfat.h,v 1.40 2002/03/18 22:37:13 hbirr Exp $ */
2
3 #include <ddk/ntifs.h>
4
5 struct _BootSector
6 {
7 unsigned char magic0, res0, magic1;
8 unsigned char OEMName[8];
9 unsigned short BytesPerSector;
10 unsigned char SectorsPerCluster;
11 unsigned short ReservedSectors;
12 unsigned char FATCount;
13 unsigned short RootEntries, Sectors;
14 unsigned char Media;
15 unsigned short FATSectors, SectorsPerTrack, Heads;
16 unsigned long HiddenSectors, SectorsHuge;
17 unsigned char Drive, Res1, Sig;
18 unsigned long VolumeID;
19 unsigned char VolumeLabel[11], SysType[8];
20 unsigned char Res2[450];
21 } __attribute__((packed));
22
23 struct _BootSector32
24 {
25 unsigned char magic0, res0, magic1; // 0
26 unsigned char OEMName[8]; // 3
27 unsigned short BytesPerSector; // 11
28 unsigned char SectorsPerCluster; // 13
29 unsigned short ReservedSectors; // 14
30 unsigned char FATCount; // 16
31 unsigned short RootEntries, Sectors; // 17
32 unsigned char Media; // 21
33 unsigned short FATSectors, SectorsPerTrack, Heads; // 22
34 unsigned long HiddenSectors, SectorsHuge; // 28
35 unsigned long FATSectors32; // 36
36 unsigned short ExtFlag; // 40
37 unsigned short FSVersion; // 42
38 unsigned long RootCluster; // 44
39 unsigned short FSInfoSector; // 48
40 unsigned long BootBackup; // 50
41 unsigned char Res3[10]; // 54
42 unsigned char Drive; // 64
43 unsigned char Res4; // 65
44 unsigned char ExtBootSignature; // 66
45 unsigned long VolumeID; // 67
46 unsigned char VolumeLabel[11], SysType[8]; // 71
47 unsigned char Res2[418]; // 90
48 unsigned long Signature1; // 508
49 } __attribute__((packed));
50
51 struct _BootBackupSector
52 {
53 unsigned long ExtBootSignature2; // 0
54 unsigned char Res6[480]; // 4
55 unsigned long FSINFOSignature; // 484
56 unsigned long FreeCluster; // 488
57 unsigned long NextCluster; // 492
58 unsigned char Res7[12]; // 496
59 unsigned long Signatur2; // 508
60 } __attribute__((packed));
61
62 typedef struct _BootSector BootSector;
63
64 struct _FATDirEntry
65 {
66 unsigned char Filename[8], Ext[3], Attrib, Res[2];
67 unsigned short CreationTime,CreationDate,AccessDate;
68 unsigned short FirstClusterHigh; // higher
69 unsigned short UpdateTime; //time create/update
70 unsigned short UpdateDate; //date create/update
71 unsigned short FirstCluster;
72 unsigned long FileSize;
73 } __attribute__((packed));
74
75 typedef struct _FATDirEntry FATDirEntry, FAT_DIR_ENTRY, *PFAT_DIR_ENTRY;
76
77 struct _slot
78 {
79 unsigned char id; // sequence number for slot
80 WCHAR name0_4[5]; // first 5 characters in name
81 unsigned char attr; // attribute byte
82 unsigned char reserved; // always 0
83 unsigned char alias_checksum; // checksum for 8.3 alias
84 WCHAR name5_10[6]; // 6 more characters in name
85 unsigned char start[2]; // starting cluster number
86 WCHAR name11_12[2]; // last 2 characters in name
87 } __attribute__((packed));
88
89
90 typedef struct _slot slot;
91
92 #define BLOCKSIZE 512
93
94 #define FAT16 (1)
95 #define FAT12 (2)
96 #define FAT32 (3)
97
98 #define VCB_VOLUME_LOCKED 0x0001
99 #define VCB_DISMOUNT_PENDING 0x0002
100
101 typedef struct
102 {
103 ULONG VolumeID;
104 ULONG FATStart;
105 ULONG FATCount;
106 ULONG FATSectors;
107 ULONG rootDirectorySectors;
108 ULONG rootStart;
109 ULONG dataStart;
110 ULONG RootCluster;
111 ULONG SectorsPerCluster;
112 ULONG BytesPerSector;
113 ULONG BytesPerCluster;
114 ULONG NumberOfClusters;
115 ULONG FatType;
116 }
117 FATINFO, *PFATINFO;
118
119 typedef struct
120 {
121 ERESOURCE DirResource;
122 ERESOURCE FatResource;
123
124 KSPIN_LOCK FcbListLock;
125 LIST_ENTRY FcbListHead;
126
127 PDEVICE_OBJECT StorageDevice;
128 PFILE_OBJECT FATFileObject;
129 FATINFO FatInfo;
130 ULONG LastAvailableCluster;
131 ULONG AvailableClusters;
132 BOOLEAN AvailableClustersValid;
133 ULONG Flags;
134 }
135 DEVICE_EXTENSION, *PDEVICE_EXTENSION, VCB, *PVCB;
136
137 typedef struct
138 {
139 PDRIVER_OBJECT DriverObject;
140 PDEVICE_OBJECT DeviceObject;
141 ULONG Flags;
142 }
143 VFAT_GLOBAL_DATA, *PVFAT_GLOBAL_DATA;
144
145 extern PVFAT_GLOBAL_DATA VfatGlobalData;
146
147 #define FCB_CACHE_INITIALIZED 0x0001
148 #define FCB_DELETE_PENDING 0x0002
149 #define FCB_IS_FAT 0x0004
150 #define FCB_IS_PAGE_FILE 0x0008
151
152 typedef struct _VFATFCB
153 {
154 REACTOS_COMMON_FCB_HEADER RFCB;
155 SECTION_OBJECT_POINTERS SectionObjectPointers;
156 FATDirEntry entry;
157 /* point on filename (250 chars max) in PathName */
158 WCHAR *ObjectName;
159 /* path+filename 260 max */
160 WCHAR PathName[MAX_PATH];
161 LONG RefCount;
162 PDEVICE_EXTENSION pDevExt;
163 LIST_ENTRY FcbListEntry;
164 struct _VFATFCB* parentFcb;
165 ULONG Flags;
166 PFILE_OBJECT FileObject;
167 ULONG dirIndex;
168 ERESOURCE PagingIoResource;
169 ERESOURCE MainResource;
170 ULONG TimerCount;
171
172 /* Structure members used only for paging files. */
173 ULONG FatChainSize;
174 PULONG FatChain;
175 }
176 VFATFCB, *PVFATFCB;
177
178 typedef struct _VFATCCB
179 {
180 VFATFCB * pFcb;
181 LIST_ENTRY NextCCB;
182 PFILE_OBJECT PtrFileObject;
183 LARGE_INTEGER CurrentByteOffset;
184 /* for DirectoryControl */
185 ULONG Entry;
186 /* for DirectoryControl */
187 PWCHAR DirectorySearchPattern;
188 ULONG LastCluster;
189 ULONG LastOffset;
190
191 }
192 VFATCCB, *PVFATCCB;
193
194 #define TAG(A, B, C, D) (ULONG)(((A)<<0) + ((B)<<8) + ((C)<<16) + ((D)<<24))
195
196 #define TAG_CCB TAG('V', 'C', 'C', 'B')
197
198 #define ENTRIES_PER_SECTOR (BLOCKSIZE / sizeof(FATDirEntry))
199
200 typedef struct __DOSTIME
201 {
202 WORD Second:5;
203 WORD Minute:6;
204 WORD Hour:5;
205 }
206 DOSTIME, *PDOSTIME;
207
208 typedef struct __DOSDATE
209 {
210 WORD Day:5;
211 WORD Month:4;
212 WORD Year:5;
213 }
214 DOSDATE, *PDOSDATE;
215
216 #define IRPCONTEXT_CANWAIT 0x0001
217
218 typedef struct
219 {
220 PIRP Irp;
221 PDEVICE_OBJECT DeviceObject;
222 PDEVICE_EXTENSION DeviceExt;
223 ULONG Flags;
224 WORK_QUEUE_ITEM WorkQueueItem;
225 PIO_STACK_LOCATION Stack;
226 UCHAR MajorFunction;
227 UCHAR MinorFunction;
228 PFILE_OBJECT FileObject;
229 }
230 VFAT_IRP_CONTEXT, *PVFAT_IRP_CONTEXT;
231
232 /* ------------------------------------------------------ shutdown.c */
233
234 NTSTATUS STDCALL VfatShutdown (PDEVICE_OBJECT DeviceObject,
235 PIRP Irp);
236
237 /* -------------------------------------------------------- volume.c */
238
239 NTSTATUS VfatQueryVolumeInformation (PVFAT_IRP_CONTEXT IrpContext);
240
241 NTSTATUS VfatSetVolumeInformation (PVFAT_IRP_CONTEXT IrpContext);
242
243 /* ------------------------------------------------------ blockdev.c */
244
245 NTSTATUS VfatReadSectors(IN PDEVICE_OBJECT pDeviceObject,
246 IN ULONG DiskSector,
247 IN ULONG SectorCount,
248 IN PUCHAR Buffer);
249
250 NTSTATUS VfatWriteSectors(IN PDEVICE_OBJECT pDeviceObject,
251 IN ULONG DiskSector,
252 IN ULONG SectorCount,
253 IN PUCHAR Buffer);
254
255 NTSTATUS VfatBlockDeviceIoControl (IN PDEVICE_OBJECT DeviceObject,
256 IN ULONG CtlCode,
257 IN PVOID InputBuffer,
258 IN ULONG InputBufferSize,
259 IN OUT PVOID OutputBuffer,
260 IN OUT PULONG pOutputBufferSize);
261
262 /* ----------------------------------------------------------- dir.c */
263
264 NTSTATUS VfatDirectoryControl (PVFAT_IRP_CONTEXT);
265
266 BOOL FsdDosDateTimeToFileTime (WORD wDosDate,
267 WORD wDosTime,
268 TIME *FileTime);
269
270 BOOL FsdFileTimeToDosDateTime (TIME *FileTime,
271 WORD *pwDosDate,
272 WORD *pwDosTime);
273
274 /* -------------------------------------------------------- create.c */
275
276 NTSTATUS VfatCreate (PVFAT_IRP_CONTEXT IrpContext);
277
278 NTSTATUS VfatOpenFile (PDEVICE_EXTENSION DeviceExt,
279 PFILE_OBJECT FileObject,
280 PWSTR FileName);
281
282 NTSTATUS FindFile (PDEVICE_EXTENSION DeviceExt,
283 PVFATFCB Fcb,
284 PVFATFCB Parent,
285 PWSTR FileToFind,
286 PULONG pDirIndex,
287 PULONG pDirIndex2);
288
289 VOID vfat8Dot3ToString (PCHAR pBasename,
290 PCHAR pExtension,
291 PWSTR pName);
292
293 NTSTATUS ReadVolumeLabel(PDEVICE_EXTENSION DeviceExt,
294 PVPB Vpb);
295
296 BOOLEAN IsDeletedEntry (PVOID Block,
297 ULONG Offset);
298
299 BOOLEAN IsLastEntry (PVOID Block,
300 ULONG Offset);
301
302 /* --------------------------------------------------------- close.c */
303
304 NTSTATUS VfatClose (PVFAT_IRP_CONTEXT IrpContext);
305
306 NTSTATUS VfatCloseFile(PDEVICE_EXTENSION DeviceExt,
307 PFILE_OBJECT FileObject);
308
309 /* ------------------------------------------------------- cleanup.c */
310
311 NTSTATUS VfatCleanup (PVFAT_IRP_CONTEXT IrpContext);
312
313 /* --------------------------------------------------------- fsctl.c */
314
315 NTSTATUS VfatFileSystemControl (PVFAT_IRP_CONTEXT IrpContext);
316
317 /* --------------------------------------------------------- finfo.c */
318
319 NTSTATUS VfatQueryInformation (PVFAT_IRP_CONTEXT IrpContext);
320
321 NTSTATUS VfatSetInformation (PVFAT_IRP_CONTEXT IrpContext);
322
323 /* --------------------------------------------------------- iface.c */
324
325 NTSTATUS STDCALL DriverEntry (PDRIVER_OBJECT DriverObject,
326 PUNICODE_STRING RegistryPath);
327
328 /* --------------------------------------------------------- dirwr.c */
329
330 NTSTATUS addEntry (PDEVICE_EXTENSION DeviceExt,
331 PFILE_OBJECT pFileObject,
332 ULONG RequestedOptions,UCHAR ReqAttr);
333
334 NTSTATUS updEntry (PDEVICE_EXTENSION DeviceExt,
335 PFILE_OBJECT pFileObject);
336
337 NTSTATUS delEntry(PDEVICE_EXTENSION,
338 PFILE_OBJECT);
339
340 /* -------------------------------------------------------- string.c */
341
342 VOID vfat_initstr (wchar_t *wstr,
343 ULONG wsize);
344
345 wchar_t* vfat_wcsncat (wchar_t * dest,
346 const wchar_t * src,
347 size_t wstart,
348 size_t wcount);
349
350 wchar_t* vfat_wcsncpy (wchar_t * dest,
351 const wchar_t *src,
352 size_t wcount);
353
354 wchar_t* vfat_movstr (wchar_t *src,
355 ULONG dpos,
356 ULONG spos,
357 ULONG len);
358
359 BOOLEAN wstrcmpi (PWSTR s1,
360 PWSTR s2);
361
362 BOOLEAN wstrcmpjoki (PWSTR s1,
363 PWSTR s2);
364
365 PWCHAR vfatGetNextPathElement (PWCHAR pFileName);
366
367 VOID vfatWSubString (PWCHAR pTarget,
368 const PWCHAR pSource,
369 size_t pLength);
370
371 BOOL vfatIsFileNameValid (PWCHAR pFileName);
372
373 /* ----------------------------------------------------------- fat.c */
374
375 NTSTATUS OffsetToCluster (PDEVICE_EXTENSION DeviceExt,
376 PVFATFCB Fcb,
377 ULONG FirstCluster,
378 ULONG FileOffset,
379 PULONG Cluster,
380 BOOLEAN Extend);
381
382 ULONG ClusterToSector (PDEVICE_EXTENSION DeviceExt,
383 ULONG Cluster);
384
385 NTSTATUS GetNextCluster (PDEVICE_EXTENSION DeviceExt,
386 ULONG CurrentCluster,
387 PULONG NextCluster,
388 BOOLEAN Extend);
389
390 NTSTATUS GetNextSector (PDEVICE_EXTENSION DeviceExt,
391 ULONG CurrentSector,
392 PULONG NextSector,
393 BOOLEAN Extend);
394
395 NTSTATUS VfatRawReadCluster (PDEVICE_EXTENSION DeviceExt,
396 ULONG FirstCluster,
397 PVOID Buffer,
398 ULONG Cluster,
399 ULONG Count);
400
401 NTSTATUS VfatRawWriteCluster (PDEVICE_EXTENSION DeviceExt,
402 ULONG FirstCluster,
403 PVOID Buffer,
404 ULONG Cluster,
405 ULONG Count);
406
407 NTSTATUS CountAvailableClusters (PDEVICE_EXTENSION DeviceExt,
408 PLARGE_INTEGER Clusters);
409
410 /* ------------------------------------------------------ direntry.c */
411
412 ULONG vfatDirEntryGetFirstCluster (PDEVICE_EXTENSION pDeviceExt,
413 PFAT_DIR_ENTRY pDirEntry);
414
415 BOOL vfatIsDirEntryDeleted (FATDirEntry * pFatDirEntry);
416
417 BOOL vfatIsDirEntryVolume (FATDirEntry * pFatDirEntry);
418
419 BOOL vfatIsDirEntryEndMarker (FATDirEntry * pFatDirEntry);
420
421 VOID vfatGetDirEntryName (PFAT_DIR_ENTRY pDirEntry,
422 PWSTR pEntryName);
423
424 NTSTATUS vfatGetNextDirEntry (PDEVICE_EXTENSION pDeviceExt,
425 PVFATFCB pDirectoryFCB,
426 ULONG * pDirectoryIndex,
427 PWSTR pLongFileName,
428 PFAT_DIR_ENTRY pDirEntry);
429
430 /* ----------------------------------------------------------- fcb.c */
431
432 PVFATFCB vfatNewFCB (PWCHAR pFileName);
433
434 VOID vfatDestroyFCB (PVFATFCB pFCB);
435
436 VOID vfatGrabFCB (PDEVICE_EXTENSION pVCB,
437 PVFATFCB pFCB);
438
439 VOID vfatReleaseFCB (PDEVICE_EXTENSION pVCB,
440 PVFATFCB pFCB);
441
442 VOID vfatAddFCBToTable (PDEVICE_EXTENSION pVCB,
443 PVFATFCB pFCB);
444
445 PVFATFCB vfatGrabFCBFromTable (PDEVICE_EXTENSION pDeviceExt,
446 PWSTR pFileName);
447
448 PVFATFCB vfatMakeRootFCB (PDEVICE_EXTENSION pVCB);
449
450 PVFATFCB vfatOpenRootFCB (PDEVICE_EXTENSION pVCB);
451
452 BOOL vfatFCBIsDirectory (PDEVICE_EXTENSION pVCB,
453 PVFATFCB FCB);
454
455 NTSTATUS vfatAttachFCBToFileObject (PDEVICE_EXTENSION vcb,
456 PVFATFCB fcb,
457 PFILE_OBJECT fileObject);
458
459 NTSTATUS vfatDirFindFile (PDEVICE_EXTENSION pVCB,
460 PVFATFCB parentFCB,
461 PWSTR elementName,
462 PVFATFCB * fileFCB);
463
464 NTSTATUS vfatGetFCBForFile (PDEVICE_EXTENSION pVCB,
465 PVFATFCB *pParentFCB,
466 PVFATFCB *pFCB,
467 const PWSTR pFileName);
468
469 NTSTATUS vfatMakeFCBFromDirEntry (PVCB vcb,
470 PVFATFCB directoryFCB,
471 PWSTR longName,
472 PFAT_DIR_ENTRY dirEntry,
473 ULONG dirIndex,
474 PVFATFCB * fileFCB);
475
476 /* ------------------------------------------------------------ rw.c */
477
478 NTSTATUS VfatRead (PVFAT_IRP_CONTEXT IrpContext);
479
480 NTSTATUS VfatWrite (PVFAT_IRP_CONTEXT IrpContext);
481
482 NTSTATUS vfatExtendSpace (PDEVICE_EXTENSION pDeviceExt,
483 PFILE_OBJECT pFileObject,
484 ULONG NewSize);
485 /*
486 NTSTATUS VfatWriteFile (PDEVICE_EXTENSION DeviceExt,
487 PFILE_OBJECT FileObject,
488 PVOID Buffer,
489 ULONG Length,
490 ULONG WriteOffset,
491 BOOLEAN NoCache,
492 BOOLEAN PageIo);
493
494
495 NTSTATUS VfatReadFile (PDEVICE_EXTENSION DeviceExt,
496 PFILE_OBJECT FileObject,
497 PVOID Buffer, ULONG Length,
498 ULONG ReadOffset,
499 PULONG LengthRead,
500 ULONG NoCache);
501 */
502
503 NTSTATUS NextCluster(PDEVICE_EXTENSION DeviceExt,
504 PVFATFCB Fcb,
505 ULONG FirstCluster,
506 PULONG CurrentCluster,
507 BOOLEAN Extend);
508
509 /* ----------------------------------------------------------- misc.c */
510
511 NTSTATUS VfatQueueRequest(PVFAT_IRP_CONTEXT IrpContext);
512
513 PVFAT_IRP_CONTEXT VfatAllocateIrpContext(PDEVICE_OBJECT DeviceObject,
514 PIRP Irp);
515
516 VOID VfatFreeIrpContext(PVFAT_IRP_CONTEXT IrpContext);
517
518 NTSTATUS STDCALL VfatBuildRequest (PDEVICE_OBJECT DeviceObject,
519 PIRP Irp);
520 /*
521 PVOID VfatGetUserBuffer(IN PIRP);
522
523 NTSTATUS VfatLockUserBuffer(IN PIRP, IN ULONG,
524 IN LOCK_OPERATION);
525 */
526
527 /* EOF */