3782c011a86b6dddb25a2c47a35026ff124aefb8
[reactos.git] / reactos / drivers / filesystems / ntfs / ntfs.h
1 #ifndef NTFS_H
2 #define NTFS_H
3
4 #include <ntifs.h>
5
6 #define CACHEPAGESIZE(pDeviceExt) \
7 ((pDeviceExt)->NtfsInfo.UCHARsPerCluster > PAGE_SIZE ? \
8 (pDeviceExt)->NtfsInfo.UCHARsPerCluster : PAGE_SIZE)
9
10 #define TAG_NTFS 'SFTN'
11
12 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
13
14 #define DEVICE_NAME L"\\Ntfs"
15
16 #include <pshpack1.h>
17 typedef struct _BIOS_PARAMETERS_BLOCK
18 {
19 USHORT BytesPerSector; // 0x0B
20 UCHAR SectorsPerCluster; // 0x0D
21 UCHAR Unused0[7]; // 0x0E, checked when volume is mounted
22 UCHAR MediaId; // 0x15
23 UCHAR Unused1[2]; // 0x16
24 USHORT SectorsPerTrack; // 0x18
25 USHORT Heads; // 0x1A
26 UCHAR Unused2[4]; // 0x1C
27 UCHAR Unused3[4]; // 0x20, checked when volume is mounted
28 } BIOS_PARAMETERS_BLOCK, *PBIOS_PARAMETERS_BLOCK;
29
30 typedef struct _EXTENDED_BIOS_PARAMETERS_BLOCK
31 {
32 USHORT Unknown[2]; // 0x24, always 80 00 80 00
33 ULONGLONG SectorCount; // 0x28
34 ULONGLONG MftLocation; // 0x30
35 ULONGLONG MftMirrLocation; // 0x38
36 CHAR ClustersPerMftRecord; // 0x40
37 UCHAR Unused4[3]; // 0x41
38 CHAR ClustersPerIndexRecord; // 0x44
39 UCHAR Unused5[3]; // 0x45
40 ULONGLONG SerialNumber; // 0x48
41 UCHAR Checksum[4]; // 0x50
42 } EXTENDED_BIOS_PARAMETERS_BLOCK, *PEXTENDED_BIOS_PARAMETERS_BLOCK;
43
44 typedef struct _BOOT_SECTOR
45 {
46 UCHAR Jump[3]; // 0x00
47 UCHAR OEMID[8]; // 0x03
48 BIOS_PARAMETERS_BLOCK BPB;
49 EXTENDED_BIOS_PARAMETERS_BLOCK EBPB;
50 UCHAR BootStrap[426]; // 0x54
51 USHORT EndSector; // 0x1FE
52 } BOOT_SECTOR, *PBOOT_SECTOR;
53 #include <poppack.h>
54
55 //typedef struct _BootSector BootSector;
56
57 typedef struct _NTFS_INFO
58 {
59 ULONG BytesPerSector;
60 ULONG SectorsPerCluster;
61 ULONG BytesPerCluster;
62 ULONGLONG SectorCount;
63 ULARGE_INTEGER MftStart;
64 ULARGE_INTEGER MftMirrStart;
65 ULONG BytesPerFileRecord;
66
67 ULONGLONG SerialNumber;
68 USHORT VolumeLabelLength;
69 WCHAR VolumeLabel[MAXIMUM_VOLUME_LABEL_LENGTH];
70 UCHAR MajorVersion;
71 UCHAR MinorVersion;
72 USHORT Flags;
73
74 } NTFS_INFO, *PNTFS_INFO;
75
76 #define NTFS_TYPE_CCB '20SF'
77 #define NTFS_TYPE_FCB '30SF'
78 #define NTFS_TYPE_VCB '50SF'
79 #define NTFS_TYPE_IRP_CONTEST '60SF'
80 #define NTFS_TYPE_GLOBAL_DATA '70SF'
81
82 typedef struct
83 {
84 ULONG Type;
85 ULONG Size;
86 } NTFSIDENTIFIER, *PNTFSIDENTIFIER;
87
88
89 typedef struct
90 {
91 NTFSIDENTIFIER Identifier;
92
93 ERESOURCE DirResource;
94 // ERESOURCE FatResource;
95
96 KSPIN_LOCK FcbListLock;
97 LIST_ENTRY FcbListHead;
98
99 PVPB Vpb;
100 PDEVICE_OBJECT StorageDevice;
101 PFILE_OBJECT StreamFileObject;
102
103 NTFS_INFO NtfsInfo;
104
105 } DEVICE_EXTENSION, *PDEVICE_EXTENSION, NTFS_VCB, *PNTFS_VCB;
106
107
108 #define FCB_CACHE_INITIALIZED 0x0001
109 #define FCB_IS_VOLUME_STREAM 0x0002
110 #define FCB_IS_VOLUME 0x0004
111 #define MAX_PATH 260
112
113 typedef struct _FCB
114 {
115 NTFSIDENTIFIER Identifier;
116
117 FSRTL_COMMON_FCB_HEADER RFCB;
118 SECTION_OBJECT_POINTERS SectionObjectPointers;
119
120 PFILE_OBJECT FileObject;
121 PNTFS_VCB Vcb;
122
123 WCHAR *ObjectName; /* point on filename (250 chars max) in PathName */
124 WCHAR PathName[MAX_PATH]; /* path+filename 260 max */
125
126 ERESOURCE PagingIoResource;
127 ERESOURCE MainResource;
128
129 LIST_ENTRY FcbListEntry;
130 struct _FCB* ParentFcb;
131
132 ULONG DirIndex;
133
134 LONG RefCount;
135 ULONG Flags;
136
137 // DIR_RECORD Entry;
138
139 } NTFS_FCB, *PNTFS_FCB;
140
141
142 typedef struct
143 {
144 NTFSIDENTIFIER Identifier;
145 LIST_ENTRY NextCCB;
146 PFILE_OBJECT PtrFileObject;
147 LARGE_INTEGER CurrentByteOffset;
148 /* for DirectoryControl */
149 ULONG Entry;
150 /* for DirectoryControl */
151 PWCHAR DirectorySearchPattern;
152 ULONG LastCluster;
153 ULONG LastOffset;
154 } NTFS_CCB, *PNTFS_CCB;
155
156 #define TAG_CCB 'BCCI'
157
158 typedef struct
159 {
160 NTFSIDENTIFIER Identifier;
161 ERESOURCE Resource;
162 PDRIVER_OBJECT DriverObject;
163 PDEVICE_OBJECT DeviceObject;
164 CACHE_MANAGER_CALLBACKS CacheMgrCallbacks;
165 ULONG Flags;
166 } NTFS_GLOBAL_DATA, *PNTFS_GLOBAL_DATA;
167
168
169 typedef enum
170 {
171 AttributeStandardInformation = 0x10,
172 AttributeAttributeList = 0x20,
173 AttributeFileName = 0x30,
174 AttributeObjectId = 0x40,
175 AttributeSecurityDescriptor = 0x50,
176 AttributeVolumeName = 0x60,
177 AttributeVolumeInformation = 0x70,
178 AttributeData = 0x80,
179 AttributeIndexRoot = 0x90,
180 AttributeIndexAllocation = 0xA0,
181 AttributeBitmap = 0xB0,
182 AttributeReparsePoint = 0xC0,
183 AttributeEAInformation = 0xD0,
184 AttributeEA = 0xE0,
185 AttributePropertySet = 0xF0,
186 AttributeLoggedUtilityStream = 0x100
187 } ATTRIBUTE_TYPE, *PATTRIBUTE_TYPE;
188
189 #define COLLATION_BINARY 0x00
190 #define COLLATION_FILE_NAME 0x01
191 #define COLLATION_UNICODE_STRING 0x02
192 #define COLLATION_NTOFS_ULONG 0x10
193 #define COLLATION_NTOFS_SID 0x11
194 #define COLLATION_NTOFS_SECURITY_HASH 0x12
195 #define COLLATION_NTOFS_ULONGS 0x13
196
197 #define INDEX_ROOT_SMALL 0x0
198 #define INDEX_ROOT_LARGE 0x1
199
200 typedef struct
201 {
202 ULONG Type; /* Magic number 'FILE' */
203 USHORT UsaOffset; /* Offset to the update sequence */
204 USHORT UsaCount; /* Size in words of Update Sequence Number & Array (S) */
205 ULONGLONG Lsn; /* $LogFile Sequence Number (LSN) */
206 } NTFS_RECORD_HEADER, *PNTFS_RECORD_HEADER;
207
208 /* NTFS_RECORD_HEADER.Type */
209 #define NRH_FILE_TYPE 0x454C4946 /* 'FILE' */
210
211
212 typedef struct
213 {
214 NTFS_RECORD_HEADER Ntfs;
215 USHORT SequenceNumber; /* Sequence number */
216 USHORT LinkCount; /* Hard link count */
217 USHORT AttributeOffset; /* Offset to the first Attribute */
218 USHORT Flags; /* Flags */
219 ULONG BytesInUse; /* Real size of the FILE record */
220 ULONG BytesAllocated; /* Allocated size of the FILE record */
221 ULONGLONG BaseFileRecord; /* File reference to the base FILE record */
222 USHORT NextAttributeNumber; /* Next Attribute Id */
223 USHORT Pading; /* Align to 4 UCHAR boundary (XP) */
224 ULONG MFTRecordNumber; /* Number of this MFT Record (XP) */
225 } FILE_RECORD_HEADER, *PFILE_RECORD_HEADER;
226
227 /* Flags in FILE_RECORD_HEADER */
228
229 #define FRH_IN_USE 0x0001 /* Record is in use */
230 #define FRH_DIRECTORY 0x0002 /* Record is a directory */
231 #define FRH_UNKNOWN1 0x0004 /* Don't know */
232 #define FRH_UNKNOWN2 0x0008 /* Don't know */
233
234 typedef struct
235 {
236 ATTRIBUTE_TYPE AttributeType;
237 ULONG Length;
238 BOOLEAN Nonresident;
239 UCHAR NameLength;
240 USHORT NameOffset;
241 USHORT Flags;
242 USHORT AttributeNumber;
243 } ATTRIBUTE, *PATTRIBUTE;
244
245 typedef struct
246 {
247 ATTRIBUTE Attribute;
248 ULONG ValueLength;
249 USHORT ValueOffset;
250 UCHAR Flags;
251 // UCHAR Padding0;
252 } RESIDENT_ATTRIBUTE, *PRESIDENT_ATTRIBUTE;
253
254 typedef struct
255 {
256 ATTRIBUTE Attribute;
257 ULONGLONG StartVcn; // LowVcn
258 ULONGLONG LastVcn; // HighVcn
259 USHORT RunArrayOffset;
260 USHORT CompressionUnit;
261 ULONG Padding0;
262 UCHAR IndexedFlag;
263 ULONGLONG AllocatedSize;
264 ULONGLONG DataSize;
265 ULONGLONG InitializedSize;
266 ULONGLONG CompressedSize;
267 } NONRESIDENT_ATTRIBUTE, *PNONRESIDENT_ATTRIBUTE;
268
269
270 typedef struct
271 {
272 ULONGLONG CreationTime;
273 ULONGLONG ChangeTime;
274 ULONGLONG LastWriteTime;
275 ULONGLONG LastAccessTime;
276 ULONG FileAttribute;
277 ULONG AlignmentOrReserved[3];
278 #if 0
279 ULONG QuotaId;
280 ULONG SecurityId;
281 ULONGLONG QuotaCharge;
282 USN Usn;
283 #endif
284 } STANDARD_INFORMATION, *PSTANDARD_INFORMATION;
285
286
287 typedef struct
288 {
289 ATTRIBUTE_TYPE AttributeType;
290 USHORT Length;
291 UCHAR NameLength;
292 UCHAR NameOffset;
293 ULONGLONG StartVcn; // LowVcn
294 ULONGLONG FileReferenceNumber;
295 USHORT AttributeNumber;
296 USHORT AlignmentOrReserved[3];
297 } ATTRIBUTE_LIST, *PATTRIBUTE_LIST;
298
299
300 typedef struct
301 {
302 ULONGLONG DirectoryFileReferenceNumber;
303 ULONGLONG CreationTime;
304 ULONGLONG ChangeTime;
305 ULONGLONG LastWriteTime;
306 ULONGLONG LastAccessTime;
307 ULONGLONG AllocatedSize;
308 ULONGLONG DataSize;
309 ULONG FileAttributes;
310 ULONG AlignmentOrReserved;
311 UCHAR NameLength;
312 UCHAR NameType;
313 WCHAR Name[1];
314 } FILENAME_ATTRIBUTE, *PFILENAME_ATTRIBUTE;
315
316 typedef struct
317 {
318 ULONG FirstEntryOffset;
319 ULONG TotalSizeOfEntries;
320 ULONG AllocatedSize;
321 UCHAR Flags;
322 UCHAR Padding[3];
323 } INDEX_HEADER_ATTRIBUTE, *PINDEX_HEADER_ATTRIBUTE;
324
325 typedef struct
326 {
327 ULONG AttributeType;
328 ULONG CollationRule;
329 ULONG SizeOfEntry;
330 UCHAR ClustersPerIndexRecord;
331 UCHAR Padding[3];
332 INDEX_HEADER_ATTRIBUTE Header;
333 } INDEX_ROOT_ATTRIBUTE, *PINDEX_ROOT_ATTRIBUTE;
334
335 typedef struct
336 {
337 ULONGLONG Unknown1;
338 UCHAR MajorVersion;
339 UCHAR MinorVersion;
340 USHORT Flags;
341 ULONG Unknown2;
342 } VOLINFO_ATTRIBUTE, *PVOLINFO_ATTRIBUTE;
343
344 typedef struct
345 {
346 NTFSIDENTIFIER Identifier;
347 ULONG Flags;
348 UCHAR MajorFunction;
349 UCHAR MinorFunction;
350 WORK_QUEUE_ITEM WorkQueueItem;
351 PIRP Irp;
352 BOOLEAN IsTopLevel;
353 PDEVICE_OBJECT DeviceObject;
354 NTSTATUS SavedExceptionCode;
355 } NTFS_IRP_CONTEXT, *PNTFS_IRP_CONTEXT;
356
357
358 extern PNTFS_GLOBAL_DATA NtfsGlobalData;
359
360 //int CdfsStrcmpi( wchar_t *str1, wchar_t *str2 );
361 //void CdfsWstrcpy( wchar_t *str1, wchar_t *str2, int max );
362
363
364 /* attrib.c */
365
366 //VOID
367 //NtfsDumpAttribute(PATTRIBUTE Attribute);
368
369 //LONGLONG RunLCN(PUCHAR run);
370
371 //ULONG RunLength(PUCHAR run);
372
373 BOOLEAN
374 FindRun(PNONRESIDENT_ATTRIBUTE NresAttr,
375 ULONGLONG vcn,
376 PULONGLONG lcn,
377 PULONGLONG count);
378
379 VOID
380 NtfsDumpFileAttributes(PFILE_RECORD_HEADER FileRecord);
381
382
383 /* blockdev.c */
384
385 NTSTATUS
386 NtfsReadSectors(IN PDEVICE_OBJECT DeviceObject,
387 IN ULONG DiskSector,
388 IN ULONG SectorCount,
389 IN ULONG SectorSize,
390 IN OUT PUCHAR Buffer,
391 IN BOOLEAN Override);
392
393 NTSTATUS
394 NtfsDeviceIoControl(IN PDEVICE_OBJECT DeviceObject,
395 IN ULONG ControlCode,
396 IN PVOID InputBuffer,
397 IN ULONG InputBufferSize,
398 IN OUT PVOID OutputBuffer,
399 IN OUT PULONG OutputBufferSize,
400 IN BOOLEAN Override);
401
402
403 /* close.c */
404
405 DRIVER_DISPATCH NtfsFsdClose;
406 NTSTATUS NTAPI
407 NtfsFsdClose(PDEVICE_OBJECT DeviceObject,
408 PIRP Irp);
409
410
411 /* create.c */
412
413 DRIVER_DISPATCH NtfsFsdCreate;
414 NTSTATUS NTAPI
415 NtfsFsdCreate(PDEVICE_OBJECT DeviceObject,
416 PIRP Irp);
417
418
419 /* dirctl.c */
420
421 DRIVER_DISPATCH NtfsFsdDirectoryControl;
422 NTSTATUS NTAPI
423 NtfsFsdDirectoryControl(PDEVICE_OBJECT DeviceObject,
424 PIRP Irp);
425
426
427 /* dispatch.c */
428
429 DRIVER_DISPATCH NtfsFsdDispatch;
430 NTSTATUS NTAPI
431 NtfsFsdDispatch(PDEVICE_OBJECT DeviceObject,
432 PIRP Irp);
433
434
435 /* fastio.c */
436
437 BOOLEAN NTAPI
438 NtfsAcqLazyWrite(PVOID Context,
439 BOOLEAN Wait);
440
441 VOID NTAPI
442 NtfsRelLazyWrite(PVOID Context);
443
444 BOOLEAN NTAPI
445 NtfsAcqReadAhead(PVOID Context,
446 BOOLEAN Wait);
447
448 VOID NTAPI
449 NtfsRelReadAhead(PVOID Context);
450
451
452 /* fcb.c */
453
454 PNTFS_FCB
455 NtfsCreateFCB(PCWSTR FileName,
456 PNTFS_VCB Vcb);
457
458 VOID
459 NtfsDestroyFCB(PNTFS_FCB Fcb);
460
461 BOOLEAN
462 NtfsFCBIsDirectory(PNTFS_FCB Fcb);
463
464 BOOLEAN
465 NtfsFCBIsRoot(PNTFS_FCB Fcb);
466
467 VOID
468 NtfsGrabFCB(PNTFS_VCB Vcb,
469 PNTFS_FCB Fcb);
470
471 VOID
472 NtfsReleaseFCB(PNTFS_VCB Vcb,
473 PNTFS_FCB Fcb);
474
475 VOID
476 NtfsAddFCBToTable(PNTFS_VCB Vcb,
477 PNTFS_FCB Fcb);
478
479 PNTFS_FCB
480 NtfsGrabFCBFromTable(PNTFS_VCB Vcb,
481 PCWSTR FileName);
482
483 NTSTATUS
484 NtfsFCBInitializeCache(PNTFS_VCB Vcb,
485 PNTFS_FCB Fcb);
486
487 PNTFS_FCB
488 NtfsMakeRootFCB(PNTFS_VCB Vcb);
489
490 PNTFS_FCB
491 NtfsOpenRootFCB(PNTFS_VCB Vcb);
492
493 NTSTATUS
494 NtfsAttachFCBToFileObject(PNTFS_VCB Vcb,
495 PNTFS_FCB Fcb,
496 PFILE_OBJECT FileObject);
497
498 NTSTATUS
499 NtfsGetFCBForFile(PNTFS_VCB Vcb,
500 PNTFS_FCB *pParentFCB,
501 PNTFS_FCB *pFCB,
502 const PWSTR pFileName);
503
504
505 /* finfo.c */
506
507 DRIVER_DISPATCH NtfsFsdQueryInformation;
508 NTSTATUS NTAPI
509 NtfsFsdQueryInformation(PDEVICE_OBJECT DeviceObject,
510 PIRP Irp);
511
512
513 /* fsctl.c */
514
515 DRIVER_DISPATCH NtfsFsdFileSystemControl;
516 NTSTATUS NTAPI
517 NtfsFsdFileSystemControl(PDEVICE_OBJECT DeviceObject,
518 PIRP Irp);
519
520
521 /* mft.c */
522 NTSTATUS
523 NtfsOpenMft(PDEVICE_EXTENSION Vcb);
524
525
526 VOID
527 ReadAttribute(PATTRIBUTE attr,
528 PVOID buffer,
529 PDEVICE_EXTENSION Vcb,
530 PDEVICE_OBJECT DeviceObject);
531
532 ULONG
533 AttributeDataLength(PATTRIBUTE attr);
534
535 ULONG
536 AttributeAllocatedLength(PATTRIBUTE Attribute);
537
538 NTSTATUS
539 ReadFileRecord(PDEVICE_EXTENSION Vcb,
540 ULONG index,
541 PFILE_RECORD_HEADER file,
542 PFILE_RECORD_HEADER Mft);
543
544 PATTRIBUTE
545 FindAttribute(PFILE_RECORD_HEADER file,
546 ATTRIBUTE_TYPE type,
547 PWSTR name);
548
549 ULONG
550 AttributeLengthAllocated(PATTRIBUTE attr);
551
552 VOID
553 ReadVCN(PDEVICE_EXTENSION Vcb,
554 PFILE_RECORD_HEADER file,
555 ATTRIBUTE_TYPE type,
556 ULONGLONG vcn,
557 ULONG count,
558 PVOID buffer);
559
560 VOID
561 FixupUpdateSequenceArray(PFILE_RECORD_HEADER file);
562
563 VOID
564 ReadExternalAttribute(PDEVICE_EXTENSION Vcb,
565 PNONRESIDENT_ATTRIBUTE NresAttr,
566 ULONGLONG vcn,
567 ULONG count,
568 PVOID buffer);
569
570 NTSTATUS
571 ReadLCN(PDEVICE_EXTENSION Vcb,
572 ULONGLONG lcn,
573 ULONG count,
574 PVOID buffer);
575
576 VOID
577 EnumerAttribute(PFILE_RECORD_HEADER file,
578 PDEVICE_EXTENSION Vcb,
579 PDEVICE_OBJECT DeviceObject);
580
581
582 /* misc.c */
583
584 BOOLEAN
585 NtfsIsIrpTopLevel(PIRP Irp);
586
587 PNTFS_IRP_CONTEXT
588 NtfsAllocateIrpContext(PDEVICE_OBJECT DeviceObject,
589 PIRP Irp);
590
591 #if 0
592 BOOLEAN
593 wstrcmpjoki(PWSTR s1, PWSTR s2);
594
595 VOID
596 CdfsSwapString(PWCHAR Out,
597 PUCHAR In,
598 ULONG Count);
599
600 VOID
601 CdfsDateTimeToFileTime(PFCB Fcb,
602 TIME *FileTime);
603
604 VOID
605 CdfsFileFlagsToAttributes(PFCB Fcb,
606 PULONG FileAttributes);
607 #endif
608
609
610 /* rw.c */
611
612 DRIVER_DISPATCH NtfsFsdRead;
613 NTSTATUS NTAPI
614 NtfsFsdRead(PDEVICE_OBJECT DeviceObject,
615 PIRP Irp);
616
617 DRIVER_DISPATCH NtfsFsdWrite;
618 NTSTATUS NTAPI
619 NtfsFsdWrite(PDEVICE_OBJECT DeviceObject,
620 PIRP Irp);
621
622
623 /* volinfo.c */
624
625 NTSTATUS
626 NtfsQueryVolumeInformation(PNTFS_IRP_CONTEXT IrpContext);
627
628 NTSTATUS
629 NtfsSetVolumeInformation(PNTFS_IRP_CONTEXT IrpContext);
630
631
632 /* ntfs.c */
633
634 DRIVER_INITIALIZE DriverEntry;
635
636 VOID
637 NTAPI
638 NtfsInitializeFunctionPointers(PDRIVER_OBJECT DriverObject);
639
640 #endif /* NTFS_H */