Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2580 lines
88 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. Ntfs.h
  5. Current Version Numbers:
  6. Major.Minor Version: 3.1
  7. Abstract:
  8. This module defines the on-disk structure of the Ntfs file system.
  9. An Ntfs volume consists of sectors of data allocated on a granularity
  10. called a cluster. The cluster factor is the number of sectors per
  11. cluster. Valid cluster factors are 1, 2, 4, 8, etc.
  12. The Ntfs volume starts with a boot sector at LBN=0, and a duplicate
  13. boot sector at LBN=(number of sectors on the partition div 2). So
  14. a disk with N sectors start with two boot sectors as illustrated.
  15. 0 ... N/2 ... N
  16. +-----------+-------+------------+-------+------------+
  17. |BootSector | ... | BootSector | ... | |
  18. +-----------+-------+------------+-------+------------+
  19. The boot sector gives you the standard Bios Parameter Block, and
  20. tells you how many sectors are in the volume, and gives you the starting
  21. LCNs of the master file table (mft) and the duplicate master file table
  22. (mft2).
  23. The master file table contains the file record segments for all of
  24. the volume. The first 16 or so file record segments are reserved for
  25. special files. Mft2 only mirrors the first three record segments.
  26. 0 1 2 3 4 5 6 7 8 9 ...
  27. +---+---+---+---+---+---+---+---+---+---+-----+
  28. | M | M | L | V | A | R | B | B | B | Q | |
  29. | f | f | o | o | t | o | i | o | a | u | |
  30. | t | t | g | l | t | o | t | o | d | o | |
  31. | | 2 | F | D | r | t | M | t | C | t | ... |
  32. | | | i | a | D | D | a | | l | a | |
  33. | | | l | s | e | i | p | | u | | |
  34. | | | e | d | f | r | | | s | | |
  35. +---+---+---+---+---+---+---+---+---+---+-----+
  36. Each file record segment starts with a file record segment header, and
  37. is followed by one or more attributes. Each attribute starts with an
  38. attribute record header. The attribute record denotes the attribute type,
  39. optional name, and value for the attribute. If the attribute is resident
  40. the value is contained in the file record and immediately follows the
  41. attribute record header. If the attribute is non-resident the value
  42. value is off in some other sectors on the disk.
  43. +---------+-----------------+-------+
  44. | File | Attrib : Name | |
  45. | Record | Record : and/or | ... |
  46. | Segment | Header : Attrib | |
  47. | Header | : Data | |
  48. +---------+-----------------+-------+
  49. Now if we run out of space for storing attributes in the file record
  50. segment we allocate additional file record segments and insert in the
  51. first (or base) file record segment an attribute called the Attribute
  52. List. The attribute list indicates for every attribute associated
  53. with the file where the attribute can be found. This includes
  54. those in the base file record.
  55. The value part of the attribute we're calling the attribute list is
  56. a list of sorted attribute list entries. Though illustrated
  57. here as resident the attribute list can be nonresident.
  58. +---------+---------------------------+-----------------+-------+
  59. | File | Attrib : Attrib : | Attrib : Name | |
  60. | Record | Record : List : ... | Record : and/or | ... |
  61. | Segment | Header : Entry : | Header : Attrib | |
  62. | Header | : : | : Data | |
  63. +---------+---------------------------+-----------------+-------+
  64. |
  65. V
  66. +---------+-----------------+-------+
  67. | File | Attrib : Name | |
  68. | Record | Record : and/or | ... |
  69. | Segment | Header : Attrib | |
  70. | Header | : Data | |
  71. +---------+-----------------+-------+
  72. This file defines all of the above structures and also lays out the
  73. structures for some predefined attributes values (e.g., standard
  74. information, etc).
  75. Attributes are stored in ascending order in the file record and
  76. attribute list. The sorting is done by first sorting according to
  77. attribute type code, then attribute name, and lastly attribute value.
  78. NTFS guarantees that if two attributes of the same type code and
  79. name exist on a file then they must have different values, and the
  80. values must be resident.
  81. The indexing attributes are the last interesting attribute. The data
  82. component of an index root attribute must always be resident and contains
  83. an index header followed by a list of index list entries.
  84. +--------+------------------------+
  85. | Attrib | : Index : |
  86. | Record | Index : List : ... |
  87. | Header | Header : Entry : |
  88. | | : : |
  89. +--------+------------------------+
  90. Each index list entry contains the key for the index and a reference
  91. to the file record segment for the index. If ever we need to spill
  92. out of the file record segment we allocate an additional cluster from
  93. the disk (not file record segments). The storage for the additional
  94. clusters is the data part of an index allocation attribute. So what
  95. we wind up with is an index allocation attribute (non-resident) consisting
  96. of Index Allocation Buffers which are referenced by the b-tree used in in
  97. the index.
  98. +--------+------------------------+-----+---------------------+
  99. | Attrib | Index : Index : | | Attrib : Index |
  100. | Record | List : List : ... | ... | Record : Allocation |
  101. | Header | Header : Entry : | | Header : |
  102. | | : : | | : |
  103. +--------+------------------------+-----+---------------------+
  104. |
  105. | (VCN within index allocation)
  106. V
  107. +------------+------------------------------+
  108. | Index | : Index : Index : |
  109. | Allocation | Index : List : List : ... |
  110. | Buffer | Header: Entry : Entry : |
  111. | | : : : |
  112. +------------+------------------------------+
  113. Resident attributes are straight forward. Non-resident attributes require
  114. a little more work. If the attribute is non-resident then following
  115. the attribute record header is a list of retrieval information giving a
  116. VCN to LCN mapping for the attribute. In the figure above the
  117. Index allocation attribute is a a non-resident attribute
  118. +---------+----------------------+-------+
  119. | File | Attrib : Retrieval | |
  120. | Record | Record : Information | ... |
  121. | Segment | Header : | |
  122. | Header | : | |
  123. +---------+----------------------+-------+
  124. If the retrieval information does not fit in the base file segment then
  125. it can be stored in an external file record segment all by itself, and
  126. if in the still doesn't fit in one external file record segment then
  127. there is a provision in the attribute list to contain multiple
  128. entries for an attribute that needs additional retrieval information.
  129. Author:
  130. Brian Andrew [BrianAn] 21-May-1991
  131. David Goebel [DavidGoe]
  132. Gary Kimura [GaryKi]
  133. Tom Miller [TomM]
  134. Revision History:
  135. IMPORTANT NOTE:
  136. The NTFS on-disk structure must guarantee natural alignment of all
  137. arithmetic quantities on disk up to and including quad-word (64-bit)
  138. numbers. Therefore, all attribute records are quad-word aligned, etc.
  139. --*/
  140. #ifndef _NTFS_
  141. #define _NTFS_
  142. #pragma pack(4)
  143. //
  144. // The fundamental unit of allocation on an Ntfs volume is the
  145. // cluster. Format guarantees that the cluster size is an integral
  146. // power of two times the physical sector size of the device. Ntfs
  147. // reserves 64-bits to describe a cluster, in order to support
  148. // large disks. The LCN represents a physical cluster number on
  149. // the disk, and the VCN represents a virtual cluster number within
  150. // an attribute.
  151. //
  152. typedef LONGLONG LCN;
  153. typedef LCN *PLCN;
  154. typedef LONGLONG VCN;
  155. typedef VCN *PVCN;
  156. typedef LONGLONG LBO;
  157. typedef LBO *PLBO;
  158. typedef LONGLONG VBO;
  159. typedef VBO *PVBO;
  160. //
  161. // Current Versions
  162. //
  163. #define NTFS_MAJOR_VERSION 3
  164. #define NTFS_MINOR_VERSION 1
  165. //
  166. // Temporary definitions ****
  167. //
  168. typedef ULONG COLLATION_RULE;
  169. typedef ULONG DISPLAY_RULE;
  170. //
  171. // The compression chunk size is constant for now, at 4KB.
  172. //
  173. #define NTFS_CHUNK_SIZE (0x1000)
  174. #define NTFS_CHUNK_SHIFT (12)
  175. //
  176. // This number is actually the log of the number of clusters per compression
  177. // unit to be stored in a nonresident attribute record header.
  178. //
  179. #define NTFS_CLUSTERS_PER_COMPRESSION (4)
  180. //
  181. // This is the sparse file unit. This is the unit we use for generating holes
  182. // in sparse files.
  183. //
  184. #define NTFS_SPARSE_FILE_UNIT (0x10000)
  185. //
  186. // Collation Rules
  187. //
  188. //
  189. // For binary collation, values are collated by a binary compare of
  190. // their bytes, with the first byte being most significant.
  191. //
  192. #define COLLATION_BINARY (0)
  193. //
  194. // For collation of Ntfs file names, file names are collated as
  195. // Unicode strings. See below.
  196. //
  197. #define COLLATION_FILE_NAME (1)
  198. //
  199. // For collation of Unicode strings, the strings are collated by
  200. // their binary Unicode value, with the exception that for
  201. // characters which may be upcased, the lower case value for that
  202. // character collates immediately after the upcased value.
  203. //
  204. #define COLLATION_UNICODE_STRING (2)
  205. //
  206. // Total number of collation rules
  207. //
  208. #define COLLATION_NUMBER_RULES (3)
  209. //
  210. // Define the NtOfs Collation Rules
  211. //
  212. #define COLLATION_NTOFS_FIRST (16)
  213. #define COLLATION_NTOFS_ULONG (16)
  214. #define COLLATION_NTOFS_SID (17)
  215. #define COLLATION_NTOFS_SECURITY_HASH (18)
  216. #define COLLATION_NTOFS_ULONGS (19)
  217. #define COLLATION_NTOFS_LAST (19)
  218. //
  219. // The following macros are used to set and query with respect to
  220. // the update sequence arrays.
  221. //
  222. #define UpdateSequenceStructureSize(MSH) ( \
  223. ((((PMULTI_SECTOR_HEADER)(MSH))->UpdateSequenceArraySize-1) * \
  224. SEQUENCE_NUMBER_STRIDE) \
  225. )
  226. #define UpdateSequenceArraySize(STRUCT_SIZE) ( \
  227. ((STRUCT_SIZE) / SEQUENCE_NUMBER_STRIDE + 1) \
  228. )
  229. //
  230. // The MFT Segment Reference is an address in the MFT tagged with
  231. // a circularly reused sequence number set at the time that the MFT
  232. // Segment Reference was valid. Note that this format limits the
  233. // size of the Master File Table to 2**48 segments. So, for
  234. // example, with a 1KB segment size the maximum size of the master
  235. // file would be 2**58 bytes, or 2**28 gigabytes.
  236. //
  237. typedef struct _MFT_SEGMENT_REFERENCE {
  238. //
  239. // First a 48 bit segment number.
  240. //
  241. ULONG SegmentNumberLowPart; // offset = 0x000
  242. USHORT SegmentNumberHighPart; // offset = 0x004
  243. //
  244. // Now a 16 bit nonzero sequence number. A value of 0 is
  245. // reserved to allow the possibility of a routine accepting
  246. // 0 as a sign that the sequence number check should be
  247. // repressed.
  248. //
  249. USHORT SequenceNumber; // offset = 0x006
  250. } MFT_SEGMENT_REFERENCE, *PMFT_SEGMENT_REFERENCE; // sizeof = 0x008
  251. //
  252. // A file reference in NTFS is simply the MFT Segment Reference of
  253. // the Base file record.
  254. //
  255. typedef MFT_SEGMENT_REFERENCE FILE_REFERENCE, *PFILE_REFERENCE;
  256. //
  257. // While the format allows 48 bits worth of segment number, the current
  258. // implementation restricts this to 32 bits. Using NtfsUnsafeSegmentNumber
  259. // results in a performance win. When the implementation changes, the
  260. // unsafe segment numbers must be cleaned up. NtfsFullSegmentNumber is
  261. // used in a few spots to guarantee integrity of the disk.
  262. //
  263. #define NtfsSegmentNumber(fr) NtfsUnsafeSegmentNumber( fr )
  264. #define NtfsFullSegmentNumber(fr) ( (*(ULONGLONG UNALIGNED *)(fr)) & 0xFFFFFFFFFFFF )
  265. #define NtfsUnsafeSegmentNumber(fr) ((fr)->SegmentNumberLowPart)
  266. #define NtfsSetSegmentNumber(fr,high,low) \
  267. ((fr)->SegmentNumberHighPart = (high), (fr)->SegmentNumberLowPart = (low))
  268. #define NtfsEqualMftRef(X,Y) ( NtfsSegmentNumber( X ) == NtfsSegmentNumber( Y ) )
  269. #define NtfsLtrMftRef(X,Y) ( NtfsSegmentNumber( X ) < NtfsSegmentNumber( Y ) )
  270. #define NtfsGtrMftRef(X,Y) ( NtfsSegmentNumber( X ) > NtfsSegmentNumber( Y ) ) \
  271. #define NtfsLeqMftRef(X,Y) ( NtfsSegmentNumber( X ) <= NtfsSegmentNumber( Y ) )
  272. #define NtfsGeqMftRef(X,Y) ( NtfsSegmentNumber( X ) >= NtfsSegmentNumber( Y ) )
  273. //
  274. // System File Numbers. The following file numbers are a fixed
  275. // part of the volume number. For the system files, the
  276. // SequenceNumber is always equal to the File Number. So to form a
  277. // File Reference for a given System File, set LowPart and
  278. // SequenceNumber to the File Number, and set HighPart to 0. Any
  279. // unused file numbers prior to FIRST_USER_FILE_NUMBER should not
  280. // be used. They are reserved to allow the potential for easy
  281. // upgrade of existing volumes from future versions of the file
  282. // system.
  283. //
  284. // Each definition below is followed by a comment containing
  285. // the file name for the file:
  286. //
  287. // Number Name
  288. // ------ ----
  289. #define MASTER_FILE_TABLE_NUMBER (0) // $Mft
  290. #define MASTER_FILE_TABLE2_NUMBER (1) // $MftMirr
  291. #define LOG_FILE_NUMBER (2) // $LogFile
  292. #define VOLUME_DASD_NUMBER (3) // $Volume
  293. #define ATTRIBUTE_DEF_TABLE_NUMBER (4) // $AttrDef
  294. #define ROOT_FILE_NAME_INDEX_NUMBER (5) // .
  295. #define BIT_MAP_FILE_NUMBER (6) // $BitMap
  296. #define BOOT_FILE_NUMBER (7) // $Boot
  297. #define BAD_CLUSTER_FILE_NUMBER (8) // $BadClus
  298. #define SECURITY_FILE_NUMBER (9) // $Secure
  299. #define UPCASE_TABLE_NUMBER (10) // $UpCase
  300. #define EXTEND_NUMBER (11) // $Extend
  301. #define LAST_SYSTEM_FILE_NUMBER (11)
  302. #define FIRST_USER_FILE_NUMBER (16)
  303. //
  304. // The number of bits to extend the Mft and bitmap. We round these up to a
  305. // cluster boundary for a large cluster volume
  306. //
  307. #define BITMAP_EXTEND_GRANULARITY (64)
  308. #define MFT_HOLE_GRANULARITY (32)
  309. #define MFT_EXTEND_GRANULARITY (16)
  310. //
  311. // The shift values for determining the threshold for the Mft defragging.
  312. //
  313. #define MFT_DEFRAG_UPPER_THRESHOLD (3) // Defrag if 1/8 of free space
  314. #define MFT_DEFRAG_LOWER_THRESHOLD (4) // Stop at 1/16 of free space
  315. //
  316. // Attribute Type Code. Attribute Types also have a Unicode Name,
  317. // and the correspondence between the Unicode Name and the
  318. // Attribute Type Code is stored in the Attribute Definition File.
  319. //
  320. typedef ULONG ATTRIBUTE_TYPE_CODE;
  321. typedef ATTRIBUTE_TYPE_CODE *PATTRIBUTE_TYPE_CODE;
  322. //
  323. // System-defined Attribute Type Codes. For the System-defined
  324. // attributes, the Unicode Name is exactly equal to the name of the
  325. // following symbols. For this reason, all of the system-defined
  326. // attribute names start with "$", to always distinguish them when
  327. // attribute names are listed, and to reserve a namespace for
  328. // attributes defined in the future. I.e., a User-Defined
  329. // attribute name will never collide with a current or future
  330. // system-defined attribute name if it does not start with "$".
  331. // User attribute numbers should not start until
  332. // $FIRST_USER_DEFINED_ATTRIBUTE, to allow the potential for
  333. // upgrading existing volumes with new user-defined attributes in
  334. // future versions of NTFS. The tagged attribute list is
  335. // terminated with a lone-standing 0 ($END) - the rest of the
  336. // attribute record does not exist.
  337. //
  338. // The type code value of 0 is reserved for convenience of the
  339. // implementation.
  340. //
  341. #define $UNUSED (0X0)
  342. #define $STANDARD_INFORMATION (0x10)
  343. #define $ATTRIBUTE_LIST (0x20)
  344. #define $FILE_NAME (0x30)
  345. #define $OBJECT_ID (0x40)
  346. #define $SECURITY_DESCRIPTOR (0x50)
  347. #define $VOLUME_NAME (0x60)
  348. #define $VOLUME_INFORMATION (0x70)
  349. #define $DATA (0x80)
  350. #define $INDEX_ROOT (0x90)
  351. #define $INDEX_ALLOCATION (0xA0)
  352. #define $BITMAP (0xB0)
  353. #define $REPARSE_POINT (0xC0)
  354. #define $EA_INFORMATION (0xD0)
  355. #define $EA (0xE0)
  356. // #define $LOGGED_UTILITY_STREAM (0x100) // defined in ntfsexp.h
  357. #define $FIRST_USER_DEFINED_ATTRIBUTE (0x1000)
  358. #define $END (0xFFFFFFFF)
  359. //
  360. // The boot sector is duplicated on the partition. The first copy
  361. // is on the first physical sector (LBN == 0) of the partition, and
  362. // the second copy is at <number sectors on partition> / 2. If the
  363. // first copy can not be read when trying to mount the disk, the
  364. // second copy may be read and has the identical contents. Format
  365. // must figure out which cluster the second boot record belongs in,
  366. // and it must zero all of the other sectors that happen to be in
  367. // the same cluster. The boot file minimally contains with two
  368. // clusters, which are the two clusters which contain the copies of
  369. // the boot record. If format knows that some system likes to put
  370. // code somewhere, then it should also align this requirement to
  371. // even clusters, and add that to the boot file as well.
  372. //
  373. // Part of the sector contains a BIOS Parameter Block. The BIOS in
  374. // the sector is packed (i.e., unaligned) so we'll supply an
  375. // unpacking macro to translate a packed BIOS into its unpacked
  376. // equivalent. The unpacked BIOS structure is already defined in
  377. // ntioapi.h so we only need to define the packed BIOS.
  378. //
  379. //
  380. // Define the Packed and Unpacked BIOS Parameter Block
  381. //
  382. typedef struct _PACKED_BIOS_PARAMETER_BLOCK {
  383. UCHAR BytesPerSector[2]; // offset = 0x000
  384. UCHAR SectorsPerCluster[1]; // offset = 0x002
  385. UCHAR ReservedSectors[2]; // offset = 0x003 (zero)
  386. UCHAR Fats[1]; // offset = 0x005 (zero)
  387. UCHAR RootEntries[2]; // offset = 0x006 (zero)
  388. UCHAR Sectors[2]; // offset = 0x008 (zero)
  389. UCHAR Media[1]; // offset = 0x00A
  390. UCHAR SectorsPerFat[2]; // offset = 0x00B (zero)
  391. UCHAR SectorsPerTrack[2]; // offset = 0x00D
  392. UCHAR Heads[2]; // offset = 0x00F
  393. UCHAR HiddenSectors[4]; // offset = 0x011 (zero)
  394. UCHAR LargeSectors[4]; // offset = 0x015 (zero)
  395. } PACKED_BIOS_PARAMETER_BLOCK; // sizeof = 0x019
  396. typedef PACKED_BIOS_PARAMETER_BLOCK *PPACKED_BIOS_PARAMETER_BLOCK;
  397. typedef struct BIOS_PARAMETER_BLOCK {
  398. USHORT BytesPerSector;
  399. UCHAR SectorsPerCluster;
  400. USHORT ReservedSectors;
  401. UCHAR Fats;
  402. USHORT RootEntries;
  403. USHORT Sectors;
  404. UCHAR Media;
  405. USHORT SectorsPerFat;
  406. USHORT SectorsPerTrack;
  407. USHORT Heads;
  408. ULONG HiddenSectors;
  409. ULONG LargeSectors;
  410. } BIOS_PARAMETER_BLOCK;
  411. typedef BIOS_PARAMETER_BLOCK *PBIOS_PARAMETER_BLOCK;
  412. //
  413. // This macro takes a Packed BIOS and fills in its Unpacked
  414. // equivalent
  415. //
  416. #define NtfsUnpackBios(Bios,Pbios) { \
  417. CopyUchar2(&((Bios)->BytesPerSector), &(Pbios)->BytesPerSector ); \
  418. CopyUchar1(&((Bios)->SectorsPerCluster), &(Pbios)->SectorsPerCluster); \
  419. CopyUchar2(&((Bios)->ReservedSectors), &(Pbios)->ReservedSectors ); \
  420. CopyUchar1(&((Bios)->Fats), &(Pbios)->Fats ); \
  421. CopyUchar2(&((Bios)->RootEntries), &(Pbios)->RootEntries ); \
  422. CopyUchar2(&((Bios)->Sectors), &(Pbios)->Sectors ); \
  423. CopyUchar1(&((Bios)->Media), &(Pbios)->Media ); \
  424. CopyUchar2(&((Bios)->SectorsPerFat), &(Pbios)->SectorsPerFat ); \
  425. CopyUchar2(&((Bios)->SectorsPerTrack), &(Pbios)->SectorsPerTrack ); \
  426. CopyUchar2(&((Bios)->Heads), &(Pbios)->Heads ); \
  427. CopyUchar4(&((Bios)->HiddenSectors), &(Pbios)->HiddenSectors ); \
  428. CopyUchar4(&((Bios)->LargeSectors), &(Pbios)->LargeSectors ); \
  429. }
  430. //
  431. // Define the boot sector. Note that MFT2 is exactly three file
  432. // record segments long, and it mirrors the first three file record
  433. // segments from the MFT, which are MFT, MFT2 and the Log File.
  434. //
  435. // The Oem field contains the ASCII characters "NTFS ".
  436. //
  437. // The Checksum field is a simple additive checksum of all of the
  438. // ULONGs which precede the Checksum ULONG. The rest of the sector
  439. // is not included in this Checksum.
  440. //
  441. typedef struct _PACKED_BOOT_SECTOR {
  442. UCHAR Jump[3]; // offset = 0x000
  443. UCHAR Oem[8]; // offset = 0x003
  444. PACKED_BIOS_PARAMETER_BLOCK PackedBpb; // offset = 0x00B
  445. UCHAR Unused[4]; // offset = 0x024
  446. LONGLONG NumberSectors; // offset = 0x028
  447. LCN MftStartLcn; // offset = 0x030
  448. LCN Mft2StartLcn; // offset = 0x038
  449. CHAR ClustersPerFileRecordSegment; // offset = 0x040
  450. UCHAR Reserved0[3];
  451. CHAR DefaultClustersPerIndexAllocationBuffer; // offset = 0x044
  452. UCHAR Reserved1[3];
  453. LONGLONG SerialNumber; // offset = 0x048
  454. ULONG Checksum; // offset = 0x050
  455. UCHAR BootStrap[0x200-0x054]; // offset = 0x054
  456. } PACKED_BOOT_SECTOR; // sizeof = 0x200
  457. typedef PACKED_BOOT_SECTOR *PPACKED_BOOT_SECTOR;
  458. //
  459. // File Record Segment. This is the header that begins every File
  460. // Record Segment in the Master File Table.
  461. //
  462. typedef struct _FILE_RECORD_SEGMENT_HEADER {
  463. //
  464. // Multi-Sector Header as defined by the Cache Manager. This
  465. // structure will always contain the signature "FILE" and a
  466. // description of the location and size of the Update Sequence
  467. // Array.
  468. //
  469. MULTI_SECTOR_HEADER MultiSectorHeader; // offset = 0x000
  470. //
  471. // Log File Sequence Number of last logged update to this File
  472. // Record Segment.
  473. //
  474. LSN Lsn; // offset = 0x008
  475. //
  476. // Sequence Number. This is incremented each time that a File
  477. // Record segment is freed, and 0 is not used. The
  478. // SequenceNumber field of a File Reference must match the
  479. // contents of this field, or else the File Reference is
  480. // incorrect (presumably stale).
  481. //
  482. USHORT SequenceNumber; // offset = 0x010
  483. //
  484. // This is the count of the number of references which exist
  485. // for this segment, from an INDEX_xxx attribute. In File
  486. // Records Segments other than the Base File Record Segment,
  487. // this field is 0.
  488. //
  489. USHORT ReferenceCount; // offset = 0x012
  490. //
  491. // Offset to the first Attribute record in bytes.
  492. //
  493. USHORT FirstAttributeOffset; // offset = 0x014
  494. //
  495. // FILE_xxx flags.
  496. //
  497. USHORT Flags; // offset = 0x016
  498. //
  499. // First free byte available for attribute storage, from start
  500. // of this header. This value should always be aligned to a
  501. // quad-word boundary, since attributes are quad-word aligned.
  502. //
  503. ULONG FirstFreeByte; // offset = x0018
  504. //
  505. // Total bytes available in this file record segment, from the
  506. // start of this header. This is essentially the file record
  507. // segment size.
  508. //
  509. ULONG BytesAvailable; // offset = 0x01C
  510. //
  511. // This is a File Reference to the Base file record segment for
  512. // this file. If this is the Base, then the value of this
  513. // field is all 0's.
  514. //
  515. FILE_REFERENCE BaseFileRecordSegment; // offset = 0x020
  516. //
  517. // This is the attribute instance number to be used when
  518. // creating an attribute. It is zeroed when the base file
  519. // record is created, and captured for each new attribute as it
  520. // is created and incremented afterwards for the next
  521. // attribute. Instance numbering must also occur for the
  522. // initial attributes. Zero is a valid attribute instance
  523. // number, and typically used for standard information.
  524. //
  525. USHORT NextAttributeInstance; // offset = 0x028
  526. //
  527. // Current FRS record - this is here for recovery alone and added in 5.1
  528. // Note: this is not aligned
  529. //
  530. USHORT SegmentNumberHighPart; // offset = 0x02A
  531. ULONG SegmentNumberLowPart; // offset = 0x02C
  532. //
  533. // Update Sequence Array to protect multi-sector transfers of
  534. // the File Record Segment. Accesses to already initialized
  535. // File Record Segments should go through the offset above, for
  536. // upwards compatibility.
  537. //
  538. UPDATE_SEQUENCE_ARRAY UpdateArrayForCreateOnly; // offset = 0x030
  539. } FILE_RECORD_SEGMENT_HEADER;
  540. typedef FILE_RECORD_SEGMENT_HEADER *PFILE_RECORD_SEGMENT_HEADER;
  541. //
  542. // earlier version of FRS from 5.0
  543. //
  544. typedef struct _FILE_RECORD_SEGMENT_HEADER_V0 {
  545. //
  546. // Multi-Sector Header as defined by the Cache Manager. This
  547. // structure will always contain the signature "FILE" and a
  548. // description of the location and size of the Update Sequence
  549. // Array.
  550. //
  551. MULTI_SECTOR_HEADER MultiSectorHeader; // offset = 0x000
  552. //
  553. // Log File Sequence Number of last logged update to this File
  554. // Record Segment.
  555. //
  556. LSN Lsn; // offset = 0x008
  557. //
  558. // Sequence Number. This is incremented each time that a File
  559. // Record segment is freed, and 0 is not used. The
  560. // SequenceNumber field of a File Reference must match the
  561. // contents of this field, or else the File Reference is
  562. // incorrect (presumably stale).
  563. //
  564. USHORT SequenceNumber; // offset = 0x010
  565. //
  566. // This is the count of the number of references which exist
  567. // for this segment, from an INDEX_xxx attribute. In File
  568. // Records Segments other than the Base File Record Segment,
  569. // this field is 0.
  570. //
  571. USHORT ReferenceCount; // offset = 0x012
  572. //
  573. // Offset to the first Attribute record in bytes.
  574. //
  575. USHORT FirstAttributeOffset; // offset = 0x014
  576. //
  577. // FILE_xxx flags.
  578. //
  579. USHORT Flags; // offset = 0x016
  580. //
  581. // First free byte available for attribute storage, from start
  582. // of this header. This value should always be aligned to a
  583. // quad-word boundary, since attributes are quad-word aligned.
  584. //
  585. ULONG FirstFreeByte; // offset = x0018
  586. //
  587. // Total bytes available in this file record segment, from the
  588. // start of this header. This is essentially the file record
  589. // segment size.
  590. //
  591. ULONG BytesAvailable; // offset = 0x01C
  592. //
  593. // This is a File Reference to the Base file record segment for
  594. // this file. If this is the Base, then the value of this
  595. // field is all 0's.
  596. //
  597. FILE_REFERENCE BaseFileRecordSegment; // offset = 0x020
  598. //
  599. // This is the attribute instance number to be used when
  600. // creating an attribute. It is zeroed when the base file
  601. // record is created, and captured for each new attribute as it
  602. // is created and incremented afterwards for the next
  603. // attribute. Instance numbering must also occur for the
  604. // initial attributes. Zero is a valid attribute instance
  605. // number, and typically used for standard information.
  606. //
  607. USHORT NextAttributeInstance; // offset = 0x028
  608. //
  609. // Update Sequence Array to protect multi-sector transfers of
  610. // the File Record Segment. Accesses to already initialized
  611. // File Record Segments should go through the offset above, for
  612. // upwards compatibility.
  613. //
  614. UPDATE_SEQUENCE_ARRAY UpdateArrayForCreateOnly; // offset = 0x02A
  615. } FILE_RECORD_SEGMENT_HEADER_V0;
  616. //
  617. // FILE_xxx flags.
  618. //
  619. #define FILE_RECORD_SEGMENT_IN_USE (0x0001)
  620. #define FILE_FILE_NAME_INDEX_PRESENT (0x0002)
  621. #define FILE_SYSTEM_FILE (0x0004)
  622. #define FILE_VIEW_INDEX_PRESENT (0x0008)
  623. //
  624. // Define a macro to determine the maximum space available for a
  625. // single attribute. For example, this is required when a
  626. // nonresident attribute has to split into multiple file records -
  627. // we need to know how much we can squeeze into a single file
  628. // record. If this macro has any inaccurracy, it must be in the
  629. // direction of returning a slightly smaller number than actually
  630. // required.
  631. //
  632. // ULONG
  633. // NtfsMaximumAttributeSize (
  634. // IN ULONG FileRecordSegmentSize
  635. // );
  636. //
  637. #define NtfsMaximumAttributeSize(FRSS) ( \
  638. (FRSS) - QuadAlign(sizeof(FILE_RECORD_SEGMENT_HEADER)) - \
  639. QuadAlign((((FRSS) / SEQUENCE_NUMBER_STRIDE) * sizeof(UPDATE_SEQUENCE_NUMBER))) - \
  640. QuadAlign(sizeof(ATTRIBUTE_TYPE_CODE)) \
  641. )
  642. //
  643. // Attribute Record. Logically an attribute has a type, an
  644. // optional name, and a value, however the storage details make it
  645. // a little more complicated. For starters, an attribute's value
  646. // may either be resident in the file record segment itself, on
  647. // nonresident in a separate data stream. If it is nonresident, it
  648. // may actually exist multiple times in multiple file record
  649. // segments to describe different ranges of VCNs.
  650. //
  651. // Attribute Records are always aligned on a quad word (64-bit)
  652. // boundary.
  653. //
  654. typedef struct _ATTRIBUTE_RECORD_HEADER {
  655. //
  656. // Attribute Type Code.
  657. //
  658. ATTRIBUTE_TYPE_CODE TypeCode; // offset = 0x000
  659. //
  660. // Length of this Attribute Record in bytes. The length is
  661. // always rounded to a quad word boundary, if necessary. Also
  662. // the length only reflects the size necessary to store the
  663. // given record variant.
  664. //
  665. ULONG RecordLength; // offset = 0x004
  666. //
  667. // Attribute Form Code (see below)
  668. //
  669. UCHAR FormCode; // offset = 0x008
  670. //
  671. // Length of the optional attribute name in characters, or 0 if
  672. // there is none.
  673. //
  674. UCHAR NameLength; // offset = 0x009
  675. //
  676. // Offset to the attribute name from start of attribute record,
  677. // in bytes, if it exists. This field is undefined if
  678. // NameLength is 0.
  679. //
  680. USHORT NameOffset; // offset = 0x00A
  681. //
  682. // ATTRIBUTE_xxx flags.
  683. //
  684. USHORT Flags; // offset = 0x00C
  685. //
  686. // The file-record-unique attribute instance number for this
  687. // attribute.
  688. //
  689. USHORT Instance; // offset = 0x00E
  690. //
  691. // The following union handles the cases distinguished by the
  692. // Form Code.
  693. //
  694. union {
  695. //
  696. // Resident Form. Attribute resides in file record segment.
  697. //
  698. struct {
  699. //
  700. // Length of attribute value in bytes.
  701. //
  702. ULONG ValueLength; // offset = 0x010
  703. //
  704. // Offset to value from start of attribute record, in
  705. // bytes.
  706. //
  707. USHORT ValueOffset; // offset = 0x014
  708. //
  709. // RESIDENT_FORM_xxx Flags.
  710. //
  711. UCHAR ResidentFlags; // offset = 0x016
  712. //
  713. // Reserved.
  714. //
  715. UCHAR Reserved; // offset = 0x017
  716. } Resident;
  717. //
  718. // Nonresident Form. Attribute resides in separate stream.
  719. //
  720. struct {
  721. //
  722. // Lowest VCN covered by this attribute record.
  723. //
  724. VCN LowestVcn; // offset = 0x010
  725. //
  726. // Highest VCN covered by this attribute record.
  727. //
  728. VCN HighestVcn; // offset = 0x018
  729. //
  730. // Offset to the Mapping Pairs Array (defined below),
  731. // in bytes, from the start of the attribute record.
  732. //
  733. USHORT MappingPairsOffset; // offset = 0x020
  734. //
  735. // Unit of Compression size for this stream, expressed
  736. // as a log of the cluster size.
  737. //
  738. // 0 means file is not compressed
  739. // 1, 2, 3, and 4 are potentially legal values if the
  740. // stream is compressed, however the implementation
  741. // may only choose to use 4, or possibly 3. Note
  742. // that 4 means cluster size time 16. If convenient
  743. // the implementation may wish to accept a
  744. // reasonable range of legal values here (1-5?),
  745. // even if the implementation only generates
  746. // a smaller set of values itself.
  747. //
  748. UCHAR CompressionUnit; // offset = 0x022
  749. //
  750. // Reserved to get to quad word boundary.
  751. //
  752. UCHAR Reserved[5]; // offset = 0x023
  753. //
  754. // Allocated Length of the file in bytes. This is
  755. // obviously an even multiple of the cluster size.
  756. // (Not present if LowestVcn != 0.)
  757. //
  758. LONGLONG AllocatedLength; // offset = 0x028
  759. //
  760. // File Size in bytes (highest byte which may be read +
  761. // 1). (Not present if LowestVcn != 0.)
  762. //
  763. LONGLONG FileSize; // offset = 0x030
  764. //
  765. // Valid Data Length (highest initialized byte + 1).
  766. // This field must also be rounded to a cluster
  767. // boundary, and the data must always be initialized to
  768. // a cluster boundary. (Not present if LowestVcn != 0.)
  769. //
  770. LONGLONG ValidDataLength; // offset = 0x038
  771. //
  772. // Totally allocated. This field is only present for the first
  773. // file record of a compressed stream. It represents the sum of
  774. // the allocated clusters for a file.
  775. //
  776. LONGLONG TotalAllocated; // offset = 0x040
  777. //
  778. //
  779. // Mapping Pairs Array, starting at the offset stored
  780. // above.
  781. //
  782. // The Mapping Pairs Array is stored in a compressed
  783. // form, and assumes that this information is
  784. // decompressed and cached by the system. The reason
  785. // for compressing this information is clear, it is
  786. // done in the hopes that all of the retrieval
  787. // information always fits in a single file record
  788. // segment.
  789. //
  790. // Logically, the MappingPairs Array stores a series of
  791. // NextVcn/CurrentLcn pairs. So, for example, given
  792. // that we know the first Vcn (from LowestVcn above),
  793. // the first Mapping Pair tells us what the next Vcn is
  794. // (for the next Mapping Pair), and what Lcn the
  795. // current Vcn is mapped to, or 0 if the Current Vcn is
  796. // not allocated. (This is exactly the FsRtl MCB
  797. // structure).
  798. //
  799. // For example, if a file has a single run of 8
  800. // clusters, starting at Lcn 128, and the file starts
  801. // at LowestVcn=0, then the Mapping Pairs array has
  802. // just one entry, which is:
  803. //
  804. // NextVcn = 8
  805. // CurrentLcn = 128
  806. //
  807. // The compression is implemented with the following
  808. // algorithm. Assume that you initialize two "working"
  809. // variables as follows:
  810. //
  811. // NextVcn = LowestVcn (from above)
  812. // CurrentLcn = 0
  813. //
  814. // The MappingPairs array is byte stream, which simply
  815. // store the changes to the working variables above,
  816. // when processed sequentially. The byte stream is to
  817. // be interpreted as a zero-terminated stream of
  818. // triples, as follows:
  819. //
  820. // count byte = v + (l * 16)
  821. //
  822. // where v = number of changed low-order Vcn bytes
  823. // l = number of changed low-order Lcn bytes
  824. //
  825. // v Vcn change bytes
  826. // l Lcn change bytes
  827. //
  828. // The byte stream terminates when a count byte of 0 is
  829. // encountered.
  830. //
  831. // The decompression algorithm goes as follows,
  832. // assuming that Attribute is a pointer to the
  833. // attribute record.
  834. //
  835. // 1. Initialize:
  836. // NextVcn = Attribute->LowestVcn;
  837. // CurrentLcn = 0;
  838. //
  839. // 2. Initialize byte stream pointer to: (PCHAR)Attribute +
  840. // Attribute->AttributeForm->Nonresident->MappingPairsOffset
  841. //
  842. // 3. CurrentVcn = NextVcn;
  843. //
  844. // 4. Read next byte from stream. If it is 0, then
  845. // break, else extract v and l (see above).
  846. //
  847. // 5. Interpret the next v bytes as a signed quantity,
  848. // with the low-order byte coming first. Unpack it
  849. // sign-extended into 64 bits and add it to NextVcn.
  850. // (It can really only be positive, but the Lcn
  851. // change can be positive or negative.)
  852. //
  853. // 6. Interpret the next l bytes as a signed quantity,
  854. // with the low-order byte coming first. Unpack it
  855. // sign-extended into 64 bits and add it to
  856. // CurrentLcn. Remember, if this produces a
  857. // CurrentLcn of 0, then the Vcns from the
  858. // CurrentVcn to NextVcn-1 are unallocated.
  859. //
  860. // 7. Update cached mapping information from
  861. // CurrentVcn, NextVcn and CurrentLcn.
  862. //
  863. // 8. Loop back to 3.
  864. //
  865. // The compression algorithm should now be obvious, as
  866. // it is the reverse of the above. The compression and
  867. // decompression algorithms will be available as common
  868. // RTL routines, available to NTFS and file utilities.
  869. //
  870. // In defense of this algorithm, not only does it
  871. // provide compression of the on-disk storage
  872. // requirements, but it results in a single
  873. // representation, independent of disk size and file
  874. // size. Contrast this with solutions which are in use
  875. // which define multiple sizes for virtual and logical
  876. // cluster sizes, depending on the size of the disk,
  877. // etc. For example, two byte cluster numbers might
  878. // suffice for a floppy, while four bytes would be
  879. // required for most hard disks today, and five or six
  880. // bytes are required after a certain number of
  881. // gigabytes, etc. This eventually results in more
  882. // complex code than above (because of the cases) and
  883. // worse yet - untested cases. So, more important than
  884. // the compression, the above algorithm provides one
  885. // case which efficiently handles any size disk.
  886. //
  887. } Nonresident;
  888. } Form;
  889. } ATTRIBUTE_RECORD_HEADER;
  890. typedef ATTRIBUTE_RECORD_HEADER *PATTRIBUTE_RECORD_HEADER;
  891. //
  892. // Attribute Form Codes
  893. //
  894. #define RESIDENT_FORM (0x00)
  895. #define NONRESIDENT_FORM (0x01)
  896. //
  897. // Define Attribute Flags
  898. //
  899. //
  900. // The first range of flag bits is reserved for
  901. // storing the compression method. This constant
  902. // defines the mask of the bits reserved for
  903. // compression method. It is also the first
  904. // illegal value, since we increment it to calculate
  905. // the code to pass to the Rtl routines. Thus it is
  906. // impossible for us to store COMPRESSION_FORMAT_DEFAULT.
  907. //
  908. #define ATTRIBUTE_FLAG_COMPRESSION_MASK (0x00FF)
  909. #define ATTRIBUTE_FLAG_SPARSE (0x8000)
  910. #define ATTRIBUTE_FLAG_ENCRYPTED (0x4000)
  911. //
  912. // RESIDENT_FORM_xxx flags
  913. //
  914. //
  915. // This attribute is indexed.
  916. //
  917. #define RESIDENT_FORM_INDEXED (0x01)
  918. //
  919. // The maximum attribute name length is 255 (in chars)
  920. //
  921. #define NTFS_MAX_ATTR_NAME_LEN (255)
  922. //
  923. // Define macros for the size of resident and nonresident headers.
  924. //
  925. #define SIZEOF_RESIDENT_ATTRIBUTE_HEADER ( \
  926. FIELD_OFFSET(ATTRIBUTE_RECORD_HEADER,Form.Resident.Reserved)+1 \
  927. )
  928. #define SIZEOF_FULL_NONRES_ATTR_HEADER ( \
  929. sizeof(ATTRIBUTE_RECORD_HEADER) \
  930. )
  931. #define SIZEOF_PARTIAL_NONRES_ATTR_HEADER ( \
  932. FIELD_OFFSET(ATTRIBUTE_RECORD_HEADER,Form.Nonresident.TotalAllocated) \
  933. )
  934. //
  935. // Standard Information Attribute. This attribute is present in
  936. // every base file record, and must be resident.
  937. //
  938. typedef struct _STANDARD_INFORMATION {
  939. //
  940. // File creation time.
  941. //
  942. LONGLONG CreationTime; // offset = 0x000
  943. //
  944. // Last time the DATA attribute was modified.
  945. //
  946. LONGLONG LastModificationTime; // offset = 0x008
  947. //
  948. // Last time any attribute was modified.
  949. //
  950. LONGLONG LastChangeTime; // offset = 0x010
  951. //
  952. // Last time the file was accessed. This field may not always
  953. // be updated (write-protected media), and even when it is
  954. // updated, it may only be updated if the time would change by
  955. // a certain delta. It is meant to tell someone approximately
  956. // when the file was last accessed, for purposes of possible
  957. // file migration.
  958. //
  959. LONGLONG LastAccessTime; // offset = 0x018
  960. //
  961. // File attributes. The first byte is the standard "Fat"
  962. // flags for this file.
  963. //
  964. ULONG FileAttributes; // offset = 0x020
  965. //
  966. // Maximum file versions allowed for this file. If this field
  967. // is 0, then versioning is not enabled for this file. If
  968. // there are multiple files with the same version, then the
  969. // value of Maximum file versions in the file with the highest
  970. // version is the correct one.
  971. //
  972. ULONG MaximumVersions; // offset = 0x024
  973. //
  974. // Version number for this file.
  975. //
  976. ULONG VersionNumber; // offset = 0x028
  977. //
  978. // Class Id from the bidirectional Class Id index
  979. //
  980. ULONG ClassId; // offset = 0x02c
  981. //
  982. // Id for file owner, from bidir security index
  983. //
  984. ULONG OwnerId; // offset = 0x030
  985. //
  986. // SecurityId for the file - translates via bidir index to
  987. // granted access Acl.
  988. //
  989. ULONG SecurityId; // offset = 0x034
  990. //
  991. // Current amount of quota that has been charged for all the
  992. // streams of this file. Changed in same transaction with the
  993. // quota file itself.
  994. //
  995. ULONGLONG QuotaCharged; // offset = 0x038
  996. //
  997. // Update sequence number for this file.
  998. //
  999. ULONGLONG Usn; // offset = 0x040
  1000. } STANDARD_INFORMATION; // sizeof = 0x048
  1001. typedef STANDARD_INFORMATION *PSTANDARD_INFORMATION;
  1002. //
  1003. // Large Standard Information Attribute. We use this to find the
  1004. // security ID field.
  1005. //
  1006. typedef struct LARGE_STANDARD_INFORMATION {
  1007. //
  1008. // File creation time.
  1009. //
  1010. LONGLONG CreationTime; // offset = 0x000
  1011. //
  1012. // Last time the DATA attribute was modified.
  1013. //
  1014. LONGLONG LastModificationTime; // offset = 0x008
  1015. //
  1016. // Last time any attribute was modified.
  1017. //
  1018. LONGLONG LastChangeTime; // offset = 0x010
  1019. //
  1020. // Last time the file was accessed. This field may not always
  1021. // be updated (write-protected media), and even when it is
  1022. // updated, it may only be updated if the time would change by
  1023. // a certain delta. It is meant to tell someone approximately
  1024. // when the file was last accessed, for purposes of possible
  1025. // file migration.
  1026. //
  1027. LONGLONG LastAccessTime; // offset = 0x018
  1028. //
  1029. // File attributes. The first byte is the standard "Fat"
  1030. // flags for this file.
  1031. //
  1032. ULONG FileAttributes; // offset = 0x020
  1033. //
  1034. // Maximum file versions allowed for this file. If this field
  1035. // is 0, then versioning is not enabled for this file. If
  1036. // there are multiple files with the same version, then the
  1037. // value of Maximum file versions in the file with the highest
  1038. // version is the correct one.
  1039. //
  1040. ULONG MaximumVersions; // offset = 0x024
  1041. //
  1042. // Version number for this file.
  1043. //
  1044. ULONG VersionNumber; // offset = 0x028
  1045. ULONG UnusedUlong;
  1046. //
  1047. // Id for file owner, from bidir security index
  1048. //
  1049. ULONG OwnerId; // offset = 0x030
  1050. //
  1051. // SecurityId for the file - translates via bidir index to
  1052. // granted access Acl.
  1053. //
  1054. ULONG SecurityId; // offset = 0x034
  1055. } LARGE_STANDARD_INFORMATION;
  1056. typedef LARGE_STANDARD_INFORMATION *PLARGE_STANDARD_INFORMATION;
  1057. //
  1058. // This was the size of standard information prior to NT4.0
  1059. //
  1060. #define SIZEOF_OLD_STANDARD_INFORMATION (0x30)
  1061. //
  1062. // Define the file attributes, starting with the Fat attributes.
  1063. //
  1064. #define FAT_DIRENT_ATTR_READ_ONLY (0x01)
  1065. #define FAT_DIRENT_ATTR_HIDDEN (0x02)
  1066. #define FAT_DIRENT_ATTR_SYSTEM (0x04)
  1067. #define FAT_DIRENT_ATTR_VOLUME_ID (0x08)
  1068. #define FAT_DIRENT_ATTR_ARCHIVE (0x20)
  1069. #define FAT_DIRENT_ATTR_DEVICE (0x40)
  1070. //
  1071. // Attribute List. Because there is not a special header that goes
  1072. // before the list of attribute list entries we do not need to
  1073. // declare an attribute list header
  1074. //
  1075. //
  1076. // The Attributes List attribute is an ordered-list of quad-word
  1077. // aligned ATTRIBUTE_LIST_ENTRY records. It is ordered first by
  1078. // Attribute Type Code, and then by Attribute Name (if present).
  1079. // No two attributes may exist with the same type code, name and
  1080. // LowestVcn. This also means that at most one occurrence of a
  1081. // given Attribute Type Code without a name may exist.
  1082. //
  1083. // To binary search this attribute, it is first necessary to make a
  1084. // quick pass through it and form a list of pointers, since the
  1085. // optional name makes it variable-length.
  1086. //
  1087. typedef struct _ATTRIBUTE_LIST_ENTRY {
  1088. //
  1089. // Attribute Type Code, the first key on which this list is
  1090. // ordered.
  1091. //
  1092. ATTRIBUTE_TYPE_CODE AttributeTypeCode; // offset = 0x000
  1093. //
  1094. // Size of this record in bytes, including the optional name
  1095. // appended to this structure.
  1096. //
  1097. USHORT RecordLength; // offset = 0x004
  1098. //
  1099. // Length of attribute name, if there is one. If a name exists
  1100. // (AttributeNameLength != 0), then it is a Unicode string of
  1101. // the specified number of characters immediately following
  1102. // this record. This is the second key on which this list is
  1103. // ordered.
  1104. //
  1105. UCHAR AttributeNameLength; // offset = 0x006
  1106. //
  1107. // Reserved to get to quad-word boundary
  1108. //
  1109. UCHAR AttributeNameOffset; // offset = 0x007
  1110. //
  1111. // Lowest Vcn for this attribute. This field is always zero
  1112. // unless the attribute requires multiple file record segments
  1113. // to describe all of its runs, and this is a reference to a
  1114. // segment other than the first one. The field says what the
  1115. // lowest Vcn is that is described by the referenced segment.
  1116. //
  1117. VCN LowestVcn; // offset = 0x008
  1118. //
  1119. // Reference to the MFT segment in which the attribute resides.
  1120. //
  1121. MFT_SEGMENT_REFERENCE SegmentReference; // offset = 0x010
  1122. //
  1123. // The file-record-unique attribute instance number for this
  1124. // attribute.
  1125. //
  1126. USHORT Instance; // offset = 0x018
  1127. //
  1128. // When creating an attribute list entry, start the name here.
  1129. // (When reading one, use the AttributeNameOffset field.)
  1130. //
  1131. WCHAR AttributeName[1]; // offset = 0x01A
  1132. } ATTRIBUTE_LIST_ENTRY;
  1133. typedef ATTRIBUTE_LIST_ENTRY *PATTRIBUTE_LIST_ENTRY;
  1134. typedef struct _DUPLICATED_INFORMATION {
  1135. //
  1136. // File creation time.
  1137. //
  1138. LONGLONG CreationTime; // offset = 0x000
  1139. //
  1140. // Last time the DATA attribute was modified.
  1141. //
  1142. LONGLONG LastModificationTime; // offset = 0x008
  1143. //
  1144. // Last time any attribute was modified.
  1145. //
  1146. LONGLONG LastChangeTime; // offset = 0x010
  1147. //
  1148. // Last time the file was accessed. This field may not always
  1149. // be updated (write-protected media), and even when it is
  1150. // updated, it may only be updated if the time would change by
  1151. // a certain delta. It is meant to tell someone approximately
  1152. // when the file was last accessed, for purposes of possible
  1153. // file migration.
  1154. //
  1155. LONGLONG LastAccessTime; // offset = 0x018
  1156. //
  1157. // Allocated Length of the file in bytes. This is obviously
  1158. // an even multiple of the cluster size. (Not present if
  1159. // LowestVcn != 0.)
  1160. //
  1161. LONGLONG AllocatedLength; // offset = 0x020
  1162. //
  1163. // File Size in bytes (highest byte which may be read + 1).
  1164. // (Not present if LowestVcn != 0.)
  1165. //
  1166. LONGLONG FileSize; // offset = 0x028
  1167. //
  1168. // File attributes. The first byte is the standard "Fat"
  1169. // flags for this file.
  1170. //
  1171. ULONG FileAttributes; // offset = 0x030
  1172. //
  1173. // This union enables the retrieval of the tag in reparse
  1174. // points when there are no Ea's.
  1175. //
  1176. union {
  1177. struct {
  1178. //
  1179. // The size of buffer needed to pack these Ea's
  1180. //
  1181. USHORT PackedEaSize; // offset = 0x034
  1182. //
  1183. // Reserved for quad word alignment
  1184. //
  1185. USHORT Reserved; // offset = 0x036
  1186. };
  1187. //
  1188. // The tag of the data in a reparse point. It represents
  1189. // the type of a reparse point. It enables different layered
  1190. // filters to operate on their own reparse points.
  1191. //
  1192. ULONG ReparsePointTag; // offset = 0x034
  1193. };
  1194. } DUPLICATED_INFORMATION; // sizeof = 0x038
  1195. typedef DUPLICATED_INFORMATION *PDUPLICATED_INFORMATION;
  1196. //
  1197. // This bit is duplicated from the file record, to indicate that
  1198. // this file has a file name index present (is a "directory").
  1199. //
  1200. #define DUP_FILE_NAME_INDEX_PRESENT (0x10000000)
  1201. //
  1202. // This bit is duplicated from the file record, to indicate that
  1203. // this file has a view index present, such as the quota or
  1204. // object id index.
  1205. //
  1206. #define DUP_VIEW_INDEX_PRESENT (0x20000000)
  1207. //
  1208. // The following macros examine fields of the duplicated structure.
  1209. //
  1210. #define IsDirectory( DUPLICATE ) \
  1211. (FlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1212. DUP_FILE_NAME_INDEX_PRESENT ))
  1213. #define IsViewIndex( DUPLICATE ) \
  1214. (FlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1215. DUP_VIEW_INDEX_PRESENT ))
  1216. #define IsReadOnly( DUPLICATE ) \
  1217. (FlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1218. FILE_ATTRIBUTE_READONLY ))
  1219. #define IsHidden( DUPLICATE ) \
  1220. (FlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1221. FILE_ATTRIBUTE_HIDDEN ))
  1222. #define IsSystem( DUPLICATE ) \
  1223. (FlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1224. FILE_ATTRIBUTE_SYSTEM ))
  1225. #define IsEncrypted( DUPLICATE ) \
  1226. (FlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1227. FILE_ATTRIBUTE_ENCRYPTED ))
  1228. #define IsCompressed( DUPLICATE ) \
  1229. (FlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1230. FILE_ATTRIBUTE_COMPRESSED ))
  1231. #define BooleanIsDirectory( DUPLICATE ) \
  1232. (BooleanFlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1233. DUP_FILE_NAME_INDEX_PRESENT ))
  1234. #define BooleanIsReadOnly( DUPLICATE ) \
  1235. (BooleanFlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1236. FILE_ATTRIBUTE_READONLY ))
  1237. #define BooleanIsHidden( DUPLICATE ) \
  1238. (BooleanFlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1239. FILE_ATTRIBUTE_HIDDEN ))
  1240. #define BooleanIsSystem( DUPLICATE ) \
  1241. (BooleanFlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1242. FILE_ATTRIBUTE_SYSTEM ))
  1243. #define HasReparsePoint( DUPLICATE ) \
  1244. (FlagOn( ((PDUPLICATED_INFORMATION) (DUPLICATE))->FileAttributes, \
  1245. FILE_ATTRIBUTE_REPARSE_POINT ))
  1246. //
  1247. // File Name attribute. A file has one File Name attribute for
  1248. // every directory it is entered into (hard links).
  1249. //
  1250. typedef struct _FILE_NAME {
  1251. //
  1252. // This is a File Reference to the directory file which indexes
  1253. // to this name.
  1254. //
  1255. FILE_REFERENCE ParentDirectory; // offset = 0x000
  1256. //
  1257. // Information for faster directory operations.
  1258. //
  1259. DUPLICATED_INFORMATION Info; // offset = 0x008
  1260. //
  1261. // Length of the name to follow, in (Unicode) characters.
  1262. //
  1263. UCHAR FileNameLength; // offset = 0x040
  1264. //
  1265. // FILE_NAME_xxx flags
  1266. //
  1267. UCHAR Flags; // offset = 0x041
  1268. //
  1269. // First character of Unicode File Name
  1270. //
  1271. WCHAR FileName[1]; // offset = 0x042
  1272. } FILE_NAME;
  1273. typedef FILE_NAME *PFILE_NAME;
  1274. //
  1275. // File Name flags
  1276. //
  1277. #define FILE_NAME_NTFS (0x01)
  1278. #define FILE_NAME_DOS (0x02)
  1279. //
  1280. // The maximum file name length is 255 (in chars)
  1281. //
  1282. #define NTFS_MAX_FILE_NAME_LENGTH (255)
  1283. //
  1284. // The maximum number of links on a file is 1024
  1285. //
  1286. #define NTFS_MAX_LINK_COUNT (1024)
  1287. //
  1288. // This flag is not part of the disk structure, but is defined here
  1289. // to explain its use and avoid possible future collisions. For
  1290. // enumerations of "directories" this bit may be set to convey to
  1291. // the collating routine that it should not match file names that
  1292. // only have the FILE_NAME_DOS bit set.
  1293. //
  1294. #define FILE_NAME_IGNORE_DOS_ONLY (0x80)
  1295. #define NtfsFileNameSizeFromLength(LEN) ( \
  1296. (sizeof( FILE_NAME ) + LEN - sizeof( WCHAR )) \
  1297. )
  1298. #define NtfsFileNameSize(PFN) ( \
  1299. (sizeof( FILE_NAME ) + ((PFN)->FileNameLength - 1) * sizeof( WCHAR )) \
  1300. )
  1301. //
  1302. // Object id attribute.
  1303. //
  1304. //
  1305. // On disk representation of an object id.
  1306. //
  1307. #define OBJECT_ID_KEY_LENGTH 16
  1308. #define OBJECT_ID_EXT_INFO_LENGTH 48
  1309. typedef struct _NTFS_OBJECTID_INFORMATION {
  1310. //
  1311. // Data the filesystem needs to identify the file with this object id.
  1312. //
  1313. FILE_REFERENCE FileSystemReference;
  1314. //
  1315. // This portion of the object id is not indexed, it's just
  1316. // some metadata for the user's benefit.
  1317. //
  1318. UCHAR ExtendedInfo[OBJECT_ID_EXT_INFO_LENGTH];
  1319. } NTFS_OBJECTID_INFORMATION, *PNTFS_OBJECTID_INFORMATION;
  1320. #define OBJECT_ID_FLAG_CORRUPT (0x00000001)
  1321. //
  1322. // Security Descriptor attribute. This is just a normal attribute
  1323. // stream containing a security descriptor as defined by NT
  1324. // security and is really treated pretty opaque by NTFS.
  1325. //
  1326. //
  1327. // Security descriptors are stored only once on a volume since there may be
  1328. // many files that share the same descriptor bits. Typically each principal
  1329. // will create files with a single descriptor.
  1330. //
  1331. // The descriptors themselves are stored in a stream, packed on double quad word
  1332. // boundaries. No descriptor will span a 256K cache boundary. The descriptors are
  1333. // assigned a ULONG Id each time a unique descriptor is stored. Prefixing each
  1334. // descriptor in the stream is the hash of the descriptor, the assigned security
  1335. // ID, the length, and the offset within the stream to the beginning of the structure.
  1336. //
  1337. // For robustness, all security descriptors are written to the stream in two
  1338. // different places, with a fixed offset between them. The fixed offset is the
  1339. // size of the VACB_MAPPING_GRANULARITY.
  1340. //
  1341. // An index is used to map from a security Id to offset within the stream. This
  1342. // is used to retrieve the security descriptor bits for access validation. The key
  1343. // format is simply the ULONG security Id. The data portion of the index record
  1344. // is the header of the security descriptor in the stream (see above paragraph).
  1345. //
  1346. // Another index is used to map from a hash to offset within the stream. To
  1347. // simplify the job of the indexing package, the key used in this index is the
  1348. // hash followed by the assigned Id. When a security descriptor is stored,
  1349. // a hash is computed and an approximate seek is made on this index. As entries
  1350. // are enumerated in the index, the descriptor stream is mapped and the security
  1351. // descriptor bits are compared. The key format is a structure that contains
  1352. // the hash and then the Id. The collation routine tests the hash before the Id.
  1353. // The data portion of the index record is the header of the security descriptor.
  1354. //
  1355. //
  1356. // Key structure for Security Hash index
  1357. //
  1358. typedef struct _SECURITY_HASH_KEY
  1359. {
  1360. ULONG Hash; // Hash value for descriptor
  1361. ULONG SecurityId; // Security Id (guaranteed unique)
  1362. } SECURITY_HASH_KEY, *PSECURITY_HASH_KEY;
  1363. //
  1364. // Key structure for Security Id index is simply the SECURITY_ID itself
  1365. //
  1366. //
  1367. // Header for security descriptors in the security descriptor stream. This
  1368. // is the data format for all indexes and is part of SharedSecurity
  1369. //
  1370. typedef struct _SECURITY_DESCRIPTOR_HEADER
  1371. {
  1372. SECURITY_HASH_KEY HashKey; // Hash value for the descriptor
  1373. ULONGLONG Offset; // offset to beginning of header
  1374. ULONG Length; // Length in bytes
  1375. } SECURITY_DESCRIPTOR_HEADER, *PSECURITY_DESCRIPTOR_HEADER;
  1376. #define GETSECURITYDESCRIPTORLENGTH(HEADER) \
  1377. ((HEADER)->Length - sizeof( SECURITY_DESCRIPTOR_HEADER ))
  1378. #define SetSecurityDescriptorLength(HEADER,LENGTH) \
  1379. ((HEADER)->Length = (LENGTH) + sizeof( SECURITY_DESCRIPTOR_HEADER ))
  1380. //
  1381. // Define standard values for well-known security IDs
  1382. //
  1383. #define SECURITY_ID_INVALID (0x00000000)
  1384. #define SECURITY_ID_FIRST (0x00000100)
  1385. //
  1386. // Volume Name attribute. This attribute is just a normal
  1387. // attribute stream containing the unicode characters that make up
  1388. // the volume label. It is an attribute of the Mft File.
  1389. //
  1390. //
  1391. // Volume Information attribute. This attribute is only intended
  1392. // to be used on the Volume DASD file.
  1393. //
  1394. typedef struct _VOLUME_INFORMATION {
  1395. LONGLONG Reserved;
  1396. //
  1397. // Major and minor version number of NTFS on this volume,
  1398. // starting with 1.0. The major and minor version numbers are
  1399. // set from the major and minor version of the Format and NTFS
  1400. // implementation for which they are initialized. The policy
  1401. // for incementing major and minor versions will always be
  1402. // decided on a case by case basis, however, the following two
  1403. // paragraphs attempt to suggest an approximate strategy.
  1404. //
  1405. // The major version number is incremented if/when a volume
  1406. // format change is made which requires major structure changes
  1407. // (hopefully never?). If an implementation of NTFS sees a
  1408. // volume with a higher major version number, it should refuse
  1409. // to mount the volume. If a newer implementation of NTFS sees
  1410. // an older major version number, it knows the volume cannot be
  1411. // accessed without performing a one-time conversion.
  1412. //
  1413. // The minor version number is incremented if/when minor
  1414. // enhancements are made to a major version, which potentially
  1415. // support enhanced functionality through additional file or
  1416. // attribute record fields, or new system-defined files or
  1417. // attributes. If an older implementation of NTFS sees a newer
  1418. // minor version number on a volume, it may issue some kind of
  1419. // warning, but it will proceed to access the volume - with
  1420. // presumably some degradation in functionality compared to the
  1421. // version of NTFS which initialized the volume. If a newer
  1422. // implementation of NTFS sees a volume with an older minor
  1423. // version number, it may issue a warning and proceed. In this
  1424. // case, it may choose to increment the minor version number on
  1425. // the volume and begin full or incremental upgrade of the
  1426. // volume on an as-needed basis. It may also leave the minor
  1427. // version number unchanged, until some sort of explicit
  1428. // directive from the user specifies that the minor version
  1429. // should be updated.
  1430. //
  1431. UCHAR MajorVersion; // offset = 0x008
  1432. UCHAR MinorVersion; // offset = 0x009
  1433. //
  1434. // VOLUME_xxx flags.
  1435. //
  1436. USHORT VolumeFlags; // offset = 0x00A
  1437. //
  1438. // The following fields will only exist on version 4 and greater
  1439. //
  1440. UCHAR LastMountedMajorVersion; // offset = 0x00C
  1441. UCHAR LastMountedMinorVersion; // offset = 0x00D
  1442. USHORT Reserved2; // offset = 0x00E
  1443. USN LowestOpenUsn; // offset = 0x010
  1444. } VOLUME_INFORMATION; // sizeof = 0xC or 0x18
  1445. typedef VOLUME_INFORMATION *PVOLUME_INFORMATION;
  1446. //
  1447. // Volume is Dirty
  1448. //
  1449. #define VOLUME_DIRTY (0x0001)
  1450. #define VOLUME_RESIZE_LOG_FILE (0x0002)
  1451. #define VOLUME_UPGRADE_ON_MOUNT (0x0004)
  1452. #define VOLUME_MOUNTED_ON_40 (0x0008)
  1453. #define VOLUME_DELETE_USN_UNDERWAY (0x0010)
  1454. #define VOLUME_REPAIR_OBJECT_ID (0x0020)
  1455. #define VOLUME_CHKDSK_RAN_ONCE (0x4000) // this bit is used by autochk/chkdsk only
  1456. #define VOLUME_MODIFIED_BY_CHKDSK (0x8000)
  1457. //
  1458. // Common Index Header for Index Root and Index Allocation Buffers.
  1459. // This structure is used to locate the Index Entries and describe
  1460. // the free space in either of the two structures above.
  1461. //
  1462. typedef struct _INDEX_HEADER {
  1463. //
  1464. // Offset from the start of this structure to the first Index
  1465. // Entry.
  1466. //
  1467. ULONG FirstIndexEntry; // offset = 0x000
  1468. //
  1469. // Offset from the start of the first index entry to the first
  1470. // (quad-word aligned) free byte.
  1471. //
  1472. ULONG FirstFreeByte; // offset = 0x004
  1473. //
  1474. // Total number of bytes available, from the start of the first
  1475. // index entry. In the Index Root, this number must always be
  1476. // equal to FirstFreeByte, as the total attribute record will
  1477. // be grown and shrunk as required.
  1478. //
  1479. ULONG BytesAvailable; // offset = 0x008
  1480. //
  1481. // INDEX_xxx flags.
  1482. //
  1483. UCHAR Flags; // offset = 0x00C
  1484. //
  1485. // Reserved to round up to quad word boundary.
  1486. //
  1487. UCHAR Reserved[3]; // offset = 0x00D
  1488. } INDEX_HEADER; // sizeof = 0x010
  1489. typedef INDEX_HEADER *PINDEX_HEADER;
  1490. //
  1491. // INDEX_xxx flags
  1492. //
  1493. //
  1494. // This Index or Index Allocation buffer is an intermediate node,
  1495. // as opposed to a leaf in the Btree. All Index Entries will have
  1496. // a block down pointer.
  1497. //
  1498. #define INDEX_NODE (0x01)
  1499. //
  1500. // Index Root attribute. The index attribute consists of an index
  1501. // header record followed by one or more index entries.
  1502. //
  1503. typedef struct _INDEX_ROOT {
  1504. //
  1505. // Attribute Type Code of the attribute being indexed.
  1506. //
  1507. ATTRIBUTE_TYPE_CODE IndexedAttributeType; // offset = 0x000
  1508. //
  1509. // Collation rule for this index.
  1510. //
  1511. COLLATION_RULE CollationRule; // offset = 0x004
  1512. //
  1513. // Size of Index Allocation Buffer in bytes.
  1514. //
  1515. ULONG BytesPerIndexBuffer; // offset = 0x008
  1516. //
  1517. // Size of Index Allocation Buffers in units of blocks.
  1518. // Blocks will be clusters when index buffer is equal or
  1519. // larger than clusters and log blocks for large
  1520. // cluster systems.
  1521. //
  1522. UCHAR BlocksPerIndexBuffer; // offset = 0x00C
  1523. //
  1524. // Reserved to round to quad word boundary.
  1525. //
  1526. UCHAR Reserved[3]; // offset = 0x00D
  1527. //
  1528. // Index Header to describe the Index Entries which follow
  1529. //
  1530. INDEX_HEADER IndexHeader; // offset = 0x010
  1531. } INDEX_ROOT; // sizeof = 0x020
  1532. typedef INDEX_ROOT *PINDEX_ROOT;
  1533. //
  1534. // Index Allocation record is used for non-root clusters of the
  1535. // b-tree. Each non root cluster is contained in the data part of
  1536. // the index allocation attribute. Each cluster starts with an
  1537. // index allocation list header and is followed by one or more
  1538. // index entries.
  1539. //
  1540. typedef struct _INDEX_ALLOCATION_BUFFER {
  1541. //
  1542. // Multi-Sector Header as defined by the Cache Manager. This
  1543. // structure will always contain the signature "INDX" and a
  1544. // description of the location and size of the Update Sequence
  1545. // Array.
  1546. //
  1547. MULTI_SECTOR_HEADER MultiSectorHeader; // offset = 0x000
  1548. //
  1549. // Log File Sequence Number of last logged update to this Index
  1550. // Allocation Buffer.
  1551. //
  1552. LSN Lsn; // offset = 0x008
  1553. //
  1554. // We store the index block of this Index Allocation buffer for
  1555. // convenience and possible consistency checking.
  1556. //
  1557. VCN ThisBlock; // offset = 0x010
  1558. //
  1559. // Index Header to describe the Index Entries which follow
  1560. //
  1561. INDEX_HEADER IndexHeader; // offset = 0x018
  1562. //
  1563. // Update Sequence Array to protect multi-sector transfers of
  1564. // the Index Allocation Buffer.
  1565. //
  1566. UPDATE_SEQUENCE_ARRAY UpdateSequenceArray; // offset = 0x028
  1567. } INDEX_ALLOCATION_BUFFER;
  1568. typedef INDEX_ALLOCATION_BUFFER *PINDEX_ALLOCATION_BUFFER;
  1569. //
  1570. // Default size of index buffer and index blocks.
  1571. //
  1572. #define DEFAULT_INDEX_BLOCK_SIZE (0x200)
  1573. #define DEFAULT_INDEX_BLOCK_BYTE_SHIFT (9)
  1574. //
  1575. // Index Entry. This structure is common to both the resident
  1576. // index list attribute and the Index Allocation records
  1577. //
  1578. typedef struct _INDEX_ENTRY {
  1579. //
  1580. // Define a union to distinguish directory indices from view indices
  1581. //
  1582. union {
  1583. //
  1584. // Reference to file containing the attribute with this
  1585. // attribute value.
  1586. //
  1587. FILE_REFERENCE FileReference; // offset = 0x000
  1588. //
  1589. // For views, describe the Data Offset and Length in bytes
  1590. //
  1591. struct {
  1592. USHORT DataOffset; // offset = 0x000
  1593. USHORT DataLength; // offset = 0x001
  1594. ULONG ReservedForZero; // offset = 0x002
  1595. };
  1596. };
  1597. //
  1598. // Length of this index entry, in bytes.
  1599. //
  1600. USHORT Length; // offset = 0x008
  1601. //
  1602. // Length of attribute value, in bytes. The attribute value
  1603. // immediately follows this record.
  1604. //
  1605. USHORT AttributeLength; // offset = 0x00A
  1606. //
  1607. // INDEX_ENTRY_xxx Flags.
  1608. //
  1609. USHORT Flags; // offset = 0x00C
  1610. //
  1611. // Reserved to round to quad-word boundary.
  1612. //
  1613. USHORT Reserved; // offset = 0x00E
  1614. //
  1615. // If this Index Entry is an intermediate node in the tree, as
  1616. // determined by the INDEX_xxx flags, then a VCN is stored at
  1617. // the end of this entry at Length - sizeof(VCN).
  1618. //
  1619. } INDEX_ENTRY; // sizeof = 0x010
  1620. typedef INDEX_ENTRY *PINDEX_ENTRY;
  1621. //
  1622. // INDEX_ENTRY_xxx flags
  1623. //
  1624. //
  1625. // This entry is currently in the intermediate node form, i.e., it
  1626. // has a Vcn at the end.
  1627. //
  1628. #define INDEX_ENTRY_NODE (0x0001)
  1629. //
  1630. // This entry is the special END record for the Index or Index
  1631. // Allocation buffer.
  1632. //
  1633. #define INDEX_ENTRY_END (0x0002)
  1634. //
  1635. // This flag is *not* part of the on-disk structure. It is defined
  1636. // and reserved here for the convenience of the implementation to
  1637. // help avoid allocating buffers from the pool and copying.
  1638. //
  1639. #define INDEX_ENTRY_POINTER_FORM (0x8000)
  1640. #define NtfsIndexEntryBlock(IE) ( \
  1641. *(PLONGLONG)((PCHAR)(IE) + (ULONG)(IE)->Length - sizeof(LONGLONG)) \
  1642. )
  1643. #define NtfsSetIndexEntryBlock(IE,IB) { \
  1644. *(PLONGLONG)((PCHAR)(IE) + (ULONG)(IE)->Length - sizeof(LONGLONG)) = (IB); \
  1645. }
  1646. #define NtfsFirstIndexEntry(IH) ( \
  1647. (PINDEX_ENTRY)((PCHAR)(IH) + (IH)->FirstIndexEntry) \
  1648. )
  1649. #define NtfsNextIndexEntry(IE) ( \
  1650. (PINDEX_ENTRY)((PCHAR)(IE) + (ULONG)(IE)->Length) \
  1651. )
  1652. #define NtfsCheckIndexBound(IE, IH) { \
  1653. if (((PCHAR)(IE) < (PCHAR)(IH)) || \
  1654. (((PCHAR)(IE) + sizeof( INDEX_ENTRY )) > ((PCHAR)Add2Ptr((IH), (IH)->BytesAvailable)))) { \
  1655. NtfsRaiseStatus(IrpContext, STATUS_FILE_CORRUPT_ERROR, NULL, NULL ); \
  1656. } \
  1657. }
  1658. //
  1659. // MFT Bitmap attribute
  1660. //
  1661. // The MFT Bitmap is simply a normal attribute stream in which
  1662. // there is one bit to represent the allocation state of each File
  1663. // Record Segment in the MFT. Bit clear means free, and bit set
  1664. // means allocated.
  1665. //
  1666. // Whenever the MFT Data attribute is extended, the MFT Bitmap
  1667. // attribute must also be extended. If the bitmap is still in a
  1668. // file record segment for the MFT, then it must be extended and
  1669. // the new bits cleared. When the MFT Bitmap is in the Nonresident
  1670. // form, then the allocation should always be sufficient to store
  1671. // enough bits to describe the MFT, however ValidDataLength insures
  1672. // that newly allocated space to the MFT Bitmap has an initial
  1673. // value of all 0's. This means that if the MFT Bitmap is extended,
  1674. // the newly represented file record segments are automatically in
  1675. // the free state.
  1676. //
  1677. // No structure definition is required; the positional offset of
  1678. // the file record segment is exactly equal to the bit offset of
  1679. // its corresponding bit in the Bitmap.
  1680. //
  1681. //
  1682. // USN Journal Instance
  1683. //
  1684. // The following describe the current instance of the Usn journal.
  1685. //
  1686. typedef struct _USN_JOURNAL_INSTANCE {
  1687. #ifdef __cplusplus
  1688. CREATE_USN_JOURNAL_DATA JournalData;
  1689. #else // __cplusplus
  1690. CREATE_USN_JOURNAL_DATA;
  1691. #endif // __cplusplus
  1692. ULONGLONG JournalId;
  1693. USN LowestValidUsn;
  1694. } USN_JOURNAL_INSTANCE, *PUSN_JOURNAL_INSTANCE;
  1695. //
  1696. // Reparse point index keys.
  1697. //
  1698. // The index with all the reparse points that exist in a volume at a
  1699. // given time contains entries with keys of the form
  1700. // <reparse tag, file record id>.
  1701. // The data part of these records is empty.
  1702. //
  1703. typedef struct _REPARSE_INDEX_KEY {
  1704. //
  1705. // The tag of the reparse point.
  1706. //
  1707. ULONG FileReparseTag;
  1708. //
  1709. // The file record Id where the reparse point is set.
  1710. //
  1711. LARGE_INTEGER FileId;
  1712. } REPARSE_INDEX_KEY, *PREPARSE_INDEX_KEY;
  1713. //
  1714. // Ea Information attribute
  1715. //
  1716. // This attribute is only present if the file/directory also has an
  1717. // EA attribute. It is used to store common EA query information.
  1718. //
  1719. typedef struct _EA_INFORMATION {
  1720. //
  1721. // The size of buffer needed to pack these Ea's
  1722. //
  1723. USHORT PackedEaSize; // offset = 0x000
  1724. //
  1725. // This is the count of Ea's with their NEED_EA
  1726. // bit set.
  1727. //
  1728. USHORT NeedEaCount; // offset = 0x002
  1729. //
  1730. // The size of the buffer needed to return all Ea's
  1731. // in their unpacked form.
  1732. //
  1733. ULONG UnpackedEaSize; // offset = 0x004
  1734. } EA_INFORMATION; // sizeof = 0x008
  1735. typedef EA_INFORMATION *PEA_INFORMATION;
  1736. //
  1737. // Define the struture of the quota data in the quota index. The key for
  1738. // the quota index is the 32 bit owner id.
  1739. //
  1740. typedef struct _QUOTA_USER_DATA {
  1741. ULONG QuotaVersion;
  1742. ULONG QuotaFlags;
  1743. ULONGLONG QuotaUsed;
  1744. ULONGLONG QuotaChangeTime;
  1745. ULONGLONG QuotaThreshold;
  1746. ULONGLONG QuotaLimit;
  1747. ULONGLONG QuotaExceededTime;
  1748. SID QuotaSid;
  1749. } QUOTA_USER_DATA, *PQUOTA_USER_DATA;
  1750. //
  1751. // Define the size of the quota user data structure without the quota SID.
  1752. //
  1753. #define SIZEOF_QUOTA_USER_DATA FIELD_OFFSET(QUOTA_USER_DATA, QuotaSid)
  1754. //
  1755. // Define the current version of the quote user data.
  1756. //
  1757. #define QUOTA_USER_VERSION 2
  1758. //
  1759. // Define the quota flags.
  1760. //
  1761. #define QUOTA_FLAG_DEFAULT_LIMITS (0x00000001)
  1762. #define QUOTA_FLAG_LIMIT_REACHED (0x00000002)
  1763. #define QUOTA_FLAG_ID_DELETED (0x00000004)
  1764. #define QUOTA_FLAG_USER_MASK (0x00000007)
  1765. //
  1766. // The following flags are only stored in the quota defaults index entry.
  1767. //
  1768. #define QUOTA_FLAG_TRACKING_ENABLED (0x00000010)
  1769. #define QUOTA_FLAG_ENFORCEMENT_ENABLED (0x00000020)
  1770. #define QUOTA_FLAG_TRACKING_REQUESTED (0x00000040)
  1771. #define QUOTA_FLAG_LOG_THRESHOLD (0x00000080)
  1772. #define QUOTA_FLAG_LOG_LIMIT (0x00000100)
  1773. #define QUOTA_FLAG_OUT_OF_DATE (0x00000200)
  1774. #define QUOTA_FLAG_CORRUPT (0x00000400)
  1775. #define QUOTA_FLAG_PENDING_DELETES (0x00000800)
  1776. //
  1777. // Define special quota owner ids.
  1778. //
  1779. #define QUOTA_INVALID_ID 0x00000000
  1780. #define QUOTA_DEFAULTS_ID 0x00000001
  1781. #define QUOTA_FISRT_USER_ID 0x00000100
  1782. //
  1783. // Attribute Definition Table
  1784. //
  1785. // The following struct defines the columns of this table.
  1786. // Initially they will be stored as simple records, and ordered by
  1787. // Attribute Type Code.
  1788. //
  1789. typedef struct _ATTRIBUTE_DEFINITION_COLUMNS {
  1790. //
  1791. // Unicode attribute name.
  1792. //
  1793. WCHAR AttributeName[64]; // offset = 0x000
  1794. //
  1795. // Attribute Type Code.
  1796. //
  1797. ATTRIBUTE_TYPE_CODE AttributeTypeCode; // offset = 0x080
  1798. //
  1799. // Default Display Rule for this attribute
  1800. //
  1801. DISPLAY_RULE DisplayRule; // offset = 0x084
  1802. //
  1803. // Default Collation rule
  1804. //
  1805. COLLATION_RULE CollationRule; // offset = 0x088
  1806. //
  1807. // ATTRIBUTE_DEF_xxx flags
  1808. //
  1809. ULONG Flags; // offset = 0x08C
  1810. //
  1811. // Minimum Length for attribute, if present.
  1812. //
  1813. LONGLONG MinimumLength; // offset = 0x090
  1814. //
  1815. // Maximum Length for attribute.
  1816. //
  1817. LONGLONG MaximumLength; // offset = 0x098
  1818. } ATTRIBUTE_DEFINITION_COLUMNS; // sizeof = 0x0A0
  1819. typedef ATTRIBUTE_DEFINITION_COLUMNS *PATTRIBUTE_DEFINITION_COLUMNS;
  1820. //
  1821. // ATTRIBUTE_DEF_xxx flags
  1822. //
  1823. //
  1824. // This flag is set if the attribute may be indexed.
  1825. //
  1826. #define ATTRIBUTE_DEF_INDEXABLE (0x00000002)
  1827. //
  1828. // This flag is set if the attribute may occur more than once, such
  1829. // as is allowed for the File Name attribute.
  1830. //
  1831. #define ATTRIBUTE_DEF_DUPLICATES_ALLOWED (0x00000004)
  1832. //
  1833. // This flag is set if the value of the attribute may not be
  1834. // entirely null, i.e., all binary 0's.
  1835. //
  1836. #define ATTRIBUTE_DEF_MAY_NOT_BE_NULL (0x00000008)
  1837. //
  1838. // This attribute must be indexed, and no two attributes may exist
  1839. // with the same value in the same file record segment.
  1840. //
  1841. #define ATTRIBUTE_DEF_MUST_BE_INDEXED (0x00000010)
  1842. //
  1843. // This attribute must be named, and no two attributes may exist
  1844. // with the same name in the same file record segment.
  1845. //
  1846. #define ATTRIBUTE_DEF_MUST_BE_NAMED (0x00000020)
  1847. //
  1848. // This attribute must be in the Resident Form.
  1849. //
  1850. #define ATTRIBUTE_DEF_MUST_BE_RESIDENT (0x00000040)
  1851. //
  1852. // Modifications to this attribute should be logged even if the
  1853. // attribute is nonresident.
  1854. //
  1855. #define ATTRIBUTE_DEF_LOG_NONRESIDENT (0X00000080)
  1856. //
  1857. // MACROS
  1858. //
  1859. // Define some macros that are helpful for manipulating NTFS on
  1860. // disk structures.
  1861. //
  1862. //
  1863. // The following macro returns the first attribute record in a file
  1864. // record segment.
  1865. //
  1866. // PATTRIBUTE_RECORD_HEADER
  1867. // NtfsFirstAttribute (
  1868. // IN PFILE_RECORD_SEGMENT_HEADER FileRecord
  1869. // );
  1870. //
  1871. // The following macro takes a pointer to an attribute record (or
  1872. // attribute list entry) and returns a pointer to the next
  1873. // attribute record (or attribute list entry) in the list
  1874. //
  1875. // PVOID
  1876. // NtfsGetNextRecord (
  1877. // IN PATTRIB_RECORD or PATTRIB_LIST_ENTRY Struct
  1878. // );
  1879. //
  1880. //
  1881. // The following macro takes as input a attribute record or
  1882. // attribute list entry and initializes a string variable to the
  1883. // name found in the record or entry. The memory used for the
  1884. // string buffer is the memory found in the attribute.
  1885. //
  1886. // VOID
  1887. // NtfsInitializeStringFromAttribute (
  1888. // IN OUT PUNICODE_STRING Name,
  1889. // IN PATTRIBUTE_RECORD_HEADER Attribute
  1890. // );
  1891. //
  1892. // VOID
  1893. // NtfsInitializeStringFromEntry (
  1894. // IN OUT PUNICODE_STRING Name,
  1895. // IN PATTRIBUTE_LIST_ENTRY Entry
  1896. // );
  1897. //
  1898. //
  1899. // The following two macros assume resident form and should only be
  1900. // used when that state is known. They return a pointer to the
  1901. // value a resident attribute or a pointer to the byte one beyond
  1902. // the value.
  1903. //
  1904. // PVOID
  1905. // NtfsGetValue (
  1906. // IN PATTRIBUTE_RECORD_HEADER Attribute
  1907. // );
  1908. //
  1909. // PVOID
  1910. // NtfsGetBeyondValue (
  1911. // IN PATTRIBUTE_RECORD_HEADER Attribute
  1912. // );
  1913. //
  1914. // The following two macros return a boolean value indicating if
  1915. // the input attribute record is of the specified type code, or the
  1916. // indicated value. The equivalent routine to comparing attribute
  1917. // names cannot be defined as a macro and is declared in AttrSup.c
  1918. //
  1919. // BOOLEAN
  1920. // NtfsEqualAttributeTypeCode (
  1921. // IN PATTRIBUTE_RECORD_HEADER Attribute,
  1922. // IN ATTRIBUTE_TYPE_CODE Code
  1923. // );
  1924. //
  1925. // BOOLEAN
  1926. // NtfsEqualAttributeValue (
  1927. // IN PATTRIBUTE_RECORD_HEADER Attribute,
  1928. // IN PVOID Value,
  1929. // IN ULONG Length
  1930. // );
  1931. //
  1932. #define NtfsFirstAttribute(FRS) ( \
  1933. (PATTRIBUTE_RECORD_HEADER)((PCHAR)(FRS) + (FRS)->FirstAttributeOffset) \
  1934. )
  1935. #define NtfsGetNextRecord(STRUCT) ( \
  1936. (PVOID)((PUCHAR)(STRUCT) + (STRUCT)->RecordLength) \
  1937. )
  1938. #define NtfsCheckRecordBound(PTR, SPTR, SIZ) { \
  1939. if (((PCHAR)(PTR) < (PCHAR)(SPTR)) || ((PCHAR)(PTR) >= ((PCHAR)(SPTR) + (SIZ)))) { \
  1940. NtfsRaiseStatus(IrpContext, STATUS_FILE_CORRUPT_ERROR, NULL, NULL ); \
  1941. } \
  1942. }
  1943. #define NtfsInitializeStringFromAttribute(NAME,ATTRIBUTE) { \
  1944. (NAME)->Length = (USHORT)(ATTRIBUTE)->NameLength << 1; \
  1945. (NAME)->MaximumLength = (NAME)->Length; \
  1946. (NAME)->Buffer = (PWSTR)Add2Ptr((ATTRIBUTE), (ATTRIBUTE)->NameOffset); \
  1947. }
  1948. #define NtfsInitializeStringFromEntry(NAME,ENTRY) { \
  1949. (NAME)->Length = (USHORT)(ENTRY)->AttributeNameLength << 1; \
  1950. (NAME)->MaximumLength = (NAME)->Length; \
  1951. (NAME)->Buffer = (PWSTR)((ENTRY) + 1); \
  1952. }
  1953. #define NtfsGetValue(ATTRIBUTE) ( \
  1954. Add2Ptr((ATTRIBUTE), (ATTRIBUTE)->Form.Resident.ValueOffset) \
  1955. )
  1956. #define NtfsGetBeyondValue(ATTRIBUTE) ( \
  1957. Add2Ptr(NtfsGetValue(ATTRIBUTE), (ATTRIBUTE)->Form.Resident.ValueLength) \
  1958. )
  1959. #define NtfsEqualAttributeTypeCode(A,C) ( \
  1960. (C) == (A)->TypeCode \
  1961. )
  1962. #define NtfsEqualAttributeValue(A,V,L) ( \
  1963. NtfsIsAttributeResident(A) && \
  1964. (A)->Form.Resident.ValueLength == (L) && \
  1965. RtlEqualMemory(NtfsGetValue(A),(V),(L)) \
  1966. )
  1967. #pragma pack()
  1968. #endif // _NTFS_